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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
c5169421afd04a30fc9f410fa49ea4bdc9ac8505 | bbecf0f1968d1fba4124103e4f6b55251d08e9c4 | /src/group_theory/congruence.lean | de1dabcc5ebe2e53f31be2168497a14d4e016890 | [
"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 | 48,183 | lean | /-
Copyright (c) 2019 Amelia Livingston. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Amelia Livingston
-/
import data.setoid.basic
import algebra.group.pi
import algebra.group.prod
import data.equiv.mul_add
import group_theory.submonoid.operations
/-!
# Congruence relations
This file defines congruence relations: equivalence relations that preserve a binary operation,
which in this case is multiplication or addition. The principal definition is a `structure`
extending a `setoid` (an equivalence relation), and the inductive definition of the smallest
congruence relation containing a binary relation is also given (see `con_gen`).
The file also proves basic properties of the quotient of a type by a congruence relation, and the
complete lattice of congruence relations on a type. We then establish an order-preserving bijection
between the set of congruence relations containing a congruence relation `c` and the set of
congruence relations on the quotient by `c`.
The second half of the file concerns congruence relations on monoids, in which case the
quotient by the congruence relation is also a monoid. There are results about the universal
property of quotients of monoids, and the isomorphism theorems for monoids.
## Implementation notes
The inductive definition of a congruence relation could be a nested inductive type, defined using
the equivalence closure of a binary relation `eqv_gen`, but the recursor generated does not work.
A nested inductive definition could conceivably shorten proofs, because they would allow invocation
of the corresponding lemmas about `eqv_gen`.
The lemmas `refl`, `symm` and `trans` are not tagged with `@[refl]`, `@[symm]`, and `@[trans]`
respectively as these tags do not work on a structure coerced to a binary relation.
There is a coercion from elements of a type to the element's equivalence class under a
congruence relation.
A congruence relation on a monoid `M` can be thought of as a submonoid of `M × M` for which
membership is an equivalence relation, but whilst this fact is established in the file, it is not
used, since this perspective adds more layers of definitional unfolding.
## Tags
congruence, congruence relation, quotient, quotient by congruence relation, monoid,
quotient monoid, isomorphism theorems
-/
variables (M : Type*) {N : Type*} {P : Type*}
open function setoid
/-- A congruence relation on a type with an addition is an equivalence relation which
preserves addition. -/
structure add_con [has_add M] extends setoid M :=
(add' : ∀ {w x y z}, r w x → r y z → r (w + y) (x + z))
/-- A congruence relation on a type with a multiplication is an equivalence relation which
preserves multiplication. -/
@[to_additive add_con] structure con [has_mul M] extends setoid M :=
(mul' : ∀ {w x y z}, r w x → r y z → r (w * y) (x * z))
/-- The equivalence relation underlying an additive congruence relation. -/
add_decl_doc add_con.to_setoid
/-- The equivalence relation underlying a multiplicative congruence relation. -/
add_decl_doc con.to_setoid
variables {M}
/-- The inductively defined smallest additive congruence relation containing a given binary
relation. -/
inductive add_con_gen.rel [has_add M] (r : M → M → Prop) : M → M → Prop
| of : Π x y, r x y → add_con_gen.rel x y
| refl : Π x, add_con_gen.rel x x
| symm : Π x y, add_con_gen.rel x y → add_con_gen.rel y x
| trans : Π x y z, add_con_gen.rel x y → add_con_gen.rel y z → add_con_gen.rel x z
| add : Π w x y z, add_con_gen.rel w x → add_con_gen.rel y z → add_con_gen.rel (w + y) (x + z)
/-- The inductively defined smallest multiplicative congruence relation containing a given binary
relation. -/
@[to_additive add_con_gen.rel]
inductive con_gen.rel [has_mul M] (r : M → M → Prop) : M → M → Prop
| of : Π x y, r x y → con_gen.rel x y
| refl : Π x, con_gen.rel x x
| symm : Π x y, con_gen.rel x y → con_gen.rel y x
| trans : Π x y z, con_gen.rel x y → con_gen.rel y z → con_gen.rel x z
| mul : Π w x y z, con_gen.rel w x → con_gen.rel y z → con_gen.rel (w * y) (x * z)
/-- The inductively defined smallest multiplicative congruence relation containing a given binary
relation. -/
@[to_additive add_con_gen "The inductively defined smallest additive congruence relation containing
a given binary relation."]
def con_gen [has_mul M] (r : M → M → Prop) : con M :=
⟨⟨con_gen.rel r, ⟨con_gen.rel.refl, con_gen.rel.symm, con_gen.rel.trans⟩⟩, con_gen.rel.mul⟩
namespace con
section
variables [has_mul M] [has_mul N] [has_mul P] (c : con M)
@[to_additive]
instance : inhabited (con M) :=
⟨con_gen empty_relation⟩
/-- A coercion from a congruence relation to its underlying binary relation. -/
@[to_additive "A coercion from an additive congruence relation to its underlying binary relation."]
instance : has_coe_to_fun (con M) := ⟨_, λ c, λ x y, @setoid.r _ c.to_setoid x y⟩
@[simp, to_additive] lemma rel_eq_coe (c : con M) : c.r = c := rfl
/-- Congruence relations are reflexive. -/
@[to_additive "Additive congruence relations are reflexive."]
protected lemma refl (x) : c x x := c.to_setoid.refl' x
/-- Congruence relations are symmetric. -/
@[to_additive "Additive congruence relations are symmetric."]
protected lemma symm : ∀ {x y}, c x y → c y x := λ _ _ h, c.to_setoid.symm' h
/-- Congruence relations are transitive. -/
@[to_additive "Additive congruence relations are transitive."]
protected lemma trans : ∀ {x y z}, c x y → c y z → c x z :=
λ _ _ _ h, c.to_setoid.trans' h
/-- Multiplicative congruence relations preserve multiplication. -/
@[to_additive "Additive congruence relations preserve addition."]
protected lemma mul : ∀ {w x y z}, c w x → c y z → c (w * y) (x * z) :=
λ _ _ _ _ h1 h2, c.mul' h1 h2
@[simp, to_additive] lemma rel_mk {s : setoid M} {h a b} :
con.mk s h a b ↔ r a b :=
iff.rfl
/-- Given a type `M` with a multiplication, a congruence relation `c` on `M`, and elements of `M`
`x, y`, `(x, y) ∈ M × M` iff `x` is related to `y` by `c`. -/
@[to_additive "Given a type `M` with an addition, `x, y ∈ M`, and an additive congruence relation
`c` on `M`, `(x, y) ∈ M × M` iff `x` is related to `y` by `c`."]
instance : has_mem (M × M) (con M) := ⟨λ x c, c x.1 x.2⟩
variables {c}
/-- The map sending a congruence relation to its underlying binary relation is injective. -/
@[to_additive "The map sending an additive congruence relation to its underlying binary relation
is injective."]
lemma ext' {c d : con M} (H : c.r = d.r) : c = d :=
by { rcases c with ⟨⟨⟩⟩, rcases d with ⟨⟨⟩⟩, cases H, congr, }
/-- Extensionality rule for congruence relations. -/
@[ext, to_additive "Extensionality rule for additive congruence relations."]
lemma ext {c d : con M} (H : ∀ x y, c x y ↔ d x y) : c = d :=
ext' $ by ext; apply H
/-- The map sending a congruence relation to its underlying equivalence relation is injective. -/
@[to_additive "The map sending an additive congruence relation to its underlying equivalence
relation is injective."]
lemma to_setoid_inj {c d : con M} (H : c.to_setoid = d.to_setoid) : c = d :=
ext $ ext_iff.1 H
/-- Iff version of extensionality rule for congruence relations. -/
@[to_additive "Iff version of extensionality rule for additive congruence relations."]
lemma ext_iff {c d : con M} : (∀ x y, c x y ↔ d x y) ↔ c = d :=
⟨ext, λ h _ _, h ▸ iff.rfl⟩
/-- Two congruence relations are equal iff their underlying binary relations are equal. -/
@[to_additive "Two additive congruence relations are equal iff their underlying binary relations
are equal."]
lemma ext'_iff {c d : con M} : c.r = d.r ↔ c = d :=
⟨ext', λ h, h ▸ rfl⟩
/-- The kernel of a multiplication-preserving function as a congruence relation. -/
@[to_additive "The kernel of an addition-preserving function as an additive congruence relation."]
def mul_ker (f : M → P) (h : ∀ x y, f (x * y) = f x * f y) : con M :=
{ to_setoid := setoid.ker f,
mul' := λ _ _ _ _ h1 h2, by { dsimp [setoid.ker] at *, rw [h, h1, h2, h], } }
/-- Given types with multiplications `M, N`, the product of two congruence relations `c` on `M` and
`d` on `N`: `(x₁, x₂), (y₁, y₂) ∈ M × N` are related by `c.prod d` iff `x₁` is related to `y₁`
by `c` and `x₂` is related to `y₂` by `d`. -/
@[to_additive prod "Given types with additions `M, N`, the product of two congruence relations
`c` on `M` and `d` on `N`: `(x₁, x₂), (y₁, y₂) ∈ M × N` are related by `c.prod d` iff `x₁`
is related to `y₁` by `c` and `x₂` is related to `y₂` by `d`."]
protected def prod (c : con M) (d : con N) : con (M × N) :=
{ mul' := λ _ _ _ _ h1 h2, ⟨c.mul h1.1 h2.1, d.mul h1.2 h2.2⟩, ..c.to_setoid.prod d.to_setoid }
/-- The product of an indexed collection of congruence relations. -/
@[to_additive "The product of an indexed collection of additive congruence relations."]
def pi {ι : Type*} {f : ι → Type*} [Π i, has_mul (f i)]
(C : Π i, con (f i)) : con (Π i, f i) :=
{ mul' := λ _ _ _ _ h1 h2 i, (C i).mul (h1 i) (h2 i), ..@pi_setoid _ _ $ λ i, (C i).to_setoid }
variables (c)
-- Quotients
/-- Defining the quotient by a congruence relation of a type with a multiplication. -/
@[to_additive "Defining the quotient by an additive congruence relation of a type with
an addition."]
protected def quotient := quotient $ c.to_setoid
/-- Coercion from a type with a multiplication to its quotient by a congruence relation.
See Note [use has_coe_t]. -/
@[to_additive "Coercion from a type with an addition to its quotient by an additive congruence
relation", priority 0]
instance : has_coe_t M c.quotient := ⟨@quotient.mk _ c.to_setoid⟩
/-- The quotient by a decidable congruence relation has decidable equality. -/
@[to_additive "The quotient by a decidable additive congruence relation has decidable equality."]
instance [d : ∀ a b, decidable (c a b)] : decidable_eq c.quotient :=
@quotient.decidable_eq M c.to_setoid d
@[simp, to_additive] lemma quot_mk_eq_coe {M : Type*} [has_mul M] (c : con M) (x : M) :
quot.mk c x = (x : c.quotient) :=
rfl
/-- The function on the quotient by a congruence relation `c` induced by a function that is
constant on `c`'s equivalence classes. -/
@[elab_as_eliminator, to_additive "The function on the quotient by a congruence relation `c`
induced by a function that is constant on `c`'s equivalence classes."]
protected def lift_on {β} {c : con M} (q : c.quotient) (f : M → β)
(h : ∀ a b, c a b → f a = f b) : β := quotient.lift_on' q f h
/-- The binary function on the quotient by a congruence relation `c` induced by a binary function
that is constant on `c`'s equivalence classes. -/
@[elab_as_eliminator, to_additive "The binary function on the quotient by a congruence relation `c`
induced by a binary function that is constant on `c`'s equivalence classes."]
protected def lift_on₂ {β} {c : con M} (q r : c.quotient) (f : M → M → β)
(h : ∀ a₁ a₂ b₁ b₂, c a₁ b₁ → c a₂ b₂ → f a₁ a₂ = f b₁ b₂) : β := quotient.lift_on₂' q r f h
/-- A version of `quotient.hrec_on₂'` for quotients by `con`. -/
@[to_additive "A version of `quotient.hrec_on₂'` for quotients by `add_con`."]
protected def hrec_on₂ {cM : con M} {cN : con N} {φ : cM.quotient → cN.quotient → Sort*}
(a : cM.quotient) (b : cN.quotient)
(f : Π (x : M) (y : N), φ x y) (h : ∀ x y x' y', cM x x' → cN y y' → f x y == f x' y') :
φ a b :=
quotient.hrec_on₂' a b f h
@[simp, to_additive] lemma hrec_on₂_coe {cM : con M} {cN : con N}
{φ : cM.quotient → cN.quotient → Sort*} (a : M) (b : N)
(f : Π (x : M) (y : N), φ x y) (h : ∀ x y x' y', cM x x' → cN y y' → f x y == f x' y') :
con.hrec_on₂ ↑a ↑b f h = f a b :=
rfl
variables {c}
/-- The inductive principle used to prove propositions about the elements of a quotient by a
congruence relation. -/
@[elab_as_eliminator, to_additive "The inductive principle used to prove propositions about
the elements of a quotient by an additive congruence relation."]
protected lemma induction_on {C : c.quotient → Prop} (q : c.quotient) (H : ∀ x : M, C x) : C q :=
quotient.induction_on' q H
/-- A version of `con.induction_on` for predicates which take two arguments. -/
@[elab_as_eliminator, to_additive "A version of `add_con.induction_on` for predicates which take
two arguments."]
protected lemma induction_on₂ {d : con N} {C : c.quotient → d.quotient → Prop}
(p : c.quotient) (q : d.quotient) (H : ∀ (x : M) (y : N), C x y) : C p q :=
quotient.induction_on₂' p q H
variables (c)
/-- Two elements are related by a congruence relation `c` iff they are represented by the same
element of the quotient by `c`. -/
@[simp, to_additive "Two elements are related by an additive congruence relation `c` iff they
are represented by the same element of the quotient by `c`."]
protected lemma eq {a b : M} : (a : c.quotient) = b ↔ c a b :=
quotient.eq'
/-- The multiplication induced on the quotient by a congruence relation on a type with a
multiplication. -/
@[to_additive "The addition induced on the quotient by an additive congruence relation on a type
with an addition."]
instance has_mul : has_mul c.quotient :=
⟨λ x y, quotient.lift_on₂' x y (λ w z, ((w * z : M) : c.quotient))
$ λ _ _ _ _ h1 h2, c.eq.2 $ c.mul h1 h2⟩
/-- The kernel of the quotient map induced by a congruence relation `c` equals `c`. -/
@[simp, to_additive "The kernel of the quotient map induced by an additive congruence relation
`c` equals `c`."]
lemma mul_ker_mk_eq : mul_ker (coe : M → c.quotient) (λ x y, rfl) = c :=
ext $ λ x y, quotient.eq'
variables {c}
/-- The coercion to the quotient of a congruence relation commutes with multiplication (by
definition). -/
@[simp, to_additive "The coercion to the quotient of an additive congruence relation commutes with
addition (by definition)."]
lemma coe_mul (x y : M) : (↑(x * y) : c.quotient) = ↑x * ↑y := rfl
/-- Definition of the function on the quotient by a congruence relation `c` induced by a function
that is constant on `c`'s equivalence classes. -/
@[simp, to_additive "Definition of the function on the quotient by an additive congruence
relation `c` induced by a function that is constant on `c`'s equivalence classes."]
protected lemma lift_on_coe {β} (c : con M) (f : M → β)
(h : ∀ a b, c a b → f a = f b) (x : M) :
con.lift_on (x : c.quotient) f h = f x := rfl
/-- Makes an isomorphism of quotients by two congruence relations, given that the relations are
equal. -/
@[to_additive "Makes an additive isomorphism of quotients by two additive congruence relations,
given that the relations are equal."]
protected def congr {c d : con M} (h : c = d) : c.quotient ≃* d.quotient :=
{ map_mul' := λ x y, by rcases x; rcases y; refl,
..quotient.congr (equiv.refl M) $ by apply ext_iff.2 h }
-- The complete lattice of congruence relations on a type
/-- For congruence relations `c, d` on a type `M` with a multiplication, `c ≤ d` iff `∀ x y ∈ M`,
`x` is related to `y` by `d` if `x` is related to `y` by `c`. -/
@[to_additive "For additive congruence relations `c, d` on a type `M` with an addition, `c ≤ d` iff
`∀ x y ∈ M`, `x` is related to `y` by `d` if `x` is related to `y` by `c`."]
instance : has_le (con M) := ⟨λ c d, ∀ ⦃x y⦄, c x y → d x y⟩
/-- Definition of `≤` for congruence relations. -/
@[to_additive "Definition of `≤` for additive congruence relations."]
theorem le_def {c d : con M} : c ≤ d ↔ ∀ {x y}, c x y → d x y := iff.rfl
/-- The infimum of a set of congruence relations on a given type with a multiplication. -/
@[to_additive "The infimum of a set of additive congruence relations on a given type with
an addition."]
instance : has_Inf (con M) :=
⟨λ S, ⟨⟨λ x y, ∀ c : con M, c ∈ S → c x y,
⟨λ x c hc, c.refl x, λ _ _ h c hc, c.symm $ h c hc,
λ _ _ _ h1 h2 c hc, c.trans (h1 c hc) $ h2 c hc⟩⟩,
λ _ _ _ _ h1 h2 c hc, c.mul (h1 c hc) $ h2 c hc⟩⟩
/-- The infimum of a set of congruence relations is the same as the infimum of the set's image
under the map to the underlying equivalence relation. -/
@[to_additive "The infimum of a set of additive congruence relations is the same as the infimum of
the set's image under the map to the underlying equivalence relation."]
lemma Inf_to_setoid (S : set (con M)) : (Inf S).to_setoid = Inf (to_setoid '' S) :=
setoid.ext' $ λ x y, ⟨λ h r ⟨c, hS, hr⟩, by rw ←hr; exact h c hS,
λ h c hS, h c.to_setoid ⟨c, hS, rfl⟩⟩
/-- The infimum of a set of congruence relations is the same as the infimum of the set's image
under the map to the underlying binary relation. -/
@[to_additive "The infimum of a set of additive congruence relations is the same as the infimum
of the set's image under the map to the underlying binary relation."]
lemma Inf_def (S : set (con M)) : ⇑(Inf S) = Inf (@set.image (con M) (M → M → Prop) coe_fn S) :=
by { ext, simp only [Inf_image, infi_apply, infi_Prop_eq], refl }
@[to_additive]
instance : partial_order (con M) :=
{ le := (≤),
lt := λ c d, c ≤ d ∧ ¬d ≤ c,
le_refl := λ c _ _, id,
le_trans := λ c1 c2 c3 h1 h2 x y h, h2 $ h1 h,
lt_iff_le_not_le := λ _ _, iff.rfl,
le_antisymm := λ c d hc hd, ext $ λ x y, ⟨λ h, hc h, λ h, hd h⟩ }
/-- The complete lattice of congruence relations on a given type with a multiplication. -/
@[to_additive "The complete lattice of additive congruence relations on a given type with
an addition."]
instance : complete_lattice (con M) :=
{ inf := λ c d, ⟨(c.to_setoid ⊓ d.to_setoid), λ _ _ _ _ h1 h2, ⟨c.mul h1.1 h2.1, d.mul h1.2 h2.2⟩⟩,
inf_le_left := λ _ _ _ _ h, h.1,
inf_le_right := λ _ _ _ _ h, h.2,
le_inf := λ _ _ _ hb hc _ _ h, ⟨hb h, hc h⟩,
top := { mul' := by tauto, ..setoid.complete_lattice.top},
le_top := λ _ _ _ h, trivial,
bot := { mul' := λ _ _ _ _ h1 h2, h1 ▸ h2 ▸ rfl, ..setoid.complete_lattice.bot},
bot_le := λ c x y h, h ▸ c.refl x,
.. complete_lattice_of_Inf (con M) $ assume s,
⟨λ r hr x y h, (h : ∀ r ∈ s, (r : con M) x y) r hr, λ r hr x y h r' hr', hr hr' h⟩ }
/-- The infimum of two congruence relations equals the infimum of the underlying binary
operations. -/
@[to_additive "The infimum of two additive congruence relations equals the infimum of the
underlying binary operations."]
lemma inf_def {c d : con M} : (c ⊓ d).r = c.r ⊓ d.r := rfl
/-- Definition of the infimum of two congruence relations. -/
@[to_additive "Definition of the infimum of two additive congruence relations."]
theorem inf_iff_and {c d : con M} {x y} : (c ⊓ d) x y ↔ c x y ∧ d x y := iff.rfl
/-- The inductively defined smallest congruence relation containing a binary relation `r` equals
the infimum of the set of congruence relations containing `r`. -/
@[to_additive add_con_gen_eq "The inductively defined smallest additive congruence relation
containing a binary relation `r` equals the infimum of the set of additive congruence relations
containing `r`."]
theorem con_gen_eq (r : M → M → Prop) :
con_gen r = Inf {s : con M | ∀ x y, r x y → s x y} :=
le_antisymm
(λ x y H, con_gen.rel.rec_on H (λ _ _ h _ hs, hs _ _ h) (con.refl _) (λ _ _ _, con.symm _)
(λ _ _ _ _ _, con.trans _)
$ λ w x y z _ _ h1 h2 c hc, c.mul (h1 c hc) $ h2 c hc)
(Inf_le (λ _ _, con_gen.rel.of _ _))
/-- The smallest congruence relation containing a binary relation `r` is contained in any
congruence relation containing `r`. -/
@[to_additive add_con_gen_le "The smallest additive congruence relation containing a binary
relation `r` is contained in any additive congruence relation containing `r`."]
theorem con_gen_le {r : M → M → Prop} {c : con M} (h : ∀ x y, r x y → @setoid.r _ c.to_setoid x y) :
con_gen r ≤ c :=
by rw con_gen_eq; exact Inf_le h
/-- Given binary relations `r, s` with `r` contained in `s`, the smallest congruence relation
containing `s` contains the smallest congruence relation containing `r`. -/
@[to_additive add_con_gen_mono "Given binary relations `r, s` with `r` contained in `s`, the
smallest additive congruence relation containing `s` contains the smallest additive congruence
relation containing `r`."]
theorem con_gen_mono {r s : M → M → Prop} (h : ∀ x y, r x y → s x y) :
con_gen r ≤ con_gen s :=
con_gen_le $ λ x y hr, con_gen.rel.of _ _ $ h x y hr
/-- Congruence relations equal the smallest congruence relation in which they are contained. -/
@[simp, to_additive add_con_gen_of_add_con "Additive congruence relations equal the smallest
additive congruence relation in which they are contained."]
lemma con_gen_of_con (c : con M) : con_gen c = c :=
le_antisymm (by rw con_gen_eq; exact Inf_le (λ _ _, id)) con_gen.rel.of
/-- The map sending a binary relation to the smallest congruence relation in which it is
contained is idempotent. -/
@[simp, to_additive add_con_gen_idem "The map sending a binary relation to the smallest additive
congruence relation in which it is contained is idempotent."]
lemma con_gen_idem (r : M → M → Prop) :
con_gen (con_gen r) = con_gen r :=
con_gen_of_con _
/-- The supremum of congruence relations `c, d` equals the smallest congruence relation containing
the binary relation '`x` is related to `y` by `c` or `d`'. -/
@[to_additive sup_eq_add_con_gen "The supremum of additive congruence relations `c, d` equals the
smallest additive congruence relation containing the binary relation '`x` is related to `y`
by `c` or `d`'."]
lemma sup_eq_con_gen (c d : con M) :
c ⊔ d = con_gen (λ x y, c x y ∨ d x y) :=
begin
rw con_gen_eq,
apply congr_arg Inf,
simp only [le_def, or_imp_distrib, ← forall_and_distrib]
end
/-- The supremum of two congruence relations equals the smallest congruence relation containing
the supremum of the underlying binary operations. -/
@[to_additive "The supremum of two additive congruence relations equals the smallest additive
congruence relation containing the supremum of the underlying binary operations."]
lemma sup_def {c d : con M} : c ⊔ d = con_gen (c.r ⊔ d.r) :=
by rw sup_eq_con_gen; refl
/-- The supremum of a set of congruence relations `S` equals the smallest congruence relation
containing the binary relation 'there exists `c ∈ S` such that `x` is related to `y` by
`c`'. -/
@[to_additive Sup_eq_add_con_gen "The supremum of a set of additive congruence relations `S` equals
the smallest additive congruence relation containing the binary relation 'there exists `c ∈ S`
such that `x` is related to `y` by `c`'."]
lemma Sup_eq_con_gen (S : set (con M)) :
Sup S = con_gen (λ x y, ∃ c : con M, c ∈ S ∧ c x y) :=
begin
rw con_gen_eq,
apply congr_arg Inf,
ext,
exact ⟨λ h _ _ ⟨r, hr⟩, h hr.1 hr.2,
λ h r hS _ _ hr, h _ _ ⟨r, hS, hr⟩⟩,
end
/-- The supremum of a set of congruence relations is the same as the smallest congruence relation
containing the supremum of the set's image under the map to the underlying binary relation. -/
@[to_additive "The supremum of a set of additive congruence relations is the same as the smallest
additive congruence relation containing the supremum of the set's image under the map to the
underlying binary relation."]
lemma Sup_def {S : set (con M)} :
Sup S = con_gen (Sup (@set.image (con M) (M → M → Prop) coe_fn S)) :=
begin
rw [Sup_eq_con_gen, Sup_image],
congr' with x y,
simp only [Sup_image, supr_apply, supr_Prop_eq, exists_prop, rel_eq_coe]
end
variables (M)
/-- There is a Galois insertion of congruence relations on a type with a multiplication `M` into
binary relations on `M`. -/
@[to_additive "There is a Galois insertion of additive congruence relations on a type with
an addition `M` into binary relations on `M`."]
protected noncomputable def gi :
@galois_insertion (M → M → Prop) (con M) _ _ con_gen coe_fn :=
{ choice := λ r h, con_gen r,
gc := λ r c, ⟨λ H _ _ h, H $ con_gen.rel.of _ _ h, λ H, con_gen_of_con c ▸ con_gen_mono H⟩,
le_l_u := λ x, (con_gen_of_con x).symm ▸ le_refl x,
choice_eq := λ _ _, rfl }
variables {M} (c)
/-- Given a function `f`, the smallest congruence relation containing the binary relation on `f`'s
image defined by '`x ≈ y` iff the elements of `f⁻¹(x)` are related to the elements of `f⁻¹(y)`
by a congruence relation `c`.' -/
@[to_additive "Given a function `f`, the smallest additive congruence relation containing the
binary relation on `f`'s image defined by '`x ≈ y` iff the elements of `f⁻¹(x)` are related to the
elements of `f⁻¹(y)` by an additive congruence relation `c`.'"]
def map_gen (f : M → N) : con N :=
con_gen $ λ x y, ∃ a b, f a = x ∧ f b = y ∧ c a b
/-- Given a surjective multiplicative-preserving function `f` whose kernel is contained in a
congruence relation `c`, the congruence relation on `f`'s codomain defined by '`x ≈ y` iff the
elements of `f⁻¹(x)` are related to the elements of `f⁻¹(y)` by `c`.' -/
@[to_additive "Given a surjective addition-preserving function `f` whose kernel is contained in
an additive congruence relation `c`, the additive congruence relation on `f`'s codomain defined
by '`x ≈ y` iff the elements of `f⁻¹(x)` are related to the elements of `f⁻¹(y)` by `c`.'"]
def map_of_surjective (f : M → N) (H : ∀ x y, f (x * y) = f x * f y) (h : mul_ker f H ≤ c)
(hf : surjective f) : con N :=
{ mul' := λ w x y z ⟨a, b, hw, hx, h1⟩ ⟨p, q, hy, hz, h2⟩,
⟨a * p, b * q, by rw [H, hw, hy], by rw [H, hx, hz], c.mul h1 h2⟩,
..c.to_setoid.map_of_surjective f h hf }
/-- A specialization of 'the smallest congruence relation containing a congruence relation `c`
equals `c`'. -/
@[to_additive "A specialization of 'the smallest additive congruence relation containing
an additive congruence relation `c` equals `c`'."]
lemma map_of_surjective_eq_map_gen {c : con M} {f : M → N} (H : ∀ x y, f (x * y) = f x * f y)
(h : mul_ker f H ≤ c) (hf : surjective f) :
c.map_gen f = c.map_of_surjective f H h hf :=
by rw ←con_gen_of_con (c.map_of_surjective f H h hf); refl
/-- Given types with multiplications `M, N` and a congruence relation `c` on `N`, a
multiplication-preserving map `f : M → N` induces a congruence relation on `f`'s domain
defined by '`x ≈ y` iff `f(x)` is related to `f(y)` by `c`.' -/
@[to_additive "Given types with additions `M, N` and an additive congruence relation `c` on `N`,
an addition-preserving map `f : M → N` induces an additive congruence relation on `f`'s domain
defined by '`x ≈ y` iff `f(x)` is related to `f(y)` by `c`.' "]
def comap (f : M → N) (H : ∀ x y, f (x * y) = f x * f y) (c : con N) : con M :=
{ mul' := λ w x y z h1 h2, show c (f (w * y)) (f (x * z)), by rw [H, H]; exact c.mul h1 h2,
..c.to_setoid.comap f }
@[simp, to_additive] lemma comap_rel {f : M → N} (H : ∀ x y, f (x * y) = f x * f y)
{c : con N} {x y : M} :
comap f H c x y ↔ c (f x) (f y) :=
iff.rfl
section
open quotient
/-- Given a congruence relation `c` on a type `M` with a multiplication, the order-preserving
bijection between the set of congruence relations containing `c` and the congruence relations
on the quotient of `M` by `c`. -/
@[to_additive "Given an additive congruence relation `c` on a type `M` with an addition,
the order-preserving bijection between the set of additive congruence relations containing `c` and
the additive congruence relations on the quotient of `M` by `c`."]
def correspondence : {d // c ≤ d} ≃o (con c.quotient) :=
{ to_fun := λ d, d.1.map_of_surjective coe _
(by rw mul_ker_mk_eq; exact d.2) $ @exists_rep _ c.to_setoid,
inv_fun := λ d, ⟨comap (coe : M → c.quotient) (λ x y, rfl) d, λ _ _ h,
show d _ _, by rw c.eq.2 h; exact d.refl _ ⟩,
left_inv := λ d, subtype.ext_iff_val.2 $ ext $ λ _ _,
⟨λ h, let ⟨a, b, hx, hy, H⟩ := h in
d.1.trans (d.1.symm $ d.2 $ c.eq.1 hx) $ d.1.trans H $ d.2 $ c.eq.1 hy,
λ h, ⟨_, _, rfl, rfl, h⟩⟩,
right_inv := λ d, let Hm : mul_ker (coe : M → c.quotient) (λ x y, rfl) ≤
comap (coe : M → c.quotient) (λ x y, rfl) d :=
λ x y h, show d _ _, by rw mul_ker_mk_eq at h; exact c.eq.2 h ▸ d.refl _ in
ext $ λ x y, ⟨λ h, let ⟨a, b, hx, hy, H⟩ := h in hx ▸ hy ▸ H,
con.induction_on₂ x y $ λ w z h, ⟨w, z, rfl, rfl, h⟩⟩,
map_rel_iff' := λ s t, ⟨λ h _ _ hs, let ⟨a, b, hx, hy, ht⟩ := h ⟨_, _, rfl, rfl, hs⟩ in
t.1.trans (t.1.symm $ t.2 $ eq_rel.1 hx) $ t.1.trans ht $ t.2 $ eq_rel.1 hy,
λ h _ _ hs, let ⟨a, b, hx, hy, Hs⟩ := hs in ⟨a, b, hx, hy, h Hs⟩⟩ }
end
end
section mul_one_class
variables {M} [mul_one_class M] [mul_one_class N] [mul_one_class P] (c : con M)
/-- The quotient of a monoid by a congruence relation is a monoid. -/
@[to_additive "The quotient of an `add_monoid` by an additive congruence relation is
an `add_monoid`."]
instance mul_one_class : mul_one_class c.quotient :=
{ one := ((1 : M) : c.quotient),
mul := (*),
mul_one := λ x, quotient.induction_on' x $ λ _, congr_arg coe $ mul_one _,
one_mul := λ x, quotient.induction_on' x $ λ _, congr_arg coe $ one_mul _ }
variables {c}
/-- The 1 of the quotient of a monoid by a congruence relation is the equivalence class of the
monoid's 1. -/
@[simp, to_additive "The 0 of the quotient of an `add_monoid` by an additive congruence relation
is the equivalence class of the `add_monoid`'s 0."]
lemma coe_one : ((1 : M) : c.quotient) = 1 := rfl
variables (M c)
/-- The submonoid of `M × M` defined by a congruence relation on a monoid `M`. -/
@[to_additive "The `add_submonoid` of `M × M` defined by an additive congruence
relation on an `add_monoid` `M`."]
protected def submonoid : submonoid (M × M) :=
{ carrier := { x | c x.1 x.2 },
one_mem' := c.iseqv.1 1,
mul_mem' := λ _ _, c.mul }
variables {M c}
/-- The congruence relation on a monoid `M` from a submonoid of `M × M` for which membership
is an equivalence relation. -/
@[to_additive "The additive congruence relation on an `add_monoid` `M` from
an `add_submonoid` of `M × M` for which membership is an equivalence relation."]
def of_submonoid (N : submonoid (M × M)) (H : equivalence (λ x y, (x, y) ∈ N)) : con M :=
{ r := λ x y, (x, y) ∈ N,
iseqv := H,
mul' := λ _ _ _ _, N.mul_mem }
/-- Coercion from a congruence relation `c` on a monoid `M` to the submonoid of `M × M` whose
elements are `(x, y)` such that `x` is related to `y` by `c`. -/
@[to_additive "Coercion from a congruence relation `c` on an `add_monoid` `M`
to the `add_submonoid` of `M × M` whose elements are `(x, y)` such that `x`
is related to `y` by `c`."]
instance to_submonoid : has_coe (con M) (submonoid (M × M)) := ⟨λ c, c.submonoid M⟩
@[to_additive] lemma mem_coe {c : con M} {x y} :
(x, y) ∈ (↑c : submonoid (M × M)) ↔ (x, y) ∈ c := iff.rfl
@[to_additive]
theorem to_submonoid_inj (c d : con M) (H : (c : submonoid (M × M)) = d) : c = d :=
ext $ λ x y, show (x, y) ∈ (c : submonoid (M × M)) ↔ (x, y) ∈ ↑d, by rw H
@[to_additive]
lemma le_iff {c d : con M} : c ≤ d ↔ (c : submonoid (M × M)) ≤ d :=
⟨λ h x H, h H, λ h x y hc, h $ show (x, y) ∈ c, from hc⟩
/-- The kernel of a monoid homomorphism as a congruence relation. -/
@[to_additive "The kernel of an `add_monoid` homomorphism as an additive congruence relation."]
def ker (f : M →* P) : con M := mul_ker f f.3
/-- The definition of the congruence relation defined by a monoid homomorphism's kernel. -/
@[simp, to_additive "The definition of the additive congruence relation defined by an `add_monoid`
homomorphism's kernel."]
lemma ker_rel (f : M →* P) {x y} : ker f x y ↔ f x = f y := iff.rfl
/-- There exists an element of the quotient of a monoid by a congruence relation (namely 1). -/
@[to_additive "There exists an element of the quotient of an `add_monoid` by a congruence relation
(namely 0)."]
instance quotient.inhabited : inhabited c.quotient := ⟨((1 : M) : c.quotient)⟩
variables (c)
/-- The natural homomorphism from a monoid to its quotient by a congruence relation. -/
@[to_additive "The natural homomorphism from an `add_monoid` to its quotient by an additive
congruence relation."]
def mk' : M →* c.quotient := ⟨coe, rfl, λ _ _, rfl⟩
variables (x y : M)
/-- The kernel of the natural homomorphism from a monoid to its quotient by a congruence
relation `c` equals `c`. -/
@[simp, to_additive "The kernel of the natural homomorphism from an `add_monoid` to its quotient by
an additive congruence relation `c` equals `c`."]
lemma mk'_ker : ker c.mk' = c := ext $ λ _ _, c.eq
variables {c}
/-- The natural homomorphism from a monoid to its quotient by a congruence relation is
surjective. -/
@[to_additive "The natural homomorphism from an `add_monoid` to its quotient by a congruence
relation is surjective."]
lemma mk'_surjective : surjective c.mk' :=
quotient.surjective_quotient_mk'
@[simp, to_additive] lemma coe_mk' : (c.mk' : M → c.quotient) = coe := rfl
/-- The elements related to `x ∈ M`, `M` a monoid, by the kernel of a monoid homomorphism are
those in the preimage of `f(x)` under `f`. -/
@[to_additive "The elements related to `x ∈ M`, `M` an `add_monoid`, by the kernel of
an `add_monoid` homomorphism are those in the preimage of `f(x)` under `f`. "]
lemma ker_apply_eq_preimage {f : M →* P} (x) : (ker f) x = f ⁻¹' {f x} :=
set.ext $ λ x,
⟨λ h, set.mem_preimage.2 $ set.mem_singleton_iff.2 h.symm,
λ h, (set.mem_singleton_iff.1 $ set.mem_preimage.1 h).symm⟩
/-- Given a monoid homomorphism `f : N → M` and a congruence relation `c` on `M`, the congruence
relation induced on `N` by `f` equals the kernel of `c`'s quotient homomorphism composed with
`f`. -/
@[to_additive "Given an `add_monoid` homomorphism `f : N → M` and an additive congruence relation
`c` on `M`, the additive congruence relation induced on `N` by `f` equals the kernel of `c`'s
quotient homomorphism composed with `f`."]
lemma comap_eq {f : N →* M} : comap f f.map_mul c = ker (c.mk'.comp f) :=
ext $ λ x y, show c _ _ ↔ c.mk' _ = c.mk' _, by rw ←c.eq; refl
variables (c) (f : M →* P)
/-- The homomorphism on the quotient of a monoid by a congruence relation `c` induced by a
homomorphism constant on `c`'s equivalence classes. -/
@[to_additive "The homomorphism on the quotient of an `add_monoid` by an additive congruence
relation `c` induced by a homomorphism constant on `c`'s equivalence classes."]
def lift (H : c ≤ ker f) : c.quotient →* P :=
{ to_fun := λ x, con.lift_on x f $ λ _ _ h, H h,
map_one' := by rw ←f.map_one; refl,
map_mul' := λ x y, con.induction_on₂ x y $ λ m n, f.map_mul m n ▸ rfl }
variables {c f}
/-- The diagram describing the universal property for quotients of monoids commutes. -/
@[to_additive "The diagram describing the universal property for quotients of `add_monoid`s
commutes."]
lemma lift_mk' (H : c ≤ ker f) (x) :
c.lift f H (c.mk' x) = f x := rfl
/-- The diagram describing the universal property for quotients of monoids commutes. -/
@[simp, to_additive "The diagram describing the universal property for quotients of `add_monoid`s
commutes."]
lemma lift_coe (H : c ≤ ker f) (x : M) :
c.lift f H x = f x := rfl
/-- The diagram describing the universal property for quotients of monoids commutes. -/
@[simp, to_additive "The diagram describing the universal property for quotients of `add_monoid`s
commutes."]
theorem lift_comp_mk' (H : c ≤ ker f) :
(c.lift f H).comp c.mk' = f := by ext; refl
/-- Given a homomorphism `f` from the quotient of a monoid by a congruence relation, `f` equals the
homomorphism on the quotient induced by `f` composed with the natural map from the monoid to
the quotient. -/
@[simp, to_additive "Given a homomorphism `f` from the quotient of an `add_monoid` by an additive
congruence relation, `f` equals the homomorphism on the quotient induced by `f` composed with the
natural map from the `add_monoid` to the quotient."]
lemma lift_apply_mk' (f : c.quotient →* P) :
c.lift (f.comp c.mk') (λ x y h, show f ↑x = f ↑y, by rw c.eq.2 h) = f :=
by ext; rcases x; refl
/-- Homomorphisms on the quotient of a monoid by a congruence relation are equal if they
are equal on elements that are coercions from the monoid. -/
@[to_additive "Homomorphisms on the quotient of an `add_monoid` by an additive congruence relation
are equal if they are equal on elements that are coercions from the `add_monoid`."]
lemma lift_funext (f g : c.quotient →* P) (h : ∀ a : M, f a = g a) : f = g :=
begin
rw [←lift_apply_mk' f, ←lift_apply_mk' g],
congr' 1,
exact monoid_hom.ext_iff.2 h,
end
/-- The uniqueness part of the universal property for quotients of monoids. -/
@[to_additive "The uniqueness part of the universal property for quotients of `add_monoid`s."]
theorem lift_unique (H : c ≤ ker f) (g : c.quotient →* P)
(Hg : g.comp c.mk' = f) : g = c.lift f H :=
lift_funext g (c.lift f H) $ λ x, by { subst f, refl }
/-- Given a congruence relation `c` on a monoid and a homomorphism `f` constant on `c`'s
equivalence classes, `f` has the same image as the homomorphism that `f` induces on the
quotient. -/
@[to_additive "Given an additive congruence relation `c` on an `add_monoid` and a homomorphism `f`
constant on `c`'s equivalence classes, `f` has the same image as the homomorphism that `f` induces
on the quotient."]
theorem lift_range (H : c ≤ ker f) : (c.lift f H).mrange = f.mrange :=
submonoid.ext $ λ x, ⟨by rintros ⟨⟨y⟩, hy⟩; exact ⟨y, hy⟩, λ ⟨y, hy⟩, ⟨↑y, hy⟩⟩
/-- Surjective monoid homomorphisms constant on a congruence relation `c`'s equivalence classes
induce a surjective homomorphism on `c`'s quotient. -/
@[to_additive "Surjective `add_monoid` homomorphisms constant on an additive congruence
relation `c`'s equivalence classes induce a surjective homomorphism on `c`'s quotient."]
lemma lift_surjective_of_surjective (h : c ≤ ker f) (hf : surjective f) :
surjective (c.lift f h) :=
λ y, exists.elim (hf y) $ λ w hw, ⟨w, (lift_mk' h w).symm ▸ hw⟩
variables (c f)
/-- Given a monoid homomorphism `f` from `M` to `P`, the kernel of `f` is the unique congruence
relation on `M` whose induced map from the quotient of `M` to `P` is injective. -/
@[to_additive "Given an `add_monoid` homomorphism `f` from `M` to `P`, the kernel of `f`
is the unique additive congruence relation on `M` whose induced map from the quotient of `M`
to `P` is injective."]
lemma ker_eq_lift_of_injective (H : c ≤ ker f) (h : injective (c.lift f H)) :
ker f = c :=
to_setoid_inj $ ker_eq_lift_of_injective f H h
variables {c}
/-- The homomorphism induced on the quotient of a monoid by the kernel of a monoid homomorphism. -/
@[to_additive "The homomorphism induced on the quotient of an `add_monoid` by the kernel
of an `add_monoid` homomorphism."]
def ker_lift : (ker f).quotient →* P :=
(ker f).lift f $ λ _ _, id
variables {f}
/-- The diagram described by the universal property for quotients of monoids, when the congruence
relation is the kernel of the homomorphism, commutes. -/
@[simp, to_additive "The diagram described by the universal property for quotients
of `add_monoid`s, when the additive congruence relation is the kernel of the homomorphism,
commutes."]
lemma ker_lift_mk (x : M) : ker_lift f x = f x := rfl
/-- Given a monoid homomorphism `f`, the induced homomorphism on the quotient by `f`'s kernel has
the same image as `f`. -/
@[simp, to_additive "Given an `add_monoid` homomorphism `f`, the induced homomorphism
on the quotient by `f`'s kernel has the same image as `f`."]
lemma ker_lift_range_eq : (ker_lift f).mrange = f.mrange :=
lift_range $ λ _ _, id
/-- A monoid homomorphism `f` induces an injective homomorphism on the quotient by `f`'s kernel. -/
@[to_additive "An `add_monoid` homomorphism `f` induces an injective homomorphism on the quotient
by `f`'s kernel."]
lemma ker_lift_injective (f : M →* P) : injective (ker_lift f) :=
λ x y, quotient.induction_on₂' x y $ λ _ _, (ker f).eq.2
/-- Given congruence relations `c, d` on a monoid such that `d` contains `c`, `d`'s quotient
map induces a homomorphism from the quotient by `c` to the quotient by `d`. -/
@[to_additive "Given additive congruence relations `c, d` on an `add_monoid` such that `d`
contains `c`, `d`'s quotient map induces a homomorphism from the quotient by `c` to the quotient
by `d`."]
def map (c d : con M) (h : c ≤ d) : c.quotient →* d.quotient :=
c.lift d.mk' $ λ x y hc, show (ker d.mk') x y, from
(mk'_ker d).symm ▸ h hc
/-- Given congruence relations `c, d` on a monoid such that `d` contains `c`, the definition of
the homomorphism from the quotient by `c` to the quotient by `d` induced by `d`'s quotient
map. -/
@[to_additive "Given additive congruence relations `c, d` on an `add_monoid` such that `d`
contains `c`, the definition of the homomorphism from the quotient by `c` to the quotient by `d`
induced by `d`'s quotient map."]
lemma map_apply {c d : con M} (h : c ≤ d) (x) :
c.map d h x = c.lift d.mk' (λ x y hc, d.eq.2 $ h hc) x := rfl
variables (c)
/-- The first isomorphism theorem for monoids. -/
@[to_additive "The first isomorphism theorem for `add_monoid`s."]
noncomputable def quotient_ker_equiv_range (f : M →* P) : (ker f).quotient ≃* f.mrange :=
{ map_mul' := monoid_hom.map_mul _,
..equiv.of_bijective
((@mul_equiv.to_monoid_hom (ker_lift f).mrange _ _ _
$ mul_equiv.submonoid_congr ker_lift_range_eq).comp (ker_lift f).mrange_restrict) $
(equiv.bijective _).comp
⟨λ x y h, ker_lift_injective f $ by rcases x; rcases y; injections,
λ ⟨w, z, hz⟩, ⟨z, by rcases hz; rcases _x; refl⟩⟩ }
/-- The first isomorphism theorem for monoids in the case of a homomorphism with right inverse. -/
@[to_additive "The first isomorphism theorem for `add_monoid`s in the case of a homomorphism
with right inverse.", simps]
def quotient_ker_equiv_of_right_inverse (f : M →* P) (g : P → M)
(hf : function.right_inverse g f) :
(ker f).quotient ≃* P :=
{ to_fun := ker_lift f,
inv_fun := coe ∘ g,
left_inv := λ x, ker_lift_injective _ (by rw [function.comp_app, ker_lift_mk, hf]),
right_inv := hf,
.. ker_lift f }
/-- The first isomorphism theorem for monoids in the case of a surjective homomorphism.
For a `computable` version, see `con.quotient_ker_equiv_of_right_inverse`.
-/
@[to_additive "The first isomorphism theorem for `add_monoid`s in the case of a surjective
homomorphism.
For a `computable` version, see `add_con.quotient_ker_equiv_of_right_inverse`.
"]
noncomputable def quotient_ker_equiv_of_surjective (f : M →* P) (hf : surjective f) :
(ker f).quotient ≃* P :=
quotient_ker_equiv_of_right_inverse _ _ hf.has_right_inverse.some_spec
/-- The second isomorphism theorem for monoids. -/
@[to_additive "The second isomorphism theorem for `add_monoid`s."]
noncomputable def comap_quotient_equiv (f : N →* M) :
(comap f f.map_mul c).quotient ≃* (c.mk'.comp f).mrange :=
(con.congr comap_eq).trans $ quotient_ker_equiv_range $ c.mk'.comp f
/-- The third isomorphism theorem for monoids. -/
@[to_additive "The third isomorphism theorem for `add_monoid`s."]
def quotient_quotient_equiv_quotient (c d : con M) (h : c ≤ d) :
(ker (c.map d h)).quotient ≃* d.quotient :=
{ map_mul' := λ x y, con.induction_on₂ x y $ λ w z, con.induction_on₂ w z $ λ a b,
show _ = d.mk' a * d.mk' b, by rw ←d.mk'.map_mul; refl,
..quotient_quotient_equiv_quotient c.to_setoid d.to_setoid h }
end mul_one_class
section monoids
/-- The quotient of a monoid by a congruence relation is a monoid. -/
@[to_additive "The quotient of an `add_monoid` by an additive congruence relation is
an `add_monoid`."]
instance monoid {M : Type*} [monoid M] (c : con M): monoid c.quotient :=
{ one := ((1 : M) : c.quotient),
mul := (*),
mul_assoc := λ x y z, quotient.induction_on₃' x y z
$ λ _ _ _, congr_arg coe $ mul_assoc _ _ _,
.. c.mul_one_class }
/-- The quotient of a `comm_monoid` by a congruence relation is a `comm_monoid`. -/
@[to_additive "The quotient of an `add_comm_monoid` by an additive congruence
relation is an `add_comm_monoid`."]
instance comm_monoid {M : Type*} [comm_monoid M] (c : con M) :
comm_monoid c.quotient :=
{ mul_comm := λ x y, con.induction_on₂ x y $ λ w z, by rw [←coe_mul, ←coe_mul, mul_comm],
..c.monoid}
end monoids
section groups
variables {M} [group M] [group N] [group P] (c : con M)
/-- Multiplicative congruence relations preserve inversion. -/
@[to_additive "Additive congruence relations preserve negation."]
protected lemma inv : ∀ {w x}, c w x → c w⁻¹ x⁻¹ :=
λ x y h, by simpa using c.symm (c.mul (c.mul (c.refl x⁻¹) h) (c.refl y⁻¹))
/-- The inversion induced on the quotient by a congruence relation on a type with a
inversion. -/
@[to_additive "The negation induced on the quotient by an additive congruence relation on a type
with an negation."]
instance has_inv : has_inv c.quotient :=
⟨λ x, quotient.lift_on' x (λ w, ((w⁻¹ : M) : c.quotient))
$ λ x y h, c.eq.2 $ c.inv h⟩
/-- The quotient of a group by a congruence relation is a group. -/
@[to_additive "The quotient of an `add_group` by an additive congruence relation is
an `add_group`."]
instance group : group c.quotient :=
{ inv := λ x, x⁻¹,
mul_left_inv := λ x, show x⁻¹ * x = 1,
from quotient.induction_on' x $ λ _, congr_arg coe $ mul_left_inv _,
.. con.monoid c}
end groups
section units
variables {α : Type*} [monoid M] {c : con M}
/-- In order to define a function `units (con.quotient c) → α` on the units of `con.quotient c`,
where `c : con M` is a multiplicative congruence on a monoid, it suffices to define a function `f`
that takes elements `x y : M` with proofs of `c (x * y) 1` and `c (y * x) 1`, and returns an element
of `α` provided that `f x y _ _ = f x' y' _ _` whenever `c x x'` and `c y y'`. -/
@[to_additive lift_on_add_units] def lift_on_units (u : units c.quotient)
(f : Π (x y : M), c (x * y) 1 → c (y * x) 1 → α)
(Hf : ∀ x y hxy hyx x' y' hxy' hyx', c x x' → c y y' → f x y hxy hyx = f x' y' hxy' hyx') :
α :=
begin
refine @con.hrec_on₂ M M _ _ c c (λ x y, x * y = 1 → y * x = 1 → α)
(u : c.quotient) (↑u⁻¹ : c.quotient)
(λ (x y : M) (hxy : (x * y : c.quotient) = 1) (hyx : (y * x : c.quotient) = 1),
f x y (c.eq.1 hxy) (c.eq.1 hyx)) (λ x y x' y' hx hy, _) u.3 u.4,
ext1, { rw [c.eq.2 hx, c.eq.2 hy] },
rintro Hxy Hxy' -,
ext1, { rw [c.eq.2 hx, c.eq.2 hy] },
rintro Hyx Hyx' -,
exact heq_of_eq (Hf _ _ _ _ _ _ _ _ hx hy)
end
/-- In order to define a function `units (con.quotient c) → α` on the units of `con.quotient c`,
where `c : con M` is a multiplicative congruence on a monoid, it suffices to define a function `f`
that takes elements `x y : M` with proofs of `c (x * y) 1` and `c (y * x) 1`, and returns an element
of `α` provided that `f x y _ _ = f x' y' _ _` whenever `c x x'` and `c y y'`. -/
add_decl_doc add_con.lift_on_add_units
@[simp, to_additive]
lemma lift_on_units_mk (f : Π (x y : M), c (x * y) 1 → c (y * x) 1 → α)
(Hf : ∀ x y hxy hyx x' y' hxy' hyx', c x x' → c y y' → f x y hxy hyx = f x' y' hxy' hyx')
(x y : M) (hxy hyx) :
lift_on_units ⟨(x : c.quotient), y, hxy, hyx⟩ f Hf = f x y (c.eq.1 hxy) (c.eq.1 hyx) :=
rfl
@[elab_as_eliminator, to_additive induction_on_add_units]
lemma induction_on_units {p : units c.quotient → Prop} (u : units c.quotient)
(H : ∀ (x y : M) (hxy : c (x * y) 1) (hyx : c (y * x) 1), p ⟨x, y, c.eq.2 hxy, c.eq.2 hyx⟩) :
p u :=
begin
rcases u with ⟨⟨x⟩, ⟨y⟩, h₁, h₂⟩,
exact H x y (c.eq.1 h₁) (c.eq.1 h₂)
end
end units
end con
|
973fd51ae30ff8408ae540039e3e543ecfcb339b | ae1e94c332e17c7dc7051ce976d5a9eebe7ab8a5 | /tests/compiler/array_test2.lean | 36acaafc896bc68e99f6695b9157a77d1e2e33f3 | [
"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 | 461 | lean | #lang lean4
def check (b : Bool) : IO Unit := do
unless b do IO.println "failed"
def main : IO Unit :=
let a1 := [2, 3, 5].toArray;
let a2 := [4, 7, 9].toArray;
let a3 := [4, 7, 8].toArray;
do
check (Array.isEqv a1 a2 (fun v w => v % 2 == w % 2));
check (!Array.isEqv a1 a3 (fun v w => v % 2 == w % 2));
check (a1 ++ a2 == [2, 3, 5, 4, 7, 9].toArray);
check (a1.any (fun a => a > 4));
check (!a1.any (fun a => a >10));
check (a1.all (fun a => a < 10))
|
f76434eaadbe41d8ea399cf19ca7fb0e6b1c4d34 | cb1829c15cd3d28210f93507f96dfb1f56ec0128 | /theorem_proving/02-dependent_types_exercises.lean | 422001170764d3004e5134c5b5b04cc4c2181714 | [] | no_license | williamdemeo/LEAN_wjd | 69f9f76e35092b89e4479a320be2fa3c18aed6fe | 13826c75c06ef435166a26a72e76fe984c15bad7 | refs/heads/master | 1,609,516,630,137 | 1,518,123,893,000 | 1,518,123,893,000 | 97,740,278 | 2 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,375 | lean | #print "------------------------------------------------"
#print "Section 2.10 Exercises"
/- 1. Define the function =Do_twice=, as described in Section 2.4.-/
def Do_Twice : ((ℕ → ℕ) → (ℕ → ℕ)) → (ℕ → ℕ) → (ℕ → ℕ) :=
λ (f: (ℕ → ℕ) → (ℕ → ℕ)) (g: ℕ → ℕ), f (f g)
/- 2. Define the functions =curry= and =uncurry=, as described in Section 2.4. -/
def curry (α β γ : Type) (f : α × β → γ) : α → β → γ :=
λ (x : α) (y : β), f (x, y)
def uncurry (α β γ : Type) (f : α → β → γ) : α × β → γ :=
λ (p : α × β), f p.1 p.2
/- 3. We used the example =vec α n= for vectors of elements of type =α= of length =n=.
Declare a constant =vec_add= that could represent a function that adds two vectors
of natural numbers of the same length, and a constant =vec_reverse= that can represent
a function that reverses its argument. Use implicit arguments for parameters that can
be inferred. Declare some variables and check some expressions involving the constants
that you have declared. -/
namespace exercise3
universe u
constant vec : Type u → ℕ → Type u
namespace vec
constant empty : Π {α : Type u}, vec α 0
constant cons : Π {α : Type u} {n : ℕ}, α → vec α n → vec α (n + 1)
constant append : Π {α : Type u} {n m : ℕ},
vec α n → vec α m → vec α (n + m)
constant add : Π {α : Type u} {n : ℕ}, vec α n → vec α n → vec α n
constant reverse : Π {α : Type u} {n : ℕ}, vec α n → vec α n
end vec
variable α : Type u
variable v : vec α 10
variable w : vec α 5
variable z : vec α 5
-- (partial application types)
#check @vec.append α 10 5 v -- vec α 5 → vec α (10 + 5)
#check @vec.append α 10 _ v -- vec α ?M_1 → vec α (10 + ?M_1)
#check vec.append v w -- vec α (10 + 5)
#check vec.add w z -- vec α 5
#check vec.add v (vec.append w z) -- vec α 10
end exercise3
/- 4. Similarly, declare a constant =matrix= so that =matrix α m n= could represent the type
of =m= by =n= matrices. Declare some constants to represent functions on this type,
such as matrix addition and multiplication, and (using =vec=) multiplication of a
matrix by a vector. Once again, declare some variables and check some expressions
involving the constants that you have declared. -/
namespace exercise4
universe u
constant matrix : Type u → ℕ → ℕ → Type u
namespace matrix
constant empty : Π {α : Type u}, matrix α 0 0
constant plus : Π {α : Type u} {m n : ℕ}, matrix α m n → matrix α m n → matrix α m n
constant prod : Π {α : Type u} {i k j : ℕ}, matrix α i k → matrix α k j → matrix α i j
end matrix
variables α β : Type u
variables M1 M2 : matrix α 2 3
variable M3 : matrix α 3 4
variable N3 : matrix β 3 4
#check matrix.plus M1 M2 -- matrix α 2 3
#check matrix.prod M2 M3 -- matrix α 2 4
-- #check matrix.plus M1 M3 -- (dimensions not compatible with plus)
-- #check matrix.prod M1 M2 -- (dimensions not compatible with prod)
-- #check matrix.prod M2 N3 -- (expected `matrix α 3 ?` but `N3 : matrix β 3 4`)
end exercise4
|
f7464fe35240631d05774ac00471d2203571a896 | 969dbdfed67fda40a6f5a2b4f8c4a3c7dc01e0fb | /src/ring_theory/witt_vector/witt_polynomial.lean | 976224adf668a97352a251c5e00f3a5c15a57ed9 | [
"Apache-2.0"
] | permissive | SAAluthwela/mathlib | 62044349d72dd63983a8500214736aa7779634d3 | 83a4b8b990907291421de54a78988c024dc8a552 | refs/heads/master | 1,679,433,873,417 | 1,615,998,031,000 | 1,615,998,031,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 10,801 | lean | /-
Copyright (c) 2020 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin, Robert Y. Lewis
-/
import algebra.char_p.invertible
import data.mv_polynomial.variables
import data.mv_polynomial.comm_ring
import data.mv_polynomial.expand
import data.zmod.basic
/-!
# Witt polynomials
To endow `witt_vector p R` with a ring structure,
we need to study the so-called Witt polynomials.
Fix a base value `p : ℕ`.
The `p`-adic Witt polynomials are an infinite family of polynomials
indexed by a natural number `n`, taking values in an arbitrary ring `R`.
The variables of these polynomials are represented by natural numbers.
The variable set of the `n`th Witt polynomial contains at most `n+1` elements `{0, ..., n}`,
with exactly these variables when `R` has characteristic `0`.
These polynomials are used to define the addition and multiplication operators
on the type of Witt vectors. (While this type itself is not complicated,
the ring operations are what make it interesting.)
When the base `p` is invertible in `R`, the `p`-adic Witt polynomials
form a basis for `mv_polynomial ℕ R`, equivalent to the standard basis.
## Main declarations
* `witt_polynomial p R n`: the `n`-th Witt polynomial, viewed as polynomial over the ring `R`
* `X_in_terms_of_W p R n`: if `p` is invertible, the polynomial `X n` is contained in the subalgebra
generated by the Witt polynomials. `X_in_terms_of_W p R n` is the explicit polynomial,
which upon being bound to the Witt polynomials yields `X n`.
* `bind₁_witt_polynomial_X_in_terms_of_W`: the proof of the claim that
`bind₁ (X_in_terms_of_W p R) (W_ R n) = X n`
* `bind₁_X_in_terms_of_W_witt_polynomial`: the converse of the above statement
## Notation
In this file we use the following notation
* `p` is a natural number, typically assumed to be prime.
* `R` and `S` are commutative rings
* `W n` (and `W_ R n` when the ring needs to be explicit) denotes the `n`th Witt polynomial
## References
* [Hazewinkel, *Witt Vectors*][Haze09]
* [Commelin and Lewis, *Formalizing the Ring of Witt Vectors*][CL21]
-/
open mv_polynomial
open finset (hiding map)
open finsupp (single)
open_locale big_operators
local attribute [-simp] coe_eval₂_hom
variables (p : ℕ)
variables (R : Type*) [comm_ring R]
/-- `witt_polynomial p R n` is the `n`-th Witt polynomial
with respect to a prime `p` with coefficients in a commutative ring `R`.
It is defined as:
`∑_{i ≤ n} p^i X_i^{p^{n-i}} ∈ R[X_0, X_1, X_2, …]`. -/
noncomputable def witt_polynomial (n : ℕ) : mv_polynomial ℕ R :=
∑ i in range (n+1), monomial (single i (p ^ (n - i))) (p ^ i)
lemma witt_polynomial_eq_sum_C_mul_X_pow (n : ℕ) :
witt_polynomial p R n = ∑ i in range (n+1), C (p ^ i : R) * X i ^ (p ^ (n - i)) :=
begin
apply sum_congr rfl,
rintro i -,
rw [monomial_eq, finsupp.prod_single_index],
rw pow_zero,
end
/-! We set up notation locally to this file, to keep statements short and comprehensible.
This allows us to simply write `W n` or `W_ ℤ n`. -/
-- Notation with ring of coefficients explicit
localized "notation `W_` := witt_polynomial p" in witt
-- Notation with ring of coefficients implicit
localized "notation `W` := witt_polynomial p _" in witt
open_locale witt
open mv_polynomial
/- The first observation is that the Witt polynomial doesn't really depend on the coefficient ring.
If we map the coefficients through a ring homomorphism, we obtain the corresponding Witt polynomial
over the target ring. -/
section
variables {R} {S : Type*} [comm_ring S]
@[simp] lemma map_witt_polynomial (f : R →+* S) (n : ℕ) :
map f (W n) = W n :=
begin
rw [witt_polynomial, ring_hom.map_sum, witt_polynomial, sum_congr rfl],
intros i hi,
rw [map_monomial, ring_hom.map_pow, ring_hom.map_nat_cast],
end
variables (R)
@[simp] lemma constant_coeff_witt_polynomial [hp : fact p.prime] (n : ℕ) :
constant_coeff (witt_polynomial p R n) = 0 :=
begin
simp only [witt_polynomial, ring_hom.map_sum, constant_coeff_monomial],
rw [sum_eq_zero],
rintro i hi,
rw [if_neg],
rw [finsupp.single_eq_zero],
exact ne_of_gt (pow_pos hp.pos _)
end
@[simp] lemma witt_polynomial_zero : witt_polynomial p R 0 = X 0 :=
by simp only [witt_polynomial, X, sum_singleton, range_one, pow_zero]
@[simp] lemma witt_polynomial_one : witt_polynomial p R 1 = C ↑p * X 1 + (X 0) ^ p :=
by simp only [witt_polynomial_eq_sum_C_mul_X_pow, sum_range_succ, range_one,
sum_singleton, one_mul, pow_one, C_1, pow_zero]
lemma aeval_witt_polynomial {A : Type*} [comm_ring A] [algebra R A] (f : ℕ → A) (n : ℕ) :
aeval f (W_ R n) = ∑ i in range (n+1), p^i * (f i) ^ (p ^ (n-i)) :=
by simp [witt_polynomial, alg_hom.map_sum, aeval_monomial, finsupp.prod_single_index]
/--
Over the ring `zmod (p^(n+1))`, we produce the `n+1`st Witt polynomial
by expanding the `n`th witt polynomial by `p`.
-/
@[simp] lemma witt_polynomial_zmod_self (n : ℕ) :
W_ (zmod (p ^ (n + 1))) (n + 1) = expand p (W_ (zmod (p^(n + 1))) n) :=
begin
simp only [witt_polynomial_eq_sum_C_mul_X_pow],
rw [sum_range_succ, ← nat.cast_pow, char_p.cast_eq_zero (zmod (p^(n+1))) (p^(n+1)), C_0, zero_mul,
zero_add, alg_hom.map_sum, sum_congr rfl],
intros k hk,
rw [alg_hom.map_mul, alg_hom.map_pow, expand_X, alg_hom_C, ← pow_mul, ← pow_succ],
congr,
rw mem_range at hk,
rw [add_comm, nat.add_sub_assoc (nat.lt_succ_iff.mp hk), ← add_comm],
end
section p_prime
-- in fact, `0 < p` would be sufficient
variables [hp : fact p.prime]
include hp
lemma witt_polynomial_vars [char_zero R] (n : ℕ) :
(witt_polynomial p R n).vars = range (n + 1) :=
begin
have : ∀ i, (monomial (finsupp.single i (p ^ (n - i))) (p ^ i : R)).vars = {i},
{ intro i,
rw vars_monomial_single,
{ rw ← pos_iff_ne_zero,
apply pow_pos hp.pos },
{ rw [← nat.cast_pow, nat.cast_ne_zero],
apply ne_of_gt,
apply pow_pos hp.pos i } },
rw [witt_polynomial, vars_sum_of_disjoint],
{ simp only [this, int.nat_cast_eq_coe_nat, bUnion_singleton_eq_self], },
{ simp only [this, int.nat_cast_eq_coe_nat],
intros a b h,
apply singleton_disjoint.mpr,
rwa mem_singleton, },
end
lemma witt_polynomial_vars_subset (n : ℕ) :
(witt_polynomial p R n).vars ⊆ range (n + 1) :=
begin
rw [← map_witt_polynomial p (int.cast_ring_hom R), ← witt_polynomial_vars p ℤ],
apply vars_map,
end
end p_prime
end
/-!
## Witt polynomials as a basis of the polynomial algebra
If `p` is invertible in `R`, then the Witt polynomials form a basis
of the polynomial algebra `mv_polynomial ℕ R`.
The polynomials `X_in_terms_of_W` give the coordinate transformation in the backwards direction.
-/
/-- The `X_in_terms_of_W p R n` is the polynomial on the basis of Witt polynomials
that corresponds to the ordinary `X n`. -/
noncomputable def X_in_terms_of_W [invertible (p : R)] :
ℕ → mv_polynomial ℕ R
| n := (X n - (∑ i : fin n,
have _ := i.2, (C (p^(i : ℕ) : R) * (X_in_terms_of_W i)^(p^(n-i))))) * C (⅟p ^ n : R)
lemma X_in_terms_of_W_eq [invertible (p : R)] {n : ℕ} :
X_in_terms_of_W p R n =
(X n - (∑ i in range n, C (p^i : R) * X_in_terms_of_W p R i ^ p ^ (n - i))) * C (⅟p ^ n : R) :=
by { rw [X_in_terms_of_W, ← fin.sum_univ_eq_sum_range] }
@[simp] lemma constant_coeff_X_in_terms_of_W [hp : fact p.prime] [invertible (p : R)] (n : ℕ) :
constant_coeff (X_in_terms_of_W p R n) = 0 :=
begin
apply nat.strong_induction_on n; clear n,
intros n IH,
rw [X_in_terms_of_W_eq, mul_comm, ring_hom.map_mul, ring_hom.map_sub, ring_hom.map_sum,
constant_coeff_C, sum_eq_zero],
{ simp only [constant_coeff_X, sub_zero, mul_zero] },
{ intros m H,
rw mem_range at H,
simp only [ring_hom.map_mul, ring_hom.map_pow, constant_coeff_C, IH m H],
rw [zero_pow, mul_zero],
apply pow_pos hp.pos, }
end
@[simp] lemma X_in_terms_of_W_zero [invertible (p : R)] :
X_in_terms_of_W p R 0 = X 0 :=
by rw [X_in_terms_of_W_eq, range_zero, sum_empty, pow_zero, C_1, mul_one, sub_zero]
section p_prime
variables [hp : fact p.prime]
include hp
lemma X_in_terms_of_W_vars_aux (n : ℕ) :
n ∈ (X_in_terms_of_W p ℚ n).vars ∧
(X_in_terms_of_W p ℚ n).vars ⊆ range (n + 1) :=
begin
apply nat.strong_induction_on n, clear n,
intros n ih,
rw [X_in_terms_of_W_eq, mul_comm, vars_C_mul, vars_sub_of_disjoint, vars_X, range_succ,
insert_eq],
swap 3, { apply nonzero_of_invertible },
work_on_goal 0 {
simp only [true_and, true_or, eq_self_iff_true,
mem_union, mem_singleton],
intro i,
rw [mem_union, mem_union],
apply or.imp id },
work_on_goal 1 { rw [vars_X, singleton_disjoint] },
all_goals {
intro H,
replace H := vars_sum_subset _ _ H,
rw mem_bUnion at H,
rcases H with ⟨j, hj, H⟩,
rw vars_C_mul at H,
swap,
{ apply pow_ne_zero, exact_mod_cast hp.ne_zero },
rw mem_range at hj,
replace H := (ih j hj).2 (vars_pow _ _ H),
rw mem_range at H },
{ rw mem_range,
exact lt_of_lt_of_le H hj },
{ exact lt_irrefl n (lt_of_lt_of_le H hj) },
end
lemma X_in_terms_of_W_vars_subset (n : ℕ) :
(X_in_terms_of_W p ℚ n).vars ⊆ range (n + 1) :=
(X_in_terms_of_W_vars_aux p n).2
end p_prime
lemma X_in_terms_of_W_aux [invertible (p : R)] (n : ℕ) :
X_in_terms_of_W p R n * C (p^n : R) =
X n - ∑ i in range n, C (p^i : R) * (X_in_terms_of_W p R i)^p^(n-i) :=
by rw [X_in_terms_of_W_eq, mul_assoc, ← C_mul, ← mul_pow, inv_of_mul_self, one_pow, C_1, mul_one]
@[simp] lemma bind₁_X_in_terms_of_W_witt_polynomial [invertible (p : R)] (k : ℕ) :
bind₁ (X_in_terms_of_W p R) (W_ R k) = X k :=
begin
rw [witt_polynomial_eq_sum_C_mul_X_pow, alg_hom.map_sum],
simp only [alg_hom.map_pow, C_pow, alg_hom.map_mul, alg_hom_C],
rw [sum_range_succ, nat.sub_self, pow_zero, pow_one, bind₁_X_right,
mul_comm, ← C_pow, X_in_terms_of_W_aux],
simp only [C_pow, bind₁_X_right, sub_add_cancel],
end
@[simp] lemma bind₁_witt_polynomial_X_in_terms_of_W [invertible (p : R)] (n : ℕ) :
bind₁ (W_ R) (X_in_terms_of_W p R n) = X n :=
begin
apply nat.strong_induction_on n,
clear n, intros n H,
rw [X_in_terms_of_W_eq, alg_hom.map_mul, alg_hom.map_sub, bind₁_X_right, alg_hom_C,
alg_hom.map_sum],
have : W_ R n - ∑ i in range n, C (p ^ i : R) * (X i) ^ p ^ (n - i) = C (p ^ n : R) * X n,
by simp only [witt_polynomial_eq_sum_C_mul_X_pow, nat.sub_self, sum_range_succ,
pow_one, add_sub_cancel, pow_zero],
rw [sum_congr rfl, this],
{ -- this is really slow for some reason
rw [mul_right_comm, ← C_mul, ← mul_pow, mul_inv_of_self, one_pow, C_1, one_mul] },
{ intros i h,
rw mem_range at h,
simp only [alg_hom.map_mul, alg_hom.map_pow, alg_hom_C, H i h] },
end
|
6cd45c101690c089fe54a972ea0ec947f7c80d6a | 5d76f062116fa5bd22eda20d6fd74da58dba65bb | /src/general_lemmas/mv_X_mul.lean | f6cf0eddbf21cfb663b3f828f99070c9533a3505 | [] | no_license | brando90/formal_baby_snark | 59e4732dfb43f97776a3643f2731262f58d2bb81 | 4732da237784bd461ff949729cc011db83917907 | refs/heads/master | 1,682,650,246,414 | 1,621,103,975,000 | 1,621,103,975,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 402 | lean |
import data.mv_polynomial.basic
open mv_polynomial
section
universes u
variables {R : Type u}
variables {σ : Type*}
variables [decidable_eq σ]
variables [comm_semiring R]
lemma coeff_X_mul' (m) (s : σ) (p : mv_polynomial σ R) :
coeff m (X s * p) = if s ∈ m.support then coeff (m - finsupp.single s 1) p else 0 :=
begin
rw mul_comm,
rw mv_polynomial.coeff_mul_X',
finish,
end
end |
0634d5e19f37a3793c94b2698e2b32155fc3dc03 | e00ea76a720126cf9f6d732ad6216b5b824d20a7 | /src/ring_theory/principal_ideal_domain.lean | 3a5bd02c1e95d928531ea93df20213e06bdeba3a | [
"Apache-2.0"
] | permissive | vaibhavkarve/mathlib | a574aaf68c0a431a47fa82ce0637f0f769826bfe | 17f8340912468f49bdc30acdb9a9fa02eeb0473a | refs/heads/master | 1,621,263,802,637 | 1,585,399,588,000 | 1,585,399,588,000 | 250,833,447 | 0 | 0 | Apache-2.0 | 1,585,410,341,000 | 1,585,410,341,000 | null | UTF-8 | Lean | false | false | 6,859 | lean | /-
Copyright (c) 2018 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Chris Hughes, Morenikeji Neri
-/
import algebra.euclidean_domain
import ring_theory.ideals ring_theory.noetherian ring_theory.unique_factorization_domain
variables {α : Type*}
open set function ideal
open_locale classical
class ideal.is_principal [comm_ring α] (S : ideal α) : Prop :=
(principal : ∃ a, S = span {a})
section prio
set_option default_priority 100 -- see Note [default priority]
class principal_ideal_domain (α : Type*) extends integral_domain α :=
(principal : ∀ (S : ideal α), S.is_principal)
end prio
-- see Note [lower instance priority]
attribute [instance, priority 500] principal_ideal_domain.principal
namespace ideal.is_principal
variable [comm_ring α]
noncomputable def generator (S : ideal α) [S.is_principal] : α :=
classical.some (principal S)
lemma span_singleton_generator (S : ideal α) [S.is_principal] : span {generator S} = S :=
eq.symm (classical.some_spec (principal S))
@[simp] lemma generator_mem (S : ideal α) [S.is_principal] : generator S ∈ S :=
by conv {to_rhs, rw ← span_singleton_generator S}; exact subset_span (mem_singleton _)
lemma mem_iff_generator_dvd (S : ideal α) [S.is_principal] {x : α} : x ∈ S ↔ generator S ∣ x :=
by rw [← mem_span_singleton, span_singleton_generator]
lemma eq_bot_iff_generator_eq_zero (S : ideal α) [S.is_principal] :
S = ⊥ ↔ generator S = 0 :=
by rw [← span_singleton_eq_bot, span_singleton_generator]
end ideal.is_principal
namespace is_prime
open ideal.is_principal ideal
lemma to_maximal_ideal [principal_ideal_domain α] {S : ideal α}
[hpi : is_prime S] (hS : S ≠ ⊥) : is_maximal S :=
is_maximal_iff.2 ⟨(ne_top_iff_one S).1 hpi.1, begin
assume T x hST hxS hxT,
haveI := principal_ideal_domain.principal S,
haveI := principal_ideal_domain.principal T,
cases (mem_iff_generator_dvd _).1 (hST $ generator_mem S) with z hz,
cases hpi.2 (show generator T * z ∈ S, from hz ▸ generator_mem S),
{ have hTS : T ≤ S, rwa [← span_singleton_generator T, span_le, singleton_subset_iff],
exact (hxS $ hTS hxT).elim },
cases (mem_iff_generator_dvd _).1 h with y hy,
have : generator S ≠ 0 := mt (eq_bot_iff_generator_eq_zero _).2 hS,
rw [← mul_one (generator S), hy, mul_left_comm, domain.mul_left_inj this] at hz,
exact hz.symm ▸ ideal.mul_mem_right _ (generator_mem T)
end⟩
end is_prime
section
open euclidean_domain
variable [euclidean_domain α]
lemma mod_mem_iff {S : ideal α} {x y : α} (hy : y ∈ S) : x % y ∈ S ↔ x ∈ S :=
⟨λ hxy, div_add_mod x y ▸ ideal.add_mem S (mul_mem_right S hy) hxy,
λ hx, (mod_eq_sub_mul_div x y).symm ▸ ideal.sub_mem S hx (ideal.mul_mem_right S hy)⟩
@[priority 100] -- see Note [lower instance priority]
instance euclidean_domain.to_principal_ideal_domain : principal_ideal_domain α :=
{ principal := λ S, by exactI
⟨if h : {x : α | x ∈ S ∧ x ≠ 0}.nonempty
then
have wf : well_founded euclidean_domain.r := euclidean_domain.r_well_founded α,
have hmin : well_founded.min wf {x : α | x ∈ S ∧ x ≠ 0} h ∈ S ∧
well_founded.min wf {x : α | x ∈ S ∧ x ≠ 0} h ≠ 0,
from well_founded.min_mem wf {x : α | x ∈ S ∧ x ≠ 0} h,
⟨well_founded.min wf {x : α | x ∈ S ∧ x ≠ 0} h,
submodule.ext $ λ x,
⟨λ hx, div_add_mod x (well_founded.min wf {x : α | x ∈ S ∧ x ≠ 0} h) ▸
(mem_span_singleton.2 $ dvd_add (dvd_mul_right _ _) $
have (x % (well_founded.min wf {x : α | x ∈ S ∧ x ≠ 0} h) ∉ {x : α | x ∈ S ∧ x ≠ 0}),
from λ h₁, well_founded.not_lt_min wf _ h h₁ (mod_lt x hmin.2),
have x % well_founded.min wf {x : α | x ∈ S ∧ x ≠ 0} h = 0, by finish [(mod_mem_iff hmin.1).2 hx],
by simp *),
λ hx, let ⟨y, hy⟩ := mem_span_singleton.1 hx in hy.symm ▸ ideal.mul_mem_right _ hmin.1⟩⟩
else ⟨0, submodule.ext $ λ a, by rw [← @submodule.bot_coe α α _ _ ring.to_module, span_eq, submodule.mem_bot]; exact
⟨λ haS, by_contradiction $ λ ha0, h ⟨a, ⟨haS, ha0⟩⟩,
λ h₁, h₁.symm ▸ S.zero_mem⟩⟩⟩ }
end
namespace principal_ideal_domain
variables [principal_ideal_domain α]
@[priority 100] -- see Note [lower instance priority]
instance is_noetherian_ring : is_noetherian_ring α :=
⟨assume s : ideal α,
begin
cases (principal s).principal with a hs,
refine ⟨finset.singleton a, submodule.ext' _⟩, rw hs, refl
end⟩
section
open_locale classical
open submodule
lemma factors_decreasing (b₁ b₂ : α) (h₁ : b₁ ≠ 0) (h₂ : ¬ is_unit b₂) :
submodule.span α ({b₁ * b₂} : set α) < submodule.span α {b₁} :=
lt_of_le_not_le (ideal.span_le.2 $ singleton_subset_iff.2 $
ideal.mem_span_singleton.2 ⟨b₂, rfl⟩) $ λ h,
h₂ $ is_unit_of_dvd_one _ $ (mul_dvd_mul_iff_left h₁).1 $
by rwa [mul_one, ← ideal.span_singleton_le_span_singleton]
end
lemma is_maximal_of_irreducible {p : α} (hp : irreducible p) :
is_maximal (span ({p} : set α)) :=
⟨mt span_singleton_eq_top.1 hp.1, λ I hI, begin
rcases principal I with ⟨a, rfl⟩,
rw span_singleton_eq_top,
unfreezeI,
rcases span_singleton_le_span_singleton.1 (le_of_lt hI) with ⟨b, rfl⟩,
refine (of_irreducible_mul hp).resolve_right (mt (λ hb, _) (not_le_of_lt hI)),
rw [span_singleton_le_span_singleton, mul_dvd_of_is_unit_right hb]
end⟩
lemma irreducible_iff_prime {p : α} : irreducible p ↔ prime p :=
⟨λ hp, (span_singleton_prime hp.ne_zero).1 $
(is_maximal_of_irreducible hp).is_prime,
irreducible_of_prime⟩
lemma associates_irreducible_iff_prime : ∀{p : associates α}, irreducible p ↔ p.prime :=
associates.forall_associated.2 $ assume a,
by rw [associates.irreducible_mk_iff, associates.prime_mk, irreducible_iff_prime]
section
open_locale classical
noncomputable def factors (a : α) : multiset α :=
if h : a = 0 then ∅ else classical.some
(is_noetherian_ring.exists_factors a h)
lemma factors_spec (a : α) (h : a ≠ 0) :
(∀b∈factors a, irreducible b) ∧ associated a (factors a).prod :=
begin
unfold factors, rw [dif_neg h],
exact classical.some_spec
(is_noetherian_ring.exists_factors a h)
end
/-- The unique factorization domain structure given by the principal ideal domain.
This is not added as type class instance, since the `factors` might be computed in a different way.
E.g. factors could return normalized values.
-/
noncomputable def to_unique_factorization_domain : unique_factorization_domain α :=
{ factors := factors,
factors_prod := assume a ha, associated.symm (factors_spec a ha).2,
prime_factors := assume a ha, by simpa [irreducible_iff_prime] using (factors_spec a ha).1 }
end
end principal_ideal_domain
|
6f297e4a3fa98266db686456e4e47b0092693af4 | 302b541ac2e998a523ae04da7673fd0932ded126 | /tests/playground/list_lambda.lean | f3ff88bb59c4e7fd41125de637d4c5ebd1eb0321 | [] | no_license | mattweingarten/lambdapure | 4aeff69e8e3b8e78ea3c0a2b9b61770ef5a689b1 | f920a4ad78e6b1e3651f30bf8445c9105dfa03a8 | refs/heads/master | 1,680,665,168,790 | 1,618,420,180,000 | 1,618,420,180,000 | 310,816,264 | 2 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 405 | lean | set_option trace.compiler.ir.init true
inductive L
| Nil
| Cons : Nat -> L -> L
open L
def map : (Nat -> Nat) -> L -> L
| f, Nil => Nil
| f, Cons n l => Cons (f n) (map f l)
inductive Tree
| Nil
| Node_2 (l r : Tree) : Tree
| Node_3 (l m r : Tree) : Tree
open Tree
def t : Tree -> Tree
| Nil => Nil
| Node_3 l m r => Node_2 (t l) (t m)
| Node_2 l r => Node_2 (t l) (t r)
|
c384d813100c3a5fefe72b13e6386e14f50827cb | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/ring_theory/integral_domain.lean | c9e84fe09ac29c59ea81c3f804834ba0be81212f | [
"Apache-2.0"
] | permissive | alreadydone/mathlib | dc0be621c6c8208c581f5170a8216c5ba6721927 | c982179ec21091d3e102d8a5d9f5fe06c8fafb73 | refs/heads/master | 1,685,523,275,196 | 1,670,184,141,000 | 1,670,184,141,000 | 287,574,545 | 0 | 0 | Apache-2.0 | 1,670,290,714,000 | 1,597,421,623,000 | Lean | UTF-8 | Lean | false | false | 8,315 | lean | /-
Copyright (c) 2020 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin, Chris Hughes
-/
import data.polynomial.ring_division
import group_theory.specific_groups.cyclic
import algebra.geom_sum
/-!
# Integral domains
Assorted theorems about integral domains.
## Main theorems
* `is_cyclic_of_subgroup_is_domain`: A finite subgroup of the units of an integral domain is cyclic.
* `fintype.field_of_domain`: A finite integral domain is a field.
## TODO
Prove Wedderburn's little theorem, which shows that all finite division rings are actually fields.
## Tags
integral domain, finite integral domain, finite field
-/
section
open finset polynomial function
open_locale big_operators nat
section cancel_monoid_with_zero
-- There doesn't seem to be a better home for these right now
variables {M : Type*} [cancel_monoid_with_zero M] [finite M]
lemma mul_right_bijective_of_finite₀ {a : M} (ha : a ≠ 0) : bijective (λ b, a * b) :=
finite.injective_iff_bijective.1 $ mul_right_injective₀ ha
lemma mul_left_bijective_of_finite₀ {a : M} (ha : a ≠ 0) : bijective (λ b, b * a) :=
finite.injective_iff_bijective.1 $ mul_left_injective₀ ha
/-- Every finite nontrivial cancel_monoid_with_zero is a group_with_zero. -/
def fintype.group_with_zero_of_cancel (M : Type*) [cancel_monoid_with_zero M] [decidable_eq M]
[fintype M] [nontrivial M] : group_with_zero M :=
{ inv := λ a, if h : a = 0 then 0 else fintype.bij_inv (mul_right_bijective_of_finite₀ h) 1,
mul_inv_cancel := λ a ha,
by { simp [has_inv.inv, dif_neg ha], exact fintype.right_inverse_bij_inv _ _ },
inv_zero := by { simp [has_inv.inv, dif_pos rfl] },
..‹nontrivial M›,
..‹cancel_monoid_with_zero M› }
end cancel_monoid_with_zero
variables {R : Type*} {G : Type*}
section ring
variables [ring R] [is_domain R] [fintype R]
/-- Every finite domain is a division ring.
TODO: Prove Wedderburn's little theorem,
which shows a finite domain is in fact commutative, hence a field. -/
def fintype.division_ring_of_is_domain (R : Type*) [ring R] [is_domain R] [decidable_eq R]
[fintype R] : division_ring R :=
{ ..show group_with_zero R, from fintype.group_with_zero_of_cancel R,
..‹ring R› }
/-- Every finite commutative domain is a field.
TODO: Prove Wedderburn's little theorem, which shows a finite domain is automatically commutative,
dropping one assumption from this theorem. -/
def fintype.field_of_domain (R) [comm_ring R] [is_domain R] [decidable_eq R] [fintype R] :
field R :=
{ .. fintype.group_with_zero_of_cancel R,
.. ‹comm_ring R› }
lemma finite.is_field_of_domain (R) [comm_ring R] [is_domain R] [finite R] : is_field R :=
by { casesI nonempty_fintype R,
exact @field.to_is_field R (@@fintype.field_of_domain R _ _ (classical.dec_eq R) _) }
end ring
variables [comm_ring R] [is_domain R] [group G]
lemma card_nth_roots_subgroup_units [fintype G] (f : G →* R) (hf : injective f) {n : ℕ} (hn : 0 < n)
(g₀ : G) :
({g ∈ univ | g ^ n = g₀} : finset G).card ≤ (nth_roots n (f g₀)).card :=
begin
haveI : decidable_eq R := classical.dec_eq _,
refine le_trans _ (nth_roots n (f g₀)).to_finset_card_le,
apply card_le_card_of_inj_on f,
{ intros g hg,
rw [sep_def, mem_filter] at hg,
rw [multiset.mem_to_finset, mem_nth_roots hn, ← f.map_pow, hg.2] },
{ intros, apply hf, assumption }
end
/-- A finite subgroup of the unit group of an integral domain is cyclic. -/
lemma is_cyclic_of_subgroup_is_domain [finite G] (f : G →* R) (hf : injective f) : is_cyclic G :=
begin
classical,
casesI nonempty_fintype G,
apply is_cyclic_of_card_pow_eq_one_le,
intros n hn,
convert (le_trans (card_nth_roots_subgroup_units f hf hn 1) (card_nth_roots n (f 1)))
end
/-- The unit group of a finite integral domain is cyclic.
To support `ℤˣ` and other infinite monoids with finite groups of units, this requires only
`finite Rˣ` rather than deducing it from `finite R`. -/
instance [finite Rˣ] : is_cyclic Rˣ :=
is_cyclic_of_subgroup_is_domain (units.coe_hom R) $ units.ext
section
variables (S : subgroup Rˣ) [finite S]
/-- A finite subgroup of the units of an integral domain is cyclic. -/
instance subgroup_units_cyclic : is_cyclic S :=
begin
refine is_cyclic_of_subgroup_is_domain ⟨(coe : S → R), _, _⟩
(units.ext.comp subtype.val_injective),
{ simp },
{ intros, simp },
end
end
variables [fintype G]
lemma card_fiber_eq_of_mem_range {H : Type*} [group H] [decidable_eq H]
(f : G →* H) {x y : H} (hx : x ∈ set.range f) (hy : y ∈ set.range f) :
(univ.filter $ λ g, f g = x).card = (univ.filter $ λ g, f g = y).card :=
begin
rcases hx with ⟨x, rfl⟩,
rcases hy with ⟨y, rfl⟩,
refine card_congr (λ g _, g * x⁻¹ * y) _ _ (λ g hg, ⟨g * y⁻¹ * x, _⟩),
{ simp only [mem_filter, one_mul, monoid_hom.map_mul, mem_univ, mul_right_inv,
eq_self_iff_true, monoid_hom.map_mul_inv, and_self, forall_true_iff] {contextual := tt} },
{ simp only [mul_left_inj, imp_self, forall_2_true_iff], },
{ simp only [true_and, mem_filter, mem_univ] at hg,
simp only [hg, mem_filter, one_mul, monoid_hom.map_mul, mem_univ, mul_right_inv,
eq_self_iff_true, exists_prop_of_true, monoid_hom.map_mul_inv, and_self,
mul_inv_cancel_right, inv_mul_cancel_right], }
end
/-- In an integral domain, a sum indexed by a nontrivial homomorphism from a finite group is zero.
-/
lemma sum_hom_units_eq_zero (f : G →* R) (hf : f ≠ 1) : ∑ g : G, f g = 0 :=
begin
classical,
obtain ⟨x, hx⟩ : ∃ x : monoid_hom.range f.to_hom_units,
∀ y : monoid_hom.range f.to_hom_units, y ∈ submonoid.powers x,
from is_cyclic.exists_monoid_generator,
have hx1 : x ≠ 1,
{ rintro rfl,
apply hf,
ext g,
rw [monoid_hom.one_apply],
cases hx ⟨f.to_hom_units g, g, rfl⟩ with n hn,
rwa [subtype.ext_iff, units.ext_iff, subtype.coe_mk, monoid_hom.coe_to_hom_units,
one_pow, eq_comm] at hn, },
replace hx1 : (x : R) - 1 ≠ 0,
from λ h, hx1 (subtype.eq (units.ext (sub_eq_zero.1 h))),
let c := (univ.filter (λ g, f.to_hom_units g = 1)).card,
calc ∑ g : G, f g
= ∑ g : G, f.to_hom_units g : rfl
... = ∑ u : Rˣ in univ.image f.to_hom_units,
(univ.filter (λ g, f.to_hom_units g = u)).card • u : sum_comp (coe : Rˣ → R) f.to_hom_units
... = ∑ u : Rˣ in univ.image f.to_hom_units, c • u :
sum_congr rfl (λ u hu, congr_arg2 _ _ rfl) -- remaining goal 1, proven below
... = ∑ b : monoid_hom.range f.to_hom_units, c • ↑b : finset.sum_subtype _
(by simp ) _
... = c • ∑ b : monoid_hom.range f.to_hom_units, (b : R) : smul_sum.symm
... = c • 0 : congr_arg2 _ rfl _ -- remaining goal 2, proven below
... = 0 : smul_zero _,
{ -- remaining goal 1
show (univ.filter (λ (g : G), f.to_hom_units g = u)).card = c,
apply card_fiber_eq_of_mem_range f.to_hom_units,
{ simpa only [mem_image, mem_univ, exists_prop_of_true, set.mem_range] using hu, },
{ exact ⟨1, f.to_hom_units.map_one⟩ } },
-- remaining goal 2
show ∑ b : monoid_hom.range f.to_hom_units, (b : R) = 0,
calc ∑ b : monoid_hom.range f.to_hom_units, (b : R)
= ∑ n in range (order_of x), x ^ n :
eq.symm $ sum_bij (λ n _, x ^ n)
(by simp only [mem_univ, forall_true_iff])
(by simp only [implies_true_iff, eq_self_iff_true, subgroup.coe_pow, units.coe_pow, coe_coe])
(λ m n hm hn, pow_injective_of_lt_order_of _
(by simpa only [mem_range] using hm)
(by simpa only [mem_range] using hn))
(λ b hb, let ⟨n, hn⟩ := hx b in ⟨n % order_of x, mem_range.2 (nat.mod_lt _ (order_of_pos _)),
by rw [← pow_eq_mod_order_of, hn]⟩)
... = 0 : _,
rw [← mul_left_inj' hx1, zero_mul, geom_sum_mul, coe_coe],
norm_cast,
simp [pow_order_of_eq_one],
end
/-- In an integral domain, a sum indexed by a homomorphism from a finite group is zero,
unless the homomorphism is trivial, in which case the sum is equal to the cardinality of the group.
-/
lemma sum_hom_units (f : G →* R) [decidable (f = 1)] :
∑ g : G, f g = if f = 1 then fintype.card G else 0 :=
begin
split_ifs with h h,
{ simp [h, card_univ] },
{ exact sum_hom_units_eq_zero f h }
end
end
|
e4844ab22890f1d739f343a202eda23b9fb3451b | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/topology/category/Profinite/default.lean | 33669f7141d695bb3b59402049006163d657f4ad | [
"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 | 11,233 | lean | /-
Copyright (c) 2020 Kevin Buzzard. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kevin Buzzard, Calle Sönne
-/
import topology.category.CompHaus
import topology.connected
import topology.subset_properties
import topology.locally_constant.basic
import category_theory.adjunction.reflective
import category_theory.monad.limits
import category_theory.limits.constructions.epi_mono
import category_theory.Fintype
/-!
# The category of Profinite Types
We construct the category of profinite topological spaces,
often called profinite sets -- perhaps they could be called
profinite types in Lean.
The type of profinite topological spaces is called `Profinite`. It has a category
instance and is a fully faithful subcategory of `Top`. The fully faithful functor
is called `Profinite_to_Top`.
## Implementation notes
A profinite type is defined to be a topological space which is
compact, Hausdorff and totally disconnected.
## TODO
0. Link to category of projective limits of finite discrete sets.
1. finite coproducts
2. Clausen/Scholze topology on the category `Profinite`.
## Tags
profinite
-/
universe u
open category_theory
/-- The type of profinite topological spaces. -/
structure Profinite :=
(to_CompHaus : CompHaus)
[is_totally_disconnected : totally_disconnected_space to_CompHaus]
namespace Profinite
/--
Construct a term of `Profinite` from a type endowed with the structure of a
compact, Hausdorff and totally disconnected topological space.
-/
def of (X : Type*) [topological_space X] [compact_space X] [t2_space X]
[totally_disconnected_space X] : Profinite := ⟨⟨⟨X⟩⟩⟩
instance : inhabited Profinite := ⟨Profinite.of pempty⟩
instance category : category Profinite := induced_category.category to_CompHaus
instance concrete_category : concrete_category Profinite := induced_category.concrete_category _
instance has_forget₂ : has_forget₂ Profinite Top := induced_category.has_forget₂ _
instance : has_coe_to_sort Profinite Type* := ⟨λ X, X.to_CompHaus⟩
instance {X : Profinite} : totally_disconnected_space X := X.is_totally_disconnected
-- We check that we automatically infer that Profinite sets are compact and Hausdorff.
example {X : Profinite} : compact_space X := infer_instance
example {X : Profinite} : t2_space X := infer_instance
@[simp]
lemma coe_to_CompHaus {X : Profinite} : (X.to_CompHaus : Type*) = X :=
rfl
@[simp] lemma coe_id (X : Profinite) : (𝟙 X : X → X) = id := rfl
@[simp] lemma coe_comp {X Y Z : Profinite} (f : X ⟶ Y) (g : Y ⟶ Z) : (f ≫ g : X → Z) = g ∘ f := rfl
end Profinite
/-- The fully faithful embedding of `Profinite` in `CompHaus`. -/
@[simps, derive [full, faithful]]
def Profinite_to_CompHaus : Profinite ⥤ CompHaus := induced_functor _
/-- The fully faithful embedding of `Profinite` in `Top`. This is definitionally the same as the
obvious composite. -/
@[simps, derive [full, faithful]]
def Profinite.to_Top : Profinite ⥤ Top := forget₂ _ _
@[simp] lemma Profinite.to_CompHaus_to_Top :
Profinite_to_CompHaus ⋙ CompHaus_to_Top = Profinite.to_Top :=
rfl
section Profinite
/--
(Implementation) The object part of the connected_components functor from compact Hausdorff spaces
to Profinite spaces, given by quotienting a space by its connected components.
See: https://stacks.math.columbia.edu/tag/0900
-/
-- Without explicit universe annotations here, Lean introduces two universe variables and
-- unhelpfully defines a function `CompHaus.{max u₁ u₂} → Profinite.{max u₁ u₂}`.
def CompHaus.to_Profinite_obj (X : CompHaus.{u}) : Profinite.{u} :=
{ to_CompHaus :=
{ to_Top := Top.of (connected_components X),
is_compact := quotient.compact_space,
is_hausdorff := connected_components.t2 },
is_totally_disconnected := connected_components.totally_disconnected_space }
/--
(Implementation) The bijection of homsets to establish the reflective adjunction of Profinite
spaces in compact Hausdorff spaces.
-/
def Profinite.to_CompHaus_equivalence (X : CompHaus.{u}) (Y : Profinite.{u}) :
(CompHaus.to_Profinite_obj X ⟶ Y) ≃ (X ⟶ Profinite_to_CompHaus.obj Y) :=
{ to_fun := λ f, f.comp ⟨quotient.mk', continuous_quotient_mk⟩,
inv_fun := λ g,
{ to_fun := continuous.connected_components_lift g.2,
continuous_to_fun := continuous.connected_components_lift_continuous g.2},
left_inv := λ f, continuous_map.ext $ connected_components.surjective_coe.forall.2 $ λ a, rfl,
right_inv := λ f, continuous_map.ext $ λ x, rfl }
/--
The connected_components functor from compact Hausdorff spaces to profinite spaces,
left adjoint to the inclusion functor.
-/
def CompHaus.to_Profinite : CompHaus ⥤ Profinite :=
adjunction.left_adjoint_of_equiv Profinite.to_CompHaus_equivalence (λ _ _ _ _ _, rfl)
lemma CompHaus.to_Profinite_obj' (X : CompHaus) :
↥(CompHaus.to_Profinite.obj X) = connected_components X := rfl
/-- Finite types are given the discrete topology. -/
def Fintype.discrete_topology (A : Fintype) : topological_space A := ⊥
section discrete_topology
local attribute [instance] Fintype.discrete_topology
/-- The natural functor from `Fintype` to `Profinite`, endowing a finite type with the
discrete topology. -/
@[simps] def Fintype.to_Profinite : Fintype ⥤ Profinite :=
{ obj := λ A, Profinite.of A,
map := λ _ _ f, ⟨f⟩ }
end discrete_topology
end Profinite
namespace Profinite
/-- An explicit limit cone for a functor `F : J ⥤ Profinite`, defined in terms of
`Top.limit_cone`. -/
def limit_cone {J : Type u} [small_category J] (F : J ⥤ Profinite.{u}) :
limits.cone F :=
{ X :=
{ to_CompHaus := (CompHaus.limit_cone (F ⋙ Profinite_to_CompHaus)).X,
is_totally_disconnected :=
begin
change totally_disconnected_space ↥{u : Π (j : J), (F.obj j) | _},
exact subtype.totally_disconnected_space,
end },
π := { app := (CompHaus.limit_cone (F ⋙ Profinite_to_CompHaus)).π.app } }
/-- The limit cone `Profinite.limit_cone F` is indeed a limit cone. -/
def limit_cone_is_limit {J : Type u} [small_category J] (F : J ⥤ Profinite.{u}) :
limits.is_limit (limit_cone F) :=
{ lift := λ S, (CompHaus.limit_cone_is_limit (F ⋙ Profinite_to_CompHaus)).lift
(Profinite_to_CompHaus.map_cone S),
uniq' := λ S m h,
(CompHaus.limit_cone_is_limit _).uniq (Profinite_to_CompHaus.map_cone S) _ h }
/-- The adjunction between CompHaus.to_Profinite and Profinite.to_CompHaus -/
def to_Profinite_adj_to_CompHaus : CompHaus.to_Profinite ⊣ Profinite_to_CompHaus :=
adjunction.adjunction_of_equiv_left _ _
/-- The category of profinite sets is reflective in the category of compact hausdroff spaces -/
instance to_CompHaus.reflective : reflective Profinite_to_CompHaus :=
{ to_is_right_adjoint := ⟨CompHaus.to_Profinite, Profinite.to_Profinite_adj_to_CompHaus⟩ }
noncomputable
instance to_CompHaus.creates_limits : creates_limits Profinite_to_CompHaus :=
monadic_creates_limits _
noncomputable
instance to_Top.reflective : reflective Profinite.to_Top :=
reflective.comp Profinite_to_CompHaus CompHaus_to_Top
noncomputable
instance to_Top.creates_limits : creates_limits Profinite.to_Top :=
monadic_creates_limits _
instance has_limits : limits.has_limits Profinite :=
has_limits_of_has_limits_creates_limits Profinite.to_Top
instance has_colimits : limits.has_colimits Profinite :=
has_colimits_of_reflective Profinite_to_CompHaus
noncomputable
instance forget_preserves_limits : limits.preserves_limits (forget Profinite) :=
by apply limits.comp_preserves_limits Profinite.to_Top (forget Top)
variables {X Y : Profinite.{u}} (f : X ⟶ Y)
/-- Any morphism of profinite spaces is a closed map. -/
lemma is_closed_map : is_closed_map f :=
CompHaus.is_closed_map _
/-- Any continuous bijection of profinite spaces induces an isomorphism. -/
lemma is_iso_of_bijective (bij : function.bijective f) : is_iso f :=
begin
haveI := CompHaus.is_iso_of_bijective (Profinite_to_CompHaus.map f) bij,
exact is_iso_of_fully_faithful Profinite_to_CompHaus _
end
/-- Any continuous bijection of profinite spaces induces an isomorphism. -/
noncomputable def iso_of_bijective (bij : function.bijective f) : X ≅ Y :=
by letI := Profinite.is_iso_of_bijective f bij; exact as_iso f
instance forget_reflects_isomorphisms : reflects_isomorphisms (forget Profinite) :=
⟨by introsI A B f hf; exact Profinite.is_iso_of_bijective _ ((is_iso_iff_bijective f).mp hf)⟩
/-- Construct an isomorphism from a homeomorphism. -/
@[simps hom inv] def iso_of_homeo (f : X ≃ₜ Y) : X ≅ Y :=
{ hom := ⟨f, f.continuous⟩,
inv := ⟨f.symm, f.symm.continuous⟩,
hom_inv_id' := by { ext x, exact f.symm_apply_apply x },
inv_hom_id' := by { ext x, exact f.apply_symm_apply x } }
/-- Construct a homeomorphism from an isomorphism. -/
@[simps] def homeo_of_iso (f : X ≅ Y) : X ≃ₜ Y :=
{ to_fun := f.hom,
inv_fun := f.inv,
left_inv := λ x, by { change (f.hom ≫ f.inv) x = x, rw [iso.hom_inv_id, coe_id, id.def] },
right_inv := λ x, by { change (f.inv ≫ f.hom) x = x, rw [iso.inv_hom_id, coe_id, id.def] },
continuous_to_fun := f.hom.continuous,
continuous_inv_fun := f.inv.continuous }
/-- The equivalence between isomorphisms in `Profinite` and homeomorphisms
of topological spaces. -/
@[simps] def iso_equiv_homeo : (X ≅ Y) ≃ (X ≃ₜ Y) :=
{ to_fun := homeo_of_iso,
inv_fun := iso_of_homeo,
left_inv := λ f, by { ext, refl },
right_inv := λ f, by { ext, refl } }
lemma epi_iff_surjective {X Y : Profinite.{u}} (f : X ⟶ Y) : epi f ↔ function.surjective f :=
begin
split,
{ contrapose!,
rintros ⟨y, hy⟩ hf,
let C := set.range f,
have hC : is_closed C := (is_compact_range f.continuous).is_closed,
let U := Cᶜ,
have hU : is_open U := is_open_compl_iff.mpr hC,
have hyU : y ∈ U,
{ refine set.mem_compl _, rintro ⟨y', hy'⟩, exact hy y' hy' },
have hUy : U ∈ nhds y := hU.mem_nhds hyU,
obtain ⟨V, hV, hyV, hVU⟩ := is_topological_basis_clopen.mem_nhds_iff.mp hUy,
classical,
letI : topological_space (ulift.{u} $ fin 2) := ⊥,
let Z := of (ulift.{u} $ fin 2),
let g : Y ⟶ Z := ⟨(locally_constant.of_clopen hV).map ulift.up, locally_constant.continuous _⟩,
let h : Y ⟶ Z := ⟨λ _, ⟨1⟩, continuous_const⟩,
have H : h = g,
{ rw ← cancel_epi f,
ext x, dsimp [locally_constant.of_clopen],
rw if_neg, { refl },
refine mt (λ α, hVU α) _,
simp only [set.mem_range_self, not_true, not_false_iff, set.mem_compl_eq], },
apply_fun (λ e, (e y).down) at H,
dsimp [locally_constant.of_clopen] at H,
rw if_pos hyV at H,
exact top_ne_bot H },
{ rw ← category_theory.epi_iff_surjective,
apply faithful_reflects_epi (forget Profinite) },
end
lemma mono_iff_injective {X Y : Profinite.{u}} (f : X ⟶ Y) : mono f ↔ function.injective f :=
begin
split,
{ intro h,
haveI : limits.preserves_limits Profinite_to_CompHaus := infer_instance,
haveI : mono (Profinite_to_CompHaus.map f) := infer_instance,
rwa ← CompHaus.mono_iff_injective },
{ rw ← category_theory.mono_iff_injective,
apply faithful_reflects_mono (forget Profinite) }
end
end Profinite
|
ddb7893ec46986a2dd46d5382891e87a02f0ca1e | 82e44445c70db0f03e30d7be725775f122d72f3e | /src/algebra/archimedean.lean | 2af368ab980605fc9afe78e7fae9c2236489acd9 | [
"Apache-2.0"
] | permissive | stjordanis/mathlib | 51e286d19140e3788ef2c470bc7b953e4991f0c9 | 2568d41bca08f5d6bf39d915434c8447e21f42ee | refs/heads/master | 1,631,748,053,501 | 1,627,938,886,000 | 1,627,938,886,000 | 228,728,358 | 0 | 0 | Apache-2.0 | 1,576,630,588,000 | 1,576,630,587,000 | null | UTF-8 | Lean | false | false | 13,958 | 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.field_power
import data.rat
import data.int.least_greatest
/-!
# Archimedean groups and fields.
This file defines the archimedean property for ordered groups and proves several results connected
to this notion. Being archimedean means that for all elements `x` and `y>0` there exists a natural
number `n` such that `x ≤ n • y`.
## Main definitions
* `archimedean` is a typeclass for an ordered additive commutative monoid to have the archimedean
property.
* `archimedean.floor_ring` defines a floor function on an archimedean linearly ordered ring making
it into a `floor_ring`.
* `round` defines a function rounding to the nearest integer for a linearly ordered field which is
also a floor ring.
## Main statements
* `ℕ`, `ℤ`, and `ℚ` are archimedean.
-/
variables {α : Type*}
/-- An ordered additive commutative monoid is called `archimedean` if for any two elements `x`, `y`
such that `0 < y` there exists a natural number `n` such that `x ≤ n • y`. -/
class archimedean (α) [ordered_add_comm_monoid α] : Prop :=
(arch : ∀ (x : α) {y}, 0 < y → ∃ n : ℕ, x ≤ n • y)
instance order_dual.archimedean [ordered_add_comm_group α] [archimedean α] :
archimedean (order_dual α) :=
⟨λ x y hy, let ⟨n, hn⟩ := archimedean.arch (-x : α) (neg_pos.2 hy) in
⟨n, by rwa [neg_nsmul, neg_le_neg_iff] at hn⟩⟩
namespace linear_ordered_add_comm_group
variables [linear_ordered_add_comm_group α] [archimedean α]
/-- An archimedean decidable linearly ordered `add_comm_group` has a version of the floor: for
`a > 0`, any `g` in the group lies between some two consecutive multiples of `a`. -/
lemma exists_int_smul_near_of_pos {a : α} (ha : 0 < a) (g : α) :
∃ (k : ℤ), k • a ≤ g ∧ g < (k + 1) • a :=
begin
let s : set ℤ := {n : ℤ | n • a ≤ g},
obtain ⟨k, hk : -g ≤ k • a⟩ := archimedean.arch (-g) ha,
have h_ne : s.nonempty := ⟨-k, by simpa using neg_le_neg hk⟩,
obtain ⟨k, hk⟩ := archimedean.arch g ha,
have h_bdd : ∀ n ∈ s, n ≤ (k : ℤ),
{ assume n hn,
apply (gsmul_le_gsmul_iff ha).mp,
rw ← gsmul_coe_nat at hk,
exact le_trans hn hk },
obtain ⟨m, hm, hm'⟩ := int.exists_greatest_of_bdd ⟨k, h_bdd⟩ h_ne,
refine ⟨m, hm, _⟩,
by_contra H,
linarith [hm' _ $ not_lt.mp H]
end
lemma exists_int_smul_near_of_pos' {a : α} (ha : 0 < a) (g : α) :
∃ (k : ℤ), 0 ≤ g - k • a ∧ g - k • a < a :=
begin
obtain ⟨k, h1, h2⟩ := exists_int_smul_near_of_pos ha g,
rw add_gsmul at h2,
refine ⟨k, sub_nonneg.mpr h1, _⟩,
simpa [sub_lt_iff_lt_add'] using h2
end
end linear_ordered_add_comm_group
theorem exists_nat_gt [ordered_semiring α] [nontrivial α] [archimedean α]
(x : α) : ∃ n : ℕ, x < n :=
let ⟨n, h⟩ := archimedean.arch x zero_lt_one in
⟨n+1, lt_of_le_of_lt (by rwa ← nsmul_one)
(nat.cast_lt.2 (nat.lt_succ_self _))⟩
theorem exists_nat_ge [ordered_semiring α] [archimedean α] (x : α) :
∃ n : ℕ, x ≤ n :=
begin
nontriviality α,
exact (exists_nat_gt x).imp (λ n, le_of_lt)
end
lemma add_one_pow_unbounded_of_pos [ordered_semiring α] [nontrivial α] [archimedean α]
(x : α) {y : α} (hy : 0 < y) :
∃ n : ℕ, x < (y + 1) ^ n :=
have 0 ≤ 1 + y, from add_nonneg zero_le_one hy.le,
let ⟨n, h⟩ := archimedean.arch x hy in
⟨n, calc x ≤ n • y : h
... = n * y : nsmul_eq_mul _ _
... < 1 + n * y : lt_one_add _
... ≤ (1 + y) ^ n : one_add_mul_le_pow' (mul_nonneg hy.le hy.le) (mul_nonneg this this)
(add_nonneg zero_le_two hy.le) _
... = (y + 1) ^ n : by rw [add_comm]⟩
section linear_ordered_ring
variables [linear_ordered_ring α] [archimedean α]
lemma pow_unbounded_of_one_lt (x : α) {y : α} (hy1 : 1 < y) :
∃ n : ℕ, x < y ^ n :=
sub_add_cancel y 1 ▸ add_one_pow_unbounded_of_pos _ (sub_pos.2 hy1)
/-- Every x greater than or equal to 1 is between two successive
natural-number powers of every y greater than one. -/
lemma exists_nat_pow_near {x : α} {y : α} (hx : 1 ≤ x) (hy : 1 < y) :
∃ n : ℕ, y ^ n ≤ x ∧ x < y ^ (n + 1) :=
have h : ∃ n : ℕ, x < y ^ n, from pow_unbounded_of_one_lt _ hy,
by classical; exact let n := nat.find h in
have hn : x < y ^ n, from nat.find_spec h,
have hnp : 0 < n, from pos_iff_ne_zero.2 (λ hn0,
by rw [hn0, pow_zero] at hn; exact (not_le_of_gt hn hx)),
have hnsp : nat.pred n + 1 = n, from nat.succ_pred_eq_of_pos hnp,
have hltn : nat.pred n < n, from nat.pred_lt (ne_of_gt hnp),
⟨nat.pred n, le_of_not_lt (nat.find_min h hltn), by rwa hnsp⟩
theorem exists_int_gt (x : α) : ∃ n : ℤ, x < n :=
let ⟨n, h⟩ := exists_nat_gt x in ⟨n, by rwa ← coe_coe⟩
theorem exists_int_lt (x : α) : ∃ n : ℤ, (n : α) < x :=
let ⟨n, h⟩ := exists_int_gt (-x) in ⟨-n, by rw int.cast_neg; exact neg_lt.1 h⟩
theorem exists_floor (x : α) :
∃ (fl : ℤ), ∀ (z : ℤ), z ≤ fl ↔ (z : α) ≤ x :=
begin
haveI := classical.prop_decidable,
have : ∃ (ub : ℤ), (ub:α) ≤ x ∧ ∀ (z : ℤ), (z:α) ≤ x → z ≤ ub :=
int.exists_greatest_of_bdd
(let ⟨n, hn⟩ := exists_int_gt x in ⟨n, λ z h',
int.cast_le.1 $ le_trans h' $ le_of_lt hn⟩)
(let ⟨n, hn⟩ := exists_int_lt x in ⟨n, le_of_lt hn⟩),
refine this.imp (λ fl h z, _),
cases h with h₁ h₂,
exact ⟨λ h, le_trans (int.cast_le.2 h) h₁, h₂ z⟩,
end
end linear_ordered_ring
section linear_ordered_field
variables [linear_ordered_field α]
/-- Every positive `x` is between two successive integer powers of
another `y` greater than one. This is the same as `exists_int_pow_near'`,
but with ≤ and < the other way around. -/
lemma exists_int_pow_near [archimedean α]
{x : α} {y : α} (hx : 0 < x) (hy : 1 < y) :
∃ n : ℤ, y ^ n ≤ x ∧ x < y ^ (n + 1) :=
by classical; exact
let ⟨N, hN⟩ := pow_unbounded_of_one_lt x⁻¹ hy in
have he: ∃ m : ℤ, y ^ m ≤ x, from
⟨-N, le_of_lt (by { rw [fpow_neg y (↑N), gpow_coe_nat],
exact (inv_lt hx (lt_trans (inv_pos.2 hx) hN)).1 hN })⟩,
let ⟨M, hM⟩ := pow_unbounded_of_one_lt x hy in
have hb: ∃ b : ℤ, ∀ m, y ^ m ≤ x → m ≤ b, from
⟨M, λ m hm, le_of_not_lt (λ hlt, not_lt_of_ge
(fpow_le_of_le (le_of_lt hy) (le_of_lt hlt))
(lt_of_le_of_lt hm (by rwa ← gpow_coe_nat at hM)))⟩,
let ⟨n, hn₁, hn₂⟩ := int.exists_greatest_of_bdd hb he in
⟨n, hn₁, lt_of_not_ge (λ hge, not_le_of_gt (int.lt_succ _) (hn₂ _ hge))⟩
/-- Every positive `x` is between two successive integer powers of
another `y` greater than one. This is the same as `exists_int_pow_near`,
but with ≤ and < the other way around. -/
lemma exists_int_pow_near' [archimedean α]
{x : α} {y : α} (hx : 0 < x) (hy : 1 < y) :
∃ n : ℤ, y ^ n < x ∧ x ≤ y ^ (n + 1) :=
let ⟨m, hle, hlt⟩ := exists_int_pow_near (inv_pos.2 hx) hy in
have hyp : 0 < y, from lt_trans zero_lt_one hy,
⟨-(m+1),
by rwa [fpow_neg, inv_lt (fpow_pos_of_pos hyp _) hx],
by rwa [neg_add, neg_add_cancel_right, fpow_neg,
le_inv hx (fpow_pos_of_pos hyp _)]⟩
/-- For any `y < 1` and any positive `x`, there exists `n : ℕ` with `y ^ n < x`. -/
lemma exists_pow_lt_of_lt_one [archimedean α] {x y : α} (hx : 0 < x) (hy : y < 1) :
∃ n : ℕ, y ^ n < x :=
begin
by_cases y_pos : y ≤ 0,
{ use 1, simp only [pow_one], linarith, },
rw [not_le] at y_pos,
rcases pow_unbounded_of_one_lt (x⁻¹) (one_lt_inv y_pos hy) with ⟨q, hq⟩,
exact ⟨q, by rwa [inv_pow', inv_lt_inv hx (pow_pos y_pos _)] at hq⟩
end
/-- Given `x` and `y` between `0` and `1`, `x` is between two successive powers of `y`.
This is the same as `exists_nat_pow_near`, but for elements between `0` and `1` -/
lemma exists_nat_pow_near_of_lt_one [archimedean α]
{x : α} {y : α} (xpos : 0 < x) (hx : x ≤ 1) (ypos : 0 < y) (hy : y < 1) :
∃ n : ℕ, y ^ (n + 1) < x ∧ x ≤ y ^ n :=
begin
rcases exists_nat_pow_near (one_le_inv_iff.2 ⟨xpos, hx⟩) (one_lt_inv_iff.2 ⟨ypos, hy⟩)
with ⟨n, hn, h'n⟩,
refine ⟨n, _, _⟩,
{ rwa [inv_pow', inv_lt_inv xpos (pow_pos ypos _)] at h'n },
{ rwa [inv_pow', inv_le_inv (pow_pos ypos _) xpos] at hn }
end
variables [floor_ring α]
lemma sub_floor_div_mul_nonneg (x : α) {y : α} (hy : 0 < y) :
0 ≤ x - ⌊x / y⌋ * y :=
begin
conv in x {rw ← div_mul_cancel x (ne_of_lt hy).symm},
rw ← sub_mul,
exact mul_nonneg (sub_nonneg.2 (floor_le _)) (le_of_lt hy)
end
lemma sub_floor_div_mul_lt (x : α) {y : α} (hy : 0 < y) :
x - ⌊x / y⌋ * y < y :=
sub_lt_iff_lt_add.2 begin
conv in y {rw ← one_mul y},
conv in x {rw ← div_mul_cancel x (ne_of_lt hy).symm},
rw ← add_mul,
exact (mul_lt_mul_right hy).2 (by rw add_comm; exact lt_floor_add_one _),
end
end linear_ordered_field
instance : archimedean ℕ :=
⟨λ n m m0, ⟨n, by simpa only [mul_one, nat.nsmul_eq_mul] using nat.mul_le_mul_left n m0⟩⟩
instance : archimedean ℤ :=
⟨λ n m m0, ⟨n.to_nat, le_trans (int.le_to_nat _) $
by simpa only [nsmul_eq_mul, int.nat_cast_eq_coe_nat, zero_add, mul_one]
using mul_le_mul_of_nonneg_left (int.add_one_le_iff.2 m0) (int.coe_zero_le n.to_nat)⟩⟩
/-- A linear ordered archimedean ring is a floor ring. This is not an `instance` because in some
cases we have a computable `floor` function. -/
noncomputable def archimedean.floor_ring (α)
[linear_ordered_ring α] [archimedean α] : floor_ring α :=
{ floor := λ x, classical.some (exists_floor x),
le_floor := λ z x, classical.some_spec (exists_floor x) z }
section linear_ordered_field
variables [linear_ordered_field α]
theorem archimedean_iff_nat_lt :
archimedean α ↔ ∀ x : α, ∃ n : ℕ, x < n :=
⟨@exists_nat_gt α _ _, λ H, ⟨λ x y y0,
(H (x / y)).imp $ λ n h, le_of_lt $
by rwa [div_lt_iff y0, ← nsmul_eq_mul] at h⟩⟩
theorem archimedean_iff_nat_le :
archimedean α ↔ ∀ x : α, ∃ n : ℕ, x ≤ n :=
archimedean_iff_nat_lt.trans
⟨λ H x, (H x).imp $ λ _, le_of_lt,
λ H x, let ⟨n, h⟩ := H x in ⟨n+1,
lt_of_le_of_lt h (nat.cast_lt.2 (lt_add_one _))⟩⟩
theorem exists_rat_gt [archimedean α] (x : α) : ∃ q : ℚ, x < q :=
let ⟨n, h⟩ := exists_nat_gt x in ⟨n, by rwa rat.cast_coe_nat⟩
theorem archimedean_iff_rat_lt :
archimedean α ↔ ∀ x : α, ∃ q : ℚ, x < q :=
⟨@exists_rat_gt α _,
λ H, archimedean_iff_nat_lt.2 $ λ x,
let ⟨q, h⟩ := H x in
⟨nat_ceil q, lt_of_lt_of_le h $
by simpa only [rat.cast_coe_nat] using (@rat.cast_le α _ _ _).2 (le_nat_ceil _)⟩⟩
theorem archimedean_iff_rat_le :
archimedean α ↔ ∀ x : α, ∃ q : ℚ, x ≤ q :=
archimedean_iff_rat_lt.trans
⟨λ H x, (H x).imp $ λ _, le_of_lt,
λ H x, let ⟨n, h⟩ := H x in ⟨n+1,
lt_of_le_of_lt h (rat.cast_lt.2 (lt_add_one _))⟩⟩
variable [archimedean α]
theorem exists_rat_lt (x : α) : ∃ q : ℚ, (q : α) < x :=
let ⟨n, h⟩ := exists_int_lt x in ⟨n, by rwa rat.cast_coe_int⟩
theorem exists_rat_btwn {x y : α} (h : x < y) : ∃ q : ℚ, x < q ∧ (q:α) < y :=
begin
cases exists_nat_gt (y - x)⁻¹ with n nh,
cases exists_floor (x * n) with z zh,
refine ⟨(z + 1 : ℤ) / n, _⟩,
have n0' := (inv_pos.2 (sub_pos.2 h)).trans nh,
have n0 := nat.cast_pos.1 n0',
rw [rat.cast_div_of_ne_zero, rat.cast_coe_nat, rat.cast_coe_int, div_lt_iff n0'],
refine ⟨(lt_div_iff n0').2 $
(lt_iff_lt_of_le_iff_le (zh _)).1 (lt_add_one _), _⟩,
rw [int.cast_add, int.cast_one],
refine lt_of_le_of_lt (add_le_add_right ((zh _).1 (le_refl _)) _) _,
rwa [← lt_sub_iff_add_lt', ← sub_mul,
← div_lt_iff' (sub_pos.2 h), one_div],
{ rw [rat.coe_int_denom, nat.cast_one], exact one_ne_zero },
{ intro H, rw [rat.coe_nat_num, ← coe_coe, nat.cast_eq_zero] at H, subst H, cases n0 },
{ rw [rat.coe_nat_denom, nat.cast_one], exact one_ne_zero }
end
theorem exists_nat_one_div_lt {ε : α} (hε : 0 < ε) : ∃ n : ℕ, 1 / (n + 1: α) < ε :=
begin
cases exists_nat_gt (1/ε) with n hn,
use n,
rw [div_lt_iff, ← div_lt_iff' hε],
{ apply hn.trans,
simp [zero_lt_one] },
{ exact n.cast_add_one_pos }
end
theorem exists_pos_rat_lt {x : α} (x0 : 0 < x) : ∃ q : ℚ, 0 < q ∧ (q : α) < x :=
by simpa only [rat.cast_pos] using exists_rat_btwn x0
end linear_ordered_field
section
variables [linear_ordered_field α] [floor_ring α]
/-- `round` rounds a number to the nearest integer. `round (1 / 2) = 1` -/
def round (x : α) : ℤ := ⌊x + 1 / 2⌋
@[simp] lemma round_zero : round (0 : α) = 0 := floor_eq_iff.2 (by norm_num)
@[simp] lemma round_one : round (1 : α) = 1 := floor_eq_iff.2 (by norm_num)
lemma abs_sub_round (x : α) : abs (x - round x) ≤ 1 / 2 :=
begin
rw [round, abs_sub_le_iff],
have := floor_le (x + 1 / 2),
have := lt_floor_add_one (x + 1 / 2),
split; linarith
end
@[simp, norm_cast] theorem rat.cast_floor (x : ℚ) : ⌊(x:α)⌋ = ⌊x⌋ :=
floor_eq_iff.2 (by exact_mod_cast floor_eq_iff.1 (eq.refl ⌊x⌋))
@[simp, norm_cast] theorem rat.cast_ceil (x : ℚ) : ⌈(x:α)⌉ = ⌈x⌉ :=
by rw [ceil, ← rat.cast_neg, rat.cast_floor, ← ceil]
@[simp, norm_cast] theorem rat.cast_round (x : ℚ) : round (x:α) = round x :=
have ((x + 1 / 2 : ℚ) : α) = x + 1 / 2, by simp,
by rw [round, round, ← this, rat.cast_floor]
end
section
variables [linear_ordered_field α] [archimedean α]
theorem exists_rat_near (x : α) {ε : α} (ε0 : 0 < ε) :
∃ q : ℚ, abs (x - q) < ε :=
let ⟨q, h₁, h₂⟩ := exists_rat_btwn $
lt_trans ((sub_lt_self_iff x).2 ε0) ((lt_add_iff_pos_left x).2 ε0) in
⟨q, abs_sub_lt_iff.2 ⟨sub_lt.1 h₁, sub_lt_iff_lt_add.2 h₂⟩⟩
instance : archimedean ℚ :=
archimedean_iff_rat_le.2 $ λ q, ⟨q, by rw rat.cast_id⟩
end
|
1cd6412bdc07fc57e92885418ac471949cfa1b14 | b7f22e51856f4989b970961f794f1c435f9b8f78 | /tests/lean/run/ind6.lean | fa213dd3248d06bbfd36b2935d459c5eb6896e50 | [
"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 | inductive tree.{u} (A : Type.{u}) : Type.{max u 1} :=
| node : A → forest.{u} A → tree.{u} A
with forest : Type.{max u 1} :=
| nil : forest.{u} A
| cons : tree.{u} A → forest.{u} A → forest.{u} A
check tree.{1}
check forest.{1}
check tree.rec.{1 1}
check forest.rec.{1 1}
|
16f4dc0373e93988f79164c1049de6f6a87b6319 | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/order/filter/pi.lean | db98c92f4c49146286ab5b8e08d5bf0cfcc3eeff | [
"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 | 7,292 | lean | /-
Copyright (c) 2021 Yury G. Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury G. Kudryashov, Alex Kontorovich
-/
import order.filter.bases
/-!
# (Co)product of a family of filters
In this file we define two filters on `Π i, α i` and prove some basic properties of these filters.
* `filter.pi (f : Π i, filter (α i))` to be the maximal filter on `Π i, α i` such that
`∀ i, filter.tendsto (function.eval i) (filter.pi f) (f i)`. It is defined as
`Π i, filter.comap (function.eval i) (f i)`. This is a generalization of `filter.prod` to indexed
products.
* `filter.Coprod (f : Π i, filter (α i))`: a generalization of `filter.coprod`; it is the supremum
of `comap (eval i) (f i)`.
-/
open set function
open_locale classical filter
namespace filter
variables {ι : Type*} {α : ι → Type*} {f f₁ f₂ : Π i, filter (α i)} {s : Π i, set (α i)}
section pi
/-- The product of an indexed family of filters. -/
def pi (f : Π i, filter (α i)) : filter (Π i, α i) := ⨅ i, comap (eval i) (f i)
lemma tendsto_eval_pi (f : Π i, filter (α i)) (i : ι) :
tendsto (eval i) (pi f) (f i) :=
tendsto_infi' i tendsto_comap
lemma tendsto_pi {β : Type*} {m : β → Π i, α i} {l : filter β} :
tendsto m l (pi f) ↔ ∀ i, tendsto (λ x, m x i) l (f i) :=
by simp only [pi, tendsto_infi, tendsto_comap_iff]
lemma le_pi {g : filter (Π i, α i)} : g ≤ pi f ↔ ∀ i, tendsto (eval i) g (f i) := tendsto_pi
@[mono] lemma pi_mono (h : ∀ i, f₁ i ≤ f₂ i) : pi f₁ ≤ pi f₂ :=
infi_le_infi $ λ i, comap_mono $ h i
lemma mem_pi_of_mem (i : ι) {s : set (α i)} (hs : s ∈ f i) :
eval i ⁻¹' s ∈ pi f :=
mem_infi_of_mem i $ preimage_mem_comap hs
lemma pi_mem_pi {I : set ι} (hI : finite I) (h : ∀ i ∈ I, s i ∈ f i) :
I.pi s ∈ pi f :=
begin
rw [pi_def, bInter_eq_Inter],
refine mem_infi_of_Inter hI (λ i, _) subset.rfl,
exact preimage_mem_comap (h i i.2)
end
lemma mem_pi {s : set (Π i, α i)} : s ∈ pi f ↔
∃ (I : set ι), finite I ∧ ∃ t : Π i, set (α i), (∀ i, t i ∈ f i) ∧ I.pi t ⊆ s :=
begin
split,
{ simp only [pi, mem_infi', mem_comap, pi_def],
rintro ⟨I, If, V, hVf, hVI, rfl, -⟩, choose t htf htV using hVf,
exact ⟨I, If, t, htf, Inter₂_mono (λ i _, htV i)⟩ },
{ rintro ⟨I, If, t, htf, hts⟩,
exact mem_of_superset (pi_mem_pi If $ λ i _, htf i) hts }
end
lemma mem_pi' {s : set (Π i, α i)} : s ∈ pi f ↔
∃ (I : finset ι), ∃ t : Π i, set (α i), (∀ i, t i ∈ f i) ∧ set.pi ↑I t ⊆ s :=
mem_pi.trans exists_finite_iff_finset
lemma mem_of_pi_mem_pi [∀ i, ne_bot (f i)] {I : set ι} (h : I.pi s ∈ pi f) {i : ι} (hi : i ∈ I) :
s i ∈ f i :=
begin
rcases mem_pi.1 h with ⟨I', I'f, t, htf, hts⟩,
refine mem_of_superset (htf i) (λ x hx, _),
have : ∀ i, (t i).nonempty, from λ i, nonempty_of_mem (htf i),
choose g hg,
have : update g i x ∈ I'.pi t,
{ intros j hj, rcases eq_or_ne j i with (rfl|hne); simp * },
simpa using hts this i hi
end
@[simp] lemma pi_mem_pi_iff [∀ i, ne_bot (f i)] {I : set ι} (hI : finite I) :
I.pi s ∈ pi f ↔ ∀ i ∈ I, s i ∈ f i :=
⟨λ h i hi, mem_of_pi_mem_pi h hi, pi_mem_pi hI⟩
@[simp] lemma pi_inf_principal_univ_pi_eq_bot :
pi f ⊓ 𝓟 (set.pi univ s) = ⊥ ↔ ∃ i, f i ⊓ 𝓟 (s i) = ⊥ :=
begin
split,
{ simp only [inf_principal_eq_bot, mem_pi], contrapose!,
rintros (hsf : ∀ i, ∃ᶠ x in f i, x ∈ s i) I If t htf hts,
have : ∀ i, (s i ∩ t i).nonempty, from λ i, ((hsf i).and_eventually (htf i)).exists,
choose x hxs hxt,
exact hts (λ i hi, hxt i) (mem_univ_pi.2 hxs) },
{ simp only [inf_principal_eq_bot],
rintro ⟨i, hi⟩,
filter_upwards [mem_pi_of_mem i hi] with x using mt (λ h, h i trivial), },
end
@[simp] lemma pi_inf_principal_pi_eq_bot [Π i, ne_bot (f i)] {I : set ι} :
pi f ⊓ 𝓟 (set.pi I s) = ⊥ ↔ ∃ i ∈ I, f i ⊓ 𝓟 (s i) = ⊥ :=
begin
rw [← univ_pi_piecewise I, pi_inf_principal_univ_pi_eq_bot],
refine exists_congr (λ i, _),
by_cases hi : i ∈ I; simp [hi, (‹Π i, ne_bot (f i)› i).ne]
end
@[simp] lemma pi_inf_principal_univ_pi_ne_bot :
ne_bot (pi f ⊓ 𝓟 (set.pi univ s)) ↔ ∀ i, ne_bot (f i ⊓ 𝓟 (s i)) :=
by simp [ne_bot_iff]
@[simp] lemma pi_inf_principal_pi_ne_bot [Π i, ne_bot (f i)] {I : set ι} :
ne_bot (pi f ⊓ 𝓟 (I.pi s)) ↔ ∀ i ∈ I, ne_bot (f i ⊓ 𝓟 (s i)) :=
by simp [ne_bot_iff]
instance pi_inf_principal_pi.ne_bot [h : ∀ i, ne_bot (f i ⊓ 𝓟 (s i))] {I : set ι} :
ne_bot (pi f ⊓ 𝓟 (I.pi s)) :=
(pi_inf_principal_univ_pi_ne_bot.2 ‹_›).mono $ inf_le_inf_left _ $ principal_mono.2 $
λ x hx i hi, hx i trivial
@[simp] lemma pi_eq_bot : pi f = ⊥ ↔ ∃ i, f i = ⊥ :=
by simpa using @pi_inf_principal_univ_pi_eq_bot ι α f (λ _, univ)
@[simp] lemma pi_ne_bot : ne_bot (pi f) ↔ ∀ i, ne_bot (f i) := by simp [ne_bot_iff]
instance [∀ i, ne_bot (f i)] : ne_bot (pi f) := pi_ne_bot.2 ‹_›
end pi
/-! ### `n`-ary coproducts of filters -/
section Coprod
/-- Coproduct of filters. -/
protected def Coprod (f : Π i, filter (α i)) : filter (Π i, α i) :=
⨆ i : ι, comap (eval i) (f i)
lemma mem_Coprod_iff {s : set (Π i, α i)} :
(s ∈ filter.Coprod f) ↔ (∀ i : ι, (∃ t₁ ∈ f i, eval i ⁻¹' t₁ ⊆ s)) :=
by simp [filter.Coprod]
lemma compl_mem_Coprod_iff {s : set (Π i, α i)} :
sᶜ ∈ filter.Coprod f ↔ ∃ t : Π i, set (α i), (∀ i, (t i)ᶜ ∈ f i) ∧ s ⊆ set.pi univ (λ i, t i) :=
begin
rw [(surjective_pi_map (λ i, @compl_surjective (set (α i)) _)).exists],
simp_rw [mem_Coprod_iff, classical.skolem, exists_prop, @subset_compl_comm _ _ s,
← preimage_compl, ← subset_Inter_iff, ← univ_pi_eq_Inter, compl_compl]
end
lemma Coprod_ne_bot_iff' :
ne_bot (filter.Coprod f) ↔ (∀ i, nonempty (α i)) ∧ ∃ d, ne_bot (f d) :=
by simp only [filter.Coprod, supr_ne_bot, ← exists_and_distrib_left, ← comap_eval_ne_bot_iff']
@[simp] lemma Coprod_ne_bot_iff [∀ i, nonempty (α i)] :
ne_bot (filter.Coprod f) ↔ ∃ d, ne_bot (f d) :=
by simp [Coprod_ne_bot_iff', *]
lemma ne_bot.Coprod [∀ i, nonempty (α i)] {i : ι} (h : ne_bot (f i)) :
ne_bot (filter.Coprod f) :=
Coprod_ne_bot_iff.2 ⟨i, h⟩
@[instance] lemma Coprod_ne_bot [∀ i, nonempty (α i)] [nonempty ι] (f : Π i, filter (α i))
[H : ∀ i, ne_bot (f i)] : ne_bot (filter.Coprod f) :=
(H (classical.arbitrary ι)).Coprod
@[mono] lemma Coprod_mono (hf : ∀ i, f₁ i ≤ f₂ i) : filter.Coprod f₁ ≤ filter.Coprod f₂ :=
supr_le_supr $ λ i, comap_mono (hf i)
variables {β : ι → Type*} {m : Π i, α i → β i}
lemma map_pi_map_Coprod_le :
map (λ (k : Π i, α i), λ i, m i (k i)) (filter.Coprod f) ≤ filter.Coprod (λ i, map (m i) (f i)) :=
begin
simp only [le_def, mem_map, mem_Coprod_iff],
intros s h i,
obtain ⟨t, H, hH⟩ := h i,
exact ⟨{x : α i | m i x ∈ t}, H, λ x hx, hH hx⟩
end
lemma tendsto.pi_map_Coprod {g : Π i, filter (β i)} (h : ∀ i, tendsto (m i) (f i) (g i)) :
tendsto (λ (k : Π i, α i), λ i, m i (k i)) (filter.Coprod f) (filter.Coprod g) :=
map_pi_map_Coprod_le.trans (Coprod_mono h)
end Coprod
end filter
|
680b3cea8bb28e3989e4e3e6244af771b79f299b | d1a52c3f208fa42c41df8278c3d280f075eb020c | /src/Lean/Elab/Tactic/Config.lean | 2a14f47119fe0cbf6cdd1ddc369d6e688dc1b51d | [
"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 | 846 | lean | /-
Copyright (c) 2021 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Elab.Tactic.Basic
import Lean.Elab.SyntheticMVars
namespace Lean.Elab.Tactic
open Meta
macro "declare_config_elab" elabName:ident type:ident : command =>
`(unsafe def evalUnsafe (e : Expr) : TermElabM $type :=
Term.evalExpr $type ``$type e
@[implementedBy evalUnsafe] constant eval (e : Expr) : TermElabM $type
def $elabName (optConfig : Syntax) : TermElabM $type := do
if optConfig.isNone then
return { : $type }
else
withoutModifyingState <| withLCtx {} {} <| Term.withSynthesize do
let c ← Term.elabTermEnsuringType optConfig[0][3] (Lean.mkConst ``$type)
eval (← instantiateMVars c)
)
end Lean.Elab.Tactic
|
6c866854e5166bb8dac008ab949956182516523f | dfbd4b284cc03e870b12af5532c592e0ad8d02f1 | /xenalib/M1Fstuff.lean | a2df17bfaf641c4fe314740420e4c5aa64f9b117 | [] | no_license | Shamrock-Frost/xena | 7a1e27df933f9321584e6256c0319e5a1afb9a38 | 9eb0c2c23363b963a2ebb2ea5bb37cc7dd602b1a | refs/heads/master | 1,626,672,528,677 | 1,509,140,744,000 | 1,509,140,744,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 17,970 | lean | import analysis.real init.classical
-- instance coe_rat_real : has_coe rat real := ⟨of_rat⟩
-- example : has_coe int real := by apply_instance
-- instance coe_int_real : has_coe int real := ⟨of_rat ∘ rat.of_int⟩
-- example : has_coe int real := by apply_instance
-- instance coe_nat_real : has_coe nat real := ⟨of_rat ∘ rat.of_int ∘ int.of_nat⟩
-- example : right_cancel_semigroup ℝ := by apply_instance
lemma of_rat_lt_of_rat {q₁ q₂ : ℚ} : of_rat q₁ < of_rat q₂ ↔ q₁ < q₂ :=
begin
simp [lt_iff_le_and_ne, of_rat_le_of_rat]
end
-- Helpful simp lemmas for reals: thanks to Sebastian Ullrich
run_cmd mk_simp_attr `real_simps
attribute [real_simps] of_rat_zero of_rat_one of_rat_neg of_rat_add of_rat_sub of_rat_mul
attribute [real_simps] of_rat_inv of_rat_le_of_rat of_rat_lt_of_rat
@[real_simps] lemma of_rat_bit0 (a : ℚ) : bit0 (of_rat a) = of_rat (bit0 a) := of_rat_add
@[real_simps] lemma of_rat_bit1 (a : ℚ) : bit1 (of_rat a) = of_rat (bit1 a) :=
by simp [bit1, bit0, of_rat_add,of_rat_one]
@[real_simps] lemma of_rat_div {r₁ r₂ : ℚ} : of_rat r₁ / of_rat r₂ = of_rat (r₁ / r₂) :=
by simp [has_div.div, algebra.div] with real_simps
-- I don't understand this code; however it is the only way that I as
-- a muggle know how to access norm_num. Thanks to Mario Carneiro
namespace tactic
meta def eval_num_tac : tactic unit :=
do t ← target,
(lhs, rhs) ← match_eq t,
(new_lhs, pr1) ← norm_num lhs,
(new_rhs, pr2) ← norm_num rhs,
is_def_eq new_lhs new_rhs,
`[exact eq.trans %%pr1 (eq.symm %%pr2)]
end tactic
-- variable α : Type
-- example : set α = (α → Prop) := rfl
/-
#print nonempty
-- set_option pp.all true
noncomputable def floor_with_proof : ℝ → ℤ := λ x,
begin
-- have H2 : 0+x < 1+x, by
-- apply add_lt_add_of_lt_of_le (zero_lt_one) (le_of_eq (rfl)),
-- have H3 : x < x+1, by simp [H2],
let rat_in_interval := {q // x < of_rat q ∧ of_rat q < x + 1},
have H : ∃ (q : ℚ), x < of_rat q ∧ of_rat q < x + 1,
exact @exists_lt_of_rat_of_rat_gt x (x+1) (by simp [zero_lt_one]),
have H2 : ∃ (s : rat_in_interval), true,
simp [H],
have H3 : nonempty rat_in_interval,
apply exists.elim H2,
intro a,
intro Pa,
constructor,
exact a,
have qHq : rat_in_interval := classical.choice (H3),
cases qHq with q Hq,
exact (if (x < rat.floor q) then rat.floor q - 1 else rat.floor q ),
end
-- theorems need classical logic
-- should it be
-- theorem floor_le : ∀ x, floor x ≤ x
-- or
-- theorem floor_ge : ∀ x, x ≥ floor x
-- or any other combination of these ideas?
-- How many? One? Four?
noncomputable theorem floor_le (x : ℝ) : ↑(floor x) ≤ x :=
begin
unfold floor,
simp,
have n : ℤ := floor x,
admit,
end
-- should I prove floor + 1 or 1 + floor?
theorem lt_floor_add_one (x : ℝ) : x < 1 + floor x := sorry
-/
-- set_option pp.notation false
-- set_option pp.all true
-- #check le_of_lt
-- noncomputable example : preorder ℝ := by apply_instance
-- #print sub_lt_iff
-- #check @rat.cast_le
-- #check sub_lt
theorem floor_real_exists : ∀ (x : ℝ), ∃ (n : ℤ), ↑n ≤ x ∧ x < n+1 :=
begin
intro x,
have H : ∃ (q : ℚ), x < ↑q ∧ ↑q < x + 1,
exact @exists_rat_btwn x (x+1) (by simp [zero_lt_one]),
cases H with q Hq,
cases classical.em (x < rat.floor q) with Hb Hs,
exact ⟨rat.floor q - 1,
begin
split,
simp [rat.floor_le q,Hq.right],
suffices H7 : (↑q:real) ≤ x+1,
exact calc (↑(rat.floor q):ℝ) = (↑((rat.floor q):ℚ):ℝ) : by simp
... ≤ (↑q:ℝ) : rat.cast_le.mpr (rat.floor_le q)
... ≤ x+1 : H7,
exact le_of_lt Hq.right,
simp,
rw [←add_assoc],
simp [Hb]
end
⟩,
exact ⟨rat.floor q,
begin
split,
{
have H : (x < ↑(rat.floor q)) ∨ (x ≥ ↑(rat.floor q)),
exact lt_or_ge x ↑(rat.floor q),
cases H with F T,
exact false.elim (Hs F),
exact T
},
{
clear Hs,
have H1 : x < ↑q,
{ exact Hq.left },
clear Hq,
/-
rw [←coe_rat_eq_of_rat] at H1,
suffices H2 : x < ↑(rat.floor q) + ↑(1:ℤ),
simp [H2],
suffices H3 : q < ↑(((rat.floor q):ℤ):ℚ) + ↑(1:ℤ),
exact lt_trans H1 _,-- (by rw [←rat.cast_add]),
-/
-- insanity starts here
suffices H2 : q < ↑((rat.floor q)+(1:ℤ)),
have H3 : ¬ (rat.floor q + 1 ≤ rat.floor q),
intro H4,
suffices H5 : rat.floor q < rat.floor q + 1,
exact (lt_iff_not_ge (rat.floor q) ((rat.floor q)+1)).mp H5 H4,
-- exact (lt_iff_not_ge q (((rat.floor q) + 1):int):rat).mpr,
simp,
tactic.swap,
apply (lt_iff_not_ge q _).mpr,
intro H2,
have H3 : (rat.floor q) + 1 ≤ rat.floor q,
exact rat.le_floor.mpr H2,
have H4: (1:ℤ) > 0,
exact int.one_pos,
suffices H5 : (rat.floor q) + 1 > rat.floor q,
exact (lt_iff_not_ge (rat.floor q) (rat.floor q + 1)).mp H5 H3,
-- rw [add_comm (rat.floor q) (1:ℤ)],
-- exact add_lt_add_left H4 (rat.floor q),add_zero (rat.floor q)],
have H6 :rat.floor q + 0 < rat.floor q + 1,
exact (add_lt_add_left H4 (rat.floor q)),
exact @eq.subst _ (λ y, y < rat.floor q + 1) _ _ (add_zero (rat.floor q)) H6,
clear H3,
suffices H3 : of_rat q < ↑(rat.floor q) + 1,
-- exact lt.trans H1 H3,
exact calc x < ↑q : H1
... = of_rat q : coe_rat_eq_of_rat q
... < ↑(rat.floor q) + 1 : H3,
clear H1,
rw [←coe_rat_eq_of_rat],
have H : (↑(rat.floor q):ℝ) + (1:ℝ) = (((rat.floor q):ℚ):ℝ) + (((1:ℤ):ℚ):ℝ),
simp,
rw [H,←rat.cast_add,rat.cast_lt,←int.cast_add],
exact H2
}
end⟩
end
theorem square_cont_at_zero : ∀ (r:ℝ), r>0 → ∃ (eps:ℝ),(eps>0) ∧ eps^2<r :=
begin
intros r Hrgt0,
cases classical.em (r<1) with Hrl1 Hrnl1,
have H : r^2<r,
unfold pow_nat has_pow_nat.pow_nat monoid.pow,
simp,
exact calc r*r < r*1 : mul_lt_mul_of_pos_left Hrl1 Hrgt0
... = r : mul_one r,
existsi r,
exact ⟨Hrgt0,H⟩,
have Hrge1 : r ≥ 1,
exact le_of_not_lt Hrnl1,
cases le_iff_eq_or_lt.mp Hrge1 with r1 rg1,
existsi ((1/2):ℝ),
split,
suffices H : 0 < ((1/2):ℝ),
exact H,
simp with real_simps,
exact dec_trivial,
-- *TODO* 1/2 > 0 doesn't work!
-- need of_rat_gt, not in realsimps
rw [←r1],
unfold pow_nat has_pow_nat.pow_nat monoid.pow,
simp with real_simps,
exact dec_trivial,
clear Hrnl1 Hrge1,
existsi (1:ℝ),
split,
exact zero_lt_one,
unfold pow_nat has_pow_nat.pow_nat monoid.pow,
simp,
exact rg1
end
theorem exists_square_root (r:ℝ) (rnneg : r ≥ 0) : ∃ (q : ℝ), (q ≥ 0) ∧ q^2=r :=
begin
cases le_iff_eq_or_lt.mp rnneg with r0 rpos,
rw [←r0],
have H : (0:ℝ)≥ 0 ∧ (0:ℝ)^2 = 0,
split,
exact le_of_eq (by simp),
unfold pow_nat has_pow_nat.pow_nat monoid.pow,
simp,
exact ⟨(0:ℝ),H⟩,
clear rnneg,
let s := { x:ℝ | x^2 ≤ r},
have H0 : (0:ℝ) ∈ s,
simp,
unfold pow_nat has_pow_nat.pow_nat monoid.pow,
simp,
exact le_of_lt rpos,
have H1 : max r 1 ∈ upper_bounds s,
cases classical.em (r ≤ 1) with rle1 rgt1,
unfold upper_bounds,
unfold set_of,
intro t,
intro Ht,
suffices H : t ≤ 1,
exact le_trans H (le_max_right r 1),
have H : t^2 ≤ 1,
exact le_trans Ht rle1,
cases classical.em (t≤1) with tle1 tgt1,
exact tle1,
have H2: t > 1,
exact lt_of_not_ge tgt1,
exfalso,
have H3 : t*t>1,
exact calc 1<t : H2
... = t*1 : eq.symm (mul_one t)
... < t*t : mul_lt_mul_of_pos_left H2 (lt_trans zero_lt_one H2),
unfold pow_nat has_pow_nat.pow_nat monoid.pow at H,
simp at H,
exact not_lt_of_ge H H3,
have H : 1<r,
exact lt_of_not_ge rgt1,
unfold upper_bounds,
unfold set_of,
intro t,
intro Ht,
suffices H : t ≤ r,
exact le_trans H (le_max_left r 1),
cases classical.em (t≤r) with Hl Hg,
exact Hl,
have H1 : r<t,
exact lt_of_not_ge Hg,
have H2 : t^2 ≤ r,
exact Ht,
clear H0 Ht s Hg rgt1,
exfalso,
have H3 : 1<t,
exact lt_trans H H1,
have H4 : t^2 < t,
exact lt_of_le_of_lt H2 H1,
have H5 : t < t^2,
exact calc t = 1*t : eq.symm (one_mul t)
... < t*t : mul_lt_mul_of_pos_right H3 (lt_trans zero_lt_one H3)
... = t^2 : by {unfold pow_nat has_pow_nat.pow_nat monoid.pow,simp},
have H6 : t < t,
exact lt_trans H5 H4,
have H7 : ¬ (t=t),
exact ne_of_lt H6,
exact H7 (rfl),
have H : ∃ (x : ℝ), is_lub s x,
exact exists_supremum_real H0 H1,
cases H with q Hq,
existsi q,
unfold is_lub at Hq,
unfold is_least at Hq,
split,
exact Hq.left 0 H0,
have Hqge0 : 0 ≤ q,
exact Hq.left 0 H0,
-- idea is to prove q^2=r by showing not < or >
-- first not <
have H2 : ¬ (q^2<r),
intro Hq2r,
have H2 : q ∈ upper_bounds s,
exact Hq.left,
clear Hq H0 H1,
unfold upper_bounds at H2,
have H3 : ∀ qe, q<qe → ¬(qe^2≤r),
intro qe,
intro qlqe,
intro H4,
have H5 : qe ≤ q,
exact H2 qe H4,
exact not_lt_of_ge H5 qlqe,
have H4 : ∀ eps > 0,(q+eps)^2>r,
intros eps Heps,
exact lt_of_not_ge (H3 (q+eps) ((lt_add_iff_pos_right q).mpr Heps)),
clear H3 H2 s,
cases le_iff_eq_or_lt.mp Hqge0 with Hq0 Hqg0,
cases (square_cont_at_zero r rpos) with eps Heps,
specialize H4 eps,
rw [←Hq0] at H4,
simp at H4,
have H3 : eps^2>r,
exact H4 Heps.left,
exact (lt_iff_not_ge r (eps^2)).mp H3 (le_of_lt Heps.right),
clear Hqge0,
-- want eps such that 2*q*eps+eps^2 <= r-q^2
-- so eps=min((r-q^2)/4q,thing-produced-by-square-cts-function)
have H0 : (0:ℝ)<2,
simp with real_simps,
exact dec_trivial,
have H : 0<(r-q^2),
exact sub_pos_of_lt Hq2r,
have H2 : 0 < (r-q^2)/2,
exact div_pos_of_pos_of_pos H H0,
have H3 : 0 < (r-q^2)/2/(2*q),
exact div_pos_of_pos_of_pos H2 (mul_pos H0 Hqg0),
cases (square_cont_at_zero ((r-q^2)/2) H2) with e0 He0,
let e1 := min ((r-q^2)/2/(2*q)) e0,
have He1 : e1>0,
exact lt_min H3 He0.left,
specialize H4 e1, -- should be a contradiction
have H1 : (q+e1)^2 > r,
exact H4 He1,
have H5 : e1 ≤ ((r-q^2)/2/(2*q)),
exact (min_le_left ((r-q^2)/2/(2*q)) e0),
have H6 : e1*e1<(r - q ^ 2) / 2,
exact calc e1*e1 ≤ e0*e1 : mul_le_mul_of_nonneg_right (min_le_right ((r - q ^ 2) / 2 / (2 * q)) e0) (le_of_lt He1)
... ≤ e0*e0 : mul_le_mul_of_nonneg_left (min_le_right ((r - q ^ 2) / 2 / (2 * q)) e0) (le_of_lt He0.left )
... = e0^2 : by {unfold pow_nat has_pow_nat.pow_nat monoid.pow,simp}
... < (r-q^2)/2 : He0.right,
have Hn1 : (q+e1)^2 < r,
exact calc (q+e1)^2 = (q+e1)*(q+e1) : by {unfold pow_nat has_pow_nat.pow_nat monoid.pow,simp}
... = q*q+2*q*e1+e1*e1 : by rw [mul_add,add_mul,add_mul,mul_comm e1 q,two_mul,add_mul,add_assoc,add_assoc,add_assoc]
... = q^2 + (2*q)*e1 + e1*e1 : by {unfold pow_nat has_pow_nat.pow_nat monoid.pow,simp}
... ≤ q^2 + (2*q)*((r - q ^ 2) / 2 / (2 * q)) + e1*e1 : add_le_add_right (add_le_add_left ((mul_le_mul_left (mul_pos H0 Hqg0)).mpr H5) (q^2)) (e1*e1)
... < q^2 + (2*q)*((r - q ^ 2) / 2 / (2 * q)) + (r-q^2)/2 : add_lt_add_left H6 _
... = r : by rw [mul_comm,div_mul_eq_mul_div,mul_div_assoc,div_self (ne_of_gt (mul_pos H0 Hqg0)),mul_one,add_assoc,div_add_div_same,←two_mul,mul_comm,mul_div_assoc,div_self (ne_of_gt H0),mul_one,add_sub,add_comm,←add_sub,sub_self,add_zero], -- rw [mul_div_cancel'], -- nearly there
exact not_lt_of_ge (le_of_lt H1) Hn1,
-- now not >
have H3 : ¬ (q^2>r),
intro Hq2r,
have H3 : q ∈ lower_bounds (upper_bounds s),
exact Hq.right,
clear Hq H0 H1 H2,
have Hqg0 : 0 < q,
cases le_iff_eq_or_lt.mp Hqge0 with Hq0 H,
tactic.swap,
exact H,
unfold pow_nat has_pow_nat.pow_nat monoid.pow at Hq2r,
rw [←Hq0] at Hq2r,
simp at Hq2r,
exfalso,
exact not_lt_of_ge (le_of_lt rpos) Hq2r,
clear Hqge0,
have H : ∀ (eps:ℝ), (eps > 0 ∧ eps < q) → (q-eps)^2 < r,
unfold lower_bounds at H3,
unfold set_of at H3,
unfold has_mem.mem set.mem has_mem.mem at H3,
intros eps Heps,
have H : ¬ ((q-eps) ∈ (upper_bounds s)),
intro H,
have H2 : q ≤ q-eps,
exact H3 (q-eps) H,
rw [le_sub_iff_add_le] at H2,
have Hf : q<q,
exact calc
q < eps+q : lt_add_of_pos_left q Heps.left
... = q+eps : add_comm eps q
... ≤ q : H2,
have Hf2 : ¬ (q=q),
exact ne_of_lt Hf,
exact Hf2 (by simp),
unfold upper_bounds at H,
unfold has_mem.mem set.mem has_mem.mem set_of at H,
have H2 : ∃ (b:ℝ), ¬ (s b → b ≤ q-eps),
exact classical.not_forall.mp H,
cases H2 with b Hb,
clear H,
cases classical.em (s b) with Hsb Hsnb,
tactic.swap,
have Hnb : s b → b ≤ q - eps,
intro Hsb,
exfalso,
exact Hsnb Hsb,
exfalso,
exact Hb Hnb,
cases classical.em (b ≤ q - eps) with Hlt Hg,
exfalso,
exact Hb (λ _,Hlt),
have Hh : q-eps < b,
exact lt_of_not_ge Hg,
clear Hg Hb,
-- todo: (q-eps)>0, (q-eps)^2<b^2<=r,
have H0 : 0<q-eps,
rw [lt_sub_iff,zero_add],exact Heps.right,
unfold pow_nat has_pow_nat.pow_nat monoid.pow,
exact calc (q-eps)*((q-eps)*1) = (q-eps)*(q-eps) : congr_arg (λ t, (q-eps)*t) (mul_one (q-eps))
... < (q-eps) * b : mul_lt_mul_of_pos_left Hh H0
... < b * b : mul_lt_mul_of_pos_right Hh (lt_trans H0 Hh)
... = b^2 : by { unfold pow_nat has_pow_nat.pow_nat monoid.pow, simp}
... ≤ r : Hsb,
-- We now know (q-eps)^2<r for all eps>0, and q^2>r. Need a contradiction.
-- Idea: (q^2-2*q*eps+eps^2)<r so 2q.eps-eps^2>q^2-r>0,
-- so we need to find eps such that 2q.eps-eps^2<(q^2-r)
-- so set eps=min((q^2-r)/2q,q)
have H0 : (0:ℝ)<2,
simp with real_simps,
exact dec_trivial,
have H1 : 0<(q^2-r),
exact sub_pos_of_lt Hq2r,
have H2 : 0 < (q/2),
exact div_pos_of_pos_of_pos Hqg0 H0,
have J1 : 0 < (q^2-r)/(2*q),
exact div_pos_of_pos_of_pos H1 (mul_pos H0 Hqg0),
let e1 := min ((q^2-r)/(2*q)) (q/2),
have He1 : e1>0,
exact lt_min J1 H2,
specialize H e1, -- should be a contradiction
have J0 : e1<q,
exact calc e1 ≤ (q/2) : min_le_right ((q^2-r)/(2*q)) (q/2)
... = q*(1/2) : by rw [←mul_div_assoc,mul_one]
... < q*1 : mul_lt_mul_of_pos_left (by simp with real_simps;exact dec_trivial) Hqg0
... = q : by rw [mul_one],
have H4 : (q-e1)^2 < r,
exact H ⟨He1,J0⟩,
have H5 : e1 ≤ ((q^2-r)/(2*q)),
exact (min_le_left ((q^2-r)/(2*q)) (q/2)),
have H6 : e1*e1>0,
exact mul_pos He1 He1,
have Hn1 : (q-e1)^2 > r,
exact calc (q-e1)^2 = (q-e1)*(q-e1) : by {unfold pow_nat has_pow_nat.pow_nat monoid.pow,simp}
... = q*q-2*q*e1+e1*e1 : by rw [mul_sub,sub_mul,sub_mul,mul_comm e1 q,two_mul,add_mul];simp
... = q^2 - (2*q)*e1 + e1*e1 : by {unfold pow_nat has_pow_nat.pow_nat monoid.pow,simp}
... > q^2 - (2*q)*e1 : lt_add_of_pos_right (q^2 -(2*q)*e1) H6
... ≥ q^2 - (2*q)*((q ^ 2 - r) / (2 * q)) : sub_le_sub (le_of_eq (eq.refl (q^2))) (mul_le_mul_of_nonneg_left H5 (le_of_lt (mul_pos H0 Hqg0))) -- lt_add_iff_pos_right -- (add_le_add_left ((mul_le_mul_left (mul_pos H0 Hqg0)).mpr H5) (q^2)) (e1*e1)
... = r : by rw [←div_mul_eq_mul_div_comm,div_self (ne_of_gt (mul_pos H0 Hqg0)),one_mul];simp, -- ... = r : by rw [mul_comm,div_mul_eq_mul_div,mul_div_assoc,div_self (ne_of_gt (mul_pos H0 Hqg0)),mul_one,add_assoc,div_add_div_same,←two_mul,mul_comm,mul_div_assoc,div_self (ne_of_gt H0),mul_one,add_sub,add_comm,←add_sub,sub_self,add_zero], -- rw [mul_div_cancel'], -- nearly there
exact not_lt_of_ge (le_of_lt (H ⟨He1,J0⟩)) Hn1,
have H : q^2 ≤ r,
exact le_of_not_lt H3,
cases lt_or_eq_of_le H with Hlt Heq,
exfalso,
exact H2 Hlt,
exact Heq
end
namespace M1F
lemma rat.zero_eq_int_zero (z : int) : ↑ z = (0:rat) → z = 0 :=
begin
simp [rat.mk_eq_zero,nat.one_pos,rat.coe_int_eq_mk]
end
lemma rat.of_int_inj (z₁ z₂ : int) : (z₁ : rat) = (z₂ : rat) → z₁ = z₂ :=
begin
intro H12,
have H2 : ↑(z₁ - z₂) = (0:rat),
exact calc
↑(z₁ - z₂) = (↑z₁ - ↑z₂ : ℚ) : by simp -- (rat.cast_sub z₁ z₂)
... = (↑ z₂ - ↑ z₂:ℚ) : by rw H12
... = (0 : rat) : by simp,
have H3 : z₁ - z₂ = 0,
exact rat.zero_eq_int_zero (z₁ -z₂) H2,
clear H12 H2,
exact sub_eq_zero.mp H3
end
lemma rational_half_not_an_integer : ¬ (∃ y : ℤ, 1/2 = (y:rat)) :=
begin
simp,
assume x:ℤ,
intro H,
unfold has_inv.inv at H, -- why does this become such an effort?
unfold division_ring.inv at H,
unfold field.inv at H,
unfold linear_ordered_field.inv at H,
unfold discrete_linear_ordered_field.inv at H,
unfold discrete_field.inv at H,
have H2 : ((2:rat)*rat.inv 2) = (2:rat)*x,
simp [H],
have H21 : (2:rat)≠ 0 := dec_trivial,
have H3 : (2:rat)*rat.inv 2 = (1:rat),
exact rat.mul_inv_cancel 2 H21,
have H4 : (1:rat) = (2:rat)*(x:rat),
exact H3 ▸ H2,
have H5 : ((((2:int)*x):int):rat)=((2:int):rat)*(x:rat),
simp [rat.cast_mul],
have H6 : ↑ ((2:int)*x) = (↑1:rat),
exact eq.trans H5 (eq.symm H4),
clear H H2 H21 H3 H4 H5,
have H7 : 2*x=1,
exact rat.of_int_inj (2*x) 1 H6,
clear H6,
have H8 : (2*x) % 2 = 0,
simp [@int.add_mul_mod_self 0],
have H9 : (1:int) % 2 = 0,
apply @eq.subst ℤ (λ t, t%2 =0) _ _ H7 H8,
have H10 : (1:int) % 2 ≠ 0,
exact dec_trivial,
contradiction,
end
lemma real_half_not_an_integer : ¬ (∃ y : ℤ, of_rat (1/2) = of_rat(y)) :=
begin
assume H : (∃ y : ℤ, of_rat (1/2) = of_rat(y)),
have H2 : (∃ y : ℤ , (1:rat)/2 = (y:rat)),
apply exists.elim H,
intro a,
intro H3,
existsi a,
exact (@of_rat_inj (1/2) (a:rat)).mp H3,
exact rational_half_not_an_integer H2,
end
-- #check @of_rat_inj
end M1F |
f8198f8397335e3c8603926ac2b053f7fc4fdc4d | c86b74188c4b7a462728b1abd659ab4e5828dd61 | /stage0/src/Lean/PrettyPrinter/Delaborator/Basic.lean | f4df5f2c139a01d770501252dd77823cba555ab7 | [
"Apache-2.0"
] | permissive | cwb96/lean4 | 75e1f92f1ba98bbaa6b34da644b3dfab2ce7bf89 | b48831cda76e64f13dd1c0edde7ba5fb172ed57a | refs/heads/master | 1,686,347,881,407 | 1,624,483,842,000 | 1,624,483,842,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 17,711 | lean | /-
Copyright (c) 2020 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sebastian Ullrich
-/
/-!
The delaborator is the first stage of the pretty printer, and the inverse of the
elaborator: it turns fully elaborated `Expr` core terms back into surface-level
`Syntax`, omitting some implicit information again and using higher-level syntax
abstractions like notations where possible. The exact behavior can be customized
using pretty printer options; activating `pp.all` should guarantee that the
delaborator is injective and that re-elaborating the resulting `Syntax`
round-trips.
Pretty printer options can be given not only for the whole term, but also
specific subterms. This is used both when automatically refining pp options
until round-trip and when interactively selecting pp options for a subterm (both
TBD). The association of options to subterms is done by assigning a unique,
synthetic Nat position to each subterm derived from its position in the full
term. This position is added to the corresponding Syntax object so that
elaboration errors and interactions with the pretty printer output can be traced
back to the subterm.
The delaborator is extensible via the `[delab]` attribute.
-/
import Lean.KeyedDeclsAttribute
import Lean.ProjFns
import Lean.Syntax
import Lean.Meta.Match
import Lean.Elab.Term
namespace Lean
register_builtin_option pp.all : Bool := {
defValue := false
group := "pp"
descr := "(pretty printer) display coercions, implicit parameters, proof terms, fully qualified names, universes, " ++
"and disable beta reduction and notations during pretty printing"
}
register_builtin_option pp.notation : Bool := {
defValue := true
group := "pp"
descr := "(pretty printer) disable/enable notation (infix, mixfix, postfix operators and unicode characters)"
}
register_builtin_option pp.coercions : Bool := {
defValue := true
group := "pp"
descr := "(pretty printer) hide coercion applications"
}
register_builtin_option pp.universes : Bool := {
defValue := false
group := "pp"
descr := "(pretty printer) display universes"
}
register_builtin_option pp.full_names : Bool := {
defValue := false
group := "pp"
descr := "(pretty printer) display fully qualified names"
}
register_builtin_option pp.private_names : Bool := {
defValue := false
group := "pp"
descr := "(pretty printer) display internal names assigned to private declarations"
}
register_builtin_option pp.binder_types : Bool := {
defValue := false
group := "pp"
descr := "(pretty printer) display types of lambda and Pi parameters"
}
register_builtin_option pp.structure_instances : Bool := {
defValue := true
group := "pp"
-- TODO: implement second part
descr := "(pretty printer) display structure instances using the '{ fieldName := fieldValue, ... }' notation " ++
"or '⟨fieldValue, ... ⟩' if structure is tagged with [pp_using_anonymous_constructor] attribute"
}
register_builtin_option pp.structure_projections : Bool := {
defValue := true
group := "pp"
descr := "(pretty printer) display structure projections using field notation"
}
register_builtin_option pp.explicit : Bool := {
defValue := false
group := "pp"
descr := "(pretty printer) display implicit arguments"
}
register_builtin_option pp.structure_instance_type : Bool := {
defValue := false
group := "pp"
descr := "(pretty printer) display type of structure instances"
}
register_builtin_option pp.safe_shadowing : Bool := {
defValue := true
group := "pp"
descr := "(pretty printer) allow variable shadowing if there is no collision"
}
register_builtin_option pp.proofs : Bool := {
defValue := false
group := "pp"
descr := "(pretty printer) if set to false, replace proofs appearing as an argument to a function with a placeholder"
}
register_builtin_option pp.proofs.withType : Bool := {
defValue := true
group := "pp"
descr := "(pretty printer) when eliding a proof (see `pp.proofs`), show its type instead"
}
register_builtin_option pp.motives.pi : Bool := {
defValue := true
group := "pp"
descr := "(pretty printer) print all motives that return pi types"
}
register_builtin_option pp.motives.nonConst : Bool := {
defValue := false
group := "pp"
descr := "(pretty printer) print all motives that are not constant functions"
}
register_builtin_option pp.motives.all : Bool := {
defValue := false
group := "pp"
descr := "(pretty printer) print all motives"
}
-- TODO:
/-
register_builtin_option g_pp_max_depth : Nat := {
defValue := false
group := "pp"
descr := "(pretty printer) maximum expression depth, after that it will use ellipsis"
}
register_builtin_option g_pp_max_steps : Nat := {
defValue := false
group := "pp"
descr := "(pretty printer) maximum number of visited expressions, after that it will use ellipsis"
}
register_builtin_option g_pp_locals_full_names : Bool := {
defValue := false
group := "pp"
descr := "(pretty printer) show full names of locals"
}
register_builtin_option g_pp_beta : Bool := {
defValue := false
group := "pp"
descr := "(pretty printer) apply beta-reduction when pretty printing"
}
register_builtin_option g_pp_goal_compact : Bool := {
defValue := false
group := "pp"
descr := "(pretty printer) try to display goal in a single line when possible"
}
register_builtin_option g_pp_goal_max_hyps : Nat := {
defValue := false
group := "pp"
descr := "(pretty printer) maximum number of hypotheses to be displayed"
}
register_builtin_option g_pp_instantiate_mvars : Bool := {
defValue := false
group := "pp"
descr := "(pretty printer) instantiate assigned metavariables before pretty printing terms and goals"
}
register_builtin_option g_pp_annotations : Bool := {
defValue := false
group := "pp"
descr := "(pretty printer) display internal annotations (for debugging purposes only)"
}
register_builtin_option g_pp_compact_let : Bool := {
defValue := false
group := "pp"
descr := "(pretty printer) minimal indentation at `let`-declarations"
}
-/
def getPPAll (o : Options) : Bool := o.get `pp.all false
def getPPBinderTypes (o : Options) : Bool := o.get `pp.binder_types true
def getPPCoercions (o : Options) : Bool := o.get `pp.coercions (!getPPAll o)
def getPPExplicit (o : Options) : Bool := o.get `pp.explicit (getPPAll o)
def getPPNotation (o : Options) : Bool := o.get `pp.notation (!getPPAll o)
def getPPStructureProjections (o : Options) : Bool := o.get `pp.structure_projections (!getPPAll o)
def getPPStructureInstances (o : Options) : Bool := o.get `pp.structure_instances (!getPPAll o)
def getPPStructureInstanceType (o : Options) : Bool := o.get `pp.structure_instance_type (getPPAll o)
def getPPUniverses (o : Options) : Bool := o.get `pp.universes (getPPAll o)
def getPPFullNames (o : Options) : Bool := o.get `pp.full_names (getPPAll o)
def getPPPrivateNames (o : Options) : Bool := o.get `pp.private_names (getPPAll o)
def getPPUnicode (o : Options) : Bool := o.get `pp.unicode true
def getPPSafeShadowing (o : Options) : Bool := o.get `pp.safe_shadowing true
def getPPProofs (o : Options) : Bool := o.get pp.proofs.name (getPPAll o)
def getPPProofsWithType (o : Options) : Bool := o.get pp.proofs.withType.name true
def getPPMotivesPi (o : Options) : Bool := o.get pp.motives.pi.name pp.motives.pi.defValue
def getPPMotivesNonConst (o : Options) : Bool := o.get pp.motives.nonConst.name pp.motives.nonConst.defValue
def getPPMotivesAll (o : Options) : Bool := o.get pp.motives.all.name pp.motives.all.defValue
/-- Associate pretty printer options to a specific subterm using a synthetic position. -/
abbrev OptionsPerPos := Std.RBMap Nat Options compare
namespace PrettyPrinter
namespace Delaborator
open Lean.Meta
structure Context where
-- In contrast to other systems like the elaborator, we do not pass the current term explicitly as a
-- parameter, but store it in the monad so that we can keep it in sync with `pos`.
expr : Expr
pos : Nat := 1
defaultOptions : Options
optionsPerPos : OptionsPerPos
currNamespace : Name
openDecls : List OpenDecl
inPattern : Bool := false -- true whe delaborating `match` patterns
-- Exceptions from delaborators are not expected. We use an internal exception to signal whether
-- the delaborator was able to produce a Syntax object.
builtin_initialize delabFailureId : InternalExceptionId ← registerInternalExceptionId `delabFailure
abbrev DelabM := ReaderT Context MetaM
abbrev Delab := DelabM Syntax
instance {α} : Inhabited (DelabM α) where
default := throw arbitrary
@[inline] protected def orElse {α} (d₁ d₂ : DelabM α) : DelabM α := do
catchInternalId delabFailureId d₁ (fun _ => d₂)
protected def failure {α} : DelabM α := throw $ Exception.internal delabFailureId
instance : Alternative DelabM := {
orElse := Delaborator.orElse,
failure := Delaborator.failure
}
-- HACK: necessary since it would otherwise prefer the instance from MonadExcept
instance {α} : OrElse (DelabM α) := ⟨Delaborator.orElse⟩
-- Macro scopes in the delaborator output are ultimately ignored by the pretty printer,
-- so give a trivial implementation.
instance : MonadQuotation DelabM := {
getCurrMacroScope := pure arbitrary,
getMainModule := pure arbitrary,
withFreshMacroScope := fun x => x
}
unsafe def mkDelabAttribute : IO (KeyedDeclsAttribute Delab) :=
KeyedDeclsAttribute.init {
builtinName := `builtinDelab,
name := `delab,
descr := "Register a delaborator.
[delab k] registers a declaration of type `Lean.PrettyPrinter.Delaborator.Delab` for the `Lean.Expr`
constructor `k`. Multiple delaborators for a single constructor are tried in turn until
the first success. If the term to be delaborated is an application of a constant `c`,
elaborators for `app.c` are tried first; this is also done for `Expr.const`s (\"nullary applications\")
to reduce special casing. If the term is an `Expr.mdata` with a single key `k`, `mdata.k`
is tried first.",
valueTypeName := `Lean.PrettyPrinter.Delaborator.Delab
} `Lean.PrettyPrinter.Delaborator.delabAttribute
@[builtinInit mkDelabAttribute] constant delabAttribute : KeyedDeclsAttribute Delab
def getExpr : DelabM Expr := do
let ctx ← read
pure ctx.expr
def getExprKind : DelabM Name := do
let e ← getExpr
pure $ match e with
| Expr.bvar _ _ => `bvar
| Expr.fvar _ _ => `fvar
| Expr.mvar _ _ => `mvar
| Expr.sort _ _ => `sort
| Expr.const c _ _ =>
-- we identify constants as "nullary applications" to reduce special casing
`app ++ c
| Expr.app fn _ _ => match fn.getAppFn with
| Expr.const c _ _ => `app ++ c
| _ => `app
| Expr.lam _ _ _ _ => `lam
| Expr.forallE _ _ _ _ => `forallE
| Expr.letE _ _ _ _ _ => `letE
| Expr.lit _ _ => `lit
| Expr.mdata m _ _ => match m.entries with
| [(key, _)] => `mdata ++ key
| _ => `mdata
| Expr.proj _ _ _ _ => `proj
/-- Evaluate option accessor, using subterm-specific options if set. -/
def getPPOption (opt : Options → Bool) : DelabM Bool := do
let ctx ← read
let mut opts := ctx.defaultOptions
if let some opts' ← ctx.optionsPerPos.find? ctx.pos then
for (k, v) in opts' do
opts := opts.insert k v
return opt opts
def whenPPOption (opt : Options → Bool) (d : Delab) : Delab := do
let b ← getPPOption opt
if b then d else failure
def whenNotPPOption (opt : Options → Bool) (d : Delab) : Delab := do
let b ← getPPOption opt
if b then failure else d
/--
Descend into `child`, the `childIdx`-th subterm of the current term, and update position.
Because `childIdx < 3` in the case of `Expr`, we can injectively map a path
`childIdxs` to a natural number by computing the value of the 3-ary representation
`1 :: childIdxs`, since n-ary representations without leading zeros are unique.
Note that `pos` is initialized to `1` (case `childIdxs == []`).
-/
def descend {α} (child : Expr) (childIdx : Nat) (d : DelabM α) : DelabM α :=
withReader (fun cfg => { cfg with expr := child, pos := cfg.pos * 3 + childIdx }) d
def withAppFn {α} (d : DelabM α) : DelabM α := do
let Expr.app fn _ _ ← getExpr | unreachable!
descend fn 0 d
def withAppArg {α} (d : DelabM α) : DelabM α := do
let Expr.app _ arg _ ← getExpr | unreachable!
descend arg 1 d
partial def withAppFnArgs {α} : DelabM α → (α → DelabM α) → DelabM α
| fnD, argD => do
let Expr.app fn arg _ ← getExpr | fnD
let a ← withAppFn (withAppFnArgs fnD argD)
withAppArg (argD a)
def withBindingDomain {α} (d : DelabM α) : DelabM α := do
let e ← getExpr
descend e.bindingDomain! 0 d
def withBindingBody {α} (n : Name) (d : DelabM α) : DelabM α := do
let e ← getExpr
withLocalDecl n e.binderInfo e.bindingDomain! fun fvar =>
let b := e.bindingBody!.instantiate1 fvar
descend b 1 d
def withProj {α} (d : DelabM α) : DelabM α := do
let Expr.proj _ _ e _ ← getExpr | unreachable!
descend e 0 d
def withMDataExpr {α} (d : DelabM α) : DelabM α := do
let Expr.mdata _ e _ ← getExpr | unreachable!
-- do not change position so that options on an mdata are automatically forwarded to the child
withReader ({ · with expr := e }) d
partial def annotatePos (pos : Nat) : Syntax → Syntax
| stx@(Syntax.ident _ _ _ _) => stx.setInfo (SourceInfo.synthetic pos pos)
-- app => annotate function
| stx@(Syntax.node `Lean.Parser.Term.app args) => stx.modifyArg 0 (annotatePos pos)
-- otherwise, annotate first direct child token if any
| stx => match stx.getArgs.findIdx? Syntax.isAtom with
| some idx => stx.modifyArg idx (Syntax.setInfo (SourceInfo.synthetic pos pos))
| none => stx
def annotateCurPos (stx : Syntax) : Delab := do
let ctx ← read
pure $ annotatePos ctx.pos stx
def getUnusedName (suggestion : Name) (body : Expr) : DelabM Name := do
-- Use a nicer binder name than `[anonymous]`. We probably shouldn't do this in all LocalContext use cases, so do it here.
let suggestion := if suggestion.isAnonymous then `a else suggestion;
let suggestion := suggestion.eraseMacroScopes
let lctx ← getLCtx
if !lctx.usesUserName suggestion then
return suggestion
else if (← getPPOption getPPSafeShadowing) && !bodyUsesSuggestion lctx suggestion then
return suggestion
else
return lctx.getUnusedName suggestion
where
bodyUsesSuggestion (lctx : LocalContext) (suggestion' : Name) : Bool :=
Option.isSome <| body.find? fun
| Expr.fvar fvarId _ =>
match lctx.find? fvarId with
| none => false
| some decl => decl.userName == suggestion'
| _ => false
def withBindingBodyUnusedName {α} (d : Syntax → DelabM α) : DelabM α := do
let n ← getUnusedName (← getExpr).bindingName! (← getExpr).bindingBody!
let stxN ← annotateCurPos (mkIdent n)
withBindingBody n $ d stxN
@[inline] def liftMetaM {α} (x : MetaM α) : DelabM α :=
liftM x
partial def delabFor : Name → Delab
| Name.anonymous => failure
| k => do
(delabAttribute.getValues (← getEnv) k).firstM id >>= annotateCurPos <|>
-- have `app.Option.some` fall back to `app` etc.
delabFor k.getRoot
partial def delab : Delab := do
unless (← getPPOption getPPProofs) do
let e ← getExpr
-- no need to hide atomic proofs
unless e.isAtomic do
try
let ty ← Meta.inferType (← getExpr)
if ← Meta.isProp ty then
if ← getPPOption getPPProofsWithType then
return ← ``((_ : $(← descend ty 0 delab)))
else
return ← ``(_)
catch _ => pure ()
let k ← getExprKind
delabFor k <|> (liftM $ show MetaM Syntax from throwError "don't know how to delaborate '{k}'")
unsafe def mkAppUnexpanderAttribute : IO (KeyedDeclsAttribute Unexpander) :=
KeyedDeclsAttribute.init {
name := `appUnexpander,
descr := "Register an unexpander for applications of a given constant.
[appUnexpander c] registers a `Lean.PrettyPrinter.Unexpander` for applications of the constant `c`. The unexpander is
passed the result of pre-pretty printing the application *without* implicitly passed arguments. If `pp.explicit` is set
to true or `pp.notation` is set to false, it will not be called at all.",
valueTypeName := `Lean.PrettyPrinter.Unexpander
} `Lean.PrettyPrinter.Delaborator.appUnexpanderAttribute
@[builtinInit mkAppUnexpanderAttribute] constant appUnexpanderAttribute : KeyedDeclsAttribute Unexpander
end Delaborator
/-- "Delaborate" the given term into surface-level syntax using the default and given subterm-specific options. -/
def delab (currNamespace : Name) (openDecls : List OpenDecl) (e : Expr) (optionsPerPos : OptionsPerPos := {}) : MetaM Syntax := do
trace[PrettyPrinter.delab.input] "{fmt e}"
let mut opts ← MonadOptions.getOptions
-- default `pp.proofs` to `true` if `e` is a proof
if pp.proofs.get? opts == none then
try
let ty ← Meta.inferType e
if ← Meta.isProp ty then
opts := pp.proofs.set opts true
catch _ => pure ()
catchInternalId Delaborator.delabFailureId
(Delaborator.delab.run { expr := e, defaultOptions := opts, optionsPerPos := optionsPerPos, currNamespace := currNamespace, openDecls := openDecls })
(fun _ => unreachable!)
builtin_initialize registerTraceClass `PrettyPrinter.delab
end PrettyPrinter
end Lean
|
eb20c30e575663f569bdc2602125536e02d5ccba | c777c32c8e484e195053731103c5e52af26a25d1 | /src/data/nat/parity.lean | 6a0cf0a24bcbc1bbcb1819c3cbe7f57e2a413846 | [
"Apache-2.0"
] | permissive | kbuzzard/mathlib | 2ff9e85dfe2a46f4b291927f983afec17e946eb8 | 58537299e922f9c77df76cb613910914a479c1f7 | refs/heads/master | 1,685,313,702,744 | 1,683,974,212,000 | 1,683,974,212,000 | 128,185,277 | 1 | 0 | null | 1,522,920,600,000 | 1,522,920,600,000 | null | UTF-8 | Lean | false | false | 11,191 | lean | /-
Copyright (c) 2019 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad, Benjamin Davidson
-/
import data.nat.modeq
import algebra.parity
/-!
# Parity of natural numbers
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
This file contains theorems about the `even` and `odd` predicates on the natural numbers.
## Tags
even, odd
-/
namespace nat
variables {m n : ℕ}
@[simp] theorem mod_two_ne_one : ¬ n % 2 = 1 ↔ n % 2 = 0 :=
by cases mod_two_eq_zero_or_one n with h h; simp [h]
@[simp] theorem mod_two_ne_zero : ¬ n % 2 = 0 ↔ n % 2 = 1 :=
by cases mod_two_eq_zero_or_one n with h h; simp [h]
theorem even_iff : even n ↔ n % 2 = 0 :=
⟨λ ⟨m, hm⟩, by simp [← two_mul, hm],
λ h, ⟨n / 2, (mod_add_div n 2).symm.trans (by simp [← two_mul, h])⟩⟩
theorem odd_iff : odd n ↔ n % 2 = 1 :=
⟨λ ⟨m, hm⟩, by norm_num [hm, add_mod],
λ h, ⟨n / 2, (mod_add_div n 2).symm.trans (by rw [h, add_comm])⟩⟩
lemma not_even_iff : ¬ even n ↔ n % 2 = 1 :=
by rw [even_iff, mod_two_ne_zero]
lemma not_odd_iff : ¬ odd n ↔ n % 2 = 0 :=
by rw [odd_iff, mod_two_ne_one]
lemma even_iff_not_odd : even n ↔ ¬ odd n :=
by rw [not_odd_iff, even_iff]
@[simp] lemma odd_iff_not_even : odd n ↔ ¬ even n :=
by rw [not_even_iff, odd_iff]
lemma is_compl_even_odd : is_compl {n : ℕ | even n} {n | odd n} :=
by simp only [←set.compl_set_of, is_compl_compl, odd_iff_not_even]
lemma even_or_odd (n : ℕ) : even n ∨ odd n :=
or.imp_right odd_iff_not_even.2 $ em $ even n
lemma even_or_odd' (n : ℕ) : ∃ k, n = 2 * k ∨ n = 2 * k + 1 :=
by simpa only [← two_mul, exists_or_distrib, ← odd, ← even] using even_or_odd n
lemma even_xor_odd (n : ℕ) : xor (even n) (odd n) :=
begin
cases even_or_odd n with h,
{ exact or.inl ⟨h, even_iff_not_odd.mp h⟩ },
{ exact or.inr ⟨h, odd_iff_not_even.mp h⟩ },
end
lemma even_xor_odd' (n : ℕ) : ∃ k, xor (n = 2 * k) (n = 2 * k + 1) :=
begin
rcases even_or_odd n with ⟨k, rfl⟩ | ⟨k, rfl⟩;
use k,
{ simpa only [← two_mul, xor, true_and, eq_self_iff_true, not_true, or_false, and_false]
using (succ_ne_self (2*k)).symm },
{ simp only [xor, add_right_eq_self, false_or, eq_self_iff_true, not_true, not_false_iff,
one_ne_zero, and_self] },
end
@[simp] theorem two_dvd_ne_zero : ¬ 2 ∣ n ↔ n % 2 = 1 :=
even_iff_two_dvd.symm.not.trans not_even_iff
instance : decidable_pred (even : ℕ → Prop) := λ n, decidable_of_iff _ even_iff.symm
instance : decidable_pred (odd : ℕ → Prop) := λ n, decidable_of_iff _ odd_iff_not_even.symm
theorem mod_two_add_add_odd_mod_two (m : ℕ) {n : ℕ} (hn : odd n) : m % 2 + (m + n) % 2 = 1 :=
(even_or_odd m).elim (λ hm, by rw [even_iff.1 hm, odd_iff.1 (hm.add_odd hn)]) $
λ hm, by rw [odd_iff.1 hm, even_iff.1 (hm.add_odd hn)]
@[simp] theorem mod_two_add_succ_mod_two (m : ℕ) : m % 2 + (m + 1) % 2 = 1 :=
mod_two_add_add_odd_mod_two m odd_one
@[simp] theorem succ_mod_two_add_mod_two (m : ℕ) : (m + 1) % 2 + m % 2 = 1 :=
by rw [add_comm, mod_two_add_succ_mod_two]
mk_simp_attribute parity_simps "Simp attribute for lemmas about `even`"
@[simp] theorem not_even_one : ¬ even 1 :=
by rw even_iff; norm_num
@[parity_simps] theorem even_add : even (m + n) ↔ (even m ↔ even n) :=
by cases mod_two_eq_zero_or_one m with h₁ h₁;
cases mod_two_eq_zero_or_one n with h₂ h₂;
simp [even_iff, h₁, h₂, nat.add_mod];
norm_num
theorem even_add' : even (m + n) ↔ (odd m ↔ odd n) :=
by rw [even_add, even_iff_not_odd, even_iff_not_odd, not_iff_not]
@[parity_simps] theorem even_add_one : even (n + 1) ↔ ¬ even n :=
by simp [even_add]
@[simp] theorem not_even_bit1 (n : ℕ) : ¬ even (bit1 n) :=
by simp [bit1] with parity_simps
lemma two_not_dvd_two_mul_add_one (n : ℕ) : ¬(2 ∣ 2 * n + 1) :=
by simp [add_mod]
lemma two_not_dvd_two_mul_sub_one : Π {n} (w : 0 < n), ¬(2 ∣ 2 * n - 1)
| (n + 1) _ := two_not_dvd_two_mul_add_one n
@[parity_simps] theorem even_sub (h : n ≤ m) : even (m - n) ↔ (even m ↔ even n) :=
begin
conv { to_rhs, rw [←tsub_add_cancel_of_le h, even_add] },
by_cases h : even n; simp [h]
end
theorem even_sub' (h : n ≤ m) : even (m - n) ↔ (odd m ↔ odd n) :=
by rw [even_sub h, even_iff_not_odd, even_iff_not_odd, not_iff_not]
theorem odd.sub_odd (hm : odd m) (hn : odd n) : even (m - n) :=
(le_total n m).elim
(λ h, by simp only [even_sub' h, *])
(λ h, by simp only [tsub_eq_zero_iff_le.mpr h, even_zero])
@[parity_simps] theorem even_mul : even (m * n) ↔ even m ∨ even n :=
by cases mod_two_eq_zero_or_one m with h₁ h₁;
cases mod_two_eq_zero_or_one n with h₂ h₂;
simp [even_iff, h₁, h₂, nat.mul_mod];
norm_num
theorem odd_mul : odd (m * n) ↔ odd m ∧ odd n :=
by simp [not_or_distrib] with parity_simps
theorem odd.of_mul_left (h : odd (m * n)) : odd m :=
(odd_mul.mp h).1
theorem odd.of_mul_right (h : odd (m * n)) : odd n :=
(odd_mul.mp h).2
/-- If `m` and `n` are natural numbers, then the natural number `m^n` is even
if and only if `m` is even and `n` is positive. -/
@[parity_simps] theorem even_pow : even (m ^ n) ↔ even m ∧ n ≠ 0 :=
by { induction n with n ih; simp [*, pow_succ', even_mul], tauto }
theorem even_pow' (h : n ≠ 0) : even (m ^ n) ↔ even m :=
even_pow.trans $ and_iff_left h
theorem even_div : even (m / n) ↔ m % (2 * n) / n = 0 :=
by rw [even_iff_two_dvd, dvd_iff_mod_eq_zero, nat.div_mod_eq_mod_mul_div, mul_comm]
@[parity_simps] theorem odd_add : odd (m + n) ↔ (odd m ↔ even n) :=
by rw [odd_iff_not_even, even_add, not_iff, odd_iff_not_even]
theorem odd_add' : odd (m + n) ↔ (odd n ↔ even m) :=
by rw [add_comm, odd_add]
lemma ne_of_odd_add (h : odd (m + n)) : m ≠ n :=
λ hnot, by simpa [hnot] with parity_simps using h
@[parity_simps] theorem odd_sub (h : n ≤ m) : odd (m - n) ↔ (odd m ↔ even n) :=
by rw [odd_iff_not_even, even_sub h, not_iff, odd_iff_not_even]
theorem odd.sub_even (h : n ≤ m) (hm : odd m) (hn : even n) : odd (m - n) :=
(odd_sub h).mpr $ iff_of_true hm hn
theorem odd_sub' (h : n ≤ m) : odd (m - n) ↔ (odd n ↔ even m) :=
by rw [odd_iff_not_even, even_sub h, not_iff, not_iff_comm, odd_iff_not_even]
theorem even.sub_odd (h : n ≤ m) (hm : even m) (hn : odd n) : odd (m - n) :=
(odd_sub' h).mpr $ iff_of_true hn hm
lemma even_mul_succ_self (n : ℕ) : even (n * (n + 1)) :=
begin
rw even_mul,
convert n.even_or_odd,
simp with parity_simps
end
lemma even_mul_self_pred (n : ℕ) : even (n * (n - 1)) :=
begin
cases n,
{ exact even_zero },
{ rw mul_comm,
apply even_mul_succ_self }
end
lemma two_mul_div_two_of_even : even n → 2 * (n / 2) = n :=
λ h, nat.mul_div_cancel_left' (even_iff_two_dvd.mp h)
lemma div_two_mul_two_of_even : even n → n / 2 * 2 = n :=
λ h, nat.div_mul_cancel (even_iff_two_dvd.mp h)
lemma two_mul_div_two_add_one_of_odd (h : odd n) : 2 * (n / 2) + 1 = n :=
by { rw mul_comm, convert nat.div_add_mod' n 2, rw odd_iff.mp h }
lemma div_two_mul_two_add_one_of_odd (h : odd n) : n / 2 * 2 + 1 = n :=
by { convert nat.div_add_mod' n 2, rw odd_iff.mp h }
lemma one_add_div_two_mul_two_of_odd (h : odd n) : 1 + n / 2 * 2 = n :=
by { rw add_comm, convert nat.div_add_mod' n 2, rw odd_iff.mp h }
lemma bit0_div_two : bit0 n / 2 = n :=
by rw [←nat.bit0_eq_bit0, bit0_eq_two_mul, two_mul_div_two_of_even (even_bit0 n)]
lemma bit1_div_two : bit1 n / 2 = n :=
by rw [←nat.bit1_eq_bit1, bit1, bit0_eq_two_mul, nat.two_mul_div_two_add_one_of_odd (odd_bit1 n)]
@[simp] lemma bit0_div_bit0 : bit0 n / bit0 m = n / m :=
by rw [bit0_eq_two_mul m, ←nat.div_div_eq_div_mul, bit0_div_two]
@[simp] lemma bit1_div_bit0 : bit1 n / bit0 m = n / m :=
by rw [bit0_eq_two_mul, ←nat.div_div_eq_div_mul, bit1_div_two]
@[simp] lemma bit0_mod_bit0 : bit0 n % bit0 m = bit0 (n % m) :=
by rw [bit0_eq_two_mul n, bit0_eq_two_mul m, bit0_eq_two_mul (n % m), nat.mul_mod_mul_left]
@[simp] lemma bit1_mod_bit0 : bit1 n % bit0 m = bit1 (n % m) :=
begin
have h₁ := congr_arg bit1 (nat.div_add_mod n m),
-- `∀ m n : ℕ, bit0 m * n = bit0 (m * n)` seems to be missing...
rw [bit1_add, bit0_eq_two_mul, ← mul_assoc, ← bit0_eq_two_mul] at h₁,
have h₂ := nat.div_add_mod (bit1 n) (bit0 m),
rw [bit1_div_bit0] at h₂,
exact add_left_cancel (h₂.trans h₁.symm),
end
-- Here are examples of how `parity_simps` can be used with `nat`.
example (m n : ℕ) (h : even m) : ¬ even (n + 3) ↔ even (m^2 + m + n) :=
by simp [*, (dec_trivial : ¬ 2 = 0)] with parity_simps
example : ¬ even 25394535 :=
by simp
end nat
open nat
namespace function
namespace involutive
variables {α : Type*} {f : α → α} {n : ℕ}
theorem iterate_bit0 (hf : involutive f) (n : ℕ) : f^[bit0 n] = id :=
by rw [bit0, ← two_mul, iterate_mul, involutive_iff_iter_2_eq_id.1 hf, iterate_id]
theorem iterate_bit1 (hf : involutive f) (n : ℕ) : f^[bit1 n] = f :=
by rw [bit1, iterate_succ, hf.iterate_bit0, comp.left_id]
theorem iterate_even (hf : involutive f) (hn : even n) : f^[n] = id :=
let ⟨m, hm⟩ := hn in hm.symm ▸ hf.iterate_bit0 m
theorem iterate_odd (hf : involutive f) (hn : odd n) : f^[n] = f :=
let ⟨m, hm⟩ := odd_iff_exists_bit1.mp hn in hm.symm ▸ hf.iterate_bit1 m
theorem iterate_eq_self (hf : involutive f) (hne : f ≠ id) : f^[n] = f ↔ odd n :=
⟨λ H, odd_iff_not_even.2 $ λ hn, hne $ by rwa [hf.iterate_even hn, eq_comm] at H, hf.iterate_odd⟩
theorem iterate_eq_id (hf : involutive f) (hne : f ≠ id) : f^[n] = id ↔ even n :=
⟨λ H, even_iff_not_odd.2 $ λ hn, hne $ by rwa [hf.iterate_odd hn] at H, hf.iterate_even⟩
end involutive
end function
variables {R : Type*} [monoid R] [has_distrib_neg R] {n : ℕ}
lemma neg_one_pow_eq_one_iff_even (h : (-1 : R) ≠ 1) : (-1 : R) ^ n = 1 ↔ even n :=
⟨λ h', of_not_not $ λ hn, h $ (odd.neg_one_pow $ odd_iff_not_even.mpr hn).symm.trans h',
even.neg_one_pow⟩
/-- If `a` is even, then `n` is odd iff `n % a` is odd. -/
lemma odd.mod_even_iff {n a : ℕ} (ha : even a) : odd (n % a) ↔ odd n :=
((even_sub' $ mod_le n a).mp $ even_iff_two_dvd.mpr $ (even_iff_two_dvd.mp ha).trans $
dvd_sub_mod n).symm
/-- If `a` is even, then `n` is even iff `n % a` is even. -/
lemma even.mod_even_iff {n a : ℕ} (ha : even a) : even (n % a) ↔ even n :=
((even_sub $ mod_le n a).mp $ even_iff_two_dvd.mpr $ (even_iff_two_dvd.mp ha).trans $
dvd_sub_mod n).symm
/-- If `n` is odd and `a` is even, then `n % a` is odd. -/
lemma odd.mod_even {n a : ℕ} (hn : odd n) (ha : even a) : odd (n % a) :=
(odd.mod_even_iff ha).mpr hn
/-- If `n` is even and `a` is even, then `n % a` is even. -/
lemma even.mod_even {n a : ℕ} (hn : even n) (ha : even a) : even (n % a) :=
(even.mod_even_iff ha).mpr hn
theorem odd.of_dvd_nat {m n : ℕ} (hn : odd n) (hm : m ∣ n) : odd m :=
odd_iff_not_even.2 $ mt hm.even (odd_iff_not_even.1 hn)
/-- `2` is not a factor of an odd natural number. -/
theorem odd.ne_two_of_dvd_nat {m n : ℕ} (hn : odd n) (hm : m ∣ n) : m ≠ 2 :=
begin
rintro rfl,
exact absurd (hn.of_dvd_nat hm) dec_trivial
end
|
3ade8cf77bd1906afbcffd169c73c95c6cdea438 | 2a70b774d16dbdf5a533432ee0ebab6838df0948 | /_target/deps/mathlib/src/data/finset/lattice.lean | 66cf07c5a5fb1ef90d1e44df08962197e04ce549 | [
"Apache-2.0"
] | permissive | hjvromen/lewis | 40b035973df7c77ebf927afab7878c76d05ff758 | 105b675f73630f028ad5d890897a51b3c1146fb0 | refs/heads/master | 1,677,944,636,343 | 1,676,555,301,000 | 1,676,555,301,000 | 327,553,599 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 25,797 | lean | /-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Mario Carneiro
-/
import data.finset.fold
import data.multiset.lattice
import order.order_dual
import order.complete_lattice
/-!
# Lattice operations on finsets
-/
variables {α β γ : Type*}
namespace finset
open multiset order_dual
/-! ### sup -/
section sup
variables [semilattice_sup_bot α]
/-- Supremum of a finite set: `sup {a, b, c} f = f a ⊔ f b ⊔ f c` -/
def sup (s : finset β) (f : β → α) : α := s.fold (⊔) ⊥ f
variables {s s₁ s₂ : finset β} {f : β → α}
lemma sup_def : s.sup f = (s.1.map f).sup := rfl
@[simp] lemma sup_empty : (∅ : finset β).sup f = ⊥ :=
fold_empty
@[simp] lemma sup_insert [decidable_eq β] {b : β} : (insert b s : finset β).sup f = f b ⊔ s.sup f :=
fold_insert_idem
@[simp] lemma sup_singleton {b : β} : ({b} : finset β).sup f = f b :=
sup_singleton
lemma sup_union [decidable_eq β] : (s₁ ∪ s₂).sup f = s₁.sup f ⊔ s₂.sup f :=
finset.induction_on s₁ (by rw [empty_union, sup_empty, bot_sup_eq]) $ λ a s has ih,
by rw [insert_union, sup_insert, sup_insert, ih, sup_assoc]
theorem sup_congr {f g : β → α} (hs : s₁ = s₂) (hfg : ∀a∈s₂, f a = g a) : s₁.sup f = s₂.sup g :=
by subst hs; exact finset.fold_congr hfg
@[simp] lemma sup_le_iff {a : α} : s.sup f ≤ a ↔ (∀b ∈ s, f b ≤ a) :=
begin
apply iff.trans multiset.sup_le,
simp only [multiset.mem_map, and_imp, exists_imp_distrib],
exact ⟨λ k b hb, k _ _ hb rfl, λ k a' b hb h, h ▸ k _ hb⟩,
end
lemma sup_le {a : α} : (∀b ∈ s, f b ≤ a) → s.sup f ≤ a :=
sup_le_iff.2
lemma le_sup {b : β} (hb : b ∈ s) : f b ≤ s.sup f :=
sup_le_iff.1 (le_refl _) _ hb
lemma sup_mono_fun {g : β → α} (h : ∀b∈s, f b ≤ g b) : s.sup f ≤ s.sup g :=
sup_le (λ b hb, le_trans (h b hb) (le_sup hb))
lemma sup_mono (h : s₁ ⊆ s₂) : s₁.sup f ≤ s₂.sup f :=
sup_le $ assume b hb, le_sup (h hb)
@[simp] lemma sup_lt_iff [is_total α (≤)] {a : α} (ha : ⊥ < a) :
s.sup f < a ↔ (∀b ∈ s, f b < a) :=
by letI := classical.dec_eq β; from
⟨ λh b hb, lt_of_le_of_lt (le_sup hb) h,
finset.induction_on s (by simp [ha]) (by simp {contextual := tt}) ⟩
lemma comp_sup_eq_sup_comp [semilattice_sup_bot γ] {s : finset β}
{f : β → α} (g : α → γ) (g_sup : ∀ x y, g (x ⊔ y) = g x ⊔ g y) (bot : g ⊥ = ⊥) :
g (s.sup f) = s.sup (g ∘ f) :=
by letI := classical.dec_eq β; from
finset.induction_on s (by simp [bot]) (by simp [g_sup] {contextual := tt})
lemma comp_sup_eq_sup_comp_of_is_total [is_total α (≤)] {γ : Type} [semilattice_sup_bot γ]
(g : α → γ) (mono_g : monotone g) (bot : g ⊥ = ⊥) : g (s.sup f) = s.sup (g ∘ f) :=
comp_sup_eq_sup_comp g mono_g.map_sup bot
/-- Computating `sup` in a subtype (closed under `sup`) is the same as computing it in `α`. -/
lemma sup_coe {P : α → Prop}
{Pbot : P ⊥} {Psup : ∀{{x y}}, P x → P y → P (x ⊔ y)}
(t : finset β) (f : β → {x : α // P x}) :
(@sup _ _ (subtype.semilattice_sup_bot Pbot Psup) t f : α) = t.sup (λ x, f x) :=
by { classical, rw [comp_sup_eq_sup_comp coe]; intros; refl }
theorem subset_range_sup_succ (s : finset ℕ) : s ⊆ range (s.sup id).succ :=
λ n hn, mem_range.2 $ nat.lt_succ_of_le $ le_sup hn
theorem exists_nat_subset_range (s : finset ℕ) : ∃n : ℕ, s ⊆ range n :=
⟨_, s.subset_range_sup_succ⟩
lemma mem_sup {α β} [decidable_eq β] {s : finset α} {f : α → multiset β}
{x : β} : x ∈ s.sup f ↔ ∃ v ∈ s, x ∈ f v :=
begin
classical,
apply s.induction_on,
{ simp },
{ intros a s has hxs,
rw [finset.sup_insert, multiset.sup_eq_union, multiset.mem_union],
split,
{ intro hxi,
cases hxi with hf hf,
{ refine ⟨a, _, hf⟩,
simp only [true_or, eq_self_iff_true, finset.mem_insert] },
{ rcases hxs.mp hf with ⟨v, hv, hfv⟩,
refine ⟨v, _, hfv⟩,
simp only [hv, or_true, finset.mem_insert] } },
{ rintros ⟨v, hv, hfv⟩,
rw [finset.mem_insert] at hv,
rcases hv with rfl | hv,
{ exact or.inl hfv },
{ refine or.inr (hxs.mpr ⟨v, hv, hfv⟩) } } },
end
lemma sup_subset {α β} [semilattice_sup_bot β] {s t : finset α} (hst : s ⊆ t) (f : α → β) :
s.sup f ≤ t.sup f :=
by classical;
calc t.sup f = (s ∪ t).sup f : by rw [finset.union_eq_right_iff_subset.mpr hst]
... = s.sup f ⊔ t.sup f : by rw finset.sup_union
... ≥ s.sup f : le_sup_left
lemma sup_closed_of_sup_closed {s : set α} (t : finset α) (htne : t.nonempty) (h_subset : ↑t ⊆ s)
(h : ∀⦃a b⦄, a ∈ s → b ∈ s → a ⊔ b ∈ s) : t.sup id ∈ s :=
begin
classical,
induction t using finset.induction_on with x t h₀ h₁,
{ exfalso, apply finset.not_nonempty_empty htne, },
{ rw [finset.coe_insert, set.insert_subset] at h_subset,
cases t.eq_empty_or_nonempty with hte htne,
{ subst hte, simp only [insert_emptyc_eq, id.def, finset.sup_singleton, h_subset], },
{ rw [finset.sup_insert, id.def], exact h h_subset.1 (h₁ htne h_subset.2), }, },
end
end sup
lemma sup_eq_supr [complete_lattice β] (s : finset α) (f : α → β) : s.sup f = (⨆a∈s, f a) :=
le_antisymm
(finset.sup_le $ assume a ha, le_supr_of_le a $ le_supr _ ha)
(supr_le $ assume a, supr_le $ assume ha, le_sup ha)
lemma sup_eq_Sup [complete_lattice α] (s : finset α) : s.sup id = Sup s :=
by simp [Sup_eq_supr, sup_eq_supr]
/-! ### inf -/
section inf
variables [semilattice_inf_top α]
/-- Infimum of a finite set: `inf {a, b, c} f = f a ⊓ f b ⊓ f c` -/
def inf (s : finset β) (f : β → α) : α := s.fold (⊓) ⊤ f
variables {s s₁ s₂ : finset β} {f : β → α}
lemma inf_def : s.inf f = (s.1.map f).inf := rfl
@[simp] lemma inf_empty : (∅ : finset β).inf f = ⊤ :=
fold_empty
lemma le_inf_iff {a : α} : a ≤ s.inf f ↔ ∀b ∈ s, a ≤ f b :=
@sup_le_iff (order_dual α) _ _ _ _ _
@[simp] lemma inf_insert [decidable_eq β] {b : β} : (insert b s : finset β).inf f = f b ⊓ s.inf f :=
fold_insert_idem
@[simp] lemma inf_singleton {b : β} : ({b} : finset β).inf f = f b :=
inf_singleton
lemma inf_union [decidable_eq β] : (s₁ ∪ s₂).inf f = s₁.inf f ⊓ s₂.inf f :=
@sup_union (order_dual α) _ _ _ _ _ _
theorem inf_congr {f g : β → α} (hs : s₁ = s₂) (hfg : ∀a∈s₂, f a = g a) : s₁.inf f = s₂.inf g :=
by subst hs; exact finset.fold_congr hfg
lemma inf_le {b : β} (hb : b ∈ s) : s.inf f ≤ f b :=
le_inf_iff.1 (le_refl _) _ hb
lemma le_inf {a : α} : (∀b ∈ s, a ≤ f b) → a ≤ s.inf f :=
le_inf_iff.2
lemma inf_mono_fun {g : β → α} (h : ∀b∈s, f b ≤ g b) : s.inf f ≤ s.inf g :=
le_inf (λ b hb, le_trans (inf_le hb) (h b hb))
lemma inf_mono (h : s₁ ⊆ s₂) : s₂.inf f ≤ s₁.inf f :=
le_inf $ assume b hb, inf_le (h hb)
lemma lt_inf_iff [h : is_total α (≤)] {a : α} (ha : a < ⊤) : a < s.inf f ↔ (∀b ∈ s, a < f b) :=
@sup_lt_iff (order_dual α) _ _ _ _ (@is_total.swap α _ h) _ ha
lemma comp_inf_eq_inf_comp [semilattice_inf_top γ] {s : finset β}
{f : β → α} (g : α → γ) (g_inf : ∀ x y, g (x ⊓ y) = g x ⊓ g y) (top : g ⊤ = ⊤) :
g (s.inf f) = s.inf (g ∘ f) :=
@comp_sup_eq_sup_comp (order_dual α) _ (order_dual γ) _ _ _ _ _ g_inf top
lemma comp_inf_eq_inf_comp_of_is_total [h : is_total α (≤)] {γ : Type} [semilattice_inf_top γ]
(g : α → γ) (mono_g : monotone g) (top : g ⊤ = ⊤) : g (s.inf f) = s.inf (g ∘ f) :=
comp_inf_eq_inf_comp g mono_g.map_inf top
/-- Computating `inf` in a subtype (closed under `inf`) is the same as computing it in `α`. -/
lemma inf_coe {P : α → Prop}
{Ptop : P ⊤} {Pinf : ∀{{x y}}, P x → P y → P (x ⊓ y)}
(t : finset β) (f : β → {x : α // P x}) :
(@inf _ _ (subtype.semilattice_inf_top Ptop Pinf) t f : α) = t.inf (λ x, f x) :=
by { classical, rw [comp_inf_eq_inf_comp coe]; intros; refl }
end inf
lemma inf_eq_infi [complete_lattice β] (s : finset α) (f : α → β) : s.inf f = (⨅a∈s, f a) :=
@sup_eq_supr _ (order_dual β) _ _ _
lemma inf_eq_Inf [complete_lattice α] (s : finset α) : s.inf id = Inf s :=
by simp [Inf_eq_infi, inf_eq_infi]
/-! ### max and min of finite sets -/
section max_min
variables [linear_order α]
/-- Let `s` be a finset in a linear order. Then `s.max` is the maximum of `s` if `s` is not empty,
and `none` otherwise. It belongs to `option α`. If you want to get an element of `α`, see
`s.max'`. -/
protected def max : finset α → option α :=
fold (option.lift_or_get max) none some
theorem max_eq_sup_with_bot (s : finset α) :
s.max = @sup (with_bot α) α _ s some := rfl
@[simp] theorem max_empty : (∅ : finset α).max = none := rfl
@[simp] theorem max_insert {a : α} {s : finset α} :
(insert a s).max = option.lift_or_get max (some a) s.max := fold_insert_idem
@[simp] theorem max_singleton {a : α} : finset.max {a} = some a :=
by { rw [← insert_emptyc_eq], exact max_insert }
theorem max_of_mem {s : finset α} {a : α} (h : a ∈ s) : ∃ b, b ∈ s.max :=
(@le_sup (with_bot α) _ _ _ _ _ h _ rfl).imp $ λ b, Exists.fst
theorem max_of_nonempty {s : finset α} (h : s.nonempty) : ∃ a, a ∈ s.max :=
let ⟨a, ha⟩ := h in max_of_mem ha
theorem max_eq_none {s : finset α} : s.max = none ↔ s = ∅ :=
⟨λ h, s.eq_empty_or_nonempty.elim id
(λ H, let ⟨a, ha⟩ := max_of_nonempty H in by rw h at ha; cases ha),
λ h, h.symm ▸ max_empty⟩
theorem mem_of_max {s : finset α} : ∀ {a : α}, a ∈ s.max → a ∈ s :=
finset.induction_on s (λ _ H, by cases H)
(λ b s _ (ih : ∀ {a}, a ∈ s.max → a ∈ s) a (h : a ∈ (insert b s).max),
begin
by_cases p : b = a,
{ induction p, exact mem_insert_self b s },
{ cases option.lift_or_get_choice max_choice (some b) s.max with q q;
rw [max_insert, q] at h,
{ cases h, cases p rfl },
{ exact mem_insert_of_mem (ih h) } }
end)
theorem le_max_of_mem {s : finset α} {a b : α} (h₁ : a ∈ s) (h₂ : b ∈ s.max) : a ≤ b :=
by rcases @le_sup (with_bot α) _ _ _ _ _ h₁ _ rfl with ⟨b', hb, ab⟩;
cases h₂.symm.trans hb; assumption
/-- Let `s` be a finset in a linear order. Then `s.min` is the minimum of `s` if `s` is not empty,
and `none` otherwise. It belongs to `option α`. If you want to get an element of `α`, see
`s.min'`. -/
protected def min : finset α → option α :=
fold (option.lift_or_get min) none some
theorem min_eq_inf_with_top (s : finset α) :
s.min = @inf (with_top α) α _ s some := rfl
@[simp] theorem min_empty : (∅ : finset α).min = none := rfl
@[simp] theorem min_insert {a : α} {s : finset α} :
(insert a s).min = option.lift_or_get min (some a) s.min :=
fold_insert_idem
@[simp] theorem min_singleton {a : α} : finset.min {a} = some a :=
by { rw ← insert_emptyc_eq, exact min_insert }
theorem min_of_mem {s : finset α} {a : α} (h : a ∈ s) : ∃ b, b ∈ s.min :=
(@inf_le (with_top α) _ _ _ _ _ h _ rfl).imp $ λ b, Exists.fst
theorem min_of_nonempty {s : finset α} (h : s.nonempty) : ∃ a, a ∈ s.min :=
let ⟨a, ha⟩ := h in min_of_mem ha
theorem min_eq_none {s : finset α} : s.min = none ↔ s = ∅ :=
⟨λ h, s.eq_empty_or_nonempty.elim id
(λ H, let ⟨a, ha⟩ := min_of_nonempty H in by rw h at ha; cases ha),
λ h, h.symm ▸ min_empty⟩
theorem mem_of_min {s : finset α} : ∀ {a : α}, a ∈ s.min → a ∈ s :=
finset.induction_on s (λ _ H, by cases H) $
λ b s _ (ih : ∀ {a}, a ∈ s.min → a ∈ s) a (h : a ∈ (insert b s).min),
begin
by_cases p : b = a,
{ induction p, exact mem_insert_self b s },
{ cases option.lift_or_get_choice min_choice (some b) s.min with q q;
rw [min_insert, q] at h,
{ cases h, cases p rfl },
{ exact mem_insert_of_mem (ih h) } }
end
theorem min_le_of_mem {s : finset α} {a b : α} (h₁ : b ∈ s) (h₂ : a ∈ s.min) : a ≤ b :=
by rcases @inf_le (with_top α) _ _ _ _ _ h₁ _ rfl with ⟨b', hb, ab⟩;
cases h₂.symm.trans hb; assumption
/-- Given a nonempty finset `s` in a linear order `α `, then `s.min' h` is its minimum, as an
element of `α`, where `h` is a proof of nonemptiness. Without this assumption, use instead `s.min`,
taking values in `option α`. -/
def min' (s : finset α) (H : s.nonempty) : α :=
@option.get _ s.min $
let ⟨k, hk⟩ := H in
let ⟨b, hb⟩ := min_of_mem hk in by simp at hb; simp [hb]
/-- Given a nonempty finset `s` in a linear order `α `, then `s.max' h` is its maximum, as an
element of `α`, where `h` is a proof of nonemptiness. Without this assumption, use instead `s.max`,
taking values in `option α`. -/
def max' (s : finset α) (H : s.nonempty) : α :=
@option.get _ s.max $
let ⟨k, hk⟩ := H in
let ⟨b, hb⟩ := max_of_mem hk in by simp at hb; simp [hb]
variables (s : finset α) (H : s.nonempty)
theorem min'_mem : s.min' H ∈ s := mem_of_min $ by simp [min']
theorem min'_le (x) (H2 : x ∈ s) : s.min' ⟨x, H2⟩ ≤ x := min_le_of_mem H2 $ option.get_mem _
theorem le_min' (x) (H2 : ∀ y ∈ s, x ≤ y) : x ≤ s.min' H := H2 _ $ min'_mem _ _
/-- `{a}.min' _` is `a`. -/
@[simp] lemma min'_singleton (a : α) :
({a} : finset α).min' (singleton_nonempty _) = a :=
by simp [min']
theorem max'_mem : s.max' H ∈ s := mem_of_max $ by simp [max']
theorem le_max' (x) (H2 : x ∈ s) : x ≤ s.max' ⟨x, H2⟩ := le_max_of_mem H2 $ option.get_mem _
theorem max'_le (x) (H2 : ∀ y ∈ s, y ≤ x) : s.max' H ≤ x := H2 _ $ max'_mem _ _
/-- `{a}.max' _` is `a`. -/
@[simp] lemma max'_singleton (a : α) :
({a} : finset α).max' (singleton_nonempty _) = a :=
by simp [max']
theorem min'_lt_max' {i j} (H1 : i ∈ s) (H2 : j ∈ s) (H3 : i ≠ j) :
s.min' ⟨i, H1⟩ < s.max' ⟨i, H1⟩ :=
begin
rcases lt_trichotomy i j with H4 | H4 | H4,
{ have H5 := min'_le s i H1,
have H6 := le_max' s j H2,
apply lt_of_le_of_lt H5,
apply lt_of_lt_of_le H4 H6 },
{ cc },
{ have H5 := min'_le s j H2,
have H6 := le_max' s i H1,
apply lt_of_le_of_lt H5,
apply lt_of_lt_of_le H4 H6 }
end
/--
If there's more than 1 element, the min' is less than the max'. An alternate version of
`min'_lt_max'` which is sometimes more convenient.
-/
lemma min'_lt_max'_of_card (h₂ : 1 < card s) :
s.min' (finset.card_pos.mp $ lt_trans zero_lt_one h₂) <
s.max' (finset.card_pos.mp $ lt_trans zero_lt_one h₂) :=
begin
apply lt_of_not_ge,
intro a,
apply not_le_of_lt h₂ (le_of_eq _),
rw card_eq_one,
use (max' s (finset.card_pos.mp $ lt_trans zero_lt_one h₂)),
rw eq_singleton_iff_unique_mem,
refine ⟨max'_mem _ _, λ t Ht, le_antisymm (le_max' s t Ht) (le_trans a (min'_le s t Ht))⟩,
end
lemma max'_eq_of_dual_min' {s : finset α} (hs : s.nonempty) :
max' s hs = of_dual (min' (image to_dual s) (nonempty.image hs to_dual)) :=
begin
rw [of_dual, to_dual, equiv.coe_fn_mk, equiv.coe_fn_symm_mk, id.def],
simp_rw (@image_id (order_dual α) (s : finset (order_dual α))),
refl,
end
lemma min'_eq_of_dual_max' {s : finset α} (hs : s.nonempty) :
min' s hs = of_dual (max' (image to_dual s) (nonempty.image hs to_dual)) :=
begin
rw [of_dual, to_dual, equiv.coe_fn_mk, equiv.coe_fn_symm_mk, id.def],
simp_rw (@image_id (order_dual α) (s : finset (order_dual α))),
refl,
end
@[simp] lemma of_dual_max_eq_min_of_dual {a b : α} :
of_dual (max a b) = min (of_dual a) (of_dual b) := rfl
@[simp] lemma of_dual_min_eq_max_of_dual {a b : α} :
of_dual (min a b) = max (of_dual a) (of_dual b) := rfl
end max_min
section exists_max_min
variables [linear_order α]
lemma exists_max_image (s : finset β) (f : β → α) (h : s.nonempty) :
∃ x ∈ s, ∀ x' ∈ s, f x' ≤ f x :=
begin
cases max_of_nonempty (h.image f) with y hy,
rcases mem_image.mp (mem_of_max hy) with ⟨x, hx, rfl⟩,
exact ⟨x, hx, λ x' hx', le_max_of_mem (mem_image_of_mem f hx') hy⟩,
end
lemma exists_min_image (s : finset β) (f : β → α) (h : s.nonempty) :
∃ x ∈ s, ∀ x' ∈ s, f x ≤ f x' :=
@exists_max_image (order_dual α) β _ s f h
end exists_max_min
end finset
namespace multiset
lemma count_sup [decidable_eq β] (s : finset α) (f : α → multiset β) (b : β) :
count b (s.sup f) = s.sup (λa, count b (f a)) :=
begin
letI := classical.dec_eq α,
refine s.induction _ _,
{ exact count_zero _ },
{ assume i s his ih,
rw [finset.sup_insert, sup_eq_union, count_union, finset.sup_insert, ih],
refl }
end
end multiset
section lattice
variables {ι : Type*} {ι' : Sort*} [complete_lattice α]
/-- Supremum of `s i`, `i : ι`, is equal to the supremum over `t : finset ι` of suprema
`⨆ i ∈ t, s i`. This version assumes `ι` is a `Type*`. See `supr_eq_supr_finset'` for a version
that works for `ι : Sort*`. -/
lemma supr_eq_supr_finset (s : ι → α) :
(⨆i, s i) = (⨆t:finset ι, ⨆i∈t, s i) :=
begin
classical,
exact le_antisymm
(supr_le $ assume b, le_supr_of_le {b} $ le_supr_of_le b $ le_supr_of_le
(by simp) $ le_refl _)
(supr_le $ assume t, supr_le $ assume b, supr_le $ assume hb, le_supr _ _)
end
/-- Supremum of `s i`, `i : ι`, is equal to the supremum over `t : finset ι` of suprema
`⨆ i ∈ t, s i`. This version works for `ι : Sort*`. See `supr_eq_supr_finset` for a version
that assumes `ι : Type*` but has no `plift`s. -/
lemma supr_eq_supr_finset' (s : ι' → α) :
(⨆i, s i) = (⨆t:finset (plift ι'), ⨆i∈t, s (plift.down i)) :=
by rw [← supr_eq_supr_finset, ← equiv.plift.surjective.supr_comp]; refl
/-- Infimum of `s i`, `i : ι`, is equal to the infimum over `t : finset ι` of infima
`⨆ i ∈ t, s i`. This version assumes `ι` is a `Type*`. See `infi_eq_infi_finset'` for a version
that works for `ι : Sort*`. -/
lemma infi_eq_infi_finset (s : ι → α) :
(⨅i, s i) = (⨅t:finset ι, ⨅i∈t, s i) :=
@supr_eq_supr_finset (order_dual α) _ _ _
/-- Infimum of `s i`, `i : ι`, is equal to the infimum over `t : finset ι` of infima
`⨆ i ∈ t, s i`. This version works for `ι : Sort*`. See `infi_eq_infi_finset` for a version
that assumes `ι : Type*` but has no `plift`s. -/
lemma infi_eq_infi_finset' (s : ι' → α) :
(⨅i, s i) = (⨅t:finset (plift ι'), ⨅i∈t, s (plift.down i)) :=
@supr_eq_supr_finset' (order_dual α) _ _ _
end lattice
namespace set
variables {ι : Type*} {ι' : Sort*}
/-- Union of an indexed family of sets `s : ι → set α` is equal to the union of the unions
of finite subfamilies. This version assumes `ι : Type*`. See also `Union_eq_Union_finset'` for
a version that works for `ι : Sort*`. -/
lemma Union_eq_Union_finset (s : ι → set α) :
(⋃i, s i) = (⋃t:finset ι, ⋃i∈t, s i) :=
supr_eq_supr_finset s
/-- Union of an indexed family of sets `s : ι → set α` is equal to the union of the unions
of finite subfamilies. This version works for `ι : Sort*`. See also `Union_eq_Union_finset` for
a version that assumes `ι : Type*` but avoids `plift`s in the right hand side. -/
lemma Union_eq_Union_finset' (s : ι' → set α) :
(⋃i, s i) = (⋃t:finset (plift ι'), ⋃i∈t, s (plift.down i)) :=
supr_eq_supr_finset' s
/-- Intersection of an indexed family of sets `s : ι → set α` is equal to the intersection of the
intersections of finite subfamilies. This version assumes `ι : Type*`. See also
`Inter_eq_Inter_finset'` for a version that works for `ι : Sort*`. -/
lemma Inter_eq_Inter_finset (s : ι → set α) :
(⋂i, s i) = (⋂t:finset ι, ⋂i∈t, s i) :=
infi_eq_infi_finset s
/-- Intersection of an indexed family of sets `s : ι → set α` is equal to the intersection of the
intersections of finite subfamilies. This version works for `ι : Sort*`. See also
`Inter_eq_Inter_finset` for a version that assumes `ι : Type*` but avoids `plift`s in the right
hand side. -/
lemma Inter_eq_Inter_finset' (s : ι' → set α) :
(⋂i, s i) = (⋂t:finset (plift ι'), ⋂i∈t, s (plift.down i)) :=
infi_eq_infi_finset' s
end set
namespace finset
open function
/-! ### Interaction with big lattice/set operations -/
section lattice
lemma supr_coe [has_Sup β] (f : α → β) (s : finset α) :
(⨆ x ∈ (↑s : set α), f x) = ⨆ x ∈ s, f x :=
rfl
lemma infi_coe [has_Inf β] (f : α → β) (s : finset α) :
(⨅ x ∈ (↑s : set α), f x) = ⨅ x ∈ s, f x :=
rfl
variables [complete_lattice β]
theorem supr_singleton (a : α) (s : α → β) : (⨆ x ∈ ({a} : finset α), s x) = s a :=
by simp
theorem infi_singleton (a : α) (s : α → β) : (⨅ x ∈ ({a} : finset α), s x) = s a :=
by simp
lemma supr_option_to_finset (o : option α) (f : α → β) :
(⨆ x ∈ o.to_finset, f x) = ⨆ x ∈ o, f x :=
by { congr, ext, rw [option.mem_to_finset] }
lemma infi_option_to_finset (o : option α) (f : α → β) :
(⨅ x ∈ o.to_finset, f x) = ⨅ x ∈ o, f x :=
@supr_option_to_finset _ (order_dual β) _ _ _
variables [decidable_eq α]
theorem supr_union {f : α → β} {s t : finset α} :
(⨆ x ∈ s ∪ t, f x) = (⨆x∈s, f x) ⊔ (⨆x∈t, f x) :=
by simp [supr_or, supr_sup_eq]
theorem infi_union {f : α → β} {s t : finset α} :
(⨅ x ∈ s ∪ t, f x) = (⨅ x ∈ s, f x) ⊓ (⨅ x ∈ t, f x) :=
by simp [infi_or, infi_inf_eq]
lemma supr_insert (a : α) (s : finset α) (t : α → β) :
(⨆ x ∈ insert a s, t x) = t a ⊔ (⨆ x ∈ s, t x) :=
by { rw insert_eq, simp only [supr_union, finset.supr_singleton] }
lemma infi_insert (a : α) (s : finset α) (t : α → β) :
(⨅ x ∈ insert a s, t x) = t a ⊓ (⨅ x ∈ s, t x) :=
by { rw insert_eq, simp only [infi_union, finset.infi_singleton] }
lemma supr_finset_image {f : γ → α} {g : α → β} {s : finset γ} :
(⨆ x ∈ s.image f, g x) = (⨆ y ∈ s, g (f y)) :=
by rw [← supr_coe, coe_image, supr_image, supr_coe]
lemma infi_finset_image {f : γ → α} {g : α → β} {s : finset γ} :
(⨅ x ∈ s.image f, g x) = (⨅ y ∈ s, g (f y)) :=
by rw [← infi_coe, coe_image, infi_image, infi_coe]
lemma supr_insert_update {x : α} {t : finset α} (f : α → β) {s : β} (hx : x ∉ t) :
(⨆ (i ∈ insert x t), function.update f x s i) = (s ⊔ ⨆ (i ∈ t), f i) :=
begin
simp only [finset.supr_insert, update_same],
rcongr i hi, apply update_noteq, rintro rfl, exact hx hi
end
lemma infi_insert_update {x : α} {t : finset α} (f : α → β) {s : β} (hx : x ∉ t) :
(⨅ (i ∈ insert x t), update f x s i) = (s ⊓ ⨅ (i ∈ t), f i) :=
@supr_insert_update α (order_dual β) _ _ _ _ f _ hx
lemma supr_bind (s : finset γ) (t : γ → finset α) (f : α → β) :
(⨆ y ∈ s.bind t, f y) = ⨆ (x ∈ s) (y ∈ t x), f y :=
calc (⨆ y ∈ s.bind t, f y) = ⨆ y (hy : ∃ x ∈ s, y ∈ t x), f y :
congr_arg _ $ funext $ λ y, by rw [mem_bind]
... = _ : by simp only [supr_exists, @supr_comm _ α]
lemma infi_bind (s : finset γ) (t : γ → finset α) (f : α → β) :
(⨅ y ∈ s.bind t, f y) = ⨅ (x ∈ s) (y ∈ t x), f y :=
@supr_bind _ (order_dual β) _ _ _ _ _ _
end lattice
@[simp] theorem bUnion_coe (s : finset α) (t : α → set β) :
(⋃ x ∈ (↑s : set α), t x) = ⋃ x ∈ s, t x :=
rfl
@[simp] theorem bInter_coe (s : finset α) (t : α → set β) :
(⋂ x ∈ (↑s : set α), t x) = ⋂ x ∈ s, t x :=
rfl
@[simp] theorem bUnion_singleton (a : α) (s : α → set β) : (⋃ x ∈ ({a} : finset α), s x) = s a :=
supr_singleton a s
@[simp] theorem bInter_singleton (a : α) (s : α → set β) : (⋂ x ∈ ({a} : finset α), s x) = s a :=
infi_singleton a s
@[simp] lemma bUnion_preimage_singleton (f : α → β) (s : finset β) :
(⋃ y ∈ s, f ⁻¹' {y}) = f ⁻¹' ↑s :=
set.bUnion_preimage_singleton f ↑s
@[simp] lemma bUnion_option_to_finset (o : option α) (f : α → set β) :
(⋃ x ∈ o.to_finset, f x) = ⋃ x ∈ o, f x :=
supr_option_to_finset o f
@[simp] lemma bInter_option_to_finset (o : option α) (f : α → set β) :
(⋂ x ∈ o.to_finset, f x) = ⋂ x ∈ o, f x :=
infi_option_to_finset o f
variables [decidable_eq α]
lemma bUnion_union (s t : finset α) (u : α → set β) :
(⋃ x ∈ s ∪ t, u x) = (⋃ x ∈ s, u x) ∪ (⋃ x ∈ t, u x) :=
supr_union
lemma bInter_inter (s t : finset α) (u : α → set β) :
(⋂ x ∈ s ∪ t, u x) = (⋂ x ∈ s, u x) ∩ (⋂ x ∈ t, u x) :=
infi_union
@[simp] lemma bUnion_insert (a : α) (s : finset α) (t : α → set β) :
(⋃ x ∈ insert a s, t x) = t a ∪ (⋃ x ∈ s, t x) :=
supr_insert a s t
@[simp] lemma bInter_insert (a : α) (s : finset α) (t : α → set β) :
(⋂ x ∈ insert a s, t x) = t a ∩ (⋂ x ∈ s, t x) :=
infi_insert a s t
@[simp] lemma bUnion_finset_image {f : γ → α} {g : α → set β} {s : finset γ} :
(⋃x ∈ s.image f, g x) = (⋃y ∈ s, g (f y)) :=
supr_finset_image
@[simp] lemma bInter_finset_image {f : γ → α} {g : α → set β} {s : finset γ} :
(⋂ x ∈ s.image f, g x) = (⋂ y ∈ s, g (f y)) :=
infi_finset_image
lemma bUnion_insert_update {x : α} {t : finset α} (f : α → set β) {s : set β} (hx : x ∉ t) :
(⋃ (i ∈ insert x t), @update _ _ _ f x s i) = (s ∪ ⋃ (i ∈ t), f i) :=
supr_insert_update f hx
lemma bInter_insert_update {x : α} {t : finset α} (f : α → set β) {s : set β} (hx : x ∉ t) :
(⋂ (i ∈ insert x t), @update _ _ _ f x s i) = (s ∩ ⋂ (i ∈ t), f i) :=
infi_insert_update f hx
@[simp] lemma bUnion_bind (s : finset γ) (t : γ → finset α) (f : α → set β) :
(⋃ y ∈ s.bind t, f y) = ⋃ (x ∈ s) (y ∈ t x), f y :=
supr_bind s t f
@[simp] lemma bInter_bind (s : finset γ) (t : γ → finset α) (f : α → set β) :
(⋂ y ∈ s.bind t, f y) = ⋂ (x ∈ s) (y ∈ t x), f y :=
infi_bind s t f
end finset
|
e8acf0f5999b5eb3624eec6352fb1e30893a7744 | 74924b1fe80b8f61262cefcfc0cd4d96135c8731 | /src/algebra/quadratic_discriminant.lean | c092b8c69e5b30728cd4aec82e1d5a3e8d02362d | [
"Apache-2.0"
] | permissive | 101damnations/mathlib | 0938b3806a09032d8716d3642cbab65db7688c23 | 900c53ae6d5e3f8cc47953363479593e8debc4d8 | refs/heads/master | 1,593,832,305,164 | 1,565,631,735,000 | 1,565,631,735,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 6,406 | lean | /-
Copyright (c) 2019 Zhouhang Zhou. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Zhouhang Zhou
-/
import algebra.ordered_field
import tactic.linarith tactic.ring
/-!
# Quadratic discriminants and roots of a quadratic
This file defines the discriminant of a quadratic and prove their
## Main definition
The discriminant of a quadratic `a*x*x + b*x + c` is `b*b - 4*a*c`.
## Main statements
• Roots of a quadratic can be written as `(-b + s) / (2 * a)` or `(-b - s) / (2 * a)`,
where `s` is the square root of the discriminant.
• If the discriminant has no square root, then the corresponding quadratic has no root.
• If a quadratic is always non-negative, then its discriminant is non-positive.
## Tags
polynomial, quadratic, discriminant, root
-/
variables {α : Type*}
section lemmas
variables [linear_ordered_field α] {a b c : α}
lemma exists_le_mul : ∀ a : α, ∃ x : α, a ≤ x * x :=
begin
assume a, cases le_total 1 a with ha ha,
{ use a, exact le_mul_of_ge_one_left (by linarith) ha },
{ use 1, linarith }
end
lemma exists_lt_mul : ∀ a : α, ∃ x : α, a < x * x :=
begin
assume a, rcases (exists_le_mul a) with ⟨x, hx⟩,
cases le_total 0 x with hx' hx',
{ use (x + 1),
have : (x+1)*(x+1) = x*x + 2*x + 1, ring,
exact lt_of_le_of_lt hx (by rw this; linarith) },
{ use (x - 1),
have : (x-1)*(x-1) = x*x - 2*x + 1, ring,
exact lt_of_le_of_lt hx (by rw this; linarith) }
end
end lemmas
variables [linear_ordered_field α] {a b c x : α}
/-- Discriminant of a quadratic -/
def discrim [ring α] (a b c : α) : α := b^2 - 4 * a * c
/--
A quadratic has roots if and only if its discriminant equals some square.
-/
lemma quadratic_eq_zero_iff_discrim_eq_square (ha : a ≠ 0) :
∀ x : α, a * x * x + b * x + c = 0 ↔ discrim a b c = (2 * a * x + b)^2 :=
assume x, iff.intro
( assume h, calc
discrim a b c = 4*a*(a*x*x + b*x + c) + b*b - 4*a*c : by rw [h, discrim]; ring
... = (2*a*x + b)^2 : by ring )
( assume h,
have ha : 2*2*a ≠ 0 := mul_ne_zero (mul_ne_zero two_ne_zero two_ne_zero) ha,
eq_of_mul_eq_mul_left_of_ne_zero ha $
calc
2 * 2 * a * (a * x * x + b * x + c) = (2*a*x + b)^2 - (b^2 - 4*a*c) : by ring
... = 0 : by { rw [← h, discrim], ring }
... = 2*2*a*0 : by ring )
/-- Roots of a quadratic -/
lemma quadratic_eq_zero_iff (ha : a ≠ 0) {s : α} (h : discrim a b c = s * s) :
∀ x : α, a * x * x + b * x + c = 0 ↔ x = (-b + s) / (2 * a) ∨ x = (-b - s) / (2 * a) := assume x,
begin
rw [quadratic_eq_zero_iff_discrim_eq_square ha, h, pow_two, mul_self_eq_mul_self_iff],
have ne : 2 * a ≠ 0 := mul_ne_zero two_ne_zero ha,
have : x = 2 * a * x / (2 * a) := (mul_div_cancel_left x ne).symm,
have h₁ : 2 * a * ((-b + s) / (2 * a)) = -b + s := mul_div_cancel' _ ne,
have h₂ : 2 * a * ((-b - s) / (2 * a)) = -b - s := mul_div_cancel' _ ne,
split,
{ intro h', rcases h', { left, rw h', simpa }, { right, rw h', simpa } },
{ intro h', rcases h', { left, rw [h', h₁], ring }, { right, rw [h', h₂], ring } }
end
/-- A quadratic has roots if its discriminant has square roots -/
lemma exist_quadratic_eq_zero (ha : a ≠ 0) (h : ∃ s, discrim a b c = s * s) :
∃ x, a * x * x + b * x + c = 0 :=
begin
rcases h with ⟨s, hs⟩,
use (-b + s) / (2 * a),
rw quadratic_eq_zero_iff ha hs,
simp
end
/-- Root of a quadratic when its discriminant equals zero -/
lemma quadratic_eq_zero_iff_of_discrim_eq_zero (ha : a ≠ 0) (h : discrim a b c = 0) :
∀ x : α, a * x * x + b * x + c = 0 ↔ x = -b / (2 * a) := assume x,
begin
have : discrim a b c = 0 * 0 := eq.trans h (by ring),
rw quadratic_eq_zero_iff ha this,
simp
end
/-- A quadratic has no root if its discriminant has no square root. -/
lemma quadratic_ne_zero_of_discrim_ne_square (ha : a ≠ 0) (h : ∀ s : α, discrim a b c ≠ s * s) :
∀ (x : α), a * x * x + b * x + c ≠ 0 :=
begin
assume x h',
rw [quadratic_eq_zero_iff_discrim_eq_square ha, pow_two] at h',
have := h _,
contradiction
end
/-- If a polynomial of degree 2 is always nonnegative, then its dicriminant is nonpositive -/
lemma discriminant_le_zero {a b c : α} (h : ∀ x : α, 0 ≤ a*x*x + b*x + c) : discrim a b c ≤ 0 :=
have hc : 0 ≤ c, by { have := h 0, linarith },
begin
rw [discrim, pow_two],
cases lt_trichotomy a 0 with ha ha,
-- if a < 0
cases classical.em (b = 0) with hb hb,
{ rw hb at *,
rcases exists_lt_mul (-c/a) with ⟨x, hx⟩,
have := mul_lt_mul_of_neg_left hx ha,
rw [mul_div_cancel' _ (ne_of_lt ha), ← mul_assoc] at this,
have h₂ := h x, linarith },
{ cases classical.em (c = 0) with hc' hc',
{ rw hc' at *,
have : -(a*-b*-b + b*-b + 0) = (1-a)*(b*b), ring,
have h := h (-b), rw [← neg_nonpos, this] at h,
have : b * b ≤ 0, from nonpos_of_mul_nonpos_left h (by linarith),
linarith },
{ have h := h (-c/b),
have : a*(-c/b)*(-c/b) + b*(-c/b) + c = a*((c/b)*(c/b)),
rw mul_div_cancel' _ hb, ring,
rw this at h,
have : 0 ≤ a := nonneg_of_mul_nonneg_right h (mul_self_pos $ div_ne_zero hc' hb),
linarith [ha] } },
cases ha with ha ha,
-- if a = 0
cases classical.em (b = 0) with hb hb,
{ rw [ha, hb], linarith },
{ have := h ((-c-1)/b), rw [ha, mul_div_cancel' _ hb] at this, linarith },
-- if a > 0
have := calc
4*a* (a*(-(b/a)*(1/2))*(-(b/a)*(1/2)) + b*(-(b/a)*(1/2)) + c)
= (a*(b/a)) * (a*(b/a)) - 2*(a*(b/a))*b + 4*a*c : by ring
... = -(b*b - 4*a*c) : by { simp only [mul_div_cancel' b (ne_of_gt ha)], ring },
have ha' : 0 ≤ 4*a, linarith,
have h := (mul_nonneg ha' (h (-(b/a) * (1/2)))),
rw this at h, rwa ← neg_nonneg
end
/--
If a polynomial of degree 2 is always positive, then its dicriminant is negtive,
at least when the coefficient of the quadratic term is nonzero.
-/
lemma discriminant_lt_zero {a b c : α} (ha : a ≠ 0) (h : ∀ x : α, 0 < a*x*x + b*x + c) :
discrim a b c < 0 :=
begin
have : ∀ x : α, 0 ≤ a*x*x + b*x + c := assume x, le_of_lt (h x),
refine lt_of_le_of_ne (discriminant_le_zero this) _,
assume h',
have := h (-b / (2 * a)),
have : a * (-b / (2 * a)) * (-b / (2 * a)) + b * (-b / (2 * a)) + c = 0,
rw [quadratic_eq_zero_iff_of_discrim_eq_zero ha h' (-b / (2 * a))],
linarith
end
|
ffbb916694ce512deaa733aa6c36fc45bf5dcb77 | 586535ff9853f5b85dc5fdeb5b78f1ad2d573a6e | /src/util.lean | 6f1360a934d27c159d46f06b3041d08d4643d23b | [
"Apache-2.0"
] | permissive | Kha/rc-correctness | fede4ae228c5f04985cf332e718a5a433230b150 | be5a521f1b0528ccbc685619b265cd11ea854edf | refs/heads/master | 1,595,967,325,275 | 1,568,889,756,000 | 1,568,889,756,000 | 209,523,947 | 0 | 0 | Apache-2.0 | 1,568,888,337,000 | 1,568,888,336,000 | null | UTF-8 | Lean | false | false | 29,730 | lean | import tactic.fin_cases
import logic.function
import data.nat.basic
namespace list
open well_founded_tactics
-- sizeof_lt_sizeof_of_mem, map_wf, map_wf_eq_map courtesy of Sebastian Ullrich
lemma sizeof_lt_sizeof_of_mem {α} [has_sizeof α] {a : α} : ∀ {l : list α}, a ∈ l → sizeof a < sizeof l
| [] h := absurd h (not_mem_nil _)
| (b::bs) h :=
begin
cases eq_or_mem_of_mem_cons h with h_1 h_2,
subst h_1,
{ unfold_sizeof, cancel_nat_add_lt, trivial_nat_lt },
{ have aux₁ := sizeof_lt_sizeof_of_mem h_2,
unfold_sizeof,
exact nat.lt_add_left _ _ _ (nat.lt_add_left _ _ _ aux₁) }
end
def map_wf {α β : Type*} [has_sizeof α] (xs : list α) (f : Π (a : α), (sizeof a < 1 + sizeof xs) → β) : list β :=
xs.attach.map (λ p, have sizeof p.val < 1 + sizeof xs,
from nat.lt_add_left _ _ _ (list.sizeof_lt_sizeof_of_mem p.property),
f p.val this)
lemma map_wf_eq_map {α β : Type*} [has_sizeof α] {xs : list α} {f : α → β} : map_wf xs (λ a _, f a) = map f xs :=
by simp only [map_wf, attach, map_pmap, pmap_eq_map]
lemma sizeof_filter_le_sizeof {α : Type*} (p : α → Prop) [decidable_pred p] (xs : list α) : list.sizeof (filter p xs) <= list.sizeof xs :=
begin
induction xs,
{ rw list.filter_nil },
by_cases p xs_hd,
{ rw filter_cons_of_pos xs_tl h,
unfold_sizeof,
assumption },
{ rw filter_cons_of_neg xs_tl h,
unfold_sizeof,
exact le_add_left xs_ih }
end
def group {α : Type*} [p : setoid α] [decidable_rel p.r] : list α → list (list α)
| [] := []
| (x :: xs) := have list.sizeof (filter (not ∘ (≈ x)) xs) < 1 + list.sizeof xs, from
begin
have h : list.sizeof (filter (not ∘ (≈ x)) xs) ≤ list.sizeof xs, from sizeof_filter_le_sizeof _ xs,
rw nat.add_comm,
rw ←nat.succ_eq_add_one,
rwa nat.lt_succ_iff
end, (x :: filter (≈ x) xs) :: group (filter (not ∘ (≈ x)) xs)
lemma sizeof_lt_of_length_lt {α : Type*} {xs ys : list α} (h : length xs < length ys) : list.sizeof xs < list.sizeof ys :=
begin
induction xs generalizing ys,
{ induction ys;
unfold_sizeof,
{ exact absurd h (lt_irrefl (length nil)) },
induction ys_tl;
unfold_sizeof,
{ exact zero_lt_one },
exact nat.zero_lt_one_add (list.sizeof ys_tl_tl) },
induction ys;
unfold_sizeof,
{ simp only [add_comm, length] at h,
exact false.elim (nat.not_succ_le_zero (1 + length xs_tl) h) },
simp only [add_comm, length, add_lt_add_iff_left] at h,
exact xs_ih h
end
@[elab_as_eliminator] def strong_induction_on {α : Type*} {p : list α → Sort*} :
∀ xs : list α, (∀ xs, (∀ ys, length ys < length xs → p ys) → p xs) → p xs
| xs := λ ih, ih xs (λ ys h1,
have list.sizeof ys < list.sizeof xs, from sizeof_lt_of_length_lt h1,
strong_induction_on ys ih)
lemma length_filter_le_length {α : Type*} (p : α → Prop) [decidable_pred p] (xs : list α) :
length (filter p xs) <= length xs :=
length_le_of_sublist (filter_sublist xs)
lemma filter_append_not_filter_perm {α : Type*} (p : α → Prop) [decidable_pred p] (xs : list α) :
filter p xs ++ filter (not ∘ p) xs ~ xs :=
begin
letI := classical.dec_eq α,
rw perm_iff_count,
intro a,
induction xs,
{ simp only [filter_nil, append_nil] },
by_cases p xs_hd,
{ rw filter_cons_of_pos xs_tl h,
rw @filter_cons_of_neg _ (not ∘ p) _ _ xs_tl (non_contradictory_intro h),
simp only [cons_append, count_cons'],
apply congr_arg (+ ite (a = xs_hd) 1 0),
assumption },
{ rw @filter_cons_of_pos _ (not ∘ p) _ _ xs_tl h,
rw filter_cons_of_neg xs_tl h,
simp only [count_append, count_cons'],
rw ←add_assoc,
apply congr_arg (+ ite (a = xs_hd) 1 0),
rwa ←count_append }
end
lemma length_filter_lt_length_cons {α : Type*} (p : α → Prop) [decidable_pred p] (x : α) (xs : list α) :
length (filter p xs) < length (x :: xs) :=
calc length (filter p xs)
≤ length xs : length_filter_le_length _ xs
... < length (x :: xs) : by simp only [lt_add_iff_pos_left, add_comm, length, nat.zero_lt_one]
lemma join_group_perm {α : Type*} [p : setoid α] [decidable_rel p.r] (xs : list α) : join (group xs) ~ xs :=
begin
letI := classical.dec_eq α,
induction xs using list.strong_induction_on with xs ih,
cases xs,
{ simp only [group, join] },
rw perm_iff_count,
intro a,
simp only [group, cons_append, join, count_cons'],
apply congr_arg (+ ite (a = xs_hd) 1 0),
simp only [count_append],
simp only [] at ih,
replace ih := ih (filter (not ∘ (≈ xs_hd)) xs_tl),
have h : length (filter (not ∘ (≈ xs_hd)) xs_tl) < length (xs_hd :: xs_tl),
{ exact length_filter_lt_length_cons _ xs_hd xs_tl },
have perm, from ih h,
rw perm_iff_count at perm,
rw perm a,
rw ←count_append,
revert a,
rw ←perm_iff_count,
exact filter_append_not_filter_perm (≈ xs_hd) xs_tl
end
lemma group_equiv {α : Type*} [p : setoid α] [decidable_rel p.r] {xs : list α} :
∀ g ∈ group xs, ∀ x y ∈ g, x ≈ y :=
begin
intros g g_group x y x_in_g y_in_g,
induction xs using list.strong_induction_on with xs ih,
cases xs,
{ simp only [group, not_mem_nil] at g_group,
contradiction },
simp only [group, mem_cons_iff] at g_group,
cases g_group,
{ rw g_group at *,
simp only [mem_cons_iff, mem_filter] at x_in_g y_in_g,
cases x_in_g,
{ rw x_in_g,
cases y_in_g,
{ rw y_in_g },
{ symmetry,
exact y_in_g.right } },
{ cases y_in_g,
{ rw ←y_in_g at x_in_g,
exact x_in_g.right },
{ transitivity,
{ exact x_in_g.right },
{ symmetry,
exact y_in_g.right } } } },
exact ih _ (length_filter_lt_length_cons _ xs_hd xs_tl) g_group
end
lemma nil_not_mem_group {α : Type*} [p : setoid α] [decidable_rel p.r] (xs : list α)
: [] ∉ group xs :=
begin
induction xs using list.strong_induction_on with xs ih,
cases xs,
{ simp only [group], exact not_mem_nil nil },
simp only [group, mem_cons_iff],
rw not_or_distrib,
simp only [true_and, not_false_iff],
exact ih (filter (not ∘ λ (_x : α), _x ≈ xs_hd) xs_tl) (length_filter_lt_length_cons _ xs_hd xs_tl)
end
lemma group_equiv_disjoint' {α : Type*} [p : setoid α] [decidable_rel p.r] (xs : list α) :
∀ g1 g2 ∈ group xs, (∀ x1 ∈ g1, ∀ x2 ∈ g2, x1 ≈ x2) → g1 = g2 :=
begin
intros g1 g2 g1_group g2_group h,
induction xs using list.strong_induction_on with xs ih,
cases xs,
{ simp only [group, not_mem_nil] at g1_group,
contradiction },
simp only [group, mem_cons_iff] at g1_group g2_group,
cases g1_group,
{ rw g1_group at *,
cases g2_group,
{ rw g2_group at * },
{ cases g2,
{ exact absurd g2_group (nil_not_mem_group _) },
have h3 : ∃ l, l ∈ group (filter (not ∘ λ (_x : α), _x ≈ xs_hd) xs_tl) ∧ g2_hd ∈ l,
from ⟨g2_hd :: g2_tl, ⟨g2_group, mem_cons_self g2_hd g2_tl⟩⟩,
replace h3 := mem_join.mpr h3,
rw mem_of_perm (join_group_perm _) at h3,
replace h3 := of_mem_filter h3,
simp only [function.comp_app] at h3,
have h4 : g2_hd ≈ xs_hd,
{ symmetry,
exact h xs_hd (mem_cons_self xs_hd _) g2_hd (mem_cons_self g2_hd g2_tl) },
contradiction } },
cases g2_group,
rw g2_group at *,
{ cases g1,
{ exact absurd g1_group (nil_not_mem_group _) },
have h3 : ∃ l, l ∈ group (filter (not ∘ λ (_x : α), _x ≈ xs_hd) xs_tl) ∧ g1_hd ∈ l,
from ⟨g1_hd :: g1_tl, ⟨g1_group, mem_cons_self g1_hd g1_tl⟩⟩,
replace h3 := mem_join.mpr h3,
rw mem_of_perm (join_group_perm _) at h3,
replace h3 := of_mem_filter h3,
simp only [function.comp_app] at h3,
have h4 : g1_hd ≈ xs_hd, from h g1_hd (mem_cons_self g1_hd g1_tl) xs_hd (mem_cons_self xs_hd _),
contradiction },
exact ih _ (length_filter_lt_length_cons _ xs_hd xs_tl) g1_group g2_group
end
section group_equiv_disjoint
local attribute [instance] classical.prop_decidable
lemma group_equiv_disjoint {α : Type*} [p : setoid α] [d : decidable_rel p.r] (xs : list α) :
∀ g1 g2 ∈ group xs, g1 ≠ g2 → ∀ x1 ∈ g1, ∀ x2 ∈ g2, ¬(x1 ≈ x2) :=
begin
intros g1 g2 g1_group g2_group,
contrapose,
simp only [and_imp, not_imp, not_not, exists_imp_distrib, not_forall],
intros x1 x1_in_g1 x2 x2_in_g2 h,
suffices g : ∀ x1 ∈ g1, ∀ x2 ∈ g2, x1 ≈ x2,
{ exact group_equiv_disjoint' xs g1 g2 g1_group g2_group g },
intros y1 y1_in_g1 y2 y2_in_g2,
calc y1 ≈ x1 : group_equiv g1 g1_group y1 x1 y1_in_g1 x1_in_g1
... ≈ x2 : h
... ≈ y2 : group_equiv g2 g2_group x2 y2 x2_in_g2 y2_in_g2
end
end group_equiv_disjoint
lemma nodup_perm_group {α : Type*} [decidable_eq α] [p : setoid α] [decidable_rel p.r] (xs : list α) :
pairwise (λ a b, ¬(a ~ b)) (group xs) :=
begin
induction xs using list.strong_induction_on with xs ih,
cases xs,
{ simp only [group],
exact pairwise.nil },
simp only [group, pairwise_cons],
split,
{ intros a h h',
have h2 : ∃ l, l ∈ group (filter (not ∘ λ (_x : α), _x ≈ xs_hd) xs_tl) ∧ xs_hd ∈ l,
{ use a,
split,
{ assumption },
rw mem_of_perm h'.symm,
exact mem_cons_self xs_hd _ },
replace h2 := mem_join.mpr h2,
rw mem_of_perm (join_group_perm _) at h2,
replace h2 := of_mem_filter h2,
simp only [function.comp_app] at h2,
exact absurd (setoid.refl xs_hd) h2 },
exact ih _ (length_filter_lt_length_cons _ xs_hd xs_tl)
end
lemma nodup_group {α : Type*} [decidable_eq α] [p : setoid α] [decidable_rel p.r] (xs : list α) :
nodup (group xs) :=
begin
have h, from nodup_perm_group xs,
unfold nodup,
have h' : ∀ a b, (λ (a b : list α), ¬a ~ b) a b → ne a b,
{ intros a b h' a_eq_b,
rw a_eq_b at h',
exact h' (setoid.refl b) },
exact pairwise.imp h' h
end
lemma pairwise_equiv_disjoint_group {α : Type*} [decidable_eq α] [p : setoid α] [decidable_rel p.r] (xs : list α) :
pairwise (λ g1 g2 : list α, ∀ x1 ∈ g1, ∀ x2 ∈ g2, ¬(x1 ≈ x2)) (group xs) :=
begin
have h, from nodup_perm_group xs,
suffices h' : ∀ g1 g2, g1 ∈ group xs → g2 ∈ group xs → ¬(g1 ~ g2) → ∀ x1 ∈ g1, ∀ x2 ∈ g2, ¬(x1 ≈ x2),
{ exact pairwise.imp_of_mem h' h },
intros g1 g2 g1_group g2_group g1_not_perm_g2,
have g1_neq_g2 : g1 ≠ g2,
{ intro h',
rw h' at g1_not_perm_g2,
exact absurd (setoid.refl g2) g1_not_perm_g2 },
exact group_equiv_disjoint xs g1 g2 g1_group g2_group g1_neq_g2
end
lemma pairwise_disjoint_group {α : Type*} [decidable_eq α] [p : setoid α] [decidable_rel p.r] (xs : list α) :
pairwise disjoint (group xs) :=
begin
have h, from pairwise_equiv_disjoint_group xs,
suffices h' : ∀ g1 g2 : list α, (∀ (x1 : α), x1 ∈ g1 → ∀ (x2 : α), x2 ∈ g2 → ¬x1 ≈ x2) → disjoint g1 g2,
{ exact pairwise.imp h' h },
intros g1 g2 h',
unfold disjoint,
intros a a_in_g1 a_in_g2,
exact absurd (setoid.refl a) (h' a a_in_g1 a a_in_g2)
end
lemma group_perm_iff_group_sub {α : Type*} [p : setoid α] [decidable_rel p.r] (xs ys : list α) :
group xs ~ group ys ↔ group xs ⊆ group ys ∧ group ys ⊆ group xs :=
begin
letI := classical.dec_eq α,
split,
{ intro h,
exact ⟨perm_subset h, perm_subset h.symm⟩ },
rintro ⟨xs_sub_ys, ys_sub_xs⟩,
rw perm_iff_count,
intro g,
have h1, from nat.of_le_succ (nodup_iff_count_le_one.mp (nodup_group xs) g),
have h2, from nat.of_le_succ (nodup_iff_count_le_one.mp (nodup_group ys) g),
rw le_zero_iff_eq at h1 h2,
cases h1,
{ cases h2,
{ rw h1, rw h2 },
{ replace h1 := not_mem_of_count_eq_zero h1,
replace h2 := count_pos.mp (h2.symm.subst nat.zero_lt_one),
rw subset_def at ys_sub_xs,
exact absurd (ys_sub_xs h2) h1 } },
{ cases h2,
{ replace h1 := count_pos.mp (h1.symm.subst nat.zero_lt_one),
replace h2 := not_mem_of_count_eq_zero h2,
rw subset_def at ys_sub_xs,
exact absurd (xs_sub_ys h1) h2 },
{ rw h1, rw h2 } }
end
lemma filter_equiv_mem_group {α : Type*} [p : setoid α] [decidable_rel p.r] {x : α} {xs : list α} (h : x ∈ xs) :
filter (≈ x) xs ∈ group xs :=
begin
induction xs using list.strong_induction_on with xs ih,
cases xs,
{ simp only [not_mem_nil] at h, contradiction },
simp only [group, mem_cons_iff],
by_cases h_equiv : xs_hd ≈ x,
{ apply or.inl,
have g : ∀ y ∈ xs_tl, y ≈ xs_hd ↔ y ≈ x,
{ intros y y_in_tl,
split;
intro g,
{ transitivity, exact g, exact h_equiv },
{ transitivity, exact g, exact (setoid.symm h_equiv) } },
rw filter_congr g,
apply filter_cons_of_pos,
assumption },
apply or.inr,
rw @filter_cons_of_neg _ (λ y, y ≈ x) _ xs_hd xs_tl h_equiv,
simp only [mem_cons_iff] at h,
cases h,
{ rw h at h_equiv,
exact absurd (setoid.refl xs_hd) h_equiv },
have h' : x ∈ filter (not ∘ λ (_x : α), _x ≈ xs_hd) xs_tl,
{ apply mem_filter_of_mem h,
simp only [function.comp_app],
intro h',
exact absurd (setoid.symm h') h_equiv },
have g, from ih _ (length_filter_lt_length_cons _ xs_hd xs_tl) h',
rw filter_filter at g,
simp only [function.comp_app] at g,
have g' : ∀ y ∈ xs_tl, (y ≈ x ∧ ¬y ≈ xs_hd) ↔ y ≈ x,
{ intros y y_in_tl,
split,
{ intro h'',
exact h''.left },
intro y_eq_x,
split,
{ assumption },
intro h'',
exact absurd (setoid.trans (setoid.symm h'') y_eq_x) h_equiv },
rwa filter_congr g' at g
end
lemma cons_eq_filter_of_group {α : Type*} [p : setoid α] [decidable_rel p.r] {g_hd : α} {xs g_tl : list α}
(h : (g_hd :: g_tl : list α) ∈ group xs) :
(g_hd :: g_tl : list α) = filter (≈ g_hd) xs :=
begin
induction xs using list.strong_induction_on with xs ih,
cases xs,
{ simp only [group, not_mem_nil] at h, contradiction },
simp only [group, mem_cons_iff] at h,
rcases h with ⟨hd_eq, h⟩,
{ rw hd_eq at *,
rw h at *,
exact (@filter_cons_of_pos _ (≈ xs_hd) _ _ xs_tl (setoid.refl xs_hd)).symm },
have g : (g_hd :: g_tl : list α) = _, from ih _ (length_filter_lt_length_cons _ xs_hd xs_tl) h,
rw g,
simp only [filter_filter, function.comp_app] at *,
by_cases g' : xs_hd ≈ g_hd,
{ have eqv : ∀ y ∈ xs_tl, (y ≈ g_hd ∧ ¬y ≈ xs_hd) ↔ false,
{ intros y y_in_tl,
split,
{ rintros ⟨a, b⟩,
exact absurd (setoid.trans a (setoid.symm g')) b },
{ intro f, contradiction } },
rw filter_congr eqv at g,
simp only [filter_false] at g,
contradiction },
rw @filter_cons_of_neg _ (λ y, y ≈ g_hd) _ xs_hd xs_tl g',
have eqv : ∀ y ∈ xs_tl, (y ≈ g_hd ∧ ¬y ≈ xs_hd) ↔ y ≈ g_hd,
{ intros y y_in_tl,
split,
{ intro x, exact x.left },
{ intro y_eq_g_hd,
split,
{ assumption },
{ intro y_eq_xs_hd,
exact absurd (setoid.trans (setoid.symm y_eq_xs_hd) y_eq_g_hd) g' } } },
rw filter_congr eqv
end
lemma group_eq_of_mem_equiv {α : Type*} [p : setoid α] [decidable_rel p.r] {xs g1 g2 : list α}
(h1 : g1 ∈ group xs) (h2 : g2 ∈ group xs) (h : ∃ x ∈ g1, ∃ y ∈ g2, x ≈ y) :
g1 = g2 :=
begin
cases g1,
{ exact absurd h1 (nil_not_mem_group xs) },
cases g2,
{ exact absurd h2 (nil_not_mem_group xs) },
rw cons_eq_filter_of_group h1 at *,
rw cons_eq_filter_of_group h2 at *,
rcases h with ⟨x, x_in_g1, y, y_in_g2, h⟩,
simp only [mem_filter] at *,
apply filter_congr,
intros z z_in_xs,
split;
intro h_eq,
{ calc z ≈ g1_hd : h_eq
... ≈ x : setoid.symm x_in_g1.right
... ≈ y : h
... ≈ g2_hd : y_in_g2.right },
{ calc z ≈ g2_hd : h_eq
... ≈ y : setoid.symm y_in_g2.right
... ≈ x : setoid.symm h
... ≈ g1_hd : x_in_g1.right }
end
lemma group_perm_of_perm {α : Type*} [p : setoid α] [decidable_rel p.r] {xs ys : list α} (h : xs ~ ys) :
∀ gx ∈ group xs, ∃ gy ∈ group ys, gx ~ gy :=
begin
intros gx gx_group,
cases gx,
{ exact absurd gx_group (nil_not_mem_group xs) },
set gy := filter (≈ gx_hd) ys with gy_def,
use gy,
have g : gx_hd ∈ ys,
from (mem_of_perm h).mp
((mem_of_perm (join_group_perm xs)).mp
(mem_join_of_mem gx_group (mem_cons_self gx_hd gx_tl))),
use filter_equiv_mem_group g,
rw gy_def,
rw cons_eq_filter_of_group gx_group,
apply perm_filter,
assumption
end
section map_on_of_nodup
local attribute [instance] classical.prop_decidable
lemma map_on_of_nodup {α β : Type*} {f : α → β} {s : list α} (nd : nodup (map f s))
{x : α} (x_in_s : x ∈ s) {y : α} (y_in_s : y ∈ s) (f_eq : f x = f y) : x = y :=
begin
unfold nodup at nd,
rw pairwise_map at nd,
have s : symmetric (λ (a b : α), f a ≠ f b),
{ unfold symmetric,
intros x y,
exact ne.symm },
have h, from forall_of_pairwise s nd x x_in_s y y_in_s,
by_contradiction,
exact absurd f_eq (h a)
end
end map_on_of_nodup
end list
namespace multiset
def group' {α : Type*} [p : setoid α] [decidable_rel p.r] (xs : list α) : multiset (multiset α) :=
↑(list.map (λ g, (↑g : multiset α)) (list.group xs))
lemma nodup_map_coe_of_perm_nodup {α : Type*} [p : setoid α] [decidable_rel p.r] (xs : list (list α)) (h : list.pairwise (λ a b, ¬(a ~ b)) xs) :
list.nodup (list.map coe xs : list (multiset α)) :=
begin
letI := classical.dec_eq α,
rw list.nodup_iff_count_le_one,
intro s,
induction xs,
{ simp only [list.not_mem_nil, list.count_eq_zero_of_not_mem, zero_le, not_false_iff, list.map, zero_le_one] },
simp only [list.map],
rw list.count_cons,
split_ifs,
{ apply nat.succ_le_succ,
simp only [le_zero_iff_eq],
rw list.count_eq_zero_of_not_mem,
rw h_1 at *,
simp only [not_exists, coe_eq_coe, list.mem_map, not_and],
intros a a_in_tl,
replace h := list.rel_of_pairwise_cons h a_in_tl,
intro h',
exact absurd (h'.symm) h },
{ exact xs_ih (list.pairwise_of_pairwise_cons h) }
end
lemma group'_eq_of_perm {α : Type*} [p : setoid α] [decidable_rel p.r] {xs ys : list α} (h : xs ~ ys) : group' xs = group' ys :=
begin
letI := classical.dec_eq α,
unfold group',
simp only [coe_eq_coe],
rw list.perm_ext (nodup_map_coe_of_perm_nodup _ (list.nodup_perm_group xs))
(nodup_map_coe_of_perm_nodup _ (list.nodup_perm_group ys)),
simp only [list.mem_map],
intro s,
split,
{ rintro ⟨ax, ax_group, ax_eq_s⟩,
rcases list.group_perm_of_perm h ax ax_group with ⟨ay, ⟨ay_group, ax_eq_ay⟩⟩,
use [ay, ay_group],
rw ←coe_eq_coe at ax_eq_ay,
rw ←ax_eq_ay,
assumption },
{ rintro ⟨ay, ay_group, ay_eq_s⟩,
rcases list.group_perm_of_perm h.symm ay ay_group with ⟨ax, ⟨ax_group, ay_eq_ax⟩⟩,
use [ax, ax_group],
rw ←coe_eq_coe at ay_eq_ax,
rw ←ay_eq_ax,
assumption }
end
lemma join_group'_eq {α : Type*} [p : setoid α] [decidable_rel p.r] (xs : list α) : join (group' xs) = xs :=
begin
simp only [group', coe_join, coe_eq_coe],
exact list.join_group_perm xs
end
lemma nil_not_mem_group' {α : Type*} [p : setoid α] [decidable_rel p.r] (xs : list α)
: ∅ ∉ group' xs :=
begin
simp [group'],
intros x x_in_group h,
rw coe_eq_zero at h,
rw h at x_in_group,
have g, from list.nil_not_mem_group xs,
contradiction
end
lemma group'_equiv {α : Type*} [p : setoid α] [decidable_rel p.r] {xs : list α} :
∀ g ∈ group' xs, ∀ x y ∈ g, x ≈ y :=
begin
simp only [group', and_imp, list.mem_map, mem_coe, exists_imp_distrib],
intros g x h heq,
rw ←heq,
simp only [mem_coe],
exact list.group_equiv x h
end
lemma pairwise_equiv_disjoint_group' {α : Type*} [decidable_eq α] [p : setoid α] [decidable_rel p.r] (xs : list α) :
pairwise (λ g1 g2 : multiset α, ∀ x1 ∈ g1, ∀ x2 ∈ g2, ¬(x1 ≈ x2)) (group' xs) :=
begin
simp only [group'],
rw pairwise_coe_iff_pairwise,
have h, from list.pairwise_equiv_disjoint_group xs,
{ rw list.pairwise_map,
simpa only [mem_coe] },
unfold symmetric,
intros x y h x1 x1_in_y x2 x2_in_x h',
exact absurd (setoid.symm h') (h x2 x2_in_x x1 x1_in_y)
end
lemma filter_equiv_mem_group' {α : Type*} [p : setoid α] [decidable_rel p.r] {x : α} {xs : list α} (h : x ∈ xs) :
filter (≈ x) xs ∈ group' xs :=
begin
simp only [group', coe_filter, coe_eq_coe, list.mem_map, mem_coe],
have h, from list.filter_equiv_mem_group h,
use list.filter (λ (_x : α), _x ≈ x) xs,
split,
{ assumption },
refl
end
lemma subset_of_eq {α : Type*} {xs ys : multiset α} (h : xs = ys) : xs ⊆ ys ∧ ys ⊆ xs :=
begin
rw h,
exact ⟨subset.refl ys, subset.refl ys⟩
end
lemma cons_eq_filter_of_group' {α : Type*} [p : setoid α] [decidable_rel p.r] {g_hd : α} {g_tl : multiset α} {xs : list α}
(h : g_hd :: g_tl ∈ group' xs) :
g_hd :: g_tl = filter (≈ g_hd) xs :=
begin
letI := classical.dec_eq α,
simp only [group', list.mem_map, mem_coe, list.map] at h,
simp only [coe_filter],
rcases h with ⟨a, a_group, a_eq_g⟩,
cases a,
{ simp only [coe_nil_eq_zero, zero_ne_cons] at a_eq_g,
contradiction },
have g : (a_hd :: a_tl : list α) = list.filter (λ (_x : α), _x ≈ a_hd) xs, from list.cons_eq_filter_of_group a_group,
rw ←a_eq_g,
rw g,
simp only [coe_eq_coe],
have g_hd_mem_a, from (subset_of_eq a_eq_g).right (mem_cons_self g_hd g_tl),
simp only [mem_coe] at g_hd_mem_a,
have g_hd_equiv_a_hd, from list.group_equiv (a_hd :: a_tl) a_group a_hd g_hd (list.mem_cons_self a_hd a_tl) g_hd_mem_a,
have equiv : (∀ x ∈ xs, x ≈ a_hd ↔ x ≈ g_hd),
{ intros x x_in_xs,
split;
intro h_eq,
{ exact setoid.trans h_eq g_hd_equiv_a_hd },
{ exact setoid.trans h_eq (setoid.symm g_hd_equiv_a_hd) } },
rw list.filter_congr equiv
end
lemma group'_eq_of_mem_equiv {α : Type*} [p : setoid α] [decidable_rel p.r] {xs : list α} {g1 g2 : multiset α}
(h1 : g1 ∈ group' xs) (h2 : g2 ∈ group' xs) (h : ∃ x ∈ g1, ∃ y ∈ g2, x ≈ y) :
g1 = g2 :=
begin
simp only [group', list.mem_map, mem_coe] at h1 h2,
rcases h1 with ⟨g1a, g1a_group, g1a_def⟩,
rcases h2 with ⟨g2a, g2a_group, g2a_def⟩,
rw ←g1a_def at *,
rw ←g2a_def at *,
simp only [coe_eq_coe],
simp only [exists_prop, mem_coe] at h,
rcases h with ⟨x, x_in_g1a, y, y_in_g2a, h⟩,
rw list.group_eq_of_mem_equiv g1a_group g2a_group ⟨x, x_in_g1a, y, y_in_g2a, h⟩
end
def group {α : Type*} [p : setoid α] [decidable_rel p.r] (s : multiset α) : multiset (multiset α) :=
quot.lift_on s group' (@group'_eq_of_perm _ _ _)
lemma join_group_eq {α : Type*} [p : setoid α] [decidable_rel p.r] (xs : multiset α) : join (group xs) = xs :=
quot.induction_on xs join_group'_eq
lemma nil_not_mem_group {α : Type*} [p : setoid α] [decidable_rel p.r] (xs : multiset α) : ∅ ∉ group xs :=
quot.induction_on xs nil_not_mem_group'
lemma group_equiv {α : Type*} [p : setoid α] [decidable_rel p.r] {xs : multiset α} :
∀ g ∈ group xs, ∀ x y ∈ g, x ≈ y :=
quot.induction_on xs (@group'_equiv _ _ _)
lemma pairwise_equiv_disjoint_group {α : Type*} [decidable_eq α] [p : setoid α] [decidable_rel p.r] (xs : multiset α) :
pairwise (λ g1 g2 : multiset α, ∀ x1 ∈ g1, ∀ x2 ∈ g2, ¬(x1 ≈ x2)) (group xs) :=
quot.induction_on xs pairwise_equiv_disjoint_group'
lemma filter_equiv_mem_group {α : Type*} [p : setoid α] [decidable_rel p.r] {x : α} {xs : multiset α} :
x ∈ xs → filter (≈ x) xs ∈ group xs :=
quot.induction_on xs (λ l, filter_equiv_mem_group')
lemma cons_eq_filter_of_group {α : Type*} [p : setoid α] [decidable_rel p.r] {g_hd : α} {g_tl : multiset α} {xs : multiset α} :
g_hd :: g_tl ∈ group xs → g_hd :: g_tl = filter (≈ g_hd) xs :=
quot.induction_on xs (λ l, cons_eq_filter_of_group')
lemma group_eq_of_mem_equiv {α : Type*} [p : setoid α] [decidable_rel p.r] {xs g1 g2 : multiset α} :
g1 ∈ group xs → g2 ∈ group xs → (∃ x ∈ g1, ∃ y ∈ g2, x ≈ y) → g1 = g2 :=
quot.induction_on xs (λ l, @group'_eq_of_mem_equiv _ _ _ l _ _)
@[simp] lemma filter_true {α : Type*} {h : decidable_pred (λ a : α, true)} (s : multiset α) :
@filter α (λ _, true) h s = s :=
quot.induction_on s (λ l, congr_arg coe (list.filter_true l))
@[simp] lemma filter_false {α : Type*} {h : decidable_pred (λ a : α, false)} (s : multiset α) :
@filter α (λ _, false) h s = [] :=
quot.induction_on s (λ l, congr_arg coe (list.filter_false l))
lemma disjoint_filter_filter {α : Type*} {p1 p2 : α → Prop} [decidable_pred p1] [decidable_pred p2] {s : multiset α} :
disjoint (s.filter p1) (s.filter p2) ↔ ∀ x ∈ s, p1 x → ¬p2 x :=
begin
simp only [disjoint, and_imp, mem_filter],
split;
intro h,
{ intros x x_in_s p1_x,
exact h x_in_s p1_x x_in_s },
intros x x_in_s p1_x x_in_s,
exact h x x_in_s p1_x
end
lemma map_on_of_nodup {α β : Type*} {f : α → β} {s : multiset α} :
nodup (map f s) → ∀ x ∈ s, ∀ y ∈ s, f x = f y → x = y :=
quot.induction_on s (λ l, @list.map_on_of_nodup α β f l)
lemma map_add_of_disjoint {α β : Type*} [decidable_eq α] (f1 f2 : α → β) {s1 s2 : multiset α} (h : disjoint s1 s2) :
map f1 s1 + map f2 s2 = map (λ x, if x ∈ s1 then f1 x else f2 x) (s1 + s2) :=
begin
simp only [map_add],
have h1 : map f1 s1 = map (λ (x : α), ite (x ∈ s1) (f1 x) (f2 x)) s1,
{ have h1' : ∀ x ∈ s1, f1 x = ite (x ∈ s1) (f1 x) (f2 x),
{ intros x x_in_s1,
rw if_pos x_in_s1 },
rw map_congr h1' },
have h2 : map f2 s2 = map (λ (x : α), ite (x ∈ s1) (f1 x) (f2 x)) s2,
{ have h2' : ∀ x ∈ s2, f2 x = ite (x ∈ s1) (f1 x) (f2 x),
{ intros x x_in_s2,
rw if_neg,
exact h.symm x_in_s2 },
rw map_congr h2' },
rw h1, rw h2
end
end multiset
namespace finset
def join {α : Type*} [decidable_eq α] (xs : list (finset α)) : finset α := xs.foldr (∪) ∅
@[simp] theorem mem_join {α : Type*} [decidable_eq α] {x : α} {xs : list (finset α)} : x ∈ join xs ↔ ∃ S ∈ xs, x ∈ S :=
begin
apply iff.intro,
{ intro h,
induction xs;
unfold join at *,
{ simp only [list.foldr_nil, not_mem_empty] at h,
contradiction },
{ simp only [mem_union, list.foldr_cons] at h,
cases h,
{ exact ⟨xs_hd, ⟨or.inl rfl, h⟩⟩ },
{ rcases xs_ih h with ⟨S, S_in_tl, x_in_S⟩,
exact ⟨S, ⟨or.inr S_in_tl, x_in_S⟩⟩ } } },
{ intro h,
induction xs,
{ simp only [list.not_mem_nil, exists_false, not_false_iff, exists_prop_of_false] at h,
contradiction },
{ unfold join at *,
rcases h with ⟨S, S_in_hd_or_tl, x_in_S⟩,
simp only [mem_union, list.foldr_cons, list.mem_cons_iff] at *,
cases S_in_hd_or_tl,
{ rw S_in_hd_or_tl at x_in_S,
exact or.inl x_in_S },
{ exact or.inr (xs_ih ⟨S, S_in_hd_or_tl, x_in_S⟩) } } }
end
@[simp] lemma erase_insert_eq_erase {α : Type*} [decidable_eq α] (s : finset α) (a : α) :
erase (insert a s) a = erase s a :=
begin
ext,
simp only [mem_insert, mem_erase, and_or_distrib_left, not_and_self, false_or]
end
lemma erase_insert_eq_insert_erase {α : Type*} [decidable_eq α] {a b : α} (s : finset α)
(h : a ≠ b) :
erase (insert a s) b = insert a (erase s b) :=
begin
ext,
simp only [mem_insert, mem_erase, and_or_distrib_left],
apply iff.intro;
intro h₁;
cases h₁,
{ exact or.inl h₁.right },
{ exact or.inr h₁ },
{ rw h₁, exact or.inl ⟨h, rfl⟩ },
{ exact or.inr h₁ }
end
-- cool sort lemmas that i didn't need in the end that are useful for
-- induction over a finset in a sort
lemma sort_empty {α : Type*} (r : α → α → Prop) [decidable_rel r]
[is_trans α r] [is_antisymm α r] [is_total α r] :
sort r ∅ = [] :=
begin
apply (multiset.coe_eq_zero (sort r ∅)).mp,
simp only [sort_eq, empty_val]
end
lemma sort_split {α : Type*} [decidable_eq α] (p : α → α → Prop) [decidable_rel p]
[is_trans α p] [is_antisymm α p] [is_total α p]
(a : α) (s : finset α) :
∃ l r : list α, sort p (insert a s) = l ++ a :: r :=
list.mem_split ((mem_sort p).mpr (mem_insert_self a s))
end finset
namespace function
notation f `[` a `↦` b `]` := function.update f a b
end function |
193e619d72406573d640bee11cb5d0d9b43c5ec4 | 94e33a31faa76775069b071adea97e86e218a8ee | /src/analysis/complex/removable_singularity.lean | 245b0a0ab5b5148833c90ce3af1e89bba5a866d2 | [
"Apache-2.0"
] | permissive | urkud/mathlib | eab80095e1b9f1513bfb7f25b4fa82fa4fd02989 | 6379d39e6b5b279df9715f8011369a301b634e41 | refs/heads/master | 1,658,425,342,662 | 1,658,078,703,000 | 1,658,078,703,000 | 186,910,338 | 0 | 0 | Apache-2.0 | 1,568,512,083,000 | 1,557,958,709,000 | Lean | UTF-8 | Lean | false | false | 7,321 | lean | /-
Copyright (c) 2022 Yury Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury Kudryashov
-/
import analysis.calculus.fderiv_analytic
import analysis.asymptotics.specific_asymptotics
import analysis.complex.cauchy_integral
/-!
# Removable singularity theorem
In this file we prove Riemann's removable singularity theorem: if `f : ℂ → E` is complex
differentiable in a punctured neighborhood of a point `c` and is bounded in a punctured neighborhood
of `c` (or, more generally, $f(z) - f(c)=o((z-c)^{-1})$), then it has a limit at `c` and the
function `function.update f c (lim (𝓝[≠] c) f)` is complex differentiable in a neighborhood of `c`.
-/
open topological_space metric set filter asymptotics function
open_locale topological_space filter nnreal
universe u
variables {E : Type u} [normed_group E] [normed_space ℂ E] [complete_space E]
namespace complex
/-- **Removable singularity** theorem, weak version. If `f : ℂ → E` is differentiable in a punctured
neighborhood of a point and is continuous at this point, then it is analytic at this point. -/
lemma analytic_at_of_differentiable_on_punctured_nhds_of_continuous_at {f : ℂ → E} {c : ℂ}
(hd : ∀ᶠ z in 𝓝[≠] c, differentiable_at ℂ f z) (hc : continuous_at f c) :
analytic_at ℂ f c :=
begin
rcases (nhds_within_has_basis nhds_basis_closed_ball _).mem_iff.1 hd with ⟨R, hR0, hRs⟩,
lift R to ℝ≥0 using hR0.le,
replace hc : continuous_on f (closed_ball c R),
{ refine λ z hz, continuous_at.continuous_within_at _,
rcases eq_or_ne z c with rfl | hne,
exacts [hc, (hRs ⟨hz, hne⟩).continuous_at] },
exact (has_fpower_series_on_ball_of_differentiable_off_countable (countable_singleton c) hc
(λ z hz, hRs (diff_subset_diff_left ball_subset_closed_ball hz)) hR0).analytic_at
end
lemma differentiable_on_compl_singleton_and_continuous_at_iff {f : ℂ → E} {s : set ℂ} {c : ℂ}
(hs : s ∈ 𝓝 c) : differentiable_on ℂ f (s \ {c}) ∧ continuous_at f c ↔ differentiable_on ℂ f s :=
begin
refine ⟨_, λ hd, ⟨hd.mono (diff_subset _ _), (hd.differentiable_at hs).continuous_at⟩⟩,
rintro ⟨hd, hc⟩ x hx,
rcases eq_or_ne x c with rfl | hne,
{ refine (analytic_at_of_differentiable_on_punctured_nhds_of_continuous_at _ hc)
.differentiable_at.differentiable_within_at,
refine eventually_nhds_within_iff.2 ((eventually_mem_nhds.2 hs).mono $ λ z hz hzx, _),
exact hd.differentiable_at (inter_mem hz (is_open_ne.mem_nhds hzx)) },
{ simpa only [differentiable_within_at, has_fderiv_within_at, hne.nhds_within_diff_singleton]
using hd x ⟨hx, hne⟩ }
end
lemma differentiable_on_dslope {f : ℂ → E} {s : set ℂ} {c : ℂ} (hc : s ∈ 𝓝 c) :
differentiable_on ℂ (dslope f c) s ↔ differentiable_on ℂ f s :=
⟨λ h, h.of_dslope, λ h, (differentiable_on_compl_singleton_and_continuous_at_iff hc).mp $
⟨iff.mpr (differentiable_on_dslope_of_nmem $ λ h, h.2 rfl) (h.mono $ diff_subset _ _),
continuous_at_dslope_same.2 $ h.differentiable_at hc⟩⟩
/-- **Removable singularity** theorem: if `s` is a neighborhood of `c : ℂ`, a function `f : ℂ → E`
is complex differentiable on `s \ {c}`, and $f(z) - f(c)=o((z-c)^{-1})$, then `f` redefined to be
equal to `lim (𝓝[≠] c) f` at `c` is complex differentiable on `s`. -/
lemma differentiable_on_update_lim_of_is_o {f : ℂ → E} {s : set ℂ} {c : ℂ}
(hc : s ∈ 𝓝 c) (hd : differentiable_on ℂ f (s \ {c}))
(ho : (λ z, f z - f c) =o[𝓝[≠] c] (λ z, (z - c)⁻¹)) :
differentiable_on ℂ (update f c (lim (𝓝[≠] c) f)) s :=
begin
set F : ℂ → E := λ z, (z - c) • f z with hF,
suffices : differentiable_on ℂ F (s \ {c}) ∧ continuous_at F c,
{ rw [differentiable_on_compl_singleton_and_continuous_at_iff hc, ← differentiable_on_dslope hc,
dslope_sub_smul] at this; try { apply_instance },
have hc : tendsto f (𝓝[≠] c) (𝓝 (deriv F c)),
from continuous_at_update_same.mp (this.continuous_on.continuous_at hc),
rwa hc.lim_eq },
refine ⟨(differentiable_on_id.sub_const _).smul hd, _⟩,
rw ← continuous_within_at_compl_self,
have H := ho.tendsto_inv_smul_nhds_zero,
have H' : tendsto (λ z, (z - c) • f c) (𝓝[≠] c) (𝓝 (F c)),
from (continuous_within_at_id.tendsto.sub tendsto_const_nhds).smul tendsto_const_nhds,
simpa [← smul_add, continuous_within_at] using H.add H'
end
/-- **Removable singularity** theorem: if `s` is a punctured neighborhood of `c : ℂ`, a function
`f : ℂ → E` is complex differentiable on `s`, and $f(z) - f(c)=o((z-c)^{-1})$, then `f` redefined to
be equal to `lim (𝓝[≠] c) f` at `c` is complex differentiable on `{c} ∪ s`. -/
lemma differentiable_on_update_lim_insert_of_is_o {f : ℂ → E} {s : set ℂ} {c : ℂ}
(hc : s ∈ 𝓝[≠] c) (hd : differentiable_on ℂ f s)
(ho : (λ z, f z - f c) =o[𝓝[≠] c] (λ z, (z - c)⁻¹)) :
differentiable_on ℂ (update f c (lim (𝓝[≠] c) f)) (insert c s) :=
differentiable_on_update_lim_of_is_o (insert_mem_nhds_iff.2 hc)
(hd.mono $ λ z hz, hz.1.resolve_left hz.2) ho
/-- **Removable singularity** theorem: if `s` is a neighborhood of `c : ℂ`, a function `f : ℂ → E`
is complex differentiable and is bounded on `s \ {c}`, then `f` redefined to be equal to
`lim (𝓝[≠] c) f` at `c` is complex differentiable on `s`. -/
lemma differentiable_on_update_lim_of_bdd_above {f : ℂ → E} {s : set ℂ} {c : ℂ}
(hc : s ∈ 𝓝 c) (hd : differentiable_on ℂ f (s \ {c}))
(hb : bdd_above (norm ∘ f '' (s \ {c}))) :
differentiable_on ℂ (update f c (lim (𝓝[≠] c) f)) s :=
differentiable_on_update_lim_of_is_o hc hd $ is_bounded_under.is_o_sub_self_inv $
let ⟨C, hC⟩ := hb in ⟨C + ∥f c∥, eventually_map.2 $ mem_nhds_within_iff_exists_mem_nhds_inter.2
⟨s, hc, λ z hz, norm_sub_le_of_le (hC $ mem_image_of_mem _ hz) le_rfl⟩⟩
/-- **Removable singularity** theorem: if a function `f : ℂ → E` is complex differentiable on a
punctured neighborhood of `c` and $f(z) - f(c)=o((z-c)^{-1})$, then `f` has a limit at `c`. -/
lemma tendsto_lim_of_differentiable_on_punctured_nhds_of_is_o {f : ℂ → E} {c : ℂ}
(hd : ∀ᶠ z in 𝓝[≠] c, differentiable_at ℂ f z)
(ho : (λ z, f z - f c) =o[𝓝[≠] c] (λ z, (z - c)⁻¹)) :
tendsto f (𝓝[≠] c) (𝓝 $ lim (𝓝[≠] c) f) :=
begin
rw eventually_nhds_within_iff at hd,
have : differentiable_on ℂ f ({z | z ≠ c → differentiable_at ℂ f z} \ {c}),
from λ z hz, (hz.1 hz.2).differentiable_within_at,
have H := differentiable_on_update_lim_of_is_o hd this ho,
exact continuous_at_update_same.1 (H.differentiable_at hd).continuous_at
end
/-- **Removable singularity** theorem: if a function `f : ℂ → E` is complex differentiable and
bounded on a punctured neighborhood of `c`, then `f` has a limit at `c`. -/
lemma tendsto_lim_of_differentiable_on_punctured_nhds_of_bounded_under {f : ℂ → E}
{c : ℂ} (hd : ∀ᶠ z in 𝓝[≠] c, differentiable_at ℂ f z)
(hb : is_bounded_under (≤) (𝓝[≠] c) (λ z, ∥f z - f c∥)) :
tendsto f (𝓝[≠] c) (𝓝 $ lim (𝓝[≠] c) f) :=
tendsto_lim_of_differentiable_on_punctured_nhds_of_is_o hd hb.is_o_sub_self_inv
end complex
|
98d7b294621e42f802f1bfe1ecb3eab886dd1f7c | 08bd4ba4ca87dba1f09d2c96a26f5d65da81f4b4 | /src/Lean/Parser/Tactic.lean | 93ac937869c803d3397f02fd8350871f649c19c4 | [
"LLVM-exception",
"NCSA",
"LGPL-3.0-only",
"LicenseRef-scancode-inner-net-2.0",
"BSD-3-Clause",
"LGPL-2.0-or-later",
"Spencer-94",
"LGPL-2.1-or-later",
"HPND",
"LicenseRef-scancode-pcre",
"ISC",
"LGPL-2.1-only",
"Apache-2.0",
"LicenseRef-scancode-other-permissive",
"SunPro",
"CMU-Mach"... | permissive | gebner/lean4 | d51c4922640a52a6f7426536ea669ef18a1d9af5 | 8cd9ce06843c9d42d6d6dc43d3e81e3b49dfc20f | refs/heads/master | 1,685,732,780,391 | 1,672,962,627,000 | 1,673,459,398,000 | 373,307,283 | 0 | 0 | Apache-2.0 | 1,691,316,730,000 | 1,622,669,271,000 | Lean | UTF-8 | Lean | false | false | 3,090 | 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.Parser.Term
namespace Lean
namespace Parser
namespace Tactic
builtin_initialize
register_parser_alias tacticSeq
/- This is a fallback tactic parser for any identifier which exists only
to improve syntax error messages.
```
example : True := by foo -- unknown tactic
```
-/
@[builtin_tactic_parser] def «unknown» := leading_parser
withPosition (ident >> errorAtSavedPos "unknown tactic" true)
@[builtin_tactic_parser] def nestedTactic := tacticSeqBracketed
def matchRhs := Term.hole <|> Term.syntheticHole <|> tacticSeq
def matchAlts := Term.matchAlts (rhsParser := matchRhs)
/-- `match` performs case analysis on one or more expressions.
See [Induction and Recursion][tpil4].
The syntax for the `match` tactic is the same as term-mode `match`, except that
the match arms are tactics instead of expressions.
```
example (n : Nat) : n = n := by
match n with
| 0 => rfl
| i+1 => simp
```
[tpil4]: https://leanprover.github.io/theorem_proving_in_lean4/induction_and_recursion.html
-/
@[builtin_tactic_parser] def «match» := leading_parser:leadPrec
"match " >> optional Term.generalizingParam >>
optional Term.motive >> sepBy1 Term.matchDiscr ", " >>
" with " >> ppDedent matchAlts
/--
The tactic
```
intro
| pat1 => tac1
| pat2 => tac2
```
is the same as:
```
intro x
match x with
| pat1 => tac1
| pat2 => tac2
```
That is, `intro` can be followed by match arms and it introduces the values while
doing a pattern match. This is equivalent to `fun` with match arms in term mode.
-/
@[builtin_tactic_parser] def introMatch := leading_parser
nonReservedSymbol "intro " >> matchAlts
/-- `decide` will attempt to prove a goal of type `p` by synthesizing an instance
of `Decidable p` and then evaluating it to `isTrue ..`. Because this uses kernel
computation to evaluate the term, it may not work in the presence of definitions
by well founded recursion, since this requires reducing proofs.
```
example : 2 + 2 ≠ 5 := by decide
```
-/
@[builtin_tactic_parser] def decide := leading_parser
nonReservedSymbol "decide"
/-- `native_decide` will attempt to prove a goal of type `p` by synthesizing an instance
of `Decidable p` and then evaluating it to `isTrue ..`. Unlike `decide`, this
uses `#eval` to evaluate the decidability instance.
This should be used with care because it adds the entire lean compiler to the trusted
part, and the axiom `ofReduceBool` will show up in `#print axioms` for theorems using
this method or anything that transitively depends on them. Nevertheless, because it is
compiled, this can be significantly more efficient than using `decide`, and for very
large computations this is one way to run external programs and trust the result.
```
example : (List.range 1000).length = 1000 := by native_decide
```
-/
@[builtin_tactic_parser] def nativeDecide := leading_parser
nonReservedSymbol "native_decide"
end Tactic
end Parser
end Lean
|
dce5cddb2c221d3fedf02ac3f9e3797fecb6b047 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/ring_theory/witt_vector/isocrystal.lean | a64ba515eb81f64fa199e9a102cc87ec0896eded | [
"Apache-2.0"
] | permissive | Vierkantor/mathlib | 0ea59ac32a3a43c93c44d70f441c4ee810ccceca | 83bc3b9ce9b13910b57bda6b56222495ebd31c2f | refs/heads/master | 1,658,323,012,449 | 1,652,256,003,000 | 1,652,256,003,000 | 209,296,341 | 0 | 1 | Apache-2.0 | 1,568,807,655,000 | 1,568,807,655,000 | null | UTF-8 | Lean | false | false | 8,432 | lean | /-
Copyright (c) 2022 Heather Macbeth. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Heather Macbeth
-/
import ring_theory.witt_vector.frobenius_fraction_field
/-!
## F-isocrystals over a perfect field
When `k` is an integral domain, so is `𝕎 k`, and we can consider its field of fractions `K(p, k)`.
The endomorphism `witt_vector.frobenius` lifts to `φ : K(p, k) → K(p, k)`; if `k` is perfect, `φ` is
an automorphism.
Let `k` be a perfect integral domain. Let `V` be a vector space over `K(p,k)`.
An *isocrystal* is a bijective map `V → V` that is `φ`-semilinear.
A theorem of Dieudonné and Manin classifies the finite-dimensional isocrystals over algebraically
closed fields. In the one-dimensional case, this classification states that the isocrystal
structures are parametrized by their "slope" `m : ℤ`.
Any one-dimensional isocrystal is isomorphic to `φ(p^m • x) : K(p,k) → K(p,k)` for some `m`.
This file proves this one-dimensional case of the classification theorem.
The construction is described in Dupuis, Lewis, and Macbeth,
[Formalized functional analysis via semilinear maps][dupuis-lewis-macbeth2022].
## Main declarations
* `witt_vector.isocrystal`: a vector space over the field `K(p, k)` additionally equipped with a
Frobenius-linear automorphism.
* `witt_vector.isocrystal_classification`: a one-dimensional isocrystal admits an isomorphism to one
of the standard one-dimensional isocrystals.
## Notation
This file introduces notation in the locale `isocrystal`.
* `K(p, k)`: `fraction_ring (witt_vector p k)`
* `φ(p, k)`: `witt_vector.fraction_ring.frobenius_ring_hom p k`
* `M →ᶠˡ[p, k] M₂`: `linear_map (witt_vector.fraction_ring.frobenius_ring_hom p k) M M₂`
* `M ≃ᶠˡ[p, k] M₂`: `linear_equiv (witt_vector.fraction_ring.frobenius_ring_hom p k) M M₂`
* `Φ(p, k)`: `witt_vector.isocrystal.frobenius p k`
* `M →ᶠⁱ[p, k] M₂`: `witt_vector.isocrystal_hom p k M M₂`
* `M ≃ᶠⁱ[p, k] M₂`: `witt_vector.isocrystal_equiv p k M M₂`
## References
* [Formalized functional analysis via semilinear maps][dupuis-lewis-macbeth2022]
* [Theory of commutative formal groups over fields of finite characteristic][manin1963]
* <https://www.math.ias.edu/~lurie/205notes/Lecture26-Isocrystals.pdf>
-/
noncomputable theory
open finite_dimensional
namespace witt_vector
variables (p : ℕ) [fact p.prime]
variables (k : Type*) [comm_ring k]
localized "notation `K(` p`,` k`)` := fraction_ring (witt_vector p k)" in isocrystal
section perfect_ring
variables [is_domain k] [char_p k p] [perfect_ring k p]
/-! ### Frobenius-linear maps -/
/-- The Frobenius automorphism of `k` induces an automorphism of `K`. -/
def fraction_ring.frobenius : K(p, k) ≃+* K(p, k) :=
is_fraction_ring.field_equiv_of_ring_equiv (frobenius_equiv p k)
/-- The Frobenius automorphism of `k` induces an endomorphism of `K`. For notation purposes. -/
def fraction_ring.frobenius_ring_hom : K(p, k) →+* K(p, k) := fraction_ring.frobenius p k
localized "notation `φ(` p`,` k`)` := witt_vector.fraction_ring.frobenius_ring_hom p k"
in isocrystal
instance inv_pair₁ : ring_hom_inv_pair (φ(p, k)) _ :=
ring_hom_inv_pair.of_ring_equiv (fraction_ring.frobenius p k)
instance inv_pair₂ :
ring_hom_inv_pair ((fraction_ring.frobenius p k).symm : K(p, k) →+* K(p, k)) _ :=
ring_hom_inv_pair.of_ring_equiv (fraction_ring.frobenius p k).symm
localized "notation M ` →ᶠˡ[`:50 p `,` k `] ` M₂ :=
linear_map (witt_vector.fraction_ring.frobenius_ring_hom p k) M M₂" in isocrystal
localized "notation M ` ≃ᶠˡ[`:50 p `,` k `] ` M₂ :=
linear_equiv (witt_vector.fraction_ring.frobenius_ring_hom p k) M M₂" in isocrystal
/-! ### Isocrystals -/
/--
An isocrystal is a vector space over the field `K(p, k)` additionally equipped with a
Frobenius-linear automorphism.
-/
class isocrystal (V : Type*) [add_comm_group V] extends module K(p, k) V :=
( frob : V ≃ᶠˡ[p, k] V )
variables (V : Type*) [add_comm_group V] [isocrystal p k V]
variables (V₂ : Type*) [add_comm_group V₂] [isocrystal p k V₂]
variables {V}
/--
Project the Frobenius automorphism from an isocrystal. Denoted by `Φ(p, k)` when V can be inferred.
-/
def isocrystal.frobenius : V ≃ᶠˡ[p, k] V := @isocrystal.frob p _ k _ _ _ _ _ _ _
variables (V)
localized "notation `Φ(` p`,` k`)` := witt_vector.isocrystal.frobenius p k" in isocrystal
/-- A homomorphism between isocrystals respects the Frobenius map. -/
@[nolint has_inhabited_instance]
structure isocrystal_hom extends V →ₗ[K(p, k)] V₂ :=
( frob_equivariant : ∀ x : V, Φ(p, k) (to_linear_map x) = to_linear_map (Φ(p, k) x) )
/-- An isomorphism between isocrystals respects the Frobenius map. -/
@[nolint has_inhabited_instance]
structure isocrystal_equiv extends V ≃ₗ[K(p, k)] V₂ :=
( frob_equivariant : ∀ x : V, Φ(p, k) (to_linear_equiv x) = to_linear_equiv (Φ(p, k) x) )
localized "notation M ` →ᶠⁱ[`:50 p `,` k `] ` M₂ := witt_vector.isocrystal_hom p k M M₂"
in isocrystal
localized "notation M ` ≃ᶠⁱ[`:50 p `,` k `] ` M₂ := witt_vector.isocrystal_equiv p k M M₂"
in isocrystal
end perfect_ring
open_locale isocrystal
/-! ### Classification of isocrystals in dimension 1 -/
/-- A helper instance for type class inference. -/
local attribute [instance]
def fraction_ring.module : module K(p, k) K(p, k) := semiring.to_module
/--
Type synonym for `K(p, k)` to carry the standard 1-dimensional isocrystal structure
of slope `m : ℤ`.
-/
@[nolint unused_arguments has_inhabited_instance, derive [add_comm_group, module K(p, k)]]
def standard_one_dim_isocrystal (m : ℤ) : Type* :=
K(p, k)
section perfect_ring
variables [is_domain k] [char_p k p] [perfect_ring k p]
/-- The standard one-dimensional isocrystal of slope `m : ℤ` is an isocrystal. -/
instance (m : ℤ) : isocrystal p k (standard_one_dim_isocrystal p k m) :=
{ frob := (fraction_ring.frobenius p k).to_semilinear_equiv.trans
(linear_equiv.smul_of_ne_zero _ _ _ (zpow_ne_zero m (witt_vector.fraction_ring.p_nonzero p k))) }
@[simp] lemma standard_one_dim_isocrystal.frobenius_apply (m : ℤ)
(x : standard_one_dim_isocrystal p k m) :
Φ(p, k) x = (p:K(p, k)) ^ m • φ(p, k) x :=
rfl
end perfect_ring
/-- A one-dimensional isocrystal over an algebraically closed field
admits an isomorphism to one of the standard (indexed by `m : ℤ`) one-dimensional isocrystals. -/
theorem isocrystal_classification
(k : Type*) [field k] [is_alg_closed k] [char_p k p]
(V : Type*) [add_comm_group V] [isocrystal p k V]
(h_dim : finrank K(p, k) V = 1) :
∃ (m : ℤ), nonempty (standard_one_dim_isocrystal p k m ≃ᶠⁱ[p, k] V) :=
begin
haveI : nontrivial V := finite_dimensional.nontrivial_of_finrank_eq_succ h_dim,
obtain ⟨x, hx⟩ : ∃ x : V, x ≠ 0 := exists_ne 0,
have : Φ(p, k) x ≠ 0 := by simpa only [map_zero] using Φ(p,k).injective.ne hx,
obtain ⟨a, ha, hax⟩ : ∃ a : K(p, k), a ≠ 0 ∧ Φ(p, k) x = a • x,
{ rw finrank_eq_one_iff_of_nonzero' x hx at h_dim,
obtain ⟨a, ha⟩ := h_dim (Φ(p, k) x),
refine ⟨a, _, ha.symm⟩,
intros ha',
apply this,
simp only [←ha, ha', zero_smul] },
obtain ⟨b, hb, m, hmb⟩ := witt_vector.exists_frobenius_solution_fraction_ring p ha,
replace hmb : φ(p, k) b * a = p ^ m * b := by convert hmb,
use m,
let F₀ : standard_one_dim_isocrystal p k m →ₗ[K(p,k)] V :=
linear_map.to_span_singleton K(p, k) V x,
let F : standard_one_dim_isocrystal p k m ≃ₗ[K(p,k)] V,
{ refine linear_equiv.of_bijective F₀ _ _,
{ rw ← linear_map.ker_eq_bot,
exact linear_map.ker_to_span_singleton K(p, k) V hx },
{ rw ← linear_map.range_eq_top,
rw ← (finrank_eq_one_iff_of_nonzero x hx).mp h_dim,
rw linear_map.span_singleton_eq_range } },
refine ⟨⟨(linear_equiv.smul_of_ne_zero K(p, k) _ _ hb).trans F, _⟩⟩,
intros c,
rw [linear_equiv.trans_apply, linear_equiv.trans_apply,
linear_equiv.smul_of_ne_zero_apply, linear_equiv.smul_of_ne_zero_apply,
linear_equiv.map_smul, linear_equiv.map_smul],
simp only [hax, linear_equiv.of_bijective_apply, linear_map.to_span_singleton_apply,
linear_equiv.map_smulₛₗ, standard_one_dim_isocrystal.frobenius_apply, algebra.id.smul_eq_mul],
simp only [←mul_smul],
congr' 1,
linear_combination (hmb, φ(p,k) c),
end
end witt_vector
|
e1a2dcabb37dc31ee555f480f1193058dfca54cf | 4d2583807a5ac6caaffd3d7a5f646d61ca85d532 | /src/measure_theory/group/prod.lean | 3f81d54b9d0bc8cce7553f280ee499e7811ab1ac | [
"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 | 11,240 | lean | /-
Copyright (c) 2021 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris van Doorn
-/
import measure_theory.constructions.prod
import measure_theory.group.basic
/-!
# Measure theory in the product of groups
In this file we show properties about measure theory in products of topological groups
and properties of iterated integrals in topological groups.
These lemmas show the uniqueness of left invariant measures on locally compact groups, up to
scaling. In this file we follow the proof and refer to the book *Measure Theory* by Paul Halmos.
The idea of the proof is to use the translation invariance of measures to prove `μ(F) = c * μ(E)`
for two sets `E` and `F`, where `c` is a constant that does not depend on `μ`. Let `e` and `f` be
the characteristic functions of `E` and `F`.
Assume that `μ` and `ν` are left-invariant measures. Then the map `(x, y) ↦ (y * x, x⁻¹)`
preserves the measure `μ.prod ν`, which means that
```
∫ x, ∫ y, h x y ∂ν ∂μ = ∫ x, ∫ y, h (y * x) x⁻¹ ∂ν ∂μ
```
If we apply this to `h x y := e x * f y⁻¹ / ν ((λ h, h * y⁻¹) ⁻¹' E)`, we can rewrite the RHS to
`μ(F)`, and the LHS to `c * μ(E)`, where `c = c(ν)` does not depend on `μ`.
Applying this to `μ` and to `ν` gives `μ (F) / μ (E) = ν (F) / ν (E)`, which is the uniqueness up to
scalar multiplication.
The proof in [Halmos] seems to contain an omission in §60 Th. A, see
`measure_theory.measure_lintegral_div_measure` and
https://math.stackexchange.com/questions/3974485/does-right-translation-preserve-finiteness-for-a-left-invariant-measure
-/
noncomputable theory
open topological_space set (hiding prod_eq) function
open_locale classical ennreal pointwise
namespace measure_theory
open measure
variables {G : Type*} [topological_space G] [measurable_space G] [second_countable_topology G]
variables [borel_space G] [group G] [topological_group G]
variables {μ ν : measure G} [sigma_finite ν] [sigma_finite μ]
/-- This condition is part of the definition of a measurable group in [Halmos, §59].
There, the map in this lemma is called `S`. -/
@[to_additive map_prod_sum_eq]
lemma map_prod_mul_eq (hν : is_mul_left_invariant ν) :
map (λ z : G × G, (z.1, z.1 * z.2)) (μ.prod ν) = μ.prod ν :=
begin
refine (prod_eq _).symm, intros s t hs ht,
simp_rw [map_apply (measurable_fst.prod_mk (measurable_fst.mul measurable_snd)) (hs.prod ht),
prod_apply ((measurable_fst.prod_mk (measurable_fst.mul measurable_snd)) (hs.prod ht)),
preimage_preimage],
conv_lhs { congr, skip, funext, rw [mk_preimage_prod_right_fn_eq_if ((*) x), measure_if] },
simp_rw [hν _ ht, lintegral_indicator _ hs, set_lintegral_const, mul_comm]
end
/-- The function we are mapping along is `SR` in [Halmos, §59],
where `S` is the map in `map_prod_mul_eq` and `R` is `prod.swap`. -/
@[to_additive map_prod_add_eq_swap]
lemma map_prod_mul_eq_swap (hμ : is_mul_left_invariant μ) :
map (λ z : G × G, (z.2, z.2 * z.1)) (μ.prod ν) = ν.prod μ :=
begin
rw [← prod_swap],
simp_rw [map_map (measurable_snd.prod_mk (measurable_snd.mul measurable_fst)) measurable_swap],
exact map_prod_mul_eq hμ
end
/-- The function we are mapping along is `S⁻¹` in [Halmos, §59],
where `S` is the map in `map_prod_mul_eq`. -/
@[to_additive map_prod_neg_add_eq]
lemma map_prod_inv_mul_eq (hν : is_mul_left_invariant ν) :
map (λ z : G × G, (z.1, z.1⁻¹ * z.2)) (μ.prod ν) = μ.prod ν :=
(homeomorph.shear_mul_right G).to_measurable_equiv.map_apply_eq_iff_map_symm_apply_eq.mp $
map_prod_mul_eq hν
/-- The function we are mapping along is `S⁻¹R` in [Halmos, §59],
where `S` is the map in `map_prod_mul_eq` and `R` is `prod.swap`. -/
@[to_additive map_prod_neg_add_eq_swap]
lemma map_prod_inv_mul_eq_swap (hμ : is_mul_left_invariant μ) :
map (λ z : G × G, (z.2, z.2⁻¹ * z.1)) (μ.prod ν) = ν.prod μ :=
begin
rw [← prod_swap],
simp_rw
[map_map (measurable_snd.prod_mk $ measurable_snd.inv.mul measurable_fst) measurable_swap],
exact map_prod_inv_mul_eq hμ
end
/-- The function we are mapping along is `S⁻¹RSR` in [Halmos, §59],
where `S` is the map in `map_prod_mul_eq` and `R` is `prod.swap`. -/
@[to_additive map_prod_add_neg_eq]
lemma map_prod_mul_inv_eq (hμ : is_mul_left_invariant μ) (hν : is_mul_left_invariant ν) :
map (λ z : G × G, (z.2 * z.1, z.1⁻¹)) (μ.prod ν) = μ.prod ν :=
begin
let S := (homeomorph.shear_mul_right G).to_measurable_equiv,
suffices : map ((λ z : G × G, (z.2, z.2⁻¹ * z.1)) ∘ (λ z : G × G, (z.2, z.2 * z.1))) (μ.prod ν) =
μ.prod ν,
{ convert this, ext1 ⟨x, y⟩, simp },
simp_rw [← map_map (measurable_snd.prod_mk (measurable_snd.inv.mul measurable_fst))
(measurable_snd.prod_mk (measurable_snd.mul measurable_fst)), map_prod_mul_eq_swap hμ,
map_prod_inv_mul_eq_swap hν]
end
@[to_additive] lemma quasi_measure_preserving_inv (hμ : is_mul_left_invariant μ) :
quasi_measure_preserving (has_inv.inv : G → G) μ μ :=
begin
refine ⟨measurable_inv, absolutely_continuous.mk $ λ s hsm hμs, _⟩,
rw [map_apply measurable_inv hsm, inv_preimage],
have hf : measurable (λ z : G × G, (z.2 * z.1, z.1⁻¹)) :=
(measurable_snd.mul measurable_fst).prod_mk measurable_fst.inv,
suffices : map (λ z : G × G, (z.2 * z.1, z.1⁻¹)) (μ.prod μ) ((s⁻¹).prod s⁻¹) = 0,
{ simpa only [map_prod_mul_inv_eq hμ hμ, prod_prod, mul_eq_zero, or_self] using this },
have hsm' : measurable_set (s⁻¹.prod s⁻¹) := hsm.inv.prod hsm.inv,
simp_rw [map_apply hf hsm', prod_apply_symm (hf hsm'), preimage_preimage, mk_preimage_prod,
inv_preimage, set.inv_inv, measure_mono_null (inter_subset_right _ _) hμs, lintegral_zero]
end
@[to_additive]
lemma measure_inv_null (hμ : is_mul_left_invariant μ) {E : set G} :
μ ((λ x, x⁻¹) ⁻¹' E) = 0 ↔ μ E = 0 :=
begin
refine ⟨λ hE, _, (quasi_measure_preserving_inv hμ).preimage_null⟩,
convert (quasi_measure_preserving_inv hμ).preimage_null hE,
exact set.inv_inv.symm
end
@[to_additive]
lemma measurable_measure_mul_right {E : set G} (hE : measurable_set E) :
measurable (λ x, μ ((λ y, y * x) ⁻¹' E)) :=
begin
suffices :
measurable (λ y, μ ((λ x, (x, y)) ⁻¹' ((λ z : G × G, (1, z.1 * z.2)) ⁻¹' set.prod univ E))),
{ convert this, ext1 x, congr' 1 with y : 1, simp },
apply measurable_measure_prod_mk_right,
exact measurable_const.prod_mk (measurable_fst.mul measurable_snd) (measurable_set.univ.prod hE)
end
@[to_additive]
lemma lintegral_lintegral_mul_inv (hμ : is_mul_left_invariant μ) (hν : is_mul_left_invariant ν)
(f : G → G → ℝ≥0∞) (hf : ae_measurable (uncurry f) (μ.prod ν)) :
∫⁻ x, ∫⁻ y, f (y * x) x⁻¹ ∂ν ∂μ = ∫⁻ x, ∫⁻ y, f x y ∂ν ∂μ :=
begin
have h : measurable (λ z : G × G, (z.2 * z.1, z.1⁻¹)) :=
(measurable_snd.mul measurable_fst).prod_mk measurable_fst.inv,
have h2f : ae_measurable (uncurry $ λ x y, f (y * x) x⁻¹) (μ.prod ν),
{ apply hf.comp_measurable' h (map_prod_mul_inv_eq hμ hν).absolutely_continuous },
simp_rw [lintegral_lintegral h2f, lintegral_lintegral hf],
conv_rhs { rw [← map_prod_mul_inv_eq hμ hν] },
symmetry,
exact lintegral_map' (hf.mono' (map_prod_mul_inv_eq hμ hν).absolutely_continuous) h,
end
@[to_additive]
lemma measure_mul_right_null (hμ : is_mul_left_invariant μ) {E : set G} (y : G) :
μ ((λ x, x * y) ⁻¹' E) = 0 ↔ μ E = 0 :=
calc μ ((λ x, x * y) ⁻¹' E) = 0 ↔ μ (has_inv.inv ⁻¹' ((λ x, y⁻¹ * x) ⁻¹' (has_inv.inv ⁻¹' E))) = 0 :
by simp only [preimage_preimage, mul_inv_rev, inv_inv]
... ↔ μ E = 0 : by simp only [measure_inv_null hμ, hμ.measure_preimage_mul]
@[to_additive]
lemma measure_mul_right_ne_zero (hμ : is_mul_left_invariant μ) {E : set G}
(h2E : μ E ≠ 0) (y : G) : μ ((λ x, x * y) ⁻¹' E) ≠ 0 :=
(not_iff_not_of_iff (measure_mul_right_null hμ y)).mpr h2E
/-- A technical lemma relating two different measures. This is basically [Halmos, §60 Th. A].
Note that if `f` is the characteristic function of a measurable set `F` this states that
`μ F = c * μ E` for a constant `c` that does not depend on `μ`.
There seems to be a gap in the last step of the proof in [Halmos].
In the last line, the equality `g(x⁻¹)ν(Ex⁻¹) = f(x)` holds if we can prove that
`0 < ν(Ex⁻¹) < ∞`. The first inequality follows from §59, Th. D, but I couldn't find the second
inequality. For this reason, we use a compact `E` instead of a measurable `E` as in [Halmos], and
additionally assume that `ν` is a regular measure (we only need that it is finite on compact
sets). -/
@[to_additive]
lemma measure_lintegral_div_measure [t2_space G] (hμ : is_mul_left_invariant μ)
(hν : is_mul_left_invariant ν) [regular ν] {E : set G} (hE : is_compact E) (h2E : ν E ≠ 0)
(f : G → ℝ≥0∞) (hf : measurable f) :
μ E * ∫⁻ y, f y⁻¹ / ν ((λ h, h * y⁻¹) ⁻¹' E) ∂ν = ∫⁻ x, f x ∂μ :=
begin
have Em := hE.measurable_set,
symmetry,
set g := λ y, f y⁻¹ / ν ((λ h, h * y⁻¹) ⁻¹' E),
have hg : measurable g := (hf.comp measurable_inv).div
((measurable_measure_mul_right Em).comp measurable_inv),
rw [← set_lintegral_one, ← lintegral_indicator _ Em,
← lintegral_lintegral_mul (measurable_const.indicator Em).ae_measurable hg.ae_measurable,
← lintegral_lintegral_mul_inv hμ hν],
swap, { exact (((measurable_const.indicator Em).comp measurable_fst).mul
(hg.comp measurable_snd)).ae_measurable },
have mE : ∀ x : G, measurable (λ y, ((λ z, z * x) ⁻¹' E).indicator (λ z, (1 : ℝ≥0∞)) y) :=
λ x, measurable_const.indicator (measurable_mul_const _ Em),
have : ∀ x y, E.indicator (λ (z : G), (1 : ℝ≥0∞)) (y * x) =
((λ z, z * x) ⁻¹' E).indicator (λ (b : G), 1) y,
{ intros x y, symmetry, convert indicator_comp_right (λ y, y * x), ext1 z, refl },
have h3E : ∀ y, ν ((λ x, x * y) ⁻¹' E) ≠ ∞ :=
λ y, (regular.lt_top_of_is_compact $ (homeomorph.mul_right _).compact_preimage.mpr hE).ne,
simp_rw [this, lintegral_mul_const _ (mE _), lintegral_indicator _ (measurable_mul_const _ Em),
set_lintegral_one, g, inv_inv,
ennreal.mul_div_cancel' (measure_mul_right_ne_zero hν h2E _) (h3E _)]
end
/-- This is roughly the uniqueness (up to a scalar) of left invariant Borel measures on a second
countable locally compact group. The uniqueness of Haar measure is proven from this in
`measure_theory.measure.haar_measure_unique` -/
@[to_additive]
lemma measure_mul_measure_eq [t2_space G] (hμ : is_mul_left_invariant μ)
(hν : is_mul_left_invariant ν) [regular ν] {E F : set G}
(hE : is_compact E) (hF : measurable_set F) (h2E : ν E ≠ 0) : μ E * ν F = ν E * μ F :=
begin
have h1 := measure_lintegral_div_measure hν hν hE h2E (F.indicator (λ x, 1))
(measurable_const.indicator hF),
have h2 := measure_lintegral_div_measure hμ hν hE h2E (F.indicator (λ x, 1))
(measurable_const.indicator hF),
rw [lintegral_indicator _ hF, set_lintegral_one] at h1 h2,
rw [← h1, mul_left_comm, h2],
end
end measure_theory
|
fba58c4be080bd6a703d4ba7f1ce2971ec070dde | 8c02fed42525b65813b55c064afe2484758d6d09 | /src/irsem.lean | d58be8c58d6af89aa16ef7c02ab3dc3e23816830 | [
"LicenseRef-scancode-generic-cla",
"MIT"
] | permissive | microsoft/AliveInLean | 3eac351a34154efedd3ffc4fe2fa4ec01b219e0d | 4b739dd6e4266b26a045613849df221374119871 | refs/heads/master | 1,691,419,737,939 | 1,689,365,567,000 | 1,689,365,568,000 | 131,156,103 | 23 | 18 | NOASSERTION | 1,660,342,040,000 | 1,524,747,538,000 | Lean | UTF-8 | Lean | false | false | 12,728 | lean | -- Copyright (c) Microsoft Corporation. All rights reserved.
-- Licensed under the MIT license.
/-
Semantics of LLVM IR
-/
import .lang
import .common
import .ops
import system.io
structure irsem :=
(intty:size → Type)
(poisonty:Type)
-- This is for UB as well
(boolty:Type)
namespace irsem
section
open io
parameter (sem : irsem)
-- uint_like types
variable [ihc:has_comp sem.intty sem.boolty]
variable [ioc:has_overflow_check sem.intty sem.boolty]
variable [iul:uint_like sem.intty]
-- poison types
variable [pbl:bool_like sem.poisonty]
variable [p2b:has_coe sem.poisonty sem.boolty]
-- bool types
variable [bbl:bool_like sem.boolty]
variable [b2i:has_coe sem.boolty (sem.intty size.one)]
variable [bhi:Π (sz:size), has_ite sem.boolty (sem.intty sz)]
variable [bhi2:has_ite sem.boolty sem.poisonty]
inductive valty
| ival: Π (sz:size), sem.intty sz → sem.poisonty → valty
-- A register file.
def regfile := list (string × valty)
def regfile.get (r:regfile) (s:string) : option valty :=
match (list.filter (λ (itm:string × valty), s = itm.fst) r) with
| (name,val)::_ := some val
| nil := none
end
def regfile.update (r:regfile) (s:string) (v:valty) :=
(s,v)::r
def regfile.empty : regfile :=
[]
def regfile.apply_to_values (r:regfile) (f:valty → valty): regfile :=
list.map (λ x:(string × valty), (x.1, f x.2)) r
def regfile.regnames (r:regfile) : list string :=
list.map (λ x:(string × valty), x.1) r
def regfile.to_string' [has_to_string valty] (rs:regfile) : string :=
list.foldr
(λ (itm:string × valty) (befst:string),
befst ++ (itm.1) ++ ":" ++ (to_string itm.2) ++ ",\n")
"" rs
include ihc
include ioc
include iul
include bbl
include pbl
include p2b
include b2i
include bhi
include bhi2
-- Current state of execution.
-- 'state' is already defined, Lean says.
def irstate := sem.boolty × regfile
def irstate.empty : irstate :=
(bbl.tt, regfile.empty)
section
variable (s:irstate)
def irstate.getreg (name:string) : option valty :=
regfile.get s.2 name
def irstate.regnames : list string :=
regfile.regnames s.2
def irstate.updatereg (name:string) (v:valty)
: irstate :=
(s.1, regfile.update s.2 name v)
def irstate.updateub (u:sem.boolty) : irstate :=
(s.1 & u, s.2)
def irstate.setub (u:sem.boolty) : irstate :=
(u, s.2)
def irstate.getub : sem.boolty :=
s.1
def irstate.apply_to_values (f:valty → valty): irstate :=
(s.1, regfile.apply_to_values s.2 f)
def irstate.to_string' [has_to_string valty] [has_to_string sem.poisonty] [has_to_string sem.boolty]
: string :=
"(Is not UB?: " ++ to_string(s.1) ++ ",\n" ++ regfile.to_string' s.2 ++ ")"
end
-- Get current value from syntactic val (either register or constant).
-- For example, if val is a register "x", get_value loads "x" and returns
-- the value.
def get_value (s:irstate) (v:operand) (t:ty): option valty :=
match v with
| operand.reg (reg.r name) := (irstate.getreg s name)
| operand.const (const.int z) :=
match t with
| ty.int bitsz :=
if h:(0 < bitsz) then
let sz := (⟨bitsz, h⟩:size),
intgen := @uint_like.from_z sem.intty iul sz,
nopoison := pbl.tt in
if h0:(within_signed_range sz z) then
-- e.g. -128 ≤ z ≤ 127 (if bitsz is 8)
some (valty.ival sz (intgen z) nopoison)
else if h1:(↑(int_min_nat sz) ≤ z ∧ z ≤ (all_one_nat sz)) then
-- e.g. 128 ≤ z ≤ 255 (if bitsz is 8)
-- Convert into the corresponding negative number.
some (valty.ival sz (intgen (z - int.of_nat (2 ^ bitsz))) nopoison)
else
none -- the constant does not fit in bitsize!
else
none -- bitsize should be always larger than 0!
| ty.arbitrary_int := none -- arbitrary_int should not appear at execution time.
end
end
-- Cast bool to poison
@[reducible]
def b2p (b:sem.boolty) : sem.poisonty :=
has_ite.ite b (pbl.tt) (pbl.ff)
-- If v1 ≠ v2, yields poison
@[reducible]
def neq2p {sz:size} (v1 v2:sem.intty sz) : sem.poisonty :=
b2p (v1 =_{sem.boolty} v2)
-- If v1 = v2, yields poison
@[reducible]
def eq2p {sz:size} (v1 v2:sem.intty sz) : sem.poisonty :=
b2p (v1 ≠_{sem.boolty} v2)
-- Performs given binary operation.
def bop_val (sz:size) : bopcode → sem.intty sz → sem.intty sz → sem.intty sz
| bopcode.add n1 n2 := n1 + n2
| bopcode.sub n1 n2 := n1 - n2
| bopcode.mul n1 n2 := n1 * n2
| bopcode.udiv n1 n2 := n1 /u n2
| bopcode.urem n1 n2 := n1 %u n2
| bopcode.sdiv n1 n2 := n1 / n2
| bopcode.srem n1 n2 := n1 % n2
| bopcode.and n1 n2 := n1 & n2
| bopcode.or n1 n2 := n1 |b n2
| bopcode.xor n1 n2 := n1 ^b n2
| bopcode.shl n1 n2 := n1 << n2
| bopcode.lshr n1 n2 := n1 >>l n2
| bopcode.ashr n1 n2 := n1 >>a n2
-- Returns true if it fits into the range of signed/unsigned integer
def bop_overflow_check (nsw:bool) (sz:size)
(bc:bopcode) (op1:sem.intty sz) (op2:sem.intty sz)
: sem.poisonty :=
b2p (match bc with
| bopcode.add := has_overflow_check.add_chk sem.boolty nsw op1 op2
| bopcode.sub := has_overflow_check.sub_chk sem.boolty nsw op1 op2
| bopcode.mul := has_overflow_check.mul_chk sem.boolty nsw op1 op2
| bopcode.shl := has_overflow_check.shl_chk sem.boolty nsw op1 op2
-- If the instruction is well-typed, it is unreachable here.
| _ := has_overflow_check.mul_chk sem.boolty nsw op1 op2
end)
-- Returns true if it fits into the range of signed/unsigned integer
def bop_exact_check (sz:size) (bc:bopcode) (op1:sem.intty sz) (op2:sem.intty sz)
: sem.poisonty :=
let zero:sem.intty sz := uint_like.zero sz,
sz':sem.intty sz := uint_like.from_z sz sz,
op2' := op2 %u sz' in
match bc with
| bopcode.sdiv := eq2p (op1 % op2) zero
| bopcode.srem := eq2p (op1 % op2) zero
| bopcode.udiv := eq2p (op1 %u op2) zero
| bopcode.urem := eq2p (op1 %u op2) zero
| bopcode.ashr := eq2p ((op1 >>a op2') << op2') op1
| bopcode.lshr := eq2p ((op1 >>l op2') << op2') op1
| _ := eq2p op1 op2 -- Unreachable
end
-- Checks whether the binary operation returns poison.
def bop_poison_flag (sz:size) (code:bopcode) (flag:bopflag) (v1:sem.intty sz) (v2:sem.intty sz)
: sem.poisonty :=
~ (match flag with
| bopflag.nsw := bop_overflow_check tt sz code v1 v2
| bopflag.nuw := bop_overflow_check ff sz code v1 v2
| bopflag.exact := bop_exact_check sz code v1 v2
end)
def bop_poison (sz:size) (code:bopcode) (v1:sem.intty sz) (v2:sem.intty sz)
: sem.poisonty :=
let sz':sem.intty sz := uint_like.from_z sz sz,
res := match code with
| bopcode.shl := v2 <u_{sem.boolty} sz'
| bopcode.ashr := v2 <u_{sem.boolty} sz'
| bopcode.lshr := v2 <u_{sem.boolty} sz'
| _ := bbl.tt
end in
b2p res
-- Checks whether the binary operation yields UB.
def bop_ub (sz:size) (code:bopcode)
(op1:sem.intty sz) (op1p:sem.poisonty) (op2:sem.intty sz) (op2p:sem.poisonty)
: sem.boolty :=
if bop_isdiv code = tt then
let zero:sem.intty sz := uint_like.zero sz,
allone:sem.intty sz := uint_like.allone sz,
intmin:sem.intty sz := uint_like.signonly sz in
let cmp := (eq2p op2 zero) in
let signeddiv :=
if code = bopcode.sdiv ∨ code = bopcode.srem then
(op1p & (eq2p op1 intmin)) |b (eq2p op2 allone)
else pbl.tt in
op2p & cmp & signeddiv
else bbl.tt
def bop_poison_all (sz:size) (code:bopcode) (flags:list bopflag)
(op1:sem.intty sz) (op2:sem.intty sz) : sem.poisonty :=
list.foldr (λ flag p1, p1 & (bop_poison_flag sz code flag op1 op2))
(bop_poison sz code op1 op2) flags
-- Semantics of binary operator instruction.
def bop (sz:size) (code:bopcode) (flags:list bopflag)
(op1:sem.intty sz) (op1p:sem.poisonty)
(op2:sem.intty sz) (op2p:sem.poisonty)
: (sem.boolty × (sem.intty sz × sem.poisonty)) :=
-- 'cast' is needed to cast 'vector sz2' type into 'vector sz1'.
let res := bop_val sz code op1 op2,
op_poison := op1p & op2p,
poison := (bop_poison_all sz code flags op1 op2) & op_poison,
ub := bop_ub sz code op1 op1p op2 op2p in
(ub, (res, poison))
-- Semantics of cast instruction.
def castop (fromsz:size) (code:uopcode)
(op1:sem.intty fromsz) (op1p:sem.poisonty) (tosz:size)
: (sem.intty tosz × sem.poisonty) :=
if fromsz.val < tosz.val then
(match code with
| uopcode.zext := uint_like.zext fromsz tosz op1
| uopcode.sext := uint_like.sext fromsz tosz op1
| _ := uint_like.sext fromsz tosz op1 -- Unreachable
end, op1p)
else if fromsz.val = tosz.val then
(uint_like.zero tosz, op1p) -- unreacahble
else
(uint_like.trunc fromsz tosz op1, op1p)
-- Semantics of icmp instruction.
def icmpop (sz:size) (cond:icmpcond) (a:sem.intty sz) (ap:sem.poisonty)
(b:sem.intty sz) (bp:sem.poisonty)
: (sem.intty (size.one) × sem.poisonty) :=
let res := match cond with
| icmpcond.eq := a =_{sem.boolty} b
| icmpcond.ne := a ≠_{sem.boolty} b
| icmpcond.ugt := a >u_{sem.boolty} b
| icmpcond.uge := a ≥u_{sem.boolty} b
| icmpcond.ult := a <u_{sem.boolty} b
| icmpcond.ule := a ≤u_{sem.boolty} b
| icmpcond.sgt := a >_{sem.boolty} b
| icmpcond.sge := a ≥_{sem.boolty} b
| icmpcond.slt := a <_{sem.boolty} b
| icmpcond.sle := a ≤_{sem.boolty} b
end in
(res, ap & bp)
-- Semantics of select instruction.
def selectop (cond:sem.intty size.one) (pcond:sem.poisonty) (sz:size)
(a:sem.intty sz) (ap:sem.poisonty) (b:sem.intty sz) (bp:sem.poisonty)
: (sem.intty sz × sem.poisonty) :=
let one:sem.intty _ := uint_like.from_z size.one 1,
eqtest := cond =_{sem.boolty} one in
let c := @has_ite.ite sem.boolty (sem.intty sz) (bhi sz) eqtest a b in
let cp := has_ite.ite eqtest ap bp in
(c, pcond & cp)
/-
- Execution of a single instruction
-/
def to_ival {sz:size} (xp:sem.intty sz × sem.poisonty ) :=
valty.ival sz xp.1 xp.2
-- Execution of bop, but it fails if type check fails
def step_bop (v1 v2:valty) (code:bopcode)
(bflags:list bopflag) (s1:irstate) (lhsn:string) : option irstate :=
match v1,v2 with
| (valty.ival sz1 n1 p1), (valty.ival sz2 n2 p2) :=
if H:sz1 = sz2 then do
let (u3, x) := (bop sz1 code bflags n1 p1 (cast (by rw H) n2) p2) in
some (irstate.updatereg (irstate.updateub s1 u3)
lhsn (to_ival x))
else none
end
-- Execution of unaryop, but it fails if type check fails
def step_unaryop (v1:valty) (code:uopcode)
(toisz:nat) (s1:irstate) (lhsn:string) : option irstate :=
match v1, code with
| _, uopcode.freeze := none -- Not implemented yet
| (valty.ival sz1 n1 p1), _ :=
if H:toisz > 0 then
let tosz := subtype.mk toisz H in
some (irstate.updatereg s1 lhsn (to_ival (castop sz1 code n1 p1 tosz)))
else -- isz cannot be 0 size!
none
end
-- Execution of bop, but it fails if type check fails
def step_icmpop (v1 v2:valty) (cond:icmpcond)
(s1:irstate) (lhsn:string) : option irstate :=
match v1,v2 with
| (valty.ival sz1 n1 p1), (valty.ival sz2 n2 p2) :=
if H:sz1 = sz2 then
some (irstate.updatereg s1 lhsn
(to_ival (icmpop sz1 cond n1 p1 (cast (by rw H) n2) p2)))
else
none
end
-- Execution of bop, but it fails if type check fails
def step_selectop (vcond v1 v2:valty)
(s1:irstate) (lhsn:string) : option irstate :=
match vcond,v1,v2 with
| (valty.ival szcond ncond pcond),
(valty.ival sz1 n1 p1), (valty.ival sz2 n2 p2) :=
if Hc:szcond = size.one then
if H:sz1 = sz2 then
some (irstate.updatereg s1 lhsn (to_ival
(selectop (cast (by rw Hc) ncond) pcond
sz1 n1 p1 (cast (by rw H) n2) p2 )))
else none
else none
end
def step (s1:irstate) (i:instruction) : option irstate :=
match i with
| instruction.binop retty (reg.r lhsn) code bflags op1 op2 :=
do
v1 ← get_value s1 op1 retty,
v2 ← get_value s1 op2 retty,
step_bop v1 v2 code bflags s1 lhsn
| instruction.unaryop (reg.r lhsn) code fromty op1 toty@(ty.int toisz):=
do
v1 ← get_value s1 op1 fromty,
step_unaryop v1 code toisz s1 lhsn
| instruction.icmpop opty (reg.r lhsn) cond op1 op2 :=
do
v1 ← get_value s1 op1 opty,
v2 ← get_value s1 op2 opty,
step_icmpop v1 v2 cond s1 lhsn
| instruction.selectop (reg.r lhsn) condty cond vty op1 op2 :=
do
vcond ← get_value s1 cond condty,
v1 ← get_value s1 op1 vty,
v2 ← get_value s1 op2 vty,
step_selectop vcond v1 v2 s1 lhsn
| _ := none -- Stuck!
end
-- Execution of a program
def bigstep (inits:irstate) (p:program): option irstate :=
list.foldl (λ befst inst,
match befst with
| none := none -- Stuck!
| some st := step st inst
end) (some inits) (p.insts)
end
end irsem
|
7812a8193452894de3fea38409c08aec7543a035 | 500f65bb93c499cd35c3254d894d762208cae042 | /src/analysis/calculus/deriv.lean | 5049ab85f9bbee9f4343883b7b505956c75ca51d | [
"Apache-2.0"
] | permissive | PatrickMassot/mathlib | c39dc0ff18bbde42f1c93a1642f6e429adad538c | 45df75b3c9da159fe3192fa7f769dfbec0bd6bda | refs/heads/master | 1,623,168,646,390 | 1,566,940,765,000 | 1,566,940,765,000 | 115,220,590 | 0 | 1 | null | 1,514,061,524,000 | 1,514,061,524,000 | null | UTF-8 | Lean | false | false | 46,807 | lean | /-
Copyright (c) 2019 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad, Sébastien Gouëzel
The Fréchet derivative.
Let `E` and `F` be normed spaces, `f : E → F`, and `f' : E →L[k] F` a
continuous k-linear map, where `k` is a non-discrete normed field. Then
`has_fderiv_within_at f f' s x`
says that `f` has derivative `f'` at `x`, where the domain of interest
is restricted to `s`. We also have
`has_fderiv_at f f' x := has_fderiv_within_at f f' x univ`
The derivative is defined in terms of the `is_o` relation, but also
characterized in terms of the `tendsto` relation.
We also introduce predicates `differentiable_within_at k f s x` (where `k` is the base field,
`f` the function to be differentiated, `x` the point at which the derivative is asserted to exist,
and `s` the set along which the derivative is defined), as well as `differentiable_at k f x`,
`differentiable_on k f s` and `differentiable k f` to express the existence of a derivative.
To be able to compute with derivatives, we write `fderiv_within k f s x` and `fderiv k f x`
for some choice of a derivative if it exists, and the zero function otherwise. This choice only
behaves well along sets for which the derivative is unique, i.e., those for which the tangent
directions span a dense subset of the whole space. The predicates `unique_diff_within_at s x` and
`unique_diff_on s`, defined in `tangent_cone.lean` express this property. We prove that indeed
they imply the uniqueness of the derivative. This is satisfied for open subsets, and in particular
for `univ`. This uniqueness only holds when the field is non-discrete, which we request at the very
beginning: otherwise, a derivative can be defined, but it has no interesting properties whatsoever.
In addition to the definition and basic properties of the derivative, this file contains the
usual formulas (and existence assertions) for the derivative of
* constants
* the identity
* bounded linear maps
* bounded bilinear maps
* sum of two functions
* multiplication of a function by a scalar constant
* negative of a function
* subtraction of two functions
* multiplication of a function by a scalar function
* multiplication of two scalar functions
* composition of functions (the chain rule)
-/
import analysis.asymptotics analysis.calculus.tangent_cone
open filter asymptotics continuous_linear_map set
noncomputable theory
local attribute [instance, priority 0] classical.decidable_inhabited classical.prop_decidable
set_option class.instance_max_depth 90
section
variables {k : Type*} [nondiscrete_normed_field k]
variables {E : Type*} [normed_group E] [normed_space k E]
variables {F : Type*} [normed_group F] [normed_space k F]
variables {G : Type*} [normed_group G] [normed_space k G]
def has_fderiv_at_filter (f : E → F) (f' : E →L[k] F) (x : E) (L : filter E) :=
is_o (λ x', f x' - f x - f' (x' - x)) (λ x', x' - x) L
def has_fderiv_within_at (f : E → F) (f' : E →L[k] F) (s : set E) (x : E) :=
has_fderiv_at_filter f f' x (nhds_within x s)
def has_fderiv_at (f : E → F) (f' : E →L[k] F) (x : E) :=
has_fderiv_at_filter f f' x (nhds x)
variables (k)
def differentiable_within_at (f : E → F) (s : set E) (x : E) :=
∃f' : E →L[k] F, has_fderiv_within_at f f' s x
def differentiable_at (f : E → F) (x : E) :=
∃f' : E →L[k] F, has_fderiv_at f f' x
def fderiv_within (f : E → F) (s : set E) (x : E) : E →L[k] F :=
if h : ∃f', has_fderiv_within_at f f' s x then classical.some h else 0
def fderiv (f : E → F) (x : E) : E →L[k] F :=
if h : ∃f', has_fderiv_at f f' x then classical.some h else 0
def differentiable_on (f : E → F) (s : set E) :=
∀x ∈ s, differentiable_within_at k f s x
def differentiable (f : E → F) :=
∀x, differentiable_at k f x
variables {k}
variables {f f₀ f₁ g : E → F}
variables {f' f₀' f₁' g' : E →L[k] F}
variables {x : E}
variables {s t : set E}
variables {L L₁ L₂ : filter E}
section derivative_uniqueness
/- In this section, we discuss the uniqueness of the derivative.
We prove that the definitions `unique_diff_within_at` and `unique_diff_on` indeed imply the
uniqueness of the derivative. -/
/-- `unique_diff_within_at` achieves its goal: it implies the uniqueness of the derivative. -/
theorem unique_diff_within_at.eq (H : unique_diff_within_at k s x)
(h : has_fderiv_within_at f f' s x) (h₁ : has_fderiv_within_at f f₁' s x) : f' = f₁' :=
begin
have A : ∀y ∈ tangent_cone_at k s x, f' y = f₁' y,
{ assume y hy,
rcases hy with ⟨c, d, hd, hc, ylim⟩,
have at_top_is_finer : at_top ≤ comap (λ (n : ℕ), x + d n) (nhds_within (x + 0) s),
{ rw [←tendsto_iff_comap, nhds_within, tendsto_inf],
split,
{ apply tendsto_add tendsto_const_nhds (tangent_cone_at.lim_zero hc ylim) },
{ rwa tendsto_principal } },
rw add_zero at at_top_is_finer,
have : is_o (λ y, f₁' (y - x) - f' (y - x)) (λ y, y - x) (nhds_within x s),
by simpa using h.sub h₁,
have : is_o (λ n:ℕ, f₁' ((x + d n) - x) - f' ((x + d n) - x)) (λ n, (x + d n) - x)
((nhds_within x s).comap (λn, x+ d n)) := is_o.comp this _,
have L1 : is_o (λ n:ℕ, f₁' (d n) - f' (d n)) d
((nhds_within x s).comap (λn, x + d n)) := by simpa using this,
have L2 : is_o (λn:ℕ, f₁' (d n) - f' (d n)) d at_top :=
is_o.mono at_top_is_finer L1,
have L3 : is_o (λn:ℕ, c n • (f₁' (d n) - f' (d n))) (λn, c n • d n) at_top :=
is_o_smul L2,
have L4 : is_o (λn:ℕ, c n • (f₁' (d n) - f' (d n))) (λn, (1:ℝ)) at_top :=
L3.trans_is_O (is_O_one_of_tendsto ylim),
have L : tendsto (λn:ℕ, c n • (f₁' (d n) - f' (d n))) at_top (nhds 0) :=
is_o_one_iff.1 L4,
have L' : tendsto (λ (n : ℕ), c n • (f₁' (d n) - f' (d n))) at_top (nhds (f₁' y - f' y)),
{ simp only [smul_sub, (continuous_linear_map.map_smul _ _ _).symm],
apply tendsto_sub ((f₁'.continuous.tendsto _).comp ylim) ((f'.continuous.tendsto _).comp ylim) },
have : f₁' y - f' y = 0 := tendsto_nhds_unique (by simp) L' L,
exact (sub_eq_zero_iff_eq.1 this).symm },
have B : ∀y ∈ submodule.span k (tangent_cone_at k s x), f' y = f₁' y,
{ assume y hy,
apply submodule.span_induction hy,
{ exact λy hy, A y hy },
{ simp only [continuous_linear_map.map_zero] },
{ simp {contextual := tt} },
{ simp {contextual := tt} } },
have C : ∀y ∈ closure ((submodule.span k (tangent_cone_at k s x)) : set E), f' y = f₁' y,
{ assume y hy,
let K := {y | f' y = f₁' y},
have : (submodule.span k (tangent_cone_at k s x) : set E) ⊆ K := B,
have : closure (submodule.span k (tangent_cone_at k s x) : set E) ⊆ closure K :=
closure_mono this,
have : y ∈ closure K := this hy,
rwa closure_eq_of_is_closed (is_closed_eq f'.continuous f₁'.continuous) at this },
rw H.1 at C,
ext y,
exact C y (mem_univ _)
end
theorem unique_diff_on.eq (H : unique_diff_on k s) (hx : x ∈ s)
(h : has_fderiv_within_at f f' s x) (h₁ : has_fderiv_within_at f f₁' s x) : f' = f₁' :=
unique_diff_within_at.eq (H x hx) h h₁
end derivative_uniqueness
/- Basic properties of the derivative -/
section fderiv_properties
theorem has_fderiv_at_filter_iff_tendsto :
has_fderiv_at_filter f f' x L ↔
tendsto (λ x', ∥x' - x∥⁻¹ * ∥f x' - f x - f' (x' - x)∥) L (nhds 0) :=
have h : ∀ x', ∥x' - x∥ = 0 → ∥f x' - f x - f' (x' - x)∥ = 0, from λ x' hx',
by { rw [sub_eq_zero.1 ((norm_eq_zero (x' - x)).1 hx')], simp },
begin
unfold has_fderiv_at_filter,
rw [←is_o_norm_left, ←is_o_norm_right, is_o_iff_tendsto h],
exact tendsto.congr'r (λ _, div_eq_inv_mul),
end
theorem has_fderiv_within_at_iff_tendsto : has_fderiv_within_at f f' s x ↔
tendsto (λ x', ∥x' - x∥⁻¹ * ∥f x' - f x - f' (x' - x)∥) (nhds_within x s) (nhds 0) :=
has_fderiv_at_filter_iff_tendsto
theorem has_fderiv_at_iff_tendsto : has_fderiv_at f f' x ↔
tendsto (λ x', ∥x' - x∥⁻¹ * ∥f x' - f x - f' (x' - x)∥) (nhds x) (nhds 0) :=
has_fderiv_at_filter_iff_tendsto
theorem has_fderiv_at_filter.mono (h : has_fderiv_at_filter f f' x L₂) (hst : L₁ ≤ L₂) :
has_fderiv_at_filter f f' x L₁ :=
is_o.mono hst h
theorem has_fderiv_within_at.mono (h : has_fderiv_within_at f f' t x) (hst : s ⊆ t) :
has_fderiv_within_at f f' s x :=
h.mono (nhds_within_mono _ hst)
theorem has_fderiv_at.has_fderiv_at_filter (h : has_fderiv_at f f' x) (hL : L ≤ nhds x) :
has_fderiv_at_filter f f' x L :=
h.mono hL
theorem has_fderiv_at.has_fderiv_within_at
(h : has_fderiv_at f f' x) : has_fderiv_within_at f f' s x :=
h.has_fderiv_at_filter lattice.inf_le_left
lemma has_fderiv_within_at.differentiable_within_at (h : has_fderiv_within_at f f' s x) :
differentiable_within_at k f s x :=
⟨f', h⟩
lemma has_fderiv_at.differentiable_at (h : has_fderiv_at f f' x) : differentiable_at k f x :=
⟨f', h⟩
@[simp] lemma has_fderiv_within_at_univ :
has_fderiv_within_at f f' univ x ↔ has_fderiv_at f f' x :=
by { simp only [has_fderiv_within_at, nhds_within_univ], refl }
theorem has_fderiv_at_unique
(h₀ : has_fderiv_at f f₀' x) (h₁ : has_fderiv_at f f₁' x) : f₀' = f₁' :=
begin
rw ← has_fderiv_within_at_univ at h₀ h₁,
exact unique_diff_within_at_univ.eq h₀ h₁
end
lemma has_fderiv_within_at_inter' (h : t ∈ nhds_within x s) :
has_fderiv_within_at f f' (s ∩ t) x ↔ has_fderiv_within_at f f' s x :=
by simp [has_fderiv_within_at, nhds_within_restrict'' s h]
lemma has_fderiv_within_at_inter (h : t ∈ nhds x) :
has_fderiv_within_at f f' (s ∩ t) x ↔ has_fderiv_within_at f f' s x :=
by simp [has_fderiv_within_at, nhds_within_restrict' s h]
lemma differentiable_within_at.has_fderiv_within_at (h : differentiable_within_at k f s x) :
has_fderiv_within_at f (fderiv_within k f s x) s x :=
begin
dunfold fderiv_within,
dunfold differentiable_within_at at h,
rw dif_pos h,
exact classical.some_spec h
end
lemma differentiable_at.has_fderiv_at (h : differentiable_at k f x) :
has_fderiv_at f (fderiv k f x) x :=
begin
dunfold fderiv,
dunfold differentiable_at at h,
rw dif_pos h,
exact classical.some_spec h
end
lemma has_fderiv_at.fderiv (h : has_fderiv_at f f' x) : fderiv k f x = f' :=
by { ext, rw has_fderiv_at_unique h h.differentiable_at.has_fderiv_at }
lemma has_fderiv_within_at.fderiv_within
(h : has_fderiv_within_at f f' s x) (hxs : unique_diff_within_at k s x) :
fderiv_within k f s x = f' :=
by { ext, rw hxs.eq h h.differentiable_within_at.has_fderiv_within_at }
lemma differentiable_within_at.mono (h : differentiable_within_at k f t x) (st : s ⊆ t) :
differentiable_within_at k f s x :=
begin
rcases h with ⟨f', hf'⟩,
exact ⟨f', hf'.mono st⟩
end
lemma differentiable_within_at_univ :
differentiable_within_at k f univ x ↔ differentiable_at k f x :=
begin
simp [differentiable_within_at, has_fderiv_within_at, nhds_within_univ],
refl
end
lemma differentiable_within_at_inter (ht : t ∈ nhds x) :
differentiable_within_at k f (s ∩ t) x ↔ differentiable_within_at k f s x :=
by simp only [differentiable_within_at, has_fderiv_within_at, has_fderiv_at_filter,
nhds_within_restrict' s ht]
lemma differentiable_at.differentiable_within_at
(h : differentiable_at k f x) : differentiable_within_at k f s x :=
(differentiable_within_at_univ.2 h).mono (subset_univ _)
lemma differentiable_within_at.differentiable_at
(h : differentiable_within_at k f s x) (hs : s ∈ nhds x) : differentiable_at k f x :=
begin
have : s = univ ∩ s, by rw univ_inter,
rwa [this, differentiable_within_at_inter hs, differentiable_within_at_univ] at h
end
lemma differentiable.fderiv_within
(h : differentiable_at k f x) (hxs : unique_diff_within_at k s x) :
fderiv_within k f s x = fderiv k f x :=
begin
apply has_fderiv_within_at.fderiv_within _ hxs,
exact h.has_fderiv_at.has_fderiv_within_at
end
lemma differentiable_on.mono (h : differentiable_on k f t) (st : s ⊆ t) :
differentiable_on k f s :=
λx hx, (h x (st hx)).mono st
lemma differentiable_on_univ :
differentiable_on k f univ ↔ differentiable k f :=
by { simp [differentiable_on, differentiable_within_at_univ], refl }
lemma differentiable.differentiable_on (h : differentiable k f) : differentiable_on k f s :=
(differentiable_on_univ.2 h).mono (subset_univ _)
lemma differentiable_on_of_locally_differentiable_on
(h : ∀x∈s, ∃u, is_open u ∧ x ∈ u ∧ differentiable_on k f (s ∩ u)) : differentiable_on k f s :=
begin
assume x xs,
rcases h x xs with ⟨t, t_open, xt, ht⟩,
exact (differentiable_within_at_inter (mem_nhds_sets t_open xt)).1 (ht x ⟨xs, xt⟩)
end
lemma fderiv_within_subset (st : s ⊆ t) (ht : unique_diff_within_at k s x)
(h : differentiable_within_at k f t x) :
fderiv_within k f s x = fderiv_within k f t x :=
((differentiable_within_at.has_fderiv_within_at h).mono st).fderiv_within ht
@[simp] lemma fderiv_within_univ : fderiv_within k f univ = fderiv k f :=
begin
ext x : 1,
by_cases h : differentiable_at k f x,
{ apply has_fderiv_within_at.fderiv_within _ (is_open_univ.unique_diff_within_at (mem_univ _)),
rw has_fderiv_within_at_univ,
apply h.has_fderiv_at },
{ have : fderiv k f x = 0,
by { unfold differentiable_at at h, simp [fderiv, h] },
rw this,
have : ¬(differentiable_within_at k f univ x), by rwa differentiable_within_at_univ,
unfold differentiable_within_at at this,
simp [fderiv_within, this, -has_fderiv_within_at_univ] }
end
lemma fderiv_within_inter (ht : t ∈ nhds x) (hs : unique_diff_within_at k s x) :
fderiv_within k f (s ∩ t) x = fderiv_within k f s x :=
begin
by_cases h : differentiable_within_at k f (s ∩ t) x,
{ apply fderiv_within_subset (inter_subset_left _ _) _ ((differentiable_within_at_inter ht).1 h),
apply hs.inter ht },
{ have : fderiv_within k f (s ∩ t) x = 0,
by { unfold differentiable_within_at at h, simp [fderiv_within, h] },
rw this,
rw differentiable_within_at_inter ht at h,
have : fderiv_within k f s x = 0,
by { unfold differentiable_within_at at h, simp [fderiv_within, h] },
rw this }
end
end fderiv_properties
/- Congr -/
section congr
theorem has_fderiv_at_filter_congr_of_mem_sets
(hx : f₀ x = f₁ x) (h₀ : {x | f₀ x = f₁ x} ∈ L) (h₁ : ∀ x, f₀' x = f₁' x) :
has_fderiv_at_filter f₀ f₀' x L ↔ has_fderiv_at_filter f₁ f₁' x L :=
by { rw (ext h₁), exact is_o_congr
(by filter_upwards [h₀] λ x (h : _ = _), by simp [h, hx])
(univ_mem_sets' $ λ _, rfl) }
lemma has_fderiv_at_filter.congr_of_mem_sets (h : has_fderiv_at_filter f f' x L)
(hL : {x | f₁ x = f x} ∈ L) (hx : f₁ x = f x) : has_fderiv_at_filter f₁ f' x L :=
begin
apply (has_fderiv_at_filter_congr_of_mem_sets hx hL _).2 h,
exact λx, rfl
end
lemma has_fderiv_within_at.congr_mono (h : has_fderiv_within_at f f' s x) (ht : ∀x ∈ t, f₁ x = f x)
(hx : f₁ x = f x) (h₁ : t ⊆ s) : has_fderiv_within_at f₁ f' t x :=
has_fderiv_at_filter.congr_of_mem_sets (h.mono h₁) (filter.mem_inf_sets_of_right ht) hx
lemma has_fderiv_within_at.congr_of_mem_nhds_within (h : has_fderiv_within_at f f' s x)
(h₁ : {y | f₁ y = f y} ∈ nhds_within x s) (hx : f₁ x = f x) : has_fderiv_within_at f₁ f' s x :=
has_fderiv_at_filter.congr_of_mem_sets h h₁ hx
lemma has_fderiv_at.congr_of_mem_nhds (h : has_fderiv_at f f' x)
(h₁ : {y | f₁ y = f y} ∈ nhds x) : has_fderiv_at f₁ f' x :=
has_fderiv_at_filter.congr_of_mem_sets h h₁ (mem_of_nhds h₁ : _)
lemma differentiable_within_at.congr_mono (h : differentiable_within_at k f s x)
(ht : ∀x ∈ t, f₁ x = f x) (hx : f₁ x = f x) (h₁ : t ⊆ s) : differentiable_within_at k f₁ t x :=
(has_fderiv_within_at.congr_mono h.has_fderiv_within_at ht hx h₁).differentiable_within_at
lemma differentiable_within_at.congr_of_mem_nhds_within
(h : differentiable_within_at k f s x) (h₁ : {y | f₁ y = f y} ∈ nhds_within x s)
(hx : f₁ x = f x) : differentiable_within_at k f₁ s x :=
(h.has_fderiv_within_at.congr_of_mem_nhds_within h₁ hx).differentiable_within_at
lemma differentiable_on.congr_mono (h : differentiable_on k f s) (h' : ∀x ∈ t, f₁ x = f x)
(h₁ : t ⊆ s) : differentiable_on k f₁ t :=
λ x hx, (h x (h₁ hx)).congr_mono h' (h' x hx) h₁
lemma differentiable_at.congr_of_mem_nhds (h : differentiable_at k f x)
(hL : {y | f₁ y = f y} ∈ nhds x) : differentiable_at k f₁ x :=
has_fderiv_at.differentiable_at (has_fderiv_at_filter.congr_of_mem_sets h.has_fderiv_at hL (mem_of_nhds hL : _))
lemma differentiable_within_at.fderiv_within_congr_mono (h : differentiable_within_at k f s x)
(hs : ∀x ∈ t, f₁ x = f x) (hx : f₁ x = f x) (hxt : unique_diff_within_at k t x) (h₁ : t ⊆ s) :
fderiv_within k f₁ t x = fderiv_within k f s x :=
(has_fderiv_within_at.congr_mono h.has_fderiv_within_at hs hx h₁).fderiv_within hxt
lemma fderiv_within_congr_of_mem_nhds_within (hs : unique_diff_within_at k s x)
(hL : {y | f₁ y = f y} ∈ nhds_within x s) (hx : f₁ x = f x) :
fderiv_within k f₁ s x = fderiv_within k f s x :=
begin
by_cases h : differentiable_within_at k f s x ∨ differentiable_within_at k f₁ s x,
{ cases h,
{ apply has_fderiv_within_at.fderiv_within _ hs,
exact has_fderiv_at_filter.congr_of_mem_sets h.has_fderiv_within_at hL hx },
{ symmetry,
apply has_fderiv_within_at.fderiv_within _ hs,
apply has_fderiv_at_filter.congr_of_mem_sets h.has_fderiv_within_at _ hx.symm,
convert hL,
ext y,
exact eq_comm } },
{ push_neg at h,
have A : fderiv_within k f s x = 0,
by { unfold differentiable_within_at at h, simp [fderiv_within, h] },
have A₁ : fderiv_within k f₁ s x = 0,
by { unfold differentiable_within_at at h, simp [fderiv_within, h] },
rw [A, A₁] }
end
lemma fderiv_within_congr (hs : unique_diff_within_at k s x)
(hL : ∀y∈s, f₁ y = f y) (hx : f₁ x = f x) :
fderiv_within k f₁ s x = fderiv_within k f s x :=
begin
apply fderiv_within_congr_of_mem_nhds_within hs _ hx,
apply mem_sets_of_superset self_mem_nhds_within,
exact hL
end
lemma fderiv_congr_of_mem_nhds (hL : {y | f₁ y = f y} ∈ nhds x) :
fderiv k f₁ x = fderiv k f x :=
begin
have A : f₁ x = f x := (mem_of_nhds hL : _),
rw [← fderiv_within_univ, ← fderiv_within_univ],
rw ← nhds_within_univ at hL,
exact fderiv_within_congr_of_mem_nhds_within unique_diff_within_at_univ hL A
end
end congr
/- id -/
section id
theorem has_fderiv_at_filter_id (x : E) (L : filter E) :
has_fderiv_at_filter id (id : E →L[k] E) x L :=
(is_o_zero _ _).congr_left $ by simp
theorem has_fderiv_within_at_id (x : E) (s : set E) :
has_fderiv_within_at id (id : E →L[k] E) s x :=
has_fderiv_at_filter_id _ _
theorem has_fderiv_at_id (x : E) : has_fderiv_at id (id : E →L[k] E) x :=
has_fderiv_at_filter_id _ _
lemma differentiable_at_id : differentiable_at k id x :=
(has_fderiv_at_id x).differentiable_at
lemma differentiable_within_at_id : differentiable_within_at k id s x :=
differentiable_at_id.differentiable_within_at
lemma differentiable_id : differentiable k (id : E → E) :=
λx, differentiable_at_id
lemma differentiable_on_id : differentiable_on k id s :=
differentiable_id.differentiable_on
lemma fderiv_id : fderiv k id x = id :=
has_fderiv_at.fderiv (has_fderiv_at_id x)
lemma fderiv_within_id (hxs : unique_diff_within_at k s x) :
fderiv_within k id s x = id :=
begin
rw differentiable.fderiv_within (differentiable_at_id) hxs,
exact fderiv_id
end
end id
/- constants -/
section const
theorem has_fderiv_at_filter_const (c : F) (x : E) (L : filter E) :
has_fderiv_at_filter (λ x, c) (0 : E →L[k] F) x L :=
(is_o_zero _ _).congr_left $ λ _, by simp only [zero_apply, sub_self]
theorem has_fderiv_within_at_const (c : F) (x : E) (s : set E) :
has_fderiv_within_at (λ x, c) (0 : E →L[k] F) s x :=
has_fderiv_at_filter_const _ _ _
theorem has_fderiv_at_const (c : F) (x : E) :
has_fderiv_at (λ x, c) (0 : E →L[k] F) x :=
has_fderiv_at_filter_const _ _ _
lemma differentiable_at_const (c : F) : differentiable_at k (λx, c) x :=
⟨0, has_fderiv_at_const c x⟩
lemma differentiable_within_at_const (c : F) : differentiable_within_at k (λx, c) s x :=
differentiable_at.differentiable_within_at (differentiable_at_const _)
lemma fderiv_const (c : F) : fderiv k (λy, c) x = 0 :=
has_fderiv_at.fderiv (has_fderiv_at_const c x)
lemma fderiv_within_const (c : F) (hxs : unique_diff_within_at k s x) :
fderiv_within k (λy, c) s x = 0 :=
begin
rw differentiable.fderiv_within (differentiable_at_const _) hxs,
exact fderiv_const _
end
lemma differentiable_const (c : F) : differentiable k (λx : E, c) :=
λx, differentiable_at_const _
lemma differentiable_on_const (c : F) : differentiable_on k (λx, c) s :=
(differentiable_const _).differentiable_on
end const
/- Bounded linear maps -/
section is_bounded_linear_map
lemma is_bounded_linear_map.has_fderiv_at_filter (h : is_bounded_linear_map k f) :
has_fderiv_at_filter f h.to_continuous_linear_map x L :=
begin
have : (λ (x' : E), f x' - f x - h.to_continuous_linear_map (x' - x)) = λx', 0,
{ ext,
have : ∀a, h.to_continuous_linear_map a = f a := λa, rfl,
simp,
simp [this] },
rw [has_fderiv_at_filter, this],
exact asymptotics.is_o_zero _ _
end
lemma is_bounded_linear_map.has_fderiv_within_at (h : is_bounded_linear_map k f) :
has_fderiv_within_at f h.to_continuous_linear_map s x :=
h.has_fderiv_at_filter
lemma is_bounded_linear_map.has_fderiv_at (h : is_bounded_linear_map k f) :
has_fderiv_at f h.to_continuous_linear_map x :=
h.has_fderiv_at_filter
lemma is_bounded_linear_map.differentiable_at (h : is_bounded_linear_map k f) :
differentiable_at k f x :=
h.has_fderiv_at.differentiable_at
lemma is_bounded_linear_map.differentiable_within_at (h : is_bounded_linear_map k f) :
differentiable_within_at k f s x :=
h.differentiable_at.differentiable_within_at
lemma is_bounded_linear_map.fderiv (h : is_bounded_linear_map k f) :
fderiv k f x = h.to_continuous_linear_map :=
has_fderiv_at.fderiv (h.has_fderiv_at)
lemma is_bounded_linear_map.fderiv_within (h : is_bounded_linear_map k f)
(hxs : unique_diff_within_at k s x) : fderiv_within k f s x = h.to_continuous_linear_map :=
begin
rw differentiable.fderiv_within h.differentiable_at hxs,
exact h.fderiv
end
lemma is_bounded_linear_map.differentiable (h : is_bounded_linear_map k f) :
differentiable k f :=
λx, h.differentiable_at
lemma is_bounded_linear_map.differentiable_on (h : is_bounded_linear_map k f) :
differentiable_on k f s :=
h.differentiable.differentiable_on
end is_bounded_linear_map
/- multiplication by a constant -/
section smul_const
theorem has_fderiv_at_filter.smul (h : has_fderiv_at_filter f f' x L) (c : k) :
has_fderiv_at_filter (λ x, c • f x) (c • f') x L :=
(is_o_const_smul_left h c).congr_left $ λ x, by simp [smul_neg, smul_add]
theorem has_fderiv_within_at.smul (h : has_fderiv_within_at f f' s x) (c : k) :
has_fderiv_within_at (λ x, c • f x) (c • f') s x :=
h.smul c
theorem has_fderiv_at.smul (h : has_fderiv_at f f' x) (c : k) :
has_fderiv_at (λ x, c • f x) (c • f') x :=
h.smul c
lemma differentiable_within_at.smul (h : differentiable_within_at k f s x) (c : k) :
differentiable_within_at k (λy, c • f y) s x :=
(h.has_fderiv_within_at.smul c).differentiable_within_at
lemma differentiable_at.smul (h : differentiable_at k f x) (c : k) :
differentiable_at k (λy, c • f y) x :=
(h.has_fderiv_at.smul c).differentiable_at
lemma differentiable_on.smul (h : differentiable_on k f s) (c : k) :
differentiable_on k (λy, c • f y) s :=
λx hx, (h x hx).smul c
lemma differentiable.smul (h : differentiable k f) (c : k) :
differentiable k (λy, c • f y) :=
λx, (h x).smul c
lemma fderiv_within_smul (hxs : unique_diff_within_at k s x)
(h : differentiable_within_at k f s x) (c : k) :
fderiv_within k (λy, c • f y) s x = c • fderiv_within k f s x :=
(h.has_fderiv_within_at.smul c).fderiv_within hxs
lemma fderiv_smul (h : differentiable_at k f x) (c : k) :
fderiv k (λy, c • f y) x = c • fderiv k f x :=
(h.has_fderiv_at.smul c).fderiv
end smul_const
/- add -/
section add
theorem has_fderiv_at_filter.add
(hf : has_fderiv_at_filter f f' x L) (hg : has_fderiv_at_filter g g' x L) :
has_fderiv_at_filter (λ y, f y + g y) (f' + g') x L :=
(hf.add hg).congr_left $ λ _, by simp
theorem has_fderiv_within_at.add
(hf : has_fderiv_within_at f f' s x) (hg : has_fderiv_within_at g g' s x) :
has_fderiv_within_at (λ y, f y + g y) (f' + g') s x :=
hf.add hg
theorem has_fderiv_at.add
(hf : has_fderiv_at f f' x) (hg : has_fderiv_at g g' x) :
has_fderiv_at (λ x, f x + g x) (f' + g') x :=
hf.add hg
lemma differentiable_within_at.add
(hf : differentiable_within_at k f s x) (hg : differentiable_within_at k g s x) :
differentiable_within_at k (λ y, f y + g y) s x :=
(hf.has_fderiv_within_at.add hg.has_fderiv_within_at).differentiable_within_at
lemma differentiable_at.add
(hf : differentiable_at k f x) (hg : differentiable_at k g x) :
differentiable_at k (λ y, f y + g y) x :=
(hf.has_fderiv_at.add hg.has_fderiv_at).differentiable_at
lemma differentiable_on.add
(hf : differentiable_on k f s) (hg : differentiable_on k g s) :
differentiable_on k (λy, f y + g y) s :=
λx hx, (hf x hx).add (hg x hx)
lemma differentiable.add
(hf : differentiable k f) (hg : differentiable k g) :
differentiable k (λy, f y + g y) :=
λx, (hf x).add (hg x)
lemma fderiv_within_add (hxs : unique_diff_within_at k s x)
(hf : differentiable_within_at k f s x) (hg : differentiable_within_at k g s x) :
fderiv_within k (λy, f y + g y) s x = fderiv_within k f s x + fderiv_within k g s x :=
(hf.has_fderiv_within_at.add hg.has_fderiv_within_at).fderiv_within hxs
lemma fderiv_add
(hf : differentiable_at k f x) (hg : differentiable_at k g x) :
fderiv k (λy, f y + g y) x = fderiv k f x + fderiv k g x :=
(hf.has_fderiv_at.add hg.has_fderiv_at).fderiv
end add
/- neg -/
section neg
theorem has_fderiv_at_filter.neg (h : has_fderiv_at_filter f f' x L) :
has_fderiv_at_filter (λ x, -f x) (-f') x L :=
(h.smul (-1:k)).congr (by simp) (by simp)
theorem has_fderiv_within_at.neg (h : has_fderiv_within_at f f' s x) :
has_fderiv_within_at (λ x, -f x) (-f') s x :=
h.neg
theorem has_fderiv_at.neg (h : has_fderiv_at f f' x) :
has_fderiv_at (λ x, -f x) (-f') x :=
h.neg
lemma differentiable_within_at.neg (h : differentiable_within_at k f s x) :
differentiable_within_at k (λy, -f y) s x :=
h.has_fderiv_within_at.neg.differentiable_within_at
lemma differentiable_at.neg (h : differentiable_at k f x) :
differentiable_at k (λy, -f y) x :=
h.has_fderiv_at.neg.differentiable_at
lemma differentiable_on.neg (h : differentiable_on k f s) :
differentiable_on k (λy, -f y) s :=
λx hx, (h x hx).neg
lemma differentiable.neg (h : differentiable k f) :
differentiable k (λy, -f y) :=
λx, (h x).neg
lemma fderiv_within_neg (hxs : unique_diff_within_at k s x)
(h : differentiable_within_at k f s x) :
fderiv_within k (λy, -f y) s x = - fderiv_within k f s x :=
h.has_fderiv_within_at.neg.fderiv_within hxs
lemma fderiv_neg (h : differentiable_at k f x) :
fderiv k (λy, -f y) x = - fderiv k f x :=
h.has_fderiv_at.neg.fderiv
end neg
/- sub -/
section sub
theorem has_fderiv_at_filter.sub
(hf : has_fderiv_at_filter f f' x L) (hg : has_fderiv_at_filter g g' x L) :
has_fderiv_at_filter (λ x, f x - g x) (f' - g') x L :=
hf.add hg.neg
theorem has_fderiv_within_at.sub
(hf : has_fderiv_within_at f f' s x) (hg : has_fderiv_within_at g g' s x) :
has_fderiv_within_at (λ x, f x - g x) (f' - g') s x :=
hf.sub hg
theorem has_fderiv_at.sub
(hf : has_fderiv_at f f' x) (hg : has_fderiv_at g g' x) :
has_fderiv_at (λ x, f x - g x) (f' - g') x :=
hf.sub hg
lemma differentiable_within_at.sub
(hf : differentiable_within_at k f s x) (hg : differentiable_within_at k g s x) :
differentiable_within_at k (λ y, f y - g y) s x :=
(hf.has_fderiv_within_at.sub hg.has_fderiv_within_at).differentiable_within_at
lemma differentiable_at.sub
(hf : differentiable_at k f x) (hg : differentiable_at k g x) :
differentiable_at k (λ y, f y - g y) x :=
(hf.has_fderiv_at.sub hg.has_fderiv_at).differentiable_at
lemma differentiable_on.sub
(hf : differentiable_on k f s) (hg : differentiable_on k g s) :
differentiable_on k (λy, f y - g y) s :=
λx hx, (hf x hx).sub (hg x hx)
lemma differentiable.sub
(hf : differentiable k f) (hg : differentiable k g) :
differentiable k (λy, f y - g y) :=
λx, (hf x).sub (hg x)
lemma fderiv_within_sub (hxs : unique_diff_within_at k s x)
(hf : differentiable_within_at k f s x) (hg : differentiable_within_at k g s x) :
fderiv_within k (λy, f y - g y) s x = fderiv_within k f s x - fderiv_within k g s x :=
(hf.has_fderiv_within_at.sub hg.has_fderiv_within_at).fderiv_within hxs
lemma fderiv_sub
(hf : differentiable_at k f x) (hg : differentiable_at k g x) :
fderiv k (λy, f y - g y) x = fderiv k f x - fderiv k g x :=
(hf.has_fderiv_at.sub hg.has_fderiv_at).fderiv
theorem has_fderiv_at_filter.is_O_sub (h : has_fderiv_at_filter f f' x L) :
is_O (λ x', f x' - f x) (λ x', x' - x) L :=
h.to_is_O.congr_of_sub.2 (f'.is_O_sub _ _)
end sub
/- Continuity -/
section continuous
theorem has_fderiv_at_filter.tendsto_nhds
(hL : L ≤ nhds x) (h : has_fderiv_at_filter f f' x L) :
tendsto f L (nhds (f x)) :=
begin
have : tendsto (λ x', f x' - f x) L (nhds 0),
{ refine h.is_O_sub.trans_tendsto (tendsto_le_left hL _),
rw ← sub_self x, exact tendsto_sub tendsto_id tendsto_const_nhds },
have := tendsto_add this tendsto_const_nhds,
rw zero_add (f x) at this,
exact this.congr (by simp)
end
theorem has_fderiv_within_at.continuous_within_at
(h : has_fderiv_within_at f f' s x) : continuous_within_at f s x :=
has_fderiv_at_filter.tendsto_nhds lattice.inf_le_left h
theorem has_fderiv_at.continuous_at (h : has_fderiv_at f f' x) :
continuous_at f x :=
has_fderiv_at_filter.tendsto_nhds (le_refl _) h
lemma differentiable_within_at.continuous_within_at (h : differentiable_within_at k f s x) :
continuous_within_at f s x :=
let ⟨f', hf'⟩ := h in hf'.continuous_within_at
lemma differentiable_at.continuous_at (h : differentiable_at k f x) : continuous_at f x :=
let ⟨f', hf'⟩ := h in hf'.continuous_at
lemma differentiable_on.continuous_on (h : differentiable_on k f s) : continuous_on f s :=
λx hx, (h x hx).continuous_within_at
lemma differentiable.continuous (h : differentiable k f) : continuous f :=
continuous_iff_continuous_at.2 $ λx, (h x).continuous_at
end continuous
/- Bounded bilinear maps -/
section bilinear_map
variables {b : E × F → G} {u : set (E × F) }
lemma is_bounded_bilinear_map.has_fderiv_at (h : is_bounded_bilinear_map k b) (p : E × F) :
has_fderiv_at b (h.deriv p) p :=
begin
have : (λ (x : E × F), b x - b p - (h.deriv p) (x - p)) = (λx, b (x.1 - p.1, x.2 - p.2)),
{ ext x,
delta is_bounded_bilinear_map.deriv,
change b x - b p - (b (p.1, x.2-p.2) + b (x.1-p.1, p.2))
= b (x.1 - p.1, x.2 - p.2),
have : b x = b (x.1, x.2), by { cases x, refl },
rw this,
have : b p = b (p.1, p.2), by { cases p, refl },
rw this,
simp only [h.map_sub_left, h.map_sub_right],
abel },
rw [has_fderiv_at, has_fderiv_at_filter, this],
rcases h.bound with ⟨C, Cpos, hC⟩,
have A : asymptotics.is_O (λx : E × F, b (x.1 - p.1, x.2 - p.2))
(λx, ∥x - p∥ * ∥x - p∥) (nhds p) :=
⟨C, Cpos, filter.univ_mem_sets' (λx, begin
simp only [mem_set_of_eq, norm_mul, norm_norm],
calc ∥b (x.1 - p.1, x.2 - p.2)∥ ≤ C * ∥x.1 - p.1∥ * ∥x.2 - p.2∥ : hC _ _
... ≤ C * ∥x-p∥ * ∥x-p∥ : by apply_rules [mul_le_mul, le_max_left, le_max_right, norm_nonneg,
le_of_lt Cpos, le_refl, mul_nonneg, norm_nonneg, norm_nonneg]
... = C * (∥x-p∥ * ∥x-p∥) : mul_assoc _ _ _ end)⟩,
have B : asymptotics.is_o (λ (x : E × F), ∥x - p∥ * ∥x - p∥)
(λx, 1 * ∥x - p∥) (nhds p),
{ apply asymptotics.is_o_mul_right _ (asymptotics.is_O_refl _ _),
rw [asymptotics.is_o_iff_tendsto],
{ simp only [div_one],
have : 0 = ∥p - p∥, by simp,
rw this,
have : continuous (λx, ∥x-p∥) :=
continuous_norm.comp (continuous_sub continuous_id continuous_const),
exact this.tendsto p },
simp only [forall_prop_of_false, not_false_iff, one_ne_zero, forall_true_iff] },
simp only [one_mul, asymptotics.is_o_norm_right] at B,
exact A.trans_is_o B
end
lemma is_bounded_bilinear_map.has_fderiv_within_at (h : is_bounded_bilinear_map k b) (p : E × F) :
has_fderiv_within_at b (h.deriv p) u p :=
(h.has_fderiv_at p).has_fderiv_within_at
lemma is_bounded_bilinear_map.differentiable_at (h : is_bounded_bilinear_map k b) (p : E × F) :
differentiable_at k b p :=
(h.has_fderiv_at p).differentiable_at
lemma is_bounded_bilinear_map.differentiable_within_at (h : is_bounded_bilinear_map k b) (p : E × F) :
differentiable_within_at k b u p :=
(h.differentiable_at p).differentiable_within_at
lemma is_bounded_bilinear_map.fderiv (h : is_bounded_bilinear_map k b) (p : E × F) :
fderiv k b p = h.deriv p :=
has_fderiv_at.fderiv (h.has_fderiv_at p)
lemma is_bounded_bilinear_map.fderiv_within (h : is_bounded_bilinear_map k b) (p : E × F)
(hxs : unique_diff_within_at k u p) : fderiv_within k b u p = h.deriv p :=
begin
rw differentiable.fderiv_within (h.differentiable_at p) hxs,
exact h.fderiv p
end
lemma is_bounded_bilinear_map.differentiable (h : is_bounded_bilinear_map k b) :
differentiable k b :=
λx, h.differentiable_at x
lemma is_bounded_bilinear_map.differentiable_on (h : is_bounded_bilinear_map k b) :
differentiable_on k b u :=
h.differentiable.differentiable_on
lemma is_bounded_bilinear_map.continuous (h : is_bounded_bilinear_map k b) :
continuous b :=
h.differentiable.continuous
end bilinear_map
/- Cartesian products -/
section cartesian_product
variables {f₂ : E → G} {f₂' : E →L[k] G}
lemma has_fderiv_at_filter.prod
(hf₁ : has_fderiv_at_filter f₁ f₁' x L) (hf₂ : has_fderiv_at_filter f₂ f₂' x L) :
has_fderiv_at_filter (λx, (f₁ x, f₂ x)) (continuous_linear_map.prod f₁' f₂') x L :=
begin
have : (λ (x' : E), (f₁ x', f₂ x') - (f₁ x, f₂ x) - (continuous_linear_map.prod f₁' f₂') (x' -x)) =
(λ (x' : E), (f₁ x' - f₁ x - f₁' (x' - x), f₂ x' - f₂ x - f₂' (x' - x))) := rfl,
rw [has_fderiv_at_filter, this],
rw [asymptotics.is_o_prod_left],
exact ⟨hf₁, hf₂⟩
end
lemma has_fderiv_within_at.prod
(hf₁ : has_fderiv_within_at f₁ f₁' s x) (hf₂ : has_fderiv_within_at f₂ f₂' s x) :
has_fderiv_within_at (λx, (f₁ x, f₂ x)) (continuous_linear_map.prod f₁' f₂') s x :=
hf₁.prod hf₂
lemma has_fderiv_at.prod (hf₁ : has_fderiv_at f₁ f₁' x) (hf₂ : has_fderiv_at f₂ f₂' x) :
has_fderiv_at (λx, (f₁ x, f₂ x)) (continuous_linear_map.prod f₁' f₂') x :=
hf₁.prod hf₂
lemma differentiable_within_at.prod
(hf₁ : differentiable_within_at k f₁ s x) (hf₂ : differentiable_within_at k f₂ s x) :
differentiable_within_at k (λx:E, (f₁ x, f₂ x)) s x :=
(hf₁.has_fderiv_within_at.prod hf₂.has_fderiv_within_at).differentiable_within_at
lemma differentiable_at.prod (hf₁ : differentiable_at k f₁ x) (hf₂ : differentiable_at k f₂ x) :
differentiable_at k (λx:E, (f₁ x, f₂ x)) x :=
(hf₁.has_fderiv_at.prod hf₂.has_fderiv_at).differentiable_at
lemma differentiable_on.prod (hf₁ : differentiable_on k f₁ s) (hf₂ : differentiable_on k f₂ s) :
differentiable_on k (λx:E, (f₁ x, f₂ x)) s :=
λx hx, differentiable_within_at.prod (hf₁ x hx) (hf₂ x hx)
lemma differentiable.prod (hf₁ : differentiable k f₁) (hf₂ : differentiable k f₂) :
differentiable k (λx:E, (f₁ x, f₂ x)) :=
λ x, differentiable_at.prod (hf₁ x) (hf₂ x)
lemma differentiable_at.fderiv_prod
(hf₁ : differentiable_at k f₁ x) (hf₂ : differentiable_at k f₂ x) :
fderiv k (λx:E, (f₁ x, f₂ x)) x =
continuous_linear_map.prod (fderiv k f₁ x) (fderiv k f₂ x) :=
has_fderiv_at.fderiv (has_fderiv_at.prod hf₁.has_fderiv_at hf₂.has_fderiv_at)
lemma differentiable_at.fderiv_within_prod
(hf₁ : differentiable_within_at k f₁ s x) (hf₂ : differentiable_within_at k f₂ s x)
(hxs : unique_diff_within_at k s x) :
fderiv_within k (λx:E, (f₁ x, f₂ x)) s x =
continuous_linear_map.prod (fderiv_within k f₁ s x) (fderiv_within k f₂ s x) :=
begin
apply has_fderiv_within_at.fderiv_within _ hxs,
exact has_fderiv_within_at.prod hf₁.has_fderiv_within_at hf₂.has_fderiv_within_at
end
end cartesian_product
/- Composition -/
section composition
/- For composition lemmas, we put x explicit to help the elaborator, as otherwise Lean tends to
get confused since there are too many possibilities for composition -/
variable (x)
theorem has_fderiv_at_filter.comp {g : F → G} {g' : F →L[k] G}
(hg : has_fderiv_at_filter g g' (f x) (L.map f))
(hf : has_fderiv_at_filter f f' x L) :
has_fderiv_at_filter (g ∘ f) (g'.comp f') x L :=
let eq₁ := (g'.is_O_comp _ _).trans_is_o hf in
let eq₂ := ((hg.comp f).mono le_comap_map).trans_is_O hf.is_O_sub in
by { refine eq₂.tri (eq₁.congr_left (λ x', _)), simp }
/- A readable version of the previous theorem,
a general form of the chain rule. -/
example {g : F → G} {g' : F →L[k] G}
(hg : has_fderiv_at_filter g g' (f x) (L.map f))
(hf : has_fderiv_at_filter f f' x L) :
has_fderiv_at_filter (g ∘ f) (g'.comp f') x L :=
begin
unfold has_fderiv_at_filter at hg,
have : is_o (λ x', g (f x') - g (f x) - g' (f x' - f x)) (λ x', f x' - f x) L,
from (hg.comp f).mono le_comap_map,
have eq₁ : is_o (λ x', g (f x') - g (f x) - g' (f x' - f x)) (λ x', x' - x) L,
from this.trans_is_O hf.is_O_sub,
have eq₂ : is_o (λ x', f x' - f x - f' (x' - x)) (λ x', x' - x) L,
from hf,
have : is_O
(λ x', g' (f x' - f x - f' (x' - x))) (λ x', f x' - f x - f' (x' - x)) L,
from g'.is_O_comp _ _,
have : is_o (λ x', g' (f x' - f x - f' (x' - x))) (λ x', x' - x) L,
from this.trans_is_o eq₂,
have eq₃ : is_o (λ x', g' (f x' - f x) - (g' (f' (x' - x)))) (λ x', x' - x) L,
by { refine this.congr_left _, simp},
exact eq₁.tri eq₃
end
theorem has_fderiv_within_at.comp {g : F → G} {g' : F →L[k] G}
(hg : has_fderiv_within_at g g' (f '' s) (f x))
(hf : has_fderiv_within_at f f' s x) :
has_fderiv_within_at (g ∘ f) (g'.comp f') s x :=
(has_fderiv_at_filter.mono hg
hf.continuous_within_at.tendsto_nhds_within_image).comp x hf
/-- The chain rule. -/
theorem has_fderiv_at.comp {g : F → G} {g' : F →L[k] G}
(hg : has_fderiv_at g g' (f x)) (hf : has_fderiv_at f f' x) :
has_fderiv_at (g ∘ f) (g'.comp f') x :=
(hg.mono hf.continuous_at).comp x hf
theorem has_fderiv_at.comp_has_fderiv_within_at {g : F → G} {g' : F →L[k] G}
(hg : has_fderiv_at g g' (f x)) (hf : has_fderiv_within_at f f' s x) :
has_fderiv_within_at (g ∘ f) (g'.comp f') s x :=
begin
rw ← has_fderiv_within_at_univ at hg,
exact has_fderiv_within_at.comp x (hg.mono (subset_univ _)) hf
end
lemma differentiable_within_at.comp {g : F → G} {t : set F}
(hg : differentiable_within_at k g t (f x)) (hf : differentiable_within_at k f s x)
(h : f '' s ⊆ t) : differentiable_within_at k (g ∘ f) s x :=
begin
rcases hf with ⟨f', hf'⟩,
rcases hg with ⟨g', hg'⟩,
exact ⟨continuous_linear_map.comp g' f', (hg'.mono h).comp x hf'⟩
end
lemma differentiable_at.comp {g : F → G}
(hg : differentiable_at k g (f x)) (hf : differentiable_at k f x) :
differentiable_at k (g ∘ f) x :=
(hg.has_fderiv_at.comp x hf.has_fderiv_at).differentiable_at
lemma fderiv_within.comp {g : F → G} {t : set F}
(hg : differentiable_within_at k g t (f x)) (hf : differentiable_within_at k f s x)
(h : f '' s ⊆ t) (hxs : unique_diff_within_at k s x) :
fderiv_within k (g ∘ f) s x =
continuous_linear_map.comp (fderiv_within k g t (f x)) (fderiv_within k f s x) :=
begin
apply has_fderiv_within_at.fderiv_within _ hxs,
apply has_fderiv_within_at.comp x _ (hf.has_fderiv_within_at),
apply hg.has_fderiv_within_at.mono h
end
lemma fderiv.comp {g : F → G}
(hg : differentiable_at k g (f x)) (hf : differentiable_at k f x) :
fderiv k (g ∘ f) x = continuous_linear_map.comp (fderiv k g (f x)) (fderiv k f x) :=
begin
apply has_fderiv_at.fderiv,
exact has_fderiv_at.comp x hg.has_fderiv_at hf.has_fderiv_at
end
lemma differentiable_on.comp {g : F → G} {t : set F}
(hg : differentiable_on k g t) (hf : differentiable_on k f s) (st : f '' s ⊆ t) :
differentiable_on k (g ∘ f) s :=
λx hx, differentiable_within_at.comp x (hg (f x) (st (mem_image_of_mem _ hx))) (hf x hx) st
lemma differentiable.comp {g : F → G} (hg : differentiable k g) (hf : differentiable k f) :
differentiable k (g ∘ f) :=
λx, differentiable_at.comp x (hg (f x)) (hf x)
end composition
/- Multiplication by a scalar function -/
section smul
variables {c : E → k} {c' : E →L[k] k}
theorem has_fderiv_within_at.smul'
(hc : has_fderiv_within_at c c' s x) (hf : has_fderiv_within_at f f' s x) :
has_fderiv_within_at (λ y, c y • f y) (c x • f' + c'.scalar_prod_space_iso (f x)) s x :=
begin
have : is_bounded_bilinear_map k (λ (p : k × F), p.1 • p.2) := is_bounded_bilinear_map_smul,
exact has_fderiv_at.comp_has_fderiv_within_at x (this.has_fderiv_at (c x, f x)) (hc.prod hf)
end
theorem has_fderiv_at.smul' (hc : has_fderiv_at c c' x) (hf : has_fderiv_at f f' x) :
has_fderiv_at (λ y, c y • f y) (c x • f' + c'.scalar_prod_space_iso (f x)) x :=
begin
have : is_bounded_bilinear_map k (λ (p : k × F), p.1 • p.2) := is_bounded_bilinear_map_smul,
exact has_fderiv_at.comp x (this.has_fderiv_at (c x, f x)) (hc.prod hf)
end
lemma differentiable_within_at.smul'
(hc : differentiable_within_at k c s x) (hf : differentiable_within_at k f s x) :
differentiable_within_at k (λ y, c y • f y) s x :=
(hc.has_fderiv_within_at.smul' hf.has_fderiv_within_at).differentiable_within_at
lemma differentiable_at.smul' (hc : differentiable_at k c x) (hf : differentiable_at k f x) :
differentiable_at k (λ y, c y • f y) x :=
(hc.has_fderiv_at.smul' hf.has_fderiv_at).differentiable_at
lemma differentiable_on.smul' (hc : differentiable_on k c s) (hf : differentiable_on k f s) :
differentiable_on k (λ y, c y • f y) s :=
λx hx, (hc x hx).smul' (hf x hx)
lemma differentiable.smul' (hc : differentiable k c) (hf : differentiable k f) :
differentiable k (λ y, c y • f y) :=
λx, (hc x).smul' (hf x)
lemma fderiv_within_smul' (hxs : unique_diff_within_at k s x)
(hc : differentiable_within_at k c s x) (hf : differentiable_within_at k f s x) :
fderiv_within k (λ y, c y • f y) s x =
c x • fderiv_within k f s x + (fderiv_within k c s x).scalar_prod_space_iso (f x) :=
(hc.has_fderiv_within_at.smul' hf.has_fderiv_within_at).fderiv_within hxs
lemma fderiv_smul' (hc : differentiable_at k c x) (hf : differentiable_at k f x) :
fderiv k (λ y, c y • f y) x =
c x • fderiv k f x + (fderiv k c x).scalar_prod_space_iso (f x) :=
(hc.has_fderiv_at.smul' hf.has_fderiv_at).fderiv
end smul
/- Multiplication of scalar functions -/
section mul
set_option class.instance_max_depth 120
variables {c d : E → k} {c' d' : E →L[k] k}
theorem has_fderiv_within_at.mul
(hc : has_fderiv_within_at c c' s x) (hd : has_fderiv_within_at d d' s x) :
has_fderiv_within_at (λ y, c y * d y) (c x • d' + d x • c') s x :=
begin
have : is_bounded_bilinear_map k (λ (p : k × k), p.1 * p.2) := is_bounded_bilinear_map_mul,
convert has_fderiv_at.comp_has_fderiv_within_at x (this.has_fderiv_at (c x, d x)) (hc.prod hd),
ext z,
change c x * d' z + d x * c' z = c x * d' z + c' z * d x,
ring
end
theorem has_fderiv_at.mul (hc : has_fderiv_at c c' x) (hd : has_fderiv_at d d' x) :
has_fderiv_at (λ y, c y * d y) (c x • d' + d x • c') x :=
begin
have : is_bounded_bilinear_map k (λ (p : k × k), p.1 * p.2) := is_bounded_bilinear_map_mul,
convert has_fderiv_at.comp x (this.has_fderiv_at (c x, d x)) (hc.prod hd),
ext z,
change c x * d' z + d x * c' z = c x * d' z + c' z * d x,
ring
end
lemma differentiable_within_at.mul
(hc : differentiable_within_at k c s x) (hd : differentiable_within_at k d s x) :
differentiable_within_at k (λ y, c y * d y) s x :=
(hc.has_fderiv_within_at.mul hd.has_fderiv_within_at).differentiable_within_at
lemma differentiable_at.mul (hc : differentiable_at k c x) (hd : differentiable_at k d x) :
differentiable_at k (λ y, c y * d y) x :=
(hc.has_fderiv_at.mul hd.has_fderiv_at).differentiable_at
lemma differentiable_on.mul (hc : differentiable_on k c s) (hd : differentiable_on k d s) :
differentiable_on k (λ y, c y * d y) s :=
λx hx, (hc x hx).mul (hd x hx)
lemma differentiable.mul (hc : differentiable k c) (hd : differentiable k d) :
differentiable k (λ y, c y * d y) :=
λx, (hc x).mul (hd x)
lemma fderiv_within_mul (hxs : unique_diff_within_at k s x)
(hc : differentiable_within_at k c s x) (hd : differentiable_within_at k d s x) :
fderiv_within k (λ y, c y * d y) s x =
c x • fderiv_within k d s x + d x • fderiv_within k c s x :=
(hc.has_fderiv_within_at.mul hd.has_fderiv_within_at).fderiv_within hxs
lemma fderiv_mul (hc : differentiable_at k c x) (hd : differentiable_at k d x) :
fderiv k (λ y, c y * d y) x =
c x • fderiv k d x + d x • fderiv k c x :=
(hc.has_fderiv_at.mul hd.has_fderiv_at).fderiv
end mul
end
/-
In the special case of a normed space over the reals,
we can use scalar multiplication in the `tendsto` characterization
of the Fréchet derivative.
-/
section
variables {E : Type*} [normed_group E] [normed_space ℝ E]
variables {F : Type*} [normed_group F] [normed_space ℝ F]
variables {G : Type*} [normed_group G] [normed_space ℝ G]
theorem has_fderiv_at_filter_real_equiv {f : E → F} {f' : E →L[ℝ] F} {x : E} {L : filter E} :
tendsto (λ x' : E, ∥x' - x∥⁻¹ * ∥f x' - f x - f' (x' - x)∥) L (nhds 0) ↔
tendsto (λ x' : E, ∥x' - x∥⁻¹ • (f x' - f x - f' (x' - x))) L (nhds 0) :=
begin
symmetry,
rw [tendsto_iff_norm_tendsto_zero], refine tendsto.congr'r (λ x', _),
have : ∥x' + -x∥⁻¹ ≥ 0, from inv_nonneg.mpr (norm_nonneg _),
simp [norm_smul, real.norm_eq_abs, abs_of_nonneg this]
end
end
|
d04419bbebeeeba3ad91e7c235c9742174c0d796 | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/topology/algebra/polynomial.lean | e6555fe22cab072373de5218d823105ed39e9918 | [
"Apache-2.0"
] | permissive | jjgarzella/mathlib | 96a345378c4e0bf26cf604aed84f90329e4896a2 | 395d8716c3ad03747059d482090e2bb97db612c8 | refs/heads/master | 1,686,480,124,379 | 1,625,163,323,000 | 1,625,163,323,000 | 281,190,421 | 2 | 0 | Apache-2.0 | 1,595,268,170,000 | 1,595,268,169,000 | null | UTF-8 | Lean | false | false | 5,239 | lean | /-
Copyright (c) 2018 Robert Y. Lewis. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Robert Y. Lewis
-/
import data.polynomial.algebra_map
import data.polynomial.div
import topology.metric_space.cau_seq_filter
/-!
# Polynomials and limits
In this file we prove the following lemmas.
* `polynomial.continuous_eval₂: `polynomial.eval₂` defines a continuous function.
* `polynomial.continuous_aeval: `polynomial.aeval` defines a continuous function;
we also prove convenience lemmas `polynomial.continuous_at_aeval`,
`polynomial.continuous_within_at_aeval`, `polynomial.continuous_on_aeval`.
* `polynomial.continuous`: `polynomial.eval` defines a continuous functions;
we also prove convenience lemmas `polynomial.continuous_at`, `polynomial.continuous_within_at`,
`polynomial.continuous_on`.
* `polynomial.tendsto_norm_at_top`: `λ x, ∥polynomial.eval (z x) p∥` tends to infinity provided that
`λ x, ∥z x∥` tends to infinity and `0 < degree p`;
* `polynomial.tendsto_abv_eval₂_at_top`, `polynomial.tendsto_abv_at_top`,
`polynomial.tendsto_abv_aeval_at_top`: a few versions of the previous statement for
`is_absolute_value abv` instead of norm.
## Tags
polynomial, continuity
-/
open is_absolute_value filter
namespace polynomial
section topological_semiring
variables {R S : Type*} [semiring R] [topological_space R] [topological_semiring R]
(p : polynomial R)
@[continuity]
protected lemma continuous_eval₂ [semiring S] (p : polynomial S) (f : S →+* R) :
continuous (λ x, p.eval₂ f x) :=
begin
dsimp only [eval₂_eq_sum, finsupp.sum],
exact continuous_finset_sum _ (λ c hc, continuous_const.mul (continuous_pow _))
end
@[continuity]
protected lemma continuous : continuous (λ x, p.eval x) :=
p.continuous_eval₂ _
protected lemma continuous_at {a : R} : continuous_at (λ x, p.eval x) a :=
p.continuous.continuous_at
protected lemma continuous_within_at {s a} : continuous_within_at (λ x, p.eval x) s a :=
p.continuous.continuous_within_at
protected lemma continuous_on {s} : continuous_on (λ x, p.eval x) s :=
p.continuous.continuous_on
end topological_semiring
section topological_algebra
variables {R A : Type*} [comm_semiring R] [semiring A] [algebra R A]
[topological_space A] [topological_semiring A]
(p : polynomial R)
@[continuity]
protected lemma continuous_aeval : continuous (λ x : A, aeval x p) :=
p.continuous_eval₂ _
protected lemma continuous_at_aeval {a : A} : continuous_at (λ x : A, aeval x p) a :=
p.continuous_aeval.continuous_at
protected lemma continuous_within_at_aeval {s a} : continuous_within_at (λ x : A, aeval x p) s a :=
p.continuous_aeval.continuous_within_at
protected lemma continuous_on_aeval {s} : continuous_on (λ x : A, aeval x p) s :=
p.continuous_aeval.continuous_on
end topological_algebra
lemma tendsto_abv_eval₂_at_top {R S k α : Type*} [semiring R] [ring S] [linear_ordered_field k]
(f : R →+* S) (abv : S → k) [is_absolute_value abv] (p : polynomial R) (hd : 0 < degree p)
(hf : f p.leading_coeff ≠ 0) {l : filter α} {z : α → S} (hz : tendsto (abv ∘ z) l at_top) :
tendsto (λ x, abv (p.eval₂ f (z x))) l at_top :=
begin
revert hf, refine degree_pos_induction_on p hd _ _ _; clear hd p,
{ rintros c - hc,
rw [leading_coeff_mul_X, leading_coeff_C] at hc,
simpa [abv_mul abv] using hz.const_mul_at_top ((abv_pos abv).2 hc) },
{ intros p hpd ihp hf,
rw [leading_coeff_mul_X] at hf,
simpa [abv_mul abv] using (ihp hf).at_top_mul_at_top hz },
{ intros p a hd ihp hf,
rw [add_comm, leading_coeff_add_of_degree_lt (degree_C_le.trans_lt hd)] at hf,
refine tendsto_at_top_of_add_const_right (abv (-f a)) _,
refine tendsto_at_top_mono (λ _, abv_add abv _ _) _,
simpa using ihp hf }
end
lemma tendsto_abv_at_top {R k α : Type*} [ring R] [linear_ordered_field k]
(abv : R → k) [is_absolute_value abv] (p : polynomial R) (h : 0 < degree p)
{l : filter α} {z : α → R} (hz : tendsto (abv ∘ z) l at_top) :
tendsto (λ x, abv (p.eval (z x))) l at_top :=
tendsto_abv_eval₂_at_top _ _ _ h (mt leading_coeff_eq_zero.1 $ ne_zero_of_degree_gt h) hz
lemma tendsto_abv_aeval_at_top {R A k α : Type*} [comm_semiring R] [ring A] [algebra R A]
[linear_ordered_field k] (abv : A → k) [is_absolute_value abv] (p : polynomial R)
(hd : 0 < degree p) (h₀ : algebra_map R A p.leading_coeff ≠ 0)
{l : filter α} {z : α → A} (hz : tendsto (abv ∘ z) l at_top) :
tendsto (λ x, abv (aeval (z x) p)) l at_top :=
tendsto_abv_eval₂_at_top _ abv p hd h₀ hz
variables {α R : Type*} [normed_ring R] [is_absolute_value (norm : R → ℝ)]
lemma tendsto_norm_at_top (p : polynomial R) (h : 0 < degree p) {l : filter α} {z : α → R}
(hz : tendsto (λ x, ∥z x∥) l at_top) :
tendsto (λ x, ∥p.eval (z x)∥) l at_top :=
p.tendsto_abv_at_top norm h hz
lemma exists_forall_norm_le [proper_space R] (p : polynomial R) :
∃ x, ∀ y, ∥p.eval x∥ ≤ ∥p.eval y∥ :=
if hp0 : 0 < degree p
then p.continuous.norm.exists_forall_le $ p.tendsto_norm_at_top hp0 tendsto_norm_cocompact_at_top
else ⟨p.coeff 0, by rw [eq_C_of_degree_le_zero (le_of_not_gt hp0)]; simp⟩
end polynomial
|
aa4c58cc0e3d48b481959e8e37327f70c02f636e | 618003631150032a5676f229d13a079ac875ff77 | /src/category_theory/yoneda.lean | 489ba52182fb6a7d03783aac024015ad3a410743 | [
"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 | 6,545 | lean | /-
Copyright (c) 2017 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import category_theory.hom_functor
/-!
# The Yoneda embedding
The Yoneda embedding as a functor `yoneda : C ⥤ (Cᵒᵖ ⥤ Type v₁)`,
along with an instance that it is `fully_faithful`.
Also the Yoneda lemma, `yoneda_lemma : (yoneda_pairing C) ≅ (yoneda_evaluation C)`.
-/
namespace category_theory
open opposite
universes v₁ u₁ u₂ -- declare the `v`'s first; see `category_theory.category` for an explanation
variables {C : Type u₁} [category.{v₁} C]
@[simps] def yoneda : C ⥤ (Cᵒᵖ ⥤ Type v₁) :=
{ obj := λ X,
{ obj := λ Y, unop Y ⟶ X,
map := λ Y Y' f g, f.unop ≫ g,
map_comp' := λ _ _ _ f g, begin ext, dsimp, erw [category.assoc] end,
map_id' := λ Y, begin ext, dsimp, erw [category.id_comp] end },
map := λ X X' f, { app := λ Y g, g ≫ f } }
@[simps] def coyoneda : Cᵒᵖ ⥤ (C ⥤ Type v₁) :=
{ obj := λ X,
{ obj := λ Y, unop X ⟶ Y,
map := λ Y Y' f g, g ≫ f,
map_comp' := λ _ _ _ f g, begin ext1, dsimp, erw [category.assoc] end,
map_id' := λ Y, begin ext1, dsimp, erw [category.comp_id] end },
map := λ X X' f, { app := λ Y g, f.unop ≫ g },
map_comp' := λ _ _ _ f g, begin ext, dsimp, erw [category.assoc] end,
map_id' := λ X, begin ext, dsimp, erw [category.id_comp] end }
namespace yoneda
lemma obj_map_id {X Y : C} (f : op X ⟶ op Y) :
((@yoneda C _).obj X).map f (𝟙 X) = ((@yoneda C _).map f.unop).app (op Y) (𝟙 Y) :=
by obviously
@[simp] lemma naturality {X Y : C} (α : yoneda.obj X ⟶ yoneda.obj Y)
{Z Z' : C} (f : Z ⟶ Z') (h : Z' ⟶ X) : f ≫ α.app (op Z') h = α.app (op Z) (f ≫ h) :=
(functor_to_types.naturality _ _ α f.op h).symm
instance yoneda_full : full (@yoneda C _) :=
{ preimage := λ X Y f, (f.app (op X)) (𝟙 X) }
instance yoneda_faithful : faithful (@yoneda C _) :=
{ injectivity' := λ X Y f g p,
begin
injection p with h,
convert (congr_fun (congr_fun h (op X)) (𝟙 X)); dsimp; simp,
end }
/-- Extensionality via Yoneda. The typical usage would be
```
-- Goal is `X ≅ Y`
apply yoneda.ext,
-- Goals are now functions `(Z ⟶ X) → (Z ⟶ Y)`, `(Z ⟶ Y) → (Z ⟶ X)`, and the fact that these
functions are inverses and natural in `Z`.
```
-/
def ext (X Y : C)
(p : Π {Z : C}, (Z ⟶ X) → (Z ⟶ Y)) (q : Π {Z : C}, (Z ⟶ Y) → (Z ⟶ X))
(h₁ : Π {Z : C} (f : Z ⟶ X), q (p f) = f) (h₂ : Π {Z : C} (f : Z ⟶ Y), p (q f) = f)
(n : Π {Z Z' : C} (f : Z' ⟶ Z) (g : Z ⟶ X), p (f ≫ g) = f ≫ p g) : X ≅ Y :=
@preimage_iso _ _ _ _ yoneda _ _ _ _
(nat_iso.of_components (λ Z, { hom := p, inv := q, }) (by tidy))
def is_iso {X Y : C} (f : X ⟶ Y) [is_iso (yoneda.map f)] : is_iso f :=
is_iso_of_fully_faithful yoneda f
end yoneda
namespace coyoneda
@[simp] lemma naturality {X Y : Cᵒᵖ} (α : coyoneda.obj X ⟶ coyoneda.obj Y)
{Z Z' : C} (f : Z' ⟶ Z) (h : unop X ⟶ Z') : (α.app Z' h) ≫ f = α.app Z (h ≫ f) :=
begin erw [functor_to_types.naturality], refl end
instance coyoneda_full : full (@coyoneda C _) :=
{ preimage := λ X Y f, ((f.app (unop X)) (𝟙 _)).op }
instance coyoneda_faithful : faithful (@coyoneda C _) :=
{ injectivity' := λ X Y f g p,
begin
injection p with h,
have t := (congr_fun (congr_fun h (unop X)) (𝟙 _)),
simpa using congr_arg has_hom.hom.op t,
end }
def is_iso {X Y : Cᵒᵖ} (f : X ⟶ Y) [is_iso (coyoneda.map f)] : is_iso f :=
is_iso_of_fully_faithful coyoneda f
end coyoneda
class representable (F : Cᵒᵖ ⥤ Type v₁) :=
(X : C)
(w : yoneda.obj X ≅ F)
end category_theory
namespace category_theory
-- For the rest of the file, we are using product categories,
-- so need to restrict to the case morphisms are in 'Type', not 'Sort'.
universes v₁ u₁ u₂ -- declare the `v`'s first; see `category_theory.category` for an explanation
open opposite
variables (C : Type u₁) [category.{v₁} C]
-- We need to help typeclass inference with some awkward universe levels here.
instance prod_category_instance_1 : category ((Cᵒᵖ ⥤ Type v₁) × Cᵒᵖ) :=
category_theory.prod.{(max u₁ v₁) v₁} (Cᵒᵖ ⥤ Type v₁) Cᵒᵖ
instance prod_category_instance_2 : category (Cᵒᵖ × (Cᵒᵖ ⥤ Type v₁)) :=
category_theory.prod.{v₁ (max u₁ v₁)} Cᵒᵖ (Cᵒᵖ ⥤ Type v₁)
open yoneda
def yoneda_evaluation : Cᵒᵖ × (Cᵒᵖ ⥤ Type v₁) ⥤ Type (max u₁ v₁) :=
evaluation_uncurried Cᵒᵖ (Type v₁) ⋙ ulift_functor.{u₁}
@[simp] lemma yoneda_evaluation_map_down
(P Q : Cᵒᵖ × (Cᵒᵖ ⥤ Type v₁)) (α : P ⟶ Q) (x : (yoneda_evaluation C).obj P) :
((yoneda_evaluation C).map α x).down = α.2.app Q.1 (P.2.map α.1 x.down) := rfl
def yoneda_pairing : Cᵒᵖ × (Cᵒᵖ ⥤ Type v₁) ⥤ Type (max u₁ v₁) :=
functor.prod yoneda.op (𝟭 (Cᵒᵖ ⥤ Type v₁)) ⋙ functor.hom (Cᵒᵖ ⥤ Type v₁)
@[simp] lemma yoneda_pairing_map
(P Q : Cᵒᵖ × (Cᵒᵖ ⥤ Type v₁)) (α : P ⟶ Q) (β : (yoneda_pairing C).obj P) :
(yoneda_pairing C).map α β = yoneda.map α.1.unop ≫ β ≫ α.2 := rfl
def yoneda_lemma : yoneda_pairing C ≅ yoneda_evaluation C :=
{ hom :=
{ app := λ F x, ulift.up ((x.app F.1) (𝟙 (unop F.1))),
naturality' :=
begin
intros X Y f, ext, dsimp,
erw [category.id_comp, ←functor_to_types.naturality],
simp only [category.comp_id, yoneda_obj_map],
end },
inv :=
{ app := λ F x,
{ app := λ X a, (F.2.map a.op) x.down,
naturality' :=
begin
intros X Y f, ext, dsimp,
rw [functor_to_types.map_comp_apply]
end },
naturality' :=
begin
intros X Y f, ext, dsimp,
rw [←functor_to_types.naturality, functor_to_types.map_comp_apply]
end },
hom_inv_id' :=
begin
ext, dsimp,
erw [←functor_to_types.naturality,
obj_map_id],
simp only [yoneda_map_app, has_hom.hom.unop_op],
erw [category.id_comp],
end,
inv_hom_id' :=
begin
ext, dsimp,
rw [functor_to_types.map_id_apply]
end }.
variables {C}
@[simp] def yoneda_sections (X : C) (F : Cᵒᵖ ⥤ Type v₁) :
(yoneda.obj X ⟶ F) ≅ ulift.{u₁} (F.obj (op X)) :=
(yoneda_lemma C).app (op X, F)
@[simp] def yoneda_sections_small {C : Type u₁} [small_category C] (X : C) (F : Cᵒᵖ ⥤ Type u₁) :
(yoneda.obj X ⟶ F) ≅ F.obj (op X) :=
yoneda_sections X F ≪≫ ulift_trivial _
end category_theory
|
2abc122faeddd379f4a5edbc624129f0b3dd56a6 | 618003631150032a5676f229d13a079ac875ff77 | /src/tactic/monotonicity/basic.lean | 0621864263074be7367526d145c2bcfd9db12259 | [
"Apache-2.0"
] | permissive | awainverse/mathlib | 939b68c8486df66cfda64d327ad3d9165248c777 | ea76bd8f3ca0a8bf0a166a06a475b10663dec44a | refs/heads/master | 1,659,592,962,036 | 1,590,987,592,000 | 1,590,987,592,000 | 268,436,019 | 1 | 0 | Apache-2.0 | 1,590,990,500,000 | 1,590,990,500,000 | null | UTF-8 | Lean | false | false | 5,436 | lean | /-
Copyright (c) 2019 Simon Hudon. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Simon Hudon
-/
import algebra.order_functions
namespace tactic.interactive
open tactic list
open lean lean.parser interactive
open interactive.types
@[derive inhabited]
structure mono_cfg :=
(unify := ff)
@[derive [decidable_eq, has_reflect, inhabited]]
inductive mono_selection : Type
| left : mono_selection
| right : mono_selection
| both : mono_selection
section compare
parameter opt : mono_cfg
meta def compare (e₀ e₁ : expr) : tactic unit := do
if opt.unify then do
guard (¬ e₀.is_mvar ∧ ¬ e₁.is_mvar),
unify e₀ e₁
else is_def_eq e₀ e₁
meta def find_one_difference
: list expr → list expr → tactic (list expr × expr × expr × list expr)
| (x :: xs) (y :: ys) :=
do c ← try_core (compare x y),
if c.is_some
then prod.map (cons x) id <$> find_one_difference xs ys
else do
guard (xs.length = ys.length),
mzip_with' compare xs ys,
return ([],x,y,xs)
| xs ys := fail format!"find_one_difference: {xs}, {ys}"
end compare
def last_two {α : Type*} (l : list α) : option (α × α) :=
match l.reverse with
| (x₁ :: x₀ :: _) := some (x₀, x₁)
| _ := none
end
meta def match_imp : expr → tactic (expr × expr)
| `(%%e₀ → %%e₁) :=
do guard (¬ e₁.has_var),
return (e₀,e₁)
| _ := failed
open expr
meta def same_operator : expr → expr → bool
| (app e₀ _) (app e₁ _) :=
let fn₀ := e₀.get_app_fn,
fn₁ := e₁.get_app_fn in
fn₀.is_constant ∧ fn₀.const_name = fn₁.const_name
| (pi _ _ _ _) (pi _ _ _ _) := tt
| _ _ := ff
meta def get_operator (e : expr) : option name :=
guard (¬ e.is_pi) >> pure e.get_app_fn.const_name
meta def monotoncity.check_rel (xs : list expr) (l r : expr) : tactic (option name) :=
do guard (same_operator l r) <|>
do { fail format!"{l} and {r} should be the f x and f y for some f" },
if l.is_pi then pure none
else pure r.get_app_fn.const_name
@[reducible]
def mono_key := (with_bot name × with_bot name)
open nat
meta def mono_head_candidates : ℕ → list expr → expr → tactic mono_key
| 0 _ h := failed
| (succ n) xs h :=
do { (rel,l,r) ← if h.is_arrow
then pure (none,h.binding_domain,h.binding_body)
else guard h.get_app_fn.is_constant >>
prod.mk (some h.get_app_fn.const_name) <$> last_two h.get_app_args,
prod.mk <$> monotoncity.check_rel xs.reverse l r <*> pure rel } <|>
match xs with
| [] := fail format!"oh? {h}"
| (x::xs) := mono_head_candidates n xs (h.pis [x])
end
meta def monotoncity.check (lm_n : name) (prio : ℕ) (persistent : bool) : tactic mono_key :=
do lm ← mk_const lm_n,
lm_t ← infer_type lm,
(xs,h) ← mk_local_pis lm_t,
mono_head_candidates 3 xs.reverse h
meta instance : has_to_format mono_selection :=
⟨ λ x, match x with
| mono_selection.left := "left"
| mono_selection.right := "right"
| mono_selection.both := "both"
end ⟩
meta def side : lean.parser mono_selection :=
with_desc "expecting 'left', 'right' or 'both' (default)" $
do some n ← optional ident | pure mono_selection.both,
if n = `left then pure $ mono_selection.left
else if n = `right then pure $ mono_selection.right
else if n = `both then pure $ mono_selection.both
else fail format!"invalid argument: {n}, expecting 'left', 'right' or 'both' (default)"
open function
@[user_attribute]
meta def monotonicity.attr : user_attribute
(native.rb_lmap mono_key (name))
(option mono_key × mono_selection) :=
{ name := `mono
, descr := "monotonicity of function `f` wrt relations `R₀` and `R₁`: R₀ x y → R₁ (f x) (f y)"
, cache_cfg :=
{ dependencies := [],
mk_cache := λ ls,
do ps ← ls.mmap monotonicity.attr.get_param,
let ps := ps.filter_map prod.fst,
pure $ (ps.zip ls).foldl
(flip $ uncurry (λ k n m, m.insert k n))
(native.rb_lmap.mk mono_key _) }
, after_set := some $ λ n prio p,
do { (none,v) ← monotonicity.attr.get_param n | pure (),
k ← monotoncity.check n prio p,
monotonicity.attr.set n (some k,v) p }
, parser := prod.mk none <$> side }
meta def filter_instances (e : mono_selection) (ns : list name) : tactic (list name) :=
ns.mfilter $ λ n,
do d ← user_attribute.get_param_untyped monotonicity.attr n,
(_,d) ← to_expr ``(id %%d) >>= eval_expr (option mono_key × mono_selection),
return (e = d : bool)
meta def get_monotonicity_lemmas (k : expr) (e : mono_selection) : tactic (list name) :=
do ns ← monotonicity.attr.get_cache,
k' ← if k.is_pi
then pure (get_operator k.binding_domain,none)
else do { (x₀,x₁) ← last_two k.get_app_args,
pure (get_operator x₀,some k.get_app_fn.const_name) },
let ns := ns.find_def [] k',
ns' ← filter_instances e ns,
if e ≠ mono_selection.both then (++) ns' <$> filter_instances mono_selection.both ns
else pure ns'
end tactic.interactive
attribute [mono] add_le_add mul_le_mul neg_le_neg
mul_lt_mul_of_pos_left mul_lt_mul_of_pos_right
imp_imp_imp le_implies_le_of_le_of_le
sub_le_sub abs_le_abs
attribute [mono left] add_lt_add_of_le_of_lt mul_lt_mul'
attribute [mono right] add_lt_add_of_lt_of_le mul_lt_mul
open tactic.interactive
|
f4bd26aae724162b6f88a0c0376fceff52a514cc | ea11767c9c6a467c4b7710ec6f371c95cfc023fd | /src/monoidal_categories/strict_monoidal_category.lean | 21cf65324f1a74b13faa484410ed55a6633112bf | [] | no_license | RitaAhmadi/lean-monoidal-categories | 68a23f513e902038e44681336b87f659bbc281e0 | 81f43e1e0d623a96695aa8938951d7422d6d7ba6 | refs/heads/master | 1,651,458,686,519 | 1,529,824,613,000 | 1,529,824,613,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 4,221 | lean | -- -- Copyright (c) 2017 Scott Morrison. All rights reserved.
-- -- Released under Apache 2.0 license as described in the file LICENSE.
-- -- Authors: Stephen Morgan, Scott Morrison
-- import .monoidal_category
-- open categories
-- open categories.isomorphism
-- open categories.functor
-- open categories.natural_transformation
-- open categories.products
-- open categories.monoidal_category
-- namespace categories.strict_monoidal_category
-- structure TensorProduct_is_strict { C : Category } ( tensor : TensorProduct C ) ( tensor_unit : C.Obj ) :=
-- ( associativeOnObjects : ∀ X Y Z : C.Obj, tensor.onObjects (tensor.onObjects (X, Y), Z) = tensor.onObjects (X, tensor.onObjects (Y, Z)) )
-- ( strictLeftTensorUnit : ∀ X : C.Obj, tensor.onObjects (tensor_unit, X) = X )
-- ( strictRightTensorUnit : ∀ X : C.Obj, tensor.onObjects (X, tensor_unit) = X )
-- -- TODO why is this not a valid ematch lemma?
-- -- attribute [ematch] TensorProduct_is_strict.associativeOnObjects
-- -- TODO why is this not a valid simp lemma?
-- -- attribute [simp,ematch] TensorProduct_is_strict.strictLeftTensorUnit
-- -- attribute [simp,ematch] TensorProduct_is_strict.strictRightTensorUnit
-- -- set_option pp.implicit true
-- -- definition construct_StrictMonoidalCategory { C : Category } { tensor : TensorProduct C } { tensor_unit : C.Obj } ( is_strict : TensorProduct_is_strict tensor tensor_unit ) : MonoidalStructure C :=
-- -- {
-- -- tensor := {
-- -- onObjects := λ p, tensor.onObjects p,
-- -- onMorphisms := λ _ _ f, tensor.onMorphisms f,
-- -- identities := ♮,
-- -- functoriality := ♮
-- -- },
-- -- tensor_unit := tensor_unit,
-- -- associator_transformation := {
-- -- components := λ t, begin
-- -- induction t with pq r,
-- -- induction pq with p q,
-- -- refine (cast _ (C.identity (tensor.onObjects (tensor.onObjects (p, q), r)))),
-- -- blast, -- TODO Why doesn't blast do the next rewrite?
-- -- rewrite ← is_strict.associativeOnObjects,
-- -- exact sorry
-- -- end,
-- -- naturality := begin
-- -- -- TODO Given how we constructed components, I have no idea how to prove naturality.
-- -- intros,
-- -- dsimp,
-- -- exact sorry
-- -- end
-- -- },
-- -- left_unitor := sorry,
-- -- right_unitor := sorry,
-- -- associator_is_isomorphism := sorry,
-- -- left_unitor_is_isomorphism := sorry,
-- -- right_unitor_is_isomorphism := sorry,
-- -- pentagon := sorry,
-- -- triangle := sorry
-- -- }
-- @[reducible] definition tensorList { C : Category } ( m : MonoidalStructure C ) ( X : list C.Obj ) : C.Obj := list.foldl m.tensorObjects m.tensor_unit X
-- -- @[reducible] definition tensorListConcatenation { C : MonoidalCategory } ( X : list C.Obj × list C.Obj ) : Isomorphism C (C.tensorObjects (tensorList X.1) (tensorList X.2)) (tensorList (append X.1 X.2)) :=
-- -- {
-- -- morphism := sorry,
-- -- inverse := sorry,
-- -- witness_1 := sorry,
-- -- witness_2 := sorry
-- -- }
-- -- @[reducible] definition ListObjectsCategory ( C : MonoidalCategory ) : Category := {
-- -- Obj := list C.Obj,
-- -- Hom := λ X Y, C.Hom (tensorList X) (tensorList Y),
-- -- identity := λ X, C.identity (tensorList X),
-- -- compose := λ _ _ _ f g, C.compose f g,
-- -- left_identity := ♮,
-- -- right_identity := ♮,
-- -- associativity := sorry
-- -- }
-- -- definition StrictTensorProduct ( C : MonoidalCategory ) : TensorProduct (ListObjectsCategory C) := {
-- -- onObjects := λ X, append X.1 X.2,
-- -- onMorphisms := λ X Y f, sorry, -- C.compose (C.compose (tensorListConcatenation X).inverse f) (tensorListConcatenation Y).morphism,
-- -- identities := sorry,
-- -- functoriality := sorry
-- -- }
-- -- PROJECT
-- -- * show that StrictTensorProduct is strict
-- -- * construct a functor from C
-- -- * show that it is part of an equivalence
-- end categories.strict_monoidal_category
|
32b97b711f2517a516d9a8d85c0fda33739ab16d | a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91 | /hott/algebra/category/constructions/discrete.hlean | c2e6b9da09c703755d7600ab610662c845417e83 | [
"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 | 2,830 | 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
Discrete category
-/
import ..groupoid types.bool ..nat_trans
open eq is_trunc iso bool functor nat_trans
namespace category
definition precategory_of_1_type [constructor] (A : Type) [H : is_trunc 1 A] : precategory A :=
@precategory.mk _ _ (@is_trunc_eq _ _ H)
(λ (a b c : A) (p : b = c) (q : a = b), q ⬝ p)
(λ (a : A), refl a)
(λ (a b c d : A) (p : c = d) (q : b = c) (r : a = b), con.assoc r q p)
(λ (a b : A) (p : a = b), con_idp p)
(λ (a b : A) (p : a = b), idp_con p)
definition groupoid_of_1_type [constructor] (A : Type) [H : is_trunc 1 A] : groupoid A :=
groupoid.mk !precategory_of_1_type
(λ (a b : A) (p : a = b), is_iso.mk _ !con.right_inv !con.left_inv)
definition Precategory_of_1_type [constructor] (A : Type) [H : is_trunc 1 A] : Precategory :=
precategory.Mk (precategory_of_1_type A)
definition Groupoid_of_1_type [constructor] (A : Type) [H : is_trunc 1 A] : Groupoid :=
groupoid.Mk _ (groupoid_of_1_type A)
definition discrete_precategory [constructor] (A : Type) [H : is_set A] : precategory A :=
precategory_of_1_type A
definition discrete_groupoid [constructor] (A : Type) [H : is_set A] : groupoid A :=
groupoid_of_1_type A
definition Discrete_precategory [constructor] (A : Type) [H : is_set A] : Precategory :=
precategory.Mk (discrete_precategory A)
definition Discrete_groupoid [constructor] (A : Type) [H : is_set A] : Groupoid :=
groupoid.Mk _ (discrete_groupoid A)
definition c2 [constructor] : Precategory := Discrete_precategory bool
definition c2_functor [constructor] (C : Precategory) (x y : C) : c2 ⇒ C :=
functor.mk (bool.rec x y)
(bool.rec (bool.rec (λf, id) (by contradiction))
(bool.rec (by contradiction) (λf, id)))
abstract (bool.rec idp idp) end
abstract begin intro b₁ b₂ b₃ g f, induction b₁: induction b₂: induction b₃:
esimp at *: try contradiction: exact !id_id⁻¹ end end
definition c2_functor_eta {C : Precategory} (F : c2 ⇒ C) :
c2_functor C (to_fun_ob F ff) (to_fun_ob F tt) = F :=
begin
fapply functor_eq: esimp,
{ intro b, induction b: reflexivity},
{ intro b₁ b₂ p, induction p, induction b₁: esimp; rewrite [id_leftright]; exact !respect_id⁻¹}
end
definition c2_nat_trans [constructor] {C : Precategory} {x y u v : C} (f : x ⟶ u) (g : y ⟶ v) :
c2_functor C x y ⟹ c2_functor C u v :=
begin
fapply nat_trans.mk: esimp,
{ intro b, induction b, exact f, exact g},
{ intro b₁ b₂ p, induction p, induction b₁: esimp: apply id_comp_eq_comp_id},
end
end category
|
64deb5a7fbbbf8f7a4b03bd616aeb089883cf46a | 3dd1b66af77106badae6edb1c4dea91a146ead30 | /tests/lean/run/ind0.lean | 7730dcf30d85e6d23f924c3679250651cd1245f7 | [
"Apache-2.0"
] | permissive | silky/lean | 79c20c15c93feef47bb659a2cc139b26f3614642 | df8b88dca2f8da1a422cb618cd476ef5be730546 | refs/heads/master | 1,610,737,587,697 | 1,406,574,534,000 | 1,406,574,534,000 | 22,362,176 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 86 | lean | inductive nat : Type :=
| zero : nat
| succ : nat → nat
check nat
check nat_rec.{1} |
8112a49909de99ee0eb7ed3d654e04f622141393 | e030b0259b777fedcdf73dd966f3f1556d392178 | /library/init/data/int/basic.lean | 4021bc08e555b9da688d473ba0050e7f0b7a9320 | [
"Apache-2.0"
] | permissive | fgdorais/lean | 17b46a095b70b21fa0790ce74876658dc5faca06 | c3b7c54d7cca7aaa25328f0a5660b6b75fe26055 | refs/heads/master | 1,611,523,590,686 | 1,484,412,902,000 | 1,484,412,902,000 | 38,489,734 | 0 | 0 | null | 1,435,923,380,000 | 1,435,923,379,000 | null | UTF-8 | Lean | false | false | 17,229 | 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.
-/
prelude
import init.data.nat.lemmas
open nat
/- the type, coercions, and notation -/
inductive int : Type
| of_nat : nat → int
| neg_succ_of_nat : nat → int
notation `ℤ` := int
instance : has_coe nat int := ⟨int.of_nat⟩
notation `-[1+ ` n `]` := int.neg_succ_of_nat n
instance : decidable_eq int :=
by tactic.mk_dec_eq_instance
protected def int.to_string : int → string
| (int.of_nat n) := to_string n
| (int.neg_succ_of_nat n) := "-" ++ to_string (succ n)
instance : has_to_string int :=
⟨int.to_string⟩
namespace int
protected lemma coe_nat_eq (n : ℕ) : ↑n = int.of_nat n := rfl
protected def zero : ℤ := of_nat 0
protected def one : ℤ := of_nat 1
instance : has_zero ℤ := ⟨int.zero⟩
instance : has_one ℤ := ⟨int.one⟩
lemma of_nat_zero : of_nat (0 : nat) = (0 : int) := rfl
lemma of_nat_one : of_nat (1 : nat) = (1 : int) := rfl
/- definitions of basic functions -/
def neg_of_nat : ℕ → ℤ
| 0 := 0
| (succ m) := -[1+ m]
def sub_nat_nat (m n : ℕ) : ℤ :=
match (n - m : nat) with
| 0 := of_nat (m - n) -- m ≥ n
| (succ k) := -[1+ k] -- m < n, and n - m = succ k
end
private lemma sub_nat_nat_of_sub_eq_zero {m n : ℕ} (h : n - m = 0) :
sub_nat_nat m n = of_nat (m - n) :=
begin unfold sub_nat_nat, rw h, unfold sub_nat_nat._match_1, reflexivity end
private lemma sub_nat_nat_of_sub_eq_succ {m n k : ℕ} (h : n - m = succ k) :
sub_nat_nat m n = -[1+ k] :=
begin unfold sub_nat_nat, rw h, unfold sub_nat_nat._match_1, reflexivity end
protected def neg : ℤ → ℤ
| (of_nat n) := neg_of_nat n
| -[1+ n] := succ n
protected def add : ℤ → ℤ → ℤ
| (of_nat m) (of_nat n) := of_nat (m + n)
| (of_nat m) -[1+ n] := sub_nat_nat m (succ n)
| -[1+ m] (of_nat n) := sub_nat_nat n (succ m)
| -[1+ m] -[1+ n] := -[1+ succ (m + n)]
protected def mul : ℤ → ℤ → ℤ
| (of_nat m) (of_nat n) := of_nat (m * n)
| (of_nat m) -[1+ n] := neg_of_nat (m * succ n)
| -[1+ m] (of_nat n) := neg_of_nat (succ m * n)
| -[1+ m] -[1+ n] := of_nat (succ m * succ n)
instance : has_neg ℤ := ⟨int.neg⟩
instance : has_add ℤ := ⟨int.add⟩
instance : has_mul ℤ := ⟨int.mul⟩
lemma of_nat_add (n m : ℕ) : of_nat (n + m) = of_nat n + of_nat m := rfl
lemma of_nat_mul (n m : ℕ) : of_nat (n * m) = of_nat n * of_nat m := rfl
lemma of_nat_succ (n : ℕ) : of_nat (succ n) = of_nat n + 1 := rfl
lemma neg_of_nat_zero : -(of_nat 0) = 0 := rfl
lemma neg_of_nat_of_succ (n : ℕ) : -(of_nat (succ n)) = -[1+ n] := rfl
lemma neg_neg_of_nat_succ (n : ℕ) : -(-[1+ n]) = of_nat (succ n) := rfl
lemma of_nat_eq_coe (n : ℕ) : of_nat n = ↑n := rfl
lemma neg_succ_of_nat_coe (n : ℕ) : -[1+ n] = -↑(n + 1) := rfl
protected lemma coe_nat_add (m n : ℕ) : (↑(m + n) : ℤ) = ↑m + ↑n := rfl
protected lemma coe_nat_mul (m n : ℕ) : (↑(m * n) : ℤ) = ↑m * ↑n := rfl
protected lemma coe_nat_zero : ↑(0 : ℕ) = (0 : ℤ) := rfl
protected lemma coe_nat_one : ↑(1 : ℕ) = (1 : ℤ) := rfl
protected lemma coe_nat_succ (n : ℕ) : (↑(succ n) : ℤ) = ↑n + 1 := rfl
protected lemma coe_nat_add_out (m n : ℕ) : ↑m + ↑n = (m + n : ℤ) := rfl
protected lemma coe_nat_mul_out (m n : ℕ) : ↑m * ↑n = (↑(m * n) : ℤ) := rfl
protected lemma coe_nat_add_one_out (n : ℕ) : ↑n + (1 : ℤ) = ↑(succ n) := rfl
/- these are only for internal use -/
private lemma of_nat_add_of_nat (m n : nat) : of_nat m + of_nat n = of_nat (m + n) := rfl
private lemma of_nat_add_neg_succ_of_nat (m n : nat) :
of_nat m + -[1+ n] = sub_nat_nat m (succ n) := rfl
private lemma neg_succ_of_nat_add_of_nat (m n : nat) :
-[1+ m] + of_nat n = sub_nat_nat n (succ m) := rfl
private lemma neg_succ_of_nat_add_neg_succ_of_nat (m n : nat) :
-[1+ m] + -[1+ n] = -[1+ succ (m + n)] := rfl
private lemma of_nat_mul_of_nat (m n : nat) : of_nat m * of_nat n = of_nat (m * n) := rfl
private lemma of_nat_mul_neg_succ_of_nat (m n : nat) :
of_nat m * -[1+ n] = neg_of_nat (m * succ n) := rfl
private lemma neg_succ_of_nat_of_nat (m n : nat) :
-[1+ m] * of_nat n = neg_of_nat (succ m * n) := rfl
private lemma mul_neg_succ_of_nat_neg_succ_of_nat (m n : nat) :
-[1+ m] * -[1+ n] = of_nat (succ m * succ n) := rfl
local attribute [simp] of_nat_add_of_nat of_nat_mul_of_nat neg_of_nat_zero neg_of_nat_of_succ
neg_neg_of_nat_succ of_nat_add_neg_succ_of_nat neg_succ_of_nat_add_of_nat
neg_succ_of_nat_add_neg_succ_of_nat of_nat_mul_neg_succ_of_nat neg_succ_of_nat_of_nat
mul_neg_succ_of_nat_neg_succ_of_nat
/- some basic functions and properties -/
protected lemma of_nat_inj {m n : ℕ} (h : of_nat m = of_nat n) : m = n :=
int.no_confusion h id
protected lemma coe_nat_inj {m n : ℕ} (h : (↑m : ℤ) = ↑n) : m = n :=
int.of_nat_inj h
lemma of_nat_eq_of_nat_iff (m n : ℕ) : of_nat m = of_nat n ↔ m = n :=
iff.intro int.of_nat_inj (congr_arg _)
protected lemma coe_nat_eq_coe_nat_iff (m n : ℕ) : (↑m : ℤ) = ↑n ↔ m = n :=
of_nat_eq_of_nat_iff m n
lemma neg_succ_of_nat_inj {m n : ℕ} (h : neg_succ_of_nat m = neg_succ_of_nat n) : m = n :=
int.no_confusion h id
lemma neg_succ_of_nat_eq (n : ℕ) : -[1+ n] = -(n + 1) := rfl
private lemma sub_nat_nat_of_ge {m n : ℕ} (h : m ≥ n) : sub_nat_nat m n = of_nat (m - n) :=
sub_nat_nat_of_sub_eq_zero (sub_eq_zero_of_le h)
private lemma sub_nat_nat_of_lt {m n : ℕ} (h : m < n) : sub_nat_nat m n = -[1+ pred (n - m)] :=
have n - m = succ (pred (n - m)), from eq.symm (succ_pred_eq_of_pos (nat.sub_pos_of_lt h)),
by rewrite sub_nat_nat_of_sub_eq_succ this
def nat_abs (a : ℤ) : ℕ := int.cases_on a id succ
lemma nat_abs_of_nat (n : ℕ) : nat_abs ↑n = n := rfl
lemma eq_zero_of_nat_abs_eq_zero : Π {a : ℤ}, nat_abs a = 0 → a = 0
| (of_nat m) H := congr_arg of_nat H
| -[1+ m'] H := absurd H (succ_ne_zero _)
lemma nat_abs_zero : nat_abs (0 : int) = (0 : nat) := rfl
lemma nat_abs_one : nat_abs (1 : int) = (1 : nat) := rfl
/-
int is a ring
-/
/- addition -/
protected lemma add_comm : ∀ a b : ℤ, a + b = b + a
| (of_nat n) (of_nat m) := by simp
| (of_nat n) -[1+ m] := rfl
| -[1+ n] (of_nat m) := rfl
| -[1+ n] -[1+m] := by simp
protected lemma add_zero : ∀ a : ℤ, a + 0 = a
| (of_nat n) := rfl
| -[1+ n] := rfl
protected lemma zero_add (a : ℤ) : 0 + a = a :=
int.add_comm a 0 ▸ int.add_zero a
private lemma sub_nat_nat_add_add (m n k : ℕ) : sub_nat_nat (m + k) (n + k) = sub_nat_nat m n :=
or.elim (le_or_gt n m)
(assume h : n ≤ m,
have n + k ≤ m + k, from nat.add_le_add_right h k,
begin rw [sub_nat_nat_of_ge h, sub_nat_nat_of_ge this, nat.add_sub_add_right] end)
(assume h : n > m,
have n + k > m + k, from nat.add_lt_add_right h k,
by rw [sub_nat_nat_of_lt h, sub_nat_nat_of_lt this, nat.add_sub_add_right])
private lemma sub_nat_nat_sub {m n : ℕ} (h : m ≥ n) (k : ℕ) :
sub_nat_nat (m - n) k = sub_nat_nat m (k + n) :=
calc
sub_nat_nat (m - n) k = sub_nat_nat (m - n + n) (k + n) : by rewrite sub_nat_nat_add_add
... = sub_nat_nat m (k + n) : by rewrite [nat.sub_add_cancel h]
private lemma sub_nat_nat_add (m n k : ℕ) : sub_nat_nat (m + n) k = of_nat m + sub_nat_nat n k :=
begin
note h := le_or_gt k n,
cases h with h' h',
{ rw [sub_nat_nat_of_ge h'],
assert h₂ : k ≤ m + n, exact (le_trans h' (le_add_left _ _)),
rw [sub_nat_nat_of_ge h₂], simp,
rw nat.add_sub_assoc h' },
rw [sub_nat_nat_of_lt h'], simp, rw [succ_pred_eq_of_pos (nat.sub_pos_of_lt h')],
transitivity, rw [-nat.sub_add_cancel (le_of_lt h')],
apply sub_nat_nat_add_add
end
private lemma sub_nat_nat_add_neg_succ_of_nat (m n k : ℕ) :
sub_nat_nat m n + -[1+ k] = sub_nat_nat m (n + succ k) :=
begin
note h := le_or_gt n m,
cases h with h' h',
{ rw [sub_nat_nat_of_ge h'], simp, rw [sub_nat_nat_sub h', add_comm] },
assert h₂ : m < n + succ k, exact nat.lt_of_lt_of_le h' (le_add_right _ _),
assert h₃ : m ≤ n + k, exact le_of_succ_le_succ h₂,
rw [sub_nat_nat_of_lt h', sub_nat_nat_of_lt h₂], simp,
rw [-add_succ, succ_pred_eq_of_pos (nat.sub_pos_of_lt h'), add_succ, succ_sub h₃, pred_succ],
rw [add_comm n, nat.add_sub_assoc (le_of_lt h')]
end
private lemma add_assoc_aux1 (m n : ℕ) :
∀ c : ℤ, of_nat m + of_nat n + c = of_nat m + (of_nat n + c)
| (of_nat k) := by simp
| -[1+ k] := by simp [sub_nat_nat_add]
private lemma add_assoc_aux2 (m n k : ℕ) :
-[1+ m] + -[1+ n] + of_nat k = -[1+ m] + (-[1+ n] + of_nat k) :=
begin
simp [add_succ], rw [int.add_comm, sub_nat_nat_add_neg_succ_of_nat], simp [add_succ, succ_add]
end
protected lemma add_assoc : ∀ a b c : ℤ, a + b + c = a + (b + c)
| (of_nat m) (of_nat n) c := add_assoc_aux1 _ _ _
| (of_nat m) b (of_nat k) := by rw [int.add_comm, -add_assoc_aux1, int.add_comm (of_nat k),
add_assoc_aux1, int.add_comm b]
| a (of_nat n) (of_nat k) := by rw [int.add_comm, int.add_comm a, -add_assoc_aux1,
int.add_comm a, int.add_comm (of_nat k)]
| -[1+ m] -[1+ n] (of_nat k) := add_assoc_aux2 _ _ _
| -[1+ m] (of_nat n) -[1+ k] := by rw [int.add_comm, -add_assoc_aux2, int.add_comm (of_nat n),
-add_assoc_aux2, int.add_comm -[1+ m] ]
| (of_nat m) -[1+ n] -[1+ k] := by rw [int.add_comm, int.add_comm (of_nat m),
int.add_comm (of_nat m), -add_assoc_aux2,
int.add_comm -[1+ k] ]
| -[1+ m] -[1+ n] -[1+ k] := by simp [add_succ, neg_of_nat_of_succ]
/- negation -/
private lemma sub_nat_self : ∀ n, sub_nat_nat n n = 0
| 0 := rfl
| (succ m) := begin rw [sub_nat_nat_of_sub_eq_zero, nat.sub_self, of_nat_zero], rw nat.sub_self end
local attribute [simp] sub_nat_self
protected lemma add_left_inv : ∀ a : ℤ, -a + a = 0
| (of_nat 0) := rfl
| (of_nat (succ m)) := by simp
| -[1+ m] := by simp
/- multiplication -/
protected lemma mul_comm : ∀ a b : ℤ, a * b = b * a
| (of_nat m) (of_nat n) := by simp
| (of_nat m) -[1+ n] := by simp
| -[1+ m] (of_nat n) := by simp
| -[1+ m] -[1+ n] := by simp
private lemma of_nat_mul_neg_of_nat (m : ℕ) :
∀ n, of_nat m * neg_of_nat n = neg_of_nat (m * n)
| 0 := rfl
| (succ n) := begin unfold neg_of_nat, simp end
private lemma neg_of_nat_mul_of_nat (m n : ℕ) :
neg_of_nat m * of_nat n = neg_of_nat (m * n) :=
begin rw int.mul_comm, simp [of_nat_mul_neg_of_nat] end
private lemma neg_succ_of_nat_mul_neg_of_nat (m : ℕ) :
∀ n, -[1+ m] * neg_of_nat n = of_nat (succ m * n)
| 0 := rfl
| (succ n) := begin unfold neg_of_nat, simp end
private lemma neg_of_nat_mul_neg_succ_of_nat (m n : ℕ) :
neg_of_nat n * -[1+ m] = of_nat (n * succ m) :=
begin rw int.mul_comm, simp [neg_succ_of_nat_mul_neg_of_nat] end
local attribute [simp] of_nat_mul_neg_of_nat neg_of_nat_mul_of_nat
neg_succ_of_nat_mul_neg_of_nat neg_of_nat_mul_neg_succ_of_nat
protected lemma mul_assoc : ∀ a b c : ℤ, a * b * c = a * (b * c)
| (of_nat m) (of_nat n) (of_nat k) := by simp
| (of_nat m) (of_nat n) -[1+ k] := by simp
| (of_nat m) -[1+ n] (of_nat k) := by simp
| (of_nat m) -[1+ n] -[1+ k] := by simp
| -[1+ m] (of_nat n) (of_nat k) := by simp
| -[1+ m] (of_nat n) -[1+ k] := by simp
| -[1+ m] -[1+ n] (of_nat k) := by simp
| -[1+ m] -[1+ n] -[1+ k] := by simp
protected lemma mul_one : ∀ (a : ℤ), a * 1 = a
| (of_nat m) := show of_nat m * of_nat 1 = of_nat m, by simp
| -[1+ m] := show -[1+ m] * of_nat 1 = -[1+ m], begin simp, reflexivity end
protected lemma one_mul (a : ℤ) : 1 * a = a :=
int.mul_comm a 1 ▸ int.mul_one a
protected lemma mul_zero : ∀ (a : ℤ), a * 0 = 0
| (of_nat m) := begin unfold zero, reflexivity end
| -[1+ m] := begin unfold zero, reflexivity end
protected lemma zero_mul (a : ℤ) : 0 * a = 0 :=
int.mul_comm a 0 ▸ int.mul_zero a
private lemma neg_of_nat_eq_sub_nat_nat_zero : ∀ n, neg_of_nat n = sub_nat_nat 0 n
| 0 := rfl
| (succ n) := rfl
private lemma of_nat_mul_sub_nat_nat (m n k : ℕ) :
of_nat m * sub_nat_nat n k = sub_nat_nat (m * n) (m * k) :=
begin
assert h₀ : m > 0 ∨ 0 = m,
exact lt_or_eq_of_le (zero_le _),
cases h₀ with h₀ h₀,
{ note h := nat.lt_or_ge n k,
cases h with h h,
{ assert h' : m * n < m * k,
exact nat.mul_lt_mul_of_pos_left h h₀,
rw [sub_nat_nat_of_lt h, sub_nat_nat_of_lt h'],
simp, rw [succ_pred_eq_of_pos (nat.sub_pos_of_lt h)],
rw [-neg_of_nat_of_succ, nat.mul_sub_left_distrib],
rw [-succ_pred_eq_of_pos (nat.sub_pos_of_lt h')], reflexivity },
assert h' : m * k ≤ m * n,
exact mul_le_mul_left _ h,
rw [sub_nat_nat_of_ge h, sub_nat_nat_of_ge h'], simp,
rw [nat.mul_sub_left_distrib]
},
assert h₂ : of_nat 0 = 0, exact rfl,
subst h₀, simp [h₂, int.zero_mul]
end
private lemma neg_of_nat_add (m n : ℕ) :
neg_of_nat m + neg_of_nat n = neg_of_nat (m + n) :=
begin
cases m,
{ cases n,
{ simp, reflexivity },
simp, reflexivity },
cases n,
{ simp, reflexivity },
simp [nat.succ_add], reflexivity
end
private lemma neg_succ_of_nat_mul_sub_nat_nat (m n k : ℕ) :
-[1+ m] * sub_nat_nat n k = sub_nat_nat (succ m * k) (succ m * n) :=
begin
note h := nat.lt_or_ge n k,
cases h with h h,
{ assert h' : succ m * n < succ m * k,
exact nat.mul_lt_mul_of_pos_left h (nat.succ_pos m),
rw [sub_nat_nat_of_lt h, sub_nat_nat_of_ge (le_of_lt h')],
simp [succ_pred_eq_of_pos (nat.sub_pos_of_lt h), nat.mul_sub_left_distrib]},
assert h' : n > k ∨ k = n,
exact lt_or_eq_of_le h,
cases h' with h' h',
{ assert h₁ : succ m * n > succ m * k,
exact nat.mul_lt_mul_of_pos_left h' (nat.succ_pos m),
rw [sub_nat_nat_of_ge h, sub_nat_nat_of_lt h₁], simp [nat.mul_sub_left_distrib],
rw [nat.mul_comm k, nat.mul_comm n, -succ_pred_eq_of_pos (nat.sub_pos_of_lt h₁),
-neg_of_nat_of_succ],
reflexivity },
subst h', simp, reflexivity
end
local attribute [simp] of_nat_mul_sub_nat_nat neg_of_nat_add neg_succ_of_nat_mul_sub_nat_nat
protected lemma distrib_left : ∀ a b c : ℤ, a * (b + c) = a * b + a * c
| (of_nat m) (of_nat n) (of_nat k) := by simp [nat.left_distrib]
| (of_nat m) (of_nat n) -[1+ k] := begin simp [neg_of_nat_eq_sub_nat_nat_zero],
rw -sub_nat_nat_add, reflexivity end
| (of_nat m) -[1+ n] (of_nat k) := begin simp [neg_of_nat_eq_sub_nat_nat_zero],
rw [int.add_comm, -sub_nat_nat_add], reflexivity end
| (of_nat m) -[1+ n] -[1+ k] := begin simp, rw [-nat.left_distrib, succ_add], reflexivity end
| -[1+ m] (of_nat n) (of_nat k) := begin simp, rw [-nat.right_distrib, mul_comm] end
| -[1+ m] (of_nat n) -[1+ k] := begin simp [neg_of_nat_eq_sub_nat_nat_zero],
rw [int.add_comm, -sub_nat_nat_add], reflexivity end
| -[1+ m] -[1+ n] (of_nat k) := begin simp [neg_of_nat_eq_sub_nat_nat_zero],
rw [-sub_nat_nat_add], reflexivity end
| -[1+ m] -[1+ n] -[1+ k] := begin simp, rw [-nat.left_distrib, succ_add], reflexivity end
protected lemma distrib_right (a b c : ℤ) : (a + b) * c = a * c + b * c :=
begin rw [int.mul_comm, int.distrib_left], simp [int.mul_comm] end
instance : comm_ring int :=
{ add := int.add,
add_assoc := int.add_assoc,
zero := int.zero,
zero_add := int.zero_add,
add_zero := int.add_zero,
neg := int.neg,
add_left_inv := int.add_left_inv,
add_comm := int.add_comm,
mul := int.mul,
mul_assoc := int.mul_assoc,
one := int.one,
one_mul := int.one_mul,
mul_one := int.mul_one,
left_distrib := int.distrib_left,
right_distrib := int.distrib_right,
mul_comm := int.mul_comm }
/- Extra instances to short-circuit type class resolution -/
instance : has_sub int := by apply_instance
instance : add_comm_monoid int := by apply_instance
instance : add_monoid int := by apply_instance
instance : monoid int := by apply_instance
instance : comm_monoid int := by apply_instance
instance : comm_semigroup int := by apply_instance
instance : semigroup int := by apply_instance
instance : add_comm_semigroup int := by apply_instance
instance : add_semigroup int := by apply_instance
instance : comm_semiring int := by apply_instance
instance : semiring int := by apply_instance
instance : ring int := by apply_instance
instance : distrib int := by apply_instance
protected lemma zero_ne_one : (0 : int) ≠ 1 :=
assume h : 0 = 1, succ_ne_zero _ (int.of_nat_inj h)^.symm
end int
|
89d4a17b7960ed8d222c87fdfb6a8b97e6ad386c | 75db7e3219bba2fbf41bf5b905f34fcb3c6ca3f2 | /hott/types/bool.hlean | 7a316329ef677b68e6839441898a1c46f3735bab | [
"Apache-2.0"
] | permissive | jroesch/lean | 30ef0860fa905d35b9ad6f76de1a4f65c9af6871 | 3de4ec1a6ce9a960feb2a48eeea8b53246fa34f2 | refs/heads/master | 1,586,090,835,348 | 1,455,142,203,000 | 1,455,142,277,000 | 51,536,958 | 1 | 0 | null | 1,455,215,811,000 | 1,455,215,811,000 | null | UTF-8 | Lean | false | false | 4,927 | hlean | /-
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura, Floris van Doorn
Partially ported from the standard library
-/
open eq eq.ops decidable
namespace bool
local attribute bor [reducible]
local attribute band [reducible]
theorem dichotomy (b : bool) : b = ff ⊎ b = tt :=
bool.cases_on b (sum.inl rfl) (sum.inr rfl)
theorem cond_ff {A : Type} (t e : A) : cond ff t e = e :=
rfl
theorem cond_tt {A : Type} (t e : A) : cond tt t e = t :=
rfl
theorem eq_tt_of_ne_ff : Π {a : bool}, a ≠ ff → a = tt
| @eq_tt_of_ne_ff tt H := rfl
| @eq_tt_of_ne_ff ff H := absurd rfl H
theorem eq_ff_of_ne_tt : Π {a : bool}, a ≠ tt → a = ff
| @eq_ff_of_ne_tt tt H := absurd rfl H
| @eq_ff_of_ne_tt ff H := rfl
theorem absurd_of_eq_ff_of_eq_tt {B : Type} {a : bool} (H₁ : a = ff) (H₂ : a = tt) : B :=
absurd (H₁⁻¹ ⬝ H₂) ff_ne_tt
theorem tt_bor (a : bool) : bor tt a = tt :=
rfl
notation a || b := bor a b
theorem bor_tt (a : bool) : a || tt = tt :=
bool.cases_on a rfl rfl
theorem ff_bor (a : bool) : ff || a = a :=
bool.cases_on a rfl rfl
theorem bor_ff (a : bool) : a || ff = a :=
bool.cases_on a rfl rfl
theorem bor_self (a : bool) : a || a = a :=
bool.cases_on a rfl rfl
theorem bor.comm (a b : bool) : a || b = b || a :=
by cases a; repeat (cases b | reflexivity)
theorem bor.assoc (a b c : bool) : (a || b) || c = a || (b || c) :=
match a with
| ff := by rewrite *ff_bor
| tt := by rewrite *tt_bor
end
theorem or_of_bor_eq {a b : bool} : a || b = tt → a = tt ⊎ b = tt :=
bool.rec_on a
(suppose ff || b = tt,
have b = tt, from !ff_bor ▸ this,
sum.inr this)
(suppose tt || b = tt,
sum.inl rfl)
theorem bor_inl {a b : bool} (H : a = tt) : a || b = tt :=
by rewrite H
theorem bor_inr {a b : bool} (H : b = tt) : a || b = tt :=
bool.rec_on a (by rewrite H) (by rewrite H)
theorem ff_band (a : bool) : ff && a = ff :=
rfl
theorem tt_band (a : bool) : tt && a = a :=
bool.cases_on a rfl rfl
theorem band_ff (a : bool) : a && ff = ff :=
bool.cases_on a rfl rfl
theorem band_tt (a : bool) : a && tt = a :=
bool.cases_on a rfl rfl
theorem band_self (a : bool) : a && a = a :=
bool.cases_on a rfl rfl
theorem band.comm (a b : bool) : a && b = b && a :=
bool.cases_on a
(bool.cases_on b rfl rfl)
(bool.cases_on b rfl rfl)
theorem band.assoc (a b c : bool) : (a && b) && c = a && (b && c) :=
match a with
| ff := by rewrite *ff_band
| tt := by rewrite *tt_band
end
theorem band_elim_left {a b : bool} (H : a && b = tt) : a = tt :=
sum.elim (dichotomy a)
(suppose a = ff,
absurd
(calc ff = ff && b : ff_band
... = a && b : this
... = tt : H)
ff_ne_tt)
(suppose a = tt, this)
theorem band_intro {a b : bool} (H₁ : a = tt) (H₂ : b = tt) : a && b = tt :=
by rewrite [H₁, H₂]
theorem band_elim_right {a b : bool} (H : a && b = tt) : b = tt :=
band_elim_left (!band.comm ⬝ H)
theorem bnot_bnot (a : bool) : bnot (bnot a) = a :=
bool.cases_on a rfl rfl
theorem bnot_empty : bnot ff = tt :=
rfl
theorem bnot_unit : bnot tt = ff :=
rfl
theorem eq_tt_of_bnot_eq_ff {a : bool} : bnot a = ff → a = tt :=
bool.cases_on a (by contradiction) (λ h, rfl)
theorem eq_ff_of_bnot_eq_tt {a : bool} : bnot a = tt → a = ff :=
bool.cases_on a (λ h, rfl) (by contradiction)
definition bxor (x:bool) (y:bool) := cond x (bnot y) y
/- HoTT-related stuff -/
open is_equiv equiv function is_trunc option unit decidable
definition is_equiv_bnot [constructor] [instance] [priority 500] : is_equiv bnot :=
begin
fapply is_equiv.mk,
exact bnot,
all_goals (intro b;cases b), do 6 reflexivity
-- all_goals (focus (intro b;cases b;all_goals reflexivity)),
end
definition bnot_ne : Π(b : bool), bnot b ≠ b
| bnot_ne tt := ff_ne_tt
| bnot_ne ff := ne.symm ff_ne_tt
definition equiv_bnot [constructor] : bool ≃ bool := equiv.mk bnot _
definition eq_bnot : bool = bool := ua equiv_bnot
definition eq_bnot_ne_idp : eq_bnot ≠ idp :=
assume H : eq_bnot = idp,
assert H2 : bnot = id, from !cast_ua_fn⁻¹ ⬝ ap cast H,
absurd (ap10 H2 tt) ff_ne_tt
theorem is_hset_bool : is_hset bool := _
theorem not_is_hprop_bool_eq_bool : ¬ is_hprop (bool = bool) :=
λ H, eq_bnot_ne_idp !is_hprop.elim
definition bool_equiv_option_unit [constructor] : bool ≃ option unit :=
begin
fapply equiv.MK,
{ intro b, cases b, exact none, exact some star},
{ intro u, cases u, exact ff, exact tt},
{ intro u, cases u with u, reflexivity, cases u, reflexivity},
{ intro b, cases b, reflexivity, reflexivity},
end
definition tbool [constructor] : hset := trunctype.mk bool _
end bool
|
a123ed4a8481efba8cd4c1cb09f819e1fe6ff3d6 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/measure_theory/function/strongly_measurable/lp.lean | 296d54884495b03dfc1ca032007e720948b187e5 | [
"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 | 2,716 | lean | /-
Copyright (c) 2022 Rémy Degenne. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Rémy Degenne
-/
import measure_theory.function.simple_func_dense_lp
import measure_theory.function.strongly_measurable.basic
/-!
# Finitely strongly measurable functions in `Lp`
Functions in `Lp` for `0 < p < ∞` are finitely strongly measurable.
## Main statements
* `mem_ℒp.ae_fin_strongly_measurable`: if `mem_ℒp f p μ` with `0 < p < ∞`, then
`ae_fin_strongly_measurable f μ`.
* `Lp.fin_strongly_measurable`: for `0 < p < ∞`, `Lp` functions are finitely strongly measurable.
## References
* Hytönen, Tuomas, Jan Van Neerven, Mark Veraar, and Lutz Weis. Analysis in Banach spaces.
Springer, 2016.
-/
open measure_theory filter topological_space function
open_locale ennreal topological_space measure_theory
namespace measure_theory
local infixr ` →ₛ `:25 := simple_func
variables {α G : Type*} {p : ℝ≥0∞} {m m0 : measurable_space α} {μ : measure α}
[normed_add_comm_group G]
{f : α → G}
lemma mem_ℒp.fin_strongly_measurable_of_strongly_measurable
(hf : mem_ℒp f p μ) (hf_meas : strongly_measurable f) (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) :
fin_strongly_measurable f μ :=
begin
borelize G,
haveI : separable_space (set.range f ∪ {0} : set G) :=
hf_meas.separable_space_range_union_singleton,
let fs := simple_func.approx_on f hf_meas.measurable (set.range f ∪ {0}) 0 (by simp),
refine ⟨fs, _, _⟩,
{ have h_fs_Lp : ∀ n, mem_ℒp (fs n) p μ,
from simple_func.mem_ℒp_approx_on_range hf_meas.measurable hf,
exact λ n, (fs n).measure_support_lt_top_of_mem_ℒp (h_fs_Lp n) hp_ne_zero hp_ne_top },
{ assume x,
apply simple_func.tendsto_approx_on,
apply subset_closure,
simp },
end
lemma mem_ℒp.ae_fin_strongly_measurable (hf : mem_ℒp f p μ) (hp_ne_zero : p ≠ 0)
(hp_ne_top : p ≠ ∞) :
ae_fin_strongly_measurable f μ :=
⟨hf.ae_strongly_measurable.mk f, ((mem_ℒp_congr_ae hf.ae_strongly_measurable.ae_eq_mk).mp hf)
.fin_strongly_measurable_of_strongly_measurable
hf.ae_strongly_measurable.strongly_measurable_mk hp_ne_zero hp_ne_top,
hf.ae_strongly_measurable.ae_eq_mk⟩
lemma integrable.ae_fin_strongly_measurable (hf : integrable f μ) :
ae_fin_strongly_measurable f μ :=
(mem_ℒp_one_iff_integrable.mpr hf).ae_fin_strongly_measurable one_ne_zero ennreal.coe_ne_top
lemma Lp.fin_strongly_measurable (f : Lp G p μ) (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) :
fin_strongly_measurable f μ :=
(Lp.mem_ℒp f).fin_strongly_measurable_of_strongly_measurable
(Lp.strongly_measurable f) hp_ne_zero hp_ne_top
end measure_theory
|
d5b16642a42febf562c7494d74ff35b9a8728699 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/analysis/special_functions/stirling.lean | 488a468a7c81a83fbb29163af814916ddb50deba | [
"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 | 11,261 | lean | /-
Copyright (c) 2022. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Moritz Firsching, Fabian Kruse, Nikolas Kuhn
-/
import analysis.p_series
import data.real.pi.wallis
/-!
# Stirling's formula
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
This file proves Stirling's formula for the factorial.
It states that $n!$ grows asymptotically like $\sqrt{2\pi n}(\frac{n}{e})^n$.
## Proof outline
The proof follows: <https://proofwiki.org/wiki/Stirling%27s_Formula>.
We proceed in two parts.
**Part 1**: We consider the sequence $a_n$ of fractions $\frac{n!}{\sqrt{2n}(\frac{n}{e})^n}$
and prove that this sequence converges to a real, positive number $a$. For this the two main
ingredients are
- taking the logarithm of the sequence and
- using the series expansion of $\log(1 + x)$.
**Part 2**: We use the fact that the series defined in part 1 converges againt a real number $a$
and prove that $a = \sqrt{\pi}$. Here the main ingredient is the convergence of Wallis' product
formula for `π`.
-/
open_locale topology real big_operators nat
open finset filter nat real
namespace stirling
/-!
### Part 1
https://proofwiki.org/wiki/Stirling%27s_Formula#Part_1
-/
/--
Define `stirling_seq n` as $\frac{n!}{\sqrt{2n}(\frac{n}{e})^n}$.
Stirling's formula states that this sequence has limit $\sqrt(π)$.
-/
noncomputable def stirling_seq (n : ℕ) : ℝ :=
n! / (sqrt (2 * n) * (n / exp 1) ^ n)
@[simp] lemma stirling_seq_zero : stirling_seq 0 = 0 :=
by rw [stirling_seq, cast_zero, mul_zero, real.sqrt_zero, zero_mul, div_zero]
@[simp] lemma stirling_seq_one : stirling_seq 1 = exp 1 / sqrt 2 :=
by rw [stirling_seq, pow_one, factorial_one, cast_one, mul_one, mul_one_div, one_div_div]
/--
We have the expression
`log (stirling_seq (n + 1)) = log(n + 1)! - 1 / 2 * log(2 * n) - n * log ((n + 1) / e)`.
-/
lemma log_stirling_seq_formula (n : ℕ) : log (stirling_seq n.succ) =
log n.succ!- 1 / 2 * log (2 * n.succ) - n.succ * log (n.succ / exp 1) :=
by rw [stirling_seq, log_div, log_mul, sqrt_eq_rpow, log_rpow, real.log_pow, tsub_tsub];
try { apply ne_of_gt }; positivity -- TODO: Make `positivity` handle `≠ 0` goals
/--
The sequence `log (stirling_seq (m + 1)) - log (stirling_seq (m + 2))` has the series expansion
`∑ 1 / (2 * (k + 1) + 1) * (1 / 2 * (m + 1) + 1)^(2 * (k + 1))`
-/
lemma log_stirling_seq_diff_has_sum (m : ℕ) :
has_sum (λ k : ℕ, (1 : ℝ) / (2 * k.succ + 1) * ((1 / (2 * m.succ + 1)) ^ 2) ^ k.succ)
(log (stirling_seq m.succ) - log (stirling_seq m.succ.succ)) :=
begin
change has_sum ((λ b : ℕ, 1 / (2 * (b : ℝ) + 1) * ((1 / (2 * m.succ + 1)) ^ 2) ^ b) ∘ succ) _,
refine (has_sum_nat_add_iff 1).mpr _,
convert (has_sum_log_one_add_inv $ cast_pos.mpr (succ_pos m)).mul_left ((m.succ : ℝ) + 1 / 2),
{ ext k,
rw [← pow_mul, pow_add],
push_cast,
have : 2 * (k : ℝ) + 1 ≠ 0, {norm_cast, exact succ_ne_zero (2*k)},
have : 2 * ((m : ℝ) + 1) + 1 ≠ 0, {norm_cast, exact succ_ne_zero (2*m.succ)},
field_simp,
ring },
{ have h : ∀ (x : ℝ) (hx : x ≠ 0), 1 + x⁻¹ = (x + 1) / x,
{ intros, rw [_root_.add_div, div_self hx, inv_eq_one_div], },
simp only [log_stirling_seq_formula, log_div, log_mul, log_exp, factorial_succ, cast_mul,
cast_succ, cast_zero, range_one, sum_singleton, h] { discharger :=
`[norm_cast, apply_rules [mul_ne_zero, succ_ne_zero, factorial_ne_zero, exp_ne_zero]] },
ring },
end
/-- The sequence `log ∘ stirling_seq ∘ succ` is monotone decreasing -/
lemma log_stirling_seq'_antitone : antitone (real.log ∘ stirling_seq ∘ succ) :=
antitone_nat_of_succ_le $ λ n, sub_nonneg.mp $ (log_stirling_seq_diff_has_sum n).nonneg $ λ m,
by positivity
/--
We have a bound for successive elements in the sequence `log (stirling_seq k)`.
-/
lemma log_stirling_seq_diff_le_geo_sum (n : ℕ) :
log (stirling_seq n.succ) - log (stirling_seq n.succ.succ) ≤
(1 / (2 * n.succ + 1)) ^ 2 / (1 - (1 / (2 * n.succ + 1)) ^ 2) :=
begin
have h_nonneg : 0 ≤ ((1 / (2 * (n.succ : ℝ) + 1)) ^ 2) := sq_nonneg _,
have g : has_sum (λ k : ℕ, ((1 / (2 * (n.succ : ℝ) + 1)) ^ 2) ^ k.succ)
((1 / (2 * n.succ + 1)) ^ 2 / (1 - (1 / (2 * n.succ + 1)) ^ 2)),
{ have := (has_sum_geometric_of_lt_1 h_nonneg _).mul_left ((1 / (2 * (n.succ : ℝ) + 1)) ^ 2),
{ simp_rw ←pow_succ at this,
exact this, },
rw [one_div, inv_pow],
exact inv_lt_one (one_lt_pow ((lt_add_iff_pos_left 1).mpr $ by positivity) two_ne_zero) },
have hab : ∀ (k : ℕ), (1 / (2 * (k.succ : ℝ) + 1)) * ((1 / (2 * n.succ + 1)) ^ 2) ^ k.succ ≤
((1 / (2 * n.succ + 1)) ^ 2) ^ k.succ,
{ refine λ k, mul_le_of_le_one_left (pow_nonneg h_nonneg k.succ) _,
rw one_div,
exact inv_le_one (le_add_of_nonneg_left $ by positivity) },
exact has_sum_le hab (log_stirling_seq_diff_has_sum n) g,
end
/--
We have the bound `log (stirling_seq n) - log (stirling_seq (n+1))` ≤ 1/(4 n^2)
-/
lemma log_stirling_seq_sub_log_stirling_seq_succ (n : ℕ) :
log (stirling_seq n.succ) - log (stirling_seq n.succ.succ) ≤ 1 / (4 * n.succ ^ 2) :=
begin
have h₁ : 0 < 4 * ((n : ℝ) + 1) ^ 2 := by positivity,
have h₃ : 0 < (2 * ((n : ℝ) + 1) + 1) ^ 2 := by positivity,
have h₂ : 0 < 1 - (1 / (2 * ((n : ℝ) + 1) + 1)) ^ 2,
{ rw ← mul_lt_mul_right h₃,
have H : 0 < (2 * ((n : ℝ) + 1) + 1) ^ 2 - 1 := by nlinarith [@cast_nonneg ℝ _ n],
convert H using 1; field_simp [h₃.ne'] },
refine (log_stirling_seq_diff_le_geo_sum n).trans _,
push_cast,
rw div_le_div_iff h₂ h₁,
field_simp [h₃.ne'],
rw div_le_div_right h₃,
ring_nf,
norm_cast,
linarith,
end
/-- For any `n`, we have `log_stirling_seq 1 - log_stirling_seq n ≤ 1/4 * ∑' 1/k^2` -/
lemma log_stirling_seq_bounded_aux :
∃ (c : ℝ), ∀ (n : ℕ), log (stirling_seq 1) - log (stirling_seq n.succ) ≤ c :=
begin
let d := ∑' k : ℕ, (1 : ℝ) / k.succ ^ 2,
use (1 / 4 * d : ℝ),
let log_stirling_seq' : ℕ → ℝ := λ k, log (stirling_seq k.succ),
intro n,
have h₁ : ∀ k, log_stirling_seq' k - log_stirling_seq' (k + 1) ≤ 1 / 4 * (1 / k.succ ^ 2) :=
by { intro k, convert log_stirling_seq_sub_log_stirling_seq_succ k using 1, field_simp, },
have h₂ : ∑ (k : ℕ) in range n, (1 : ℝ) / (k.succ) ^ 2 ≤ d := by
{ exact sum_le_tsum (range n) (λ k _, by positivity)
((summable_nat_add_iff 1).mpr $ real.summable_one_div_nat_pow.mpr one_lt_two) },
calc
log (stirling_seq 1) - log (stirling_seq n.succ) = log_stirling_seq' 0 - log_stirling_seq' n : rfl
... = ∑ k in range n, (log_stirling_seq' k - log_stirling_seq' (k + 1)) : by
rw ← sum_range_sub' log_stirling_seq' n
... ≤ ∑ k in range n, (1/4) * (1 / k.succ^2) : sum_le_sum (λ k _, h₁ k)
... = 1 / 4 * ∑ k in range n, 1 / k.succ ^ 2 : by rw mul_sum
... ≤ 1 / 4 * d : mul_le_mul_of_nonneg_left h₂ $ by positivity,
end
/-- The sequence `log_stirling_seq` is bounded below for `n ≥ 1`. -/
lemma log_stirling_seq_bounded_by_constant : ∃ c, ∀ (n : ℕ), c ≤ log (stirling_seq n.succ) :=
begin
obtain ⟨d, h⟩ := log_stirling_seq_bounded_aux,
exact ⟨log (stirling_seq 1) - d, λ n, sub_le_comm.mp (h n)⟩,
end
/-- The sequence `stirling_seq` is positive for `n > 0` -/
lemma stirling_seq'_pos (n : ℕ) : 0 < stirling_seq n.succ := by { unfold stirling_seq, positivity }
/--
The sequence `stirling_seq` has a positive lower bound.
-/
lemma stirling_seq'_bounded_by_pos_constant : ∃ a, 0 < a ∧ ∀ n : ℕ, a ≤ stirling_seq n.succ :=
begin
cases log_stirling_seq_bounded_by_constant with c h,
refine ⟨exp c, exp_pos _, λ n, _⟩,
rw ← le_log_iff_exp_le (stirling_seq'_pos n),
exact h n,
end
/-- The sequence `stirling_seq ∘ succ` is monotone decreasing -/
lemma stirling_seq'_antitone : antitone (stirling_seq ∘ succ) :=
λ n m h, (log_le_log (stirling_seq'_pos m) (stirling_seq'_pos n)).mp (log_stirling_seq'_antitone h)
/-- The limit `a` of the sequence `stirling_seq` satisfies `0 < a` -/
lemma stirling_seq_has_pos_limit_a :
∃ (a : ℝ), 0 < a ∧ tendsto stirling_seq at_top (𝓝 a) :=
begin
obtain ⟨x, x_pos, hx⟩ := stirling_seq'_bounded_by_pos_constant,
have hx' : x ∈ lower_bounds (set.range (stirling_seq ∘ succ)) := by simpa [lower_bounds] using hx,
refine ⟨_, lt_of_lt_of_le x_pos (le_cInf (set.range_nonempty _) hx'), _⟩,
rw ←filter.tendsto_add_at_top_iff_nat 1,
exact tendsto_at_top_cinfi stirling_seq'_antitone ⟨x, hx'⟩,
end
/-!
### Part 2
https://proofwiki.org/wiki/Stirling%27s_Formula#Part_2
-/
/-- The sequence `n / (2 * n + 1)` tends to `1/2` -/
lemma tendsto_self_div_two_mul_self_add_one :
tendsto (λ (n : ℕ), (n : ℝ) / (2 * n + 1)) at_top (𝓝 (1 / 2)) :=
begin
conv { congr, skip, skip, rw [one_div, ←add_zero (2 : ℝ)] },
refine (((tendsto_const_div_at_top_nhds_0_nat 1).const_add (2 : ℝ)).inv₀
((add_zero (2 : ℝ)).symm ▸ two_ne_zero)).congr' (eventually_at_top.mpr ⟨1, λ n hn, _⟩),
rw [add_div' (1 : ℝ) 2 n (cast_ne_zero.mpr (one_le_iff_ne_zero.mp hn)), inv_div],
end
/-- For any `n ≠ 0`, we have the identity
`(stirling_seq n)^4 / (stirling_seq (2*n))^2 * (n / (2 * n + 1)) = W n`, where `W n` is the
`n`-th partial product of Wallis' formula for `π / 2`. -/
lemma stirling_seq_pow_four_div_stirling_seq_pow_two_eq (n : ℕ) (hn : n ≠ 0) :
((stirling_seq n) ^ 4 / (stirling_seq (2 * n)) ^ 2) * (n / (2 * n + 1)) = wallis.W n :=
begin
rw [bit0_eq_two_mul, stirling_seq, pow_mul, stirling_seq, wallis.W_eq_factorial_ratio],
simp_rw [div_pow, mul_pow],
rw [sq_sqrt, sq_sqrt],
any_goals { positivity },
have : (n : ℝ) ≠ 0, from cast_ne_zero.mpr hn,
have : (exp 1) ≠ 0, from exp_ne_zero 1,
have : ((2 * n)!: ℝ) ≠ 0, from cast_ne_zero.mpr (factorial_ne_zero (2 * n)),
have : 2 * (n : ℝ) + 1 ≠ 0, by {norm_cast, exact succ_ne_zero (2*n)},
field_simp,
simp only [mul_pow, mul_comm 2 n, mul_comm 4 n, pow_mul],
ring,
end
/--
Suppose the sequence `stirling_seq` (defined above) has the limit `a ≠ 0`.
Then the Wallis sequence `W n` has limit `a^2 / 2`.
-/
lemma second_wallis_limit (a : ℝ) (hane : a ≠ 0) (ha : tendsto stirling_seq at_top (𝓝 a)) :
tendsto wallis.W at_top (𝓝 (a ^ 2 / 2)):=
begin
refine tendsto.congr' (eventually_at_top.mpr ⟨1, λ n hn,
stirling_seq_pow_four_div_stirling_seq_pow_two_eq n (one_le_iff_ne_zero.mp hn)⟩) _,
have h : a ^ 2 / 2 = (a ^ 4 / a ^ 2) * (1 / 2),
{ rw [mul_one_div, ←mul_one_div (a ^ 4) (a ^ 2), one_div, ←pow_sub_of_lt a],
norm_num },
rw h,
exact ((ha.pow 4).div ((ha.comp (tendsto_id.const_mul_at_top' two_pos)).pow 2)
(pow_ne_zero 2 hane)).mul tendsto_self_div_two_mul_self_add_one,
end
/-- **Stirling's Formula** -/
theorem tendsto_stirling_seq_sqrt_pi : tendsto (λ (n : ℕ), stirling_seq n) at_top (𝓝 (sqrt π)) :=
begin
obtain ⟨a, hapos, halimit⟩ := stirling_seq_has_pos_limit_a,
have hπ : π / 2 = a ^ 2 / 2 := tendsto_nhds_unique wallis.tendsto_W_nhds_pi_div_two
(second_wallis_limit a hapos.ne' halimit),
rwa [(div_left_inj' (two_ne_zero' ℝ)).mp hπ, sqrt_sq hapos.le],
end
end stirling
|
00a6aca299cacd39a6742c236f7a7fc02199f2ab | 193da933cf42f2f9188bb47e3c973205bc2abc5c | /AA_Algebras/01_dm_bool/2a_dm_bool.lean | 0f747c854d25ef35a0d399749ac2cab0f36627b5 | [] | no_license | pandaman64/cs-dm | aa4e2621c7a19e2dae911bc237c33e02fcb0c7a3 | bfd2f5fd2612472e15bd970c7870b5d0dd73bd1c | refs/heads/master | 1,647,620,340,607 | 1,570,055,187,000 | 1,570,055,187,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 39,074 | lean | /-
In this unit, we implement our own version of the Boolean algebra
already built into Lean. We will define our own version of Lean's
bool type, and our own versions of its and, or, and not operators.
-/
/-
In doing this, we will encounter several important concepts in
Lean, which are also shared by many other languages. They include
the following:
- namespaces
- inductive type definitions
- sets, product sets, tuples, binary relations, and functions
- type judgments, type declarations, and type inference
- function types and function values
- functional programing: case analysis via pattern matching
-/
-- NAMESPACES
/-
Picking good names for concepts is important in every field.
It's especially important in computer science and software
development because we want complex "code," often involving
hundreds, thousands, or even millions of identifiers (the
CS term for a name to which a meaning is given in a program),
to be humanly understandable.
For example, rather than using a cryptic identifier, such as
"n", to refer to the "next node" to be processed in a loop,
one might instead use the identifier "next_node". It makes
no difference to the machine, but it makes a big difference
to the programmer coming to the code for the first time or
returning to it after some time has passed. The names that
we pick in this way can serve to document the intended
interpretation of the otherwise cryptic logic of the code.
A problem arises, of course, when two programmers, working
on two different parts of a larger program, decide to use
the same name for different concepts. One can imagine two
programmers both deciding to use "next_node" to refer to
the next object to be processed in two unrelated pieces of
code. When those pieces of code are brought together, the
result could be that the name becomes ambiguous: with two
different meanings.
A general solution to this problem employed by many
different programming languages is to define meanings
for identifiers within different namespaces. A namespace
is just a prefix that it implicitly prepended to each
name defined within the namespace.
If you've programmed in Python, Java, or most any other
industrial programming language, then you've seen the
use of namespaces.
In this class, we will enclose all of our definitions
within a namespace called edu.virginia.cs.dm. The idea
is that (1) the namespace explains the context in which
names are defined, i.e., the discrete math (dm) class
at UVa; and (2) no one else seems likely to pick the
same namespace. The next line of code tells Lean that
all names between here at the "end edu.virginia.cs.dm"
command at the end of the file are defined within the
edu.virginia.cs.dm namespace.
-/
namespace edu.virginia.cs.dm
/-
What this means is that every name, such as dm_bool,
below is implicitly prefixed with edu.virginia.cs.dm.
The "fully qualified" name for dm_bool as defined below
is thus edu.virginia.cs.dm.dm_bool.
-/
/-
With that ancillary detail out of the way we can
now turn to the main content of this chapter, giving
our own implementation of Boolean algebra.
-/
/-
**** INDUCTIVE DEFINITIONS OF TYPES: CASE STUDY OF bool ****
-/
/-
Our implementation comprises (1) a definition of the "carrier
set" of values of the Boolean type, and (2) a definition of a
set of operations (functions) closed on this set.
We first specify the "carrier set" of values of Boolean
algebra. The values are typically called true and false
(with different names in different natural languages).
The set of values of Boolean algebra can thus be written
as { true, false }. We call this "display notation".
A data type defines nothing other than a set of values,
namely the values that are specified as having that type
and no other values whatsoever.
Here's how we will represent the carrier set of Boolean
algebra as a data type in the Lean language. We explain
this code below.
-/
inductive dm_bool : Type
| dm_ff : dm_bool
| dm_tt : dm_bool
/-
To avoid any possibility of confusion between the
built-in bool type, and its tt and ff values (even
though we're in our own namespace here), we will
define our own Boolean algebra implementation to
be like the built-in version but with all names
prefixed with an "dm".
-/
/-
In Lean, as in many other functional languages, we
define types inductively. We specify the name of the
type and then we give a list of constructors that
specify the values of the type being defined.
The Lean keyword "inductive" introduces an inductive
definition. The name of the type being defined is
dm_bool, which is declared to be a type. (Technically
it is declared to be a value of type, Type, which,
as you might guess, is a type! Yep, types are values
too, in Lean: values of type Type.)
Finally a list of constructors is given, each starting
with a vertical bar, followed by the name of the
constructor and, at least in the simple case here,
a statement saying that the given constructor *is* a
value of the type being defined.
We have thus just defined a type called dm_bool with
two values, and no other values, namely dm_tt and dm_ff.
Our *intended* interpretation is that dm_bool represents
the carrier set of Boolean algebra, dm_tt represents the
true value of Boolean algebram and dm_ff represents
the false value.
This definition precisely mirrors the definition of
the built-in Boolean type in Lean, which is specified
like this:
inductive bool : Type
| ff : bool
| tt : bool
-/
/-
Our inductive type definition has actually defined meanings
for three different names: dm_bool, dm_tt, and dm_ff. The name, or
identifier, dm_bool, now refers to a type with two values, dm_tt
and dm_ff. Each dm_tt and dm_ff refer to two different constructors
that in effect define the two different values of the dm_bool
type.
We can use Lean's #check command to see that these names are
defined, and Lean will tell us the type of thing that each
name means. We start by using the fully qualified name of
our dm_bool type. Hover your mouse pointer over the two
parts of this command to see what it tells you.
-/
#check edu.virginia.cs.dm.dm_bool
/-
Because we are within the edu.virginia.cs.dm namespace here,
and because there is no ambiguity, we can leave off the whole
edu.virginia.cs.dm prefix. Note that when you hover over dm_bool
in the following #check command, it does tell you the fully
qualified name of dm_bool.
-/
#check dm_bool
/-
Importantly, Lean tells you that the type of dm_bool is Type.
That's it's way of saying "dm_bool is a type!"
-/
/-
You might think that you can also use the #check command
to check to see what's the type of dm_tt, and indeed we can.
However, we can't just do "#check dm_tt" because the name,
dm_tt, is defined in a namespace created by the dm_bool type.
Uncomment the following code and see what error you get.
-/
-- #check dm_tt
/-
Here's what you need to do instead.
-/
#check dm_bool.dm_tt
/-
This is equivalent to the following.
-/
#check edu.virginia.cs.dm.dm_bool.dm_tt
/-
What we see is that the name, dm_bool, as defined in the
current namespace, is "visible" in this namespace, but
the names defined in its namespace are still hidden away.
If you want to be able to use the names dm_tt and dm_ff
without qualifiers, you have to "open" the namespace
in which they are defined, i.e., the dm_bool namespace.
-/
open dm_bool
/-
Ah, now it all just works. That's it on namespaces.
-/
#check dm_tt
#check dm_ff
/-
We've thus got the first component of our algebra defined: the
carrier set of Boolean values! To complete our implementation,
we need to define a set of operations on this set: operations
such as "and", "or", and "not", which should already be familiar
from CS1 (abh: CS 1xxx?). Take a few minutes to recall exactly how they work in
a language you already know (e..g, Python).
-/
/-
EXERCISES:
* In the language you used for CS1, assign the Boolean
value, true, however it is written in your language, to
a variable called X. Assign the Boolean variable, false,
to Y. Assign to a variable, Z, the value of the negation
of ("not") X. Assign to W the disjunction ("or") of X and
Y. Finally print the value of the conjunction of W and
true. What value is produced? Every useful programming
languages provides a built-in implementation of Boolean
algebra. Now you're building your own.
-/
-- MATHEMATICAL FUNCTIONS AND PURE FUNCTIONAL PROGRAMS
/-
As you now recall from CS1, "and", "or", and "not" are really
just functions that take and return Boolean values. The "not"
function takes a single bool value as an argument ("not" is a
unary function) and returns its opposite bool value. The "and"
and "or" operations each take two bool values (they are binary
operations) and return bool values.
-/
/-
We now complete a definition of a limited version Boolean algebra
by defining several unary functions and binary operations over the
Booleans. A unary operation (aka function) takes one argument. A
binary operation takes two. Let's start with unary operations.
-/
/- THE UNARY OPERATIONS OF BOOLEAN ALGEBRA -/
/-
A unary operation takes one argument and on the basis of its value
alone returns a result. Boolean operations take and return Boolean
values, here implemented (represented, if you will) as the value of
our type, bool.
The functions of an algebra are "closed" on the carrier set of that
algebra. What this means is that each such functions yields a result
in that set when given any argument values from that set. We can also
call such a function "total." A "partial function" is not necessarily
total, and a "strictly partial" function is definitely not total. An
example of a partial function on the real numbers is division. It is
not defined when the second argument (the denominator) is zero.
We are interested in this section only in total unary functions on
the Booleans. What this means is, first, that for each Boolean value
there must be a corresponding Boolean result, and, second, to be a
*function*, there can be no more than one result. That is, there is
exactly one result for each argument value.
-/
/-
As a mathematical object, such a function is a set of pairs with
exactly one pair having each Boolean value in the first position.
For example, the set, { (tt, ff), (ff, tt) } is such a function.
The set { (tt, tt), (tt, ff) } is not, for two different reasons.
First, it is not a function at all, because it has two pairs with
the same first element, and so is not single-valued. Second, it
doesn't have a pair with ff as a first element and so is not total.
-/
/-
We can graphically depict a total unary function on the Booleans
as a table with two rows and two columns. The first entry in each
row indicates the argument to the function, and the second entry,
the corresponding result. Every such table will have the same first
column, listing each and every possible argument value. Different
functions will then be defined by the corresponding entries in the
second column. Such a table looks like this, where underscores are
placeholders for return values.
Arg Ret
+----+----+
| tt | __ |
+----+----|
| ff | __ |
+----+----+
-/
/-
One of the important concepts in discrete mathematics is that of
"counting" the number of objects of some particular kind. Here the
question is, how many unary functions are there on the Booleans?
The answer is equal to the number of different ways in which that
second column can be completed. For example, the co-called identity
function on the Booleans is the function where the return result
is always the same as the argument value. Here's the table.
Arg Ret
+----+----+
| tt | tt |
+----+----|
| ff | ff |
+----+----+
Of course such a table is just another way of representing the set
of pairs, { (tt, tt), (ff, ff) }, which is the right way to think of
the identity function as a mathematical object. If we wanted to give
this function a name, we might call it id_bool, with a prefix, id,
suggesting the identity function, and the suffix, _bool, suggesting
that this is the identity function for the bool type.
If you were writing out the algebra in ordinary paper-and-pencil
mathematics, you'd write this function as id_bool(b) = b, where
b is any Boolean value. You can imagine the corresponding identity
functions for any other type. E.g., for the natural numbers, you
could define id_nat as id_nat(n) = n, where n is any natural number.
-/
/-
We can now put all of these ideas together to write a pure
functional program that implements this function. We will call
this program id_bool. If we apply this resulting program to a
Boolean-valued argument value, b, which would be done by writing
the expression, "id_bool b", the result that is returned will be
just b itself.
As we've now seen, a Boolean function can be represented in at
least three ways:
- as a set of pairs, such as { (tt, tt), (ff, ff) }
- in the form of a table, namely a truth table
- using an equation, such as "id_bool(b) = b"
Simple pure functional programs are generally written in the
equational form. Here's the code for the id_bool function.
-/
def id_bool (b: dm_bool) : dm_bool := b
/-
The "def" keyword introduces a new definition -- a binding of a name,
or "identifier", here id_bool, to a value, here a definition of the
identity function that takes a bool argument and returns that same
value as the result. (Yes, function bodies are values, too.)
You can thus pronounce this code as follows: "we define id_bool to
be a function that takes one argument of type bool, bound to the
identifier b, and that also returns a value of type bool, namely
that obtained by evaluating the expression, "b", in the context of
the prevailing binding of the identifier, b, to the value of the
bool argument to which the function is applied in any given case.
-/
/-
EXERCISE: Write pure functional programs called false_bool and
true_bool, respectively, each of which takes a bool argument and
that always returns false or true, respectively.
-/
-- TYPE INFERENCE
/-
There's a shorter way to write the same function: we can leave out
the explicit return type (the bool after the colon) because Lean
can infer what it must be by analyzing the type of the expression
that defines the return result. The argument, b, is declared to
be of type bool, so it is clear that type obtained by evaluating
the expression, b, defining the return result, is also of type bool.
Here's another version of the same definition, with a "prime" mark
to make the name unique, exhibiting the use of type inference.
-/
def id_bool' (b: dm_bool) := b
/-
EXERCISE: Use type inference to write shorter versions of
you true_bool and false_bool programs.
-/
-- FUNCTION APPLICATION EXPRESSIONS
/-
A function application expression is an expression written
as a function name (or more generally as an expression that
reduces to a function value)) followed by an expression that
reduces to a value that is taken to be the argument to the
function.
The simplest form of a function application expression is
just a function name applied to a so-called "literal value"
of the required type. In the function application expression,
"id_bool tt", you see first a function name, the "variable",
id_bool, followed by the literal expression, tt.
Here's an example in which id_bool is
applied to the literal value, tt. By hovering over the
#reduce command, you can see the value to which this
function application expression is reduced.
-/
#reduce id_bool dm_tt
/-
EXERCISES: Write and reduce expressions in which you apply
your true_bool and false_bool programs to each of the two
bool values, thereby exhaustively testing each program for
correctness.
-/
-- EVALUATION OF FUNCTION APPLICATION EXPRESSIONS
/-
Reducing a function application expression is a very simple
process. First you evaluate the function expression, then you
evaluate the argument expression, then you apply the resulting
function to the resulting value. Let's do this in steps.
First, the function expression is given by the identifier,
id_bool. To obtain the actual function, Lean looks up its
definition and finds a function that takes a bool as a value
and that returns that same bool value as a result.
abh: there are a lot of references to "tt" where we clearly
mean "dm_tt"
Second, the identifier, tt, is a literal expression for the
tt value/constructor of the bool type.
Finally, the function is applied to this argument value.
This is done by substituting the argument value for the
argument variable wherever it appears in the body of the
function and by then evaluating the resulting expression.
The body of the function in this case is just "b". So the
value, tt, is substituted for "b". Finally this expression
is evaluated, once again producing the value, tt, and that
is the result of the function application!
-/
/-
EXERCISE: We previously confirmed that the definition of
tt is ambiguous in the current environment. So why was it
okay here to write tt without qualifiers in the expression,
id_bool tt?
-/
/-
EXERCISE: Explain in precise, concise English exactly
how your true_bool program is evaluated when applied to
the argument given by the literal expression, tt.
-/
-- FUNCTION TYPES
-- abh: There's some redundancy between dm_bool.lean and functions.lean
/-
Functions also have types. We can check the type of id_boolean
using the #check command. Hover your mouse over the #check.
Lean reports that the type of this function is boolean → boolean.
That is how, in type theory, we write the type of a function
that takes an argument of type bool and that returns a result
of that same type.
-/
#check id_bool
/-
EXERCISE: First mentally determine the types of your false_bool
and true_bool functions, and then use #check commands to test
your predictions.
-/
-- TESTING FUNCTION IMPLEMENTATIONS FOR CORRECTNESS
/-
Whenever we write programs that are meant to compute values
of functions for given arguments, the question arises, did
we represent/implement the intended function correctly?
An important observation is that the question presumes
that we have a definition of a function to be implemented
against which the correctness of the implementation can
be evaluated.
Consider our implementation of id_bool? Against what
specification should its correctness be determined?
Here the best answers are that we can evaluate the
correctness of id_bool against either the truth table
or the equivalent set-theoretic definition. The tuples
in the definition of the function, (tt, tt) and (ff, ff),
tell us what result to *expect* for each argument value:
expect tt when given tt as an argument and expect ff
when given ff as an argument.
-/
/-
The process of software *testing* is one in which a
program is evaluated for one or more argument values
and the actual results are compared with the expected
results to identify any discrepancies. A single pair,
(argument value, expected result) is called a *test
case*. The tuples in our set-theoretic definition
of id_bool can thus serve as test cases. Consider
the tuple, (tt, tt). Viewing it as a test case tells
us that we should expect that "id_bool tt = tt."
-/
-- PROPOSITIONS AND PROOFS
/-
A claim like this---an assertion that a certain state
of affairs holds in a given situation, here the assertion
that if we evaluate id_bool with argument tt the result
will be tt---is what in logic we call a proposition.
A proposition is a truth claim. A proposition can thus
be true, or false, or, in some logics the truth of a
given proposition can be indeterminate. In any case,
establishing the truth of a proposition requires what
we call a proof.
-/
/-
The real power of Lean is that in addition to letting
us write programs, it also let us write propositions
and proofs, and it checks that proofs are correct.
Here, the proposition for which we want a proof is
the proposition, id_bool tt = tt. We can write and
prove this proposition in Lean as follows.
-/
theorem id_bool_correct_for_tt: id_bool dm_tt = dm_tt :=
rfl
/-
We introduce a proposition and its proof with the
keyword, theorem. Technically theorem is just the
same as definition: it's a way to say we're about
to define a value but we intend for that value to
be a proof of some proposition
Following this keyword we give a name to the proof
that we intend to define: id_bool_correct_for_tt.
Next, after the colon comes the proposition itself:
here, id_bool tt = tt.
Next comes a :=. Finally we write the proof: here
the cryptic term, rfl.
-/
/-
PROOF BY SIMPLIFICATION AND THE REFLEXIVE PROPERTY
OF EQUALITY
-/
/-
Proofs come in many forms. As a mathematician,
you learn what forms of proofs work for what
kinds of propositions. Here we have propositions
that two expressions reduce to the same value.
To construct such a proof, you first reduce each
expression to a value. The expression id_bool dm_tt
reduces to tt. The expression dm_tt reduces to dm_tt
(it's already reduced to a value). Then what
you have is the proposition, dm_tt = dm_tt. But
that is true by the reflexive property of
equality: for any value x, no matter what it
is, x = x. So dm_tt = dm_tt. The proposition is
thus proved.
-/
/-
You thus read "rfl" as saying what a mathematician
would pronounce as, "by simplification of expressions
and the reflexive property of equality."
The fact that Lean accepts rfl as a proof (value)
provides a very strong mechanized check on the
correctness of the proof.
-/
/-
EXERCISE: Use Lean to state and prove the proposition
that id_bool dm_ff = dm_ff.
-/
/-
EXERCISE: Write a similar definition, bad_proof_1,
asserting that there is a proof of the proposition,
id_bool dm_tt = dm_ff. Does Lean accept the proof? What
error messages does Lean report?
-/
-- YOUR WORK HERE
/-
The red under rfl indicates a "type mismatch", stating
that rfl was expected to be a proof that something
(here given the arbitrary name m_2) is equal to itself,
but that (in so many words) the things asserted to be
equal are not equal.
The red under bad_proof states, in effect, that the
name, bad_proof, was expected to be bound to a proof
of some proposition, but it is not so bound (due to
the preceding error).
A proposition that is false has no proof. To see that
rfl cannot be a proof of id_bool dm_tt = dm_ff, observe that
id_bool dm_tt reduces to dm_tt, so the proposition reduces
to dm_tt = dm_ff, but dm_tt and dm_ff are not equal, and so rfl
is not a valid proof: it can only be used to prove that
two values are equal to themselves. In the logic of
Lean, different constructors *always* build different
values -- values that are never equal to each other --
so dm_ff cannot be equal to dm_tt. The proposed proof, rfl,
is thus rejected.
-/
/-
EXERCISE: Give a proof, call it one_equals_one, for
the proposition, 1 = 1.
-/
-- YOUR WORK HERE
/-
EXERCISE: Attempt to give a proof, using rfl, of the
proposition that 1 = 2. What happens. Investigate and
briefly explain in plain English the meanings of the
error messages that are reported.
-/
/- UNIVERSAL QUANTIFICATION AND PROOF BY CASE ANALYSIS -/
/-
Testing that a program is correct for one input, which is
what a test case asserts, does not prove that it is correct
for all possible inputs, unless there is only one possible
input, in which case the function is pretty much useless.
The kind of propositions that we really want to prove are
ones that claim= a program is correct *for all* possible
inputs.
-/
theorem id_bool_correct:
∀ b: dm_bool, id_bool b = b
| dm_tt := rfl
| dm_ff := rfl
/-
We once again give our proof a name that reflects the
proposition that it proves: here, id_bool_correct. We
are claiming that the function is correct for every
possible input. On the second lines is the proposition
itself. The "universal quantifier", ∀, pronounced as
"for all", or "for every", or "for any". It is followed
by a variable, b, and its type, bool. So far we can thus
pronounce the proposition as saying "for any value, b,
of type bool." Then comes a comma followed by the rest
of the proposition: namely, the claim that, for any
such b, id_bool b = b. The "b" in this part of the
proposition is the b "bound" in the quantifier part
of the expression. The whole proposition thus covers
all possible cases, and reads, "for any boolean value,
b, id_bool b is equal to b."
We can't use rfl directly as a proof, because the
form of the proposition is not a simple assertion
of equality of the values of two expressions. It is
instead a "universally quantified" proposition
The remainder is the code instead gives a "proof by
case analysis." We show that for each possible value
of b considered in turn, the claim that id_bool b = b
is true.
Because there are only two values of type bool, there
are two cases: one where b is tt, and one where b is
ff.
Each case starts with a vertical bar, followed by the
case (the value of b) being considered. Then comes a
:=, and followed by a proof for that case.
Consider the first case, in which b is bound to tt.
In this case, making this substitution in the "body"
of the proposition gives us id_bool tt = tt. For
this proposition, we already have a proof! It's rfl.
The same holds true for the second case.
As we've now given a proof for each individual case,
we've given a proof "for all" cases, showing that
the overall quantified proposition is true. Proving
universally quantified propositions about software
correctness is called formal verification, and is a
state of the art approach to producing ultra-high
quality code. Such a high standard of correctness
is not always necessary or practical, but when
lives or nations depend on correctness of code, it
is a "gold standard" approach to software quality.
-/
/-
EXERCISE: In a similar style, state and prove the
proposition that every natural number, n, is equal
to itself. Call your proof nat_refl. You can get
the ∀ character (the universal quantifier, for all)
in Lean by typing \forall followed by a space.
NOTE: You don't know how to write such a proof
yet, so just write "sorry" instead. This tells
Lean to accept the proposition as being true
even though you haven't yet given a proof (and
even if it's actually not even true). You are
saying, "accept the proposition without proof",
or "accept it as an "axiom." An axiom is any
proposition that is accepted without a proof.
This is really just an exercise that asks you
to write a proposition in Lean using a ∀.
-/
-- theorem nat_refl: ∀ n: nat, n = n := sorry
/-
EXERCISE: State a proposition and give a proof
in Lean for the proposition stating that for
*every* possible argument, b, to false_bool,
false_bool b = false. Similarly prove that
your implementation of true_bool is correct
with respect to our understanding of how it
should behave.
-/
/-
Exercise: How many test cases do we need to "prove" that
the function works correctly for all possible inputs of
type boolean. (Hint: how many such inputs are there?)
Write any additional test cases need to prove that our
definition of the identity function works as we expect
it to.
-/
/-
Proof by case analysis often works well when you want to
prove that something is true for every element in a finite
set of elements. It isn't an appropriate proof strategy when
the set of values to be considered is infinite, as it would
be impossible to test every individual case. For example,
you can't prove that a functional program that takes a
natural number as an argument is correct by giving a proof
for each natural number in turn because there is an infinity
of such values. Another proof strategy would be need. It
goes by the name of "proof by induction." More on that later!
-/
-- THE MAJOR FUNCTIONS OF BOOLEAN ALGEBRA
/-
So far we have implemented three of the four unary functions
of Boolean algebra. The remaining one is usually called "not"
or "negation." We will give it the name, negb. Given one of
the two Boolean values as an argument, negb returns the other.
The set-theoretic definition is negb = { (tt, ff), (ff, tt) }.
The truth table depicts this definition graphically.
abh: the table below has switched from tt/ff to T/F
Arg Ret
+---+---+
| T | F |
+---+---|
| F | T |
+---+---+
-/
/-
We don't yet have the tools needed to implement this function.
The tool we need is called pattern matching. It's just a form
of case analysis! Here's the code.
-/
def negb (b: dm_bool): dm_bool :=
match b with
| dm_tt := dm_ff
| dm_ff := dm_tt
end
/-
We define negb to be a function that takes a Boolean value and
returns a Boolean value. That is, the type of this function is,
like the others we've defined so far, bool → bool. The thing that
is different about this function is that we have to inspect the
argument value to determining what result to return. We do that by
case analysis! What the body of this function says is "match the
value of b with each of its possible cases. THe first case is tt,
and in this case (after the :=), the return value is given by
the expression ff. Similarly, for the case, ff, the result is tt.
There are no more cases, and so this function has given a result
value "for every" possible value of b. This is thus a definition
of a total function by case analysis!
(You get the → symbol in Lean by typing \to. EXERCISE: Try it.)
-/
/-
EXERCISE: Write a second implementation of id_bool, call it
id_bool', using case analysis. Prove by case analysis that it
is correct with respect to its expected behavior.
-/
-- FUNCTION TYPES
/-
In Lean, every function is required to be total. That is, a
function must define how to construct a return value of the
specified type "for every" value of its argument type. The
four unary functions we've seen so far all do this. For every
value of type dm_bool, each of them returns a value of type dm_bool.
We've seen that you can write this type as dm_bool → dm_bool, but
another way to write it is, ∀ b: dm_bool, dm_bool. This just says,
for every value of type bool you can give me, I promise to
give you back a value of type dm_bool. Hover over the #check
that follows. You will see that we have just found two ways
to write the same function type!
-/
#check ∀ b: dm_bool, dm_bool
/-
EXERCISE: Use a universal quantifier to write and check the
type of functions from natural numbers to dm_bools. An example
of such a function would be one that took any natural number
and returned dm_tt if and only it it was even, and dm_ff otherwise.
-/
#check ∀ n: nat, dm_bool
/-
THE BINARY OPERATORS OF BOOLEAN ALGEBRA
-/
/-
Binary operations of an algebra are functions that take two
arguments of a given type (such as dm_bool) and that return a
value of that same type. The conjunction, aka and, operation
in Boolean algebra is an example. It takes two Boolean values
as arguments and returns a Boolean result. If both arguments
are true, its result is true, otherwise it is false. This
behavior is reflected in the following set-theoretic and
truth table specifications. We will call the function xandb.
xandb = { ((T, T), T), ((T, F), F), ((F, T), F), ((F, F), F) }
Note that we again give the function as a set of argument/result
pairs, but now each argument is itself a pair of values.
A truth table view:
xandb
+---+---+---+
| T | T | T |
+---+---|---+
| T | F | F |
+---+---+---+
| F | T | F |
+---+---+---+
| F | F | F |
+---+---+---+
abh: An alternative truth table view
(lower-case inputs, upper-case result):
arg 1
+---+---+
| t | f |
a +---+---|---+
r | t | T | F |
g +---+---+---+
| f | F | F |
2 +---+---+---+
-/
/-
We already have all the tools but for one to implement this
function. The one tool we need is pattern matching (i.e., case
analysis) for *pairs* of argument values. This following code
shows how to do this. Instead of matching on one value, b, we
now match on the comma-separated pair of values, b1, b2; and
each case now corresponds to a possible pair of argument values.
There are four possible pairs of Boolean values, and so there
are four cases to consider. Note how the visual organization
of the code reflects the truth table contents (and thus the
equivalent set-theoretic definition nearly directly.)
-/
def and_boolean' (b1 b2: dm_bool): dm_bool :=
match b1, b2 with
| dm_tt, dm_tt := dm_tt
| dm_tt, dm_ff := dm_ff
| dm_ff, dm_tt := dm_ff
| dm_ff, dm_ff := dm_ff
end
/-
It's often the case (ha ha) that in a case analysis, one
case stands out and several others or all the rest can be
considered at once. To define "xandb" all we really have to
say is "if b1 and b2 are both true, the result is true, and
in any other case the result is false." In Lean and in many
other functional programming language, we can write cases
analysis using "wildcards" (here underscores) that match
any values not considered in previous cases.
-/
def and_boolean (b1 b2: dm_bool): dm_bool :=
match b1, b2 with
| dm_tt, dm_tt := dm_tt
| _, _ := dm_ff
end
/-
EXERCISES:
1. Using case analysis, write definitions of the following binary
functions on booleans in the form of (a) sets, using display notation,
(b) truth tables, (3) functional programs. When writing functional
programs, use wildcards where possible to shorten your definitions.
* or (call it dm_orb)-- false if both arguments are false, otherwise true
* xor (dm_orb) -- true if either not both both arguments are true
* xnand (dm_nandb) -- the negation of the conjunction of the arguments
* ximplies (dm_implb) -- false if b1 is true and b2 is false otherwise true
* xnor (dm_norb)-- the negation of the "disjunction" of its arguments
2. By the method of case analysis, prove that your "or" and "xor" programs are
correct with respect to your truth table definitions, i.e., that they produce the
outputs specified by the truth tables for the given inputs.
3. How many binary functions on booleans are there? Justify your answer. Hint:
think about the truth tables. The set of possible arguments is always the set of
pairs of booleans. How many different ways can these arguments be associated with
boolean results?
4. Write a second definition of nandb, call it nandb', that instead of
using pattern matching applies a combination of andb and negb functions.
Surround function application expressions with parentheses to specify
groupings when they might otherwise be misinterpreted, e.g., vvv (www x y)
in the case where the argument to vvv is meant to be the result of
evaluating www x y.
-/
/-
FORMAL AND INFORMAL PROOFS.
-/
/-
Our formal proofs are very precise and their correctness is assured by Lean's
automated proof checking mechanism. By contrast, most working mathematicians
write informal proofs. These are still highly precise, but they are written in
structured English (or another natural language) rather than in what amounts
to code. The major benefit of informal proofs is that they are easier for most
people to understand. The downside is that mistakes in proofs can, and often
do, go undetected. One benefit of formal proofs is that they are checked for
correctness by a computer, and, when verified can be accepted as correct with
very high levels of confidence. Another benefit of learning how to write such
proofs is that the relationships between forms of propositions (e.g., equality
claims or universally quantified claims) and the forms of the corresponding
proofs becomes very clear. Learning how to write both informal and formal
propositions and proofs is an important goal of this class.
-/
/-
As an example, let's recast our formal proof of the correctness of our
program for id_bool as an informal proof. Here's how it might be written.
"The goal is to show that for every value, b, id_bool = b. We do this by
case analysis. There are two cases: b = dm_tt, and b = dm_ff, respectively. We
first consider the case where b = dm_tt. In this case, the proposition to be
proved is that id_bool dm_tt = dm_tt. We prove this by simplification of the left
hand side to tt by applying the definition of id_bool. What's left to prove
is that dm_tt = dm_tt, and this is done trivially by appealing to the reflexive
property of equality. The proof of the second case is by the same strategy
of simplification and reflexivity of equality."
-/
/-
EXERCISE: Write both a formal and a corresponding informal proof of the
correctness of your andb function.
-/
/-
BOOLEAN ALGEBRA AS AN ALGEBRA
-/
/-
We have now gotten to the point where we can make sense of the term, boolean algebra.
Boolean algebra is an algebra, which is to say it is a particular set of values and
a particular collection of operations closed on that set. We have represented the set
of values as a type, namely our dm_bool type. We have represented the operations as
pure functional programs taking and returning values of this type.
Moreover, by defining this set and its operations in a namespace, we've grouped
the values and operations on them in a meaningful way.
+ --------------+
| csdm.dm_bool | DATA
+---------------+
| csdm.dm_negb |
| csdm.dm_andb | OPERATIONS
| csdm.dm_orb |
| ... |
+---------------+
Such a structure, comprising a data type and a collection of operations on it is
also known, in the computer science field, as an "abstract data type (ADT)". When
computer scientists talk about "types", sometimes they mean inductively defined
types, such as the bool type (separate from operations), and sometimes (actually
more often) then mean abstract data types. Abstract data types, i.e., algebras!,
are fundamental building blocks of software. It took a while for us to build up
our bool ADT, but now that we're done, you can step back and now view our data
type definition and our definitions of a collection of associated operations as
a coherent whole, an abstract data type that implements the algebra of Boolean
truth values.
-/
-- USING AN ABSTRCT DATA TYPE
/-
The last question we address in this chapter is how to use such an abstract
data type implementation. To this end, please open and shift your attention
to the file csdm_bool_test.lean. It will show you how to import and use type
and function definitions in another file: this file in this case.
-/
/-
SUMMARY
- boolean algebra
* inductive definition of the type of booleans
* functions on booleans
** set theoretic, truth table, and pure functional representations
** unary functions on booleans
** binary functions on booleans
- types and values
- inductive definitions
- tuple values and tuple types
- relations and functions (set theoretic)
- functional programs: their types and values
- propositions
* about equality of terms
* universally quantified propositions
- an application: propositions and proofs of program correctness
- proof strategies:
* by simplification and reflexivity of equality
* by exhaustive case analysis
- formal and informal proofs
- algebras
-/
end edu.virginia.cs.dm |
284ec3b2ab78c9db3a6e056d623374f6aac7c467 | dfbb669f3f58ceb57cb207dcfab5726a07425b03 | /vscode-lean4/test/test-fixtures/multi/test/Test.lean | 0a49f6242b55ab8654dddfb1f7795b8128ae6039 | [
"Apache-2.0"
] | permissive | leanprover/vscode-lean4 | 8bcf7f06867b3c1d42007fe6da863a7a17444dbb | 6ef0bfa668bdeaad0979e6df10551d42fcc01094 | refs/heads/master | 1,692,247,771,767 | 1,691,608,804,000 | 1,691,608,804,000 | 325,845,305 | 64 | 24 | Apache-2.0 | 1,694,176,429,000 | 1,609,435,614,000 | TypeScript | UTF-8 | Lean | false | false | 19 | lean | def hello := "test" |
44c5225b8f2de4a04b88e588f1a56d2153df2ebd | 07c76fbd96ea1786cc6392fa834be62643cea420 | /hott/types/prod.hlean | 2e46a14e125ca0e1f8e94cb5d3f8d78786e37e11 | [
"Apache-2.0"
] | permissive | fpvandoorn/lean2 | 5a430a153b570bf70dc8526d06f18fc000a60ad9 | 0889cf65b7b3cebfb8831b8731d89c2453dd1e9f | refs/heads/master | 1,592,036,508,364 | 1,545,093,958,000 | 1,545,093,958,000 | 75,436,854 | 0 | 0 | null | 1,480,718,780,000 | 1,480,718,780,000 | null | UTF-8 | Lean | false | false | 12,632 | 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, Jakob von Raumer
Ported from Coq HoTT
Theorems about products
-/
open eq equiv is_equiv is_trunc prod prod.ops unit
variables {A A' B B' C D : Type} {P Q : A → Type}
{a a' a'' : A} {b b₁ b₂ b' b'' : B} {u v w : A × B}
namespace prod
/- Paths in a product space -/
protected definition eta [unfold 3] (u : A × B) : (pr₁ u, pr₂ u) = u :=
by cases u; reflexivity
definition pair_eq [unfold 7 8] (pa : a = a') (pb : b = b') : (a, b) = (a', b') :=
ap011 prod.mk pa pb
definition prod_eq [unfold 3 4 5 6] (H₁ : u.1 = v.1) (H₂ : u.2 = v.2) : u = v :=
by cases u; cases v; exact pair_eq H₁ H₂
definition eq_pr1 [unfold 5] (p : u = v) : u.1 = v.1 :=
ap pr1 p
definition eq_pr2 [unfold 5] (p : u = v) : u.2 = v.2 :=
ap pr2 p
namespace ops
postfix `..1`:(max+1) := eq_pr1
postfix `..2`:(max+1) := eq_pr2
end ops
open ops
protected definition ap_pr1 (p : u = v) : ap pr1 p = p..1 := idp
protected definition ap_pr2 (p : u = v) : ap pr2 p = p..2 := idp
definition pair_prod_eq (p : u.1 = v.1) (q : u.2 = v.2)
: ((prod_eq p q)..1, (prod_eq p q)..2) = (p, q) :=
by induction u; induction v; esimp at *; induction p; induction q; reflexivity
definition prod_eq_pr1 (p : u.1 = v.1) (q : u.2 = v.2) : (prod_eq p q)..1 = p :=
(pair_prod_eq p q)..1
definition prod_eq_pr2 (p : u.1 = v.1) (q : u.2 = v.2) : (prod_eq p q)..2 = q :=
(pair_prod_eq p q)..2
definition prod_eq_eta (p : u = v) : prod_eq (p..1) (p..2) = p :=
by induction p; induction u; reflexivity
-- the uncurried version of prod_eq. We will prove that this is an equivalence
definition prod_eq_unc [unfold 5] (H : u.1 = v.1 × u.2 = v.2) : u = v :=
by cases H with H₁ H₂; exact prod_eq H₁ H₂
definition pair_prod_eq_unc : Π(pq : u.1 = v.1 × u.2 = v.2),
((prod_eq_unc pq)..1, (prod_eq_unc pq)..2) = pq
| pair_prod_eq_unc (pq₁, pq₂) := pair_prod_eq pq₁ pq₂
definition prod_eq_unc_pr1 (pq : u.1 = v.1 × u.2 = v.2) : (prod_eq_unc pq)..1 = pq.1 :=
(pair_prod_eq_unc pq)..1
definition prod_eq_unc_pr2 (pq : u.1 = v.1 × u.2 = v.2) : (prod_eq_unc pq)..2 = pq.2 :=
(pair_prod_eq_unc pq)..2
definition prod_eq_unc_eta (p : u = v) : prod_eq_unc (p..1, p..2) = p :=
prod_eq_eta p
definition is_equiv_prod_eq [instance] [constructor] (u v : A × B)
: is_equiv (prod_eq_unc : u.1 = v.1 × u.2 = v.2 → u = v) :=
adjointify prod_eq_unc
(λp, (p..1, p..2))
prod_eq_unc_eta
pair_prod_eq_unc
definition prod_eq_equiv [constructor] (u v : A × B) : (u = v) ≃ (u.1 = v.1 × u.2 = v.2) :=
(equiv.mk prod_eq_unc _)⁻¹ᵉ
definition pair_eq_pair_equiv [constructor] (a a' : A) (b b' : B) :
((a, b) = (a', b')) ≃ (a = a' × b = b') :=
prod_eq_equiv (a, b) (a', b')
definition ap_prod_mk_left (p : a = a') : ap (λa, prod.mk a b) p = prod_eq p idp :=
ap_eq_ap011_left prod.mk p b
definition ap_prod_mk_right (p : b = b') : ap (λb, prod.mk a b) p = prod_eq idp p :=
ap_eq_ap011_right prod.mk a p
definition pair_eq_eta {A B : Type} {u v : A × B}
(p : u = v) : pair_eq (p..1) (p..2) = prod.eta u ⬝ p ⬝ (prod.eta v)⁻¹ :=
by induction p; induction u; reflexivity
definition prod_eq_eq {A B : Type} {u v : A × B}
{p₁ q₁ : u.1 = v.1} {p₂ q₂ : u.2 = v.2} (α₁ : p₁ = q₁) (α₂ : p₂ = q₂)
: prod_eq p₁ p₂ = prod_eq q₁ q₂ :=
by cases α₁; cases α₂; reflexivity
definition prod_eq_assemble {A B : Type} {u v : A × B}
{p q : u = v} (α₁ : p..1 = q..1) (α₂ : p..2 = q..2) : p = q :=
(prod_eq_eta p)⁻¹ ⬝ prod.prod_eq_eq α₁ α₂ ⬝ prod_eq_eta q
definition eq_pr1_concat {A B : Type} {u v w : A × B}
(p : u = v) (q : v = w)
: (p ⬝ q)..1 = p..1 ⬝ q..1 :=
by cases q; reflexivity
definition eq_pr2_concat {A B : Type} {u v w : A × B}
(p : u = v) (q : v = w)
: (p ⬝ q)..2 = p..2 ⬝ q..2 :=
by cases q; reflexivity
/- Groupoid structure -/
definition prod_eq_inv (p : a = a') (q : b = b') : (prod_eq p q)⁻¹ = prod_eq p⁻¹ q⁻¹ :=
by cases p; cases q; reflexivity
definition prod_eq_concat (p : a = a') (p' : a' = a'') (q : b = b') (q' : b' = b'')
: prod_eq p q ⬝ prod_eq p' q' = prod_eq (p ⬝ p') (q ⬝ q') :=
by cases p; cases q; cases p'; cases q'; reflexivity
definition prod_eq_concat_idp (p : a = a') (q : b = b')
: prod_eq p idp ⬝ prod_eq idp q = prod_eq p q :=
by cases p; cases q; reflexivity
/- Transport -/
definition prod_transport (p : a = a') (u : P a × Q a)
: p ▸ u = (p ▸ u.1, p ▸ u.2) :=
by induction p; induction u; reflexivity
definition prod_eq_transport (p : a = a') (q : b = b') {R : A × B → Type} (r : R (a, b))
: (prod_eq p q) ▸ r = p ▸ q ▸ r :=
by induction p; induction q; reflexivity
/- Pathovers -/
definition etao (p : a = a') (bc : P a × Q a) : bc =[p] (p ▸ bc.1, p ▸ bc.2) :=
by induction p; induction bc; apply idpo
definition prod_pathover (p : a = a') (u : P a × Q a) (v : P a' × Q a')
(r : u.1 =[p] v.1) (s : u.2 =[p] v.2) : u =[p] v :=
begin
induction u, induction v, esimp at *, induction r,
induction s using idp_rec_on,
apply idpo
end
open prod.ops
definition prod_pathover_equiv {A : Type} {B C : A → Type} {a a' : A} (p : a = a')
(x : B a × C a) (x' : B a' × C a') : x =[p] x' ≃ x.1 =[p] x'.1 × x.2 =[p] x'.2 :=
begin
fapply equiv.MK,
{ intro q, induction q, constructor: constructor },
{ intro v, induction v with q r, exact prod_pathover _ _ _ q r },
{ intro v, induction v with q r, induction x with b c, induction x' with b' c',
esimp at *, induction q, refine idp_rec_on r _, reflexivity },
{ intro q, induction q, induction x with b c, reflexivity }
end
/-
TODO:
* define the projections from the type u =[p] v
* show that the uncurried version of prod_pathover is an equivalence
-/
/- Functorial action -/
variables (f : A → A') (g : B → B')
definition prod_functor [unfold 7] (u : A × B) : A' × B' :=
(f u.1, g u.2)
infix ` ×→ `:63 := prod_functor
definition ap_prod_functor (p : u.1 = v.1) (q : u.2 = v.2)
: ap (prod_functor f g) (prod_eq p q) = prod_eq (ap f p) (ap g q) :=
by induction u; induction v; esimp at *; induction p; induction q; reflexivity
/- Helpers for functions of two arguments -/
definition ap_diagonal {a a' : A} (p : a = a')
: ap (λx : A, (x,x)) p = prod_eq p p :=
by cases p; constructor
definition ap_binary (m : A → B → C) (p : a = a') (q : b = b')
: ap (λz : A × B, m z.1 z.2) (prod_eq p q)
= ap (m a) q ⬝ ap (λx : A, m x b') p :=
by cases p; cases q; constructor
definition ap_prod_elim {A B C : Type} {a a' : A} {b b' : B} (m : A → B → C)
(p : a = a') (q : b = b') : ap (prod.rec m) (prod_eq p q)
= ap (m a) q ⬝ ap (λx : A, m x b') p :=
by cases p; cases q; constructor
definition ap_prod_elim_idp {A B C : Type} {a a' : A} (m : A → B → C)
(p : a = a') (b : B) : ap (prod.rec m) (prod_eq p idp) = ap (λx : A, m x b) p :=
ap_prod_elim m p idp ⬝ !idp_con
/- Equivalences -/
definition is_equiv_prod_functor [instance] [constructor] [H : is_equiv f] [H : is_equiv g]
: is_equiv (prod_functor f g) :=
begin
apply adjointify _ (prod_functor f⁻¹ g⁻¹),
intro u, induction u, rewrite [▸*,right_inv f,right_inv g],
intro u, induction u, rewrite [▸*,left_inv f,left_inv g],
end
definition prod_equiv_prod_of_is_equiv [constructor] [H : is_equiv f] [H : is_equiv g]
: A × B ≃ A' × B' :=
equiv.mk (prod_functor f g) _
definition prod_equiv_prod [constructor] (f : A ≃ A') (g : B ≃ B') : A × B ≃ A' × B' :=
equiv.mk (prod_functor f g) _
infix ` ×≃ `:63 := prod_equiv_prod
definition prod_equiv_prod_right [constructor] (g : B ≃ B') : A × B ≃ A × B' :=
prod_equiv_prod equiv.rfl g
definition prod_equiv_prod_left [constructor] (f : A ≃ A') : A × B ≃ A' × B :=
prod_equiv_prod f equiv.rfl
/- Symmetry -/
definition is_equiv_flip [instance] [constructor] (A B : Type)
: is_equiv (flip : A × B → B × A) :=
adjointify flip
flip
(λu, destruct u (λb a, idp))
(λu, destruct u (λa b, idp))
definition prod_comm_equiv [constructor] (A B : Type) : A × B ≃ B × A :=
equiv.mk flip _
/- Associativity -/
definition prod_assoc_equiv [constructor] (A B C : Type) : A × (B × C) ≃ (A × B) × C :=
begin
fapply equiv.MK,
{ intro z, induction z with a z, induction z with b c, exact (a, b, c)},
{ intro z, induction z with z c, induction z with a b, exact (a, (b, c))},
{ intro z, induction z with z c, induction z with a b, reflexivity},
{ intro z, induction z with a z, induction z with b c, reflexivity},
end
definition prod_contr_equiv [constructor] (A B : Type) [H : is_contr B] : A × B ≃ A :=
equiv.MK pr1
(λx, (x, !center))
(λx, idp)
(λx, by cases x with a b; exact pair_eq idp !center_eq)
definition prod_unit_equiv [constructor] (A : Type) : A × unit ≃ A :=
!prod_contr_equiv
definition prod_empty_equiv (A : Type) : A × empty ≃ empty :=
begin
fapply equiv.MK,
{ intro x, cases x with a e, cases e },
{ intro e, cases e },
{ intro e, cases e },
{ intro x, cases x with a e, cases e }
end
/- Universal mapping properties -/
definition is_equiv_prod_rec [instance] [constructor] (P : A × B → Type)
: is_equiv (prod.rec : (Πa b, P (a, b)) → Πu, P u) :=
adjointify _
(λg a b, g (a, b))
(λg, eq_of_homotopy (λu, by induction u;reflexivity))
(λf, idp)
definition equiv_prod_rec [constructor] (P : A × B → Type) : (Πa b, P (a, b)) ≃ (Πu, P u) :=
equiv.mk prod.rec _
definition imp_imp_equiv_prod_imp [constructor] (A B C : Type) : (A → B → C) ≃ (A × B → C) :=
!equiv_prod_rec
definition prod_corec_unc [unfold 4] {P Q : A → Type} (u : (Πa, P a) × (Πa, Q a)) (a : A)
: P a × Q a :=
(u.1 a, u.2 a)
definition is_equiv_prod_corec [constructor] (P Q : A → Type)
: is_equiv (prod_corec_unc : (Πa, P a) × (Πa, Q a) → Πa, P a × Q a) :=
adjointify _
(λg, (λa, (g a).1, λa, (g a).2))
(by intro g; apply eq_of_homotopy; intro a; esimp; induction (g a); reflexivity)
(by intro h; induction h with f g; reflexivity)
definition equiv_prod_corec [constructor] (P Q : A → Type)
: ((Πa, P a) × (Πa, Q a)) ≃ (Πa, P a × Q a) :=
equiv.mk _ !is_equiv_prod_corec
definition imp_prod_imp_equiv_imp_prod [constructor] (A B C : Type)
: (A → B) × (A → C) ≃ (A → (B × C)) :=
!equiv_prod_corec
theorem is_trunc_prod (A B : Type) (n : trunc_index) [HA : is_trunc n A] [HB : is_trunc n B]
: is_trunc n (A × B) :=
begin
revert A B HA HB, induction n with n IH, all_goals intro A B HA HB,
{ fapply is_contr.mk,
exact (!center, !center),
intro u, apply prod_eq, all_goals apply center_eq},
{ apply is_trunc_succ_intro, intro u v,
apply is_trunc_equiv_closed_rev, apply prod_eq_equiv,
exact IH _ _ _ _}
end
end prod
attribute prod.is_trunc_prod [instance] [priority 1510]
namespace prod
/- pointed products -/
open pointed
definition pointed_prod [instance] [constructor] (A B : Type) [H1 : pointed A] [H2 : pointed B]
: pointed (A × B) :=
pointed.mk (pt,pt)
definition pprod [constructor] (A B : Type*) : Type* :=
pointed.mk' (A × B)
infixr ` ×* `:35 := pprod
definition ppr1 [constructor] {A B : Type*} : A ×* B →* A :=
pmap.mk pr1 idp
definition ppr2 [constructor] {A B : Type*} : A ×* B →* B :=
pmap.mk pr2 idp
definition tprod [constructor] {n : trunc_index} (A B : n-Type) : n-Type :=
trunctype.mk (A × B) _
infixr `×t`:30 := tprod
definition ptprod [constructor] {n : ℕ₋₂} (A B : n-Type*) : n-Type* :=
ptrunctype.mk' n (A × B)
definition pprod_functor [constructor] {A B C D : Type*} (f : A →* C) (g : B →* D) : A ×* B →* C ×* D :=
pmap.mk (prod_functor f g) (prod_eq (respect_pt f) (respect_pt g))
definition pprod_incl1 [constructor] (X Y : Type*) : X →* X ×* Y := pmap.mk (λx, (x, pt)) idp
definition pprod_incl2 [constructor] (X Y : Type*) : Y →* X ×* Y := pmap.mk (λy, (pt, y)) idp
end prod
|
bda88cbd66d8ee1daca7cf712be04097381fbd0c | 9dd3f3912f7321eb58ee9aa8f21778ad6221f87c | /tests/lean/coe2.lean | 56c2464717529047dcdd1344911926113b6905df | [
"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 | 885 | lean | namespace hidden
set_option pp.coercions false
check if tt then "a" else "b"
/- Remark: in the standard library nat_to_int and int_to_real are has_lift instances
instead of has_coe. -/
constant int : Type
constant real : Type
constant nat_to_int : has_coe nat int
constant int_to_real : has_coe int real
attribute [instance] nat_to_int int_to_real
constant sine : real → real
constants n m : nat
constants i j : int
constants x y : real
check sine x
check sine n
check sine i
constant int_has_add : has_add int
constant real_has_add : has_add real
attribute [instance] int_has_add real_has_add
check x + i
/- The following one does not work because the implicit argument ?A of add is bound to int
when x is processed. -/
check i + x -- FAIL
/- The workaround is to use the explicit lift -/
check ↑i + x
check x + n
check n + x -- FAIL
check ↑n + x
end hidden
|
14d3368e879795c3d3faaefdd7662d7c368ca49d | 26ac254ecb57ffcb886ff709cf018390161a9225 | /src/set_theory/cardinal.lean | 9643346ae322445f47a0c791ef62dea635550dc8 | [
"Apache-2.0"
] | permissive | eric-wieser/mathlib | 42842584f584359bbe1fc8b88b3ff937c8acd72d | d0df6b81cd0920ad569158c06a3fd5abb9e63301 | refs/heads/master | 1,669,546,404,255 | 1,595,254,668,000 | 1,595,254,668,000 | 281,173,504 | 0 | 0 | Apache-2.0 | 1,595,263,582,000 | 1,595,263,581,000 | null | UTF-8 | Lean | false | false | 45,955 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Johannes Hölzl, Mario Carneiro
-/
import data.set.countable
import set_theory.schroeder_bernstein
import data.fintype.card
/-!
# Cardinal Numbers
We define cardinal numbers as a quotient of types under the equivalence relation of equinumerity.
We define the order on cardinal numbers, define omega, and do basic cardinal arithmetic:
addition, multiplication, power, cardinal successor, minimum, supremum,
infinitary sums and products
## Implementation notes
* There is a type of cardinal numbers in every universe level: `cardinal.{u} : Type (u + 1)`
is the quotient of types in `Type u`.
There is a lift operation lifting cardinal numbers to a higher level.
* Cardinal arithmetic specifically for infinite cardinals (like `κ * κ = κ`) is in the file
`set_theory/ordinal.lean`, because concepts from that file are used in the proof.
## References
* <https://en.wikipedia.org/wiki/Cardinal_number>
## Tags
cardinal number, cardinal arithmetic, cardinal exponentiation, omega
-/
open function set
open_locale classical
universes u v w x
variables {α β : Type u}
/-- The equivalence relation on types given by equivalence (bijective correspondence) of types.
Quotienting by this equivalence relation gives the cardinal numbers.
-/
instance cardinal.is_equivalent : setoid (Type u) :=
{ r := λα β, nonempty (α ≃ β),
iseqv := ⟨λα,
⟨equiv.refl α⟩,
λα β ⟨e⟩, ⟨e.symm⟩,
λα β γ ⟨e₁⟩ ⟨e₂⟩, ⟨e₁.trans e₂⟩⟩ }
/-- `cardinal.{u}` is the type of cardinal numbers in `Type u`,
defined as the quotient of `Type u` by existence of an equivalence
(a bijection with explicit inverse). -/
def cardinal : Type (u + 1) := quotient cardinal.is_equivalent
namespace cardinal
/-- The cardinal number of a type -/
def mk : Type u → cardinal := quotient.mk
localized "notation `#` := cardinal.mk" in cardinal
protected lemma eq : mk α = mk β ↔ nonempty (α ≃ β) := quotient.eq
@[simp] theorem mk_def (α : Type u) : @eq cardinal ⟦α⟧ (mk α) := rfl
@[simp] theorem mk_out (c : cardinal) : mk (c.out) = c := quotient.out_eq _
/-- We define the order on cardinal numbers by `mk α ≤ mk β` if and only if
there exists an embedding (injective function) from α to β. -/
instance : has_le cardinal.{u} :=
⟨λq₁ q₂, quotient.lift_on₂ q₁ q₂ (λα β, nonempty $ α ↪ β) $
assume α β γ δ ⟨e₁⟩ ⟨e₂⟩,
propext ⟨assume ⟨e⟩, ⟨e.congr e₁ e₂⟩, assume ⟨e⟩, ⟨e.congr e₁.symm e₂.symm⟩⟩⟩
theorem mk_le_of_injective {α β : Type u} {f : α → β} (hf : injective f) : mk α ≤ mk β :=
⟨⟨f, hf⟩⟩
theorem mk_le_of_surjective {α β : Type u} {f : α → β} (hf : surjective f) : mk β ≤ mk α :=
⟨embedding.of_surjective f hf⟩
theorem le_mk_iff_exists_set {c : cardinal} {α : Type u} :
c ≤ mk α ↔ ∃ p : set α, mk p = c :=
⟨quotient.induction_on c $ λ β ⟨⟨f, hf⟩⟩,
⟨set.range f, eq.symm $ quot.sound ⟨equiv.set.range f hf⟩⟩,
λ ⟨p, e⟩, e ▸ ⟨⟨subtype.val, λ a b, subtype.eq⟩⟩⟩
theorem out_embedding {c c' : cardinal} : c ≤ c' ↔ nonempty (c.out ↪ c'.out) :=
by { transitivity _, rw [←quotient.out_eq c, ←quotient.out_eq c'], refl }
instance : linear_order cardinal.{u} :=
{ le := (≤),
le_refl := by rintros ⟨α⟩; exact ⟨embedding.refl _⟩,
le_trans := by rintros ⟨α⟩ ⟨β⟩ ⟨γ⟩ ⟨e₁⟩ ⟨e₂⟩; exact ⟨e₁.trans e₂⟩,
le_antisymm := by rintros ⟨α⟩ ⟨β⟩ ⟨e₁⟩ ⟨e₂⟩; exact quotient.sound (e₁.antisymm e₂),
le_total := by rintros ⟨α⟩ ⟨β⟩; exact embedding.total }
noncomputable instance : decidable_linear_order cardinal.{u} := classical.DLO _
noncomputable instance : distrib_lattice cardinal.{u} := by apply_instance -- short-circuit type class inference
instance : has_zero cardinal.{u} := ⟨⟦pempty⟧⟩
instance : inhabited cardinal.{u} := ⟨0⟩
theorem ne_zero_iff_nonempty {α : Type u} : mk α ≠ 0 ↔ nonempty α :=
not_iff_comm.1
⟨λ h, quotient.sound ⟨(equiv.empty_of_not_nonempty h).trans equiv.empty_equiv_pempty⟩,
λ e, let ⟨h⟩ := quotient.exact e in λ ⟨a⟩, (h a).elim⟩
instance : has_one cardinal.{u} := ⟨⟦punit⟧⟩
instance : nontrivial cardinal.{u} :=
⟨⟨1, 0, ne_zero_iff_nonempty.2 ⟨punit.star⟩⟩⟩
theorem le_one_iff_subsingleton {α : Type u} : mk α ≤ 1 ↔ subsingleton α :=
⟨λ ⟨f⟩, ⟨λ a b, f.injective (subsingleton.elim _ _)⟩,
λ ⟨h⟩, ⟨⟨λ a, punit.star, λ a b _, h _ _⟩⟩⟩
theorem one_lt_iff_nontrivial {α : Type u} : 1 < mk α ↔ nontrivial α :=
by { rw [← not_iff_not, not_nontrivial_iff_subsingleton, ← le_one_iff_subsingleton], simp }
instance : has_add cardinal.{u} :=
⟨λq₁ q₂, quotient.lift_on₂ q₁ q₂ (λα β, mk (α ⊕ β)) $ assume α β γ δ ⟨e₁⟩ ⟨e₂⟩,
quotient.sound ⟨equiv.sum_congr e₁ e₂⟩⟩
@[simp] theorem add_def (α β) : mk α + mk β = mk (α ⊕ β) := rfl
instance : has_mul cardinal.{u} :=
⟨λq₁ q₂, quotient.lift_on₂ q₁ q₂ (λα β, mk (α × β)) $ assume α β γ δ ⟨e₁⟩ ⟨e₂⟩,
quotient.sound ⟨equiv.prod_congr e₁ e₂⟩⟩
@[simp] theorem mul_def (α β : Type u) : mk α * mk β = mk (α × β) := rfl
private theorem add_comm (a b : cardinal.{u}) : a + b = b + a :=
quotient.induction_on₂ a b $ assume α β, quotient.sound ⟨equiv.sum_comm α β⟩
private theorem mul_comm (a b : cardinal.{u}) : a * b = b * a :=
quotient.induction_on₂ a b $ assume α β, quotient.sound ⟨equiv.prod_comm α β⟩
private theorem zero_add (a : cardinal.{u}) : 0 + a = a :=
quotient.induction_on a $ assume α, quotient.sound ⟨equiv.pempty_sum α⟩
private theorem zero_mul (a : cardinal.{u}) : 0 * a = 0 :=
quotient.induction_on a $ assume α, quotient.sound ⟨equiv.pempty_prod α⟩
private theorem one_mul (a : cardinal.{u}) : 1 * a = a :=
quotient.induction_on a $ assume α, quotient.sound ⟨equiv.punit_prod α⟩
private theorem left_distrib (a b c : cardinal.{u}) : a * (b + c) = a * b + a * c :=
quotient.induction_on₃ a b c $ assume α β γ, quotient.sound ⟨equiv.prod_sum_distrib α β γ⟩
instance : comm_semiring cardinal.{u} :=
{ zero := 0,
one := 1,
add := (+),
mul := (*),
zero_add := zero_add,
add_zero := assume a, by rw [add_comm a 0, zero_add a],
add_assoc := λa b c, quotient.induction_on₃ a b c $ assume α β γ,
quotient.sound ⟨equiv.sum_assoc α β γ⟩,
add_comm := add_comm,
zero_mul := zero_mul,
mul_zero := assume a, by rw [mul_comm a 0, zero_mul a],
one_mul := one_mul,
mul_one := assume a, by rw [mul_comm a 1, one_mul a],
mul_assoc := λa b c, quotient.induction_on₃ a b c $ assume α β γ,
quotient.sound ⟨equiv.prod_assoc α β γ⟩,
mul_comm := mul_comm,
left_distrib := left_distrib,
right_distrib := assume a b c,
by rw [mul_comm (a + b) c, left_distrib c a b, mul_comm c a, mul_comm c b] }
/-- The cardinal exponential. `mk α ^ mk β` is the cardinal of `β → α`. -/
protected def power (a b : cardinal.{u}) : cardinal.{u} :=
quotient.lift_on₂ a b (λα β, mk (β → α)) $ assume α₁ α₂ β₁ β₂ ⟨e₁⟩ ⟨e₂⟩,
quotient.sound ⟨equiv.arrow_congr e₂ e₁⟩
instance : has_pow cardinal cardinal := ⟨cardinal.power⟩
local infixr ^ := @has_pow.pow cardinal cardinal cardinal.has_pow
@[simp] theorem power_def (α β) : mk α ^ mk β = mk (β → α) := rfl
@[simp] theorem power_zero {a : cardinal} : a ^ 0 = 1 :=
quotient.induction_on a $ assume α, quotient.sound
⟨equiv.pempty_arrow_equiv_punit α⟩
@[simp] theorem power_one {a : cardinal} : a ^ 1 = a :=
quotient.induction_on a $ assume α, quotient.sound
⟨equiv.punit_arrow_equiv α⟩
@[simp] theorem one_power {a : cardinal} : 1 ^ a = 1 :=
quotient.induction_on a $ assume α, quotient.sound
⟨equiv.arrow_punit_equiv_punit α⟩
@[simp] theorem prop_eq_two : mk (ulift Prop) = 2 :=
quot.sound ⟨equiv.ulift.trans $ equiv.Prop_equiv_bool.trans equiv.bool_equiv_punit_sum_punit⟩
@[simp] theorem zero_power {a : cardinal} : a ≠ 0 → 0 ^ a = 0 :=
quotient.induction_on a $ assume α heq,
nonempty.rec_on (ne_zero_iff_nonempty.1 heq) $ assume a,
quotient.sound ⟨equiv.equiv_pempty $ assume f, pempty.rec (λ _, false) (f a)⟩
theorem power_ne_zero {a : cardinal} (b) : a ≠ 0 → a ^ b ≠ 0 :=
quotient.induction_on₂ a b $ λ α β h,
let ⟨a⟩ := ne_zero_iff_nonempty.1 h in
ne_zero_iff_nonempty.2 ⟨λ _, a⟩
theorem mul_power {a b c : cardinal} : (a * b) ^ c = a ^ c * b ^ c :=
quotient.induction_on₃ a b c $ assume α β γ,
quotient.sound ⟨equiv.arrow_prod_equiv_prod_arrow α β γ⟩
theorem power_add {a b c : cardinal} : a ^ (b + c) = a ^ b * a ^ c :=
quotient.induction_on₃ a b c $ assume α β γ,
quotient.sound ⟨equiv.sum_arrow_equiv_prod_arrow β γ α⟩
theorem power_mul {a b c : cardinal} : (a ^ b) ^ c = a ^ (b * c) :=
by rw [_root_.mul_comm b c];
from (quotient.induction_on₃ a b c $ assume α β γ,
quotient.sound ⟨equiv.arrow_arrow_equiv_prod_arrow γ β α⟩)
@[simp] lemma pow_cast_right (κ : cardinal.{u}) :
∀ n : ℕ, (κ ^ (↑n : cardinal.{u})) = @has_pow.pow _ _ monoid.has_pow κ n
| 0 := by simp
| (_+1) := by rw [nat.cast_succ, power_add, power_one, _root_.mul_comm, pow_succ, pow_cast_right]
section order_properties
open sum
theorem zero_le : ∀(a : cardinal), 0 ≤ a :=
by rintro ⟨α⟩; exact ⟨embedding.of_not_nonempty $ λ ⟨a⟩, a.elim⟩
theorem le_zero (a : cardinal) : a ≤ 0 ↔ a = 0 :=
by simp [le_antisymm_iff, zero_le]
theorem pos_iff_ne_zero {o : cardinal} : 0 < o ↔ o ≠ 0 :=
by simp [lt_iff_le_and_ne, eq_comm, zero_le]
@[simp] theorem zero_lt_one : (0 : cardinal) < 1 :=
lt_of_le_of_ne (zero_le _) zero_ne_one
lemma zero_power_le (c : cardinal.{u}) : (0 : cardinal.{u}) ^ c ≤ 1 :=
by { by_cases h : c = 0, rw [h, power_zero], rw [zero_power h], apply zero_le }
theorem add_le_add : ∀{a b c d : cardinal}, a ≤ b → c ≤ d → a + c ≤ b + d :=
by rintros ⟨α⟩ ⟨β⟩ ⟨γ⟩ ⟨δ⟩ ⟨e₁⟩ ⟨e₂⟩; exact ⟨e₁.sum_map e₂⟩
theorem add_le_add_left (a) {b c : cardinal} : b ≤ c → a + b ≤ a + c :=
add_le_add (le_refl _)
theorem add_le_add_right {a b : cardinal} (c) (h : a ≤ b) : a + c ≤ b + c :=
add_le_add h (le_refl _)
theorem le_add_right (a b : cardinal) : a ≤ a + b :=
by simpa using add_le_add_left a (zero_le b)
theorem le_add_left (a b : cardinal) : a ≤ b + a :=
by simpa using add_le_add_right a (zero_le b)
theorem mul_le_mul : ∀{a b c d : cardinal}, a ≤ b → c ≤ d → a * c ≤ b * d :=
by rintros ⟨α⟩ ⟨β⟩ ⟨γ⟩ ⟨δ⟩ ⟨e₁⟩ ⟨e₂⟩; exact ⟨e₁.prod_map e₂⟩
theorem mul_le_mul_left (a) {b c : cardinal} : b ≤ c → a * b ≤ a * c :=
mul_le_mul (le_refl _)
theorem mul_le_mul_right {a b : cardinal} (c) (h : a ≤ b) : a * c ≤ b * c :=
mul_le_mul h (le_refl _)
theorem power_le_power_left : ∀{a b c : cardinal}, a ≠ 0 → b ≤ c → a ^ b ≤ a ^ c :=
by rintros ⟨α⟩ ⟨β⟩ ⟨γ⟩ hα ⟨e⟩; exact
let ⟨a⟩ := ne_zero_iff_nonempty.1 hα in
⟨@embedding.arrow_congr_right _ _ _ ⟨a⟩ e⟩
theorem power_le_max_power_one {a b c : cardinal} (h : b ≤ c) : a ^ b ≤ max (a ^ c) 1 :=
begin
by_cases ha : a = 0,
simp [ha, zero_power_le],
exact le_trans (power_le_power_left ha h) (le_max_left _ _)
end
theorem power_le_power_right {a b c : cardinal} : a ≤ b → a ^ c ≤ b ^ c :=
quotient.induction_on₃ a b c $ assume α β γ ⟨e⟩, ⟨embedding.arrow_congr_left e⟩
theorem le_iff_exists_add {a b : cardinal} : a ≤ b ↔ ∃ c, b = a + c :=
⟨quotient.induction_on₂ a b $ λ α β ⟨⟨f, hf⟩⟩,
have (α ⊕ ((range f)ᶜ : set β)) ≃ β, from
(equiv.sum_congr (equiv.set.range f hf) (equiv.refl _)).trans $
(equiv.set.sum_compl (range f)),
⟨⟦↥(range f)ᶜ⟧, quotient.sound ⟨this.symm⟩⟩,
λ ⟨c, e⟩, add_zero a ▸ e.symm ▸ add_le_add_left _ (zero_le _)⟩
end order_properties
instance : order_bot cardinal.{u} :=
{ bot := 0, bot_le := zero_le, ..cardinal.linear_order }
instance : canonically_ordered_add_monoid cardinal.{u} :=
{ add_le_add_left := λ a b h c, add_le_add_left _ h,
lt_of_add_lt_add_left := λ a b c, lt_imp_lt_of_le_imp_le (add_le_add_left _),
le_iff_exists_add := @le_iff_exists_add,
..cardinal.order_bot,
..cardinal.comm_semiring, ..cardinal.linear_order }
theorem cantor : ∀(a : cardinal.{u}), a < 2 ^ a :=
by rw ← prop_eq_two; rintros ⟨a⟩; exact ⟨
⟨⟨λ a b, ⟨a = b⟩, λ a b h, cast (ulift.up.inj (@congr_fun _ _ _ _ h b)).symm rfl⟩⟩,
λ ⟨⟨f, hf⟩⟩, cantor_injective (λ s, f (λ a, ⟨s a⟩)) $
λ s t h, by funext a; injection congr_fun (hf h) a⟩
instance : no_top_order cardinal.{u} :=
{ no_top := λ a, ⟨_, cantor a⟩, ..cardinal.linear_order }
/-- The minimum cardinal in a family of cardinals (the existence
of which is provided by `injective_min`). -/
noncomputable def min {ι} (I : nonempty ι) (f : ι → cardinal) : cardinal :=
f $ classical.some $
@embedding.min_injective _ (λ i, (f i).out) I
theorem min_eq {ι} (I) (f : ι → cardinal) : ∃ i, min I f = f i :=
⟨_, rfl⟩
theorem min_le {ι I} (f : ι → cardinal) (i) : min I f ≤ f i :=
by rw [← mk_out (min I f), ← mk_out (f i)]; exact
let ⟨g⟩ := classical.some_spec
(@embedding.min_injective _ (λ i, (f i).out) I) in
⟨g i⟩
theorem le_min {ι I} {f : ι → cardinal} {a} : a ≤ min I f ↔ ∀ i, a ≤ f i :=
⟨λ h i, le_trans h (min_le _ _),
λ h, let ⟨i, e⟩ := min_eq I f in e.symm ▸ h i⟩
protected theorem wf : @well_founded cardinal.{u} (<) :=
⟨λ a, classical.by_contradiction $ λ h,
let ι := {c :cardinal // ¬ acc (<) c},
f : ι → cardinal := subtype.val,
⟨⟨c, hc⟩, hi⟩ := @min_eq ι ⟨⟨_, h⟩⟩ f in
hc (acc.intro _ (λ j ⟨_, h'⟩,
classical.by_contradiction $ λ hj, h' $
by have := min_le f ⟨j, hj⟩; rwa hi at this))⟩
instance has_wf : @has_well_founded cardinal.{u} := ⟨(<), cardinal.wf⟩
instance wo : @is_well_order cardinal.{u} (<) := ⟨cardinal.wf⟩
/-- The successor cardinal - the smallest cardinal greater than
`c`. This is not the same as `c + 1` except in the case of finite `c`. -/
noncomputable def succ (c : cardinal) : cardinal :=
@min {c' // c < c'} ⟨⟨_, cantor _⟩⟩ subtype.val
theorem lt_succ_self (c : cardinal) : c < succ c :=
by cases min_eq _ _ with s e; rw [succ, e]; exact s.2
theorem succ_le {a b : cardinal} : succ a ≤ b ↔ a < b :=
⟨lt_of_lt_of_le (lt_succ_self _), λ h,
by exact min_le _ (subtype.mk b h)⟩
theorem lt_succ {a b : cardinal} : a < succ b ↔ a ≤ b :=
by rw [← not_le, succ_le, not_lt]
theorem add_one_le_succ (c : cardinal) : c + 1 ≤ succ c :=
begin
refine quot.induction_on c (λ α, _) (lt_succ_self c),
refine quot.induction_on (succ (quot.mk setoid.r α)) (λ β h, _),
cases h.left with f,
have : ¬ surjective f := λ hn,
ne_of_lt h (quotient.sound ⟨equiv.of_bijective f ⟨f.injective, hn⟩⟩),
cases classical.not_forall.1 this with b nex,
refine ⟨⟨sum.rec (by exact f) _, _⟩⟩,
{ exact λ _, b },
{ intros a b h, rcases a with a|⟨⟨⟨⟩⟩⟩; rcases b with b|⟨⟨⟨⟩⟩⟩,
{ rw f.injective h },
{ exact nex.elim ⟨_, h⟩ },
{ exact nex.elim ⟨_, h.symm⟩ },
{ refl } }
end
lemma succ_ne_zero (c : cardinal) : succ c ≠ 0 :=
by { rw [←pos_iff_ne_zero, lt_succ], apply zero_le }
/-- The indexed sum of cardinals is the cardinality of the
indexed disjoint union, i.e. sigma type. -/
def sum {ι} (f : ι → cardinal) : cardinal := mk Σ i, (f i).out
theorem le_sum {ι} (f : ι → cardinal) (i) : f i ≤ sum f :=
by rw ← quotient.out_eq (f i); exact
⟨⟨λ a, ⟨i, a⟩, λ a b h, eq_of_heq $ by injection h⟩⟩
@[simp] theorem sum_mk {ι} (f : ι → Type*) : sum (λ i, mk (f i)) = mk (Σ i, f i) :=
quot.sound ⟨equiv.sigma_congr_right $ λ i,
classical.choice $ quotient.exact $ quot.out_eq $ mk (f i)⟩
theorem sum_const (ι : Type u) (a : cardinal.{u}) : sum (λ _:ι, a) = mk ι * a :=
quotient.induction_on a $ λ α, by simp; exact
quotient.sound ⟨equiv.sigma_equiv_prod _ _⟩
theorem sum_le_sum {ι} (f g : ι → cardinal) (H : ∀ i, f i ≤ g i) : sum f ≤ sum g :=
⟨(embedding.refl _).sigma_map $ λ i, classical.choice $
by have := H i; rwa [← quot.out_eq (f i), ← quot.out_eq (g i)] at this⟩
/-- The indexed supremum of cardinals is the smallest cardinal above
everything in the family. -/
noncomputable def sup {ι} (f : ι → cardinal) : cardinal :=
@min {c // ∀ i, f i ≤ c} ⟨⟨sum f, le_sum f⟩⟩ (λ a, a.1)
theorem le_sup {ι} (f : ι → cardinal) (i) : f i ≤ sup f :=
by dsimp [sup]; cases min_eq _ _ with c hc; rw hc; exact c.2 i
theorem sup_le {ι} {f : ι → cardinal} {a} : sup f ≤ a ↔ ∀ i, f i ≤ a :=
⟨λ h i, le_trans (le_sup _ _) h,
λ h, by dsimp [sup]; change a with (⟨a, h⟩:subtype _).1; apply min_le⟩
theorem sup_le_sup {ι} (f g : ι → cardinal) (H : ∀ i, f i ≤ g i) : sup f ≤ sup g :=
sup_le.2 $ λ i, le_trans (H i) (le_sup _ _)
theorem sup_le_sum {ι} (f : ι → cardinal) : sup f ≤ sum f :=
sup_le.2 $ le_sum _
theorem sum_le_sup {ι : Type u} (f : ι → cardinal.{u}) : sum f ≤ mk ι * sup.{u u} f :=
by rw ← sum_const; exact sum_le_sum _ _ (le_sup _)
theorem sup_eq_zero {ι} {f : ι → cardinal} (h : ι → false) : sup f = 0 :=
by { rw [←le_zero, sup_le], intro x, exfalso, exact h x }
/-- The indexed product of cardinals is the cardinality of the Pi type
(dependent product). -/
def prod {ι : Type u} (f : ι → cardinal) : cardinal := mk (Π i, (f i).out)
@[simp] theorem prod_mk {ι} (f : ι → Type*) : prod (λ i, mk (f i)) = mk (Π i, f i) :=
quot.sound ⟨equiv.Pi_congr_right $ λ i,
classical.choice $ quotient.exact $ mk_out $ mk (f i)⟩
theorem prod_const (ι : Type u) (a : cardinal.{u}) : prod (λ _:ι, a) = a ^ mk ι :=
quotient.induction_on a $ by simp
theorem prod_le_prod {ι} (f g : ι → cardinal) (H : ∀ i, f i ≤ g i) : prod f ≤ prod g :=
⟨embedding.Pi_congr_right $ λ i, classical.choice $
by have := H i; rwa [← mk_out (f i), ← mk_out (g i)] at this⟩
theorem prod_ne_zero {ι} (f : ι → cardinal) : prod f ≠ 0 ↔ ∀ i, f i ≠ 0 :=
begin
conv in (f _) {rw ← mk_out (f i)},
simp [prod, ne_zero_iff_nonempty, -mk_out, -ne.def],
exact ⟨λ ⟨F⟩ i, ⟨F i⟩, λ h, ⟨λ i, classical.choice (h i)⟩⟩,
end
theorem prod_eq_zero {ι} (f : ι → cardinal) : prod f = 0 ↔ ∃ i, f i = 0 :=
not_iff_not.1 $ by simpa using prod_ne_zero f
/-- The universe lift operation on cardinals. You can specify the universes explicitly with
`lift.{u v} : cardinal.{u} → cardinal.{max u v}` -/
def lift (c : cardinal.{u}) : cardinal.{max u v} :=
quotient.lift_on c (λ α, ⟦ulift α⟧) $ λ α β ⟨e⟩,
quotient.sound ⟨equiv.ulift.trans $ e.trans equiv.ulift.symm⟩
theorem lift_mk (α) : lift.{u v} (mk α) = mk (ulift.{v u} α) := rfl
theorem lift_umax : lift.{u (max u v)} = lift.{u v} :=
funext $ λ a, quot.induction_on a $ λ α,
quotient.sound ⟨equiv.ulift.trans equiv.ulift.symm⟩
theorem lift_id' (a : cardinal) : lift a = a :=
quot.induction_on a $ λ α, quot.sound ⟨equiv.ulift⟩
@[simp] theorem lift_id : ∀ a, lift.{u u} a = a := lift_id'.{u u}
@[simp] theorem lift_lift (a : cardinal) : lift.{(max u v) w} (lift.{u v} a) = lift.{u (max v w)} a :=
quot.induction_on a $ λ α,
quotient.sound ⟨equiv.ulift.trans $ equiv.ulift.trans equiv.ulift.symm⟩
theorem lift_mk_le {α : Type u} {β : Type v} :
lift.{u (max v w)} (mk α) ≤ lift.{v (max u w)} (mk β) ↔ nonempty (α ↪ β) :=
⟨λ ⟨f⟩, ⟨embedding.congr equiv.ulift equiv.ulift f⟩,
λ ⟨f⟩, ⟨embedding.congr equiv.ulift.symm equiv.ulift.symm f⟩⟩
theorem lift_mk_eq {α : Type u} {β : Type v} :
lift.{u (max v w)} (mk α) = lift.{v (max u w)} (mk β) ↔ nonempty (α ≃ β) :=
quotient.eq.trans
⟨λ ⟨f⟩, ⟨equiv.ulift.symm.trans $ f.trans equiv.ulift⟩,
λ ⟨f⟩, ⟨equiv.ulift.trans $ f.trans equiv.ulift.symm⟩⟩
@[simp] theorem lift_le {a b : cardinal} : lift a ≤ lift b ↔ a ≤ b :=
quotient.induction_on₂ a b $ λ α β,
by rw ← lift_umax; exact lift_mk_le
@[simp] theorem lift_inj {a b : cardinal} : lift a = lift b ↔ a = b :=
by simp [le_antisymm_iff]
@[simp] theorem lift_lt {a b : cardinal} : lift a < lift b ↔ a < b :=
by simp [lt_iff_le_not_le, -not_le]
@[simp] theorem lift_zero : lift 0 = 0 :=
quotient.sound ⟨equiv.ulift.trans equiv.pempty_equiv_pempty⟩
@[simp] theorem lift_one : lift 1 = 1 :=
quotient.sound ⟨equiv.ulift.trans equiv.punit_equiv_punit⟩
@[simp] theorem lift_add (a b) : lift (a + b) = lift a + lift b :=
quotient.induction_on₂ a b $ λ α β,
quotient.sound ⟨equiv.ulift.trans (equiv.sum_congr equiv.ulift equiv.ulift).symm⟩
@[simp] theorem lift_mul (a b) : lift (a * b) = lift a * lift b :=
quotient.induction_on₂ a b $ λ α β,
quotient.sound ⟨equiv.ulift.trans (equiv.prod_congr equiv.ulift equiv.ulift).symm⟩
@[simp] theorem lift_power (a b) : lift (a ^ b) = lift a ^ lift b :=
quotient.induction_on₂ a b $ λ α β,
quotient.sound ⟨equiv.ulift.trans (equiv.arrow_congr equiv.ulift equiv.ulift).symm⟩
@[simp] theorem lift_two_power (a) : lift (2 ^ a) = 2 ^ lift a :=
by simp [bit0]
@[simp] theorem lift_min {ι I} (f : ι → cardinal) : lift (min I f) = min I (lift ∘ f) :=
le_antisymm (le_min.2 $ λ a, lift_le.2 $ min_le _ a) $
let ⟨i, e⟩ := min_eq I (lift ∘ f) in
by rw e; exact lift_le.2 (le_min.2 $ λ j, lift_le.1 $
by have := min_le (lift ∘ f) j; rwa e at this)
theorem lift_down {a : cardinal.{u}} {b : cardinal.{max u v}} :
b ≤ lift a → ∃ a', lift a' = b :=
quotient.induction_on₂ a b $ λ α β,
by dsimp; rw [← lift_id (mk β), ← lift_umax, ← lift_umax.{u v}, lift_mk_le]; exact
λ ⟨f⟩, ⟨mk (set.range f), eq.symm $ lift_mk_eq.2
⟨embedding.equiv_of_surjective
(embedding.cod_restrict _ f set.mem_range_self)
$ λ ⟨a, ⟨b, e⟩⟩, ⟨b, subtype.eq e⟩⟩⟩
theorem le_lift_iff {a : cardinal.{u}} {b : cardinal.{max u v}} :
b ≤ lift a ↔ ∃ a', lift a' = b ∧ a' ≤ a :=
⟨λ h, let ⟨a', e⟩ := lift_down h in ⟨a', e, lift_le.1 $ e.symm ▸ h⟩,
λ ⟨a', e, h⟩, e ▸ lift_le.2 h⟩
theorem lt_lift_iff {a : cardinal.{u}} {b : cardinal.{max u v}} :
b < lift a ↔ ∃ a', lift a' = b ∧ a' < a :=
⟨λ h, let ⟨a', e⟩ := lift_down (le_of_lt h) in
⟨a', e, lift_lt.1 $ e.symm ▸ h⟩,
λ ⟨a', e, h⟩, e ▸ lift_lt.2 h⟩
@[simp] theorem lift_succ (a) : lift (succ a) = succ (lift a) :=
le_antisymm
(le_of_not_gt $ λ h, begin
rcases lt_lift_iff.1 h with ⟨b, e, h⟩,
rw [lt_succ, ← lift_le, e] at h,
exact not_lt_of_le h (lt_succ_self _)
end)
(succ_le.2 $ lift_lt.2 $ lt_succ_self _)
@[simp] theorem lift_max {a : cardinal.{u}} {b : cardinal.{v}} :
lift.{u (max v w)} a = lift.{v (max u w)} b ↔ lift.{u v} a = lift.{v u} b :=
calc lift.{u (max v w)} a = lift.{v (max u w)} b
↔ lift.{(max u v) w} (lift.{u v} a)
= lift.{(max u v) w} (lift.{v u} b) : by simp
... ↔ lift.{u v} a = lift.{v u} b : lift_inj
theorem mk_prod {α : Type u} {β : Type v} :
mk (α × β) = lift.{u v} (mk α) * lift.{v u} (mk β) :=
quotient.sound ⟨equiv.prod_congr (equiv.ulift).symm (equiv.ulift).symm⟩
theorem sum_const_eq_lift_mul (ι : Type u) (a : cardinal.{v}) :
sum (λ _:ι, a) = lift.{u v} (mk ι) * lift.{v u} a :=
begin
apply quotient.induction_on a,
intro α,
simp only [cardinal.mk_def, cardinal.sum_mk, cardinal.lift_id],
convert mk_prod using 1,
exact quotient.sound ⟨equiv.sigma_equiv_prod ι α⟩,
end
/-- `ω` is the smallest infinite cardinal, also known as ℵ₀. -/
def omega : cardinal.{u} := lift (mk ℕ)
lemma mk_nat : mk nat = omega := (lift_id _).symm
theorem omega_ne_zero : omega ≠ 0 :=
ne_zero_iff_nonempty.2 ⟨⟨0⟩⟩
theorem omega_pos : 0 < omega :=
pos_iff_ne_zero.2 omega_ne_zero
@[simp] theorem lift_omega : lift omega = omega := lift_lift _
/- properties about the cast from nat -/
@[simp] theorem mk_fin : ∀ (n : ℕ), mk (fin n) = n
| 0 := quotient.sound ⟨(equiv.pempty_of_not_nonempty $ λ ⟨h⟩, h.elim0)⟩
| (n+1) := by rw [nat.cast_succ, ← mk_fin]; exact
quotient.sound (fintype.card_eq.1 $ by simp)
@[simp] theorem lift_nat_cast (n : ℕ) : lift n = n :=
by induction n; simp *
lemma lift_eq_nat_iff {a : cardinal.{u}} {n : ℕ} : lift.{u v} a = n ↔ a = n :=
by rw [← lift_nat_cast.{u v} n, lift_inj]
lemma nat_eq_lift_eq_iff {n : ℕ} {a : cardinal.{u}} :
(n : cardinal) = lift.{u v} a ↔ (n : cardinal) = a :=
by rw [← lift_nat_cast.{u v} n, lift_inj]
theorem lift_mk_fin (n : ℕ) : lift (mk (fin n)) = n := by simp
theorem fintype_card (α : Type u) [fintype α] : mk α = fintype.card α :=
by rw [← lift_mk_fin.{u}, ← lift_id (mk α), lift_mk_eq.{u 0 u}];
exact fintype.card_eq.1 (by simp)
theorem card_le_of_finset {α} (s : finset α) :
(s.card : cardinal) ≤ cardinal.mk α :=
begin
rw (_ : (s.card : cardinal) = cardinal.mk (↑s : set α)),
{ exact ⟨function.embedding.subtype _⟩ },
rw [cardinal.fintype_card, fintype.card_coe]
end
@[simp, norm_cast] theorem nat_cast_pow {m n : ℕ} : (↑(pow m n) : cardinal) = m ^ n :=
by induction n; simp [nat.pow_succ, -_root_.add_comm, power_add, *]
@[simp, norm_cast] theorem nat_cast_le {m n : ℕ} : (m : cardinal) ≤ n ↔ m ≤ n :=
by rw [← lift_mk_fin, ← lift_mk_fin, lift_le]; exact
⟨λ ⟨⟨f, hf⟩⟩, begin
have : _ = fintype.card _ := finset.card_image_of_injective finset.univ hf,
simp at this,
rw [← fintype.card_fin n, ← this],
exact finset.card_le_of_subset (finset.subset_univ _)
end,
λ h, ⟨⟨λ i, ⟨i.1, lt_of_lt_of_le i.2 h⟩, λ a b h,
have _, from fin.veq_of_eq h, fin.eq_of_veq this⟩⟩⟩
@[simp, norm_cast] theorem nat_cast_lt {m n : ℕ} : (m : cardinal) < n ↔ m < n :=
by simp [lt_iff_le_not_le, -not_le]
@[simp, norm_cast] theorem nat_cast_inj {m n : ℕ} : (m : cardinal) = n ↔ m = n :=
by simp [le_antisymm_iff]
@[simp, norm_cast, priority 900] theorem nat_succ (n : ℕ) : (n.succ : cardinal) = succ n :=
le_antisymm (add_one_le_succ _) (succ_le.2 $ nat_cast_lt.2 $ nat.lt_succ_self _)
@[simp] theorem succ_zero : succ 0 = 1 :=
by norm_cast
theorem cantor' (a) {b : cardinal} (hb : 1 < b) : a < b ^ a :=
by rw [← succ_le, (by norm_cast : succ 1 = 2)] at hb;
exact lt_of_lt_of_le (cantor _) (power_le_power_right hb)
theorem one_le_iff_pos {c : cardinal} : 1 ≤ c ↔ 0 < c :=
by rw [← succ_zero, succ_le]
theorem one_le_iff_ne_zero {c : cardinal} : 1 ≤ c ↔ c ≠ 0 :=
by rw [one_le_iff_pos, pos_iff_ne_zero]
theorem nat_lt_omega (n : ℕ) : (n : cardinal.{u}) < omega :=
succ_le.1 $ by rw [← nat_succ, ← lift_mk_fin, omega, lift_mk_le.{0 0 u}]; exact
⟨⟨fin.val, λ a b, fin.eq_of_veq⟩⟩
@[simp] theorem one_lt_omega : 1 < omega :=
by simpa using nat_lt_omega 1
theorem lt_omega {c : cardinal.{u}} : c < omega ↔ ∃ n : ℕ, c = n :=
⟨λ h, begin
rcases lt_lift_iff.1 h with ⟨c, rfl, h'⟩,
rcases le_mk_iff_exists_set.1 h'.1 with ⟨S, rfl⟩,
suffices : finite S,
{ cases this, resetI,
existsi fintype.card S,
rw [← lift_nat_cast.{0 u}, lift_inj, fintype_card S] },
by_contra nf,
have P : ∀ (n : ℕ) (IH : ∀ i<n, S), ∃ a : S, ¬ ∃ y h, IH y h = a :=
λ n IH,
let g : {i | i < n} → S := λ ⟨i, h⟩, IH i h in
classical.not_forall.1 (λ h, nf
⟨fintype.of_surjective g (λ a, subtype.exists.2 (h a))⟩),
let F : ℕ → S := nat.lt_wf.fix (λ n IH, classical.some (P n IH)),
refine not_le_of_lt h' ⟨⟨F, _⟩⟩,
suffices : ∀ (n : ℕ) (m < n), F m ≠ F n,
{ refine λ m n, not_imp_not.1 (λ ne, _),
rcases lt_trichotomy m n with h|h|h,
{ exact this n m h },
{ contradiction },
{ exact (this m n h).symm } },
intros n m h,
have := classical.some_spec (P n (λ y _, F y)),
rw [← show F n = classical.some (P n (λ y _, F y)),
from nat.lt_wf.fix_eq (λ n IH, classical.some (P n IH)) n] at this,
exact λ e, this ⟨m, h, e⟩,
end, λ ⟨n, e⟩, e.symm ▸ nat_lt_omega _⟩
theorem omega_le {c : cardinal.{u}} : omega ≤ c ↔ ∀ n : ℕ, (n:cardinal) ≤ c :=
⟨λ h n, le_trans (le_of_lt (nat_lt_omega _)) h,
λ h, le_of_not_lt $ λ hn, begin
rcases lt_omega.1 hn with ⟨n, rfl⟩,
exact not_le_of_lt (nat.lt_succ_self _) (nat_cast_le.1 (h (n+1)))
end⟩
theorem lt_omega_iff_fintype {α : Type u} : mk α < omega ↔ nonempty (fintype α) :=
lt_omega.trans ⟨λ ⟨n, e⟩, begin
rw [← lift_mk_fin n] at e,
cases quotient.exact e with f,
exact ⟨fintype.of_equiv _ f.symm⟩
end, λ ⟨_⟩, by exactI ⟨_, fintype_card _⟩⟩
theorem lt_omega_iff_finite {α} {S : set α} : mk S < omega ↔ finite S :=
lt_omega_iff_fintype
instance can_lift_cardinal_nat : can_lift cardinal ℕ :=
⟨ coe, λ x, x < omega, λ x hx, let ⟨n, hn⟩ := lt_omega.mp hx in ⟨n, hn.symm⟩⟩
theorem add_lt_omega {a b : cardinal} (ha : a < omega) (hb : b < omega) : a + b < omega :=
match a, b, lt_omega.1 ha, lt_omega.1 hb with
| _, _, ⟨m, rfl⟩, ⟨n, rfl⟩ := by rw [← nat.cast_add]; apply nat_lt_omega
end
lemma add_lt_omega_iff {a b : cardinal} : a + b < omega ↔ a < omega ∧ b < omega :=
⟨λ h, ⟨lt_of_le_of_lt (le_add_right _ _) h, lt_of_le_of_lt (le_add_left _ _) h⟩,
λ⟨h1, h2⟩, add_lt_omega h1 h2⟩
theorem mul_lt_omega {a b : cardinal} (ha : a < omega) (hb : b < omega) : a * b < omega :=
match a, b, lt_omega.1 ha, lt_omega.1 hb with
| _, _, ⟨m, rfl⟩, ⟨n, rfl⟩ := by rw [← nat.cast_mul]; apply nat_lt_omega
end
lemma mul_lt_omega_iff {a b : cardinal} : a * b < omega ↔ a = 0 ∨ b = 0 ∨ a < omega ∧ b < omega :=
begin
split,
{ intro h, by_cases ha : a = 0, { left, exact ha },
right, by_cases hb : b = 0, { left, exact hb },
right, rw [← ne, ← one_le_iff_ne_zero] at ha hb, split,
{ rw [← mul_one a], refine lt_of_le_of_lt (mul_le_mul (le_refl a) hb) h },
{ rw [← _root_.one_mul b], refine lt_of_le_of_lt (mul_le_mul ha (le_refl b)) h }},
rintro (rfl|rfl|⟨ha,hb⟩); simp only [*, mul_lt_omega, omega_pos, _root_.zero_mul, mul_zero]
end
lemma mul_lt_omega_iff_of_ne_zero {a b : cardinal} (ha : a ≠ 0) (hb : b ≠ 0) :
a * b < omega ↔ a < omega ∧ b < omega :=
by simp [mul_lt_omega_iff, ha, hb]
theorem power_lt_omega {a b : cardinal} (ha : a < omega) (hb : b < omega) : a ^ b < omega :=
match a, b, lt_omega.1 ha, lt_omega.1 hb with
| _, _, ⟨m, rfl⟩, ⟨n, rfl⟩ := by rw [← nat_cast_pow]; apply nat_lt_omega
end
lemma eq_one_iff_subsingleton_and_nonempty {α : Type*} :
mk α = 1 ↔ (subsingleton α ∧ nonempty α) :=
calc mk α = 1 ↔ mk α ≤ 1 ∧ ¬mk α < 1 : eq_iff_le_not_lt
... ↔ subsingleton α ∧ nonempty α :
begin
apply and_congr le_one_iff_subsingleton,
push_neg,
rw [one_le_iff_ne_zero, ne_zero_iff_nonempty]
end
theorem infinite_iff {α : Type u} : infinite α ↔ omega ≤ mk α :=
by rw [←not_lt, lt_omega_iff_fintype, not_nonempty_fintype]
lemma countable_iff (s : set α) : countable s ↔ mk s ≤ omega :=
begin
rw [countable_iff_exists_injective], split,
rintro ⟨f, hf⟩, exact ⟨embedding.trans ⟨f, hf⟩ equiv.ulift.symm.to_embedding⟩,
rintro ⟨f'⟩, cases embedding.trans f' equiv.ulift.to_embedding with f hf, exact ⟨f, hf⟩
end
lemma denumerable_iff {α : Type u} : nonempty (denumerable α) ↔ mk α = omega :=
⟨λ⟨h⟩, quotient.sound $ by exactI ⟨ (denumerable.eqv α).trans equiv.ulift.symm ⟩,
λ h, by { cases quotient.exact h with f, exact ⟨denumerable.mk' $ f.trans equiv.ulift⟩ }⟩
lemma mk_int : mk ℤ = omega :=
denumerable_iff.mp ⟨by apply_instance⟩
lemma mk_pnat : mk ℕ+ = omega :=
denumerable_iff.mp ⟨by apply_instance⟩
lemma two_le_iff : (2 : cardinal) ≤ mk α ↔ ∃x y : α, x ≠ y :=
begin
split,
{ rintro ⟨f⟩, refine ⟨f $ sum.inl ⟨⟩, f $ sum.inr ⟨⟩, _⟩, intro h, cases f.2 h },
{ rintro ⟨x, y, h⟩, by_contra h',
rw [not_le, ←nat.cast_two, nat_succ, lt_succ, nat.cast_one, le_one_iff_subsingleton] at h',
apply h, exactI subsingleton.elim _ _ }
end
lemma two_le_iff' (x : α) : (2 : cardinal) ≤ mk α ↔ ∃y : α, x ≠ y :=
begin
rw [two_le_iff],
split,
{ rintro ⟨y, z, h⟩, refine classical.by_cases (λ(h' : x = y), _) (λ h', ⟨y, h'⟩),
rw [←h'] at h, exact ⟨z, h⟩ },
{ rintro ⟨y, h⟩, exact ⟨x, y, h⟩ }
end
/-- König's theorem -/
theorem sum_lt_prod {ι} (f g : ι → cardinal) (H : ∀ i, f i < g i) : sum f < prod g :=
lt_of_not_ge $ λ ⟨F⟩, begin
have : inhabited (Π (i : ι), (g i).out),
{ refine ⟨λ i, classical.choice $ ne_zero_iff_nonempty.1 _⟩,
rw mk_out,
exact ne_of_gt (lt_of_le_of_lt (zero_le _) (H i)) }, resetI,
let G := inv_fun F,
have sG : surjective G := inv_fun_surjective F.2,
choose C hc using show ∀ i, ∃ b, ∀ a, G ⟨i, a⟩ i ≠ b,
{ assume i,
simp only [- not_exists, not_exists.symm, classical.not_forall.symm],
refine λ h, not_le_of_lt (H i) _,
rw [← mk_out (f i), ← mk_out (g i)],
exact ⟨embedding.of_surjective _ h⟩ },
exact (let ⟨⟨i, a⟩, h⟩ := sG C in hc i a (congr_fun h _))
end
@[simp] theorem mk_empty : mk empty = 0 :=
fintype_card empty
@[simp] theorem mk_pempty : mk pempty = 0 :=
fintype_card pempty
@[simp] theorem mk_plift_of_false {p : Prop} (h : ¬ p) : mk (plift p) = 0 :=
quotient.sound ⟨equiv.plift.trans $ equiv.equiv_pempty h⟩
theorem mk_unit : mk unit = 1 :=
(fintype_card unit).trans nat.cast_one
@[simp] theorem mk_punit : mk punit = 1 :=
(fintype_card punit).trans nat.cast_one
@[simp] theorem mk_singleton {α : Type u} (x : α) : mk ({x} : set α) = 1 :=
quotient.sound ⟨equiv.set.singleton x⟩
@[simp] theorem mk_plift_of_true {p : Prop} (h : p) : mk (plift p) = 1 :=
quotient.sound ⟨equiv.plift.trans $ equiv.prop_equiv_punit h⟩
@[simp] theorem mk_bool : mk bool = 2 :=
quotient.sound ⟨equiv.bool_equiv_punit_sum_punit⟩
@[simp] theorem mk_Prop : mk Prop = 2 :=
(quotient.sound ⟨equiv.Prop_equiv_bool⟩ : mk Prop = mk bool).trans mk_bool
@[simp] theorem mk_option {α : Type u} : mk (option α) = mk α + 1 :=
quotient.sound ⟨equiv.option_equiv_sum_punit α⟩
theorem mk_list_eq_sum_pow (α : Type u) : mk (list α) = sum (λ n : ℕ, (mk α)^(n:cardinal.{u})) :=
calc mk (list α)
= mk (Σ n, vector α n) : quotient.sound ⟨(equiv.sigma_preimage_equiv list.length).symm⟩
... = mk (Σ n, fin n → α) : quotient.sound ⟨equiv.sigma_congr_right $ λ n,
⟨vector.nth, vector.of_fn, vector.of_fn_nth, λ f, funext $ vector.nth_of_fn f⟩⟩
... = mk (Σ n : ℕ, ulift.{u} (fin n) → α) : quotient.sound ⟨equiv.sigma_congr_right $ λ n,
equiv.arrow_congr equiv.ulift.symm (equiv.refl α)⟩
... = sum (λ n : ℕ, (mk α)^(n:cardinal.{u})) : by simp only [(lift_mk_fin _).symm, lift_mk, power_def, sum_mk]
theorem mk_quot_le {α : Type u} {r : α → α → Prop} : mk (quot r) ≤ mk α :=
mk_le_of_surjective quot.exists_rep
theorem mk_quotient_le {α : Type u} {s : setoid α} : mk (quotient s) ≤ mk α :=
mk_quot_le
theorem mk_subtype_le {α : Type u} (p : α → Prop) : mk (subtype p) ≤ mk α :=
⟨embedding.subtype p⟩
theorem mk_subtype_le_of_subset {α : Type u} {p q : α → Prop} (h : ∀ ⦃x⦄, p x → q x) :
mk (subtype p) ≤ mk (subtype q) :=
⟨embedding.subtype_map (embedding.refl α) h⟩
@[simp] theorem mk_emptyc (α : Type u) : mk (∅ : set α) = 0 :=
quotient.sound ⟨equiv.set.pempty α⟩
lemma mk_emptyc_iff {α : Type u} {s : set α} : mk s = 0 ↔ s = ∅ :=
begin
split,
{ intro h,
have h2 : cardinal.mk s = cardinal.mk pempty, by simp [h],
refine set.eq_empty_iff_forall_not_mem.mpr (λ _ hx, _),
rcases cardinal.eq.mp h2 with ⟨f, _⟩,
cases f ⟨_, hx⟩ },
{ intro, convert mk_emptyc _ }
end
theorem mk_univ {α : Type u} : mk (@univ α) = mk α :=
quotient.sound ⟨equiv.set.univ α⟩
theorem mk_image_le {α β : Type u} {f : α → β} {s : set α} : mk (f '' s) ≤ mk s :=
mk_le_of_surjective surjective_onto_image
theorem mk_image_le_lift {α : Type u} {β : Type v} {f : α → β} {s : set α} :
lift.{v u} (mk (f '' s)) ≤ lift.{u v} (mk s) :=
lift_mk_le.{v u 0}.mpr ⟨embedding.of_surjective _ surjective_onto_image⟩
theorem mk_range_le {α β : Type u} {f : α → β} : mk (range f) ≤ mk α :=
mk_le_of_surjective surjective_onto_range
lemma mk_range_eq (f : α → β) (h : injective f) : mk (range f) = mk α :=
quotient.sound ⟨(equiv.set.range f h).symm⟩
lemma mk_range_eq_of_injective {α : Type u} {β : Type v} {f : α → β} (hf : injective f) :
lift.{v u} (mk (range f)) = lift.{u v} (mk α) :=
begin
have := (@lift_mk_eq.{v u max u v} (range f) α).2 ⟨(equiv.set.range f hf).symm⟩,
simp only [lift_umax.{u v}, lift_umax.{v u}] at this,
exact this
end
lemma mk_range_eq_lift {α : Type u} {β : Type v} {f : α → β} (hf : injective f) :
lift.{v (max u w)} (# (range f)) = lift.{u (max v w)} (# α) :=
lift_mk_eq.mpr ⟨(equiv.set.range f hf).symm⟩
theorem mk_image_eq {α β : Type u} {f : α → β} {s : set α} (hf : injective f) :
mk (f '' s) = mk s :=
quotient.sound ⟨(equiv.set.image f s hf).symm⟩
theorem mk_Union_le_sum_mk {α ι : Type u} {f : ι → set α} : mk (⋃ i, f i) ≤ sum (λ i, mk (f i)) :=
calc mk (⋃ i, f i) ≤ mk (Σ i, f i) : mk_le_of_surjective (set.sigma_to_Union_surjective f)
... = sum (λ i, mk (f i)) : (sum_mk _).symm
theorem mk_Union_eq_sum_mk {α ι : Type u} {f : ι → set α} (h : ∀i j, i ≠ j → disjoint (f i) (f j)) :
mk (⋃ i, f i) = sum (λ i, mk (f i)) :=
calc mk (⋃ i, f i) = mk (Σi, f i) : quot.sound ⟨set.Union_eq_sigma_of_disjoint h⟩
... = sum (λi, mk (f i)) : (sum_mk _).symm
lemma mk_Union_le {α ι : Type u} (f : ι → set α) :
mk (⋃ i, f i) ≤ mk ι * cardinal.sup.{u u} (λ i, mk (f i)) :=
le_trans mk_Union_le_sum_mk (sum_le_sup _)
lemma mk_sUnion_le {α : Type u} (A : set (set α)) :
mk (⋃₀ A) ≤ mk A * cardinal.sup.{u u} (λ s : A, mk s) :=
by { rw [sUnion_eq_Union], apply mk_Union_le }
lemma mk_bUnion_le {ι α : Type u} (A : ι → set α) (s : set ι) :
mk (⋃(x ∈ s), A x) ≤ mk s * cardinal.sup.{u u} (λ x : s, mk (A x.1)) :=
by { rw [bUnion_eq_Union], apply mk_Union_le }
@[simp] lemma finset_card {α : Type u} {s : finset α} : ↑(finset.card s) = mk (↑s : set α) :=
by rw [fintype_card, nat_cast_inj, fintype.card_coe]
lemma finset_card_lt_omega (s : finset α) : mk (↑s : set α) < omega :=
by { rw [lt_omega_iff_fintype], exact ⟨finset.subtype.fintype s⟩ }
theorem mk_union_add_mk_inter {α : Type u} {S T : set α} :
mk (S ∪ T : set α) + mk (S ∩ T : set α) = mk S + mk T :=
quot.sound ⟨equiv.set.union_sum_inter S T⟩
/-- The cardinality of a union is at most the sum of the cardinalities
of the two sets. -/
lemma mk_union_le {α : Type u} (S T : set α) : mk (S ∪ T : set α) ≤ mk S + mk T :=
@mk_union_add_mk_inter α S T ▸ le_add_right (mk (S ∪ T : set α)) (mk (S ∩ T : set α))
theorem mk_union_of_disjoint {α : Type u} {S T : set α} (H : disjoint S T) :
mk (S ∪ T : set α) = mk S + mk T :=
quot.sound ⟨equiv.set.union H⟩
lemma mk_sum_compl {α} (s : set α) : #s + #(sᶜ : set α) = #α :=
quotient.sound ⟨equiv.set.sum_compl s⟩
lemma mk_le_mk_of_subset {α} {s t : set α} (h : s ⊆ t) : mk s ≤ mk t :=
⟨set.embedding_of_subset s t h⟩
lemma mk_subtype_mono {p q : α → Prop} (h : ∀x, p x → q x) : mk {x // p x} ≤ mk {x // q x} :=
⟨embedding_of_subset _ _ h⟩
lemma mk_set_le (s : set α) : mk s ≤ mk α :=
mk_subtype_le s
lemma mk_image_eq_lift {α : Type u} {β : Type v} (f : α → β) (s : set α) (h : injective f) :
lift.{v u} (mk (f '' s)) = lift.{u v} (mk s) :=
lift_mk_eq.{v u 0}.mpr ⟨(equiv.set.image f s h).symm⟩
lemma mk_image_eq_of_inj_on_lift {α : Type u} {β : Type v} (f : α → β) (s : set α)
(h : inj_on f s) : lift.{v u} (mk (f '' s)) = lift.{u v} (mk s) :=
lift_mk_eq.{v u 0}.mpr ⟨(equiv.set.image_of_inj_on f s h).symm⟩
lemma mk_image_eq_of_inj_on {α β : Type u} (f : α → β) (s : set α) (h : inj_on f s) :
mk (f '' s) = mk s :=
quotient.sound ⟨(equiv.set.image_of_inj_on f s h).symm⟩
lemma mk_subtype_of_equiv {α β : Type u} (p : β → Prop) (e : α ≃ β) :
mk {a : α // p (e a)} = mk {b : β // p b} :=
quotient.sound ⟨equiv.subtype_equiv_of_subtype e⟩
lemma mk_sep (s : set α) (t : α → Prop) : mk ({ x ∈ s | t x } : set α) = mk { x : s | t x.1 } :=
quotient.sound ⟨equiv.set.sep s t⟩
lemma mk_preimage_of_injective_lift {α : Type u} {β : Type v} (f : α → β) (s : set β)
(h : injective f) : lift.{u v} (mk (f ⁻¹' s)) ≤ lift.{v u} (mk s) :=
begin
rw lift_mk_le.{u v 0}, use subtype.coind (λ x, f x.1) (λ x, x.2),
apply subtype.coind_injective, exact h.comp subtype.val_injective
end
lemma mk_preimage_of_subset_range_lift {α : Type u} {β : Type v} (f : α → β) (s : set β)
(h : s ⊆ range f) : lift.{v u} (mk s) ≤ lift.{u v} (mk (f ⁻¹' s)) :=
begin
rw lift_mk_le.{v u 0},
refine ⟨⟨_, _⟩⟩,
{ rintro ⟨y, hy⟩, rcases classical.subtype_of_exists (h hy) with ⟨x, rfl⟩, exact ⟨x, hy⟩ },
rintro ⟨y, hy⟩ ⟨y', hy'⟩, dsimp,
rcases classical.subtype_of_exists (h hy) with ⟨x, rfl⟩,
rcases classical.subtype_of_exists (h hy') with ⟨x', rfl⟩,
simp, intro hxx', rw hxx'
end
lemma mk_preimage_of_injective_of_subset_range_lift {β : Type v} (f : α → β) (s : set β)
(h : injective f) (h2 : s ⊆ range f) : lift.{u v} (mk (f ⁻¹' s)) = lift.{v u} (mk s) :=
le_antisymm (mk_preimage_of_injective_lift f s h) (mk_preimage_of_subset_range_lift f s h2)
lemma mk_preimage_of_injective (f : α → β) (s : set β) (h : injective f) :
mk (f ⁻¹' s) ≤ mk s :=
by { convert mk_preimage_of_injective_lift.{u u} f s h using 1; rw [lift_id] }
lemma mk_preimage_of_subset_range (f : α → β) (s : set β)
(h : s ⊆ range f) : mk s ≤ mk (f ⁻¹' s) :=
by { convert mk_preimage_of_subset_range_lift.{u u} f s h using 1; rw [lift_id] }
lemma mk_preimage_of_injective_of_subset_range (f : α → β) (s : set β)
(h : injective f) (h2 : s ⊆ range f) : mk (f ⁻¹' s) = mk s :=
by { convert mk_preimage_of_injective_of_subset_range_lift.{u u} f s h h2 using 1; rw [lift_id] }
lemma mk_subset_ge_of_subset_image_lift {α : Type u} {β : Type v} (f : α → β) {s : set α}
{t : set β} (h : t ⊆ f '' s) :
lift.{v u} (mk t) ≤ lift.{u v} (mk ({ x ∈ s | f x ∈ t } : set α)) :=
by { rw [image_eq_range] at h, convert mk_preimage_of_subset_range_lift _ _ h using 1, rw [mk_sep], refl }
lemma mk_subset_ge_of_subset_image (f : α → β) {s : set α} {t : set β} (h : t ⊆ f '' s) :
mk t ≤ mk ({ x ∈ s | f x ∈ t } : set α) :=
by { rw [image_eq_range] at h, convert mk_preimage_of_subset_range _ _ h using 1, rw [mk_sep], refl }
theorem le_mk_iff_exists_subset {c : cardinal} {α : Type u} {s : set α} :
c ≤ mk s ↔ ∃ p : set α, p ⊆ s ∧ mk p = c :=
begin
rw [le_mk_iff_exists_set, ←subtype.exists_set_subtype],
apply exists_congr, intro t, rw [mk_image_eq], apply subtype.val_injective
end
/-- The function α^{<β}, defined to be sup_{γ < β} α^γ.
We index over {s : set β.out // mk s < β } instead of {γ // γ < β}, because the latter lives in a
higher universe -/
noncomputable def powerlt (α β : cardinal.{u}) : cardinal.{u} :=
sup.{u u} (λ(s : {s : set β.out // mk s < β}), α ^ mk.{u} s)
infix ` ^< `:80 := powerlt
theorem powerlt_aux {c c' : cardinal} (h : c < c') :
∃(s : {s : set c'.out // mk s < c'}), mk s = c :=
begin
cases out_embedding.mp (le_of_lt h) with f,
have : mk ↥(range ⇑f) = c, { rwa [mk_range_eq, mk, quotient.out_eq c], exact f.2 },
exact ⟨⟨range f, by convert h⟩, this⟩
end
lemma le_powerlt {c₁ c₂ c₃ : cardinal} (h : c₂ < c₃) : c₁ ^ c₂ ≤ c₁ ^< c₃ :=
by { rcases powerlt_aux h with ⟨s, rfl⟩, apply le_sup _ s }
lemma powerlt_le {c₁ c₂ c₃ : cardinal} : c₁ ^< c₂ ≤ c₃ ↔ ∀(c₄ < c₂), c₁ ^ c₄ ≤ c₃ :=
begin
rw [powerlt, sup_le],
split,
{ intros h c₄ hc₄, rcases powerlt_aux hc₄ with ⟨s, rfl⟩, exact h s },
intros h s, exact h _ s.2
end
lemma powerlt_le_powerlt_left {a b c : cardinal} (h : b ≤ c) : a ^< b ≤ a ^< c :=
by { rw [powerlt, sup_le], rintro ⟨s, hs⟩, apply le_powerlt, exact lt_of_lt_of_le hs h }
lemma powerlt_succ {c₁ c₂ : cardinal} (h : c₁ ≠ 0) : c₁ ^< c₂.succ = c₁ ^ c₂ :=
begin
apply le_antisymm,
{ rw powerlt_le, intros c₃ h2, apply power_le_power_left h, rwa [←lt_succ] },
{ apply le_powerlt, apply lt_succ_self }
end
lemma powerlt_max {c₁ c₂ c₃ : cardinal} : c₁ ^< max c₂ c₃ = max (c₁ ^< c₂) (c₁ ^< c₃) :=
by { cases le_total c₂ c₃; simp only [max_eq_left, max_eq_right, h, powerlt_le_powerlt_left] }
lemma zero_powerlt {a : cardinal} (h : a ≠ 0) : 0 ^< a = 1 :=
begin
apply le_antisymm,
{ rw [powerlt_le], intros c hc, apply zero_power_le },
convert le_powerlt (pos_iff_ne_zero.2 h), rw [power_zero]
end
lemma powerlt_zero {a : cardinal} : a ^< 0 = 0 :=
by { apply sup_eq_zero, rintro ⟨x, hx⟩, rw [←not_le] at hx, apply hx, apply zero_le }
end cardinal
|
2c031cf0fc6b53676df500e2dfa334307d6daebe | 02005f45e00c7ecf2c8ca5db60251bd1e9c860b5 | /src/control/traversable/derive.lean | b525eb0b64357f7a705c7f15e60f654c0c394a2a | [
"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 | 17,042 | lean | /-
Copyright (c) 2018 Simon Hudon. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Simon Hudon
Automation to construct `traversable` instances
-/
import control.traversable.lemmas
import data.list.basic
namespace tactic.interactive
open tactic list monad functor
meta def with_prefix : option name → name → name
| none n := n
| (some p) n := p ++ n
/-- similar to `nested_traverse` but for `functor` -/
meta def nested_map (f v : expr) : expr → tactic expr
| t :=
do t ← instantiate_mvars t,
mcond (succeeds $ is_def_eq t v)
(pure f)
(if ¬ v.occurs (t.app_fn)
then do
cl ← mk_app ``functor [t.app_fn],
_inst ← mk_instance cl,
f' ← nested_map t.app_arg,
mk_mapp ``functor.map [t.app_fn,_inst,none,none,f']
else fail format!"type {t} is not a functor with respect to variable {v}")
/-- similar to `traverse_field` but for `functor` -/
meta def map_field (n : name) (cl f α β e : expr) : tactic expr :=
do t ← infer_type e >>= whnf,
if t.get_app_fn.const_name = n
then fail "recursive types not supported"
else if α =ₐ e then pure β
else if α.occurs t
then do f' ← nested_map f α t,
pure $ f' e
else
(is_def_eq t.app_fn cl >> mk_app ``comp.mk [e])
<|> pure e
/-- similar to `traverse_constructor` but for `functor` -/
meta def map_constructor (c n : name) (f α β : expr)
(args₀ : list expr)
(args₁ : list (bool × expr)) (rec_call : list expr) : tactic expr :=
do g ← target,
(_, args') ← mmap_accuml (λ (x : list expr) (y : bool × expr),
if y.1 then pure (x.tail,x.head)
else prod.mk rec_call <$> map_field n g.app_fn f α β y.2) rec_call args₁,
constr ← mk_const c,
let r := constr.mk_app (args₀ ++ args'),
return r
/-- derive the `map` definition of a `functor` -/
meta def mk_map (type : name) :=
do ls ← local_context,
[α,β,f,x] ← tactic.intro_lst [`α,`β,`f,`x],
et ← infer_type x,
xs ← tactic.induction x,
xs.mmap' (λ (x : name × list expr × list (name × expr)),
do let (c,args,_) := x,
(args,rec_call) ← args.mpartition $ λ e, (bnot ∘ β.occurs) <$> infer_type e,
args₀ ← args.mmap $ λ a, do { b ← et.occurs <$> infer_type a, pure (b,a) },
map_constructor c type f α β (ls ++ [β]) args₀ rec_call >>= tactic.exact)
meta def mk_mapp_aux' : expr → expr → list expr → tactic expr
| fn (expr.pi n bi d b) (a::as) :=
do infer_type a >>= unify d,
fn ← head_beta (fn a),
t ← whnf (b.instantiate_var a),
mk_mapp_aux' fn t as
| fn _ _ := pure fn
meta def mk_mapp' (fn : expr) (args : list expr) : tactic expr :=
do t ← infer_type fn >>= whnf,
mk_mapp_aux' fn t args
/-- derive the equations for a specific `map` definition -/
meta def derive_map_equations (pre : option name) (n : name) (vs : list expr) (tgt : expr) : tactic unit :=
do e ← get_env,
((e.constructors_of n).enum_from 1).mmap' $ λ ⟨i,c⟩,
do { mk_meta_var tgt >>= set_goals ∘ pure,
vs ← intro_lst $ vs.map expr.local_pp_name,
[α,β,f] ← tactic.intro_lst [`α,`β,`f] >>= mmap instantiate_mvars,
c' ← mk_mapp c $ vs.map some ++ [α],
tgt' ← infer_type c' >>= pis vs,
mk_meta_var tgt' >>= set_goals ∘ pure,
vs ← tactic.intro_lst $ vs.map expr.local_pp_name,
vs' ← tactic.intros,
c' ← mk_mapp c $ vs.map some ++ [α],
arg ← mk_mapp' c' vs',
n_map ← mk_const (with_prefix pre n <.> "map"),
let call_map := λ x, mk_mapp' n_map (vs ++ [α,β,f,x]),
lhs ← call_map arg,
args ← vs'.mmap $ λ a,
do { t ← infer_type a,
pure ((expr.const_name (expr.get_app_fn t) = n : bool),a) },
let rec_call := args.filter_map $
λ ⟨b, e⟩, guard b >> pure e,
rec_call ← rec_call.mmap call_map,
rhs ← map_constructor c n f α β (vs ++ [β]) args rec_call,
monad.join $ unify <$> infer_type lhs <*> infer_type rhs,
eqn ← mk_app ``eq [lhs,rhs],
let ws := eqn.list_local_consts,
eqn ← pis ws.reverse eqn,
eqn ← instantiate_mvars eqn,
(_,pr) ← solve_aux eqn (tactic.intros >> refine ``(rfl)),
let eqn_n := (with_prefix pre n <.> "map" <.> "equations" <.> "_eqn").append_after i,
pr ← instantiate_mvars pr,
add_decl $ declaration.thm eqn_n eqn.collect_univ_params eqn (pure pr),
return () },
set_goals [],
return ()
meta def derive_functor (pre : option name) : tactic unit :=
do vs ← local_context,
`(functor %%f) ← target,
env ← get_env,
let n := f.get_app_fn.const_name,
d ← get_decl n,
refine ``( { functor . map := _ , .. } ),
tgt ← target,
extract_def (with_prefix pre n <.> "map") d.is_trusted $ mk_map n,
when (d.is_trusted) $ do
tgt ← pis vs tgt,
derive_map_equations pre n vs tgt
/-- `seq_apply_constructor f [x,y,z]` synthesizes `f <*> x <*> y <*> z` -/
private meta def seq_apply_constructor : expr → list (expr ⊕ expr) → tactic (list (tactic expr) × expr)
| e (sum.inr x :: xs) := prod.map (cons intro1) id <$> (to_expr ``(%%e <*> %%x) >>= flip seq_apply_constructor xs)
| e (sum.inl x :: xs) := prod.map (cons $ pure x) id <$> seq_apply_constructor e xs
| e [] := return ([],e)
/-- ``nested_traverse f α (list (array n (list α)))`` synthesizes the expression
`traverse (traverse (traverse f))`. `nested_traverse` assumes that `α` appears in
`(list (array n (list α)))` -/
meta def nested_traverse (f v : expr) : expr → tactic expr
| t :=
do t ← instantiate_mvars t,
mcond (succeeds $ is_def_eq t v)
(pure f)
(if ¬ v.occurs (t.app_fn)
then do
cl ← mk_app ``traversable [t.app_fn],
_inst ← mk_instance cl,
f' ← nested_traverse t.app_arg,
mk_mapp ``traversable.traverse [t.app_fn,_inst,none,none,none,none,f']
else fail format!"type {t} is not traversable with respect to variable {v}")
/--
For a sum type `inductive foo (α : Type) | foo1 : list α → ℕ → foo | ...`
``traverse_field `foo appl_inst f `α `(x : list α)`` synthesizes
`traverse f x` as part of traversing `foo1`. -/
meta def traverse_field (n : name) (appl_inst cl f v e : expr) : tactic (expr ⊕ expr) :=
do t ← infer_type e >>= whnf,
if t.get_app_fn.const_name = n
then fail "recursive types not supported"
else if v.occurs t
then do f' ← nested_traverse f v t,
pure $ sum.inr $ f' e
else
(is_def_eq t.app_fn cl >> sum.inr <$> mk_app ``comp.mk [e])
<|> pure (sum.inl e)
/--
For a sum type `inductive foo (α : Type) | foo1 : list α → ℕ → foo | ...`
``traverse_constructor `foo1 `foo appl_inst f `α `β [`(x : list α), `(y : ℕ)]``
synthesizes `foo1 <$> traverse f x <*> pure y.` -/
meta def traverse_constructor (c n : name) (appl_inst f α β : expr)
(args₀ : list expr)
(args₁ : list (bool × expr)) (rec_call : list expr) : tactic expr :=
do g ← target,
args' ← mmap (traverse_field n appl_inst g.app_fn f α) args₀,
(_, args') ← mmap_accuml (λ (x : list expr) (y : bool × _),
if y.1 then pure (x.tail, sum.inr x.head)
else prod.mk x <$> traverse_field n appl_inst g.app_fn f α y.2) rec_call args₁,
constr ← mk_const c,
v ← mk_mvar,
constr' ← to_expr ``(@pure _ (%%appl_inst).to_has_pure _ %%v),
(vars_intro,r) ← seq_apply_constructor constr' (args₀.map sum.inl ++ args'),
gs ← get_goals,
set_goals [v],
vs ← vars_intro.mmap id,
tactic.exact (constr.mk_app vs),
done,
set_goals gs,
return r
/-- derive the `traverse` definition of a `traversable` instance -/
meta def mk_traverse (type : name) := do
do ls ← local_context,
[m,appl_inst,α,β,f,x] ← tactic.intro_lst [`m,`appl_inst,`α,`β,`f,`x],
et ← infer_type x,
reset_instance_cache,
xs ← tactic.induction x,
xs.mmap'
(λ (x : name × list expr × list (name × expr)),
do let (c,args,_) := x,
(args,rec_call) ← args.mpartition $ λ e, (bnot ∘ β.occurs) <$> infer_type e,
args₀ ← args.mmap $ λ a, do { b ← et.occurs <$> infer_type a, pure (b,a) },
traverse_constructor c type appl_inst f α β (ls ++ [β]) args₀ rec_call >>= tactic.exact)
open applicative
/-- derive the equations for a specific `traverse` definition -/
meta def derive_traverse_equations (pre : option name) (n : name) (vs : list expr) (tgt : expr) : tactic unit :=
do e ← get_env,
((e.constructors_of n).enum_from 1).mmap' $ λ ⟨i,c⟩,
do { mk_meta_var tgt >>= set_goals ∘ pure,
vs ← intro_lst $ vs.map expr.local_pp_name,
[m,appl_inst,α,β,f] ← tactic.intro_lst [`m,`appl_inst,`α,`β,`f] >>= mmap instantiate_mvars,
c' ← mk_mapp c $ vs.map some ++ [α],
tgt' ← infer_type c' >>= pis vs,
mk_meta_var tgt' >>= set_goals ∘ pure,
vs ← tactic.intro_lst $ vs.map expr.local_pp_name,
c' ← mk_mapp c $ vs.map some ++ [α],
vs' ← tactic.intros,
arg ← mk_mapp' c' vs',
n_map ← mk_const (with_prefix pre n <.> "traverse"),
let call_traverse := λ x, mk_mapp' n_map (vs ++ [m,appl_inst,α,β,f,x]),
lhs ← call_traverse arg,
args ← vs'.mmap $ λ a,
do { t ← infer_type a,
pure ((expr.const_name (expr.get_app_fn t) = n : bool),a) },
let rec_call := args.filter_map $
λ ⟨b, e⟩, guard b >> pure e,
rec_call ← rec_call.mmap call_traverse,
rhs ← traverse_constructor c n appl_inst f α β (vs ++ [β]) args rec_call,
monad.join $ unify <$> infer_type lhs <*> infer_type rhs,
eqn ← mk_app ``eq [lhs,rhs],
let ws := eqn.list_local_consts,
eqn ← pis ws.reverse eqn,
eqn ← instantiate_mvars eqn,
(_,pr) ← solve_aux eqn (tactic.intros >> refine ``(rfl)),
let eqn_n := (with_prefix pre n <.> "traverse" <.> "equations" <.> "_eqn").append_after i,
pr ← instantiate_mvars pr,
add_decl $ declaration.thm eqn_n eqn.collect_univ_params eqn (pure pr),
return () },
set_goals [],
return ()
meta def derive_traverse (pre : option name) : tactic unit :=
do vs ← local_context,
`(traversable %%f) ← target,
env ← get_env,
let n := f.get_app_fn.const_name,
d ← get_decl n,
constructor,
tgt ← target,
extract_def (with_prefix pre n <.> "traverse") d.is_trusted $ mk_traverse n,
when (d.is_trusted) $ do
tgt ← pis vs tgt,
derive_traverse_equations pre n vs tgt
meta def mk_one_instance
(n : name)
(cls : name)
(tac : tactic unit)
(namesp : option name)
(mk_inst : name → expr → tactic expr := λ n arg, mk_app n [arg]) :
tactic unit :=
do decl ← get_decl n,
cls_decl ← get_decl cls,
env ← get_env,
guard (env.is_inductive n) <|> fail format!"failed to derive '{cls}', '{n}' is not an inductive type",
let ls := decl.univ_params.map $ λ n, level.param n,
-- incrementally build up target expression `Π (hp : p) [cls hp] ..., cls (n.{ls} hp ...)`
-- where `p ...` are the inductive parameter types of `n`
let tgt : expr := expr.const n ls,
⟨params, _⟩ ← open_pis (decl.type.instantiate_univ_params (decl.univ_params.zip ls)),
let params := params.init,
let tgt := tgt.mk_app params,
tgt ← mk_inst cls tgt,
tgt ← params.enum.mfoldr (λ ⟨i, param⟩ tgt,
do -- add typeclass hypothesis for each inductive parameter
tgt ← do {
guard $ i < env.inductive_num_params n,
param_cls ← mk_app cls [param],
pure $ expr.pi `a binder_info.inst_implicit param_cls tgt
} <|> pure tgt,
pure $ tgt.bind_pi param
) tgt,
() <$ mk_instance tgt <|> do
(_, val) ← tactic.solve_aux tgt (do
tactic.intros >> tac),
val ← instantiate_mvars val,
let trusted := decl.is_trusted ∧ cls_decl.is_trusted,
let inst_n := with_prefix namesp n ++ cls,
add_decl (declaration.defn inst_n
decl.univ_params
tgt val reducibility_hints.abbrev trusted),
set_basic_attribute `instance inst_n namesp.is_none
open interactive
meta def get_equations_of (n : name) : tactic (list pexpr) :=
do e ← get_env,
let pre := n <.> "equations",
let x := e.fold [] $ λ d xs, if pre.is_prefix_of d.to_name then d.to_name :: xs else xs,
x.mmap resolve_name
meta def derive_lawful_functor (pre : option name) : tactic unit :=
do `(@is_lawful_functor %%f %%d) ← target,
refine ``( { .. } ),
let n := f.get_app_fn.const_name,
let rules := λ r, [simp_arg_type.expr r, simp_arg_type.all_hyps],
let goal := loc.ns [none],
solve1 (do
vs ← tactic.intros,
try $ dunfold [``functor.map] (loc.ns [none]),
dunfold [with_prefix pre n <.> "map",``id] (loc.ns [none]),
() <$ tactic.induction vs.ilast;
simp none none ff (rules ``(functor.map_id)) [] goal),
focus1 (do
vs ← tactic.intros,
try $ dunfold [``functor.map] (loc.ns [none]),
dunfold [with_prefix pre n <.> "map",``id] (loc.ns [none]),
() <$ tactic.induction vs.ilast;
simp none none ff (rules ``(functor.map_comp_map)) [] goal),
return ()
meta def simp_functor (rs : list simp_arg_type := []) : tactic unit :=
simp none none ff rs [`functor_norm] (loc.ns [none])
meta def traversable_law_starter (rs : list simp_arg_type) :=
do vs ← tactic.intros,
resetI,
dunfold [``traversable.traverse,``functor.map] (loc.ns [none]),
() <$ tactic.induction vs.ilast;
simp_functor rs
meta def derive_lawful_traversable (pre : option name) : tactic unit :=
do `(@is_lawful_traversable %%f %%d) ← target,
let n := f.get_app_fn.const_name,
eqns ← get_equations_of (with_prefix pre n <.> "traverse"),
eqns' ← get_equations_of (with_prefix pre n <.> "map"),
let def_eqns := eqns.map simp_arg_type.expr ++
eqns'.map simp_arg_type.expr ++
[simp_arg_type.all_hyps],
let comp_def := [ simp_arg_type.expr ``(function.comp) ],
let tr_map := list.map simp_arg_type.expr [``(traversable.traverse_eq_map_id')],
let natur := λ (η : expr), [simp_arg_type.expr ``(traversable.naturality_pf %%η)],
let goal := loc.ns [none],
constructor;
[ traversable_law_starter def_eqns; refl,
traversable_law_starter def_eqns; (refl <|> simp_functor (def_eqns ++ comp_def)),
traversable_law_starter def_eqns; (refl <|> simp none none tt tr_map [] goal ),
traversable_law_starter def_eqns; (refl <|> do
η ← get_local `η <|> do {
t ← mk_const ``is_lawful_traversable.naturality >>= infer_type >>= pp,
fail format!"expecting an `applicative_transformation` called `η` in\nnaturality : {t}"},
simp none none tt (natur η) [] goal) ];
refl,
return ()
open function
meta def guard_class (cls : name) (hdl : derive_handler) : derive_handler :=
λ p n,
if p.is_constant_of cls then
hdl p n
else
pure ff
meta def higher_order_derive_handler
(cls : name)
(tac : tactic unit)
(deps : list derive_handler := [])
(namesp : option name)
(mk_inst : name → expr → tactic expr := λ n arg, mk_app n [arg]) :
derive_handler :=
λ p n,
do mmap' (λ f : derive_handler, f p n) deps,
mk_one_instance n cls tac namesp mk_inst,
pure tt
meta def functor_derive_handler' (nspace : option name := none) : derive_handler :=
higher_order_derive_handler ``functor (derive_functor nspace) [] nspace
@[derive_handler]
meta def functor_derive_handler : derive_handler :=
guard_class ``functor functor_derive_handler'
meta def traversable_derive_handler' (nspace : option name := none) : derive_handler :=
higher_order_derive_handler ``traversable (derive_traverse nspace)
[functor_derive_handler' nspace] nspace
@[derive_handler]
meta def traversable_derive_handler : derive_handler :=
guard_class ``traversable traversable_derive_handler'
meta def lawful_functor_derive_handler' (nspace : option name := none) : derive_handler :=
higher_order_derive_handler
``is_lawful_functor (derive_lawful_functor nspace)
[traversable_derive_handler' nspace]
nspace
(λ n arg, mk_mapp n [arg,none])
@[derive_handler]
meta def lawful_functor_derive_handler : derive_handler :=
guard_class ``is_lawful_functor lawful_functor_derive_handler'
meta def lawful_traversable_derive_handler' (nspace : option name := none) : derive_handler :=
higher_order_derive_handler
``is_lawful_traversable (derive_lawful_traversable nspace)
[traversable_derive_handler' nspace,
lawful_functor_derive_handler' nspace]
nspace
(λ n arg, mk_mapp n [arg,none])
@[derive_handler]
meta def lawful_traversable_derive_handler : derive_handler :=
guard_class ``is_lawful_traversable lawful_traversable_derive_handler'
end tactic.interactive
|
af034536f4ebdbe7796ccc31379bc3501d4c5ec7 | 0c1546a496eccfb56620165cad015f88d56190c5 | /library/init/algebra/ordered_ring.lean | 59680a5634b21d9c7426aa2c6e71ebbc68df3945 | [
"Apache-2.0"
] | permissive | Solertis/lean | 491e0939957486f664498fbfb02546e042699958 | 84188c5aa1673fdf37a082b2de8562dddf53df3f | refs/heads/master | 1,610,174,257,606 | 1,486,263,620,000 | 1,486,263,620,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 16,079 | lean | /-
Copyright (c) 2016 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad, Leonardo de Moura
-/
prelude
import init.algebra.ordered_group init.algebra.ring
/- Make sure instances defined in this file have lower priority than the ones
defined for concrete structures -/
set_option default_priority 100
universe variable u
class ordered_semiring (α : Type u)
extends semiring α, ordered_cancel_comm_monoid α :=
(mul_le_mul_of_nonneg_left: ∀ a b c : α, a ≤ b → 0 ≤ c → c * a ≤ c * b)
(mul_le_mul_of_nonneg_right: ∀ a b c : α, a ≤ b → 0 ≤ c → a * c ≤ b * c)
(mul_lt_mul_of_pos_left: ∀ a b c : α, a < b → 0 < c → c * a < c * b)
(mul_lt_mul_of_pos_right: ∀ a b c : α, a < b → 0 < c → a * c < b * c)
variable {α : Type u}
section ordered_semiring
variable [ordered_semiring α]
lemma mul_le_mul_of_nonneg_left {a b c : α} (h₁ : a ≤ b) (h₂ : 0 ≤ c) : c * a ≤ c * b :=
ordered_semiring.mul_le_mul_of_nonneg_left a b c h₁ h₂
lemma mul_le_mul_of_nonneg_right {a b c : α} (h₁ : a ≤ b) (h₂ : 0 ≤ c) : a * c ≤ b * c :=
ordered_semiring.mul_le_mul_of_nonneg_right a b c h₁ h₂
lemma mul_lt_mul_of_pos_left {a b c : α} (h₁ : a < b) (h₂ : 0 < c) : c * a < c * b :=
ordered_semiring.mul_lt_mul_of_pos_left a b c h₁ h₂
lemma mul_lt_mul_of_pos_right {a b c : α} (h₁ : a < b) (h₂ : 0 < c) : a * c < b * c :=
ordered_semiring.mul_lt_mul_of_pos_right a b c h₁ h₂
-- TODO: there are four variations, depending on which variables we assume to be nonneg
lemma mul_le_mul {a b c d : α} (hac : a ≤ c) (hbd : b ≤ d) (nn_b : 0 ≤ b) (nn_c : 0 ≤ c) : a * b ≤ c * d :=
calc
a * b ≤ c * b : mul_le_mul_of_nonneg_right hac nn_b
... ≤ c * d : mul_le_mul_of_nonneg_left hbd nn_c
lemma mul_nonneg {a b : α} (ha : a ≥ 0) (hb : b ≥ 0) : a * b ≥ 0 :=
have h : 0 * b ≤ a * b, from mul_le_mul_of_nonneg_right ha hb,
by rwa [zero_mul] at h
lemma mul_nonpos_of_nonneg_of_nonpos {a b : α} (ha : a ≥ 0) (hb : b ≤ 0) : a * b ≤ 0 :=
have h : a * b ≤ a * 0, from mul_le_mul_of_nonneg_left hb ha,
by rwa mul_zero at h
lemma mul_nonpos_of_nonpos_of_nonneg {a b : α} (ha : a ≤ 0) (hb : b ≥ 0) : a * b ≤ 0 :=
have h : a * b ≤ 0 * b, from mul_le_mul_of_nonneg_right ha hb,
by rwa zero_mul at h
lemma mul_lt_mul {a b c d : α} (hac : a < c) (hbd : b ≤ d) (pos_b : 0 < b) (nn_c : 0 ≤ c) : a * b < c * d :=
calc
a * b < c * b : mul_lt_mul_of_pos_right hac pos_b
... ≤ c * d : mul_le_mul_of_nonneg_left hbd nn_c
lemma mul_lt_mul' {a b c d : α} (h1 : a < c) (h2 : b < d) (h3 : b ≥ 0) (h4 : c > 0) :
a * b < c * d :=
calc
a * b ≤ c * b : mul_le_mul_of_nonneg_right (le_of_lt h1) h3
... < c * d : mul_lt_mul_of_pos_left h2 h4
lemma mul_pos {a b : α} (ha : a > 0) (hb : b > 0) : a * b > 0 :=
have h : 0 * b < a * b, from mul_lt_mul_of_pos_right ha hb,
by rwa zero_mul at h
lemma mul_neg_of_pos_of_neg {a b : α} (ha : a > 0) (hb : b < 0) : a * b < 0 :=
have h : a * b < a * 0, from mul_lt_mul_of_pos_left hb ha,
by rwa mul_zero at h
lemma mul_neg_of_neg_of_pos {a b : α} (ha : a < 0) (hb : b > 0) : a * b < 0 :=
have h : a * b < 0 * b, from mul_lt_mul_of_pos_right ha hb,
by rwa zero_mul at h
lemma mul_self_lt_mul_self {a b : α} (h1 : 0 ≤ a) (h2 : a < b) : a * a < b * b :=
mul_lt_mul' h2 h2 h1 (lt_of_le_of_lt h1 h2)
end ordered_semiring
class linear_ordered_semiring (α : Type u) extends ordered_semiring α, linear_strong_order_pair α :=
(zero_lt_one : zero < one)
section linear_ordered_semiring
variable [linear_ordered_semiring α]
lemma zero_lt_one : 0 < (1:α) :=
linear_ordered_semiring.zero_lt_one α
lemma lt_of_mul_lt_mul_left {a b c : α} (h : c * a < c * b) (hc : c ≥ 0) : a < b :=
lt_of_not_ge
(assume h1 : b ≤ a,
have h2 : c * b ≤ c * a, from mul_le_mul_of_nonneg_left h1 hc,
not_lt_of_ge h2 h)
lemma lt_of_mul_lt_mul_right {a b c : α} (h : a * c < b * c) (hc : c ≥ 0) : a < b :=
lt_of_not_ge
(assume h1 : b ≤ a,
have h2 : b * c ≤ a * c, from mul_le_mul_of_nonneg_right h1 hc,
not_lt_of_ge h2 h)
lemma le_of_mul_le_mul_left {a b c : α} (h : c * a ≤ c * b) (hc : c > 0) : a ≤ b :=
le_of_not_gt
(assume h1 : b < a,
have h2 : c * b < c * a, from mul_lt_mul_of_pos_left h1 hc,
not_le_of_gt h2 h)
lemma le_of_mul_le_mul_right {a b c : α} (h : a * c ≤ b * c) (hc : c > 0) : a ≤ b :=
le_of_not_gt
(assume h1 : b < a,
have h2 : b * c < a * c, from mul_lt_mul_of_pos_right h1 hc,
not_le_of_gt h2 h)
lemma pos_of_mul_pos_left {a b : α} (h : 0 < a * b) (h1 : 0 ≤ a) : 0 < b :=
lt_of_not_ge
(assume h2 : b ≤ 0,
have h3 : a * b ≤ 0, from mul_nonpos_of_nonneg_of_nonpos h1 h2,
not_lt_of_ge h3 h)
lemma pos_of_mul_pos_right {a b : α} (h : 0 < a * b) (h1 : 0 ≤ b) : 0 < a :=
lt_of_not_ge
(assume h2 : a ≤ 0,
have h3 : a * b ≤ 0, from mul_nonpos_of_nonpos_of_nonneg h2 h1,
not_lt_of_ge h3 h)
lemma nonneg_of_mul_nonneg_left {a b : α} (h : 0 ≤ a * b) (h1 : 0 < a) : 0 ≤ b :=
le_of_not_gt
(assume h2 : b < 0,
not_le_of_gt (mul_neg_of_pos_of_neg h1 h2) h)
lemma nonneg_of_mul_nonneg_right {a b : α} (h : 0 ≤ a * b) (h1 : 0 < b) : 0 ≤ a :=
le_of_not_gt
(assume h2 : a < 0,
not_le_of_gt (mul_neg_of_neg_of_pos h2 h1) h)
lemma neg_of_mul_neg_left {a b : α} (h : a * b < 0) (h1 : 0 ≤ a) : b < 0 :=
lt_of_not_ge
(assume h2 : b ≥ 0,
not_lt_of_ge (mul_nonneg h1 h2) h)
lemma neg_of_mul_neg_right {a b : α} (h : a * b < 0) (h1 : 0 ≤ b) : a < 0 :=
lt_of_not_ge
(assume h2 : a ≥ 0,
not_lt_of_ge (mul_nonneg h2 h1) h)
lemma nonpos_of_mul_nonpos_left {a b : α} (h : a * b ≤ 0) (h1 : 0 < a) : b ≤ 0 :=
le_of_not_gt
(assume h2 : b > 0,
not_le_of_gt (mul_pos h1 h2) h)
lemma nonpos_of_mul_nonpos_right {a b : α} (h : a * b ≤ 0) (h1 : 0 < b) : a ≤ 0 :=
le_of_not_gt
(assume h2 : a > 0,
not_le_of_gt (mul_pos h2 h1) h)
end linear_ordered_semiring
class decidable_linear_ordered_semiring (α : Type u) extends linear_ordered_semiring α, decidable_linear_order α
class ordered_ring (α : Type u) extends ring α, ordered_comm_group α, zero_ne_one_class α :=
(mul_nonneg : ∀ a b : α, 0 ≤ a → 0 ≤ b → 0 ≤ a * b)
(mul_pos : ∀ a b : α, 0 < a → 0 < b → 0 < a * b)
lemma ordered_ring.mul_le_mul_of_nonneg_left [s : ordered_ring α] {a b c : α}
(h₁ : a ≤ b) (h₂ : 0 ≤ c) : c * a ≤ c * b :=
have 0 ≤ b - a, from sub_nonneg_of_le h₁,
have 0 ≤ c * (b - a), from ordered_ring.mul_nonneg c (b - a) h₂ this,
begin
rw mul_sub_left_distrib at this,
apply le_of_sub_nonneg this
end
lemma ordered_ring.mul_le_mul_of_nonneg_right [s : ordered_ring α] {a b c : α}
(h₁ : a ≤ b) (h₂ : 0 ≤ c) : a * c ≤ b * c :=
have 0 ≤ b - a, from sub_nonneg_of_le h₁,
have 0 ≤ (b - a) * c, from ordered_ring.mul_nonneg (b - a) c this h₂,
begin
rw mul_sub_right_distrib at this,
apply le_of_sub_nonneg this
end
lemma ordered_ring.mul_lt_mul_of_pos_left [s : ordered_ring α] {a b c : α}
(h₁ : a < b) (h₂ : 0 < c) : c * a < c * b :=
have 0 < b - a, from sub_pos_of_lt h₁,
have 0 < c * (b - a), from ordered_ring.mul_pos c (b - a) h₂ this,
begin
rw mul_sub_left_distrib at this,
apply lt_of_sub_pos this
end
lemma ordered_ring.mul_lt_mul_of_pos_right [s : ordered_ring α] {a b c : α}
(h₁ : a < b) (h₂ : 0 < c) : a * c < b * c :=
have 0 < b - a, from sub_pos_of_lt h₁,
have 0 < (b - a) * c, from ordered_ring.mul_pos (b - a) c this h₂,
begin
rw mul_sub_right_distrib at this,
apply lt_of_sub_pos this
end
instance ordered_ring.to_ordered_semiring [s : ordered_ring α] : ordered_semiring α :=
{ s with
mul_zero := mul_zero,
zero_mul := zero_mul,
add_left_cancel := @add_left_cancel α _,
add_right_cancel := @add_right_cancel α _,
le_of_add_le_add_left := @le_of_add_le_add_left α _,
mul_le_mul_of_nonneg_left := @ordered_ring.mul_le_mul_of_nonneg_left α _,
mul_le_mul_of_nonneg_right := @ordered_ring.mul_le_mul_of_nonneg_right α _,
mul_lt_mul_of_pos_left := @ordered_ring.mul_lt_mul_of_pos_left α _,
mul_lt_mul_of_pos_right := @ordered_ring.mul_lt_mul_of_pos_right α _,
lt_of_add_lt_add_left := @lt_of_add_lt_add_left α _}
section ordered_ring
variable [ordered_ring α]
lemma mul_le_mul_of_nonpos_left {a b c : α} (h : b ≤ a) (hc : c ≤ 0) : c * a ≤ c * b :=
have -c ≥ 0, from neg_nonneg_of_nonpos hc,
have -c * b ≤ -c * a, from mul_le_mul_of_nonneg_left h this,
have -(c * b) ≤ -(c * a), by rwa [-neg_mul_eq_neg_mul, -neg_mul_eq_neg_mul] at this,
le_of_neg_le_neg this
lemma mul_le_mul_of_nonpos_right {a b c : α} (h : b ≤ a) (hc : c ≤ 0) : a * c ≤ b * c :=
have -c ≥ 0, from neg_nonneg_of_nonpos hc,
have b * -c ≤ a * -c, from mul_le_mul_of_nonneg_right h this,
have -(b * c) ≤ -(a * c), by rwa [-neg_mul_eq_mul_neg, -neg_mul_eq_mul_neg] at this,
le_of_neg_le_neg this
lemma mul_nonneg_of_nonpos_of_nonpos {a b : α} (ha : a ≤ 0) (hb : b ≤ 0) : 0 ≤ a * b :=
have 0 * b ≤ a * b, from mul_le_mul_of_nonpos_right ha hb,
by rwa zero_mul at this
lemma mul_lt_mul_of_neg_left {a b c : α} (h : b < a) (hc : c < 0) : c * a < c * b :=
have -c > 0, from neg_pos_of_neg hc,
have -c * b < -c * a, from mul_lt_mul_of_pos_left h this,
have -(c * b) < -(c * a), by rwa [-neg_mul_eq_neg_mul, -neg_mul_eq_neg_mul] at this,
lt_of_neg_lt_neg this
lemma mul_lt_mul_of_neg_right {a b c : α} (h : b < a) (hc : c < 0) : a * c < b * c :=
have -c > 0, from neg_pos_of_neg hc,
have b * -c < a * -c, from mul_lt_mul_of_pos_right h this,
have -(b * c) < -(a * c), by rwa [-neg_mul_eq_mul_neg, -neg_mul_eq_mul_neg] at this,
lt_of_neg_lt_neg this
lemma mul_pos_of_neg_of_neg {a b : α} (ha : a < 0) (hb : b < 0) : 0 < a * b :=
have 0 * b < a * b, from mul_lt_mul_of_neg_right ha hb,
by rwa zero_mul at this
end ordered_ring
class linear_ordered_ring (α : Type u) extends ordered_ring α, linear_strong_order_pair α :=
(zero_lt_one : lt zero one)
instance linear_ordered_ring.to_linear_ordered_semiring [s : linear_ordered_ring α] : linear_ordered_semiring α :=
{ s with
mul_zero := mul_zero,
zero_mul := zero_mul,
add_left_cancel := @add_left_cancel α _,
add_right_cancel := @add_right_cancel α _,
le_of_add_le_add_left := @le_of_add_le_add_left α _,
mul_le_mul_of_nonneg_left := @mul_le_mul_of_nonneg_left α _,
mul_le_mul_of_nonneg_right := @mul_le_mul_of_nonneg_right α _,
mul_lt_mul_of_pos_left := @mul_lt_mul_of_pos_left α _,
mul_lt_mul_of_pos_right := @mul_lt_mul_of_pos_right α _,
le_total := linear_ordered_ring.le_total,
lt_of_add_lt_add_left := @lt_of_add_lt_add_left α _ }
section linear_ordered_ring
variable [linear_ordered_ring α]
lemma mul_self_nonneg (a : α) : a * a ≥ 0 :=
or.elim (le_total 0 a)
(assume h : a ≥ 0, mul_nonneg h h)
(assume h : a ≤ 0, mul_nonneg_of_nonpos_of_nonpos h h)
lemma zero_le_one : 0 ≤ (1:α) :=
have 1 * 1 ≥ (0:α), from mul_self_nonneg (1:α),
by rwa one_mul at this
lemma pos_and_pos_or_neg_and_neg_of_mul_pos {a b : α} (hab : a * b > 0) :
(a > 0 ∧ b > 0) ∨ (a < 0 ∧ b < 0) :=
match lt_trichotomy 0 a with
| or.inl hlt₁ :=
match lt_trichotomy 0 b with
| or.inl hlt₂ := or.inl ⟨hlt₁, hlt₂⟩
| or.inr (or.inl heq₂) := begin rw [-heq₂, mul_zero] at hab, exact absurd hab (lt_irrefl _) end
| or.inr (or.inr hgt₂) := absurd hab (lt_asymm (mul_neg_of_pos_of_neg hlt₁ hgt₂))
end
| or.inr (or.inl heq₁) := begin rw [-heq₁, zero_mul] at hab, exact absurd hab (lt_irrefl _) end
| or.inr (or.inr hgt₁) :=
match lt_trichotomy 0 b with
| or.inl hlt₂ := absurd hab (lt_asymm (mul_neg_of_neg_of_pos hgt₁ hlt₂))
| or.inr (or.inl heq₂) := begin rw [-heq₂, mul_zero] at hab, exact absurd hab (lt_irrefl _) end
| or.inr (or.inr hgt₂) := or.inr ⟨hgt₁, hgt₂⟩
end
end
lemma gt_of_mul_lt_mul_neg_left {a b c : α} (h : c * a < c * b) (hc : c ≤ 0) : a > b :=
have nhc : -c ≥ 0, from neg_nonneg_of_nonpos hc,
have h2 : -(c * b) < -(c * a), from neg_lt_neg h,
have h3 : (-c) * b < (-c) * a, from calc
(-c) * b = - (c * b) : by rewrite neg_mul_eq_neg_mul
... < -(c * a) : h2
... = (-c) * a : by rewrite neg_mul_eq_neg_mul,
lt_of_mul_lt_mul_left h3 nhc
lemma zero_gt_neg_one : -1 < (0:α) :=
begin
note this := neg_lt_neg (@zero_lt_one α _),
rwa neg_zero at this
end
lemma le_of_mul_le_of_ge_one {a b c : α} (h : a * c ≤ b) (hb : b ≥ 0) (hc : c ≥ 1) : a ≤ b :=
have h' : a * c ≤ b * c, from calc
a * c ≤ b : h
... = b * 1 : by rewrite mul_one
... ≤ b * c : mul_le_mul_of_nonneg_left hc hb,
le_of_mul_le_mul_right h' (lt_of_lt_of_le zero_lt_one hc)
lemma nonneg_le_nonneg_of_squares_le {a b : α} (ha : a ≥ 0) (hb : b ≥ 0) (h : a * a ≤ b * b) : a ≤ b :=
begin
apply le_of_not_gt,
intro hab,
note hposa := lt_of_le_of_lt hb hab,
note h' := calc
b * b ≤ a * b : mul_le_mul_of_nonneg_right (le_of_lt hab) hb
... < a * a : mul_lt_mul_of_pos_left hab hposa,
apply (not_le_of_gt h') h
end
end linear_ordered_ring
class linear_ordered_comm_ring (α : Type u) extends linear_ordered_ring α, comm_monoid α
lemma linear_ordered_comm_ring.eq_zero_or_eq_zero_of_mul_eq_zero [s : linear_ordered_comm_ring α]
{a b : α} (h : a * b = 0) : a = 0 ∨ b = 0 :=
match lt_trichotomy 0 a with
| or.inl hlt₁ :=
match lt_trichotomy 0 b with
| or.inl hlt₂ :=
have 0 < a * b, from mul_pos hlt₁ hlt₂,
begin rw h at this, exact absurd this (lt_irrefl _) end
| or.inr (or.inl heq₂) := or.inr heq₂^.symm
| or.inr (or.inr hgt₂) :=
have 0 > a * b, from mul_neg_of_pos_of_neg hlt₁ hgt₂,
begin rw h at this, exact absurd this (lt_irrefl _) end
end
| or.inr (or.inl heq₁) := or.inl heq₁^.symm
| or.inr (or.inr hgt₁) :=
match lt_trichotomy 0 b with
| or.inl hlt₂ :=
have 0 > a * b, from mul_neg_of_neg_of_pos hgt₁ hlt₂,
begin rw h at this, exact absurd this (lt_irrefl _) end
| or.inr (or.inl heq₂) := or.inr heq₂^.symm
| or.inr (or.inr hgt₂) :=
have 0 < a * b, from mul_pos_of_neg_of_neg hgt₁ hgt₂,
begin rw h at this, exact absurd this (lt_irrefl _) end
end
end
instance linear_ordered_comm_ring.to_integral_domain [s: linear_ordered_comm_ring α] : integral_domain α :=
{s with
eq_zero_or_eq_zero_of_mul_eq_zero := @linear_ordered_comm_ring.eq_zero_or_eq_zero_of_mul_eq_zero α s }
class decidable_linear_ordered_comm_ring (α : Type u) extends linear_ordered_comm_ring α,
decidable_linear_ordered_comm_group α
instance decidable_linear_ordered_comm_ring.to_decidable_linear_ordered_semiring [d : decidable_linear_ordered_comm_ring α] :
decidable_linear_ordered_semiring α :=
let s : linear_ordered_semiring α := @linear_ordered_ring.to_linear_ordered_semiring α _ in
{d with
zero_mul := @linear_ordered_semiring.zero_mul α s,
mul_zero := @linear_ordered_semiring.mul_zero α s,
add_left_cancel := @linear_ordered_semiring.add_left_cancel α s,
add_right_cancel := @linear_ordered_semiring.add_right_cancel α s,
le_of_add_le_add_left := @linear_ordered_semiring.le_of_add_le_add_left α s,
lt_of_add_lt_add_left := @linear_ordered_semiring.lt_of_add_lt_add_left α s,
mul_le_mul_of_nonneg_left := @linear_ordered_semiring.mul_le_mul_of_nonneg_left α s,
mul_le_mul_of_nonneg_right := @linear_ordered_semiring.mul_le_mul_of_nonneg_right α s,
mul_lt_mul_of_pos_left := @linear_ordered_semiring.mul_lt_mul_of_pos_left α s,
mul_lt_mul_of_pos_right := @linear_ordered_semiring.mul_lt_mul_of_pos_right α s}
|
f27bf5bbb258073eeb411f98eae4d0cf347bfc28 | a7dd8b83f933e72c40845fd168dde330f050b1c9 | /src/analysis/normed_space/banach.lean | 2e2374b90b27c044ffc2950b06964315d3a8f99f | [
"Apache-2.0"
] | permissive | NeilStrickland/mathlib | 10420e92ee5cb7aba1163c9a01dea2f04652ed67 | 3efbd6f6dff0fb9b0946849b43b39948560a1ffe | refs/heads/master | 1,589,043,046,346 | 1,558,938,706,000 | 1,558,938,706,000 | 181,285,984 | 0 | 0 | Apache-2.0 | 1,568,941,848,000 | 1,555,233,833,000 | Lean | UTF-8 | Lean | false | false | 10,515 | lean | /-
Copyright (c) 2019 Sébastien Gouëzel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sébastien Gouëzel
Banach spaces, i.e., complete vector spaces.
This file contains the Banach open mapping theorem, i.e., the fact that a bijective
bounded linear map between Banach spaces has a bounded inverse.
-/
import topology.metric_space.baire analysis.normed_space.bounded_linear_maps
local attribute [instance] classical.prop_decidable
open function metric set filter finset
class banach_space (α : Type*) (β : Type*) [normed_field α]
extends normed_space α β, complete_space β
variables {k : Type*} [nondiscrete_normed_field k]
variables {E : Type*} [banach_space k E]
variables {F : Type*} [banach_space k F] {f : E → F}
include k
set_option class.instance_max_depth 70
/-- The Banach open mapping theorem: if a bounded linear map between Banach spaces is onto, then
any point has a preimage with controlled norm. -/
theorem exists_preimage_norm_le (hf : is_bounded_linear_map k f) (surj : surjective f) :
∃C, 0 < C ∧ ∀y, ∃x, f x = y ∧ ∥x∥ ≤ C * ∥y∥ :=
begin
have lin := hf.to_is_linear_map,
haveI : nonempty F := ⟨0⟩,
/- First step of the proof (using completeness of `F`): by Baire's theorem, there exists a
ball in E whose image closure has nonempty interior. Rescaling everything, it follows that
any `y ∈ F` is arbitrarily well approached by images of elements of norm at
most `C * ∥y∥`. For further use, we will only need such an element whose image
is within distance ∥y∥/2 of y, to apply an iterative process. -/
have A : (⋃n:ℕ, closure (f '' (ball 0 n))) = univ,
{ refine subset.antisymm (subset_univ _) (λy hy, _),
rcases surj y with ⟨x, hx⟩,
rcases exists_nat_gt (∥x∥) with ⟨n, hn⟩,
refine mem_Union.2 ⟨n, subset_closure _⟩,
refine (mem_image _ _ _).2 ⟨x, ⟨_, hx⟩⟩,
rwa [mem_ball, dist_eq_norm, sub_zero] },
have : ∃(n:ℕ) y ε, 0 < ε ∧ ball y ε ⊆ closure (f '' (ball 0 n)) :=
nonempty_interior_of_Union_of_closed (λn, is_closed_closure) A,
have : ∃C, 0 ≤ C ∧ ∀y, ∃x, dist (f x) y ≤ (1/2) * ∥y∥ ∧ ∥x∥ ≤ C * ∥y∥,
{ rcases this with ⟨n, a, ε, ⟨εpos, H⟩⟩,
rcases exists_one_lt_norm k with ⟨c, hc⟩,
refine ⟨(ε/2)⁻¹ * ∥c∥ * 2 * n, _, λy, _⟩,
{ refine mul_nonneg (mul_nonneg (mul_nonneg _ (norm_nonneg _)) (by norm_num)) _,
refine inv_nonneg.2 (div_nonneg' (le_of_lt εpos) (by norm_num)),
exact nat.cast_nonneg n },
{ by_cases hy : y = 0,
{ use 0, simp [hy, lin.map_zero] },
{ rcases rescale_to_shell hc (half_pos εpos) hy with ⟨d, hd, ydle, leyd, dinv⟩,
let δ := ∥d∥ * ∥y∥/4,
have δpos : 0 < δ :=
div_pos (mul_pos ((norm_pos_iff _).2 hd) ((norm_pos_iff _).2 hy)) (by norm_num),
have : a + d • y ∈ ball a ε,
by simp [dist_eq_norm, lt_of_le_of_lt ydle (half_lt_self εpos)],
rcases mem_closure_iff'.1 (H this) _ δpos with ⟨z₁, z₁im, h₁⟩,
rcases (mem_image _ _ _).1 z₁im with ⟨x₁, hx₁, xz₁⟩,
rw ← xz₁ at h₁,
rw [mem_ball, dist_eq_norm, sub_zero] at hx₁,
have : a ∈ ball a ε, by { simp, exact εpos },
rcases mem_closure_iff'.1 (H this) _ δpos with ⟨z₂, z₂im, h₂⟩,
rcases (mem_image _ _ _).1 z₂im with ⟨x₂, hx₂, xz₂⟩,
rw ← xz₂ at h₂,
rw [mem_ball, dist_eq_norm, sub_zero] at hx₂,
let x := x₁ - x₂,
have I : ∥f x - d • y∥ ≤ 2 * δ := calc
∥f x - d • y∥ = ∥f x₁ - (a + d • y) - (f x₂ - a)∥ :
by { congr' 1, simp only [x, lin.map_sub], abel }
... ≤ ∥f x₁ - (a + d • y)∥ + ∥f x₂ - a∥ :
norm_triangle_sub
... ≤ δ + δ : begin
apply add_le_add,
{ rw [← dist_eq_norm, dist_comm], exact le_of_lt h₁ },
{ rw [← dist_eq_norm, dist_comm], exact le_of_lt h₂ }
end
... = 2 * δ : (two_mul _).symm,
have J : ∥f (d⁻¹ • x) - y∥ ≤ 1/2 * ∥y∥ := calc
∥f (d⁻¹ • x) - y∥ = ∥d⁻¹ • f x - (d⁻¹ * d) • y∥ :
by rwa [lin.smul, inv_mul_cancel, one_smul]
... = ∥d⁻¹ • (f x - d • y)∥ : by rw [mul_smul, smul_sub]
... = ∥d∥⁻¹ * ∥f x - d • y∥ : by rw [norm_smul, norm_inv]
... ≤ ∥d∥⁻¹ * (2 * δ) : begin
apply mul_le_mul_of_nonneg_left I,
rw inv_nonneg,
exact norm_nonneg _
end
... = (∥d∥⁻¹ * ∥d∥) * ∥y∥ /2 : by { simp only [δ], ring }
... = ∥y∥/2 : by { rw [inv_mul_cancel, one_mul], simp [norm_eq_zero, hd] }
... = (1/2) * ∥y∥ : by ring,
rw ← dist_eq_norm at J,
have K : ∥d⁻¹ • x∥ ≤ (ε / 2)⁻¹ * ∥c∥ * 2 * ↑n * ∥y∥ := calc
∥d⁻¹ • x∥ = ∥d∥⁻¹ * ∥x₁ - x₂∥ : by rw [norm_smul, norm_inv]
... ≤ ((ε / 2)⁻¹ * ∥c∥ * ∥y∥) * (n + n) : begin
refine mul_le_mul dinv _ (norm_nonneg _) _,
{ exact le_trans (norm_triangle_sub) (add_le_add (le_of_lt hx₁) (le_of_lt hx₂)) },
{ apply mul_nonneg (mul_nonneg _ (norm_nonneg _)) (norm_nonneg _),
exact inv_nonneg.2 (le_of_lt (half_pos εpos)) }
end
... = (ε / 2)⁻¹ * ∥c∥ * 2 * ↑n * ∥y∥ : by ring,
exact ⟨d⁻¹ • x, J, K⟩ } } },
rcases this with ⟨C, C0, hC⟩,
/- Second step of the proof: starting from `y`, we want an exact preimage of `y`. Let `g y` be
the approximate preimage of `y` given by the first step, and `h y = y - f(g y)` the part that
has no preimage yet. We will iterate this process, taking the approximate preimage of `h y`,
leaving only `h^2 y` without preimage yet, and so on. Let `u n` be the approximate preimage
of `h^n y`. Then `u` is a converging series, and by design the sum of the series is a
preimage of `y`. This uses completeness of `E`. -/
choose g hg using hC,
let h := λy, y - f (g y),
have hle : ∀y, ∥h y∥ ≤ (1/2) * ∥y∥,
{ assume y,
rw [← dist_eq_norm, dist_comm],
exact (hg y).1 },
refine ⟨2 * C + 1, by linarith, λy, _⟩,
have hnle : ∀n:ℕ, ∥(h^[n]) y∥ ≤ (1/2)^n * ∥y∥,
{ assume n,
induction n with n IH,
{ simp only [one_div_eq_inv, nat.nat_zero_eq_zero, one_mul, nat.iterate_zero, pow_zero] },
{ rw [nat.iterate_succ'],
apply le_trans (hle _) _,
rw [pow_succ, mul_assoc],
apply mul_le_mul_of_nonneg_left IH,
norm_num } },
let u := λn, g((h^[n]) y),
have ule : ∀n, ∥u n∥ ≤ (1/2)^n * (C * ∥y∥),
{ assume n,
apply le_trans (hg _).2 _,
calc C * ∥(h^[n]) y∥ ≤ C * ((1/2)^n * ∥y∥) : mul_le_mul_of_nonneg_left (hnle n) C0
... = (1 / 2) ^ n * (C * ∥y∥) : by ring },
have sNu : summable (λn, ∥u n∥),
{ refine summable_of_nonneg_of_le (λn, norm_nonneg _) ule _,
exact summable_mul_right _ (summable_geometric (by norm_num) (by norm_num)) },
have su : summable u := summable_of_summable_norm sNu,
let x := tsum u,
have x_ineq : ∥x∥ ≤ (2 * C + 1) * ∥y∥ := calc
∥x∥ ≤ (∑n, ∥u n∥) : norm_tsum_le_tsum_norm sNu
... ≤ (∑n, (1/2)^n * (C * ∥y∥)) :
tsum_le_tsum ule sNu (summable_mul_right _ summable_geometric_two)
... = (∑n, (1/2)^n) * (C * ∥y∥) : by { rw tsum_mul_right, exact summable_geometric_two }
... = 2 * (C * ∥y∥) : by rw tsum_geometric_two
... = 2 * C * ∥y∥ + 0 : by rw [add_zero, mul_assoc]
... ≤ 2 * C * ∥y∥ + ∥y∥ : add_le_add (le_refl _) (norm_nonneg _)
... = (2 * C + 1) * ∥y∥ : by ring,
have fsumeq : ∀n:ℕ, f((range n).sum u) = y - (h^[n]) y,
{ assume n,
induction n with n IH,
{ simp [lin.map_zero] },
{ rw [sum_range_succ, lin.add, IH, nat.iterate_succ'],
simp [u, h] } },
have : tendsto (λn, (range n).sum u) at_top (nhds x) :=
tendsto_sum_nat_of_has_sum (has_sum_tsum su),
have L₁ : tendsto (λn, f((range n).sum u)) at_top (nhds (f x)) :=
tendsto.comp (hf.continuous.tendsto _) this,
simp only [fsumeq] at L₁,
have L₂ : tendsto (λn, y - (h^[n]) y) at_top (nhds (y - 0)),
{ refine tendsto_sub tendsto_const_nhds _,
rw tendsto_iff_norm_tendsto_zero,
simp only [sub_zero],
refine squeeze_zero (λ_, norm_nonneg _) hnle _,
have : 0 = 0 * ∥y∥, by rw zero_mul,
rw this,
refine tendsto_mul _ tendsto_const_nhds,
exact tendsto_pow_at_top_nhds_0_of_lt_1 (by norm_num) (by norm_num) },
have feq : f x = y - 0,
{ apply tendsto_nhds_unique _ L₁ L₂,
simp },
rw sub_zero at feq,
exact ⟨x, feq, x_ineq⟩
end
/-- The Banach open mapping theorem: a surjective bounded linear map between Banach spaces is open. -/
theorem open_mapping (hf : is_bounded_linear_map k f) (surj : surjective f) : is_open_map f :=
begin
assume s hs,
have lin := hf.to_is_linear_map,
rcases exists_preimage_norm_le hf surj with ⟨C, Cpos, hC⟩,
refine is_open_iff.2 (λy yfs, _),
rcases mem_image_iff_bex.1 yfs with ⟨x, xs, fxy⟩,
rcases is_open_iff.1 hs x xs with ⟨ε, εpos, hε⟩,
refine ⟨ε/C, div_pos εpos Cpos, λz hz, _⟩,
rcases hC (z-y) with ⟨w, wim, wnorm⟩,
have : f (x + w) = z, by { rw [lin.add, wim, fxy], abel },
rw ← this,
have : x + w ∈ ball x ε := calc
dist (x+w) x = ∥w∥ : by { rw dist_eq_norm, simp }
... ≤ C * ∥z - y∥ : wnorm
... < C * (ε/C) : begin
apply mul_lt_mul_of_pos_left _ Cpos,
rwa [mem_ball, dist_eq_norm] at hz,
end
... = ε : mul_div_cancel' _ (ne_of_gt Cpos),
exact set.mem_image_of_mem _ (hε this)
end
/-- If a bounded linear map is a bijection, then its inverse is also a bounded linear map. -/
theorem linear_equiv.is_bounded_inv (e : linear_equiv k E F) (h : is_bounded_linear_map k e.to_fun) :
is_bounded_linear_map k e.inv_fun :=
{ bound := begin
have : surjective e.to_fun := (equiv.bijective e.to_equiv).2,
rcases exists_preimage_norm_le h this with ⟨M, Mpos, hM⟩,
refine ⟨M, Mpos, λy, _⟩,
rcases hM y with ⟨x, hx, xnorm⟩,
have : x = e.inv_fun y, by { rw ← hx, exact (e.symm_apply_apply _).symm },
rwa ← this
end,
..e.symm }
|
dc0b36233d2024a6747c2217c0c800dde5fa52ab | e61a235b8468b03aee0120bf26ec615c045005d2 | /tests/lean/run/induction1.lean | 0e37260fc3aa25618524f589a85c4eee5612cd03 | [
"Apache-2.0"
] | permissive | SCKelemen/lean4 | 140dc63a80539f7c61c8e43e1c174d8500ec3230 | e10507e6615ddbef73d67b0b6c7f1e4cecdd82bc | refs/heads/master | 1,660,973,595,917 | 1,590,278,033,000 | 1,590,278,033,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,419 | lean | @[recursor 4]
def Or.elim2 {p q r : Prop} (major : p ∨ q) (left : p → r) (right : q → r) : r :=
Or.elim major left right
new_frontend
theorem tst0 {p q : Prop } (h : p ∨ q) : q ∨ p :=
begin
induction h;
{ apply Or.inr; assumption };
{ apply Or.inl; assumption }
end
theorem tst1 {p q : Prop } (h : p ∨ q) : q ∨ p :=
begin
induction h with
| inr h2 => exact Or.inl h2
| inl h1 => exact Or.inr h1
end
theorem tst2 {p q : Prop } (h : p ∨ q) : q ∨ p :=
begin
induction h using elim2 with
| left _ => { apply Or.inr; assumption }
| right _ => { apply Or.inl; assumption }
end
theorem tst3 {p q : Prop } (h : p ∨ q) : q ∨ p :=
begin
induction h using elim2 with
| right h => exact Or.inl h
| left h => exact Or.inr h
end
theorem tst4 {p q : Prop } (h : p ∨ q) : q ∨ p :=
begin
induction h using elim2 with
| right h => ?myright
| left h => ?myleft;
case myleft { exact Or.inr h };
case myright { exact Or.inl h };
end
theorem tst5 {p q : Prop } (h : p ∨ q) : q ∨ p :=
begin
induction h using elim2 with
| right h => _
| left h => { refine Or.inr _; exact h };
case right { exact Or.inl h }
end
theorem tst6 {p q : Prop } (h : p ∨ q) : q ∨ p :=
begin
cases h with
| inr h2 => exact Or.inl h2
| inl h1 => exact Or.inr h1
end
theorem tst7 {α : Type} (xs : List α) (h : (a : α) → (as : List α) → xs ≠ a :: as) : xs = [] :=
begin
induction xs with
| nil => exact rfl
| cons z zs ih => exact absurd rfl (h z zs)
end
theorem tst8 {α : Type} (xs : List α) (h : (a : α) → (as : List α) → xs ≠ a :: as) : xs = [] :=
begin
induction xs;
exact rfl;
exact absurd rfl $ h _ _
end
theorem tst9 {α : Type} (xs : List α) (h : (a : α) → (as : List α) → xs ≠ a :: as) : xs = [] :=
begin
cases xs with
| nil => exact rfl
| cons z zs => exact absurd rfl (h z zs)
end
theorem tst10 {p q : Prop } (h₁ : p ↔ q) (h₂ : p) : q :=
begin
induction h₁ using Iff.elim with
| _ h _ => exact h h₂
end
def Iff2 (m p q : Prop) := p ↔ q
theorem tst11 {p q r : Prop } (h₁ : Iff2 r p q) (h₂ : p) : q :=
begin
induction h₁ using Iff.elim with
| _ h _ => exact h h₂
end
theorem tst12 {p q : Prop } (h₁ : p ∨ q) (h₂ : p ↔ q) (h₃ : p) : q :=
begin
failIfSuccess (induction h₁ using Iff.elim);
induction h₂ using Iff.elim with
| _ h _ => exact h h₃
end
|
95b0b98a43d4bd77186fd2d703cf0410fe36df22 | 2a70b774d16dbdf5a533432ee0ebab6838df0948 | /_target/deps/mathlib/src/ring_theory/principal_ideal_domain.lean | 684f5c655a7beed10aab812163780bea666c4861 | [
"Apache-2.0"
] | permissive | hjvromen/lewis | 40b035973df7c77ebf927afab7878c76d05ff758 | 105b675f73630f028ad5d890897a51b3c1146fb0 | refs/heads/master | 1,677,944,636,343 | 1,676,555,301,000 | 1,676,555,301,000 | 327,553,599 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 8,959 | lean | /-
Copyright (c) 2018 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Chris Hughes, Morenikeji Neri
-/
import ring_theory.noetherian
import ring_theory.unique_factorization_domain
/-!
# Principal ideal rings and principal ideal domains
A principal ideal ring (PIR) is a commutative ring in which all ideals are principal. A
principal ideal domain (PID) is an integral domain which is a principal ideal ring.
# Main definitions
Note that for principal ideal domains, one should use
`[integral domain R] [is_principal_ideal_ring R]`. There is no explicit definition of a PID.
Theorems about PID's are in the `principal_ideal_ring` namespace.
- `is_principal_ideal_ring`: a predicate on commutative rings, saying that every
ideal is principal.
- `generator`: a generator of a principal ideal (or more generally submodule)
- `to_unique_factorization_monoid`: a PID is a unique factorization domain
# Main results
- `to_maximal_ideal`: a non-zero prime ideal in a PID is maximal.
- `euclidean_domain.to_principal_ideal_domain` : a Euclidean domain is a PID.
-/
universes u v
variables {R : Type u} {M : Type v}
open set function
open submodule
open_locale classical
/-- An `R`-submodule of `M` is principal if it is generated by one element. -/
class submodule.is_principal [ring R] [add_comm_group M] [module R M] (S : submodule R M) : Prop :=
(principal [] : ∃ a, S = span R {a})
/-- A commutative ring is a principal ideal ring if all ideals are principal. -/
class is_principal_ideal_ring (R : Type u) [comm_ring R] : Prop :=
(principal : ∀ (S : ideal R), S.is_principal)
attribute [instance] is_principal_ideal_ring.principal
namespace submodule.is_principal
variables [comm_ring R] [add_comm_group M] [module R M]
/-- `generator I`, if `I` is a principal submodule, is an `x ∈ M` such that `span R {x} = I` -/
noncomputable def generator (S : submodule R M) [S.is_principal] : M :=
classical.some (principal S)
lemma span_singleton_generator (S : submodule R M) [S.is_principal] : span R {generator S} = S :=
eq.symm (classical.some_spec (principal S))
@[simp] lemma generator_mem (S : submodule R M) [S.is_principal] : generator S ∈ S :=
by { conv_rhs { rw ← span_singleton_generator S }, exact subset_span (mem_singleton _) }
lemma mem_iff_eq_smul_generator (S : submodule R M) [S.is_principal] {x : M} :
x ∈ S ↔ ∃ s : R, x = s • generator S :=
by simp_rw [@eq_comm _ x, ← mem_span_singleton, span_singleton_generator]
lemma mem_iff_generator_dvd (S : ideal R) [S.is_principal] {x : R} : x ∈ S ↔ generator S ∣ x :=
(mem_iff_eq_smul_generator S).trans (exists_congr (λ a, by simp only [mul_comm, smul_eq_mul]))
lemma eq_bot_iff_generator_eq_zero (S : submodule R M) [S.is_principal] :
S = ⊥ ↔ generator S = 0 :=
by rw [← @span_singleton_eq_bot R M, span_singleton_generator]
end submodule.is_principal
namespace is_prime
open submodule.is_principal ideal
-- TODO -- for a non-ID one could perhaps prove that if p < q are prime then q maximal;
-- 0 isn't prime in a non-ID PIR but the Krull dimension is still <= 1.
-- The below result follows from this, but we could also use the below result to
-- prove this (quotient out by p).
lemma to_maximal_ideal [integral_domain R] [is_principal_ideal_ring R] {S : ideal R}
[hpi : is_prime S] (hS : S ≠ ⊥) : is_maximal S :=
is_maximal_iff.2 ⟨(ne_top_iff_one S).1 hpi.1, begin
assume T x hST hxS hxT,
cases (mem_iff_generator_dvd _).1 (hST $ generator_mem S) with z hz,
cases hpi.2 (show generator T * z ∈ S, from hz ▸ generator_mem S),
{ have hTS : T ≤ S, rwa [← span_singleton_generator T, submodule.span_le, singleton_subset_iff],
exact (hxS $ hTS hxT).elim },
cases (mem_iff_generator_dvd _).1 h with y hy,
have : generator S ≠ 0 := mt (eq_bot_iff_generator_eq_zero _).2 hS,
rw [← mul_one (generator S), hy, mul_left_comm, mul_right_inj' this] at hz,
exact hz.symm ▸ T.mul_mem_right _ (generator_mem T)
end⟩
end is_prime
section
open euclidean_domain
variable [euclidean_domain R]
lemma mod_mem_iff {S : ideal R} {x y : R} (hy : y ∈ S) : x % y ∈ S ↔ x ∈ S :=
⟨λ hxy, div_add_mod x y ▸ S.add_mem (S.mul_mem_right _ hy) hxy,
λ hx, (mod_eq_sub_mul_div x y).symm ▸ S.sub_mem hx (S.mul_mem_right _ hy)⟩
@[priority 100] -- see Note [lower instance priority]
instance euclidean_domain.to_principal_ideal_domain : is_principal_ideal_ring R :=
{ principal := λ S, by exactI
⟨if h : {x : R | x ∈ S ∧ x ≠ 0}.nonempty
then
have wf : well_founded (euclidean_domain.r : R → R → Prop) := euclidean_domain.r_well_founded,
have hmin : well_founded.min wf {x : R | x ∈ S ∧ x ≠ 0} h ∈ S ∧
well_founded.min wf {x : R | x ∈ S ∧ x ≠ 0} h ≠ 0,
from well_founded.min_mem wf {x : R | x ∈ S ∧ x ≠ 0} h,
⟨well_founded.min wf {x : R | x ∈ S ∧ x ≠ 0} h,
submodule.ext $ λ x,
⟨λ hx, div_add_mod x (well_founded.min wf {x : R | x ∈ S ∧ x ≠ 0} h) ▸
(ideal.mem_span_singleton.2 $ dvd_add (dvd_mul_right _ _) $
have (x % (well_founded.min wf {x : R | x ∈ S ∧ x ≠ 0} h) ∉ {x : R | x ∈ S ∧ x ≠ 0}),
from λ h₁, well_founded.not_lt_min wf _ h h₁ (mod_lt x hmin.2),
have x % well_founded.min wf {x : R | x ∈ S ∧ x ≠ 0} h = 0, by finish [(mod_mem_iff hmin.1).2 hx],
by simp *),
λ hx, let ⟨y, hy⟩ := ideal.mem_span_singleton.1 hx in hy.symm ▸ S.mul_mem_right _ hmin.1⟩⟩
else ⟨0, submodule.ext $ λ a, by rw [← @submodule.bot_coe R R _ _ _, span_eq, submodule.mem_bot]; exact
⟨λ haS, by_contradiction $ λ ha0, h ⟨a, ⟨haS, ha0⟩⟩,
λ h₁, h₁.symm ▸ S.zero_mem⟩⟩⟩ }
end
namespace principal_ideal_ring
open is_principal_ideal_ring
variables [integral_domain R] [is_principal_ideal_ring R]
@[priority 100] -- see Note [lower instance priority]
instance is_noetherian_ring : is_noetherian_ring R :=
⟨assume s : ideal R,
begin
rcases (is_principal_ideal_ring.principal s).principal with ⟨a, rfl⟩,
rw [← finset.coe_singleton],
exact ⟨{a}, submodule.coe_injective rfl⟩
end⟩
lemma is_maximal_of_irreducible {p : R} (hp : irreducible p) :
ideal.is_maximal (span R ({p} : set R)) :=
⟨mt ideal.span_singleton_eq_top.1 hp.1, λ I hI, begin
rcases principal I with ⟨a, rfl⟩,
erw ideal.span_singleton_eq_top,
unfreezingI { rcases ideal.span_singleton_le_span_singleton.1 (le_of_lt hI) with ⟨b, rfl⟩ },
refine (of_irreducible_mul hp).resolve_right (mt (λ hb, _) (not_le_of_lt hI)),
erw [ideal.span_singleton_le_span_singleton, is_unit.mul_right_dvd hb]
end⟩
lemma irreducible_iff_prime {p : R} : irreducible p ↔ prime p :=
⟨λ hp, (ideal.span_singleton_prime hp.ne_zero).1 $
(is_maximal_of_irreducible hp).is_prime,
irreducible_of_prime⟩
lemma associates_irreducible_iff_prime : ∀{p : associates R}, irreducible p ↔ prime p :=
associates.irreducible_iff_prime_iff.1 (λ _, irreducible_iff_prime)
section
open_locale classical
/-- `factors a` is a multiset of irreducible elements whose product is `a`, up to units -/
noncomputable def factors (a : R) : multiset R :=
if h : a = 0 then ∅ else classical.some (wf_dvd_monoid.exists_factors a h)
lemma factors_spec (a : R) (h : a ≠ 0) :
(∀b∈factors a, irreducible b) ∧ associated (factors a).prod a :=
begin
unfold factors, rw [dif_neg h],
exact classical.some_spec (wf_dvd_monoid.exists_factors a h)
end
lemma ne_zero_of_mem_factors {R : Type v} [integral_domain R] [is_principal_ideal_ring R] {a b : R}
(ha : a ≠ 0) (hb : b ∈ factors a) : b ≠ 0 := irreducible.ne_zero ((factors_spec a ha).1 b hb)
lemma mem_submonoid_of_factors_subset_of_units_subset (s : submonoid R)
{a : R} (ha : a ≠ 0) (hfac : ∀ b ∈ factors a, b ∈ s) (hunit : ∀ c : units R, (c : R) ∈ s) :
a ∈ s :=
begin
rcases ((factors_spec a ha).2) with ⟨c, hc⟩,
rw [← hc],
exact submonoid.mul_mem _ (submonoid.multiset_prod_mem _ _ hfac) (hunit _),
end
/-- If a `ring_hom` maps all units and all factors of an element `a` into a submonoid `s`, then it
also maps `a` into that submonoid. -/
lemma ring_hom_mem_submonoid_of_factors_subset_of_units_subset {R S : Type*}
[integral_domain R] [is_principal_ideal_ring R] [semiring S]
(f : R →+* S) (s : submonoid S) (a : R) (ha : a ≠ 0)
(h : ∀ b ∈ factors a, f b ∈ s) (hf: ∀ c : units R, f c ∈ s) :
f a ∈ s :=
mem_submonoid_of_factors_subset_of_units_subset (s.comap f.to_monoid_hom) ha h hf
/-- A principal ideal domain has unique factorization -/
@[priority 100] -- see Note [lower instance priority]
instance to_unique_factorization_monoid : unique_factorization_monoid R :=
{ irreducible_iff_prime := λ _, principal_ideal_ring.irreducible_iff_prime
.. (is_noetherian_ring.wf_dvd_monoid : wf_dvd_monoid R) }
end
end principal_ideal_ring
|
f2bacb54e0ef987dd06db954cf53806081efd6ca | 624f6f2ae8b3b1adc5f8f67a365c51d5126be45a | /tests/lean/run/eval_unboxed_const.lean | 06c9d3814ec2e58de5295e0760b622c807e09fc9 | [
"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 | 479 | lean | import Init.Lean
open Lean
def c1 : Bool := true
unsafe def tst1 : MetaIO Unit := do
env ← MetaIO.getEnv;
let v := env.evalConst Bool `c1;
IO.println v
#eval tst1 -- outputs incorrect value (ok false). Reason: the unboxed value `true` is `1`, but the boxed `false` value is also `1`.
def c2 : Bool := false
unsafe def tst2 : MetaIO Unit := do
env ← MetaIO.getEnv;
let v := env.evalConst Bool `c2;
IO.println v
#eval tst2 -- crashes since `0` is not a valid boxed value
|
e2f0952e16b1093140482725f0af7d08f796cd27 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/number_theory/padics/padic_val.lean | f6689bb034751537ecca50728add90f9895cb500 | [
"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 | 20,067 | lean | /-
Copyright (c) 2018 Robert Y. Lewis. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Robert Y. Lewis
-/
import number_theory.divisors
import ring_theory.int.basic
import tactic.ring_exp
/-!
# p-adic Valuation
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
This file defines the `p`-adic valuation on `ℕ`, `ℤ`, and `ℚ`.
The `p`-adic valuation on `ℚ` is the difference of the multiplicities of `p` in the numerator and
denominator of `q`. This function obeys the standard properties of a valuation, with the appropriate
assumptions on `p`. The `p`-adic valuations on `ℕ` and `ℤ` agree with that on `ℚ`.
The valuation induces a norm on `ℚ`. This norm is defined in padic_norm.lean.
## Notations
This file uses the local notation `/.` for `rat.mk`.
## Implementation notes
Much, but not all, of this file assumes that `p` is prime. This assumption is inferred automatically
by taking `[fact p.prime]` as a type class argument.
## References
* [F. Q. Gouvêa, *p-adic numbers*][gouvea1997]
* [R. Y. Lewis, *A formal proof of Hensel's lemma over the p-adic integers*][lewis2019]
* <https://en.wikipedia.org/wiki/P-adic_number>
## Tags
p-adic, p adic, padic, norm, valuation
-/
universe u
open nat
open_locale rat
open multiplicity
/-- For `p ≠ 1`, the `p`-adic valuation of a natural `n ≠ 0` is the largest natural number `k` such
that `p^k` divides `z`. If `n = 0` or `p = 1`, then `padic_val_nat p q` defaults to `0`. -/
def padic_val_nat (p : ℕ) (n : ℕ) : ℕ :=
if h : p ≠ 1 ∧ 0 < n then (multiplicity p n).get (multiplicity.finite_nat_iff.2 h) else 0
namespace padic_val_nat
open multiplicity
variables {p : ℕ}
/-- `padic_val_nat p 0` is `0` for any `p`. -/
@[simp] protected lemma zero : padic_val_nat p 0 = 0 := by simp [padic_val_nat]
/-- `padic_val_nat p 1` is `0` for any `p`. -/
@[simp] protected lemma one : padic_val_nat p 1 = 0 := by { unfold padic_val_nat, split_ifs, simp }
/-- If `p ≠ 0` and `p ≠ 1`, then `padic_val_rat p p` is `1`. -/
@[simp] lemma self (hp : 1 < p) : padic_val_nat p p = 1 :=
begin
have neq_one : (¬ p = 1) ↔ true,
{ exact iff_of_true ((ne_of_lt hp).symm) trivial },
have eq_zero_false : p = 0 ↔ false,
{ exact iff_false_intro ((ne_of_lt (trans zero_lt_one hp)).symm) },
simp [padic_val_nat, neq_one, eq_zero_false]
end
@[simp] lemma eq_zero_iff {n : ℕ} : padic_val_nat p n = 0 ↔ p = 1 ∨ n = 0 ∨ ¬ p ∣ n :=
by simp only [padic_val_nat, dite_eq_right_iff, part_enat.get_eq_iff_eq_coe, nat.cast_zero,
multiplicity_eq_zero, and_imp, pos_iff_ne_zero, ne.def, ← or_iff_not_imp_left]
lemma eq_zero_of_not_dvd {n : ℕ} (h : ¬ p ∣ n) : padic_val_nat p n = 0 :=
eq_zero_iff.2 $ or.inr $ or.inr h
end padic_val_nat
/-- For `p ≠ 1`, the `p`-adic valuation of an integer `z ≠ 0` is the largest natural number `k` such
that `p^k` divides `z`. If `x = 0` or `p = 1`, then `padic_val_int p q` defaults to `0`. -/
def padic_val_int (p : ℕ) (z : ℤ) : ℕ := padic_val_nat p z.nat_abs
namespace padic_val_int
open multiplicity
variables {p : ℕ}
lemma of_ne_one_ne_zero {z : ℤ} (hp : p ≠ 1) (hz : z ≠ 0) : padic_val_int p z =
(multiplicity (p : ℤ) z).get (by {apply multiplicity.finite_int_iff.2, simp [hp, hz]}) :=
begin
rw [padic_val_int, padic_val_nat, dif_pos (and.intro hp (int.nat_abs_pos_of_ne_zero hz))],
simp only [multiplicity.int.nat_abs p z],
refl
end
/-- `padic_val_int p 0` is `0` for any `p`. -/
@[simp] protected lemma zero : padic_val_int p 0 = 0 := by simp [padic_val_int]
/-- `padic_val_int p 1` is `0` for any `p`. -/
@[simp] protected lemma one : padic_val_int p 1 = 0 := by simp [padic_val_int]
/-- The `p`-adic value of a natural is its `p`-adic value as an integer. -/
@[simp] lemma of_nat {n : ℕ} : padic_val_int p n = padic_val_nat p n := by simp [padic_val_int]
/-- If `p ≠ 0` and `p ≠ 1`, then `padic_val_int p p` is `1`. -/
lemma self (hp : 1 < p) : padic_val_int p p = 1 := by simp [padic_val_nat.self hp]
lemma eq_zero_of_not_dvd {z : ℤ} (h : ¬ (p : ℤ) ∣ z) : padic_val_int p z = 0 :=
begin
rw [padic_val_int, padic_val_nat],
split_ifs; simp [multiplicity.int.nat_abs, multiplicity_eq_zero.2 h],
end
end padic_val_int
/-- `padic_val_rat` defines the valuation of a rational `q` to be the valuation of `q.num` minus the
valuation of `q.denom`. If `q = 0` or `p = 1`, then `padic_val_rat p q` defaults to `0`. -/
def padic_val_rat (p : ℕ) (q : ℚ) : ℤ := padic_val_int p q.num - padic_val_nat p q.denom
namespace padic_val_rat
open multiplicity
variables {p : ℕ}
/-- `padic_val_rat p q` is symmetric in `q`. -/
@[simp] protected lemma neg (q : ℚ) : padic_val_rat p (-q) = padic_val_rat p q :=
by simp [padic_val_rat, padic_val_int]
/-- `padic_val_rat p 0` is `0` for any `p`. -/
@[simp] protected lemma zero : padic_val_rat p 0 = 0 := by simp [padic_val_rat]
/-- `padic_val_rat p 1` is `0` for any `p`. -/
@[simp] protected lemma one : padic_val_rat p 1 = 0 := by simp [padic_val_rat]
/-- The `p`-adic value of an integer `z ≠ 0` is its `p`-adic_value as a rational. -/
@[simp] lemma of_int {z : ℤ} : padic_val_rat p z = padic_val_int p z := by simp [padic_val_rat]
/-- The `p`-adic value of an integer `z ≠ 0` is the multiplicity of `p` in `z`. -/
lemma of_int_multiplicity {z : ℤ} (hp : p ≠ 1) (hz : z ≠ 0) :
padic_val_rat p (z : ℚ) = (multiplicity (p : ℤ) z).get (finite_int_iff.2 ⟨hp, hz⟩) :=
by rw [of_int, padic_val_int.of_ne_one_ne_zero hp hz]
lemma multiplicity_sub_multiplicity {q : ℚ} (hp : p ≠ 1) (hq : q ≠ 0) : padic_val_rat p q =
(multiplicity (p : ℤ) q.num).get (finite_int_iff.2 ⟨hp, rat.num_ne_zero_of_ne_zero hq⟩) -
(multiplicity p q.denom).get (by { rw [← finite_iff_dom, finite_nat_iff], exact ⟨hp, q.pos⟩ }) :=
begin
rw [padic_val_rat, padic_val_int.of_ne_one_ne_zero hp, padic_val_nat, dif_pos],
{ refl },
{ exact ⟨hp, q.pos⟩ },
{ exact rat.num_ne_zero_of_ne_zero hq }
end
/-- The `p`-adic value of an integer `z ≠ 0` is its `p`-adic value as a rational. -/
@[simp] lemma of_nat {n : ℕ} : padic_val_rat p n = padic_val_nat p n := by simp [padic_val_rat]
/-- If `p ≠ 0` and `p ≠ 1`, then `padic_val_rat p p` is `1`. -/
lemma self (hp : 1 < p) : padic_val_rat p p = 1 := by simp [hp]
end padic_val_rat
section padic_val_nat
variables {p : ℕ}
lemma zero_le_padic_val_rat_of_nat (n : ℕ) : 0 ≤ padic_val_rat p n := by simp
/-- `padic_val_rat` coincides with `padic_val_nat`. -/
@[norm_cast] lemma padic_val_rat_of_nat (n : ℕ) : ↑(padic_val_nat p n) = padic_val_rat p n :=
by simp
/-- A simplification of `padic_val_nat` when one input is prime, by analogy with
`padic_val_rat_def`. -/
lemma padic_val_nat_def [hp : fact p.prime] {n : ℕ} (hn : 0 < n) :
padic_val_nat p n
= (multiplicity p n).get (multiplicity.finite_nat_iff.2 ⟨hp.out.ne_one, hn⟩) :=
dif_pos ⟨hp.out.ne_one, hn⟩
lemma padic_val_nat_def' {n : ℕ} (hp : p ≠ 1) (hn : 0 < n) :
↑(padic_val_nat p n) = multiplicity p n :=
by simp [padic_val_nat, hp, hn]
@[simp] lemma padic_val_nat_self [fact p.prime] : padic_val_nat p p = 1 :=
by simp [padic_val_nat_def (fact.out p.prime).pos]
lemma one_le_padic_val_nat_of_dvd {n : ℕ} [hp : fact p.prime] (hn : 0 < n) (div : p ∣ n) :
1 ≤ padic_val_nat p n :=
by rwa [← part_enat.coe_le_coe, padic_val_nat_def' hp.out.ne_one hn, ← pow_dvd_iff_le_multiplicity,
pow_one]
lemma dvd_iff_padic_val_nat_ne_zero {p n : ℕ} [fact p.prime] (hn0 : n ≠ 0) :
(p ∣ n) ↔ padic_val_nat p n ≠ 0 :=
⟨λ h, one_le_iff_ne_zero.mp (one_le_padic_val_nat_of_dvd hn0.bot_lt h),
λ h, not_not.1 (mt padic_val_nat.eq_zero_of_not_dvd h)⟩
end padic_val_nat
namespace padic_val_rat
open multiplicity
variables {p : ℕ} [hp : fact p.prime]
include hp
/-- The multiplicity of `p : ℕ` in `a : ℤ` is finite exactly when `a ≠ 0`. -/
lemma finite_int_prime_iff {a : ℤ} : finite (p : ℤ) a ↔ a ≠ 0 :=
by simp [finite_int_iff, ne.symm (ne_of_lt hp.1.one_lt)]
/-- A rewrite lemma for `padic_val_rat p q` when `q` is expressed in terms of `rat.mk`. -/
protected lemma defn (p : ℕ) [hp : fact p.prime] {q : ℚ} {n d : ℤ} (hqz : q ≠ 0)
(qdf : q = n /. d) : padic_val_rat p q
= (multiplicity (p : ℤ) n).get
(finite_int_iff.2 ⟨ne.symm $ ne_of_lt hp.1.one_lt, λ hn, by simp * at *⟩)
- (multiplicity (p : ℤ) d).get
(finite_int_iff.2 ⟨ne.symm $ ne_of_lt hp.1.one_lt, λ hd, by simp * at *⟩) :=
have hd : d ≠ 0, from rat.mk_denom_ne_zero_of_ne_zero hqz qdf,
let ⟨c, hc1, hc2⟩ := rat.num_denom_mk hd qdf in
begin
rw [padic_val_rat.multiplicity_sub_multiplicity];
simp [hc1, hc2, multiplicity.mul' (nat.prime_iff_prime_int.1 hp.1),
ne.symm (ne_of_lt hp.1.one_lt), hqz, pos_iff_ne_zero, int.coe_nat_multiplicity p q.denom]
end
/-- A rewrite lemma for `padic_val_rat p (q * r)` with conditions `q ≠ 0`, `r ≠ 0`. -/
protected lemma mul {q r : ℚ} (hq : q ≠ 0) (hr : r ≠ 0) :
padic_val_rat p (q * r) = padic_val_rat p q + padic_val_rat p r :=
have q * r = (q.num * r.num) /. (q.denom * r.denom), by rw_mod_cast rat.mul_num_denom,
have hq' : q.num /. q.denom ≠ 0, by rw rat.num_denom; exact hq,
have hr' : r.num /. r.denom ≠ 0, by rw rat.num_denom; exact hr,
have hp' : _root_.prime (p : ℤ), from nat.prime_iff_prime_int.1 hp.1,
begin
rw [padic_val_rat.defn p (mul_ne_zero hq hr) this],
conv_rhs { rw [← @rat.num_denom q, padic_val_rat.defn p hq', ← @rat.num_denom r,
padic_val_rat.defn p hr'] },
rw [multiplicity.mul' hp', multiplicity.mul' hp']; simp [add_comm, add_left_comm, sub_eq_add_neg]
end
/-- A rewrite lemma for `padic_val_rat p (q^k)` with condition `q ≠ 0`. -/
protected lemma pow {q : ℚ} (hq : q ≠ 0) {k : ℕ} :
padic_val_rat p (q ^ k) = k * padic_val_rat p q :=
by induction k; simp [*, padic_val_rat.mul hq (pow_ne_zero _ hq), pow_succ, add_mul, add_comm]
/-- A rewrite lemma for `padic_val_rat p (q⁻¹)` with condition `q ≠ 0`. -/
protected lemma inv (q : ℚ) : padic_val_rat p q⁻¹ = -padic_val_rat p q :=
begin
by_cases hq : q = 0,
{ simp [hq] },
{ rw [eq_neg_iff_add_eq_zero, ← padic_val_rat.mul (inv_ne_zero hq) hq, inv_mul_cancel hq,
padic_val_rat.one],
exact hp },
end
/-- A rewrite lemma for `padic_val_rat p (q / r)` with conditions `q ≠ 0`, `r ≠ 0`. -/
protected lemma div {q r : ℚ} (hq : q ≠ 0) (hr : r ≠ 0) :
padic_val_rat p (q / r) = padic_val_rat p q - padic_val_rat p r :=
begin
rw [div_eq_mul_inv, padic_val_rat.mul hq (inv_ne_zero hr), padic_val_rat.inv r, sub_eq_add_neg],
all_goals { exact hp }
end
/-- A condition for `padic_val_rat p (n₁ / d₁) ≤ padic_val_rat p (n₂ / d₂)`, in terms of
divisibility by `p^n`. -/
lemma padic_val_rat_le_padic_val_rat_iff {n₁ n₂ d₁ d₂ : ℤ}
(hn₁ : n₁ ≠ 0) (hn₂ : n₂ ≠ 0) (hd₁ : d₁ ≠ 0) (hd₂ : d₂ ≠ 0) :
padic_val_rat p (n₁ /. d₁) ≤ padic_val_rat p (n₂ /. d₂) ↔
∀ n : ℕ, ↑p ^ n ∣ n₁ * d₂ → ↑p ^ n ∣ n₂ * d₁ :=
have hf1 : finite (p : ℤ) (n₁ * d₂),
from finite_int_prime_iff.2 (mul_ne_zero hn₁ hd₂),
have hf2 : finite (p : ℤ) (n₂ * d₁),
from finite_int_prime_iff.2 (mul_ne_zero hn₂ hd₁),
by conv
{ to_lhs,
rw [padic_val_rat.defn p (rat.mk_ne_zero_of_ne_zero hn₁ hd₁) rfl,
padic_val_rat.defn p (rat.mk_ne_zero_of_ne_zero hn₂ hd₂) rfl,
sub_le_iff_le_add', ← add_sub_assoc, le_sub_iff_add_le],
norm_cast,
rw [← multiplicity.mul' (nat.prime_iff_prime_int.1 hp.1) hf1, add_comm,
← multiplicity.mul' (nat.prime_iff_prime_int.1 hp.1) hf2,
part_enat.get_le_get, multiplicity_le_multiplicity_iff] }
/-- Sufficient conditions to show that the `p`-adic valuation of `q` is less than or equal to the
`p`-adic valuation of `q + r`. -/
theorem le_padic_val_rat_add_of_le {q r : ℚ} (hqr : q + r ≠ 0)
(h : padic_val_rat p q ≤ padic_val_rat p r) : padic_val_rat p q ≤ padic_val_rat p (q + r) :=
if hq : q = 0 then by simpa [hq] using h else
if hr : r = 0 then by simp [hr] else
have hqn : q.num ≠ 0, from rat.num_ne_zero_of_ne_zero hq,
have hqd : (q.denom : ℤ) ≠ 0, by exact_mod_cast rat.denom_ne_zero _,
have hrn : r.num ≠ 0, from rat.num_ne_zero_of_ne_zero hr,
have hrd : (r.denom : ℤ) ≠ 0, by exact_mod_cast rat.denom_ne_zero _,
have hqreq : q + r = (q.num * r.denom + q.denom * r.num) /. (q.denom * r.denom),
from rat.add_num_denom _ _,
have hqrd : q.num * r.denom + q.denom * r.num ≠ 0,
from rat.mk_num_ne_zero_of_ne_zero hqr hqreq,
begin
conv_lhs { rw ← @rat.num_denom q },
rw [hqreq, padic_val_rat_le_padic_val_rat_iff hqn hqrd hqd (mul_ne_zero hqd hrd),
← multiplicity_le_multiplicity_iff, mul_left_comm,
multiplicity.mul (nat.prime_iff_prime_int.1 hp.1), add_mul],
rw [← @rat.num_denom q, ← @rat.num_denom r, padic_val_rat_le_padic_val_rat_iff hqn hrn hqd hrd,
← multiplicity_le_multiplicity_iff] at h,
calc _ ≤ min (multiplicity ↑p (q.num * ↑r.denom * ↑q.denom))
(multiplicity ↑p (↑q.denom * r.num * ↑q.denom)) : (le_min
(by rw [@multiplicity.mul _ _ _ _ (_ * _) _ (nat.prime_iff_prime_int.1 hp.1), add_comm])
(by rw [mul_assoc, @multiplicity.mul _ _ _ _ (q.denom : ℤ)
(_ * _) (nat.prime_iff_prime_int.1 hp.1)]; exact add_le_add_left h _))
... ≤ _ : min_le_multiplicity_add,
all_goals { exact hp }
end
/-- The minimum of the valuations of `q` and `r` is at most the valuation of `q + r`. -/
theorem min_le_padic_val_rat_add {q r : ℚ} (hqr : q + r ≠ 0) :
min (padic_val_rat p q) (padic_val_rat p r) ≤ padic_val_rat p (q + r) :=
(le_total (padic_val_rat p q) (padic_val_rat p r)).elim
(λ h, by rw [min_eq_left h]; exact le_padic_val_rat_add_of_le hqr h)
(λ h, by rw [min_eq_right h, add_comm]; exact le_padic_val_rat_add_of_le (by rwa add_comm) h)
open_locale big_operators
/-- A finite sum of rationals with positive `p`-adic valuation has positive `p`-adic valuation
(if the sum is non-zero). -/
theorem sum_pos_of_pos {n : ℕ} {F : ℕ → ℚ} (hF : ∀ i, i < n → 0 < padic_val_rat p (F i))
(hn0 : ∑ i in finset.range n, F i ≠ 0) : 0 < padic_val_rat p (∑ i in finset.range n, F i) :=
begin
induction n with d hd,
{ exact false.elim (hn0 rfl) },
{ rw finset.sum_range_succ at hn0 ⊢,
by_cases h : ∑ (x : ℕ) in finset.range d, F x = 0,
{ rw [h, zero_add],
exact hF d (lt_add_one _) },
{ refine lt_of_lt_of_le _ (min_le_padic_val_rat_add hn0),
{ refine lt_min (hd (λ i hi, _) h) (hF d (lt_add_one _)),
exact hF _ (lt_trans hi (lt_add_one _)) } } }
end
end padic_val_rat
namespace padic_val_nat
variables {p a b : ℕ} [hp : fact p.prime]
include hp
/-- A rewrite lemma for `padic_val_nat p (a * b)` with conditions `a ≠ 0`, `b ≠ 0`. -/
protected lemma mul : a ≠ 0 → b ≠ 0 →
padic_val_nat p (a * b) = padic_val_nat p a + padic_val_nat p b :=
by exact_mod_cast @padic_val_rat.mul p _ a b
protected lemma div_of_dvd (h : b ∣ a) :
padic_val_nat p (a / b) = padic_val_nat p a - padic_val_nat p b :=
begin
rcases eq_or_ne a 0 with rfl | ha,
{ simp },
obtain ⟨k, rfl⟩ := h,
obtain ⟨hb, hk⟩ := mul_ne_zero_iff.mp ha,
rw [mul_comm, k.mul_div_cancel hb.bot_lt, padic_val_nat.mul hk hb, nat.add_sub_cancel],
exact hp
end
/-- Dividing out by a prime factor reduces the `padic_val_nat` by `1`. -/
protected lemma div (dvd : p ∣ b) : padic_val_nat p (b / p) = (padic_val_nat p b) - 1 :=
begin
convert padic_val_nat.div_of_dvd dvd,
rw padic_val_nat_self,
exact hp
end
/-- A version of `padic_val_rat.pow` for `padic_val_nat`. -/
protected lemma pow (n : ℕ) (ha : a ≠ 0) :
padic_val_nat p (a ^ n) = n * padic_val_nat p a :=
by simpa only [← @nat.cast_inj ℤ] with push_cast using padic_val_rat.pow (cast_ne_zero.mpr ha)
@[simp] protected lemma prime_pow (n : ℕ) : padic_val_nat p (p ^ n) = n :=
by rwa [padic_val_nat.pow _ (fact.out p.prime).ne_zero, padic_val_nat_self, mul_one]
protected lemma div_pow (dvd : p ^ a ∣ b) : padic_val_nat p (b / p ^ a) = (padic_val_nat p b) - a :=
begin
rw [padic_val_nat.div_of_dvd dvd, padic_val_nat.prime_pow],
exact hp
end
protected lemma div' {m : ℕ} (cpm : coprime p m) {b : ℕ} (dvd : m ∣ b) :
padic_val_nat p (b / m) = padic_val_nat p b :=
by rw [padic_val_nat.div_of_dvd dvd, eq_zero_of_not_dvd (hp.out.coprime_iff_not_dvd.mp cpm),
nat.sub_zero]; assumption
end padic_val_nat
section padic_val_nat
variables {p : ℕ}
lemma dvd_of_one_le_padic_val_nat {n : ℕ} (hp : 1 ≤ padic_val_nat p n) : p ∣ n :=
begin
by_contra h,
rw padic_val_nat.eq_zero_of_not_dvd h at hp,
exact lt_irrefl 0 (lt_of_lt_of_le zero_lt_one hp)
end
lemma pow_padic_val_nat_dvd {n : ℕ} : p ^ padic_val_nat p n ∣ n :=
begin
rcases n.eq_zero_or_pos with rfl | hn, { simp },
rcases eq_or_ne p 1 with rfl | hp, { simp },
rw [multiplicity.pow_dvd_iff_le_multiplicity, padic_val_nat_def']; assumption
end
lemma padic_val_nat_dvd_iff_le [hp : fact p.prime] {a n : ℕ} (ha : a ≠ 0) :
p ^ n ∣ a ↔ n ≤ padic_val_nat p a :=
by rw [pow_dvd_iff_le_multiplicity, ← padic_val_nat_def' hp.out.ne_one ha.bot_lt,
part_enat.coe_le_coe]
lemma padic_val_nat_dvd_iff (n : ℕ) [hp : fact p.prime] (a : ℕ) :
p ^ n ∣ a ↔ a = 0 ∨ n ≤ padic_val_nat p a :=
begin
rcases eq_or_ne a 0 with rfl | ha,
{ exact iff_of_true (dvd_zero _) (or.inl rfl) },
{ simp only [ha, false_or, padic_val_nat_dvd_iff_le ha] }
end
lemma pow_succ_padic_val_nat_not_dvd {n : ℕ} [hp : fact p.prime] (hn : n ≠ 0) :
¬ p ^ (padic_val_nat p n + 1) ∣ n :=
begin
rw [padic_val_nat_dvd_iff_le hn, not_le],
exacts [nat.lt_succ_self _, hp]
end
lemma padic_val_nat_primes {q : ℕ} [hp : fact p.prime] [hq : fact q.prime] (neq : p ≠ q) :
padic_val_nat p q = 0 :=
@padic_val_nat.eq_zero_of_not_dvd p q $
(not_congr (iff.symm (prime_dvd_prime_iff_eq hp.1 hq.1))).mp neq
open_locale big_operators
lemma range_pow_padic_val_nat_subset_divisors {n : ℕ} (hn : n ≠ 0) :
(finset.range (padic_val_nat p n + 1)).image (pow p) ⊆ n.divisors :=
begin
intros t ht,
simp only [exists_prop, finset.mem_image, finset.mem_range] at ht,
obtain ⟨k, hk, rfl⟩ := ht,
rw nat.mem_divisors,
exact ⟨(pow_dvd_pow p $ by linarith).trans pow_padic_val_nat_dvd, hn⟩
end
lemma range_pow_padic_val_nat_subset_divisors' {n : ℕ} [hp : fact p.prime] :
(finset.range (padic_val_nat p n)).image (λ t, p ^ (t + 1)) ⊆ n.divisors.erase 1 :=
begin
rcases eq_or_ne n 0 with rfl | hn,
{ simp },
intros t ht,
simp only [exists_prop, finset.mem_image, finset.mem_range] at ht,
obtain ⟨k, hk, rfl⟩ := ht,
rw [finset.mem_erase, nat.mem_divisors],
refine ⟨_, (pow_dvd_pow p $ succ_le_iff.2 hk).trans pow_padic_val_nat_dvd, hn⟩,
exact (nat.one_lt_pow _ _ k.succ_pos hp.out.one_lt).ne'
end
end padic_val_nat
section padic_val_int
variables {p : ℕ} [hp : fact p.prime]
include hp
lemma padic_val_int_dvd_iff (n : ℕ) (a : ℤ) : (p : ℤ) ^ n ∣ a ↔ a = 0 ∨ n ≤ padic_val_int p a :=
by rw [padic_val_int, ← int.nat_abs_eq_zero, ← padic_val_nat_dvd_iff, ← int.coe_nat_dvd_left,
int.coe_nat_pow]
lemma padic_val_int_dvd (a : ℤ) : (p : ℤ) ^ padic_val_int p a ∣ a :=
begin
rw padic_val_int_dvd_iff,
exact or.inr le_rfl
end
lemma padic_val_int_self : padic_val_int p p = 1 := padic_val_int.self hp.out.one_lt
lemma padic_val_int.mul {a b : ℤ} (ha : a ≠ 0) (hb : b ≠ 0) :
padic_val_int p (a * b) = padic_val_int p a + padic_val_int p b :=
begin
simp_rw padic_val_int,
rw [int.nat_abs_mul, padic_val_nat.mul];
rwa int.nat_abs_ne_zero
end
lemma padic_val_int_mul_eq_succ (a : ℤ) (ha : a ≠ 0) :
padic_val_int p (a * p) = (padic_val_int p a) + 1 :=
begin
rw padic_val_int.mul ha (int.coe_nat_ne_zero.mpr hp.out.ne_zero),
simp only [eq_self_iff_true, padic_val_int.of_nat, padic_val_nat_self],
exact hp
end
end padic_val_int
|
47ef0b1d74a422f7e559c3390044385d221cad5b | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/analysis/locally_convex/polar.lean | aa00e7a61709fdb30cf40770db8d9ec8a95629b8 | [
"Apache-2.0"
] | permissive | jcommelin/mathlib | d8456447c36c176e14d96d9e76f39841f69d2d9b | ee8279351a2e434c2852345c51b728d22af5a156 | refs/heads/master | 1,664,782,136,488 | 1,663,638,983,000 | 1,663,638,983,000 | 132,563,656 | 0 | 0 | Apache-2.0 | 1,663,599,929,000 | 1,525,760,539,000 | Lean | UTF-8 | Lean | false | false | 4,697 | lean | /-
Copyright (c) 2022 Moritz Doll. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Moritz Doll, Kalle Kytölä
-/
import analysis.normed.field.basic
import analysis.convex.basic
import linear_algebra.sesquilinear_form
import topology.algebra.module.weak_dual
/-!
# Polar set
In this file we define the polar set. There are different notions of the polar, we will define the
*absolute polar*. The advantage over the real polar is that we can define the absolute polar for
any bilinear form `B : E →ₗ[𝕜] F →ₗ[𝕜] 𝕜`, where `𝕜` is a normed commutative ring and
`E` and `F` are modules over `𝕜`.
## Main definitions
* `linear_map.polar`: The polar of a bilinear form `B : E →ₗ[𝕜] F →ₗ[𝕜] 𝕜`.
## Main statements
* `linear_map.polar_eq_Inter`: The polar as an intersection.
* `linear_map.subset_bipolar`: The polar is a subset of the bipolar.
* `linear_map.polar_weak_closed`: The polar is closed in the weak topology induced by `B.flip`.
## References
* [H. H. Schaefer, *Topological Vector Spaces*][schaefer1966]
## Tags
polar
-/
variables {𝕜 E F : Type*}
namespace linear_map
section normed_ring
variables [normed_comm_ring 𝕜] [add_comm_monoid E] [add_comm_monoid F]
variables [module 𝕜 E] [module 𝕜 F]
variables (B : E →ₗ[𝕜] F →ₗ[𝕜] 𝕜)
/-- The (absolute) polar of `s : set E` is given by the set of all `y : F` such that `∥B x y∥ ≤ 1`
for all `x ∈ s`.-/
def polar (s : set E) : set F :=
{y : F | ∀ x ∈ s, ∥B x y∥ ≤ 1 }
lemma polar_mem_iff (s : set E) (y : F) :
y ∈ B.polar s ↔ ∀ x ∈ s, ∥B x y∥ ≤ 1 := iff.rfl
lemma polar_mem (s : set E) (y : F) (hy : y ∈ B.polar s) :
∀ x ∈ s, ∥B x y∥ ≤ 1 := hy
@[simp] lemma zero_mem_polar (s : set E) :
(0 : F) ∈ B.polar s :=
λ _ _, by simp only [map_zero, norm_zero, zero_le_one]
lemma polar_eq_Inter {s : set E} :
B.polar s = ⋂ x ∈ s, {y : F | ∥B x y∥ ≤ 1} :=
by { ext, simp only [polar_mem_iff, set.mem_Inter, set.mem_set_of_eq] }
/-- The map `B.polar : set E → set F` forms an order-reversing Galois connection with
`B.flip.polar : set F → set E`. We use `order_dual.to_dual` and `order_dual.of_dual` to express
that `polar` is order-reversing. -/
lemma polar_gc : galois_connection (order_dual.to_dual ∘ B.polar)
(B.flip.polar ∘ order_dual.of_dual) :=
λ s t, ⟨λ h _ hx _ hy, h hy _ hx, λ h _ hx _ hy, h hy _ hx⟩
@[simp] lemma polar_Union {ι} {s : ι → set E} : B.polar (⋃ i, s i) = ⋂ i, B.polar (s i) :=
B.polar_gc.l_supr
@[simp] lemma polar_union {s t : set E} : B.polar (s ∪ t) = B.polar s ∩ B.polar t :=
B.polar_gc.l_sup
lemma polar_antitone : antitone (B.polar : set E → set F) := B.polar_gc.monotone_l
@[simp] lemma polar_empty : B.polar ∅ = set.univ := B.polar_gc.l_bot
@[simp] lemma polar_zero : B.polar ({0} : set E) = set.univ :=
begin
refine set.eq_univ_iff_forall.mpr (λ y x hx, _),
rw [set.mem_singleton_iff.mp hx, map_zero, linear_map.zero_apply, norm_zero],
exact zero_le_one,
end
lemma subset_bipolar (s : set E) : s ⊆ B.flip.polar (B.polar s) :=
λ x hx y hy, by { rw B.flip_apply, exact hy x hx }
@[simp] lemma tripolar_eq_polar (s : set E) : B.polar (B.flip.polar (B.polar s)) = B.polar s :=
begin
refine (B.polar_antitone (B.subset_bipolar s)).antisymm _,
convert subset_bipolar B.flip (B.polar s),
exact B.flip_flip.symm,
end
/-- The polar set is closed in the weak topology induced by `B.flip`. -/
lemma polar_weak_closed (s : set E) :
@is_closed _ (weak_bilin.topological_space B.flip) (B.polar s) :=
begin
rw polar_eq_Inter,
refine is_closed_Inter (λ x, is_closed_Inter (λ _, _)),
exact is_closed_le (weak_bilin.eval_continuous B.flip x).norm continuous_const,
end
end normed_ring
section nontrivially_normed_field
variables [nontrivially_normed_field 𝕜] [add_comm_monoid E] [add_comm_monoid F]
variables [module 𝕜 E] [module 𝕜 F]
variables (B : E →ₗ[𝕜] F →ₗ[𝕜] 𝕜)
lemma polar_univ (h : separating_right B) :
B.polar set.univ = {(0 : F)} :=
begin
rw set.eq_singleton_iff_unique_mem,
refine ⟨by simp only [zero_mem_polar], λ y hy, h _ (λ x, _)⟩,
refine norm_le_zero_iff.mp (le_of_forall_le_of_dense $ λ ε hε, _),
rcases normed_field.exists_norm_lt 𝕜 hε with ⟨c, hc, hcε⟩,
calc ∥B x y∥ = ∥c∥ * ∥B (c⁻¹ • x) y∥ :
by rw [B.map_smul, linear_map.smul_apply, algebra.id.smul_eq_mul, norm_mul, norm_inv,
mul_inv_cancel_left₀ hc.ne']
... ≤ ε * 1 : mul_le_mul hcε.le (hy _ trivial) (norm_nonneg _) hε.le
... = ε : mul_one _
end
end nontrivially_normed_field
end linear_map
|
940407f5f22eded47f3af1411555cfd7d17c8823 | 2eab05920d6eeb06665e1a6df77b3157354316ad | /src/linear_algebra/finsupp_vector_space.lean | 31d7501dd0e241939e409afd2f8be5d6e79c7742 | [
"Apache-2.0"
] | permissive | ayush1801/mathlib | 78949b9f789f488148142221606bf15c02b960d2 | ce164e28f262acbb3de6281b3b03660a9f744e3c | refs/heads/master | 1,692,886,907,941 | 1,635,270,866,000 | 1,635,270,866,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 7,673 | 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 data.mv_polynomial
import linear_algebra.dimension
import linear_algebra.direct_sum.finsupp
import linear_algebra.finite_dimensional
import linear_algebra.std_basis
/-!
# Linear structures on function with finite support `ι →₀ M`
This file contains results on the `R`-module structure on functions of finite support from a type
`ι` to an `R`-module `M`, in particular in the case that `R` is a field.
Furthermore, it contains some facts about isomorphisms of vector spaces from equality of dimension
as well as the cardinality of finite dimensional vector spaces.
## TODO
Move the second half of this file to more appropriate other files.
-/
noncomputable theory
local attribute [instance, priority 100] classical.prop_decidable
open set linear_map submodule
open_locale cardinal
universes u v w
namespace finsupp
section ring
variables {R : Type*} {M : Type*} {ι : Type*}
variables [ring R] [add_comm_group M] [module R M]
lemma linear_independent_single {φ : ι → Type*}
{f : Π ι, φ ι → M} (hf : ∀i, linear_independent R (f i)) :
linear_independent R (λ ix : Σ i, φ i, single ix.1 (f ix.1 ix.2)) :=
begin
apply @linear_independent_Union_finite R _ _ _ _ ι φ (λ i x, single i (f i x)),
{ assume i,
have h_disjoint : disjoint (span R (range (f i))) (ker (lsingle i)),
{ rw ker_lsingle,
exact disjoint_bot_right },
apply (hf i).map h_disjoint },
{ intros i t ht hit,
refine (disjoint_lsingle_lsingle {i} t (disjoint_singleton_left.2 hit)).mono _ _,
{ rw span_le,
simp only [supr_singleton],
rw range_coe,
apply range_comp_subset_range },
{ refine supr_le_supr (λ i, supr_le_supr _),
intros hi,
rw span_le,
rw range_coe,
apply range_comp_subset_range } }
end
open linear_map submodule
/-- The basis on `ι →₀ M` with basis vectors `λ ⟨i, x⟩, single i (b i x)`. -/
protected def basis {φ : ι → Type*} (b : ∀ i, basis (φ i) R M) :
basis (Σ i, φ i) R (ι →₀ M) :=
basis.of_repr
{ to_fun := λ g,
{ to_fun := λ ix, (b ix.1).repr (g ix.1) ix.2,
support := g.support.sigma (λ i, ((b i).repr (g i)).support),
mem_support_to_fun := λ ix,
by { simp only [finset.mem_sigma, mem_support_iff, and_iff_right_iff_imp, ne.def],
intros b hg,
simpa [hg] using b } },
inv_fun := λ g,
{ to_fun := λ i, (b i).repr.symm (g.comap_domain _
(set.inj_on_of_injective sigma_mk_injective _)),
support := g.support.image sigma.fst,
mem_support_to_fun := λ i,
by { rw [ne.def, ← (b i).repr.injective.eq_iff, (b i).repr.apply_symm_apply, ext_iff],
simp only [exists_prop, linear_equiv.map_zero, comap_domain_apply, zero_apply,
exists_and_distrib_right, mem_support_iff, exists_eq_right, sigma.exists,
finset.mem_image, not_forall] } },
left_inv := λ g,
by { ext i, rw ← (b i).repr.injective.eq_iff, ext x,
simp only [coe_mk, linear_equiv.apply_symm_apply, comap_domain_apply] },
right_inv := λ g,
by { ext ⟨i, x⟩,
simp only [coe_mk, linear_equiv.apply_symm_apply, comap_domain_apply] },
map_add' := λ g h, by { ext ⟨i, x⟩, simp only [coe_mk, add_apply, linear_equiv.map_add] },
map_smul' := λ c h, by { ext ⟨i, x⟩, simp only [coe_mk, smul_apply, linear_equiv.map_smul,
ring_hom.id_apply] } }
@[simp] lemma basis_repr {φ : ι → Type*} (b : ∀ i, basis (φ i) R M)
(g : ι →₀ M) (ix) :
(finsupp.basis b).repr g ix = (b ix.1).repr (g ix.1) ix.2 :=
rfl
@[simp] lemma coe_basis {φ : ι → Type*} (b : ∀ i, basis (φ i) R M) :
⇑(finsupp.basis b) = λ (ix : Σ i, φ i), single ix.1 (b ix.1 ix.2) :=
funext $ λ ⟨i, x⟩, basis.apply_eq_iff.mpr $
begin
ext ⟨j, y⟩,
by_cases h : i = j,
{ cases h,
simp only [basis_repr, single_eq_same, basis.repr_self,
basis.finsupp.single_apply_left sigma_mk_injective] },
simp only [basis_repr, single_apply, h, false_and, if_false, linear_equiv.map_zero, zero_apply]
end
/-- The basis on `ι →₀ M` with basis vectors `λ i, single i 1`. -/
@[simps]
protected def basis_single_one :
basis ι R (ι →₀ R) :=
basis.of_repr (linear_equiv.refl _ _)
@[simp] lemma coe_basis_single_one :
(finsupp.basis_single_one : ι → (ι →₀ R)) = λ i, finsupp.single i 1 :=
funext $ λ i, basis.apply_eq_iff.mpr rfl
end ring
section dim
variables {K : Type u} {V : Type v} {ι : Type v}
variables [field K] [add_comm_group V] [module K V]
lemma dim_eq : module.rank K (ι →₀ V) = #ι * module.rank K V :=
begin
let bs := basis.of_vector_space K V,
rw [← cardinal.lift_inj, cardinal.lift_mul, ← bs.mk_eq_dim,
← (finsupp.basis (λa:ι, bs)).mk_eq_dim, ← cardinal.sum_mk,
← cardinal.lift_mul, cardinal.lift_inj],
{ simp only [cardinal.mk_image_eq (single_injective.{u u} _), cardinal.sum_const] }
end
end dim
end finsupp
section module
variables {K : Type u} {V V₁ V₂ : Type v} {V' : Type w}
variables [field K]
variables [add_comm_group V] [module K V]
variables [add_comm_group V₁] [module K V₁]
variables [add_comm_group V₂] [module K V₂]
variables [add_comm_group V'] [module K V']
open module
lemma equiv_of_dim_eq_lift_dim
(h : cardinal.lift.{w} (module.rank K V) = cardinal.lift.{v} (module.rank K V')) :
nonempty (V ≃ₗ[K] V') :=
begin
haveI := classical.dec_eq V,
haveI := classical.dec_eq V',
let m := basis.of_vector_space K V,
let m' := basis.of_vector_space K V',
rw [←cardinal.lift_inj.1 m.mk_eq_dim, ←cardinal.lift_inj.1 m'.mk_eq_dim] at h,
rcases quotient.exact h with ⟨e⟩,
let e := (equiv.ulift.symm.trans e).trans equiv.ulift,
exact ⟨(m.repr ≪≫ₗ (finsupp.dom_lcongr e)) ≪≫ₗ m'.repr.symm⟩
end
/-- Two `K`-vector spaces are equivalent if their dimension is the same. -/
def equiv_of_dim_eq_dim (h : module.rank K V₁ = module.rank K V₂) : V₁ ≃ₗ[K] V₂ :=
begin
classical,
exact classical.choice (equiv_of_dim_eq_lift_dim (cardinal.lift_inj.2 h))
end
/-- An `n`-dimensional `K`-vector space is equivalent to `fin n → K`. -/
def fin_dim_vectorspace_equiv (n : ℕ)
(hn : (module.rank K V) = n) : V ≃ₗ[K] (fin n → K) :=
begin
have : cardinal.lift.{u} (n : cardinal.{v}) = cardinal.lift.{v} (n : cardinal.{u}),
by simp,
have hn := cardinal.lift_inj.{v u}.2 hn,
rw this at hn,
rw ←@dim_fin_fun K _ n at hn,
exact classical.choice (equiv_of_dim_eq_lift_dim hn),
end
end module
section module
open module
variables (K V : Type u) [field K] [add_comm_group V] [module K V]
lemma cardinal_mk_eq_cardinal_mk_field_pow_dim [finite_dimensional K V] :
#V = #K ^ module.rank K V :=
begin
let s := basis.of_vector_space_index K V,
let hs := basis.of_vector_space K V,
calc #V = #(s →₀ K) : quotient.sound ⟨hs.repr.to_equiv⟩
... = #(s → K) : quotient.sound ⟨finsupp.equiv_fun_on_fintype⟩
... = _ : by rw [← cardinal.lift_inj.1 hs.mk_eq_dim, cardinal.power_def]
end
lemma cardinal_lt_omega_of_finite_dimensional [fintype K] [finite_dimensional K V] :
#V < ω :=
begin
letI : is_noetherian K V := is_noetherian.iff_fg.2 infer_instance,
rw cardinal_mk_eq_cardinal_mk_field_pow_dim K V,
exact cardinal.power_lt_omega (cardinal.lt_omega_iff_fintype.2 ⟨infer_instance⟩)
(is_noetherian.dim_lt_omega K V),
end
end module
|
4f019ece1ddb54cebebc8e805f91b90f596c5826 | 9028d228ac200bbefe3a711342514dd4e4458bff | /src/set_theory/cardinal.lean | 7fd0872b4ddf2fecffbb99642538a5c56d48eff7 | [
"Apache-2.0"
] | permissive | mcncm/mathlib | 8d25099344d9d2bee62822cb9ed43aa3e09fa05e | fde3d78cadeec5ef827b16ae55664ef115e66f57 | refs/heads/master | 1,672,743,316,277 | 1,602,618,514,000 | 1,602,618,514,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 46,816 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Johannes Hölzl, Mario Carneiro
-/
import data.set.countable
import set_theory.schroeder_bernstein
import data.fintype.card
/-!
# Cardinal Numbers
We define cardinal numbers as a quotient of types under the equivalence relation of equinumerity.
We define the order on cardinal numbers, define omega, and do basic cardinal arithmetic:
addition, multiplication, power, cardinal successor, minimum, supremum,
infinitary sums and products
The fact that the cardinality of `α × α` coincides with that of `α` when `α` is infinite is not
proved in this file, as it relies on facts on well-orders. Instead, it is in
`cardinal_ordinal.lean` (together with many other facts on cardinals, for instance the
cardinality of `list α`).
## Implementation notes
* There is a type of cardinal numbers in every universe level: `cardinal.{u} : Type (u + 1)`
is the quotient of types in `Type u`.
There is a lift operation lifting cardinal numbers to a higher level.
* Cardinal arithmetic specifically for infinite cardinals (like `κ * κ = κ`) is in the file
`set_theory/ordinal.lean`, because concepts from that file are used in the proof.
## References
* <https://en.wikipedia.org/wiki/Cardinal_number>
## Tags
cardinal number, cardinal arithmetic, cardinal exponentiation, omega
-/
open function set
open_locale classical
universes u v w x
variables {α β : Type u}
/-- The equivalence relation on types given by equivalence (bijective correspondence) of types.
Quotienting by this equivalence relation gives the cardinal numbers.
-/
instance cardinal.is_equivalent : setoid (Type u) :=
{ r := λα β, nonempty (α ≃ β),
iseqv := ⟨λα,
⟨equiv.refl α⟩,
λα β ⟨e⟩, ⟨e.symm⟩,
λα β γ ⟨e₁⟩ ⟨e₂⟩, ⟨e₁.trans e₂⟩⟩ }
/-- `cardinal.{u}` is the type of cardinal numbers in `Type u`,
defined as the quotient of `Type u` by existence of an equivalence
(a bijection with explicit inverse). -/
def cardinal : Type (u + 1) := quotient cardinal.is_equivalent
namespace cardinal
/-- The cardinal number of a type -/
def mk : Type u → cardinal := quotient.mk
localized "notation `#` := cardinal.mk" in cardinal
protected lemma eq : mk α = mk β ↔ nonempty (α ≃ β) := quotient.eq
@[simp] theorem mk_def (α : Type u) : @eq cardinal ⟦α⟧ (mk α) := rfl
@[simp] theorem mk_out (c : cardinal) : mk (c.out) = c := quotient.out_eq _
/-- We define the order on cardinal numbers by `mk α ≤ mk β` if and only if
there exists an embedding (injective function) from α to β. -/
instance : has_le cardinal.{u} :=
⟨λq₁ q₂, quotient.lift_on₂ q₁ q₂ (λα β, nonempty $ α ↪ β) $
assume α β γ δ ⟨e₁⟩ ⟨e₂⟩,
propext ⟨assume ⟨e⟩, ⟨e.congr e₁ e₂⟩, assume ⟨e⟩, ⟨e.congr e₁.symm e₂.symm⟩⟩⟩
theorem mk_le_of_injective {α β : Type u} {f : α → β} (hf : injective f) : mk α ≤ mk β :=
⟨⟨f, hf⟩⟩
theorem mk_le_of_surjective {α β : Type u} {f : α → β} (hf : surjective f) : mk β ≤ mk α :=
⟨embedding.of_surjective f hf⟩
theorem le_mk_iff_exists_set {c : cardinal} {α : Type u} :
c ≤ mk α ↔ ∃ p : set α, mk p = c :=
⟨quotient.induction_on c $ λ β ⟨⟨f, hf⟩⟩,
⟨set.range f, eq.symm $ quot.sound ⟨equiv.set.range f hf⟩⟩,
λ ⟨p, e⟩, e ▸ ⟨⟨subtype.val, λ a b, subtype.eq⟩⟩⟩
theorem out_embedding {c c' : cardinal} : c ≤ c' ↔ nonempty (c.out ↪ c'.out) :=
by { transitivity _, rw [←quotient.out_eq c, ←quotient.out_eq c'], refl }
instance : linear_order cardinal.{u} :=
{ le := (≤),
le_refl := by rintros ⟨α⟩; exact ⟨embedding.refl _⟩,
le_trans := by rintros ⟨α⟩ ⟨β⟩ ⟨γ⟩ ⟨e₁⟩ ⟨e₂⟩; exact ⟨e₁.trans e₂⟩,
le_antisymm := by rintros ⟨α⟩ ⟨β⟩ ⟨e₁⟩ ⟨e₂⟩; exact quotient.sound (e₁.antisymm e₂),
le_total := by rintros ⟨α⟩ ⟨β⟩; exact embedding.total }
noncomputable instance : decidable_linear_order cardinal.{u} := classical.DLO _
noncomputable instance : distrib_lattice cardinal.{u} := by apply_instance -- short-circuit type class inference
instance : has_zero cardinal.{u} := ⟨⟦pempty⟧⟩
instance : inhabited cardinal.{u} := ⟨0⟩
theorem ne_zero_iff_nonempty {α : Type u} : mk α ≠ 0 ↔ nonempty α :=
not_iff_comm.1
⟨λ h, quotient.sound ⟨(equiv.empty_of_not_nonempty h).trans equiv.empty_equiv_pempty⟩,
λ e, let ⟨h⟩ := quotient.exact e in λ ⟨a⟩, (h a).elim⟩
instance : has_one cardinal.{u} := ⟨⟦punit⟧⟩
instance : nontrivial cardinal.{u} :=
⟨⟨1, 0, ne_zero_iff_nonempty.2 ⟨punit.star⟩⟩⟩
theorem le_one_iff_subsingleton {α : Type u} : mk α ≤ 1 ↔ subsingleton α :=
⟨λ ⟨f⟩, ⟨λ a b, f.injective (subsingleton.elim _ _)⟩,
λ ⟨h⟩, ⟨⟨λ a, punit.star, λ a b _, h _ _⟩⟩⟩
theorem one_lt_iff_nontrivial {α : Type u} : 1 < mk α ↔ nontrivial α :=
by { rw [← not_iff_not, not_nontrivial_iff_subsingleton, ← le_one_iff_subsingleton], simp }
instance : has_add cardinal.{u} :=
⟨λq₁ q₂, quotient.lift_on₂ q₁ q₂ (λα β, mk (α ⊕ β)) $ assume α β γ δ ⟨e₁⟩ ⟨e₂⟩,
quotient.sound ⟨equiv.sum_congr e₁ e₂⟩⟩
@[simp] theorem add_def (α β) : mk α + mk β = mk (α ⊕ β) := rfl
instance : has_mul cardinal.{u} :=
⟨λq₁ q₂, quotient.lift_on₂ q₁ q₂ (λα β, mk (α × β)) $ assume α β γ δ ⟨e₁⟩ ⟨e₂⟩,
quotient.sound ⟨equiv.prod_congr e₁ e₂⟩⟩
@[simp] theorem mul_def (α β : Type u) : mk α * mk β = mk (α × β) := rfl
private theorem add_comm (a b : cardinal.{u}) : a + b = b + a :=
quotient.induction_on₂ a b $ assume α β, quotient.sound ⟨equiv.sum_comm α β⟩
private theorem mul_comm (a b : cardinal.{u}) : a * b = b * a :=
quotient.induction_on₂ a b $ assume α β, quotient.sound ⟨equiv.prod_comm α β⟩
private theorem zero_add (a : cardinal.{u}) : 0 + a = a :=
quotient.induction_on a $ assume α, quotient.sound ⟨equiv.pempty_sum α⟩
private theorem zero_mul (a : cardinal.{u}) : 0 * a = 0 :=
quotient.induction_on a $ assume α, quotient.sound ⟨equiv.pempty_prod α⟩
private theorem one_mul (a : cardinal.{u}) : 1 * a = a :=
quotient.induction_on a $ assume α, quotient.sound ⟨equiv.punit_prod α⟩
private theorem left_distrib (a b c : cardinal.{u}) : a * (b + c) = a * b + a * c :=
quotient.induction_on₃ a b c $ assume α β γ, quotient.sound ⟨equiv.prod_sum_distrib α β γ⟩
instance : comm_semiring cardinal.{u} :=
{ zero := 0,
one := 1,
add := (+),
mul := (*),
zero_add := zero_add,
add_zero := assume a, by rw [add_comm a 0, zero_add a],
add_assoc := λa b c, quotient.induction_on₃ a b c $ assume α β γ,
quotient.sound ⟨equiv.sum_assoc α β γ⟩,
add_comm := add_comm,
zero_mul := zero_mul,
mul_zero := assume a, by rw [mul_comm a 0, zero_mul a],
one_mul := one_mul,
mul_one := assume a, by rw [mul_comm a 1, one_mul a],
mul_assoc := λa b c, quotient.induction_on₃ a b c $ assume α β γ,
quotient.sound ⟨equiv.prod_assoc α β γ⟩,
mul_comm := mul_comm,
left_distrib := left_distrib,
right_distrib := assume a b c,
by rw [mul_comm (a + b) c, left_distrib c a b, mul_comm c a, mul_comm c b] }
/-- The cardinal exponential. `mk α ^ mk β` is the cardinal of `β → α`. -/
protected def power (a b : cardinal.{u}) : cardinal.{u} :=
quotient.lift_on₂ a b (λα β, mk (β → α)) $ assume α₁ α₂ β₁ β₂ ⟨e₁⟩ ⟨e₂⟩,
quotient.sound ⟨equiv.arrow_congr e₂ e₁⟩
instance : has_pow cardinal cardinal := ⟨cardinal.power⟩
local infixr ^ := @has_pow.pow cardinal cardinal cardinal.has_pow
@[simp] theorem power_def (α β) : mk α ^ mk β = mk (β → α) := rfl
@[simp] theorem power_zero {a : cardinal} : a ^ 0 = 1 :=
quotient.induction_on a $ assume α, quotient.sound
⟨equiv.pempty_arrow_equiv_punit α⟩
@[simp] theorem power_one {a : cardinal} : a ^ 1 = a :=
quotient.induction_on a $ assume α, quotient.sound
⟨equiv.punit_arrow_equiv α⟩
@[simp] theorem one_power {a : cardinal} : 1 ^ a = 1 :=
quotient.induction_on a $ assume α, quotient.sound
⟨equiv.arrow_punit_equiv_punit α⟩
@[simp] theorem prop_eq_two : mk (ulift Prop) = 2 :=
quot.sound ⟨equiv.ulift.trans $ equiv.Prop_equiv_bool.trans equiv.bool_equiv_punit_sum_punit⟩
@[simp] theorem zero_power {a : cardinal} : a ≠ 0 → 0 ^ a = 0 :=
quotient.induction_on a $ assume α heq,
nonempty.rec_on (ne_zero_iff_nonempty.1 heq) $ assume a,
quotient.sound ⟨equiv.equiv_pempty $ assume f, pempty.rec (λ _, false) (f a)⟩
theorem power_ne_zero {a : cardinal} (b) : a ≠ 0 → a ^ b ≠ 0 :=
quotient.induction_on₂ a b $ λ α β h,
let ⟨a⟩ := ne_zero_iff_nonempty.1 h in
ne_zero_iff_nonempty.2 ⟨λ _, a⟩
theorem mul_power {a b c : cardinal} : (a * b) ^ c = a ^ c * b ^ c :=
quotient.induction_on₃ a b c $ assume α β γ,
quotient.sound ⟨equiv.arrow_prod_equiv_prod_arrow α β γ⟩
theorem power_add {a b c : cardinal} : a ^ (b + c) = a ^ b * a ^ c :=
quotient.induction_on₃ a b c $ assume α β γ,
quotient.sound ⟨equiv.sum_arrow_equiv_prod_arrow β γ α⟩
theorem power_mul {a b c : cardinal} : (a ^ b) ^ c = a ^ (b * c) :=
by rw [_root_.mul_comm b c];
from (quotient.induction_on₃ a b c $ assume α β γ,
quotient.sound ⟨equiv.arrow_arrow_equiv_prod_arrow γ β α⟩)
@[simp] lemma pow_cast_right (κ : cardinal.{u}) :
∀ n : ℕ, (κ ^ (↑n : cardinal.{u})) = @has_pow.pow _ _ monoid.has_pow κ n
| 0 := by simp
| (_+1) := by rw [nat.cast_succ, power_add, power_one, _root_.mul_comm, pow_succ, pow_cast_right]
section order_properties
open sum
theorem zero_le : ∀(a : cardinal), 0 ≤ a :=
by rintro ⟨α⟩; exact ⟨embedding.of_not_nonempty $ λ ⟨a⟩, a.elim⟩
theorem le_zero (a : cardinal) : a ≤ 0 ↔ a = 0 :=
by simp [le_antisymm_iff, zero_le]
theorem pos_iff_ne_zero {o : cardinal} : 0 < o ↔ o ≠ 0 :=
by simp [lt_iff_le_and_ne, eq_comm, zero_le]
@[simp] theorem zero_lt_one : (0 : cardinal) < 1 :=
lt_of_le_of_ne (zero_le _) zero_ne_one
lemma zero_power_le (c : cardinal.{u}) : (0 : cardinal.{u}) ^ c ≤ 1 :=
by { by_cases h : c = 0, rw [h, power_zero], rw [zero_power h], apply zero_le }
theorem add_le_add : ∀{a b c d : cardinal}, a ≤ b → c ≤ d → a + c ≤ b + d :=
by rintros ⟨α⟩ ⟨β⟩ ⟨γ⟩ ⟨δ⟩ ⟨e₁⟩ ⟨e₂⟩; exact ⟨e₁.sum_map e₂⟩
theorem add_le_add_left (a) {b c : cardinal} : b ≤ c → a + b ≤ a + c :=
add_le_add (le_refl _)
theorem add_le_add_right {a b : cardinal} (c) (h : a ≤ b) : a + c ≤ b + c :=
add_le_add h (le_refl _)
theorem le_add_right (a b : cardinal) : a ≤ a + b :=
by simpa using add_le_add_left a (zero_le b)
theorem le_add_left (a b : cardinal) : a ≤ b + a :=
by simpa using add_le_add_right a (zero_le b)
theorem mul_le_mul : ∀{a b c d : cardinal}, a ≤ b → c ≤ d → a * c ≤ b * d :=
by rintros ⟨α⟩ ⟨β⟩ ⟨γ⟩ ⟨δ⟩ ⟨e₁⟩ ⟨e₂⟩; exact ⟨e₁.prod_map e₂⟩
theorem mul_le_mul_left (a) {b c : cardinal} : b ≤ c → a * b ≤ a * c :=
mul_le_mul (le_refl _)
theorem mul_le_mul_right {a b : cardinal} (c) (h : a ≤ b) : a * c ≤ b * c :=
mul_le_mul h (le_refl _)
theorem power_le_power_left : ∀{a b c : cardinal}, a ≠ 0 → b ≤ c → a ^ b ≤ a ^ c :=
by rintros ⟨α⟩ ⟨β⟩ ⟨γ⟩ hα ⟨e⟩; exact
let ⟨a⟩ := ne_zero_iff_nonempty.1 hα in
⟨@embedding.arrow_congr_right _ _ _ ⟨a⟩ e⟩
theorem power_le_max_power_one {a b c : cardinal} (h : b ≤ c) : a ^ b ≤ max (a ^ c) 1 :=
begin
by_cases ha : a = 0,
simp [ha, zero_power_le],
exact le_trans (power_le_power_left ha h) (le_max_left _ _)
end
theorem power_le_power_right {a b c : cardinal} : a ≤ b → a ^ c ≤ b ^ c :=
quotient.induction_on₃ a b c $ assume α β γ ⟨e⟩, ⟨embedding.arrow_congr_left e⟩
theorem le_iff_exists_add {a b : cardinal} : a ≤ b ↔ ∃ c, b = a + c :=
⟨quotient.induction_on₂ a b $ λ α β ⟨⟨f, hf⟩⟩,
have (α ⊕ ((range f)ᶜ : set β)) ≃ β, from
(equiv.sum_congr (equiv.set.range f hf) (equiv.refl _)).trans $
(equiv.set.sum_compl (range f)),
⟨⟦↥(range f)ᶜ⟧, quotient.sound ⟨this.symm⟩⟩,
λ ⟨c, e⟩, add_zero a ▸ e.symm ▸ add_le_add_left _ (zero_le _)⟩
end order_properties
instance : order_bot cardinal.{u} :=
{ bot := 0, bot_le := zero_le, ..cardinal.linear_order }
instance : canonically_ordered_add_monoid cardinal.{u} :=
{ add_le_add_left := λ a b h c, add_le_add_left _ h,
lt_of_add_lt_add_left := λ a b c, lt_imp_lt_of_le_imp_le (add_le_add_left _),
le_iff_exists_add := @le_iff_exists_add,
..cardinal.order_bot,
..cardinal.comm_semiring, ..cardinal.linear_order }
theorem cantor : ∀(a : cardinal.{u}), a < 2 ^ a :=
by rw ← prop_eq_two; rintros ⟨a⟩; exact ⟨
⟨⟨λ a b, ⟨a = b⟩, λ a b h, cast (ulift.up.inj (@congr_fun _ _ _ _ h b)).symm rfl⟩⟩,
λ ⟨⟨f, hf⟩⟩, cantor_injective (λ s, f (λ a, ⟨s a⟩)) $
λ s t h, by funext a; injection congr_fun (hf h) a⟩
instance : no_top_order cardinal.{u} :=
{ no_top := λ a, ⟨_, cantor a⟩, ..cardinal.linear_order }
/-- The minimum cardinal in a family of cardinals (the existence
of which is provided by `injective_min`). -/
noncomputable def min {ι} (I : nonempty ι) (f : ι → cardinal) : cardinal :=
f $ classical.some $
@embedding.min_injective _ (λ i, (f i).out) I
theorem min_eq {ι} (I) (f : ι → cardinal) : ∃ i, min I f = f i :=
⟨_, rfl⟩
theorem min_le {ι I} (f : ι → cardinal) (i) : min I f ≤ f i :=
by rw [← mk_out (min I f), ← mk_out (f i)]; exact
let ⟨g⟩ := classical.some_spec
(@embedding.min_injective _ (λ i, (f i).out) I) in
⟨g i⟩
theorem le_min {ι I} {f : ι → cardinal} {a} : a ≤ min I f ↔ ∀ i, a ≤ f i :=
⟨λ h i, le_trans h (min_le _ _),
λ h, let ⟨i, e⟩ := min_eq I f in e.symm ▸ h i⟩
protected theorem wf : @well_founded cardinal.{u} (<) :=
⟨λ a, classical.by_contradiction $ λ h,
let ι := {c :cardinal // ¬ acc (<) c},
f : ι → cardinal := subtype.val,
⟨⟨c, hc⟩, hi⟩ := @min_eq ι ⟨⟨_, h⟩⟩ f in
hc (acc.intro _ (λ j ⟨_, h'⟩,
classical.by_contradiction $ λ hj, h' $
by have := min_le f ⟨j, hj⟩; rwa hi at this))⟩
instance has_wf : @has_well_founded cardinal.{u} := ⟨(<), cardinal.wf⟩
instance wo : @is_well_order cardinal.{u} (<) := ⟨cardinal.wf⟩
/-- The successor cardinal - the smallest cardinal greater than
`c`. This is not the same as `c + 1` except in the case of finite `c`. -/
noncomputable def succ (c : cardinal) : cardinal :=
@min {c' // c < c'} ⟨⟨_, cantor _⟩⟩ subtype.val
theorem lt_succ_self (c : cardinal) : c < succ c :=
by cases min_eq _ _ with s e; rw [succ, e]; exact s.2
theorem succ_le {a b : cardinal} : succ a ≤ b ↔ a < b :=
⟨lt_of_lt_of_le (lt_succ_self _), λ h,
by exact min_le _ (subtype.mk b h)⟩
theorem lt_succ {a b : cardinal} : a < succ b ↔ a ≤ b :=
by rw [← not_le, succ_le, not_lt]
theorem add_one_le_succ (c : cardinal) : c + 1 ≤ succ c :=
begin
refine quot.induction_on c (λ α, _) (lt_succ_self c),
refine quot.induction_on (succ (quot.mk setoid.r α)) (λ β h, _),
cases h.left with f,
have : ¬ surjective f := λ hn,
ne_of_lt h (quotient.sound ⟨equiv.of_bijective f ⟨f.injective, hn⟩⟩),
cases not_forall.1 this with b nex,
refine ⟨⟨sum.rec (by exact f) _, _⟩⟩,
{ exact λ _, b },
{ intros a b h, rcases a with a|⟨⟨⟨⟩⟩⟩; rcases b with b|⟨⟨⟨⟩⟩⟩,
{ rw f.injective h },
{ exact nex.elim ⟨_, h⟩ },
{ exact nex.elim ⟨_, h.symm⟩ },
{ refl } }
end
lemma succ_ne_zero (c : cardinal) : succ c ≠ 0 :=
by { rw [←pos_iff_ne_zero, lt_succ], apply zero_le }
/-- The indexed sum of cardinals is the cardinality of the
indexed disjoint union, i.e. sigma type. -/
def sum {ι} (f : ι → cardinal) : cardinal := mk Σ i, (f i).out
theorem le_sum {ι} (f : ι → cardinal) (i) : f i ≤ sum f :=
by rw ← quotient.out_eq (f i); exact
⟨⟨λ a, ⟨i, a⟩, λ a b h, eq_of_heq $ by injection h⟩⟩
@[simp] theorem sum_mk {ι} (f : ι → Type*) : sum (λ i, mk (f i)) = mk (Σ i, f i) :=
quot.sound ⟨equiv.sigma_congr_right $ λ i,
classical.choice $ quotient.exact $ quot.out_eq $ mk (f i)⟩
theorem sum_const (ι : Type u) (a : cardinal.{u}) : sum (λ _:ι, a) = mk ι * a :=
quotient.induction_on a $ λ α, by simp; exact
quotient.sound ⟨equiv.sigma_equiv_prod _ _⟩
theorem sum_le_sum {ι} (f g : ι → cardinal) (H : ∀ i, f i ≤ g i) : sum f ≤ sum g :=
⟨(embedding.refl _).sigma_map $ λ i, classical.choice $
by have := H i; rwa [← quot.out_eq (f i), ← quot.out_eq (g i)] at this⟩
/-- The indexed supremum of cardinals is the smallest cardinal above
everything in the family. -/
noncomputable def sup {ι} (f : ι → cardinal) : cardinal :=
@min {c // ∀ i, f i ≤ c} ⟨⟨sum f, le_sum f⟩⟩ (λ a, a.1)
theorem le_sup {ι} (f : ι → cardinal) (i) : f i ≤ sup f :=
by dsimp [sup]; cases min_eq _ _ with c hc; rw hc; exact c.2 i
theorem sup_le {ι} {f : ι → cardinal} {a} : sup f ≤ a ↔ ∀ i, f i ≤ a :=
⟨λ h i, le_trans (le_sup _ _) h,
λ h, by dsimp [sup]; change a with (⟨a, h⟩:subtype _).1; apply min_le⟩
theorem sup_le_sup {ι} (f g : ι → cardinal) (H : ∀ i, f i ≤ g i) : sup f ≤ sup g :=
sup_le.2 $ λ i, le_trans (H i) (le_sup _ _)
theorem sup_le_sum {ι} (f : ι → cardinal) : sup f ≤ sum f :=
sup_le.2 $ le_sum _
theorem sum_le_sup {ι : Type u} (f : ι → cardinal.{u}) : sum f ≤ mk ι * sup.{u u} f :=
by rw ← sum_const; exact sum_le_sum _ _ (le_sup _)
theorem sup_eq_zero {ι} {f : ι → cardinal} (h : ι → false) : sup f = 0 :=
by { rw [←le_zero, sup_le], intro x, exfalso, exact h x }
/-- The indexed product of cardinals is the cardinality of the Pi type
(dependent product). -/
def prod {ι : Type u} (f : ι → cardinal) : cardinal := mk (Π i, (f i).out)
@[simp] theorem prod_mk {ι} (f : ι → Type*) : prod (λ i, mk (f i)) = mk (Π i, f i) :=
quot.sound ⟨equiv.Pi_congr_right $ λ i,
classical.choice $ quotient.exact $ mk_out $ mk (f i)⟩
theorem prod_const (ι : Type u) (a : cardinal.{u}) : prod (λ _:ι, a) = a ^ mk ι :=
quotient.induction_on a $ by simp
theorem prod_le_prod {ι} (f g : ι → cardinal) (H : ∀ i, f i ≤ g i) : prod f ≤ prod g :=
⟨embedding.Pi_congr_right $ λ i, classical.choice $
by have := H i; rwa [← mk_out (f i), ← mk_out (g i)] at this⟩
theorem prod_ne_zero {ι} (f : ι → cardinal) : prod f ≠ 0 ↔ ∀ i, f i ≠ 0 :=
begin
conv in (f _) {rw ← mk_out (f i)},
simp [prod, ne_zero_iff_nonempty, -mk_out, -ne.def],
exact ⟨λ ⟨F⟩ i, ⟨F i⟩, λ h, ⟨λ i, classical.choice (h i)⟩⟩,
end
theorem prod_eq_zero {ι} (f : ι → cardinal) : prod f = 0 ↔ ∃ i, f i = 0 :=
not_iff_not.1 $ by simpa using prod_ne_zero f
/-- The universe lift operation on cardinals. You can specify the universes explicitly with
`lift.{u v} : cardinal.{u} → cardinal.{max u v}` -/
def lift (c : cardinal.{u}) : cardinal.{max u v} :=
quotient.lift_on c (λ α, ⟦ulift α⟧) $ λ α β ⟨e⟩,
quotient.sound ⟨equiv.ulift.trans $ e.trans equiv.ulift.symm⟩
theorem lift_mk (α) : lift.{u v} (mk α) = mk (ulift.{v u} α) := rfl
theorem lift_umax : lift.{u (max u v)} = lift.{u v} :=
funext $ λ a, quot.induction_on a $ λ α,
quotient.sound ⟨equiv.ulift.trans equiv.ulift.symm⟩
theorem lift_id' (a : cardinal) : lift a = a :=
quot.induction_on a $ λ α, quot.sound ⟨equiv.ulift⟩
@[simp] theorem lift_id : ∀ a, lift.{u u} a = a := lift_id'.{u u}
@[simp] theorem lift_lift (a : cardinal) : lift.{(max u v) w} (lift.{u v} a) = lift.{u (max v w)} a :=
quot.induction_on a $ λ α,
quotient.sound ⟨equiv.ulift.trans $ equiv.ulift.trans equiv.ulift.symm⟩
theorem lift_mk_le {α : Type u} {β : Type v} :
lift.{u (max v w)} (mk α) ≤ lift.{v (max u w)} (mk β) ↔ nonempty (α ↪ β) :=
⟨λ ⟨f⟩, ⟨embedding.congr equiv.ulift equiv.ulift f⟩,
λ ⟨f⟩, ⟨embedding.congr equiv.ulift.symm equiv.ulift.symm f⟩⟩
theorem lift_mk_eq {α : Type u} {β : Type v} :
lift.{u (max v w)} (mk α) = lift.{v (max u w)} (mk β) ↔ nonempty (α ≃ β) :=
quotient.eq.trans
⟨λ ⟨f⟩, ⟨equiv.ulift.symm.trans $ f.trans equiv.ulift⟩,
λ ⟨f⟩, ⟨equiv.ulift.trans $ f.trans equiv.ulift.symm⟩⟩
@[simp] theorem lift_le {a b : cardinal} : lift a ≤ lift b ↔ a ≤ b :=
quotient.induction_on₂ a b $ λ α β,
by rw ← lift_umax; exact lift_mk_le
@[simp] theorem lift_inj {a b : cardinal} : lift a = lift b ↔ a = b :=
by simp [le_antisymm_iff]
@[simp] theorem lift_lt {a b : cardinal} : lift a < lift b ↔ a < b :=
by simp [lt_iff_le_not_le, -not_le]
@[simp] theorem lift_zero : lift 0 = 0 :=
quotient.sound ⟨equiv.ulift.trans equiv.pempty_equiv_pempty⟩
@[simp] theorem lift_one : lift 1 = 1 :=
quotient.sound ⟨equiv.ulift.trans equiv.punit_equiv_punit⟩
@[simp] theorem lift_add (a b) : lift (a + b) = lift a + lift b :=
quotient.induction_on₂ a b $ λ α β,
quotient.sound ⟨equiv.ulift.trans (equiv.sum_congr equiv.ulift equiv.ulift).symm⟩
@[simp] theorem lift_mul (a b) : lift (a * b) = lift a * lift b :=
quotient.induction_on₂ a b $ λ α β,
quotient.sound ⟨equiv.ulift.trans (equiv.prod_congr equiv.ulift equiv.ulift).symm⟩
@[simp] theorem lift_power (a b) : lift (a ^ b) = lift a ^ lift b :=
quotient.induction_on₂ a b $ λ α β,
quotient.sound ⟨equiv.ulift.trans (equiv.arrow_congr equiv.ulift equiv.ulift).symm⟩
@[simp] theorem lift_two_power (a) : lift (2 ^ a) = 2 ^ lift a :=
by simp [bit0]
@[simp] theorem lift_min {ι I} (f : ι → cardinal) : lift (min I f) = min I (lift ∘ f) :=
le_antisymm (le_min.2 $ λ a, lift_le.2 $ min_le _ a) $
let ⟨i, e⟩ := min_eq I (lift ∘ f) in
by rw e; exact lift_le.2 (le_min.2 $ λ j, lift_le.1 $
by have := min_le (lift ∘ f) j; rwa e at this)
theorem lift_down {a : cardinal.{u}} {b : cardinal.{max u v}} :
b ≤ lift a → ∃ a', lift a' = b :=
quotient.induction_on₂ a b $ λ α β,
by dsimp; rw [← lift_id (mk β), ← lift_umax, ← lift_umax.{u v}, lift_mk_le]; exact
λ ⟨f⟩, ⟨mk (set.range f), eq.symm $ lift_mk_eq.2
⟨embedding.equiv_of_surjective
(embedding.cod_restrict _ f set.mem_range_self)
$ λ ⟨a, ⟨b, e⟩⟩, ⟨b, subtype.eq e⟩⟩⟩
theorem le_lift_iff {a : cardinal.{u}} {b : cardinal.{max u v}} :
b ≤ lift a ↔ ∃ a', lift a' = b ∧ a' ≤ a :=
⟨λ h, let ⟨a', e⟩ := lift_down h in ⟨a', e, lift_le.1 $ e.symm ▸ h⟩,
λ ⟨a', e, h⟩, e ▸ lift_le.2 h⟩
theorem lt_lift_iff {a : cardinal.{u}} {b : cardinal.{max u v}} :
b < lift a ↔ ∃ a', lift a' = b ∧ a' < a :=
⟨λ h, let ⟨a', e⟩ := lift_down (le_of_lt h) in
⟨a', e, lift_lt.1 $ e.symm ▸ h⟩,
λ ⟨a', e, h⟩, e ▸ lift_lt.2 h⟩
@[simp] theorem lift_succ (a) : lift (succ a) = succ (lift a) :=
le_antisymm
(le_of_not_gt $ λ h, begin
rcases lt_lift_iff.1 h with ⟨b, e, h⟩,
rw [lt_succ, ← lift_le, e] at h,
exact not_lt_of_le h (lt_succ_self _)
end)
(succ_le.2 $ lift_lt.2 $ lt_succ_self _)
@[simp] theorem lift_max {a : cardinal.{u}} {b : cardinal.{v}} :
lift.{u (max v w)} a = lift.{v (max u w)} b ↔ lift.{u v} a = lift.{v u} b :=
calc lift.{u (max v w)} a = lift.{v (max u w)} b
↔ lift.{(max u v) w} (lift.{u v} a)
= lift.{(max u v) w} (lift.{v u} b) : by simp
... ↔ lift.{u v} a = lift.{v u} b : lift_inj
theorem mk_prod {α : Type u} {β : Type v} :
mk (α × β) = lift.{u v} (mk α) * lift.{v u} (mk β) :=
quotient.sound ⟨equiv.prod_congr (equiv.ulift).symm (equiv.ulift).symm⟩
theorem sum_const_eq_lift_mul (ι : Type u) (a : cardinal.{v}) :
sum (λ _:ι, a) = lift.{u v} (mk ι) * lift.{v u} a :=
begin
apply quotient.induction_on a,
intro α,
simp only [cardinal.mk_def, cardinal.sum_mk, cardinal.lift_id],
convert mk_prod using 1,
exact quotient.sound ⟨equiv.sigma_equiv_prod ι α⟩,
end
/-- `ω` is the smallest infinite cardinal, also known as ℵ₀. -/
def omega : cardinal.{u} := lift (mk ℕ)
lemma mk_nat : mk nat = omega := (lift_id _).symm
theorem omega_ne_zero : omega ≠ 0 :=
ne_zero_iff_nonempty.2 ⟨⟨0⟩⟩
theorem omega_pos : 0 < omega :=
pos_iff_ne_zero.2 omega_ne_zero
@[simp] theorem lift_omega : lift omega = omega := lift_lift _
/- properties about the cast from nat -/
@[simp] theorem mk_fin : ∀ (n : ℕ), mk (fin n) = n
| 0 := quotient.sound ⟨(equiv.pempty_of_not_nonempty $ λ ⟨h⟩, h.elim0)⟩
| (n+1) := by rw [nat.cast_succ, ← mk_fin]; exact
quotient.sound (fintype.card_eq.1 $ by simp)
@[simp] theorem lift_nat_cast (n : ℕ) : lift n = n :=
by induction n; simp *
lemma lift_eq_nat_iff {a : cardinal.{u}} {n : ℕ} : lift.{u v} a = n ↔ a = n :=
by rw [← lift_nat_cast.{u v} n, lift_inj]
lemma nat_eq_lift_eq_iff {n : ℕ} {a : cardinal.{u}} :
(n : cardinal) = lift.{u v} a ↔ (n : cardinal) = a :=
by rw [← lift_nat_cast.{u v} n, lift_inj]
theorem lift_mk_fin (n : ℕ) : lift (mk (fin n)) = n := by simp
theorem fintype_card (α : Type u) [fintype α] : mk α = fintype.card α :=
by rw [← lift_mk_fin.{u}, ← lift_id (mk α), lift_mk_eq.{u 0 u}];
exact fintype.card_eq.1 (by simp)
theorem card_le_of_finset {α} (s : finset α) :
(s.card : cardinal) ≤ cardinal.mk α :=
begin
rw (_ : (s.card : cardinal) = cardinal.mk (↑s : set α)),
{ exact ⟨function.embedding.subtype _⟩ },
rw [cardinal.fintype_card, fintype.card_coe]
end
@[simp, norm_cast] theorem nat_cast_pow {m n : ℕ} : (↑(pow m n) : cardinal) = m ^ n :=
by induction n; simp [pow_succ', -_root_.add_comm, power_add, *]
@[simp, norm_cast] theorem nat_cast_le {m n : ℕ} : (m : cardinal) ≤ n ↔ m ≤ n :=
by rw [← lift_mk_fin, ← lift_mk_fin, lift_le]; exact
⟨λ ⟨⟨f, hf⟩⟩, begin
have : _ = fintype.card _ := finset.card_image_of_injective finset.univ hf,
simp at this,
rw [← fintype.card_fin n, ← this],
exact finset.card_le_of_subset (finset.subset_univ _)
end,
λ h, ⟨⟨λ i, ⟨i.1, lt_of_lt_of_le i.2 h⟩, λ a b h,
have _, from fin.veq_of_eq h, fin.eq_of_veq this⟩⟩⟩
@[simp, norm_cast] theorem nat_cast_lt {m n : ℕ} : (m : cardinal) < n ↔ m < n :=
by simp [lt_iff_le_not_le, -not_le]
@[simp, norm_cast] theorem nat_cast_inj {m n : ℕ} : (m : cardinal) = n ↔ m = n :=
by simp [le_antisymm_iff]
@[simp, norm_cast, priority 900] theorem nat_succ (n : ℕ) : (n.succ : cardinal) = succ n :=
le_antisymm (add_one_le_succ _) (succ_le.2 $ nat_cast_lt.2 $ nat.lt_succ_self _)
@[simp] theorem succ_zero : succ 0 = 1 :=
by norm_cast
theorem card_le_of {α : Type u} {n : ℕ} (H : ∀ s : finset α, s.card ≤ n) :
# α ≤ n :=
begin
refine lt_succ.1 (lt_of_not_ge $ λ hn, _),
rw [← cardinal.nat_succ, ← cardinal.lift_mk_fin n.succ] at hn,
cases hn with f,
refine not_lt_of_le (H $ finset.univ.map f) _,
rw [finset.card_map, ← fintype.card, fintype.card_ulift, fintype.card_fin],
exact n.lt_succ_self
end
theorem cantor' (a) {b : cardinal} (hb : 1 < b) : a < b ^ a :=
by rw [← succ_le, (by norm_cast : succ 1 = 2)] at hb;
exact lt_of_lt_of_le (cantor _) (power_le_power_right hb)
theorem one_le_iff_pos {c : cardinal} : 1 ≤ c ↔ 0 < c :=
by rw [← succ_zero, succ_le]
theorem one_le_iff_ne_zero {c : cardinal} : 1 ≤ c ↔ c ≠ 0 :=
by rw [one_le_iff_pos, pos_iff_ne_zero]
theorem nat_lt_omega (n : ℕ) : (n : cardinal.{u}) < omega :=
succ_le.1 $ by rw [← nat_succ, ← lift_mk_fin, omega, lift_mk_le.{0 0 u}]; exact
⟨⟨coe, λ a b, fin.ext⟩⟩
@[simp] theorem one_lt_omega : 1 < omega :=
by simpa using nat_lt_omega 1
theorem lt_omega {c : cardinal.{u}} : c < omega ↔ ∃ n : ℕ, c = n :=
⟨λ h, begin
rcases lt_lift_iff.1 h with ⟨c, rfl, h'⟩,
rcases le_mk_iff_exists_set.1 h'.1 with ⟨S, rfl⟩,
suffices : finite S,
{ cases this, resetI,
existsi fintype.card S,
rw [← lift_nat_cast.{0 u}, lift_inj, fintype_card S] },
by_contra nf,
have P : ∀ (n : ℕ) (IH : ∀ i<n, S), ∃ a : S, ¬ ∃ y h, IH y h = a :=
λ n IH,
let g : {i | i < n} → S := λ ⟨i, h⟩, IH i h in
not_forall.1 (λ h, nf
⟨fintype.of_surjective g (λ a, subtype.exists.2 (h a))⟩),
let F : ℕ → S := nat.lt_wf.fix (λ n IH, classical.some (P n IH)),
refine not_le_of_lt h' ⟨⟨F, _⟩⟩,
suffices : ∀ (n : ℕ) (m < n), F m ≠ F n,
{ refine λ m n, not_imp_not.1 (λ ne, _),
rcases lt_trichotomy m n with h|h|h,
{ exact this n m h },
{ contradiction },
{ exact (this m n h).symm } },
intros n m h,
have := classical.some_spec (P n (λ y _, F y)),
rw [← show F n = classical.some (P n (λ y _, F y)),
from nat.lt_wf.fix_eq (λ n IH, classical.some (P n IH)) n] at this,
exact λ e, this ⟨m, h, e⟩,
end, λ ⟨n, e⟩, e.symm ▸ nat_lt_omega _⟩
theorem omega_le {c : cardinal.{u}} : omega ≤ c ↔ ∀ n : ℕ, (n:cardinal) ≤ c :=
⟨λ h n, le_trans (le_of_lt (nat_lt_omega _)) h,
λ h, le_of_not_lt $ λ hn, begin
rcases lt_omega.1 hn with ⟨n, rfl⟩,
exact not_le_of_lt (nat.lt_succ_self _) (nat_cast_le.1 (h (n+1)))
end⟩
theorem lt_omega_iff_fintype {α : Type u} : mk α < omega ↔ nonempty (fintype α) :=
lt_omega.trans ⟨λ ⟨n, e⟩, begin
rw [← lift_mk_fin n] at e,
cases quotient.exact e with f,
exact ⟨fintype.of_equiv _ f.symm⟩
end, λ ⟨_⟩, by exactI ⟨_, fintype_card _⟩⟩
theorem lt_omega_iff_finite {α} {S : set α} : mk S < omega ↔ finite S :=
lt_omega_iff_fintype
instance can_lift_cardinal_nat : can_lift cardinal ℕ :=
⟨ coe, λ x, x < omega, λ x hx, let ⟨n, hn⟩ := lt_omega.mp hx in ⟨n, hn.symm⟩⟩
theorem add_lt_omega {a b : cardinal} (ha : a < omega) (hb : b < omega) : a + b < omega :=
match a, b, lt_omega.1 ha, lt_omega.1 hb with
| _, _, ⟨m, rfl⟩, ⟨n, rfl⟩ := by rw [← nat.cast_add]; apply nat_lt_omega
end
lemma add_lt_omega_iff {a b : cardinal} : a + b < omega ↔ a < omega ∧ b < omega :=
⟨λ h, ⟨lt_of_le_of_lt (le_add_right _ _) h, lt_of_le_of_lt (le_add_left _ _) h⟩,
λ⟨h1, h2⟩, add_lt_omega h1 h2⟩
theorem mul_lt_omega {a b : cardinal} (ha : a < omega) (hb : b < omega) : a * b < omega :=
match a, b, lt_omega.1 ha, lt_omega.1 hb with
| _, _, ⟨m, rfl⟩, ⟨n, rfl⟩ := by rw [← nat.cast_mul]; apply nat_lt_omega
end
lemma mul_lt_omega_iff {a b : cardinal} : a * b < omega ↔ a = 0 ∨ b = 0 ∨ a < omega ∧ b < omega :=
begin
split,
{ intro h, by_cases ha : a = 0, { left, exact ha },
right, by_cases hb : b = 0, { left, exact hb },
right, rw [← ne, ← one_le_iff_ne_zero] at ha hb, split,
{ rw [← mul_one a], refine lt_of_le_of_lt (mul_le_mul (le_refl a) hb) h },
{ rw [← _root_.one_mul b], refine lt_of_le_of_lt (mul_le_mul ha (le_refl b)) h }},
rintro (rfl|rfl|⟨ha,hb⟩); simp only [*, mul_lt_omega, omega_pos, _root_.zero_mul, mul_zero]
end
lemma mul_lt_omega_iff_of_ne_zero {a b : cardinal} (ha : a ≠ 0) (hb : b ≠ 0) :
a * b < omega ↔ a < omega ∧ b < omega :=
by simp [mul_lt_omega_iff, ha, hb]
theorem power_lt_omega {a b : cardinal} (ha : a < omega) (hb : b < omega) : a ^ b < omega :=
match a, b, lt_omega.1 ha, lt_omega.1 hb with
| _, _, ⟨m, rfl⟩, ⟨n, rfl⟩ := by rw [← nat_cast_pow]; apply nat_lt_omega
end
lemma eq_one_iff_subsingleton_and_nonempty {α : Type*} :
mk α = 1 ↔ (subsingleton α ∧ nonempty α) :=
calc mk α = 1 ↔ mk α ≤ 1 ∧ ¬mk α < 1 : eq_iff_le_not_lt
... ↔ subsingleton α ∧ nonempty α :
begin
apply and_congr le_one_iff_subsingleton,
push_neg,
rw [one_le_iff_ne_zero, ne_zero_iff_nonempty]
end
theorem infinite_iff {α : Type u} : infinite α ↔ omega ≤ mk α :=
by rw [←not_lt, lt_omega_iff_fintype, not_nonempty_fintype]
lemma countable_iff (s : set α) : countable s ↔ mk s ≤ omega :=
begin
rw [countable_iff_exists_injective], split,
rintro ⟨f, hf⟩, exact ⟨embedding.trans ⟨f, hf⟩ equiv.ulift.symm.to_embedding⟩,
rintro ⟨f'⟩, cases embedding.trans f' equiv.ulift.to_embedding with f hf, exact ⟨f, hf⟩
end
lemma denumerable_iff {α : Type u} : nonempty (denumerable α) ↔ mk α = omega :=
⟨λ⟨h⟩, quotient.sound $ by exactI ⟨ (denumerable.eqv α).trans equiv.ulift.symm ⟩,
λ h, by { cases quotient.exact h with f, exact ⟨denumerable.mk' $ f.trans equiv.ulift⟩ }⟩
lemma mk_int : mk ℤ = omega :=
denumerable_iff.mp ⟨by apply_instance⟩
lemma mk_pnat : mk ℕ+ = omega :=
denumerable_iff.mp ⟨by apply_instance⟩
lemma two_le_iff : (2 : cardinal) ≤ mk α ↔ ∃x y : α, x ≠ y :=
begin
split,
{ rintro ⟨f⟩, refine ⟨f $ sum.inl ⟨⟩, f $ sum.inr ⟨⟩, _⟩, intro h, cases f.2 h },
{ rintro ⟨x, y, h⟩, by_contra h',
rw [not_le, ←nat.cast_two, nat_succ, lt_succ, nat.cast_one, le_one_iff_subsingleton] at h',
apply h, exactI subsingleton.elim _ _ }
end
lemma two_le_iff' (x : α) : (2 : cardinal) ≤ mk α ↔ ∃y : α, x ≠ y :=
begin
rw [two_le_iff],
split,
{ rintro ⟨y, z, h⟩, refine classical.by_cases (λ(h' : x = y), _) (λ h', ⟨y, h'⟩),
rw [←h'] at h, exact ⟨z, h⟩ },
{ rintro ⟨y, h⟩, exact ⟨x, y, h⟩ }
end
/-- König's theorem -/
theorem sum_lt_prod {ι} (f g : ι → cardinal) (H : ∀ i, f i < g i) : sum f < prod g :=
lt_of_not_ge $ λ ⟨F⟩, begin
have : inhabited (Π (i : ι), (g i).out),
{ refine ⟨λ i, classical.choice $ ne_zero_iff_nonempty.1 _⟩,
rw mk_out,
exact ne_of_gt (lt_of_le_of_lt (zero_le _) (H i)) }, resetI,
let G := inv_fun F,
have sG : surjective G := inv_fun_surjective F.2,
choose C hc using show ∀ i, ∃ b, ∀ a, G ⟨i, a⟩ i ≠ b,
{ assume i,
simp only [- not_exists, not_exists.symm, not_forall.symm],
refine λ h, not_le_of_lt (H i) _,
rw [← mk_out (f i), ← mk_out (g i)],
exact ⟨embedding.of_surjective _ h⟩ },
exact (let ⟨⟨i, a⟩, h⟩ := sG C in hc i a (congr_fun h _))
end
@[simp] theorem mk_empty : mk empty = 0 :=
fintype_card empty
@[simp] theorem mk_pempty : mk pempty = 0 :=
fintype_card pempty
@[simp] theorem mk_plift_of_false {p : Prop} (h : ¬ p) : mk (plift p) = 0 :=
quotient.sound ⟨equiv.plift.trans $ equiv.equiv_pempty h⟩
theorem mk_unit : mk unit = 1 :=
(fintype_card unit).trans nat.cast_one
@[simp] theorem mk_punit : mk punit = 1 :=
(fintype_card punit).trans nat.cast_one
@[simp] theorem mk_singleton {α : Type u} (x : α) : mk ({x} : set α) = 1 :=
quotient.sound ⟨equiv.set.singleton x⟩
@[simp] theorem mk_plift_of_true {p : Prop} (h : p) : mk (plift p) = 1 :=
quotient.sound ⟨equiv.plift.trans $ equiv.prop_equiv_punit h⟩
@[simp] theorem mk_bool : mk bool = 2 :=
quotient.sound ⟨equiv.bool_equiv_punit_sum_punit⟩
@[simp] theorem mk_Prop : mk Prop = 2 :=
(quotient.sound ⟨equiv.Prop_equiv_bool⟩ : mk Prop = mk bool).trans mk_bool
@[simp] theorem mk_set {α : Type u} : mk (set α) = 2 ^ mk α :=
begin
rw [← prop_eq_two, cardinal.power_def (ulift Prop) α, cardinal.eq],
exact ⟨equiv.arrow_congr (equiv.refl _) equiv.ulift.symm⟩,
end
@[simp] theorem mk_option {α : Type u} : mk (option α) = mk α + 1 :=
quotient.sound ⟨equiv.option_equiv_sum_punit α⟩
theorem mk_list_eq_sum_pow (α : Type u) : mk (list α) = sum (λ n : ℕ, (mk α)^(n:cardinal.{u})) :=
calc mk (list α)
= mk (Σ n, vector α n) : quotient.sound ⟨(equiv.sigma_preimage_equiv list.length).symm⟩
... = mk (Σ n, fin n → α) : quotient.sound ⟨equiv.sigma_congr_right $ λ n,
⟨vector.nth, vector.of_fn, vector.of_fn_nth, λ f, funext $ vector.nth_of_fn f⟩⟩
... = mk (Σ n : ℕ, ulift.{u} (fin n) → α) : quotient.sound ⟨equiv.sigma_congr_right $ λ n,
equiv.arrow_congr equiv.ulift.symm (equiv.refl α)⟩
... = sum (λ n : ℕ, (mk α)^(n:cardinal.{u})) : by simp only [(lift_mk_fin _).symm, lift_mk, power_def, sum_mk]
theorem mk_quot_le {α : Type u} {r : α → α → Prop} : mk (quot r) ≤ mk α :=
mk_le_of_surjective quot.exists_rep
theorem mk_quotient_le {α : Type u} {s : setoid α} : mk (quotient s) ≤ mk α :=
mk_quot_le
theorem mk_subtype_le {α : Type u} (p : α → Prop) : mk (subtype p) ≤ mk α :=
⟨embedding.subtype p⟩
theorem mk_subtype_le_of_subset {α : Type u} {p q : α → Prop} (h : ∀ ⦃x⦄, p x → q x) :
mk (subtype p) ≤ mk (subtype q) :=
⟨embedding.subtype_map (embedding.refl α) h⟩
@[simp] theorem mk_emptyc (α : Type u) : mk (∅ : set α) = 0 :=
quotient.sound ⟨equiv.set.pempty α⟩
lemma mk_emptyc_iff {α : Type u} {s : set α} : mk s = 0 ↔ s = ∅ :=
begin
split,
{ intro h,
have h2 : cardinal.mk s = cardinal.mk pempty, by simp [h],
refine set.eq_empty_iff_forall_not_mem.mpr (λ _ hx, _),
rcases cardinal.eq.mp h2 with ⟨f, _⟩,
cases f ⟨_, hx⟩ },
{ intro, convert mk_emptyc _ }
end
theorem mk_univ {α : Type u} : mk (@univ α) = mk α :=
quotient.sound ⟨equiv.set.univ α⟩
theorem mk_image_le {α β : Type u} {f : α → β} {s : set α} : mk (f '' s) ≤ mk s :=
mk_le_of_surjective surjective_onto_image
theorem mk_image_le_lift {α : Type u} {β : Type v} {f : α → β} {s : set α} :
lift.{v u} (mk (f '' s)) ≤ lift.{u v} (mk s) :=
lift_mk_le.{v u 0}.mpr ⟨embedding.of_surjective _ surjective_onto_image⟩
theorem mk_range_le {α β : Type u} {f : α → β} : mk (range f) ≤ mk α :=
mk_le_of_surjective surjective_onto_range
lemma mk_range_eq (f : α → β) (h : injective f) : mk (range f) = mk α :=
quotient.sound ⟨(equiv.set.range f h).symm⟩
lemma mk_range_eq_of_injective {α : Type u} {β : Type v} {f : α → β} (hf : injective f) :
lift.{v u} (mk (range f)) = lift.{u v} (mk α) :=
begin
have := (@lift_mk_eq.{v u max u v} (range f) α).2 ⟨(equiv.set.range f hf).symm⟩,
simp only [lift_umax.{u v}, lift_umax.{v u}] at this,
exact this
end
lemma mk_range_eq_lift {α : Type u} {β : Type v} {f : α → β} (hf : injective f) :
lift.{v (max u w)} (# (range f)) = lift.{u (max v w)} (# α) :=
lift_mk_eq.mpr ⟨(equiv.set.range f hf).symm⟩
theorem mk_image_eq {α β : Type u} {f : α → β} {s : set α} (hf : injective f) :
mk (f '' s) = mk s :=
quotient.sound ⟨(equiv.set.image f s hf).symm⟩
theorem mk_Union_le_sum_mk {α ι : Type u} {f : ι → set α} : mk (⋃ i, f i) ≤ sum (λ i, mk (f i)) :=
calc mk (⋃ i, f i) ≤ mk (Σ i, f i) : mk_le_of_surjective (set.sigma_to_Union_surjective f)
... = sum (λ i, mk (f i)) : (sum_mk _).symm
theorem mk_Union_eq_sum_mk {α ι : Type u} {f : ι → set α} (h : ∀i j, i ≠ j → disjoint (f i) (f j)) :
mk (⋃ i, f i) = sum (λ i, mk (f i)) :=
calc mk (⋃ i, f i) = mk (Σi, f i) : quot.sound ⟨set.Union_eq_sigma_of_disjoint h⟩
... = sum (λi, mk (f i)) : (sum_mk _).symm
lemma mk_Union_le {α ι : Type u} (f : ι → set α) :
mk (⋃ i, f i) ≤ mk ι * cardinal.sup.{u u} (λ i, mk (f i)) :=
le_trans mk_Union_le_sum_mk (sum_le_sup _)
lemma mk_sUnion_le {α : Type u} (A : set (set α)) :
mk (⋃₀ A) ≤ mk A * cardinal.sup.{u u} (λ s : A, mk s) :=
by { rw [sUnion_eq_Union], apply mk_Union_le }
lemma mk_bUnion_le {ι α : Type u} (A : ι → set α) (s : set ι) :
mk (⋃(x ∈ s), A x) ≤ mk s * cardinal.sup.{u u} (λ x : s, mk (A x.1)) :=
by { rw [bUnion_eq_Union], apply mk_Union_le }
@[simp] lemma finset_card {α : Type u} {s : finset α} : ↑(finset.card s) = mk (↑s : set α) :=
by rw [fintype_card, nat_cast_inj, fintype.card_coe]
lemma finset_card_lt_omega (s : finset α) : mk (↑s : set α) < omega :=
by { rw [lt_omega_iff_fintype], exact ⟨finset.subtype.fintype s⟩ }
theorem mk_union_add_mk_inter {α : Type u} {S T : set α} :
mk (S ∪ T : set α) + mk (S ∩ T : set α) = mk S + mk T :=
quot.sound ⟨equiv.set.union_sum_inter S T⟩
/-- The cardinality of a union is at most the sum of the cardinalities
of the two sets. -/
lemma mk_union_le {α : Type u} (S T : set α) : mk (S ∪ T : set α) ≤ mk S + mk T :=
@mk_union_add_mk_inter α S T ▸ le_add_right (mk (S ∪ T : set α)) (mk (S ∩ T : set α))
theorem mk_union_of_disjoint {α : Type u} {S T : set α} (H : disjoint S T) :
mk (S ∪ T : set α) = mk S + mk T :=
quot.sound ⟨equiv.set.union H⟩
lemma mk_sum_compl {α} (s : set α) : #s + #(sᶜ : set α) = #α :=
quotient.sound ⟨equiv.set.sum_compl s⟩
lemma mk_le_mk_of_subset {α} {s t : set α} (h : s ⊆ t) : mk s ≤ mk t :=
⟨set.embedding_of_subset s t h⟩
lemma mk_subtype_mono {p q : α → Prop} (h : ∀x, p x → q x) : mk {x // p x} ≤ mk {x // q x} :=
⟨embedding_of_subset _ _ h⟩
lemma mk_set_le (s : set α) : mk s ≤ mk α :=
mk_subtype_le s
lemma mk_image_eq_lift {α : Type u} {β : Type v} (f : α → β) (s : set α) (h : injective f) :
lift.{v u} (mk (f '' s)) = lift.{u v} (mk s) :=
lift_mk_eq.{v u 0}.mpr ⟨(equiv.set.image f s h).symm⟩
lemma mk_image_eq_of_inj_on_lift {α : Type u} {β : Type v} (f : α → β) (s : set α)
(h : inj_on f s) : lift.{v u} (mk (f '' s)) = lift.{u v} (mk s) :=
lift_mk_eq.{v u 0}.mpr ⟨(equiv.set.image_of_inj_on f s h).symm⟩
lemma mk_image_eq_of_inj_on {α β : Type u} (f : α → β) (s : set α) (h : inj_on f s) :
mk (f '' s) = mk s :=
quotient.sound ⟨(equiv.set.image_of_inj_on f s h).symm⟩
lemma mk_subtype_of_equiv {α β : Type u} (p : β → Prop) (e : α ≃ β) :
mk {a : α // p (e a)} = mk {b : β // p b} :=
quotient.sound ⟨equiv.subtype_equiv_of_subtype e⟩
lemma mk_sep (s : set α) (t : α → Prop) : mk ({ x ∈ s | t x } : set α) = mk { x : s | t x.1 } :=
quotient.sound ⟨equiv.set.sep s t⟩
lemma mk_preimage_of_injective_lift {α : Type u} {β : Type v} (f : α → β) (s : set β)
(h : injective f) : lift.{u v} (mk (f ⁻¹' s)) ≤ lift.{v u} (mk s) :=
begin
rw lift_mk_le.{u v 0}, use subtype.coind (λ x, f x.1) (λ x, x.2),
apply subtype.coind_injective, exact h.comp subtype.val_injective
end
lemma mk_preimage_of_subset_range_lift {α : Type u} {β : Type v} (f : α → β) (s : set β)
(h : s ⊆ range f) : lift.{v u} (mk s) ≤ lift.{u v} (mk (f ⁻¹' s)) :=
begin
rw lift_mk_le.{v u 0},
refine ⟨⟨_, _⟩⟩,
{ rintro ⟨y, hy⟩, rcases classical.subtype_of_exists (h hy) with ⟨x, rfl⟩, exact ⟨x, hy⟩ },
rintro ⟨y, hy⟩ ⟨y', hy'⟩, dsimp,
rcases classical.subtype_of_exists (h hy) with ⟨x, rfl⟩,
rcases classical.subtype_of_exists (h hy') with ⟨x', rfl⟩,
simp, intro hxx', rw hxx'
end
lemma mk_preimage_of_injective_of_subset_range_lift {β : Type v} (f : α → β) (s : set β)
(h : injective f) (h2 : s ⊆ range f) : lift.{u v} (mk (f ⁻¹' s)) = lift.{v u} (mk s) :=
le_antisymm (mk_preimage_of_injective_lift f s h) (mk_preimage_of_subset_range_lift f s h2)
lemma mk_preimage_of_injective (f : α → β) (s : set β) (h : injective f) :
mk (f ⁻¹' s) ≤ mk s :=
by { convert mk_preimage_of_injective_lift.{u u} f s h using 1; rw [lift_id] }
lemma mk_preimage_of_subset_range (f : α → β) (s : set β)
(h : s ⊆ range f) : mk s ≤ mk (f ⁻¹' s) :=
by { convert mk_preimage_of_subset_range_lift.{u u} f s h using 1; rw [lift_id] }
lemma mk_preimage_of_injective_of_subset_range (f : α → β) (s : set β)
(h : injective f) (h2 : s ⊆ range f) : mk (f ⁻¹' s) = mk s :=
by { convert mk_preimage_of_injective_of_subset_range_lift.{u u} f s h h2 using 1; rw [lift_id] }
lemma mk_subset_ge_of_subset_image_lift {α : Type u} {β : Type v} (f : α → β) {s : set α}
{t : set β} (h : t ⊆ f '' s) :
lift.{v u} (mk t) ≤ lift.{u v} (mk ({ x ∈ s | f x ∈ t } : set α)) :=
by { rw [image_eq_range] at h, convert mk_preimage_of_subset_range_lift _ _ h using 1, rw [mk_sep], refl }
lemma mk_subset_ge_of_subset_image (f : α → β) {s : set α} {t : set β} (h : t ⊆ f '' s) :
mk t ≤ mk ({ x ∈ s | f x ∈ t } : set α) :=
by { rw [image_eq_range] at h, convert mk_preimage_of_subset_range _ _ h using 1, rw [mk_sep], refl }
theorem le_mk_iff_exists_subset {c : cardinal} {α : Type u} {s : set α} :
c ≤ mk s ↔ ∃ p : set α, p ⊆ s ∧ mk p = c :=
begin
rw [le_mk_iff_exists_set, ←subtype.exists_set_subtype],
apply exists_congr, intro t, rw [mk_image_eq], apply subtype.val_injective
end
/-- The function α^{<β}, defined to be sup_{γ < β} α^γ.
We index over {s : set β.out // mk s < β } instead of {γ // γ < β}, because the latter lives in a
higher universe -/
noncomputable def powerlt (α β : cardinal.{u}) : cardinal.{u} :=
sup.{u u} (λ(s : {s : set β.out // mk s < β}), α ^ mk.{u} s)
infix ` ^< `:80 := powerlt
theorem powerlt_aux {c c' : cardinal} (h : c < c') :
∃(s : {s : set c'.out // mk s < c'}), mk s = c :=
begin
cases out_embedding.mp (le_of_lt h) with f,
have : mk ↥(range ⇑f) = c, { rwa [mk_range_eq, mk, quotient.out_eq c], exact f.2 },
exact ⟨⟨range f, by convert h⟩, this⟩
end
lemma le_powerlt {c₁ c₂ c₃ : cardinal} (h : c₂ < c₃) : c₁ ^ c₂ ≤ c₁ ^< c₃ :=
by { rcases powerlt_aux h with ⟨s, rfl⟩, apply le_sup _ s }
lemma powerlt_le {c₁ c₂ c₃ : cardinal} : c₁ ^< c₂ ≤ c₃ ↔ ∀(c₄ < c₂), c₁ ^ c₄ ≤ c₃ :=
begin
rw [powerlt, sup_le],
split,
{ intros h c₄ hc₄, rcases powerlt_aux hc₄ with ⟨s, rfl⟩, exact h s },
intros h s, exact h _ s.2
end
lemma powerlt_le_powerlt_left {a b c : cardinal} (h : b ≤ c) : a ^< b ≤ a ^< c :=
by { rw [powerlt, sup_le], rintro ⟨s, hs⟩, apply le_powerlt, exact lt_of_lt_of_le hs h }
lemma powerlt_succ {c₁ c₂ : cardinal} (h : c₁ ≠ 0) : c₁ ^< c₂.succ = c₁ ^ c₂ :=
begin
apply le_antisymm,
{ rw powerlt_le, intros c₃ h2, apply power_le_power_left h, rwa [←lt_succ] },
{ apply le_powerlt, apply lt_succ_self }
end
lemma powerlt_max {c₁ c₂ c₃ : cardinal} : c₁ ^< max c₂ c₃ = max (c₁ ^< c₂) (c₁ ^< c₃) :=
by { cases le_total c₂ c₃; simp only [max_eq_left, max_eq_right, h, powerlt_le_powerlt_left] }
lemma zero_powerlt {a : cardinal} (h : a ≠ 0) : 0 ^< a = 1 :=
begin
apply le_antisymm,
{ rw [powerlt_le], intros c hc, apply zero_power_le },
convert le_powerlt (pos_iff_ne_zero.2 h), rw [power_zero]
end
lemma powerlt_zero {a : cardinal} : a ^< 0 = 0 :=
by { apply sup_eq_zero, rintro ⟨x, hx⟩, rw [←not_le] at hx, apply hx, apply zero_le }
end cardinal
|
dfb1c1ba66ef4d8defc55bb3a0f88dad49e6f53e | f1dc39e1c68f71465c8bf79910c4664d03824751 | /tests/lean/run/sebastien_coe_simp.lean | 06ae3b2fcbbc3efdaf9e206b0a33623ba240afa4 | [
"Apache-2.0"
] | permissive | kckennylau/lean-2 | 6504f45da07bc98b098d726b74130103be25885c | c9a9368bc0fd600d832bd56c5cb2124b8a523ef9 | refs/heads/master | 1,659,140,308,864 | 1,589,361,166,000 | 1,589,361,166,000 | 263,748,786 | 0 | 0 | null | 1,589,405,915,000 | 1,589,405,915,000 | null | UTF-8 | Lean | false | false | 525 | lean | variable (α : Type*)
structure my_equiv :=
(to_fun : α → α)
instance : has_coe_to_fun (my_equiv α) := ⟨_, my_equiv.to_fun⟩
def my_equiv1 : my_equiv α :=
{ to_fun := id }
def my_equiv2 : my_equiv α :=
{ to_fun := id }
@[simp] lemma one_eq_two : my_equiv1 α = my_equiv2 α := rfl
lemma other (x : ℕ) : my_equiv1 ℕ (x + 0) = my_equiv2 ℕ x := by simp -- does not fail
@[simp] lemma two_apply (x : α) : my_equiv2 α x = x := rfl
lemma one_apply (x : α) : my_equiv1 α x = x := by simp -- does not fail |
7fcb6614dca3a614aa16599b1e43e1fe2bf6a2f2 | dd0f5513e11c52db157d2fcc8456d9401a6cd9da | /03_Propositions_and_Proofs.org.28.lean | bda96e7cd9684eb4ce3ee4b03f1866153fe4626d | [] | no_license | cjmazey/lean-tutorial | ba559a49f82aa6c5848b9bf17b7389bf7f4ba645 | 381f61c9fcac56d01d959ae0fa6e376f2c4e3b34 | refs/heads/master | 1,610,286,098,832 | 1,447,124,923,000 | 1,447,124,923,000 | 43,082,433 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 142 | lean | /- page 41 -/
import standard
variables p q r : Prop
-- BEGIN
example (Hnp : ¬p) (Hq : q) (Hqp : q → p) : r :=
absurd (Hqp Hq) Hnp
-- END
|
e3a58a31ed0048bb19f2e6dcdded3f0aab9b24cc | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/data/bitvec/core_auto.lean | 65762f432b2636b880241fdd799e8c9b30b57375 | [] | no_license | AurelienSaue/Mathlib4_auto | f538cfd0980f65a6361eadea39e6fc639e9dae14 | 590df64109b08190abe22358fabc3eae000943f2 | refs/heads/master | 1,683,906,849,776 | 1,622,564,669,000 | 1,622,564,669,000 | 371,723,747 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 7,876 | lean | /-
Copyright (c) 2015 Joe Hendrix. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Joe Hendrix, Sebastian Ullrich
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.data.vector2
import Mathlib.data.nat.basic
import Mathlib.PostPort
namespace Mathlib
/-!
# Basic operations on bitvectors
This is a work-in-progress, and contains additions to other theories.
This file was moved to mathlib from core Lean in the switch to Lean 3.20.0c. It is not fully in compliance with mathlib style standards.
-/
/-- `bitvec n` is a `vector` of `bool` with length `n`. -/
def bitvec (n : ℕ) := vector Bool n
namespace bitvec
/-- Create a zero bitvector -/
protected def zero (n : ℕ) : bitvec n := vector.repeat false n
/-- Create a bitvector of length `n` whose `n-1`st entry is 1 and other entries are 0. -/
protected def one (n : ℕ) : bitvec n := sorry
/-- Create a bitvector from another with a provably equal length. -/
protected def cong {a : ℕ} {b : ℕ} (h : a = b) : bitvec a → bitvec b := sorry
/-- `bitvec` specific version of `vector.append` -/
def append {m : ℕ} {n : ℕ} : bitvec m → bitvec n → bitvec (m + n) := vector.append
/-! ### Shift operations -/
/-- `shl x i` is the bitvector obtained by left-shifting `x` `i` times and padding with `ff`.
If `x.length < i` then this will return the all-`ff`s bitvector. -/
def shl {n : ℕ} (x : bitvec n) (i : ℕ) : bitvec n :=
bitvec.cong sorry (vector.append (vector.drop i x) (vector.repeat false (min n i)))
/-- `fill_shr x i fill` is the bitvector obtained by right-shifting `x` `i` times and then
padding with `fill : bool`. If `x.length < i` then this will return the constant `fill`
bitvector. -/
def fill_shr {n : ℕ} (x : bitvec n) (i : ℕ) (fill : Bool) : bitvec n :=
bitvec.cong sorry (vector.append (vector.repeat fill (min n i)) (vector.take (n - i) x))
/-- unsigned shift right -/
def ushr {n : ℕ} (x : bitvec n) (i : ℕ) : bitvec n := fill_shr x i false
/-- signed shift right -/
def sshr {m : ℕ} : bitvec m → ℕ → bitvec m := sorry
/-! ### Bitwise operations -/
/-- bitwise not -/
/-- bitwise and -/
def not {n : ℕ} : bitvec n → bitvec n := vector.map bnot
/-- bitwise or -/
def and {n : ℕ} : bitvec n → bitvec n → bitvec n := vector.map₂ band
/-- bitwise xor -/
def or {n : ℕ} : bitvec n → bitvec n → bitvec n := vector.map₂ bor
def xor {n : ℕ} : bitvec n → bitvec n → bitvec n := vector.map₂ bxor
/-! ### Arithmetic operators -/
/-- `xor3 x y c` is `((x XOR y) XOR c)`. -/
/-- `carry x y c` is `x && y || x && c || y && c`. -/
protected def xor3 (x : Bool) (y : Bool) (c : Bool) : Bool := bxor (bxor x y) c
protected def carry (x : Bool) (y : Bool) (c : Bool) : Bool := x && y || x && c || y && c
/-- `neg x` is the two's complement of `x`. -/
protected def neg {n : ℕ} (x : bitvec n) : bitvec n :=
let f : Bool → Bool → Bool × Bool := fun (y c : Bool) => (y || c, bxor y c);
prod.snd (vector.map_accumr f x false)
/-- Add with carry (no overflow) -/
def adc {n : ℕ} (x : bitvec n) (y : bitvec n) (c : Bool) : bitvec (n + 1) :=
let f : Bool → Bool → Bool → Bool × Bool :=
fun (x y c : Bool) => (bitvec.carry x y c, bitvec.xor3 x y c);
sorry
/-- The sum of two bitvectors -/
protected def add {n : ℕ} (x : bitvec n) (y : bitvec n) : bitvec n := vector.tail (adc x y false)
/-- Subtract with borrow -/
def sbb {n : ℕ} (x : bitvec n) (y : bitvec n) (b : Bool) : Bool × bitvec n :=
let f : Bool → Bool → Bool → Bool × Bool :=
fun (x y c : Bool) => (bitvec.carry (!x) y c, bitvec.xor3 x y c);
vector.map_accumr₂ f x y b
/-- The difference of two bitvectors -/
protected def sub {n : ℕ} (x : bitvec n) (y : bitvec n) : bitvec n := prod.snd (sbb x y false)
protected instance has_zero {n : ℕ} : HasZero (bitvec n) := { zero := bitvec.zero n }
protected instance has_one {n : ℕ} : HasOne (bitvec n) := { one := bitvec.one n }
protected instance has_add {n : ℕ} : Add (bitvec n) := { add := bitvec.add }
protected instance has_sub {n : ℕ} : Sub (bitvec n) := { sub := bitvec.sub }
protected instance has_neg {n : ℕ} : Neg (bitvec n) := { neg := bitvec.neg }
/-- The product of two bitvectors -/
protected def mul {n : ℕ} (x : bitvec n) (y : bitvec n) : bitvec n :=
let f : bitvec n → Bool → bitvec n := fun (r : bitvec n) (b : Bool) => cond b (r + r + y) (r + r);
list.foldl f 0 (vector.to_list x)
protected instance has_mul {n : ℕ} : Mul (bitvec n) := { mul := bitvec.mul }
/-! ### Comparison operators -/
/-- `uborrow x y` returns `tt` iff the "subtract with borrow" operation on `x`, `y` and `ff`
required a borrow. -/
def uborrow {n : ℕ} (x : bitvec n) (y : bitvec n) : Bool := prod.fst (sbb x y false)
/-- unsigned less-than proposition -/
/-- unsigned greater-than proposition -/
def ult {n : ℕ} (x : bitvec n) (y : bitvec n) := ↥(uborrow x y)
def ugt {n : ℕ} (x : bitvec n) (y : bitvec n) := ult y x
/-- unsigned less-than-or-equal-to proposition -/
/-- unsigned greater-than-or-equal-to proposition -/
def ule {n : ℕ} (x : bitvec n) (y : bitvec n) := ¬ult y x
def uge {n : ℕ} (x : bitvec n) (y : bitvec n) := ule y x
/-- `sborrow x y` returns `tt` iff `x < y` as two's complement integers -/
def sborrow {n : ℕ} : bitvec n → bitvec n → Bool := sorry
/-- signed less-than proposition -/
/-- signed greater-than proposition -/
def slt {n : ℕ} (x : bitvec n) (y : bitvec n) := ↥(sborrow x y)
/-- signed less-than-or-equal-to proposition -/
def sgt {n : ℕ} (x : bitvec n) (y : bitvec n) := slt y x
/-- signed greater-than-or-equal-to proposition -/
def sle {n : ℕ} (x : bitvec n) (y : bitvec n) := ¬slt y x
def sge {n : ℕ} (x : bitvec n) (y : bitvec n) := sle y x
/-! ### Conversion to `nat` and `int` -/
/-- Create a bitvector from a `nat` -/
protected def of_nat (n : ℕ) : ℕ → bitvec n := sorry
/-- Create a bitvector in the two's complement representation from an `int` -/
protected def of_int (n : ℕ) : ℤ → bitvec (Nat.succ n) := sorry
/-- `add_lsb r b` is `r + r + 1` if `b` is `tt` and `r + r` otherwise. -/
def add_lsb (r : ℕ) (b : Bool) : ℕ := r + r + cond b 1 0
/-- Given a `list` of `bool`s, return the `nat` they represent as a list of binary digits. -/
def bits_to_nat (v : List Bool) : ℕ := list.foldl add_lsb 0 v
/-- Return the natural number encoded by the input bitvector -/
protected def to_nat {n : ℕ} (v : bitvec n) : ℕ := bits_to_nat (vector.to_list v)
theorem bits_to_nat_to_list {n : ℕ} (x : bitvec n) :
bitvec.to_nat x = bits_to_nat (vector.to_list x) :=
rfl
-- mul_left_comm
theorem to_nat_append {m : ℕ} (xs : bitvec m) (b : Bool) :
bitvec.to_nat (vector.append xs (b::ᵥvector.nil)) =
bitvec.to_nat xs * bit0 1 + bitvec.to_nat (b::ᵥvector.nil) :=
sorry
theorem bits_to_nat_to_bool (n : ℕ) :
bitvec.to_nat (to_bool (n % bit0 1 = 1)::ᵥvector.nil) = n % bit0 1 :=
sorry
theorem of_nat_succ {k : ℕ} {n : ℕ} :
bitvec.of_nat (Nat.succ k) n =
vector.append (bitvec.of_nat k (n / bit0 1)) (to_bool (n % bit0 1 = 1)::ᵥvector.nil) :=
rfl
theorem to_nat_of_nat {k : ℕ} {n : ℕ} : bitvec.to_nat (bitvec.of_nat k n) = n % bit0 1 ^ k := sorry
/-- Return the integer encoded by the input bitvector -/
protected def to_int {n : ℕ} : bitvec n → ℤ := sorry
/-! ### Miscellaneous instances -/
protected instance has_repr (n : ℕ) : has_repr (bitvec n) := has_repr.mk repr
end bitvec
protected instance bitvec.ult.decidable {n : ℕ} {x : bitvec n} {y : bitvec n} :
Decidable (bitvec.ult x y) :=
bool.decidable_eq (bitvec.uborrow x y) tt
protected instance bitvec.ugt.decidable {n : ℕ} {x : bitvec n} {y : bitvec n} :
Decidable (bitvec.ugt x y) :=
bool.decidable_eq (bitvec.uborrow y x) tt
end Mathlib |
a697b954962e16ade4ee70ec567b14e9ea41c927 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/algebra/gcd_monoid/multiset.lean | a97dc728e058e40673ae1af73a7a5981e41371fd | [
"Apache-2.0"
] | permissive | Vierkantor/mathlib | 0ea59ac32a3a43c93c44d70f441c4ee810ccceca | 83bc3b9ce9b13910b57bda6b56222495ebd31c2f | refs/heads/master | 1,658,323,012,449 | 1,652,256,003,000 | 1,652,256,003,000 | 209,296,341 | 0 | 1 | Apache-2.0 | 1,568,807,655,000 | 1,568,807,655,000 | null | UTF-8 | Lean | false | false | 5,390 | 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.gcd_monoid.basic
import data.multiset.lattice
/-!
# GCD and LCM operations on multisets
## Main definitions
- `multiset.gcd` - the greatest common denominator of a `multiset` of elements of a `gcd_monoid`
- `multiset.lcm` - the least common multiple of a `multiset` of elements of a `gcd_monoid`
## Implementation notes
TODO: simplify with a tactic and `data.multiset.lattice`
## Tags
multiset, gcd
-/
namespace multiset
variables {α : Type*} [cancel_comm_monoid_with_zero α] [normalized_gcd_monoid α]
/-! ### lcm -/
section lcm
/-- Least common multiple of a multiset -/
def lcm (s : multiset α) : α := s.fold gcd_monoid.lcm 1
@[simp] lemma lcm_zero : (0 : multiset α).lcm = 1 :=
fold_zero _ _
@[simp] lemma lcm_cons (a : α) (s : multiset α) :
(a ::ₘ s).lcm = gcd_monoid.lcm a s.lcm :=
fold_cons_left _ _ _ _
@[simp] lemma lcm_singleton {a : α} : ({a} : multiset α).lcm = normalize a :=
(fold_singleton _ _ _).trans $ lcm_one_right _
@[simp] lemma lcm_add (s₁ s₂ : multiset α) : (s₁ + s₂).lcm = gcd_monoid.lcm s₁.lcm s₂.lcm :=
eq.trans (by simp [lcm]) (fold_add _ _ _ _ _)
lemma lcm_dvd {s : multiset α} {a : α} : s.lcm ∣ a ↔ (∀ b ∈ s, b ∣ a) :=
multiset.induction_on s (by simp)
(by simp [or_imp_distrib, forall_and_distrib, lcm_dvd_iff] {contextual := tt})
lemma dvd_lcm {s : multiset α} {a : α} (h : a ∈ s) : a ∣ s.lcm :=
lcm_dvd.1 dvd_rfl _ h
lemma lcm_mono {s₁ s₂ : multiset α} (h : s₁ ⊆ s₂) : s₁.lcm ∣ s₂.lcm :=
lcm_dvd.2 $ assume b hb, dvd_lcm (h hb)
@[simp] lemma normalize_lcm (s : multiset α) : normalize (s.lcm) = s.lcm :=
multiset.induction_on s (by simp) $ λ a s IH, by simp
@[simp] theorem lcm_eq_zero_iff [nontrivial α] (s : multiset α) : s.lcm = 0 ↔ (0 : α) ∈ s :=
begin
induction s using multiset.induction_on with a s ihs,
{ simp only [lcm_zero, one_ne_zero, not_mem_zero] },
{ simp only [mem_cons, lcm_cons, lcm_eq_zero_iff, ihs, @eq_comm _ a] },
end
variables [decidable_eq α]
@[simp] lemma lcm_dedup (s : multiset α) : (dedup s).lcm = s.lcm :=
multiset.induction_on s (by simp) $ λ a s IH, begin
by_cases a ∈ s; simp [IH, h],
unfold lcm,
rw [← cons_erase h, fold_cons_left, ← lcm_assoc, lcm_same],
apply lcm_eq_of_associated_left (associated_normalize _),
end
@[simp] lemma lcm_ndunion (s₁ s₂ : multiset α) :
(ndunion s₁ s₂).lcm = gcd_monoid.lcm s₁.lcm s₂.lcm :=
by { rw [← lcm_dedup, dedup_ext.2, lcm_dedup, lcm_add], simp }
@[simp] lemma lcm_union (s₁ s₂ : multiset α) :
(s₁ ∪ s₂).lcm = gcd_monoid.lcm s₁.lcm s₂.lcm :=
by { rw [← lcm_dedup, dedup_ext.2, lcm_dedup, lcm_add], simp }
@[simp] lemma lcm_ndinsert (a : α) (s : multiset α) :
(ndinsert a s).lcm = gcd_monoid.lcm a s.lcm :=
by { rw [← lcm_dedup, dedup_ext.2, lcm_dedup, lcm_cons], simp }
end lcm
/-! ### gcd -/
section gcd
/-- Greatest common divisor of a multiset -/
def gcd (s : multiset α) : α := s.fold gcd_monoid.gcd 0
@[simp] lemma gcd_zero : (0 : multiset α).gcd = 0 :=
fold_zero _ _
@[simp] lemma gcd_cons (a : α) (s : multiset α) :
(a ::ₘ s).gcd = gcd_monoid.gcd a s.gcd :=
fold_cons_left _ _ _ _
@[simp] lemma gcd_singleton {a : α} : ({a} : multiset α).gcd = normalize a :=
(fold_singleton _ _ _).trans $ gcd_zero_right _
@[simp] lemma gcd_add (s₁ s₂ : multiset α) : (s₁ + s₂).gcd = gcd_monoid.gcd s₁.gcd s₂.gcd :=
eq.trans (by simp [gcd]) (fold_add _ _ _ _ _)
lemma dvd_gcd {s : multiset α} {a : α} : a ∣ s.gcd ↔ (∀ b ∈ s, a ∣ b) :=
multiset.induction_on s (by simp)
(by simp [or_imp_distrib, forall_and_distrib, dvd_gcd_iff] {contextual := tt})
lemma gcd_dvd {s : multiset α} {a : α} (h : a ∈ s) : s.gcd ∣ a :=
dvd_gcd.1 dvd_rfl _ h
lemma gcd_mono {s₁ s₂ : multiset α} (h : s₁ ⊆ s₂) : s₂.gcd ∣ s₁.gcd :=
dvd_gcd.2 $ assume b hb, gcd_dvd (h hb)
@[simp] lemma normalize_gcd (s : multiset α) : normalize (s.gcd) = s.gcd :=
multiset.induction_on s (by simp) $ λ a s IH, by simp
theorem gcd_eq_zero_iff (s : multiset α) : s.gcd = 0 ↔ ∀ (x : α), x ∈ s → x = 0 :=
begin
split,
{ intros h x hx,
apply eq_zero_of_zero_dvd,
rw ← h,
apply gcd_dvd hx },
{ apply s.induction_on,
{ simp },
intros a s sgcd h,
simp [h a (mem_cons_self a s), sgcd (λ x hx, h x (mem_cons_of_mem hx))] }
end
variables [decidable_eq α]
@[simp] lemma gcd_dedup (s : multiset α) : (dedup s).gcd = s.gcd :=
multiset.induction_on s (by simp) $ λ a s IH, begin
by_cases a ∈ s; simp [IH, h],
unfold gcd,
rw [← cons_erase h, fold_cons_left, ← gcd_assoc, gcd_same],
apply (associated_normalize _).gcd_eq_left,
end
@[simp] lemma gcd_ndunion (s₁ s₂ : multiset α) :
(ndunion s₁ s₂).gcd = gcd_monoid.gcd s₁.gcd s₂.gcd :=
by { rw [← gcd_dedup, dedup_ext.2, gcd_dedup, gcd_add], simp }
@[simp] lemma gcd_union (s₁ s₂ : multiset α) :
(s₁ ∪ s₂).gcd = gcd_monoid.gcd s₁.gcd s₂.gcd :=
by { rw [← gcd_dedup, dedup_ext.2, gcd_dedup, gcd_add], simp }
@[simp] lemma gcd_ndinsert (a : α) (s : multiset α) :
(ndinsert a s).gcd = gcd_monoid.gcd a s.gcd :=
by { rw [← gcd_dedup, dedup_ext.2, gcd_dedup, gcd_cons], simp }
end gcd
end multiset
|
116b313f21756d697de1e76330a22a984c6ed307 | 54deab7025df5d2df4573383df7e1e5497b7a2c2 | /data/seq/computation.lean | 375c0e6bb684f77d0fb0b261c5f97227837917cd | [
"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 | 34,538 | lean | /-
Copyright (c) 2017 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Mario Carneiro
Coinductive formalization of unbounded computations.
-/
import data.stream data.nat.basic
universes u v w
/-
coinductive computation (α : Type u) : Type u
| return : α → computation α
| think : computation α → computation α
-/
def computation (α : Type u) : Type u :=
{ f : stream (option α) // ∀ {n a}, f n = some a → f (n+1) = some a }
namespace computation
variables {α : Type u} {β : Type v} {γ : Type w}
-- constructors
def return (a : α) : computation α := ⟨stream.const (some a), λn a', id⟩
instance : has_coe α (computation α) := ⟨return⟩
def think (c : computation α) : computation α :=
⟨none :: c.1, λn a h, by {cases n with n, contradiction, exact c.2 h}⟩
def thinkN (c : computation α) : ℕ → computation α
| 0 := c
| (n+1) := think (thinkN n)
-- check for immediate result
def head (c : computation α) : option α := c.1.head
-- one step of computation
def tail (c : computation α) : computation α :=
⟨c.1.tail, λ n a, let t := c.2 in t⟩
def empty (α) : computation α := ⟨stream.const none, λn a', id⟩
def run_for : computation α → ℕ → option α := subtype.val
def destruct (s : computation α) : α ⊕ computation α :=
match s.1 0 with
| none := sum.inr (tail s)
| some a := sum.inl a
end
meta def run : computation α → α | c :=
match destruct c with
| sum.inl a := a
| sum.inr ca := run ca
end
theorem destruct_eq_ret {s : computation α} {a : α} :
destruct s = sum.inl a → s = return a :=
begin
dsimp [destruct],
ginduction s.1 0 with f0; intro h,
{ contradiction },
{ apply subtype.eq, apply funext,
dsimp [return], intro n,
induction n with n IH,
{ injection h, rwa h at f0 },
{ exact s.2 IH } }
end
theorem destruct_eq_think {s : computation α} {s'} :
destruct s = sum.inr s' → s = think s' :=
begin
dsimp [destruct],
ginduction s.1 0 with f0 a'; intro h,
{ injection h, rw ←h,
cases s with f al,
apply subtype.eq, dsimp [think, tail],
rw ←f0, exact (stream.eta f).symm },
{ contradiction }
end
@[simp] theorem destruct_ret (a : α) : destruct (return a) = sum.inl a := rfl
@[simp] theorem destruct_think : ∀ s : computation α, destruct (think s) = sum.inr s
| ⟨f, al⟩ := rfl
@[simp] theorem destruct_empty : destruct (empty α) = sum.inr (empty α) := rfl
@[simp] theorem head_ret (a : α) : head (return a) = some a := rfl
@[simp] theorem head_think (s : computation α) : head (think s) = none := rfl
@[simp] theorem head_empty : head (empty α) = none := rfl
@[simp] theorem tail_ret (a : α) : tail (return a) = return a := rfl
@[simp] theorem tail_think (s : computation α) : tail (think s) = s :=
by cases s with f al; apply subtype.eq; dsimp [tail, think]; rw [stream.tail_cons]
@[simp] theorem tail_empty : tail (empty α) = empty α := rfl
theorem think_empty : empty α = think (empty α) :=
destruct_eq_think destruct_empty
def cases_on {C : computation α → Sort v} (s : computation α)
(h1 : ∀ a, C (return a)) (h2 : ∀ s, C (think s)) : C s := begin
ginduction destruct s with H,
{ rw destruct_eq_ret H, apply h1 },
{ cases a with a s', rw destruct_eq_think H, apply h2 }
end
def corec.F (f : β → α ⊕ β) : α ⊕ β → option α × (α ⊕ β)
| (sum.inl a) := (some a, sum.inl a)
| (sum.inr b) := (match f b with
| sum.inl a := some a
| sum.inr b' := none
end, f b)
def corec (f : β → α ⊕ β) (b : β) : computation α :=
begin
refine ⟨stream.corec' (corec.F f) (sum.inr b), λn a' h, _⟩,
rw stream.corec'_eq,
change stream.corec' (corec.F f) (corec.F f (sum.inr b)).2 n = some a',
revert h, generalize : sum.inr b = o, revert o,
induction n with n IH; intro o,
{ change (corec.F f o).1 = some a' → (corec.F f (corec.F f o).2).1 = some a',
cases o with a b; intro h, { exact h },
dsimp [corec.F] at h, dsimp [corec.F],
cases f b with a b', { exact h },
{ contradiction } },
{ rw [stream.corec'_eq (corec.F f) (corec.F f o).2,
stream.corec'_eq (corec.F f) o],
exact IH (corec.F f o).2 }
end
def lmap (f : α → β) : α ⊕ γ → β ⊕ γ
| (sum.inl a) := sum.inl (f a)
| (sum.inr b) := sum.inr b
def rmap (f : β → γ) : α ⊕ β → α ⊕ γ
| (sum.inl a) := sum.inl a
| (sum.inr b) := sum.inr (f b)
attribute [simp] lmap rmap
@[simp] def corec_eq (f : β → α ⊕ β) (b : β) :
destruct (corec f b) = rmap (corec f) (f b) :=
begin
dsimp [corec, destruct],
change stream.corec' (corec.F f) (sum.inr b) 0 with corec.F._match_1 (f b),
ginduction f b with h a b', { refl },
dsimp [corec.F, destruct],
apply congr_arg, apply subtype.eq,
dsimp [corec, tail],
rw [stream.corec'_eq, stream.tail_cons],
dsimp [corec.F], rw h
end
section bisim
variable (R : computation α → computation α → Prop)
local infix ~ := R
def bisim_o : α ⊕ computation α → α ⊕ computation α → Prop
| (sum.inl a) (sum.inl a') := a = a'
| (sum.inr s) (sum.inr s') := R s s'
| _ _ := false
attribute [simp] bisim_o
def is_bisimulation := ∀ ⦃s₁ s₂⦄, s₁ ~ s₂ → bisim_o R (destruct s₁) (destruct s₂)
-- If two computations are bisimilar, then they are equal
theorem eq_of_bisim (bisim : is_bisimulation R) {s₁ s₂} (r : s₁ ~ s₂) : s₁ = s₂ :=
begin
apply subtype.eq,
apply stream.eq_of_bisim (λx y, ∃ s s' : computation α, s.1 = x ∧ s'.1 = y ∧ R s s'),
dsimp [stream.is_bisimulation],
intros t₁ t₂ e,
exact match t₁, t₂, e with ._, ._, ⟨s, s', rfl, rfl, r⟩ :=
suffices head s = head s' ∧ R (tail s) (tail s'), from
and.imp id (λr, ⟨tail s, tail s',
by cases s; refl, by cases s'; refl, r⟩) this,
begin
have := bisim r, revert r this,
apply cases_on s _ _; intros; apply cases_on s' _ _; intros; intros r this,
{ constructor, dsimp at this, rw this, assumption },
{ rw [destruct_ret, destruct_think] at this,
exact false.elim this },
{ rw [destruct_ret, destruct_think] at this,
exact false.elim this },
{ simp at this, simp [*] }
end
end,
exact ⟨s₁, s₂, rfl, rfl, r⟩
end
end bisim
-- It's more of a stretch to use ∈ for this relation, but it
-- asserts that the computation limits to the given value.
protected def mem (a : α) (s : computation α) := some a ∈ s.1
instance : has_mem α (computation α) :=
⟨computation.mem⟩
theorem le_stable (s : computation α) {a m n} (h : m ≤ n) :
s.1 m = some a → s.1 n = some a :=
by {cases s with f al, induction h with n h IH, exacts [id, λ h2, al (IH h2)]}
theorem mem_unique :
relator.left_unique ((∈) : α → computation α → Prop) :=
λa s b ⟨m, ha⟩ ⟨n, hb⟩, by injection
(le_stable s (le_max_left m n) ha.symm).symm.trans
(le_stable s (le_max_right m n) hb.symm)
@[class] def terminates (s : computation α) : Prop := ∃ a, a ∈ s
theorem terminates_of_mem {s : computation α} {a : α} : a ∈ s → terminates s :=
exists.intro a
theorem terminates_def (s : computation α) : terminates s ↔ ∃ n, (s.1 n).is_some :=
⟨λ⟨a, n, h⟩, ⟨n, by {dsimp [stream.nth] at h, rw ←h, exact rfl}⟩,
λ⟨n, h⟩, ⟨option.get h, n, (option.eq_some_of_is_some h).symm⟩⟩
theorem ret_mem (a : α) : a ∈ return a :=
exists.intro 0 rfl
theorem eq_of_ret_mem {a a' : α} (h : a' ∈ return a) : a' = a :=
mem_unique h (ret_mem _)
instance ret_terminates (a : α) : terminates (return a) :=
terminates_of_mem (ret_mem _)
theorem think_mem {s : computation α} {a} : a ∈ s → a ∈ think s
| ⟨n, h⟩ := ⟨n+1, h⟩
instance think_terminates (s : computation α) :
∀ [terminates s], terminates (think s)
| ⟨a, n, h⟩ := ⟨a, n+1, h⟩
theorem of_think_mem {s : computation α} {a} : a ∈ think s → a ∈ s
| ⟨n, h⟩ := by {cases n with n', contradiction, exact ⟨n', h⟩}
theorem of_think_terminates {s : computation α} :
terminates (think s) → terminates s
| ⟨a, h⟩ := ⟨a, of_think_mem h⟩
theorem not_mem_empty (a : α) : a ∉ empty α :=
λ ⟨n, h⟩, by clear _fun_match; contradiction
theorem not_terminates_empty : ¬ terminates (empty α) :=
λ ⟨a, h⟩, not_mem_empty a h
theorem eq_empty_of_not_terminates {s} (H : ¬ terminates s) : s = empty α :=
begin
apply subtype.eq, apply funext, intro n,
ginduction s.val n with h, {refl},
refine absurd _ H, exact ⟨_, _, h.symm⟩
end
theorem thinkN_mem {s : computation α} {a} : ∀ n, a ∈ thinkN s n ↔ a ∈ s
| 0 := iff.rfl
| (n+1) := iff.trans ⟨of_think_mem, think_mem⟩ (thinkN_mem n)
instance thinkN_terminates (s : computation α) :
∀ [terminates s] n, terminates (thinkN s n)
| ⟨a, h⟩ n := ⟨a, (thinkN_mem n).2 h⟩
theorem of_thinkN_terminates (s : computation α) (n) :
terminates (thinkN s n) → terminates s
| ⟨a, h⟩ := ⟨a, (thinkN_mem _).1 h⟩
def promises (s : computation α) (a : α) : Prop := ∀ ⦃a'⦄, a' ∈ s → a = a'
infix ` ~> `:50 := promises
theorem mem_promises {s : computation α} {a : α} : a ∈ s → s ~> a :=
λ h a', mem_unique h
theorem empty_promises (a : α) : empty α ~> a :=
λ a' h, absurd h (not_mem_empty _)
section get
variables (s : computation α) [h : terminates s]
include s h
def length : ℕ := nat.find ((terminates_def _).1 h)
-- If a computation has a result, we can retrieve it
def get : α := option.get (nat.find_spec $ (terminates_def _).1 h)
def get_mem : get s ∈ s :=
exists.intro (length s) (option.eq_some_of_is_some _).symm
def get_eq_of_mem {a} : a ∈ s → get s = a :=
mem_unique (get_mem _)
def mem_of_get_eq {a} : get s = a → a ∈ s :=
by intro h; rw ←h; apply get_mem
@[simp] theorem get_think : get (think s) = get s :=
get_eq_of_mem _ $ let ⟨n, h⟩ := get_mem s in ⟨n+1, h⟩
@[simp] theorem get_thinkN (n) : get (thinkN s n) = get s :=
get_eq_of_mem _ $ (thinkN_mem _).2 (get_mem _)
def get_promises : s ~> get s := λ a, get_eq_of_mem _
def mem_of_promises {a} (p : s ~> a) : a ∈ s :=
by cases h with a' h; rw p h; exact h
def get_eq_of_promises {a} : s ~> a → get s = a :=
get_eq_of_mem _ ∘ mem_of_promises _
end get
def results (s : computation α) (a : α) (n : ℕ) :=
∃ (h : a ∈ s), @length _ s (terminates_of_mem h) = n
def results_of_terminates (s : computation α) [T : terminates s] :
results s (get s) (length s) :=
⟨get_mem _, rfl⟩
def results_of_terminates' (s : computation α) [T : terminates s] {a} (h : a ∈ s) :
results s a (length s) :=
by rw ←get_eq_of_mem _ h; apply results_of_terminates
def results.mem {s : computation α} {a n} : results s a n → a ∈ s
| ⟨m, _⟩ := m
def results.terminates {s : computation α} {a n} (h : results s a n) : terminates s :=
terminates_of_mem h.mem
def results.length {s : computation α} {a n} [T : terminates s] :
results s a n → length s = n
| ⟨_, h⟩ := h
def results.val_unique {s : computation α} {a b m n}
(h1 : results s a m) (h2 : results s b n) : a = b :=
mem_unique h1.mem h2.mem
def results.len_unique {s : computation α} {a b m n}
(h1 : results s a m) (h2 : results s b n) : m = n :=
by have := h1.terminates; have := h2.terminates; rw [←h1.length, h2.length]
def exists_results_of_mem {s : computation α} {a} (h : a ∈ s) : ∃ n, results s a n :=
by have := terminates_of_mem h; have := results_of_terminates' s h; exact ⟨_, this⟩
@[simp] theorem get_ret (a : α) : get (return a) = a :=
get_eq_of_mem _ ⟨0, rfl⟩
@[simp] theorem length_ret (a : α) : length (return a) = 0 :=
let h := computation.ret_terminates a in
nat.eq_zero_of_le_zero $ nat.find_min' ((terminates_def (return a)).1 h) rfl
theorem results_ret (a : α) : results (return a) a 0 :=
⟨_, length_ret _⟩
@[simp] theorem length_think (s : computation α) [h : terminates s] :
length (think s) = length s + 1 :=
begin
apply le_antisymm,
{ exact nat.find_min' _ (nat.find_spec ((terminates_def _).1 h)) },
{ have : (option.is_some ((think s).val (length (think s))) : Prop) :=
nat.find_spec ((terminates_def _).1 s.think_terminates),
cases length (think s) with n,
{ contradiction },
{ apply nat.succ_le_succ, apply nat.find_min', apply this } }
end
theorem results_think {s : computation α} {a n}
(h : results s a n) : results (think s) a (n + 1) :=
by have := h.terminates; exact ⟨think_mem h.mem, by rw [length_think, h.length]⟩
theorem of_results_think {s : computation α} {a n}
(h : results (think s) a n) : ∃ m, results s a m ∧ n = m + 1 :=
begin
have := of_think_terminates h.terminates,
have := results_of_terminates' _ (of_think_mem h.mem),
exact ⟨_, this, results.len_unique h (results_think this)⟩,
end
@[simp] theorem results_think_iff {s : computation α} {a n} :
results (think s) a (n + 1) ↔ results s a n :=
⟨λ h, let ⟨n', r, e⟩ := of_results_think h in by injection e; rwa h,
results_think⟩
theorem results_thinkN {s : computation α} {a m} :
∀ n, results s a m → results (thinkN s n) a (m + n)
| 0 h := h
| (n+1) h := results_think (results_thinkN n h)
theorem results_thinkN_ret (a : α) (n) : results (thinkN (return a) n) a n :=
by have := results_thinkN n (results_ret a); rwa zero_add at this
@[simp] theorem length_thinkN (s : computation α) [h : terminates s] (n) :
length (thinkN s n) = length s + n :=
(results_thinkN n (results_of_terminates _)).length
def eq_thinkN {s : computation α} {a n} (h : results s a n) :
s = thinkN (return a) n :=
begin
revert s,
induction n with n IH; intro s;
apply cases_on s (λ a', _) (λ s, _); intro h,
{ rw ←eq_of_ret_mem h.mem, refl },
{ cases of_results_think h with n h, cases h, contradiction },
{ have := h.len_unique (results_ret _), contradiction },
{ rw IH (results_think_iff.1 h), refl }
end
def eq_thinkN' (s : computation α) [h : terminates s] :
s = thinkN (return (get s)) (length s) :=
eq_thinkN (results_of_terminates _)
def mem_rec_on {C : computation α → Sort v} {a s} (M : a ∈ s)
(h1 : C (return a)) (h2 : ∀ s, C s → C (think s)) : C s :=
begin
have T := terminates_of_mem M,
rw [eq_thinkN' s, get_eq_of_mem s M],
generalize : length s = n,
induction n with n IH, exacts [h1, h2 _ IH]
end
def terminates_rec_on {C : computation α → Sort v} (s) [terminates s]
(h1 : ∀ a, C (return a)) (h2 : ∀ s, C s → C (think s)) : C s :=
mem_rec_on (get_mem s) (h1 _) h2
def map (f : α → β) : computation α → computation β
| ⟨s, al⟩ := ⟨s.map (λo, option.cases_on o none (some ∘ f)),
λn b, begin
dsimp [stream.map, stream.nth],
ginduction s n with e a; intro h,
{ contradiction }, { rw [al e, ←h] }
end⟩
def bind.G : β ⊕ computation β → β ⊕ computation α ⊕ computation β
| (sum.inl b) := sum.inl b
| (sum.inr cb') := sum.inr $ sum.inr cb'
def bind.F (f : α → computation β) :
computation α ⊕ computation β → β ⊕ computation α ⊕ computation β
| (sum.inl ca) :=
match destruct ca with
| sum.inl a := bind.G $ destruct (f a)
| sum.inr ca' := sum.inr $ sum.inl ca'
end
| (sum.inr cb) := bind.G $ destruct cb
def bind (c : computation α) (f : α → computation β) : computation β :=
corec (bind.F f) (sum.inl c)
instance : has_bind computation := ⟨@bind⟩
theorem has_bind_eq_bind {β} (c : computation α) (f : α → computation β) :
c >>= f = bind c f := rfl
def join (c : computation (computation α)) : computation α := c >>= id
@[simp] theorem map_ret (f : α → β) (a) : map f (return a) = return (f a) := rfl
@[simp] theorem map_think (f : α → β) : ∀ s, map f (think s) = think (map f s)
| ⟨s, al⟩ := by apply subtype.eq; dsimp [think, map]; rw stream.map_cons
@[simp] theorem destruct_map (f : α → β) (s) : destruct (map f s) = lmap f (rmap (map f) (destruct s)) :=
by apply s.cases_on; intro; simp
@[simp] theorem map_id : ∀ (s : computation α), map id s = s
| ⟨f, al⟩ := begin
apply subtype.eq; simp [map, function.comp],
have e : (@option.rec α (λ_, option α) none some) = id,
{ apply funext, intro, cases x; refl },
simp [e, stream.map_id]
end
theorem map_comp (f : α → β) (g : β → γ) :
∀ (s : computation α), map (g ∘ f) s = map g (map f s)
| ⟨s, al⟩ := begin
apply subtype.eq; dsimp [map],
rw stream.map_map,
apply congr_arg (λ f : _ → option γ, stream.map f s),
apply funext, intro, cases x with x; refl
end
@[simp] theorem ret_bind (a) (f : α → computation β) :
bind (return a) f = f a :=
begin
apply eq_of_bisim (λc1 c2,
c1 = bind (return a) f ∧ c2 = f a ∨
c1 = corec (bind.F f) (sum.inr c2)),
{ intros c1 c2 h,
exact match c1, c2, h with
| ._, ._, or.inl ⟨rfl, rfl⟩ := begin
simp [bind, bind.F],
cases destruct (f a) with b cb; simp [bind.G]
end
| ._, c, or.inr rfl := begin
simp [bind.F],
cases destruct c with b cb; simp [bind.G]
end end },
{ simp }
end
@[simp] theorem think_bind (c) (f : α → computation β) :
bind (think c) f = think (bind c f) :=
destruct_eq_think $ by simp [bind, bind.F]
@[simp] theorem bind_ret (f : α → β) (s) : bind s (return ∘ f) = map f s :=
begin
apply eq_of_bisim (λc1 c2, c1 = c2 ∨
∃ s, c1 = bind s (return ∘ f) ∧ c2 = map f s),
{ intros c1 c2 h,
exact match c1, c2, h with
| c, ._, or.inl rfl := by cases destruct c with b cb; simp
| ._, ._, or.inr ⟨s, rfl, rfl⟩ := begin
apply cases_on s; intros s; simp,
exact or.inr ⟨s, rfl, rfl⟩
end end },
{ exact or.inr ⟨s, rfl, rfl⟩ }
end
@[simp] theorem bind_ret' (s : computation α) : bind s return = s :=
by rw bind_ret; change (λ x : α, x) with @id α; rw map_id
@[simp] theorem bind_assoc (s : computation α) (f : α → computation β) (g : β → computation γ) :
bind (bind s f) g = bind s (λ (x : α), bind (f x) g) :=
begin
apply eq_of_bisim (λc1 c2, c1 = c2 ∨
∃ s, c1 = bind (bind s f) g ∧ c2 = bind s (λ (x : α), bind (f x) g)),
{ intros c1 c2 h,
exact match c1, c2, h with
| c, ._, or.inl rfl := by cases destruct c with b cb; simp
| ._, ._, or.inr ⟨s, rfl, rfl⟩ := begin
apply cases_on s; intros s; simp,
{ generalize : f s = fs,
apply cases_on fs; intros t; simp,
{ cases destruct (g t) with b cb; simp } },
{ exact or.inr ⟨s, rfl, rfl⟩ }
end end },
{ exact or.inr ⟨s, rfl, rfl⟩ }
end
theorem results_bind {s : computation α} {f : α → computation β} {a b m n}
(h1 : results s a m) (h2 : results (f a) b n) : results (bind s f) b (n + m) :=
begin
have := h1.mem, revert m,
apply mem_rec_on this _ (λ s IH, _); intros m h1,
{ rw [ret_bind], rw h1.len_unique (results_ret _), exact h2 },
{ rw [think_bind], cases of_results_think h1 with m' h, cases h with h1 e,
rw e, exact results_think (IH h1) }
end
theorem mem_bind {s : computation α} {f : α → computation β} {a b}
(h1 : a ∈ s) (h2 : b ∈ f a) : b ∈ bind s f :=
let ⟨m, h1⟩ := exists_results_of_mem h1,
⟨n, h2⟩ := exists_results_of_mem h2 in (results_bind h1 h2).mem
instance terminates_bind (s : computation α) (f : α → computation β)
[terminates s] [terminates (f (get s))] :
terminates (bind s f) :=
terminates_of_mem (mem_bind (get_mem s) (get_mem (f (get s))))
@[simp] theorem get_bind (s : computation α) (f : α → computation β)
[terminates s] [terminates (f (get s))] :
get (bind s f) = get (f (get s)) :=
get_eq_of_mem _ (mem_bind (get_mem s) (get_mem (f (get s))))
@[simp] theorem length_bind (s : computation α) (f : α → computation β)
[T1 : terminates s] [T2 : terminates (f (get s))] :
length (bind s f) = length (f (get s)) + length s :=
(results_of_terminates _).len_unique $
results_bind (results_of_terminates _) (results_of_terminates _)
theorem of_results_bind {s : computation α} {f : α → computation β} {b k} :
results (bind s f) b k →
∃ a m n, results s a m ∧ results (f a) b n ∧ k = n + m :=
begin
revert s, induction k with n IH; intro s;
apply cases_on s (λ a, _) (λ s', _); intro e,
{ simp [thinkN] at e, refine ⟨a, _, _, results_ret _, e, rfl⟩ },
{ have := congr_arg head (eq_thinkN e), contradiction },
{ simp at e, refine ⟨a, _, n+1, results_ret _, e, rfl⟩ },
{ simp at e, exact let ⟨a, m, n', h1, h2, e'⟩ := IH e in
by rw e'; exact ⟨a, m.succ, n', results_think h1, h2, rfl⟩ }
end
theorem exists_of_mem_bind {s : computation α} {f : α → computation β} {b}
(h : b ∈ bind s f) : ∃ a ∈ s, b ∈ f a :=
let ⟨k, h⟩ := exists_results_of_mem h,
⟨a, m, n, h1, h2, e⟩ := of_results_bind h in ⟨a, h1.mem, h2.mem⟩
theorem bind_promises {s : computation α} {f : α → computation β} {a b}
(h1 : s ~> a) (h2 : f a ~> b) : bind s f ~> b :=
λ b' bB, begin
cases exists_of_mem_bind bB with a' a's, cases a's with a's ba',
rw ←h1 a's at ba', exact h2 ba'
end
instance : monad computation :=
{ map := @map,
pure := @return,
bind := @bind,
id_map := @map_id,
bind_pure_comp_eq_map := @bind_ret,
pure_bind := @ret_bind,
bind_assoc := @bind_assoc }
theorem has_map_eq_map {β} (f : α → β) (c : computation α) : f <$> c = map f c := rfl
@[simp] theorem return_def (a) : (_root_.return a : computation α) = return a := rfl
@[simp] theorem map_ret' {α β} : ∀ (f : α → β) (a), f <$> return a = return (f a) := map_ret
@[simp] theorem map_think' {α β} : ∀ (f : α → β) s, f <$> think s = think (f <$> s) := map_think
theorem mem_map (f : α → β) {a} {s : computation α} (m : a ∈ s) : f a ∈ map f s :=
by rw ←bind_ret; apply mem_bind m; apply ret_mem
theorem exists_of_mem_map {f : α → β} {b : β} {s : computation α} (h : b ∈ map f s) :
∃ a, a ∈ s ∧ f a = b :=
by rw ←bind_ret at h; exact
let ⟨a, as, fb⟩ := exists_of_mem_bind h in ⟨a, as, mem_unique (ret_mem _) fb⟩
instance terminates_map (f : α → β) (s : computation α) [terminates s] : terminates (map f s) :=
by rw ←bind_ret; apply_instance
theorem terminates_map_iff (f : α → β) (s : computation α) :
terminates (map f s) ↔ terminates s :=
⟨λ⟨a, h⟩, let ⟨b, h1, _⟩ := exists_of_mem_map h in ⟨_, h1⟩, @computation.terminates_map _ _ _ _⟩
-- Parallel computation
def orelse (c1 c2 : computation α) : computation α :=
@computation.corec α (computation α × computation α)
(λ⟨c1, c2⟩, match destruct c1 with
| sum.inl a := sum.inl a
| sum.inr c1' := match destruct c2 with
| sum.inl a := sum.inl a
| sum.inr c2' := sum.inr (c1', c2')
end
end) (c1, c2)
instance : alternative computation :=
{ computation.monad with
orelse := @orelse,
failure := @empty }
@[simp] theorem ret_orelse (a : α) (c2 : computation α) :
(return a <|> c2) = return a :=
destruct_eq_ret $ by unfold has_orelse.orelse; simp [orelse]
@[simp] theorem orelse_ret (c1 : computation α) (a : α) :
(think c1 <|> return a) = return a :=
destruct_eq_ret $ by unfold has_orelse.orelse; simp [orelse]
@[simp] theorem orelse_think (c1 c2 : computation α) :
(think c1 <|> think c2) = think (c1 <|> c2) :=
destruct_eq_think $ by unfold has_orelse.orelse; simp [orelse]
@[simp] theorem empty_orelse (c) : (empty α <|> c) = c :=
begin
apply eq_of_bisim (λc1 c2, (empty α <|> c2) = c1) _ rfl,
intros s' s h, rw ←h,
apply cases_on s; intros s; rw think_empty; simp,
rw ←think_empty,
end
@[simp] theorem orelse_empty (c : computation α) : (c <|> empty α) = c :=
begin
apply eq_of_bisim (λc1 c2, (c2 <|> empty α) = c1) _ rfl,
intros s' s h, rw ←h,
apply cases_on s; intros s; rw think_empty; simp,
rw←think_empty,
end
def equiv (c1 c2 : computation α) : Prop := ∀ a, a ∈ c1 ↔ a ∈ c2
infix ~ := equiv
@[refl] theorem equiv.refl (s : computation α) : s ~ s := λ_, iff.rfl
@[symm] theorem equiv.symm {s t : computation α} : s ~ t → t ~ s :=
λh a, (h a).symm
@[trans] theorem equiv.trans {s t u : computation α} : s ~ t → t ~ u → s ~ u :=
λh1 h2 a, (h1 a).trans (h2 a)
theorem equiv.equivalence : equivalence (@equiv α) :=
⟨@equiv.refl _, @equiv.symm _, @equiv.trans _⟩
theorem equiv_of_mem {s t : computation α} {a} (h1 : a ∈ s) (h2 : a ∈ t) : s ~ t :=
λa', ⟨λma, by rw mem_unique ma h1; exact h2,
λma, by rw mem_unique ma h2; exact h1⟩
theorem terminates_congr {c1 c2 : computation α}
(h : c1 ~ c2) : terminates c1 ↔ terminates c2 :=
exists_congr h
theorem promises_congr {c1 c2 : computation α}
(h : c1 ~ c2) (a) : c1 ~> a ↔ c2 ~> a :=
forall_congr (λa', imp_congr (h a') iff.rfl)
theorem get_equiv {c1 c2 : computation α} (h : c1 ~ c2)
[terminates c1] [terminates c2] : get c1 = get c2 :=
get_eq_of_mem _ $ (h _).2 $ get_mem _
theorem think_equiv (s : computation α) : think s ~ s :=
λ a, ⟨of_think_mem, think_mem⟩
theorem thinkN_equiv (s : computation α) (n) : thinkN s n ~ s :=
λ a, thinkN_mem n
theorem bind_congr {s1 s2 : computation α} {f1 f2 : α → computation β}
(h1 : s1 ~ s2) (h2 : ∀ a, f1 a ~ f2 a) : bind s1 f1 ~ bind s2 f2 :=
λ b, ⟨λh, let ⟨a, ha, hb⟩ := exists_of_mem_bind h in
mem_bind ((h1 a).1 ha) ((h2 a b).1 hb),
λh, let ⟨a, ha, hb⟩ := exists_of_mem_bind h in
mem_bind ((h1 a).2 ha) ((h2 a b).2 hb)⟩
theorem equiv_ret_of_mem {s : computation α} {a} (h : a ∈ s) : s ~ return a :=
equiv_of_mem h (ret_mem _)
def lift_rel (R : α → β → Prop) (ca : computation α) (cb : computation β) : Prop :=
(∀ {a}, a ∈ ca → ∃ {b}, b ∈ cb ∧ R a b) ∧
∀ {b}, b ∈ cb → ∃ {a}, a ∈ ca ∧ R a b
theorem lift_rel.swap (R : α → β → Prop) (ca : computation α) (cb : computation β) :
lift_rel (function.swap R) cb ca ↔ lift_rel R ca cb :=
and_comm _ _
theorem lift_eq_iff_equiv (c1 c2 : computation α) : lift_rel (=) c1 c2 ↔ c1 ~ c2 :=
⟨λ⟨h1, h2⟩ a,
⟨λ a1, let ⟨b, b2, ab⟩ := h1 a1 in by rwa ab,
λ a2, let ⟨b, b1, ab⟩ := h2 a2 in by rwa ←ab⟩,
λe, ⟨λ a a1, ⟨a, (e _).1 a1, rfl⟩, λ a a2, ⟨a, (e _).2 a2, rfl⟩⟩⟩
def lift_rel.refl (R : α → α → Prop) (H : reflexive R) : reflexive (lift_rel R) :=
λ s, ⟨λ a as, ⟨a, as, H a⟩, λ b bs, ⟨b, bs, H b⟩⟩
def lift_rel.symm (R : α → α → Prop) (H : symmetric R) : symmetric (lift_rel R) :=
λ s1 s2 ⟨l, r⟩,
⟨λ a a2, let ⟨b, b1, ab⟩ := r a2 in ⟨b, b1, H ab⟩,
λ a a1, let ⟨b, b2, ab⟩ := l a1 in ⟨b, b2, H ab⟩⟩
def lift_rel.trans (R : α → α → Prop) (H : transitive R) : transitive (lift_rel R) :=
λ s1 s2 s3 ⟨l1, r1⟩ ⟨l2, r2⟩,
⟨λ a a1, let ⟨b, b2, ab⟩ := l1 a1, ⟨c, c3, bc⟩ := l2 b2 in ⟨c, c3, H ab bc⟩,
λ c c3, let ⟨b, b2, bc⟩ := r2 c3, ⟨a, a1, ab⟩ := r1 b2 in ⟨a, a1, H ab bc⟩⟩
def lift_rel.equiv (R : α → α → Prop) : equivalence R → equivalence (lift_rel R)
| ⟨refl, symm, trans⟩ :=
⟨lift_rel.refl R refl, lift_rel.symm R symm, lift_rel.trans R trans⟩
def lift_rel.imp {R S : α → β → Prop} (H : ∀ {a b}, R a b → S a b) (s t) :
lift_rel R s t → lift_rel S s t | ⟨l, r⟩ :=
⟨λ a as, let ⟨b, bt, ab⟩ := l as in ⟨b, bt, H ab⟩,
λ b bt, let ⟨a, as, ab⟩ := r bt in ⟨a, as, H ab⟩⟩
def terminates_of_lift_rel {R : α → β → Prop} {s t} :
lift_rel R s t → (terminates s ↔ terminates t) | ⟨l, r⟩ :=
⟨λ ⟨a, as⟩, let ⟨b, bt, ab⟩ := l as in ⟨b, bt⟩,
λ ⟨b, bt⟩, let ⟨a, as, ab⟩ := r bt in ⟨a, as⟩⟩
def rel_of_lift_rel {R : α → β → Prop} {ca cb} :
lift_rel R ca cb → ∀ {a b}, a ∈ ca → b ∈ cb → R a b
| ⟨l, r⟩ a b ma mb :=
let ⟨b', mb', ab'⟩ := l ma in by rw mem_unique mb mb'; exact ab'
theorem lift_rel_of_mem {R : α → β → Prop} {a b ca cb}
(ma : a ∈ ca) (mb : b ∈ cb) (ab : R a b) : lift_rel R ca cb :=
⟨λ a' ma', by rw mem_unique ma' ma; exact ⟨b, mb, ab⟩,
λ b' mb', by rw mem_unique mb' mb; exact ⟨a, ma, ab⟩⟩
theorem exists_of_lift_rel_left {R : α → β → Prop} {ca cb}
(H : lift_rel R ca cb) {a} (h : a ∈ ca) : ∃ {b}, b ∈ cb ∧ R a b :=
H.left h
theorem exists_of_lift_rel_right {R : α → β → Prop} {ca cb}
(H : lift_rel R ca cb) {b} (h : b ∈ cb) : ∃ {a}, a ∈ ca ∧ R a b :=
H.right h
theorem lift_rel_def {R : α → β → Prop} {ca cb} : lift_rel R ca cb ↔
(terminates ca ↔ terminates cb) ∧ ∀ {a b}, a ∈ ca → b ∈ cb → R a b :=
⟨λh, ⟨terminates_of_lift_rel h, λ a b ma mb,
let ⟨b', mb', ab⟩ := h.left ma in by rwa mem_unique mb mb'⟩,
λ⟨l, r⟩,
⟨λ a ma, let ⟨b, mb⟩ := l.1 ⟨_, ma⟩ in ⟨b, mb, r ma mb⟩,
λ b mb, let ⟨a, ma⟩ := l.2 ⟨_, mb⟩ in ⟨a, ma, r ma mb⟩⟩⟩
theorem lift_rel_bind {δ} (R : α → β → Prop) (S : γ → δ → Prop)
{s1 : computation α} {s2 : computation β}
{f1 : α → computation γ} {f2 : β → computation δ}
(h1 : lift_rel R s1 s2) (h2 : ∀ {a b}, R a b → lift_rel S (f1 a) (f2 b))
: lift_rel S (bind s1 f1) (bind s2 f2) :=
let ⟨l1, r1⟩ := h1 in
⟨λ c cB,
let ⟨a, a1, c1⟩ := exists_of_mem_bind cB,
⟨b, b2, ab⟩ := l1 a1,
⟨l2, r2⟩ := h2 ab,
⟨d, d2, cd⟩ := l2 c1 in
⟨_, mem_bind b2 d2, cd⟩,
λ d dB,
let ⟨b, b1, d1⟩ := exists_of_mem_bind dB,
⟨a, a2, ab⟩ := r1 b1,
⟨l2, r2⟩ := h2 ab,
⟨c, c2, cd⟩ := r2 d1 in
⟨_, mem_bind a2 c2, cd⟩⟩
@[simp] theorem lift_rel_return_left (R : α → β → Prop) (a : α) (cb : computation β) :
lift_rel R (return a) cb ↔ ∃ {b}, b ∈ cb ∧ R a b :=
⟨λ⟨l, r⟩, l (ret_mem _),
λ⟨b, mb, ab⟩,
⟨λ a' ma', by rw eq_of_ret_mem ma'; exact ⟨b, mb, ab⟩,
λ b' mb', ⟨_, ret_mem _, by rw mem_unique mb' mb; exact ab⟩⟩⟩
@[simp] theorem lift_rel_return_right (R : α → β → Prop) (ca : computation α) (b : β) :
lift_rel R ca (return b) ↔ ∃ {a}, a ∈ ca ∧ R a b :=
by rw [lift_rel.swap, lift_rel_return_left]
@[simp] theorem lift_rel_return (R : α → β → Prop) (a : α) (b : β) :
lift_rel R (return a) (return b) ↔ R a b :=
by rw [lift_rel_return_left]; exact
⟨λ⟨b', mb', ab'⟩, by rwa eq_of_ret_mem mb' at ab',
λab, ⟨_, ret_mem _, ab⟩⟩
@[simp] theorem lift_rel_think_left (R : α → β → Prop) (ca : computation α) (cb : computation β) :
lift_rel R (think ca) cb ↔ lift_rel R ca cb :=
and_congr (forall_congr $ λb, imp_congr ⟨of_think_mem, think_mem⟩ iff.rfl)
(forall_congr $ λb, imp_congr iff.rfl $
exists_congr $ λ b, and_congr ⟨of_think_mem, think_mem⟩ iff.rfl)
@[simp] theorem lift_rel_think_right (R : α → β → Prop) (ca : computation α) (cb : computation β) :
lift_rel R ca (think cb) ↔ lift_rel R ca cb :=
by rw [←lift_rel.swap R, ←lift_rel.swap R]; apply lift_rel_think_left
theorem lift_rel_mem_cases {R : α → β → Prop} {ca cb}
(Ha : ∀ a ∈ ca, lift_rel R ca cb)
(Hb : ∀ b ∈ cb, lift_rel R ca cb) : lift_rel R ca cb :=
⟨λ a ma, (Ha _ ma).left ma, λ b mb, (Hb _ mb).right mb⟩
theorem lift_rel_congr {R : α → β → Prop} {ca ca' : computation α} {cb cb' : computation β}
(ha : ca ~ ca') (hb : cb ~ cb') : lift_rel R ca cb ↔ lift_rel R ca' cb' :=
and_congr
(forall_congr $ λ a, imp_congr (ha _) $ exists_congr $ λ b, and_congr (hb _) iff.rfl)
(forall_congr $ λ b, imp_congr (hb _) $ exists_congr $ λ a, and_congr (ha _) iff.rfl)
theorem lift_rel_map {δ} (R : α → β → Prop) (S : γ → δ → Prop)
{s1 : computation α} {s2 : computation β}
{f1 : α → γ} {f2 : β → δ}
(h1 : lift_rel R s1 s2) (h2 : ∀ {a b}, R a b → S (f1 a) (f2 b))
: lift_rel S (map f1 s1) (map f2 s2) :=
by rw [←bind_ret, ←bind_ret]; apply lift_rel_bind _ _ h1; simp; exact @h2
theorem map_congr (R : α → α → Prop) (S : β → β → Prop)
{s1 s2 : computation α} {f : α → β}
(h1 : s1 ~ s2) : map f s1 ~ map f s2 :=
by rw [←lift_eq_iff_equiv];
exact lift_rel_map eq _ ((lift_eq_iff_equiv _ _).2 h1) (λ a b, congr_arg _)
def lift_rel_aux (R : α → β → Prop)
(C : computation α → computation β → Prop) :
α ⊕ computation α → β ⊕ computation β → Prop
| (sum.inl a) (sum.inl b) := R a b
| (sum.inl a) (sum.inr cb) := ∃ {b}, b ∈ cb ∧ R a b
| (sum.inr ca) (sum.inl b) := ∃ {a}, a ∈ ca ∧ R a b
| (sum.inr ca) (sum.inr cb) := C ca cb
attribute [simp] lift_rel_aux
@[simp] def lift_rel_aux.ret_left (R : α → β → Prop)
(C : computation α → computation β → Prop) (a cb) :
lift_rel_aux R C (sum.inl a) (destruct cb) ↔ ∃ {b}, b ∈ cb ∧ R a b :=
begin
apply cb.cases_on (λ b, _) (λ cb, _),
{ exact ⟨λ h, ⟨_, ret_mem _, h⟩, λ ⟨b', mb, h⟩,
by rw [mem_unique (ret_mem _) mb]; exact h⟩ },
{ rw [destruct_think],
exact ⟨λ ⟨b, h, r⟩, ⟨b, think_mem h, r⟩,
λ ⟨b, h, r⟩, ⟨b, of_think_mem h, r⟩⟩ }
end
theorem lift_rel_aux.swap (R : α → β → Prop) (C) (a b) :
lift_rel_aux (function.swap R) (function.swap C) b a = lift_rel_aux R C a b :=
by cases a with a ca; cases b with b cb; simp only [lift_rel_aux]
@[simp] def lift_rel_aux.ret_right (R : α → β → Prop)
(C : computation α → computation β → Prop) (b ca) :
lift_rel_aux R C (destruct ca) (sum.inl b) ↔ ∃ {a}, a ∈ ca ∧ R a b :=
by rw [←lift_rel_aux.swap, lift_rel_aux.ret_left]
theorem lift_rel_rec.lem {R : α → β → Prop} (C : computation α → computation β → Prop)
(H : ∀ {ca cb}, C ca cb → lift_rel_aux R C (destruct ca) (destruct cb))
(ca cb) (Hc : C ca cb) (a) (ha : a ∈ ca) : lift_rel R ca cb :=
begin
revert cb, refine mem_rec_on ha _ (λ ca' IH, _);
intros cb Hc; have h := H Hc,
{ simp at h, simp [h] },
{ have h := H Hc, simp, revert h, apply cb.cases_on (λ b, _) (λ cb', _);
intro h; simp at h; simp [h], exact IH _ h }
end
theorem lift_rel_rec {R : α → β → Prop} (C : computation α → computation β → Prop)
(H : ∀ {ca cb}, C ca cb → lift_rel_aux R C (destruct ca) (destruct cb))
(ca cb) (Hc : C ca cb) : lift_rel R ca cb :=
lift_rel_mem_cases (lift_rel_rec.lem C @H ca cb Hc) (λ b hb,
(lift_rel.swap _ _ _).2 $
lift_rel_rec.lem (function.swap C)
(λ cb ca h, cast (lift_rel_aux.swap _ _ _ _).symm $ H h)
cb ca Hc b hb)
end computation
|
cf9fbfb702c340fc8ee24741e717444865530a02 | 9dd3f3912f7321eb58ee9aa8f21778ad6221f87c | /tests/lean/run/unification_hints.lean | 72e145b05a011eee05a9db7ea52e0f19a235e0a5 | [
"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 | 789 | lean | open list nat
namespace toy
constants (A : Type.{1}) (f h : A → A) (x y z : A)
attribute [irreducible]
noncomputable definition g (x y : A) : A := f z
@[unify]
noncomputable definition toy_hint (x y : A) : unification_hint :=
{ pattern := g x y ≟ f z,
constraints := [] }
open tactic
set_option trace.type_context.unification_hint true
definition ex1 (a : A) (H : g x y = a) : f z = a :=
by do {trace_state, assumption}
print ex1
end toy
namespace add
constants (n : ℕ)
attribute [irreducible] add
open tactic
@[unify]
definition add_zero_hint (m n : ℕ) [has_add ℕ] [has_one ℕ] [has_zero ℕ] : unification_hint :=
{ pattern := m + 1 ≟ succ n,
constraints := [m ≟ n] }
definition ex2 (H : n + 1 = 0) : succ n = 0 :=
by assumption
print ex2
end add
|
18118db08df975618c61fbb99aa86f7d6ded194d | 2a70b774d16dbdf5a533432ee0ebab6838df0948 | /_target/deps/mathlib/src/measure_theory/bochner_integration.lean | 148927603d93cdfe4d50743778416af0c2afbd22 | [
"Apache-2.0"
] | permissive | hjvromen/lewis | 40b035973df7c77ebf927afab7878c76d05ff758 | 105b675f73630f028ad5d890897a51b3c1146fb0 | refs/heads/master | 1,677,944,636,343 | 1,676,555,301,000 | 1,676,555,301,000 | 327,553,599 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 67,199 | 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
-/
import measure_theory.simple_func_dense
import analysis.normed_space.bounded_linear_maps
import topology.sequences
/-!
# Bochner integral
The Bochner integral extends the definition of the Lebesgue integral to functions that map from a
measure space into a Banach space (complete normed vector space). It is constructed here by
extending the integral on simple functions.
## Main definitions
The Bochner integral is defined following these steps:
1. Define the integral on simple functions of the type `simple_func α E` (notation : `α →ₛ E`)
where `E` is a real normed space.
(See `simple_func.bintegral` and section `bintegral` for details. Also see `simple_func.integral`
for the integral on simple functions of the type `simple_func α ennreal`.)
2. Use `α →ₛ E` to cut out the simple functions from L1 functions, and define integral
on these. The type of simple functions in L1 space is written as `α →₁ₛ[μ] E`.
3. Show that the embedding of `α →₁ₛ[μ] E` into L1 is a dense and uniform one.
4. Show that the integral defined on `α →₁ₛ[μ] E` is a continuous linear map.
5. Define the Bochner integral on L1 functions by extending the integral on integrable simple
functions `α →₁ₛ[μ] E` using `continuous_linear_map.extend`. Define the Bochner integral on
functions as the Bochner integral of its equivalence class in L1 space.
## Main statements
1. Basic properties of the Bochner integral on functions of type `α → E`, where `α` is a measure
space and `E` is a real normed space.
* `integral_zero` : `∫ 0 ∂μ = 0`
* `integral_add` : `∫ x, f x + g x ∂μ = ∫ x, f ∂μ + ∫ x, g x ∂μ`
* `integral_neg` : `∫ x, - f x ∂μ = - ∫ x, f x ∂μ`
* `integral_sub` : `∫ x, f x - g x ∂μ = ∫ x, f x ∂μ - ∫ x, g x ∂μ`
* `integral_smul` : `∫ x, r • f x ∂μ = r • ∫ x, f x ∂μ`
* `integral_congr_ae` : `f =ᵐ[μ] g → ∫ x, f x ∂μ = ∫ x, g x ∂μ`
* `norm_integral_le_integral_norm` : `∥∫ x, f x ∂μ∥ ≤ ∫ x, ∥f x∥ ∂μ`
2. Basic properties of the Bochner integral on functions of type `α → ℝ`, where `α` is a measure
space.
* `integral_nonneg_of_ae` : `0 ≤ᵐ[μ] f → 0 ≤ ∫ x, f x ∂μ`
* `integral_nonpos_of_ae` : `f ≤ᵐ[μ] 0 → ∫ x, f x ∂μ ≤ 0`
* `integral_mono_ae` : `f ≤ᵐ[μ] g → ∫ x, f x ∂μ ≤ ∫ x, g x ∂μ`
* `integral_nonneg` : `0 ≤ f → 0 ≤ ∫ x, f x ∂μ`
* `integral_nonpos` : `f ≤ 0 → ∫ x, f x ∂μ ≤ 0`
* `integral_mono` : `f ≤ᵐ[μ] g → ∫ x, f x ∂μ ≤ ∫ x, g x ∂μ`
3. Propositions connecting the Bochner integral with the integral on `ennreal`-valued functions,
which is called `lintegral` and has the notation `∫⁻`.
* `integral_eq_lintegral_max_sub_lintegral_min` : `∫ x, f x ∂μ = ∫⁻ x, f⁺ x ∂μ - ∫⁻ x, f⁻ x ∂μ`,
where `f⁺` is the positive part of `f` and `f⁻` is the negative part of `f`.
* `integral_eq_lintegral_of_nonneg_ae` : `0 ≤ᵐ[μ] f → ∫ x, f x ∂μ = ∫⁻ x, f x ∂μ`
4. `tendsto_integral_of_dominated_convergence` : the Lebesgue dominated convergence theorem
## Notes
Some tips on how to prove a proposition if the API for the Bochner integral is not enough so that
you need to unfold the definition of the Bochner integral and go back to simple functions.
One method is to use the theorem `integrable.induction` in the file `set_integral`, which allows
you to prove something for an arbitrary measurable + integrable function.
Another method is using the following steps.
See `integral_eq_lintegral_max_sub_lintegral_min` for a complicated example, which proves that
`∫ f = ∫⁻ f⁺ - ∫⁻ f⁻`, with the first integral sign being the Bochner integral of a real-valued
function `f : α → ℝ`, and second and third integral sign being the integral on ennreal-valued
functions (called `lintegral`). The proof of `integral_eq_lintegral_max_sub_lintegral_min` is
scattered in sections with the name `pos_part`.
Here are the usual steps of proving that a property `p`, say `∫ f = ∫⁻ f⁺ - ∫⁻ f⁻`, holds for all
functions :
1. First go to the `L¹` space.
For example, if you see `ennreal.to_real (∫⁻ a, ennreal.of_real $ ∥f a∥)`, that is the norm of
`f` in `L¹` space. Rewrite using `l1.norm_of_fun_eq_lintegral_norm`.
2. Show that the set `{f ∈ L¹ | ∫ f = ∫⁻ f⁺ - ∫⁻ f⁻}` is closed in `L¹` using `is_closed_eq`.
3. Show that the property holds for all simple functions `s` in `L¹` space.
Typically, you need to convert various notions to their `simple_func` counterpart, using lemmas
like `l1.integral_coe_eq_integral`.
4. Since simple functions are dense in `L¹`,
```
univ = closure {s simple}
= closure {s simple | ∫ s = ∫⁻ s⁺ - ∫⁻ s⁻} : the property holds for all simple functions
⊆ closure {f | ∫ f = ∫⁻ f⁺ - ∫⁻ f⁻}
= {f | ∫ f = ∫⁻ f⁺ - ∫⁻ f⁻} : closure of a closed set is itself
```
Use `is_closed_property` or `dense_range.induction_on` for this argument.
## Notations
* `α →ₛ E` : simple functions (defined in `measure_theory/integration`)
* `α →₁[μ] E` : functions in L1 space, i.e., equivalence classes of integrable functions (defined in
`measure_theory/l1_space`)
* `α →₁ₛ[μ] E` : simple functions in L1 space, i.e., equivalence classes of integrable simple
functions
Note : `ₛ` is typed using `\_s`. Sometimes it shows as a box if font is missing.
## Tags
Bochner integral, simple function, function space, Lebesgue dominated convergence theorem
-/
noncomputable theory
open_locale classical topological_space big_operators nnreal
namespace measure_theory
variables {α E : Type*} [measurable_space α] [linear_order E] [has_zero E]
local infixr ` →ₛ `:25 := simple_func
namespace simple_func
section pos_part
/-- Positive part of a simple function. -/
def pos_part (f : α →ₛ E) : α →ₛ E := f.map (λb, max b 0)
/-- Negative part of a simple function. -/
def neg_part [has_neg E] (f : α →ₛ E) : α →ₛ E := pos_part (-f)
lemma pos_part_map_norm (f : α →ₛ ℝ) : (pos_part f).map norm = pos_part f :=
begin
ext,
rw [map_apply, real.norm_eq_abs, abs_of_nonneg],
rw [pos_part, map_apply],
exact le_max_right _ _
end
lemma neg_part_map_norm (f : α →ₛ ℝ) : (neg_part f).map norm = neg_part f :=
by { rw neg_part, exact pos_part_map_norm _ }
lemma pos_part_sub_neg_part (f : α →ₛ ℝ) : f.pos_part - f.neg_part = f :=
begin
simp only [pos_part, neg_part],
ext a,
rw coe_sub,
exact max_zero_sub_eq_self (f a)
end
end pos_part
end simple_func
end measure_theory
namespace measure_theory
open set filter topological_space ennreal emetric
variables {α E F : Type*} [measurable_space α]
local infixr ` →ₛ `:25 := simple_func
namespace simple_func
section integral
/-!
### The Bochner integral of simple functions
Define the Bochner integral of simple functions of the type `α →ₛ β` where `β` is a normed group,
and prove basic property of this integral.
-/
open finset
variables [normed_group E] [measurable_space E] [normed_group F]
variables {μ : measure α}
/-- For simple functions with a `normed_group` as codomain, being integrable is the same as having
finite volume support. -/
lemma integrable_iff_fin_meas_supp {f : α →ₛ E} {μ : measure α} :
integrable f μ ↔ f.fin_meas_supp μ :=
calc integrable f μ ↔ ∫⁻ x, f.map (coe ∘ nnnorm : E → ennreal) x ∂μ < ⊤ :
and_iff_right f.ae_measurable
... ↔ (f.map (coe ∘ nnnorm : E → ennreal)).lintegral μ < ⊤ : by rw lintegral_eq_lintegral
... ↔ (f.map (coe ∘ nnnorm : E → ennreal)).fin_meas_supp μ : iff.symm $
fin_meas_supp.iff_lintegral_lt_top $ eventually_of_forall $ λ x, coe_lt_top
... ↔ _ : fin_meas_supp.map_iff $ λ b, coe_eq_zero.trans nnnorm_eq_zero
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
variables [normed_space ℝ F]
/-- Bochner integral of simple functions whose codomain is a real `normed_space`. -/
def integral (μ : measure α) (f : α →ₛ F) : F :=
∑ x in f.range, (ennreal.to_real (μ (f ⁻¹' {x}))) • x
lemma integral_eq_sum_filter (f : α →ₛ F) (μ) :
f.integral μ = ∑ x in f.range.filter (λ x, x ≠ 0), (ennreal.to_real (μ (f ⁻¹' {x}))) • x :=
eq.symm $ sum_filter_of_ne $ λ x _, mt $ λ h0, h0.symm ▸ smul_zero _
/-- The Bochner integral is equal to a sum over any set that includes `f.range` (except `0`). -/
lemma integral_eq_sum_of_subset {f : α →ₛ F} {μ : measure α} {s : finset F}
(hs : f.range.filter (λ x, x ≠ 0) ⊆ s) : f.integral μ = ∑ x in s, (μ (f ⁻¹' {x})).to_real • x :=
begin
rw [simple_func.integral_eq_sum_filter, finset.sum_subset hs],
rintro x - hx, rw [finset.mem_filter, not_and_distrib, ne.def, not_not] at hx,
rcases hx with hx|rfl; [skip, simp],
rw [simple_func.mem_range] at hx, rw [preimage_eq_empty]; simp [disjoint_singleton_left, hx]
end
/-- Calculate the integral of `g ∘ f : α →ₛ F`, where `f` is an integrable function from `α` to `E`
and `g` is a function from `E` to `F`. We require `g 0 = 0` so that `g ∘ f` is integrable. -/
lemma map_integral (f : α →ₛ E) (g : E → F) (hf : integrable f μ) (hg : g 0 = 0) :
(f.map g).integral μ = ∑ x in f.range, (ennreal.to_real (μ (f ⁻¹' {x}))) • (g x) :=
begin
-- We start as in the proof of `map_lintegral`
simp only [integral, range_map],
refine finset.sum_image' _ (assume b hb, _),
rcases mem_range.1 hb with ⟨a, rfl⟩,
rw [map_preimage_singleton, ← sum_measure_preimage_singleton _
(λ _ _, f.is_measurable_preimage _)],
-- Now we use `hf : integrable f μ` to show that `ennreal.to_real` is additive.
by_cases ha : g (f a) = 0,
{ simp only [ha, smul_zero],
refine (sum_eq_zero $ λ x hx, _).symm,
simp only [mem_filter] at hx,
simp [hx.2] },
{ rw [to_real_sum, sum_smul],
{ refine sum_congr rfl (λ x hx, _),
simp only [mem_filter] at hx,
rw [hx.2] },
{ intros x hx,
simp only [mem_filter] at hx,
refine (integrable_iff_fin_meas_supp.1 hf).meas_preimage_singleton_ne_zero _,
exact λ h0, ha (hx.2 ▸ h0.symm ▸ hg) } },
end
/-- `simple_func.integral` and `simple_func.lintegral` agree when the integrand has type
`α →ₛ ennreal`. But since `ennreal` is not a `normed_space`, we need some form of coercion.
See `integral_eq_lintegral` for a simpler version. -/
lemma integral_eq_lintegral' {f : α →ₛ E} {g : E → ennreal} (hf : integrable f μ) (hg0 : g 0 = 0)
(hgt : ∀b, g b < ⊤):
(f.map (ennreal.to_real ∘ g)).integral μ = ennreal.to_real (∫⁻ a, g (f a) ∂μ) :=
begin
have hf' : f.fin_meas_supp μ := integrable_iff_fin_meas_supp.1 hf,
simp only [← map_apply g f, lintegral_eq_lintegral],
rw [map_integral f _ hf, map_lintegral, ennreal.to_real_sum],
{ refine finset.sum_congr rfl (λb hb, _),
rw [smul_eq_mul, to_real_mul, mul_comm] },
{ assume a ha,
by_cases a0 : a = 0,
{ rw [a0, hg0, zero_mul], exact with_top.zero_lt_top },
{ apply mul_lt_top (hgt a) (hf'.meas_preimage_singleton_ne_zero a0) } },
{ simp [hg0] }
end
variables [normed_space ℝ E]
lemma integral_congr {f g : α →ₛ E} (hf : integrable f μ) (h : f =ᵐ[μ] g):
f.integral μ = g.integral μ :=
show ((pair f g).map prod.fst).integral μ = ((pair f g).map prod.snd).integral μ, from
begin
have inte := integrable_pair hf (hf.congr h),
rw [map_integral (pair f g) _ inte prod.fst_zero, map_integral (pair f g) _ inte prod.snd_zero],
refine finset.sum_congr rfl (assume p hp, _),
rcases mem_range.1 hp with ⟨a, rfl⟩,
by_cases eq : f a = g a,
{ dsimp only [pair_apply], rw eq },
{ have : μ ((pair f g) ⁻¹' {(f a, g a)}) = 0,
{ refine measure_mono_null (assume a' ha', _) h,
simp only [set.mem_preimage, mem_singleton_iff, pair_apply, prod.mk.inj_iff] at ha',
show f a' ≠ g a',
rwa [ha'.1, ha'.2] },
simp only [this, pair_apply, zero_smul, ennreal.zero_to_real] },
end
/-- `simple_func.bintegral` and `simple_func.integral` agree when the integrand has type
`α →ₛ ennreal`. But since `ennreal` is not a `normed_space`, we need some form of coercion. -/
lemma integral_eq_lintegral {f : α →ₛ ℝ} (hf : integrable f μ) (h_pos : 0 ≤ᵐ[μ] f) :
f.integral μ = ennreal.to_real (∫⁻ a, ennreal.of_real (f a) ∂μ) :=
begin
have : f =ᵐ[μ] f.map (ennreal.to_real ∘ ennreal.of_real) :=
h_pos.mono (λ a h, (ennreal.to_real_of_real h).symm),
rw [← integral_eq_lintegral' hf],
{ exact integral_congr hf this },
{ exact ennreal.of_real_zero },
{ assume b, rw ennreal.lt_top_iff_ne_top, exact ennreal.of_real_ne_top }
end
lemma integral_add {f g : α →ₛ E} (hf : integrable f μ) (hg : integrable g μ) :
integral μ (f + g) = integral μ f + integral μ g :=
calc integral μ (f + g) = ∑ x in (pair f g).range,
ennreal.to_real (μ ((pair f g) ⁻¹' {x})) • (x.fst + x.snd) :
begin
rw [add_eq_map₂, map_integral (pair f g)],
{ exact integrable_pair hf hg },
{ simp only [add_zero, prod.fst_zero, prod.snd_zero] }
end
... = ∑ x in (pair f g).range,
(ennreal.to_real (μ ((pair f g) ⁻¹' {x})) • x.fst +
ennreal.to_real (μ ((pair f g) ⁻¹' {x})) • x.snd) :
finset.sum_congr rfl $ assume a ha, smul_add _ _ _
... = ∑ x in (pair f g).range,
ennreal.to_real (μ ((pair f g) ⁻¹' {x})) • x.fst +
∑ x in (pair f g).range,
ennreal.to_real (μ ((pair f g) ⁻¹' {x})) • x.snd :
by rw finset.sum_add_distrib
... = ((pair f g).map prod.fst).integral μ + ((pair f g).map prod.snd).integral μ :
begin
rw [map_integral (pair f g), map_integral (pair f g)],
{ exact integrable_pair hf hg }, { refl },
{ exact integrable_pair hf hg }, { refl }
end
... = integral μ f + integral μ g : rfl
lemma integral_neg {f : α →ₛ E} (hf : integrable f μ) : integral μ (-f) = - integral μ f :=
calc integral μ (-f) = integral μ (f.map (has_neg.neg)) : rfl
... = - integral μ f :
begin
rw [map_integral f _ hf neg_zero, integral, ← sum_neg_distrib],
refine finset.sum_congr rfl (λx h, smul_neg _ _),
end
lemma integral_sub [borel_space E] {f g : α →ₛ E} (hf : integrable f μ) (hg : integrable g μ) :
integral μ (f - g) = integral μ f - integral μ g :=
begin
rw [sub_eq_add_neg, integral_add hf, integral_neg hg, sub_eq_add_neg],
exact hg.neg
end
lemma integral_smul (r : ℝ) {f : α →ₛ E} (hf : integrable f μ) :
integral μ (r • f) = r • integral μ f :=
calc integral μ (r • f) = ∑ x in f.range, ennreal.to_real (μ (f ⁻¹' {x})) • r • x :
by rw [smul_eq_map r f, map_integral f _ hf (smul_zero _)]
... = ∑ x in f.range, ((ennreal.to_real (μ (f ⁻¹' {x}))) * r) • x :
finset.sum_congr rfl $ λb hb, by apply smul_smul
... = r • integral μ f :
by simp only [integral, smul_sum, smul_smul, mul_comm]
lemma norm_integral_le_integral_norm (f : α →ₛ E) (hf : integrable f μ) :
∥f.integral μ∥ ≤ (f.map norm).integral μ :=
begin
rw [map_integral f norm hf norm_zero, integral],
calc ∥∑ x in f.range, ennreal.to_real (μ (f ⁻¹' {x})) • x∥ ≤
∑ x in f.range, ∥ennreal.to_real (μ (f ⁻¹' {x})) • x∥ :
norm_sum_le _ _
... = ∑ x in f.range, ennreal.to_real (μ (f ⁻¹' {x})) • ∥x∥ :
begin
refine finset.sum_congr rfl (λb hb, _),
rw [norm_smul, smul_eq_mul, real.norm_eq_abs, abs_of_nonneg to_real_nonneg]
end
end
lemma integral_add_measure {ν} (f : α →ₛ E) (hf : integrable f (μ + ν)) :
f.integral (μ + ν) = f.integral μ + f.integral ν :=
begin
simp only [integral_eq_sum_filter, ← sum_add_distrib, ← add_smul, measure.add_apply],
refine sum_congr rfl (λ x hx, _),
rw [to_real_add];
refine ne_of_lt ((integrable_iff_fin_meas_supp.1 _).meas_preimage_singleton_ne_zero
(mem_filter.1 hx).2),
exacts [hf.left_of_add_measure, hf.right_of_add_measure]
end
end integral
end simple_func
namespace l1
open ae_eq_fun
variables
[normed_group E] [second_countable_topology E] [measurable_space E] [borel_space E]
[normed_group F] [second_countable_topology F] [measurable_space F] [borel_space F]
{μ : measure α}
variables (α E μ)
-- We use `Type*` instead of `add_subgroup` because otherwise we loose dot notation.
/-- `l1.simple_func` is a subspace of L1 consisting of equivalence classes of an integrable simple
function. -/
def simple_func : Type* :=
↥({ carrier := {f : α →₁[μ] E | ∃ (s : α →ₛ E), (ae_eq_fun.mk s s.ae_measurable : α →ₘ[μ] E) = f},
zero_mem' := ⟨0, rfl⟩,
add_mem' := λ f g ⟨s, hs⟩ ⟨t, ht⟩,
⟨s + t, by simp only [coe_add, ← hs, ← ht, mk_add_mk, ← simple_func.coe_add]⟩,
neg_mem' := λ f ⟨s, hs⟩, ⟨-s, by simp only [coe_neg, ← hs, neg_mk, ← simple_func.coe_neg]⟩ } :
add_subgroup (α →₁[μ] E))
variables {α E μ}
notation α ` →₁ₛ[`:25 μ `] ` E := measure_theory.l1.simple_func α E μ
namespace simple_func
section instances
/-! Simple functions in L1 space form a `normed_space`. -/
instance : has_coe (α →₁ₛ[μ] E) (α →₁[μ] E) := coe_subtype
instance : has_coe_to_fun (α →₁ₛ[μ] E) := ⟨λ f, α → E, λ f, ⇑(f : α →₁[μ] E)⟩
@[simp, norm_cast] lemma coe_coe (f : α →₁ₛ[μ] E) : ⇑(f : α →₁[μ] E) = f := rfl
protected lemma eq {f g : α →₁ₛ[μ] E} : (f : α →₁[μ] E) = (g : α →₁[μ] E) → f = g := subtype.eq
protected lemma eq' {f g : α →₁ₛ[μ] E} : (f : α →ₘ[μ] E) = (g : α →ₘ[μ] E) → f = g :=
subtype.eq ∘ subtype.eq
@[norm_cast] protected lemma eq_iff {f g : α →₁ₛ[μ] E} : (f : α →₁[μ] E) = g ↔ f = g :=
subtype.ext_iff.symm
@[norm_cast] protected lemma eq_iff' {f g : α →₁ₛ[μ] E} : (f : α →ₘ[μ] E) = g ↔ f = g :=
iff.intro (simple_func.eq') (congr_arg _)
/-- L1 simple functions forms a `emetric_space`, with the emetric being inherited from L1 space,
i.e., `edist f g = ∫⁻ a, edist (f a) (g a)`.
Not declared as an instance as `α →₁ₛ[μ] β` will only be useful in the construction of the Bochner
integral. -/
protected def emetric_space : emetric_space (α →₁ₛ[μ] E) := subtype.emetric_space
/-- L1 simple functions forms a `metric_space`, with the metric being inherited from L1 space,
i.e., `dist f g = ennreal.to_real (∫⁻ a, edist (f a) (g a)`).
Not declared as an instance as `α →₁ₛ[μ] β` will only be useful in the construction of the Bochner
integral. -/
protected def metric_space : metric_space (α →₁ₛ[μ] E) := subtype.metric_space
local attribute [instance] simple_func.metric_space simple_func.emetric_space
/-- Functions `α →₁ₛ[μ] E` form an additive commutative group. -/
local attribute [instance, priority 10000]
protected def add_comm_group : add_comm_group (α →₁ₛ[μ] E) := add_subgroup.to_add_comm_group _
instance : inhabited (α →₁ₛ[μ] E) := ⟨0⟩
@[simp, norm_cast]
lemma coe_zero : ((0 : α →₁ₛ[μ] E) : α →₁[μ] E) = 0 := rfl
@[simp, norm_cast]
lemma coe_add (f g : α →₁ₛ[μ] E) : ((f + g : α →₁ₛ[μ] E) : α →₁[μ] E) = f + g := rfl
@[simp, norm_cast]
lemma coe_neg (f : α →₁ₛ[μ] E) : ((-f : α →₁ₛ[μ] E) : α →₁[μ] E) = -f := rfl
@[simp, norm_cast]
lemma coe_sub (f g : α →₁ₛ[μ] E) : ((f - g : α →₁ₛ[μ] E) : α →₁[μ] E) = f - g := rfl
@[simp] lemma edist_eq (f g : α →₁ₛ[μ] E) : edist f g = edist (f : α →₁[μ] E) (g : α →₁[μ] E) := rfl
@[simp] lemma dist_eq (f g : α →₁ₛ[μ] E) : dist f g = dist (f : α →₁[μ] E) (g : α →₁[μ] E) := rfl
/-- The norm on `α →₁ₛ[μ] E` is inherited from L1 space. That is, `∥f∥ = ∫⁻ a, edist (f a) 0`.
Not declared as an instance as `α →₁ₛ[μ] E` will only be useful in the construction of the Bochner
integral. -/
protected def has_norm : has_norm (α →₁ₛ[μ] E) := ⟨λf, ∥(f : α →₁[μ] E)∥⟩
local attribute [instance] simple_func.has_norm
lemma norm_eq (f : α →₁ₛ[μ] E) : ∥f∥ = ∥(f : α →₁[μ] E)∥ := rfl
lemma norm_eq' (f : α →₁ₛ[μ] E) : ∥f∥ = ennreal.to_real (edist (f : α →ₘ[μ] E) 0) := rfl
/-- Not declared as an instance as `α →₁ₛ[μ] E` will only be useful in the construction of the
Bochner integral. -/
protected def normed_group : normed_group (α →₁ₛ[μ] E) :=
normed_group.of_add_dist (λ x, rfl) $ by
{ intros, simp only [dist_eq, coe_add, l1.dist_eq, l1.coe_add], rw edist_add_right }
variables {𝕜 : Type*} [normed_field 𝕜] [normed_space 𝕜 E]
/-- Not declared as an instance as `α →₁ₛ[μ] E` will only be useful in the construction of the
Bochner integral. -/
protected def has_scalar : has_scalar 𝕜 (α →₁ₛ[μ] E) := ⟨λk f, ⟨k • f,
begin
rcases f with ⟨f, ⟨s, hs⟩⟩,
use k • s,
rw [coe_smul, subtype.coe_mk, ← hs], refl
end ⟩⟩
local attribute [instance, priority 10000] simple_func.has_scalar
@[simp, norm_cast] lemma coe_smul (c : 𝕜) (f : α →₁ₛ[μ] E) :
((c • f : α →₁ₛ[μ] E) : α →₁[μ] E) = c • (f : α →₁[μ] E) := rfl
/-- Not declared as an instance as `α →₁ₛ[μ] E` will only be useful in the construction of the
Bochner integral. -/
protected def semimodule : semimodule 𝕜 (α →₁ₛ[μ] E) :=
{ one_smul := λf, simple_func.eq (by { simp only [coe_smul], exact one_smul _ _ }),
mul_smul := λx y f, simple_func.eq (by { simp only [coe_smul], exact mul_smul _ _ _ }),
smul_add := λx f g, simple_func.eq (by { simp only [coe_smul, coe_add], exact smul_add _ _ _ }),
smul_zero := λx, simple_func.eq (by { simp only [coe_zero, coe_smul], exact smul_zero _ }),
add_smul := λx y f, simple_func.eq (by { simp only [coe_smul], exact add_smul _ _ _ }),
zero_smul := λf, simple_func.eq (by { simp only [coe_smul], exact zero_smul _ _ }) }
local attribute [instance] simple_func.normed_group simple_func.semimodule
/-- Not declared as an instance as `α →₁ₛ[μ] E` will only be useful in the construction of the
Bochner integral. -/
protected def normed_space : normed_space 𝕜 (α →₁ₛ[μ] E) :=
⟨ λc f, by { rw [norm_eq, norm_eq, coe_smul, norm_smul] } ⟩
end instances
local attribute [instance] simple_func.normed_group simple_func.normed_space
section of_simple_func
/-- Construct the equivalence class `[f]` of an integrable simple function `f`. -/
@[reducible] def of_simple_func (f : α →ₛ E) (hf : integrable f μ) : (α →₁ₛ[μ] E) :=
⟨l1.of_fun f hf, ⟨f, rfl⟩⟩
lemma of_simple_func_eq_of_fun (f : α →ₛ E) (hf : integrable f μ) :
(of_simple_func f hf : α →₁[μ] E) = l1.of_fun f hf := rfl
lemma of_simple_func_eq_mk (f : α →ₛ E) (hf : integrable f μ) :
(of_simple_func f hf : α →ₘ[μ] E) = ae_eq_fun.mk f f.ae_measurable := rfl
lemma of_simple_func_zero : of_simple_func (0 : α →ₛ E) (integrable_zero α E μ) = 0 := rfl
lemma of_simple_func_add (f g : α →ₛ E) (hf : integrable f μ) (hg : integrable g μ) :
of_simple_func (f + g) (hf.add hg) = of_simple_func f hf + of_simple_func g hg := rfl
lemma of_simple_func_neg (f : α →ₛ E) (hf : integrable f μ) :
of_simple_func (-f) hf.neg = -of_simple_func f hf := rfl
lemma of_simple_func_sub (f g : α →ₛ E) (hf : integrable f μ) (hg : integrable g μ) :
of_simple_func (f - g) (hf.sub hg) = of_simple_func f hf - of_simple_func g hg :=
by { simp only [sub_eq_add_neg, ← of_simple_func_neg, ← of_simple_func_add], refl }
variables {𝕜 : Type*} [normed_field 𝕜] [normed_space 𝕜 E]
lemma of_simple_func_smul (f : α →ₛ E) (hf : integrable f μ) (c : 𝕜) :
of_simple_func (c • f) (hf.smul c) = c • of_simple_func f hf := rfl
lemma norm_of_simple_func (f : α →ₛ E) (hf : integrable f μ) :
∥of_simple_func f hf∥ = ennreal.to_real (∫⁻ a, edist (f a) 0 ∂μ) :=
rfl
end of_simple_func
section to_simple_func
/-- Find a representative of a `l1.simple_func`. -/
def to_simple_func (f : α →₁ₛ[μ] E) : α →ₛ E := classical.some f.2
/-- `f.to_simple_func` is measurable. -/
protected lemma measurable (f : α →₁ₛ[μ] E) : measurable f.to_simple_func :=
f.to_simple_func.measurable
protected lemma ae_measurable (f : α →₁ₛ[μ] E) : ae_measurable f.to_simple_func μ :=
f.measurable.ae_measurable
/-- `f.to_simple_func` is integrable. -/
protected lemma integrable (f : α →₁ₛ[μ] E) : integrable f.to_simple_func μ :=
let h := classical.some_spec f.2 in (integrable_mk f.ae_measurable).1 $ h.symm ▸ (f : α →₁[μ] E).2
lemma of_simple_func_to_simple_func (f : α →₁ₛ[μ] E) :
of_simple_func (f.to_simple_func) f.integrable = f :=
by { rw ← simple_func.eq_iff', exact classical.some_spec f.2 }
lemma to_simple_func_of_simple_func (f : α →ₛ E) (hfi : integrable f μ) :
(of_simple_func f hfi).to_simple_func =ᵐ[μ] f :=
by { rw ← mk_eq_mk, exact classical.some_spec (of_simple_func f hfi).2 }
lemma to_simple_func_eq_to_fun (f : α →₁ₛ[μ] E) : f.to_simple_func =ᵐ[μ] f :=
begin
rw [← of_fun_eq_of_fun f.to_simple_func f f.integrable (f : α →₁[μ] E).integrable, ← l1.eq_iff],
simp only [of_fun_eq_mk, ← coe_coe, mk_to_fun],
exact classical.some_spec f.coe_prop
end
variables (α E)
lemma zero_to_simple_func : (0 : α →₁ₛ[μ] E).to_simple_func =ᵐ[μ] 0 :=
begin
filter_upwards [to_simple_func_eq_to_fun (0 : α →₁ₛ[μ] E), l1.zero_to_fun α E],
assume a h₁ h₂,
rwa h₁,
end
variables {α E}
lemma add_to_simple_func (f g : α →₁ₛ[μ] E) :
(f + g).to_simple_func =ᵐ[μ] f.to_simple_func + g.to_simple_func :=
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, l1.add_to_fun (f : α →₁[μ] E) g],
assume a,
simp only [← coe_coe, coe_add, pi.add_apply],
iterate 4 { assume h, rw h }
end
lemma neg_to_simple_func (f : α →₁ₛ[μ] E) : (-f).to_simple_func =ᵐ[μ] - f.to_simple_func :=
begin
filter_upwards [to_simple_func_eq_to_fun (-f), to_simple_func_eq_to_fun f,
l1.neg_to_fun (f : α →₁[μ] E)],
assume a,
simp only [pi.neg_apply, coe_neg, ← coe_coe],
repeat { assume h, rw h }
end
lemma sub_to_simple_func (f g : α →₁ₛ[μ] E) :
(f - g).to_simple_func =ᵐ[μ] f.to_simple_func - g.to_simple_func :=
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, l1.sub_to_fun (f : α →₁[μ] E) g],
assume a,
simp only [coe_sub, pi.sub_apply, ← coe_coe],
repeat { assume h, rw h }
end
variables {𝕜 : Type*} [normed_field 𝕜] [normed_space 𝕜 E]
lemma smul_to_simple_func (k : 𝕜) (f : α →₁ₛ[μ] E) :
(k • f).to_simple_func =ᵐ[μ] k • f.to_simple_func :=
begin
filter_upwards [to_simple_func_eq_to_fun (k • f), to_simple_func_eq_to_fun f,
l1.smul_to_fun k (f : α →₁[μ] E)],
assume a,
simp only [pi.smul_apply, coe_smul, ← coe_coe],
repeat { assume h, rw h }
end
lemma lintegral_edist_to_simple_func_lt_top (f g : α →₁ₛ[μ] E) :
∫⁻ (x : α), edist ((to_simple_func f) x) ((to_simple_func g) x) ∂μ < ⊤ :=
begin
rw lintegral_rw₂ (to_simple_func_eq_to_fun f) (to_simple_func_eq_to_fun g),
exact lintegral_edist_to_fun_lt_top _ _
end
lemma dist_to_simple_func (f g : α →₁ₛ[μ] E) : dist f g =
ennreal.to_real (∫⁻ x, edist (f.to_simple_func x) (g.to_simple_func x) ∂μ) :=
begin
rw [dist_eq, l1.dist_to_fun, ennreal.to_real_eq_to_real],
{ rw lintegral_rw₂, repeat { exact ae_eq_symm (to_simple_func_eq_to_fun _) } },
{ exact l1.lintegral_edist_to_fun_lt_top _ _ },
{ exact lintegral_edist_to_simple_func_lt_top _ _ }
end
lemma norm_to_simple_func (f : α →₁ₛ[μ] E) :
∥f∥ = ennreal.to_real (∫⁻ (a : α), nnnorm ((to_simple_func f) a) ∂μ) :=
calc ∥f∥ =
ennreal.to_real (∫⁻x, edist (f.to_simple_func x) ((0 : α →₁ₛ[μ] E).to_simple_func x) ∂μ) :
begin
rw [← dist_zero_right, dist_to_simple_func]
end
... = ennreal.to_real (∫⁻ (x : α), (coe ∘ nnnorm) (f.to_simple_func x) ∂μ) :
begin
rw lintegral_nnnorm_eq_lintegral_edist,
have : ∫⁻ x, edist ((to_simple_func f) x) ((to_simple_func (0 : α →₁ₛ[μ] E)) x) ∂μ =
∫⁻ x, edist ((to_simple_func f) x) 0 ∂μ,
{ refine lintegral_congr_ae ((zero_to_simple_func α E).mono (λ a h, _)),
rw [h, pi.zero_apply] },
rw [ennreal.to_real_eq_to_real],
{ exact this },
{ exact lintegral_edist_to_simple_func_lt_top _ _ },
{ rw ← this, exact lintegral_edist_to_simple_func_lt_top _ _ }
end
lemma norm_eq_integral (f : α →₁ₛ[μ] E) : ∥f∥ = (f.to_simple_func.map norm).integral μ :=
-- calc ∥f∥ = ennreal.to_real (∫⁻ (x : α), (coe ∘ nnnorm) (f.to_simple_func x) ∂μ) :
-- by { rw norm_to_simple_func }
-- ... = (f.to_simple_func.map norm).integral μ :
begin
rw [norm_to_simple_func, simple_func.integral_eq_lintegral],
{ simp only [simple_func.map_apply, of_real_norm_eq_coe_nnnorm] },
{ exact f.integrable.norm },
{ exact eventually_of_forall (λ x, norm_nonneg _) }
end
end to_simple_func
section coe_to_l1
protected lemma uniform_continuous : uniform_continuous (coe : (α →₁ₛ[μ] E) → (α →₁[μ] E)) :=
uniform_continuous_comap
protected lemma uniform_embedding : uniform_embedding (coe : (α →₁ₛ[μ] E) → (α →₁[μ] E)) :=
uniform_embedding_comap subtype.val_injective
protected lemma uniform_inducing : uniform_inducing (coe : (α →₁ₛ[μ] E) → (α →₁[μ] E)) :=
simple_func.uniform_embedding.to_uniform_inducing
protected lemma dense_embedding : dense_embedding (coe : (α →₁ₛ[μ] E) → (α →₁[μ] E)) :=
begin
apply simple_func.uniform_embedding.dense_embedding,
rintros ⟨f, hfi⟩,
have A : ae_eq_fun.mk f f.ae_measurable = f := mk_coe_fn _,
rw mem_closure_iff_seq_limit,
have hfi' := integrable_coe_fn.2 hfi,
refine ⟨λ n, ↑(of_simple_func (simple_func.approx_on f f.measurable univ 0 trivial n)
(simple_func.integrable_approx_on_univ f.measurable hfi' n)), λ n, mem_range_self _, _⟩,
simp only [tendsto_iff_edist_tendsto_0, of_fun_eq_mk, subtype.coe_mk, edist_eq],
dsimp,
conv in (edist _ _) { congr, skip, rw ← A },
simpa only [edist_mk_mk] using simple_func.tendsto_approx_on_univ_l1_edist f.measurable hfi'
end
protected lemma dense_inducing : dense_inducing (coe : (α →₁ₛ[μ] E) → (α →₁[μ] E)) :=
simple_func.dense_embedding.to_dense_inducing
protected lemma dense_range : dense_range (coe : (α →₁ₛ[μ] E) → (α →₁[μ] E)) :=
simple_func.dense_inducing.dense
variables (𝕜 : Type*) [normed_field 𝕜] [normed_space 𝕜 E]
variables (α E)
/-- The uniform and dense embedding of L1 simple functions into L1 functions. -/
def coe_to_l1 : (α →₁ₛ[μ] E) →L[𝕜] (α →₁[μ] E) :=
{ to_fun := (coe : (α →₁ₛ[μ] E) → (α →₁[μ] E)),
map_add' := λf g, rfl,
map_smul' := λk f, rfl,
cont := l1.simple_func.uniform_continuous.continuous, }
variables {α E 𝕜}
end coe_to_l1
section pos_part
/-- Positive part of a simple function in L1 space. -/
def pos_part (f : α →₁ₛ[μ] ℝ) : α →₁ₛ[μ] ℝ := ⟨l1.pos_part (f : α →₁[μ] ℝ),
begin
rcases f with ⟨f, s, hsf⟩,
use s.pos_part,
simp only [subtype.coe_mk, l1.coe_pos_part, ← hsf, ae_eq_fun.pos_part_mk, simple_func.pos_part,
simple_func.coe_map]
end ⟩
/-- Negative part of a simple function in L1 space. -/
def neg_part (f : α →₁ₛ[μ] ℝ) : α →₁ₛ[μ] ℝ := pos_part (-f)
@[norm_cast]
lemma coe_pos_part (f : α →₁ₛ[μ] ℝ) : (f.pos_part : α →₁[μ] ℝ) = (f : α →₁[μ] ℝ).pos_part := rfl
@[norm_cast]
lemma coe_neg_part (f : α →₁ₛ[μ] ℝ) : (f.neg_part : α →₁[μ] ℝ) = (f : α →₁[μ] ℝ).neg_part := rfl
end pos_part
section simple_func_integral
/-! Define the Bochner integral on `α →₁ₛ[μ] E` and prove basic properties of this integral. -/
variables [normed_space ℝ E]
/-- The Bochner integral over simple functions in l1 space. -/
def integral (f : α →₁ₛ[μ] E) : E := (f.to_simple_func).integral μ
lemma integral_eq_integral (f : α →₁ₛ[μ] E) : integral f = (f.to_simple_func).integral μ := rfl
lemma integral_eq_lintegral {f : α →₁ₛ[μ] ℝ} (h_pos : 0 ≤ᵐ[μ] f.to_simple_func) :
integral f = ennreal.to_real (∫⁻ a, ennreal.of_real (f.to_simple_func a) ∂μ) :=
by rw [integral, simple_func.integral_eq_lintegral f.integrable h_pos]
lemma integral_congr {f g : α →₁ₛ[μ] E} (h : f.to_simple_func =ᵐ[μ] g.to_simple_func) :
integral f = integral g :=
simple_func.integral_congr f.integrable h
lemma integral_add (f g : α →₁ₛ[μ] E) : integral (f + g) = integral f + integral g :=
begin
simp only [integral],
rw ← simple_func.integral_add f.integrable g.integrable,
apply measure_theory.simple_func.integral_congr (f + g).integrable,
apply add_to_simple_func
end
lemma integral_smul (r : ℝ) (f : α →₁ₛ[μ] E) : integral (r • f) = r • integral f :=
begin
simp only [integral],
rw ← simple_func.integral_smul _ f.integrable,
apply measure_theory.simple_func.integral_congr (r • f).integrable,
apply smul_to_simple_func
end
lemma norm_integral_le_norm (f : α →₁ₛ[μ] E) : ∥ integral f ∥ ≤ ∥f∥ :=
begin
rw [integral, norm_eq_integral],
exact f.to_simple_func.norm_integral_le_integral_norm f.integrable
end
/-- The Bochner integral over simple functions in l1 space as a continuous linear map. -/
def integral_clm : (α →₁ₛ[μ] E) →L[ℝ] E :=
linear_map.mk_continuous ⟨integral, integral_add, integral_smul⟩
1 (λf, le_trans (norm_integral_le_norm _) $ by rw one_mul)
local notation `Integral` := @integral_clm α E _ _ _ _ _ μ _
open continuous_linear_map
lemma norm_Integral_le_one : ∥Integral∥ ≤ 1 :=
linear_map.mk_continuous_norm_le _ (zero_le_one) _
section pos_part
lemma pos_part_to_simple_func (f : α →₁ₛ[μ] ℝ) :
f.pos_part.to_simple_func =ᵐ[μ] f.to_simple_func.pos_part :=
begin
have eq : ∀ a, f.to_simple_func.pos_part a = max (f.to_simple_func a) 0 := λa, rfl,
have ae_eq : ∀ᵐ a ∂μ, f.pos_part.to_simple_func a = max (f.to_simple_func a) 0,
{ filter_upwards [to_simple_func_eq_to_fun f.pos_part, pos_part_to_fun (f : α →₁[μ] ℝ),
to_simple_func_eq_to_fun f],
assume a h₁ h₂ h₃,
rw [h₁, ← coe_coe, coe_pos_part, h₂, coe_coe, ← h₃] },
refine ae_eq.mono (assume a h, _),
rw [h, eq]
end
lemma neg_part_to_simple_func (f : α →₁ₛ[μ] ℝ) :
f.neg_part.to_simple_func =ᵐ[μ] f.to_simple_func.neg_part :=
begin
rw [simple_func.neg_part, measure_theory.simple_func.neg_part],
filter_upwards [pos_part_to_simple_func (-f), neg_to_simple_func f],
assume a h₁ h₂,
rw h₁,
show max _ _ = max _ _,
rw h₂,
refl
end
lemma integral_eq_norm_pos_part_sub (f : α →₁ₛ[μ] ℝ) : f.integral = ∥f.pos_part∥ - ∥f.neg_part∥ :=
begin
-- Convert things in `L¹` to their `simple_func` counterpart
have ae_eq₁ : f.to_simple_func.pos_part =ᵐ[μ] (f.pos_part).to_simple_func.map norm,
{ filter_upwards [pos_part_to_simple_func f],
assume a h,
rw [simple_func.map_apply, h],
conv_lhs { rw [← simple_func.pos_part_map_norm, simple_func.map_apply] } },
-- Convert things in `L¹` to their `simple_func` counterpart
have ae_eq₂ : f.to_simple_func.neg_part =ᵐ[μ] (f.neg_part).to_simple_func.map norm,
{ filter_upwards [neg_part_to_simple_func f],
assume a h,
rw [simple_func.map_apply, h],
conv_lhs { rw [← simple_func.neg_part_map_norm, simple_func.map_apply] } },
-- Convert things in `L¹` to their `simple_func` counterpart
have ae_eq : ∀ᵐ a ∂μ, f.to_simple_func.pos_part a - f.to_simple_func.neg_part a =
(f.pos_part).to_simple_func.map norm a - (f.neg_part).to_simple_func.map norm a,
{ filter_upwards [ae_eq₁, ae_eq₂],
assume a h₁ h₂,
rw [h₁, h₂] },
rw [integral, norm_eq_integral, norm_eq_integral, ← simple_func.integral_sub],
{ show f.to_simple_func.integral μ =
((f.pos_part.to_simple_func).map norm - f.neg_part.to_simple_func.map norm).integral μ,
apply measure_theory.simple_func.integral_congr f.integrable,
filter_upwards [ae_eq₁, ae_eq₂],
assume a h₁ h₂, show _ = _ - _,
rw [← h₁, ← h₂],
have := f.to_simple_func.pos_part_sub_neg_part,
conv_lhs {rw ← this},
refl },
{ exact f.integrable.max_zero.congr ae_eq₁ },
{ exact f.integrable.neg.max_zero.congr ae_eq₂ }
end
end pos_part
end simple_func_integral
end simple_func
open simple_func
variables [normed_space ℝ E] [normed_space ℝ F] [complete_space E]
section integration_in_l1
local notation `to_l1` := coe_to_l1 α E ℝ
local attribute [instance] simple_func.normed_group simple_func.normed_space
open continuous_linear_map
/-- The Bochner integral in l1 space as a continuous linear map. -/
def integral_clm : (α →₁[μ] E) →L[ℝ] E :=
integral_clm.extend to_l1 simple_func.dense_range simple_func.uniform_inducing
/-- The Bochner integral in l1 space -/
def integral (f : α →₁[μ] E) : E := integral_clm f
lemma integral_eq (f : α →₁[μ] E) : integral f = integral_clm f := rfl
@[norm_cast] lemma simple_func.integral_l1_eq_integral (f : α →₁ₛ[μ] E) :
integral (f : α →₁[μ] E) = f.integral :=
uniformly_extend_of_ind simple_func.uniform_inducing simple_func.dense_range
simple_func.integral_clm.uniform_continuous _
variables (α E)
@[simp] lemma integral_zero : integral (0 : α →₁[μ] E) = 0 :=
map_zero integral_clm
variables {α E}
lemma integral_add (f g : α →₁[μ] E) : integral (f + g) = integral f + integral g :=
map_add integral_clm f g
lemma integral_neg (f : α →₁[μ] E) : integral (-f) = - integral f :=
map_neg integral_clm f
lemma integral_sub (f g : α →₁[μ] E) : integral (f - g) = integral f - integral g :=
map_sub integral_clm f g
lemma integral_smul (r : ℝ) (f : α →₁[μ] E) : integral (r • f) = r • integral f :=
map_smul r integral_clm f
local notation `Integral` := @integral_clm α E _ _ _ _ _ μ _ _
local notation `sIntegral` := @simple_func.integral_clm α E _ _ _ _ _ μ _
lemma norm_Integral_le_one : ∥Integral∥ ≤ 1 :=
calc ∥Integral∥ ≤ (1 : ℝ≥0) * ∥sIntegral∥ :
op_norm_extend_le _ _ _ $ λs, by {rw [nnreal.coe_one, one_mul], refl}
... = ∥sIntegral∥ : one_mul _
... ≤ 1 : norm_Integral_le_one
lemma norm_integral_le (f : α →₁[μ] E) : ∥integral f∥ ≤ ∥f∥ :=
calc ∥integral f∥ = ∥Integral f∥ : rfl
... ≤ ∥Integral∥ * ∥f∥ : le_op_norm _ _
... ≤ 1 * ∥f∥ : mul_le_mul_of_nonneg_right norm_Integral_le_one $ norm_nonneg _
... = ∥f∥ : one_mul _
@[continuity]
lemma continuous_integral : continuous (λ (f : α →₁[μ] E), f.integral) :=
by simp [l1.integral, l1.integral_clm.continuous]
section pos_part
lemma integral_eq_norm_pos_part_sub (f : α →₁[μ] ℝ) : integral f = ∥pos_part f∥ - ∥neg_part f∥ :=
begin
-- Use `is_closed_property` and `is_closed_eq`
refine @is_closed_property _ _ _ (coe : (α →₁ₛ[μ] ℝ) → (α →₁[μ] ℝ))
(λ f : α →₁[μ] ℝ, integral f = ∥pos_part f∥ - ∥neg_part f∥)
l1.simple_func.dense_range (is_closed_eq _ _) _ f,
{ exact cont _ },
{ refine continuous.sub (continuous_norm.comp l1.continuous_pos_part)
(continuous_norm.comp l1.continuous_neg_part) },
-- Show that the property holds for all simple functions in the `L¹` space.
{ assume s,
norm_cast,
rw [← simple_func.norm_eq, ← simple_func.norm_eq],
exact simple_func.integral_eq_norm_pos_part_sub _}
end
end pos_part
end integration_in_l1
end l1
variables [normed_group E] [second_countable_topology E] [normed_space ℝ E] [complete_space E]
[measurable_space E] [borel_space E]
[normed_group F] [second_countable_topology F] [normed_space ℝ F] [complete_space F]
[measurable_space F] [borel_space F]
/-- The Bochner integral -/
def integral (μ : measure α) (f : α → E) : E :=
if hf : integrable f μ then (l1.of_fun f hf).integral else 0
/-! In the notation for integrals, an expression like `∫ x, g ∥x∥ ∂μ` will not be parsed correctly,
and needs parentheses. We do not set the binding power of `r` to `0`, because then
`∫ x, f x = 0` will be parsed incorrectly. -/
notation `∫` binders `, ` r:(scoped:60 f, f) ` ∂` μ:70 := integral μ r
notation `∫` binders `, ` r:(scoped:60 f, integral volume f) := r
notation `∫` binders ` in ` s `, ` r:(scoped:60 f, f) ` ∂` μ:70 := integral (measure.restrict μ s) r
notation `∫` binders ` in ` s `, ` r:(scoped:60 f, integral (measure.restrict volume s) f) := r
section properties
open continuous_linear_map measure_theory.simple_func
variables {f g : α → E} {μ : measure α}
lemma integral_eq (f : α → E) (hf : integrable f μ) :
∫ a, f a ∂μ = (l1.of_fun f hf).integral :=
dif_pos hf
lemma l1.integral_eq_integral (f : α →₁[μ] E) : f.integral = ∫ a, f a ∂μ :=
by rw [integral_eq, l1.of_fun_to_fun]
lemma integral_undef (h : ¬ integrable f μ) : ∫ a, f a ∂μ = 0 :=
dif_neg h
lemma integral_non_ae_measurable (h : ¬ ae_measurable f μ) : ∫ a, f a ∂μ = 0 :=
integral_undef $ not_and_of_not_left _ h
variables (α E)
lemma integral_zero : ∫ a : α, (0:E) ∂μ = 0 :=
by rw [integral_eq, l1.of_fun_zero, l1.integral_zero]
@[simp] lemma integral_zero' : integral μ (0 : α → E) = 0 :=
integral_zero α E
variables {α E}
lemma integral_add (hf : integrable f μ) (hg : integrable g μ) :
∫ a, f a + g a ∂μ = ∫ a, f a ∂μ + ∫ a, g a ∂μ :=
by { rw [integral_eq, integral_eq f hf, integral_eq g hg, ← l1.integral_add, ← l1.of_fun_add],
refl }
lemma integral_add' (hf : integrable f μ) (hg : integrable g μ) :
∫ a, (f + g) a ∂μ = ∫ a, f a ∂μ + ∫ a, g a ∂μ :=
integral_add hf hg
lemma integral_neg (f : α → E) : ∫ a, -f a ∂μ = - ∫ a, f a ∂μ :=
begin
by_cases hf : integrable f μ,
{ rw [integral_eq f hf, integral_eq (λa, - f a) hf.neg, ← l1.integral_neg, ← l1.of_fun_neg],
refl },
{ rw [integral_undef hf, integral_undef, neg_zero], rwa [← integrable_neg_iff] at hf }
end
lemma integral_neg' (f : α → E) : ∫ a, (-f) a ∂μ = - ∫ a, f a ∂μ :=
integral_neg f
lemma integral_sub (hf : integrable f μ) (hg : integrable g μ) :
∫ a, f a - g a ∂μ = ∫ a, f a ∂μ - ∫ a, g a ∂μ :=
by { simp only [sub_eq_add_neg, ← integral_neg], exact integral_add hf hg.neg }
lemma integral_sub' (hf : integrable f μ) (hg : integrable g μ) :
∫ a, (f - g) a ∂μ = ∫ a, f a ∂μ - ∫ a, g a ∂μ :=
integral_sub hf hg
lemma integral_smul (r : ℝ) (f : α → E) : ∫ a, r • (f a) ∂μ = r • ∫ a, f a ∂μ :=
begin
by_cases hf : integrable f μ,
{ rw [integral_eq f hf, integral_eq (λa, r • (f a)), l1.of_fun_smul, l1.integral_smul] },
{ by_cases hr : r = 0,
{ simp only [hr, measure_theory.integral_zero, zero_smul] },
have hf' : ¬ integrable (λ x, r • f x) μ,
{ change ¬ integrable (r • f) μ, rwa [integrable_smul_iff hr f] },
rw [integral_undef hf, integral_undef hf', smul_zero] }
end
lemma integral_mul_left (r : ℝ) (f : α → ℝ) : ∫ a, r * (f a) ∂μ = r * ∫ a, f a ∂μ :=
integral_smul r f
lemma integral_mul_right (r : ℝ) (f : α → ℝ) : ∫ a, (f a) * r ∂μ = ∫ a, f a ∂μ * r :=
by { simp only [mul_comm], exact integral_mul_left r f }
lemma integral_div (r : ℝ) (f : α → ℝ) : ∫ a, (f a) / r ∂μ = ∫ a, f a ∂μ / r :=
integral_mul_right r⁻¹ f
lemma integral_congr_ae (h : f =ᵐ[μ] g) : ∫ a, f a ∂μ = ∫ a, g a ∂μ :=
begin
by_cases hfi : integrable f μ,
{ have hgi : integrable g μ := hfi.congr h,
rw [integral_eq f hfi, integral_eq g hgi, (l1.of_fun_eq_of_fun f g hfi hgi).2 h] },
{ have hgi : ¬ integrable g μ, { rw integrable_congr h at hfi, exact hfi },
rw [integral_undef hfi, integral_undef hgi] },
end
@[simp] lemma l1.integral_of_fun_eq_integral {f : α → E} (hf : integrable f μ) :
∫ a, (l1.of_fun f hf) a ∂μ = ∫ a, f a ∂μ :=
integral_congr_ae (l1.to_fun_of_fun f hf)
@[continuity]
lemma continuous_integral : continuous (λ (f : α →₁[μ] E), ∫ a, f a ∂μ) :=
by { simp only [← l1.integral_eq_integral], exact l1.continuous_integral }
lemma norm_integral_le_lintegral_norm (f : α → E) :
∥∫ a, f a ∂μ∥ ≤ ennreal.to_real (∫⁻ a, (ennreal.of_real ∥f a∥) ∂μ) :=
begin
by_cases hf : integrable f μ,
{ rw [integral_eq f hf, ← l1.norm_of_fun_eq_lintegral_norm f hf], exact l1.norm_integral_le _ },
{ rw [integral_undef hf, norm_zero], exact to_real_nonneg }
end
lemma ennnorm_integral_le_lintegral_ennnorm (f : α → E) :
(nnnorm (∫ a, f a ∂μ) : ennreal) ≤ ∫⁻ a, (nnnorm (f a)) ∂μ :=
by { simp_rw [← of_real_norm_eq_coe_nnnorm], apply ennreal.of_real_le_of_le_to_real,
exact norm_integral_le_lintegral_norm f }
lemma integral_eq_zero_of_ae {f : α → E} (hf : f =ᵐ[μ] 0) : ∫ a, f a ∂μ = 0 :=
if hfm : ae_measurable f μ then by simp [integral_congr_ae hf, integral_zero]
else integral_non_ae_measurable hfm
/-- If `F i → f` in `L1`, then `∫ x, F i x ∂μ → ∫ x, f x∂μ`. -/
lemma tendsto_integral_of_l1 {ι} (f : α → E) (hfi : integrable f μ)
{F : ι → α → E} {l : filter ι} (hFi : ∀ᶠ i in l, integrable (F i) μ)
(hF : tendsto (λ i, ∫⁻ x, edist (F i x) (f x) ∂μ) l (𝓝 0)) :
tendsto (λ i, ∫ x, F i x ∂μ) l (𝓝 $ ∫ x, f x ∂μ) :=
begin
rw [tendsto_iff_norm_tendsto_zero],
replace hF : tendsto (λ i, ennreal.to_real $ ∫⁻ x, edist (F i x) (f x) ∂μ) l (𝓝 0) :=
(ennreal.tendsto_to_real zero_ne_top).comp hF,
refine squeeze_zero_norm' (hFi.mp $ hFi.mono $ λ i hFi hFm, _) hF,
simp only [norm_norm, ← integral_sub hFi hfi, edist_dist, dist_eq_norm],
apply norm_integral_le_lintegral_norm
end
/-- Lebesgue dominated convergence theorem provides sufficient conditions under which almost
everywhere convergence of a sequence of functions implies the convergence of their integrals. -/
theorem tendsto_integral_of_dominated_convergence {F : ℕ → α → E} {f : α → E} (bound : α → ℝ)
(F_measurable : ∀ n, ae_measurable (F n) μ)
(f_measurable : ae_measurable f μ)
(bound_integrable : integrable bound μ)
(h_bound : ∀ n, ∀ᵐ a ∂μ, ∥F n a∥ ≤ bound a)
(h_lim : ∀ᵐ a ∂μ, tendsto (λ n, F n a) at_top (𝓝 (f a))) :
tendsto (λn, ∫ a, F n a ∂μ) at_top (𝓝 $ ∫ a, f a ∂μ) :=
begin
/- To show `(∫ a, F n a) --> (∫ f)`, suffices to show `∥∫ a, F n a - ∫ f∥ --> 0` -/
rw tendsto_iff_norm_tendsto_zero,
/- But `0 ≤ ∥∫ a, F n a - ∫ f∥ = ∥∫ a, (F n a - f a) ∥ ≤ ∫ a, ∥F n a - f a∥, and thus we apply the
sandwich theorem and prove that `∫ a, ∥F n a - f a∥ --> 0` -/
have lintegral_norm_tendsto_zero :
tendsto (λn, ennreal.to_real $ ∫⁻ a, (ennreal.of_real ∥F n a - f a∥) ∂μ) at_top (𝓝 0) :=
(tendsto_to_real zero_ne_top).comp
(tendsto_lintegral_norm_of_dominated_convergence
F_measurable f_measurable bound_integrable.has_finite_integral h_bound h_lim),
-- Use the sandwich theorem
refine squeeze_zero (λ n, norm_nonneg _) _ lintegral_norm_tendsto_zero,
-- Show `∥∫ a, F n a - ∫ f∥ ≤ ∫ a, ∥F n a - f a∥` for all `n`
{ assume n,
have h₁ : integrable (F n) μ := bound_integrable.mono' (F_measurable n) (h_bound _),
have h₂ : integrable f μ :=
⟨f_measurable, has_finite_integral_of_dominated_convergence
bound_integrable.has_finite_integral h_bound h_lim⟩,
rw ← integral_sub h₁ h₂,
exact norm_integral_le_lintegral_norm _ }
end
/-- Lebesgue dominated convergence theorem for filters with a countable basis -/
lemma tendsto_integral_filter_of_dominated_convergence {ι} {l : filter ι}
{F : ι → α → E} {f : α → E} (bound : α → ℝ)
(hl_cb : l.is_countably_generated)
(hF_meas : ∀ᶠ n in l, ae_measurable (F n) μ)
(f_measurable : ae_measurable f μ)
(h_bound : ∀ᶠ n in l, ∀ᵐ a ∂μ, ∥F n a∥ ≤ bound a)
(bound_integrable : integrable bound μ)
(h_lim : ∀ᵐ a ∂μ, tendsto (λ n, F n a) l (𝓝 (f a))) :
tendsto (λn, ∫ a, F n a ∂μ) l (𝓝 $ ∫ a, f a ∂μ) :=
begin
rw hl_cb.tendsto_iff_seq_tendsto,
{ intros x xl,
have hxl, { rw tendsto_at_top' at xl, exact xl },
have h := inter_mem_sets hF_meas h_bound,
replace h := hxl _ h,
rcases h with ⟨k, h⟩,
rw ← tendsto_add_at_top_iff_nat k,
refine tendsto_integral_of_dominated_convergence _ _ _ _ _ _,
{ exact bound },
{ intro, refine (h _ _).1, exact nat.le_add_left _ _ },
{ assumption },
{ assumption },
{ intro, refine (h _ _).2, exact nat.le_add_left _ _ },
{ filter_upwards [h_lim],
assume a h_lim,
apply @tendsto.comp _ _ _ (λn, x (n + k)) (λn, F n a),
{ assumption },
rw tendsto_add_at_top_iff_nat,
assumption } },
end
/-- The Bochner integral of a real-valued function `f : α → ℝ` is the difference between the
integral of the positive part of `f` and the integral of the negative part of `f`. -/
lemma integral_eq_lintegral_max_sub_lintegral_min {f : α → ℝ} (hf : integrable f μ) :
∫ a, f a ∂μ =
ennreal.to_real (∫⁻ a, (ennreal.of_real $ max (f a) 0) ∂μ) -
ennreal.to_real (∫⁻ a, (ennreal.of_real $ - min (f a) 0) ∂μ) :=
let f₁ : α →₁[μ] ℝ := l1.of_fun f hf in
-- Go to the `L¹` space
have eq₁ : ennreal.to_real (∫⁻ a, (ennreal.of_real $ max (f a) 0) ∂μ) = ∥l1.pos_part f₁∥ :=
begin
rw l1.norm_eq_norm_to_fun,
congr' 1,
apply lintegral_congr_ae,
filter_upwards [l1.pos_part_to_fun f₁, l1.to_fun_of_fun f hf],
assume a h₁ h₂,
rw [h₁, h₂, real.norm_eq_abs, abs_of_nonneg],
exact le_max_right _ _
end,
-- Go to the `L¹` space
have eq₂ : ennreal.to_real (∫⁻ a, (ennreal.of_real $ -min (f a) 0) ∂μ) = ∥l1.neg_part f₁∥ :=
begin
rw l1.norm_eq_norm_to_fun,
congr' 1,
apply lintegral_congr_ae,
filter_upwards [l1.neg_part_to_fun_eq_min f₁, l1.to_fun_of_fun f hf],
assume a h₁ h₂,
rw [h₁, h₂, real.norm_eq_abs, abs_of_nonneg],
rw [neg_nonneg],
exact min_le_right _ _
end,
begin
rw [eq₁, eq₂, integral, dif_pos],
exact l1.integral_eq_norm_pos_part_sub _
end
lemma integral_eq_lintegral_of_nonneg_ae {f : α → ℝ} (hf : 0 ≤ᵐ[μ] f) (hfm : ae_measurable f μ) :
∫ a, f a ∂μ = ennreal.to_real (∫⁻ a, (ennreal.of_real $ f a) ∂μ) :=
begin
by_cases hfi : integrable f μ,
{ rw integral_eq_lintegral_max_sub_lintegral_min hfi,
have h_min : ∫⁻ a, ennreal.of_real (-min (f a) 0) ∂μ = 0,
{ rw lintegral_eq_zero_iff',
{ refine hf.mono _,
simp only [pi.zero_apply],
assume a h,
simp only [min_eq_right h, neg_zero, ennreal.of_real_zero] },
{ exact measurable_of_real.comp_ae_measurable (measurable_id.neg.comp_ae_measurable
$ hfm.min ae_measurable_const) } },
have h_max : ∫⁻ a, ennreal.of_real (max (f a) 0) ∂μ = ∫⁻ a, ennreal.of_real (f a) ∂μ,
{ refine lintegral_congr_ae (hf.mono (λ a h, _)),
rw [pi.zero_apply] at h,
rw max_eq_left h },
rw [h_min, h_max, zero_to_real, _root_.sub_zero] },
{ rw integral_undef hfi,
simp_rw [integrable, hfm, has_finite_integral_iff_norm, lt_top_iff_ne_top, ne.def, true_and,
not_not] at hfi,
have : ∫⁻ (a : α), ennreal.of_real (f a) ∂μ = ∫⁻ a, (ennreal.of_real ∥f a∥) ∂μ,
{ refine lintegral_congr_ae (hf.mono $ assume a h, _),
rw [real.norm_eq_abs, abs_of_nonneg h] },
rw [this, hfi], refl }
end
lemma integral_nonneg_of_ae {f : α → ℝ} (hf : 0 ≤ᵐ[μ] f) : 0 ≤ ∫ a, f a ∂μ :=
begin
by_cases hfm : ae_measurable f μ,
{ rw integral_eq_lintegral_of_nonneg_ae hf hfm, exact to_real_nonneg },
{ rw integral_non_ae_measurable hfm }
end
lemma lintegral_coe_eq_integral (f : α → ℝ≥0) (hfi : integrable (λ x, (f x : real)) μ) :
∫⁻ a, f a ∂μ = ennreal.of_real ∫ a, f a ∂μ :=
begin
simp_rw [integral_eq_lintegral_of_nonneg_ae (eventually_of_forall (λ x, (f x).coe_nonneg))
hfi.ae_measurable, ← ennreal.coe_nnreal_eq], rw [ennreal.of_real_to_real],
rw [← lt_top_iff_ne_top], convert hfi.has_finite_integral, ext1 x, rw [real.nnnorm_coe_eq_self]
end
lemma integral_to_real {f : α → ennreal} (hfm : ae_measurable f μ) (hf : ∀ᵐ x ∂μ, f x < ⊤) :
∫ a, (f a).to_real ∂μ = (∫⁻ a, f a ∂μ).to_real :=
begin
rw [integral_eq_lintegral_of_nonneg_ae _ hfm.to_real],
{ rw lintegral_congr_ae, refine hf.mp (eventually_of_forall _),
intros x hx, rw [lt_top_iff_ne_top] at hx, simp [hx] },
{ exact (eventually_of_forall $ λ x, ennreal.to_real_nonneg) }
end
lemma integral_nonneg {f : α → ℝ} (hf : 0 ≤ f) : 0 ≤ ∫ a, f a ∂μ :=
integral_nonneg_of_ae $ eventually_of_forall hf
lemma integral_nonpos_of_ae {f : α → ℝ} (hf : f ≤ᵐ[μ] 0) : ∫ a, f a ∂μ ≤ 0 :=
begin
have hf : 0 ≤ᵐ[μ] (-f) := hf.mono (assume a h, by rwa [pi.neg_apply, pi.zero_apply, neg_nonneg]),
have : 0 ≤ ∫ a, -f a ∂μ := integral_nonneg_of_ae hf,
rwa [integral_neg, neg_nonneg] at this,
end
lemma integral_nonpos {f : α → ℝ} (hf : f ≤ 0) : ∫ a, f a ∂μ ≤ 0 :=
integral_nonpos_of_ae $ eventually_of_forall hf
lemma integral_eq_zero_iff_of_nonneg_ae {f : α → ℝ} (hf : 0 ≤ᵐ[μ] f) (hfi : integrable f μ) :
∫ x, f x ∂μ = 0 ↔ f =ᵐ[μ] 0 :=
by simp_rw [integral_eq_lintegral_of_nonneg_ae hf hfi.1, ennreal.to_real_eq_zero_iff,
lintegral_eq_zero_iff' (ennreal.measurable_of_real.comp_ae_measurable hfi.1),
← ennreal.not_lt_top, ← has_finite_integral_iff_of_real hf, hfi.2, not_true, or_false,
← hf.le_iff_eq, filter.eventually_eq, filter.eventually_le, (∘), pi.zero_apply,
ennreal.of_real_eq_zero]
lemma integral_eq_zero_iff_of_nonneg {f : α → ℝ} (hf : 0 ≤ f) (hfi : integrable f μ) :
∫ x, f x ∂μ = 0 ↔ f =ᵐ[μ] 0 :=
integral_eq_zero_iff_of_nonneg_ae (eventually_of_forall hf) hfi
lemma integral_pos_iff_support_of_nonneg_ae {f : α → ℝ} (hf : 0 ≤ᵐ[μ] f) (hfi : integrable f μ) :
(0 < ∫ x, f x ∂μ) ↔ 0 < μ (function.support f) :=
by simp_rw [(integral_nonneg_of_ae hf).lt_iff_ne, pos_iff_ne_zero, ne.def, @eq_comm ℝ 0,
integral_eq_zero_iff_of_nonneg_ae hf hfi, filter.eventually_eq, ae_iff, pi.zero_apply,
function.support]
lemma integral_pos_iff_support_of_nonneg {f : α → ℝ} (hf : 0 ≤ f) (hfi : integrable f μ) :
(0 < ∫ x, f x ∂μ) ↔ 0 < μ (function.support f) :=
integral_pos_iff_support_of_nonneg_ae (eventually_of_forall hf) hfi
section normed_group
variables {H : Type*} [normed_group H] [second_countable_topology H] [measurable_space H]
[borel_space H]
lemma l1.norm_eq_integral_norm (f : α →₁[μ] H) : ∥f∥ = ∫ a, ∥f a∥ ∂μ :=
by rw [l1.norm_eq_norm_to_fun,
integral_eq_lintegral_of_nonneg_ae (eventually_of_forall $ by simp [norm_nonneg])
(continuous_norm.measurable.comp_ae_measurable f.ae_measurable)]
lemma l1.norm_of_fun_eq_integral_norm {f : α → H} (hf : integrable f μ) :
∥ l1.of_fun f hf ∥ = ∫ a, ∥f a∥ ∂μ :=
begin
rw l1.norm_eq_integral_norm,
refine integral_congr_ae _,
apply (l1.to_fun_of_fun f hf).mono,
intros a ha,
simp [ha]
end
end normed_group
lemma integral_mono_ae {f g : α → ℝ} (hf : integrable f μ) (hg : integrable g μ) (h : f ≤ᵐ[μ] g) :
∫ a, f a ∂μ ≤ ∫ a, g a ∂μ :=
le_of_sub_nonneg $ integral_sub hg hf ▸ integral_nonneg_of_ae $ h.mono (λ a, sub_nonneg_of_le)
lemma integral_mono {f g : α → ℝ} (hf : integrable f μ) (hg : integrable g μ) (h : f ≤ g) :
∫ a, f a ∂μ ≤ ∫ a, g a ∂μ :=
integral_mono_ae hf hg $ eventually_of_forall h
lemma integral_mono_of_nonneg {f g : α → ℝ} (hf : 0 ≤ᵐ[μ] f) (hgi : integrable g μ)
(h : f ≤ᵐ[μ] g) : ∫ a, f a ∂μ ≤ ∫ a, g a ∂μ :=
begin
by_cases hfm : ae_measurable f μ,
{ refine integral_mono_ae ⟨hfm, _⟩ hgi h,
refine (hgi.has_finite_integral.mono $ h.mp $ hf.mono $ λ x hf hfg, _),
simpa [real.norm_eq_abs, abs_of_nonneg hf, abs_of_nonneg (le_trans hf hfg)] },
{ rw [integral_non_ae_measurable hfm],
exact integral_nonneg_of_ae (hf.trans h) }
end
lemma norm_integral_le_integral_norm (f : α → E) : ∥(∫ a, f a ∂μ)∥ ≤ ∫ a, ∥f a∥ ∂μ :=
have le_ae : ∀ᵐ a ∂μ, 0 ≤ ∥f a∥ := eventually_of_forall (λa, norm_nonneg _),
classical.by_cases
( λh : ae_measurable f μ,
calc ∥∫ a, f a ∂μ∥ ≤ ennreal.to_real (∫⁻ a, (ennreal.of_real ∥f a∥) ∂μ) :
norm_integral_le_lintegral_norm _
... = ∫ a, ∥f a∥ ∂μ : (integral_eq_lintegral_of_nonneg_ae le_ae $ ae_measurable.norm h).symm )
( λh : ¬ae_measurable f μ,
begin
rw [integral_non_ae_measurable h, norm_zero],
exact integral_nonneg_of_ae le_ae
end )
lemma norm_integral_le_of_norm_le {f : α → E} {g : α → ℝ} (hg : integrable g μ)
(h : ∀ᵐ x ∂μ, ∥f x∥ ≤ g x) : ∥∫ x, f x ∂μ∥ ≤ ∫ x, g x ∂μ :=
calc ∥∫ x, f x ∂μ∥ ≤ ∫ x, ∥f x∥ ∂μ : norm_integral_le_integral_norm f
... ≤ ∫ x, g x ∂μ :
integral_mono_of_nonneg (eventually_of_forall $ λ x, norm_nonneg _) hg h
lemma integral_finset_sum {ι} (s : finset ι) {f : ι → α → E} (hf : ∀ i, integrable (f i) μ) :
∫ a, ∑ i in s, f i a ∂μ = ∑ i in s, ∫ a, f i a ∂μ :=
begin
refine finset.induction_on s _ _,
{ simp only [integral_zero, finset.sum_empty] },
{ assume i s his ih,
simp only [his, finset.sum_insert, not_false_iff],
rw [integral_add (hf _) (integrable_finset_sum s hf), ih] }
end
lemma simple_func.integral_eq_integral (f : α →ₛ E) (hfi : integrable f μ) :
f.integral μ = ∫ x, f x ∂μ :=
begin
rw [integral_eq f hfi, ← l1.simple_func.of_simple_func_eq_of_fun,
l1.simple_func.integral_l1_eq_integral, l1.simple_func.integral_eq_integral],
exact simple_func.integral_congr hfi (l1.simple_func.to_simple_func_of_simple_func _ _).symm
end
@[simp] lemma integral_const (c : E) : ∫ x : α, c ∂μ = (μ univ).to_real • c :=
begin
by_cases hμ : μ univ < ⊤,
{ haveI : finite_measure μ := ⟨hμ⟩,
calc ∫ x : α, c ∂μ = (simple_func.const α c).integral μ :
((simple_func.const α c).integral_eq_integral (integrable_const _)).symm
... = _ : _,
rw [simple_func.integral],
by_cases ha : nonempty α,
{ resetI, simp [preimage_const_of_mem] },
{ simp [μ.eq_zero_of_not_nonempty ha] } },
{ by_cases hc : c = 0,
{ simp [hc, integral_zero] },
{ have : ¬integrable (λ x : α, c) μ,
{ simp only [integrable_const_iff, not_or_distrib],
exact ⟨hc, hμ⟩ },
simp only [not_lt, top_le_iff] at hμ,
simp [integral_undef, *] } }
end
lemma norm_integral_le_of_norm_le_const [finite_measure μ] {f : α → E} {C : ℝ}
(h : ∀ᵐ x ∂μ, ∥f x∥ ≤ C) :
∥∫ x, f x ∂μ∥ ≤ C * (μ univ).to_real :=
calc ∥∫ x, f x ∂μ∥ ≤ ∫ x, C ∂μ : norm_integral_le_of_norm_le (integrable_const C) h
... = C * (μ univ).to_real : by rw [integral_const, smul_eq_mul, mul_comm]
lemma tendsto_integral_approx_on_univ_of_measurable
{f : α → E} (fmeas : measurable f) (hf : integrable f μ) :
tendsto (λ n, (simple_func.approx_on f fmeas univ 0 trivial n).integral μ) at_top
(𝓝 $ ∫ x, f x ∂μ) :=
begin
have : tendsto (λ n, ∫ x, simple_func.approx_on f fmeas univ 0 trivial n x ∂μ)
at_top (𝓝 $ ∫ x, f x ∂μ) :=
tendsto_integral_of_l1 _ hf
(eventually_of_forall $ simple_func.integrable_approx_on_univ fmeas hf)
(simple_func.tendsto_approx_on_univ_l1_edist fmeas hf),
simpa only [simple_func.integral_eq_integral, simple_func.integrable_approx_on_univ fmeas hf]
end
variable {ν : measure α}
private lemma integral_add_measure_of_measurable
{f : α → E} (fmeas : measurable f) (hμ : integrable f μ) (hν : integrable f ν) :
∫ x, f x ∂(μ + ν) = ∫ x, f x ∂μ + ∫ x, f x ∂ν :=
begin
have hfi := hμ.add_measure hν,
refine tendsto_nhds_unique (tendsto_integral_approx_on_univ_of_measurable fmeas hfi) _,
simpa only [simple_func.integral_add_measure _
(simple_func.integrable_approx_on_univ fmeas hfi _)]
using (tendsto_integral_approx_on_univ_of_measurable fmeas hμ).add
(tendsto_integral_approx_on_univ_of_measurable fmeas hν)
end
lemma integral_add_measure {f : α → E} (hμ : integrable f μ) (hν : integrable f ν) :
∫ x, f x ∂(μ + ν) = ∫ x, f x ∂μ + ∫ x, f x ∂ν :=
begin
have h : ae_measurable f (μ + ν) := hμ.ae_measurable.add_measure hν.ae_measurable,
let g := h.mk f,
have A : f =ᵐ[μ + ν] g := h.ae_eq_mk,
have B : f =ᵐ[μ] g := A.filter_mono (ae_mono (measure.le_add_right (le_refl μ))),
have C : f =ᵐ[ν] g := A.filter_mono (ae_mono (measure.le_add_left (le_refl ν))),
calc ∫ x, f x ∂(μ + ν) = ∫ x, g x ∂(μ + ν) : integral_congr_ae A
... = ∫ x, g x ∂μ + ∫ x, g x ∂ν :
integral_add_measure_of_measurable h.measurable_mk ((integrable_congr B).1 hμ)
((integrable_congr C).1 hν)
... = ∫ x, f x ∂μ + ∫ x, f x ∂ν :
by { congr' 1, { exact integral_congr_ae B.symm }, { exact integral_congr_ae C.symm } }
end
@[simp] lemma integral_zero_measure (f : α → E) : ∫ x, f x ∂0 = 0 :=
norm_le_zero_iff.1 $ le_trans (norm_integral_le_lintegral_norm f) $ by simp
private lemma integral_smul_measure_aux {f : α → E} {c : ennreal}
(h0 : 0 < c) (hc : c < ⊤) (fmeas : measurable f) (hfi : integrable f μ) :
∫ x, f x ∂(c • μ) = c.to_real • ∫ x, f x ∂μ :=
begin
refine tendsto_nhds_unique _
(tendsto_const_nhds.smul (tendsto_integral_approx_on_univ_of_measurable fmeas hfi)),
convert tendsto_integral_approx_on_univ_of_measurable fmeas (hfi.smul_measure hc),
simp only [simple_func.integral, measure.smul_apply, finset.smul_sum, smul_smul,
ennreal.to_real_mul]
end
@[simp] lemma integral_smul_measure (f : α → E) (c : ennreal) :
∫ x, f x ∂(c • μ) = c.to_real • ∫ x, f x ∂μ :=
begin
-- First we consider “degenerate” cases:
-- `c = 0`
rcases (zero_le c).eq_or_lt with rfl|h0, { simp },
-- `f` is not almost everywhere measurable
by_cases hfm : ae_measurable f μ, swap,
{ have : ¬ (ae_measurable f (c • μ)), by simpa [ne_of_gt h0] using hfm,
simp [integral_non_ae_measurable, hfm, this] },
-- `c = ⊤`
rcases (le_top : c ≤ ⊤).eq_or_lt with rfl|hc,
{ rw [ennreal.top_to_real, zero_smul],
by_cases hf : f =ᵐ[μ] 0,
{ have : f =ᵐ[⊤ • μ] 0 := ae_smul_measure hf ⊤,
exact integral_eq_zero_of_ae this },
{ apply integral_undef,
rw [integrable, has_finite_integral, iff_true_intro (hfm.smul_measure ⊤), true_and,
lintegral_smul_measure, top_mul, if_neg],
{ apply lt_irrefl },
{ rw [lintegral_eq_zero_iff' hfm.ennnorm],
refine λ h, hf (h.mono $ λ x, _),
simp } } },
-- `f` is not integrable and `0 < c < ⊤`
by_cases hfi : integrable f μ, swap,
{ rw [integral_undef hfi, smul_zero],
refine integral_undef (mt (λ h, _) hfi),
convert h.smul_measure (ennreal.inv_lt_top.2 h0),
rw [smul_smul, ennreal.inv_mul_cancel (ne_of_gt h0) (ne_of_lt hc), one_smul] },
-- Main case: `0 < c < ⊤`, `f` is almost everywhere measurable and integrable
let g := hfm.mk f,
calc ∫ x, f x ∂(c • μ) = ∫ x, g x ∂(c • μ) : integral_congr_ae $ ae_smul_measure hfm.ae_eq_mk c
... = c.to_real • ∫ x, g x ∂μ :
integral_smul_measure_aux h0 hc hfm.measurable_mk $ hfi.congr hfm.ae_eq_mk
... = c.to_real • ∫ x, f x ∂μ :
by { congr' 1, exact integral_congr_ae (hfm.ae_eq_mk.symm) }
end
lemma integral_map_of_measurable {β} [measurable_space β] {φ : α → β} (hφ : measurable φ)
{f : β → E} (hfm : measurable f) :
∫ y, f y ∂(measure.map φ μ) = ∫ x, f (φ x) ∂μ :=
begin
by_cases hfi : integrable f (measure.map φ μ), swap,
{ rw [integral_undef hfi, integral_undef],
rwa [← integrable_map_measure hfm.ae_measurable hφ] },
refine tendsto_nhds_unique (tendsto_integral_approx_on_univ_of_measurable hfm hfi) _,
convert tendsto_integral_approx_on_univ_of_measurable (hfm.comp hφ)
((integrable_map_measure hfm.ae_measurable hφ).1 hfi),
ext1 i,
simp only [simple_func.approx_on_comp, simple_func.integral, measure.map_apply, hφ,
simple_func.is_measurable_preimage, ← preimage_comp, simple_func.coe_comp],
refine (finset.sum_subset (simple_func.range_comp_subset_range _ hφ) (λ y _ hy, _)).symm,
rw [simple_func.mem_range, ← set.preimage_singleton_eq_empty, simple_func.coe_comp] at hy,
simp [hy]
end
lemma integral_map {β} [measurable_space β] {φ : α → β} (hφ : measurable φ)
{f : β → E} (hfm : ae_measurable f (measure.map φ μ)) :
∫ y, f y ∂(measure.map φ μ) = ∫ x, f (φ x) ∂μ :=
let g := hfm.mk f in calc
∫ y, f y ∂(measure.map φ μ) = ∫ y, g y ∂(measure.map φ μ) : integral_congr_ae hfm.ae_eq_mk
... = ∫ x, g (φ x) ∂μ : integral_map_of_measurable hφ hfm.measurable_mk
... = ∫ x, f (φ x) ∂μ : integral_congr_ae $ ae_eq_comp hφ (hfm.ae_eq_mk).symm
lemma integral_dirac (f : α → E) (a : α) (hfm : measurable f) :
∫ x, f x ∂(measure.dirac a) = f a :=
calc ∫ x, f x ∂(measure.dirac a) = ∫ x, f a ∂(measure.dirac a) :
integral_congr_ae $ eventually_eq_dirac hfm
... = f a : by simp [measure.dirac_apply_of_mem]
end properties
mk_simp_attribute integral_simps "Simp set for integral rules."
attribute [integral_simps] integral_neg integral_smul l1.integral_add l1.integral_sub
l1.integral_smul l1.integral_neg
attribute [irreducible] integral l1.integral
end measure_theory
|
22dda2ed16accf368876c1514e2c2c95fd56bca5 | b2fe74b11b57d362c13326bc5651244f111fa6f4 | /src/analysis/analytic/composition.lean | 28390d9e5b4af57a27a02294077c2f9608533e70 | [
"Apache-2.0"
] | permissive | midfield/mathlib | c4db5fa898b5ac8f2f80ae0d00c95eb6f745f4c7 | 775edc615ecec631d65b6180dbcc7bc26c3abc26 | refs/heads/master | 1,675,330,551,921 | 1,608,304,514,000 | 1,608,304,514,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 56,721 | lean | /-
Copyright (c) 2020 Sébastien Gouëzel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sébastien Gouëzel, Johan Commelin
-/
import analysis.analytic.basic
import combinatorics.composition
/-!
# Composition of analytic functions
in this file we prove that the composition of analytic functions is analytic.
The argument is the following. Assume `g z = ∑' qₙ (z, ..., z)` and `f y = ∑' pₖ (y, ..., y)`. Then
`g (f y) = ∑' qₙ (∑' pₖ (y, ..., y), ..., ∑' pₖ (y, ..., y))
= ∑' qₙ (p_{i₁} (y, ..., y), ..., p_{iₙ} (y, ..., y))`.
For each `n` and `i₁, ..., iₙ`, define a `i₁ + ... + iₙ` multilinear function mapping
`(y₀, ..., y_{i₁ + ... + iₙ - 1})` to
`qₙ (p_{i₁} (y₀, ..., y_{i₁-1}), p_{i₂} (y_{i₁}, ..., y_{i₁ + i₂ - 1}), ..., p_{iₙ} (....)))`.
Then `g ∘ f` is obtained by summing all these multilinear functions.
To formalize this, we use compositions of an integer `N`, i.e., its decompositions into
a sum `i₁ + ... + iₙ` of positive integers. Given such a composition `c` and two formal
multilinear series `q` and `p`, let `q.comp_along_composition p c` be the above multilinear
function. Then the `N`-th coefficient in the power series expansion of `g ∘ f` is the sum of these
terms over all `c : composition N`.
To complete the proof, we need to show that this power series has a positive radius of convergence.
This follows from the fact that `composition N` has cardinality `2^(N-1)` and estimates on
the norm of `qₙ` and `pₖ`, which give summability. We also need to show that it indeed converges to
`g ∘ f`. For this, we note that the composition of partial sums converges to `g ∘ f`, and that it
corresponds to a part of the whole sum, on a subset that increases to the whole space. By
summability of the norms, this implies the overall convergence.
## Main results
* `q.comp p` is the formal composition of the formal multilinear series `q` and `p`.
* `has_fpower_series_at.comp` states that if two functions `g` and `f` admit power series expansions
`q` and `p`, then `g ∘ f` admits a power series expansion given by `q.comp p`.
* `analytic_at.comp` states that the composition of analytic functions is analytic.
* `formal_multilinear_series.comp_assoc` states that composition is associative on formal
multilinear series.
## Implementation details
The main technical difficulty is to write down things. In particular, we need to define precisely
`q.comp_along_composition p c` and to show that it is indeed a continuous multilinear
function. This requires a whole interface built on the class `composition`. Once this is set,
the main difficulty is to reorder the sums, writing the composition of the partial sums as a sum
over some subset of `Σ n, composition n`. We need to check that the reordering is a bijection,
running over difficulties due to the dependent nature of the types under consideration, that are
controlled thanks to the interface for `composition`.
The associativity of composition on formal multilinear series is a nontrivial result: it does not
follow from the associativity of composition of analytic functions, as there is no uniqueness for
the formal multilinear series representing a function (and also, it holds even when the radius of
convergence of the series is `0`). Instead, we give a direct proof, which amounts to reordering
double sums in a careful way. The change of variables is a canonical (combinatorial) bijection
`composition.sigma_equiv_sigma_pi` between `(Σ (a : composition n), composition a.length)` and
`(Σ (c : composition n), Π (i : fin c.length), composition (c.blocks_fun i))`, and is described
in more details below in the paragraph on associativity.
-/
noncomputable theory
variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜]
{E : Type*} [normed_group E] [normed_space 𝕜 E]
{F : Type*} [normed_group F] [normed_space 𝕜 F]
{G : Type*} [normed_group G] [normed_space 𝕜 G]
{H : Type*} [normed_group H] [normed_space 𝕜 H]
open filter list
open_locale topological_space big_operators classical nnreal
/-! ### Composing formal multilinear series -/
namespace formal_multilinear_series
/-!
In this paragraph, we define the composition of formal multilinear series, by summing over all
possible compositions of `n`.
-/
/-- Given a formal multilinear series `p`, a composition `c` of `n` and the index `i` of a
block of `c`, we may define a function on `fin n → E` by picking the variables in the `i`-th block
of `n`, and applying the corresponding coefficient of `p` to these variables. This function is
called `p.apply_composition c v i` for `v : fin n → E` and `i : fin c.length`. -/
def apply_composition
(p : formal_multilinear_series 𝕜 E F) {n : ℕ} (c : composition n) :
(fin n → E) → (fin (c.length) → F) :=
λ v i, p (c.blocks_fun i) (v ∘ (c.embedding i))
lemma apply_composition_ones (p : formal_multilinear_series 𝕜 E F) (n : ℕ) :
apply_composition p (composition.ones n) =
λ v i, p 1 (λ _, v (fin.cast_le (composition.length_le _) i)) :=
begin
funext v i,
apply p.congr (composition.ones_blocks_fun _ _),
intros j hjn hj1,
obtain rfl : j = 0, { linarith },
refine congr_arg v _,
rw [fin.ext_iff, fin.coe_cast_le, composition.ones_embedding, fin.coe_mk],
end
/-- Technical lemma stating how `p.apply_composition` commutes with updating variables. This
will be the key point to show that functions constructed from `apply_composition` retain
multilinearity. -/
lemma apply_composition_update
(p : formal_multilinear_series 𝕜 E F) {n : ℕ} (c : composition n)
(j : fin n) (v : fin n → E) (z : E) :
p.apply_composition c (function.update v j z) =
function.update (p.apply_composition c v) (c.index j)
(p (c.blocks_fun (c.index j))
(function.update (v ∘ (c.embedding (c.index j))) (c.inv_embedding j) z)) :=
begin
ext k,
by_cases h : k = c.index j,
{ rw h,
let r : fin (c.blocks_fun (c.index j)) → fin n := c.embedding (c.index j),
simp only [function.update_same],
change p (c.blocks_fun (c.index j)) ((function.update v j z) ∘ r) = _,
let j' := c.inv_embedding j,
suffices B : (function.update v j z) ∘ r = function.update (v ∘ r) j' z,
by rw B,
suffices C : (function.update v (r j') z) ∘ r = function.update (v ∘ r) j' z,
by { convert C, exact (c.embedding_comp_inv j).symm },
exact function.update_comp_eq_of_injective _ (c.embedding _).injective _ _ },
{ simp only [h, function.update_eq_self, function.update_noteq, ne.def, not_false_iff],
let r : fin (c.blocks_fun k) → fin n := c.embedding k,
change p (c.blocks_fun k) ((function.update v j z) ∘ r) = p (c.blocks_fun k) (v ∘ r),
suffices B : (function.update v j z) ∘ r = v ∘ r, by rw B,
apply function.update_comp_eq_of_not_mem_range,
rwa c.mem_range_embedding_iff' }
end
/-- Given two formal multilinear series `q` and `p` and a composition `c` of `n`, one may
form a multilinear map in `n` variables by applying the right coefficient of `p` to each block of
the composition, and then applying `q c.length` to the resulting vector. It is called
`q.comp_along_composition_multilinear p c`. This function admits a version as a continuous
multilinear map, called `q.comp_along_composition p c` below. -/
def comp_along_composition_multilinear {n : ℕ}
(q : formal_multilinear_series 𝕜 F G) (p : formal_multilinear_series 𝕜 E F)
(c : composition n) : multilinear_map 𝕜 (λ i : fin n, E) G :=
{ to_fun := λ v, q c.length (p.apply_composition c v),
map_add' := λ v i x y, by simp only [apply_composition_update,
continuous_multilinear_map.map_add],
map_smul' := λ v i c x, by simp only [apply_composition_update,
continuous_multilinear_map.map_smul] }
/-- The norm of `q.comp_along_composition_multilinear p c` is controlled by the product of
the norms of the relevant bits of `q` and `p`. -/
lemma comp_along_composition_multilinear_bound {n : ℕ}
(q : formal_multilinear_series 𝕜 F G) (p : formal_multilinear_series 𝕜 E F)
(c : composition n) (v : fin n → E) :
∥q.comp_along_composition_multilinear p c v∥ ≤
∥q c.length∥ * (∏ i, ∥p (c.blocks_fun i)∥) * (∏ i : fin n, ∥v i∥) :=
calc ∥q.comp_along_composition_multilinear p c v∥ = ∥q c.length (p.apply_composition c v)∥ : rfl
... ≤ ∥q c.length∥ * ∏ i, ∥p.apply_composition c v i∥ : continuous_multilinear_map.le_op_norm _ _
... ≤ ∥q c.length∥ * ∏ i, ∥p (c.blocks_fun i)∥ * ∏ j : fin (c.blocks_fun i), ∥(v ∘ (c.embedding i)) j∥ :
begin
apply mul_le_mul_of_nonneg_left _ (norm_nonneg _),
refine finset.prod_le_prod (λ i hi, norm_nonneg _) (λ i hi, _),
apply continuous_multilinear_map.le_op_norm,
end
... = ∥q c.length∥ * (∏ i, ∥p (c.blocks_fun i)∥) *
∏ i (j : fin (c.blocks_fun i)), ∥(v ∘ (c.embedding i)) j∥ :
by rw [finset.prod_mul_distrib, mul_assoc]
... = ∥q c.length∥ * (∏ i, ∥p (c.blocks_fun i)∥) * (∏ i : fin n, ∥v i∥) :
by { rw [← c.blocks_fin_equiv.prod_comp, ← finset.univ_sigma_univ, finset.prod_sigma],
congr }
/-- Given two formal multilinear series `q` and `p` and a composition `c` of `n`, one may
form a continuous multilinear map in `n` variables by applying the right coefficient of `p` to each
block of the composition, and then applying `q c.length` to the resulting vector. It is
called `q.comp_along_composition p c`. It is constructed from the analogous multilinear
function `q.comp_along_composition_multilinear p c`, together with a norm control to get
the continuity. -/
def comp_along_composition {n : ℕ}
(q : formal_multilinear_series 𝕜 F G) (p : formal_multilinear_series 𝕜 E F)
(c : composition n) : continuous_multilinear_map 𝕜 (λ i : fin n, E) G :=
(q.comp_along_composition_multilinear p c).mk_continuous _
(q.comp_along_composition_multilinear_bound p c)
@[simp] lemma comp_along_composition_apply {n : ℕ}
(q : formal_multilinear_series 𝕜 F G) (p : formal_multilinear_series 𝕜 E F)
(c : composition n) (v : fin n → E) :
(q.comp_along_composition p c) v = q c.length (p.apply_composition c v) := rfl
/-- The norm of `q.comp_along_composition p c` is controlled by the product of
the norms of the relevant bits of `q` and `p`. -/
lemma comp_along_composition_norm {n : ℕ}
(q : formal_multilinear_series 𝕜 F G) (p : formal_multilinear_series 𝕜 E F)
(c : composition n) :
∥q.comp_along_composition p c∥ ≤ ∥q c.length∥ * ∏ i, ∥p (c.blocks_fun i)∥ :=
multilinear_map.mk_continuous_norm_le _
(mul_nonneg (norm_nonneg _) (finset.prod_nonneg (λ i hi, norm_nonneg _))) _
lemma comp_along_composition_nnnorm {n : ℕ}
(q : formal_multilinear_series 𝕜 F G) (p : formal_multilinear_series 𝕜 E F)
(c : composition n) :
nnnorm (q.comp_along_composition p c) ≤ nnnorm (q c.length) * ∏ i, nnnorm (p (c.blocks_fun i)) :=
by simpa only [← nnreal.coe_le_coe, coe_nnnorm, nnreal.coe_mul, coe_nnnorm, nnreal.coe_prod, coe_nnnorm]
using q.comp_along_composition_norm p c
/-- Formal composition of two formal multilinear series. The `n`-th coefficient in the composition
is defined to be the sum of `q.comp_along_composition p c` over all compositions of
`n`. In other words, this term (as a multilinear function applied to `v_0, ..., v_{n-1}`) is
`∑'_{k} ∑'_{i₁ + ... + iₖ = n} pₖ (q_{i_1} (...), ..., q_{i_k} (...))`, where one puts all variables
`v_0, ..., v_{n-1}` in increasing order in the dots.-/
protected def comp (q : formal_multilinear_series 𝕜 F G) (p : formal_multilinear_series 𝕜 E F) :
formal_multilinear_series 𝕜 E G :=
λ n, ∑ c : composition n, q.comp_along_composition p c
/-- The `0`-th coefficient of `q.comp p` is `q 0`. Since these maps are multilinear maps in zero
variables, but on different spaces, we can not state this directly, so we state it when applied to
arbitrary vectors (which have to be the zero vector). -/
lemma comp_coeff_zero (q : formal_multilinear_series 𝕜 F G) (p : formal_multilinear_series 𝕜 E F)
(v : fin 0 → E) (v' : fin 0 → F) :
(q.comp p) 0 v = q 0 v' :=
begin
let c : composition 0 := composition.ones 0,
dsimp [formal_multilinear_series.comp],
have : {c} = (finset.univ : finset (composition 0)),
{ apply finset.eq_of_subset_of_card_le; simp [finset.card_univ, composition_card 0] },
rw ← this,
simp only [finset.sum_singleton, continuous_multilinear_map.sum_apply],
change q c.length (p.apply_composition c v) = q 0 v',
congr' with i,
simp only [composition.ones_length] at i,
exact fin_zero_elim i
end
@[simp] lemma comp_coeff_zero'
(q : formal_multilinear_series 𝕜 F G) (p : formal_multilinear_series 𝕜 E F) (v : fin 0 → E) :
(q.comp p) 0 v = q 0 (λ i, 0) :=
q.comp_coeff_zero p v _
/-- The `0`-th coefficient of `q.comp p` is `q 0`. When `p` goes from `E` to `E`, this can be
expressed as a direct equality -/
lemma comp_coeff_zero'' (q : formal_multilinear_series 𝕜 E F) (p : formal_multilinear_series 𝕜 E E) :
(q.comp p) 0 = q 0 :=
by { ext v, exact q.comp_coeff_zero p _ _ }
/-!
### The identity formal power series
We will now define the identity power series, and show that it is a neutral element for left and
right composition.
-/
section
variables (𝕜 E)
/-- The identity formal multilinear series, with all coefficients equal to `0` except for `n = 1`
where it is (the continuous multilinear version of) the identity. -/
def id : formal_multilinear_series 𝕜 E E
| 0 := 0
| 1 := (continuous_multilinear_curry_fin1 𝕜 E E).symm (continuous_linear_map.id 𝕜 E)
| _ := 0
/-- The first coefficient of `id 𝕜 E` is the identity. -/
@[simp] lemma id_apply_one (v : fin 1 → E) : (formal_multilinear_series.id 𝕜 E) 1 v = v 0 := rfl
/-- The `n`th coefficient of `id 𝕜 E` is the identity when `n = 1`. We state this in a dependent
way, as it will often appear in this form. -/
lemma id_apply_one' {n : ℕ} (h : n = 1) (v : fin n → E) :
(id 𝕜 E) n v = v ⟨0, h.symm ▸ zero_lt_one⟩ :=
begin
let w : fin 1 → E := λ i, v ⟨i.1, h.symm ▸ i.2⟩,
have : v ⟨0, h.symm ▸ zero_lt_one⟩ = w 0 := rfl,
rw [this, ← id_apply_one 𝕜 E w],
apply congr _ h,
intros,
obtain rfl : i = 0, { linarith },
exact this,
end
/-- For `n ≠ 1`, the `n`-th coefficient of `id 𝕜 E` is zero, by definition. -/
@[simp] lemma id_apply_ne_one {n : ℕ} (h : n ≠ 1) : (formal_multilinear_series.id 𝕜 E) n = 0 :=
by { cases n, { refl }, cases n, { contradiction }, refl }
end
@[simp] theorem comp_id (p : formal_multilinear_series 𝕜 E F) : p.comp (id 𝕜 E) = p :=
begin
ext1 n,
dsimp [formal_multilinear_series.comp],
rw finset.sum_eq_single (composition.ones n),
show comp_along_composition p (id 𝕜 E) (composition.ones n) = p n,
{ ext v,
rw comp_along_composition_apply,
apply p.congr (composition.ones_length n),
intros,
rw apply_composition_ones,
refine congr_arg v _,
rw [fin.ext_iff, fin.coe_cast_le, fin.coe_mk, fin.coe_mk], },
show ∀ (b : composition n),
b ∈ finset.univ → b ≠ composition.ones n → comp_along_composition p (id 𝕜 E) b = 0,
{ assume b _ hb,
obtain ⟨k, hk, lt_k⟩ : ∃ (k : ℕ) (H : k ∈ composition.blocks b), 1 < k :=
composition.ne_ones_iff.1 hb,
obtain ⟨i, i_lt, hi⟩ : ∃ (i : ℕ) (h : i < b.blocks.length), b.blocks.nth_le i h = k :=
nth_le_of_mem hk,
let j : fin b.length := ⟨i, b.blocks_length ▸ i_lt⟩,
have A : 1 < b.blocks_fun j := by convert lt_k,
ext v,
rw [comp_along_composition_apply, continuous_multilinear_map.zero_apply],
apply continuous_multilinear_map.map_coord_zero _ j,
dsimp [apply_composition],
rw id_apply_ne_one _ _ (ne_of_gt A),
refl },
{ simp }
end
theorem id_comp (p : formal_multilinear_series 𝕜 E F) (h : p 0 = 0) : (id 𝕜 F).comp p = p :=
begin
ext1 n,
by_cases hn : n = 0,
{ rw [hn, h],
ext v,
rw [comp_coeff_zero', id_apply_ne_one _ _ zero_ne_one],
refl },
{ dsimp [formal_multilinear_series.comp],
have n_pos : 0 < n := bot_lt_iff_ne_bot.mpr hn,
rw finset.sum_eq_single (composition.single n n_pos),
show comp_along_composition (id 𝕜 F) p (composition.single n n_pos) = p n,
{ ext v,
rw [comp_along_composition_apply, id_apply_one' _ _ (composition.single_length n_pos)],
dsimp [apply_composition],
refine p.congr rfl (λ i him hin, congr_arg v $ _),
ext, simp },
show ∀ (b : composition n),
b ∈ finset.univ → b ≠ composition.single n n_pos → comp_along_composition (id 𝕜 F) p b = 0,
{ assume b _ hb,
have A : b.length ≠ 1, by simpa [composition.eq_single_iff] using hb,
ext v,
rw [comp_along_composition_apply, id_apply_ne_one _ _ A],
refl },
{ simp } }
end
/-! ### Summability properties of the composition of formal power series-/
/-- If two formal multilinear series have positive radius of convergence, then the terms appearing
in the definition of their composition are also summable (when multiplied by a suitable positive
geometric term). -/
theorem comp_summable_nnreal
(q : formal_multilinear_series 𝕜 F G) (p : formal_multilinear_series 𝕜 E F)
(hq : 0 < q.radius) (hp : 0 < p.radius) :
∃ (r : ℝ≥0), 0 < r ∧ summable (λ i, nnnorm (q.comp_along_composition p i.2) * r ^ i.1 :
(Σ n, composition n) → ℝ≥0) :=
begin
/- This follows from the fact that the growth rate of `∥qₙ∥` and `∥pₙ∥` is at most geometric,
giving a geometric bound on each `∥q.comp_along_composition p op∥`, together with the
fact that there are `2^(n-1)` compositions of `n`, giving at most a geometric loss. -/
rcases ennreal.lt_iff_exists_nnreal_btwn.1 hq with ⟨rq, rq_pos, hrq⟩,
rcases ennreal.lt_iff_exists_nnreal_btwn.1 hp with ⟨rp, rp_pos, hrp⟩,
obtain ⟨Cq, hCq⟩ : ∃ (Cq : ℝ≥0), ∀ n, nnnorm (q n) * rq^n ≤ Cq := q.bound_of_lt_radius hrq,
obtain ⟨Cp, hCp⟩ : ∃ (Cp : ℝ≥0), ∀ n, nnnorm (p n) * rp^n ≤ Cp := p.bound_of_lt_radius hrp,
let r0 : ℝ≥0 := (4 * max Cp 1)⁻¹,
set r := min rp 1 * min rq 1 * r0,
have r_pos : 0 < r,
{ apply mul_pos (mul_pos _ _),
{ rw [nnreal.inv_pos],
apply mul_pos,
{ norm_num },
{ exact lt_of_lt_of_le zero_lt_one (le_max_right _ _) } },
{ rw ennreal.coe_pos at rp_pos, simp [rp_pos, zero_lt_one] },
{ rw ennreal.coe_pos at rq_pos, simp [rq_pos, zero_lt_one] } },
let a : ennreal := ((4 : ℝ≥0) ⁻¹ : ℝ≥0),
have two_a : 2 * a < 1,
{ change ((2 : ℝ≥0) : ennreal) * ((4 : ℝ≥0) ⁻¹ : ℝ≥0) < (1 : ℝ≥0),
rw [← ennreal.coe_mul, ennreal.coe_lt_coe, ← nnreal.coe_lt_coe, nnreal.coe_mul],
change (2 : ℝ) * (4 : ℝ)⁻¹ < 1,
norm_num },
have I : ∀ (i : Σ (n : ℕ), composition n),
↑(nnnorm (q.comp_along_composition p i.2) * r ^ i.1) ≤ (Cq : ennreal) * a ^ i.1,
{ rintros ⟨n, c⟩,
rw [← ennreal.coe_pow, ← ennreal.coe_mul, ennreal.coe_le_coe],
calc nnnorm (q.comp_along_composition p c) * r ^ n
≤ (nnnorm (q c.length) * ∏ i, nnnorm (p (c.blocks_fun i))) * r ^ n :
mul_le_mul_of_nonneg_right (q.comp_along_composition_nnnorm p c) (bot_le)
... = (nnnorm (q c.length) * (min rq 1)^n) *
((∏ i, nnnorm (p (c.blocks_fun i))) * (min rp 1) ^ n) *
r0 ^ n : by { dsimp [r], ring_exp }
... ≤ (nnnorm (q c.length) * (min rq 1) ^ c.length) *
(∏ i, nnnorm (p (c.blocks_fun i)) * (min rp 1) ^ (c.blocks_fun i)) * r0 ^ n :
begin
apply_rules [mul_le_mul, bot_le, le_refl, pow_le_pow_of_le_one, min_le_right, c.length_le],
apply le_of_eq,
rw finset.prod_mul_distrib,
congr' 1,
conv_lhs { rw [← c.sum_blocks_fun, ← finset.prod_pow_eq_pow_sum] },
end
... ≤ Cq * (∏ i : fin c.length, Cp) * r0 ^ n :
begin
apply_rules [mul_le_mul, bot_le, le_trans _ (hCq c.length), le_refl, finset.prod_le_prod',
pow_le_pow_of_le_left, min_le_left],
assume i hi,
refine le_trans (mul_le_mul (le_refl _) _ bot_le bot_le) (hCp (c.blocks_fun i)),
exact pow_le_pow_of_le_left bot_le (min_le_left _ _) _
end
... ≤ Cq * (max Cp 1) ^ n * r0 ^ n :
begin
apply_rules [mul_le_mul, bot_le, le_refl],
simp only [finset.card_fin, finset.prod_const],
refine le_trans (pow_le_pow_of_le_left bot_le (le_max_left Cp 1) c.length) _,
apply pow_le_pow (le_max_right Cp 1) c.length_le,
end
... = Cq * 4⁻¹ ^ n :
begin
dsimp [r0],
have A : (4 : ℝ≥0) ≠ 0, by norm_num,
have B : max Cp 1 ≠ 0 :=
ne_of_gt (lt_of_lt_of_le zero_lt_one (le_max_right Cp 1)),
field_simp [A, B],
ring_exp
end },
refine ⟨r, r_pos, _⟩,
rw [← ennreal.tsum_coe_ne_top_iff_summable],
apply ne_of_lt,
calc (∑' (i : Σ (n : ℕ), composition n), ↑(nnnorm (q.comp_along_composition p i.2) * r ^ i.1))
≤ (∑' (i : Σ (n : ℕ), composition n), (Cq : ennreal) * a ^ i.1) : ennreal.tsum_le_tsum I
... = (∑' (n : ℕ), (∑' (c : composition n), (Cq : ennreal) * a ^ n)) : ennreal.tsum_sigma' _
... = (∑' (n : ℕ), ↑(fintype.card (composition n)) * (Cq : ennreal) * a ^ n) :
begin
congr' 1 with n : 1,
rw [tsum_fintype, finset.sum_const, nsmul_eq_mul, finset.card_univ, mul_assoc]
end
... ≤ (∑' (n : ℕ), (2 : ennreal) ^ n * (Cq : ennreal) * a ^ n) :
begin
apply ennreal.tsum_le_tsum (λ n, _),
apply ennreal.mul_le_mul (ennreal.mul_le_mul _ (le_refl _)) (le_refl _),
rw composition_card,
simp only [nat.cast_bit0, nat.cast_one, nat.cast_pow],
apply ennreal.pow_le_pow _ (nat.sub_le n 1),
have : (1 : ℝ≥0) ≤ (2 : ℝ≥0), by norm_num,
rw ← ennreal.coe_le_coe at this,
exact this
end
... = (∑' (n : ℕ), (Cq : ennreal) * (2 * a) ^ n) : by { congr' 1 with n : 1, rw mul_pow, ring }
... = (Cq : ennreal) * (1 - 2 * a) ⁻¹ : by rw [ennreal.tsum_mul_left, ennreal.tsum_geometric]
... < ⊤ : by simp [lt_top_iff_ne_top, ennreal.mul_eq_top, two_a]
end
/-- Bounding below the radius of the composition of two formal multilinear series assuming
summability over all compositions. -/
theorem le_comp_radius_of_summable
(q : formal_multilinear_series 𝕜 F G) (p : formal_multilinear_series 𝕜 E F) (r : ℝ≥0)
(hr : summable (λ i, nnnorm (q.comp_along_composition p i.2) * r ^ i.1 :
(Σ n, composition n) → ℝ≥0)) :
(r : ennreal) ≤ (q.comp p).radius :=
begin
apply le_radius_of_bound _ (tsum (λ (i : Σ (n : ℕ), composition n),
(nnnorm (comp_along_composition q p i.snd) * r ^ i.fst))),
assume n,
calc nnnorm (formal_multilinear_series.comp q p n) * r ^ n ≤
∑' (c : composition n), nnnorm (comp_along_composition q p c) * r ^ n :
begin
rw [tsum_fintype, ← finset.sum_mul],
exact mul_le_mul_of_nonneg_right (nnnorm_sum_le _ _) bot_le
end
... ≤ ∑' (i : Σ (n : ℕ), composition n),
nnnorm (comp_along_composition q p i.snd) * r ^ i.fst :
begin
let f : composition n → (Σ (n : ℕ), composition n) := λ c, ⟨n, c⟩,
have : function.injective f, by tidy,
convert nnreal.tsum_comp_le_tsum_of_inj hr this
end
end
/-!
### Composing analytic functions
Now, we will prove that the composition of the partial sums of `q` and `p` up to order `N` is
given by a sum over some large subset of `Σ n, composition n` of `q.comp_along_composition p`, to
deduce that the series for `q.comp p` indeed converges to `g ∘ f` when `q` is a power series for
`g` and `p` is a power series for `f`.
This proof is a big reindexing argument of a sum. Since it is a bit involved, we define first
the source of the change of variables (`comp_partial_source`), its target
(`comp_partial_target`) and the change of variables itself (`comp_change_of_variables`) before
giving the main statement in `comp_partial_sum`. -/
/-- Source set in the change of variables to compute the composition of partial sums of formal
power series.
See also `comp_partial_sum`. -/
def comp_partial_sum_source (N : ℕ) : finset (Σ n, (fin n) → ℕ) :=
finset.sigma (finset.range N) (λ (n : ℕ), fintype.pi_finset (λ (i : fin n), finset.Ico 1 N) : _)
@[simp] lemma mem_comp_partial_sum_source_iff (N : ℕ) (i : Σ n, (fin n) → ℕ) :
i ∈ comp_partial_sum_source N ↔ i.1 < N ∧ ∀ (a : fin i.1), 1 ≤ i.2 a ∧ i.2 a < N :=
by simp only [comp_partial_sum_source, finset.Ico.mem,
fintype.mem_pi_finset, finset.mem_sigma, finset.mem_range]
/-- Change of variables appearing to compute the composition of partial sums of formal
power series -/
def comp_change_of_variables (N : ℕ) (i : Σ n, (fin n) → ℕ) (hi : i ∈ comp_partial_sum_source N) :
(Σ n, composition n) :=
begin
rcases i with ⟨n, f⟩,
rw mem_comp_partial_sum_source_iff at hi,
refine ⟨∑ j, f j, of_fn (λ a, f a), λ i hi', _, by simp [sum_of_fn]⟩,
obtain ⟨j, rfl⟩ : ∃ (j : fin n), f j = i, by rwa [mem_of_fn, set.mem_range] at hi',
exact (hi.2 j).1
end
@[simp] lemma comp_change_of_variables_length
(N : ℕ) {i : Σ n, (fin n) → ℕ} (hi : i ∈ comp_partial_sum_source N) :
composition.length (comp_change_of_variables N i hi).2 = i.1 :=
begin
rcases i with ⟨k, blocks_fun⟩,
dsimp [comp_change_of_variables],
simp only [composition.length, map_of_fn, length_of_fn]
end
lemma comp_change_of_variables_blocks_fun
(N : ℕ) {i : Σ n, (fin n) → ℕ} (hi : i ∈ comp_partial_sum_source N) (j : fin i.1) :
(comp_change_of_variables N i hi).2.blocks_fun
⟨j, (comp_change_of_variables_length N hi).symm ▸ j.2⟩ = i.2 j :=
begin
rcases i with ⟨n, f⟩,
dsimp [composition.blocks_fun, composition.blocks, comp_change_of_variables],
simp only [map_of_fn, nth_le_of_fn', function.comp_app],
apply congr_arg,
rw [fin.ext_iff, fin.mk_coe]
end
/-- Target set in the change of variables to compute the composition of partial sums of formal
power series, here given a a set. -/
def comp_partial_sum_target_set (N : ℕ) : set (Σ n, composition n) :=
{i | (i.2.length < N) ∧ (∀ (j : fin i.2.length), i.2.blocks_fun j < N)}
lemma comp_partial_sum_target_subset_image_comp_partial_sum_source
(N : ℕ) (i : Σ n, composition n) (hi : i ∈ comp_partial_sum_target_set N) :
∃ j (hj : j ∈ comp_partial_sum_source N), i = comp_change_of_variables N j hj :=
begin
rcases i with ⟨n, c⟩,
refine ⟨⟨c.length, c.blocks_fun⟩, _, _⟩,
{ simp only [comp_partial_sum_target_set, set.mem_set_of_eq] at hi,
simp only [mem_comp_partial_sum_source_iff, hi.left, hi.right, true_and, and_true],
exact λ a, c.one_le_blocks' _ },
{ dsimp [comp_change_of_variables],
rw composition.sigma_eq_iff_blocks_eq,
simp only [composition.blocks_fun, composition.blocks, subtype.coe_eta, nth_le_map'],
conv_lhs { rw ← of_fn_nth_le c.blocks },
simp only [fin.val_eq_coe], refl, /- where does this fin.val come from? -/ }
end
/-- Target set in the change of variables to compute the composition of partial sums of formal
power series, here given a a finset.
See also `comp_partial_sum`. -/
def comp_partial_sum_target (N : ℕ) : finset (Σ n, composition n) :=
set.finite.to_finset $ (finset.finite_to_set _).dependent_image
(comp_partial_sum_target_subset_image_comp_partial_sum_source N)
@[simp] lemma mem_comp_partial_sum_target_iff {N : ℕ} {a : Σ n, composition n} :
a ∈ comp_partial_sum_target N ↔ a.2.length < N ∧ (∀ (j : fin a.2.length), a.2.blocks_fun j < N) :=
by simp [comp_partial_sum_target, comp_partial_sum_target_set]
/-- The auxiliary set corresponding to the composition of partial sums asymptotically contains
all possible compositions. -/
lemma comp_partial_sum_target_tendsto_at_top :
tendsto comp_partial_sum_target at_top at_top :=
begin
apply monotone.tendsto_at_top_finset,
{ assume m n hmn a ha,
have : ∀ i, i < m → i < n := λ i hi, lt_of_lt_of_le hi hmn,
tidy },
{ rintros ⟨n, c⟩,
simp only [mem_comp_partial_sum_target_iff],
obtain ⟨n, hn⟩ : bdd_above ↑(finset.univ.image (λ (i : fin c.length), c.blocks_fun i)) :=
finset.bdd_above _,
refine ⟨max n c.length + 1, lt_of_le_of_lt (le_max_right n c.length) (lt_add_one _),
λ j, lt_of_le_of_lt (le_trans _ (le_max_left _ _)) (lt_add_one _)⟩,
apply hn,
simp only [finset.mem_image_of_mem, finset.mem_coe, finset.mem_univ] }
end
/-- Composing the partial sums of two multilinear series coincides with the sum over all
compositions in `comp_partial_sum_target N`. This is precisely the motivation for the definition of
`comp_partial_sum_target N`. -/
lemma comp_partial_sum
(q : formal_multilinear_series 𝕜 F G) (p : formal_multilinear_series 𝕜 E F) (N : ℕ) (z : E) :
q.partial_sum N (∑ i in finset.Ico 1 N, p i (λ j, z)) =
∑ i in comp_partial_sum_target N, q.comp_along_composition_multilinear p i.2 (λ j, z) :=
begin
-- we expand the composition, using the multilinearity of `q` to expand along each coordinate.
suffices H : ∑ n in finset.range N, ∑ r in fintype.pi_finset (λ (i : fin n), finset.Ico 1 N),
q n (λ (i : fin n), p (r i) (λ j, z)) =
∑ i in comp_partial_sum_target N, q.comp_along_composition_multilinear p i.2 (λ j, z),
by simpa only [formal_multilinear_series.partial_sum,
continuous_multilinear_map.map_sum_finset] using H,
-- rewrite the first sum as a big sum over a sigma type
rw ← @finset.sum_sigma _ _ _ _
(finset.range N) (λ (n : ℕ), (fintype.pi_finset (λ (i : fin n), finset.Ico 1 N)) : _)
(λ i, q i.1 (λ (j : fin i.1), p (i.2 j) (λ (k : fin (i.2 j)), z))),
show ∑ i in comp_partial_sum_source N,
q i.1 (λ (j : fin i.1), p (i.2 j) (λ (k : fin (i.2 j)), z)) =
∑ i in comp_partial_sum_target N, q.comp_along_composition_multilinear p i.2 (λ j, z),
-- show that the two sums correspond to each other by reindexing the variables.
apply finset.sum_bij (comp_change_of_variables N),
-- To conclude, we should show that the correspondance we have set up is indeed a bijection
-- between the index sets of the two sums.
-- 1 - show that the image belongs to `comp_partial_sum_target N`
{ rintros ⟨k, blocks_fun⟩ H,
rw mem_comp_partial_sum_source_iff at H,
simp only [mem_comp_partial_sum_target_iff, composition.length, composition.blocks, H.left,
map_of_fn, length_of_fn, true_and, comp_change_of_variables],
assume j,
simp only [composition.blocks_fun, (H.right _).right, nth_le_of_fn'] },
-- 2 - show that the composition gives the `comp_along_composition` application
{ rintros ⟨k, blocks_fun⟩ H,
apply congr _ (comp_change_of_variables_length N H).symm,
intros,
rw ← comp_change_of_variables_blocks_fun N H,
refl },
-- 3 - show that the map is injective
{ rintros ⟨k, blocks_fun⟩ ⟨k', blocks_fun'⟩ H H' heq,
obtain rfl : k = k',
{ have := (comp_change_of_variables_length N H).symm,
rwa [heq, comp_change_of_variables_length] at this, },
congr,
funext i,
calc blocks_fun i = (comp_change_of_variables N _ H).2.blocks_fun _ :
(comp_change_of_variables_blocks_fun N H i).symm
... = (comp_change_of_variables N _ H').2.blocks_fun _ :
begin
apply composition.blocks_fun_congr; try { rw heq },
refl
end
... = blocks_fun' i : comp_change_of_variables_blocks_fun N H' i },
-- 4 - show that the map is surjective
{ assume i hi,
apply comp_partial_sum_target_subset_image_comp_partial_sum_source N i,
simpa [comp_partial_sum_target] using hi }
end
end formal_multilinear_series
open formal_multilinear_series
/-- If two functions `g` and `f` have power series `q` and `p` respectively at `f x` and `x`, then
`g ∘ f` admits the power series `q.comp p` at `x`. -/
theorem has_fpower_series_at.comp {g : F → G} {f : E → F}
{q : formal_multilinear_series 𝕜 F G} {p : formal_multilinear_series 𝕜 E F} {x : E}
(hg : has_fpower_series_at g q (f x)) (hf : has_fpower_series_at f p x) :
has_fpower_series_at (g ∘ f) (q.comp p) x :=
begin
/- Consider `rf` and `rg` such that `f` and `g` have power series expansion on the disks
of radius `rf` and `rg`. -/
rcases hg with ⟨rg, Hg⟩,
rcases hf with ⟨rf, Hf⟩,
/- The terms defining `q.comp p` are geometrically summable in a disk of some radius `r`. -/
rcases q.comp_summable_nnreal p Hg.radius_pos Hf.radius_pos with ⟨r, r_pos, hr⟩,
/- We will consider `y` which is smaller than `r` and `rf`, and also small enough that
`f (x + y)` is close enough to `f x` to be in the disk where `g` is well behaved. Let
`min (r, rf, δ)` be this new radius.-/
have : continuous_at f x := Hf.analytic_at.continuous_at,
obtain ⟨δ, δpos, hδ⟩ : ∃ (δ : ennreal) (H : 0 < δ),
∀ {z : E}, z ∈ emetric.ball x δ → f z ∈ emetric.ball (f x) rg,
{ have : emetric.ball (f x) rg ∈ 𝓝 (f x) := emetric.ball_mem_nhds _ Hg.r_pos,
rcases emetric.mem_nhds_iff.1 (Hf.analytic_at.continuous_at this) with ⟨δ, δpos, Hδ⟩,
exact ⟨δ, δpos, λ z hz, Hδ hz⟩ },
let rf' := min rf δ,
have min_pos : 0 < min rf' r,
by simp only [r_pos, Hf.r_pos, δpos, lt_min_iff, ennreal.coe_pos, and_self],
/- We will show that `g ∘ f` admits the power series `q.comp p` in the disk of
radius `min (r, rf', δ)`. -/
refine ⟨min rf' r, _⟩,
refine ⟨le_trans (min_le_right rf' r)
(formal_multilinear_series.le_comp_radius_of_summable q p r hr), min_pos, λ y hy, _⟩,
/- Let `y` satisfy `∥y∥ < min (r, rf', δ)`. We want to show that `g (f (x + y))` is the sum of
`q.comp p` applied to `y`. -/
-- First, check that `y` is small enough so that estimates for `f` and `g` apply.
have y_mem : y ∈ emetric.ball (0 : E) rf :=
(emetric.ball_subset_ball (le_trans (min_le_left _ _) (min_le_left _ _))) hy,
have fy_mem : f (x + y) ∈ emetric.ball (f x) rg,
{ apply hδ,
have : y ∈ emetric.ball (0 : E) δ :=
(emetric.ball_subset_ball (le_trans (min_le_left _ _) (min_le_right _ _))) hy,
simpa [edist_eq_coe_nnnorm_sub, edist_eq_coe_nnnorm] },
/- Now the proof starts. To show that the sum of `q.comp p` at `y` is `g (f (x + y))`, we will
write `q.comp p` applied to `y` as a big sum over all compositions. Since the sum is
summable, to get its convergence it suffices to get the convergence along some increasing sequence
of sets. We will use the sequence of sets `comp_partial_sum_target n`, along which the sum is
exactly the composition of the partial sums of `q` and `p`, by design. To show that it converges
to `g (f (x + y))`, pointwise convergence would not be enough, but we have uniform convergence
to save the day. -/
-- First step: the partial sum of `p` converges to `f (x + y)`.
have A : tendsto (λ n, ∑ a in finset.Ico 1 n, p a (λ b, y)) at_top (𝓝 (f (x + y) - f x)),
{ have L : ∀ᶠ n in at_top, ∑ a in finset.range n, p a (λ b, y) - f x =
∑ a in finset.Ico 1 n, p a (λ b, y),
{ rw eventually_at_top,
refine ⟨1, λ n hn, _⟩,
symmetry,
rw [eq_sub_iff_add_eq', finset.range_eq_Ico, ← Hf.coeff_zero (λi, y),
finset.sum_eq_sum_Ico_succ_bot hn] },
have : tendsto (λ n, ∑ a in finset.range n, p a (λ b, y) - f x) at_top (𝓝 (f (x + y) - f x)) :=
(Hf.has_sum y_mem).tendsto_sum_nat.sub tendsto_const_nhds,
exact tendsto.congr' L this },
-- Second step: the composition of the partial sums of `q` and `p` converges to `g (f (x + y))`.
have B : tendsto (λ n, q.partial_sum n (∑ a in finset.Ico 1 n, p a (λ b, y)))
at_top (𝓝 (g (f (x + y)))),
{ -- we use the fact that the partial sums of `q` converge locally uniformly to `g`, and that
-- composition passes to the limit under locally uniform convergence.
have B₁ : continuous_at (λ (z : F), g (f x + z)) (f (x + y) - f x),
{ refine continuous_at.comp _ (continuous_const.add continuous_id).continuous_at,
simp only [add_sub_cancel'_right, id.def],
exact Hg.continuous_on.continuous_at (mem_nhds_sets (emetric.is_open_ball) fy_mem) },
have B₂ : f (x + y) - f x ∈ emetric.ball (0 : F) rg,
by simpa [edist_eq_coe_nnnorm, edist_eq_coe_nnnorm_sub] using fy_mem,
rw [← nhds_within_eq_of_open B₂ emetric.is_open_ball] at A,
convert Hg.tendsto_locally_uniformly_on.tendsto_comp B₁.continuous_within_at B₂ A,
simp only [add_sub_cancel'_right] },
-- Third step: the sum over all compositions in `comp_partial_sum_target n` converges to
-- `g (f (x + y))`. As this sum is exactly the composition of the partial sum, this is a direct
-- consequence of the second step
have C : tendsto (λ n,
∑ i in comp_partial_sum_target n, q.comp_along_composition_multilinear p i.2 (λ j, y))
at_top (𝓝 (g (f (x + y)))),
by simpa [comp_partial_sum] using B,
-- Fourth step: the sum over all compositions is `g (f (x + y))`. This follows from the
-- convergence along a subsequence proved in the third step, and the fact that the sum is Cauchy
-- thanks to the summability properties.
have D : has_sum (λ i : (Σ n, composition n),
q.comp_along_composition_multilinear p i.2 (λ j, y)) (g (f (x + y))),
{ have cau : cauchy_seq (λ (s : finset (Σ n, composition n)),
∑ i in s, q.comp_along_composition_multilinear p i.2 (λ j, y)),
{ apply cauchy_seq_finset_of_norm_bounded _ (nnreal.summable_coe.2 hr) _,
simp only [coe_nnnorm, nnreal.coe_mul, nnreal.coe_pow],
rintros ⟨n, c⟩,
calc ∥(comp_along_composition q p c) (λ (j : fin n), y)∥
≤ ∥comp_along_composition q p c∥ * ∏ j : fin n, ∥y∥ :
by apply continuous_multilinear_map.le_op_norm
... ≤ ∥comp_along_composition q p c∥ * (r : ℝ) ^ n :
begin
apply mul_le_mul_of_nonneg_left _ (norm_nonneg _),
rw [finset.prod_const, finset.card_fin],
apply pow_le_pow_of_le_left (norm_nonneg _),
rw [emetric.mem_ball, edist_eq_coe_nnnorm] at hy,
have := (le_trans (le_of_lt hy) (min_le_right _ _)),
rwa [ennreal.coe_le_coe, ← nnreal.coe_le_coe, coe_nnnorm] at this
end },
exact tendsto_nhds_of_cauchy_seq_of_subseq cau
comp_partial_sum_target_tendsto_at_top C },
-- Fifth step: the sum over `n` of `q.comp p n` can be expressed as a particular resummation of
-- the sum over all compositions, by grouping together the compositions of the same
-- integer `n`. The convergence of the whole sum therefore implies the converence of the sum
-- of `q.comp p n`
have E : has_sum (λ n, (q.comp p) n (λ j, y)) (g (f (x + y))),
{ apply D.sigma,
assume n,
dsimp [formal_multilinear_series.comp],
convert has_sum_fintype _,
simp only [continuous_multilinear_map.sum_apply],
refl },
exact E
end
/-- If two functions `g` and `f` are analytic respectively at `f x` and `x`, then `g ∘ f` is
analytic at `x`. -/
theorem analytic_at.comp {g : F → G} {f : E → F} {x : E}
(hg : analytic_at 𝕜 g (f x)) (hf : analytic_at 𝕜 f x) : analytic_at 𝕜 (g ∘ f) x :=
let ⟨q, hq⟩ := hg, ⟨p, hp⟩ := hf in (hq.comp hp).analytic_at
/-!
### Associativity of the composition of formal multilinear series
In this paragraph, we us prove the associativity of the composition of formal power series.
By definition,
```
(r.comp q).comp p n v
= ∑_{i₁ + ... + iₖ = n} (r.comp q)ₖ (p_{i₁} (v₀, ..., v_{i₁ -1}), p_{i₂} (...), ..., p_{iₖ}(...))
= ∑_{a : composition n} (r.comp q) a.length (apply_composition p a v)
```
decomposing `r.comp q` in the same way, we get
```
(r.comp q).comp p n v
= ∑_{a : composition n} ∑_{b : composition a.length}
r b.length (apply_composition q b (apply_composition p a v))
```
On the other hand,
```
r.comp (q.comp p) n v = ∑_{c : composition n} r c.length (apply_composition (q.comp p) c v)
```
Here, `apply_composition (q.comp p) c v` is a vector of length `c.length`, whose `i`-th term is
given by `(q.comp p) (c.blocks_fun i) (v_l, v_{l+1}, ..., v_{m-1})` where `{l, ..., m-1}` is the
`i`-th block in the composition `c`, of length `c.blocks_fun i` by definition. To compute this term,
we expand it as `∑_{dᵢ : composition (c.blocks_fun i)} q dᵢ.length (apply_composition p dᵢ v')`,
where `v' = (v_l, v_{l+1}, ..., v_{m-1})`. Therefore, we get
```
r.comp (q.comp p) n v =
∑_{c : composition n} ∑_{d₀ : composition (c.blocks_fun 0),
..., d_{c.length - 1} : composition (c.blocks_fun (c.length - 1))}
r c.length (λ i, q dᵢ.length (apply_composition p dᵢ v'ᵢ))
```
To show that these terms coincide, we need to explain how to reindex the sums to put them in
bijection (and then the terms we are summing will correspond to each other). Suppose we have a
composition `a` of `n`, and a composition `b` of `a.length`. Then `b` indicates how to group
together some blocks of `a`, giving altogether `b.length` blocks of blocks. These blocks of blocks
can be called `d₀, ..., d_{a.length - 1}`, and one obtains a composition `c` of `n` by saying that
each `dᵢ` is one single block. Conversely, if one starts from `c` and the `dᵢ`s, one can concatenate
the `dᵢ`s to obtain a composition `a` of `n`, and register the lengths of the `dᵢ`s in a composition
`b` of `a.length`.
An example might be enlightening. Suppose `a = [2, 2, 3, 4, 2]`. It is a composition of
length 5 of 13. The content of the blocks may be represented as `0011222333344`.
Now take `b = [2, 3]` as a composition of `a.length = 5`. It says that the first 2 blocks of `a`
should be merged, and the last 3 blocks of `a` should be merged, giving a new composition of `13`
made of two blocks of length `4` and `9`, i.e., `c = [4, 9]`. But one can also remember that
the new first block was initially made of two blocks of size `2`, so `d₀ = [2, 2]`, and the new
second block was initially made of three blocks of size `3`, `4` and `2`, so `d₁ = [3, 4, 2]`.
This equivalence is called `composition.sigma_equiv_sigma_pi n` below.
We start with preliminary results on compositions, of a very specialized nature, then define the
equivalence `composition.sigma_equiv_sigma_pi n`, and we deduce finally the associativity of
composition of formal multilinear series in `formal_multilinear_series.comp_assoc`.
-/
namespace composition
variable {n : ℕ}
/-- Rewriting equality in the dependent type `Σ (a : composition n), composition a.length)` in
non-dependent terms with lists, requiring that the blocks coincide. -/
lemma sigma_composition_eq_iff (i j : Σ (a : composition n), composition a.length) :
i = j ↔ i.1.blocks = j.1.blocks ∧ i.2.blocks = j.2.blocks :=
begin
refine ⟨by rintro rfl; exact ⟨rfl, rfl⟩, _⟩,
rcases i with ⟨a, b⟩,
rcases j with ⟨a', b'⟩,
rintros ⟨h, h'⟩,
have H : a = a', by { ext1, exact h },
induction H, congr, ext1, exact h'
end
/-- Rewriting equality in the dependent type
`Σ (c : composition n), Π (i : fin c.length), composition (c.blocks_fun i)` in
non-dependent terms with lists, requiring that the lists of blocks coincide. -/
lemma sigma_pi_composition_eq_iff
(u v : Σ (c : composition n), Π (i : fin c.length), composition (c.blocks_fun i)) :
u = v ↔ of_fn (λ i, (u.2 i).blocks) = of_fn (λ i, (v.2 i).blocks) :=
begin
refine ⟨λ H, by rw H, λ H, _⟩,
rcases u with ⟨a, b⟩,
rcases v with ⟨a', b'⟩,
dsimp at H,
have h : a = a',
{ ext1,
have : map list.sum (of_fn (λ (i : fin (composition.length a)), (b i).blocks)) =
map list.sum (of_fn (λ (i : fin (composition.length a')), (b' i).blocks)), by rw H,
simp only [map_of_fn] at this,
change of_fn (λ (i : fin (composition.length a)), (b i).blocks.sum) =
of_fn (λ (i : fin (composition.length a')), (b' i).blocks.sum) at this,
simpa [composition.blocks_sum, composition.of_fn_blocks_fun] using this },
induction h,
simp only [true_and, eq_self_iff_true, heq_iff_eq],
ext i : 2,
have : nth_le (of_fn (λ (i : fin (composition.length a)), (b i).blocks)) i (by simp [i.is_lt]) =
nth_le (of_fn (λ (i : fin (composition.length a)), (b' i).blocks)) i (by simp [i.is_lt]) :=
nth_le_of_eq H _,
rwa [nth_le_of_fn, nth_le_of_fn] at this
end
/-- When `a` is a composition of `n` and `b` is a composition of `a.length`, `a.gather b` is the
composition of `n` obtained by gathering all the blocks of `a` corresponding to a block of `b`.
For instance, if `a = [6, 5, 3, 5, 2]` and `b = [2, 3]`, one should gather together
the first two blocks of `a` and its last three blocks, giving `a.gather b = [11, 10]`. -/
def gather (a : composition n) (b : composition a.length) : composition n :=
{ blocks := (a.blocks.split_wrt_composition b).map sum,
blocks_pos :=
begin
rw forall_mem_map_iff,
intros j hj,
suffices H : ∀ i ∈ j, 1 ≤ i, from
calc 0 < j.length : length_pos_of_mem_split_wrt_composition hj
... ≤ j.sum : length_le_sum_of_one_le _ H,
intros i hi,
apply a.one_le_blocks,
rw ← a.blocks.join_split_wrt_composition b,
exact mem_join_of_mem hj hi,
end,
blocks_sum := by { rw [← sum_join, join_split_wrt_composition, a.blocks_sum] } }
lemma length_gather (a : composition n) (b : composition a.length) :
length (a.gather b) = b.length :=
show (map list.sum (a.blocks.split_wrt_composition b)).length = b.blocks.length,
by rw [length_map, length_split_wrt_composition]
/-- An auxiliary function used in the definition of `sigma_equiv_sigma_pi` below, associating to
two compositions `a` of `n` and `b` of `a.length`, and an index `i` bounded by the length of
`a.gather b`, the subcomposition of `a` made of those blocks belonging to the `i`-th block of
`a.gather b`. -/
def sigma_composition_aux (a : composition n) (b : composition a.length)
(i : fin (a.gather b).length) :
composition ((a.gather b).blocks_fun i) :=
{ blocks := nth_le (a.blocks.split_wrt_composition b) i
(by { rw [length_split_wrt_composition, ← length_gather], exact i.2 }),
blocks_pos := assume i hi, a.blocks_pos
(by { rw ← a.blocks.join_split_wrt_composition b, exact mem_join_of_mem (nth_le_mem _ _ _) hi }),
blocks_sum := by simp only [composition.blocks_fun, nth_le_map', composition.gather, fin.val_eq_coe] }
/- Where did the fin.val come from in the proof on the preceding line? -/
lemma length_sigma_composition_aux (a : composition n) (b : composition a.length) (i : fin b.length) :
composition.length (composition.sigma_composition_aux a b ⟨i, (length_gather a b).symm ▸ i.2⟩) =
composition.blocks_fun b i :=
show list.length (nth_le (split_wrt_composition a.blocks b) i _) = blocks_fun b i,
by { rw [nth_le_map_rev list.length, nth_le_of_eq (map_length_split_wrt_composition _ _)], refl }
lemma blocks_fun_sigma_composition_aux (a : composition n) (b : composition a.length)
(i : fin b.length) (j : fin (blocks_fun b i)) :
blocks_fun (sigma_composition_aux a b ⟨i, (length_gather a b).symm ▸ i.2⟩)
⟨j, (length_sigma_composition_aux a b i).symm ▸ j.2⟩ = blocks_fun a (embedding b i j) :=
show nth_le (nth_le _ _ _) _ _ = nth_le a.blocks _ _,
by { rw [nth_le_of_eq (nth_le_split_wrt_composition _ _ _), nth_le_drop', nth_le_take'], refl }
/-- Auxiliary lemma to prove that the composition of formal multilinear series is associative.
Consider a composition `a` of `n` and a composition `b` of `a.length`. Grouping together some
blocks of `a` according to `b` as in `a.gather b`, one can compute the total size of the blocks
of `a` up to an index `size_up_to b i + j` (where the `j` corresponds to a set of blocks of `a`
that do not fill a whole block of `a.gather b`). The first part corresponds to a sum of blocks
in `a.gather b`, and the second one to a sum of blocks in the next block of
`sigma_composition_aux a b`. This is the content of this lemma. -/
lemma size_up_to_size_up_to_add (a : composition n) (b : composition a.length)
{i j : ℕ} (hi : i < b.length) (hj : j < blocks_fun b ⟨i, hi⟩) :
size_up_to a (size_up_to b i + j) = size_up_to (a.gather b) i +
(size_up_to (sigma_composition_aux a b ⟨i, (length_gather a b).symm ▸ hi⟩) j) :=
begin
induction j with j IHj,
{ show sum (take ((b.blocks.take i).sum) a.blocks) =
sum (take i (map sum (split_wrt_composition a.blocks b))),
induction i with i IH,
{ refl },
{ have A : i < b.length := nat.lt_of_succ_lt hi,
have B : i < list.length (map list.sum (split_wrt_composition a.blocks b)), by simp [A],
have C : 0 < blocks_fun b ⟨i, A⟩ := composition.blocks_pos' _ _ _,
rw [sum_take_succ _ _ B, ← IH A C],
have : take (sum (take i b.blocks)) a.blocks =
take (sum (take i b.blocks)) (take (sum (take (i+1) b.blocks)) a.blocks),
{ rw [take_take, min_eq_left],
apply monotone_sum_take _ (nat.le_succ _) },
rw [this, nth_le_map', nth_le_split_wrt_composition,
← take_append_drop (sum (take i b.blocks)) ((take (sum (take (nat.succ i) b.blocks)) a.blocks)),
sum_append],
congr,
rw [take_append_drop] } },
{ have A : j < blocks_fun b ⟨i, hi⟩ := lt_trans (lt_add_one j) hj,
have B : j < length (sigma_composition_aux a b ⟨i, (length_gather a b).symm ▸ hi⟩),
by { convert A, rw [← length_sigma_composition_aux], refl },
have C : size_up_to b i + j < size_up_to b (i + 1),
{ simp only [size_up_to_succ b hi, add_lt_add_iff_left],
exact A },
have D : size_up_to b i + j < length a := lt_of_lt_of_le C (b.size_up_to_le _),
have : size_up_to b i + nat.succ j = (size_up_to b i + j).succ := rfl,
rw [this, size_up_to_succ _ D, IHj A, size_up_to_succ _ B],
simp only [sigma_composition_aux, add_assoc, add_left_inj, fin.coe_mk],
rw [nth_le_of_eq (nth_le_split_wrt_composition _ _ _), nth_le_drop', nth_le_take _ _ C] }
end
/--
Natural equivalence between `(Σ (a : composition n), composition a.length)` and
`(Σ (c : composition n), Π (i : fin c.length), composition (c.blocks_fun i))`, that shows up as a
change of variables in the proof that composition of formal multilinear series is associative.
Consider a composition `a` of `n` and a composition `b` of `a.length`. Then `b` indicates how to
group together some blocks of `a`, giving altogether `b.length` blocks of blocks. These blocks of
blocks can be called `d₀, ..., d_{a.length - 1}`, and one obtains a composition `c` of `n` by
saying that each `dᵢ` is one single block. The map `⟨a, b⟩ → ⟨c, (d₀, ..., d_{a.length - 1})⟩` is
the direct map in the equiv.
Conversely, if one starts from `c` and the `dᵢ`s, one can join the `dᵢ`s to obtain a composition
`a` of `n`, and register the lengths of the `dᵢ`s in a composition `b` of `a.length`. This is the
inverse map of the equiv.
-/
def sigma_equiv_sigma_pi (n : ℕ) :
(Σ (a : composition n), composition a.length) ≃
(Σ (c : composition n), Π (i : fin c.length), composition (c.blocks_fun i)) :=
{ to_fun := λ i, ⟨i.1.gather i.2, i.1.sigma_composition_aux i.2⟩,
inv_fun := λ i, ⟨
{ blocks := (of_fn (λ j, (i.2 j).blocks)).join,
blocks_pos :=
begin
simp only [and_imp, mem_join, exists_imp_distrib, forall_mem_of_fn_iff],
exact λ i j hj, composition.blocks_pos _ hj
end,
blocks_sum := by simp [sum_of_fn, composition.blocks_sum, composition.sum_blocks_fun] },
{ blocks := of_fn (λ j, (i.2 j).length),
blocks_pos := forall_mem_of_fn_iff.2
(λ j, composition.length_pos_of_pos _ (composition.blocks_pos' _ _ _)),
blocks_sum := by { dsimp only [composition.length], simp [sum_of_fn] } }⟩,
left_inv :=
begin
-- the fact that we have a left inverse is essentially `join_split_wrt_composition`,
-- but we need to massage it to take care of the dependent setting.
rintros ⟨a, b⟩,
rw sigma_composition_eq_iff,
dsimp,
split,
{ have A := length_map list.sum (split_wrt_composition a.blocks b),
conv_rhs { rw [← join_split_wrt_composition a.blocks b,
← of_fn_nth_le (split_wrt_composition a.blocks b)] },
congr,
{ exact A },
{ exact (fin.heq_fun_iff A).2 (λ i, rfl) } },
{ have B : composition.length (composition.gather a b) = list.length b.blocks :=
composition.length_gather _ _,
conv_rhs { rw [← of_fn_nth_le b.blocks] },
congr' 1,
{ exact B },
{ apply (fin.heq_fun_iff B).2 (λ i, _),
rw [sigma_composition_aux, composition.length, nth_le_map_rev list.length,
nth_le_of_eq (map_length_split_wrt_composition _ _)], refl } }
end,
right_inv :=
begin
-- the fact that we have a right inverse is essentially `split_wrt_composition_join`,
-- but we need to massage it to take care of the dependent setting.
rintros ⟨c, d⟩,
have : map list.sum (of_fn (λ (i : fin (composition.length c)), (d i).blocks)) = c.blocks,
by simp [map_of_fn, (∘), composition.blocks_sum, composition.of_fn_blocks_fun],
rw sigma_pi_composition_eq_iff,
dsimp,
congr,
{ ext1,
dsimp [composition.gather],
rwa split_wrt_composition_join,
simp only [map_of_fn] },
{ rw fin.heq_fun_iff,
{ assume i,
dsimp [composition.sigma_composition_aux],
rw [nth_le_of_eq (split_wrt_composition_join _ _ _)],
{ simp only [nth_le_of_fn'] },
{ simp only [map_of_fn] } },
{ congr,
ext1,
dsimp [composition.gather],
rwa split_wrt_composition_join,
simp only [map_of_fn] } }
end }
end composition
namespace formal_multilinear_series
open composition
theorem comp_assoc (r : formal_multilinear_series 𝕜 G H) (q : formal_multilinear_series 𝕜 F G)
(p : formal_multilinear_series 𝕜 E F) :
(r.comp q).comp p = r.comp (q.comp p) :=
begin
ext n v,
/- First, rewrite the two compositions appearing in the theorem as two sums over complicated
sigma types, as in the description of the proof above. -/
let f : (Σ (a : composition n), composition a.length) → H :=
λ ⟨a, b⟩, r b.length (apply_composition q b (apply_composition p a v)),
let g : (Σ (c : composition n), Π (i : fin c.length), composition (c.blocks_fun i)) → H :=
λ ⟨c, d⟩, r c.length
(λ (i : fin c.length), q (d i).length (apply_composition p (d i) (v ∘ c.embedding i))),
suffices A : ∑ c, f c = ∑ c, g c,
{ dsimp [formal_multilinear_series.comp],
simp only [continuous_multilinear_map.sum_apply, comp_along_composition_apply],
rw ← @finset.sum_sigma _ _ _ _ (finset.univ : finset (composition n)) _ f,
dsimp [apply_composition],
simp only [continuous_multilinear_map.sum_apply, comp_along_composition_apply,
continuous_multilinear_map.map_sum],
rw ← @finset.sum_sigma _ _ _ _ (finset.univ : finset (composition n)) _ g,
exact A },
/- Now, we use `composition.sigma_equiv_sigma_pi n` to change
variables in the second sum, and check that we get exactly the same sums. -/
rw ← (sigma_equiv_sigma_pi n).sum_comp,
/- To check that we have the same terms, we should check that we apply the same component of
`r`, and the same component of `q`, and the same component of `p`, to the same coordinate of
`v`. This is true by definition, but at each step one needs to convince Lean that the types
one considers are the same, using a suitable congruence lemma to avoid dependent type issues.
This dance has to be done three times, one for `r`, one for `q` and one for `p`.-/
apply finset.sum_congr rfl,
rintros ⟨a, b⟩ _,
dsimp [f, g, sigma_equiv_sigma_pi],
-- check that the `r` components are the same. Based on `composition.length_gather`
apply r.congr (composition.length_gather a b).symm,
intros i hi1 hi2,
-- check that the `q` components are the same. Based on `length_sigma_composition_aux`
apply q.congr (length_sigma_composition_aux a b _).symm,
intros j hj1 hj2,
-- check that the `p` components are the same. Based on `blocks_fun_sigma_composition_aux`
apply p.congr (blocks_fun_sigma_composition_aux a b _ _).symm,
intros k hk1 hk2,
-- finally, check that the coordinates of `v` one is using are the same. Based on
-- `size_up_to_size_up_to_add`.
refine congr_arg v (fin.eq_of_veq _),
dsimp [composition.embedding],
rw [size_up_to_size_up_to_add _ _ hi1 hj1, add_assoc],
end
end formal_multilinear_series
|
09f0f4e416ec7a30b53f93f96f3efb5c4efe10d6 | 5412d79aa1dc0b521605c38bef9f0d4557b5a29d | /stage0/src/Lean/Meta/Reduce.lean | e80518b6183afbb4fafdf25716a9d5ebb856a54d | [
"Apache-2.0"
] | permissive | smunix/lean4 | a450ec0927dc1c74816a1bf2818bf8600c9fc9bf | 3407202436c141e3243eafbecb4b8720599b970a | refs/heads/master | 1,676,334,875,188 | 1,610,128,510,000 | 1,610,128,521,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,260 | lean | /-
Copyright (c) 2019 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Meta.Basic
import Lean.Meta.FunInfo
namespace Lean.Meta
partial def reduce (e : Expr) (explicitOnly skipTypes skipProofs := true) : MetaM Expr :=
let rec visit (e : Expr) := do
if (← (pure skipTypes <&&> isType e)) then
pure e
else if (← (pure skipProofs <&&> isProof e)) then
pure e
else
let e ← whnf e
match e with
| Expr.app .. =>
let f := e.getAppFn
let nargs := e.getAppNumArgs
let finfo ← getFunInfoNArgs f nargs
let mut args := e.getAppArgs
for i in [:args.size] do
if i < finfo.paramInfo.size then
let info := finfo.paramInfo[i]
if !explicitOnly || info.isExplicit then
args ← args.modifyM i visit
else
args ← args.modifyM i visit
pure (mkAppN f args)
| Expr.lam .. => lambdaTelescope e fun xs b => do mkLambdaFVars xs (← visit b)
| Expr.forallE .. => forallTelescope e fun xs b => do mkForallFVars xs (← visit b)
| _ => pure e
visit e
end Lean.Meta
|
fba220329318c2eefccbfbf3339f76f26899954b | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/category_theory/limits/constructions/equalizers.lean | fb51d341ed8106d8628610f89f14888ff7d5f848 | [
"Apache-2.0"
] | permissive | alreadydone/mathlib | dc0be621c6c8208c581f5170a8216c5ba6721927 | c982179ec21091d3e102d8a5d9f5fe06c8fafb73 | refs/heads/master | 1,685,523,275,196 | 1,670,184,141,000 | 1,670,184,141,000 | 287,574,545 | 0 | 0 | Apache-2.0 | 1,670,290,714,000 | 1,597,421,623,000 | Lean | UTF-8 | Lean | false | false | 9,204 | lean | /-
Copyright (c) 2020 Bhavik Mehta. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Bhavik Mehta, Andrew Yang
-/
import category_theory.limits.shapes.equalizers
import category_theory.limits.shapes.binary_products
import category_theory.limits.shapes.pullbacks
import category_theory.limits.preserves.shapes.pullbacks
import category_theory.limits.preserves.shapes.binary_products
/-!
# Constructing equalizers from pullbacks and binary products.
If a category has pullbacks and binary products, then it has equalizers.
TODO: generalize universe
-/
noncomputable theory
universes v v' u u'
open category_theory category_theory.category
namespace category_theory.limits
variables {C : Type u} [category.{v} C]
variables {D : Type u'} [category.{v'} D] (G : C ⥤ D)
-- We hide the "implementation details" inside a namespace
namespace has_equalizers_of_has_pullbacks_and_binary_products
variables [has_binary_products C] [has_pullbacks C]
/-- Define the equalizing object -/
@[reducible]
def construct_equalizer (F : walking_parallel_pair ⥤ C) : C :=
pullback (prod.lift (𝟙 _) (F.map walking_parallel_pair_hom.left))
(prod.lift (𝟙 _) (F.map walking_parallel_pair_hom.right))
/-- Define the equalizing morphism -/
abbreviation pullback_fst (F : walking_parallel_pair ⥤ C) :
construct_equalizer F ⟶ F.obj walking_parallel_pair.zero :=
pullback.fst
lemma pullback_fst_eq_pullback_snd (F : walking_parallel_pair ⥤ C) :
pullback_fst F = pullback.snd :=
by convert pullback.condition =≫ limits.prod.fst; simp
/-- Define the equalizing cone -/
@[reducible]
def equalizer_cone (F : walking_parallel_pair ⥤ C) : cone F :=
cone.of_fork
(fork.of_ι (pullback_fst F)
(begin
conv_rhs { rw pullback_fst_eq_pullback_snd, },
convert pullback.condition =≫ limits.prod.snd using 1; simp
end))
/-- Show the equalizing cone is a limit -/
def equalizer_cone_is_limit (F : walking_parallel_pair ⥤ C) : is_limit (equalizer_cone F) :=
{ lift :=
begin
intro c, apply pullback.lift (c.π.app _) (c.π.app _),
apply limit.hom_ext,
rintro (_ | _); simp
end,
fac' := by rintros c (_ | _); simp,
uniq' :=
begin
intros c _ J,
have J0 := J walking_parallel_pair.zero, simp at J0,
apply pullback.hom_ext,
{ rwa limit.lift_π },
{ erw [limit.lift_π, ← J0, pullback_fst_eq_pullback_snd] }
end }
end has_equalizers_of_has_pullbacks_and_binary_products
open has_equalizers_of_has_pullbacks_and_binary_products
/-- Any category with pullbacks and binary products, has equalizers. -/
-- This is not an instance, as it is not always how one wants to construct equalizers!
lemma has_equalizers_of_has_pullbacks_and_binary_products
[has_binary_products C] [has_pullbacks C] :
has_equalizers C :=
{ has_limit := λ F, has_limit.mk
{ cone := equalizer_cone F,
is_limit := equalizer_cone_is_limit F } }
local attribute[instance] has_pullback_of_preserves_pullback
/-- A functor that preserves pullbacks and binary products also presrves equalizers. -/
def preserves_equalizers_of_preserves_pullbacks_and_binary_products
[has_binary_products C] [has_pullbacks C]
[preserves_limits_of_shape (discrete walking_pair) G]
[preserves_limits_of_shape walking_cospan G] :
preserves_limits_of_shape walking_parallel_pair G :=
⟨λ K, preserves_limit_of_preserves_limit_cone (equalizer_cone_is_limit K) $
{ lift := λ c, begin
refine pullback.lift _ _ _ ≫ (@@preserves_pullback.iso _ _ _ _ _ _ _ _).inv,
{ exact c.π.app walking_parallel_pair.zero },
{ exact c.π.app walking_parallel_pair.zero },
apply (map_is_limit_of_preserves_of_is_limit G _ _ (prod_is_prod _ _)).hom_ext,
swap, apply_instance,
rintro (_|_),
{ simp only [category.assoc, ← G.map_comp, prod.lift_fst,
binary_fan.π_app_left, binary_fan.mk_fst], },
{ simp only [binary_fan.π_app_right, binary_fan.mk_snd,
category.assoc, ← G.map_comp, prod.lift_snd],
exact (c.π.naturality (walking_parallel_pair_hom.left)).symm.trans
(c.π.naturality (walking_parallel_pair_hom.right)) },
end,
fac' := λ c j, begin
rcases j with (_|_);
simp only [category.comp_id, preserves_pullback.iso_inv_fst, cone.of_fork_π, G.map_comp,
preserves_pullback.iso_inv_fst_assoc, functor.map_cone_π_app, eq_to_hom_refl,
category.assoc, fork.of_ι_π_app, pullback.lift_fst, pullback.lift_fst_assoc],
exact (c.π.naturality (walking_parallel_pair_hom.left)).symm.trans (category.id_comp _)
end,
uniq' := λ s m h, begin
rw iso.eq_comp_inv,
have := h walking_parallel_pair.zero,
dsimp [equalizer_cone] at this,
ext; simp only [preserves_pullback.iso_hom_snd,
category.assoc, preserves_pullback.iso_hom_fst, pullback.lift_fst, pullback.lift_snd,
category.comp_id, ← pullback_fst_eq_pullback_snd, ← this],
end }⟩
-- We hide the "implementation details" inside a namespace
namespace has_coequalizers_of_has_pushouts_and_binary_coproducts
variables [has_binary_coproducts C] [has_pushouts C]
/-- Define the equalizing object -/
@[reducible]
def construct_coequalizer (F : walking_parallel_pair ⥤ C) : C :=
pushout (coprod.desc (𝟙 _) (F.map walking_parallel_pair_hom.left))
(coprod.desc (𝟙 _) (F.map walking_parallel_pair_hom.right))
/-- Define the equalizing morphism -/
abbreviation pushout_inl (F : walking_parallel_pair ⥤ C) :
F.obj walking_parallel_pair.one ⟶ construct_coequalizer F :=
pushout.inl
lemma pushout_inl_eq_pushout_inr (F : walking_parallel_pair ⥤ C) :
pushout_inl F = pushout.inr :=
by convert limits.coprod.inl ≫= pushout.condition; simp
/-- Define the equalizing cocone -/
@[reducible]
def coequalizer_cocone (F : walking_parallel_pair ⥤ C) : cocone F :=
cocone.of_cofork
(cofork.of_π (pushout_inl F)
(begin
conv_rhs { rw pushout_inl_eq_pushout_inr, },
convert limits.coprod.inr ≫= pushout.condition using 1; simp
end))
/-- Show the equalizing cocone is a colimit -/
def coequalizer_cocone_is_colimit (F : walking_parallel_pair ⥤ C) :
is_colimit (coequalizer_cocone F) :=
{ desc :=
begin
intro c, apply pushout.desc (c.ι.app _) (c.ι.app _),
apply colimit.hom_ext,
rintro (_ | _); simp
end,
fac' := by rintros c (_ | _); simp,
uniq' :=
begin
intros c _ J,
have J1 : pushout_inl F ≫ m = c.ι.app walking_parallel_pair.one :=
by simpa using J walking_parallel_pair.one,
apply pushout.hom_ext,
{ rw colimit.ι_desc, exact J1 },
{ rw [colimit.ι_desc, ← pushout_inl_eq_pushout_inr], exact J1 }
end }
end has_coequalizers_of_has_pushouts_and_binary_coproducts
open has_coequalizers_of_has_pushouts_and_binary_coproducts
/-- Any category with pullbacks and binary products, has equalizers. -/
-- This is not an instance, as it is not always how one wants to construct equalizers!
lemma has_coequalizers_of_has_pushouts_and_binary_coproducts
[has_binary_coproducts C] [has_pushouts C] : has_coequalizers C :=
{ has_colimit := λ F, has_colimit.mk
{ cocone := coequalizer_cocone F,
is_colimit := coequalizer_cocone_is_colimit F } }
local attribute[instance] has_pushout_of_preserves_pushout
/-- A functor that preserves pushouts and binary coproducts also presrves coequalizers. -/
def preserves_coequalizers_of_preserves_pushouts_and_binary_coproducts
[has_binary_coproducts C] [has_pushouts C]
[preserves_colimits_of_shape (discrete walking_pair) G]
[preserves_colimits_of_shape walking_span G] :
preserves_colimits_of_shape walking_parallel_pair G :=
⟨λ K, preserves_colimit_of_preserves_colimit_cocone (coequalizer_cocone_is_colimit K) $
{ desc := λ c, begin
refine (@@preserves_pushout.iso _ _ _ _ _ _ _ _).inv ≫ pushout.desc _ _ _,
{ exact c.ι.app walking_parallel_pair.one },
{ exact c.ι.app walking_parallel_pair.one },
apply (map_is_colimit_of_preserves_of_is_colimit G _ _ (coprod_is_coprod _ _)).hom_ext,
swap, apply_instance,
rintro (_|_),
{ simp only [binary_cofan.ι_app_left, binary_cofan.mk_inl, category.assoc, ← G.map_comp_assoc,
coprod.inl_desc] },
{ simp only [binary_cofan.ι_app_right, binary_cofan.mk_inr, category.assoc, ← G.map_comp_assoc,
coprod.inr_desc],
exact (c.ι.naturality walking_parallel_pair_hom.left).trans
(c.ι.naturality walking_parallel_pair_hom.right).symm, },
end,
fac' := λ c j, begin
rcases j with (_|_); simp only [functor.map_cocone_ι_app, cocone.of_cofork_ι, category.id_comp,
eq_to_hom_refl, category.assoc, functor.map_comp, cofork.of_π_ι_app, pushout.inl_desc,
preserves_pushout.inl_iso_inv_assoc],
exact (c.ι.naturality (walking_parallel_pair_hom.left)).trans (category.comp_id _)
end,
uniq' := λ s m h, begin
rw iso.eq_inv_comp,
have := h walking_parallel_pair.one,
dsimp [coequalizer_cocone] at this,
ext; simp only [preserves_pushout.inl_iso_hom_assoc, category.id_comp, pushout.inl_desc,
pushout.inr_desc, preserves_pushout.inr_iso_hom_assoc,
← pushout_inl_eq_pushout_inr, ← this],
end }⟩
end category_theory.limits
|
3d0bb470fffd733dc0e6c06e340da147f6df0fee | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /src/Init/Data/Nat/Bitwise.lean | eecafaeb07ff5bef465ac8d2c0ab85d0003e920f | [
"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 | 1,486 | lean | /-
Copyright (c) 2019 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
prelude
import Init.Data.Nat.Basic
import Init.Data.Nat.Div
import Init.Coe
namespace Nat
theorem bitwise_rec_lemma {n : Nat} (hNe : n ≠ 0) : n / 2 < n :=
Nat.div_lt_self (Nat.zero_lt_of_ne_zero hNe) (Nat.lt_succ_self _)
def bitwise (f : Bool → Bool → Bool) (n m : Nat) : Nat :=
if n = 0 then
if f false true then m else 0
else if m = 0 then
if f true false then n else 0
else
let n' := n / 2
let m' := m / 2
let b₁ := n % 2 = 1
let b₂ := m % 2 = 1
let r := bitwise f n' m'
if f b₁ b₂ then
r+r+1
else
r+r
decreasing_by apply bitwise_rec_lemma; assumption
@[extern "lean_nat_land"]
def land : @& Nat → @& Nat → Nat := bitwise and
@[extern "lean_nat_lor"]
def lor : @& Nat → @& Nat → Nat := bitwise or
@[extern "lean_nat_lxor"]
def xor : @& Nat → @& Nat → Nat := bitwise bne
@[extern "lean_nat_shiftl"]
def shiftLeft : @& Nat → @& Nat → Nat
| n, 0 => n
| n, succ m => shiftLeft (2*n) m
@[extern "lean_nat_shiftr"]
def shiftRight : @& Nat → @& Nat → Nat
| n, 0 => n
| n, succ m => shiftRight n m / 2
instance : AndOp Nat := ⟨Nat.land⟩
instance : OrOp Nat := ⟨Nat.lor⟩
instance : Xor Nat := ⟨Nat.xor⟩
instance : ShiftLeft Nat := ⟨Nat.shiftLeft⟩
instance : ShiftRight Nat := ⟨Nat.shiftRight⟩
end Nat
|
7ba57f7bfa0a3b135f7795830f17282c79e59778 | be5348f86d661459153802318209304b793c0e2a | /src/shrink.lean | 9d2a20879f2c5b7ad71db88074e6bbdee5422f2f | [] | no_license | reglayass/lean-avl | 6b758c7708bdb3316b1b97ada3e3f259b49da58a | c7bffa75d7548e5ff8cdd7d69f5a58499f883df1 | refs/heads/master | 1,692,297,536,477 | 1,633,946,864,000 | 1,633,946,864,000 | 340,881,572 | 4 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 7,889 | lean | import definitions rotations forall_keys tactic.linarith tactic.induction
set_option pp.generalized_field_notation false
universe u
namespace shrink_lemmas
open btree rotation_lemmas forall_keys_lemmas
variables {α : Type u}
lemma shrink_shrink_view (t : btree α) :
shrink_view t (shrink t) :=
begin
cases t,
case empty {
exact shrink_view.empty,
},
case node : l k a r {
dsimp [shrink],
cases h : shrink r,
case none {
dsimp only [shrink._match_1],
apply shrink_view.nonempty_empty; assumption,
},
case some {
rcases val with ⟨x, a', sh⟩,
dsimp only [shrink._match_1],
by_cases h' : (height l > height sh + 1),
{ simp only [if_pos h'],
apply shrink_view.nonempty_nonempty₁, try { assumption },
assumption, simp,
},
{ simp only [if_neg h'],
apply shrink_view.nonempty_nonempty₂, try { assumption },
linarith,
},
},
},
end
/- All the keys in a tree after shrinking are all keys that came from the original tree -/
lemma shrink_keys {t sh : btree α} {x : nat} {a : α} :
ordered t ∧ shrink t = some (x, a, sh) →
bound x t = tt ∧ (∀ k', bound k' sh = tt → bound k' t = tt) :=
begin
intro h₁,
cases_matching* (_ ∧ _),
induction t generalizing x a sh,
case empty { contradiction, },
case node : l k v r ihl ihr {
rw ordered at h₁_left,
cases_matching* (_ ∧ _),
cases' shrink_shrink_view (node l k v r),
case nonempty_empty {
cases' h₁_right,
split,
{ simp [bound], },
{ intros k' h₂,
simp [bound],
tauto,
},
},
case nonempty_nonempty₁ {
rw h_2 at h₁_right,
clear h_2,
cases' h₁_right,
specialize ihr h₁_left_right_left h,
split,
{ cases_matching* (_ ∧ _),
simp [bound],
tauto,
},
{ intros k' h₂,
rw ← rotate_right_keys at h₂,
simp [bound] at h₂ ⊢,
tauto,
},
},
case nonempty_nonempty₂ {
cases' h₁_right,
specialize ihr h₁_left_right_left h,
split,
{ cases_matching* (_ ∧ _),
simp [bound],
tauto,
},
{ intros k' h₂,
simp [bound] at h₂ ⊢,
tauto,
},
},
},
end
/- Two auxiliary lemmas for easy usage -/
lemma shrink_keys_aux_1 {t sh : btree α} {x : nat} {a : α} :
ordered t ∧ shrink t = some (x, a, sh) → bound x t :=
begin
intro h₁,
apply and.left (shrink_keys h₁),
end
lemma shrink_keys_aux_2 {t sh : btree α} {x : nat} {a : α} :
ordered t ∧ shrink t = some (x, a, sh) → (∀ k', bound k' sh → bound k' t) :=
begin
intro h₁,
apply and.right (shrink_keys h₁),
end
/-
After shrinking, if k has a certain relation with a tree, then
that is preserved with the shrunken tree
-/
lemma forall_shrink {t sh : btree α} {k x : nat} {a : α} {p : nat → nat → Prop} :
forall_keys p k t ∧ shrink t = some (x, a, sh) → forall_keys p k sh ∧ p k x :=
begin
intro h₁,
induction t generalizing x a sh,
case empty {
cases_matching* (_ ∧ _),
contradiction,
},
case node : l k v r ihl ihr {
cases_matching* (_ ∧ _),
cases' shrink_shrink_view (node l k v r),
case nonempty_empty {
cases' h₁_right,
rw ← forall_keys_char at h₁_left,
cases_matching* (_ ∧ _),
split; assumption,
},
case nonempty_nonempty₁ {
rw h_2 at h₁_right,
clear h_2,
cases' h₁_right,
rw ← forall_keys_char at h₁_left,
cases_matching* (_ ∧ _),
split,
{ apply forall_rotate_right,
rw ← forall_keys_char,
repeat { split }; try { assumption },
{ specialize ihr ⟨ h₁_left_right_right, h ⟩,
apply and.left ihr,
},
},
{ specialize ihr ⟨ h₁_left_right_right, h ⟩,
apply and.right ihr,
},
},
case nonempty_nonempty₂ {
cases' h₁_right,
rw ← forall_keys_char at h₁_left,
cases_matching* (_ ∧ _),
split,
{ rw ← forall_keys_char,
repeat { split }; try { assumption },
{ specialize ihr ⟨ h₁_left_right_right, h ⟩,
apply and.left ihr,
},
},
{ specialize ihr ⟨ h₁_left_right_right, h ⟩,
apply and.right ihr,
},
},
},
end
/- Auxiliary lemmas for easy usage -/
lemma forall_shrink_aux_1 {t sh : btree α} {k x : nat} {a : α} {p : nat → nat → Prop} :
forall_keys p k t ∧ shrink t = some (x, a, sh) → forall_keys p k sh :=
begin
intro h₁,
apply and.left (forall_shrink h₁),
end
lemma forall_shrink_aux_2 {t sh : btree α} {k x : nat} {a : α} {p : nat → nat → Prop} :
forall_keys p k t ∧ shrink t = some (x, a, sh) → p k x :=
begin
intro h₁,
apply and.right (forall_shrink h₁),
end
/-
Shrink preserves order: if a tree is ordered, the resulting of shrinking the tree is also ordered, with
x being larger than all the keys in the shrunken tree
-/
lemma shrink_ordered {t sh : btree α} {x : nat} {a : α} :
ordered t ∧ shrink t = some (x, a, sh) → ordered sh ∧ forall_keys gt x sh :=
begin
intro h₁,
induction t generalizing x a sh,
case empty {
cases_matching* (_ ∧ _),
contradiction,
},
case node : l k v r ihl ihr {
rw ordered at h₁,
cases_matching* (_ ∧ _),
cases' shrink_shrink_view (node l k v r),
case nonempty_empty {
cases' h₁_right,
split; assumption,
},
case nonempty_nonempty₁ {
rw h_2 at h₁_right,
clear h_2,
cases' h₁_right,
specialize ihr ⟨h₁_left_right_left, h⟩,
cases_matching* (_ ∧ _),
split,
{ apply rotate_right_ordered,
repeat { split }; try { assumption },
{ apply forall_shrink_aux_1 (and.intro h₁_left_right_right_right h), }
},
{ apply forall_rotate_right,
have h₂ : x_1 > k,
{ have h₃ : bound x_1 r ∧ (∀ k', bound k' sh_1 → bound k' r),
{ apply shrink_keys (and.intro h₁_left_right_left h), },
cases_matching* (_ ∧ _),
unfold forall_keys at h₁_left_right_right_right,
apply h₁_left_right_right_right,
assumption,
},
have h₃ : forall_keys gt x_1 l,
{ apply forall_keys_trans l (>) k; try { assumption },
{ apply trans, },
},
rw ← forall_keys_char,
repeat { split }; assumption,
},
},
case nonempty_nonempty₂ {
cases' h₁_right,
specialize ihr ⟨h₁_left_right_left, h⟩,
cases_matching* (_ ∧ _),
split,
{ rw ordered,
repeat { split }; try { assumption },
{ apply forall_shrink_aux_1 (and.intro h₁_left_right_right_right h), },
},
{ have h₂ : x_1 > k,
{ have h₃ : bound x_1 r ∧ (∀ k', bound k' sh_1 → bound k' r),
{ apply shrink_keys (and.intro h₁_left_right_left h), },
cases_matching* (_ ∧ _),
unfold forall_keys at h₁_left_right_right_right,
apply h₁_left_right_right_right,
assumption,
},
have h₃ : forall_keys gt x_1 l,
{ apply forall_keys_trans l (>) k; try { assumption },
{ apply trans, },
},
rw ← forall_keys_char,
repeat { split }; assumption,
},
},
},
end
/- Auxiliary lemmas for easy usage -/
lemma shrink_ordered_aux_1 {t sh : btree α} {x : nat} {a : α} :
ordered t ∧ shrink t = some (x, a, sh) → ordered sh :=
begin
intro h₁,
apply and.left (shrink_ordered h₁),
end
lemma shrink_ordered_aux_2 {t sh : btree α} {x : nat} {a : α} :
ordered t ∧ shrink t = some (x, a, sh) → forall_keys (>) x sh :=
begin
intro h₁,
apply and.right (shrink_ordered h₁),
end
end shrink_lemmas |
2ed2e773dd5e2238d238c552abf58b92ae80722c | 36c7a18fd72e5b57229bd8ba36493daf536a19ce | /library/data/nat/power.lean | 0526d5d6c66cb6c35914d4450224515032e9658e | [
"Apache-2.0"
] | permissive | YHVHvx/lean | 732bf0fb7a298cd7fe0f15d82f8e248c11db49e9 | 038369533e0136dd395dc252084d3c1853accbf2 | refs/heads/master | 1,610,701,080,210 | 1,449,128,595,000 | 1,449,128,595,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 4,662 | lean | /-
Copyright (c) 2015 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura, Jeremy Avigad
The power function on the natural numbers.
-/
import data.nat.basic data.nat.order data.nat.div data.nat.gcd algebra.ring_power
open algebra
namespace nat
definition nat_has_pow_nat [instance] [reducible] [priority nat.prio] : has_pow_nat nat :=
has_pow_nat.mk has_pow_nat.pow_nat
theorem pow_le_pow_of_le {x y : ℕ} (i : ℕ) (H : x ≤ y) : x^i ≤ y^i :=
algebra.pow_le_pow_of_le i !zero_le H
theorem eq_zero_of_pow_eq_zero {a m : ℕ} (H : a^m = 0) : a = 0 :=
or.elim (eq_zero_or_pos m)
(suppose m = 0,
by rewrite [`m = 0` at H, pow_zero at H]; contradiction)
(suppose m > 0,
have h₁ : ∀ m, a^succ m = 0 → a = 0,
begin
intro m,
induction m with m ih,
{rewrite pow_one; intros; assumption},
rewrite pow_succ,
intro H,
cases eq_zero_or_eq_zero_of_mul_eq_zero H with h₃ h₄,
assumption,
exact ih h₄
end,
obtain m' (h₂ : m = succ m'), from exists_eq_succ_of_pos `m > 0`,
show a = 0, by rewrite h₂ at H; apply h₁ m' H)
-- generalize to semirings?
theorem le_pow_self {x : ℕ} (H : x > 1) : ∀ i, i ≤ x^i
| 0 := !zero_le
| (succ j) := have x > 0, from lt.trans zero_lt_one H,
have h₁ : x^j ≥ 1, from succ_le_of_lt (pow_pos_of_pos _ this),
have x ≥ 2, from succ_le_of_lt H,
calc
succ j = j + 1 : rfl
... ≤ x^j + 1 : add_le_add_right (le_pow_self j)
... ≤ x^j + x^j : add_le_add_left h₁
... = x^j * (1 + 1) : by rewrite [left_distrib, *mul_one]
... = x^j * 2 : rfl
... ≤ x^j * x : mul_le_mul_left _ `x ≥ 2`
... = x^(succ j) : pow_succ'
-- TODO: eventually this will be subsumed under the algebraic theorems
theorem mul_self_eq_pow_2 (a : nat) : a * a = a ^ 2 :=
show a * a = a ^ (succ (succ zero)), from
by rewrite [*pow_succ, *pow_zero, mul_one]
theorem pow_cancel_left : ∀ {a b c : nat}, a > 1 → a ^ b = a ^ c → b = c
| a 0 0 h₁ h₂ := rfl
| a (succ b) 0 h₁ h₂ :=
assert a = 1, by rewrite [pow_succ at h₂, pow_zero at h₂]; exact (eq_one_of_mul_eq_one_right h₂),
assert 1 < 1, by rewrite [this at h₁]; exact h₁,
absurd `1 <[nat] 1` !lt.irrefl
| a 0 (succ c) h₁ h₂ :=
assert a = 1, by rewrite [pow_succ at h₂, pow_zero at h₂]; exact (eq_one_of_mul_eq_one_right (eq.symm h₂)),
assert 1 < 1, by rewrite [this at h₁]; exact h₁,
absurd `1 <[nat] 1` !lt.irrefl
| a (succ b) (succ c) h₁ h₂ :=
assert a ≠ 0, from assume aeq0, by rewrite [aeq0 at h₁]; exact (absurd h₁ dec_trivial),
assert a^b = a^c, by rewrite [*pow_succ at h₂]; exact (eq_of_mul_eq_mul_left (pos_of_ne_zero this) h₂),
by rewrite [pow_cancel_left h₁ this]
theorem pow_div_cancel : ∀ {a b : nat}, a ≠ 0 → (a ^ succ b) / a = a ^ b
| a 0 h := by rewrite [pow_succ, pow_zero, mul_one, nat.div_self (pos_of_ne_zero h)]
| a (succ b) h := by rewrite [pow_succ, nat.mul_div_cancel_left _ (pos_of_ne_zero h)]
lemma dvd_pow : ∀ (i : nat) {n : nat}, n > 0 → i ∣ i^n
| i 0 h := absurd h !lt.irrefl
| i (succ n) h := by rewrite [pow_succ']; apply dvd_mul_left
lemma dvd_pow_of_dvd_of_pos : ∀ {i j n : nat}, i ∣ j → n > 0 → i ∣ j^n
| i j 0 h₁ h₂ := absurd h₂ !lt.irrefl
| i j (succ n) h₁ h₂ := by rewrite [pow_succ']; apply dvd_mul_of_dvd_right h₁
lemma pow_mod_eq_zero (i : nat) {n : nat} (h : n > 0) : (i ^ n) % i = 0 :=
iff.mp !dvd_iff_mod_eq_zero (dvd_pow i h)
lemma pow_dvd_of_pow_succ_dvd {p i n : nat} : p^(succ i) ∣ n → p^i ∣ n :=
suppose p^(succ i) ∣ n,
assert p^i ∣ p^(succ i),
by rewrite [pow_succ']; apply nat.dvd_of_eq_mul; apply rfl,
dvd.trans `p^i ∣ p^(succ i)` `p^(succ i) ∣ n`
lemma dvd_of_pow_succ_dvd_mul_pow {p i n : nat} (Ppos : p > 0) :
p^(succ i) ∣ (n * p^i) → p ∣ n :=
by rewrite [pow_succ]; apply nat.dvd_of_mul_dvd_mul_right; apply pow_pos_of_pos _ Ppos
lemma coprime_pow_right {a b} : ∀ n, coprime b a → coprime b (a^n)
| 0 h := !comprime_one_right
| (succ n) h :=
begin
rewrite [pow_succ'],
apply coprime_mul_right,
exact coprime_pow_right n h,
exact h
end
lemma coprime_pow_left {a b} : ∀ n, coprime b a → coprime (b^n) a :=
take n, suppose coprime b a,
coprime_swap (coprime_pow_right n (coprime_swap this))
end nat
|
cdb24bbff6dfa4ec0947a1d4062da536da132b4b | bb31430994044506fa42fd667e2d556327e18dfe | /src/algebra/quaternion.lean | b9b6694cb7567c482221ee2c1216d9956f940a3f | [
"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 | 25,143 | 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 algebra.algebra.equiv
import set_theory.cardinal.ordinal
import tactic.ring_exp
/-!
# Quaternions
In this file we define quaternions `ℍ[R]` over a commutative ring `R`, and define some
algebraic structures on `ℍ[R]`.
## Main definitions
* `quaternion_algebra R a b`, `ℍ[R, a, b]` :
[quaternion algebra](https://en.wikipedia.org/wiki/Quaternion_algebra) with coefficients `a`, `b`
* `quaternion R`, `ℍ[R]` : the space of quaternions, a.k.a. `quaternion_algebra R (-1) (-1)`;
* `quaternion.norm_sq` : square of the norm of a quaternion;
* `quaternion.conj` : conjugate of a quaternion;
We also define the following algebraic structures on `ℍ[R]`:
* `ring ℍ[R, a, b]` and `algebra R ℍ[R, a, b]` : for any commutative ring `R`;
* `ring ℍ[R]` and `algebra R ℍ[R]` : for any commutative ring `R`;
* `domain ℍ[R]` : for a linear ordered commutative ring `R`;
* `division_algebra ℍ[R]` : for a linear ordered field `R`.
## Notation
The following notation is available with `open_locale quaternion`.
* `ℍ[R, c₁, c₂]` : `quaternion_algebra R c₁ c₂`
* `ℍ[R]` : quaternions over `R`.
## Implementation notes
We define quaternions over any ring `R`, not just `ℝ` to be able to deal with, e.g., integer
or rational quaternions without using real numbers. In particular, all definitions in this file
are computable.
## Tags
quaternion
-/
/-- Quaternion algebra over a type with fixed coefficients $a=i^2$ and $b=j^2$.
Implemented as a structure with four fields: `re`, `im_i`, `im_j`, and `im_k`. -/
@[nolint unused_arguments, ext]
structure quaternion_algebra (R : Type*) (a b : R) :=
mk {} :: (re : R) (im_i : R) (im_j : R) (im_k : R)
localized "notation (name := quaternion_algebra) `ℍ[` R`,` a`,` b `]` :=
quaternion_algebra R a b" in quaternion
namespace quaternion_algebra
/-- The equivalence between a quaternion algebra over R and R × R × R × R. -/
@[simps]
def equiv_prod {R : Type*} (c₁ c₂ : R) : ℍ[R, c₁, c₂] ≃ R × R × R × R :=
{ to_fun := λ a, ⟨a.1, a.2, a.3, a.4⟩,
inv_fun := λ a, ⟨a.1, a.2.1, a.2.2.1, a.2.2.2⟩,
left_inv := λ ⟨a₁, a₂, a₃, a₄⟩, rfl,
right_inv := λ ⟨a₁, a₂, a₃, a₄⟩, rfl }
@[simp] lemma mk.eta {R : Type*} {c₁ c₂} : ∀ a : ℍ[R, c₁, c₂], mk a.1 a.2 a.3 a.4 = a
| ⟨a₁, a₂, a₃, a₄⟩ := rfl
variables {R : Type*} [comm_ring R] {c₁ c₂ : R} (r x y z : R) (a b c : ℍ[R, c₁, c₂])
instance : has_coe_t R (ℍ[R, c₁, c₂]) := ⟨λ x, ⟨x, 0, 0, 0⟩⟩
@[simp] lemma coe_re : (x : ℍ[R, c₁, c₂]).re = x := rfl
@[simp] lemma coe_im_i : (x : ℍ[R, c₁, c₂]).im_i = 0 := rfl
@[simp] lemma coe_im_j : (x : ℍ[R, c₁, c₂]).im_j = 0 := rfl
@[simp] lemma coe_im_k : (x : ℍ[R, c₁, c₂]).im_k = 0 := rfl
lemma coe_injective : function.injective (coe : R → ℍ[R, c₁, c₂]) :=
λ x y h, congr_arg re h
@[simp] lemma coe_inj {x y : R} : (x : ℍ[R, c₁, c₂]) = y ↔ x = y := coe_injective.eq_iff
@[simps] instance : has_zero ℍ[R, c₁, c₂] := ⟨⟨0, 0, 0, 0⟩⟩
@[simp, norm_cast] lemma coe_zero : ((0 : R) : ℍ[R, c₁, c₂]) = 0 := rfl
instance : inhabited ℍ[R, c₁, c₂] := ⟨0⟩
@[simps] instance : has_one ℍ[R, c₁, c₂] := ⟨⟨1, 0, 0, 0⟩⟩
@[simp, norm_cast] lemma coe_one : ((1 : R) : ℍ[R, c₁, c₂]) = 1 := rfl
@[simps] instance : has_add ℍ[R, c₁, c₂] :=
⟨λ a b, ⟨a.1 + b.1, a.2 + b.2, a.3 + b.3, a.4 + b.4⟩⟩
@[simp] lemma mk_add_mk (a₁ a₂ a₃ a₄ b₁ b₂ b₃ b₄ : R) :
(mk a₁ a₂ a₃ a₄ : ℍ[R, c₁, c₂]) + mk b₁ b₂ b₃ b₄ = mk (a₁ + b₁) (a₂ + b₂) (a₃ + b₃) (a₄ + b₄) :=
rfl
@[norm_cast, simp] lemma coe_add : ((x + y : R) : ℍ[R, c₁, c₂]) = x + y :=
by ext; simp
@[simps] instance : has_neg ℍ[R, c₁, c₂] := ⟨λ a, ⟨-a.1, -a.2, -a.3, -a.4⟩⟩
@[simp] lemma neg_mk (a₁ a₂ a₃ a₄ : R) : -(mk a₁ a₂ a₃ a₄ : ℍ[R, c₁, c₂]) = ⟨-a₁, -a₂, -a₃, -a₄⟩ :=
rfl
@[norm_cast, simp] lemma coe_neg : ((-x : R) : ℍ[R, c₁, c₂]) = -x :=
by ext; simp
@[simps] instance : has_sub ℍ[R, c₁, c₂] :=
⟨λ a b, ⟨a.1 - b.1, a.2 - b.2, a.3 - b.3, a.4 - b.4⟩⟩
@[simp] lemma mk_sub_mk (a₁ a₂ a₃ a₄ b₁ b₂ b₃ b₄ : R) :
(mk a₁ a₂ a₃ a₄ : ℍ[R, c₁, c₂]) - mk b₁ b₂ b₃ b₄ = mk (a₁ - b₁) (a₂ - b₂) (a₃ - b₃) (a₄ - b₄) :=
rfl
/-- Multiplication is given by
* `1 * x = x * 1 = x`;
* `i * i = c₁`;
* `j * j = c₂`;
* `i * j = k`, `j * i = -k`;
* `k * k = -c₁ * c₂`;
* `i * k = c₁ * j`, `k * i = `-c₁ * j`;
* `j * k = -c₂ * i`, `k * j = c₂ * i`. -/
@[simps] instance : has_mul ℍ[R, c₁, c₂] := ⟨λ a b,
⟨a.1 * b.1 + c₁ * a.2 * b.2 + c₂ * a.3 * b.3 - c₁ * c₂ * a.4 * b.4,
a.1 * b.2 + a.2 * b.1 - c₂ * a.3 * b.4 + c₂ * a.4 * b.3,
a.1 * b.3 + c₁ * a.2 * b.4 + a.3 * b.1 - c₁ * a.4 * b.2,
a.1 * b.4 + a.2 * b.3 - a.3 * b.2 + a.4 * b.1⟩⟩
@[simp] lemma mk_mul_mk (a₁ a₂ a₃ a₄ b₁ b₂ b₃ b₄ : R) :
(mk a₁ a₂ a₃ a₄ : ℍ[R, c₁, c₂]) * mk b₁ b₂ b₃ b₄ =
⟨a₁ * b₁ + c₁ * a₂ * b₂ + c₂ * a₃ * b₃ - c₁ * c₂ * a₄ * b₄,
a₁ * b₂ + a₂ * b₁ - c₂ * a₃ * b₄ + c₂ * a₄ * b₃,
a₁ * b₃ + c₁ * a₂ * b₄ + a₃ * b₁ - c₁ * a₄ * b₂,
a₁ * b₄ + a₂ * b₃ - a₃ * b₂ + a₄ * b₁⟩ := rfl
instance : add_comm_group ℍ[R, c₁, c₂] :=
by refine_struct
{ add := (+),
neg := has_neg.neg,
sub := has_sub.sub,
zero := (0 : ℍ[R, c₁, c₂]),
zsmul := @zsmul_rec _ ⟨(0 : ℍ[R, c₁, c₂])⟩ ⟨(+)⟩ ⟨has_neg.neg⟩,
nsmul := @nsmul_rec _ ⟨(0 : ℍ[R, c₁, c₂])⟩ ⟨(+)⟩ };
intros; try { refl }; ext; simp; ring_exp
instance : add_group_with_one ℍ[R, c₁, c₂] :=
{ nat_cast := λ n, ((n : R) : ℍ[R, c₁, c₂]),
nat_cast_zero := by simp,
nat_cast_succ := by simp,
int_cast := λ n, ((n : R) : ℍ[R, c₁, c₂]),
int_cast_of_nat := λ _, congr_arg coe (int.cast_of_nat _),
int_cast_neg_succ_of_nat := λ n,
show ↑↑_ = -↑↑_, by rw [int.cast_neg, int.cast_coe_nat, coe_neg],
one := 1,
.. quaternion_algebra.add_comm_group }
instance : ring ℍ[R, c₁, c₂] :=
by refine_struct
{ add := (+),
mul := (*),
one := 1,
npow := @npow_rec _ ⟨(1 : ℍ[R, c₁, c₂])⟩ ⟨(*)⟩,
.. quaternion_algebra.add_group_with_one,
.. quaternion_algebra.add_comm_group };
intros; try { refl }; ext; simp; ring_exp
instance : algebra R ℍ[R, c₁, c₂] :=
{ smul := λ r a, ⟨r * a.1, r * a.2, r * a.3, r * a.4⟩,
to_fun := coe,
map_one' := rfl,
map_zero' := rfl,
map_mul' := λ x y, by ext; simp,
map_add' := λ x y, by ext; simp,
smul_def' := λ r x, by ext; simp,
commutes' := λ r x, by ext; simp [mul_comm] }
@[simp] lemma smul_re : (r • a).re = r • a.re := rfl
@[simp] lemma smul_im_i : (r • a).im_i = r • a.im_i := rfl
@[simp] lemma smul_im_j : (r • a).im_j = r • a.im_j := rfl
@[simp] lemma smul_im_k : (r • a).im_k = r • a.im_k := rfl
@[simp] lemma smul_mk (re im_i im_j im_k : R) :
r • (⟨re, im_i, im_j, im_k⟩ : ℍ[R, c₁, c₂]) = ⟨r • re, r • im_i, r • im_j, r • im_k⟩ := rfl
lemma algebra_map_eq (r : R) : algebra_map R ℍ[R,c₁,c₂] r = ⟨r, 0, 0, 0⟩ := rfl
section
variables (R c₁ c₂)
/-- `quaternion_algebra.re` as a `linear_map`-/
@[simps] def re_lm : ℍ[R, c₁, c₂] →ₗ[R] R :=
{ to_fun := re, map_add' := λ x y, rfl, map_smul' := λ r x, rfl }
/-- `quaternion_algebra.im_i` as a `linear_map`-/
@[simps] def im_i_lm : ℍ[R, c₁, c₂] →ₗ[R] R :=
{ to_fun := im_i, map_add' := λ x y, rfl, map_smul' := λ r x, rfl }
/-- `quaternion_algebra.im_j` as a `linear_map`-/
@[simps] def im_j_lm : ℍ[R, c₁, c₂] →ₗ[R] R :=
{ to_fun := im_j, map_add' := λ x y, rfl, map_smul' := λ r x, rfl }
/-- `quaternion_algebra.im_k` as a `linear_map`-/
@[simps] def im_k_lm : ℍ[R, c₁, c₂] →ₗ[R] R :=
{ to_fun := im_k, map_add' := λ x y, rfl, map_smul' := λ r x, rfl }
end
@[norm_cast, simp] lemma coe_sub : ((x - y : R) : ℍ[R, c₁, c₂]) = x - y :=
(algebra_map R ℍ[R, c₁, c₂]).map_sub x y
@[norm_cast, simp] lemma coe_mul : ((x * y : R) : ℍ[R, c₁, c₂]) = x * y :=
(algebra_map R ℍ[R, c₁, c₂]).map_mul x y
lemma coe_commutes : ↑r * a = a * r := algebra.commutes r a
lemma coe_commute : commute ↑r a := coe_commutes r a
lemma coe_mul_eq_smul : ↑r * a = r • a := (algebra.smul_def r a).symm
lemma mul_coe_eq_smul : a * r = r • a :=
by rw [← coe_commutes, coe_mul_eq_smul]
@[norm_cast, simp] lemma coe_algebra_map : ⇑(algebra_map R ℍ[R, c₁, c₂]) = coe := rfl
lemma smul_coe : x • (y : ℍ[R, c₁, c₂]) = ↑(x * y) := by rw [coe_mul, coe_mul_eq_smul]
/-- Quaternion conjugate. -/
def conj : ℍ[R, c₁, c₂] ≃ₗ[R] ℍ[R, c₁, c₂] :=
linear_equiv.of_involutive
{ to_fun := λ a, ⟨a.1, -a.2, -a.3, -a.4⟩,
map_add' := λ a b, by ext; simp [neg_add],
map_smul' := λ r a, by ext; simp } $
λ a, by simp
@[simp] lemma re_conj : (conj a).re = a.re := rfl
@[simp] lemma im_i_conj : (conj a).im_i = - a.im_i := rfl
@[simp] lemma im_j_conj : (conj a).im_j = - a.im_j := rfl
@[simp] lemma im_k_conj : (conj a).im_k = - a.im_k := rfl
@[simp] lemma conj_mk (a₁ a₂ a₃ a₄ : R) :
conj (mk a₁ a₂ a₃ a₄ : ℍ[R, c₁, c₂]) = ⟨a₁, -a₂, -a₃, -a₄⟩ :=
rfl
@[simp] lemma conj_conj : a.conj.conj = a := ext _ _ rfl (neg_neg _) (neg_neg _) (neg_neg _)
lemma conj_add : (a + b).conj = a.conj + b.conj := conj.map_add a b
@[simp] lemma conj_mul : (a * b).conj = b.conj * a.conj := by ext; simp; ring_exp
lemma conj_conj_mul : (a.conj * b).conj = b.conj * a :=
by rw [conj_mul, conj_conj]
lemma conj_mul_conj : (a * b.conj).conj = b * a.conj :=
by rw [conj_mul, conj_conj]
lemma self_add_conj' : a + a.conj = ↑(2 * a.re) := by ext; simp [two_mul]
lemma self_add_conj : a + a.conj = 2 * a.re :=
by simp only [self_add_conj', two_mul, coe_add]
lemma conj_add_self' : a.conj + a = ↑(2 * a.re) := by rw [add_comm, self_add_conj']
lemma conj_add_self : a.conj + a = 2 * a.re := by rw [add_comm, self_add_conj]
lemma conj_eq_two_re_sub : a.conj = ↑(2 * a.re) - a := eq_sub_iff_add_eq.2 a.conj_add_self'
lemma commute_conj_self : commute a.conj a :=
begin
rw [a.conj_eq_two_re_sub],
exact (coe_commute (2 * a.re) a).sub_left (commute.refl a)
end
lemma commute_self_conj : commute a a.conj :=
a.commute_conj_self.symm
lemma commute_conj_conj {a b : ℍ[R, c₁, c₂]} (h : commute a b) : commute a.conj b.conj :=
calc a.conj * b.conj = (b * a).conj : (conj_mul b a).symm
... = (a * b).conj : by rw h.eq
... = b.conj * a.conj : conj_mul a b
@[simp] lemma conj_coe : conj (x : ℍ[R, c₁, c₂]) = x := by ext; simp
lemma conj_smul : conj (r • a) = r • conj a := conj.map_smul r a
@[simp] lemma conj_one : conj (1 : ℍ[R, c₁, c₂]) = 1 := conj_coe 1
lemma eq_re_of_eq_coe {a : ℍ[R, c₁, c₂]} {x : R} (h : a = x) : a = a.re :=
by rw [h, coe_re]
lemma eq_re_iff_mem_range_coe {a : ℍ[R, c₁, c₂]} :
a = a.re ↔ a ∈ set.range (coe : R → ℍ[R, c₁, c₂]) :=
⟨λ h, ⟨a.re, h.symm⟩, λ ⟨x, h⟩, eq_re_of_eq_coe h.symm⟩
@[simp]
lemma conj_fixed {R : Type*} [comm_ring R] [no_zero_divisors R] [char_zero R]
{c₁ c₂ : R} {a : ℍ[R, c₁, c₂]} :
conj a = a ↔ a = a.re :=
by simp [ext_iff, neg_eq_iff_add_eq_zero, add_self_eq_zero]
-- Can't use `rw ← conj_fixed` in the proof without additional assumptions
lemma conj_mul_eq_coe : conj a * a = (conj a * a).re := by ext; simp; ring_exp
lemma mul_conj_eq_coe : a * conj a = (a * conj a).re :=
by { rw a.commute_self_conj.eq, exact a.conj_mul_eq_coe }
lemma conj_zero : conj (0 : ℍ[R, c₁, c₂]) = 0 := conj.map_zero
lemma conj_neg : (-a).conj = -a.conj := (conj : ℍ[R, c₁, c₂] ≃ₗ[R] _).map_neg a
lemma conj_sub : (a - b).conj = a.conj - b.conj := (conj : ℍ[R, c₁, c₂] ≃ₗ[R] _).map_sub a b
instance : star_ring ℍ[R, c₁, c₂] :=
{ star := conj,
star_involutive := conj_conj,
star_add := conj_add,
star_mul := conj_mul }
@[simp] lemma star_def (a : ℍ[R, c₁, c₂]) : star a = conj a := rfl
open mul_opposite
/-- Quaternion conjugate as an `alg_equiv` to the opposite ring. -/
def conj_ae : ℍ[R, c₁, c₂] ≃ₐ[R] (ℍ[R, c₁, c₂]ᵐᵒᵖ) :=
{ to_fun := op ∘ conj,
inv_fun := conj ∘ unop,
map_mul' := λ x y, by simp,
commutes' := λ r, by simp,
.. conj.to_add_equiv.trans op_add_equiv }
@[simp] lemma coe_conj_ae : ⇑(conj_ae : ℍ[R, c₁, c₂] ≃ₐ[R] _) = op ∘ conj := rfl
end quaternion_algebra
/-- Space of quaternions over a type. Implemented as a structure with four fields:
`re`, `im_i`, `im_j`, and `im_k`. -/
def quaternion (R : Type*) [has_one R] [has_neg R] := quaternion_algebra R (-1) (-1)
localized "notation (name := quaternion) `ℍ[` R `]` := quaternion R" in quaternion
/-- The equivalence between the quaternions over R and R × R × R × R. -/
def quaternion.equiv_prod (R : Type*) [has_one R] [has_neg R] : ℍ[R] ≃ R × R × R × R :=
quaternion_algebra.equiv_prod _ _
namespace quaternion
variables {R : Type*} [comm_ring R] (r x y z : R) (a b c : ℍ[R])
export quaternion_algebra (re im_i im_j im_k)
instance : has_coe_t R ℍ[R] := quaternion_algebra.has_coe_t
instance : ring ℍ[R] := quaternion_algebra.ring
instance : inhabited ℍ[R] := quaternion_algebra.inhabited
instance : algebra R ℍ[R] := quaternion_algebra.algebra
instance : star_ring ℍ[R] := quaternion_algebra.star_ring
@[ext] lemma ext : a.re = b.re → a.im_i = b.im_i → a.im_j = b.im_j → a.im_k = b.im_k → a = b :=
quaternion_algebra.ext a b
lemma ext_iff {a b : ℍ[R]} :
a = b ↔ a.re = b.re ∧ a.im_i = b.im_i ∧ a.im_j = b.im_j ∧ a.im_k = b.im_k :=
quaternion_algebra.ext_iff a b
@[simp, norm_cast] lemma coe_re : (x : ℍ[R]).re = x := rfl
@[simp, norm_cast] lemma coe_im_i : (x : ℍ[R]).im_i = 0 := rfl
@[simp, norm_cast] lemma coe_im_j : (x : ℍ[R]).im_j = 0 := rfl
@[simp, norm_cast] lemma coe_im_k : (x : ℍ[R]).im_k = 0 := rfl
@[simp] lemma zero_re : (0 : ℍ[R]).re = 0 := rfl
@[simp] lemma zero_im_i : (0 : ℍ[R]).im_i = 0 := rfl
@[simp] lemma zero_im_j : (0 : ℍ[R]).im_j = 0 := rfl
@[simp] lemma zero_im_k : (0 : ℍ[R]).im_k = 0 := rfl
@[simp, norm_cast] lemma coe_zero : ((0 : R) : ℍ[R]) = 0 := rfl
@[simp] lemma one_re : (1 : ℍ[R]).re = 1 := rfl
@[simp] lemma one_im_i : (1 : ℍ[R]).im_i = 0 := rfl
@[simp] lemma one_im_j : (1 : ℍ[R]).im_j = 0 := rfl
@[simp] lemma one_im_k : (1 : ℍ[R]).im_k = 0 := rfl
@[simp, norm_cast] lemma coe_one : ((1 : R) : ℍ[R]) = 1 := rfl
@[simp] lemma add_re : (a + b).re = a.re + b.re := rfl
@[simp] lemma add_im_i : (a + b).im_i = a.im_i + b.im_i := rfl
@[simp] lemma add_im_j : (a + b).im_j = a.im_j + b.im_j := rfl
@[simp] lemma add_im_k : (a + b).im_k = a.im_k + b.im_k := rfl
@[simp, norm_cast] lemma coe_add : ((x + y : R) : ℍ[R]) = x + y := quaternion_algebra.coe_add x y
@[simp] lemma neg_re : (-a).re = -a.re := rfl
@[simp] lemma neg_im_i : (-a).im_i = -a.im_i := rfl
@[simp] lemma neg_im_j : (-a).im_j = -a.im_j := rfl
@[simp] lemma neg_im_k : (-a).im_k = -a.im_k := rfl
@[simp, norm_cast] lemma coe_neg : ((-x : R) : ℍ[R]) = -x := quaternion_algebra.coe_neg x
@[simp] lemma sub_re : (a - b).re = a.re - b.re := rfl
@[simp] lemma sub_im_i : (a - b).im_i = a.im_i - b.im_i := rfl
@[simp] lemma sub_im_j : (a - b).im_j = a.im_j - b.im_j := rfl
@[simp] lemma sub_im_k : (a - b).im_k = a.im_k - b.im_k := rfl
@[simp, norm_cast] lemma coe_sub : ((x - y : R) : ℍ[R]) = x - y := quaternion_algebra.coe_sub x y
@[simp] lemma mul_re :
(a * b).re = a.re * b.re - a.im_i * b.im_i - a.im_j * b.im_j - a.im_k * b.im_k :=
(quaternion_algebra.has_mul_mul_re a b).trans $
by simp only [one_mul, neg_mul, sub_eq_add_neg, neg_neg]
@[simp] lemma mul_im_i :
(a * b).im_i = a.re * b.im_i + a.im_i * b.re + a.im_j * b.im_k - a.im_k * b.im_j :=
(quaternion_algebra.has_mul_mul_im_i a b).trans $
by simp only [one_mul, neg_mul, sub_eq_add_neg, neg_neg]
@[simp] lemma mul_im_j :
(a * b).im_j = a.re * b.im_j - a.im_i * b.im_k + a.im_j * b.re + a.im_k * b.im_i :=
(quaternion_algebra.has_mul_mul_im_j a b).trans $
by simp only [one_mul, neg_mul, sub_eq_add_neg, neg_neg]
@[simp] lemma mul_im_k :
(a * b).im_k = a.re * b.im_k + a.im_i * b.im_j - a.im_j * b.im_i + a.im_k * b.re :=
(quaternion_algebra.has_mul_mul_im_k a b).trans $
by simp only [one_mul, neg_mul, sub_eq_add_neg, neg_neg]
@[simp, norm_cast] lemma coe_mul : ((x * y : R) : ℍ[R]) = x * y := quaternion_algebra.coe_mul x y
lemma coe_injective : function.injective (coe : R → ℍ[R]) := quaternion_algebra.coe_injective
@[simp] lemma coe_inj {x y : R} : (x : ℍ[R]) = y ↔ x = y := coe_injective.eq_iff
@[simp] lemma smul_re : (r • a).re = r • a.re := rfl
@[simp] lemma smul_im_i : (r • a).im_i = r • a.im_i := rfl
@[simp] lemma smul_im_j : (r • a).im_j = r • a.im_j := rfl
@[simp] lemma smul_im_k : (r • a).im_k = r • a.im_k := rfl
lemma coe_commutes : ↑r * a = a * r := quaternion_algebra.coe_commutes r a
lemma coe_commute : commute ↑r a := quaternion_algebra.coe_commute r a
lemma coe_mul_eq_smul : ↑r * a = r • a := quaternion_algebra.coe_mul_eq_smul r a
lemma mul_coe_eq_smul : a * r = r • a := quaternion_algebra.mul_coe_eq_smul r a
@[simp] lemma algebra_map_def : ⇑(algebra_map R ℍ[R]) = coe := rfl
lemma smul_coe : x • (y : ℍ[R]) = ↑(x * y) := quaternion_algebra.smul_coe x y
/-- Quaternion conjugate. -/
def conj : ℍ[R] ≃ₗ[R] ℍ[R] := quaternion_algebra.conj
@[simp] lemma conj_re : a.conj.re = a.re := rfl
@[simp] lemma conj_im_i : a.conj.im_i = - a.im_i := rfl
@[simp] lemma conj_im_j : a.conj.im_j = - a.im_j := rfl
@[simp] lemma conj_im_k : a.conj.im_k = - a.im_k := rfl
@[simp] lemma conj_conj : a.conj.conj = a := a.conj_conj
@[simp] lemma conj_add : (a + b).conj = a.conj + b.conj := a.conj_add b
@[simp] lemma conj_mul : (a * b).conj = b.conj * a.conj := a.conj_mul b
lemma conj_conj_mul : (a.conj * b).conj = b.conj * a := a.conj_conj_mul b
lemma conj_mul_conj : (a * b.conj).conj = b * a.conj := a.conj_mul_conj b
lemma self_add_conj' : a + a.conj = ↑(2 * a.re) := a.self_add_conj'
lemma self_add_conj : a + a.conj = 2 * a.re := a.self_add_conj
lemma conj_add_self' : a.conj + a = ↑(2 * a.re) := a.conj_add_self'
lemma conj_add_self : a.conj + a = 2 * a.re := a.conj_add_self
lemma conj_eq_two_re_sub : a.conj = ↑(2 * a.re) - a := a.conj_eq_two_re_sub
lemma commute_conj_self : commute a.conj a := a.commute_conj_self
lemma commute_self_conj : commute a a.conj := a.commute_self_conj
lemma commute_conj_conj {a b : ℍ[R]} (h : commute a b) : commute a.conj b.conj :=
quaternion_algebra.commute_conj_conj h
alias commute_conj_conj ← commute.quaternion_conj
@[simp] lemma conj_coe : conj (x : ℍ[R]) = x := quaternion_algebra.conj_coe x
@[simp] lemma conj_smul : conj (r • a) = r • conj a := a.conj_smul r
@[simp] lemma conj_one : conj (1 : ℍ[R]) = 1 := conj_coe 1
lemma eq_re_of_eq_coe {a : ℍ[R]} {x : R} (h : a = x) : a = a.re :=
quaternion_algebra.eq_re_of_eq_coe h
lemma eq_re_iff_mem_range_coe {a : ℍ[R]} : a = a.re ↔ a ∈ set.range (coe : R → ℍ[R]) :=
quaternion_algebra.eq_re_iff_mem_range_coe
@[simp] lemma conj_fixed {R : Type*} [comm_ring R] [no_zero_divisors R] [char_zero R] {a : ℍ[R]} :
conj a = a ↔ a = a.re :=
quaternion_algebra.conj_fixed
lemma conj_mul_eq_coe : conj a * a = (conj a * a).re := a.conj_mul_eq_coe
lemma mul_conj_eq_coe : a * conj a = (a * conj a).re := a.mul_conj_eq_coe
@[simp] lemma conj_zero : conj (0:ℍ[R]) = 0 := quaternion_algebra.conj_zero
@[simp] lemma conj_neg : (-a).conj = -a.conj := a.conj_neg
@[simp] lemma conj_sub : (a - b).conj = a.conj - b.conj := a.conj_sub b
open mul_opposite
/-- Quaternion conjugate as an `alg_equiv` to the opposite ring. -/
def conj_ae : ℍ[R] ≃ₐ[R] (ℍ[R]ᵐᵒᵖ) := quaternion_algebra.conj_ae
@[simp] lemma coe_conj_ae : ⇑(conj_ae : ℍ[R] ≃ₐ[R] ℍ[R]ᵐᵒᵖ) = op ∘ conj := rfl
/-- Square of the norm. -/
def norm_sq : ℍ[R] →*₀ R :=
{ to_fun := λ a, (a * a.conj).re,
map_zero' := by rw [conj_zero, zero_mul, zero_re],
map_one' := by rw [conj_one, one_mul, one_re],
map_mul' := λ x y, coe_injective $ by conv_lhs { rw [← mul_conj_eq_coe, conj_mul, mul_assoc,
← mul_assoc y, y.mul_conj_eq_coe, coe_commutes, ← mul_assoc, x.mul_conj_eq_coe, ← coe_mul] } }
lemma norm_sq_def : norm_sq a = (a * a.conj).re := rfl
lemma norm_sq_def' : norm_sq a = a.1^2 + a.2^2 + a.3^2 + a.4^2 :=
by simp only [norm_sq_def, sq, mul_neg, sub_neg_eq_add,
mul_re, conj_re, conj_im_i, conj_im_j, conj_im_k]
lemma norm_sq_coe : norm_sq (x : ℍ[R]) = x^2 :=
by rw [norm_sq_def, conj_coe, ← coe_mul, coe_re, sq]
@[simp] lemma norm_sq_neg : norm_sq (-a) = norm_sq a :=
by simp only [norm_sq_def, conj_neg, neg_mul_neg]
lemma self_mul_conj : a * a.conj = norm_sq a := by rw [mul_conj_eq_coe, norm_sq_def]
lemma conj_mul_self : a.conj * a = norm_sq a := by rw [← a.commute_self_conj.eq, self_mul_conj]
lemma coe_norm_sq_add :
(norm_sq (a + b) : ℍ[R]) = norm_sq a + a * b.conj + b * a.conj + norm_sq b :=
by simp [← self_mul_conj, mul_add, add_mul, add_assoc]
end quaternion
namespace quaternion
variables {R : Type*}
section linear_ordered_comm_ring
variables [linear_ordered_comm_ring R] {a : ℍ[R]}
@[simp] lemma norm_sq_eq_zero : norm_sq a = 0 ↔ a = 0 :=
begin
refine ⟨λ h, _, λ h, h.symm ▸ norm_sq.map_zero⟩,
rw [norm_sq_def', add_eq_zero_iff', add_eq_zero_iff', add_eq_zero_iff'] at h,
exact ext a 0 (pow_eq_zero h.1.1.1) (pow_eq_zero h.1.1.2) (pow_eq_zero h.1.2) (pow_eq_zero h.2),
all_goals { apply_rules [sq_nonneg, add_nonneg] }
end
lemma norm_sq_ne_zero : norm_sq a ≠ 0 ↔ a ≠ 0 := not_congr norm_sq_eq_zero
@[simp] lemma norm_sq_nonneg : 0 ≤ norm_sq a :=
by { rw norm_sq_def', apply_rules [sq_nonneg, add_nonneg] }
@[simp] lemma norm_sq_le_zero : norm_sq a ≤ 0 ↔ a = 0 :=
by simpa only [le_antisymm_iff, norm_sq_nonneg, and_true] using @norm_sq_eq_zero _ _ a
instance : nontrivial ℍ[R] :=
{ exists_pair_ne := ⟨0, 1, mt (congr_arg re) zero_ne_one⟩, }
instance : no_zero_divisors ℍ[R] :=
{ eq_zero_or_eq_zero_of_mul_eq_zero := λ a b hab,
have norm_sq a * norm_sq b = 0, by rwa [← norm_sq.map_mul, norm_sq_eq_zero],
(eq_zero_or_eq_zero_of_mul_eq_zero this).imp norm_sq_eq_zero.1 norm_sq_eq_zero.1,
..quaternion.nontrivial, }
instance : is_domain ℍ[R] :=
no_zero_divisors.to_is_domain _
end linear_ordered_comm_ring
section field
variables [linear_ordered_field R] (a b : ℍ[R])
@[simps { attrs := [] }]instance : has_inv ℍ[R] := ⟨λ a, (norm_sq a)⁻¹ • a.conj⟩
instance : division_ring ℍ[R] :=
{ inv := has_inv.inv,
inv_zero := by rw [has_inv_inv, conj_zero, smul_zero],
mul_inv_cancel := λ a ha, by rw [has_inv_inv, algebra.mul_smul_comm, self_mul_conj, smul_coe,
inv_mul_cancel (norm_sq_ne_zero.2 ha), coe_one],
.. quaternion.nontrivial,
.. quaternion.ring }
@[simp] lemma norm_sq_inv : norm_sq a⁻¹ = (norm_sq a)⁻¹ := map_inv₀ norm_sq _
@[simp] lemma norm_sq_div : norm_sq (a / b) = norm_sq a / norm_sq b := map_div₀ norm_sq a b
end field
end quaternion
namespace cardinal
open_locale cardinal quaternion
section quaternion_algebra
variables {R : Type*} (c₁ c₂ : R)
private theorem pow_four [infinite R] : #R ^ 4 = #R :=
power_nat_eq (aleph_0_le_mk R) $ by simp
/-- The cardinality of a quaternion algebra, as a type. -/
lemma mk_quaternion_algebra : #ℍ[R, c₁, c₂] = #R ^ 4 :=
by { rw mk_congr (quaternion_algebra.equiv_prod c₁ c₂), simp only [mk_prod, lift_id], ring }
@[simp] lemma mk_quaternion_algebra_of_infinite [infinite R] : #ℍ[R, c₁, c₂] = #R :=
by rw [mk_quaternion_algebra, pow_four]
/-- The cardinality of a quaternion algebra, as a set. -/
lemma mk_univ_quaternion_algebra : #(set.univ : set ℍ[R, c₁, c₂]) = #R ^ 4 :=
by rw [mk_univ, mk_quaternion_algebra]
@[simp] lemma mk_univ_quaternion_algebra_of_infinite [infinite R] :
#(set.univ : set ℍ[R, c₁, c₂]) = #R :=
by rw [mk_univ_quaternion_algebra, pow_four]
end quaternion_algebra
section quaternion
variables (R : Type*) [has_one R] [has_neg R]
/-- The cardinality of the quaternions, as a type. -/
@[simp] lemma mk_quaternion : #ℍ[R] = #R ^ 4 :=
mk_quaternion_algebra _ _
@[simp] lemma mk_quaternion_of_infinite [infinite R] : #ℍ[R] = #R :=
by rw [mk_quaternion, pow_four]
/-- The cardinality of the quaternions, as a set. -/
@[simp] lemma mk_univ_quaternion : #(set.univ : set ℍ[R]) = #R ^ 4 :=
mk_univ_quaternion_algebra _ _
@[simp] lemma mk_univ_quaternion_of_infinite [infinite R] : #(set.univ : set ℍ[R]) = #R :=
by rw [mk_univ_quaternion, pow_four]
end quaternion
end cardinal
|
a064b3ed0567b92c8badde75387be9b3e75a739e | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/category_theory/monoidal/braided.lean | bc15bf64ccc396f751c7aa59da9d2906f5c8cd64 | [
"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 | 35,004 | lean | /-
Copyright (c) 2020 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import category_theory.monoidal.coherence_lemmas
import category_theory.monoidal.natural_transformation
import category_theory.monoidal.discrete
/-!
# Braided and symmetric monoidal categories
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
The basic definitions of braided monoidal categories, and symmetric monoidal categories,
as well as braided functors.
## Implementation note
We make `braided_monoidal_category` another typeclass, but then have `symmetric_monoidal_category`
extend this. The rationale is that we are not carrying any additional data,
just requiring a property.
## Future work
* Construct the Drinfeld center of a monoidal category as a braided monoidal category.
* Say something about pseudo-natural transformations.
-/
open category_theory
universes v v₁ v₂ v₃ u u₁ u₂ u₃
namespace category_theory
/--
A braided monoidal category is a monoidal category equipped with a braiding isomorphism
`β_ X Y : X ⊗ Y ≅ Y ⊗ X`
which is natural in both arguments,
and also satisfies the two hexagon identities.
-/
class braided_category (C : Type u) [category.{v} C] [monoidal_category.{v} C] :=
-- braiding natural iso:
(braiding : Π X Y : C, X ⊗ Y ≅ Y ⊗ X)
(braiding_naturality' : ∀ {X X' Y Y' : C} (f : X ⟶ Y) (g : X' ⟶ Y'),
(f ⊗ g) ≫ (braiding Y Y').hom = (braiding X X').hom ≫ (g ⊗ f) . obviously)
-- hexagon identities:
(hexagon_forward' : Π X Y Z : C,
(α_ X Y Z).hom ≫ (braiding X (Y ⊗ Z)).hom ≫ (α_ Y Z X).hom
= ((braiding X Y).hom ⊗ (𝟙 Z)) ≫ (α_ Y X Z).hom ≫ ((𝟙 Y) ⊗ (braiding X Z).hom)
. obviously)
(hexagon_reverse' : Π X Y Z : C,
(α_ X Y Z).inv ≫ (braiding (X ⊗ Y) Z).hom ≫ (α_ Z X Y).inv
= ((𝟙 X) ⊗ (braiding Y Z).hom) ≫ (α_ X Z Y).inv ≫ ((braiding X Z).hom ⊗ (𝟙 Y))
. obviously)
restate_axiom braided_category.braiding_naturality'
attribute [simp,reassoc] braided_category.braiding_naturality
restate_axiom braided_category.hexagon_forward'
restate_axiom braided_category.hexagon_reverse'
attribute [reassoc] braided_category.hexagon_forward braided_category.hexagon_reverse
open category
open monoidal_category
open braided_category
notation `β_` := braiding
/--
Verifying the axioms for a braiding by checking that the candidate braiding is sent to a braiding
by a faithful monoidal functor.
-/
def braided_category_of_faithful {C D : Type*} [category C] [category D]
[monoidal_category C] [monoidal_category D] (F : monoidal_functor C D) [faithful F.to_functor]
[braided_category D] (β : Π X Y : C, X ⊗ Y ≅ Y ⊗ X)
(w : ∀ X Y, F.μ _ _ ≫ F.map (β X Y).hom = (β_ _ _).hom ≫ F.μ _ _) : braided_category C :=
{ braiding := β,
braiding_naturality' := begin
intros,
apply F.to_functor.map_injective,
refine (cancel_epi (F.μ _ _)).1 _,
rw [functor.map_comp, ←lax_monoidal_functor.μ_natural_assoc, w, functor.map_comp, reassoc_of w,
braiding_naturality_assoc, lax_monoidal_functor.μ_natural],
end,
hexagon_forward' := begin
intros,
apply F.to_functor.map_injective,
refine (cancel_epi (F.μ _ _)).1 _,
refine (cancel_epi (F.μ _ _ ⊗ 𝟙 _)).1 _,
rw [functor.map_comp, functor.map_comp, functor.map_comp, functor.map_comp,
←lax_monoidal_functor.μ_natural_assoc, functor.map_id, ←comp_tensor_id_assoc, w,
comp_tensor_id, category.assoc, lax_monoidal_functor.associativity_assoc,
lax_monoidal_functor.associativity_assoc, ←lax_monoidal_functor.μ_natural, functor.map_id,
←id_tensor_comp_assoc, w, id_tensor_comp_assoc, reassoc_of w, braiding_naturality_assoc,
lax_monoidal_functor.associativity, hexagon_forward_assoc],
end,
hexagon_reverse' := begin
intros,
apply F.to_functor.map_injective,
refine (cancel_epi (F.μ _ _)).1 _,
refine (cancel_epi (𝟙 _ ⊗ F.μ _ _)).1 _,
rw [functor.map_comp, functor.map_comp, functor.map_comp, functor.map_comp,
←lax_monoidal_functor.μ_natural_assoc, functor.map_id, ←id_tensor_comp_assoc, w,
id_tensor_comp_assoc, lax_monoidal_functor.associativity_inv_assoc,
lax_monoidal_functor.associativity_inv_assoc, ←lax_monoidal_functor.μ_natural, functor.map_id,
←comp_tensor_id_assoc, w, comp_tensor_id_assoc, reassoc_of w, braiding_naturality_assoc,
lax_monoidal_functor.associativity_inv, hexagon_reverse_assoc],
end, }
/-- Pull back a braiding along a fully faithful monoidal functor. -/
noncomputable
def braided_category_of_fully_faithful {C D : Type*} [category C] [category D]
[monoidal_category C] [monoidal_category D] (F : monoidal_functor C D)
[full F.to_functor] [faithful F.to_functor]
[braided_category D] : braided_category C :=
braided_category_of_faithful F (λ X Y, F.to_functor.preimage_iso
((as_iso (F.μ _ _)).symm ≪≫ β_ (F.obj X) (F.obj Y) ≪≫ (as_iso (F.μ _ _))))
(by tidy)
section
/-!
We now establish how the braiding interacts with the unitors.
I couldn't find a detailed proof in print, but this is discussed in:
* Proposition 1 of André Joyal and Ross Street,
"Braided monoidal categories", Macquarie Math Reports 860081 (1986).
* Proposition 2.1 of André Joyal and Ross Street,
"Braided tensor categories" , Adv. Math. 102 (1993), 20–78.
* Exercise 8.1.6 of Etingof, Gelaki, Nikshych, Ostrik,
"Tensor categories", vol 25, Mathematical Surveys and Monographs (2015), AMS.
-/
variables (C : Type u₁) [category.{v₁} C] [monoidal_category C] [braided_category C]
lemma braiding_left_unitor_aux₁ (X : C) :
(α_ (𝟙_ C) (𝟙_ C) X).hom ≫ (𝟙 (𝟙_ C) ⊗ (β_ X (𝟙_ C)).inv) ≫ (α_ _ X _).inv ≫ ((λ_ X).hom ⊗ 𝟙 _) =
((λ_ _).hom ⊗ 𝟙 X) ≫ (β_ X (𝟙_ C)).inv :=
by { rw [←left_unitor_tensor, left_unitor_naturality], simp, }
lemma braiding_left_unitor_aux₂ (X : C) :
((β_ X (𝟙_ C)).hom ⊗ (𝟙 (𝟙_ C))) ≫ ((λ_ X).hom ⊗ (𝟙 (𝟙_ C))) = (ρ_ X).hom ⊗ (𝟙 (𝟙_ C)) :=
calc ((β_ X (𝟙_ C)).hom ⊗ (𝟙 (𝟙_ C))) ≫ ((λ_ X).hom ⊗ (𝟙 (𝟙_ C)))
= ((β_ X (𝟙_ C)).hom ⊗ (𝟙 (𝟙_ C))) ≫ (α_ _ _ _).hom ≫ (α_ _ _ _).inv ≫
((λ_ X).hom ⊗ (𝟙 (𝟙_ C)))
: by coherence
... = ((β_ X (𝟙_ C)).hom ⊗ (𝟙 (𝟙_ C))) ≫ (α_ _ _ _).hom ≫ (𝟙 _ ⊗ (β_ X _).hom) ≫
(𝟙 _ ⊗ (β_ X _).inv) ≫ (α_ _ _ _).inv ≫ ((λ_ X).hom ⊗ (𝟙 (𝟙_ C)))
: by { slice_rhs 3 4 { rw [←id_tensor_comp, iso.hom_inv_id, tensor_id], }, rw [id_comp], }
... = (α_ _ _ _).hom ≫ (β_ _ _).hom ≫
(α_ _ _ _).hom ≫ (𝟙 _ ⊗ (β_ X _).inv) ≫ (α_ _ _ _).inv ≫ ((λ_ X).hom ⊗ (𝟙 (𝟙_ C)))
: by { slice_lhs 1 3 { rw ←hexagon_forward }, simp only [assoc], }
... = (α_ _ _ _).hom ≫ (β_ _ _).hom ≫ ((λ_ _).hom ⊗ 𝟙 X) ≫ (β_ X _).inv
: by rw braiding_left_unitor_aux₁
... = (α_ _ _ _).hom ≫ (𝟙 _ ⊗ (λ_ _).hom) ≫ (β_ _ _).hom ≫ (β_ X _).inv
: by { slice_lhs 2 3 { rw [←braiding_naturality] }, simp only [assoc], }
... = (α_ _ _ _).hom ≫ (𝟙 _ ⊗ (λ_ _).hom)
: by rw [iso.hom_inv_id, comp_id]
... = (ρ_ X).hom ⊗ (𝟙 (𝟙_ C))
: by rw triangle
@[simp]
lemma braiding_left_unitor (X : C) : (β_ X (𝟙_ C)).hom ≫ (λ_ X).hom = (ρ_ X).hom :=
by rw [←tensor_right_iff, comp_tensor_id, braiding_left_unitor_aux₂]
lemma braiding_right_unitor_aux₁ (X : C) :
(α_ X (𝟙_ C) (𝟙_ C)).inv ≫ ((β_ (𝟙_ C) X).inv ⊗ 𝟙 (𝟙_ C)) ≫ (α_ _ X _).hom ≫ (𝟙 _ ⊗ (ρ_ X).hom) =
(𝟙 X ⊗ (ρ_ _).hom) ≫ (β_ (𝟙_ C) X).inv :=
by { rw [←right_unitor_tensor, right_unitor_naturality], simp, }
lemma braiding_right_unitor_aux₂ (X : C) :
((𝟙 (𝟙_ C)) ⊗ (β_ (𝟙_ C) X).hom) ≫ ((𝟙 (𝟙_ C)) ⊗ (ρ_ X).hom) = (𝟙 (𝟙_ C)) ⊗ (λ_ X).hom :=
calc ((𝟙 (𝟙_ C)) ⊗ (β_ (𝟙_ C) X).hom) ≫ ((𝟙 (𝟙_ C)) ⊗ (ρ_ X).hom)
= ((𝟙 (𝟙_ C)) ⊗ (β_ (𝟙_ C) X).hom) ≫ (α_ _ _ _).inv ≫ (α_ _ _ _).hom ≫
((𝟙 (𝟙_ C)) ⊗ (ρ_ X).hom)
: by coherence
... = ((𝟙 (𝟙_ C)) ⊗ (β_ (𝟙_ C) X).hom) ≫ (α_ _ _ _).inv ≫ ((β_ _ X).hom ⊗ 𝟙 _) ≫
((β_ _ X).inv ⊗ 𝟙 _) ≫ (α_ _ _ _).hom ≫ ((𝟙 (𝟙_ C)) ⊗ (ρ_ X).hom)
: by { slice_rhs 3 4 { rw [←comp_tensor_id, iso.hom_inv_id, tensor_id], }, rw [id_comp], }
... = (α_ _ _ _).inv ≫ (β_ _ _).hom ≫
(α_ _ _ _).inv ≫ ((β_ _ X).inv ⊗ 𝟙 _) ≫ (α_ _ _ _).hom ≫ ((𝟙 (𝟙_ C)) ⊗ (ρ_ X).hom)
: by { slice_lhs 1 3 { rw ←hexagon_reverse }, simp only [assoc], }
... = (α_ _ _ _).inv ≫ (β_ _ _).hom ≫ (𝟙 X ⊗ (ρ_ _).hom) ≫ (β_ _ X).inv
: by rw braiding_right_unitor_aux₁
... = (α_ _ _ _).inv ≫ ((ρ_ _).hom ⊗ 𝟙 _) ≫ (β_ _ X).hom ≫ (β_ _ _).inv
: by { slice_lhs 2 3 { rw [←braiding_naturality] }, simp only [assoc], }
... = (α_ _ _ _).inv ≫ ((ρ_ _).hom ⊗ 𝟙 _)
: by rw [iso.hom_inv_id, comp_id]
... = (𝟙 (𝟙_ C)) ⊗ (λ_ X).hom
: by rw [triangle_assoc_comp_right]
@[simp]
lemma braiding_right_unitor (X : C) : (β_ (𝟙_ C) X).hom ≫ (ρ_ X).hom = (λ_ X).hom :=
by rw [←tensor_left_iff, id_tensor_comp, braiding_right_unitor_aux₂]
@[simp]
lemma left_unitor_inv_braiding (X : C) : (λ_ X).inv ≫ (β_ (𝟙_ C) X).hom = (ρ_ X).inv :=
begin
apply (cancel_mono (ρ_ X).hom).1,
simp only [assoc, braiding_right_unitor, iso.inv_hom_id],
end
@[simp]
lemma right_unitor_inv_braiding (X : C) : (ρ_ X).inv ≫ (β_ X (𝟙_ C)).hom = (λ_ X).inv :=
begin
apply (cancel_mono (λ_ X).hom).1,
simp only [assoc, braiding_left_unitor, iso.inv_hom_id],
end
end
/--
A symmetric monoidal category is a braided monoidal category for which the braiding is symmetric.
See <https://stacks.math.columbia.edu/tag/0FFW>.
-/
class symmetric_category (C : Type u) [category.{v} C] [monoidal_category.{v} C]
extends braided_category.{v} C :=
-- braiding symmetric:
(symmetry' : ∀ X Y : C, (β_ X Y).hom ≫ (β_ Y X).hom = 𝟙 (X ⊗ Y) . obviously)
restate_axiom symmetric_category.symmetry'
attribute [simp,reassoc] symmetric_category.symmetry
initialize_simps_projections symmetric_category
(to_braided_category_braiding → braiding, -to_braided_category)
variables (C : Type u₁) [category.{v₁} C] [monoidal_category C] [braided_category C]
variables (D : Type u₂) [category.{v₂} D] [monoidal_category D] [braided_category D]
variables (E : Type u₃) [category.{v₃} E] [monoidal_category E] [braided_category E]
/--
A lax braided functor between braided monoidal categories is a lax monoidal functor
which preserves the braiding.
-/
structure lax_braided_functor extends lax_monoidal_functor C D :=
(braided' : ∀ X Y : C, μ X Y ≫ map (β_ X Y).hom = (β_ (obj X) (obj Y)).hom ≫ μ Y X . obviously)
restate_axiom lax_braided_functor.braided'
namespace lax_braided_functor
/-- The identity lax braided monoidal functor. -/
@[simps] def id : lax_braided_functor C C :=
{ .. monoidal_functor.id C }
instance : inhabited (lax_braided_functor C C) := ⟨id C⟩
variables {C D E}
/-- The composition of lax braided monoidal functors. -/
@[simps]
def comp (F : lax_braided_functor C D) (G : lax_braided_functor D E) : lax_braided_functor C E :=
{ braided' := λ X Y,
begin
dsimp,
slice_lhs 2 3 { rw [←category_theory.functor.map_comp, F.braided,
category_theory.functor.map_comp], },
slice_lhs 1 2 { rw [G.braided], },
simp only [category.assoc],
end,
..(lax_monoidal_functor.comp F.to_lax_monoidal_functor G.to_lax_monoidal_functor) }
instance category_lax_braided_functor : category (lax_braided_functor C D) :=
induced_category.category lax_braided_functor.to_lax_monoidal_functor
@[simp] lemma comp_to_nat_trans {F G H : lax_braided_functor C D} {α : F ⟶ G} {β : G ⟶ H} :
(α ≫ β).to_nat_trans =
@category_struct.comp (C ⥤ D) _ _ _ _ (α.to_nat_trans) (β.to_nat_trans) := rfl
/--
Interpret a natural isomorphism of the underlyling lax monoidal functors as an
isomorphism of the lax braided monoidal functors.
-/
@[simps]
def mk_iso {F G : lax_braided_functor C D}
(i : F.to_lax_monoidal_functor ≅ G.to_lax_monoidal_functor) : F ≅ G :=
{ ..i }
end lax_braided_functor
/--
A braided functor between braided monoidal categories is a monoidal functor
which preserves the braiding.
-/
structure braided_functor extends monoidal_functor C D :=
-- Note this is stated differently than for `lax_braided_functor`.
-- We move the `μ X Y` to the right hand side,
-- so that this makes a good `@[simp]` lemma.
(braided' :
∀ X Y : C, map (β_ X Y).hom = inv (μ X Y) ≫ (β_ (obj X) (obj Y)).hom ≫ μ Y X . obviously)
restate_axiom braided_functor.braided'
attribute [simp] braided_functor.braided
/-- A braided category with a braided functor to a symmetric category is itself symmetric. -/
def symmetric_category_of_faithful {C D : Type*} [category C] [category D]
[monoidal_category C] [monoidal_category D] [braided_category C] [symmetric_category D]
(F : braided_functor C D) [faithful F.to_functor] : symmetric_category C :=
{ symmetry' := λ X Y, F.to_functor.map_injective (by simp), }
namespace braided_functor
/-- Turn a braided functor into a lax braided functor. -/
@[simps]
def to_lax_braided_functor (F : braided_functor C D) : lax_braided_functor C D :=
{ braided' := λ X Y, by { rw F.braided, simp, }
.. F }
/-- The identity braided monoidal functor. -/
@[simps] def id : braided_functor C C :=
{ .. monoidal_functor.id C }
instance : inhabited (braided_functor C C) := ⟨id C⟩
variables {C D E}
/-- The composition of braided monoidal functors. -/
@[simps]
def comp (F : braided_functor C D) (G : braided_functor D E) : braided_functor C E :=
{ ..(monoidal_functor.comp F.to_monoidal_functor G.to_monoidal_functor) }
instance category_braided_functor : category (braided_functor C D) :=
induced_category.category braided_functor.to_monoidal_functor
@[simp] lemma comp_to_nat_trans {F G H : braided_functor C D} {α : F ⟶ G} {β : G ⟶ H} :
(α ≫ β).to_nat_trans =
@category_struct.comp (C ⥤ D) _ _ _ _ (α.to_nat_trans) (β.to_nat_trans) := rfl
/--
Interpret a natural isomorphism of the underlyling monoidal functors as an
isomorphism of the braided monoidal functors.
-/
@[simps]
def mk_iso {F G : braided_functor C D}
(i : F.to_monoidal_functor ≅ G.to_monoidal_functor) : F ≅ G :=
{ ..i }
end braided_functor
section comm_monoid
variables (M : Type u) [comm_monoid M]
instance : braided_category (discrete M) :=
{ braiding := λ X Y, discrete.eq_to_iso (mul_comm X.as Y.as), }
variables {M} {N : Type u} [comm_monoid N]
/--
A multiplicative morphism between commutative monoids gives a braided functor between
the corresponding discrete braided monoidal categories.
-/
@[simps]
def discrete.braided_functor (F : M →* N) : braided_functor (discrete M) (discrete N) :=
{ ..discrete.monoidal_functor F }
end comm_monoid
section tensor
/-- The strength of the tensor product functor from `C × C` to `C`. -/
def tensor_μ (X Y : C × C) : (tensor C).obj X ⊗ (tensor C).obj Y ⟶ (tensor C).obj (X ⊗ Y) :=
(α_ X.1 X.2 (Y.1 ⊗ Y.2)).hom ≫ (𝟙 X.1 ⊗ (α_ X.2 Y.1 Y.2).inv) ≫
(𝟙 X.1 ⊗ ((β_ X.2 Y.1).hom ⊗ 𝟙 Y.2)) ≫
(𝟙 X.1 ⊗ (α_ Y.1 X.2 Y.2).hom) ≫ (α_ X.1 Y.1 (X.2 ⊗ Y.2)).inv
lemma tensor_μ_def₁ (X₁ X₂ Y₁ Y₂ : C) :
tensor_μ C (X₁, X₂) (Y₁, Y₂) ≫ (α_ X₁ Y₁ (X₂ ⊗ Y₂)).hom ≫ (𝟙 X₁ ⊗ (α_ Y₁ X₂ Y₂).inv)
= (α_ X₁ X₂ (Y₁ ⊗ Y₂)).hom ≫ (𝟙 X₁ ⊗ (α_ X₂ Y₁ Y₂).inv) ≫ (𝟙 X₁ ⊗ ((β_ X₂ Y₁).hom ⊗ 𝟙 Y₂)) :=
by { dsimp [tensor_μ], simp }
lemma tensor_μ_def₂ (X₁ X₂ Y₁ Y₂ : C) :
(𝟙 X₁ ⊗ (α_ X₂ Y₁ Y₂).hom) ≫ (α_ X₁ X₂ (Y₁ ⊗ Y₂)).inv ≫ tensor_μ C (X₁, X₂) (Y₁, Y₂)
= (𝟙 X₁ ⊗ ((β_ X₂ Y₁).hom ⊗ 𝟙 Y₂)) ≫ (𝟙 X₁ ⊗ (α_ Y₁ X₂ Y₂).hom) ≫ (α_ X₁ Y₁ (X₂ ⊗ Y₂)).inv :=
by { dsimp [tensor_μ], simp }
lemma tensor_μ_natural {X₁ X₂ Y₁ Y₂ U₁ U₂ V₁ V₂ : C}
(f₁ : X₁ ⟶ Y₁) (f₂ : X₂ ⟶ Y₂) (g₁ : U₁ ⟶ V₁) (g₂ : U₂ ⟶ V₂) :
((f₁ ⊗ f₂) ⊗ (g₁ ⊗ g₂)) ≫ tensor_μ C (Y₁, Y₂) (V₁, V₂) =
tensor_μ C (X₁, X₂) (U₁, U₂) ≫ ((f₁ ⊗ g₁) ⊗ (f₂ ⊗ g₂)) :=
begin
dsimp [tensor_μ],
slice_lhs 1 2 { rw [associator_naturality] },
slice_lhs 2 3 { rw [←tensor_comp,
comp_id f₁, ←id_comp f₁,
associator_inv_naturality,
tensor_comp] },
slice_lhs 3 4 { rw [←tensor_comp, ←tensor_comp,
comp_id f₁, ←id_comp f₁,
comp_id g₂, ←id_comp g₂,
braiding_naturality,
tensor_comp, tensor_comp] },
slice_lhs 4 5 { rw [←tensor_comp,
comp_id f₁, ←id_comp f₁,
associator_naturality,
tensor_comp] },
slice_lhs 5 6 { rw [associator_inv_naturality] },
simp only [assoc],
end
lemma tensor_left_unitality (X₁ X₂ : C) :
(λ_ (X₁ ⊗ X₂)).hom
= ((λ_ (𝟙_ C)).inv ⊗ 𝟙 (X₁ ⊗ X₂)) ≫
tensor_μ C (𝟙_ C, 𝟙_ C) (X₁, X₂) ≫
((λ_ X₁).hom ⊗ (λ_ X₂).hom) :=
begin
dsimp [tensor_μ],
have :
((λ_ (𝟙_ C)).inv ⊗ 𝟙 (X₁ ⊗ X₂)) ≫
(α_ (𝟙_ C) (𝟙_ C) (X₁ ⊗ X₂)).hom ≫
(𝟙 (𝟙_ C) ⊗ (α_ (𝟙_ C) X₁ X₂).inv)
= 𝟙 (𝟙_ C) ⊗ ((λ_ X₁).inv ⊗ 𝟙 X₂) := by pure_coherence,
slice_rhs 1 3 { rw this }, clear this,
slice_rhs 1 2 { rw [←tensor_comp, ←tensor_comp,
comp_id, comp_id,
left_unitor_inv_braiding] },
simp only [assoc],
coherence,
end
lemma tensor_right_unitality (X₁ X₂ : C) :
(ρ_ (X₁ ⊗ X₂)).hom
= (𝟙 (X₁ ⊗ X₂) ⊗ (λ_ (𝟙_ C)).inv) ≫
tensor_μ C (X₁, X₂) (𝟙_ C, 𝟙_ C) ≫
((ρ_ X₁).hom ⊗ (ρ_ X₂).hom) :=
begin
dsimp [tensor_μ],
have :
(𝟙 (X₁ ⊗ X₂) ⊗ (λ_ (𝟙_ C)).inv) ≫
(α_ X₁ X₂ (𝟙_ C ⊗ 𝟙_ C)).hom ≫
(𝟙 X₁ ⊗ (α_ X₂ (𝟙_ C) (𝟙_ C)).inv)
= (α_ X₁ X₂ (𝟙_ C)).hom ≫
(𝟙 X₁ ⊗ ((ρ_ X₂).inv ⊗ 𝟙 (𝟙_ C))) := by pure_coherence,
slice_rhs 1 3 { rw this }, clear this,
slice_rhs 2 3 { rw [←tensor_comp, ←tensor_comp,
comp_id, comp_id,
right_unitor_inv_braiding] },
simp only [assoc],
coherence,
end
/-
Diagram B6 from Proposition 1 of [Joyal and Street, *Braided monoidal categories*][Joyal_Street].
-/
lemma tensor_associativity_aux (W X Y Z : C) :
((β_ W X).hom ⊗ 𝟙 (Y ⊗ Z)) ≫
(α_ X W (Y ⊗ Z)).hom ≫
(𝟙 X ⊗ (α_ W Y Z).inv) ≫
(𝟙 X ⊗ (β_ (W ⊗ Y) Z).hom) ≫
(𝟙 X ⊗ (α_ Z W Y).inv)
= (𝟙 (W ⊗ X) ⊗ (β_ Y Z).hom) ≫
(α_ (W ⊗ X) Z Y).inv ≫
((α_ W X Z).hom ⊗ 𝟙 Y) ≫
((β_ W (X ⊗ Z)).hom ⊗ 𝟙 Y) ≫
((α_ X Z W).hom ⊗ 𝟙 Y) ≫
(α_ X (Z ⊗ W) Y).hom :=
begin
slice_rhs 3 5 { rw [←tensor_comp, ←tensor_comp,
hexagon_forward,
tensor_comp, tensor_comp] },
slice_rhs 5 6 { rw [associator_naturality] },
slice_rhs 2 3 { rw [←associator_inv_naturality] },
slice_rhs 3 5 { rw [←pentagon_hom_inv] },
slice_rhs 1 2 { rw [tensor_id,
id_tensor_comp_tensor_id,
←tensor_id_comp_id_tensor] },
slice_rhs 2 3 { rw [← tensor_id, associator_naturality] },
slice_rhs 3 5 { rw [←tensor_comp, ←tensor_comp,
←hexagon_reverse,
tensor_comp, tensor_comp] },
end
lemma tensor_associativity (X₁ X₂ Y₁ Y₂ Z₁ Z₂ : C) :
(tensor_μ C (X₁, X₂) (Y₁, Y₂) ⊗ 𝟙 (Z₁ ⊗ Z₂)) ≫
tensor_μ C (X₁ ⊗ Y₁, X₂ ⊗ Y₂) (Z₁, Z₂) ≫
((α_ X₁ Y₁ Z₁).hom ⊗ (α_ X₂ Y₂ Z₂).hom)
= (α_ (X₁ ⊗ X₂) (Y₁ ⊗ Y₂) (Z₁ ⊗ Z₂)).hom ≫
(𝟙 (X₁ ⊗ X₂) ⊗ tensor_μ C (Y₁, Y₂) (Z₁, Z₂)) ≫
tensor_μ C (X₁, X₂) (Y₁ ⊗ Z₁, Y₂ ⊗ Z₂) :=
begin
have :
((α_ X₁ Y₁ Z₁).hom ⊗ (α_ X₂ Y₂ Z₂).hom)
= (α_ (X₁ ⊗ Y₁) Z₁ ((X₂ ⊗ Y₂) ⊗ Z₂)).hom ≫
(𝟙 (X₁ ⊗ Y₁) ⊗ (α_ Z₁ (X₂ ⊗ Y₂) Z₂).inv) ≫
(α_ X₁ Y₁ ((Z₁ ⊗ (X₂ ⊗ Y₂)) ⊗ Z₂)).hom ≫
(𝟙 X₁ ⊗ (α_ Y₁ (Z₁ ⊗ (X₂ ⊗ Y₂)) Z₂).inv) ≫
(α_ X₁ (Y₁ ⊗ (Z₁ ⊗ (X₂ ⊗ Y₂))) Z₂).inv ≫
((𝟙 X₁ ⊗ (𝟙 Y₁ ⊗ (α_ Z₁ X₂ Y₂).inv)) ⊗ 𝟙 Z₂) ≫
((𝟙 X₁ ⊗ (α_ Y₁ (Z₁ ⊗ X₂) Y₂).inv) ⊗ 𝟙 Z₂) ≫
((𝟙 X₁ ⊗ ((α_ Y₁ Z₁ X₂).inv ⊗ 𝟙 Y₂)) ⊗ 𝟙 Z₂) ≫
(α_ X₁ (((Y₁ ⊗ Z₁) ⊗ X₂) ⊗ Y₂) Z₂).hom ≫
(𝟙 X₁ ⊗ (α_ ((Y₁ ⊗ Z₁) ⊗ X₂) Y₂ Z₂).hom) ≫
(𝟙 X₁ ⊗ (α_ (Y₁ ⊗ Z₁) X₂ (Y₂ ⊗ Z₂)).hom) ≫
(α_ X₁ (Y₁ ⊗ Z₁) (X₂ ⊗ (Y₂ ⊗ Z₂))).inv := by pure_coherence,
rw this, clear this,
slice_lhs 2 4 { rw [tensor_μ_def₁] },
slice_lhs 4 5 { rw [←tensor_id, associator_naturality] },
slice_lhs 5 6 { rw [←tensor_comp,
associator_inv_naturality,
tensor_comp] },
slice_lhs 6 7 { rw [associator_inv_naturality] },
have :
(α_ (X₁ ⊗ Y₁) (X₂ ⊗ Y₂) (Z₁ ⊗ Z₂)).hom ≫
(𝟙 (X₁ ⊗ Y₁) ⊗ (α_ (X₂ ⊗ Y₂) Z₁ Z₂).inv) ≫
(α_ X₁ Y₁ (((X₂ ⊗ Y₂) ⊗ Z₁) ⊗ Z₂)).hom ≫
(𝟙 X₁ ⊗ (α_ Y₁ ((X₂ ⊗ Y₂) ⊗ Z₁) Z₂).inv) ≫
(α_ X₁ (Y₁ ⊗ ((X₂ ⊗ Y₂) ⊗ Z₁)) Z₂).inv
= ((α_ X₁ Y₁ (X₂ ⊗ Y₂)).hom ⊗ 𝟙 (Z₁ ⊗ Z₂)) ≫
((𝟙 X₁ ⊗ (α_ Y₁ X₂ Y₂).inv) ⊗ 𝟙 (Z₁ ⊗ Z₂)) ≫
(α_ (X₁ ⊗ ((Y₁ ⊗ X₂) ⊗ Y₂)) Z₁ Z₂).inv ≫
((α_ X₁ ((Y₁ ⊗ X₂) ⊗ Y₂) Z₁).hom ⊗ 𝟙 Z₂) ≫
((𝟙 X₁ ⊗ (α_ (Y₁ ⊗ X₂) Y₂ Z₁).hom) ⊗ 𝟙 Z₂) ≫
((𝟙 X₁ ⊗ (α_ Y₁ X₂ (Y₂ ⊗ Z₁)).hom) ⊗ 𝟙 Z₂) ≫
((𝟙 X₁ ⊗ (𝟙 Y₁ ⊗ (α_ X₂ Y₂ Z₁).inv)) ⊗ 𝟙 Z₂) := by pure_coherence,
slice_lhs 2 6 { rw this }, clear this,
slice_lhs 1 3 { rw [←tensor_comp, ←tensor_comp,
tensor_μ_def₁,
tensor_comp, tensor_comp] },
slice_lhs 3 4 { rw [←tensor_id,
associator_inv_naturality] },
slice_lhs 4 5 { rw [←tensor_comp,
associator_naturality,
tensor_comp] },
slice_lhs 5 6 { rw [←tensor_comp, ←tensor_comp,
associator_naturality,
tensor_comp, tensor_comp] },
slice_lhs 6 10 { rw [←tensor_comp, ←tensor_comp, ←tensor_comp, ←tensor_comp,
←tensor_comp, ←tensor_comp, ←tensor_comp, ←tensor_comp,
tensor_id,
tensor_associativity_aux,
←tensor_id,
←id_comp (𝟙 X₁ ≫ 𝟙 X₁ ≫ 𝟙 X₁ ≫ 𝟙 X₁ ≫ 𝟙 X₁),
←id_comp (𝟙 Z₂ ≫ 𝟙 Z₂ ≫ 𝟙 Z₂ ≫ 𝟙 Z₂ ≫ 𝟙 Z₂),
tensor_comp, tensor_comp, tensor_comp, tensor_comp, tensor_comp,
tensor_comp, tensor_comp, tensor_comp, tensor_comp, tensor_comp] },
slice_lhs 11 12 { rw [←tensor_comp, ←tensor_comp,
iso.hom_inv_id],
simp },
simp only [assoc, id_comp],
slice_lhs 10 11 { rw [←tensor_comp, ←tensor_comp, ←tensor_comp,
iso.hom_inv_id],
simp },
simp only [assoc, id_comp],
slice_lhs 9 10 { rw [associator_naturality] },
slice_lhs 10 11 { rw [←tensor_comp,
associator_naturality,
tensor_comp] },
slice_lhs 11 13 { rw [tensor_id, ←tensor_μ_def₂] },
have :
((𝟙 X₁ ⊗ (α_ (X₂ ⊗ Y₁) Z₁ Y₂).inv) ⊗ 𝟙 Z₂) ≫
((𝟙 X₁ ⊗ (α_ X₂ Y₁ Z₁).hom ⊗ 𝟙 Y₂) ⊗ 𝟙 Z₂) ≫
(α_ X₁ ((X₂ ⊗ Y₁ ⊗ Z₁) ⊗ Y₂) Z₂).hom ≫
(𝟙 X₁ ⊗ (α_ (X₂ ⊗ Y₁ ⊗ Z₁) Y₂ Z₂).hom) ≫
(𝟙 X₁ ⊗ (α_ X₂ (Y₁ ⊗ Z₁) (Y₂ ⊗ Z₂)).hom) ≫
(α_ X₁ X₂ ((Y₁ ⊗ Z₁) ⊗ Y₂ ⊗ Z₂)).inv
= (α_ X₁ ((X₂ ⊗ Y₁) ⊗ (Z₁ ⊗ Y₂)) Z₂).hom ≫
(𝟙 X₁ ⊗ (α_ (X₂ ⊗ Y₁) (Z₁ ⊗ Y₂) Z₂).hom) ≫
(𝟙 X₁ ⊗ (α_ X₂ Y₁ ((Z₁ ⊗ Y₂) ⊗ Z₂)).hom) ≫
(α_ X₁ X₂ (Y₁ ⊗ ((Z₁ ⊗ Y₂) ⊗ Z₂))).inv ≫
(𝟙 (X₁ ⊗ X₂) ⊗ (𝟙 Y₁ ⊗ (α_ Z₁ Y₂ Z₂).hom)) ≫
(𝟙 (X₁ ⊗ X₂) ⊗ (α_ Y₁ Z₁ (Y₂ ⊗ Z₂)).inv) := by pure_coherence,
slice_lhs 7 12 { rw this }, clear this,
slice_lhs 6 7 { rw [associator_naturality] },
slice_lhs 7 8 { rw [←tensor_comp,
associator_naturality,
tensor_comp] },
slice_lhs 8 9 { rw [←tensor_comp,
associator_naturality,
tensor_comp] },
slice_lhs 9 10 { rw [associator_inv_naturality] },
slice_lhs 10 12 { rw [←tensor_comp, ←tensor_comp,
←tensor_μ_def₂,
tensor_comp, tensor_comp] },
dsimp,
coherence,
end
/-- The tensor product functor from `C × C` to `C` as a monoidal functor. -/
@[simps]
def tensor_monoidal : monoidal_functor (C × C) C :=
{ ε := (λ_ (𝟙_ C)).inv,
μ := λ X Y, tensor_μ C X Y,
μ_natural' := λ X Y X' Y' f g, tensor_μ_natural C f.1 f.2 g.1 g.2,
associativity' := λ X Y Z, tensor_associativity C X.1 X.2 Y.1 Y.2 Z.1 Z.2,
left_unitality' := λ ⟨X₁, X₂⟩, tensor_left_unitality C X₁ X₂,
right_unitality' := λ ⟨X₁, X₂⟩, tensor_right_unitality C X₁ X₂,
μ_is_iso := by { dsimp [tensor_μ], apply_instance },
.. tensor C }
lemma left_unitor_monoidal (X₁ X₂ : C) :
(λ_ X₁).hom ⊗ (λ_ X₂).hom
= tensor_μ C (𝟙_ C, X₁) (𝟙_ C, X₂) ≫
((λ_ (𝟙_ C)).hom ⊗ 𝟙 (X₁ ⊗ X₂)) ≫
(λ_ (X₁ ⊗ X₂)).hom :=
begin
dsimp [tensor_μ],
have :
(λ_ X₁).hom ⊗ (λ_ X₂).hom
= (α_ (𝟙_ C) X₁ (𝟙_ C ⊗ X₂)).hom ≫
(𝟙 (𝟙_ C) ⊗ (α_ X₁ (𝟙_ C) X₂).inv) ≫
(λ_ ((X₁ ⊗ (𝟙_ C)) ⊗ X₂)).hom ≫
((ρ_ X₁).hom ⊗ (𝟙 X₂)) := by pure_coherence,
rw this, clear this,
rw ←braiding_left_unitor,
slice_lhs 3 4 { rw [←id_comp (𝟙 X₂), tensor_comp] },
slice_lhs 3 4 { rw [←left_unitor_naturality] },
coherence,
end
lemma right_unitor_monoidal (X₁ X₂ : C) :
(ρ_ X₁).hom ⊗ (ρ_ X₂).hom
= tensor_μ C (X₁, 𝟙_ C) (X₂, 𝟙_ C) ≫
(𝟙 (X₁ ⊗ X₂) ⊗ (λ_ (𝟙_ C)).hom) ≫
(ρ_ (X₁ ⊗ X₂)).hom :=
begin
dsimp [tensor_μ],
have :
(ρ_ X₁).hom ⊗ (ρ_ X₂).hom
= (α_ X₁ (𝟙_ C) (X₂ ⊗ (𝟙_ C))).hom ≫
(𝟙 X₁ ⊗ (α_ (𝟙_ C) X₂ (𝟙_ C)).inv) ≫
(𝟙 X₁ ⊗ (ρ_ (𝟙_ C ⊗ X₂)).hom) ≫
(𝟙 X₁ ⊗ (λ_ X₂).hom) := by pure_coherence,
rw this, clear this,
rw ←braiding_right_unitor,
slice_lhs 3 4 { rw [←id_comp (𝟙 X₁), tensor_comp, id_comp] },
slice_lhs 3 4 { rw [←tensor_comp,
←right_unitor_naturality,
tensor_comp] },
coherence,
end
lemma associator_monoidal_aux (W X Y Z : C) :
(𝟙 W ⊗ (β_ X (Y ⊗ Z)).hom) ≫
(𝟙 W ⊗ (α_ Y Z X).hom) ≫
(α_ W Y (Z ⊗ X)).inv ≫
((β_ W Y).hom ⊗ 𝟙 (Z ⊗ X))
= (α_ W X (Y ⊗ Z)).inv ≫
(α_ (W ⊗ X) Y Z).inv ≫
((β_ (W ⊗ X) Y).hom ⊗ 𝟙 Z) ≫
((α_ Y W X).inv ⊗ 𝟙 Z) ≫
(α_ (Y ⊗ W) X Z).hom ≫
(𝟙 (Y ⊗ W) ⊗ (β_ X Z).hom) :=
begin
slice_rhs 1 2 { rw ←pentagon_inv },
slice_rhs 3 5 { rw [←tensor_comp, ←tensor_comp,
hexagon_reverse,
tensor_comp, tensor_comp] },
slice_rhs 5 6 { rw associator_naturality },
slice_rhs 6 7 { rw [tensor_id,
tensor_id_comp_id_tensor,
←id_tensor_comp_tensor_id] },
slice_rhs 2 3 { rw ←associator_inv_naturality },
slice_rhs 3 5 { rw pentagon_inv_inv_hom },
slice_rhs 4 5 { rw [←tensor_id,
←associator_inv_naturality] },
slice_rhs 2 4 { rw [←tensor_comp, ←tensor_comp,
←hexagon_forward,
tensor_comp, tensor_comp] },
simp,
end
lemma associator_monoidal (X₁ X₂ X₃ Y₁ Y₂ Y₃ : C) :
tensor_μ C (X₁ ⊗ X₂, X₃) (Y₁ ⊗ Y₂, Y₃) ≫
(tensor_μ C (X₁, X₂) (Y₁, Y₂) ⊗ 𝟙 (X₃ ⊗ Y₃)) ≫
(α_ (X₁ ⊗ Y₁) (X₂ ⊗ Y₂) (X₃ ⊗ Y₃)).hom
= ((α_ X₁ X₂ X₃).hom ⊗ (α_ Y₁ Y₂ Y₃).hom) ≫
tensor_μ C (X₁, X₂ ⊗ X₃) (Y₁, Y₂ ⊗ Y₃) ≫
(𝟙 (X₁ ⊗ Y₁) ⊗ tensor_μ C (X₂, X₃) (Y₂, Y₃)) :=
begin
have :
(α_ (X₁ ⊗ Y₁) (X₂ ⊗ Y₂) (X₃ ⊗ Y₃)).hom
= ((α_ X₁ Y₁ (X₂ ⊗ Y₂)).hom ⊗ 𝟙 (X₃ ⊗ Y₃)) ≫
((𝟙 X₁ ⊗ (α_ Y₁ X₂ Y₂).inv) ⊗ 𝟙 (X₃ ⊗ Y₃)) ≫
(α_ (X₁ ⊗ ((Y₁ ⊗ X₂) ⊗ Y₂)) X₃ Y₃).inv ≫
((α_ X₁ ((Y₁ ⊗ X₂) ⊗ Y₂) X₃).hom ⊗ 𝟙 Y₃) ≫
((𝟙 X₁ ⊗ (α_ (Y₁ ⊗ X₂) Y₂ X₃).hom) ⊗ 𝟙 Y₃) ≫
(α_ X₁ ((Y₁ ⊗ X₂) ⊗ (Y₂ ⊗ X₃)) Y₃).hom ≫
(𝟙 X₁ ⊗ (α_ (Y₁ ⊗ X₂) (Y₂ ⊗ X₃) Y₃).hom) ≫
(𝟙 X₁ ⊗ (α_ Y₁ X₂ ((Y₂ ⊗ X₃) ⊗ Y₃)).hom) ≫
(α_ X₁ Y₁ (X₂ ⊗ ((Y₂ ⊗ X₃) ⊗ Y₃))).inv ≫
(𝟙 (X₁ ⊗ Y₁) ⊗ (𝟙 X₂ ⊗ (α_ Y₂ X₃ Y₃).hom)) ≫
(𝟙 (X₁ ⊗ Y₁) ⊗ (α_ X₂ Y₂ (X₃ ⊗ Y₃)).inv) := by pure_coherence,
rw this, clear this,
slice_lhs 2 4 { rw [←tensor_comp, ←tensor_comp,
tensor_μ_def₁,
tensor_comp, tensor_comp] },
slice_lhs 4 5 { rw [←tensor_id,
associator_inv_naturality] },
slice_lhs 5 6 { rw [←tensor_comp,
associator_naturality,
tensor_comp] },
slice_lhs 6 7 { rw [←tensor_comp, ←tensor_comp,
associator_naturality,
tensor_comp, tensor_comp] },
have :
((α_ X₁ X₂ (Y₁ ⊗ Y₂)).hom ⊗ 𝟙 (X₃ ⊗ Y₃)) ≫
((𝟙 X₁ ⊗ (α_ X₂ Y₁ Y₂).inv) ⊗ 𝟙 (X₃ ⊗ Y₃)) ≫
(α_ (X₁ ⊗ ((X₂ ⊗ Y₁) ⊗ Y₂)) X₃ Y₃).inv ≫
((α_ X₁ ((X₂ ⊗ Y₁) ⊗ Y₂) X₃).hom ⊗ 𝟙 Y₃) ≫
((𝟙 X₁ ⊗ (α_ (X₂ ⊗ Y₁) Y₂ X₃).hom) ⊗ 𝟙 Y₃)
= (α_ (X₁ ⊗ X₂) (Y₁ ⊗ Y₂) (X₃ ⊗ Y₃)).hom ≫
(𝟙 (X₁ ⊗ X₂) ⊗ (α_ (Y₁ ⊗ Y₂) X₃ Y₃).inv) ≫
(α_ X₁ X₂ (((Y₁ ⊗ Y₂) ⊗ X₃) ⊗ Y₃)).hom ≫
(𝟙 X₁ ⊗ (α_ X₂ ((Y₁ ⊗ Y₂) ⊗ X₃) Y₃).inv) ≫
(α_ X₁ (X₂ ⊗ ((Y₁ ⊗ Y₂) ⊗ X₃)) Y₃).inv ≫
((𝟙 X₁ ⊗ (𝟙 X₂ ⊗ (α_ Y₁ Y₂ X₃).hom)) ⊗ 𝟙 Y₃) ≫
((𝟙 X₁ ⊗ (α_ X₂ Y₁ (Y₂ ⊗ X₃)).inv) ⊗ 𝟙 Y₃) := by pure_coherence,
slice_lhs 2 6 { rw this }, clear this,
slice_lhs 1 3 { rw tensor_μ_def₁ },
slice_lhs 3 4 { rw [←tensor_id,
associator_naturality] },
slice_lhs 4 5 { rw [←tensor_comp,
associator_inv_naturality,
tensor_comp] },
slice_lhs 5 6 { rw associator_inv_naturality },
slice_lhs 6 9 { rw [←tensor_comp, ←tensor_comp, ←tensor_comp,
←tensor_comp, ←tensor_comp, ←tensor_comp,
tensor_id,
associator_monoidal_aux,
←id_comp (𝟙 X₁ ≫ 𝟙 X₁ ≫ 𝟙 X₁ ≫ 𝟙 X₁),
←id_comp (𝟙 X₁ ≫ 𝟙 X₁ ≫ 𝟙 X₁ ≫ 𝟙 X₁ ≫ 𝟙 X₁),
←id_comp (𝟙 Y₃ ≫ 𝟙 Y₃ ≫ 𝟙 Y₃ ≫ 𝟙 Y₃),
←id_comp (𝟙 Y₃ ≫ 𝟙 Y₃ ≫ 𝟙 Y₃ ≫ 𝟙 Y₃ ≫ 𝟙 Y₃),
tensor_comp, tensor_comp, tensor_comp, tensor_comp, tensor_comp,
tensor_comp, tensor_comp, tensor_comp, tensor_comp, tensor_comp] },
slice_lhs 11 12 { rw associator_naturality },
slice_lhs 12 13 { rw [←tensor_comp,
associator_naturality,
tensor_comp] },
slice_lhs 13 14 { rw [←tensor_comp, ←tensor_id,
associator_naturality,
tensor_comp] },
slice_lhs 14 15 { rw associator_inv_naturality },
slice_lhs 15 17 { rw [tensor_id, ←tensor_comp, ←tensor_comp,
←tensor_μ_def₂,
tensor_comp, tensor_comp] },
have :
((𝟙 X₁ ⊗ ((α_ Y₁ X₂ X₃).inv ⊗ 𝟙 Y₂)) ⊗ 𝟙 Y₃) ≫
((𝟙 X₁ ⊗ (α_ (Y₁ ⊗ X₂) X₃ Y₂).hom) ⊗ 𝟙 Y₃) ≫
(α_ X₁ ((Y₁ ⊗ X₂) ⊗ (X₃ ⊗ Y₂)) Y₃).hom ≫
(𝟙 X₁ ⊗ (α_ (Y₁ ⊗ X₂) (X₃ ⊗ Y₂) Y₃).hom) ≫
(𝟙 X₁ ⊗ (α_ Y₁ X₂ ((X₃ ⊗ Y₂) ⊗ Y₃)).hom) ≫
(α_ X₁ Y₁ (X₂ ⊗ ((X₃ ⊗ Y₂) ⊗ Y₃))).inv ≫
(𝟙 (X₁ ⊗ Y₁) ⊗ (𝟙 X₂ ⊗ (α_ X₃ Y₂ Y₃).hom)) ≫
(𝟙 (X₁ ⊗ Y₁) ⊗ (α_ X₂ X₃ (Y₂ ⊗ Y₃)).inv)
= (α_ X₁ ((Y₁ ⊗ (X₂ ⊗ X₃)) ⊗ Y₂) Y₃).hom ≫
(𝟙 X₁ ⊗ (α_ (Y₁ ⊗ (X₂ ⊗ X₃)) Y₂ Y₃).hom) ≫
(𝟙 X₁ ⊗ (α_ Y₁ (X₂ ⊗ X₃) (Y₂ ⊗ Y₃)).hom) ≫
(α_ X₁ Y₁ ((X₂ ⊗ X₃) ⊗ (Y₂ ⊗ Y₃))).inv := by pure_coherence,
slice_lhs 9 16 { rw this }, clear this,
slice_lhs 8 9 { rw associator_naturality },
slice_lhs 9 10 { rw [←tensor_comp,
associator_naturality,
tensor_comp] },
slice_lhs 10 12 { rw [tensor_id,
←tensor_μ_def₂] },
dsimp,
coherence,
end
end tensor
end category_theory
|
719d81888b33b589bc5e118d888c68d745b97421 | b29f946a2f0afd23ef86b9219116968babbb9f4f | /src/finite_sums.lean | 5814119cc823926943e4a9594a71aca63a9faeab | [
"Apache-2.0"
] | permissive | ImperialCollegeLondon/M1P1-lean | 58be7394fded719d95e45e6b10e1ecf2ed3c7c4c | 3723468cc50f8bebd00a9811caf25224a578de17 | refs/heads/master | 1,587,063,867,779 | 1,572,727,164,000 | 1,572,727,164,000 | 165,845,802 | 14 | 4 | Apache-2.0 | 1,549,730,698,000 | 1,547,554,675,000 | Lean | UTF-8 | Lean | false | false | 886 | lean | import data.fintype -- for finset and finset.sum
import data.real.basic
universe u
variables {R : Type u} [add_comm_monoid R]
-- my_sum_to_n eats a summand f(n) and returns the function sending t to f(0)+f(1)+...+f(t-1).
-- Chris says use finsets.
definition sum_to_n_minus_one (summand : ℕ → R) : ℕ → R :=
λ n, (finset.range n).sum summand
--#reduce sum_to_n_minus_one (λ n, n ^ 2) 1000000
#check finset.range 4 -- finset ℕ
#check @finset.sum
definition sum_from_a_to_b_minus_one (summand : ℕ → R) (a b : ℕ) : R :=
(finset.Ico a b).sum summand
#eval sum_from_a_to_b_minus_one (id) 4 7
theorem finset.polygon {α : Type*} [decidable_eq α] (X : finset α) (summand : α → ℝ) :
abs (X.sum summand) ≤ X.sum (λ a, abs (summand a)) :=
begin
apply finset.induction_on X,
{ simp },
{ intro a,
intro s,
intro hs,
intro ih,
sorry
}
end |
037982b0d3cf33b3ea41d55cf49b3327f90cd512 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/algebra/order/invertible.lean | afdc5e4dd870d9d845d4fa37222edb4dc07f8007 | [
"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 | 1,246 | lean | /-
Copyright (c) 2021 Yury G. Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury G. Kudryashov
-/
import algebra.order.ring
import algebra.invertible
/-!
# Lemmas about `inv_of` in ordered (semi)rings.
-/
variables {α : Type*} [linear_ordered_semiring α] {a : α}
@[simp] lemma inv_of_pos [invertible a] : 0 < ⅟a ↔ 0 < a :=
begin
have : 0 < a * ⅟a, by simp only [mul_inv_of_self, zero_lt_one],
exact ⟨λ h, pos_of_mul_pos_right this h.le, λ h, pos_of_mul_pos_left this h.le⟩
end
@[simp] lemma inv_of_nonpos [invertible a] : ⅟a ≤ 0 ↔ a ≤ 0 :=
by simp only [← not_lt, inv_of_pos]
@[simp] lemma inv_of_nonneg [invertible a] : 0 ≤ ⅟a ↔ 0 ≤ a :=
begin
have : 0 < a * ⅟a, by simp only [mul_inv_of_self, zero_lt_one],
exact ⟨λ h, (pos_of_mul_pos_right this h).le, λ h, (pos_of_mul_pos_left this h).le⟩
end
@[simp] lemma inv_of_lt_zero [invertible a] : ⅟a < 0 ↔ a < 0 :=
by simp only [← not_le, inv_of_nonneg]
@[simp] lemma inv_of_le_one [invertible a] (h : 1 ≤ a) : ⅟a ≤ 1 :=
by haveI := @linear_order.decidable_le α _; exact
mul_inv_of_self a ▸ decidable.le_mul_of_one_le_left (inv_of_nonneg.2 $ zero_le_one.trans h) h
|
37dd50451215bea63621658f9353b802705a2799 | 9028d228ac200bbefe3a711342514dd4e4458bff | /src/category_theory/limits/shapes/types.lean | e76ab84c889f4e787ae6bc95f8b0a0af60fbfd77 | [
"Apache-2.0"
] | permissive | mcncm/mathlib | 8d25099344d9d2bee62822cb9ed43aa3e09fa05e | fde3d78cadeec5ef827b16ae55664ef115e66f57 | refs/heads/master | 1,672,743,316,277 | 1,602,618,514,000 | 1,602,618,514,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 4,257 | lean | /-
Copyright (c) 2020 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import category_theory.limits.types
import category_theory.limits.shapes.products
import category_theory.limits.shapes.binary_products
import category_theory.limits.shapes.terminal
/-!
# Special shapes for limits in `Type`.
The general shape (co)limits defined in `category_theory.limits.types`
are intended for use through the limits API,
and the actual implementation should mostly be considered "sealed".
In this file, we provide definitions of the "standard" special shapes of limits in `Type`,
giving the expected definitional implementation:
* the terminal object is `punit`
* the binary product of `X` and `Y` is `X × Y`
* the product of a family `f : J → Type` is `Π j, f j`.
Because these are not intended for use with the `has_limit` API,
we instead construct terms of `limit_data`.
As an example, when setting up the monoidal category structure on `Type`
we use the `types_has_terminal` and `types_has_binary_products` instances.
-/
universes u
open category_theory
open category_theory.limits
namespace category_theory.limits.types
/-- A restatement of `types.lift_π_apply` that uses `pi.π` and `pi.lift`. -/
@[simp]
lemma pi_lift_π_apply {β : Type u} (f : β → Type u) {P : Type u} (s : Π b, P ⟶ f b) (b : β) (x : P) :
(pi.π f b : (∏ f) → f b) (@pi.lift β _ _ f _ P s x) = s b x :=
congr_fun (limit.lift_π (fan.mk s) b) x
/-- A restatement of `types.map_π_apply` that uses `pi.π` and `pi.map`. -/
@[simp]
lemma pi_map_π_apply {β : Type u} {f g : β → Type u} (α : Π j, f j ⟶ g j) (b : β) (x) :
(pi.π g b : (∏ g) → g b) (pi.map α x) = α b ((pi.π f b : (∏ f) → f b) x) :=
limit.map_π_apply _ _ _
/-- The category of types has `punit` as a terminal object. -/
def terminal_limit_cone : limits.limit_cone (functor.empty (Type u)) :=
{ cone :=
{ X := punit,
π := by tidy, },
is_limit := by tidy, }
/-- The category of types has `pempty` as an initial object. -/
def initial_limit_cone : limits.colimit_cocone (functor.empty (Type u)) :=
{ cocone :=
{ X := pempty,
ι := by tidy, },
is_colimit := by tidy, }
open category_theory.limits.walking_pair
local attribute [tidy] tactic.case_bash
/--
The category of types has `X × Y`, the usual cartesian product,
as the binary product of `X` and `Y`.
-/
def binary_product_limit_cone (X Y : Type u) : limits.limit_cone (pair X Y) :=
{ cone :=
{ X := X × Y,
π :=
{ app := by { rintro ⟨_|_⟩, exact prod.fst, exact prod.snd, } }, },
is_limit :=
{ lift := λ s x, (s.π.app left x, s.π.app right x),
uniq' := λ s m w,
begin
ext,
exact congr_fun (w left) x,
exact congr_fun (w right) x,
end }, }
/--
The category of types has `X ⊕ Y`,
as the binary coproduct of `X` and `Y`.
-/
def binary_coproduct_limit_cone (X Y : Type u) : limits.colimit_cocone (pair X Y) :=
{ cocone :=
{ X := X ⊕ Y,
ι :=
{ app := by { rintro ⟨_|_⟩, exact sum.inl, exact sum.inr, } }, },
is_colimit :=
{ desc := λ s x, sum.elim (s.ι.app left) (s.ι.app right) x,
uniq' := λ s m w,
begin
ext (x|x),
exact (congr_fun (w left) x : _),
exact (congr_fun (w right) x : _),
end }, }
/--
The category of types has `Π j, f j` as the product of a type family `f : J → Type`.
-/
def product_limit_cone {J : Type u} (F : J → Type u) : limits.limit_cone (discrete.functor F) :=
{ cone :=
{ X := Π j, F j,
π :=
{ app := λ j f, f j }, },
is_limit :=
{ lift := λ s x j, s.π.app j x,
uniq' := λ s m w,
begin
ext x j,
have := congr_fun (w j) x,
exact this,
end }, }
/--
The category of types has `Σ j, f j` as the coproduct of a type family `f : J → Type`.
-/
def coproduct_limit_cone {J : Type u} (F : J → Type u) : limits.colimit_cocone (discrete.functor F) :=
{ cocone :=
{ X := Σ j, F j,
ι :=
{ app := λ j x, ⟨j, x⟩ }, },
is_colimit :=
{ desc := λ s x, s.ι.app x.1 x.2,
uniq' := λ s m w,
begin
ext ⟨j, x⟩,
have := congr_fun (w j) x,
exact this,
end }, }
end category_theory.limits.types
|
cdeb22d34ed9562ce42dfbaa9514f2f1b8a9a848 | 367134ba5a65885e863bdc4507601606690974c1 | /src/tactic/generalizes.lean | ad5f9c44f318902eeeb5a209dcdfe78b89874cf6 | [
"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 | 8,912 | lean | /-
Copyright (c) 2020 Jannis Limperg. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Jannis Limperg
-/
import tactic.core
/-!
# The `generalizes` tactic
This module defines the `tactic.generalizes'` tactic and its interactive version
`tactic.interactive.generalizes`. These work like `generalize`, but they can
generalize over multiple expressions at once. This is particularly handy when
there are dependencies between the expressions, in which case `generalize` will
usually fail but `generalizes` may succeed.
## Implementation notes
To generalize the target `T` over expressions `j₁ : J₁, ..., jₙ : Jₙ`, we first
create the new target type
T' = ∀ (k₁ : J₁) ... (kₙ : Jₙ) (k₁_eq : k₁ = j₁) ... (kₙ_eq : kₙ == jₙ), U
where `U` is `T` with any occurrences of the `jᵢ` replaced by the corresponding
`kᵢ`. Note that some of the `kᵢ_eq` may be heterogeneous; this happens when
there are dependencies between the `jᵢ`. The construction of `T'` is performed
by `generalizes.step1` and `generalizes.step2`.
Having constructed `T'`, we can `assert` it and use it to construct a proof of
the original target by instantiating the binders with
j₁ ... jₙ (eq.refl j₁) ... (heq.refl jₙ).
This leaves us with a generalized goal. This construction is performed by
`generalizes.step3`.
-/
universes u v w
namespace tactic
open expr
namespace generalizes
/--
Input:
- Target expression `e`.
- List of expressions `jᵢ` to be generalised, along with a name for the local
const that will replace them. The `jᵢ` must be in dependency order:
`[n, fin n]` is okay but `[fin n, n]` is not.
Output:
- List of new local constants `kᵢ`, one for each `jᵢ`.
- `e` with the `jᵢ` replaced by the `kᵢ`, i.e. `e[jᵢ := kᵢ]...[j₀ := k₀]`.
Note that the substitution also affects the types of the `kᵢ`: If `jᵢ : Jᵢ` then
`kᵢ : Jᵢ[jᵢ₋₁ := kᵢ₋₁]...[j₀ := k₀]`.
The transparency `md` and the boolean `unify` are passed to `kabstract` when we
abstract over occurrences of the `jᵢ` in `e`.
-/
meta def step1 (md : transparency) (unify : bool)
(e : expr) (to_generalize : list (name × expr)) : tactic (expr × list expr) := do
let go : name × expr → expr × list expr → tactic (expr × list expr) :=
λ ⟨n, j⟩ ⟨e, ks⟩, do {
J ← infer_type j,
k ← mk_local' n binder_info.default J,
e ← kreplace e j k md unify,
ks ← ks.mmap $ λ k', kreplace k' j k md unify,
pure (e, k :: ks)
},
to_generalize.mfoldr go (e, [])
/--
Input: for each equation that should be generated: the equation name, the
argument `jᵢ` and the corresponding local constant `kᵢ` from step 1.
Output: for each element of the input list a new local constant of type
`jᵢ = kᵢ` or `jᵢ == kᵢ` and a proof of `jᵢ = jᵢ` or `jᵢ == jᵢ`.
The transparency `md` is used when determining whether the type of `jᵢ` is defeq
to the type of `kᵢ` (and thus whether to generate a homogeneous or heterogeneous
equation).
-/
meta def step2 (md : transparency)
(to_generalize : list (name × expr × expr))
: tactic (list (expr × expr)) :=
to_generalize.mmap $ λ ⟨n, j, k⟩, do
J ← infer_type j,
K ← infer_type k,
sort u ← infer_type K |
fail! "generalizes'/step2: expected the type of {K} to be a sort",
homogeneous ← succeeds $ is_def_eq J K md,
let ⟨eq_type, eq_proof⟩ :=
if homogeneous
then ((const `eq [u]) K k j , (const `eq.refl [u]) J j)
else ((const `heq [u]) K k J j, (const `heq.refl [u]) J j),
eq ← mk_local' n binder_info.default eq_type,
pure (eq, eq_proof)
/--
Input: The `jᵢ`; the local constants `kᵢ` from step 1; the equations and their
proofs from step 2.
This step is the first one that changes the goal (and also the last one
overall). It asserts the generalized goal, then derives the current goal from
it. This leaves us with the generalized goal.
-/
meta def step3 (e : expr) (js ks eqs eq_proofs : list expr)
: tactic unit :=
focus1 $ do
let new_target_type := (e.pis eqs).pis ks,
type_check new_target_type <|> fail!
"generalizes': unable to generalize the target because the generalized target type does not type check:\n{new_target_type}",
n ← mk_fresh_name,
new_target ← assert n new_target_type,
swap,
let target_proof := new_target.mk_app $ js ++ eq_proofs,
exact target_proof
end generalizes
open generalizes
/--
Generalizes the target over each of the expressions in `args`. Given
`args = [(a₁, h₁, arg₁), ...]`, this changes the target to
∀ (a₁ : T₁) ... (h₁ : a₁ = arg₁) ..., U
where `U` is the current target with every occurrence of `argᵢ` replaced by
`aᵢ`. A similar effect can be achieved by using `generalize` once for each of
the `args`, but if there are dependencies between the `args`, this may fail to
perform some generalizations.
The replacement is performed using keyed matching/unification with transparency
`md`. `unify` determines whether matching or unification is used. See
`kabstract`.
The `args` must be given in dependency order, so `[n, fin n]` is okay but
`[fin n, n]` will result in an error.
After generalizing the `args`, the target type may no longer type check.
`generalizes'` will then raise an error.
-/
meta def generalizes' (args : list (name × option name × expr))
(md := semireducible) (unify := tt) : tactic unit := do
tgt ← target,
let stage1_args := args.map $ λ ⟨n, _, j⟩, (n, j),
⟨e, ks⟩ ← step1 md unify tgt stage1_args,
let stage2_args : list (option (name × expr × expr)) :=
args.map₂ (λ ⟨_, eq_name, j⟩ k, eq_name.map $ λ eq_name, (eq_name, j, k)) ks,
let stage2_args := stage2_args.reduce_option,
eqs_and_proofs ← step2 md stage2_args,
let eqs := eqs_and_proofs.map prod.fst,
let eq_proofs := eqs_and_proofs.map prod.snd,
let js := args.map (prod.snd ∘ prod.snd),
step3 e js ks eqs eq_proofs
/--
Like `generalizes'`, but also introduces the generalized constants and their
associated equations into the context.
-/
meta def generalizes_intro (args : list (name × option name × expr))
(md := semireducible) (unify := tt) : tactic (list expr × list expr) := do
generalizes' args md unify,
ks ← intron' args.length,
eqs ← intron' $ args.countp $ λ x, x.snd.fst.is_some,
pure (ks, eqs)
namespace interactive
open interactive
open lean.parser
private meta def generalizes_arg_parser_eq : pexpr → lean.parser (pexpr × name)
| (app (app (macro _ [const `eq _ ]) e) (local_const x _ _ _)) := pure (e, x)
| (app (app (macro _ [const `heq _ ]) e) (local_const x _ _ _)) := pure (e, x)
| _ := failure
private meta def generalizes_arg_parser : lean.parser (name × option name × pexpr) :=
with_desc "(id :)? expr = id" $ do
lhs ← lean.parser.pexpr 0,
(tk ":" >> match lhs with
| local_const hyp_name _ _ _ := do
(arg, arg_name) ← lean.parser.pexpr 0 >>= generalizes_arg_parser_eq,
pure (arg_name, some hyp_name, arg)
| _ := failure
end) <|>
(do
(arg, arg_name) ← generalizes_arg_parser_eq lhs,
pure (arg_name, none, arg))
private meta def generalizes_args_parser
: lean.parser (list (name × option name × pexpr)) :=
with_desc "[(id :)? expr = id, ...]" $
tk "[" *> sep_by (tk ",") generalizes_arg_parser <* tk "]"
/--
Generalizes the target over multiple expressions. For example, given the goal
P : ∀ n, fin n → Prop
n : ℕ
f : fin n
⊢ P (nat.succ n) (fin.succ f)
you can use `generalizes [n'_eq : nat.succ n = n', f'_eq : fin.succ f == f']` to
get
P : ∀ n, fin n → Prop
n : ℕ
f : fin n
n' : ℕ
n'_eq : n' = nat.succ n
f' : fin n'
f'_eq : f' == fin.succ f
⊢ P n' f'
The expressions must be given in dependency order, so
`[f'_eq : fin.succ f == f', n'_eq : nat.succ n = n']` would fail since the type
of `fin.succ f` is `nat.succ n`.
You can choose to omit some or all of the generated equations. For the above
example, `generalizes [nat.succ n = n', fin.succ f == f']` gets you
P : ∀ n, fin n → Prop
n : ℕ
f : fin n
n' : ℕ
f' : fin n'
⊢ P n' f'
After generalization, the target type may no longer type check. `generalizes`
will then raise an error.
-/
meta def generalizes (args : parse generalizes_args_parser) : tactic unit :=
propagate_tags $ do
args ← args.mmap $ λ ⟨arg_name, hyp_name, arg⟩, do {
arg ← to_expr arg,
pure (arg_name, hyp_name, arg)
},
generalizes_intro args,
pure ()
add_tactic_doc
{ name := "generalizes",
category := doc_category.tactic,
decl_names := [`tactic.interactive.generalizes],
tags := ["context management"],
inherit_description_from := `tactic.interactive.generalizes }
end interactive
end tactic
|
4122bfaa6ff0a9c397527892c3aede94954b3b14 | d9ed0fce1c218297bcba93e046cb4e79c83c3af8 | /library/tools/super/prover_state.lean | 29c65eeea150f768b3f4ac23545d856a5d62adc3 | [
"Apache-2.0"
] | permissive | leodemoura/lean_clone | 005c63aa892a6492f2d4741ee3c2cb07a6be9d7f | cc077554b584d39bab55c360bc12a6fe7957afe6 | refs/heads/master | 1,610,506,475,484 | 1,482,348,354,000 | 1,482,348,543,000 | 77,091,586 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 15,383 | lean | /-
Copyright (c) 2016 Gabriel Ebner. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Gabriel Ebner
-/
import .clause .lpo .cdcl_solver data.monad.transformers
open tactic monad expr
namespace super
structure score :=
(priority : ℕ)
(in_sos : bool)
(cost : ℕ)
(age : ℕ)
namespace score
def prio.immediate : ℕ := 0
def prio.default : ℕ := 1
def prio.never : ℕ := 2
def sched_default (sc : score) : score := { sc with priority := prio.default }
def sched_now (sc : score) : score := { sc with priority := prio.immediate }
def inc_cost (sc : score) (n : ℕ) : score := { sc with cost := sc^.cost + n }
def min (a b : score) : score :=
{ priority := nat.min a^.priority b^.priority,
in_sos := a^.in_sos && b^.in_sos,
cost := nat.min a^.cost b^.cost,
age := nat.min a^.age b^.age }
def combine (a b : score) : score :=
{ priority := nat.max a^.priority b^.priority,
in_sos := a^.in_sos && b^.in_sos,
cost := a^.cost + b^.cost,
age := nat.max a^.age b^.age }
end score
namespace score
meta instance : has_to_string score :=
⟨λe, "[" ++ to_string e^.priority ++
"," ++ to_string e^.cost ++
"," ++ to_string e^.age ++
",sos=" ++ to_string e^.in_sos ++ "]"⟩
end score
def clause_id := ℕ
namespace clause_id
def to_nat (id : clause_id) : ℕ := id
instance : decidable_eq clause_id := nat.decidable_eq
instance : has_ordering clause_id := nat.has_ordering
end clause_id
meta structure derived_clause :=
(id : clause_id)
(c : clause)
(selected : list ℕ)
(assertions : list expr)
(sc : score)
namespace derived_clause
meta instance : has_to_tactic_format derived_clause :=
⟨λc, do
c_fmt ← pp c^.c,
ass_fmt ← pp (c^.assertions^.for (λa, a^.local_type)),
return $
to_string c^.sc ++ " " ++
c_fmt ++ " <- " ++ ass_fmt ++
" (selected: " ++ to_fmt c^.selected ++
")"
⟩
meta def clause_with_assertions (ac : derived_clause) : clause :=
ac^.c^.close_constn ac^.assertions
end derived_clause
meta structure locked_clause :=
(dc : derived_clause)
(reasons : list (list expr))
namespace locked_clause
meta instance : has_to_tactic_format locked_clause :=
⟨λc, do
c_fmt ← pp c^.dc,
reasons_fmt ← pp (c^.reasons^.for (λr, r^.for (λa, a^.local_type))),
return $ c_fmt ++ " (locked in case of: " ++ reasons_fmt ++ ")"
⟩
end locked_clause
meta structure prover_state :=
(active : rb_map clause_id derived_clause)
(passive : rb_map clause_id derived_clause)
(newly_derived : list derived_clause)
(prec : list expr)
(locked : list locked_clause)
(local_false : expr)
(sat_solver : cdcl.state)
(current_model : rb_map expr bool)
(sat_hyps : rb_map expr (expr × expr))
(needs_sat_run : bool)
(clause_counter : nat)
open prover_state
private meta def join_with_nl : list format → format :=
list.foldl (λx y, x ++ format.line ++ y) format.nil
private meta def prover_state_tactic_fmt (s : prover_state) : tactic format := do
active_fmts ← mapm pp $ rb_map.values s^.active,
passive_fmts ← mapm pp $ rb_map.values s^.passive,
new_fmts ← mapm pp s^.newly_derived,
locked_fmts ← mapm pp s^.locked,
sat_fmts ← mapm pp s^.sat_solver^.clauses,
prec_fmts ← mapm pp s^.prec,
return (join_with_nl
([to_fmt "active:"] ++ map (append (to_fmt " ")) active_fmts ++
[to_fmt "passive:"] ++ map (append (to_fmt " ")) passive_fmts ++
[to_fmt "new:"] ++ map (append (to_fmt " ")) new_fmts ++
[to_fmt "locked:"] ++ map (append (to_fmt " ")) locked_fmts ++
[to_fmt "sat formulas:"] ++ map (append (to_fmt " ")) sat_fmts ++
[to_fmt "precedence order: " ++ to_fmt prec_fmts]))
meta instance : has_to_tactic_format prover_state :=
⟨prover_state_tactic_fmt⟩
meta def prover := state_t prover_state tactic
meta instance : monad prover := state_t.monad _ _
meta instance : has_monad_lift tactic prover := monad.monad_transformer_lift (state_t prover_state) tactic
meta def prover.fail {A B : Type} [has_to_format B] (msg : B) : prover A := ♯ @tactic.fail A _ _ msg
meta def prover.failed {A : Type} : prover A :=
prover.fail "failed"
meta def prover.orelse {A : Type} (p1 p2 : prover A) : prover A :=
take state, p1 state <|> p2 state
meta instance : alternative prover :=
alternative.mk (@monad.map _ _)
(@applicative.pure _ (monad_is_applicative prover))
(@applicative.seq _ (monad_is_applicative prover))
@prover.failed
@prover.orelse
meta def selection_strategy := derived_clause → prover derived_clause
meta def get_active : prover (rb_map clause_id derived_clause) :=
do state ← state_t.read, return state^.active
meta def add_active (a : derived_clause) : prover unit :=
do state ← state_t.read,
state_t.write { state with active := state^.active^.insert a^.id a }
meta def get_passive : prover (rb_map clause_id derived_clause) :=
lift passive state_t.read
meta def get_precedence : prover (list expr) :=
do state ← state_t.read, return state^.prec
meta def get_term_order : prover (expr → expr → bool) := do
state ← state_t.read,
return $ lpo (prec_gt_of_name_list (map name_of_funsym state^.prec))
private meta def set_precedence (new_prec : list expr) : prover unit :=
do state ← state_t.read, state_t.write { state with prec := new_prec }
meta def register_consts_in_precedence (consts : list expr) := do
p ← get_precedence,
p_set ← return (rb_map.set_of_list (map name_of_funsym p)),
new_syms ← return $ list.filter (λc, ¬p_set^.contains (name_of_funsym c)) consts,
set_precedence (new_syms ++ p)
meta def in_sat_solver {A} (cmd : cdcl.solver A) : prover A := do
state ← state_t.read,
result ← ♯ cmd state^.sat_solver,
state_t.write { state with sat_solver := result.2 },
return result.1
meta def collect_ass_hyps (c : clause) : prover (list expr) :=
let lcs := contained_lconsts c^.proof in
do st ← state_t.read,
return (do
hs ← st^.sat_hyps^.values,
h ← [hs.1, hs.2],
guard $ lcs^.contains h^.local_uniq_name,
[h])
meta def get_clause_count : prover ℕ :=
do s ← state_t.read, return s^.clause_counter
meta def get_new_cls_id : prover clause_id := do
state ← state_t.read,
state_t.write { state with clause_counter := state^.clause_counter + 1 },
return state^.clause_counter
meta def mk_derived (c : clause) (sc : score) : prover derived_clause := do
ass ← collect_ass_hyps c,
id ← ♯ get_new_cls_id,
return { id := id, c := c, selected := [], assertions := ass, sc := sc }
meta def add_inferred (c : derived_clause) : prover unit := do
c' ← ♯c^.c^.normalize, c' ← return { c with c := c' },
register_consts_in_precedence (contained_funsyms c'^.c^.type)^.values,
state ← state_t.read,
state_t.write { state with newly_derived := c' :: state^.newly_derived }
-- FIXME: what if we've seen the variable before, but with a weaker score?
meta def mk_sat_var (v : expr) (suggested_ph : bool) (suggested_ev : score) : prover unit :=
do st ← state_t.read, if st^.sat_hyps^.contains v then return () else do
hpv ← ♯ mk_local_def `h v,
hnv ← ♯ mk_local_def `hn $ imp v st^.local_false,
state_t.modify $ λst, { st with sat_hyps := st^.sat_hyps^.insert v (hpv, hnv) },
in_sat_solver $ cdcl.mk_var_core v suggested_ph,
match v with
| (pi _ _ _ _) := do
c ← ♯ clause.of_proof st^.local_false hpv,
mk_derived c suggested_ev >>= add_inferred
| _ := do cp ← ♯ clause.of_proof st^.local_false hpv, mk_derived cp suggested_ev >>= add_inferred,
cn ← ♯ clause.of_proof st^.local_false hnv, mk_derived cn suggested_ev >>= add_inferred
end
meta def get_sat_hyp_core (v : expr) (ph : bool) : prover (option expr) :=
flip monad.lift state_t.read $ λst,
match st^.sat_hyps^.find v with
| some (hp, hn) := some $ if ph then hp else hn
| none := none
end
meta def get_sat_hyp (v : expr) (ph : bool) : prover expr :=
do hyp_opt ← get_sat_hyp_core v ph,
match hyp_opt with
| some hyp := return hyp
| none := prover.fail $ "unknown sat variable: " ++ v^.to_string
end
meta def add_sat_clause (c : clause) (suggested_ev : score) : prover unit := do
c ← ♯ c^.distinct,
already_added ← flip monad.lift state_t.read $ λst, decidable.to_bool $
c^.type ∈ st^.sat_solver^.clauses^.for (λd, d^.type),
if already_added then return () else do
for c^.get_lits $ λl, mk_sat_var l^.formula l^.is_neg suggested_ev,
in_sat_solver $ cdcl.mk_clause c,
state_t.modify $ λst, { st with needs_sat_run := tt }
meta def sat_eval_lit (v : expr) (pol : bool) : prover bool :=
do v_st ← flip monad.lift state_t.read $ λst, st^.current_model^.find v,
match v_st with
| some ph := return $ if pol then ph else bnot ph
| none := return tt
end
meta def sat_eval_assertion (assertion : expr) : prover bool :=
do lf ← flip monad.lift state_t.read $ λst, st^.local_false,
match is_local_not lf assertion^.local_type with
| some v := sat_eval_lit v ff
| none := sat_eval_lit assertion^.local_type tt
end
meta def sat_eval_assertions : list expr → prover bool
| (a::ass) := do v_a ← sat_eval_assertion a,
if v_a then
sat_eval_assertions ass
else
return ff
| [] := return tt
private meta def intern_clause (c : derived_clause) : prover derived_clause := do
hyp_name ← ♯ get_unused_name (mk_simple_name $ "clause_" ++ to_string c^.id^.to_nat) none,
c' ← return $ c^.c^.close_constn c^.assertions,
♯ assertv hyp_name c'^.type c'^.proof,
proof' ← ♯ get_local hyp_name,
type ← ♯ infer_type proof', -- FIXME: otherwise ""
return { c with c := { (c^.c : clause) with proof := app_of_list proof' c^.assertions } }
meta def register_as_passive (c : derived_clause) : prover unit := do
c ← intern_clause c,
ass_v ← sat_eval_assertions c^.assertions,
if c^.c^.num_quants = 0 ∧ c^.c^.num_lits = 0 then
add_sat_clause c^.clause_with_assertions c^.sc
else if ¬ass_v then do
state_t.modify $ λst, { st with locked := ⟨c, []⟩ :: st^.locked }
else do
state_t.modify $ λst, { st with passive := st^.passive^.insert c^.id c }
meta def remove_passive (id : clause_id) : prover unit :=
do state ← state_t.read, state_t.write { state with passive := state^.passive^.erase id }
meta def move_locked_to_passive : prover unit := do
locked ← flip monad.lift state_t.read (λst, st^.locked),
new_locked ← flip filter locked (λlc, do
reason_vals ← mapm sat_eval_assertions lc^.reasons,
c_val ← sat_eval_assertions lc^.dc^.assertions,
if reason_vals^.for_all (λr, r = ff) ∧ c_val then do
state_t.modify $ λst, { st with passive := st^.passive^.insert lc^.dc^.id lc^.dc },
return ff
else
return tt
),
state_t.modify $ λst, { st with locked := new_locked }
meta def move_active_to_locked : prover unit :=
do active ← get_active, for' active^.values $ λac, do
c_val ← sat_eval_assertions ac^.assertions,
if ¬c_val then do
state_t.modify $ λst, { st with
active := st^.active^.erase ac^.id,
locked := ⟨ac, []⟩ :: st^.locked
}
else
return ()
meta def move_passive_to_locked : prover unit :=
do passive ← flip monad.lift state_t.read $ λst, st^.passive, for' passive^.to_list $ λpc, do
c_val ← sat_eval_assertions pc.2^.assertions,
if ¬c_val then do
state_t.modify $ λst, { st with
passive := st^.passive^.erase pc.1,
locked := ⟨pc.2, []⟩ :: st^.locked
}
else
return ()
meta def do_sat_run : prover (option expr) :=
do sat_result ← in_sat_solver $ cdcl.run (return none),
state_t.modify $ λst, { st with needs_sat_run := ff },
old_model ← lift prover_state.current_model state_t.read,
match sat_result with
| (cdcl.result.unsat proof) := return (some proof)
| (cdcl.result.sat new_model) := do
state_t.modify $ λst, { st with current_model := new_model },
move_locked_to_passive,
move_active_to_locked,
move_passive_to_locked,
return none
end
meta def take_newly_derived : prover (list derived_clause) := do
state ← state_t.read,
state_t.write { state with newly_derived := [] },
return state^.newly_derived
meta def remove_redundant (id : clause_id) (parents : list derived_clause) : prover unit := do
guard $ parents^.for_all $ λp, p^.id ≠ id,
red ← flip monad.lift state_t.read (λst, st^.active^.find id),
match red with
| none := return ()
| some red := do
let reasons := parents^.for (λp, p^.assertions),
assertion := red^.assertions in
if reasons^.for_all $ λr, r^.subset_of assertion then do
state_t.modify $ λst, { st with active := st^.active^.erase id }
else do
state_t.modify $ λst, { st with active := st^.active^.erase id,
locked := ⟨red, reasons⟩ :: st^.locked }
end
meta def inference := derived_clause → prover unit
meta structure inf_decl := (prio : ℕ) (inf : inference)
meta def inf_attr : user_attribute :=
⟨ `super.inf, "inference for the super prover" ⟩
run_command attribute.register `super.inf_attr
meta def seq_inferences : list inference → inference
| [] := λgiven, return ()
| (inf::infs) := λgiven, do
inf given,
now_active ← get_active,
if rb_map.contains now_active given^.id then
seq_inferences infs given
else
return ()
meta def simp_inference (simpl : derived_clause → prover (option clause)) : inference :=
λgiven, do maybe_simpld ← simpl given,
match maybe_simpld with
| some simpld := do
derived_simpld ← mk_derived simpld given^.sc^.sched_now,
add_inferred derived_simpld,
remove_redundant given^.id []
| none := return ()
end
meta def preprocessing_rule (f : list derived_clause → prover (list derived_clause)) : prover unit := do
state ← state_t.read,
newly_derived' ← f state^.newly_derived,
state' ← state_t.read,
state_t.write { state' with newly_derived := newly_derived' }
meta def clause_selection_strategy := ℕ → prover clause_id
namespace prover_state
meta def empty (local_false : expr) : prover_state :=
{ active := rb_map.mk _ _, passive := rb_map.mk _ _,
newly_derived := [], prec := [], clause_counter := 0,
local_false := local_false,
locked := [], sat_solver := cdcl.state.initial local_false,
current_model := rb_map.mk _ _, sat_hyps := rb_map.mk _ _, needs_sat_run := ff }
meta def initial (local_false : expr) (clauses : list clause) : tactic prover_state := do
after_setup ← for' clauses (λc,
let in_sos := decidable.to_bool ((contained_lconsts c^.proof)^.size = 0) in
do mk_derived c { priority := score.prio.immediate, in_sos := in_sos,
age := 0, cost := 0 } >>= add_inferred
) $ empty local_false,
return after_setup.2
end prover_state
meta def inf_score (add_cost : ℕ) (scores : list score) : prover score := do
age ← get_clause_count,
return $ list.foldl score.combine { priority := score.prio.default,
in_sos := tt,
age := age,
cost := add_cost
} scores
meta def inf_if_successful (add_cost : ℕ) (parent : derived_clause) (tac : tactic (list clause)) : prover unit :=
(do inferred ← ♯tac,
for' inferred $ λc,
inf_score add_cost [parent^.sc] >>= mk_derived c >>= add_inferred)
<|> return ()
meta def simp_if_successful (parent : derived_clause) (tac : tactic (list clause)) : prover unit :=
(do inferred ← ♯tac,
for' inferred $ λc,
mk_derived c parent^.sc^.sched_now >>= add_inferred,
remove_redundant parent^.id [])
<|> return ()
end super
|
f922f9eb7da8a445dd699bc987e7270ff31ffc59 | 6dc0c8ce7a76229dd81e73ed4474f15f88a9e294 | /stage0/src/Lean/Meta/SizeOf.lean | 3baab742c247871c5101f53fa717907c6885a88a | [
"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 | 21,993 | lean | /-
Copyright (c) 2021 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Meta.AppBuilder
import Lean.Meta.Instances
namespace Lean.Meta
/-- Create `SizeOf` local instances for applicable parameters, and execute `k` using them. -/
private partial def mkLocalInstances {α} (params : Array Expr) (k : Array Expr → MetaM α) : MetaM α :=
loop 0 #[]
where
loop (i : Nat) (insts : Array Expr) : MetaM α := do
if i < params.size then
let param := params[i]
let paramType ← inferType param
let instType? ← forallTelescopeReducing paramType fun xs _ => do
let type ← mkAppN param xs
try
let sizeOf ← mkAppM `SizeOf #[type]
let instType ← mkForallFVars xs sizeOf
return some instType
catch _ =>
return none
match instType? with
| none => loop (i+1) insts
| some instType =>
let instName ← mkFreshUserName `inst
withLocalDecl instName BinderInfo.instImplicit instType fun inst =>
loop (i+1) (insts.push inst)
else
k insts
/--
Return `some x` if `fvar` has type of the form `... -> motive ... fvar` where `motive` in `motiveFVars`.
That is, `x` "produces" one of the recursor motives.
-/
private def isInductiveHypothesis? (motiveFVars : Array Expr) (fvar : Expr) : MetaM (Option Expr) := do
forallTelescopeReducing (← inferType fvar) fun _ type =>
if type.isApp && motiveFVars.contains type.getAppFn then
return some type.appArg!
else
return none
private def isInductiveHypothesis (motiveFVars : Array Expr) (fvar : Expr) : MetaM Bool :=
return (← isInductiveHypothesis? motiveFVars fvar).isSome
/--
Let `motiveFVars` be free variables for each motive in a kernel recursor, and `minorFVars` the free variables for a minor premise.
Then, return `some idx` if `minorFVars[idx]` has a type of the form `... -> motive ... fvar` for some `motive` in `motiveFVars`.
-/
private def isRecField? (motiveFVars : Array Expr) (minorFVars : Array Expr) (fvar : Expr) : MetaM (Option Nat) := do
let mut idx := 0
for minorFVar in minorFVars do
if let some fvar' ← isInductiveHypothesis? motiveFVars minorFVar then
if fvar == fvar' then
return some idx
idx := idx + 1
return none
private partial def mkSizeOfMotives {α} (motiveFVars : Array Expr) (k : Array Expr → MetaM α) : MetaM α :=
loop 0 #[]
where
loop (i : Nat) (motives : Array Expr) : MetaM α := do
if i < motiveFVars.size then
let type ← inferType motiveFVars[i]
let motive ← forallTelescopeReducing type fun xs _ => do
mkLambdaFVars xs <| mkConst ``Nat
trace[Meta.sizeOf]! "motive: {motive}"
loop (i+1) (motives.push motive)
else
k motives
private partial def mkSizeOfMinors {α} (motiveFVars : Array Expr) (minorFVars : Array Expr) (minorFVars' : Array Expr) (k : Array Expr → MetaM α) : MetaM α :=
assert! minorFVars.size == minorFVars'.size
loop 0 #[]
where
loop (i : Nat) (minors : Array Expr) : MetaM α := do
if i < minorFVars.size then
forallTelescopeReducing (← inferType minorFVars[i]) fun xs _ =>
forallBoundedTelescope (← inferType minorFVars'[i]) xs.size fun xs' _ => do
let mut minor ← mkNumeral (mkConst ``Nat) 1
for x in xs, x' in xs' do
unless (← isInductiveHypothesis motiveFVars x) do
unless (← whnf (← inferType x)).isForall do -- we suppress higher-order fields
match (← isRecField? motiveFVars xs x) with
| some idx => minor ← mkAdd minor xs'[idx]
| none => minor ← mkAdd minor (← mkAppM ``SizeOf.sizeOf #[x'])
minor ← mkLambdaFVars xs' minor
trace[Meta.sizeOf]! "minor: {minor}"
loop (i+1) (minors.push minor)
else
k minors
/--
Create a "sizeOf" function with name `declName` using the recursor `recName`.
-/
partial def mkSizeOfFn (recName : Name) (declName : Name): MetaM Unit := do
trace[Meta.sizeOf]! "recName: {recName}"
let recInfo : RecursorVal ← getConstInfoRec recName
forallTelescopeReducing recInfo.type fun xs type =>
let levelParams := recInfo.levelParams.tail! -- universe parameters for declaration being defined
let params := xs[:recInfo.numParams]
let motiveFVars := xs[recInfo.numParams : recInfo.numParams + recInfo.numMotives]
let minorFVars := xs[recInfo.getFirstMinorIdx : recInfo.getFirstMinorIdx + recInfo.numMinors]
let indices := xs[recInfo.getFirstIndexIdx : recInfo.getFirstIndexIdx + recInfo.numIndices]
let major := xs[recInfo.getMajorIdx]
let nat := mkConst ``Nat
mkLocalInstances params fun localInsts =>
mkSizeOfMotives motiveFVars fun motives => do
let us := levelOne :: levelParams.map mkLevelParam -- universe level parameters for `rec`-application
let recFn := mkConst recName us
let val := mkAppN recFn (params ++ motives)
forallBoundedTelescope (← inferType val) recInfo.numMinors fun minorFVars' _ =>
mkSizeOfMinors motiveFVars minorFVars minorFVars' fun minors => do
let sizeOfParams := params ++ localInsts ++ indices ++ #[major]
let sizeOfType ← mkForallFVars sizeOfParams nat
let val := mkAppN val (minors ++ indices ++ #[major])
trace[Meta.sizeOf]! "val: {val}"
let sizeOfValue ← mkLambdaFVars sizeOfParams val
addDecl <| Declaration.defnDecl {
name := declName
levelParams := levelParams
type := sizeOfType
value := sizeOfValue
safety := DefinitionSafety.safe
hints := ReducibilityHints.abbrev
}
/--
Create `sizeOf` functions for all inductive datatypes in the mutual inductive declaration containing `typeName`
The resulting array contains the generated functions names. The `NameMap` maps recursor names into the generated function names.
There is a function for each element of the mutual inductive declaration, and for auxiliary recursors for nested inductive types.
-/
def mkSizeOfFns (typeName : Name) : MetaM (Array Name × NameMap Name) := do
let indInfo ← getConstInfoInduct typeName
let recInfo ← getConstInfoRec (mkRecName typeName)
let numExtra := recInfo.numMotives - indInfo.all.length -- numExtra > 0 for nested inductive types
let mut result := #[]
let baseName := indInfo.all.head! ++ `_sizeOf -- we use the first inductive type as the base name for `sizeOf` functions
let mut i := 1
let mut recMap : NameMap Name := {}
for indTypeName in indInfo.all do
let sizeOfName := baseName.appendIndexAfter i
let recName := mkRecName indTypeName
mkSizeOfFn recName sizeOfName
recMap := recMap.insert recName sizeOfName
result := result.push sizeOfName
i := i + 1
for j in [:numExtra] do
let recName := (mkRecName indInfo.all.head!).appendIndexAfter (j+1)
let sizeOfName := baseName.appendIndexAfter i
mkSizeOfFn recName sizeOfName
recMap := recMap.insert recName sizeOfName
result := result.push sizeOfName
i := i + 1
return (result, recMap)
def mkSizeOfSpecLemmaName (ctorName : Name) : Name :=
ctorName ++ `sizeOf_spec
def mkSizeOfSpecLemmaInstance (ctorApp : Expr) : MetaM Expr :=
matchConstCtor ctorApp.getAppFn (fun _ => throwError! "failed to apply 'sizeOf' spec, constructor expected{indentExpr ctorApp}") fun ctorInfo ctorLevels => do
let ctorArgs := ctorApp.getAppArgs
let ctorFields := ctorArgs[ctorArgs.size - ctorInfo.numFields:]
let lemmaName := mkSizeOfSpecLemmaName ctorInfo.name
let lemmaInfo ← getConstInfo lemmaName
let lemmaArity ← forallTelescopeReducing lemmaInfo.type fun xs _ => return xs.size
let lemmaArgMask := mkArray (lemmaArity - ctorInfo.numFields) (none (α := Expr))
let lemmaArgMask := lemmaArgMask ++ ctorFields.toArray.map some
mkAppOptM lemmaName lemmaArgMask
/- SizeOf spec theorem for nested inductive types -/
namespace SizeOfSpecNested
structure Context where
indInfo : InductiveVal
sizeOfFns : Array Name
ctorName : Name
params : Array Expr
localInsts : Array Expr
recMap : NameMap Name -- mapping from recursor name into `_sizeOf_<idx>` function name (see `mkSizeOfFns`)
abbrev M := ReaderT Context MetaM
def throwUnexpected {α} (msg : MessageData) : M α := do
throwError! "failed to generate sizeOf lemma for {(← read).ctorName} (use `set_option genSizeOfSpec false` to disable lemma generation), {msg}"
def throwFailed {α} : M α := do
throwError! "failed to generate sizeOf lemma for {(← read).ctorName}, (use `set_option genSizeOfSpec false` to disable lemma generation)"
/-- Convert a recursor application into a `_sizeOf_<idx>` application. -/
private def recToSizeOf (e : Expr) : M Expr := do
matchConstRec e.getAppFn (fun _ => throwFailed) fun info us => do
match (← read).recMap.find? info.name with
| none => throwUnexpected m!"expected recursor application {indentExpr e}"
| some sizeOfName =>
let args := e.getAppArgs
let indices := args[info.getFirstIndexIdx : info.getFirstIndexIdx + info.numIndices]
let major := args[info.getMajorIdx]
return mkAppN (mkConst sizeOfName us.tail!) ((← read).params ++ (← read).localInsts ++ indices ++ #[major])
mutual
/-- Construct minor premise proof for `mkSizeOfAuxLemmaProof`. `ys` contains fields and inductive hypotheses for the minor premise. -/
private partial def mkMinorProof (ys : Array Expr) (lhs rhs : Expr) : M Expr := do
trace[Meta.sizeOf.minor]! "{lhs} =?= {rhs}"
if (← isDefEq lhs rhs) then
mkEqRefl rhs
else
match (← whnfI lhs).natAdd?, (← whnfI rhs).natAdd? with
| some (a₁, b₁), some (a₂, b₂) =>
let p₁ ← mkMinorProof ys a₁ a₂
let p₂ ← mkMinorProofStep ys b₁ b₂
mkCongr (← mkCongrArg (mkConst ``Nat.add) p₁) p₂
| _, _ =>
throwUnexpected m!"expected 'Nat.add' application, lhs is {indentExpr lhs}\nrhs is{indentExpr rhs}"
/--
Helper method for `mkMinorProof`. The proof step is one of the following
- Reflexivity
- Assumption (i.e., using an inductive hypotheses from `ys`)
- `mkSizeOfAuxLemma` application. This case happens when we have multiple levels of nesting
-/
private partial def mkMinorProofStep (ys : Array Expr) (lhs rhs : Expr) : M Expr := do
if (← isDefEq lhs rhs) then
mkEqRefl rhs
else
let lhs ← recToSizeOf lhs
trace[Meta.sizeOf.minor.step]! "{lhs} =?= {rhs}"
let target ← mkEq lhs rhs
for y in ys do
if (← isDefEq (← inferType y) target) then
return y
mkSizeOfAuxLemma lhs rhs
/-- Construct proof of auxiliary lemma. See `mkSizeOfAuxLemma` -/
private partial def mkSizeOfAuxLemmaProof (info : InductiveVal) (lhs rhs : Expr) : M Expr := do
let lhsArgs := lhs.getAppArgs
let sizeOfBaseArgs := lhsArgs[:lhsArgs.size - info.numIndices - 1]
let indicesMajor := lhsArgs[lhsArgs.size - info.numIndices - 1:]
let sizeOfLevels := lhs.getAppFn.constLevels!
/- Auxiliary function for constructing an `_sizeOf_<idx>` for `ys`,
where `ys` are the indices + major.
Recall that if `info.name` is part of a mutually inductive declaration, then the resulting application
is not necessarily a `lhs.getAppFn` application.
The result is an application of one of the `(← read),sizeOfFns` functions.
We use this auxiliary function to builtin the motive of the recursor. -/
let rec mkSizeOf (ys : Array Expr) : M Expr := do
for sizeOfFn in (← read).sizeOfFns do
let candidate := mkAppN (mkAppN (mkConst sizeOfFn sizeOfLevels) sizeOfBaseArgs) ys
if (← isTypeCorrect candidate) then
return candidate
throwFailed
let major := lhs.appArg!
let majorType ← whnf (← inferType major)
let majorTypeArgs := majorType.getAppArgs
match majorType.getAppFn.const? with
| none => throwFailed
| some (_, us) =>
let recName := mkRecName info.name
let recInfo ← getConstInfoRec recName
let r := mkConst recName (levelZero :: us)
let r := mkAppN r majorTypeArgs[:info.numParams]
forallBoundedTelescope (← inferType r) recInfo.numMotives fun motiveFVars _ => do
let mut r := r
-- Add motives
for motiveFVar in motiveFVars do
let motive ← forallTelescopeReducing (← inferType motiveFVar) fun ys _ => do
let lhs ← mkSizeOf ys
let rhs ← mkAppM ``SizeOf.sizeOf #[ys.back]
mkLambdaFVars ys (← mkEq lhs rhs)
r := mkApp r motive
forallBoundedTelescope (← inferType r) recInfo.numMinors fun minorFVars _ => do
let mut r := r
-- Add minors
for minorFVar in minorFVars do
let minor ← forallTelescopeReducing (← inferType minorFVar) fun ys target => do
let target ← whnf target
match target.eq? with
| none => throwFailed
| some (_, lhs, rhs) =>
if (← isDefEq lhs rhs) then
mkLambdaFVars ys (← mkEqRefl rhs)
else
let lhs ← unfoldDefinition lhs -- Unfold `_sizeOf_<idx>`
-- rhs is of the form `sizeOf (ctor ...)`
let ctorApp := rhs.appArg!
let specLemma ← mkSizeOfSpecLemmaInstance ctorApp
let specEq ← whnf (← inferType specLemma)
match specEq.eq? with
| none => throwFailed
| some (_, rhs, rhsExpanded) =>
let lhs_eq_rhsExpanded ← mkMinorProof ys lhs rhsExpanded
let rhsExpanded_eq_rhs ← mkEqSymm specLemma
mkLambdaFVars ys (← mkEqTrans lhs_eq_rhsExpanded rhsExpanded_eq_rhs)
r := mkApp r minor
-- Add indices and major
return mkAppN r indicesMajor
/--
Generate proof for `C._sizeOf_<idx> t = sizeOf t` where `C._sizeOf_<idx>` is a auxiliary function
generated for a nested inductive type in `C`.
For example, given
```lean
inductive Expr where
| app (f : String) (args : List Expr)
```
We generate the auxiliary function `Expr._sizeOf_1 : List Expr → Nat`.
To generate the `sizeOf` spec lemma
```
sizeOf (Expr.app f args) = 1 + sizeOf f + sizeOf args
```
we need an auxiliary lemma for showing `Expr._sizeOf_1 args = sizeOf args`.
Recall that `sizeOf (Expr.app f args)` is definitionally equal to `1 + sizeOf f + Expr._sizeOf_1 args`, but
`Expr._sizeOf_1 args` is **not** definitionally equal to `sizeOf args`. We need a proof by induction.
-/
private partial def mkSizeOfAuxLemma (lhs rhs : Expr) : M Expr := do
trace[Meta.sizeOf.aux]! "{lhs} =?= {rhs}"
match lhs.getAppFn.const? with
| none => throwFailed
| some (fName, us) =>
let thmLevelParams ← us.mapM fun
| Level.param n _ => return n
| _ => throwFailed
let thmName := fName.appendAfter "_eq"
if (← getEnv).contains thmName then
-- Auxiliary lemma has already been defined
return mkAppN (mkConst thmName us) lhs.getAppArgs
else
-- Define auxiliary lemma
-- First, generalize indices
let x := lhs.appArg!
let xType ← whnf (← inferType x)
matchConstInduct xType.getAppFn (fun _ => throwFailed) fun info _ => do
let params := xType.getAppArgs[:info.numParams]
forallTelescopeReducing (← inferType (mkAppN xType.getAppFn params)) fun indices _ => do
let majorType := mkAppN (mkAppN xType.getAppFn params) indices
withLocalDeclD `x majorType fun major => do
let lhsArgs := lhs.getAppArgs
let lhsArgsNew := lhsArgs[:lhsArgs.size - 1 - indices.size] ++ indices ++ #[major]
let lhsNew := mkAppN lhs.getAppFn lhsArgsNew
let rhsNew ← mkAppM ``SizeOf.sizeOf #[major]
let eq ← mkEq lhsNew rhsNew
let thmParams := lhsArgsNew
let thmType ← mkForallFVars thmParams eq
let thmValue ← mkSizeOfAuxLemmaProof info lhsNew rhsNew
let thmValue ← mkLambdaFVars thmParams thmValue
trace[Meta.sizeOf]! "thmValue: {thmValue}"
addDecl <| Declaration.thmDecl {
name := thmName
levelParams := thmLevelParams
type := thmType
value := thmValue
}
return mkAppN (mkConst thmName us) lhs.getAppArgs
end
/- Prove SizeOf spec lemma of the form `sizeOf <ctor-application> = 1 + sizeOf <field_1> + ... + sizeOf <field_n> -/
partial def main (lhs rhs : Expr) : M Expr := do
if (← isDefEq lhs rhs) then
mkEqRefl rhs
else
/- Expand lhs and rhs to obtain `Nat.add` applications -/
let lhs ← whnfI lhs -- Expand `sizeOf (ctor ...)` into `_sizeOf_<idx>` application
let lhs ← unfoldDefinition lhs -- Unfold `_sizeOf_<idx>` application into `HAdd.hAdd` application
loop lhs rhs
where
loop (lhs rhs : Expr) : M Expr := do
trace[Meta.sizeOf.loop]! "{lhs} =?= {rhs}"
if (← isDefEq lhs rhs) then
mkEqRefl rhs
else
match (← whnfI lhs).natAdd?, (← whnfI rhs).natAdd? with
| some (a₁, b₁), some (a₂, b₂) =>
let p₁ ← loop a₁ a₂
let p₂ ← step b₁ b₂
mkCongr (← mkCongrArg (mkConst ``Nat.add) p₁) p₂
| _, _ =>
throwUnexpected m!"expected 'Nat.add' application, lhs is {indentExpr lhs}\nrhs is{indentExpr rhs}"
step (lhs rhs : Expr) : M Expr := do
if (← isDefEq lhs rhs) then
mkEqRefl rhs
else
let lhs ← recToSizeOf lhs
mkSizeOfAuxLemma lhs rhs
end SizeOfSpecNested
private def mkSizeOfSpecTheorem (indInfo : InductiveVal) (sizeOfFns : Array Name) (recMap : NameMap Name) (ctorName : Name) : MetaM Unit := do
let ctorInfo ← getConstInfoCtor ctorName
let us := ctorInfo.levelParams.map mkLevelParam
forallTelescopeReducing ctorInfo.type fun xs _ => do
let params := xs[:ctorInfo.numParams]
let fields := xs[ctorInfo.numParams:]
let ctorApp := mkAppN (mkConst ctorName us) xs
mkLocalInstances params fun localInsts => do
let lhs ← mkAppM ``SizeOf.sizeOf #[ctorApp]
let mut rhs ← mkNumeral (mkConst ``Nat) 1
for field in fields do
unless (← whnf (← inferType field)).isForall do
rhs ← mkAdd rhs (← mkAppM ``SizeOf.sizeOf #[field])
let target ← mkEq lhs rhs
let thmName := mkSizeOfSpecLemmaName ctorName
let thmParams := params ++ localInsts ++ fields
let thmType ← mkForallFVars thmParams target
let thmValue ←
if indInfo.isNested then
SizeOfSpecNested.main lhs rhs |>.run {
indInfo := indInfo, sizeOfFns := sizeOfFns, ctorName := ctorName, params := params, localInsts := localInsts, recMap := recMap
}
else
mkEqRefl rhs
let thmValue ← mkLambdaFVars thmParams thmValue
addDecl <| Declaration.thmDecl {
name := thmName
levelParams := ctorInfo.levelParams
type := thmType
value := thmValue
}
private def mkSizeOfSpecTheorems (indTypeNames : Array Name) (sizeOfFns : Array Name) (recMap : NameMap Name) : MetaM Unit := do
for indTypeName in indTypeNames do
let indInfo ← getConstInfoInduct indTypeName
for ctorName in indInfo.ctors do
mkSizeOfSpecTheorem indInfo sizeOfFns recMap ctorName
return ()
register_builtin_option genSizeOf : Bool := {
defValue := true
descr := "generate `SizeOf` instance for inductive types and structures"
}
register_builtin_option genSizeOfSpec : Bool := {
defValue := true
descr := "generate `SizeOf` specificiation theorems for automatically generated instances"
}
def mkSizeOfInstances (typeName : Name) : MetaM Unit := do
if (← getEnv).contains ``SizeOf && genSizeOf.get (← getOptions) && !(← isInductivePredicate typeName) then
let indInfo ← getConstInfoInduct typeName
unless indInfo.isUnsafe do
let (fns, recMap) ← mkSizeOfFns typeName
for indTypeName in indInfo.all, fn in fns do
let indInfo ← getConstInfoInduct indTypeName
forallTelescopeReducing indInfo.type fun xs _ =>
let params := xs[:indInfo.numParams]
let indices := xs[indInfo.numParams:]
mkLocalInstances params fun localInsts => do
let us := indInfo.levelParams.map mkLevelParam
let indType := mkAppN (mkConst indTypeName us) xs
let sizeOfIndType ← mkAppM ``SizeOf #[indType]
withLocalDeclD `m indType fun m => do
let v ← mkLambdaFVars #[m] <| mkAppN (mkConst fn us) (params ++ localInsts ++ indices ++ #[m])
let sizeOfMk ← mkAppM ``SizeOf.mk #[v]
let instDeclName := indTypeName ++ `_sizeOf_inst
let instDeclType ← mkForallFVars (xs ++ localInsts) sizeOfIndType
let instDeclValue ← mkLambdaFVars (xs ++ localInsts) sizeOfMk
addDecl <| Declaration.defnDecl {
name := instDeclName
levelParams := indInfo.levelParams
type := instDeclType
value := instDeclValue
safety := DefinitionSafety.safe
hints := ReducibilityHints.abbrev
}
addInstance instDeclName AttributeKind.global (evalPrio! default)
if genSizeOfSpec.get (← getOptions) then
mkSizeOfSpecTheorems indInfo.all.toArray fns recMap
builtin_initialize
registerTraceClass `Meta.sizeOf
end Lean.Meta
|
db32bbb2f107fc3005db7c256dea3b7bbbf7a64f | 7850aae797be6c31052ce4633d86f5991178d3df | /stage0/src/Lean/Elab/Util.lean | f4ca458cd1ac9f546089a842034944175bf8a7e7 | [
"Apache-2.0"
] | permissive | miriamgoetze/lean4 | 4dc24d4dbd360cc969713647c2958c6691947d16 | 062cc5d5672250be456a168e9c7b9299a9c69bdb | refs/heads/master | 1,685,865,971,011 | 1,624,107,703,000 | 1,624,107,703,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 7,871 | lean | /-
Copyright (c) 2019 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Util.Trace
import Lean.Parser.Syntax
import Lean.Parser.Extension
import Lean.KeyedDeclsAttribute
import Lean.Elab.Exception
namespace Lean
def Syntax.prettyPrint (stx : Syntax) : Format :=
match stx.unsetTrailing.reprint with -- TODO use syntax pretty printer
| some str => format str.toFormat
| none => format stx
def MacroScopesView.format (view : MacroScopesView) (mainModule : Name) : Format :=
fmt $
if view.scopes.isEmpty then
view.name
else if view.mainModule == mainModule then
view.scopes.foldl Name.mkNum (view.name ++ view.imported)
else
view.scopes.foldl Name.mkNum (view.name ++ view.imported ++ view.mainModule)
namespace Elab
def expandOptNamedPrio (stx : Syntax) : MacroM Nat :=
if stx.isNone then
return eval_prio default
else match stx[0] with
| `(Parser.Command.namedPrio| (priority := $prio)) => evalPrio prio
| _ => Macro.throwUnsupported
def expandOptNamedName (stx : Syntax) : MacroM (Option Name) := do
if stx.isNone then
return none
else match stx[0] with
| `(Parser.Command.namedName| (name := $name)) => return name.getId
| _ => Macro.throwUnsupported
structure MacroStackElem where
before : Syntax
after : Syntax
abbrev MacroStack := List MacroStackElem
/- If `ref` does not have position information, then try to use macroStack -/
def getBetterRef (ref : Syntax) (macroStack : MacroStack) : Syntax :=
match ref.getPos? with
| some _ => ref
| none =>
match macroStack.find? (·.before.getPos? != none) with
| some elem => elem.before
| none => ref
register_builtin_option pp.macroStack : Bool := {
defValue := false
group := "pp"
descr := "dispaly macro expansion stack"
}
def addMacroStack {m} [Monad m] [MonadOptions m] (msgData : MessageData) (macroStack : MacroStack) : m MessageData := do
if !pp.macroStack.get (← getOptions) then pure msgData else
match macroStack with
| [] => pure msgData
| stack@(top::_) =>
let msgData := msgData ++ Format.line ++ "with resulting expansion" ++ indentD top.after
pure $ stack.foldl
(fun (msgData : MessageData) (elem : MacroStackElem) =>
msgData ++ Format.line ++ "while expanding" ++ indentD elem.before)
msgData
def checkSyntaxNodeKind (k : Name) : AttrM Name := do
if Parser.isValidSyntaxNodeKind (← getEnv) k then pure k
else throwError "failed"
def checkSyntaxNodeKindAtNamespacesAux (k : Name) : Name → AttrM Name
| n@(Name.str p _ _) => checkSyntaxNodeKind (n ++ k) <|> checkSyntaxNodeKindAtNamespacesAux k p
| _ => throwError "failed"
def checkSyntaxNodeKindAtNamespaces (k : Name) : AttrM Name := do
let ctx ← read
checkSyntaxNodeKindAtNamespacesAux k ctx.currNamespace
def syntaxNodeKindOfAttrParam (defaultParserNamespace : Name) (stx : Syntax) : AttrM SyntaxNodeKind := do
let k ← Attribute.Builtin.getId stx
checkSyntaxNodeKind k
<|>
checkSyntaxNodeKindAtNamespaces k
<|>
checkSyntaxNodeKind (defaultParserNamespace ++ k)
<|>
throwError "invalid syntax node kind '{k}'"
private unsafe def evalSyntaxConstantUnsafe (env : Environment) (opts : Options) (constName : Name) : ExceptT String Id Syntax :=
env.evalConstCheck Syntax opts `Lean.Syntax constName
@[implementedBy evalSyntaxConstantUnsafe]
constant evalSyntaxConstant (env : Environment) (opts : Options) (constName : Name) : ExceptT String Id Syntax := throw ""
unsafe def mkElabAttribute (γ) (attrDeclName attrBuiltinName attrName : Name) (parserNamespace : Name) (typeName : Name) (kind : String)
: IO (KeyedDeclsAttribute γ) :=
KeyedDeclsAttribute.init {
builtinName := attrBuiltinName
name := attrName
descr := kind ++ " elaborator"
valueTypeName := typeName
evalKey := fun _ stx => syntaxNodeKindOfAttrParam parserNamespace stx
} attrDeclName
unsafe def mkMacroAttributeUnsafe : IO (KeyedDeclsAttribute Macro) :=
mkElabAttribute Macro `Lean.Elab.macroAttribute `builtinMacro `macro Name.anonymous `Lean.Macro "macro"
@[implementedBy mkMacroAttributeUnsafe]
constant mkMacroAttribute : IO (KeyedDeclsAttribute Macro)
builtin_initialize macroAttribute : KeyedDeclsAttribute Macro ← mkMacroAttribute
private def expandMacroFns (stx : Syntax) : List Macro → MacroM Syntax
| [] => throw Macro.Exception.unsupportedSyntax
| m::ms => do
try
m stx
catch
| Macro.Exception.unsupportedSyntax => expandMacroFns stx ms
| ex => throw ex
def getMacros (env : Environment) : Macro := fun stx =>
expandMacroFns stx (macroAttribute.getValues env stx.getKind)
class MonadMacroAdapter (m : Type → Type) where
getCurrMacroScope : m MacroScope
getNextMacroScope : m MacroScope
setNextMacroScope : MacroScope → m Unit
instance (m n) [MonadLift m n] [MonadMacroAdapter m] : MonadMacroAdapter n := {
getCurrMacroScope := liftM (MonadMacroAdapter.getCurrMacroScope : m _),
getNextMacroScope := liftM (MonadMacroAdapter.getNextMacroScope : m _),
setNextMacroScope := fun s => liftM (MonadMacroAdapter.setNextMacroScope s : m _)
}
private def expandMacro? (env : Environment) (stx : Syntax) : MacroM (Option Syntax) := do
try
let newStx ← getMacros env stx
pure (some newStx)
catch
| Macro.Exception.unsupportedSyntax => pure none
| ex => throw ex
@[inline] def liftMacroM {α} {m : Type → Type} [Monad m] [MonadMacroAdapter m] [MonadEnv m] [MonadRecDepth m] [MonadError m] [MonadResolveName m] [MonadTrace m] [MonadOptions m] [AddMessageContext m] (x : MacroM α) : m α := do
let env ← getEnv
let currNamespace ← getCurrNamespace
let openDecls ← getOpenDecls
let methods := Macro.mkMethods {
expandMacro? := expandMacro? env
hasDecl := fun declName => return env.contains declName
getCurrNamespace := return currNamespace
resolveNamespace? := fun n => return ResolveName.resolveNamespace? env currNamespace openDecls n
resolveGlobalName := fun n => return ResolveName.resolveGlobalName env currNamespace openDecls n
}
match x { methods := methods
ref := ← getRef
currMacroScope := ← MonadMacroAdapter.getCurrMacroScope
mainModule := env.mainModule
currRecDepth := ← MonadRecDepth.getRecDepth
maxRecDepth := ← MonadRecDepth.getMaxRecDepth
} { macroScope := (← MonadMacroAdapter.getNextMacroScope) } with
| EStateM.Result.error Macro.Exception.unsupportedSyntax _ => throwUnsupportedSyntax
| EStateM.Result.error (Macro.Exception.error ref msg) _ => throwErrorAt ref msg
| EStateM.Result.ok a s =>
MonadMacroAdapter.setNextMacroScope s.macroScope
s.traceMsgs.reverse.forM fun (clsName, msg) => trace clsName fun _ => msg
pure a
@[inline] def adaptMacro {m : Type → Type} [Monad m] [MonadMacroAdapter m] [MonadEnv m] [MonadRecDepth m] [MonadError m] [MonadResolveName m] [MonadTrace m] [MonadOptions m] [AddMessageContext m] (x : Macro) (stx : Syntax) : m Syntax :=
liftMacroM (x stx)
partial def mkUnusedBaseName (baseName : Name) : MacroM Name := do
let currNamespace ← Macro.getCurrNamespace
if ← Macro.hasDecl (currNamespace ++ baseName) then
let rec loop (idx : Nat) := do
let name := baseName.appendIndexAfter idx
if ← Macro.hasDecl (currNamespace ++ name) then
loop (idx+1)
else
name
loop 1
else
return baseName
builtin_initialize
registerTraceClass `Elab
registerTraceClass `Elab.step
end Lean.Elab
|
fb705ac857415e962ae24a11165849cd256a380f | c777c32c8e484e195053731103c5e52af26a25d1 | /src/field_theory/finite/polynomial.lean | 97d574232fdc3234b76aade7ba6bbf358ea8c445 | [
"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 | 8,060 | lean | /-
Copyright (c) 2020 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin
-/
import linear_algebra.finite_dimensional
import linear_algebra.basic
import ring_theory.mv_polynomial.basic
import data.mv_polynomial.expand
import field_theory.finite.basic
/-!
## Polynomials over finite fields
-/
namespace mv_polynomial
variables {σ : Type*}
/-- A polynomial over the integers is divisible by `n : ℕ`
if and only if it is zero over `zmod n`. -/
lemma C_dvd_iff_zmod (n : ℕ) (φ : mv_polynomial σ ℤ) :
C (n:ℤ) ∣ φ ↔ map (int.cast_ring_hom (zmod n)) φ = 0 :=
C_dvd_iff_map_hom_eq_zero _ _ (char_p.int_cast_eq_zero_iff (zmod n) n) _
section frobenius
variables {p : ℕ} [fact p.prime]
lemma frobenius_zmod (f : mv_polynomial σ (zmod p)) : frobenius _ p f = expand p f :=
begin
apply induction_on f,
{ intro a, rw [expand_C, frobenius_def, ← C_pow, zmod.pow_card], },
{ simp only [alg_hom.map_add, ring_hom.map_add], intros _ _ hf hg, rw [hf, hg] },
{ simp only [expand_X, ring_hom.map_mul, alg_hom.map_mul],
intros _ _ hf, rw [hf, frobenius_def], },
end
lemma expand_zmod (f : mv_polynomial σ (zmod p)) : expand p f = f ^ p :=
(frobenius_zmod _).symm
end frobenius
end mv_polynomial
namespace mv_polynomial
noncomputable theory
open_locale big_operators classical
open set linear_map submodule
variables {K : Type*} {σ : Type*}
section indicator
variables [fintype K] [fintype σ]
/-- Over a field, this is the indicator function as an `mv_polynomial`. -/
def indicator [comm_ring K] (a : σ → K) : mv_polynomial σ K :=
∏ n, (1 - (X n - C (a n)) ^ (fintype.card K - 1))
section comm_ring
variables [comm_ring K]
lemma eval_indicator_apply_eq_one (a : σ → K) :
eval a (indicator a) = 1 :=
begin
nontriviality,
have : 0 < fintype.card K - 1 := tsub_pos_of_lt fintype.one_lt_card,
simp only [indicator, map_prod, map_sub, map_one, map_pow, eval_X, eval_C,
sub_self, zero_pow this, sub_zero, finset.prod_const_one]
end
lemma degrees_indicator (c : σ → K) :
degrees (indicator c) ≤ ∑ s : σ, (fintype.card K - 1) • {s} :=
begin
rw [indicator],
refine le_trans (degrees_prod _ _) (finset.sum_le_sum $ assume s hs, _),
refine le_trans (degrees_sub _ _) _,
rw [degrees_one, ← bot_eq_zero, bot_sup_eq],
refine le_trans (degrees_pow _ _) (nsmul_le_nsmul_of_le_right _ _),
refine le_trans (degrees_sub _ _) _,
rw [degrees_C, ← bot_eq_zero, sup_bot_eq],
exact degrees_X' _
end
lemma indicator_mem_restrict_degree (c : σ → K) :
indicator c ∈ restrict_degree σ K (fintype.card K - 1) :=
begin
rw [mem_restrict_degree_iff_sup, indicator],
assume n,
refine le_trans (multiset.count_le_of_le _ $ degrees_indicator _) (le_of_eq _),
simp_rw [ ← multiset.coe_count_add_monoid_hom, (multiset.count_add_monoid_hom n).map_sum,
add_monoid_hom.map_nsmul, multiset.coe_count_add_monoid_hom, nsmul_eq_mul, nat.cast_id],
transitivity,
refine finset.sum_eq_single n _ _,
{ assume b hb ne, rw [multiset.count_singleton, if_neg ne.symm, mul_zero] },
{ assume h, exact (h $ finset.mem_univ _).elim },
{ rw [multiset.count_singleton_self, mul_one] }
end
end comm_ring
variables [field K]
lemma eval_indicator_apply_eq_zero (a b : σ → K) (h : a ≠ b) :
eval a (indicator b) = 0 :=
begin
obtain ⟨i, hi⟩ : ∃ i, a i ≠ b i := by rwa [(≠), function.funext_iff, not_forall] at h,
simp only [indicator, map_prod, map_sub, map_one, map_pow, eval_X, eval_C,
sub_self, finset.prod_eq_zero_iff],
refine ⟨i, finset.mem_univ _, _⟩,
rw [finite_field.pow_card_sub_one_eq_one, sub_self],
rwa [(≠), sub_eq_zero],
end
end indicator
section
variables (K σ)
/-- `mv_polynomial.eval` as a `K`-linear map. -/
@[simps] def evalₗ [comm_semiring K] : mv_polynomial σ K →ₗ[K] (σ → K) → K :=
{ to_fun := λ p e, eval e p,
map_add' := λ p q, by { ext x, rw ring_hom.map_add, refl, },
map_smul' := λ a p, by { ext e, rw [smul_eq_C_mul, ring_hom.map_mul, eval_C], refl } }
end
variables [field K] [fintype K] [finite σ]
lemma map_restrict_dom_evalₗ : (restrict_degree σ K (fintype.card K - 1)).map (evalₗ K σ) = ⊤ :=
begin
casesI nonempty_fintype σ,
refine top_unique (set_like.le_def.2 $ assume e _, mem_map.2 _),
refine ⟨∑ n : σ → K, e n • indicator n, _, _⟩,
{ exact sum_mem (assume c _, smul_mem _ _ (indicator_mem_restrict_degree _)) },
{ ext n,
simp only [linear_map.map_sum, @finset.sum_apply (σ → K) (λ_, K) _ _ _ _ _,
pi.smul_apply, linear_map.map_smul],
simp only [evalₗ_apply],
transitivity,
refine finset.sum_eq_single n (λ b _ h, _) _,
{ rw [eval_indicator_apply_eq_zero _ _ h.symm, smul_zero] },
{ exact λ h, (h $ finset.mem_univ n).elim },
{ rw [eval_indicator_apply_eq_one, smul_eq_mul, mul_one] } }
end
end mv_polynomial
namespace mv_polynomial
open_locale classical cardinal
open linear_map submodule
universe u
variables (σ : Type u) (K : Type u) [fintype K]
/-- The submodule of multivariate polynomials whose degree of each variable is strictly less
than the cardinality of K. -/
@[derive [add_comm_group, module K, inhabited]]
def R [comm_ring K] : Type u := restrict_degree σ K (fintype.card K - 1)
/-- Evaluation in the `mv_polynomial.R` subtype. -/
def evalᵢ [comm_ring K] : R σ K →ₗ[K] (σ → K) → K :=
((evalₗ K σ).comp (restrict_degree σ K (fintype.card K - 1)).subtype)
section comm_ring
variables [comm_ring K]
noncomputable instance decidable_restrict_degree (m : ℕ) :
decidable_pred (∈ {n : σ →₀ ℕ | ∀i, n i ≤ m }) :=
by simp only [set.mem_set_of_eq]; apply_instance
end comm_ring
variables [field K]
lemma rank_R [fintype σ] : module.rank K (R σ K) = fintype.card (σ → K) :=
calc module.rank K (R σ K) =
module.rank K (↥{s : σ →₀ ℕ | ∀ (n : σ), s n ≤ fintype.card K - 1} →₀ K) :
linear_equiv.rank_eq
(finsupp.supported_equiv_finsupp {s : σ →₀ ℕ | ∀n:σ, s n ≤ fintype.card K - 1 })
... = #{s : σ →₀ ℕ | ∀ (n : σ), s n ≤ fintype.card K - 1} : by rw [rank_finsupp_self']
... = #{s : σ → ℕ | ∀ (n : σ), s n < fintype.card K } :
begin
refine quotient.sound ⟨equiv.subtype_equiv finsupp.equiv_fun_on_finite $ assume f, _⟩,
refine forall_congr (assume n, le_tsub_iff_right _),
exact fintype.card_pos_iff.2 ⟨0⟩
end
... = #(σ → {n // n < fintype.card K}) :
(@equiv.subtype_pi_equiv_pi σ (λ_, ℕ) (λs n, n < fintype.card K)).cardinal_eq
... = #(σ → fin (fintype.card K)) :
(equiv.arrow_congr (equiv.refl σ) fin.equiv_subtype.symm).cardinal_eq
... = #(σ → K) :
(equiv.arrow_congr (equiv.refl σ) (fintype.equiv_fin K).symm).cardinal_eq
... = fintype.card (σ → K) : cardinal.mk_fintype _
instance [finite σ] : finite_dimensional K (R σ K) :=
by { casesI nonempty_fintype σ,
exact is_noetherian.iff_fg.1 (is_noetherian.iff_rank_lt_aleph_0.mpr $
by simpa only [rank_R] using cardinal.nat_lt_aleph_0 (fintype.card (σ → K))) }
lemma finrank_R [fintype σ] : finite_dimensional.finrank K (R σ K) = fintype.card (σ → K) :=
finite_dimensional.finrank_eq_of_rank_eq (rank_R σ K)
lemma range_evalᵢ [finite σ] : (evalᵢ σ K).range = ⊤ :=
begin
rw [evalᵢ, linear_map.range_comp, range_subtype],
exact map_restrict_dom_evalₗ
end
lemma ker_evalₗ [finite σ] : (evalᵢ σ K).ker = ⊥ :=
begin
casesI nonempty_fintype σ,
refine (ker_eq_bot_iff_range_eq_top_of_finrank_eq_finrank _).mpr (range_evalᵢ _ _),
rw [finite_dimensional.finrank_fintype_fun_eq_card, finrank_R]
end
lemma eq_zero_of_eval_eq_zero [finite σ] (p : mv_polynomial σ K)
(h : ∀v:σ → K, eval v p = 0) (hp : p ∈ restrict_degree σ K (fintype.card K - 1)) :
p = 0 :=
let p' : R σ K := ⟨p, hp⟩ in
have p' ∈ (evalᵢ σ K).ker := funext h,
show p'.1 = (0 : R σ K).1, from congr_arg _ $ by rwa [ker_evalₗ, mem_bot] at this
end mv_polynomial
|
4f915fcac9db9e7d1cff587f9aedda60cbc5caa9 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/analysis/normed_space/multilinear.lean | aba5f7d1d9f1cfbff5314b3dca83cd86702cb20f | [
"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 | 73,390 | lean | /-
Copyright (c) 2020 Sébastien Gouëzel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sébastien Gouëzel
-/
import analysis.normed_space.operator_norm
import topology.algebra.module.multilinear
/-!
# Operator norm on the space of continuous multilinear maps
When `f` is a continuous multilinear map in finitely many variables, we define its norm `‖f‖` as the
smallest number such that `‖f m‖ ≤ ‖f‖ * ∏ i, ‖m i‖` for all `m`.
We show that it is indeed a norm, and prove its basic properties.
## Main results
Let `f` be a multilinear map in finitely many variables.
* `exists_bound_of_continuous` asserts that, if `f` is continuous, then there exists `C > 0`
with `‖f m‖ ≤ C * ∏ i, ‖m i‖` for all `m`.
* `continuous_of_bound`, conversely, asserts that this bound implies continuity.
* `mk_continuous` constructs the associated continuous multilinear map.
Let `f` be a continuous multilinear map in finitely many variables.
* `‖f‖` is its norm, i.e., the smallest number such that `‖f m‖ ≤ ‖f‖ * ∏ i, ‖m i‖` for
all `m`.
* `le_op_norm f m` asserts the fundamental inequality `‖f m‖ ≤ ‖f‖ * ∏ i, ‖m i‖`.
* `norm_image_sub_le f m₁ m₂` gives a control of the difference `f m₁ - f m₂` in terms of
`‖f‖` and `‖m₁ - m₂‖`.
We also register isomorphisms corresponding to currying or uncurrying variables, transforming a
continuous multilinear function `f` in `n+1` variables into a continuous linear function taking
values in continuous multilinear functions in `n` variables, and also into a continuous multilinear
function in `n` variables taking values in continuous linear functions. These operations are called
`f.curry_left` and `f.curry_right` respectively (with inverses `f.uncurry_left` and
`f.uncurry_right`). They induce continuous linear equivalences between spaces of
continuous multilinear functions in `n+1` variables and spaces of continuous linear functions into
continuous multilinear functions in `n` variables (resp. continuous multilinear functions in `n`
variables taking values in continuous linear functions), called respectively
`continuous_multilinear_curry_left_equiv` and `continuous_multilinear_curry_right_equiv`.
## Implementation notes
We mostly follow the API (and the proofs) of `operator_norm.lean`, with the additional complexity
that we should deal with multilinear maps in several variables. The currying/uncurrying
constructions are based on those in `multilinear.lean`.
From the mathematical point of view, all the results follow from the results on operator norm in
one variable, by applying them to one variable after the other through currying. However, this
is only well defined when there is an order on the variables (for instance on `fin n`) although
the final result is independent of the order. While everything could be done following this
approach, it turns out that direct proofs are easier and more efficient.
-/
noncomputable theory
open_locale classical big_operators nnreal
open finset metric
local attribute [instance, priority 1001]
add_comm_group.to_add_comm_monoid normed_add_comm_group.to_add_comm_group normed_space.to_module'
/-!
### Type variables
We use the following type variables in this file:
* `𝕜` : a `nontrivially_normed_field`;
* `ι`, `ι'` : finite index types with decidable equality;
* `E`, `E₁` : families of normed vector spaces over `𝕜` indexed by `i : ι`;
* `E'` : a family of normed vector spaces over `𝕜` indexed by `i' : ι'`;
* `Ei` : a family of normed vector spaces over `𝕜` indexed by `i : fin (nat.succ n)`;
* `G`, `G'` : normed vector spaces over `𝕜`.
-/
universes u v v' wE wE₁ wE' wEi wG wG'
variables {𝕜 : Type u} {ι : Type v} {ι' : Type v'} {n : ℕ}
{E : ι → Type wE} {E₁ : ι → Type wE₁} {E' : ι' → Type wE'} {Ei : fin n.succ → Type wEi}
{G : Type wG} {G' : Type wG'}
[decidable_eq ι] [fintype ι] [decidable_eq ι'] [fintype ι'] [nontrivially_normed_field 𝕜]
[Π i, normed_add_comm_group (E i)] [Π i, normed_space 𝕜 (E i)]
[Π i, normed_add_comm_group (E₁ i)] [Π i, normed_space 𝕜 (E₁ i)]
[Π i, normed_add_comm_group (E' i)] [Π i, normed_space 𝕜 (E' i)]
[Π i, normed_add_comm_group (Ei i)] [Π i, normed_space 𝕜 (Ei i)]
[normed_add_comm_group G] [normed_space 𝕜 G] [normed_add_comm_group G'] [normed_space 𝕜 G']
/-!
### Continuity properties of multilinear maps
We relate continuity of multilinear maps to the inequality `‖f m‖ ≤ C * ∏ i, ‖m i‖`, in
both directions. Along the way, we prove useful bounds on the difference `‖f m₁ - f m₂‖`.
-/
namespace multilinear_map
variable (f : multilinear_map 𝕜 E G)
/-- If a multilinear map in finitely many variables on normed spaces satisfies the inequality
`‖f m‖ ≤ C * ∏ i, ‖m i‖` on a shell `ε i / ‖c i‖ < ‖m i‖ < ε i` for some positive numbers `ε i`
and elements `c i : 𝕜`, `1 < ‖c i‖`, then it satisfies this inequality for all `m`. -/
lemma bound_of_shell {ε : ι → ℝ} {C : ℝ} (hε : ∀ i, 0 < ε i) {c : ι → 𝕜} (hc : ∀ i, 1 < ‖c i‖)
(hf : ∀ m : Π i, E i, (∀ i, ε i / ‖c i‖ ≤ ‖m i‖) → (∀ i, ‖m i‖ < ε i) → ‖f m‖ ≤ C * ∏ i, ‖m i‖)
(m : Π i, E i) : ‖f m‖ ≤ C * ∏ i, ‖m i‖ :=
begin
rcases em (∃ i, m i = 0) with ⟨i, hi⟩|hm; [skip, push_neg at hm],
{ simp [f.map_coord_zero i hi, prod_eq_zero (mem_univ i), hi] },
choose δ hδ0 hδm_lt hle_δm hδinv using λ i, rescale_to_shell (hc i) (hε i) (hm i),
have hδ0 : 0 < ∏ i, ‖δ i‖, from prod_pos (λ i _, norm_pos_iff.2 (hδ0 i)),
simpa [map_smul_univ, norm_smul, prod_mul_distrib, mul_left_comm C, mul_le_mul_left hδ0]
using hf (λ i, δ i • m i) hle_δm hδm_lt,
end
/-- If a multilinear map in finitely many variables on normed spaces is continuous, then it
satisfies the inequality `‖f m‖ ≤ C * ∏ i, ‖m i‖`, for some `C` which can be chosen to be
positive. -/
theorem exists_bound_of_continuous (hf : continuous f) :
∃ (C : ℝ), 0 < C ∧ (∀ m, ‖f m‖ ≤ C * ∏ i, ‖m i‖) :=
begin
casesI is_empty_or_nonempty ι,
{ refine ⟨‖f 0‖ + 1, add_pos_of_nonneg_of_pos (norm_nonneg _) zero_lt_one, λ m, _⟩,
obtain rfl : m = 0, from funext (is_empty.elim ‹_›),
simp [univ_eq_empty, zero_le_one] },
obtain ⟨ε : ℝ, ε0 : 0 < ε, hε : ∀ m : Π i, E i, ‖m - 0‖ < ε → ‖f m - f 0‖ < 1⟩ :=
normed_add_comm_group.tendsto_nhds_nhds.1 (hf.tendsto 0) 1 zero_lt_one,
simp only [sub_zero, f.map_zero] at hε,
rcases normed_field.exists_one_lt_norm 𝕜 with ⟨c, hc⟩,
have : 0 < (‖c‖ / ε) ^ fintype.card ι, from pow_pos (div_pos (zero_lt_one.trans hc) ε0) _,
refine ⟨_, this, _⟩,
refine f.bound_of_shell (λ _, ε0) (λ _, hc) (λ m hcm hm, _),
refine (hε m ((pi_norm_lt_iff ε0).2 hm)).le.trans _,
rw [← div_le_iff' this, one_div, ← inv_pow, inv_div, fintype.card, ← prod_const],
exact prod_le_prod (λ _ _, div_nonneg ε0.le (norm_nonneg _)) (λ i _, hcm i)
end
/-- If `f` satisfies a boundedness property around `0`, one can deduce a bound on `f m₁ - f m₂`
using the multilinearity. Here, we give a precise but hard to use version. See
`norm_image_sub_le_of_bound` for a less precise but more usable version. The bound reads
`‖f m - f m'‖ ≤
C * ‖m 1 - m' 1‖ * max ‖m 2‖ ‖m' 2‖ * max ‖m 3‖ ‖m' 3‖ * ... * max ‖m n‖ ‖m' n‖ + ...`,
where the other terms in the sum are the same products where `1` is replaced by any `i`. -/
lemma norm_image_sub_le_of_bound' {C : ℝ} (hC : 0 ≤ C)
(H : ∀ m, ‖f m‖ ≤ C * ∏ i, ‖m i‖) (m₁ m₂ : Πi, E i) :
‖f m₁ - f m₂‖ ≤
C * ∑ i, ∏ j, if j = i then ‖m₁ i - m₂ i‖ else max ‖m₁ j‖ ‖m₂ j‖ :=
begin
have A : ∀(s : finset ι), ‖f m₁ - f (s.piecewise m₂ m₁)‖
≤ C * ∑ i in s, ∏ j, if j = i then ‖m₁ i - m₂ i‖ else max ‖m₁ j‖ ‖m₂ j‖,
{ refine finset.induction (by simp) _,
assume i s his Hrec,
have I : ‖f (s.piecewise m₂ m₁) - f ((insert i s).piecewise m₂ m₁)‖
≤ C * ∏ j, if j = i then ‖m₁ i - m₂ i‖ else max ‖m₁ j‖ ‖m₂ j‖,
{ have A : ((insert i s).piecewise m₂ m₁)
= function.update (s.piecewise m₂ m₁) i (m₂ i) := s.piecewise_insert _ _ _,
have B : s.piecewise m₂ m₁ = function.update (s.piecewise m₂ m₁) i (m₁ i),
{ ext j,
by_cases h : j = i,
{ rw h, simp [his] },
{ simp [h] } },
rw [B, A, ← f.map_sub],
apply le_trans (H _) (mul_le_mul_of_nonneg_left _ hC),
refine prod_le_prod (λj hj, norm_nonneg _) (λj hj, _),
by_cases h : j = i,
{ rw h, simp },
{ by_cases h' : j ∈ s;
simp [h', h, le_refl] } },
calc ‖f m₁ - f ((insert i s).piecewise m₂ m₁)‖ ≤
‖f m₁ - f (s.piecewise m₂ m₁)‖ + ‖f (s.piecewise m₂ m₁) - f ((insert i s).piecewise m₂ m₁)‖ :
by { rw [← dist_eq_norm, ← dist_eq_norm, ← dist_eq_norm], exact dist_triangle _ _ _ }
... ≤ (C * ∑ i in s, ∏ j, if j = i then ‖m₁ i - m₂ i‖ else max ‖m₁ j‖ ‖m₂ j‖)
+ C * ∏ j, if j = i then ‖m₁ i - m₂ i‖ else max ‖m₁ j‖ ‖m₂ j‖ :
add_le_add Hrec I
... = C * ∑ i in insert i s, ∏ j, if j = i then ‖m₁ i - m₂ i‖ else max ‖m₁ j‖ ‖m₂ j‖ :
by simp [his, add_comm, left_distrib] },
convert A univ,
simp
end
/-- If `f` satisfies a boundedness property around `0`, one can deduce a bound on `f m₁ - f m₂`
using the multilinearity. Here, we give a usable but not very precise version. See
`norm_image_sub_le_of_bound'` for a more precise but less usable version. The bound is
`‖f m - f m'‖ ≤ C * card ι * ‖m - m'‖ * (max ‖m‖ ‖m'‖) ^ (card ι - 1)`. -/
lemma norm_image_sub_le_of_bound {C : ℝ} (hC : 0 ≤ C)
(H : ∀ m, ‖f m‖ ≤ C * ∏ i, ‖m i‖) (m₁ m₂ : Πi, E i) :
‖f m₁ - f m₂‖ ≤ C * (fintype.card ι) * (max ‖m₁‖ ‖m₂‖) ^ (fintype.card ι - 1) * ‖m₁ - m₂‖ :=
begin
have A : ∀ (i : ι), ∏ j, (if j = i then ‖m₁ i - m₂ i‖ else max ‖m₁ j‖ ‖m₂ j‖)
≤ ‖m₁ - m₂‖ * (max ‖m₁‖ ‖m₂‖) ^ (fintype.card ι - 1),
{ assume i,
calc ∏ j, (if j = i then ‖m₁ i - m₂ i‖ else max ‖m₁ j‖ ‖m₂ j‖)
≤ ∏ j : ι, function.update (λ j, max ‖m₁‖ ‖m₂‖) i (‖m₁ - m₂‖) j :
begin
apply prod_le_prod,
{ assume j hj, by_cases h : j = i; simp [h, norm_nonneg] },
{ assume j hj,
by_cases h : j = i,
{ rw h, simp, exact norm_le_pi_norm (m₁ - m₂) i },
{ simp [h, max_le_max, norm_le_pi_norm (_ : Π i, E i)] } }
end
... = ‖m₁ - m₂‖ * (max ‖m₁‖ ‖m₂‖) ^ (fintype.card ι - 1) :
by { rw prod_update_of_mem (finset.mem_univ _), simp [card_univ_diff] } },
calc
‖f m₁ - f m₂‖
≤ C * ∑ i, ∏ j, if j = i then ‖m₁ i - m₂ i‖ else max ‖m₁ j‖ ‖m₂ j‖ :
f.norm_image_sub_le_of_bound' hC H m₁ m₂
... ≤ C * ∑ i, ‖m₁ - m₂‖ * (max ‖m₁‖ ‖m₂‖) ^ (fintype.card ι - 1) :
mul_le_mul_of_nonneg_left (sum_le_sum (λi hi, A i)) hC
... = C * (fintype.card ι) * (max ‖m₁‖ ‖m₂‖) ^ (fintype.card ι - 1) * ‖m₁ - m₂‖ :
by { rw [sum_const, card_univ, nsmul_eq_mul], ring }
end
/-- If a multilinear map satisfies an inequality `‖f m‖ ≤ C * ∏ i, ‖m i‖`, then it is
continuous. -/
theorem continuous_of_bound (C : ℝ) (H : ∀ m, ‖f m‖ ≤ C * ∏ i, ‖m i‖) :
continuous f :=
begin
let D := max C 1,
have D_pos : 0 ≤ D := le_trans zero_le_one (le_max_right _ _),
replace H : ∀ m, ‖f m‖ ≤ D * ∏ i, ‖m i‖,
{ assume m,
apply le_trans (H m) (mul_le_mul_of_nonneg_right (le_max_left _ _) _),
exact prod_nonneg (λ(i : ι) hi, norm_nonneg (m i)) },
refine continuous_iff_continuous_at.2 (λm, _),
refine continuous_at_of_locally_lipschitz zero_lt_one
(D * (fintype.card ι) * (‖m‖ + 1) ^ (fintype.card ι - 1)) (λm' h', _),
rw [dist_eq_norm, dist_eq_norm],
have : 0 ≤ (max ‖m'‖ ‖m‖), by simp,
have : (max ‖m'‖ ‖m‖) ≤ ‖m‖ + 1,
by simp [zero_le_one, norm_le_of_mem_closed_ball (le_of_lt h'), -add_comm],
calc
‖f m' - f m‖
≤ D * (fintype.card ι) * (max ‖m'‖ ‖m‖) ^ (fintype.card ι - 1) * ‖m' - m‖ :
f.norm_image_sub_le_of_bound D_pos H m' m
... ≤ D * (fintype.card ι) * (‖m‖ + 1) ^ (fintype.card ι - 1) * ‖m' - m‖ :
by apply_rules [mul_le_mul_of_nonneg_right, mul_le_mul_of_nonneg_left, mul_nonneg,
norm_nonneg, nat.cast_nonneg, pow_le_pow_of_le_left]
end
/-- Constructing a continuous multilinear map from a multilinear map satisfying a boundedness
condition. -/
def mk_continuous (C : ℝ) (H : ∀ m, ‖f m‖ ≤ C * ∏ i, ‖m i‖) :
continuous_multilinear_map 𝕜 E G :=
{ cont := f.continuous_of_bound C H, ..f }
@[simp] lemma coe_mk_continuous (C : ℝ) (H : ∀ m, ‖f m‖ ≤ C * ∏ i, ‖m i‖) :
⇑(f.mk_continuous C H) = f :=
rfl
/-- Given a multilinear map in `n` variables, if one restricts it to `k` variables putting `z` on
the other coordinates, then the resulting restricted function satisfies an inequality
`‖f.restr v‖ ≤ C * ‖z‖^(n-k) * Π ‖v i‖` if the original function satisfies `‖f v‖ ≤ C * Π ‖v i‖`. -/
lemma restr_norm_le {k n : ℕ} (f : (multilinear_map 𝕜 (λ i : fin n, G) G' : _))
(s : finset (fin n)) (hk : s.card = k) (z : G) {C : ℝ}
(H : ∀ m, ‖f m‖ ≤ C * ∏ i, ‖m i‖) (v : fin k → G) :
‖f.restr s hk z v‖ ≤ C * ‖z‖ ^ (n - k) * ∏ i, ‖v i‖ :=
begin
rw [mul_right_comm, mul_assoc],
convert H _ using 2,
simp only [apply_dite norm, fintype.prod_dite, prod_const (‖z‖), finset.card_univ,
fintype.card_of_subtype sᶜ (λ x, mem_compl), card_compl, fintype.card_fin, hk, mk_coe,
← (s.order_iso_of_fin hk).symm.bijective.prod_comp (λ x, ‖v x‖)],
refl
end
end multilinear_map
/-!
### Continuous multilinear maps
We define the norm `‖f‖` of a continuous multilinear map `f` in finitely many variables as the
smallest number such that `‖f m‖ ≤ ‖f‖ * ∏ i, ‖m i‖` for all `m`. We show that this
defines a normed space structure on `continuous_multilinear_map 𝕜 E G`.
-/
namespace continuous_multilinear_map
variables (c : 𝕜) (f g : continuous_multilinear_map 𝕜 E G) (m : Πi, E i)
theorem bound : ∃ (C : ℝ), 0 < C ∧ (∀ m, ‖f m‖ ≤ C * ∏ i, ‖m i‖) :=
f.to_multilinear_map.exists_bound_of_continuous f.2
open real
/-- The operator norm of a continuous multilinear map is the inf of all its bounds. -/
def op_norm := Inf {c | 0 ≤ (c : ℝ) ∧ ∀ m, ‖f m‖ ≤ c * ∏ i, ‖m i‖}
instance has_op_norm : has_norm (continuous_multilinear_map 𝕜 E G) := ⟨op_norm⟩
/-- An alias of `continuous_multilinear_map.has_op_norm` with non-dependent types to help typeclass
search. -/
instance has_op_norm' : has_norm (continuous_multilinear_map 𝕜 (λ (i : ι), G) G') :=
continuous_multilinear_map.has_op_norm
lemma norm_def : ‖f‖ = Inf {c | 0 ≤ (c : ℝ) ∧ ∀ m, ‖f m‖ ≤ c * ∏ i, ‖m i‖} := rfl
-- So that invocations of `le_cInf` make sense: we show that the set of
-- bounds is nonempty and bounded below.
lemma bounds_nonempty {f : continuous_multilinear_map 𝕜 E G} :
∃ c, c ∈ {c | 0 ≤ c ∧ ∀ m, ‖f m‖ ≤ c * ∏ i, ‖m i‖} :=
let ⟨M, hMp, hMb⟩ := f.bound in ⟨M, le_of_lt hMp, hMb⟩
lemma bounds_bdd_below {f : continuous_multilinear_map 𝕜 E G} :
bdd_below {c | 0 ≤ c ∧ ∀ m, ‖f m‖ ≤ c * ∏ i, ‖m i‖} :=
⟨0, λ _ ⟨hn, _⟩, hn⟩
lemma op_norm_nonneg : 0 ≤ ‖f‖ :=
le_cInf bounds_nonempty (λ _ ⟨hx, _⟩, hx)
/-- The fundamental property of the operator norm of a continuous multilinear map:
`‖f m‖` is bounded by `‖f‖` times the product of the `‖m i‖`. -/
theorem le_op_norm : ‖f m‖ ≤ ‖f‖ * ∏ i, ‖m i‖ :=
begin
have A : 0 ≤ ∏ i, ‖m i‖ := prod_nonneg (λj hj, norm_nonneg _),
cases A.eq_or_lt with h hlt,
{ rcases prod_eq_zero_iff.1 h.symm with ⟨i, _, hi⟩,
rw norm_eq_zero at hi,
have : f m = 0 := f.map_coord_zero i hi,
rw [this, norm_zero],
exact mul_nonneg (op_norm_nonneg f) A },
{ rw [← div_le_iff hlt],
apply le_cInf bounds_nonempty,
rintro c ⟨_, hc⟩, rw [div_le_iff hlt], apply hc }
end
theorem le_of_op_norm_le {C : ℝ} (h : ‖f‖ ≤ C) : ‖f m‖ ≤ C * ∏ i, ‖m i‖ :=
(f.le_op_norm m).trans $ mul_le_mul_of_nonneg_right h (prod_nonneg $ λ i _, norm_nonneg (m i))
lemma ratio_le_op_norm : ‖f m‖ / ∏ i, ‖m i‖ ≤ ‖f‖ :=
div_le_of_nonneg_of_le_mul (prod_nonneg $ λ i _, norm_nonneg _) (op_norm_nonneg _) (f.le_op_norm m)
/-- The image of the unit ball under a continuous multilinear map is bounded. -/
lemma unit_le_op_norm (h : ‖m‖ ≤ 1) : ‖f m‖ ≤ ‖f‖ :=
calc
‖f m‖ ≤ ‖f‖ * ∏ i, ‖m i‖ : f.le_op_norm m
... ≤ ‖f‖ * ∏ i : ι, 1 :
mul_le_mul_of_nonneg_left (prod_le_prod (λi hi, norm_nonneg _)
(λi hi, le_trans (norm_le_pi_norm (_ : Π i, E i) _) h)) (op_norm_nonneg f)
... = ‖f‖ : by simp
/-- If one controls the norm of every `f x`, then one controls the norm of `f`. -/
lemma op_norm_le_bound {M : ℝ} (hMp: 0 ≤ M) (hM : ∀ m, ‖f m‖ ≤ M * ∏ i, ‖m i‖) :
‖f‖ ≤ M :=
cInf_le bounds_bdd_below ⟨hMp, hM⟩
/-- The operator norm satisfies the triangle inequality. -/
theorem op_norm_add_le : ‖f + g‖ ≤ ‖f‖ + ‖g‖ :=
cInf_le bounds_bdd_below
⟨add_nonneg (op_norm_nonneg _) (op_norm_nonneg _), λ x, by { rw add_mul,
exact norm_add_le_of_le (le_op_norm _ _) (le_op_norm _ _) }⟩
lemma op_norm_zero : ‖(0 : continuous_multilinear_map 𝕜 E G)‖ = 0 :=
(op_norm_nonneg _).antisymm' $ op_norm_le_bound 0 le_rfl $ λ m, by simp
/-- A continuous linear map is zero iff its norm vanishes. -/
theorem op_norm_zero_iff : ‖f‖ = 0 ↔ f = 0 :=
⟨λ h, by { ext m, simpa [h] using f.le_op_norm m }, by { rintro rfl, exact op_norm_zero }⟩
section
variables {𝕜' : Type*} [normed_field 𝕜'] [normed_space 𝕜' G] [smul_comm_class 𝕜 𝕜' G]
lemma op_norm_smul_le (c : 𝕜') : ‖c • f‖ ≤ ‖c‖ * ‖f‖ :=
(c • f).op_norm_le_bound
(mul_nonneg (norm_nonneg _) (op_norm_nonneg _))
begin
intro m,
erw [norm_smul, mul_assoc],
exact mul_le_mul_of_nonneg_left (le_op_norm _ _) (norm_nonneg _)
end
lemma op_norm_neg : ‖-f‖ = ‖f‖ := by { rw norm_def, apply congr_arg, ext, simp }
/-- Continuous multilinear maps themselves form a normed space with respect to
the operator norm. -/
instance normed_add_comm_group : normed_add_comm_group (continuous_multilinear_map 𝕜 E G) :=
add_group_norm.to_normed_add_comm_group
{ to_fun := norm,
map_zero' := op_norm_zero,
neg' := op_norm_neg,
add_le' := op_norm_add_le,
eq_zero_of_map_eq_zero' := λ f, f.op_norm_zero_iff.1 }
/-- An alias of `continuous_multilinear_map.normed_add_comm_group` with non-dependent types to help
typeclass search. -/
instance normed_add_comm_group' :
normed_add_comm_group (continuous_multilinear_map 𝕜 (λ i : ι, G) G') :=
continuous_multilinear_map.normed_add_comm_group
instance normed_space : normed_space 𝕜' (continuous_multilinear_map 𝕜 E G) :=
⟨λ c f, f.op_norm_smul_le c⟩
/-- An alias of `continuous_multilinear_map.normed_space` with non-dependent types to help typeclass
search. -/
instance normed_space' : normed_space 𝕜' (continuous_multilinear_map 𝕜 (λ i : ι, G') G) :=
continuous_multilinear_map.normed_space
theorem le_op_norm_mul_prod_of_le {b : ι → ℝ} (hm : ∀ i, ‖m i‖ ≤ b i) : ‖f m‖ ≤ ‖f‖ * ∏ i, b i :=
(f.le_op_norm m).trans $ mul_le_mul_of_nonneg_left
(prod_le_prod (λ _ _, norm_nonneg _) (λ i _, hm i)) (norm_nonneg f)
theorem le_op_norm_mul_pow_card_of_le {b : ℝ} (hm : ∀ i, ‖m i‖ ≤ b) :
‖f m‖ ≤ ‖f‖ * b ^ fintype.card ι :=
by simpa only [prod_const] using f.le_op_norm_mul_prod_of_le m hm
theorem le_op_norm_mul_pow_of_le {Ei : fin n → Type*} [Π i, normed_add_comm_group (Ei i)]
[Π i, normed_space 𝕜 (Ei i)] (f : continuous_multilinear_map 𝕜 Ei G) (m : Π i, Ei i)
{b : ℝ} (hm : ‖m‖ ≤ b) :
‖f m‖ ≤ ‖f‖ * b ^ n :=
by simpa only [fintype.card_fin]
using f.le_op_norm_mul_pow_card_of_le m (λ i, (norm_le_pi_norm m i).trans hm)
/-- The fundamental property of the operator norm of a continuous multilinear map:
`‖f m‖` is bounded by `‖f‖` times the product of the `‖m i‖`, `nnnorm` version. -/
theorem le_op_nnnorm : ‖f m‖₊ ≤ ‖f‖₊ * ∏ i, ‖m i‖₊ :=
nnreal.coe_le_coe.1 $ by { push_cast, exact f.le_op_norm m }
theorem le_of_op_nnnorm_le {C : ℝ≥0} (h : ‖f‖₊ ≤ C) : ‖f m‖₊ ≤ C * ∏ i, ‖m i‖₊ :=
(f.le_op_nnnorm m).trans $ mul_le_mul' h le_rfl
lemma op_norm_prod (f : continuous_multilinear_map 𝕜 E G) (g : continuous_multilinear_map 𝕜 E G') :
‖f.prod g‖ = max (‖f‖) (‖g‖) :=
le_antisymm
(op_norm_le_bound _ (norm_nonneg (f, g)) (λ m,
have H : 0 ≤ ∏ i, ‖m i‖, from prod_nonneg $ λ _ _, norm_nonneg _,
by simpa only [prod_apply, prod.norm_def, max_mul_of_nonneg, H]
using max_le_max (f.le_op_norm m) (g.le_op_norm m))) $
max_le
(f.op_norm_le_bound (norm_nonneg _) $ λ m, (le_max_left _ _).trans ((f.prod g).le_op_norm _))
(g.op_norm_le_bound (norm_nonneg _) $ λ m, (le_max_right _ _).trans ((f.prod g).le_op_norm _))
lemma norm_pi {ι' : Type v'} [fintype ι'] {E' : ι' → Type wE'} [Π i', normed_add_comm_group (E' i')]
[Π i', normed_space 𝕜 (E' i')] (f : Π i', continuous_multilinear_map 𝕜 E (E' i')) :
‖pi f‖ = ‖f‖ :=
begin
apply le_antisymm,
{ refine (op_norm_le_bound _ (norm_nonneg f) (λ m, _)),
dsimp,
rw pi_norm_le_iff_of_nonneg,
exacts [λ i, (f i).le_of_op_norm_le m (norm_le_pi_norm f i),
mul_nonneg (norm_nonneg f) (prod_nonneg $ λ _ _, norm_nonneg _)] },
{ refine (pi_norm_le_iff_of_nonneg (norm_nonneg _)).2 (λ i, _),
refine (op_norm_le_bound _ (norm_nonneg _) (λ m, _)),
refine le_trans _ ((pi f).le_op_norm m),
convert norm_le_pi_norm (λ j, f j m) i }
end
section
variables (𝕜 E E' G G')
/-- `continuous_multilinear_map.prod` as a `linear_isometry_equiv`. -/
def prodL :
(continuous_multilinear_map 𝕜 E G) × (continuous_multilinear_map 𝕜 E G') ≃ₗᵢ[𝕜]
continuous_multilinear_map 𝕜 E (G × G') :=
{ to_fun := λ f, f.1.prod f.2,
inv_fun := λ f, ((continuous_linear_map.fst 𝕜 G G').comp_continuous_multilinear_map f,
(continuous_linear_map.snd 𝕜 G G').comp_continuous_multilinear_map f),
map_add' := λ f g, rfl,
map_smul' := λ c f, rfl,
left_inv := λ f, by ext; refl,
right_inv := λ f, by ext; refl,
norm_map' := λ f, op_norm_prod f.1 f.2 }
/-- `continuous_multilinear_map.pi` as a `linear_isometry_equiv`. -/
def piₗᵢ {ι' : Type v'} [fintype ι'] {E' : ι' → Type wE'} [Π i', normed_add_comm_group (E' i')]
[Π i', normed_space 𝕜 (E' i')] :
@linear_isometry_equiv 𝕜 𝕜 _ _ (ring_hom.id 𝕜) _ _ _
(Π i', continuous_multilinear_map 𝕜 E (E' i')) (continuous_multilinear_map 𝕜 E (Π i, E' i)) _ _
(@pi.module ι' _ 𝕜 _ _ (λ i', infer_instance)) _ :=
{ to_linear_equiv :=
-- note: `pi_linear_equiv` does not unify correctly here, presumably due to issues with dependent
-- typeclass arguments.
{ map_add' := λ f g, rfl,
map_smul' := λ c f, rfl,
.. pi_equiv, },
norm_map' := norm_pi }
end
end
section restrict_scalars
variables {𝕜' : Type*} [nontrivially_normed_field 𝕜'] [normed_algebra 𝕜' 𝕜]
variables [normed_space 𝕜' G] [is_scalar_tower 𝕜' 𝕜 G]
variables [Π i, normed_space 𝕜' (E i)] [∀ i, is_scalar_tower 𝕜' 𝕜 (E i)]
@[simp] lemma norm_restrict_scalars : ‖f.restrict_scalars 𝕜'‖ = ‖f‖ :=
by simp only [norm_def, coe_restrict_scalars]
variable (𝕜')
/-- `continuous_multilinear_map.restrict_scalars` as a `continuous_multilinear_map`. -/
def restrict_scalars_linear :
continuous_multilinear_map 𝕜 E G →L[𝕜'] continuous_multilinear_map 𝕜' E G :=
linear_map.mk_continuous
{ to_fun := restrict_scalars 𝕜',
map_add' := λ m₁ m₂, rfl,
map_smul' := λ c m, rfl } 1 $ λ f, by simp
variable {𝕜'}
lemma continuous_restrict_scalars :
continuous (restrict_scalars 𝕜' : continuous_multilinear_map 𝕜 E G →
continuous_multilinear_map 𝕜' E G) :=
(restrict_scalars_linear 𝕜').continuous
end restrict_scalars
/-- The difference `f m₁ - f m₂` is controlled in terms of `‖f‖` and `‖m₁ - m₂‖`, precise version.
For a less precise but more usable version, see `norm_image_sub_le`. The bound reads
`‖f m - f m'‖ ≤
‖f‖ * ‖m 1 - m' 1‖ * max ‖m 2‖ ‖m' 2‖ * max ‖m 3‖ ‖m' 3‖ * ... * max ‖m n‖ ‖m' n‖ + ...`,
where the other terms in the sum are the same products where `1` is replaced by any `i`.-/
lemma norm_image_sub_le' (m₁ m₂ : Πi, E i) :
‖f m₁ - f m₂‖ ≤
‖f‖ * ∑ i, ∏ j, if j = i then ‖m₁ i - m₂ i‖ else max ‖m₁ j‖ ‖m₂ j‖ :=
f.to_multilinear_map.norm_image_sub_le_of_bound' (norm_nonneg _) f.le_op_norm _ _
/-- The difference `f m₁ - f m₂` is controlled in terms of `‖f‖` and `‖m₁ - m₂‖`, less precise
version. For a more precise but less usable version, see `norm_image_sub_le'`.
The bound is `‖f m - f m'‖ ≤ ‖f‖ * card ι * ‖m - m'‖ * (max ‖m‖ ‖m'‖) ^ (card ι - 1)`.-/
lemma norm_image_sub_le (m₁ m₂ : Πi, E i) :
‖f m₁ - f m₂‖ ≤ ‖f‖ * (fintype.card ι) * (max ‖m₁‖ ‖m₂‖) ^ (fintype.card ι - 1) * ‖m₁ - m₂‖ :=
f.to_multilinear_map.norm_image_sub_le_of_bound (norm_nonneg _) f.le_op_norm _ _
/-- Applying a multilinear map to a vector is continuous in both coordinates. -/
lemma continuous_eval :
continuous (λ p : continuous_multilinear_map 𝕜 E G × Π i, E i, p.1 p.2) :=
begin
apply continuous_iff_continuous_at.2 (λp, _),
apply continuous_at_of_locally_lipschitz zero_lt_one
((‖p‖ + 1) * (fintype.card ι) * (‖p‖ + 1) ^ (fintype.card ι - 1) + ∏ i, ‖p.2 i‖)
(λq hq, _),
have : 0 ≤ (max ‖q.2‖ ‖p.2‖), by simp,
have : 0 ≤ ‖p‖ + 1 := zero_le_one.trans ((le_add_iff_nonneg_left 1).2 $ norm_nonneg p),
have A : ‖q‖ ≤ ‖p‖ + 1 := norm_le_of_mem_closed_ball hq.le,
have : (max ‖q.2‖ ‖p.2‖) ≤ ‖p‖ + 1 :=
(max_le_max (norm_snd_le q) (norm_snd_le p)).trans (by simp [A, -add_comm, zero_le_one]),
have : ∀ (i : ι), i ∈ univ → 0 ≤ ‖p.2 i‖ := λ i hi, norm_nonneg _,
calc dist (q.1 q.2) (p.1 p.2)
≤ dist (q.1 q.2) (q.1 p.2) + dist (q.1 p.2) (p.1 p.2) : dist_triangle _ _ _
... = ‖q.1 q.2 - q.1 p.2‖ + ‖q.1 p.2 - p.1 p.2‖ : by rw [dist_eq_norm, dist_eq_norm]
... ≤ ‖q.1‖ * (fintype.card ι) * (max ‖q.2‖ ‖p.2‖) ^ (fintype.card ι - 1) * ‖q.2 - p.2‖
+ ‖q.1 - p.1‖ * ∏ i, ‖p.2 i‖ :
add_le_add (norm_image_sub_le _ _ _) ((q.1 - p.1).le_op_norm p.2)
... ≤ (‖p‖ + 1) * (fintype.card ι) * (‖p‖ + 1) ^ (fintype.card ι - 1) * ‖q - p‖
+ ‖q - p‖ * ∏ i, ‖p.2 i‖ :
by apply_rules [add_le_add, mul_le_mul, le_refl, le_trans (norm_fst_le q) A, nat.cast_nonneg,
mul_nonneg, pow_le_pow_of_le_left, pow_nonneg, norm_snd_le (q - p), norm_nonneg,
norm_fst_le (q - p), prod_nonneg]
... = ((‖p‖ + 1) * (fintype.card ι) * (‖p‖ + 1) ^ (fintype.card ι - 1)
+ (∏ i, ‖p.2 i‖)) * dist q p : by { rw dist_eq_norm, ring }
end
lemma continuous_eval_left (m : Π i, E i) :
continuous (λ p : continuous_multilinear_map 𝕜 E G, p m) :=
continuous_eval.comp (continuous_id.prod_mk continuous_const)
lemma has_sum_eval
{α : Type*} {p : α → continuous_multilinear_map 𝕜 E G} {q : continuous_multilinear_map 𝕜 E G}
(h : has_sum p q) (m : Π i, E i) : has_sum (λ a, p a m) (q m) :=
begin
dsimp [has_sum] at h ⊢,
convert ((continuous_eval_left m).tendsto _).comp h,
ext s,
simp
end
lemma tsum_eval {α : Type*} {p : α → continuous_multilinear_map 𝕜 E G} (hp : summable p)
(m : Π i, E i) : (∑' a, p a) m = ∑' a, p a m :=
(has_sum_eval hp.has_sum m).tsum_eq.symm
open_locale topological_space
open filter
/-- If the target space is complete, the space of continuous multilinear maps with its norm is also
complete. The proof is essentially the same as for the space of continuous linear maps (modulo the
addition of `finset.prod` where needed. The duplication could be avoided by deducing the linear
case from the multilinear case via a currying isomorphism. However, this would mess up imports,
and it is more satisfactory to have the simplest case as a standalone proof. -/
instance [complete_space G] : complete_space (continuous_multilinear_map 𝕜 E G) :=
begin
have nonneg : ∀ (v : Π i, E i), 0 ≤ ∏ i, ‖v i‖ :=
λ v, finset.prod_nonneg (λ i hi, norm_nonneg _),
-- We show that every Cauchy sequence converges.
refine metric.complete_of_cauchy_seq_tendsto (λ f hf, _),
-- We now expand out the definition of a Cauchy sequence,
rcases cauchy_seq_iff_le_tendsto_0.1 hf with ⟨b, b0, b_bound, b_lim⟩,
-- and establish that the evaluation at any point `v : Π i, E i` is Cauchy.
have cau : ∀ v, cauchy_seq (λ n, f n v),
{ assume v,
apply cauchy_seq_iff_le_tendsto_0.2 ⟨λ n, b n * ∏ i, ‖v i‖, λ n, _, _, _⟩,
{ exact mul_nonneg (b0 n) (nonneg v) },
{ assume n m N hn hm,
rw dist_eq_norm,
apply le_trans ((f n - f m).le_op_norm v) _,
exact mul_le_mul_of_nonneg_right (b_bound n m N hn hm) (nonneg v) },
{ simpa using b_lim.mul tendsto_const_nhds } },
-- We assemble the limits points of those Cauchy sequences
-- (which exist as `G` is complete)
-- into a function which we call `F`.
choose F hF using λv, cauchy_seq_tendsto_of_complete (cau v),
-- Next, we show that this `F` is multilinear,
let Fmult : multilinear_map 𝕜 E G :=
{ to_fun := F,
map_add' := λ v i x y, begin
have A := hF (function.update v i (x + y)),
have B := (hF (function.update v i x)).add (hF (function.update v i y)),
simp at A B,
exact tendsto_nhds_unique A B
end,
map_smul' := λ v i c x, begin
have A := hF (function.update v i (c • x)),
have B := filter.tendsto.smul (@tendsto_const_nhds _ ℕ _ c _) (hF (function.update v i x)),
simp at A B,
exact tendsto_nhds_unique A B
end },
-- and that `F` has norm at most `(b 0 + ‖f 0‖)`.
have Fnorm : ∀ v, ‖F v‖ ≤ (b 0 + ‖f 0‖) * ∏ i, ‖v i‖,
{ assume v,
have A : ∀ n, ‖f n v‖ ≤ (b 0 + ‖f 0‖) * ∏ i, ‖v i‖,
{ assume n,
apply le_trans ((f n).le_op_norm _) _,
apply mul_le_mul_of_nonneg_right _ (nonneg v),
calc ‖f n‖ = ‖(f n - f 0) + f 0‖ : by { congr' 1, abel }
... ≤ ‖f n - f 0‖ + ‖f 0‖ : norm_add_le _ _
... ≤ b 0 + ‖f 0‖ : begin
apply add_le_add_right,
simpa [dist_eq_norm] using b_bound n 0 0 (zero_le _) (zero_le _)
end },
exact le_of_tendsto (hF v).norm (eventually_of_forall A) },
-- Thus `F` is continuous, and we propose that as the limit point of our original Cauchy sequence.
let Fcont := Fmult.mk_continuous _ Fnorm,
use Fcont,
-- Our last task is to establish convergence to `F` in norm.
have : ∀ n, ‖f n - Fcont‖ ≤ b n,
{ assume n,
apply op_norm_le_bound _ (b0 n) (λ v, _),
have A : ∀ᶠ m in at_top, ‖(f n - f m) v‖ ≤ b n * ∏ i, ‖v i‖,
{ refine eventually_at_top.2 ⟨n, λ m hm, _⟩,
apply le_trans ((f n - f m).le_op_norm _) _,
exact mul_le_mul_of_nonneg_right (b_bound n m n le_rfl hm) (nonneg v) },
have B : tendsto (λ m, ‖(f n - f m) v‖) at_top (𝓝 (‖(f n - Fcont) v‖)) :=
tendsto.norm (tendsto_const_nhds.sub (hF v)),
exact le_of_tendsto B A },
erw tendsto_iff_norm_tendsto_zero,
exact squeeze_zero (λ n, norm_nonneg _) this b_lim,
end
end continuous_multilinear_map
/-- If a continuous multilinear map is constructed from a multilinear map via the constructor
`mk_continuous`, then its norm is bounded by the bound given to the constructor if it is
nonnegative. -/
lemma multilinear_map.mk_continuous_norm_le (f : multilinear_map 𝕜 E G) {C : ℝ} (hC : 0 ≤ C)
(H : ∀ m, ‖f m‖ ≤ C * ∏ i, ‖m i‖) : ‖f.mk_continuous C H‖ ≤ C :=
continuous_multilinear_map.op_norm_le_bound _ hC (λm, H m)
/-- If a continuous multilinear map is constructed from a multilinear map via the constructor
`mk_continuous`, then its norm is bounded by the bound given to the constructor if it is
nonnegative. -/
lemma multilinear_map.mk_continuous_norm_le' (f : multilinear_map 𝕜 E G) {C : ℝ}
(H : ∀ m, ‖f m‖ ≤ C * ∏ i, ‖m i‖) : ‖f.mk_continuous C H‖ ≤ max C 0 :=
continuous_multilinear_map.op_norm_le_bound _ (le_max_right _ _) $
λ m, (H m).trans $ mul_le_mul_of_nonneg_right (le_max_left _ _)
(prod_nonneg $ λ _ _, norm_nonneg _)
namespace continuous_multilinear_map
/-- Given a continuous multilinear map `f` on `n` variables (parameterized by `fin n`) and a subset
`s` of `k` of these variables, one gets a new continuous multilinear map on `fin k` by varying
these variables, and fixing the other ones equal to a given value `z`. It is denoted by
`f.restr s hk z`, where `hk` is a proof that the cardinality of `s` is `k`. The implicit
identification between `fin k` and `s` that we use is the canonical (increasing) bijection. -/
def restr {k n : ℕ} (f : (G [×n]→L[𝕜] G' : _)) (s : finset (fin n)) (hk : s.card = k) (z : G) :
G [×k]→L[𝕜] G' :=
(f.to_multilinear_map.restr s hk z).mk_continuous
(‖f‖ * ‖z‖^(n-k)) $ λ v, multilinear_map.restr_norm_le _ _ _ _ f.le_op_norm _
lemma norm_restr {k n : ℕ} (f : G [×n]→L[𝕜] G') (s : finset (fin n)) (hk : s.card = k) (z : G) :
‖f.restr s hk z‖ ≤ ‖f‖ * ‖z‖ ^ (n - k) :=
begin
apply multilinear_map.mk_continuous_norm_le,
exact mul_nonneg (norm_nonneg _) (pow_nonneg (norm_nonneg _) _)
end
section
variables {𝕜 ι} {A : Type*} [normed_comm_ring A] [normed_algebra 𝕜 A]
@[simp]
lemma norm_mk_pi_algebra_le [nonempty ι] :
‖continuous_multilinear_map.mk_pi_algebra 𝕜 ι A‖ ≤ 1 :=
begin
have := λ f, @op_norm_le_bound 𝕜 ι (λ i, A) A _ _ _ _ _ _ _ f _ zero_le_one,
refine this _ _,
intros m,
simp only [continuous_multilinear_map.mk_pi_algebra_apply, one_mul],
exact norm_prod_le' _ univ_nonempty _,
end
lemma norm_mk_pi_algebra_of_empty [is_empty ι] :
‖continuous_multilinear_map.mk_pi_algebra 𝕜 ι A‖ = ‖(1 : A)‖ :=
begin
apply le_antisymm,
{ have := λ f, @op_norm_le_bound 𝕜 ι (λ i, A) A _ _ _ _ _ _ _ f _ (norm_nonneg (1 : A)),
refine this _ _,
simp, },
{ convert ratio_le_op_norm _ (λ _, (1 : A)),
simp [eq_empty_of_is_empty (univ : finset ι)], },
end
@[simp] lemma norm_mk_pi_algebra [norm_one_class A] :
‖continuous_multilinear_map.mk_pi_algebra 𝕜 ι A‖ = 1 :=
begin
casesI is_empty_or_nonempty ι,
{ simp [norm_mk_pi_algebra_of_empty] },
{ refine le_antisymm norm_mk_pi_algebra_le _,
convert ratio_le_op_norm _ (λ _, 1); [skip, apply_instance],
simp },
end
end
section
variables {𝕜 n} {A : Type*} [normed_ring A] [normed_algebra 𝕜 A]
lemma norm_mk_pi_algebra_fin_succ_le :
‖continuous_multilinear_map.mk_pi_algebra_fin 𝕜 n.succ A‖ ≤ 1 :=
begin
have := λ f, @op_norm_le_bound 𝕜 (fin n.succ) (λ i, A) A _ _ _ _ _ _ _ f _ zero_le_one,
refine this _ _,
intros m,
simp only [continuous_multilinear_map.mk_pi_algebra_fin_apply, one_mul, list.of_fn_eq_map,
fin.prod_univ_def, multiset.coe_map, multiset.coe_prod],
refine (list.norm_prod_le' _).trans_eq _,
{ rw [ne.def, list.map_eq_nil, list.fin_range_eq_nil],
exact nat.succ_ne_zero _, },
rw list.map_map,
end
lemma norm_mk_pi_algebra_fin_le_of_pos (hn : 0 < n) :
‖continuous_multilinear_map.mk_pi_algebra_fin 𝕜 n A‖ ≤ 1 :=
begin
obtain ⟨n, rfl⟩ := nat.exists_eq_succ_of_ne_zero hn.ne',
exact norm_mk_pi_algebra_fin_succ_le
end
lemma norm_mk_pi_algebra_fin_zero :
‖continuous_multilinear_map.mk_pi_algebra_fin 𝕜 0 A‖ = ‖(1 : A)‖ :=
begin
refine le_antisymm _ _,
{ have := λ f, @op_norm_le_bound 𝕜 (fin 0) (λ i, A) A _ _ _ _ _ _ _ f _ (norm_nonneg (1 : A)),
refine this _ _,
simp, },
{ convert ratio_le_op_norm _ (λ _, (1 : A)),
simp }
end
@[simp] lemma norm_mk_pi_algebra_fin [norm_one_class A] :
‖continuous_multilinear_map.mk_pi_algebra_fin 𝕜 n A‖ = 1 :=
begin
cases n,
{ simp [norm_mk_pi_algebra_fin_zero] },
{ refine le_antisymm norm_mk_pi_algebra_fin_succ_le _,
convert ratio_le_op_norm _ (λ _, 1); [skip, apply_instance],
simp }
end
end
variables (𝕜 ι)
/-- The canonical continuous multilinear map on `𝕜^ι`, associating to `m` the product of all the
`m i` (multiplied by a fixed reference element `z` in the target module) -/
protected def mk_pi_field (z : G) : continuous_multilinear_map 𝕜 (λ(i : ι), 𝕜) G :=
multilinear_map.mk_continuous
(multilinear_map.mk_pi_ring 𝕜 ι z) (‖z‖)
(λ m, by simp only [multilinear_map.mk_pi_ring_apply, norm_smul, norm_prod,
mul_comm])
variables {𝕜 ι}
@[simp] lemma mk_pi_field_apply (z : G) (m : ι → 𝕜) :
(continuous_multilinear_map.mk_pi_field 𝕜 ι z : (ι → 𝕜) → G) m = (∏ i, m i) • z := rfl
lemma mk_pi_field_apply_one_eq_self (f : continuous_multilinear_map 𝕜 (λ(i : ι), 𝕜) G) :
continuous_multilinear_map.mk_pi_field 𝕜 ι (f (λi, 1)) = f :=
to_multilinear_map_inj f.to_multilinear_map.mk_pi_ring_apply_one_eq_self
@[simp] lemma norm_mk_pi_field (z : G) : ‖continuous_multilinear_map.mk_pi_field 𝕜 ι z‖ = ‖z‖ :=
(multilinear_map.mk_continuous_norm_le _ (norm_nonneg z) _).antisymm $
by simpa using (continuous_multilinear_map.mk_pi_field 𝕜 ι z).le_op_norm (λ _, 1)
lemma mk_pi_field_eq_iff {z₁ z₂ : G} :
continuous_multilinear_map.mk_pi_field 𝕜 ι z₁ = continuous_multilinear_map.mk_pi_field 𝕜 ι z₂ ↔
z₁ = z₂ :=
begin
rw [← to_multilinear_map_inj.eq_iff],
exact multilinear_map.mk_pi_ring_eq_iff
end
lemma mk_pi_field_zero :
continuous_multilinear_map.mk_pi_field 𝕜 ι (0 : G) = 0 :=
by ext; rw [mk_pi_field_apply, smul_zero, continuous_multilinear_map.zero_apply]
lemma mk_pi_field_eq_zero_iff (z : G) :
continuous_multilinear_map.mk_pi_field 𝕜 ι z = 0 ↔ z = 0 :=
by rw [← mk_pi_field_zero, mk_pi_field_eq_iff]
variables (𝕜 ι G)
/-- Continuous multilinear maps on `𝕜^n` with values in `G` are in bijection with `G`, as such a
continuous multilinear map is completely determined by its value on the constant vector made of
ones. We register this bijection as a linear isometry in
`continuous_multilinear_map.pi_field_equiv`. -/
protected def pi_field_equiv : G ≃ₗᵢ[𝕜] (continuous_multilinear_map 𝕜 (λ(i : ι), 𝕜) G) :=
{ to_fun := λ z, continuous_multilinear_map.mk_pi_field 𝕜 ι z,
inv_fun := λ f, f (λi, 1),
map_add' := λ z z', by { ext m, simp [smul_add] },
map_smul' := λ c z, by { ext m, simp [smul_smul, mul_comm] },
left_inv := λ z, by simp,
right_inv := λ f, f.mk_pi_field_apply_one_eq_self,
norm_map' := norm_mk_pi_field }
end continuous_multilinear_map
namespace continuous_linear_map
lemma norm_comp_continuous_multilinear_map_le (g : G →L[𝕜] G')
(f : continuous_multilinear_map 𝕜 E G) :
‖g.comp_continuous_multilinear_map f‖ ≤ ‖g‖ * ‖f‖ :=
continuous_multilinear_map.op_norm_le_bound _ (mul_nonneg (norm_nonneg _) (norm_nonneg _)) $ λ m,
calc ‖g (f m)‖ ≤ ‖g‖ * (‖f‖ * ∏ i, ‖m i‖) : g.le_op_norm_of_le $ f.le_op_norm _
... = _ : (mul_assoc _ _ _).symm
variables (𝕜 E G G')
/-- `continuous_linear_map.comp_continuous_multilinear_map` as a bundled continuous bilinear map. -/
def comp_continuous_multilinear_mapL :
(G →L[𝕜] G') →L[𝕜] continuous_multilinear_map 𝕜 E G →L[𝕜] continuous_multilinear_map 𝕜 E G' :=
linear_map.mk_continuous₂
(linear_map.mk₂ 𝕜 comp_continuous_multilinear_map (λ f₁ f₂ g, rfl) (λ c f g, rfl)
(λ f g₁ g₂, by { ext1, apply f.map_add }) (λ c f g, by { ext1, simp }))
1 $ λ f g, by { rw one_mul, exact f.norm_comp_continuous_multilinear_map_le g }
variables {𝕜 E G G'}
/-- Flip arguments in `f : G →L[𝕜] continuous_multilinear_map 𝕜 E G'` to get
`continuous_multilinear_map 𝕜 E (G →L[𝕜] G')` -/
def flip_multilinear (f : G →L[𝕜] continuous_multilinear_map 𝕜 E G') :
continuous_multilinear_map 𝕜 E (G →L[𝕜] G') :=
multilinear_map.mk_continuous
{ to_fun := λ m, linear_map.mk_continuous
{ to_fun := λ x, f x m,
map_add' := λ x y, by simp only [map_add, continuous_multilinear_map.add_apply],
map_smul' := λ c x, by simp only [continuous_multilinear_map.smul_apply, map_smul,
ring_hom.id_apply] }
(‖f‖ * ∏ i, ‖m i‖) $ λ x,
by { rw mul_right_comm, exact (f x).le_of_op_norm_le _ (f.le_op_norm x) },
map_add' := λ m i x y,
by { ext1, simp only [add_apply, continuous_multilinear_map.map_add, linear_map.coe_mk,
linear_map.mk_continuous_apply]},
map_smul' := λ m i c x,
by { ext1, simp only [coe_smul', continuous_multilinear_map.map_smul, linear_map.coe_mk,
linear_map.mk_continuous_apply, pi.smul_apply]} }
‖f‖ $ λ m,
linear_map.mk_continuous_norm_le _
(mul_nonneg (norm_nonneg f) (prod_nonneg $ λ i hi, norm_nonneg (m i))) _
end continuous_linear_map
open continuous_multilinear_map
namespace multilinear_map
/-- Given a map `f : G →ₗ[𝕜] multilinear_map 𝕜 E G'` and an estimate
`H : ∀ x m, ‖f x m‖ ≤ C * ‖x‖ * ∏ i, ‖m i‖`, construct a continuous linear
map from `G` to `continuous_multilinear_map 𝕜 E G'`.
In order to lift, e.g., a map `f : (multilinear_map 𝕜 E G) →ₗ[𝕜] multilinear_map 𝕜 E' G'`
to a map `(continuous_multilinear_map 𝕜 E G) →L[𝕜] continuous_multilinear_map 𝕜 E' G'`,
one can apply this construction to `f.comp continuous_multilinear_map.to_multilinear_map_linear`
which is a linear map from `continuous_multilinear_map 𝕜 E G` to `multilinear_map 𝕜 E' G'`. -/
def mk_continuous_linear (f : G →ₗ[𝕜] multilinear_map 𝕜 E G') (C : ℝ)
(H : ∀ x m, ‖f x m‖ ≤ C * ‖x‖ * ∏ i, ‖m i‖) :
G →L[𝕜] continuous_multilinear_map 𝕜 E G' :=
linear_map.mk_continuous
{ to_fun := λ x, (f x).mk_continuous (C * ‖x‖) $ H x,
map_add' := λ x y, by { ext1, simp only [_root_.map_add], refl },
map_smul' := λ c x, by { ext1, simp only [smul_hom_class.map_smul], refl } }
(max C 0) $ λ x, ((f x).mk_continuous_norm_le' _).trans_eq $
by rw [max_mul_of_nonneg _ _ (norm_nonneg x), zero_mul]
lemma mk_continuous_linear_norm_le' (f : G →ₗ[𝕜] multilinear_map 𝕜 E G') (C : ℝ)
(H : ∀ x m, ‖f x m‖ ≤ C * ‖x‖ * ∏ i, ‖m i‖) :
‖mk_continuous_linear f C H‖ ≤ max C 0 :=
begin
dunfold mk_continuous_linear,
exact linear_map.mk_continuous_norm_le _ (le_max_right _ _) _
end
lemma mk_continuous_linear_norm_le (f : G →ₗ[𝕜] multilinear_map 𝕜 E G') {C : ℝ} (hC : 0 ≤ C)
(H : ∀ x m, ‖f x m‖ ≤ C * ‖x‖ * ∏ i, ‖m i‖) :
‖mk_continuous_linear f C H‖ ≤ C :=
(mk_continuous_linear_norm_le' f C H).trans_eq (max_eq_left hC)
/-- Given a map `f : multilinear_map 𝕜 E (multilinear_map 𝕜 E' G)` and an estimate
`H : ∀ m m', ‖f m m'‖ ≤ C * ∏ i, ‖m i‖ * ∏ i, ‖m' i‖`, upgrade all `multilinear_map`s in the type to
`continuous_multilinear_map`s. -/
def mk_continuous_multilinear (f : multilinear_map 𝕜 E (multilinear_map 𝕜 E' G)) (C : ℝ)
(H : ∀ m₁ m₂, ‖f m₁ m₂‖ ≤ C * (∏ i, ‖m₁ i‖) * ∏ i, ‖m₂ i‖) :
continuous_multilinear_map 𝕜 E (continuous_multilinear_map 𝕜 E' G) :=
mk_continuous
{ to_fun := λ m, mk_continuous (f m) (C * ∏ i, ‖m i‖) $ H m,
map_add' := λ m i x y, by { ext1, simp },
map_smul' := λ m i c x, by { ext1, simp } }
(max C 0) $ λ m, ((f m).mk_continuous_norm_le' _).trans_eq $
by { rw [max_mul_of_nonneg, zero_mul], exact prod_nonneg (λ _ _, norm_nonneg _) }
@[simp] lemma mk_continuous_multilinear_apply (f : multilinear_map 𝕜 E (multilinear_map 𝕜 E' G))
{C : ℝ} (H : ∀ m₁ m₂, ‖f m₁ m₂‖ ≤ C * (∏ i, ‖m₁ i‖) * ∏ i, ‖m₂ i‖) (m : Π i, E i) :
⇑(mk_continuous_multilinear f C H m) = f m :=
rfl
lemma mk_continuous_multilinear_norm_le' (f : multilinear_map 𝕜 E (multilinear_map 𝕜 E' G)) (C : ℝ)
(H : ∀ m₁ m₂, ‖f m₁ m₂‖ ≤ C * (∏ i, ‖m₁ i‖) * ∏ i, ‖m₂ i‖) :
‖mk_continuous_multilinear f C H‖ ≤ max C 0 :=
begin
dunfold mk_continuous_multilinear,
exact mk_continuous_norm_le _ (le_max_right _ _) _
end
lemma mk_continuous_multilinear_norm_le (f : multilinear_map 𝕜 E (multilinear_map 𝕜 E' G)) {C : ℝ}
(hC : 0 ≤ C) (H : ∀ m₁ m₂, ‖f m₁ m₂‖ ≤ C * (∏ i, ‖m₁ i‖) * ∏ i, ‖m₂ i‖) :
‖mk_continuous_multilinear f C H‖ ≤ C :=
(mk_continuous_multilinear_norm_le' f C H).trans_eq (max_eq_left hC)
end multilinear_map
namespace continuous_multilinear_map
lemma norm_comp_continuous_linear_le (g : continuous_multilinear_map 𝕜 E₁ G)
(f : Π i, E i →L[𝕜] E₁ i) :
‖g.comp_continuous_linear_map f‖ ≤ ‖g‖ * ∏ i, ‖f i‖ :=
op_norm_le_bound _ (mul_nonneg (norm_nonneg _) $ prod_nonneg $ λ i hi, norm_nonneg _) $ λ m,
calc ‖g (λ i, f i (m i))‖ ≤ ‖g‖ * ∏ i, ‖f i (m i)‖ : g.le_op_norm _
... ≤ ‖g‖ * ∏ i, (‖f i‖ * ‖m i‖) :
mul_le_mul_of_nonneg_left
(prod_le_prod (λ _ _, norm_nonneg _) (λ i hi, (f i).le_op_norm (m i))) (norm_nonneg g)
... = (‖g‖ * ∏ i, ‖f i‖) * ∏ i, ‖m i‖ : by rw [prod_mul_distrib, mul_assoc]
/-- `continuous_multilinear_map.comp_continuous_linear_map` as a bundled continuous linear map.
This implementation fixes `f : Π i, E i →L[𝕜] E₁ i`.
TODO: Actually, the map is multilinear in `f` but an attempt to formalize this failed because of
issues with class instances. -/
def comp_continuous_linear_mapL (f : Π i, E i →L[𝕜] E₁ i) :
continuous_multilinear_map 𝕜 E₁ G →L[𝕜] continuous_multilinear_map 𝕜 E G :=
linear_map.mk_continuous
{ to_fun := λ g, g.comp_continuous_linear_map f,
map_add' := λ g₁ g₂, rfl,
map_smul' := λ c g, rfl }
(∏ i, ‖f i‖) $ λ g, (norm_comp_continuous_linear_le _ _).trans_eq (mul_comm _ _)
@[simp] lemma comp_continuous_linear_mapL_apply (g : continuous_multilinear_map 𝕜 E₁ G)
(f : Π i, E i →L[𝕜] E₁ i) :
comp_continuous_linear_mapL f g = g.comp_continuous_linear_map f :=
rfl
lemma norm_comp_continuous_linear_mapL_le (f : Π i, E i →L[𝕜] E₁ i) :
‖@comp_continuous_linear_mapL 𝕜 ι E E₁ G _ _ _ _ _ _ _ _ _ f‖ ≤ (∏ i, ‖f i‖) :=
linear_map.mk_continuous_norm_le _ (prod_nonneg $ λ i _, norm_nonneg _) _
end continuous_multilinear_map
section smul
variables {R : Type*} [semiring R] [module R G] [smul_comm_class 𝕜 R G]
[has_continuous_const_smul R G]
instance : has_continuous_const_smul R (continuous_multilinear_map 𝕜 E G) :=
⟨λ c, (continuous_linear_map.comp_continuous_multilinear_mapL 𝕜 _ G G
(c • continuous_linear_map.id 𝕜 G)).2⟩
end smul
section currying
/-!
### Currying
We associate to a continuous multilinear map in `n+1` variables (i.e., based on `fin n.succ`) two
curried functions, named `f.curry_left` (which is a continuous linear map on `E 0` taking values
in continuous multilinear maps in `n` variables) and `f.curry_right` (which is a continuous
multilinear map in `n` variables taking values in continuous linear maps on `E (last n)`).
The inverse operations are called `uncurry_left` and `uncurry_right`.
We also register continuous linear equiv versions of these correspondences, in
`continuous_multilinear_curry_left_equiv` and `continuous_multilinear_curry_right_equiv`.
-/
open fin function
lemma continuous_linear_map.norm_map_tail_le
(f : Ei 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.succ) G)) (m : Πi, Ei i) :
‖f (m 0) (tail m)‖ ≤ ‖f‖ * ∏ i, ‖m i‖ :=
calc
‖f (m 0) (tail m)‖ ≤ ‖f (m 0)‖ * ∏ i, ‖(tail m) i‖ : (f (m 0)).le_op_norm _
... ≤ (‖f‖ * ‖m 0‖) * ∏ i, ‖(tail m) i‖ :
mul_le_mul_of_nonneg_right (f.le_op_norm _) (prod_nonneg (λi hi, norm_nonneg _))
... = ‖f‖ * (‖m 0‖ * ∏ i, ‖(tail m) i‖) : by ring
... = ‖f‖ * ∏ i, ‖m i‖ : by { rw prod_univ_succ, refl }
lemma continuous_multilinear_map.norm_map_init_le
(f : continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.cast_succ) (Ei (last n) →L[𝕜] G))
(m : Πi, Ei i) :
‖f (init m) (m (last n))‖ ≤ ‖f‖ * ∏ i, ‖m i‖ :=
calc
‖f (init m) (m (last n))‖ ≤ ‖f (init m)‖ * ‖m (last n)‖ : (f (init m)).le_op_norm _
... ≤ (‖f‖ * (∏ i, ‖(init m) i‖)) * ‖m (last n)‖ :
mul_le_mul_of_nonneg_right (f.le_op_norm _) (norm_nonneg _)
... = ‖f‖ * ((∏ i, ‖(init m) i‖) * ‖m (last n)‖) : mul_assoc _ _ _
... = ‖f‖ * ∏ i, ‖m i‖ : by { rw prod_univ_cast_succ, refl }
lemma continuous_multilinear_map.norm_map_cons_le
(f : continuous_multilinear_map 𝕜 Ei G) (x : Ei 0) (m : Π(i : fin n), Ei i.succ) :
‖f (cons x m)‖ ≤ ‖f‖ * ‖x‖ * ∏ i, ‖m i‖ :=
calc
‖f (cons x m)‖ ≤ ‖f‖ * ∏ i, ‖cons x m i‖ : f.le_op_norm _
... = (‖f‖ * ‖x‖) * ∏ i, ‖m i‖ : by { rw prod_univ_succ, simp [mul_assoc] }
lemma continuous_multilinear_map.norm_map_snoc_le
(f : continuous_multilinear_map 𝕜 Ei G) (m : Π(i : fin n), Ei i.cast_succ) (x : Ei (last n)) :
‖f (snoc m x)‖ ≤ ‖f‖ * (∏ i, ‖m i‖) * ‖x‖ :=
calc
‖f (snoc m x)‖ ≤ ‖f‖ * ∏ i, ‖snoc m x i‖ : f.le_op_norm _
... = ‖f‖ * (∏ i, ‖m i‖) * ‖x‖ : by { rw prod_univ_cast_succ, simp [mul_assoc] }
/-! #### Left currying -/
/-- Given a continuous linear map `f` from `E 0` to continuous multilinear maps on `n` variables,
construct the corresponding continuous multilinear map on `n+1` variables obtained by concatenating
the variables, given by `m ↦ f (m 0) (tail m)`-/
def continuous_linear_map.uncurry_left
(f : Ei 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.succ) G)) :
continuous_multilinear_map 𝕜 Ei G :=
(@linear_map.uncurry_left 𝕜 n Ei G _ _ _ _ _
(continuous_multilinear_map.to_multilinear_map_linear.comp f.to_linear_map)).mk_continuous
(‖f‖) (λm, continuous_linear_map.norm_map_tail_le f m)
@[simp] lemma continuous_linear_map.uncurry_left_apply
(f : Ei 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.succ) G)) (m : Πi, Ei i) :
f.uncurry_left m = f (m 0) (tail m) := rfl
/-- Given a continuous multilinear map `f` in `n+1` variables, split the first variable to obtain
a continuous linear map into continuous multilinear maps in `n` variables, given by
`x ↦ (m ↦ f (cons x m))`. -/
def continuous_multilinear_map.curry_left
(f : continuous_multilinear_map 𝕜 Ei G) :
Ei 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.succ) G) :=
linear_map.mk_continuous
{ -- define a linear map into `n` continuous multilinear maps from an `n+1` continuous multilinear
-- map
to_fun := λx, (f.to_multilinear_map.curry_left x).mk_continuous
(‖f‖ * ‖x‖) (f.norm_map_cons_le x),
map_add' := λx y, by { ext m, exact f.cons_add m x y },
map_smul' := λc x, by { ext m, exact f.cons_smul m c x } }
-- then register its continuity thanks to its boundedness properties.
(‖f‖) (λx, multilinear_map.mk_continuous_norm_le _ (mul_nonneg (norm_nonneg _) (norm_nonneg _)) _)
@[simp] lemma continuous_multilinear_map.curry_left_apply
(f : continuous_multilinear_map 𝕜 Ei G) (x : Ei 0) (m : Π(i : fin n), Ei i.succ) :
f.curry_left x m = f (cons x m) := rfl
@[simp] lemma continuous_linear_map.curry_uncurry_left
(f : Ei 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.succ) G)) :
f.uncurry_left.curry_left = f :=
begin
ext m x,
simp only [tail_cons, continuous_linear_map.uncurry_left_apply,
continuous_multilinear_map.curry_left_apply],
rw cons_zero
end
@[simp] lemma continuous_multilinear_map.uncurry_curry_left
(f : continuous_multilinear_map 𝕜 Ei G) : f.curry_left.uncurry_left = f :=
continuous_multilinear_map.to_multilinear_map_inj $ f.to_multilinear_map.uncurry_curry_left
variables (𝕜 Ei G)
/-- The space of continuous multilinear maps on `Π(i : fin (n+1)), E i` is canonically isomorphic to
the space of continuous linear maps from `E 0` to the space of continuous multilinear maps on
`Π(i : fin n), E i.succ `, by separating the first variable. We register this isomorphism in
`continuous_multilinear_curry_left_equiv 𝕜 E E₂`. The algebraic version (without topology) is given
in `multilinear_curry_left_equiv 𝕜 E E₂`.
The direct and inverse maps are given by `f.uncurry_left` and `f.curry_left`. Use these
unless you need the full framework of linear isometric equivs. -/
def continuous_multilinear_curry_left_equiv :
(Ei 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.succ) G)) ≃ₗᵢ[𝕜]
(continuous_multilinear_map 𝕜 Ei G) :=
linear_isometry_equiv.of_bounds
{ to_fun := continuous_linear_map.uncurry_left,
map_add' := λf₁ f₂, by { ext m, refl },
map_smul' := λc f, by { ext m, refl },
inv_fun := continuous_multilinear_map.curry_left,
left_inv := continuous_linear_map.curry_uncurry_left,
right_inv := continuous_multilinear_map.uncurry_curry_left }
(λ f, multilinear_map.mk_continuous_norm_le _ (norm_nonneg f) _)
(λ f, linear_map.mk_continuous_norm_le _ (norm_nonneg f) _)
variables {𝕜 Ei G}
@[simp] lemma continuous_multilinear_curry_left_equiv_apply
(f : Ei 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ i : fin n, Ei i.succ) G)) (v : Π i, Ei i) :
continuous_multilinear_curry_left_equiv 𝕜 Ei G f v = f (v 0) (tail v) := rfl
@[simp] lemma continuous_multilinear_curry_left_equiv_symm_apply
(f : continuous_multilinear_map 𝕜 Ei G) (x : Ei 0) (v : Π i : fin n, Ei i.succ) :
(continuous_multilinear_curry_left_equiv 𝕜 Ei G).symm f x v = f (cons x v) := rfl
@[simp] lemma continuous_multilinear_map.curry_left_norm
(f : continuous_multilinear_map 𝕜 Ei G) : ‖f.curry_left‖ = ‖f‖ :=
(continuous_multilinear_curry_left_equiv 𝕜 Ei G).symm.norm_map f
@[simp] lemma continuous_linear_map.uncurry_left_norm
(f : Ei 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.succ) G)) :
‖f.uncurry_left‖ = ‖f‖ :=
(continuous_multilinear_curry_left_equiv 𝕜 Ei G).norm_map f
/-! #### Right currying -/
/-- Given a continuous linear map `f` from continuous multilinear maps on `n` variables to
continuous linear maps on `E 0`, construct the corresponding continuous multilinear map on `n+1`
variables obtained by concatenating the variables, given by `m ↦ f (init m) (m (last n))`. -/
def continuous_multilinear_map.uncurry_right
(f : continuous_multilinear_map 𝕜 (λ i : fin n, Ei i.cast_succ) (Ei (last n) →L[𝕜] G)) :
continuous_multilinear_map 𝕜 Ei G :=
let f' : multilinear_map 𝕜 (λ(i : fin n), Ei i.cast_succ) (Ei (last n) →ₗ[𝕜] G) :=
{ to_fun := λ m, (f m).to_linear_map,
map_add' := λ m i x y, by simp,
map_smul' := λ m i c x, by simp } in
(@multilinear_map.uncurry_right 𝕜 n Ei G _ _ _ _ _ f').mk_continuous
(‖f‖) (λm, f.norm_map_init_le m)
@[simp] lemma continuous_multilinear_map.uncurry_right_apply
(f : continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.cast_succ) (Ei (last n) →L[𝕜] G))
(m : Πi, Ei i) :
f.uncurry_right m = f (init m) (m (last n)) := rfl
/-- Given a continuous multilinear map `f` in `n+1` variables, split the last variable to obtain
a continuous multilinear map in `n` variables into continuous linear maps, given by
`m ↦ (x ↦ f (snoc m x))`. -/
def continuous_multilinear_map.curry_right
(f : continuous_multilinear_map 𝕜 Ei G) :
continuous_multilinear_map 𝕜 (λ i : fin n, Ei i.cast_succ) (Ei (last n) →L[𝕜] G) :=
let f' : multilinear_map 𝕜 (λ(i : fin n), Ei i.cast_succ) (Ei (last n) →L[𝕜] G) :=
{ to_fun := λm, (f.to_multilinear_map.curry_right m).mk_continuous
(‖f‖ * ∏ i, ‖m i‖) $ λx, f.norm_map_snoc_le m x,
map_add' := λ m i x y, by { simp, refl },
map_smul' := λ m i c x, by { simp, refl } } in
f'.mk_continuous (‖f‖) (λm, linear_map.mk_continuous_norm_le _
(mul_nonneg (norm_nonneg _) (prod_nonneg (λj hj, norm_nonneg _))) _)
@[simp] lemma continuous_multilinear_map.curry_right_apply
(f : continuous_multilinear_map 𝕜 Ei G) (m : Π i : fin n, Ei i.cast_succ) (x : Ei (last n)) :
f.curry_right m x = f (snoc m x) := rfl
@[simp] lemma continuous_multilinear_map.curry_uncurry_right
(f : continuous_multilinear_map 𝕜 (λ i : fin n, Ei i.cast_succ) (Ei (last n) →L[𝕜] G)) :
f.uncurry_right.curry_right = f :=
begin
ext m x,
simp only [snoc_last, continuous_multilinear_map.curry_right_apply,
continuous_multilinear_map.uncurry_right_apply],
rw init_snoc
end
@[simp] lemma continuous_multilinear_map.uncurry_curry_right
(f : continuous_multilinear_map 𝕜 Ei G) : f.curry_right.uncurry_right = f :=
by { ext m, simp }
variables (𝕜 Ei G)
/--
The space of continuous multilinear maps on `Π(i : fin (n+1)), Ei i` is canonically isomorphic to
the space of continuous multilinear maps on `Π(i : fin n), Ei i.cast_succ` with values in the space
of continuous linear maps on `Ei (last n)`, by separating the last variable. We register this
isomorphism as a continuous linear equiv in `continuous_multilinear_curry_right_equiv 𝕜 Ei G`.
The algebraic version (without topology) is given in `multilinear_curry_right_equiv 𝕜 Ei G`.
The direct and inverse maps are given by `f.uncurry_right` and `f.curry_right`. Use these
unless you need the full framework of linear isometric equivs.
-/
def continuous_multilinear_curry_right_equiv :
(continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.cast_succ) (Ei (last n) →L[𝕜] G)) ≃ₗᵢ[𝕜]
(continuous_multilinear_map 𝕜 Ei G) :=
linear_isometry_equiv.of_bounds
{ to_fun := continuous_multilinear_map.uncurry_right,
map_add' := λf₁ f₂, by { ext m, refl },
map_smul' := λc f, by { ext m, refl },
inv_fun := continuous_multilinear_map.curry_right,
left_inv := continuous_multilinear_map.curry_uncurry_right,
right_inv := continuous_multilinear_map.uncurry_curry_right }
(λ f, multilinear_map.mk_continuous_norm_le _ (norm_nonneg f) _)
(λ f, multilinear_map.mk_continuous_norm_le _ (norm_nonneg f) _)
variables (n G')
/-- The space of continuous multilinear maps on `Π(i : fin (n+1)), G` is canonically isomorphic to
the space of continuous multilinear maps on `Π(i : fin n), G` with values in the space
of continuous linear maps on `G`, by separating the last variable. We register this
isomorphism as a continuous linear equiv in `continuous_multilinear_curry_right_equiv' 𝕜 n G G'`.
For a version allowing dependent types, see `continuous_multilinear_curry_right_equiv`. When there
are no dependent types, use the primed version as it helps Lean a lot for unification.
The direct and inverse maps are given by `f.uncurry_right` and `f.curry_right`. Use these
unless you need the full framework of linear isometric equivs. -/
def continuous_multilinear_curry_right_equiv' :
(G [×n]→L[𝕜] (G →L[𝕜] G')) ≃ₗᵢ[𝕜] (G [×n.succ]→L[𝕜] G') :=
continuous_multilinear_curry_right_equiv 𝕜 (λ (i : fin n.succ), G) G'
variables {n 𝕜 G Ei G'}
@[simp] lemma continuous_multilinear_curry_right_equiv_apply
(f : (continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.cast_succ) (Ei (last n) →L[𝕜] G)))
(v : Π i, Ei i) :
(continuous_multilinear_curry_right_equiv 𝕜 Ei G) f v = f (init v) (v (last n)) := rfl
@[simp] lemma continuous_multilinear_curry_right_equiv_symm_apply
(f : continuous_multilinear_map 𝕜 Ei G)
(v : Π (i : fin n), Ei i.cast_succ) (x : Ei (last n)) :
(continuous_multilinear_curry_right_equiv 𝕜 Ei G).symm f v x = f (snoc v x) := rfl
@[simp] lemma continuous_multilinear_curry_right_equiv_apply'
(f : G [×n]→L[𝕜] (G →L[𝕜] G')) (v : fin (n + 1) → G) :
continuous_multilinear_curry_right_equiv' 𝕜 n G G' f v = f (init v) (v (last n)) := rfl
@[simp] lemma continuous_multilinear_curry_right_equiv_symm_apply'
(f : G [×n.succ]→L[𝕜] G') (v : fin n → G) (x : G) :
(continuous_multilinear_curry_right_equiv' 𝕜 n G G').symm f v x = f (snoc v x) := rfl
@[simp] lemma continuous_multilinear_map.curry_right_norm
(f : continuous_multilinear_map 𝕜 Ei G) : ‖f.curry_right‖ = ‖f‖ :=
(continuous_multilinear_curry_right_equiv 𝕜 Ei G).symm.norm_map f
@[simp] lemma continuous_multilinear_map.uncurry_right_norm
(f : continuous_multilinear_map 𝕜 (λ i : fin n, Ei i.cast_succ) (Ei (last n) →L[𝕜] G)) :
‖f.uncurry_right‖ = ‖f‖ :=
(continuous_multilinear_curry_right_equiv 𝕜 Ei G).norm_map f
/-!
#### Currying with `0` variables
The space of multilinear maps with `0` variables is trivial: such a multilinear map is just an
arbitrary constant (note that multilinear maps in `0` variables need not map `0` to `0`!).
Therefore, the space of continuous multilinear maps on `(fin 0) → G` with values in `E₂` is
isomorphic (and even isometric) to `E₂`. As this is the zeroth step in the construction of iterated
derivatives, we register this isomorphism. -/
section
variables {𝕜 G G'}
/-- Associating to a continuous multilinear map in `0` variables the unique value it takes. -/
def continuous_multilinear_map.uncurry0
(f : continuous_multilinear_map 𝕜 (λ (i : fin 0), G) G') : G' := f 0
variables (𝕜 G)
/-- Associating to an element `x` of a vector space `E₂` the continuous multilinear map in `0`
variables taking the (unique) value `x` -/
def continuous_multilinear_map.curry0 (x : G') : G [×0]→L[𝕜] G' :=
{ to_fun := λm, x,
map_add' := λ m i, fin.elim0 i,
map_smul' := λ m i, fin.elim0 i,
cont := continuous_const }
variable {G}
@[simp] lemma continuous_multilinear_map.curry0_apply (x : G') (m : (fin 0) → G) :
continuous_multilinear_map.curry0 𝕜 G x m = x := rfl
variable {𝕜}
@[simp] lemma continuous_multilinear_map.uncurry0_apply (f : G [×0]→L[𝕜] G') :
f.uncurry0 = f 0 := rfl
@[simp] lemma continuous_multilinear_map.apply_zero_curry0 (f : G [×0]→L[𝕜] G') {x : fin 0 → G} :
continuous_multilinear_map.curry0 𝕜 G (f x) = f :=
by { ext m, simp [(subsingleton.elim _ _ : x = m)] }
lemma continuous_multilinear_map.uncurry0_curry0 (f : G [×0]→L[𝕜] G') :
continuous_multilinear_map.curry0 𝕜 G (f.uncurry0) = f :=
by simp
variables (𝕜 G)
@[simp] lemma continuous_multilinear_map.curry0_uncurry0 (x : G') :
(continuous_multilinear_map.curry0 𝕜 G x).uncurry0 = x := rfl
@[simp] lemma continuous_multilinear_map.curry0_norm (x : G') :
‖continuous_multilinear_map.curry0 𝕜 G x‖ = ‖x‖ :=
begin
apply le_antisymm,
{ exact continuous_multilinear_map.op_norm_le_bound _ (norm_nonneg _) (λm, by simp) },
{ simpa using (continuous_multilinear_map.curry0 𝕜 G x).le_op_norm 0 }
end
variables {𝕜 G}
@[simp] lemma continuous_multilinear_map.fin0_apply_norm (f : G [×0]→L[𝕜] G') {x : fin 0 → G} :
‖f x‖ = ‖f‖ :=
begin
obtain rfl : x = 0 := subsingleton.elim _ _,
refine le_antisymm (by simpa using f.le_op_norm 0) _,
have : ‖continuous_multilinear_map.curry0 𝕜 G (f.uncurry0)‖ ≤ ‖f.uncurry0‖ :=
continuous_multilinear_map.op_norm_le_bound _ (norm_nonneg _) (λm,
by simp [-continuous_multilinear_map.apply_zero_curry0]),
simpa
end
lemma continuous_multilinear_map.uncurry0_norm (f : G [×0]→L[𝕜] G') : ‖f.uncurry0‖ = ‖f‖ :=
by simp
variables (𝕜 G G')
/-- The continuous linear isomorphism between elements of a normed space, and continuous multilinear
maps in `0` variables with values in this normed space.
The direct and inverse maps are `uncurry0` and `curry0`. Use these unless you need the full
framework of linear isometric equivs. -/
def continuous_multilinear_curry_fin0 : (G [×0]→L[𝕜] G') ≃ₗᵢ[𝕜] G' :=
{ to_fun := λf, continuous_multilinear_map.uncurry0 f,
inv_fun := λf, continuous_multilinear_map.curry0 𝕜 G f,
map_add' := λf g, rfl,
map_smul' := λc f, rfl,
left_inv := continuous_multilinear_map.uncurry0_curry0,
right_inv := continuous_multilinear_map.curry0_uncurry0 𝕜 G,
norm_map' := continuous_multilinear_map.uncurry0_norm }
variables {𝕜 G G'}
@[simp] lemma continuous_multilinear_curry_fin0_apply (f : G [×0]→L[𝕜] G') :
continuous_multilinear_curry_fin0 𝕜 G G' f = f 0 := rfl
@[simp] lemma continuous_multilinear_curry_fin0_symm_apply (x : G') (v : (fin 0) → G) :
(continuous_multilinear_curry_fin0 𝕜 G G').symm x v = x := rfl
end
/-! #### With 1 variable -/
variables (𝕜 G G')
/-- Continuous multilinear maps from `G^1` to `G'` are isomorphic with continuous linear maps from
`G` to `G'`. -/
def continuous_multilinear_curry_fin1 : (G [×1]→L[𝕜] G') ≃ₗᵢ[𝕜] (G →L[𝕜] G') :=
(continuous_multilinear_curry_right_equiv 𝕜 (λ (i : fin 1), G) G').symm.trans
(continuous_multilinear_curry_fin0 𝕜 G (G →L[𝕜] G'))
variables {𝕜 G G'}
@[simp] lemma continuous_multilinear_curry_fin1_apply (f : G [×1]→L[𝕜] G') (x : G) :
continuous_multilinear_curry_fin1 𝕜 G G' f x = f (fin.snoc 0 x) := rfl
@[simp] lemma continuous_multilinear_curry_fin1_symm_apply
(f : G →L[𝕜] G') (v : (fin 1) → G) :
(continuous_multilinear_curry_fin1 𝕜 G G').symm f v = f (v 0) := rfl
namespace continuous_multilinear_map
variables (𝕜 G G')
/-- An equivalence of the index set defines a linear isometric equivalence between the spaces
of multilinear maps. -/
def dom_dom_congr (σ : ι ≃ ι') :
continuous_multilinear_map 𝕜 (λ _ : ι, G) G' ≃ₗᵢ[𝕜]
continuous_multilinear_map 𝕜 (λ _ : ι', G) G' :=
linear_isometry_equiv.of_bounds
{ to_fun := λ f, (multilinear_map.dom_dom_congr σ f.to_multilinear_map).mk_continuous ‖f‖ $
λ m, (f.le_op_norm (λ i, m (σ i))).trans_eq $ by rw [← σ.prod_comp],
inv_fun := λ f, (multilinear_map.dom_dom_congr σ.symm f.to_multilinear_map).mk_continuous ‖f‖ $
λ m, (f.le_op_norm (λ i, m (σ.symm i))).trans_eq $ by rw [← σ.symm.prod_comp],
left_inv := λ f, ext $ λ m, congr_arg f $ by simp only [σ.symm_apply_apply],
right_inv := λ f, ext $ λ m, congr_arg f $ by simp only [σ.apply_symm_apply],
map_add' := λ f g, rfl,
map_smul' := λ c f, rfl }
(λ f, multilinear_map.mk_continuous_norm_le _ (norm_nonneg f) _)
(λ f, multilinear_map.mk_continuous_norm_le _ (norm_nonneg f) _)
variables {𝕜 G G'}
section
variable [decidable_eq (ι ⊕ ι')]
/-- A continuous multilinear map with variables indexed by `ι ⊕ ι'` defines a continuous multilinear
map with variables indexed by `ι` taking values in the space of continuous multilinear maps with
variables indexed by `ι'`. -/
def curry_sum (f : continuous_multilinear_map 𝕜 (λ x : ι ⊕ ι', G) G') :
continuous_multilinear_map 𝕜 (λ x : ι, G) (continuous_multilinear_map 𝕜 (λ x : ι', G) G') :=
multilinear_map.mk_continuous_multilinear (multilinear_map.curry_sum f.to_multilinear_map) (‖f‖) $
λ m m', by simpa [fintype.prod_sum_type, mul_assoc] using f.le_op_norm (sum.elim m m')
@[simp] lemma curry_sum_apply (f : continuous_multilinear_map 𝕜 (λ x : ι ⊕ ι', G) G')
(m : ι → G) (m' : ι' → G) :
f.curry_sum m m' = f (sum.elim m m') :=
rfl
/-- A continuous multilinear map with variables indexed by `ι` taking values in the space of
continuous multilinear maps with variables indexed by `ι'` defines a continuous multilinear map with
variables indexed by `ι ⊕ ι'`. -/
def uncurry_sum
(f : continuous_multilinear_map 𝕜 (λ x : ι, G) (continuous_multilinear_map 𝕜 (λ x : ι', G) G')) :
continuous_multilinear_map 𝕜 (λ x : ι ⊕ ι', G) G' :=
multilinear_map.mk_continuous
(to_multilinear_map_linear.comp_multilinear_map f.to_multilinear_map).uncurry_sum (‖f‖) $ λ m,
by simpa [fintype.prod_sum_type, mul_assoc]
using (f (m ∘ sum.inl)).le_of_op_norm_le (m ∘ sum.inr) (f.le_op_norm _)
@[simp] lemma uncurry_sum_apply
(f : continuous_multilinear_map 𝕜 (λ x : ι, G) (continuous_multilinear_map 𝕜 (λ x : ι', G) G'))
(m : ι ⊕ ι' → G) :
f.uncurry_sum m = f (m ∘ sum.inl) (m ∘ sum.inr) :=
rfl
variables (𝕜 ι ι' G G')
/-- Linear isometric equivalence between the space of continuous multilinear maps with variables
indexed by `ι ⊕ ι'` and the space of continuous multilinear maps with variables indexed by `ι`
taking values in the space of continuous multilinear maps with variables indexed by `ι'`.
The forward and inverse functions are `continuous_multilinear_map.curry_sum`
and `continuous_multilinear_map.uncurry_sum`. Use this definition only if you need
some properties of `linear_isometry_equiv`. -/
def curry_sum_equiv : continuous_multilinear_map 𝕜 (λ x : ι ⊕ ι', G) G' ≃ₗᵢ[𝕜]
continuous_multilinear_map 𝕜 (λ x : ι, G) (continuous_multilinear_map 𝕜 (λ x : ι', G) G') :=
linear_isometry_equiv.of_bounds
{ to_fun := curry_sum,
inv_fun := uncurry_sum,
map_add' := λ f g, by { ext, refl },
map_smul' := λ c f, by { ext, refl },
left_inv := λ f, by { ext m, exact congr_arg f (sum.elim_comp_inl_inr m) },
right_inv := λ f, by { ext m₁ m₂, change f _ _ = f _ _,
rw [sum.elim_comp_inl, sum.elim_comp_inr] } }
(λ f, multilinear_map.mk_continuous_multilinear_norm_le _ (norm_nonneg f) _)
(λ f, multilinear_map.mk_continuous_norm_le _ (norm_nonneg f) _)
end
section
variables (𝕜 G G') {k l : ℕ} {s : finset (fin n)}
/-- If `s : finset (fin n)` is a finite set of cardinality `k` and its complement has cardinality
`l`, then the space of continuous multilinear maps `G [×n]→L[𝕜] G'` of `n` variables is isomorphic
to the space of continuous multilinear maps `G [×k]→L[𝕜] G [×l]→L[𝕜] G'` of `k` variables taking
values in the space of continuous multilinear maps of `l` variables. -/
def curry_fin_finset {k l n : ℕ} {s : finset (fin n)}
(hk : s.card = k) (hl : sᶜ.card = l) :
(G [×n]→L[𝕜] G') ≃ₗᵢ[𝕜] (G [×k]→L[𝕜] G [×l]→L[𝕜] G') :=
(dom_dom_congr 𝕜 G G' (fin_sum_equiv_of_finset hk hl).symm).trans
(curry_sum_equiv 𝕜 (fin k) (fin l) G G')
variables {𝕜 G G'}
@[simp] lemma curry_fin_finset_apply (hk : s.card = k) (hl : sᶜ.card = l)
(f : G [×n]→L[𝕜] G') (mk : fin k → G) (ml : fin l → G) :
curry_fin_finset 𝕜 G G' hk hl f mk ml =
f (λ i, sum.elim mk ml ((fin_sum_equiv_of_finset hk hl).symm i)) :=
rfl
@[simp] lemma curry_fin_finset_symm_apply (hk : s.card = k) (hl : sᶜ.card = l)
(f : G [×k]→L[𝕜] G [×l]→L[𝕜] G') (m : fin n → G) :
(curry_fin_finset 𝕜 G G' hk hl).symm f m =
f (λ i, m $ fin_sum_equiv_of_finset hk hl (sum.inl i))
(λ i, m $ fin_sum_equiv_of_finset hk hl (sum.inr i)) :=
rfl
@[simp] lemma curry_fin_finset_symm_apply_piecewise_const (hk : s.card = k) (hl : sᶜ.card = l)
(f : G [×k]→L[𝕜] G [×l]→L[𝕜] G') (x y : G) :
(curry_fin_finset 𝕜 G G' hk hl).symm f (s.piecewise (λ _, x) (λ _, y)) = f (λ _, x) (λ _, y) :=
multilinear_map.curry_fin_finset_symm_apply_piecewise_const hk hl _ x y
@[simp] lemma curry_fin_finset_symm_apply_const (hk : s.card = k) (hl : sᶜ.card = l)
(f : G [×k]→L[𝕜] G [×l]→L[𝕜] G') (x : G) :
(curry_fin_finset 𝕜 G G' hk hl).symm f (λ _, x) = f (λ _, x) (λ _, x) :=
rfl
@[simp] lemma curry_fin_finset_apply_const (hk : s.card = k) (hl : sᶜ.card = l)
(f : G [×n]→L[𝕜] G') (x y : G) :
curry_fin_finset 𝕜 G G' hk hl f (λ _, x) (λ _, y) = f (s.piecewise (λ _, x) (λ _, y)) :=
begin
refine (curry_fin_finset_symm_apply_piecewise_const hk hl _ _ _).symm.trans _, -- `rw` fails
rw linear_isometry_equiv.symm_apply_apply
end
end
end continuous_multilinear_map
end currying
|
192f8df629408a256c67402d4157dc5f25a9e2e0 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/category_theory/limits/shapes/types.lean | c92488d57f25cfffaf5c2a206840db284cdf3369 | [
"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 | 21,348 | lean | /-
Copyright (c) 2020 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import category_theory.limits.types
import category_theory.limits.shapes.products
import category_theory.limits.shapes.binary_products
import category_theory.limits.shapes.terminal
import category_theory.concrete_category.basic
import tactic.elementwise
/-!
# Special shapes for limits in `Type`.
The general shape (co)limits defined in `category_theory.limits.types`
are intended for use through the limits API,
and the actual implementation should mostly be considered "sealed".
In this file, we provide definitions of the "standard" special shapes of limits in `Type`,
giving the expected definitional implementation:
* the terminal object is `punit`
* the binary product of `X` and `Y` is `X × Y`
* the product of a family `f : J → Type` is `Π j, f j`
* the coproduct of a family `f : J → Type` is `Σ j, f j`
* the binary coproduct of `X` and `Y` is the sum type `X ⊕ Y`
* the equalizer of a pair of maps `(g, h)` is the subtype `{x : Y // g x = h x}`
* the coequalizer of a pair of maps `(f, g)` is the quotient of `Y` by `∀ x : Y, f x ~ g x`
* the pullback of `f : X ⟶ Z` and `g : Y ⟶ Z` is the subtype `{ p : X × Y // f p.1 = g p.2 }`
of the product
We first construct terms of `is_limit` and `limit_cone`, and then provide isomorphisms with the
types generated by the `has_limit` API.
As an example, when setting up the monoidal category structure on `Type`
we use the `types_has_terminal` and `types_has_binary_products` instances.
-/
universes u v
open category_theory
open category_theory.limits
namespace category_theory.limits.types
local attribute [tidy] tactic.discrete_cases
/-- A restatement of `types.lift_π_apply` that uses `pi.π` and `pi.lift`. -/
@[simp]
lemma pi_lift_π_apply
{β : Type u} (f : β → Type u) {P : Type u} (s : Π b, P ⟶ f b) (b : β) (x : P) :
(pi.π f b : (∏ f) → f b) (@pi.lift β _ _ f _ P s x) = s b x :=
congr_fun (limit.lift_π (fan.mk P s) ⟨b⟩) x
/-- A restatement of `types.map_π_apply` that uses `pi.π` and `pi.map`. -/
@[simp]
lemma pi_map_π_apply {β : Type u} {f g : β → Type u} (α : Π j, f j ⟶ g j) (b : β) (x) :
(pi.π g b : (∏ g) → g b) (pi.map α x) = α b ((pi.π f b : (∏ f) → f b) x) :=
limit.map_π_apply _ _ _
/-- The category of types has `punit` as a terminal object. -/
def terminal_limit_cone : limits.limit_cone (functor.empty (Type u)) :=
{ cone :=
{ X := punit,
π := by tidy, },
is_limit := by tidy, }
/-- The terminal object in `Type u` is `punit`. -/
noncomputable def terminal_iso : ⊤_ (Type u) ≅ punit :=
limit.iso_limit_cone terminal_limit_cone
/-- The terminal object in `Type u` is `punit`. -/
noncomputable
def is_terminal_punit : is_terminal (punit : Type u) :=
terminal_is_terminal.of_iso terminal_iso
/-- The category of types has `pempty` as an initial object. -/
def initial_colimit_cocone : limits.colimit_cocone (functor.empty (Type u)) :=
{ cocone :=
{ X := pempty,
ι := by tidy, },
is_colimit := by tidy, }
/-- The initial object in `Type u` is `pempty`. -/
noncomputable def initial_iso : ⊥_ (Type u) ≅ pempty :=
colimit.iso_colimit_cocone initial_colimit_cocone
/-- The initial object in `Type u` is `pempty`. -/
noncomputable
def is_initial_punit : is_initial (pempty : Type u) :=
initial_is_initial.of_iso initial_iso
open category_theory.limits.walking_pair
/-- The product type `X × Y` forms a cone for the binary product of `X` and `Y`. -/
-- We manually generate the other projection lemmas since the simp-normal form for the legs is
-- otherwise not created correctly.
@[simps X]
def binary_product_cone (X Y : Type u) : binary_fan X Y :=
binary_fan.mk prod.fst prod.snd
@[simp]
lemma binary_product_cone_fst (X Y : Type u) :
(binary_product_cone X Y).fst = prod.fst :=
rfl
@[simp]
lemma binary_product_cone_snd (X Y : Type u) :
(binary_product_cone X Y).snd = prod.snd :=
rfl
/-- The product type `X × Y` is a binary product for `X` and `Y`. -/
@[simps]
def binary_product_limit (X Y : Type u) : is_limit (binary_product_cone X Y) :=
{ lift := λ (s : binary_fan X Y) x, (s.fst x, s.snd x),
fac' := λ s j, discrete.rec_on j (λ j, walking_pair.cases_on j rfl rfl),
uniq' := λ s m w, funext $ λ x, prod.ext (congr_fun (w ⟨left⟩) x) (congr_fun (w ⟨right⟩) x) }
/--
The category of types has `X × Y`, the usual cartesian product,
as the binary product of `X` and `Y`.
-/
@[simps]
def binary_product_limit_cone (X Y : Type u) : limits.limit_cone (pair X Y) :=
⟨_, binary_product_limit X Y⟩
/-- The categorical binary product in `Type u` is cartesian product. -/
noncomputable def binary_product_iso (X Y : Type u) : limits.prod X Y ≅ X × Y :=
limit.iso_limit_cone (binary_product_limit_cone X Y)
@[simp, elementwise] lemma binary_product_iso_hom_comp_fst (X Y : Type u) :
(binary_product_iso X Y).hom ≫ prod.fst = limits.prod.fst :=
limit.iso_limit_cone_hom_π (binary_product_limit_cone X Y) ⟨walking_pair.left⟩
@[simp, elementwise] lemma binary_product_iso_hom_comp_snd (X Y : Type u) :
(binary_product_iso X Y).hom ≫ prod.snd = limits.prod.snd :=
limit.iso_limit_cone_hom_π (binary_product_limit_cone X Y) ⟨walking_pair.right⟩
@[simp, elementwise] lemma binary_product_iso_inv_comp_fst (X Y : Type u) :
(binary_product_iso X Y).inv ≫ limits.prod.fst = prod.fst :=
limit.iso_limit_cone_inv_π (binary_product_limit_cone X Y) ⟨walking_pair.left⟩
@[simp, elementwise] lemma binary_product_iso_inv_comp_snd (X Y : Type u) :
(binary_product_iso X Y).inv ≫ limits.prod.snd = prod.snd :=
limit.iso_limit_cone_inv_π (binary_product_limit_cone X Y) ⟨walking_pair.right⟩
/-- The functor which sends `X, Y` to the product type `X × Y`. -/
-- We add the option `type_md` to tell `@[simps]` to not treat homomorphisms `X ⟶ Y` in `Type*` as
-- a function type
@[simps {type_md := reducible}]
def binary_product_functor : Type u ⥤ Type u ⥤ Type u :=
{ obj := λ X,
{ obj := λ Y, X × Y,
map := λ Y₁ Y₂ f, (binary_product_limit X Y₂).lift (binary_fan.mk prod.fst (prod.snd ≫ f)) },
map := λ X₁ X₂ f,
{ app := λ Y, (binary_product_limit X₂ Y).lift (binary_fan.mk (prod.fst ≫ f) prod.snd) } }
/--
The product functor given by the instance `has_binary_products (Type u)` is isomorphic to the
explicit binary product functor given by the product type.
-/
noncomputable def binary_product_iso_prod : binary_product_functor ≅ (prod.functor : Type u ⥤ _) :=
begin
apply nat_iso.of_components (λ X, _) _,
{ apply nat_iso.of_components (λ Y, _) _,
{ exact ((limit.is_limit _).cone_point_unique_up_to_iso (binary_product_limit X Y)).symm },
{ intros Y₁ Y₂ f,
ext1;
simp } },
{ intros X₁ X₂ g,
ext : 3;
simp }
end
/-- The sum type `X ⊕ Y` forms a cocone for the binary coproduct of `X` and `Y`. -/
@[simps]
def binary_coproduct_cocone (X Y : Type u) : cocone (pair X Y) :=
binary_cofan.mk sum.inl sum.inr
/-- The sum type `X ⊕ Y` is a binary coproduct for `X` and `Y`. -/
@[simps]
def binary_coproduct_colimit (X Y : Type u) : is_colimit (binary_coproduct_cocone X Y) :=
{ desc := λ (s : binary_cofan X Y), sum.elim s.inl s.inr,
fac' := λ s j, discrete.rec_on j (λ j, walking_pair.cases_on j rfl rfl),
uniq' := λ s m w, funext $ λ x, sum.cases_on x (congr_fun (w ⟨left⟩)) (congr_fun (w ⟨right⟩)) }
/--
The category of types has `X ⊕ Y`,
as the binary coproduct of `X` and `Y`.
-/
def binary_coproduct_colimit_cocone (X Y : Type u) : limits.colimit_cocone (pair X Y) :=
⟨_, binary_coproduct_colimit X Y⟩
/-- The categorical binary coproduct in `Type u` is the sum `X ⊕ Y`. -/
noncomputable def binary_coproduct_iso (X Y : Type u) : limits.coprod X Y ≅ X ⊕ Y :=
colimit.iso_colimit_cocone (binary_coproduct_colimit_cocone X Y)
open_locale category_theory.Type
@[simp, elementwise] lemma binary_coproduct_iso_inl_comp_hom (X Y : Type u) :
limits.coprod.inl ≫ (binary_coproduct_iso X Y).hom = sum.inl :=
colimit.iso_colimit_cocone_ι_hom (binary_coproduct_colimit_cocone X Y) ⟨walking_pair.left⟩
@[simp, elementwise] lemma binary_coproduct_iso_inr_comp_hom (X Y : Type u) :
limits.coprod.inr ≫ (binary_coproduct_iso X Y).hom = sum.inr :=
colimit.iso_colimit_cocone_ι_hom (binary_coproduct_colimit_cocone X Y) ⟨walking_pair.right⟩
@[simp, elementwise] lemma binary_coproduct_iso_inl_comp_inv (X Y : Type u) :
↾(sum.inl : X ⟶ X ⊕ Y) ≫ (binary_coproduct_iso X Y).inv = limits.coprod.inl :=
colimit.iso_colimit_cocone_ι_inv (binary_coproduct_colimit_cocone X Y) ⟨walking_pair.left⟩
@[simp, elementwise] lemma binary_coproduct_iso_inr_comp_inv (X Y : Type u) :
↾(sum.inr : Y ⟶ X ⊕ Y) ≫ (binary_coproduct_iso X Y).inv = limits.coprod.inr :=
colimit.iso_colimit_cocone_ι_inv (binary_coproduct_colimit_cocone X Y) ⟨walking_pair.right⟩
open function (injective)
lemma binary_cofan_is_colimit_iff {X Y : Type u} (c : binary_cofan X Y) :
nonempty (is_colimit c) ↔
injective c.inl ∧ injective c.inr ∧ is_compl (set.range c.inl) (set.range c.inr) :=
begin
classical,
split,
{ rintro ⟨h⟩,
rw [← show _ = c.inl, from h.comp_cocone_point_unique_up_to_iso_inv
(binary_coproduct_colimit X Y) ⟨walking_pair.left⟩,
← show _ = c.inr, from h.comp_cocone_point_unique_up_to_iso_inv
(binary_coproduct_colimit X Y) ⟨walking_pair.right⟩],
dsimp [binary_coproduct_cocone],
refine
⟨(h.cocone_point_unique_up_to_iso (binary_coproduct_colimit X Y)).symm.to_equiv.injective.comp
sum.inl_injective, (h.cocone_point_unique_up_to_iso (binary_coproduct_colimit X Y)).symm
.to_equiv.injective.comp sum.inr_injective, _⟩,
erw [set.range_comp, ← eq_compl_iff_is_compl, set.range_comp _ sum.inr, ← set.image_compl_eq
(h.cocone_point_unique_up_to_iso (binary_coproduct_colimit X Y)).symm.to_equiv.bijective],
congr' 1,
exact set.compl_range_inr.symm },
{ rintros ⟨h₁, h₂, h₃⟩,
have : ∀ x, x ∈ set.range c.inl ∨ x ∈ set.range c.inr,
{ rw [eq_compl_iff_is_compl.mpr h₃.symm], exact λ _, or_not },
refine ⟨binary_cofan.is_colimit.mk _ _ _ _ _⟩,
{ intros T f g x,
exact if h : x ∈ set.range c.inl
then f ((equiv.of_injective _ h₁).symm ⟨x, h⟩)
else g ((equiv.of_injective _ h₂).symm ⟨x, (this x).resolve_left h⟩) },
{ intros T f g, ext x, dsimp, simp [h₁.eq_iff] },
{ intros T f g, ext x, dsimp,
simp only [forall_exists_index, equiv.of_injective_symm_apply,
dif_ctx_congr, dite_eq_right_iff],
intros y e,
have : c.inr x ∈ set.range c.inl ⊓ set.range c.inr := ⟨⟨_, e⟩, ⟨_, rfl⟩⟩,
rw disjoint_iff.mp h₃.1 at this,
exact this.elim },
{ rintro T _ _ m rfl rfl, ext x, dsimp,
split_ifs; exact congr_arg _ (equiv.apply_of_injective_symm _ ⟨_, _⟩).symm } }
end
/-- Any monomorphism in `Type` is an coproduct injection. -/
noncomputable
def is_coprod_of_mono {X Y : Type u} (f : X ⟶ Y) [mono f] :
is_colimit (binary_cofan.mk f (subtype.val : (set.range f)ᶜ → Y)) :=
nonempty.some $ (binary_cofan_is_colimit_iff _).mpr
⟨(mono_iff_injective f).mp infer_instance, subtype.val_injective,
(eq_compl_iff_is_compl.mp $ subtype.range_val).symm⟩
/--
The category of types has `Π j, f j` as the product of a type family `f : J → Type`.
-/
def product_limit_cone {J : Type u} (F : J → Type (max u v)) :
limits.limit_cone (discrete.functor F) :=
{ cone :=
{ X := Π j, F j,
π := { app := λ j f, f j.as }, },
is_limit :=
{ lift := λ s x j, s.π.app ⟨j⟩ x,
uniq' := λ s m w, funext $ λ x, funext $ λ j, (congr_fun (w ⟨j⟩) x : _) } }
/-- The categorical product in `Type u` is the type theoretic product `Π j, F j`. -/
noncomputable def product_iso {J : Type u} (F : J → Type (max u v)) : ∏ F ≅ Π j, F j :=
limit.iso_limit_cone (product_limit_cone F)
@[simp, elementwise] lemma product_iso_hom_comp_eval {J : Type u} (F : J → Type (max u v)) (j : J) :
(product_iso F).hom ≫ (λ f, f j) = pi.π F j :=
rfl
@[simp, elementwise] lemma product_iso_inv_comp_π {J : Type u} (F : J → Type (max u v)) (j : J) :
(product_iso F).inv ≫ pi.π F j = (λ f, f j) :=
limit.iso_limit_cone_inv_π (product_limit_cone F) ⟨j⟩
/--
The category of types has `Σ j, f j` as the coproduct of a type family `f : J → Type`.
-/
def coproduct_colimit_cocone {J : Type u} (F : J → Type u) :
limits.colimit_cocone (discrete.functor F) :=
{ cocone :=
{ X := Σ j, F j,
ι :=
{ app := λ j x, ⟨j.as, x⟩ }, },
is_colimit :=
{ desc := λ s x, s.ι.app ⟨x.1⟩ x.2,
uniq' := λ s m w,
begin
ext ⟨j, x⟩,
have := congr_fun (w ⟨j⟩) x,
exact this,
end }, }
/-- The categorical coproduct in `Type u` is the type theoretic coproduct `Σ j, F j`. -/
noncomputable def coproduct_iso {J : Type u} (F : J → Type u) : ∐ F ≅ Σ j, F j :=
colimit.iso_colimit_cocone (coproduct_colimit_cocone F)
@[simp, elementwise] lemma coproduct_iso_ι_comp_hom {J : Type u} (F : J → Type u) (j : J) :
sigma.ι F j ≫ (coproduct_iso F).hom = (λ x : F j, (⟨j, x⟩ : Σ j, F j)) :=
colimit.iso_colimit_cocone_ι_hom (coproduct_colimit_cocone F) ⟨j⟩
@[simp, elementwise] lemma coproduct_iso_mk_comp_inv {J : Type u} (F : J → Type u) (j : J) :
↾(λ x : F j, (⟨j, x⟩ : Σ j, F j)) ≫ (coproduct_iso F).inv = sigma.ι F j :=
rfl
section fork
variables {X Y Z : Type u} (f : X ⟶ Y) {g h : Y ⟶ Z} (w : f ≫ g = f ≫ h)
/--
Show the given fork in `Type u` is an equalizer given that any element in the "difference kernel"
comes from `X`.
The converse of `unique_of_type_equalizer`.
-/
noncomputable def type_equalizer_of_unique (t : ∀ (y : Y), g y = h y → ∃! (x : X), f x = y) :
is_limit (fork.of_ι _ w) :=
fork.is_limit.mk' _ $ λ s,
begin
refine ⟨λ i, _, _, _⟩,
{ apply classical.some (t (s.ι i) _),
apply congr_fun s.condition i },
{ ext i,
apply (classical.some_spec (t (s.ι i) _)).1 },
{ intros m hm,
ext i,
apply (classical.some_spec (t (s.ι i) _)).2,
apply congr_fun hm i },
end
/-- The converse of `type_equalizer_of_unique`. -/
lemma unique_of_type_equalizer (t : is_limit (fork.of_ι _ w)) (y : Y) (hy : g y = h y) :
∃! (x : X), f x = y :=
begin
let y' : punit ⟶ Y := λ _, y,
have hy' : y' ≫ g = y' ≫ h := funext (λ _, hy),
refine ⟨(fork.is_limit.lift' t _ hy').1 ⟨⟩, congr_fun (fork.is_limit.lift' t y' _).2 ⟨⟩, _⟩,
intros x' hx',
suffices : (λ (_ : punit), x') = (fork.is_limit.lift' t y' hy').1,
rw ← this,
apply fork.is_limit.hom_ext t,
ext ⟨⟩,
apply hx'.trans (congr_fun (fork.is_limit.lift' t _ hy').2 ⟨⟩).symm,
end
lemma type_equalizer_iff_unique :
nonempty (is_limit (fork.of_ι _ w)) ↔ (∀ (y : Y), g y = h y → ∃! (x : X), f x = y) :=
⟨λ i, unique_of_type_equalizer _ _ (classical.choice i), λ k, ⟨type_equalizer_of_unique f w k⟩⟩
/-- Show that the subtype `{x : Y // g x = h x}` is an equalizer for the pair `(g,h)`. -/
def equalizer_limit : limits.limit_cone (parallel_pair g h) :=
{ cone := fork.of_ι (subtype.val : {x : Y // g x = h x} → Y) (funext subtype.prop),
is_limit := fork.is_limit.mk' _ $ λ s,
⟨λ i, ⟨s.ι i, by apply congr_fun s.condition i⟩,
rfl,
λ m hm, funext $ λ x, subtype.ext (congr_fun hm x)⟩ }
variables (g h)
/-- The categorical equalizer in `Type u` is `{x : Y // g x = h x}`. -/
noncomputable def equalizer_iso : equalizer g h ≅ {x : Y // g x = h x} :=
limit.iso_limit_cone equalizer_limit
@[simp, elementwise] lemma equalizer_iso_hom_comp_subtype :
(equalizer_iso g h).hom ≫ subtype.val = equalizer.ι g h :=
rfl
@[simp, elementwise] lemma equalizer_iso_inv_comp_ι :
(equalizer_iso g h).inv ≫ equalizer.ι g h = subtype.val :=
limit.iso_limit_cone_inv_π equalizer_limit walking_parallel_pair.zero
end fork
section cofork
variables {X Y Z : Type u} (f g : X ⟶ Y)
/-- (Implementation) The relation to be quotiented to obtain the coequalizer. -/
inductive coequalizer_rel : Y → Y → Prop
| rel (x : X) : coequalizer_rel (f x) (g x)
/--
Show that the quotient by the relation generated by `f(x) ~ g(x)`
is a coequalizer for the pair `(f, g)`.
-/
def coequalizer_colimit : limits.colimit_cocone (parallel_pair f g) :=
{ cocone := cofork.of_π (quot.mk (coequalizer_rel f g))
(funext (λ x, quot.sound (coequalizer_rel.rel x))),
is_colimit := cofork.is_colimit.mk' _ $ λ s,
⟨ quot.lift s.π (λ a b (h : coequalizer_rel f g a b),
by { cases h, exact congr_fun s.condition h_1 }),
rfl,
λ m hm, funext $ λ x, quot.induction_on x (congr_fun hm : _) ⟩ }
/-- If `π : Y ⟶ Z` is an equalizer for `(f, g)`, and `U ⊆ Y` such that `f ⁻¹' U = g ⁻¹' U`,
then `π ⁻¹' (π '' U) = U`.
-/
lemma coequalizer_preimage_image_eq_of_preimage_eq (π : Y ⟶ Z)
(e : f ≫ π = g ≫ π) (h : is_colimit (cofork.of_π π e)) (U : set Y) (H : f ⁻¹' U = g ⁻¹' U) :
π ⁻¹' (π '' U) = U :=
begin
have lem : ∀ x y, (coequalizer_rel f g x y) → (x ∈ U ↔ y ∈ U),
{ rintros _ _ ⟨x⟩, change x ∈ f ⁻¹' U ↔ x ∈ g ⁻¹' U, congr' 2 },
have eqv : _root_.equivalence (λ x y, x ∈ U ↔ y ∈ U) := by tidy,
ext,
split,
{ rw ← (show _ = π, from h.comp_cocone_point_unique_up_to_iso_inv
(coequalizer_colimit f g).2 walking_parallel_pair.one),
rintro ⟨y, hy, e'⟩,
dsimp at e',
replace e' := (mono_iff_injective (h.cocone_point_unique_up_to_iso
(coequalizer_colimit f g).is_colimit).inv).mp infer_instance e',
exact (eqv.eqv_gen_iff.mp (eqv_gen.mono lem (quot.exact _ e'))).mp hy },
{ exact λ hx, ⟨x, hx, rfl⟩ }
end
/-- The categorical coequalizer in `Type u` is the quotient by `f g ~ g x`. -/
noncomputable def coequalizer_iso : coequalizer f g ≅ _root_.quot (coequalizer_rel f g) :=
colimit.iso_colimit_cocone (coequalizer_colimit f g)
@[simp, elementwise] lemma coequalizer_iso_π_comp_hom :
coequalizer.π f g ≫ (coequalizer_iso f g).hom = quot.mk (coequalizer_rel f g) :=
colimit.iso_colimit_cocone_ι_hom (coequalizer_colimit f g) walking_parallel_pair.one
@[simp, elementwise] lemma coequalizer_iso_quot_comp_inv :
↾(quot.mk (coequalizer_rel f g)) ≫ (coequalizer_iso f g).inv = coequalizer.π f g :=
rfl
end cofork
section pullback
open category_theory.limits.walking_pair
open category_theory.limits.walking_cospan
open category_theory.limits.walking_cospan.hom
variables {W X Y Z : Type u}
variables (f : X ⟶ Z) (g : Y ⟶ Z)
/--
The usual explicit pullback in the category of types, as a subtype of the product.
The full `limit_cone` data is bundled as `pullback_limit_cone f g`.
-/
@[nolint has_nonempty_instance]
abbreviation pullback_obj : Type u := { p : X × Y // f p.1 = g p.2 }
-- `pullback_obj f g` comes with a coercion to the product type `X × Y`.
example (p : pullback_obj f g) : X × Y := p
/--
The explicit pullback cone on `pullback_obj f g`.
This is bundled with the `is_limit` data as `pullback_limit_cone f g`.
-/
abbreviation pullback_cone : limits.pullback_cone f g :=
pullback_cone.mk (λ p : pullback_obj f g, p.1.1) (λ p, p.1.2) (funext (λ p, p.2))
/--
The explicit pullback in the category of types, bundled up as a `limit_cone`
for given `f` and `g`.
-/
@[simps]
def pullback_limit_cone (f : X ⟶ Z) (g : Y ⟶ Z) : limits.limit_cone (cospan f g) :=
{ cone := pullback_cone f g,
is_limit := pullback_cone.is_limit_aux _
(λ s x, ⟨⟨s.fst x, s.snd x⟩, congr_fun s.condition x⟩)
(by tidy)
(by tidy)
(λ s m w, funext $ λ x, subtype.ext $
prod.ext (congr_fun (w walking_cospan.left) x)
(congr_fun (w walking_cospan.right) x)) }
/--
The pullback cone given by the instance `has_pullbacks (Type u)` is isomorphic to the
explicit pullback cone given by `pullback_limit_cone`.
-/
noncomputable def pullback_cone_iso_pullback : limit.cone (cospan f g) ≅ pullback_cone f g :=
(limit.is_limit _).unique_up_to_iso (pullback_limit_cone f g).is_limit
/--
The pullback given by the instance `has_pullbacks (Type u)` is isomorphic to the
explicit pullback object given by `pullback_limit_obj`.
-/
noncomputable def pullback_iso_pullback : pullback f g ≅ pullback_obj f g :=
(cones.forget _).map_iso $ pullback_cone_iso_pullback f g
@[simp] lemma pullback_iso_pullback_hom_fst (p : pullback f g) :
((pullback_iso_pullback f g).hom p : X × Y).fst = (pullback.fst : _ ⟶ X) p :=
congr_fun ((pullback_cone_iso_pullback f g).hom.w left) p
@[simp] lemma pullback_iso_pullback_hom_snd (p : pullback f g) :
((pullback_iso_pullback f g).hom p : X × Y).snd = (pullback.snd : _ ⟶ Y) p :=
congr_fun ((pullback_cone_iso_pullback f g).hom.w right) p
@[simp] lemma pullback_iso_pullback_inv_fst :
(pullback_iso_pullback f g).inv ≫ pullback.fst = (λ p, (p : X × Y).fst) :=
(pullback_cone_iso_pullback f g).inv.w left
@[simp] lemma pullback_iso_pullback_inv_snd :
(pullback_iso_pullback f g).inv ≫ pullback.snd = (λ p, (p : X × Y).snd) :=
(pullback_cone_iso_pullback f g).inv.w right
end pullback
end category_theory.limits.types
|
f1ceeb912bfaa57d3e9022a250e55894abbfea67 | 60bf3fa4185ec5075eaea4384181bfbc7e1dc319 | /src/game/series/L01defs.lean | c1ddccd83d1cfe80dc8d1eb416970beebbcb5033 | [
"Apache-2.0"
] | permissive | anrddh/real-number-game | 660f1127d03a78fd35986c771d65c3132c5f4025 | c708c4e02ec306c657e1ea67862177490db041b0 | refs/heads/master | 1,668,214,277,092 | 1,593,105,075,000 | 1,593,105,075,000 | 264,269,218 | 0 | 0 | null | 1,589,567,264,000 | 1,589,567,264,000 | null | UTF-8 | Lean | false | false | 454 | lean | import data.real.basic
import topology.algebra.infinite_sum
def partial_sum (a : ℕ → ℝ) (M : ℕ):= (finset.range M).sum a
def partial_sum_sequence (a : ℕ → ℝ ) : ℕ → ℝ := (λ (n : ℕ), partial_sum a n )
notation `|` x `|` := abs x -- hide
def is_limit (a : ℕ → ℝ) (L : ℝ) := ∀ ε : ℝ, 0 < ε → ∃ N : ℕ,
∀ n : ℕ, N ≤ n → |a n - L| < ε
def is_convergent (a : ℕ → ℝ) := ∃ L : ℝ, is_limit a L
|
653f93a5ea5771a8586cbc530e617de8c5077794 | a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91 | /hott/algebra/field.hlean | 094f3925ef9a713fe55eb895355d2100827837ef | [
"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 | 21,803 | hlean | /-
Copyright (c) 2014 Robert Lewis. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Robert Lewis
Structures with multiplicative and additive components, including division rings and fields.
The development is modeled after Isabelle's library.
-/
import algebra.binary algebra.group algebra.ring
open eq eq.ops algebra
set_option class.force_new true
variable {A : Type}
namespace algebra
structure division_ring [class] (A : Type) extends ring A, has_inv A, zero_ne_one_class A :=
(mul_inv_cancel : Π{a}, a ≠ zero → mul a (inv a) = one)
(inv_mul_cancel : Π{a}, a ≠ zero → mul (inv a) a = one)
section division_ring
variables [s : division_ring A] {a b c : A}
include s
protected definition algebra.div (a b : A) : A := a * b⁻¹
definition division_ring_has_div [instance] : has_div A :=
has_div.mk algebra.div
lemma division.def (a b : A) : a / b = a * b⁻¹ :=
rfl
theorem mul_inv_cancel (H : a ≠ 0) : a * a⁻¹ = 1 :=
division_ring.mul_inv_cancel H
theorem inv_mul_cancel (H : a ≠ 0) : a⁻¹ * a = 1 :=
division_ring.inv_mul_cancel H
theorem inv_eq_one_div (a : A) : a⁻¹ = 1 / a := !one_mul⁻¹
theorem div_eq_mul_one_div (a b : A) : a / b = a * (1 / b) :=
by rewrite [*division.def, one_mul]
theorem mul_one_div_cancel (H : a ≠ 0) : a * (1 / a) = 1 :=
by rewrite [-inv_eq_one_div, (mul_inv_cancel H)]
theorem one_div_mul_cancel (H : a ≠ 0) : (1 / a) * a = 1 :=
by rewrite [-inv_eq_one_div, (inv_mul_cancel H)]
theorem div_self (H : a ≠ 0) : a / a = 1 := mul_inv_cancel H
theorem one_div_one : 1 / 1 = (1:A) := div_self (ne.symm zero_ne_one)
theorem mul_div_assoc (a b : A) : (a * b) / c = a * (b / c) := !mul.assoc
theorem one_div_ne_zero (H : a ≠ 0) : 1 / a ≠ 0 :=
assume H2 : 1 / a = 0,
have C1 : 0 = (1:A), from symm (by rewrite [-(mul_one_div_cancel H), H2, mul_zero]),
absurd C1 zero_ne_one
theorem one_inv_eq : 1⁻¹ = (1:A) :=
by rewrite [-mul_one, inv_mul_cancel (ne.symm (@zero_ne_one A _))]
theorem div_one (a : A) : a / 1 = a :=
by rewrite [*division.def, one_inv_eq, mul_one]
theorem zero_div (a : A) : 0 / a = 0 := !zero_mul
-- note: integral domain has a "mul_ne_zero". A commutative division ring is an integral
-- domain, but let's not define that class for now.
theorem division_ring.mul_ne_zero (Ha : a ≠ 0) (Hb : b ≠ 0) : a * b ≠ 0 :=
assume H : a * b = 0,
have C1 : a = 0, by rewrite [-mul_one, -(mul_one_div_cancel Hb), -mul.assoc, H, zero_mul],
absurd C1 Ha
theorem mul_ne_zero_comm (H : a * b ≠ 0) : b * a ≠ 0 :=
have H2 : a ≠ 0 × b ≠ 0, from ne_zero_prod_ne_zero_of_mul_ne_zero H,
division_ring.mul_ne_zero (prod.pr2 H2) (prod.pr1 H2)
theorem eq_one_div_of_mul_eq_one (H : a * b = 1) : b = 1 / a :=
have a ≠ 0, from
(suppose a = 0,
have 0 = (1:A), by rewrite [-(zero_mul b), -this, H],
absurd this zero_ne_one),
show b = 1 / a, from symm (calc
1 / a = (1 / a) * 1 : mul_one
... = (1 / a) * (a * b) : H
... = (1 / a) * a * b : mul.assoc
... = 1 * b : one_div_mul_cancel this
... = b : one_mul)
theorem eq_one_div_of_mul_eq_one_left (H : b * a = 1) : b = 1 / a :=
have a ≠ 0, from
(suppose a = 0,
have 0 = 1, from symm (calc
1 = b * a : symm H
... = b * 0 : this
... = 0 : mul_zero),
absurd this zero_ne_one),
show b = 1 / a, from symm (calc
1 / a = 1 * (1 / a) : one_mul
... = b * a * (1 / a) : H
... = b * (a * (1 / a)) : mul.assoc
... = b * 1 : mul_one_div_cancel this
... = b : mul_one)
theorem division_ring.one_div_mul_one_div (Ha : a ≠ 0) (Hb : b ≠ 0) :
(1 / a) * (1 / b) = 1 / (b * a) :=
have (b * a) * ((1 / a) * (1 / b)) = 1, by
rewrite [mul.assoc, -(mul.assoc a), (mul_one_div_cancel Ha), one_mul,
(mul_one_div_cancel Hb)],
eq_one_div_of_mul_eq_one this
theorem one_div_neg_one_eq_neg_one : (1:A) / (-1) = -1 :=
have (-1) * (-1) = (1:A), by rewrite [-neg_eq_neg_one_mul, neg_neg],
symm (eq_one_div_of_mul_eq_one this)
theorem division_ring.one_div_neg_eq_neg_one_div (H : a ≠ 0) : 1 / (- a) = - (1 / a) :=
have -1 ≠ (0:A), from
(suppose -1 = 0, absurd (symm (calc
1 = -(-1) : neg_neg
... = -0 : this
... = (0:A) : neg_zero)) zero_ne_one),
calc
1 / (- a) = 1 / ((-1) * a) : neg_eq_neg_one_mul
... = (1 / a) * (1 / (- 1)) : division_ring.one_div_mul_one_div H this
... = (1 / a) * (-1) : one_div_neg_one_eq_neg_one
... = - (1 / a) : mul_neg_one_eq_neg
theorem div_neg_eq_neg_div (b : A) (Ha : a ≠ 0) : b / (- a) = - (b / a) :=
calc
b / (- a) = b * (1 / (- a)) : by rewrite -inv_eq_one_div
... = b * -(1 / a) : division_ring.one_div_neg_eq_neg_one_div Ha
... = -(b * (1 / a)) : neg_mul_eq_mul_neg
... = - (b * a⁻¹) : inv_eq_one_div
theorem neg_div (a b : A) : (-b) / a = - (b / a) :=
by rewrite [neg_eq_neg_one_mul, mul_div_assoc, -neg_eq_neg_one_mul]
theorem division_ring.neg_div_neg_eq (a : A) {b : A} (Hb : b ≠ 0) : (-a) / (-b) = a / b :=
by rewrite [(div_neg_eq_neg_div _ Hb), neg_div, neg_neg]
theorem division_ring.one_div_one_div (H : a ≠ 0) : 1 / (1 / a) = a :=
symm (eq_one_div_of_mul_eq_one_left (mul_one_div_cancel H))
theorem division_ring.eq_of_one_div_eq_one_div (Ha : a ≠ 0) (Hb : b ≠ 0) (H : 1 / a = 1 / b) :
a = b :=
by rewrite [-(division_ring.one_div_one_div Ha), H, (division_ring.one_div_one_div Hb)]
theorem mul_inv_eq (Ha : a ≠ 0) (Hb : b ≠ 0) : (b * a)⁻¹ = a⁻¹ * b⁻¹ :=
inverse (calc
a⁻¹ * b⁻¹ = (1 / a) * b⁻¹ : inv_eq_one_div
... = (1 / a) * (1 / b) : inv_eq_one_div
... = (1 / (b * a)) : division_ring.one_div_mul_one_div Ha Hb
... = (b * a)⁻¹ : inv_eq_one_div)
theorem mul_div_cancel (a : A) {b : A} (Hb : b ≠ 0) : a * b / b = a :=
by rewrite [*division.def, mul.assoc, (mul_inv_cancel Hb), mul_one]
theorem div_mul_cancel (a : A) {b : A} (Hb : b ≠ 0) : a / b * b = a :=
by rewrite [*division.def, mul.assoc, (inv_mul_cancel Hb), mul_one]
theorem div_add_div_same (a b c : A) : a / c + b / c = (a + b) / c := !right_distrib⁻¹
theorem div_sub_div_same (a b c : A) : (a / c) - (b / c) = (a - b) / c :=
by rewrite [sub_eq_add_neg, -neg_div, div_add_div_same]
theorem one_div_mul_add_mul_one_div_eq_one_div_add_one_div (Ha : a ≠ 0) (Hb : b ≠ 0) :
(1 / a) * (a + b) * (1 / b) = 1 / a + 1 / b :=
by rewrite [(left_distrib (1 / a)), (one_div_mul_cancel Ha), right_distrib, one_mul,
mul.assoc, (mul_one_div_cancel Hb), mul_one, add.comm]
theorem one_div_mul_sub_mul_one_div_eq_one_div_add_one_div (Ha : a ≠ 0) (Hb : b ≠ 0) :
(1 / a) * (b - a) * (1 / b) = 1 / a - 1 / b :=
by rewrite [(mul_sub_left_distrib (1 / a)), (one_div_mul_cancel Ha), mul_sub_right_distrib,
one_mul, mul.assoc, (mul_one_div_cancel Hb), mul_one]
theorem div_eq_one_iff_eq (a : A) {b : A} (Hb : b ≠ 0) : a / b = 1 ↔ a = b :=
iff.intro
(suppose a / b = 1, symm (calc
b = 1 * b : one_mul
... = a / b * b : this
... = a : div_mul_cancel _ Hb))
(suppose a = b, calc
a / b = b / b : this
... = 1 : div_self Hb)
theorem eq_of_div_eq_one (a : A) {b : A} (Hb : b ≠ 0) : a / b = 1 → a = b :=
iff.mp (!div_eq_one_iff_eq Hb)
theorem eq_div_iff_mul_eq (a : A) {b : A} (Hc : c ≠ 0) : a = b / c ↔ a * c = b :=
iff.intro
(suppose a = b / c, by rewrite [this, (!div_mul_cancel Hc)])
(suppose a * c = b, by rewrite [-(!mul_div_cancel Hc), this])
theorem eq_div_of_mul_eq (a b : A) {c : A} (Hc : c ≠ 0) : a * c = b → a = b / c :=
iff.mpr (!eq_div_iff_mul_eq Hc)
theorem mul_eq_of_eq_div (a b: A) {c : A} (Hc : c ≠ 0) : a = b / c → a * c = b :=
iff.mp (!eq_div_iff_mul_eq Hc)
theorem add_div_eq_mul_add_div (a b : A) {c : A} (Hc : c ≠ 0) : a + b / c = (a * c + b) / c :=
have (a + b / c) * c = a * c + b, by rewrite [right_distrib, (!div_mul_cancel Hc)],
(iff.elim_right (!eq_div_iff_mul_eq Hc)) this
theorem mul_mul_div (a : A) {c : A} (Hc : c ≠ 0) : a = a * c * (1 / c) :=
calc
a = a * 1 : mul_one
... = a * (c * (1 / c)) : mul_one_div_cancel Hc
... = a * c * (1 / c) : mul.assoc
-- There are many similar rules to these last two in the Isabelle library
-- that haven't been ported yet. Do as necessary.
end division_ring
structure field [class] (A : Type) extends division_ring A, comm_ring A
section field
variables [s : field A] {a b c d: A}
include s
theorem field.one_div_mul_one_div (Ha : a ≠ 0) (Hb : b ≠ 0) : (1 / a) * (1 / b) = 1 / (a * b) :=
by rewrite [(division_ring.one_div_mul_one_div Ha Hb), mul.comm b]
theorem field.div_mul_right (Hb : b ≠ 0) (H : a * b ≠ 0) : a / (a * b) = 1 / b :=
have a ≠ 0, from prod.pr1 (ne_zero_prod_ne_zero_of_mul_ne_zero H),
symm (calc
1 / b = 1 * (1 / b) : one_mul
... = (a * a⁻¹) * (1 / b) : mul_inv_cancel this
... = a * (a⁻¹ * (1 / b)) : mul.assoc
... = a * ((1 / a) * (1 / b)) : inv_eq_one_div
... = a * (1 / (b * a)) : division_ring.one_div_mul_one_div this Hb
... = a * (1 / (a * b)) : mul.comm
... = a * (a * b)⁻¹ : inv_eq_one_div)
theorem field.div_mul_left (Ha : a ≠ 0) (H : a * b ≠ 0) : b / (a * b) = 1 / a :=
let H1 : b * a ≠ 0 := mul_ne_zero_comm H in
by rewrite [mul.comm a, (field.div_mul_right Ha H1)]
theorem mul_div_cancel_left (Ha : a ≠ 0) : a * b / a = b :=
by rewrite [mul.comm a, (!mul_div_cancel Ha)]
theorem mul_div_cancel' (Hb : b ≠ 0) : b * (a / b) = a :=
by rewrite [mul.comm, (!div_mul_cancel Hb)]
theorem one_div_add_one_div (Ha : a ≠ 0) (Hb : b ≠ 0) : 1 / a + 1 / b = (a + b) / (a * b) :=
have a * b ≠ 0, from (division_ring.mul_ne_zero Ha Hb),
by rewrite [add.comm, -(field.div_mul_left Ha this), -(field.div_mul_right Hb this), *division.def,
-right_distrib]
theorem field.div_mul_div (a : A) {b : A} (c : A) {d : A} (Hb : b ≠ 0) (Hd : d ≠ 0) :
(a / b) * (c / d) = (a * c) / (b * d) :=
by rewrite [*division.def, 2 mul.assoc, (mul.comm b⁻¹), mul.assoc, (mul_inv_eq Hd Hb)]
theorem mul_div_mul_left (a : A) {b c : A} (Hb : b ≠ 0) (Hc : c ≠ 0) :
(c * a) / (c * b) = a / b :=
by rewrite [-(!field.div_mul_div Hc Hb), (div_self Hc), one_mul]
theorem mul_div_mul_right (a : A) {b c : A} (Hb : b ≠ 0) (Hc : c ≠ 0) :
(a * c) / (b * c) = a / b :=
by rewrite [(mul.comm a), (mul.comm b), (!mul_div_mul_left Hb Hc)]
theorem div_mul_eq_mul_div (a b c : A) : (b / c) * a = (b * a) / c :=
by rewrite [*division.def, mul.assoc, (mul.comm c⁻¹), -mul.assoc]
theorem field.div_mul_eq_mul_div_comm (a b : A) {c : A} (Hc : c ≠ 0) :
(b / c) * a = b * (a / c) :=
by rewrite [(div_mul_eq_mul_div), -(one_mul c), -(!field.div_mul_div (ne.symm zero_ne_one) Hc),
div_one, one_mul]
theorem div_add_div (a : A) {b : A} (c : A) {d : A} (Hb : b ≠ 0) (Hd : d ≠ 0) :
(a / b) + (c / d) = ((a * d) + (b * c)) / (b * d) :=
by rewrite [-(!mul_div_mul_right Hb Hd), -(!mul_div_mul_left Hd Hb), div_add_div_same]
theorem div_sub_div (a : A) {b : A} (c : A) {d : A} (Hb : b ≠ 0) (Hd : d ≠ 0) :
(a / b) - (c / d) = ((a * d) - (b * c)) / (b * d) :=
by rewrite [*sub_eq_add_neg, neg_eq_neg_one_mul, -mul_div_assoc, (!div_add_div Hb Hd),
-mul.assoc, (mul.comm b), mul.assoc, -neg_eq_neg_one_mul]
theorem mul_eq_mul_of_div_eq_div (a : A) {b : A} (c : A) {d : A} (Hb : b ≠ 0)
(Hd : d ≠ 0) (H : a / b = c / d) : a * d = c * b :=
by rewrite [-mul_one, mul.assoc, (mul.comm d), -mul.assoc, -(div_self Hb),
-(!field.div_mul_eq_mul_div_comm Hb), H, (div_mul_eq_mul_div), (!div_mul_cancel Hd)]
theorem field.one_div_div (Ha : a ≠ 0) (Hb : b ≠ 0) : 1 / (a / b) = b / a :=
have (a / b) * (b / a) = 1, from calc
(a / b) * (b / a) = (a * b) / (b * a) : !field.div_mul_div Hb Ha
... = (a * b) / (a * b) : mul.comm
... = 1 : div_self (division_ring.mul_ne_zero Ha Hb),
symm (eq_one_div_of_mul_eq_one this)
theorem field.div_div_eq_mul_div (a : A) {b c : A} (Hb : b ≠ 0) (Hc : c ≠ 0) :
a / (b / c) = (a * c) / b :=
by rewrite [div_eq_mul_one_div, (field.one_div_div Hb Hc), -mul_div_assoc]
theorem field.div_div_eq_div_mul (a : A) {b c : A} (Hb : b ≠ 0) (Hc : c ≠ 0) :
(a / b) / c = a / (b * c) :=
by rewrite [div_eq_mul_one_div, (!field.div_mul_div Hb Hc), mul_one]
theorem field.div_div_div_div_eq (a : A) {b c d : A} (Hb : b ≠ 0) (Hc : c ≠ 0) (Hd : d ≠ 0) :
(a / b) / (c / d) = (a * d) / (b * c) :=
by rewrite [(!field.div_div_eq_mul_div Hc Hd), (div_mul_eq_mul_div),
(!field.div_div_eq_div_mul Hb Hc)]
theorem field.div_mul_eq_div_mul_one_div (a : A) {b c : A} (Hb : b ≠ 0) (Hc : c ≠ 0) :
a / (b * c) = (a / b) * (1 / c) :=
by rewrite [-!field.div_div_eq_div_mul Hb Hc, -div_eq_mul_one_div]
theorem eq_of_mul_eq_mul_of_nonzero_left {a b c : A} (H : a ≠ 0) (H2 : a * b = a * c) : b = c :=
by rewrite [-one_mul b, -div_self H, div_mul_eq_mul_div, H2, mul_div_cancel_left H]
theorem eq_of_mul_eq_mul_of_nonzero_right {a b c : A} (H : c ≠ 0) (H2 : a * c = b * c) : a = b :=
by rewrite [-mul_one a, -div_self H, -mul_div_assoc, H2, mul_div_cancel _ H]
end field
structure discrete_field [class] (A : Type) extends field A :=
(has_decidable_eq : decidable_eq A)
(inv_zero : inv zero = zero)
attribute discrete_field.has_decidable_eq [instance]
section discrete_field
variable [s : discrete_field A]
include s
variables {a b c d : A}
-- many of the theorems in discrete_field are the same as theorems in field sum division ring,
-- but with fewer hypotheses since 0⁻¹ = 0 and equality is decidable.
theorem discrete_field.eq_zero_sum_eq_zero_of_mul_eq_zero
(x y : A) (H : x * y = 0) : x = 0 ⊎ y = 0 :=
decidable.by_cases
(suppose x = 0, sum.inl this)
(suppose x ≠ 0,
sum.inr (by rewrite [-one_mul, -(inv_mul_cancel this), mul.assoc, H, mul_zero]))
definition discrete_field.to_integral_domain [trans_instance] : integral_domain A :=
⦃ integral_domain, s,
eq_zero_sum_eq_zero_of_mul_eq_zero := discrete_field.eq_zero_sum_eq_zero_of_mul_eq_zero⦄
theorem inv_zero : 0⁻¹ = (0:A) := !discrete_field.inv_zero
theorem one_div_zero : 1 / 0 = (0:A) :=
calc
1 / 0 = 1 * 0⁻¹ : refl
... = 1 * 0 : inv_zero
... = 0 : mul_zero
theorem div_zero (a : A) : a / 0 = 0 := by rewrite [div_eq_mul_one_div, one_div_zero, mul_zero]
theorem ne_zero_of_one_div_ne_zero (H : 1 / a ≠ 0) : a ≠ 0 :=
assume Ha : a = 0, absurd (Ha⁻¹ ▸ one_div_zero) H
theorem eq_zero_of_one_div_eq_zero (H : 1 / a = 0) : a = 0 :=
decidable.by_cases
(assume Ha, Ha)
(assume Ha, empty.elim ((one_div_ne_zero Ha) H))
variables (a b)
theorem one_div_mul_one_div' : (1 / a) * (1 / b) = 1 / (b * a) :=
decidable.by_cases
(suppose a = 0,
by rewrite [this, div_zero, zero_mul, -(@div_zero A s 1), mul_zero b])
(assume Ha : a ≠ 0,
decidable.by_cases
(suppose b = 0,
by rewrite [this, div_zero, mul_zero, -(@div_zero A s 1), zero_mul a])
(suppose b ≠ 0, division_ring.one_div_mul_one_div Ha this))
theorem one_div_neg_eq_neg_one_div : 1 / (- a) = - (1 / a) :=
decidable.by_cases
(suppose a = 0, by rewrite [this, neg_zero, 2 div_zero, neg_zero])
(suppose a ≠ 0, division_ring.one_div_neg_eq_neg_one_div this)
theorem neg_div_neg_eq : (-a) / (-b) = a / b :=
decidable.by_cases
(assume Hb : b = 0, by rewrite [Hb, neg_zero, 2 div_zero])
(assume Hb : b ≠ 0, !division_ring.neg_div_neg_eq Hb)
theorem one_div_one_div : 1 / (1 / a) = a :=
decidable.by_cases
(assume Ha : a = 0, by rewrite [Ha, 2 div_zero])
(assume Ha : a ≠ 0, division_ring.one_div_one_div Ha)
variables {a b}
theorem eq_of_one_div_eq_one_div (H : 1 / a = 1 / b) : a = b :=
decidable.by_cases
(assume Ha : a = 0,
have Hb : b = 0, from eq_zero_of_one_div_eq_zero (by rewrite [-H, Ha, div_zero]),
Hb⁻¹ ▸ Ha)
(assume Ha : a ≠ 0,
have Hb : b ≠ 0, from ne_zero_of_one_div_ne_zero (H ▸ (one_div_ne_zero Ha)),
division_ring.eq_of_one_div_eq_one_div Ha Hb H)
variables (a b)
theorem mul_inv' : (b * a)⁻¹ = a⁻¹ * b⁻¹ :=
decidable.by_cases
(assume Ha : a = 0, by rewrite [Ha, mul_zero, 2 inv_zero, zero_mul])
(assume Ha : a ≠ 0,
decidable.by_cases
(assume Hb : b = 0, by rewrite [Hb, zero_mul, 2 inv_zero, mul_zero])
(assume Hb : b ≠ 0, mul_inv_eq Ha Hb))
-- the following are specifically for fields
theorem one_div_mul_one_div : (1 / a) * (1 / b) = 1 / (a * b) :=
by rewrite [one_div_mul_one_div', mul.comm b]
variable {a}
theorem div_mul_right (Ha : a ≠ 0) : a / (a * b) = 1 / b :=
decidable.by_cases
(assume Hb : b = 0, by rewrite [Hb, mul_zero, 2 div_zero])
(assume Hb : b ≠ 0, field.div_mul_right Hb (mul_ne_zero Ha Hb))
variables (a) {b}
theorem div_mul_left (Hb : b ≠ 0) : b / (a * b) = 1 / a :=
by rewrite [mul.comm a, div_mul_right _ Hb]
variables (a b c)
theorem div_mul_div : (a / b) * (c / d) = (a * c) / (b * d) :=
decidable.by_cases
(assume Hb : b = 0, by rewrite [Hb, div_zero, zero_mul, -(@div_zero A s (a * c)), zero_mul])
(assume Hb : b ≠ 0,
decidable.by_cases
(assume Hd : d = 0, by rewrite [Hd, div_zero, mul_zero, -(@div_zero A s (a * c)),
mul_zero])
(assume Hd : d ≠ 0, !field.div_mul_div Hb Hd))
variable {c}
theorem mul_div_mul_left' (Hc : c ≠ 0) : (c * a) / (c * b) = a / b :=
decidable.by_cases
(assume Hb : b = 0, by rewrite [Hb, mul_zero, 2 div_zero])
(assume Hb : b ≠ 0, !mul_div_mul_left Hb Hc)
theorem mul_div_mul_right' (Hc : c ≠ 0) : (a * c) / (b * c) = a / b :=
by rewrite [(mul.comm a), (mul.comm b), (!mul_div_mul_left' Hc)]
variables (a b c d)
theorem div_mul_eq_mul_div_comm : (b / c) * a = b * (a / c) :=
decidable.by_cases
(assume Hc : c = 0, by rewrite [Hc, div_zero, zero_mul, -(mul_zero b), -(@div_zero A s a)])
(assume Hc : c ≠ 0, !field.div_mul_eq_mul_div_comm Hc)
theorem one_div_div : 1 / (a / b) = b / a :=
decidable.by_cases
(assume Ha : a = 0, by rewrite [Ha, zero_div, 2 div_zero])
(assume Ha : a ≠ 0,
decidable.by_cases
(assume Hb : b = 0, by rewrite [Hb, 2 div_zero, zero_div])
(assume Hb : b ≠ 0, field.one_div_div Ha Hb))
theorem div_div_eq_mul_div : a / (b / c) = (a * c) / b :=
by rewrite [div_eq_mul_one_div, one_div_div, -mul_div_assoc]
theorem div_div_eq_div_mul : (a / b) / c = a / (b * c) :=
by rewrite [div_eq_mul_one_div, div_mul_div, mul_one]
theorem div_div_div_div_eq : (a / b) / (c / d) = (a * d) / (b * c) :=
by rewrite [div_div_eq_mul_div, div_mul_eq_mul_div, div_div_eq_div_mul]
variable {a}
theorem div_helper (H : a ≠ 0) : (1 / (a * b)) * a = 1 / b :=
by rewrite [div_mul_eq_mul_div, one_mul, !div_mul_right H]
variable (a)
theorem div_mul_eq_div_mul_one_div : a / (b * c) = (a / b) * (1 / c) :=
by rewrite [-div_div_eq_div_mul, -div_eq_mul_one_div]
end discrete_field
namespace norm_num
theorem div_add_helper [s : field A] (n d b c val : A) (Hd : d ≠ 0) (H : n + b * d = val)
(H2 : c * d = val) : n / d + b = c :=
begin
apply eq_of_mul_eq_mul_of_nonzero_right Hd,
rewrite [H2, -H, right_distrib, div_mul_cancel _ Hd]
end
theorem add_div_helper [s : field A] (n d b c val : A) (Hd : d ≠ 0) (H : d * b + n = val)
(H2 : d * c = val) : b + n / d = c :=
begin
apply eq_of_mul_eq_mul_of_nonzero_left Hd,
rewrite [H2, -H, left_distrib, mul_div_cancel' Hd]
end
theorem div_mul_helper [s : field A] (n d c v : A) (Hd : d ≠ 0) (H : (n * c) / d = v) :
(n / d) * c = v :=
by rewrite [-H, field.div_mul_eq_mul_div_comm _ _ Hd, mul_div_assoc]
theorem mul_div_helper [s : field A] (a n d v : A) (Hd : d ≠ 0) (H : (a * n) / d = v) :
a * (n / d) = v :=
by rewrite [-H, mul_div_assoc]
theorem nonzero_of_div_helper [s : field A] (a b : A) (Ha : a ≠ 0) (Hb : b ≠ 0) : a / b ≠ 0 :=
begin
intro Hab,
have Habb : (a / b) * b = 0, by rewrite [Hab, zero_mul],
rewrite [div_mul_cancel _ Hb at Habb],
exact Ha Habb
end
theorem div_helper [s : field A] (n d v : A) (Hd : d ≠ 0) (H : v * d = n) : n / d = v :=
begin
apply eq_of_mul_eq_mul_of_nonzero_right Hd,
rewrite (div_mul_cancel _ Hd),
exact inverse H
end
theorem div_eq_div_helper [s : field A] (a b c d v : A) (H1 : a * d = v) (H2 : c * b = v)
(Hb : b ≠ 0) (Hd : d ≠ 0) : a / b = c / d :=
begin
apply eq_div_of_mul_eq,
exact Hd,
rewrite div_mul_eq_mul_div,
apply inverse,
apply eq_div_of_mul_eq,
exact Hb,
rewrite [H1, H2]
end
theorem subst_into_div [s : has_div A] (a₁ b₁ a₂ b₂ v : A) (H : a₁ / b₁ = v) (H1 : a₂ = a₁)
(H2 : b₂ = b₁) : a₂ / b₂ = v :=
by rewrite [H1, H2, H]
end norm_num
end algebra
|
bbf57356bdd4ff8065aa30b0dcb4916a70cf9f86 | 0f5090f82d527e0df5bf3adac9f9e2e1d81d71e2 | /src/nitin+maya/lftcm2020_exercises.lean | afb57a6a16e81ba4322e4ee663e9d4fd9ab6d214 | [] | no_license | apurvanakade/mc2020-lean-projects | 36eb42c4baccc37183635c36f8e1b3afa4ec1230 | 02466225aa629ab1232043bcc0a053a099fdb939 | refs/heads/master | 1,688,791,717,534 | 1,597,874,092,000 | 1,597,874,092,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,999 | lean | import tactic
open tactic
/-!
This file contains three tactic-programming exercises of increasing difficulty.
They were (hastily) written to follow the metaprogramming tutorial at
Lean for the Curious Mathematician 2020.
If you're looking for more (better) exercises, we strongly recommend the
exercises by Blanchette et al
for the course Logical Verification at the Vrije Universiteit Amsterdam,
and the corresponding chapter of the course notes:
https://github.com/blanchette/logical_verification_2020/blob/master/lean/love07_metaprogramming_exercise_sheet.lean
https://github.com/blanchette/logical_verification_2020/raw/master/hitchhikers_guide.pdf
## Exercise 1
Write a `contradiction` tactic.
The tactic should look through the hypotheses in the local context
trying to find two that contradict each other,
i.e. proving `P` and `¬ P` for some proposition `P`.
It should use this contradiction to close the goal.
Bonus: handle `P → false` as well as `¬ P`.
This exercise is to practice manipulating the hypotheses and goal.
Note: this exists as `tactic.interactive.contradiction`.
-/
meta def tactic.interactive.contr : tactic unit := sorry
-- it only costs a factor of two to check all pairs, let's start with that
-- mmap reduces to the case of having one expr and checking for a contradiction
-- to start, can we just trace the pair h, p for all p in the context
example (P Q R : Prop) (hp : P) (hq : Q) (hr : ¬ R) (hnq : ¬ Q) : false :=
by contr
example (P Q R : Prop) (hnq : ¬ Q) (hp : P) (hq : Q) (hr : ¬ R) : 0 = 1 :=
by contr
example (P Q R : Prop) (hp : P) (hq : Q) (hr : ¬ R) (hnq : Q → false) : false :=
by contr
/-!
## Exercise 2
Write a tactic that proves a given `nat`-valued declaration is nonnegative.
The tactic should take the name of a declaration whose return type is `ℕ`
(presumably with some arguments), e.g. `nat.add : ℕ → ℕ → ℕ`
or `list.length : Π α : Type, list α → ℕ`.
It should add a new declaration to the environment which proves all applications
of this function are nonnegative,
e.g. `nat.add_nonneg : ∀ m n : ℕ, 0 ≤ nat.add m n`.
Bonus: create reasonable names for these declarations, and/or take an optional argument
for the new name.
This tactic is not useful by itself, but it's a good way to practice
querying and modifying an environment and working under binders.
It is not a tactic to be used during a proof, but rather as a command.
Hints:
* For looking at declarations in the environment, you will need the `declaration` type,
as well as the tactics `get_decl` and `add_decl`.
* You will have to manipulate an expression under binders.
The tactics `mk_local_pis` and `pis`, or their lambda equivalents, will be helpful here.
* `mk_mapp` is a variant of `mk_app` that lets you provide implicit arguments.
-/
meta def add_nonneg_proof (n : name) : tactic unit := sorry
run_cmd add_nonneg_proof `nat.add
run_cmd add_nonneg_proof `list.length
#check nat.add_nonneg
#check list.length_nonneg
/-!
## Exercise 3 (challenge!)
The mathlib tactic `cancel_denoms` is intended to get rid of division by numerals
in expressions where this makes sense. For example,
-/
example (q : ℚ) (h : q / 3 > 0) : q > 0 :=
begin
cancel_denoms at h, exact h
end
/-!
But it is not complete. In particular, it doesn't like nested division
or other operators in denominators. These all fail:
-/
example (q : ℚ) (h : q / (3 / 4) > 0) : false :=
begin
cancel_denoms at h,
end
example (p q : ℚ) (h : q / 2 / 3 < q) : false :=
begin
cancel_denoms at h,
end
example (p q : ℚ) (h : q / 2 < 3 / (4*q)) : false :=
begin
cancel_denoms at h,
end
-- this one succeeds but doesn't do what it should
example (p q : ℚ) (h : q / (2*3) < q) : false :=
begin
cancel_denoms at h,
end
/-!
Look at the code in `src/tactic/cancel_denoms.lean` and try to fix it.
See if you can solve any or all of these failing test cases.
If you succeed, a pull request to mathlib is strongly encouraged!
-/
|
b554b7117b0244372eb6150ab868136189ef53e1 | 76df16d6c3760cb415f1294caee997cc4736e09b | /lean/src/workspace/load.lean | 15d0c535db040527c156eeb030e661918148e930 | [
"MIT"
] | permissive | uw-unsat/leanette-popl22-artifact | 70409d9cbd8921d794d27b7992bf1d9a4087e9fe | 80fea2519e61b45a283fbf7903acdf6d5528dbe7 | refs/heads/master | 1,681,592,449,670 | 1,637,037,431,000 | 1,637,037,431,000 | 414,331,908 | 6 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 327 | lean |
import ..sym_factory.sym_factory
import ..generation.main
import ..generation.printer
import ..interp.sym.defs
import ..op.sym
open lang.exp
open op.sym
def input_exp : lang.exp op.lang.datum op.lang.op
:= (bool tt)
#eval result.to_str $
(interp.interpS sym_factory.sym_factory 100
input_exp
[] initial_state)
|
44b9c782f46f279175b373f85bb39134a132014f | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/category_theory/monoidal/functor_category.lean | 2800c914e10e9b0964085b086e922d87d698139b | [
"Apache-2.0"
] | permissive | jjgarzella/mathlib | 96a345378c4e0bf26cf604aed84f90329e4896a2 | 395d8716c3ad03747059d482090e2bb97db612c8 | refs/heads/master | 1,686,480,124,379 | 1,625,163,323,000 | 1,625,163,323,000 | 281,190,421 | 2 | 0 | Apache-2.0 | 1,595,268,170,000 | 1,595,268,169,000 | null | UTF-8 | Lean | false | false | 5,585 | lean | /-
Copyright (c) 2020 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import category_theory.monoidal.braided
import category_theory.functor_category
import category_theory.const
/-!
# Monoidal structure on `C ⥤ D` when `D` is monoidal.
When `C` is any category, and `D` is a monoidal category,
there is a natural "pointwise" monoidal structure on `C ⥤ D`.
The initial intended application is tensor product of presheaves.
-/
universes v₁ v₂ u₁ u₂
open category_theory
open category_theory.monoidal_category
namespace category_theory.monoidal
variables {C : Type u₁} [category.{v₁} C]
variables {D : Type u₂} [category.{v₂} D] [monoidal_category.{v₂} D]
namespace functor_category
variables (F G F' G' : C ⥤ D)
/--
(An auxiliary definition for `functor_category_monoidal`.)
Tensor product of functors `C ⥤ D`, when `D` is monoidal.
-/
@[simps]
def tensor_obj : C ⥤ D :=
{ obj := λ X, F.obj X ⊗ G.obj X,
map := λ X Y f, F.map f ⊗ G.map f,
map_id' := λ X, by rw [F.map_id, G.map_id, tensor_id],
map_comp' := λ X Y Z f g, by rw [F.map_comp, G.map_comp, tensor_comp], }
variables {F G F' G'}
variables (α : F ⟶ G) (β : F' ⟶ G')
/--
(An auxiliary definition for `functor_category_monoidal`.)
Tensor product of natural transformations into `D`, when `D` is monoidal.
-/
@[simps]
def tensor_hom : tensor_obj F F' ⟶ tensor_obj G G' :=
{ app := λ X, α.app X ⊗ β.app X,
naturality' :=
λ X Y f, by { dsimp, rw [←tensor_comp, α.naturality, β.naturality, tensor_comp], } }
end functor_category
open category_theory.monoidal.functor_category
/--
When `C` is any category, and `D` is a monoidal category,
the functor category `C ⥤ D` has a natural pointwise monoidal structure,
where `(F ⊗ G).obj X = F.obj X ⊗ G.obj X`.
-/
instance functor_category_monoidal : monoidal_category (C ⥤ D) :=
{ tensor_obj := λ F G, tensor_obj F G,
tensor_hom := λ F G F' G' α β, tensor_hom α β,
tensor_id' := λ F G, by { ext, dsimp, rw [tensor_id], },
tensor_comp' := λ F G H F' G' H' α β γ δ, by { ext, dsimp, rw [tensor_comp], },
tensor_unit := (category_theory.functor.const C).obj (𝟙_ D),
left_unitor := λ F,
nat_iso.of_components (λ X, λ_ (F.obj X)) (λ X Y f, by { dsimp, rw left_unitor_naturality, }),
right_unitor := λ F,
nat_iso.of_components (λ X, ρ_ (F.obj X)) (λ X Y f, by { dsimp, rw right_unitor_naturality, }),
associator := λ F G H,
nat_iso.of_components
(λ X, α_ (F.obj X) (G.obj X) (H.obj X)) (λ X Y f, by { dsimp, rw associator_naturality, }),
left_unitor_naturality' := λ F G α, by { ext X, dsimp, rw left_unitor_naturality, },
right_unitor_naturality' := λ F G α, by { ext X, dsimp, rw right_unitor_naturality, },
associator_naturality' := λ F G H F' G' H' α β γ, by { ext X, dsimp, rw associator_naturality, },
triangle' := λ F G, begin ext X, dsimp, rw triangle, end,
pentagon' := λ F G H K, begin ext X, dsimp, rw pentagon, end, }
@[simp]
lemma tensor_unit_obj {X} : (𝟙_ (C ⥤ D)).obj X = 𝟙_ D := rfl
@[simp]
lemma tensor_unit_map {X Y} {f : X ⟶ Y} : (𝟙_ (C ⥤ D)).map f = 𝟙 (𝟙_ D) := rfl
@[simp]
lemma tensor_obj_obj {F G : C ⥤ D} {X} : (F ⊗ G).obj X = F.obj X ⊗ G.obj X := rfl
@[simp]
lemma tensor_obj_map {F G : C ⥤ D} {X Y} {f : X ⟶ Y} : (F ⊗ G).map f = F.map f ⊗ G.map f := rfl
@[simp]
lemma tensor_hom_app {F G F' G' : C ⥤ D} {α : F ⟶ G} {β : F' ⟶ G'} {X} :
(α ⊗ β).app X = α.app X ⊗ β.app X := rfl
@[simp]
lemma left_unitor_hom_app {F : C ⥤ D} {X} :
((λ_ F).hom : (𝟙_ _) ⊗ F ⟶ F).app X = (λ_ (F.obj X)).hom := rfl
@[simp]
lemma left_unitor_inv_app {F : C ⥤ D} {X} :
((λ_ F).inv : F ⟶ (𝟙_ _) ⊗ F).app X = (λ_ (F.obj X)).inv := rfl
@[simp]
lemma right_unitor_hom_app {F : C ⥤ D} {X} :
((ρ_ F).hom : F ⊗ (𝟙_ _) ⟶ F).app X = (ρ_ (F.obj X)).hom := rfl
@[simp]
lemma right_unitor_inv_app {F : C ⥤ D} {X} :
((ρ_ F).inv : F ⟶ F ⊗ (𝟙_ _)).app X = (ρ_ (F.obj X)).inv := rfl
@[simp]
lemma associator_hom_app {F G H : C ⥤ D} {X} :
((α_ F G H).hom : (F ⊗ G) ⊗ H ⟶ F ⊗ (G ⊗ H)).app X = (α_ (F.obj X) (G.obj X) (H.obj X)).hom :=
rfl
@[simp]
lemma associator_inv_app {F G H : C ⥤ D} {X} :
((α_ F G H).inv : F ⊗ (G ⊗ H) ⟶ (F ⊗ G) ⊗ H).app X = (α_ (F.obj X) (G.obj X) (H.obj X)).inv :=
rfl
section braided_category
open category_theory.braided_category
variables [braided_category.{v₂} D]
/--
When `C` is any category, and `D` is a braided monoidal category,
the natural pointwise monoidal structure on the functor category `C ⥤ D`
is also braided.
-/
instance functor_category_braided : braided_category (C ⥤ D) :=
{ braiding := λ F G, nat_iso.of_components (λ X, β_ _ _) (by tidy),
hexagon_forward' := λ F G H, by { ext X, apply hexagon_forward, },
hexagon_reverse' := λ F G H, by { ext X, apply hexagon_reverse, }, }
example : braided_category (C ⥤ D) := category_theory.monoidal.functor_category_braided
end braided_category
section symmetric_category
open category_theory.symmetric_category
variables [symmetric_category.{v₂} D]
/--
When `C` is any category, and `D` is a symmetric monoidal category,
the natural pointwise monoidal structure on the functor category `C ⥤ D`
is also symmetric.
-/
instance functor_category_symmetric : symmetric_category (C ⥤ D) :=
{ symmetry' := λ F G, by { ext X, apply symmetry, },}
end symmetric_category
end category_theory.monoidal
|
4ceaf1dc75ea0d134e2679aff2c9e3d856db57c5 | 1abd1ed12aa68b375cdef28959f39531c6e95b84 | /src/set_theory/cofinality.lean | 5d98ad6ea9d4957e8e6cfb0be7906daa86e50c45 | [
"Apache-2.0"
] | permissive | jumpy4/mathlib | d3829e75173012833e9f15ac16e481e17596de0f | af36f1a35f279f0e5b3c2a77647c6bf2cfd51a13 | refs/heads/master | 1,693,508,842,818 | 1,636,203,271,000 | 1,636,203,271,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 25,517 | lean | /-
Copyright (c) 2017 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro, Floris van Doorn
-/
import set_theory.cardinal_ordinal
/-!
# Cofinality
This file contains the definition of cofinality of a ordinal number and regular cardinals
## Main Definitions
* `ordinal.cof o` is the cofinality of the ordinal `o`.
If `o` is the order type of the relation `<` on `α`, then `o.cof` is the smallest cardinality of a
subset `s` of α that is *cofinal* in `α`, i.e. `∀ x : α, ∃ y ∈ s, ¬ y < x`.
* `cardinal.is_limit c` means that `c` is a (weak) limit cardinal: `c ≠ 0 ∧ ∀ x < c, succ x < c`.
* `cardinal.is_strong_limit c` means that `c` is a strong limit cardinal:
`c ≠ 0 ∧ ∀ x < c, 2 ^ x < c`.
* `cardinal.is_regular c` means that `c` is a regular cardinal: `ω ≤ c ∧ c.ord.cof = c`.
* `cardinal.is_inaccessible c` means that `c` is strongly inaccessible:
`ω < c ∧ is_regular c ∧ is_strong_limit c`.
## Main Statements
* `ordinal.infinite_pigeonhole_card`: the infinite pigeonhole principle
* `cardinal.lt_power_cof`: A consequence of König's theorem stating that `c < c ^ c.ord.cof` for
`c ≥ ω`
* `cardinal.univ_inaccessible`: The type of ordinals in `Type u` form an inaccessible cardinal
(in `Type v` with `v > u`). This shows (externally) that in `Type u` there are at least `u`
inaccessible cardinals.
## Implementation Notes
* The cofinality is defined for ordinals.
If `c` is a cardinal number, its cofinality is `c.ord.cof`.
## Tags
cofinality, regular cardinals, limits cardinals, inaccessible cardinals,
infinite pigeonhole principle
-/
noncomputable theory
open function cardinal set
open_locale classical cardinal
universes u v w
variables {α : Type*} {r : α → α → Prop}
namespace order
/-- Cofinality of a reflexive order `≼`. This is the smallest cardinality
of a subset `S : set α` such that `∀ a, ∃ b ∈ S, a ≼ b`. -/
def cof (r : α → α → Prop) [is_refl α r] : cardinal :=
@cardinal.min {S : set α // ∀ a, ∃ b ∈ S, r a b}
⟨⟨set.univ, λ a, ⟨a, ⟨⟩, refl _⟩⟩⟩
(λ S, #S)
lemma cof_le (r : α → α → Prop) [is_refl α r] {S : set α} (h : ∀a, ∃(b ∈ S), r a b) :
order.cof r ≤ #S :=
le_trans (cardinal.min_le _ ⟨S, h⟩) (le_refl _)
lemma le_cof {r : α → α → Prop} [is_refl α r] (c : cardinal) :
c ≤ order.cof r ↔ ∀ {S : set α} (h : ∀a, ∃(b ∈ S), r a b) , c ≤ #S :=
by { rw [order.cof, cardinal.le_min], exact ⟨λ H S h, H ⟨S, h⟩, λ H ⟨S, h⟩, H h ⟩ }
end order
theorem rel_iso.cof.aux {α : Type u} {β : Type v} {r s}
[is_refl α r] [is_refl β s] (f : r ≃r s) :
cardinal.lift.{(max u v)} (order.cof r) ≤
cardinal.lift.{(max u v)} (order.cof s) :=
begin
rw [order.cof, order.cof, lift_min, lift_min, cardinal.le_min],
intro S, cases S with S H, simp only [comp, coe_sort_coe_base, subtype.coe_mk],
refine le_trans (min_le _ _) _,
{ exact ⟨f ⁻¹' S, λ a,
let ⟨b, bS, h⟩ := H (f a) in ⟨f.symm b, by simp [bS, ← f.map_rel_iff, h,
-coe_fn_coe_base, -coe_fn_coe_trans, principal_seg.coe_coe_fn', initial_seg.coe_coe_fn]⟩⟩ },
{ exact lift_mk_le.{u v (max u v)}.2
⟨⟨λ ⟨x, h⟩, ⟨f x, h⟩, λ ⟨x, h₁⟩ ⟨y, h₂⟩ h₃,
by congr; injection h₃ with h'; exact f.to_equiv.injective h'⟩⟩ }
end
theorem rel_iso.cof {α : Type u} {β : Type v} {r s}
[is_refl α r] [is_refl β s] (f : r ≃r s) :
cardinal.lift.{(max u v)} (order.cof r) =
cardinal.lift.{(max u v)} (order.cof s) :=
le_antisymm (rel_iso.cof.aux f) (rel_iso.cof.aux f.symm)
def strict_order.cof (r : α → α → Prop) [h : is_irrefl α r] : cardinal :=
@order.cof α (λ x y, ¬ r y x) ⟨h.1⟩
namespace ordinal
/-- Cofinality of an ordinal. This is the smallest cardinal of a
subset `S` of the ordinal which is unbounded, in the sense
`∀ a, ∃ b ∈ S, ¬(b > a)`. It is defined for all ordinals, but
`cof 0 = 0` and `cof (succ o) = 1`, so it is only really
interesting on limit ordinals (when it is an infinite cardinal). -/
def cof (o : ordinal.{u}) : cardinal.{u} :=
quot.lift_on o (λ ⟨α, r, _⟩, by exactI strict_order.cof r)
begin
rintros ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨⟨f, hf⟩⟩,
rw ← cardinal.lift_inj,
apply rel_iso.cof ⟨f, _⟩,
simp [hf]
end
lemma cof_type (r : α → α → Prop) [is_well_order α r] : (type r).cof = strict_order.cof r := rfl
theorem le_cof_type [is_well_order α r] {c} : c ≤ cof (type r) ↔
∀ S : set α, (∀ a, ∃ b ∈ S, ¬ r b a) → c ≤ #S :=
by dsimp [cof, strict_order.cof, order.cof, type, quotient.mk, quot.lift_on];
rw [cardinal.le_min, subtype.forall]; refl
theorem cof_type_le [is_well_order α r] (S : set α) (h : ∀ a, ∃ b ∈ S, ¬ r b a) :
cof (type r) ≤ #S :=
le_cof_type.1 (le_refl _) S h
theorem lt_cof_type [is_well_order α r] (S : set α) (hl : #S < cof (type r)) :
∃ a, ∀ b ∈ S, r b a :=
not_forall_not.1 $ λ h, not_le_of_lt hl $ cof_type_le S (λ a, not_ball.1 (h a))
theorem cof_eq (r : α → α → Prop) [is_well_order α r] :
∃ S : set α, (∀ a, ∃ b ∈ S, ¬ r b a) ∧ #S = cof (type r) :=
begin
have : ∃ i, cof (type r) = _,
{ dsimp [cof, order.cof, type, quotient.mk, quot.lift_on],
apply cardinal.min_eq },
exact let ⟨⟨S, hl⟩, e⟩ := this in ⟨S, hl, e.symm⟩,
end
theorem ord_cof_eq (r : α → α → Prop) [is_well_order α r] :
∃ S : set α, (∀ a, ∃ b ∈ S, ¬ r b a) ∧ type (subrel r S) = (cof (type r)).ord :=
let ⟨S, hS, e⟩ := cof_eq r, ⟨s, _, e'⟩ := cardinal.ord_eq S,
T : set α := {a | ∃ aS : a ∈ S, ∀ b : S, s b ⟨_, aS⟩ → r b a} in
begin
resetI, suffices,
{ refine ⟨T, this,
le_antisymm _ (cardinal.ord_le.2 $ cof_type_le T this)⟩,
rw [← e, e'],
refine type_le'.2 ⟨rel_embedding.of_monotone
(λ a, ⟨a, let ⟨aS, _⟩ := a.2 in aS⟩) (λ a b h, _)⟩,
rcases a with ⟨a, aS, ha⟩, rcases b with ⟨b, bS, hb⟩,
change s ⟨a, _⟩ ⟨b, _⟩,
refine ((trichotomous_of s _ _).resolve_left (λ hn, _)).resolve_left _,
{ exact asymm h (ha _ hn) },
{ intro e, injection e with e, subst b,
exact irrefl _ h } },
{ intro a,
have : {b : S | ¬ r b a}.nonempty := let ⟨b, bS, ba⟩ := hS a in ⟨⟨b, bS⟩, ba⟩,
let b := (is_well_order.wf).min _ this,
have ba : ¬r b a := (is_well_order.wf).min_mem _ this,
refine ⟨b, ⟨b.2, λ c, not_imp_not.1 $ λ h, _⟩, ba⟩,
rw [show ∀b:S, (⟨b, b.2⟩:S) = b, by intro b; cases b; refl],
exact (is_well_order.wf).not_lt_min _ this
(is_order_connected.neg_trans h ba) }
end
theorem lift_cof (o) : (cof o).lift = cof o.lift :=
induction_on o $ begin introsI α r _,
cases lift_type r with _ e, rw e,
apply le_antisymm,
{ unfreezingI { refine le_cof_type.2 (λ S H, _) },
have : (#(ulift.up ⁻¹' S)).lift ≤ #S :=
⟨⟨λ ⟨⟨x, h⟩⟩, ⟨⟨x⟩, h⟩,
λ ⟨⟨x, h₁⟩⟩ ⟨⟨y, h₂⟩⟩ e, by simp at e; congr; injection e⟩⟩,
refine le_trans (cardinal.lift_le.2 $ cof_type_le _ _) this,
exact λ a, let ⟨⟨b⟩, bs, br⟩ := H ⟨a⟩ in ⟨b, bs, br⟩ },
{ rcases cof_eq r with ⟨S, H, e'⟩,
have : #(ulift.down ⁻¹' S) ≤ (#S).lift :=
⟨⟨λ ⟨⟨x⟩, h⟩, ⟨⟨x, h⟩⟩,
λ ⟨⟨x⟩, h₁⟩ ⟨⟨y⟩, h₂⟩ e, by simp at e; congr; injections⟩⟩,
rw e' at this,
unfreezingI { refine le_trans (cof_type_le _ _) this },
exact λ ⟨a⟩, let ⟨b, bs, br⟩ := H a in ⟨⟨b⟩, bs, br⟩ }
end
theorem cof_le_card (o) : cof o ≤ card o :=
induction_on o $ λ α r _, begin
resetI,
have : #(@set.univ α) = card (type r) :=
quotient.sound ⟨equiv.set.univ _⟩,
rw ← this, exact cof_type_le set.univ (λ a, ⟨a, ⟨⟩, irrefl a⟩)
end
theorem cof_ord_le (c : cardinal) : cof c.ord ≤ c :=
by simpa using cof_le_card c.ord
@[simp] theorem cof_zero : cof 0 = 0 :=
le_antisymm (by simpa using cof_le_card 0) (cardinal.zero_le _)
@[simp] theorem cof_eq_zero {o} : cof o = 0 ↔ o = 0 :=
⟨induction_on o $ λ α r _ z, by exactI
let ⟨S, hl, e⟩ := cof_eq r in type_eq_zero_iff_is_empty.2 $
⟨λ a, let ⟨b, h, _⟩ := hl a in
(mk_eq_zero_iff.1 (e.trans z)).elim' ⟨_, h⟩⟩,
λ e, by simp [e]⟩
@[simp] theorem cof_succ (o) : cof (succ o) = 1 :=
begin
apply le_antisymm,
{ refine induction_on o (λ α r _, _),
change cof (type _) ≤ _,
rw [← (_ : #_ = 1)], apply cof_type_le,
{ refine λ a, ⟨sum.inr punit.star, set.mem_singleton _, _⟩,
rcases a with a|⟨⟨⟨⟩⟩⟩; simp [empty_relation] },
{ rw [cardinal.fintype_card, set.card_singleton], simp } },
{ rw [← cardinal.succ_zero, cardinal.succ_le],
simpa [lt_iff_le_and_ne, cardinal.zero_le] using
λ h, succ_ne_zero o (cof_eq_zero.1 (eq.symm h)) }
end
@[simp] theorem cof_eq_one_iff_is_succ {o} : cof.{u} o = 1 ↔ ∃ a, o = succ a :=
⟨induction_on o $ λ α r _ z, begin
resetI,
rcases cof_eq r with ⟨S, hl, e⟩, rw z at e,
cases mk_ne_zero_iff.1 (by rw e; exact one_ne_zero) with a,
refine ⟨typein r a, eq.symm $ quotient.sound
⟨rel_iso.of_surjective (rel_embedding.of_monotone _
(λ x y, _)) (λ x, _)⟩⟩,
{ apply sum.rec; [exact subtype.val, exact λ _, a] },
{ rcases x with x|⟨⟨⟨⟩⟩⟩; rcases y with y|⟨⟨⟨⟩⟩⟩;
simp [subrel, order.preimage, empty_relation],
exact x.2 },
{ suffices : r x a ∨ ∃ (b : punit), ↑a = x, {simpa},
rcases trichotomous_of r x a with h|h|h,
{ exact or.inl h },
{ exact or.inr ⟨punit.star, h.symm⟩ },
{ rcases hl x with ⟨a', aS, hn⟩,
rw (_ : ↑a = a') at h, {exact absurd h hn},
refine congr_arg subtype.val (_ : a = ⟨a', aS⟩),
haveI := le_one_iff_subsingleton.1 (le_of_eq e),
apply subsingleton.elim } }
end, λ ⟨a, e⟩, by simp [e]⟩
@[simp] theorem cof_add (a b : ordinal) : b ≠ 0 → cof (a + b) = cof b :=
induction_on a $ λ α r _, induction_on b $ λ β s _ b0, begin
resetI,
change cof (type _) = _,
refine eq_of_forall_le_iff (λ c, _),
rw [le_cof_type, le_cof_type],
split; intros H S hS,
{ refine le_trans (H {a | sum.rec_on a (∅:set α) S} (λ a, _)) ⟨⟨_, _⟩⟩,
{ cases a with a b,
{ cases type_ne_zero_iff_nonempty.1 b0 with b,
rcases hS b with ⟨b', bs, _⟩,
exact ⟨sum.inr b', bs, by simp⟩ },
{ rcases hS b with ⟨b', bs, h⟩,
exact ⟨sum.inr b', bs, by simp [h]⟩ } },
{ exact λ a, match a with ⟨sum.inr b, h⟩ := ⟨b, h⟩ end },
{ exact λ a b, match a, b with
⟨sum.inr a, h₁⟩, ⟨sum.inr b, h₂⟩, h := by congr; injection h
end } },
{ refine le_trans (H (sum.inr ⁻¹' S) (λ a, _)) ⟨⟨_, _⟩⟩,
{ rcases hS (sum.inr a) with ⟨a'|b', bs, h⟩; simp at h,
{ cases h }, { exact ⟨b', bs, h⟩ } },
{ exact λ ⟨a, h⟩, ⟨_, h⟩ },
{ exact λ ⟨a, h₁⟩ ⟨b, h₂⟩ h,
by injection h with h; congr; injection h } }
end
@[simp] theorem cof_cof (o : ordinal) : cof (cof o).ord= cof o :=
le_antisymm (le_trans (cof_le_card _) (by simp)) $
induction_on o $ λ α r _, by exactI
let ⟨S, hS, e₁⟩ := ord_cof_eq r,
⟨T, hT, e₂⟩ := cof_eq (subrel r S) in begin
rw e₁ at e₂, rw ← e₂,
refine le_trans (cof_type_le {a | ∃ h, (subtype.mk a h : S) ∈ T} (λ a, _)) ⟨⟨_, _⟩⟩,
{ rcases hS a with ⟨b, bS, br⟩,
rcases hT ⟨b, bS⟩ with ⟨⟨c, cS⟩, cT, cs⟩,
exact ⟨c, ⟨cS, cT⟩, is_order_connected.neg_trans cs br⟩ },
{ exact λ ⟨a, h⟩, ⟨⟨a, h.fst⟩, h.snd⟩ },
{ exact λ ⟨a, ha⟩ ⟨b, hb⟩ h,
by injection h with h; congr; injection h },
end
theorem omega_le_cof {o} : ω ≤ cof o ↔ is_limit o :=
begin
rcases zero_or_succ_or_limit o with rfl|⟨o,rfl⟩|l,
{ simp [not_zero_is_limit, cardinal.omega_ne_zero] },
{ simp [not_succ_is_limit, cardinal.one_lt_omega] },
{ simp [l], refine le_of_not_lt (λ h, _),
cases cardinal.lt_omega.1 h with n e,
have := cof_cof o,
rw [e, ord_nat] at this,
cases n,
{ simp at e, simpa [e, not_zero_is_limit] using l },
{ rw [← nat_cast_succ, cof_succ] at this,
rw [← this, cof_eq_one_iff_is_succ] at e,
rcases e with ⟨a, rfl⟩,
exact not_succ_is_limit _ l } }
end
@[simp] theorem cof_omega : cof omega = ω :=
le_antisymm
(by rw ← card_omega; apply cof_le_card)
(omega_le_cof.2 omega_is_limit)
theorem cof_eq' (r : α → α → Prop) [is_well_order α r] (h : is_limit (type r)) :
∃ S : set α, (∀ a, ∃ b ∈ S, r a b) ∧ #S = cof (type r) :=
let ⟨S, H, e⟩ := cof_eq r in
⟨S, λ a,
let a' := enum r _ (h.2 _ (typein_lt_type r a)) in
let ⟨b, h, ab⟩ := H a' in
⟨b, h, (is_order_connected.conn a b a' $ (typein_lt_typein r).1
(by rw typein_enum; apply ordinal.lt_succ_self)).resolve_right ab⟩,
e⟩
theorem cof_sup_le_lift {ι} (f : ι → ordinal) (H : ∀ i, f i < sup f) :
cof (sup f) ≤ (#ι).lift :=
begin
generalize e : sup f = o,
refine ordinal.induction_on o _ e, introsI α r _ e',
rw e' at H,
refine le_trans (cof_type_le (set.range (λ i, enum r _ (H i))) _)
⟨embedding.of_surjective _ _⟩,
{ intro a, by_contra h,
apply not_le_of_lt (typein_lt_type r a),
rw [← e', sup_le],
intro i,
have h : ∀ (x : ι), r (enum r (f x) _) a, { simpa using h },
simpa only [typein_enum] using le_of_lt ((typein_lt_typein r).2 (h i)) },
{ exact λ i, ⟨_, set.mem_range_self i.1⟩ },
{ intro a, rcases a with ⟨_, i, rfl⟩, exact ⟨⟨i⟩, by simp⟩ }
end
theorem cof_sup_le {ι} (f : ι → ordinal) (H : ∀ i, f i < sup.{u u} f) :
cof (sup.{u u} f) ≤ #ι :=
by simpa using cof_sup_le_lift.{u u} f H
theorem cof_bsup_le_lift {o : ordinal} : ∀ (f : Π a < o, ordinal), (∀ i h, f i h < bsup o f) →
cof (bsup o f) ≤ o.card.lift :=
induction_on o $ λ α r _ f H,
by rw bsup_type; refine cof_sup_le_lift _ _;
rw ← bsup_type; intro a; apply H
theorem cof_bsup_le {o : ordinal} : ∀ (f : Π a < o, ordinal), (∀ i h, f i h < bsup.{u u} o f) →
cof (bsup.{u u} o f) ≤ o.card :=
induction_on o $ λ α r _ f H,
by simpa using cof_bsup_le_lift.{u u} f H
@[simp] theorem cof_univ : cof univ.{u v} = cardinal.univ :=
le_antisymm (cof_le_card _) begin
refine le_of_forall_lt (λ c h, _),
rcases lt_univ'.1 h with ⟨c, rfl⟩,
rcases @cof_eq ordinal.{u} (<) _ with ⟨S, H, Se⟩,
rw [univ, ← lift_cof, ← cardinal.lift_lift, cardinal.lift_lt, ← Se],
refine lt_of_not_ge (λ h, _),
cases cardinal.lift_down h with a e,
refine quotient.induction_on a (λ α e, _) e,
cases quotient.exact e with f,
have f := equiv.ulift.symm.trans f,
let g := λ a, (f a).1,
let o := succ (sup.{u u} g),
rcases H o with ⟨b, h, l⟩,
refine l (lt_succ.2 _),
rw ← show g (f.symm ⟨b, h⟩) = b, by dsimp [g]; simp,
apply le_sup
end
theorem sup_lt_ord {ι} (f : ι → ordinal) {c : ordinal} (H1 : #ι < c.cof)
(H2 : ∀ i, f i < c) : sup.{u u} f < c :=
begin
apply lt_of_le_of_ne,
{ rw [sup_le], exact λ i, le_of_lt (H2 i) },
rintro h, apply not_le_of_lt H1,
simpa [sup_ord, H2, h] using cof_sup_le.{u} f
end
theorem sup_lt {ι} (f : ι → cardinal) {c : cardinal} (H1 : #ι < c.ord.cof)
(H2 : ∀ i, f i < c) : cardinal.sup.{u u} f < c :=
by { rw [←ord_lt_ord, ←sup_ord], apply sup_lt_ord _ H1, intro i, rw ord_lt_ord, apply H2 }
/-- If the union of s is unbounded and s is smaller than the cofinality,
then s has an unbounded member -/
theorem unbounded_of_unbounded_sUnion (r : α → α → Prop) [wo : is_well_order α r] {s : set (set α)}
(h₁ : unbounded r $ ⋃₀ s) (h₂ : #s < strict_order.cof r) : ∃(x ∈ s), unbounded r x :=
begin
by_contra h, simp only [not_exists, exists_prop, not_and, not_unbounded_iff] at h,
apply not_le_of_lt h₂,
let f : s → α := λ x : s, wo.wf.sup x (h x.1 x.2),
let t : set α := range f,
have : #t ≤ #s, exact mk_range_le, refine le_trans _ this,
have : unbounded r t,
{ intro x, rcases h₁ x with ⟨y, ⟨c, hc, hy⟩, hxy⟩,
refine ⟨f ⟨c, hc⟩, mem_range_self _, _⟩, intro hxz, apply hxy,
refine trans (wo.wf.lt_sup _ hy) hxz },
exact cardinal.min_le _ (subtype.mk t this)
end
/-- If the union of s is unbounded and s is smaller than the cofinality,
then s has an unbounded member -/
theorem unbounded_of_unbounded_Union {α β : Type u} (r : α → α → Prop) [wo : is_well_order α r]
(s : β → set α)
(h₁ : unbounded r $ ⋃x, s x) (h₂ : #β < strict_order.cof r) : ∃x : β, unbounded r (s x) :=
begin
rw [← sUnion_range] at h₁,
have : #(range (λ (i : β), s i)) < strict_order.cof r := lt_of_le_of_lt mk_range_le h₂,
rcases unbounded_of_unbounded_sUnion r h₁ this with ⟨_, ⟨x, rfl⟩, u⟩, exact ⟨x, u⟩
end
/-- The infinite pigeonhole principle -/
theorem infinite_pigeonhole {β α : Type u} (f : β → α) (h₁ : ω ≤ #β)
(h₂ : #α < (#β).ord.cof) : ∃a : α, #(f ⁻¹' {a}) = #β :=
begin
have : ¬∀a, #(f ⁻¹' {a}) < #β,
{ intro h,
apply not_lt_of_ge (ge_of_eq $ mk_univ),
rw [←@preimage_univ _ _ f, ←Union_of_singleton, preimage_Union],
apply lt_of_le_of_lt mk_Union_le_sum_mk,
apply lt_of_le_of_lt (sum_le_sup _),
apply mul_lt_of_lt h₁ (lt_of_lt_of_le h₂ $ cof_ord_le _),
exact sup_lt _ h₂ h },
rw [not_forall] at this, cases this with x h,
use x, apply le_antisymm _ (le_of_not_gt h),
rw [le_mk_iff_exists_set], exact ⟨_, rfl⟩
end
/-- pigeonhole principle for a cardinality below the cardinality of the domain -/
theorem infinite_pigeonhole_card {β α : Type u} (f : β → α) (θ : cardinal) (hθ : θ ≤ #β)
(h₁ : ω ≤ θ) (h₂ : #α < θ.ord.cof) : ∃a : α, θ ≤ #(f ⁻¹' {a}) :=
begin
rcases le_mk_iff_exists_set.1 hθ with ⟨s, rfl⟩,
cases infinite_pigeonhole (f ∘ subtype.val : s → α) h₁ h₂ with a ha,
use a, rw [←ha, @preimage_comp _ _ _ subtype.val f],
apply mk_preimage_of_injective _ _ subtype.val_injective
end
theorem infinite_pigeonhole_set {β α : Type u} {s : set β} (f : s → α) (θ : cardinal)
(hθ : θ ≤ #s) (h₁ : ω ≤ θ) (h₂ : #α < θ.ord.cof) :
∃(a : α) (t : set β) (h : t ⊆ s), θ ≤ #t ∧ ∀{{x}} (hx : x ∈ t), f ⟨x, h hx⟩ = a :=
begin
cases infinite_pigeonhole_card f θ hθ h₁ h₂ with a ha,
refine ⟨a, {x | ∃(h : x ∈ s), f ⟨x, h⟩ = a}, _, _, _⟩,
{ rintro x ⟨hx, hx'⟩, exact hx },
{ refine le_trans ha _, apply ge_of_eq, apply quotient.sound, constructor,
refine equiv.trans _ (equiv.subtype_subtype_equiv_subtype_exists _ _).symm,
simp only [set_coe_eq_subtype, mem_singleton_iff, mem_preimage, mem_set_of_eq] },
rintro x ⟨hx, hx'⟩, exact hx'
end
end ordinal
namespace cardinal
open ordinal
local infixr ^ := @pow cardinal.{u} cardinal cardinal.has_pow
/-- A cardinal is a limit if it is not zero or a successor
cardinal. Note that `ω` is a limit cardinal by this definition. -/
def is_limit (c : cardinal) : Prop :=
c ≠ 0 ∧ ∀ x < c, succ x < c
/-- A cardinal is a strong limit if it is not zero and it is
closed under powersets. Note that `ω` is a strong limit by this definition. -/
def is_strong_limit (c : cardinal) : Prop :=
c ≠ 0 ∧ ∀ x < c, 2 ^ x < c
theorem is_strong_limit.is_limit {c} (H : is_strong_limit c) : is_limit c :=
⟨H.1, λ x h, lt_of_le_of_lt (succ_le.2 $ cantor _) (H.2 _ h)⟩
/-- A cardinal is regular if it is infinite and it equals its own cofinality. -/
def is_regular (c : cardinal) : Prop :=
ω ≤ c ∧ c.ord.cof = c
lemma is_regular.pos {c : cardinal} (H : c.is_regular) : 0 < c :=
omega_pos.trans_le H.left
lemma is_regular.ord_pos {c : cardinal} (H : c.is_regular) : 0 < c.ord :=
by { rw cardinal.lt_ord, exact H.pos }
theorem cof_is_regular {o : ordinal} (h : o.is_limit) : is_regular o.cof :=
⟨omega_le_cof.2 h, cof_cof _⟩
theorem omega_is_regular : is_regular ω :=
⟨le_refl _, by simp⟩
theorem succ_is_regular {c : cardinal.{u}} (h : ω ≤ c) : is_regular (succ c) :=
⟨le_trans h (le_of_lt $ lt_succ_self _), begin
refine le_antisymm (cof_ord_le _) (succ_le.2 _),
cases quotient.exists_rep (succ c) with α αe, simp at αe,
rcases ord_eq α with ⟨r, wo, re⟩, resetI,
have := ord_is_limit (le_trans h $ le_of_lt $ lt_succ_self _),
rw [← αe, re] at this ⊢,
rcases cof_eq' r this with ⟨S, H, Se⟩,
rw [← Se],
apply lt_imp_lt_of_le_imp_le
(λ (h : #S ≤ c), mul_le_mul_right' h c),
rw [mul_eq_self h, ← succ_le, ← αe, ← sum_const'],
refine le_trans _ (sum_le_sum (λ x:S, card (typein r x)) _ _),
{ simp only [← card_typein, ← mk_sigma],
refine ⟨embedding.of_surjective _ _⟩,
{ exact λ x, x.2.1 },
{ exact λ a, let ⟨b, h, ab⟩ := H a in ⟨⟨⟨_, h⟩, _, ab⟩, rfl⟩ } },
{ intro i,
rw [← lt_succ, ← lt_ord, ← αe, re],
apply typein_lt_type }
end⟩
/--
A function whose codomain's cardinality is infinite but strictly smaller than its domain's
has a fiber with cardinality strictly great than the codomain.
-/
theorem infinite_pigeonhole_card_lt {β α : Type u} (f : β → α)
(w : #α < #β) (w' : ω ≤ #α) :
∃ a : α, #α < #(f ⁻¹' {a}) :=
begin
simp_rw [← succ_le],
exact ordinal.infinite_pigeonhole_card f (#α).succ (succ_le.mpr w)
(w'.trans (lt_succ_self _).le)
((lt_succ_self _).trans_le (succ_is_regular w').2.ge),
end
/--
A function whose codomain's cardinality is infinite but strictly smaller than its domain's
has an infinite fiber.
-/
theorem exists_infinite_fiber {β α : Type*} (f : β → α)
(w : #α < #β) (w' : _root_.infinite α) :
∃ a : α, _root_.infinite (f ⁻¹' {a}) :=
begin
simp_rw [cardinal.infinite_iff] at ⊢ w',
cases infinite_pigeonhole_card_lt f w w' with a ha,
exact ⟨a, w'.trans ha.le⟩,
end
/--
If an infinite type `β` can be expressed as a union of finite sets,
then the cardinality of the collection of those finite sets
must be at least the cardinality of `β`.
-/
lemma le_range_of_union_finset_eq_top
{α β : Type*} [infinite β] (f : α → finset β) (w : (⋃ a, (f a : set β)) = ⊤) :
#β ≤ #(range f) :=
begin
have k : _root_.infinite (range f),
{ rw infinite_coe_iff,
apply mt (union_finset_finite_of_range_finite f),
rw w,
exact infinite_univ, },
by_contradiction h,
simp only [not_le] at h,
let u : Π b, ∃ a, b ∈ f a := λ b, by simpa using (w.ge : _) (set.mem_univ b),
let u' : β → range f := λ b, ⟨f (u b).some, by simp⟩,
have v' : ∀ a, u' ⁻¹' {⟨f a, by simp⟩} ≤ f a, begin rintros a p m,
simp at m,
rw ←m,
apply (λ b, (u b).some_spec),
end,
obtain ⟨⟨-, ⟨a, rfl⟩⟩, p⟩ := exists_infinite_fiber u' h k,
exact (@infinite.of_injective _ _ p (inclusion (v' a)) (inclusion_injective _)).false,
end
theorem sup_lt_ord_of_is_regular {ι} (f : ι → ordinal)
{c} (hc : is_regular c) (H1 : #ι < c)
(H2 : ∀ i, f i < c.ord) : ordinal.sup.{u u} f < c.ord :=
by { apply sup_lt_ord _ _ H2, rw [hc.2], exact H1 }
theorem sup_lt_of_is_regular {ι} (f : ι → cardinal)
{c} (hc : is_regular c) (H1 : #ι < c)
(H2 : ∀ i, f i < c) : sup.{u u} f < c :=
by { apply sup_lt _ _ H2, rwa [hc.2] }
theorem sum_lt_of_is_regular {ι} (f : ι → cardinal)
{c} (hc : is_regular c) (H1 : #ι < c)
(H2 : ∀ i, f i < c) : sum.{u u} f < c :=
lt_of_le_of_lt (sum_le_sup _) $ mul_lt_of_lt hc.1 H1 $
sup_lt_of_is_regular f hc H1 H2
/-- A cardinal is inaccessible if it is an uncountable regular strong limit cardinal. -/
def is_inaccessible (c : cardinal) :=
ω < c ∧ is_regular c ∧ is_strong_limit c
theorem is_inaccessible.mk {c}
(h₁ : ω < c) (h₂ : c ≤ c.ord.cof) (h₃ : ∀ x < c, 2 ^ x < c) :
is_inaccessible c :=
⟨h₁, ⟨le_of_lt h₁, le_antisymm (cof_ord_le _) h₂⟩,
ne_of_gt (lt_trans omega_pos h₁), h₃⟩
/- Lean's foundations prove the existence of ω many inaccessible cardinals -/
theorem univ_inaccessible : is_inaccessible (univ.{u v}) :=
is_inaccessible.mk
(by simpa using lift_lt_univ' ω)
(by simp)
(λ c h, begin
rcases lt_univ'.1 h with ⟨c, rfl⟩,
rw ← lift_two_power.{u (max (u+1) v)},
apply lift_lt_univ'
end)
theorem lt_power_cof {c : cardinal.{u}} : ω ≤ c → c < c ^ cof c.ord :=
quotient.induction_on c $ λ α h, begin
rcases ord_eq α with ⟨r, wo, re⟩, resetI,
have := ord_is_limit h,
rw [mk_def, re] at this ⊢,
rcases cof_eq' r this with ⟨S, H, Se⟩,
have := sum_lt_prod (λ a:S, #{x // r x a}) (λ _, #α) (λ i, _),
{ simp only [cardinal.prod_const, cardinal.lift_id, ← Se, ← mk_sigma, power_def] at this ⊢,
refine lt_of_le_of_lt _ this,
refine ⟨embedding.of_surjective _ _⟩,
{ exact λ x, x.2.1 },
{ exact λ a, let ⟨b, h, ab⟩ := H a in ⟨⟨⟨_, h⟩, _, ab⟩, rfl⟩ } },
{ have := typein_lt_type r i,
rwa [← re, lt_ord] at this }
end
theorem lt_cof_power {a b : cardinal} (ha : ω ≤ a) (b1 : 1 < b) :
a < cof (b ^ a).ord :=
begin
have b0 : b ≠ 0 := ne_of_gt (lt_trans zero_lt_one b1),
apply lt_imp_lt_of_le_imp_le (power_le_power_left $ power_ne_zero a b0),
rw [← power_mul, mul_eq_self ha],
exact lt_power_cof (le_trans ha $ le_of_lt $ cantor' _ b1),
end
end cardinal
|
0d421af5186f7f7c638ac2e4744d53bedd915e28 | c777c32c8e484e195053731103c5e52af26a25d1 | /src/data/set/prod.lean | dc29231b9b051f9a108ad2c7aeaa46d1d67370ae | [
"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 | 26,426 | 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, Patrick Massot
-/
import data.set.image
/-!
# Sets in product and pi types
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
This file defines the product of sets in `α × β` and in `Π i, α i` along with the diagonal of a
type.
## Main declarations
* `set.prod`: Binary product of sets. For `s : set α`, `t : set β`, we have
`s.prod t : set (α × β)`.
* `set.diagonal`: Diagonal of a type. `set.diagonal α = {(x, x) | x : α}`.
* `set.off_diag`: Off-diagonal. `s ×ˢ s` without the diagonal.
* `set.pi`: Arbitrary product of sets.
-/
open function
namespace set
/-! ### Cartesian binary product of sets -/
section prod
variables {α β γ δ : Type*} {s s₁ s₂ : set α} {t t₁ t₂ : set β} {a : α} {b : β}
/-- The cartesian product `prod s t` is the set of `(a, b)` such that `a ∈ s` and `b ∈ t`. -/
def prod (s : set α) (t : set β) : set (α × β) := {p | p.1 ∈ s ∧ p.2 ∈ t}
/- This notation binds more strongly than (pre)images, unions and intersections. -/
infixr (name := set.prod) ` ×ˢ `:82 := set.prod
lemma prod_eq (s : set α) (t : set β) : s ×ˢ t = prod.fst ⁻¹' s ∩ prod.snd ⁻¹' t := rfl
lemma mem_prod_eq {p : α × β} : p ∈ s ×ˢ t = (p.1 ∈ s ∧ p.2 ∈ t) := rfl
@[simp] lemma mem_prod {p : α × β} : p ∈ s ×ˢ t ↔ p.1 ∈ s ∧ p.2 ∈ t := iff.rfl
@[simp] lemma prod_mk_mem_set_prod_eq : (a, b) ∈ s ×ˢ t = (a ∈ s ∧ b ∈ t) := rfl
lemma mk_mem_prod (ha : a ∈ s) (hb : b ∈ t) : (a, b) ∈ s ×ˢ t := ⟨ha, hb⟩
instance decidable_mem_prod [hs : decidable_pred (∈ s)] [ht : decidable_pred (∈ t)] :
decidable_pred (∈ (s ×ˢ t)) :=
λ _, and.decidable
lemma prod_mono (hs : s₁ ⊆ s₂) (ht : t₁ ⊆ t₂) : s₁ ×ˢ t₁ ⊆ s₂ ×ˢ t₂ :=
λ x ⟨h₁, h₂⟩, ⟨hs h₁, ht h₂⟩
lemma prod_mono_left (hs : s₁ ⊆ s₂) : s₁ ×ˢ t ⊆ s₂ ×ˢ t := prod_mono hs subset.rfl
lemma prod_mono_right (ht : t₁ ⊆ t₂) : s ×ˢ t₁ ⊆ s ×ˢ t₂ := prod_mono subset.rfl ht
@[simp] lemma prod_self_subset_prod_self : s₁ ×ˢ s₁ ⊆ s₂ ×ˢ s₂ ↔ s₁ ⊆ s₂ :=
⟨λ h x hx, (h (mk_mem_prod hx hx)).1, λ h x hx, ⟨h hx.1, h hx.2⟩⟩
@[simp] lemma prod_self_ssubset_prod_self : s₁ ×ˢ s₁ ⊂ s₂ ×ˢ s₂ ↔ s₁ ⊂ s₂ :=
and_congr prod_self_subset_prod_self $ not_congr prod_self_subset_prod_self
lemma prod_subset_iff {P : set (α × β)} : s ×ˢ t ⊆ P ↔ ∀ (x ∈ s) (y ∈ t), (x, y) ∈ P :=
⟨λ h _ hx _ hy, h (mk_mem_prod hx hy), λ h ⟨_, _⟩ hp, h _ hp.1 _ hp.2⟩
lemma forall_prod_set {p : α × β → Prop} : (∀ x ∈ s ×ˢ t, p x) ↔ ∀ (x ∈ s) (y ∈ t), p (x, y) :=
prod_subset_iff
lemma exists_prod_set {p : α × β → Prop} : (∃ x ∈ s ×ˢ t, p x) ↔ ∃ (x ∈ s) (y ∈ t), p (x, y) :=
by simp [and_assoc]
@[simp] lemma prod_empty : s ×ˢ (∅ : set β) = ∅ := by { ext, exact and_false _ }
@[simp] lemma empty_prod : (∅ : set α) ×ˢ t = ∅ := by { ext, exact false_and _ }
@[simp] lemma univ_prod_univ : @univ α ×ˢ @univ β = univ := by { ext, exact true_and _ }
lemma univ_prod {t : set β} : (univ : set α) ×ˢ t = prod.snd ⁻¹' t := by simp [prod_eq]
lemma prod_univ {s : set α} : s ×ˢ (univ : set β) = prod.fst ⁻¹' s := by simp [prod_eq]
@[simp] lemma singleton_prod : ({a} : set α) ×ˢ t = prod.mk a '' t :=
by { ext ⟨x, y⟩, simp [and.left_comm, eq_comm] }
@[simp] lemma prod_singleton : s ×ˢ ({b} : set β) = (λ a, (a, b)) '' s :=
by { ext ⟨x, y⟩, simp [and.left_comm, eq_comm] }
lemma singleton_prod_singleton : ({a} : set α) ×ˢ ({b} : set β) = {(a, b)} :=by simp
@[simp] lemma union_prod : (s₁ ∪ s₂) ×ˢ t = s₁ ×ˢ t ∪ s₂ ×ˢ t :=
by { ext ⟨x, y⟩, simp [or_and_distrib_right] }
@[simp] lemma prod_union : s ×ˢ (t₁ ∪ t₂) = s ×ˢ t₁ ∪ s ×ˢ t₂ :=
by { ext ⟨x, y⟩, simp [and_or_distrib_left] }
lemma inter_prod : (s₁ ∩ s₂) ×ˢ t = s₁ ×ˢ t ∩ s₂ ×ˢ t :=
by { ext ⟨x, y⟩, simp only [←and_and_distrib_right, mem_inter_iff, mem_prod] }
lemma prod_inter : s ×ˢ (t₁ ∩ t₂) = s ×ˢ t₁ ∩ s ×ˢ t₂ :=
by { ext ⟨x, y⟩, simp only [←and_and_distrib_left, mem_inter_iff, mem_prod] }
lemma prod_inter_prod : s₁ ×ˢ t₁ ∩ s₂ ×ˢ t₂ = (s₁ ∩ s₂) ×ˢ (t₁ ∩ t₂) :=
by { ext ⟨x, y⟩, simp [and_assoc, and.left_comm] }
lemma disjoint_prod : disjoint (s₁ ×ˢ t₁) (s₂ ×ˢ t₂) ↔ disjoint s₁ s₂ ∨ disjoint t₁ t₂ :=
begin
simp_rw [disjoint_left, mem_prod, not_and_distrib, prod.forall, and_imp,
←@forall_or_distrib_right α, ←@forall_or_distrib_left β,
←@forall_or_distrib_right (_ ∈ s₁), ←@forall_or_distrib_left (_ ∈ t₁)],
end
lemma insert_prod : insert a s ×ˢ t = (prod.mk a '' t) ∪ s ×ˢ t :=
by { ext ⟨x, y⟩, simp [image, iff_def, or_imp_distrib, imp.swap] {contextual := tt} }
lemma prod_insert : s ×ˢ (insert b t) = ((λa, (a, b)) '' s) ∪ s ×ˢ t :=
by { ext ⟨x, y⟩, simp [image, iff_def, or_imp_distrib, imp.swap] {contextual := tt} }
lemma prod_preimage_eq {f : γ → α} {g : δ → β} :
(f ⁻¹' s) ×ˢ (g ⁻¹' t) = (λ p : γ × δ, (f p.1, g p.2)) ⁻¹' s ×ˢ t := rfl
lemma prod_preimage_left {f : γ → α} :
(f ⁻¹' s) ×ˢ t = (λ p : γ × β, (f p.1, p.2)) ⁻¹' s ×ˢ t := rfl
lemma prod_preimage_right {g : δ → β} :
s ×ˢ (g ⁻¹' t) = (λ p : α × δ, (p.1, g p.2)) ⁻¹' s ×ˢ t := rfl
lemma preimage_prod_map_prod (f : α → β) (g : γ → δ) (s : set β) (t : set δ) :
prod.map f g ⁻¹' s ×ˢ t = (f ⁻¹' s) ×ˢ (g ⁻¹' t) :=
rfl
lemma mk_preimage_prod (f : γ → α) (g : γ → β) :
(λ x, (f x, g x)) ⁻¹' s ×ˢ t = f ⁻¹' s ∩ g ⁻¹' t := rfl
@[simp] lemma mk_preimage_prod_left (hb : b ∈ t) : (λ a, (a, b)) ⁻¹' s ×ˢ t = s :=
by { ext a, simp [hb] }
@[simp] lemma mk_preimage_prod_right (ha : a ∈ s) : prod.mk a ⁻¹' s ×ˢ t = t :=
by { ext b, simp [ha] }
@[simp] lemma mk_preimage_prod_left_eq_empty (hb : b ∉ t) : (λ a, (a, b)) ⁻¹' s ×ˢ t = ∅ :=
by { ext a, simp [hb] }
@[simp] lemma mk_preimage_prod_right_eq_empty (ha : a ∉ s) : prod.mk a ⁻¹' s ×ˢ t = ∅ :=
by { ext b, simp [ha] }
lemma mk_preimage_prod_left_eq_if [decidable_pred (∈ t)] :
(λ a, (a, b)) ⁻¹' s ×ˢ t = if b ∈ t then s else ∅ :=
by split_ifs; simp [h]
lemma mk_preimage_prod_right_eq_if [decidable_pred (∈ s)] :
prod.mk a ⁻¹' s ×ˢ t = if a ∈ s then t else ∅ :=
by split_ifs; simp [h]
lemma mk_preimage_prod_left_fn_eq_if [decidable_pred (∈ t)] (f : γ → α) :
(λ a, (f a, b)) ⁻¹' s ×ˢ t = if b ∈ t then f ⁻¹' s else ∅ :=
by rw [← mk_preimage_prod_left_eq_if, prod_preimage_left, preimage_preimage]
lemma mk_preimage_prod_right_fn_eq_if [decidable_pred (∈ s)] (g : δ → β) :
(λ b, (a, g b)) ⁻¹' s ×ˢ t = if a ∈ s then g ⁻¹' t else ∅ :=
by rw [← mk_preimage_prod_right_eq_if, prod_preimage_right, preimage_preimage]
@[simp] lemma preimage_swap_prod (s : set α) (t : set β) : prod.swap ⁻¹' s ×ˢ t = t ×ˢ s :=
by { ext ⟨x, y⟩, simp [and_comm] }
@[simp] lemma image_swap_prod (s : set α) (t : set β) : prod.swap '' s ×ˢ t = t ×ˢ s :=
by rw [image_swap_eq_preimage_swap, preimage_swap_prod]
lemma prod_image_image_eq {m₁ : α → γ} {m₂ : β → δ} :
(m₁ '' s) ×ˢ (m₂ '' t) = (λ p : α × β, (m₁ p.1, m₂ p.2)) '' s ×ˢ t :=
ext $ by simp [-exists_and_distrib_right, exists_and_distrib_right.symm, and.left_comm,
and.assoc, and.comm]
lemma prod_range_range_eq {m₁ : α → γ} {m₂ : β → δ} :
(range m₁) ×ˢ (range m₂) = range (λ p : α × β, (m₁ p.1, m₂ p.2)) :=
ext $ by simp [range]
@[simp] lemma range_prod_map {m₁ : α → γ} {m₂ : β → δ} :
range (prod.map m₁ m₂) = (range m₁) ×ˢ (range m₂) :=
prod_range_range_eq.symm
lemma prod_range_univ_eq {m₁ : α → γ} :
(range m₁) ×ˢ (univ : set β) = range (λ p : α × β, (m₁ p.1, p.2)) :=
ext $ by simp [range]
lemma prod_univ_range_eq {m₂ : β → δ} :
(univ : set α) ×ˢ (range m₂) = range (λ p : α × β, (p.1, m₂ p.2)) :=
ext $ by simp [range]
lemma range_pair_subset (f : α → β) (g : α → γ) :
range (λ x, (f x, g x)) ⊆ (range f) ×ˢ (range g) :=
have (λ x, (f x, g x)) = prod.map f g ∘ (λ x, (x, x)), from funext (λ x, rfl),
by { rw [this, ← range_prod_map], apply range_comp_subset_range }
lemma nonempty.prod : s.nonempty → t.nonempty → (s ×ˢ t).nonempty :=
λ ⟨x, hx⟩ ⟨y, hy⟩, ⟨(x, y), ⟨hx, hy⟩⟩
lemma nonempty.fst : (s ×ˢ t).nonempty → s.nonempty := λ ⟨x, hx⟩, ⟨x.1, hx.1⟩
lemma nonempty.snd : (s ×ˢ t).nonempty → t.nonempty := λ ⟨x, hx⟩, ⟨x.2, hx.2⟩
lemma prod_nonempty_iff : (s ×ˢ t).nonempty ↔ s.nonempty ∧ t.nonempty :=
⟨λ h, ⟨h.fst, h.snd⟩, λ h, h.1.prod h.2⟩
lemma prod_eq_empty_iff : s ×ˢ t = ∅ ↔ s = ∅ ∨ t = ∅ :=
by simp only [not_nonempty_iff_eq_empty.symm, prod_nonempty_iff, not_and_distrib]
lemma prod_sub_preimage_iff {W : set γ} {f : α × β → γ} :
s ×ˢ t ⊆ f ⁻¹' W ↔ ∀ a b, a ∈ s → b ∈ t → f (a, b) ∈ W :=
by simp [subset_def]
lemma image_prod_mk_subset_prod {f : α → β} {g : α → γ} {s : set α} :
(λ x, (f x, g x)) '' s ⊆ (f '' s) ×ˢ (g '' s) :=
by { rintros _ ⟨x, hx, rfl⟩, exact mk_mem_prod (mem_image_of_mem f hx) (mem_image_of_mem g hx) }
lemma image_prod_mk_subset_prod_left (hb : b ∈ t) : (λ a, (a, b)) '' s ⊆ s ×ˢ t :=
by { rintro _ ⟨a, ha, rfl⟩, exact ⟨ha, hb⟩ }
lemma image_prod_mk_subset_prod_right (ha : a ∈ s) : prod.mk a '' t ⊆ s ×ˢ t :=
by { rintro _ ⟨b, hb, rfl⟩, exact ⟨ha, hb⟩ }
lemma prod_subset_preimage_fst (s : set α) (t : set β) : s ×ˢ t ⊆ prod.fst ⁻¹' s :=
inter_subset_left _ _
lemma fst_image_prod_subset (s : set α) (t : set β) : prod.fst '' s ×ˢ t ⊆ s :=
image_subset_iff.2 $ prod_subset_preimage_fst s t
lemma fst_image_prod (s : set β) {t : set α} (ht : t.nonempty) : prod.fst '' s ×ˢ t = s :=
(fst_image_prod_subset _ _).antisymm $ λ y hy, let ⟨x, hx⟩ := ht in ⟨(y, x), ⟨hy, hx⟩, rfl⟩
lemma prod_subset_preimage_snd (s : set α) (t : set β) : s ×ˢ t ⊆ prod.snd ⁻¹' t :=
inter_subset_right _ _
lemma snd_image_prod_subset (s : set α) (t : set β) : prod.snd '' s ×ˢ t ⊆ t :=
image_subset_iff.2 $ prod_subset_preimage_snd s t
lemma snd_image_prod {s : set α} (hs : s.nonempty) (t : set β) : prod.snd '' s ×ˢ t = t :=
(snd_image_prod_subset _ _).antisymm $ λ y y_in, let ⟨x, x_in⟩ := hs in ⟨(x, y), ⟨x_in, y_in⟩, rfl⟩
lemma prod_diff_prod : s ×ˢ t \ s₁ ×ˢ t₁ = s ×ˢ (t \ t₁) ∪ (s \ s₁) ×ˢ t :=
by { ext x, by_cases h₁ : x.1 ∈ s₁; by_cases h₂ : x.2 ∈ t₁; simp * }
/-- A product set is included in a product set if and only factors are included, or a factor of the
first set is empty. -/
lemma prod_subset_prod_iff : s ×ˢ t ⊆ s₁ ×ˢ t₁ ↔ s ⊆ s₁ ∧ t ⊆ t₁ ∨ s = ∅ ∨ t = ∅ :=
begin
cases (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,
refine ⟨λ H, or.inl ⟨_, _⟩, _⟩,
{ have := image_subset (prod.fst : α × β → α) H,
rwa [fst_image_prod _ st.2, fst_image_prod _ (h.mono H).snd] at this },
{ have := image_subset (prod.snd : α × β → β) H,
rwa [snd_image_prod st.1, snd_image_prod (h.mono H).fst] at this },
{ intro H,
simp only [st.1.ne_empty, st.2.ne_empty, or_false] at H,
exact prod_mono H.1 H.2 }
end
lemma prod_eq_prod_iff_of_nonempty (h : (s ×ˢ t).nonempty) :
s ×ˢ t = s₁ ×ˢ t₁ ↔ s = s₁ ∧ t = t₁ :=
begin
split,
{ intro heq,
have h₁ : (s₁ ×ˢ t₁ : set _).nonempty, { rwa [← heq] },
rw [prod_nonempty_iff] at h h₁,
rw [← fst_image_prod s h.2, ← fst_image_prod s₁ h₁.2, heq, eq_self_iff_true, true_and,
← snd_image_prod h.1 t, ← snd_image_prod h₁.1 t₁, heq] },
{ rintro ⟨rfl, rfl⟩, refl }
end
lemma prod_eq_prod_iff : s ×ˢ t = s₁ ×ˢ t₁ ↔ s = s₁ ∧ t = t₁ ∨ (s = ∅ ∨ t = ∅) ∧
(s₁ = ∅ ∨ t₁ = ∅) :=
begin
symmetry,
cases eq_empty_or_nonempty (s ×ˢ t) with h h,
{ simp_rw [h, @eq_comm _ ∅, prod_eq_empty_iff, prod_eq_empty_iff.mp h, true_and,
or_iff_right_iff_imp],
rintro ⟨rfl, rfl⟩, exact prod_eq_empty_iff.mp h },
rw [prod_eq_prod_iff_of_nonempty h],
rw [nonempty_iff_ne_empty, ne.def, prod_eq_empty_iff] at h,
simp_rw [h, false_and, or_false],
end
@[simp] lemma prod_eq_iff_eq (ht : t.nonempty) : s ×ˢ t = s₁ ×ˢ t ↔ s = s₁ :=
begin
simp_rw [prod_eq_prod_iff, ht.ne_empty, eq_self_iff_true, and_true, or_iff_left_iff_imp,
or_false],
rintro ⟨rfl, rfl⟩,
refl,
end
section mono
variables [preorder α] {f : α → set β} {g : α → set γ}
theorem _root_.monotone.set_prod (hf : monotone f) (hg : monotone g) : monotone (λ x, f x ×ˢ g x) :=
λ a b h, prod_mono (hf h) (hg h)
theorem _root_.antitone.set_prod (hf : antitone f) (hg : antitone g) : antitone (λ x, f x ×ˢ g x) :=
λ a b h, prod_mono (hf h) (hg h)
theorem _root_.monotone_on.set_prod (hf : monotone_on f s) (hg : monotone_on g s) :
monotone_on (λ x, f x ×ˢ g x) s :=
λ a ha b hb h, prod_mono (hf ha hb h) (hg ha hb h)
theorem _root_.antitone_on.set_prod (hf : antitone_on f s) (hg : antitone_on g s) :
antitone_on (λ x, f x ×ˢ g x) s :=
λ a ha b hb h, prod_mono (hf ha hb h) (hg ha hb h)
end mono
end prod
/-! ### Diagonal
In this section we prove some lemmas about the diagonal set `{p | p.1 = p.2}` and the diagonal map
`λ x, (x, x)`.
-/
section diagonal
variables {α : Type*} {s t : set α}
/-- `diagonal α` is the set of `α × α` consisting of all pairs of the form `(a, a)`. -/
def diagonal (α : Type*) : set (α × α) := {p | p.1 = p.2}
lemma mem_diagonal (x : α) : (x, x) ∈ diagonal α := by simp [diagonal]
@[simp] lemma mem_diagonal_iff {x : α × α} : x ∈ diagonal α ↔ x.1 = x.2 := iff.rfl
lemma diagonal_nonempty [nonempty α] : (diagonal α).nonempty :=
nonempty.elim ‹_› $ λ x, ⟨_, mem_diagonal x⟩
instance decidable_mem_diagonal [h : decidable_eq α] (x : α × α) : decidable (x ∈ diagonal α) :=
h x.1 x.2
lemma preimage_coe_coe_diagonal (s : set α) : (prod.map coe coe) ⁻¹' (diagonal α) = diagonal s :=
by { ext ⟨⟨x, hx⟩, ⟨y, hy⟩⟩, simp [set.diagonal] }
@[simp] lemma range_diag : range (λ x, (x, x)) = diagonal α :=
by { ext ⟨x, y⟩, simp [diagonal, eq_comm] }
lemma diagonal_subset_iff {s} : diagonal α ⊆ s ↔ ∀ x, (x, x) ∈ s :=
by rw [← range_diag, range_subset_iff]
@[simp] lemma prod_subset_compl_diagonal_iff_disjoint : s ×ˢ t ⊆ (diagonal α)ᶜ ↔ disjoint s t :=
prod_subset_iff.trans disjoint_iff_forall_ne.symm
@[simp] lemma diag_preimage_prod (s t : set α) : (λ x, (x, x)) ⁻¹' (s ×ˢ t) = s ∩ t := rfl
lemma diag_preimage_prod_self (s : set α) : (λ x, (x, x)) ⁻¹' (s ×ˢ s) = s := inter_self s
lemma diag_image (s : set α) : (λ x, (x, x)) '' s = diagonal α ∩ (s ×ˢ s) :=
begin
ext x, split,
{ rintro ⟨x, hx, rfl⟩, exact ⟨rfl, hx, hx⟩ },
{ obtain ⟨x, y⟩ := x,
rintro ⟨rfl : x = y, h2x⟩,
exact mem_image_of_mem _ h2x.1 }
end
end diagonal
section off_diag
variables {α : Type*} {s t : set α} {x : α × α} {a : α}
/-- The off-diagonal of a set `s` is the set of pairs `(a, b)` with `a, b ∈ s` and `a ≠ b`. -/
def off_diag (s : set α) : set (α × α) := {x | x.1 ∈ s ∧ x.2 ∈ s ∧ x.1 ≠ x.2}
@[simp] lemma mem_off_diag : x ∈ s.off_diag ↔ x.1 ∈ s ∧ x.2 ∈ s ∧ x.1 ≠ x.2 := iff.rfl
lemma off_diag_mono : monotone (off_diag : set α → set (α × α)) :=
λ s t h x, and.imp (@h _) $ and.imp_left $ @h _
@[simp] lemma off_diag_nonempty : s.off_diag.nonempty ↔ s.nontrivial :=
by simp [off_diag, set.nonempty, set.nontrivial]
@[simp] lemma off_diag_eq_empty : s.off_diag = ∅ ↔ s.subsingleton :=
by rw [←not_nonempty_iff_eq_empty, ←not_nontrivial_iff, off_diag_nonempty.not]
alias off_diag_nonempty ↔ _ nontrivial.off_diag_nonempty
alias off_diag_nonempty ↔ _ subsingleton.off_diag_eq_empty
variables (s t)
lemma off_diag_subset_prod : s.off_diag ⊆ s ×ˢ s := λ x hx, ⟨hx.1, hx.2.1⟩
lemma off_diag_eq_sep_prod : s.off_diag = {x ∈ s ×ˢ s | x.1 ≠ x.2} := ext $ λ _, and.assoc.symm
@[simp] lemma off_diag_empty : (∅ : set α).off_diag = ∅ := by simp
@[simp] lemma off_diag_singleton (a : α) : ({a} : set α).off_diag = ∅ := by simp
@[simp] lemma off_diag_univ : (univ : set α).off_diag = (diagonal α)ᶜ := ext $ by simp
@[simp] lemma prod_sdiff_diagonal : s ×ˢ s \ diagonal α = s.off_diag := ext $ λ _, and.assoc
@[simp] lemma disjoint_diagonal_off_diag : disjoint (diagonal α) s.off_diag :=
disjoint_left.mpr $ λ x hd ho, ho.2.2 hd
lemma off_diag_inter : (s ∩ t).off_diag = s.off_diag ∩ t.off_diag :=
ext $ λ x, by { simp only [mem_off_diag, mem_inter_iff], tauto }
variables {s t}
lemma off_diag_union (h : disjoint s t) :
(s ∪ t).off_diag = s.off_diag ∪ t.off_diag ∪ s ×ˢ t ∪ t ×ˢ s :=
begin
rw [off_diag_eq_sep_prod, union_prod, prod_union, prod_union, union_comm _ (t ×ˢ t), union_assoc,
union_left_comm (s ×ˢ t), ←union_assoc, sep_union, sep_union, ←off_diag_eq_sep_prod,
←off_diag_eq_sep_prod, sep_eq_self_iff_mem_true.2, ←union_assoc],
simp only [mem_union, mem_prod, ne.def, prod.forall],
rintro i j (⟨hi, hj⟩ | ⟨hi, hj⟩) rfl; exact h.le_bot ⟨‹_›, ‹_›⟩,
end
lemma off_diag_insert (ha : a ∉ s) : (insert a s).off_diag = s.off_diag ∪ {a} ×ˢ s ∪ s ×ˢ {a} :=
begin
rw [insert_eq, union_comm, off_diag_union, off_diag_singleton, union_empty, union_right_comm],
rw disjoint_left,
rintro b hb (rfl : b = a),
exact ha hb
end
end off_diag
/-! ### Cartesian set-indexed product of sets -/
section pi
variables {ι : Type*} {α β : ι → Type*} {s s₁ s₂ : set ι} {t t₁ t₂ : Π i, set (α i)} {i : ι}
/-- Given an index set `ι` and a family of sets `t : Π i, set (α i)`, `pi s t`
is the set of dependent functions `f : Πa, π a` such that `f a` belongs to `t a`
whenever `a ∈ s`. -/
def pi (s : set ι) (t : Π i, set (α i)) : set (Π i, α i) := {f | ∀ i ∈ s, f i ∈ t i}
@[simp] lemma mem_pi {f : Π i, α i} : f ∈ s.pi t ↔ ∀ i ∈ s, f i ∈ t i := iff.rfl
@[simp] lemma mem_univ_pi {f : Π i, α i} : f ∈ pi univ t ↔ ∀ i, f i ∈ t i := by simp
@[simp] lemma empty_pi (s : Π i, set (α i)) : pi ∅ s = univ := by { ext, simp [pi] }
@[simp] lemma pi_univ (s : set ι) : pi s (λ i, (univ : set (α i))) = univ :=
eq_univ_of_forall $ λ f i hi, mem_univ _
lemma pi_mono (h : ∀ i ∈ s, t₁ i ⊆ t₂ i) : pi s t₁ ⊆ pi s t₂ :=
λ x hx i hi, (h i hi $ hx i hi)
lemma pi_inter_distrib : s.pi (λ i, t i ∩ t₁ i) = s.pi t ∩ s.pi t₁ :=
ext $ λ x, by simp only [forall_and_distrib, mem_pi, mem_inter_iff]
lemma pi_congr (h : s₁ = s₂) (h' : ∀ i ∈ s₁, t₁ i = t₂ i) : s₁.pi t₁ = s₂.pi t₂ :=
h ▸ (ext $ λ x, forall₂_congr $ λ i hi, h' i hi ▸ iff.rfl)
lemma pi_eq_empty (hs : i ∈ s) (ht : t i = ∅) : s.pi t = ∅ :=
by { ext f, simp only [mem_empty_iff_false, not_forall, iff_false, mem_pi, not_imp],
exact ⟨i, hs, by simp [ht]⟩ }
lemma univ_pi_eq_empty (ht : t i = ∅) : pi univ t = ∅ := pi_eq_empty (mem_univ i) ht
lemma pi_nonempty_iff : (s.pi t).nonempty ↔ ∀ i, ∃ x, i ∈ s → x ∈ t i :=
by simp [classical.skolem, set.nonempty]
lemma univ_pi_nonempty_iff : (pi univ t).nonempty ↔ ∀ i, (t i).nonempty :=
by simp [classical.skolem, set.nonempty]
lemma pi_eq_empty_iff : s.pi t = ∅ ↔ ∃ i, is_empty (α i) ∨ i ∈ s ∧ t i = ∅ :=
begin
rw [← not_nonempty_iff_eq_empty, pi_nonempty_iff],
push_neg,
refine exists_congr (λ i, _),
casesI is_empty_or_nonempty (α i); simp [*, forall_and_distrib, eq_empty_iff_forall_not_mem],
end
@[simp] lemma univ_pi_eq_empty_iff : pi univ t = ∅ ↔ ∃ i, t i = ∅ :=
by simp [← not_nonempty_iff_eq_empty, univ_pi_nonempty_iff]
@[simp] lemma univ_pi_empty [h : nonempty ι] : pi univ (λ i, ∅ : Π i, set (α i)) = ∅ :=
univ_pi_eq_empty_iff.2 $ h.elim $ λ x, ⟨x, rfl⟩
@[simp] lemma disjoint_univ_pi : disjoint (pi univ t₁) (pi univ t₂) ↔ ∃ i, disjoint (t₁ i) (t₂ i) :=
by simp only [disjoint_iff_inter_eq_empty, ← pi_inter_distrib, univ_pi_eq_empty_iff]
@[simp] lemma range_dcomp (f : Π i, α i → β i) :
range (λ (g : Π i, α i), (λ i, f i (g i))) = pi univ (λ i, range (f i)) :=
begin
apply subset.antisymm _ (λ x hx, _),
{ rintro _ ⟨x, rfl⟩ i -,
exact ⟨x i, rfl⟩ },
{ choose y hy using hx,
exact ⟨λ i, y i trivial, funext $ λ i, hy i trivial⟩ }
end
@[simp] lemma insert_pi (i : ι) (s : set ι) (t : Π i, set (α i)) :
pi (insert i s) t = (eval i ⁻¹' t i) ∩ pi s t :=
by { ext, simp [pi, or_imp_distrib, forall_and_distrib] }
@[simp] lemma singleton_pi (i : ι) (t : Π i, set (α i)) : pi {i} t = (eval i ⁻¹' t i) :=
by { ext, simp [pi] }
lemma singleton_pi' (i : ι) (t : Π i, set (α i)) : pi {i} t = {x | x i ∈ t i} := singleton_pi i t
lemma univ_pi_singleton (f : Π i, α i) : pi univ (λ i, {f i}) = ({f} : set (Π i, α i)) :=
ext $ λ g, by simp [funext_iff]
lemma preimage_pi (s : set ι) (t : Π i, set (β i)) (f : Π i, α i → β i) :
(λ (g : Π i, α i) i, f _ (g i)) ⁻¹' s.pi t = s.pi (λ i, f i ⁻¹' t i) := rfl
lemma pi_if {p : ι → Prop} [h : decidable_pred p] (s : set ι) (t₁ t₂ : Π i, set (α i)) :
pi s (λ i, if p i then t₁ i else t₂ i) = pi {i ∈ s | p i} t₁ ∩ pi {i ∈ s | ¬ p i} t₂ :=
begin
ext f,
refine ⟨λ h, _, _⟩,
{ split; { rintro i ⟨his, hpi⟩, simpa [*] using h i } },
{ rintro ⟨ht₁, ht₂⟩ i his,
by_cases p i; simp * at * }
end
lemma union_pi : (s₁ ∪ s₂).pi t = s₁.pi t ∩ s₂.pi t :=
by simp [pi, or_imp_distrib, forall_and_distrib, set_of_and]
@[simp] lemma pi_inter_compl (s : set ι) : pi s t ∩ pi sᶜ t = pi univ t :=
by rw [← union_pi, union_compl_self]
lemma pi_update_of_not_mem [decidable_eq ι] (hi : i ∉ s) (f : Π j, α j) (a : α i)
(t : Π j, α j → set (β j)) :
s.pi (λ j, t j (update f i a j)) = s.pi (λ j, t j (f j)) :=
pi_congr rfl $ λ j hj, by { rw update_noteq, exact λ h, hi (h ▸ hj) }
lemma pi_update_of_mem [decidable_eq ι] (hi : i ∈ s) (f : Π j, α j) (a : α i)
(t : Π j, α j → set (β j)) :
s.pi (λ j, t j (update f i a j)) = {x | x i ∈ t i a} ∩ (s \ {i}).pi (λ j, t j (f j)) :=
calc s.pi (λ j, t j (update f i a j)) = ({i} ∪ s \ {i}).pi (λ j, t j (update f i a j)) :
by rw [union_diff_self, union_eq_self_of_subset_left (singleton_subset_iff.2 hi)]
... = {x | x i ∈ t i a} ∩ (s \ {i}).pi (λ j, t j (f j)) :
by { rw [union_pi, singleton_pi', update_same, pi_update_of_not_mem], simp }
lemma univ_pi_update [decidable_eq ι] {β : Π i, Type*} (i : ι) (f : Π j, α j) (a : α i)
(t : Π j, α j → set (β j)) :
pi univ (λ j, t j (update f i a j)) = {x | x i ∈ t i a} ∩ pi {i}ᶜ (λ j, t j (f j)) :=
by rw [compl_eq_univ_diff, ← pi_update_of_mem (mem_univ _)]
lemma univ_pi_update_univ [decidable_eq ι] (i : ι) (s : set (α i)) :
pi univ (update (λ j : ι, (univ : set (α j))) i s) = eval i ⁻¹' s :=
by rw [univ_pi_update i (λ j, (univ : set (α j))) s (λ j t, t), pi_univ, inter_univ, preimage]
lemma eval_image_pi_subset (hs : i ∈ s) : eval i '' s.pi t ⊆ t i :=
image_subset_iff.2 $ λ f hf, hf i hs
lemma eval_image_univ_pi_subset : eval i '' pi univ t ⊆ t i :=
eval_image_pi_subset (mem_univ i)
lemma subset_eval_image_pi (ht : (s.pi t).nonempty) (i : ι) : t i ⊆ eval i '' s.pi t :=
begin
classical,
obtain ⟨f, hf⟩ := ht,
refine λ y hy, ⟨update f i y, λ j hj, _, update_same _ _ _⟩,
obtain rfl | hji := eq_or_ne j i; simp [*, hf _ hj]
end
lemma eval_image_pi (hs : i ∈ s) (ht : (s.pi t).nonempty) : eval i '' s.pi t = t i :=
(eval_image_pi_subset hs).antisymm (subset_eval_image_pi ht i)
@[simp] lemma eval_image_univ_pi (ht : (pi univ t).nonempty) :
(λ f : Π i, α i, f i) '' pi univ t = t i :=
eval_image_pi (mem_univ i) ht
lemma pi_subset_pi_iff : pi s t₁ ⊆ pi s t₂ ↔ (∀ i ∈ s, t₁ i ⊆ t₂ i) ∨ pi s t₁ = ∅ :=
begin
refine ⟨λ h, or_iff_not_imp_right.2 _, λ h, h.elim pi_mono (λ h', h'.symm ▸ empty_subset _)⟩,
rw [← ne.def, ←nonempty_iff_ne_empty],
intros hne i hi,
simpa only [eval_image_pi hi hne, eval_image_pi hi (hne.mono h)]
using image_subset (λ f : Π i, α i, f i) h
end
lemma univ_pi_subset_univ_pi_iff : pi univ t₁ ⊆ pi univ t₂ ↔ (∀ i, t₁ i ⊆ t₂ i) ∨ ∃ i, t₁ i = ∅ :=
by simp [pi_subset_pi_iff]
lemma eval_preimage [decidable_eq ι] {s : set (α i)} :
eval i ⁻¹' s = pi univ (update (λ i, univ) i s) :=
by { ext x, simp [@forall_update_iff _ (λ i, set (α i)) _ _ _ _ (λ i' y, x i' ∈ y)] }
lemma eval_preimage' [decidable_eq ι] {s : set (α i)} :
eval i ⁻¹' s = pi {i} (update (λ i, univ) i s) :=
by { ext, simp }
lemma update_preimage_pi [decidable_eq ι] {f : Π i, α i} (hi : i ∈ s)
(hf : ∀ j ∈ s, j ≠ i → f j ∈ t j) :
(update f i) ⁻¹' s.pi t = t i :=
begin
ext x,
refine ⟨λ h, _, λ hx j hj, _⟩,
{ convert h i hi,
simp },
{ obtain rfl | h := eq_or_ne j i,
{ simpa },
{ rw update_noteq h,
exact hf j hj h } }
end
lemma update_preimage_univ_pi [decidable_eq ι] {f : Π i, α i} (hf : ∀ j ≠ i, f j ∈ t j) :
(update f i) ⁻¹' pi univ t = t i :=
update_preimage_pi (mem_univ i) (λ j _, hf j)
lemma subset_pi_eval_image (s : set ι) (u : set (Π i, α i)) : u ⊆ pi s (λ i, eval i '' u) :=
λ f hf i hi, ⟨f, hf, rfl⟩
lemma univ_pi_ite (s : set ι) [decidable_pred (∈ s)] (t : Π i, set (α i)) :
pi univ (λ i, if i ∈ s then t i else univ) = s.pi t :=
by { ext, simp_rw [mem_univ_pi], refine forall_congr (λ i, _), split_ifs; simp [h] }
end pi
end set
|
ed98a941d721b66854d79cd8bc2902aaa4a51141 | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/category_theory/limits/filtered_colimit_commutes_finite_limit.lean | 71c73bfcd1c211a264e548166621ce5cb78776dc | [
"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 | 16,745 | lean | /-
Copyright (c) 2020 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import category_theory.limits.colimit_limit
import category_theory.limits.preserves.functor_category
import category_theory.limits.preserves.finite
import category_theory.limits.shapes.finite_limits
import category_theory.limits.preserves.filtered
/-!
# Filtered colimits commute with finite limits.
We show that for a functor `F : J × K ⥤ Type v`, when `J` is finite and `K` is filtered,
the universal morphism `colimit_limit_to_limit_colimit F` comparing the
colimit (over `K`) of the limits (over `J`) with the limit of the colimits is an isomorphism.
(In fact, to prove that it is injective only requires that `J` has finitely many objects.)
## References
* Borceux, Handbook of categorical algebra 1, Theorem 2.13.4
* [Stacks: Filtered colimits](https://stacks.math.columbia.edu/tag/002W)
-/
universes v u
open category_theory
open category_theory.category
open category_theory.limits.types
open category_theory.limits.types.filtered_colimit
namespace category_theory.limits
variables {J K : Type v} [small_category J] [small_category K]
variables (F : J × K ⥤ Type v)
open category_theory.prod
variables [is_filtered K]
section
/-!
Injectivity doesn't need that we have finitely many morphisms in `J`,
only that there are finitely many objects.
-/
variables [fintype J]
/--
This follows this proof from
* Borceux, Handbook of categorical algebra 1, Theorem 2.13.4
-/
lemma colimit_limit_to_limit_colimit_injective :
function.injective (colimit_limit_to_limit_colimit F) :=
begin
classical,
-- Suppose we have two terms `x y` in the colimit (over `K`) of the limits (over `J`),
-- and that these have the same image under `colimit_limit_to_limit_colimit F`.
intros x y h,
-- These elements of the colimit have representatives somewhere:
obtain ⟨kx, x, rfl⟩ := jointly_surjective' x,
obtain ⟨ky, y, rfl⟩ := jointly_surjective' y,
dsimp at x y,
-- Since the images of `x` and `y` are equal in a limit, they are equal componentwise
-- (indexed by `j : J`),
replace h := λ j, congr_arg (limit.π ((curry.obj F) ⋙ colim) j) h,
-- and they are equations in a filtered colimit,
-- so for each `j` we have some place `k j` to the right of both `kx` and `ky`
simp [colimit_eq_iff] at h,
let k := λ j, (h j).some,
let f : Π j, kx ⟶ k j := λ j, (h j).some_spec.some,
let g : Π j, ky ⟶ k j := λ j, (h j).some_spec.some_spec.some,
-- where the images of the components of the representatives become equal:
have w : Π j,
F.map ((𝟙 j, f j) : (j, kx) ⟶ (j, k j)) (limit.π ((curry.obj (swap K J ⋙ F)).obj kx) j x) =
F.map ((𝟙 j, g j) : (j, ky) ⟶ (j, k j)) (limit.π ((curry.obj (swap K J ⋙ F)).obj ky) j y) :=
λ j, (h j).some_spec.some_spec.some_spec,
-- We now use that `K` is filtered, picking some point to the right of all these
-- morphisms `f j` and `g j`.
let O : finset K := (finset.univ).image k ∪ {kx, ky},
have kxO : kx ∈ O := finset.mem_union.mpr (or.inr (by simp)),
have kyO : ky ∈ O := finset.mem_union.mpr (or.inr (by simp)),
have kjO : ∀ j, k j ∈ O := λ j, finset.mem_union.mpr (or.inl (by simp)),
let H : finset (Σ' (X Y : K) (mX : X ∈ O) (mY : Y ∈ O), X ⟶ Y) :=
(finset.univ).image (λ j : J, ⟨kx, k j, kxO,
finset.mem_union.mpr (or.inl (by simp)),
f j⟩) ∪
(finset.univ).image (λ j : J, ⟨ky, k j, kyO,
finset.mem_union.mpr (or.inl (by simp)),
g j⟩),
obtain ⟨S, T, W⟩ := is_filtered.sup_exists O H,
have fH :
∀ j, (⟨kx, k j, kxO, kjO j, f j⟩ : (Σ' (X Y : K) (mX : X ∈ O) (mY : Y ∈ O), X ⟶ Y)) ∈ H :=
λ j, (finset.mem_union.mpr (or.inl
begin
simp only [true_and, finset.mem_univ, eq_self_iff_true, exists_prop_of_true,
finset.mem_image, heq_iff_eq],
refine ⟨j, rfl, _⟩,
simp only [heq_iff_eq],
exact ⟨rfl, rfl, rfl⟩,
end)),
have gH :
∀ j, (⟨ky, k j, kyO, kjO j, g j⟩ : (Σ' (X Y : K) (mX : X ∈ O) (mY : Y ∈ O), X ⟶ Y)) ∈ H :=
λ j, (finset.mem_union.mpr (or.inr
begin
simp only [true_and, finset.mem_univ, eq_self_iff_true, exists_prop_of_true,
finset.mem_image, heq_iff_eq],
refine ⟨j, rfl, _⟩,
simp only [heq_iff_eq],
exact ⟨rfl, rfl, rfl⟩,
end)),
-- Our goal is now an equation between equivalence classes of representatives of a colimit,
-- and so it suffices to show those representative become equal somewhere, in particular at `S`.
apply colimit_sound' (T kxO) (T kyO),
-- We can check if two elements of a limit (in `Type`) are equal by comparing them componentwise.
ext,
-- Now it's just a calculation using `W` and `w`.
simp only [functor.comp_map, limit.map_π_apply, curry.obj_map_app, swap_map],
rw ←W _ _ (fH j),
rw ←W _ _ (gH j),
simp [w],
end
end
variables [fin_category J]
/--
This follows this proof from
* Borceux, Handbook of categorical algebra 1, Theorem 2.13.4
although with different names.
-/
lemma colimit_limit_to_limit_colimit_surjective :
function.surjective (colimit_limit_to_limit_colimit F) :=
begin
classical,
-- We begin with some element `x` in the limit (over J) over the colimits (over K),
intro x,
-- This consists of some coherent family of elements in the various colimits,
-- and so our first task is to pick representatives of these elements.
have z := λ j, jointly_surjective' (limit.π (curry.obj F ⋙ limits.colim) j x),
-- `k : J ⟶ K` records where the representative of the element in the `j`-th element of `x` lives
let k : J → K := λ j, (z j).some,
-- `y j : F.obj (j, k j)` is the representative
let y : Π j, F.obj (j, k j) := λ j, (z j).some_spec.some,
-- and we record that these representatives, when mapped back into the relevant colimits,
-- are actually the components of `x`.
have e : ∀ j,
colimit.ι ((curry.obj F).obj j) (k j) (y j) =
limit.π (curry.obj F ⋙ limits.colim) j x := λ j, (z j).some_spec.some_spec,
clear_value k y, -- A little tidying up of things we no longer need.
clear z,
-- As a first step, we use that `K` is filtered to pick some point `k' : K` above all the `k j`
let k' : K := is_filtered.sup (finset.univ.image k) ∅,
-- and name the morphisms as `g j : k j ⟶ k'`.
have g : Π j, k j ⟶ k' := λ j, is_filtered.to_sup (finset.univ.image k) ∅ (by simp),
clear_value k',
-- Recalling that the components of `x`, which are indexed by `j : J`, are "coherent",
-- in other words preserved by morphisms in the `J` direction,
-- we see that for any morphism `f : j ⟶ j'` in `J`,
-- the images of `y j` and `y j'`, when mapped to `F.obj (j', k')` respectively by
-- `(f, g j)` and `(𝟙 j', g j')`, both represent the same element in the colimit.
have w : ∀ {j j' : J} (f : j ⟶ j'),
colimit.ι ((curry.obj F).obj j') k' (F.map ((𝟙 j', g j') : (j', k j') ⟶ (j', k')) (y j')) =
colimit.ι ((curry.obj F).obj j') k' (F.map ((f, g j) : (j, k j) ⟶ (j', k')) (y j)),
{ intros j j' f,
have t : (f, g j) = (((f, 𝟙 (k j)) : (j, k j) ⟶ (j', k j)) ≫ (𝟙 j', g j) : (j, k j) ⟶ (j', k')),
{ simp only [id_comp, comp_id, prod_comp], },
erw [colimit.w_apply, t, functor_to_types.map_comp_apply, colimit.w_apply, e,
←limit.w_apply f, ←e],
simp, },
-- Because `K` is filtered, we can restate this as saying that
-- for each such `f`, there is some place to the right of `k'`
-- where these images of `y j` and `y j'` become equal.
simp_rw colimit_eq_iff at w,
-- We take a moment to restate `w` more conveniently.
let kf : Π {j j'} (f : j ⟶ j'), K := λ _ _ f, (w f).some,
let gf : Π {j j'} (f : j ⟶ j'), k' ⟶ kf f := λ _ _ f, (w f).some_spec.some,
let hf : Π {j j'} (f : j ⟶ j'), k' ⟶ kf f := λ _ _ f, (w f).some_spec.some_spec.some,
have wf : Π {j j'} (f : j ⟶ j'),
F.map ((𝟙 j', g j' ≫ gf f) : (j', k j') ⟶ (j', kf f)) (y j') =
F.map ((f, g j ≫ hf f) : (j, k j) ⟶ (j', kf f)) (y j) := λ j j' f,
begin
have q :
((curry.obj F).obj j').map (gf f) (F.map _ (y j')) =
((curry.obj F).obj j').map (hf f) (F.map _ (y j)) :=
(w f).some_spec.some_spec.some_spec,
dsimp at q,
simp_rw ←functor_to_types.map_comp_apply at q,
convert q; simp only [comp_id],
end,
clear_value kf gf hf, -- and clean up some things that are no longer needed.
clear w,
-- We're now ready to use the fact that `K` is filtered a second time,
-- picking some place to the right of all of
-- the morphisms `gf f : k' ⟶ kh f` and `hf f : k' ⟶ kf f`.
-- At this point we're relying on there being only finitely morphisms in `J`.
let O := finset.univ.bUnion (λ j, finset.univ.bUnion (λ j', finset.univ.image (@kf j j'))) ∪ {k'},
have kfO : ∀ {j j'} (f : j ⟶ j'), kf f ∈ O := λ j j' f, finset.mem_union.mpr (or.inl (
begin
rw [finset.mem_bUnion],
refine ⟨j, finset.mem_univ j, _⟩,
rw [finset.mem_bUnion],
refine ⟨j', finset.mem_univ j', _⟩,
rw [finset.mem_image],
refine ⟨f, finset.mem_univ _, _⟩,
refl,
end)),
have k'O : k' ∈ O := finset.mem_union.mpr (or.inr (finset.mem_singleton.mpr rfl)),
let H : finset (Σ' (X Y : K) (mX : X ∈ O) (mY : Y ∈ O), X ⟶ Y) :=
finset.univ.bUnion (λ j : J, finset.univ.bUnion (λ j' : J, finset.univ.bUnion (λ f : j ⟶ j',
{⟨k', kf f, k'O, kfO f, gf f⟩, ⟨k', kf f, k'O, kfO f, hf f⟩}))),
obtain ⟨k'', i', s'⟩ := is_filtered.sup_exists O H,
-- We then restate this slightly more conveniently, as a family of morphism `i f : kf f ⟶ k''`,
-- satisfying `gf f ≫ i f = hf f' ≫ i f'`.
let i : Π {j j'} (f : j ⟶ j'), kf f ⟶ k'' := λ j j' f, i' (kfO f),
have s : ∀ {j₁ j₂ j₃ j₄} (f : j₁ ⟶ j₂) (f' : j₃ ⟶ j₄), gf f ≫ i f = hf f' ≫ i f' :=
begin
intros,
rw [s', s'],
swap 2,
exact k'O,
swap 2,
{ rw [finset.mem_bUnion],
refine ⟨j₁, finset.mem_univ _, _⟩,
rw [finset.mem_bUnion],
refine ⟨j₂, finset.mem_univ _, _⟩,
rw [finset.mem_bUnion],
refine ⟨f, finset.mem_univ _, _⟩,
simp only [true_or, eq_self_iff_true, and_self, finset.mem_insert, heq_iff_eq], },
{ rw [finset.mem_bUnion],
refine ⟨j₃, finset.mem_univ _, _⟩,
rw [finset.mem_bUnion],
refine ⟨j₄, finset.mem_univ _, _⟩,
rw [finset.mem_bUnion],
refine ⟨f', finset.mem_univ _, _⟩,
simp only [eq_self_iff_true, or_true, and_self, finset.mem_insert, finset.mem_singleton,
heq_iff_eq], }
end,
clear_value i,
clear s' i' H kfO k'O O,
-- We're finally ready to construct the pre-image, and verify it really maps to `x`.
fsplit,
{ -- We construct the pre-image (which, recall is meant to be a point
-- in the colimit (over `K`) of the limits (over `J`)) via a representative at `k''`.
apply colimit.ι (curry.obj (swap K J ⋙ F) ⋙ limits.lim) k'' _,
dsimp,
-- This representative is meant to be an element of a limit,
-- so we need to construct a family of elements in `F.obj (j, k'')` for varying `j`,
-- then show that are coherent with respect to morphisms in the `j` direction.
ext, swap,
{ -- We construct the elements as the images of the `y j`.
exact λ j, F.map (⟨𝟙 j, g j ≫ gf (𝟙 j) ≫ i (𝟙 j)⟩ : (j, k j) ⟶ (j, k'')) (y j), },
{ -- After which it's just a calculation, using `s` and `wf`, to see they are coherent.
dsimp,
simp only [←functor_to_types.map_comp_apply, prod_comp, id_comp, comp_id],
calc F.map ((f, g j ≫ gf (𝟙 j) ≫ i (𝟙 j)) : (j, k j) ⟶ (j', k'')) (y j)
= F.map ((f, g j ≫ hf f ≫ i f) : (j, k j) ⟶ (j', k'')) (y j)
: by rw s (𝟙 j) f
... = F.map ((𝟙 j', i f) : (j', kf f) ⟶ (j', k''))
(F.map ((f, g j ≫ hf f) : (j, k j) ⟶ (j', kf f)) (y j))
: by rw [←functor_to_types.map_comp_apply, prod_comp, comp_id, assoc]
... = F.map ((𝟙 j', i f) : (j', kf f) ⟶ (j', k''))
(F.map ((𝟙 j', g j' ≫ gf f) : (j', k j') ⟶ (j', kf f)) (y j'))
: by rw ←wf f
... = F.map ((𝟙 j', g j' ≫ gf f ≫ i f) : (j', k j') ⟶ (j', k'')) (y j')
: by rw [←functor_to_types.map_comp_apply, prod_comp, id_comp, assoc]
... = F.map ((𝟙 j', g j' ≫ gf (𝟙 j') ≫ i (𝟙 j')) : (j', k j') ⟶ (j', k'')) (y j')
: by rw [s f (𝟙 j'), ←s (𝟙 j') (𝟙 j')], }, },
-- Finally we check that this maps to `x`.
{ -- We can do this componentwise:
apply limit_ext,
intro j,
-- and as each component is an equation in a colimit, we can verify it by
-- pointing out the morphism which carries one representative to the other:
simp only [←e, colimit_eq_iff, curry.obj_obj_map, limit.π_mk,
bifunctor.map_id_comp, id.def, types_comp_apply,
limits.ι_colimit_limit_to_limit_colimit_π_apply],
refine ⟨k'', 𝟙 k'', g j ≫ gf (𝟙 j) ≫ i (𝟙 j), _⟩,
simp only [bifunctor.map_id_comp, types_comp_apply, bifunctor.map_id, types_id_apply], },
end
instance colimit_limit_to_limit_colimit_is_iso :
is_iso (colimit_limit_to_limit_colimit F) :=
(is_iso_iff_bijective _).mpr
⟨colimit_limit_to_limit_colimit_injective F, colimit_limit_to_limit_colimit_surjective F⟩
instance colimit_limit_to_limit_colimit_cone_iso (F : J ⥤ K ⥤ Type v) :
is_iso (colimit_limit_to_limit_colimit_cone F) :=
begin
haveI : is_iso (colimit_limit_to_limit_colimit_cone F).hom,
{ dsimp only [colimit_limit_to_limit_colimit_cone], apply_instance },
apply cones.cone_iso_of_hom_iso,
end
noncomputable
instance filtered_colim_preserves_finite_limits_of_types :
preserves_finite_limits (colim : (K ⥤ Type v) ⥤ _) := ⟨λ J _ _, by exactI ⟨λ F, ⟨λ c hc,
begin
apply is_limit.of_iso_limit (limit.is_limit _),
symmetry,
transitivity (colim.map_cone (limit.cone F)),
exact functor.map_iso _ (hc.unique_up_to_iso (limit.is_limit F)),
exact as_iso (colimit_limit_to_limit_colimit_cone F),
end ⟩⟩⟩
variables {C : Type u} [category.{v} C] [concrete_category.{v} C]
section
variables [has_limits_of_shape J C] [has_colimits_of_shape K C]
variables [reflects_limits_of_shape J (forget C)] [preserves_colimits_of_shape K (forget C)]
variables [preserves_limits_of_shape J (forget C)]
noncomputable
instance filtered_colim_preserves_finite_limits :
preserves_limits_of_shape J (colim : (K ⥤ C) ⥤ _) :=
begin
haveI : preserves_limits_of_shape J ((colim : (K ⥤ C) ⥤ _) ⋙ forget C) :=
preserves_limits_of_shape_of_nat_iso (preserves_colimit_nat_iso _).symm,
exactI preserves_limits_of_shape_of_reflects_of_preserves _ (forget C)
end
end
local attribute [instance] reflects_limits_of_shape_of_reflects_isomorphisms
noncomputable
instance [preserves_finite_limits (forget C)] [preserves_filtered_colimits (forget C)]
[has_finite_limits C] [has_colimits_of_shape K C] [reflects_isomorphisms (forget C)] :
preserves_finite_limits (colim : (K ⥤ C) ⥤ _) :=
⟨λ _ _ _, by exactI category_theory.limits.filtered_colim_preserves_finite_limits⟩
section
variables [has_limits_of_shape J C] [has_colimits_of_shape K C]
variables [reflects_limits_of_shape J (forget C)] [preserves_colimits_of_shape K (forget C)]
variables [preserves_limits_of_shape J (forget C)]
/-- A curried version of the fact that filtered colimits commute with finite limits. -/
noncomputable def colimit_limit_iso (F : J ⥤ K ⥤ C) :
colimit (limit F) ≅ limit (colimit F.flip) :=
(is_limit_of_preserves colim (limit.is_limit _)).cone_point_unique_up_to_iso (limit.is_limit _) ≪≫
(has_limit.iso_of_nat_iso (colimit_flip_iso_comp_colim _).symm)
@[simp, reassoc]
lemma ι_colimit_limit_iso_limit_π (F : J ⥤ K ⥤ C) (a) (b) :
colimit.ι (limit F) a ≫ (colimit_limit_iso F).hom ≫ limit.π (colimit F.flip) b =
(limit.π F b).app a ≫ (colimit.ι F.flip a).app b :=
begin
dsimp [colimit_limit_iso],
simp only [functor.map_cone_π_app, iso.symm_hom,
limits.limit.cone_point_unique_up_to_iso_hom_comp_assoc, limits.limit.cone_π,
limits.colimit.ι_map_assoc, limits.colimit_flip_iso_comp_colim_inv_app, assoc,
limits.has_limit.iso_of_nat_iso_hom_π],
congr' 1,
simp only [← category.assoc, iso.comp_inv_eq,
limits.colimit_obj_iso_colimit_comp_evaluation_ι_app_hom,
limits.has_colimit.iso_of_nat_iso_ι_hom, nat_iso.of_components.hom_app],
dsimp,
simp,
end
end
end category_theory.limits
|
ddcd8c54311e89fd351749bc9dedfe24f7c9f6a7 | 2eab05920d6eeb06665e1a6df77b3157354316ad | /src/ring_theory/polynomial/homogeneous.lean | f0f83d99c9e1e7fc2aaf977c7efacc38969bef96 | [
"Apache-2.0"
] | permissive | ayush1801/mathlib | 78949b9f789f488148142221606bf15c02b960d2 | ce164e28f262acbb3de6281b3b03660a9f744e3c | refs/heads/master | 1,692,886,907,941 | 1,635,270,866,000 | 1,635,270,866,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 9,913 | lean | /-
Copyright (c) 2020 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin
-/
import data.mv_polynomial
import algebra.algebra.operations
import data.fintype.card
import algebra.direct_sum.algebra
/-!
# Homogeneous polynomials
A multivariate polynomial `φ` is homogeneous of degree `n`
if all monomials occuring in `φ` have degree `n`.
## Main definitions/lemmas
* `is_homogeneous φ n`: a predicate that asserts that `φ` is homogeneous of degree `n`.
* `homogeneous_submodule σ R n`: the submodule of homogeneous polynomials of degree `n`.
* `homogeneous_component n`: the additive morphism that projects polynomials onto
their summand that is homogeneous of degree `n`.
* `sum_homogeneous_component`: every polynomial is the sum of its homogeneous components
-/
open_locale big_operators
namespace mv_polynomial
variables {σ : Type*} {τ : Type*} {R : Type*} {S : Type*}
/-
TODO
* create definition for `∑ i in d.support, d i`
* show that `mv_polynomial σ R ≃ₐ[R] ⨁ i, homogeneous_submodule σ R i`
-/
/-- A multivariate polynomial `φ` is homogeneous of degree `n`
if all monomials occuring in `φ` have degree `n`. -/
def is_homogeneous [comm_semiring R] (φ : mv_polynomial σ R) (n : ℕ) :=
∀ ⦃d⦄, coeff d φ ≠ 0 → ∑ i in d.support, d i = n
variables (σ R)
/-- The submodule of homogeneous `mv_polynomial`s of degree `n`. -/
noncomputable def homogeneous_submodule [comm_semiring R] (n : ℕ) :
submodule R (mv_polynomial σ R) :=
{ carrier := { x | x.is_homogeneous n },
smul_mem' := λ r a ha c hc, begin
rw coeff_smul at hc,
apply ha,
intro h,
apply hc,
rw h,
exact smul_zero r,
end,
zero_mem' := λ d hd, false.elim (hd $ coeff_zero _),
add_mem' := λ a b ha hb c hc, begin
rw coeff_add at hc,
obtain h|h : coeff c a ≠ 0 ∨ coeff c b ≠ 0,
{ contrapose! hc, simp only [hc, add_zero] },
{ exact ha h },
{ exact hb h }
end }
variables {σ R}
@[simp] lemma mem_homogeneous_submodule [comm_semiring R] (n : ℕ) (p : mv_polynomial σ R) :
p ∈ homogeneous_submodule σ R n ↔ p.is_homogeneous n := iff.rfl
variables (σ R)
/-- While equal, the former has a convenient definitional reduction. -/
lemma homogeneous_submodule_eq_finsupp_supported [comm_semiring R] (n : ℕ) :
homogeneous_submodule σ R n = finsupp.supported _ R {d | ∑ i in d.support, d i = n} :=
begin
ext,
rw [finsupp.mem_supported, set.subset_def],
simp only [finsupp.mem_support_iff, finset.mem_coe],
refl,
end
variables {σ R}
lemma homogeneous_submodule_mul [comm_semiring R] (m n : ℕ) :
homogeneous_submodule σ R m * homogeneous_submodule σ R n ≤ homogeneous_submodule σ R (m + n) :=
begin
rw submodule.mul_le,
intros φ hφ ψ hψ c hc,
rw [coeff_mul] at hc,
obtain ⟨⟨d, e⟩, hde, H⟩ := finset.exists_ne_zero_of_sum_ne_zero hc,
have aux : coeff d φ ≠ 0 ∧ coeff e ψ ≠ 0,
{ contrapose! H,
by_cases h : coeff d φ = 0;
simp only [*, ne.def, not_false_iff, zero_mul, mul_zero] at * },
specialize hφ aux.1, specialize hψ aux.2,
rw finsupp.mem_antidiagonal at hde,
classical,
have hd' : d.support ⊆ d.support ∪ e.support := finset.subset_union_left _ _,
have he' : e.support ⊆ d.support ∪ e.support := finset.subset_union_right _ _,
rw [← hde, ← hφ, ← hψ, finset.sum_subset (finsupp.support_add),
finset.sum_subset hd', finset.sum_subset he', ← finset.sum_add_distrib],
{ congr },
all_goals { intros i hi, apply finsupp.not_mem_support_iff.mp },
end
section
variables [comm_semiring R]
variables {σ R}
lemma is_homogeneous_monomial (d : σ →₀ ℕ) (r : R) (n : ℕ) (hn : ∑ i in d.support, d i = n) :
is_homogeneous (monomial d r) n :=
begin
intros c hc,
classical,
rw coeff_monomial at hc,
split_ifs at hc with h,
{ subst c, exact hn },
{ contradiction }
end
variables (σ) {R}
lemma is_homogeneous_C (r : R) :
is_homogeneous (C r : mv_polynomial σ R) 0 :=
begin
apply is_homogeneous_monomial,
simp only [finsupp.zero_apply, finset.sum_const_zero],
end
variables (σ R)
lemma is_homogeneous_zero (n : ℕ) : is_homogeneous (0 : mv_polynomial σ R) n :=
(homogeneous_submodule σ R n).zero_mem
lemma is_homogeneous_one : is_homogeneous (1 : mv_polynomial σ R) 0 :=
is_homogeneous_C _ _
variables {σ} (R)
lemma is_homogeneous_X (i : σ) :
is_homogeneous (X i : mv_polynomial σ R) 1 :=
begin
apply is_homogeneous_monomial,
simp only [finsupp.support_single_ne_zero one_ne_zero, finset.sum_singleton],
exact finsupp.single_eq_same
end
end
namespace is_homogeneous
variables [comm_semiring R] {φ ψ : mv_polynomial σ R} {m n : ℕ}
lemma coeff_eq_zero (hφ : is_homogeneous φ n) (d : σ →₀ ℕ) (hd : ∑ i in d.support, d i ≠ n) :
coeff d φ = 0 :=
by { have aux := mt (@hφ d) hd, classical, rwa not_not at aux }
lemma inj_right (hm : is_homogeneous φ m) (hn : is_homogeneous φ n) (hφ : φ ≠ 0) :
m = n :=
begin
obtain ⟨d, hd⟩ : ∃ d, coeff d φ ≠ 0 := exists_coeff_ne_zero hφ,
rw [← hm hd, ← hn hd]
end
lemma add (hφ : is_homogeneous φ n) (hψ : is_homogeneous ψ n) :
is_homogeneous (φ + ψ) n :=
(homogeneous_submodule σ R n).add_mem hφ hψ
lemma sum {ι : Type*} (s : finset ι) (φ : ι → mv_polynomial σ R) (n : ℕ)
(h : ∀ i ∈ s, is_homogeneous (φ i) n) :
is_homogeneous (∑ i in s, φ i) n :=
(homogeneous_submodule σ R n).sum_mem h
lemma mul (hφ : is_homogeneous φ m) (hψ : is_homogeneous ψ n) :
is_homogeneous (φ * ψ) (m + n) :=
homogeneous_submodule_mul m n $ submodule.mul_mem_mul hφ hψ
lemma prod {ι : Type*} (s : finset ι) (φ : ι → mv_polynomial σ R) (n : ι → ℕ)
(h : ∀ i ∈ s, is_homogeneous (φ i) (n i)) :
is_homogeneous (∏ i in s, φ i) (∑ i in s, n i) :=
begin
classical,
revert h,
apply finset.induction_on s,
{ intro, simp only [is_homogeneous_one, finset.sum_empty, finset.prod_empty] },
{ intros i s his IH h,
simp only [his, finset.prod_insert, finset.sum_insert, not_false_iff],
apply (h i (finset.mem_insert_self _ _)).mul (IH _),
intros j hjs,
exact h j (finset.mem_insert_of_mem hjs) }
end
lemma total_degree (hφ : is_homogeneous φ n) (h : φ ≠ 0) :
total_degree φ = n :=
begin
rw total_degree,
apply le_antisymm,
{ apply finset.sup_le,
intros d hd,
rw mem_support_iff at hd,
rw [finsupp.sum, hφ hd], },
{ obtain ⟨d, hd⟩ : ∃ d, coeff d φ ≠ 0 := exists_coeff_ne_zero h,
simp only [← hφ hd, finsupp.sum],
replace hd := finsupp.mem_support_iff.mpr hd,
exact finset.le_sup hd, }
end
/--
The homogeneous submodules form a graded ring. This instance is used by `direct_sum.comm_semiring`
and `direct_sum.algebra`. -/
noncomputable instance homogeneous_submodule.gcomm_semiring :
direct_sum.gcomm_semiring (λ i, homogeneous_submodule σ R i) :=
direct_sum.gcomm_semiring.of_submodules _
(is_homogeneous_one σ R)
(λ i j hi hj, is_homogeneous.mul hi.prop hj.prop)
open_locale direct_sum
noncomputable example : comm_semiring (⨁ i, homogeneous_submodule σ R i) := infer_instance
noncomputable example : algebra R (⨁ i, homogeneous_submodule σ R i) := infer_instance
end is_homogeneous
section
noncomputable theory
open_locale classical
open finset
/-- `homogeneous_component n φ` is the part of `φ` that is homogeneous of degree `n`.
See `sum_homogeneous_component` for the statement that `φ` is equal to the sum
of all its homogeneous components. -/
def homogeneous_component [comm_semiring R] (n : ℕ) :
mv_polynomial σ R →ₗ[R] mv_polynomial σ R :=
(submodule.subtype _).comp $ finsupp.restrict_dom _ _ {d | ∑ i in d.support, d i = n}
section homogeneous_component
open finset
variables [comm_semiring R] (n : ℕ) (φ : mv_polynomial σ R)
lemma coeff_homogeneous_component (d : σ →₀ ℕ) :
coeff d (homogeneous_component n φ) = if ∑ i in d.support, d i = n then coeff d φ else 0 :=
by convert finsupp.filter_apply (λ d : σ →₀ ℕ, ∑ i in d.support, d i = n) φ d
lemma homogeneous_component_apply :
homogeneous_component n φ =
∑ d in φ.support.filter (λ d, ∑ i in d.support, d i = n), monomial d (coeff d φ) :=
by convert finsupp.filter_eq_sum (λ d : σ →₀ ℕ, ∑ i in d.support, d i = n) φ
lemma homogeneous_component_is_homogeneous :
(homogeneous_component n φ).is_homogeneous n :=
begin
intros d hd,
contrapose! hd,
rw [coeff_homogeneous_component, if_neg hd]
end
lemma homogeneous_component_zero : homogeneous_component 0 φ = C (coeff 0 φ) :=
begin
ext1 d,
rcases em (d = 0) with (rfl|hd),
{ simp only [coeff_homogeneous_component, sum_eq_zero_iff, finsupp.zero_apply, if_true, coeff_C,
eq_self_iff_true, forall_true_iff] },
{ rw [coeff_homogeneous_component, if_neg, coeff_C, if_neg (ne.symm hd)],
simp only [finsupp.ext_iff, finsupp.zero_apply] at hd,
simp [hd] }
end
lemma homogeneous_component_eq_zero' (h : ∀ d : σ →₀ ℕ, d ∈ φ.support → ∑ i in d.support, d i ≠ n) :
homogeneous_component n φ = 0 :=
begin
rw [homogeneous_component_apply, sum_eq_zero],
intros d hd, rw mem_filter at hd,
exfalso, exact h _ hd.1 hd.2
end
lemma homogeneous_component_eq_zero (h : φ.total_degree < n) :
homogeneous_component n φ = 0 :=
begin
apply homogeneous_component_eq_zero',
rw [total_degree, finset.sup_lt_iff] at h,
{ intros d hd, exact ne_of_lt (h d hd), },
{ exact lt_of_le_of_lt (nat.zero_le _) h, }
end
lemma sum_homogeneous_component :
∑ i in range (φ.total_degree + 1), homogeneous_component i φ = φ :=
begin
ext1 d,
suffices : φ.total_degree < d.support.sum d → 0 = coeff d φ,
by simpa [coeff_sum, coeff_homogeneous_component],
exact λ h, (coeff_eq_zero_of_total_degree_lt h).symm
end
end homogeneous_component
end
end mv_polynomial
|
61b2c2d9cb8f5103b6c8a0d78a48943b5685508b | 82b86ba2ae0d5aed0f01f49c46db5afec0eb2bd7 | /src/Lean/Meta/DiscrTreeTypes.lean | 74a977b6523f964a05b1acd2d753e67bf8ce1a11 | [
"Apache-2.0"
] | permissive | banksonian/lean4 | 3a2e6b0f1eb63aa56ff95b8d07b2f851072d54dc | 78da6b3aa2840693eea354a41e89fc5b212a5011 | refs/heads/master | 1,673,703,624,165 | 1,605,123,551,000 | 1,605,123,551,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,483 | lean | /-
Copyright (c) 2019 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Expr
namespace Lean.Meta
/- See file `DiscrTree.lean` for the actual implementation and documentation. -/
namespace DiscrTree
inductive Key
| const : Name → Nat → Key
| fvar : FVarId → Nat → Key
| lit : Literal → Key
| star : Key
| other : Key
instance : Inhabited Key := ⟨Key.star⟩
protected def Key.hash : Key → USize
| Key.const n a => mixHash 5237 $ mixHash (hash n) (hash a)
| Key.fvar n a => mixHash 3541 $ mixHash (hash n) (hash a)
| Key.lit v => mixHash 1879 $ hash v
| Key.star => 7883
| Key.other => 2411
instance : Hashable Key := ⟨Key.hash⟩
protected def Key.beq : Key → Key → Bool
| Key.const c₁ a₁, Key.const c₂ a₂ => c₁ == c₂ && a₁ == a₂
| Key.fvar c₁ a₁, Key.fvar c₂ a₂ => c₁ == c₂ && a₁ == a₂
| Key.lit v₁, Key.lit v₂ => v₁ == v₂
| Key.star, Key.star => true
| Key.other, Key.other => true
| _, _ => false
instance : BEq Key := ⟨Key.beq⟩
inductive Trie (α : Type)
| node (vs : Array α) (children : Array (Key × Trie α)) : Trie α
end DiscrTree
open DiscrTree
open Std (PersistentHashMap)
structure DiscrTree (α : Type) :=
(root : PersistentHashMap Key (Trie α) := {})
end Lean.Meta
|
d87d7c38302d30c52eba126b168be0a46e94a78f | b7f22e51856f4989b970961f794f1c435f9b8f78 | /tests/lean/run/mult.lean | 23e1544ec38cb9de234ab5cdcf69a0f3748c6d7a | [
"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 | 288 | lean | structure C [class] :=
(val : nat)
attribute C [multiple_instances]
definition c1 [instance] : C := C.mk 1
definition c2 [instance] : C := C.mk 2
set_option trace.class_instances true
definition f [s : C] : nat := C.val
definition tst1 : f = 1 :=
rfl
definition tst2 : f = 2 :=
rfl
|
03070fd69803f7071a4b23dc6adb1fe6c6c1191d | fbef61907e2e2afef7e1e91395505ed27448a945 | /LeanProtocPlugin/Google/Protobuf/Descriptor.lean | 074b0436c848e31dc86b7333d70773e9a1c39a16 | [
"MIT"
] | permissive | yatima-inc/Protoc.lean | 7edecca1f5b33909e6dfef1bcebcbbf9a322ca8c | 3ad7839ec911906e19984e5b60958ee9c866b7ec | refs/heads/master | 1,682,196,995,576 | 1,620,507,180,000 | 1,620,507,180,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 130,535 | lean | -- Generated by the Lean protobuf compiler. Do not edit manually.
-- source: google/protobuf/descriptor.proto
import LeanProto
import Std.Data.AssocList
set_option maxHeartbeats 10000000
set_option maxRecDepth 2048
set_option synthInstance.maxHeartbeats 10000000
set_option genSizeOfSpec false
open Std (AssocList)
namespace LeanProtocPlugin.Google.Protobuf
inductive FieldDescriptorProto_Type where
| TYPE_DOUBLE : FieldDescriptorProto_Type
| TYPE_FLOAT : FieldDescriptorProto_Type
| TYPE_INT64 : FieldDescriptorProto_Type
| TYPE_UINT64 : FieldDescriptorProto_Type
| TYPE_INT32 : FieldDescriptorProto_Type
| TYPE_FIXED64 : FieldDescriptorProto_Type
| TYPE_FIXED32 : FieldDescriptorProto_Type
| TYPE_BOOL : FieldDescriptorProto_Type
| TYPE_STRING : FieldDescriptorProto_Type
| TYPE_GROUP : FieldDescriptorProto_Type
| TYPE_MESSAGE : FieldDescriptorProto_Type
| TYPE_BYTES : FieldDescriptorProto_Type
| TYPE_UINT32 : FieldDescriptorProto_Type
| TYPE_ENUM : FieldDescriptorProto_Type
| TYPE_SFIXED32 : FieldDescriptorProto_Type
| TYPE_SFIXED64 : FieldDescriptorProto_Type
| TYPE_SINT32 : FieldDescriptorProto_Type
| TYPE_SINT64 : FieldDescriptorProto_Type
deriving Repr, Inhabited, BEq
instance : LeanProto.ProtoEnum FieldDescriptorProto_Type where
toInt
| FieldDescriptorProto_Type.TYPE_DOUBLE => 1
| FieldDescriptorProto_Type.TYPE_FLOAT => 2
| FieldDescriptorProto_Type.TYPE_INT64 => 3
| FieldDescriptorProto_Type.TYPE_UINT64 => 4
| FieldDescriptorProto_Type.TYPE_INT32 => 5
| FieldDescriptorProto_Type.TYPE_FIXED64 => 6
| FieldDescriptorProto_Type.TYPE_FIXED32 => 7
| FieldDescriptorProto_Type.TYPE_BOOL => 8
| FieldDescriptorProto_Type.TYPE_STRING => 9
| FieldDescriptorProto_Type.TYPE_GROUP => 10
| FieldDescriptorProto_Type.TYPE_MESSAGE => 11
| FieldDescriptorProto_Type.TYPE_BYTES => 12
| FieldDescriptorProto_Type.TYPE_UINT32 => 13
| FieldDescriptorProto_Type.TYPE_ENUM => 14
| FieldDescriptorProto_Type.TYPE_SFIXED32 => 15
| FieldDescriptorProto_Type.TYPE_SFIXED64 => 16
| FieldDescriptorProto_Type.TYPE_SINT32 => 17
| FieldDescriptorProto_Type.TYPE_SINT64 => 18
ofInt
| 1 => some FieldDescriptorProto_Type.TYPE_DOUBLE
| 2 => some FieldDescriptorProto_Type.TYPE_FLOAT
| 3 => some FieldDescriptorProto_Type.TYPE_INT64
| 4 => some FieldDescriptorProto_Type.TYPE_UINT64
| 5 => some FieldDescriptorProto_Type.TYPE_INT32
| 6 => some FieldDescriptorProto_Type.TYPE_FIXED64
| 7 => some FieldDescriptorProto_Type.TYPE_FIXED32
| 8 => some FieldDescriptorProto_Type.TYPE_BOOL
| 9 => some FieldDescriptorProto_Type.TYPE_STRING
| 10 => some FieldDescriptorProto_Type.TYPE_GROUP
| 11 => some FieldDescriptorProto_Type.TYPE_MESSAGE
| 12 => some FieldDescriptorProto_Type.TYPE_BYTES
| 13 => some FieldDescriptorProto_Type.TYPE_UINT32
| 14 => some FieldDescriptorProto_Type.TYPE_ENUM
| 15 => some FieldDescriptorProto_Type.TYPE_SFIXED32
| 16 => some FieldDescriptorProto_Type.TYPE_SFIXED64
| 17 => some FieldDescriptorProto_Type.TYPE_SINT32
| 18 => some FieldDescriptorProto_Type.TYPE_SINT64
| _ => none
inductive FieldDescriptorProto_Label where
| LABEL_OPTIONAL : FieldDescriptorProto_Label
| LABEL_REQUIRED : FieldDescriptorProto_Label
| LABEL_REPEATED : FieldDescriptorProto_Label
deriving Repr, Inhabited, BEq
instance : LeanProto.ProtoEnum FieldDescriptorProto_Label where
toInt
| FieldDescriptorProto_Label.LABEL_OPTIONAL => 1
| FieldDescriptorProto_Label.LABEL_REQUIRED => 2
| FieldDescriptorProto_Label.LABEL_REPEATED => 3
ofInt
| 1 => some FieldDescriptorProto_Label.LABEL_OPTIONAL
| 2 => some FieldDescriptorProto_Label.LABEL_REQUIRED
| 3 => some FieldDescriptorProto_Label.LABEL_REPEATED
| _ => none
inductive FileOptions_OptimizeMode where
| SPEED : FileOptions_OptimizeMode
| CODE_SIZE : FileOptions_OptimizeMode
| LITE_RUNTIME : FileOptions_OptimizeMode
deriving Repr, Inhabited, BEq
instance : LeanProto.ProtoEnum FileOptions_OptimizeMode where
toInt
| FileOptions_OptimizeMode.SPEED => 1
| FileOptions_OptimizeMode.CODE_SIZE => 2
| FileOptions_OptimizeMode.LITE_RUNTIME => 3
ofInt
| 1 => some FileOptions_OptimizeMode.SPEED
| 2 => some FileOptions_OptimizeMode.CODE_SIZE
| 3 => some FileOptions_OptimizeMode.LITE_RUNTIME
| _ => none
inductive FieldOptions_CType where
| STRING : FieldOptions_CType
| CORD : FieldOptions_CType
| STRING_PIECE : FieldOptions_CType
deriving Repr, Inhabited, BEq
instance : LeanProto.ProtoEnum FieldOptions_CType where
toInt
| FieldOptions_CType.STRING => 0
| FieldOptions_CType.CORD => 1
| FieldOptions_CType.STRING_PIECE => 2
ofInt
| 0 => some FieldOptions_CType.STRING
| 1 => some FieldOptions_CType.CORD
| 2 => some FieldOptions_CType.STRING_PIECE
| _ => none
inductive FieldOptions_JSType where
| JS_NORMAL : FieldOptions_JSType
| JS_STRING : FieldOptions_JSType
| JS_NUMBER : FieldOptions_JSType
deriving Repr, Inhabited, BEq
instance : LeanProto.ProtoEnum FieldOptions_JSType where
toInt
| FieldOptions_JSType.JS_NORMAL => 0
| FieldOptions_JSType.JS_STRING => 1
| FieldOptions_JSType.JS_NUMBER => 2
ofInt
| 0 => some FieldOptions_JSType.JS_NORMAL
| 1 => some FieldOptions_JSType.JS_STRING
| 2 => some FieldOptions_JSType.JS_NUMBER
| _ => none
inductive MethodOptions_IdempotencyLevel where
| IDEMPOTENCY_UNKNOWN : MethodOptions_IdempotencyLevel
| NO_SIDE_EFFECTS : MethodOptions_IdempotencyLevel
| IDEMPOTENT : MethodOptions_IdempotencyLevel
deriving Repr, Inhabited, BEq
instance : LeanProto.ProtoEnum MethodOptions_IdempotencyLevel where
toInt
| MethodOptions_IdempotencyLevel.IDEMPOTENCY_UNKNOWN => 0
| MethodOptions_IdempotencyLevel.NO_SIDE_EFFECTS => 1
| MethodOptions_IdempotencyLevel.IDEMPOTENT => 2
ofInt
| 0 => some MethodOptions_IdempotencyLevel.IDEMPOTENCY_UNKNOWN
| 1 => some MethodOptions_IdempotencyLevel.NO_SIDE_EFFECTS
| 2 => some MethodOptions_IdempotencyLevel.IDEMPOTENT
| _ => none
mutual
-- Starting
-- Starting FileDescriptorSet
inductive FileDescriptorSet where
| mk
(file : (Array (FileDescriptorProto)))
: FileDescriptorSet
-- Starting FileDescriptorProto
inductive FileDescriptorProto where
| mk
(name : (String))
(package : (String))
(dependency : (Array (String)))
(publicDependency : (Array (Int)))
(weakDependency : (Array (Int)))
(messageType : (Array (DescriptorProto)))
(enumType : (Array (EnumDescriptorProto)))
(service : (Array (ServiceDescriptorProto)))
(extension : (Array (FieldDescriptorProto)))
(options : (Option (FileOptions)))
(sourceCodeInfo : (Option (SourceCodeInfo)))
(_syntax : (String))
: FileDescriptorProto
-- Starting DescriptorProto
inductive DescriptorProto where
| mk
(name : (String))
(field : (Array (FieldDescriptorProto)))
(extension : (Array (FieldDescriptorProto)))
(nestedType : (Array (DescriptorProto)))
(enumType : (Array (EnumDescriptorProto)))
(extensionRange : (Array (DescriptorProto_ExtensionRange)))
(oneofDecl : (Array (OneofDescriptorProto)))
(options : (Option (MessageOptions)))
(reservedRange : (Array (DescriptorProto_ReservedRange)))
(reservedName : (Array (String)))
: DescriptorProto
-- Starting DescriptorProto_ExtensionRange
inductive DescriptorProto_ExtensionRange where
| mk
(start : (Int))
(_end : (Int))
(options : (Option (ExtensionRangeOptions)))
: DescriptorProto_ExtensionRange
-- Starting DescriptorProto_ReservedRange
inductive DescriptorProto_ReservedRange where
| mk
(start : (Int))
(_end : (Int))
: DescriptorProto_ReservedRange
-- Starting ExtensionRangeOptions
inductive ExtensionRangeOptions where
| mk
(uninterpretedOption : (Array (UninterpretedOption)))
: ExtensionRangeOptions
-- Starting FieldDescriptorProto
inductive FieldDescriptorProto where
| mk
(name : (String))
(number : (Int))
(label : (FieldDescriptorProto_Label))
(type : (FieldDescriptorProto_Type))
(typeName : (String))
(extendee : (String))
(defaultValue : (String))
(oneofIndex : (Int))
(jsonName : (String))
(options : (Option (FieldOptions)))
(proto3Optional : (Bool))
: FieldDescriptorProto
-- Starting OneofDescriptorProto
inductive OneofDescriptorProto where
| mk
(name : (String))
(options : (Option (OneofOptions)))
: OneofDescriptorProto
-- Starting EnumDescriptorProto
inductive EnumDescriptorProto where
| mk
(name : (String))
(value : (Array (EnumValueDescriptorProto)))
(options : (Option (EnumOptions)))
(reservedRange : (Array (EnumDescriptorProto_EnumReservedRange)))
(reservedName : (Array (String)))
: EnumDescriptorProto
-- Starting EnumDescriptorProto_EnumReservedRange
inductive EnumDescriptorProto_EnumReservedRange where
| mk
(start : (Int))
(_end : (Int))
: EnumDescriptorProto_EnumReservedRange
-- Starting EnumValueDescriptorProto
inductive EnumValueDescriptorProto where
| mk
(name : (String))
(number : (Int))
(options : (Option (EnumValueOptions)))
: EnumValueDescriptorProto
-- Starting ServiceDescriptorProto
inductive ServiceDescriptorProto where
| mk
(name : (String))
(method : (Array (MethodDescriptorProto)))
(options : (Option (ServiceOptions)))
: ServiceDescriptorProto
-- Starting MethodDescriptorProto
inductive MethodDescriptorProto where
| mk
(name : (String))
(inputType : (String))
(outputType : (String))
(options : (Option (MethodOptions)))
(clientStreaming : (Bool))
(serverStreaming : (Bool))
: MethodDescriptorProto
-- Starting FileOptions
inductive FileOptions where
| mk
(javaPackage : (String))
(javaOuterClassname : (String))
(javaMultipleFiles : (Bool))
(javaGenerateEqualsAndHash : (Bool))
(javaStringCheckUtf8 : (Bool))
(optimizeFor : (FileOptions_OptimizeMode))
(goPackage : (String))
(ccGenericServices : (Bool))
(javaGenericServices : (Bool))
(pyGenericServices : (Bool))
(phpGenericServices : (Bool))
(deprecated : (Bool))
(ccEnableArenas : (Bool))
(objcClassPrefix : (String))
(csharpNamespace : (String))
(swiftPrefix : (String))
(phpClassPrefix : (String))
(phpNamespace : (String))
(phpMetadataNamespace : (String))
(rubyPackage : (String))
(uninterpretedOption : (Array (UninterpretedOption)))
: FileOptions
-- Starting MessageOptions
inductive MessageOptions where
| mk
(messageSetWireFormat : (Bool))
(noStandardDescriptorAccessor : (Bool))
(deprecated : (Bool))
(mapEntry : (Bool))
(uninterpretedOption : (Array (UninterpretedOption)))
: MessageOptions
-- Starting FieldOptions
inductive FieldOptions where
| mk
(ctype : (FieldOptions_CType))
(packed : (Bool))
(jstype : (FieldOptions_JSType))
(lazy : (Bool))
(deprecated : (Bool))
(weak : (Bool))
(uninterpretedOption : (Array (UninterpretedOption)))
: FieldOptions
-- Starting OneofOptions
inductive OneofOptions where
| mk
(uninterpretedOption : (Array (UninterpretedOption)))
: OneofOptions
-- Starting EnumOptions
inductive EnumOptions where
| mk
(allowAlias : (Bool))
(deprecated : (Bool))
(uninterpretedOption : (Array (UninterpretedOption)))
: EnumOptions
-- Starting EnumValueOptions
inductive EnumValueOptions where
| mk
(deprecated : (Bool))
(uninterpretedOption : (Array (UninterpretedOption)))
: EnumValueOptions
-- Starting ServiceOptions
inductive ServiceOptions where
| mk
(deprecated : (Bool))
(uninterpretedOption : (Array (UninterpretedOption)))
: ServiceOptions
-- Starting MethodOptions
inductive MethodOptions where
| mk
(deprecated : (Bool))
(idempotencyLevel : (MethodOptions_IdempotencyLevel))
(uninterpretedOption : (Array (UninterpretedOption)))
: MethodOptions
-- Starting UninterpretedOption
inductive UninterpretedOption where
| mk
(name : (Array (UninterpretedOption_NamePart)))
(identifierValue : (String))
(positiveIntValue : (Nat))
(negativeIntValue : (Int))
(doubleValue : (Float))
(stringValue : (ByteArray))
(aggregateValue : (String))
: UninterpretedOption
-- Starting UninterpretedOption_NamePart
inductive UninterpretedOption_NamePart where
| mk
(namePart : (String))
(isExtension : (Bool))
: UninterpretedOption_NamePart
-- Starting SourceCodeInfo
inductive SourceCodeInfo where
| mk
(location : (Array (SourceCodeInfo_Location)))
: SourceCodeInfo
-- Starting SourceCodeInfo_Location
inductive SourceCodeInfo_Location where
| mk
(path : (Array (Int)))
(span : (Array (Int)))
(leadingComments : (String))
(trailingComments : (String))
(leadingDetachedComments : (Array (String)))
: SourceCodeInfo_Location
-- Starting GeneratedCodeInfo
inductive GeneratedCodeInfo where
| mk
(annotation : (Array (GeneratedCodeInfo_Annotation)))
: GeneratedCodeInfo
-- Starting GeneratedCodeInfo_Annotation
inductive GeneratedCodeInfo_Annotation where
| mk
(path : (Array (Int)))
(sourceFile : (String))
(_begin : (Int))
(_end : (Int))
: GeneratedCodeInfo_Annotation
end
def FileDescriptorSet.mkDefault : FileDescriptorSet :=
FileDescriptorSet.mk arbitrary
instance : Inhabited FileDescriptorSet where
default := FileDescriptorSet.mkDefault
def FileDescriptorSet.file : FileDescriptorSet → (Array (FileDescriptorProto))
| mk v0 => v0
def FileDescriptorSet.set_file (orig: FileDescriptorSet) (val: (Array (FileDescriptorProto)))
: FileDescriptorSet := match orig with
| mk v0 => FileDescriptorSet.mk val
def FileDescriptorProto.mkDefault : FileDescriptorProto :=
FileDescriptorProto.mk arbitrary arbitrary arbitrary arbitrary arbitrary arbitrary arbitrary arbitrary arbitrary none none arbitrary
instance : Inhabited FileDescriptorProto where
default := FileDescriptorProto.mkDefault
def FileDescriptorProto.name : FileDescriptorProto → (String)
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 => v0
def FileDescriptorProto.set_name (orig: FileDescriptorProto) (val: (String))
: FileDescriptorProto := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 => FileDescriptorProto.mk val v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11
def FileDescriptorProto.package : FileDescriptorProto → (String)
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 => v1
def FileDescriptorProto.set_package (orig: FileDescriptorProto) (val: (String))
: FileDescriptorProto := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 => FileDescriptorProto.mk v0 val v2 v3 v4 v5 v6 v7 v8 v9 v10 v11
def FileDescriptorProto.dependency : FileDescriptorProto → (Array (String))
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 => v2
def FileDescriptorProto.set_dependency (orig: FileDescriptorProto) (val: (Array (String)))
: FileDescriptorProto := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 => FileDescriptorProto.mk v0 v1 val v3 v4 v5 v6 v7 v8 v9 v10 v11
def FileDescriptorProto.publicDependency : FileDescriptorProto → (Array (Int))
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 => v3
def FileDescriptorProto.set_publicDependency (orig: FileDescriptorProto) (val: (Array (Int)))
: FileDescriptorProto := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 => FileDescriptorProto.mk v0 v1 v2 val v4 v5 v6 v7 v8 v9 v10 v11
def FileDescriptorProto.weakDependency : FileDescriptorProto → (Array (Int))
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 => v4
def FileDescriptorProto.set_weakDependency (orig: FileDescriptorProto) (val: (Array (Int)))
: FileDescriptorProto := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 => FileDescriptorProto.mk v0 v1 v2 v3 val v5 v6 v7 v8 v9 v10 v11
def FileDescriptorProto.messageType : FileDescriptorProto → (Array (DescriptorProto))
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 => v5
def FileDescriptorProto.set_messageType (orig: FileDescriptorProto) (val: (Array (DescriptorProto)))
: FileDescriptorProto := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 => FileDescriptorProto.mk v0 v1 v2 v3 v4 val v6 v7 v8 v9 v10 v11
def FileDescriptorProto.enumType : FileDescriptorProto → (Array (EnumDescriptorProto))
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 => v6
def FileDescriptorProto.set_enumType (orig: FileDescriptorProto) (val: (Array (EnumDescriptorProto)))
: FileDescriptorProto := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 => FileDescriptorProto.mk v0 v1 v2 v3 v4 v5 val v7 v8 v9 v10 v11
def FileDescriptorProto.service : FileDescriptorProto → (Array (ServiceDescriptorProto))
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 => v7
def FileDescriptorProto.set_service (orig: FileDescriptorProto) (val: (Array (ServiceDescriptorProto)))
: FileDescriptorProto := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 => FileDescriptorProto.mk v0 v1 v2 v3 v4 v5 v6 val v8 v9 v10 v11
def FileDescriptorProto.extension : FileDescriptorProto → (Array (FieldDescriptorProto))
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 => v8
def FileDescriptorProto.set_extension (orig: FileDescriptorProto) (val: (Array (FieldDescriptorProto)))
: FileDescriptorProto := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 => FileDescriptorProto.mk v0 v1 v2 v3 v4 v5 v6 v7 val v9 v10 v11
def FileDescriptorProto.options : FileDescriptorProto → (Option (FileOptions))
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 => v9
def FileDescriptorProto.set_options (orig: FileDescriptorProto) (val: (Option (FileOptions)))
: FileDescriptorProto := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 => FileDescriptorProto.mk v0 v1 v2 v3 v4 v5 v6 v7 v8 val v10 v11
def FileDescriptorProto.sourceCodeInfo : FileDescriptorProto → (Option (SourceCodeInfo))
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 => v10
def FileDescriptorProto.set_sourceCodeInfo (orig: FileDescriptorProto) (val: (Option (SourceCodeInfo)))
: FileDescriptorProto := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 => FileDescriptorProto.mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 val v11
def FileDescriptorProto._syntax : FileDescriptorProto → (String)
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 => v11
def FileDescriptorProto.set__syntax (orig: FileDescriptorProto) (val: (String))
: FileDescriptorProto := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 => FileDescriptorProto.mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 val
def DescriptorProto.mkDefault : DescriptorProto :=
DescriptorProto.mk arbitrary arbitrary arbitrary arbitrary arbitrary arbitrary arbitrary none arbitrary arbitrary
instance : Inhabited DescriptorProto where
default := DescriptorProto.mkDefault
def DescriptorProto.name : DescriptorProto → (String)
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 => v0
def DescriptorProto.set_name (orig: DescriptorProto) (val: (String))
: DescriptorProto := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 => DescriptorProto.mk val v1 v2 v3 v4 v5 v6 v7 v8 v9
def DescriptorProto.field : DescriptorProto → (Array (FieldDescriptorProto))
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 => v1
def DescriptorProto.set_field (orig: DescriptorProto) (val: (Array (FieldDescriptorProto)))
: DescriptorProto := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 => DescriptorProto.mk v0 val v2 v3 v4 v5 v6 v7 v8 v9
def DescriptorProto.extension : DescriptorProto → (Array (FieldDescriptorProto))
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 => v2
def DescriptorProto.set_extension (orig: DescriptorProto) (val: (Array (FieldDescriptorProto)))
: DescriptorProto := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 => DescriptorProto.mk v0 v1 val v3 v4 v5 v6 v7 v8 v9
def DescriptorProto.nestedType : DescriptorProto → (Array (DescriptorProto))
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 => v3
def DescriptorProto.set_nestedType (orig: DescriptorProto) (val: (Array (DescriptorProto)))
: DescriptorProto := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 => DescriptorProto.mk v0 v1 v2 val v4 v5 v6 v7 v8 v9
def DescriptorProto.enumType : DescriptorProto → (Array (EnumDescriptorProto))
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 => v4
def DescriptorProto.set_enumType (orig: DescriptorProto) (val: (Array (EnumDescriptorProto)))
: DescriptorProto := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 => DescriptorProto.mk v0 v1 v2 v3 val v5 v6 v7 v8 v9
def DescriptorProto.extensionRange : DescriptorProto → (Array (DescriptorProto_ExtensionRange))
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 => v5
def DescriptorProto.set_extensionRange (orig: DescriptorProto) (val: (Array (DescriptorProto_ExtensionRange)))
: DescriptorProto := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 => DescriptorProto.mk v0 v1 v2 v3 v4 val v6 v7 v8 v9
def DescriptorProto.oneofDecl : DescriptorProto → (Array (OneofDescriptorProto))
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 => v6
def DescriptorProto.set_oneofDecl (orig: DescriptorProto) (val: (Array (OneofDescriptorProto)))
: DescriptorProto := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 => DescriptorProto.mk v0 v1 v2 v3 v4 v5 val v7 v8 v9
def DescriptorProto.options : DescriptorProto → (Option (MessageOptions))
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 => v7
def DescriptorProto.set_options (orig: DescriptorProto) (val: (Option (MessageOptions)))
: DescriptorProto := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 => DescriptorProto.mk v0 v1 v2 v3 v4 v5 v6 val v8 v9
def DescriptorProto.reservedRange : DescriptorProto → (Array (DescriptorProto_ReservedRange))
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 => v8
def DescriptorProto.set_reservedRange (orig: DescriptorProto) (val: (Array (DescriptorProto_ReservedRange)))
: DescriptorProto := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 => DescriptorProto.mk v0 v1 v2 v3 v4 v5 v6 v7 val v9
def DescriptorProto.reservedName : DescriptorProto → (Array (String))
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 => v9
def DescriptorProto.set_reservedName (orig: DescriptorProto) (val: (Array (String)))
: DescriptorProto := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 => DescriptorProto.mk v0 v1 v2 v3 v4 v5 v6 v7 v8 val
def DescriptorProto_ExtensionRange.mkDefault : DescriptorProto_ExtensionRange :=
DescriptorProto_ExtensionRange.mk arbitrary arbitrary none
instance : Inhabited DescriptorProto_ExtensionRange where
default := DescriptorProto_ExtensionRange.mkDefault
def DescriptorProto_ExtensionRange.start : DescriptorProto_ExtensionRange → (Int)
| mk v0 v1 v2 => v0
def DescriptorProto_ExtensionRange.set_start (orig: DescriptorProto_ExtensionRange) (val: (Int))
: DescriptorProto_ExtensionRange := match orig with
| mk v0 v1 v2 => DescriptorProto_ExtensionRange.mk val v1 v2
def DescriptorProto_ExtensionRange._end : DescriptorProto_ExtensionRange → (Int)
| mk v0 v1 v2 => v1
def DescriptorProto_ExtensionRange.set__end (orig: DescriptorProto_ExtensionRange) (val: (Int))
: DescriptorProto_ExtensionRange := match orig with
| mk v0 v1 v2 => DescriptorProto_ExtensionRange.mk v0 val v2
def DescriptorProto_ExtensionRange.options : DescriptorProto_ExtensionRange → (Option (ExtensionRangeOptions))
| mk v0 v1 v2 => v2
def DescriptorProto_ExtensionRange.set_options (orig: DescriptorProto_ExtensionRange) (val: (Option (ExtensionRangeOptions)))
: DescriptorProto_ExtensionRange := match orig with
| mk v0 v1 v2 => DescriptorProto_ExtensionRange.mk v0 v1 val
def DescriptorProto_ReservedRange.mkDefault : DescriptorProto_ReservedRange :=
DescriptorProto_ReservedRange.mk arbitrary arbitrary
instance : Inhabited DescriptorProto_ReservedRange where
default := DescriptorProto_ReservedRange.mkDefault
def DescriptorProto_ReservedRange.start : DescriptorProto_ReservedRange → (Int)
| mk v0 v1 => v0
def DescriptorProto_ReservedRange.set_start (orig: DescriptorProto_ReservedRange) (val: (Int))
: DescriptorProto_ReservedRange := match orig with
| mk v0 v1 => DescriptorProto_ReservedRange.mk val v1
def DescriptorProto_ReservedRange._end : DescriptorProto_ReservedRange → (Int)
| mk v0 v1 => v1
def DescriptorProto_ReservedRange.set__end (orig: DescriptorProto_ReservedRange) (val: (Int))
: DescriptorProto_ReservedRange := match orig with
| mk v0 v1 => DescriptorProto_ReservedRange.mk v0 val
def ExtensionRangeOptions.mkDefault : ExtensionRangeOptions :=
ExtensionRangeOptions.mk arbitrary
instance : Inhabited ExtensionRangeOptions where
default := ExtensionRangeOptions.mkDefault
def ExtensionRangeOptions.uninterpretedOption : ExtensionRangeOptions → (Array (UninterpretedOption))
| mk v0 => v0
def ExtensionRangeOptions.set_uninterpretedOption (orig: ExtensionRangeOptions) (val: (Array (UninterpretedOption)))
: ExtensionRangeOptions := match orig with
| mk v0 => ExtensionRangeOptions.mk val
def FieldDescriptorProto.mkDefault : FieldDescriptorProto :=
FieldDescriptorProto.mk arbitrary arbitrary arbitrary arbitrary arbitrary arbitrary arbitrary (-1) arbitrary none arbitrary
instance : Inhabited FieldDescriptorProto where
default := FieldDescriptorProto.mkDefault
def FieldDescriptorProto.name : FieldDescriptorProto → (String)
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 => v0
def FieldDescriptorProto.set_name (orig: FieldDescriptorProto) (val: (String))
: FieldDescriptorProto := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 => FieldDescriptorProto.mk val v1 v2 v3 v4 v5 v6 v7 v8 v9 v10
def FieldDescriptorProto.number : FieldDescriptorProto → (Int)
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 => v1
def FieldDescriptorProto.set_number (orig: FieldDescriptorProto) (val: (Int))
: FieldDescriptorProto := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 => FieldDescriptorProto.mk v0 val v2 v3 v4 v5 v6 v7 v8 v9 v10
def FieldDescriptorProto.label : FieldDescriptorProto → (FieldDescriptorProto_Label)
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 => v2
def FieldDescriptorProto.set_label (orig: FieldDescriptorProto) (val: (FieldDescriptorProto_Label))
: FieldDescriptorProto := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 => FieldDescriptorProto.mk v0 v1 val v3 v4 v5 v6 v7 v8 v9 v10
def FieldDescriptorProto.type : FieldDescriptorProto → (FieldDescriptorProto_Type)
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 => v3
def FieldDescriptorProto.set_type (orig: FieldDescriptorProto) (val: (FieldDescriptorProto_Type))
: FieldDescriptorProto := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 => FieldDescriptorProto.mk v0 v1 v2 val v4 v5 v6 v7 v8 v9 v10
def FieldDescriptorProto.typeName : FieldDescriptorProto → (String)
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 => v4
def FieldDescriptorProto.set_typeName (orig: FieldDescriptorProto) (val: (String))
: FieldDescriptorProto := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 => FieldDescriptorProto.mk v0 v1 v2 v3 val v5 v6 v7 v8 v9 v10
def FieldDescriptorProto.extendee : FieldDescriptorProto → (String)
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 => v5
def FieldDescriptorProto.set_extendee (orig: FieldDescriptorProto) (val: (String))
: FieldDescriptorProto := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 => FieldDescriptorProto.mk v0 v1 v2 v3 v4 val v6 v7 v8 v9 v10
def FieldDescriptorProto.defaultValue : FieldDescriptorProto → (String)
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 => v6
def FieldDescriptorProto.set_defaultValue (orig: FieldDescriptorProto) (val: (String))
: FieldDescriptorProto := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 => FieldDescriptorProto.mk v0 v1 v2 v3 v4 v5 val v7 v8 v9 v10
def FieldDescriptorProto.oneofIndex : FieldDescriptorProto → (Int)
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 => v7
def FieldDescriptorProto.set_oneofIndex (orig: FieldDescriptorProto) (val: (Int))
: FieldDescriptorProto := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 => FieldDescriptorProto.mk v0 v1 v2 v3 v4 v5 v6 val v8 v9 v10
def FieldDescriptorProto.jsonName : FieldDescriptorProto → (String)
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 => v8
def FieldDescriptorProto.set_jsonName (orig: FieldDescriptorProto) (val: (String))
: FieldDescriptorProto := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 => FieldDescriptorProto.mk v0 v1 v2 v3 v4 v5 v6 v7 val v9 v10
def FieldDescriptorProto.options : FieldDescriptorProto → (Option (FieldOptions))
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 => v9
def FieldDescriptorProto.set_options (orig: FieldDescriptorProto) (val: (Option (FieldOptions)))
: FieldDescriptorProto := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 => FieldDescriptorProto.mk v0 v1 v2 v3 v4 v5 v6 v7 v8 val v10
def FieldDescriptorProto.proto3Optional : FieldDescriptorProto → (Bool)
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 => v10
def FieldDescriptorProto.set_proto3Optional (orig: FieldDescriptorProto) (val: (Bool))
: FieldDescriptorProto := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 => FieldDescriptorProto.mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 val
def OneofDescriptorProto.mkDefault : OneofDescriptorProto :=
OneofDescriptorProto.mk arbitrary none
instance : Inhabited OneofDescriptorProto where
default := OneofDescriptorProto.mkDefault
def OneofDescriptorProto.name : OneofDescriptorProto → (String)
| mk v0 v1 => v0
def OneofDescriptorProto.set_name (orig: OneofDescriptorProto) (val: (String))
: OneofDescriptorProto := match orig with
| mk v0 v1 => OneofDescriptorProto.mk val v1
def OneofDescriptorProto.options : OneofDescriptorProto → (Option (OneofOptions))
| mk v0 v1 => v1
def OneofDescriptorProto.set_options (orig: OneofDescriptorProto) (val: (Option (OneofOptions)))
: OneofDescriptorProto := match orig with
| mk v0 v1 => OneofDescriptorProto.mk v0 val
def EnumDescriptorProto.mkDefault : EnumDescriptorProto :=
EnumDescriptorProto.mk arbitrary arbitrary none arbitrary arbitrary
instance : Inhabited EnumDescriptorProto where
default := EnumDescriptorProto.mkDefault
def EnumDescriptorProto.name : EnumDescriptorProto → (String)
| mk v0 v1 v2 v3 v4 => v0
def EnumDescriptorProto.set_name (orig: EnumDescriptorProto) (val: (String))
: EnumDescriptorProto := match orig with
| mk v0 v1 v2 v3 v4 => EnumDescriptorProto.mk val v1 v2 v3 v4
def EnumDescriptorProto.value : EnumDescriptorProto → (Array (EnumValueDescriptorProto))
| mk v0 v1 v2 v3 v4 => v1
def EnumDescriptorProto.set_value (orig: EnumDescriptorProto) (val: (Array (EnumValueDescriptorProto)))
: EnumDescriptorProto := match orig with
| mk v0 v1 v2 v3 v4 => EnumDescriptorProto.mk v0 val v2 v3 v4
def EnumDescriptorProto.options : EnumDescriptorProto → (Option (EnumOptions))
| mk v0 v1 v2 v3 v4 => v2
def EnumDescriptorProto.set_options (orig: EnumDescriptorProto) (val: (Option (EnumOptions)))
: EnumDescriptorProto := match orig with
| mk v0 v1 v2 v3 v4 => EnumDescriptorProto.mk v0 v1 val v3 v4
def EnumDescriptorProto.reservedRange : EnumDescriptorProto → (Array (EnumDescriptorProto_EnumReservedRange))
| mk v0 v1 v2 v3 v4 => v3
def EnumDescriptorProto.set_reservedRange (orig: EnumDescriptorProto) (val: (Array (EnumDescriptorProto_EnumReservedRange)))
: EnumDescriptorProto := match orig with
| mk v0 v1 v2 v3 v4 => EnumDescriptorProto.mk v0 v1 v2 val v4
def EnumDescriptorProto.reservedName : EnumDescriptorProto → (Array (String))
| mk v0 v1 v2 v3 v4 => v4
def EnumDescriptorProto.set_reservedName (orig: EnumDescriptorProto) (val: (Array (String)))
: EnumDescriptorProto := match orig with
| mk v0 v1 v2 v3 v4 => EnumDescriptorProto.mk v0 v1 v2 v3 val
def EnumDescriptorProto_EnumReservedRange.mkDefault : EnumDescriptorProto_EnumReservedRange :=
EnumDescriptorProto_EnumReservedRange.mk arbitrary arbitrary
instance : Inhabited EnumDescriptorProto_EnumReservedRange where
default := EnumDescriptorProto_EnumReservedRange.mkDefault
def EnumDescriptorProto_EnumReservedRange.start : EnumDescriptorProto_EnumReservedRange → (Int)
| mk v0 v1 => v0
def EnumDescriptorProto_EnumReservedRange.set_start (orig: EnumDescriptorProto_EnumReservedRange) (val: (Int))
: EnumDescriptorProto_EnumReservedRange := match orig with
| mk v0 v1 => EnumDescriptorProto_EnumReservedRange.mk val v1
def EnumDescriptorProto_EnumReservedRange._end : EnumDescriptorProto_EnumReservedRange → (Int)
| mk v0 v1 => v1
def EnumDescriptorProto_EnumReservedRange.set__end (orig: EnumDescriptorProto_EnumReservedRange) (val: (Int))
: EnumDescriptorProto_EnumReservedRange := match orig with
| mk v0 v1 => EnumDescriptorProto_EnumReservedRange.mk v0 val
def EnumValueDescriptorProto.mkDefault : EnumValueDescriptorProto :=
EnumValueDescriptorProto.mk arbitrary arbitrary none
instance : Inhabited EnumValueDescriptorProto where
default := EnumValueDescriptorProto.mkDefault
def EnumValueDescriptorProto.name : EnumValueDescriptorProto → (String)
| mk v0 v1 v2 => v0
def EnumValueDescriptorProto.set_name (orig: EnumValueDescriptorProto) (val: (String))
: EnumValueDescriptorProto := match orig with
| mk v0 v1 v2 => EnumValueDescriptorProto.mk val v1 v2
def EnumValueDescriptorProto.number : EnumValueDescriptorProto → (Int)
| mk v0 v1 v2 => v1
def EnumValueDescriptorProto.set_number (orig: EnumValueDescriptorProto) (val: (Int))
: EnumValueDescriptorProto := match orig with
| mk v0 v1 v2 => EnumValueDescriptorProto.mk v0 val v2
def EnumValueDescriptorProto.options : EnumValueDescriptorProto → (Option (EnumValueOptions))
| mk v0 v1 v2 => v2
def EnumValueDescriptorProto.set_options (orig: EnumValueDescriptorProto) (val: (Option (EnumValueOptions)))
: EnumValueDescriptorProto := match orig with
| mk v0 v1 v2 => EnumValueDescriptorProto.mk v0 v1 val
def ServiceDescriptorProto.mkDefault : ServiceDescriptorProto :=
ServiceDescriptorProto.mk arbitrary arbitrary none
instance : Inhabited ServiceDescriptorProto where
default := ServiceDescriptorProto.mkDefault
def ServiceDescriptorProto.name : ServiceDescriptorProto → (String)
| mk v0 v1 v2 => v0
def ServiceDescriptorProto.set_name (orig: ServiceDescriptorProto) (val: (String))
: ServiceDescriptorProto := match orig with
| mk v0 v1 v2 => ServiceDescriptorProto.mk val v1 v2
def ServiceDescriptorProto.method : ServiceDescriptorProto → (Array (MethodDescriptorProto))
| mk v0 v1 v2 => v1
def ServiceDescriptorProto.set_method (orig: ServiceDescriptorProto) (val: (Array (MethodDescriptorProto)))
: ServiceDescriptorProto := match orig with
| mk v0 v1 v2 => ServiceDescriptorProto.mk v0 val v2
def ServiceDescriptorProto.options : ServiceDescriptorProto → (Option (ServiceOptions))
| mk v0 v1 v2 => v2
def ServiceDescriptorProto.set_options (orig: ServiceDescriptorProto) (val: (Option (ServiceOptions)))
: ServiceDescriptorProto := match orig with
| mk v0 v1 v2 => ServiceDescriptorProto.mk v0 v1 val
def MethodDescriptorProto.mkDefault : MethodDescriptorProto :=
MethodDescriptorProto.mk arbitrary arbitrary arbitrary none (false) (false)
instance : Inhabited MethodDescriptorProto where
default := MethodDescriptorProto.mkDefault
def MethodDescriptorProto.name : MethodDescriptorProto → (String)
| mk v0 v1 v2 v3 v4 v5 => v0
def MethodDescriptorProto.set_name (orig: MethodDescriptorProto) (val: (String))
: MethodDescriptorProto := match orig with
| mk v0 v1 v2 v3 v4 v5 => MethodDescriptorProto.mk val v1 v2 v3 v4 v5
def MethodDescriptorProto.inputType : MethodDescriptorProto → (String)
| mk v0 v1 v2 v3 v4 v5 => v1
def MethodDescriptorProto.set_inputType (orig: MethodDescriptorProto) (val: (String))
: MethodDescriptorProto := match orig with
| mk v0 v1 v2 v3 v4 v5 => MethodDescriptorProto.mk v0 val v2 v3 v4 v5
def MethodDescriptorProto.outputType : MethodDescriptorProto → (String)
| mk v0 v1 v2 v3 v4 v5 => v2
def MethodDescriptorProto.set_outputType (orig: MethodDescriptorProto) (val: (String))
: MethodDescriptorProto := match orig with
| mk v0 v1 v2 v3 v4 v5 => MethodDescriptorProto.mk v0 v1 val v3 v4 v5
def MethodDescriptorProto.options : MethodDescriptorProto → (Option (MethodOptions))
| mk v0 v1 v2 v3 v4 v5 => v3
def MethodDescriptorProto.set_options (orig: MethodDescriptorProto) (val: (Option (MethodOptions)))
: MethodDescriptorProto := match orig with
| mk v0 v1 v2 v3 v4 v5 => MethodDescriptorProto.mk v0 v1 v2 val v4 v5
def MethodDescriptorProto.clientStreaming : MethodDescriptorProto → (Bool)
| mk v0 v1 v2 v3 v4 v5 => v4
def MethodDescriptorProto.set_clientStreaming (orig: MethodDescriptorProto) (val: (Bool))
: MethodDescriptorProto := match orig with
| mk v0 v1 v2 v3 v4 v5 => MethodDescriptorProto.mk v0 v1 v2 v3 val v5
def MethodDescriptorProto.serverStreaming : MethodDescriptorProto → (Bool)
| mk v0 v1 v2 v3 v4 v5 => v5
def MethodDescriptorProto.set_serverStreaming (orig: MethodDescriptorProto) (val: (Bool))
: MethodDescriptorProto := match orig with
| mk v0 v1 v2 v3 v4 v5 => MethodDescriptorProto.mk v0 v1 v2 v3 v4 val
def FileOptions.mkDefault : FileOptions :=
FileOptions.mk arbitrary arbitrary (false) arbitrary (false) FileOptions_OptimizeMode.SPEED arbitrary (false) (false) (false) (false) (false) (true) arbitrary arbitrary arbitrary arbitrary arbitrary arbitrary arbitrary arbitrary
instance : Inhabited FileOptions where
default := FileOptions.mkDefault
def FileOptions.javaPackage : FileOptions → (String)
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 => v0
def FileOptions.set_javaPackage (orig: FileOptions) (val: (String))
: FileOptions := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 => FileOptions.mk val v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20
def FileOptions.javaOuterClassname : FileOptions → (String)
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 => v1
def FileOptions.set_javaOuterClassname (orig: FileOptions) (val: (String))
: FileOptions := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 => FileOptions.mk v0 val v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20
def FileOptions.javaMultipleFiles : FileOptions → (Bool)
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 => v2
def FileOptions.set_javaMultipleFiles (orig: FileOptions) (val: (Bool))
: FileOptions := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 => FileOptions.mk v0 v1 val v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20
def FileOptions.javaGenerateEqualsAndHash : FileOptions → (Bool)
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 => v3
def FileOptions.set_javaGenerateEqualsAndHash (orig: FileOptions) (val: (Bool))
: FileOptions := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 => FileOptions.mk v0 v1 v2 val v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20
def FileOptions.javaStringCheckUtf8 : FileOptions → (Bool)
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 => v4
def FileOptions.set_javaStringCheckUtf8 (orig: FileOptions) (val: (Bool))
: FileOptions := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 => FileOptions.mk v0 v1 v2 v3 val v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20
def FileOptions.optimizeFor : FileOptions → (FileOptions_OptimizeMode)
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 => v5
def FileOptions.set_optimizeFor (orig: FileOptions) (val: (FileOptions_OptimizeMode))
: FileOptions := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 => FileOptions.mk v0 v1 v2 v3 v4 val v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20
def FileOptions.goPackage : FileOptions → (String)
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 => v6
def FileOptions.set_goPackage (orig: FileOptions) (val: (String))
: FileOptions := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 => FileOptions.mk v0 v1 v2 v3 v4 v5 val v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20
def FileOptions.ccGenericServices : FileOptions → (Bool)
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 => v7
def FileOptions.set_ccGenericServices (orig: FileOptions) (val: (Bool))
: FileOptions := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 => FileOptions.mk v0 v1 v2 v3 v4 v5 v6 val v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20
def FileOptions.javaGenericServices : FileOptions → (Bool)
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 => v8
def FileOptions.set_javaGenericServices (orig: FileOptions) (val: (Bool))
: FileOptions := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 => FileOptions.mk v0 v1 v2 v3 v4 v5 v6 v7 val v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20
def FileOptions.pyGenericServices : FileOptions → (Bool)
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 => v9
def FileOptions.set_pyGenericServices (orig: FileOptions) (val: (Bool))
: FileOptions := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 => FileOptions.mk v0 v1 v2 v3 v4 v5 v6 v7 v8 val v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20
def FileOptions.phpGenericServices : FileOptions → (Bool)
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 => v10
def FileOptions.set_phpGenericServices (orig: FileOptions) (val: (Bool))
: FileOptions := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 => FileOptions.mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 val v11 v12 v13 v14 v15 v16 v17 v18 v19 v20
def FileOptions.deprecated : FileOptions → (Bool)
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 => v11
def FileOptions.set_deprecated (orig: FileOptions) (val: (Bool))
: FileOptions := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 => FileOptions.mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 val v12 v13 v14 v15 v16 v17 v18 v19 v20
def FileOptions.ccEnableArenas : FileOptions → (Bool)
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 => v12
def FileOptions.set_ccEnableArenas (orig: FileOptions) (val: (Bool))
: FileOptions := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 => FileOptions.mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 val v13 v14 v15 v16 v17 v18 v19 v20
def FileOptions.objcClassPrefix : FileOptions → (String)
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 => v13
def FileOptions.set_objcClassPrefix (orig: FileOptions) (val: (String))
: FileOptions := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 => FileOptions.mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 val v14 v15 v16 v17 v18 v19 v20
def FileOptions.csharpNamespace : FileOptions → (String)
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 => v14
def FileOptions.set_csharpNamespace (orig: FileOptions) (val: (String))
: FileOptions := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 => FileOptions.mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 val v15 v16 v17 v18 v19 v20
def FileOptions.swiftPrefix : FileOptions → (String)
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 => v15
def FileOptions.set_swiftPrefix (orig: FileOptions) (val: (String))
: FileOptions := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 => FileOptions.mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 val v16 v17 v18 v19 v20
def FileOptions.phpClassPrefix : FileOptions → (String)
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 => v16
def FileOptions.set_phpClassPrefix (orig: FileOptions) (val: (String))
: FileOptions := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 => FileOptions.mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 val v17 v18 v19 v20
def FileOptions.phpNamespace : FileOptions → (String)
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 => v17
def FileOptions.set_phpNamespace (orig: FileOptions) (val: (String))
: FileOptions := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 => FileOptions.mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 val v18 v19 v20
def FileOptions.phpMetadataNamespace : FileOptions → (String)
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 => v18
def FileOptions.set_phpMetadataNamespace (orig: FileOptions) (val: (String))
: FileOptions := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 => FileOptions.mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 val v19 v20
def FileOptions.rubyPackage : FileOptions → (String)
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 => v19
def FileOptions.set_rubyPackage (orig: FileOptions) (val: (String))
: FileOptions := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 => FileOptions.mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 val v20
def FileOptions.uninterpretedOption : FileOptions → (Array (UninterpretedOption))
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 => v20
def FileOptions.set_uninterpretedOption (orig: FileOptions) (val: (Array (UninterpretedOption)))
: FileOptions := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 => FileOptions.mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 val
def MessageOptions.mkDefault : MessageOptions :=
MessageOptions.mk (false) (false) (false) arbitrary arbitrary
instance : Inhabited MessageOptions where
default := MessageOptions.mkDefault
def MessageOptions.messageSetWireFormat : MessageOptions → (Bool)
| mk v0 v1 v2 v3 v4 => v0
def MessageOptions.set_messageSetWireFormat (orig: MessageOptions) (val: (Bool))
: MessageOptions := match orig with
| mk v0 v1 v2 v3 v4 => MessageOptions.mk val v1 v2 v3 v4
def MessageOptions.noStandardDescriptorAccessor : MessageOptions → (Bool)
| mk v0 v1 v2 v3 v4 => v1
def MessageOptions.set_noStandardDescriptorAccessor (orig: MessageOptions) (val: (Bool))
: MessageOptions := match orig with
| mk v0 v1 v2 v3 v4 => MessageOptions.mk v0 val v2 v3 v4
def MessageOptions.deprecated : MessageOptions → (Bool)
| mk v0 v1 v2 v3 v4 => v2
def MessageOptions.set_deprecated (orig: MessageOptions) (val: (Bool))
: MessageOptions := match orig with
| mk v0 v1 v2 v3 v4 => MessageOptions.mk v0 v1 val v3 v4
def MessageOptions.mapEntry : MessageOptions → (Bool)
| mk v0 v1 v2 v3 v4 => v3
def MessageOptions.set_mapEntry (orig: MessageOptions) (val: (Bool))
: MessageOptions := match orig with
| mk v0 v1 v2 v3 v4 => MessageOptions.mk v0 v1 v2 val v4
def MessageOptions.uninterpretedOption : MessageOptions → (Array (UninterpretedOption))
| mk v0 v1 v2 v3 v4 => v4
def MessageOptions.set_uninterpretedOption (orig: MessageOptions) (val: (Array (UninterpretedOption)))
: MessageOptions := match orig with
| mk v0 v1 v2 v3 v4 => MessageOptions.mk v0 v1 v2 v3 val
def FieldOptions.mkDefault : FieldOptions :=
FieldOptions.mk FieldOptions_CType.STRING (true) FieldOptions_JSType.JS_NORMAL (false) (false) (false) arbitrary
instance : Inhabited FieldOptions where
default := FieldOptions.mkDefault
def FieldOptions.ctype : FieldOptions → (FieldOptions_CType)
| mk v0 v1 v2 v3 v4 v5 v6 => v0
def FieldOptions.set_ctype (orig: FieldOptions) (val: (FieldOptions_CType))
: FieldOptions := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 => FieldOptions.mk val v1 v2 v3 v4 v5 v6
def FieldOptions.packed : FieldOptions → (Bool)
| mk v0 v1 v2 v3 v4 v5 v6 => v1
def FieldOptions.set_packed (orig: FieldOptions) (val: (Bool))
: FieldOptions := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 => FieldOptions.mk v0 val v2 v3 v4 v5 v6
def FieldOptions.jstype : FieldOptions → (FieldOptions_JSType)
| mk v0 v1 v2 v3 v4 v5 v6 => v2
def FieldOptions.set_jstype (orig: FieldOptions) (val: (FieldOptions_JSType))
: FieldOptions := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 => FieldOptions.mk v0 v1 val v3 v4 v5 v6
def FieldOptions.lazy : FieldOptions → (Bool)
| mk v0 v1 v2 v3 v4 v5 v6 => v3
def FieldOptions.set_lazy (orig: FieldOptions) (val: (Bool))
: FieldOptions := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 => FieldOptions.mk v0 v1 v2 val v4 v5 v6
def FieldOptions.deprecated : FieldOptions → (Bool)
| mk v0 v1 v2 v3 v4 v5 v6 => v4
def FieldOptions.set_deprecated (orig: FieldOptions) (val: (Bool))
: FieldOptions := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 => FieldOptions.mk v0 v1 v2 v3 val v5 v6
def FieldOptions.weak : FieldOptions → (Bool)
| mk v0 v1 v2 v3 v4 v5 v6 => v5
def FieldOptions.set_weak (orig: FieldOptions) (val: (Bool))
: FieldOptions := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 => FieldOptions.mk v0 v1 v2 v3 v4 val v6
def FieldOptions.uninterpretedOption : FieldOptions → (Array (UninterpretedOption))
| mk v0 v1 v2 v3 v4 v5 v6 => v6
def FieldOptions.set_uninterpretedOption (orig: FieldOptions) (val: (Array (UninterpretedOption)))
: FieldOptions := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 => FieldOptions.mk v0 v1 v2 v3 v4 v5 val
def OneofOptions.mkDefault : OneofOptions :=
OneofOptions.mk arbitrary
instance : Inhabited OneofOptions where
default := OneofOptions.mkDefault
def OneofOptions.uninterpretedOption : OneofOptions → (Array (UninterpretedOption))
| mk v0 => v0
def OneofOptions.set_uninterpretedOption (orig: OneofOptions) (val: (Array (UninterpretedOption)))
: OneofOptions := match orig with
| mk v0 => OneofOptions.mk val
def EnumOptions.mkDefault : EnumOptions :=
EnumOptions.mk arbitrary (false) arbitrary
instance : Inhabited EnumOptions where
default := EnumOptions.mkDefault
def EnumOptions.allowAlias : EnumOptions → (Bool)
| mk v0 v1 v2 => v0
def EnumOptions.set_allowAlias (orig: EnumOptions) (val: (Bool))
: EnumOptions := match orig with
| mk v0 v1 v2 => EnumOptions.mk val v1 v2
def EnumOptions.deprecated : EnumOptions → (Bool)
| mk v0 v1 v2 => v1
def EnumOptions.set_deprecated (orig: EnumOptions) (val: (Bool))
: EnumOptions := match orig with
| mk v0 v1 v2 => EnumOptions.mk v0 val v2
def EnumOptions.uninterpretedOption : EnumOptions → (Array (UninterpretedOption))
| mk v0 v1 v2 => v2
def EnumOptions.set_uninterpretedOption (orig: EnumOptions) (val: (Array (UninterpretedOption)))
: EnumOptions := match orig with
| mk v0 v1 v2 => EnumOptions.mk v0 v1 val
def EnumValueOptions.mkDefault : EnumValueOptions :=
EnumValueOptions.mk (false) arbitrary
instance : Inhabited EnumValueOptions where
default := EnumValueOptions.mkDefault
def EnumValueOptions.deprecated : EnumValueOptions → (Bool)
| mk v0 v1 => v0
def EnumValueOptions.set_deprecated (orig: EnumValueOptions) (val: (Bool))
: EnumValueOptions := match orig with
| mk v0 v1 => EnumValueOptions.mk val v1
def EnumValueOptions.uninterpretedOption : EnumValueOptions → (Array (UninterpretedOption))
| mk v0 v1 => v1
def EnumValueOptions.set_uninterpretedOption (orig: EnumValueOptions) (val: (Array (UninterpretedOption)))
: EnumValueOptions := match orig with
| mk v0 v1 => EnumValueOptions.mk v0 val
def ServiceOptions.mkDefault : ServiceOptions :=
ServiceOptions.mk (false) arbitrary
instance : Inhabited ServiceOptions where
default := ServiceOptions.mkDefault
def ServiceOptions.deprecated : ServiceOptions → (Bool)
| mk v0 v1 => v0
def ServiceOptions.set_deprecated (orig: ServiceOptions) (val: (Bool))
: ServiceOptions := match orig with
| mk v0 v1 => ServiceOptions.mk val v1
def ServiceOptions.uninterpretedOption : ServiceOptions → (Array (UninterpretedOption))
| mk v0 v1 => v1
def ServiceOptions.set_uninterpretedOption (orig: ServiceOptions) (val: (Array (UninterpretedOption)))
: ServiceOptions := match orig with
| mk v0 v1 => ServiceOptions.mk v0 val
def MethodOptions.mkDefault : MethodOptions :=
MethodOptions.mk (false) MethodOptions_IdempotencyLevel.IDEMPOTENCY_UNKNOWN arbitrary
instance : Inhabited MethodOptions where
default := MethodOptions.mkDefault
def MethodOptions.deprecated : MethodOptions → (Bool)
| mk v0 v1 v2 => v0
def MethodOptions.set_deprecated (orig: MethodOptions) (val: (Bool))
: MethodOptions := match orig with
| mk v0 v1 v2 => MethodOptions.mk val v1 v2
def MethodOptions.idempotencyLevel : MethodOptions → (MethodOptions_IdempotencyLevel)
| mk v0 v1 v2 => v1
def MethodOptions.set_idempotencyLevel (orig: MethodOptions) (val: (MethodOptions_IdempotencyLevel))
: MethodOptions := match orig with
| mk v0 v1 v2 => MethodOptions.mk v0 val v2
def MethodOptions.uninterpretedOption : MethodOptions → (Array (UninterpretedOption))
| mk v0 v1 v2 => v2
def MethodOptions.set_uninterpretedOption (orig: MethodOptions) (val: (Array (UninterpretedOption)))
: MethodOptions := match orig with
| mk v0 v1 v2 => MethodOptions.mk v0 v1 val
def UninterpretedOption.mkDefault : UninterpretedOption :=
UninterpretedOption.mk arbitrary arbitrary arbitrary arbitrary arbitrary arbitrary arbitrary
instance : Inhabited UninterpretedOption where
default := UninterpretedOption.mkDefault
def UninterpretedOption.name : UninterpretedOption → (Array (UninterpretedOption_NamePart))
| mk v0 v1 v2 v3 v4 v5 v6 => v0
def UninterpretedOption.set_name (orig: UninterpretedOption) (val: (Array (UninterpretedOption_NamePart)))
: UninterpretedOption := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 => UninterpretedOption.mk val v1 v2 v3 v4 v5 v6
def UninterpretedOption.identifierValue : UninterpretedOption → (String)
| mk v0 v1 v2 v3 v4 v5 v6 => v1
def UninterpretedOption.set_identifierValue (orig: UninterpretedOption) (val: (String))
: UninterpretedOption := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 => UninterpretedOption.mk v0 val v2 v3 v4 v5 v6
def UninterpretedOption.positiveIntValue : UninterpretedOption → (Nat)
| mk v0 v1 v2 v3 v4 v5 v6 => v2
def UninterpretedOption.set_positiveIntValue (orig: UninterpretedOption) (val: (Nat))
: UninterpretedOption := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 => UninterpretedOption.mk v0 v1 val v3 v4 v5 v6
def UninterpretedOption.negativeIntValue : UninterpretedOption → (Int)
| mk v0 v1 v2 v3 v4 v5 v6 => v3
def UninterpretedOption.set_negativeIntValue (orig: UninterpretedOption) (val: (Int))
: UninterpretedOption := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 => UninterpretedOption.mk v0 v1 v2 val v4 v5 v6
def UninterpretedOption.doubleValue : UninterpretedOption → (Float)
| mk v0 v1 v2 v3 v4 v5 v6 => v4
def UninterpretedOption.set_doubleValue (orig: UninterpretedOption) (val: (Float))
: UninterpretedOption := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 => UninterpretedOption.mk v0 v1 v2 v3 val v5 v6
def UninterpretedOption.stringValue : UninterpretedOption → (ByteArray)
| mk v0 v1 v2 v3 v4 v5 v6 => v5
def UninterpretedOption.set_stringValue (orig: UninterpretedOption) (val: (ByteArray))
: UninterpretedOption := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 => UninterpretedOption.mk v0 v1 v2 v3 v4 val v6
def UninterpretedOption.aggregateValue : UninterpretedOption → (String)
| mk v0 v1 v2 v3 v4 v5 v6 => v6
def UninterpretedOption.set_aggregateValue (orig: UninterpretedOption) (val: (String))
: UninterpretedOption := match orig with
| mk v0 v1 v2 v3 v4 v5 v6 => UninterpretedOption.mk v0 v1 v2 v3 v4 v5 val
def UninterpretedOption_NamePart.mkDefault : UninterpretedOption_NamePart :=
UninterpretedOption_NamePart.mk arbitrary arbitrary
instance : Inhabited UninterpretedOption_NamePart where
default := UninterpretedOption_NamePart.mkDefault
def UninterpretedOption_NamePart.namePart : UninterpretedOption_NamePart → (String)
| mk v0 v1 => v0
def UninterpretedOption_NamePart.set_namePart (orig: UninterpretedOption_NamePart) (val: (String))
: UninterpretedOption_NamePart := match orig with
| mk v0 v1 => UninterpretedOption_NamePart.mk val v1
def UninterpretedOption_NamePart.isExtension : UninterpretedOption_NamePart → (Bool)
| mk v0 v1 => v1
def UninterpretedOption_NamePart.set_isExtension (orig: UninterpretedOption_NamePart) (val: (Bool))
: UninterpretedOption_NamePart := match orig with
| mk v0 v1 => UninterpretedOption_NamePart.mk v0 val
def SourceCodeInfo.mkDefault : SourceCodeInfo :=
SourceCodeInfo.mk arbitrary
instance : Inhabited SourceCodeInfo where
default := SourceCodeInfo.mkDefault
def SourceCodeInfo.location : SourceCodeInfo → (Array (SourceCodeInfo_Location))
| mk v0 => v0
def SourceCodeInfo.set_location (orig: SourceCodeInfo) (val: (Array (SourceCodeInfo_Location)))
: SourceCodeInfo := match orig with
| mk v0 => SourceCodeInfo.mk val
def SourceCodeInfo_Location.mkDefault : SourceCodeInfo_Location :=
SourceCodeInfo_Location.mk arbitrary arbitrary arbitrary arbitrary arbitrary
instance : Inhabited SourceCodeInfo_Location where
default := SourceCodeInfo_Location.mkDefault
def SourceCodeInfo_Location.path : SourceCodeInfo_Location → (Array (Int))
| mk v0 v1 v2 v3 v4 => v0
def SourceCodeInfo_Location.set_path (orig: SourceCodeInfo_Location) (val: (Array (Int)))
: SourceCodeInfo_Location := match orig with
| mk v0 v1 v2 v3 v4 => SourceCodeInfo_Location.mk val v1 v2 v3 v4
def SourceCodeInfo_Location.span : SourceCodeInfo_Location → (Array (Int))
| mk v0 v1 v2 v3 v4 => v1
def SourceCodeInfo_Location.set_span (orig: SourceCodeInfo_Location) (val: (Array (Int)))
: SourceCodeInfo_Location := match orig with
| mk v0 v1 v2 v3 v4 => SourceCodeInfo_Location.mk v0 val v2 v3 v4
def SourceCodeInfo_Location.leadingComments : SourceCodeInfo_Location → (String)
| mk v0 v1 v2 v3 v4 => v2
def SourceCodeInfo_Location.set_leadingComments (orig: SourceCodeInfo_Location) (val: (String))
: SourceCodeInfo_Location := match orig with
| mk v0 v1 v2 v3 v4 => SourceCodeInfo_Location.mk v0 v1 val v3 v4
def SourceCodeInfo_Location.trailingComments : SourceCodeInfo_Location → (String)
| mk v0 v1 v2 v3 v4 => v3
def SourceCodeInfo_Location.set_trailingComments (orig: SourceCodeInfo_Location) (val: (String))
: SourceCodeInfo_Location := match orig with
| mk v0 v1 v2 v3 v4 => SourceCodeInfo_Location.mk v0 v1 v2 val v4
def SourceCodeInfo_Location.leadingDetachedComments : SourceCodeInfo_Location → (Array (String))
| mk v0 v1 v2 v3 v4 => v4
def SourceCodeInfo_Location.set_leadingDetachedComments (orig: SourceCodeInfo_Location) (val: (Array (String)))
: SourceCodeInfo_Location := match orig with
| mk v0 v1 v2 v3 v4 => SourceCodeInfo_Location.mk v0 v1 v2 v3 val
def GeneratedCodeInfo.mkDefault : GeneratedCodeInfo :=
GeneratedCodeInfo.mk arbitrary
instance : Inhabited GeneratedCodeInfo where
default := GeneratedCodeInfo.mkDefault
def GeneratedCodeInfo.annotation : GeneratedCodeInfo → (Array (GeneratedCodeInfo_Annotation))
| mk v0 => v0
def GeneratedCodeInfo.set_annotation (orig: GeneratedCodeInfo) (val: (Array (GeneratedCodeInfo_Annotation)))
: GeneratedCodeInfo := match orig with
| mk v0 => GeneratedCodeInfo.mk val
def GeneratedCodeInfo_Annotation.mkDefault : GeneratedCodeInfo_Annotation :=
GeneratedCodeInfo_Annotation.mk arbitrary arbitrary arbitrary arbitrary
instance : Inhabited GeneratedCodeInfo_Annotation where
default := GeneratedCodeInfo_Annotation.mkDefault
def GeneratedCodeInfo_Annotation.path : GeneratedCodeInfo_Annotation → (Array (Int))
| mk v0 v1 v2 v3 => v0
def GeneratedCodeInfo_Annotation.set_path (orig: GeneratedCodeInfo_Annotation) (val: (Array (Int)))
: GeneratedCodeInfo_Annotation := match orig with
| mk v0 v1 v2 v3 => GeneratedCodeInfo_Annotation.mk val v1 v2 v3
def GeneratedCodeInfo_Annotation.sourceFile : GeneratedCodeInfo_Annotation → (String)
| mk v0 v1 v2 v3 => v1
def GeneratedCodeInfo_Annotation.set_sourceFile (orig: GeneratedCodeInfo_Annotation) (val: (String))
: GeneratedCodeInfo_Annotation := match orig with
| mk v0 v1 v2 v3 => GeneratedCodeInfo_Annotation.mk v0 val v2 v3
def GeneratedCodeInfo_Annotation._begin : GeneratedCodeInfo_Annotation → (Int)
| mk v0 v1 v2 v3 => v2
def GeneratedCodeInfo_Annotation.set__begin (orig: GeneratedCodeInfo_Annotation) (val: (Int))
: GeneratedCodeInfo_Annotation := match orig with
| mk v0 v1 v2 v3 => GeneratedCodeInfo_Annotation.mk v0 v1 val v3
def GeneratedCodeInfo_Annotation._end : GeneratedCodeInfo_Annotation → (Int)
| mk v0 v1 v2 v3 => v3
def GeneratedCodeInfo_Annotation.set__end (orig: GeneratedCodeInfo_Annotation) (val: (Int))
: GeneratedCodeInfo_Annotation := match orig with
| mk v0 v1 v2 v3 => GeneratedCodeInfo_Annotation.mk v0 v1 v2 val
deriving instance BEq for GeneratedCodeInfo_Annotation, GeneratedCodeInfo, SourceCodeInfo_Location, SourceCodeInfo, UninterpretedOption_NamePart, UninterpretedOption, MethodOptions, ServiceOptions, EnumValueOptions, EnumOptions, OneofOptions, FieldOptions, MessageOptions, FileOptions, MethodDescriptorProto, ServiceDescriptorProto, EnumValueDescriptorProto, EnumDescriptorProto_EnumReservedRange, EnumDescriptorProto, OneofDescriptorProto, FieldDescriptorProto, ExtensionRangeOptions, DescriptorProto_ReservedRange, DescriptorProto_ExtensionRange, DescriptorProto, FileDescriptorProto, FileDescriptorSet
mutual
partial def FileDescriptorSet_deserializeAux (x: FileDescriptorSet) : LeanProto.EncDec.ProtoParseM FileDescriptorSet := do
if (← LeanProto.EncDec.done) then return x
let (_type, key) ← LeanProto.EncDec.parseKey
match key with
| 1 => do FileDescriptorSet_deserializeAux (x.set_file (← (LeanProto.EncDec.parseKeyAndNonPackedArray ((fun v => LeanProto.EncDec.parseMessage (FileDescriptorProto_deserializeAux v)) arbitrary) _type) (x.file)))
| 0 => do throw $ IO.userError "Decoding message with field number 0"
| _ => do let _ ← LeanProto.EncDec.parseUnknown _type; FileDescriptorSet_deserializeAux x
partial def FileDescriptorProto_deserializeAux (x: FileDescriptorProto) : LeanProto.EncDec.ProtoParseM FileDescriptorProto := do
if (← LeanProto.EncDec.done) then return x
let (_type, key) ← LeanProto.EncDec.parseKey
match key with
| 1 => do FileDescriptorProto_deserializeAux (x.set_name (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseString) (x.name)))
| 2 => do FileDescriptorProto_deserializeAux (x.set_package (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseString) (x.package)))
| 3 => do FileDescriptorProto_deserializeAux (x.set_dependency (← (LeanProto.EncDec.parseKeyAndNonPackedArray ((LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseString) arbitrary) _type) (x.dependency)))
| 10 => do FileDescriptorProto_deserializeAux (x.set_publicDependency (← (LeanProto.EncDec.parseKeyAndMixedArray ((LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseInt32AsInt) arbitrary) _type) (x.publicDependency)))
| 11 => do FileDescriptorProto_deserializeAux (x.set_weakDependency (← (LeanProto.EncDec.parseKeyAndMixedArray ((LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseInt32AsInt) arbitrary) _type) (x.weakDependency)))
| 4 => do FileDescriptorProto_deserializeAux (x.set_messageType (← (LeanProto.EncDec.parseKeyAndNonPackedArray ((fun v => LeanProto.EncDec.parseMessage (DescriptorProto_deserializeAux v)) arbitrary) _type) (x.messageType)))
| 5 => do FileDescriptorProto_deserializeAux (x.set_enumType (← (LeanProto.EncDec.parseKeyAndNonPackedArray ((fun v => LeanProto.EncDec.parseMessage (EnumDescriptorProto_deserializeAux v)) arbitrary) _type) (x.enumType)))
| 6 => do FileDescriptorProto_deserializeAux (x.set_service (← (LeanProto.EncDec.parseKeyAndNonPackedArray ((fun v => LeanProto.EncDec.parseMessage (ServiceDescriptorProto_deserializeAux v)) arbitrary) _type) (x.service)))
| 7 => do FileDescriptorProto_deserializeAux (x.set_extension (← (LeanProto.EncDec.parseKeyAndNonPackedArray ((fun v => LeanProto.EncDec.parseMessage (FieldDescriptorProto_deserializeAux v)) arbitrary) _type) (x.extension)))
| 8 => do FileDescriptorProto_deserializeAux (x.set_options (← (fun v => LeanProto.EncDec.parseMessage (FileOptions_deserializeAux v)) (x.options.getD arbitrary)))
| 9 => do FileDescriptorProto_deserializeAux (x.set_sourceCodeInfo (← (fun v => LeanProto.EncDec.parseMessage (SourceCodeInfo_deserializeAux v)) (x.sourceCodeInfo.getD arbitrary)))
| 12 => do FileDescriptorProto_deserializeAux (x.set__syntax (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseString) (x._syntax)))
| 0 => do throw $ IO.userError "Decoding message with field number 0"
| _ => do let _ ← LeanProto.EncDec.parseUnknown _type; FileDescriptorProto_deserializeAux x
partial def DescriptorProto_deserializeAux (x: DescriptorProto) : LeanProto.EncDec.ProtoParseM DescriptorProto := do
if (← LeanProto.EncDec.done) then return x
let (_type, key) ← LeanProto.EncDec.parseKey
match key with
| 1 => do DescriptorProto_deserializeAux (x.set_name (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseString) (x.name)))
| 2 => do DescriptorProto_deserializeAux (x.set_field (← (LeanProto.EncDec.parseKeyAndNonPackedArray ((fun v => LeanProto.EncDec.parseMessage (FieldDescriptorProto_deserializeAux v)) arbitrary) _type) (x.field)))
| 6 => do DescriptorProto_deserializeAux (x.set_extension (← (LeanProto.EncDec.parseKeyAndNonPackedArray ((fun v => LeanProto.EncDec.parseMessage (FieldDescriptorProto_deserializeAux v)) arbitrary) _type) (x.extension)))
| 3 => do DescriptorProto_deserializeAux (x.set_nestedType (← (LeanProto.EncDec.parseKeyAndNonPackedArray ((fun v => LeanProto.EncDec.parseMessage (DescriptorProto_deserializeAux v)) arbitrary) _type) (x.nestedType)))
| 4 => do DescriptorProto_deserializeAux (x.set_enumType (← (LeanProto.EncDec.parseKeyAndNonPackedArray ((fun v => LeanProto.EncDec.parseMessage (EnumDescriptorProto_deserializeAux v)) arbitrary) _type) (x.enumType)))
| 5 => do DescriptorProto_deserializeAux (x.set_extensionRange (← (LeanProto.EncDec.parseKeyAndNonPackedArray ((fun v => LeanProto.EncDec.parseMessage (DescriptorProto_ExtensionRange_deserializeAux v)) arbitrary) _type) (x.extensionRange)))
| 8 => do DescriptorProto_deserializeAux (x.set_oneofDecl (← (LeanProto.EncDec.parseKeyAndNonPackedArray ((fun v => LeanProto.EncDec.parseMessage (OneofDescriptorProto_deserializeAux v)) arbitrary) _type) (x.oneofDecl)))
| 7 => do DescriptorProto_deserializeAux (x.set_options (← (fun v => LeanProto.EncDec.parseMessage (MessageOptions_deserializeAux v)) (x.options.getD arbitrary)))
| 9 => do DescriptorProto_deserializeAux (x.set_reservedRange (← (LeanProto.EncDec.parseKeyAndNonPackedArray ((fun v => LeanProto.EncDec.parseMessage (DescriptorProto_ReservedRange_deserializeAux v)) arbitrary) _type) (x.reservedRange)))
| 10 => do DescriptorProto_deserializeAux (x.set_reservedName (← (LeanProto.EncDec.parseKeyAndNonPackedArray ((LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseString) arbitrary) _type) (x.reservedName)))
| 0 => do throw $ IO.userError "Decoding message with field number 0"
| _ => do let _ ← LeanProto.EncDec.parseUnknown _type; DescriptorProto_deserializeAux x
partial def DescriptorProto_ExtensionRange_deserializeAux (x: DescriptorProto_ExtensionRange) : LeanProto.EncDec.ProtoParseM DescriptorProto_ExtensionRange := do
if (← LeanProto.EncDec.done) then return x
let (_type, key) ← LeanProto.EncDec.parseKey
match key with
| 1 => do DescriptorProto_ExtensionRange_deserializeAux (x.set_start (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseInt32AsInt) (x.start)))
| 2 => do DescriptorProto_ExtensionRange_deserializeAux (x.set__end (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseInt32AsInt) (x._end)))
| 3 => do DescriptorProto_ExtensionRange_deserializeAux (x.set_options (← (fun v => LeanProto.EncDec.parseMessage (ExtensionRangeOptions_deserializeAux v)) (x.options.getD arbitrary)))
| 0 => do throw $ IO.userError "Decoding message with field number 0"
| _ => do let _ ← LeanProto.EncDec.parseUnknown _type; DescriptorProto_ExtensionRange_deserializeAux x
partial def DescriptorProto_ReservedRange_deserializeAux (x: DescriptorProto_ReservedRange) : LeanProto.EncDec.ProtoParseM DescriptorProto_ReservedRange := do
if (← LeanProto.EncDec.done) then return x
let (_type, key) ← LeanProto.EncDec.parseKey
match key with
| 1 => do DescriptorProto_ReservedRange_deserializeAux (x.set_start (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseInt32AsInt) (x.start)))
| 2 => do DescriptorProto_ReservedRange_deserializeAux (x.set__end (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseInt32AsInt) (x._end)))
| 0 => do throw $ IO.userError "Decoding message with field number 0"
| _ => do let _ ← LeanProto.EncDec.parseUnknown _type; DescriptorProto_ReservedRange_deserializeAux x
partial def ExtensionRangeOptions_deserializeAux (x: ExtensionRangeOptions) : LeanProto.EncDec.ProtoParseM ExtensionRangeOptions := do
if (← LeanProto.EncDec.done) then return x
let (_type, key) ← LeanProto.EncDec.parseKey
match key with
| 999 => do ExtensionRangeOptions_deserializeAux (x.set_uninterpretedOption (← (LeanProto.EncDec.parseKeyAndNonPackedArray ((fun v => LeanProto.EncDec.parseMessage (UninterpretedOption_deserializeAux v)) arbitrary) _type) (x.uninterpretedOption)))
| 0 => do throw $ IO.userError "Decoding message with field number 0"
| _ => do let _ ← LeanProto.EncDec.parseUnknown _type; ExtensionRangeOptions_deserializeAux x
partial def FieldDescriptorProto_deserializeAux (x: FieldDescriptorProto) : LeanProto.EncDec.ProtoParseM FieldDescriptorProto := do
if (← LeanProto.EncDec.done) then return x
let (_type, key) ← LeanProto.EncDec.parseKey
match key with
| 1 => do FieldDescriptorProto_deserializeAux (x.set_name (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseString) (x.name)))
| 3 => do FieldDescriptorProto_deserializeAux (x.set_number (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseInt32AsInt) (x.number)))
| 4 => do FieldDescriptorProto_deserializeAux (x.set_label (← (LeanProto.EncDec.withIgnoredState (LeanProto.EncDec.parseEnum (α:=FieldDescriptorProto_Label))) (x.label)))
| 5 => do FieldDescriptorProto_deserializeAux (x.set_type (← (LeanProto.EncDec.withIgnoredState (LeanProto.EncDec.parseEnum (α:=FieldDescriptorProto_Type))) (x.type)))
| 6 => do FieldDescriptorProto_deserializeAux (x.set_typeName (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseString) (x.typeName)))
| 2 => do FieldDescriptorProto_deserializeAux (x.set_extendee (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseString) (x.extendee)))
| 7 => do FieldDescriptorProto_deserializeAux (x.set_defaultValue (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseString) (x.defaultValue)))
| 9 => do FieldDescriptorProto_deserializeAux (x.set_oneofIndex (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseInt32AsInt) (x.oneofIndex)))
| 10 => do FieldDescriptorProto_deserializeAux (x.set_jsonName (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseString) (x.jsonName)))
| 8 => do FieldDescriptorProto_deserializeAux (x.set_options (← (fun v => LeanProto.EncDec.parseMessage (FieldOptions_deserializeAux v)) (x.options.getD arbitrary)))
| 17 => do FieldDescriptorProto_deserializeAux (x.set_proto3Optional (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseBool) (x.proto3Optional)))
| 0 => do throw $ IO.userError "Decoding message with field number 0"
| _ => do let _ ← LeanProto.EncDec.parseUnknown _type; FieldDescriptorProto_deserializeAux x
partial def OneofDescriptorProto_deserializeAux (x: OneofDescriptorProto) : LeanProto.EncDec.ProtoParseM OneofDescriptorProto := do
if (← LeanProto.EncDec.done) then return x
let (_type, key) ← LeanProto.EncDec.parseKey
match key with
| 1 => do OneofDescriptorProto_deserializeAux (x.set_name (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseString) (x.name)))
| 2 => do OneofDescriptorProto_deserializeAux (x.set_options (← (fun v => LeanProto.EncDec.parseMessage (OneofOptions_deserializeAux v)) (x.options.getD arbitrary)))
| 0 => do throw $ IO.userError "Decoding message with field number 0"
| _ => do let _ ← LeanProto.EncDec.parseUnknown _type; OneofDescriptorProto_deserializeAux x
partial def EnumDescriptorProto_deserializeAux (x: EnumDescriptorProto) : LeanProto.EncDec.ProtoParseM EnumDescriptorProto := do
if (← LeanProto.EncDec.done) then return x
let (_type, key) ← LeanProto.EncDec.parseKey
match key with
| 1 => do EnumDescriptorProto_deserializeAux (x.set_name (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseString) (x.name)))
| 2 => do EnumDescriptorProto_deserializeAux (x.set_value (← (LeanProto.EncDec.parseKeyAndNonPackedArray ((fun v => LeanProto.EncDec.parseMessage (EnumValueDescriptorProto_deserializeAux v)) arbitrary) _type) (x.value)))
| 3 => do EnumDescriptorProto_deserializeAux (x.set_options (← (fun v => LeanProto.EncDec.parseMessage (EnumOptions_deserializeAux v)) (x.options.getD arbitrary)))
| 4 => do EnumDescriptorProto_deserializeAux (x.set_reservedRange (← (LeanProto.EncDec.parseKeyAndNonPackedArray ((fun v => LeanProto.EncDec.parseMessage (EnumDescriptorProto_EnumReservedRange_deserializeAux v)) arbitrary) _type) (x.reservedRange)))
| 5 => do EnumDescriptorProto_deserializeAux (x.set_reservedName (← (LeanProto.EncDec.parseKeyAndNonPackedArray ((LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseString) arbitrary) _type) (x.reservedName)))
| 0 => do throw $ IO.userError "Decoding message with field number 0"
| _ => do let _ ← LeanProto.EncDec.parseUnknown _type; EnumDescriptorProto_deserializeAux x
partial def EnumDescriptorProto_EnumReservedRange_deserializeAux (x: EnumDescriptorProto_EnumReservedRange) : LeanProto.EncDec.ProtoParseM EnumDescriptorProto_EnumReservedRange := do
if (← LeanProto.EncDec.done) then return x
let (_type, key) ← LeanProto.EncDec.parseKey
match key with
| 1 => do EnumDescriptorProto_EnumReservedRange_deserializeAux (x.set_start (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseInt32AsInt) (x.start)))
| 2 => do EnumDescriptorProto_EnumReservedRange_deserializeAux (x.set__end (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseInt32AsInt) (x._end)))
| 0 => do throw $ IO.userError "Decoding message with field number 0"
| _ => do let _ ← LeanProto.EncDec.parseUnknown _type; EnumDescriptorProto_EnumReservedRange_deserializeAux x
partial def EnumValueDescriptorProto_deserializeAux (x: EnumValueDescriptorProto) : LeanProto.EncDec.ProtoParseM EnumValueDescriptorProto := do
if (← LeanProto.EncDec.done) then return x
let (_type, key) ← LeanProto.EncDec.parseKey
match key with
| 1 => do EnumValueDescriptorProto_deserializeAux (x.set_name (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseString) (x.name)))
| 2 => do EnumValueDescriptorProto_deserializeAux (x.set_number (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseInt32AsInt) (x.number)))
| 3 => do EnumValueDescriptorProto_deserializeAux (x.set_options (← (fun v => LeanProto.EncDec.parseMessage (EnumValueOptions_deserializeAux v)) (x.options.getD arbitrary)))
| 0 => do throw $ IO.userError "Decoding message with field number 0"
| _ => do let _ ← LeanProto.EncDec.parseUnknown _type; EnumValueDescriptorProto_deserializeAux x
partial def ServiceDescriptorProto_deserializeAux (x: ServiceDescriptorProto) : LeanProto.EncDec.ProtoParseM ServiceDescriptorProto := do
if (← LeanProto.EncDec.done) then return x
let (_type, key) ← LeanProto.EncDec.parseKey
match key with
| 1 => do ServiceDescriptorProto_deserializeAux (x.set_name (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseString) (x.name)))
| 2 => do ServiceDescriptorProto_deserializeAux (x.set_method (← (LeanProto.EncDec.parseKeyAndNonPackedArray ((fun v => LeanProto.EncDec.parseMessage (MethodDescriptorProto_deserializeAux v)) arbitrary) _type) (x.method)))
| 3 => do ServiceDescriptorProto_deserializeAux (x.set_options (← (fun v => LeanProto.EncDec.parseMessage (ServiceOptions_deserializeAux v)) (x.options.getD arbitrary)))
| 0 => do throw $ IO.userError "Decoding message with field number 0"
| _ => do let _ ← LeanProto.EncDec.parseUnknown _type; ServiceDescriptorProto_deserializeAux x
partial def MethodDescriptorProto_deserializeAux (x: MethodDescriptorProto) : LeanProto.EncDec.ProtoParseM MethodDescriptorProto := do
if (← LeanProto.EncDec.done) then return x
let (_type, key) ← LeanProto.EncDec.parseKey
match key with
| 1 => do MethodDescriptorProto_deserializeAux (x.set_name (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseString) (x.name)))
| 2 => do MethodDescriptorProto_deserializeAux (x.set_inputType (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseString) (x.inputType)))
| 3 => do MethodDescriptorProto_deserializeAux (x.set_outputType (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseString) (x.outputType)))
| 4 => do MethodDescriptorProto_deserializeAux (x.set_options (← (fun v => LeanProto.EncDec.parseMessage (MethodOptions_deserializeAux v)) (x.options.getD arbitrary)))
| 5 => do MethodDescriptorProto_deserializeAux (x.set_clientStreaming (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseBool) (x.clientStreaming)))
| 6 => do MethodDescriptorProto_deserializeAux (x.set_serverStreaming (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseBool) (x.serverStreaming)))
| 0 => do throw $ IO.userError "Decoding message with field number 0"
| _ => do let _ ← LeanProto.EncDec.parseUnknown _type; MethodDescriptorProto_deserializeAux x
partial def FileOptions_deserializeAux (x: FileOptions) : LeanProto.EncDec.ProtoParseM FileOptions := do
if (← LeanProto.EncDec.done) then return x
let (_type, key) ← LeanProto.EncDec.parseKey
match key with
| 1 => do FileOptions_deserializeAux (x.set_javaPackage (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseString) (x.javaPackage)))
| 8 => do FileOptions_deserializeAux (x.set_javaOuterClassname (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseString) (x.javaOuterClassname)))
| 10 => do FileOptions_deserializeAux (x.set_javaMultipleFiles (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseBool) (x.javaMultipleFiles)))
| 20 => do FileOptions_deserializeAux (x.set_javaGenerateEqualsAndHash (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseBool) (x.javaGenerateEqualsAndHash)))
| 27 => do FileOptions_deserializeAux (x.set_javaStringCheckUtf8 (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseBool) (x.javaStringCheckUtf8)))
| 9 => do FileOptions_deserializeAux (x.set_optimizeFor (← (LeanProto.EncDec.withIgnoredState (LeanProto.EncDec.parseEnum (α:=FileOptions_OptimizeMode))) (x.optimizeFor)))
| 11 => do FileOptions_deserializeAux (x.set_goPackage (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseString) (x.goPackage)))
| 16 => do FileOptions_deserializeAux (x.set_ccGenericServices (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseBool) (x.ccGenericServices)))
| 17 => do FileOptions_deserializeAux (x.set_javaGenericServices (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseBool) (x.javaGenericServices)))
| 18 => do FileOptions_deserializeAux (x.set_pyGenericServices (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseBool) (x.pyGenericServices)))
| 42 => do FileOptions_deserializeAux (x.set_phpGenericServices (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseBool) (x.phpGenericServices)))
| 23 => do FileOptions_deserializeAux (x.set_deprecated (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseBool) (x.deprecated)))
| 31 => do FileOptions_deserializeAux (x.set_ccEnableArenas (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseBool) (x.ccEnableArenas)))
| 36 => do FileOptions_deserializeAux (x.set_objcClassPrefix (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseString) (x.objcClassPrefix)))
| 37 => do FileOptions_deserializeAux (x.set_csharpNamespace (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseString) (x.csharpNamespace)))
| 39 => do FileOptions_deserializeAux (x.set_swiftPrefix (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseString) (x.swiftPrefix)))
| 40 => do FileOptions_deserializeAux (x.set_phpClassPrefix (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseString) (x.phpClassPrefix)))
| 41 => do FileOptions_deserializeAux (x.set_phpNamespace (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseString) (x.phpNamespace)))
| 44 => do FileOptions_deserializeAux (x.set_phpMetadataNamespace (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseString) (x.phpMetadataNamespace)))
| 45 => do FileOptions_deserializeAux (x.set_rubyPackage (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseString) (x.rubyPackage)))
| 999 => do FileOptions_deserializeAux (x.set_uninterpretedOption (← (LeanProto.EncDec.parseKeyAndNonPackedArray ((fun v => LeanProto.EncDec.parseMessage (UninterpretedOption_deserializeAux v)) arbitrary) _type) (x.uninterpretedOption)))
| 0 => do throw $ IO.userError "Decoding message with field number 0"
| _ => do let _ ← LeanProto.EncDec.parseUnknown _type; FileOptions_deserializeAux x
partial def MessageOptions_deserializeAux (x: MessageOptions) : LeanProto.EncDec.ProtoParseM MessageOptions := do
if (← LeanProto.EncDec.done) then return x
let (_type, key) ← LeanProto.EncDec.parseKey
match key with
| 1 => do MessageOptions_deserializeAux (x.set_messageSetWireFormat (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseBool) (x.messageSetWireFormat)))
| 2 => do MessageOptions_deserializeAux (x.set_noStandardDescriptorAccessor (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseBool) (x.noStandardDescriptorAccessor)))
| 3 => do MessageOptions_deserializeAux (x.set_deprecated (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseBool) (x.deprecated)))
| 7 => do MessageOptions_deserializeAux (x.set_mapEntry (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseBool) (x.mapEntry)))
| 999 => do MessageOptions_deserializeAux (x.set_uninterpretedOption (← (LeanProto.EncDec.parseKeyAndNonPackedArray ((fun v => LeanProto.EncDec.parseMessage (UninterpretedOption_deserializeAux v)) arbitrary) _type) (x.uninterpretedOption)))
| 0 => do throw $ IO.userError "Decoding message with field number 0"
| _ => do let _ ← LeanProto.EncDec.parseUnknown _type; MessageOptions_deserializeAux x
partial def FieldOptions_deserializeAux (x: FieldOptions) : LeanProto.EncDec.ProtoParseM FieldOptions := do
if (← LeanProto.EncDec.done) then return x
let (_type, key) ← LeanProto.EncDec.parseKey
match key with
| 1 => do FieldOptions_deserializeAux (x.set_ctype (← (LeanProto.EncDec.withIgnoredState (LeanProto.EncDec.parseEnum (α:=FieldOptions_CType))) (x.ctype)))
| 2 => do FieldOptions_deserializeAux (x.set_packed (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseBool) (x.packed)))
| 6 => do FieldOptions_deserializeAux (x.set_jstype (← (LeanProto.EncDec.withIgnoredState (LeanProto.EncDec.parseEnum (α:=FieldOptions_JSType))) (x.jstype)))
| 5 => do FieldOptions_deserializeAux (x.set_lazy (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseBool) (x.lazy)))
| 3 => do FieldOptions_deserializeAux (x.set_deprecated (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseBool) (x.deprecated)))
| 10 => do FieldOptions_deserializeAux (x.set_weak (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseBool) (x.weak)))
| 999 => do FieldOptions_deserializeAux (x.set_uninterpretedOption (← (LeanProto.EncDec.parseKeyAndNonPackedArray ((fun v => LeanProto.EncDec.parseMessage (UninterpretedOption_deserializeAux v)) arbitrary) _type) (x.uninterpretedOption)))
| 0 => do throw $ IO.userError "Decoding message with field number 0"
| _ => do let _ ← LeanProto.EncDec.parseUnknown _type; FieldOptions_deserializeAux x
partial def OneofOptions_deserializeAux (x: OneofOptions) : LeanProto.EncDec.ProtoParseM OneofOptions := do
if (← LeanProto.EncDec.done) then return x
let (_type, key) ← LeanProto.EncDec.parseKey
match key with
| 999 => do OneofOptions_deserializeAux (x.set_uninterpretedOption (← (LeanProto.EncDec.parseKeyAndNonPackedArray ((fun v => LeanProto.EncDec.parseMessage (UninterpretedOption_deserializeAux v)) arbitrary) _type) (x.uninterpretedOption)))
| 0 => do throw $ IO.userError "Decoding message with field number 0"
| _ => do let _ ← LeanProto.EncDec.parseUnknown _type; OneofOptions_deserializeAux x
partial def EnumOptions_deserializeAux (x: EnumOptions) : LeanProto.EncDec.ProtoParseM EnumOptions := do
if (← LeanProto.EncDec.done) then return x
let (_type, key) ← LeanProto.EncDec.parseKey
match key with
| 2 => do EnumOptions_deserializeAux (x.set_allowAlias (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseBool) (x.allowAlias)))
| 3 => do EnumOptions_deserializeAux (x.set_deprecated (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseBool) (x.deprecated)))
| 999 => do EnumOptions_deserializeAux (x.set_uninterpretedOption (← (LeanProto.EncDec.parseKeyAndNonPackedArray ((fun v => LeanProto.EncDec.parseMessage (UninterpretedOption_deserializeAux v)) arbitrary) _type) (x.uninterpretedOption)))
| 0 => do throw $ IO.userError "Decoding message with field number 0"
| _ => do let _ ← LeanProto.EncDec.parseUnknown _type; EnumOptions_deserializeAux x
partial def EnumValueOptions_deserializeAux (x: EnumValueOptions) : LeanProto.EncDec.ProtoParseM EnumValueOptions := do
if (← LeanProto.EncDec.done) then return x
let (_type, key) ← LeanProto.EncDec.parseKey
match key with
| 1 => do EnumValueOptions_deserializeAux (x.set_deprecated (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseBool) (x.deprecated)))
| 999 => do EnumValueOptions_deserializeAux (x.set_uninterpretedOption (← (LeanProto.EncDec.parseKeyAndNonPackedArray ((fun v => LeanProto.EncDec.parseMessage (UninterpretedOption_deserializeAux v)) arbitrary) _type) (x.uninterpretedOption)))
| 0 => do throw $ IO.userError "Decoding message with field number 0"
| _ => do let _ ← LeanProto.EncDec.parseUnknown _type; EnumValueOptions_deserializeAux x
partial def ServiceOptions_deserializeAux (x: ServiceOptions) : LeanProto.EncDec.ProtoParseM ServiceOptions := do
if (← LeanProto.EncDec.done) then return x
let (_type, key) ← LeanProto.EncDec.parseKey
match key with
| 33 => do ServiceOptions_deserializeAux (x.set_deprecated (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseBool) (x.deprecated)))
| 999 => do ServiceOptions_deserializeAux (x.set_uninterpretedOption (← (LeanProto.EncDec.parseKeyAndNonPackedArray ((fun v => LeanProto.EncDec.parseMessage (UninterpretedOption_deserializeAux v)) arbitrary) _type) (x.uninterpretedOption)))
| 0 => do throw $ IO.userError "Decoding message with field number 0"
| _ => do let _ ← LeanProto.EncDec.parseUnknown _type; ServiceOptions_deserializeAux x
partial def MethodOptions_deserializeAux (x: MethodOptions) : LeanProto.EncDec.ProtoParseM MethodOptions := do
if (← LeanProto.EncDec.done) then return x
let (_type, key) ← LeanProto.EncDec.parseKey
match key with
| 33 => do MethodOptions_deserializeAux (x.set_deprecated (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseBool) (x.deprecated)))
| 34 => do MethodOptions_deserializeAux (x.set_idempotencyLevel (← (LeanProto.EncDec.withIgnoredState (LeanProto.EncDec.parseEnum (α:=MethodOptions_IdempotencyLevel))) (x.idempotencyLevel)))
| 999 => do MethodOptions_deserializeAux (x.set_uninterpretedOption (← (LeanProto.EncDec.parseKeyAndNonPackedArray ((fun v => LeanProto.EncDec.parseMessage (UninterpretedOption_deserializeAux v)) arbitrary) _type) (x.uninterpretedOption)))
| 0 => do throw $ IO.userError "Decoding message with field number 0"
| _ => do let _ ← LeanProto.EncDec.parseUnknown _type; MethodOptions_deserializeAux x
partial def UninterpretedOption_deserializeAux (x: UninterpretedOption) : LeanProto.EncDec.ProtoParseM UninterpretedOption := do
if (← LeanProto.EncDec.done) then return x
let (_type, key) ← LeanProto.EncDec.parseKey
match key with
| 2 => do UninterpretedOption_deserializeAux (x.set_name (← (LeanProto.EncDec.parseKeyAndNonPackedArray ((fun v => LeanProto.EncDec.parseMessage (UninterpretedOption_NamePart_deserializeAux v)) arbitrary) _type) (x.name)))
| 3 => do UninterpretedOption_deserializeAux (x.set_identifierValue (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseString) (x.identifierValue)))
| 4 => do UninterpretedOption_deserializeAux (x.set_positiveIntValue (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseUInt64AsNat) (x.positiveIntValue)))
| 5 => do UninterpretedOption_deserializeAux (x.set_negativeIntValue (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseInt64AsInt) (x.negativeIntValue)))
| 6 => do UninterpretedOption_deserializeAux (x.set_doubleValue (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseFloat64AsFloat) (x.doubleValue)))
| 7 => do UninterpretedOption_deserializeAux (x.set_stringValue (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseByteArray) (x.stringValue)))
| 8 => do UninterpretedOption_deserializeAux (x.set_aggregateValue (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseString) (x.aggregateValue)))
| 0 => do throw $ IO.userError "Decoding message with field number 0"
| _ => do let _ ← LeanProto.EncDec.parseUnknown _type; UninterpretedOption_deserializeAux x
partial def UninterpretedOption_NamePart_deserializeAux (x: UninterpretedOption_NamePart) : LeanProto.EncDec.ProtoParseM UninterpretedOption_NamePart := do
if (← LeanProto.EncDec.done) then return x
let (_type, key) ← LeanProto.EncDec.parseKey
match key with
| 1 => do UninterpretedOption_NamePart_deserializeAux (x.set_namePart (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseString) (x.namePart)))
| 2 => do UninterpretedOption_NamePart_deserializeAux (x.set_isExtension (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseBool) (x.isExtension)))
| 0 => do throw $ IO.userError "Decoding message with field number 0"
| _ => do let _ ← LeanProto.EncDec.parseUnknown _type; UninterpretedOption_NamePart_deserializeAux x
partial def SourceCodeInfo_deserializeAux (x: SourceCodeInfo) : LeanProto.EncDec.ProtoParseM SourceCodeInfo := do
if (← LeanProto.EncDec.done) then return x
let (_type, key) ← LeanProto.EncDec.parseKey
match key with
| 1 => do SourceCodeInfo_deserializeAux (x.set_location (← (LeanProto.EncDec.parseKeyAndNonPackedArray ((fun v => LeanProto.EncDec.parseMessage (SourceCodeInfo_Location_deserializeAux v)) arbitrary) _type) (x.location)))
| 0 => do throw $ IO.userError "Decoding message with field number 0"
| _ => do let _ ← LeanProto.EncDec.parseUnknown _type; SourceCodeInfo_deserializeAux x
partial def SourceCodeInfo_Location_deserializeAux (x: SourceCodeInfo_Location) : LeanProto.EncDec.ProtoParseM SourceCodeInfo_Location := do
if (← LeanProto.EncDec.done) then return x
let (_type, key) ← LeanProto.EncDec.parseKey
match key with
| 1 => do SourceCodeInfo_Location_deserializeAux (x.set_path (← (LeanProto.EncDec.parseKeyAndMixedArray ((LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseInt32AsInt) arbitrary) _type) (x.path)))
| 2 => do SourceCodeInfo_Location_deserializeAux (x.set_span (← (LeanProto.EncDec.parseKeyAndMixedArray ((LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseInt32AsInt) arbitrary) _type) (x.span)))
| 3 => do SourceCodeInfo_Location_deserializeAux (x.set_leadingComments (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseString) (x.leadingComments)))
| 4 => do SourceCodeInfo_Location_deserializeAux (x.set_trailingComments (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseString) (x.trailingComments)))
| 6 => do SourceCodeInfo_Location_deserializeAux (x.set_leadingDetachedComments (← (LeanProto.EncDec.parseKeyAndNonPackedArray ((LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseString) arbitrary) _type) (x.leadingDetachedComments)))
| 0 => do throw $ IO.userError "Decoding message with field number 0"
| _ => do let _ ← LeanProto.EncDec.parseUnknown _type; SourceCodeInfo_Location_deserializeAux x
partial def GeneratedCodeInfo_deserializeAux (x: GeneratedCodeInfo) : LeanProto.EncDec.ProtoParseM GeneratedCodeInfo := do
if (← LeanProto.EncDec.done) then return x
let (_type, key) ← LeanProto.EncDec.parseKey
match key with
| 1 => do GeneratedCodeInfo_deserializeAux (x.set_annotation (← (LeanProto.EncDec.parseKeyAndNonPackedArray ((fun v => LeanProto.EncDec.parseMessage (GeneratedCodeInfo_Annotation_deserializeAux v)) arbitrary) _type) (x.annotation)))
| 0 => do throw $ IO.userError "Decoding message with field number 0"
| _ => do let _ ← LeanProto.EncDec.parseUnknown _type; GeneratedCodeInfo_deserializeAux x
partial def GeneratedCodeInfo_Annotation_deserializeAux (x: GeneratedCodeInfo_Annotation) : LeanProto.EncDec.ProtoParseM GeneratedCodeInfo_Annotation := do
if (← LeanProto.EncDec.done) then return x
let (_type, key) ← LeanProto.EncDec.parseKey
match key with
| 1 => do GeneratedCodeInfo_Annotation_deserializeAux (x.set_path (← (LeanProto.EncDec.parseKeyAndMixedArray ((LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseInt32AsInt) arbitrary) _type) (x.path)))
| 2 => do GeneratedCodeInfo_Annotation_deserializeAux (x.set_sourceFile (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseString) (x.sourceFile)))
| 3 => do GeneratedCodeInfo_Annotation_deserializeAux (x.set__begin (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseInt32AsInt) (x._begin)))
| 4 => do GeneratedCodeInfo_Annotation_deserializeAux (x.set__end (← (LeanProto.EncDec.withIgnoredState LeanProto.EncDec.parseInt32AsInt) (x._end)))
| 0 => do throw $ IO.userError "Decoding message with field number 0"
| _ => do let _ ← LeanProto.EncDec.parseUnknown _type; GeneratedCodeInfo_Annotation_deserializeAux x
end
mutual
partial def FileDescriptorSet_serializeAux : FileDescriptorSet -> LeanProto.EncDec.ProtoSerAction
| FileDescriptorSet.mk v0 => do
(LeanProto.EncDec.serializeUnpackedArrayWithTag ((LeanProto.EncDec.serializeMessage (FileDescriptorProto_serializeAux))) (LeanProto.EncDec.WireType.ofLit 2 rfl) 1) v0
partial def FileDescriptorProto_serializeAux : FileDescriptorProto -> LeanProto.EncDec.ProtoSerAction
| FileDescriptorProto.mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 => do
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeString) (LeanProto.EncDec.WireType.ofLit 2 rfl) 1) v0
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeString) (LeanProto.EncDec.WireType.ofLit 2 rfl) 2) v1
(LeanProto.EncDec.serializeUnpackedArrayWithTag (LeanProto.EncDec.serializeString) (LeanProto.EncDec.WireType.ofLit 2 rfl) 3) v2
(LeanProto.EncDec.serializeUnpackedArrayWithTag (LeanProto.EncDec.serializeIntAsInt32) (LeanProto.EncDec.WireType.ofLit 0 rfl) 10) v3
(LeanProto.EncDec.serializeUnpackedArrayWithTag (LeanProto.EncDec.serializeIntAsInt32) (LeanProto.EncDec.WireType.ofLit 0 rfl) 11) v4
(LeanProto.EncDec.serializeUnpackedArrayWithTag ((LeanProto.EncDec.serializeMessage (DescriptorProto_serializeAux))) (LeanProto.EncDec.WireType.ofLit 2 rfl) 4) v5
(LeanProto.EncDec.serializeUnpackedArrayWithTag ((LeanProto.EncDec.serializeMessage (EnumDescriptorProto_serializeAux))) (LeanProto.EncDec.WireType.ofLit 2 rfl) 5) v6
(LeanProto.EncDec.serializeUnpackedArrayWithTag ((LeanProto.EncDec.serializeMessage (ServiceDescriptorProto_serializeAux))) (LeanProto.EncDec.WireType.ofLit 2 rfl) 6) v7
(LeanProto.EncDec.serializeUnpackedArrayWithTag ((LeanProto.EncDec.serializeMessage (FieldDescriptorProto_serializeAux))) (LeanProto.EncDec.WireType.ofLit 2 rfl) 7) v8
LeanProto.EncDec.serializeOpt (LeanProto.EncDec.serializeWithTag ((LeanProto.EncDec.serializeMessage (FileOptions_serializeAux))) (LeanProto.EncDec.WireType.ofLit 2 rfl) 8) v9
LeanProto.EncDec.serializeOpt (LeanProto.EncDec.serializeWithTag ((LeanProto.EncDec.serializeMessage (SourceCodeInfo_serializeAux))) (LeanProto.EncDec.WireType.ofLit 2 rfl) 9) v10
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeString) (LeanProto.EncDec.WireType.ofLit 2 rfl) 12) v11
partial def DescriptorProto_serializeAux : DescriptorProto -> LeanProto.EncDec.ProtoSerAction
| DescriptorProto.mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 => do
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeString) (LeanProto.EncDec.WireType.ofLit 2 rfl) 1) v0
(LeanProto.EncDec.serializeUnpackedArrayWithTag ((LeanProto.EncDec.serializeMessage (FieldDescriptorProto_serializeAux))) (LeanProto.EncDec.WireType.ofLit 2 rfl) 2) v1
(LeanProto.EncDec.serializeUnpackedArrayWithTag ((LeanProto.EncDec.serializeMessage (FieldDescriptorProto_serializeAux))) (LeanProto.EncDec.WireType.ofLit 2 rfl) 6) v2
(LeanProto.EncDec.serializeUnpackedArrayWithTag ((LeanProto.EncDec.serializeMessage (DescriptorProto_serializeAux))) (LeanProto.EncDec.WireType.ofLit 2 rfl) 3) v3
(LeanProto.EncDec.serializeUnpackedArrayWithTag ((LeanProto.EncDec.serializeMessage (EnumDescriptorProto_serializeAux))) (LeanProto.EncDec.WireType.ofLit 2 rfl) 4) v4
(LeanProto.EncDec.serializeUnpackedArrayWithTag ((LeanProto.EncDec.serializeMessage (DescriptorProto_ExtensionRange_serializeAux))) (LeanProto.EncDec.WireType.ofLit 2 rfl) 5) v5
(LeanProto.EncDec.serializeUnpackedArrayWithTag ((LeanProto.EncDec.serializeMessage (OneofDescriptorProto_serializeAux))) (LeanProto.EncDec.WireType.ofLit 2 rfl) 8) v6
LeanProto.EncDec.serializeOpt (LeanProto.EncDec.serializeWithTag ((LeanProto.EncDec.serializeMessage (MessageOptions_serializeAux))) (LeanProto.EncDec.WireType.ofLit 2 rfl) 7) v7
(LeanProto.EncDec.serializeUnpackedArrayWithTag ((LeanProto.EncDec.serializeMessage (DescriptorProto_ReservedRange_serializeAux))) (LeanProto.EncDec.WireType.ofLit 2 rfl) 9) v8
(LeanProto.EncDec.serializeUnpackedArrayWithTag (LeanProto.EncDec.serializeString) (LeanProto.EncDec.WireType.ofLit 2 rfl) 10) v9
partial def DescriptorProto_ExtensionRange_serializeAux : DescriptorProto_ExtensionRange -> LeanProto.EncDec.ProtoSerAction
| DescriptorProto_ExtensionRange.mk v0 v1 v2 => do
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeIntAsInt32) (LeanProto.EncDec.WireType.ofLit 0 rfl) 1) v0
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeIntAsInt32) (LeanProto.EncDec.WireType.ofLit 0 rfl) 2) v1
LeanProto.EncDec.serializeOpt (LeanProto.EncDec.serializeWithTag ((LeanProto.EncDec.serializeMessage (ExtensionRangeOptions_serializeAux))) (LeanProto.EncDec.WireType.ofLit 2 rfl) 3) v2
partial def DescriptorProto_ReservedRange_serializeAux : DescriptorProto_ReservedRange -> LeanProto.EncDec.ProtoSerAction
| DescriptorProto_ReservedRange.mk v0 v1 => do
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeIntAsInt32) (LeanProto.EncDec.WireType.ofLit 0 rfl) 1) v0
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeIntAsInt32) (LeanProto.EncDec.WireType.ofLit 0 rfl) 2) v1
partial def ExtensionRangeOptions_serializeAux : ExtensionRangeOptions -> LeanProto.EncDec.ProtoSerAction
| ExtensionRangeOptions.mk v0 => do
(LeanProto.EncDec.serializeUnpackedArrayWithTag ((LeanProto.EncDec.serializeMessage (UninterpretedOption_serializeAux))) (LeanProto.EncDec.WireType.ofLit 2 rfl) 999) v0
partial def FieldDescriptorProto_serializeAux : FieldDescriptorProto -> LeanProto.EncDec.ProtoSerAction
| FieldDescriptorProto.mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 => do
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeString) (LeanProto.EncDec.WireType.ofLit 2 rfl) 1) v0
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeIntAsInt32) (LeanProto.EncDec.WireType.ofLit 0 rfl) 3) v1
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeEnum) (LeanProto.EncDec.WireType.ofLit 0 rfl) 4) v2
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeEnum) (LeanProto.EncDec.WireType.ofLit 0 rfl) 5) v3
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeString) (LeanProto.EncDec.WireType.ofLit 2 rfl) 6) v4
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeString) (LeanProto.EncDec.WireType.ofLit 2 rfl) 2) v5
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeString) (LeanProto.EncDec.WireType.ofLit 2 rfl) 7) v6
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeIntAsInt32) (LeanProto.EncDec.WireType.ofLit 0 rfl) 9) v7
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeString) (LeanProto.EncDec.WireType.ofLit 2 rfl) 10) v8
LeanProto.EncDec.serializeOpt (LeanProto.EncDec.serializeWithTag ((LeanProto.EncDec.serializeMessage (FieldOptions_serializeAux))) (LeanProto.EncDec.WireType.ofLit 2 rfl) 8) v9
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeBool) (LeanProto.EncDec.WireType.ofLit 0 rfl) 17) v10
partial def OneofDescriptorProto_serializeAux : OneofDescriptorProto -> LeanProto.EncDec.ProtoSerAction
| OneofDescriptorProto.mk v0 v1 => do
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeString) (LeanProto.EncDec.WireType.ofLit 2 rfl) 1) v0
LeanProto.EncDec.serializeOpt (LeanProto.EncDec.serializeWithTag ((LeanProto.EncDec.serializeMessage (OneofOptions_serializeAux))) (LeanProto.EncDec.WireType.ofLit 2 rfl) 2) v1
partial def EnumDescriptorProto_serializeAux : EnumDescriptorProto -> LeanProto.EncDec.ProtoSerAction
| EnumDescriptorProto.mk v0 v1 v2 v3 v4 => do
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeString) (LeanProto.EncDec.WireType.ofLit 2 rfl) 1) v0
(LeanProto.EncDec.serializeUnpackedArrayWithTag ((LeanProto.EncDec.serializeMessage (EnumValueDescriptorProto_serializeAux))) (LeanProto.EncDec.WireType.ofLit 2 rfl) 2) v1
LeanProto.EncDec.serializeOpt (LeanProto.EncDec.serializeWithTag ((LeanProto.EncDec.serializeMessage (EnumOptions_serializeAux))) (LeanProto.EncDec.WireType.ofLit 2 rfl) 3) v2
(LeanProto.EncDec.serializeUnpackedArrayWithTag ((LeanProto.EncDec.serializeMessage (EnumDescriptorProto_EnumReservedRange_serializeAux))) (LeanProto.EncDec.WireType.ofLit 2 rfl) 4) v3
(LeanProto.EncDec.serializeUnpackedArrayWithTag (LeanProto.EncDec.serializeString) (LeanProto.EncDec.WireType.ofLit 2 rfl) 5) v4
partial def EnumDescriptorProto_EnumReservedRange_serializeAux : EnumDescriptorProto_EnumReservedRange -> LeanProto.EncDec.ProtoSerAction
| EnumDescriptorProto_EnumReservedRange.mk v0 v1 => do
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeIntAsInt32) (LeanProto.EncDec.WireType.ofLit 0 rfl) 1) v0
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeIntAsInt32) (LeanProto.EncDec.WireType.ofLit 0 rfl) 2) v1
partial def EnumValueDescriptorProto_serializeAux : EnumValueDescriptorProto -> LeanProto.EncDec.ProtoSerAction
| EnumValueDescriptorProto.mk v0 v1 v2 => do
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeString) (LeanProto.EncDec.WireType.ofLit 2 rfl) 1) v0
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeIntAsInt32) (LeanProto.EncDec.WireType.ofLit 0 rfl) 2) v1
LeanProto.EncDec.serializeOpt (LeanProto.EncDec.serializeWithTag ((LeanProto.EncDec.serializeMessage (EnumValueOptions_serializeAux))) (LeanProto.EncDec.WireType.ofLit 2 rfl) 3) v2
partial def ServiceDescriptorProto_serializeAux : ServiceDescriptorProto -> LeanProto.EncDec.ProtoSerAction
| ServiceDescriptorProto.mk v0 v1 v2 => do
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeString) (LeanProto.EncDec.WireType.ofLit 2 rfl) 1) v0
(LeanProto.EncDec.serializeUnpackedArrayWithTag ((LeanProto.EncDec.serializeMessage (MethodDescriptorProto_serializeAux))) (LeanProto.EncDec.WireType.ofLit 2 rfl) 2) v1
LeanProto.EncDec.serializeOpt (LeanProto.EncDec.serializeWithTag ((LeanProto.EncDec.serializeMessage (ServiceOptions_serializeAux))) (LeanProto.EncDec.WireType.ofLit 2 rfl) 3) v2
partial def MethodDescriptorProto_serializeAux : MethodDescriptorProto -> LeanProto.EncDec.ProtoSerAction
| MethodDescriptorProto.mk v0 v1 v2 v3 v4 v5 => do
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeString) (LeanProto.EncDec.WireType.ofLit 2 rfl) 1) v0
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeString) (LeanProto.EncDec.WireType.ofLit 2 rfl) 2) v1
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeString) (LeanProto.EncDec.WireType.ofLit 2 rfl) 3) v2
LeanProto.EncDec.serializeOpt (LeanProto.EncDec.serializeWithTag ((LeanProto.EncDec.serializeMessage (MethodOptions_serializeAux))) (LeanProto.EncDec.WireType.ofLit 2 rfl) 4) v3
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeBool) (LeanProto.EncDec.WireType.ofLit 0 rfl) 5) v4
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeBool) (LeanProto.EncDec.WireType.ofLit 0 rfl) 6) v5
partial def FileOptions_serializeAux : FileOptions -> LeanProto.EncDec.ProtoSerAction
| FileOptions.mk v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 => do
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeString) (LeanProto.EncDec.WireType.ofLit 2 rfl) 1) v0
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeString) (LeanProto.EncDec.WireType.ofLit 2 rfl) 8) v1
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeBool) (LeanProto.EncDec.WireType.ofLit 0 rfl) 10) v2
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeBool) (LeanProto.EncDec.WireType.ofLit 0 rfl) 20) v3
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeBool) (LeanProto.EncDec.WireType.ofLit 0 rfl) 27) v4
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeEnum) (LeanProto.EncDec.WireType.ofLit 0 rfl) 9) v5
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeString) (LeanProto.EncDec.WireType.ofLit 2 rfl) 11) v6
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeBool) (LeanProto.EncDec.WireType.ofLit 0 rfl) 16) v7
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeBool) (LeanProto.EncDec.WireType.ofLit 0 rfl) 17) v8
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeBool) (LeanProto.EncDec.WireType.ofLit 0 rfl) 18) v9
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeBool) (LeanProto.EncDec.WireType.ofLit 0 rfl) 42) v10
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeBool) (LeanProto.EncDec.WireType.ofLit 0 rfl) 23) v11
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeBool) (LeanProto.EncDec.WireType.ofLit 0 rfl) 31) v12
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeString) (LeanProto.EncDec.WireType.ofLit 2 rfl) 36) v13
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeString) (LeanProto.EncDec.WireType.ofLit 2 rfl) 37) v14
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeString) (LeanProto.EncDec.WireType.ofLit 2 rfl) 39) v15
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeString) (LeanProto.EncDec.WireType.ofLit 2 rfl) 40) v16
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeString) (LeanProto.EncDec.WireType.ofLit 2 rfl) 41) v17
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeString) (LeanProto.EncDec.WireType.ofLit 2 rfl) 44) v18
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeString) (LeanProto.EncDec.WireType.ofLit 2 rfl) 45) v19
(LeanProto.EncDec.serializeUnpackedArrayWithTag ((LeanProto.EncDec.serializeMessage (UninterpretedOption_serializeAux))) (LeanProto.EncDec.WireType.ofLit 2 rfl) 999) v20
partial def MessageOptions_serializeAux : MessageOptions -> LeanProto.EncDec.ProtoSerAction
| MessageOptions.mk v0 v1 v2 v3 v4 => do
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeBool) (LeanProto.EncDec.WireType.ofLit 0 rfl) 1) v0
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeBool) (LeanProto.EncDec.WireType.ofLit 0 rfl) 2) v1
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeBool) (LeanProto.EncDec.WireType.ofLit 0 rfl) 3) v2
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeBool) (LeanProto.EncDec.WireType.ofLit 0 rfl) 7) v3
(LeanProto.EncDec.serializeUnpackedArrayWithTag ((LeanProto.EncDec.serializeMessage (UninterpretedOption_serializeAux))) (LeanProto.EncDec.WireType.ofLit 2 rfl) 999) v4
partial def FieldOptions_serializeAux : FieldOptions -> LeanProto.EncDec.ProtoSerAction
| FieldOptions.mk v0 v1 v2 v3 v4 v5 v6 => do
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeEnum) (LeanProto.EncDec.WireType.ofLit 0 rfl) 1) v0
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeBool) (LeanProto.EncDec.WireType.ofLit 0 rfl) 2) v1
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeEnum) (LeanProto.EncDec.WireType.ofLit 0 rfl) 6) v2
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeBool) (LeanProto.EncDec.WireType.ofLit 0 rfl) 5) v3
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeBool) (LeanProto.EncDec.WireType.ofLit 0 rfl) 3) v4
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeBool) (LeanProto.EncDec.WireType.ofLit 0 rfl) 10) v5
(LeanProto.EncDec.serializeUnpackedArrayWithTag ((LeanProto.EncDec.serializeMessage (UninterpretedOption_serializeAux))) (LeanProto.EncDec.WireType.ofLit 2 rfl) 999) v6
partial def OneofOptions_serializeAux : OneofOptions -> LeanProto.EncDec.ProtoSerAction
| OneofOptions.mk v0 => do
(LeanProto.EncDec.serializeUnpackedArrayWithTag ((LeanProto.EncDec.serializeMessage (UninterpretedOption_serializeAux))) (LeanProto.EncDec.WireType.ofLit 2 rfl) 999) v0
partial def EnumOptions_serializeAux : EnumOptions -> LeanProto.EncDec.ProtoSerAction
| EnumOptions.mk v0 v1 v2 => do
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeBool) (LeanProto.EncDec.WireType.ofLit 0 rfl) 2) v0
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeBool) (LeanProto.EncDec.WireType.ofLit 0 rfl) 3) v1
(LeanProto.EncDec.serializeUnpackedArrayWithTag ((LeanProto.EncDec.serializeMessage (UninterpretedOption_serializeAux))) (LeanProto.EncDec.WireType.ofLit 2 rfl) 999) v2
partial def EnumValueOptions_serializeAux : EnumValueOptions -> LeanProto.EncDec.ProtoSerAction
| EnumValueOptions.mk v0 v1 => do
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeBool) (LeanProto.EncDec.WireType.ofLit 0 rfl) 1) v0
(LeanProto.EncDec.serializeUnpackedArrayWithTag ((LeanProto.EncDec.serializeMessage (UninterpretedOption_serializeAux))) (LeanProto.EncDec.WireType.ofLit 2 rfl) 999) v1
partial def ServiceOptions_serializeAux : ServiceOptions -> LeanProto.EncDec.ProtoSerAction
| ServiceOptions.mk v0 v1 => do
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeBool) (LeanProto.EncDec.WireType.ofLit 0 rfl) 33) v0
(LeanProto.EncDec.serializeUnpackedArrayWithTag ((LeanProto.EncDec.serializeMessage (UninterpretedOption_serializeAux))) (LeanProto.EncDec.WireType.ofLit 2 rfl) 999) v1
partial def MethodOptions_serializeAux : MethodOptions -> LeanProto.EncDec.ProtoSerAction
| MethodOptions.mk v0 v1 v2 => do
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeBool) (LeanProto.EncDec.WireType.ofLit 0 rfl) 33) v0
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeEnum) (LeanProto.EncDec.WireType.ofLit 0 rfl) 34) v1
(LeanProto.EncDec.serializeUnpackedArrayWithTag ((LeanProto.EncDec.serializeMessage (UninterpretedOption_serializeAux))) (LeanProto.EncDec.WireType.ofLit 2 rfl) 999) v2
partial def UninterpretedOption_serializeAux : UninterpretedOption -> LeanProto.EncDec.ProtoSerAction
| UninterpretedOption.mk v0 v1 v2 v3 v4 v5 v6 => do
(LeanProto.EncDec.serializeUnpackedArrayWithTag ((LeanProto.EncDec.serializeMessage (UninterpretedOption_NamePart_serializeAux))) (LeanProto.EncDec.WireType.ofLit 2 rfl) 2) v0
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeString) (LeanProto.EncDec.WireType.ofLit 2 rfl) 3) v1
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeNatAsUInt64) (LeanProto.EncDec.WireType.ofLit 0 rfl) 4) v2
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeIntAsInt64) (LeanProto.EncDec.WireType.ofLit 0 rfl) 5) v3
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeFloatAsFloat64) (LeanProto.EncDec.WireType.ofLit 1 rfl) 6) v4
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeByteArray) (LeanProto.EncDec.WireType.ofLit 2 rfl) 7) v5
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeString) (LeanProto.EncDec.WireType.ofLit 2 rfl) 8) v6
partial def UninterpretedOption_NamePart_serializeAux : UninterpretedOption_NamePart -> LeanProto.EncDec.ProtoSerAction
| UninterpretedOption_NamePart.mk v0 v1 => do
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeString) (LeanProto.EncDec.WireType.ofLit 2 rfl) 1) v0
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeBool) (LeanProto.EncDec.WireType.ofLit 0 rfl) 2) v1
partial def SourceCodeInfo_serializeAux : SourceCodeInfo -> LeanProto.EncDec.ProtoSerAction
| SourceCodeInfo.mk v0 => do
(LeanProto.EncDec.serializeUnpackedArrayWithTag ((LeanProto.EncDec.serializeMessage (SourceCodeInfo_Location_serializeAux))) (LeanProto.EncDec.WireType.ofLit 2 rfl) 1) v0
partial def SourceCodeInfo_Location_serializeAux : SourceCodeInfo_Location -> LeanProto.EncDec.ProtoSerAction
| SourceCodeInfo_Location.mk v0 v1 v2 v3 v4 => do
(LeanProto.EncDec.serializeIfNonempty (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializePackedArray (LeanProto.EncDec.serializeIntAsInt32)) (LeanProto.EncDec.WireType.ofLit 2 rfl) 1)) v0
(LeanProto.EncDec.serializeIfNonempty (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializePackedArray (LeanProto.EncDec.serializeIntAsInt32)) (LeanProto.EncDec.WireType.ofLit 2 rfl) 2)) v1
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeString) (LeanProto.EncDec.WireType.ofLit 2 rfl) 3) v2
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeString) (LeanProto.EncDec.WireType.ofLit 2 rfl) 4) v3
(LeanProto.EncDec.serializeUnpackedArrayWithTag (LeanProto.EncDec.serializeString) (LeanProto.EncDec.WireType.ofLit 2 rfl) 6) v4
partial def GeneratedCodeInfo_serializeAux : GeneratedCodeInfo -> LeanProto.EncDec.ProtoSerAction
| GeneratedCodeInfo.mk v0 => do
(LeanProto.EncDec.serializeUnpackedArrayWithTag ((LeanProto.EncDec.serializeMessage (GeneratedCodeInfo_Annotation_serializeAux))) (LeanProto.EncDec.WireType.ofLit 2 rfl) 1) v0
partial def GeneratedCodeInfo_Annotation_serializeAux : GeneratedCodeInfo_Annotation -> LeanProto.EncDec.ProtoSerAction
| GeneratedCodeInfo_Annotation.mk v0 v1 v2 v3 => do
(LeanProto.EncDec.serializeIfNonempty (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializePackedArray (LeanProto.EncDec.serializeIntAsInt32)) (LeanProto.EncDec.WireType.ofLit 2 rfl) 1)) v0
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeString) (LeanProto.EncDec.WireType.ofLit 2 rfl) 2) v1
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeIntAsInt32) (LeanProto.EncDec.WireType.ofLit 0 rfl) 3) v2
LeanProto.EncDec.serializeSkipDefault (LeanProto.EncDec.serializeWithTag (LeanProto.EncDec.serializeIntAsInt32) (LeanProto.EncDec.WireType.ofLit 0 rfl) 4) v3
end
instance : LeanProto.ProtoSerialize FileDescriptorSet where
serialize x := do
let res := LeanProto.EncDec.serialize (FileDescriptorSet_serializeAux x)
LeanProto.EncDec.resultStateToExcept res
instance : LeanProto.ProtoDeserialize FileDescriptorSet where
deserialize b :=
let res := LeanProto.EncDec.parse b (FileDescriptorSet_deserializeAux (FileDescriptorSet.mkDefault))
LeanProto.EncDec.resultToExcept res
instance : LeanProto.ProtoSerialize FileDescriptorProto where
serialize x := do
let res := LeanProto.EncDec.serialize (FileDescriptorProto_serializeAux x)
LeanProto.EncDec.resultStateToExcept res
instance : LeanProto.ProtoDeserialize FileDescriptorProto where
deserialize b :=
let res := LeanProto.EncDec.parse b (FileDescriptorProto_deserializeAux (FileDescriptorProto.mkDefault))
LeanProto.EncDec.resultToExcept res
instance : LeanProto.ProtoSerialize DescriptorProto where
serialize x := do
let res := LeanProto.EncDec.serialize (DescriptorProto_serializeAux x)
LeanProto.EncDec.resultStateToExcept res
instance : LeanProto.ProtoDeserialize DescriptorProto where
deserialize b :=
let res := LeanProto.EncDec.parse b (DescriptorProto_deserializeAux (DescriptorProto.mkDefault))
LeanProto.EncDec.resultToExcept res
instance : LeanProto.ProtoSerialize DescriptorProto_ExtensionRange where
serialize x := do
let res := LeanProto.EncDec.serialize (DescriptorProto_ExtensionRange_serializeAux x)
LeanProto.EncDec.resultStateToExcept res
instance : LeanProto.ProtoDeserialize DescriptorProto_ExtensionRange where
deserialize b :=
let res := LeanProto.EncDec.parse b (DescriptorProto_ExtensionRange_deserializeAux (DescriptorProto_ExtensionRange.mkDefault))
LeanProto.EncDec.resultToExcept res
instance : LeanProto.ProtoSerialize DescriptorProto_ReservedRange where
serialize x := do
let res := LeanProto.EncDec.serialize (DescriptorProto_ReservedRange_serializeAux x)
LeanProto.EncDec.resultStateToExcept res
instance : LeanProto.ProtoDeserialize DescriptorProto_ReservedRange where
deserialize b :=
let res := LeanProto.EncDec.parse b (DescriptorProto_ReservedRange_deserializeAux (DescriptorProto_ReservedRange.mkDefault))
LeanProto.EncDec.resultToExcept res
instance : LeanProto.ProtoSerialize ExtensionRangeOptions where
serialize x := do
let res := LeanProto.EncDec.serialize (ExtensionRangeOptions_serializeAux x)
LeanProto.EncDec.resultStateToExcept res
instance : LeanProto.ProtoDeserialize ExtensionRangeOptions where
deserialize b :=
let res := LeanProto.EncDec.parse b (ExtensionRangeOptions_deserializeAux (ExtensionRangeOptions.mkDefault))
LeanProto.EncDec.resultToExcept res
instance : LeanProto.ProtoSerialize FieldDescriptorProto where
serialize x := do
let res := LeanProto.EncDec.serialize (FieldDescriptorProto_serializeAux x)
LeanProto.EncDec.resultStateToExcept res
instance : LeanProto.ProtoDeserialize FieldDescriptorProto where
deserialize b :=
let res := LeanProto.EncDec.parse b (FieldDescriptorProto_deserializeAux (FieldDescriptorProto.mkDefault))
LeanProto.EncDec.resultToExcept res
instance : LeanProto.ProtoSerialize OneofDescriptorProto where
serialize x := do
let res := LeanProto.EncDec.serialize (OneofDescriptorProto_serializeAux x)
LeanProto.EncDec.resultStateToExcept res
instance : LeanProto.ProtoDeserialize OneofDescriptorProto where
deserialize b :=
let res := LeanProto.EncDec.parse b (OneofDescriptorProto_deserializeAux (OneofDescriptorProto.mkDefault))
LeanProto.EncDec.resultToExcept res
instance : LeanProto.ProtoSerialize EnumDescriptorProto where
serialize x := do
let res := LeanProto.EncDec.serialize (EnumDescriptorProto_serializeAux x)
LeanProto.EncDec.resultStateToExcept res
instance : LeanProto.ProtoDeserialize EnumDescriptorProto where
deserialize b :=
let res := LeanProto.EncDec.parse b (EnumDescriptorProto_deserializeAux (EnumDescriptorProto.mkDefault))
LeanProto.EncDec.resultToExcept res
instance : LeanProto.ProtoSerialize EnumDescriptorProto_EnumReservedRange where
serialize x := do
let res := LeanProto.EncDec.serialize (EnumDescriptorProto_EnumReservedRange_serializeAux x)
LeanProto.EncDec.resultStateToExcept res
instance : LeanProto.ProtoDeserialize EnumDescriptorProto_EnumReservedRange where
deserialize b :=
let res := LeanProto.EncDec.parse b (EnumDescriptorProto_EnumReservedRange_deserializeAux (EnumDescriptorProto_EnumReservedRange.mkDefault))
LeanProto.EncDec.resultToExcept res
instance : LeanProto.ProtoSerialize EnumValueDescriptorProto where
serialize x := do
let res := LeanProto.EncDec.serialize (EnumValueDescriptorProto_serializeAux x)
LeanProto.EncDec.resultStateToExcept res
instance : LeanProto.ProtoDeserialize EnumValueDescriptorProto where
deserialize b :=
let res := LeanProto.EncDec.parse b (EnumValueDescriptorProto_deserializeAux (EnumValueDescriptorProto.mkDefault))
LeanProto.EncDec.resultToExcept res
instance : LeanProto.ProtoSerialize ServiceDescriptorProto where
serialize x := do
let res := LeanProto.EncDec.serialize (ServiceDescriptorProto_serializeAux x)
LeanProto.EncDec.resultStateToExcept res
instance : LeanProto.ProtoDeserialize ServiceDescriptorProto where
deserialize b :=
let res := LeanProto.EncDec.parse b (ServiceDescriptorProto_deserializeAux (ServiceDescriptorProto.mkDefault))
LeanProto.EncDec.resultToExcept res
instance : LeanProto.ProtoSerialize MethodDescriptorProto where
serialize x := do
let res := LeanProto.EncDec.serialize (MethodDescriptorProto_serializeAux x)
LeanProto.EncDec.resultStateToExcept res
instance : LeanProto.ProtoDeserialize MethodDescriptorProto where
deserialize b :=
let res := LeanProto.EncDec.parse b (MethodDescriptorProto_deserializeAux (MethodDescriptorProto.mkDefault))
LeanProto.EncDec.resultToExcept res
instance : LeanProto.ProtoSerialize FileOptions where
serialize x := do
let res := LeanProto.EncDec.serialize (FileOptions_serializeAux x)
LeanProto.EncDec.resultStateToExcept res
instance : LeanProto.ProtoDeserialize FileOptions where
deserialize b :=
let res := LeanProto.EncDec.parse b (FileOptions_deserializeAux (FileOptions.mkDefault))
LeanProto.EncDec.resultToExcept res
instance : LeanProto.ProtoSerialize MessageOptions where
serialize x := do
let res := LeanProto.EncDec.serialize (MessageOptions_serializeAux x)
LeanProto.EncDec.resultStateToExcept res
instance : LeanProto.ProtoDeserialize MessageOptions where
deserialize b :=
let res := LeanProto.EncDec.parse b (MessageOptions_deserializeAux (MessageOptions.mkDefault))
LeanProto.EncDec.resultToExcept res
instance : LeanProto.ProtoSerialize FieldOptions where
serialize x := do
let res := LeanProto.EncDec.serialize (FieldOptions_serializeAux x)
LeanProto.EncDec.resultStateToExcept res
instance : LeanProto.ProtoDeserialize FieldOptions where
deserialize b :=
let res := LeanProto.EncDec.parse b (FieldOptions_deserializeAux (FieldOptions.mkDefault))
LeanProto.EncDec.resultToExcept res
instance : LeanProto.ProtoSerialize OneofOptions where
serialize x := do
let res := LeanProto.EncDec.serialize (OneofOptions_serializeAux x)
LeanProto.EncDec.resultStateToExcept res
instance : LeanProto.ProtoDeserialize OneofOptions where
deserialize b :=
let res := LeanProto.EncDec.parse b (OneofOptions_deserializeAux (OneofOptions.mkDefault))
LeanProto.EncDec.resultToExcept res
instance : LeanProto.ProtoSerialize EnumOptions where
serialize x := do
let res := LeanProto.EncDec.serialize (EnumOptions_serializeAux x)
LeanProto.EncDec.resultStateToExcept res
instance : LeanProto.ProtoDeserialize EnumOptions where
deserialize b :=
let res := LeanProto.EncDec.parse b (EnumOptions_deserializeAux (EnumOptions.mkDefault))
LeanProto.EncDec.resultToExcept res
instance : LeanProto.ProtoSerialize EnumValueOptions where
serialize x := do
let res := LeanProto.EncDec.serialize (EnumValueOptions_serializeAux x)
LeanProto.EncDec.resultStateToExcept res
instance : LeanProto.ProtoDeserialize EnumValueOptions where
deserialize b :=
let res := LeanProto.EncDec.parse b (EnumValueOptions_deserializeAux (EnumValueOptions.mkDefault))
LeanProto.EncDec.resultToExcept res
instance : LeanProto.ProtoSerialize ServiceOptions where
serialize x := do
let res := LeanProto.EncDec.serialize (ServiceOptions_serializeAux x)
LeanProto.EncDec.resultStateToExcept res
instance : LeanProto.ProtoDeserialize ServiceOptions where
deserialize b :=
let res := LeanProto.EncDec.parse b (ServiceOptions_deserializeAux (ServiceOptions.mkDefault))
LeanProto.EncDec.resultToExcept res
instance : LeanProto.ProtoSerialize MethodOptions where
serialize x := do
let res := LeanProto.EncDec.serialize (MethodOptions_serializeAux x)
LeanProto.EncDec.resultStateToExcept res
instance : LeanProto.ProtoDeserialize MethodOptions where
deserialize b :=
let res := LeanProto.EncDec.parse b (MethodOptions_deserializeAux (MethodOptions.mkDefault))
LeanProto.EncDec.resultToExcept res
instance : LeanProto.ProtoSerialize UninterpretedOption where
serialize x := do
let res := LeanProto.EncDec.serialize (UninterpretedOption_serializeAux x)
LeanProto.EncDec.resultStateToExcept res
instance : LeanProto.ProtoDeserialize UninterpretedOption where
deserialize b :=
let res := LeanProto.EncDec.parse b (UninterpretedOption_deserializeAux (UninterpretedOption.mkDefault))
LeanProto.EncDec.resultToExcept res
instance : LeanProto.ProtoSerialize UninterpretedOption_NamePart where
serialize x := do
let res := LeanProto.EncDec.serialize (UninterpretedOption_NamePart_serializeAux x)
LeanProto.EncDec.resultStateToExcept res
instance : LeanProto.ProtoDeserialize UninterpretedOption_NamePart where
deserialize b :=
let res := LeanProto.EncDec.parse b (UninterpretedOption_NamePart_deserializeAux (UninterpretedOption_NamePart.mkDefault))
LeanProto.EncDec.resultToExcept res
instance : LeanProto.ProtoSerialize SourceCodeInfo where
serialize x := do
let res := LeanProto.EncDec.serialize (SourceCodeInfo_serializeAux x)
LeanProto.EncDec.resultStateToExcept res
instance : LeanProto.ProtoDeserialize SourceCodeInfo where
deserialize b :=
let res := LeanProto.EncDec.parse b (SourceCodeInfo_deserializeAux (SourceCodeInfo.mkDefault))
LeanProto.EncDec.resultToExcept res
instance : LeanProto.ProtoSerialize SourceCodeInfo_Location where
serialize x := do
let res := LeanProto.EncDec.serialize (SourceCodeInfo_Location_serializeAux x)
LeanProto.EncDec.resultStateToExcept res
instance : LeanProto.ProtoDeserialize SourceCodeInfo_Location where
deserialize b :=
let res := LeanProto.EncDec.parse b (SourceCodeInfo_Location_deserializeAux (SourceCodeInfo_Location.mkDefault))
LeanProto.EncDec.resultToExcept res
instance : LeanProto.ProtoSerialize GeneratedCodeInfo where
serialize x := do
let res := LeanProto.EncDec.serialize (GeneratedCodeInfo_serializeAux x)
LeanProto.EncDec.resultStateToExcept res
instance : LeanProto.ProtoDeserialize GeneratedCodeInfo where
deserialize b :=
let res := LeanProto.EncDec.parse b (GeneratedCodeInfo_deserializeAux (GeneratedCodeInfo.mkDefault))
LeanProto.EncDec.resultToExcept res
instance : LeanProto.ProtoSerialize GeneratedCodeInfo_Annotation where
serialize x := do
let res := LeanProto.EncDec.serialize (GeneratedCodeInfo_Annotation_serializeAux x)
LeanProto.EncDec.resultStateToExcept res
instance : LeanProto.ProtoDeserialize GeneratedCodeInfo_Annotation where
deserialize b :=
let res := LeanProto.EncDec.parse b (GeneratedCodeInfo_Annotation_deserializeAux (GeneratedCodeInfo_Annotation.mkDefault))
LeanProto.EncDec.resultToExcept res
end LeanProtocPlugin.Google.Protobuf |
9cb8ec3b7e113b5d2d7342651bcb91033a196e66 | 9028d228ac200bbefe3a711342514dd4e4458bff | /src/algebra/group/basic.lean | 4ff8daf0d6d166bdb9a4e93c8c24578757e35a6d | [
"Apache-2.0"
] | permissive | mcncm/mathlib | 8d25099344d9d2bee62822cb9ed43aa3e09fa05e | fde3d78cadeec5ef827b16ae55664ef115e66f57 | refs/heads/master | 1,672,743,316,277 | 1,602,618,514,000 | 1,602,618,514,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 13,502 | 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, Simon Hudon, Mario Carneiro
-/
import algebra.group.defs
import logic.function.basic
universe u
section monoid
variables {M : Type u} [monoid M]
@[to_additive]
lemma ite_mul_one {P : Prop} [decidable P] {a b : M} :
ite P (a * b) 1 = ite P a 1 * ite P b 1 :=
by { by_cases h : P; simp [h], }
end monoid
section comm_semigroup
variables {G : Type u} [comm_semigroup G]
@[no_rsimp, to_additive]
lemma mul_left_comm : ∀ a b c : G, a * (b * c) = b * (a * c) :=
left_comm has_mul.mul mul_comm mul_assoc
attribute [no_rsimp] add_left_comm
@[to_additive]
lemma mul_right_comm : ∀ a b c : G, a * b * c = a * c * b :=
right_comm has_mul.mul mul_comm mul_assoc
@[to_additive]
theorem mul_mul_mul_comm (a b c d : G) : (a * b) * (c * d) = (a * c) * (b * d) :=
by simp only [mul_left_comm, mul_assoc]
end comm_semigroup
local attribute [simp] mul_assoc sub_eq_add_neg
section add_monoid
variables {M : Type u} [add_monoid M] {a b c : M}
@[simp] lemma bit0_zero : bit0 (0 : M) = 0 := add_zero _
@[simp] lemma bit1_zero [has_one M] : bit1 (0 : M) = 1 :=
by rw [bit1, bit0_zero, zero_add]
end add_monoid
section comm_monoid
variables {M : Type u} [comm_monoid M] {x y z : M}
@[to_additive] lemma inv_unique (hy : x * y = 1) (hz : x * z = 1) : y = z :=
left_inv_eq_right_inv (trans (mul_comm _ _) hy) hz
end comm_monoid
section left_cancel_monoid
variables {M : Type u} [left_cancel_monoid M]
@[to_additive] lemma eq_one_of_mul_self_left_cancel {a : M} (h : a * a = a) : a = 1 :=
mul_left_cancel (show a * a = a * 1, by rwa mul_one)
@[to_additive] lemma eq_one_of_left_cancel_mul_self {a : M} (h : a = a * a) : a = 1 :=
mul_left_cancel (show a * a = a * 1, by rwa [mul_one, eq_comm])
end left_cancel_monoid
section right_cancel_monoid
variables {M : Type u} [right_cancel_monoid M]
@[to_additive] lemma eq_one_of_mul_self_right_cancel {a : M} (h : a * a = a) : a = 1 :=
mul_right_cancel (show a * a = 1 * a, by rwa one_mul)
@[to_additive] lemma eq_one_of_right_cancel_mul_self {a : M} (h : a = a * a) : a = 1 :=
mul_right_cancel (show a * a = 1 * a, by rwa [one_mul, eq_comm])
end right_cancel_monoid
section group
variables {G : Type u} [group G] {a b c : G}
@[simp, to_additive]
lemma inv_mul_cancel_right (a b : G) : a * b⁻¹ * b = a :=
by simp [mul_assoc]
@[simp, to_additive neg_zero]
lemma one_inv : 1⁻¹ = (1 : G) :=
inv_eq_of_mul_eq_one (one_mul 1)
@[to_additive]
theorem left_inverse_inv (G) [group G] :
function.left_inverse (λ a : G, a⁻¹) (λ a, a⁻¹) :=
inv_inv
@[to_additive]
lemma inv_involutive : function.involutive (has_inv.inv : G → G) := inv_inv
@[to_additive]
lemma inv_injective : function.injective (has_inv.inv : G → G) :=
inv_involutive.injective
@[simp, to_additive] theorem inv_inj : a⁻¹ = b⁻¹ ↔ a = b := inv_injective.eq_iff
@[simp, to_additive]
lemma mul_inv_cancel_left (a b : G) : a * (a⁻¹ * b) = b :=
by rw [← mul_assoc, mul_right_inv, one_mul]
@[to_additive]
theorem mul_left_surjective (a : G) : function.surjective ((*) a) :=
λ x, ⟨a⁻¹ * x, mul_inv_cancel_left a x⟩
@[to_additive]
theorem mul_right_surjective (a : G) : function.surjective (λ x, x * a) :=
λ x, ⟨x * a⁻¹, inv_mul_cancel_right x a⟩
@[simp, to_additive neg_add_rev]
lemma mul_inv_rev (a b : G) : (a * b)⁻¹ = b⁻¹ * a⁻¹ :=
inv_eq_of_mul_eq_one $ by simp
@[to_additive]
lemma eq_inv_of_eq_inv (h : a = b⁻¹) : b = a⁻¹ :=
by simp [h]
@[to_additive]
lemma eq_inv_of_mul_eq_one (h : a * b = 1) : a = b⁻¹ :=
have a⁻¹ = b, from inv_eq_of_mul_eq_one h,
by simp [this.symm]
@[to_additive]
lemma eq_mul_inv_of_mul_eq (h : a * c = b) : a = b * c⁻¹ :=
by simp [h.symm]
@[to_additive]
lemma eq_inv_mul_of_mul_eq (h : b * a = c) : a = b⁻¹ * c :=
by simp [h.symm]
@[to_additive]
lemma inv_mul_eq_of_eq_mul (h : b = a * c) : a⁻¹ * b = c :=
by simp [h]
@[to_additive]
lemma mul_inv_eq_of_eq_mul (h : a = c * b) : a * b⁻¹ = c :=
by simp [h]
@[to_additive]
lemma eq_mul_of_mul_inv_eq (h : a * c⁻¹ = b) : a = b * c :=
by simp [h.symm]
@[to_additive]
lemma eq_mul_of_inv_mul_eq (h : b⁻¹ * a = c) : a = b * c :=
by simp [h.symm, mul_inv_cancel_left]
@[to_additive]
lemma mul_eq_of_eq_inv_mul (h : b = a⁻¹ * c) : a * b = c :=
by rw [h, mul_inv_cancel_left]
@[to_additive]
lemma mul_eq_of_eq_mul_inv (h : a = c * b⁻¹) : a * b = c :=
by simp [h]
@[to_additive]
theorem mul_self_iff_eq_one : a * a = a ↔ a = 1 :=
by have := @mul_right_inj _ _ a a 1; rwa mul_one at this
@[simp, to_additive]
theorem inv_eq_one : a⁻¹ = 1 ↔ a = 1 :=
by rw [← @inv_inj _ _ a 1, one_inv]
@[to_additive]
theorem inv_ne_one : a⁻¹ ≠ 1 ↔ a ≠ 1 :=
not_congr inv_eq_one
@[to_additive]
theorem eq_inv_iff_eq_inv : a = b⁻¹ ↔ b = a⁻¹ :=
⟨eq_inv_of_eq_inv, eq_inv_of_eq_inv⟩
@[to_additive]
theorem inv_eq_iff_inv_eq : a⁻¹ = b ↔ b⁻¹ = a :=
eq_comm.trans $ eq_inv_iff_eq_inv.trans eq_comm
@[to_additive]
theorem mul_eq_one_iff_eq_inv : a * b = 1 ↔ a = b⁻¹ :=
by simpa [mul_left_inv, -mul_left_inj] using @mul_left_inj _ _ b a (b⁻¹)
@[to_additive]
theorem mul_eq_one_iff_inv_eq : a * b = 1 ↔ a⁻¹ = b :=
by rw [mul_eq_one_iff_eq_inv, eq_inv_iff_eq_inv, eq_comm]
@[to_additive]
theorem eq_inv_iff_mul_eq_one : a = b⁻¹ ↔ a * b = 1 :=
mul_eq_one_iff_eq_inv.symm
@[to_additive]
theorem inv_eq_iff_mul_eq_one : a⁻¹ = b ↔ a * b = 1 :=
mul_eq_one_iff_inv_eq.symm
@[to_additive]
theorem eq_mul_inv_iff_mul_eq : a = b * c⁻¹ ↔ a * c = b :=
⟨λ h, by rw [h, inv_mul_cancel_right], λ h, by rw [← h, mul_inv_cancel_right]⟩
@[to_additive]
theorem eq_inv_mul_iff_mul_eq : a = b⁻¹ * c ↔ b * a = c :=
⟨λ h, by rw [h, mul_inv_cancel_left], λ h, by rw [← h, inv_mul_cancel_left]⟩
@[to_additive]
theorem inv_mul_eq_iff_eq_mul : a⁻¹ * b = c ↔ b = a * c :=
⟨λ h, by rw [← h, mul_inv_cancel_left], λ h, by rw [h, inv_mul_cancel_left]⟩
@[to_additive]
theorem mul_inv_eq_iff_eq_mul : a * b⁻¹ = c ↔ a = c * b :=
⟨λ h, by rw [← h, inv_mul_cancel_right], λ h, by rw [h, mul_inv_cancel_right]⟩
@[to_additive]
theorem mul_inv_eq_one : a * b⁻¹ = 1 ↔ a = b :=
by rw [mul_eq_one_iff_eq_inv, inv_inv]
@[to_additive]
theorem inv_mul_eq_one : a⁻¹ * b = 1 ↔ a = b :=
by rw [mul_eq_one_iff_eq_inv, inv_inj]
@[simp, to_additive]
lemma mul_left_eq_self : a * b = b ↔ a = 1 :=
⟨λ h, @mul_right_cancel _ _ a b 1 (by simp [h]), λ h, by simp [h]⟩
@[simp, to_additive]
lemma mul_right_eq_self : a * b = a ↔ b = 1 :=
⟨λ h, @mul_left_cancel _ _ a b 1 (by simp [h]), λ h, by simp [h]⟩
end group
section add_group
variables {G : Type u} [add_group G] {a b c d : G}
@[simp] lemma sub_self (a : G) : a - a = 0 :=
add_right_neg a
@[simp] lemma sub_add_cancel (a b : G) : a - b + b = a :=
neg_add_cancel_right a b
@[simp] lemma add_sub_cancel (a b : G) : a + b - b = a :=
add_neg_cancel_right a b
lemma add_sub_assoc (a b c : G) : a + b - c = a + (b - c) :=
by rw [sub_eq_add_neg, add_assoc, ←sub_eq_add_neg]
lemma eq_of_sub_eq_zero (h : a - b = 0) : a = b :=
have 0 + b = b, by rw zero_add,
have (a - b) + b = b, by rwa h,
by rwa [sub_eq_add_neg, neg_add_cancel_right] at this
lemma sub_eq_zero_of_eq (h : a = b) : a - b = 0 :=
by rw [h, sub_self]
lemma sub_eq_zero_iff_eq : a - b = 0 ↔ a = b :=
⟨eq_of_sub_eq_zero, sub_eq_zero_of_eq⟩
@[simp] lemma zero_sub (a : G) : 0 - a = -a :=
zero_add (-a)
@[simp] lemma sub_zero (a : G) : a - 0 = a :=
by rw [sub_eq_add_neg, neg_zero, add_zero]
lemma sub_ne_zero_of_ne (h : a ≠ b) : a - b ≠ 0 :=
begin
intro hab,
apply h,
apply eq_of_sub_eq_zero hab
end
@[simp] lemma sub_neg_eq_add (a b : G) : a - (-b) = a + b :=
by rw [sub_eq_add_neg, neg_neg]
@[simp] lemma neg_sub (a b : G) : -(a - b) = b - a :=
neg_eq_of_add_eq_zero (by rw [sub_eq_add_neg, sub_eq_add_neg, add_assoc, neg_add_cancel_left,
add_right_neg])
local attribute [simp] add_assoc
lemma add_sub (a b c : G) : a + (b - c) = a + b - c :=
by simp
lemma sub_add_eq_sub_sub_swap (a b c : G) : a - (b + c) = a - c - b :=
by simp
@[simp] lemma add_sub_add_right_eq_sub (a b c : G) : (a + c) - (b + c) = a - b :=
by rw [sub_add_eq_sub_sub_swap]; simp
lemma eq_sub_of_add_eq (h : a + c = b) : a = b - c :=
by simp [h.symm]
lemma sub_eq_of_eq_add (h : a = c + b) : a - b = c :=
by simp [h]
lemma eq_add_of_sub_eq (h : a - c = b) : a = b + c :=
by simp [h.symm]
lemma add_eq_of_eq_sub (h : a = c - b) : a + b = c :=
by simp [h]
@[simp] lemma sub_right_inj : a - b = a - c ↔ b = c :=
(add_right_inj _).trans neg_inj
@[simp] lemma sub_left_inj : b - a = c - a ↔ b = c :=
add_left_inj _
lemma sub_add_sub_cancel (a b c : G) : (a - b) + (b - c) = a - c :=
by rw [← add_sub_assoc, sub_add_cancel]
lemma sub_sub_sub_cancel_right (a b c : G) : (a - c) - (b - c) = a - b :=
by rw [← neg_sub c b, sub_neg_eq_add, sub_add_sub_cancel]
theorem sub_sub_assoc_swap : a - (b - c) = a + c - b :=
by simp
theorem sub_eq_zero : a - b = 0 ↔ a = b :=
⟨eq_of_sub_eq_zero, λ h, by rw [h, sub_self]⟩
theorem sub_ne_zero : a - b ≠ 0 ↔ a ≠ b :=
not_congr sub_eq_zero
theorem eq_sub_iff_add_eq : a = b - c ↔ a + c = b :=
eq_add_neg_iff_add_eq
theorem sub_eq_iff_eq_add : a - b = c ↔ a = c + b :=
add_neg_eq_iff_eq_add
theorem eq_iff_eq_of_sub_eq_sub (H : a - b = c - d) : a = b ↔ c = d :=
by rw [← sub_eq_zero, H, sub_eq_zero]
theorem left_inverse_sub_add_left (c : G) : function.left_inverse (λ x, x - c) (λ x, x + c) :=
assume x, add_sub_cancel x c
theorem left_inverse_add_left_sub (c : G) : function.left_inverse (λ x, x + c) (λ x, x - c) :=
assume x, sub_add_cancel x c
theorem left_inverse_add_right_neg_add (c : G) :
function.left_inverse (λ x, c + x) (λ x, - c + x) :=
assume x, add_neg_cancel_left c x
theorem left_inverse_neg_add_add_right (c : G) :
function.left_inverse (λ x, - c + x) (λ x, c + x) :=
assume x, neg_add_cancel_left c x
end add_group
section comm_group
variables {G : Type u} [comm_group G]
@[to_additive neg_add]
lemma mul_inv (a b : G) : (a * b)⁻¹ = a⁻¹ * b⁻¹ :=
by rw [mul_inv_rev, mul_comm]
end comm_group
section add_comm_group
variables {G : Type u} [add_comm_group G] {a b c d : G}
local attribute [simp] add_assoc add_comm add_left_comm sub_eq_add_neg
lemma sub_add_eq_sub_sub (a b c : G) : a - (b + c) = a - b - c :=
by simp
lemma neg_add_eq_sub (a b : G) : -a + b = b - a :=
by simp
lemma sub_add_eq_add_sub (a b c : G) : a - b + c = a + c - b :=
by simp
lemma sub_sub (a b c : G) : a - b - c = a - (b + c) :=
by simp
lemma sub_add (a b c : G) : a - b + c = a - (b - c) :=
by simp
@[simp] lemma add_sub_add_left_eq_sub (a b c : G) : (c + a) - (c + b) = a - b :=
by simp
lemma eq_sub_of_add_eq' (h : c + a = b) : a = b - c :=
by simp [h.symm]
lemma sub_eq_of_eq_add' (h : a = b + c) : a - b = c :=
begin simp [h], rw [add_left_comm], simp end
lemma eq_add_of_sub_eq' (h : a - b = c) : a = b + c :=
by simp [h.symm]
lemma add_eq_of_eq_sub' (h : b = c - a) : a + b = c :=
begin simp [h], rw [add_comm c, add_neg_cancel_left] end
lemma sub_sub_self (a b : G) : a - (a - b) = b :=
begin simp, rw [add_comm b, add_neg_cancel_left] end
lemma add_sub_comm (a b c d : G) : a + b - (c + d) = (a - c) + (b - d) :=
by simp
lemma sub_eq_sub_add_sub (a b c : G) : a - b = c - b + (a - c) :=
begin simp, rw [add_left_comm c], simp end
lemma neg_neg_sub_neg (a b : G) : - (-a - -b) = a - b :=
by simp
@[simp] lemma sub_sub_cancel (a b : G) : a - (a - b) = b := sub_sub_self a b
lemma sub_eq_neg_add (a b : G) : a - b = -b + a :=
add_comm _ _
theorem neg_add' (a b : G) : -(a + b) = -a - b := neg_add a b
@[simp]
lemma neg_sub_neg (a b : G) : -a - -b = b - a :=
by simp [sub_eq_neg_add, add_comm]
lemma eq_sub_iff_add_eq' : a = b - c ↔ c + a = b :=
by rw [eq_sub_iff_add_eq, add_comm]
lemma sub_eq_iff_eq_add' : a - b = c ↔ a = b + c :=
by rw [sub_eq_iff_eq_add, add_comm]
@[simp]
lemma add_sub_cancel' (a b : G) : a + b - a = b :=
by rw [sub_eq_neg_add, neg_add_cancel_left]
@[simp]
lemma add_sub_cancel'_right (a b : G) : a + (b - a) = b :=
by rw [← add_sub_assoc, add_sub_cancel']
-- This lemma is in the `simp` set under the name `add_neg_cancel_comm_assoc`,
-- defined in `algebra/group/commute`
lemma add_add_neg_cancel'_right (a b : G) : a + (b + -a) = b :=
add_sub_cancel'_right a b
lemma sub_right_comm (a b c : G) : a - b - c = a - c - b :=
add_right_comm _ _ _
@[simp] lemma add_add_sub_cancel (a b c : G) : (a + c) + (b - c) = a + b :=
by rw [add_assoc, add_sub_cancel'_right]
@[simp] lemma sub_add_add_cancel (a b c : G) : (a - c) + (b + c) = a + b :=
by rw [add_left_comm, sub_add_cancel, add_comm]
@[simp] lemma sub_add_sub_cancel' (a b c : G) : (a - b) + (c - a) = c - b :=
by rw add_comm; apply sub_add_sub_cancel
@[simp] lemma add_sub_sub_cancel (a b c : G) : (a + b) - (a - c) = b + c :=
by rw [← sub_add, add_sub_cancel']
@[simp] lemma sub_sub_sub_cancel_left (a b c : G) : (c - a) - (c - b) = b - a :=
by rw [← neg_sub b c, sub_neg_eq_add, add_comm, sub_add_sub_cancel]
lemma sub_eq_sub_iff_add_eq_add : a - b = c - d ↔ a + d = c + b :=
begin
rw [sub_eq_iff_eq_add, sub_add_eq_add_sub, eq_comm, sub_eq_iff_eq_add'],
simp only [add_comm, eq_comm]
end
lemma sub_eq_sub_iff_sub_eq_sub : a - b = c - d ↔ a - c = b - d :=
by simp [-sub_eq_add_neg, sub_eq_sub_iff_add_eq_add, add_comm]
end add_comm_group
|
045cdb7f903cb0ac67540f7c4ea8b4465f6bdc4b | 75db7e3219bba2fbf41bf5b905f34fcb3c6ca3f2 | /hott/algebra/category/constructions/cone.hlean | cfa52ac23e0a8cfe763c0864d280b38fa0b237d8 | [
"Apache-2.0"
] | permissive | jroesch/lean | 30ef0860fa905d35b9ad6f76de1a4f65c9af6871 | 3de4ec1a6ce9a960feb2a48eeea8b53246fa34f2 | refs/heads/master | 1,586,090,835,348 | 1,455,142,203,000 | 1,455,142,277,000 | 51,536,958 | 1 | 0 | null | 1,455,215,811,000 | 1,455,215,811,000 | null | UTF-8 | Lean | false | false | 7,291 | 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
Cones of a diagram in a category
-/
import ..nat_trans ..category
open functor nat_trans eq equiv is_trunc is_equiv iso sigma sigma.ops pi
namespace category
structure cone_obj {I C : Precategory} (F : I ⇒ C) :=
(c : C)
(η : constant_functor I c ⟹ F)
variables {I C D : Precategory} {F : I ⇒ C} {x y z : cone_obj F} {i : I}
definition cone_to_obj [unfold 4] := @cone_obj.c
definition cone_to_nat [unfold 4] (c : cone_obj F) : constant_functor I (cone_to_obj c) ⟹ F :=
cone_obj.η c
local attribute cone_to_obj [coercion]
structure cone_hom (x y : cone_obj F) :=
(f : x ⟶ y)
(p : Πi, cone_to_nat y i ∘ f = cone_to_nat x i)
definition cone_to_hom [unfold 6] := @cone_hom.f
definition cone_to_eq [unfold 6] (f : cone_hom x y) (i : I)
: cone_to_nat y i ∘ (cone_to_hom f) = cone_to_nat x i :=
cone_hom.p f i
local attribute cone_to_hom [coercion]
definition cone_id [constructor] (x : cone_obj F) : cone_hom x x :=
cone_hom.mk id
(λi, !id_right)
definition cone_comp [constructor] (g : cone_hom y z) (f : cone_hom x y) : cone_hom x z :=
cone_hom.mk (cone_to_hom g ∘ cone_to_hom f)
abstract λi, by rewrite [assoc, +cone_to_eq] end
definition cone_obj_eq (p : cone_to_obj x = cone_to_obj y)
(q : Πi, cone_to_nat x i = cone_to_nat y i ∘ hom_of_eq p) : x = y :=
begin
induction x, induction y, esimp at *, induction p, apply ap (cone_obj.mk c),
apply nat_trans_eq, intro i, exact q i ⬝ !id_right
end
theorem c_cone_obj_eq (p : cone_to_obj x = cone_to_obj y)
(q : Πi, cone_to_nat x i = cone_to_nat y i ∘ hom_of_eq p) : ap cone_to_obj (cone_obj_eq p q) = p :=
begin
induction x, induction y, esimp at *, induction p,
esimp [cone_obj_eq], rewrite [-ap_compose,↑function.compose,ap_constant]
end
theorem cone_hom_eq {f f' : cone_hom x y} (q : cone_to_hom f = cone_to_hom f') : f = f' :=
begin
induction f, induction f', esimp at *, induction q, apply ap (cone_hom.mk f),
apply @is_hprop.elim, apply pi.is_trunc_pi, intro x, apply is_trunc_eq, -- type class fails
end
variable (F)
definition precategory_cone [instance] [constructor] : precategory (cone_obj F) :=
@precategory.mk _ cone_hom
abstract begin
intro x y,
assert H : cone_hom x y ≃ Σ(f : x ⟶ y), Πi, cone_to_nat y i ∘ f = cone_to_nat x i,
{ fapply equiv.MK,
{ intro f, induction f, constructor, assumption},
{ intro v, induction v, constructor, assumption},
{ intro v, induction v, reflexivity},
{ intro f, induction f, reflexivity}},
apply is_trunc.is_trunc_equiv_closed_rev, exact H,
fapply sigma.is_trunc_sigma, intros,
apply is_trunc_succ, apply pi.is_trunc_pi, intros, esimp,
/-exact _,-/ -- type class inference fails here
apply is_trunc_eq,
end end
(λx y z, cone_comp)
cone_id
abstract begin intros, apply cone_hom_eq, esimp, apply assoc end end
abstract begin intros, apply cone_hom_eq, esimp, apply id_left end end
abstract begin intros, apply cone_hom_eq, esimp, apply id_right end end
definition cone [constructor] : Precategory :=
precategory.Mk (precategory_cone F)
variable {F}
definition cone_iso_pr1 [constructor] (h : x ≅ y) : cone_to_obj x ≅ cone_to_obj y :=
iso.MK
(cone_to_hom (to_hom h))
(cone_to_hom (to_inv h))
(ap cone_to_hom (to_left_inverse h))
(ap cone_to_hom (to_right_inverse h))
definition cone_iso.mk [constructor] (f : cone_to_obj x ≅ cone_to_obj y)
(p : Πi, cone_to_nat y i ∘ to_hom f = cone_to_nat x i) : x ≅ y :=
begin
fapply iso.MK,
{ exact !cone_hom.mk p},
{ fapply cone_hom.mk,
{ exact to_inv f},
{ intro i, apply comp_inverse_eq_of_eq_comp, exact (p i)⁻¹}},
{ apply cone_hom_eq, esimp, apply left_inverse},
{ apply cone_hom_eq, esimp, apply right_inverse},
end
variables (x y)
definition cone_iso_equiv [constructor] : (x ≅ y) ≃ Σ(f : cone_to_obj x ≅ cone_to_obj y),
Πi, cone_to_nat y i ∘ to_hom f = cone_to_nat x i :=
begin
fapply equiv.MK,
{ intro h, exact ⟨cone_iso_pr1 h, cone_to_eq (to_hom h)⟩},
{ intro v, exact cone_iso.mk v.1 v.2},
{ intro v, induction v with f p, fapply sigma_eq: esimp,
{ apply iso_eq, reflexivity},
{ apply is_hprop.elimo, apply is_trunc_pi, intro i, apply is_hprop_hom_eq}},
{ intro h, esimp, apply iso_eq, apply cone_hom_eq, reflexivity},
end
definition cone_eq_equiv : (x = y) ≃ Σ(f : cone_to_obj x = cone_to_obj y),
Πi, cone_to_nat y i ∘ hom_of_eq f = cone_to_nat x i :=
begin
fapply equiv.MK,
{ intro r, fapply sigma.mk, exact ap cone_to_obj r, induction r, intro i, apply id_right},
{ intro v, induction v with p q, induction x with c η, induction y with c' η', esimp at *,
apply cone_obj_eq p, esimp, intro i, exact (q i)⁻¹},
{ intro v, induction v with p q, induction x with c η, induction y with c' η', esimp at *,
induction p, esimp, fapply sigma_eq: esimp,
{ apply c_cone_obj_eq},
{ apply is_hprop.elimo, apply is_trunc_pi, intro i, apply is_hprop_hom_eq}},
{ intro r, induction r, esimp, induction x, esimp, apply ap02, apply is_hprop.elim},
end
section is_univalent
definition is_univalent_cone {I : Precategory} {C : Category} (F : I ⇒ C)
: is_univalent (cone F) :=
begin
intro x y,
fapply is_equiv_of_equiv_of_homotopy,
{ exact calc
(x = y) ≃ (Σ(f : cone_to_obj x = cone_to_obj y), Πi, cone_to_nat y i ∘ hom_of_eq f = cone_to_nat x i)
: cone_eq_equiv
... ≃ (Σ(f : cone_to_obj x ≅ cone_to_obj y), Πi, cone_to_nat y i ∘ to_hom f = cone_to_nat x i)
: sigma_equiv_sigma !eq_equiv_iso (λa, !equiv.refl)
... ≃ (x ≅ y) : cone_iso_equiv },
{ intro p, induction p, esimp [equiv.trans,equiv.symm], esimp [sigma_functor],
apply iso_eq, reflexivity}
end
definition category_cone [instance] [constructor] {I : Precategory} {C : Category} (F : I ⇒ C)
: category (cone_obj F) :=
category.mk _ (is_univalent_cone F)
definition Category_cone [constructor] {I : Precategory} {C : Category} (F : I ⇒ C)
: Category :=
Category.mk _ (category_cone F)
end is_univalent
definition cone_obj_compose [constructor] (G : C ⇒ D) (x : cone_obj F) : cone_obj (G ∘f F) :=
begin
fapply cone_obj.mk,
{ exact G x},
{ fapply change_natural_map,
{ refine ((G ∘fn cone_to_nat x) ∘n _), apply nat_trans_of_eq, fapply functor_eq: esimp,
intro i j k, esimp, rewrite [id_leftright,respect_id]},
{ intro i, esimp, exact G (cone_to_nat x i)},
{ intro i, esimp, rewrite [ap010_functor_eq, ▸*, id_right]}}
end
end category
|
b844c65c76d12e03e992b96e5bf00d22005b5f3b | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/data/matrix/pequiv_auto.lean | c0a3a959d6fc74a134ed80a3af0e9e7da9d5f59a | [] | no_license | AurelienSaue/Mathlib4_auto | f538cfd0980f65a6361eadea39e6fc639e9dae14 | 590df64109b08190abe22358fabc3eae000943f2 | refs/heads/master | 1,683,906,849,776 | 1,622,564,669,000 | 1,622,564,669,000 | 371,723,747 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 5,675 | lean | /-
Copyright (c) 2019 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.data.matrix.basic
import Mathlib.data.pequiv
import Mathlib.PostPort
universes u_3 u_4 v u_2 u_1
namespace Mathlib
/-
# partial equivalences for matrices
Using partial equivalences to represent matrices.
This file introduces the function `pequiv.to_matrix`, which returns a matrix containing ones and
zeros. For any partial equivalence `f`, `f.to_matrix i j = 1 ↔ f i = some j`.
The following important properties of this function are proved
`to_matrix_trans : (f.trans g).to_matrix = f.to_matrix ⬝ g.to_matrix`
`to_matrix_symm : f.symm.to_matrix = f.to_matrixᵀ`
`to_matrix_refl : (pequiv.refl n).to_matrix = 1`
`to_matrix_bot : ⊥.to_matrix = 0`
This theory gives the matrix representation of projection linear maps, and their right inverses.
For example, the matrix `(single (0 : fin 1) (i : fin n)).to_matrix` corresponds to the the ith
projection map from R^n to R.
Any injective function `fin m → fin n` gives rise to a `pequiv`, whose matrix is the projection
map from R^m → R^n represented by the same function. The transpose of this matrix is the right
inverse of this map, sending anything not in the image to zero.
## notations
This file uses the notation ` ⬝ ` for `matrix.mul` and `ᵀ` for `matrix.transpose`.
-/
namespace pequiv
/-- `to_matrix` returns a matrix containing ones and zeros. `f.to_matrix i j` is `1` if
`f i = some j` and `0` otherwise -/
def to_matrix {m : Type u_3} {n : Type u_4} [fintype m] [fintype n] {α : Type v} [DecidableEq n]
[HasZero α] [HasOne α] (f : m ≃. n) : matrix m n α :=
sorry
theorem mul_matrix_apply {l : Type u_2} {m : Type u_3} {n : Type u_4} [fintype l] [fintype m]
[fintype n] {α : Type v} [DecidableEq m] [semiring α] (f : l ≃. m) (M : matrix m n α) (i : l)
(j : n) :
matrix.mul (to_matrix f) M i j = option.cases_on (coe_fn f i) 0 fun (fi : m) => M fi j :=
sorry
theorem to_matrix_symm {m : Type u_3} {n : Type u_4} [fintype m] [fintype n] {α : Type v}
[DecidableEq m] [DecidableEq n] [HasZero α] [HasOne α] (f : m ≃. n) :
to_matrix (pequiv.symm f) = matrix.transpose (to_matrix f) :=
sorry
@[simp] theorem to_matrix_refl {n : Type u_4} [fintype n] {α : Type v} [DecidableEq n] [HasZero α]
[HasOne α] : to_matrix (pequiv.refl n) = 1 :=
sorry
theorem matrix_mul_apply {l : Type u_2} {m : Type u_3} {n : Type u_4} [fintype l] [fintype m]
[fintype n] {α : Type v} [semiring α] [DecidableEq n] (M : matrix l m α) (f : m ≃. n) (i : l)
(j : n) :
matrix.mul M (to_matrix f) i j =
option.cases_on (coe_fn (pequiv.symm f) j) 0 fun (fj : m) => M i fj :=
sorry
theorem to_pequiv_mul_matrix {m : Type u_3} {n : Type u_4} [fintype m] [fintype n] {α : Type v}
[DecidableEq m] [semiring α] (f : m ≃ m) (M : matrix m n α) :
matrix.mul (to_matrix (equiv.to_pequiv f)) M = fun (i : m) => M (coe_fn f i) :=
sorry
theorem to_matrix_trans {l : Type u_2} {m : Type u_3} {n : Type u_4} [fintype l] [fintype m]
[fintype n] {α : Type v} [DecidableEq m] [DecidableEq n] [semiring α] (f : l ≃. m)
(g : m ≃. n) : to_matrix (pequiv.trans f g) = matrix.mul (to_matrix f) (to_matrix g) :=
sorry
@[simp] theorem to_matrix_bot {m : Type u_3} {n : Type u_4} [fintype m] [fintype n] {α : Type v}
[DecidableEq n] [HasZero α] [HasOne α] : to_matrix ⊥ = 0 :=
rfl
theorem to_matrix_injective {m : Type u_3} {n : Type u_4} [fintype m] [fintype n] {α : Type v}
[DecidableEq n] [monoid_with_zero α] [nontrivial α] : function.injective to_matrix :=
sorry
theorem to_matrix_swap {n : Type u_4} [fintype n] {α : Type v} [DecidableEq n] [ring α] (i : n)
(j : n) :
to_matrix (equiv.to_pequiv (equiv.swap i j)) =
1 - to_matrix (single i i) - to_matrix (single j j) + to_matrix (single i j) +
to_matrix (single j i) :=
sorry
@[simp] theorem single_mul_single {k : Type u_1} {m : Type u_3} {n : Type u_4} [fintype k]
[fintype m] [fintype n] {α : Type v} [DecidableEq k] [DecidableEq m] [DecidableEq n]
[semiring α] (a : m) (b : n) (c : k) :
matrix.mul (to_matrix (single a b)) (to_matrix (single b c)) = to_matrix (single a c) :=
sorry
theorem single_mul_single_of_ne {k : Type u_1} {m : Type u_3} {n : Type u_4} [fintype k] [fintype m]
[fintype n] {α : Type v} [DecidableEq k] [DecidableEq m] [DecidableEq n] [semiring α] {b₁ : n}
{b₂ : n} (hb : b₁ ≠ b₂) (a : m) (c : k) :
matrix.mul (to_matrix (single a b₁)) (to_matrix (single b₂ c)) = 0 :=
sorry
/-- Restatement of `single_mul_single`, which will simplify expressions in `simp` normal form,
when associativity may otherwise need to be carefully applied. -/
@[simp] theorem single_mul_single_right {k : Type u_1} {l : Type u_2} {m : Type u_3} {n : Type u_4}
[fintype k] [fintype l] [fintype m] [fintype n] {α : Type v} [DecidableEq k] [DecidableEq m]
[DecidableEq n] [semiring α] (a : m) (b : n) (c : k) (M : matrix k l α) :
matrix.mul (to_matrix (single a b)) (matrix.mul (to_matrix (single b c)) M) =
matrix.mul (to_matrix (single a c)) M :=
sorry
/-- We can also define permutation matrices by permuting the rows of the identity matrix. -/
theorem equiv_to_pequiv_to_matrix {n : Type u_4} [fintype n] {α : Type v} [DecidableEq n]
[HasZero α] [HasOne α] (σ : n ≃ n) (i : n) (j : n) :
to_matrix (equiv.to_pequiv σ) i j = HasOne.one (coe_fn σ i) j :=
if_congr option.some_inj rfl rfl
end Mathlib |
88bc4bc851450717084fa5e4dd0b6d9d948bffd7 | 626e312b5c1cb2d88fca108f5933076012633192 | /src/number_theory/padics/padic_norm.lean | 0436a5cabb38c5f9592c2f65ef76d3686c922602 | [
"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 | 27,984 | lean | /-
Copyright (c) 2018 Robert Y. Lewis. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Robert Y. Lewis
-/
import ring_theory.int.basic
import algebra.field_power
import ring_theory.multiplicity
import data.real.cau_seq
import tactic.ring_exp
import tactic.basic
/-!
# p-adic norm
This file defines the p-adic valuation and the p-adic norm on ℚ.
The p-adic valuation on ℚ is the difference of the multiplicities of `p` in the numerator and
denominator of `q`. This function obeys the standard properties of a valuation, with the appropriate
assumptions on p.
The valuation induces a norm on ℚ. This norm is a nonarchimedean absolute value.
It takes values in {0} ∪ {1/p^k | k ∈ ℤ}.
## Notations
This file uses the local notation `/.` for `rat.mk`.
## Implementation notes
Much, but not all, of this file assumes that `p` is prime. This assumption is inferred automatically
by taking `[fact (prime p)]` as a type class argument.
## References
* [F. Q. Gouêva, *p-adic numbers*][gouvea1997]
* [R. Y. Lewis, *A formal proof of Hensel's lemma over the p-adic integers*][lewis2019]
* <https://en.wikipedia.org/wiki/P-adic_number>
## Tags
p-adic, p adic, padic, norm, valuation
-/
universe u
open nat
open_locale rat
open multiplicity
/--
For `p ≠ 1`, the p-adic valuation of an integer `z ≠ 0` is the largest natural number `n` such that
p^n divides z.
`padic_val_rat` defines the valuation of a rational `q` to be the valuation of `q.num` minus the
valuation of `q.denom`.
If `q = 0` or `p = 1`, then `padic_val_rat p q` defaults to 0.
-/
def padic_val_rat (p : ℕ) (q : ℚ) : ℤ :=
if h : q ≠ 0 ∧ p ≠ 1
then (multiplicity (p : ℤ) q.num).get
(multiplicity.finite_int_iff.2 ⟨h.2, rat.num_ne_zero_of_ne_zero h.1⟩) -
(multiplicity (p : ℤ) q.denom).get
(multiplicity.finite_int_iff.2 ⟨h.2, by exact_mod_cast rat.denom_ne_zero _⟩)
else 0
/--
A simplification of the definition of `padic_val_rat p q` when `q ≠ 0` and `p` is prime.
-/
lemma padic_val_rat_def (p : ℕ) [hp : fact p.prime] {q : ℚ} (hq : q ≠ 0) : padic_val_rat p q =
(multiplicity (p : ℤ) q.num).get (finite_int_iff.2 ⟨hp.1.ne_one, rat.num_ne_zero_of_ne_zero hq⟩) -
(multiplicity (p : ℤ) q.denom).get
(finite_int_iff.2 ⟨hp.1.ne_one, by exact_mod_cast rat.denom_ne_zero _⟩) :=
dif_pos ⟨hq, hp.1.ne_one⟩
namespace padic_val_rat
open multiplicity
variables {p : ℕ}
/--
`padic_val_rat p q` is symmetric in `q`.
-/
@[simp] protected lemma neg (q : ℚ) : padic_val_rat p (-q) = padic_val_rat p q :=
begin
unfold padic_val_rat,
split_ifs,
{ simp [-add_comm]; refl },
{ exfalso, simp * at * },
{ exfalso, simp * at * },
{ refl }
end
/--
`padic_val_rat p 1` is 0 for any `p`.
-/
@[simp] protected lemma one : padic_val_rat p 1 = 0 :=
by unfold padic_val_rat; split_ifs; simp *
/--
For `p ≠ 0, p ≠ 1, `padic_val_rat p p` is 1.
-/
@[simp] lemma padic_val_rat_self (hp : 1 < p) : padic_val_rat p p = 1 :=
by unfold padic_val_rat; split_ifs; simp [*, nat.one_lt_iff_ne_zero_and_ne_one] at *
/--
The p-adic value of an integer `z ≠ 0` is the multiplicity of `p` in `z`.
-/
lemma padic_val_rat_of_int (z : ℤ) (hp : p ≠ 1) (hz : z ≠ 0) :
padic_val_rat p (z : ℚ) = (multiplicity (p : ℤ) z).get
(finite_int_iff.2 ⟨hp, hz⟩) :=
by rw [padic_val_rat, dif_pos]; simp *; refl
end padic_val_rat
/--
A convenience function for the case of `padic_val_rat` when both inputs are natural numbers.
-/
def padic_val_nat (p : ℕ) (n : ℕ) : ℕ :=
int.to_nat (padic_val_rat p n)
section padic_val_nat
/--
`padic_val_nat` is defined as an `int.to_nat` cast;
this lemma ensures that the cast is well-behaved.
-/
lemma zero_le_padic_val_rat_of_nat (p n : ℕ) : 0 ≤ padic_val_rat p n :=
begin
unfold padic_val_rat,
split_ifs,
{ simp, },
{ trivial, },
end
/--
`padic_val_rat` coincides with `padic_val_nat`.
-/
@[simp, norm_cast] lemma padic_val_rat_of_nat (p n : ℕ) :
↑(padic_val_nat p n) = padic_val_rat p n :=
begin
unfold padic_val_nat,
rw int.to_nat_of_nonneg (zero_le_padic_val_rat_of_nat p n),
end
/--
A simplification of `padic_val_nat` when one input is prime, by analogy with `padic_val_rat_def`.
-/
lemma padic_val_nat_def {p : ℕ} [hp : fact p.prime] {n : ℕ} (hn : n ≠ 0) :
padic_val_nat p n =
(multiplicity p n).get
(multiplicity.finite_nat_iff.2 ⟨nat.prime.ne_one hp.1, bot_lt_iff_ne_bot.mpr hn⟩) :=
begin
have n_nonzero : (n : ℚ) ≠ 0, by simpa only [cast_eq_zero, ne.def],
-- Infinite loop with @simp padic_val_rat_of_nat unless we restrict the available lemmas here,
-- hence the very long list
simpa only
[ int.coe_nat_multiplicity p n, rat.coe_nat_denom n, (padic_val_rat_of_nat p n).symm,
int.coe_nat_zero, int.coe_nat_inj', sub_zero, get_one_right, int.coe_nat_succ, zero_add,
rat.coe_nat_num ]
using padic_val_rat_def p n_nonzero,
end
lemma one_le_padic_val_nat_of_dvd
{n p : nat} [prime : fact p.prime] (nonzero : n ≠ 0) (div : p ∣ n) :
1 ≤ padic_val_nat p n :=
begin
rw @padic_val_nat_def _ prime _ nonzero,
let one_le_mul : _ ≤ multiplicity p n :=
@multiplicity.le_multiplicity_of_pow_dvd _ _ _ p n 1 (begin norm_num, exact div end),
simp only [enat.coe_one] at one_le_mul,
rcases one_le_mul with ⟨_, q⟩,
dsimp at q,
solve_by_elim,
end
@[simp]
lemma padic_val_nat_zero (m : nat) : padic_val_nat m 0 = 0 := by simpa
@[simp]
lemma padic_val_nat_one (m : nat) : padic_val_nat m 1 = 0 := by simp [padic_val_nat]
end padic_val_nat
namespace padic_val_rat
open multiplicity
variables (p : ℕ) [p_prime : fact p.prime]
include p_prime
/--
The multiplicity of `p : ℕ` in `a : ℤ` is finite exactly when `a ≠ 0`.
-/
lemma finite_int_prime_iff {p : ℕ} [p_prime : fact p.prime] {a : ℤ} : finite (p : ℤ) a ↔ a ≠ 0 :=
by simp [finite_int_iff, ne.symm (ne_of_lt (p_prime.1.one_lt))]
/--
A rewrite lemma for `padic_val_rat p q` when `q` is expressed in terms of `rat.mk`.
-/
protected lemma defn {q : ℚ} {n d : ℤ} (hqz : q ≠ 0) (qdf : q = n /. d) :
padic_val_rat p q = (multiplicity (p : ℤ) n).get (finite_int_iff.2
⟨ne.symm $ ne_of_lt p_prime.1.one_lt, λ hn, by simp * at *⟩) -
(multiplicity (p : ℤ) d).get (finite_int_iff.2 ⟨ne.symm $ ne_of_lt p_prime.1.one_lt,
λ hd, by simp * at *⟩) :=
have hn : n ≠ 0, from rat.mk_num_ne_zero_of_ne_zero hqz qdf,
have hd : d ≠ 0, from rat.mk_denom_ne_zero_of_ne_zero hqz qdf,
let ⟨c, hc1, hc2⟩ := rat.num_denom_mk hn hd qdf in
by rw [padic_val_rat, dif_pos];
simp [hc1, hc2, multiplicity.mul' (nat.prime_iff_prime_int.1 p_prime.1),
(ne.symm (ne_of_lt p_prime.1.one_lt)), hqz]
/--
A rewrite lemma for `padic_val_rat p (q * r)` with conditions `q ≠ 0`, `r ≠ 0`.
-/
protected lemma mul {q r : ℚ} (hq : q ≠ 0) (hr : r ≠ 0) :
padic_val_rat p (q * r) = padic_val_rat p q + padic_val_rat p r :=
have q*r = (q.num * r.num) /. (↑q.denom * ↑r.denom), by rw_mod_cast rat.mul_num_denom,
have hq' : q.num /. q.denom ≠ 0, by rw rat.num_denom; exact hq,
have hr' : r.num /. r.denom ≠ 0, by rw rat.num_denom; exact hr,
have hp' : _root_.prime (p : ℤ), from nat.prime_iff_prime_int.1 p_prime.1,
begin
rw [padic_val_rat.defn p (mul_ne_zero hq hr) this],
conv_rhs { rw [←(@rat.num_denom q), padic_val_rat.defn p hq',
←(@rat.num_denom r), padic_val_rat.defn p hr'] },
rw [multiplicity.mul' hp', multiplicity.mul' hp']; simp [add_comm, add_left_comm, sub_eq_add_neg]
end
/--
A rewrite lemma for `padic_val_rat p (q^k) with condition `q ≠ 0`.
-/
protected lemma pow {q : ℚ} (hq : q ≠ 0) {k : ℕ} :
padic_val_rat p (q ^ k) = k * padic_val_rat p q :=
by induction k; simp [*, padic_val_rat.mul _ hq (pow_ne_zero _ hq),
pow_succ, add_mul, add_comm]
/--
A rewrite lemma for `padic_val_rat p (q⁻¹)` with condition `q ≠ 0`.
-/
protected lemma inv {q : ℚ} (hq : q ≠ 0) :
padic_val_rat p (q⁻¹) = -padic_val_rat p q :=
by rw [eq_neg_iff_add_eq_zero, ← padic_val_rat.mul p (inv_ne_zero hq) hq,
inv_mul_cancel hq, padic_val_rat.one]
/--
A rewrite lemma for `padic_val_rat p (q / r)` with conditions `q ≠ 0`, `r ≠ 0`.
-/
protected lemma div {q r : ℚ} (hq : q ≠ 0) (hr : r ≠ 0) :
padic_val_rat p (q / r) = padic_val_rat p q - padic_val_rat p r :=
by rw [div_eq_mul_inv, padic_val_rat.mul p hq (inv_ne_zero hr),
padic_val_rat.inv p hr, sub_eq_add_neg]
/--
A condition for `padic_val_rat p (n₁ / d₁) ≤ padic_val_rat p (n₂ / d₂),
in terms of divisibility by `p^n`.
-/
lemma padic_val_rat_le_padic_val_rat_iff {n₁ n₂ d₁ d₂ : ℤ}
(hn₁ : n₁ ≠ 0) (hn₂ : n₂ ≠ 0) (hd₁ : d₁ ≠ 0) (hd₂ : d₂ ≠ 0) :
padic_val_rat p (n₁ /. d₁) ≤ padic_val_rat p (n₂ /. d₂) ↔
∀ (n : ℕ), ↑p ^ n ∣ n₁ * d₂ → ↑p ^ n ∣ n₂ * d₁ :=
have hf1 : finite (p : ℤ) (n₁ * d₂),
from finite_int_prime_iff.2 (mul_ne_zero hn₁ hd₂),
have hf2 : finite (p : ℤ) (n₂ * d₁),
from finite_int_prime_iff.2 (mul_ne_zero hn₂ hd₁),
by conv {
to_lhs,
rw [padic_val_rat.defn p (rat.mk_ne_zero_of_ne_zero hn₁ hd₁) rfl,
padic_val_rat.defn p (rat.mk_ne_zero_of_ne_zero hn₂ hd₂) rfl,
sub_le_iff_le_add',
← add_sub_assoc,
le_sub_iff_add_le],
norm_cast,
rw [← multiplicity.mul' (nat.prime_iff_prime_int.1 p_prime.1) hf1, add_comm,
← multiplicity.mul' (nat.prime_iff_prime_int.1 p_prime.1) hf2,
enat.get_le_get, multiplicity_le_multiplicity_iff] }
/--
Sufficient conditions to show that the p-adic valuation of `q` is less than or equal to the
p-adic vlauation of `q + r`.
-/
theorem le_padic_val_rat_add_of_le {q r : ℚ}
(hq : q ≠ 0) (hr : r ≠ 0) (hqr : q + r ≠ 0)
(h : padic_val_rat p q ≤ padic_val_rat p r) :
padic_val_rat p q ≤ padic_val_rat p (q + r) :=
have hqn : q.num ≠ 0, from rat.num_ne_zero_of_ne_zero hq,
have hqd : (q.denom : ℤ) ≠ 0, by exact_mod_cast rat.denom_ne_zero _,
have hrn : r.num ≠ 0, from rat.num_ne_zero_of_ne_zero hr,
have hrd : (r.denom : ℤ) ≠ 0, by exact_mod_cast rat.denom_ne_zero _,
have hqreq : q + r = (((q.num * r.denom + q.denom * r.num : ℤ)) /. (↑q.denom * ↑r.denom : ℤ)),
from rat.add_num_denom _ _,
have hqrd : q.num * ↑(r.denom) + ↑(q.denom) * r.num ≠ 0,
from rat.mk_num_ne_zero_of_ne_zero hqr hqreq,
begin
conv_lhs { rw ←(@rat.num_denom q) },
rw [hqreq, padic_val_rat_le_padic_val_rat_iff p hqn hqrd hqd (mul_ne_zero hqd hrd),
← multiplicity_le_multiplicity_iff, mul_left_comm,
multiplicity.mul (nat.prime_iff_prime_int.1 p_prime.1), add_mul],
rw [←(@rat.num_denom q), ←(@rat.num_denom r),
padic_val_rat_le_padic_val_rat_iff p hqn hrn hqd hrd, ← multiplicity_le_multiplicity_iff] at h,
calc _ ≤ min (multiplicity ↑p (q.num * ↑(r.denom) * ↑(q.denom)))
(multiplicity ↑p (↑(q.denom) * r.num * ↑(q.denom))) : (le_min
(by rw [@multiplicity.mul _ _ _ _ (_ * _) _ (nat.prime_iff_prime_int.1 p_prime.1), add_comm])
(by rw [mul_assoc, @multiplicity.mul _ _ _ _ (q.denom : ℤ)
(_ * _) (nat.prime_iff_prime_int.1 p_prime.1)];
exact add_le_add_left h _))
... ≤ _ : min_le_multiplicity_add
end
/--
The minimum of the valuations of `q` and `r` is less than or equal to the valuation of `q + r`.
-/
theorem min_le_padic_val_rat_add {q r : ℚ}
(hq : q ≠ 0) (hr : r ≠ 0) (hqr : q + r ≠ 0) :
min (padic_val_rat p q) (padic_val_rat p r) ≤ padic_val_rat p (q + r) :=
(le_total (padic_val_rat p q) (padic_val_rat p r)).elim
(λ h, by rw [min_eq_left h]; exact le_padic_val_rat_add_of_le _ hq hr hqr h)
(λ h, by rw [min_eq_right h, add_comm]; exact le_padic_val_rat_add_of_le _ hr hq
(by rwa add_comm) h)
open_locale big_operators
/-- A finite sum of rationals with positive p-adic valuation has positive p-adic valuation
(if the sum is non-zero). -/
theorem sum_pos_of_pos {n : ℕ} {F : ℕ → ℚ}
(hF : ∀ i, i < n → 0 < padic_val_rat p (F i)) (hn0 : ∑ i in finset.range n, F i ≠ 0) :
0 < padic_val_rat p (∑ i in finset.range n, F i) :=
begin
induction n with d hd,
{ exact false.elim (hn0 rfl) },
{ rw finset.sum_range_succ at hn0 ⊢,
by_cases h : ∑ (x : ℕ) in finset.range d, F x = 0,
{ rw [h, zero_add],
exact hF d (lt_add_one _) },
{ refine lt_of_lt_of_le _ (min_le_padic_val_rat_add p h (λ h1, _) hn0),
{ refine lt_min (hd (λ i hi, _) h) (hF d (lt_add_one _)),
exact hF _ (lt_trans hi (lt_add_one _)) },
{ have h2 := hF d (lt_add_one _),
rw h1 at h2,
exact lt_irrefl _ h2 } } }
end
end padic_val_rat
namespace padic_val_nat
/--
A rewrite lemma for `padic_val_nat p (q * r)` with conditions `q ≠ 0`, `r ≠ 0`.
-/
protected lemma mul (p : ℕ) [p_prime : fact p.prime] {q r : ℕ} (hq : q ≠ 0) (hr : r ≠ 0) :
padic_val_nat p (q * r) = padic_val_nat p q + padic_val_nat p r :=
begin
apply int.coe_nat_inj,
simp only [padic_val_rat_of_nat, nat.cast_mul],
rw padic_val_rat.mul,
norm_cast,
exact cast_ne_zero.mpr hq,
exact cast_ne_zero.mpr hr,
end
/--
Dividing out by a prime factor reduces the padic_val_nat by 1.
-/
protected lemma div {p : ℕ} [p_prime : fact p.prime] {b : ℕ} (dvd : p ∣ b) :
(padic_val_nat p (b / p)) = (padic_val_nat p b) - 1 :=
begin
by_cases b_split : (b = 0),
{ simp [b_split], },
{ have split_frac : padic_val_rat p (b / p) = padic_val_rat p b - padic_val_rat p p :=
padic_val_rat.div p (nat.cast_ne_zero.mpr b_split)
(nat.cast_ne_zero.mpr (nat.prime.ne_zero p_prime.1)),
rw padic_val_rat.padic_val_rat_self (nat.prime.one_lt p_prime.1) at split_frac,
have r : 1 ≤ padic_val_nat p b := one_le_padic_val_nat_of_dvd b_split dvd,
exact_mod_cast split_frac, }
end
/-- A version of `padic_val_rat.pow` for `padic_val_nat` -/
protected lemma pow (p q n : ℕ) [fact p.prime] (hq : q ≠ 0) :
padic_val_nat p (q ^ n) = n * padic_val_nat p q :=
begin
apply @nat.cast_injective ℤ,
push_cast,
exact padic_val_rat.pow _ (cast_ne_zero.mpr hq),
end
end padic_val_nat
section padic_val_nat
/--
If a prime doesn't appear in `n`, `padic_val_nat p n` is `0`.
-/
lemma padic_val_nat_of_not_dvd {p : ℕ} [fact p.prime] {n : ℕ} (not_dvd : ¬(p ∣ n)) :
padic_val_nat p n = 0 :=
begin
by_cases hn : n = 0,
{ subst hn, simp at not_dvd, trivial, },
{ rw padic_val_nat_def hn,
exact (@multiplicity.unique' _ _ _ p n 0 (by simp) (by simpa using not_dvd)).symm,
assumption, },
end
lemma dvd_of_one_le_padic_val_nat {n p : nat} [prime : fact p.prime] (hp : 1 ≤ padic_val_nat p n) :
p ∣ n :=
begin
by_contra h,
rw padic_val_nat_of_not_dvd h at hp,
exact lt_irrefl 0 (lt_of_lt_of_le zero_lt_one hp),
end
lemma pow_padic_val_nat_dvd {p n : ℕ} [fact (nat.prime p)] : p ^ (padic_val_nat p n) ∣ n :=
begin
cases nat.eq_zero_or_pos n with hn hn,
{ rw hn, exact dvd_zero (p ^ padic_val_nat p 0) },
{ rw multiplicity.pow_dvd_iff_le_multiplicity,
apply le_of_eq,
rw padic_val_nat_def (ne_of_gt hn),
{ apply enat.coe_get },
{ apply_instance } }
end
lemma pow_succ_padic_val_nat_not_dvd {p n : ℕ} [hp : fact (nat.prime p)] (hn : 0 < n) :
¬ p ^ (padic_val_nat p n + 1) ∣ n :=
begin
{ rw multiplicity.pow_dvd_iff_le_multiplicity,
rw padic_val_nat_def (ne_of_gt hn),
{ rw [enat.coe_add, enat.coe_get],
simp only [enat.coe_one, not_le],
apply enat.lt_add_one (ne_top_iff_finite.2 (finite_nat_iff.2 ⟨hp.elim.ne_one, hn⟩)) },
{ apply_instance } }
end
lemma padic_val_nat_primes {p q : ℕ} [p_prime : fact p.prime] [q_prime : fact q.prime]
(neq : p ≠ q) : padic_val_nat p q = 0 :=
@padic_val_nat_of_not_dvd p p_prime q $
(not_congr (iff.symm (prime_dvd_prime_iff_eq p_prime.1 q_prime.1))).mp neq
protected lemma padic_val_nat.div' {p : ℕ} [p_prime : fact p.prime] :
∀ {m : ℕ} (cpm : coprime p m) {b : ℕ} (dvd : m ∣ b), padic_val_nat p (b / m) = padic_val_nat p b
| 0 := λ cpm b dvd, by { rw zero_dvd_iff at dvd, rw [dvd, nat.zero_div], }
| (n + 1) :=
λ cpm b dvd,
begin
rcases dvd with ⟨c, rfl⟩,
rw [mul_div_right c (nat.succ_pos _)],by_cases hc : c = 0,
{ rw [hc, mul_zero] },
{ rw padic_val_nat.mul,
{ suffices : ¬ p ∣ (n+1),
{ rw [padic_val_nat_of_not_dvd this, zero_add] },
contrapose! cpm,
exact p_prime.1.dvd_iff_not_coprime.mp cpm },
{ exact nat.succ_ne_zero _ },
{ exact hc } },
end
lemma padic_val_nat_eq_factors_count (p : ℕ) [hp : fact p.prime] :
∀ (n : ℕ), padic_val_nat p n = (factors n).count p
| 0 := by simp
| 1 := by simp
| (m + 2) :=
let n := m + 2 in
let q := min_fac n in
have hq : fact q.prime := ⟨min_fac_prime (show m + 2 ≠ 1, by linarith)⟩,
have wf : n / q < n := nat.div_lt_self (nat.succ_pos _) hq.1.one_lt,
begin
rw factors_add_two,
show padic_val_nat p n = list.count p (q :: (factors (n / q))),
rw [list.count_cons', ← padic_val_nat_eq_factors_count],
split_ifs with h,
have p_dvd_n : p ∣ n,
{ have: q ∣ n := nat.min_fac_dvd n,
cc },
{ rw [←h, padic_val_nat.div],
{ have: 1 ≤ padic_val_nat p n := one_le_padic_val_nat_of_dvd (by linarith) p_dvd_n,
exact (nat.sub_eq_iff_eq_add this).mp rfl, },
{ exact p_dvd_n, }, },
{ suffices : p.coprime q,
{ rw [padic_val_nat.div' this (min_fac_dvd n), add_zero], },
rwa nat.coprime_primes hp.1 hq.1, },
end
@[simp] lemma padic_val_nat_self (p : ℕ) [fact p.prime] : padic_val_nat p p = 1 :=
by simp [padic_val_nat_def (fact.out p.prime).ne_zero]
@[simp] lemma padic_val_nat_prime_pow (p n : ℕ) [fact p.prime] : padic_val_nat p (p ^ n) = n :=
by rw [padic_val_nat.pow p _ _ (fact.out p.prime).ne_zero, padic_val_nat_self p, mul_one]
open_locale big_operators
lemma prod_pow_prime_padic_val_nat (n : nat) (hn : n ≠ 0) (m : nat) (pr : n < m) :
∏ p in finset.filter nat.prime (finset.range m), p ^ (padic_val_nat p n) = n :=
begin
rw ← pos_iff_ne_zero at hn,
have H : (factors n : multiset ℕ).prod = n,
{ rw [multiset.coe_prod, prod_factors hn], },
rw finset.prod_multiset_count at H,
conv_rhs { rw ← H, },
refine finset.prod_bij_ne_one (λ p hp hp', p) _ _ _ _,
{ rintro p hp hpn,
rw [finset.mem_filter, finset.mem_range] at hp,
rw [multiset.mem_to_finset, multiset.mem_coe, mem_factors_iff_dvd hn hp.2],
contrapose! hpn,
haveI Hp : fact p.prime := ⟨hp.2⟩,
rw [padic_val_nat_of_not_dvd hpn, pow_zero], },
{ intros, assumption },
{ intros p hp hpn,
rw [multiset.mem_to_finset, multiset.mem_coe] at hp,
haveI Hp : fact p.prime := ⟨prime_of_mem_factors hp⟩,
simp only [exists_prop, ne.def, finset.mem_filter, finset.mem_range],
refine ⟨p, ⟨_, Hp.1⟩, ⟨_, rfl⟩⟩,
{ rw mem_factors_iff_dvd hn Hp.1 at hp, exact lt_of_le_of_lt (le_of_dvd hn hp) pr },
{ rw padic_val_nat_eq_factors_count,
simpa [ne.def, multiset.coe_count] using hpn } },
{ intros p hp hpn,
rw [finset.mem_filter, finset.mem_range] at hp,
haveI Hp : fact p.prime := ⟨hp.2⟩,
rw [padic_val_nat_eq_factors_count, multiset.coe_count] }
end
end padic_val_nat
/--
If `q ≠ 0`, the p-adic norm of a rational `q` is `p ^ (-(padic_val_rat p q))`.
If `q = 0`, the p-adic norm of `q` is 0.
-/
def padic_norm (p : ℕ) (q : ℚ) : ℚ :=
if q = 0 then 0 else (↑p : ℚ) ^ (-(padic_val_rat p q))
namespace padic_norm
section padic_norm
open padic_val_rat
variables (p : ℕ)
/--
Unfolds the definition of the p-adic norm of `q` when `q ≠ 0`.
-/
@[simp] protected lemma eq_fpow_of_nonzero {q : ℚ} (hq : q ≠ 0) :
padic_norm p q = p ^ (-(padic_val_rat p q)) :=
by simp [hq, padic_norm]
/--
The p-adic norm is nonnegative.
-/
protected lemma nonneg (q : ℚ) : 0 ≤ padic_norm p q :=
if hq : q = 0 then by simp [hq, padic_norm]
else
begin
unfold padic_norm; split_ifs,
apply fpow_nonneg,
exact_mod_cast nat.zero_le _
end
/--
The p-adic norm of 0 is 0.
-/
@[simp] protected lemma zero : padic_norm p 0 = 0 := by simp [padic_norm]
/--
The p-adic norm of 1 is 1.
-/
@[simp] protected lemma one : padic_norm p 1 = 1 := by simp [padic_norm]
/--
The p-adic norm of `p` is `1/p` if `p > 1`.
See also `padic_norm.padic_norm_p_of_prime` for a version that assumes `p` is prime.
-/
lemma padic_norm_p {p : ℕ} (hp : 1 < p) : padic_norm p p = 1 / p :=
by simp [padic_norm, (show p ≠ 0, by linarith), padic_val_rat.padic_val_rat_self hp]
/--
The p-adic norm of `p` is `1/p` if `p` is prime.
See also `padic_norm.padic_norm_p` for a version that assumes `1 < p`.
-/
@[simp] lemma padic_norm_p_of_prime (p : ℕ) [fact p.prime] : padic_norm p p = 1 / p :=
padic_norm_p $ nat.prime.one_lt (fact.out _)
/-- The p-adic norm of `q` is `1` if `q` is prime and not equal to `p`. -/
lemma padic_norm_of_prime_of_ne {p q : ℕ} [p_prime : fact p.prime] [q_prime : fact q.prime]
(neq : p ≠ q) : padic_norm p q = 1 :=
begin
have p : padic_val_rat p q = 0,
{ exact_mod_cast @padic_val_nat_primes p q p_prime q_prime neq },
simp [padic_norm, p, q_prime.1.1, q_prime.1.ne_zero],
end
/--
The p-adic norm of `p` is less than 1 if `1 < p`.
See also `padic_norm.padic_norm_p_lt_one_of_prime` for a version assuming `prime p`.
-/
lemma padic_norm_p_lt_one {p : ℕ} (hp : 1 < p) : padic_norm p p < 1 :=
begin
rw [padic_norm_p hp, div_lt_iff, one_mul],
{ exact_mod_cast hp },
{ exact_mod_cast zero_lt_one.trans hp },
end
/--
The p-adic norm of `p` is less than 1 if `p` is prime.
See also `padic_norm.padic_norm_p_lt_one` for a version assuming `1 < p`.
-/
lemma padic_norm_p_lt_one_of_prime (p : ℕ) [fact p.prime] : padic_norm p p < 1 :=
padic_norm_p_lt_one $ nat.prime.one_lt (fact.out _)
/--
`padic_norm p q` takes discrete values `p ^ -z` for `z : ℤ`.
-/
protected theorem values_discrete {q : ℚ} (hq : q ≠ 0) : ∃ z : ℤ, padic_norm p q = p ^ (-z) :=
⟨ (padic_val_rat p q), by simp [padic_norm, hq] ⟩
/--
`padic_norm p` is symmetric.
-/
@[simp] protected lemma neg (q : ℚ) : padic_norm p (-q) = padic_norm p q :=
if hq : q = 0 then by simp [hq]
else by simp [padic_norm, hq]
variable [hp : fact p.prime]
include hp
/--
If `q ≠ 0`, then `padic_norm p q ≠ 0`.
-/
protected lemma nonzero {q : ℚ} (hq : q ≠ 0) : padic_norm p q ≠ 0 :=
begin
rw padic_norm.eq_fpow_of_nonzero p hq,
apply fpow_ne_zero_of_ne_zero,
exact_mod_cast ne_of_gt hp.1.pos
end
/--
If the p-adic norm of `q` is 0, then `q` is 0.
-/
lemma zero_of_padic_norm_eq_zero {q : ℚ} (h : padic_norm p q = 0) : q = 0 :=
begin
apply by_contradiction, intro hq,
unfold padic_norm at h, rw if_neg hq at h,
apply absurd h,
apply fpow_ne_zero_of_ne_zero,
exact_mod_cast hp.1.ne_zero
end
/--
The p-adic norm is multiplicative.
-/
@[simp] protected theorem mul (q r : ℚ) : padic_norm p (q*r) = padic_norm p q * padic_norm p r :=
if hq : q = 0 then
by simp [hq]
else if hr : r = 0 then
by simp [hr]
else
have q*r ≠ 0, from mul_ne_zero hq hr,
have (↑p : ℚ) ≠ 0, by simp [hp.1.ne_zero],
by simp [padic_norm, *, padic_val_rat.mul, fpow_add this, mul_comm]
/--
The p-adic norm respects division.
-/
@[simp] protected theorem div (q r : ℚ) : padic_norm p (q / r) = padic_norm p q / padic_norm p r :=
if hr : r = 0 then by simp [hr] else
eq_div_of_mul_eq (padic_norm.nonzero _ hr) (by rw [←padic_norm.mul, div_mul_cancel _ hr])
/--
The p-adic norm of an integer is at most 1.
-/
protected theorem of_int (z : ℤ) : padic_norm p ↑z ≤ 1 :=
if hz : z = 0 then by simp [hz, zero_le_one] else
begin
unfold padic_norm,
rw [if_neg _],
{ refine fpow_le_one_of_nonpos _ _,
{ exact_mod_cast le_of_lt hp.1.one_lt, },
{ rw [padic_val_rat_of_int _ hp.1.ne_one hz, neg_nonpos],
norm_cast, simp }},
exact_mod_cast hz
end
private lemma nonarchimedean_aux {q r : ℚ} (h : padic_val_rat p q ≤ padic_val_rat p r) :
padic_norm p (q + r) ≤ max (padic_norm p q) (padic_norm p r) :=
have hnqp : padic_norm p q ≥ 0, from padic_norm.nonneg _ _,
have hnrp : padic_norm p r ≥ 0, from padic_norm.nonneg _ _,
if hq : q = 0 then
by simp [hq, max_eq_right hnrp, le_max_right]
else if hr : r = 0 then
by simp [hr, max_eq_left hnqp, le_max_left]
else if hqr : q + r = 0 then
le_trans (by simpa [hqr] using hnqp) (le_max_left _ _)
else
begin
unfold padic_norm, split_ifs,
apply le_max_iff.2,
left,
apply fpow_le_of_le,
{ exact_mod_cast le_of_lt hp.1.one_lt },
{ apply neg_le_neg,
have : padic_val_rat p q =
min (padic_val_rat p q) (padic_val_rat p r),
from (min_eq_left h).symm,
rw this,
apply min_le_padic_val_rat_add; assumption }
end
/--
The p-adic norm is nonarchimedean: the norm of `p + q` is at most the max of the norm of `p` and
the norm of `q`.
-/
protected theorem nonarchimedean {q r : ℚ} :
padic_norm p (q + r) ≤ max (padic_norm p q) (padic_norm p r) :=
begin
wlog hle := le_total (padic_val_rat p q) (padic_val_rat p r) using [q r],
exact nonarchimedean_aux p hle
end
/--
The p-adic norm respects the triangle inequality: the norm of `p + q` is at most the norm of `p`
plus the norm of `q`.
-/
theorem triangle_ineq (q r : ℚ) : padic_norm p (q + r) ≤ padic_norm p q + padic_norm p r :=
calc padic_norm p (q + r) ≤ max (padic_norm p q) (padic_norm p r) : padic_norm.nonarchimedean p
... ≤ padic_norm p q + padic_norm p r :
max_le_add_of_nonneg (padic_norm.nonneg p _) (padic_norm.nonneg p _)
/--
The p-adic norm of a difference is at most the max of each component. Restates the archimedean
property of the p-adic norm.
-/
protected theorem sub {q r : ℚ} : padic_norm p (q - r) ≤ max (padic_norm p q) (padic_norm p r) :=
by rw [sub_eq_add_neg, ←padic_norm.neg p r]; apply padic_norm.nonarchimedean
/--
If the p-adic norms of `q` and `r` are different, then the norm of `q + r` is equal to the max of
the norms of `q` and `r`.
-/
lemma add_eq_max_of_ne {q r : ℚ} (hne : padic_norm p q ≠ padic_norm p r) :
padic_norm p (q + r) = max (padic_norm p q) (padic_norm p r) :=
begin
wlog hle := le_total (padic_norm p r) (padic_norm p q) using [q r],
have hlt : padic_norm p r < padic_norm p q, from lt_of_le_of_ne hle hne.symm,
have : padic_norm p q ≤ max (padic_norm p (q + r)) (padic_norm p r), from calc
padic_norm p q = padic_norm p (q + r - r) : by congr; ring
... ≤ max (padic_norm p (q + r)) (padic_norm p (-r)) : padic_norm.nonarchimedean p
... = max (padic_norm p (q + r)) (padic_norm p r) : by simp,
have hnge : padic_norm p r ≤ padic_norm p (q + r),
{ apply le_of_not_gt,
intro hgt,
rw max_eq_right_of_lt hgt at this,
apply not_lt_of_ge this,
assumption },
have : padic_norm p q ≤ padic_norm p (q + r), by rwa [max_eq_left hnge] at this,
apply _root_.le_antisymm,
{ apply padic_norm.nonarchimedean p },
{ rw max_eq_left_of_lt hlt,
assumption }
end
/--
The p-adic norm is an absolute value: positive-definite and multiplicative, satisfying the triangle
inequality.
-/
instance : is_absolute_value (padic_norm p) :=
{ abv_nonneg := padic_norm.nonneg p,
abv_eq_zero :=
begin
intros,
constructor; intro,
{ apply zero_of_padic_norm_eq_zero p, assumption },
{ simp [*] }
end,
abv_add := padic_norm.triangle_ineq p,
abv_mul := padic_norm.mul p }
variable {p}
lemma dvd_iff_norm_le {n : ℕ} {z : ℤ} : ↑(p^n) ∣ z ↔ padic_norm p z ≤ ↑p ^ (-n : ℤ) :=
begin
unfold padic_norm, split_ifs with hz,
{ norm_cast at hz,
have : 0 ≤ (p^n : ℚ), {apply pow_nonneg, exact_mod_cast le_of_lt hp.1.pos },
simp [hz, this] },
{ rw [fpow_le_iff_le, neg_le_neg_iff, padic_val_rat_of_int _ hp.1.ne_one _],
{ norm_cast,
rw [← enat.coe_le_coe, enat.coe_get, ← multiplicity.pow_dvd_iff_le_multiplicity],
simp },
{ exact_mod_cast hz },
{ exact_mod_cast hp.1.one_lt } }
end
end padic_norm
end padic_norm
|
21fd9d7030cf55f45f8d05ee61ad3403d862ca9a | c3f2fcd060adfa2ca29f924839d2d925e8f2c685 | /hott/init/axioms/funext_of_ua.hlean | ddeeec0ee6e342d38ba53ef489d00a6609b7f59a | [
"Apache-2.0"
] | permissive | respu/lean | 6582d19a2f2838a28ecd2b3c6f81c32d07b5341d | 8c76419c60b63d0d9f7bc04ebb0b99812d0ec654 | refs/heads/master | 1,610,882,451,231 | 1,427,747,084,000 | 1,427,747,429,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 5,856 | hlean | /-
Copyright (c) 2014 Jakob von Raumer. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Module: init.axioms.funext_of_ua
Author: Jakob von Raumer
Ported from Coq HoTT
-/
prelude
import ..equiv ..datatypes ..types.prod
import .funext_varieties .ua
open eq function prod is_trunc sigma equiv is_equiv unit
context
universe variables l
private theorem ua_isequiv_postcompose {A B : Type.{l}} {C : Type}
{w : A → B} [H0 : is_equiv w] : is_equiv (@compose C A B w) :=
let w' := equiv.mk w H0 in
let eqinv : A = B := ((@is_equiv.inv _ _ _ (univalence A B)) w') in
let eq' := equiv_of_eq eqinv in
is_equiv.adjointify (@compose C A B w)
(@compose C B A (is_equiv.inv w))
(λ (x : C → B),
have eqretr : eq' = w',
from (@retr _ _ (@equiv_of_eq A B) (univalence A B) w'),
have invs_eq : (to_fun eq')⁻¹ = (to_fun w')⁻¹,
from inv_eq eq' w' eqretr,
have eqfin : (to_fun eq') ∘ ((to_fun eq')⁻¹ ∘ x) = x,
from (λ p,
(@eq.rec_on Type.{l} A
(λ B' p', Π (x' : C → B'), (to_fun (equiv_of_eq p'))
∘ ((to_fun (equiv_of_eq p'))⁻¹ ∘ x') = x')
B p (λ x', idp))
) eqinv x,
have eqfin' : (to_fun w') ∘ ((to_fun eq')⁻¹ ∘ x) = x,
from eqretr ▹ eqfin,
have eqfin'' : (to_fun w') ∘ ((to_fun w')⁻¹ ∘ x) = x,
from invs_eq ▹ eqfin',
eqfin''
)
(λ (x : C → A),
have eqretr : eq' = w',
from (@retr _ _ (@equiv_of_eq A B) (univalence A B) w'),
have invs_eq : (to_fun eq')⁻¹ = (to_fun w')⁻¹,
from inv_eq eq' w' eqretr,
have eqfin : (to_fun eq')⁻¹ ∘ ((to_fun eq') ∘ x) = x,
from (λ p, eq.rec_on p idp) eqinv,
have eqfin' : (to_fun eq')⁻¹ ∘ ((to_fun w') ∘ x) = x,
from eqretr ▹ eqfin,
have eqfin'' : (to_fun w')⁻¹ ∘ ((to_fun w') ∘ x) = x,
from invs_eq ▹ eqfin',
eqfin''
)
-- We are ready to prove functional extensionality,
-- starting with the naive non-dependent version.
private definition diagonal [reducible] (B : Type) : Type
:= Σ xy : B × B, pr₁ xy = pr₂ xy
private definition isequiv_src_compose {A B : Type}
: @is_equiv (A → diagonal B)
(A → B)
(compose (pr₁ ∘ pr1)) :=
@ua_isequiv_postcompose _ _ _ (pr₁ ∘ pr1)
(is_equiv.adjointify (pr₁ ∘ pr1)
(λ x, sigma.mk (x , x) idp) (λx, idp)
(λ x, sigma.rec_on x
(λ xy, prod.rec_on xy
(λ b c p, eq.rec_on p idp))))
private definition isequiv_tgt_compose {A B : Type}
: @is_equiv (A → diagonal B)
(A → B)
(compose (pr₂ ∘ pr1)) :=
@ua_isequiv_postcompose _ _ _ (pr2 ∘ pr1)
(is_equiv.adjointify (pr2 ∘ pr1)
(λ x, sigma.mk (x , x) idp) (λx, idp)
(λ x, sigma.rec_on x
(λ xy, prod.rec_on xy
(λ b c p, eq.rec_on p idp))))
set_option class.conservative false
theorem nondep_funext_from_ua {A : Type} {B : Type}
: Π {f g : A → B}, f ∼ g → f = g :=
(λ (f g : A → B) (p : f ∼ g),
let d := λ (x : A), sigma.mk (f x , f x) idp in
let e := λ (x : A), sigma.mk (f x , g x) (p x) in
let precomp1 := compose (pr₁ ∘ pr1) in
have equiv1 [visible] : is_equiv precomp1,
from @isequiv_src_compose A B,
have equiv2 [visible] : Π x y, is_equiv (ap precomp1),
from is_equiv.is_equiv_ap precomp1,
have H' : Π (x y : A → diagonal B),
pr₁ ∘ pr1 ∘ x = pr₁ ∘ pr1 ∘ y → x = y,
from (λ x y, is_equiv.inv (ap precomp1)),
have eq2 : pr₁ ∘ pr1 ∘ d = pr₁ ∘ pr1 ∘ e,
from idp,
have eq0 : d = e,
from H' d e eq2,
have eq1 : (pr₂ ∘ pr1) ∘ d = (pr₂ ∘ pr1) ∘ e,
from ap _ eq0,
eq1
)
end
-- Now we use this to prove weak funext, which as we know
-- implies (with dependent eta) also the strong dependent funext.
theorem weak_funext_of_ua : weak_funext :=
(λ (A : Type) (P : A → Type) allcontr,
let U := (λ (x : A), unit) in
have pequiv : Π (x : A), P x ≃ U x,
from (λ x, @equiv_unit_of_is_contr (P x) (allcontr x)),
have psim : Π (x : A), P x = U x,
from (λ x, @is_equiv.inv _ _
equiv_of_eq (univalence _ _) (pequiv x)),
have p : P = U,
from @nondep_funext_from_ua A Type P U psim,
have tU' : is_contr (A → unit),
from is_contr.mk (λ x, ⋆)
(λ f, @nondep_funext_from_ua A unit (λ x, ⋆) f
(λ x, unit.rec_on (f x) idp)),
have tU : is_contr (Π x, U x),
from tU',
have tlast : is_contr (Πx, P x),
from eq.transport _ p⁻¹ tU,
tlast
)
-- In the following we will proof function extensionality using the univalence axiom
definition funext_of_ua : funext :=
funext_of_weak_funext (@weak_funext_of_ua)
variables {A : Type} {P : A → Type} {f g : Π x, P x}
namespace funext
definition is_equiv_apD [instance] (f g : Π x, P x) : is_equiv (@apD10 A P f g) :=
funext_of_ua f g
end funext
open funext
definition eq_equiv_homotopy : (f = g) ≃ (f ∼ g) :=
equiv.mk apD10 _
definition eq_of_homotopy [reducible] : f ∼ g → f = g :=
(@apD10 A P f g)⁻¹
--TODO: rename to eq_of_homotopy_idp
definition eq_of_homotopy_id (f : Π x, P x) : eq_of_homotopy (λx : A, idpath (f x)) = idpath f :=
is_equiv.sect apD10 idp
definition naive_funext_of_ua : naive_funext :=
λ A P f g h, eq_of_homotopy h
protected definition homotopy.rec_on {Q : (f ∼ g) → Type} (p : f ∼ g)
(H : Π(q : f = g), Q (apD10 q)) : Q p :=
retr apD10 p ▹ H (eq_of_homotopy p)
|
705a9329f3639c56ee5441d24415f8e7c87b90c8 | 69d4931b605e11ca61881fc4f66db50a0a875e39 | /src/ring_theory/jacobson.lean | 98daa88f48e7d3fac0736b3e01855b7869161da6 | [
"Apache-2.0"
] | permissive | abentkamp/mathlib | d9a75d291ec09f4637b0f30cc3880ffb07549ee5 | 5360e476391508e092b5a1e5210bd0ed22dc0755 | refs/heads/master | 1,682,382,954,948 | 1,622,106,077,000 | 1,622,106,077,000 | 149,285,665 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 33,197 | lean | /-
Copyright (c) 2020 Devon Tuma. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Devon Tuma
-/
import data.mv_polynomial
import ring_theory.ideal.over
import ring_theory.jacobson_ideal
import ring_theory.localization
/-!
# Jacobson Rings
The following conditions are equivalent for a ring `R`:
1. Every radical ideal `I` is equal to its Jacobson radical
2. Every radical ideal `I` can be written as an intersection of maximal ideals
3. Every prime ideal `I` is equal to its Jacobson radical
Any ring satisfying any of these equivalent conditions is said to be Jacobson.
Some particular examples of Jacobson rings are also proven.
`is_jacobson_quotient` says that the quotient of a Jacobson ring is Jacobson.
`is_jacobson_localization` says the localization of a Jacobson ring to a single element is Jacobson.
`is_jacobson_polynomial_iff_is_jacobson` says polynomials over a Jacobson ring form a Jacobson ring.
## Main definitions
Let `R` be a commutative ring. Jacobson Rings are defined using the first of the above conditions
* `is_jacobson R` is the proposition that `R` is a Jacobson ring. It is a class,
implemented as the predicate that for any ideal, `I.radical = I` implies `I.jacobson = I`.
## Main statements
* `is_jacobson_iff_prime_eq` is the equivalence between conditions 1 and 3 above.
* `is_jacobson_iff_Inf_maximal` is the equivalence between conditions 1 and 2 above.
* `is_jacobson_of_surjective` says that if `R` is a Jacobson ring and `f : R →+* S` is surjective,
then `S` is also a Jacobson ring
* `is_jacobson_mv_polynomial` says that multi-variate polynomials over a Jacobson ring are Jacobson.
## Tags
Jacobson, Jacobson Ring
-/
namespace ideal
open polynomial
section is_jacobson
variables {R S : Type*} [comm_ring R] [comm_ring S] {I : ideal R}
/-- A ring is a Jacobson ring if for every radical ideal `I`,
the Jacobson radical of `I` is equal to `I`.
See `is_jacobson_iff_prime_eq` and `is_jacobson_iff_Inf_maximal` for equivalent definitions. -/
class is_jacobson (R : Type*) [comm_ring R] : Prop :=
(out' : ∀ (I : ideal R), I.radical = I → I.jacobson = I)
theorem is_jacobson_iff {R} [comm_ring R] :
is_jacobson R ↔ ∀ (I : ideal R), I.radical = I → I.jacobson = I :=
⟨λ h, h.1, λ h, ⟨h⟩⟩
theorem is_jacobson.out {R} [comm_ring R] :
is_jacobson R → ∀ {I : ideal R}, I.radical = I → I.jacobson = I := is_jacobson_iff.1
/-- A ring is a Jacobson ring if and only if for all prime ideals `P`,
the Jacobson radical of `P` is equal to `P`. -/
lemma is_jacobson_iff_prime_eq : is_jacobson R ↔ ∀ P : ideal R, is_prime P → P.jacobson = P :=
begin
refine is_jacobson_iff.trans ⟨λ h I hI, h I (is_prime.radical hI), _⟩,
refine λ h I hI, le_antisymm (λ x hx, _) (λ x hx, mem_Inf.mpr (λ _ hJ, hJ.left hx)),
rw [← hI, radical_eq_Inf I, mem_Inf],
intros P hP,
rw set.mem_set_of_eq at hP,
erw mem_Inf at hx,
erw [← h P hP.right, mem_Inf],
exact λ J hJ, hx ⟨le_trans hP.left hJ.left, hJ.right⟩
end
/-- A ring `R` is Jacobson if and only if for every prime ideal `I`,
`I` can be written as the infimum of some collection of maximal ideals.
Allowing ⊤ in the set `M` of maximal ideals is equivalent, but makes some proofs cleaner. -/
lemma is_jacobson_iff_Inf_maximal : is_jacobson R ↔
∀ {I : ideal R}, I.is_prime → ∃ M : set (ideal R), (∀ J ∈ M, is_maximal J ∨ J = ⊤) ∧ I = Inf M :=
⟨λ H I h, eq_jacobson_iff_Inf_maximal.1 (H.out (is_prime.radical h)),
λ H, is_jacobson_iff_prime_eq.2 (λ P hP, eq_jacobson_iff_Inf_maximal.2 (H hP))⟩
lemma is_jacobson_iff_Inf_maximal' : is_jacobson R ↔
∀ {I : ideal R}, I.is_prime → ∃ M : set (ideal R),
(∀ (J ∈ M) (K : ideal R), J < K → K = ⊤) ∧ I = Inf M :=
⟨λ H I h, eq_jacobson_iff_Inf_maximal'.1 (H.out (is_prime.radical h)),
λ H, is_jacobson_iff_prime_eq.2 (λ P hP, eq_jacobson_iff_Inf_maximal'.2 (H hP))⟩
lemma radical_eq_jacobson [H : is_jacobson R] (I : ideal R) : I.radical = I.jacobson :=
le_antisymm (le_Inf (λ J ⟨hJ, hJ_max⟩, (is_prime.radical_le_iff hJ_max.is_prime).mpr hJ))
((H.out (radical_idem I)) ▸ (jacobson_mono le_radical))
/-- Fields have only two ideals, and the condition holds for both of them. -/
@[priority 100]
instance is_jacobson_field {K : Type*} [field K] : is_jacobson K :=
⟨λ I hI, or.rec_on (eq_bot_or_top I)
(λ h, le_antisymm
(Inf_le ⟨le_of_eq rfl, (eq.symm h) ▸ bot_is_maximal⟩)
((eq.symm h) ▸ bot_le))
(λ h, by rw [h, jacobson_eq_top_iff])⟩
theorem is_jacobson_of_surjective [H : is_jacobson R] :
(∃ (f : R →+* S), function.surjective f) → is_jacobson S :=
begin
rintros ⟨f, hf⟩,
rw is_jacobson_iff_Inf_maximal,
intros p hp,
use map f '' {J : ideal R | comap f p ≤ J ∧ J.is_maximal },
use λ j ⟨J, hJ, hmap⟩, hmap ▸ or.symm (map_eq_top_or_is_maximal_of_surjective f hf hJ.right),
have : p = map f ((comap f p).jacobson),
from (is_jacobson.out' (comap f p) (by rw [← comap_radical, is_prime.radical hp])).symm
▸ (map_comap_of_surjective f hf p).symm,
exact eq.trans this (map_Inf hf (λ J ⟨hJ, _⟩, le_trans (ideal.ker_le_comap f) hJ)),
end
@[priority 100]
instance is_jacobson_quotient [is_jacobson R] : is_jacobson (quotient I) :=
is_jacobson_of_surjective ⟨quotient.mk I, (by rintro ⟨x⟩; use x; refl)⟩
lemma is_jacobson_iso (e : R ≃+* S) : is_jacobson R ↔ is_jacobson S :=
⟨λ h, @is_jacobson_of_surjective _ _ _ _ h ⟨(e : R →+* S), e.surjective⟩,
λ h, @is_jacobson_of_surjective _ _ _ _ h ⟨(e.symm : S →+* R), e.symm.surjective⟩⟩
lemma is_jacobson_of_is_integral [algebra R S] (hRS : algebra.is_integral R S)
(hR : is_jacobson R) : is_jacobson S :=
begin
rw is_jacobson_iff_prime_eq,
introsI P hP,
by_cases hP_top : comap (algebra_map R S) P = ⊤,
{ simp [comap_eq_top_iff.1 hP_top] },
{ haveI : nontrivial (comap (algebra_map R S) P).quotient := quotient.nontrivial hP_top,
rw jacobson_eq_iff_jacobson_quotient_eq_bot,
refine eq_bot_of_comap_eq_bot (is_integral_quotient_of_is_integral hRS) _,
rw [eq_bot_iff, ← jacobson_eq_iff_jacobson_quotient_eq_bot.1 ((is_jacobson_iff_prime_eq.1 hR)
(comap (algebra_map R S) P) (comap_is_prime _ _)), comap_jacobson],
refine Inf_le_Inf (λ J hJ, _),
simp only [true_and, set.mem_image, bot_le, set.mem_set_of_eq],
haveI : J.is_maximal := by simpa using hJ,
exact exists_ideal_over_maximal_of_is_integral (is_integral_quotient_of_is_integral hRS) J
(comap_bot_le_of_injective _ algebra_map_quotient_injective) }
end
lemma is_jacobson_of_is_integral' (f : R →+* S) (hf : f.is_integral)
(hR : is_jacobson R) : is_jacobson S :=
@is_jacobson_of_is_integral _ _ _ _ f.to_algebra hf hR
end is_jacobson
section localization
open localization_map submonoid
variables {R S : Type*} [comm_ring R] [comm_ring S] {I : ideal R}
variables {y : R} (f : away_map y S)
lemma disjoint_powers_iff_not_mem (hI : I.radical = I) :
disjoint ((submonoid.powers y) : set R) ↑I ↔ y ∉ I.1 :=
begin
refine ⟨λ h, set.disjoint_left.1 h (mem_powers _), λ h, (disjoint_iff).mpr (eq_bot_iff.mpr _)⟩,
rintros x ⟨⟨n, rfl⟩, hx'⟩,
rw [← hI] at hx',
exact absurd (hI ▸ mem_radical_of_pow_mem hx' : y ∈ I.carrier) h
end
/-- If `R` is a Jacobson ring, then maximal ideals in the localization at `y`
correspond to maximal ideals in the original ring `R` that don't contain `y`.
This lemma gives the correspondence in the particular case of an ideal and its comap.
See `le_rel_iso_of_maximal` for the more general relation isomorphism -/
lemma is_maximal_iff_is_maximal_disjoint [H : is_jacobson R] (J : ideal S) :
J.is_maximal ↔ (comap f.to_map J).is_maximal ∧ y ∉ ideal.comap f.to_map J :=
begin
split,
{ refine λ h, ⟨_, λ hy, h.ne_top (ideal.eq_top_of_is_unit_mem _ hy
(map_units f ⟨y, submonoid.mem_powers _⟩))⟩,
have hJ : J.is_prime := is_maximal.is_prime h,
rw is_prime_iff_is_prime_disjoint f at hJ,
have : y ∉ (comap f.to_map J).1 :=
set.disjoint_left.1 hJ.right (submonoid.mem_powers _),
erw [← H.out (is_prime.radical hJ.left), mem_Inf] at this,
push_neg at this,
rcases this with ⟨I, hI, hI'⟩,
convert hI.right,
by_cases hJ : J = map f.to_map I,
{ rw [hJ, comap_map_of_is_prime_disjoint f I (is_maximal.is_prime hI.right)],
rwa disjoint_powers_iff_not_mem (is_maximal.is_prime hI.right).radical},
{ have hI_p : (map f.to_map I).is_prime,
{ refine is_prime_of_is_prime_disjoint f I hI.right.is_prime _,
rwa disjoint_powers_iff_not_mem (is_maximal.is_prime hI.right).radical },
have : J ≤ map f.to_map I := (map_comap f J) ▸ (map_mono hI.left),
exact absurd (h.1.2 _ (lt_of_le_of_ne this hJ)) hI_p.1 } },
{ refine λ h, ⟨⟨λ hJ, h.1.ne_top (eq_top_iff.2 _), λ I hI, _⟩⟩,
{ rwa [eq_top_iff, ← f.order_embedding.le_iff_le] at hJ },
{ have := congr_arg (map f.to_map) (h.1.1.2 _ ⟨comap_mono (le_of_lt hI), _⟩),
rwa [map_comap f I, map_top f.to_map] at this,
refine λ hI', hI.right _,
rw [← map_comap f I, ← map_comap f J],
exact map_mono hI' } }
end
/-- If `R` is a Jacobson ring, then maximal ideals in the localization at `y`
correspond to maximal ideals in the original ring `R` that don't contain `y`.
This lemma gives the correspondence in the particular case of an ideal and its map.
See `le_rel_iso_of_maximal` for the more general statement, and the reverse of this implication -/
lemma is_maximal_of_is_maximal_disjoint [is_jacobson R] (I : ideal R) (hI : I.is_maximal)
(hy : y ∉ I) : (map f.to_map I).is_maximal :=
begin
rw [is_maximal_iff_is_maximal_disjoint f,
comap_map_of_is_prime_disjoint f I (is_maximal.is_prime hI)
((disjoint_powers_iff_not_mem (is_maximal.is_prime hI).radical).2 hy)],
exact ⟨hI, hy⟩
end
/-- If `R` is a Jacobson ring, then maximal ideals in the localization at `y`
correspond to maximal ideals in the original ring `R` that don't contain `y` -/
def order_iso_of_maximal [is_jacobson R] :
{p : ideal S // p.is_maximal} ≃o {p : ideal R // p.is_maximal ∧ y ∉ p} :=
{ to_fun := λ p, ⟨ideal.comap f.to_map p.1, (is_maximal_iff_is_maximal_disjoint f p.1).1 p.2⟩,
inv_fun := λ p, ⟨ideal.map f.to_map p.1, is_maximal_of_is_maximal_disjoint f p.1 p.2.1 p.2.2⟩,
left_inv := λ J, subtype.eq (map_comap f J),
right_inv := λ I, subtype.eq (comap_map_of_is_prime_disjoint f I.1 (is_maximal.is_prime I.2.1)
((disjoint_powers_iff_not_mem I.2.1.is_prime.radical).2 I.2.2)),
map_rel_iff' := λ I I', ⟨λ h, (show I.val ≤ I'.val,
from (map_comap f I.val) ▸ (map_comap f I'.val) ▸ (ideal.map_mono h)), λ h x hx, h hx⟩ }
/-- If `S` is the localization of the Jacobson ring `R` at the submonoid generated by `y : R`, then
`S` is Jacobson. -/
lemma is_jacobson_localization [H : is_jacobson R]
(f : away_map y S) : is_jacobson S :=
begin
rw is_jacobson_iff_prime_eq,
refine λ P' hP', le_antisymm _ le_jacobson,
obtain ⟨hP', hPM⟩ := (localization_map.is_prime_iff_is_prime_disjoint f P').mp hP',
have hP := H.out (is_prime.radical hP'),
refine le_trans (le_trans (le_of_eq (localization_map.map_comap f P'.jacobson).symm) (map_mono _))
(le_of_eq (localization_map.map_comap f P')),
have : Inf { I : ideal R | comap f.to_map P' ≤ I ∧ I.is_maximal ∧ y ∉ I } ≤ comap f.to_map P',
{ intros x hx,
have hxy : x * y ∈ (comap f.to_map P').jacobson,
{ rw [ideal.jacobson, mem_Inf],
intros J hJ,
by_cases y ∈ J,
{ exact J.smul_mem x h },
{ exact (mul_comm y x) ▸ J.smul_mem y ((mem_Inf.1 hx) ⟨hJ.left, ⟨hJ.right, h⟩⟩) } },
rw hP at hxy,
cases hP'.mem_or_mem hxy with hxy hxy,
{ exact hxy },
{ exact (hPM ⟨submonoid.mem_powers _, hxy⟩).elim } },
refine le_trans _ this,
rw [ideal.jacobson, comap_Inf', Inf_eq_infi],
refine infi_le_infi_of_subset (λ I hI, ⟨map f.to_map I, ⟨_, _⟩⟩),
{ exact ⟨le_trans (le_of_eq ((localization_map.map_comap f P').symm)) (map_mono hI.1),
is_maximal_of_is_maximal_disjoint f _ hI.2.1 hI.2.2⟩ },
{ exact localization_map.comap_map_of_is_prime_disjoint f I (is_maximal.is_prime hI.2.1)
((disjoint_powers_iff_not_mem hI.2.1.is_prime.radical).2 hI.2.2) }
end
end localization
namespace polynomial
open polynomial
section comm_ring
variables {R S : Type*} [comm_ring R] [integral_domain S]
variables {Rₘ Sₘ : Type*} [comm_ring Rₘ] [comm_ring Sₘ]
/-- If `I` is a prime ideal of `polynomial R` and `pX ∈ I` is a non-constant polynomial,
then the map `R →+* R[x]/I` descends to an integral map when localizing at `pX.leading_coeff`.
In particular `X` is integral because it satisfies `pX`, and constants are trivially integral,
so integrality of the entire extension follows by closure under addition and multiplication. -/
lemma is_integral_localization_map_polynomial_quotient
(P : ideal (polynomial R)) [P.is_prime] (pX : polynomial R) (hpX : pX ∈ P)
(ϕ : localization_map (submonoid.powers (pX.map (quotient.mk (P.comap C))).leading_coeff) Rₘ)
(ϕ' : localization_map ((submonoid.powers (pX.map (quotient.mk (P.comap C))).leading_coeff).map
(quotient_map P C le_rfl) : submonoid P.quotient) Sₘ) :
(ϕ.map ((submonoid.powers (pX.map (quotient.mk (P.comap C))).leading_coeff).apply_coe_mem_map
(quotient_map P C le_rfl :
(P.comap C : ideal R).quotient →* P.quotient)) ϕ').is_integral :=
begin
let P' : ideal R := P.comap C,
let M : submonoid P'.quotient :=
submonoid.powers (pX.map (quotient.mk (P.comap C))).leading_coeff,
let φ : P'.quotient →+* P.quotient := quotient_map P C le_rfl,
let φ' := (ϕ.map (M.apply_coe_mem_map (φ : P'.quotient →* P.quotient)) ϕ'),
have hφ' : φ.comp (quotient.mk P') = (quotient.mk P).comp C := rfl,
intro p,
obtain ⟨⟨p', ⟨q, hq⟩⟩, hp⟩ := ϕ'.surj p,
suffices : φ'.is_integral_elem (ϕ'.to_map p'),
{ obtain ⟨q', hq', rfl⟩ := hq,
obtain ⟨q'', hq''⟩ := is_unit_iff_exists_inv'.1 (ϕ.map_units ⟨q', hq'⟩),
refine φ'.is_integral_of_is_integral_mul_unit p (ϕ'.to_map (φ q')) q'' _ (hp.symm ▸ this),
convert trans (trans (φ'.map_mul _ _).symm (congr_arg φ' hq'')) φ'.map_one using 2,
rw [← φ'.comp_apply, localization_map.map_comp, ϕ'.to_map.comp_apply, subtype.coe_mk] },
refine is_integral_of_mem_closure''
((ϕ'.to_map.comp (quotient.mk P)) '' (insert X {p | p.degree ≤ 0})) _ _ _,
{ rintros x ⟨p, hp, rfl⟩,
refine hp.rec_on (λ hy, _) (λ hy, _),
{ refine hy.symm ▸ (φ.is_integral_elem_localization_at_leading_coeff ((quotient.mk P) X)
(pX.map (quotient.mk P')) _ M ⟨1, pow_one _⟩ _ _),
rwa [eval₂_map, hφ', ← hom_eval₂, quotient.eq_zero_iff_mem, eval₂_C_X] },
{ rw [set.mem_set_of_eq, degree_le_zero_iff] at hy,
refine hy.symm ▸ ⟨X - C (ϕ.to_map ((quotient.mk P') (p.coeff 0))), monic_X_sub_C _, _⟩,
simp only [eval₂_sub, eval₂_C, eval₂_X],
rw [sub_eq_zero, ← φ'.comp_apply, localization_map.map_comp, ring_hom.comp_apply],
refl } },
{ obtain ⟨p, rfl⟩ := quotient.mk_surjective p',
refine polynomial.induction_on p
(λ r, subring.subset_closure $ set.mem_image_of_mem _ (or.inr degree_C_le))
(λ _ _ h1 h2, _) (λ n _ hr, _),
{ convert subring.add_mem _ h1 h2,
rw [ring_hom.map_add, ring_hom.map_add] },
{ rw [pow_succ X n, mul_comm X, ← mul_assoc, ring_hom.map_mul, ϕ'.to_map.map_mul],
exact subring.mul_mem _ hr (subring.subset_closure (set.mem_image_of_mem _ (or.inl rfl))) } },
end
/-- If `f : R → S` descends to an integral map in the localization at `x`,
and `R` is a Jacobson ring, then the intersection of all maximal ideals in `S` is trivial -/
lemma jacobson_bot_of_integral_localization {R : Type*} [integral_domain R] [is_jacobson R]
(φ : R →+* S) (hφ : function.injective φ) (x : R) (hx : x ≠ 0)
(ϕ : localization_map (submonoid.powers x) Rₘ)
(ϕ' : localization_map ((submonoid.powers x).map φ : submonoid S) Sₘ)
(hφ' : (ϕ.map ((submonoid.powers x).apply_coe_mem_map (φ : R →* S)) ϕ').is_integral) :
(⊥ : ideal S).jacobson = ⊥ :=
begin
have hM : ((submonoid.powers x).map φ : submonoid S) ≤ non_zero_divisors S :=
map_le_non_zero_divisors_of_injective hφ (powers_le_non_zero_divisors_of_domain hx),
letI : integral_domain Sₘ := localization_map.integral_domain_of_le_non_zero_divisors ϕ' hM,
let φ' : Rₘ →+* Sₘ := ϕ.map ((submonoid.powers x).apply_coe_mem_map (φ : R →* S)) ϕ',
suffices : ∀ I : ideal Sₘ, I.is_maximal → (I.comap ϕ'.to_map).is_maximal,
{ have hϕ' : comap ϕ'.to_map ⊥ = ⊥,
{ simpa [ring_hom.injective_iff_ker_eq_bot, ring_hom.ker_eq_comap_bot] using ϕ'.injective hM },
refine eq_bot_iff.2 (le_trans _ (le_of_eq hϕ')),
have hSₘ : is_jacobson Sₘ := is_jacobson_of_is_integral' φ' hφ' (is_jacobson_localization ϕ),
rw [← hSₘ.out radical_bot_of_integral_domain, comap_jacobson],
exact Inf_le_Inf (λ j hj, ⟨bot_le, let ⟨J, hJ⟩ := hj in hJ.2 ▸ this J hJ.1.2⟩) },
introsI I hI,
-- Remainder of the proof is pulling and pushing ideals around the square and the quotient square
haveI : (I.comap ϕ'.to_map).is_prime := comap_is_prime ϕ'.to_map I,
haveI : (I.comap φ').is_prime := comap_is_prime φ' I,
haveI : (⊥ : ideal (I.comap ϕ'.to_map).quotient).is_prime := bot_prime,
have hcomm: φ'.comp ϕ.to_map = ϕ'.to_map.comp φ := ϕ.map_comp _,
let f := quotient_map (I.comap ϕ'.to_map) φ le_rfl,
let g := quotient_map I ϕ'.to_map le_rfl,
have := is_maximal_comap_of_is_integral_of_is_maximal' φ' hφ' I
(by convert hI; casesI _inst_4; refl),
have := ((is_maximal_iff_is_maximal_disjoint ϕ _).1 this).left,
have : ((I.comap ϕ'.to_map).comap φ).is_maximal,
{ rwa [comap_comap, hcomm, ← comap_comap] at this },
rw ← bot_quotient_is_maximal_iff at this ⊢,
refine is_maximal_of_is_integral_of_is_maximal_comap' f _ ⊥
((eq_bot_iff.2 (comap_bot_le_of_injective f quotient_map_injective)).symm ▸ this),
exact f.is_integral_tower_bot_of_is_integral g quotient_map_injective
((comp_quotient_map_eq_of_comp_eq hcomm I).symm ▸
(ring_hom.is_integral_trans _ _ (ring_hom.is_integral_of_surjective _
(localization_map.surjective_quotient_map_of_maximal_of_localization
(by rwa [comap_comap, hcomm, ← bot_quotient_is_maximal_iff])))
(ring_hom.is_integral_quotient_of_is_integral _ hφ'))),
end
/-- Used to bootstrap the proof of `is_jacobson_polynomial_iff_is_jacobson`.
That theorem is more general and should be used instead of this one. -/
private lemma is_jacobson_polynomial_of_domain (R : Type*) [integral_domain R] [hR : is_jacobson R]
(P : ideal (polynomial R)) [is_prime P] (hP : ∀ (x : R), C x ∈ P → x = 0) :
P.jacobson = P :=
begin
by_cases Pb : (P = ⊥),
{ exact Pb.symm ▸ jacobson_bot_polynomial_of_jacobson_bot
(hR.out radical_bot_of_integral_domain) },
{ refine jacobson_eq_iff_jacobson_quotient_eq_bot.mpr _,
haveI : (P.comap (C : R →+* polynomial R)).is_prime := comap_is_prime C P,
obtain ⟨p, pP, p0⟩ := exists_nonzero_mem_of_ne_bot Pb hP,
refine jacobson_bot_of_integral_localization (quotient_map P C le_rfl) quotient_map_injective
_ _ (localization.of (submonoid.powers (p.map (quotient.mk (P.comap C))).leading_coeff))
(localization.of _)
(by apply (is_integral_localization_map_polynomial_quotient P _ pP _ _)),
rwa [ne.def, leading_coeff_eq_zero] }
end
lemma is_jacobson_polynomial_of_is_jacobson (hR : is_jacobson R) :
is_jacobson (polynomial R) :=
begin
refine is_jacobson_iff_prime_eq.mpr (λ I, _),
introI hI,
let R' : subring I.quotient := ((quotient.mk I).comp C).range,
let i : R →+* R' := ((quotient.mk I).comp C).range_restrict,
have hi : function.surjective (i : R → R') := ((quotient.mk I).comp C).range_restrict_surjective,
have hi' : (polynomial.map_ring_hom i : polynomial R →+* polynomial R').ker ≤ I,
{ refine λ f hf, polynomial_mem_ideal_of_coeff_mem_ideal I f (λ n, _),
replace hf := congr_arg (λ (g : polynomial (((quotient.mk I).comp C).range)), g.coeff n) hf,
change (polynomial.map ((quotient.mk I).comp C).range_restrict f).coeff n = 0 at hf,
rw [coeff_map, subtype.ext_iff] at hf,
rwa [mem_comap, ← quotient.eq_zero_iff_mem, ← ring_hom.comp_apply], },
haveI : (ideal.map (map_ring_hom i) I).is_prime :=
map_is_prime_of_surjective (map_surjective i hi) hi',
suffices : (I.map (polynomial.map_ring_hom i)).jacobson = (I.map (polynomial.map_ring_hom i)),
{ replace this := congr_arg (comap (polynomial.map_ring_hom i)) this,
rw [← map_jacobson_of_surjective _ hi',
comap_map_of_surjective _ _, comap_map_of_surjective _ _] at this,
refine le_antisymm (le_trans (le_sup_left_of_le le_rfl)
(le_trans (le_of_eq this) (sup_le le_rfl hi'))) le_jacobson,
all_goals {exact polynomial.map_surjective i hi} },
exact @is_jacobson_polynomial_of_domain R' _ (is_jacobson_of_surjective ⟨i, hi⟩)
(map (map_ring_hom i) I) _ (eq_zero_of_polynomial_mem_map_range I),
end
theorem is_jacobson_polynomial_iff_is_jacobson :
is_jacobson (polynomial R) ↔ is_jacobson R :=
begin
refine ⟨_, is_jacobson_polynomial_of_is_jacobson⟩,
introI H,
exact is_jacobson_of_surjective ⟨eval₂_ring_hom (ring_hom.id _) 1, λ x,
⟨C x, by simp only [coe_eval₂_ring_hom, ring_hom.id_apply, eval₂_C]⟩⟩,
end
instance [is_jacobson R] : is_jacobson (polynomial R) :=
is_jacobson_polynomial_iff_is_jacobson.mpr ‹is_jacobson R›
end comm_ring
section integral_domain
variables {R : Type*} [integral_domain R] [is_jacobson R]
variables (P : ideal (polynomial R)) [hP : P.is_maximal]
include P hP
lemma is_maximal_comap_C_of_is_maximal (hP' : ∀ (x : R), C x ∈ P → x = 0) :
is_maximal (comap C P : ideal R) :=
begin
haveI hp'_prime : (P.comap C : ideal R).is_prime := comap_is_prime C P,
obtain ⟨m, hm⟩ := submodule.nonzero_mem_of_bot_lt (bot_lt_of_maximal P polynomial_not_is_field),
have : (m : polynomial R) ≠ 0, rwa [ne.def, submodule.coe_eq_zero],
let φ : (P.comap C : ideal R).quotient →+* P.quotient := quotient_map P C le_rfl,
let M : submonoid (P.comap C : ideal R).quotient :=
submonoid.powers ((m : polynomial R).map (quotient.mk (P.comap C : ideal R))).leading_coeff,
rw ← bot_quotient_is_maximal_iff at hP ⊢,
have hp0 : ((m : polynomial R).map (quotient.mk (P.comap C : ideal R))).leading_coeff ≠ 0 :=
λ hp0', this $ map_injective (quotient.mk (P.comap C : ideal R))
((quotient.mk (P.comap C : ideal R)).injective_iff.2 (λ x hx,
by rwa [quotient.eq_zero_iff_mem, (by rwa eq_bot_iff : (P.comap C : ideal R) = ⊥)] at hx))
(by simpa only [leading_coeff_eq_zero, map_zero] using hp0'),
let ϕ : localization_map M (localization M) := localization.of M,
have hM : (0 : ((P.comap C : ideal R)).quotient) ∉ M := λ ⟨n, hn⟩, hp0 (pow_eq_zero hn),
suffices : (⊥ : ideal (localization M)).is_maximal,
{ rw ← ϕ.comap_map_of_is_prime_disjoint ⊥ bot_prime (λ x hx, hM (hx.2 ▸ hx.1)),
refine ((is_maximal_iff_is_maximal_disjoint ϕ _).mp _).1,
rwa map_bot },
let M' : submonoid P.quotient := M.map φ,
have hM' : (0 : P.quotient) ∉ M' :=
λ ⟨z, hz⟩, hM (quotient_map_injective (trans hz.2 φ.map_zero.symm) ▸ hz.1),
letI : integral_domain (localization M') :=
localization_map.integral_domain_localization (le_non_zero_divisors_of_domain hM'),
let ϕ' : localization_map (M.map ↑φ) (localization (M.map ↑φ)) := localization.of (M.map ↑φ),
suffices : (⊥ : ideal (localization M')).is_maximal,
{ rw le_antisymm bot_le (comap_bot_le_of_injective _ (map_injective_of_injective _
quotient_map_injective M ϕ ϕ' (le_non_zero_divisors_of_domain hM'))),
refine is_maximal_comap_of_is_integral_of_is_maximal' _ _ ⊥ this,
apply is_integral_localization_map_polynomial_quotient P _ (submodule.coe_mem m) ϕ (ϕ' : _) },
rw (map_bot.symm : (⊥ : ideal (localization M')) = map ϕ'.to_map ⊥),
refine map.is_maximal ϕ'.to_map (localization_map_bijective_of_field hM' _ ϕ') hP,
rwa [← quotient.maximal_ideal_iff_is_field_quotient, ← bot_quotient_is_maximal_iff],
end
/-- Used to bootstrap the more general `quotient_mk_comp_C_is_integral_of_jacobson` -/
private lemma quotient_mk_comp_C_is_integral_of_jacobson' (hR : is_jacobson R)
(hP' : ∀ (x : R), C x ∈ P → x = 0) :
((quotient.mk P).comp C : R →+* P.quotient).is_integral :=
begin
refine (is_integral_quotient_map_iff _).mp _,
let P' : ideal R := P.comap C,
obtain ⟨pX, hpX, hp0⟩ :=
exists_nonzero_mem_of_ne_bot (ne_of_lt (bot_lt_of_maximal P polynomial_not_is_field)).symm hP',
let M : submonoid P'.quotient := submonoid.powers (pX.map (quotient.mk P')).leading_coeff,
let φ : P'.quotient →+* P.quotient := quotient_map P C le_rfl,
let ϕ' : localization_map (M.map ↑φ) (localization (M.map ↑φ)) := localization.of (M.map ↑φ),
haveI hp'_prime : P'.is_prime := comap_is_prime C P,
have hM : (0 : P'.quotient) ∉ M := λ ⟨n, hn⟩, hp0 $ leading_coeff_eq_zero.mp (pow_eq_zero hn),
refine ((quotient_map P C le_rfl).is_integral_tower_bot_of_is_integral
(localization.of (M.map ↑(quotient_map P C le_rfl))).to_map _ _),
{ refine ϕ'.injective (le_non_zero_divisors_of_domain (λ hM', hM _)),
exact (let ⟨z, zM, z0⟩ := hM' in (quotient_map_injective (trans z0 φ.map_zero.symm)) ▸ zM) },
{ let ϕ : localization_map M (localization M) := localization.of M,
rw ← (ϕ.map_comp _),
refine ring_hom.is_integral_trans ϕ.to_map
(ϕ.map (M.apply_coe_mem_map (φ : P'.quotient →* P.quotient)) ϕ') _ _,
{ exact ϕ.to_map.is_integral_of_surjective (localization_map_bijective_of_field hM
((quotient.maximal_ideal_iff_is_field_quotient _).mp
(is_maximal_comap_C_of_is_maximal P hP')) _).2 },
{ exact is_integral_localization_map_polynomial_quotient P pX hpX _ _ } }
end
/-- If `R` is a Jacobson ring, and `P` is a maximal ideal of `polynomial R`,
then `R → (polynomial R)/P` is an integral map. -/
lemma quotient_mk_comp_C_is_integral_of_jacobson :
((quotient.mk P).comp C : R →+* P.quotient).is_integral :=
begin
let P' : ideal R := P.comap C,
haveI : P'.is_prime := comap_is_prime C P,
let f : polynomial R →+* polynomial P'.quotient := polynomial.map_ring_hom (quotient.mk P'),
have hf : function.surjective f := map_surjective (quotient.mk P') quotient.mk_surjective,
have hPJ : P = (P.map f).comap f,
{ rw comap_map_of_surjective _ hf,
refine le_antisymm (le_sup_left_of_le le_rfl) (sup_le le_rfl _),
refine λ p hp, polynomial_mem_ideal_of_coeff_mem_ideal P p (λ n, quotient.eq_zero_iff_mem.mp _),
simpa only [coeff_map, coe_map_ring_hom] using (polynomial.ext_iff.mp hp) n },
refine ring_hom.is_integral_tower_bot_of_is_integral _ _ (injective_quotient_le_comap_map P) _,
rw ← quotient_mk_maps_eq,
refine ring_hom.is_integral_trans _ _
((quotient.mk P').is_integral_of_surjective quotient.mk_surjective) _,
apply quotient_mk_comp_C_is_integral_of_jacobson' _ _ (λ x hx, _),
any_goals { exact ideal.is_jacobson_quotient },
{ exact or.rec_on (map_eq_top_or_is_maximal_of_surjective f hf hP)
(λ h, absurd (trans (h ▸ hPJ : P = comap f ⊤) comap_top : P = ⊤) hP.ne_top) id },
{ obtain ⟨z, rfl⟩ := quotient.mk_surjective x,
rwa [quotient.eq_zero_iff_mem, mem_comap, hPJ, mem_comap, coe_map_ring_hom, map_C] }
end
lemma is_maximal_comap_C_of_is_jacobson : (P.comap (C : R →+* polynomial R)).is_maximal :=
begin
rw [← @mk_ker _ _ P, ring_hom.ker_eq_comap_bot, comap_comap],
exact is_maximal_comap_of_is_integral_of_is_maximal' _
(quotient_mk_comp_C_is_integral_of_jacobson P) ⊥ ((bot_quotient_is_maximal_iff _).mpr hP),
end
omit P hP
lemma comp_C_integral_of_surjective_of_jacobson
{S : Type*} [field S] (f : (polynomial R) →+* S) (hf : function.surjective f) :
(f.comp C).is_integral :=
begin
haveI : (f.ker).is_maximal := @comap_is_maximal_of_surjective _ _ _ _ f ⊥ hf bot_is_maximal,
let g : f.ker.quotient →+* S := ideal.quotient.lift f.ker f (λ _ h, h),
have hfg : (g.comp (quotient.mk f.ker)) = f := ring_hom_ext' rfl rfl,
rw [← hfg, ring_hom.comp_assoc],
refine ring_hom.is_integral_trans _ g (quotient_mk_comp_C_is_integral_of_jacobson f.ker)
(g.is_integral_of_surjective _), --(quotient.lift_surjective f.ker f _ hf)),
rw [← hfg] at hf,
exact function.surjective.of_comp hf,
end
end integral_domain
end polynomial
namespace mv_polynomial
open mv_polynomial ring_hom
lemma is_jacobson_mv_polynomial_fin {R : Type*} [comm_ring R] [H : is_jacobson R] :
∀ (n : ℕ), is_jacobson (mv_polynomial (fin n) R)
| 0 := ((is_jacobson_iso ((rename_equiv R
(equiv.equiv_pempty (fin 0))).to_ring_equiv.trans (pempty_ring_equiv R))).mpr H)
| (n+1) := (is_jacobson_iso (fin_succ_equiv R n).to_ring_equiv).2
(polynomial.is_jacobson_polynomial_iff_is_jacobson.2 (is_jacobson_mv_polynomial_fin n))
/-- General form of the nullstellensatz for Jacobson rings, since in a Jacobson ring we have
`Inf {P maximal | P ≥ I} = Inf {P prime | P ≥ I} = I.radical`. Fields are always Jacobson,
and in that special case this is (most of) the classical Nullstellensatz,
since `I(V(I))` is the intersection of maximal ideals containing `I`, which is then `I.radical` -/
instance {R : Type*} [comm_ring R] {ι : Type*} [fintype ι] [is_jacobson R] :
is_jacobson (mv_polynomial ι R) :=
begin
haveI := classical.dec_eq ι,
let e := fintype.equiv_fin ι,
rw is_jacobson_iso (rename_equiv R e).to_ring_equiv,
exact is_jacobson_mv_polynomial_fin _
end
variables {n : ℕ}
lemma quotient_mk_comp_C_is_integral_of_jacobson {R : Type*} [integral_domain R] [is_jacobson R]
(P : ideal (mv_polynomial (fin n) R)) [P.is_maximal] :
((quotient.mk P).comp mv_polynomial.C : R →+* P.quotient).is_integral :=
begin
unfreezingI {induction n with n IH},
{ refine ring_hom.is_integral_of_surjective _ (function.surjective.comp quotient.mk_surjective _),
exact C_surjective_fin_0 },
{ rw [← fin_succ_equiv_comp_C_eq_C, ← ring_hom.comp_assoc, ← ring_hom.comp_assoc,
← quotient_map_comp_mk le_rfl, ring_hom.comp_assoc (polynomial.C),
← quotient_map_comp_mk le_rfl, ring_hom.comp_assoc, ring_hom.comp_assoc,
← quotient_map_comp_mk le_rfl, ← ring_hom.comp_assoc (quotient.mk _)],
refine ring_hom.is_integral_trans _ _ _ _,
{ refine ring_hom.is_integral_trans _ _ (is_integral_of_surjective _ quotient.mk_surjective) _,
refine ring_hom.is_integral_trans _ _ _ _,
{ apply (is_integral_quotient_map_iff _).mpr (IH _),
apply polynomial.is_maximal_comap_C_of_is_jacobson _,
{ exact mv_polynomial.is_jacobson_mv_polynomial_fin n },
{ apply comap_is_maximal_of_surjective,
exact (fin_succ_equiv R n).symm.surjective } },
{ refine (is_integral_quotient_map_iff _).mpr _,
rw ← quotient_map_comp_mk le_rfl,
refine ring_hom.is_integral_trans _ _ _ ((is_integral_quotient_map_iff _).mpr _),
{ exact ring_hom.is_integral_of_surjective _ quotient.mk_surjective },
{ apply polynomial.quotient_mk_comp_C_is_integral_of_jacobson _,
{ exact mv_polynomial.is_jacobson_mv_polynomial_fin n },
{ exact comap_is_maximal_of_surjective _ (fin_succ_equiv R n).symm.surjective } } } },
{ refine (is_integral_quotient_map_iff _).mpr _,
refine ring_hom.is_integral_trans _ _ _ (is_integral_of_surjective _ quotient.mk_surjective),
exact ring_hom.is_integral_of_surjective _ (fin_succ_equiv R n).symm.surjective } }
end
lemma comp_C_integral_of_surjective_of_jacobson {R : Type*} [integral_domain R] [is_jacobson R]
{σ : Type*} [fintype σ] {S : Type*} [field S] (f : mv_polynomial σ R →+* S)
(hf : function.surjective f) : (f.comp C).is_integral :=
begin
haveI := classical.dec_eq σ,
obtain ⟨e⟩ := fintype.trunc_equiv_fin σ,
let f' : mv_polynomial (fin _) R →+* S :=
f.comp (rename_equiv R e.symm).to_ring_equiv.to_ring_hom,
have hf' : function.surjective f' :=
((function.surjective.comp hf (rename_equiv R e.symm).surjective)),
have : (f'.comp C).is_integral,
{ haveI : (f'.ker).is_maximal := @comap_is_maximal_of_surjective _ _ _ _ f' ⊥ hf' bot_is_maximal,
let g : f'.ker.quotient →+* S := ideal.quotient.lift f'.ker f' (λ _ h, h),
have hfg : (g.comp (quotient.mk f'.ker)) = f' := ring_hom_ext (λ r, rfl) (λ i, rfl),
rw [← hfg, ring_hom.comp_assoc],
refine ring_hom.is_integral_trans _ g (quotient_mk_comp_C_is_integral_of_jacobson f'.ker)
(g.is_integral_of_surjective _),
rw ← hfg at hf',
exact function.surjective.of_comp hf' },
rw ring_hom.comp_assoc at this,
convert this,
refine ring_hom.ext (λ x, _),
exact ((rename_equiv R e.symm).commutes' x).symm,
end
end mv_polynomial
end ideal
|
8bd583756db4896694b1cd8a65f1813cd27704d6 | de91c42b87530c3bdcc2b138ef1a3c3d9bee0d41 | /eval/time_expr_wip.lean | a1bf320ce382cc1c03fd4b046b9daee31931a56d | [] | no_license | kevinsullivan/lang | d3e526ba363dc1ddf5ff1c2f36607d7f891806a7 | e9d869bff94fb13ad9262222a6f3c4aafba82d5e | refs/heads/master | 1,687,840,064,795 | 1,628,047,969,000 | 1,628,047,969,000 | 282,210,749 | 0 | 1 | null | 1,608,153,830,000 | 1,595,592,637,000 | Lean | UTF-8 | Lean | false | false | 119 | lean | import ..expressions.time_expr_wip
open lang.time
def kp : K := 4
namespace lang.time
def ko : K := 4
end lang.time |
2e704bb09d21a846019d72a179c8eee1b8140d34 | e6b8240a90527fd55d42d0ec6649253d5d0bd414 | /src/ring_theory/algebra.lean | aba4fb9926786d5f94da2f5fdb7bd94de6ea5df2 | [
"Apache-2.0"
] | permissive | mattearnshaw/mathlib | ac90f9fb8168aa642223bea3ffd0286b0cfde44f | d8dc1445cf8a8c74f8df60b9f7a1f5cf10946666 | refs/heads/master | 1,606,308,351,137 | 1,576,594,130,000 | 1,576,594,130,000 | 228,666,195 | 0 | 0 | Apache-2.0 | 1,576,603,094,000 | 1,576,603,093,000 | null | UTF-8 | Lean | false | false | 22,470 | lean | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau
Algebra over Commutative Ring (under category)
-/
import data.polynomial data.mv_polynomial
import data.complex.basic
import data.matrix.basic
import linear_algebra.tensor_product
import ring_theory.subring
noncomputable theory
universes u v w u₁ v₁
open lattice
open_locale tensor_product
section prio
-- We set this priority to 0 later in this file
set_option default_priority 200 -- see Note [default priority]
/-- The category of R-algebras where R is a commutative
ring is the under category R ↓ CRing. In the categorical
setting we have a forgetful functor R-Alg ⥤ R-Mod.
However here it extends module in order to preserve
definitional equality in certain cases. -/
class algebra (R : Type u) (A : Type v) [comm_ring R] [ring A] extends has_scalar R A :=
(to_fun : R → A) [hom : is_ring_hom to_fun]
(commutes' : ∀ r x, x * to_fun r = to_fun r * x)
(smul_def' : ∀ r x, r • x = to_fun r * x)
end prio
def algebra_map {R : Type u} (A : Type v) [comm_ring R] [ring A] [algebra R A] (x : R) : A :=
algebra.to_fun A x
namespace algebra
variables {R : Type u} {S : Type v} {A : Type w}
variables [comm_ring R] [comm_ring S] [ring A] [algebra R A]
instance : is_ring_hom (algebra_map A : R → A) := algebra.hom _ A
variables (A)
@[simp] lemma map_add (r s : R) : algebra_map A (r + s) = algebra_map A r + algebra_map A s :=
is_ring_hom.map_add _
@[simp] lemma map_neg (r : R) : algebra_map A (-r) = -algebra_map A r :=
is_ring_hom.map_neg _
@[simp] lemma map_sub (r s : R) : algebra_map A (r - s) = algebra_map A r - algebra_map A s :=
is_ring_hom.map_sub _
@[simp] lemma map_mul (r s : R) : algebra_map A (r * s) = algebra_map A r * algebra_map A s :=
is_ring_hom.map_mul _
variables (R)
@[simp] lemma map_zero : algebra_map A (0 : R) = 0 :=
is_ring_hom.map_zero _
@[simp] lemma map_one : algebra_map A (1 : R) = 1 :=
is_ring_hom.map_one _
variables {R A}
/-- Creating an algebra from a morphism in CRing. -/
def of_ring_hom (i : R → S) (hom : is_ring_hom i) : algebra R S :=
{ smul := λ c x, i c * x,
to_fun := i,
commutes' := λ _ _, mul_comm _ _,
smul_def' := λ c x, rfl }
lemma smul_def'' (r : R) (x : A) : r • x = algebra_map A r * x :=
algebra.smul_def' r x
@[priority 200] -- see Note [lower instance priority]
instance to_module : module R A :=
{ one_smul := by simp [smul_def''],
mul_smul := by simp [smul_def'', mul_assoc],
smul_add := by simp [smul_def'', mul_add],
smul_zero := by simp [smul_def''],
add_smul := by simp [smul_def'', add_mul],
zero_smul := by simp [smul_def''] }
-- from now on, we don't want to use the following instance anymore
attribute [instance, priority 0] algebra.to_has_scalar
lemma smul_def (r : R) (x : A) : r • x = algebra_map A r * x :=
algebra.smul_def' r x
theorem commutes (r : R) (x : A) : x * algebra_map A r = algebra_map A r * x :=
algebra.commutes' r x
theorem left_comm (r : R) (x y : A) : x * (algebra_map A r * y) = algebra_map A r * (x * y) :=
by rw [← mul_assoc, commutes, mul_assoc]
@[simp] lemma mul_smul_comm (s : R) (x y : A) :
x * (s • y) = s • (x * y) :=
by rw [smul_def, smul_def, left_comm]
@[simp] lemma smul_mul_assoc (r : R) (x y : A) :
(r • x) * y = r • (x * y) :=
by rw [smul_def, smul_def, mul_assoc]
/-- R[X] is the generator of the category R-Alg. -/
instance polynomial (R : Type u) [comm_ring R] : algebra R (polynomial R) :=
{ to_fun := polynomial.C,
commutes' := λ _ _, mul_comm _ _,
smul_def' := λ c p, (polynomial.C_mul' c p).symm,
.. polynomial.module }
/-- The algebra of multivariate polynomials. -/
instance mv_polynomial (R : Type u) [comm_ring R]
(ι : Type v) : algebra R (mv_polynomial ι R) :=
{ to_fun := mv_polynomial.C,
commutes' := λ _ _, mul_comm _ _,
smul_def' := λ c p, (mv_polynomial.C_mul' c p).symm,
.. mv_polynomial.module }
/-- Creating an algebra from a subring. This is the dual of ring extension. -/
instance of_subring (S : set R) [is_subring S] : algebra S R :=
of_ring_hom subtype.val ⟨rfl, λ _ _, rfl, λ _ _, rfl⟩
variables (R A)
/-- The multiplication in an algebra is a bilinear map. -/
def lmul : A →ₗ A →ₗ A :=
linear_map.mk₂ R (*)
(λ x y z, add_mul x y z)
(λ c x y, by rw [smul_def, smul_def, mul_assoc _ x y])
(λ x y z, mul_add x y z)
(λ c x y, by rw [smul_def, smul_def, left_comm])
def lmul_left (r : A) : A →ₗ A :=
lmul R A r
def lmul_right (r : A) : A →ₗ A :=
(lmul R A).flip r
variables {R A}
@[simp] lemma lmul_apply (p q : A) : lmul R A p q = p * q := rfl
@[simp] lemma lmul_left_apply (p q : A) : lmul_left R A p q = p * q := rfl
@[simp] lemma lmul_right_apply (p q : A) : lmul_right R A p q = q * p := rfl
end algebra
instance module.endomorphism_algebra (R : Type u) (M : Type v)
[comm_ring R] [add_comm_group M] [module R M] : algebra R (M →ₗ[R] M) :=
{ to_fun := (λ r, r • linear_map.id),
hom := by apply is_ring_hom.mk; intros; ext; simp [mul_smul, add_smul],
commutes' := by intros; ext; simp,
smul_def' := by intros; ext; simp }
set_option class.instance_max_depth 40
instance matrix_algebra (n : Type u) (R : Type v)
[fintype n] [decidable_eq n] [comm_ring R] : algebra R (matrix n n R) :=
{ to_fun := (λ r, r • 1),
hom := { map_one := by simp,
map_mul := by { intros, simp [mul_smul], },
map_add := by { intros, simp [add_smul], } },
commutes' := by { intros, simp },
smul_def' := by { intros, simp } }
set_option old_structure_cmd true
/-- Defining the homomorphism in the category R-Alg. -/
structure alg_hom (R : Type u) (A : Type v) (B : Type w)
[comm_ring R] [ring A] [ring B] [algebra R A] [algebra R B] extends ring_hom A B :=
(commutes' : ∀ r : R, to_fun (algebra_map A r) = algebra_map B r)
infixr ` →ₐ `:25 := alg_hom _
notation A ` →ₐ[`:25 R `] ` B := alg_hom R A B
namespace alg_hom
variables {R : Type u} {A : Type v} {B : Type w} {C : Type u₁} {D : Type v₁}
variables {rR : comm_ring R} {rA : ring A} {rB : ring B} {rC : ring C} {rD : ring D}
variables {aA : algebra R A} {aB : algebra R B} {aC : algebra R C} {aD : algebra R D}
include R rR rA rB aA aB
instance : has_coe_to_fun (A →ₐ[R] B) := ⟨_, λ f, f.to_fun⟩
instance : has_coe (A →ₐ[R] B) (A →+* B) := ⟨alg_hom.to_ring_hom⟩
variables (φ : A →ₐ[R] B)
instance : is_ring_hom ⇑φ := ring_hom.is_ring_hom φ.to_ring_hom
@[ext]
theorem ext {φ₁ φ₂ : A →ₐ[R] B} (H : ∀ x, φ₁ x = φ₂ x) : φ₁ = φ₂ :=
by cases φ₁; cases φ₂; congr' 1; ext; apply H
theorem commutes (r : R) : φ (algebra_map A r) = algebra_map B r := φ.commutes' r
@[simp] lemma map_add (r s : A) : φ (r + s) = φ r + φ s :=
is_ring_hom.map_add _
@[simp] lemma map_zero : φ 0 = 0 :=
is_ring_hom.map_zero _
@[simp] lemma map_neg (x) : φ (-x) = -φ x :=
is_ring_hom.map_neg _
@[simp] lemma map_sub (x y) : φ (x - y) = φ x - φ y :=
is_ring_hom.map_sub _
@[simp] lemma map_mul (x y) : φ (x * y) = φ x * φ y :=
is_ring_hom.map_mul _
@[simp] lemma map_one : φ 1 = 1 :=
is_ring_hom.map_one _
/-- R-Alg ⥤ R-Mod -/
def to_linear_map : A →ₗ B :=
{ to_fun := φ,
add := φ.map_add,
smul := λ (c : R) x, by rw [algebra.smul_def, φ.map_mul, φ.commutes c, algebra.smul_def] }
@[simp] lemma to_linear_map_apply (p : A) : φ.to_linear_map p = φ p := rfl
theorem to_linear_map_inj {φ₁ φ₂ : A →ₐ[R] B} (H : φ₁.to_linear_map = φ₂.to_linear_map) : φ₁ = φ₂ :=
ext $ λ x, show φ₁.to_linear_map x = φ₂.to_linear_map x, by rw H
variables (R A)
omit rB aB
variables [rR] [rA] [aA]
protected def id : A →ₐ[R] A :=
{ commutes' := λ _, rfl,
..ring_hom.id A }
variables {R A rR rA aA}
@[simp] lemma id_to_linear_map :
(alg_hom.id R A).to_linear_map = @linear_map.id R A _ _ _ := rfl
@[simp] lemma id_apply (p : A) : alg_hom.id R A p = p := rfl
include rB rC aB aC
def comp (φ₁ : B →ₐ[R] C) (φ₂ : A →ₐ[R] B) : A →ₐ[R] C :=
{ commutes' := λ r : R, by rw [← φ₁.commutes, ← φ₂.commutes]; refl,
.. φ₁.to_ring_hom.comp ↑φ₂ }
@[simp] lemma comp_to_linear_map (f : A →ₐ[R] B) (g : B →ₐ[R] C) :
(g.comp f).to_linear_map = g.to_linear_map.comp f.to_linear_map := rfl
@[simp] lemma comp_apply (φ₁ : B →ₐ[R] C) (φ₂ : A →ₐ[R] B) (p : A) :
φ₁.comp φ₂ p = φ₁ (φ₂ p) := rfl
omit rC aC
@[simp] theorem comp_id : φ.comp (alg_hom.id R A) = φ :=
ext $ λ x, rfl
@[simp] theorem id_comp : (alg_hom.id R B).comp φ = φ :=
ext $ λ x, rfl
include rC aC rD aD
theorem comp_assoc (φ₁ : C →ₐ[R] D) (φ₂ : B →ₐ[R] C) (φ₃ : A →ₐ[R] B) :
(φ₁.comp φ₂).comp φ₃ = φ₁.comp (φ₂.comp φ₃) :=
ext $ λ x, rfl
end alg_hom
namespace algebra
variables (R : Type u) (S : Type v) (A : Type w)
include R S A
/-- `comap R S A` is a type alias for `A`, and has an R-algebra structure defined on it
when `algebra R S` and `algebra S A`. -/
/- This is done to avoid a type class search with meta-variables `algebra R ?m_1` and
`algebra ?m_1 A -/
/- The `nolint` attribute is added because it has unused arguments `R` and `S`, but these are necessary for synthesizing the
appropriate type classes -/
@[nolint] def comap : Type w := A
def comap.to_comap : A → comap R S A := id
def comap.of_comap : comap R S A → A := id
omit R S A
variables [comm_ring R] [comm_ring S] [ring A] [algebra R S] [algebra S A]
instance comap.ring : ring (comap R S A) := _inst_3
instance comap.comm_ring (R : Type u) (S : Type v) (A : Type w)
[comm_ring R] [comm_ring S] [comm_ring A] [algebra R S] [algebra S A] :
comm_ring (comap R S A) := _inst_8
instance comap.module : module S (comap R S A) := show module S A, by apply_instance
instance comap.has_scalar : has_scalar S (comap R S A) := show has_scalar S A, by apply_instance
set_option class.instance_max_depth 40
/-- R ⟶ S induces S-Alg ⥤ R-Alg -/
instance comap.algebra : algebra R (comap R S A) :=
{ smul := λ r x, (algebra_map S r • x : A),
to_fun := (algebra_map A : S → A) ∘ algebra_map S,
hom := by letI : is_ring_hom (algebra_map A) := _inst_5.hom; apply_instance,
commutes' := λ r x, algebra.commutes _ _,
smul_def' := λ _ _, algebra.smul_def _ _ }
def to_comap : S →ₐ[R] comap R S A :=
{ commutes' := λ r, rfl,
..ring_hom.of (algebra_map A : S → A) }
theorem to_comap_apply (x) : to_comap R S A x = (algebra_map A : S → A) x := rfl
end algebra
namespace alg_hom
variables {R : Type u} {S : Type v} {A : Type w} {B : Type u₁}
variables [comm_ring R] [comm_ring S] [ring A] [ring B]
variables [algebra R S] [algebra S A] [algebra S B] (φ : A →ₐ[S] B)
include R
/-- R ⟶ S induces S-Alg ⥤ R-Alg -/
def comap : algebra.comap R S A →ₐ[R] algebra.comap R S B :=
{ commutes' := λ r, φ.commutes (algebra_map S r)
..φ }
end alg_hom
namespace polynomial
variables (R : Type u) (A : Type v)
variables [comm_ring R] [comm_ring A] [algebra R A]
variables (x : A)
/-- A → Hom[R-Alg](R[X],A) -/
def aeval : polynomial R →ₐ[R] A :=
{ commutes' := λ r, eval₂_C _ _,
..ring_hom.of (eval₂ (algebra_map A) x) }
theorem aeval_def (p : polynomial R) : aeval R A x p = eval₂ (algebra_map A) x p := rfl
@[simp] lemma aeval_X : aeval R A x X = x := eval₂_X _ x
@[simp] lemma aeval_C (r : R) : aeval R A x (C r) = algebra_map A r := eval₂_C _ x
instance aeval.is_ring_hom : is_ring_hom (aeval R A x) :=
by apply_instance
theorem eval_unique (φ : polynomial R →ₐ[R] A) (p) :
φ p = eval₂ (algebra_map A) (φ X) p :=
begin
apply polynomial.induction_on p,
{ intro r, rw eval₂_C, exact φ.commutes r },
{ intros f g ih1 ih2,
rw [is_ring_hom.map_add φ, ih1, ih2, eval₂_add] },
{ intros n r ih,
rw [pow_succ', ← mul_assoc, is_ring_hom.map_mul φ, eval₂_mul (algebra_map A : R → A), eval₂_X, ih] }
end
end polynomial
namespace mv_polynomial
variables (R : Type u) (A : Type v)
variables [comm_ring R] [comm_ring A] [algebra R A]
variables (σ : set A)
/-- (ι → A) → Hom[R-Alg](R[ι],A) -/
def aeval : mv_polynomial σ R →ₐ[R] A :=
{ commutes' := λ r, eval₂_C _ _ _
..ring_hom.of (eval₂ (algebra_map A) subtype.val) }
theorem aeval_def (p : mv_polynomial σ R) : aeval R A σ p = eval₂ (algebra_map A) subtype.val p := rfl
@[simp] lemma aeval_X (s : σ) : aeval R A σ (X s) = s := eval₂_X _ _ _
@[simp] lemma aeval_C (r : R) : aeval R A σ (C r) = algebra_map A r := eval₂_C _ _ _
instance aeval.is_ring_hom : is_ring_hom (aeval R A σ) :=
by apply_instance
variables (ι : Type w)
theorem eval_unique (φ : mv_polynomial ι R →ₐ[R] A) (p) :
φ p = eval₂ (algebra_map A) (φ ∘ X) p :=
begin
apply mv_polynomial.induction_on p,
{ intro r, rw eval₂_C, exact φ.commutes r },
{ intros f g ih1 ih2,
rw [is_ring_hom.map_add φ, ih1, ih2, eval₂_add] },
{ intros p j ih,
rw [is_ring_hom.map_mul φ, eval₂_mul, eval₂_X, ih] }
end
end mv_polynomial
namespace rat
instance algebra_rat {α} [field α] [char_zero α] : algebra ℚ α :=
algebra.of_ring_hom rat.cast (by apply_instance)
end rat
namespace complex
instance algebra_over_reals : algebra ℝ ℂ :=
algebra.of_ring_hom coe $ by constructor; intros; simp [one_re]
instance : has_scalar ℝ ℂ := { smul := λ r c, ↑r * c}
end complex
structure subalgebra (R : Type u) (A : Type v)
[comm_ring R] [ring A] [algebra R A] : Type v :=
(carrier : set A) [subring : is_subring carrier]
(range_le' : set.range (algebra_map A : R → A) ≤ carrier)
namespace subalgebra
variables {R : Type u} {A : Type v}
variables [comm_ring R] [ring A] [algebra R A]
include R
instance : has_coe (subalgebra R A) (set A) :=
⟨λ S, S.carrier⟩
lemma range_le (S : subalgebra R A) : set.range (algebra_map A : R → A) ≤ S := S.range_le'
instance : has_mem A (subalgebra R A) :=
⟨λ x S, x ∈ (S : set A)⟩
variables {A}
theorem mem_coe {x : A} {s : subalgebra R A} : x ∈ (s : set A) ↔ x ∈ s :=
iff.rfl
@[ext] theorem ext {S T : subalgebra R A}
(h : ∀ x : A, x ∈ S ↔ x ∈ T) : S = T :=
by cases S; cases T; congr; ext x; exact h x
variables (S : subalgebra R A)
instance : is_subring (S : set A) := S.subring
instance : ring S := @@subtype.ring _ S.is_subring
instance (R : Type u) (A : Type v) {rR : comm_ring R} [comm_ring A]
{aA : algebra R A} (S : subalgebra R A) : comm_ring S := @@subtype.comm_ring _ S.is_subring
instance algebra : algebra R S :=
{ smul := λ (c:R) x, ⟨c • x.1,
by rw algebra.smul_def; exact @@is_submonoid.mul_mem _ S.2.2 (S.3 ⟨c, rfl⟩) x.2⟩,
to_fun := λ r, ⟨algebra_map A r, S.range_le ⟨r, rfl⟩⟩,
hom := ⟨subtype.eq $ algebra.map_one R A, λ x y, subtype.eq $ algebra.map_mul A x y,
λ x y, subtype.eq $ algebra.map_add A x y⟩,
commutes' := λ c x, subtype.eq $ by apply _inst_3.4,
smul_def' := λ c x, subtype.eq $ by apply _inst_3.5 }
instance to_algebra (R : Type u) (A : Type v) [comm_ring R] [comm_ring A]
[algebra R A] (S : subalgebra R A) : algebra S A :=
algebra.of_subring _
def val : S →ₐ[R] A :=
by refine_struct { to_fun := subtype.val }; intros; refl
def to_submodule : submodule R A :=
{ carrier := S,
zero := (0:S).2,
add := λ x y hx hy, (⟨x, hx⟩ + ⟨y, hy⟩ : S).2,
smul := λ c x hx, (algebra.smul_def c x).symm ▸ (⟨algebra_map A c, S.range_le ⟨c, rfl⟩⟩ * ⟨x, hx⟩:S).2 }
instance coe_to_submodule : has_coe (subalgebra R A) (submodule R A) :=
⟨to_submodule⟩
instance to_submodule.is_subring : is_subring ((S : submodule R A) : set A) := S.2
instance : partial_order (subalgebra R A) :=
{ le := λ S T, (S : set A) ≤ (T : set A),
le_refl := λ _, le_refl _,
le_trans := λ _ _ _, le_trans,
le_antisymm := λ S T hst hts, ext $ λ x, ⟨@hst x, @hts x⟩ }
def comap {R : Type u} {S : Type v} {A : Type w}
[comm_ring R] [comm_ring S] [ring A] [algebra R S] [algebra S A]
(iSB : subalgebra S A) : subalgebra R (algebra.comap R S A) :=
{ carrier := (iSB : set A),
subring := iSB.is_subring,
range_le' := λ a ⟨r, hr⟩, hr ▸ iSB.range_le ⟨_, rfl⟩ }
def under {R : Type u} {A : Type v} [comm_ring R] [comm_ring A]
{i : algebra R A} (S : subalgebra R A)
(T : subalgebra S A) : subalgebra R A :=
{ carrier := T,
range_le' := (λ a ⟨r, hr⟩, hr ▸ T.range_le ⟨⟨algebra_map A r, S.range_le ⟨r, rfl⟩⟩, rfl⟩) }
end subalgebra
namespace alg_hom
variables {R : Type u} {A : Type v} {B : Type w}
variables [comm_ring R] [ring A] [ring B] [algebra R A] [algebra R B]
variables (φ : A →ₐ[R] B)
protected def range : subalgebra R B :=
{ carrier := set.range φ,
subring :=
{ one_mem := ⟨1, φ.map_one⟩,
mul_mem := λ y₁ y₂ ⟨x₁, hx₁⟩ ⟨x₂, hx₂⟩, ⟨x₁ * x₂, hx₁ ▸ hx₂ ▸ φ.map_mul x₁ x₂⟩ },
range_le' := λ y ⟨r, hr⟩, ⟨algebra_map A r, hr ▸ φ.commutes r⟩ }
end alg_hom
namespace algebra
variables {R : Type u} (A : Type v)
variables [comm_ring R] [ring A] [algebra R A]
include R
variables (R)
instance id : algebra R R :=
algebra.of_ring_hom id $ by apply_instance
namespace id
@[simp] lemma map_eq_self (x : R) : algebra_map R x = x := rfl
@[simp] lemma smul_eq_mul (x y : R) : x • y = x * y := rfl
end id
def of_id : R →ₐ A :=
{ commutes' := λ _, rfl, .. ring_hom.of (algebra_map A) }
variables {R}
theorem of_id_apply (r) : of_id R A r = algebra_map A r := rfl
variables (R) {A}
def adjoin (s : set A) : subalgebra R A :=
{ carrier := ring.closure (set.range (algebra_map A : R → A) ∪ s),
range_le' := le_trans (set.subset_union_left _ _) ring.subset_closure }
variables {R}
protected lemma gc : galois_connection (adjoin R : set A → subalgebra R A) coe :=
λ s S, ⟨λ H, le_trans (le_trans (set.subset_union_right _ _) ring.subset_closure) H,
λ H, ring.closure_subset $ set.union_subset S.range_le H⟩
protected def gi : galois_insertion (adjoin R : set A → subalgebra R A) coe :=
{ choice := λ s hs, adjoin R s,
gc := algebra.gc,
le_l_u := λ S, (algebra.gc (S : set A) (adjoin R S)).1 $ le_refl _,
choice_eq := λ _ _, rfl }
instance : complete_lattice (subalgebra R A) :=
galois_insertion.lift_complete_lattice algebra.gi
theorem mem_bot {x : A} : x ∈ (⊥ : subalgebra R A) ↔ x ∈ set.range (algebra_map A : R → A) :=
suffices (⊥ : subalgebra R A) = (of_id R A).range, by rw this; refl,
le_antisymm bot_le $ subalgebra.range_le _
theorem mem_top {x : A} : x ∈ (⊤ : subalgebra R A) :=
ring.mem_closure $ or.inr trivial
def to_top : A →ₐ[R] (⊤ : subalgebra R A) :=
by refine_struct { to_fun := λ x, (⟨x, mem_top⟩ : (⊤ : subalgebra R A)) }; intros; refl
end algebra
section int
variables (R : Type*) [comm_ring R]
/-- CRing ⥤ ℤ-Alg -/
def alg_hom_int
{R : Type u} [comm_ring R] [algebra ℤ R]
{S : Type v} [comm_ring S] [algebra ℤ S]
(f : R → S) [is_ring_hom f] : R →ₐ[ℤ] S :=
{ commutes' := λ i, by change (ring_hom.of f).to_fun with f; exact
int.induction_on i (by rw [algebra.map_zero, algebra.map_zero, is_ring_hom.map_zero f])
(λ i ih, by rw [algebra.map_add, algebra.map_add, algebra.map_one, algebra.map_one];
rw [is_ring_hom.map_add f, is_ring_hom.map_one f, ih])
(λ i ih, by rw [algebra.map_sub, algebra.map_sub, algebra.map_one, algebra.map_one];
rw [is_ring_hom.map_sub f, is_ring_hom.map_one f, ih]),
..ring_hom.of f }
/-- CRing ⥤ ℤ-Alg -/
instance algebra_int : algebra ℤ R :=
algebra.of_ring_hom coe $ by constructor; intros; simp
variables {R}
/-- CRing ⥤ ℤ-Alg -/
def subalgebra_of_subring (S : set R) [is_subring S] : subalgebra ℤ R :=
{ carrier := S, range_le' := λ x ⟨i, h⟩, h ▸ int.induction_on i
(by rw algebra.map_zero; exact is_add_submonoid.zero_mem _)
(λ i hi, by rw [algebra.map_add, algebra.map_one]; exact is_add_submonoid.add_mem hi (is_submonoid.one_mem _))
(λ i hi, by rw [algebra.map_sub, algebra.map_one]; exact is_add_subgroup.sub_mem _ _ _ hi (is_submonoid.one_mem _)) }
@[simp] lemma mem_subalgebra_of_subring {x : R} {S : set R} [is_subring S] :
x ∈ subalgebra_of_subring S ↔ x ∈ S :=
iff.rfl
section span_int
open submodule
lemma span_int_eq_add_group_closure (s : set R) :
↑(span ℤ s) = add_group.closure s :=
set.subset.antisymm (λ x hx, span_induction hx
(λ _, add_group.mem_closure)
(is_add_submonoid.zero_mem _)
(λ a b ha hb, is_add_submonoid.add_mem ha hb)
(λ n a ha, by { exact is_add_subgroup.gsmul_mem ha }))
(add_group.closure_subset subset_span)
@[simp] lemma span_int_eq (s : set R) [is_add_subgroup s] :
(↑(span ℤ s) : set R) = s :=
by rw [span_int_eq_add_group_closure, add_group.closure_add_subgroup]
end span_int
end int
section restrict_scalars
/- In this section, we describe restriction of scalars: if `S` is an algebra over `R`, then
`S`-modules are also `R`-modules. -/
variables (R : Type*) [comm_ring R] (S : Type*) [comm_ring S] [algebra R S]
(E : Type*) [add_comm_group E] [module S E] {F : Type*} [add_comm_group F] [module S F]
/-- When `E` is a module over a ring `S`, and `S` is an algebra over `R`, then `E` inherits a
module structure over `R`, called `module.restrict S R E`.
Not registered as an instance as `S` can not be inferred. -/
def module.restrict_scalars : module R E :=
{ smul := λc x, (algebra_map S c) • x,
one_smul := by simp,
mul_smul := by simp [mul_smul],
smul_add := by simp [smul_add],
smul_zero := by simp [smul_zero],
add_smul := by simp [add_smul],
zero_smul := by simp [zero_smul] }
variables {S E}
local attribute [instance] module.restrict_scalars
/-- The `R`-linear map induced by an `S`-linear map when `S` is an algebra over `R`. -/
def linear_map.restrict_scalars (f : E →ₗ[S] F) : E →ₗ[R] F :=
{ to_fun := f.to_fun,
add := λx y, f.map_add x y,
smul := λc x, f.map_smul (algebra_map S c) x }
@[simp, squash_cast] lemma linear_map.coe_restrict_scalars_eq_coe (f : E →ₗ[S] F) :
(f.restrict_scalars R : E → F) = f := rfl
/- Register as an instance (with low priority) the fact that a complex vector space is also a real
vector space. -/
instance module.complex_to_real (E : Type*) [add_comm_group E] [module ℂ E] : module ℝ E :=
module.restrict_scalars ℝ ℂ E
attribute [instance, priority 900] module.complex_to_real
end restrict_scalars
|
cdfb64e083bfe98f61653a6dad2805b4e04d8d64 | c777c32c8e484e195053731103c5e52af26a25d1 | /src/analysis/inner_product_space/symmetric.lean | 4033741a0e5b3c0d6d68f82515d08f9d9d91195f | [
"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 | 7,576 | lean | /-
Copyright (c) 2022 Anatole Dedecker. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Moritz Doll, Frédéric Dupuis, Heather Macbeth
-/
import analysis.inner_product_space.basic
import analysis.normed_space.banach
import linear_algebra.sesquilinear_form
/-!
# Symmetric linear maps in an inner product space
This file defines and proves basic theorems about symmetric **not necessarily bounded** operators
on an inner product space, i.e linear maps `T : E → E` such that `∀ x y, ⟪T x, y⟫ = ⟪x, T y⟫`.
In comparison to `is_self_adjoint`, this definition works for non-continuous linear maps, and
doesn't rely on the definition of the adjoint, which allows it to be stated in non-complete space.
## Main definitions
* `linear_map.is_symmetric`: a (not necessarily bounded) operator on an inner product space is
symmetric, if for all `x`, `y`, we have `⟪T x, y⟫ = ⟪x, T y⟫`
## Main statements
* `is_symmetric.continuous`: if a symmetric operator is defined on a complete space, then
it is automatically continuous.
## Tags
self-adjoint, symmetric
-/
open is_R_or_C
open_locale complex_conjugate
variables {𝕜 E E' F G : Type*} [is_R_or_C 𝕜]
variables [normed_add_comm_group E] [inner_product_space 𝕜 E]
variables [normed_add_comm_group F] [inner_product_space 𝕜 F]
variables [normed_add_comm_group G] [inner_product_space 𝕜 G]
variables [normed_add_comm_group E'] [inner_product_space ℝ E']
local notation `⟪`x`, `y`⟫` := @inner 𝕜 _ _ x y
namespace linear_map
/-! ### Symmetric operators -/
/-- A (not necessarily bounded) operator on an inner product space is symmetric, if for all
`x`, `y`, we have `⟪T x, y⟫ = ⟪x, T y⟫`. -/
def is_symmetric (T : E →ₗ[𝕜] E) : Prop := ∀ x y, ⟪T x, y⟫ = ⟪x, T y⟫
section real
variables
/-- An operator `T` on an inner product space is symmetric if and only if it is
`linear_map.is_self_adjoint` with respect to the sesquilinear form given by the inner product. -/
lemma is_symmetric_iff_sesq_form (T : E →ₗ[𝕜] E) :
T.is_symmetric ↔
@linear_map.is_self_adjoint 𝕜 E _ _ _ (star_ring_end 𝕜) sesq_form_of_inner T :=
⟨λ h x y, (h y x).symm, λ h x y, (h y x).symm⟩
end real
lemma is_symmetric.conj_inner_sym {T : E →ₗ[𝕜] E} (hT : is_symmetric T) (x y : E) :
conj ⟪T x, y⟫ = ⟪T y, x⟫ :=
by rw [hT x y, inner_conj_symm]
@[simp] lemma is_symmetric.apply_clm {T : E →L[𝕜] E} (hT : is_symmetric (T : E →ₗ[𝕜] E))
(x y : E) : ⟪T x, y⟫ = ⟪x, T y⟫ :=
hT x y
lemma is_symmetric_zero : (0 : E →ₗ[𝕜] E).is_symmetric :=
λ x y, (inner_zero_right x : ⟪x, 0⟫ = 0).symm ▸ (inner_zero_left y : ⟪0, y⟫ = 0)
lemma is_symmetric_id : (linear_map.id : E →ₗ[𝕜] E).is_symmetric :=
λ x y, rfl
lemma is_symmetric.add {T S : E →ₗ[𝕜] E} (hT : T.is_symmetric) (hS : S.is_symmetric) :
(T + S).is_symmetric :=
begin
intros x y,
rw [linear_map.add_apply, inner_add_left, hT x y, hS x y, ← inner_add_right],
refl
end
/-- The **Hellinger--Toeplitz theorem**: if a symmetric operator is defined on a complete space,
then it is automatically continuous. -/
lemma is_symmetric.continuous [complete_space E] {T : E →ₗ[𝕜] E} (hT : is_symmetric T) :
continuous T :=
begin
-- We prove it by using the closed graph theorem
refine T.continuous_of_seq_closed_graph (λ u x y hu hTu, _),
rw [←sub_eq_zero, ←@inner_self_eq_zero 𝕜],
have hlhs : ∀ k : ℕ, ⟪T (u k) - T x, y - T x⟫ = ⟪u k - x, T (y - T x)⟫ :=
by { intro k, rw [←T.map_sub, hT] },
refine tendsto_nhds_unique ((hTu.sub_const _).inner tendsto_const_nhds) _,
simp_rw hlhs,
rw ←inner_zero_left (T (y - T x)),
refine filter.tendsto.inner _ tendsto_const_nhds,
rw ←sub_self x,
exact hu.sub_const _,
end
/-- For a symmetric operator `T`, the function `λ x, ⟪T x, x⟫` is real-valued. -/
@[simp] lemma is_symmetric.coe_re_apply_inner_self_apply
{T : E →L[𝕜] E} (hT : is_symmetric (T : E →ₗ[𝕜] E)) (x : E) :
(T.re_apply_inner_self x : 𝕜) = ⟪T x, x⟫ :=
begin
rsuffices ⟨r, hr⟩ : ∃ r : ℝ, ⟪T x, x⟫ = r,
{ simp [hr, T.re_apply_inner_self_apply] },
rw ← conj_eq_iff_real,
exact hT.conj_inner_sym x x
end
/-- If a symmetric operator preserves a submodule, its restriction to that submodule is
symmetric. -/
lemma is_symmetric.restrict_invariant {T : E →ₗ[𝕜] E} (hT : is_symmetric T)
{V : submodule 𝕜 E} (hV : ∀ v ∈ V, T v ∈ V) :
is_symmetric (T.restrict hV) :=
λ v w, hT v w
lemma is_symmetric.restrict_scalars {T : E →ₗ[𝕜] E} (hT : T.is_symmetric) :
@linear_map.is_symmetric ℝ E _ _ (inner_product_space.is_R_or_C_to_real 𝕜 E)
(@linear_map.restrict_scalars ℝ 𝕜 _ _ _ _ _ _
(inner_product_space.is_R_or_C_to_real 𝕜 E).to_module
(inner_product_space.is_R_or_C_to_real 𝕜 E).to_module _ _ _ T) :=
λ x y, by simp [hT x y, real_inner_eq_re_inner, linear_map.coe_restrict_scalars_eq_coe]
section complex
variables {V : Type*}
[normed_add_comm_group V] [inner_product_space ℂ V]
/-- A linear operator on a complex inner product space is symmetric precisely when
`⟪T v, v⟫_ℂ` is real for all v.-/
lemma is_symmetric_iff_inner_map_self_real (T : V →ₗ[ℂ] V):
is_symmetric T ↔ ∀ (v : V), conj ⟪T v, v⟫_ℂ = ⟪T v, v⟫_ℂ :=
begin
split,
{ intros hT v,
apply is_symmetric.conj_inner_sym hT },
{ intros h x y,
nth_rewrite 1 ← inner_conj_symm,
nth_rewrite 1 inner_map_polarization,
simp only [star_ring_end_apply, star_div', star_sub, star_add, star_mul],
simp only [← star_ring_end_apply],
rw [h (x + y), h (x - y), h (x + complex.I • y), h (x - complex.I • y)],
simp only [complex.conj_I],
rw inner_map_polarization',
norm_num,
ring },
end
end complex
/-- Polarization identity for symmetric linear maps.
See `inner_map_polarization` for the complex version without the symmetric assumption. -/
lemma is_symmetric.inner_map_polarization {T : E →ₗ[𝕜] E} (hT : T.is_symmetric) (x y : E) :
⟪T x, y⟫ = (⟪T (x + y), x + y⟫ - ⟪T (x - y), x - y⟫ -
I * ⟪T (x + (I : 𝕜) • y), x + (I : 𝕜) • y⟫ +
I * ⟪T (x - (I : 𝕜) • y), x - (I : 𝕜) • y⟫) / 4 :=
begin
rcases @I_mul_I_ax 𝕜 _ with (h | h),
{ simp_rw [h, zero_mul, sub_zero, add_zero, map_add, map_sub, inner_add_left,
inner_add_right, inner_sub_left, inner_sub_right, hT x, ← inner_conj_symm x (T y)],
suffices : (re ⟪T y, x⟫ : 𝕜) = ⟪T y, x⟫,
{ rw conj_eq_iff_re.mpr this,
ring, },
{ rw ← re_add_im ⟪T y, x⟫,
simp_rw [h, mul_zero, add_zero],
norm_cast, } },
{ simp_rw [map_add, map_sub, inner_add_left, inner_add_right, inner_sub_left, inner_sub_right,
linear_map.map_smul, inner_smul_left, inner_smul_right, is_R_or_C.conj_I, mul_add,
mul_sub, sub_sub, ← mul_assoc, mul_neg, h, neg_neg, one_mul, neg_one_mul],
ring, },
end
/-- A symmetric linear map `T` is zero if and only if `⟪T x, x⟫_ℝ = 0` for all `x`.
See `inner_map_self_eq_zero` for the complex version without the symmetric assumption. -/
lemma is_symmetric.inner_map_self_eq_zero {T : E →ₗ[𝕜] E} (hT : T.is_symmetric) :
(∀ x, ⟪T x, x⟫ = 0) ↔ T = 0 :=
begin
simp_rw [linear_map.ext_iff, zero_apply],
refine ⟨λ h x, _, λ h, by simp_rw [h, inner_zero_left, forall_const]⟩,
rw [← @inner_self_eq_zero 𝕜, hT.inner_map_polarization],
simp_rw [h _],
ring,
end
end linear_map
|
f9ee3719742e1a54a5e91232d60f8e1872b9bd3b | b210f3162055e6c91af0e5938b8e29ad2b66957d | /notes01.28.2020.lean | adbb8d1f1c21de51732e64d01c39abaa3d9607f1 | [] | no_license | colemanjenkins/SharedFiles | db8a5cea23e51b7781ce779af546045c4230c17d | 6ac3cc21a2912f3c8e62a23a882384b0b2a5d41b | refs/heads/master | 1,608,650,217,053 | 1,598,992,893,000 | 1,598,992,893,000 | 236,838,099 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,902 | lean | def x := 1+2
#eval x
def z' : ℕ := (nat.zero : ℕ)
def z'' := (nat.zero : ℕ)
def z''' : ℕ := nat.zero
def z := 0
-- Function expression are terms that have types
#check band
#check nat.add
#check string.append
-- Function application expressions are terms that have types
#check band tt tt
#check nat.add 2 3
#check string.append "I love " "logic!"
-- Function application expessions reduce to return values
#eval band tt tt
#eval nat.add 2 3
#eval string.append "I love " "logic!"
-- Lean strictly and statically enforces typing
--Can't do these!
#check band ff "Hi!"
#check nat.add 4 tt
#eval string.append "I love " 5 -- strange error, same problem
/- what type are its areguments
what type are its return value -/
tt tt ff
tt ff ff
--data type dm_bool
--dm_tt
--dm_ff
inductive /-define new data type-/ dm_bool /-name-/: /-of type-/ Type /-type-/
--rules for building values of type
| /-rule-/ dm_tt /-name-/: /-of type-/ dm_bool
| dm_ff : dm_bool
--every type comes with own namespace; namespace closed by default
--name of namespace is name of type
#check dm_bool.dm_tt
--OR
--not a bad idea to have them (namespace) closed in general
open dm_bool /-open namespace dm_bool-/
#check dm_bool.dm_tt
def b1 := dm_tt
def b2 := dm_ff
inductive day : Type
| sunday : day
| monday : day
| tuesday : day
| wednesday : day
| thursday : day
| friday : day
| saturday : day
--OR
inductive day2 : Type
| sun
| mon
| tues
| wed
| thurs
| fri
| sat
-- unary functions / operators
def n /-Ident-/: ℕ /-type-/:= 0 /-value-/
-- → is "\to" or "\imp"
def dm_not : dm_bool → dm_bool :=
λ /- a function -/ /- that takes-/(/-argument-/b /- of type-/: dm_bool), /-and that returns-/
match b with
| dm_tt := dm_ff
| dm_ff := dm_tt
end
/-
ARG RES
---------------
dm_tt | dm_ff
---------------
dm_ff | dm_tt
---------------
-/ |
c4252d6e617ed302c438c217f8b5f0fddd0fc106 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/run/conv1.lean | ac09191860724732ad9cac3a91ff882bdb5adff2 | [
"Apache-2.0",
"LLVM-exception",
"NCSA",
"LGPL-3.0-only",
"LicenseRef-scancode-inner-net-2.0",
"BSD-3-Clause",
"LGPL-2.0-or-later",
"Spencer-94",
"LGPL-2.1-or-later",
"HPND",
"LicenseRef-scancode-pcre",
"ISC",
"LGPL-2.1-only",
"LicenseRef-scancode-other-permissive",
"SunPro",
"CMU-Mach"... | permissive | leanprover/lean4 | 4bdf9790294964627eb9be79f5e8f6157780b4cc | f1f9dc0f2f531af3312398999d8b8303fa5f096b | refs/heads/master | 1,693,360,665,786 | 1,693,350,868,000 | 1,693,350,868,000 | 129,571,436 | 2,827 | 311 | Apache-2.0 | 1,694,716,156,000 | 1,523,760,560,000 | Lean | UTF-8 | Lean | false | false | 2,038 | lean | set_option pp.analyze false
def p (x y : Nat) := x = y
example (x y : Nat) : p (x + y) (y + x + 0) := by
conv =>
whnf
congr
. rfl
. whnf; rfl
trace_state
rw [Nat.add_comm]
rfl
example (x y : Nat) : p (x + y) (y + x + 0) := by
conv =>
whnf
rhs
whnf
trace_state
rw [Nat.add_comm]
rfl
example (x y : Nat) : p (x + y) (y + x + 0) := by
conv =>
whnf
lhs
whnf
conv =>
rhs
whnf
trace_state
apply Nat.add_comm x y
example (x y : Nat) : p (x + y) (0 + y + x) := by
conv =>
whnf
rhs
rw [Nat.zero_add, Nat.add_comm]
trace_state
rfl
done
axiom div_self (x : Nat) : x ≠ 0 → x / x = 1
example (h : x ≠ 0) : x / x + x = x.succ := by
conv =>
lhs
arg 1
rw [div_self]
rfl
tactic => assumption
done
show 1 + x = x.succ
rw [Nat.succ_add, Nat.zero_add]
example (h1 : x ≠ 0) (h2 : y = x / x) : y = 1 := by
conv at h2 =>
rhs
rw [div_self]
rfl
tactic => assumption
assumption
example : id (fun x => 0 + x) = id := by
conv =>
lhs
arg 1
ext y
rw [Nat.zero_add]
def f (x : Nat) :=
if x > 0 then
x + 1
else
x + 2
example (g : Nat → Nat) (h₁ : g x = x + 1) (h₂ : x > 0) : g x = f x := by
conv =>
rhs
simp [f, h₂]
exact h₁
example (h₁ : f x = x + 1) (h₂ : x > 0) : f x = f x := by
conv =>
rhs
simp [f, h₂]
exact h₁
example (x y : Nat) (f : Nat → Nat → Nat) (g : Nat → Nat) (h₁ : ∀ z, f z z = z) (h₂ : ∀ x y, f (g x) (g y) = y) : f (g (0 + y)) (f (g x) (g (x + 0))) = x := by
conv in _ + 0 => apply Nat.add_zero
trace_state
conv in 0 + _ => apply Nat.zero_add
trace_state
simp [h₁, h₂]
example (x y : Nat) (f : Nat → Nat → Nat) (g : Nat → Nat)
(h₁ : ∀ z, f z z = z) (h₂ : ∀ x y, f (g x) (g y) = y)
(h₃ : f (g (0 + x)) (g x) = 0)
: g x = 0 := by
conv at h₃ in 0 + x => apply Nat.zero_add
trace_state
conv at h₃ => lhs; apply h₁
trace_state
assumption
|
a9ebca39cb9ac4b8b3ff705296183ebd634fef9c | 7cef822f3b952965621309e88eadf618da0c8ae9 | /src/topology/stone_cech.lean | 557b16246c8b4635d5c8ea71d549abd9addbe224 | [
"Apache-2.0"
] | permissive | rmitta/mathlib | 8d90aee30b4db2b013e01f62c33f297d7e64a43d | 883d974b608845bad30ae19e27e33c285200bf84 | refs/heads/master | 1,585,776,832,544 | 1,576,874,096,000 | 1,576,874,096,000 | 153,663,165 | 0 | 2 | Apache-2.0 | 1,544,806,490,000 | 1,539,884,365,000 | Lean | UTF-8 | Lean | false | false | 11,252 | lean | /-
Copyright (c) 2018 Reid Barton. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Reid Barton
Construction of the Stone-Čech compactification using ultrafilters.
Parts of the formalization are based on "Ultrafilters and Topology"
by Marius Stekelenburg, particularly section 5.
-/
import topology.bases topology.dense_embedding
noncomputable theory
open filter lattice set
open_locale topological_space
universes u v
section ultrafilter
/- The set of ultrafilters on α carries a natural topology which makes
it the Stone-Čech compactification of α (viewed as a discrete space). -/
/-- Basis for the topology on `ultrafilter α`. -/
def ultrafilter_basis (α : Type u) : set (set (ultrafilter α)) :=
{t | ∃ (s : set α), t = {u | s ∈ u.val}}
variables {α : Type u}
instance : topological_space (ultrafilter α) :=
topological_space.generate_from (ultrafilter_basis α)
lemma ultrafilter_basis_is_basis :
topological_space.is_topological_basis (ultrafilter_basis α) :=
⟨begin
rintros _ ⟨a, rfl⟩ _ ⟨b, rfl⟩ u ⟨ua, ub⟩,
refine ⟨_, ⟨a ∩ b, rfl⟩, u.val.inter_sets ua ub, assume v hv, ⟨_, _⟩⟩;
apply v.val.sets_of_superset hv; simp
end,
eq_univ_of_univ_subset $ subset_sUnion_of_mem $
⟨univ, eq.symm (eq_univ_of_forall (λ u, u.val.univ_sets))⟩,
rfl⟩
/-- The basic open sets for the topology on ultrafilters are open. -/
lemma ultrafilter_is_open_basic (s : set α) :
is_open {u : ultrafilter α | s ∈ u.val} :=
topological_space.is_open_of_is_topological_basis ultrafilter_basis_is_basis ⟨s, rfl⟩
/-- The basic open sets for the topology on ultrafilters are also closed. -/
lemma ultrafilter_is_closed_basic (s : set α) :
is_closed {u : ultrafilter α | s ∈ u.val} :=
begin
change is_open (- _),
convert ultrafilter_is_open_basic (-s),
ext u,
exact (ultrafilter_iff_compl_mem_iff_not_mem.mp u.property s).symm
end
/-- Every ultrafilter `u` on `ultrafilter α` converges to a unique
point of `ultrafilter α`, namely `mjoin u`. -/
lemma ultrafilter_converges_iff {u : ultrafilter (ultrafilter α)} {x : ultrafilter α} :
u.val ≤ 𝓝 x ↔ x = mjoin u :=
begin
rw [eq_comm, ultrafilter.eq_iff_val_le_val],
change u.val ≤ 𝓝 x ↔ x.val.sets ⊆ {a | {v : ultrafilter α | a ∈ v.val} ∈ u.val},
simp only [topological_space.nhds_generate_from, lattice.le_infi_iff, ultrafilter_basis,
le_principal_iff],
split; intro h,
{ intros a ha, exact h _ ⟨ha, a, rfl⟩ },
{ rintros _ ⟨xi, a, rfl⟩, exact h xi }
end
instance ultrafilter_compact : compact_space (ultrafilter α) :=
⟨compact_iff_ultrafilter_le_nhds.mpr $ assume f uf _,
⟨mjoin ⟨f, uf⟩, trivial, ultrafilter_converges_iff.mpr rfl⟩⟩
instance ultrafilter.t2_space : t2_space (ultrafilter α) :=
t2_iff_ultrafilter.mpr $ assume f x y u fx fy,
have hx : x = mjoin ⟨f, u⟩, from ultrafilter_converges_iff.mp fx,
have hy : y = mjoin ⟨f, u⟩, from ultrafilter_converges_iff.mp fy,
hx.trans hy.symm
lemma ultrafilter_comap_pure_nhds (b : ultrafilter α) : comap pure (𝓝 b) ≤ b.val :=
begin
rw topological_space.nhds_generate_from,
simp only [comap_infi, comap_principal],
intros s hs,
rw ←le_principal_iff,
refine lattice.infi_le_of_le {u | s ∈ u.val} _,
refine lattice.infi_le_of_le ⟨hs, ⟨s, rfl⟩⟩ _,
rw principal_mono,
intros a ha,
exact mem_pure_iff.mp ha
end
section embedding
lemma ultrafilter_pure_injective : function.injective (pure : α → ultrafilter α) :=
begin
intros x y h,
have : {x} ∈ (pure x : ultrafilter α).val := singleton_mem_pure_sets,
rw h at this,
exact (mem_singleton_iff.mp (mem_pure_sets.mp this)).symm
end
open topological_space
/-- `pure : α → ultrafilter α` defines a dense inducing of `α` in `ultrafilter α`. -/
lemma dense_inducing_pure : @dense_inducing _ _ ⊥ _ (pure : α → ultrafilter α) :=
by letI : topological_space α := ⊥; exact
dense_inducing.mk' pure continuous_bot
(assume x, mem_closure_iff_ultrafilter.mpr
⟨x.map ultrafilter.pure, range_mem_map,
ultrafilter_converges_iff.mpr (bind_pure x).symm⟩)
(assume a s as,
⟨{u | s ∈ u.val},
mem_nhds_sets (ultrafilter_is_open_basic s) (mem_pure_sets.mpr (mem_of_nhds as)),
assume b hb, mem_pure_sets.mp hb⟩)
-- The following refined version will never be used
/-- `pure : α → ultrafilter α` defines a dense embedding of `α` in `ultrafilter α`. -/
lemma dense_embedding_pure : @dense_embedding _ _ ⊥ _ (pure : α → ultrafilter α) :=
by letI : topological_space α := ⊥ ;
exact { inj := ultrafilter_pure_injective, ..dense_inducing_pure }
end embedding
section extension
/- Goal: Any function `α → γ` to a compact Hausdorff space `γ` has a
unique extension to a continuous function `ultrafilter α → γ`. We
already know it must be unique because `α → ultrafilter α` is a
dense embedding and `γ` is Hausdorff. For existence, we will invoke
`dense_embedding.continuous_extend`. -/
variables {γ : Type*} [topological_space γ]
/-- The extension of a function `α → γ` to a function `ultrafilter α → γ`.
When `γ` is a compact Hausdorff space it will be continuous. -/
def ultrafilter.extend (f : α → γ) : ultrafilter α → γ :=
by letI : topological_space α := ⊥; exact dense_inducing_pure.extend f
variables [t2_space γ]
lemma ultrafilter_extend_extends (f : α → γ) : ultrafilter.extend f ∘ pure = f :=
begin
letI : topological_space α := ⊥,
letI : discrete_topology α := ⟨rfl⟩,
exact funext (dense_inducing_pure.extend_eq_of_cont continuous_of_discrete_topology)
end
variables [compact_space γ]
lemma continuous_ultrafilter_extend (f : α → γ) : continuous (ultrafilter.extend f) :=
have ∀ (b : ultrafilter α), ∃ c, tendsto f (comap ultrafilter.pure (𝓝 b)) (𝓝 c) := assume b,
-- b.map f is an ultrafilter on γ, which is compact, so it converges to some c in γ.
let ⟨c, _, h⟩ := compact_iff_ultrafilter_le_nhds.mp compact_univ (b.map f).val (b.map f).property
(by rw [le_principal_iff]; exact univ_mem_sets) in
⟨c, le_trans (map_mono (ultrafilter_comap_pure_nhds _)) h⟩,
begin
letI : topological_space α := ⊥,
letI : normal_space γ := normal_of_compact_t2,
exact dense_inducing_pure.continuous_extend this
end
/-- The value of `ultrafilter.extend f` on an ultrafilter `b` is the
unique limit of the ultrafilter `b.map f` in `γ`. -/
lemma ultrafilter_extend_eq_iff {f : α → γ} {b : ultrafilter α} {c : γ} :
ultrafilter.extend f b = c ↔ b.val.map f ≤ 𝓝 c :=
⟨assume h, begin
-- Write b as an ultrafilter limit of pure ultrafilters, and use
-- the facts that ultrafilter.extend is a continuous extension of f.
let b' : ultrafilter (ultrafilter α) := b.map pure,
have t : b'.val ≤ 𝓝 b,
from ultrafilter_converges_iff.mpr (by exact (bind_pure _).symm),
rw ←h,
have := (continuous_ultrafilter_extend f).tendsto b,
refine le_trans _ (le_trans (map_mono t) this),
change _ ≤ map (ultrafilter.extend f ∘ pure) b.val,
rw ultrafilter_extend_extends,
exact le_refl _
end,
assume h, by letI : topological_space α := ⊥; exact
dense_inducing_pure.extend_eq (le_trans (map_mono (ultrafilter_comap_pure_nhds _)) h)⟩
end extension
end ultrafilter
section stone_cech
/- Now, we start with a (not necessarily discrete) topological space α
and we want to construct its Stone-Čech compactification. We can
build it as a quotient of `ultrafilter α` by the relation which
identifies two points if the extension of every continuous function
α → γ to a compact Hausdorff space sends the two points to the same
point of γ. -/
variables (α : Type u) [topological_space α]
instance stone_cech_setoid : setoid (ultrafilter α) :=
{ r := λ x y, ∀ (γ : Type u) [topological_space γ], by exactI
∀ [t2_space γ] [compact_space γ] (f : α → γ) (hf : continuous f),
ultrafilter.extend f x = ultrafilter.extend f y,
iseqv :=
⟨assume x γ tγ h₁ h₂ f hf, rfl,
assume x y xy γ tγ h₁ h₂ f hf, by exactI (xy γ f hf).symm,
assume x y z xy yz γ tγ h₁ h₂ f hf, by exactI (xy γ f hf).trans (yz γ f hf)⟩ }
/-- The Stone-Čech compactification of a topological space. -/
def stone_cech : Type u := quotient (stone_cech_setoid α)
variables {α}
instance : topological_space (stone_cech α) := by unfold stone_cech; apply_instance
/-- The natural map from α to its Stone-Čech compactification. -/
def stone_cech_unit (x : α) : stone_cech α := ⟦pure x⟧
/-- The image of stone_cech_unit is dense. (But stone_cech_unit need
not be an embedding, for example if α is not Hausdorff.) -/
lemma stone_cech_unit_dense : closure (range (@stone_cech_unit α _)) = univ :=
begin
convert quotient_dense_of_dense (eq_univ_iff_forall.mp dense_inducing_pure.closure_range),
rw [←range_comp], refl
end
section extension
variables {γ : Type u} [topological_space γ] [t2_space γ] [compact_space γ]
variables {f : α → γ} (hf : continuous f)
local attribute [elab_with_expected_type] quotient.lift
/-- The extension of a continuous function from α to a compact
Hausdorff space γ to the Stone-Čech compactification of α. -/
def stone_cech_extend : stone_cech α → γ :=
quotient.lift (ultrafilter.extend f) (λ x y xy, xy γ f hf)
lemma stone_cech_extend_extends : stone_cech_extend hf ∘ stone_cech_unit = f :=
ultrafilter_extend_extends f
lemma continuous_stone_cech_extend : continuous (stone_cech_extend hf) :=
continuous_quot_lift _ (continuous_ultrafilter_extend f)
end extension
lemma convergent_eqv_pure {u : ultrafilter α} {x : α} (ux : u.val ≤ 𝓝 x) : u ≈ pure x :=
assume γ tγ h₁ h₂ f hf, begin
resetI,
transitivity f x, swap, symmetry,
all_goals { refine ultrafilter_extend_eq_iff.mpr (le_trans (map_mono _) (hf.tendsto _)) },
{ apply pure_le_nhds }, { exact ux }
end
lemma continuous_stone_cech_unit : continuous (stone_cech_unit : α → stone_cech α) :=
continuous_iff_ultrafilter.mpr $ λ x g u gx,
let g' : ultrafilter α := ⟨g, u⟩ in
have (g'.map ultrafilter.pure).val ≤ 𝓝 g',
by rw ultrafilter_converges_iff; exact (bind_pure _).symm,
have (g'.map stone_cech_unit).val ≤ 𝓝 ⟦g'⟧, from
(continuous_at_iff_ultrafilter g').mp
(continuous_quotient_mk.tendsto g') _ (ultrafilter_map u) this,
by rwa (show ⟦g'⟧ = ⟦pure x⟧, from quotient.sound $ convergent_eqv_pure gx) at this
instance stone_cech.t2_space : t2_space (stone_cech α) :=
begin
rw t2_iff_ultrafilter,
rintros g ⟨x⟩ ⟨y⟩ u gx gy,
apply quotient.sound,
intros γ tγ h₁ h₂ f hf,
resetI,
let ff := stone_cech_extend hf,
change ff ⟦x⟧ = ff ⟦y⟧,
have lim : ∀ z : ultrafilter α, g ≤ 𝓝 ⟦z⟧ → tendsto ff g (𝓝 (ff ⟦z⟧)) :=
assume z gz,
calc map ff g ≤ map ff (𝓝 ⟦z⟧) : map_mono gz
... ≤ 𝓝 (ff ⟦z⟧) : (continuous_stone_cech_extend hf).tendsto _,
exact tendsto_nhds_unique u.1 (lim x gx) (lim y gy)
end
instance stone_cech.compact_space : compact_space (stone_cech α) :=
quotient.compact_space
end stone_cech
|
e10f215dc1ae2c2b0231ef821522de116a0aff4d | ad0c7d243dc1bd563419e2767ed42fb323d7beea | /data/multiset.lean | e1ec02fc34ceeb54bd7bb9574b0b4d8b1b5bbc59 | [
"Apache-2.0"
] | permissive | sebzim4500/mathlib | e0b5a63b1655f910dee30badf09bd7e191d3cf30 | 6997cafbd3a7325af5cb318561768c316ceb7757 | refs/heads/master | 1,585,549,958,618 | 1,538,221,723,000 | 1,538,221,723,000 | 150,869,076 | 0 | 0 | Apache-2.0 | 1,538,229,323,000 | 1,538,229,323,000 | null | UTF-8 | Lean | false | false | 120,321 | lean | /-
Copyright (c) 2015 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Mario Carneiro
Multisets.
-/
import logic.function order.boolean_algebra
data.list.basic data.list.perm data.list.sort data.quot data.string
algebra.order_functions algebra.group_power algebra.ordered_group
category.traversable.lemmas tactic.interactive
category.traversable.instances category.basic
open list subtype nat lattice
variables {α : Type*} {β : Type*} {γ : Type*}
local infix ` • ` := add_monoid.smul
instance list.perm.setoid (α : Type*) : setoid (list α) :=
setoid.mk perm ⟨perm.refl, @perm.symm _, @perm.trans _⟩
/-- `multiset α` is the quotient of `list α` by list permutation. The result
is a type of finite sets with duplicates allowed. -/
def {u} multiset (α : Type u) : Type u :=
quotient (list.perm.setoid α)
namespace multiset
instance : has_coe (list α) (multiset α) := ⟨quot.mk _⟩
@[simp] theorem quot_mk_to_coe (l : list α) : @eq (multiset α) ⟦l⟧ l := rfl
@[simp] theorem quot_mk_to_coe' (l : list α) : @eq (multiset α) (quot.mk (≈) l) l := rfl
@[simp] theorem quot_mk_to_coe'' (l : list α) : @eq (multiset α) (quot.mk setoid.r l) l := rfl
@[simp] theorem coe_eq_coe {l₁ l₂ : list α} : (l₁ : multiset α) = l₂ ↔ l₁ ~ l₂ := quotient.eq
instance has_decidable_eq [decidable_eq α] : decidable_eq (multiset α)
| s₁ s₂ := quotient.rec_on_subsingleton₂ s₁ s₂ $ λ l₁ l₂,
decidable_of_iff' _ quotient.eq
/- empty multiset -/
/-- `0 : multiset α` is the empty set -/
protected def zero : multiset α := @nil α
instance : has_zero (multiset α) := ⟨multiset.zero⟩
instance : has_emptyc (multiset α) := ⟨0⟩
instance : inhabited (multiset α) := ⟨0⟩
@[simp] theorem coe_nil_eq_zero : (@nil α : multiset α) = 0 := rfl
@[simp] theorem empty_eq_zero : (∅ : multiset α) = 0 := rfl
/- cons -/
/-- `cons a s` is the multiset which contains `s` plus one more
instance of `a`. -/
def cons (a : α) (s : multiset α) : multiset α :=
quot.lift_on s (λ l, (a :: l : multiset α))
(λ l₁ l₂ p, quot.sound ((perm_cons a).2 p))
notation a :: b := cons a b
instance : has_insert α (multiset α) := ⟨cons⟩
@[simp] theorem insert_eq_cons (a : α) (s : multiset α) :
insert a s = a::s := rfl
@[simp] theorem cons_coe (a : α) (l : list α) :
(a::l : multiset α) = (a::l : list α) := rfl
theorem singleton_coe (a : α) : (a::0 : multiset α) = ([a] : list α) := rfl
@[simp] theorem cons_inj_left {a b : α} (s : multiset α) :
a::s = b::s ↔ a = b :=
⟨quot.induction_on s $ λ l e,
have [a] ++ l ~ [b] ++ l, from quotient.exact e,
eq_singleton_of_perm $ (perm_app_right_iff _).1 this, congr_arg _⟩
@[simp] theorem cons_inj_right (a : α) : ∀{s t : multiset α}, a::s = a::t ↔ s = t :=
by rintros ⟨l₁⟩ ⟨l₂⟩; simp [perm_cons]
@[recursor 5] protected theorem induction {p : multiset α → Prop}
(h₁ : p 0) (h₂ : ∀ ⦃a : α⦄ {s : multiset α}, p s → p (a :: s)) : ∀s, p s :=
by rintros ⟨l⟩; induction l with _ _ ih; [exact h₁, exact h₂ ih]
@[elab_as_eliminator] protected theorem induction_on {p : multiset α → Prop}
(s : multiset α) (h₁ : p 0) (h₂ : ∀ ⦃a : α⦄ {s : multiset α}, p s → p (a :: s)) : p s :=
multiset.induction h₁ h₂ s
theorem cons_swap (a b : α) (s : multiset α) : a :: b :: s = b :: a :: s :=
quot.induction_on s $ λ l, quotient.sound $ perm.swap _ _ _
section rec
variables {C : multiset α → Sort*}
/-- Dependent recursor on multisets.
TODO: should be @[recursor 6], but then the definition of `multiset.pi` failes with a stack
overflow in `whnf`.
-/
protected def rec
(C_0 : C 0)
(C_cons : Πa m, C m → C (a::m))
(C_cons_heq : ∀a a' m b, C_cons a (a'::m) (C_cons a' m b) == C_cons a' (a::m) (C_cons a m b))
(m : multiset α) : C m :=
quotient.hrec_on m (@list.rec α (λl, C ⟦l⟧) C_0 (λa l b, C_cons a ⟦l⟧ b)) $
assume l l' h,
list.rec_heq_of_perm h
(assume a l l' b b' hl, have ⟦l⟧ = ⟦l'⟧, from quot.sound hl, by cc)
(assume a a' l, C_cons_heq a a' ⟦l⟧)
@[elab_as_eliminator]
protected def rec_on (m : multiset α)
(C_0 : C 0)
(C_cons : Πa m, C m → C (a::m))
(C_cons_heq : ∀a a' m b, C_cons a (a'::m) (C_cons a' m b) == C_cons a' (a::m) (C_cons a m b)) :
C m :=
multiset.rec C_0 C_cons C_cons_heq m
variables {C_0 : C 0} {C_cons : Πa m, C m → C (a::m)}
{C_cons_heq : ∀a a' m b, C_cons a (a'::m) (C_cons a' m b) == C_cons a' (a::m) (C_cons a m b)}
@[simp] lemma rec_on_0 : @multiset.rec_on α C (0:multiset α) C_0 C_cons C_cons_heq = C_0 :=
rfl
@[simp] lemma rec_on_cons (a : α) (m : multiset α) :
(a :: m).rec_on C_0 C_cons C_cons_heq = C_cons a m (m.rec_on C_0 C_cons C_cons_heq) :=
quotient.induction_on m $ assume l, rfl
end rec
section mem
/-- `a ∈ s` means that `a` has nonzero multiplicity in `s`. -/
def mem (a : α) (s : multiset α) : Prop :=
quot.lift_on s (λ l, a ∈ l) (λ l₁ l₂ (e : l₁ ~ l₂), propext $ mem_of_perm e)
instance : has_mem α (multiset α) := ⟨mem⟩
@[simp] lemma mem_coe {a : α} {l : list α} : a ∈ (l : multiset α) ↔ a ∈ l := iff.rfl
instance decidable_mem [decidable_eq α] (a : α) (s : multiset α) : decidable (a ∈ s) :=
quot.rec_on_subsingleton s $ list.decidable_mem a
@[simp] theorem mem_cons {a b : α} {s : multiset α} : a ∈ b :: s ↔ a = b ∨ a ∈ s :=
quot.induction_on s $ λ l, iff.rfl
lemma mem_cons_of_mem {a b : α} {s : multiset α} (h : a ∈ s) : a ∈ b :: s :=
mem_cons.2 $ or.inr h
@[simp] theorem mem_cons_self (a : α) (s : multiset α) : a ∈ a :: s :=
mem_cons.2 (or.inl rfl)
theorem exists_cons_of_mem {s : multiset α} {a : α} : a ∈ s → ∃ t, s = a :: t :=
quot.induction_on s $ λ l (h : a ∈ l),
let ⟨l₁, l₂, e⟩ := mem_split h in
e.symm ▸ ⟨(l₁++l₂ : list α), quot.sound perm_middle⟩
@[simp] theorem not_mem_zero (a : α) : a ∉ (0 : multiset α) := id
theorem eq_zero_of_forall_not_mem {s : multiset α} : (∀x, x ∉ s) → s = 0 :=
quot.induction_on s $ λ l H, by rw eq_nil_of_forall_not_mem H; refl
theorem exists_mem_of_ne_zero {s : multiset α} : s ≠ 0 → ∃ a : α, a ∈ s :=
quot.induction_on s $ assume l hl,
match l, hl with
| [] := assume h, false.elim $ h rfl
| (a :: l) := assume _, ⟨a, by simp⟩
end
@[simp] lemma zero_ne_cons {a : α} {m : multiset α} : 0 ≠ a :: m :=
assume h, have a ∈ (0:multiset α), from h.symm ▸ mem_cons_self _ _, not_mem_zero _ this
@[simp] lemma cons_ne_zero {a : α} {m : multiset α} : a :: m ≠ 0 := zero_ne_cons.symm
lemma cons_eq_cons {a b : α} {as bs : multiset α} :
a :: as = b :: bs ↔ ((a = b ∧ as = bs) ∨ (a ≠ b ∧ ∃cs, as = b :: cs ∧ bs = a :: cs)) :=
begin
haveI : decidable_eq α := classical.dec_eq α,
split,
{ assume eq,
by_cases a = b,
{ subst h, simp * at * },
{ have : a ∈ b :: bs, from eq ▸ mem_cons_self _ _,
have : a ∈ bs, by simpa [h],
rcases exists_cons_of_mem this with ⟨cs, hcs⟩,
simp [h, hcs],
have : a :: as = b :: a :: cs, by simp [eq, hcs],
have : a :: as = a :: b :: cs, by rwa [cons_swap],
simpa using this } },
{ assume h,
rcases h with ⟨eq₁, eq₂⟩ | ⟨h, cs, eq₁, eq₂⟩,
{ simp * },
{ simp [*, cons_swap a b] } }
end
end mem
/- subset -/
section subset
/-- `s ⊆ t` is the lift of the list subset relation. It means that any
element with nonzero multiplicity in `s` has nonzero multiplicity in `t`,
but it does not imply that the multiplicity of `a` in `s` is less or equal than in `t`;
see `s ≤ t` for this relation. -/
protected def subset (s t : multiset α) : Prop := ∀ ⦃a : α⦄, a ∈ s → a ∈ t
instance : has_subset (multiset α) := ⟨multiset.subset⟩
@[simp] theorem coe_subset {l₁ l₂ : list α} : (l₁ : multiset α) ⊆ l₂ ↔ l₁ ⊆ l₂ := iff.rfl
@[simp] theorem subset.refl (s : multiset α) : s ⊆ s := λ a h, h
theorem subset.trans {s t u : multiset α} : s ⊆ t → t ⊆ u → s ⊆ u :=
λ h₁ h₂ a m, h₂ (h₁ m)
theorem subset_iff {s t : multiset α} : s ⊆ t ↔ (∀⦃x⦄, x ∈ s → x ∈ t) := iff.rfl
theorem mem_of_subset {s t : multiset α} {a : α} (h : s ⊆ t) : a ∈ s → a ∈ t := @h _
@[simp] theorem zero_subset (s : multiset α) : 0 ⊆ s :=
λ a, (not_mem_nil a).elim
@[simp] theorem cons_subset {a : α} {s t : multiset α} : (a :: s) ⊆ t ↔ a ∈ t ∧ s ⊆ t :=
by simp [subset_iff, or_imp_distrib, forall_and_distrib]
theorem eq_zero_of_subset_zero {s : multiset α} (h : s ⊆ 0) : s = 0 :=
eq_zero_of_forall_not_mem h
theorem subset_zero {s : multiset α} : s ⊆ 0 ↔ s = 0 :=
⟨eq_zero_of_subset_zero, λ xeq, xeq.symm ▸ subset.refl 0⟩
end subset
/- multiset order -/
/-- `s ≤ t` means that `s` is a sublist of `t` (up to permutation).
Equivalently, `s ≤ t` means that `count a s ≤ count a t` for all `a`. -/
protected def le (s t : multiset α) : Prop :=
quotient.lift_on₂ s t (<+~) $ λ v₁ v₂ w₁ w₂ p₁ p₂,
propext (p₂.subperm_left.trans p₁.subperm_right)
instance : partial_order (multiset α) :=
{ le := multiset.le,
le_refl := by rintros ⟨l⟩; exact subperm.refl _,
le_trans := by rintros ⟨l₁⟩ ⟨l₂⟩ ⟨l₃⟩; exact @subperm.trans _ _ _ _,
le_antisymm := by rintros ⟨l₁⟩ ⟨l₂⟩ h₁ h₂; exact quot.sound (subperm.antisymm h₁ h₂) }
theorem subset_of_le {s t : multiset α} : s ≤ t → s ⊆ t :=
quotient.induction_on₂ s t $ λ l₁ l₂, subset_of_subperm
theorem mem_of_le {s t : multiset α} {a : α} (h : s ≤ t) : a ∈ s → a ∈ t :=
mem_of_subset (subset_of_le h)
@[simp] theorem coe_le {l₁ l₂ : list α} : (l₁ : multiset α) ≤ l₂ ↔ l₁ <+~ l₂ := iff.rfl
@[elab_as_eliminator] theorem le_induction_on {C : multiset α → multiset α → Prop}
{s t : multiset α} (h : s ≤ t)
(H : ∀ {l₁ l₂ : list α}, l₁ <+ l₂ → C l₁ l₂) : C s t :=
quotient.induction_on₂ s t (λ l₁ l₂ ⟨l, p, s⟩,
(show ⟦l⟧ = ⟦l₁⟧, from quot.sound p) ▸ H s) h
theorem zero_le (s : multiset α) : 0 ≤ s :=
quot.induction_on s $ λ l, subperm_of_sublist $ nil_sublist l
theorem le_zero {s : multiset α} : s ≤ 0 ↔ s = 0 :=
⟨λ h, le_antisymm h (zero_le _), le_of_eq⟩
theorem lt_cons_self (s : multiset α) (a : α) : s < a :: s :=
quot.induction_on s $ λ l,
suffices l <+~ a :: l ∧ (¬l ~ a :: l),
by simpa [lt_iff_le_and_ne],
⟨subperm_of_sublist (sublist_cons _ _),
λ p, ne_of_lt (lt_succ_self (length l)) (perm_length p)⟩
theorem le_cons_self (s : multiset α) (a : α) : s ≤ a :: s :=
le_of_lt $ lt_cons_self _ _
theorem cons_le_cons_iff (a : α) {s t : multiset α} : a :: s ≤ a :: t ↔ s ≤ t :=
quotient.induction_on₂ s t $ λ l₁ l₂, subperm_cons a
theorem cons_le_cons (a : α) {s t : multiset α} : s ≤ t → a :: s ≤ a :: t :=
(cons_le_cons_iff a).2
theorem le_cons_of_not_mem {a : α} {s t : multiset α} (m : a ∉ s) : s ≤ a :: t ↔ s ≤ t :=
begin
refine ⟨_, λ h, le_trans h $ le_cons_self _ _⟩,
suffices : ∀ {t'} (_ : s ≤ t') (_ : a ∈ t'), a :: s ≤ t',
{ exact λ h, (cons_le_cons_iff a).1 (this h (mem_cons_self _ _)) },
introv h, revert m, refine le_induction_on h _,
introv s m₁ m₂,
rcases mem_split m₂ with ⟨r₁, r₂, rfl⟩,
exact perm_middle.subperm_left.2 ((subperm_cons _).2 $ subperm_of_sublist $
(sublist_or_mem_of_sublist s).resolve_right m₁)
end
/- cardinality -/
/-- The cardinality of a multiset is the sum of the multiplicities
of all its elements, or simply the length of the underlying list. -/
def card (s : multiset α) : ℕ :=
quot.lift_on s length $ λ l₁ l₂, perm_length
@[simp] theorem coe_card (l : list α) : card (l : multiset α) = length l := rfl
@[simp] theorem card_zero : @card α 0 = 0 := rfl
@[simp] theorem card_cons (a : α) (s : multiset α) : card (a :: s) = card s + 1 :=
quot.induction_on s $ λ l, rfl
@[simp] theorem card_singleton (a : α) : card (a::0) = 1 := by simp
theorem card_le_of_le {s t : multiset α} (h : s ≤ t) : card s ≤ card t :=
le_induction_on h $ λ l₁ l₂, length_le_of_sublist
theorem eq_of_le_of_card_le {s t : multiset α} (h : s ≤ t) : card t ≤ card s → s = t :=
le_induction_on h $ λ l₁ l₂ s h₂, congr_arg coe $ eq_of_sublist_of_length_le s h₂
theorem card_lt_of_lt {s t : multiset α} (h : s < t) : card s < card t :=
lt_of_not_ge $ λ h₂, ne_of_lt h $ eq_of_le_of_card_le (le_of_lt h) h₂
theorem lt_iff_cons_le {s t : multiset α} : s < t ↔ ∃ a, a :: s ≤ t :=
⟨quotient.induction_on₂ s t $ λ l₁ l₂ h,
subperm.exists_of_length_lt (le_of_lt h) (card_lt_of_lt h),
λ ⟨a, h⟩, lt_of_lt_of_le (lt_cons_self _ _) h⟩
@[simp] theorem card_eq_zero {s : multiset α} : card s = 0 ↔ s = 0 :=
⟨λ h, (eq_of_le_of_card_le (zero_le _) (le_of_eq h)).symm, λ e, by simp [e]⟩
theorem card_pos {s : multiset α} : 0 < card s ↔ s ≠ 0 :=
pos_iff_ne_zero.trans $ not_congr card_eq_zero
theorem card_pos_iff_exists_mem {s : multiset α} : 0 < card s ↔ ∃ a, a ∈ s :=
quot.induction_on s $ λ l, length_pos_iff_exists_mem
@[elab_as_eliminator] def strong_induction_on {p : multiset α → Sort*} :
∀ (s : multiset α), (∀ s, (∀t < s, p t) → p s) → p s
| s := λ ih, ih s $ λ t h,
have card t < card s, from card_lt_of_lt h,
strong_induction_on t ih
using_well_founded {rel_tac := λ _ _, `[exact ⟨_, measure_wf card⟩]}
theorem strong_induction_eq {p : multiset α → Sort*}
(s : multiset α) (H) : @strong_induction_on _ p s H =
H s (λ t h, @strong_induction_on _ p t H) :=
by rw [strong_induction_on]
@[elab_as_eliminator] lemma case_strong_induction_on {p : multiset α → Prop}
(s : multiset α) (h₀ : p 0) (h₁ : ∀ a s, (∀t ≤ s, p t) → p (a :: s)) : p s :=
multiset.strong_induction_on s $ assume s,
multiset.induction_on s (λ _, h₀) $ λ a s _ ih, h₁ _ _ $
λ t h, ih _ $ lt_of_le_of_lt h $ lt_cons_self _ _
/- singleton -/
@[simp] theorem singleton_eq_singleton (a : α) : singleton a = a::0 := rfl
@[simp] theorem mem_singleton {a b : α} : b ∈ a::0 ↔ b = a := by simp
theorem mem_singleton_self (a : α) : a ∈ (a::0 : multiset α) := mem_cons_self _ _
theorem singleton_inj {a b : α} : a::0 = b::0 ↔ a = b := cons_inj_left _
@[simp] theorem singleton_ne_zero (a : α) : a::0 ≠ 0 :=
ne_of_gt (lt_cons_self _ _)
@[simp] theorem singleton_le {a : α} {s : multiset α} : a::0 ≤ s ↔ a ∈ s :=
⟨λ h, mem_of_le h (mem_singleton_self _),
λ h, let ⟨t, e⟩ := exists_cons_of_mem h in e.symm ▸ cons_le_cons _ (zero_le _)⟩
theorem card_eq_one {s : multiset α} : card s = 1 ↔ ∃ a, s = a::0 :=
⟨quot.induction_on s $ λ l h,
(list.length_eq_one.1 h).imp $ λ a, congr_arg coe,
λ ⟨a, e⟩, e.symm ▸ rfl⟩
/- add -/
/-- The sum of two multisets is the lift of the list append operation.
This adds the multiplicities of each element,
i.e. `count a (s + t) = count a s + count a t`. -/
protected def add (s₁ s₂ : multiset α) : multiset α :=
quotient.lift_on₂ s₁ s₂ (λ l₁ l₂, ((l₁ ++ l₂ : list α) : multiset α)) $
λ v₁ v₂ w₁ w₂ p₁ p₂, quot.sound $ perm_app p₁ p₂
instance : has_add (multiset α) := ⟨multiset.add⟩
@[simp] theorem coe_add (s t : list α) : (s + t : multiset α) = (s ++ t : list α) := rfl
protected theorem add_comm (s t : multiset α) : s + t = t + s :=
quotient.induction_on₂ s t $ λ l₁ l₂, quot.sound perm_app_comm
protected theorem zero_add (s : multiset α) : 0 + s = s :=
quot.induction_on s $ λ l, rfl
theorem singleton_add (a : α) (s : multiset α) : ↑[a] + s = a::s := rfl
protected theorem add_le_add_left (s) {t u : multiset α} : s + t ≤ s + u ↔ t ≤ u :=
quotient.induction_on₃ s t u $ λ l₁ l₂ l₃, subperm_app_left _
protected theorem add_left_cancel (s) {t u : multiset α} (h : s + t = s + u) : t = u :=
le_antisymm ((multiset.add_le_add_left _).1 (le_of_eq h))
((multiset.add_le_add_left _).1 (le_of_eq h.symm))
instance : ordered_cancel_comm_monoid (multiset α) :=
{ zero := 0,
add := (+),
add_comm := multiset.add_comm,
add_assoc := λ s₁ s₂ s₃, quotient.induction_on₃ s₁ s₂ s₃ $ λ l₁ l₂ l₃,
congr_arg coe $ append_assoc l₁ l₂ l₃,
zero_add := multiset.zero_add,
add_zero := λ s, by rw [multiset.add_comm, multiset.zero_add],
add_left_cancel := multiset.add_left_cancel,
add_right_cancel := λ s₁ s₂ s₃ h, multiset.add_left_cancel s₂ $
by simpa [multiset.add_comm] using h,
add_le_add_left := λ s₁ s₂ h s₃, (multiset.add_le_add_left _).2 h,
le_of_add_le_add_left := λ s₁ s₂ s₃, (multiset.add_le_add_left _).1,
..@multiset.partial_order α }
@[simp] theorem cons_add (a : α) (s t : multiset α) : a :: s + t = a :: (s + t) :=
by rw [← singleton_add, ← singleton_add, add_assoc]
@[simp] theorem add_cons (a : α) (s t : multiset α) : s + a :: t = a :: (s + t) :=
by rw [add_comm, cons_add, add_comm]
theorem le_add_right (s t : multiset α) : s ≤ s + t :=
by simpa using add_le_add_left (zero_le t) s
theorem le_add_left (s t : multiset α) : s ≤ t + s :=
by simpa using add_le_add_right (zero_le t) s
@[simp] theorem card_add (s t : multiset α) : card (s + t) = card s + card t :=
quotient.induction_on₂ s t length_append
@[simp] theorem mem_add {a : α} {s t : multiset α} : a ∈ s + t ↔ a ∈ s ∨ a ∈ t :=
quotient.induction_on₂ s t $ λ l₁ l₂, mem_append
theorem le_iff_exists_add {s t : multiset α} : s ≤ t ↔ ∃ u, t = s + u :=
⟨λ h, le_induction_on h $ λ l₁ l₂ s,
let ⟨l, p⟩ := exists_perm_append_of_sublist s in ⟨l, quot.sound p⟩,
λ⟨u, e⟩, e.symm ▸ le_add_right s u⟩
instance : canonically_ordered_monoid (multiset α) :=
{ lt_of_add_lt_add_left := @lt_of_add_lt_add_left _ _,
le_iff_exists_add := @le_iff_exists_add _,
..multiset.ordered_cancel_comm_monoid }
/- repeat -/
/-- `repeat a n` is the multiset containing only `a` with multiplicity `n`. -/
def repeat (a : α) (n : ℕ) : multiset α := repeat a n
@[simp] lemma repeat_zero (a : α) : repeat a 0 = 0 := rfl
@[simp] lemma repeat_succ (a : α) (n) : repeat a (n+1) = a :: repeat a n := by simp [repeat]
@[simp] lemma repeat_one (a : α) : repeat a 1 = a :: 0 := by simp
@[simp] lemma card_repeat : ∀ (a : α) n, card (repeat a n) = n := length_repeat
theorem eq_of_mem_repeat {a b : α} {n} : b ∈ repeat a n → b = a := eq_of_mem_repeat
theorem eq_repeat' {a : α} {s : multiset α} : s = repeat a s.card ↔ ∀ b ∈ s, b = a :=
quot.induction_on s $ λ l, iff.trans ⟨λ h,
(perm_repeat.1 $ (quotient.exact h).symm).symm, congr_arg coe⟩ eq_repeat'
theorem eq_repeat_of_mem {a : α} {s : multiset α} : (∀ b ∈ s, b = a) → s = repeat a s.card :=
eq_repeat'.2
theorem eq_repeat {a : α} {n} {s : multiset α} : s = repeat a n ↔ card s = n ∧ ∀ b ∈ s, b = a :=
⟨λ h, h.symm ▸ ⟨card_repeat _ _, λ b, eq_of_mem_repeat⟩,
λ ⟨e, al⟩, e ▸ eq_repeat_of_mem al⟩
theorem repeat_subset_singleton : ∀ (a : α) n, repeat a n ⊆ a::0 := repeat_subset_singleton
theorem repeat_le_coe {a : α} {n} {l : list α} : repeat a n ≤ l ↔ list.repeat a n <+ l :=
⟨λ ⟨l', p, s⟩, (perm_repeat.1 p.symm).symm ▸ s, subperm_of_sublist⟩
/- range -/
/-- `range n` is the multiset lifted from the list `range n`,
that is, the set `{0, 1, ..., n-1}`. -/
def range (n : ℕ) : multiset ℕ := range n
@[simp] theorem range_zero (n : ℕ) : range 0 = 0 := rfl
@[simp] theorem range_succ (n : ℕ) : range (succ n) = n :: range n :=
by rw [range, range_concat, ← coe_add, add_comm]; refl
@[simp] theorem card_range (n : ℕ) : card (range n) = n := length_range _
theorem range_subset {m n : ℕ} : range m ⊆ range n ↔ m ≤ n := range_subset
@[simp] theorem mem_range {m n : ℕ} : m ∈ range n ↔ m < n := mem_range
@[simp] theorem not_mem_range_self {n : ℕ} : n ∉ range n := not_mem_range_self
/- erase -/
section erase
variables [decidable_eq α] {s t : multiset α} {a b : α}
/-- `erase s a` is the multiset that subtracts 1 from the
multiplicity of `a`. -/
def erase (s : multiset α) (a : α) : multiset α :=
quot.lift_on s (λ l, (l.erase a : multiset α))
(λ l₁ l₂ p, quot.sound (erase_perm_erase a p))
@[simp] theorem coe_erase (l : list α) (a : α) :
erase (l : multiset α) a = l.erase a := rfl
@[simp] theorem erase_zero (a : α) : (0 : multiset α).erase a = 0 := rfl
@[simp] theorem erase_cons_head (a : α) (s : multiset α) : (a :: s).erase a = s :=
quot.induction_on s $ λ l, congr_arg coe $ erase_cons_head a l
@[simp] theorem erase_cons_tail {a b : α} (s : multiset α) (h : b ≠ a) : (b::s).erase a = b :: s.erase a :=
quot.induction_on s $ λ l, congr_arg coe $ erase_cons_tail l h
@[simp] theorem erase_of_not_mem {a : α} {s : multiset α} : a ∉ s → s.erase a = s :=
quot.induction_on s $ λ l h, congr_arg coe $ erase_of_not_mem h
@[simp] theorem cons_erase {s : multiset α} {a : α} : a ∈ s → a :: s.erase a = s :=
quot.induction_on s $ λ l h, quot.sound (perm_erase h).symm
theorem le_cons_erase (s : multiset α) (a : α) : s ≤ a :: s.erase a :=
if h : a ∈ s then le_of_eq (cons_erase h).symm
else by rw erase_of_not_mem h; apply le_cons_self
@[simp] theorem card_erase_of_mem {a : α} {s : multiset α} : a ∈ s → card (s.erase a) = pred (card s) :=
quot.induction_on s $ λ l, length_erase_of_mem
theorem erase_add_left_pos {a : α} {s : multiset α} (t) : a ∈ s → (s + t).erase a = s.erase a + t :=
quotient.induction_on₂ s t $ λ l₁ l₂ h, congr_arg coe $ erase_append_left l₂ h
theorem erase_add_right_pos {a : α} (s) {t : multiset α} (h : a ∈ t) : (s + t).erase a = s + t.erase a :=
by rw [add_comm, erase_add_left_pos s h, add_comm]
theorem erase_add_right_neg {a : α} {s : multiset α} (t) : a ∉ s → (s + t).erase a = s + t.erase a :=
quotient.induction_on₂ s t $ λ l₁ l₂ h, congr_arg coe $ erase_append_right l₂ h
theorem erase_add_left_neg {a : α} (s) {t : multiset α} (h : a ∉ t) : (s + t).erase a = s.erase a + t :=
by rw [add_comm, erase_add_right_neg s h, add_comm]
theorem erase_le (a : α) (s : multiset α) : s.erase a ≤ s :=
quot.induction_on s $ λ l, subperm_of_sublist (erase_sublist a l)
@[simp] theorem erase_lt {a : α} {s : multiset α} : s.erase a < s ↔ a ∈ s :=
⟨λ h, not_imp_comm.1 erase_of_not_mem (ne_of_lt h),
λ h, by simpa [h] using lt_cons_self (s.erase a) a⟩
theorem erase_subset (a : α) (s : multiset α) : s.erase a ⊆ s :=
subset_of_le (erase_le a s)
theorem mem_erase_of_ne {a b : α} {s : multiset α} (ab : a ≠ b) : a ∈ s.erase b ↔ a ∈ s :=
quot.induction_on s $ λ l, list.mem_erase_of_ne ab
theorem mem_of_mem_erase {a b : α} {s : multiset α} : a ∈ s.erase b → a ∈ s :=
mem_of_subset (erase_subset _ _)
theorem erase_comm (s : multiset α) (a b : α) : (s.erase a).erase b = (s.erase b).erase a :=
quot.induction_on s $ λ l, congr_arg coe $ l.erase_comm a b
theorem erase_le_erase {s t : multiset α} (a : α) (h : s ≤ t) : s.erase a ≤ t.erase a :=
le_induction_on h $ λ l₁ l₂ h, subperm_of_sublist (erase_sublist_erase _ h)
theorem erase_le_iff_le_cons {s t : multiset α} {a : α} : s.erase a ≤ t ↔ s ≤ a :: t :=
⟨λ h, le_trans (le_cons_erase _ _) (cons_le_cons _ h),
λ h, if m : a ∈ s
then by rw ← cons_erase m at h; exact (cons_le_cons_iff _).1 h
else le_trans (erase_le _ _) ((le_cons_of_not_mem m).1 h)⟩
end erase
@[simp] theorem coe_reverse (l : list α) : (reverse l : multiset α) = l :=
quot.sound $ reverse_perm _
/- map -/
/-- `map f s` is the lift of the list `map` operation. The multiplicity
of `b` in `map f s` is the number of `a ∈ s` (counting multiplicity)
such that `f a = b`. -/
def map (f : α → β) (s : multiset α) : multiset β :=
quot.lift_on s (λ l : list α, (l.map f : multiset β))
(λ l₁ l₂ p, quot.sound (perm_map f p))
@[simp] theorem coe_map (f : α → β) (l : list α) : map f ↑l = l.map f := rfl
@[simp] theorem map_zero (f : α → β) : map f 0 = 0 := rfl
@[simp] theorem map_cons (f : α → β) (a s) : map f (a::s) = f a :: map f s :=
quot.induction_on s $ λ l, rfl
@[simp] lemma map_singleton (f : α → β) (a : α) : ({a} : multiset α).map f = {f a} := rfl
@[simp] theorem map_add (f : α → β) (s t) : map f (s + t) = map f s + map f t :=
quotient.induction_on₂ s t $ λ l₁ l₂, congr_arg coe $ map_append _ _ _
@[simp] theorem mem_map {f : α → β} {b : β} {s : multiset α} :
b ∈ map f s ↔ ∃ a, a ∈ s ∧ f a = b :=
quot.induction_on s $ λ l, mem_map
@[simp] theorem card_map (f : α → β) (s) : card (map f s) = card s :=
quot.induction_on s $ λ l, length_map _ _
theorem mem_map_of_mem (f : α → β) {a : α} {s : multiset α} (h : a ∈ s) : f a ∈ map f s :=
mem_map.2 ⟨_, h, rfl⟩
@[simp] theorem mem_map_of_inj {f : α → β} (H : function.injective f) {a : α} {s : multiset α} :
f a ∈ map f s ↔ a ∈ s :=
quot.induction_on s $ λ l, mem_map_of_inj H
@[simp] theorem map_map (g : β → γ) (f : α → β) (s : multiset α) : map g (map f s) = map (g ∘ f) s :=
quot.induction_on s $ λ l, congr_arg coe $ list.map_map _ _ _
@[simp] theorem map_id (s : multiset α) : map id s = s :=
quot.induction_on s $ λ l, congr_arg coe $ map_id _
@[simp] lemma map_id' (s : multiset α) : map (λx, x) s = s := map_id s
@[simp] theorem map_const (s : multiset α) (b : β) : map (function.const α b) s = repeat b s.card :=
quot.induction_on s $ λ l, congr_arg coe $ map_const _ _
@[congr] theorem map_congr {f g : α → β} {s : multiset α} : (∀ x ∈ s, f x = g x) → map f s = map g s :=
quot.induction_on s $ λ l H, congr_arg coe $ map_congr H
lemma map_hcongr {β' : Type*} {m : multiset α} {f : α → β} {f' : α → β'}
(h : β = β') (hf : ∀a∈m, f a == f' a) : map f m == map f' m :=
begin subst h, simp at hf, simp [map_congr hf] end
theorem eq_of_mem_map_const {b₁ b₂ : β} {l : list α} (h : b₁ ∈ map (function.const α b₂) l) : b₁ = b₂ :=
eq_of_mem_repeat $ by rwa map_const at h
@[simp] theorem map_le_map {f : α → β} {s t : multiset α} (h : s ≤ t) : map f s ≤ map f t :=
le_induction_on h $ λ l₁ l₂ h, subperm_of_sublist $ map_sublist_map f h
@[simp] theorem map_subset_map {f : α → β} {s t : multiset α} (H : s ⊆ t) : map f s ⊆ map f t :=
λ b m, let ⟨a, h, e⟩ := mem_map.1 m in mem_map.2 ⟨a, H h, e⟩
/- fold -/
/-- `foldl f H b s` is the lift of the list operation `foldl f b l`,
which folds `f` over the multiset. It is well defined when `f` is right-commutative,
that is, `f (f b a₁) a₂ = f (f b a₂) a₁`. -/
def foldl (f : β → α → β) (H : right_commutative f) (b : β) (s : multiset α) : β :=
quot.lift_on s (λ l, foldl f b l)
(λ l₁ l₂ p, foldl_eq_of_perm H p b)
@[simp] theorem foldl_zero (f : β → α → β) (H b) : foldl f H b 0 = b := rfl
@[simp] theorem foldl_cons (f : β → α → β) (H b a s) : foldl f H b (a :: s) = foldl f H (f b a) s :=
quot.induction_on s $ λ l, rfl
@[simp] theorem foldl_add (f : β → α → β) (H b s t) : foldl f H b (s + t) = foldl f H (foldl f H b s) t :=
quotient.induction_on₂ s t $ λ l₁ l₂, foldl_append _ _ _ _
/-- `foldr f H b s` is the lift of the list operation `foldr f b l`,
which folds `f` over the multiset. It is well defined when `f` is left-commutative,
that is, `f a₁ (f a₂ b) = f a₂ (f a₁ b)`. -/
def foldr (f : α → β → β) (H : left_commutative f) (b : β) (s : multiset α) : β :=
quot.lift_on s (λ l, foldr f b l)
(λ l₁ l₂ p, foldr_eq_of_perm H p b)
@[simp] theorem foldr_zero (f : α → β → β) (H b) : foldr f H b 0 = b := rfl
@[simp] theorem foldr_cons (f : α → β → β) (H b a s) : foldr f H b (a :: s) = f a (foldr f H b s) :=
quot.induction_on s $ λ l, rfl
@[simp] theorem foldr_add (f : α → β → β) (H b s t) : foldr f H b (s + t) = foldr f H (foldr f H b t) s :=
quotient.induction_on₂ s t $ λ l₁ l₂, foldr_append _ _ _ _
@[simp] theorem coe_foldr (f : α → β → β) (H : left_commutative f) (b : β) (l : list α) :
foldr f H b l = l.foldr f b := rfl
@[simp] theorem coe_foldl (f : β → α → β) (H : right_commutative f) (b : β) (l : list α) :
foldl f H b l = l.foldl f b := rfl
theorem coe_foldr_swap (f : α → β → β) (H : left_commutative f) (b : β) (l : list α) :
foldr f H b l = l.foldl (λ x y, f y x) b :=
(congr_arg (foldr f H b) (coe_reverse l)).symm.trans $ foldr_reverse _ _ _
theorem foldr_swap (f : α → β → β) (H : left_commutative f) (b : β) (s : multiset α) :
foldr f H b s = foldl (λ x y, f y x) (λ x y z, (H _ _ _).symm) b s :=
quot.induction_on s $ λ l, coe_foldr_swap _ _ _ _
theorem foldl_swap (f : β → α → β) (H : right_commutative f) (b : β) (s : multiset α) :
foldl f H b s = foldr (λ x y, f y x) (λ x y z, (H _ _ _).symm) b s :=
(foldr_swap _ _ _ _).symm
/-- Product of a multiset given a commutative monoid structure on `α`.
`prod {a, b, c} = a * b * c` -/
def prod [comm_monoid α] : multiset α → α :=
foldr (*) (λ x y z, by simp [mul_left_comm]) 1
attribute [to_additive multiset.sum._proof_1] prod._proof_1
attribute [to_additive multiset.sum] prod
@[to_additive multiset.sum_eq_foldr]
theorem prod_eq_foldr [comm_monoid α] (s : multiset α) :
prod s = foldr (*) (λ x y z, by simp [mul_left_comm]) 1 s := rfl
@[to_additive multiset.sum_eq_foldl]
theorem prod_eq_foldl [comm_monoid α] (s : multiset α) :
prod s = foldl (*) (λ x y z, by simp [mul_right_comm]) 1 s :=
(foldr_swap _ _ _ _).trans (by simp [mul_comm])
@[simp, to_additive multiset.coe_sum]
theorem coe_prod [comm_monoid α] (l : list α) : prod ↑l = l.prod :=
prod_eq_foldl _
@[simp, to_additive multiset.sum_zero]
theorem prod_zero [comm_monoid α] : @prod α _ 0 = 1 := rfl
@[simp, to_additive multiset.sum_cons]
theorem prod_cons [comm_monoid α] (a : α) (s) : prod (a :: s) = a * prod s :=
foldr_cons _ _ _ _ _
@[to_additive multiset.sum_singleton]
theorem prod_singleton [comm_monoid α] (a : α) : prod (a :: 0) = a := by simp
@[simp, to_additive multiset.sum_add]
theorem prod_add [comm_monoid α] (s t : multiset α) : prod (s + t) = prod s * prod t :=
quotient.induction_on₂ s t $ λ l₁ l₂, by simp
@[simp] theorem prod_repeat [comm_monoid α] (a : α) (n : ℕ) : prod (multiset.repeat a n) = a ^ n :=
by simp [repeat, list.prod_repeat]
@[simp] theorem sum_repeat [add_comm_monoid α] : ∀ (a : α) (n : ℕ), sum (multiset.repeat a n) = n • a :=
@prod_repeat (multiplicative α) _
attribute [to_additive multiset.sum_repeat] prod_repeat
@[simp] lemma prod_map_one [comm_monoid γ] {m : multiset α} :
prod (m.map (λa, (1 : γ))) = (1 : γ) :=
multiset.induction_on m (by simp) (by simp)
@[simp] lemma sum_map_zero [add_comm_monoid γ] {m : multiset α} :
sum (m.map (λa, (0 : γ))) = (0 : γ) :=
multiset.induction_on m (by simp) (by simp)
attribute [to_additive multiset.sum_map_zero] prod_map_one
@[simp, to_additive multiset.sum_map_add]
lemma prod_map_mul [comm_monoid γ] {m : multiset α} {f g : α → γ} :
prod (m.map $ λa, f a * g a) = prod (m.map f) * prod (m.map g) :=
multiset.induction_on m (by simp) (assume a m ih, by simp [ih]; cc)
lemma prod_map_prod_map [comm_monoid γ] (m : multiset α) (n : multiset β) {f : α → β → γ} :
prod (m.map $ λa, prod $ n.map $ λb, f a b) = prod (n.map $ λb, prod $ m.map $ λa, f a b) :=
multiset.induction_on m (by simp) (assume a m ih, by simp [ih])
lemma sum_map_sum_map [add_comm_monoid γ] : ∀ (m : multiset α) (n : multiset β) {f : α → β → γ},
sum (m.map $ λa, sum $ n.map $ λb, f a b) = sum (n.map $ λb, sum $ m.map $ λa, f a b) :=
@prod_map_prod_map _ _ (multiplicative γ) _
attribute [to_additive multiset.sum_map_sum_map] prod_map_prod_map
lemma sum_map_mul_left [semiring β] {b : β} {s : multiset α} {f : α → β} :
sum (s.map (λa, b * f a)) = b * sum (s.map f) :=
multiset.induction_on s (by simp) (assume a s ih, by simp [ih, mul_add])
lemma sum_map_mul_right [semiring β] {b : β} {s : multiset α} {f : α → β} :
sum (s.map (λa, f a * b)) = sum (s.map f) * b :=
multiset.induction_on s (by simp) (assume a s ih, by simp [ih, add_mul])
/- join -/
/-- `join S`, where `S` is a multiset of multisets, is the lift of the list join
operation, that is, the union of all the sets.
join {{1, 2}, {1, 2}, {0, 1}} = {0, 1, 1, 1, 2, 2} -/
def join : multiset (multiset α) → multiset α := sum
theorem coe_join : ∀ L : list (list α),
join (L.map (@coe _ (multiset α) _) : multiset (multiset α)) = L.join
| [] := rfl
| (l :: L) := congr_arg (λ s : multiset α, ↑l + s) (coe_join L)
@[simp] theorem join_zero : @join α 0 = 0 := rfl
@[simp] theorem join_cons (s S) : @join α (s :: S) = s + join S :=
sum_cons _ _
@[simp] theorem join_add (S T) : @join α (S + T) = join S + join T :=
sum_add _ _
@[simp] theorem mem_join {a S} : a ∈ @join α S ↔ ∃ s ∈ S, a ∈ s :=
multiset.induction_on S (by simp) $
by simp [or_and_distrib_right, exists_or_distrib] {contextual := tt}
@[simp] theorem card_join (S) : card (@join α S) = sum (map card S) :=
multiset.induction_on S (by simp) (by simp)
/- bind -/
/-- `bind s f` is the monad bind operation, defined as `join (map f s)`.
It is the union of `f a` as `a` ranges over `s`. -/
def bind (s : multiset α) (f : α → multiset β) : multiset β :=
join (map f s)
@[simp] theorem coe_bind (l : list α) (f : α → list β) :
@bind α β l (λ a, f a) = l.bind f :=
by rw [list.bind, ← coe_join, list.map_map]; refl
@[simp] theorem zero_bind (f : α → multiset β) : bind 0 f = 0 := rfl
@[simp] theorem cons_bind (a s) (f : α → multiset β) : bind (a::s) f = f a + bind s f :=
by simp [bind]
@[simp] theorem add_bind (s t) (f : α → multiset β) : bind (s + t) f = bind s f + bind t f :=
by simp [bind]
@[simp] theorem bind_zero (s : multiset α) : bind s (λa, 0 : α → multiset β) = 0 :=
by simp [bind, -map_const, join]
@[simp] theorem bind_add (s : multiset α) (f g : α → multiset β) :
bind s (λa, f a + g a) = bind s f + bind s g :=
by simp [bind, join]
@[simp] theorem bind_cons (s : multiset α) (f : α → β) (g : α → multiset β) :
bind s (λa, f a :: g a) = map f s + bind s g :=
multiset.induction_on s (by simp) (by simp {contextual := tt})
@[simp] theorem mem_bind {b s} {f : α → multiset β} : b ∈ bind s f ↔ ∃ a ∈ s, b ∈ f a :=
by simp [bind]; simp [-exists_and_distrib_right, exists_and_distrib_right.symm];
rw exists_swap; simp [and_assoc]
@[simp] theorem card_bind (s) (f : α → multiset β) : card (bind s f) = sum (map (card ∘ f) s) :=
by simp [bind]
lemma bind_congr {f g : α → multiset β} {m : multiset α} : (∀a∈m, f a = g a) → bind m f = bind m g :=
by simp [bind] {contextual := tt}
lemma bind_hcongr {β' : Type*} {m : multiset α} {f : α → multiset β} {f' : α → multiset β'}
(h : β = β') (hf : ∀a∈m, f a == f' a) : bind m f == bind m f' :=
begin subst h, simp at hf, simp [bind_congr hf] end
lemma map_bind (m : multiset α) (n : α → multiset β) (f : β → γ) :
map f (bind m n) = bind m (λa, map f (n a)) :=
multiset.induction_on m (by simp) (by simp {contextual := tt})
lemma bind_map (m : multiset α) (n : β → multiset γ) (f : α → β) :
bind (map f m) n = bind m (λa, n (f a)) :=
multiset.induction_on m (by simp) (by simp {contextual := tt})
lemma bind_assoc {s : multiset α} {f : α → multiset β} {g : β → multiset γ} :
(s.bind f).bind g = s.bind (λa, (f a).bind g) :=
multiset.induction_on s (by simp) (by simp {contextual := tt})
lemma bind_bind (m : multiset α) (n : multiset β) {f : α → β → multiset γ} :
(bind m $ λa, bind n $ λb, f a b) = (bind n $ λb, bind m $ λa, f a b) :=
multiset.induction_on m (by simp) (by simp {contextual := tt})
lemma bind_map_comm (m : multiset α) (n : multiset β) {f : α → β → γ} :
(bind m $ λa, n.map $ λb, f a b) = (bind n $ λb, m.map $ λa, f a b) :=
multiset.induction_on m (by simp) (by simp {contextual := tt})
@[simp, to_additive multiset.sum_bind]
lemma prod_bind [comm_monoid β] (s : multiset α) (t : α → multiset β) :
prod (bind s t) = prod (s.map $ λa, prod (t a)) :=
multiset.induction_on s (by simp) (assume a s ih, by simp [ih, cons_bind])
/- product -/
/-- The multiplicity of `(a, b)` in `product s t` is
the product of the multiplicity of `a` in `s` and `b` in `t`. -/
def product (s : multiset α) (t : multiset β) : multiset (α × β) :=
s.bind $ λ a, t.map $ prod.mk a
@[simp] theorem coe_product (l₁ : list α) (l₂ : list β) :
@product α β l₁ l₂ = l₁.product l₂ :=
by rw [product, list.product, ← coe_bind]; simp
@[simp] theorem zero_product (t) : @product α β 0 t = 0 := rfl
@[simp] theorem cons_product (a : α) (s : multiset α) (t : multiset β) :
product (a :: s) t = map (prod.mk a) t + product s t :=
by simp [product]
@[simp] theorem product_singleton (a : α) (b : β) : product (a::0) (b::0) = (a,b)::0 := rfl
@[simp] theorem add_product (s t : multiset α) (u : multiset β) :
product (s + t) u = product s u + product t u :=
by simp [product]
@[simp] theorem product_add (s : multiset α) : ∀ t u : multiset β,
product s (t + u) = product s t + product s u :=
multiset.induction_on s (λ t u, rfl) $ λ a s IH t u,
by rw [cons_product, IH]; simp
@[simp] theorem mem_product {s t} : ∀ {p : α × β}, p ∈ @product α β s t ↔ p.1 ∈ s ∧ p.2 ∈ t
| (a, b) := by simp [product, and.left_comm]
@[simp] theorem card_product (s : multiset α) (t : multiset β) : card (product s t) = card s * card t :=
by simp [product, repeat, (∘), mul_comm]
/- sigma -/
section
variable {σ : α → Type*}
/-- `sigma s t` is the dependent version of `product`. It is the sum of
`(a, b)` as `a` ranges over `s` and `b` ranges over `t a`. -/
protected def sigma (s : multiset α) (t : Π a, multiset (σ a)) : multiset (Σ a, σ a) :=
s.bind $ λ a, (t a).map $ sigma.mk a
@[simp] theorem coe_sigma (l₁ : list α) (l₂ : Π a, list (σ a)) :
@multiset.sigma α σ l₁ (λ a, l₂ a) = l₁.sigma l₂ :=
by rw [multiset.sigma, list.sigma, ← coe_bind]; simp
@[simp] theorem zero_sigma (t) : @multiset.sigma α σ 0 t = 0 := rfl
@[simp] theorem cons_sigma (a : α) (s : multiset α) (t : Π a, multiset (σ a)) :
(a :: s).sigma t = map (sigma.mk a) (t a) + s.sigma t :=
by simp [multiset.sigma]
@[simp] theorem sigma_singleton (a : α) (b : α → β) :
(a::0).sigma (λ a, b a::0) = ⟨a, b a⟩::0 := rfl
@[simp] theorem add_sigma (s t : multiset α) (u : Π a, multiset (σ a)) :
(s + t).sigma u = s.sigma u + t.sigma u :=
by simp [multiset.sigma]
@[simp] theorem sigma_add (s : multiset α) : ∀ t u : Π a, multiset (σ a),
s.sigma (λ a, t a + u a) = s.sigma t + s.sigma u :=
multiset.induction_on s (λ t u, rfl) $ λ a s IH t u,
by rw [cons_sigma, IH]; simp
@[simp] theorem mem_sigma {s t} : ∀ {p : Σ a, σ a},
p ∈ @multiset.sigma α σ s t ↔ p.1 ∈ s ∧ p.2 ∈ t p.1
| ⟨a, b⟩ := by simp [multiset.sigma, and_assoc, and.left_comm]
@[simp] theorem card_sigma (s : multiset α) (t : Π a, multiset (σ a)) :
card (s.sigma t) = sum (map (λ a, card (t a)) s) :=
by simp [multiset.sigma, (∘)]
end
/- map for partial functions -/
/-- Lift of the list `pmap` operation. Map a partial function `f` over a multiset
`s` whose elements are all in the domain of `f`. -/
def pmap {p : α → Prop} (f : Π a, p a → β) (s : multiset α) : (∀ a ∈ s, p a) → multiset β :=
quot.rec_on s (λ l H, ↑(pmap f l H)) $ λ l₁ l₂ (pp : l₁ ~ l₂),
funext $ λ (H₂ : ∀ a ∈ l₂, p a),
have H₁ : ∀ a ∈ l₁, p a, from λ a h, H₂ a ((mem_of_perm pp).1 h),
have ∀ {s₂ e H}, @eq.rec (multiset α) l₁
(λ s, (∀ a ∈ s, p a) → multiset β) (λ _, ↑(pmap f l₁ H₁))
s₂ e H = ↑(pmap f l₁ H₁), by intros s₂ e _; subst e,
this.trans $ quot.sound $ perm_pmap f pp
@[simp] theorem coe_pmap {p : α → Prop} (f : Π a, p a → β)
(l : list α) (H : ∀ a ∈ l, p a) : pmap f l H = l.pmap f H := rfl
@[simp] lemma pmap_zero {p : α → Prop} (f : Π a, p a → β) (h : ∀a∈(0:multiset α), p a) :
pmap f 0 h = 0 := rfl
@[simp] lemma pmap_cons {p : α → Prop} (f : Π a, p a → β) (a : α) (m : multiset α) :
∀(h : ∀b∈a::m, p b), pmap f (a :: m) h =
f a (h a (mem_cons_self a m)) :: pmap f m (λa ha, h a $ mem_cons_of_mem ha) :=
quotient.induction_on m $ assume l h, rfl
/-- "Attach" a proof that `a ∈ s` to each element `a` in `s` to produce
a multiset on `{x // x ∈ s}`. -/
def attach (s : multiset α) : multiset {x // x ∈ s} := pmap subtype.mk s (λ a, id)
@[simp] theorem coe_attach (l : list α) :
@eq (multiset {x // x ∈ l}) (@attach α l) l.attach := rfl
theorem pmap_eq_map (p : α → Prop) (f : α → β) (s : multiset α) :
∀ H, @pmap _ _ p (λ a _, f a) s H = map f s :=
quot.induction_on s $ λ l H, congr_arg coe $ pmap_eq_map p f l H
theorem pmap_congr {p q : α → Prop} {f : Π a, p a → β} {g : Π a, q a → β}
(s : multiset α) {H₁ H₂} (h : ∀ a h₁ h₂, f a h₁ = g a h₂) :
pmap f s H₁ = pmap g s H₂ :=
quot.induction_on s (λ l H₁ H₂, congr_arg coe $ pmap_congr l h) H₁ H₂
theorem map_pmap {p : α → Prop} (g : β → γ) (f : Π a, p a → β)
(s) : ∀ H, map g (pmap f s H) = pmap (λ a h, g (f a h)) s H :=
quot.induction_on s $ λ l H, congr_arg coe $ map_pmap g f l H
theorem pmap_eq_map_attach {p : α → Prop} (f : Π a, p a → β)
(s) : ∀ H, pmap f s H = s.attach.map (λ x, f x.1 (H _ x.2)) :=
quot.induction_on s $ λ l H, congr_arg coe $ pmap_eq_map_attach f l H
theorem attach_map_val (s : multiset α) : s.attach.map subtype.val = s :=
quot.induction_on s $ λ l, congr_arg coe $ attach_map_val l
@[simp] theorem mem_attach (s : multiset α) : ∀ x, x ∈ s.attach :=
quot.induction_on s $ λ l, mem_attach _
@[simp] theorem mem_pmap {p : α → Prop} {f : Π a, p a → β}
{s H b} : b ∈ pmap f s H ↔ ∃ a (h : a ∈ s), f a (H a h) = b :=
quot.induction_on s (λ l H, mem_pmap) H
@[simp] theorem card_pmap {p : α → Prop} (f : Π a, p a → β)
(s H) : card (pmap f s H) = card s :=
quot.induction_on s (λ l H, length_pmap) H
@[simp] theorem card_attach {m : multiset α} : card (attach m) = card m := card_pmap _ _ _
@[simp] lemma attach_zero : (0 : multiset α).attach = 0 := rfl
lemma attach_cons (a : α) (m : multiset α) :
(a :: m).attach = ⟨a, mem_cons_self a m⟩ :: (m.attach.map $ λp, ⟨p.1, mem_cons_of_mem p.2⟩) :=
quotient.induction_on m $ assume l, congr_arg coe $ congr_arg (list.cons _) $
by rw [list.map_pmap]; exact list.pmap_congr _ (assume a' h₁ h₂, subtype.eq rfl)
section decidable_pi_exists
variables {m : multiset α}
protected def decidable_forall_multiset {p : α → Prop} [hp : ∀a, decidable (p a)] :
decidable (∀a∈m, p a) :=
quotient.rec_on_subsingleton m (λl, decidable_of_iff (∀a∈l, p a) $ by simp)
instance decidable_dforall_multiset {p : Πa∈m, Prop} [hp : ∀a (h : a ∈ m), decidable (p a h)] :
decidable (∀a (h : a ∈ m), p a h) :=
decidable_of_decidable_of_iff
(@multiset.decidable_forall_multiset {a // a ∈ m} m.attach (λa, p a.1 a.2) _)
(iff.intro (assume h a ha, h ⟨a, ha⟩ (mem_attach _ _)) (assume h ⟨a, ha⟩ _, h _ _))
/-- decidable equality for functions whose domain is bounded by multisets -/
instance decidable_eq_pi_multiset {β : α → Type*} [h : ∀a, decidable_eq (β a)] :
decidable_eq (Πa∈m, β a) :=
assume f g, decidable_of_iff (∀a (h : a ∈ m), f a h = g a h) (by simp [function.funext_iff])
def decidable_exists_multiset {p : α → Prop} [decidable_pred p] :
decidable (∃ x ∈ m, p x) :=
quotient.rec_on_subsingleton m list.decidable_exists_mem
instance decidable_dexists_multiset {p : Πa∈m, Prop} [hp : ∀a (h : a ∈ m), decidable (p a h)] :
decidable (∃a (h : a ∈ m), p a h) :=
decidable_of_decidable_of_iff
(@multiset.decidable_exists_multiset {a // a ∈ m} m.attach (λa, p a.1 a.2) _)
(iff.intro (λ ⟨⟨a, ha₁⟩, _, ha₂⟩, ⟨a, ha₁, ha₂⟩)
(λ ⟨a, ha₁, ha₂⟩, ⟨⟨a, ha₁⟩, mem_attach _ _, ha₂⟩))
end decidable_pi_exists
/- subtraction -/
section
variables [decidable_eq α] {s t u : multiset α} {a b : α}
/-- `s - t` is the multiset such that
`count a (s - t) = count a s - count a t` for all `a`. -/
protected def sub (s t : multiset α) : multiset α :=
quotient.lift_on₂ s t (λ l₁ l₂, (l₁.diff l₂ : multiset α)) $ λ v₁ v₂ w₁ w₂ p₁ p₂,
quot.sound $ perm_diff_right w₁ p₂ ▸ perm_diff_left _ p₁
instance : has_sub (multiset α) := ⟨multiset.sub⟩
@[simp] theorem coe_sub (s t : list α) : (s - t : multiset α) = (s.diff t : list α) := rfl
theorem sub_eq_fold_erase (s t : multiset α) : s - t = foldl erase erase_comm s t :=
quotient.induction_on₂ s t $ λ l₁ l₂,
show ↑(l₁.diff l₂) = foldl erase erase_comm ↑l₁ ↑l₂,
by rw diff_eq_foldl l₁ l₂; exact foldl_hom _ _ _ _ (λ x y, rfl) _
@[simp] theorem sub_zero (s : multiset α) : s - 0 = s :=
quot.induction_on s $ λ l, rfl
@[simp] theorem sub_cons (a : α) (s t : multiset α) : s - a::t = s.erase a - t :=
quotient.induction_on₂ s t $ λ l₁ l₂, congr_arg coe $ diff_cons _ _ _
theorem add_sub_of_le (h : s ≤ t) : s + (t - s) = t :=
begin
revert t,
refine multiset.induction_on s (by simp) (λ a s IH t h, _),
have := cons_erase (mem_of_le h (mem_cons_self _ _)),
rw [cons_add, sub_cons, IH, this],
exact (cons_le_cons_iff a).1 (this.symm ▸ h)
end
theorem sub_add' : s - (t + u) = s - t - u :=
quotient.induction_on₃ s t u $
λ l₁ l₂ l₃, congr_arg coe $ diff_append _ _ _
theorem sub_add_cancel (h : t ≤ s) : s - t + t = s :=
by rw [add_comm, add_sub_of_le h]
theorem add_sub_cancel_left (s : multiset α) : ∀ t, s + t - s = t :=
multiset.induction_on s (by simp)
(λ a s IH t, by rw [cons_add, sub_cons, erase_cons_head, IH])
theorem add_sub_cancel (s t : multiset α) : s + t - t = s :=
by rw [add_comm, add_sub_cancel_left]
theorem sub_le_sub_right (h : s ≤ t) (u) : s - u ≤ t - u :=
by revert s t h; exact
multiset.induction_on u (by simp {contextual := tt})
(λ a u IH s t h, by simp [IH, erase_le_erase a h])
theorem sub_le_sub_left (h : s ≤ t) : ∀ u, u - t ≤ u - s :=
le_induction_on h $ λ l₁ l₂ h, begin
induction h with l₁ l₂ a s IH l₁ l₂ a s IH; intro u,
{ refl },
{ rw [← cons_coe, sub_cons],
exact le_trans (sub_le_sub_right (erase_le _ _) _) (IH u) },
{ rw [← cons_coe, sub_cons, ← cons_coe, sub_cons],
exact IH _ }
end
theorem sub_le_iff_le_add : s - t ≤ u ↔ s ≤ u + t :=
by revert s; exact
multiset.induction_on t (by simp)
(λ a t IH s, by simp [IH, erase_le_iff_le_cons])
theorem le_sub_add (s t : multiset α) : s ≤ s - t + t :=
sub_le_iff_le_add.1 (le_refl _)
theorem sub_le_self (s t : multiset α) : s - t ≤ s :=
sub_le_iff_le_add.2 (le_add_right _ _)
@[simp] theorem card_sub {s t : multiset α} (h : t ≤ s) : card (s - t) = card s - card t :=
(nat.sub_eq_of_eq_add $ by rw [add_comm, ← card_add, sub_add_cancel h]).symm
/- union -/
/-- `s ∪ t` is the lattice join operation with respect to the
multiset `≤`. The multiplicity of `a` in `s ∪ t` is the maximum
of the multiplicities in `s` and `t`. -/
def union (s t : multiset α) : multiset α := s - t + t
instance : has_union (multiset α) := ⟨union⟩
theorem union_def (s t : multiset α) : s ∪ t = s - t + t := rfl
theorem le_union_left (s t : multiset α) : s ≤ s ∪ t := le_sub_add _ _
theorem le_union_right (s t : multiset α) : t ≤ s ∪ t := le_add_left _ _
theorem eq_union_left : t ≤ s → s ∪ t = s := sub_add_cancel
theorem union_le_union_right (h : s ≤ t) (u) : s ∪ u ≤ t ∪ u :=
add_le_add_right (sub_le_sub_right h _) u
theorem union_le (h₁ : s ≤ u) (h₂ : t ≤ u) : s ∪ t ≤ u :=
by rw ← eq_union_left h₂; exact union_le_union_right h₁ t
@[simp] theorem mem_union : a ∈ s ∪ t ↔ a ∈ s ∨ a ∈ t :=
⟨λ h, (mem_add.1 h).imp_left (mem_of_le $ sub_le_self _ _),
or.rec (mem_of_le $ le_union_left _ _) (mem_of_le $ le_union_right _ _)⟩
@[simp] theorem map_union [decidable_eq β] {f : α → β} (finj : function.injective f) {s t : multiset α} :
map f (s ∪ t) = map f s ∪ map f t :=
quotient.induction_on₂ s t $ λ l₁ l₂,
congr_arg coe (by rw [list.map_append f, list.map_diff finj])
/- inter -/
/-- `s ∩ t` is the lattice meet operation with respect to the
multiset `≤`. The multiplicity of `a` in `s ∩ t` is the minimum
of the multiplicities in `s` and `t`. -/
def inter (s t : multiset α) : multiset α :=
quotient.lift_on₂ s t (λ l₁ l₂, (l₁.bag_inter l₂ : multiset α)) $ λ v₁ v₂ w₁ w₂ p₁ p₂,
quot.sound $ perm_bag_inter_right w₁ p₂ ▸ perm_bag_inter_left _ p₁
instance : has_inter (multiset α) := ⟨inter⟩
@[simp] theorem inter_zero (s : multiset α) : s ∩ 0 = 0 :=
quot.induction_on s $ λ l, congr_arg coe l.bag_inter_nil
@[simp] theorem zero_inter (s : multiset α) : 0 ∩ s = 0 :=
quot.induction_on s $ λ l, congr_arg coe l.nil_bag_inter
@[simp] theorem cons_inter_of_pos {a} (s : multiset α) {t} :
a ∈ t → (a :: s) ∩ t = a :: s ∩ t.erase a :=
quotient.induction_on₂ s t $ λ l₁ l₂ h,
congr_arg coe $ cons_bag_inter_of_pos _ h
@[simp] theorem cons_inter_of_neg {a} (s : multiset α) {t} :
a ∉ t → (a :: s) ∩ t = s ∩ t :=
quotient.induction_on₂ s t $ λ l₁ l₂ h,
congr_arg coe $ cons_bag_inter_of_neg _ h
theorem inter_le_left (s t : multiset α) : s ∩ t ≤ s :=
quotient.induction_on₂ s t $ λ l₁ l₂,
subperm_of_sublist $ bag_inter_sublist_left _ _
theorem inter_le_right (s : multiset α) : ∀ t, s ∩ t ≤ t :=
multiset.induction_on s (λ t, (zero_inter t).symm ▸ zero_le _) $
λ a s IH t, if h : a ∈ t
then by simpa [h] using cons_le_cons a (IH (t.erase a))
else by simp [h, IH]
theorem le_inter (h₁ : s ≤ t) (h₂ : s ≤ u) : s ≤ t ∩ u :=
begin
revert s u, refine multiset.induction_on t _ (λ a t IH, _); intros,
{ simp [h₁] },
by_cases a ∈ u,
{ rw [cons_inter_of_pos _ h, ← erase_le_iff_le_cons],
exact IH (erase_le_iff_le_cons.2 h₁) (erase_le_erase _ h₂) },
{ rw cons_inter_of_neg _ h,
exact IH ((le_cons_of_not_mem $ mt (mem_of_le h₂) h).1 h₁) h₂ }
end
@[simp] theorem mem_inter : a ∈ s ∩ t ↔ a ∈ s ∧ a ∈ t :=
⟨λ h, ⟨mem_of_le (inter_le_left _ _) h, mem_of_le (inter_le_right _ _) h⟩,
λ ⟨h₁, h₂⟩, by rw [← cons_erase h₁, cons_inter_of_pos _ h₂]; apply mem_cons_self⟩
instance : lattice (multiset α) :=
{ sup := (∪),
sup_le := @union_le _ _,
le_sup_left := le_union_left,
le_sup_right := le_union_right,
inf := (∩),
le_inf := @le_inter _ _,
inf_le_left := inter_le_left,
inf_le_right := inter_le_right,
..@multiset.partial_order α }
@[simp] theorem sup_eq_union (s t : multiset α) : s ⊔ t = s ∪ t := rfl
@[simp] theorem inf_eq_inter (s t : multiset α) : s ⊓ t = s ∩ t := rfl
@[simp] theorem le_inter_iff : s ≤ t ∩ u ↔ s ≤ t ∧ s ≤ u := le_inf_iff
@[simp] theorem union_le_iff : s ∪ t ≤ u ↔ s ≤ u ∧ t ≤ u := sup_le_iff
instance : semilattice_inf_bot (multiset α) :=
{ bot := 0, bot_le := zero_le, ..multiset.lattice.lattice }
theorem union_comm (s t : multiset α) : s ∪ t = t ∪ s := sup_comm
theorem inter_comm (s t : multiset α) : s ∩ t = t ∩ s := inf_comm
theorem eq_union_right (h : s ≤ t) : s ∪ t = t :=
by rw [union_comm, eq_union_left h]
theorem union_le_union_left (h : s ≤ t) (u) : u ∪ s ≤ u ∪ t :=
sup_le_sup_left h _
theorem union_le_add (s t : multiset α) : s ∪ t ≤ s + t :=
union_le (le_add_right _ _) (le_add_left _ _)
theorem union_add_distrib (s t u : multiset α) : (s ∪ t) + u = (s + u) ∪ (t + u) :=
by simpa [(∪), union, eq_comm] using show s + u - (t + u) = s - t,
by rw [add_comm t, sub_add', add_sub_cancel]
theorem add_union_distrib (s t u : multiset α) : s + (t ∪ u) = (s + t) ∪ (s + u) :=
by rw [add_comm, union_add_distrib, add_comm s, add_comm s]
theorem cons_union_distrib (a : α) (s t : multiset α) : a :: (s ∪ t) = (a :: s) ∪ (a :: t) :=
by simpa using add_union_distrib (a::0) s t
theorem inter_add_distrib (s t u : multiset α) : (s ∩ t) + u = (s + u) ∩ (t + u) :=
begin
by_contra h,
cases lt_iff_cons_le.1 (lt_of_le_of_ne (le_inter
(add_le_add_right (inter_le_left s t) u)
(add_le_add_right (inter_le_right s t) u)) h) with a hl,
rw ← cons_add at hl,
exact not_le_of_lt (lt_cons_self (s ∩ t) a) (le_inter
(le_of_add_le_add_right (le_trans hl (inter_le_left _ _)))
(le_of_add_le_add_right (le_trans hl (inter_le_right _ _))))
end
theorem add_inter_distrib (s t u : multiset α) : s + (t ∩ u) = (s + t) ∩ (s + u) :=
by rw [add_comm, inter_add_distrib, add_comm s, add_comm s]
theorem cons_inter_distrib (a : α) (s t : multiset α) : a :: (s ∩ t) = (a :: s) ∩ (a :: t) :=
by simp
theorem union_add_inter (s t : multiset α) : s ∪ t + s ∩ t = s + t :=
begin
apply le_antisymm,
{ rw union_add_distrib,
refine union_le (add_le_add_left (inter_le_right _ _) _) _,
rw add_comm, exact add_le_add_right (inter_le_left _ _) _ },
{ rw [add_comm, add_inter_distrib],
refine le_inter (add_le_add_right (le_union_right _ _) _) _,
rw add_comm, exact add_le_add_right (le_union_left _ _) _ }
end
theorem sub_add_inter (s t : multiset α) : s - t + s ∩ t = s :=
begin
rw [inter_comm],
revert s, refine multiset.induction_on t (by simp) (λ a t IH s, _),
by_cases a ∈ s,
{ rw [cons_inter_of_pos _ h, sub_cons, add_cons, IH, cons_erase h] },
{ rw [cons_inter_of_neg _ h, sub_cons, erase_of_not_mem h, IH] }
end
theorem sub_inter (s t : multiset α) : s - (s ∩ t) = s - t :=
add_right_cancel $
by rw [sub_add_inter s t, sub_add_cancel (inter_le_left _ _)]
end
/- filter -/
section
variables {p : α → Prop} [decidable_pred p]
/-- `filter p s` returns the elements in `s` (with the same multiplicities)
which satisfy `p`, and removes the rest. -/
def filter (p : α → Prop) [h : decidable_pred p] (s : multiset α) : multiset α :=
quot.lift_on s (λ l, (filter p l : multiset α))
(λ l₁ l₂ h, quot.sound $ perm_filter p h)
@[simp] theorem coe_filter (p : α → Prop) [h : decidable_pred p]
(l : list α) : filter p (↑l) = l.filter p := rfl
@[simp] theorem filter_zero (p : α → Prop) [h : decidable_pred p] : filter p 0 = 0 := rfl
@[simp] theorem filter_cons_of_pos {a : α} (s) : p a → filter p (a::s) = a :: filter p s :=
quot.induction_on s $ λ l h, congr_arg coe $ filter_cons_of_pos l h
@[simp] theorem filter_cons_of_neg {a : α} (s) : ¬ p a → filter p (a::s) = filter p s :=
quot.induction_on s $ λ l h, @congr_arg _ _ _ _ coe $ filter_cons_of_neg l h
lemma filter_congr {p q : α → Prop} [decidable_pred p] [decidable_pred q]
{s : multiset α} : (∀ x ∈ s, p x ↔ q x) → filter p s = filter q s :=
quot.induction_on s $ λ l h, congr_arg coe $ filter_congr h
@[simp] theorem filter_add (s t : multiset α) :
filter p (s + t) = filter p s + filter p t :=
quotient.induction_on₂ s t $ λ l₁ l₂, congr_arg coe $ filter_append _ _
@[simp] theorem filter_le (s : multiset α) : filter p s ≤ s :=
quot.induction_on s $ λ l, subperm_of_sublist $ filter_sublist _
@[simp] theorem filter_subset (s : multiset α) : filter p s ⊆ s :=
subset_of_le $ filter_le _
@[simp] theorem mem_filter {a : α} {s} : a ∈ filter p s ↔ a ∈ s ∧ p a :=
quot.induction_on s $ λ l, mem_filter
theorem of_mem_filter {a : α} {s} (h : a ∈ filter p s) : p a :=
(mem_filter.1 h).2
theorem mem_of_mem_filter {a : α} {s} (h : a ∈ filter p s) : a ∈ s :=
(mem_filter.1 h).1
theorem mem_filter_of_mem {a : α} {l} (m : a ∈ l) (h : p a) : a ∈ filter p l :=
mem_filter.2 ⟨m, h⟩
theorem filter_eq_self {s} : filter p s = s ↔ ∀ a ∈ s, p a :=
quot.induction_on s $ λ l, iff.trans ⟨λ h,
eq_of_sublist_of_length_eq (filter_sublist _) (@congr_arg _ _ _ _ card h),
congr_arg coe⟩ filter_eq_self
theorem filter_eq_nil {s} : filter p s = 0 ↔ ∀ a ∈ s, ¬p a :=
quot.induction_on s $ λ l, iff.trans ⟨λ h,
eq_nil_of_length_eq_zero (@congr_arg _ _ _ _ card h),
congr_arg coe⟩ filter_eq_nil
theorem filter_le_filter {s t} (h : s ≤ t) : filter p s ≤ filter p t :=
le_induction_on h $ λ l₁ l₂ h, subperm_of_sublist $ filter_sublist_filter h
theorem le_filter {s t} : s ≤ filter p t ↔ s ≤ t ∧ ∀ a ∈ s, p a :=
⟨λ h, ⟨le_trans h (filter_le _), λ a m, of_mem_filter (mem_of_le h m)⟩,
λ ⟨h, al⟩, filter_eq_self.2 al ▸ filter_le_filter h⟩
@[simp] theorem filter_sub [decidable_eq α] (s t : multiset α) :
filter p (s - t) = filter p s - filter p t :=
begin
revert s, refine multiset.induction_on t (by simp) (λ a t IH s, _),
rw [sub_cons, IH],
by_cases p a,
{ rw [filter_cons_of_pos _ h, sub_cons], congr,
by_cases m : a ∈ s,
{ rw [← cons_inj_right a, ← filter_cons_of_pos _ h,
cons_erase (mem_filter_of_mem m h), cons_erase m] },
{ rw [erase_of_not_mem m, erase_of_not_mem (mt mem_of_mem_filter m)] } },
{ rw [filter_cons_of_neg _ h],
by_cases m : a ∈ s,
{ rw [(by rw filter_cons_of_neg _ h : filter p (erase s a) = filter p (a :: erase s a)),
cons_erase m] },
{ rw [erase_of_not_mem m] } }
end
@[simp] theorem filter_union [decidable_eq α] (s t : multiset α) :
filter p (s ∪ t) = filter p s ∪ filter p t :=
by simp [(∪), union]
@[simp] theorem filter_inter [decidable_eq α] (s t : multiset α) :
filter p (s ∩ t) = filter p s ∩ filter p t :=
le_antisymm (le_inter
(filter_le_filter $ inter_le_left _ _)
(filter_le_filter $ inter_le_right _ _)) $ le_filter.2
⟨inf_le_inf (filter_le _) (filter_le _),
λ a h, of_mem_filter (mem_of_le (inter_le_left _ _) h)⟩
@[simp] theorem filter_filter {q} [decidable_pred q] (s : multiset α) :
filter p (filter q s) = filter (λ a, p a ∧ q a) s :=
quot.induction_on s $ λ l, congr_arg coe $ filter_filter l
theorem filter_add_filter {q} [decidable_pred q] (s : multiset α) :
filter p s + filter q s = filter (λ a, p a ∨ q a) s + filter (λ a, p a ∧ q a) s :=
multiset.induction_on s rfl $ λ a s IH,
by by_cases p a; by_cases q a; simp *
theorem filter_add_not (s : multiset α) :
filter p s + filter (λ a, ¬ p a) s = s :=
by rw [filter_add_filter, filter_eq_self.2, filter_eq_nil.2]; simp [decidable.em]
/- filter_map -/
/-- `filter_map f s` is a combination filter/map operation on `s`.
The function `f : α → option β` is applied to each element of `s`;
if `f a` is `some b` then `b` is added to the result, otherwise
`a` is removed from the resulting multiset. -/
def filter_map (f : α → option β) (s : multiset α) : multiset β :=
quot.lift_on s (λ l, (filter_map f l : multiset β))
(λ l₁ l₂ h, quot.sound $perm_filter_map f h)
@[simp] theorem coe_filter_map (f : α → option β) (l : list α) : filter_map f l = l.filter_map f := rfl
@[simp] theorem filter_map_zero (f : α → option β) : filter_map f 0 = 0 := rfl
@[simp] theorem filter_map_cons_none {f : α → option β} (a : α) (s : multiset α) (h : f a = none) :
filter_map f (a :: s) = filter_map f s :=
quot.induction_on s $ λ l, @congr_arg _ _ _ _ coe $ filter_map_cons_none a l h
@[simp] theorem filter_map_cons_some (f : α → option β)
(a : α) (s : multiset α) {b : β} (h : f a = some b) :
filter_map f (a :: s) = b :: filter_map f s :=
quot.induction_on s $ λ l, @congr_arg _ _ _ _ coe $ filter_map_cons_some f a l h
theorem filter_map_eq_map (f : α → β) : filter_map (some ∘ f) = map f :=
funext $ λ s, quot.induction_on s $ λ l,
@congr_arg _ _ _ _ coe $ congr_fun (filter_map_eq_map f) l
theorem filter_map_eq_filter (p : α → Prop) [decidable_pred p] :
filter_map (option.guard p) = filter p :=
funext $ λ s, quot.induction_on s $ λ l,
@congr_arg _ _ _ _ coe $ congr_fun (filter_map_eq_filter p) l
theorem filter_map_filter_map (f : α → option β) (g : β → option γ) (s : multiset α) :
filter_map g (filter_map f s) = filter_map (λ x, (f x).bind g) s :=
quot.induction_on s $ λ l, congr_arg coe $ filter_map_filter_map f g l
theorem map_filter_map (f : α → option β) (g : β → γ) (s : multiset α) :
map g (filter_map f s) = filter_map (λ x, (f x).map g) s :=
quot.induction_on s $ λ l, congr_arg coe $ map_filter_map f g l
theorem filter_map_map (f : α → β) (g : β → option γ) (s : multiset α) :
filter_map g (map f s) = filter_map (g ∘ f) s :=
quot.induction_on s $ λ l, congr_arg coe $ filter_map_map f g l
theorem filter_filter_map (f : α → option β) (p : β → Prop) [decidable_pred p] (s : multiset α) :
filter p (filter_map f s) = filter_map (λ x, (f x).filter p) s :=
quot.induction_on s $ λ l, congr_arg coe $ filter_filter_map f p l
theorem filter_map_filter (p : α → Prop) [decidable_pred p] (f : α → option β) (s : multiset α) :
filter_map f (filter p s) = filter_map (λ x, if p x then f x else none) s :=
quot.induction_on s $ λ l, congr_arg coe $ filter_map_filter p f l
@[simp] theorem filter_map_some (s : multiset α) : filter_map some s = s :=
quot.induction_on s $ λ l, congr_arg coe $ filter_map_some l
@[simp] theorem mem_filter_map (f : α → option β) (s : multiset α) {b : β} :
b ∈ filter_map f s ↔ ∃ a, a ∈ s ∧ f a = some b :=
quot.induction_on s $ λ l, mem_filter_map f l
theorem map_filter_map_of_inv (f : α → option β) (g : β → α)
(H : ∀ x : α, (f x).map g = some x) (s : multiset α) :
map g (filter_map f s) = s :=
quot.induction_on s $ λ l, congr_arg coe $ map_filter_map_of_inv f g H l
theorem filter_map_le_filter_map (f : α → option β) {s t : multiset α}
(h : s ≤ t) : filter_map f s ≤ filter_map f t :=
le_induction_on h $ λ l₁ l₂ h,
subperm_of_sublist $ filter_map_sublist_filter_map _ h
/- powerset -/
def powerset_aux (l : list α) : list (multiset α) :=
0 :: sublists_aux l (λ x y, x :: y)
theorem powerset_aux_eq_map_coe {l : list α} :
powerset_aux l = (sublists l).map coe :=
by simp [powerset_aux, sublists];
rw [← show @sublists_aux₁ α (multiset α) l (λ x, [↑x]) =
sublists_aux l (λ x, list.cons ↑x),
from sublists_aux₁_eq_sublists_aux _ _,
sublists_aux_cons_eq_sublists_aux₁,
← bind_ret_eq_map, sublists_aux₁_bind]; refl
@[simp] theorem mem_powerset_aux {l : list α} {s} :
s ∈ powerset_aux l ↔ s ≤ ↑l :=
quotient.induction_on s $
by simp [powerset_aux_eq_map_coe, subperm, and.comm]
def powerset_aux' (l : list α) : list (multiset α) := (sublists' l).map coe
theorem powerset_aux_perm_powerset_aux' {l : list α} :
powerset_aux l ~ powerset_aux' l :=
by rw powerset_aux_eq_map_coe; exact
perm_map _ (sublists_perm_sublists' _)
@[simp] theorem powerset_aux'_nil : powerset_aux' (@nil α) = [0] := rfl
@[simp] theorem powerset_aux'_cons (a : α) (l : list α) :
powerset_aux' (a::l) = powerset_aux' l ++ list.map (cons a) (powerset_aux' l) :=
by simp [powerset_aux']; refl
theorem powerset_aux'_perm {l₁ l₂ : list α} (p : l₁ ~ l₂) :
powerset_aux' l₁ ~ powerset_aux' l₂ :=
begin
induction p with a l₁ l₂ p IH a b l l₁ l₂ l₃ p₁ p₂ IH₁ IH₂, {simp},
{ simp, exact perm_app IH (perm_map _ IH) },
{ simp, apply perm_app_right,
rw [← append_assoc, ← append_assoc,
(by funext s; simp [cons_swap] : cons b ∘ cons a = cons a ∘ cons b)],
exact perm_app_left _ perm_app_comm },
{ exact IH₁.trans IH₂ }
end
theorem powerset_aux_perm {l₁ l₂ : list α} (p : l₁ ~ l₂) :
powerset_aux l₁ ~ powerset_aux l₂ :=
powerset_aux_perm_powerset_aux'.trans $
(powerset_aux'_perm p).trans powerset_aux_perm_powerset_aux'.symm
def powerset (s : multiset α) : multiset (multiset α) :=
quot.lift_on s
(λ l, (powerset_aux l : multiset (multiset α)))
(λ l₁ l₂ h, quot.sound (powerset_aux_perm h))
theorem powerset_coe (l : list α) :
@powerset α l = ((sublists l).map coe : list (multiset α)) :=
congr_arg coe powerset_aux_eq_map_coe
@[simp] theorem powerset_coe' (l : list α) :
@powerset α l = ((sublists' l).map coe : list (multiset α)) :=
quot.sound powerset_aux_perm_powerset_aux'
@[simp] theorem powerset_zero : @powerset α 0 = 0::0 := rfl
@[simp] theorem powerset_cons (a : α) (s) :
powerset (a::s) = powerset s + map (cons a) (powerset s) :=
quotient.induction_on s $ λ l, by simp; refl
@[simp] theorem mem_powerset {s t : multiset α} :
s ∈ powerset t ↔ s ≤ t :=
quotient.induction_on₂ s t $ by simp [subperm, and.comm]
theorem map_single_le_powerset (s : multiset α) :
s.map (λ a, a::0) ≤ powerset s :=
quotient.induction_on s $ λ l, begin
simp [powerset_coe],
show l.map (coe ∘ list.ret) <+~ (sublists l).map coe,
rw ← list.map_map,
exact subperm_of_sublist
(map_sublist_map _ (map_ret_sublist_sublists _))
end
@[simp] theorem card_powerset (s : multiset α) :
card (powerset s) = 2 ^ card s :=
quotient.induction_on s $ by simp
/- diagonal -/
theorem revzip_powerset_aux {l : list α} ⦃s t⦄
(h : (s, t) ∈ revzip (powerset_aux l)) : s + t = ↑l :=
begin
rw [revzip, powerset_aux_eq_map_coe, ← map_reverse, zip_map, ← revzip] at h,
simp at h, rcases h with ⟨l₁, l₂, h, rfl, rfl⟩,
exact quot.sound (revzip_sublists _ _ _ h)
end
theorem revzip_powerset_aux' {l : list α} ⦃s t⦄
(h : (s, t) ∈ revzip (powerset_aux' l)) : s + t = ↑l :=
begin
rw [revzip, powerset_aux', ← map_reverse, zip_map, ← revzip] at h,
simp at h, rcases h with ⟨l₁, l₂, h, rfl, rfl⟩,
exact quot.sound (revzip_sublists' _ _ _ h)
end
theorem revzip_powerset_aux_lemma [decidable_eq α] (l : list α)
{l' : list (multiset α)} (H : ∀ ⦃s t⦄, (s, t) ∈ revzip l' → s + t = ↑l) :
revzip l' = l'.map (λ x, (x, ↑l - x)) :=
begin
have : forall₂ (λ (p : multiset α × multiset α) (s : multiset α), p = (s, ↑l - s))
(revzip l') ((revzip l').map prod.fst),
{ rw forall₂_map_right_iff,
apply forall₂_same, rintro ⟨s, t⟩ h,
dsimp, rw [← H h, add_sub_cancel_left] },
rw [← forall₂_eq_eq_eq, forall₂_map_right_iff], simpa
end
theorem revzip_powerset_aux_perm_aux' {l : list α} :
revzip (powerset_aux l) ~ revzip (powerset_aux' l) :=
begin
haveI := classical.dec_eq α,
rw [revzip_powerset_aux_lemma l revzip_powerset_aux,
revzip_powerset_aux_lemma l revzip_powerset_aux'],
exact perm_map _ powerset_aux_perm_powerset_aux',
end
theorem revzip_powerset_aux_perm {l₁ l₂ : list α} (p : l₁ ~ l₂) :
revzip (powerset_aux l₁) ~ revzip (powerset_aux l₂) :=
begin
haveI := classical.dec_eq α,
simp [λ l:list α, revzip_powerset_aux_lemma l revzip_powerset_aux, coe_eq_coe.2 p],
exact perm_map _ (powerset_aux_perm p)
end
def diagonal (s : multiset α) : multiset (multiset α × multiset α) :=
quot.lift_on s
(λ l, (revzip (powerset_aux l) : multiset (multiset α × multiset α)))
(λ l₁ l₂ h, quot.sound (revzip_powerset_aux_perm h))
theorem diagonal_coe (l : list α) :
@diagonal α l = revzip (powerset_aux l) := rfl
@[simp] theorem diagonal_coe' (l : list α) :
@diagonal α l = revzip (powerset_aux' l) :=
quot.sound revzip_powerset_aux_perm_aux'
@[simp] theorem mem_diagonal {s₁ s₂ t : multiset α} :
(s₁, s₂) ∈ diagonal t ↔ s₁ + s₂ = t :=
quotient.induction_on t $ λ l, begin
simp [diagonal_coe], refine ⟨λ h, revzip_powerset_aux h, λ h, _⟩,
haveI := classical.dec_eq α,
simp [revzip_powerset_aux_lemma l revzip_powerset_aux, h.symm],
exact ⟨_, le_add_right _ _, rfl, add_sub_cancel_left _ _⟩
end
@[simp] theorem diagonal_map_fst (s : multiset α) :
(diagonal s).map prod.fst = powerset s :=
quotient.induction_on s $ λ l,
by simp [powerset_aux']
@[simp] theorem diagonal_map_snd (s : multiset α) :
(diagonal s).map prod.snd = powerset s :=
quotient.induction_on s $ λ l,
by simp [powerset_aux']
@[simp] theorem diagonal_zero : @diagonal α 0 = (0, 0)::0 := rfl
@[simp] theorem diagonal_cons (a : α) (s) : diagonal (a::s) =
map (prod.map id (cons a)) (diagonal s) +
map (prod.map (cons a) id) (diagonal s) :=
quotient.induction_on s $ λ l, begin
simp [revzip, reverse_append],
rw [← zip_map, ← zip_map, zip_append, (_ : _++_=_)],
{congr; simp}, {simp}
end
@[simp] theorem card_diagonal (s : multiset α) :
card (diagonal s) = 2 ^ card s :=
by have := card_powerset s;
rwa [← diagonal_map_fst, card_map] at this
lemma prod_map_add [comm_semiring β] {s : multiset α} {f g : α → β} :
prod (s.map (λa, f a + g a)) = sum ((diagonal s).map (λp, (p.1.map f).prod * (p.2.map g).prod)) :=
begin
refine s.induction_on _ _,
{ simp },
{ assume a s ih, simp [ih, add_mul, mul_comm, mul_left_comm, mul_assoc, sum_map_mul_left.symm] },
end
/- countp -/
/-- `countp p s` counts the number of elements of `s` (with multiplicity) that
satisfy `p`. -/
def countp (p : α → Prop) [decidable_pred p] (s : multiset α) : ℕ :=
quot.lift_on s (countp p) (λ l₁ l₂, perm_countp p)
@[simp] theorem coe_countp (l : list α) : countp p l = l.countp p := rfl
@[simp] theorem countp_zero (p : α → Prop) [decidable_pred p] : countp p 0 = 0 := rfl
@[simp] theorem countp_cons_of_pos {a : α} (s) : p a → countp p (a::s) = countp p s + 1 :=
quot.induction_on s countp_cons_of_pos
@[simp] theorem countp_cons_of_neg {a : α} (s) : ¬ p a → countp p (a::s) = countp p s :=
quot.induction_on s countp_cons_of_neg
theorem countp_eq_card_filter (s) : countp p s = card (filter p s) :=
quot.induction_on s $ λ l, countp_eq_length_filter _
@[simp] theorem countp_add (s t) : countp p (s + t) = countp p s + countp p t :=
by simp [countp_eq_card_filter]
theorem countp_pos {s} : 0 < countp p s ↔ ∃ a ∈ s, p a :=
by simp [countp_eq_card_filter, card_pos_iff_exists_mem]
@[simp] theorem countp_sub [decidable_eq α] {s t : multiset α} (h : t ≤ s) :
countp p (s - t) = countp p s - countp p t :=
by simp [countp_eq_card_filter, h, filter_le_filter]
theorem countp_pos_of_mem {s a} (h : a ∈ s) (pa : p a) : 0 < countp p s :=
countp_pos.2 ⟨_, h, pa⟩
theorem countp_le_of_le {s t} (h : s ≤ t) : countp p s ≤ countp p t :=
by simpa [countp_eq_card_filter] using card_le_of_le (filter_le_filter h)
@[simp] theorem countp_filter {q} [decidable_pred q] (s : multiset α) :
countp p (filter q s) = countp (λ a, p a ∧ q a) s :=
by simp [countp_eq_card_filter]
end
/- count -/
section
variable [decidable_eq α]
/-- `count a s` is the multiplicity of `a` in `s`. -/
def count (a : α) : multiset α → ℕ := countp (eq a)
@[simp] theorem coe_count (a : α) (l : list α) : count a (↑l) = l.count a := coe_countp _
@[simp] theorem count_zero (a : α) : count a 0 = 0 := rfl
@[simp] theorem count_cons_self (a : α) (s : multiset α) : count a (a::s) = succ (count a s) :=
countp_cons_of_pos _ rfl
@[simp] theorem count_cons_of_ne {a b : α} (h : a ≠ b) (s : multiset α) : count a (b::s) = count a s :=
countp_cons_of_neg _ h
theorem count_le_of_le (a : α) {s t} : s ≤ t → count a s ≤ count a t :=
countp_le_of_le
theorem count_le_count_cons (a b : α) (s : multiset α) : count a s ≤ count a (b :: s) :=
count_le_of_le _ (le_cons_self _ _)
theorem count_singleton (a : α) : count a (a::0) = 1 :=
by simp
@[simp] theorem count_add (a : α) : ∀ s t, count a (s + t) = count a s + count a t :=
countp_add
@[simp] theorem count_smul (a : α) (n s) : count a (n • s) = n * count a s :=
by induction n; simp [*, succ_smul', succ_mul]
theorem count_pos {a : α} {s : multiset α} : 0 < count a s ↔ a ∈ s :=
by simp [count, countp_pos]
@[simp] theorem count_eq_zero_of_not_mem {a : α} {s : multiset α} (h : a ∉ s) : count a s = 0 :=
by_contradiction $ λ h', h $ count_pos.1 (nat.pos_of_ne_zero h')
theorem count_eq_zero {a : α} {s : multiset α} : count a s = 0 ↔ a ∉ s :=
iff_not_comm.1 $ count_pos.symm.trans pos_iff_ne_zero
@[simp] theorem count_repeat (a : α) (n : ℕ) : count a (repeat a n) = n :=
by simp [repeat]
@[simp] theorem count_erase_self (a : α) (s : multiset α) : count a (erase s a) = pred (count a s) :=
begin
by_cases a ∈ s,
{ rw [(by rw cons_erase h : count a s = count a (a::erase s a)),
count_cons_self]; refl },
{ rw [erase_of_not_mem h, count_eq_zero.2 h]; refl }
end
@[simp] theorem count_erase_of_ne {a b : α} (ab : a ≠ b) (s : multiset α) : count a (erase s b) = count a s :=
begin
by_cases b ∈ s,
{ rw [← count_cons_of_ne ab, cons_erase h] },
{ rw [erase_of_not_mem h] }
end
@[simp] theorem count_sub (a : α) (s t : multiset α) : count a (s - t) = count a s - count a t :=
begin
revert s, refine multiset.induction_on t (by simp) (λ b t IH s, _),
rw [sub_cons, IH],
by_cases ab : a = b,
{ subst b, rw [count_erase_self, count_cons_self, sub_succ, pred_sub] },
{ rw [count_erase_of_ne ab, count_cons_of_ne ab] }
end
@[simp] theorem count_union (a : α) (s t : multiset α) : count a (s ∪ t) = max (count a s) (count a t) :=
by simp [(∪), union, sub_add_eq_max, -add_comm]
@[simp] theorem count_inter (a : α) (s t : multiset α) : count a (s ∩ t) = min (count a s) (count a t) :=
begin
apply @nat.add_left_cancel (count a (s - t)),
rw [← count_add, sub_add_inter, count_sub, sub_add_min],
end
lemma count_bind {m : multiset β} {f : β → multiset α} {a : α} :
count a (bind m f) = sum (m.map $ λb, count a $ f b) :=
multiset.induction_on m (by simp) (by simp)
theorem le_count_iff_repeat_le {a : α} {s : multiset α} {n : ℕ} : n ≤ count a s ↔ repeat a n ≤ s :=
quot.induction_on s $ λ l, le_count_iff_repeat_sublist.trans repeat_le_coe.symm
@[simp] theorem count_filter {p} [decidable_pred p]
{a} {s : multiset α} (h : p a) : count a (filter p s) = count a s :=
quot.induction_on s $ λ l, count_filter h
theorem ext {s t : multiset α} : s = t ↔ ∀ a, count a s = count a t :=
quotient.induction_on₂ s t $ λ l₁ l₂, quotient.eq.trans perm_iff_count
@[extensionality]
theorem ext' {s t : multiset α} : (∀ a, count a s = count a t) → s = t :=
ext.2
theorem le_iff_count {s t : multiset α} : s ≤ t ↔ ∀ a, count a s ≤ count a t :=
⟨λ h a, count_le_of_le a h, λ al,
by rw ← (ext.2 (λ a, by simp [max_eq_right (al a)]) : s ∪ t = t);
apply le_union_left⟩
instance : distrib_lattice (multiset α) :=
{ le_sup_inf := λ s t u, le_of_eq $ eq.symm $
ext.2 $ λ a, by simp [max_min_distrib_left],
..multiset.lattice.lattice }
instance : semilattice_sup_bot (multiset α) :=
{ bot := 0,
bot_le := zero_le,
..multiset.lattice.lattice }
end
/- relator -/
section rel
/-- `rel r s t` -- lift the relation `r` between two elements to a relation between `s` and `t`,
s.t. there is a one-to-one mapping betweem elements in `s` and `t` following `r`. -/
inductive rel (r : α → β → Prop) : multiset α → multiset β → Prop
| zero {} : rel 0 0
| cons {a b as bs} : r a b → rel as bs → rel (a :: as) (b :: bs)
run_cmd tactic.mk_iff_of_inductive_prop `multiset.rel `multiset.rel_iff
variables {δ : Type*} {r : α → β → Prop} {p : γ → δ → Prop}
private lemma rel_flip_aux {s t} (h : rel r s t) : rel (flip r) t s :=
rel.rec_on h rel.zero (assume _ _ _ _ h₀ h₁ ih, rel.cons h₀ ih)
lemma rel_flip {s t} : rel (flip r) s t ↔ rel r t s :=
⟨rel_flip_aux, rel_flip_aux⟩
lemma rel_eq_refl {s : multiset α} : rel (=) s s :=
multiset.induction_on s rel.zero (assume a s, rel.cons rfl)
lemma rel_eq {s t : multiset α} : rel (=) s t ↔ s = t :=
begin
split,
{ assume h, induction h; simp * },
{ assume h, subst h, exact rel_eq_refl }
end
lemma rel.mono {p : α → β → Prop} {s t} (h : ∀a b, r a b → p a b) (hst : rel r s t) : rel p s t :=
begin
induction hst,
case rel.zero { exact rel.zero },
case rel.cons : a b s t hab hst ih { exact ih.cons (h a b hab) }
end
lemma rel.add {s t u v} (hst : rel r s t) (huv : rel r u v) : rel r (s + u) (t + v) :=
begin
induction hst,
case rel.zero { simpa using huv },
case rel.cons : a b s t hab hst ih { simpa using ih.cons hab }
end
lemma rel_flip_eq {s t : multiset α} : rel (λa b, b = a) s t ↔ s = t :=
show rel (flip (=)) s t ↔ s = t, by rw [rel_flip, rel_eq, eq_comm]
@[simp] lemma rel_zero_left {b : multiset β} : rel r 0 b ↔ b = 0 :=
by rw [rel_iff]; simp
@[simp] lemma rel_zero_right {a : multiset α} : rel r a 0 ↔ a = 0 :=
by rw [rel_iff]; simp
lemma rel_cons_left {a as bs} :
rel r (a :: as) bs ↔ (∃b bs', r a b ∧ rel r as bs' ∧ bs = b :: bs') :=
begin
split,
{ generalize hm : a :: as = m,
assume h,
induction h generalizing as,
case rel.zero { simp at hm, contradiction },
case rel.cons : a' b as' bs ha'b h ih {
rcases cons_eq_cons.1 hm with ⟨eq₁, eq₂⟩ | ⟨h, cs, eq₁, eq₂⟩,
{ subst eq₁, subst eq₂, exact ⟨b, bs, ha'b, h, rfl⟩ },
{ rcases ih eq₂.symm with ⟨b', bs', h₁, h₂, eq⟩,
exact ⟨b', b::bs', h₁, eq₁.symm ▸ rel.cons ha'b h₂, eq.symm ▸ cons_swap _ _ _⟩ }
} },
{ exact assume ⟨b, bs', hab, h, eq⟩, eq.symm ▸ rel.cons hab h }
end
lemma rel_cons_right {as b bs} :
rel r as (b :: bs) ↔ (∃a as', r a b ∧ rel r as' bs ∧ as = a :: as') :=
begin
rw [← rel_flip, rel_cons_left],
apply exists_congr, assume a,
apply exists_congr, assume as',
rw [rel_flip, flip]
end
lemma rel_add_left {as₀ as₁} :
∀{bs}, rel r (as₀ + as₁) bs ↔ (∃bs₀ bs₁, rel r as₀ bs₀ ∧ rel r as₁ bs₁ ∧ bs = bs₀ + bs₁) :=
multiset.induction_on as₀ (by simp)
begin
assume a s ih bs,
simp only [ih, cons_add, rel_cons_left],
split,
{ assume h,
rcases h with ⟨b, bs', hab, h, rfl⟩,
rcases h with ⟨bs₀, bs₁, h₀, h₁, rfl⟩,
exact ⟨b :: bs₀, bs₁, ⟨b, bs₀, hab, h₀, rfl⟩, h₁, by simp⟩ },
{ assume h,
rcases h with ⟨bs₀, bs₁, h, h₁, rfl⟩,
rcases h with ⟨b, bs, hab, h₀, rfl⟩,
exact ⟨b, bs + bs₁, hab, ⟨bs, bs₁, h₀, h₁, rfl⟩, by simp⟩ }
end
lemma rel_add_right {as bs₀ bs₁} :
rel r as (bs₀ + bs₁) ↔ (∃as₀ as₁, rel r as₀ bs₀ ∧ rel r as₁ bs₁ ∧ as = as₀ + as₁) :=
by rw [← rel_flip, rel_add_left]; simp [rel_flip]
lemma rel_map_left {s : multiset γ} {f : γ → α} :
∀{t}, rel r (s.map f) t ↔ rel (λa b, r (f a) b) s t :=
multiset.induction_on s (by simp) (by simp [rel_cons_left] {contextual := tt})
lemma rel_map_right {s : multiset α} {t : multiset γ} {f : γ → β} :
rel r s (t.map f) ↔ rel (λa b, r a (f b)) s t :=
by rw [← rel_flip, rel_map_left, ← rel_flip]; refl
lemma rel_join {s t} (h : rel (rel r) s t) : rel r s.join t.join :=
begin
induction h,
case rel.zero { simp },
case rel.cons : a b s t hab hst ih { simpa using hab.add ih }
end
lemma rel_map {p : γ → δ → Prop} {s t} {f : α → γ} {g : β → δ} (h : (r ⇒ p) f g) (hst : rel r s t) :
rel p (s.map f) (t.map g) :=
by rw [rel_map_left, rel_map_right]; exact hst.mono (assume a b, h)
lemma rel_bind {p : γ → δ → Prop} {s t} {f : α → multiset γ} {g : β → multiset δ}
(h : (r ⇒ rel p) f g) (hst : rel r s t) :
rel p (s.bind f) (t.bind g) :=
by apply rel_join; apply rel_map; assumption
lemma card_eq_card_of_rel {r : α → β → Prop} {s : multiset α} {t : multiset β} (h : rel r s t) :
card s = card t :=
by induction h; simp [*]
end rel
section map
theorem map_eq_map {f : α → β} (hf : function.injective f) {s t : multiset α} :
s.map f = t.map f ↔ s = t :=
by rw [← rel_eq, ← rel_eq, rel_map_left, rel_map_right]; simp [hf.eq_iff]
theorem injective_map {f : α → β} (hf : function.injective f) :
function.injective (multiset.map f) :=
assume x y, (map_eq_map hf).1
end map
section quot
theorem map_mk_eq_map_mk_of_rel {r : α → α → Prop} {s t : multiset α} (hst : s.rel r t) :
s.map (quot.mk r) = t.map (quot.mk r) :=
rel.rec_on hst rfl $ assume a b s t hab hst ih, by simp [ih, quot.sound hab]
theorem exists_multiset_eq_map_quot_mk {r : α → α → Prop} (s : multiset (quot r)) :
∃t:multiset α, s = t.map (quot.mk r) :=
multiset.induction_on s ⟨0, rfl⟩ $
assume a s ⟨t, ht⟩, quot.induction_on a $ assume a, ht.symm ▸ ⟨a::t, (map_cons _ _ _).symm⟩
theorem induction_on_multiset_quot
{r : α → α → Prop} {p : multiset (quot r) → Prop} (s : multiset (quot r)) :
(∀s:multiset α, p (s.map (quot.mk r))) → p s :=
match s, exists_multiset_eq_map_quot_mk s with _, ⟨t, rfl⟩ := assume h, h _ end
end quot
/- disjoint -/
/-- `disjoint s t` means that `s` and `t` have no elements in common. -/
def disjoint (s t : multiset α) : Prop := ∀ ⦃a⦄, a ∈ s → a ∈ t → false
@[simp] theorem coe_disjoint (l₁ l₂ : list α) : @disjoint α l₁ l₂ ↔ l₁.disjoint l₂ := iff.rfl
theorem disjoint.symm {s t : multiset α} (d : disjoint s t) : disjoint t s
| a i₂ i₁ := d i₁ i₂
@[simp] theorem disjoint_comm {s t : multiset α} : disjoint s t ↔ disjoint t s :=
⟨disjoint.symm, disjoint.symm⟩
theorem disjoint_left {s t : multiset α} : disjoint s t ↔ ∀ {a}, a ∈ s → a ∉ t := iff.rfl
theorem disjoint_right {s t : multiset α} : disjoint s t ↔ ∀ {a}, a ∈ t → a ∉ s :=
disjoint_comm
theorem disjoint_iff_ne {s t : multiset α} : disjoint s t ↔ ∀ a ∈ s, ∀ b ∈ t, a ≠ b :=
by simp [disjoint_left, imp_not_comm]
theorem disjoint_of_subset_left {s t u : multiset α} (h : s ⊆ u) (d : disjoint u t) : disjoint s t
| x m₁ := d (h m₁)
theorem disjoint_of_subset_right {s t u : multiset α} (h : t ⊆ u) (d : disjoint s u) : disjoint s t
| x m m₁ := d m (h m₁)
theorem disjoint_of_le_left {s t u : multiset α} (h : s ≤ u) : disjoint u t → disjoint s t :=
disjoint_of_subset_left (subset_of_le h)
theorem disjoint_of_le_right {s t u : multiset α} (h : t ≤ u) : disjoint s u → disjoint s t :=
disjoint_of_subset_right (subset_of_le h)
@[simp] theorem zero_disjoint (l : multiset α) : disjoint 0 l
| a := (not_mem_nil a).elim
@[simp] theorem singleton_disjoint {l : multiset α} {a : α} : disjoint (a::0) l ↔ a ∉ l :=
by simp [disjoint]; refl
@[simp] theorem disjoint_singleton {l : multiset α} {a : α} : disjoint l (a::0) ↔ a ∉ l :=
by rw disjoint_comm; simp
@[simp] theorem disjoint_add_left {s t u : multiset α} :
disjoint (s + t) u ↔ disjoint s u ∧ disjoint t u :=
by simp [disjoint, or_imp_distrib, forall_and_distrib]
@[simp] theorem disjoint_add_right {s t u : multiset α} :
disjoint s (t + u) ↔ disjoint s t ∧ disjoint s u :=
disjoint_comm.trans $ by simp [disjoint_append_left]
@[simp] theorem disjoint_cons_left {a : α} {s t : multiset α} :
disjoint (a::s) t ↔ a ∉ t ∧ disjoint s t :=
(@disjoint_add_left _ (a::0) s t).trans $ by simp
@[simp] theorem disjoint_cons_right {a : α} {s t : multiset α} :
disjoint s (a::t) ↔ a ∉ s ∧ disjoint s t :=
disjoint_comm.trans $ by simp [disjoint_cons_left]
theorem inter_eq_zero_iff_disjoint [decidable_eq α] {s t : multiset α} : s ∩ t = 0 ↔ disjoint s t :=
by rw ← subset_zero; simp [subset_iff, disjoint]
@[simp] theorem disjoint_union_left [decidable_eq α] {s t u : multiset α} :
disjoint (s ∪ t) u ↔ disjoint s u ∧ disjoint t u :=
by simp [disjoint, or_imp_distrib, forall_and_distrib]
@[simp] theorem disjoint_union_right [decidable_eq α] {s t u : multiset α} :
disjoint s (t ∪ u) ↔ disjoint s t ∧ disjoint s u :=
by simp [disjoint, or_imp_distrib, forall_and_distrib]
lemma disjoint_map_map {f : α → γ} {g : β → γ} {s : multiset α} {t : multiset β} :
disjoint (s.map f) (t.map g) ↔ (∀a∈s, ∀b∈t, f a ≠ g b) :=
begin
simp [disjoint],
split,
from assume h a ha b hb eq, h _ ha rfl _ hb eq.symm,
from assume h c a ha eq₁ b hb eq₂, h _ ha _ hb (eq₂.symm ▸ eq₁)
end
/-- `pairwise r m` states that there exists a list of the elements s.t. `r` holds pairwise on this list. -/
def pairwise (r : α → α → Prop) (m : multiset α) : Prop :=
∃l:list α, m = l ∧ l.pairwise r
lemma pairwise_coe_iff_pairwise {r : α → α → Prop} (hr : symmetric r) {l : list α} :
multiset.pairwise r l ↔ l.pairwise r :=
iff.intro
(assume ⟨l', eq, h⟩, (list.perm_pairwise hr (quotient.exact eq)).2 h)
(assume h, ⟨l, rfl, h⟩)
/- nodup -/
/-- `nodup s` means that `s` has no duplicates, i.e. the multiplicity of
any element is at most 1. -/
def nodup (s : multiset α) : Prop :=
quot.lift_on s nodup (λ s t p, propext $ perm_nodup p)
@[simp] theorem coe_nodup {l : list α} : @nodup α l ↔ l.nodup := iff.rfl
@[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_zero : @nodup α 0 := pairwise.nil _
@[simp] theorem nodup_cons {a : α} {s : multiset α} : nodup (a::s) ↔ a ∉ s ∧ nodup s :=
quot.induction_on s $ λ l, nodup_cons
theorem nodup_cons_of_nodup {a : α} {s : multiset α} (m : a ∉ s) (n : nodup s) : nodup (a::s) :=
nodup_cons.2 ⟨m, n⟩
theorem nodup_singleton : ∀ a : α, nodup (a::0) := nodup_singleton
theorem nodup_of_nodup_cons {a : α} {s : multiset α} (h : nodup (a::s)) : nodup s :=
(nodup_cons.1 h).2
theorem not_mem_of_nodup_cons {a : α} {s : multiset α} (h : nodup (a::s)) : a ∉ s :=
(nodup_cons.1 h).1
theorem nodup_of_le {s t : multiset α} (h : s ≤ t) : nodup t → nodup s :=
le_induction_on h $ λ l₁ l₂, nodup_of_sublist
theorem not_nodup_pair : ∀ a : α, ¬ nodup (a::a::0) := not_nodup_pair
theorem nodup_iff_le {s : multiset α} : nodup s ↔ ∀ a : α, ¬ a::a::0 ≤ s :=
quot.induction_on s $ λ l, nodup_iff_sublist.trans $ forall_congr $ λ a,
not_congr (@repeat_le_coe _ a 2 _).symm
theorem nodup_iff_count_le_one [decidable_eq α] {s : multiset α} : nodup s ↔ ∀ a, count a s ≤ 1 :=
quot.induction_on s $ λ l, nodup_iff_count_le_one
@[simp] theorem count_eq_one_of_mem [decidable_eq α] {a : α} {s : multiset α}
(d : nodup s) (h : a ∈ s) : count a s = 1 :=
le_antisymm (nodup_iff_count_le_one.1 d a) (count_pos.2 h)
lemma pairwise_of_nodup {r : α → α → Prop} {s : multiset α} :
(∀a∈s, ∀b∈s, a ≠ b → r a b) → nodup s → pairwise r s :=
quotient.induction_on s $ assume l h hl, ⟨l, rfl, hl.imp_of_mem $ assume a b ha hb, h a ha b hb⟩
theorem nodup_add {s t : multiset α} : nodup (s + t) ↔ nodup s ∧ nodup t ∧ disjoint s t :=
quotient.induction_on₂ s t $ λ l₁ l₂, nodup_append
theorem disjoint_of_nodup_add {s t : multiset α} (d : nodup (s + t)) : disjoint s t :=
(nodup_add.1 d).2.2
theorem nodup_add_of_nodup {s t : multiset α} (d₁ : nodup s) (d₂ : nodup t) : nodup (s + t) ↔ disjoint s t :=
by simp [nodup_add, d₁, d₂]
theorem nodup_of_nodup_map (f : α → β) {s : multiset α} : nodup (map f s) → nodup s :=
quot.induction_on s $ λ l, nodup_of_nodup_map f
theorem nodup_map_on {f : α → β} {s : multiset α} : (∀x∈s, ∀y∈s, f x = f y → x = y) →
nodup s → nodup (map f s) :=
quot.induction_on s $ λ l, nodup_map_on
theorem nodup_map {f : α → β} {s : multiset α} (hf : function.injective f) : nodup s → nodup (map f s) :=
nodup_map_on (λ x _ y _ h, hf h)
theorem nodup_filter (p : α → Prop) [decidable_pred p] {s} : nodup s → nodup (filter p s) :=
quot.induction_on s $ λ l, nodup_filter p
@[simp] theorem nodup_attach {s : multiset α} : nodup (attach s) ↔ nodup s :=
quot.induction_on s $ λ l, nodup_attach
theorem nodup_pmap {p : α → Prop} {f : Π a, p a → β} {s : multiset α} {H}
(hf : ∀ a ha b hb, f a ha = f b hb → a = b) : nodup s → nodup (pmap f s H) :=
quot.induction_on s (λ l H, nodup_pmap hf) H
instance nodup_decidable [decidable_eq α] (s : multiset α) : decidable (nodup s) :=
quotient.rec_on_subsingleton s $ λ l, l.nodup_decidable
theorem nodup_erase_eq_filter [decidable_eq α] (a : α) {s} : nodup s → s.erase a = filter (≠ a) s :=
quot.induction_on s $ λ l d, congr_arg coe $ nodup_erase_eq_filter a d
theorem nodup_erase_of_nodup [decidable_eq α] (a : α) {l} : nodup l → nodup (l.erase a) :=
nodup_of_le (erase_le _ _)
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 [and_comm]
theorem mem_erase_of_nodup [decidable_eq α] {a : α} {l} (h : nodup l) : a ∉ l.erase a :=
by rw mem_erase_iff_of_nodup h; simp
theorem nodup_product {s : multiset α} {t : multiset β} : nodup s → nodup t → nodup (product s t) :=
quotient.induction_on₂ s t $ λ l₁ l₂ d₁ d₂, by simp [nodup_product d₁ d₂]
theorem nodup_sigma {σ : α → Type*} {s : multiset α} {t : Π a, multiset (σ a)} :
nodup s → (∀ a, nodup (t a)) → nodup (s.sigma t) :=
quot.induction_on s $ λ l₁,
let l₂ (a) : list (σ a) := classical.some (quotient.exists_rep (t a)) in
have t = λ a, l₂ a, from eq.symm $ funext $ λ a,
classical.some_spec (quotient.exists_rep (t a)),
by rw [this]; simpa using nodup_sigma
theorem nodup_filter_map (f : α → option β) {s : multiset α}
(H : ∀ (a a' : α) (b : β), b ∈ f a → b ∈ f a' → a = a') :
nodup s → nodup (filter_map f s) :=
quot.induction_on s $ λ l, nodup_filter_map H
theorem nodup_range (n : ℕ) : nodup (range n) := nodup_range _
theorem nodup_inter_left [decidable_eq α] {s : multiset α} (t) : nodup s → nodup (s ∩ t) :=
nodup_of_le $ inter_le_left _ _
theorem nodup_inter_right [decidable_eq α] (s) {t : multiset α} : nodup t → nodup (s ∩ t) :=
nodup_of_le $ inter_le_right _ _
@[simp] theorem nodup_union [decidable_eq α] {s t : multiset α} : nodup (s ∪ t) ↔ nodup s ∧ nodup t :=
⟨λ h, ⟨nodup_of_le (le_union_left _ _) h, nodup_of_le (le_union_right _ _) h⟩,
λ ⟨h₁, h₂⟩, nodup_iff_count_le_one.2 $ λ a, by rw [count_union]; exact
max_le (nodup_iff_count_le_one.1 h₁ a) (nodup_iff_count_le_one.1 h₂ a)⟩
@[simp] theorem nodup_powerset {s : multiset α} : nodup (powerset s) ↔ nodup s :=
⟨λ h, nodup_of_nodup_map _ (nodup_of_le (map_single_le_powerset _) h),
quotient.induction_on s $ λ l h,
by simp; refine list.nodup_map_on _ (nodup_sublists'.2 h); exact
λ x sx y sy e,
(perm_ext_sublist_nodup h (mem_sublists'.1 sx) (mem_sublists'.1 sy)).1
(quotient.exact e)⟩
@[simp] lemma nodup_bind {s : multiset α} {t : α → multiset β} :
nodup (bind s t) ↔ ((∀a∈s, nodup (t a)) ∧ (s.pairwise (λa b, disjoint (t a) (t b)))) :=
have h₁ : ∀a, ∃l:list β, t a = l, from
assume a, quot.induction_on (t a) $ assume l, ⟨l, rfl⟩,
let ⟨t', h'⟩ := classical.axiom_of_choice h₁ in
have t = λa, t' a, from funext h',
have hd : symmetric (λa b, list.disjoint (t' a) (t' b)), from assume a b h, h.symm,
quot.induction_on s $ by simp [this, list.nodup_bind, pairwise_coe_iff_pairwise hd]
theorem nodup_ext {s t : multiset α} : nodup s → nodup t → (s = t ↔ ∀ a, a ∈ s ↔ a ∈ t) :=
quotient.induction_on₂ s t $ λ l₁ l₂ d₁ d₂, quotient.eq.trans $ perm_ext d₁ d₂
theorem le_iff_subset {s t : multiset α} : nodup s → (s ≤ t ↔ s ⊆ t) :=
quotient.induction_on₂ s t $ λ l₁ l₂ d, ⟨subset_of_le, subperm_of_subset_nodup d⟩
theorem range_le {m n : ℕ} : range m ≤ range n ↔ m ≤ n :=
(le_iff_subset (nodup_range _)).trans range_subset
theorem mem_sub_of_nodup [decidable_eq α] {a : α} {s t : multiset α} (d : nodup s) :
a ∈ s - t ↔ a ∈ s ∧ a ∉ t :=
⟨λ h, ⟨mem_of_le (sub_le_self _ _) h, λ h',
by refine count_eq_zero.1 _ h; rw [count_sub a s t, nat.sub_eq_zero_iff_le];
exact le_trans (nodup_iff_count_le_one.1 d _) (count_pos.2 h')⟩,
λ ⟨h₁, h₂⟩, or.resolve_right (mem_add.1 $ mem_of_le (le_sub_add _ _) h₁) h₂⟩
section
variable [decidable_eq α]
/- erase_dup -/
/-- `erase_dup s` removes duplicates from `s`, yielding a `nodup` multiset. -/
def erase_dup (s : multiset α) : multiset α :=
quot.lift_on s (λ l, (l.erase_dup : multiset α))
(λ s t p, quot.sound (perm_erase_dup_of_perm p))
@[simp] theorem coe_erase_dup (l : list α) : @erase_dup α _ l = l.erase_dup := rfl
@[simp] theorem erase_dup_zero : @erase_dup α _ 0 = 0 := rfl
@[simp] theorem mem_erase_dup {a : α} {s : multiset α} : a ∈ erase_dup s ↔ a ∈ s :=
quot.induction_on s $ λ l, mem_erase_dup
@[simp] theorem erase_dup_cons_of_mem {a : α} {s : multiset α} : a ∈ s →
erase_dup (a::s) = erase_dup s :=
quot.induction_on s $ λ l m, @congr_arg _ _ _ _ coe $ erase_dup_cons_of_mem m
@[simp] theorem erase_dup_cons_of_not_mem {a : α} {s : multiset α} : a ∉ s →
erase_dup (a::s) = a :: erase_dup s :=
quot.induction_on s $ λ l m, congr_arg coe $ erase_dup_cons_of_not_mem m
theorem erase_dup_le (s : multiset α) : erase_dup s ≤ s :=
quot.induction_on s $ λ l, subperm_of_sublist $ erase_dup_sublist _
theorem erase_dup_subset (s : multiset α) : erase_dup s ⊆ s :=
subset_of_le $ erase_dup_le _
theorem subset_erase_dup (s : multiset α) : s ⊆ erase_dup s :=
λ a, mem_erase_dup.2
@[simp] theorem erase_dup_subset' {s t : multiset α} : erase_dup s ⊆ t ↔ s ⊆ t :=
⟨subset.trans (subset_erase_dup _), subset.trans (erase_dup_subset _)⟩
@[simp] theorem subset_erase_dup' {s t : multiset α} : s ⊆ erase_dup t ↔ s ⊆ t :=
⟨λ h, subset.trans h (erase_dup_subset _), λ h, subset.trans h (subset_erase_dup _)⟩
@[simp] theorem nodup_erase_dup (s : multiset α) : nodup (erase_dup s) :=
quot.induction_on s nodup_erase_dup
theorem erase_dup_eq_self {s : multiset α} : erase_dup s = s ↔ nodup s :=
⟨λ e, e ▸ nodup_erase_dup s,
quot.induction_on s $ λ l h, congr_arg coe $ erase_dup_eq_self.2 h⟩
@[simp] theorem erase_dup_singleton {a : α} : erase_dup (a :: 0) = a :: 0 :=
erase_dup_eq_self.2 $ nodup_singleton _
theorem le_erase_dup {s t : multiset α} : s ≤ erase_dup t ↔ s ≤ t ∧ nodup s :=
⟨λ h, ⟨le_trans h (erase_dup_le _), nodup_of_le h (nodup_erase_dup _)⟩,
λ ⟨l, d⟩, (le_iff_subset d).2 $ subset.trans (subset_of_le l) (subset_erase_dup _)⟩
theorem erase_dup_ext {s t : multiset α} : erase_dup s = erase_dup t ↔ ∀ a, a ∈ s ↔ a ∈ t :=
by simp [nodup_ext]
theorem erase_dup_map_erase_dup_eq [decidable_eq β] (f : α → β) (s : multiset α) :
erase_dup (map f (erase_dup s)) = erase_dup (map f s) := by simp [erase_dup_ext]
/- finset insert -/
/-- `ndinsert a s` is the lift of the list `insert` operation. This operation
does not respect multiplicities, unlike `cons`, but it is suitable as
an insert operation on `finset`. -/
def ndinsert (a : α) (s : multiset α) : multiset α :=
quot.lift_on s (λ l, (l.insert a : multiset α))
(λ s t p, quot.sound (perm_insert a p))
@[simp] theorem coe_ndinsert (a : α) (l : list α) : ndinsert a l = (insert a l : list α) := rfl
@[simp] theorem ndinsert_zero (a : α) : ndinsert a 0 = a::0 := rfl
@[simp] theorem ndinsert_of_mem {a : α} {s : multiset α} : a ∈ s → ndinsert a s = s :=
quot.induction_on s $ λ l h, congr_arg coe $ insert_of_mem h
@[simp] theorem ndinsert_of_not_mem {a : α} {s : multiset α} : a ∉ s → ndinsert a s = a :: s :=
quot.induction_on s $ λ l h, congr_arg coe $ insert_of_not_mem h
@[simp] theorem mem_ndinsert {a b : α} {s : multiset α} : a ∈ ndinsert b s ↔ a = b ∨ a ∈ s :=
quot.induction_on s $ λ l, mem_insert_iff
@[simp] theorem le_ndinsert_self (a : α) (s : multiset α) : s ≤ ndinsert a s :=
quot.induction_on s $ λ l, subperm_of_sublist $ sublist_of_suffix $ suffix_insert _ _
@[simp] theorem mem_ndinsert_self (a : α) (s : multiset α) : a ∈ ndinsert a s :=
mem_ndinsert.2 (or.inl rfl)
@[simp] theorem mem_ndinsert_of_mem {a b : α} {s : multiset α} (h : a ∈ s) : a ∈ ndinsert b s :=
mem_ndinsert.2 (or.inr h)
@[simp] theorem length_ndinsert_of_mem {a : α} [decidable_eq α] {s : multiset α} (h : a ∈ s) :
card (ndinsert a s) = card s :=
by simp [h]
@[simp] theorem length_ndinsert_of_not_mem {a : α} [decidable_eq α] {s : multiset α} (h : a ∉ s) :
card (ndinsert a s) = card s + 1 :=
by simp [h]
theorem erase_dup_cons {a : α} {s : multiset α} :
erase_dup (a::s) = ndinsert a (erase_dup s) :=
by by_cases a ∈ s; simp [h]
theorem nodup_ndinsert (a : α) {s : multiset α} : nodup s → nodup (ndinsert a s) :=
quot.induction_on s $ λ l, nodup_insert
theorem ndinsert_le {a : α} {s t : multiset α} : ndinsert a s ≤ t ↔ s ≤ t ∧ a ∈ t :=
⟨λ h, ⟨le_trans (le_ndinsert_self _ _) h, mem_of_le h (mem_ndinsert_self _ _)⟩,
λ ⟨l, m⟩, if h : a ∈ s then by simp [h, l] else
by rw [ndinsert_of_not_mem h, ← cons_erase m, cons_le_cons_iff,
← le_cons_of_not_mem h, cons_erase m]; exact l⟩
lemma attach_ndinsert (a : α) (s : multiset α) :
(s.ndinsert a).attach =
ndinsert ⟨a, mem_ndinsert_self a s⟩ (s.attach.map $ λp, ⟨p.1, mem_ndinsert_of_mem p.2⟩) :=
have eq : ∀h : ∀(p : {x // x ∈ s}), p.1 ∈ s,
(λ (p : {x // x ∈ s}), ⟨p.val, h p⟩ : {x // x ∈ s} → {x // x ∈ s}) = id, from
assume h, funext $ assume p, subtype.eq rfl,
have ∀t (eq : s.ndinsert a = t), t.attach = ndinsert ⟨a, eq ▸ mem_ndinsert_self a s⟩
(s.attach.map $ λp, ⟨p.1, eq ▸ mem_ndinsert_of_mem p.2⟩),
begin
intros t ht,
by_cases a ∈ s,
{ rw [ndinsert_of_mem h] at ht,
subst ht,
rw [eq, map_id, ndinsert_of_mem (mem_attach _ _)] },
{ rw [ndinsert_of_not_mem h] at ht,
subst ht,
simp [attach_cons, h] }
end,
this _ rfl
@[simp] theorem disjoint_ndinsert_left {a : α} {s t : multiset α} :
disjoint (ndinsert a s) t ↔ a ∉ t ∧ disjoint s t :=
iff.trans (by simp [disjoint]) disjoint_cons_left
@[simp] theorem disjoint_ndinsert_right {a : α} {s t : multiset α} :
disjoint s (ndinsert a t) ↔ a ∉ s ∧ disjoint s t :=
disjoint_comm.trans $ by simp
/- finset union -/
/-- `ndunion s t` is the lift of the list `union` operation. This operation
does not respect multiplicities, unlike `s ∪ t`, but it is suitable as
a union operation on `finset`. (`s ∪ t` would also work as a union operation
on finset, but this is more efficient.) -/
def ndunion (s t : multiset α) : multiset α :=
quotient.lift_on₂ s t (λ l₁ l₂, (l₁.union l₂ : multiset α)) $ λ v₁ v₂ w₁ w₂ p₁ p₂,
quot.sound $ perm_union p₁ p₂
@[simp] theorem coe_ndunion (l₁ l₂ : list α) : @ndunion α _ l₁ l₂ = (l₁ ∪ l₂ : list α) := rfl
@[simp] theorem zero_ndunion (s : multiset α) : ndunion 0 s = s :=
quot.induction_on s $ λ l, rfl
@[simp] theorem cons_ndunion (s t : multiset α) (a : α) : ndunion (a :: s) t = ndinsert a (ndunion s t) :=
quotient.induction_on₂ s t $ λ l₁ l₂, rfl
@[simp] theorem mem_ndunion {s t : multiset α} {a : α} : a ∈ ndunion s t ↔ a ∈ s ∨ a ∈ t :=
quotient.induction_on₂ s t $ λ l₁ l₂, list.mem_union
theorem le_ndunion_right (s t : multiset α) : t ≤ ndunion s t :=
quotient.induction_on₂ s t $ λ l₁ l₂,
subperm_of_sublist $ sublist_of_suffix $ suffix_union_right _ _
theorem ndunion_le_add (s t : multiset α) : ndunion s t ≤ s + t :=
quotient.induction_on₂ s t $ λ l₁ l₂, subperm_of_sublist $ union_sublist_append _ _
theorem ndunion_le {s t u : multiset α} : ndunion s t ≤ u ↔ s ⊆ u ∧ t ≤ u :=
multiset.induction_on s (by simp) (by simp [ndinsert_le, and_comm, and.left_comm] {contextual := tt})
theorem subset_ndunion_left (s t : multiset α) : s ⊆ ndunion s t :=
λ a h, mem_ndunion.2 $ or.inl h
theorem le_ndunion_left {s} (t : multiset α) (d : nodup s) : s ≤ ndunion s t :=
(le_iff_subset d).2 $ subset_ndunion_left _ _
theorem ndunion_le_union (s t : multiset α) : ndunion s t ≤ s ∪ t :=
ndunion_le.2 ⟨subset_of_le (le_union_left _ _), le_union_right _ _⟩
theorem nodup_ndunion (s : multiset α) {t : multiset α} : nodup t → nodup (ndunion s t) :=
quotient.induction_on₂ s t $ λ l₁ l₂, list.nodup_union _
@[simp] theorem ndunion_eq_union {s t : multiset α} (d : nodup s) : ndunion s t = s ∪ t :=
le_antisymm (ndunion_le_union _ _) $ union_le (le_ndunion_left _ d) (le_ndunion_right _ _)
theorem erase_dup_add (s t : multiset α) : erase_dup (s + t) = ndunion s (erase_dup t) :=
quotient.induction_on₂ s t $ λ l₁ l₂, congr_arg coe $ erase_dup_append _ _
/- finset inter -/
/-- `ndinter s t` is the lift of the list `∩` operation. This operation
does not respect multiplicities, unlike `s ∩ t`, but it is suitable as
an intersection operation on `finset`. (`s ∩ t` would also work as a union operation
on finset, but this is more efficient.) -/
def ndinter (s t : multiset α) : multiset α := filter (∈ t) s
@[simp] theorem coe_ndinter (l₁ l₂ : list α) : @ndinter α _ l₁ l₂ = (l₁ ∩ l₂ : list α) := rfl
@[simp] theorem zero_ndinter (s : multiset α) : ndinter 0 s = 0 := rfl
@[simp] theorem cons_ndinter_of_mem {a : α} (s : multiset α) {t : multiset α} (h : a ∈ t) :
ndinter (a::s) t = a :: (ndinter s t) := by simp [ndinter, h]
@[simp] theorem ndinter_cons_of_not_mem {a : α} (s : multiset α) {t : multiset α} (h : a ∉ t) :
ndinter (a::s) t = ndinter s t := by simp [ndinter, h]
@[simp] theorem mem_ndinter {s t : multiset α} {a : α} : a ∈ ndinter s t ↔ a ∈ s ∧ a ∈ t :=
mem_filter
theorem nodup_ndinter {s : multiset α} (t : multiset α) : nodup s → nodup (ndinter s t) :=
nodup_filter _
theorem le_ndinter {s t u : multiset α} : s ≤ ndinter t u ↔ s ≤ t ∧ s ⊆ u :=
by simp [ndinter, le_filter, subset_iff]
theorem ndinter_le_left (s t : multiset α) : ndinter s t ≤ s :=
(le_ndinter.1 (le_refl _)).1
theorem ndinter_subset_right (s t : multiset α) : ndinter s t ⊆ t :=
(le_ndinter.1 (le_refl _)).2
theorem ndinter_le_right {s} (t : multiset α) (d : nodup s) : ndinter s t ≤ t :=
(le_iff_subset $ nodup_ndinter _ d).2 (ndinter_subset_right _ _)
theorem inter_le_ndinter (s t : multiset α) : s ∩ t ≤ ndinter s t :=
le_ndinter.2 ⟨inter_le_left _ _, subset_of_le $ inter_le_right _ _⟩
@[simp] theorem ndinter_eq_inter {s t : multiset α} (d : nodup s) : ndinter s t = s ∩ t :=
le_antisymm (le_inter (ndinter_le_left _ _) (ndinter_le_right _ d)) (inter_le_ndinter _ _)
theorem ndinter_eq_zero_iff_disjoint {s t : multiset α} : ndinter s t = 0 ↔ disjoint s t :=
by rw ← subset_zero; simp [subset_iff, disjoint]
end
/- fold -/
section fold
variables (op : α → α → α) [hc : is_commutative α op] [ha : is_associative α op]
local notation a * b := op a b
include hc ha
/-- `fold op b s` folds a commutative associative operation `op` over
the multiset `s`. -/
def fold : α → multiset α → α := foldr op (left_comm _ hc.comm ha.assoc)
theorem fold_eq_foldr (b : α) (s : multiset α) : fold op b s = foldr op (left_comm _ hc.comm ha.assoc) b s := rfl
@[simp] theorem coe_fold_r (b : α) (l : list α) : fold op b l = l.foldr op b := rfl
theorem coe_fold_l (b : α) (l : list α) : fold op b l = l.foldl op b :=
(coe_foldr_swap op _ b l).trans $ by simp [hc.comm]
theorem fold_eq_foldl (b : α) (s : multiset α) : fold op b s = foldl op (right_comm _ hc.comm ha.assoc) b s :=
quot.induction_on s $ λ l, coe_fold_l _ _ _
@[simp] theorem fold_zero (b : α) : (0 : multiset α).fold op b = b := rfl
@[simp] theorem fold_cons_left : ∀ (b a : α) (s : multiset α),
(a :: s).fold op b = a * s.fold op b := foldr_cons _ _
theorem fold_cons_right (b a : α) (s : multiset α) : (a :: s).fold op b = s.fold op b * a :=
by simp [hc.comm]
theorem fold_cons'_right (b a : α) (s : multiset α) : (a :: s).fold op b = s.fold op (b * a) :=
by rw [fold_eq_foldl, foldl_cons, ← fold_eq_foldl]
theorem fold_cons'_left (b a : α) (s : multiset α) : (a :: s).fold op b = s.fold op (a * b) :=
by rw [fold_cons'_right, hc.comm]
theorem fold_add (b₁ b₂ : α) (s₁ s₂ : multiset α) : (s₁ + s₂).fold op (b₁ * b₂) = s₁.fold op b₁ * s₂.fold op b₂ :=
multiset.induction_on s₂
(by rw [add_zero, fold_zero, ← fold_cons'_right, ← fold_cons_right op])
(by simp {contextual := tt}; cc)
theorem fold_singleton (b a : α) : (a::0 : multiset α).fold op b = a * b := by simp
theorem fold_distrib {f g : β → α} (u₁ u₂ : α) (s : multiset β) :
(s.map (λx, f x * g x)).fold op (u₁ * u₂) = (s.map f).fold op u₁ * (s.map g).fold op u₂ :=
multiset.induction_on s (by simp) (by simp {contextual := tt}; cc)
theorem fold_hom {op' : β → β → β} [is_commutative β op'] [is_associative β op']
{m : α → β} (hm : ∀x y, m (op x y) = op' (m x) (m y)) (b : α) (s : multiset α) :
(s.map m).fold op' (m b) = m (s.fold op b) :=
multiset.induction_on s (by simp) (by simp [hm] {contextual := tt})
theorem fold_union_inter [decidable_eq α] (s₁ s₂ : multiset α) (b₁ b₂ : α) :
(s₁ ∪ s₂).fold op b₁ * (s₁ ∩ s₂).fold op b₂ = s₁.fold op b₁ * s₂.fold op b₂ :=
by rw [← fold_add op, union_add_inter, fold_add op]
@[simp] theorem fold_erase_dup_idem [decidable_eq α] [hi : is_idempotent α op] (s : multiset α) (b : α) :
(erase_dup s).fold op b = s.fold op b :=
multiset.induction_on s (by simp) $ λ a s IH, begin
by_cases a ∈ s; simp [IH, h],
show fold op b s = op a (fold op b s),
rw [← cons_erase h, fold_cons_left, ← ha.assoc, hi.idempotent],
end
end fold
theorem le_smul_erase_dup [decidable_eq α] (s : multiset α) :
∃ n : ℕ, s ≤ n • erase_dup s :=
⟨(s.map (λ a, count a s)).fold max 0, le_iff_count.2 $ λ a, begin
rw count_smul, by_cases a ∈ s,
{ refine le_trans _ (mul_le_mul_left _ $ count_pos.2 $ mem_erase_dup.2 h),
have : count a s ≤ fold max 0 (map (λ a, count a s) (a :: erase s a));
[simp [le_max_left], simpa [cons_erase h]] },
{ simp [count_eq_zero.2 h, nat.zero_le] }
end⟩
section sup
variables [semilattice_sup_bot α]
/-- Supremum of a multiset: `sup {a, b, c} = a ⊔ b ⊔ c` -/
def sup (s : multiset α) : α := s.fold (⊔) ⊥
@[simp] lemma sup_zero : (0 : multiset α).sup = ⊥ :=
fold_zero _ _
@[simp] lemma sup_cons (a : α) (s : multiset α) :
(a :: s).sup = a ⊔ s.sup :=
fold_cons_left _ _ _ _
@[simp] lemma sup_singleton {a : α} : (a::0).sup = a := by simp
@[simp] lemma sup_add (s₁ s₂ : multiset α) : (s₁ + s₂).sup = s₁.sup ⊔ s₂.sup :=
eq.trans (by simp [sup]) (fold_add _ _ _ _ _)
variables [decidable_eq α]
@[simp] lemma sup_erase_dup (s : multiset α) : (erase_dup s).sup = s.sup :=
fold_erase_dup_idem _ _ _
@[simp] lemma sup_ndunion (s₁ s₂ : multiset α) :
(ndunion s₁ s₂).sup = s₁.sup ⊔ s₂.sup :=
by rw [← sup_erase_dup, erase_dup_ext.2, sup_erase_dup, sup_add]; simp
@[simp] lemma sup_union (s₁ s₂ : multiset α) :
(s₁ ∪ s₂).sup = s₁.sup ⊔ s₂.sup :=
by rw [← sup_erase_dup, erase_dup_ext.2, sup_erase_dup, sup_add]; simp
@[simp] lemma sup_ndinsert (a : α) (s : multiset α) :
(ndinsert a s).sup = a ⊔ s.sup :=
by rw [← sup_erase_dup, erase_dup_ext.2, sup_erase_dup, sup_cons]; simp
lemma sup_le {s : multiset α} {a : α} : s.sup ≤ a ↔ (∀b ∈ s, b ≤ a) :=
multiset.induction_on s (by simp)
(by simp [or_imp_distrib, forall_and_distrib] {contextual := tt})
lemma le_sup {s : multiset α} {a : α} (h : a ∈ s) : a ≤ s.sup :=
sup_le.1 (le_refl _) _ h
lemma sup_mono {s₁ s₂ : multiset α} (h : s₁ ⊆ s₂) : s₁.sup ≤ s₂.sup :=
sup_le.2 $ assume b hb, le_sup (h hb)
end sup
section inf
variables [semilattice_inf_top α]
/-- Infimum of a multiset: `inf {a, b, c} = a ⊓ b ⊓ c` -/
def inf (s : multiset α) : α := s.fold (⊓) ⊤
@[simp] lemma inf_zero : (0 : multiset α).inf = ⊤ :=
fold_zero _ _
@[simp] lemma inf_cons (a : α) (s : multiset α) :
(a :: s).inf = a ⊓ s.inf :=
fold_cons_left _ _ _ _
@[simp] lemma inf_singleton {a : α} : (a::0).inf = a := by simp
@[simp] lemma inf_add (s₁ s₂ : multiset α) : (s₁ + s₂).inf = s₁.inf ⊓ s₂.inf :=
eq.trans (by simp [inf]) (fold_add _ _ _ _ _)
variables [decidable_eq α]
@[simp] lemma inf_erase_dup (s : multiset α) : (erase_dup s).inf = s.inf :=
fold_erase_dup_idem _ _ _
@[simp] lemma inf_ndunion (s₁ s₂ : multiset α) :
(ndunion s₁ s₂).inf = s₁.inf ⊓ s₂.inf :=
by rw [← inf_erase_dup, erase_dup_ext.2, inf_erase_dup, inf_add]; simp
@[simp] lemma inf_union (s₁ s₂ : multiset α) :
(s₁ ∪ s₂).inf = s₁.inf ⊓ s₂.inf :=
by rw [← inf_erase_dup, erase_dup_ext.2, inf_erase_dup, inf_add]; simp
@[simp] lemma inf_ndinsert (a : α) (s : multiset α) :
(ndinsert a s).inf = a ⊓ s.inf :=
by rw [← inf_erase_dup, erase_dup_ext.2, inf_erase_dup, inf_cons]; simp
lemma le_inf {s : multiset α} {a : α} : a ≤ s.inf ↔ (∀b ∈ s, a ≤ b) :=
multiset.induction_on s (by simp)
(by simp [or_imp_distrib, forall_and_distrib] {contextual := tt})
lemma inf_le {s : multiset α} {a : α} (h : a ∈ s) : s.inf ≤ a :=
le_inf.1 (le_refl _) _ h
lemma inf_mono {s₁ s₂ : multiset α} (h : s₁ ⊆ s₂) : s₂.inf ≤ s₁.inf :=
le_inf.2 $ assume b hb, inf_le (h hb)
end inf
section sort
variables (r : α → α → Prop) [decidable_rel r]
[is_trans α r] [is_antisymm α r] [is_total α r]
/-- `sort s` constructs a sorted list from the multiset `s`.
(Uses merge sort algorithm.) -/
def sort (s : multiset α) : list α :=
quot.lift_on s (merge_sort r) $ λ a b h,
eq_of_sorted_of_perm
((perm_merge_sort _ _).trans $ h.trans (perm_merge_sort _ _).symm)
(sorted_merge_sort r _)
(sorted_merge_sort r _)
@[simp] theorem coe_sort (l : list α) : sort r l = merge_sort r l := rfl
@[simp] theorem sort_sorted (s : multiset α) : sorted r (sort r s) :=
quot.induction_on s $ λ l, sorted_merge_sort r _
@[simp] theorem sort_eq (s : multiset α) : ↑(sort r s) = s :=
quot.induction_on s $ λ l, quot.sound $ perm_merge_sort _ _
@[simp] theorem mem_sort {s : multiset α} {a : α} : a ∈ sort r s ↔ a ∈ s :=
by rw [← mem_coe, sort_eq]
end sort
instance [has_repr α] : has_repr (multiset α) :=
⟨λ s, "{" ++ string.intercalate ", " ((s.map repr).sort (≤)) ++ "}"⟩
section sections
def sections (s : multiset (multiset α)) : multiset (multiset α) :=
multiset.rec_on s {0} (λs _ c, s.bind $ λa, c.map ((::) a))
(assume a₀ a₁ s pi, by simp [map_bind, bind_bind a₀ a₁, cons_swap])
@[simp] lemma sections_zero : sections (0 : multiset (multiset α)) = 0::0 :=
rfl
@[simp] lemma sections_cons (s : multiset (multiset α)) (m : multiset α) :
sections (m :: s) = m.bind (λa, (sections s).map ((::) a)) :=
rec_on_cons m s
lemma coe_sections : ∀(l : list (list α)),
sections ((l.map (λl:list α, (l : multiset α))) : multiset (multiset α)) =
((l.sections.map (λl:list α, (l : multiset α))) : multiset (multiset α))
| [] := rfl
| (a :: l) :=
begin
simp,
rw [← cons_coe, sections_cons, bind_map_comm, coe_sections l],
simp [list.sections, (∘), list.bind]
end
@[simp] lemma sections_add (s t : multiset (multiset α)) :
sections (s + t) = (sections s).bind (λm, (sections t).map ((+) m)) :=
multiset.induction_on s (by simp)
(assume a s ih, by simp [ih, bind_assoc, map_bind, bind_map, -add_comm])
lemma mem_sections {s : multiset (multiset α)} :
∀{a}, a ∈ sections s ↔ s.rel (λs a, a ∈ s) a :=
multiset.induction_on s (by simp)
(assume a s ih a',
by simp [ih, rel_cons_left, -exists_and_distrib_left, exists_and_distrib_left.symm, eq_comm])
lemma card_sections {s : multiset (multiset α)} : card (sections s) = prod (s.map card) :=
multiset.induction_on s (by simp) (by simp {contextual := tt})
lemma prod_map_sum [comm_semiring α] {s : multiset (multiset α)} :
prod (s.map sum) = sum ((sections s).map prod) :=
multiset.induction_on s (by simp)
(assume a s ih, by simp [ih, map_bind, sum_map_mul_left, sum_map_mul_right])
end sections
section pi
variables [decidable_eq α] {δ : α → Type*}
open function
def pi.cons (m : multiset α) (a : α) (b : δ a) (f : Πa∈m, δ a) : Πa'∈a::m, δ a' :=
λa' ha', if h : a' = a then eq.rec b h.symm else f a' $ (mem_cons.1 ha').resolve_left h
def pi.empty (δ : α → Type*) : (Πa∈(0:multiset α), δ a) .
lemma pi.cons_same {m : multiset α} {a : α} {b : δ a} {f : Πa∈m, δ a} (h : a ∈ a :: m) :
pi.cons m a b f a h = b :=
dif_pos rfl
lemma pi.cons_ne {m : multiset α} {a a' : α} {b : δ a} {f : Πa∈m, δ a} (h' : a' ∈ a :: m) (h : a' ≠ a) :
pi.cons m a b f a' h' = f a' ((mem_cons.1 h').resolve_left h) :=
dif_neg h
lemma pi.cons_swap {a a' : α} {b : δ a} {b' : δ a'} {m : multiset α} {f : Πa∈m, δ a} (h : a ≠ a') :
pi.cons (a' :: m) a b (pi.cons m a' b' f) == pi.cons (a :: m) a' b' (pi.cons m a b f) :=
begin
apply hfunext, { refl }, intros a'' _ h, subst h,
apply hfunext, { rw [cons_swap] }, intros ha₁ ha₂ h,
by_cases h₁ : a'' = a; by_cases h₂ : a'' = a';
simp [*, pi.cons_same, pi.cons_ne] at *,
{ subst h₁, rw [pi.cons_same, pi.cons_same] },
{ subst h₂, rw [pi.cons_same, pi.cons_same] }
end
/-- `pi m t` constructs the Cartesian product over `t` indexed by `m`. -/
def pi (m : multiset α) (t : Πa, multiset (δ a)) : multiset (Πa∈m, δ a) :=
m.rec_on {pi.empty δ} (λa m (p : multiset (Πa∈m, δ a)), (t a).bind $ λb, p.map $ pi.cons m a b)
begin
intros a a' m n,
by_cases eq : a = a',
{ subst eq },
{ simp [map_bind, bind_bind (t a') (t a)],
apply bind_hcongr, { rw [cons_swap a a'] },
intros b hb,
apply bind_hcongr, { rw [cons_swap a a'] },
intros b' hb',
apply map_hcongr, { rw [cons_swap a a'] },
intros f hf,
exact pi.cons_swap eq }
end
@[simp] lemma pi_zero (t : Πa, multiset (δ a)) : pi 0 t = pi.empty δ :: 0 := rfl
@[simp] lemma pi_cons (m : multiset α) (t : Πa, multiset (δ a)) (a : α) :
pi (a :: m) t = ((t a).bind $ λb, (pi m t).map $ pi.cons m a b) :=
rec_on_cons a m
lemma injective_pi_cons {a : α} {b : δ a} {s : multiset α} (hs : a ∉ s) :
function.injective (pi.cons s a b) :=
assume f₁ f₂ eq, funext $ assume a', funext $ assume h',
have ne : a ≠ a', from assume h, hs $ h.symm ▸ h',
have a' ∈ a :: s, from mem_cons_of_mem h',
calc f₁ a' h' = pi.cons s a b f₁ a' this : by rw [pi.cons_ne this ne.symm]
... = pi.cons s a b f₂ a' this : by rw [eq]
... = f₂ a' h' : by rw [pi.cons_ne this ne.symm]
lemma card_pi (m : multiset α) (t : Πa, multiset (δ a)) :
card (pi m t) = prod (m.map $ λa, card (t a)) :=
multiset.induction_on m (by simp) (by simp [mul_comm] {contextual := tt})
lemma nodup_pi {s : multiset α} {t : Πa, multiset (δ a)} :
nodup s → (∀a∈s, nodup (t a)) → nodup (pi s t) :=
multiset.induction_on s (assume _ _, nodup_singleton _)
begin
assume a s ih hs ht,
have has : a ∉ s, by simp at hs; exact hs.1,
have hs : nodup s, by simp at hs; exact hs.2,
simp,
split,
{ assume b hb,
from nodup_map (injective_pi_cons has) (ih hs $ assume a' h', ht a' $ mem_cons_of_mem h') },
{ apply pairwise_of_nodup _ (ht a $ mem_cons_self _ _),
from assume b₁ hb₁ b₂ hb₂ neb, disjoint_map_map.2 (assume f hf g hg eq,
have pi.cons s a b₁ f a (mem_cons_self _ _) = pi.cons s a b₂ g a (mem_cons_self _ _),
by rw [eq],
neb $ show b₁ = b₂, by rwa [pi.cons_same, pi.cons_same] at this) }
end
lemma mem_pi (m : multiset α) (t : Πa, multiset (δ a)) :
∀f:Πa∈m, δ a, (f ∈ pi m t) ↔ (∀a (h : a ∈ m), f a h ∈ t a) :=
begin
refine multiset.induction_on m (λ f, _) (λ a m ih f, _),
{ simpa using show f = pi.empty δ, by funext a ha; exact ha.elim },
simp, split,
{ rintro ⟨b, hb, f', hf', rfl⟩ a' ha',
rw [ih] at hf',
by_cases a' = a,
{ subst h, rwa [pi.cons_same] },
{ rw [pi.cons_ne _ h], apply hf' } },
{ intro hf,
refine ⟨_, hf a (mem_cons_self a _), λa ha, f a (mem_cons_of_mem ha),
(ih _).2 (λ a' h', hf _ _), _⟩,
funext a' h',
by_cases a' = a,
{ subst h, rw [pi.cons_same] },
{ rw [pi.cons_ne _ h] } }
end
end pi
end multiset
namespace multiset
instance : functor multiset :=
{ map := @map }
instance : is_lawful_functor multiset :=
by refine { .. }; intros; simp
open is_lawful_traversable is_comm_applicative
variables {F : Type u_1 → Type u_1} [applicative F] [is_comm_applicative F]
variables {α' β' : Type u_1} (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.skip {
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
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
(by { intro, rw [traverse,quotient.lift_beta,function.comp],
simp, congr })
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
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.