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
f7b76a185443acffe2c2285adc09c037423f358e
30b012bb72d640ec30c8fdd4c45fdfa67beb012c
/data/equiv/basic.lean
f88dd9e5bef5c82947ebc7262927fea3e3b14f9d
[ "Apache-2.0" ]
permissive
kckennylau/mathlib
21fb810b701b10d6606d9002a4004f7672262e83
47b3477e20ffb5a06588dd3abb01fe0fe3205646
refs/heads/master
1,634,976,409,281
1,542,042,832,000
1,542,319,733,000
109,560,458
0
0
Apache-2.0
1,542,369,208,000
1,509,867,494,000
Lean
UTF-8
Lean
false
false
29,913
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, Mario Carneiro In the standard library we cannot assume the univalence axiom. We say two types are equivalent if they are isomorphic. Two equivalent types have the same cardinality. -/ import logic.function data.set.basic open function universes u v w variables {α : Sort u} {β : Sort v} {γ : Sort w} /-- `α ≃ β` is the type of functions from `α → β` with a two-sided inverse. -/ structure equiv (α : Sort*) (β : Sort*) := (to_fun : α → β) (inv_fun : β → α) (left_inv : left_inverse inv_fun to_fun) (right_inv : right_inverse inv_fun to_fun) namespace equiv /-- `perm α` is the type of bijections from `α` to itself. -/ @[reducible] def perm (α : Sort*) := equiv α α infix ` ≃ `:50 := equiv instance : has_coe_to_fun (α ≃ β) := ⟨_, to_fun⟩ @[simp] theorem coe_fn_mk (f : α → β) (g l r) : (equiv.mk f g l r : α → β) = f := rfl theorem eq_of_to_fun_eq : ∀ {e₁ e₂ : equiv α β}, (e₁ : α → β) = e₂ → e₁ = e₂ | ⟨f₁, g₁, l₁, r₁⟩ ⟨f₂, g₂, l₂, r₂⟩ h := have f₁ = f₂, from h, have g₁ = g₂, from funext $ assume x, have f₁ (g₁ x) = f₂ (g₂ x), from (r₁ x).trans (r₂ x).symm, have f₁ (g₁ x) = f₁ (g₂ x), by subst f₂; exact this, show g₁ x = g₂ x, from injective_of_left_inverse l₁ this, by simp * @[extensionality] lemma ext (f g : equiv α β) (H : ∀ x, f x = g x) : f = g := eq_of_to_fun_eq (funext H) @[extensionality] lemma perm.ext (σ τ : equiv.perm α) (H : ∀ x, σ x = τ x) : σ = τ := equiv.ext _ _ H @[refl] protected def refl (α : Sort*) : α ≃ α := ⟨id, id, λ x, rfl, λ x, rfl⟩ @[symm] protected def symm (e : α ≃ β) : β ≃ α := ⟨e.inv_fun, e.to_fun, e.right_inv, e.left_inv⟩ @[trans] protected def trans (e₁ : α ≃ β) (e₂ : β ≃ γ) : α ≃ γ := ⟨e₂.to_fun ∘ e₁.to_fun, e₁.inv_fun ∘ e₂.inv_fun, e₂.left_inv.comp e₁.left_inv, e₂.right_inv.comp e₁.right_inv⟩ protected theorem bijective : ∀ f : α ≃ β, bijective f | ⟨f, g, h₁, h₂⟩ := ⟨injective_of_left_inverse h₁, surjective_of_has_right_inverse ⟨_, h₂⟩⟩ protected theorem subsingleton (e : α ≃ β) : ∀ [subsingleton β], subsingleton α | ⟨H⟩ := ⟨λ a b, e.bijective.1 (H _ _)⟩ protected def decidable_eq (e : α ≃ β) [H : decidable_eq β] : decidable_eq α | a b := decidable_of_iff _ e.bijective.1.eq_iff protected def cast {α β : Sort*} (h : α = β) : α ≃ β := ⟨cast h, cast h.symm, λ x, by cases h; refl, λ x, by cases h; refl⟩ @[simp] theorem coe_fn_symm_mk (f : α → β) (g l r) : ((equiv.mk f g l r).symm : β → α) = g := rfl @[simp] theorem refl_apply (x : α) : equiv.refl α x = x := rfl @[simp] theorem trans_apply (f : α ≃ β) (g : β ≃ γ) (a : α) : (f.trans g) a = g (f a) := rfl @[simp] theorem apply_inverse_apply : ∀ (e : α ≃ β) (x : β), e (e.symm x) = x | ⟨f₁, g₁, l₁, r₁⟩ x := by simp [equiv.symm]; rw r₁ @[simp] theorem inverse_apply_apply : ∀ (e : α ≃ β) (x : α), e.symm (e x) = x | ⟨f₁, g₁, l₁, r₁⟩ x := by simp [equiv.symm]; rw l₁ @[simp] lemma inverse_trans_apply (f : α ≃ β) (g : β ≃ γ) (a : γ) : (f.trans g).symm a = f.symm (g.symm a) := rfl @[simp] theorem apply_eq_iff_eq : ∀ (f : α ≃ β) (x y : α), f x = f y ↔ x = y | ⟨f₁, g₁, l₁, r₁⟩ x y := (injective_of_left_inverse l₁).eq_iff @[simp] theorem cast_apply {α β} (h : α = β) (x : α) : equiv.cast h x = cast h x := rfl lemma symm_apply_eq {α β} (e : α ≃ β) {x y} : e.symm x = y ↔ x = e y := ⟨λ H, by simp [H.symm], λ H, by simp [H]⟩ lemma eq_symm_apply {α β} (e : α ≃ β) {x y} : y = e.symm x ↔ e y = x := (eq_comm.trans e.symm_apply_eq).trans eq_comm @[simp] theorem symm_symm (e : α ≃ β) : e.symm.symm = e := by cases e; refl @[simp] theorem trans_refl (e : α ≃ β) : e.trans (equiv.refl β) = e := by cases e; refl @[simp] theorem refl_trans (e : α ≃ β) : (equiv.refl α).trans e = e := by cases e; refl @[simp] theorem symm_trans (e : α ≃ β) : e.symm.trans e = equiv.refl β := ext _ _ (by simp) @[simp] theorem trans_symm (e : α ≃ β) : e.trans e.symm = equiv.refl α := ext _ _ (by simp) lemma trans_assoc {δ} (ab : α ≃ β) (bc : β ≃ γ) (cd : γ ≃ δ) : (ab.trans bc).trans cd = ab.trans (bc.trans cd) := equiv.ext _ _ $ assume a, rfl theorem left_inverse_symm (f : equiv α β) : left_inverse f.symm f := f.left_inv theorem right_inverse_symm (f : equiv α β) : function.right_inverse f.symm f := f.right_inv def equiv_congr {δ} (ab : α ≃ β) (cd : γ ≃ δ) : (α ≃ γ) ≃ (β ≃ δ) := ⟨ λac, (ab.symm.trans ac).trans cd, λbd, ab.trans $ bd.trans $ cd.symm, assume ac, begin simp [trans_assoc], rw [← trans_assoc], simp end, assume ac, begin simp [trans_assoc], rw [← trans_assoc], simp end, ⟩ def perm_congr {α : Type*} {β : Type*} (e : α ≃ β) : perm α ≃ perm β := equiv_congr e e protected lemma image_eq_preimage {α β} (e : α ≃ β) (s : set α) : e '' s = e.symm ⁻¹' s := set.ext $ assume x, set.mem_image_iff_of_inverse e.left_inv e.right_inv protected lemma subset_image {α β} (e : α ≃ β) (s : set α) (t : set β) : t ⊆ e '' s ↔ e.symm '' t ⊆ s := by rw [set.image_subset_iff, e.image_eq_preimage] lemma symm_image_image {α β} (f : equiv α β) (s : set α) : f.symm '' (f '' s) = s := by rw [← set.image_comp]; simpa using set.image_id s protected lemma image_compl {α β} (f : equiv α β) (s : set α) : f '' -s = -(f '' s) := set.image_compl_eq f.bijective /- The group of permutations (self-equivalences) of a type `α` -/ namespace perm instance perm_group {α : Type u} : group (perm α) := begin refine { mul := λ f g, equiv.trans g f, one := equiv.refl α, inv:= equiv.symm, ..}; intros; apply equiv.ext; try { apply trans_apply }, apply inverse_apply_apply end @[simp] theorem mul_apply {α : Type u} (f g : perm α) (x) : (f * g) x = f (g x) := equiv.trans_apply _ _ _ @[simp] theorem one_apply {α : Type u} (x) : (1 : perm α) x = x := rfl @[simp] lemma inv_apply_self {α : Type u} (f : perm α) (x) : f⁻¹ (f x) = x := equiv.inverse_apply_apply _ _ @[simp] lemma apply_inv_self {α : Type u} (f : perm α) (x) : f (f⁻¹ x) = x := equiv.apply_inverse_apply _ _ lemma one_def {α : Type u} : (1 : perm α) = equiv.refl α := rfl lemma mul_def {α : Type u} (f g : perm α) : f * g = g.trans f := rfl lemma inv_def {α : Type u} (f : perm α) : f⁻¹ = f.symm := rfl end perm def equiv_empty (h : α → false) : α ≃ empty := ⟨λ x, (h x).elim, λ e, e.rec _, λ x, (h x).elim, λ e, e.rec _⟩ def false_equiv_empty : false ≃ empty := equiv_empty _root_.id def equiv_pempty (h : α → false) : α ≃ pempty := ⟨λ x, (h x).elim, λ e, e.rec _, λ x, (h x).elim, λ e, e.rec _⟩ def false_equiv_pempty : false ≃ pempty := equiv_pempty _root_.id def empty_equiv_pempty : empty ≃ pempty := equiv_pempty $ empty.rec _ def pempty_equiv_pempty : pempty.{v} ≃ pempty.{w} := equiv_pempty pempty.elim def empty_of_not_nonempty {α : Sort*} (h : ¬ nonempty α) : α ≃ empty := equiv_empty $ assume a, h ⟨a⟩ def pempty_of_not_nonempty {α : Sort*} (h : ¬ nonempty α) : α ≃ pempty := equiv_pempty $ assume a, h ⟨a⟩ def prop_equiv_punit {p : Prop} (h : p) : p ≃ punit := ⟨λ x, (), λ x, h, λ _, rfl, λ ⟨⟩, rfl⟩ def true_equiv_punit : true ≃ punit := prop_equiv_punit trivial protected def ulift {α : Type u} : ulift α ≃ α := ⟨ulift.down, ulift.up, ulift.up_down, λ a, rfl⟩ protected def plift : plift α ≃ α := ⟨plift.down, plift.up, plift.up_down, plift.down_up⟩ @[congr] def arrow_congr {α₁ β₁ α₂ β₂ : Sort*} : α₁ ≃ α₂ → β₁ ≃ β₂ → (α₁ → β₁) ≃ (α₂ → β₂) | ⟨f₁, g₁, l₁, r₁⟩ ⟨f₂, g₂, l₂, r₂⟩ := ⟨λ (h : α₁ → β₁) (a : α₂), f₂ (h (g₁ a)), λ (h : α₂ → β₂) (a : α₁), g₂ (h (f₁ a)), λ h, by funext a; dsimp; rw [l₁, l₂], λ h, by funext a; dsimp; rw [r₁, r₂]⟩ def punit_equiv_punit : punit.{v} ≃ punit.{w} := ⟨λ _, punit.star, λ _, punit.star, λ u, by cases u; refl, λ u, by cases u; reflexivity⟩ section @[simp] def arrow_punit_equiv_punit (α : Sort*) : (α → punit.{v}) ≃ punit.{w} := ⟨λ f, punit.star, λ u f, punit.star, λ f, by funext x; cases f x; refl, λ u, by cases u; reflexivity⟩ @[simp] def punit_arrow_equiv (α : Sort*) : (punit.{u} → α) ≃ α := ⟨λ f, f punit.star, λ a u, a, λ f, by funext x; cases x; refl, λ u, rfl⟩ @[simp] def empty_arrow_equiv_punit (α : Sort*) : (empty → α) ≃ punit.{u} := ⟨λ f, punit.star, λ u e, e.rec _, λ f, funext $ λ x, x.rec _, λ u, by cases u; refl⟩ @[simp] def pempty_arrow_equiv_punit (α : Sort*) : (pempty → α) ≃ punit.{u} := ⟨λ f, punit.star, λ u e, e.rec _, λ f, funext $ λ x, x.rec _, λ u, by cases u; refl⟩ @[simp] def false_arrow_equiv_punit (α : Sort*) : (false → α) ≃ punit.{u} := calc (false → α) ≃ (empty → α) : arrow_congr false_equiv_empty (equiv.refl _) ... ≃ punit : empty_arrow_equiv_punit _ end @[congr] def prod_congr {α₁ β₁ α₂ β₂ : Sort*} (e₁ : α₁ ≃ α₂) (e₂ :β₁ ≃ β₂) : (α₁ × β₁) ≃ (α₂ × β₂) := ⟨λp, (e₁ p.1, e₂ p.2), λp, (e₁.symm p.1, e₂.symm p.2), λ ⟨a, b⟩, show (e₁.symm (e₁ a), e₂.symm (e₂ b)) = (a, b), by rw [inverse_apply_apply, inverse_apply_apply], λ ⟨a, b⟩, show (e₁ (e₁.symm a), e₂ (e₂.symm b)) = (a, b), by rw [apply_inverse_apply, apply_inverse_apply]⟩ @[simp] theorem prod_congr_apply {α₁ β₁ α₂ β₂ : Sort*} (e₁ : α₁ ≃ α₂) (e₂ : β₁ ≃ β₂) (a : α₁) (b : β₁) : prod_congr e₁ e₂ (a, b) = (e₁ a, e₂ b) := rfl @[simp] def prod_comm (α β : Sort*) : (α × β) ≃ (β × α) := ⟨λ p, (p.2, p.1), λ p, (p.2, p.1), λ⟨a, b⟩, rfl, λ⟨a, b⟩, rfl⟩ @[simp] def prod_assoc (α β γ : Sort*) : ((α × β) × γ) ≃ (α × (β × γ)) := ⟨λ p, ⟨p.1.1, ⟨p.1.2, p.2⟩⟩, λp, ⟨⟨p.1, p.2.1⟩, p.2.2⟩, λ ⟨⟨a, b⟩, c⟩, rfl, λ ⟨a, ⟨b, c⟩⟩, rfl⟩ @[simp] theorem prod_assoc_apply {α β γ : Sort*} (p : (α × β) × γ) : prod_assoc α β γ p = ⟨p.1.1, ⟨p.1.2, p.2⟩⟩ := rfl section @[simp] def prod_punit (α : Sort*) : (α × punit.{u+1}) ≃ α := ⟨λ p, p.1, λ a, (a, punit.star), λ ⟨_, punit.star⟩, rfl, λ a, rfl⟩ @[simp] theorem prod_punit_apply {α : Sort*} (a : α × punit.{u+1}) : prod_punit α a = a.1 := rfl @[simp] def punit_prod (α : Sort*) : (punit.{u+1} × α) ≃ α := calc (punit × α) ≃ (α × punit) : prod_comm _ _ ... ≃ α : prod_punit _ @[simp] theorem punit_prod_apply {α : Sort*} (a : punit.{u+1} × α) : punit_prod α a = a.2 := rfl @[simp] def prod_empty (α : Sort*) : (α × empty) ≃ empty := equiv_empty (λ ⟨_, e⟩, e.rec _) @[simp] def empty_prod (α : Sort*) : (empty × α) ≃ empty := equiv_empty (λ ⟨e, _⟩, e.rec _) @[simp] def prod_pempty (α : Sort*) : (α × pempty) ≃ pempty := equiv_pempty (λ ⟨_, e⟩, e.rec _) @[simp] def pempty_prod (α : Sort*) : (pempty × α) ≃ pempty := equiv_pempty (λ ⟨e, _⟩, e.rec _) end section open sum def psum_equiv_sum (α β : Sort*) : psum α β ≃ (α ⊕ β) := ⟨λ s, psum.cases_on s inl inr, λ s, sum.cases_on s psum.inl psum.inr, λ s, by cases s; refl, λ s, by cases s; refl⟩ def sum_congr {α₁ β₁ α₂ β₂ : Sort*} : α₁ ≃ α₂ → β₁ ≃ β₂ → (α₁ ⊕ β₁) ≃ (α₂ ⊕ β₂) | ⟨f₁, g₁, l₁, r₁⟩ ⟨f₂, g₂, l₂, r₂⟩ := ⟨λ s, match s with inl a₁ := inl (f₁ a₁) | inr b₁ := inr (f₂ b₁) end, λ s, match s with inl a₂ := inl (g₁ a₂) | inr b₂ := inr (g₂ b₂) end, λ s, match s with inl a := congr_arg inl (l₁ a) | inr a := congr_arg inr (l₂ a) end, λ s, match s with inl a := congr_arg inl (r₁ a) | inr a := congr_arg inr (r₂ a) end⟩ @[simp] theorem sum_congr_apply_inl {α₁ β₁ α₂ β₂ : Sort*} (e₁ : α₁ ≃ α₂) (e₂ : β₁ ≃ β₂) (a : α₁) : sum_congr e₁ e₂ (inl a) = inl (e₁ a) := by cases e₁; cases e₂; refl @[simp] theorem sum_congr_apply_inr {α₁ β₁ α₂ β₂ : Sort*} (e₁ : α₁ ≃ α₂) (e₂ : β₁ ≃ β₂) (b : β₁) : sum_congr e₁ e₂ (inr b) = inr (e₂ b) := by cases e₁; cases e₂; refl def bool_equiv_punit_sum_punit : bool ≃ (punit.{u+1} ⊕ punit.{v+1}) := ⟨λ b, cond b (inr punit.star) (inl punit.star), λ s, sum.rec_on s (λ_, ff) (λ_, tt), λ b, by cases b; refl, λ s, by rcases s with ⟨⟨⟩⟩ | ⟨⟨⟩⟩; refl⟩ noncomputable def Prop_equiv_bool : Prop ≃ bool := ⟨λ p, @to_bool p (classical.prop_decidable _), λ b, b, λ p, by simp, λ b, by simp⟩ @[simp] def sum_comm (α β : Sort*) : (α ⊕ β) ≃ (β ⊕ α) := ⟨λ s, match s with inl a := inr a | inr b := inl b end, λ s, match s with inl b := inr b | inr a := inl a end, λ s, by cases s; refl, λ s, by cases s; refl⟩ @[simp] def sum_assoc (α β γ : Sort*) : ((α ⊕ β) ⊕ γ) ≃ (α ⊕ (β ⊕ γ)) := ⟨λ s, match s with inl (inl a) := inl a | inl (inr b) := inr (inl b) | inr c := inr (inr c) end, λ s, match s with inl a := inl (inl a) | inr (inl b) := inl (inr b) | inr (inr c) := inr c end, λ s, by rcases s with ⟨_ | _⟩ | _; refl, λ s, by rcases s with _ | _ | _; refl⟩ @[simp] theorem sum_assoc_apply_in1 {α β γ} (a) : sum_assoc α β γ (inl (inl a)) = inl a := rfl @[simp] theorem sum_assoc_apply_in2 {α β γ} (b) : sum_assoc α β γ (inl (inr b)) = inr (inl b) := rfl @[simp] theorem sum_assoc_apply_in3 {α β γ} (c) : sum_assoc α β γ (inr c) = inr (inr c) := rfl @[simp] def sum_empty (α : Sort*) : (α ⊕ empty) ≃ α := ⟨λ s, match s with inl a := a | inr e := empty.rec _ e end, inl, λ s, by rcases s with _ | ⟨⟨⟩⟩; refl, λ a, rfl⟩ @[simp] def empty_sum (α : Sort*) : (empty ⊕ α) ≃ α := (sum_comm _ _).trans $ sum_empty _ @[simp] def sum_pempty (α : Sort*) : (α ⊕ pempty) ≃ α := ⟨λ s, match s with inl a := a | inr e := pempty.rec _ e end, inl, λ s, by rcases s with _ | ⟨⟨⟩⟩; refl, λ a, rfl⟩ @[simp] def pempty_sum (α : Sort*) : (pempty ⊕ α) ≃ α := (sum_comm _ _).trans $ sum_pempty _ @[simp] def option_equiv_sum_punit (α : Sort*) : option α ≃ (α ⊕ punit.{u+1}) := ⟨λ o, match o with none := inr punit.star | some a := inl a end, λ s, match s with inr _ := none | inl a := some a end, λ o, by cases o; refl, λ s, by rcases s with _ | ⟨⟨⟩⟩; refl⟩ def sum_equiv_sigma_bool (α β : Sort*) : (α ⊕ β) ≃ (Σ b: bool, cond b α β) := ⟨λ s, match s with inl a := ⟨tt, a⟩ | inr b := ⟨ff, b⟩ end, λ s, match s with ⟨tt, a⟩ := inl a | ⟨ff, b⟩ := inr b end, λ s, by cases s; refl, λ s, by rcases s with ⟨_|_, _⟩; refl⟩ def equiv_fib {α β : Type*} (f : α → β) : α ≃ Σ y : β, {x // f x = y} := ⟨λ x, ⟨f x, x, rfl⟩, λ x, x.2.1, λ x, rfl, λ ⟨y, x, rfl⟩, rfl⟩ end section def Pi_congr_right {α} {β₁ β₂ : α → Sort*} (F : ∀ a, β₁ a ≃ β₂ a) : (Π a, β₁ a) ≃ (Π a, β₂ a) := ⟨λ H a, F a (H a), λ H a, (F a).symm (H a), λ H, funext $ by simp, λ H, funext $ by simp⟩ end section def psigma_equiv_sigma {α} (β : α → Sort*) : psigma β ≃ sigma β := ⟨λ ⟨a, b⟩, ⟨a, b⟩, λ ⟨a, b⟩, ⟨a, b⟩, λ ⟨a, b⟩, rfl, λ ⟨a, b⟩, rfl⟩ def sigma_congr_right {α} {β₁ β₂ : α → Sort*} (F : ∀ a, β₁ a ≃ β₂ a) : sigma β₁ ≃ sigma β₂ := ⟨λ ⟨a, b⟩, ⟨a, F a b⟩, λ ⟨a, b⟩, ⟨a, (F a).symm b⟩, λ ⟨a, b⟩, congr_arg (sigma.mk a) $ inverse_apply_apply (F a) b, λ ⟨a, b⟩, congr_arg (sigma.mk a) $ apply_inverse_apply (F a) b⟩ def sigma_congr_left {α₁ α₂} {β : α₂ → Sort*} : ∀ f : α₁ ≃ α₂, (Σ a:α₁, β (f a)) ≃ (Σ a:α₂, β a) | ⟨f, g, l, r⟩ := ⟨λ ⟨a, b⟩, ⟨f a, b⟩, λ ⟨a, b⟩, ⟨g a, @@eq.rec β b (r a).symm⟩, λ ⟨a, b⟩, match g (f a), l a : ∀ a' (h : a' = a), @sigma.mk _ (β ∘ f) _ (@@eq.rec β b (congr_arg f h.symm)) = ⟨a, b⟩ with | _, rfl := rfl end, λ ⟨a, b⟩, match f (g a), _ : ∀ a' (h : a' = a), sigma.mk a' (@@eq.rec β b h.symm) = ⟨a, b⟩ with | _, rfl := rfl end⟩ def sigma_equiv_prod (α β : Sort*) : (Σ_:α, β) ≃ (α × β) := ⟨λ ⟨a, b⟩, ⟨a, b⟩, λ ⟨a, b⟩, ⟨a, b⟩, λ ⟨a, b⟩, rfl, λ ⟨a, b⟩, rfl⟩ def sigma_equiv_prod_of_equiv {α β} {β₁ : α → Sort*} (F : ∀ a, β₁ a ≃ β) : sigma β₁ ≃ (α × β) := (sigma_congr_right F).trans (sigma_equiv_prod α β) end section def arrow_prod_equiv_prod_arrow (α β γ : Type*) : (γ → α × β) ≃ ((γ → α) × (γ → β)) := ⟨λ f, (λ c, (f c).1, λ c, (f c).2), λ p c, (p.1 c, p.2 c), λ f, funext $ λ c, prod.mk.eta, λ p, by cases p; refl⟩ def arrow_arrow_equiv_prod_arrow (α β γ : Sort*) : (α → β → γ) ≃ (α × β → γ) := ⟨λ f, λ p, f p.1 p.2, λ f, λ a b, f (a, b), λ f, rfl, λ f, by funext p; cases p; refl⟩ open sum def sum_arrow_equiv_prod_arrow (α β γ : Type*) : ((α ⊕ β) → γ) ≃ ((α → γ) × (β → γ)) := ⟨λ f, (f ∘ inl, f ∘ inr), λ p s, sum.rec_on s p.1 p.2, λ f, by funext s; cases s; refl, λ p, by cases p; refl⟩ def sum_prod_distrib (α β γ : Sort*) : ((α ⊕ β) × γ) ≃ ((α × γ) ⊕ (β × γ)) := ⟨λ p, match p with (inl a, c) := inl (a, c) | (inr b, c) := inr (b, c) end, λ s, match s with inl (a, c) := (inl a, c) | inr (b, c) := (inr b, c) end, λ p, by rcases p with ⟨_ | _, _⟩; refl, λ s, by rcases s with ⟨_, _⟩ | ⟨_, _⟩; refl⟩ @[simp] theorem sum_prod_distrib_apply_left {α β γ} (a : α) (c : γ) : sum_prod_distrib α β γ (sum.inl a, c) = sum.inl (a, c) := rfl @[simp] theorem sum_prod_distrib_apply_right {α β γ} (b : β) (c : γ) : sum_prod_distrib α β γ (sum.inr b, c) = sum.inr (b, c) := rfl def prod_sum_distrib (α β γ : Sort*) : (α × (β ⊕ γ)) ≃ ((α × β) ⊕ (α × γ)) := calc (α × (β ⊕ γ)) ≃ ((β ⊕ γ) × α) : prod_comm _ _ ... ≃ ((β × α) ⊕ (γ × α)) : sum_prod_distrib _ _ _ ... ≃ ((α × β) ⊕ (α × γ)) : sum_congr (prod_comm _ _) (prod_comm _ _) @[simp] theorem prod_sum_distrib_apply_left {α β γ} (a : α) (b : β) : prod_sum_distrib α β γ (a, sum.inl b) = sum.inl (a, b) := rfl @[simp] theorem prod_sum_distrib_apply_right {α β γ} (a : α) (c : γ) : prod_sum_distrib α β γ (a, sum.inr c) = sum.inr (a, c) := rfl def bool_prod_equiv_sum (α : Type u) : (bool × α) ≃ (α ⊕ α) := calc (bool × α) ≃ ((unit ⊕ unit) × α) : prod_congr bool_equiv_punit_sum_punit (equiv.refl _) ... ≃ (α × (unit ⊕ unit)) : prod_comm _ _ ... ≃ ((α × unit) ⊕ (α × unit)) : prod_sum_distrib _ _ _ ... ≃ (α ⊕ α) : sum_congr (prod_punit _) (prod_punit _) end section open sum nat def nat_equiv_nat_sum_punit : ℕ ≃ (ℕ ⊕ punit.{u+1}) := ⟨λ n, match n with zero := inr punit.star | succ a := inl a end, λ s, match s with inl n := succ n | inr punit.star := zero end, λ n, begin cases n, repeat { refl } end, λ s, begin cases s with a u, { refl }, {cases u, { refl }} end⟩ @[simp] def nat_sum_punit_equiv_nat : (ℕ ⊕ punit.{u+1}) ≃ ℕ := nat_equiv_nat_sum_punit.symm def int_equiv_nat_sum_nat : ℤ ≃ (ℕ ⊕ ℕ) := by refine ⟨_, _, _, _⟩; intro z; {cases z; [left, right]; assumption} <|> {cases z; refl} end def list_equiv_of_equiv {α β : Type*} : α ≃ β → list α ≃ list β | ⟨f, g, l, r⟩ := by refine ⟨list.map f, list.map g, λ x, _, λ x, _⟩; simp [id_of_left_inverse l, id_of_right_inverse r] def fin_equiv_subtype (n : ℕ) : fin n ≃ {m // m < n} := ⟨λ x, ⟨x.1, x.2⟩, λ x, ⟨x.1, x.2⟩, λ ⟨a, b⟩, rfl,λ ⟨a, b⟩, rfl⟩ def decidable_eq_of_equiv [decidable_eq β] (e : α ≃ β) : decidable_eq α | a₁ a₂ := decidable_of_iff (e a₁ = e a₂) e.bijective.1.eq_iff def inhabited_of_equiv [inhabited β] (e : α ≃ β) : inhabited α := ⟨e.symm (default _)⟩ section open subtype def subtype_equiv_of_subtype {p : α → Prop} : Π (e : α ≃ β), {a : α // p a} ≃ {b : β // p (e.symm b)} | ⟨f, g, l, r⟩ := ⟨subtype.map f $ assume a ha, show p (g (f a)), by rwa [l], subtype.map g $ assume a ha, ha, assume p, by simp [map_comp, l.comp_eq_id]; rw [map_id]; refl, assume p, by simp [map_comp, r.comp_eq_id]; rw [map_id]; refl⟩ def subtype_subtype_equiv_subtype {α : Type u} (p : α → Prop) (q : subtype p → Prop) : subtype q ≃ {a : α // ∃h:p a, q ⟨a, h⟩ } := ⟨λ⟨⟨a, ha⟩, ha'⟩, ⟨a, ha, ha'⟩, λ⟨a, ha⟩, ⟨⟨a, ha.cases_on $ assume h _, h⟩, by cases ha; exact ha_h⟩, assume ⟨⟨a, ha⟩, h⟩, rfl, assume ⟨a, h₁, h₂⟩, rfl⟩ /-- aka coimage -/ def equiv_sigma_subtype {α : Type u} {β : Type v} (f : α → β) : α ≃ Σ b, {x : α // f x = b} := ⟨λ x, ⟨f x, x, rfl⟩, λ x, x.2.1, λ x, rfl, λ ⟨b, x, H⟩, sigma.eq H $ eq.drec_on H $ subtype.eq rfl⟩ def pi_equiv_subtype_sigma (ι : Type*) (π : ι → Type*) : (Πi, π i) ≃ {f : ι → Σi, π i | ∀i, (f i).1 = i } := ⟨ λf, ⟨λi, ⟨i, f i⟩, assume i, rfl⟩, λf i, begin rw ← f.2 i, exact (f.1 i).2 end, assume f, funext $ assume i, rfl, assume ⟨f, hf⟩, subtype.eq $ funext $ assume i, sigma.eq (hf i).symm $ eq_of_heq $ rec_heq_of_heq _ $ rec_heq_of_heq _ $ heq.refl _⟩ end section local attribute [elab_with_expected_type] quot.lift def quot_equiv_of_quot' {r : α → α → Prop} {s : β → β → Prop} (e : α ≃ β) (h : ∀ a a', r a a' ↔ s (e a) (e a')) : quot r ≃ quot s := ⟨quot.lift (λ a, quot.mk _ (e a)) (λ a a' H, quot.sound ((h a a').mp H)), quot.lift (λ b, quot.mk _ (e.symm b)) (λ b b' H, quot.sound ((h _ _).mpr (by convert H; simp))), quot.ind $ by simp, quot.ind $ by simp⟩ def quot_equiv_of_quot {r : α → α → Prop} (e : α ≃ β) : quot r ≃ quot (λ b b', r (e.symm b) (e.symm b')) := quot_equiv_of_quot' e (by simp) end namespace set open set protected def univ (α) : @univ α ≃ α := ⟨subtype.val, λ a, ⟨a, trivial⟩, λ ⟨a, _⟩, rfl, λ a, rfl⟩ protected def empty (α) : (∅ : set α) ≃ empty := equiv_empty $ λ ⟨x, h⟩, not_mem_empty x h protected def pempty (α) : (∅ : set α) ≃ pempty := equiv_pempty $ λ ⟨x, h⟩, not_mem_empty x h protected def union' {α} {s t : set α} (p : α → Prop) [decidable_pred p] (hs : ∀ x ∈ s, p x) (ht : ∀ x ∈ t, ¬ p x) : (s ∪ t : set α) ≃ (s ⊕ t) := ⟨λ ⟨x, h⟩, if hp : p x then sum.inl ⟨_, h.resolve_right (λ xt, ht _ xt hp)⟩ else sum.inr ⟨_, h.resolve_left (λ xs, hp (hs _ xs))⟩, λ o, match o with | (sum.inl ⟨x, h⟩) := ⟨x, or.inl h⟩ | (sum.inr ⟨x, h⟩) := ⟨x, or.inr h⟩ end, λ ⟨x, h'⟩, by by_cases p x; simp [union'._match_1, union'._match_2, h]; congr, λ o, by rcases o with ⟨x, h⟩ | ⟨x, h⟩; simp [union'._match_1, union'._match_2, h]; [simp [hs _ h], simp [ht _ h]]⟩ protected def union {α} {s t : set α} [decidable_pred s] (H : s ∩ t = ∅) : (s ∪ t : set α) ≃ (s ⊕ t) := set.union' s (λ _, id) (λ x xt xs, subset_empty_iff.2 H ⟨xs, xt⟩) protected def singleton {α} (a : α) : ({a} : set α) ≃ punit.{u} := ⟨λ _, punit.star, λ _, ⟨a, mem_singleton _⟩, λ ⟨x, h⟩, by simp at h; subst x, λ ⟨⟩, rfl⟩ protected def insert {α} {s : set.{u} α} [decidable_pred s] {a : α} (H : a ∉ s) : (insert a s : set α) ≃ (s ⊕ punit.{u+1}) := by rw ← union_singleton; exact (set.union $ inter_singleton_eq_empty.2 H).trans (sum_congr (equiv.refl _) (set.singleton _)) protected def sum_compl {α} (s : set α) [decidable_pred s] : (s ⊕ (-s : set α)) ≃ α := (set.union (inter_compl_self _)).symm.trans (by rw union_compl_self; exact set.univ _) protected def union_sum_inter {α : Type u} (s t : set α) [decidable_pred s] : ((s ∪ t : set α) ⊕ (s ∩ t : set α)) ≃ (s ⊕ t) := calc ((s ∪ t : set α) ⊕ (s ∩ t : set α)) ≃ ((s ∪ t \ s : set α) ⊕ (s ∩ t : set α)) : by rw [union_diff_self] ... ≃ ((s ⊕ (t \ s : set α)) ⊕ (s ∩ t : set α)) : sum_congr (set.union (inter_diff_self _ _)) (equiv.refl _) ... ≃ (s ⊕ (t \ s : set α) ⊕ (s ∩ t : set α)) : sum_assoc _ _ _ ... ≃ (s ⊕ (t \ s ∪ s ∩ t : set α)) : sum_congr (equiv.refl _) begin refine (set.union' (∉ s) _ _).symm, exacts [λ x hx, hx.2, λ x hx, not_not_intro hx.1] end ... ≃ (s ⊕ t) : by rw (_ : t \ s ∪ s ∩ t = t); rw [union_comm, inter_comm, inter_union_diff] protected def prod {α β} (s : set α) (t : set β) : (s.prod t) ≃ (s × t) := ⟨λ ⟨⟨x, y⟩, ⟨h₁, h₂⟩⟩, ⟨⟨x, h₁⟩, ⟨y, h₂⟩⟩, λ ⟨⟨x, h₁⟩, ⟨y, h₂⟩⟩, ⟨⟨x, y⟩, ⟨h₁, h₂⟩⟩, λ ⟨⟨x, y⟩, ⟨h₁, h₂⟩⟩, rfl, λ ⟨⟨x, h₁⟩, ⟨y, h₂⟩⟩, rfl⟩ protected noncomputable def image {α β} (f : α → β) (s : set α) (H : injective f) : s ≃ (f '' s) := ⟨λ ⟨x, h⟩, ⟨f x, mem_image_of_mem _ h⟩, λ ⟨y, h⟩, ⟨classical.some h, (classical.some_spec h).1⟩, λ ⟨x, h⟩, subtype.eq (H (classical.some_spec (mem_image_of_mem f h)).2), λ ⟨y, h⟩, subtype.eq (classical.some_spec h).2⟩ @[simp] theorem image_apply {α β} (f : α → β) (s : set α) (H : injective f) (a h) : set.image f s H ⟨a, h⟩ = ⟨f a, mem_image_of_mem _ h⟩ := rfl protected noncomputable def range {α β} (f : α → β) (H : injective f) : α ≃ range f := (set.univ _).symm.trans $ (set.image f univ H).trans (equiv.cast $ by rw image_univ) @[simp] theorem range_apply {α β} (f : α → β) (H : injective f) (a) : set.range f H a = ⟨f a, set.mem_range_self _⟩ := by dunfold equiv.set.range equiv.set.univ; simp [set_coe_cast, -image_univ, image_univ.symm] end set noncomputable def of_bijective {α β} {f : α → β} (hf : bijective f) : α ≃ β := ⟨f, λ x, classical.some (hf.2 x), λ x, hf.1 (classical.some_spec (hf.2 (f x))), λ x, classical.some_spec (hf.2 x)⟩ @[simp] theorem of_bijective_to_fun {α β} {f : α → β} (hf : bijective f) : (of_bijective hf : α → β) = f := rfl section swap variable [decidable_eq α] open decidable def swap_core (a b r : α) : α := if r = a then b else if r = b then a else r theorem swap_core_self (r a : α) : swap_core a a r = r := by unfold swap_core; split_ifs; cc theorem swap_core_swap_core (r a b : α) : swap_core a b (swap_core a b r) = r := by unfold swap_core; split_ifs; cc theorem swap_core_comm (r a b : α) : swap_core a b r = swap_core b a r := by unfold swap_core; split_ifs; cc /-- `swap a b` is the permutation that swaps `a` and `b` and leaves other values as is. -/ def swap (a b : α) : perm α := ⟨swap_core a b, swap_core a b, λr, swap_core_swap_core r a b, λr, swap_core_swap_core r a b⟩ theorem swap_self (a : α) : swap a a = equiv.refl _ := eq_of_to_fun_eq $ funext $ λ r, swap_core_self r a theorem swap_comm (a b : α) : swap a b = swap b a := eq_of_to_fun_eq $ funext $ λ r, swap_core_comm r _ _ theorem swap_apply_def (a b x : α) : swap a b x = if x = a then b else if x = b then a else x := rfl @[simp] theorem swap_apply_left (a b : α) : swap a b a = b := if_pos rfl @[simp] theorem swap_apply_right (a b : α) : swap a b b = a := by by_cases b = a; simp [swap_apply_def, *] theorem swap_apply_of_ne_of_ne {a b x : α} : x ≠ a → x ≠ b → swap a b x = x := by simp [swap_apply_def] {contextual := tt} @[simp] theorem swap_swap (a b : α) : (swap a b).trans (swap a b) = equiv.refl _ := eq_of_to_fun_eq $ funext $ λ x, swap_core_swap_core _ _ _ theorem swap_comp_apply {a b x : α} (π : perm α) : π.trans (swap a b) x = if π x = a then b else if π x = b then a else π x := by cases π; refl @[simp] lemma swap_inv {α : Type*} [decidable_eq α] (x y : α) : (swap x y)⁻¹ = swap x y := rfl @[simp] lemma symm_trans_swap_trans [decidable_eq α] [decidable_eq β] (a b : α) (e : α ≃ β) : (e.symm.trans (swap a b)).trans e = swap (e a) (e b) := equiv.ext _ _ (λ x, begin have : ∀ a, e.symm x = a ↔ x = e a := λ a, by rw @eq_comm _ (e.symm x); split; intros; simp * at *, simp [swap_apply_def, this], split_ifs; simp end) @[simp] lemma swap_mul_self {α : Type*} [decidable_eq α] (i j : α) : swap i j * swap i j = 1 := equiv.swap_swap i j @[simp] lemma swap_apply_self {α : Type*} [decidable_eq α] (i j a : α) : swap i j (swap i j a) = a := by rw [← perm.mul_apply, swap_mul_self, perm.one_apply] /-- Augment an equivalence with a prescribed mapping `f a = b` -/ def set_value (f : α ≃ β) (a : α) (b : β) : α ≃ β := (swap a (f.symm b)).trans f @[simp] theorem set_value_eq (f : α ≃ β) (a : α) (b : β) : set_value f a b a = b := by dsimp [set_value]; simp [swap_apply_left] end swap end equiv instance {α} [subsingleton α] : subsingleton (ulift α) := equiv.ulift.subsingleton instance {α} [subsingleton α] : subsingleton (plift α) := equiv.plift.subsingleton instance {α} [decidable_eq α] : decidable_eq (ulift α) := equiv.ulift.decidable_eq instance {α} [decidable_eq α] : decidable_eq (plift α) := equiv.plift.decidable_eq
c4d86271d498a3a3b609541c8170c57778bb1036
0845ae2ca02071debcfd4ac24be871236c01784f
/library/init/lean/declaration.lean
e3e71b91ac998b1c5d7fadcf5f64d6506b2decef
[ "Apache-2.0" ]
permissive
GaloisInc/lean4
74c267eb0e900bfaa23df8de86039483ecbd60b7
228ddd5fdcd98dd4e9c009f425284e86917938aa
refs/heads/master
1,643,131,356,301
1,562,715,572,000
1,562,715,572,000
192,390,898
0
0
null
1,560,792,750,000
1,560,792,749,000
null
UTF-8
Lean
false
false
6,561
lean
/- Copyright (c) 2018 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.lean.expr namespace Lean /-- Reducibility hints are used in the convertibility checker. When trying to solve a constraint such a (f ...) =?= (g ...) where f and g are definitions, the checker has to decide which one will be unfolded. If f (g) is opaque, then g (f) is unfolded if it is also not marked as opaque, Else if f (g) is abbrev, then f (g) is unfolded if g (f) is also not marked as abbrev, Else if f and g are regular, then we unfold the one with the biggest definitional height. Otherwise both are unfolded. The arguments of the `regular` Constructor are: the definitional height and the flag `selfOpt`. The definitional height is by default computed by the kernel. It only takes into account other regular definitions used in a definition. When creating declarations using meta-programming, we can specify the definitional depth manually. Remark: the hint only affects performance. None of the hints prevent the kernel from unfolding a declaration during Type checking. Remark: the ReducibilityHints are not related to the attributes: reducible/irrelevance/semireducible. These attributes are used by the Elaborator. The ReducibilityHints are used by the kernel (and Elaborator). Moreover, the ReducibilityHints cannot be changed after a declaration is added to the kernel. -/ inductive ReducibilityHints | opaque : ReducibilityHints | «abbrev» : ReducibilityHints | regular : UInt32 → ReducibilityHints /-- Base structure for `AxiomVal`, `DefinitionVal`, `TheoremVal`, `InductiveVal`, `ConstructorVal`, `RecursorVal` and `QuotVal`. -/ structure ConstantVal := (name : Name) (lparams : List Name) (type : Expr) structure AxiomVal extends ConstantVal := (isUnsafe : Bool) structure DefinitionVal extends ConstantVal := (value : Expr) (hints : ReducibilityHints) (isUnsafe : Bool) structure TheoremVal extends ConstantVal := (value : Task Expr) /- Value for an opaque constant declaration `constant x : t := e` -/ structure OpaqueVal extends ConstantVal := (value : Expr) structure Constructor := (name : Name) (type : Expr) structure inductiveType := (name : Name) (type : Expr) (ctors : List Constructor) /-- Declaration object that can be sent to the kernel. -/ inductive Declaration | axiomDecl (val : AxiomVal) | defnDecl (val : DefinitionVal) | thmDecl (val : TheoremVal) | opaqueDecl (val : OpaqueVal) | quotDecl | mutualDefnDecl (defns : List DefinitionVal) -- All definitions must be marked as `unsafe` | inductDecl (lparams : List Name) (nparams : Nat) (types : List inductiveType) (isUnsafe : Bool) /-- The kernel compiles (mutual) inductive declarations (see `inductiveDecls`) into a set of - `Declaration.inductDecl` (for each inductive datatype in the mutual Declaration), - `Declaration.ctorDecl` (for each Constructor in the mutual Declaration), - `Declaration.recDecl` (automatically generated recursors). This data is used to implement iota-reduction efficiently and compile nested inductive declarations. A series of checks are performed by the kernel to check whether a `inductiveDecls` is valid or not. -/ structure InductiveVal extends ConstantVal := (nparams : Nat) -- Number of parameters (nindices : Nat) -- Number of indices (all : List Name) -- List of all (including this one) inductive datatypes in the mutual declaration containing this one (ctors : List Name) -- List of all constructors for this inductive datatype (isRec : Bool) -- `true` Iff it is recursive (isUnsafe : Bool) (isReflexive : Bool) structure ConstructorVal extends ConstantVal := (induct : Name) -- Inductive Type this Constructor is a member of (cidx : Nat) -- Constructor index (i.e., Position in the inductive declaration) (nparams : Nat) -- Number of parameters in inductive datatype `induct` (nfields : Nat) -- Number of fields (i.e., arity - nparams) (isUnsafe : Bool) /-- Information for reducing a recursor -/ structure RecursorRule := (ctor : Name) -- Reduction rule for this Constructor (nfields : Nat) -- Number of fields (i.e., without counting inductive datatype parameters) (rhs : Expr) -- Right hand side of the reduction rule structure RecursorVal extends ConstantVal := (all : List Name) -- List of all inductive datatypes in the mutual declaration that generated this recursor (nparams : Nat) -- Number of parameters (nindices : Nat) -- Number of indices (nmotives : Nat) -- Number of motives (nminor : Nat) -- Number of minor premises (rules : List RecursorRule) -- A reduction for each Constructor (k : Bool) -- It supports K-like reduction (isUnsafe : Bool) inductive QuotKind | type -- `Quot` | ctor -- `Quot.mk` | lift -- `Quot.lift` | ind -- `Quot.ind` structure QuotVal extends ConstantVal := (kind : QuotKind) /-- Information associated with constant declarations. -/ inductive ConstantInfo | axiomInfo (val : AxiomVal) | defnInfo (val : DefinitionVal) | thmInfo (val : TheoremVal) | opaqueInfo (val : OpaqueVal) | quotInfo (val : QuotVal) | inductInfo (val : InductiveVal) | ctorInfo (val : ConstructorVal) | recInfo (val : RecursorVal) namespace ConstantInfo def toConstantVal : ConstantInfo → ConstantVal | (defnInfo {toConstantVal := d, ..}) := d | (axiomInfo {toConstantVal := d, ..}) := d | (thmInfo {toConstantVal := d, ..}) := d | (opaqueInfo {toConstantVal := d, ..}) := d | (quotInfo {toConstantVal := d, ..}) := d | (inductInfo {toConstantVal := d, ..}) := d | (ctorInfo {toConstantVal := d, ..}) := d | (recInfo {toConstantVal := d, ..}) := d def name (d : ConstantInfo) : Name := d.toConstantVal.name def lparams (d : ConstantInfo) : List Name := d.toConstantVal.lparams def type (d : ConstantInfo) : Expr := d.toConstantVal.type def value : ConstantInfo → Option Expr | (defnInfo {value := r, ..}) := some r | (thmInfo {value := r, ..}) := some r.get | _ := none def hasValue : ConstantInfo → Bool | (defnInfo {value := r, ..}) := true | (thmInfo {value := r, ..}) := true | _ := false def hints : ConstantInfo → ReducibilityHints | (defnInfo {hints := r, ..}) := r | _ := ReducibilityHints.opaque end ConstantInfo end Lean
ebc6119aef58bedfd6a281d4ee17fa61b67d0d3e
bb31430994044506fa42fd667e2d556327e18dfe
/src/topology/algebra/field.lean
657ca380445ede91710be76a37c4f4269c00e1f6
[ "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
10,073
lean
/- Copyright (c) 2021 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot, Scott Morrison -/ import topology.algebra.ring import topology.algebra.group_with_zero import topology.local_extr import field_theory.subfield /-! # Topological fields A topological division ring is a topological ring whose inversion function is continuous at every non-zero element. -/ namespace topological_ring open topological_space function variables (R : Type*) [semiring R] variables [topological_space R] /-- The induced topology on units of a topological semiring. This is not a global instance since other topologies could be relevant. Instead there is a class `induced_units` asserting that something equivalent to this construction holds. -/ def topological_space_units : topological_space Rˣ := induced (coe : Rˣ → R) ‹_› /-- Asserts the topology on units is the induced topology. Note: this is not always the correct topology. Another good candidate is the subspace topology of $R \times R$, with the units embedded via $u \mapsto (u, u^{-1})$. These topologies are not (propositionally) equal in general. -/ class induced_units [t : topological_space $ Rˣ] : Prop := (top_eq : t = induced (coe : Rˣ → R) ‹_›) variables [topological_space $ Rˣ] lemma units_topology_eq [induced_units R] : ‹topological_space Rˣ› = induced (coe : Rˣ → R) ‹_› := induced_units.top_eq lemma induced_units.continuous_coe [induced_units R] : continuous (coe : Rˣ → R) := (units_topology_eq R).symm ▸ continuous_induced_dom lemma units_embedding [induced_units R] : embedding (coe : Rˣ → R) := { induced := units_topology_eq R, inj := λ x y h, units.ext h } instance top_monoid_units [topological_semiring R] [induced_units R] : has_continuous_mul Rˣ := ⟨begin let mulR := (λ (p : R × R), p.1*p.2), let mulRx := (λ (p : Rˣ × Rˣ), p.1*p.2), have key : coe ∘ mulRx = mulR ∘ (λ p, (p.1.val, p.2.val)), from rfl, rw [continuous_iff_le_induced, units_topology_eq R, prod_induced_induced, induced_compose, key, ← induced_compose], apply induced_mono, rw ← continuous_iff_le_induced, exact continuous_mul, end⟩ end topological_ring variables {K : Type*} [division_ring K] [topological_space K] /-- Left-multiplication by a nonzero element of a topological division ring is proper, i.e., inverse images of compact sets are compact. -/ lemma filter.tendsto_cocompact_mul_left₀ [has_continuous_mul K] {a : K} (ha : a ≠ 0) : filter.tendsto (λ x : K, a * x) (filter.cocompact K) (filter.cocompact K) := filter.tendsto_cocompact_mul_left (inv_mul_cancel ha) /-- Right-multiplication by a nonzero element of a topological division ring is proper, i.e., inverse images of compact sets are compact. -/ lemma filter.tendsto_cocompact_mul_right₀ [has_continuous_mul K] {a : K} (ha : a ≠ 0) : filter.tendsto (λ x : K, x * a) (filter.cocompact K) (filter.cocompact K) := filter.tendsto_cocompact_mul_right (mul_inv_cancel ha) variables (K) /-- A topological division ring is a division ring with a topology where all operations are continuous, including inversion. -/ class topological_division_ring extends topological_ring K, has_continuous_inv₀ K : Prop namespace topological_division_ring open filter set /-! In this section, we show that units of a topological division ring endowed with the induced topology form a topological group. These are not global instances because one could want another topology on units. To turn on this feature, use: ```lean local attribute [instance] topological_semiring.topological_space_units topological_division_ring.units_top_group ``` -/ local attribute [instance] topological_ring.topological_space_units @[priority 100] instance induced_units : topological_ring.induced_units K := ⟨rfl⟩ variables [topological_division_ring K] lemma units_top_group : topological_group Kˣ := { continuous_inv := begin rw continuous_iff_continuous_at, intros x, rw [continuous_at, nhds_induced, nhds_induced, tendsto_iff_comap, ←function.semiconj.filter_comap units.coe_inv _], apply comap_mono, rw [← tendsto_iff_comap, units.coe_inv], exact continuous_at_inv₀ x.ne_zero end, ..topological_ring.top_monoid_units K} local attribute [instance] units_top_group lemma continuous_units_inv : continuous (λ x : Kˣ, (↑(x⁻¹) : K)) := (topological_ring.induced_units.continuous_coe K).comp continuous_inv end topological_division_ring section subfield variables {α : Type*} [field α] [topological_space α] [topological_division_ring α] /-- The (topological-space) closure of a subfield of a topological field is itself a subfield. -/ def subfield.topological_closure (K : subfield α) : subfield α := { carrier := closure (K : set α), inv_mem' := begin intros x hx, by_cases h : x = 0, { rwa [h, inv_zero, ← h], }, { convert mem_closure_image (continuous_at_inv₀ h) hx using 2, ext x, split, { exact λ hx, ⟨x⁻¹, ⟨K.inv_mem hx, inv_inv x⟩⟩, }, { rintros ⟨y, ⟨hy, rfl⟩⟩, exact K.inv_mem hy, }}, end, ..K.to_subring.topological_closure, } lemma subfield.le_topological_closure (s : subfield α) : s ≤ s.topological_closure := subset_closure lemma subfield.is_closed_topological_closure (s : subfield α) : is_closed (s.topological_closure : set α) := is_closed_closure lemma subfield.topological_closure_minimal (s : subfield α) {t : subfield α} (h : s ≤ t) (ht : is_closed (t : set α)) : s.topological_closure ≤ t := closure_minimal h ht end subfield section affine_homeomorph /-! This section is about affine homeomorphisms from a topological field `𝕜` to itself. Technically it does not require `𝕜` to be a topological field, a topological ring that happens to be a field is enough. -/ variables {𝕜 : Type*} [field 𝕜] [topological_space 𝕜] [topological_ring 𝕜] /-- The map `λ x, a * x + b`, as a homeomorphism from `𝕜` (a topological field) to itself, when `a ≠ 0`. -/ @[simps] def affine_homeomorph (a b : 𝕜) (h : a ≠ 0) : 𝕜 ≃ₜ 𝕜 := { to_fun := λ x, a * x + b, inv_fun := λ y, (y - b) / a, left_inv := λ x, by { simp only [add_sub_cancel], exact mul_div_cancel_left x h, }, right_inv := λ y, by { simp [mul_div_cancel' _ h], }, } end affine_homeomorph section local_extr variables {α β : Type*} [topological_space α] [linear_ordered_semifield β] {a : α} open_locale topological_space lemma is_local_min.inv {f : α → β} {a : α} (h1 : is_local_min f a) (h2 : ∀ᶠ z in 𝓝 a, 0 < f z) : is_local_max f⁻¹ a := by filter_upwards [h1, h2] with z h3 h4 using (inv_le_inv h4 h2.self_of_nhds).mpr h3 end local_extr section preconnected /-! Some results about functions on preconnected sets valued in a ring or field with a topology. -/ open set variables {α 𝕜 : Type*} {f g : α → 𝕜} {S : set α} [topological_space α] [topological_space 𝕜] [t1_space 𝕜] /-- If `f` is a function `α → 𝕜` which is continuous on a preconnected set `S`, and `f ^ 2 = 1` on `S`, then either `f = 1` on `S`, or `f = -1` on `S`. -/ lemma is_preconnected.eq_one_or_eq_neg_one_of_sq_eq [ring 𝕜] [no_zero_divisors 𝕜] (hS : is_preconnected S) (hf : continuous_on f S) (hsq : eq_on (f ^ 2) 1 S) : (eq_on f 1 S) ∨ (eq_on f (-1) S) := begin simp_rw [eq_on, pi.one_apply, pi.pow_apply, sq_eq_one_iff] at hsq, -- First deal with crazy case where `S` is empty. by_cases hSe : ∀ (x:α), x ∉ S, { left, intros x hx, exfalso, exact hSe x hx, }, push_neg at hSe, choose y hy using hSe, suffices : ∀ (x:α), x ∈ S → f x = f y, { rcases (hsq hy), { left, intros z hz, rw [pi.one_apply z, ←h], exact this z hz, }, { right, intros z hz, rw [pi.neg_apply, pi.one_apply, ←h], exact this z hz, } }, refine λ x hx, hS.constant_of_maps_to hf (λ z hz, _) hx hy, show f z ∈ ({-1, 1} : set 𝕜), { exact mem_insert_iff.mpr (hsq hz).symm, }, exact discrete_of_t1_of_finite, end /-- If `f, g` are functions `α → 𝕜`, both continuous on a preconnected set `S`, with `f ^ 2 = g ^ 2` on `S`, and `g z ≠ 0` all `z ∈ S`, then either `f = g` or `f = -g` on `S`. -/ lemma is_preconnected.eq_or_eq_neg_of_sq_eq [field 𝕜] [has_continuous_inv₀ 𝕜] [has_continuous_mul 𝕜] (hS : is_preconnected S) (hf : continuous_on f S) (hg : continuous_on g S) (hsq : eq_on (f ^ 2) (g ^ 2) S) (hg_ne : ∀ {x:α}, x ∈ S → g x ≠ 0) : (eq_on f g S) ∨ (eq_on f (-g) S) := begin rcases hS.eq_one_or_eq_neg_one_of_sq_eq (hf.div hg (λ z hz, hg_ne hz)) (λ x hx, _) with h | h, { refine or.inl (λ x hx, _), rw ←div_eq_one_iff_eq (hg_ne hx), exact h hx }, { refine or.inr (λ x hx, _), specialize h hx, rwa [pi.div_apply, pi.neg_apply, pi.one_apply, div_eq_iff (hg_ne hx), neg_one_mul] at h, }, { rw [pi.one_apply, div_pow, pi.div_apply, hsq hx, div_self], exact pow_ne_zero _ (hg_ne hx) }, end /-- If `f, g` are functions `α → 𝕜`, both continuous on a preconnected set `S`, with `f ^ 2 = g ^ 2` on `S`, and `g z ≠ 0` all `z ∈ S`, then as soon as `f = g` holds at one point of `S` it holds for all points. -/ lemma is_preconnected.eq_of_sq_eq [field 𝕜] [has_continuous_inv₀ 𝕜] [has_continuous_mul 𝕜] (hS : is_preconnected S) (hf : continuous_on f S) (hg : continuous_on g S) (hsq : eq_on (f ^ 2) (g ^ 2) S) (hg_ne : ∀ {x:α}, x ∈ S → g x ≠ 0) {y : α} (hy : y ∈ S) (hy' : f y = g y) : eq_on f g S := λ x hx, begin rcases hS.eq_or_eq_neg_of_sq_eq hf hg @hsq @hg_ne with h | h, { exact h hx }, { rw [h hy, eq_comm, ←sub_eq_zero, sub_eq_add_neg, pi.neg_apply, neg_neg, ←mul_two, mul_eq_zero] at hy', cases hy', -- need to handle case of `char 𝕜 = 2` separately { exfalso, exact hg_ne hy hy' }, { rw [h hx, pi.neg_apply, eq_comm, ←sub_eq_zero, sub_eq_add_neg, neg_neg, ←mul_two, hy', mul_zero], } }, end end preconnected
218cf92f98624fd1f5d76d0f51f0495dd861b01c
82b86ba2ae0d5aed0f01f49c46db5afec0eb2bd7
/stage0/src/Lean/Message.lean
c35a39f704b9cfa86a9ab40c55dfa8cb7292592a
[ "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
13,848
lean
/- Copyright (c) 2018 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Sebastian Ullrich, Leonardo de Moura Message Type used by the Lean frontend -/ import Lean.Data.Position import Lean.Data.OpenDecl import Lean.Syntax import Lean.MetavarContext import Lean.Environment import Lean.Util.PPExt import Lean.Util.PPGoal namespace Lean def mkErrorStringWithPos (fileName : String) (line col : Nat) (msg : String) : String := fileName ++ ":" ++ toString line ++ ":" ++ toString col ++ ": " ++ toString msg inductive MessageSeverity | information | warning | error structure MessageDataContext := (env : Environment) (mctx : MetavarContext) (lctx : LocalContext) (opts : Options) structure NamingContext := (currNamespace : Name) (openDecls : List OpenDecl) /- Structure message data. We use it for reporting errors, trace messages, etc. -/ inductive MessageData | ofFormat : Format → MessageData | ofSyntax : Syntax → MessageData | ofExpr : Expr → MessageData | ofLevel : Level → MessageData | ofName : Name → MessageData | ofGoal : MVarId → MessageData /- `withContext ctx d` specifies the pretty printing context `(env, mctx, lctx, opts)` for the nested expressions in `d`. -/ | withContext : MessageDataContext → MessageData → MessageData | withNamingContext : NamingContext → MessageData → MessageData /- Lifted `Format.nest` -/ | nest : Nat → MessageData → MessageData /- Lifted `Format.group` -/ | group : MessageData → MessageData /- Lifted `Format.compose` -/ | compose : MessageData → MessageData → MessageData /- Tagged sections. `Name` should be viewed as a "kind", and is used by `MessageData` inspector functions. Example: an inspector that tries to find "definitional equality failures" may look for the tag "DefEqFailure". -/ | tagged : Name → MessageData → MessageData | node : Array MessageData → MessageData namespace MessageData def nil : MessageData := ofFormat Format.nil def isNil : MessageData → Bool | ofFormat Format.nil => true | _ => false def isNest : MessageData → Bool | nest _ _ => true | _ => false instance : Inhabited MessageData := ⟨MessageData.ofFormat (arbitrary _)⟩ def mkPPContext (nCtx : NamingContext) (ctx : MessageDataContext) : PPContext := { env := ctx.env, mctx := ctx.mctx, lctx := ctx.lctx, opts := ctx.opts, currNamespace := nCtx.currNamespace, openDecls := nCtx.openDecls } partial def formatAux : NamingContext → Option MessageDataContext → MessageData → IO Format | _, _, ofFormat fmt => pure fmt | _, _, ofLevel u => pure $ fmt u | _, _, ofName n => pure $ fmt n | nCtx, some ctx, ofSyntax s => ppTerm (mkPPContext nCtx ctx) s -- HACK: might not be a term | _, none, ofSyntax s => pure $ s.formatStx | _, none, ofExpr e => pure $ format (toString e) | nCtx, some ctx, ofExpr e => ppExpr (mkPPContext nCtx ctx) e | _, none, ofGoal mvarId => pure $ "goal " ++ format (mkMVar mvarId) | nCtx, some ctx, ofGoal mvarId => ppGoal (mkPPContext nCtx ctx) mvarId | nCtx, _, withContext ctx d => formatAux nCtx ctx d | _, ctx, withNamingContext nCtx d => formatAux nCtx ctx d | nCtx, ctx, tagged cls d => do let d ← formatAux nCtx ctx d; pure $ Format.sbracket (format cls) ++ " " ++ d | nCtx, ctx, nest n d => Format.nest n <$> formatAux nCtx ctx d | nCtx, ctx, compose d₁ d₂ => do let d₁ ← formatAux nCtx ctx d₁; let d₂ ← formatAux nCtx ctx d₂; pure $ d₁ ++ d₂ | nCtx, ctx, group d => Format.group <$> formatAux nCtx ctx d | nCtx, ctx, node ds => Format.nest 2 <$> ds.foldlM (fun r d => do let d ← formatAux nCtx ctx d; pure $ r ++ Format.line ++ d) Format.nil protected def format (msgData : MessageData) : IO Format := formatAux { currNamespace := Name.anonymous, openDecls := [] } none msgData protected def toString (msgData : MessageData) : IO String := do let fmt ← msgData.format pure $ toString fmt instance : Append MessageData := ⟨compose⟩ instance : Coe String MessageData := ⟨ofFormat ∘ format⟩ instance : Coe Format MessageData := ⟨ofFormat⟩ instance : Coe Level MessageData := ⟨ofLevel⟩ instance : Coe Expr MessageData := ⟨ofExpr⟩ instance : Coe Name MessageData := ⟨ofName⟩ instance : Coe Syntax MessageData := ⟨ofSyntax⟩ instance : Coe (Option Expr) MessageData := ⟨fun o => match o with | none => "none" | some e => ofExpr e⟩ partial def arrayExpr.toMessageData (es : Array Expr) (i : Nat) (acc : MessageData) : MessageData := if h : i < es.size then let e := es.get ⟨i, h⟩; let acc := if i == 0 then acc ++ ofExpr e else acc ++ ", " ++ ofExpr e; toMessageData es (i+1) acc else acc ++ "]" instance : Coe (Array Expr) MessageData := ⟨fun es => arrayExpr.toMessageData es 0 "#["⟩ def bracket (l : String) (f : MessageData) (r : String) : MessageData := group (nest l.length $ l ++ f ++ r) def paren (f : MessageData) : MessageData := bracket "(" f ")" def sbracket (f : MessageData) : MessageData := bracket "[" f "]" def joinSep : List MessageData → MessageData → MessageData | [], sep => Format.nil | [a], sep => a | a::as, sep => a ++ sep ++ joinSep as sep def ofList: List MessageData → MessageData | [] => "[]" | xs => sbracket $ joinSep xs ("," ++ Format.line) def ofArray (msgs : Array MessageData) : MessageData := ofList msgs.toList instance : Coe (List MessageData) MessageData := ⟨ofList⟩ instance : Coe (List Expr) MessageData := ⟨fun es => ofList $ es.map ofExpr⟩ end MessageData structure Message := (fileName : String) (pos : Position) (endPos : Option Position := none) (severity : MessageSeverity := MessageSeverity.error) (caption : String := "") (data : MessageData) @[export lean_mk_message] def mkMessageEx (fileName : String) (pos : Position) (endPos : Option Position) (severity : MessageSeverity) (caption : String) (text : String) : Message := { fileName := fileName, pos := pos, endPos := endPos, severity := severity, caption := caption, data := text } namespace Message protected def toString (msg : Message) : IO String := do let str ← msg.data.toString pure $ mkErrorStringWithPos msg.fileName msg.pos.line msg.pos.column ((match msg.severity with | MessageSeverity.information => "" | MessageSeverity.warning => "warning: " | MessageSeverity.error => "error: ") ++ (if msg.caption == "" then "" else msg.caption ++ ":\n") ++ str) instance : Inhabited Message := ⟨{ fileName := "", pos := ⟨0, 1⟩, data := arbitrary _}⟩ @[export lean_message_pos] def getPostEx (msg : Message) : Position := msg.pos @[export lean_message_severity] def getSeverityEx (msg : Message) : MessageSeverity := msg.severity @[export lean_message_string] unsafe def getMessageStringEx (msg : Message) : String := match unsafeIO (msg.data.toString) with -- hack: this is going to be deleted | Except.ok msg => msg | Except.error e => "error formatting message: " ++ toString e end Message structure MessageLog := (msgs : Std.PersistentArray Message := {}) namespace MessageLog def empty : MessageLog := ⟨{}⟩ def isEmpty (log : MessageLog) : Bool := log.msgs.isEmpty instance : Inhabited MessageLog := ⟨{}⟩ def add (msg : Message) (log : MessageLog) : MessageLog := ⟨log.msgs.push msg⟩ protected def append (l₁ l₂ : MessageLog) : MessageLog := ⟨l₁.msgs ++ l₂.msgs⟩ instance : Append MessageLog := ⟨MessageLog.append⟩ def hasErrors (log : MessageLog) : Bool := log.msgs.any fun m => match m.severity with | MessageSeverity.error => true | _ => false def errorsToWarnings (log : MessageLog) : MessageLog := { msgs := log.msgs.map (fun m => match m.severity with | MessageSeverity.error => { m with severity := MessageSeverity.warning } | _ => m) } def getInfoMessages (log : MessageLog) : MessageLog := { msgs := log.msgs.filter fun m => match m.severity with | MessageSeverity.information => true | _ => false } def forM {m : Type → Type} [Monad m] (log : MessageLog) (f : Message → m Unit) : m Unit := log.msgs.forM f def toList (log : MessageLog) : List Message := (log.msgs.foldl (fun acc msg => msg :: acc) []).reverse end MessageLog def MessageData.nestD (msg : MessageData) : MessageData := MessageData.nest 2 msg def indentD (msg : MessageData) : MessageData := MessageData.nestD (Format.line ++ msg) def indentExpr (e : Expr) : MessageData := indentD e namespace KernelException private def mkCtx (env : Environment) (lctx : LocalContext) (opts : Options) (msg : MessageData) : MessageData := MessageData.withContext { env := env, mctx := {}, lctx := lctx, opts := opts } msg def toMessageData (e : KernelException) (opts : Options) : MessageData := match e with | unknownConstant env constName => mkCtx env {} opts $ "(kernel) unknown constant " ++ constName | alreadyDeclared env constName => mkCtx env {} opts $ "(kernel) constant has already been declared " ++ constName | declTypeMismatch env decl givenType => let process (n : Name) (expectedType : Expr) : MessageData := "(kernel) declaration type mismatch " ++ n ++ Format.line ++ "has type" ++ indentExpr givenType ++ Format.line ++ "but it is expected to have type" ++ indentExpr expectedType; match decl with | Declaration.defnDecl { name := n, type := type, .. } => process n type | Declaration.thmDecl { name := n, type := type, .. } => process n type | _ => "(kernel) declaration type mismatch" -- TODO fix type checker, type mismatch for mutual decls does not have enough information | declHasMVars env constName _ => mkCtx env {} opts $ "(kernel) declaration has metavariables " ++ constName | declHasFVars env constName _ => mkCtx env {} opts $ "(kernel) declaration has free variables " ++ constName | funExpected env lctx e => mkCtx env lctx opts $ "(kernel) function expected" ++ indentExpr e | typeExpected env lctx e => mkCtx env lctx opts $ "(kernel) type expected" ++ indentExpr e | letTypeMismatch env lctx n _ _ => mkCtx env lctx opts $ "(kernel) let-declaration type mismatch " ++ n | exprTypeMismatch env lctx e _ => mkCtx env lctx opts $ "(kernel) type mismatch at " ++ indentExpr e | appTypeMismatch env lctx e fnType argType => mkCtx env lctx opts $ "application type mismatch" ++ indentExpr e ++ Format.line ++ "argument has type" ++ indentExpr argType ++ Format.line ++ "but function has type" ++ indentExpr fnType | invalidProj env lctx e => mkCtx env lctx opts $ "(kernel) invalid projection" ++ indentExpr e | other msg => "(kernel) " ++ msg end KernelException class AddMessageContext (m : Type → Type) := (addMessageContext : MessageData → m MessageData) export AddMessageContext (addMessageContext) instance (m n) [AddMessageContext m] [MonadLift m n] : AddMessageContext n := { addMessageContext := fun msg => liftM (addMessageContext msg : m _) } def addMessageContextPartial {m} [Monad m] [MonadEnv m] [MonadOptions m] (msgData : MessageData) : m MessageData := do let env ← getEnv let opts ← getOptions pure $ MessageData.withContext { env := env, mctx := {}, lctx := {}, opts := opts } msgData def addMessageContextFull {m} [Monad m] [MonadEnv m] [MonadMCtx m] [MonadLCtx m] [MonadOptions m] (msgData : MessageData) : m MessageData := do let env ← getEnv let mctx ← getMCtx let lctx ← getLCtx let opts ← getOptions pure $ MessageData.withContext { env := env, mctx := mctx, lctx := lctx, opts := opts } msgData class ToMessageData (α : Type) := (toMessageData : α → MessageData) export ToMessageData (toMessageData) def stringToMessageData (str : String) : MessageData := let lines := str.split (· == '\n') let lines := lines.map (MessageData.ofFormat ∘ fmt) MessageData.joinSep lines (MessageData.ofFormat Format.line) instance {α} [ToFormat α] : ToMessageData α := ⟨MessageData.ofFormat ∘ fmt⟩ instance : ToMessageData Expr := ⟨MessageData.ofExpr⟩ instance : ToMessageData Level := ⟨MessageData.ofLevel⟩ instance : ToMessageData Name := ⟨MessageData.ofName⟩ instance : ToMessageData String := ⟨stringToMessageData⟩ instance : ToMessageData Syntax := ⟨MessageData.ofSyntax⟩ instance : ToMessageData Format := ⟨MessageData.ofFormat⟩ instance : ToMessageData MessageData := ⟨id⟩ instance {α} [ToMessageData α] : ToMessageData (List α) := ⟨fun as => MessageData.ofList $ as.map toMessageData⟩ instance {α} [ToMessageData α] : ToMessageData (Array α) := ⟨fun as => toMessageData as.toList⟩ instance {α} [ToMessageData α] : ToMessageData (Option α) := ⟨fun | none => "none" | some e => "some (" ++ toMessageData e ++ ")"⟩ instance : ToMessageData (Option Expr) := ⟨fun | none => "<not-available>" | some e => toMessageData e⟩ syntax:max "msg!" (interpolatedStr term) : term macro_rules | `(msg! $interpStr) => do let chunks := interpStr.getArgs let r ← Lean.Syntax.expandInterpolatedStrChunks chunks (fun a b => `($a ++ $b)) (fun a => `(toMessageData $a)) `(($r : MessageData)) end Lean
b9305c9fa90b1e070b45736d3cd28cf7abf86c41
82b86ba2ae0d5aed0f01f49c46db5afec0eb2bd7
/src/Lean/PrettyPrinter.lean
56811184a38789fcd2967fcc178c8df5554970a2
[ "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
3,201
lean
/- Copyright (c) 2020 Sebastian Ullrich. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sebastian Ullrich -/ import Lean.Delaborator import Lean.PrettyPrinter.Parenthesizer import Lean.PrettyPrinter.Formatter import Lean.Parser.Module import Lean.ParserCompiler namespace Lean def PPContext.runCoreM {α : Type} (ppCtx : PPContext) (x : CoreM α) : IO α := Prod.fst <$> x.toIO { options := ppCtx.opts } { env := ppCtx.env } def PPContext.runMetaM {α : Type} (ppCtx : PPContext) (x : MetaM α) : IO α := ppCtx.runCoreM $ x.run' { lctx := ppCtx.lctx } { mctx := ppCtx.mctx } namespace PrettyPrinter def ppTerm (stx : Syntax) : CoreM Format := do let opts ← getOptions let stx := (sanitizeSyntax stx).run' { options := opts } parenthesizeTerm stx >>= formatTerm def ppExpr (currNamespace : Name) (openDecls : List OpenDecl) (e : Expr) : MetaM Format := do let lctx ← getLCtx let opts ← getOptions let lctx := lctx.sanitizeNames.run' { options := opts } Meta.withLCtx lctx #[] do let stx ← delab currNamespace openDecls e ppTerm stx @[export lean_pp_expr] def ppExprLegacy (env : Environment) (mctx : MetavarContext) (lctx : LocalContext) (opts : Options) (e : Expr) : IO Format := Prod.fst <$> ((ppExpr Name.anonymous [] e).run' { lctx := lctx } { mctx := mctx }).toIO { options := opts } { env := env } def ppCommand (stx : Syntax) : CoreM Format := parenthesizeCommand stx >>= formatCommand def ppModule (stx : Syntax) : CoreM Format := do parenthesize Lean.Parser.Module.module.parenthesizer stx >>= format Lean.Parser.Module.module.formatter private partial def noContext : MessageData → MessageData | MessageData.withContext ctx msg => noContext msg | MessageData.withNamingContext ctx msg => MessageData.withNamingContext ctx (noContext msg) | MessageData.nest n msg => MessageData.nest n (noContext msg) | MessageData.group msg => MessageData.group (noContext msg) | MessageData.compose msg₁ msg₂ => MessageData.compose (noContext msg₁) (noContext msg₂) | MessageData.tagged tag msg => MessageData.tagged tag (noContext msg) | MessageData.node msgs => MessageData.node (msgs.map noContext) | msg => msg -- strip context (including environments with registered pretty printers) to prevent infinite recursion when pretty printing pretty printer error private def withoutContext {m} [MonadExcept Exception m] (x : m Format) : m Format := tryCatch x fun | Exception.error ref msg => throw $ Exception.error ref (noContext msg) | ex => throw ex builtin_initialize ppFnsRef.set { ppExpr := fun ctx e => ctx.runMetaM $ withoutContext $ ppExpr ctx.currNamespace ctx.openDecls e, ppTerm := fun ctx stx => ctx.runCoreM $ withoutContext $ ppTerm stx, } builtin_initialize registerTraceClass `PrettyPrinter @[builtinInit] unsafe def registerParserCompilers : IO Unit := do ParserCompiler.registerParserCompiler ⟨`parenthesizer, parenthesizerAttribute, combinatorParenthesizerAttribute⟩ ParserCompiler.registerParserCompiler ⟨`formatter, formatterAttribute, combinatorFormatterAttribute⟩ end PrettyPrinter end Lean
849ecf8677cbd92a5e976b9076e6267810b3898c
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/stage0/src/Init/Data/Ord.lean
e5e5f4ea30ad29beb964932bf96f92d5c36d8e0a
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
EdAyers/lean4
57ac632d6b0789cb91fab2170e8c9e40441221bd
37ba0df5841bde51dbc2329da81ac23d4f6a4de4
refs/heads/master
1,676,463,245,298
1,660,619,433,000
1,660,619,433,000
183,433,437
1
0
Apache-2.0
1,657,612,672,000
1,556,196,574,000
Lean
UTF-8
Lean
false
false
1,894
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Dany Fabian, Sebastian Ullrich -/ prelude import Init.Data.Int import Init.Data.String inductive Ordering where | lt | eq | gt deriving Inhabited, BEq class Ord (α : Type u) where compare : α → α → Ordering export Ord (compare) @[inline] def compareOfLessAndEq {α} (x y : α) [LT α] [Decidable (x < y)] [DecidableEq α] : Ordering := if x < y then Ordering.lt else if x = y then Ordering.eq else Ordering.gt instance : Ord Nat where compare x y := compareOfLessAndEq x y instance : Ord Int where compare x y := compareOfLessAndEq x y instance : Ord Bool where compare | false, true => Ordering.lt | true, false => Ordering.gt | _, _ => Ordering.eq instance : Ord String where compare x y := compareOfLessAndEq x y instance (n : Nat) : Ord (Fin n) where compare x y := compare x.val y.val instance : Ord UInt8 where compare x y := compareOfLessAndEq x y instance : Ord UInt16 where compare x y := compareOfLessAndEq x y instance : Ord UInt32 where compare x y := compareOfLessAndEq x y instance : Ord UInt64 where compare x y := compareOfLessAndEq x y instance : Ord USize where compare x y := compareOfLessAndEq x y instance : Ord Char where compare x y := compareOfLessAndEq x y def ltOfOrd [Ord α] : LT α where lt a b := compare a b == Ordering.lt instance [Ord α] : DecidableRel (@LT.lt α ltOfOrd) := inferInstanceAs (DecidableRel (fun a b => compare a b == Ordering.lt)) def Ordering.isLE : Ordering → Bool | Ordering.lt => true | Ordering.eq => true | Ordering.gt => false def leOfOrd [Ord α] : LE α where le a b := (compare a b).isLE instance [Ord α] : DecidableRel (@LE.le α leOfOrd) := inferInstanceAs (DecidableRel (fun a b => (compare a b).isLE))
027193dab60ef9e4a5849e770094656eaad06ff6
9dc8cecdf3c4634764a18254e94d43da07142918
/src/analysis/normed_space/extend.lean
66bfa28a352c46e49f32fd8067ee1021b76d6643
[ "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
6,722
lean
/- Copyright (c) 2020 Ruben Van de Velde. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Ruben Van de Velde -/ import algebra.algebra.restrict_scalars import data.complex.is_R_or_C /-! # Extending a continuous `ℝ`-linear map to a continuous `𝕜`-linear map In this file we provide a way to extend a continuous `ℝ`-linear map to a continuous `𝕜`-linear map in a way that bounds the norm by the norm of the original map, when `𝕜` is either `ℝ` (the extension is trivial) or `ℂ`. We formulate the extension uniformly, by assuming `is_R_or_C 𝕜`. We motivate the form of the extension as follows. Note that `fc : F →ₗ[𝕜] 𝕜` is determined fully by `Re fc`: for all `x : F`, `fc (I • x) = I * fc x`, so `Im (fc x) = -Re (fc (I • x))`. Therefore, given an `fr : F →ₗ[ℝ] ℝ`, we define `fc x = fr x - fr (I • x) * I`. ## Main definitions * `linear_map.extend_to_𝕜` * `continuous_linear_map.extend_to_𝕜` ## Implementation details For convenience, the main definitions above operate in terms of `restrict_scalars ℝ 𝕜 F`. Alternate forms which operate on `[is_scalar_tower ℝ 𝕜 F]` instead are provided with a primed name. -/ open is_R_or_C variables {𝕜 : Type*} [is_R_or_C 𝕜] {F : Type*} [seminormed_add_comm_group F] [normed_space 𝕜 F] local notation `abs𝕜` := @is_R_or_C.abs 𝕜 _ /-- Extend `fr : F →ₗ[ℝ] ℝ` to `F →ₗ[𝕜] 𝕜` in a way that will also be continuous and have its norm bounded by `∥fr∥` if `fr` is continuous. -/ noncomputable def linear_map.extend_to_𝕜' [module ℝ F] [is_scalar_tower ℝ 𝕜 F] (fr : F →ₗ[ℝ] ℝ) : F →ₗ[𝕜] 𝕜 := begin let fc : F → 𝕜 := λ x, (fr x : 𝕜) - (I : 𝕜) * (fr ((I : 𝕜) • x)), have add : ∀ x y : F, fc (x + y) = fc x + fc y, { assume x y, simp only [fc], simp only [smul_add, linear_map.map_add, of_real_add], rw mul_add, abel, }, have A : ∀ (c : ℝ) (x : F), (fr ((c : 𝕜) • x) : 𝕜) = (c : 𝕜) * (fr x : 𝕜), { assume c x, rw [← of_real_mul], congr' 1, rw [is_R_or_C.of_real_alg, smul_assoc, fr.map_smul, algebra.id.smul_eq_mul, one_smul] }, have smul_ℝ : ∀ (c : ℝ) (x : F), fc ((c : 𝕜) • x) = (c : 𝕜) * fc x, { assume c x, simp only [fc, A], rw A c x, rw [smul_smul, mul_comm I (c : 𝕜), ← smul_smul, A, mul_sub], ring }, have smul_I : ∀ x : F, fc ((I : 𝕜) • x) = (I : 𝕜) * fc x, { assume x, simp only [fc], cases @I_mul_I_ax 𝕜 _ with h h, { simp [h] }, rw [mul_sub, ← mul_assoc, smul_smul, h], simp only [neg_mul, linear_map.map_neg, one_mul, one_smul, mul_neg, of_real_neg, neg_smul, sub_neg_eq_add, add_comm] }, have smul_𝕜 : ∀ (c : 𝕜) (x : F), fc (c • x) = c • fc x, { assume c x, rw [← re_add_im c, add_smul, add_smul, add, smul_ℝ, ← smul_smul, smul_ℝ, smul_I, ← mul_assoc], refl }, exact { to_fun := fc, map_add' := add, map_smul' := smul_𝕜 } end lemma linear_map.extend_to_𝕜'_apply [module ℝ F] [is_scalar_tower ℝ 𝕜 F] (fr : F →ₗ[ℝ] ℝ) (x : F) : fr.extend_to_𝕜' x = (fr x : 𝕜) - (I : 𝕜) * fr ((I : 𝕜) • x) := rfl /-- The norm of the extension is bounded by `∥fr∥`. -/ lemma norm_bound [normed_space ℝ F] [is_scalar_tower ℝ 𝕜 F] (fr : F →L[ℝ] ℝ) (x : F) : ∥(fr.to_linear_map.extend_to_𝕜' x : 𝕜)∥ ≤ ∥fr∥ * ∥x∥ := begin let lm : F →ₗ[𝕜] 𝕜 := fr.to_linear_map.extend_to_𝕜', -- We aim to find a `t : 𝕜` such that -- * `lm (t • x) = fr (t • x)` (so `lm (t • x) = t * lm x ∈ ℝ`) -- * `∥lm x∥ = ∥lm (t • x)∥` (so `t.abs` must be 1) -- If `lm x ≠ 0`, `(lm x)⁻¹` satisfies the first requirement, and after normalizing, it -- satisfies the second. -- (If `lm x = 0`, the goal is trivial.) classical, by_cases h : lm x = 0, { rw [h, norm_zero], apply mul_nonneg; exact norm_nonneg _ }, let fx := (lm x)⁻¹, let t := fx / (abs𝕜 fx : 𝕜), have ht : abs𝕜 t = 1, by field_simp [abs_of_real, of_real_inv, is_R_or_C.abs_inv, is_R_or_C.abs_div, is_R_or_C.abs_abs, h], have h1 : (fr (t • x) : 𝕜) = lm (t • x), { apply ext, { simp only [lm, of_real_re, linear_map.extend_to_𝕜'_apply, mul_re, I_re, of_real_im, zero_mul, add_monoid_hom.map_sub, sub_zero, mul_zero], refl }, { symmetry, calc im (lm (t • x)) = im (t * lm x) : by rw [lm.map_smul, smul_eq_mul] ... = im ((lm x)⁻¹ / (abs𝕜 (lm x)⁻¹) * lm x) : rfl ... = im (1 / (abs𝕜 (lm x)⁻¹ : 𝕜)) : by rw [div_mul_eq_mul_div, inv_mul_cancel h] ... = 0 : by rw [← of_real_one, ← of_real_div, of_real_im] ... = im (fr (t • x) : 𝕜) : by rw [of_real_im] } }, calc ∥lm x∥ = abs𝕜 t * ∥lm x∥ : by rw [ht, one_mul] ... = ∥t * lm x∥ : by rw [← norm_eq_abs, norm_mul] ... = ∥lm (t • x)∥ : by rw [←smul_eq_mul, lm.map_smul] ... = ∥(fr (t • x) : 𝕜)∥ : by rw h1 ... = ∥fr (t • x)∥ : by rw [norm_eq_abs, abs_of_real, norm_eq_abs, abs_to_real] ... ≤ ∥fr∥ * ∥t • x∥ : continuous_linear_map.le_op_norm _ _ ... = ∥fr∥ * (∥t∥ * ∥x∥) : by rw norm_smul ... ≤ ∥fr∥ * ∥x∥ : by rw [norm_eq_abs, ht, one_mul] end /-- Extend `fr : F →L[ℝ] ℝ` to `F →L[𝕜] 𝕜`. -/ noncomputable def continuous_linear_map.extend_to_𝕜' [normed_space ℝ F] [is_scalar_tower ℝ 𝕜 F] (fr : F →L[ℝ] ℝ) : F →L[𝕜] 𝕜 := linear_map.mk_continuous _ (∥fr∥) (norm_bound _) lemma continuous_linear_map.extend_to_𝕜'_apply [normed_space ℝ F] [is_scalar_tower ℝ 𝕜 F] (fr : F →L[ℝ] ℝ) (x : F) : fr.extend_to_𝕜' x = (fr x : 𝕜) - (I : 𝕜) * fr ((I : 𝕜) • x) := rfl /-- Extend `fr : restrict_scalars ℝ 𝕜 F →ₗ[ℝ] ℝ` to `F →ₗ[𝕜] 𝕜`. -/ noncomputable def linear_map.extend_to_𝕜 (fr : (restrict_scalars ℝ 𝕜 F) →ₗ[ℝ] ℝ) : F →ₗ[𝕜] 𝕜 := fr.extend_to_𝕜' lemma linear_map.extend_to_𝕜_apply (fr : (restrict_scalars ℝ 𝕜 F) →ₗ[ℝ] ℝ) (x : F) : fr.extend_to_𝕜 x = (fr x : 𝕜) - (I : 𝕜) * fr ((I : 𝕜) • x : _) := rfl /-- Extend `fr : restrict_scalars ℝ 𝕜 F →L[ℝ] ℝ` to `F →L[𝕜] 𝕜`. -/ noncomputable def continuous_linear_map.extend_to_𝕜 (fr : (restrict_scalars ℝ 𝕜 F) →L[ℝ] ℝ) : F →L[𝕜] 𝕜 := fr.extend_to_𝕜' lemma continuous_linear_map.extend_to_𝕜_apply (fr : (restrict_scalars ℝ 𝕜 F) →L[ℝ] ℝ) (x : F) : fr.extend_to_𝕜 x = (fr x : 𝕜) - (I : 𝕜) * fr ((I : 𝕜) • x : _) := rfl
34e8b76e446cc71191ce96c066ff6cbf061feaf0
63abd62053d479eae5abf4951554e1064a4c45b4
/src/topology/bases.lean
7e03cc8e1e2c5a12c0329657a7ab585502f44221
[ "Apache-2.0" ]
permissive
Lix0120/mathlib
0020745240315ed0e517cbf32e738d8f9811dd80
e14c37827456fc6707f31b4d1d16f1f3a3205e91
refs/heads/master
1,673,102,855,024
1,604,151,044,000
1,604,151,044,000
308,930,245
0
0
Apache-2.0
1,604,164,710,000
1,604,163,547,000
null
UTF-8
Lean
false
false
15,158
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro Bases of topologies. Countability axioms. -/ import topology.continuous_on open set filter classical open_locale topological_space filter noncomputable theory namespace topological_space /- countability axioms For our applications we are interested that there exists a countable basis, but we do not need the concrete basis itself. This allows us to declare these type classes as `Prop` to use them as mixins. -/ universe u variables {α : Type u} [t : topological_space α] include t /-- A topological basis is one that satisfies the necessary conditions so that it suffices to take unions of the basis sets to get a topology (without taking finite intersections as well). -/ def is_topological_basis (s : set (set α)) : Prop := (∀t₁∈s, ∀t₂∈s, ∀ x ∈ t₁ ∩ t₂, ∃ t₃∈s, x ∈ t₃ ∧ t₃ ⊆ t₁ ∩ t₂) ∧ (⋃₀ s) = univ ∧ t = generate_from s lemma is_topological_basis_of_subbasis {s : set (set α)} (hs : t = generate_from s) : is_topological_basis ((λf, ⋂₀ f) '' {f:set (set α) | finite f ∧ f ⊆ s ∧ (⋂₀ f).nonempty}) := let b' := (λf, ⋂₀ f) '' {f:set (set α) | finite f ∧ f ⊆ s ∧ (⋂₀ f).nonempty} in ⟨assume s₁ ⟨t₁, ⟨hft₁, ht₁b, ht₁⟩, eq₁⟩ s₂ ⟨t₂, ⟨hft₂, ht₂b, ht₂⟩, eq₂⟩, have ie : ⋂₀(t₁ ∪ t₂) = ⋂₀ t₁ ∩ ⋂₀ t₂, from Inf_union, eq₁ ▸ eq₂ ▸ assume x h, ⟨_, ⟨t₁ ∪ t₂, ⟨hft₁.union hft₂, union_subset ht₁b ht₂b, ie.symm ▸ ⟨_, h⟩⟩, ie⟩, h, subset.refl _⟩, eq_univ_iff_forall.2 $ assume a, ⟨univ, ⟨∅, ⟨finite_empty, empty_subset _, by rw sInter_empty; exact ⟨a, mem_univ a⟩⟩, sInter_empty⟩, mem_univ _⟩, have generate_from s = generate_from b', from le_antisymm (le_generate_from $ assume u ⟨t, ⟨hft, htb, ne⟩, eq⟩, eq ▸ @is_open_sInter _ (generate_from s) _ hft (assume s hs, generate_open.basic _ $ htb hs)) (le_generate_from $ assume s hs, s.eq_empty_or_nonempty.elim (assume : s = ∅, by rw [this]; apply @is_open_empty _ _) (assume : s.nonempty, generate_open.basic _ ⟨{s}, ⟨finite_singleton s, singleton_subset_iff.2 hs, by rwa sInter_singleton⟩, sInter_singleton s⟩)), this ▸ hs⟩ lemma is_topological_basis_of_open_of_nhds {s : set (set α)} (h_open : ∀ u ∈ s, is_open u) (h_nhds : ∀(a:α) (u : set α), a ∈ u → is_open u → ∃v ∈ s, a ∈ v ∧ v ⊆ u) : is_topological_basis s := ⟨assume t₁ ht₁ t₂ ht₂ x ⟨xt₁, xt₂⟩, h_nhds x (t₁ ∩ t₂) ⟨xt₁, xt₂⟩ (is_open_inter (h_open _ ht₁) (h_open _ ht₂)), eq_univ_iff_forall.2 $ assume a, let ⟨u, h₁, h₂, _⟩ := h_nhds a univ trivial is_open_univ in ⟨u, h₁, h₂⟩, le_antisymm (le_generate_from h_open) (assume u hu, (@is_open_iff_nhds α (generate_from _) _).mpr $ assume a hau, let ⟨v, hvs, hav, hvu⟩ := h_nhds a u hau hu in by rw nhds_generate_from; exact infi_le_of_le v (infi_le_of_le ⟨hav, hvs⟩ $ le_principal_iff.2 hvu))⟩ lemma mem_nhds_of_is_topological_basis {a : α} {s : set α} {b : set (set α)} (hb : is_topological_basis b) : s ∈ 𝓝 a ↔ ∃t∈b, a ∈ t ∧ t ⊆ s := begin change s ∈ (𝓝 a).sets ↔ ∃t∈b, a ∈ t ∧ t ⊆ s, rw [hb.2.2, nhds_generate_from, binfi_sets_eq], { simp only [mem_bUnion_iff, exists_prop, mem_set_of_eq, and_assoc, and.left_comm], refl }, { exact assume s ⟨hs₁, hs₂⟩ t ⟨ht₁, ht₂⟩, have a ∈ s ∩ t, from ⟨hs₁, ht₁⟩, let ⟨u, hu₁, hu₂, hu₃⟩ := hb.1 _ hs₂ _ ht₂ _ this in ⟨u, ⟨hu₂, hu₁⟩, le_principal_iff.2 (subset.trans hu₃ (inter_subset_left _ _)), le_principal_iff.2 (subset.trans hu₃ (inter_subset_right _ _))⟩ }, { rcases eq_univ_iff_forall.1 hb.2.1 a with ⟨i, h1, h2⟩, exact ⟨i, h2, h1⟩ } end lemma is_open_of_is_topological_basis {s : set α} {b : set (set α)} (hb : is_topological_basis b) (hs : s ∈ b) : is_open s := is_open_iff_mem_nhds.2 $ λ a as, (mem_nhds_of_is_topological_basis hb).2 ⟨s, hs, as, subset.refl _⟩ lemma mem_basis_subset_of_mem_open {b : set (set α)} (hb : is_topological_basis b) {a:α} {u : set α} (au : a ∈ u) (ou : is_open u) : ∃v ∈ b, a ∈ v ∧ v ⊆ u := (mem_nhds_of_is_topological_basis hb).1 $ mem_nhds_sets ou au lemma sUnion_basis_of_is_open {B : set (set α)} (hB : is_topological_basis B) {u : set α} (ou : is_open u) : ∃ S ⊆ B, u = ⋃₀ S := ⟨{s ∈ B | s ⊆ u}, λ s h, h.1, set.ext $ λ a, ⟨λ ha, let ⟨b, hb, ab, bu⟩ := mem_basis_subset_of_mem_open hB ha ou in ⟨b, ⟨hb, bu⟩, ab⟩, λ ⟨b, ⟨hb, bu⟩, ab⟩, bu ab⟩⟩ lemma Union_basis_of_is_open {B : set (set α)} (hB : is_topological_basis B) {u : set α} (ou : is_open u) : ∃ (β : Type u) (f : β → set α), u = (⋃ i, f i) ∧ ∀ i, f i ∈ B := let ⟨S, sb, su⟩ := sUnion_basis_of_is_open hB ou in ⟨S, subtype.val, su.trans set.sUnion_eq_Union, λ ⟨b, h⟩, sb h⟩ variables (α) /-- A separable space is one with a countable dense subset, available through `topological_space.exists_countable_dense`. If `α` is also known to be nonempty, then `topological_space.dense_seq` provides a sequence `ℕ → α` with dense range, see `topological_space.dense_range_dense_seq`. If `α` is a uniform space with countably generated uniformity filter (e.g., an `emetric_space`), then this condition is equivalent to `topological_space.second_countable_topology α`. In this case the latter should be used as a typeclass argument in theorems because Lean can automatically deduce `separable_space` from `second_countable_topology` but it can't deduce `second_countable_topology` and `emetric_space`. -/ class separable_space : Prop := (exists_countable_dense : ∃s:set α, countable s ∧ dense s) lemma exists_countable_dense [separable_space α] : ∃ s : set α, countable s ∧ dense s := separable_space.exists_countable_dense /-- A nonempty separable space admits a sequence with dense range. Instead of running `cases` on the conclusion of this lemma, you might want to use `topological_space.dense_seq` and `topological_space.dense_range_dense_seq`. If `α` might be empty, then `exists_countable_dense` is the main way to use separability of `α`. -/ lemma exists_dense_seq [separable_space α] [nonempty α] : ∃ u : ℕ → α, dense_range u := begin obtain ⟨s : set α, hs, s_dense⟩ := exists_countable_dense α, cases countable_iff_exists_surjective.mp hs with u hu, exact ⟨u, s_dense.mono hu⟩, end /-- A sequence dense in a non-empty separable topological space. If `α` might be empty, then `exists_countable_dense` is the main way to use separability of `α`. -/ def dense_seq [separable_space α] [nonempty α] : ℕ → α := classical.some (exists_dense_seq α) /-- The sequence `dense_seq α` has dense range. -/ @[simp] lemma dense_range_dense_seq [separable_space α] [nonempty α] : dense_range (dense_seq α) := classical.some_spec (exists_dense_seq α) end topological_space open topological_space /-- If `α` is a separable space and `f : α → β` is a continuous map with dense range, then `β` is a separable space as well. E.g., the completion of a separable uniform space is separable. -/ protected lemma dense_range.separable_space {α β : Type*} [topological_space α] [separable_space α] [topological_space β] {f : α → β} (h : dense_range f) (h' : continuous f) : separable_space β := let ⟨s, s_cnt, s_dense⟩ := exists_countable_dense α in ⟨⟨f '' s, countable.image s_cnt f, h.dense_image h' s_dense⟩⟩ namespace topological_space universe u variables (α : Type u) [t : topological_space α] include t /-- A first-countable space is one in which every point has a countable neighborhood basis. -/ class first_countable_topology : Prop := (nhds_generated_countable : ∀a:α, (𝓝 a).is_countably_generated) namespace first_countable_topology variable {α} lemma tendsto_subseq [first_countable_topology α] {u : ℕ → α} {x : α} (hx : map_cluster_pt x at_top u) : ∃ (ψ : ℕ → ℕ), (strict_mono ψ) ∧ (tendsto (u ∘ ψ) at_top (𝓝 x)) := (nhds_generated_countable x).subseq_tendsto hx end first_countable_topology variables {α} lemma is_countably_generated_nhds [first_countable_topology α] (x : α) : is_countably_generated (𝓝 x) := first_countable_topology.nhds_generated_countable x lemma is_countably_generated_nhds_within [first_countable_topology α] (x : α) (s : set α) : is_countably_generated (𝓝[s] x) := (is_countably_generated_nhds x).inf_principal s variable (α) /-- A second-countable space is one with a countable basis. -/ class second_countable_topology : Prop := (is_open_generated_countable [] : ∃b:set (set α), countable b ∧ t = topological_space.generate_from b) @[priority 100] -- see Note [lower instance priority] instance second_countable_topology.to_first_countable_topology [second_countable_topology α] : first_countable_topology α := let ⟨b, hb, eq⟩ := second_countable_topology.is_open_generated_countable α in ⟨begin intros, rw [eq, nhds_generate_from], exact is_countably_generated_binfi_principal (hb.mono (assume x, and.right)), end⟩ lemma second_countable_topology_induced (β) [t : topological_space β] [second_countable_topology β] (f : α → β) : @second_countable_topology α (t.induced f) := begin rcases second_countable_topology.is_open_generated_countable β with ⟨b, hb, eq⟩, refine { is_open_generated_countable := ⟨preimage f '' b, hb.image _, _⟩ }, rw [eq, induced_generate_from_eq] end instance subtype.second_countable_topology (s : set α) [second_countable_topology α] : second_countable_topology s := second_countable_topology_induced s α coe lemma is_open_generated_countable_inter [second_countable_topology α] : ∃b:set (set α), countable b ∧ ∅ ∉ b ∧ is_topological_basis b := let ⟨b, hb₁, hb₂⟩ := second_countable_topology.is_open_generated_countable α in let b' := (λs, ⋂₀ s) '' {s:set (set α) | finite s ∧ s ⊆ b ∧ (⋂₀ s).nonempty} in ⟨b', ((countable_set_of_finite_subset hb₁).mono (by { simp only [← and_assoc], apply inter_subset_left })).image _, assume ⟨s, ⟨_, _, hn⟩, hp⟩, absurd hn (not_nonempty_iff_eq_empty.2 hp), is_topological_basis_of_subbasis hb₂⟩ /- TODO: more fine grained instances for first_countable_topology, separable_space, t2_space, ... -/ instance {β : Type*} [topological_space β] [second_countable_topology α] [second_countable_topology β] : second_countable_topology (α × β) := ⟨let ⟨a, ha₁, ha₂, ha₃, ha₄, ha₅⟩ := is_open_generated_countable_inter α in let ⟨b, hb₁, hb₂, hb₃, hb₄, hb₅⟩ := is_open_generated_countable_inter β in ⟨{g | ∃u∈a, ∃v∈b, g = set.prod u v}, have {g | ∃u∈a, ∃v∈b, g = set.prod u v} = (⋃u∈a, ⋃v∈b, {set.prod u v}), by apply set.ext; simp, by rw [this]; exact (ha₁.bUnion $ assume u hu, hb₁.bUnion $ by simp), by rw [ha₅, hb₅, prod_generate_from_generate_from_eq ha₄ hb₄]⟩⟩ instance second_countable_topology_fintype {ι : Type*} {π : ι → Type*} [fintype ι] [t : ∀a, topological_space (π a)] [sc : ∀a, second_countable_topology (π a)] : second_countable_topology (∀a, π a) := have ∀i, ∃b : set (set (π i)), countable b ∧ ∅ ∉ b ∧ is_topological_basis b, from assume a, @is_open_generated_countable_inter (π a) _ (sc a), let ⟨g, hg⟩ := classical.axiom_of_choice this in have t = (λa, generate_from (g a)), from funext $ assume a, (hg a).2.2.2.2, begin constructor, refine ⟨pi univ '' pi univ g, countable.image _ _, _⟩, { suffices : countable {f : Πa, set (π a) | ∀a, f a ∈ g a}, { simpa [pi] }, exact countable_pi (assume i, (hg i).1), }, rw [this, pi_generate_from_eq_fintype], { congr' 1 with f, simp [pi, eq_comm] }, exact assume a, (hg a).2.2.2.1 end @[priority 100] -- see Note [lower instance priority] instance second_countable_topology.to_separable_space [second_countable_topology α] : separable_space α := begin rcases is_open_generated_countable_inter α with ⟨b, hbc, hbne, hb, hbU, eq⟩, set S : α → set (set α) := λ a, {s : set α | a ∈ s ∧ s ∈ b}, have nhds_eq : ∀a, 𝓝 a = (⨅ s ∈ S a, 𝓟 s), { intro a, rw [eq, nhds_generate_from] }, have : ∀ s ∈ b, set.nonempty s := assume s hs, ne_empty_iff_nonempty.1 $ λ eq, absurd hs (eq.symm ▸ hbne), choose f hf, refine ⟨⟨⋃ s ∈ b, {f s ‹_›}, hbc.bUnion (λ _ _, countable_singleton _), λ a, _⟩⟩, suffices : (⨅ s ∈ S a, 𝓟 (s ∩ ⋃ t ∈ b, {f t ‹_›})).ne_bot, { obtain ⟨t, htb, hta⟩ : a ∈ ⋃₀ b, { simp only [hbU] }, have A : ∃ s, s ∈ S a := ⟨t, hta, htb⟩, simpa only [← inf_principal, mem_closure_iff_cluster_pt, cluster_pt, nhds_eq, binfi_inf A] using this }, rw [infi_subtype'], haveI : nonempty α := ⟨a⟩, refine infi_ne_bot_of_directed _ _, { rintros ⟨s₁, has₁, hs₁⟩ ⟨s₂, has₂, hs₂⟩, obtain ⟨t, htb, hta, ht⟩ : ∃ t ∈ b, a ∈ t ∧ t ⊆ s₁ ∩ s₂, from hb _ hs₁ _ hs₂ a ⟨has₁, has₂⟩, refine ⟨⟨t, hta, htb⟩, _⟩, simp only [subset_inter_iff] at ht, simp only [principal_mono, subtype.coe_mk, (≥)], exact ⟨inter_subset_inter_left _ ht.1, inter_subset_inter_left _ ht.2⟩ }, rintros ⟨s, hsa, hsb⟩, suffices : (s ∩ ⋃ t ∈ b, {f t ‹_›}).nonempty, { simpa [principal_ne_bot_iff] }, refine ⟨_, hf _ hsb, _⟩, simp only [mem_Union], exact ⟨s, hsb, rfl⟩ end variables {α} lemma is_open_Union_countable [second_countable_topology α] {ι} (s : ι → set α) (H : ∀ i, is_open (s i)) : ∃ T : set ι, countable T ∧ (⋃ i ∈ T, s i) = ⋃ i, s i := let ⟨B, cB, _, bB⟩ := is_open_generated_countable_inter α in begin let B' := {b ∈ B | ∃ i, b ⊆ s i}, choose f hf using λ b:B', b.2.2, haveI : encodable B' := (cB.mono (sep_subset _ _)).to_encodable, refine ⟨_, countable_range f, subset.antisymm (bUnion_subset_Union _ _) (sUnion_subset _)⟩, rintro _ ⟨i, rfl⟩ x xs, rcases mem_basis_subset_of_mem_open bB xs (H _) with ⟨b, hb, xb, bs⟩, exact ⟨_, ⟨_, rfl⟩, _, ⟨⟨⟨_, hb, _, bs⟩, rfl⟩, rfl⟩, hf _ (by exact xb)⟩ end lemma is_open_sUnion_countable [second_countable_topology α] (S : set (set α)) (H : ∀ s ∈ S, is_open s) : ∃ T : set (set α), countable T ∧ T ⊆ S ∧ ⋃₀ T = ⋃₀ S := let ⟨T, cT, hT⟩ := is_open_Union_countable (λ s:S, s.1) (λ s, H s.1 s.2) in ⟨subtype.val '' T, cT.image _, image_subset_iff.2 $ λ ⟨x, xs⟩ xt, xs, by rwa [sUnion_image, sUnion_eq_Union]⟩ end topological_space
c04b318b9c79b157f40bb7576538adba3a1e045e
9dc8cecdf3c4634764a18254e94d43da07142918
/src/data/polynomial/div.lean
0ee1356baca2ed37efb0d5cd55490ebfcd5a1a48
[ "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
21,146
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker -/ import data.polynomial.inductions import data.polynomial.monic import ring_theory.multiplicity /-! # Division of univariate polynomials The main defs are `div_by_monic` and `mod_by_monic`. The compatibility between these is given by `mod_by_monic_add_div`. We also define `root_multiplicity`. -/ noncomputable theory open_locale classical big_operators polynomial open finset namespace polynomial universes u v w z variables {R : Type u} {S : Type v} {T : Type w} {A : Type z} {a b : R} {n : ℕ} section comm_semiring variables [comm_semiring R] theorem X_dvd_iff {α : Type u} [comm_semiring α] {f : α[X]} : X ∣ f ↔ f.coeff 0 = 0 := ⟨λ ⟨g, hfg⟩, by rw [hfg, mul_comm, coeff_mul_X_zero], λ hf, ⟨f.div_X, by rw [mul_comm, ← add_zero (f.div_X * X), ← C_0, ← hf, div_X_mul_X_add]⟩⟩ end comm_semiring section comm_semiring variables [comm_semiring R] {p q : R[X]} lemma multiplicity_finite_of_degree_pos_of_monic (hp : (0 : with_bot ℕ) < degree p) (hmp : monic p) (hq : q ≠ 0) : multiplicity.finite p q := have zn0 : (0 : R) ≠ 1, by haveI := nontrivial.of_polynomial_ne hq; exact zero_ne_one, ⟨nat_degree q, λ ⟨r, hr⟩, have hp0 : p ≠ 0, from λ hp0, by simp [hp0] at hp; contradiction, have hr0 : r ≠ 0, from λ hr0, by simp * at *, have hpn1 : leading_coeff p ^ (nat_degree q + 1) = 1, by simp [show _ = _, from hmp], have hpn0' : leading_coeff p ^ (nat_degree q + 1) ≠ 0, from hpn1.symm ▸ zn0.symm, have hpnr0 : leading_coeff (p ^ (nat_degree q + 1)) * leading_coeff r ≠ 0, by simp only [leading_coeff_pow' hpn0', leading_coeff_eq_zero, hpn1, one_pow, one_mul, ne.def, hr0]; simp, have hnp : 0 < nat_degree p, by rw [← with_bot.coe_lt_coe, ← degree_eq_nat_degree hp0]; exact hp, begin have := congr_arg nat_degree hr, rw [nat_degree_mul' hpnr0, nat_degree_pow' hpn0', add_mul, add_assoc] at this, exact ne_of_lt (lt_add_of_le_of_pos (le_mul_of_one_le_right (nat.zero_le _) hnp) (add_pos_of_pos_of_nonneg (by rwa one_mul) (nat.zero_le _))) this end⟩ end comm_semiring section ring variables [ring R] {p q : R[X]} lemma div_wf_lemma (h : degree q ≤ degree p ∧ p ≠ 0) (hq : monic q) : degree (p - C (leading_coeff p) * X ^ (nat_degree p - nat_degree q) * q) < degree p := have hp : leading_coeff p ≠ 0 := mt leading_coeff_eq_zero.1 h.2, have hq0 : q ≠ 0 := hq.ne_zero_of_polynomial_ne h.2, have hlt : nat_degree q ≤ nat_degree p := with_bot.coe_le_coe.1 (by rw [← degree_eq_nat_degree h.2, ← degree_eq_nat_degree hq0]; exact h.1), degree_sub_lt (by rw [hq.degree_mul, degree_C_mul_X_pow _ hp, degree_eq_nat_degree h.2, degree_eq_nat_degree hq0, ← with_bot.coe_add, tsub_add_cancel_of_le hlt]) h.2 (by rw [leading_coeff_mul_monic hq, leading_coeff_mul_X_pow, leading_coeff_C]) /-- See `div_by_monic`. -/ noncomputable def div_mod_by_monic_aux : Π (p : R[X]) {q : R[X]}, monic q → R[X] × R[X] | p := λ q hq, if h : degree q ≤ degree p ∧ p ≠ 0 then let z := C (leading_coeff p) * X^(nat_degree p - nat_degree q) in have wf : _ := div_wf_lemma h hq, let dm := div_mod_by_monic_aux (p - z * q) hq in ⟨z + dm.1, dm.2⟩ else ⟨0, p⟩ using_well_founded {dec_tac := tactic.assumption} /-- `div_by_monic` gives the quotient of `p` by a monic polynomial `q`. -/ def div_by_monic (p q : R[X]) : R[X] := if hq : monic q then (div_mod_by_monic_aux p hq).1 else 0 /-- `mod_by_monic` gives the remainder of `p` by a monic polynomial `q`. -/ def mod_by_monic (p q : R[X]) : R[X] := if hq : monic q then (div_mod_by_monic_aux p hq).2 else p infixl ` /ₘ ` : 70 := div_by_monic infixl ` %ₘ ` : 70 := mod_by_monic lemma degree_mod_by_monic_lt [nontrivial R] : ∀ (p : R[X]) {q : R[X]} (hq : monic q), degree (p %ₘ q) < degree q | p := λ q hq, if h : degree q ≤ degree p ∧ p ≠ 0 then have wf : _ := div_wf_lemma ⟨h.1, h.2⟩ hq, have degree ((p - C (leading_coeff p) * X ^ (nat_degree p - nat_degree q) * q) %ₘ q) < degree q := degree_mod_by_monic_lt (p - C (leading_coeff p) * X ^ (nat_degree p - nat_degree q) * q) hq, begin unfold mod_by_monic at this ⊢, unfold div_mod_by_monic_aux, rw dif_pos hq at this ⊢, rw if_pos h, exact this end else or.cases_on (not_and_distrib.1 h) begin unfold mod_by_monic div_mod_by_monic_aux, rw [dif_pos hq, if_neg h], exact lt_of_not_ge, end begin assume hp, unfold mod_by_monic div_mod_by_monic_aux, rw [dif_pos hq, if_neg h, not_not.1 hp], exact lt_of_le_of_ne bot_le (ne.symm (mt degree_eq_bot.1 hq.ne_zero)), end using_well_founded {dec_tac := tactic.assumption} @[simp] lemma zero_mod_by_monic (p : R[X]) : 0 %ₘ p = 0 := begin unfold mod_by_monic div_mod_by_monic_aux, by_cases hp : monic p, { rw [dif_pos hp, if_neg (mt and.right (not_not_intro rfl))] }, { rw [dif_neg hp] } end @[simp] lemma zero_div_by_monic (p : R[X]) : 0 /ₘ p = 0 := begin unfold div_by_monic div_mod_by_monic_aux, by_cases hp : monic p, { rw [dif_pos hp, if_neg (mt and.right (not_not_intro rfl))] }, { rw [dif_neg hp] } end @[simp] lemma mod_by_monic_zero (p : R[X]) : p %ₘ 0 = p := if h : monic (0 : R[X]) then by { haveI := monic_zero_iff_subsingleton.mp h, simp } else by unfold mod_by_monic div_mod_by_monic_aux; rw dif_neg h @[simp] lemma div_by_monic_zero (p : R[X]) : p /ₘ 0 = 0 := if h : monic (0 : R[X]) then by { haveI := monic_zero_iff_subsingleton.mp h, simp } else by unfold div_by_monic div_mod_by_monic_aux; rw dif_neg h lemma div_by_monic_eq_of_not_monic (p : R[X]) (hq : ¬monic q) : p /ₘ q = 0 := dif_neg hq lemma mod_by_monic_eq_of_not_monic (p : R[X]) (hq : ¬monic q) : p %ₘ q = p := dif_neg hq lemma mod_by_monic_eq_self_iff [nontrivial R] (hq : monic q) : p %ₘ q = p ↔ degree p < degree q := ⟨λ h, h ▸ degree_mod_by_monic_lt _ hq, λ h, have ¬ degree q ≤ degree p := not_le_of_gt h, by unfold mod_by_monic div_mod_by_monic_aux; rw [dif_pos hq, if_neg (mt and.left this)]⟩ theorem degree_mod_by_monic_le (p : R[X]) {q : R[X]} (hq : monic q) : degree (p %ₘ q) ≤ degree q := by { nontriviality R, exact (degree_mod_by_monic_lt _ hq).le } end ring section comm_ring variables [comm_ring R] {p q : R[X]} lemma mod_by_monic_eq_sub_mul_div : ∀ (p : R[X]) {q : R[X]} (hq : monic q), p %ₘ q = p - q * (p /ₘ q) | p := λ q hq, if h : degree q ≤ degree p ∧ p ≠ 0 then have wf : _ := div_wf_lemma h hq, have ih : _ := mod_by_monic_eq_sub_mul_div (p - C (leading_coeff p) * X ^ (nat_degree p - nat_degree q) * q) hq, begin unfold mod_by_monic div_by_monic div_mod_by_monic_aux, rw [dif_pos hq, if_pos h], rw [mod_by_monic, dif_pos hq] at ih, refine ih.trans _, unfold div_by_monic, rw [dif_pos hq, dif_pos hq, if_pos h, mul_add, sub_add_eq_sub_sub, mul_comm] end else begin unfold mod_by_monic div_by_monic div_mod_by_monic_aux, rw [dif_pos hq, if_neg h, dif_pos hq, if_neg h, mul_zero, sub_zero] end using_well_founded {dec_tac := tactic.assumption} lemma mod_by_monic_add_div (p : R[X]) {q : R[X]} (hq : monic q) : p %ₘ q + q * (p /ₘ q) = p := eq_sub_iff_add_eq.1 (mod_by_monic_eq_sub_mul_div p hq) lemma div_by_monic_eq_zero_iff [nontrivial R] (hq : monic q) : p /ₘ q = 0 ↔ degree p < degree q := ⟨λ h, by have := mod_by_monic_add_div p hq; rwa [h, mul_zero, add_zero, mod_by_monic_eq_self_iff hq] at this, λ h, have ¬ degree q ≤ degree p := not_le_of_gt h, by unfold div_by_monic div_mod_by_monic_aux; rw [dif_pos hq, if_neg (mt and.left this)]⟩ lemma degree_add_div_by_monic (hq : monic q) (h : degree q ≤ degree p) : degree q + degree (p /ₘ q) = degree p := begin nontriviality R, have hdiv0 : p /ₘ q ≠ 0 := by rwa [(≠), div_by_monic_eq_zero_iff hq, not_lt], have hlc : leading_coeff q * leading_coeff (p /ₘ q) ≠ 0 := by rwa [monic.def.1 hq, one_mul, (≠), leading_coeff_eq_zero], have hmod : degree (p %ₘ q) < degree (q * (p /ₘ q)) := calc degree (p %ₘ q) < degree q : degree_mod_by_monic_lt _ hq ... ≤ _ : by rw [degree_mul' hlc, degree_eq_nat_degree hq.ne_zero, degree_eq_nat_degree hdiv0, ← with_bot.coe_add, with_bot.coe_le_coe]; exact nat.le_add_right _ _, calc degree q + degree (p /ₘ q) = degree (q * (p /ₘ q)) : eq.symm (degree_mul' hlc) ... = degree (p %ₘ q + q * (p /ₘ q)) : (degree_add_eq_right_of_degree_lt hmod).symm ... = _ : congr_arg _ (mod_by_monic_add_div _ hq) end lemma degree_div_by_monic_le (p q : R[X]) : degree (p /ₘ q) ≤ degree p := if hp0 : p = 0 then by simp only [hp0, zero_div_by_monic, le_refl] else if hq : monic q then if h : degree q ≤ degree p then by haveI := nontrivial.of_polynomial_ne hp0; rw [← degree_add_div_by_monic hq h, degree_eq_nat_degree hq.ne_zero, degree_eq_nat_degree (mt (div_by_monic_eq_zero_iff hq).1 (not_lt.2 h))]; exact with_bot.coe_le_coe.2 (nat.le_add_left _ _) else by unfold div_by_monic div_mod_by_monic_aux; simp only [dif_pos hq, h, false_and, if_false, degree_zero, bot_le] else (div_by_monic_eq_of_not_monic p hq).symm ▸ bot_le lemma degree_div_by_monic_lt (p : R[X]) {q : R[X]} (hq : monic q) (hp0 : p ≠ 0) (h0q : 0 < degree q) : degree (p /ₘ q) < degree p := if hpq : degree p < degree q then begin haveI := nontrivial.of_polynomial_ne hp0, rw [(div_by_monic_eq_zero_iff hq).2 hpq, degree_eq_nat_degree hp0], exact with_bot.bot_lt_coe _ end else begin haveI := nontrivial.of_polynomial_ne hp0, rw [← degree_add_div_by_monic hq (not_lt.1 hpq), degree_eq_nat_degree hq.ne_zero, degree_eq_nat_degree (mt (div_by_monic_eq_zero_iff hq).1 hpq)], exact with_bot.coe_lt_coe.2 (nat.lt_add_of_pos_left (with_bot.coe_lt_coe.1 $ (degree_eq_nat_degree hq.ne_zero) ▸ h0q)) end theorem nat_degree_div_by_monic {R : Type u} [comm_ring R] (f : R[X]) {g : R[X]} (hg : g.monic) : nat_degree (f /ₘ g) = nat_degree f - nat_degree g := begin nontriviality R, by_cases hfg : f /ₘ g = 0, { rw [hfg, nat_degree_zero], rw div_by_monic_eq_zero_iff hg at hfg, rw tsub_eq_zero_iff_le.mpr (nat_degree_le_nat_degree $ le_of_lt hfg) }, have hgf := hfg, rw div_by_monic_eq_zero_iff hg at hgf, push_neg at hgf, have := degree_add_div_by_monic hg hgf, have hf : f ≠ 0, { intro hf, apply hfg, rw [hf, zero_div_by_monic] }, rw [degree_eq_nat_degree hf, degree_eq_nat_degree hg.ne_zero, degree_eq_nat_degree hfg, ← with_bot.coe_add, with_bot.coe_eq_coe] at this, rw [← this, add_tsub_cancel_left] end lemma div_mod_by_monic_unique {f g} (q r : R[X]) (hg : monic g) (h : r + g * q = f ∧ degree r < degree g) : f /ₘ g = q ∧ f %ₘ g = r := begin nontriviality R, have h₁ : r - f %ₘ g = -g * (q - f /ₘ g), from eq_of_sub_eq_zero (by rw [← sub_eq_zero_of_eq (h.1.trans (mod_by_monic_add_div f hg).symm)]; simp [mul_add, mul_comm, sub_eq_add_neg, add_comm, add_left_comm, add_assoc]), have h₂ : degree (r - f %ₘ g) = degree (g * (q - f /ₘ g)), by simp [h₁], have h₄ : degree (r - f %ₘ g) < degree g, from calc degree (r - f %ₘ g) ≤ max (degree r) (degree (f %ₘ g)) : degree_sub_le _ _ ... < degree g : max_lt_iff.2 ⟨h.2, degree_mod_by_monic_lt _ hg⟩, have h₅ : q - (f /ₘ g) = 0, from by_contradiction (λ hqf, not_le_of_gt h₄ $ calc degree g ≤ degree g + degree (q - f /ₘ g) : by erw [degree_eq_nat_degree hg.ne_zero, degree_eq_nat_degree hqf, with_bot.coe_le_coe]; exact nat.le_add_right _ _ ... = degree (r - f %ₘ g) : by rw [h₂, degree_mul']; simpa [monic.def.1 hg]), exact ⟨eq.symm $ eq_of_sub_eq_zero h₅, eq.symm $ eq_of_sub_eq_zero $ by simpa [h₅] using h₁⟩ end lemma map_mod_div_by_monic [comm_ring S] (f : R →+* S) (hq : monic q) : (p /ₘ q).map f = p.map f /ₘ q.map f ∧ (p %ₘ q).map f = p.map f %ₘ q.map f := begin nontriviality S, haveI : nontrivial R := f.domain_nontrivial, have : map f p /ₘ map f q = map f (p /ₘ q) ∧ map f p %ₘ map f q = map f (p %ₘ q), { exact (div_mod_by_monic_unique ((p /ₘ q).map f) _ (hq.map f) ⟨eq.symm $ by rw [← polynomial.map_mul, ← polynomial.map_add, mod_by_monic_add_div _ hq], calc _ ≤ degree (p %ₘ q) : degree_map_le _ _ ... < degree q : degree_mod_by_monic_lt _ hq ... = _ : eq.symm $ degree_map_eq_of_leading_coeff_ne_zero _ (by rw [monic.def.1 hq, f.map_one]; exact one_ne_zero)⟩) }, exact ⟨this.1.symm, this.2.symm⟩ end lemma map_div_by_monic [comm_ring S] (f : R →+* S) (hq : monic q) : (p /ₘ q).map f = p.map f /ₘ q.map f := (map_mod_div_by_monic f hq).1 lemma map_mod_by_monic [comm_ring S] (f : R →+* S) (hq : monic q) : (p %ₘ q).map f = p.map f %ₘ q.map f := (map_mod_div_by_monic f hq).2 lemma dvd_iff_mod_by_monic_eq_zero (hq : monic q) : p %ₘ q = 0 ↔ q ∣ p := ⟨λ h, by rw [← mod_by_monic_add_div p hq, h, zero_add]; exact dvd_mul_right _ _, λ h, begin nontriviality R, obtain ⟨r, hr⟩ := exists_eq_mul_right_of_dvd h, by_contradiction hpq0, have hmod : p %ₘ q = q * (r - p /ₘ q), { rw [mod_by_monic_eq_sub_mul_div _ hq, mul_sub, ← hr] }, have : degree (q * (r - p /ₘ q)) < degree q := hmod ▸ degree_mod_by_monic_lt _ hq, have hrpq0 : leading_coeff (r - p /ₘ q) ≠ 0 := λ h, hpq0 $ leading_coeff_eq_zero.1 (by rw [hmod, leading_coeff_eq_zero.1 h, mul_zero, leading_coeff_zero]), have hlc : leading_coeff q * leading_coeff (r - p /ₘ q) ≠ 0 := by rwa [monic.def.1 hq, one_mul], rw [degree_mul' hlc, degree_eq_nat_degree hq.ne_zero, degree_eq_nat_degree (mt leading_coeff_eq_zero.2 hrpq0)] at this, exact not_lt_of_ge (nat.le_add_right _ _) (with_bot.some_lt_some.1 this) end⟩ theorem map_dvd_map [comm_ring S] (f : R →+* S) (hf : function.injective f) {x y : R[X]} (hx : x.monic) : x.map f ∣ y.map f ↔ x ∣ y := begin rw [← dvd_iff_mod_by_monic_eq_zero hx, ← dvd_iff_mod_by_monic_eq_zero (hx.map f), ← map_mod_by_monic f hx], exact ⟨λ H, map_injective f hf $ by rw [H, polynomial.map_zero], λ H, by rw [H, polynomial.map_zero]⟩ end @[simp] lemma mod_by_monic_one (p : R[X]) : p %ₘ 1 = 0 := (dvd_iff_mod_by_monic_eq_zero (by convert monic_one)).2 (one_dvd _) @[simp] lemma div_by_monic_one (p : R[X]) : p /ₘ 1 = p := by conv_rhs { rw [← mod_by_monic_add_div p monic_one] }; simp @[simp] lemma mod_by_monic_X_sub_C_eq_C_eval (p : R[X]) (a : R) : p %ₘ (X - C a) = C (p.eval a) := begin nontriviality R, have h : (p %ₘ (X - C a)).eval a = p.eval a, { rw [mod_by_monic_eq_sub_mul_div _ (monic_X_sub_C a), eval_sub, eval_mul, eval_sub, eval_X, eval_C, sub_self, zero_mul, sub_zero] }, have : degree (p %ₘ (X - C a)) < 1 := degree_X_sub_C a ▸ degree_mod_by_monic_lt p (monic_X_sub_C a), have : degree (p %ₘ (X - C a)) ≤ 0, { cases (degree (p %ₘ (X - C a))), { exact bot_le }, { exact with_bot.some_le_some.2 (nat.le_of_lt_succ (with_bot.some_lt_some.1 this)) } }, rw [eq_C_of_degree_le_zero this, eval_C] at h, rw [eq_C_of_degree_le_zero this, h] end lemma mul_div_by_monic_eq_iff_is_root : (X - C a) * (p /ₘ (X - C a)) = p ↔ is_root p a := ⟨λ h, by rw [← h, is_root.def, eval_mul, eval_sub, eval_X, eval_C, sub_self, zero_mul], λ h : p.eval a = 0, by conv {to_rhs, rw ← mod_by_monic_add_div p (monic_X_sub_C a)}; rw [mod_by_monic_X_sub_C_eq_C_eval, h, C_0, zero_add]⟩ lemma dvd_iff_is_root : (X - C a) ∣ p ↔ is_root p a := ⟨λ h, by rwa [← dvd_iff_mod_by_monic_eq_zero (monic_X_sub_C _), mod_by_monic_X_sub_C_eq_C_eval, ← C_0, C_inj] at h, λ h, ⟨(p /ₘ (X - C a)), by rw mul_div_by_monic_eq_iff_is_root.2 h⟩⟩ lemma mod_by_monic_X (p : R[X]) : p %ₘ X = C (p.eval 0) := by rw [← mod_by_monic_X_sub_C_eq_C_eval, C_0, sub_zero] lemma eval₂_mod_by_monic_eq_self_of_root [comm_ring S] {f : R →+* S} {p q : R[X]} (hq : q.monic) {x : S} (hx : q.eval₂ f x = 0) : (p %ₘ q).eval₂ f x = p.eval₂ f x := by rw [mod_by_monic_eq_sub_mul_div p hq, eval₂_sub, eval₂_mul, hx, zero_mul, sub_zero] lemma sum_mod_by_monic_coeff (hq : q.monic) {n : ℕ} (hn : q.degree ≤ n) : ∑ (i : fin n), monomial i ((p %ₘ q).coeff i) = p %ₘ q := begin nontriviality R, exact (sum_fin (λ i c, monomial i c) (by simp) ((degree_mod_by_monic_lt _ hq).trans_le hn)).trans (sum_monomial_eq _) end lemma sub_dvd_eval_sub (a b : R) (p : R[X]) : a - b ∣ p.eval a - p.eval b := begin suffices : X - C b ∣ p - C (p.eval b), { simpa only [coe_eval_ring_hom, eval_sub, eval_X, eval_C] using (eval_ring_hom a).map_dvd this }, simp [dvd_iff_is_root] end variable (R) lemma not_is_field : ¬ is_field R[X] := begin nontriviality R, rw ring.not_is_field_iff_exists_ideal_bot_lt_and_lt_top, use ideal.span {polynomial.X}, split, { rw [bot_lt_iff_ne_bot, ne.def, ideal.span_singleton_eq_bot], exact polynomial.X_ne_zero, }, { rw [lt_top_iff_ne_top, ne.def, ideal.eq_top_iff_one, ideal.mem_span_singleton, polynomial.X_dvd_iff, polynomial.coeff_one_zero], exact one_ne_zero, } end variable {R} section multiplicity /-- An algorithm for deciding polynomial divisibility. The algorithm is "compute `p %ₘ q` and compare to `0`". See `polynomial.mod_by_monic` for the algorithm that computes `%ₘ`. -/ def decidable_dvd_monic (p : R[X]) (hq : monic q) : decidable (q ∣ p) := decidable_of_iff (p %ₘ q = 0) (dvd_iff_mod_by_monic_eq_zero hq) open_locale classical lemma multiplicity_X_sub_C_finite (a : R) (h0 : p ≠ 0) : multiplicity.finite (X - C a) p := begin haveI := nontrivial.of_polynomial_ne h0, refine multiplicity_finite_of_degree_pos_of_monic _ (monic_X_sub_C _) h0, rw degree_X_sub_C, dec_trivial, end /-- The largest power of `X - C a` which divides `p`. This is computable via the divisibility algorithm `polynomial.decidable_dvd_monic`. -/ def root_multiplicity (a : R) (p : R[X]) : ℕ := if h0 : p = 0 then 0 else let I : decidable_pred (λ n : ℕ, ¬(X - C a) ^ (n + 1) ∣ p) := λ n, @not.decidable _ (decidable_dvd_monic p ((monic_X_sub_C a).pow (n + 1))) in by exactI nat.find (multiplicity_X_sub_C_finite a h0) lemma root_multiplicity_eq_multiplicity (p : R[X]) (a : R) : root_multiplicity a p = if h0 : p = 0 then 0 else (multiplicity (X - C a) p).get (multiplicity_X_sub_C_finite a h0) := by simp [multiplicity, root_multiplicity, part.dom]; congr; funext; congr @[simp] lemma root_multiplicity_zero {x : R} : root_multiplicity x 0 = 0 := dif_pos rfl lemma root_multiplicity_eq_zero {p : R[X]} {x : R} (h : ¬ is_root p x) : root_multiplicity x p = 0 := begin rw root_multiplicity_eq_multiplicity, split_ifs, { refl }, rw [← part_enat.coe_inj, part_enat.coe_get, multiplicity.multiplicity_eq_zero_of_not_dvd, nat.cast_zero], intro hdvd, exact h (dvd_iff_is_root.mp hdvd) end lemma root_multiplicity_pos {p : R[X]} (hp : p ≠ 0) {x : R} : 0 < root_multiplicity x p ↔ is_root p x := begin rw [← dvd_iff_is_root, root_multiplicity_eq_multiplicity, dif_neg hp, ← part_enat.coe_lt_coe, part_enat.coe_get], exact multiplicity.dvd_iff_multiplicity_pos end @[simp] lemma root_multiplicity_C (r a : R) : root_multiplicity a (C r) = 0 := begin rcases eq_or_ne r 0 with rfl|hr, { simp }, { exact root_multiplicity_eq_zero (not_is_root_C _ _ hr) } end lemma pow_root_multiplicity_dvd (p : R[X]) (a : R) : (X - C a) ^ root_multiplicity a p ∣ p := if h : p = 0 then by simp [h] else by rw [root_multiplicity_eq_multiplicity, dif_neg h]; exact multiplicity.pow_multiplicity_dvd _ lemma div_by_monic_mul_pow_root_multiplicity_eq (p : R[X]) (a : R) : p /ₘ ((X - C a) ^ root_multiplicity a p) * (X - C a) ^ root_multiplicity a p = p := have monic ((X - C a) ^ root_multiplicity a p), from (monic_X_sub_C _).pow _, by conv_rhs { rw [← mod_by_monic_add_div p this, (dvd_iff_mod_by_monic_eq_zero this).2 (pow_root_multiplicity_dvd _ _)] }; simp [mul_comm] lemma eval_div_by_monic_pow_root_multiplicity_ne_zero {p : R[X]} (a : R) (hp : p ≠ 0) : eval a (p /ₘ ((X - C a) ^ root_multiplicity a p)) ≠ 0 := begin haveI : nontrivial R := nontrivial.of_polynomial_ne hp, rw [ne.def, ← is_root.def, ← dvd_iff_is_root], rintros ⟨q, hq⟩, have := div_by_monic_mul_pow_root_multiplicity_eq p a, rw [mul_comm, hq, ← mul_assoc, ← pow_succ', root_multiplicity_eq_multiplicity, dif_neg hp] at this, exact multiplicity.is_greatest' (multiplicity_finite_of_degree_pos_of_monic (show (0 : with_bot ℕ) < degree (X - C a), by rw degree_X_sub_C; exact dec_trivial) (monic_X_sub_C _) hp) (nat.lt_succ_self _) (dvd_of_mul_right_eq _ this) end end multiplicity end comm_ring end polynomial
075a2bc5f8ff9f8bda3da945df2bba57dc30fc6b
9dc8cecdf3c4634764a18254e94d43da07142918
/src/category_theory/limits/concrete_category.lean
be26475736bd300b20d29d38859d1c30a64a6073
[ "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
12,264
lean
/- Copyright (c) 2017 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Adam Topaz -/ import category_theory.limits.preserves.basic import category_theory.limits.types import category_theory.limits.shapes.wide_pullbacks import category_theory.limits.shapes.multiequalizer import category_theory.concrete_category.elementwise /-! # Facts about (co)limits of functors into concrete categories -/ universes w v u open category_theory namespace category_theory.limits local attribute [instance] concrete_category.has_coe_to_fun concrete_category.has_coe_to_sort section limits variables {C : Type u} [category.{v} C] [concrete_category.{(max w v)} C] {J : Type w} [small_category J] (F : J ⥤ C) [preserves_limit F (forget C)] lemma concrete.to_product_injective_of_is_limit {D : cone F} (hD : is_limit D) : function.injective (λ (x : D.X) (j : J), D.π.app j x) := begin let E := (forget C).map_cone D, let hE : is_limit E := is_limit_of_preserves _ hD, let G := types.limit_cone.{w v} (F ⋙ forget C), let hG := types.limit_cone_is_limit.{w v} (F ⋙ forget C), let T : E.X ≅ G.X := hE.cone_point_unique_up_to_iso hG, change function.injective (T.hom ≫ (λ x j, G.π.app j x)), have h : function.injective T.hom, { intros a b h, suffices : T.inv (T.hom a) = T.inv (T.hom b), by simpa, rw h }, suffices : function.injective (λ (x : G.X) j, G.π.app j x), by exact this.comp h, apply subtype.ext, end lemma concrete.is_limit_ext {D : cone F} (hD : is_limit D) (x y : D.X) : (∀ j, D.π.app j x = D.π.app j y) → x = y := λ h, concrete.to_product_injective_of_is_limit _ hD (funext h) lemma concrete.limit_ext [has_limit F] (x y : limit F) : (∀ j, limit.π F j x = limit.π F j y) → x = y := concrete.is_limit_ext F (limit.is_limit _) _ _ section wide_pullback open wide_pullback open wide_pullback_shape lemma concrete.wide_pullback_ext {B : C} {ι : Type w} {X : ι → C} (f : Π j : ι, X j ⟶ B) [has_wide_pullback B X f] [preserves_limit (wide_cospan B X f) (forget C)] (x y : wide_pullback B X f) (h₀ : base f x = base f y) (h : ∀ j, π f j x = π f j y) : x = y := begin apply concrete.limit_ext, rintro (_|j), { exact h₀ }, { apply h } end lemma concrete.wide_pullback_ext' {B : C} {ι : Type w} [nonempty ι] {X : ι → C} (f : Π j : ι, X j ⟶ B) [has_wide_pullback.{w} B X f] [preserves_limit (wide_cospan B X f) (forget C)] (x y : wide_pullback B X f) (h : ∀ j, π f j x = π f j y) : x = y := begin apply concrete.wide_pullback_ext _ _ _ _ h, inhabit ι, simp only [← π_arrow f (arbitrary _), comp_apply, h], end end wide_pullback section multiequalizer lemma concrete.multiequalizer_ext {I : multicospan_index.{w} C} [has_multiequalizer I] [preserves_limit I.multicospan (forget C)] (x y : multiequalizer I) (h : ∀ (t : I.L), multiequalizer.ι I t x = multiequalizer.ι I t y) : x = y := begin apply concrete.limit_ext, rintros (a|b), { apply h }, { rw [← limit.w I.multicospan (walking_multicospan.hom.fst b), comp_apply, comp_apply, h] } end /-- An auxiliary equivalence to be used in `multiequalizer_equiv` below.-/ def concrete.multiequalizer_equiv_aux (I : multicospan_index C) : (I.multicospan ⋙ (forget C)).sections ≃ { x : Π (i : I.L), I.left i // ∀ (i : I.R), I.fst i (x _) = I.snd i (x _) } := { to_fun := λ x, ⟨λ i, x.1 (walking_multicospan.left _), λ i, begin have a := x.2 (walking_multicospan.hom.fst i), have b := x.2 (walking_multicospan.hom.snd i), rw ← b at a, exact a, end⟩, inv_fun := λ x, { val := λ j, match j with | walking_multicospan.left a := x.1 _ | walking_multicospan.right b := I.fst b (x.1 _) end, property := begin rintros (a|b) (a'|b') (f|f|f), { change (I.multicospan.map (𝟙 _)) _ = _, simp }, { refl }, { dsimp, erw ← x.2 b', refl }, { change (I.multicospan.map (𝟙 _)) _ = _, simp }, end }, left_inv := begin intros x, ext (a|b), { refl }, { change _ = x.val _, rw ← x.2 (walking_multicospan.hom.fst b), refl } end, right_inv := by { intros x, ext i, refl } } /-- The equivalence between the noncomputable multiequalizer and and the concrete multiequalizer. -/ noncomputable def concrete.multiequalizer_equiv (I : multicospan_index.{w} C) [has_multiequalizer I] [preserves_limit I.multicospan (forget C)] : (multiequalizer I : C) ≃ { x : Π (i : I.L), I.left i // ∀ (i : I.R), I.fst i (x _) = I.snd i (x _) } := let h1 := (limit.is_limit I.multicospan), h2 := (is_limit_of_preserves (forget C) h1), E := h2.cone_point_unique_up_to_iso (types.limit_cone_is_limit _) in equiv.trans E.to_equiv (concrete.multiequalizer_equiv_aux I) @[simp] lemma concrete.multiequalizer_equiv_apply (I : multicospan_index.{w} C) [has_multiequalizer I] [preserves_limit I.multicospan (forget C)] (x : multiequalizer I) (i : I.L) : ((concrete.multiequalizer_equiv I) x : Π (i : I.L), I.left i) i = multiequalizer.ι I i x := rfl end multiequalizer -- TODO: Add analogous lemmas about products and equalizers. end limits section colimits -- We don't mark this as an `@[ext]` lemma as we don't always want to work elementwise. lemma cokernel_funext {C : Type*} [category C] [has_zero_morphisms C] [concrete_category C] {M N K : C} {f : M ⟶ N} [has_cokernel f] {g h : cokernel f ⟶ K} (w : ∀ (n : N), g (cokernel.π f n) = h (cokernel.π f n)) : g = h := begin apply coequalizer.hom_ext, apply concrete_category.hom_ext _ _, simpa using w, end variables {C : Type u} [category.{v} C] [concrete_category.{v} C] {J : Type v} [small_category J] (F : J ⥤ C) [preserves_colimit F (forget C)] lemma concrete.from_union_surjective_of_is_colimit {D : cocone F} (hD : is_colimit D) : let ff : (Σ (j : J), F.obj j) → D.X := λ a, D.ι.app a.1 a.2 in function.surjective ff := begin intro ff, let E := (forget C).map_cocone D, let hE : is_colimit E := is_colimit_of_preserves _ hD, let G := types.colimit_cocone.{v v} (F ⋙ forget C), let hG := types.colimit_cocone_is_colimit.{v v} (F ⋙ forget C), let T : E ≅ G := hE.unique_up_to_iso hG, let TX : E.X ≅ G.X := (cocones.forget _).map_iso T, suffices : function.surjective (TX.hom ∘ ff), { intro a, obtain ⟨b, hb⟩ := this (TX.hom a), refine ⟨b, _⟩, apply_fun TX.inv at hb, change (TX.hom ≫ TX.inv) (ff b) = (TX.hom ≫ TX.inv) _ at hb, simpa only [TX.hom_inv_id] using hb }, have : TX.hom ∘ ff = λ a, G.ι.app a.1 a.2, { ext a, change (E.ι.app a.1 ≫ hE.desc G) a.2 = _, rw hE.fac }, rw this, rintro ⟨⟨j,a⟩⟩, exact ⟨⟨j,a⟩,rfl⟩, end lemma concrete.is_colimit_exists_rep {D : cocone F} (hD : is_colimit D) (x : D.X) : ∃ (j : J) (y : F.obj j), D.ι.app j y = x := begin obtain ⟨a, rfl⟩ := concrete.from_union_surjective_of_is_colimit F hD x, exact ⟨a.1, a.2, rfl⟩, end lemma concrete.colimit_exists_rep [has_colimit F] (x : colimit F) : ∃ (j : J) (y : F.obj j), colimit.ι F j y = x := concrete.is_colimit_exists_rep F (colimit.is_colimit _) x lemma concrete.is_colimit_rep_eq_of_exists {D : cocone F} {i j : J} (hD : is_colimit D) (x : F.obj i) (y : F.obj j) (h : ∃ k (f : i ⟶ k) (g : j ⟶ k), F.map f x = F.map g y) : D.ι.app i x = D.ι.app j y := begin let E := (forget C).map_cocone D, let hE : is_colimit E := is_colimit_of_preserves _ hD, let G := types.colimit_cocone.{v v} (F ⋙ forget C), let hG := types.colimit_cocone_is_colimit.{v v} (F ⋙ forget C), let T : E ≅ G := hE.unique_up_to_iso hG, let TX : E.X ≅ G.X := (cocones.forget _).map_iso T, apply_fun TX.hom, swap, { suffices : function.bijective TX.hom, by exact this.1, rw ← is_iso_iff_bijective, apply is_iso.of_iso }, change (E.ι.app i ≫ TX.hom) x = (E.ι.app j ≫ TX.hom) y, erw [T.hom.w, T.hom.w], obtain ⟨k, f, g, h⟩ := h, have : G.ι.app i x = (G.ι.app k (F.map f x) : G.X) := quot.sound ⟨f,rfl⟩, rw [this, h], symmetry, exact quot.sound ⟨g,rfl⟩, end lemma concrete.colimit_rep_eq_of_exists [has_colimit F] {i j : J} (x : F.obj i) (y : F.obj j) (h : ∃ k (f : i ⟶ k) (g : j ⟶ k), F.map f x = F.map g y) : colimit.ι F i x = colimit.ι F j y := concrete.is_colimit_rep_eq_of_exists F (colimit.is_colimit _) x y h section filtered_colimits variable [is_filtered J] lemma concrete.is_colimit_exists_of_rep_eq {D : cocone F} {i j : J} (hD : is_colimit D) (x : F.obj i) (y : F.obj j) (h : D.ι.app _ x = D.ι.app _ y) : ∃ k (f : i ⟶ k) (g : j ⟶ k), F.map f x = F.map g y := begin let E := (forget C).map_cocone D, let hE : is_colimit E := is_colimit_of_preserves _ hD, let G := types.colimit_cocone.{v v} (F ⋙ forget C), let hG := types.colimit_cocone_is_colimit.{v v} (F ⋙ forget C), let T : E ≅ G := hE.unique_up_to_iso hG, let TX : E.X ≅ G.X := (cocones.forget _).map_iso T, apply_fun TX.hom at h, change (E.ι.app i ≫ TX.hom) x = (E.ι.app j ≫ TX.hom) y at h, erw [T.hom.w, T.hom.w] at h, replace h := quot.exact _ h, suffices : ∀ (a b : Σ j, F.obj j) (h : eqv_gen (limits.types.quot.rel.{v v} (F ⋙ forget C)) a b), ∃ k (f : a.1 ⟶ k) (g : b.1 ⟶ k), F.map f a.2 = F.map g b.2, { exact this ⟨i,x⟩ ⟨j,y⟩ h }, intros a b h, induction h, case eqv_gen.rel : x y hh { obtain ⟨e,he⟩ := hh, use [y.1, e, 𝟙 _], simpa using he.symm }, case eqv_gen.refl : x { use [x.1, 𝟙 _, 𝟙 _, rfl] }, case eqv_gen.symm : x y _ hh { obtain ⟨k, f, g, hh⟩ := hh, use [k, g, f, hh.symm] }, case eqv_gen.trans : x y z _ _ hh1 hh2 { obtain ⟨k1, f1, g1, h1⟩ := hh1, obtain ⟨k2, f2, g2, h2⟩ := hh2, let k0 : J := is_filtered.max k1 k2, let e1 : k1 ⟶ k0 := is_filtered.left_to_max _ _, let e2 : k2 ⟶ k0 := is_filtered.right_to_max _ _, let k : J := is_filtered.coeq (g1 ≫ e1) (f2 ≫ e2), let e : k0 ⟶ k := is_filtered.coeq_hom _ _, use [k, f1 ≫ e1 ≫ e, g2 ≫ e2 ≫ e], simp only [F.map_comp, comp_apply, h1, ← h2], simp only [← comp_apply, ← F.map_comp], rw is_filtered.coeq_condition }, end theorem concrete.is_colimit_rep_eq_iff_exists {D : cocone F} {i j : J} (hD : is_colimit D) (x : F.obj i) (y : F.obj j) : D.ι.app i x = D.ι.app j y ↔ ∃ k (f : i ⟶ k) (g : j ⟶ k), F.map f x = F.map g y := ⟨concrete.is_colimit_exists_of_rep_eq _ hD _ _, concrete.is_colimit_rep_eq_of_exists _ hD _ _⟩ lemma concrete.colimit_exists_of_rep_eq [has_colimit F] {i j : J} (x : F.obj i) (y : F.obj j) (h : colimit.ι F _ x = colimit.ι F _ y) : ∃ k (f : i ⟶ k) (g : j ⟶ k), F.map f x = F.map g y := concrete.is_colimit_exists_of_rep_eq F (colimit.is_colimit _) x y h theorem concrete.colimit_rep_eq_iff_exists [has_colimit F] {i j : J} (x : F.obj i) (y : F.obj j) : colimit.ι F i x = colimit.ι F j y ↔ ∃ k (f : i ⟶ k) (g : j ⟶ k), F.map f x = F.map g y := ⟨concrete.colimit_exists_of_rep_eq _ _ _, concrete.colimit_rep_eq_of_exists _ _ _⟩ end filtered_colimits section wide_pushout open wide_pushout open wide_pushout_shape lemma concrete.wide_pushout_exists_rep {B : C} {α : Type*} {X : α → C} (f : Π j : α, B ⟶ X j) [has_wide_pushout.{v} B X f] [preserves_colimit (wide_span B X f) (forget C)] (x : wide_pushout B X f) : (∃ y : B, head f y = x) ∨ (∃ (i : α) (y : X i), ι f i y = x) := begin obtain ⟨_ | j, y, rfl⟩ := concrete.colimit_exists_rep _ x, { use y }, { right, use [j,y] } end lemma concrete.wide_pushout_exists_rep' {B : C} {α : Type*} [nonempty α] {X : α → C} (f : Π j : α, B ⟶ X j) [has_wide_pushout.{v} B X f] [preserves_colimit (wide_span B X f) (forget C)] (x : wide_pushout B X f) : ∃ (i : α) (y : X i), ι f i y = x := begin rcases concrete.wide_pushout_exists_rep f x with ⟨y, rfl⟩ | ⟨i, y, rfl⟩, { inhabit α, use [arbitrary _, f _ y], simp only [← arrow_ι _ (arbitrary α), comp_apply] }, { use [i,y] } end end wide_pushout -- TODO: Add analogous lemmas about coproducts and coequalizers. end colimits end category_theory.limits
c8ec35c271bcbc43af3318ceb7d2bd44bc80289e
a7602958ab456501ff85db8cf5553f7bcab201d7
/Exam2/exam2.lean
b7857c49efd38c77ed8cd9facb2d9774e423dc06
[]
no_license
enlauren/math-logic
081e2e737c8afb28dbb337968df95ead47321ba0
086b6935543d1841f1db92d0e49add1124054c37
refs/heads/master
1,594,506,621,950
1,558,634,976,000
1,558,634,976,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
947
lean
universe u -- 1. #check implies -- 2. #check prod -- 3. #check fun {a: Type} (x y : a), y -- 4. #check Type -- 5. def double (x: ℕ): ℕ := x + x def twice {a: Type u} (f: a → a) (g: a) := f (f g) #check twice #reduce twice twice double 2 -- 6. def fourTimes {a: Type u}: (a → a) → a → a := twice twice #reduce fourTimes double 2 -- 7. def eightTimes: ℕ → ℕ := fourTimes double #reduce eightTimes 2 -- 8. -- Look at Assignment6. -- 9. variable A: Prop section variable h1: A ↔ ¬ A example: ¬ A := assume h2: A, show false, from iff.elim_left h1 h2 h2 end -- 10. section variable h1: A ↔ ¬ A variable h3: ¬ A example: A := iff.elim_right h1 h3 end -- 11. section variable h1: ¬ (A ∨ ¬ A) example: ¬ A := assume h2: A, show false, from h1 (or.inl h2) end -- 12. -- The way Cheng started the problem, it is not possible -- without classical logic (proof by contradiction).
ea42757136949b401bdf17b4b00c9257361e31af
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/set_theory/cardinal_ordinal.lean
fae73f40052f13c0a451ca0601d94bb9fbdbfc67
[ "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
36,455
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Floris van Doorn -/ import set_theory.ordinal_arithmetic import tactic.linarith /-! # Cardinals and ordinals Relationships between cardinals and ordinals, properties of cardinals that are proved using ordinals. ## Main definitions * The function `cardinal.aleph'` gives the cardinals listed by their ordinal index, and is the inverse of `cardinal.aleph_idx`. `aleph' n = n`, `aleph' ω = cardinal.omega = ℵ₀`, `aleph' (ω + 1) = ℵ₁`, etc. It is an order isomorphism between ordinals and cardinals. * The function `cardinal.aleph` gives the infinite cardinals listed by their ordinal index. `aleph 0 = cardinal.omega = ℵ₀`, `aleph 1 = ℵ₁` is the first uncountable cardinal, and so on. ## Main Statements * `cardinal.mul_eq_max` and `cardinal.add_eq_max` state that the product (resp. sum) of two infinite cardinals is just their maximum. Several variations around this fact are also given. * `cardinal.mk_list_eq_mk` : when `α` is infinite, `α` and `list α` have the same cardinality. * simp lemmas for inequalities between `bit0 a` and `bit1 b` are registered, making `simp` able to prove inequalities about numeral cardinals. ## Tags cardinal arithmetic (for infinite cardinals) -/ noncomputable theory open function cardinal set equiv open_locale classical cardinal universes u v w namespace cardinal section using_ordinals open ordinal theorem ord_is_limit {c} (co : omega ≤ c) : (ord c).is_limit := begin refine ⟨λ h, omega_ne_zero _, λ a, lt_imp_lt_of_le_imp_le _⟩, { rw [← ordinal.le_zero, ord_le] at h, simpa only [card_zero, nonpos_iff_eq_zero] using le_trans co h }, { intro h, rw [ord_le] at h ⊢, rwa [← @add_one_of_omega_le (card a), ← card_succ], rw [← ord_le, ← le_succ_of_is_limit, ord_le], { exact le_trans co h }, { rw ord_omega, exact omega_is_limit } } end /-- The `aleph'` index function, which gives the ordinal index of a cardinal. (The `aleph'` part is because unlike `aleph` this counts also the finite stages. So `aleph_idx n = n`, `aleph_idx ω = ω`, `aleph_idx ℵ₁ = ω + 1` and so on.) In this definition, we register additionally that this function is an initial segment, i.e., it is order preserving and its range is an initial segment of the ordinals. For the basic function version, see `aleph_idx`. For an upgraded version stating that the range is everything, see `aleph_idx.rel_iso`. -/ def aleph_idx.initial_seg : @initial_seg cardinal ordinal (<) (<) := @rel_embedding.collapse cardinal ordinal (<) (<) _ cardinal.ord.order_embedding.lt_embedding /-- The `aleph'` index function, which gives the ordinal index of a cardinal. (The `aleph'` part is because unlike `aleph` this counts also the finite stages. So `aleph_idx n = n`, `aleph_idx ω = ω`, `aleph_idx ℵ₁ = ω + 1` and so on.) For an upgraded version stating that the range is everything, see `aleph_idx.rel_iso`. -/ def aleph_idx : cardinal → ordinal := aleph_idx.initial_seg @[simp] theorem aleph_idx.initial_seg_coe : (aleph_idx.initial_seg : cardinal → ordinal) = aleph_idx := rfl @[simp] theorem aleph_idx_lt {a b} : aleph_idx a < aleph_idx b ↔ a < b := aleph_idx.initial_seg.to_rel_embedding.map_rel_iff @[simp] theorem aleph_idx_le {a b} : aleph_idx a ≤ aleph_idx b ↔ a ≤ b := by rw [← not_lt, ← not_lt, aleph_idx_lt] theorem aleph_idx.init {a b} : b < aleph_idx a → ∃ c, aleph_idx c = b := aleph_idx.initial_seg.init _ _ /-- The `aleph'` index function, which gives the ordinal index of a cardinal. (The `aleph'` part is because unlike `aleph` this counts also the finite stages. So `aleph_idx n = n`, `aleph_idx ω = ω`, `aleph_idx ℵ₁ = ω + 1` and so on.) In this version, we register additionally that this function is an order isomorphism between cardinals and ordinals. For the basic function version, see `aleph_idx`. -/ def aleph_idx.rel_iso : @rel_iso cardinal.{u} ordinal.{u} (<) (<) := @rel_iso.of_surjective cardinal.{u} ordinal.{u} (<) (<) aleph_idx.initial_seg.{u} $ (initial_seg.eq_or_principal aleph_idx.initial_seg.{u}).resolve_right $ λ ⟨o, e⟩, begin have : ∀ c, aleph_idx c < o := λ c, (e _).2 ⟨_, rfl⟩, refine ordinal.induction_on o _ this, introsI α r _ h, let s := sup.{u u} (λ a:α, inv_fun aleph_idx (ordinal.typein r a)), apply not_le_of_gt (lt_succ_self s), have I : injective aleph_idx := aleph_idx.initial_seg.to_embedding.injective, simpa only [typein_enum, left_inverse_inv_fun I (succ s)] using le_sup.{u u} (λ a, inv_fun aleph_idx (ordinal.typein r a)) (ordinal.enum r _ (h (succ s))), end @[simp] theorem aleph_idx.rel_iso_coe : (aleph_idx.rel_iso : cardinal → ordinal) = aleph_idx := rfl @[simp] theorem type_cardinal : @ordinal.type cardinal (<) _ = ordinal.univ.{u (u+1)} := by rw ordinal.univ_id; exact quotient.sound ⟨aleph_idx.rel_iso⟩ @[simp] theorem mk_cardinal : mk cardinal = univ.{u (u+1)} := by simpa only [card_type, card_univ] using congr_arg card type_cardinal /-- The `aleph'` function gives the cardinals listed by their ordinal index, and is the inverse of `aleph_idx`. `aleph' n = n`, `aleph' ω = ω`, `aleph' (ω + 1) = ℵ₁`, etc. In this version, we register additionally that this function is an order isomorphism between ordinals and cardinals. For the basic function version, see `aleph'`. -/ def aleph'.rel_iso := cardinal.aleph_idx.rel_iso.symm /-- The `aleph'` function gives the cardinals listed by their ordinal index, and is the inverse of `aleph_idx`. `aleph' n = n`, `aleph' ω = ω`, `aleph' (ω + 1) = ℵ₁`, etc. -/ def aleph' : ordinal → cardinal := aleph'.rel_iso @[simp] theorem aleph'.rel_iso_coe : (aleph'.rel_iso : ordinal → cardinal) = aleph' := rfl @[simp] theorem aleph'_lt {o₁ o₂ : ordinal.{u}} : aleph' o₁ < aleph' o₂ ↔ o₁ < o₂ := aleph'.rel_iso.map_rel_iff @[simp] theorem aleph'_le {o₁ o₂ : ordinal.{u}} : aleph' o₁ ≤ aleph' o₂ ↔ o₁ ≤ o₂ := le_iff_le_iff_lt_iff_lt.2 aleph'_lt @[simp] theorem aleph'_aleph_idx (c : cardinal.{u}) : aleph' c.aleph_idx = c := cardinal.aleph_idx.rel_iso.to_equiv.symm_apply_apply c @[simp] theorem aleph_idx_aleph' (o : ordinal.{u}) : (aleph' o).aleph_idx = o := cardinal.aleph_idx.rel_iso.to_equiv.apply_symm_apply o @[simp] theorem aleph'_zero : aleph' 0 = 0 := by rw [← nonpos_iff_eq_zero, ← aleph'_aleph_idx 0, aleph'_le]; apply ordinal.zero_le @[simp] theorem aleph'_succ {o : ordinal.{u}} : aleph' o.succ = (aleph' o).succ := le_antisymm (cardinal.aleph_idx_le.1 $ by rw [aleph_idx_aleph', ordinal.succ_le, ← aleph'_lt, aleph'_aleph_idx]; apply cardinal.lt_succ_self) (cardinal.succ_le.2 $ aleph'_lt.2 $ ordinal.lt_succ_self _) @[simp] theorem aleph'_nat : ∀ n : ℕ, aleph' n = n | 0 := aleph'_zero | (n+1) := show aleph' (ordinal.succ n) = n.succ, by rw [aleph'_succ, aleph'_nat, nat_succ] theorem aleph'_le_of_limit {o : ordinal.{u}} (l : o.is_limit) {c} : aleph' o ≤ c ↔ ∀ o' < o, aleph' o' ≤ c := ⟨λ h o' h', le_trans (aleph'_le.2 $ le_of_lt h') h, λ h, begin rw [← aleph'_aleph_idx c, aleph'_le, ordinal.limit_le l], intros x h', rw [← aleph'_le, aleph'_aleph_idx], exact h _ h' end⟩ @[simp] theorem aleph'_omega : aleph' ordinal.omega = omega := eq_of_forall_ge_iff $ λ c, begin simp only [aleph'_le_of_limit omega_is_limit, ordinal.lt_omega, exists_imp_distrib, omega_le], exact forall_swap.trans (forall_congr $ λ n, by simp only [forall_eq, aleph'_nat]), end /-- `aleph'` and `aleph_idx` form an equivalence between `ordinal` and `cardinal` -/ @[simp] def aleph'_equiv : ordinal ≃ cardinal := ⟨aleph', aleph_idx, aleph_idx_aleph', aleph'_aleph_idx⟩ /-- The `aleph` function gives the infinite cardinals listed by their ordinal index. `aleph 0 = ω`, `aleph 1 = succ ω` is the first uncountable cardinal, and so on. -/ def aleph (o : ordinal) : cardinal := aleph' (ordinal.omega + o) @[simp] theorem aleph_lt {o₁ o₂ : ordinal.{u}} : aleph o₁ < aleph o₂ ↔ o₁ < o₂ := aleph'_lt.trans (ordinal.add_lt_add_iff_left _) @[simp] theorem aleph_le {o₁ o₂ : ordinal.{u}} : aleph o₁ ≤ aleph o₂ ↔ o₁ ≤ o₂ := le_iff_le_iff_lt_iff_lt.2 aleph_lt @[simp] theorem aleph_succ {o : ordinal.{u}} : aleph o.succ = (aleph o).succ := by rw [aleph, ordinal.add_succ, aleph'_succ]; refl @[simp] theorem aleph_zero : aleph 0 = omega := by simp only [aleph, add_zero, aleph'_omega] theorem omega_le_aleph' {o : ordinal} : omega ≤ aleph' o ↔ ordinal.omega ≤ o := by rw [← aleph'_omega, aleph'_le] theorem omega_le_aleph (o : ordinal) : omega ≤ aleph o := by rw [aleph, omega_le_aleph']; apply ordinal.le_add_right theorem ord_aleph_is_limit (o : ordinal) : is_limit (aleph o).ord := ord_is_limit $ omega_le_aleph _ theorem exists_aleph {c : cardinal} : omega ≤ c ↔ ∃ o, c = aleph o := ⟨λ h, ⟨aleph_idx c - ordinal.omega, by rw [aleph, ordinal.add_sub_cancel_of_le, aleph'_aleph_idx]; rwa [← omega_le_aleph', aleph'_aleph_idx]⟩, λ ⟨o, e⟩, e.symm ▸ omega_le_aleph _⟩ theorem aleph'_is_normal : is_normal (ord ∘ aleph') := ⟨λ o, ord_lt_ord.2 $ aleph'_lt.2 $ ordinal.lt_succ_self _, λ o l a, by simp only [ord_le, aleph'_le_of_limit l]⟩ theorem aleph_is_normal : is_normal (ord ∘ aleph) := aleph'_is_normal.trans $ add_is_normal ordinal.omega lemma countable_iff_lt_aleph_one {α : Type*} (s : set α) : countable s ↔ #s < aleph 1 := begin have : aleph 1 = (aleph 0).succ, by simp only [← aleph_succ, ordinal.succ_zero], rw [countable_iff, ← aleph_zero, this, lt_succ], end /-! ### Properties of `mul` -/ /-- If `α` is an infinite type, then `α × α` and `α` have the same cardinality. -/ theorem mul_eq_self {c : cardinal} (h : omega ≤ c) : c * c = c := begin refine le_antisymm _ (by simpa only [mul_one] using canonically_ordered_semiring.mul_le_mul_left' (one_lt_omega.le.trans h) c), -- the only nontrivial part is `c * c ≤ c`. We prove it inductively. refine acc.rec_on (cardinal.wf.apply c) (λ c _, quotient.induction_on c $ λ α IH ol, _) h, -- consider the minimal well-order `r` on `α` (a type with cardinality `c`). rcases ord_eq α with ⟨r, wo, e⟩, resetI, letI := linear_order_of_STO' r, haveI : is_well_order α (<) := wo, -- Define an order `s` on `α × α` by writing `(a, b) < (c, d)` if `max a b < max c d`, or -- the max are equal and `a < c`, or the max are equal and `a = c` and `b < d`. let g : α × α → α := λ p, max p.1 p.2, let f : α × α ↪ ordinal × (α × α) := ⟨λ p:α×α, (typein (<) (g p), p), λ p q, congr_arg prod.snd⟩, let s := f ⁻¹'o (prod.lex (<) (prod.lex (<) (<))), -- this is a well order on `α × α`. haveI : is_well_order _ s := (rel_embedding.preimage _ _).is_well_order, /- it suffices to show that this well order is smaller than `r` if it were larger, then `r` would be a strict prefix of `s`. It would be contained in `β × β` for some `β` of cardinality `< c`. By the inductive assumption, this set has the same cardinality as `β` (or it is finite if `β` is finite), so it is `< c`, which is a contradiction. -/ suffices : type s ≤ type r, {exact card_le_card this}, refine le_of_forall_lt (λ o h, _), rcases typein_surj s h with ⟨p, rfl⟩, rw [← e, lt_ord], refine lt_of_le_of_lt (_ : _ ≤ card (typein (<) (g p)).succ * card (typein (<) (g p)).succ) _, { have : {q|s q p} ⊆ (insert (g p) {x | x < (g p)}).prod (insert (g p) {x | x < (g p)}), { intros q h, simp only [s, embedding.coe_fn_mk, order.preimage, typein_lt_typein, prod.lex_def, typein_inj] at h, exact max_le_iff.1 (le_iff_lt_or_eq.2 $ h.imp_right and.left) }, suffices H : (insert (g p) {x | r x (g p)} : set α) ≃ ({x | r x (g p)} ⊕ punit), { exact ⟨(set.embedding_of_subset _ _ this).trans ((equiv.set.prod _ _).trans (H.prod_congr H)).to_embedding⟩ }, refine (equiv.set.insert _).trans ((equiv.refl _).sum_congr punit_equiv_punit), apply @irrefl _ r }, cases lt_or_ge (card (typein (<) (g p)).succ) omega with qo qo, { exact lt_of_lt_of_le (mul_lt_omega qo qo) ol }, { suffices, {exact lt_of_le_of_lt (IH _ this qo) this}, rw ← lt_ord, apply (ord_is_limit ol).2, rw [mk_def, e], apply typein_lt_type } end end using_ordinals /-- If `α` and `β` are infinite types, then the cardinality of `α × β` is the maximum of the cardinalities of `α` and `β`. -/ theorem mul_eq_max {a b : cardinal} (ha : omega ≤ a) (hb : omega ≤ b) : a * b = max a b := le_antisymm (mul_eq_self (le_trans ha (le_max_left a b)) ▸ canonically_ordered_semiring.mul_le_mul (le_max_left _ _) (le_max_right _ _)) $ max_le (by simpa only [mul_one] using canonically_ordered_semiring.mul_le_mul_left' (one_lt_omega.le.trans hb) a) (by simpa only [one_mul] using canonically_ordered_semiring.mul_le_mul_right' (one_lt_omega.le.trans ha) b) theorem mul_lt_of_lt {a b c : cardinal} (hc : omega ≤ c) (h1 : a < c) (h2 : b < c) : a * b < c := lt_of_le_of_lt (canonically_ordered_semiring.mul_le_mul (le_max_left a b) (le_max_right a b)) $ (lt_or_le (max a b) omega).elim (λ h, lt_of_lt_of_le (mul_lt_omega h h) hc) (λ h, by rw mul_eq_self h; exact max_lt h1 h2) lemma mul_le_max_of_omega_le_left {a b : cardinal} (h : omega ≤ a) : a * b ≤ max a b := begin convert canonically_ordered_semiring.mul_le_mul (le_max_left a b) (le_max_right a b), rw [mul_eq_self], refine le_trans h (le_max_left a b) end lemma mul_eq_max_of_omega_le_left {a b : cardinal} (h : omega ≤ a) (h' : b ≠ 0) : a * b = max a b := begin apply le_antisymm, apply mul_le_max_of_omega_le_left h, cases le_or_gt omega b with hb hb, rw [mul_eq_max h hb], have : b ≤ a, exact le_trans (le_of_lt hb) h, rw [max_eq_left this], convert canonically_ordered_semiring.mul_le_mul_left' (one_le_iff_ne_zero.mpr h') _, rw [mul_one], end lemma mul_eq_left {a b : cardinal} (ha : omega ≤ a) (hb : b ≤ a) (hb' : b ≠ 0) : a * b = a := by { rw [mul_eq_max_of_omega_le_left ha hb', max_eq_left hb] } lemma mul_eq_right {a b : cardinal} (hb : omega ≤ b) (ha : a ≤ b) (ha' : a ≠ 0) : a * b = b := by { rw [mul_comm, mul_eq_left hb ha ha'] } lemma le_mul_left {a b : cardinal} (h : b ≠ 0) : a ≤ b * a := by { convert canonically_ordered_semiring.mul_le_mul_right' (one_le_iff_ne_zero.mpr h) _, rw [one_mul] } lemma le_mul_right {a b : cardinal} (h : b ≠ 0) : a ≤ a * b := by { rw [mul_comm], exact le_mul_left h } lemma mul_eq_left_iff {a b : cardinal} : a * b = a ↔ ((max omega b ≤ a ∧ b ≠ 0) ∨ b = 1 ∨ a = 0) := begin rw [max_le_iff], split, { intro h, cases (le_or_lt omega a) with ha ha, { have : a ≠ 0, { rintro rfl, exact not_lt_of_le ha omega_pos }, left, use ha, { rw [← not_lt], intro hb, apply ne_of_gt _ h, refine lt_of_lt_of_le hb (le_mul_left this) }, { rintro rfl, apply this, rw [_root_.mul_zero] at h, subst h }}, right, by_cases h2a : a = 0, { right, exact h2a }, have hb : b ≠ 0, { rintro rfl, apply h2a, rw [mul_zero] at h, subst h }, left, rw [← h, mul_lt_omega_iff, lt_omega, lt_omega] at ha, rcases ha with rfl|rfl|⟨⟨n, rfl⟩, ⟨m, rfl⟩⟩, contradiction, contradiction, rw [← ne] at h2a, rw [← one_le_iff_ne_zero] at h2a hb, norm_cast at h2a hb h ⊢, apply le_antisymm _ hb, rw [← not_lt], intro h2b, apply ne_of_gt _ h, conv_lhs { rw [← mul_one n] }, rwa [mul_lt_mul_left], apply nat.lt_of_succ_le h2a }, { rintro (⟨⟨ha, hab⟩, hb⟩|rfl|rfl), { rw [mul_eq_max_of_omega_le_left ha hb, max_eq_left hab] }, all_goals {simp}} end /-! ### Properties of `add` -/ /-- If `α` is an infinite type, then `α ⊕ α` and `α` have the same cardinality. -/ theorem add_eq_self {c : cardinal} (h : omega ≤ c) : c + c = c := le_antisymm (by simpa only [nat.cast_bit0, nat.cast_one, mul_eq_self h, two_mul] using canonically_ordered_semiring.mul_le_mul_right' ((nat_lt_omega 2).le.trans h) c) (self_le_add_left c c) /-- If `α` is an infinite type, then the cardinality of `α ⊕ β` is the maximum of the cardinalities of `α` and `β`. -/ theorem add_eq_max {a b : cardinal} (ha : omega ≤ a) : a + b = max a b := le_antisymm (add_eq_self (le_trans ha (le_max_left a b)) ▸ add_le_add (le_max_left _ _) (le_max_right _ _)) $ max_le (self_le_add_right _ _) (self_le_add_left _ _) theorem add_lt_of_lt {a b c : cardinal} (hc : omega ≤ c) (h1 : a < c) (h2 : b < c) : a + b < c := lt_of_le_of_lt (add_le_add (le_max_left a b) (le_max_right a b)) $ (lt_or_le (max a b) omega).elim (λ h, lt_of_lt_of_le (add_lt_omega h h) hc) (λ h, by rw add_eq_self h; exact max_lt h1 h2) lemma eq_of_add_eq_of_omega_le {a b c : cardinal} (h : a + b = c) (ha : a < c) (hc : omega ≤ c) : b = c := begin apply le_antisymm, { rw [← h], apply self_le_add_left }, rw[← not_lt], intro hb, have : a + b < c := add_lt_of_lt hc ha hb, simpa [h, lt_irrefl] using this end lemma add_eq_left {a b : cardinal} (ha : omega ≤ a) (hb : b ≤ a) : a + b = a := by { rw [add_eq_max ha, max_eq_left hb] } lemma add_eq_right {a b : cardinal} (hb : omega ≤ b) (ha : a ≤ b) : a + b = b := by { rw [add_comm, add_eq_left hb ha] } lemma add_eq_left_iff {a b : cardinal} : a + b = a ↔ (max omega b ≤ a ∨ b = 0) := begin rw [max_le_iff], split, { intro h, cases (le_or_lt omega a) with ha ha, { left, use ha, rw [← not_lt], intro hb, apply ne_of_gt _ h, exact lt_of_lt_of_le hb (self_le_add_left b a) }, right, rw [← h, add_lt_omega_iff, lt_omega, lt_omega] at ha, rcases ha with ⟨⟨n, rfl⟩, ⟨m, rfl⟩⟩, norm_cast at h ⊢, rw [← add_right_inj, h, add_zero] }, { rintro (⟨h1, h2⟩|h3), rw [add_eq_max h1, max_eq_left h2], rw [h3, add_zero] } end lemma add_eq_right_iff {a b : cardinal} : a + b = b ↔ (max omega a ≤ b ∨ a = 0) := by { rw [add_comm, add_eq_left_iff] } lemma add_one_eq {a : cardinal} (ha : omega ≤ a) : a + 1 = a := have 1 ≤ a, from le_trans (le_of_lt one_lt_omega) ha, add_eq_left ha this protected lemma eq_of_add_eq_add_left {a b c : cardinal} (h : a + b = a + c) (ha : a < omega) : b = c := begin cases le_or_lt omega b with hb hb, { have : a < b := lt_of_lt_of_le ha hb, rw [add_eq_right hb (le_of_lt this), eq_comm] at h, rw [eq_of_add_eq_of_omega_le h this hb] }, { have hc : c < omega, { rw [← not_le], intro hc, apply lt_irrefl omega, apply lt_of_le_of_lt (le_trans hc (self_le_add_left _ a)), rw [← h], apply add_lt_omega ha hb }, rw [lt_omega] at *, rcases ha with ⟨n, rfl⟩, rcases hb with ⟨m, rfl⟩, rcases hc with ⟨k, rfl⟩, norm_cast at h ⊢, apply add_left_cancel h } end protected lemma eq_of_add_eq_add_right {a b c : cardinal} (h : a + b = c + b) (hb : b < omega) : a = c := by { rw [add_comm a b, add_comm c b] at h, exact cardinal.eq_of_add_eq_add_left h hb } /-! ### Properties about power -/ theorem pow_le {κ μ : cardinal.{u}} (H1 : omega ≤ κ) (H2 : μ < omega) : κ ^ μ ≤ κ := let ⟨n, H3⟩ := lt_omega.1 H2 in H3.symm ▸ (quotient.induction_on κ (λ α H1, nat.rec_on n (le_of_lt $ lt_of_lt_of_le (by rw [nat.cast_zero, power_zero]; from one_lt_omega) H1) (λ n ih, trans_rel_left _ (by { rw [nat.cast_succ, power_add, power_one]; exact canonically_ordered_semiring.mul_le_mul_right' ih _ }) (mul_eq_self H1))) H1) lemma power_self_eq {c : cardinal} (h : omega ≤ c) : c ^ c = 2 ^ c := begin apply le_antisymm, { apply le_trans (power_le_power_right $ le_of_lt $ cantor c), rw [← power_mul, mul_eq_self h] }, { convert power_le_power_right (le_trans (le_of_lt $ nat_lt_omega 2) h), apply nat.cast_two.symm } end lemma power_nat_le {c : cardinal.{u}} {n : ℕ} (h : omega ≤ c) : c ^ (n : cardinal.{u}) ≤ c := pow_le h (nat_lt_omega n) lemma powerlt_omega {c : cardinal} (h : omega ≤ c) : c ^< omega = c := begin apply le_antisymm, { rw [powerlt_le], intro c', rw [lt_omega], rintro ⟨n, rfl⟩, apply power_nat_le h }, convert le_powerlt one_lt_omega, rw [power_one] end lemma powerlt_omega_le (c : cardinal) : c ^< omega ≤ max c omega := begin cases le_or_gt omega c, { rw [powerlt_omega h], apply le_max_left }, rw [powerlt_le], intros c' hc', refine le_trans (le_of_lt $ power_lt_omega h hc') (le_max_right _ _) end /-! ### Computing cardinality of various types -/ theorem mk_list_eq_mk {α : Type u} (H1 : omega ≤ mk α) : mk (list α) = mk α := eq.symm $ le_antisymm ⟨⟨λ x, [x], λ x y H, (list.cons.inj H).1⟩⟩ $ calc mk (list α) = sum (λ n : ℕ, mk α ^ (n : cardinal.{u})) : mk_list_eq_sum_pow α ... ≤ sum (λ n : ℕ, mk α) : sum_le_sum _ _ $ λ n, pow_le H1 $ nat_lt_omega n ... = sum (λ n : ulift.{u} ℕ, mk α) : quotient.sound ⟨@sigma_congr_left _ _ (λ _, quotient.out (mk α)) equiv.ulift.symm⟩ ... = omega * mk α : sum_const _ _ ... = max (omega) (mk α) : mul_eq_max (le_refl _) H1 ... = mk α : max_eq_right H1 theorem mk_finset_eq_mk {α : Type u} (h : omega ≤ mk α) : mk (finset α) = mk α := eq.symm $ le_antisymm (mk_le_of_injective (λ x y, finset.singleton_inj.1)) $ calc mk (finset α) ≤ mk (list α) : mk_le_of_surjective list.to_finset_surjective ... = mk α : mk_list_eq_mk h lemma mk_bounded_set_le_of_omega_le (α : Type u) (c : cardinal) (hα : omega ≤ mk α) : mk {t : set α // mk t ≤ c} ≤ mk α ^ c := begin refine le_trans _ (by rw [←add_one_eq hα]), refine quotient.induction_on c _, clear c, intro β, fapply mk_le_of_surjective, { intro f, use sum.inl ⁻¹' range f, refine le_trans (mk_preimage_of_injective _ _ (λ x y, sum.inl.inj)) _, apply mk_range_le }, rintro ⟨s, ⟨g⟩⟩, use λ y, if h : ∃(x : s), g x = y then sum.inl (classical.some h).val else sum.inr ⟨⟩, apply subtype.eq, ext, split, { rintro ⟨y, h⟩, dsimp only at h, by_cases h' : ∃ (z : s), g z = y, { rw [dif_pos h'] at h, cases sum.inl.inj h, exact (classical.some h').2 }, { rw [dif_neg h'] at h, cases h }}, { intro h, have : ∃(z : s), g z = g ⟨x, h⟩, exact ⟨⟨x, h⟩, rfl⟩, use g ⟨x, h⟩, dsimp only, rw [dif_pos this], congr', suffices : classical.some this = ⟨x, h⟩, exact congr_arg subtype.val this, apply g.2, exact classical.some_spec this } end lemma mk_bounded_set_le (α : Type u) (c : cardinal) : mk {t : set α // mk t ≤ c} ≤ max (mk α) omega ^ c := begin transitivity mk {t : set (ulift.{u} nat ⊕ α) // mk t ≤ c}, { refine ⟨embedding.subtype_map _ _⟩, apply embedding.image, use sum.inr, apply sum.inr.inj, intros s hs, exact le_trans mk_image_le hs }, refine le_trans (mk_bounded_set_le_of_omega_le (ulift.{u} nat ⊕ α) c (self_le_add_right omega (mk α))) _, rw [max_comm, ←add_eq_max]; refl end lemma mk_bounded_subset_le {α : Type u} (s : set α) (c : cardinal.{u}) : mk {t : set α // t ⊆ s ∧ mk t ≤ c} ≤ max (mk s) omega ^ c := begin refine le_trans _ (mk_bounded_set_le s c), refine ⟨embedding.cod_restrict _ _ _⟩, use λ t, coe ⁻¹' t.1, { rintros ⟨t, ht1, ht2⟩ ⟨t', h1t', h2t'⟩ h, apply subtype.eq, dsimp only at h ⊢, refine (preimage_eq_preimage' _ _).1 h; rw [subtype.range_coe]; assumption }, rintro ⟨t, h1t, h2t⟩, exact le_trans (mk_preimage_of_injective _ _ subtype.val_injective) h2t end /-! ### Properties of `compl` -/ lemma mk_compl_of_omega_le {α : Type*} (s : set α) (h : omega ≤ #α) (h2 : #s < #α) : #(sᶜ : set α) = #α := by { refine eq_of_add_eq_of_omega_le _ h2 h, exact mk_sum_compl s } lemma mk_compl_finset_of_omega_le {α : Type*} (s : finset α) (h : omega ≤ #α) : #((↑s)ᶜ : set α) = #α := by { apply mk_compl_of_omega_le _ h, exact lt_of_lt_of_le (finset_card_lt_omega s) h } lemma mk_compl_eq_mk_compl_infinite {α : Type*} {s t : set α} (h : omega ≤ #α) (hs : #s < #α) (ht : #t < #α) : #(sᶜ : set α) = #(tᶜ : set α) := by { rw [mk_compl_of_omega_le s h hs, mk_compl_of_omega_le t h ht] } lemma mk_compl_eq_mk_compl_finite_lift {α : Type u} {β : Type v} {s : set α} {t : set β} (hα : #α < omega) (h1 : lift.{u (max v w)} (#α) = lift.{v (max u w)} (#β)) (h2 : lift.{u (max v w)} (#s) = lift.{v (max u w)} (#t)) : lift.{u (max v w)} (#(sᶜ : set α)) = lift.{v (max u w)} (#(tᶜ : set β)) := begin have hα' := hα, have h1' := h1, rw [← mk_sum_compl s, ← mk_sum_compl t] at h1, rw [← mk_sum_compl s, add_lt_omega_iff] at hα, lift #s to ℕ using hα.1 with n hn, lift #(sᶜ : set α) to ℕ using hα.2 with m hm, have : #(tᶜ : set β) < omega, { refine lt_of_le_of_lt (mk_subtype_le _) _, rw [← lift_lt, lift_omega, ← h1', ← lift_omega.{u (max v w)}, lift_lt], exact hα' }, lift #(tᶜ : set β) to ℕ using this with k hk, simp [nat_eq_lift_eq_iff] at h2, rw [nat_eq_lift_eq_iff.{v (max u w)}] at h2, simp [h2.symm] at h1 ⊢, norm_cast at h1, simp at h1, exact h1 end lemma mk_compl_eq_mk_compl_finite {α β : Type u} {s : set α} {t : set β} (hα : #α < omega) (h1 : #α = #β) (h : #s = #t) : #(sᶜ : set α) = #(tᶜ : set β) := by { rw [← lift_inj], apply mk_compl_eq_mk_compl_finite_lift hα; rw [lift_inj]; assumption } lemma mk_compl_eq_mk_compl_finite_same {α : Type*} {s t : set α} (hα : #α < omega) (h : #s = #t) : #(sᶜ : set α) = #(tᶜ : set α) := mk_compl_eq_mk_compl_finite hα rfl h /-! ### Extending an injection to an equiv -/ theorem extend_function {α β : Type*} {s : set α} (f : s ↪ β) (h : nonempty ((sᶜ : set α) ≃ ((range f)ᶜ : set β))) : ∃ (g : α ≃ β), ∀ x : s, g x = f x := begin intros, have := h, cases this with g, let h : α ≃ β := (set.sum_compl (s : set α)).symm.trans ((sum_congr (equiv.of_injective f f.2) g).trans (set.sum_compl (range f))), refine ⟨h, _⟩, rintro ⟨x, hx⟩, simp [set.sum_compl_symm_apply_of_mem, hx] end theorem extend_function_finite {α β : Type*} {s : set α} (f : s ↪ β) (hs : #α < omega) (h : nonempty (α ≃ β)) : ∃ (g : α ≃ β), ∀ x : s, g x = f x := begin apply extend_function f, have := h, cases this with g, rw [← lift_mk_eq] at h, rw [←lift_mk_eq, mk_compl_eq_mk_compl_finite_lift hs h], rw [mk_range_eq_lift], exact f.2 end theorem extend_function_of_lt {α β : Type*} {s : set α} (f : s ↪ β) (hs : #s < #α) (h : nonempty (α ≃ β)) : ∃ (g : α ≃ β), ∀ x : s, g x = f x := begin cases (le_or_lt omega (#α)) with hα hα, { apply extend_function f, have := h, cases this with g, rw [← lift_mk_eq] at h, cases cardinal.eq.mp (mk_compl_of_omega_le s hα hs) with g2, cases cardinal.eq.mp (mk_compl_of_omega_le (range f) _ _) with g3, { constructor, exact g2.trans (g.trans g3.symm) }, { rw [← lift_le, ← h], refine le_trans _ (lift_le.mpr hα), simp }, rwa [← lift_lt, ← h, mk_range_eq_lift, lift_lt], exact f.2 }, { exact extend_function_finite f hα h } end section bit /-! This section proves inequalities for `bit0` and `bit1`, enabling `simp` to solve inequalities for numeral cardinals. The complexity of the resulting algorithm is not good, as in some cases `simp` reduces an inequality to a disjunction of two situations, depending on whether a cardinal is finite or infinite. Since the evaluation of the branches is not lazy, this is bad. It is good enough for practical situations, though. For specific numbers, these inequalities could also be deduced from the corresponding inequalities of natural numbers using `norm_cast`: ``` example : (37 : cardinal) < 42 := by { norm_cast, norm_num } ``` -/ @[simp] lemma bit0_ne_zero (a : cardinal) : ¬bit0 a = 0 ↔ ¬a = 0 := by simp [bit0] @[simp] lemma bit1_ne_zero (a : cardinal) : ¬bit1 a = 0 := by simp [bit1] @[simp] lemma zero_lt_bit0 (a : cardinal) : 0 < bit0 a ↔ 0 < a := by { rw ←not_iff_not, simp [bit0], } @[simp] lemma zero_lt_bit1 (a : cardinal) : 0 < bit1 a := lt_of_lt_of_le zero_lt_one (self_le_add_left _ _) @[simp] lemma one_le_bit0 (a : cardinal) : 1 ≤ bit0 a ↔ 0 < a := ⟨λ h, (zero_lt_bit0 a).mp (lt_of_lt_of_le zero_lt_one h), λ h, le_trans (one_le_iff_pos.mpr h) (self_le_add_left a a)⟩ @[simp] lemma one_le_bit1 (a : cardinal) : 1 ≤ bit1 a := self_le_add_left _ _ theorem bit0_eq_self {c : cardinal} (h : omega ≤ c) : bit0 c = c := add_eq_self h @[simp] theorem bit0_lt_omega {c : cardinal} : bit0 c < omega ↔ c < omega := by simp [bit0, add_lt_omega_iff] @[simp] theorem omega_le_bit0 {c : cardinal} : omega ≤ bit0 c ↔ omega ≤ c := by { rw ← not_iff_not, simp } @[simp] theorem bit1_eq_self_iff {c : cardinal} : bit1 c = c ↔ omega ≤ c := begin by_cases h : omega ≤ c, { simp only [bit1, bit0_eq_self h, h, eq_self_iff_true, add_one_of_omega_le] }, { simp only [h, iff_false], apply ne_of_gt, rcases lt_omega.1 (not_le.1 h) with ⟨n, rfl⟩, norm_cast, dsimp [bit1, bit0], linarith } end @[simp] theorem bit1_lt_omega {c : cardinal} : bit1 c < omega ↔ c < omega := by simp [bit1, bit0, add_lt_omega_iff, one_lt_omega] @[simp] theorem omega_le_bit1 {c : cardinal} : omega ≤ bit1 c ↔ omega ≤ c := by { rw ← not_iff_not, simp } @[simp] lemma bit0_le_bit0 {a b : cardinal} : bit0 a ≤ bit0 b ↔ a ≤ b := begin by_cases ha : omega ≤ a; by_cases hb : omega ≤ b, { rw [bit0_eq_self ha, bit0_eq_self hb] }, { rw bit0_eq_self ha, have I1 : ¬ (a ≤ b), { assume h, apply hb, exact le_trans ha h }, have I2 : ¬ (a ≤ bit0 b), { assume h, have A : bit0 b < omega, by simpa using hb, exact lt_irrefl _ (lt_of_lt_of_le (lt_of_lt_of_le A ha) h) }, simp [I1, I2] }, { rw [bit0_eq_self hb], simp only [not_le] at ha, have I1 : a ≤ b := le_of_lt (lt_of_lt_of_le ha hb), have I2 : bit0 a ≤ b := le_trans (le_of_lt (bit0_lt_omega.2 ha)) hb, simp [I1, I2] }, { simp at ha hb, rcases lt_omega.1 ha with ⟨m, rfl⟩, rcases lt_omega.1 hb with ⟨n, rfl⟩, norm_cast, simp } end @[simp] lemma bit0_le_bit1 {a b : cardinal} : bit0 a ≤ bit1 b ↔ a ≤ b := begin by_cases ha : omega ≤ a; by_cases hb : omega ≤ b, { rw [bit0_eq_self ha, bit1_eq_self_iff.2 hb], }, { rw bit0_eq_self ha, have I1 : ¬ (a ≤ b), { assume h, apply hb, exact le_trans ha h }, have I2 : ¬ (a ≤ bit1 b), { assume h, have A : bit1 b < omega, by simpa using hb, exact lt_irrefl _ (lt_of_lt_of_le (lt_of_lt_of_le A ha) h) }, simp [I1, I2] }, { rw [bit1_eq_self_iff.2 hb], simp only [not_le] at ha, have I1 : a ≤ b := le_of_lt (lt_of_lt_of_le ha hb), have I2 : bit0 a ≤ b := le_trans (le_of_lt (bit0_lt_omega.2 ha)) hb, simp [I1, I2] }, { simp at ha hb, rcases lt_omega.1 ha with ⟨m, rfl⟩, rcases lt_omega.1 hb with ⟨n, rfl⟩, norm_cast, simp } end @[simp] lemma bit1_le_bit1 {a b : cardinal} : bit1 a ≤ bit1 b ↔ a ≤ b := begin split, { assume h, apply bit0_le_bit1.1 (le_trans (self_le_add_right (bit0 a) 1) h) }, { assume h, calc a + a + 1 ≤ a + b + 1 : add_le_add_right (add_le_add_left h a) 1 ... ≤ b + b + 1 : add_le_add_right (add_le_add_right h b) 1 } end @[simp] lemma bit1_le_bit0 {a b : cardinal} : bit1 a ≤ bit0 b ↔ (a < b ∨ (a ≤ b ∧ omega ≤ a)) := begin by_cases ha : omega ≤ a; by_cases hb : omega ≤ b, { simp only [bit1_eq_self_iff.mpr ha, bit0_eq_self hb, ha, and_true], refine ⟨λ h, or.inr h, λ h, _⟩, cases h, { exact le_of_lt h }, { exact h } }, { rw bit1_eq_self_iff.2 ha, have I1 : ¬ (a ≤ b), { assume h, apply hb, exact le_trans ha h }, have I2 : ¬ (a ≤ bit0 b), { assume h, have A : bit0 b < omega, by simpa using hb, exact lt_irrefl _ (lt_of_lt_of_le (lt_of_lt_of_le A ha) h) }, simp [I1, I2, le_of_not_ge I1] }, { rw [bit0_eq_self hb], simp only [not_le] at ha, have I1 : a < b := lt_of_lt_of_le ha hb, have I2 : bit1 a ≤ b := le_trans (le_of_lt (bit1_lt_omega.2 ha)) hb, simp [I1, I2] }, { simp at ha hb, rcases lt_omega.1 ha with ⟨m, rfl⟩, rcases lt_omega.1 hb with ⟨n, rfl⟩, norm_cast, simp [not_le.mpr ha], } end @[simp] lemma bit0_lt_bit0 {a b : cardinal} : bit0 a < bit0 b ↔ a < b := begin by_cases ha : omega ≤ a; by_cases hb : omega ≤ b, { rw [bit0_eq_self ha, bit0_eq_self hb] }, { rw bit0_eq_self ha, have I1 : ¬ (a < b), { assume h, apply hb, exact le_trans ha (le_of_lt h) }, have I2 : ¬ (a < bit0 b), { assume h, have A : bit0 b < omega, by simpa using hb, exact lt_irrefl _ (lt_trans (lt_of_lt_of_le A ha) h) }, simp [I1, I2] }, { rw [bit0_eq_self hb], simp only [not_le] at ha, have I1 : a < b := lt_of_lt_of_le ha hb, have I2 : bit0 a < b := lt_of_lt_of_le (bit0_lt_omega.2 ha) hb, simp [I1, I2] }, { simp at ha hb, rcases lt_omega.1 ha with ⟨m, rfl⟩, rcases lt_omega.1 hb with ⟨n, rfl⟩, norm_cast, simp } end @[simp] lemma bit1_lt_bit0 {a b : cardinal} : bit1 a < bit0 b ↔ a < b := begin by_cases ha : omega ≤ a; by_cases hb : omega ≤ b, { rw [bit1_eq_self_iff.2 ha, bit0_eq_self hb], }, { rw bit1_eq_self_iff.2 ha, have I1 : ¬ (a < b), { assume h, apply hb, exact le_of_lt (lt_of_le_of_lt ha h) }, have I2 : ¬ (a < bit0 b), { assume h, have A : bit0 b < omega, by simpa using hb, exact lt_irrefl _ (lt_trans (lt_of_lt_of_le A ha) h) }, simp [I1, I2] }, { rw [bit0_eq_self hb], simp only [not_le] at ha, have I1 : a < b := (lt_of_lt_of_le ha hb), have I2 : bit1 a < b := lt_of_lt_of_le (bit1_lt_omega.2 ha) hb, simp [I1, I2] }, { simp at ha hb, rcases lt_omega.1 ha with ⟨m, rfl⟩, rcases lt_omega.1 hb with ⟨n, rfl⟩, norm_cast, simp } end @[simp] lemma bit1_lt_bit1 {a b : cardinal} : bit1 a < bit1 b ↔ a < b := begin by_cases ha : omega ≤ a; by_cases hb : omega ≤ b, { rw [bit1_eq_self_iff.2 ha, bit1_eq_self_iff.2 hb], }, { rw bit1_eq_self_iff.2 ha, have I1 : ¬ (a < b), { assume h, apply hb, exact le_of_lt (lt_of_le_of_lt ha h) }, have I2 : ¬ (a < bit1 b), { assume h, have A : bit1 b < omega, by simpa using hb, exact lt_irrefl _ (lt_trans (lt_of_lt_of_le A ha) h) }, simp [I1, I2] }, { rw [bit1_eq_self_iff.2 hb], simp only [not_le] at ha, have I1 : a < b := (lt_of_lt_of_le ha hb), have I2 : bit1 a < b := lt_of_lt_of_le (bit1_lt_omega.2 ha) hb, simp [I1, I2] }, { simp at ha hb, rcases lt_omega.1 ha with ⟨m, rfl⟩, rcases lt_omega.1 hb with ⟨n, rfl⟩, norm_cast, simp } end @[simp] lemma bit0_lt_bit1 {a b : cardinal} : bit0 a < bit1 b ↔ (a < b ∨ (a ≤ b ∧ a < omega)) := begin by_cases ha : omega ≤ a; by_cases hb : omega ≤ b, { simp [bit0_eq_self ha, bit1_eq_self_iff.2 hb, not_lt.mpr ha] }, { rw bit0_eq_self ha, have I1 : ¬ (a < b), { assume h, apply hb, exact le_of_lt (lt_of_le_of_lt ha h) }, have I2 : ¬ (a < bit1 b), { assume h, have A : bit1 b < omega, by simpa using hb, exact lt_irrefl _ (lt_trans (lt_of_lt_of_le A ha) h) }, simp [I1, I2, not_lt.mpr ha] }, { rw [bit1_eq_self_iff.2 hb], simp only [not_le] at ha, have I1 : a < b := (lt_of_lt_of_le ha hb), have I2 : bit0 a < b := lt_of_lt_of_le (bit0_lt_omega.2 ha) hb, simp [I1, I2] }, { simp at ha hb, rcases lt_omega.1 ha with ⟨m, rfl⟩, rcases lt_omega.1 hb with ⟨n, rfl⟩, norm_cast, simp only [ha, and_true, nat.bit0_lt_bit1_iff], refine ⟨λ h, or.inr h, λ h, _⟩, cases h, { exact le_of_lt h }, { exact h } } end lemma one_lt_two : (1 : cardinal) < 2 := -- This strategy works generally to prove inequalities between numerals in `cardinality`. by { norm_cast, norm_num } @[simp] lemma one_lt_bit0 {a : cardinal} : 1 < bit0 a ↔ 0 < a := by simp [← bit1_zero] @[simp] lemma one_lt_bit1 (a : cardinal) : 1 < bit1 a ↔ 0 < a := by simp [← bit1_zero] @[simp] lemma one_le_one : (1 : cardinal) ≤ 1 := le_refl _ end bit end cardinal
5a6adce5adde0132b83eec73526e13f5d9752a1d
4bcaca5dc83d49803f72b7b5920b75b6e7d9de2d
/src/Lean/PrettyPrinter/Delaborator/Basic.lean
d9612f3f706713a80c0f6732aa8f4eca62c37e55
[ "Apache-2.0" ]
permissive
subfish-zhou/leanprover-zh_CN.github.io
30b9fba9bd790720bd95764e61ae796697d2f603
8b2985d4a3d458ceda9361ac454c28168d920d3f
refs/heads/master
1,689,709,967,820
1,632,503,056,000
1,632,503,056,000
409,962,097
1
0
null
null
null
null
UTF-8
Lean
false
false
12,270
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sebastian Ullrich -/ import Lean.KeyedDeclsAttribute import Lean.ProjFns import Lean.Syntax import Lean.Meta.Match.Match import Lean.Elab.Term import Lean.PrettyPrinter.Delaborator.Options import Lean.PrettyPrinter.Delaborator.SubExpr import Lean.PrettyPrinter.Delaborator.TopDownAnalyze /-! 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. -/ namespace Lean.PrettyPrinter.Delaborator open Lean.Meta SubExpr open Lean.Elab (Info TermInfo Info.ofTermInfo) structure Context where defaultOptions : Options optionsPerPos : OptionsPerPos currNamespace : Name openDecls : List OpenDecl inPattern : Bool := false -- true when delaborating `match` patterns subExpr : SubExpr structure State where /-- We attach `Elab.Info` at various locations in the `Syntax` output in order to convey its semantics. While the elaborator emits `InfoTree`s, here we have no real text location tree to traverse, so we use a flattened map. -/ infos : Std.RBMap Pos Info compare := {} /-- See `SubExpr.nextExtraPos`. -/ holeIter : SubExpr.HoleIterator := {} -- 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 (StateRefT State MetaM) abbrev Delab := DelabM Syntax instance : Inhabited (DelabM α) where default := throw arbitrary @[inline] protected def orElse (d₁ : DelabM α) (d₂ : Unit → DelabM α) : DelabM α := do catchInternalId delabFailureId d₁ fun _ => d₂ () protected def failure : DelabM α := throw $ Exception.internal delabFailureId instance : Alternative DelabM where orElse := Delaborator.orElse failure := Delaborator.failure -- HACK: necessary since it would otherwise prefer the instance from MonadExcept instance {α} : OrElse (DelabM α) := ⟨Delaborator.orElse⟩ -- Low priority instances so `read`/`get`/etc default to the whole `Context`/`State` instance (priority := low) : MonadReaderOf SubExpr DelabM where read := Context.subExpr <$> read instance (priority := low) : MonadWithReaderOf SubExpr DelabM where withReader f x := fun ctx => x { ctx with subExpr := f ctx.subExpr } instance (priority := low) : MonadStateOf SubExpr.HoleIterator DelabM where get := State.holeIter <$> get set iter := modify fun ⟨infos, _⟩ => ⟨infos, iter⟩ modifyGet f := modifyGet fun ⟨infos, iter⟩ => let (ret, iter') := f iter; (ret, ⟨infos, iter'⟩) -- 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 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 def getOptionsAtCurrPos : DelabM Options := do let ctx ← read let mut opts := ctx.defaultOptions if let some opts' ← ctx.optionsPerPos.find? (← getPos) then for (k, v) in opts' do opts := opts.insert k v opts /-- Evaluate option accessor, using subterm-specific options if set. -/ def getPPOption (opt : Options → Bool) : DelabM Bool := do return opt (← getOptionsAtCurrPos) 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 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 annotatePos (← getPos) 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 def addTermInfo (pos : Pos) (stx : Syntax) (e : Expr) (isBinder : Bool := false) : DelabM Unit := do let info ← mkTermInfo stx e isBinder modify fun s => { s with infos := s.infos.insert pos info } where mkTermInfo stx e isBinder := do Info.ofTermInfo { elaborator := `Delab, stx := stx, lctx := (← getLCtx), expectedType? := none, expr := e, isBinder := isBinder } def addFieldInfo (pos : Pos) (projName fieldName : Name) (stx : Syntax) (val : Expr) : DelabM Unit := do let info ← mkFieldInfo projName fieldName stx val modify fun s => { s with infos := s.infos.insert pos info } where mkFieldInfo projName fieldName stx val := do Info.ofFieldInfo { projName := projName, fieldName := fieldName, lctx := (← getLCtx), val := val, stx := stx } partial def delabFor : Name → Delab | Name.anonymous => failure | k => (do let stx ← (delabAttribute.getValues (← getEnv) k).firstM id let stx ← annotateCurPos stx addTermInfo (← getPos) stx (← getExpr) stx) -- have `app.Option.some` fall back to `app` etc. <|> delabFor k.getRoot partial def delab : Delab := do checkMaxHeartbeats "delab" let e ← getExpr -- no need to hide atomic proofs if ← !e.isAtomic <&&> !(← getPPOption getPPProofs) <&&> (try Meta.isProof e catch ex => false) then if ← getPPOption getPPProofsWithType then let stx ← withType delab return ← ``((_ : $stx)) else return ← ``(_) let k ← getExprKind let stx ← delabFor k <|> (liftM $ show MetaM Syntax from throwError "don't know how to delaborate '{k}'") if ← getPPOption getPPAnalyzeTypeAscriptions <&&> getPPOption getPPAnalysisNeedsType <&&> !e.isMData then let typeStx ← withType delab `(($stx:term : $typeStx:term)) >>= annotateCurPos else stx 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 open Delaborator (OptionsPerPos topDownAnalyze Pos) def delabCore (currNamespace : Name) (openDecls : List OpenDecl) (e : Expr) (optionsPerPos : OptionsPerPos := {}) : MetaM (Syntax × Std.RBMap Pos Elab.Info compare) := do trace[PrettyPrinter.delab.input] "{Std.format e}" let mut opts ← MonadOptions.getOptions -- default `pp.proofs` to `true` if `e` is a proof if pp.proofs.get? opts == none then try if ← Meta.isProof e then opts := pp.proofs.set opts true catch _ => pure () let e ← if getPPInstantiateMVars opts then ← Meta.instantiateMVars e else e let optionsPerPos ← if !getPPAll opts && getPPAnalyze opts && optionsPerPos.isEmpty then withTheReader Core.Context (fun ctx => { ctx with options := opts }) do topDownAnalyze e else optionsPerPos let (stx, {infos := infos, ..}) ← catchInternalId Delaborator.delabFailureId (Delaborator.delab { defaultOptions := opts optionsPerPos := optionsPerPos currNamespace := currNamespace openDecls := openDecls subExpr := Delaborator.SubExpr.mkRoot e } |>.run { : Delaborator.State }) (fun _ => unreachable!) return (stx, infos) /-- "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 let (stx, _) ← delabCore currNamespace openDecls e optionsPerPos stx builtin_initialize registerTraceClass `PrettyPrinter.delab end Lean.PrettyPrinter
f33e0a7bee2d553aa32a70b34ace7a83d1ceba47
e61a235b8468b03aee0120bf26ec615c045005d2
/stage0/src/Init/Lean/Util/CollectFVars.lean
040bf9a2c9b3bbf0caa4d95cc41c920af366f652
[ "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
1,191
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import Init.Lean.Expr namespace Lean namespace CollectFVars structure State := (visitedExpr : ExprSet := {}) (fvarSet : NameSet := {}) instance State.inhabited : Inhabited State := ⟨{}⟩ abbrev Visitor := State → State @[inline] def visit (f : Expr → Visitor) (e : Expr) : Visitor := fun s => if !e.hasFVar || s.visitedExpr.contains e then s else f e { s with visitedExpr := s.visitedExpr.insert e } partial def main : Expr → Visitor | Expr.proj _ _ e _ => visit main e | Expr.forallE _ d b _ => visit main b ∘ visit main d | Expr.lam _ d b _ => visit main b ∘ visit main d | Expr.letE _ t v b _ => visit main b ∘ visit main v ∘ visit main t | Expr.app f a _ => visit main a ∘ visit main f | Expr.mdata _ b _ => visit main b | Expr.fvar fvarId _ => fun s => { s with fvarSet := s.fvarSet.insert fvarId } | _ => id end CollectFVars def collectFVars (s : CollectFVars.State) (e : Expr) : CollectFVars.State := CollectFVars.main e s end Lean
8a4b09eec18e70e31cb83a9190395048e81eee4a
6e36ebd5594a0d512dea8bc6ffe78c71b5b5032d
/src/mywork/Lectures/lecture_10.lean
1884974f064e712905fe148c5c7f3390c42fa688
[]
no_license
wrw2ztk/cs2120f21
cdc4b1b4043c8ae8f3c8c3c0e91cdacb2cfddb16
f55df4c723d3ce989908679f5653e4be669334ae
refs/heads/main
1,691,764,473,342
1,633,707,809,000
1,633,707,809,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
9,215
lean
/- In today's class, we'll continue with our exploration of the proposition, "false", its elimination rule, and their vital uses in logical reasoning: especially in - proof of ¬P by negation - proof of P by false elimination Here are the inference rules in display notation: NEGATION INTRODUCTION The first, proof by negation, says that from a proof of P → false you can derive a proof of ¬P. Indeed! It's definitional. Recall: def not (P : Prop) := P → false. (P : Prop) (np : P → false) --------------------------- [by defn'n] (pf : ¬P) This rule is the foundation for "proof by negation." To prove ¬P you first assume P, is true, then you show that in this context you can derive a contradiction. What you have then shown, of course, is P → false. So, to prove ¬P, assume P and show that in this context there is a contradiction. This is proof by negation. It is not to be confused with proof by contradition, which is a different thing entirely. (Proof by contradiction. You can use this approach to a proposition, P, by assuming ¬P and showing that this assumption leads to a contradiction. That proves ¬¬P. Then you use the *indepedent* axiom of negation elimination to infer P from ¬¬P.) FALSE ELIMINATION The second rule says that if you have a proof of false and any other proposition, P, the logic is useless and so you might as well just admit that P is true. Why is the logic useless? Well, if false is true then there's no longer a difference! In this situation, tHere's sense in reasoning any further, and you can just dismiss it. A contradiction makes a logic inconsistent. (P : Prop) (f : false) ---------------------- (false_elim f) (pf : P) -/ /- We covered the relatively simpler notion of negation introduction last time, so today we will start by focusing on false elimination. Understanding why it's not magic and in fact makes perfect sense takes a bit of thought. -/ /- To make sense of false elimination, think in terms of case analysis. Proof by case analysis says that if the truth of some proposition, Y, follows from *any possible form of proof* of X, then you've proved X → Y. If X is a disjunction, P ∨ Q (so now you want to prove P ∨ Q → Y) you must consider two cases: one where P is assumed true and one where Q is assumed true. If X is a proposition with just one form of proof (e.g., the proposition, true), there's just one case to consider. -/ -- two cases example : true ∨ false → true := begin assume h, cases h, assumption, -- context has exact proof contradiction, -- context has contradiction end -- one case example : true → true := begin assume t, cases t, -- just one case exact true.intro, end /- How many cases must you consider if you putatively have a putative proof of false? ZERO! To consider *all* cases you need consider exactly zero cases. Proving all cases when the number of cases is zero is trivial, so the conclusion *follows*. -/ example : false → false := begin assume f, cases f, -- case analysis: there are no cases! end /- In fact, it doesn't matter what your conclusion is: it will always be true in a context in which you have a proof of false. And this makes sense, because if you have a proof of false, then false is true, so whether a given proposition is true or false, it's true, because even if it's false, well, false is true, so it's also true! -/ theorem false_elim : ∀ (P : Prop), false → P := begin assume P, assume f, cases f, end /- This, then, is the general principle for false elimination: ANYTHING FOLLOWS FROM FALSE. This principle gives you a powerful way to prove any proposition is true (conditional on your having a proof that can't exist). The theorem states that if you're given any proposition, P, and a proof, f, of false, then in that context, you can return a proof of P by using false elimination. The only problem with this super-power is that in reality, there is no proof of false, so there's no real danger of any old proposition being automatically true! The rule of false elimination tells you that if you're in a situation that can't happen, then no worries, be happy, you're done (just use false elimination). -/ /- The elimination principle for false is called false.elim in Lean. If you are given or can derive a proof, f, of false, then all you have to do to finish your proof is to say, "this is situation can't happen, so we need not consider it any further." Or, formally, (false.elim f). -/ -- Suppose P is *any* (an arbitrary) proposition axiom P : Prop example : false → P := begin assume f, exact false.elim f, -- Using Lean's version -- P is implicit argument end /- SOME THEOREMS INVOLVING FALSE AND NEGATION -/ theorem no_contradiction : ∀ (P : Prop), ¬(P ∧ ¬P) := begin assume P, assume panp, cases panp, contradiction, end theorem no_contradiction : ∀ (P : Prop), ¬(P ∧ ¬P) := begin assume P, assume h, have p := h.left, have np := h.right, have f := np p, --^^^^ This is really important exact f, end /- The so-called "law" (it's really an axiom) of the excluded middle says that any proposition is either true or false. There's no middle ground. But in the constructive logic of Lean, this isn't true. To prove P ∨ ¬P, as you recall, we need to have either a proof of P (in this case use or.intro_left) or a proof of ¬P, in which case we use or.intro_right to prove P ∨ ¬P. But what if we don't have a proof either way? There are many important questions in computer science and mathematics where we don't know either way. If you call one of those propositions, P, and try to prove P ∨ ¬P in Lean, you just get stuck. -/ theorem excluded_middle' : ∀ (P : Prop), (P ∨ ¬P) := begin assume P, -- we don't have a proof of either P or of ¬P! end /- Let P be the conjecture, "every even whole number greater than 2 is the sum of two prime numbers." This conjecture, dating (in a slightly different form) to a letter from Goldback to Euler in 1742 is still neither proved nor disproved! Below you will find a placeholder for a proof. Just fill it in (correctly) and you will win $1M and probably a Fields Medal (the "Nobel Prize" in mathematics). -/ axioms (ev : ℕ → Prop) (prime : ℕ → Prop) def goldbach_conjecture := ∀ (n : ℕ), n > 2 → (∃ h k : ℕ, n = h + k ∧ prime h ∧ prime k) theorem goldbach_conjecture_true : goldbach_conjecture := _ theorem goldbach_conjecture_false : ¬goldbach_conjecture := _ example : goldbach_conjecture ∨ ¬goldbach_conjecture := _ /- Our only options are or.intro_left or or.intro_right, but we don't have a required argument (proof) to use either of them! -/ /- The axioms of the constructive logic of Lean are not strong enough to prove the "law of the excluded middle." Rather, if we want to use it, we have to accept it as an additional axiom. We thus have two different logics: one without and one with the law of the excluded middle! -/ axiom excluded_middle : ∀ (P : Prop), (P ∨ ¬P) /- Now, for any proposition whatsoever, P, we can always prove P ∨ ¬P by applying excluded middle to P (using the elimination rule for ∀). What we get in return is a proof of P ∨ ¬P for whatever proposition P is. Here is an example where the proposition is ItsRaining. -/ axiom ItsRaining : Prop theorem example_em : ItsRaining ∨ ¬ItsRaining := begin apply excluded_middle ItsRaining, end /- PROOF BY NEGATION, EXAMPLE -/ /- Next we return to the negation connective, which in logic is pronounced "not." Given any proposition, P, ¬P is also a proposition; and what it means is, exactly, P → false. If P is true, then false is true, and false cannot possibly be true, so P must not be. Thus, to prove ¬P you prove P → false. Another way to read P → false is that, if we assume that P is true, we can derive a proof of false, which cannot exist, so P must not be true. MEMORIZE THIS REASONING. Again, to prove ¬P, *assume P and show that in that context, you can derive a contradiction in the form of a proof of false. This is the strategy call proof by contradiction. -/ /- How about we prove ¬(0 = 1) in English. Proof. By negation. Assume 0 = 1 and show that this leads to a contradiction. That is, show 0 = 1 → false. The rest of the proof is by case analysis on the assumed proof of 0 = 1. The only way to have a proof of equality is by the reflexive property of =. But the reflexive property doesn't imply that there's a proof of 0 = 1. So there can be no proof of 0 = 1, and the assumption that 0 = 1 is a contradiction. We finish the proof by case analysis on the possible proofs of 0 = 1, of which there are zero. So in all (zero!) cases, the conclusion (false) follows. Therefore 0 = 1 → false, which is to say, ¬(0 = 1) is proved and thus true. QED. -/ example : ¬0 = 1 := begin show 0 = 1 → false, -- rewrite goal to a definitionally equal goal assume h, cases h, -- there are *zero* ways to build a proof of 0 = 1 -- case analysis discharges our "proof obligation" end
798dbffe95566c5641b24ab3fea01422596c4338
618003631150032a5676f229d13a079ac875ff77
/src/category_theory/limits/functor_category.lean
e4f743eaaec4bb6eec97fdebecdbc45b99971bfa
[ "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
4,819
lean
/- Copyright (c) 2018 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import category_theory.limits.preserves open category_theory category_theory.category namespace category_theory.limits universes v u -- declare the `v`'s first; see `category_theory.category` for an explanation variables {C : Type u} [category.{v} C] variables {J K : Type v} [small_category J] [small_category K] @[simp] lemma cone.functor_w {F : J ⥤ (K ⥤ C)} (c : cone F) {j j' : J} (f : j ⟶ j') (k : K) : (c.π.app j).app k ≫ (F.map f).app k = (c.π.app j').app k := by convert ←nat_trans.congr_app (c.π.naturality f).symm k; apply id_comp @[simp] lemma cocone.functor_w {F : J ⥤ (K ⥤ C)} (c : cocone F) {j j' : J} (f : j ⟶ j') (k : K) : (F.map f).app k ≫ (c.ι.app j').app k = (c.ι.app j).app k := by convert ←nat_trans.congr_app (c.ι.naturality f) k; apply comp_id @[simps] def functor_category_limit_cone [has_limits_of_shape J C] (F : J ⥤ K ⥤ C) : cone F := { X := F.flip ⋙ lim, π := { app := λ j, { app := λ k, limit.π (F.flip.obj k) j }, naturality' := λ j j' f, by ext k; convert (limit.w (F.flip.obj k) _).symm using 1; apply id_comp } } @[simps] def functor_category_colimit_cocone [has_colimits_of_shape J C] (F : J ⥤ K ⥤ C) : cocone F := { X := F.flip ⋙ colim, ι := { app := λ j, { app := λ k, colimit.ι (F.flip.obj k) j }, naturality' := λ j j' f, by ext k; convert (colimit.w (F.flip.obj k) _) using 1; apply comp_id } } @[simp] def evaluate_functor_category_limit_cone [has_limits_of_shape J C] (F : J ⥤ K ⥤ C) (k : K) : ((evaluation K C).obj k).map_cone (functor_category_limit_cone F) ≅ limit.cone (F.flip.obj k) := cones.ext (iso.refl _) (by tidy) @[simp] def evaluate_functor_category_colimit_cocone [has_colimits_of_shape J C] (F : J ⥤ K ⥤ C) (k : K) : ((evaluation K C).obj k).map_cocone (functor_category_colimit_cocone F) ≅ colimit.cocone (F.flip.obj k) := cocones.ext (iso.refl _) (by tidy) def functor_category_is_limit_cone [has_limits_of_shape J C] (F : J ⥤ K ⥤ C) : is_limit (functor_category_limit_cone F) := { lift := λ s, { app := λ k, limit.lift (F.flip.obj k) (((evaluation K C).obj k).map_cone s) }, uniq' := λ s m w, begin ext1, ext1 k, exact is_limit.uniq _ (((evaluation K C).obj k).map_cone s) (m.app k) (λ j, nat_trans.congr_app (w j) k) end } def functor_category_is_colimit_cocone [has_colimits_of_shape.{v} J C] (F : J ⥤ K ⥤ C) : is_colimit (functor_category_colimit_cocone F) := { desc := λ s, { app := λ k, colimit.desc (F.flip.obj k) (((evaluation K C).obj k).map_cocone s) }, uniq' := λ s m w, begin ext1, ext1 k, exact is_colimit.uniq _ (((evaluation K C).obj k).map_cocone s) (m.app k) (λ j, nat_trans.congr_app (w j) k) end } instance functor_category_has_limits_of_shape [has_limits_of_shape J C] : has_limits_of_shape J (K ⥤ C) := { has_limit := λ F, { cone := functor_category_limit_cone F, is_limit := functor_category_is_limit_cone F } } instance functor_category_has_colimits_of_shape [has_colimits_of_shape J C] : has_colimits_of_shape J (K ⥤ C) := { has_colimit := λ F, { cocone := functor_category_colimit_cocone F, is_colimit := functor_category_is_colimit_cocone F } } instance functor_category_has_limits [has_limits.{v} C] : has_limits.{v} (K ⥤ C) := { has_limits_of_shape := λ J 𝒥, by resetI; apply_instance } instance functor_category_has_colimits [has_colimits.{v} C] : has_colimits.{v} (K ⥤ C) := { has_colimits_of_shape := λ J 𝒥, by resetI; apply_instance } instance evaluation_preserves_limits_of_shape [has_limits_of_shape J C] (k : K) : preserves_limits_of_shape J ((evaluation K C).obj k) := { preserves_limit := λ F, preserves_limit_of_preserves_limit_cone (limit.is_limit _) $ is_limit.of_iso_limit (limit.is_limit _) (evaluate_functor_category_limit_cone F k).symm } instance evaluation_preserves_colimits_of_shape [has_colimits_of_shape J C] (k : K) : preserves_colimits_of_shape J ((evaluation K C).obj k) := { preserves_colimit := λ F, preserves_colimit_of_preserves_colimit_cocone (colimit.is_colimit _) $ is_colimit.of_iso_colimit (colimit.is_colimit _) (evaluate_functor_category_colimit_cocone F k).symm } instance evaluation_preserves_limits [has_limits.{v} C] (k : K) : preserves_limits ((evaluation K C).obj k) := { preserves_limits_of_shape := λ J 𝒥, by resetI; apply_instance } instance evaluation_preserves_colimits [has_colimits.{v} C] (k : K) : preserves_colimits ((evaluation K C).obj k) := { preserves_colimits_of_shape := λ J 𝒥, by resetI; apply_instance } end category_theory.limits
17f3b5452207ecd1c27ff2759bad567d44bc5336
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/analysis/quaternion.lean
c4688009f216bd1a4a032f9408799126a5a67b32
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
3,361
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.quaternion import analysis.normed_space.inner_product /-! # Quaternions as a normed algebra In this file we define the following structures on the space `ℍ := ℍ[ℝ]` of quaternions: * inner product space; * normed ring; * normed space over `ℝ`. ## Notation The following notation is available with `open_locale quaternion`: * `ℍ` : quaternions ## Tags quaternion, normed ring, normed space, normed algebra -/ localized "notation `ℍ` := quaternion ℝ" in quaternion open_locale real_inner_product_space noncomputable theory namespace quaternion instance : has_inner ℝ ℍ := ⟨λ a b, (a * b.conj).re⟩ lemma inner_self (a : ℍ) : ⟪a, a⟫ = norm_sq a := rfl lemma inner_def (a b : ℍ) : ⟪a, b⟫ = (a * b.conj).re := rfl instance : inner_product_space ℝ ℍ := inner_product_space.of_core { inner := has_inner.inner, conj_sym := λ x y, by simp [inner_def, mul_comm], nonneg_re := λ x, norm_sq_nonneg, definite := λ x, norm_sq_eq_zero.1, add_left := λ x y z, by simp only [inner_def, add_mul, add_re], smul_left := λ x y r, by simp [inner_def] } lemma norm_sq_eq_norm_sq (a : ℍ) : norm_sq a = ∥a∥ * ∥a∥ := by rw [← inner_self, real_inner_self_eq_norm_sq] instance : norm_one_class ℍ := ⟨by rw [norm_eq_sqrt_real_inner, inner_self, norm_sq.map_one, real.sqrt_one]⟩ @[simp] lemma norm_mul (a b : ℍ) : ∥a * b∥ = ∥a∥ * ∥b∥ := begin simp only [norm_eq_sqrt_real_inner, inner_self, norm_sq.map_mul], exact real.sqrt_mul norm_sq_nonneg _ end @[simp, norm_cast] lemma norm_coe (a : ℝ) : ∥(a : ℍ)∥ = ∥a∥ := by rw [norm_eq_sqrt_real_inner, inner_self, norm_sq_coe, real.sqrt_sq_eq_abs, real.norm_eq_abs] noncomputable instance : normed_ring ℍ := { dist_eq := λ _ _, rfl, norm_mul := λ a b, (norm_mul a b).le } noncomputable instance : normed_algebra ℝ ℍ := { norm_algebra_map_eq := norm_coe, to_algebra := quaternion.algebra } instance : has_coe ℂ ℍ := ⟨λ z, ⟨z.re, z.im, 0, 0⟩⟩ @[simp, norm_cast] lemma coe_complex_re (z : ℂ) : (z : ℍ).re = z.re := rfl @[simp, norm_cast] lemma coe_complex_im_i (z : ℂ) : (z : ℍ).im_i = z.im := rfl @[simp, norm_cast] lemma coe_complex_im_j (z : ℂ) : (z : ℍ).im_j = 0 := rfl @[simp, norm_cast] lemma coe_complex_im_k (z : ℂ) : (z : ℍ).im_k = 0 := rfl @[simp, norm_cast] lemma coe_complex_add (z w : ℂ) : ↑(z + w) = (z + w : ℍ) := by ext; simp @[simp, norm_cast] lemma coe_complex_mul (z w : ℂ) : ↑(z * w) = (z * w : ℍ) := by ext; simp @[simp, norm_cast] lemma coe_complex_zero : ((0 : ℂ) : ℍ) = 0 := rfl @[simp, norm_cast] lemma coe_complex_one : ((1 : ℂ) : ℍ) = 1 := rfl @[simp, norm_cast] lemma coe_real_complex_mul (r : ℝ) (z : ℂ) : (r • z : ℍ) = ↑r * ↑z := by ext; simp @[simp, norm_cast] lemma coe_complex_coe (r : ℝ) : ((r : ℂ) : ℍ) = r := rfl /-- Coercion `ℂ →ₐ[ℝ] ℍ` as an algebra homomorphism. -/ def of_complex : ℂ →ₐ[ℝ] ℍ := { to_fun := coe, map_one' := rfl, map_zero' := rfl, map_add' := coe_complex_add, map_mul' := coe_complex_mul, commutes' := λ x, rfl } @[simp] lemma coe_of_complex : ⇑of_complex = coe := rfl end quaternion
f580389b2b4613b3f6b9a7c6256bb21cfd28d531
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/analysis/normed_space/indicator_function_auto.lean
b6655dc0cb70342a538e6103a63a27e9b5a43708
[]
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
1,973
lean
/- Copyright (c) 2020 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou, Yury Kudryashov -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.indicator_function import Mathlib.analysis.normed_space.basic import Mathlib.PostPort universes u_1 u_2 namespace Mathlib /-! # Indicator function and norm This file contains a few simple lemmas about `set.indicator` and `norm`. ## Tags indicator, norm -/ theorem norm_indicator_eq_indicator_norm {α : Type u_1} {E : Type u_2} [normed_group E] {s : set α} (f : α → E) (a : α) : norm (set.indicator s f a) = set.indicator s (fun (a : α) => norm (f a)) a := flip congr_fun a (Eq.symm (set.indicator_comp_of_zero norm_zero)) theorem nnnorm_indicator_eq_indicator_nnnorm {α : Type u_1} {E : Type u_2} [normed_group E] {s : set α} (f : α → E) (a : α) : nnnorm (set.indicator s f a) = set.indicator s (fun (a : α) => nnnorm (f a)) a := flip congr_fun a (Eq.symm (set.indicator_comp_of_zero nnnorm_zero)) theorem norm_indicator_le_of_subset {α : Type u_1} {E : Type u_2} [normed_group E] {s : set α} {t : set α} (h : s ⊆ t) (f : α → E) (a : α) : norm (set.indicator s f a) ≤ norm (set.indicator t f a) := sorry theorem indicator_norm_le_norm_self {α : Type u_1} {E : Type u_2} [normed_group E] {s : set α} (f : α → E) (a : α) : set.indicator s (fun (a : α) => norm (f a)) a ≤ norm (f a) := set.indicator_le_self' (fun (_x : α) (_x : ¬_x ∈ s) => norm_nonneg (f a)) a theorem norm_indicator_le_norm_self {α : Type u_1} {E : Type u_2} [normed_group E] {s : set α} (f : α → E) (a : α) : norm (set.indicator s f a) ≤ norm (f a) := eq.mpr (id (Eq._oldrec (Eq.refl (norm (set.indicator s f a) ≤ norm (f a))) (norm_indicator_eq_indicator_norm f a))) (indicator_norm_le_norm_self (fun (a : α) => f a) a) end Mathlib
f86cae6864b8c0b1f1dd8c46cf0ed6c6b6af104d
271e26e338b0c14544a889c31c30b39c989f2e0f
/stage0/src/Init/Default.lean
2b16aeac7ad6e6e85f48fef6fa4ebfe62f33dfce
[ "Apache-2.0" ]
permissive
dgorokho/lean4
805f99b0b60c545b64ac34ab8237a8504f89d7d4
e949a052bad59b1c7b54a82d24d516a656487d8a
refs/heads/master
1,607,061,363,851
1,578,006,086,000
1,578,006,086,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
332
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import Init.Core import Init.Control import Init.Data.Basic import Init.Coe import Init.WF import Init.Data import Init.System import Init.Util import Init.Fix
18d5ff3d68344e3af535478117d1a7915903b4d1
e0f9ba56b7fedc16ef8697f6caeef5898b435143
/test/lint.lean
a12b920ab1f69106a7c01a9c8fdca1c207f43bc2
[ "Apache-2.0" ]
permissive
anrddh/mathlib
6a374da53c7e3a35cb0298b0cd67824efef362b4
a4266a01d2dcb10de19369307c986d038c7bb6a6
refs/heads/master
1,656,710,827,909
1,589,560,456,000
1,589,560,456,000
264,271,800
0
0
Apache-2.0
1,589,568,062,000
1,589,568,061,000
null
UTF-8
Lean
false
false
4,300
lean
import tactic.lint def foo1 (n m : ℕ) : ℕ := n + 1 def foo2 (n m : ℕ) : m = m := by refl lemma foo3 (n m : ℕ) : ℕ := n - m lemma foo.foo (n m : ℕ) : n ≥ n := le_refl n instance bar.bar : has_add ℕ := by apply_instance -- we don't check the name of instances lemma foo.bar (ε > 0) : ε = ε := rfl -- >/≥ is allowed in binders (and in fact, in all hypotheses) -- section -- local attribute [instance, priority 1001] classical.prop_decidable -- lemma foo4 : (if 3 = 3 then 1 else 2) = 1 := if_pos (by refl) -- end open tactic meta def fold_over_with_cond {α} (l : list declaration) (tac : declaration → tactic (option α)) : tactic (list (declaration × α)) := l.mmap_filter $ λ d, option.map (λ x, (d, x)) <$> tac d run_cmd do let t := name × list ℕ, e ← get_env, let l := e.filter (λ d, e.in_current_file' d.to_name ∧ ¬ d.is_auto_or_internal e), l2 ← fold_over_with_cond l (return ∘ check_unused_arguments), guard $ l2.length = 4, let l2 : list t := l2.map $ λ x, ⟨x.1.to_name, x.2⟩, guard $ (⟨`foo1, [2]⟩ : t) ∈ l2, guard $ (⟨`foo2, [1]⟩ : t) ∈ l2, guard $ (⟨`foo.foo, [2]⟩ : t) ∈ l2, guard $ (⟨`foo.bar, [2]⟩ : t) ∈ l2, l2 ← fold_over_with_cond l linter.def_lemma.test, guard $ l2.length = 2, let l2 : list (name × _) := l2.map $ λ x, ⟨x.1.to_name, x.2⟩, guard $ ∃(x ∈ l2), (x : name × _).1 = `foo2, guard $ ∃(x ∈ l2), (x : name × _).1 = `foo3, l3 ← fold_over_with_cond l linter.dup_namespace.test, guard $ l3.length = 1, guard $ ∃(x ∈ l3), (x : declaration × _).1.to_name = `foo.foo, l4 ← fold_over_with_cond l linter.ge_or_gt.test, guard $ l4.length = 1, guard $ ∃(x ∈ l4), (x : declaration × _).1.to_name = `foo.foo, -- guard $ ∃(x ∈ l4), (x : declaration × _).1.to_name = `foo4, (_, s) ← lint ff, guard $ "/- (slow tests skipped) -/\n".is_suffix_of s.to_string, (_, s2) ← lint tt, guard $ s.to_string ≠ s2.to_string, skip /- check customizability and nolint -/ meta def dummy_check (d : declaration) : tactic (option string) := return $ if d.to_name.last = "foo" then some "gotcha!" else none meta def linter.dummy_linter : linter := { test := dummy_check, auto_decls := ff, no_errors_found := "found nothing", errors_found := "found something" } @[nolint dummy_linter] def bar.foo : (if 3 = 3 then 1 else 2) = 1 := if_pos (by refl) run_cmd do (_, s) ← lint tt tt [`linter.dummy_linter] tt, guard $ "/- found something: -/\n#print foo.foo /- gotcha! -/\n".is_suffix_of s.to_string def incorrect_type_class_argument_test {α : Type} (x : α) [x = x] [decidable_eq α] [group α] : unit := () run_cmd do d ← get_decl `incorrect_type_class_argument_test, x ← linter.incorrect_type_class_argument.test d, guard $ x = some "These are not classes. argument 3: [_inst_1 : x = x]" section def impossible_instance_test {α β : Type} [add_group α] : has_add α := infer_instance local attribute [instance] impossible_instance_test run_cmd do d ← get_decl `impossible_instance_test, x ← linter.impossible_instance.test d, guard $ x = some "Impossible to infer argument 2: {β : Type}" def dangerous_instance_test {α β γ : Type} [ring α] [add_comm_group β] [has_coe α β] [has_inv γ] : has_add β := infer_instance local attribute [instance] dangerous_instance_test run_cmd do d ← get_decl `dangerous_instance_test, x ← linter.dangerous_instance.test d, guard $ x = some "The following arguments become metavariables. argument 1: {α : Type}, argument 3: {γ : Type}" end section def foo_has_mul {α} [has_mul α] : has_mul α := infer_instance local attribute [instance, priority 1] foo_has_mul run_cmd do d ← get_decl `has_mul, some s ← fails_quickly 500 d, guard $ s = "type-class inference timed out" local attribute [instance, priority 10000] foo_has_mul run_cmd do d ← get_decl `has_mul, some s ← fails_quickly 3000 d, guard $ "maximum class-instance resolution depth has been reached".is_prefix_of s end /- test of `apply_to_fresh_variables` -/ run_cmd do e ← mk_const `id, e2 ← apply_to_fresh_variables e, type_check e2, `(@id %%α %%a) ← instantiate_mvars e2, expr.sort (level.succ $ level.mvar u) ← infer_type α, skip
a674ca20e0e318ae1eaa7ff5eb67d7c3a860e149
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/algebra/group_power/basic.lean
3fa7894c8f747921d27c7c5436cdff09c3e0b7da
[ "Apache-2.0" ]
permissive
robertylewis/mathlib
3d16e3e6daf5ddde182473e03a1b601d2810952c
1d13f5b932f5e40a8308e3840f96fc882fae01f0
refs/heads/master
1,651,379,945,369
1,644,276,960,000
1,644,276,960,000
98,875,504
0
0
Apache-2.0
1,644,253,514,000
1,501,495,700,000
Lean
UTF-8
Lean
false
false
14,189
lean
/- Copyright (c) 2015 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Robert Y. Lewis -/ import data.nat.basic import tactic.monotonicity.basic import group_theory.group_action.defs /-! # Power operations on monoids and groups The power operation on monoids and groups. We separate this from group, because it depends on `ℕ`, which in turn depends on other parts of algebra. This module contains lemmas about `a ^ n` and `n • a`, where `n : ℕ` or `n : ℤ`. Further lemmas can be found in `algebra.group_power.lemmas`. The analogous results for groups with zero can be found in `algebra.group_with_zero.power`. ## Notation - `a ^ n` is used as notation for `has_pow.pow a n`; in this file `n : ℕ` or `n : ℤ`. - `n • a` is used as notation for `has_scalar.smul n a`; in this file `n : ℕ` or `n : ℤ`. ## Implementation details We adopt the convention that `0^0 = 1`. -/ universes u v w x y z u₁ u₂ variables {M : Type u} {N : Type v} {G : Type w} {H : Type x} {A : Type y} {B : Type z} {R : Type u₁} {S : Type u₂} /-! ### Commutativity First we prove some facts about `semiconj_by` and `commute`. They do not require any theory about `pow` and/or `nsmul` and will be useful later in this file. -/ section monoid variables [monoid M] [monoid N] [add_monoid A] [add_monoid B] @[simp, to_additive one_nsmul] theorem pow_one (a : M) : a^1 = a := by rw [pow_succ, pow_zero, mul_one] /-- Note that most of the lemmas about powers of two refer to it as `sq`. -/ @[to_additive two_nsmul] theorem pow_two (a : M) : a^2 = a * a := by rw [pow_succ, pow_one] alias pow_two ← sq @[to_additive nsmul_add_comm'] theorem pow_mul_comm' (a : M) (n : ℕ) : a^n * a = a * a^n := commute.pow_self a n @[to_additive add_nsmul] theorem pow_add (a : M) (m n : ℕ) : a^(m + n) = a^m * a^n := by induction n with n ih; [rw [nat.add_zero, pow_zero, mul_one], rw [pow_succ', ← mul_assoc, ← ih, ← pow_succ', nat.add_assoc]] @[simp] lemma pow_ite (P : Prop) [decidable P] (a : M) (b c : ℕ) : a ^ (if P then b else c) = if P then a ^ b else a ^ c := by split_ifs; refl @[simp] lemma ite_pow (P : Prop) [decidable P] (a b : M) (c : ℕ) : (if P then a else b) ^ c = if P then a ^ c else b ^ c := by split_ifs; refl @[simp] lemma pow_boole (P : Prop) [decidable P] (a : M) : a ^ (if P then 1 else 0) = if P then a else 1 := by simp -- the attributes are intentionally out of order. `smul_zero` proves `nsmul_zero`. @[to_additive nsmul_zero, simp] theorem one_pow (n : ℕ) : (1 : M)^n = 1 := by induction n with n ih; [exact pow_zero _, rw [pow_succ, ih, one_mul]] @[to_additive mul_nsmul'] theorem pow_mul (a : M) (m n : ℕ) : a^(m * n) = (a^m)^n := begin induction n with n ih, { rw [nat.mul_zero, pow_zero, pow_zero] }, { rw [nat.mul_succ, pow_add, pow_succ', ih] } end @[to_additive nsmul_left_comm] lemma pow_right_comm (a : M) (m n : ℕ) : (a^m)^n = (a^n)^m := by rw [←pow_mul, nat.mul_comm, pow_mul] @[to_additive mul_nsmul] theorem pow_mul' (a : M) (m n : ℕ) : a^(m * n) = (a^n)^m := by rw [nat.mul_comm, pow_mul] @[to_additive nsmul_add_sub_nsmul] theorem pow_mul_pow_sub (a : M) {m n : ℕ} (h : m ≤ n) : a ^ m * a ^ (n - m) = a ^ n := by rw [←pow_add, nat.add_comm, tsub_add_cancel_of_le h] @[to_additive sub_nsmul_nsmul_add] theorem pow_sub_mul_pow (a : M) {m n : ℕ} (h : m ≤ n) : a ^ (n - m) * a ^ m = a ^ n := by rw [←pow_add, tsub_add_cancel_of_le h] @[to_additive bit0_nsmul] theorem pow_bit0 (a : M) (n : ℕ) : a ^ bit0 n = a^n * a^n := pow_add _ _ _ @[to_additive bit1_nsmul] theorem pow_bit1 (a : M) (n : ℕ) : a ^ bit1 n = a^n * a^n * a := by rw [bit1, pow_succ', pow_bit0] @[to_additive nsmul_add_comm] theorem pow_mul_comm (a : M) (m n : ℕ) : a^m * a^n = a^n * a^m := commute.pow_pow_self a m n @[to_additive] lemma commute.mul_pow {a b : M} (h : commute a b) (n : ℕ) : (a * b) ^ n = a ^ n * b ^ n := nat.rec_on n (by simp only [pow_zero, one_mul]) $ λ n ihn, by simp only [pow_succ, ihn, ← mul_assoc, (h.pow_left n).right_comm] @[to_additive bit0_nsmul'] theorem pow_bit0' (a : M) (n : ℕ) : a ^ bit0 n = (a * a) ^ n := by rw [pow_bit0, (commute.refl a).mul_pow] @[to_additive bit1_nsmul'] theorem pow_bit1' (a : M) (n : ℕ) : a ^ bit1 n = (a * a) ^ n * a := by rw [bit1, pow_succ', pow_bit0'] end monoid /-! ### Commutative (additive) monoid -/ section comm_monoid variables [comm_monoid M] [add_comm_monoid A] @[to_additive nsmul_add] theorem mul_pow (a b : M) (n : ℕ) : (a * b)^n = a^n * b^n := (commute.all a b).mul_pow n /-- The `n`th power map on a commutative monoid for a natural `n`, considered as a morphism of monoids. -/ @[to_additive nsmul_add_monoid_hom "Multiplication by a natural `n` on a commutative additive monoid, considered as a morphism of additive monoids.", simps] def pow_monoid_hom (n : ℕ) : M →* M := { to_fun := (^ n), map_one' := one_pow _, map_mul' := λ a b, mul_pow a b n } -- the below line causes the linter to complain :-/ -- attribute [simps] pow_monoid_hom nsmul_add_monoid_hom lemma dvd_pow {x y : M} (hxy : x ∣ y) : ∀ {n : ℕ} (hn : n ≠ 0), x ∣ y^n | 0 hn := (hn rfl).elim | (n + 1) hn := by { rw pow_succ, exact hxy.mul_right _ } alias dvd_pow ← has_dvd.dvd.pow lemma dvd_pow_self (a : M) {n : ℕ} (hn : n ≠ 0) : a ∣ a^n := dvd_rfl.pow hn end comm_monoid section div_inv_monoid variable [div_inv_monoid G] open int @[simp, to_additive one_zsmul] theorem zpow_one (a : G) : a ^ (1:ℤ) = a := by { convert pow_one a using 1, exact zpow_coe_nat a 1 } theorem zpow_two (a : G) : a ^ (2 : ℤ) = a * a := by { convert pow_two a using 1, exact zpow_coe_nat a 2 } @[to_additive neg_one_zsmul] theorem zpow_neg_one (x : G) : x ^ (-1:ℤ) = x⁻¹ := (zpow_neg_succ_of_nat x 0).trans $ congr_arg has_inv.inv (pow_one x) @[to_additive] theorem zpow_neg_coe_of_pos (a : G) : ∀ {n : ℕ}, 0 < n → a ^ -(n:ℤ) = (a ^ n)⁻¹ | (n+1) _ := zpow_neg_succ_of_nat _ _ end div_inv_monoid section group variables [group G] [group H] [add_group A] [add_group B] open int section nat @[simp, to_additive neg_nsmul] theorem inv_pow (a : G) (n : ℕ) : (a⁻¹)^n = (a^n)⁻¹ := begin induction n with n ih, { rw [pow_zero, pow_zero, one_inv] }, { rw [pow_succ', pow_succ, ih, mul_inv_rev] } end @[to_additive nsmul_sub] -- rename to sub_nsmul? theorem pow_sub (a : G) {m n : ℕ} (h : n ≤ m) : a^(m - n) = a^m * (a^n)⁻¹ := have h1 : m - n + n = m, from tsub_add_cancel_of_le h, have h2 : a^(m - n) * a^n = a^m, by rw [←pow_add, h1], eq_mul_inv_of_mul_eq h2 @[to_additive nsmul_neg_comm] theorem pow_inv_comm (a : G) (m n : ℕ) : (a⁻¹)^m * a^n = a^n * (a⁻¹)^m := (commute.refl a).inv_left.pow_pow m n @[to_additive sub_nsmul_neg] theorem inv_pow_sub (a : G) {m n : ℕ} (h : n ≤ m) : a⁻¹^(m - n) = (a^m)⁻¹ * a^n := by rw [pow_sub a⁻¹ h, inv_pow, inv_pow, inv_inv] end nat @[simp, to_additive zsmul_zero] theorem one_zpow : ∀ (n : ℤ), (1 : G) ^ n = 1 | (n : ℕ) := by rw [zpow_coe_nat, one_pow] | -[1+ n] := by rw [zpow_neg_succ_of_nat, one_pow, one_inv] @[simp, to_additive neg_zsmul] theorem zpow_neg (a : G) : ∀ (n : ℤ), a ^ -n = (a ^ n)⁻¹ | (n+1:ℕ) := div_inv_monoid.zpow_neg' _ _ | 0 := by { change a ^ (0 : ℤ) = (a ^ (0 : ℤ))⁻¹, simp } | -[1+ n] := by { rw [zpow_neg_succ_of_nat, inv_inv, ← zpow_coe_nat], refl } @[to_additive neg_one_zsmul_add] lemma mul_zpow_neg_one (a b : G) : (a*b)^(-(1:ℤ)) = b^(-(1:ℤ))*a^(-(1:ℤ)) := by simp only [mul_inv_rev, zpow_one, zpow_neg] @[to_additive zsmul_neg] theorem inv_zpow (a : G) : ∀n:ℤ, a⁻¹ ^ n = (a ^ n)⁻¹ | (n : ℕ) := by rw [zpow_coe_nat, zpow_coe_nat, inv_pow] | -[1+ n] := by rw [zpow_neg_succ_of_nat, zpow_neg_succ_of_nat, inv_pow] @[to_additive add_commute.zsmul_add] theorem commute.mul_zpow {a b : G} (h : commute a b) : ∀ n : ℤ, (a * b) ^ n = a ^ n * b ^ n | (n : ℕ) := by simp [zpow_coe_nat, h.mul_pow n] | -[1+n] := by simp [h.mul_pow, (h.pow_pow n.succ n.succ).inv_inv.symm.eq] end group section comm_group variables [comm_group G] [add_comm_group A] @[to_additive zsmul_add] theorem mul_zpow (a b : G) (n : ℤ) : (a * b)^n = a^n * b^n := (commute.all a b).mul_zpow n @[to_additive zsmul_sub] theorem div_zpow (a b : G) (n : ℤ) : (a / b) ^ n = a ^ n / b ^ n := by rw [div_eq_mul_inv, div_eq_mul_inv, mul_zpow, inv_zpow] /-- The `n`th power map (`n` an integer) on a commutative group, considered as a group homomorphism. -/ @[to_additive "Multiplication by an integer `n` on a commutative additive group, considered as an additive group homomorphism.", simps] def zpow_group_hom (n : ℤ) : G →* G := { to_fun := (^ n), map_one' := one_zpow n, map_mul' := λ a b, mul_zpow a b n } end comm_group lemma zero_pow [monoid_with_zero R] : ∀ {n : ℕ}, 0 < n → (0 : R) ^ n = 0 | (n+1) _ := by rw [pow_succ, zero_mul] lemma zero_pow_eq [monoid_with_zero R] (n : ℕ) : (0 : R)^n = if n = 0 then 1 else 0 := begin split_ifs with h, { rw [h, pow_zero], }, { rw [zero_pow (nat.pos_of_ne_zero h)] }, end lemma pow_eq_zero_of_le [monoid_with_zero M] {x : M} {n m : ℕ} (hn : n ≤ m) (hx : x^n = 0) : x^m = 0 := by rw [← tsub_add_cancel_of_le hn, pow_add, hx, mul_zero] namespace ring_hom variables [semiring R] [semiring S] protected lemma map_pow (f : R →+* S) (a) : ∀ n : ℕ, f (a ^ n) = (f a) ^ n := map_pow f a end ring_hom lemma pow_dvd_pow [monoid R] (a : R) {m n : ℕ} (h : m ≤ n) : a ^ m ∣ a ^ n := ⟨a ^ (n - m), by rw [← pow_add, nat.add_comm, tsub_add_cancel_of_le h]⟩ theorem pow_dvd_pow_of_dvd [comm_monoid R] {a b : R} (h : a ∣ b) : ∀ n : ℕ, a ^ n ∣ b ^ n | 0 := by rw [pow_zero, pow_zero] | (n+1) := by { rw [pow_succ, pow_succ], exact mul_dvd_mul h (pow_dvd_pow_of_dvd n) } theorem pow_eq_zero [monoid_with_zero R] [no_zero_divisors R] {x : R} {n : ℕ} (H : x^n = 0) : x = 0 := begin induction n with n ih, { rw pow_zero at H, rw [← mul_one x, H, mul_zero] }, { rw pow_succ at H, exact or.cases_on (mul_eq_zero.1 H) id ih } end @[simp] lemma pow_eq_zero_iff [monoid_with_zero R] [no_zero_divisors R] {a : R} {n : ℕ} (hn : 0 < n) : a ^ n = 0 ↔ a = 0 := begin refine ⟨pow_eq_zero, _⟩, rintros rfl, exact zero_pow hn, end lemma pow_ne_zero_iff [monoid_with_zero R] [no_zero_divisors R] {a : R} {n : ℕ} (hn : 0 < n) : a ^ n ≠ 0 ↔ a ≠ 0 := by rwa [not_iff_not, pow_eq_zero_iff] @[field_simps] theorem pow_ne_zero [monoid_with_zero R] [no_zero_divisors R] {a : R} (n : ℕ) (h : a ≠ 0) : a ^ n ≠ 0 := mt pow_eq_zero h section semiring variables [semiring R] lemma min_pow_dvd_add {n m : ℕ} {a b c : R} (ha : c ^ n ∣ a) (hb : c ^ m ∣ b) : c ^ (min n m) ∣ a + b := begin replace ha := (pow_dvd_pow c (min_le_left n m)).trans ha, replace hb := (pow_dvd_pow c (min_le_right n m)).trans hb, exact dvd_add ha hb end end semiring section comm_semiring variables [comm_semiring R] lemma add_sq (a b : R) : (a + b) ^ 2 = a ^ 2 + 2 * a * b + b ^ 2 := by simp only [sq, add_mul_self_eq] alias add_sq ← add_pow_two end comm_semiring section ring variable [ring R] section variables (R) theorem neg_one_pow_eq_or : ∀ n : ℕ, (-1 : R)^n = 1 ∨ (-1 : R)^n = -1 | 0 := or.inl (pow_zero _) | (n+1) := (neg_one_pow_eq_or n).swap.imp (λ h, by rw [pow_succ, h, neg_one_mul, neg_neg]) (λ h, by rw [pow_succ, h, mul_one]) end @[simp] lemma neg_one_pow_mul_eq_zero_iff {n : ℕ} {r : R} : (-1)^n * r = 0 ↔ r = 0 := by rcases neg_one_pow_eq_or R n; simp [h] @[simp] lemma mul_neg_one_pow_eq_zero_iff {n : ℕ} {r : R} : r * (-1)^n = 0 ↔ r = 0 := by rcases neg_one_pow_eq_or R n; simp [h] theorem neg_pow (a : R) (n : ℕ) : (- a) ^ n = (-1) ^ n * a ^ n := (neg_one_mul a) ▸ (commute.neg_one_left a).mul_pow n @[simp] theorem neg_pow_bit0 (a : R) (n : ℕ) : (- a) ^ (bit0 n) = a ^ (bit0 n) := by rw [pow_bit0', neg_mul_neg, pow_bit0'] @[simp] theorem neg_pow_bit1 (a : R) (n : ℕ) : (- a) ^ (bit1 n) = - a ^ (bit1 n) := by simp only [bit1, pow_succ, neg_pow_bit0, neg_mul_eq_neg_mul] @[simp] lemma neg_sq (a : R) : (-a)^2 = a^2 := by simp [sq] alias neg_sq ← neg_pow_two end ring section comm_ring variables [comm_ring R] lemma sq_sub_sq (a b : R) : a ^ 2 - b ^ 2 = (a + b) * (a - b) := by rw [sq, sq, mul_self_sub_mul_self] alias sq_sub_sq ← pow_two_sub_pow_two lemma eq_or_eq_neg_of_sq_eq_sq [is_domain R] (a b : R) (h : a ^ 2 = b ^ 2) : a = b ∨ a = -b := by rwa [← add_eq_zero_iff_eq_neg, ← sub_eq_zero, or_comm, ← mul_eq_zero, ← sq_sub_sq a b, sub_eq_zero] lemma sub_sq (a b : R) : (a - b) ^ 2 = a ^ 2 - 2 * a * b + b ^ 2 := by rw [sub_eq_add_neg, add_sq, neg_sq, mul_neg_eq_neg_mul_symm, ← sub_eq_add_neg] alias sub_sq ← sub_pow_two end comm_ring lemma of_add_nsmul [add_monoid A] (x : A) (n : ℕ) : multiplicative.of_add (n • x) = (multiplicative.of_add x)^n := rfl lemma of_add_zsmul [add_group A] (x : A) (n : ℤ) : multiplicative.of_add (n • x) = (multiplicative.of_add x)^n := rfl lemma of_mul_pow [monoid A] (x : A) (n : ℕ) : additive.of_mul (x ^ n) = n • (additive.of_mul x) := rfl lemma of_mul_zpow [group G] (x : G) (n : ℤ) : additive.of_mul (x ^ n) = n • additive.of_mul x := rfl @[simp] lemma semiconj_by.zpow_right [group G] {a x y : G} (h : semiconj_by a x y) : ∀ m : ℤ, semiconj_by a (x^m) (y^m) | (n : ℕ) := by simp [zpow_coe_nat, h.pow_right n] | -[1+n] := by simp [(h.pow_right n.succ).inv_right] namespace commute variables [group G] {a b : G} @[simp] lemma zpow_right (h : commute a b) (m : ℤ) : commute a (b^m) := h.zpow_right m @[simp] lemma zpow_left (h : commute a b) (m : ℤ) : commute (a^m) b := (h.symm.zpow_right m).symm lemma zpow_zpow (h : commute a b) (m n : ℤ) : commute (a^m) (b^n) := (h.zpow_left m).zpow_right n variables (a) (m n : ℤ) @[simp] theorem self_zpow : commute a (a ^ n) := (commute.refl a).zpow_right n @[simp] theorem zpow_self : commute (a ^ n) a := (commute.refl a).zpow_left n @[simp] theorem zpow_zpow_self : commute (a ^ m) (a ^ n) := (commute.refl a).zpow_zpow m n end commute
19a71a633be16d688346be323a1ab4214b793dcc
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/tests/lean/run/ind7.lean
39d9d143663a7dbb94f711370b97b5273b459ebf
[ "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
192
lean
namespace list inductive list (A : Type*) : Type* | nil : list | cons : A → list → list check list.{1} check list.cons.{1} check list.rec.{1 1} end list check list.list.{1}
c0726a196d17ab57f2752d7faf0f243c5a310883
5d76f062116fa5bd22eda20d6fd74da58dba65bb
/src/general_lemmas/polynomial_smul_eq_C_mul.lean
406af09f026d20f0e8d4d7e22e00dfda6e795af2
[]
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
650
lean
import data.polynomial.basic import data.polynomial.monomial import data.polynomial.coeff namespace polynomial universes u variables {R : Type u} {a : R} {m n : ℕ} section semiring variables [semiring R] {p q : polynomial R} /-- A scalar multiplication is equivalent to constant polynomial multiplication for polynomials -/ lemma smul_eq_C_mul (a : R) : a • p = (polynomial.C a) * p := by simp [ext_iff] -- [mathlib PR](https://github.com/leanprover-community/mathlib/pull/7240) -- TODO this has now been merged, so it should be possible to upgrade the mathlib for this project and remove this file at some point end semiring end polynomial
4f21e1cf4c60db0be53306a446b4a649c36db384
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/test/interval_cases.lean
5c9f6074378f2b3534dcb678210fc7e347493242
[ "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
4,518
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import tactic.interval_cases example (n : ℕ) : true := begin success_if_fail { interval_cases n }, trivial end example (n : ℕ) (h : 2 ≤ n) : true := begin success_if_fail { interval_cases n }, trivial end example (n m : ℕ) (h : n ≤ m) : true := begin success_if_fail { interval_cases n }, trivial, end example (n : ℕ) (w₂ : n < 0) : false := by interval_cases n example (n : ℕ) (w₂ : n < 1) : n = 0 := by { interval_cases n, } example (n : ℕ) (w₂ : n < 2) : n = 0 ∨ n = 1 := by { interval_cases n, { left, refl }, { right, refl }, } example (n : ℕ) (w₁ : 1 ≤ n) (w₂ : n < 3) : n = 1 ∨ n = 2 := by { interval_cases n, { left, refl }, { right, refl }, } example (n : ℕ) (w₁ : 1 ≤ n) (w₂ : n < 3) : n = 1 ∨ n = 2 := by { interval_cases using w₁ w₂, { left, refl }, { right, refl }, } -- make sure we only pick up bounds on the specified variable: example (n m : ℕ) (w₁ : 1 ≤ n) (w₂ : n < 3) (w₃ : m < 2) : n = 1 ∨ n = 2 := by { interval_cases n, { left, refl }, { right, refl }, } example (n : ℕ) (w₁ : 1 < n) (w₂ : n < 4) : n = 2 ∨ n = 3 := by { interval_cases n, { left, refl }, { right, refl }, } example (n : ℕ) (w₁ : n ≥ 3) (w₂ : n < 5) : n = 3 ∨ n = 4 := by { interval_cases n, { left, refl }, { right, refl }, } example (n : ℕ) (w₀ : n ≥ 2) (w₁ : n ≥ 3) (w₂ : n < 5) : n = 3 ∨ n = 4 := by { interval_cases n, { left, refl }, { right, refl }, } example (n : ℕ) (w₁ : n > 2) (w₂ : n < 5) : n = 3 ∨ n = 4 := by { interval_cases n, { left, refl }, { right, refl }, } example (n : ℕ) (w₁ : n > 2) (w₂ : n ≤ 4) : n = 3 ∨ n = 4 := by { interval_cases n, { left, refl }, { right, refl }, } example (n : ℕ) (w₁ : 2 < n) (w₂ : 4 ≥ n) : n = 3 ∨ n = 4 := by { interval_cases n, { left, refl }, { right, refl }, } example (n : ℕ) (h1 : 4 < n) (h2 : n ≤ 6) : n < 20 := begin interval_cases n, guard_target 5 < 20, norm_num, guard_target 6 < 20, norm_num, end example (n : ℕ) (w₁ : n % 3 < 1) : n % 3 = 0 := by { interval_cases n % 3, assumption } example (n : ℕ) (h1 : 4 ≤ n) (h2 : n < 10) : n < 20 := begin interval_cases using h1 h2, all_goals {norm_num} end example (n : ℕ+) (w₂ : n < 1) : false := by interval_cases n example (n : ℕ+) (w₂ : n < 2) : n = 1 := by interval_cases n example (n : ℕ+) (h1 : 4 ≤ n) (h2 : n < 5) : n = 4 := by interval_cases n example (n : ℕ+) (w₁ : 2 < n) (w₂ : 4 ≥ n) : n = 3 ∨ n = 4 := begin interval_cases n, { guard_target' (3 : ℕ+) = 3 ∨ (3 : ℕ+) = 4, left, refl }, { guard_target' (4 : ℕ+) = 3 ∨ (4 : ℕ+) = 4, right, refl }, end example (n : ℕ+) (w₁ : 1 < n) (w₂ : n < 4) : n = 2 ∨ n = 3 := by { interval_cases n, { left, refl }, { right, refl }, } example (n : ℕ+) (w₂ : n < 3) : n = 1 ∨ n = 2 := by { interval_cases n, { left, refl }, { right, refl }, } example (n : ℕ+) (w₂ : n < 4) : n = 1 ∨ n = 2 ∨ n = 3 := by { interval_cases n, { left, refl }, { right, left, refl }, { right, right, refl }, } example (z : ℤ) (h1 : z ≥ -3) (h2 : z < 2) : z < 20 := begin interval_cases using h1 h2, all_goals {norm_num} end example (z : ℤ) (h1 : z ≥ -3) (h2 : z < 2) : z < 20 := begin interval_cases z, guard_target (-3 : ℤ) < 20, norm_num, guard_target (-2 : ℤ) < 20, norm_num, guard_target (-1 : ℤ) < 20, norm_num, guard_target (0 : ℤ) < 20, norm_num, guard_target (1 : ℤ) < 20, norm_num, end example (n : ℕ) : n % 2 = 0 ∨ n % 2 = 1 := begin set r := n % 2 with hr, have h2 : r < 2 := nat.mod_lt _ (dec_trivial), interval_cases r with hrv, { left, exact hrv }, { right, exact hrv } end /- https://leanprover.zulipchat.com/#narrow/stream/113488-general/topic/interval_cases.20bug -/ example {x : ℕ} (hx2 : x < 2) (h : false) : false := begin have : x ≤ 1, interval_cases x, exact zero_le_one, -- this solves the side-goal left by `interval_cases`, closing the `have` exact h, end example : ∀ y, y ≤ 3 → true := begin refine λ y hy, _, interval_cases y, all_goals { trivial }, end /- Sadly, this one doesn't work, reporting: `deep recursion was detected at 'expression equality test'` -/ -- example (n : ℕ) (w₁ : n > 1000000) (w₁ : n < 1000002) : n < 2000000 := -- begin -- interval_cases n, -- norm_num, -- end
06ec7680dba3e6717fcfa8564e562206e707d3d7
4a092885406df4e441e9bb9065d9405dacb94cd8
/src/for_mathlib/sheaves_of_types.lean
f82f0e2ec277327c31d5ba7336ae2f471726066c
[ "Apache-2.0" ]
permissive
semorrison/lean-perfectoid-spaces
78c1572cedbfae9c3e460d8aaf91de38616904d8
bb4311dff45791170bcb1b6a983e2591bee88a19
refs/heads/master
1,588,841,765,494
1,554,805,620,000
1,554,805,620,000
180,353,546
0
1
null
1,554,809,880,000
1,554,809,880,000
null
UTF-8
Lean
false
false
6,137
lean
-- presheaf of types basics and equivalence -- written by KB, tidied up by Mario import topology.opens topology.constructions universes u v u₁ v₁ u₂ v₂ open topological_space structure presheaf_of_typesU (X : Type u) [topological_space X] := (F : opens X → Type u) (res : ∀ {U V : opens X} (h : V ≤ U), F U → F V) (id : ∀ (U : opens X) (x : F U), res (le_refl U) x = x) (comp : ∀ {U V W : opens X} (hUV : V ≤ U) (hVW : W ≤ V) (x : F U), res hVW (res hUV x) = res (le_trans hVW hUV) x) instance (X : Type u) [topological_space X] : has_coe_to_fun (presheaf_of_typesU X) := ⟨_, presheaf_of_typesU.F⟩ namespace topological_space.opens variables {α : Type*} {β : Type*} {γ : Type*} variables [topological_space α] [topological_space β] [topological_space γ] def comap (f : α → β) (hf : continuous f) (U : opens β) : opens α := ⟨f ⁻¹' U, hf _ U.2⟩ theorem comap_id (U : opens α) : comap id continuous_id U = U := ext rfl theorem comap_comp {f : α → β} {g : β → γ} {hf : continuous f} {hg : continuous g} (U : opens γ) : comap _ (hf.comp hg) U = comap _ hf (comap _ hg U) := ext rfl theorem comap_mono {α β} [topological_space α] [topological_space β] {f : α → β} {hf : continuous f} {U V : opens β} (h : U ≤ V) : U.comap f hf ≤ V.comap f hf := λ x hx, h hx end topological_space.opens namespace presheaf_of_typesU open topological_space.opens variables {X : Type u} {Y : Type u} {Z : Type u} {W : Type u} [topological_space X] [topological_space Y] [topological_space Z] [topological_space W] (ℱ : presheaf_of_typesU X) (𝒢 : presheaf_of_typesU Y) (ℋ : presheaf_of_typesU Z) (𝒥 : presheaf_of_typesU W) @[simp] theorem id' (U : opens X) (x : ℱ.F U) (h) : ℱ.res h x = x := ℱ.id _ _ structure morphism (ℱ 𝒢 : presheaf_of_typesU X) := (ρ : ∀ U : opens X, ℱ.F U → 𝒢.F U) (nat : ∀ U V : opens X, ∀ h : V ≤ U, ∀ x : ℱ.F U, ρ V (ℱ.res h x) = 𝒢.res h (ρ U x)) def morphism.id (ℱ : presheaf_of_typesU X) : morphism ℱ ℱ := { ρ := λ U x, x, nat := λ U V h x, rfl } def morphism.comp (ℱ 𝒢 ℋ : presheaf_of_typesU X) (fℱ𝒢 : morphism ℱ 𝒢) (f𝒢ℋ : morphism 𝒢 ℋ) : morphism ℱ ℋ := { ρ := λ U x, f𝒢ℋ.ρ U (fℱ𝒢.ρ U x), nat := λ U V h x, by rw [fℱ𝒢.nat, f𝒢ℋ.nat] } def pushforward (f : X → Y) [hf : continuous f] (ℱ : presheaf_of_typesU X) : presheaf_of_typesU Y := { F := λ V, ℱ.F ⟨f ⁻¹' V, hf _ V.2⟩, res := λ _ _ hUV, ℱ.res (λ _ hx, hUV hx), id := λ _, ℱ.id _, comp := λ U V W hUV hVW, ℱ.comp _ _, } structure f_map (𝒢 : presheaf_of_typesU Y) (ℱ : presheaf_of_typesU X) := (f : X → Y) (hf : continuous f) (ρ : ∀ V : opens Y, 𝒢.F V → ℱ.F (V.comap f hf)) (nat : ∀ U V : opens Y, ∀ hUV : V ≤ U, ∀ x : 𝒢.F U, ρ V (𝒢.res hUV x) = ℱ.res (comap_mono hUV) (ρ U x)) namespace f_map variables {𝒢 ℱ} theorem ext {α β : f_map 𝒢 ℱ} (hf : α.f = β.f) (hρ : ∀ V x, α.ρ V x = ℱ.res (le_of_eq (by congr')) (β.ρ V x)) : α = β := begin cases α with αf αhf αρ αnat, cases β with βf βhf βρ βnat, cases hf, congr', funext V x, simpa using hρ V x end variables (𝒢 ℱ) def id (ℱ : presheaf_of_typesU X) : f_map ℱ ℱ := { f := λ x, x, hf := continuous_id, ρ := λ V x, ℱ.res (le_of_eq (comap_id _)) x, nat := λ U V hUV x, by rw [ℱ.comp, ℱ.comp] } def comp {ℱ : presheaf_of_typesU X} {𝒢 : presheaf_of_typesU Y} {ℋ : presheaf_of_typesU Z} (fℱ𝒢 : f_map ℱ 𝒢) (f𝒢ℋ : f_map 𝒢 ℋ) : f_map ℱ ℋ := { f := λ z, fℱ𝒢.f (f𝒢ℋ.f z), hf := continuous.comp f𝒢ℋ.hf fℱ𝒢.hf, ρ := λ U x, ℋ.res (le_of_eq (comap_comp _)) (f𝒢ℋ.ρ _ (fℱ𝒢.ρ U x)), nat := λ U V hUV x, by rw [fℱ𝒢.nat, f𝒢ℋ.nat, ℋ.comp, ℋ.comp] } lemma comp_assoc {ℱ : presheaf_of_typesU X} {𝒢 : presheaf_of_typesU Y} {ℋ : presheaf_of_typesU Z} {𝒥 : presheaf_of_typesU W} (fℱ𝒢 : f_map ℱ 𝒢) (f𝒢ℋ : f_map 𝒢 ℋ) (fℋ𝒥 : f_map ℋ 𝒥) : comp (comp fℱ𝒢 f𝒢ℋ) fℋ𝒥 = comp fℱ𝒢 (comp f𝒢ℋ fℋ𝒥) := f_map.ext rfl $ λ V x, begin simp, dsimp [comp], simp [𝒥.comp], refl end lemma id_comp (fℱ𝒢 : f_map ℱ 𝒢) : comp (f_map.id ℱ) fℱ𝒢 = fℱ𝒢 := f_map.ext rfl $ λ V x, begin simp, dsimp [comp, id], simp [𝒢.comp], rw [fℱ𝒢.nat, 𝒢.id'] end lemma comp_id (fℱ𝒢 : f_map ℱ 𝒢) : comp fℱ𝒢 (f_map.id 𝒢) = fℱ𝒢 := f_map.ext rfl $ λ V x, begin simp, dsimp [comp, id], simp [𝒢.comp] end end f_map structure presheaf_of_types_equiv (ℱ : presheaf_of_typesU X) (𝒢 : presheaf_of_typesU Y) := (to_fun : f_map ℱ 𝒢) (inv_fun : f_map 𝒢 ℱ) (left_inv : f_map.comp inv_fun to_fun = f_map.id 𝒢) (right_inv : f_map.comp to_fun inv_fun = f_map.id ℱ) def presheaf_of_types_equiv.refl (ℱ : presheaf_of_typesU X) : presheaf_of_types_equiv ℱ ℱ := { to_fun := f_map.id ℱ, inv_fun := f_map.id ℱ, left_inv := f_map.id_comp _ _ _, right_inv := f_map.id_comp _ _ _ } def presheaf_of_types_equiv.symm (ℱ : presheaf_of_typesU X) (𝒢 : presheaf_of_typesU Y) (h : presheaf_of_types_equiv ℱ 𝒢) : presheaf_of_types_equiv 𝒢 ℱ := { to_fun := h.inv_fun, inv_fun := h.to_fun, left_inv := h.right_inv, right_inv := h.left_inv } --local infix ` ** `:50 := f_map.comp def presheaf_of_types_equiv.trans (ℱ : presheaf_of_typesU X) (𝒢 : presheaf_of_typesU Y) (ℋ : presheaf_of_typesU Z) (hℱ𝒢 : presheaf_of_types_equiv ℱ 𝒢) (h𝒢ℋ : presheaf_of_types_equiv 𝒢 ℋ) : presheaf_of_types_equiv ℱ ℋ := { to_fun := f_map.comp hℱ𝒢.to_fun h𝒢ℋ.to_fun, inv_fun := f_map.comp h𝒢ℋ.inv_fun hℱ𝒢.inv_fun, left_inv := by rw [f_map.comp_assoc, ←f_map.comp_assoc hℱ𝒢.inv_fun, hℱ𝒢.left_inv, f_map.id_comp, h𝒢ℋ.left_inv], right_inv := by rw [f_map.comp_assoc, ←f_map.comp_assoc h𝒢ℋ.to_fun, h𝒢ℋ.right_inv, f_map.id_comp, hℱ𝒢.right_inv] } end presheaf_of_typesU
3ab68e0ce21b40c9bc085cdefd3f854cc290222e
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/test/mk_iff_of_inductive.lean
0b8b1b6ffc908f7348c3977c784982bce4c3f9e4
[ "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
1,021
lean
import data.list import data.list.perm import data.multiset.basic mk_iff_of_inductive_prop list.chain test.chain_iff mk_iff_of_inductive_prop false test.false_iff mk_iff_of_inductive_prop true test.true_iff mk_iff_of_inductive_prop nonempty test.non_empty_iff mk_iff_of_inductive_prop and test.and_iff mk_iff_of_inductive_prop or test.or_iff mk_iff_of_inductive_prop eq test.eq_iff mk_iff_of_inductive_prop heq test.heq_iff mk_iff_of_inductive_prop list.perm test.perm_iff mk_iff_of_inductive_prop list.pairwise test.pairwise_iff inductive test.is_true (p : Prop) : Prop | triviality : p → test.is_true mk_iff_of_inductive_prop test.is_true test.is_true_iff @[mk_iff] structure foo (m n : ℕ) : Prop := (equal : m = n) (sum_eq_two : m + n = 2) example (m n : ℕ) : foo m n ↔ m = n ∧ m + n = 2 := foo_iff m n @[mk_iff bar] structure foo2 (m n : ℕ) : Prop := (equal : m = n) (sum_eq_two : m + n = 2) example (m n : ℕ) : foo2 m n ↔ m = n ∧ m + n = 2 := bar m n
a7d920caa08c5e09fa6a1913a8fc528310364ac8
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/category_theory/equivalence.lean
f62e8a6e3ba345c70bff1dd03ed107f948ecfcf3
[ "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
24,771
lean
/- Copyright (c) 2017 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Tim Baumann, Stephen Morgan, Scott Morrison, Floris van Doorn -/ import category_theory.fully_faithful import category_theory.full_subcategory import category_theory.whiskering import category_theory.essential_image import tactic.slice /-! # Equivalence of categories An equivalence of categories `C` and `D` is a pair of functors `F : C ⥤ D` and `G : D ⥤ C` such that `η : 𝟭 C ≅ F ⋙ G` and `ε : G ⋙ F ≅ 𝟭 D`. In many situations, equivalences are a better notion of "sameness" of categories than the stricter isomorphims of categories. Recall that one way to express that two functors `F : C ⥤ D` and `G : D ⥤ C` are adjoint is using two natural transformations `η : 𝟭 C ⟶ F ⋙ G` and `ε : G ⋙ F ⟶ 𝟭 D`, called the unit and the counit, such that the compositions `F ⟶ FGF ⟶ F` and `G ⟶ GFG ⟶ G` are the identity. Unfortunately, it is not the case that the natural isomorphisms `η` and `ε` in the definition of an equivalence automatically give an adjunction. However, it is true that * if one of the two compositions is the identity, then so is the other, and * given an equivalence of categories, it is always possible to refine `η` in such a way that the identities are satisfied. For this reason, in mathlib we define an equivalence to be a "half-adjoint equivalence", which is a tuple `(F, G, η, ε)` as in the first paragraph such that the composite `F ⟶ FGF ⟶ F` is the identity. By the remark above, this already implies that the tuple is an "adjoint equivalence", i.e., that the composite `G ⟶ GFG ⟶ G` is also the identity. We also define essentially surjective functors and show that a functor is an equivalence if and only if it is full, faithful and essentially surjective. ## Main definitions * `equivalence`: bundled (half-)adjoint equivalences of categories * `is_equivalence`: type class on a functor `F` containing the data of the inverse `G` as well as the natural isomorphisms `η` and `ε`. * `ess_surj`: type class on a functor `F` containing the data of the preimages and the isomorphisms `F.obj (preimage d) ≅ d`. ## Main results * `equivalence.mk`: upgrade an equivalence to a (half-)adjoint equivalence * `equivalence_of_fully_faithfully_ess_surj`: a fully faithful essentially surjective functor is an equivalence. ## Notations We write `C ≌ D` (`\backcong`, not to be confused with `≅`/`\cong`) for a bundled equivalence. -/ namespace category_theory open category_theory.functor nat_iso category -- declare the `v`'s first; see `category_theory.category` for an explanation universes v₁ v₂ v₃ u₁ u₂ u₃ /-- We define an equivalence as a (half)-adjoint equivalence, a pair of functors with a unit and counit which are natural isomorphisms and the triangle law `Fη ≫ εF = 1`, or in other words the composite `F ⟶ FGF ⟶ F` is the identity. In `unit_inverse_comp`, we show that this is actually an adjoint equivalence, i.e., that the composite `G ⟶ GFG ⟶ G` is also the identity. The triangle equation is written as a family of equalities between morphisms, it is more complicated if we write it as an equality of natural transformations, because then we would have to insert natural transformations like `F ⟶ F1`. See https://stacks.math.columbia.edu/tag/001J -/ structure equivalence (C : Type u₁) [category.{v₁} C] (D : Type u₂) [category.{v₂} D] := mk' :: (functor : C ⥤ D) (inverse : D ⥤ C) (unit_iso : 𝟭 C ≅ functor ⋙ inverse) (counit_iso : inverse ⋙ functor ≅ 𝟭 D) (functor_unit_iso_comp' : ∀(X : C), functor.map ((unit_iso.hom : 𝟭 C ⟶ functor ⋙ inverse).app X) ≫ counit_iso.hom.app (functor.obj X) = 𝟙 (functor.obj X) . obviously) restate_axiom equivalence.functor_unit_iso_comp' infixr ` ≌ `:10 := equivalence variables {C : Type u₁} [category.{v₁} C] {D : Type u₂} [category.{v₂} D] namespace equivalence /-- The unit of an equivalence of categories. -/ abbreviation unit (e : C ≌ D) : 𝟭 C ⟶ e.functor ⋙ e.inverse := e.unit_iso.hom /-- The counit of an equivalence of categories. -/ abbreviation counit (e : C ≌ D) : e.inverse ⋙ e.functor ⟶ 𝟭 D := e.counit_iso.hom /-- The inverse of the unit of an equivalence of categories. -/ abbreviation unit_inv (e : C ≌ D) : e.functor ⋙ e.inverse ⟶ 𝟭 C := e.unit_iso.inv /-- The inverse of the counit of an equivalence of categories. -/ abbreviation counit_inv (e : C ≌ D) : 𝟭 D ⟶ e.inverse ⋙ e.functor := e.counit_iso.inv /- While these abbreviations are convenient, they also cause some trouble, preventing structure projections from unfolding. -/ @[simp] lemma equivalence_mk'_unit (functor inverse unit_iso counit_iso f) : (⟨functor, inverse, unit_iso, counit_iso, f⟩ : C ≌ D).unit = unit_iso.hom := rfl @[simp] lemma equivalence_mk'_counit (functor inverse unit_iso counit_iso f) : (⟨functor, inverse, unit_iso, counit_iso, f⟩ : C ≌ D).counit = counit_iso.hom := rfl @[simp] lemma equivalence_mk'_unit_inv (functor inverse unit_iso counit_iso f) : (⟨functor, inverse, unit_iso, counit_iso, f⟩ : C ≌ D).unit_inv = unit_iso.inv := rfl @[simp] lemma equivalence_mk'_counit_inv (functor inverse unit_iso counit_iso f) : (⟨functor, inverse, unit_iso, counit_iso, f⟩ : C ≌ D).counit_inv = counit_iso.inv := rfl @[simp] lemma functor_unit_comp (e : C ≌ D) (X : C) : e.functor.map (e.unit.app X) ≫ e.counit.app (e.functor.obj X) = 𝟙 (e.functor.obj X) := e.functor_unit_iso_comp X @[simp] lemma counit_inv_functor_comp (e : C ≌ D) (X : C) : e.counit_inv.app (e.functor.obj X) ≫ e.functor.map (e.unit_inv.app X) = 𝟙 (e.functor.obj X) := begin erw [iso.inv_eq_inv (e.functor.map_iso (e.unit_iso.app X) ≪≫ e.counit_iso.app (e.functor.obj X)) (iso.refl _)], exact e.functor_unit_comp X end lemma counit_inv_app_functor (e : C ≌ D) (X : C) : e.counit_inv.app (e.functor.obj X) = e.functor.map (e.unit.app X) := by { symmetry, erw [←iso.comp_hom_eq_id (e.counit_iso.app _), functor_unit_comp], refl } lemma counit_app_functor (e : C ≌ D) (X : C) : e.counit.app (e.functor.obj X) = e.functor.map (e.unit_inv.app X) := by { erw [←iso.hom_comp_eq_id (e.functor.map_iso (e.unit_iso.app X)), functor_unit_comp], refl } /-- The other triangle equality. The proof follows the following proof in Globular: http://globular.science/1905.001 -/ @[simp] lemma unit_inverse_comp (e : C ≌ D) (Y : D) : e.unit.app (e.inverse.obj Y) ≫ e.inverse.map (e.counit.app Y) = 𝟙 (e.inverse.obj Y) := begin rw [←id_comp (e.inverse.map _), ←map_id e.inverse, ←counit_inv_functor_comp, map_comp, ←iso.hom_inv_id_assoc (e.unit_iso.app _) (e.inverse.map (e.functor.map _)), app_hom, app_inv], slice_lhs 2 3 { erw [e.unit.naturality] }, slice_lhs 1 2 { erw [e.unit.naturality] }, slice_lhs 4 4 { rw [←iso.hom_inv_id_assoc (e.inverse.map_iso (e.counit_iso.app _)) (e.unit_inv.app _)] }, slice_lhs 3 4 { erw [←map_comp e.inverse, e.counit.naturality], erw [(e.counit_iso.app _).hom_inv_id, map_id] }, erw [id_comp], slice_lhs 2 3 { erw [←map_comp e.inverse, e.counit_iso.inv.naturality, map_comp] }, slice_lhs 3 4 { erw [e.unit_inv.naturality] }, slice_lhs 4 5 { erw [←map_comp (e.functor ⋙ e.inverse), (e.unit_iso.app _).hom_inv_id, map_id] }, erw [id_comp], slice_lhs 3 4 { erw [←e.unit_inv.naturality] }, slice_lhs 2 3 { erw [←map_comp e.inverse, ←e.counit_iso.inv.naturality, (e.counit_iso.app _).hom_inv_id, map_id] }, erw [id_comp, (e.unit_iso.app _).hom_inv_id], refl end @[simp] lemma inverse_counit_inv_comp (e : C ≌ D) (Y : D) : e.inverse.map (e.counit_inv.app Y) ≫ e.unit_inv.app (e.inverse.obj Y) = 𝟙 (e.inverse.obj Y) := begin erw [iso.inv_eq_inv (e.unit_iso.app (e.inverse.obj Y) ≪≫ e.inverse.map_iso (e.counit_iso.app Y)) (iso.refl _)], exact e.unit_inverse_comp Y end lemma unit_app_inverse (e : C ≌ D) (Y : D) : e.unit.app (e.inverse.obj Y) = e.inverse.map (e.counit_inv.app Y) := by { erw [←iso.comp_hom_eq_id (e.inverse.map_iso (e.counit_iso.app Y)), unit_inverse_comp], refl } lemma unit_inv_app_inverse (e : C ≌ D) (Y : D) : e.unit_inv.app (e.inverse.obj Y) = e.inverse.map (e.counit.app Y) := by { symmetry, erw [←iso.hom_comp_eq_id (e.unit_iso.app _), unit_inverse_comp], refl } @[simp] lemma fun_inv_map (e : C ≌ D) (X Y : D) (f : X ⟶ Y) : e.functor.map (e.inverse.map f) = e.counit.app X ≫ f ≫ e.counit_inv.app Y := (nat_iso.naturality_2 (e.counit_iso) f).symm @[simp] lemma inv_fun_map (e : C ≌ D) (X Y : C) (f : X ⟶ Y) : e.inverse.map (e.functor.map f) = e.unit_inv.app X ≫ f ≫ e.unit.app Y := (nat_iso.naturality_1 (e.unit_iso) f).symm section -- In this section we convert an arbitrary equivalence to a half-adjoint equivalence. variables {F : C ⥤ D} {G : D ⥤ C} (η : 𝟭 C ≅ F ⋙ G) (ε : G ⋙ F ≅ 𝟭 D) /-- If `η : 𝟭 C ≅ F ⋙ G` is part of a (not necessarily half-adjoint) equivalence, we can upgrade it to a refined natural isomorphism `adjointify_η η : 𝟭 C ≅ F ⋙ G` which exhibits the properties required for a half-adjoint equivalence. See `equivalence.mk`. -/ def adjointify_η : 𝟭 C ≅ F ⋙ G := calc 𝟭 C ≅ F ⋙ G : η ... ≅ F ⋙ (𝟭 D ⋙ G) : iso_whisker_left F (left_unitor G).symm ... ≅ F ⋙ ((G ⋙ F) ⋙ G) : iso_whisker_left F (iso_whisker_right ε.symm G) ... ≅ F ⋙ (G ⋙ (F ⋙ G)) : iso_whisker_left F (associator G F G) ... ≅ (F ⋙ G) ⋙ (F ⋙ G) : (associator F G (F ⋙ G)).symm ... ≅ 𝟭 C ⋙ (F ⋙ G) : iso_whisker_right η.symm (F ⋙ G) ... ≅ F ⋙ G : left_unitor (F ⋙ G) lemma adjointify_η_ε (X : C) : F.map ((adjointify_η η ε).hom.app X) ≫ ε.hom.app (F.obj X) = 𝟙 (F.obj X) := begin dsimp [adjointify_η], simp, have := ε.hom.naturality (F.map (η.inv.app X)), dsimp at this, rw [this], clear this, rw [←assoc _ _ (F.map _)], have := ε.hom.naturality (ε.inv.app $ F.obj X), dsimp at this, rw [this], clear this, have := (ε.app $ F.obj X).hom_inv_id, dsimp at this, rw [this], clear this, rw [id_comp], have := (F.map_iso $ η.app X).hom_inv_id, dsimp at this, rw [this] end end /-- Every equivalence of categories consisting of functors `F` and `G` such that `F ⋙ G` and `G ⋙ F` are naturally isomorphic to identity functors can be transformed into a half-adjoint equivalence without changing `F` or `G`. -/ protected definition mk (F : C ⥤ D) (G : D ⥤ C) (η : 𝟭 C ≅ F ⋙ G) (ε : G ⋙ F ≅ 𝟭 D) : C ≌ D := ⟨F, G, adjointify_η η ε, ε, adjointify_η_ε η ε⟩ /-- Equivalence of categories is reflexive. -/ @[refl, simps] def refl : C ≌ C := ⟨𝟭 C, 𝟭 C, iso.refl _, iso.refl _, λ X, category.id_comp _⟩ instance : inhabited (C ≌ C) := ⟨refl⟩ /-- Equivalence of categories is symmetric. -/ @[symm, simps] def symm (e : C ≌ D) : D ≌ C := ⟨e.inverse, e.functor, e.counit_iso.symm, e.unit_iso.symm, e.inverse_counit_inv_comp⟩ variables {E : Type u₃} [category.{v₃} E] /-- Equivalence of categories is transitive. -/ @[trans, simps] def trans (e : C ≌ D) (f : D ≌ E) : C ≌ E := { functor := e.functor ⋙ f.functor, inverse := f.inverse ⋙ e.inverse, unit_iso := begin refine iso.trans e.unit_iso _, exact iso_whisker_left e.functor (iso_whisker_right f.unit_iso e.inverse) , end, counit_iso := begin refine iso.trans _ f.counit_iso, exact iso_whisker_left f.inverse (iso_whisker_right e.counit_iso f.functor) end, -- We wouldn't have needed to give this proof if we'd used `equivalence.mk`, -- but we choose to avoid using that here, for the sake of good structure projection `simp` -- lemmas. functor_unit_iso_comp' := λ X, begin dsimp, rw [← f.functor.map_comp_assoc, e.functor.map_comp, ←counit_inv_app_functor, fun_inv_map, iso.inv_hom_id_app_assoc, assoc, iso.inv_hom_id_app, counit_app_functor, ← functor.map_comp], erw [comp_id, iso.hom_inv_id_app, functor.map_id], end } /-- Composing a functor with both functors of an equivalence yields a naturally isomorphic functor. -/ def fun_inv_id_assoc (e : C ≌ D) (F : C ⥤ E) : e.functor ⋙ e.inverse ⋙ F ≅ F := (functor.associator _ _ _).symm ≪≫ iso_whisker_right e.unit_iso.symm F ≪≫ F.left_unitor @[simp] lemma fun_inv_id_assoc_hom_app (e : C ≌ D) (F : C ⥤ E) (X : C) : (fun_inv_id_assoc e F).hom.app X = F.map (e.unit_inv.app X) := by { dsimp [fun_inv_id_assoc], tidy } @[simp] lemma fun_inv_id_assoc_inv_app (e : C ≌ D) (F : C ⥤ E) (X : C) : (fun_inv_id_assoc e F).inv.app X = F.map (e.unit.app X) := by { dsimp [fun_inv_id_assoc], tidy } /-- Composing a functor with both functors of an equivalence yields a naturally isomorphic functor. -/ def inv_fun_id_assoc (e : C ≌ D) (F : D ⥤ E) : e.inverse ⋙ e.functor ⋙ F ≅ F := (functor.associator _ _ _).symm ≪≫ iso_whisker_right e.counit_iso F ≪≫ F.left_unitor @[simp] lemma inv_fun_id_assoc_hom_app (e : C ≌ D) (F : D ⥤ E) (X : D) : (inv_fun_id_assoc e F).hom.app X = F.map (e.counit.app X) := by { dsimp [inv_fun_id_assoc], tidy } @[simp] lemma inv_fun_id_assoc_inv_app (e : C ≌ D) (F : D ⥤ E) (X : D) : (inv_fun_id_assoc e F).inv.app X = F.map (e.counit_inv.app X) := by { dsimp [inv_fun_id_assoc], tidy } /-- If `C` is equivalent to `D`, then `C ⥤ E` is equivalent to `D ⥤ E`. -/ @[simps functor inverse unit_iso counit_iso] def congr_left (e : C ≌ D) : (C ⥤ E) ≌ (D ⥤ E) := equivalence.mk ((whiskering_left _ _ _).obj e.inverse) ((whiskering_left _ _ _).obj e.functor) (nat_iso.of_components (λ F, (e.fun_inv_id_assoc F).symm) (by tidy)) (nat_iso.of_components (λ F, e.inv_fun_id_assoc F) (by tidy)) /-- If `C` is equivalent to `D`, then `E ⥤ C` is equivalent to `E ⥤ D`. -/ @[simps functor inverse unit_iso counit_iso] def congr_right (e : C ≌ D) : (E ⥤ C) ≌ (E ⥤ D) := equivalence.mk ((whiskering_right _ _ _).obj e.functor) ((whiskering_right _ _ _).obj e.inverse) (nat_iso.of_components (λ F, F.right_unitor.symm ≪≫ iso_whisker_left F e.unit_iso ≪≫ functor.associator _ _ _) (by tidy)) (nat_iso.of_components (λ F, functor.associator _ _ _ ≪≫ iso_whisker_left F e.counit_iso ≪≫ F.right_unitor) (by tidy)) section cancellation_lemmas variables (e : C ≌ D) /- We need special forms of `cancel_nat_iso_hom_right(_assoc)` and `cancel_nat_iso_inv_right(_assoc)` for units and counits, because neither `simp` or `rw` will apply those lemmas in this setting without providing `e.unit_iso` (or similar) as an explicit argument. We also provide the lemmas for length four compositions, since they're occasionally useful. (e.g. in proving that equivalences take monos to monos) -/ @[simp] lemma cancel_unit_right {X Y : C} (f f' : X ⟶ Y) : f ≫ e.unit.app Y = f' ≫ e.unit.app Y ↔ f = f' := by simp only [cancel_mono] @[simp] lemma cancel_unit_inv_right {X Y : C} (f f' : X ⟶ e.inverse.obj (e.functor.obj Y)) : f ≫ e.unit_inv.app Y = f' ≫ e.unit_inv.app Y ↔ f = f' := by simp only [cancel_mono] @[simp] lemma cancel_counit_right {X Y : D} (f f' : X ⟶ e.functor.obj (e.inverse.obj Y)) : f ≫ e.counit.app Y = f' ≫ e.counit.app Y ↔ f = f' := by simp only [cancel_mono] @[simp] lemma cancel_counit_inv_right {X Y : D} (f f' : X ⟶ Y) : f ≫ e.counit_inv.app Y = f' ≫ e.counit_inv.app Y ↔ f = f' := by simp only [cancel_mono] @[simp] lemma cancel_unit_right_assoc {W X X' Y : C} (f : W ⟶ X) (g : X ⟶ Y) (f' : W ⟶ X') (g' : X' ⟶ Y) : f ≫ g ≫ e.unit.app Y = f' ≫ g' ≫ e.unit.app Y ↔ f ≫ g = f' ≫ g' := by simp only [←category.assoc, cancel_mono] @[simp] lemma cancel_counit_inv_right_assoc {W X X' Y : D} (f : W ⟶ X) (g : X ⟶ Y) (f' : W ⟶ X') (g' : X' ⟶ Y) : f ≫ g ≫ e.counit_inv.app Y = f' ≫ g' ≫ e.counit_inv.app Y ↔ f ≫ g = f' ≫ g' := by simp only [←category.assoc, cancel_mono] @[simp] lemma cancel_unit_right_assoc' {W X X' Y Y' Z : C} (f : W ⟶ X) (g : X ⟶ Y) (h : Y ⟶ Z) (f' : W ⟶ X') (g' : X' ⟶ Y') (h' : Y' ⟶ Z) : f ≫ g ≫ h ≫ e.unit.app Z = f' ≫ g' ≫ h' ≫ e.unit.app Z ↔ f ≫ g ≫ h = f' ≫ g' ≫ h' := by simp only [←category.assoc, cancel_mono] @[simp] lemma cancel_counit_inv_right_assoc' {W X X' Y Y' Z : D} (f : W ⟶ X) (g : X ⟶ Y) (h : Y ⟶ Z) (f' : W ⟶ X') (g' : X' ⟶ Y') (h' : Y' ⟶ Z) : f ≫ g ≫ h ≫ e.counit_inv.app Z = f' ≫ g' ≫ h' ≫ e.counit_inv.app Z ↔ f ≫ g ≫ h = f' ≫ g' ≫ h' := by simp only [←category.assoc, cancel_mono] end cancellation_lemmas section -- There's of course a monoid structure on `C ≌ C`, -- but let's not encourage using it. -- The power structure is nevertheless useful. /-- Natural number powers of an auto-equivalence. Use `(^)` instead. -/ def pow_nat (e : C ≌ C) : ℕ → (C ≌ C) | 0 := equivalence.refl | 1 := e | (n+2) := e.trans (pow_nat (n+1)) /-- Powers of an auto-equivalence. Use `(^)` instead. -/ def pow (e : C ≌ C) : ℤ → (C ≌ C) | (int.of_nat n) := e.pow_nat n | (int.neg_succ_of_nat n) := e.symm.pow_nat (n+1) instance : has_pow (C ≌ C) ℤ := ⟨pow⟩ @[simp] lemma pow_zero (e : C ≌ C) : e^(0 : ℤ) = equivalence.refl := rfl @[simp] lemma pow_one (e : C ≌ C) : e^(1 : ℤ) = e := rfl @[simp] lemma pow_neg_one (e : C ≌ C) : e^(-1 : ℤ) = e.symm := rfl -- TODO as necessary, add the natural isomorphisms `(e^a).trans e^b ≅ e^(a+b)`. -- At this point, we haven't even defined the category of equivalences. end end equivalence /-- A functor that is part of a (half) adjoint equivalence -/ class is_equivalence (F : C ⥤ D) := mk' :: (inverse : D ⥤ C) (unit_iso : 𝟭 C ≅ F ⋙ inverse) (counit_iso : inverse ⋙ F ≅ 𝟭 D) (functor_unit_iso_comp' : ∀ (X : C), F.map ((unit_iso.hom : 𝟭 C ⟶ F ⋙ inverse).app X) ≫ counit_iso.hom.app (F.obj X) = 𝟙 (F.obj X) . obviously) restate_axiom is_equivalence.functor_unit_iso_comp' namespace is_equivalence instance of_equivalence (F : C ≌ D) : is_equivalence F.functor := { ..F } instance of_equivalence_inverse (F : C ≌ D) : is_equivalence F.inverse := is_equivalence.of_equivalence F.symm open equivalence /-- To see that a functor is an equivalence, it suffices to provide an inverse functor `G` such that `F ⋙ G` and `G ⋙ F` are naturally isomorphic to identity functors. -/ protected definition mk {F : C ⥤ D} (G : D ⥤ C) (η : 𝟭 C ≅ F ⋙ G) (ε : G ⋙ F ≅ 𝟭 D) : is_equivalence F := ⟨G, adjointify_η η ε, ε, adjointify_η_ε η ε⟩ end is_equivalence namespace functor /-- Interpret a functor that is an equivalence as an equivalence. -/ def as_equivalence (F : C ⥤ D) [is_equivalence F] : C ≌ D := ⟨F, is_equivalence.inverse F, is_equivalence.unit_iso, is_equivalence.counit_iso, is_equivalence.functor_unit_iso_comp⟩ instance is_equivalence_refl : is_equivalence (𝟭 C) := is_equivalence.of_equivalence equivalence.refl /-- The inverse functor of a functor that is an equivalence. -/ def inv (F : C ⥤ D) [is_equivalence F] : D ⥤ C := is_equivalence.inverse F instance is_equivalence_inv (F : C ⥤ D) [is_equivalence F] : is_equivalence F.inv := is_equivalence.of_equivalence F.as_equivalence.symm @[simp] lemma as_equivalence_functor (F : C ⥤ D) [is_equivalence F] : F.as_equivalence.functor = F := rfl @[simp] lemma as_equivalence_inverse (F : C ⥤ D) [is_equivalence F] : F.as_equivalence.inverse = inv F := rfl @[simp] lemma inv_inv (F : C ⥤ D) [is_equivalence F] : inv (inv F) = F := rfl variables {E : Type u₃} [category.{v₃} E] instance is_equivalence_trans (F : C ⥤ D) (G : D ⥤ E) [is_equivalence F] [is_equivalence G] : is_equivalence (F ⋙ G) := is_equivalence.of_equivalence (equivalence.trans (as_equivalence F) (as_equivalence G)) end functor namespace equivalence @[simp] lemma functor_inv (E : C ≌ D) : E.functor.inv = E.inverse := rfl @[simp] lemma inverse_inv (E : C ≌ D) : E.inverse.inv = E.functor := rfl @[simp] lemma functor_as_equivalence (E : C ≌ D) : E.functor.as_equivalence = E := by { cases E, congr, } @[simp] lemma inverse_as_equivalence (E : C ≌ D) : E.inverse.as_equivalence = E.symm := by { cases E, congr, } end equivalence namespace is_equivalence @[simp] lemma fun_inv_map (F : C ⥤ D) [is_equivalence F] (X Y : D) (f : X ⟶ Y) : F.map (F.inv.map f) = F.as_equivalence.counit.app X ≫ f ≫ F.as_equivalence.counit_inv.app Y := begin erw [nat_iso.naturality_2], refl end @[simp] lemma inv_fun_map (F : C ⥤ D) [is_equivalence F] (X Y : C) (f : X ⟶ Y) : F.inv.map (F.map f) = F.as_equivalence.unit_inv.app X ≫ f ≫ F.as_equivalence.unit.app Y := begin erw [nat_iso.naturality_1], refl end end is_equivalence namespace equivalence /-- An equivalence is essentially surjective. See https://stacks.math.columbia.edu/tag/02C3. -/ lemma ess_surj_of_equivalence (F : C ⥤ D) [is_equivalence F] : ess_surj F := ⟨λ Y, ⟨F.inv.obj Y, ⟨F.as_equivalence.counit_iso.app Y⟩⟩⟩ /-- An equivalence is faithful. See https://stacks.math.columbia.edu/tag/02C3. -/ @[priority 100] -- see Note [lower instance priority] instance faithful_of_equivalence (F : C ⥤ D) [is_equivalence F] : faithful F := { map_injective' := λ X Y f g w, begin have p := congr_arg (@category_theory.functor.map _ _ _ _ F.inv _ _) w, simpa only [cancel_epi, cancel_mono, is_equivalence.inv_fun_map] using p end }. /-- An equivalence is full. See https://stacks.math.columbia.edu/tag/02C3. -/ @[priority 100] -- see Note [lower instance priority] instance full_of_equivalence (F : C ⥤ D) [is_equivalence F] : full F := { preimage := λ X Y f, F.as_equivalence.unit.app X ≫ F.inv.map f ≫ F.as_equivalence.unit_inv.app Y, witness' := λ X Y f, F.inv.map_injective $ by simpa only [is_equivalence.inv_fun_map, assoc, iso.inv_hom_id_app_assoc, iso.inv_hom_id_app] using comp_id _ } @[simps] private noncomputable def equivalence_inverse (F : C ⥤ D) [full F] [faithful F] [ess_surj F] : D ⥤ C := { obj := λ X, F.obj_preimage X, map := λ X Y f, F.preimage ((F.obj_obj_preimage_iso X).hom ≫ f ≫ (F.obj_obj_preimage_iso Y).inv), map_id' := λ X, begin apply F.map_injective, tidy end, map_comp' := λ X Y Z f g, by apply F.map_injective; simp } /-- A functor which is full, faithful, and essentially surjective is an equivalence. See https://stacks.math.columbia.edu/tag/02C3. -/ noncomputable def equivalence_of_fully_faithfully_ess_surj (F : C ⥤ D) [full F] [faithful F] [ess_surj F] : is_equivalence F := is_equivalence.mk (equivalence_inverse F) (nat_iso.of_components (λ X, (preimage_iso $ F.obj_obj_preimage_iso $ F.obj X).symm) (λ X Y f, by { apply F.map_injective, obviously })) (nat_iso.of_components F.obj_obj_preimage_iso (by tidy)) @[simp] lemma functor_map_inj_iff (e : C ≌ D) {X Y : C} (f g : X ⟶ Y) : e.functor.map f = e.functor.map g ↔ f = g := ⟨λ h, e.functor.map_injective h, λ h, h ▸ rfl⟩ @[simp] lemma inverse_map_inj_iff (e : C ≌ D) {X Y : D} (f g : X ⟶ Y) : e.inverse.map f = e.inverse.map g ↔ f = g := functor_map_inj_iff e.symm f g instance ess_surj_induced_functor {C' : Type*} (e : C' ≃ D) : ess_surj (induced_functor e) := { mem_ess_image := λ Y, ⟨e.symm Y, by simp⟩, } noncomputable instance induced_functor_of_equiv {C' : Type*} (e : C' ≃ D) : is_equivalence (induced_functor e) := equivalence_of_fully_faithfully_ess_surj _ end equivalence section partial_order variables {α β : Type*} [partial_order α] [partial_order β] /-- A categorical equivalence between partial orders is just an order isomorphism. -/ def equivalence.to_order_iso (e : α ≌ β) : α ≃o β := { to_fun := e.functor.obj, inv_fun := e.inverse.obj, left_inv := λ a, (e.unit_iso.app a).to_eq.symm, right_inv := λ b, (e.counit_iso.app b).to_eq, map_rel_iff' := λ a a', ⟨λ h, ((equivalence.unit e).app a ≫ e.inverse.map h.hom ≫ (equivalence.unit_inv e).app a').le, λ (h : a ≤ a'), (e.functor.map h.hom).le⟩, } -- `@[simps]` on `equivalence.to_order_iso` produces lemmas that fail the `simp_nf` linter, -- so we provide them by hand: @[simp] lemma equivalence.to_order_iso_apply (e : α ≌ β) (a : α) : e.to_order_iso a = e.functor.obj a := rfl @[simp] lemma equivalence.to_order_iso_symm_apply (e : α ≌ β) (b : β) : e.to_order_iso.symm b = e.inverse.obj b := rfl end partial_order end category_theory
4b2234db25c430bc9459686c7f7d21c3e5e1ef55
94e33a31faa76775069b071adea97e86e218a8ee
/src/algebraic_topology/cech_nerve.lean
49f1e00f84407b634a2d2dcb8b1a904aa2fa79c9
[ "Apache-2.0" ]
permissive
urkud/mathlib
eab80095e1b9f1513bfb7f25b4fa82fa4fd02989
6379d39e6b5b279df9715f8011369a301b634e41
refs/heads/master
1,658,425,342,662
1,658,078,703,000
1,658,078,703,000
186,910,338
0
0
Apache-2.0
1,568,512,083,000
1,557,958,709,000
Lean
UTF-8
Lean
false
false
12,735
lean
/- Copyright (c) 2021 Adam Topaz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Adam Topaz -/ import algebraic_topology.simplicial_object import category_theory.limits.shapes.wide_pullbacks import category_theory.arrow /-! # The Čech Nerve This file provides a definition of the Čech nerve associated to an arrow, provided the base category has the correct wide pullbacks. Several variants are provided, given `f : arrow C`: 1. `f.cech_nerve` is the Čech nerve, considered as a simplicial object in `C`. 2. `f.augmented_cech_nerve` is the augmented Čech nerve, considered as an augmented simplicial object in `C`. 3. `simplicial_object.cech_nerve` and `simplicial_object.augmented_cech_nerve` are functorial versions of 1 resp. 2. -/ open category_theory open category_theory.limits noncomputable theory universes v u variables {C : Type u} [category.{v} C] namespace category_theory.arrow variables (f : arrow C) variables [∀ n : ℕ, has_wide_pullback.{0} f.right (λ i : fin (n+1), f.left) (λ i, f.hom)] /-- The Čech nerve associated to an arrow. -/ @[simps] def cech_nerve : simplicial_object C := { obj := λ n, wide_pullback.{0} f.right (λ i : fin (n.unop.len + 1), f.left) (λ i, f.hom), map := λ m n g, wide_pullback.lift (wide_pullback.base _) (λ i, wide_pullback.π (λ i, f.hom) $ g.unop.to_order_hom i) $ λ j, by simp, map_id' := λ x, by { ext ⟨⟩, { simpa }, { simp } }, map_comp' := λ x y z f g, by { ext ⟨⟩, { simpa }, { simp } } } /-- The morphism between Čech nerves associated to a morphism of arrows. -/ @[simps] def map_cech_nerve {f g : arrow C} [∀ n : ℕ, has_wide_pullback f.right (λ i : fin (n+1), f.left) (λ i, f.hom)] [∀ n : ℕ, has_wide_pullback g.right (λ i : fin (n+1), g.left) (λ i, g.hom)] (F : f ⟶ g) : f.cech_nerve ⟶ g.cech_nerve := { app := λ n, wide_pullback.lift (wide_pullback.base _ ≫ F.right) (λ i, wide_pullback.π _ i ≫ F.left) $ λ j, by simp, naturality' := λ x y f, by { ext ⟨⟩, { simp }, { simp } } } /-- The augmented Čech nerve associated to an arrow. -/ @[simps] def augmented_cech_nerve : simplicial_object.augmented C := { left := f.cech_nerve, right := f.right, hom := { app := λ i, wide_pullback.base _, naturality' := λ x y f, by { dsimp, simp } } } /-- The morphism between augmented Čech nerve associated to a morphism of arrows. -/ @[simps] def map_augmented_cech_nerve {f g : arrow C} [∀ n : ℕ, has_wide_pullback f.right (λ i : fin (n+1), f.left) (λ i, f.hom)] [∀ n : ℕ, has_wide_pullback g.right (λ i : fin (n+1), g.left) (λ i, g.hom)] (F : f ⟶ g) : f.augmented_cech_nerve ⟶ g.augmented_cech_nerve := { left := map_cech_nerve F, right := F.right, w' := by { ext, simp } } end category_theory.arrow namespace category_theory namespace simplicial_object variables [∀ (n : ℕ) (f : arrow C), has_wide_pullback f.right (λ i : fin (n+1), f.left) (λ i, f.hom)] /-- The Čech nerve construction, as a functor from `arrow C`. -/ @[simps] def cech_nerve : arrow C ⥤ simplicial_object C := { obj := λ f, f.cech_nerve, map := λ f g F, arrow.map_cech_nerve F, map_id' := λ i, by { ext, { simp }, { simp } }, map_comp' := λ x y z f g, by { ext, { simp }, { simp } } } /-- The augmented Čech nerve construction, as a functor from `arrow C`. -/ @[simps] def augmented_cech_nerve : arrow C ⥤ simplicial_object.augmented C := { obj := λ f, f.augmented_cech_nerve, map := λ f g F, arrow.map_augmented_cech_nerve F, map_id' := λ x, by { ext, { simp }, { simp }, { refl } }, map_comp' := λ x y z f g, by { ext, { simp }, { simp }, { refl } } } /-- A helper function used in defining the Čech adjunction. -/ @[simps] def equivalence_right_to_left (X : simplicial_object.augmented C) (F : arrow C) (G : X ⟶ F.augmented_cech_nerve) : augmented.to_arrow.obj X ⟶ F := { left := G.left.app _ ≫ wide_pullback.π (λ i, F.hom) 0, right := G.right, w' := begin have := G.w, apply_fun (λ e, e.app (opposite.op $ simplex_category.mk 0)) at this, simpa using this, end } /-- A helper function used in defining the Čech adjunction. -/ @[simps] def equivalence_left_to_right (X : simplicial_object.augmented C) (F : arrow C) (G : augmented.to_arrow.obj X ⟶ F) : X ⟶ F.augmented_cech_nerve := { left := { app := λ x, limits.wide_pullback.lift (X.hom.app _ ≫ G.right) (λ i, X.left.map (simplex_category.const x.unop i).op ≫ G.left) (λ i, by { dsimp, erw [category.assoc, arrow.w, augmented.to_arrow_obj_hom, nat_trans.naturality_assoc, functor.const.obj_map, category.id_comp] } ), naturality' := begin intros x y f, ext, { dsimp, simp only [wide_pullback.lift_π, category.assoc], rw [← category.assoc, ← X.left.map_comp], refl }, { dsimp, simp only [functor.const.obj_map, nat_trans.naturality_assoc, wide_pullback.lift_base, category.assoc], erw category.id_comp } end }, right := G.right, w' := by { ext, dsimp, simp } } /-- A helper function used in defining the Čech adjunction. -/ @[simps] def cech_nerve_equiv (X : simplicial_object.augmented C) (F : arrow C) : (augmented.to_arrow.obj X ⟶ F) ≃ (X ⟶ F.augmented_cech_nerve) := { to_fun := equivalence_left_to_right _ _, inv_fun := equivalence_right_to_left _ _, left_inv := begin intro A, dsimp, ext, { dsimp, erw wide_pullback.lift_π, nth_rewrite 1 ← category.id_comp A.left, congr' 1, convert X.left.map_id _, rw ← op_id, congr' 1, ext ⟨a,ha⟩, change a < 1 at ha, change 0 = a, linarith }, { refl, } end, right_inv := begin intro A, ext _ ⟨j⟩, { dsimp, simp only [arrow.cech_nerve_map, wide_pullback.lift_π, nat_trans.naturality_assoc], erw wide_pullback.lift_π, refl }, { erw wide_pullback.lift_base, have := A.w, apply_fun (λ e, e.app x) at this, rw nat_trans.comp_app at this, erw this, refl }, { refl } end } /-- The augmented Čech nerve construction is right adjoint to the `to_arrow` functor. -/ abbreviation cech_nerve_adjunction : (augmented.to_arrow : _ ⥤ arrow C) ⊣ augmented_cech_nerve := adjunction.mk_of_hom_equiv { hom_equiv := cech_nerve_equiv, hom_equiv_naturality_left_symm' := λ x y f g h, by { ext, { simp }, { simp } }, hom_equiv_naturality_right' := λ x y f g h, by { ext, { simp }, { simp }, { refl } } } end simplicial_object end category_theory namespace category_theory.arrow variables (f : arrow C) variables [∀ n : ℕ, has_wide_pushout f.left (λ i : fin (n+1), f.right) (λ i, f.hom)] /-- The Čech conerve associated to an arrow. -/ @[simps] def cech_conerve : cosimplicial_object C := { obj := λ n, wide_pushout f.left (λ i : fin (n.len + 1), f.right) (λ i, f.hom), map := λ m n g, wide_pushout.desc (wide_pushout.head _) (λ i, wide_pushout.ι (λ i, f.hom) $ g.to_order_hom i) $ λ i, by { rw [wide_pushout.arrow_ι (λ i, f.hom)] }, map_id' := λ x, by { ext ⟨⟩, { simpa }, { simp } }, map_comp' := λ x y z f g, by { ext ⟨⟩, { simpa }, { simp } } } /-- The morphism between Čech conerves associated to a morphism of arrows. -/ @[simps] def map_cech_conerve {f g : arrow C} [∀ n : ℕ, has_wide_pushout f.left (λ i : fin (n+1), f.right) (λ i, f.hom)] [∀ n : ℕ, has_wide_pushout g.left (λ i : fin (n+1), g.right) (λ i, g.hom)] (F : f ⟶ g) : f.cech_conerve ⟶ g.cech_conerve := { app := λ n, wide_pushout.desc (F.left ≫ wide_pushout.head _) (λ i, F.right ≫ wide_pushout.ι _ i) $ λ i, by { rw [← arrow.w_assoc F, wide_pushout.arrow_ι (λ i, g.hom)] }, naturality' := λ x y f, by { ext, { simp }, { simp } } } /-- The augmented Čech conerve associated to an arrow. -/ @[simps] def augmented_cech_conerve : cosimplicial_object.augmented C := { left := f.left, right := f.cech_conerve, hom := { app := λ i, wide_pushout.head _, naturality' := λ x y f, by { dsimp, simp } } } /-- The morphism between augmented Čech conerves associated to a morphism of arrows. -/ @[simps] def map_augmented_cech_conerve {f g : arrow C} [∀ n : ℕ, has_wide_pushout f.left (λ i : fin (n+1), f.right) (λ i, f.hom)] [∀ n : ℕ, has_wide_pushout g.left (λ i : fin (n+1), g.right) (λ i, g.hom)] (F : f ⟶ g) : f.augmented_cech_conerve ⟶ g.augmented_cech_conerve := { left := F.left, right := map_cech_conerve F, w' := by { ext, simp } } end category_theory.arrow namespace category_theory namespace cosimplicial_object variables [∀ (n : ℕ) (f : arrow C), has_wide_pushout f.left (λ i : fin (n+1), f.right) (λ i, f.hom)] /-- The Čech conerve construction, as a functor from `arrow C`. -/ @[simps] def cech_conerve : arrow C ⥤ cosimplicial_object C := { obj := λ f, f.cech_conerve, map := λ f g F, arrow.map_cech_conerve F, map_id' := λ i, by { ext, { dsimp, simp }, { dsimp, simp } }, map_comp' := λ f g h F G, by { ext, { simp }, { simp } } } /-- The augmented Čech conerve construction, as a functor from `arrow C`. -/ @[simps] def augmented_cech_conerve : arrow C ⥤ cosimplicial_object.augmented C := { obj := λ f, f.augmented_cech_conerve, map := λ f g F, arrow.map_augmented_cech_conerve F, map_id' := λ f, by { ext, { refl }, { dsimp, simp }, { dsimp, simp } }, map_comp' := λ f g h F G, by { ext, { refl }, { simp }, { simp } } } /-- A helper function used in defining the Čech conerve adjunction. -/ @[simps] def equivalence_left_to_right (F : arrow C) (X : cosimplicial_object.augmented C) (G : F.augmented_cech_conerve ⟶ X) : F ⟶ augmented.to_arrow.obj X := { left := G.left, right := (wide_pushout.ι (λ i, F.hom) 0 ≫ G.right.app (simplex_category.mk 0) : _), w' := begin have := G.w, apply_fun (λ e, e.app (simplex_category.mk 0)) at this, simpa only [category_theory.functor.id_map, augmented.to_arrow_obj_hom, wide_pushout.arrow_ι_assoc (λ i, F.hom)], end } /-- A helper function used in defining the Čech conerve adjunction. -/ @[simps] def equivalence_right_to_left (F : arrow C) (X : cosimplicial_object.augmented C) (G : F ⟶ augmented.to_arrow.obj X) : F.augmented_cech_conerve ⟶ X := { left := G.left, right := { app := λ x, limits.wide_pushout.desc (G.left ≫ X.hom.app _) (λ i, G.right ≫ X.right.map (simplex_category.const x i)) begin rintros j, rw ← arrow.w_assoc G, have t := X.hom.naturality (x.const j), dsimp at t ⊢, simp only [category.id_comp] at t, rw ← t, end, naturality' := begin intros x y f, ext, { dsimp, simp only [wide_pushout.ι_desc_assoc, wide_pushout.ι_desc], rw [category.assoc, ←X.right.map_comp], refl }, { dsimp, simp only [functor.const.obj_map, ←nat_trans.naturality, wide_pushout.head_desc_assoc, wide_pushout.head_desc, category.assoc], erw category.id_comp } end }, w' := by { ext, simp } } /-- A helper function used in defining the Čech conerve adjunction. -/ @[simps] def cech_conerve_equiv (F : arrow C) (X : cosimplicial_object.augmented C) : (F.augmented_cech_conerve ⟶ X) ≃ (F ⟶ augmented.to_arrow.obj X) := { to_fun := equivalence_left_to_right _ _, inv_fun := equivalence_right_to_left _ _, left_inv := begin intro A, dsimp, ext _, { refl }, ext _ ⟨⟩, -- A bug in the `ext` tactic? { dsimp, simp only [arrow.cech_conerve_map, wide_pushout.ι_desc, category.assoc, ← nat_trans.naturality, wide_pushout.ι_desc_assoc], refl }, { erw wide_pushout.head_desc, have := A.w, apply_fun (λ e, e.app x) at this, rw nat_trans.comp_app at this, erw this, refl }, end, right_inv := begin intro A, ext, { refl, }, { dsimp, erw wide_pushout.ι_desc, nth_rewrite 1 ← category.comp_id A.right, congr' 1, convert X.right.map_id _, ext ⟨a,ha⟩, change a < 1 at ha, change 0 = a, linarith }, end } /-- The augmented Čech conerve construction is left adjoint to the `to_arrow` functor. -/ abbreviation cech_conerve_adjunction : augmented_cech_conerve ⊣ (augmented.to_arrow : _ ⥤ arrow C) := adjunction.mk_of_hom_equiv { hom_equiv := cech_conerve_equiv, hom_equiv_naturality_left_symm' := λ x y f g h, by { ext, { refl }, { simp }, { simp } }, hom_equiv_naturality_right' := λ x y f g h, by { ext, { simp }, { simp } } } end cosimplicial_object end category_theory
4a476cb6663f12ca32cc4f68732f05533bdcafe3
7cef822f3b952965621309e88eadf618da0c8ae9
/src/analysis/convex.lean
4c714bd1316ed774ad7d41cd5c8aeae387ef4c85
[ "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
26,149
lean
/- Copyright (c) 2019 Alexander Bentkamp. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Alexander Bentkamp, Yury Kudriashov -/ import analysis.normed_space.basic import data.complex.basic import data.set.intervals import tactic.interactive import tactic.linarith import linear_algebra.basic import ring_theory.algebra /-! # Convex sets and functions on real vector spaces In a real normed space, we define the following objects and properties. * `segment x y` is the closed segment joining `x` and `y`. * A set `A` is `convex` if for any two points `x y ∈ A` it includes `segment x y`; * A function `f` is `convex_on` a set `D` if `D` is itself a convex set, and for any two points `x y ∈ D` the segment joining `(x, f x)` to `(y, f y)` is (non-strictly) above the graph of `f`; equivalently, `convex_on f D` means that the epigraph `{p : α × ℝ | p.1 ∈ D ∧ f p.1 ≤ p.2}` is a convex set; * Center mass of a finite set of points with prescribed weights. We also provide various equivalent versions of the definitions above, prove that some specific sets are convex, and prove Jensen's inequality. -/ open set open_locale classical local notation `I` := (Icc 0 1 : set ℝ) section vector_space variables {α : Type*} {β : Type*} {ι : Sort _} [add_comm_group α] [vector_space ℝ α] [add_comm_group β] [vector_space ℝ β] (A : set α) (B : set α) (x : α) local attribute [instance] set.pointwise_add set.smul_set /-- Convexity of sets -/ def convex (A : set α) := ∀ ⦃x y : α⦄ ⦃a b : ℝ⦄, x ∈ A → y ∈ A → 0 ≤ a → 0 ≤ b → a + b = 1 → a • x + b • y ∈ A variable {A} /-- Alternative definition of set convexity -/ lemma convex_iff: convex A ↔ ∀ {x y : α} {θ : ℝ}, x ∈ A → y ∈ A → θ ∈ I → θ • x + (1 - θ) • y ∈ A := ⟨begin assume h x y θ hx hy hθ, exact (h hx hy hθ.1 (sub_nonneg.2 hθ.2) (add_sub_cancel'_right _ _)) end, begin assume h x y a b hx hy ha hb hab, have ha' : a ≤ 1, by linarith, have hb' : b = 1 - a, by linarith, rw hb', exact h hx hy ⟨ha, ha'⟩ end⟩ /-- Alternative definition of set convexity, in terms of pointwise set operations. -/ lemma convex_iff₂: convex A ↔ ∀ {a b : ℝ}, 0 ≤ a → 0 ≤ b → a + b = 1 → a • A + b • A ⊆ A := iff.intro begin rintros hA a b ha hb hab w ⟨au, ⟨u, hu, rfl⟩, bv, ⟨v, hv, rfl⟩, rfl⟩, exact hA hu hv ha hb hab end (λ h x y a b hx hy ha hb hab, (h ha hb hab) (set.add_mem_pointwise_add ⟨_, hx, rfl⟩ ⟨_, hy, rfl⟩)) /-- Alternative definition of set convexity, in terms of pointwise set operations. -/ lemma convex_iff₃: convex A ↔ ∀ {θ : ℝ}, θ ∈ I → θ • A + (1 - θ) • A ⊆ A := convex_iff₂.trans $ iff.intro (λ h θ hθ, h hθ.1 (sub_nonneg.2 hθ.2) (add_sub_cancel'_right _ _)) (λ h a b ha hb hab, have ha' : a ≤ 1, from calc a ≤ a + b : le_add_of_nonneg_right hb ... = 1 : hab, by { rw [eq_sub_of_add_eq' hab], exact h ⟨ha, ha'⟩ }) /-- Another alternative definition of set convexity -/ lemma convex_iff_div: convex A ↔ ∀ {x y : α} {a : ℝ} {b : ℝ}, x ∈ A → y ∈ A → 0 ≤ a → 0 ≤ b → 0 < a + b → (a/(a+b)) • x + (b/(a+b)) • y ∈ A := ⟨begin assume h x y a b hx hy ha hb hab, apply h hx hy, have ha', from mul_le_mul_of_nonneg_left ha (le_of_lt (inv_pos hab)), rwa [mul_zero, ←div_eq_inv_mul] at ha', have hb', from mul_le_mul_of_nonneg_left hb (le_of_lt (inv_pos hab)), rwa [mul_zero, ←div_eq_inv_mul] at hb', rw [←add_div], exact div_self (ne_of_lt hab).symm end, begin assume h x y a b hx hy ha hb hab, have h', from h hx hy ha hb, rw [hab, div_one, div_one] at h', exact h' zero_lt_one end⟩ /-- Segments in a vector space -/ def segment (x y : α) : set α := {z : α | ∃ l ∈ I, z - x = l•(y-x)} local notation `[`x `, ` y `]` := segment x y lemma left_mem_segment (x y : α) : x ∈ [x, y] := ⟨0, ⟨le_refl _, zero_le_one⟩, by simp only [sub_self, zero_smul]⟩ lemma right_mem_segment (x y : α) : y ∈ [x, y] := ⟨1, ⟨zero_le_one, le_refl _⟩, by simp only [one_smul]⟩ lemma mem_segment_iff {x y z : α} : z ∈ [x, y] ↔ ∃ l ∈ I, z = x + l•(y - x) := exists_congr $ λ l, exists_congr $ λ hl, sub_eq_iff_eq_add' lemma segment_eq_image_Icc_zero_one {x y : α} : segment x y = (λ (t : ℝ), x + t • (y - x)) '' I := by { ext z, simp only [mem_segment_iff, mem_image_iff_bex, eq_comm] } lemma mem_segment_iff' {x y z : α} : z ∈ [x, y] ↔ ∃ l ∈ I, z = ((1:ℝ)-l)•x + l•y := mem_segment_iff.trans $ exists_congr $ λ l, exists_congr $ λ hl, eq.congr_right $ by rw [sub_smul, smul_sub, one_smul, ← add_sub_assoc, sub_add_eq_add_sub] lemma segment_symm (x y : α) : [x, y] = [y, x] := begin ext z, rw [mem_segment_iff', mem_segment_iff'], split, all_goals { rintro ⟨l, ⟨hl₀, hl₁⟩, h⟩, use (1-l), split, split; linarith, rw [h]; simp }, end lemma segment_eq_image_Icc_zero_one' {x y : α} : segment x y = (λ (t : ℝ), t • x + (1 - t) • y) '' I := by { rw [segment_symm, segment_eq_image_Icc_zero_one], simp only [smul_sub, sub_smul, one_smul], simp only [sub_eq_add_neg, add_left_comm] } lemma segment_eq_Icc {a b : ℝ} (h : a ≤ b) : [a, b] = Icc a b := begin rw [segment_eq_image_Icc_zero_one], show (((+) a) ∘ (λ t, t * (b - a))) '' Icc 0 1 = Icc a b, rw [image_comp, image_mul_right_Icc (@zero_le_one ℝ _) (sub_nonneg.2 h), image_add_left_Icc], simp end lemma segment_eq_Icc' {a b : ℝ} : [a, b] = Icc (min a b) (max a b) := by cases le_total a b; [skip, rw segment_symm]; simp [segment_eq_Icc, *] lemma segment_translate (a : α) {x b c} : x ∈ [b, c] ↔ a + x ∈ [a + b, a + c] := exists_congr $ λ l, exists_congr $ λ _, by { simp only [add_sub_add_left_eq_sub] } lemma segment_translate_preimage (a b c : α) : (λ x, a + x) ⁻¹' [a + b, a + c] = [b, c] := set.ext $ λ x, (segment_translate a).symm lemma segment_translate_image (a b c: α) : (λx, a + x) '' [b, c] = [a + b, a + c] := segment_translate_preimage a b c ▸ image_preimage_eq $ add_left_surjective a /-- Alternative defintion of set convexity using segments -/ lemma convex_segment_iff : convex A ↔ ∀ x y ∈ A, [x, y] ⊆ A := begin simp only [convex_iff, segment_eq_image_Icc_zero_one', image_subset_iff], exact ⟨λ h x y hx hy θ hθ, h hx hy hθ, λ h x y θ hx hy hθ, h x y hx hy hθ⟩ end /-! ### Examples of convex sets -/ variables {A B} lemma convex_empty : convex (∅ : set α) := by finish lemma convex_singleton (a : α) : convex ({a} : set α) := begin intros x y a b hx hy ha hb hab, rw [set.eq_of_mem_singleton hx, set.eq_of_mem_singleton hy, ←add_smul, hab], simp end lemma convex_univ : convex (set.univ : set α) := by finish lemma convex.inter (hA: convex A) (hB: convex B) : convex (A ∩ B) := λ x y a b (hx : x ∈ A ∩ B) (hy : y ∈ A ∩ B) (ha : 0 ≤ a) (hb : 0 ≤ b) (hab : a + b = 1), ⟨hA hx.left hy.left ha hb hab, hB hx.right hy.right ha hb hab⟩ lemma convex_sInter {S : set (set α)} (h : ∀ s ∈ S, convex s) : convex (⋂₀ S) := assume x y a b hx hy ha hb hab s hs, h s hs (hx s hs) (hy s hs) ha hb hab lemma convex_Inter {s: ι → set α} (h: ∀ i : ι, convex (s i)) : convex (⋂ i, s i) := (sInter_range s) ▸ convex_sInter $ forall_range_iff.2 h lemma convex.prod {A : set α} {B : set β} (hA : convex A) (hB : convex B) : convex (set.prod A B) := begin intros x y a b hx hy ha hb hab, apply mem_prod.2, exact ⟨hA (mem_prod.1 hx).1 (mem_prod.1 hy).1 ha hb hab, hB (mem_prod.1 hx).2 (mem_prod.1 hy).2 ha hb hab⟩ end lemma convex.is_linear_image (hA : convex A) {f : α → β} (hf : is_linear_map ℝ f) : convex (f '' A) := begin rintros _ _ a b ⟨x, hx, rfl⟩ ⟨y, hy, rfl⟩ ha hb hab, exact ⟨a • x + b • y, hA hx hy ha hb hab, by simp only [hf.add,hf.smul]⟩ end lemma convex.linear_image (hA : convex A) (f : α →ₗ[ℝ] β) : convex (image f A) := hA.is_linear_image f.is_linear lemma convex.is_linear_preimage {A : set β} (hA : convex A) {f : α → β} (hf : is_linear_map ℝ f) : convex (preimage f A) := begin intros x y a b hx hy ha hb hab, convert hA hx hy ha hb hab, simp [hf.add, hf.smul] end lemma convex.linear_preimage {A : set β} (hA : convex A) (f : α →ₗ[ℝ] β) : convex (preimage f A) := hA.is_linear_preimage f.is_linear lemma convex.neg (hA : convex A) : convex ((λ z, -z) '' A) := hA.is_linear_image is_linear_map.is_linear_map_neg lemma convex.neg_preimage (hA : convex A) : convex ((λ z, -z) ⁻¹' A) := hA.is_linear_preimage is_linear_map.is_linear_map_neg lemma convex.smul (c : ℝ) (hA : convex A) : convex (c • A) := hA.is_linear_image (is_linear_map.is_linear_map_smul c) lemma convex.smul_preimage (c : ℝ) (hA : convex A) : convex ((λ z, c • z) ⁻¹' A) := hA.is_linear_preimage (is_linear_map.is_linear_map_smul c) lemma convex.add (hA : convex A) (hB : convex B) : convex (A + B) := by { rw pointwise_add_eq_image, exact (hA.prod hB).is_linear_image is_linear_map.is_linear_map_add } lemma convex.sub (hA : convex A) (hB : convex B) : convex ((λx : α × α, x.1 - x.2) '' (set.prod A B)) := (hA.prod hB).is_linear_image is_linear_map.is_linear_map_sub lemma convex.translate (hA : convex A) (z : α) : convex ((λx, z + x) '' A) := begin convert (convex_singleton z).add hA, ext x, simp [set.mem_image, mem_pointwise_add, eq_comm] end lemma convex.affinity (hA : convex A) (z : α) (c : ℝ) : convex ((λx, z + c • x) '' A) := begin convert (hA.smul c).translate z using 1, erw [← image_comp] end lemma convex_real_iff {A : set ℝ} : convex A ↔ ∀ {x y}, x ∈ A → y ∈ A → Icc x y ⊆ A := begin simp only [convex_segment_iff, segment_eq_Icc'], split; intros h x y hx hy, { cases le_or_lt x y with hxy hxy, { simpa [hxy] using h x y hx hy }, { simp [hxy] } }, { apply h; cases le_total x y; simp [*] } end lemma convex_Iio (r : ℝ) : convex (Iio r) := convex_real_iff.2 $ λ x y hx hy z hz, lt_of_le_of_lt hz.2 hy lemma convex_Ioi (r : ℝ) : convex (Ioi r) := convex_real_iff.2 $ λ x y hx hy z hz, lt_of_lt_of_le hx hz.1 lemma convex_Iic (r : ℝ) : convex (Iic r) := convex_real_iff.2 $ λ x y hx hy z hz, le_trans hz.2 hy lemma convex_Ici (r : ℝ) : convex (Ici r) := convex_real_iff.2 $ λ x y hx hy z hz, le_trans hx hz.1 lemma convex_Ioo (r : ℝ) (s : ℝ) : convex (Ioo r s) := (convex_Ioi _).inter (convex_Iio _) lemma convex_Ico (r : ℝ) (s : ℝ) : convex (Ico r s) := (convex_Ici _).inter (convex_Iio _) lemma convex_Ioc (r : ℝ) (s : ℝ) : convex (Ioc r s) := (convex_Ioi _).inter (convex_Iic _) lemma convex_Icc (r : ℝ) (s : ℝ) : convex (Icc r s) := (convex_Ici _).inter (convex_Iic _) lemma convex_segment (a b : α) : convex [a, b] := begin have : (λ (t : ℝ), a + t • (b - a)) = (λz : α, a + z) ∘ (λt:ℝ, t • (b - a)) := rfl, rw [segment_eq_image_Icc_zero_one, this, image_comp], refine ((convex_Icc _ _).is_linear_image _).translate _, exact is_linear_map.is_linear_map_smul' _ end lemma convex_halfspace_lt {f : α → ℝ} (h : is_linear_map ℝ f) (r : ℝ) : convex {w | f w < r} := (convex_Iio r).is_linear_preimage h lemma convex_halfspace_le {f : α → ℝ} (h : is_linear_map ℝ f) (r : ℝ) : convex {w | f w ≤ r} := (convex_Iic r).is_linear_preimage h lemma convex_halfspace_gt {f : α → ℝ} (h : is_linear_map ℝ f) (r : ℝ) : convex {w | r < f w} := (convex_Ioi r).is_linear_preimage h lemma convex_halfspace_ge {f : α → ℝ} (h : is_linear_map ℝ f) (r : ℝ) : convex {w | r ≤ f w} := (convex_Ici r).is_linear_preimage h lemma convex_hyperplane {f : α → ℝ} (h : is_linear_map ℝ f) (r : ℝ) : convex {w | f w = r} := begin show convex (f ⁻¹' {p | p = r}), rw set_of_eq_eq_singleton, exact (convex_singleton r).is_linear_preimage h end lemma convex_halfspace_re_lt (r : ℝ) : convex {c : ℂ | c.re < r} := convex_halfspace_lt (is_linear_map.mk complex.add_re complex.smul_re) _ lemma convex_halfspace_re_le (r : ℝ) : convex {c : ℂ | c.re ≤ r} := convex_halfspace_le (is_linear_map.mk complex.add_re complex.smul_re) _ lemma convex_halfspace_re_gt (r : ℝ) : convex {c : ℂ | r < c.re } := convex_halfspace_gt (is_linear_map.mk complex.add_re complex.smul_re) _ lemma convex_halfspace_re_lge (r : ℝ) : convex {c : ℂ | r ≤ c.re} := convex_halfspace_ge (is_linear_map.mk complex.add_re complex.smul_re) _ lemma convex_halfspace_im_lt (r : ℝ) : convex {c : ℂ | c.im < r} := convex_halfspace_lt (is_linear_map.mk complex.add_im complex.smul_im) _ lemma convex_halfspace_im_le (r : ℝ) : convex {c : ℂ | c.im ≤ r} := convex_halfspace_le (is_linear_map.mk complex.add_im complex.smul_im) _ lemma convex_halfspace_im_gt (r : ℝ) : convex {c : ℂ | r < c.im } := convex_halfspace_gt (is_linear_map.mk complex.add_im complex.smul_im) _ lemma convex_halfspace_im_lge (r : ℝ) : convex {c : ℂ | r ≤ c.im} := convex_halfspace_ge (is_linear_map.mk complex.add_im complex.smul_im) _ section submodule open submodule lemma submodule.convex (K : submodule ℝ α) : convex (↑K : set α) := by { repeat {intro}, refine add_mem _ (smul_mem _ _ _) (smul_mem _ _ _); assumption } lemma subspace.convex (K : subspace ℝ α) : convex (↑K : set α) := K.convex end submodule variables (D: set α) (D': set α) (f : α → ℝ) (g : α → ℝ) /-- Convexity of functions -/ def convex_on (f : α → ℝ) : Prop := convex D ∧ ∀ {x y : α} {a b : ℝ}, x ∈ D → y ∈ D → 0 ≤ a → 0 ≤ b → a + b = 1 → f (a • x + b • y) ≤ a * f x + b * f y variables {D D' f g} lemma convex_on_iff : convex_on D f ↔ convex D ∧ ∀ {x y : α} {θ : ℝ}, x ∈ D → y ∈ D → 0 ≤ θ → θ ≤ 1 → f (θ • x + (1 - θ) • y) ≤ θ * f x + (1 - θ) * f y := and_congr iff.rfl ⟨begin intros h x y θ hx hy hθ₁ hθ₂, have hθ₂: 0 ≤ 1 - θ, by linarith, exact (h hx hy hθ₁ hθ₂ (by linarith)) end, begin intros h x y a b hx hy ha hb hab, have ha': a ≤ 1, by linarith, have hb': b = 1 - a, by linarith, rw hb', exact (h hx hy ha ha') end⟩ lemma convex_on_iff_div: convex_on D f ↔ convex D ∧ ∀ {x y : α} {a : ℝ} {b : ℝ}, x ∈ D → y ∈ D → 0 ≤ a → 0 ≤ b → 0 < a + b → f ((a/(a+b)) • x + (b/(a+b)) • y) ≤ (a/(a+b)) * f x + (b/(a+b)) * f y := and_congr iff.rfl ⟨begin intros h x y a b hx hy ha hb hab, apply h hx hy, have ha', from mul_le_mul_of_nonneg_left ha (le_of_lt (inv_pos hab)), rwa [mul_zero, ←div_eq_inv_mul] at ha', have hb', from mul_le_mul_of_nonneg_left hb (le_of_lt (inv_pos hab)), rwa [mul_zero, ←div_eq_inv_mul] at hb', rw [←add_div], exact div_self (ne_of_lt hab).symm end, begin intros h x y a b hx hy ha hb hab, simpa [hab, zero_lt_one] using h hx hy ha hb, end⟩ lemma convex_on_linorder [linear_order α] {f : α → ℝ} : convex_on D f ↔ convex D ∧ ∀ {x y : α} {a b : ℝ}, x ∈ D → y ∈ D → x < y → 0 ≤ a → 0 ≤ b → a + b = 1 → f (a • x + b • y) ≤ a * f x + b * f y := begin refine and_congr iff.rfl ⟨_, _⟩; intros h x y a b hx hy, { intro hxy, exact h hx hy }, { intros ha hb hab, wlog hxy : x<=y using [x y a b, y x b a], exact le_total _ _, apply or.elim (lt_or_eq_of_le hxy), { intros hxy, exact h hx hy hxy ha hb hab }, { intros hxy, rw [hxy, ←add_smul, hab, one_smul, ←add_mul,hab,one_mul] } } end lemma convex_on.subset (h_convex_on : convex_on D f) (h_subset : A ⊆ D) (h_convex : convex A) : convex_on A f := begin apply and.intro h_convex, intros x y a b hx hy, exact h_convex_on.2 (h_subset hx) (h_subset hy), end lemma convex_on.add (hf : convex_on D f) (hg : convex_on D g) : convex_on D (λx, f x + g x) := begin apply and.intro hf.1, intros x y a b hx hy ha hb hab, calc f (a • x + b • y) + g (a • x + b • y) ≤ (a * f x + b * f y) + (a * g x + b * g y) : add_le_add (hf.2 hx hy ha hb hab) (hg.2 hx hy ha hb hab) ... = a * f x + a * g x + b * f y + b * g y : by linarith ... = a * (f x + g x) + b * (f y + g y) : by simp [mul_add] end lemma convex_on.smul {c : ℝ} (hc : 0 ≤ c) (hf : convex_on D f) : convex_on D (λx, c * f x) := begin apply and.intro hf.1, intros x y a b hx hy ha hb hab, calc c * f (a • x + b • y) ≤ c * (a * f x + b * f y) : mul_le_mul_of_nonneg_left (hf.2 hx hy ha hb hab) hc ... = a * (c * f x) + b * (c * f y) : by rw mul_add; ac_refl end lemma convex_on.le_on_interval {x y : α} {a b : ℝ} (hf : convex_on D f) (hx : x ∈ D) (hy : y ∈ D) (ha : 0 ≤ a) (hb : 0 ≤ b) (hab : a + b = 1) : f (a • x + b • y) ≤ max (f x) (f y) := calc f (a • x + b • y) ≤ a * f x + b * f y : hf.2 hx hy ha hb hab ... ≤ a * max (f x) (f y) + b * max (f x) (f y) : add_le_add (mul_le_mul_of_nonneg_left (le_max_left _ _) ha) (mul_le_mul_of_nonneg_left (le_max_right _ _) hb) ... ≤ max (f x) (f y) : by rw [←add_mul, hab, one_mul] lemma convex_on.convex_le (hf : convex_on D f) (r : ℝ) : convex {x ∈ D | f x ≤ r} := begin intros x y a b hx hy ha hb hab, simp at *, apply and.intro, { exact hf.1 hx.1 hy.1 ha hb hab }, { apply le_trans (hf.le_on_interval hx.1 hy.1 ha hb hab), exact max_le hx.2 hy.2 } end lemma convex_on.convex_lt (hf : convex_on D f) (r : ℝ) : convex {x ∈ D | f x < r} := begin intros x y a b hx hy ha hb hab, simp at *, apply and.intro, { exact hf.1 hx.1 hy.1 ha hb hab }, { apply lt_of_le_of_lt (hf.le_on_interval hx.1 hy.1 ha hb hab), exact max_lt hx.2 hy.2 } end lemma convex_on.convex_epigraph (hf : convex_on D f) : convex {p : α × ℝ | p.1 ∈ D ∧ f p.1 ≤ p.2} := begin rintros ⟨x, r⟩ ⟨y, t⟩ a b ⟨hx, hr⟩ ⟨hy, ht⟩ ha hb hab, refine ⟨hf.1 hx hy ha hb hab, _⟩, calc f (a • x + b • y) ≤ a * f x + b * f y : hf.2 hx hy ha hb hab ... ≤ a * r + b * t : add_le_add (mul_le_mul_of_nonneg_left hr ha) (mul_le_mul_of_nonneg_left ht hb) end lemma convex_on_iff_convex_epigraph : convex_on D f ↔ convex {p : α × ℝ | p.1 ∈ D ∧ f p.1 ≤ p.2} := begin refine ⟨convex_on.convex_epigraph, λ h, ⟨_, _⟩⟩, { assume x y a b hx hy ha hb hab, exact (@h (x, f x) (y, f y) a b ⟨hx, le_refl _⟩ ⟨hy, le_refl _⟩ ha hb hab).1 }, { assume x y a b hx hy ha hb hab, exact (@h (x, f x) (y, f y) a b ⟨hx, le_refl _⟩ ⟨hy, le_refl _⟩ ha hb hab).2 } end section center_mass variables {A} (hA : convex A) {γ : Type*} (a b : γ) (s : finset γ) (w : γ → ℝ) (z : γ → α) /-- Center mass of a finite collection of points with prescribed weights. Note that we require neither `0 ≤ w i` nor `∑ w = 1`. -/ noncomputable def finset.center_mass : α := (s.sum w)⁻¹ • (s.sum (λ i, w i • z i)) open finset (hiding singleton) lemma finset.center_mass_empty : (∅ : finset γ).center_mass w z = 0 := by simp only [center_mass, sum_empty, smul_zero] lemma finset.center_mass_insert (ha : a ∉ s) (hw : s.sum w ≠ 0) : (insert a s).center_mass w z = (w a / (w a + s.sum w)) • z a + (s.sum w / (w a + s.sum w)) • s.center_mass w z := begin simp only [center_mass, sum_insert ha, smul_add, (mul_smul _ _ _).symm], congr' 2, { apply mul_comm }, { rw [div_mul_eq_mul_div, mul_inv_cancel hw, one_div_eq_inv] } end lemma finset.center_mass_singleton (hw : w a ≠ 0) : (finset.singleton a).center_mass w z = z a := by rw [center_mass, sum_singleton, sum_singleton, ← mul_smul, inv_mul_cancel hw, one_smul] lemma finset.center_mass_pair (hne : a ≠ b) : ({a, b} : finset γ).center_mass w z = (w a / (w a + w b)) • z a + (w b / (w a + w b)) • z b := by simp only [center_mass, sum_pair hne, smul_add, (mul_smul _ _ _).symm, mul_comm (w a + w b)⁻¹, div_eq_mul_inv] include hA /-- Center mass of a finite subset of a convex set belongs to the set provided that all weights are non-negative, and the total weight is positive. -/ lemma convex.center_mass_mem : (∀ i ∈ s, 0 ≤ w i) → (0 < s.sum w) → (∀ i ∈ s, z i ∈ A) → s.center_mass w z ∈ A := begin refine finset.induction (by simp [lt_irrefl]) (λ a s ha hs h₀ hpos hmem, _) s, have za : z a ∈ A, from hmem _ (mem_insert_self _ _), have hs₀ : ∀ i ∈ s, 0 ≤ w i, from λ i hi, h₀ i $ mem_insert_of_mem hi, rw [sum_insert ha] at hpos, by_cases hsum_s : s.sum w = 0, { have ws : ∀ i ∈ s, w i = 0, from (sum_eq_zero_iff_of_nonneg hs₀).1 hsum_s, have wz : s.sum (λ i, w i • z i) = 0, from sum_eq_zero (λ i hi, by simp [ws i hi]), simp only [center_mass, sum_insert ha, wz, hsum_s, add_zero], simp only [hsum_s, add_zero] at hpos, rw [← mul_smul, inv_mul_cancel (ne_of_gt hpos), one_smul], exact za }, { rw [finset.center_mass_insert _ _ _ _ ha hsum_s], refine convex_iff_div.1 hA za (hs hs₀ _ _) _ (sum_nonneg hs₀) hpos, { exact lt_of_le_of_ne (sum_nonneg hs₀) (ne.symm hsum_s) }, { intros i hi, exact hmem i (mem_insert_of_mem hi) }, { exact h₀ _ (mem_insert_self _ _) } } end lemma convex.sum_mem (h₀ : ∀ i ∈ s, 0 ≤ w i) (h₁ : s.sum w = 1) (hz : ∀ i ∈ s, z i ∈ A) : s.sum (λ i, w i • z i) ∈ A := by simpa only [h₁, center_mass, inv_one, one_smul] using hA.center_mass_mem s w z h₀ (h₁.symm ▸ zero_lt_one) hz omit hA lemma convex_iff_sum_mem : convex A ↔ (∀ (s : finset α) (as : α → ℝ), (∀ i ∈ s, 0 ≤ as i) → s.sum as = 1 → (∀ x ∈ s, x ∈ A) → s.sum (λx, as x • x) ∈ A ) := begin refine ⟨λ hA s as h_sum has hs, hA.sum_mem s _ id h_sum has hs, _⟩, intros h x y a b hx hy ha hb hab, by_cases h_cases: x = y, { rw [h_cases, ←add_smul, hab, one_smul], exact hy }, { convert h {x, y} (λ z, if z = y then b else a) _ _ _, { simp only [sum_pair h_cases, if_neg h_cases, if_pos rfl] }, { simp_intros i hi, cases hi; subst i; simp [ha, hb, if_neg h_cases] }, { simp only [sum_pair h_cases, if_neg h_cases, if_pos rfl, hab] }, { simp_intros i hi, cases hi; subst i; simp [hx, hy, if_neg h_cases] } } end /-- Jensen's inequality, `finset.center_mass` version. -/ lemma convex_on.map_center_mass_le (hf : convex_on D f) (h₀ : ∀ i ∈ s, 0 ≤ w i) (hpos : 0 < s.sum w) (hmem : ∀ i ∈ s, z i ∈ D) : f (s.center_mass w z) ≤ s.center_mass w (f ∘ z) := begin have hmem' : ∀ i ∈ s, (z i, (f ∘ z) i) ∈ {p : α × ℝ | p.1 ∈ D ∧ f p.1 ≤ p.2}, from λ i hi, ⟨hmem i hi, le_refl _⟩, convert (hf.convex_epigraph.center_mass_mem s w (λ i, (z i, (f ∘ z) i)) h₀ hpos hmem').2; simp only [center_mass, function.comp, prod.smul_fst, prod.fst_sum, prod.smul_snd, prod.snd_sum] end /-- Jensen's inequality, `finset.sum` version. -/ lemma convex_on.map_sum_le (hf : convex_on D f) (h₀ : ∀ i ∈ s, 0 ≤ w i) (h₁ : s.sum w = 1) (hmem : ∀ i ∈ s, z i ∈ D) : f (s.sum (λ i, w i • z i)) ≤ s.sum (λ i, w i * (f (z i))) := by simpa only [center_mass, h₁, inv_one, one_smul] using hf.map_center_mass_le s w z h₀ (h₁.symm ▸ zero_lt_one) hmem end center_mass end vector_space section topological_vector_space variables {α : Type*} [add_comm_group α] [vector_space ℝ α] [topological_space α] [topological_add_group α] [topological_vector_space ℝ α] local attribute [instance] set.pointwise_add set.smul_set open set /-- In a topological vector space, the interior of a convex set is convex. -/ lemma convex.interior {A : set α} (hA : convex A) : convex (interior A) := convex_iff₂.mpr $ λ a b ha hb hab, have h : is_open (a • interior A + b • interior A), from or.elim (classical.em (a = 0)) (λ heq, have hne : b ≠ 0, from by { rw [heq, zero_add] at hab, rw hab, exact one_ne_zero }, is_open_pointwise_add_left ((is_open_map_smul_of_ne_zero hne _) is_open_interior)) (λ hne, is_open_pointwise_add_right ((is_open_map_smul_of_ne_zero hne _) is_open_interior)), (subset_interior_iff_subset_of_open h).mpr $ subset.trans (by { apply pointwise_add_subset_add; exact image_subset _ interior_subset }) (convex_iff₂.mp hA ha hb hab) /-- In a topological vector space, the closure of a convex set is convex. -/ lemma convex.closure {A : set α} (hA : convex A) : convex (closure A) := λ x y a b hx hy ha hb hab, let f : α → α → α := λ x' y', a • x' + b • y' in have hf : continuous ((λ p : α × α, p.fst + p.snd) ∘ (λ p : α × α, (a • p.fst, b • p.snd))), from continuous.comp continuous_add (continuous.prod_mk (continuous_const.smul continuous_fst) (continuous_const.smul continuous_snd)), show f x y ∈ closure A, from mem_closure_of_continuous2 hf hx hy (λ x' hx' y' hy', subset_closure (hA hx' hy' ha hb hab)) end topological_vector_space section normed_space variables {α : Type*} [normed_group α] [normed_space ℝ α] lemma convex_on_dist (z : α) (D : set α) (hD : convex D) : convex_on D (λz', dist z' z) := begin apply and.intro hD, intros x y a b hx hy ha hb hab, calc dist (a • x + b • y) z = ∥ (a • x + b • y) - (a + b) • z ∥ : by rw [hab, one_smul, normed_group.dist_eq] ... = ∥a • (x - z) + b • (y - z)∥ : by rw [add_smul, smul_sub, smul_sub]; simp ... ≤ ∥a • (x - z)∥ + ∥b • (y - z)∥ : norm_add_le (a • (x - z)) (b • (y - z)) ... = a * dist x z + b * dist y z : by simp [norm_smul, normed_group.dist_eq, real.norm_eq_abs, abs_of_nonneg ha, abs_of_nonneg hb] end lemma convex_ball (a : α) (r : ℝ) : convex (metric.ball a r) := by simpa only [metric.ball, sep_univ] using (convex_on_dist a _ convex_univ).convex_lt r lemma convex_closed_ball (a : α) (r : ℝ) : convex (metric.closed_ball a r) := by simpa only [metric.closed_ball, sep_univ] using (convex_on_dist a _ convex_univ).convex_le r end normed_space
a4fb61288f35fe8d790603d5964e36224a31bb32
1abd1ed12aa68b375cdef28959f39531c6e95b84
/src/algebra/lie/matrix.lean
6dbeec85119c4fb86889e181c6032bdf30246371
[ "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
3,361
lean
/- Copyright (c) 2021 Oliver Nash. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Oliver Nash -/ import algebra.lie.of_associative import linear_algebra.matrix.reindex import linear_algebra.matrix.to_linear_equiv /-! # Lie algebras of matrices An important class of Lie algebras are those arising from the associative algebra structure on square matrices over a commutative ring. This file provides some very basic definitions whose primary value stems from their utility when constructing the classical Lie algebras using matrices. ## Main definitions * `lie_equiv_matrix'` * `matrix.lie_conj` * `matrix.reindex_lie_equiv` ## Tags lie algebra, matrix -/ universes u v w w₁ w₂ section matrices open_locale matrix variables {R : Type u} [comm_ring R] variables {n : Type w} [decidable_eq n] [fintype n] /-- The natural equivalence between linear endomorphisms of finite free modules and square matrices is compatible with the Lie algebra structures. -/ def lie_equiv_matrix' : module.End R (n → R) ≃ₗ⁅R⁆ matrix n n R := { map_lie' := λ T S, begin let f := @linear_map.to_matrix' R _ n n _ _, change f (T.comp S - S.comp T) = (f T) * (f S) - (f S) * (f T), have h : ∀ (T S : module.End R _), f (T.comp S) = (f T) ⬝ (f S) := linear_map.to_matrix'_comp, rw [linear_equiv.map_sub, h, h, matrix.mul_eq_mul, matrix.mul_eq_mul], end, ..linear_map.to_matrix' } @[simp] lemma lie_equiv_matrix'_apply (f : module.End R (n → R)) : lie_equiv_matrix' f = f.to_matrix' := rfl @[simp] lemma lie_equiv_matrix'_symm_apply (A : matrix n n R) : (@lie_equiv_matrix' R _ n _ _).symm A = A.to_lin' := rfl /-- An invertible matrix induces a Lie algebra equivalence from the space of matrices to itself. -/ noncomputable def matrix.lie_conj (P : matrix n n R) (h : is_unit P) : matrix n n R ≃ₗ⁅R⁆ matrix n n R := ((@lie_equiv_matrix' R _ n _ _).symm.trans (P.to_linear_equiv' h).lie_conj).trans lie_equiv_matrix' @[simp] lemma matrix.lie_conj_apply (P A : matrix n n R) (h : is_unit P) : P.lie_conj h A = P ⬝ A ⬝ P⁻¹ := by simp [linear_equiv.conj_apply, matrix.lie_conj, linear_map.to_matrix'_comp, linear_map.to_matrix'_to_lin'] @[simp] lemma matrix.lie_conj_symm_apply (P A : matrix n n R) (h : is_unit P) : (P.lie_conj h).symm A = P⁻¹ ⬝ A ⬝ P := by simp [linear_equiv.symm_conj_apply, matrix.lie_conj, linear_map.to_matrix'_comp, linear_map.to_matrix'_to_lin'] variables {m : Type w₁} [decidable_eq m] [fintype m] (e : n ≃ m) /-- For square matrices, the natural map that reindexes a matrix's rows and columns with equivalent types, `matrix.reindex`, is an equivalence of Lie algebras. -/ def matrix.reindex_lie_equiv : matrix n n R ≃ₗ⁅R⁆ matrix m m R := { to_fun := matrix.reindex e e, map_lie' := λ M N, by simp only [lie_ring.of_associative_ring_bracket, matrix.reindex_apply, ←matrix.minor_mul_equiv _ _ _ _, matrix.mul_eq_mul, matrix.minor_sub, pi.sub_apply], ..(matrix.reindex_linear_equiv R R e e) } @[simp] lemma matrix.reindex_lie_equiv_apply (M : matrix n n R) : matrix.reindex_lie_equiv e M = matrix.reindex e e M := rfl @[simp] lemma matrix.reindex_lie_equiv_symm : (matrix.reindex_lie_equiv e : _ ≃ₗ⁅R⁆ _).symm = matrix.reindex_lie_equiv e.symm := rfl end matrices
b66bc504794dbc56f2848e0ad44299c6b89a1228
3dc4623269159d02a444fe898d33e8c7e7e9461b
/.github/workflows/project_1_a_decrire/lean-scheme-submission/src/sheaves/presheaf_of_rings_maps.lean
2a3160653a3cee3339d8be5052d96124eb524f24
[]
no_license
Or7ando/lean
cc003e6c41048eae7c34aa6bada51c9e9add9e66
d41169cf4e416a0d42092fb6bdc14131cee9dd15
refs/heads/master
1,650,600,589,722
1,587,262,906,000
1,587,262,906,000
255,387,160
0
0
null
null
null
null
UTF-8
Lean
false
false
4,030
lean
/- Continuous maps and presheaves of rings. https://stacks.math.columbia.edu/tag/008C -/ import to_mathlib.opens import sheaves.presheaf_of_rings import sheaves.stalk_of_rings import sheaves.presheaf_maps universes u v w open topological_space variables {α : Type u} [topological_space α] variables {β : Type v} [topological_space β] namespace presheaf_of_rings -- f induces a functor PSh(α) ⟶ PSh(β). section pushforward variables {f : α → β} (Hf : continuous f) def pushforward (F : presheaf_of_rings α) : presheaf_of_rings β := { Fring := λ U, F.Fring _, res_is_ring_hom := λ U V HVU, F.res_is_ring_hom _ _ _, ..presheaf.pushforward Hf F.to_presheaf } def pushforward.morphism (F G : presheaf_of_rings α) (φ : F ⟶ G) : pushforward Hf F ⟶ pushforward Hf G := { ring_homs := λ U, φ.ring_homs _, ..presheaf.pushforward.morphism Hf F.to_presheaf G.to_presheaf φ.to_morphism } end pushforward -- f induces a functor PSh(β) ⟶ PSh(α). Simplified to the case when f is 'nice'. section pullback variable (α) structure open_immersion_pullback (F : presheaf_of_rings β) := (f : α → β) -- Open immersion. TODO: Use open embedding. (Hf₁ : continuous f) (Hf₂ : ∀ (U : opens α), is_open (f '' U)) (Hf₃ : function.injective f) (range : opens β := ⟨f '' set.univ, Hf₂ opens.univ⟩) (carrier : presheaf_of_rings α := { Fring := λ U, F.Fring _, res_is_ring_hom := λ U V HVU, F.res_is_ring_hom _ _ _, ..presheaf.pullback Hf₂ F.to_presheaf }) def pullback_id (F : presheaf_of_rings β) : open_immersion_pullback β F := begin exact { f := (id : β → β), Hf₁ := continuous_id, Hf₂ := λ U, by rw set.image_id; by exact U.2, Hf₃ := function.injective_id }, exact F, end lemma pullback_id.iso (F : presheaf_of_rings β) : (pullback_id F).carrier ≅ F := nonempty.intro { mor := { map := begin intros U, have HUU : U ⊆ opens.map (pullback_id F).Hf₂ U, intros x Hx, dsimp [opens.map], erw set.image_id, exact Hx, exact F.res (opens.map (pullback_id F).Hf₂ U) U HUU, end, commutes := begin intros U V HVU, dsimp [pullback_id], rw ←presheaf.Hcomp, rw ←presheaf.Hcomp, end, ring_homs := by apply_instance, }, inv := { map := begin intros U, have HUU : opens.map (pullback_id F).Hf₂ U ⊆ U, intros x Hx, dsimp [opens.map] at Hx, erw set.image_id at Hx, exact Hx, exact F.res U (opens.map (pullback_id F).Hf₂ U) HUU, end, commutes := begin intros U V HVU, dsimp [pullback_id], rw ←presheaf.Hcomp, rw ←presheaf.Hcomp, end, ring_homs := by apply_instance, }, mor_inv_id := begin simp [presheaf.comp], congr, funext U, rw ←presheaf.Hcomp, erw presheaf.Hid, end, inv_mor_id := begin simp [presheaf.comp], congr, funext U, rw ←presheaf.Hcomp, erw presheaf.Hid, end, } end pullback -- f induces a `map` from a presheaf of rings on β to a presheaf of rings on α. variable {α} variables {f : α → β} (Hf : continuous f) def fmap (F : presheaf_of_rings α) (G : presheaf_of_rings β) := presheaf.fmap Hf F.to_presheaf G.to_presheaf namespace fmap variables {γ : Type w} [topological_space γ] variables {g : β → γ} {Hg : continuous g} variable {Hf} def comp {F : presheaf_of_rings α} {G : presheaf_of_rings β} {H : presheaf_of_rings γ} (f_ : fmap Hf F G) (g_ : fmap Hg G H) : fmap (continuous.comp Hg Hf) F H := presheaf.fmap.comp f_ g_ def induced (F : presheaf_of_rings α) (G : presheaf_of_rings β) (f' : fmap Hf F G) (x : α) : stalk_of_rings G (f x) → stalk_of_rings F x := presheaf.fmap.induced F.to_presheaf G.to_presheaf f' x end fmap end presheaf_of_rings
dfcc96967fdf8af6f143b80ab7094ad11ef56d9c
6dc0c8ce7a76229dd81e73ed4474f15f88a9e294
/tests/lean/run/newfrontend2.lean
984749b2adc943ec9c285ec1c94afbda12383ce0
[ "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
459
lean
def foo {α} (a : Option α) (b : α) : α := match a with | some a => a | none => b structure S := (x : Nat) #check if 0 == 1 then true else false def f (x : Nat) : Nat := if x < 5 then x+1 else x-1 def x := 1 #check foo x x #check match 1 with | x => x + 1 #check match 1 : Int with | x => x + 1 #check match 1 with | x => x + 1 #check match 1 : Int with | x => x + 1 def g (x : Nat × Nat) (y : Nat) := x.1 + x.2 + y #check (g ⟨·, 1⟩ ·)
cb096e2ae1d6ca7070bfb081476dcbdd79cf9b0f
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/pfunctor/univariate/basic.lean
f969ae2c868ff249150ead0f3c36e0dee333422c
[ "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
6,404
lean
/- Copyright (c) 2018 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad -/ import data.W.basic /-! # Polynomial functors > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines polynomial functors and the W-type construction as a polynomial functor. (For the M-type construction, see pfunctor/M.lean.) -/ universe u /-- A polynomial functor `P` is given by a type `A` and a family `B` of types over `A`. `P` maps any type `α` to a new type `P.obj α`, which is defined as the sigma type `Σ x, P.B x → α`. An element of `P.obj α` is a pair `⟨a, f⟩`, where `a` is an element of a type `A` and `f : B a → α`. Think of `a` as the shape of the object and `f` as an index to the relevant elements of `α`. -/ structure pfunctor := (A : Type u) (B : A → Type u) namespace pfunctor instance : inhabited pfunctor := ⟨⟨default, default⟩⟩ variables (P : pfunctor) {α β : Type u} /-- Applying `P` to an object of `Type` -/ def obj (α : Type*) := Σ x : P.A, P.B x → α /-- Applying `P` to a morphism of `Type` -/ def map {α β : Type*} (f : α → β) : P.obj α → P.obj β := λ ⟨a, g⟩, ⟨a, f ∘ g⟩ instance obj.inhabited [inhabited P.A] [inhabited α] : inhabited (P.obj α) := ⟨⟨default, default⟩⟩ instance : functor P.obj := {map := @map P} protected theorem map_eq {α β : Type*} (f : α → β) (a : P.A) (g : P.B a → α) : @functor.map P.obj _ _ _ f ⟨a, g⟩ = ⟨a, f ∘ g⟩ := rfl protected theorem id_map {α : Type*} : ∀ x : P.obj α, id <$> x = id x := λ ⟨a, b⟩, rfl protected theorem comp_map {α β γ : Type*} (f : α → β) (g : β → γ) : ∀ x : P.obj α, (g ∘ f) <$> x = g <$> (f <$> x) := λ ⟨a, b⟩, rfl instance : is_lawful_functor P.obj := {id_map := @pfunctor.id_map P, comp_map := @pfunctor.comp_map P} /-- re-export existing definition of W-types and adapt it to a packaged definition of polynomial functor -/ def W := _root_.W_type P.B /- inhabitants of W types is awkward to encode as an instance assumption because there needs to be a value `a : P.A` such that `P.B a` is empty to yield a finite tree -/ attribute [nolint has_nonempty_instance] W variables {P} /-- root element of a W tree -/ def W.head : W P → P.A | ⟨a, f⟩ := a /-- children of the root of a W tree -/ def W.children : Π x : W P, P.B (W.head x) → W P | ⟨a, f⟩ := f /-- destructor for W-types -/ def W.dest : W P → P.obj (W P) | ⟨a, f⟩ := ⟨a, f⟩ /-- constructor for W-types -/ def W.mk : P.obj (W P) → W P | ⟨a, f⟩ := ⟨a, f⟩ @[simp] theorem W.dest_mk (p : P.obj (W P)) : W.dest (W.mk p) = p := by cases p; reflexivity @[simp] theorem W.mk_dest (p : W P) : W.mk (W.dest p) = p := by cases p; reflexivity variables (P) /-- `Idx` identifies a location inside the application of a pfunctor. For `F : pfunctor`, `x : F.obj α` and `i : F.Idx`, `i` can designate one part of `x` or is invalid, if `i.1 ≠ x.1` -/ def Idx := Σ x : P.A, P.B x instance Idx.inhabited [inhabited P.A] [inhabited (P.B default)] : inhabited P.Idx := ⟨⟨default, default⟩⟩ variables {P} /-- `x.iget i` takes the component of `x` designated by `i` if any is or returns a default value -/ def obj.iget [decidable_eq P.A] {α} [inhabited α] (x : P.obj α) (i : P.Idx) : α := if h : i.1 = x.1 then x.2 (cast (congr_arg _ h) i.2) else default @[simp] lemma fst_map {α β : Type u} (x : P.obj α) (f : α → β) : (f <$> x).1 = x.1 := by { cases x; refl } @[simp] lemma iget_map [decidable_eq P.A] {α β : Type u} [inhabited α] [inhabited β] (x : P.obj α) (f : α → β) (i : P.Idx) (h : i.1 = x.1) : (f <$> x).iget i = f (x.iget i) := by { simp only [obj.iget, fst_map, *, dif_pos, eq_self_iff_true], cases x, refl } end pfunctor /- Composition of polynomial functors. -/ namespace pfunctor /-- functor composition for polynomial functors -/ def comp (P₂ P₁ : pfunctor.{u}) : pfunctor.{u} := ⟨ Σ a₂ : P₂.1, P₂.2 a₂ → P₁.1, λ a₂a₁, Σ u : P₂.2 a₂a₁.1, P₁.2 (a₂a₁.2 u) ⟩ /-- constructor for composition -/ def comp.mk (P₂ P₁ : pfunctor.{u}) {α : Type} (x : P₂.obj (P₁.obj α)) : (comp P₂ P₁).obj α := ⟨ ⟨ x.1, sigma.fst ∘ x.2 ⟩, λ a₂a₁, (x.2 a₂a₁.1).2 a₂a₁.2 ⟩ /-- destructor for composition -/ def comp.get (P₂ P₁ : pfunctor.{u}) {α : Type} (x : (comp P₂ P₁).obj α) : P₂.obj (P₁.obj α) := ⟨ x.1.1, λ a₂, ⟨x.1.2 a₂, λ a₁, x.2 ⟨a₂,a₁⟩ ⟩ ⟩ end pfunctor /- Lifting predicates and relations. -/ namespace pfunctor variables {P : pfunctor.{u}} open functor theorem liftp_iff {α : Type u} (p : α → Prop) (x : P.obj α) : liftp p x ↔ ∃ a f, x = ⟨a, f⟩ ∧ ∀ i, p (f i) := begin split, { rintros ⟨y, hy⟩, cases h : y with a f, refine ⟨a, λ i, (f i).val, _, λ i, (f i).property⟩, rw [←hy, h, pfunctor.map_eq] }, rintros ⟨a, f, xeq, pf⟩, use ⟨a, λ i, ⟨f i, pf i⟩⟩, rw [xeq], reflexivity end theorem liftp_iff' {α : Type u} (p : α → Prop) (a : P.A) (f : P.B a → α) : @liftp.{u} P.obj _ α p ⟨a,f⟩ ↔ ∀ i, p (f i) := begin simp only [liftp_iff, sigma.mk.inj_iff]; split; intro, { casesm* [Exists _, _ ∧ _], subst_vars, assumption }, repeat { constructor <|> assumption } end theorem liftr_iff {α : Type u} (r : α → α → Prop) (x y : P.obj α) : liftr r x y ↔ ∃ a f₀ f₁, x = ⟨a, f₀⟩ ∧ y = ⟨a, f₁⟩ ∧ ∀ i, r (f₀ i) (f₁ i) := begin split, { rintros ⟨u, xeq, yeq⟩, cases h : u with a f, use [a, λ i, (f i).val.fst, λ i, (f i).val.snd], split, { rw [←xeq, h], refl }, split, { rw [←yeq, h], refl }, intro i, exact (f i).property }, rintros ⟨a, f₀, f₁, xeq, yeq, h⟩, use ⟨a, λ i, ⟨(f₀ i, f₁ i), h i⟩⟩, split, { rw [xeq], refl }, rw [yeq], refl end open set theorem supp_eq {α : Type u} (a : P.A) (f : P.B a → α) : @supp.{u} P.obj _ α (⟨a,f⟩ : P.obj α) = f '' univ := begin ext, simp only [supp, image_univ, mem_range, mem_set_of_eq], split; intro h, { apply @h (λ x, ∃ (y : P.B a), f y = x), rw liftp_iff', intro, refine ⟨_,rfl⟩ }, { simp only [liftp_iff'], cases h, subst x, tauto } end end pfunctor
207960d1bde250de66e6c90fc50b85e58194669d
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/number_theory/bernoulli.lean
a88c49a72d98b1a0a6f8f99a05d0caaa4cf65b0f
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
16,860
lean
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Kevin Buzzard -/ import algebra.big_operators.nat_antidiagonal import algebra.geom_sum import data.fintype.big_operators import ring_theory.power_series.well_known import tactic.field_simp /-! # Bernoulli numbers The Bernoulli numbers are a sequence of rational numbers that frequently show up in number theory. ## Mathematical overview The Bernoulli numbers $(B_0, B_1, B_2, \ldots)=(1, -1/2, 1/6, 0, -1/30, \ldots)$ are a sequence of rational numbers. They show up in the formula for the sums of $k$th powers. They are related to the Taylor series expansions of $x/\tan(x)$ and of $\coth(x)$, and also show up in the values that the Riemann Zeta function takes both at both negative and positive integers (and hence in the theory of modular forms). For example, if $1 \leq n$ is even then $$\zeta(2n)=\sum_{t\geq1}t^{-2n}=(-1)^{n+1}\frac{(2\pi)^{2n}B_{2n}}{2(2n)!}.$$ Note however that this result is not yet formalised in Lean. The Bernoulli numbers can be formally defined using the power series $$\sum B_n\frac{t^n}{n!}=\frac{t}{1-e^{-t}}$$ although that happens to not be the definition in mathlib (this is an *implementation detail* and need not concern the mathematician). Note that $B_1=-1/2$, meaning that we are using the $B_n^-$ of [from Wikipedia](https://en.wikipedia.org/wiki/Bernoulli_number). ## Implementation detail The Bernoulli numbers are defined using well-founded induction, by the formula $$B_n=1-\sum_{k\lt n}\frac{\binom{n}{k}}{n-k+1}B_k.$$ This formula is true for all $n$ and in particular $B_0=1$. Note that this is the definition for positive Bernoulli numbers, which we call `bernoulli'`. The negative Bernoulli numbers are then defined as `bernoulli := (-1)^n * bernoulli'`. ## Main theorems `sum_bernoulli : ∑ k in finset.range n, (n.choose k : ℚ) * bernoulli k = 0` -/ open_locale nat big_operators open finset nat finset.nat power_series variables (A : Type*) [comm_ring A] [algebra ℚ A] /-! ### Definitions -/ /-- The Bernoulli numbers: the $n$-th Bernoulli number $B_n$ is defined recursively via $$B_n = 1 - \sum_{k < n} \binom{n}{k}\frac{B_k}{n+1-k}$$ -/ def bernoulli' : ℕ → ℚ := well_founded.fix lt_wf $ λ n bernoulli', 1 - ∑ k : fin n, n.choose k / (n - k + 1) * bernoulli' k k.2 lemma bernoulli'_def' (n : ℕ) : bernoulli' n = 1 - ∑ k : fin n, n.choose k / (n - k + 1) * bernoulli' k := well_founded.fix_eq _ _ _ lemma bernoulli'_def (n : ℕ) : bernoulli' n = 1 - ∑ k in range n, n.choose k / (n - k + 1) * bernoulli' k := by { rw [bernoulli'_def', ← fin.sum_univ_eq_sum_range], refl } lemma bernoulli'_spec (n : ℕ) : ∑ k in range n.succ, (n.choose (n - k) : ℚ) / (n - k + 1) * bernoulli' k = 1 := begin rw [sum_range_succ_comm, bernoulli'_def n, tsub_self], conv in (n.choose (_ - _)) { rw choose_symm (mem_range.1 H).le }, simp only [one_mul, cast_one, sub_self, sub_add_cancel, choose_zero_right, zero_add, div_one], end lemma bernoulli'_spec' (n : ℕ) : ∑ k in antidiagonal n, ((k.1 + k.2).choose k.2 : ℚ) / (k.2 + 1) * bernoulli' k.1 = 1 := begin refine ((sum_antidiagonal_eq_sum_range_succ_mk _ n).trans _).trans (bernoulli'_spec n), refine sum_congr rfl (λ x hx, _), simp only [add_tsub_cancel_of_le, mem_range_succ_iff.mp hx, cast_sub], end /-! ### Examples -/ section examples @[simp] lemma bernoulli'_zero : bernoulli' 0 = 1 := by { rw bernoulli'_def, norm_num } @[simp] lemma bernoulli'_one : bernoulli' 1 = 1/2 := by { rw bernoulli'_def, norm_num } @[simp] lemma bernoulli'_two : bernoulli' 2 = 1/6 := by { rw bernoulli'_def, norm_num [sum_range_succ] } @[simp] lemma bernoulli'_three : bernoulli' 3 = 0 := by { rw bernoulli'_def, norm_num [sum_range_succ] } @[simp] lemma bernoulli'_four : bernoulli' 4 = -1/30 := have nat.choose 4 2 = 6 := dec_trivial, -- shrug by { rw bernoulli'_def, norm_num [sum_range_succ, this] } end examples @[simp] lemma sum_bernoulli' (n : ℕ) : ∑ k in range n, (n.choose k : ℚ) * bernoulli' k = n := begin cases n, { simp }, suffices : (n + 1 : ℚ) * ∑ k in range n, ↑(n.choose k) / (n - k + 1) * bernoulli' k = ∑ x in range n, ↑(n.succ.choose x) * bernoulli' x, { rw_mod_cast [sum_range_succ, bernoulli'_def, ← this, choose_succ_self_right], ring }, simp_rw [mul_sum, ← mul_assoc], refine sum_congr rfl (λ k hk, _), congr', have : ((n - k : ℕ) : ℚ) + 1 ≠ 0 := by apply_mod_cast succ_ne_zero, field_simp [← cast_sub (mem_range.1 hk).le, mul_comm], rw_mod_cast [tsub_add_eq_add_tsub (mem_range.1 hk).le, choose_mul_succ_eq], end /-- The exponential generating function for the Bernoulli numbers `bernoulli' n`. -/ def bernoulli'_power_series := mk $ λ n, algebra_map ℚ A (bernoulli' n / n!) theorem bernoulli'_power_series_mul_exp_sub_one : bernoulli'_power_series A * (exp A - 1) = X * exp A := begin ext n, -- constant coefficient is a special case cases n, { simp }, rw [bernoulli'_power_series, coeff_mul, mul_comm X, sum_antidiagonal_succ'], suffices : ∑ p in antidiagonal n, (bernoulli' p.1 / p.1!) * ((p.2 + 1) * p.2!)⁻¹ = n!⁻¹, { simpa [ring_hom.map_sum] using congr_arg (algebra_map ℚ A) this }, apply eq_inv_of_mul_eq_one_left, rw sum_mul, convert bernoulli'_spec' n using 1, apply sum_congr rfl, simp_rw [mem_antidiagonal], rintro ⟨i, j⟩ rfl, have : (j + 1 : ℚ) ≠ 0 := by exact_mod_cast succ_ne_zero j, have : (j + 1 : ℚ) * j! * i! ≠ 0 := by simpa [factorial_ne_zero], have := factorial_mul_factorial_dvd_factorial_add i j, field_simp [mul_comm _ (bernoulli' i), mul_assoc, add_choose], rw_mod_cast [mul_comm (j + 1), mul_div_assoc, ← mul_assoc], rw [cast_mul, cast_mul, mul_div_mul_right, cast_div_char_zero, cast_mul], assumption, rwa nat.cast_succ, end /-- Odd Bernoulli numbers (greater than 1) are zero. -/ theorem bernoulli'_odd_eq_zero {n : ℕ} (h_odd : odd n) (hlt : 1 < n) : bernoulli' n = 0 := begin let B := mk (λ n, bernoulli' n / n!), suffices : (B - eval_neg_hom B) * (exp ℚ - 1) = X * (exp ℚ - 1), { cases mul_eq_mul_right_iff.mp this; simp only [power_series.ext_iff, eval_neg_hom, coeff_X] at h, { apply eq_zero_of_neg_eq, specialize h n, split_ifs at h; simp [h_odd.neg_one_pow, factorial_ne_zero, *] at * }, { simpa using h 1 } }, have h : B * (exp ℚ - 1) = X * exp ℚ, { simpa [bernoulli'_power_series] using bernoulli'_power_series_mul_exp_sub_one ℚ }, rw [sub_mul, h, mul_sub X, sub_right_inj, ← neg_sub, mul_neg, neg_eq_iff_neg_eq], suffices : eval_neg_hom (B * (exp ℚ - 1)) * exp ℚ = eval_neg_hom (X * exp ℚ) * exp ℚ, { simpa [mul_assoc, sub_mul, mul_comm (eval_neg_hom (exp ℚ)), exp_mul_exp_neg_eq_one, eq_comm] }, congr', end /-- The Bernoulli numbers are defined to be `bernoulli'` with a parity sign. -/ def bernoulli (n : ℕ) : ℚ := (-1)^n * bernoulli' n lemma bernoulli'_eq_bernoulli (n : ℕ) : bernoulli' n = (-1)^n * bernoulli n := by simp [bernoulli, ← mul_assoc, ← sq, ← pow_mul, mul_comm n 2, pow_mul] @[simp] lemma bernoulli_zero : bernoulli 0 = 1 := by simp [bernoulli] @[simp] lemma bernoulli_one : bernoulli 1 = -1/2 := by norm_num [bernoulli] theorem bernoulli_eq_bernoulli'_of_ne_one {n : ℕ} (hn : n ≠ 1) : bernoulli n = bernoulli' n := begin by_cases h0 : n = 0, { simp [h0] }, rw [bernoulli, neg_one_pow_eq_pow_mod_two], cases mod_two_eq_zero_or_one n, { simp [h] }, simp [bernoulli'_odd_eq_zero (odd_iff.mpr h) (one_lt_iff_ne_zero_and_ne_one.mpr ⟨h0, hn⟩)], end @[simp] theorem sum_bernoulli (n : ℕ): ∑ k in range n, (n.choose k : ℚ) * bernoulli k = if n = 1 then 1 else 0 := begin cases n, { simp }, cases n, { simp }, suffices : ∑ i in range n, ↑((n + 2).choose (i + 2)) * bernoulli (i + 2) = n / 2, { simp only [this, sum_range_succ', cast_succ, bernoulli_one, bernoulli_zero, choose_one_right, mul_one, choose_zero_right, cast_zero, if_false, zero_add, succ_succ_ne_one], ring }, have f := sum_bernoulli' n.succ.succ, simp_rw [sum_range_succ', bernoulli'_one, choose_one_right, cast_succ, ← eq_sub_iff_add_eq] at f, convert f, { funext x, rw bernoulli_eq_bernoulli'_of_ne_one (succ_ne_zero x ∘ succ.inj) }, { simp only [one_div, mul_one, bernoulli'_zero, cast_one, choose_zero_right, add_sub_cancel], ring }, end lemma bernoulli_spec' (n : ℕ) : ∑ k in antidiagonal n, ((k.1 + k.2).choose k.2 : ℚ) / (k.2 + 1) * bernoulli k.1 = if n = 0 then 1 else 0 := begin cases n, { simp }, rw if_neg (succ_ne_zero _), -- algebra facts have h₁ : (1, n) ∈ antidiagonal n.succ := by simp [mem_antidiagonal, add_comm], have h₂ : (n : ℚ) + 1 ≠ 0 := by apply_mod_cast succ_ne_zero, have h₃ : (1 + n).choose n = n + 1 := by simp [add_comm], -- key equation: the corresponding fact for `bernoulli'` have H := bernoulli'_spec' n.succ, -- massage it to match the structure of the goal, then convert piece by piece rw sum_eq_add_sum_diff_singleton h₁ at H ⊢, apply add_eq_of_eq_sub', convert eq_sub_of_add_eq' H using 1, { refine sum_congr rfl (λ p h, _), obtain ⟨h', h''⟩ : p ∈ _ ∧ p ≠ _ := by rwa [mem_sdiff, mem_singleton] at h, simp [bernoulli_eq_bernoulli'_of_ne_one ((not_congr (antidiagonal_congr h' h₁)).mp h'')] }, { field_simp [h₃], norm_num }, end /-- The exponential generating function for the Bernoulli numbers `bernoulli n`. -/ def bernoulli_power_series := mk $ λ n, algebra_map ℚ A (bernoulli n / n!) theorem bernoulli_power_series_mul_exp_sub_one : bernoulli_power_series A * (exp A - 1) = X := begin ext n, -- constant coefficient is a special case cases n, { simp }, simp only [bernoulli_power_series, coeff_mul, coeff_X, sum_antidiagonal_succ', one_div, coeff_mk, coeff_one, coeff_exp, linear_map.map_sub, factorial, if_pos, cast_succ, cast_one, cast_mul, sub_zero, ring_hom.map_one, add_eq_zero_iff, if_false, _root_.inv_one, zero_add, one_ne_zero, mul_zero, and_false, sub_self, ← ring_hom.map_mul, ← ring_hom.map_sum], cases n, { simp }, rw if_neg n.succ_succ_ne_one, have hfact : ∀ m, (m! : ℚ) ≠ 0 := λ m, by exact_mod_cast factorial_ne_zero m, have hite2 : ite (n.succ = 0) 1 0 = (0 : ℚ) := if_neg n.succ_ne_zero, rw [←map_zero (algebra_map ℚ A), ←zero_div (n.succ! : ℚ), ←hite2, ← bernoulli_spec', sum_div], refine congr_arg (algebra_map ℚ A) (sum_congr rfl $ λ x h, eq_div_of_mul_eq (hfact n.succ) _), rw mem_antidiagonal at h, have hj : (x.2 + 1 : ℚ) ≠ 0 := by exact_mod_cast succ_ne_zero _, field_simp [← h, mul_ne_zero hj (hfact x.2), hfact x.1, mul_comm _ (bernoulli x.1), mul_assoc, add_choose, cast_div_char_zero (factorial_mul_factorial_dvd_factorial_add _ _), nat.factorial_ne_zero, hj], cc, end section faulhaber /-- **Faulhaber's theorem** relating the **sum of of p-th powers** to the Bernoulli numbers: $$\sum_{k=0}^{n-1} k^p = \sum_{i=0}^p B_i\binom{p+1}{i}\frac{n^{p+1-i}}{p+1}.$$ See https://proofwiki.org/wiki/Faulhaber%27s_Formula and [orosi2018faulhaber] for the proof provided here. -/ theorem sum_range_pow (n p : ℕ) : ∑ k in range n, (k : ℚ) ^ p = ∑ i in range (p + 1), bernoulli i * (p + 1).choose i * n ^ (p + 1 - i) / (p + 1) := begin have hne : ∀ m : ℕ, (m! : ℚ) ≠ 0 := λ m, by exact_mod_cast factorial_ne_zero m, -- compute the Cauchy product of two power series have h_cauchy : mk (λ p, bernoulli p / p!) * mk (λ q, coeff ℚ (q + 1) (exp ℚ ^ n)) = mk (λ p, ∑ i in range (p + 1), bernoulli i * (p + 1).choose i * n ^ (p + 1 - i) / (p + 1)!), { ext q : 1, let f := λ a b, bernoulli a / a! * coeff ℚ (b + 1) (exp ℚ ^ n), -- key step: use `power_series.coeff_mul` and then rewrite sums simp only [coeff_mul, coeff_mk, cast_mul, sum_antidiagonal_eq_sum_range_succ f], apply sum_congr rfl, simp_intros m h only [finset.mem_range], simp only [f, exp_pow_eq_rescale_exp, rescale, one_div, coeff_mk, ring_hom.coe_mk, coeff_exp, ring_hom.id_apply, cast_mul, algebra_map_rat_rat], -- manipulate factorials and binomial coefficients rw [choose_eq_factorial_div_factorial h.le, eq_comm, div_eq_iff (hne q.succ), succ_eq_add_one, mul_assoc _ _ ↑q.succ!, mul_comm _ ↑q.succ!, ← mul_assoc, div_mul_eq_mul_div, mul_comm (↑n ^ (q - m + 1)), ← mul_assoc _ _ (↑n ^ (q - m + 1)), ← one_div, mul_one_div, div_div, tsub_add_eq_add_tsub (le_of_lt_succ h), cast_div, cast_mul], { ring }, { exact factorial_mul_factorial_dvd_factorial h.le }, { simp [hne] } }, -- same as our goal except we pull out `p!` for convenience have hps : ∑ k in range n, ↑k ^ p = (∑ i in range (p + 1), bernoulli i * (p + 1).choose i * n ^ (p + 1 - i) / (p + 1)!) * p!, { suffices : mk (λ p, ∑ k in range n, ↑k ^ p * algebra_map ℚ ℚ p!⁻¹) = mk (λ p, ∑ i in range (p + 1), bernoulli i * (p + 1).choose i * n ^ (p + 1 - i) / (p + 1)!), { rw [← div_eq_iff (hne p), div_eq_mul_inv, sum_mul], rw power_series.ext_iff at this, simpa using this p }, -- the power series `exp ℚ - 1` is non-zero, a fact we need in order to use `mul_right_inj'` have hexp : exp ℚ - 1 ≠ 0, { simp only [exp, power_series.ext_iff, ne, not_forall], use 1, simp }, have h_r : exp ℚ ^ n - 1 = X * mk (λ p, coeff ℚ (p + 1) (exp ℚ ^ n)), { have h_const : C ℚ (constant_coeff ℚ (exp ℚ ^ n)) = 1 := by simp, rw [← h_const, sub_const_eq_X_mul_shift] }, -- key step: a chain of equalities of power series rw [← mul_right_inj' hexp, mul_comm, ← exp_pow_sum, geom_sum_mul, h_r, ← bernoulli_power_series_mul_exp_sub_one, bernoulli_power_series, mul_right_comm], simp [h_cauchy, mul_comm] }, -- massage `hps` into our goal rw [hps, sum_mul], refine sum_congr rfl (λ x hx, _), field_simp [mul_right_comm _ ↑p!, ← mul_assoc _ _ ↑p!, cast_add_one_ne_zero, hne], end /-- Alternate form of **Faulhaber's theorem**, relating the sum of p-th powers to the Bernoulli numbers: $$\sum_{k=1}^{n} k^p = \sum_{i=0}^p (-1)^iB_i\binom{p+1}{i}\frac{n^{p+1-i}}{p+1}.$$ Deduced from `sum_range_pow`. -/ theorem sum_Ico_pow (n p : ℕ) : ∑ k in Ico 1 (n + 1), (k : ℚ) ^ p = ∑ i in range (p + 1), bernoulli' i * (p + 1).choose i * n ^ (p + 1 - i) / (p + 1) := begin rw ← nat.cast_succ, -- dispose of the trivial case cases p, { simp }, let f := λ i, bernoulli i * p.succ.succ.choose i * n ^ (p.succ.succ - i) / p.succ.succ, let f' := λ i, bernoulli' i * p.succ.succ.choose i * n ^ (p.succ.succ - i) / p.succ.succ, suffices : ∑ k in Ico 1 n.succ, ↑k ^ p.succ = ∑ i in range p.succ.succ, f' i, { convert this }, -- prove some algebraic facts that will make things easier for us later on have hle := nat.le_add_left 1 n, have hne : (p + 1 + 1 : ℚ) ≠ 0 := by exact_mod_cast succ_ne_zero p.succ, have h1 : ∀ r : ℚ, r * (p + 1 + 1) * n ^ p.succ / (p + 1 + 1 : ℚ) = r * n ^ p.succ := λ r, by rw [mul_div_right_comm, mul_div_cancel _ hne], have h2 : f 1 + n ^ p.succ = 1 / 2 * n ^ p.succ, { simp_rw [f, bernoulli_one, choose_one_right, succ_sub_succ_eq_sub, cast_succ, tsub_zero, h1], ring }, have : ∑ i in range p, bernoulli (i + 2) * (p + 2).choose (i + 2) * n ^ (p - i) / ↑(p + 2) = ∑ i in range p, bernoulli' (i + 2) * (p + 2).choose (i + 2) * n ^ (p - i) / ↑(p + 2) := sum_congr rfl (λ i h, by rw bernoulli_eq_bernoulli'_of_ne_one (succ_succ_ne_one i)), calc ∑ k in Ico 1 n.succ, ↑k ^ p.succ -- replace sum over `Ico` with sum over `range` and simplify = ∑ k in range n.succ, ↑k ^ p.succ : by simp [sum_Ico_eq_sub _ hle, succ_ne_zero] -- extract the last term of the sum ... = ∑ k in range n, (k : ℚ) ^ p.succ + n ^ p.succ : by rw sum_range_succ -- apply the key lemma, `sum_range_pow` ... = ∑ i in range p.succ.succ, f i + n ^ p.succ : by simp [f, sum_range_pow] -- extract the first two terms of the sum ... = ∑ i in range p, f i.succ.succ + f 1 + f 0 + n ^ p.succ : by simp_rw [sum_range_succ'] ... = ∑ i in range p, f i.succ.succ + (f 1 + n ^ p.succ) + f 0 : by ring ... = ∑ i in range p, f i.succ.succ + 1 / 2 * n ^ p.succ + f 0 : by rw h2 -- convert from `bernoulli` to `bernoulli'` ... = ∑ i in range p, f' i.succ.succ + f' 1 + f' 0 : by { simp only [f, f'], simpa [h1, λ i, show i + 2 = i + 1 + 1, from rfl] } -- rejoin the first two terms of the sum ... = ∑ i in range p.succ.succ, f' i : by simp_rw [sum_range_succ'], end end faulhaber
17566a4dec80fa5a8e834cf5002163a163fbb93a
8b9f17008684d796c8022dab552e42f0cb6fb347
/tests/lean/run/tactic_notation.lean
df3caa5667ab6f18bd4d6ad4c34572cbb88e1c68
[ "Apache-2.0" ]
permissive
chubbymaggie/lean
0d06ae25f9dd396306fb02190e89422ea94afd7b
d2c7b5c31928c98f545b16420d37842c43b4ae9a
refs/heads/master
1,611,313,622,901
1,430,266,839,000
1,430,267,083,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
212
lean
import logic theorem tst1 (a b c : Prop) : a → b → a ∧ b := begin intros, apply and.intro, assumption end theorem tst2 (a b c : Prop) : a → b → a ∧ b := by intros; apply and.intro; assumption
fb4db660dc1d3e8ad2a8d2ee0aaf7276907fc8b9
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/tactic/tfae.lean
be57076e49257d36d6291c96b7672cd5e52b28c0
[ "Apache-2.0" ]
permissive
hikari0108/mathlib
b7ea2b7350497ab1a0b87a09d093ecc025a50dfa
a9e7d333b0cfd45f13a20f7b96b7d52e19fa2901
refs/heads/master
1,690,483,608,260
1,631,541,580,000
1,631,541,580,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,908
lean
/- Copyright (c) 2018 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Reid Barton, Simon Hudon "The Following Are Equivalent" (tfae) : Tactic for proving the equivalence of a set of proposition using various implications between them. -/ import data.list.tfae import tactic.scc open expr tactic lean lean.parser namespace tactic open interactive interactive.types expr export list (tfae) namespace tfae @[derive has_reflect, derive inhabited] inductive arrow : Type | right : arrow | left_right : arrow | left : arrow meta def mk_implication : Π (re : arrow) (e₁ e₂ : expr), pexpr | arrow.right e₁ e₂ := ``(%%e₁ → %%e₂) | arrow.left_right e₁ e₂ := ``(%%e₁ ↔ %%e₂) | arrow.left e₁ e₂ := ``(%%e₂ → %%e₁) meta def mk_name : Π (re : arrow) (i₁ i₂ : nat), name | arrow.right i₁ i₂ := ("tfae_" ++ to_string i₁ ++ "_to_" ++ to_string i₂ : string) | arrow.left_right i₁ i₂ := ("tfae_" ++ to_string i₁ ++ "_iff_" ++ to_string i₂ : string) | arrow.left i₁ i₂ := ("tfae_" ++ to_string i₂ ++ "_to_" ++ to_string i₁ : string) end tfae namespace interactive open tactic.tfae list meta def parse_list : expr → option (list expr) | `([]) := pure [] | `(%%e :: %%es) := (::) e <$> parse_list es | _ := none /-- In a goal of the form `tfae [a₀, a₁, a₂]`, `tfae_have : i → j` creates the assertion `aᵢ → aⱼ`. The other possible notations are `tfae_have : i ← j` and `tfae_have : i ↔ j`. The user can also provide a label for the assertion, as with `have`: `tfae_have h : i ↔ j`. -/ meta def tfae_have (h : parse $ optional ident <* tk ":") (i₁ : parse (with_desc "i" small_nat)) (re : parse (((tk "→" <|> tk "->") *> return arrow.right) <|> ((tk "↔" <|> tk "<->") *> return arrow.left_right) <|> ((tk "←" <|> tk "<-") *> return arrow.left))) (i₂ : parse (with_desc "j" small_nat)) : tactic unit := do `(tfae %%l) <- target, l ← parse_list l, e₁ ← list.nth l (i₁ - 1) <|> fail format!"index {i₁} is not between 1 and {l.length}", e₂ ← list.nth l (i₂ - 1) <|> fail format!"index {i₂} is not between 1 and {l.length}", type ← to_expr (tfae.mk_implication re e₁ e₂), let h := h.get_or_else (mk_name re i₁ i₂), tactic.assert h type, return () /-- Finds all implications and equivalences in the context to prove a goal of the form `tfae [...]`. -/ meta def tfae_finish : tactic unit := applyc ``tfae_nil <|> closure.with_new_closure (λ cl, do impl_graph.mk_scc cl, `(tfae %%l) ← target, l ← parse_list l, (_,r,_) ← cl.root l.head, refine ``(tfae_of_forall %%r _ _), thm ← mk_const ``forall_mem_cons, l.mmap' (λ e, do rewrite_target thm, split, (_,r',p) ← cl.root e, tactic.exact p ), applyc ``forall_mem_nil, pure ()) end interactive end tactic /-- The `tfae` tactic suite is a set of tactics that help with proving that certain propositions are equivalent. In `data/list/basic.lean` there is a section devoted to propositions of the form ```lean tfae [p1, p2, ..., pn] ``` where `p1`, `p2`, through, `pn` are terms of type `Prop`. This proposition asserts that all the `pi` are pairwise equivalent. There are results that allow to extract the equivalence of two propositions `pi` and `pj`. To prove a goal of the form `tfae [p1, p2, ..., pn]`, there are two tactics. The first tactic is `tfae_have`. As an argument it takes an expression of the form `i arrow j`, where `i` and `j` are two positive natural numbers, and `arrow` is an arrow such as `→`, `->`, `←`, `<-`, `↔`, or `<->`. The tactic `tfae_have : i arrow j` sets up a subgoal in which the user has to prove the equivalence (or implication) of `pi` and `pj`. The remaining tactic, `tfae_finish`, is a finishing tactic. It collects all implications and equivalences from the local context and computes their transitive closure to close the main goal. `tfae_have` and `tfae_finish` can be used together in a proof as follows: ```lean example (a b c d : Prop) : tfae [a,b,c,d] := begin tfae_have : 3 → 1, { /- prove c → a -/ }, tfae_have : 2 → 3, { /- prove b → c -/ }, tfae_have : 2 ← 1, { /- prove a → b -/ }, tfae_have : 4 ↔ 2, { /- prove d ↔ b -/ }, -- a b c d : Prop, -- tfae_3_to_1 : c → a, -- tfae_2_to_3 : b → c, -- tfae_1_to_2 : a → b, -- tfae_4_iff_2 : d ↔ b -- ⊢ tfae [a, b, c, d] tfae_finish, end ``` -/ add_tactic_doc { name := "tfae", category := doc_category.tactic, decl_names := [`tactic.interactive.tfae_have, `tactic.interactive.tfae_finish], tags := ["logic"], inherit_description_from := `tactic.interactive.tfae_finish }
4c82e8e7016d084f158dfee5e1f4919b53cbd771
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/vector.lean
5085b635df073e661bffaf50070ec7f2a6adf287
[ "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
4,892
lean
import logic data.nat.basic data.prod data.unit open nat prod inductive vector (A : Type) : nat → Type := | vnil {} : vector A zero | vcons : Π {n : nat}, A → vector A n → vector A (succ n) namespace vector -- print definition no_confusion infixr `::` := vcons local abbreviation no_confusion := @vector.no_confusion local abbreviation below := @vector.below theorem vcons.inj₁ {A : Type} {n : nat} (a₁ a₂ : A) (v₁ v₂ : vector A n) : vcons a₁ v₁ = vcons a₂ v₂ → a₁ = a₂ := begin intro h, apply (no_confusion h), intros, assumption end theorem vcons.inj₂ {A : Type} {n : nat} (a₁ a₂ : A) (v₁ v₂ : vector A n) : vcons a₁ v₁ = vcons a₂ v₂ → v₁ = v₂ := begin intro h, apply eq_of_heq, apply (no_confusion h), intros, eassumption, end set_option pp.universes true check @below namespace manual section universe variables l₁ l₂ variable {A : Type.{l₁}} variable {C : Π (n : nat), vector A n → Type.{l₂}} definition brec_on {n : nat} (v : vector A n) (H : Π (n : nat) (v : vector A n), @vector.below A C n v → C n v) : C n v := have general : C n v × @vector.below A C n v, from vector.rec_on v (pair (H zero vnil poly_unit.star) poly_unit.star) (λ (n₁ : nat) (a₁ : A) (v₁ : vector A n₁) (r₁ : C n₁ v₁ × @vector.below A C n₁ v₁), have b : @vector.below A C _ (vcons a₁ v₁), from r₁, have c : C (succ n₁) (vcons a₁ v₁), from H (succ n₁) (vcons a₁ v₁) b, pair c b), pr₁ general end end manual -- check vector.brec_on definition bw := @below definition sum {n : nat} (v : vector nat n) : nat := vector.brec_on v (λ (n : nat) (v : vector nat n), vector.cases_on v (λ (B : bw vnil), zero) (λ (n₁ : nat) (a : nat) (v₁ : vector nat n₁) (B : bw (vcons a v₁)), a + pr₁ B)) example : sum (10 :: 20 :: vnil) = 30 := rfl definition addk {n : nat} (v : vector nat n) (k : nat) : vector nat n := vector.brec_on v (λ (n : nat) (v : vector nat n), vector.cases_on v (λ (B : bw vnil), vnil) (λ (n₁ : nat) (a₁ : nat) (v₁ : vector nat n₁) (B : bw (vcons a₁ v₁)), vcons (a₁+k) (pr₁ B))) example : addk (1 :: 2 :: vnil) 3 = 4 :: 5 :: vnil := rfl definition append.{l} {A : Type.{l+1}} {n m : nat} (w : vector A m) (v : vector A n) : vector A (n + m) := vector.brec_on w (λ (n : nat) (w : vector A n), vector.cases_on w (λ (B : bw vnil), v) (λ (n₁ : nat) (a₁ : A) (v₁ : vector A n₁) (B : bw (vcons a₁ v₁)), vcons a₁ (pr₁ B))) example : append ((1:nat) :: 2 :: vnil) (3 :: vnil) = 1 :: 2 :: 3 :: vnil := rfl definition head {A : Type} {n : nat} (v : vector A (succ n)) : A := vector.cases_on v (λ H : succ n = 0, nat.no_confusion H) (λn' h t (H : succ n = succ n'), h) rfl definition tail {A : Type} {n : nat} (v : vector A (succ n)) : vector A n := @vector.cases_on A (λn' v, succ n = n' → vector A (pred n')) (succ n) v (λ H : succ n = 0, nat.no_confusion H) (λ (n' : nat) (h : A) (t : vector A n') (H : succ n = succ n'), t) rfl definition add {n : nat} (w v : vector nat n) : vector nat n := @vector.brec_on nat (λ (n : nat) (v : vector nat n), vector nat n → vector nat n) n w (λ (n : nat) (w : vector nat n), vector.cases_on w (λ (B : bw vnil) (w : vector nat zero), vnil) (λ (n₁ : nat) (a₁ : nat) (v₁ : vector nat n₁) (B : bw (vcons a₁ v₁)) (v : vector nat (succ n₁)), vcons (a₁ + head v) (pr₁ B (tail v)))) v example : add (1 :: 2 :: vnil) (3 :: 5 :: vnil) = 4 :: 7 :: vnil := rfl definition map {A B C : Type} {n : nat} (f : A → B → C) (w : vector A n) (v : vector B n) : vector C n := let P := λ (n : nat) (v : vector A n), vector B n → vector C n in @vector.brec_on A P n w (λ (n : nat) (w : vector A n), begin cases w with [n', h₁, t₁], show @below A P zero vnil → vector B zero → vector C zero, from λ b v, vnil, show @below A P (succ n') (h₁ :: t₁) → vector B (succ n') → vector C (succ n'), from λ b v, begin cases v with [n', h₂, t₂], have r : vector B n' → vector C n', from pr₁ b, exact ((f h₁ h₂) :: r t₂), end end) v theorem map_nil_nil {A B C : Type} (f : A → B → C) : map f vnil vnil = vnil := rfl theorem map_cons_cons {A B C : Type} (f : A → B → C) (a : A) (b : B) {n : nat} (va : vector A n) (vb : vector B n) : map f (a :: va) (b :: vb) = f a b :: map f va vb := rfl example : map nat.add (1 :: 2 :: vnil) (3 :: 5 :: vnil) = 4 :: 7 :: vnil := rfl print definition map end vector
26fa857ae9fecd758aced543b540328274a56716
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/data/set/enumerate.lean
9a47f76a99bdaae8df28e647685a174a97efe6d2
[ "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,837
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl -/ import tactic.wlog import data.nat.order.basic /-! # Set enumeration This file allows enumeration of sets given a choice function. The definition does not assume `sel` actually is a choice function, i.e. `sel s ∈ s` and `sel s = none ↔ s = ∅`. These assumptions are added to the lemmas needing them. -/ noncomputable theory open function namespace set section enumerate parameters {α : Type*} (sel : set α → option α) /-- Given a choice function `sel`, enumerates the elements of a set in the order `a 0 = sel s`, `a 1 = sel (s \ {a 0})`, `a 2 = sel (s \ {a 0, a 1})`, ... and stops when `sel (s \ {a 0, ..., a n}) = none`. Note that we don't require `sel` to be a choice function. -/ def enumerate : set α → ℕ → option α | s 0 := sel s | s (n + 1) := do a ← sel s, enumerate (s \ {a}) n lemma enumerate_eq_none_of_sel {s : set α} (h : sel s = none) : ∀ {n}, enumerate s n = none | 0 := by simp [h, enumerate]; refl | (n + 1) := by simp [h, enumerate]; refl lemma enumerate_eq_none : ∀ {s n₁ n₂}, enumerate s n₁ = none → n₁ ≤ n₂ → enumerate s n₂ = none | s 0 m := λ h _, enumerate_eq_none_of_sel h | s (n + 1) m := λ h hm, begin cases hs : sel s, { exact enumerate_eq_none_of_sel sel hs }, { cases m, case nat.zero { have : n + 1 = 0, from nat.eq_zero_of_le_zero hm, contradiction }, case nat.succ : m' { simp [hs, enumerate] at h ⊢, have hm : n ≤ m', from nat.le_of_succ_le_succ hm, exact enumerate_eq_none h hm } } end lemma enumerate_mem (h_sel : ∀ s a, sel s = some a → a ∈ s) : ∀ {s n a}, enumerate s n = some a → a ∈ s | s 0 a := h_sel s a | s (n+1) a := begin cases h : sel s, case none { simp [enumerate_eq_none_of_sel, h] }, case some : a' { simp [enumerate, h], exact λ h' : enumerate _ (s \ {a'}) n = some a, have a ∈ s \ {a'}, from enumerate_mem h', this.left } end lemma enumerate_inj {n₁ n₂ : ℕ} {a : α} {s : set α} (h_sel : ∀ s a, sel s = some a → a ∈ s) (h₁ : enumerate s n₁ = some a) (h₂ : enumerate s n₂ = some a) : n₁ = n₂ := begin wlog hn : n₁ ≤ n₂, { rcases nat.le.dest hn with ⟨m, rfl⟩, clear hn, induction n₁ generalizing s, case nat.zero { cases m, case nat.zero { refl }, case nat.succ : m { have : enumerate sel (s \ {a}) m = some a, { simp [enumerate, *] at * }, have : a ∈ s \ {a}, from enumerate_mem _ h_sel this, by simpa } }, case nat.succ { cases h : sel s; simp [enumerate, nat.add_succ, add_comm, *] at *; tauto } } end end enumerate end set
2952902d9d3488b2f513a54c5822c7f3d5bd85b6
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/number_theory/number_field/embeddings.lean
21eb1f6a2ca08dc4c1cfa7302a65177700b78c94
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
5,054
lean
/- Copyright (c) 2022 Xavier Roblot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Alex J. Best, Xavier Roblot -/ import analysis.normed_space.basic import number_theory.number_field.basic import topology.algebra.polynomial /-! # Embeddings of number fields This file defines the embeddings of a number field into an algebraic closed field. ## Main Results * `number_field.embeddings.eq_roots`: let `x ∈ K` with `K` number field and let `A` be an algebraic closed field of char. 0, then the images of `x` by the embeddings of `K` in `A` are exactly the roots in `A` of the minimal polynomial of `x` over `ℚ`. * `number_field.embeddings.pow_eq_one_of_norm_eq_one`: an algebraic integer whose conjugates are all of norm one is a root of unity. ## Tags number field, embeddings -/ namespace number_field.embeddings section fintype open finite_dimensional variables (K : Type*) [field K] [number_field K] variables (A : Type*) [field A] [char_zero A] /-- There are finitely many embeddings of a number field. -/ noncomputable instance : fintype (K →+* A) := fintype.of_equiv (K →ₐ[ℚ] A) ring_hom.equiv_rat_alg_hom.symm variables [is_alg_closed A] /-- The number of embeddings of a number field is equal to its finrank. -/ lemma card : fintype.card (K →+* A) = finrank ℚ K := by rw [fintype.of_equiv_card ring_hom.equiv_rat_alg_hom.symm, alg_hom.card] end fintype section roots open set polynomial variables (K A : Type*) [field K] [number_field K] [field A] [algebra ℚ A] [is_alg_closed A] (x : K) /-- Let `A` be an algebraically closed field and let `x ∈ K`, with `K` a number field. The images of `x` by the embeddings of `K` in `A` are exactly the roots in `A` of the minimal polynomial of `x` over `ℚ`. -/ lemma range_eval_eq_root_set_minpoly : range (λ φ : K →+* A, φ x) = (minpoly ℚ x).root_set A := begin convert (number_field.is_algebraic K).range_eval_eq_root_set_minpoly A x using 1, ext a, exact ⟨λ ⟨φ, hφ⟩, ⟨φ.to_rat_alg_hom, hφ⟩, λ ⟨φ, hφ⟩, ⟨φ.to_ring_hom, hφ⟩⟩, end end roots section bounded open finite_dimensional polynomial set variables {K : Type*} [field K] [number_field K] variables {A : Type*} [normed_field A] [is_alg_closed A] [normed_algebra ℚ A] lemma coeff_bdd_of_norm_le {B : ℝ} {x : K} (h : ∀ φ : K →+* A, ‖φ x‖ ≤ B) (i : ℕ) : ‖(minpoly ℚ x).coeff i‖ ≤ (max B 1) ^ (finrank ℚ K) * (finrank ℚ K).choose ((finrank ℚ K) / 2) := begin have hx := is_separable.is_integral ℚ x, rw [← norm_algebra_map' A, ← coeff_map (algebra_map ℚ A)], refine coeff_bdd_of_roots_le _ (minpoly.monic hx) (is_alg_closed.splits_codomain _) (minpoly.nat_degree_le hx) (λ z hz, _) i, classical, rw ← multiset.mem_to_finset at hz, obtain ⟨φ, rfl⟩ := (range_eval_eq_root_set_minpoly K A x).symm.subset hz, exact h φ, end variables (K A) /-- Let `B` be a real number. The set of algebraic integers in `K` whose conjugates are all smaller in norm than `B` is finite. -/ lemma finite_of_norm_le (B : ℝ) : {x : K | is_integral ℤ x ∧ ∀ φ : K →+* A, ‖φ x‖ ≤ B}.finite := begin let C := nat.ceil ((max B 1) ^ (finrank ℚ K) * (finrank ℚ K).choose ((finrank ℚ K) / 2)), have := bUnion_roots_finite (algebra_map ℤ K) (finrank ℚ K) (finite_Icc (-C : ℤ) C), refine this.subset (λ x hx, _), simp_rw mem_Union, have h_map_ℚ_minpoly := minpoly.gcd_domain_eq_field_fractions' ℚ hx.1, refine ⟨_, ⟨_, λ i, _⟩, (mem_root_set_iff (minpoly.ne_zero hx.1) x).2 (minpoly.aeval ℤ x)⟩, { rw [← (minpoly.monic hx.1).nat_degree_map (algebra_map ℤ ℚ), ← h_map_ℚ_minpoly], exact minpoly.nat_degree_le (is_integral_of_is_scalar_tower hx.1) }, rw [mem_Icc, ← abs_le, ← @int.cast_le ℝ], refine (eq.trans_le _ $ coeff_bdd_of_norm_le hx.2 i).trans (nat.le_ceil _), rw [h_map_ℚ_minpoly, coeff_map, eq_int_cast, int.norm_cast_rat, int.norm_eq_abs, int.cast_abs], end /-- An algebraic integer whose conjugates are all of norm one is a root of unity. -/ lemma pow_eq_one_of_norm_eq_one {x : K} (hxi : is_integral ℤ x) (hx : ∀ φ : K →+* A, ‖φ x‖ = 1) : ∃ (n : ℕ) (hn : 0 < n), x ^ n = 1 := begin obtain ⟨a, -, b, -, habne, h⟩ := @set.infinite.exists_ne_map_eq_of_maps_to _ _ _ _ ((^) x : ℕ → K) set.infinite_univ _ (finite_of_norm_le K A (1:ℝ)), { replace habne := habne.lt_or_lt, have : _, swap, cases habne, swap, { revert a b, exact this }, { exact this b a h.symm habne }, refine λ a b h hlt, ⟨a - b, tsub_pos_of_lt hlt, _⟩, rw [← nat.sub_add_cancel hlt.le, pow_add, mul_left_eq_self₀] at h, refine h.resolve_right (λ hp, _), specialize hx (is_alg_closed.lift (number_field.is_algebraic K)).to_ring_hom, rw [pow_eq_zero hp, map_zero, norm_zero] at hx, norm_num at hx }, { exact λ a _, ⟨hxi.pow a, λ φ, by simp only [hx φ, norm_pow, one_pow, map_pow]⟩ }, end end bounded end number_field.embeddings
8c9d73ea516335c7d67da12b10f877ebdf24aabd
618003631150032a5676f229d13a079ac875ff77
/src/data/fintype/basic.lean
f3217dc45ccba986d49764e57efe396aae172d6b
[ "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
41,929
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro Finite types. -/ import data.finset import data.array.lemmas universes u v variables {α : Type*} {β : Type*} {γ : Type*} /-- `fintype α` means that `α` is finite, i.e. there are only finitely many distinct elements of type `α`. The evidence of this is a finset `elems` (a list up to permutation without duplicates), together with a proof that everything of type `α` is in the list. -/ class fintype (α : Type*) := (elems [] : finset α) (complete : ∀ x : α, x ∈ elems) namespace finset variable [fintype α] /-- `univ` is the universal finite set of type `finset α` implied from the assumption `fintype α`. -/ def univ : finset α := fintype.elems α @[simp] theorem mem_univ (x : α) : x ∈ (univ : finset α) := fintype.complete x @[simp] theorem mem_univ_val : ∀ x, x ∈ (univ : finset α).1 := mem_univ @[simp] lemma coe_univ : ↑(univ : finset α) = (set.univ : set α) := by ext; simp theorem subset_univ (s : finset α) : s ⊆ univ := λ a _, mem_univ a theorem eq_univ_iff_forall {s : finset α} : s = univ ↔ ∀ x, x ∈ s := by simp [ext] @[simp] lemma univ_inter [decidable_eq α] (s : finset α) : univ ∩ s = s := ext' $ λ a, by simp @[simp] lemma inter_univ [decidable_eq α] (s : finset α) : s ∩ univ = s := by rw [inter_comm, univ_inter] @[simp] lemma piecewise_univ [∀i : α, decidable (i ∈ (univ : finset α))] {δ : α → Sort*} (f g : Πi, δ i) : univ.piecewise f g = f := by { ext i, simp [piecewise] } lemma univ_map_equiv_to_embedding {α β : Type*} [fintype α] [fintype β] (e : α ≃ β) : univ.map e.to_embedding = univ := begin apply eq_univ_iff_forall.mpr, intro b, rw [mem_map], use e.symm b, simp, end end finset open finset function namespace fintype instance decidable_pi_fintype {α} {β : α → Type*} [∀a, decidable_eq (β a)] [fintype α] : decidable_eq (Πa, β a) := assume f g, decidable_of_iff (∀ a ∈ fintype.elems α, f a = g a) (by simp [function.funext_iff, fintype.complete]) instance decidable_forall_fintype {p : α → Prop} [decidable_pred p] [fintype α] : decidable (∀ a, p a) := decidable_of_iff (∀ a ∈ @univ α _, p a) (by simp) instance decidable_exists_fintype {p : α → Prop} [decidable_pred p] [fintype α] : decidable (∃ a, p a) := decidable_of_iff (∃ a ∈ @univ α _, p a) (by simp) instance decidable_eq_equiv_fintype [decidable_eq β] [fintype α] : decidable_eq (α ≃ β) := λ a b, decidable_of_iff (a.1 = b.1) ⟨λ h, equiv.ext (congr_fun h), congr_arg _⟩ instance decidable_injective_fintype [decidable_eq α] [decidable_eq β] [fintype α] : decidable_pred (injective : (α → β) → Prop) := λ x, by unfold injective; apply_instance instance decidable_surjective_fintype [decidable_eq β] [fintype α] [fintype β] : decidable_pred (surjective : (α → β) → Prop) := λ x, by unfold surjective; apply_instance instance decidable_bijective_fintype [decidable_eq α] [decidable_eq β] [fintype α] [fintype β] : decidable_pred (bijective : (α → β) → Prop) := λ x, by unfold bijective; apply_instance instance decidable_left_inverse_fintype [decidable_eq α] [fintype α] (f : α → β) (g : β → α) : decidable (function.right_inverse f g) := show decidable (∀ x, g (f x) = x), by apply_instance instance decidable_right_inverse_fintype [decidable_eq β] [fintype β] (f : α → β) (g : β → α) : decidable (function.left_inverse f g) := show decidable (∀ x, f (g x) = x), by apply_instance /-- Construct a proof of `fintype α` from a universal multiset -/ def of_multiset [decidable_eq α] (s : multiset α) (H : ∀ x : α, x ∈ s) : fintype α := ⟨s.to_finset, by simpa using H⟩ /-- Construct a proof of `fintype α` from a universal list -/ def of_list [decidable_eq α] (l : list α) (H : ∀ x : α, x ∈ l) : fintype α := ⟨l.to_finset, by simpa using H⟩ theorem exists_univ_list (α) [fintype α] : ∃ l : list α, l.nodup ∧ ∀ x : α, x ∈ l := let ⟨l, e⟩ := quotient.exists_rep (@univ α _).1 in by have := and.intro univ.2 mem_univ_val; exact ⟨_, by rwa ← e at this⟩ /-- `card α` is the number of elements in `α`, defined when `α` is a fintype. -/ def card (α) [fintype α] : ℕ := (@univ α _).card /-- If `l` lists all the elements of `α` without duplicates, then `α ≃ fin (l.length)`. -/ def equiv_fin_of_forall_mem_list {α} [decidable_eq α] {l : list α} (h : ∀ x:α, x ∈ l) (nd : l.nodup) : α ≃ fin (l.length) := ⟨λ a, ⟨_, list.index_of_lt_length.2 (h a)⟩, λ i, l.nth_le i.1 i.2, λ a, by simp, λ ⟨i, h⟩, fin.eq_of_veq $ list.nodup_iff_nth_le_inj.1 nd _ _ (list.index_of_lt_length.2 (list.nth_le_mem _ _ _)) h $ by simp⟩ /-- There is (computably) a bijection between `α` and `fin n` where `n = card α`. Since it is not unique, and depends on which permutation of the universe list is used, the bijection is wrapped in `trunc` to preserve computability. -/ def equiv_fin (α) [fintype α] [decidable_eq α] : trunc (α ≃ fin (card α)) := by unfold card finset.card; exact quot.rec_on_subsingleton (@univ α _).1 (λ l (h : ∀ x:α, x ∈ l) (nd : l.nodup), trunc.mk (equiv_fin_of_forall_mem_list h nd)) mem_univ_val univ.2 theorem exists_equiv_fin (α) [fintype α] : ∃ n, nonempty (α ≃ fin n) := by haveI := classical.dec_eq α; exact ⟨card α, nonempty_of_trunc (equiv_fin α)⟩ /-- Given a linearly ordered fintype `α` of cardinal `k`, the equiv `mono_equiv_of_fin α h` is the increasing bijection between `fin k` and `α`. Here, `h` is a proof that the cardinality of `s` is `k`. We use this instead of a map `fin s.card → α` to avoid casting issues in further uses of this function. -/ noncomputable def mono_equiv_of_fin (α) [fintype α] [decidable_linear_order α] {k : ℕ} (h : fintype.card α = k) : fin k ≃ α := have A : bijective (mono_of_fin univ h) := begin apply set.bijective_iff_bij_on_univ.2, rw ← @coe_univ α _, exact mono_of_fin_bij_on (univ : finset α) h end, equiv.of_bijective A instance (α : Type*) : subsingleton (fintype α) := ⟨λ ⟨s₁, h₁⟩ ⟨s₂, h₂⟩, by congr; simp [finset.ext, h₁, h₂]⟩ protected def subtype {p : α → Prop} (s : finset α) (H : ∀ x : α, x ∈ s ↔ p x) : fintype {x // p x} := ⟨⟨multiset.pmap subtype.mk s.1 (λ x, (H x).1), multiset.nodup_pmap (λ a _ b _, congr_arg subtype.val) s.2⟩, λ ⟨x, px⟩, multiset.mem_pmap.2 ⟨x, (H x).2 px, rfl⟩⟩ theorem subtype_card {p : α → Prop} (s : finset α) (H : ∀ x : α, x ∈ s ↔ p x) : @card {x // p x} (fintype.subtype s H) = s.card := multiset.card_pmap _ _ _ theorem card_of_subtype {p : α → Prop} (s : finset α) (H : ∀ x : α, x ∈ s ↔ p x) [fintype {x // p x}] : card {x // p x} = s.card := by rw ← subtype_card s H; congr /-- Construct a fintype from a finset with the same elements. -/ def of_finset {p : set α} (s : finset α) (H : ∀ x, x ∈ s ↔ x ∈ p) : fintype p := fintype.subtype s H @[simp] theorem card_of_finset {p : set α} (s : finset α) (H : ∀ x, x ∈ s ↔ x ∈ p) : @fintype.card p (of_finset s H) = s.card := fintype.subtype_card s H theorem card_of_finset' {p : set α} (s : finset α) (H : ∀ x, x ∈ s ↔ x ∈ p) [fintype p] : fintype.card p = s.card := by rw ← card_of_finset s H; congr /-- If `f : α → β` is a bijection and `α` is a fintype, then `β` is also a fintype. -/ def of_bijective [fintype α] (f : α → β) (H : function.bijective f) : fintype β := ⟨univ.map ⟨f, H.1⟩, λ b, let ⟨a, e⟩ := H.2 b in e ▸ mem_map_of_mem _ (mem_univ _)⟩ /-- If `f : α → β` is a surjection and `α` is a fintype, then `β` is also a fintype. -/ def of_surjective [fintype α] [decidable_eq β] (f : α → β) (H : function.surjective f) : fintype β := ⟨univ.image f, λ b, let ⟨a, e⟩ := H b in e ▸ mem_image_of_mem _ (mem_univ _)⟩ noncomputable def of_injective [fintype β] (f : α → β) (H : function.injective f) : fintype α := by letI := classical.dec; exact if hα : nonempty α then by letI := classical.inhabited_of_nonempty hα; exact of_surjective (inv_fun f) (inv_fun_surjective H) else ⟨∅, λ x, (hα ⟨x⟩).elim⟩ /-- If `f : α ≃ β` and `α` is a fintype, then `β` is also a fintype. -/ def of_equiv (α : Type*) [fintype α] (f : α ≃ β) : fintype β := of_bijective _ f.bijective theorem of_equiv_card [fintype α] (f : α ≃ β) : @card β (of_equiv α f) = card α := multiset.card_map _ _ theorem card_congr {α β} [fintype α] [fintype β] (f : α ≃ β) : card α = card β := by rw ← of_equiv_card f; congr theorem card_eq {α β} [F : fintype α] [G : fintype β] : card α = card β ↔ nonempty (α ≃ β) := ⟨λ h, ⟨by classical; calc α ≃ fin (card α) : trunc.out (equiv_fin α) ... ≃ fin (card β) : by rw h ... ≃ β : (trunc.out (equiv_fin β)).symm⟩, λ ⟨f⟩, card_congr f⟩ def of_subsingleton (a : α) [subsingleton α] : fintype α := ⟨{a}, λ b, finset.mem_singleton.2 (subsingleton.elim _ _)⟩ @[simp] theorem univ_of_subsingleton (a : α) [subsingleton α] : @univ _ (of_subsingleton a) = {a} := rfl @[simp] theorem card_of_subsingleton (a : α) [subsingleton α] : @fintype.card _ (of_subsingleton a) = 1 := rfl end fintype namespace set /-- Construct a finset enumerating a set `s`, given a `fintype` instance. -/ def to_finset (s : set α) [fintype s] : finset α := ⟨(@finset.univ s _).1.map subtype.val, multiset.nodup_map (λ a b, subtype.eq) finset.univ.2⟩ @[simp] theorem mem_to_finset {s : set α} [fintype s] {a : α} : a ∈ s.to_finset ↔ a ∈ s := by simp [to_finset] @[simp] theorem mem_to_finset_val {s : set α} [fintype s] {a : α} : a ∈ s.to_finset.1 ↔ a ∈ s := mem_to_finset @[simp] theorem coe_to_finset (s : set α) [fintype s] : (↑s.to_finset : set α) = s := set.ext $ λ _, mem_to_finset end set lemma finset.card_univ [fintype α] : (finset.univ : finset α).card = fintype.card α := rfl lemma finset.card_univ_diff [fintype α] [decidable_eq α] (s : finset α) : (finset.univ \ s).card = fintype.card α - s.card := finset.card_sdiff (subset_univ s) instance (n : ℕ) : fintype (fin n) := ⟨⟨list.fin_range n, list.nodup_fin_range n⟩, list.mem_fin_range⟩ @[simp] theorem fintype.card_fin (n : ℕ) : fintype.card (fin n) = n := list.length_fin_range n @[simp] lemma finset.card_fin (n : ℕ) : finset.card (finset.univ : finset (fin n)) = n := by rw [finset.card_univ, fintype.card_fin] lemma fin.univ_succ (n : ℕ) : (univ : finset (fin $ n+1)) = insert 0 (univ.image fin.succ) := begin ext m, simp only [mem_univ, mem_insert, true_iff, mem_image, exists_prop], exact fin.cases (or.inl rfl) (λ i, or.inr ⟨i, trivial, rfl⟩) m end lemma fin.univ_cast_succ (n : ℕ) : (univ : finset (fin $ n+1)) = insert (fin.last n) (univ.image fin.cast_succ) := begin ext m, simp only [mem_univ, mem_insert, true_iff, mem_image, exists_prop, true_and], by_cases h : m.val < n, { right, use fin.cast_lt m h, rw fin.cast_succ_cast_lt }, { left, exact fin.eq_last_of_not_lt h } end /-- Any increasing map between `fin k` and a finset of cardinality `k` has to coincide with the increasing bijection `mono_of_fin s h`. -/ lemma finset.mono_of_fin_unique' [decidable_linear_order α] {s : finset α} {k : ℕ} (h : s.card = k) {f : fin k → α} (fmap : set.maps_to f set.univ ↑s) (hmono : strict_mono f) : f = s.mono_of_fin h := begin have finj : set.inj_on f set.univ := hmono.injective.inj_on _, apply mono_of_fin_unique h (set.bij_on.mk fmap finj (λ y hy, _)) hmono, simp only [set.image_univ, set.mem_range], rcases surj_on_of_inj_on_of_card_le (λ i (hi : i ∈ finset.univ), f i) (λ i hi, fmap (set.mem_univ i)) (λ i j hi hj hij, finj (set.mem_univ i) (set.mem_univ j) hij) (by simp [h]) y hy with ⟨x, _, hx⟩, exact ⟨x, hx.symm⟩ end @[instance, priority 10] def unique.fintype {α : Type*} [unique α] : fintype α := fintype.of_subsingleton (default α) @[simp] lemma univ_unique {α : Type*} [unique α] [f : fintype α] : @finset.univ α _ = {default α} := by rw [subsingleton.elim f (@unique.fintype α _)]; refl instance : fintype empty := ⟨∅, empty.rec _⟩ @[simp] theorem fintype.univ_empty : @univ empty _ = ∅ := rfl @[simp] theorem fintype.card_empty : fintype.card empty = 0 := rfl instance : fintype pempty := ⟨∅, pempty.rec _⟩ @[simp] theorem fintype.univ_pempty : @univ pempty _ = ∅ := rfl @[simp] theorem fintype.card_pempty : fintype.card pempty = 0 := rfl instance : fintype unit := fintype.of_subsingleton () theorem fintype.univ_unit : @univ unit _ = {()} := rfl theorem fintype.card_unit : fintype.card unit = 1 := rfl instance : fintype punit := fintype.of_subsingleton punit.star @[simp] theorem fintype.univ_punit : @univ punit _ = {punit.star} := rfl @[simp] theorem fintype.card_punit : fintype.card punit = 1 := rfl instance : fintype bool := ⟨⟨tt::ff::0, by simp⟩, λ x, by cases x; simp⟩ @[simp] theorem fintype.univ_bool : @univ bool _ = {tt, ff} := rfl instance units_int.fintype : fintype (units ℤ) := ⟨{1, -1}, λ x, by cases int.units_eq_one_or x; simp *⟩ instance additive.fintype : Π [fintype α], fintype (additive α) := id instance multiplicative.fintype : Π [fintype α], fintype (multiplicative α) := id @[simp] theorem fintype.card_units_int : fintype.card (units ℤ) = 2 := rfl noncomputable instance [monoid α] [fintype α] : fintype (units α) := by classical; exact fintype.of_injective units.val units.ext @[simp] theorem fintype.card_bool : fintype.card bool = 2 := rfl def finset.insert_none (s : finset α) : finset (option α) := ⟨none :: s.1.map some, multiset.nodup_cons.2 ⟨by simp, multiset.nodup_map (λ a b, option.some.inj) s.2⟩⟩ @[simp] theorem finset.mem_insert_none {s : finset α} : ∀ {o : option α}, o ∈ s.insert_none ↔ ∀ a ∈ o, a ∈ s | none := iff_of_true (multiset.mem_cons_self _ _) (λ a h, by cases h) | (some a) := multiset.mem_cons.trans $ by simp; refl theorem finset.some_mem_insert_none {s : finset α} {a : α} : some a ∈ s.insert_none ↔ a ∈ s := by simp instance {α : Type*} [fintype α] : fintype (option α) := ⟨univ.insert_none, λ a, by simp⟩ @[simp] theorem fintype.card_option {α : Type*} [fintype α] : fintype.card (option α) = fintype.card α + 1 := (multiset.card_cons _ _).trans (by rw multiset.card_map; refl) instance {α : Type*} (β : α → Type*) [fintype α] [∀ a, fintype (β a)] : fintype (sigma β) := ⟨univ.sigma (λ _, univ), λ ⟨a, b⟩, by simp⟩ @[simp] lemma finset.univ_sigma_univ {α : Type*} {β : α → Type*} [fintype α] [∀ a, fintype (β a)] : (univ : finset α).sigma (λ a, (univ : finset (β a))) = univ := rfl instance (α β : Type*) [fintype α] [fintype β] : fintype (α × β) := ⟨univ.product univ, λ ⟨a, b⟩, by simp⟩ @[simp] theorem fintype.card_prod (α β : Type*) [fintype α] [fintype β] : fintype.card (α × β) = fintype.card α * fintype.card β := card_product _ _ def fintype.fintype_prod_left {α β} [decidable_eq α] [fintype (α × β)] [nonempty β] : fintype α := ⟨(fintype.elems (α × β)).image prod.fst, assume a, let ⟨b⟩ := ‹nonempty β› in by simp; exact ⟨b, fintype.complete _⟩⟩ def fintype.fintype_prod_right {α β} [decidable_eq β] [fintype (α × β)] [nonempty α] : fintype β := ⟨(fintype.elems (α × β)).image prod.snd, assume b, let ⟨a⟩ := ‹nonempty α› in by simp; exact ⟨a, fintype.complete _⟩⟩ instance (α : Type*) [fintype α] : fintype (ulift α) := fintype.of_equiv _ equiv.ulift.symm @[simp] theorem fintype.card_ulift (α : Type*) [fintype α] : fintype.card (ulift α) = fintype.card α := fintype.of_equiv_card _ instance (α : Type u) (β : Type v) [fintype α] [fintype β] : fintype (α ⊕ β) := @fintype.of_equiv _ _ (@sigma.fintype _ (λ b, cond b (ulift α) (ulift.{(max u v) v} β)) _ (λ b, by cases b; apply ulift.fintype)) ((equiv.sum_equiv_sigma_bool _ _).symm.trans (equiv.sum_congr equiv.ulift equiv.ulift)) lemma fintype.card_le_of_injective [fintype α] [fintype β] (f : α → β) (hf : function.injective f) : fintype.card α ≤ fintype.card β := by haveI := classical.prop_decidable; exact finset.card_le_card_of_inj_on f (λ _ _, finset.mem_univ _) (λ _ _ _ _ h, hf h) lemma fintype.card_eq_one_iff [fintype α] : fintype.card α = 1 ↔ (∃ x : α, ∀ y, y = x) := by rw [← fintype.card_unit, fintype.card_eq]; exact ⟨λ ⟨a⟩, ⟨a.symm (), λ y, a.injective (subsingleton.elim _ _)⟩, λ ⟨x, hx⟩, ⟨⟨λ _, (), λ _, x, λ _, (hx _).trans (hx _).symm, λ _, subsingleton.elim _ _⟩⟩⟩ lemma fintype.card_eq_zero_iff [fintype α] : fintype.card α = 0 ↔ (α → false) := ⟨λ h a, have e : α ≃ empty := classical.choice (fintype.card_eq.1 (by simp [h])), (e a).elim, λ h, have e : α ≃ empty := ⟨λ a, (h a).elim, λ a, a.elim, λ a, (h a).elim, λ a, a.elim⟩, by simp [fintype.card_congr e]⟩ lemma fintype.card_pos_iff [fintype α] : 0 < fintype.card α ↔ nonempty α := ⟨λ h, classical.by_contradiction (λ h₁, have fintype.card α = 0 := fintype.card_eq_zero_iff.2 (λ a, h₁ ⟨a⟩), lt_irrefl 0 $ by rwa this at h), λ ⟨a⟩, nat.pos_of_ne_zero (mt fintype.card_eq_zero_iff.1 (λ h, h a))⟩ lemma fintype.card_le_one_iff [fintype α] : fintype.card α ≤ 1 ↔ (∀ a b : α, a = b) := let n := fintype.card α in have hn : n = fintype.card α := rfl, match n, hn with | 0 := λ ha, ⟨λ h, λ a, (fintype.card_eq_zero_iff.1 ha.symm a).elim, λ _, ha ▸ nat.le_succ _⟩ | 1 := λ ha, ⟨λ h, λ a b, let ⟨x, hx⟩ := fintype.card_eq_one_iff.1 ha.symm in by rw [hx a, hx b], λ _, ha ▸ le_refl _⟩ | (n+2) := λ ha, ⟨λ h, by rw ← ha at h; exact absurd h dec_trivial, (λ h, fintype.card_unit ▸ fintype.card_le_of_injective (λ _, ()) (λ _ _ _, h _ _))⟩ end lemma fintype.exists_ne_of_one_lt_card [fintype α] (h : 1 < fintype.card α) (a : α) : ∃ b : α, b ≠ a := let ⟨b, hb⟩ := classical.not_forall.1 (mt fintype.card_le_one_iff.2 (not_le_of_gt h)) in let ⟨c, hc⟩ := classical.not_forall.1 hb in by haveI := classical.dec_eq α; exact if hba : b = a then ⟨c, by cc⟩ else ⟨b, hba⟩ lemma fintype.exists_pair_of_one_lt_card [fintype α] (h : 1 < fintype.card α) : ∃ (a b : α), b ≠ a := begin rcases fintype.card_pos_iff.1 (nat.lt_of_succ_lt h) with a, exact ⟨a, fintype.exists_ne_of_one_lt_card h a⟩, end lemma fintype.injective_iff_surjective [fintype α] {f : α → α} : injective f ↔ surjective f := by haveI := classical.prop_decidable; exact have ∀ {f : α → α}, injective f → surjective f, from λ f hinj x, have h₁ : image f univ = univ := eq_of_subset_of_card_le (subset_univ _) ((card_image_of_injective univ hinj).symm ▸ le_refl _), have h₂ : x ∈ image f univ := h₁.symm ▸ mem_univ _, exists_of_bex (mem_image.1 h₂), ⟨this, λ hsurj, has_left_inverse.injective ⟨surj_inv hsurj, left_inverse_of_surjective_of_right_inverse (this (injective_surj_inv _)) (right_inverse_surj_inv _)⟩⟩ lemma fintype.injective_iff_bijective [fintype α] {f : α → α} : injective f ↔ bijective f := by simp [bijective, fintype.injective_iff_surjective] lemma fintype.surjective_iff_bijective [fintype α] {f : α → α} : surjective f ↔ bijective f := by simp [bijective, fintype.injective_iff_surjective] lemma fintype.injective_iff_surjective_of_equiv [fintype α] {f : α → β} (e : α ≃ β) : injective f ↔ surjective f := have injective (e.symm ∘ f) ↔ surjective (e.symm ∘ f), from fintype.injective_iff_surjective, ⟨λ hinj, by simpa [function.comp] using e.surjective.comp (this.1 (e.symm.injective.comp hinj)), λ hsurj, by simpa [function.comp] using e.injective.comp (this.2 (e.symm.surjective.comp hsurj))⟩ lemma fintype.coe_image_univ [fintype α] [decidable_eq β] {f : α → β} : ↑(finset.image f finset.univ) = set.range f := by { ext x, simp } instance list.subtype.fintype [decidable_eq α] (l : list α) : fintype {x // x ∈ l} := fintype.of_list l.attach l.mem_attach instance multiset.subtype.fintype [decidable_eq α] (s : multiset α) : fintype {x // x ∈ s} := fintype.of_multiset s.attach s.mem_attach instance finset.subtype.fintype (s : finset α) : fintype {x // x ∈ s} := ⟨s.attach, s.mem_attach⟩ instance finset_coe.fintype (s : finset α) : fintype (↑s : set α) := finset.subtype.fintype s @[simp] lemma fintype.card_coe (s : finset α) : fintype.card (↑s : set α) = s.card := card_attach lemma finset.card_le_one_iff {s : finset α} : s.card ≤ 1 ↔ ∀ {x y}, x ∈ s → y ∈ s → x = y := begin let t : set α := ↑s, letI : fintype t := finset_coe.fintype s, have : fintype.card t = s.card := fintype.card_coe s, rw [← this, fintype.card_le_one_iff], split, { assume H x y hx hy, exact subtype.mk.inj (H ⟨x, hx⟩ ⟨y, hy⟩) }, { assume H x y, exact subtype.eq (H x.2 y.2) } end lemma finset.one_lt_card_iff {s : finset α} : 1 < s.card ↔ ∃ x y, (x ∈ s) ∧ (y ∈ s) ∧ x ≠ y := begin classical, rw ← not_iff_not, push_neg, simpa [classical.or_iff_not_imp_left] using finset.card_le_one_iff end instance plift.fintype (p : Prop) [decidable p] : fintype (plift p) := ⟨if h : p then {⟨h⟩} else ∅, λ ⟨h⟩, by simp [h]⟩ instance Prop.fintype : fintype Prop := ⟨⟨true::false::0, by simp [true_ne_false]⟩, classical.cases (by simp) (by simp)⟩ def set_fintype {α} [fintype α] (s : set α) [decidable_pred s] : fintype s := fintype.subtype (univ.filter (∈ s)) (by simp) namespace function.embedding /-- An embedding from a `fintype` to itself can be promoted to an equivalence. -/ noncomputable def equiv_of_fintype_self_embedding {α : Type*} [fintype α] (e : α ↪ α) : α ≃ α := equiv.of_bijective (fintype.injective_iff_bijective.1 e.2) @[simp] lemma equiv_of_fintype_self_embedding_to_embedding {α : Type*} [fintype α] (e : α ↪ α) : e.equiv_of_fintype_self_embedding.to_embedding = e := by { ext, refl, } end function.embedding @[simp] lemma finset.univ_map_embedding {α : Type*} [fintype α] (e : α ↪ α) : univ.map e = univ := by rw [← e.equiv_of_fintype_self_embedding_to_embedding, univ_map_equiv_to_embedding] namespace fintype variables [fintype α] [decidable_eq α] {δ : α → Type*} /-- Given for all `a : α` a finset `t a` of `δ a`, then one can define the finset `fintype.pi_finset t` of all functions taking values in `t a` for all `a`. This is the analogue of `finset.pi` where the base finset is `univ` (but formally they are not the same, as there is an additional condition `i ∈ finset.univ` in the `finset.pi` definition). -/ def pi_finset (t : Πa, finset (δ a)) : finset (Πa, δ a) := (finset.univ.pi t).map ⟨λ f a, f a (mem_univ a), λ _ _, by simp [function.funext_iff]⟩ @[simp] lemma mem_pi_finset {t : Πa, finset (δ a)} {f : Πa, δ a} : f ∈ pi_finset t ↔ (∀a, f a ∈ t a) := begin split, { simp only [pi_finset, mem_map, and_imp, forall_prop_of_true, exists_prop, mem_univ, exists_imp_distrib, mem_pi], assume g hg hgf a, rw ← hgf, exact hg a }, { simp only [pi_finset, mem_map, forall_prop_of_true, exists_prop, mem_univ, mem_pi], assume hf, exact ⟨λ a ha, f a, hf, rfl⟩ } end lemma pi_finset_subset (t₁ t₂ : Πa, finset (δ a)) (h : ∀ a, t₁ a ⊆ t₂ a) : pi_finset t₁ ⊆ pi_finset t₂ := λ g hg, mem_pi_finset.2 $ λ a, h a $ mem_pi_finset.1 hg a lemma pi_finset_disjoint_of_disjoint [∀ a, decidable_eq (δ a)] (t₁ t₂ : Πa, finset (δ a)) {a : α} (h : disjoint (t₁ a) (t₂ a)) : disjoint (pi_finset t₁) (pi_finset t₂) := disjoint_iff_ne.2 $ λ f₁ hf₁ f₂ hf₂ eq₁₂, disjoint_iff_ne.1 h (f₁ a) (mem_pi_finset.1 hf₁ a) (f₂ a) (mem_pi_finset.1 hf₂ a) (congr_fun eq₁₂ a) end fintype /-! ### pi -/ /-- A dependent product of fintypes, indexed by a fintype, is a fintype. -/ instance pi.fintype {α : Type*} {β : α → Type*} [decidable_eq α] [fintype α] [∀a, fintype (β a)] : fintype (Πa, β a) := ⟨fintype.pi_finset (λ _, univ), by simp⟩ @[simp] lemma fintype.pi_finset_univ {α : Type*} {β : α → Type*} [decidable_eq α] [fintype α] [∀a, fintype (β a)] : fintype.pi_finset (λ a : α, (finset.univ : finset (β a))) = (finset.univ : finset (Π a, β a)) := rfl instance d_array.fintype {n : ℕ} {α : fin n → Type*} [∀n, fintype (α n)] : fintype (d_array n α) := fintype.of_equiv _ (equiv.d_array_equiv_fin _).symm instance array.fintype {n : ℕ} {α : Type*} [fintype α] : fintype (array n α) := d_array.fintype instance vector.fintype {α : Type*} [fintype α] {n : ℕ} : fintype (vector α n) := fintype.of_equiv _ (equiv.vector_equiv_fin _ _).symm instance quotient.fintype [fintype α] (s : setoid α) [decidable_rel ((≈) : α → α → Prop)] : fintype (quotient s) := fintype.of_surjective quotient.mk (λ x, quotient.induction_on x (λ x, ⟨x, rfl⟩)) instance finset.fintype [fintype α] : fintype (finset α) := ⟨univ.powerset, λ x, finset.mem_powerset.2 (finset.subset_univ _)⟩ @[simp] lemma fintype.card_finset [fintype α] : fintype.card (finset α) = 2 ^ (fintype.card α) := finset.card_powerset finset.univ instance subtype.fintype (p : α → Prop) [decidable_pred p] [fintype α] : fintype {x // p x} := set_fintype _ @[simp] lemma set.to_finset_univ [fintype α] : (set.univ : set α).to_finset = finset.univ := by { ext, simp only [set.mem_univ, mem_univ, set.mem_to_finset] } theorem fintype.card_subtype_le [fintype α] (p : α → Prop) [decidable_pred p] : fintype.card {x // p x} ≤ fintype.card α := by rw fintype.subtype_card; exact card_le_of_subset (subset_univ _) theorem fintype.card_subtype_lt [fintype α] {p : α → Prop} [decidable_pred p] {x : α} (hx : ¬ p x) : fintype.card {x // p x} < fintype.card α := by rw [fintype.subtype_card]; exact finset.card_lt_card ⟨subset_univ _, classical.not_forall.2 ⟨x, by simp [*, set.mem_def]⟩⟩ instance psigma.fintype {α : Type*} {β : α → Type*} [fintype α] [∀ a, fintype (β a)] : fintype (Σ' a, β a) := fintype.of_equiv _ (equiv.psigma_equiv_sigma _).symm instance psigma.fintype_prop_left {α : Prop} {β : α → Type*} [decidable α] [∀ a, fintype (β a)] : fintype (Σ' a, β a) := if h : α then fintype.of_equiv (β h) ⟨λ x, ⟨h, x⟩, psigma.snd, λ _, rfl, λ ⟨_, _⟩, rfl⟩ else ⟨∅, λ x, h x.1⟩ instance psigma.fintype_prop_right {α : Type*} {β : α → Prop} [∀ a, decidable (β a)] [fintype α] : fintype (Σ' a, β a) := fintype.of_equiv {a // β a} ⟨λ ⟨x, y⟩, ⟨x, y⟩, λ ⟨x, y⟩, ⟨x, y⟩, λ ⟨x, y⟩, rfl, λ ⟨x, y⟩, rfl⟩ instance psigma.fintype_prop_prop {α : Prop} {β : α → Prop} [decidable α] [∀ a, decidable (β a)] : fintype (Σ' a, β a) := if h : ∃ a, β a then ⟨{⟨h.fst, h.snd⟩}, λ ⟨_, _⟩, by simp⟩ else ⟨∅, λ ⟨x, y⟩, h ⟨x, y⟩⟩ instance set.fintype [fintype α] : fintype (set α) := ⟨(@finset.univ α _).powerset.map ⟨coe, coe_injective⟩, λ s, begin classical, refine mem_map.2 ⟨finset.univ.filter s, mem_powerset.2 (subset_univ _), _⟩, apply (coe_filter _).trans, rw [coe_univ, set.sep_univ], refl end⟩ instance pfun_fintype (p : Prop) [decidable p] (α : p → Type*) [Π hp, fintype (α hp)] : fintype (Π hp : p, α hp) := if hp : p then fintype.of_equiv (α hp) ⟨λ a _, a, λ f, f hp, λ _, rfl, λ _, rfl⟩ else ⟨singleton (λ h, (hp h).elim), by simp [hp, function.funext_iff]⟩ lemma mem_image_univ_iff_mem_range {α β : Type*} [fintype α] [decidable_eq β] {f : α → β} {b : β} : b ∈ univ.image f ↔ b ∈ set.range f := by simp lemma card_lt_card_of_injective_of_not_mem {α β : Type*} [fintype α] [fintype β] (f : α → β) (h : function.injective f) {b : β} (w : b ∉ set.range f) : fintype.card α < fintype.card β := begin classical, calc fintype.card α = (univ : finset α).card : rfl ... = (image f univ).card : (card_image_of_injective univ h).symm ... < (insert b (image f univ)).card : card_lt_card (ssubset_insert (mt mem_image_univ_iff_mem_range.mp w)) ... ≤ (univ : finset β).card : card_le_of_subset (subset_univ _) ... = fintype.card β : rfl end def quotient.fin_choice_aux {ι : Type*} [decidable_eq ι] {α : ι → Type*} [S : ∀ i, setoid (α i)] : ∀ (l : list ι), (∀ i ∈ l, quotient (S i)) → @quotient (Π i ∈ l, α i) (by apply_instance) | [] f := ⟦λ i, false.elim⟧ | (i::l) f := begin refine quotient.lift_on₂ (f i (list.mem_cons_self _ _)) (quotient.fin_choice_aux l (λ j h, f j (list.mem_cons_of_mem _ h))) _ _, exact λ a l, ⟦λ j h, if e : j = i then by rw e; exact a else l _ (h.resolve_left e)⟧, refine λ a₁ l₁ a₂ l₂ h₁ h₂, quotient.sound (λ j h, _), by_cases e : j = i; simp [e], { subst j, exact h₁ }, { exact h₂ _ _ } end theorem quotient.fin_choice_aux_eq {ι : Type*} [decidable_eq ι] {α : ι → Type*} [S : ∀ i, setoid (α i)] : ∀ (l : list ι) (f : ∀ i ∈ l, α i), quotient.fin_choice_aux l (λ i h, ⟦f i h⟧) = ⟦f⟧ | [] f := quotient.sound (λ i h, h.elim) | (i::l) f := begin simp [quotient.fin_choice_aux, quotient.fin_choice_aux_eq l], refine quotient.sound (λ j h, _), by_cases e : j = i; simp [e], subst j, refl end def quotient.fin_choice {ι : Type*} [fintype ι] [decidable_eq ι] {α : ι → Type*} [S : ∀ i, setoid (α i)] (f : ∀ i, quotient (S i)) : @quotient (Π i, α i) (by apply_instance) := quotient.lift_on (@quotient.rec_on _ _ (λ l : multiset ι, @quotient (Π i ∈ l, α i) (by apply_instance)) finset.univ.1 (λ l, quotient.fin_choice_aux l (λ i _, f i)) (λ a b h, begin have := λ a, quotient.fin_choice_aux_eq a (λ i h, quotient.out (f i)), simp [quotient.out_eq] at this, simp [this], let g := λ a:multiset ι, ⟦λ (i : ι) (h : i ∈ a), quotient.out (f i)⟧, refine eq_of_heq ((eq_rec_heq _ _).trans (_ : g a == g b)), congr' 1, exact quotient.sound h, end)) (λ f, ⟦λ i, f i (finset.mem_univ _)⟧) (λ a b h, quotient.sound $ λ i, h _ _) theorem quotient.fin_choice_eq {ι : Type*} [fintype ι] [decidable_eq ι] {α : ι → Type*} [∀ i, setoid (α i)] (f : ∀ i, α i) : quotient.fin_choice (λ i, ⟦f i⟧) = ⟦f⟧ := begin let q, swap, change quotient.lift_on q _ _ = _, have : q = ⟦λ i h, f i⟧, { dsimp [q], exact quotient.induction_on (@finset.univ ι _).1 (λ l, quotient.fin_choice_aux_eq _ _) }, simp [this], exact setoid.refl _ end section equiv open list equiv equiv.perm variables [decidable_eq α] [decidable_eq β] def perms_of_list : list α → list (perm α) | [] := [1] | (a :: l) := perms_of_list l ++ l.bind (λ b, (perms_of_list l).map (λ f, swap a b * f)) lemma length_perms_of_list : ∀ l : list α, length (perms_of_list l) = l.length.fact | [] := rfl | (a :: l) := begin rw [length_cons, nat.fact_succ], simp [perms_of_list, length_bind, length_perms_of_list, function.comp, nat.succ_mul], cc end lemma mem_perms_of_list_of_mem : ∀ {l : list α} {f : perm α} (h : ∀ x, f x ≠ x → x ∈ l), f ∈ perms_of_list l | [] f h := list.mem_singleton.2 $ equiv.ext $ λ x, by simp [imp_false, *] at * | (a::l) f h := if hfa : f a = a then mem_append_left _ $ mem_perms_of_list_of_mem (λ x hx, mem_of_ne_of_mem (λ h, by rw h at hx; exact hx hfa) (h x hx)) else have hfa' : f (f a) ≠ f a, from mt (λ h, f.injective h) hfa, have ∀ (x : α), (swap a (f a) * f) x ≠ x → x ∈ l, from λ x hx, have hxa : x ≠ a, from λ h, by simpa [h, mul_apply] using hx, have hfxa : f x ≠ f a, from mt (λ h, f.injective h) hxa, list.mem_of_ne_of_mem hxa (h x (λ h, by simp [h, mul_apply, swap_apply_def] at hx; split_ifs at hx; cc)), suffices f ∈ perms_of_list l ∨ ∃ (b : α), b ∈ l ∧ ∃ g : perm α, g ∈ perms_of_list l ∧ swap a b * g = f, by simpa [perms_of_list], (@or_iff_not_imp_left _ _ (classical.prop_decidable _)).2 (λ hfl, ⟨f a, if hffa : f (f a) = a then mem_of_ne_of_mem hfa (h _ (mt (λ h, f.injective h) hfa)) else this _ $ by simp [mul_apply, swap_apply_def]; split_ifs; cc, ⟨swap a (f a) * f, mem_perms_of_list_of_mem this, by rw [← mul_assoc, mul_def (swap a (f a)) (swap a (f a)), swap_swap, ← equiv.perm.one_def, one_mul]⟩⟩) lemma mem_of_mem_perms_of_list : ∀ {l : list α} {f : perm α}, f ∈ perms_of_list l → ∀ {x}, f x ≠ x → x ∈ l | [] f h := have f = 1 := by simpa [perms_of_list] using h, by rw this; simp | (a::l) f h := (mem_append.1 h).elim (λ h x hx, mem_cons_of_mem _ (mem_of_mem_perms_of_list h hx)) (λ h x hx, let ⟨y, hy, hy'⟩ := list.mem_bind.1 h in let ⟨g, hg₁, hg₂⟩ := list.mem_map.1 hy' in if hxa : x = a then by simp [hxa] else if hxy : x = y then mem_cons_of_mem _ $ by rwa hxy else mem_cons_of_mem _ $ mem_of_mem_perms_of_list hg₁ $ by rw [eq_inv_mul_iff_mul_eq.2 hg₂, mul_apply, swap_inv, swap_apply_def]; split_ifs; cc) lemma mem_perms_of_list_iff {l : list α} {f : perm α} : f ∈ perms_of_list l ↔ ∀ {x}, f x ≠ x → x ∈ l := ⟨mem_of_mem_perms_of_list, mem_perms_of_list_of_mem⟩ lemma nodup_perms_of_list : ∀ {l : list α} (hl : l.nodup), (perms_of_list l).nodup | [] hl := by simp [perms_of_list] | (a::l) hl := have hl' : l.nodup, from nodup_of_nodup_cons hl, have hln' : (perms_of_list l).nodup, from nodup_perms_of_list hl', have hmeml : ∀ {f : perm α}, f ∈ perms_of_list l → f a = a, from λ f hf, not_not.1 (mt (mem_of_mem_perms_of_list hf) (nodup_cons.1 hl).1), by rw [perms_of_list, list.nodup_append, list.nodup_bind, pairwise_iff_nth_le]; exact ⟨hln', ⟨λ _ _, nodup_map (λ _ _, (mul_right_inj _).1) hln', λ i j hj hij x hx₁ hx₂, let ⟨f, hf⟩ := list.mem_map.1 hx₁ in let ⟨g, hg⟩ := list.mem_map.1 hx₂ in have hix : x a = nth_le l i (lt_trans hij hj), by rw [← hf.2, mul_apply, hmeml hf.1, swap_apply_left], have hiy : x a = nth_le l j hj, by rw [← hg.2, mul_apply, hmeml hg.1, swap_apply_left], absurd (hf.2.trans (hg.2.symm)) $ λ h, ne_of_lt hij $ nodup_iff_nth_le_inj.1 hl' i j (lt_trans hij hj) hj $ by rw [← hix, hiy]⟩, λ f hf₁ hf₂, let ⟨x, hx, hx'⟩ := list.mem_bind.1 hf₂ in let ⟨g, hg⟩ := list.mem_map.1 hx' in have hgxa : g⁻¹ x = a, from f.injective $ by rw [hmeml hf₁, ← hg.2]; simp, have hxa : x ≠ a, from λ h, (list.nodup_cons.1 hl).1 (h ▸ hx), (list.nodup_cons.1 hl).1 $ hgxa ▸ mem_of_mem_perms_of_list hg.1 (by rwa [apply_inv_self, hgxa])⟩ def perms_of_finset (s : finset α) : finset (perm α) := quotient.hrec_on s.1 (λ l hl, ⟨perms_of_list l, nodup_perms_of_list hl⟩) (λ a b hab, hfunext (congr_arg _ (quotient.sound hab)) (λ ha hb _, heq_of_eq $ finset.ext.2 $ by simp [mem_perms_of_list_iff, hab.mem_iff])) s.2 lemma mem_perms_of_finset_iff : ∀ {s : finset α} {f : perm α}, f ∈ perms_of_finset s ↔ ∀ {x}, f x ≠ x → x ∈ s := by rintros ⟨⟨l⟩, hs⟩ f; exact mem_perms_of_list_iff lemma card_perms_of_finset : ∀ (s : finset α), (perms_of_finset s).card = s.card.fact := by rintros ⟨⟨l⟩, hs⟩; exact length_perms_of_list l def fintype_perm [fintype α] : fintype (perm α) := ⟨perms_of_finset (@finset.univ α _), by simp [mem_perms_of_finset_iff]⟩ instance [fintype α] [fintype β] : fintype (α ≃ β) := if h : fintype.card β = fintype.card α then trunc.rec_on_subsingleton (fintype.equiv_fin α) (λ eα, trunc.rec_on_subsingleton (fintype.equiv_fin β) (λ eβ, @fintype.of_equiv _ (perm α) fintype_perm (equiv_congr (equiv.refl α) (eα.trans (eq.rec_on h eβ.symm)) : (α ≃ α) ≃ (α ≃ β)))) else ⟨∅, λ x, false.elim (h (fintype.card_eq.2 ⟨x.symm⟩))⟩ lemma fintype.card_perm [fintype α] : fintype.card (perm α) = (fintype.card α).fact := subsingleton.elim (@fintype_perm α _ _) (@equiv.fintype α α _ _ _ _) ▸ card_perms_of_finset _ lemma fintype.card_equiv [fintype α] [fintype β] (e : α ≃ β) : fintype.card (α ≃ β) = (fintype.card α).fact := fintype.card_congr (equiv_congr (equiv.refl α) e) ▸ fintype.card_perm lemma univ_eq_singleton_of_card_one {α} [fintype α] (x : α) (h : fintype.card α = 1) : (univ : finset α) = {x} := begin apply symm, apply eq_of_subset_of_card_le (subset_univ ({x})), apply le_of_eq, simp [h, finset.card_univ] end end equiv namespace fintype section choose open fintype open equiv variables [fintype α] [decidable_eq α] (p : α → Prop) [decidable_pred p] def choose_x (hp : ∃! a : α, p a) : {a // p a} := ⟨finset.choose p univ (by simp; exact hp), finset.choose_property _ _ _⟩ def choose (hp : ∃! a, p a) : α := choose_x p hp lemma choose_spec (hp : ∃! a, p a) : p (choose p hp) := (choose_x p hp).property end choose section bijection_inverse open function variables [fintype α] [decidable_eq α] variables [fintype β] [decidable_eq β] variables {f : α → β} /-- ` `bij_inv f` is the unique inverse to a bijection `f`. This acts as a computable alternative to `function.inv_fun`. -/ def bij_inv (f_bij : bijective f) (b : β) : α := fintype.choose (λ a, f a = b) begin rcases f_bij.right b with ⟨a', fa_eq_b⟩, rw ← fa_eq_b, exact ⟨a', ⟨rfl, (λ a h, f_bij.left h)⟩⟩ end lemma left_inverse_bij_inv (f_bij : bijective f) : left_inverse (bij_inv f_bij) f := λ a, f_bij.left (choose_spec (λ a', f a' = f a) _) lemma right_inverse_bij_inv (f_bij : bijective f) : right_inverse (bij_inv f_bij) f := λ b, choose_spec (λ a', f a' = b) _ lemma bijective_bij_inv (f_bij : bijective f) : bijective (bij_inv f_bij) := ⟨(right_inverse_bij_inv _).injective, (left_inverse_bij_inv _).surjective⟩ end bijection_inverse lemma well_founded_of_trans_of_irrefl [fintype α] (r : α → α → Prop) [is_trans α r] [is_irrefl α r] : well_founded r := by classical; exact have ∀ x y, r x y → (univ.filter (λ z, r z x)).card < (univ.filter (λ z, r z y)).card, from λ x y hxy, finset.card_lt_card $ by simp only [finset.lt_iff_ssubset.symm, lt_iff_le_not_le, finset.le_iff_subset, finset.subset_iff, mem_filter, true_and, mem_univ, hxy]; exact ⟨λ z hzx, trans hzx hxy, not_forall_of_exists_not ⟨x, not_imp.2 ⟨hxy, irrefl x⟩⟩⟩, subrelation.wf this (measure_wf _) lemma preorder.well_founded [fintype α] [preorder α] : well_founded ((<) : α → α → Prop) := well_founded_of_trans_of_irrefl _ @[instance, priority 10] lemma linear_order.is_well_order [fintype α] [linear_order α] : is_well_order α (<) := { wf := preorder.well_founded } end fintype class infinite (α : Type*) : Prop := (not_fintype : fintype α → false) @[simp] lemma not_nonempty_fintype {α : Type*} : ¬nonempty (fintype α) ↔ infinite α := ⟨λf, ⟨λ x, f ⟨x⟩⟩, λ⟨f⟩ ⟨x⟩, f x⟩ namespace infinite lemma exists_not_mem_finset [infinite α] (s : finset α) : ∃ x, x ∉ s := classical.not_forall.1 $ λ h, not_fintype ⟨s, h⟩ @[priority 100] -- see Note [lower instance priority] instance nonempty (α : Type*) [infinite α] : nonempty α := nonempty_of_exists (exists_not_mem_finset (∅ : finset α)) lemma of_injective [infinite β] (f : β → α) (hf : injective f) : infinite α := ⟨λ I, by exactI not_fintype (fintype.of_injective f hf)⟩ lemma of_surjective [infinite β] (f : α → β) (hf : surjective f) : infinite α := ⟨λ I, by classical; exactI not_fintype (fintype.of_surjective f hf)⟩ private noncomputable def nat_embedding_aux (α : Type*) [infinite α] : ℕ → α | n := by letI := classical.dec_eq α; exact classical.some (exists_not_mem_finset ((multiset.range n).pmap (λ m (hm : m < n), nat_embedding_aux m) (λ _, multiset.mem_range.1)).to_finset) private lemma nat_embedding_aux_injective (α : Type*) [infinite α] : function.injective (nat_embedding_aux α) := begin assume m n h, letI := classical.dec_eq α, wlog hmlen : m ≤ n using m n, by_contradiction hmn, have hmn : m < n, from lt_of_le_of_ne hmlen hmn, refine (classical.some_spec (exists_not_mem_finset ((multiset.range n).pmap (λ m (hm : m < n), nat_embedding_aux α m) (λ _, multiset.mem_range.1)).to_finset)) _, refine multiset.mem_to_finset.2 (multiset.mem_pmap.2 ⟨m, multiset.mem_range.2 hmn, _⟩), rw [h, nat_embedding_aux] end noncomputable def nat_embedding (α : Type*) [infinite α] : ℕ ↪ α := ⟨_, nat_embedding_aux_injective α⟩ end infinite lemma not_injective_infinite_fintype [infinite α] [fintype β] (f : α → β) : ¬ injective f := assume (hf : injective f), have H : fintype α := fintype.of_injective f hf, infinite.not_fintype H lemma not_surjective_fintype_infinite [fintype α] [infinite β] (f : α → β) : ¬ surjective f := assume (hf : surjective f), have H : infinite α := infinite.of_surjective f hf, @infinite.not_fintype _ H infer_instance instance nat.infinite : infinite ℕ := ⟨λ ⟨s, hs⟩, finset.not_mem_range_self $ s.subset_range_sup_succ (hs _)⟩ instance int.infinite : infinite ℤ := infinite.of_injective int.of_nat (λ _ _, int.of_nat_inj)
8b23057a5ff57ef4dac05a297716d02400fad2ea
624f6f2ae8b3b1adc5f8f67a365c51d5126be45a
/tmp/new-frontend/parser/basic.lean
f6c7f0d7cb0779bba597ffdd2e3b400afa5c9ae1
[ "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
9,501
lean
/- Copyright (c) 2018 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Sebastian Ullrich Parser for the Lean language -/ prelude import init.lean.parser.parsec init.lean.parser.syntax init.lean.parser.rec import init.lean.parser.trie import init.lean.parser.identifier init.data.rbmap init.lean.message namespace Lean namespace Parser /- Maximum standard precedence. This is the precedence of Function application. In the standard Lean language, only the token `.` has a left-binding power greater than `maxPrec` (so that field accesses like `g (h x).f` are parsed as `g ((h x).f)`, not `(g (h x)).f`). -/ def maxPrec : Nat := 1024 structure TokenConfig := («prefix» : String) /- Left-binding power used by the Term Parser. The Term Parser operates in the context of a right-binding power between 0 (used by parentheses and on the top-Level) and (usually) `maxPrec` (used by Function application). After parsing an initial Term, it continues parsing and expanding that Term only when the left-binding power of the next token is greater than the current right-binding power. For example, it never continues parsing an argument after the initial parse, unless a token with lbp > maxPrec is encountered. Conversely, the Term Parser will always continue parsing inside parentheses until it finds a token with lbp 0 (such as `)`). -/ (lbp : Nat := 0) -- reading a token should not need any State /- An optional Parser that is activated after matching `prefix`. It should return a Syntax tree with a "hole" for the `SourceInfo` surrounding the token, which will be supplied by the `token` Parser. Remark: `suffixParser` has many applications for example for parsing hexdecimal numbers, `prefix` is `0x` and `suffixParser` is the Parser `digit*`. We also use it to parse String literals: here `prefix` is just `"`. -/ (suffixParser : Option (Parsec' (SourceInfo → Syntax)) := none) -- Backtrackable State structure ParserState := (messages : MessageLog) structure TokenCacheEntry := (startIt stopIt : String.OldIterator) (tk : Syntax) -- Non-backtrackable State structure ParserCache := (tokenCache : Option TokenCacheEntry := none) -- for profiling (hit miss : Nat := 0) structure FrontendConfig := (filename : String) (input : String) (fileMap : FileMap) /- Remark: if we have a Node in the Trie with `some TokenConfig`, the String induced by the path is equal to the `TokenConfig.prefix`. -/ structure ParserConfig extends FrontendConfig := (tokens : Trie TokenConfig) instance parserConfigCoe : HasCoe ParserConfig FrontendConfig := ⟨ParserConfig.toFrontendConfig⟩ @[derive Monad Alternative MonadParsec MonadExcept] def parserCoreT (m : Type → Type) [Monad m] := ParsecT Syntax $ StateT ParserCache $ m @[derive Monad Alternative MonadReader MonadParsec MonadExcept] def ParserT (ρ : Type) (m : Type → Type) [Monad m] := ReaderT ρ $ parserCoreT m @[derive Monad Alternative MonadReader MonadParsec MonadExcept] def BasicParserM := ParserT ParserConfig Id abbrev basicParser := BasicParserM Syntax abbrev monadBasicParser := HasMonadLiftT BasicParserM section local attribute [reducible] BasicParserM ParserT parserCoreT @[inline] def getCache : BasicParserM ParserCache := monadLift (get : StateT ParserCache Id _) @[inline] def putCache : ParserCache → BasicParserM PUnit := λ c, monadLift (set c : StateT ParserCache Id _) end -- an arbitrary `Parser` Type; parsers are usually some Monad stack based on `BasicParserM` returning `Syntax` variable {ρ : Type} class HasTokens (r : ρ) := mk {} :: (tokens : List TokenConfig) @[noinline, nospecialize] def tokens (r : ρ) [HasTokens r] := HasTokens.tokens r instance HasTokens.Inhabited (r : ρ) : Inhabited (HasTokens r) := ⟨⟨[]⟩⟩ instance List.nil.tokens : Parser.HasTokens ([] : List ρ) := default _ instance List.cons.tokens (r : ρ) (rs : List ρ) [Parser.HasTokens r] [Parser.HasTokens rs] : Parser.HasTokens (r::rs) := ⟨tokens r ++ tokens rs⟩ class HasView (α : outParam Type) (r : ρ) := (view : Syntax → α) (review : α → Syntax) export HasView (view review) def tryView {α : Type} (k : SyntaxNodeKind) [HasView α k] (stx : Syntax) : Option α := if stx.isOfKind k then some (HasView.view k stx) else none instance HasView.default (r : ρ) : Inhabited (Parser.HasView Syntax r) := ⟨{ view := id, review := id }⟩ class HasViewDefault (r : ρ) (α : outParam Type) [HasView α r] (default : α) := mk {} def messageOfParsecMessage {μ : Type} (cfg : FrontendConfig) (msg : Parsec.Message μ) : Message := {filename := cfg.filename, pos := cfg.fileMap.toPosition msg.it.offset, text := msg.text} /-- Run Parser stack, returning a partial Syntax tree in case of a fatal error -/ protected def run {m : Type → Type} {α ρ : Type} [Monad m] [HasCoeT ρ FrontendConfig] (cfg : ρ) (s : String) (r : StateT ParserState (ParserT ρ m) α) : m (Sum α Syntax × MessageLog) := do (r, _) ← (((r.run {messages:=MessageLog.empty}).run cfg).parse s).run {}, pure $ match r with | Except.ok (a, st) := (Sum.inl a, st.messages) | Except.error msg := (Sum.inr msg.custom.get, MessageLog.empty.add (messageOfParsecMessage cfg msg)) open MonadParsec open Parser.HasView variables {α : Type} {m : Type → Type} local notation `Parser` := m Syntax def logMessage {μ : Type} [Monad m] [MonadReader ρ m] [HasLiftT ρ FrontendConfig] [MonadState ParserState m] (msg : Parsec.Message μ) : m Unit := do cfg ← read, modify (λ st, {st with messages := st.messages.add (messageOfParsecMessage ↑cfg msg)}) def mkTokenTrie (tokens : List TokenConfig) : Except String (Trie TokenConfig) := do -- the only hardcoded tokens, because they are never directly mentioned by a `Parser` let builtinTokens : List TokenConfig := [{«prefix» := "/-"}, {«prefix» := "--"}], t ← (builtinTokens ++ tokens).mfoldl (λ (t : Trie TokenConfig) tk, match t.find tk.prefix with | some tk' := match tk.lbp, tk'.lbp with | l, 0 := pure $ t.insert tk.prefix tk | 0, _ := pure t | l, l' := if l = l' then pure t else throw $ "invalid token '" ++ tk.prefix ++ "', has been defined with precedences " ++ toString l ++ " and " ++ toString l' | none := pure $ t.insert tk.prefix tk) Trie.empty, pure t /- Monad stacks used in multiple files -/ /- NOTE: We move `RecT` under `ParserT`'s `ReaderT` so that `termParser`, which does not have access to `commandParser`'s ρ (=`CommandParserConfig`) can still recurse into it (for command quotations). This means that the `CommandParserConfig` will be reset on a recursive call to `command.Parser`, i.e. it forgets about locally registered parsers, but that's not an issue for our intended uses of it. -/ @[derive Monad Alternative MonadReader MonadParsec MonadExcept MonadRec] def CommandParserM (ρ : Type) := ReaderT ρ $ RecT Unit Syntax $ parserCoreT Id section local attribute [reducible] ParserT CommandParserM instance CommandParserM.MonadReaderAdapter (ρ ρ' : Type) : MonadReaderAdapter ρ ρ' (CommandParserM ρ) (CommandParserM ρ') := inferInstance instance CommandParserM.basicParser (ρ : Type) [HasLiftT ρ ParserConfig] : monadBasicParser (CommandParserM ρ) := ⟨λ _ x cfg rec, x.run ↑cfg⟩ end /- The `Nat` at `RecT` is the lbp` -/ @[derive Monad Alternative MonadReader MonadParsec MonadExcept MonadRec monadBasicParser] def TermParserM := RecT Nat Syntax $ CommandParserM ParserConfig abbrev termParser := TermParserM Syntax /-- A Term Parser for a suffix or infix notation that accepts a preceding Term. -/ @[derive Monad Alternative MonadReader MonadParsec MonadExcept MonadRec monadBasicParser] def TrailingTermParserM := ReaderT Syntax TermParserM abbrev trailingTermParser := TrailingTermParserM Syntax instance trailingTermParserCoe : HasCoe termParser trailingTermParser := ⟨λ x _, x⟩ /-- A multimap indexed by tokens. Used for indexing parsers by their leading token. -/ def TokenMap (α : Type) := RBMap Name (List α) Name.quickLt def TokenMap.insert {α : Type} (map : TokenMap α) (k : Name) (v : α) : TokenMap α := match map.find k with | none := map.insert k [v] | some vs := map.insert k (v::vs) def TokenMap.ofList {α : Type} : List (Name × α) → TokenMap α | [] := mkRBMap _ _ _ | (⟨k,v⟩::xs) := (TokenMap.ofList xs).insert k v instance tokenMapNil.tokens : Parser.HasTokens $ @TokenMap.ofList ρ [] := default _ instance tokenMapCons.tokens (k : Name) (r : ρ) (rs : List (Name × ρ)) [Parser.HasTokens r] [Parser.HasTokens $ TokenMap.ofList rs] : Parser.HasTokens $ TokenMap.ofList ((k,r)::rs) := ⟨tokens r ++ tokens (TokenMap.ofList rs)⟩ -- This needs to be a separate structure since `termParser`s cannot contain themselves in their config structure CommandParserConfig extends ParserConfig := (leadingTermParsers : TokenMap termParser) (trailingTermParsers : TokenMap trailingTermParser) -- local Term parsers (such as from `local notation`) hide previous parsers instead of overloading them (localLeadingTermParsers : TokenMap termParser := mkRBMap _ _ _) (localTrailingTermParsers : TokenMap trailingTermParser := mkRBMap _ _ _) instance commandParserConfigCoeParserConfig : HasCoe CommandParserConfig ParserConfig := ⟨CommandParserConfig.toParserConfig⟩ abbrev commandParser := CommandParserM CommandParserConfig Syntax end «Parser» end Lean
e7dcea2e453da4a117c17b9c5b942c01a66dfeb9
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/starsAndBars.lean
97707205c29be1eb8f49ec7743581a80d1307c25
[ "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,805
lean
@[simp] def List.count [DecidableEq α] : List α → α → Nat | [], _ => 0 | a::as, b => if a = b then as.count b + 1 else as.count b inductive StarsAndBars : Nat → Nat → Type where | nil : StarsAndBars 0 0 | star : StarsAndBars s b → StarsAndBars (s+1) b | bar : StarsAndBars s b → StarsAndBars s (b+1) namespace StarsAndBars namespace Ex1 def toList : StarsAndBars s b → { bs : List Bool // bs.count false = s ∧ bs.count true = b } | nil => ⟨[], by simp⟩ | star h => ⟨false :: toList h, by simp [(toList h).2]⟩ | bar h => ⟨true :: toList h, by simp [(toList h).2]⟩ theorem toList_star (h : StarsAndBars s b) (h') : toList (star h) = ⟨false :: toList h, h'⟩ := by rfl theorem toList_bar (h : StarsAndBars s b) (h') : toList (bar h) = ⟨true :: toList h, h'⟩ := by rfl end Ex1 /- Same example with explicit proofs -/ namespace Ex2 theorem toList_star_proof (h : bs.count false = s ∧ bs.count true = b) : (false :: bs).count false = s.succ ∧ (false :: bs).count true = b := by simp [*] theorem toList_bar_proof (h : bs.count false = s ∧ bs.count true = b) : (true :: bs).count false = s ∧ (true :: bs).count true = b.succ := by simp [*] def toList : StarsAndBars s b → { bs : List Bool // bs.count false = s ∧ bs.count true = b } | nil => ⟨[], by simp⟩ | star h => ⟨false :: toList h, toList_star_proof (toList h).property⟩ | bar h => ⟨true :: toList h, toList_bar_proof (toList h).property⟩ theorem toList_star (h : StarsAndBars s b) : toList (star h) = ⟨false :: toList h, toList_star_proof (toList h).property⟩ := by rfl theorem toList_bar (h : StarsAndBars s b) : toList (bar h) = ⟨true :: toList h, toList_bar_proof (toList h).property⟩ := by rfl end Ex2 end StarsAndBars
4c202574f09640d76ed81853281f40f0515699f3
fb3b29254af2d298d934a2ec1bddbb387e5ee67d
/src/cayleys.lean
22bddbda5c7bb68e67371c3e944acdd340808b30
[]
no_license
th-char/cayleys_theorem
cdbc48c543184320ae474b27e6d99e4b1c931e69
c4862adbe1e6a8892fd607217c803f260ee74be2
refs/heads/master
1,668,812,911,278
1,595,171,637,000
1,595,175,437,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,570
lean
import algebra.group.basic import data.zmod.basic import data.equiv.basic import tactic import data.set.basic universes u v open equiv function set variables (G : Type u) [group G] lemma perms_eq_fun_eq {X : Type u} (p : perm X) (q : perm X) (h : p.to_fun = q.to_fun) : p = q := begin apply perm.ext, intro x, exact congr_fun h x, end def lift_to_perm (G : Type*) [group G] : G →* perm G := { to_fun := λ g, { to_fun := λ x, g * x, inv_fun := λ x, g⁻¹ * x, left_inv := λ h, by simp, right_inv := λ h, by simp }, map_one' := by {ext, simp}, map_mul' := λ g h, by {ext, simp [mul_assoc _ _ _]}, } lemma lift_to_perm_inj {G : Type*} [group G] : injective (lift_to_perm G) := begin intros g₁ g₂ h, have H : ((lift_to_perm G) g₁) 1 = ((lift_to_perm G) g₂) 1, rw h, exact (mul_left_inj 1).mp H, end def iso_induces_perm_iso {X : Type u} {Y : Type v} : X ≃ Y → perm X ≃* perm Y := λ h, { to_fun := λ p, ({ to_fun := h.1 ∘ p.1 ∘ h.2, inv_fun := h.1 ∘ p.2 ∘ h.2, left_inv := λ y, by simp, right_inv := λ y, by simp, } : perm Y), inv_fun := λ (p : perm Y), ({ to_fun := h.2 ∘ p.1 ∘ h.1, inv_fun := h.2 ∘ p.2 ∘ h.1, left_inv := λ y, by simp, right_inv := λ y, by simp, } : perm X), left_inv := λ x, by {ext, simp}, right_inv := λ x, by {ext, simp}, map_mul' := λ p q, perms_eq_fun_eq _ _ (conj_comp h p.to_fun q.to_fun) } theorem cayleys (G : Type*) [group G] : ∃ (f : G →* perm G), injective f := begin use lift_to_perm G, exact lift_to_perm_inj, end variables {G₁ : Type*} {G₂ : Type*} [group G₁] [group G₂] noncomputable def inj_hom_induces_iso (f : G₁ →* G₂) (h_inj : injective f) : G₁ ≃* range f := { to_fun := λ g, ⟨f g, set.mem_range_self g⟩, inv_fun := begin rintro ⟨a, b⟩, choose h hk using b, exact h, end, left_inv := begin intro x, simp, apply h_inj, have H : f x ∈ range f := set.mem_range_self x, rw classical.some_spec H, end, right_inv := begin intro y, ext, rcases y with ⟨y, ⟨x, hx⟩⟩, simp, have H := Exists.intro x hx, rw classical.some_spec H, end, map_mul' := λ x y, by {ext, simp}, } noncomputable theorem cayleys2 {G : Type*} [group G] : G ≃* range (lift_to_perm G) := inj_hom_induces_iso (lift_to_perm G) lift_to_perm_inj
6c00d2991dbe078622b6efd6d4ae43c002b7fbfd
432d948a4d3d242fdfb44b81c9e1b1baacd58617
/src/data/nat/fib.lean
035983a2e08dc387e20947a15ef4e24db82589ca
[ "Apache-2.0" ]
permissive
JLimperg/aesop3
306cc6570c556568897ed2e508c8869667252e8a
a4a116f650cc7403428e72bd2e2c4cda300fe03f
refs/heads/master
1,682,884,916,368
1,620,320,033,000
1,620,320,033,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
5,734
lean
/- Copyright (c) 2019 Kevin Kappelmann. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin Kappelmann -/ import data.stream.basic import tactic import data.nat.gcd /-! # The Fibonacci Sequence ## Summary Definition of the Fibonacci sequence `F₀ = 0, F₁ = 1, Fₙ₊₂ = Fₙ + Fₙ₊₁`. ## Main Definitions - `fib` returns the stream of Fibonacci numbers. ## Main Statements - `fib_succ_succ` : shows that `fib` indeed satisfies the Fibonacci recurrence `Fₙ₊₂ = Fₙ + Fₙ₊₁.`. - `fib_gcd` : `fib n` is a strong divisibility sequence. ## Implementation Notes For efficiency purposes, the sequence is defined using `stream.iterate`. ## Tags fib, fibonacci -/ namespace nat /-- Auxiliary function used in the definition of `fib_aux_stream`. -/ private def fib_aux_step : (ℕ × ℕ) → (ℕ × ℕ) := λ p, ⟨p.snd, p.fst + p.snd⟩ /-- Auxiliary stream creating Fibonacci pairs `⟨Fₙ, Fₙ₊₁⟩`. -/ private def fib_aux_stream : stream (ℕ × ℕ) := stream.iterate fib_aux_step ⟨0, 1⟩ /-- Implementation of the fibonacci sequence satisfying `fib 0 = 0, fib 1 = 1, fib (n + 2) = fib n + fib (n + 1)`. *Note:* We use a stream iterator for better performance when compared to the naive recursive implementation. -/ @[pp_nodot] def fib (n : ℕ) : ℕ := (fib_aux_stream n).fst @[simp] lemma fib_zero : fib 0 = 0 := rfl @[simp] lemma fib_one : fib 1 = 1 := rfl @[simp] lemma fib_two : fib 2 = 1 := rfl private lemma fib_aux_stream_succ {n : ℕ} : fib_aux_stream (n + 1) = fib_aux_step (fib_aux_stream n) := begin change (stream.nth (n + 1) $ stream.iterate fib_aux_step ⟨0, 1⟩) = fib_aux_step (stream.nth n $ stream.iterate fib_aux_step ⟨0, 1⟩), rw [stream.nth_succ_iterate, stream.map_iterate, stream.nth_map] end /-- Shows that `fib` indeed satisfies the Fibonacci recurrence `Fₙ₊₂ = Fₙ + Fₙ₊₁.` -/ lemma fib_succ_succ {n : ℕ} : fib (n + 2) = fib n + fib (n + 1) := by simp only [fib, fib_aux_stream_succ, fib_aux_step] lemma fib_pos {n : ℕ} (n_pos : 0 < n) : 0 < fib n := begin induction n with n IH, case nat.zero { norm_num at n_pos }, case nat.succ { cases n, case nat.zero { simp [fib_succ_succ, zero_lt_one] }, case nat.succ { have : 0 ≤ fib n, by simp, exact (lt_add_of_nonneg_of_lt this $ IH n.succ_pos) }} end lemma fib_le_fib_succ {n : ℕ} : fib n ≤ fib (n + 1) := by { cases n; simp [fib_succ_succ] } @[mono] lemma fib_mono : monotone fib := monotone_of_monotone_nat $ λ _, fib_le_fib_succ /-- `fib (n + 2)` is strictly monotone. -/ lemma fib_add_two_strict_mono : strict_mono (λ n, fib (n + 2)) := strict_mono.nat $ λ n, lt_add_of_pos_left _ $ fib_pos succ_pos' lemma le_fib_self {n : ℕ} (five_le_n : 5 ≤ n) : n ≤ fib n := begin induction five_le_n with n five_le_n IH, { have : 5 = fib 5, by refl, -- 5 ≤ fib 5 exact le_of_eq this }, { -- n + 1 ≤ fib (n + 1) for 5 ≤ n cases n with n', -- rewrite n = succ n' to use fib.succ_succ { have : 5 = 0, from nat.le_zero_iff.elim_left five_le_n, contradiction }, rw fib_succ_succ, suffices : 1 + (n' + 1) ≤ fib n' + fib (n' + 1), by rwa [nat.succ_eq_add_one, add_comm], have : n' ≠ 0, by { intro h, have : 5 ≤ 1, by rwa h at five_le_n, norm_num at this }, have : 1 ≤ fib n', from nat.succ_le_of_lt (fib_pos $ pos_iff_ne_zero.mpr this), mono } end /-- Subsequent Fibonacci numbers are coprime, see https://proofwiki.org/wiki/Consecutive_Fibonacci_Numbers_are_Coprime -/ lemma fib_coprime_fib_succ (n : ℕ) : nat.coprime (fib n) (fib (n + 1)) := begin unfold coprime, induction n with n ih, { simp }, { convert ih using 1, rw [fib_succ_succ, succ_eq_add_one, gcd_rec, add_mod_right, gcd_comm (fib n), gcd_rec (fib (n + 1))], } end /-- See https://proofwiki.org/wiki/Fibonacci_Number_in_terms_of_Smaller_Fibonacci_Numbers -/ lemma fib_add (m n : ℕ) : fib m * fib n + fib (m + 1) * fib (n + 1) = fib (m + n + 1) := begin induction n with n ih generalizing m, { simp }, { intros, specialize ih (m + 1), rw [add_assoc m 1 n, add_comm 1 n] at ih, simp only [fib_succ_succ, ← ih], ring, } end lemma gcd_fib_add_self (m n : ℕ) : gcd (fib m) (fib (n + m)) = gcd (fib m) (fib n) := begin cases eq_zero_or_pos n, { rw h, simp }, replace h := nat.succ_pred_eq_of_pos h, rw [← h, succ_eq_add_one], calc gcd (fib m) (fib (n.pred + 1 + m)) = gcd (fib m) (fib (n.pred) * (fib m) + fib (n.pred + 1) * fib (m + 1)) : by { rw fib_add n.pred _, ring_nf } ... = gcd (fib m) (fib (n.pred + 1) * fib (m + 1)) : by rw [add_comm, gcd_add_mul_self (fib m) _ (fib (n.pred))] ... = gcd (fib m) (fib (n.pred + 1)) : coprime.gcd_mul_right_cancel_right (fib (n.pred + 1)) (coprime.symm (fib_coprime_fib_succ m)) end lemma gcd_fib_add_mul_self (m n : ℕ) : ∀ k, gcd (fib m) (fib (n + k * m)) = gcd (fib m) (fib n) | 0 := by simp | (k+1) := by rw [← gcd_fib_add_mul_self k, add_mul, ← add_assoc, one_mul, gcd_fib_add_self _ _] /-- `fib n` is a strong divisibility sequence, see https://proofwiki.org/wiki/GCD_of_Fibonacci_Numbers -/ lemma fib_gcd (m n : ℕ) : fib (gcd m n) = gcd (fib m) (fib n) := begin wlog h : m ≤ n using [n m, m n], exact le_total m n, { apply gcd.induction m n, { simp }, intros m n mpos h, rw ← gcd_rec m n at h, conv_rhs { rw ← mod_add_div' n m }, rwa [gcd_fib_add_mul_self m (n % m) (n / m), gcd_comm (fib m) _] }, rwa [gcd_comm, gcd_comm (fib m)] end lemma fib_dvd (m n : ℕ) (h : m ∣ n) : fib m ∣ fib n := by rwa [gcd_eq_left_iff_dvd, ← fib_gcd, gcd_eq_left_iff_dvd.mp] end nat
95eda6cf3dd3e63c87a6d2f7a33b9b92250cec5c
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/data/option/basic.lean
466066268bde3df3fa64f0353089a74bf237165d
[ "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
17,317
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import tactic.basic import logic.is_empty /-! # Option of a type This file develops the basic theory of option types. If `α` is a type, then `option α` can be understood as the type with one more element than `α`. `option α` has terms `some a`, where `a : α`, and `none`, which is the added element. This is useful in multiple ways: * It is the prototype of addition of terms to a type. See for example `with_bot α` which uses `none` as an element smaller than all others. * It can be used to define failsafe partial functions, which return `some the_result_we_expect` if we can find `the_result_we_expect`, and `none` if there is no meaningful result. This forces any subsequent use of the partial function to explicitly deal with the exceptions that make it return `none`. * `option` is a monad. We love monads. `part` is an alternative to `option` that can be seen as the type of `true`/`false` values along with a term `a : α` if the value is `true`. ## Implementation notes `option` is currently defined in core Lean, but this will change in Lean 4. -/ namespace option variables {α : Type*} {β : Type*} {γ : Type*} lemma coe_def : (coe : α → option α) = some := rfl lemma some_ne_none (x : α) : some x ≠ none := λ h, option.no_confusion h protected lemma «forall» {p : option α → Prop} : (∀ x, p x) ↔ p none ∧ ∀ x, p (some x) := ⟨λ h, ⟨h _, λ x, h _⟩, λ h x, option.cases_on x h.1 h.2⟩ protected lemma «exists» {p : option α → Prop} : (∃ x, p x) ↔ p none ∨ ∃ x, p (some x) := ⟨λ ⟨x, hx⟩, (option.cases_on x or.inl $ λ x hx, or.inr ⟨x, hx⟩) hx, λ h, h.elim (λ h, ⟨_, h⟩) (λ ⟨x, hx⟩, ⟨_, hx⟩)⟩ @[simp] theorem get_mem : ∀ {o : option α} (h : is_some o), option.get h ∈ o | (some a) _ := rfl theorem get_of_mem {a : α} : ∀ {o : option α} (h : is_some o), a ∈ o → option.get h = a | _ _ rfl := rfl @[simp] lemma not_mem_none (a : α) : a ∉ (none : option α) := λ h, option.no_confusion h @[simp] lemma some_get : ∀ {x : option α} (h : is_some x), some (option.get h) = x | (some x) hx := rfl @[simp] lemma get_some (x : α) (h : is_some (some x)) : option.get h = x := rfl @[simp] lemma get_or_else_some (x y : α) : option.get_or_else (some x) y = x := rfl @[simp] lemma get_or_else_none (x : α) : option.get_or_else none x = x := rfl @[simp] lemma get_or_else_coe (x y : α) : option.get_or_else ↑x y = x := rfl lemma get_or_else_of_ne_none {x : option α} (hx : x ≠ none) (y : α) : some (x.get_or_else y) = x := by cases x; [contradiction, rw get_or_else_some] theorem mem_unique {o : option α} {a b : α} (ha : a ∈ o) (hb : b ∈ o) : a = b := option.some.inj $ ha.symm.trans hb theorem mem.left_unique : relator.left_unique ((∈) : α → option α → Prop) := λ a o b, mem_unique theorem some_injective (α : Type*) : function.injective (@some α) := λ _ _, some_inj.mp /-- `option.map f` is injective if `f` is injective. -/ theorem map_injective {f : α → β} (Hf : function.injective f) : function.injective (option.map f) | none none H := rfl | (some a₁) (some a₂) H := by rw Hf (option.some.inj H) @[ext] theorem ext : ∀ {o₁ o₂ : option α}, (∀ a, a ∈ o₁ ↔ a ∈ o₂) → o₁ = o₂ | none none H := rfl | (some a) o H := ((H _).1 rfl).symm | o (some b) H := (H _).2 rfl theorem eq_none_iff_forall_not_mem {o : option α} : o = none ↔ (∀ a, a ∉ o) := ⟨λ e a h, by rw e at h; cases h, λ h, ext $ by simpa⟩ @[simp] theorem none_bind {α β} (f : α → option β) : none >>= f = none := rfl @[simp] theorem some_bind {α β} (a : α) (f : α → option β) : some a >>= f = f a := rfl @[simp] theorem none_bind' (f : α → option β) : none.bind f = none := rfl @[simp] theorem some_bind' (a : α) (f : α → option β) : (some a).bind f = f a := rfl @[simp] theorem bind_some : ∀ x : option α, x >>= some = x := @bind_pure α option _ _ @[simp] theorem bind_eq_some {α β} {x : option α} {f : α → option β} {b : β} : x >>= f = some b ↔ ∃ a, x = some a ∧ f a = some b := by cases x; simp @[simp] theorem bind_eq_some' {x : option α} {f : α → option β} {b : β} : x.bind f = some b ↔ ∃ a, x = some a ∧ f a = some b := by cases x; simp @[simp] theorem bind_eq_none' {o : option α} {f : α → option β} : o.bind f = none ↔ (∀ b a, a ∈ o → b ∉ f a) := by simp only [eq_none_iff_forall_not_mem, not_exists, not_and, mem_def, bind_eq_some'] @[simp] theorem bind_eq_none {α β} {o : option α} {f : α → option β} : o >>= f = none ↔ (∀ b a, a ∈ o → b ∉ f a) := bind_eq_none' lemma bind_comm {α β γ} {f : α → β → option γ} (a : option α) (b : option β) : a.bind (λx, b.bind (f x)) = b.bind (λy, a.bind (λx, f x y)) := by cases a; cases b; refl lemma bind_assoc (x : option α) (f : α → option β) (g : β → option γ) : (x.bind f).bind g = x.bind (λ y, (f y).bind g) := by cases x; refl lemma join_eq_some {x : option (option α)} {a : α} : x.join = some a ↔ x = some (some a) := by simp lemma join_ne_none {x : option (option α)} : x.join ≠ none ↔ ∃ z, x = some (some z) := by simp lemma join_ne_none' {x : option (option α)} : ¬(x.join = none) ↔ ∃ z, x = some (some z) := by simp lemma join_eq_none {o : option (option α)} : o.join = none ↔ o = none ∨ o = some none := by rcases o with _|_|_; simp lemma bind_id_eq_join {x : option (option α)} : x >>= id = x.join := by simp lemma join_eq_join : mjoin = @join α := funext (λ x, by rw [mjoin, bind_id_eq_join]) lemma bind_eq_bind {α β : Type*} {f : α → option β} {x : option α} : x >>= f = x.bind f := rfl @[simp] lemma map_eq_map {α β} {f : α → β} : (<$>) f = option.map f := rfl theorem map_none {α β} {f : α → β} : f <$> none = none := rfl theorem map_some {α β} {a : α} {f : α → β} : f <$> some a = some (f a) := rfl @[simp] theorem map_none' {f : α → β} : option.map f none = none := rfl @[simp] theorem map_some' {a : α} {f : α → β} : option.map f (some a) = some (f a) := rfl theorem map_eq_some {α β} {x : option α} {f : α → β} {b : β} : f <$> x = some b ↔ ∃ a, x = some a ∧ f a = b := by cases x; simp @[simp] theorem map_eq_some' {x : option α} {f : α → β} {b : β} : x.map f = some b ↔ ∃ a, x = some a ∧ f a = b := by cases x; simp lemma map_eq_none {α β} {x : option α} {f : α → β} : f <$> x = none ↔ x = none := by { cases x; simp only [map_none, map_some, eq_self_iff_true] } @[simp] lemma map_eq_none' {x : option α} {f : α → β} : x.map f = none ↔ x = none := by { cases x; simp only [map_none', map_some', eq_self_iff_true] } lemma map_congr {f g : α → β} {x : option α} (h : ∀ a ∈ x, f a = g a) : option.map f x = option.map g x := by { cases x; simp only [map_none', map_some', h, mem_def] } @[simp] theorem map_id' : option.map (@id α) = id := map_id @[simp] lemma map_map (h : β → γ) (g : α → β) (x : option α) : option.map h (option.map g x) = option.map (h ∘ g) x := by { cases x; simp only [map_none', map_some'] } lemma comp_map (h : β → γ) (g : α → β) (x : option α) : option.map (h ∘ g) x = option.map h (option.map g x) := (map_map _ _ _).symm @[simp] lemma map_comp_map (f : α → β) (g : β → γ) : option.map g ∘ option.map f = option.map (g ∘ f) := by { ext x, rw comp_map } lemma mem_map_of_mem {α β : Type*} {a : α} {x : option α} (g : α → β) (h : a ∈ x) : g a ∈ x.map g := mem_def.mpr ((mem_def.mp h).symm ▸ map_some') lemma bind_map_comm {α β} {x : option (option α) } {f : α → β} : x >>= option.map f = x.map (option.map f) >>= id := by { cases x; simp } lemma join_map_eq_map_join {f : α → β} {x : option (option α)} : (x.map (option.map f)).join = x.join.map f := by { rcases x with _ | _ | x; simp } lemma join_join {x : option (option (option α))} : x.join.join = (x.map join).join := by { rcases x with _ | _ | _ | x; simp } lemma mem_of_mem_join {a : α} {x : option (option α)} (h : a ∈ x.join) : some a ∈ x := mem_def.mpr ((mem_def.mp h).symm ▸ join_eq_some.mp h) section pmap variables {p : α → Prop} (f : Π (a : α), p a → β) (x : option α) @[simp] lemma pbind_eq_bind (f : α → option β) (x : option α) : x.pbind (λ a _, f a) = x.bind f := by { cases x; simp only [pbind, none_bind', some_bind'] } lemma map_bind {α β γ} (f : β → γ) (x : option α) (g : α → option β) : option.map f (x >>= g) = (x >>= λ a, option.map f (g a)) := by simp_rw [←map_eq_map, ←bind_pure_comp_eq_map,is_lawful_monad.bind_assoc] lemma map_bind' (f : β → γ) (x : option α) (g : α → option β) : option.map f (x.bind g) = x.bind (λ a, option.map f (g a)) := by { cases x; simp } lemma map_pbind (f : β → γ) (x : option α) (g : Π a, a ∈ x → option β) : option.map f (x.pbind g) = (x.pbind (λ a H, option.map f (g a H))) := by { cases x; simp only [pbind, map_none'] } lemma pbind_map (f : α → β) (x : option α) (g : Π (b : β), b ∈ x.map f → option γ) : pbind (option.map f x) g = x.pbind (λ a h, g (f a) (mem_map_of_mem _ h)) := by { cases x; refl } @[simp] lemma pmap_none (f : Π (a : α), p a → β) {H} : pmap f (@none α) H = none := rfl @[simp] lemma pmap_some (f : Π (a : α), p a → β) {x : α} (h : p x) : pmap f (some x) = λ _, some (f x h) := rfl lemma mem_pmem {a : α} (h : ∀ a ∈ x, p a) (ha : a ∈ x) : f a (h a ha) ∈ pmap f x h := by { rw mem_def at ha ⊢, subst ha, refl } lemma pmap_map (g : γ → α) (x : option γ) (H) : pmap f (x.map g) H = pmap (λ a h, f (g a) h) x (λ a h, H _ (mem_map_of_mem _ h)) := by { cases x; simp only [map_none', map_some', pmap] } lemma map_pmap (g : β → γ) (f : Π a, p a → β) (x H) : option.map g (pmap f x H) = pmap (λ a h, g (f a h)) x H := by { cases x; simp only [map_none', map_some', pmap] } @[simp] lemma pmap_eq_map (p : α → Prop) (f : α → β) (x H) : @pmap _ _ p (λ a _, f a) x H = option.map f x := by { cases x; simp only [map_none', map_some', pmap] } lemma pmap_bind {α β γ} {x : option α} {g : α → option β} {p : β → Prop} {f : Π b, p b → γ} (H) (H' : ∀ (a : α) b ∈ g a, b ∈ x >>= g) : pmap f (x >>= g) H = (x >>= λa, pmap f (g a) (λ b h, H _ (H' a _ h))) := by { cases x; simp only [pmap, none_bind, some_bind] } lemma bind_pmap {α β γ} {p : α → Prop} (f : Π a, p a → β) (x : option α) (g : β → option γ) (H) : (pmap f x H) >>= g = x.pbind (λ a h, g (f a (H _ h))) := by { cases x; simp only [pmap, none_bind, some_bind, pbind] } variables {f x} lemma pbind_eq_none {f : Π (a : α), a ∈ x → option β} (h' : ∀ a ∈ x, f a H = none → x = none) : x.pbind f = none ↔ x = none := begin cases x, { simp }, { simp only [pbind, iff_false], intro h, cases h' x rfl h } end lemma pbind_eq_some {f : Π (a : α), a ∈ x → option β} {y : β} : x.pbind f = some y ↔ ∃ (z ∈ x), f z H = some y := begin cases x, { simp }, { simp only [pbind], split, { intro h, use x, simpa only [mem_def, exists_prop_of_true] using h }, { rintro ⟨z, H, hz⟩, simp only [mem_def] at H, simpa only [H] using hz } } end @[simp] lemma pmap_eq_none_iff {h} : pmap f x h = none ↔ x = none := by { cases x; simp } @[simp] lemma pmap_eq_some_iff {hf} {y : β} : pmap f x hf = some y ↔ ∃ (a : α) (H : x = some a), f a (hf a H) = y := begin cases x, { simp only [not_mem_none, exists_false, pmap, not_false_iff, exists_prop_of_false] }, { split, { intro h, simp only [pmap] at h, exact ⟨x, rfl, h⟩ }, { rintro ⟨a, H, rfl⟩, simp only [mem_def] at H, simp only [H, pmap] } } end @[simp] lemma join_pmap_eq_pmap_join {f : Π a, p a → β} {x : option (option α)} (H) : (pmap (pmap f) x H).join = pmap f x.join (λ a h, H (some a) (mem_of_mem_join h) _ rfl) := by { rcases x with _ | _ | x; simp } end pmap @[simp] theorem seq_some {α β} {a : α} {f : α → β} : some f <*> some a = some (f a) := rfl @[simp] theorem some_orelse' (a : α) (x : option α) : (some a).orelse x = some a := rfl @[simp] theorem some_orelse (a : α) (x : option α) : (some a <|> x) = some a := rfl @[simp] theorem none_orelse' (x : option α) : none.orelse x = x := by cases x; refl @[simp] theorem none_orelse (x : option α) : (none <|> x) = x := none_orelse' x @[simp] theorem orelse_none' (x : option α) : x.orelse none = x := by cases x; refl @[simp] theorem orelse_none (x : option α) : (x <|> none) = x := orelse_none' x @[simp] theorem is_some_none : @is_some α none = ff := rfl @[simp] theorem is_some_some {a : α} : is_some (some a) = tt := rfl theorem is_some_iff_exists {x : option α} : is_some x ↔ ∃ a, x = some a := by cases x; simp [is_some]; exact ⟨_, rfl⟩ @[simp] theorem is_none_none : @is_none α none = tt := rfl @[simp] theorem is_none_some {a : α} : is_none (some a) = ff := rfl @[simp] theorem not_is_some {a : option α} : is_some a = ff ↔ a.is_none = tt := by cases a; simp lemma eq_some_iff_get_eq {o : option α} {a : α} : o = some a ↔ ∃ h : o.is_some, option.get h = a := by cases o; simp lemma not_is_some_iff_eq_none {o : option α} : ¬o.is_some ↔ o = none := by cases o; simp lemma ne_none_iff_is_some {o : option α} : o ≠ none ↔ o.is_some := by cases o; simp lemma ne_none_iff_exists {o : option α} : o ≠ none ↔ ∃ (x : α), some x = o := by {cases o; simp} lemma ne_none_iff_exists' {o : option α} : o ≠ none ↔ ∃ (x : α), o = some x := ne_none_iff_exists.trans $ exists_congr $ λ _, eq_comm lemma bex_ne_none {p : option α → Prop} : (∃ x ≠ none, p x) ↔ ∃ x, p (some x) := ⟨λ ⟨x, hx, hp⟩, ⟨get $ ne_none_iff_is_some.1 hx, by rwa [some_get]⟩, λ ⟨x, hx⟩, ⟨some x, some_ne_none x, hx⟩⟩ lemma ball_ne_none {p : option α → Prop} : (∀ x ≠ none, p x) ↔ ∀ x, p (some x) := ⟨λ h x, h (some x) (some_ne_none x), λ h x hx, by simpa only [some_get] using h (get $ ne_none_iff_is_some.1 hx)⟩ theorem iget_mem [inhabited α] : ∀ {o : option α}, is_some o → o.iget ∈ o | (some a) _ := rfl theorem iget_of_mem [inhabited α] {a : α} : ∀ {o : option α}, a ∈ o → o.iget = a | _ rfl := rfl @[simp] theorem guard_eq_some {p : α → Prop} [decidable_pred p] {a b : α} : guard p a = some b ↔ a = b ∧ p a := by by_cases p a; simp [option.guard, h]; intro; contradiction @[simp] theorem guard_eq_some' {p : Prop} [decidable p] : ∀ u, _root_.guard p = some u ↔ p | () := by by_cases p; simp [guard, h, pure]; intro; contradiction theorem lift_or_get_choice {f : α → α → α} (h : ∀ a b, f a b = a ∨ f a b = b) : ∀ o₁ o₂, lift_or_get f o₁ o₂ = o₁ ∨ lift_or_get f o₁ o₂ = o₂ | none none := or.inl rfl | (some a) none := or.inl rfl | none (some b) := or.inr rfl | (some a) (some b) := by simpa [lift_or_get] using h a b @[simp] lemma lift_or_get_none_left {f} {b : option α} : lift_or_get f none b = b := by cases b; refl @[simp] lemma lift_or_get_none_right {f} {a : option α} : lift_or_get f a none = a := by cases a; refl @[simp] lemma lift_or_get_some_some {f} {a b : α} : lift_or_get f (some a) (some b) = f a b := rfl /-- Given an element of `a : option α`, a default element `b : β` and a function `α → β`, apply this function to `a` if it comes from `α`, and return `b` otherwise. -/ def cases_on' : option α → β → (α → β) → β | none n s := n | (some a) n s := s a @[simp] lemma cases_on'_none (x : β) (f : α → β) : cases_on' none x f = x := rfl @[simp] lemma cases_on'_some (x : β) (f : α → β) (a : α) : cases_on' (some a) x f = f a := rfl @[simp] lemma cases_on'_coe (x : β) (f : α → β) (a : α) : cases_on' (a : option α) x f = f a := rfl @[simp] lemma cases_on'_none_coe (f : option α → β) (o : option α) : cases_on' o (f none) (f ∘ coe) = f o := by cases o; refl @[simp] lemma get_or_else_map (f : α → β) (x : α) (o : option α) : get_or_else (o.map f) (f x) = f (get_or_else o x) := by cases o; refl section open_locale classical /-- An arbitrary `some a` with `a : α` if `α` is nonempty, and otherwise `none`. -/ noncomputable def choice (α : Type*) : option α := if h : nonempty α then some h.some else none lemma choice_eq {α : Type*} [subsingleton α] (a : α) : choice α = some a := begin dsimp [choice], rw dif_pos (⟨a⟩ : nonempty α), congr, end lemma choice_eq_none (α : Type*) [is_empty α] : choice α = none := dif_neg (not_nonempty_iff_imp_false.mpr is_empty_elim) lemma choice_is_some_iff_nonempty {α : Type*} : (choice α).is_some ↔ nonempty α := begin fsplit, { intro h, exact ⟨option.get h⟩, }, { intro h, dsimp only [choice], rw dif_pos h, exact is_some_some }, end end @[simp] lemma to_list_some (a : α) : (a : option α).to_list = [a] := rfl @[simp] lemma to_list_none (α : Type*) : (none : option α).to_list = [] := rfl end option
ad32d78acc8245da19cefcf169439315dbb96d85
3863d2564418bccb1859e057bf5a4ef240e75fd7
/hott/cubical/square.hlean
901db85dca7aaca330bab2bbbb3658a0784ee949
[ "Apache-2.0" ]
permissive
JacobGross/lean
118bbb067ff4d4af48a266face2c7eb9868fa91c
eb26087df940c54337cb807b4bc6d345d1fc1085
refs/heads/master
1,582,735,011,532
1,462,557,826,000
1,462,557,826,000
46,451,196
0
0
null
1,462,557,826,000
1,447,885,161,000
C++
UTF-8
Lean
false
false
29,139
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, Jakob von Raumer Squares in a type -/ import types.eq open eq equiv is_equiv sigma namespace eq variables {A B : Type} {a a' a'' a₀₀ a₂₀ a₄₀ a₀₂ a₂₂ a₂₄ a₀₄ a₄₂ a₄₄ a₁ a₂ a₃ a₄ : A} /-a₀₀-/ {p₁₀ p₁₀' : a₀₀ = a₂₀} /-a₂₀-/ {p₃₀ : a₂₀ = a₄₀} /-a₄₀-/ {p₀₁ p₀₁' : a₀₀ = a₀₂} /-s₁₁-/ {p₂₁ p₂₁' : a₂₀ = a₂₂} /-s₃₁-/ {p₄₁ : a₄₀ = a₄₂} /-a₀₂-/ {p₁₂ p₁₂' : a₀₂ = a₂₂} /-a₂₂-/ {p₃₂ : a₂₂ = a₄₂} /-a₄₂-/ {p₀₃ : a₀₂ = a₀₄} /-s₁₃-/ {p₂₃ : a₂₂ = a₂₄} /-s₃₃-/ {p₄₃ : a₄₂ = a₄₄} /-a₀₄-/ {p₁₄ : a₀₄ = a₂₄} /-a₂₄-/ {p₃₄ : a₂₄ = a₄₄} /-a₄₄-/ inductive square {A : Type} {a₀₀ : A} : Π{a₂₀ a₀₂ a₂₂ : A}, a₀₀ = a₂₀ → a₀₂ = a₂₂ → a₀₀ = a₀₂ → a₂₀ = a₂₂ → Type := ids : square idp idp idp idp /- square top bottom left right -/ variables {s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁} {s₃₁ : square p₃₀ p₃₂ p₂₁ p₄₁} {s₁₃ : square p₁₂ p₁₄ p₀₃ p₂₃} {s₃₃ : square p₃₂ p₃₄ p₂₃ p₄₃} definition ids [reducible] [constructor] := @square.ids definition idsquare [reducible] [constructor] (a : A) := @square.ids A a definition hrefl [unfold 4] (p : a = a') : square idp idp p p := by induction p; exact ids definition vrefl [unfold 4] (p : a = a') : square p p idp idp := by induction p; exact ids definition hrfl [reducible] [unfold 4] {p : a = a'} : square idp idp p p := !hrefl definition vrfl [reducible] [unfold 4] {p : a = a'} : square p p idp idp := !vrefl definition hdeg_square [unfold 6] {p q : a = a'} (r : p = q) : square idp idp p q := by induction r;apply hrefl definition vdeg_square [unfold 6] {p q : a = a'} (r : p = q) : square p q idp idp := by induction r;apply vrefl definition hdeg_square_idp (p : a = a') : hdeg_square (refl p) = hrfl := by cases p; reflexivity definition vdeg_square_idp (p : a = a') : vdeg_square (refl p) = vrfl := by cases p; reflexivity definition hconcat [unfold 16] (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) (s₃₁ : square p₃₀ p₃₂ p₂₁ p₄₁) : square (p₁₀ ⬝ p₃₀) (p₁₂ ⬝ p₃₂) p₀₁ p₄₁ := by induction s₃₁; exact s₁₁ definition vconcat [unfold 16] (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) (s₁₃ : square p₁₂ p₁₄ p₀₃ p₂₃) : square p₁₀ p₁₄ (p₀₁ ⬝ p₀₃) (p₂₁ ⬝ p₂₃) := by induction s₁₃; exact s₁₁ definition hinverse [unfold 10] (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) : square p₁₀⁻¹ p₁₂⁻¹ p₂₁ p₀₁ := by induction s₁₁;exact ids definition vinverse [unfold 10] (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) : square p₁₂ p₁₀ p₀₁⁻¹ p₂₁⁻¹ := by induction s₁₁;exact ids definition eq_vconcat [unfold 11] {p : a₀₀ = a₂₀} (r : p = p₁₀) (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) : square p p₁₂ p₀₁ p₂₁ := by induction r; exact s₁₁ definition vconcat_eq [unfold 12] {p : a₀₂ = a₂₂} (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) (r : p₁₂ = p) : square p₁₀ p p₀₁ p₂₁ := by induction r; exact s₁₁ definition eq_hconcat [unfold 11] {p : a₀₀ = a₀₂} (r : p = p₀₁) (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) : square p₁₀ p₁₂ p p₂₁ := by induction r; exact s₁₁ definition hconcat_eq [unfold 12] {p : a₂₀ = a₂₂} (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) (r : p₂₁ = p) : square p₁₀ p₁₂ p₀₁ p := by induction r; exact s₁₁ infix ` ⬝h `:75 := hconcat --type using \tr infix ` ⬝v `:75 := vconcat --type using \tr infix ` ⬝hp `:75 := hconcat_eq --type using \tr infix ` ⬝vp `:75 := vconcat_eq --type using \tr infix ` ⬝ph `:75 := eq_hconcat --type using \tr infix ` ⬝pv `:75 := eq_vconcat --type using \tr postfix `⁻¹ʰ`:(max+1) := hinverse --type using \-1h postfix `⁻¹ᵛ`:(max+1) := vinverse --type using \-1v definition transpose [unfold 10] (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) : square p₀₁ p₂₁ p₁₀ p₁₂ := by induction s₁₁;exact ids definition aps [unfold 12] {B : Type} (f : A → B) (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) : square (ap f p₁₀) (ap f p₁₂) (ap f p₀₁) (ap f p₂₁) := by induction s₁₁;exact ids definition natural_square [unfold 8] {f g : A → B} (p : f ~ g) (q : a = a') : square (ap f q) (ap g q) (p a) (p a') := eq.rec_on q hrfl definition natural_square_tr [unfold 8] {f g : A → B} (p : f ~ g) (q : a = a') : square (p a) (p a') (ap f q) (ap g q) := eq.rec_on q vrfl /- canceling, whiskering and moving thinks along the sides of the square -/ definition whisker_tl (p : a = a₀₀) (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) : square (p ⬝ p₁₀) p₁₂ (p ⬝ p₀₁) p₂₁ := by induction s₁₁;induction p;constructor definition whisker_br (p : a₂₂ = a) (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) : square p₁₀ (p₁₂ ⬝ p) p₀₁ (p₂₁ ⬝ p) := by induction p;exact s₁₁ definition whisker_rt (p : a = a₂₀) (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) : square (p₁₀ ⬝ p⁻¹) p₁₂ p₀₁ (p ⬝ p₂₁) := by induction s₁₁;induction p;constructor definition whisker_tr (p : a₂₀ = a) (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) : square (p₁₀ ⬝ p) p₁₂ p₀₁ (p⁻¹ ⬝ p₂₁) := by induction s₁₁;induction p;constructor definition whisker_bl (p : a = a₀₂) (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) : square p₁₀ (p ⬝ p₁₂) (p₀₁ ⬝ p⁻¹) p₂₁ := by induction s₁₁;induction p;constructor definition whisker_lb (p : a₀₂ = a) (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) : square p₁₀ (p⁻¹ ⬝ p₁₂) (p₀₁ ⬝ p) p₂₁ := by induction s₁₁;induction p;constructor definition cancel_tl (p : a = a₀₀) (s₁₁ : square (p ⬝ p₁₀) p₁₂ (p ⬝ p₀₁) p₂₁) : square p₁₀ p₁₂ p₀₁ p₂₁ := by induction p; rewrite +idp_con at s₁₁; exact s₁₁ definition cancel_br (p : a₂₂ = a) (s₁₁ : square p₁₀ (p₁₂ ⬝ p) p₀₁ (p₂₁ ⬝ p)) : square p₁₀ p₁₂ p₀₁ p₂₁ := by induction p;exact s₁₁ definition cancel_rt (p : a = a₂₀) (s₁₁ : square (p₁₀ ⬝ p⁻¹) p₁₂ p₀₁ (p ⬝ p₂₁)) : square p₁₀ p₁₂ p₀₁ p₂₁ := by induction p; rewrite idp_con at s₁₁; exact s₁₁ definition cancel_tr (p : a₂₀ = a) (s₁₁ : square (p₁₀ ⬝ p) p₁₂ p₀₁ (p⁻¹ ⬝ p₂₁)) : square p₁₀ p₁₂ p₀₁ p₂₁ := by induction p; rewrite [▸* at s₁₁,idp_con at s₁₁]; exact s₁₁ definition cancel_bl (p : a = a₀₂) (s₁₁ : square p₁₀ (p ⬝ p₁₂) (p₀₁ ⬝ p⁻¹) p₂₁) : square p₁₀ p₁₂ p₀₁ p₂₁ := by induction p; rewrite idp_con at s₁₁; exact s₁₁ definition cancel_lb (p : a₀₂ = a) (s₁₁ : square p₁₀ (p⁻¹ ⬝ p₁₂) (p₀₁ ⬝ p) p₂₁) : square p₁₀ p₁₂ p₀₁ p₂₁ := by induction p; rewrite [▸* at s₁₁,idp_con at s₁₁]; exact s₁₁ definition move_top_of_left {p : a₀₀ = a} {q : a = a₀₂} (s : square p₁₀ p₁₂ (p ⬝ q) p₂₁) : square (p⁻¹ ⬝ p₁₀) p₁₂ q p₂₁ := by apply cancel_tl p; rewrite con_inv_cancel_left; exact s definition move_top_of_left' {p : a = a₀₀} {q : a = a₀₂} (s : square p₁₀ p₁₂ (p⁻¹ ⬝ q) p₂₁) : square (p ⬝ p₁₀) p₁₂ q p₂₁ := by apply cancel_tl p⁻¹; rewrite inv_con_cancel_left; exact s definition move_left_of_top {p : a₀₀ = a} {q : a = a₂₀} (s : square (p ⬝ q) p₁₂ p₀₁ p₂₁) : square q p₁₂ (p⁻¹ ⬝ p₀₁) p₂₁ := by apply cancel_tl p; rewrite con_inv_cancel_left; exact s definition move_left_of_top' {p : a = a₀₀} {q : a = a₂₀} (s : square (p⁻¹ ⬝ q) p₁₂ p₀₁ p₂₁) : square q p₁₂ (p ⬝ p₀₁) p₂₁ := by apply cancel_tl p⁻¹; rewrite inv_con_cancel_left; exact s definition move_bot_of_right {p : a₂₀ = a} {q : a = a₂₂} (s : square p₁₀ p₁₂ p₀₁ (p ⬝ q)) : square p₁₀ (p₁₂ ⬝ q⁻¹) p₀₁ p := by apply cancel_br q; rewrite inv_con_cancel_right; exact s definition move_bot_of_right' {p : a₂₀ = a} {q : a₂₂ = a} (s : square p₁₀ p₁₂ p₀₁ (p ⬝ q⁻¹)) : square p₁₀ (p₁₂ ⬝ q) p₀₁ p := by apply cancel_br q⁻¹; rewrite con_inv_cancel_right; exact s definition move_right_of_bot {p : a₀₂ = a} {q : a = a₂₂} (s : square p₁₀ (p ⬝ q) p₀₁ p₂₁) : square p₁₀ p p₀₁ (p₂₁ ⬝ q⁻¹) := by apply cancel_br q; rewrite inv_con_cancel_right; exact s definition move_right_of_bot' {p : a₀₂ = a} {q : a₂₂ = a} (s : square p₁₀ (p ⬝ q⁻¹) p₀₁ p₂₁) : square p₁₀ p p₀₁ (p₂₁ ⬝ q) := by apply cancel_br q⁻¹; rewrite con_inv_cancel_right; exact s definition move_top_of_right {p : a₂₀ = a} {q : a = a₂₂} (s : square p₁₀ p₁₂ p₀₁ (p ⬝ q)) : square (p₁₀ ⬝ p) p₁₂ p₀₁ q := by apply cancel_rt p; rewrite con_inv_cancel_right; exact s definition move_right_of_top {p : a₀₀ = a} {q : a = a₂₀} (s : square (p ⬝ q) p₁₂ p₀₁ p₂₁) : square p p₁₂ p₀₁ (q ⬝ p₂₁) := by apply cancel_tr q; rewrite inv_con_cancel_left; exact s definition move_bot_of_left {p : a₀₀ = a} {q : a = a₀₂} (s : square p₁₀ p₁₂ (p ⬝ q) p₂₁) : square p₁₀ (q ⬝ p₁₂) p p₂₁ := by apply cancel_lb q; rewrite inv_con_cancel_left; exact s definition move_left_of_bot {p : a₀₂ = a} {q : a = a₂₂} (s : square p₁₀ (p ⬝ q) p₀₁ p₂₁) : square p₁₀ q (p₀₁ ⬝ p) p₂₁ := by apply cancel_bl p; rewrite con_inv_cancel_right; exact s /- some higher ∞-groupoid operations -/ definition vconcat_vrfl (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) : s₁₁ ⬝v vrefl p₁₂ = s₁₁ := by induction s₁₁; reflexivity definition hconcat_hrfl (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) : s₁₁ ⬝h hrefl p₂₁ = s₁₁ := by induction s₁₁; reflexivity /- equivalences -/ definition eq_of_square [unfold 10] (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) : p₁₀ ⬝ p₂₁ = p₀₁ ⬝ p₁₂ := by induction s₁₁; apply idp definition square_of_eq (r : p₁₀ ⬝ p₂₁ = p₀₁ ⬝ p₁₂) : square p₁₀ p₁₂ p₀₁ p₂₁ := by induction p₁₂; esimp at r; induction r; induction p₂₁; induction p₁₀; exact ids definition eq_top_of_square [unfold 10] (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) : p₁₀ = p₀₁ ⬝ p₁₂ ⬝ p₂₁⁻¹ := by induction s₁₁; apply idp definition square_of_eq_top (r : p₁₀ = p₀₁ ⬝ p₁₂ ⬝ p₂₁⁻¹) : square p₁₀ p₁₂ p₀₁ p₂₁ := by induction p₂₁; induction p₁₂; esimp at r;induction r;induction p₁₀;exact ids definition eq_bot_of_square [unfold 10] (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) : p₁₂ = p₀₁⁻¹ ⬝ p₁₀ ⬝ p₂₁ := by induction s₁₁; apply idp definition square_of_eq_bot (r : p₀₁⁻¹ ⬝ p₁₀ ⬝ p₂₁ = p₁₂) : square p₁₀ p₁₂ p₀₁ p₂₁ := by induction p₂₁; induction p₁₀; esimp at r; induction r; induction p₀₁; exact ids definition square_equiv_eq [constructor] (t : a₀₀ = a₀₂) (b : a₂₀ = a₂₂) (l : a₀₀ = a₂₀) (r : a₀₂ = a₂₂) : square t b l r ≃ t ⬝ r = l ⬝ b := begin fapply equiv.MK, { exact eq_of_square}, { exact square_of_eq}, { intro s, induction b, esimp [concat] at s, induction s, induction r, induction t, apply idp}, { intro s, induction s, apply idp}, end definition hdeg_square_equiv' [constructor] (p q : a = a') : square idp idp p q ≃ p = q := by transitivity _;apply square_equiv_eq;transitivity _;apply eq_equiv_eq_symm; apply equiv_eq_closed_right;apply idp_con definition vdeg_square_equiv' [constructor] (p q : a = a') : square p q idp idp ≃ p = q := by transitivity _;apply square_equiv_eq;apply equiv_eq_closed_right; apply idp_con definition eq_of_hdeg_square [reducible] {p q : a = a'} (s : square idp idp p q) : p = q := to_fun !hdeg_square_equiv' s definition eq_of_vdeg_square [reducible] {p q : a = a'} (s : square p q idp idp) : p = q := to_fun !vdeg_square_equiv' s definition top_deg_square (l : a₁ = a₂) (b : a₂ = a₃) (r : a₄ = a₃) : square (l ⬝ b ⬝ r⁻¹) b l r := by induction r;induction b;induction l;constructor definition bot_deg_square (l : a₁ = a₂) (t : a₁ = a₃) (r : a₃ = a₄) : square t (l⁻¹ ⬝ t ⬝ r) l r := by induction r;induction t;induction l;constructor /- the following two equivalences have as underlying inverse function the functions hdeg_square and vdeg_square, respectively. See example below the definition -/ definition hdeg_square_equiv [constructor] (p q : a = a') : square idp idp p q ≃ p = q := begin fapply equiv_change_fun, { fapply equiv_change_inv, apply hdeg_square_equiv', exact hdeg_square, intro s, induction s, induction p, reflexivity}, { exact eq_of_hdeg_square}, { reflexivity} end definition vdeg_square_equiv [constructor] (p q : a = a') : square p q idp idp ≃ p = q := begin fapply equiv_change_fun, { fapply equiv_change_inv, apply vdeg_square_equiv',exact vdeg_square, intro s, induction s, induction p, reflexivity}, { exact eq_of_vdeg_square}, { reflexivity} end example (p q : a = a') : to_inv (hdeg_square_equiv p q) = hdeg_square := idp /- characterization of pathovers in a equality type. The type B of the equality is fixed here. A version where B may also varies over the path p is given in the file squareover -/ definition eq_pathover [unfold 7] {f g : A → B} {p : a = a'} {q : f a = g a} {r : f a' = g a'} (s : square q r (ap f p) (ap g p)) : q =[p] r := by induction p;apply pathover_idp_of_eq;exact eq_of_vdeg_square s definition eq_pathover_constant_left {g : A → B} {p : a = a'} {b : B} {q : b = g a} {r : b = g a'} (s : square q r idp (ap g p)) : q =[p] r := eq_pathover (ap_constant p b ⬝ph s) definition eq_pathover_id_left {g : A → A} {p : a = a'} {q : a = g a} {r : a' = g a'} (s : square q r p (ap g p)) : q =[p] r := eq_pathover (ap_id p ⬝ph s) definition eq_pathover_constant_right {f : A → B} {p : a = a'} {b : B} {q : f a = b} {r : f a' = b} (s : square q r (ap f p) idp) : q =[p] r := eq_pathover (s ⬝hp (ap_constant p b)⁻¹) definition eq_pathover_id_right {f : A → A} {p : a = a'} {q : f a = a} {r : f a' = a'} (s : square q r (ap f p) p) : q =[p] r := eq_pathover (s ⬝hp (ap_id p)⁻¹) definition square_of_pathover [unfold 7] {f g : A → B} {p : a = a'} {q : f a = g a} {r : f a' = g a'} (s : q =[p] r) : square q r (ap f p) (ap g p) := by induction p;apply vdeg_square;exact eq_of_pathover_idp s definition eq_pathover_constant_left_id_right {p : a = a'} {a₀ : A} {q : a₀ = a} {r : a₀ = a'} (s : square q r idp p) : q =[p] r := eq_pathover (ap_constant p a₀ ⬝ph s ⬝hp (ap_id p)⁻¹) definition eq_pathover_id_left_constant_right {p : a = a'} {a₀ : A} {q : a = a₀} {r : a' = a₀} (s : square q r p idp) : q =[p] r := eq_pathover (ap_id p ⬝ph s ⬝hp (ap_constant p a₀)⁻¹) definition loop_pathover {p : a = a'} {b : B} {q : a = a} {r : a' = a'} (s : square q r p p) : q =[p] r := eq_pathover (ap_id p ⬝ph s ⬝hp (ap_id p)⁻¹) /- interaction of equivalences with operations on squares -/ definition eq_pathover_equiv_square [constructor] {f g : A → B} (p : a = a') (q : f a = g a) (r : f a' = g a') : q =[p] r ≃ square q r (ap f p) (ap g p) := equiv.MK square_of_pathover eq_pathover begin intro s, induction p, esimp [square_of_pathover,eq_pathover], exact ap vdeg_square (to_right_inv !pathover_idp (eq_of_vdeg_square s)) ⬝ to_left_inv !vdeg_square_equiv s end begin intro s, induction p, esimp [square_of_pathover,eq_pathover], exact ap pathover_idp_of_eq (to_right_inv !vdeg_square_equiv (eq_of_pathover_idp s)) ⬝ to_left_inv !pathover_idp s end definition square_of_pathover_eq_concato {f g : A → B} {p : a = a'} {q q' : f a = g a} {r : f a' = g a'} (s' : q = q') (s : q' =[p] r) : square_of_pathover (s' ⬝po s) = s' ⬝pv square_of_pathover s := by induction s;induction s';reflexivity definition square_of_pathover_concato_eq {f g : A → B} {p : a = a'} {q : f a = g a} {r r' : f a' = g a'} (s' : r = r') (s : q =[p] r) : square_of_pathover (s ⬝op s') = square_of_pathover s ⬝vp s' := by induction s;induction s';reflexivity definition square_of_pathover_concato {f g : A → B} {p : a = a'} {p' : a' = a''} {q : f a = g a} {q' : f a' = g a'} {q'' : f a'' = g a''} (s : q =[p] q') (s' : q' =[p'] q'') : square_of_pathover (s ⬝o s') = ap_con f p p' ⬝ph (square_of_pathover s ⬝v square_of_pathover s') ⬝hp (ap_con g p p')⁻¹ := by induction s';induction s;esimp [ap_con,hconcat_eq];exact !vconcat_vrfl⁻¹ definition eq_of_square_hrfl [unfold 4] (p : a = a') : eq_of_square hrfl = idp_con p := by induction p;reflexivity definition eq_of_square_vrfl [unfold 4] (p : a = a') : eq_of_square vrfl = (idp_con p)⁻¹ := by induction p;reflexivity definition eq_of_square_hdeg_square {p q : a = a'} (r : p = q) : eq_of_square (hdeg_square r) = !idp_con ⬝ r⁻¹ := by induction r;induction p;reflexivity definition eq_of_square_vdeg_square {p q : a = a'} (r : p = q) : eq_of_square (vdeg_square r) = r ⬝ !idp_con⁻¹ := by induction r;induction p;reflexivity definition eq_of_square_eq_vconcat {p : a₀₀ = a₂₀} (r : p = p₁₀) (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) : eq_of_square (r ⬝pv s₁₁) = whisker_right r p₂₁ ⬝ eq_of_square s₁₁ := by induction s₁₁;cases r;reflexivity definition eq_of_square_eq_hconcat {p : a₀₀ = a₀₂} (r : p = p₀₁) (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) : eq_of_square (r ⬝ph s₁₁) = eq_of_square s₁₁ ⬝ (whisker_right r p₁₂)⁻¹ := by induction r;reflexivity definition eq_of_square_vconcat_eq {p : a₀₂ = a₂₂} (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) (r : p₁₂ = p) : eq_of_square (s₁₁ ⬝vp r) = eq_of_square s₁₁ ⬝ whisker_left p₀₁ r := by induction r;reflexivity definition eq_of_square_hconcat_eq {p : a₂₀ = a₂₂} (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) (r : p₂₁ = p) : eq_of_square (s₁₁ ⬝hp r) = (whisker_left p₁₀ r)⁻¹ ⬝ eq_of_square s₁₁ := by induction s₁₁; induction r;reflexivity -- definition vconcat_eq [unfold 11] {p : a₀₂ = a₂₂} (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) (r : p₁₂ = p) : -- square p₁₀ p p₀₁ p₂₁ := -- by induction r; exact s₁₁ -- definition eq_hconcat [unfold 11] {p : a₀₀ = a₀₂} (r : p = p₀₁) -- (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) : square p₁₀ p₁₂ p p₂₁ := -- by induction r; exact s₁₁ -- definition hconcat_eq [unfold 11] {p : a₂₀ = a₂₂} -- (s₁₁ : square p₁₀ p₁₂ p₀₁ p₂₁) (r : p₂₁ = p) : square p₁₀ p₁₂ p₀₁ p := -- by induction r; exact s₁₁ -- the following definition is very slow, maybe it's interesting to see why? -- definition eq_pathover_equiv_square' {f g : A → B}(p : a = a') (q : f a = g a) (r : f a' = g a') -- : square q r (ap f p) (ap g p) ≃ q =[p] r := -- equiv.MK eq_pathover -- square_of_pathover -- (λs, begin -- induction p, rewrite [↑[square_of_pathover,eq_pathover], -- to_right_inv !vdeg_square_equiv (eq_of_pathover_idp s), -- to_left_inv !pathover_idp s] -- end) -- (λs, begin -- induction p, rewrite [↑[square_of_pathover,eq_pathover],▸*, -- to_right_inv !(@pathover_idp A) (eq_of_vdeg_square s), -- to_left_inv !vdeg_square_equiv s] -- end) /- recursors for squares where some sides are reflexivity -/ definition rec_on_b [recursor] {a₀₀ : A} {P : Π{a₂₀ a₁₂ : A} {t : a₀₀ = a₂₀} {l : a₀₀ = a₁₂} {r : a₂₀ = a₁₂}, square t idp l r → Type} {a₂₀ a₁₂ : A} {t : a₀₀ = a₂₀} {l : a₀₀ = a₁₂} {r : a₂₀ = a₁₂} (s : square t idp l r) (H : P ids) : P s := have H2 : P (square_of_eq (eq_of_square s)), from eq.rec_on (eq_of_square s : t ⬝ r = l) (by induction r; induction t; exact H), left_inv (to_fun !square_equiv_eq) s ▸ H2 definition rec_on_r [recursor] {a₀₀ : A} {P : Π{a₀₂ a₂₁ : A} {t : a₀₀ = a₂₁} {b : a₀₂ = a₂₁} {l : a₀₀ = a₀₂}, square t b l idp → Type} {a₀₂ a₂₁ : A} {t : a₀₀ = a₂₁} {b : a₀₂ = a₂₁} {l : a₀₀ = a₀₂} (s : square t b l idp) (H : P ids) : P s := let p : l ⬝ b = t := (eq_of_square s)⁻¹ in have H2 : P (square_of_eq (eq_of_square s)⁻¹⁻¹), from @eq.rec_on _ _ (λx p, P (square_of_eq p⁻¹)) _ p (by induction b; induction l; exact H), left_inv (to_fun !square_equiv_eq) s ▸ !inv_inv ▸ H2 definition rec_on_l [recursor] {a₀₁ : A} {P : Π {a₂₀ a₂₂ : A} {t : a₀₁ = a₂₀} {b : a₀₁ = a₂₂} {r : a₂₀ = a₂₂}, square t b idp r → Type} {a₂₀ a₂₂ : A} {t : a₀₁ = a₂₀} {b : a₀₁ = a₂₂} {r : a₂₀ = a₂₂} (s : square t b idp r) (H : P ids) : P s := let p : t ⬝ r = b := eq_of_square s ⬝ !idp_con in have H2 : P (square_of_eq (p ⬝ !idp_con⁻¹)), from eq.rec_on p (by induction r; induction t; exact H), left_inv (to_fun !square_equiv_eq) s ▸ !con_inv_cancel_right ▸ H2 definition rec_on_t [recursor] {a₁₀ : A} {P : Π {a₀₂ a₂₂ : A} {b : a₀₂ = a₂₂} {l : a₁₀ = a₀₂} {r : a₁₀ = a₂₂}, square idp b l r → Type} {a₀₂ a₂₂ : A} {b : a₀₂ = a₂₂} {l : a₁₀ = a₀₂} {r : a₁₀ = a₂₂} (s : square idp b l r) (H : P ids) : P s := let p : l ⬝ b = r := (eq_of_square s)⁻¹ ⬝ !idp_con in have H2 : P (square_of_eq ((p ⬝ !idp_con⁻¹)⁻¹)), from eq.rec_on p (by induction b; induction l; exact H), have H3 : P (square_of_eq ((eq_of_square s)⁻¹⁻¹)), from eq.rec_on !con_inv_cancel_right H2, have H4 : P (square_of_eq (eq_of_square s)), from eq.rec_on !inv_inv H3, proof left_inv (to_fun !square_equiv_eq) s ▸ H4 qed definition rec_on_tb [recursor] {a : A} {P : Π{b : A} {l : a = b} {r : a = b}, square idp idp l r → Type} {b : A} {l : a = b} {r : a = b} (s : square idp idp l r) (H : P ids) : P s := have H2 : P (square_of_eq (eq_of_square s)), from eq.rec_on (eq_of_square s : idp ⬝ r = l) (by induction r; exact H), left_inv (to_fun !square_equiv_eq) s ▸ H2 definition rec_on_lr [recursor] {a : A} {P : Π{a' : A} {t : a = a'} {b : a = a'}, square t b idp idp → Type} {a' : A} {t : a = a'} {b : a = a'} (s : square t b idp idp) (H : P ids) : P s := let p : idp ⬝ b = t := (eq_of_square s)⁻¹ in have H2 : P (square_of_eq (eq_of_square s)⁻¹⁻¹), from @eq.rec_on _ _ (λx q, P (square_of_eq q⁻¹)) _ p (by induction b; exact H), to_left_inv (!square_equiv_eq) s ▸ !inv_inv ▸ H2 --we can also do the other recursors (tl, tr, bl, br, tbl, tbr, tlr, blr), but let's postpone this until they are needed definition whisker_square [unfold 14 15 16 17] (r₁₀ : p₁₀ = p₁₀') (r₁₂ : p₁₂ = p₁₂') (r₀₁ : p₀₁ = p₀₁') (r₂₁ : p₂₁ = p₂₁') (s : square p₁₀ p₁₂ p₀₁ p₂₁) : square p₁₀' p₁₂' p₀₁' p₂₁' := by induction r₁₀; induction r₁₂; induction r₀₁; induction r₂₁; exact s /- squares commute with some operations on 2-paths -/ definition square_inv2 {p₁ p₂ p₃ p₄ : a = a'} {t : p₁ = p₂} {b : p₃ = p₄} {l : p₁ = p₃} {r : p₂ = p₄} (s : square t b l r) : square (inverse2 t) (inverse2 b) (inverse2 l) (inverse2 r) := by induction s;constructor definition square_con2 {p₁ p₂ p₃ p₄ : a₁ = a₂} {q₁ q₂ q₃ q₄ : a₂ = a₃} {t₁ : p₁ = p₂} {b₁ : p₃ = p₄} {l₁ : p₁ = p₃} {r₁ : p₂ = p₄} {t₂ : q₁ = q₂} {b₂ : q₃ = q₄} {l₂ : q₁ = q₃} {r₂ : q₂ = q₄} (s₁ : square t₁ b₁ l₁ r₁) (s₂ : square t₂ b₂ l₂ r₂) : square (t₁ ◾ t₂) (b₁ ◾ b₂) (l₁ ◾ l₂) (r₁ ◾ r₂) := by induction s₂;induction s₁;constructor open is_trunc definition is_set.elims [H : is_set A] : square p₁₀ p₁₂ p₀₁ p₂₁ := square_of_eq !is_set.elim definition is_trunc_square [instance] (n : trunc_index) [H : is_trunc n .+2 A] : is_trunc n (square p₁₀ p₁₂ p₀₁ p₂₁) := is_trunc_equiv_closed_rev n !square_equiv_eq -- definition square_of_con_inv_hsquare {p₁ p₂ p₃ p₄ : a₁ = a₂} -- {t : p₁ = p₂} {b : p₃ = p₄} {l : p₁ = p₃} {r : p₂ = p₄} -- (s : square (con_inv_eq_idp t) (con_inv_eq_idp b) (l ◾ r⁻²) idp) -- : square t b l r := -- sorry --by induction s /- Square fillers -/ -- TODO replace by "more algebraic" fillers? variables (p₁₀ p₁₂ p₀₁ p₂₁) definition square_fill_t : Σ (p : a₀₀ = a₂₀), square p p₁₂ p₀₁ p₂₁ := by induction p₀₁; induction p₂₁; exact ⟨_, !vrefl⟩ definition square_fill_b : Σ (p : a₀₂ = a₂₂), square p₁₀ p p₀₁ p₂₁ := by induction p₀₁; induction p₂₁; exact ⟨_, !vrefl⟩ definition square_fill_l : Σ (p : a₀₀ = a₀₂), square p₁₀ p₁₂ p p₂₁ := by induction p₁₀; induction p₁₂; exact ⟨_, !hrefl⟩ definition square_fill_r : Σ (p : a₂₀ = a₂₂) , square p₁₀ p₁₂ p₀₁ p := by induction p₁₀; induction p₁₂; exact ⟨_, !hrefl⟩ /- Squares having an 'ap' term on one face -/ --TODO find better names definition square_Flr_ap_idp {A B : Type} {c : B} {f : A → B} (p : Π a, f a = c) {a b : A} (q : a = b) : square (p a) (p b) (ap f q) idp := by induction q; apply vrfl definition square_Flr_idp_ap {A B : Type} {c : B} {f : A → B} (p : Π a, c = f a) {a b : A} (q : a = b) : square (p a) (p b) idp (ap f q) := by induction q; apply vrfl definition square_ap_idp_Flr {A B : Type} {b : B} {f : A → B} (p : Π a, f a = b) {a b : A} (q : a = b) : square (ap f q) idp (p a) (p b) := by induction q; apply hrfl /- Matching eq_hconcat with hconcat etc. -/ -- TODO maybe rename hconcat_eq and the like? variable (s₁₁) definition ph_eq_pv_h_vp {p : a₀₀ = a₀₂} (r : p = p₀₁) : r ⬝ph s₁₁ = !idp_con⁻¹ ⬝pv ((hdeg_square r) ⬝h s₁₁) ⬝vp !idp_con := by cases r; cases s₁₁; esimp definition hdeg_h_eq_pv_ph_vp {p : a₀₀ = a₀₂} (r : p = p₀₁) : hdeg_square r ⬝h s₁₁ = !idp_con ⬝pv (r ⬝ph s₁₁) ⬝vp !idp_con⁻¹ := by cases r; cases s₁₁; esimp definition hp_eq_h {p : a₂₀ = a₂₂} (r : p₂₁ = p) : s₁₁ ⬝hp r = s₁₁ ⬝h hdeg_square r := by cases r; cases s₁₁; esimp definition pv_eq_ph_vdeg_v_vh {p : a₀₀ = a₂₀} (r : p = p₁₀) : r ⬝pv s₁₁ = !idp_con⁻¹ ⬝ph ((vdeg_square r) ⬝v s₁₁) ⬝hp !idp_con := by cases r; cases s₁₁; esimp definition vdeg_v_eq_ph_pv_hp {p : a₀₀ = a₂₀} (r : p = p₁₀) : vdeg_square r ⬝v s₁₁ = !idp_con ⬝ph (r ⬝pv s₁₁) ⬝hp !idp_con⁻¹ := by cases r; cases s₁₁; esimp definition vp_eq_v {p : a₀₂ = a₂₂} (r : p₁₂ = p) : s₁₁ ⬝vp r = s₁₁ ⬝v vdeg_square r := by cases r; cases s₁₁; esimp end eq
64751eb2416240d87f90ddd51c5ab3bfe4a769c4
367134ba5a65885e863bdc4507601606690974c1
/src/group_theory/perm/cycles.lean
b0934c36cbc37a044de195fa8e99408c3a6532be
[ "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
16,005
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 group_theory.perm.sign /-! # Cyclic permutations ## Main definitions In the following, `f : equiv.perm β`. * `equiv.perm.is_cycle`: `f.is_cycle` when two nonfixed points of `β` are related by repeated application of `f`. * `equiv.perm.same_cycle`: `f.same_cycle x y` when `x` and `y` are in the same cycle of `f`. The following two definitions require that `β` is a `fintype`: * `equiv.perm.cycle_of`: `f.cycle_of x` is the cycle of `f` that `x` belongs to. * `equiv.perm.cycle_factors`: `f.cycle_factors` is a list of disjoint cyclic permutations that multiply to `f`. -/ namespace equiv.perm open equiv function finset variables {α : Type*} {β : Type*} [decidable_eq α] section sign_cycle /-! ### `is_cycle` -/ variables [fintype α] /-- A permutation is a cycle when any two nonfixed points of the permutation are related by repeated application of the permutation. -/ def is_cycle (f : perm β) : Prop := ∃ x, f x ≠ x ∧ ∀ y, f y ≠ y → ∃ i : ℤ, (f ^ i) x = y lemma is_cycle.swap {α : Type*} [decidable_eq α] {x y : α} (hxy : x ≠ y) : is_cycle (swap x y) := ⟨y, by rwa swap_apply_right, λ a (ha : ite (a = x) y (ite (a = y) x a) ≠ a), if hya : y = a then ⟨0, hya⟩ else ⟨1, by { rw [gpow_one, swap_apply_def], split_ifs at *; cc }⟩⟩ lemma is_cycle.inv {f : perm β} (hf : is_cycle f) : is_cycle (f⁻¹) := let ⟨x, hx⟩ := hf in ⟨x, by { simp only [inv_eq_iff_eq, *, forall_prop_of_true, ne.def] at *, cc }, λ y hy, let ⟨i, hi⟩ := hx.2 y (by { simp only [inv_eq_iff_eq, *, forall_prop_of_true, ne.def] at *, cc }) in ⟨-i, by rwa [gpow_neg, inv_gpow, inv_inv]⟩⟩ lemma is_cycle.exists_gpow_eq {f : perm β} (hf : is_cycle f) {x y : β} (hx : f x ≠ x) (hy : f y ≠ y) : ∃ i : ℤ, (f ^ i) x = y := let ⟨g, hg⟩ := hf in let ⟨a, ha⟩ := hg.2 x hx in let ⟨b, hb⟩ := hg.2 y hy in ⟨b - a, by rw [← ha, ← mul_apply, ← gpow_add, sub_add_cancel, hb]⟩ lemma is_cycle.exists_pow_eq [fintype β] {f : perm β} (hf : is_cycle f) {x y : β} (hx : f x ≠ x) (hy : f y ≠ y) : ∃ i : ℕ, (f ^ i) x = y := let ⟨n, hn⟩ := hf.exists_gpow_eq hx hy in by classical; exact ⟨(n % order_of f).to_nat, by { have := n.mod_nonneg (int.coe_nat_ne_zero.mpr (ne_of_gt (order_of_pos f))), rwa [← gpow_coe_nat, int.to_nat_of_nonneg this, ← gpow_eq_mod_order_of] }⟩ lemma is_cycle_swap_mul_aux₁ {α : Type*} [decidable_eq α] : ∀ (n : ℕ) {b x : α} {f : perm α} (hb : (swap x (f x) * f) b ≠ b) (h : (f ^ n) (f x) = b), ∃ i : ℤ, ((swap x (f x) * f) ^ i) (f x) = b | 0 := λ b x f hb h, ⟨0, h⟩ | (n+1 : ℕ) := λ b x f hb h, if hfbx : f x = b then ⟨0, hfbx⟩ else have f b ≠ b ∧ b ≠ x, from ne_and_ne_of_swap_mul_apply_ne_self hb, have hb' : (swap x (f x) * f) (f⁻¹ b) ≠ f⁻¹ b, by { rw [mul_apply, apply_inv_self, swap_apply_of_ne_of_ne this.2 (ne.symm hfbx), ne.def, ← f.injective.eq_iff, apply_inv_self], exact this.1 }, let ⟨i, hi⟩ := is_cycle_swap_mul_aux₁ n hb' (f.injective $ by { rw [apply_inv_self], rwa [pow_succ, mul_apply] at h }) in ⟨i + 1, by rw [add_comm, gpow_add, mul_apply, hi, gpow_one, mul_apply, apply_inv_self, swap_apply_of_ne_of_ne (ne_and_ne_of_swap_mul_apply_ne_self hb).2 (ne.symm hfbx)]⟩ lemma is_cycle_swap_mul_aux₂ {α : Type*} [decidable_eq α] : ∀ (n : ℤ) {b x : α} {f : perm α} (hb : (swap x (f x) * f) b ≠ b) (h : (f ^ n) (f x) = b), ∃ i : ℤ, ((swap x (f x) * f) ^ i) (f x) = b | (n : ℕ) := λ b x f, is_cycle_swap_mul_aux₁ n | -[1+ n] := λ b x f hb h, if hfbx : f⁻¹ x = b then ⟨-1, by rwa [gpow_neg, gpow_one, mul_inv_rev, mul_apply, swap_inv, swap_apply_right]⟩ else if hfbx' : f x = b then ⟨0, hfbx'⟩ else have f b ≠ b ∧ b ≠ x := ne_and_ne_of_swap_mul_apply_ne_self hb, have hb : (swap x (f⁻¹ x) * f⁻¹) (f⁻¹ b) ≠ f⁻¹ b, by { rw [mul_apply, swap_apply_def], split_ifs; simp only [inv_eq_iff_eq, perm.mul_apply, gpow_neg_succ_of_nat, ne.def, perm.apply_inv_self] at *; cc }, let ⟨i, hi⟩ := is_cycle_swap_mul_aux₁ n hb (show (f⁻¹ ^ n) (f⁻¹ x) = f⁻¹ b, by rw [← gpow_coe_nat, ← h, ← mul_apply, ← mul_apply, ← mul_apply, gpow_neg_succ_of_nat, ← inv_pow, pow_succ', mul_assoc, mul_assoc, inv_mul_self, mul_one, gpow_coe_nat, ← pow_succ', ← pow_succ]) in have h : (swap x (f⁻¹ x) * f⁻¹) (f x) = f⁻¹ x, by rw [mul_apply, inv_apply_self, swap_apply_left], ⟨-i, by rw [← add_sub_cancel i 1, neg_sub, sub_eq_add_neg, gpow_add, gpow_one, gpow_neg, ← inv_gpow, mul_inv_rev, swap_inv, mul_swap_eq_swap_mul, inv_apply_self, swap_comm _ x, gpow_add, gpow_one, mul_apply, mul_apply (_ ^ i), h, hi, mul_apply, apply_inv_self, swap_apply_of_ne_of_ne this.2 (ne.symm hfbx')]⟩ lemma is_cycle.eq_swap_of_apply_apply_eq_self {α : Type*} [decidable_eq α] {f : perm α} (hf : is_cycle f) {x : α} (hfx : f x ≠ x) (hffx : f (f x) = x) : f = swap x (f x) := equiv.ext $ λ y, let ⟨z, hz⟩ := hf in let ⟨i, hi⟩ := hz.2 x hfx in if hyx : y = x then by simp [hyx] else if hfyx : y = f x then by simp [hfyx, hffx] else begin rw [swap_apply_of_ne_of_ne hyx hfyx], refine by_contradiction (λ hy, _), cases hz.2 y hy with j hj, rw [← sub_add_cancel j i, gpow_add, mul_apply, hi] at hj, cases gpow_apply_eq_of_apply_apply_eq_self hffx (j - i) with hji hji, { rw [← hj, hji] at hyx, cc }, { rw [← hj, hji] at hfyx, cc } end lemma is_cycle.swap_mul {α : Type*} [decidable_eq α] {f : perm α} (hf : is_cycle f) {x : α} (hx : f x ≠ x) (hffx : f (f x) ≠ x) : is_cycle (swap x (f x) * f) := ⟨f x, by { simp only [swap_apply_def, mul_apply], split_ifs; simp [f.injective.eq_iff] at *; cc }, λ y hy, let ⟨i, hi⟩ := hf.exists_gpow_eq hx (ne_and_ne_of_swap_mul_apply_ne_self hy).1 in have hi : (f ^ (i - 1)) (f x) = y, from calc (f ^ (i - 1)) (f x) = (f ^ (i - 1) * f ^ (1 : ℤ)) x : by rw [gpow_one, mul_apply] ... = y : by rwa [← gpow_add, sub_add_cancel], is_cycle_swap_mul_aux₂ (i - 1) hy hi⟩ lemma is_cycle.sign : ∀ {f : perm α} (hf : is_cycle f), sign f = -(-1) ^ f.support.card | f := λ hf, let ⟨x, hx⟩ := hf in calc sign f = sign (swap x (f x) * (swap x (f x) * f)) : by rw [← mul_assoc, mul_def, mul_def, swap_swap, trans_refl] ... = -(-1) ^ f.support.card : if h1 : f (f x) = x then have h : swap x (f x) * f = 1, begin rw hf.eq_swap_of_apply_apply_eq_self hx.1 h1, simp only [perm.mul_def, perm.one_def, swap_apply_left, swap_swap] end, by { rw [sign_mul, sign_swap hx.1.symm, h, sign_one, hf.eq_swap_of_apply_apply_eq_self hx.1 h1, card_support_swap hx.1.symm], refl } else have h : card (support (swap x (f x) * f)) + 1 = card (support f), by rw [← insert_erase (mem_support.2 hx.1), support_swap_mul_eq h1, card_insert_of_not_mem (not_mem_erase _ _)], have wf : card (support (swap x (f x) * f)) < card (support f), from card_support_swap_mul hx.1, by { rw [sign_mul, sign_swap hx.1.symm, (hf.swap_mul hx.1 h1).sign, ← h], simp only [pow_add, mul_one, units.neg_neg, one_mul, units.mul_neg, eq_self_iff_true, pow_one, units.neg_mul_neg] } using_well_founded {rel_tac := λ _ _, `[exact ⟨_, measure_wf (λ f, f.support.card)⟩]} end sign_cycle /-! ### `same_cycle` -/ /-- The equivalence relation indicating that two points are in the same cycle of a permutation. -/ def same_cycle (f : perm β) (x y : β) : Prop := ∃ i : ℤ, (f ^ i) x = y @[refl] lemma same_cycle.refl (f : perm β) (x : β) : same_cycle f x x := ⟨0, rfl⟩ @[symm] lemma same_cycle.symm (f : perm β) {x y : β} : same_cycle f x y → same_cycle f y x := λ ⟨i, hi⟩, ⟨-i, by rw [gpow_neg, ← hi, inv_apply_self]⟩ @[trans] lemma same_cycle.trans (f : perm β) {x y z : β} : same_cycle f x y → same_cycle f y z → same_cycle f x z := λ ⟨i, hi⟩ ⟨j, hj⟩, ⟨j + i, by rw [gpow_add, mul_apply, hi, hj]⟩ lemma same_cycle.apply_eq_self_iff {f : perm β} {x y : β} : same_cycle f x y → (f x = x ↔ f y = y) := λ ⟨i, hi⟩, by rw [← hi, ← mul_apply, ← gpow_one_add, add_comm, gpow_add_one, mul_apply, (f ^ i).injective.eq_iff] lemma is_cycle.same_cycle {f : perm β} (hf : is_cycle f) {x y : β} (hx : f x ≠ x) (hy : f y ≠ y) : same_cycle f x y := hf.exists_gpow_eq hx hy instance [fintype α] (f : perm α) : decidable_rel (same_cycle f) := λ x y, decidable_of_iff (∃ n ∈ list.range (order_of f), (f ^ n) x = y) ⟨λ ⟨n, _, hn⟩, ⟨n, hn⟩, λ ⟨i, hi⟩, ⟨(i % order_of f).nat_abs, list.mem_range.2 (int.coe_nat_lt.1 $ by { rw int.nat_abs_of_nonneg (int.mod_nonneg _ (int.coe_nat_ne_zero_iff_pos.2 (order_of_pos _))), calc _ < _ : int.mod_lt _ (int.coe_nat_ne_zero_iff_pos.2 (order_of_pos _)) ... = _ : by simp }), by rw [← gpow_coe_nat, int.nat_abs_of_nonneg (int.mod_nonneg _ (int.coe_nat_ne_zero_iff_pos.2 (order_of_pos _))), ← gpow_eq_mod_order_of, hi]⟩⟩ lemma same_cycle_apply {f : perm β} {x y : β} : same_cycle f x (f y) ↔ same_cycle f x y := ⟨λ ⟨i, hi⟩, ⟨-1 + i, by rw [gpow_add, mul_apply, hi, gpow_neg_one, inv_apply_self]⟩, λ ⟨i, hi⟩, ⟨1 + i, by rw [gpow_add, mul_apply, hi, gpow_one]⟩⟩ lemma same_cycle_cycle {f : perm β} {x : β} (hx : f x ≠ x) : is_cycle f ↔ (∀ {y}, same_cycle f x y ↔ f y ≠ y) := ⟨λ hf y, ⟨λ ⟨i, hi⟩ hy, hx $ by { rw [← gpow_apply_eq_self_of_apply_eq_self hy i, (f ^ i).injective.eq_iff] at hi, rw [hi, hy] }, hf.exists_gpow_eq hx⟩, λ h, ⟨x, hx, λ y hy, h.2 hy⟩⟩ lemma same_cycle_inv (f : perm β) {x y : β} : same_cycle f⁻¹ x y ↔ same_cycle f x y := ⟨λ ⟨i, hi⟩, ⟨-i, by rw [gpow_neg, ← inv_gpow, hi]⟩, λ ⟨i, hi⟩, ⟨-i, by rw [gpow_neg, ← inv_gpow, inv_inv, hi]⟩ ⟩ lemma same_cycle_inv_apply {f : perm β} {x y : β} : same_cycle f x (f⁻¹ y) ↔ same_cycle f x y := by rw [← same_cycle_inv, same_cycle_apply, same_cycle_inv] /-! ### `cycle_of` -/ /-- `f.cycle_of x` is the cycle of the permutation `f` to which `x` belongs. -/ def cycle_of [fintype α] (f : perm α) (x : α) : perm α := of_subtype (@subtype_perm _ f (same_cycle f x) (λ _, same_cycle_apply.symm)) lemma cycle_of_apply [fintype α] (f : perm α) (x y : α) : cycle_of f x y = if same_cycle f x y then f y else y := rfl lemma cycle_of_inv [fintype α] (f : perm α) (x : α) : (cycle_of f x)⁻¹ = cycle_of f⁻¹ x := equiv.ext $ λ y, begin rw [inv_eq_iff_eq, cycle_of_apply, cycle_of_apply], split_ifs; simp [*, same_cycle_inv, same_cycle_inv_apply] at * end @[simp] lemma cycle_of_pow_apply_self [fintype α] (f : perm α) (x : α) : ∀ n : ℕ, (cycle_of f x ^ n) x = (f ^ n) x | 0 := rfl | (n+1) := by { rw [pow_succ, mul_apply, cycle_of_apply, cycle_of_pow_apply_self, if_pos, pow_succ, mul_apply], exact ⟨n, rfl⟩ } @[simp] lemma cycle_of_gpow_apply_self [fintype α] (f : perm α) (x : α) : ∀ n : ℤ, (cycle_of f x ^ n) x = (f ^ n) x | (n : ℕ) := cycle_of_pow_apply_self f x n | -[1+ n] := by rw [gpow_neg_succ_of_nat, ← inv_pow, cycle_of_inv, gpow_neg_succ_of_nat, ← inv_pow, cycle_of_pow_apply_self] lemma same_cycle.cycle_of_apply [fintype α] {f : perm α} {x y : α} (h : same_cycle f x y) : cycle_of f x y = f y := dif_pos h lemma cycle_of_apply_of_not_same_cycle [fintype α] {f : perm α} {x y : α} (h : ¬same_cycle f x y) : cycle_of f x y = y := dif_neg h @[simp] lemma cycle_of_apply_self [fintype α] (f : perm α) (x : α) : cycle_of f x x = f x := (same_cycle.refl _ _).cycle_of_apply lemma is_cycle.cycle_of_eq [fintype α] {f : perm α} (hf : is_cycle f) {x : α} (hx : f x ≠ x) : cycle_of f x = f := equiv.ext $ λ y, if h : same_cycle f x y then by rw [h.cycle_of_apply] else by rw [cycle_of_apply_of_not_same_cycle h, not_not.1 (mt ((same_cycle_cycle hx).1 hf).2 h)] lemma cycle_of_one [fintype α] (x : α) : cycle_of 1 x = 1 := by rw [cycle_of, subtype_perm_one (same_cycle 1 x), of_subtype.map_one] lemma is_cycle_cycle_of [fintype α] (f : perm α) {x : α} (hx : f x ≠ x) : is_cycle (cycle_of f x) := have cycle_of f x x ≠ x, by rwa [(same_cycle.refl _ _).cycle_of_apply], (same_cycle_cycle this).2 $ λ y, ⟨λ h, mt h.apply_eq_self_iff.2 this, λ h, if hxy : same_cycle f x y then let ⟨i, hi⟩ := hxy in ⟨i, by rw [cycle_of_gpow_apply_self, hi]⟩ else by { rw [cycle_of_apply_of_not_same_cycle hxy] at h, exact (h rfl).elim }⟩ /-! ### `cycle_factors` -/ /-- Given a list `l : list α` and a permutation `f : perm α` whose nonfixed points are all in `l`, recursively factors `f` into cycles. -/ def cycle_factors_aux [fintype α] : Π (l : list α) (f : perm α), (∀ {x}, f x ≠ x → x ∈ l) → {l : list (perm α) // l.prod = f ∧ (∀ g ∈ l, is_cycle g) ∧ l.pairwise disjoint} | [] f h := ⟨[], by { simp only [imp_false, list.pairwise.nil, list.not_mem_nil, forall_const, and_true, forall_prop_of_false, not_not, not_false_iff, list.prod_nil] at *, ext, simp * }⟩ | (x::l) f h := if hx : f x = x then cycle_factors_aux l f (λ y hy, list.mem_of_ne_of_mem (λ h, hy (by rwa h)) (h hy)) else let ⟨m, hm₁, hm₂, hm₃⟩ := cycle_factors_aux l ((cycle_of f x)⁻¹ * f) (λ y hy, list.mem_of_ne_of_mem (λ h : y = x, by { rw [h, mul_apply, ne.def, inv_eq_iff_eq, cycle_of_apply_self] at hy, exact hy rfl }) (h (λ h : f y = y, by { rw [mul_apply, h, ne.def, inv_eq_iff_eq, cycle_of_apply] at hy, split_ifs at hy; cc }))) in ⟨(cycle_of f x) :: m, by { rw [list.prod_cons, hm₁], simp }, λ g hg, ((list.mem_cons_iff _ _ _).1 hg).elim (λ hg, hg.symm ▸ is_cycle_cycle_of _ hx) (hm₂ g), list.pairwise_cons.2 ⟨λ g hg y, or_iff_not_imp_left.2 (λ hfy, have hxy : same_cycle f x y := not_not.1 (mt cycle_of_apply_of_not_same_cycle hfy), have hgm : g :: m.erase g ~ m := list.cons_perm_iff_perm_erase.2 ⟨hg, list.perm.refl _⟩, have ∀ h ∈ m.erase g, disjoint g h, from (list.pairwise_cons.1 ((hgm.pairwise_iff (λ a b (h : disjoint a b), h.symm)).2 hm₃)).1, classical.by_cases id $ λ hgy : g y ≠ y, (disjoint_prod_right _ this y).resolve_right $ have hsc : same_cycle f⁻¹ x (f y), by rwa [same_cycle_inv, same_cycle_apply], by { rw [disjoint_prod_perm hm₃ hgm.symm, list.prod_cons, ← eq_inv_mul_iff_mul_eq] at hm₁, rwa [hm₁, mul_apply, mul_apply, cycle_of_inv, hsc.cycle_of_apply, inv_apply_self, inv_eq_iff_eq, eq_comm] }), hm₃⟩⟩ /-- Factors a permutation `f` into a list of disjoint cyclic permutations that multiply to `f`. -/ def cycle_factors [fintype α] [linear_order α] (f : perm α) : {l : list (perm α) // l.prod = f ∧ (∀ g ∈ l, is_cycle g) ∧ l.pairwise disjoint} := cycle_factors_aux (univ.sort (≤)) f (λ _ _, (mem_sort _).2 (mem_univ _)) section fixed_points /-! ### Fixed points -/ lemma one_lt_nonfixed_point_card_of_ne_one [fintype α] {σ : perm α} (h : σ ≠ 1) : 1 < (filter (λ x, σ x ≠ x) univ).card := begin rw one_lt_card_iff, contrapose! h, ext x, dsimp, have := h (σ x) x, contrapose! this, simpa, end lemma fixed_point_card_lt_of_ne_one [fintype α] {σ : perm α} (h : σ ≠ 1) : (filter (λ x, σ x = x) univ).card < fintype.card α - 1 := begin rw [nat.lt_sub_left_iff_add_lt, ← nat.lt_sub_right_iff_add_lt, ← finset.card_compl, finset.compl_filter], exact one_lt_nonfixed_point_card_of_ne_one h end end fixed_points end equiv.perm
b8a6c49d4d4b241f23542917814bdecf40ccc445
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/algebra/regular/pow.lean
408c719f6ab94c17baba0343b25cbfc43d966a5f
[ "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
2,232
lean
/- Copyright (c) 2021 Damiano Testa. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Damiano Testa -/ import algebra.group_power.basic import algebra.regular.basic import algebra.iterate_hom /-! # Regular elements ## Implementation details Group powers and other definitions import a lot of the algebra hierarchy. Lemmas about them are kept separate to be able to provide `is_regular` early in the algebra hierarchy. -/ variables {R : Type*} {a b : R} section monoid variable [monoid R] /-- Any power of a left-regular element is left-regular. -/ lemma is_left_regular.pow (n : ℕ) (rla : is_left_regular a) : is_left_regular (a ^ n) := by simp only [is_left_regular, ← mul_left_iterate, rla.iterate n] /-- Any power of a right-regular element is right-regular. -/ lemma is_right_regular.pow (n : ℕ) (rra : is_right_regular a) : is_right_regular (a ^ n) := by { rw [is_right_regular, ← mul_right_iterate], exact rra.iterate n } /-- Any power of a regular element is regular. -/ lemma is_regular.pow (n : ℕ) (ra : is_regular a) : is_regular (a ^ n) := ⟨is_left_regular.pow n ra.left, is_right_regular.pow n ra.right⟩ /-- An element `a` is left-regular if and only if a positive power of `a` is left-regular. -/ lemma is_left_regular.pow_iff {n : ℕ} (n0 : 0 < n) : is_left_regular (a ^ n) ↔ is_left_regular a := begin refine ⟨_, is_left_regular.pow n⟩, rw [← nat.succ_pred_eq_of_pos n0, pow_succ'], exact is_left_regular.of_mul, end /-- An element `a` is right-regular if and only if a positive power of `a` is right-regular. -/ lemma is_right_regular.pow_iff {n : ℕ} (n0 : 0 < n) : is_right_regular (a ^ n) ↔ is_right_regular a := begin refine ⟨_, is_right_regular.pow n⟩, rw [← nat.succ_pred_eq_of_pos n0, pow_succ], exact is_right_regular.of_mul, end /-- An element `a` is regular if and only if a positive power of `a` is regular. -/ lemma is_regular.pow_iff {n : ℕ} (n0 : 0 < n) : is_regular (a ^ n) ↔ is_regular a := ⟨λ h, ⟨(is_left_regular.pow_iff n0).mp h.left, (is_right_regular.pow_iff n0).mp h.right⟩, λ h, ⟨is_left_regular.pow n h.left, is_right_regular.pow n h.right⟩⟩ end monoid
9615371d1799af96b348164d9cfd89894b408f89
a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940
/stage0/src/Init/Data/Nat/Gcd.lean
d9f33861250f758837b817634ca8344888fafce4
[ "Apache-2.0" ]
permissive
WojciechKarpiel/lean4
7f89706b8e3c1f942b83a2c91a3a00b05da0e65b
f6e1314fa08293dea66a329e05b6c196a0189163
refs/heads/master
1,686,633,402,214
1,625,821,189,000
1,625,821,258,000
384,640,886
0
0
Apache-2.0
1,625,903,617,000
1,625,903,026,000
null
UTF-8
Lean
false
false
931
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 -/ prelude import Init.Data.Nat.Div namespace Nat private def gcdF (x : Nat) : (∀ x₁, x₁ < x → Nat → Nat) → Nat → Nat := match x with | 0 => fun _ y => y | succ x => fun f y => f (y % succ x) (mod_lt _ (zeroLtSucc _)) (succ x) @[extern "lean_nat_gcd"] def gcd (a b : @& Nat) : Nat := WellFounded.fix ltWf gcdF a b @[simp] theorem gcd_zero_left (y : Nat) : gcd 0 y = y := rfl theorem gcd_succ (x y : Nat) : gcd (succ x) y = gcd (y % succ x) (succ x) := rfl @[simp] theorem gcd_one_left (n : Nat) : gcd 1 n = 1 := by rw [gcd_succ, mod_one] rfl @[simp] theorem gcd_zero_right (n : Nat) : gcd n 0 = n := by cases n <;> simp [gcd_succ] @[simp] theorem gcd_self (n : Nat) : gcd n n = n := by cases n <;> simp [gcd_succ] end Nat
d7581d3703f0d931329d9af01df0825326215073
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/Lean/Util/FindExpr.lean
56217625b8898c58c5282f1eaa4392a7cace1403
[ "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
3,085
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Expr import Lean.Util.PtrSet namespace Lean namespace Expr namespace FindImpl unsafe abbrev FindM := StateT (PtrSet Expr) Id @[inline] unsafe def checkVisited (e : Expr) : OptionT FindM Unit := do if (← get).contains e then failure modify fun s => s.insert e unsafe def findM? (p : Expr → Bool) (e : Expr) : OptionT FindM Expr := let rec visit (e : Expr) := do checkVisited e if p e then pure e else match e with | .forallE _ d b _ => visit d <|> visit b | .lam _ d b _ => visit d <|> visit b | .mdata _ b => visit b | .letE _ t v b _ => visit t <|> visit v <|> visit b | .app f a => visit f <|> visit a | .proj _ _ b => visit b | _ => failure visit e unsafe def findUnsafe? (p : Expr → Bool) (e : Expr) : Option Expr := Id.run <| findM? p e |>.run' mkPtrSet end FindImpl @[implemented_by FindImpl.findUnsafe?] def find? (p : Expr → Bool) (e : Expr) : Option Expr := /- This is a reference implementation for the unsafe one above -/ if p e then some e else match e with | .forallE _ d b _ => find? p d <|> find? p b | .lam _ d b _ => find? p d <|> find? p b | .mdata _ b => find? p b | .letE _ t v b _ => find? p t <|> find? p v <|> find? p b | .app f a => find? p f <|> find? p a | .proj _ _ b => find? p b | _ => none /-- Return true if `e` occurs in `t` -/ def occurs (e : Expr) (t : Expr) : Bool := (t.find? fun s => s == e).isSome /-- Return type for `findExt?` function argument. -/ inductive FindStep where /-- Found desired subterm -/ | found /-- Search subterms -/ | visit /-- Do not search subterms -/ | done namespace FindExtImpl unsafe def findM? (p : Expr → FindStep) (e : Expr) : OptionT FindImpl.FindM Expr := visit e where visitApp (e : Expr) := match e with | .app f a .. => visitApp f <|> visit a | e => visit e visit (e : Expr) := do FindImpl.checkVisited e match p e with | .done => failure | .found => pure e | .visit => match e with | .forallE _ d b _ => visit d <|> visit b | .lam _ d b _ => visit d <|> visit b | .mdata _ b => visit b | .letE _ t v b _ => visit t <|> visit v <|> visit b | .app .. => visitApp e | .proj _ _ b => visit b | _ => failure unsafe def findUnsafe? (p : Expr → FindStep) (e : Expr) : Option Expr := Id.run <| findM? p e |>.run' mkPtrSet end FindExtImpl /-- Similar to `find?`, but `p` can return `FindStep.done` to interrupt the search on subterms. Remark: Differently from `find?`, we do not invoke `p` for partial applications of an application. -/ @[implemented_by FindExtImpl.findUnsafe?] opaque findExt? (p : Expr → FindStep) (e : Expr) : Option Expr end Expr end Lean
5049d3ec29e7a6f119636ff593f7458c2135054e
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/category_theory/yoneda.lean
4d6761f2974e89c355a0f9688509760811b12ed2
[ "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
14,393
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.functor.hom import category_theory.functor.currying import category_theory.products.basic /-! # The Yoneda embedding > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. 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)`. ## References * [Stacks: Opposite Categories and the Yoneda Lemma](https://stacks.math.columbia.edu/tag/001L) -/ namespace category_theory open opposite universes v₁ u₁ u₂-- morphism levels before object levels. See note [category_theory universes]. variables {C : Type u₁} [category.{v₁} C] /-- The Yoneda embedding, as a functor from `C` into presheaves on `C`. See <https://stacks.math.columbia.edu/tag/001O>. -/ @[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 } } /-- The co-Yoneda embedding, as a functor from `Cᵒᵖ` into co-presheaves on `C`. -/ @[simps] def coyoneda : Cᵒᵖ ⥤ (C ⥤ Type v₁) := { obj := λ X, { obj := λ Y, unop X ⟶ Y, map := λ Y Y' f g, g ≫ f }, map := λ X X' f, { app := λ Y g, f.unop ≫ g } } namespace yoneda lemma obj_map_id {X Y : C} (f : op X ⟶ op Y) : (yoneda.obj X).map f (𝟙 X) = (yoneda.map f.unop).app (op Y) (𝟙 Y) := by { dsimp, simp } @[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 /-- The Yoneda embedding is full. See <https://stacks.math.columbia.edu/tag/001P>. -/ instance yoneda_full : full (yoneda : C ⥤ Cᵒᵖ ⥤ Type v₁) := { preimage := λ X Y f, f.app (op X) (𝟙 X) } /-- The Yoneda embedding is faithful. See <https://stacks.math.columbia.edu/tag/001P>. -/ instance yoneda_faithful : faithful (yoneda : C ⥤ Cᵒᵖ ⥤ Type v₁) := { map_injective' := λ X Y f g p, by convert (congr_fun (congr_app p (op X)) (𝟙 X)); dsimp; simp } /-- 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 := yoneda.preimage_iso (nat_iso.of_components (λ Z, { hom := p, inv := q, }) (by tidy)) /-- If `yoneda.map f` is an isomorphism, so was `f`. -/ lemma 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) := (functor_to_types.naturality _ _ α f h).symm instance coyoneda_full : full (coyoneda : Cᵒᵖ ⥤ C ⥤ Type v₁) := { preimage := λ X Y f, (f.app _ (𝟙 X.unop)).op } instance coyoneda_faithful : faithful (coyoneda : Cᵒᵖ ⥤ C ⥤ Type v₁) := { map_injective' := λ X Y f g p, begin have t := congr_fun (congr_app p X.unop) (𝟙 _), simpa using congr_arg quiver.hom.op t, end } /-- If `coyoneda.map f` is an isomorphism, so was `f`. -/ lemma is_iso {X Y : Cᵒᵖ} (f : X ⟶ Y) [is_iso (coyoneda.map f)] : is_iso f := is_iso_of_fully_faithful coyoneda f /-- The identity functor on `Type` is isomorphic to the coyoneda functor coming from `punit`. -/ def punit_iso : coyoneda.obj (opposite.op punit) ≅ 𝟭 (Type v₁) := nat_iso.of_components (λ X, { hom := λ f, f ⟨⟩, inv := λ x _, x }) (by tidy) /-- Taking the `unop` of morphisms is a natural isomorphism. -/ @[simps] def obj_op_op (X : C) : coyoneda.obj (op (op X)) ≅ yoneda.obj X := nat_iso.of_components (λ Y, (op_equiv _ _).to_iso) (λ X Y f, rfl) end coyoneda namespace functor /-- A functor `F : Cᵒᵖ ⥤ Type v₁` is representable if there is object `X` so `F ≅ yoneda.obj X`. See <https://stacks.math.columbia.edu/tag/001Q>. -/ class representable (F : Cᵒᵖ ⥤ Type v₁) : Prop := (has_representation : ∃ X (f : yoneda.obj X ⟶ F), is_iso f) instance {X : C} : representable (yoneda.obj X) := { has_representation := ⟨X, 𝟙 _, infer_instance⟩ } /-- A functor `F : C ⥤ Type v₁` is corepresentable if there is object `X` so `F ≅ coyoneda.obj X`. See <https://stacks.math.columbia.edu/tag/001Q>. -/ class corepresentable (F : C ⥤ Type v₁) : Prop := (has_corepresentation : ∃ X (f : coyoneda.obj X ⟶ F), is_iso f) instance {X : Cᵒᵖ} : corepresentable (coyoneda.obj X) := { has_corepresentation := ⟨X, 𝟙 _, infer_instance⟩ } -- instance : corepresentable (𝟭 (Type v₁)) := -- corepresentable_of_nat_iso (op punit) coyoneda.punit_iso section representable variables (F : Cᵒᵖ ⥤ Type v₁) variable [F.representable] /-- The representing object for the representable functor `F`. -/ noncomputable def repr_X : C := (representable.has_representation : ∃ X (f : _ ⟶ F), _).some /-- The (forward direction of the) isomorphism witnessing `F` is representable. -/ noncomputable def repr_f : yoneda.obj F.repr_X ⟶ F := representable.has_representation.some_spec.some /-- The representing element for the representable functor `F`, sometimes called the universal element of the functor. -/ noncomputable def repr_x : F.obj (op F.repr_X) := F.repr_f.app (op F.repr_X) (𝟙 F.repr_X) instance : is_iso F.repr_f := representable.has_representation.some_spec.some_spec /-- An isomorphism between `F` and a functor of the form `C(-, F.repr_X)`. Note the components `F.repr_w.app X` definitionally have type `(X.unop ⟶ F.repr_X) ≅ F.obj X`. -/ noncomputable def repr_w : yoneda.obj F.repr_X ≅ F := as_iso F.repr_f @[simp] lemma repr_w_hom : F.repr_w.hom = F.repr_f := rfl lemma repr_w_app_hom (X : Cᵒᵖ) (f : unop X ⟶ F.repr_X) : (F.repr_w.app X).hom f = F.map f.op F.repr_x := begin change F.repr_f.app X f = (F.repr_f.app (op F.repr_X) ≫ F.map f.op) (𝟙 F.repr_X), rw ←F.repr_f.naturality, dsimp, simp end end representable section corepresentable variables (F : C ⥤ Type v₁) variable [F.corepresentable] /-- The representing object for the corepresentable functor `F`. -/ noncomputable def corepr_X : C := (corepresentable.has_corepresentation : ∃ X (f : _ ⟶ F), _).some.unop /-- The (forward direction of the) isomorphism witnessing `F` is corepresentable. -/ noncomputable def corepr_f : coyoneda.obj (op F.corepr_X) ⟶ F := corepresentable.has_corepresentation.some_spec.some /-- The representing element for the corepresentable functor `F`, sometimes called the universal element of the functor. -/ noncomputable def corepr_x : F.obj F.corepr_X := F.corepr_f.app F.corepr_X (𝟙 F.corepr_X) instance : is_iso F.corepr_f := corepresentable.has_corepresentation.some_spec.some_spec /-- An isomorphism between `F` and a functor of the form `C(F.corepr X, -)`. Note the components `F.corepr_w.app X` definitionally have type `F.corepr_X ⟶ X ≅ F.obj X`. -/ noncomputable def corepr_w : coyoneda.obj (op F.corepr_X) ≅ F := as_iso F.corepr_f lemma corepr_w_app_hom (X : C) (f : F.corepr_X ⟶ X) : (F.corepr_w.app X).hom f = F.map f F.corepr_x := begin change F.corepr_f.app X f = (F.corepr_f.app F.corepr_X ≫ F.map f) (𝟙 F.corepr_X), rw ←F.corepr_f.naturality, dsimp, simp end end corepresentable end functor lemma representable_of_nat_iso (F : Cᵒᵖ ⥤ Type v₁) {G} (i : F ≅ G) [F.representable] : G.representable := { has_representation := ⟨F.repr_X, F.repr_f ≫ i.hom, infer_instance⟩ } lemma corepresentable_of_nat_iso (F : C ⥤ Type v₁) {G} (i : F ≅ G) [F.corepresentable] : G.corepresentable := { has_corepresentation := ⟨op F.corepr_X, F.corepr_f ≫ i.hom, infer_instance⟩ } instance : functor.corepresentable (𝟭 (Type v₁)) := corepresentable_of_nat_iso (coyoneda.obj (op punit)) coyoneda.punit_iso open opposite variables (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 /-- The "Yoneda evaluation" functor, which sends `X : Cᵒᵖ` and `F : Cᵒᵖ ⥤ Type` to `F.obj X`, functorially in both `X` and `F`. -/ 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 /-- The "Yoneda pairing" functor, which sends `X : Cᵒᵖ` and `F : Cᵒᵖ ⥤ Type` to `yoneda.op.obj X ⟶ F`, functorially in both `X` and `F`. -/ 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 /-- The Yoneda lemma asserts that that the Yoneda pairing `(X : Cᵒᵖ, F : Cᵒᵖ ⥤ Type) ↦ (yoneda.obj (unop X) ⟶ F)` is naturally isomorphic to the evaluation `(X, F) ↦ F.obj X`. See <https://stacks.math.columbia.edu/tag/001P>. -/ 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, quiver.hom.unop_op], erw [category.id_comp], end, inv_hom_id' := begin ext, dsimp, rw [functor_to_types.map_id_apply] end }. variables {C} /-- The isomorphism between `yoneda.obj X ⟶ F` and `F.obj (op X)` (we need to insert a `ulift` to get the universes right!) given by the Yoneda lemma. -/ @[simps] 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) /-- We have a type-level equivalence between natural transformations from the yoneda embedding and elements of `F.obj X`, without any universe switching. -/ def yoneda_equiv {X : C} {F : Cᵒᵖ ⥤ Type v₁} : (yoneda.obj X ⟶ F) ≃ F.obj (op X) := (yoneda_sections X F).to_equiv.trans equiv.ulift @[simp] lemma yoneda_equiv_apply {X : C} {F : Cᵒᵖ ⥤ Type v₁} (f : yoneda.obj X ⟶ F) : yoneda_equiv f = f.app (op X) (𝟙 X) := rfl @[simp] lemma yoneda_equiv_symm_app_apply {X : C} {F : Cᵒᵖ ⥤ Type v₁} (x : F.obj (op X)) (Y : Cᵒᵖ) (f : Y.unop ⟶ X) : (yoneda_equiv.symm x).app Y f = F.map f.op x := rfl lemma yoneda_equiv_naturality {X Y : C} {F : Cᵒᵖ ⥤ Type v₁} (f : yoneda.obj X ⟶ F) (g : Y ⟶ X) : F.map g.op (yoneda_equiv f) = yoneda_equiv (yoneda.map g ≫ f) := begin change (f.app (op X) ≫ F.map g.op) (𝟙 X) = f.app (op Y) (𝟙 Y ≫ g), rw ←f.naturality, dsimp, simp, end /-- When `C` is a small category, we can restate the isomorphism from `yoneda_sections` without having to change universes. -/ 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 _ @[simp] lemma yoneda_sections_small_hom {C : Type u₁} [small_category C] (X : C) (F : Cᵒᵖ ⥤ Type u₁) (f : yoneda.obj X ⟶ F) : (yoneda_sections_small X F).hom f = f.app _ (𝟙 _) := rfl @[simp] lemma yoneda_sections_small_inv_app_apply {C : Type u₁} [small_category C] (X : C) (F : Cᵒᵖ ⥤ Type u₁) (t : F.obj (op X)) (Y : Cᵒᵖ) (f : Y.unop ⟶ X) : ((yoneda_sections_small X F).inv t).app Y f = F.map f.op t := rfl local attribute [ext] functor.ext /-- The curried version of yoneda lemma when `C` is small. -/ def curried_yoneda_lemma {C : Type u₁} [small_category C] : (yoneda.op ⋙ coyoneda : Cᵒᵖ ⥤ (Cᵒᵖ ⥤ Type u₁) ⥤ Type u₁) ≅ evaluation Cᵒᵖ (Type u₁) := eq_to_iso (by tidy) ≪≫ curry.map_iso (yoneda_lemma C ≪≫ iso_whisker_left (evaluation_uncurried Cᵒᵖ (Type u₁)) ulift_functor_trivial) ≪≫ eq_to_iso (by tidy) /-- The curried version of yoneda lemma when `C` is small. -/ def curried_yoneda_lemma' {C : Type u₁} [small_category C] : yoneda ⋙ (whiskering_left Cᵒᵖ (Cᵒᵖ ⥤ Type u₁)ᵒᵖ (Type u₁)).obj yoneda.op ≅ 𝟭 (Cᵒᵖ ⥤ Type u₁) := eq_to_iso (by tidy) ≪≫ curry.map_iso (iso_whisker_left (prod.swap _ _) (yoneda_lemma C ≪≫ iso_whisker_left (evaluation_uncurried Cᵒᵖ (Type u₁)) ulift_functor_trivial : _)) ≪≫ eq_to_iso (by tidy) end category_theory
0b67585b8a533bb386b2b5fbc0d011aa0f6b3c7b
947b78d97130d56365ae2ec264df196ce769371a
/tests/lean/run/meta4.lean
dd5322d1ee8c7d89763f445638f6c684f698afd4
[ "Apache-2.0" ]
permissive
shyamalschandra/lean4
27044812be8698f0c79147615b1d5090b9f4b037
6e7a883b21eaf62831e8111b251dc9b18f40e604
refs/heads/master
1,671,417,126,371
1,601,859,995,000
1,601,860,020,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,362
lean
import Lean.Meta new_frontend open Lean open Lean.Meta def print (msg : MessageData) : MetaM Unit := trace! `Meta.debug msg def checkM (x : MetaM Bool) : MetaM Unit := unlessM x $ throwError "check failed" axiom Ax : forall (α β : Type), α → β → DecidableEq β set_option trace.Meta.debug true def tst1 : MetaM Unit := do let cinfo ← getConstInfo `Ax; let (_, _, e) ← forallMetaTelescopeReducing cinfo.type (some 0); checkM (pure (!e.hasMVar)); print e; let (_, _, e) ← forallMetaTelescopeReducing cinfo.type (some 1); checkM (pure e.hasMVar); checkM (pure e.isForall); print e; let (_, _, e) ← forallMetaTelescopeReducing cinfo.type (some 5); checkM (pure e.hasMVar); checkM (pure e.isForall); print e; let (_, _, e) ← forallMetaTelescopeReducing cinfo.type (some 6); checkM (pure e.hasMVar); checkM (pure (!e.isForall)); print e; let (_, _, e') ← forallMetaTelescopeReducing cinfo.type; print e'; checkM (isDefEq e e'); forallBoundedTelescope cinfo.type (some 0) $ fun xs body => checkM (pure (xs.size == 0)); forallBoundedTelescope cinfo.type (some 1) $ fun xs body => checkM (pure (xs.size == 1)); forallBoundedTelescope cinfo.type (some 6) $ fun xs body => do { print xs; checkM (pure (xs.size == 6)) }; forallBoundedTelescope cinfo.type (some 10) $ fun xs body => do { print xs; checkM (pure (xs.size == 6)) }; pure () #eval tst1
6764d98e67ddebaaf98bd088430c92a9f812b7f7
8b9f17008684d796c8022dab552e42f0cb6fb347
/library/data/list/comb.lean
0c30467a68c8d0e8eac062ebc72f1c2f4fc9e17d
[ "Apache-2.0" ]
permissive
chubbymaggie/lean
0d06ae25f9dd396306fb02190e89422ea94afd7b
d2c7b5c31928c98f545b16420d37842c43b4ae9a
refs/heads/master
1,611,313,622,901
1,430,266,839,000
1,430,267,083,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
13,143
lean
/- Copyright (c) 2015 Leonardo de Moura. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Module: data.list.comb Authors: Leonardo de Moura List combinators -/ import data.list.basic open nat prod decidable function helper_tactics namespace list variables {A B C : Type} /- map -/ definition map (f : A → B) : list A → list B | [] := [] | (a :: l) := f a :: map l theorem map_nil (f : A → B) : map f [] = [] theorem map_cons (f : A → B) (a : A) (l : list A) : map f (a :: l) = f a :: map f l theorem map_id : ∀ l : list A, map id l = l | [] := rfl | (x::xs) := begin rewrite [map_cons, map_id] end theorem map_map (g : B → C) (f : A → B) : ∀ l, map g (map f l) = map (g ∘ f) l | [] := rfl | (a :: l) := show (g ∘ f) a :: map g (map f l) = map (g ∘ f) (a :: l), by rewrite (map_map l) theorem len_map (f : A → B) : ∀ l : list A, length (map f l) = length l | [] := by esimp | (a :: l) := show length (map f l) + 1 = length l + 1, by rewrite (len_map l) theorem mem_map {A B : Type} (f : A → B) : ∀ {a l}, a ∈ l → f a ∈ map f l | a [] i := absurd i !not_mem_nil | a (x::xs) i := or.elim (eq_or_mem_of_mem_cons i) (λ aeqx : a = x, by rewrite [aeqx, map_cons]; apply mem_cons) (λ ainxs : a ∈ xs, or.inr (mem_map ainxs)) theorem eq_of_map_const {A B : Type} {b₁ b₂ : B} : ∀ {l : list A}, b₁ ∈ map (const A b₂) l → b₁ = b₂ | [] h := absurd h !not_mem_nil | (a::l) h := or.elim (eq_or_mem_of_mem_cons h) (λ b₁eqb₂ : b₁ = b₂, b₁eqb₂) (λ b₁inl : b₁ ∈ map (const A b₂) l, eq_of_map_const b₁inl) definition map₂ (f : A → B → C) : list A → list B → list C | [] _ := [] | _ [] := [] | (x::xs) (y::ys) := f x y :: map₂ xs ys /- filter -/ definition filter (p : A → Prop) [h : decidable_pred p] : list A → list A | [] := [] | (a::l) := if p a then a :: filter l else filter l theorem filter_nil (p : A → Prop) [h : decidable_pred p] : filter p [] = [] theorem filter_cons_of_pos {p : A → Prop} [h : decidable_pred p] {a : A} : ∀ l, p a → filter p (a::l) = a :: filter p l := λ l pa, if_pos pa theorem filter_cons_of_neg {p : A → Prop} [h : decidable_pred p] {a : A} : ∀ l, ¬ p a → filter p (a::l) = filter p l := λ l pa, if_neg pa theorem of_mem_filter {p : A → Prop} [h : decidable_pred p] {a : A} : ∀ {l}, a ∈ filter p l → p a | [] ain := absurd ain !not_mem_nil | (b::l) ain := by_cases (λ pb : p b, have aux : a ∈ b :: filter p l, by rewrite [filter_cons_of_pos _ pb at ain]; exact ain, or.elim (eq_or_mem_of_mem_cons aux) (λ aeqb : a = b, by rewrite [-aeqb at pb]; exact pb) (λ ainl, of_mem_filter ainl)) (λ npb : ¬ p b, by rewrite [filter_cons_of_neg _ npb at ain]; exact (of_mem_filter ain)) theorem mem_of_mem_filter {p : A → Prop} [h : decidable_pred p] {a : A} : ∀ {l}, a ∈ filter p l → a ∈ l | [] ain := absurd ain !not_mem_nil | (b::l) ain := by_cases (λ pb : p b, have aux : a ∈ b :: filter p l, by rewrite [filter_cons_of_pos _ pb at ain]; exact ain, or.elim (eq_or_mem_of_mem_cons aux) (λ aeqb : a = b, by rewrite [aeqb]; exact !mem_cons) (λ ainl, mem_cons_of_mem _ (mem_of_mem_filter ainl))) (λ npb : ¬ p b, by rewrite [filter_cons_of_neg _ npb at ain]; exact (mem_cons_of_mem _ (mem_of_mem_filter ain))) theorem mem_filter_of_mem {p : A → Prop} [h : decidable_pred p] {a : A} : ∀ {l}, a ∈ l → p a → a ∈ filter p l | [] ain pa := absurd ain !not_mem_nil | (b::l) ain pa := by_cases (λ pb : p b, or.elim (eq_or_mem_of_mem_cons ain) (λ aeqb : a = b, by rewrite [filter_cons_of_pos _ pb, aeqb]; exact !mem_cons) (λ ainl : a ∈ l, by rewrite [filter_cons_of_pos _ pb]; exact (mem_cons_of_mem _ (mem_filter_of_mem ainl pa)))) (λ npb : ¬ p b, or.elim (eq_or_mem_of_mem_cons ain) (λ aeqb : a = b, absurd (eq.rec_on aeqb pa) npb) (λ ainl : a ∈ l, by rewrite [filter_cons_of_neg _ npb]; exact (mem_filter_of_mem ainl pa))) theorem filter_subset {p : A → Prop} [h : decidable_pred p] (l : list A) : filter p l ⊆ l := λ a ain, mem_of_mem_filter ain theorem filter_append {p : A → Prop} [h : decidable_pred p] : ∀ (l₁ l₂ : list A), filter p (l₁++l₂) = filter p l₁ ++ filter p l₂ | [] l₂ := rfl | (a::l₁) l₂ := by_cases (λ pa : p a, by rewrite [append_cons, *filter_cons_of_pos _ pa, filter_append]) (λ npa : ¬ p a, by rewrite [append_cons, *filter_cons_of_neg _ npa, filter_append]) /- foldl & foldr -/ definition foldl (f : A → B → A) : A → list B → A | a [] := a | a (b :: l) := foldl (f a b) l theorem foldl_nil (f : A → B → A) (a : A) : foldl f a [] = a theorem foldl_cons (f : A → B → A) (a : A) (b : B) (l : list B) : foldl f a (b::l) = foldl f (f a b) l definition foldr (f : A → B → B) : B → list A → B | b [] := b | b (a :: l) := f a (foldr b l) theorem foldr_nil (f : A → B → B) (b : B) : foldr f b [] = b theorem foldr_cons (f : A → B → B) (b : B) (a : A) (l : list A) : foldr f b (a::l) = f a (foldr f b l) section foldl_eq_foldr -- foldl and foldr coincide when f is commutative and associative parameters {α : Type} {f : α → α → α} hypothesis (Hcomm : ∀ a b, f a b = f b a) hypothesis (Hassoc : ∀ a b c, f (f a b) c = f a (f b c)) include Hcomm Hassoc theorem foldl_eq_of_comm_of_assoc : ∀ a b l, foldl f a (b::l) = f b (foldl f a l) | a b nil := Hcomm a b | a b (c::l) := begin change (foldl f (f (f a b) c) l = f b (foldl f (f a c) l)), rewrite -foldl_eq_of_comm_of_assoc, change (foldl f (f (f a b) c) l = foldl f (f (f a c) b) l), have H₁ : f (f a b) c = f (f a c) b, by rewrite [Hassoc, Hassoc, Hcomm b c], rewrite H₁ end theorem foldl_eq_foldr : ∀ a l, foldl f a l = foldr f a l | a nil := rfl | a (b :: l) := begin rewrite foldl_eq_of_comm_of_assoc, esimp, change (f b (foldl f a l) = f b (foldr f a l)), rewrite foldl_eq_foldr end end foldl_eq_foldr theorem foldl_append (f : B → A → B) : ∀ (b : B) (l₁ l₂ : list A), foldl f b (l₁++l₂) = foldl f (foldl f b l₁) l₂ | b [] l₂ := rfl | b (a::l₁) l₂ := by rewrite [append_cons, *foldl_cons, foldl_append] theorem foldr_append (f : A → B → B) : ∀ (b : B) (l₁ l₂ : list A), foldr f b (l₁++l₂) = foldr f (foldr f b l₂) l₁ | b [] l₂ := rfl | b (a::l₁) l₂ := by rewrite [append_cons, *foldr_cons, foldr_append] /- all & any -/ definition all (l : list A) (p : A → Prop) : Prop := foldr (λ a r, p a ∧ r) true l definition any (l : list A) (p : A → Prop) : Prop := foldr (λ a r, p a ∨ r) false l theorem all_nil (p : A → Prop) : all [] p = true theorem all_cons (p : A → Prop) (a : A) (l : list A) : all (a::l) p = (p a ∧ all l p) theorem all_of_all_cons {p : A → Prop} {a : A} {l : list A} : all (a::l) p → all l p := assume h, by rewrite [all_cons at h]; exact (and.elim_right h) theorem of_all_cons {p : A → Prop} {a : A} {l : list A} : all (a::l) p → p a := assume h, by rewrite [all_cons at h]; exact (and.elim_left h) theorem all_cons_of_all {p : A → Prop} {a : A} {l : list A} : p a → all l p → all (a::l) p := assume pa alllp, and.intro pa alllp theorem all_implies {p q : A → Prop} : ∀ {l}, all l p → (∀ x, p x → q x) → all l q | [] h₁ h₂ := trivial | (a::l) h₁ h₂ := have allq : all l q, from all_implies (all_of_all_cons h₁) h₂, have qa : q a, from h₂ a (of_all_cons h₁), all_cons_of_all qa allq theorem of_mem_of_all {p : A → Prop} {a : A} : ∀ {l}, a ∈ l → all l p → p a | [] h₁ h₂ := absurd h₁ !not_mem_nil | (b::l) h₁ h₂ := or.elim (eq_or_mem_of_mem_cons h₁) (λ aeqb : a = b, by rewrite [all_cons at h₂, -aeqb at h₂]; exact (and.elim_left h₂)) (λ ainl : a ∈ l, have allp : all l p, by rewrite [all_cons at h₂]; exact (and.elim_right h₂), of_mem_of_all ainl allp) theorem any_nil (p : A → Prop) : any [] p = false theorem any_cons (p : A → Prop) (a : A) (l : list A) : any (a::l) p = (p a ∨ any l p) theorem any_of_mem (p : A → Prop) {a : A} : ∀ {l}, a ∈ l → p a → any l p | [] i h := absurd i !not_mem_nil | (b::l) i h := or.elim (eq_or_mem_of_mem_cons i) (λ aeqb : a = b, by rewrite [-aeqb]; exact (or.inl h)) (λ ainl : a ∈ l, have anyl : any l p, from any_of_mem ainl h, or.inr anyl) definition decidable_all (p : A → Prop) [H : decidable_pred p] : ∀ l, decidable (all l p) | [] := decidable_true | (a :: l) := match H a with | inl Hp₁ := match decidable_all l with | inl Hp₂ := inl (and.intro Hp₁ Hp₂) | inr Hn₂ := inr (not_and_of_not_right (p a) Hn₂) end | inr Hn := inr (not_and_of_not_left (all l p) Hn) end definition decidable_any (p : A → Prop) [H : decidable_pred p] : ∀ l, decidable (any l p) | [] := decidable_false | (a :: l) := match H a with | inl Hp := inl (or.inl Hp) | inr Hn₁ := match decidable_any l with | inl Hp₂ := inl (or.inr Hp₂) | inr Hn₂ := inr (not_or Hn₁ Hn₂) end end /- zip & unzip -/ definition zip (l₁ : list A) (l₂ : list B) : list (A × B) := map₂ (λ a b, (a, b)) l₁ l₂ definition unzip : list (A × B) → list A × list B | [] := ([], []) | ((a, b) :: l) := match unzip l with | (la, lb) := (a :: la, b :: lb) end theorem unzip_nil : unzip (@nil (A × B)) = ([], []) theorem unzip_cons (a : A) (b : B) (l : list (A × B)) : unzip ((a, b) :: l) = match unzip l with (la, lb) := (a :: la, b :: lb) end := rfl theorem zip_unzip : ∀ (l : list (A × B)), zip (pr₁ (unzip l)) (pr₂ (unzip l)) = l | [] := rfl | ((a, b) :: l) := begin rewrite unzip_cons, have r : zip (pr₁ (unzip l)) (pr₂ (unzip l)) = l, from zip_unzip l, revert r, apply (prod.cases_on (unzip l)), intros [la, lb, r], rewrite -r end /- flat -/ definition flat (l : list (list A)) : list A := foldl append nil l /- cross product -/ section cross_product definition cross_product : list A → list B → list (A × B) | [] l₂ := [] | (a::l₁) l₂ := map (λ b, (a, b)) l₂ ++ cross_product l₁ l₂ theorem nil_cross_product (l : list B) : cross_product (@nil A) l = [] theorem cross_product_cons (a : A) (l₁ : list A) (l₂ : list B) : cross_product (a::l₁) l₂ = map (λ b, (a, b)) l₂ ++ cross_product l₁ l₂ theorem cross_product_nil : ∀ (l : list A), cross_product l (@nil B) = [] | [] := rfl | (a::l) := by rewrite [cross_product_cons, map_nil, cross_product_nil] theorem eq_of_mem_map_pair₁ {a₁ a : A} {b₁ : B} {l : list B} : (a₁, b₁) ∈ map (λ b, (a, b)) l → a₁ = a := assume ain, assert h₁ : pr1 (a₁, b₁) ∈ map pr1 (map (λ b, (a, b)) l), from mem_map pr1 ain, assert h₂ : a₁ ∈ map (λb, a) l, by rewrite [map_map at h₁, ↑pr1 at h₁]; exact h₁, eq_of_map_const h₂ theorem mem_of_mem_map_pair₁ {a₁ a : A} {b₁ : B} {l : list B} : (a₁, b₁) ∈ map (λ b, (a, b)) l → b₁ ∈ l := assume ain, assert h₁ : pr2 (a₁, b₁) ∈ map pr2 (map (λ b, (a, b)) l), from mem_map pr2 ain, assert h₂ : b₁ ∈ map (λx, x) l, by rewrite [map_map at h₁, ↑pr2 at h₁]; exact h₁, by rewrite [map_id at h₂]; exact h₂ theorem mem_cross_product {a : A} {b : B} : ∀ {l₁ l₂}, a ∈ l₁ → b ∈ l₂ → (a, b) ∈ cross_product l₁ l₂ | [] l₂ h₁ h₂ := absurd h₁ !not_mem_nil | (x::l₁) l₂ h₁ h₂ := or.elim (eq_or_mem_of_mem_cons h₁) (λ aeqx : a = x, assert aux : (a, b) ∈ map (λ b, (a, b)) l₂, from mem_map _ h₂, by rewrite [-aeqx]; exact (mem_append_left _ aux)) (λ ainl₁ : a ∈ l₁, have inl₁l₂ : (a, b) ∈ cross_product l₁ l₂, from mem_cross_product ainl₁ h₂, mem_append_right _ inl₁l₂) theorem mem_of_mem_cross_product_left {a : A} {b : B} : ∀ {l₁ l₂}, (a, b) ∈ cross_product l₁ l₂ → a ∈ l₁ | [] l₂ h := absurd h !not_mem_nil | (x::l₁) l₂ h := or.elim (mem_or_mem_of_mem_append h) (λ ain : (a, b) ∈ map (λ b, (x, b)) l₂, assert aeqx : a = x, from eq_of_mem_map_pair₁ ain, by rewrite [aeqx]; exact !mem_cons) (λ ain : (a, b) ∈ cross_product l₁ l₂, have ainl₁ : a ∈ l₁, from mem_of_mem_cross_product_left ain, mem_cons_of_mem _ ainl₁) theorem mem_of_mem_cross_product_right {a : A} {b : B} : ∀ {l₁ l₂}, (a, b) ∈ cross_product l₁ l₂ → b ∈ l₂ | [] l₂ h := absurd h !not_mem_nil | (x::l₁) l₂ h := or.elim (mem_or_mem_of_mem_append h) (λ abin : (a, b) ∈ map (λ b, (x, b)) l₂, mem_of_mem_map_pair₁ abin) (λ abin : (a, b) ∈ cross_product l₁ l₂, mem_of_mem_cross_product_right abin) end cross_product end list attribute list.decidable_any [instance] attribute list.decidable_all [instance]
580b67e3176dacd8caa8915bc27c5b96712f7687
8b9f17008684d796c8022dab552e42f0cb6fb347
/library/data/rat/default.lean
b88e6b49af43aa95327c15287a819dd6035e4fa9
[ "Apache-2.0" ]
permissive
chubbymaggie/lean
0d06ae25f9dd396306fb02190e89422ea94afd7b
d2c7b5c31928c98f545b16420d37842c43b4ae9a
refs/heads/master
1,611,313,622,901
1,430,266,839,000
1,430,267,083,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
199
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Module: data.rat.default Author: Jeremy Avigad -/ import .basic
a862a7d9f951880754f88b6d704f0ddc8e993fb8
54f4ad05b219d444b709f56c2f619dd87d14ec29
/my_project/src/love13_rational_and_real_numbers_demo.lean
9a453b73470bdb5fd599c1dcd7b67323d5cf16a4
[]
no_license
yizhou7/learning-lean
8efcf838c7276e235a81bd291f467fa43ce56e0a
91fb366c624df6e56e19555b2e482ce767cd8224
refs/heads/master
1,675,649,087,737
1,609,022,281,000
1,609,022,281,000
272,072,779
0
0
null
null
null
null
UTF-8
Lean
false
false
14,036
lean
import .lovelib /- # LoVe Demo 13: Rational and Real Numbers We review the construction of `ℚ` and `ℝ` as quotient types. A procedure to construct types with specific properties: 1. Create a new type that can represent all elements, but not necessarily in a unique manner. 2. Quotient this representation, equating elements that should be equal. 3. Define operators on the quotient type by lifting functions from the base type and prove that they are compatible with the quotient relation. We used this approach in lecture 11 to construct `ℤ`. It can be used for `ℚ` and `ℝ` as well. -/ set_option pp.beta true set_option pp.generalized_field_notation false namespace LoVe /- ## Rational Numbers **Step 1:** A rational number is a number that can be expressed as a fraction `n / d` of integers `n` and `d ≠ 0`: -/ structure fraction := (num : ℤ) (denom : ℤ) (denom_ne_zero : denom ≠ 0) /- The number `n` is called the numerator, and the number `d` is called the denominator. The representation of a rational number as a fraction is not unique—e.g., `1 / 2 = 2 / 4 = -1 / -2`. **Step 2:** Two fractions `n₁ / d₁` and `n₂ / d₂` represent the same rational number if the ratio between numerator and denominator are the same—i.e., `n₁ * d₂ = n₂ * d₁`. This will be our equivalence relation `≈` on fractions. -/ namespace fraction @[instance] def setoid : setoid fraction := { r := λa b : fraction, num a * denom b = num b * denom a, iseqv := begin repeat { apply and.intro }, { intros a; refl }, { intros a b h; cc }, { intros a b c eq_ab eq_bc, apply int.eq_of_mul_eq_mul_right (denom_ne_zero b), cc } end } lemma setoid_iff (a b : fraction) : a ≈ b ↔ num a * denom b = num b * denom a := by refl /- **Step 3:** Define `0 := 0 / 1`, `1 := 1 / 1`, addition, multiplication, etc. `n₁ / d₁ + n₂ / d₂` := `(n₁ * d₂ + n₂ * d₁) / (d₁ * d₂)` `(n₁ / d₁) * (n₂ / d₂)` := `(n₁ * n₂) / (d₁ * d₂)` Then show that they are compatible with `≈`. -/ def of_int (i : ℤ) : fraction := { num := i, denom := 1, denom_ne_zero := by simp } @[instance] def has_zero : has_zero fraction := { zero := of_int 0 } @[instance] def has_one : has_one fraction := { one := of_int 1 } @[instance] def has_add : has_add fraction := { add := λa b : fraction, { num := num a * denom b + num b * denom a, denom := denom a * denom b, denom_ne_zero := by apply mul_ne_zero; exact denom_ne_zero _ } } @[simp] lemma add_num (a b : fraction) : num (a + b) = num a * denom b + num b * denom a := by refl @[simp] lemma add_denom (a b : fraction) : denom (a + b) = denom a * denom b := by refl lemma add_equiv_add {a a' b b' : fraction} (ha : a ≈ a') (hb : b ≈ b') : a + b ≈ a' + b' := begin simp [setoid_iff, add_denom, add_num] at *, calc (num a * denom b + num b * denom a) * (denom a' * denom b') = num a * denom a' * denom b * denom b' + num b * denom b' * denom a * denom a' : by simp [add_mul, mul_add]; ac_refl ... = num a' * denom a * denom b * denom b' + num b' * denom b * denom a * denom a' : by simp [*] ... = (num a' * denom b' + num b' * denom a') * (denom a * denom b) : by simp [add_mul, mul_add]; ac_refl end @[instance] def has_neg : has_neg fraction := { neg := λa : fraction, { num := - num a, ..a } } @[simp] lemma neg_num (a : fraction) : num (- a) = - num a := by refl @[simp] lemma neg_denom (a : fraction) : denom (- a) = denom a := by refl lemma setoid_neg {a a' : fraction} (hab : a ≈ a') : - a ≈ - a' := by simp [setoid_iff] at hab ⊢; exact hab @[instance] def has_mul : has_mul fraction := { mul := λa b : fraction, { num := num a * num b, denom := denom a * denom b, denom_ne_zero := mul_ne_zero (denom_ne_zero a) (denom_ne_zero b) } } @[simp] lemma mul_num (a b : fraction) : num (a * b) = num a * num b := by refl @[simp] lemma mul_denom (a b : fraction) : denom (a * b) = denom a * denom b := by refl lemma setoid_mul {a a' b b' : fraction} (ha : a ≈ a') (hb : b ≈ b') : a * b ≈ a' * b' := by simp [setoid_iff] at ha hb ⊢; cc @[instance] def has_inv : has_inv fraction := { inv := λa : fraction, if ha : num a = 0 then 0 else { num := denom a, denom := num a, denom_ne_zero := ha } } lemma inv_def (a : fraction) (ha : num a ≠ 0) : a⁻¹ = { num := denom a, denom := num a, denom_ne_zero := ha } := dif_neg ha lemma inv_zero (a : fraction) (ha : num a = 0) : a⁻¹ = 0 := dif_pos ha @[simp] lemma inv_num (a : fraction) (ha : num a ≠ 0) : num (a⁻¹) = denom a := by rw inv_def a ha @[simp] lemma inv_denom (a : fraction) (ha : num a ≠ 0) : denom (a⁻¹) = num a := by rw inv_def a ha lemma setoid_inv {a a' : fraction} (ha : a ≈ a') : a⁻¹ ≈ a'⁻¹ := begin cases classical.em (num a = 0), case or.inl : ha0 { cases classical.em (num a' = 0), case or.inl : ha'0 { simp [ha0, ha'0, inv_zero] }, case or.inr : ha'0 { simp [ha0, ha'0, setoid_iff, denom_ne_zero] at ha, cc } }, case or.inr : ha0 { cases classical.em (num a' = 0), case or.inl : ha'0 { simp [setoid_iff, ha'0, denom_ne_zero] at ha, cc }, case or.inr : ha'0 { simp [setoid_iff, ha0, ha'0] at ha ⊢, cc } } end end fraction def rat : Type := quotient fraction.setoid namespace rat @[instance] def has_zero : has_zero rat := { zero := ⟦0⟧ } @[instance] def has_one : has_one rat := { one := ⟦1⟧ } @[instance] def has_add : has_add rat := { add := quotient.lift₂ (λa b : fraction, ⟦a + b⟧) begin intros a b a' b' ha hb, apply quotient.sound, exact fraction.add_equiv_add ha hb end } @[instance] def has_neg : has_neg rat := { neg := quotient.lift (λa : fraction, ⟦- a⟧) begin intros a a' ha, apply quotient.sound, exact fraction.setoid_neg ha end } @[instance] def has_mul : has_mul rat := { mul := quotient.lift₂ (λa b : fraction, ⟦a * b⟧) begin intros a b a' b' ha hb, apply quotient.sound, exact fraction.setoid_mul ha hb end } @[instance] def has_inv : has_inv rat := { inv := quotient.lift (λa : fraction, ⟦a⁻¹⟧) begin intros a a' ha, apply quotient.sound, exact fraction.setoid_inv ha end } def a : fraction := {num := 1, denom := 2, denom_ne_zero := sorry} end rat /- ### Alternative Definitions of `ℚ` **Alternative 1:** Define `ℚ` as a subtype of `fraction`, with the requirement that the numerator and the denominator have no common divisors except `1` and `-1`: -/ namespace alternative_1 def rat.is_canonical (a : fraction) : Prop := nat.coprime (int.nat_abs (fraction.num a)) (int.nat_abs (fraction.denom a)) def rat := {a : fraction // rat.is_canonical a} end alternative_1 /- This is more or less the `mathlib` definition. Advantages: * no quotient required; * more efficient computation; * more properties are syntactic equalities up to computation. Disadvantage: * more complicated function definitions. **Alternative 2**: Define all elements syntactically, including the desired operations: -/ namespace alternative_2 inductive pre_rat : Type | zero : pre_rat | one : pre_rat | add : pre_rat → pre_rat → pre_rat | sub : pre_rat → pre_rat → pre_rat | mul : pre_rat → pre_rat → pre_rat | div : pre_rat → pre_rat → pre_rat /- Then quotient `pre_rat` to enforce congruence rules and the field axioms: -/ inductive rat.rel : pre_rat → pre_rat → Prop | add_congr {a b c d : pre_rat} : rat.rel a b → rat.rel c d → rat.rel (pre_rat.add a c) (pre_rat.add b d) | add_assoc {a b c : pre_rat} : rat.rel (pre_rat.add a (pre_rat.add b c)) (pre_rat.add (pre_rat.add a b) c) | zero_add {a : pre_rat} : rat.rel (pre_rat.add pre_rat.zero a) a -- etc. def rat : Type := quot rat.rel end alternative_2 /- Advantages: * no dependency on `ℤ`; * easy proofs of the field axioms; * general recipe reusable for other algebraic constructions (e.g., free monoids, free groups). Disadvantage: * the definition of orders and lemmas about them are more complicated. ### Real Numbers Some sequences of rational numbers seem to converge because the numbers in the sequence get closer and closer to each other, and yet do not converge to a rational number. Example: `a₀ = 1` `a₁ = 1.4` `a₂ = 1.41` `a₃ = 1.414` `a₄ = 1.4142` `a₅ = 1.41421` `a₆ = 1.414213` `a₇ = 1.4142135` ⋮ This sequence seems to converge because each `a_n` is at most `10^-n` away from any of the following numbers. But the limit is `√2`, which is not a rational number. The rational numbers are incomplete, and the reals are their __completion__. To construct the reals, we need to fill in the gaps that are revealed by these sequences that seem to converge, but do not. Mathematically, a sequence `a₀, a₁, …` of rational numbers is __Cauchy__ if for any `ε > 0`, there exists an `N ∈ ℕ` such that for all `m ≥ N`, we have `|a_N - a_m| < ε`. In other words, no matter how small we choose `ε`, we can always find a point in the sequence from which all following numbers deviate less than by `ε`. -/ def is_cau_seq (f : ℕ → ℚ) : Prop := ∀ε > 0, ∃N, ∀m ≥ N, abs (f N - f m) < ε /- Not every sequence is a Cauchy sequence: -/ lemma id_not_cau_seq : ¬ is_cau_seq (λn : ℕ, (n : ℚ)) := begin rw is_cau_seq, intro h, cases h 1 zero_lt_one with i hi, have hi_succi := hi (i + 1) (by simp), simp [←sub_sub] at hi_succi, linarith end /- We define a type of Cauchy sequences as a subtype: -/ def cau_seq : Type := {f : ℕ → ℚ // is_cau_seq f} def seq_of (f : cau_seq) : ℕ → ℚ := subtype.val f /- Cauchy sequences represent real numbers: * `a_n = 1 / n` represents the real number `0`; * `1, 1.4, 1.41, …` represents the real number `√2`; * `a_n = 0` also represents the real number `0`. Since different Cauchy sequences can represent the same real number, we need to take the quotient. Formally, two sequences represent the same real number when their difference converges to zero: -/ namespace cau_seq @[instance] def setoid : setoid cau_seq := { r := λf g : cau_seq, ∀ε > 0, ∃N, ∀m ≥ N, abs (seq_of f m - seq_of g m) < ε, iseqv := begin apply and.intro, { intros f ε hε, apply exists.intro 0, finish }, apply and.intro, { intros f g hfg ε hε, cases hfg ε hε with N hN, apply exists.intro N, intros m hm, rw abs_sub, apply hN m hm }, { intros f g h hfg hgh ε hε, cases hfg (ε / 2) (half_pos hε) with N₁ hN₁, cases hgh (ε / 2) (half_pos hε) with N₂ hN₂, apply exists.intro (max N₁ N₂), intros m hm, calc abs (seq_of f m - seq_of h m) ≤ abs (seq_of f m - seq_of g m) + abs (seq_of g m - seq_of h m) : by apply abs_sub_le ... < ε / 2 + ε / 2 : add_lt_add (hN₁ m (le_of_max_le_left hm)) (hN₂ m (le_of_max_le_right hm)) ... = ε : by simp } end } lemma setoid_iff (f g : cau_seq) : f ≈ g ↔ ∀ε > 0, ∃N, ∀m ≥ N, abs (seq_of f m - seq_of g m) < ε := by refl /- We can define constants such as `0` and `1` as a constant sequence. Any constant sequence is a Cauchy sequence: -/ def const (q : ℚ) : cau_seq := subtype.mk (λ_ : ℕ, q) (by rw is_cau_seq; intros ε hε; finish) /- Defining addition of real numbers requires a little more effort. We define addition on Cauchy sequences as pairwise addition: -/ @[instance] def has_add : has_add cau_seq := { add := λf g : cau_seq, subtype.mk (λn : ℕ, seq_of f n + seq_of g n) sorry } /- Above, we omit the proof that the addition of two Cauchy sequences is again a Cauchy sequence. Next, we need to show that this addition is compatible with `≈`: -/ lemma add_equiv_add {f f' g g' : cau_seq} (hf : f ≈ f') (hg : g ≈ g') : f + g ≈ f' + g' := begin intros ε₀ hε₀, simp [setoid_iff], cases hf (ε₀ / 2) (half_pos hε₀) with Nf hNf, cases hg (ε₀ / 2) (half_pos hε₀) with Ng hNg, apply exists.intro (max Nf Ng), intros m hm, calc abs (seq_of (f + g) m - seq_of (f' + g') m) = abs ((seq_of f m + seq_of g m) - (seq_of f' m + seq_of g' m)) : by refl ... = abs ((seq_of f m - seq_of f' m) + (seq_of g m - seq_of g' m)) : begin have arg_eq : seq_of f m + seq_of g m - (seq_of f' m + seq_of g' m) = seq_of f m - seq_of f' m + (seq_of g m - seq_of g' m), by linarith, rw arg_eq end ... ≤ abs (seq_of f m - seq_of f' m) + abs (seq_of g m - seq_of g' m) : by apply abs_add ... < ε₀ / 2 + ε₀ / 2 : add_lt_add (hNf m (le_of_max_le_left hm)) (hNg m (le_of_max_le_right hm)) ... = ε₀ : by simp end end cau_seq /- The real numbers are the quotient: -/ def real : Type := quotient cau_seq.setoid namespace real @[instance] def has_zero : has_zero real := { zero := ⟦cau_seq.const 0⟧ } @[instance] def has_one : has_one real := { one := ⟦cau_seq.const 1⟧ } @[instance] def has_add : has_add real := { add := quotient.lift₂ (λa b : cau_seq, ⟦a + b⟧) begin intros a b a' b' ha hb, apply quotient.sound, exact cau_seq.add_equiv_add ha hb, end } end real /- ### Alternative Definitions of `ℝ` * Dedekind cuts: `r : ℝ` is represented essentially as `{x : ℚ | x < r}`. * Binary sequences `ℕ → bool` can represent the interval `[0, 1]`. This can be used to build `ℝ`. -/ end LoVe
b8fb0fbec7f971a4f4bad659fb75c35e078d08d6
a7602958ab456501ff85db8cf5553f7bcab201d7
/Notes/Logic_and_Proof/Chapter9/9.11.lean
093a9af8f7367bbba34941e0cac1a7cffa276cc8
[]
no_license
enlauren/math-logic
081e2e737c8afb28dbb337968df95ead47321ba0
086b6935543d1841f1db92d0e49add1124054c37
refs/heads/master
1,594,506,621,950
1,558,634,976,000
1,558,634,976,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,242
lean
-- 9.1 ...continued #check nat -- This represents the natural numbers. #check ℕ -- Same as above. -- Can use this to prevent name collisions. namespace hidden -- Define our terms. constant mul: ℕ -> ℕ -> ℕ constant add: ℕ -> ℕ -> ℕ constant square: ℕ -> ℕ -- Define some formulas. constant even: ℕ -> Prop constant odd: ℕ -> Prop constant prime: ℕ -> Prop constant divides: ℕ -> ℕ -> Prop constant lt: ℕ -> ℕ -> Prop -- Less-than constant zero: ℕ constant one: ℕ -- So...I guess these don't have "values"? variables w x y z: ℕ -- If we were outside of the namespace |hidden|, we'd have to prefix hidden's -- members with |hidden.| before using them, as follows: #check hidden.mul x y #check hidden.odd hidden.zero #check hidden.prime (hidden.add x y) -- Clearly this works OK in the namespace as well. ------------------------------------ -- "We can even declare infix notation of binary operations and relations". -- These essentially bind some previously-defined constant infix + := hidden.add infix * := mul infix < := lt -- For some reason I can't figure out how to use the infix notation. #check add x y #check add x (add y z) #check even (add x (add y z)) /\ prime (square z) end hidden
a02d0bcc02a3eb352f61054a182d660a653764fb
947b78d97130d56365ae2ec264df196ce769371a
/tests/lean/run/structuralRec1.lean
743536237d6aa68c43b570c0954f1759eeb39ab5
[ "Apache-2.0" ]
permissive
shyamalschandra/lean4
27044812be8698f0c79147615b1d5090b9f4b037
6e7a883b21eaf62831e8111b251dc9b18f40e604
refs/heads/master
1,671,417,126,371
1,601,859,995,000
1,601,860,020,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,254
lean
new_frontend def map {α β} (f : α → β) : List α → List β | [] => [] | a::as => f a :: map f as theorem ex1 : map Nat.succ [1] = [2] := rfl theorem ex2 : map Nat.succ [] = [] := rfl theorem ex3 (a : Nat) : map Nat.succ [a] = [Nat.succ a] := rfl theorem ex4 {α β} (f : α → β) (a : α) (as : List α) : map f (a::as) = (f a) :: map f as := rfl theorem ex5 {α β} (f : α → β) : map f [] = [] := rfl def map2 {α β} (f : α → β) (as : List α) : List β := let rec loop : List α → List β | [] => [] | a::as => f a :: loop as; loop as theorem ex6 {α β} (f : α → β) (a : α) (as : List α) : map2 f (a::as) = (f a) :: map2 f as := rfl def foo (xs : List Nat) : List Nat := match xs with | [] => [] | x::xs => let y := 2 * x; match xs with | [] => [] | x::xs => (y + x) :: foo xs #eval foo [1, 2, 3, 4] theorem fooEq (x y : Nat) (xs : List Nat) : foo (x::y::xs) = (2*x + y) :: foo xs := rfl def bla (x : Nat) (ys : List Nat) : List Nat := if x % 2 == 0 then match ys with | [] => [] | y::ys => (y + x/2) :: bla (x/2) ys else match ys with | [] => [] | y::ys => (y + x/2 + 1) :: bla (x/2) ys theorem blaEq (y : Nat) (ys : List Nat) : bla 4 (y::ys) = (y+2) :: bla 2 ys := rfl
bc1339f657bbc005e18e711ecee50d6f9ee89643
82e44445c70db0f03e30d7be725775f122d72f3e
/src/combinatorics/simple_graph/subgraph.lean
26f9e133b1290d8518cc8d05baeeb1fb1acb90a2
[ "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,623
lean
/- Copyright (c) 2021 Hunter Monroe. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Hunter Monroe, Kyle Miller, Alena Gusakov -/ import combinatorics.simple_graph.basic /-! # Subgraphs of a simple graph A subgraph of a simple graph consists of subsets of the graph's vertices and edges such that the endpoints of each edge are present in the vertex subset. The edge subset is formalized as a sub-relation of the adjacency relation of the simple graph. ## Main definitions * `subgraph G` is the type of subgraphs of a `G : simple_graph` * `subgraph.neighbor_set`, `subgraph.incidence_set`, and `subgraph.degree` are like their `simple_graph` counterparts, but they refer to vertices from `G` to avoid subtype coercions. * `subgraph.coe` is the coercion from a `G' : subgraph G` to a `simple_graph G'.verts`. (This cannot be a `has_coe` instance since the destination type depends on `G'`.) * `subgraph.is_spanning` for whether a subgraph is a spanning subgraph and `subgraph.is_induced` for whether a subgraph is an induced subgraph. * A `bounded_lattice (subgraph G)` instance, under the `subgraph` relation. * `simple_graph.to_subgraph`: If a `simple_graph` is a subgraph of another, then you can turn it into a member of the larger graph's `simple_graph.subgraph` type. * Graph homomorphisms from a subgraph to a graph (`subgraph.map_top`) and between subgraphs (`subgraph.map`). ## Implementation notes * Recall that subgraphs are not determined by their vertex sets, so `set_like` does not apply to this kind of subobject. ## Todo * Images of graph homomorphisms as subgraphs. -/ universe u namespace simple_graph /-- A subgraph of a `simple_graph` is a subset of vertices along with a restriction of the adjacency relation that is symmetric and is supported by the vertex subset. They also form a bounded lattice. Thinking of `V → V → Prop` as `set (V × V)`, a set of darts (i.e., half-edges), then `subgraph.adj_sub` is that the darts of a subgraph are a subset of the darts of `G`. -/ @[ext] structure subgraph {V : Type u} (G : simple_graph V) := (verts : set V) (adj : V → V → Prop) (adj_sub : ∀ {v w : V}, adj v w → G.adj v w) (edge_vert : ∀ {v w : V}, adj v w → v ∈ verts) (sym : symmetric adj . obviously) namespace subgraph variables {V : Type u} {G : simple_graph V} lemma adj_comm (G' : subgraph G) (v w : V) : G'.adj v w ↔ G'.adj w v := ⟨λ x, G'.sym x, λ x, G'.sym x⟩ @[symm] lemma adj_symm (G' : subgraph G) {u v : V} (h : G'.adj u v) : G'.adj v u := G'.sym h /-- Coercion from `G' : subgraph G` to a `simple_graph ↥G'.verts`. -/ @[simps] def coe (G' : subgraph G) : simple_graph G'.verts := { adj := λ v w, G'.adj v w, sym := λ v w h, G'.sym h, loopless := λ v h, loopless G v (G'.adj_sub h) } @[simp] lemma coe_adj_sub (G' : subgraph G) (u v : G'.verts) (h : G'.coe.adj u v) : G.adj u v := G'.adj_sub h /-- A subgraph is called a *spanning subgraph* if it contains all the vertices of `G`. --/ def is_spanning (G' : subgraph G) : Prop := ∀ (v : V), v ∈ G'.verts /-- Coercion from `subgraph G` to `simple_graph V`. If `G'` is a spanning subgraph, then `G'.spanning_coe` yields an isomorphic graph. In general, this adds in all vertices from `V` as isolated vertices. -/ @[simps] def spanning_coe (G' : subgraph G) : simple_graph V := { adj := G'.adj, sym := G'.sym, loopless := λ v hv, G.loopless v (G'.adj_sub hv) } @[simp] lemma spanning_coe_adj_sub (H : subgraph G) (u v : H.verts) (h : H.spanning_coe.adj u v) : G.adj u v := H.adj_sub h /-- `spanning_coe` is equivalent to `coe` for a subgraph that `is_spanning`. -/ @[simps] def spanning_coe_equiv_coe_of_spanning (G' : subgraph G) (h : G'.is_spanning) : G'.spanning_coe ≃g G'.coe := { to_fun := λ v, ⟨v, h v⟩, inv_fun := λ v, v, left_inv := λ v, rfl, right_inv := λ ⟨v, hv⟩, rfl, map_rel_iff' := λ v w, iff.rfl } /-- A subgraph is called an *induced subgraph* if vertices of `G'` are adjacent if they are adjacent in `G`. -/ def is_induced (G' : subgraph G) : Prop := ∀ {v w : V}, v ∈ G'.verts → w ∈ G'.verts → G.adj v w → G'.adj v w /-- `G'.neighbor_set v` is the set of vertices adjacent to `v` in `G'`. -/ def neighbor_set (G' : subgraph G) (v : V) : set V := set_of (G'.adj v) lemma neighbor_set_subset (G' : subgraph G) (v : V) : G'.neighbor_set v ⊆ G.neighbor_set v := λ w h, G'.adj_sub h @[simp] lemma mem_neighbor_set (G' : subgraph G) (v w : V) : w ∈ G'.neighbor_set v ↔ G'.adj v w := iff.rfl /-- A subgraph as a graph has equivalent neighbor sets. -/ def coe_neighbor_set_equiv {G' : subgraph G} (v : G'.verts) : G'.coe.neighbor_set v ≃ G'.neighbor_set v := { to_fun := λ w, ⟨w, by { obtain ⟨w', hw'⟩ := w, simpa using hw' }⟩, inv_fun := λ w, ⟨⟨w, G'.edge_vert (G'.adj_symm w.2)⟩, by simpa using w.2⟩, left_inv := λ w, by simp, right_inv := λ w, by simp } /-- The edge set of `G'` consists of a subset of edges of `G`. -/ def edge_set (G' : subgraph G) : set (sym2 V) := sym2.from_rel G'.sym lemma edge_set_subset (G' : subgraph G) : G'.edge_set ⊆ G.edge_set := λ e, quotient.ind (λ e h, G'.adj_sub h) e @[simp] lemma mem_edge_set {G' : subgraph G} {v w : V} : ⟦(v, w)⟧ ∈ G'.edge_set ↔ G'.adj v w := iff.rfl lemma mem_verts_if_mem_edge {G' : subgraph G} {e : sym2 V} {v : V} (he : e ∈ G'.edge_set) (hv : v ∈ e) : v ∈ G'.verts := begin refine quotient.ind (λ e he hv, _) e he hv, cases e with v w, simp only [mem_edge_set] at he, cases sym2.mem_iff.mp hv with h h; subst h, { exact G'.edge_vert he, }, { exact G'.edge_vert (G'.sym he), }, end /-- The `incidence_set` is the set of edges incident to a given vertex. -/ def incidence_set (G' : subgraph G) (v : V) : set (sym2 V) := {e ∈ G'.edge_set | v ∈ e} lemma incidence_set_subset_incidence_set (G' : subgraph G) (v : V) : G'.incidence_set v ⊆ G.incidence_set v := λ e h, ⟨G'.edge_set_subset h.1, h.2⟩ lemma incidence_set_subset (G' : subgraph G) (v : V) : G'.incidence_set v ⊆ G'.edge_set := λ _ h, h.1 /-- Give a vertex as an element of the subgraph's vertex type. -/ @[reducible] def vert (G' : subgraph G) (v : V) (h : v ∈ G'.verts) : G'.verts := ⟨v, h⟩ /-- Create an equal copy of a subgraph (see `copy_eq`) with possibly different definitional equalities. See Note [range copy pattern]. -/ def copy (G' : subgraph G) (V'' : set V) (hV : V'' = G'.verts) (adj' : V → V → Prop) (hadj : adj' = G'.adj) : subgraph G := { verts := V'', adj := adj', adj_sub := hadj.symm ▸ G'.adj_sub, edge_vert := hV.symm ▸ hadj.symm ▸ G'.edge_vert, sym := hadj.symm ▸ G'.sym } lemma copy_eq (G' : subgraph G) (V'' : set V) (hV : V'' = G'.verts) (adj' : V → V → Prop) (hadj : adj' = G'.adj) : G'.copy V'' hV adj' hadj = G' := subgraph.ext _ _ hV hadj /-- The union of two subgraphs. -/ def union (x y : subgraph G) : subgraph G := { verts := x.verts ∪ y.verts, adj := x.adj ⊔ y.adj, adj_sub := λ v w h, or.cases_on h (λ h, x.adj_sub h) (λ h, y.adj_sub h), edge_vert := λ v w h, or.cases_on h (λ h, or.inl (x.edge_vert h)) (λ h, or.inr (y.edge_vert h)), sym := λ v w h, by rwa [sup_apply, sup_apply, x.adj_comm, y.adj_comm] } /-- The intersection of two subgraphs. -/ def inter (x y : subgraph G) : subgraph G := { verts := x.verts ∩ y.verts, adj := x.adj ⊓ y.adj, adj_sub := λ v w h, x.adj_sub h.1, edge_vert := λ v w h, ⟨x.edge_vert h.1, y.edge_vert h.2⟩, sym := λ v w h, by rwa [inf_apply, inf_apply, x.adj_comm, y.adj_comm] } /-- The `top` subgraph is `G` as a subgraph of itself. -/ def top : subgraph G := { verts := set.univ, adj := G.adj, adj_sub := λ v w h, h, edge_vert := λ v w h, set.mem_univ v, sym := G.sym } /-- The `bot` subgraph is the subgraph with no vertices or edges. -/ def bot : subgraph G := { verts := ∅, adj := λ v w, false, adj_sub := λ v w h, false.rec _ h, edge_vert := λ v w h, false.rec _ h, sym := λ u v h, h } instance subgraph_inhabited : inhabited (subgraph G) := ⟨bot⟩ /-- The relation that one subgraph is a subgraph of another. -/ def is_subgraph (x y : subgraph G) : Prop := x.verts ⊆ y.verts ∧ ∀ ⦃v w : V⦄, x.adj v w → y.adj v w instance : bounded_lattice (subgraph G) := { le := is_subgraph, sup := union, inf := inter, top := top, bot := bot, le_refl := λ x, ⟨rfl.subset, λ _ _ h, h⟩, le_trans := λ x y z hxy hyz, ⟨hxy.1.trans hyz.1, λ _ _ h, hyz.2 (hxy.2 h)⟩, le_antisymm := begin intros x y hxy hyx, ext1 v, exact set.subset.antisymm hxy.1 hyx.1, ext v w, exact iff.intro (λ h, hxy.2 h) (λ h, hyx.2 h), end, le_top := λ x, ⟨set.subset_univ _, (λ v w h, x.adj_sub h)⟩, bot_le := λ x, ⟨set.empty_subset _, (λ v w h, false.rec _ h)⟩, sup_le := λ x y z hxy hyz, ⟨set.union_subset hxy.1 hyz.1, (λ v w h, h.cases_on (λ h, hxy.2 h) (λ h, hyz.2 h))⟩, le_sup_left := λ x y, ⟨set.subset_union_left x.verts y.verts, (λ v w h, or.inl h)⟩, le_sup_right := λ x y, ⟨set.subset_union_right x.verts y.verts, (λ v w h, or.inr h)⟩, le_inf := λ x y z hxy hyz, ⟨set.subset_inter hxy.1 hyz.1, (λ v w h, ⟨hxy.2 h, hyz.2 h⟩)⟩, inf_le_left := λ x y, ⟨set.inter_subset_left x.verts y.verts, (λ v w h, h.1)⟩, inf_le_right := λ x y, ⟨set.inter_subset_right x.verts y.verts, (λ v w h, h.2)⟩ } /-- Turn a subgraph of a `simple_graph` into a member of its subgraph type. -/ @[simps] def _root_.simple_graph.to_subgraph (H : simple_graph V) (h : H ≤ G) : G.subgraph := { verts := set.univ, adj := H.adj, adj_sub := h, edge_vert := λ v w h, set.mem_univ v, sym := H.sym } lemma _root_.simple_graph.to_subgraph.is_spanning (H : simple_graph V) (h : H ≤ G) : (H.to_subgraph h).is_spanning := set.mem_univ lemma spanning_coe.is_subgraph_of_is_subgraph {H H' : subgraph G} (h : H ≤ H') : H.spanning_coe ≤ H'.spanning_coe := h.2 /-- The top of the `subgraph G` lattice is equivalent to the graph itself. -/ def top_equiv : (⊤ : subgraph G).coe ≃g G := { to_fun := λ v, ↑v, inv_fun := λ v, ⟨v, trivial⟩, left_inv := λ ⟨v, _⟩, rfl, right_inv := λ v, rfl, map_rel_iff' := λ a b, iff.rfl } /-- The bottom of the `subgraph G` lattice is equivalent to the empty graph on the empty vertex type. -/ def bot_equiv : (⊥ : subgraph G).coe ≃g (⊥ : simple_graph empty) := { to_fun := λ v, v.property.elim, inv_fun := λ v, v.elim, left_inv := λ ⟨_, h⟩, h.elim, right_inv := λ v, v.elim, map_rel_iff' := λ a b, iff.rfl } /-- Given two subgraphs, one a subgraph of the other, there is an induced injective homomorphism of the subgraphs as graphs. -/ def map {x y : subgraph G} (h : x ≤ y) : x.coe →g y.coe := { to_fun := λ v, ⟨↑v, and.left h v.property⟩, map_rel' := λ v w hvw, h.2 hvw } lemma map.injective {x y : subgraph G} (h : x ≤ y) : function.injective (map h) := λ v w h, by { simp only [map, rel_hom.coe_fn_mk, subtype.mk_eq_mk] at h, exact subtype.ext h } /-- There is an induced injective homomorphism of a subgraph of `G` into `G`. -/ def map_top (x : subgraph G) : x.coe →g G := { to_fun := λ v, v, map_rel' := λ v w hvw, x.adj_sub hvw } lemma map_top.injective {x : subgraph G} : function.injective x.map_top := λ v w h, subtype.ext h @[simp] lemma map_top_to_fun {x : subgraph G} (v : x.verts) : x.map_top v = v := rfl lemma neighbor_set_subset_of_subgraph {x y : subgraph G} (h : x ≤ y) (v : V) : x.neighbor_set v ⊆ y.neighbor_set v := λ w h', h.2 h' instance neighbor_set.decidable_pred (G' : subgraph G) [h : decidable_rel G'.adj] (v : V) : decidable_pred (∈ G'.neighbor_set v) := h v /-- If a graph is locally finite at a vertex, then so is a subgraph of that graph. -/ instance finite_at {G' : subgraph G} (v : G'.verts) [decidable_rel G'.adj] [fintype (G.neighbor_set v)] : fintype (G'.neighbor_set v) := set.fintype_subset (G.neighbor_set v) (G'.neighbor_set_subset v) /-- If a subgraph is locally finite at a vertex, then so are subgraphs of that subgraph. This is not an instance because `G''` cannot be inferred. -/ def finite_at_of_subgraph {G' G'' : subgraph G} [decidable_rel G'.adj] (h : G' ≤ G'') (v : G'.verts) [hf : fintype (G''.neighbor_set v)] : fintype (G'.neighbor_set v) := set.fintype_subset (G''.neighbor_set v) (neighbor_set_subset_of_subgraph h v) instance coe_finite_at {G' : subgraph G} (v : G'.verts) [fintype (G'.neighbor_set v)] : fintype (G'.coe.neighbor_set v) := fintype.of_equiv _ (coe_neighbor_set_equiv v).symm /-- The degree of a vertex in a subgraph. Is zero for vertices outside the subgraph. -/ def degree (G' : subgraph G) (v : V) [fintype (G'.neighbor_set v)] : ℕ := fintype.card (G'.neighbor_set v) lemma degree_le (G' : subgraph G) (v : V) [fintype (G'.neighbor_set v)] [fintype (G.neighbor_set v)] : G'.degree v ≤ G.degree v := begin rw ←card_neighbor_set_eq_degree, exact set.card_le_of_subset (G'.neighbor_set_subset v), end lemma degree_le' (G' G'' : subgraph G) (h : G' ≤ G'') (v : V) [fintype (G'.neighbor_set v)] [fintype (G''.neighbor_set v)] : G'.degree v ≤ G''.degree v := set.card_le_of_subset (neighbor_set_subset_of_subgraph h v) @[simp] lemma coe_degree (G' : subgraph G) (v : G'.verts) [fintype (G'.coe.neighbor_set v)] [fintype (G'.neighbor_set v)] : G'.coe.degree v = G'.degree v := begin rw ←card_neighbor_set_eq_degree, exact fintype.card_congr (coe_neighbor_set_equiv v), end end subgraph end simple_graph
c68cf52b63149bb2392f021f9f51d760f1049fd7
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/order/lexicographic.lean
b2da732226a5dec372a9e107fd99930aeb7dc8bd
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
8,539
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Minchao Wu -/ import tactic.basic /-! # Lexicographic order This file defines the lexicographic relation for pairs and dependent pairs of orders, partial orders and linear orders. ## Main declarations * `lex α β`: Synonym of `α × β` to equip it with lexicographic order without creating conflicting instances. * `lex_<pre/partial_/linear_>order`: Instances lifting the orders on `α` and `β` to `lex α β` * `dlex_<pre/partial_/linear_>order`: Instances lifting the orders on every `Z a` to the dependent pair `Z`. -/ universes u v /-- The cartesian product, equipped with the lexicographic order. -/ def lex (α : Type u) (β : Type v) := α × β variables {α : Type u} {β : Type v} instance [decidable_eq α] [decidable_eq β] : decidable_eq (lex α β) := prod.decidable_eq instance [inhabited α] [inhabited β] : inhabited (lex α β) := prod.inhabited /-- Dictionary / lexicographic ordering on pairs. -/ instance lex_has_le [preorder α] [preorder β] : has_le (lex α β) := { le := prod.lex (<) (≤) } instance lex_has_lt [preorder α] [preorder β] : has_lt (lex α β) := { lt := prod.lex (<) (<) } /-- Dictionary / lexicographic preorder for pairs. -/ instance lex_preorder [preorder α] [preorder β] : preorder (lex α β) := { le_refl := λ ⟨l, r⟩, by { right, apply le_refl }, le_trans := begin rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩ ⟨a₃, b₃⟩ ⟨h₁l, h₁r⟩ ⟨h₂l, h₂r⟩, { left, apply lt_trans, repeat { assumption } }, { left, assumption }, { left, assumption }, { right, apply le_trans, repeat { assumption } } end, lt_iff_le_not_le := begin rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩, split, { rintros (⟨_, _, _, _, hlt⟩ | ⟨_, _, _, hlt⟩), { split, { left, assumption }, { rintro ⟨l,r⟩, { apply lt_asymm hlt, assumption }, { apply lt_irrefl _ hlt } } }, { split, { right, rw lt_iff_le_not_le at hlt, exact hlt.1 }, { rintro ⟨l,r⟩, { apply lt_irrefl a₁, assumption }, { rw lt_iff_le_not_le at hlt, apply hlt.2, assumption } } } }, { rintros ⟨⟨h₁ll, h₁lr⟩, h₂r⟩, { left, assumption }, { right, rw lt_iff_le_not_le, split, { assumption }, { intro h, apply h₂r, right, exact h } } } end, .. lex_has_le, .. lex_has_lt } /-- Dictionary / lexicographic partial_order for pairs. -/ instance lex_partial_order [partial_order α] [partial_order β] : partial_order (lex α β) := { le_antisymm := begin rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩ (⟨_, _, _, _, hlt₁⟩ | ⟨_, _, _, hlt₁⟩) (⟨_, _, _, _, hlt₂⟩ | ⟨_, _, _, hlt₂⟩), { exfalso, exact lt_irrefl a₁ (lt_trans hlt₁ hlt₂) }, { exfalso, exact lt_irrefl a₁ hlt₁ }, { exfalso, exact lt_irrefl a₁ hlt₂ }, { have := le_antisymm hlt₁ hlt₂, simp [this] } end .. lex_preorder } /-- Dictionary / lexicographic linear_order for pairs. -/ instance lex_linear_order [linear_order α] [linear_order β] : linear_order (lex α β) := { le_total := begin rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩, obtain ha | ha := le_total a₁ a₂; cases lt_or_eq_of_le ha with a_lt a_eq, -- Deal with the two goals with a₁ ≠ a₂ { left, left, exact a_lt }, swap, { right, left, exact a_lt }, -- Now deal with the two goals with a₁ = a₂ all_goals { subst a_eq, obtain hb | hb := le_total b₁ b₂ }, { left, right, exact hb }, { right, right, exact hb }, { left, right, exact hb }, { right, right, exact hb }, end, decidable_le := begin rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩, obtain a_lt | a_le := linear_order.decidable_le a₁ a₂, { -- a₂ < a₁ left, rw not_le at a_lt, rintro ⟨l, r⟩, { apply lt_irrefl a₂, apply lt_trans, repeat { assumption } }, { apply lt_irrefl a₁, assumption } }, { -- a₁ ≤ a₂ by_cases h : a₁ = a₂, { rw h, obtain b_lt | b_le := linear_order.decidable_le b₁ b₂, { -- b₂ < b₁ left, rw not_le at b_lt, rintro ⟨l, r⟩, { apply lt_irrefl a₂, assumption }, { apply lt_irrefl b₂, apply lt_of_lt_of_le, repeat { assumption } } }, -- b₁ ≤ b₂ { right, right, assumption } }, -- a₁ < a₂ { right, left, apply lt_of_le_of_ne, repeat { assumption } } } end, .. lex_partial_order }. variables {Z : α → Type v} /-- Dictionary / lexicographic ordering on dependent pairs. The 'pointwise' partial order `prod.has_le` doesn't make sense for dependent pairs, so it's safe to mark these as instances here. -/ instance dlex_has_le [preorder α] [∀ a, preorder (Z a)] : has_le (Σ' a, Z a) := { le := psigma.lex (<) (λ a, (≤)) } instance dlex_has_lt [preorder α] [∀ a, preorder (Z a)] : has_lt (Σ' a, Z a) := { lt := psigma.lex (<) (λ a, (<)) } /-- Dictionary / lexicographic preorder on dependent pairs. -/ instance dlex_preorder [preorder α] [∀ a, preorder (Z a)] : preorder (Σ' a, Z a) := { le_refl := λ ⟨l, r⟩, by { right, apply le_refl }, le_trans := begin rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩ ⟨a₃, b₃⟩ ⟨h₁l, h₁r⟩ ⟨h₂l, h₂r⟩, { left, apply lt_trans, repeat { assumption } }, { left, assumption }, { left, assumption }, { right, apply le_trans, repeat { assumption } } end, lt_iff_le_not_le := begin rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩, split, { rintros (⟨_, _, _, _, hlt⟩ | ⟨_, _, _, hlt⟩), { split, { left, assumption }, { rintro ⟨l,r⟩, { apply lt_asymm hlt, assumption }, { apply lt_irrefl _ hlt } } }, { split, { right, rw lt_iff_le_not_le at hlt, exact hlt.1 }, { rintro ⟨l,r⟩, { apply lt_irrefl a₁, assumption }, { rw lt_iff_le_not_le at hlt, apply hlt.2, assumption } } } }, { rintros ⟨⟨h₁ll, h₁lr⟩, h₂r⟩, { left, assumption }, { right, rw lt_iff_le_not_le, split, { assumption }, { intro h, apply h₂r, right, exact h } } } end, .. dlex_has_le, .. dlex_has_lt } /-- Dictionary / lexicographic partial_order for dependent pairs. -/ instance dlex_partial_order [partial_order α] [∀ a, partial_order (Z a)] : partial_order (Σ' a, Z a) := { le_antisymm := begin rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩ (⟨_, _, _, _, hlt₁⟩ | ⟨_, _, _, hlt₁⟩) (⟨_, _, _, _, hlt₂⟩ | ⟨_, _, _, hlt₂⟩), { exfalso, exact lt_irrefl a₁ (lt_trans hlt₁ hlt₂) }, { exfalso, exact lt_irrefl a₁ hlt₁ }, { exfalso, exact lt_irrefl a₁ hlt₂ }, { have := le_antisymm hlt₁ hlt₂, simp [this] } end .. dlex_preorder } /-- Dictionary / lexicographic linear_order for pairs. -/ instance dlex_linear_order [linear_order α] [∀ a, linear_order (Z a)] : linear_order (Σ' a, Z a) := { le_total := begin rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩, obtain ha | ha := le_total a₁ a₂; cases lt_or_eq_of_le ha with a_lt a_eq, -- Deal with the two goals with a₁ ≠ a₂ { left, left, exact a_lt }, swap, { right, left, exact a_lt }, -- Now deal with the two goals with a₁ = a₂ all_goals { subst a_eq, obtain hb | hb := le_total b₁ b₂ }, { left, right, exact hb }, { right, right, exact hb }, { left, right, exact hb }, { right, right, exact hb }, end, decidable_le := begin rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩, obtain a_lt | a_le := linear_order.decidable_le a₁ a₂, { -- a₂ < a₁ left, rw not_le at a_lt, rintro ⟨l, r⟩, { apply lt_irrefl a₂, apply lt_trans, repeat { assumption } }, { apply lt_irrefl a₁, assumption } }, { -- a₁ ≤ a₂ by_cases h : a₁ = a₂, { subst h, obtain b_lt | b_le := linear_order.decidable_le b₁ b₂, { -- b₂ < b₁ left, rw not_le at b_lt, rintro ⟨l, r⟩, { apply lt_irrefl a₁, assumption }, { apply lt_irrefl b₂, apply lt_of_lt_of_le, repeat { assumption } } }, -- b₁ ≤ b₂ { right, right, assumption } }, -- a₁ < a₂ { right, left, apply lt_of_le_of_ne, repeat { assumption } } } end, .. dlex_partial_order }.
89b633fb7af5696d0507450214583a382aaea0c2
94637389e03c919023691dcd05bd4411b1034aa5
/src/inClassNotes/type_library/sum.lean
a208579b7cda8e7aec24783c4f682ef186045299
[]
no_license
kevinsullivan/complogic-s21
7c4eef2105abad899e46502270d9829d913e8afc
99039501b770248c8ceb39890be5dfe129dc1082
refs/heads/master
1,682,985,669,944
1,621,126,241,000
1,621,126,241,000
335,706,272
0
38
null
1,618,325,669,000
1,612,374,118,000
Lean
UTF-8
Lean
false
false
145
lean
namespace hidden universe u inductive sum (α β : Type u) : Type u | inl (a : α) : sum | inr (b : β) : sum end hidden #check sum nat Type
13192da36ac80d01b0d48d988241e356d9b5db73
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/src/combinatorics/simple_graph/degree_sum.lean
be8db2653e04b90165698eec453bd8ba7f580ca5
[ "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
8,327
lean
/- Copyright (c) 2020 Kyle Miller. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kyle Miller -/ import combinatorics.simple_graph.basic import algebra.big_operators.basic import data.nat.parity import data.zmod.parity /-! # Degree-sum formula and handshaking lemma The degree-sum formula is that the sum of the degrees of the vertices in a finite graph is equal to twice the number of edges. The handshaking lemma, a corollary, is that the number of odd-degree vertices is even. ## Main definitions - A `dart` is a directed edge, consisting of an ordered pair of adjacent vertices, thought of as being a directed edge. - `simple_graph.sum_degrees_eq_twice_card_edges` is the degree-sum formula. - `simple_graph.even_card_odd_degree_vertices` is the handshaking lemma. - `simple_graph.odd_card_odd_degree_vertices_ne` is that the number of odd-degree vertices different from a given odd-degree vertex is odd. - `simple_graph.exists_ne_odd_degree_of_exists_odd_degree` is that the existence of an odd-degree vertex implies the existence of another one. ## Implementation notes We give a combinatorial proof by using the facts that (1) the map from darts to vertices is such that each fiber has cardinality the degree of the corresponding vertex and that (2) the map from darts to edges is 2-to-1. ## Tags simple graphs, sums, degree-sum formula, handshaking lemma -/ open finset open_locale big_operators namespace simple_graph universes u variables {V : Type u} (G : simple_graph V) /-- A dart is a directed edge, consisting of an ordered pair of adjacent vertices. -/ @[ext, derive decidable_eq] structure dart := (fst snd : V) (is_adj : G.adj fst snd) instance dart.fintype [fintype V] [decidable_rel G.adj] : fintype G.dart := fintype.of_equiv (Σ v, G.neighbor_set v) { to_fun := λ s, ⟨s.fst, s.snd, s.snd.property⟩, inv_fun := λ d, ⟨d.fst, d.snd, d.is_adj⟩, left_inv := λ s, by ext; simp, right_inv := λ d, by ext; simp } variables {G} /-- The edge associated to the dart. -/ def dart.edge (d : G.dart) : sym2 V := ⟦(d.fst, d.snd)⟧ @[simp] lemma dart.edge_mem (d : G.dart) : d.edge ∈ G.edge_set := d.is_adj /-- The dart with reversed orientation from a given dart. -/ def dart.rev (d : G.dart) : G.dart := ⟨d.snd, d.fst, G.symm d.is_adj⟩ @[simp] lemma dart.rev_edge (d : G.dart) : d.rev.edge = d.edge := sym2.eq_swap @[simp] lemma dart.rev_rev (d : G.dart) : d.rev.rev = d := dart.ext _ _ rfl rfl @[simp] lemma dart.rev_involutive : function.involutive (dart.rev : G.dart → G.dart) := dart.rev_rev lemma dart.rev_ne (d : G.dart) : d.rev ≠ d := begin cases d with f s h, simp only [dart.rev, not_and, ne.def], rintro rfl, exact false.elim (G.loopless _ h), end lemma dart_edge_eq_iff (d₁ d₂ : G.dart) : d₁.edge = d₂.edge ↔ d₁ = d₂ ∨ d₁ = d₂.rev := begin cases d₁ with s₁ t₁ h₁, cases d₂ with s₂ t₂ h₂, simp only [dart.edge, dart.rev_edge, dart.rev], rw sym2.eq_iff, end variables (G) /-- For a given vertex `v`, this is the bijective map from the neighbor set at `v` to the darts `d` with `d.fst = v`. --/ def dart_of_neighbor_set (v : V) (w : G.neighbor_set v) : G.dart := ⟨v, w, w.property⟩ lemma dart_of_neighbor_set_injective (v : V) : function.injective (G.dart_of_neighbor_set v) := λ e₁ e₂ h, by { injection h with h₁ h₂, exact subtype.ext h₂ } instance dart.inhabited [inhabited V] [inhabited (G.neighbor_set (default _))] : inhabited G.dart := ⟨G.dart_of_neighbor_set (default _) (default _)⟩ section degree_sum variables [fintype V] [decidable_rel G.adj] lemma dart_fst_fiber [decidable_eq V] (v : V) : univ.filter (λ d : G.dart, d.fst = v) = univ.image (G.dart_of_neighbor_set v) := begin ext d, simp only [mem_image, true_and, mem_filter, set_coe.exists, mem_univ, exists_prop_of_true], split, { rintro rfl, exact ⟨_, d.is_adj, dart.ext _ _ rfl rfl⟩, }, { rintro ⟨e, he, rfl⟩, refl, }, end lemma dart_fst_fiber_card_eq_degree [decidable_eq V] (v : V) : (univ.filter (λ d : G.dart, d.fst = v)).card = G.degree v := begin have hh := card_image_of_injective univ (G.dart_of_neighbor_set_injective v), rw [finset.card_univ, card_neighbor_set_eq_degree] at hh, rwa dart_fst_fiber, end lemma dart_card_eq_sum_degrees : fintype.card G.dart = ∑ v, G.degree v := begin haveI h : decidable_eq V, { classical, apply_instance }, simp only [←card_univ, ←dart_fst_fiber_card_eq_degree], exact card_eq_sum_card_fiberwise (by simp), end variables {G} [decidable_eq V] lemma dart.edge_fiber (d : G.dart) : (univ.filter (λ (d' : G.dart), d'.edge = d.edge)) = {d, d.rev} := finset.ext (λ d', by simpa using dart_edge_eq_iff d' d) variables (G) lemma dart_edge_fiber_card (e : sym2 V) (h : e ∈ G.edge_set) : (univ.filter (λ (d : G.dart), d.edge = e)).card = 2 := begin refine quotient.ind (λ p h, _) e h, cases p with v w, let d : G.dart := ⟨v, w, h⟩, convert congr_arg card d.edge_fiber, rw [card_insert_of_not_mem, card_singleton], rw [mem_singleton], exact d.rev_ne.symm, end lemma dart_card_eq_twice_card_edges : fintype.card G.dart = 2 * G.edge_finset.card := begin rw ←card_univ, rw @card_eq_sum_card_fiberwise _ _ _ dart.edge _ G.edge_finset (λ d h, by { rw mem_edge_finset, apply dart.edge_mem }), rw [←mul_comm, sum_const_nat], intros e h, apply G.dart_edge_fiber_card e, rwa ←mem_edge_finset, end /-- The degree-sum formula. This is also known as the handshaking lemma, which might more specifically refer to `simple_graph.even_card_odd_degree_vertices`. -/ theorem sum_degrees_eq_twice_card_edges : ∑ v, G.degree v = 2 * G.edge_finset.card := G.dart_card_eq_sum_degrees.symm.trans G.dart_card_eq_twice_card_edges end degree_sum /-- The handshaking lemma. See also `simple_graph.sum_degrees_eq_twice_card_edges`. -/ theorem even_card_odd_degree_vertices [fintype V] [decidable_rel G.adj] : even (univ.filter (λ v, odd (G.degree v))).card := begin classical, have h := congr_arg ((λ n, ↑n) : ℕ → zmod 2) G.sum_degrees_eq_twice_card_edges, simp only [zmod.nat_cast_self, zero_mul, nat.cast_mul] at h, rw [nat.cast_sum, ←sum_filter_ne_zero] at h, rw @sum_congr _ _ _ _ (λ v, (G.degree v : zmod 2)) (λ v, (1 : zmod 2)) _ rfl at h, { simp only [filter_congr_decidable, mul_one, nsmul_eq_mul, sum_const, ne.def] at h, rw ←zmod.eq_zero_iff_even, convert h, ext v, rw ←zmod.ne_zero_iff_odd, congr' }, { intros v, simp only [true_and, mem_filter, mem_univ, ne.def], rw [zmod.eq_zero_iff_even, zmod.eq_one_iff_odd, nat.odd_iff_not_even, imp_self], trivial } end lemma odd_card_odd_degree_vertices_ne [fintype V] [decidable_eq V] [decidable_rel G.adj] (v : V) (h : odd (G.degree v)) : odd (univ.filter (λ w, w ≠ v ∧ odd (G.degree w))).card := begin rcases G.even_card_odd_degree_vertices with ⟨k, hg⟩, have hk : 0 < k, { have hh : (filter (λ (v : V), odd (G.degree v)) univ).nonempty, { use v, simp only [true_and, mem_filter, mem_univ], use h, }, rwa [←card_pos, hg, zero_lt_mul_left] at hh, exact zero_lt_two, }, have hc : (λ (w : V), w ≠ v ∧ odd (G.degree w)) = (λ (w : V), odd (G.degree w) ∧ w ≠ v), { ext w, rw and_comm, }, simp only [hc, filter_congr_decidable], rw [←filter_filter, filter_ne', card_erase_of_mem], { use k - 1, rw [nat.pred_eq_succ_iff, hg, nat.mul_sub_left_distrib, ← nat.sub_add_comm, eq_comm, ← (nat.sub_eq_iff_eq_add _).symm], { ring }, { exact add_le_add_right (zero_le (2 * k)) 2 }, { exact nat.mul_le_mul_left _ hk } }, { simpa only [true_and, mem_filter, mem_univ] }, end lemma exists_ne_odd_degree_of_exists_odd_degree [fintype V] [decidable_rel G.adj] (v : V) (h : odd (G.degree v)) : ∃ (w : V), w ≠ v ∧ odd (G.degree w) := begin haveI : decidable_eq V, { classical, apply_instance }, rcases G.odd_card_odd_degree_vertices_ne v h with ⟨k, hg⟩, have hg' : (filter (λ (w : V), w ≠ v ∧ odd (G.degree w)) univ).card > 0, { rw hg, apply nat.succ_pos, }, rcases card_pos.mp hg' with ⟨w, hw⟩, simp only [true_and, mem_filter, mem_univ, ne.def] at hw, exact ⟨w, hw⟩, end end simple_graph
960579292c1fa639b0a6cdd1c965f87c940166ec
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/category_theory/adjunction/lifting.lean
09d5006af78151422d8466e89427e045e7231a09
[]
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
10,937
lean
/- Copyright (c) 2020 Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.category_theory.limits.shapes.equalizers import Mathlib.category_theory.limits.shapes.reflexive import Mathlib.category_theory.adjunction.default import Mathlib.category_theory.monad.adjunction import Mathlib.category_theory.monad.coequalizer import Mathlib.PostPort universes u₂ u₃ v₂ v₃ v₁ u₁ v₄ u₄ namespace Mathlib /-! # Adjoint lifting This file gives two constructions for building left adjoints: the adjoint triangle theorem and the adjoint lifting theorem. The adjoint triangle theorem says that given a functor `U : B ⥤ C` with a left adjoint `F` such that `ε_X : FUX ⟶ X` is a regular epi. Then for any category `A` with coequalizers of reflexive pairs, a functor `R : A ⥤ B` has a left adjoint if (and only if) the composite `R ⋙ U` does. Note that the condition on `U` regarding `ε_X` is automatically satisfied in the case when `U` is a monadic functor, giving the corollary: `monadic_adjoint_triangle_lift`, i.e. if `U` is monadic, `A` has reflexive coequalizers then `R : A ⥤ B` has a left adjoint provided `R ⋙ U` does. The adjoint lifting theorem says that given a commutative square of functors (up to isomorphism): Q A → B U ↓ ↓ V C → D R where `U` and `V` are monadic and `A` has reflexive coequalizers, then if `R` has a left adjoint then `Q` has a left adjoint. ## Implementation It is more convenient to prove this theorem by assuming we are given the explicit adjunction rather than just a functor known to be a right adjoint. In docstrings, we write `(η, ε)` for the unit and counit of the adjunction `adj₁ : F ⊣ U` and `(ι, δ)` for the unit and counit of the adjunction `adj₂ : F' ⊣ R ⋙ U`. ## TODO Dualise to lift right adjoints through comonads (by reversing 1-cells) and dualise to lift right adjoints through monads (by reversing 2-cells), and the combination. ## References * https://ncatlab.org/nlab/show/adjoint+triangle+theorem * https://ncatlab.org/nlab/show/adjoint+lifting+theorem * Adjoint Lifting Theorems for Categories of Algebras (PT Johnstone, 1975) * A unified approach to the lifting of adjoints (AJ Power, 1988) -/ namespace category_theory -- Hide implementation details in this namespace namespace lift_adjoint /-- To show that `ε_X` is a coequalizer for `(FUε_X, ε_FUX)`, it suffices to assume it's always a coequalizer of something (i.e. a regular epi). -/ def counit_coequalises {B : Type u₂} {C : Type u₃} [category B] [category C] {U : B ⥤ C} {F : C ⥤ B} (adj₁ : F ⊣ U) [(X : B) → regular_epi (nat_trans.app (adjunction.counit adj₁) X)] (X : B) : limits.is_colimit (limits.cofork.of_π (nat_trans.app (adjunction.counit adj₁) X) (counit_coequalises._proof_1 adj₁ X)) := limits.cofork.is_colimit.mk' (limits.cofork.of_π (nat_trans.app (adjunction.counit adj₁) X) sorry) fun (s : limits.cofork (functor.map F (functor.map U (nat_trans.app (adjunction.counit adj₁) X))) (nat_trans.app (adjunction.counit adj₁) (functor.obj F (functor.obj U X)))) => { val := subtype.val (regular_epi.desc' (nat_trans.app (adjunction.counit adj₁) X) (limits.cofork.π s) sorry), property := sorry } /-- (Implementation) To construct the left adjoint, we use the coequalizer of `F' U ε_Y` with the composite `F' U F U X ⟶ F' U F U R F U' X ⟶ F' U R F' U X ⟶ F' U X` where the first morphism is `F' U F ι_UX`, the second is `F' U ε_RF'UX`, and the third is `δ_F'UX`. We will show that this coequalizer exists and that it forms the object map for a left adjoint to `R`. -/ def other_map {A : Type u₁} {B : Type u₂} {C : Type u₃} [category A] [category B] [category C] {U : B ⥤ C} {F : C ⥤ B} (R : A ⥤ B) (F' : C ⥤ A) (adj₁ : F ⊣ U) (adj₂ : F' ⊣ R ⋙ U) (X : B) : functor.obj F' (functor.obj U (functor.obj F (functor.obj U X))) ⟶ functor.obj F' (functor.obj U X) := functor.map F' (functor.map U (functor.map F (nat_trans.app (adjunction.unit adj₂) (functor.obj U X)) ≫ nat_trans.app (adjunction.counit adj₁) (functor.obj R (functor.obj F' (functor.obj U X))))) ≫ nat_trans.app (adjunction.counit adj₂) (functor.obj F' (functor.obj U X)) /-- `(F'Uε_X, other_map X)` is a reflexive pair: in particular if `A` has reflexive coequalizers then it has a coequalizer. -/ protected instance other_map.category_theory.is_reflexive_pair {A : Type u₁} {B : Type u₂} {C : Type u₃} [category A] [category B] [category C] {U : B ⥤ C} {F : C ⥤ B} (R : A ⥤ B) (F' : C ⥤ A) (adj₁ : F ⊣ U) (adj₂ : F' ⊣ R ⋙ U) (X : B) : is_reflexive_pair (functor.map F' (functor.map U (nat_trans.app (adjunction.counit adj₁) X))) (other_map R F' adj₁ adj₂ X) := sorry /-- Construct the object part of the desired left adjoint as the coequalizer of `F'Uε_Y` with `other_map`. -/ def construct_left_adjoint_obj {A : Type u₁} {B : Type u₂} {C : Type u₃} [category A] [category B] [category C] {U : B ⥤ C} {F : C ⥤ B} (R : A ⥤ B) (F' : C ⥤ A) (adj₁ : F ⊣ U) (adj₂ : F' ⊣ R ⋙ U) [limits.has_reflexive_coequalizers A] (Y : B) : A := limits.coequalizer (functor.map F' (functor.map U (nat_trans.app (adjunction.counit adj₁) Y))) (other_map R F' adj₁ adj₂ Y) /-- The homset equivalence which helps show that `R` is a right adjoint. -/ def construct_left_adjoint_equiv {A : Type u₁} {B : Type u₂} {C : Type u₃} [category A] [category B] [category C] {U : B ⥤ C} {F : C ⥤ B} (R : A ⥤ B) (F' : C ⥤ A) (adj₁ : F ⊣ U) (adj₂ : F' ⊣ R ⋙ U) [limits.has_reflexive_coequalizers A] [(X : B) → regular_epi (nat_trans.app (adjunction.counit adj₁) X)] (Y : A) (X : B) : (construct_left_adjoint_obj R F' adj₁ adj₂ X ⟶ Y) ≃ (X ⟶ functor.obj R Y) := equiv.trans (equiv.trans (equiv.trans (limits.cofork.is_colimit.hom_iso (limits.colimit.is_colimit (limits.parallel_pair (functor.map F' (functor.map U (nat_trans.app (adjunction.counit adj₁) X))) (other_map R F' adj₁ adj₂ X))) Y) (equiv.subtype_congr (adjunction.hom_equiv adj₂ (functor.obj U X) Y) sorry)) (equiv.subtype_congr (equiv.symm (adjunction.hom_equiv adj₁ (functor.obj U X) (functor.obj R Y))) sorry)) (equiv.symm (limits.cofork.is_colimit.hom_iso (counit_coequalises adj₁ X) (functor.obj R Y))) /-- Construct the left adjoint to `R`, with object map `construct_left_adjoint_obj`. -/ def construct_left_adjoint {A : Type u₁} {B : Type u₂} {C : Type u₃} [category A] [category B] [category C] {U : B ⥤ C} {F : C ⥤ B} (R : A ⥤ B) (F' : C ⥤ A) (adj₁ : F ⊣ U) (adj₂ : F' ⊣ R ⋙ U) [limits.has_reflexive_coequalizers A] [(X : B) → regular_epi (nat_trans.app (adjunction.counit adj₁) X)] : B ⥤ A := adjunction.left_adjoint_of_equiv (fun (X : B) (Y : A) => construct_left_adjoint_equiv R F' adj₁ adj₂ Y X) sorry end lift_adjoint /-- The adjoint triangle theorem: Suppose `U : B ⥤ C` has a left adjoint `F` such that each counit `ε_X : FUX ⟶ X` is a regular epimorphism. Then if a category `A` has coequalizers of reflexive pairs, then a functor `R : A ⥤ B` has a left adjoint if the composite `R ⋙ U` does. Note the converse is true (with weaker assumptions), by `adjunction.comp`. See https://ncatlab.org/nlab/show/adjoint+triangle+theorem -/ def adjoint_triangle_lift {A : Type u₁} {B : Type u₂} {C : Type u₃} [category A] [category B] [category C] {U : B ⥤ C} {F : C ⥤ B} (R : A ⥤ B) (adj₁ : F ⊣ U) [(X : B) → regular_epi (nat_trans.app (adjunction.counit adj₁) X)] [limits.has_reflexive_coequalizers A] [is_right_adjoint (R ⋙ U)] : is_right_adjoint R := is_right_adjoint.mk (lift_adjoint.construct_left_adjoint R (left_adjoint (R ⋙ U)) adj₁ (adjunction.of_right_adjoint (R ⋙ U))) (adjunction.adjunction_of_equiv_left (fun (X : B) (Y : A) => lift_adjoint.construct_left_adjoint_equiv R (left_adjoint (R ⋙ U)) adj₁ (adjunction.of_right_adjoint (R ⋙ U)) Y X) sorry) /-- If `R ⋙ U` has a left adjoint, the domain of `R` has reflexive coequalizers and `U` is a monadic functor, then `R` has a left adjoint. This is a special case of `adjoint_triangle_lift` which is often more useful in practice. -/ def monadic_adjoint_triangle_lift {A : Type u₁} {B : Type u₂} {C : Type u₃} [category A] [category B] [category C] (U : B ⥤ C) [monadic_right_adjoint U] {R : A ⥤ B} [limits.has_reflexive_coequalizers A] [is_right_adjoint (R ⋙ U)] : is_right_adjoint R := let R' : A ⥤ monad.algebra (left_adjoint U ⋙ U) := R ⋙ monad.comparison U; let this : is_right_adjoint (R' ⋙ functor.inv (monad.comparison U)) := adjunction.right_adjoint_of_comp; let this_1 : R' ⋙ functor.inv (monad.comparison U) ≅ R := iso_whisker_left R (functor.fun_inv_id (monad.comparison U)) ≪≫ functor.right_unitor R; adjunction.right_adjoint_of_nat_iso this_1 /-- Suppose we have a commutative square of functors Q A → B U ↓ ↓ V C → D R where `U` has a left adjoint, `A` has reflexive coequalizers and `V` has a left adjoint such that each component of the counit is a regular epi. Then `Q` has a left adjoint if `R` has a left adjoint. See https://ncatlab.org/nlab/show/adjoint+lifting+theorem -/ def adjoint_square_lift {A : Type u₁} {B : Type u₂} {C : Type u₃} [category A] [category B] [category C] {D : Type u₄} [category D] (Q : A ⥤ B) (V : B ⥤ D) (U : A ⥤ C) (R : C ⥤ D) (comm : U ⋙ R ≅ Q ⋙ V) [is_right_adjoint U] [is_right_adjoint V] [is_right_adjoint R] [(X : B) → regular_epi (nat_trans.app (adjunction.counit (adjunction.of_right_adjoint V)) X)] [limits.has_reflexive_coequalizers A] : is_right_adjoint Q := let this : is_right_adjoint (Q ⋙ V) := adjunction.right_adjoint_of_nat_iso comm; adjoint_triangle_lift Q (adjunction.of_right_adjoint V) /-- Suppose we have a commutative square of functors Q A → B U ↓ ↓ V C → D R where `U` has a left adjoint, `A` has reflexive coequalizers and `V` is monadic. Then `Q` has a left adjoint if `R` has a left adjoint. See https://ncatlab.org/nlab/show/adjoint+lifting+theorem -/ def monadic_adjoint_square_lift {A : Type u₁} {B : Type u₂} {C : Type u₃} [category A] [category B] [category C] {D : Type u₄} [category D] (Q : A ⥤ B) (V : B ⥤ D) (U : A ⥤ C) (R : C ⥤ D) (comm : U ⋙ R ≅ Q ⋙ V) [is_right_adjoint U] [monadic_right_adjoint V] [is_right_adjoint R] [limits.has_reflexive_coequalizers A] : is_right_adjoint Q := let this : is_right_adjoint (Q ⋙ V) := adjunction.right_adjoint_of_nat_iso comm; monadic_adjoint_triangle_lift V
cedda641a487e6df55a7594e3f396ec70460fe0b
aa3f8992ef7806974bc1ffd468baa0c79f4d6643
/tests/lean/acc_rec_bug.lean
57dcdb9806fd37ee15578e3d5b1bfba80f0edd89
[ "Apache-2.0" ]
permissive
codyroux/lean
7f8dff750722c5382bdd0a9a9275dc4bb2c58dd3
0cca265db19f7296531e339192e9b9bae4a31f8b
refs/heads/master
1,610,909,964,159
1,407,084,399,000
1,416,857,075,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
845
lean
import logic.prop inductive acc {A : Type} (R : A → A → Prop) : A → Prop := intro : ∀x, (∀ y, R y x → acc R y) → acc R x variables {A : Type} (R : A → A → Prop) (C : A → Type) (x₁ : A) (ac : ∀y, R y x₁ → acc R y) variable F : Πx, (Πy, R y x → C y) → C x eval @acc.rec A R C (λ (x₂ : A) (ac : ∀y, R y x₂ → acc R y) (iH : Πy, R y x₂ → C y), F x₂ iH) x₁ (acc.intro x₁ ac) check @acc.rec A R C (λ (x₂ : A) (ac : ∀y, R y x₂ → acc R y) (iH : Πy, R y x₂ → C y), F x₂ iH) x₁ (acc.intro x₁ ac) check F x₁ (λ (y : A) (a : R y x₁), acc.rec (λ (x₂ : A) (ac : ∀ (y : A), R y x₂ → acc R y) (iH : Π (y : A), R y x₂ → C y), F x₂ iH) (ac y a))
02248267680a2735f63514f701df1bc14a62c254
ec62863c729b7eedee77b86d974f2c529fa79d25
/20/a.lean
8532e0de1933ecf3908d646f5846b0500a85c1b0
[]
no_license
rwbarton/advent-of-lean-4
2ac9b17ba708f66051e3d8cd694b0249bc433b65
417c7e2718253ba7148c0279fcb251b6fc291477
refs/heads/main
1,675,917,092,057
1,609,864,581,000
1,609,864,581,000
317,700,289
24
0
null
null
null
null
UTF-8
Lean
false
false
2,759
lean
import Std.Data.HashMap open Std @[reducible] def Bits := UInt32 structure OrientedPiece where -- bits read from left to right or top to bottom in big-endian order left : Bits right : Bits top : Bits bottom : Bits instance : ToString OrientedPiece where toString p := s!"{p.left} - {p.right} {p.top} | {p.bottom}" def rev (N : Nat) (x : Bits) : Bits := do let mut res : Bits := 0 for i in [0:N] do res := res + if (x.toNat / 2^i) % 2 == 1 then UInt32.ofNat (2^(N-1-i)) else 0 res def parsePiece (str : List String) : OrientedPiece := do let N := str.length let val (row col : Nat) : Bits := if (str.get! row).get col == '#' then 1 else 0 let mut res : OrientedPiece := ⟨0, 0, 0, 0⟩ for i in [0:N] do res := { left := 2 * res.left + val i 0, right := 2 * res.right + val i (N-1), top := 2 * res.top + val 0 i, bottom := 2 * res.bottom + val (N-1) i } res instance : Monad List := { pure := λ a => [a], map := List.map, bind := λ x f => (x.map f).join } def orientPiece (N : Nat) (p : OrientedPiece) : Array OrientedPiece := Array.mk $ do let rot q := { top := q.right, right := rev N q.bottom, bottom := q.left, left := rev N q.top } let p₁ := rot p let p₂ := rot p₁ let p₃ := rot p₂ let flip q := { top := q.left, left := q.top, bottom := q.right, right := q.bottom } let p' ← [p, p₁, p₂, p₃] [p', flip p'] @[reducible] def Tiles := List (Nat × Array OrientedPiece) def buildInput (str : String) : Tiles := (str.splitOn "\n\n").map $ λ para => match para.trim.splitOn "\n" with | header :: rest => (((header.dropRight 1).drop 5).toNat!, orientPiece rest.length (parsePiece rest)) | [] => panic! "bad para" partial def go (S : Nat) (tiles : Tiles) (used : HashMap Nat (Nat × Nat)) (above : Array Bits) (left : Bits) (row col : Nat) : List Nat := if row == S then pure $ do let mut prod := 1 for ⟨i, ⟨row, col⟩⟩ in used.toArray do if (row == 0 ∨ row == S-1) ∧ (col == 0 ∨ col == S-1) then prod := prod * i prod else do let p ← tiles if used.contains p.1 then [] let ⟨nextRow, nextCol⟩ := if col + 1 == S then (row+1, 0) else (row, col+1) let o ← p.2.toList if col > 0 ∧ left ≠ o.left then [] if row > 0 ∧ above.get! col ≠ o.top then [] go S tiles (used.insert p.1 (row, col)) (above.set! col o.bottom) o.right nextRow nextCol def solve (S : Nat) (tiles : Tiles) := go S tiles HashMap.empty (Array.mkArray S 0) 0 0 0 def theUnique [Inhabited α] [BEq α] : List α → α | [] => panic! "nothing" | (x::xs) => if xs.all (· == x) then x else panic! "multiple values" def main : IO Unit := do let input ← IO.FS.readFile "a.in" IO.print (theUnique $ solve 12 (buildInput input)) IO.print "\n"
c22875c2f81edfd311f59c46d31e72a98e06881c
1e3a43e8ba59c6fe1c66775b6e833e721eaf1675
/src/ring_theory/adjoin_root.lean
20876bba0cd13a3cf83bb4a5cd12c2259163664b
[ "Apache-2.0" ]
permissive
Sterrs/mathlib
ea6910847b8dfd18500486de9ab0ee35704a3f52
d9327e433804004aa1dc65091bbe0de1e5a08c5e
refs/heads/master
1,650,769,884,257
1,587,808,694,000
1,587,808,694,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,194
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Chris Hughes Adjoining roots of polynomials -/ import data.polynomial import ring_theory.principal_ideal_domain /-! # Adjoining roots of polynomials This file defines the commutative ring `adjoin_root f`, the ring R[X]/(f) obtained from a commutative ring `R` and a polynomial `f : R[X]`. If furthermore `R` is a field and `f` is irreducible, the field structure on `adjoin_root f` is constructed. ## Main definitions and results The main definitions are in the `adjoin_root` namespace. * `mk f : polynomial R →+* adjoin_root f`, the natural ring homomorphism. * `of f : R →+* adjoin_root f`, the natural ring homomorphism. * `root f : adjoin_root f`, the image of X in R[X]/(f). * `lift (i : R →+* S) (x : S) (h : f.eval₂ i x = 0) : (adjoin_root f) →+* S`, the ring homomorphism from R[X]/(f) to S extending `i : R →+* S` and sending `X` to `x`. -/ noncomputable theory universes u v w variables {R : Type u} {S : Type v} {K : Type w} open polynomial ideal /-- Adjoin a root of a polynomial `f` to a commutative ring `R`. We define the new ring as the quotient of `R` by the principal ideal of `f`. -/ def adjoin_root [comm_ring R] (f : polynomial R) : Type u := ideal.quotient (span {f} : ideal (polynomial R)) namespace adjoin_root section comm_ring variables [comm_ring R] (f : polynomial R) instance : comm_ring (adjoin_root f) := ideal.quotient.comm_ring _ instance : inhabited (adjoin_root f) := ⟨0⟩ instance : decidable_eq (adjoin_root f) := classical.dec_eq _ /-- Ring homomorphism from `R[x]` to `adjoin_root f` sending `X` to the `root`. -/ def mk : polynomial R →+* adjoin_root f := ideal.quotient.mk_hom _ /-- Embedding of the original ring `R` into `adjoin_root f`. -/ def of : R →+* adjoin_root f := (mk f).comp (ring_hom.of C) /-- The adjoined root. -/ def root : adjoin_root f := mk f X variables {f} instance adjoin_root.has_coe_t : has_coe_t R (adjoin_root f) := ⟨of f⟩ @[simp] lemma mk_self : mk f f = 0 := quotient.sound' (mem_span_singleton.2 $ by simp) instance : is_ring_hom (coe : R → adjoin_root f) := (of f).is_ring_hom @[simp] lemma mk_C (x : R) : mk f (C x) = x := rfl @[simp] lemma eval₂_root (f : polynomial R) : f.eval₂ (of f) (root f) = 0 := quotient.induction_on' (root f) (λ (g : polynomial R) (hg : mk f g = mk f X), show finsupp.sum f (λ (e : ℕ) (a : R), mk f (C a) * mk f g ^ e) = 0, by simp only [hg, ((mk f).map_pow _ _).symm, ((mk f).map_mul _ _).symm]; rw [finsupp.sum, ← (mk f).map_sum, show finset.sum _ _ = _, from sum_C_mul_X_eq _, mk_self]) (show (root f) = mk f X, from rfl) lemma is_root_root (f : polynomial R) : is_root (f.map (of f)) (root f) := by rw [is_root, eval_map, eval₂_root] variables [comm_ring S] /-- Lift a ring homomorphism `i : R →+* S` to `adjoin_root f →+* S`. -/ def lift (i : R →+* S) (x : S) (h : f.eval₂ i x = 0) : (adjoin_root f) →+* S := begin apply ideal.quotient.lift _ (eval₂_ring_hom i x), intros g H, rcases mem_span_singleton.1 H with ⟨y, hy⟩, rw [hy, ring_hom.map_mul, coe_eval₂_ring_hom, h, zero_mul] end variables {i : R →+* S} {a : S} {h : f.eval₂ i a = 0} @[simp] lemma lift_mk {g : polynomial R} : lift i a h (mk f g) = g.eval₂ i a := ideal.quotient.lift_mk _ _ _ @[simp] lemma lift_root : lift i a h (root f) = a := by simp [root, h] @[simp] lemma lift_of {x : R} : lift i a h x = i x := by rw [← mk_C x, lift_mk, eval₂_C] end comm_ring variables [field K] {f : polynomial K} [irreducible f] instance is_maximal_span : is_maximal (span {f} : ideal (polynomial K)) := principal_ideal_domain.is_maximal_of_irreducible ‹irreducible f› noncomputable instance field : field (adjoin_root f) := ideal.quotient.field (span {f} : ideal (polynomial K)) lemma coe_injective : function.injective (coe : K → adjoin_root f) := (of f).injective variable (f) lemma mul_div_root_cancel : (X - C (root f)) * (f.map (of f) / (X - C (root f))) = f.map (of f) := mul_div_eq_iff_is_root.2 $ is_root_root _ end adjoin_root
1c80e4bb7b98c93a5866a23d60990b30946c27a8
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/algebra/pointwise.lean
b02edee980f1213f46202f0920fd439e24e70113
[ "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
56,636
lean
/- Copyright (c) 2019 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Floris van Doorn -/ import algebra.big_operators.basic import algebra.module.basic import data.finset.preimage import data.set.finite import group_theory.submonoid.basic /-! # Pointwise addition, multiplication, scalar multiplication and vector subtraction of sets. This file defines pointwise algebraic operations on sets. * For a type `α` with multiplication, multiplication is defined on `set α` by taking `s * t` to be the set of all `x * y` where `x ∈ s` and `y ∈ t`. Similarly for addition. * For `α` a semigroup, `set α` is a semigroup. * If `α` is a (commutative) monoid, we define an alias `set_semiring α` for `set α`, which then becomes a (commutative) semiring with union as addition and pointwise multiplication as multiplication. * For a type `β` with scalar multiplication by another type `α`, this file defines a scalar multiplication of `set β` by `set α` and a separate scalar multiplication of `set β` by `α`. * We also define pointwise multiplication on `finset`. Appropriate definitions and results are also transported to the additive theory via `to_additive`. ## Implementation notes * The following expressions are considered in simp-normal form in a group: `(λ h, h * g) ⁻¹' s`, `(λ h, g * h) ⁻¹' s`, `(λ h, h * g⁻¹) ⁻¹' s`, `(λ h, g⁻¹ * h) ⁻¹' s`, `s * t`, `s⁻¹`, `(1 : set _)` (and similarly for additive variants). Expressions equal to one of these will be simplified. * We put all instances in the locale `pointwise`, so that these instances are not available by default. Note that we do not mark them as reducible (as argued by note [reducible non-instances]) since we expect the locale to be open whenever the instances are actually used (and making the instances reducible changes the behavior of `simp`). ## Tags set multiplication, set addition, pointwise addition, pointwise multiplication, pointwise subtraction -/ open function variables {α β γ : Type*} namespace set /-! ### Properties about 1 -/ section one variables [has_one α] {s : set α} {a : α} /-- The set `(1 : set α)` is defined as `{1}` in locale `pointwise`. -/ @[to_additive /-"The set `(0 : set α)` is defined as `{0}` in locale `pointwise`. "-/] protected def has_one : has_one (set α) := ⟨{1}⟩ localized "attribute [instance] set.has_one set.has_zero" in pointwise @[to_additive] lemma singleton_one : ({1} : set α) = 1 := rfl @[simp, to_additive] lemma mem_one : a ∈ (1 : set α) ↔ a = 1 := iff.rfl @[to_additive] lemma one_mem_one : (1 : α) ∈ (1 : set α) := eq.refl _ @[simp, to_additive] lemma one_subset : 1 ⊆ s ↔ (1 : α) ∈ s := singleton_subset_iff @[to_additive] lemma one_nonempty : (1 : set α).nonempty := ⟨1, rfl⟩ @[simp, to_additive] lemma image_one {f : α → β} : f '' 1 = {f 1} := image_singleton end one open_locale pointwise /-! ### Properties about multiplication -/ section mul variables {s s₁ s₂ t t₁ t₂ u : set α} {a b : α} /-- The set `(s * t : set α)` is defined as `{x * y | x ∈ s, y ∈ t}` in locale `pointwise`. -/ @[to_additive /-" The set `(s + t : set α)` is defined as `{x + y | x ∈ s, y ∈ t}` in locale `pointwise`."-/] protected def has_mul [has_mul α] : has_mul (set α) := ⟨image2 has_mul.mul⟩ localized "attribute [instance] set.has_mul set.has_add" in pointwise section has_mul variables {ι : Sort*} {κ : ι → Sort*} [has_mul α] @[simp, to_additive] lemma image2_mul : image2 has_mul.mul s t = s * t := rfl @[to_additive] lemma mem_mul : a ∈ s * t ↔ ∃ x y, x ∈ s ∧ y ∈ t ∧ x * y = a := iff.rfl @[to_additive] lemma mul_mem_mul (ha : a ∈ s) (hb : b ∈ t) : a * b ∈ s * t := mem_image2_of_mem ha hb @[to_additive] lemma mul_subset_mul (h₁ : s₁ ⊆ t₁) (h₂ : s₂ ⊆ t₂) : s₁ * s₂ ⊆ t₁ * t₂ := image2_subset h₁ h₂ @[to_additive add_image_prod] lemma image_mul_prod : (λ x : α × α, x.fst * x.snd) '' (s ×ˢ t) = s * t := image_prod _ @[simp, to_additive] lemma empty_mul : ∅ * s = ∅ := image2_empty_left @[simp, to_additive] lemma mul_empty : s * ∅ = ∅ := image2_empty_right @[simp, to_additive] lemma mul_singleton : s * {b} = (* b) '' s := image2_singleton_right @[simp, to_additive] lemma singleton_mul : {a} * t = ((*) a) '' t := image2_singleton_left @[simp, to_additive] lemma singleton_mul_singleton : ({a} : set α) * {b} = {a * b} := image2_singleton @[to_additive] lemma mul_subset_mul_left (h : t₁ ⊆ t₂) : s * t₁ ⊆ s * t₂ := image2_subset_left h @[to_additive] lemma mul_subset_mul_right (h : s₁ ⊆ s₂) : s₁ * t ⊆ s₂ * t := image2_subset_right h @[to_additive] lemma union_mul : (s₁ ∪ s₂) * t = s₁ * t ∪ s₂ * t := image2_union_left @[to_additive] lemma mul_union : s * (t₁ ∪ t₂) = s * t₁ ∪ s * t₂ := image2_union_right @[to_additive] lemma inter_mul_subset : (s₁ ∩ s₂) * t ⊆ s₁ * t ∩ (s₂ * t) := image2_inter_subset_left @[to_additive] lemma mul_inter_subset : s * (t₁ ∩ t₂) ⊆ s * t₁ ∩ (s * t₂) := image2_inter_subset_right @[to_additive] lemma Union_mul_left_image : (⋃ a ∈ s, (λ x, a * x) '' t) = s * t := Union_image_left _ @[to_additive] lemma Union_mul_right_image : (⋃ a ∈ t, (λ x, x * a) '' s) = s * t := Union_image_right _ @[to_additive] lemma Union_mul (s : ι → set α) (t : set α) : (⋃ i, s i) * t = ⋃ i, s i * t := image2_Union_left _ _ _ @[to_additive] lemma mul_Union (s : set α) (t : ι → set α) : s * (⋃ i, t i) = ⋃ i, s * t i := image2_Union_right _ _ _ @[to_additive] lemma Union₂_mul (s : Π i, κ i → set α) (t : set α) : (⋃ i j, s i j) * t = ⋃ i j, s i j * t := image2_Union₂_left _ _ _ @[to_additive] lemma mul_Union₂ (s : set α) (t : Π i, κ i → set α) : s * (⋃ i j, t i j) = ⋃ i j, s * t i j := image2_Union₂_right _ _ _ @[to_additive] lemma Inter_mul_subset (s : ι → set α) (t : set α) : (⋂ i, s i) * t ⊆ ⋂ i, s i * t := image2_Inter_subset_left _ _ _ @[to_additive] lemma mul_Inter_subset (s : set α) (t : ι → set α) : s * (⋂ i, t i) ⊆ ⋂ i, s * t i := image2_Inter_subset_right _ _ _ @[to_additive] lemma Inter₂_mul_subset (s : Π i, κ i → set α) (t : set α) : (⋂ i j, s i j) * t ⊆ ⋂ i j, s i j * t := image2_Inter₂_subset_left _ _ _ @[to_additive] lemma mul_Inter₂_subset (s : set α) (t : Π i, κ i → set α) : s * (⋂ i j, t i j) ⊆ ⋂ i j, s * t i j := image2_Inter₂_subset_right _ _ _ /-- Under `[has_mul M]`, the `singleton` map from `M` to `set M` as a `mul_hom`, that is, a map which preserves multiplication. -/ @[to_additive "Under `[has_add A]`, the `singleton` map from `A` to `set A` as an `add_hom`, that is, a map which preserves addition.", simps] def singleton_mul_hom : mul_hom α (set α) := { to_fun := singleton, map_mul' := λ a b, singleton_mul_singleton.symm } end has_mul @[simp, to_additive] lemma image_mul_left [group α] : ((*) a) '' t = ((*) a⁻¹) ⁻¹' t := by { rw image_eq_preimage_of_inverse; intro c; simp } @[simp, to_additive] lemma image_mul_right [group α] : (* b) '' t = (* b⁻¹) ⁻¹' t := by { rw image_eq_preimage_of_inverse; intro c; simp } @[to_additive] lemma image_mul_left' [group α] : (λ b, a⁻¹ * b) '' t = (λ b, a * b) ⁻¹' t := by simp @[to_additive] lemma image_mul_right' [group α] : (* b⁻¹) '' t = (* b) ⁻¹' t := by simp @[simp, to_additive] lemma preimage_mul_left_singleton [group α] : ((*) a) ⁻¹' {b} = {a⁻¹ * b} := by rw [← image_mul_left', image_singleton] @[simp, to_additive] lemma preimage_mul_right_singleton [group α] : (* a) ⁻¹' {b} = {b * a⁻¹} := by rw [← image_mul_right', image_singleton] @[simp, to_additive] lemma preimage_mul_left_one [group α] : ((*) a) ⁻¹' 1 = {a⁻¹} := by rw [← image_mul_left', image_one, mul_one] @[simp, to_additive] lemma preimage_mul_right_one [group α] : (* b) ⁻¹' 1 = {b⁻¹} := by rw [← image_mul_right', image_one, one_mul] @[to_additive] lemma preimage_mul_left_one' [group α] : (λ b, a⁻¹ * b) ⁻¹' 1 = {a} := by simp @[to_additive] lemma preimage_mul_right_one' [group α] : (* b⁻¹) ⁻¹' 1 = {b} := by simp @[to_additive] protected lemma mul_comm [comm_semigroup α] : s * t = t * s := by simp only [← image2_mul, image2_swap _ s, mul_comm] /-- `set α` is a `mul_one_class` under pointwise operations if `α` is. -/ @[to_additive /-"`set α` is an `add_zero_class` under pointwise operations if `α` is."-/] protected def mul_one_class [mul_one_class α] : mul_one_class (set α) := { mul_one := λ s, by { simp only [← singleton_one, mul_singleton, mul_one, image_id'] }, one_mul := λ s, by { simp only [← singleton_one, singleton_mul, one_mul, image_id'] }, ..set.has_one, ..set.has_mul } /-- `set α` is a `semigroup` under pointwise operations if `α` is. -/ @[to_additive /-"`set α` is an `add_semigroup` under pointwise operations if `α` is. "-/] protected def semigroup [semigroup α] : semigroup (set α) := { mul_assoc := λ _ _ _, image2_assoc mul_assoc, ..set.has_mul } /-- `set α` is a `monoid` under pointwise operations if `α` is. -/ @[to_additive /-"`set α` is an `add_monoid` under pointwise operations if `α` is. "-/] protected def monoid [monoid α] : monoid (set α) := { ..set.semigroup, ..set.mul_one_class } /-- `set α` is a `comm_monoid` under pointwise operations if `α` is. -/ @[to_additive /-"`set α` is an `add_comm_monoid` under pointwise operations if `α` is. "-/] protected def comm_monoid [comm_monoid α] : comm_monoid (set α) := { mul_comm := λ _ _, set.mul_comm, ..set.monoid } localized "attribute [instance] set.mul_one_class set.add_zero_class set.semigroup set.add_semigroup set.monoid set.add_monoid set.comm_monoid set.add_comm_monoid" in pointwise @[to_additive nsmul_mem_nsmul] lemma pow_mem_pow [monoid α] (ha : a ∈ s) (n : ℕ) : a ^ n ∈ s ^ n := begin induction n with n ih, { rw pow_zero, exact set.mem_singleton 1 }, { rw pow_succ, exact set.mul_mem_mul ha ih }, end @[to_additive empty_nsmul] lemma empty_pow [monoid α] (n : ℕ) (hn : n ≠ 0) : (∅ : set α) ^ n = ∅ := by rw [← tsub_add_cancel_of_le (nat.succ_le_of_lt $ nat.pos_of_ne_zero hn), pow_succ, empty_mul] instance decidable_mem_mul [monoid α] [fintype α] [decidable_eq α] [decidable_pred (∈ s)] [decidable_pred (∈ t)] : decidable_pred (∈ s * t) := λ _, decidable_of_iff _ mem_mul.symm instance decidable_mem_pow [monoid α] [fintype α] [decidable_eq α] [decidable_pred (∈ s)] (n : ℕ) : decidable_pred (∈ (s ^ n)) := begin induction n with n ih, { simp_rw [pow_zero, mem_one], apply_instance }, { letI := ih, rw pow_succ, apply_instance } end @[to_additive] lemma subset_mul_left [mul_one_class α] (s : set α) {t : set α} (ht : (1 : α) ∈ t) : s ⊆ s * t := λ x hx, ⟨x, 1, hx, ht, mul_one _⟩ @[to_additive] lemma subset_mul_right [mul_one_class α] {s : set α} (t : set α) (hs : (1 : α) ∈ s) : t ⊆ s * t := λ x hx, ⟨1, x, hs, hx, one_mul _⟩ lemma pow_subset_pow [monoid α] (hst : s ⊆ t) (n : ℕ) : s ^ n ⊆ t ^ n := begin induction n with n ih, { rw pow_zero, exact subset.rfl }, { rw [pow_succ, pow_succ], exact mul_subset_mul hst ih }, end @[simp, to_additive] lemma univ_mul_univ [monoid α] : (univ : set α) * univ = univ := begin have : ∀x, ∃a b : α, a * b = x := λx, ⟨x, ⟨1, mul_one x⟩⟩, simpa only [mem_mul, eq_univ_iff_forall, mem_univ, true_and] end @[simp, to_additive] lemma mul_univ [group α] (hs : s.nonempty) : s * (univ : set α) = univ := let ⟨a, ha⟩ := hs in eq_univ_of_forall $ λ b, ⟨a, a⁻¹ * b, ha, trivial, mul_inv_cancel_left _ _⟩ @[simp, to_additive] lemma univ_mul [group α] (ht : t.nonempty) : (univ : set α) * t = univ := let ⟨a, ha⟩ := ht in eq_univ_of_forall $ λ b, ⟨b * a⁻¹, a, trivial, ha, inv_mul_cancel_right _ _⟩ /-- `singleton` is a monoid hom. -/ @[to_additive singleton_add_hom "singleton is an add monoid hom"] def singleton_hom [monoid α] : α →* set α := { to_fun := singleton, map_one' := rfl, map_mul' := λ a b, singleton_mul_singleton.symm } @[to_additive] lemma nonempty.mul [has_mul α] : s.nonempty → t.nonempty → (s * t).nonempty := nonempty.image2 @[to_additive] lemma finite.mul [has_mul α] (hs : finite s) (ht : finite t) : finite (s * t) := hs.image2 _ ht /-- multiplication preserves finiteness -/ @[to_additive "addition preserves finiteness"] def fintype_mul [has_mul α] [decidable_eq α] (s t : set α) [hs : fintype s] [ht : fintype t] : fintype (s * t : set α) := set.fintype_image2 _ s t @[to_additive] lemma bdd_above_mul [ordered_comm_monoid α] {A B : set α} : bdd_above A → bdd_above B → bdd_above (A * B) := begin rintros ⟨bA, hbA⟩ ⟨bB, hbB⟩, use bA * bB, rintros x ⟨xa, xb, hxa, hxb, rfl⟩, exact mul_le_mul' (hbA hxa) (hbB hxb), end end mul open_locale pointwise section big_operators open_locale big_operators variables {ι : Type*} [comm_monoid α] /-- The n-ary version of `set.mem_mul`. -/ @[to_additive /-" The n-ary version of `set.mem_add`. "-/] lemma mem_finset_prod (t : finset ι) (f : ι → set α) (a : α) : a ∈ ∏ i in t, f i ↔ ∃ (g : ι → α) (hg : ∀ {i}, i ∈ t → g i ∈ f i), ∏ i in t, g i = a := begin classical, induction t using finset.induction_on with i is hi ih generalizing a, { simp_rw [finset.prod_empty, set.mem_one], exact ⟨λ h, ⟨λ i, a, λ i, false.elim, h.symm⟩, λ ⟨f, _, hf⟩, hf.symm⟩ }, rw [finset.prod_insert hi, set.mem_mul], simp_rw [finset.prod_insert hi], simp_rw ih, split, { rintros ⟨x, y, hx, ⟨g, hg, rfl⟩, rfl⟩, refine ⟨function.update g i x, λ j hj, _, _⟩, obtain rfl | hj := finset.mem_insert.mp hj, { rw function.update_same, exact hx }, { rw update_noteq (ne_of_mem_of_not_mem hj hi), exact hg hj, }, rw [finset.prod_update_of_not_mem hi, function.update_same], }, { rintros ⟨g, hg, rfl⟩, exact ⟨g i, is.prod g, hg (is.mem_insert_self _), ⟨g, λ i hi, hg (finset.mem_insert_of_mem hi), rfl⟩, rfl⟩ }, end /-- A version of `set.mem_finset_prod` with a simpler RHS for products over a fintype. -/ @[to_additive /-" A version of `set.mem_finset_sum` with a simpler RHS for sums over a fintype. "-/] lemma mem_fintype_prod [fintype ι] (f : ι → set α) (a : α) : a ∈ ∏ i, f i ↔ ∃ (g : ι → α) (hg : ∀ i, g i ∈ f i), ∏ i, g i = a := by { rw mem_finset_prod, simp } /-- The n-ary version of `set.mul_mem_mul`. -/ @[to_additive /-" The n-ary version of `set.add_mem_add`. "-/] lemma finset_prod_mem_finset_prod (t : finset ι) (f : ι → set α) (g : ι → α) (hg : ∀ i ∈ t, g i ∈ f i) : ∏ i in t, g i ∈ ∏ i in t, f i := by { rw mem_finset_prod, exact ⟨g, hg, rfl⟩ } /-- The n-ary version of `set.mul_subset_mul`. -/ @[to_additive /-" The n-ary version of `set.add_subset_add`. "-/] lemma finset_prod_subset_finset_prod (t : finset ι) (f₁ f₂ : ι → set α) (hf : ∀ {i}, i ∈ t → f₁ i ⊆ f₂ i) : ∏ i in t, f₁ i ⊆ ∏ i in t, f₂ i := begin intro a, rw [mem_finset_prod, mem_finset_prod], rintro ⟨g, hg, rfl⟩, exact ⟨g, λ i hi, hf hi $ hg hi, rfl⟩ end @[to_additive] lemma finset_prod_singleton {M ι : Type*} [comm_monoid M] (s : finset ι) (I : ι → M) : ∏ (i : ι) in s, ({I i} : set M) = {∏ (i : ι) in s, I i} := begin letI := classical.dec_eq ι, refine finset.induction_on s _ _, { simpa }, { intros _ _ H ih, rw [finset.prod_insert H, finset.prod_insert H, ih], simp } end /-! TODO: define `decidable_mem_finset_prod` and `decidable_mem_finset_sum`. -/ end big_operators /-! ### Properties about inversion -/ section inv variables {s t : set α} {a : α} /-- The set `(s⁻¹ : set α)` is defined as `{x | x⁻¹ ∈ s}` in locale `pointwise`. It is equal to `{x⁻¹ | x ∈ s}`, see `set.image_inv`. -/ @[to_additive /-" The set `(-s : set α)` is defined as `{x | -x ∈ s}` in locale `pointwise`. It is equal to `{-x | x ∈ s}`, see `set.image_neg`. "-/] protected def has_inv [has_inv α] : has_inv (set α) := ⟨preimage has_inv.inv⟩ localized "attribute [instance] set.has_inv set.has_neg" in pointwise @[simp, to_additive] lemma inv_empty [has_inv α] : (∅ : set α)⁻¹ = ∅ := rfl @[simp, to_additive] lemma inv_univ [has_inv α] : (univ : set α)⁻¹ = univ := rfl @[simp, to_additive] lemma nonempty_inv [group α] {s : set α} : s⁻¹.nonempty ↔ s.nonempty := inv_involutive.surjective.nonempty_preimage @[to_additive] lemma nonempty.inv [group α] {s : set α} (h : s.nonempty) : s⁻¹.nonempty := nonempty_inv.2 h @[simp, to_additive] lemma mem_inv [has_inv α] : a ∈ s⁻¹ ↔ a⁻¹ ∈ s := iff.rfl @[to_additive] lemma inv_mem_inv [group α] : a⁻¹ ∈ s⁻¹ ↔ a ∈ s := by simp only [mem_inv, inv_inv] @[simp, to_additive] lemma inv_preimage [has_inv α] : has_inv.inv ⁻¹' s = s⁻¹ := rfl @[simp, to_additive] lemma image_inv [group α] : has_inv.inv '' s = s⁻¹ := by { simp only [← inv_preimage], rw [image_eq_preimage_of_inverse]; intro; simp only [inv_inv] } @[simp, to_additive] lemma inter_inv [has_inv α] : (s ∩ t)⁻¹ = s⁻¹ ∩ t⁻¹ := preimage_inter @[simp, to_additive] lemma union_inv [has_inv α] : (s ∪ t)⁻¹ = s⁻¹ ∪ t⁻¹ := preimage_union @[simp, to_additive] lemma Inter_inv {ι : Sort*} [has_inv α] (s : ι → set α) : (⋂ i, s i)⁻¹ = ⋂ i, (s i)⁻¹ := preimage_Inter @[simp, to_additive] lemma Union_inv {ι : Sort*} [has_inv α] (s : ι → set α) : (⋃ i, s i)⁻¹ = ⋃ i, (s i)⁻¹ := preimage_Union @[simp, to_additive] lemma compl_inv [has_inv α] : (sᶜ)⁻¹ = (s⁻¹)ᶜ := preimage_compl @[simp, to_additive] protected lemma inv_inv [group α] : s⁻¹⁻¹ = s := by { simp only [← inv_preimage, preimage_preimage, inv_inv, preimage_id'] } @[simp, to_additive] protected lemma univ_inv [group α] : (univ : set α)⁻¹ = univ := preimage_univ @[simp, to_additive] lemma inv_subset_inv [group α] {s t : set α} : s⁻¹ ⊆ t⁻¹ ↔ s ⊆ t := (equiv.inv α).surjective.preimage_subset_preimage_iff @[to_additive] lemma inv_subset [group α] {s t : set α} : s⁻¹ ⊆ t ↔ s ⊆ t⁻¹ := by { rw [← inv_subset_inv, set.inv_inv] } @[to_additive] lemma finite.inv [group α] {s : set α} (hs : finite s) : finite s⁻¹ := hs.preimage $ inv_injective.inj_on _ @[to_additive] lemma inv_singleton {β : Type*} [group β] (x : β) : ({x} : set β)⁻¹ = {x⁻¹} := by { ext1 y, rw [mem_inv, mem_singleton_iff, mem_singleton_iff, inv_eq_iff_inv_eq, eq_comm], } @[to_additive] protected lemma mul_inv_rev [group α] (s t : set α) : (s * t)⁻¹ = t⁻¹ * s⁻¹ := by simp_rw [←image_inv, ←image2_mul, image_image2, image2_image_left, image2_image_right, mul_inv_rev, image2_swap _ s t] end inv /-! ### Properties about scalar multiplication -/ section smul /-- The scaling of a set `(x • s : set β)` by a scalar `x ∶ α` is defined as `{x • y | y ∈ s}` in locale `pointwise`. -/ @[to_additive has_vadd_set "The translation of a set `(x +ᵥ s : set β)` by a scalar `x ∶ α` is defined as `{x +ᵥ y | y ∈ s}` in locale `pointwise`."] protected def has_scalar_set [has_scalar α β] : has_scalar α (set β) := ⟨λ a, image (has_scalar.smul a)⟩ /-- The pointwise scalar multiplication `(s • t : set β)` by a set of scalars `s ∶ set α` is defined as `{x • y | x ∈ s, y ∈ t}` in locale `pointwise`. -/ @[to_additive has_vadd "The pointwise translation `(s +ᵥ t : set β)` by a set of constants `s ∶ set α` is defined as `{x +ᵥ y | x ∈ s, y ∈ t}` in locale `pointwise`."] protected def has_scalar [has_scalar α β] : has_scalar (set α) (set β) := ⟨image2 has_scalar.smul⟩ localized "attribute [instance] set.has_scalar_set set.has_scalar" in pointwise localized "attribute [instance] set.has_vadd_set set.has_vadd" in pointwise section has_scalar variables {ι : Sort*} {κ : ι → Sort*} [has_scalar α β] {s s₁ s₂ : set α} {t t₁ t₂ u : set β} {a : α} {b : β} @[simp, to_additive] lemma image2_smul : image2 has_scalar.smul s t = s • t := rfl @[to_additive add_image_prod] lemma image_smul_prod : (λ x : α × β, x.fst • x.snd) '' (s ×ˢ t) = s • t := image_prod _ @[to_additive] lemma mem_smul : b ∈ s • t ↔ ∃ x y, x ∈ s ∧ y ∈ t ∧ x • y = b := iff.rfl @[to_additive] lemma smul_mem_smul (ha : a ∈ s) (hb : b ∈ t) : a • b ∈ s • t := mem_image2_of_mem ha hb @[to_additive] lemma smul_subset_smul (hs : s₁ ⊆ s₂) (ht : t₁ ⊆ t₂) : s₁ • t₁ ⊆ s₂ • t₂ := image2_subset hs ht @[to_additive] lemma smul_subset_iff : s • t ⊆ u ↔ ∀ (a ∈ s) (b ∈ t), a • b ∈ u := image2_subset_iff @[simp, to_additive] lemma empty_smul : (∅ : set α) • t = ∅ := image2_empty_left @[simp, to_additive] lemma smul_empty : s • (∅ : set β) = ∅ := image2_empty_right @[simp, to_additive] lemma smul_singleton : s • {b} = (• b) '' s := image2_singleton_right @[simp, to_additive] lemma singleton_smul : ({a} : set α) • t = a • t := image2_singleton_left @[simp, to_additive] lemma singleton_smul_singleton : ({a} : set α) • ({b} : set β) = {a • b} := image2_singleton @[to_additive] lemma smul_subset_smul_left (h : t₁ ⊆ t₂) : s • t₁ ⊆ s • t₂ := image2_subset_left h @[to_additive] lemma smul_subset_smul_right (h : s₁ ⊆ s₂) : s₁ • t ⊆ s₂ • t := image2_subset_right h @[to_additive] lemma union_smul : (s₁ ∪ s₂) • t = s₁ • t ∪ s₂ • t := image2_union_left @[to_additive] lemma smul_union : s • (t₁ ∪ t₂) = s • t₁ ∪ s • t₂ := image2_union_right @[to_additive] lemma inter_smul_subset : (s₁ ∩ s₂) • t ⊆ s₁ • t ∩ s₂ • t := image2_inter_subset_left @[to_additive] lemma smul_inter_subset : s • (t₁ ∩ t₂) ⊆ s • t₁ ∩ s • t₂ := image2_inter_subset_right @[to_additive] lemma Union_smul_left_image : (⋃ a ∈ s, a • t) = s • t := Union_image_left _ @[to_additive] lemma Union_smul_right_image : (⋃ a ∈ t, (λ x, x • a) '' s) = s • t := Union_image_right _ @[to_additive] lemma Union_smul (s : ι → set α) (t : set β) : (⋃ i, s i) • t = ⋃ i, s i • t := image2_Union_left _ _ _ @[to_additive] lemma smul_Union (s : set α) (t : ι → set β) : s • (⋃ i, t i) = ⋃ i, s • t i := image2_Union_right _ _ _ @[to_additive] lemma Union₂_smul (s : Π i, κ i → set α) (t : set β) : (⋃ i j, s i j) • t = ⋃ i j, s i j • t := image2_Union₂_left _ _ _ @[to_additive] lemma smul_Union₂ (s : set α) (t : Π i, κ i → set β) : s • (⋃ i j, t i j) = ⋃ i j, s • t i j := image2_Union₂_right _ _ _ @[to_additive] lemma Inter_smul_subset (s : ι → set α) (t : set β) : (⋂ i, s i) • t ⊆ ⋂ i, s i • t := image2_Inter_subset_left _ _ _ @[to_additive] lemma smul_Inter_subset (s : set α) (t : ι → set β) : s • (⋂ i, t i) ⊆ ⋂ i, s • t i := image2_Inter_subset_right _ _ _ @[to_additive] lemma Inter₂_smul_subset (s : Π i, κ i → set α) (t : set β) : (⋂ i j, s i j) • t ⊆ ⋂ i j, s i j • t := image2_Inter₂_subset_left _ _ _ @[to_additive] lemma smul_Inter₂_subset (s : set α) (t : Π i, κ i → set β) : s • (⋂ i j, t i j) ⊆ ⋂ i j, s • t i j := image2_Inter₂_subset_right _ _ _ end has_scalar section has_scalar_set variables {ι : Sort*} {κ : ι → Sort*} [has_scalar α β] {s t t₁ t₂ : set β} {a : α} {b : β} {x y : β} @[simp, to_additive] lemma image_smul : (λ x, a • x) '' t = a • t := rfl @[to_additive] lemma mem_smul_set : x ∈ a • t ↔ ∃ y, y ∈ t ∧ a • y = x := iff.rfl @[to_additive] lemma smul_mem_smul_set (hy : y ∈ t) : a • y ∈ a • t := ⟨y, hy, rfl⟩ @[to_additive] lemma mem_smul_of_mem {s : set α} (ha : a ∈ s) (hb : b ∈ t) : a • b ∈ s • t := mem_image2_of_mem ha hb @[simp, to_additive] lemma smul_set_empty : a • (∅ : set β) = ∅ := image_empty _ @[simp, to_additive] lemma smul_set_singleton : a • ({b} : set β) = {a • b} := image_singleton @[to_additive] lemma smul_set_mono (h : s ⊆ t) : a • s ⊆ a • t := image_subset _ h @[to_additive] lemma smul_set_union : a • (t₁ ∪ t₂) = a • t₁ ∪ a • t₂ := image_union _ _ _ @[to_additive] lemma smul_set_inter_subset : a • (t₁ ∩ t₂) ⊆ a • t₁ ∩ (a • t₂) := image_inter_subset _ _ _ @[to_additive] lemma smul_set_Union (a : α) (s : ι → set β) : a • (⋃ i, s i) = ⋃ i, a • s i := image_Union @[to_additive] lemma smul_set_Union₂ (a : α) (s : Π i, κ i → set β) : a • (⋃ i j, s i j) = ⋃ i j, a • s i j := image_Union₂ _ _ @[to_additive] lemma smul_set_Inter_subset (a : α) (t : ι → set β) : a • (⋂ i, t i) ⊆ ⋂ i, a • t i := image_Inter_subset _ _ @[to_additive] lemma smul_set_Inter₂_subset (a : α) (t : Π i, κ i → set β) : a • (⋂ i j, t i j) ⊆ ⋂ i j, a • t i j := image_Inter₂_subset _ _ @[to_additive] lemma finite.smul_set (hs : finite s) : finite (a • s) := hs.image _ end has_scalar_set variables {s s₁ s₂ : set α} {t t₁ t₂ : set β} {a : α} {b : β} @[to_additive] lemma smul_set_inter [group α] [mul_action α β] {s t : set β} : a • (s ∩ t) = a • s ∩ a • t := (image_inter $ mul_action.injective a).symm lemma smul_set_inter₀ [group_with_zero α] [mul_action α β] {s t : set β} (ha : a ≠ 0) : a • (s ∩ t) = a • s ∩ a • t := show units.mk0 a ha • _ = _, from smul_set_inter @[simp, to_additive] lemma smul_set_univ [group α] [mul_action α β] {a : α} : a • (univ : set β) = univ := eq_univ_of_forall $ λ b, ⟨a⁻¹ • b, trivial, smul_inv_smul _ _⟩ @[simp, to_additive] lemma smul_univ [group α] [mul_action α β] {s : set α} (hs : s.nonempty) : s • (univ : set β) = univ := let ⟨a, ha⟩ := hs in eq_univ_of_forall $ λ b, ⟨a, a⁻¹ • b, ha, trivial, smul_inv_smul _ _⟩ @[to_additive] theorem range_smul_range {ι κ : Type*} [has_scalar α β] (b : ι → α) (c : κ → β) : range b • range c = range (λ p : ι × κ, b p.1 • c p.2) := ext $ λ x, ⟨λ hx, let ⟨p, q, ⟨i, hi⟩, ⟨j, hj⟩, hpq⟩ := set.mem_smul.1 hx in ⟨(i, j), hpq ▸ hi ▸ hj ▸ rfl⟩, λ ⟨⟨i, j⟩, h⟩, set.mem_smul.2 ⟨b i, c j, ⟨i, rfl⟩, ⟨j, rfl⟩, h⟩⟩ @[to_additive] instance smul_comm_class_set [has_scalar α γ] [has_scalar β γ] [smul_comm_class α β γ] : smul_comm_class α (set β) (set γ) := { smul_comm := λ a T T', by simp only [←image2_smul, ←image_smul, image2_image_right, image_image2, smul_comm] } @[to_additive] instance smul_comm_class_set' [has_scalar α γ] [has_scalar β γ] [smul_comm_class α β γ] : smul_comm_class (set α) β (set γ) := by haveI := smul_comm_class.symm α β γ; exact smul_comm_class.symm _ _ _ @[to_additive] instance smul_comm_class [has_scalar α γ] [has_scalar β γ] [smul_comm_class α β γ] : smul_comm_class (set α) (set β) (set γ) := { smul_comm := λ T T' T'', begin simp only [←image2_smul, image2_swap _ T], exact image2_assoc (λ b c a, smul_comm a b c), end } instance is_scalar_tower [has_scalar α β] [has_scalar α γ] [has_scalar β γ] [is_scalar_tower α β γ] : is_scalar_tower α β (set γ) := { smul_assoc := λ a b T, by simp only [←image_smul, image_image, smul_assoc] } instance is_scalar_tower' [has_scalar α β] [has_scalar α γ] [has_scalar β γ] [is_scalar_tower α β γ] : is_scalar_tower α (set β) (set γ) := { smul_assoc := λ a T T', by simp only [←image_smul, ←image2_smul, image_image2, image2_image_left, smul_assoc] } instance is_scalar_tower'' [has_scalar α β] [has_scalar α γ] [has_scalar β γ] [is_scalar_tower α β γ] : is_scalar_tower (set α) (set β) (set γ) := { smul_assoc := λ T T' T'', image2_assoc smul_assoc } instance is_central_scalar [has_scalar α β] [has_scalar αᵐᵒᵖ β] [is_central_scalar α β] : is_central_scalar α (set β) := ⟨λ a S, congr_arg (λ f, f '' S) $ by exact funext (λ _, op_smul_eq_smul _ _)⟩ end smul section vsub variables {ι : Sort*} {κ : ι → Sort*} [has_vsub α β] {s s₁ s₂ t t₁ t₂ : set β} {a : α} {b c : β} include α instance has_vsub : has_vsub (set α) (set β) := ⟨image2 (-ᵥ)⟩ @[simp] lemma image2_vsub : (image2 has_vsub.vsub s t : set α) = s -ᵥ t := rfl lemma image_vsub_prod : (λ x : β × β, x.fst -ᵥ x.snd) '' (s ×ˢ t) = s -ᵥ t := image_prod _ lemma mem_vsub : a ∈ s -ᵥ t ↔ ∃ x y, x ∈ s ∧ y ∈ t ∧ x -ᵥ y = a := iff.rfl lemma vsub_mem_vsub (hb : b ∈ s) (hc : c ∈ t) : b -ᵥ c ∈ s -ᵥ t := mem_image2_of_mem hb hc lemma vsub_subset_vsub (hs : s₁ ⊆ s₂) (ht : t₁ ⊆ t₂) : s₁ -ᵥ t₁ ⊆ s₂ -ᵥ t₂ := image2_subset hs ht lemma vsub_subset_iff {u : set α} : s -ᵥ t ⊆ u ↔ ∀ (x ∈ s) (y ∈ t), x -ᵥ y ∈ u := image2_subset_iff @[simp] lemma empty_vsub (t : set β) : ∅ -ᵥ t = ∅ := image2_empty_left @[simp] lemma vsub_empty (s : set β) : s -ᵥ ∅ = ∅ := image2_empty_right @[simp] lemma vsub_singleton (s : set β) (b : β) : s -ᵥ {b} = (-ᵥ b) '' s := image2_singleton_right @[simp] lemma singleton_vsub (t : set β) (b : β) : {b} -ᵥ t = ((-ᵥ) b) '' t := image2_singleton_left @[simp] lemma singleton_vsub_singleton : ({b} : set β) -ᵥ {c} = {b -ᵥ c} := image2_singleton lemma vsub_subset_vsub_left (h : t₁ ⊆ t₂) : s -ᵥ t₁ ⊆ s -ᵥ t₂ := image2_subset_left h lemma vsub_subset_vsub_right (h : s₁ ⊆ s₂) : s₁ -ᵥ t ⊆ s₂ -ᵥ t := image2_subset_right h lemma union_vsub : (s₁ ∪ s₂) -ᵥ t = s₁ -ᵥ t ∪ (s₂ -ᵥ t) := image2_union_left lemma vsub_union : s -ᵥ (t₁ ∪ t₂) = s -ᵥ t₁ ∪ (s -ᵥ t₂) := image2_union_right lemma inter_vsub_subset : s₁ ∩ s₂ -ᵥ t ⊆ (s₁ -ᵥ t) ∩ (s₂ -ᵥ t) := image2_inter_subset_left lemma vsub_inter_subset : s -ᵥ t₁ ∩ t₂ ⊆ (s -ᵥ t₁) ∩ (s -ᵥ t₂) := image2_inter_subset_right lemma Union_vsub_left_image : (⋃ a ∈ s, ((-ᵥ) a) '' t) = s -ᵥ t := Union_image_left _ lemma Union_vsub_right_image : (⋃ a ∈ t, (-ᵥ a) '' s) = s -ᵥ t := Union_image_right _ lemma Union_vsub (s : ι → set β) (t : set β) : (⋃ i, s i) -ᵥ t = ⋃ i, s i -ᵥ t := image2_Union_left _ _ _ lemma vsub_Union (s : set β) (t : ι → set β) : s -ᵥ (⋃ i, t i) = ⋃ i, s -ᵥ t i := image2_Union_right _ _ _ lemma Union₂_vsub (s : Π i, κ i → set β) (t : set β) : (⋃ i j, s i j) -ᵥ t = ⋃ i j, s i j -ᵥ t := image2_Union₂_left _ _ _ lemma vsub_Union₂ (s : set β) (t : Π i, κ i → set β) : s -ᵥ (⋃ i j, t i j) = ⋃ i j, s -ᵥ t i j := image2_Union₂_right _ _ _ lemma Inter_vsub_subset (s : ι → set β) (t : set β) : (⋂ i, s i) -ᵥ t ⊆ ⋂ i, s i -ᵥ t := image2_Inter_subset_left _ _ _ lemma vsub_Inter_subset (s : set β) (t : ι → set β) : s -ᵥ (⋂ i, t i) ⊆ ⋂ i, s -ᵥ t i := image2_Inter_subset_right _ _ _ lemma Inter₂_vsub_subset (s : Π i, κ i → set β) (t : set β) : (⋂ i j, s i j) -ᵥ t ⊆ ⋂ i j, s i j -ᵥ t := image2_Inter₂_subset_left _ _ _ lemma vsub_Inter₂_subset (s : set β) (t : Π i, κ i → set β) : s -ᵥ (⋂ i j, t i j) ⊆ ⋂ i j, s -ᵥ t i j := image2_Inter₂_subset_right _ _ _ lemma finite.vsub (hs : finite s) (ht : finite t) : finite (s -ᵥ t) := hs.image2 _ ht lemma vsub_self_mono (h : s ⊆ t) : s -ᵥ s ⊆ t -ᵥ t := vsub_subset_vsub h h end vsub open_locale pointwise section ring variables [ring α] [add_comm_group β] [module α β] {s : set α} {t : set β} {a : α} @[simp] lemma neg_smul_set : -a • t = -(a • t) := by simp_rw [←image_smul, ←image_neg, image_image, neg_smul] @[simp] lemma smul_set_neg : a • -t = -(a • t) := by simp_rw [←image_smul, ←image_neg, image_image, smul_neg] @[simp] protected lemma neg_smul : -s • t = -(s • t) := by simp_rw [←image2_smul, ←image_neg, image2_image_left, image_image2, neg_smul] @[simp] protected lemma smul_neg : s • -t = -(s • t) := by simp_rw [←image2_smul, ←image_neg, image2_image_right, image_image2, smul_neg] end ring section monoid /-! ### `set α` as a `(∪,*)`-semiring -/ /-- An alias for `set α`, which has a semiring structure given by `∪` as "addition" and pointwise multiplication `*` as "multiplication". -/ @[derive [inhabited, partial_order, order_bot]] def set_semiring (α : Type*) : Type* := set α /-- The identitiy function `set α → set_semiring α`. -/ protected def up (s : set α) : set_semiring α := s /-- The identitiy function `set_semiring α → set α`. -/ protected def set_semiring.down (s : set_semiring α) : set α := s @[simp] protected lemma down_up {s : set α} : s.up.down = s := rfl @[simp] protected lemma up_down {s : set_semiring α} : s.down.up = s := rfl /- This lemma is not tagged `simp`, since otherwise the linter complains. -/ lemma up_le_up {s t : set α} : s.up ≤ t.up ↔ s ⊆ t := iff.rfl /- This lemma is not tagged `simp`, since otherwise the linter complains. -/ lemma up_lt_up {s t : set α} : s.up < t.up ↔ s ⊂ t := iff.rfl @[simp] lemma down_subset_down {s t : set_semiring α} : s.down ⊆ t.down ↔ s ≤ t := iff.rfl @[simp] lemma down_ssubset_down {s t : set_semiring α} : s.down ⊂ t.down ↔ s < t := iff.rfl instance set_semiring.add_comm_monoid : add_comm_monoid (set_semiring α) := { add := λ s t, (s ∪ t : set α), zero := (∅ : set α), add_assoc := union_assoc, zero_add := empty_union, add_zero := union_empty, add_comm := union_comm, } instance set_semiring.non_unital_non_assoc_semiring [has_mul α] : non_unital_non_assoc_semiring (set_semiring α) := { zero_mul := λ s, empty_mul, mul_zero := λ s, mul_empty, left_distrib := λ _ _ _, mul_union, right_distrib := λ _ _ _, union_mul, ..set.has_mul, ..set_semiring.add_comm_monoid } instance set_semiring.non_assoc_semiring [mul_one_class α] : non_assoc_semiring (set_semiring α) := { ..set_semiring.non_unital_non_assoc_semiring, ..set.mul_one_class } instance set_semiring.non_unital_semiring [semigroup α] : non_unital_semiring (set_semiring α) := { ..set_semiring.non_unital_non_assoc_semiring, ..set.semigroup } instance set_semiring.semiring [monoid α] : semiring (set_semiring α) := { ..set_semiring.non_assoc_semiring, ..set_semiring.non_unital_semiring } instance set_semiring.comm_semiring [comm_monoid α] : comm_semiring (set_semiring α) := { ..set.comm_monoid, ..set_semiring.semiring } /-- A multiplicative action of a monoid on a type β gives also a multiplicative action on the subsets of β. -/ @[to_additive "An additive action of an additive monoid on a type β gives also an additive action on the subsets of β."] protected def mul_action_set [monoid α] [mul_action α β] : mul_action α (set β) := { mul_smul := by { intros, simp only [← image_smul, image_image, ← mul_smul] }, one_smul := by { intros, simp only [← image_smul, image_eta, one_smul, image_id'] }, ..set.has_scalar_set } localized "attribute [instance] set.mul_action_set set.add_action_set" in pointwise section mul_hom variables [has_mul α] [has_mul β] (m : mul_hom α β) {s t : set α} @[to_additive] lemma image_mul : m '' (s * t) = m '' s * m '' t := by { simp only [← image2_mul, image_image2, image2_image_left, image2_image_right, m.map_mul] } @[to_additive] lemma preimage_mul_preimage_subset {s t : set β} : m ⁻¹' s * m ⁻¹' t ⊆ m ⁻¹' (s * t) := by { rintros _ ⟨_, _, _, _, rfl⟩, exact ⟨_, _, ‹_›, ‹_›, (m.map_mul _ _).symm ⟩ } instance set_semiring.no_zero_divisors : no_zero_divisors (set_semiring α) := ⟨λ a b ab, a.eq_empty_or_nonempty.imp_right $ λ ha, b.eq_empty_or_nonempty.resolve_right $ λ hb, nonempty.ne_empty ⟨_, mul_mem_mul ha.some_mem hb.some_mem⟩ ab⟩ /- Since addition on `set_semiring` is commutative (it is set union), there is no need to also have the instance `covariant_class (set_semiring α) (set_semiring α) (swap (+)) (≤)`. -/ instance set_semiring.covariant_class_add : covariant_class (set_semiring α) (set_semiring α) (+) (≤) := { elim := λ a b c, union_subset_union_right _ } instance set_semiring.covariant_class_mul_left : covariant_class (set_semiring α) (set_semiring α) (*) (≤) := { elim := λ a b c, mul_subset_mul_left } instance set_semiring.covariant_class_mul_right : covariant_class (set_semiring α) (set_semiring α) (swap (*)) (≤) := { elim := λ a b c, mul_subset_mul_right } end mul_hom /-- The image of a set under a multiplicative homomorphism is a ring homomorphism with respect to the pointwise operations on sets. -/ def image_hom [monoid α] [monoid β] (f : α →* β) : set_semiring α →+* set_semiring β := { to_fun := image f, map_zero' := image_empty _, map_one' := by simp only [← singleton_one, image_singleton, f.map_one], map_add' := image_union _, map_mul' := λ _ _, image_mul f.to_mul_hom } end monoid section comm_monoid variable [comm_monoid α] instance : canonically_ordered_comm_semiring (set_semiring α) := { add_le_add_left := λ a b, add_le_add_left, le_iff_exists_add := λ a b, ⟨λ ab, ⟨b, (union_eq_right_iff_subset.2 ab).symm⟩, by { rintro ⟨c, rfl⟩, exact subset_union_left _ _ }⟩, ..(infer_instance : comm_semiring (set_semiring α)), ..(infer_instance : partial_order (set_semiring α)), ..(infer_instance : order_bot (set_semiring α)), ..(infer_instance : no_zero_divisors (set_semiring α)) } end comm_monoid end set open set open_locale pointwise section section smul_with_zero variables [has_zero α] [has_zero β] [smul_with_zero α β] /-- A nonempty set is scaled by zero to the singleton set containing 0. -/ lemma zero_smul_set {s : set β} (h : s.nonempty) : (0 : α) • s = (0 : set β) := by simp only [← image_smul, image_eta, zero_smul, h.image_const, singleton_zero] lemma zero_smul_subset (s : set β) : (0 : α) • s ⊆ 0 := image_subset_iff.2 $ λ x _, zero_smul α x lemma subsingleton_zero_smul_set (s : set β) : ((0 : α) • s).subsingleton := subsingleton_singleton.mono (zero_smul_subset s) lemma zero_mem_smul_set {t : set β} {a : α} (h : (0 : β) ∈ t) : (0 : β) ∈ a • t := ⟨0, h, smul_zero' _ _⟩ variables [no_zero_smul_divisors α β] {s : set α} {t : set β} {a : α} lemma zero_mem_smul_iff : (0 : β) ∈ s • t ↔ (0 : α) ∈ s ∧ t.nonempty ∨ (0 : β) ∈ t ∧ s.nonempty := begin split, { rintro ⟨a, b, ha, hb, h⟩, obtain rfl | rfl := eq_zero_or_eq_zero_of_smul_eq_zero h, { exact or.inl ⟨ha, b, hb⟩ }, { exact or.inr ⟨hb, a, ha⟩ } }, { rintro (⟨hs, b, hb⟩ | ⟨ht, a, ha⟩), { exact ⟨0, b, hs, hb, zero_smul _ _⟩ }, { exact ⟨a, 0, ha, ht, smul_zero' _ _⟩ } } end lemma zero_mem_smul_set_iff (ha : a ≠ 0) : (0 : β) ∈ a • t ↔ (0 : β) ∈ t := begin refine ⟨_, zero_mem_smul_set⟩, rintro ⟨b, hb, h⟩, rwa (eq_zero_or_eq_zero_of_smul_eq_zero h).resolve_left ha at hb, end end smul_with_zero lemma smul_add_set [monoid α] [add_monoid β] [distrib_mul_action α β] (c : α) (s t : set β) : c • (s + t) = c • s + c • t := image_add (distrib_mul_action.to_add_monoid_hom β c).to_add_hom section group variables [group α] [mul_action α β] {A B : set β} {a : α} {x : β} @[simp, to_additive] lemma smul_mem_smul_set_iff : a • x ∈ a • A ↔ x ∈ A := ⟨λ h, begin rw [←inv_smul_smul a x, ←inv_smul_smul a A], exact smul_mem_smul_set h, end, smul_mem_smul_set⟩ @[to_additive] lemma mem_smul_set_iff_inv_smul_mem : x ∈ a • A ↔ a⁻¹ • x ∈ A := show x ∈ mul_action.to_perm a '' A ↔ _, from mem_image_equiv @[to_additive] lemma mem_inv_smul_set_iff : x ∈ a⁻¹ • A ↔ a • x ∈ A := by simp only [← image_smul, mem_image, inv_smul_eq_iff, exists_eq_right] @[to_additive] lemma preimage_smul (a : α) (t : set β) : (λ x, a • x) ⁻¹' t = a⁻¹ • t := ((mul_action.to_perm a).symm.image_eq_preimage _).symm @[to_additive] lemma preimage_smul_inv (a : α) (t : set β) : (λ x, a⁻¹ • x) ⁻¹' t = a • t := preimage_smul (to_units a)⁻¹ t @[simp, to_additive] lemma set_smul_subset_set_smul_iff : a • A ⊆ a • B ↔ A ⊆ B := image_subset_image_iff $ mul_action.injective _ @[to_additive] lemma set_smul_subset_iff : a • A ⊆ B ↔ A ⊆ a⁻¹ • B := (image_subset_iff).trans $ iff_of_eq $ congr_arg _ $ preimage_equiv_eq_image_symm _ $ mul_action.to_perm _ @[to_additive] lemma subset_set_smul_iff : A ⊆ a • B ↔ a⁻¹ • A ⊆ B := iff.symm $ (image_subset_iff).trans $ iff.symm $ iff_of_eq $ congr_arg _ $ image_equiv_eq_preimage_symm _ $ mul_action.to_perm _ end group section group_with_zero variables [group_with_zero α] [mul_action α β] {s : set α} {a : α} @[simp] lemma smul_mem_smul_set_iff₀ (ha : a ≠ 0) (A : set β) (x : β) : a • x ∈ a • A ↔ x ∈ A := show units.mk0 a ha • _ ∈ _ ↔ _, from smul_mem_smul_set_iff lemma mem_smul_set_iff_inv_smul_mem₀ (ha : a ≠ 0) (A : set β) (x : β) : x ∈ a • A ↔ a⁻¹ • x ∈ A := show _ ∈ units.mk0 a ha • _ ↔ _, from mem_smul_set_iff_inv_smul_mem lemma mem_inv_smul_set_iff₀ (ha : a ≠ 0) (A : set β) (x : β) : x ∈ a⁻¹ • A ↔ a • x ∈ A := show _ ∈ (units.mk0 a ha)⁻¹ • _ ↔ _, from mem_inv_smul_set_iff lemma preimage_smul₀ (ha : a ≠ 0) (t : set β) : (λ x, a • x) ⁻¹' t = a⁻¹ • t := preimage_smul (units.mk0 a ha) t lemma preimage_smul_inv₀ (ha : a ≠ 0) (t : set β) : (λ x, a⁻¹ • x) ⁻¹' t = a • t := preimage_smul ((units.mk0 a ha)⁻¹) t @[simp] lemma set_smul_subset_set_smul_iff₀ (ha : a ≠ 0) {A B : set β} : a • A ⊆ a • B ↔ A ⊆ B := show units.mk0 a ha • _ ⊆ _ ↔ _, from set_smul_subset_set_smul_iff lemma set_smul_subset_iff₀ (ha : a ≠ 0) {A B : set β} : a • A ⊆ B ↔ A ⊆ a⁻¹ • B := show units.mk0 a ha • _ ⊆ _ ↔ _, from set_smul_subset_iff lemma subset_set_smul_iff₀ (ha : a ≠ 0) {A B : set β} : A ⊆ a • B ↔ a⁻¹ • A ⊆ B := show _ ⊆ units.mk0 a ha • _ ↔ _, from subset_set_smul_iff lemma smul_univ₀ (hs : ¬ s ⊆ 0) : s • (univ : set β) = univ := let ⟨a, ha, ha₀⟩ := not_subset.1 hs in eq_univ_of_forall $ λ b, ⟨a, a⁻¹ • b, ha, trivial, smul_inv_smul₀ ha₀ _⟩ lemma smul_set_univ₀ (ha : a ≠ 0) : a • (univ : set β) = univ := eq_univ_of_forall $ λ b, ⟨a⁻¹ • b, trivial, smul_inv_smul₀ ha _⟩ end group_with_zero end namespace finset variables {a : α} {s s₁ s₂ t t₁ t₂ : finset α} /-- The finset `(1 : finset α)` is defined as `{1}` in locale `pointwise`. -/ @[to_additive /-"The finset `(0 : finset α)` is defined as `{0}` in locale `pointwise`. "-/] protected def has_one [has_one α] : has_one (finset α) := ⟨{1}⟩ localized "attribute [instance] finset.has_one finset.has_zero" in pointwise @[simp, to_additive] lemma mem_one [has_one α] : a ∈ (1 : finset α) ↔ a = 1 := by simp [has_one.one] @[simp, to_additive] theorem one_subset [has_one α] : (1 : finset α) ⊆ s ↔ (1 : α) ∈ s := singleton_subset_iff section decidable_eq variables [decidable_eq α] /-- The pointwise product of two finite sets `s` and `t`: `st = s ⬝ t = s * t = { x * y | x ∈ s, y ∈ t }`. -/ @[to_additive /-"The pointwise sum of two finite sets `s` and `t`: `s + t = { x + y | x ∈ s, y ∈ t }`. "-/] protected def has_mul [has_mul α] : has_mul (finset α) := ⟨λ s t, (s.product t).image (λ p : α × α, p.1 * p.2)⟩ localized "attribute [instance] finset.has_mul finset.has_add" in pointwise section has_mul variables [has_mul α] @[to_additive] lemma mul_def : s * t = (s.product t).image (λ p : α × α, p.1 * p.2) := rfl @[to_additive] lemma mem_mul {x : α} : x ∈ s * t ↔ ∃ y z, y ∈ s ∧ z ∈ t ∧ y * z = x := by { simp only [finset.mul_def, and.assoc, mem_image, exists_prop, prod.exists, mem_product] } @[simp, norm_cast, to_additive] lemma coe_mul : (↑(s * t) : set α) = ↑s * ↑t := by { ext, simp only [mem_mul, set.mem_mul, mem_coe] } @[to_additive] lemma mul_mem_mul {x y : α} (hx : x ∈ s) (hy : y ∈ t) : x * y ∈ s * t := by { simp only [finset.mem_mul], exact ⟨x, y, hx, hy, rfl⟩ } @[to_additive] lemma mul_card_le : (s * t).card ≤ s.card * t.card := by { convert finset.card_image_le, rw [finset.card_product, mul_comm] } @[simp, to_additive] lemma empty_mul (s : finset α) : ∅ * s = ∅ := eq_empty_of_forall_not_mem (by simp [mem_mul]) @[simp, to_additive] lemma mul_empty (s : finset α) : s * ∅ = ∅ := eq_empty_of_forall_not_mem (by simp [mem_mul]) @[simp, to_additive] lemma mul_nonempty_iff (s t : finset α) : (s * t).nonempty ↔ s.nonempty ∧ t.nonempty := by simp [finset.mul_def] @[to_additive, mono] lemma mul_subset_mul (hs : s₁ ⊆ s₂) (ht : t₁ ⊆ t₂) : s₁ * t₁ ⊆ s₂ * t₂ := image_subset_image (product_subset_product hs ht) attribute [mono] add_subset_add @[simp, to_additive] lemma mul_singleton (a : α) : s * {a} = s.image (* a) := by { rw [mul_def, product_singleton, map_eq_image, image_image], refl } @[simp, to_additive] lemma singleton_mul (a : α) : {a} * s = s.image ((*) a) := by { rw [mul_def, singleton_product, map_eq_image, image_image], refl } @[simp, to_additive] lemma singleton_mul_singleton (a b : α) : ({a} : finset α) * {b} = {a * b} := by rw [mul_def, singleton_product_singleton, image_singleton] end has_mul section mul_zero_class variables [mul_zero_class α] lemma mul_zero_subset (s : finset α) : s * 0 ⊆ 0 := by simp [subset_iff, mem_mul] lemma zero_mul_subset (s : finset α) : 0 * s ⊆ 0 := by simp [subset_iff, mem_mul] lemma nonempty.mul_zero (hs : s.nonempty) : s * 0 = 0 := s.mul_zero_subset.antisymm $ by simpa [finset.mem_mul] using hs lemma nonempty.zero_mul (hs : s.nonempty) : 0 * s = 0 := s.zero_mul_subset.antisymm $ by simpa [finset.mem_mul] using hs lemma singleton_zero_mul (s : finset α) : {(0 : α)} * s ⊆ {0} := by simp [subset_iff, mem_mul] end mul_zero_class end decidable_eq open_locale pointwise variables {u : finset α} {b : α} {x y : β} @[to_additive] lemma singleton_one [has_one α] : ({1} : finset α) = 1 := rfl @[to_additive] lemma one_mem_one [has_one α] : (1 : α) ∈ (1 : finset α) := by simp [has_one.one] @[to_additive] theorem one_nonempty [has_one α] : (1 : finset α).nonempty := ⟨1, one_mem_one⟩ @[simp, to_additive] theorem image_one [decidable_eq β] [has_one α] {f : α → β} : image f 1 = {f 1} := image_singleton f 1 @[to_additive add_image_prod] lemma image_mul_prod [decidable_eq α] [has_mul α] : image (λ x : α × α, x.fst * x.snd) (s.product t) = s * t := rfl @[simp, to_additive] lemma image_mul_left [decidable_eq α] [group α] : image (λ b, a * b) t = preimage t (λ b, a⁻¹ * b) (assume x hx y hy, (mul_right_inj a⁻¹).mp) := coe_injective $ by simp @[simp, to_additive] lemma image_mul_right [decidable_eq α] [group α] : image (* b) t = preimage t (* b⁻¹) (assume x hx y hy, (mul_left_inj b⁻¹).mp) := coe_injective $ by simp @[to_additive] lemma image_mul_left' [decidable_eq α] [group α] : image (λ b, a⁻¹ * b) t = preimage t (λ b, a * b) (assume x hx y hy, (mul_right_inj a).mp) := by simp @[to_additive] lemma image_mul_right' [decidable_eq α] [group α] : image (* b⁻¹) t = preimage t (* b) (assume x hx y hy, (mul_left_inj b).mp) := by simp @[simp, to_additive] lemma preimage_mul_left_singleton [group α] : preimage {b} ((*) a) (assume x hx y hy, (mul_right_inj a).mp) = {a⁻¹ * b} := by { classical, rw [← image_mul_left', image_singleton] } @[simp, to_additive] lemma preimage_mul_right_singleton [group α] : preimage {b} (* a) (assume x hx y hy, (mul_left_inj a).mp) = {b * a⁻¹} := by { classical, rw [← image_mul_right', image_singleton] } @[simp, to_additive] lemma preimage_mul_left_one [group α] : preimage 1 (λ b, a * b) (assume x hx y hy, (mul_right_inj a).mp) = {a⁻¹} := by {classical, rw [← image_mul_left', image_one, mul_one] } @[simp, to_additive] lemma preimage_mul_right_one [group α] : preimage 1 (* b) (assume x hx y hy, (mul_left_inj b).mp) = {b⁻¹} := by {classical, rw [← image_mul_right', image_one, one_mul] } @[to_additive] lemma preimage_mul_left_one' [group α] : preimage 1 (λ b, a⁻¹ * b) (assume x hx y hy, (mul_right_inj _).mp) = {a} := by simp @[to_additive] lemma preimage_mul_right_one' [group α] : preimage 1 (* b⁻¹) (assume x hx y hy, (mul_left_inj _).mp) = {b} := by simp @[to_additive] protected lemma mul_comm [decidable_eq α] [comm_semigroup α] : s * t = t * s := by exact_mod_cast @set.mul_comm _ (s : set α) t _ /-- `finset α` is a `mul_one_class` under pointwise operations if `α` is. -/ @[to_additive /-"`finset α` is an `add_zero_class` under pointwise operations if `α` is."-/] protected def mul_one_class [decidable_eq α] [mul_one_class α] : mul_one_class (finset α) := function.injective.mul_one_class _ coe_injective (coe_singleton 1) (by simp) /-- `finset α` is a `semigroup` under pointwise operations if `α` is. -/ @[to_additive /-"`finset α` is an `add_semigroup` under pointwise operations if `α` is. "-/] protected def semigroup [decidable_eq α] [semigroup α] : semigroup (finset α) := function.injective.semigroup _ coe_injective (by simp) /-- `finset α` is a `monoid` under pointwise operations if `α` is. -/ @[to_additive /-"`finset α` is an `add_monoid` under pointwise operations if `α` is. "-/] protected def monoid [decidable_eq α] [monoid α] : monoid (finset α) := function.injective.monoid _ coe_injective (coe_singleton 1) (by simp) /-- `finset α` is a `comm_monoid` under pointwise operations if `α` is. -/ @[to_additive /-"`finset α` is an `add_comm_monoid` under pointwise operations if `α` is. "-/] protected def comm_monoid [decidable_eq α] [comm_monoid α] : comm_monoid (finset α) := function.injective.comm_monoid _ coe_injective (coe_singleton 1) (by simp) localized "attribute [instance] finset.mul_one_class finset.add_zero_class finset.semigroup finset.add_semigroup finset.monoid finset.add_monoid finset.comm_monoid finset.add_comm_monoid" in pointwise open_locale classical /-- A finite set `U` contained in the product of two sets `S * S'` is also contained in the product of two finite sets `T * T' ⊆ S * S'`. -/ @[to_additive] lemma subset_mul {M : Type*} [monoid M] {S : set M} {S' : set M} {U : finset M} (f : ↑U ⊆ S * S') : ∃ (T T' : finset M), ↑T ⊆ S ∧ ↑T' ⊆ S' ∧ U ⊆ T * T' := begin apply finset.induction_on' U, { use [∅, ∅], simp only [finset.empty_subset, finset.coe_empty, set.empty_subset, and_self], }, rintros a s haU hs has ⟨T, T', hS, hS', h⟩, obtain ⟨x, y, hx, hy, ha⟩ := set.mem_mul.1 (f haU), use [insert x T, insert y T'], simp only [finset.coe_insert], repeat { rw [set.insert_subset], }, use [hx, hS, hy, hS'], refine finset.insert_subset.mpr ⟨_, _⟩, { rw finset.mem_mul, use [x,y], simpa only [true_and, true_or, eq_self_iff_true, finset.mem_insert], }, { suffices g : (s : set M) ⊆ insert x T * insert y T', { norm_cast at g, assumption, }, transitivity ↑(T * T'), apply h, rw finset.coe_mul, apply set.mul_subset_mul (set.subset_insert x T) (set.subset_insert y T'), }, end end finset /-! Some lemmas about pointwise multiplication and submonoids. Ideally we put these in `group_theory.submonoid.basic`, but currently we cannot because that file is imported by this. -/ namespace submonoid variables {M : Type*} [monoid M] {s t u : set M} @[to_additive] lemma mul_subset {S : submonoid M} (hs : s ⊆ S) (ht : t ⊆ S) : s * t ⊆ S := by { rintro _ ⟨p, q, hp, hq, rfl⟩, exact submonoid.mul_mem _ (hs hp) (ht hq) } @[to_additive] lemma mul_subset_closure (hs : s ⊆ u) (ht : t ⊆ u) : s * t ⊆ submonoid.closure u := mul_subset (subset.trans hs submonoid.subset_closure) (subset.trans ht submonoid.subset_closure) @[to_additive] lemma coe_mul_self_eq (s : submonoid M) : (s : set M) * s = s := begin ext x, refine ⟨_, λ h, ⟨x, 1, h, s.one_mem, mul_one x⟩⟩, rintros ⟨a, b, ha, hb, rfl⟩, exact s.mul_mem ha hb end @[to_additive] lemma closure_mul_le (S T : set M) : closure (S * T) ≤ closure S ⊔ closure T := Inf_le $ λ x ⟨s, t, hs, ht, hx⟩, hx ▸ (closure S ⊔ closure T).mul_mem (set_like.le_def.mp le_sup_left $ subset_closure hs) (set_like.le_def.mp le_sup_right $ subset_closure ht) @[to_additive] lemma sup_eq_closure (H K : submonoid M) : H ⊔ K = closure (H * K) := le_antisymm (sup_le (λ h hh, subset_closure ⟨h, 1, hh, K.one_mem, mul_one h⟩) (λ k hk, subset_closure ⟨1, k, H.one_mem, hk, one_mul k⟩)) (by conv_rhs { rw [← closure_eq H, ← closure_eq K] }; apply closure_mul_le) lemma pow_smul_mem_closure_smul {N : Type*} [comm_monoid N] [mul_action M N] [is_scalar_tower M N N] (r : M) (s : set N) {x : N} (hx : x ∈ closure s) : ∃ n : ℕ, r ^ n • x ∈ closure (r • s) := begin apply @closure_induction N _ s (λ (x : N), ∃ n : ℕ, r ^ n • x ∈ closure (r • s)) _ hx, { intros x hx, exact ⟨1, subset_closure ⟨_, hx, by rw pow_one⟩⟩ }, { exact ⟨0, by simpa using one_mem _⟩ }, { rintros x y ⟨nx, hx⟩ ⟨ny, hy⟩, use nx + ny, convert mul_mem _ hx hy, rw [pow_add, smul_mul_assoc, mul_smul, mul_comm, ← smul_mul_assoc, mul_comm] } end end submonoid namespace group lemma card_pow_eq_card_pow_card_univ_aux {f : ℕ → ℕ} (h1 : monotone f) {B : ℕ} (h2 : ∀ n, f n ≤ B) (h3 : ∀ n, f n = f (n + 1) → f (n + 1) = f (n + 2)) : ∀ k, B ≤ k → f k = f B := begin have key : ∃ n : ℕ, n ≤ B ∧ f n = f (n + 1), { contrapose! h2, suffices : ∀ n : ℕ, n ≤ B + 1 → n ≤ f n, { exact ⟨B + 1, this (B + 1) (le_refl (B + 1))⟩ }, exact λ n, nat.rec (λ h, nat.zero_le (f 0)) (λ n ih h, lt_of_le_of_lt (ih (n.le_succ.trans h)) (lt_of_le_of_ne (h1 n.le_succ) (h2 n (nat.succ_le_succ_iff.mp h)))) n }, { obtain ⟨n, hn1, hn2⟩ := key, replace key : ∀ k : ℕ, f (n + k) = f (n + k + 1) ∧ f (n + k) = f n := λ k, nat.rec ⟨hn2, rfl⟩ (λ k ih, ⟨h3 _ ih.1, ih.1.symm.trans ih.2⟩) k, replace key : ∀ k : ℕ, n ≤ k → f k = f n := λ k hk, (congr_arg f (add_tsub_cancel_of_le hk)).symm.trans (key (k - n)).2, exact λ k hk, (key k (hn1.trans hk)).trans (key B hn1).symm }, end variables {G : Type*} [group G] [fintype G] (S : set G) @[to_additive] lemma card_pow_eq_card_pow_card_univ [∀ (k : ℕ), decidable_pred (∈ (S ^ k))] : ∀ k, fintype.card G ≤ k → fintype.card ↥(S ^ k) = fintype.card ↥(S ^ (fintype.card G)) := begin have hG : 0 < fintype.card G := fintype.card_pos_iff.mpr ⟨1⟩, by_cases hS : S = ∅, { refine λ k hk, fintype.card_congr _, rw [hS, empty_pow _ (ne_of_gt (lt_of_lt_of_le hG hk)), empty_pow _ (ne_of_gt hG)] }, obtain ⟨a, ha⟩ := set.ne_empty_iff_nonempty.mp hS, classical, have key : ∀ a (s t : set G), (∀ b : G, b ∈ s → a * b ∈ t) → fintype.card s ≤ fintype.card t, { refine λ a s t h, fintype.card_le_of_injective (λ ⟨b, hb⟩, ⟨a * b, h b hb⟩) _, rintros ⟨b, hb⟩ ⟨c, hc⟩ hbc, exact subtype.ext (mul_left_cancel (subtype.ext_iff.mp hbc)) }, have mono : monotone (λ n, fintype.card ↥(S ^ n) : ℕ → ℕ) := monotone_nat_of_le_succ (λ n, key a _ _ (λ b hb, set.mul_mem_mul ha hb)), convert card_pow_eq_card_pow_card_univ_aux mono (λ n, set_fintype_card_le_univ (S ^ n)) (λ n h, le_antisymm (mono (n + 1).le_succ) (key a⁻¹ _ _ _)), { simp only [finset.filter_congr_decidable, fintype.card_of_finset] }, replace h : {a} * S ^ n = S ^ (n + 1), { refine set.eq_of_subset_of_card_le _ (le_trans (ge_of_eq h) _), { exact mul_subset_mul (set.singleton_subset_iff.mpr ha) set.subset.rfl }, { convert key a (S ^ n) ({a} * S ^ n) (λ b hb, set.mul_mem_mul (set.mem_singleton a) hb) } }, rw [pow_succ', ←h, mul_assoc, ←pow_succ', h], rintros _ ⟨b, c, hb, hc, rfl⟩, rwa [set.mem_singleton_iff.mp hb, inv_mul_cancel_left], end end group
1ffc1a5a84fe032266fbc78591e62b00d2036c7e
5fbbd711f9bfc21ee168f46a4be146603ece8835
/lean/natural_number_game/proposition/1.lean
912ba2589d49a95d399fe911f7b882677e296a6d
[ "LicenseRef-scancode-warranty-disclaimer" ]
no_license
goedel-gang/maths
22596f71e3fde9c088e59931f128a3b5efb73a2c
a20a6f6a8ce800427afd595c598a5ad43da1408d
refs/heads/master
1,623,055,941,960
1,621,599,441,000
1,621,599,441,000
169,335,840
0
0
null
null
null
null
UTF-8
Lean
false
false
73
lean
example (P Q : Prop) (p : P) (h : P → Q) : Q := begin exact h p, end
b9a05554bac53d628419c7725914cc36f3152766
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/list/range.lean
74b19b0880d7c984272690d06e6f6d59c7f03b2d
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
8,349
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Kenny Lau, Scott Morrison -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.list.chain import Mathlib.data.list.nodup import Mathlib.data.list.of_fn import Mathlib.data.list.zip import Mathlib.PostPort universes u u_1 namespace Mathlib namespace list /- iota and range(') -/ @[simp] theorem length_range' (s : ℕ) (n : ℕ) : length (range' s n) = n := sorry @[simp] theorem range'_eq_nil {s : ℕ} {n : ℕ} : range' s n = [] ↔ n = 0 := eq.mpr (id (Eq._oldrec (Eq.refl (range' s n = [] ↔ n = 0)) (Eq.symm (propext length_eq_zero)))) (eq.mpr (id (Eq._oldrec (Eq.refl (length (range' s n) = 0 ↔ n = 0)) (length_range' s n))) (iff.refl (n = 0))) @[simp] theorem mem_range' {m : ℕ} {s : ℕ} {n : ℕ} : m ∈ range' s n ↔ s ≤ m ∧ m < s + n := sorry theorem map_add_range' (a : ℕ) (s : ℕ) (n : ℕ) : map (Add.add a) (range' s n) = range' (a + s) n := sorry theorem map_sub_range' (a : ℕ) (s : ℕ) (n : ℕ) (h : a ≤ s) : map (fun (x : ℕ) => x - a) (range' s n) = range' (s - a) n := sorry theorem chain_succ_range' (s : ℕ) (n : ℕ) : chain (fun (a b : ℕ) => b = Nat.succ a) s (range' (s + 1) n) := sorry theorem chain_lt_range' (s : ℕ) (n : ℕ) : chain Less s (range' (s + 1) n) := chain.imp (fun (a b : ℕ) (e : b = Nat.succ a) => Eq.symm e ▸ nat.lt_succ_self a) (chain_succ_range' s n) theorem pairwise_lt_range' (s : ℕ) (n : ℕ) : pairwise Less (range' s n) := sorry theorem nodup_range' (s : ℕ) (n : ℕ) : nodup (range' s n) := pairwise.imp (fun (a b : ℕ) => ne_of_lt) (pairwise_lt_range' s n) @[simp] theorem range'_append (s : ℕ) (m : ℕ) (n : ℕ) : range' s m ++ range' (s + m) n = range' s (n + m) := sorry theorem range'_sublist_right {s : ℕ} {m : ℕ} {n : ℕ} : range' s m <+ range' s n ↔ m ≤ n := sorry theorem range'_subset_right {s : ℕ} {m : ℕ} {n : ℕ} : range' s m ⊆ range' s n ↔ m ≤ n := sorry theorem nth_range' (s : ℕ) {m : ℕ} {n : ℕ} : m < n → nth (range' s n) m = some (s + m) := sorry @[simp] theorem nth_le_range' {n : ℕ} {m : ℕ} (i : ℕ) (H : i < length (range' n m)) : nth_le (range' n m) i H = n + i := sorry theorem range'_concat (s : ℕ) (n : ℕ) : range' s (n + 1) = range' s n ++ [s + n] := eq.mpr (id (Eq._oldrec (Eq.refl (range' s (n + 1) = range' s n ++ [s + n])) (add_comm n 1))) (Eq.symm (range'_append s n 1)) theorem range_core_range' (s : ℕ) (n : ℕ) : range_core s (range' s n) = range' 0 (n + s) := sorry theorem range_eq_range' (n : ℕ) : range n = range' 0 n := Eq.trans (range_core_range' n 0) (eq.mpr (id (Eq._oldrec (Eq.refl (range' 0 (0 + n) = range' 0 n)) (zero_add n))) (Eq.refl (range' 0 n))) theorem range_succ_eq_map (n : ℕ) : range (n + 1) = 0 :: map Nat.succ (range n) := sorry theorem range'_eq_map_range (s : ℕ) (n : ℕ) : range' s n = map (Add.add s) (range n) := eq.mpr (id (Eq._oldrec (Eq.refl (range' s n = map (Add.add s) (range n))) (range_eq_range' n))) (eq.mpr (id (Eq._oldrec (Eq.refl (range' s n = map (Add.add s) (range' 0 n))) (map_add_range' s 0 n))) (Eq.refl (range' s n))) @[simp] theorem length_range (n : ℕ) : length (range n) = n := sorry @[simp] theorem range_eq_nil {n : ℕ} : range n = [] ↔ n = 0 := eq.mpr (id (Eq._oldrec (Eq.refl (range n = [] ↔ n = 0)) (Eq.symm (propext length_eq_zero)))) (eq.mpr (id (Eq._oldrec (Eq.refl (length (range n) = 0 ↔ n = 0)) (length_range n))) (iff.refl (n = 0))) theorem pairwise_lt_range (n : ℕ) : pairwise Less (range n) := sorry theorem nodup_range (n : ℕ) : nodup (range n) := sorry theorem range_sublist {m : ℕ} {n : ℕ} : range m <+ range n ↔ m ≤ n := sorry theorem range_subset {m : ℕ} {n : ℕ} : range m ⊆ range n ↔ m ≤ n := sorry @[simp] theorem mem_range {m : ℕ} {n : ℕ} : m ∈ range n ↔ m < n := sorry @[simp] theorem not_mem_range_self {n : ℕ} : ¬n ∈ range n := mt (iff.mp mem_range) (lt_irrefl n) @[simp] theorem self_mem_range_succ (n : ℕ) : n ∈ range (n + 1) := sorry theorem nth_range {m : ℕ} {n : ℕ} (h : m < n) : nth (range n) m = some m := sorry theorem range_succ (n : ℕ) : range (Nat.succ n) = range n ++ [n] := sorry @[simp] theorem range_zero : range 0 = [] := rfl theorem iota_eq_reverse_range' (n : ℕ) : iota n = reverse (range' 1 n) := sorry @[simp] theorem length_iota (n : ℕ) : length (iota n) = n := sorry theorem pairwise_gt_iota (n : ℕ) : pairwise gt (iota n) := sorry theorem nodup_iota (n : ℕ) : nodup (iota n) := sorry theorem mem_iota {m : ℕ} {n : ℕ} : m ∈ iota n ↔ 1 ≤ m ∧ m ≤ n := sorry theorem reverse_range' (s : ℕ) (n : ℕ) : reverse (range' s n) = map (fun (i : ℕ) => s + n - 1 - i) (range n) := sorry /-- All elements of `fin n`, from `0` to `n-1`. -/ def fin_range (n : ℕ) : List (fin n) := pmap fin.mk (range n) sorry @[simp] theorem fin_range_zero : fin_range 0 = [] := rfl @[simp] theorem mem_fin_range {n : ℕ} (a : fin n) : a ∈ fin_range n := sorry theorem nodup_fin_range (n : ℕ) : nodup (fin_range n) := nodup_pmap (fun (_x : ℕ) (_x_1 : _x < n) (_x_2 : ℕ) (_x_3 : _x_2 < n) => fin.veq_of_eq) (nodup_range n) @[simp] theorem length_fin_range (n : ℕ) : length (fin_range n) = n := eq.mpr (id (Eq._oldrec (Eq.refl (length (fin_range n) = n)) (fin_range.equations._eqn_1 n))) (eq.mpr (id (Eq._oldrec (Eq.refl (length (pmap fin.mk (range n) (fin_range._proof_1 n)) = n)) length_pmap)) (eq.mpr (id (Eq._oldrec (Eq.refl (length (range n) = n)) (length_range n))) (Eq.refl n))) @[simp] theorem fin_range_eq_nil {n : ℕ} : fin_range n = [] ↔ n = 0 := eq.mpr (id (Eq._oldrec (Eq.refl (fin_range n = [] ↔ n = 0)) (Eq.symm (propext length_eq_zero)))) (eq.mpr (id (Eq._oldrec (Eq.refl (length (fin_range n) = 0 ↔ n = 0)) (length_fin_range n))) (iff.refl (n = 0))) theorem prod_range_succ {α : Type u} [monoid α] (f : ℕ → α) (n : ℕ) : prod (map f (range (Nat.succ n))) = prod (map f (range n)) * f n := sorry /-- A variant of `prod_range_succ` which pulls off the first term in the product rather than the last.-/ theorem sum_range_succ' {α : Type u} [add_monoid α] (f : ℕ → α) (n : ℕ) : sum (map f (range (Nat.succ n))) = f 0 + sum (map (fun (i : ℕ) => f (Nat.succ i)) (range n)) := sorry @[simp] theorem enum_from_map_fst {α : Type u} (n : ℕ) (l : List α) : map prod.fst (enum_from n l) = range' n (length l) := sorry @[simp] theorem enum_map_fst {α : Type u} (l : List α) : map prod.fst (enum l) = range (length l) := sorry theorem enum_eq_zip_range {α : Type u} (l : List α) : enum l = zip (range (length l)) l := zip_of_prod (enum_map_fst l) (enum_map_snd l) @[simp] theorem unzip_enum_eq_prod {α : Type u} (l : List α) : unzip (enum l) = (range (length l), l) := sorry theorem enum_from_eq_zip_range' {α : Type u} (l : List α) {n : ℕ} : enum_from n l = zip (range' n (length l)) l := zip_of_prod (enum_from_map_fst n l) (enum_from_map_snd n l) @[simp] theorem unzip_enum_from_eq_prod {α : Type u} (l : List α) {n : ℕ} : unzip (enum_from n l) = (range' n (length l), l) := sorry @[simp] theorem nth_le_range {n : ℕ} (i : ℕ) (H : i < length (range n)) : nth_le (range n) i H = i := sorry @[simp] theorem nth_le_fin_range {n : ℕ} {i : ℕ} (h : i < length (fin_range n)) : nth_le (fin_range n) i h = { val := i, property := length_fin_range n ▸ h } := sorry theorem of_fn_eq_pmap {α : Type u_1} {n : ℕ} {f : fin n → α} : of_fn f = pmap (fun (i : ℕ) (hi : i < n) => f { val := i, property := hi }) (range n) fun (_x : ℕ) => iff.mp mem_range := sorry theorem of_fn_id (n : ℕ) : of_fn id = fin_range n := of_fn_eq_pmap theorem of_fn_eq_map {α : Type u_1} {n : ℕ} {f : fin n → α} : of_fn f = map f (fin_range n) := eq.mpr (id (Eq._oldrec (Eq.refl (of_fn f = map f (fin_range n))) (Eq.symm (of_fn_id n)))) (eq.mpr (id (Eq._oldrec (Eq.refl (of_fn f = map f (of_fn id))) (map_of_fn id f))) (eq.mpr (id (Eq._oldrec (Eq.refl (of_fn f = of_fn (f ∘ id))) (function.right_id f))) (Eq.refl (of_fn f)))) theorem nodup_of_fn {α : Type u_1} {n : ℕ} {f : fin n → α} (hf : function.injective f) : nodup (of_fn f) := sorry
9f47a7f85e4dd18782bac0daa1eeb1902734a3c5
10c7c971a1902d76057c52ce0529ebb491a69c44
/ProblemSheet1.lean
372fcae6b1dfc878c27991c9f806bb8a3bcadc6a
[]
no_license
SzymonKubica/Lean
5f6122e8dd9171239b36a9ce0515f6acbc49781a
627bff2f001ba3f009c112c9332093e8de84863c
refs/heads/main
1,675,563,490,768
1,608,538,609,000
1,608,538,609,000
307,184,347
0
0
null
null
null
null
UTF-8
Lean
false
false
2,568
lean
/- Math40001 : Introduction to university mathematics. Problem Sheet 1, October 2020. -/ /- Question 1. Let P and Q be Propositions (that is, true/false statements). Prove that P ∨ Q → Q ∨ P. -/ import tactic lemma question_one (P Q : Prop) : P ∨ Q → Q ∨ P := begin intro hPorQ, cases hPorQ with hP hQ, right, exact hP, left, exact hQ, end /- For question 2, comment out one option (or just delete it) and prove the other one. -/ -- Part (a): is → symmetric? lemma question_2a_false : ¬ (∀ P Q : Prop, (P → Q) → (Q → P)) := begin intro hPQ, end -- Part (b) : is ↔ symmetric? lemma question_2b_true (P Q : Prop) : (P ↔ Q) → (Q ↔ P) := begin sorry end lemma question_2b_false : ¬ (∀ P Q : Prop, (P ↔ Q) → (Q ↔ P)) := begin sorry end /- Question 3. Say P, Q and R are propositions, and we know: 1) if Q is true then P is true 2) If Q is false then R is false. Can we deduce that R implies P? Comment out one option and prove the other. Hint: if you're stuck, "apply classical.by_contradiction" sometimes helps. classical.by_contradiction is the theorem that ¬ ¬ P → P. -/ lemma question_3_true (P Q R : Prop) (h1 : Q → P) (h2 : ¬ Q → ¬ R) : R → P := begin sorry end lemma question_3_false : ¬ (∀ P Q R : Prop, (Q → P) → (¬ Q → ¬ R) → R → P) := begin sorry end /- Question 4. Is it possible to find three true-false statements P , Q and R, such that (P ∨ Q ∨ R) ∧ (¬P ∨ Q ∨ R) ∧ (¬P ∨ ¬Q ∨ R) ∧ (¬P ∨ ¬Q ∨ ¬R) is true? -/ lemma question_4_true : ∃ (P Q R : Prop), (P ∨ Q ∨ R) ∧ (¬P ∨ Q ∨ R) ∧ (¬P ∨ ¬Q ∨ R) ∧ (¬P ∨ ¬Q ∨ ¬R) := begin sorry end lemma question_4_false : ∀ (P Q R : Prop), ¬ ((P ∨ Q ∨ R) ∧ (¬P ∨ Q ∨ R) ∧ (¬P ∨ ¬Q ∨ R) ∧ (¬P ∨ ¬Q ∨ ¬R)) := begin sorry end /- Question 5. Say that for every integer n we have a proposition P n. Say we know P n → P (n + 8) for all n, and P n → P (n -3) for all n. Prove that the P n are either all true, or all false. This question is harder than the others. -/ lemma question_5 (P : ℤ → Prop) (h8 : ∀ n, P n → P (n + 8)) (h3 : ∀ n, P n → P (n - 3)) : (∀ n, P n) ∨ (∀ n, ¬ (P n)) := begin sorry end /- The first four of these questions can be solved using only the following tactics: intro apply (or, better, refine) left, right, cases, split assumption (or, better, exact) have, simp, use, contradiction (or, better, false.elim) The fifth question is harder. -/
d25af14ddfa76816192da4b6e418da13368b5eff
bb31430994044506fa42fd667e2d556327e18dfe
/src/geometry/euclidean/angle/oriented/right_angle.lean
52462e99f46d811a833e749686e4d0fc04d48d30
[ "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
45,544
lean
/- Copyright (c) 2022 Joseph Myers. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Joseph Myers -/ import geometry.euclidean.angle.oriented.affine import geometry.euclidean.angle.unoriented.right_angle /-! # Oriented angles in right-angled triangles. This file proves basic geometrical results about distances and oriented angles in (possibly degenerate) right-angled triangles in real inner product spaces and Euclidean affine spaces. -/ noncomputable theory open_locale euclidean_geometry open_locale real open_locale real_inner_product_space namespace orientation open finite_dimensional variables {V : Type*} [inner_product_space ℝ V] variables [hd2 : fact (finrank ℝ V = 2)] (o : orientation ℝ V (fin 2)) include hd2 o /-- An angle in a right-angled triangle expressed using `arccos`. -/ lemma oangle_add_right_eq_arccos_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = ↑(π / 2)) : o.oangle x (x + y) = real.arccos (‖x‖ / ‖x + y‖) := begin have hs : (o.oangle x (x + y)).sign = 1, { rw [oangle_sign_add_right, h, real.angle.sign_coe_pi_div_two] }, rw [o.oangle_eq_angle_of_sign_eq_one hs, inner_product_geometry.angle_add_eq_arccos_of_inner_eq_zero (o.inner_eq_zero_of_oangle_eq_pi_div_two h)] end /-- An angle in a right-angled triangle expressed using `arccos`. -/ lemma oangle_add_left_eq_arccos_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = ↑(π / 2)) : o.oangle (x + y) y = real.arccos (‖y‖ / ‖x + y‖) := begin rw [←neg_inj, oangle_rev, ←oangle_neg_orientation_eq_neg, neg_inj] at h ⊢, rw add_comm, exact (-o).oangle_add_right_eq_arccos_of_oangle_eq_pi_div_two h end /-- An angle in a right-angled triangle expressed using `arcsin`. -/ lemma oangle_add_right_eq_arcsin_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = ↑(π / 2)) : o.oangle x (x + y) = real.arcsin (‖y‖ / ‖x + y‖) := begin have hs : (o.oangle x (x + y)).sign = 1, { rw [oangle_sign_add_right, h, real.angle.sign_coe_pi_div_two] }, rw [o.oangle_eq_angle_of_sign_eq_one hs, inner_product_geometry.angle_add_eq_arcsin_of_inner_eq_zero (o.inner_eq_zero_of_oangle_eq_pi_div_two h) (or.inl (o.left_ne_zero_of_oangle_eq_pi_div_two h))] end /-- An angle in a right-angled triangle expressed using `arcsin`. -/ lemma oangle_add_left_eq_arcsin_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = ↑(π / 2)) : o.oangle (x + y) y = real.arcsin (‖x‖ / ‖x + y‖) := begin rw [←neg_inj, oangle_rev, ←oangle_neg_orientation_eq_neg, neg_inj] at h ⊢, rw add_comm, exact (-o).oangle_add_right_eq_arcsin_of_oangle_eq_pi_div_two h end /-- An angle in a right-angled triangle expressed using `arctan`. -/ lemma oangle_add_right_eq_arctan_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = ↑(π / 2)) : o.oangle x (x + y) = real.arctan (‖y‖ / ‖x‖) := begin have hs : (o.oangle x (x + y)).sign = 1, { rw [oangle_sign_add_right, h, real.angle.sign_coe_pi_div_two] }, rw [o.oangle_eq_angle_of_sign_eq_one hs, inner_product_geometry.angle_add_eq_arctan_of_inner_eq_zero (o.inner_eq_zero_of_oangle_eq_pi_div_two h) (o.left_ne_zero_of_oangle_eq_pi_div_two h)] end /-- An angle in a right-angled triangle expressed using `arctan`. -/ lemma oangle_add_left_eq_arctan_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = ↑(π / 2)) : o.oangle (x + y) y = real.arctan (‖x‖ / ‖y‖) := begin rw [←neg_inj, oangle_rev, ←oangle_neg_orientation_eq_neg, neg_inj] at h ⊢, rw add_comm, exact (-o).oangle_add_right_eq_arctan_of_oangle_eq_pi_div_two h end /-- The cosine of an angle in a right-angled triangle as a ratio of sides. -/ lemma cos_oangle_add_right_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = ↑(π / 2)) : real.angle.cos (o.oangle x (x + y)) = ‖x‖ / ‖x + y‖ := begin have hs : (o.oangle x (x + y)).sign = 1, { rw [oangle_sign_add_right, h, real.angle.sign_coe_pi_div_two] }, rw [o.oangle_eq_angle_of_sign_eq_one hs, real.angle.cos_coe, inner_product_geometry.cos_angle_add_of_inner_eq_zero (o.inner_eq_zero_of_oangle_eq_pi_div_two h)] end /-- The cosine of an angle in a right-angled triangle as a ratio of sides. -/ lemma cos_oangle_add_left_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = ↑(π / 2)) : real.angle.cos (o.oangle (x + y) y) = ‖y‖ / ‖x + y‖ := begin rw [←neg_inj, oangle_rev, ←oangle_neg_orientation_eq_neg, neg_inj] at h ⊢, rw add_comm, exact (-o).cos_oangle_add_right_of_oangle_eq_pi_div_two h end /-- The sine of an angle in a right-angled triangle as a ratio of sides. -/ lemma sin_oangle_add_right_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = ↑(π / 2)) : real.angle.sin (o.oangle x (x + y)) = ‖y‖ / ‖x + y‖ := begin have hs : (o.oangle x (x + y)).sign = 1, { rw [oangle_sign_add_right, h, real.angle.sign_coe_pi_div_two] }, rw [o.oangle_eq_angle_of_sign_eq_one hs, real.angle.sin_coe, inner_product_geometry.sin_angle_add_of_inner_eq_zero (o.inner_eq_zero_of_oangle_eq_pi_div_two h) (or.inl (o.left_ne_zero_of_oangle_eq_pi_div_two h))] end /-- The sine of an angle in a right-angled triangle as a ratio of sides. -/ lemma sin_oangle_add_left_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = ↑(π / 2)) : real.angle.sin (o.oangle (x + y) y) = ‖x‖ / ‖x + y‖ := begin rw [←neg_inj, oangle_rev, ←oangle_neg_orientation_eq_neg, neg_inj] at h ⊢, rw add_comm, exact (-o).sin_oangle_add_right_of_oangle_eq_pi_div_two h end /-- The tangent of an angle in a right-angled triangle as a ratio of sides. -/ lemma tan_oangle_add_right_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = ↑(π / 2)) : real.angle.tan (o.oangle x (x + y)) = ‖y‖ / ‖x‖ := begin have hs : (o.oangle x (x + y)).sign = 1, { rw [oangle_sign_add_right, h, real.angle.sign_coe_pi_div_two] }, rw [o.oangle_eq_angle_of_sign_eq_one hs, real.angle.tan_coe, inner_product_geometry.tan_angle_add_of_inner_eq_zero (o.inner_eq_zero_of_oangle_eq_pi_div_two h)] end /-- The tangent of an angle in a right-angled triangle as a ratio of sides. -/ lemma tan_oangle_add_left_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = ↑(π / 2)) : real.angle.tan (o.oangle (x + y) y) = ‖x‖ / ‖y‖ := begin rw [←neg_inj, oangle_rev, ←oangle_neg_orientation_eq_neg, neg_inj] at h ⊢, rw add_comm, exact (-o).tan_oangle_add_right_of_oangle_eq_pi_div_two h end /-- The cosine of an angle in a right-angled triangle multiplied by the hypotenuse equals the adjacent side. -/ lemma cos_oangle_add_right_mul_norm_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = ↑(π / 2)) : real.angle.cos (o.oangle x (x + y)) * ‖x + y‖ = ‖x‖ := begin have hs : (o.oangle x (x + y)).sign = 1, { rw [oangle_sign_add_right, h, real.angle.sign_coe_pi_div_two] }, rw [o.oangle_eq_angle_of_sign_eq_one hs, real.angle.cos_coe, inner_product_geometry.cos_angle_add_mul_norm_of_inner_eq_zero (o.inner_eq_zero_of_oangle_eq_pi_div_two h)] end /-- The cosine of an angle in a right-angled triangle multiplied by the hypotenuse equals the adjacent side. -/ lemma cos_oangle_add_left_mul_norm_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = ↑(π / 2)) : real.angle.cos (o.oangle (x + y) y) * ‖x + y‖ = ‖y‖ := begin rw [←neg_inj, oangle_rev, ←oangle_neg_orientation_eq_neg, neg_inj] at h ⊢, rw add_comm, exact (-o).cos_oangle_add_right_mul_norm_of_oangle_eq_pi_div_two h end /-- The sine of an angle in a right-angled triangle multiplied by the hypotenuse equals the opposite side. -/ lemma sin_oangle_add_right_mul_norm_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = ↑(π / 2)) : real.angle.sin (o.oangle x (x + y)) * ‖x + y‖ = ‖y‖ := begin have hs : (o.oangle x (x + y)).sign = 1, { rw [oangle_sign_add_right, h, real.angle.sign_coe_pi_div_two] }, rw [o.oangle_eq_angle_of_sign_eq_one hs, real.angle.sin_coe, inner_product_geometry.sin_angle_add_mul_norm_of_inner_eq_zero (o.inner_eq_zero_of_oangle_eq_pi_div_two h)] end /-- The sine of an angle in a right-angled triangle multiplied by the hypotenuse equals the opposite side. -/ lemma sin_oangle_add_left_mul_norm_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = ↑(π / 2)) : real.angle.sin (o.oangle (x + y) y) * ‖x + y‖ = ‖x‖ := begin rw [←neg_inj, oangle_rev, ←oangle_neg_orientation_eq_neg, neg_inj] at h ⊢, rw add_comm, exact (-o).sin_oangle_add_right_mul_norm_of_oangle_eq_pi_div_two h end /-- The tangent of an angle in a right-angled triangle multiplied by the adjacent side equals the opposite side. -/ lemma tan_oangle_add_right_mul_norm_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = ↑(π / 2)) : real.angle.tan (o.oangle x (x + y)) * ‖x‖ = ‖y‖ := begin have hs : (o.oangle x (x + y)).sign = 1, { rw [oangle_sign_add_right, h, real.angle.sign_coe_pi_div_two] }, rw [o.oangle_eq_angle_of_sign_eq_one hs, real.angle.tan_coe, inner_product_geometry.tan_angle_add_mul_norm_of_inner_eq_zero (o.inner_eq_zero_of_oangle_eq_pi_div_two h) (or.inl (o.left_ne_zero_of_oangle_eq_pi_div_two h))] end /-- The tangent of an angle in a right-angled triangle multiplied by the adjacent side equals the opposite side. -/ lemma tan_oangle_add_left_mul_norm_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = ↑(π / 2)) : real.angle.tan (o.oangle (x + y) y) * ‖y‖ = ‖x‖ := begin rw [←neg_inj, oangle_rev, ←oangle_neg_orientation_eq_neg, neg_inj] at h ⊢, rw add_comm, exact (-o).tan_oangle_add_right_mul_norm_of_oangle_eq_pi_div_two h end /-- A side of a right-angled triangle divided by the cosine of the adjacent angle equals the hypotenuse. -/ lemma norm_div_cos_oangle_add_right_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = ↑(π / 2)) : ‖x‖ / real.angle.cos (o.oangle x (x + y)) = ‖x + y‖ := begin have hs : (o.oangle x (x + y)).sign = 1, { rw [oangle_sign_add_right, h, real.angle.sign_coe_pi_div_two] }, rw [o.oangle_eq_angle_of_sign_eq_one hs, real.angle.cos_coe, inner_product_geometry.norm_div_cos_angle_add_of_inner_eq_zero (o.inner_eq_zero_of_oangle_eq_pi_div_two h) (or.inl (o.left_ne_zero_of_oangle_eq_pi_div_two h))] end /-- A side of a right-angled triangle divided by the cosine of the adjacent angle equals the hypotenuse. -/ lemma norm_div_cos_oangle_add_left_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = ↑(π / 2)) : ‖y‖ / real.angle.cos (o.oangle (x + y) y) = ‖x + y‖ := begin rw [←neg_inj, oangle_rev, ←oangle_neg_orientation_eq_neg, neg_inj] at h ⊢, rw add_comm, exact (-o).norm_div_cos_oangle_add_right_of_oangle_eq_pi_div_two h end /-- A side of a right-angled triangle divided by the sine of the opposite angle equals the hypotenuse. -/ lemma norm_div_sin_oangle_add_right_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = ↑(π / 2)) : ‖y‖ / real.angle.sin (o.oangle x (x + y)) = ‖x + y‖ := begin have hs : (o.oangle x (x + y)).sign = 1, { rw [oangle_sign_add_right, h, real.angle.sign_coe_pi_div_two] }, rw [o.oangle_eq_angle_of_sign_eq_one hs, real.angle.sin_coe, inner_product_geometry.norm_div_sin_angle_add_of_inner_eq_zero (o.inner_eq_zero_of_oangle_eq_pi_div_two h) (or.inr (o.right_ne_zero_of_oangle_eq_pi_div_two h))] end /-- A side of a right-angled triangle divided by the sine of the opposite angle equals the hypotenuse. -/ lemma norm_div_sin_oangle_add_left_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = ↑(π / 2)) : ‖x‖ / real.angle.sin (o.oangle (x + y) y) = ‖x + y‖ := begin rw [←neg_inj, oangle_rev, ←oangle_neg_orientation_eq_neg, neg_inj] at h ⊢, rw add_comm, exact (-o).norm_div_sin_oangle_add_right_of_oangle_eq_pi_div_two h end /-- A side of a right-angled triangle divided by the tangent of the opposite angle equals the adjacent side. -/ lemma norm_div_tan_oangle_add_right_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = ↑(π / 2)) : ‖y‖ / real.angle.tan (o.oangle x (x + y)) = ‖x‖ := begin have hs : (o.oangle x (x + y)).sign = 1, { rw [oangle_sign_add_right, h, real.angle.sign_coe_pi_div_two] }, rw [o.oangle_eq_angle_of_sign_eq_one hs, real.angle.tan_coe, inner_product_geometry.norm_div_tan_angle_add_of_inner_eq_zero (o.inner_eq_zero_of_oangle_eq_pi_div_two h) (or.inr (o.right_ne_zero_of_oangle_eq_pi_div_two h))] end /-- A side of a right-angled triangle divided by the tangent of the opposite angle equals the adjacent side. -/ lemma norm_div_tan_oangle_add_left_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = ↑(π / 2)) : ‖x‖ / real.angle.tan (o.oangle (x + y) y) = ‖y‖ := begin rw [←neg_inj, oangle_rev, ←oangle_neg_orientation_eq_neg, neg_inj] at h ⊢, rw add_comm, exact (-o).norm_div_tan_oangle_add_right_of_oangle_eq_pi_div_two h end /-- An angle in a right-angled triangle expressed using `arccos`, version subtracting vectors. -/ lemma oangle_sub_right_eq_arccos_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = ↑(π / 2)) : o.oangle y (y - x) = real.arccos (‖y‖ / ‖y - x‖) := begin have hs : (o.oangle y (y - x)).sign = 1, { rw [oangle_sign_sub_right_swap, h, real.angle.sign_coe_pi_div_two] }, rw [o.oangle_eq_angle_of_sign_eq_one hs, inner_product_geometry.angle_sub_eq_arccos_of_inner_eq_zero (o.inner_rev_eq_zero_of_oangle_eq_pi_div_two h)] end /-- An angle in a right-angled triangle expressed using `arccos`, version subtracting vectors. -/ lemma oangle_sub_left_eq_arccos_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = ↑(π / 2)) : o.oangle (x - y) x = real.arccos (‖x‖ / ‖x - y‖) := begin rw [←neg_inj, oangle_rev, ←oangle_neg_orientation_eq_neg, neg_inj] at h ⊢, exact (-o).oangle_sub_right_eq_arccos_of_oangle_eq_pi_div_two h end /-- An angle in a right-angled triangle expressed using `arcsin`, version subtracting vectors. -/ lemma oangle_sub_right_eq_arcsin_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = ↑(π / 2)) : o.oangle y (y - x) = real.arcsin (‖x‖ / ‖y - x‖) := begin have hs : (o.oangle y (y - x)).sign = 1, { rw [oangle_sign_sub_right_swap, h, real.angle.sign_coe_pi_div_two] }, rw [o.oangle_eq_angle_of_sign_eq_one hs, inner_product_geometry.angle_sub_eq_arcsin_of_inner_eq_zero (o.inner_rev_eq_zero_of_oangle_eq_pi_div_two h) (or.inl (o.right_ne_zero_of_oangle_eq_pi_div_two h))] end /-- An angle in a right-angled triangle expressed using `arcsin`, version subtracting vectors. -/ lemma oangle_sub_left_eq_arcsin_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = ↑(π / 2)) : o.oangle (x - y) x = real.arcsin (‖y‖ / ‖x - y‖) := begin rw [←neg_inj, oangle_rev, ←oangle_neg_orientation_eq_neg, neg_inj] at h ⊢, exact (-o).oangle_sub_right_eq_arcsin_of_oangle_eq_pi_div_two h end /-- An angle in a right-angled triangle expressed using `arctan`, version subtracting vectors. -/ lemma oangle_sub_right_eq_arctan_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = ↑(π / 2)) : o.oangle y (y - x) = real.arctan (‖x‖ / ‖y‖) := begin have hs : (o.oangle y (y - x)).sign = 1, { rw [oangle_sign_sub_right_swap, h, real.angle.sign_coe_pi_div_two] }, rw [o.oangle_eq_angle_of_sign_eq_one hs, inner_product_geometry.angle_sub_eq_arctan_of_inner_eq_zero (o.inner_rev_eq_zero_of_oangle_eq_pi_div_two h) (o.right_ne_zero_of_oangle_eq_pi_div_two h)] end /-- An angle in a right-angled triangle expressed using `arctan`, version subtracting vectors. -/ lemma oangle_sub_left_eq_arctan_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = ↑(π / 2)) : o.oangle (x - y) x = real.arctan (‖y‖ / ‖x‖) := begin rw [←neg_inj, oangle_rev, ←oangle_neg_orientation_eq_neg, neg_inj] at h ⊢, exact (-o).oangle_sub_right_eq_arctan_of_oangle_eq_pi_div_two h end /-- The cosine of an angle in a right-angled triangle as a ratio of sides, version subtracting vectors. -/ lemma cos_oangle_sub_right_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = ↑(π / 2)) : real.angle.cos (o.oangle y (y - x)) = ‖y‖ / ‖y - x‖ := begin have hs : (o.oangle y (y - x)).sign = 1, { rw [oangle_sign_sub_right_swap, h, real.angle.sign_coe_pi_div_two] }, rw [o.oangle_eq_angle_of_sign_eq_one hs, real.angle.cos_coe, inner_product_geometry.cos_angle_sub_of_inner_eq_zero (o.inner_rev_eq_zero_of_oangle_eq_pi_div_two h)] end /-- The cosine of an angle in a right-angled triangle as a ratio of sides, version subtracting vectors. -/ lemma cos_oangle_sub_left_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = ↑(π / 2)) : real.angle.cos (o.oangle (x - y) x) = ‖x‖ / ‖x - y‖ := begin rw [←neg_inj, oangle_rev, ←oangle_neg_orientation_eq_neg, neg_inj] at h ⊢, exact (-o).cos_oangle_sub_right_of_oangle_eq_pi_div_two h end /-- The sine of an angle in a right-angled triangle as a ratio of sides, version subtracting vectors. -/ lemma sin_oangle_sub_right_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = ↑(π / 2)) : real.angle.sin (o.oangle y (y - x)) = ‖x‖ / ‖y - x‖ := begin have hs : (o.oangle y (y - x)).sign = 1, { rw [oangle_sign_sub_right_swap, h, real.angle.sign_coe_pi_div_two] }, rw [o.oangle_eq_angle_of_sign_eq_one hs, real.angle.sin_coe, inner_product_geometry.sin_angle_sub_of_inner_eq_zero (o.inner_rev_eq_zero_of_oangle_eq_pi_div_two h) (or.inl (o.right_ne_zero_of_oangle_eq_pi_div_two h))] end /-- The sine of an angle in a right-angled triangle as a ratio of sides, version subtracting vectors. -/ lemma sin_oangle_sub_left_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = ↑(π / 2)) : real.angle.sin (o.oangle (x - y) x) = ‖y‖ / ‖x - y‖ := begin rw [←neg_inj, oangle_rev, ←oangle_neg_orientation_eq_neg, neg_inj] at h ⊢, exact (-o).sin_oangle_sub_right_of_oangle_eq_pi_div_two h end /-- The tangent of an angle in a right-angled triangle as a ratio of sides, version subtracting vectors. -/ lemma tan_oangle_sub_right_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = ↑(π / 2)) : real.angle.tan (o.oangle y (y - x)) = ‖x‖ / ‖y‖ := begin have hs : (o.oangle y (y - x)).sign = 1, { rw [oangle_sign_sub_right_swap, h, real.angle.sign_coe_pi_div_two] }, rw [o.oangle_eq_angle_of_sign_eq_one hs, real.angle.tan_coe, inner_product_geometry.tan_angle_sub_of_inner_eq_zero (o.inner_rev_eq_zero_of_oangle_eq_pi_div_two h)] end /-- The tangent of an angle in a right-angled triangle as a ratio of sides, version subtracting vectors. -/ lemma tan_oangle_sub_left_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = ↑(π / 2)) : real.angle.tan (o.oangle (x - y) x) = ‖y‖ / ‖x‖ := begin rw [←neg_inj, oangle_rev, ←oangle_neg_orientation_eq_neg, neg_inj] at h ⊢, exact (-o).tan_oangle_sub_right_of_oangle_eq_pi_div_two h end /-- The cosine of an angle in a right-angled triangle multiplied by the hypotenuse equals the adjacent side, version subtracting vectors. -/ lemma cos_oangle_sub_right_mul_norm_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = ↑(π / 2)) : real.angle.cos (o.oangle y (y - x)) * ‖y - x‖ = ‖y‖ := begin have hs : (o.oangle y (y - x)).sign = 1, { rw [oangle_sign_sub_right_swap, h, real.angle.sign_coe_pi_div_two] }, rw [o.oangle_eq_angle_of_sign_eq_one hs, real.angle.cos_coe, inner_product_geometry.cos_angle_sub_mul_norm_of_inner_eq_zero (o.inner_rev_eq_zero_of_oangle_eq_pi_div_two h)] end /-- The cosine of an angle in a right-angled triangle multiplied by the hypotenuse equals the adjacent side, version subtracting vectors. -/ lemma cos_oangle_sub_left_mul_norm_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = ↑(π / 2)) : real.angle.cos (o.oangle (x - y) x) * ‖x - y‖ = ‖x‖ := begin rw [←neg_inj, oangle_rev, ←oangle_neg_orientation_eq_neg, neg_inj] at h ⊢, exact (-o).cos_oangle_sub_right_mul_norm_of_oangle_eq_pi_div_two h end /-- The sine of an angle in a right-angled triangle multiplied by the hypotenuse equals the opposite side, version subtracting vectors. -/ lemma sin_oangle_sub_right_mul_norm_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = ↑(π / 2)) : real.angle.sin (o.oangle y (y - x)) * ‖y - x‖ = ‖x‖ := begin have hs : (o.oangle y (y - x)).sign = 1, { rw [oangle_sign_sub_right_swap, h, real.angle.sign_coe_pi_div_two] }, rw [o.oangle_eq_angle_of_sign_eq_one hs, real.angle.sin_coe, inner_product_geometry.sin_angle_sub_mul_norm_of_inner_eq_zero (o.inner_rev_eq_zero_of_oangle_eq_pi_div_two h)] end /-- The sine of an angle in a right-angled triangle multiplied by the hypotenuse equals the opposite side, version subtracting vectors. -/ lemma sin_oangle_sub_left_mul_norm_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = ↑(π / 2)) : real.angle.sin (o.oangle (x - y) x) * ‖x - y‖ = ‖y‖ := begin rw [←neg_inj, oangle_rev, ←oangle_neg_orientation_eq_neg, neg_inj] at h ⊢, exact (-o).sin_oangle_sub_right_mul_norm_of_oangle_eq_pi_div_two h end /-- The tangent of an angle in a right-angled triangle multiplied by the adjacent side equals the opposite side, version subtracting vectors. -/ lemma tan_oangle_sub_right_mul_norm_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = ↑(π / 2)) : real.angle.tan (o.oangle y (y - x)) * ‖y‖ = ‖x‖ := begin have hs : (o.oangle y (y - x)).sign = 1, { rw [oangle_sign_sub_right_swap, h, real.angle.sign_coe_pi_div_two] }, rw [o.oangle_eq_angle_of_sign_eq_one hs, real.angle.tan_coe, inner_product_geometry.tan_angle_sub_mul_norm_of_inner_eq_zero (o.inner_rev_eq_zero_of_oangle_eq_pi_div_two h) (or.inl (o.right_ne_zero_of_oangle_eq_pi_div_two h))] end /-- The tangent of an angle in a right-angled triangle multiplied by the adjacent side equals the opposite side, version subtracting vectors. -/ lemma tan_oangle_sub_left_mul_norm_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = ↑(π / 2)) : real.angle.tan (o.oangle (x - y) x) * ‖x‖ = ‖y‖ := begin rw [←neg_inj, oangle_rev, ←oangle_neg_orientation_eq_neg, neg_inj] at h ⊢, exact (-o).tan_oangle_sub_right_mul_norm_of_oangle_eq_pi_div_two h end /-- A side of a right-angled triangle divided by the cosine of the adjacent angle equals the hypotenuse, version subtracting vectors. -/ lemma norm_div_cos_oangle_sub_right_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = ↑(π / 2)) : ‖y‖ / real.angle.cos (o.oangle y (y - x)) = ‖y - x‖ := begin have hs : (o.oangle y (y - x)).sign = 1, { rw [oangle_sign_sub_right_swap, h, real.angle.sign_coe_pi_div_two] }, rw [o.oangle_eq_angle_of_sign_eq_one hs, real.angle.cos_coe, inner_product_geometry.norm_div_cos_angle_sub_of_inner_eq_zero (o.inner_rev_eq_zero_of_oangle_eq_pi_div_two h) (or.inl (o.right_ne_zero_of_oangle_eq_pi_div_two h))] end /-- A side of a right-angled triangle divided by the cosine of the adjacent angle equals the hypotenuse, version subtracting vectors. -/ lemma norm_div_cos_oangle_sub_left_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = ↑(π / 2)) : ‖x‖ / real.angle.cos (o.oangle (x - y) x) = ‖x - y‖ := begin rw [←neg_inj, oangle_rev, ←oangle_neg_orientation_eq_neg, neg_inj] at h ⊢, exact (-o).norm_div_cos_oangle_sub_right_of_oangle_eq_pi_div_two h end /-- A side of a right-angled triangle divided by the sine of the opposite angle equals the hypotenuse, version subtracting vectors. -/ lemma norm_div_sin_oangle_sub_right_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = ↑(π / 2)) : ‖x‖ / real.angle.sin (o.oangle y (y - x)) = ‖y - x‖ := begin have hs : (o.oangle y (y - x)).sign = 1, { rw [oangle_sign_sub_right_swap, h, real.angle.sign_coe_pi_div_two] }, rw [o.oangle_eq_angle_of_sign_eq_one hs, real.angle.sin_coe, inner_product_geometry.norm_div_sin_angle_sub_of_inner_eq_zero (o.inner_rev_eq_zero_of_oangle_eq_pi_div_two h) (or.inr (o.left_ne_zero_of_oangle_eq_pi_div_two h))] end /-- A side of a right-angled triangle divided by the sine of the opposite angle equals the hypotenuse, version subtracting vectors. -/ lemma norm_div_sin_oangle_sub_left_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = ↑(π / 2)) : ‖y‖ / real.angle.sin (o.oangle (x - y) x) = ‖x - y‖ := begin rw [←neg_inj, oangle_rev, ←oangle_neg_orientation_eq_neg, neg_inj] at h ⊢, exact (-o).norm_div_sin_oangle_sub_right_of_oangle_eq_pi_div_two h end /-- A side of a right-angled triangle divided by the tangent of the opposite angle equals the adjacent side, version subtracting vectors. -/ lemma norm_div_tan_oangle_sub_right_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = ↑(π / 2)) : ‖x‖ / real.angle.tan (o.oangle y (y - x)) = ‖y‖ := begin have hs : (o.oangle y (y - x)).sign = 1, { rw [oangle_sign_sub_right_swap, h, real.angle.sign_coe_pi_div_two] }, rw [o.oangle_eq_angle_of_sign_eq_one hs, real.angle.tan_coe, inner_product_geometry.norm_div_tan_angle_sub_of_inner_eq_zero (o.inner_rev_eq_zero_of_oangle_eq_pi_div_two h) (or.inr (o.left_ne_zero_of_oangle_eq_pi_div_two h))] end /-- A side of a right-angled triangle divided by the tangent of the opposite angle equals the adjacent side, version subtracting vectors. -/ lemma norm_div_tan_oangle_sub_left_of_oangle_eq_pi_div_two {x y : V} (h : o.oangle x y = ↑(π / 2)) : ‖y‖ / real.angle.tan (o.oangle (x - y) x) = ‖x‖ := begin rw [←neg_inj, oangle_rev, ←oangle_neg_orientation_eq_neg, neg_inj] at h ⊢, exact (-o).norm_div_tan_oangle_sub_right_of_oangle_eq_pi_div_two h end /-- An angle in a right-angled triangle expressed using `arctan`, where one side is a multiple of a rotation of another by `π / 2`. -/ lemma oangle_add_right_smul_rotation_pi_div_two {x : V} (h : x ≠ 0) (r : ℝ) : o.oangle x (x + r • o.rotation (π / 2 : ℝ) x) = real.arctan r := begin rcases lt_trichotomy r 0 with hr | rfl | hr, { have ha : o.oangle x (r • o.rotation (π / 2 : ℝ) x) = -(π / 2 : ℝ), { rw [o.oangle_smul_right_of_neg _ _ hr, o.oangle_neg_right h, o.oangle_rotation_self_right h, ←sub_eq_zero, add_comm, sub_neg_eq_add, ←real.angle.coe_add, ←real.angle.coe_add, add_assoc, add_halves, ←two_mul, real.angle.coe_two_pi], simpa using h }, rw [←neg_inj, ←oangle_neg_orientation_eq_neg, neg_neg] at ha, rw [←neg_inj, oangle_rev, ←oangle_neg_orientation_eq_neg, neg_inj, oangle_rev, (-o).oangle_add_right_eq_arctan_of_oangle_eq_pi_div_two ha, norm_smul, linear_isometry_equiv.norm_map, mul_div_assoc, div_self (norm_ne_zero_iff.2 h), mul_one, real.norm_eq_abs, abs_of_neg hr, real.arctan_neg, real.angle.coe_neg, neg_neg] }, { rw [zero_smul, add_zero, oangle_self, real.arctan_zero, real.angle.coe_zero] }, { have ha : o.oangle x (r • o.rotation (π / 2 : ℝ) x) = (π / 2 : ℝ), { rw [o.oangle_smul_right_of_pos _ _ hr, o.oangle_rotation_self_right h] }, rw [o.oangle_add_right_eq_arctan_of_oangle_eq_pi_div_two ha, norm_smul, linear_isometry_equiv.norm_map, mul_div_assoc, div_self (norm_ne_zero_iff.2 h), mul_one, real.norm_eq_abs, abs_of_pos hr] } end /-- An angle in a right-angled triangle expressed using `arctan`, where one side is a multiple of a rotation of another by `π / 2`. -/ lemma oangle_add_left_smul_rotation_pi_div_two {x : V} (h : x ≠ 0) (r : ℝ) : o.oangle (x + r • o.rotation (π / 2 : ℝ) x) (r • o.rotation (π / 2 : ℝ) x) = real.arctan r⁻¹ := begin by_cases hr : r = 0, { simp [hr] }, rw [←neg_inj, oangle_rev, ←oangle_neg_orientation_eq_neg, neg_inj, ←neg_neg ((π / 2 : ℝ) : real.angle), ←rotation_neg_orientation_eq_neg, add_comm], have hx : x = r⁻¹ • ((-o).rotation (π / 2 : ℝ) (r • ((-o).rotation (-(π / 2 : ℝ)) x))), { simp [hr] }, nth_rewrite 2 hx, refine (-o).oangle_add_right_smul_rotation_pi_div_two _ _, simp [hr, h] end /-- The tangent of an angle in a right-angled triangle, where one side is a multiple of a rotation of another by `π / 2`. -/ lemma tan_oangle_add_right_smul_rotation_pi_div_two {x : V} (h : x ≠ 0) (r : ℝ) : real.angle.tan (o.oangle x (x + r • o.rotation (π / 2 : ℝ) x)) = r := by rw [o.oangle_add_right_smul_rotation_pi_div_two h, real.angle.tan_coe, real.tan_arctan] /-- The tangent of an angle in a right-angled triangle, where one side is a multiple of a rotation of another by `π / 2`. -/ lemma tan_oangle_add_left_smul_rotation_pi_div_two {x : V} (h : x ≠ 0) (r : ℝ) : real.angle.tan (o.oangle (x + r • o.rotation (π / 2 : ℝ) x) (r • o.rotation (π / 2 : ℝ) x)) = r⁻¹ := by rw [o.oangle_add_left_smul_rotation_pi_div_two h, real.angle.tan_coe, real.tan_arctan] /-- An angle in a right-angled triangle expressed using `arctan`, where one side is a multiple of a rotation of another by `π / 2`, version subtracting vectors. -/ lemma oangle_sub_right_smul_rotation_pi_div_two {x : V} (h : x ≠ 0) (r : ℝ) : o.oangle (r • o.rotation (π / 2 : ℝ) x) (r • o.rotation (π / 2 : ℝ) x - x) = real.arctan r⁻¹ := begin by_cases hr : r = 0, { simp [hr] }, have hx : -x = r⁻¹ • (o.rotation (π / 2 : ℝ) (r • (o.rotation (π / 2 : ℝ) x))), { simp [hr, ←real.angle.coe_add] }, rw [sub_eq_add_neg, hx, o.oangle_add_right_smul_rotation_pi_div_two], simpa [hr] using h end /-- An angle in a right-angled triangle expressed using `arctan`, where one side is a multiple of a rotation of another by `π / 2`, version subtracting vectors. -/ lemma oangle_sub_left_smul_rotation_pi_div_two {x : V} (h : x ≠ 0) (r : ℝ) : o.oangle (x - r • o.rotation (π / 2 : ℝ) x) x = real.arctan r := begin by_cases hr : r = 0, { simp [hr] }, have hx : x = r⁻¹ • (o.rotation (π / 2 : ℝ) (-(r • (o.rotation (π / 2 : ℝ) x)))), { simp [hr, ←real.angle.coe_add] }, rw [sub_eq_add_neg, add_comm], nth_rewrite 2 hx, nth_rewrite 1 hx, rw [o.oangle_add_left_smul_rotation_pi_div_two, inv_inv], simpa [hr] using h end end orientation namespace euclidean_geometry open finite_dimensional variables {V : Type*} {P : Type*} [inner_product_space ℝ V] [metric_space P] variables [normed_add_torsor V P] [hd2 : fact (finrank ℝ V = 2)] [module.oriented ℝ V (fin 2)] include hd2 /-- An angle in a right-angled triangle expressed using `arccos`. -/ lemma oangle_right_eq_arccos_of_oangle_eq_pi_div_two {p₁ p₂ p₃ : P} (h : ∡ p₁ p₂ p₃ = ↑(π / 2)) : ∡ p₂ p₃ p₁ = real.arccos (dist p₃ p₂ / dist p₁ p₃) := begin have hs : (∡ p₂ p₃ p₁).sign = 1, { rw [oangle_rotate_sign, h, real.angle.sign_coe_pi_div_two] }, rw [oangle_eq_angle_of_sign_eq_one hs, angle_eq_arccos_of_angle_eq_pi_div_two (angle_eq_pi_div_two_of_oangle_eq_pi_div_two h)] end /-- An angle in a right-angled triangle expressed using `arccos`. -/ lemma oangle_left_eq_arccos_of_oangle_eq_pi_div_two {p₁ p₂ p₃ : P} (h : ∡ p₁ p₂ p₃ = ↑(π / 2)) : ∡ p₃ p₁ p₂ = real.arccos (dist p₁ p₂ / dist p₁ p₃) := begin have hs : (∡ p₃ p₁ p₂).sign = 1, { rw [←oangle_rotate_sign, h, real.angle.sign_coe_pi_div_two] }, rw [oangle_eq_angle_of_sign_eq_one hs, angle_comm, angle_eq_arccos_of_angle_eq_pi_div_two (angle_rev_eq_pi_div_two_of_oangle_eq_pi_div_two h), dist_comm p₁ p₃] end /-- An angle in a right-angled triangle expressed using `arcsin`. -/ lemma oangle_right_eq_arcsin_of_oangle_eq_pi_div_two {p₁ p₂ p₃ : P} (h : ∡ p₁ p₂ p₃ = ↑(π / 2)) : ∡ p₂ p₃ p₁ = real.arcsin (dist p₁ p₂ / dist p₁ p₃) := begin have hs : (∡ p₂ p₃ p₁).sign = 1, { rw [oangle_rotate_sign, h, real.angle.sign_coe_pi_div_two] }, rw [oangle_eq_angle_of_sign_eq_one hs, angle_eq_arcsin_of_angle_eq_pi_div_two (angle_eq_pi_div_two_of_oangle_eq_pi_div_two h) (or.inl (left_ne_of_oangle_eq_pi_div_two h))] end /-- An angle in a right-angled triangle expressed using `arcsin`. -/ lemma oangle_left_eq_arcsin_of_oangle_eq_pi_div_two {p₁ p₂ p₃ : P} (h : ∡ p₁ p₂ p₃ = ↑(π / 2)) : ∡ p₃ p₁ p₂ = real.arcsin (dist p₃ p₂ / dist p₁ p₃) := begin have hs : (∡ p₃ p₁ p₂).sign = 1, { rw [←oangle_rotate_sign, h, real.angle.sign_coe_pi_div_two] }, rw [oangle_eq_angle_of_sign_eq_one hs, angle_comm, angle_eq_arcsin_of_angle_eq_pi_div_two (angle_rev_eq_pi_div_two_of_oangle_eq_pi_div_two h) (or.inr (left_ne_of_oangle_eq_pi_div_two h)), dist_comm p₁ p₃] end /-- An angle in a right-angled triangle expressed using `arctan`. -/ lemma oangle_right_eq_arctan_of_oangle_eq_pi_div_two {p₁ p₂ p₃ : P} (h : ∡ p₁ p₂ p₃ = ↑(π / 2)) : ∡ p₂ p₃ p₁ = real.arctan (dist p₁ p₂ / dist p₃ p₂) := begin have hs : (∡ p₂ p₃ p₁).sign = 1, { rw [oangle_rotate_sign, h, real.angle.sign_coe_pi_div_two] }, rw [oangle_eq_angle_of_sign_eq_one hs, angle_eq_arctan_of_angle_eq_pi_div_two (angle_eq_pi_div_two_of_oangle_eq_pi_div_two h) (right_ne_of_oangle_eq_pi_div_two h)] end /-- An angle in a right-angled triangle expressed using `arctan`. -/ lemma oangle_left_eq_arctan_of_oangle_eq_pi_div_two {p₁ p₂ p₃ : P} (h : ∡ p₁ p₂ p₃ = ↑(π / 2)) : ∡ p₃ p₁ p₂ = real.arctan (dist p₃ p₂ / dist p₁ p₂) := begin have hs : (∡ p₃ p₁ p₂).sign = 1, { rw [←oangle_rotate_sign, h, real.angle.sign_coe_pi_div_two] }, rw [oangle_eq_angle_of_sign_eq_one hs, angle_comm, angle_eq_arctan_of_angle_eq_pi_div_two (angle_rev_eq_pi_div_two_of_oangle_eq_pi_div_two h) (left_ne_of_oangle_eq_pi_div_two h)] end /-- The cosine of an angle in a right-angled triangle as a ratio of sides. -/ lemma cos_oangle_right_of_oangle_eq_pi_div_two {p₁ p₂ p₃ : P} (h : ∡ p₁ p₂ p₃ = ↑(π / 2)) : real.angle.cos (∡ p₂ p₃ p₁) = dist p₃ p₂ / dist p₁ p₃ := begin have hs : (∡ p₂ p₃ p₁).sign = 1, { rw [oangle_rotate_sign, h, real.angle.sign_coe_pi_div_two] }, rw [oangle_eq_angle_of_sign_eq_one hs, real.angle.cos_coe, cos_angle_of_angle_eq_pi_div_two (angle_eq_pi_div_two_of_oangle_eq_pi_div_two h)] end /-- The cosine of an angle in a right-angled triangle as a ratio of sides. -/ lemma cos_oangle_left_of_oangle_eq_pi_div_two {p₁ p₂ p₃ : P} (h : ∡ p₁ p₂ p₃ = ↑(π / 2)) : real.angle.cos (∡ p₃ p₁ p₂) = dist p₁ p₂ / dist p₁ p₃ := begin have hs : (∡ p₃ p₁ p₂).sign = 1, { rw [←oangle_rotate_sign, h, real.angle.sign_coe_pi_div_two] }, rw [oangle_eq_angle_of_sign_eq_one hs, angle_comm, real.angle.cos_coe, cos_angle_of_angle_eq_pi_div_two (angle_rev_eq_pi_div_two_of_oangle_eq_pi_div_two h), dist_comm p₁ p₃] end /-- The sine of an angle in a right-angled triangle as a ratio of sides. -/ lemma sin_oangle_right_of_oangle_eq_pi_div_two {p₁ p₂ p₃ : P} (h : ∡ p₁ p₂ p₃ = ↑(π / 2)) : real.angle.sin (∡ p₂ p₃ p₁) = dist p₁ p₂ / dist p₁ p₃ := begin have hs : (∡ p₂ p₃ p₁).sign = 1, { rw [oangle_rotate_sign, h, real.angle.sign_coe_pi_div_two] }, rw [oangle_eq_angle_of_sign_eq_one hs, real.angle.sin_coe, sin_angle_of_angle_eq_pi_div_two (angle_eq_pi_div_two_of_oangle_eq_pi_div_two h) (or.inl (left_ne_of_oangle_eq_pi_div_two h))] end /-- The sine of an angle in a right-angled triangle as a ratio of sides. -/ lemma sin_oangle_left_of_oangle_eq_pi_div_two {p₁ p₂ p₃ : P} (h : ∡ p₁ p₂ p₃ = ↑(π / 2)) : real.angle.sin (∡ p₃ p₁ p₂) = dist p₃ p₂ / dist p₁ p₃ := begin have hs : (∡ p₃ p₁ p₂).sign = 1, { rw [←oangle_rotate_sign, h, real.angle.sign_coe_pi_div_two] }, rw [oangle_eq_angle_of_sign_eq_one hs, angle_comm, real.angle.sin_coe, sin_angle_of_angle_eq_pi_div_two (angle_rev_eq_pi_div_two_of_oangle_eq_pi_div_two h) (or.inr (left_ne_of_oangle_eq_pi_div_two h)), dist_comm p₁ p₃] end /-- The tangent of an angle in a right-angled triangle as a ratio of sides. -/ lemma tan_oangle_right_of_oangle_eq_pi_div_two {p₁ p₂ p₃ : P} (h : ∡ p₁ p₂ p₃ = ↑(π / 2)) : real.angle.tan (∡ p₂ p₃ p₁) = dist p₁ p₂ / dist p₃ p₂ := begin have hs : (∡ p₂ p₃ p₁).sign = 1, { rw [oangle_rotate_sign, h, real.angle.sign_coe_pi_div_two] }, rw [oangle_eq_angle_of_sign_eq_one hs, real.angle.tan_coe, tan_angle_of_angle_eq_pi_div_two (angle_eq_pi_div_two_of_oangle_eq_pi_div_two h)] end /-- The tangent of an angle in a right-angled triangle as a ratio of sides. -/ lemma tan_oangle_left_of_oangle_eq_pi_div_two {p₁ p₂ p₃ : P} (h : ∡ p₁ p₂ p₃ = ↑(π / 2)) : real.angle.tan (∡ p₃ p₁ p₂) = dist p₃ p₂ / dist p₁ p₂ := begin have hs : (∡ p₃ p₁ p₂).sign = 1, { rw [←oangle_rotate_sign, h, real.angle.sign_coe_pi_div_two] }, rw [oangle_eq_angle_of_sign_eq_one hs, angle_comm, real.angle.tan_coe, tan_angle_of_angle_eq_pi_div_two (angle_rev_eq_pi_div_two_of_oangle_eq_pi_div_two h)] end /-- The cosine of an angle in a right-angled triangle multiplied by the hypotenuse equals the adjacent side. -/ lemma cos_oangle_right_mul_dist_of_oangle_eq_pi_div_two {p₁ p₂ p₃ : P} (h : ∡ p₁ p₂ p₃ = ↑(π / 2)) : real.angle.cos (∡ p₂ p₃ p₁) * dist p₁ p₃ = dist p₃ p₂ := begin have hs : (∡ p₂ p₃ p₁).sign = 1, { rw [oangle_rotate_sign, h, real.angle.sign_coe_pi_div_two] }, rw [oangle_eq_angle_of_sign_eq_one hs, real.angle.cos_coe, cos_angle_mul_dist_of_angle_eq_pi_div_two (angle_eq_pi_div_two_of_oangle_eq_pi_div_two h)] end /-- The cosine of an angle in a right-angled triangle multiplied by the hypotenuse equals the adjacent side. -/ lemma cos_oangle_left_mul_dist_of_oangle_eq_pi_div_two {p₁ p₂ p₃ : P} (h : ∡ p₁ p₂ p₃ = ↑(π / 2)) : real.angle.cos (∡ p₃ p₁ p₂) * dist p₁ p₃ = dist p₁ p₂ := begin have hs : (∡ p₃ p₁ p₂).sign = 1, { rw [←oangle_rotate_sign, h, real.angle.sign_coe_pi_div_two] }, rw [oangle_eq_angle_of_sign_eq_one hs, angle_comm, real.angle.cos_coe, dist_comm p₁ p₃, cos_angle_mul_dist_of_angle_eq_pi_div_two (angle_rev_eq_pi_div_two_of_oangle_eq_pi_div_two h)] end /-- The sine of an angle in a right-angled triangle multiplied by the hypotenuse equals the opposite side. -/ lemma sin_oangle_right_mul_dist_of_oangle_eq_pi_div_two {p₁ p₂ p₃ : P} (h : ∡ p₁ p₂ p₃ = ↑(π / 2)) : real.angle.sin (∡ p₂ p₃ p₁) * dist p₁ p₃ = dist p₁ p₂ := begin have hs : (∡ p₂ p₃ p₁).sign = 1, { rw [oangle_rotate_sign, h, real.angle.sign_coe_pi_div_two] }, rw [oangle_eq_angle_of_sign_eq_one hs, real.angle.sin_coe, sin_angle_mul_dist_of_angle_eq_pi_div_two (angle_eq_pi_div_two_of_oangle_eq_pi_div_two h)] end /-- The sine of an angle in a right-angled triangle multiplied by the hypotenuse equals the opposite side. -/ lemma sin_oangle_left_mul_dist_of_oangle_eq_pi_div_two {p₁ p₂ p₃ : P} (h : ∡ p₁ p₂ p₃ = ↑(π / 2)) : real.angle.sin (∡ p₃ p₁ p₂) * dist p₁ p₃ = dist p₃ p₂ := begin have hs : (∡ p₃ p₁ p₂).sign = 1, { rw [←oangle_rotate_sign, h, real.angle.sign_coe_pi_div_two] }, rw [oangle_eq_angle_of_sign_eq_one hs, angle_comm, real.angle.sin_coe, dist_comm p₁ p₃, sin_angle_mul_dist_of_angle_eq_pi_div_two (angle_rev_eq_pi_div_two_of_oangle_eq_pi_div_two h)] end /-- The tangent of an angle in a right-angled triangle multiplied by the adjacent side equals the opposite side. -/ lemma tan_oangle_right_mul_dist_of_oangle_eq_pi_div_two {p₁ p₂ p₃ : P} (h : ∡ p₁ p₂ p₃ = ↑(π / 2)) : real.angle.tan (∡ p₂ p₃ p₁) * dist p₃ p₂ = dist p₁ p₂ := begin have hs : (∡ p₂ p₃ p₁).sign = 1, { rw [oangle_rotate_sign, h, real.angle.sign_coe_pi_div_two] }, rw [oangle_eq_angle_of_sign_eq_one hs, real.angle.tan_coe, tan_angle_mul_dist_of_angle_eq_pi_div_two (angle_eq_pi_div_two_of_oangle_eq_pi_div_two h) (or.inr (right_ne_of_oangle_eq_pi_div_two h))] end /-- The tangent of an angle in a right-angled triangle multiplied by the adjacent side equals the opposite side. -/ lemma tan_oangle_left_mul_dist_of_oangle_eq_pi_div_two {p₁ p₂ p₃ : P} (h : ∡ p₁ p₂ p₃ = ↑(π / 2)) : real.angle.tan (∡ p₃ p₁ p₂) * dist p₁ p₂ = dist p₃ p₂ := begin have hs : (∡ p₃ p₁ p₂).sign = 1, { rw [←oangle_rotate_sign, h, real.angle.sign_coe_pi_div_two] }, rw [oangle_eq_angle_of_sign_eq_one hs, angle_comm, real.angle.tan_coe, tan_angle_mul_dist_of_angle_eq_pi_div_two (angle_rev_eq_pi_div_two_of_oangle_eq_pi_div_two h) (or.inr (left_ne_of_oangle_eq_pi_div_two h))] end /-- A side of a right-angled triangle divided by the cosine of the adjacent angle equals the hypotenuse. -/ lemma dist_div_cos_oangle_right_of_oangle_eq_pi_div_two {p₁ p₂ p₃ : P} (h : ∡ p₁ p₂ p₃ = ↑(π / 2)) : dist p₃ p₂ / real.angle.cos (∡ p₂ p₃ p₁) = dist p₁ p₃ := begin have hs : (∡ p₂ p₃ p₁).sign = 1, { rw [oangle_rotate_sign, h, real.angle.sign_coe_pi_div_two] }, rw [oangle_eq_angle_of_sign_eq_one hs, real.angle.cos_coe, dist_div_cos_angle_of_angle_eq_pi_div_two (angle_eq_pi_div_two_of_oangle_eq_pi_div_two h) (or.inr (right_ne_of_oangle_eq_pi_div_two h))] end /-- A side of a right-angled triangle divided by the cosine of the adjacent angle equals the hypotenuse. -/ lemma dist_div_cos_oangle_left_of_oangle_eq_pi_div_two {p₁ p₂ p₃ : P} (h : ∡ p₁ p₂ p₃ = ↑(π / 2)) : dist p₁ p₂ / real.angle.cos (∡ p₃ p₁ p₂) = dist p₁ p₃ := begin have hs : (∡ p₃ p₁ p₂).sign = 1, { rw [←oangle_rotate_sign, h, real.angle.sign_coe_pi_div_two] }, rw [oangle_eq_angle_of_sign_eq_one hs, angle_comm, real.angle.cos_coe, dist_comm p₁ p₃, dist_div_cos_angle_of_angle_eq_pi_div_two (angle_rev_eq_pi_div_two_of_oangle_eq_pi_div_two h) (or.inr (left_ne_of_oangle_eq_pi_div_two h))] end /-- A side of a right-angled triangle divided by the sine of the opposite angle equals the hypotenuse. -/ lemma dist_div_sin_oangle_right_of_oangle_eq_pi_div_two {p₁ p₂ p₃ : P} (h : ∡ p₁ p₂ p₃ = ↑(π / 2)) : dist p₁ p₂ / real.angle.sin (∡ p₂ p₃ p₁) = dist p₁ p₃ := begin have hs : (∡ p₂ p₃ p₁).sign = 1, { rw [oangle_rotate_sign, h, real.angle.sign_coe_pi_div_two] }, rw [oangle_eq_angle_of_sign_eq_one hs, real.angle.sin_coe, dist_div_sin_angle_of_angle_eq_pi_div_two (angle_eq_pi_div_two_of_oangle_eq_pi_div_two h) (or.inl (left_ne_of_oangle_eq_pi_div_two h))] end /-- A side of a right-angled triangle divided by the sine of the opposite angle equals the hypotenuse. -/ lemma dist_div_sin_oangle_left_of_oangle_eq_pi_div_two {p₁ p₂ p₃ : P} (h : ∡ p₁ p₂ p₃ = ↑(π / 2)) : dist p₃ p₂ / real.angle.sin (∡ p₃ p₁ p₂) = dist p₁ p₃ := begin have hs : (∡ p₃ p₁ p₂).sign = 1, { rw [←oangle_rotate_sign, h, real.angle.sign_coe_pi_div_two] }, rw [oangle_eq_angle_of_sign_eq_one hs, angle_comm, real.angle.sin_coe, dist_comm p₁ p₃, dist_div_sin_angle_of_angle_eq_pi_div_two (angle_rev_eq_pi_div_two_of_oangle_eq_pi_div_two h) (or.inl (right_ne_of_oangle_eq_pi_div_two h))] end /-- A side of a right-angled triangle divided by the tangent of the opposite angle equals the adjacent side. -/ lemma dist_div_tan_oangle_right_of_oangle_eq_pi_div_two {p₁ p₂ p₃ : P} (h : ∡ p₁ p₂ p₃ = ↑(π / 2)) : dist p₁ p₂ / real.angle.tan (∡ p₂ p₃ p₁) = dist p₃ p₂ := begin have hs : (∡ p₂ p₃ p₁).sign = 1, { rw [oangle_rotate_sign, h, real.angle.sign_coe_pi_div_two] }, rw [oangle_eq_angle_of_sign_eq_one hs, real.angle.tan_coe, dist_div_tan_angle_of_angle_eq_pi_div_two (angle_eq_pi_div_two_of_oangle_eq_pi_div_two h) (or.inl (left_ne_of_oangle_eq_pi_div_two h))] end /-- A side of a right-angled triangle divided by the tangent of the opposite angle equals the adjacent side. -/ lemma dist_div_tan_oangle_left_of_oangle_eq_pi_div_two {p₁ p₂ p₃ : P} (h : ∡ p₁ p₂ p₃ = ↑(π / 2)) : dist p₃ p₂ / real.angle.tan (∡ p₃ p₁ p₂) = dist p₁ p₂ := begin have hs : (∡ p₃ p₁ p₂).sign = 1, { rw [←oangle_rotate_sign, h, real.angle.sign_coe_pi_div_two] }, rw [oangle_eq_angle_of_sign_eq_one hs, angle_comm, real.angle.tan_coe, dist_div_tan_angle_of_angle_eq_pi_div_two (angle_rev_eq_pi_div_two_of_oangle_eq_pi_div_two h) (or.inl (right_ne_of_oangle_eq_pi_div_two h))] end end euclidean_geometry
cfa3e51a5eeb0a37c6a1e06058314125f5f0fd02
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/algebra/group_power/lemmas.lean
b10adb48ee8c07fa59af9f80f06fcdf3ef7a1572
[ "Apache-2.0" ]
permissive
hikari0108/mathlib
b7ea2b7350497ab1a0b87a09d093ecc025a50dfa
a9e7d333b0cfd45f13a20f7b96b7d52e19fa2901
refs/heads/master
1,690,483,608,260
1,631,541,580,000
1,631,541,580,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
35,261
lean
/- Copyright (c) 2015 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Robert Y. Lewis -/ import algebra.group_power.basic import algebra.invertible import algebra.opposites import data.list.basic import data.int.cast import data.equiv.basic import data.equiv.mul_add /-! # Lemmas about power operations on monoids and groups This file contains lemmas about `monoid.pow`, `group.pow`, `nsmul`, `gsmul` which require additional imports besides those available in `.basic`. -/ universes u v w x y z u₁ u₂ variables {M : Type u} {N : Type v} {G : Type w} {H : Type x} {A : Type y} {B : Type z} {R : Type u₁} {S : Type u₂} /-! ### (Additive) monoid -/ section monoid variables [monoid M] [monoid N] [add_monoid A] [add_monoid B] @[simp] theorem nsmul_one [has_one A] : ∀ n : ℕ, n • (1 : A) = n := add_monoid_hom.eq_nat_cast ⟨λ n, n • (1 : A), zero_nsmul _, λ _ _, add_nsmul _ _ _⟩ (one_nsmul _) @[simp, norm_cast] lemma units.coe_pow (u : units M) (n : ℕ) : ((u ^ n : units M) : M) = u ^ n := (units.coe_hom M).map_pow u n instance invertible_pow (m : M) [invertible m] (n : ℕ) : invertible (m ^ n) := { inv_of := ⅟ m ^ n, inv_of_mul_self := by rw [← (commute_inv_of m).symm.mul_pow, inv_of_mul_self, one_pow], mul_inv_of_self := by rw [← (commute_inv_of m).mul_pow, mul_inv_of_self, one_pow] } lemma inv_of_pow (m : M) [invertible m] (n : ℕ) [invertible (m ^ n)] : ⅟(m ^ n) = ⅟m ^ n := @invertible_unique M _ (m ^ n) (m ^ n) rfl ‹_› (invertible_pow m n) lemma is_unit.pow {m : M} (n : ℕ) : is_unit m → is_unit (m ^ n) := λ ⟨u, hu⟩, ⟨u ^ n, by simp *⟩ lemma is_unit_pos_pow_iff {M : Type*} [comm_monoid M] {m : M} {n : ℕ} (h : 0 < n) : is_unit (m ^ n) ↔ is_unit m := begin obtain ⟨p, rfl⟩ := nat.exists_eq_succ_of_ne_zero h.ne', refine ⟨λ h, _, is_unit.pow _⟩, obtain ⟨⟨k, k', hk, hk'⟩, h⟩ := h, rw [units.coe_mk] at h, refine ⟨⟨m, m ^ p * k', _, _⟩, _⟩, { rw [←mul_assoc, ←pow_succ, ←h, hk] }, { rw [mul_right_comm, ←pow_succ', ←h, hk] }, { exact units.coe_mk _ _ _ _ } end /-- If `x ^ n.succ = 1` then `x` has an inverse, `x^n`. -/ def invertible_of_pow_succ_eq_one (x : M) (n : ℕ) (hx : x ^ n.succ = 1) : invertible x := ⟨x ^ n, (pow_succ' x n).symm.trans hx, (pow_succ x n).symm.trans hx⟩ /-- If `x ^ n = 1` then `x` has an inverse, `x^(n - 1)`. -/ def invertible_of_pow_eq_one (x : M) (n : ℕ) (hx : x ^ n = 1) (hn : 0 < n) : invertible x := begin apply invertible_of_pow_succ_eq_one x (n - 1), convert hx, exact nat.sub_add_cancel (nat.succ_le_of_lt hn), end lemma is_unit_of_pow_eq_one (x : M) (n : ℕ) (hx : x ^ n = 1) (hn : 0 < n) : is_unit x := begin haveI := invertible_of_pow_eq_one x n hx hn, exact is_unit_of_invertible x end end monoid section group variables [group G] [group H] [add_group A] [add_group B] open int local attribute [ematch] le_of_lt open nat theorem gsmul_one [has_one A] (n : ℤ) : n • (1 : A) = n := by cases n; simp lemma gpow_add_one (a : G) : ∀ n : ℤ, a ^ (n + 1) = a ^ n * a | (of_nat n) := by simp [← int.coe_nat_succ, pow_succ'] | -[1+0] := by simp [int.neg_succ_of_nat_eq] | -[1+(n+1)] := by rw [int.neg_succ_of_nat_eq, gpow_neg, neg_add, neg_add_cancel_right, gpow_neg, ← int.coe_nat_succ, gpow_coe_nat, gpow_coe_nat, pow_succ _ (n + 1), mul_inv_rev, inv_mul_cancel_right] theorem add_one_gsmul : ∀ (a : A) (i : ℤ), (i + 1) • a = i • a + a := @gpow_add_one (multiplicative A) _ lemma gpow_sub_one (a : G) (n : ℤ) : a ^ (n - 1) = a ^ n * a⁻¹ := calc a ^ (n - 1) = a ^ (n - 1) * a * a⁻¹ : (mul_inv_cancel_right _ _).symm ... = a^n * a⁻¹ : by rw [← gpow_add_one, sub_add_cancel] lemma gpow_add (a : G) (m n : ℤ) : a ^ (m + n) = a ^ m * a ^ n := begin induction n using int.induction_on with n ihn n ihn, case hz : { simp }, { simp only [← add_assoc, gpow_add_one, ihn, mul_assoc] }, { rw [gpow_sub_one, ← mul_assoc, ← ihn, ← gpow_sub_one, add_sub_assoc] } end lemma mul_self_gpow (b : G) (m : ℤ) : b*b^m = b^(m+1) := by { conv_lhs {congr, rw ← gpow_one b }, rw [← gpow_add, add_comm] } lemma mul_gpow_self (b : G) (m : ℤ) : b^m*b = b^(m+1) := by { conv_lhs {congr, skip, rw ← gpow_one b }, rw [← gpow_add, add_comm] } theorem add_gsmul : ∀ (a : A) (i j : ℤ), (i + j) • a = i • a + j • a := @gpow_add (multiplicative A) _ lemma gpow_sub (a : G) (m n : ℤ) : a ^ (m - n) = a ^ m * (a ^ n)⁻¹ := by rw [sub_eq_add_neg, gpow_add, gpow_neg] lemma sub_gsmul (m n : ℤ) (a : A) : (m - n) • a = m • a - n • a := by simpa only [sub_eq_add_neg] using @gpow_sub (multiplicative A) _ _ _ _ theorem gpow_one_add (a : G) (i : ℤ) : a ^ (1 + i) = a * a ^ i := by rw [gpow_add, gpow_one] theorem one_add_gsmul : ∀ (a : A) (i : ℤ), (1 + i) • a = a + i • a := @gpow_one_add (multiplicative A) _ theorem gpow_mul_comm (a : G) (i j : ℤ) : a ^ i * a ^ j = a ^ j * a ^ i := by rw [← gpow_add, ← gpow_add, add_comm] theorem gsmul_add_comm : ∀ (a : A) (i j : ℤ), i • a + j • a = j • a + i • a := @gpow_mul_comm (multiplicative A) _ theorem gpow_mul (a : G) (m n : ℤ) : a ^ (m * n) = (a ^ m) ^ n := int.induction_on n (by simp) (λ n ihn, by simp [mul_add, gpow_add, ihn]) (λ n ihn, by simp only [mul_sub, gpow_sub, ihn, mul_one, gpow_one]) theorem gsmul_mul' : ∀ (a : A) (m n : ℤ), (m * n) • a = n • (m • a) := @gpow_mul (multiplicative A) _ theorem gpow_mul' (a : G) (m n : ℤ) : a ^ (m * n) = (a ^ n) ^ m := by rw [mul_comm, gpow_mul] theorem mul_gsmul (a : A) (m n : ℤ) : (m * n) • a = m • (n • a) := by rw [mul_comm, gsmul_mul'] theorem gpow_bit0 (a : G) (n : ℤ) : a ^ bit0 n = a ^ n * a ^ n := gpow_add _ _ _ theorem bit0_gsmul (a : A) (n : ℤ) : bit0 n • a = n • a + n • a := @gpow_bit0 (multiplicative A) _ _ _ theorem gpow_bit1 (a : G) (n : ℤ) : a ^ bit1 n = a ^ n * a ^ n * a := by rw [bit1, gpow_add, gpow_bit0, gpow_one] theorem bit1_gsmul : ∀ (a : A) (n : ℤ), bit1 n • a = n • a + n • a + a := @gpow_bit1 (multiplicative A) _ @[simp] theorem monoid_hom.map_gpow (f : G →* H) (a : G) (n : ℤ) : f (a ^ n) = f a ^ n := by cases n; simp @[simp] theorem add_monoid_hom.map_gsmul (f : A →+ B) (a : A) (n : ℤ) : f (n • a) = n • f a := f.to_multiplicative.map_gpow a n @[simp, norm_cast] lemma units.coe_gpow (u : units G) (n : ℤ) : ((u ^ n : units G) : G) = u ^ n := (units.coe_hom G).map_gpow u n end group section ordered_add_comm_group variables [ordered_add_comm_group A] /-! Lemmas about `gsmul` under ordering, placed here (rather than in `algebra.group_power.order` with their friends) because they require facts from `data.int.basic`-/ open int lemma gsmul_pos {a : A} (ha : 0 < a) {k : ℤ} (hk : (0:ℤ) < k) : 0 < k • a := begin lift k to ℕ using int.le_of_lt hk, rw gsmul_coe_nat, apply nsmul_pos ha, exact (coe_nat_pos.mp hk).ne', end theorem gsmul_strict_mono_left {a : A} (ha : 0 < a) : strict_mono (λ n : ℤ, n • a) := λ n m h, calc n • a = n • a + 0 : (add_zero _).symm ... < n • a + (m - n) • a : add_lt_add_left (gsmul_pos ha (sub_pos.mpr h)) _ ... = m • a : by { rw [← add_gsmul], simp } theorem gsmul_mono_left {a : A} (ha : 0 ≤ a) : monotone (λ n : ℤ, n • a) := λ n m h, calc n • a = n • a + 0 : (add_zero _).symm ... ≤ n • a + (m - n) • a : add_le_add_left (gsmul_nonneg ha (sub_nonneg.mpr h)) _ ... = m • a : by { rw [← add_gsmul], simp } theorem gsmul_le_gsmul {a : A} {n m : ℤ} (ha : 0 ≤ a) (h : n ≤ m) : n • a ≤ m • a := gsmul_mono_left ha h theorem gsmul_lt_gsmul {a : A} {n m : ℤ} (ha : 0 < a) (h : n < m) : n • a < m • a := gsmul_strict_mono_left ha h theorem gsmul_le_gsmul_iff {a : A} {n m : ℤ} (ha : 0 < a) : n • a ≤ m • a ↔ n ≤ m := (gsmul_strict_mono_left ha).le_iff_le theorem gsmul_lt_gsmul_iff {a : A} {n m : ℤ} (ha : 0 < a) : n • a < m • a ↔ n < m := (gsmul_strict_mono_left ha).lt_iff_lt variables (A) lemma gsmul_strict_mono_right {n : ℤ} (hn : 0 < n) : strict_mono ((•) n : A → A) := λ a b hab, begin rw ← sub_pos at hab, rw [← sub_pos, ← gsmul_sub], exact gsmul_pos hab hn, end lemma gsmul_mono_right {n : ℤ} (hn : 0 ≤ n) : monotone ((•) n : A → A) := λ a b hab, begin rw ← sub_nonneg at hab, rw [← sub_nonneg, ← gsmul_sub], exact gsmul_nonneg hab hn, end variables {A} theorem gsmul_le_gsmul' {n : ℤ} (hn : 0 ≤ n) {a₁ a₂ : A} (h : a₁ ≤ a₂) : n • a₁ ≤ n • a₂ := gsmul_mono_right A hn h theorem gsmul_lt_gsmul' {n : ℤ} (hn : 0 < n) {a₁ a₂ : A} (h : a₁ < a₂) : n • a₁ < n • a₂ := gsmul_strict_mono_right A hn h lemma abs_nsmul {α : Type*} [linear_ordered_add_comm_group α] (n : ℕ) (a : α) : abs (n • a) = n • abs a := begin cases le_total a 0 with hneg hpos, { rw [abs_of_nonpos hneg, ← abs_neg, ← neg_nsmul, abs_of_nonneg], exact nsmul_nonneg (neg_nonneg.mpr hneg) n }, { rw [abs_of_nonneg hpos, abs_of_nonneg], exact nsmul_nonneg hpos n } end lemma abs_gsmul {α : Type*} [linear_ordered_add_comm_group α] (n : ℤ) (a : α) : abs (n • a) = (abs n) • abs a := begin by_cases n0 : 0 ≤ n, { lift n to ℕ using n0, simp only [abs_nsmul, coe_nat_abs, gsmul_coe_nat] }, { lift (- n) to ℕ using int.le_of_lt (neg_pos.mpr (not_le.mp n0)) with m h, rw [← abs_neg (n • a), ← neg_gsmul, ← abs_neg n, ← h, gsmul_coe_nat, coe_nat_abs, gsmul_coe_nat], exact abs_nsmul m _ }, end lemma abs_add_eq_add_abs_le {α : Type*} [linear_ordered_add_comm_group α] {a b : α} (hle : a ≤ b) : abs (a + b) = abs a + abs b ↔ (0 ≤ a ∧ 0 ≤ b ∨ a ≤ 0 ∧ b ≤ 0) := begin by_cases a0 : 0 ≤ a; by_cases b0 : 0 ≤ b, { simp [a0, b0, abs_of_nonneg, add_nonneg a0 b0] }, { exact (lt_irrefl (0 : α) (a0.trans_lt (hle.trans_lt (not_le.mp b0)))).elim }, any_goals { simp [(not_le.mp a0).le, (not_le.mp b0).le, abs_of_nonpos, add_nonpos, add_comm] }, obtain F := (not_le.mp a0), have : (abs (a + b) = -a + b ↔ b ≤ 0) ↔ (abs (a + b) = abs a + abs b ↔ 0 ≤ a ∧ 0 ≤ b ∨ a ≤ 0 ∧ b ≤ 0), { simp [a0, b0, abs_of_neg, abs_of_nonneg, F, F.le] }, refine this.mp ⟨λ h, _, λ h, by simp only [le_antisymm h b0, abs_of_neg F, add_zero]⟩, by_cases ba : a + b ≤ 0, { refine le_of_eq (eq_zero_of_neg_eq _), rwa [abs_of_nonpos ba, neg_add_rev, add_comm, add_right_inj] at h }, { refine (lt_irrefl (0 : α) _).elim, rw [abs_of_pos (not_le.mp ba), add_left_inj] at h, rwa eq_zero_of_neg_eq h.symm at F } end lemma abs_add_eq_add_abs_iff {α : Type*} [linear_ordered_add_comm_group α] (a b : α) : abs (a + b) = abs a + abs b ↔ (0 ≤ a ∧ 0 ≤ b ∨ a ≤ 0 ∧ b ≤ 0) := begin by_cases ab : a ≤ b, { exact abs_add_eq_add_abs_le ab }, { rw [add_comm a, add_comm (abs _), abs_add_eq_add_abs_le ((not_le.mp ab).le), and.comm, @and.comm (b ≤ 0 ) _] } end end ordered_add_comm_group section linear_ordered_add_comm_group variable [linear_ordered_add_comm_group A] theorem gsmul_le_gsmul_iff' {n : ℤ} (hn : 0 < n) {a₁ a₂ : A} : n • a₁ ≤ n • a₂ ↔ a₁ ≤ a₂ := (gsmul_strict_mono_right A hn).le_iff_le theorem gsmul_lt_gsmul_iff' {n : ℤ} (hn : 0 < n) {a₁ a₂ : A} : n • a₁ < n • a₂ ↔ a₁ < a₂ := (gsmul_strict_mono_right A hn).lt_iff_lt theorem nsmul_le_nsmul_iff {a : A} {n m : ℕ} (ha : 0 < a) : n • a ≤ m • a ↔ n ≤ m := begin refine ⟨λ h, _, nsmul_le_nsmul $ le_of_lt ha⟩, by_contra H, exact lt_irrefl _ (lt_of_lt_of_le (nsmul_lt_nsmul ha (not_le.mp H)) h) end theorem nsmul_lt_nsmul_iff {a : A} {n m : ℕ} (ha : 0 < a) : n • a < m • a ↔ n < m := begin refine ⟨λ h, _, nsmul_lt_nsmul ha⟩, by_contra H, exact lt_irrefl _ (lt_of_le_of_lt (nsmul_le_nsmul (le_of_lt ha) $ not_lt.mp H) h) end /-- See also `smul_right_injective`. TODO: provide a `no_zero_smul_divisors` instance. We can't do that here because importing that definition would create import cycles. -/ lemma gsmul_right_injective {m : ℤ} (hm : m ≠ 0) : function.injective ((•) m : A → A) := begin cases hm.symm.lt_or_lt, { exact (gsmul_strict_mono_right A h).injective, }, { intros a b hab, refine (gsmul_strict_mono_right A (neg_pos.mpr h)).injective _, rw [neg_gsmul, neg_gsmul, hab], }, end lemma gsmul_right_inj {a b : A} {m : ℤ} (hm : m ≠ 0) : m • a = m • b ↔ a = b := (gsmul_right_injective hm).eq_iff /-- Alias of `gsmul_right_inj`, for ease of discovery alongside `gsmul_le_gsmul_iff'` and `gsmul_lt_gsmul_iff'`. -/ lemma gsmul_eq_gsmul_iff' {a b : A} {m : ℤ} (hm : m ≠ 0) : m • a = m • b ↔ a = b := gsmul_right_inj hm end linear_ordered_add_comm_group @[simp] lemma with_bot.coe_nsmul [add_monoid A] (a : A) (n : ℕ) : ((n • a : A) : with_bot A) = n • a := add_monoid_hom.map_nsmul ⟨(coe : A → with_bot A), with_bot.coe_zero, with_bot.coe_add⟩ a n theorem nsmul_eq_mul' [semiring R] (a : R) (n : ℕ) : n • a = a * n := by induction n with n ih; [rw [zero_nsmul, nat.cast_zero, mul_zero], rw [succ_nsmul', ih, nat.cast_succ, mul_add, mul_one]] @[simp] theorem nsmul_eq_mul [semiring R] (n : ℕ) (a : R) : n • a = n * a := by rw [nsmul_eq_mul', (n.cast_commute a).eq] theorem mul_nsmul_left [semiring R] (a b : R) (n : ℕ) : n • (a * b) = a * (n • b) := by rw [nsmul_eq_mul', nsmul_eq_mul', mul_assoc] theorem mul_nsmul_assoc [semiring R] (a b : R) (n : ℕ) : n • (a * b) = n • a * b := by rw [nsmul_eq_mul, nsmul_eq_mul, mul_assoc] @[simp, norm_cast] theorem nat.cast_pow [semiring R] (n m : ℕ) : (↑(n ^ m) : R) = ↑n ^ m := begin induction m with m ih, { rw [pow_zero, pow_zero], exact nat.cast_one }, { rw [pow_succ', pow_succ', nat.cast_mul, ih] } end @[simp, norm_cast] theorem int.coe_nat_pow (n m : ℕ) : ((n ^ m : ℕ) : ℤ) = n ^ m := by induction m with m ih; [exact int.coe_nat_one, rw [pow_succ', pow_succ', int.coe_nat_mul, ih]] theorem int.nat_abs_pow (n : ℤ) (k : ℕ) : int.nat_abs (n ^ k) = (int.nat_abs n) ^ k := by induction k with k ih; [refl, rw [pow_succ', int.nat_abs_mul, pow_succ', ih]] -- The next four lemmas allow us to replace multiplication by a numeral with a `gsmul` expression. -- They are used by the `noncomm_ring` tactic, to normalise expressions before passing to `abel`. lemma bit0_mul [ring R] {n r : R} : bit0 n * r = (2 : ℤ) • (n * r) := by { dsimp [bit0], rw [add_mul, add_gsmul, one_gsmul], } lemma mul_bit0 [ring R] {n r : R} : r * bit0 n = (2 : ℤ) • (r * n) := by { dsimp [bit0], rw [mul_add, add_gsmul, one_gsmul], } lemma bit1_mul [ring R] {n r : R} : bit1 n * r = (2 : ℤ) • (n * r) + r := by { dsimp [bit1], rw [add_mul, bit0_mul, one_mul], } lemma mul_bit1 [ring R] {n r : R} : r * bit1 n = (2 : ℤ) • (r * n) + r := by { dsimp [bit1], rw [mul_add, mul_bit0, mul_one], } @[simp] theorem gsmul_eq_mul [ring R] (a : R) : ∀ (n : ℤ), n • a = n * a | (n : ℕ) := by { rw [gsmul_coe_nat, nsmul_eq_mul], refl } | -[1+ n] := by simp [nat.cast_succ, neg_add_rev, int.cast_neg_succ_of_nat, add_mul] theorem gsmul_eq_mul' [ring R] (a : R) (n : ℤ) : n • a = a * n := by rw [gsmul_eq_mul, (n.cast_commute a).eq] theorem mul_gsmul_left [ring R] (a b : R) (n : ℤ) : n • (a * b) = a * (n • b) := by rw [gsmul_eq_mul', gsmul_eq_mul', mul_assoc] theorem mul_gsmul_assoc [ring R] (a b : R) (n : ℤ) : n • (a * b) = n • a * b := by rw [gsmul_eq_mul, gsmul_eq_mul, mul_assoc] lemma gsmul_int_int (a b : ℤ) : a • b = a * b := by simp lemma gsmul_int_one (n : ℤ) : n • 1 = n := by simp @[simp, norm_cast] theorem int.cast_pow [ring R] (n : ℤ) (m : ℕ) : (↑(n ^ m) : R) = ↑n ^ m := begin induction m with m ih, { rw [pow_zero, pow_zero, int.cast_one] }, { rw [pow_succ, pow_succ, int.cast_mul, ih] } end lemma neg_one_pow_eq_pow_mod_two [ring R] {n : ℕ} : (-1 : R) ^ n = (-1) ^ (n % 2) := by rw [← nat.mod_add_div n 2, pow_add, pow_mul]; simp [sq] section ordered_semiring variable [ordered_semiring R] /-- Bernoulli's inequality. This version works for semirings but requires additional hypotheses `0 ≤ a * a` and `0 ≤ (1 + a) * (1 + a)`. -/ theorem one_add_mul_le_pow' {a : R} (Hsq : 0 ≤ a * a) (Hsq' : 0 ≤ (1 + a) * (1 + a)) (H : 0 ≤ 2 + a) : ∀ (n : ℕ), 1 + (n : R) * a ≤ (1 + a) ^ n | 0 := by simp | 1 := by simp | (n+2) := have 0 ≤ (n : R) * (a * a * (2 + a)) + a * a, from add_nonneg (mul_nonneg n.cast_nonneg (mul_nonneg Hsq H)) Hsq, calc 1 + (↑(n + 2) : R) * a ≤ 1 + ↑(n + 2) * a + (n * (a * a * (2 + a)) + a * a) : (le_add_iff_nonneg_right _).2 this ... = (1 + a) * (1 + a) * (1 + n * a) : by { simp [add_mul, mul_add, bit0, mul_assoc, (n.cast_commute (_ : R)).left_comm], ac_refl } ... ≤ (1 + a) * (1 + a) * (1 + a)^n : mul_le_mul_of_nonneg_left (one_add_mul_le_pow' n) Hsq' ... = (1 + a)^(n + 2) : by simp only [pow_succ, mul_assoc] private lemma pow_lt_pow_of_lt_one_aux {a : R} (h : 0 < a) (ha : a < 1) (i : ℕ) : ∀ k : ℕ, a ^ (i + k + 1) < a ^ i | 0 := begin rw [←one_mul (a^i), add_zero, pow_succ], exact mul_lt_mul ha (le_refl _) (pow_pos h _) zero_le_one end | (k+1) := begin rw [←one_mul (a^i), pow_succ], apply mul_lt_mul ha _ _ zero_le_one, { apply le_of_lt, apply pow_lt_pow_of_lt_one_aux }, { show 0 < a ^ (i + (k + 1) + 0), apply pow_pos h } end private lemma pow_le_pow_of_le_one_aux {a : R} (h : 0 ≤ a) (ha : a ≤ 1) (i : ℕ) : ∀ k : ℕ, a ^ (i + k) ≤ a ^ i | 0 := by simp | (k+1) := by { rw [←add_assoc, ←one_mul (a^i), pow_succ], exact mul_le_mul ha (pow_le_pow_of_le_one_aux _) (pow_nonneg h _) zero_le_one } lemma pow_lt_pow_of_lt_one {a : R} (h : 0 < a) (ha : a < 1) {i j : ℕ} (hij : i < j) : a ^ j < a ^ i := let ⟨k, hk⟩ := nat.exists_eq_add_of_lt hij in by rw hk; exact pow_lt_pow_of_lt_one_aux h ha _ _ lemma pow_lt_pow_iff_of_lt_one {a : R} {n m : ℕ} (hpos : 0 < a) (h : a < 1) : a ^ m < a ^ n ↔ n < m := begin have : strict_mono (λ (n : order_dual ℕ), a ^ (id n : ℕ)) := λ m n, pow_lt_pow_of_lt_one hpos h, exact this.lt_iff_lt end lemma pow_le_pow_of_le_one {a : R} (h : 0 ≤ a) (ha : a ≤ 1) {i j : ℕ} (hij : i ≤ j) : a ^ j ≤ a ^ i := let ⟨k, hk⟩ := nat.exists_eq_add_of_le hij in by rw hk; exact pow_le_pow_of_le_one_aux h ha _ _ lemma pow_le_one {x : R} : ∀ (n : ℕ) (h0 : 0 ≤ x) (h1 : x ≤ 1), x ^ n ≤ 1 | 0 h0 h1 := by rw [pow_zero] | (n+1) h0 h1 := by { rw [pow_succ], exact mul_le_one h1 (pow_nonneg h0 _) (pow_le_one n h0 h1) } end ordered_semiring section linear_ordered_semiring variables [linear_ordered_semiring R] lemma sign_cases_of_C_mul_pow_nonneg {C r : R} (h : ∀ n : ℕ, 0 ≤ C * r ^ n) : C = 0 ∨ (0 < C ∧ 0 ≤ r) := begin have : 0 ≤ C, by simpa only [pow_zero, mul_one] using h 0, refine this.eq_or_lt.elim (λ h, or.inl h.symm) (λ hC, or.inr ⟨hC, _⟩), refine nonneg_of_mul_nonneg_left _ hC, simpa only [pow_one] using h 1 end end linear_ordered_semiring section linear_ordered_ring variables [linear_ordered_ring R] {a : R} {n : ℕ} @[simp] lemma abs_pow (a : R) (n : ℕ) : abs (a ^ n) = abs a ^ n := (pow_abs a n).symm @[simp] theorem pow_bit1_neg_iff : a ^ bit1 n < 0 ↔ a < 0 := ⟨λ h, not_le.1 $ λ h', not_le.2 h $ pow_nonneg h' _, λ h, by { rw [bit1, pow_succ], exact mul_neg_of_neg_of_pos h (pow_bit0_pos h.ne _)}⟩ @[simp] theorem pow_bit1_nonneg_iff : 0 ≤ a ^ bit1 n ↔ 0 ≤ a := le_iff_le_iff_lt_iff_lt.2 pow_bit1_neg_iff @[simp] theorem pow_bit1_nonpos_iff : a ^ bit1 n ≤ 0 ↔ a ≤ 0 := by simp only [le_iff_lt_or_eq, pow_bit1_neg_iff, pow_eq_zero_iff (bit1_pos (zero_le n))] @[simp] theorem pow_bit1_pos_iff : 0 < a ^ bit1 n ↔ 0 < a := lt_iff_lt_of_le_iff_le pow_bit1_nonpos_iff theorem pow_even_nonneg (a : R) (hn : even n) : 0 ≤ a ^ n := by cases hn with k hk; simpa only [hk, two_mul] using pow_bit0_nonneg a k theorem pow_even_pos (ha : a ≠ 0) (hn : even n) : 0 < a ^ n := by cases hn with k hk; simpa only [hk, two_mul] using pow_bit0_pos ha k theorem pow_odd_nonneg (ha : 0 ≤ a) (hn : odd n) : 0 ≤ a ^ n := by cases hn with k hk; simpa only [hk, two_mul] using pow_bit1_nonneg_iff.mpr ha theorem pow_odd_pos (ha : 0 < a) (hn : odd n) : 0 < a ^ n := by cases hn with k hk; simpa only [hk, two_mul] using pow_bit1_pos_iff.mpr ha theorem pow_odd_nonpos (ha : a ≤ 0) (hn : odd n) : a ^ n ≤ 0:= by cases hn with k hk; simpa only [hk, two_mul] using pow_bit1_nonpos_iff.mpr ha theorem pow_odd_neg (ha : a < 0) (hn : odd n) : a ^ n < 0:= by cases hn with k hk; simpa only [hk, two_mul] using pow_bit1_neg_iff.mpr ha lemma pow_even_abs (a : R) {p : ℕ} (hp : even p) : abs a ^ p = a ^ p := begin rw [←abs_pow, abs_eq_self], exact pow_even_nonneg _ hp end @[simp] lemma pow_bit0_abs (a : R) (p : ℕ) : abs a ^ bit0 p = a ^ bit0 p := pow_even_abs _ (even_bit0 _) lemma strict_mono_pow_bit1 (n : ℕ) : strict_mono (λ a : R, a ^ bit1 n) := begin intros a b hab, cases le_total a 0 with ha ha, { cases le_or_lt b 0 with hb hb, { rw [← neg_lt_neg_iff, ← neg_pow_bit1, ← neg_pow_bit1], exact pow_lt_pow_of_lt_left (neg_lt_neg hab) (neg_nonneg.2 hb) (bit1_pos (zero_le n)) }, { exact (pow_bit1_nonpos_iff.2 ha).trans_lt (pow_bit1_pos_iff.2 hb) } }, { exact pow_lt_pow_of_lt_left hab ha (bit1_pos (zero_le n)) } end /-- Bernoulli's inequality for `n : ℕ`, `-2 ≤ a`. -/ theorem one_add_mul_le_pow (H : -2 ≤ a) (n : ℕ) : 1 + (n : R) * a ≤ (1 + a) ^ n := one_add_mul_le_pow' (mul_self_nonneg _) (mul_self_nonneg _) (neg_le_iff_add_nonneg'.1 H) _ /-- Bernoulli's inequality reformulated to estimate `a^n`. -/ theorem one_add_mul_sub_le_pow (H : -1 ≤ a) (n : ℕ) : 1 + (n : R) * (a - 1) ≤ a ^ n := have -2 ≤ a - 1, by rwa [bit0, neg_add, ← sub_eq_add_neg, sub_le_sub_iff_right], by simpa only [add_sub_cancel'_right] using one_add_mul_le_pow this n end linear_ordered_ring /-- Bernoulli's inequality reformulated to estimate `(n : K)`. -/ theorem nat.cast_le_pow_sub_div_sub {K : Type*} [linear_ordered_field K] {a : K} (H : 1 < a) (n : ℕ) : (n : K) ≤ (a ^ n - 1) / (a - 1) := (le_div_iff (sub_pos.2 H)).2 $ le_sub_left_of_add_le $ one_add_mul_sub_le_pow ((neg_le_self $ @zero_le_one K _).trans H.le) _ /-- For any `a > 1` and a natural `n` we have `n ≤ a ^ n / (a - 1)`. See also `nat.cast_le_pow_sub_div_sub` for a stronger inequality with `a ^ n - 1` in the numerator. -/ theorem nat.cast_le_pow_div_sub {K : Type*} [linear_ordered_field K] {a : K} (H : 1 < a) (n : ℕ) : (n : K) ≤ a ^ n / (a - 1) := (n.cast_le_pow_sub_div_sub H).trans $ div_le_div_of_le (sub_nonneg.2 H.le) (sub_le_self _ zero_le_one) namespace int lemma units_sq (u : units ℤ) : u ^ 2 = 1 := (sq u).symm ▸ units_mul_self u alias int.units_sq ← int.units_pow_two lemma units_pow_eq_pow_mod_two (u : units ℤ) (n : ℕ) : u ^ n = u ^ (n % 2) := by conv {to_lhs, rw ← nat.mod_add_div n 2}; rw [pow_add, pow_mul, units_sq, one_pow, mul_one] @[simp] lemma nat_abs_sq (x : ℤ) : (x.nat_abs ^ 2 : ℤ) = x ^ 2 := by rw [sq, int.nat_abs_mul_self', sq] alias int.nat_abs_sq ← int.nat_abs_pow_two lemma abs_le_self_sq (a : ℤ) : (int.nat_abs a : ℤ) ≤ a ^ 2 := by { rw [← int.nat_abs_sq a, sq], norm_cast, apply nat.le_mul_self } alias int.abs_le_self_sq ← int.abs_le_self_pow_two lemma le_self_sq (b : ℤ) : b ≤ b ^ 2 := le_trans (le_nat_abs) (abs_le_self_sq _) alias int.le_self_sq ← int.le_self_pow_two lemma pow_right_injective {x : ℤ} (h : 1 < x.nat_abs) : function.injective ((^) x : ℕ → ℤ) := begin suffices : function.injective (nat_abs ∘ ((^) x : ℕ → ℤ)), { exact function.injective.of_comp this }, convert nat.pow_right_injective h, ext n, rw [function.comp_app, nat_abs_pow] end end int variables (M G A) /-- Monoid homomorphisms from `multiplicative ℕ` are defined by the image of `multiplicative.of_add 1`. -/ def powers_hom [monoid M] : M ≃ (multiplicative ℕ →* M) := { to_fun := λ x, ⟨λ n, x ^ n.to_add, by { convert pow_zero x, exact to_add_one }, λ m n, pow_add x m n⟩, inv_fun := λ f, f (multiplicative.of_add 1), left_inv := pow_one, right_inv := λ f, monoid_hom.ext $ λ n, by { simp [← f.map_pow, ← of_add_nsmul] } } /-- Monoid homomorphisms from `multiplicative ℤ` are defined by the image of `multiplicative.of_add 1`. -/ def gpowers_hom [group G] : G ≃ (multiplicative ℤ →* G) := { to_fun := λ x, ⟨λ n, x ^ n.to_add, gpow_zero x, λ m n, gpow_add x m n⟩, inv_fun := λ f, f (multiplicative.of_add 1), left_inv := gpow_one, right_inv := λ f, monoid_hom.ext $ λ n, by { simp [← f.map_gpow, ← of_add_gsmul ] } } /-- Additive homomorphisms from `ℕ` are defined by the image of `1`. -/ def multiples_hom [add_monoid A] : A ≃ (ℕ →+ A) := { to_fun := λ x, ⟨λ n, n • x, zero_nsmul x, λ m n, add_nsmul _ _ _⟩, inv_fun := λ f, f 1, left_inv := one_nsmul, right_inv := λ f, add_monoid_hom.ext_nat $ one_nsmul (f 1) } /-- Additive homomorphisms from `ℤ` are defined by the image of `1`. -/ def gmultiples_hom [add_group A] : A ≃ (ℤ →+ A) := { to_fun := λ x, ⟨λ n, n • x, zero_gsmul x, λ m n, add_gsmul _ _ _⟩, inv_fun := λ f, f 1, left_inv := one_gsmul, right_inv := λ f, add_monoid_hom.ext_int $ one_gsmul (f 1) } variables {M G A} @[simp] lemma powers_hom_apply [monoid M] (x : M) (n : multiplicative ℕ) : powers_hom M x n = x ^ n.to_add := rfl @[simp] lemma powers_hom_symm_apply [monoid M] (f : multiplicative ℕ →* M) : (powers_hom M).symm f = f (multiplicative.of_add 1) := rfl @[simp] lemma gpowers_hom_apply [group G] (x : G) (n : multiplicative ℤ) : gpowers_hom G x n = x ^ n.to_add := rfl @[simp] lemma gpowers_hom_symm_apply [group G] (f : multiplicative ℤ →* G) : (gpowers_hom G).symm f = f (multiplicative.of_add 1) := rfl @[simp] lemma multiples_hom_apply [add_monoid A] (x : A) (n : ℕ) : multiples_hom A x n = n • x := rfl @[simp] lemma multiples_hom_symm_apply [add_monoid A] (f : ℕ →+ A) : (multiples_hom A).symm f = f 1 := rfl @[simp] lemma gmultiples_hom_apply [add_group A] (x : A) (n : ℤ) : gmultiples_hom A x n = n • x := rfl @[simp] lemma gmultiples_hom_symm_apply [add_group A] (f : ℤ →+ A) : (gmultiples_hom A).symm f = f 1 := rfl lemma monoid_hom.apply_mnat [monoid M] (f : multiplicative ℕ →* M) (n : multiplicative ℕ) : f n = (f (multiplicative.of_add 1)) ^ n.to_add := by rw [← powers_hom_symm_apply, ← powers_hom_apply, equiv.apply_symm_apply] @[ext] lemma monoid_hom.ext_mnat [monoid M] ⦃f g : multiplicative ℕ →* M⦄ (h : f (multiplicative.of_add 1) = g (multiplicative.of_add 1)) : f = g := monoid_hom.ext $ λ n, by rw [f.apply_mnat, g.apply_mnat, h] lemma monoid_hom.apply_mint [group M] (f : multiplicative ℤ →* M) (n : multiplicative ℤ) : f n = (f (multiplicative.of_add 1)) ^ n.to_add := by rw [← gpowers_hom_symm_apply, ← gpowers_hom_apply, equiv.apply_symm_apply] /-! `monoid_hom.ext_mint` is defined in `data.int.cast` -/ lemma add_monoid_hom.apply_nat [add_monoid M] (f : ℕ →+ M) (n : ℕ) : f n = n • (f 1) := by rw [← multiples_hom_symm_apply, ← multiples_hom_apply, equiv.apply_symm_apply] /-! `add_monoid_hom.ext_nat` is defined in `data.nat.cast` -/ lemma add_monoid_hom.apply_int [add_group M] (f : ℤ →+ M) (n : ℤ) : f n = n • (f 1) := by rw [← gmultiples_hom_symm_apply, ← gmultiples_hom_apply, equiv.apply_symm_apply] /-! `add_monoid_hom.ext_int` is defined in `data.int.cast` -/ variables (M G A) /-- If `M` is commutative, `powers_hom` is a multiplicative equivalence. -/ def powers_mul_hom [comm_monoid M] : M ≃* (multiplicative ℕ →* M) := { map_mul' := λ a b, monoid_hom.ext $ by simp [mul_pow], ..powers_hom M} /-- If `M` is commutative, `gpowers_hom` is a multiplicative equivalence. -/ def gpowers_mul_hom [comm_group G] : G ≃* (multiplicative ℤ →* G) := { map_mul' := λ a b, monoid_hom.ext $ by simp [mul_gpow], ..gpowers_hom G} /-- If `M` is commutative, `multiples_hom` is an additive equivalence. -/ def multiples_add_hom [add_comm_monoid A] : A ≃+ (ℕ →+ A) := { map_add' := λ a b, add_monoid_hom.ext $ by simp [nsmul_add], ..multiples_hom A} /-- If `M` is commutative, `gmultiples_hom` is an additive equivalence. -/ def gmultiples_add_hom [add_comm_group A] : A ≃+ (ℤ →+ A) := { map_add' := λ a b, add_monoid_hom.ext $ by simp [gsmul_add], ..gmultiples_hom A} variables {M G A} @[simp] lemma powers_mul_hom_apply [comm_monoid M] (x : M) (n : multiplicative ℕ) : powers_mul_hom M x n = x ^ n.to_add := rfl @[simp] lemma powers_mul_hom_symm_apply [comm_monoid M] (f : multiplicative ℕ →* M) : (powers_mul_hom M).symm f = f (multiplicative.of_add 1) := rfl @[simp] lemma gpowers_mul_hom_apply [comm_group G] (x : G) (n : multiplicative ℤ) : gpowers_mul_hom G x n = x ^ n.to_add := rfl @[simp] lemma gpowers_mul_hom_symm_apply [comm_group G] (f : multiplicative ℤ →* G) : (gpowers_mul_hom G).symm f = f (multiplicative.of_add 1) := rfl @[simp] lemma multiples_add_hom_apply [add_comm_monoid A] (x : A) (n : ℕ) : multiples_add_hom A x n = n • x := rfl @[simp] lemma multiples_add_hom_symm_apply [add_comm_monoid A] (f : ℕ →+ A) : (multiples_add_hom A).symm f = f 1 := rfl @[simp] lemma gmultiples_add_hom_apply [add_comm_group A] (x : A) (n : ℤ) : gmultiples_add_hom A x n = n • x := rfl @[simp] lemma gmultiples_add_hom_symm_apply [add_comm_group A] (f : ℤ →+ A) : (gmultiples_add_hom A).symm f = f 1 := rfl /-! ### Commutativity (again) Facts about `semiconj_by` and `commute` that require `gpow` or `gsmul`, or the fact that integer multiplication equals semiring multiplication. -/ namespace semiconj_by section variables [semiring R] {a x y : R} @[simp] lemma cast_nat_mul_right (h : semiconj_by a x y) (n : ℕ) : semiconj_by a ((n : R) * x) (n * y) := semiconj_by.mul_right (nat.commute_cast _ _) h @[simp] lemma cast_nat_mul_left (h : semiconj_by a x y) (n : ℕ) : semiconj_by ((n : R) * a) x y := semiconj_by.mul_left (nat.cast_commute _ _) h @[simp] lemma cast_nat_mul_cast_nat_mul (h : semiconj_by a x y) (m n : ℕ) : semiconj_by ((m : R) * a) (n * x) (n * y) := (h.cast_nat_mul_left m).cast_nat_mul_right n end variables [monoid M] [group G] [ring R] @[simp] lemma units_gpow_right {a : M} {x y : units M} (h : semiconj_by a x y) : ∀ m : ℤ, semiconj_by a (↑(x^m)) (↑(y^m)) | (n : ℕ) := by simp only [gpow_coe_nat, units.coe_pow, h, pow_right] | -[1+n] := by simp only [gpow_neg_succ_of_nat, units.coe_pow, units_inv_right, h, pow_right] variables {a b x y x' y' : R} @[simp] lemma cast_int_mul_right (h : semiconj_by a x y) (m : ℤ) : semiconj_by a ((m : ℤ) * x) (m * y) := semiconj_by.mul_right (int.commute_cast _ _) h @[simp] lemma cast_int_mul_left (h : semiconj_by a x y) (m : ℤ) : semiconj_by ((m : R) * a) x y := semiconj_by.mul_left (int.cast_commute _ _) h @[simp] lemma cast_int_mul_cast_int_mul (h : semiconj_by a x y) (m n : ℤ) : semiconj_by ((m : R) * a) (n * x) (n * y) := (h.cast_int_mul_left m).cast_int_mul_right n end semiconj_by namespace commute section variables [semiring R] {a b : R} @[simp] theorem cast_nat_mul_right (h : commute a b) (n : ℕ) : commute a ((n : R) * b) := h.cast_nat_mul_right n @[simp] theorem cast_nat_mul_left (h : commute a b) (n : ℕ) : commute ((n : R) * a) b := h.cast_nat_mul_left n @[simp] theorem cast_nat_mul_cast_nat_mul (h : commute a b) (m n : ℕ) : commute ((m : R) * a) (n * b) := h.cast_nat_mul_cast_nat_mul m n @[simp] theorem self_cast_nat_mul (n : ℕ) : commute a (n * a) := (commute.refl a).cast_nat_mul_right n @[simp] theorem cast_nat_mul_self (n : ℕ) : commute ((n : R) * a) a := (commute.refl a).cast_nat_mul_left n @[simp] theorem self_cast_nat_mul_cast_nat_mul (m n : ℕ) : commute ((m : R) * a) (n * a) := (commute.refl a).cast_nat_mul_cast_nat_mul m n end variables [monoid M] [group G] [ring R] @[simp] lemma units_gpow_right {a : M} {u : units M} (h : commute a u) (m : ℤ) : commute a (↑(u^m)) := h.units_gpow_right m @[simp] lemma units_gpow_left {u : units M} {a : M} (h : commute ↑u a) (m : ℤ) : commute (↑(u^m)) a := (h.symm.units_gpow_right m).symm variables {a b : R} @[simp] lemma cast_int_mul_right (h : commute a b) (m : ℤ) : commute a (m * b) := h.cast_int_mul_right m @[simp] lemma cast_int_mul_left (h : commute a b) (m : ℤ) : commute ((m : R) * a) b := h.cast_int_mul_left m lemma cast_int_mul_cast_int_mul (h : commute a b) (m n : ℤ) : commute ((m : R) * a) (n * b) := h.cast_int_mul_cast_int_mul m n variables (a) (m n : ℤ) @[simp] lemma cast_int_left : commute (m : R) a := by { rw [← mul_one (m : R)], exact (one_left a).cast_int_mul_left m } @[simp] lemma cast_int_right : commute a m := by { rw [← mul_one (m : R)], exact (one_right a).cast_int_mul_right m } @[simp] theorem self_cast_int_mul : commute a (n * a) := (commute.refl a).cast_int_mul_right n @[simp] theorem cast_int_mul_self : commute ((n : R) * a) a := (commute.refl a).cast_int_mul_left n theorem self_cast_int_mul_cast_int_mul : commute ((m : R) * a) (n * a) := (commute.refl a).cast_int_mul_cast_int_mul m n end commute section multiplicative open multiplicative @[simp] lemma nat.to_add_pow (a : multiplicative ℕ) (b : ℕ) : to_add (a ^ b) = to_add a * b := begin induction b with b ih, { erw [pow_zero, to_add_one, mul_zero] }, { simp [*, pow_succ, add_comm, nat.mul_succ] } end @[simp] lemma nat.of_add_mul (a b : ℕ) : of_add (a * b) = of_add a ^ b := (nat.to_add_pow _ _).symm @[simp] lemma int.to_add_pow (a : multiplicative ℤ) (b : ℕ) : to_add (a ^ b) = to_add a * b := by induction b; simp [*, mul_add, pow_succ, add_comm] @[simp] lemma int.to_add_gpow (a : multiplicative ℤ) (b : ℤ) : to_add (a ^ b) = to_add a * b := int.induction_on b (by simp) (by simp [gpow_add, mul_add] {contextual := tt}) (by simp [gpow_add, mul_add, sub_eq_add_neg, -int.add_neg_one] {contextual := tt}) @[simp] lemma int.of_add_mul (a b : ℤ) : of_add (a * b) = of_add a ^ b := (int.to_add_gpow _ _).symm end multiplicative namespace units variables [monoid M] lemma conj_pow (u : units M) (x : M) (n : ℕ) : (↑u * x * ↑(u⁻¹))^n = u * x^n * ↑(u⁻¹) := (divp_eq_iff_mul_eq.2 ((u.mk_semiconj_by x).pow_right n).eq.symm).symm lemma conj_pow' (u : units M) (x : M) (n : ℕ) : (↑(u⁻¹) * x * u)^n = ↑(u⁻¹) * x^n * u:= (u⁻¹).conj_pow x n end units namespace opposite variables [monoid M] /-- Moving to the opposite monoid commutes with taking powers. -/ @[simp] lemma op_pow (x : M) (n : ℕ) : op (x ^ n) = (op x) ^ n := begin induction n with n h, { simp }, { rw [pow_succ', op_mul, h, pow_succ] } end @[simp] lemma unop_pow (x : Mᵒᵖ) (n : ℕ) : unop (x ^ n) = (unop x) ^ n := begin induction n with n h, { simp }, { rw [pow_succ', unop_mul, h, pow_succ] } end end opposite
1d5cd1dd323b012cff94d3d517c2897dd0626cce
b9a81ebb9de684db509231c4469a7d2c88915808
/src/super/cdcl.lean
1981d26da693a071df960cffa612a919fce214b5
[]
no_license
leanprover/super
3dd81ce8d9ac3cba20bce55e84833fadb2f5716e
47b107b4cec8f3b41d72daba9cbda2f9d54025de
refs/heads/master
1,678,482,996,979
1,676,526,367,000
1,676,526,367,000
92,215,900
12
6
null
1,513,327,539,000
1,495,570,640,000
Lean
UTF-8
Lean
false
false
862
lean
/- Copyright (c) 2017 Gabriel Ebner. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Gabriel Ebner -/ import .clause .clausifier .cdcl_solver open tactic expr monad super meta def cdcl_t (th_solver : tactic unit) : tactic unit := do as_refutation, local_false ← target, clauses ← clauses_of_context, clauses ← get_clauses_classical clauses, clauses.mmap' (λc, do c_pp ← pp c, clause.validate c <|> fail c_pp), res ← cdcl.solve (cdcl.theory_solver_of_tactic th_solver) local_false clauses, match res with | (cdcl.result.unsat proof) := exact proof | (cdcl.result.sat interp) := let interp' := do e ← interp.to_list, [if e.2 = tt then e.1 else `(not %%e.1)] in do pp_interp ← pp interp', fail (to_fmt "satisfying assignment: " ++ pp_interp) end meta def cdcl : tactic unit := cdcl_t skip
256499b3ffc2d0305327ef2ba6955da5e78949c3
f3849be5d845a1cb97680f0bbbe03b85518312f0
/library/tools/super/superposition.lean
4c4280a31bc63166313b66ffb59a8d66cb28d418
[ "Apache-2.0" ]
permissive
bjoeris/lean
0ed95125d762b17bfcb54dad1f9721f953f92eeb
4e496b78d5e73545fa4f9a807155113d8e6b0561
refs/heads/master
1,611,251,218,281
1,495,337,658,000
1,495,337,658,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,862
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 .prover_state .utils open tactic monad expr namespace super def position := list ℕ meta def get_rwr_positions : expr → list position | (app a b) := [[]] ++ do arg ← list.zip_with_index (get_app_args (app a b)), pos ← get_rwr_positions arg.1, [arg.2 :: pos] | (var _) := [] | e := [[]] meta def get_position : expr → position → expr | (app a b) (p::ps) := match list.nth (get_app_args (app a b)) p with | some arg := get_position arg ps | none := (app a b) end | e _ := e meta def replace_position (v : expr) : expr → position → expr | (app a b) (p::ps) := let args := get_app_args (app a b) in match args.nth p with | some arg := app_of_list a.get_app_fn $ args.update_nth p $ replace_position arg ps | none := app a b end | e [] := v | e _ := e variable gt : expr → expr → bool variables (c1 c2 : clause) variables (ac1 ac2 : derived_clause) variables (i1 i2 : nat) variable pos : list ℕ variable ltr : bool variable lt_in_termorder : bool variable congr_ax : name lemma {u v w} sup_ltr (F : Sort u) (A : Sort v) (a1 a2) (f : A → Sort w) : (f a1 → F) → f a2 → a1 = a2 → F := take hnfa1 hfa2 he, hnfa1 (@eq.rec A a2 f hfa2 a1 he.symm) lemma {u v w} sup_rtl (F : Sort u) (A : Sort v) (a1 a2) (f : A → Sort w) : (f a1 → F) → f a2 → a2 = a1 → F := take hnfa1 hfa2 heq, hnfa1 (@eq.rec A a2 f hfa2 a1 heq) meta def is_eq_dir (e : expr) (ltr : bool) : option (expr × expr) := match is_eq e with | some (lhs, rhs) := if ltr then some (lhs, rhs) else some (rhs, lhs) | none := none end meta def try_sup : tactic clause := do guard $ (c1.get_lit i1).is_pos, qf1 ← c1.open_metan c1.num_quants, qf2 ← c2.open_metan c2.num_quants, (rwr_from, rwr_to) ← (is_eq_dir (qf1.1.get_lit i1).formula ltr).to_monad, atom ← return (qf2.1.get_lit i2).formula, eq_type ← infer_type rwr_from, atom_at_pos ← return $ get_position atom pos, atom_at_pos_type ← infer_type atom_at_pos, unify eq_type atom_at_pos_type, unify rwr_from atom_at_pos transparency.none, rwr_from' ← instantiate_mvars atom_at_pos, rwr_to' ← instantiate_mvars rwr_to, if lt_in_termorder then guard (gt rwr_from' rwr_to') else guard (¬gt rwr_to' rwr_from'), rwr_ctx_varn ← mk_fresh_name, abs_rwr_ctx ← return $ lam rwr_ctx_varn binder_info.default eq_type (if (qf2.1.get_lit i2).is_neg then replace_position (mk_var 0) atom pos else imp (replace_position (mk_var 0) atom pos) c2.local_false), lf_univ ← infer_univ c1.local_false, univ ← infer_univ eq_type, atom_univ ← infer_univ atom, op1 ← qf1.1.open_constn i1, op2 ← qf2.1.open_constn c2.num_lits, hi2 ← (op2.2.nth i2).to_monad, new_atom ← whnf_no_delta $ app abs_rwr_ctx rwr_to', new_hi2 ← return $ local_const hi2.local_uniq_name `H binder_info.default new_atom, new_fin_prf ← return $ app_of_list (const congr_ax [lf_univ, univ, atom_univ]) [c1.local_false, eq_type, rwr_from, rwr_to, abs_rwr_ctx, (op2.1.close_const hi2).proof, new_hi2], clause.meta_closure (qf1.2 ++ qf2.2) $ (op1.1.inst new_fin_prf).close_constn (op1.2 ++ op2.2.update_nth i2 new_hi2) meta def rwr_positions (c : clause) (i : nat) : list (list ℕ) := get_rwr_positions (c.get_lit i).formula meta def try_add_sup : prover unit := (do c' ← try_sup gt ac1.c ac2.c i1 i2 pos ltr ff congr_ax, inf_score 2 [ac1.sc, ac2.sc] >>= mk_derived c' >>= add_inferred) <|> return () meta def superposition_back_inf : inference := take given, do active ← get_active, sequence' $ do given_i ← given.selected, guard (given.c.get_lit given_i).is_pos, option.to_monad $ is_eq (given.c.get_lit given_i).formula, other ← rb_map.values active, guard $ ¬given.sc.in_sos ∨ ¬other.sc.in_sos, other_i ← other.selected, pos ← rwr_positions other.c other_i, -- FIXME(gabriel): ``sup_ltr fails to resolve at runtime [do try_add_sup gt given other given_i other_i pos tt ``super.sup_ltr, try_add_sup gt given other given_i other_i pos ff ``super.sup_rtl] meta def superposition_fwd_inf : inference := take given, do active ← get_active, sequence' $ do given_i ← given.selected, other ← rb_map.values active, guard $ ¬given.sc.in_sos ∨ ¬other.sc.in_sos, other_i ← other.selected, guard (other.c.get_lit other_i).is_pos, option.to_monad $ is_eq (other.c.get_lit other_i).formula, pos ← rwr_positions given.c given_i, [do try_add_sup gt other given other_i given_i pos tt ``super.sup_ltr, try_add_sup gt other given other_i given_i pos ff ``super.sup_rtl] @[super.inf] meta def superposition_inf : inf_decl := inf_decl.mk 40 $ take given, do gt ← get_term_order, superposition_fwd_inf gt given, superposition_back_inf gt given end super
c03bf82985ddd7484b853eae25fd3a0480c43b45
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/linear_algebra/eigenspace.lean
fc117cb26b704d0087c99a587ea39b900bc6590e
[ "Apache-2.0" ]
permissive
lacker/mathlib
f2439c743c4f8eb413ec589430c82d0f73b2d539
ddf7563ac69d42cfa4a1bfe41db1fed521bd795f
refs/heads/master
1,671,948,326,773
1,601,479,268,000
1,601,479,268,000
298,686,743
0
0
Apache-2.0
1,601,070,794,000
1,601,070,794,000
null
UTF-8
Lean
false
false
24,106
lean
/- Copyright (c) 2020 Alexander Bentkamp. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Alexander Bentkamp. -/ import field_theory.algebraic_closure import linear_algebra.finsupp import linear_algebra.matrix /-! # Eigenvectors and eigenvalues This file defines eigenspaces, eigenvalues, and eigenvalues, as well as their generalized counterparts. We follow Axler's approach [axler2015] because it allows us to derive many properties without choosing a basis and without using matrices. An eigenspace of a linear map `f` for a scalar `μ` is the kernel of the map `(f - μ • id)`. The nonzero elements of an eigenspace are eigenvectors `x`. They have the property `f x = μ • x`. If there are eigenvectors for a scalar `μ`, the scalar `μ` is called an eigenvalue. There is no consensus in the literature whether `0` is an eigenvector. Our definition of `has_eigenvector` permits only nonzero vectors. For an eigenvector `x` that may also be `0`, we write `x ∈ f.eigenspace μ`. A generalized eigenspace of a linear map `f` for a natural number `k` and a scalar `μ` is the kernel of the map `(f - μ • id) ^ k`. The nonzero elements of a generalized eigenspace are generalized eigenvectors `x`. If there are generalized eigenvectors for a natural number `k` and a scalar `μ`, the scalar `μ` is called a generalized eigenvalue. ## References * [Sheldon Axler, *Linear Algebra Done Right*][axler2015] * https://en.wikipedia.org/wiki/Eigenvalues_and_eigenvectors ## Tags eigenspace, eigenvector, eigenvalue, eigen -/ universes u v w namespace module namespace End open vector_space principal_ideal_ring polynomial finite_dimensional variables {K R : Type v} {V M : Type w} [comm_ring R] [add_comm_group M] [module R M] [field K] [add_comm_group V] [vector_space K V] /-- The submodule `eigenspace f μ` for a linear map `f` and a scalar `μ` consists of all vectors `x` such that `f x = μ • x`. (Def 5.36 of [axler2015])-/ def eigenspace (f : End R M) (μ : R) : submodule R M := (f - algebra_map R (End R M) μ).ker /-- A nonzero element of an eigenspace is an eigenvector. (Def 5.7 of [axler2015]) -/ def has_eigenvector (f : End R M) (μ : R) (x : M) : Prop := x ≠ 0 ∧ x ∈ eigenspace f μ /-- A scalar `μ` is an eigenvalue for a linear map `f` if there are nonzero vectors `x` such that `f x = μ • x`. (Def 5.5 of [axler2015]) -/ def has_eigenvalue (f : End R M) (a : R) : Prop := eigenspace f a ≠ ⊥ lemma mem_eigenspace_iff {f : End R M} {μ : R} {x : M} : x ∈ eigenspace f μ ↔ f x = μ • x := by rw [eigenspace, linear_map.mem_ker, linear_map.sub_apply, algebra_map_End_apply, sub_eq_zero] lemma eigenspace_div (f : End K V) (a b : K) (hb : b ≠ 0) : eigenspace f (a / b) = (b • f - algebra_map K (End K V) a).ker := calc eigenspace f (a / b) = eigenspace f (b⁻¹ * a) : by { dsimp [(/)], rw mul_comm } ... = (f - (b⁻¹ * a) • linear_map.id).ker : rfl ... = (f - b⁻¹ • a • linear_map.id).ker : by rw smul_smul ... = (f - b⁻¹ • algebra_map K (End K V) a).ker : rfl ... = (b • (f - b⁻¹ • algebra_map K (End K V) a)).ker : by rw linear_map.ker_smul _ b hb ... = (b • f - algebra_map K (End K V) a).ker : by rw [smul_sub, smul_inv_smul' hb] lemma eigenspace_aeval_polynomial_degree_1 (f : End K V) (q : polynomial K) (hq : degree q = 1) : eigenspace f (- q.coeff 0 / q.leading_coeff) = (aeval f q).ker := calc eigenspace f (- q.coeff 0 / q.leading_coeff) = (q.leading_coeff • f - algebra_map K (End K V) (- q.coeff 0)).ker : by { rw eigenspace_div, intro h, rw leading_coeff_eq_zero_iff_deg_eq_bot.1 h at hq, cases hq } ... = (aeval f (C q.leading_coeff * X + C (q.coeff 0))).ker : by { rw [C_mul', aeval_def], simpa [algebra_map, algebra.to_ring_hom], } ... = (aeval f q).ker : by { congr, apply (eq_X_add_C_of_degree_eq_one hq).symm } lemma ker_aeval_ring_hom'_unit_polynomial (f : End K V) (c : units (polynomial K)) : (aeval f (c : polynomial K)).ker = ⊥ := begin rw polynomial.eq_C_of_degree_eq_zero (degree_coe_units c), simp only [aeval_def, eval₂_C], apply ker_algebra_map_End, apply coeff_coe_units_zero_ne_zero c end theorem aeval_apply_of_has_eigenvector {f : End K V} {p : polynomial K} {μ : K} {x : V} (h : f.has_eigenvector μ x) : aeval f p x = (p.eval μ) • x := begin apply p.induction_on, { intro a, simp [module.algebra_map_End_apply] }, { intros p q hp hq, simp [hp, hq, add_smul] }, { intros n a hna, rw [mul_comm, pow_succ, mul_assoc, alg_hom.map_mul, linear_map.mul_app, mul_comm, hna], simp [algebra_map_End_apply, mem_eigenspace_iff.1 h.2, smul_smul, mul_comm] } end section minimal_polynomial variables [finite_dimensional K V] (f : End K V) protected theorem is_integral : is_integral K f := is_integral_of_noetherian (by apply_instance) f variables {f} {μ : K} theorem is_root_of_has_eigenvalue (h : f.has_eigenvalue μ) : (minimal_polynomial f.is_integral).is_root μ := begin rcases (submodule.ne_bot_iff _).1 h with ⟨w, ⟨H, ne0⟩⟩, refine or.resolve_right (smul_eq_zero.1 _) ne0, simp [← aeval_apply_of_has_eigenvector ⟨ne0, H⟩, minimal_polynomial.aeval f.is_integral], end theorem has_eigenvalue_of_is_root (h : (minimal_polynomial f.is_integral).is_root μ) : f.has_eigenvalue μ := begin cases dvd_iff_is_root.2 h with p hp, rw [has_eigenvalue, eigenspace], intro con, cases (linear_map.is_unit_iff _).2 con with u hu, have p_ne_0 : p ≠ 0, { intro con, apply minimal_polynomial.ne_zero f.is_integral, rw [hp, con, mul_zero] }, have h_deg := minimal_polynomial.degree_le_of_ne_zero f.is_integral p_ne_0 _, { rw [hp, degree_mul, degree_X_sub_C, polynomial.degree_eq_nat_degree p_ne_0] at h_deg, norm_cast at h_deg, linarith, }, { have h_aeval := minimal_polynomial.aeval f.is_integral, revert h_aeval, simp [hp, ← hu] }, end theorem has_eigenvalue_iff_is_root : f.has_eigenvalue μ ↔ (minimal_polynomial f.is_integral).is_root μ := ⟨is_root_of_has_eigenvalue, has_eigenvalue_of_is_root⟩ end minimal_polynomial /-- Every linear operator on a vector space over an algebraically closed field has an eigenvalue. (Lemma 5.21 of [axler2015]) -/ lemma exists_eigenvalue [is_alg_closed K] [finite_dimensional K V] [nontrivial V] (f : End K V) : ∃ (c : K), f.has_eigenvalue c := begin classical, -- Choose a nonzero vector `v`. obtain ⟨v, hv⟩ : ∃ v : V, v ≠ 0 := exists_ne (0 : V), -- The infinitely many vectors v, f v, f (f v), ... cannot be linearly independent -- because the vector space is finite dimensional. have h_lin_dep : ¬ linear_independent K (λ n : ℕ, (f ^ n) v), { apply not_linear_independent_of_infinite, }, -- Therefore, there must be a nonzero polynomial `p` such that `p(f) v = 0`. obtain ⟨p, ⟨h_mon, h_eval_p⟩⟩ := f.is_integral, have h_eval_p_not_unit : aeval f p ∉ is_unit.submonoid (End K V), { rw [is_unit.mem_submonoid_iff, linear_map.is_unit_iff, linear_map.ker_eq_bot'], intro h, apply hv (h v _), rw [h_eval_p, linear_map.zero_apply] }, -- Hence, there must be a factor `q` of `p` such that `q(f)` is not invertible. obtain ⟨q, hq_factor, hq_nonunit⟩ : ∃ q, q ∈ factors p ∧ ¬ is_unit (aeval f q), { simp only [←not_imp, (is_unit.mem_submonoid_iff _).symm], apply not_forall.1 (λ h, h_eval_p_not_unit (ring_hom_mem_submonoid_of_factors_subset_of_units_subset (eval₂_ring_hom' (algebra_map _ _) algebra.commutes f) (is_unit.submonoid (End K V)) p h_mon.ne_zero h _)), simp only [is_unit.mem_submonoid_iff, linear_map.is_unit_iff], apply ker_aeval_ring_hom'_unit_polynomial }, -- Since the field is algebraically closed, `q` has degree 1. have h_deg_q : q.degree = 1 := is_alg_closed.degree_eq_one_of_irreducible _ (ne_zero_of_mem_factors h_mon.ne_zero hq_factor) ((factors_spec p h_mon.ne_zero).1 q hq_factor), -- Then the kernel of `q(f)` is an eigenspace. have h_eigenspace: eigenspace f (-q.coeff 0 / q.leading_coeff) = (aeval f q).ker, from eigenspace_aeval_polynomial_degree_1 f q h_deg_q, -- Since `q(f)` is not invertible, the kernel is not `⊥`, and thus there exists an eigenvalue. show ∃ (c : K), f.has_eigenvalue c, { use -q.coeff 0 / q.leading_coeff, rw [has_eigenvalue, h_eigenspace], intro h_eval_ker, exact hq_nonunit ((linear_map.is_unit_iff (aeval f q)).2 h_eval_ker) } end /-- Eigenvectors corresponding to distinct eigenvalues of a linear operator are linearly independent. (Lemma 5.10 of [axler2015]) We use the eigenvalues as indexing set to ensure that there is only one eigenvector for each eigenvalue in the image of `xs`. -/ lemma eigenvectors_linear_independent (f : End K V) (μs : set K) (xs : μs → V) (h_eigenvec : ∀ μ : μs, f.has_eigenvector μ (xs μ)) : linear_independent K xs := begin classical, -- We need to show that if a linear combination `l` of the eigenvectors `xs` is `0`, then all -- its coefficients are zero. suffices : ∀ l, finsupp.total μs V K xs l = 0 → l = 0, { rw linear_independent_iff, apply this }, intros l hl, -- We apply induction on the finite set of eigenvalues whose eigenvectors have nonzero -- coefficients, i.e. on the support of `l`. induction h_l_support : l.support using finset.induction with μ₀ l_support' hμ₀ ih generalizing l, -- If the support is empty, all coefficients are zero and we are done. { exact finsupp.support_eq_empty.1 h_l_support }, -- Now assume that the support of `l` contains at least one eigenvalue `μ₀`. We define a new -- linear combination `l'` to apply the induction hypothesis on later. The linear combination `l'` -- is derived from `l` by multiplying the coefficient of the eigenvector with eigenvalue `μ` -- by `μ - μ₀`. -- To get started, we define `l'` as a function `l'_f : μs → K` with potentially infinite support. { let l'_f : μs → K := (λ μ : μs, (↑μ - ↑μ₀) * l μ), -- The support of `l'_f` is the support of `l` without `μ₀`. have h_l_support' : ∀ (μ : μs), μ ∈ l_support' ↔ l'_f μ ≠ 0 , { intro μ, suffices : μ ∈ l_support' → μ ≠ μ₀, { simp [l'_f, ← finsupp.not_mem_support_iff, h_l_support, sub_eq_zero, ←subtype.ext_iff], tauto }, rintro hμ rfl, contradiction }, -- Now we can define `l'_f` as an actual linear combination `l'` because we know that the -- support is finite. let l' : μs →₀ K := { to_fun := l'_f, support := l_support', mem_support_to_fun := h_l_support' }, -- The linear combination `l'` over `xs` adds up to `0`. have total_l' : finsupp.total μs V K xs l' = 0, { let g := f - algebra_map K (End K V) μ₀, have h_gμ₀: g (l μ₀ • xs μ₀) = 0, by rw [linear_map.map_smul, linear_map.sub_apply, mem_eigenspace_iff.1 (h_eigenvec _).2, algebra_map_End_apply, sub_self, smul_zero], have h_useless_filter : finset.filter (λ (a : μs), l'_f a ≠ 0) l_support' = l_support', { rw finset.filter_congr _, { apply finset.filter_true }, { apply_instance }, exact λ μ hμ, (iff_true _).mpr ((h_l_support' μ).1 hμ) }, have bodies_eq : ∀ (μ : μs), l'_f μ • xs μ = g (l μ • xs μ), { intro μ, dsimp only [g, l'_f], rw [linear_map.map_smul, linear_map.sub_apply, mem_eigenspace_iff.1 (h_eigenvec _).2, algebra_map_End_apply, ←sub_smul, smul_smul, mul_comm] }, rw [←linear_map.map_zero g, ←hl, finsupp.total_apply, finsupp.total_apply, finsupp.sum, finsupp.sum, linear_map.map_sum, h_l_support, finset.sum_insert hμ₀, h_gμ₀, zero_add], refine finset.sum_congr rfl (λ μ _, _), apply bodies_eq }, -- Therefore, by the induction hypothesis, all coefficients in `l'` are zero. have l'_eq_0 : l' = 0 := ih l' total_l' rfl, -- By the defintion of `l'`, this means that `(μ - μ₀) * l μ = 0` for all `μ`. have h_mul_eq_0 : ∀ μ : μs, (↑μ - ↑μ₀) * l μ = 0, { intro μ, calc (↑μ - ↑μ₀) * l μ = l' μ : rfl ... = 0 : by { rw [l'_eq_0], refl } }, -- Thus, the coefficients in `l` for all `μ ≠ μ₀` are `0`. have h_lμ_eq_0 : ∀ μ : μs, μ ≠ μ₀ → l μ = 0, { intros μ hμ, apply or_iff_not_imp_left.1 (mul_eq_zero.1 (h_mul_eq_0 μ)), rwa [sub_eq_zero, ←subtype.ext_iff] }, -- So if we sum over all these coefficients, we obtain `0`. have h_sum_l_support'_eq_0 : finset.sum l_support' (λ (μ : ↥μs), l μ • xs μ) = 0, { rw ←finset.sum_const_zero, apply finset.sum_congr rfl, intros μ hμ, rw h_lμ_eq_0, apply zero_smul, intro h, rw h at hμ, contradiction }, -- The only potentially nonzero coefficient in `l` is the one corresponding to `μ₀`. But since -- the overall sum is `0` by assumption, this coefficient must also be `0`. have : l μ₀ = 0, { rw [finsupp.total_apply, finsupp.sum, h_l_support, finset.sum_insert hμ₀, h_sum_l_support'_eq_0, add_zero] at hl, by_contra h, exact (h_eigenvec μ₀).1 ((smul_eq_zero.1 hl).resolve_left h) }, -- Thus, all coefficients in `l` are `0`. show l = 0, { ext μ, by_cases h_cases : μ = μ₀, { rw h_cases, assumption }, exact h_lμ_eq_0 μ h_cases } } end /-- The generalized eigenspace for a linear map `f`, a scalar `μ`, and an exponent `k ∈ ℕ` is the kernel of `(f - μ • id) ^ k`. (Def 8.10 of [axler2015])-/ def generalized_eigenspace (f : End R M) (μ : R) (k : ℕ) : submodule R M := ((f - algebra_map R (End R M) μ) ^ k).ker /-- A nonzero element of a generalized eigenspace is a generalized eigenvector. (Def 8.9 of [axler2015])-/ def has_generalized_eigenvector (f : End R M) (μ : R) (k : ℕ) (x : M) : Prop := x ≠ 0 ∧ x ∈ generalized_eigenspace f μ k /-- A scalar `μ` is a generalized eigenvalue for a linear map `f` and an exponent `k ∈ ℕ` if there are generalized eigenvectors for `f`, `k`, and `μ`. -/ def has_generalized_eigenvalue (f : End R M) (μ : R) (k : ℕ) : Prop := generalized_eigenspace f μ k ≠ ⊥ /-- The generalized eigenrange for a linear map `f`, a scalar `μ`, and an exponent `k ∈ ℕ` is the range of `(f - μ • id) ^ k`. -/ def generalized_eigenrange (f : End R M) (μ : R) (k : ℕ) : submodule R M := ((f - algebra_map R (End R M) μ) ^ k).range /-- The exponent of a generalized eigenvalue is never 0. -/ lemma exp_ne_zero_of_has_generalized_eigenvalue {f : End R M} {μ : R} {k : ℕ} (h : f.has_generalized_eigenvalue μ k) : k ≠ 0 := begin rintro rfl, exact h linear_map.ker_id end /-- A generalized eigenspace for some exponent `k` is contained in the generalized eigenspace for exponents larger than `k`. -/ lemma generalized_eigenspace_mono {f : End K V} {μ : K} {k : ℕ} {m : ℕ} (hm : k ≤ m) : f.generalized_eigenspace μ k ≤ f.generalized_eigenspace μ m := begin simp only [generalized_eigenspace, ←pow_sub_mul_pow _ hm], exact linear_map.ker_le_ker_comp ((f - algebra_map K (End K V) μ) ^ k) ((f - algebra_map K (End K V) μ) ^ (m - k)) end /-- A generalized eigenvalue for some exponent `k` is also a generalized eigenvalue for exponents larger than `k`. -/ lemma has_generalized_eigenvalue_of_has_generalized_eigenvalue_of_le {f : End K V} {μ : K} {k : ℕ} {m : ℕ} (hm : k ≤ m) (hk : f.has_generalized_eigenvalue μ k) : f.has_generalized_eigenvalue μ m := begin unfold has_generalized_eigenvalue at *, contrapose! hk, rw [←le_bot_iff, ←hk], exact generalized_eigenspace_mono hm end /-- The eigenspace is a subspace of the generalized eigenspace. -/ lemma eigenspace_le_generalized_eigenspace {f : End K V} {μ : K} {k : ℕ} (hk : 0 < k) : f.eigenspace μ ≤ f.generalized_eigenspace μ k := generalized_eigenspace_mono (nat.succ_le_of_lt hk) /-- All eigenvalues are generalized eigenvalues. -/ lemma has_generalized_eigenvalue_of_has_eigenvalue {f : End K V} {μ : K} {k : ℕ} (hk : 0 < k) (hμ : f.has_eigenvalue μ) : f.has_generalized_eigenvalue μ k := begin apply has_generalized_eigenvalue_of_has_generalized_eigenvalue_of_le hk, rwa [has_generalized_eigenvalue, generalized_eigenspace, pow_one] end /-- Every generalized eigenvector is a generalized eigenvector for exponent `findim K V`. (Lemma 8.11 of [axler2015]) -/ lemma generalized_eigenspace_le_generalized_eigenspace_findim [finite_dimensional K V] (f : End K V) (μ : K) (k : ℕ) : f.generalized_eigenspace μ k ≤ f.generalized_eigenspace μ (findim K V) := ker_pow_le_ker_pow_findim _ _ /-- Generalized eigenspaces for exponents at least `findim K V` are equal to each other. -/ lemma generalized_eigenspace_eq_generalized_eigenspace_findim_of_le [finite_dimensional K V] (f : End K V) (μ : K) {k : ℕ} (hk : findim K V ≤ k) : f.generalized_eigenspace μ k = f.generalized_eigenspace μ (findim K V) := ker_pow_eq_ker_pow_findim_of_le hk /-- If `f` maps a subspace `p` into itself, then the generalized eigenspace of the restriction of `f` to `p` is the part of the generalized eigenspace of `f` that lies in `p`. -/ lemma generalized_eigenspace_restrict (f : End K V) (p : submodule K V) (k : ℕ) (μ : K) (hfp : ∀ (x : V), x ∈ p → f x ∈ p) : generalized_eigenspace (linear_map.restrict f hfp) μ k = submodule.comap p.subtype (f.generalized_eigenspace μ k) := begin rw [generalized_eigenspace, generalized_eigenspace, ←linear_map.ker_comp], induction k with k ih, { rw [pow_zero,pow_zero], convert linear_map.ker_id, apply submodule.ker_subtype }, { erw [pow_succ', pow_succ', linear_map.ker_comp, ih, ←linear_map.ker_comp, linear_map.comp_assoc], } end /-- Generalized eigenrange and generalized eigenspace for exponent `findim K V` are disjoint. -/ lemma generalized_eigenvec_disjoint_range_ker [finite_dimensional K V] (f : End K V) (μ : K) : disjoint (f.generalized_eigenrange μ (findim K V)) (f.generalized_eigenspace μ (findim K V)) := begin have h := calc submodule.comap ((f - algebra_map _ _ μ) ^ findim K V) (f.generalized_eigenspace μ (findim K V)) = ((f - algebra_map _ _ μ) ^ findim K V * (f - algebra_map K (End K V) μ) ^ findim K V).ker : by { rw [generalized_eigenspace, ←linear_map.ker_comp], refl } ... = f.generalized_eigenspace μ (findim K V + findim K V) : by { rw ←pow_add, refl } ... = f.generalized_eigenspace μ (findim K V) : by { rw generalized_eigenspace_eq_generalized_eigenspace_findim_of_le, linarith }, rw [disjoint, generalized_eigenrange, linear_map.range, submodule.map_inf_eq_map_inf_comap, top_inf_eq, h], apply submodule.map_comap_le end /-- The generalized eigenspace of an eigenvalue has positive dimension for positive exponents. -/ lemma pos_findim_generalized_eigenspace_of_has_eigenvalue [finite_dimensional K V] {f : End K V} {k : ℕ} {μ : K} (hx : f.has_eigenvalue μ) (hk : 0 < k): 0 < findim K (f.generalized_eigenspace μ k) := calc 0 = findim K (⊥ : submodule K V) : by rw findim_bot ... < findim K (f.eigenspace μ) : submodule.findim_lt_findim_of_lt (bot_lt_iff_ne_bot.2 hx) ... ≤ findim K (f.generalized_eigenspace μ k) : submodule.findim_mono (generalized_eigenspace_mono (nat.succ_le_of_lt hk)) /-- A linear map maps a generalized eigenrange into itself. -/ lemma map_generalized_eigenrange_le {f : End K V} {μ : K} {n : ℕ} : submodule.map f (f.generalized_eigenrange μ n) ≤ f.generalized_eigenrange μ n := calc submodule.map f (f.generalized_eigenrange μ n) = (f * ((f - algebra_map _ _ μ) ^ n)).range : (linear_map.range_comp _ _).symm ... = (((f - algebra_map _ _ μ) ^ n) * f).range : by rw algebra.mul_sub_algebra_map_pow_commutes ... = submodule.map ((f - algebra_map _ _ μ) ^ n) f.range : linear_map.range_comp _ _ ... ≤ f.generalized_eigenrange μ n : linear_map.map_le_range /-- The generalized eigenvectors span the entire vector space (Lemma 8.21 of [axler2015]). -/ lemma supr_generalized_eigenspace_eq_top [is_alg_closed K] [finite_dimensional K V] (f : End K V) : (⨆ (μ : K) (k : ℕ), f.generalized_eigenspace μ k) = ⊤ := begin tactic.unfreeze_local_instances, -- We prove the claim by strong induction on the dimension of the vector space. induction h_dim : findim K V using nat.strong_induction_on with n ih generalizing V, cases n, -- If the vector space is 0-dimensional, the result is trivial. { rw ←top_le_iff, simp only [findim_eq_zero.1 (eq.trans findim_top h_dim), bot_le] }, -- Otherwise the vector space is nontrivial. { haveI : nontrivial V := findim_pos_iff.1 (by { rw h_dim, apply nat.zero_lt_succ }), -- Hence, `f` has an eigenvalue `μ₀`. obtain ⟨μ₀, hμ₀⟩ : ∃ μ₀, f.has_eigenvalue μ₀ := exists_eigenvalue f, -- We define `ES` to be the generalized eigenspace let ES := f.generalized_eigenspace μ₀ (findim K V), -- and `ER` to be the generalized eigenrange. let ER := f.generalized_eigenrange μ₀ (findim K V), -- `f` maps `ER` into itself. have h_f_ER : ∀ (x : V), x ∈ ER → f x ∈ ER, from λ x hx, map_generalized_eigenrange_le (submodule.mem_map_of_mem hx), -- Therefore, we can define the restriction `f'` of `f` to `ER`. let f' : End K ER := f.restrict h_f_ER, -- The dimension of `ES` is positive have h_dim_ES_pos : 0 < findim K ES, { dsimp only [ES], rw h_dim, apply pos_findim_generalized_eigenspace_of_has_eigenvalue hμ₀ (nat.zero_lt_succ n) }, -- and the dimensions of `ES` and `ER` add up to `findim K V`. have h_dim_add : findim K ER + findim K ES = findim K V, { apply linear_map.findim_range_add_findim_ker }, -- Therefore the dimension `ER` mus be smaller than `findim K V`. have h_dim_ER : findim K ER < n.succ, by omega, -- This allows us to apply the induction hypothesis on `ER`: have ih_ER : (⨆ (μ : K) (k : ℕ), f'.generalized_eigenspace μ k) = ⊤, from ih (findim K ER) h_dim_ER f' rfl, -- The induction hypothesis gives us a statement about subspaces of `ER`. We can transfer this -- to a statement about subspaces of `V` via `submodule.subtype`: have ih_ER' : (⨆ (μ : K) (k : ℕ), (f'.generalized_eigenspace μ k).map ER.subtype) = ER, by simp only [(submodule.map_supr _ _).symm, ih_ER, submodule.map_subtype_top ER], -- Moreover, every generalized eigenspace of `f'` is contained in the corresponding generalized -- eigenspace of `f`. have hff' : ∀ μ k, (f'.generalized_eigenspace μ k).map ER.subtype ≤ f.generalized_eigenspace μ k, { intros, rw generalized_eigenspace_restrict, apply submodule.map_comap_le }, -- It follows that `ER` is contained in the span of all generalized eigenvectors. have hER : ER ≤ ⨆ (μ : K) (k : ℕ), f.generalized_eigenspace μ k, { rw ← ih_ER', apply supr_le_supr _, exact λ μ, supr_le_supr (λ k, hff' μ k), }, -- `ES` is contained in this span by definition. have hES : ES ≤ ⨆ (μ : K) (k : ℕ), f.generalized_eigenspace μ k, from le_trans (le_supr (λ k, f.generalized_eigenspace μ₀ k) (findim K V)) (le_supr (λ (μ : K), ⨆ (k : ℕ), f.generalized_eigenspace μ k) μ₀), -- Moreover, we know that `ER` and `ES` are disjoint. have h_disjoint : disjoint ER ES, from generalized_eigenvec_disjoint_range_ker f μ₀, -- Since the dimensions of `ER` and `ES` add up to the dimension of `V`, it follows that the -- span of all generalized eigenvectors is all of `V`. show (⨆ (μ : K) (k : ℕ), f.generalized_eigenspace μ k) = ⊤, { rw [←top_le_iff, ←submodule.eq_top_of_disjoint ER ES h_dim_add h_disjoint], apply sup_le hER hES } } end end End end module
9f25bd2d436cd194b8bc1c1488c63612dc192b6e
4fa161becb8ce7378a709f5992a594764699e268
/src/algebra/linear_ordered_comm_group_with_zero.lean
860b906e2cee3ef8156069f65a7d67be4f762ecf
[ "Apache-2.0" ]
permissive
laughinggas/mathlib
e4aa4565ae34e46e834434284cb26bd9d67bc373
86dcd5cda7a5017c8b3c8876c89a510a19d49aad
refs/heads/master
1,669,496,232,688
1,592,831,995,000
1,592,831,995,000
274,155,979
0
0
Apache-2.0
1,592,835,190,000
1,592,835,189,000
null
UTF-8
Lean
false
false
4,240
lean
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Johan Commelin, Patrick Massot -/ import algebra.ordered_group import algebra.group_with_zero /-! # Linearly ordered commutative groups with a zero element adjoined This file sets up a special class of linearly ordered commutative monoids that show up as the target of so-called “valuations” in algebraic number theory. Usually, in the informal literature, these objects are constructed by taking a linearly ordered commutative group Γ and formally adjoining a zero element: Γ ∪ {0}. The disadvantage is that a type such as `nnreal` is not of that form, whereas it is a very common target for valuations. The solutions is to use a typeclass, and that is exactly what we do in this file. -/ set_option old_structure_cmd true set_option default_priority 100 -- see Note [default priority] /-- A linearly ordered commutative group with a zero element. -/ class linear_ordered_comm_group_with_zero (α : Type*) extends linear_order α, comm_group_with_zero α := (mul_le_mul_left : ∀ {a b : α}, a ≤ b → ∀ c : α, c * a ≤ c * b) (zero_le_one : (0:α) ≤ 1) variables {α : Type*} [linear_ordered_comm_group_with_zero α] variables {a b c d x y z : α} local attribute [instance] classical.prop_decidable /-- Every linearly ordered commutative group with zero is an ordered commutative monoid.-/ instance linear_ordered_comm_group_with_zero.to_ordered_comm_monoid : ordered_comm_monoid α := { lt_of_mul_lt_mul_left := λ a b c h, by { contrapose! h, exact linear_ordered_comm_group_with_zero.mul_le_mul_left h a } .. ‹linear_ordered_comm_group_with_zero α› } lemma zero_le_one' : (0 : α) ≤ 1 := linear_ordered_comm_group_with_zero.zero_le_one lemma zero_lt_one' : (0 : α) < 1 := lt_of_le_of_ne zero_le_one' zero_ne_one @[simp] lemma zero_le' : 0 ≤ a := by simpa only [mul_zero, mul_one] using mul_le_mul_left' zero_le_one' @[simp] lemma not_lt_zero' : ¬a < 0 := not_lt_of_le zero_le' @[simp] lemma le_zero_iff : a ≤ 0 ↔ a = 0 := ⟨λ h, le_antisymm h zero_le', λ h, h ▸ le_refl _⟩ lemma zero_lt_iff : 0 < a ↔ a ≠ 0 := ⟨ne_of_gt, λ h, lt_of_le_of_ne zero_le' h.symm⟩ lemma le_of_le_mul_right (h : c ≠ 0) (hab : a * c ≤ b * c) : a ≤ b := by simpa [h] using (mul_le_mul_right' hab : a * c * c⁻¹ ≤ b * c * c⁻¹) lemma le_mul_inv_of_mul_le (h : c ≠ 0) (hab : a * c ≤ b) : a ≤ b * c⁻¹ := le_of_le_mul_right h (by simpa [h] using hab) lemma mul_inv_le_of_le_mul (h : c ≠ 0) (hab : a ≤ b * c) : a * c⁻¹ ≤ b := le_of_le_mul_right h (by simpa [h] using hab) lemma div_le_div' (a b c d : α) (hb : b ≠ 0) (hd : d ≠ 0) : a * b⁻¹ ≤ c * d⁻¹ ↔ a * d ≤ c * b := begin by_cases ha : a = 0, { simp [ha] }, by_cases hc : c = 0, { simp [inv_ne_zero hb, hc, hd], }, exact (div_le_div_iff' (units.mk0 a ha) (units.mk0 b hb) (units.mk0 c hc) (units.mk0 d hd)), end lemma ne_zero_of_lt (h : b < a) : a ≠ 0 := λ h1, not_lt_zero' $ show b < 0, from h1 ▸ h @[simp] lemma zero_lt_unit (u : units α) : (0 : α) < u := zero_lt_iff.2 $ unit_ne_zero u lemma mul_lt_mul'''' (hab : a < b) (hcd : c < d) : a * c < b * d := have hb : b ≠ 0 := ne_zero_of_lt hab, have hd : d ≠ 0 := ne_zero_of_lt hcd, if ha : a = 0 then by { rw [ha, zero_mul, zero_lt_iff], exact mul_ne_zero'' hb hd } else if hc : c = 0 then by { rw [hc, mul_zero, zero_lt_iff], exact mul_ne_zero'' hb hd } else @mul_lt_mul''' _ _ (units.mk0 a ha) (units.mk0 b hb) (units.mk0 c hc) (units.mk0 d hd) hab hcd lemma mul_inv_lt_of_lt_mul' (h : x < y * z) : x * z⁻¹ < y := have hz : z ≠ 0 := (mul_ne_zero_iff.1 $ ne_zero_of_lt h).2, by { contrapose! h, simpa only [inv_inv'] using mul_inv_le_of_le_mul (inv_ne_zero hz) h } lemma mul_lt_right' (c : α) (h : a < b) (hc : c ≠ 0) : a * c < b * c := by { contrapose! h, exact le_of_le_mul_right hc h } lemma inv_lt_inv'' (ha : a ≠ 0) (hb : b ≠ 0) : a⁻¹ < b⁻¹ ↔ b < a := @inv_lt_inv_iff _ _ (units.mk0 a ha) (units.mk0 b hb) lemma inv_le_inv'' (ha : a ≠ 0) (hb : b ≠ 0) : a⁻¹ ≤ b⁻¹ ↔ b ≤ a := @inv_le_inv_iff _ _ (units.mk0 a ha) (units.mk0 b hb)
d9e9925d6317db2e9da334e73d60071acf57ce66
94e33a31faa76775069b071adea97e86e218a8ee
/src/measure_theory/integral/interval_integral.lean
70addaba8b80cfc2db1881549007f444c917df34
[ "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
136,713
lean
/- Copyright (c) 2020 Yury G. Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury G. Kudryashov, Patrick Massot, Sébastien Gouëzel -/ import analysis.normed_space.dual import data.set.intervals.disjoint import measure_theory.measure.haar_lebesgue import analysis.calculus.extend_deriv import measure_theory.function.locally_integrable import measure_theory.integral.set_integral import measure_theory.integral.vitali_caratheodory import analysis.calculus.fderiv_measurable /-! # Integral over an interval In this file we define `∫ x in a..b, f x ∂μ` to be `∫ x in Ioc a b, f x ∂μ` if `a ≤ b` and `-∫ x in Ioc b a, f x ∂μ` if `b ≤ a`. We prove a few simple properties and several versions of the [fundamental theorem of calculus](https://en.wikipedia.org/wiki/Fundamental_theorem_of_calculus). Recall that its first version states that the function `(u, v) ↦ ∫ x in u..v, f x` has derivative `(δu, δv) ↦ δv • f b - δu • f a` at `(a, b)` provided that `f` is continuous at `a` and `b`, and its second version states that, if `f` has an integrable derivative on `[a, b]`, then `∫ x in a..b, f' x = f b - f a`. ## Main statements ### FTC-1 for Lebesgue measure We prove several versions of FTC-1, all in the `interval_integral` namespace. Many of them follow the naming scheme `integral_has(_strict?)_(f?)deriv(_within?)_at(_of_tendsto_ae?)(_right|_left?)`. They formulate FTC in terms of `has(_strict?)_(f?)deriv(_within?)_at`. Let us explain the meaning of each part of the name: * `_strict` means that the theorem is about strict differentiability; * `f` means that the theorem is about differentiability in both endpoints; incompatible with `_right|_left`; * `_within` means that the theorem is about one-sided derivatives, see below for details; * `_of_tendsto_ae` means that instead of continuity the theorem assumes that `f` has a finite limit almost surely as `x` tends to `a` and/or `b`; * `_right` or `_left` mean that the theorem is about differentiability in the right (resp., left) endpoint. We also reformulate these theorems in terms of `(f?)deriv(_within?)`. These theorems are named `(f?)deriv(_within?)_integral(_of_tendsto_ae?)(_right|_left?)` with the same meaning of parts of the name. ### One-sided derivatives Theorem `integral_has_fderiv_within_at_of_tendsto_ae` states that `(u, v) ↦ ∫ x in u..v, f x` has a derivative `(δu, δv) ↦ δv • cb - δu • ca` within the set `s × t` at `(a, b)` provided that `f` tends to `ca` (resp., `cb`) almost surely at `la` (resp., `lb`), where possible values of `s`, `t`, and corresponding filters `la`, `lb` are given in the following table. | `s` | `la` | `t` | `lb` | | ------- | ---- | --- | ---- | | `Iic a` | `𝓝[≤] a` | `Iic b` | `𝓝[≤] b` | | `Ici a` | `𝓝[>] a` | `Ici b` | `𝓝[>] b` | | `{a}` | `⊥` | `{b}` | `⊥` | | `univ` | `𝓝 a` | `univ` | `𝓝 b` | We use a typeclass `FTC_filter` to make Lean automatically find `la`/`lb` based on `s`/`t`. This way we can formulate one theorem instead of `16` (or `8` if we leave only non-trivial ones not covered by `integral_has_deriv_within_at_of_tendsto_ae_(left|right)` and `integral_has_fderiv_at_of_tendsto_ae`). Similarly, `integral_has_deriv_within_at_of_tendsto_ae_right` works for both one-sided derivatives using the same typeclass to find an appropriate filter. ### FTC for a locally finite measure Before proving FTC for the Lebesgue measure, we prove a few statements that can be seen as FTC for any measure. The most general of them, `measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae`, states the following. Let `(la, la')` be an `FTC_filter` pair of filters around `a` (i.e., `FTC_filter a la la'`) and let `(lb, lb')` be an `FTC_filter` pair of filters around `b`. If `f` has finite limits `ca` and `cb` almost surely at `la'` and `lb'`, respectively, then `∫ x in va..vb, f x ∂μ - ∫ x in ua..ub, f x ∂μ = ∫ x in ub..vb, cb ∂μ - ∫ x in ua..va, ca ∂μ + o(∥∫ x in ua..va, (1:ℝ) ∂μ∥ + ∥∫ x in ub..vb, (1:ℝ) ∂μ∥)` as `ua` and `va` tend to `la` while `ub` and `vb` tend to `lb`. ### FTC-2 and corollaries We use FTC-1 to prove several versions of FTC-2 for the Lebesgue measure, using a similar naming scheme as for the versions of FTC-1. They include: * `interval_integral.integral_eq_sub_of_has_deriv_right_of_le` - most general version, for functions with a right derivative * `interval_integral.integral_eq_sub_of_has_deriv_at'` - version for functions with a derivative on an open set * `interval_integral.integral_deriv_eq_sub'` - version that is easiest to use when computing the integral of a specific function We then derive additional integration techniques from FTC-2: * `interval_integral.integral_mul_deriv_eq_deriv_mul` - integration by parts * `interval_integral.integral_comp_mul_deriv''` - integration by substitution Many applications of these theorems can be found in the file `analysis.special_functions.integrals`. Note that the assumptions of FTC-2 are formulated in the form that `f'` is integrable. To use it in a context with the stronger assumption that `f'` is continuous, one can use `continuous_on.interval_integrable` or `continuous_on.integrable_on_Icc` or `continuous_on.integrable_on_interval`. ## Implementation notes ### Avoiding `if`, `min`, and `max` In order to avoid `if`s in the definition, we define `interval_integrable f μ a b` as `integrable_on f (Ioc a b) μ ∧ integrable_on f (Ioc b a) μ`. For any `a`, `b` one of these intervals is empty and the other coincides with `set.interval_oc a b = set.Ioc (min a b) (max a b)`. Similarly, we define `∫ x in a..b, f x ∂μ` to be `∫ x in Ioc a b, f x ∂μ - ∫ x in Ioc b a, f x ∂μ`. Again, for any `a`, `b` one of these integrals is zero, and the other gives the expected result. This way some properties can be translated from integrals over sets without dealing with the cases `a ≤ b` and `b ≤ a` separately. ### Choice of the interval We use integral over `set.interval_oc a b = set.Ioc (min a b) (max a b)` instead of one of the other three possible intervals with the same endpoints for two reasons: * this way `∫ x in a..b, f x ∂μ + ∫ x in b..c, f x ∂μ = ∫ x in a..c, f x ∂μ` holds whenever `f` is integrable on each interval; in particular, it works even if the measure `μ` has an atom at `b`; this rules out `set.Ioo` and `set.Icc` intervals; * with this definition for a probability measure `μ`, the integral `∫ x in a..b, 1 ∂μ` equals the difference $F_μ(b)-F_μ(a)$, where $F_μ(a)=μ(-∞, a]$ is the [cumulative distribution function](https://en.wikipedia.org/wiki/Cumulative_distribution_function) of `μ`. ### `FTC_filter` class As explained above, many theorems in this file rely on the typeclass `FTC_filter (a : ℝ) (l l' : filter ℝ)` to avoid code duplication. This typeclass combines four assumptions: - `pure a ≤ l`; - `l' ≤ 𝓝 a`; - `l'` has a basis of measurable sets; - if `u n` and `v n` tend to `l`, then for any `s ∈ l'`, `Ioc (u n) (v n)` is eventually included in `s`. This typeclass has the following “real” instances: `(a, pure a, ⊥)`, `(a, 𝓝[≥] a, 𝓝[>] a)`, `(a, 𝓝[≤] a, 𝓝[≤] a)`, `(a, 𝓝 a, 𝓝 a)`. Furthermore, we have the following instances that are equal to the previously mentioned instances: `(a, 𝓝[{a}] a, ⊥)` and `(a, 𝓝[univ] a, 𝓝[univ] a)`. While the difference between `Ici a` and `Ioi a` doesn't matter for theorems about Lebesgue measure, it becomes important in the versions of FTC about any locally finite measure if this measure has an atom at one of the endpoints. ### Combining one-sided and two-sided derivatives There are some `FTC_filter` instances where the fact that it is one-sided or two-sided depends on the point, namely `(x, 𝓝[Icc a b] x, 𝓝[Icc a b] x)` (resp. `(x, 𝓝[[a, b]] x, 𝓝[[a, b]] x)`, where `[a, b] = set.interval a b`), with `x ∈ Icc a b` (resp. `x ∈ [a, b]`). This results in a two-sided derivatives for `x ∈ Ioo a b` and one-sided derivatives for `x ∈ {a, b}`. Other instances could be added when needed (in that case, one also needs to add instances for `filter.is_measurably_generated` and `filter.tendsto_Ixx_class`). ## Tags integral, fundamental theorem of calculus, FTC-1, FTC-2, change of variables in integrals -/ noncomputable theory open topological_space (second_countable_topology) open measure_theory set classical filter function open_locale classical topological_space filter ennreal big_operators interval nnreal variables {ι 𝕜 E F : Type*} [normed_group E] /-! ### Integrability at an interval -/ /-- A function `f` is called *interval integrable* with respect to a measure `μ` on an unordered interval `a..b` if it is integrable on both intervals `(a, b]` and `(b, a]`. One of these intervals is always empty, so this property is equivalent to `f` being integrable on `(min a b, max a b]`. -/ def interval_integrable (f : ℝ → E) (μ : measure ℝ) (a b : ℝ) := integrable_on f (Ioc a b) μ ∧ integrable_on f (Ioc b a) μ section variables {f : ℝ → E} {a b : ℝ} {μ : measure ℝ} /-- A function is interval integrable with respect to a given measure `μ` on `a..b` if and only if it is integrable on `interval_oc a b` with respect to `μ`. This is an equivalent definition of `interval_integrable`. -/ lemma interval_integrable_iff : interval_integrable f μ a b ↔ integrable_on f (Ι a b) μ := by rw [interval_oc_eq_union, integrable_on_union, interval_integrable] /-- If a function is interval integrable with respect to a given measure `μ` on `a..b` then it is integrable on `interval_oc a b` with respect to `μ`. -/ lemma interval_integrable.def (h : interval_integrable f μ a b) : integrable_on f (Ι a b) μ := interval_integrable_iff.mp h lemma interval_integrable_iff_integrable_Ioc_of_le (hab : a ≤ b) : interval_integrable f μ a b ↔ integrable_on f (Ioc a b) μ := by rw [interval_integrable_iff, interval_oc_of_le hab] lemma integrable_on_Icc_iff_integrable_on_Ioc' {E : Type*} [normed_group E] {f : ℝ → E} (ha : μ {a} ≠ ∞) : integrable_on f (Icc a b) μ ↔ integrable_on f (Ioc a b) μ := begin cases le_or_lt a b with hab hab, { have : Icc a b = Icc a a ∪ Ioc a b := (Icc_union_Ioc_eq_Icc le_rfl hab).symm, rw [this, integrable_on_union], simp [ha.lt_top] }, { simp [hab, hab.le] }, end lemma integrable_on_Icc_iff_integrable_on_Ioc {E : Type*}[normed_group E] [has_no_atoms μ] {f : ℝ → E} {a b : ℝ} : integrable_on f (Icc a b) μ ↔ integrable_on f (Ioc a b) μ := integrable_on_Icc_iff_integrable_on_Ioc' (by simp) lemma integrable_on_Ioc_iff_integrable_on_Ioo' {E : Type*} [normed_group E] {f : ℝ → E} {a b : ℝ} (hb : μ {b} ≠ ∞) : integrable_on f (Ioc a b) μ ↔ integrable_on f (Ioo a b) μ := begin cases lt_or_le a b with hab hab, { have : Ioc a b = Ioo a b ∪ Icc b b := (Ioo_union_Icc_eq_Ioc hab le_rfl).symm, rw [this, integrable_on_union], simp [hb.lt_top] }, { simp [hab] }, end lemma integrable_on_Ioc_iff_integrable_on_Ioo {E : Type*} [normed_group E] [has_no_atoms μ] {f : ℝ → E} {a b : ℝ} : integrable_on f (Ioc a b) μ ↔ integrable_on f (Ioo a b) μ := integrable_on_Ioc_iff_integrable_on_Ioo' (by simp) lemma integrable_on_Icc_iff_integrable_on_Ioo {E : Type*} [normed_group E] [has_no_atoms μ] {f : ℝ → E} {a b : ℝ} : integrable_on f (Icc a b) μ ↔ integrable_on f (Ioo a b) μ := by rw [integrable_on_Icc_iff_integrable_on_Ioc, integrable_on_Ioc_iff_integrable_on_Ioo] lemma interval_integrable_iff' [has_no_atoms μ] : interval_integrable f μ a b ↔ integrable_on f (interval a b) μ := by rw [interval_integrable_iff, interval, interval_oc, integrable_on_Icc_iff_integrable_on_Ioc] lemma interval_integrable_iff_integrable_Icc_of_le {E : Type*} [normed_group E] {f : ℝ → E} {a b : ℝ} (hab : a ≤ b) {μ : measure ℝ} [has_no_atoms μ] : interval_integrable f μ a b ↔ integrable_on f (Icc a b) μ := by rw [interval_integrable_iff_integrable_Ioc_of_le hab, integrable_on_Icc_iff_integrable_on_Ioc] lemma integrable_on_Ici_iff_integrable_on_Ioi' {E : Type*} [normed_group E] {f : ℝ → E} (ha : μ {a} ≠ ∞) : integrable_on f (Ici a) μ ↔ integrable_on f (Ioi a) μ := begin have : Ici a = Icc a a ∪ Ioi a := (Icc_union_Ioi_eq_Ici le_rfl).symm, rw [this, integrable_on_union], simp [ha.lt_top] end lemma integrable_on_Ici_iff_integrable_on_Ioi {E : Type*} [normed_group E] [has_no_atoms μ] {f : ℝ → E} : integrable_on f (Ici a) μ ↔ integrable_on f (Ioi a) μ := integrable_on_Ici_iff_integrable_on_Ioi' (by simp) /-- If a function is integrable with respect to a given measure `μ` then it is interval integrable with respect to `μ` on `interval a b`. -/ lemma measure_theory.integrable.interval_integrable (hf : integrable f μ) : interval_integrable f μ a b := ⟨hf.integrable_on, hf.integrable_on⟩ lemma measure_theory.integrable_on.interval_integrable (hf : integrable_on f [a, b] μ) : interval_integrable f μ a b := ⟨measure_theory.integrable_on.mono_set hf (Ioc_subset_Icc_self.trans Icc_subset_interval), measure_theory.integrable_on.mono_set hf (Ioc_subset_Icc_self.trans Icc_subset_interval')⟩ lemma interval_integrable_const_iff {c : E} : interval_integrable (λ _, c) μ a b ↔ c = 0 ∨ μ (Ι a b) < ∞ := by simp only [interval_integrable_iff, integrable_on_const] @[simp] lemma interval_integrable_const [is_locally_finite_measure μ] {c : E} : interval_integrable (λ _, c) μ a b := interval_integrable_const_iff.2 $ or.inr measure_Ioc_lt_top end namespace interval_integrable section variables {f : ℝ → E} {a b c d : ℝ} {μ ν : measure ℝ} @[symm] lemma symm (h : interval_integrable f μ a b) : interval_integrable f μ b a := h.symm @[refl] lemma refl : interval_integrable f μ a a := by split; simp @[trans] lemma trans {a b c : ℝ} (hab : interval_integrable f μ a b) (hbc : interval_integrable f μ b c) : interval_integrable f μ a c := ⟨(hab.1.union hbc.1).mono_set Ioc_subset_Ioc_union_Ioc, (hbc.2.union hab.2).mono_set Ioc_subset_Ioc_union_Ioc⟩ lemma trans_iterate_Ico {a : ℕ → ℝ} {m n : ℕ} (hmn : m ≤ n) (hint : ∀ k ∈ Ico m n, interval_integrable f μ (a k) (a $ k+1)) : interval_integrable f μ (a m) (a n) := begin revert hint, refine nat.le_induction _ _ n hmn, { simp }, { assume p hp IH h, exact (IH (λ k hk, h k (Ico_subset_Ico_right p.le_succ hk))).trans (h p (by simp [hp])) } end lemma trans_iterate {a : ℕ → ℝ} {n : ℕ} (hint : ∀ k < n, interval_integrable f μ (a k) (a $ k+1)) : interval_integrable f μ (a 0) (a n) := trans_iterate_Ico bot_le (λ k hk, hint k hk.2) lemma neg (h : interval_integrable f μ a b) : interval_integrable (-f) μ a b := ⟨h.1.neg, h.2.neg⟩ lemma norm (h : interval_integrable f μ a b) : interval_integrable (λ x, ∥f x∥) μ a b := ⟨h.1.norm, h.2.norm⟩ lemma abs {f : ℝ → ℝ} (h : interval_integrable f μ a b) : interval_integrable (λ x, |f x|) μ a b := h.norm lemma mono (hf : interval_integrable f ν a b) (h1 : [c, d] ⊆ [a, b]) (h2 : μ ≤ ν) : interval_integrable f μ c d := interval_integrable_iff.mpr $ hf.def.mono (interval_oc_subset_interval_oc_of_interval_subset_interval h1) h2 lemma mono_set (hf : interval_integrable f μ a b) (h : [c, d] ⊆ [a, b]) : interval_integrable f μ c d := hf.mono h rfl.le lemma mono_measure (hf : interval_integrable f ν a b) (h : μ ≤ ν) : interval_integrable f μ a b := hf.mono rfl.subset h lemma mono_set_ae (hf : interval_integrable f μ a b) (h : Ι c d ≤ᵐ[μ] Ι a b) : interval_integrable f μ c d := interval_integrable_iff.mpr $ hf.def.mono_set_ae h lemma mono_fun [normed_group F] {g : ℝ → F} (hf : interval_integrable f μ a b) (hgm : ae_strongly_measurable g (μ.restrict (Ι a b))) (hle : (λ x, ∥g x∥) ≤ᵐ[μ.restrict (Ι a b)] (λ x, ∥f x∥)) : interval_integrable g μ a b := interval_integrable_iff.2 $ hf.def.integrable.mono hgm hle lemma mono_fun' {g : ℝ → ℝ} (hg : interval_integrable g μ a b) (hfm : ae_strongly_measurable f (μ.restrict (Ι a b))) (hle : (λ x, ∥f x∥) ≤ᵐ[μ.restrict (Ι a b)] g) : interval_integrable f μ a b := interval_integrable_iff.2 $ hg.def.integrable.mono' hfm hle protected lemma ae_strongly_measurable (h : interval_integrable f μ a b) : ae_strongly_measurable f (μ.restrict (Ioc a b)):= h.1.ae_strongly_measurable protected lemma ae_strongly_measurable' (h : interval_integrable f μ a b) : ae_strongly_measurable f (μ.restrict (Ioc b a)):= h.2.ae_strongly_measurable end variables {f g : ℝ → E} {a b : ℝ} {μ : measure ℝ} lemma smul [normed_field 𝕜] [normed_space 𝕜 E] {f : ℝ → E} {a b : ℝ} {μ : measure ℝ} (h : interval_integrable f μ a b) (r : 𝕜) : interval_integrable (r • f) μ a b := ⟨h.1.smul r, h.2.smul r⟩ @[simp] lemma add (hf : interval_integrable f μ a b) (hg : interval_integrable g μ a b) : interval_integrable (λ x, f x + g x) μ a b := ⟨hf.1.add hg.1, hf.2.add hg.2⟩ @[simp] lemma sub (hf : interval_integrable f μ a b) (hg : interval_integrable g μ a b) : interval_integrable (λ x, f x - g x) μ a b := ⟨hf.1.sub hg.1, hf.2.sub hg.2⟩ lemma sum (s : finset ι) {f : ι → ℝ → E} (h : ∀ i ∈ s, interval_integrable (f i) μ a b) : interval_integrable (∑ i in s, f i) μ a b := ⟨integrable_finset_sum' s (λ i hi, (h i hi).1), integrable_finset_sum' s (λ i hi, (h i hi).2)⟩ lemma mul_continuous_on {f g : ℝ → ℝ} (hf : interval_integrable f μ a b) (hg : continuous_on g [a, b]) : interval_integrable (λ x, f x * g x) μ a b := begin rw interval_integrable_iff at hf ⊢, exact hf.mul_continuous_on_of_subset hg measurable_set_Ioc is_compact_interval Ioc_subset_Icc_self end lemma continuous_on_mul {f g : ℝ → ℝ} (hf : interval_integrable f μ a b) (hg : continuous_on g [a, b]) : interval_integrable (λ x, g x * f x) μ a b := by simpa [mul_comm] using hf.mul_continuous_on hg lemma comp_mul_left (hf : interval_integrable f volume a b) (c : ℝ) : interval_integrable (λ x, f (c * x)) volume (a / c) (b / c) := begin rcases eq_or_ne c 0 with hc|hc, { rw hc, simp }, rw interval_integrable_iff' at hf ⊢, have A : measurable_embedding (λ x, x * c⁻¹) := (homeomorph.mul_right₀ _ (inv_ne_zero hc)).closed_embedding.measurable_embedding, rw [←real.smul_map_volume_mul_right (inv_ne_zero hc), integrable_on, measure.restrict_smul, integrable_smul_measure (by simpa : ennreal.of_real (|c⁻¹|) ≠ 0) ennreal.of_real_ne_top, ←integrable_on, measurable_embedding.integrable_on_map_iff A], convert hf using 1, { ext, simp only [comp_app], congr' 1, field_simp, ring }, { rw preimage_mul_const_interval (inv_ne_zero hc), field_simp [hc] }, end lemma iff_comp_neg : interval_integrable f volume a b ↔ interval_integrable (λ x, f (-x)) volume (-a) (-b) := begin split, all_goals { intro hf, convert comp_mul_left hf (-1), simp, field_simp, field_simp }, end end interval_integrable section variables {μ : measure ℝ} [is_locally_finite_measure μ] lemma continuous_on.interval_integrable {u : ℝ → E} {a b : ℝ} (hu : continuous_on u (interval a b)) : interval_integrable u μ a b := (continuous_on.integrable_on_Icc hu).interval_integrable lemma continuous_on.interval_integrable_of_Icc {u : ℝ → E} {a b : ℝ} (h : a ≤ b) (hu : continuous_on u (Icc a b)) : interval_integrable u μ a b := continuous_on.interval_integrable ((interval_of_le h).symm ▸ hu) /-- A continuous function on `ℝ` is `interval_integrable` with respect to any locally finite measure `ν` on ℝ. -/ lemma continuous.interval_integrable {u : ℝ → E} (hu : continuous u) (a b : ℝ) : interval_integrable u μ a b := hu.continuous_on.interval_integrable end section variables {μ : measure ℝ} [is_locally_finite_measure μ] [conditionally_complete_linear_order E] [order_topology E] [second_countable_topology E] lemma monotone_on.interval_integrable {u : ℝ → E} {a b : ℝ} (hu : monotone_on u (interval a b)) : interval_integrable u μ a b := begin rw interval_integrable_iff, exact (hu.integrable_on_compact is_compact_interval).mono_set Ioc_subset_Icc_self, end lemma antitone_on.interval_integrable {u : ℝ → E} {a b : ℝ} (hu : antitone_on u (interval a b)) : interval_integrable u μ a b := hu.dual_right.interval_integrable lemma monotone.interval_integrable {u : ℝ → E} {a b : ℝ} (hu : monotone u) : interval_integrable u μ a b := (hu.monotone_on _).interval_integrable lemma antitone.interval_integrable {u : ℝ → E} {a b : ℝ} (hu : antitone u) : interval_integrable u μ a b := (hu.antitone_on _).interval_integrable end /-- Let `l'` be a measurably generated filter; let `l` be a of filter such that each `s ∈ l'` eventually includes `Ioc u v` as both `u` and `v` tend to `l`. Let `μ` be a measure finite at `l'`. Suppose that `f : ℝ → E` has a finite limit at `l' ⊓ μ.ae`. Then `f` is interval integrable on `u..v` provided that both `u` and `v` tend to `l`. Typeclass instances allow Lean to find `l'` based on `l` but not vice versa, so `apply tendsto.eventually_interval_integrable_ae` will generate goals `filter ℝ` and `tendsto_Ixx_class Ioc ?m_1 l'`. -/ lemma filter.tendsto.eventually_interval_integrable_ae {f : ℝ → E} {μ : measure ℝ} {l l' : filter ℝ} (hfm : strongly_measurable_at_filter f l' μ) [tendsto_Ixx_class Ioc l l'] [is_measurably_generated l'] (hμ : μ.finite_at_filter l') {c : E} (hf : tendsto f (l' ⊓ μ.ae) (𝓝 c)) {u v : ι → ℝ} {lt : filter ι} (hu : tendsto u lt l) (hv : tendsto v lt l) : ∀ᶠ t in lt, interval_integrable f μ (u t) (v t) := have _ := (hf.integrable_at_filter_ae hfm hμ).eventually, ((hu.Ioc hv).eventually this).and $ (hv.Ioc hu).eventually this /-- Let `l'` be a measurably generated filter; let `l` be a of filter such that each `s ∈ l'` eventually includes `Ioc u v` as both `u` and `v` tend to `l`. Let `μ` be a measure finite at `l'`. Suppose that `f : ℝ → E` has a finite limit at `l`. Then `f` is interval integrable on `u..v` provided that both `u` and `v` tend to `l`. Typeclass instances allow Lean to find `l'` based on `l` but not vice versa, so `apply tendsto.eventually_interval_integrable_ae` will generate goals `filter ℝ` and `tendsto_Ixx_class Ioc ?m_1 l'`. -/ lemma filter.tendsto.eventually_interval_integrable {f : ℝ → E} {μ : measure ℝ} {l l' : filter ℝ} (hfm : strongly_measurable_at_filter f l' μ) [tendsto_Ixx_class Ioc l l'] [is_measurably_generated l'] (hμ : μ.finite_at_filter l') {c : E} (hf : tendsto f l' (𝓝 c)) {u v : ι → ℝ} {lt : filter ι} (hu : tendsto u lt l) (hv : tendsto v lt l) : ∀ᶠ t in lt, interval_integrable f μ (u t) (v t) := (hf.mono_left inf_le_left).eventually_interval_integrable_ae hfm hμ hu hv /-! ### Interval integral: definition and basic properties In this section we define `∫ x in a..b, f x ∂μ` as `∫ x in Ioc a b, f x ∂μ - ∫ x in Ioc b a, f x ∂μ` and prove some basic properties. -/ variables [complete_space E] [normed_space ℝ E] /-- The interval integral `∫ x in a..b, f x ∂μ` is defined as `∫ x in Ioc a b, f x ∂μ - ∫ x in Ioc b a, f x ∂μ`. If `a ≤ b`, then it equals `∫ x in Ioc a b, f x ∂μ`, otherwise it equals `-∫ x in Ioc b a, f x ∂μ`. -/ def interval_integral (f : ℝ → E) (a b : ℝ) (μ : measure ℝ) := ∫ x in Ioc a b, f x ∂μ - ∫ x in Ioc b a, f x ∂μ notation `∫` binders ` in ` a `..` b `, ` r:(scoped:60 f, f) ` ∂` μ:70 := interval_integral r a b μ notation `∫` binders ` in ` a `..` b `, ` r:(scoped:60 f, interval_integral f a b volume) := r namespace interval_integral section basic variables {a b : ℝ} {f g : ℝ → E} {μ : measure ℝ} @[simp] lemma integral_zero : ∫ x in a..b, (0 : E) ∂μ = 0 := by simp [interval_integral] lemma integral_of_le (h : a ≤ b) : ∫ x in a..b, f x ∂μ = ∫ x in Ioc a b, f x ∂μ := by simp [interval_integral, h] @[simp] lemma integral_same : ∫ x in a..a, f x ∂μ = 0 := sub_self _ lemma integral_symm (a b) : ∫ x in b..a, f x ∂μ = -∫ x in a..b, f x ∂μ := by simp only [interval_integral, neg_sub] lemma integral_of_ge (h : b ≤ a) : ∫ x in a..b, f x ∂μ = -∫ x in Ioc b a, f x ∂μ := by simp only [integral_symm b, integral_of_le h] lemma interval_integral_eq_integral_interval_oc (f : ℝ → E) (a b : ℝ) (μ : measure ℝ) : ∫ x in a..b, f x ∂μ = (if a ≤ b then 1 else -1 : ℝ) • ∫ x in Ι a b, f x ∂μ := begin split_ifs with h, { simp only [integral_of_le h, interval_oc_of_le h, one_smul] }, { simp only [integral_of_ge (not_le.1 h).le, interval_oc_of_lt (not_le.1 h), neg_one_smul] } end lemma integral_cases (f : ℝ → E) (a b) : ∫ x in a..b, f x ∂μ ∈ ({∫ x in Ι a b, f x ∂μ, -∫ x in Ι a b, f x ∂μ} : set E) := by { rw interval_integral_eq_integral_interval_oc, split_ifs; simp } lemma integral_undef (h : ¬ interval_integrable f μ a b) : ∫ x in a..b, f x ∂μ = 0 := by cases le_total a b with hab hab; simp only [integral_of_le, integral_of_ge, hab, neg_eq_zero]; refine integral_undef (not_imp_not.mpr integrable.integrable_on' _); simpa [hab] using not_and_distrib.mp h lemma integral_non_ae_strongly_measurable (hf : ¬ ae_strongly_measurable f (μ.restrict (Ι a b))) : ∫ x in a..b, f x ∂μ = 0 := by rw [interval_integral_eq_integral_interval_oc, integral_non_ae_strongly_measurable hf, smul_zero] lemma integral_non_ae_strongly_measurable_of_le (h : a ≤ b) (hf : ¬ ae_strongly_measurable f (μ.restrict (Ioc a b))) : ∫ x in a..b, f x ∂μ = 0 := integral_non_ae_strongly_measurable $ by rwa [interval_oc_of_le h] lemma norm_integral_min_max (f : ℝ → E) : ∥∫ x in min a b..max a b, f x ∂μ∥ = ∥∫ x in a..b, f x ∂μ∥ := by cases le_total a b; simp [*, integral_symm a b] lemma norm_integral_eq_norm_integral_Ioc (f : ℝ → E) : ∥∫ x in a..b, f x ∂μ∥ = ∥∫ x in Ι a b, f x ∂μ∥ := by rw [← norm_integral_min_max, integral_of_le min_le_max, interval_oc] lemma abs_integral_eq_abs_integral_interval_oc (f : ℝ → ℝ) : |∫ x in a..b, f x ∂μ| = |∫ x in Ι a b, f x ∂μ| := norm_integral_eq_norm_integral_Ioc f lemma norm_integral_le_integral_norm_Ioc : ∥∫ x in a..b, f x ∂μ∥ ≤ ∫ x in Ι a b, ∥f x∥ ∂μ := calc ∥∫ x in a..b, f x ∂μ∥ = ∥∫ x in Ι a b, f x ∂μ∥ : norm_integral_eq_norm_integral_Ioc f ... ≤ ∫ x in Ι a b, ∥f x∥ ∂μ : norm_integral_le_integral_norm f lemma norm_integral_le_abs_integral_norm : ∥∫ x in a..b, f x ∂μ∥ ≤ |∫ x in a..b, ∥f x∥ ∂μ| := begin simp only [← real.norm_eq_abs, norm_integral_eq_norm_integral_Ioc], exact le_trans (norm_integral_le_integral_norm _) (le_abs_self _) end lemma norm_integral_le_integral_norm (h : a ≤ b) : ∥∫ x in a..b, f x ∂μ∥ ≤ ∫ x in a..b, ∥f x∥ ∂μ := norm_integral_le_integral_norm_Ioc.trans_eq $ by rw [interval_oc_of_le h, integral_of_le h] lemma norm_integral_le_of_norm_le_const_ae {a b C : ℝ} {f : ℝ → E} (h : ∀ᵐ x, x ∈ Ι a b → ∥f x∥ ≤ C) : ∥∫ x in a..b, f x∥ ≤ C * |b - a| := begin rw [norm_integral_eq_norm_integral_Ioc], convert norm_set_integral_le_of_norm_le_const_ae'' _ measurable_set_Ioc h, { rw [real.volume_Ioc, max_sub_min_eq_abs, ennreal.to_real_of_real (abs_nonneg _)] }, { simp only [real.volume_Ioc, ennreal.of_real_lt_top] }, end lemma norm_integral_le_of_norm_le_const {a b C : ℝ} {f : ℝ → E} (h : ∀ x ∈ Ι a b, ∥f x∥ ≤ C) : ∥∫ x in a..b, f x∥ ≤ C * |b - a| := norm_integral_le_of_norm_le_const_ae $ eventually_of_forall h @[simp] lemma integral_add (hf : interval_integrable f μ a b) (hg : interval_integrable g μ a b) : ∫ x in a..b, f x + g x ∂μ = ∫ x in a..b, f x ∂μ + ∫ x in a..b, g x ∂μ := by simp only [interval_integral_eq_integral_interval_oc, integral_add hf.def hg.def, smul_add] lemma integral_finset_sum {ι} {s : finset ι} {f : ι → ℝ → E} (h : ∀ i ∈ s, interval_integrable (f i) μ a b) : ∫ x in a..b, ∑ i in s, f i x ∂μ = ∑ i in s, ∫ x in a..b, f i x ∂μ := by simp only [interval_integral_eq_integral_interval_oc, integral_finset_sum s (λ i hi, (h i hi).def), finset.smul_sum] @[simp] lemma integral_neg : ∫ x in a..b, -f x ∂μ = -∫ x in a..b, f x ∂μ := by { simp only [interval_integral, integral_neg], abel } @[simp] lemma integral_sub (hf : interval_integrable f μ a b) (hg : interval_integrable g μ a b) : ∫ x in a..b, f x - g x ∂μ = ∫ x in a..b, f x ∂μ - ∫ x in a..b, g x ∂μ := by simpa only [sub_eq_add_neg] using (integral_add hf hg.neg).trans (congr_arg _ integral_neg) @[simp] lemma integral_smul {𝕜 : Type*} [nondiscrete_normed_field 𝕜] [normed_space 𝕜 E] [smul_comm_class ℝ 𝕜 E] (r : 𝕜) (f : ℝ → E) : ∫ x in a..b, r • f x ∂μ = r • ∫ x in a..b, f x ∂μ := by simp only [interval_integral, integral_smul, smul_sub] @[simp] lemma integral_smul_const {𝕜 : Type*} [is_R_or_C 𝕜] [normed_space 𝕜 E] (f : ℝ → 𝕜) (c : E) : ∫ x in a..b, f x • c ∂μ = (∫ x in a..b, f x ∂μ) • c := by simp only [interval_integral_eq_integral_interval_oc, integral_smul_const, smul_assoc] @[simp] lemma integral_const_mul {𝕜 : Type*} [is_R_or_C 𝕜] (r : 𝕜) (f : ℝ → 𝕜) : ∫ x in a..b, r * f x ∂μ = r * ∫ x in a..b, f x ∂μ := integral_smul r f @[simp] lemma integral_mul_const {𝕜 : Type*} [is_R_or_C 𝕜] (r : 𝕜) (f : ℝ → 𝕜) : ∫ x in a..b, f x * r ∂μ = ∫ x in a..b, f x ∂μ * r := by simpa only [mul_comm r] using integral_const_mul r f @[simp] lemma integral_div {𝕜 : Type*} [is_R_or_C 𝕜] (r : 𝕜) (f : ℝ → 𝕜) : ∫ x in a..b, f x / r ∂μ = ∫ x in a..b, f x ∂μ / r := by simpa only [div_eq_mul_inv] using integral_mul_const r⁻¹ f lemma integral_const' (c : E) : ∫ x in a..b, c ∂μ = ((μ $ Ioc a b).to_real - (μ $ Ioc b a).to_real) • c := by simp only [interval_integral, set_integral_const, sub_smul] @[simp] lemma integral_const (c : E) : ∫ x in a..b, c = (b - a) • c := by simp only [integral_const', real.volume_Ioc, ennreal.to_real_of_real', ← neg_sub b, max_zero_sub_eq_self] lemma integral_smul_measure (c : ℝ≥0∞) : ∫ x in a..b, f x ∂(c • μ) = c.to_real • ∫ x in a..b, f x ∂μ := by simp only [interval_integral, measure.restrict_smul, integral_smul_measure, smul_sub] variables [normed_group F] [complete_space F] [normed_space ℝ F] lemma _root_.continuous_linear_map.interval_integral_comp_comm (L : E →L[ℝ] F) (hf : interval_integrable f μ a b) : ∫ x in a..b, L (f x) ∂μ = L (∫ x in a..b, f x ∂μ) := begin rw [interval_integral, interval_integral, L.integral_comp_comm, L.integral_comp_comm, L.map_sub], exacts [hf.2, hf.1] end end basic section comp variables {a b c d : ℝ} (f : ℝ → E) @[simp] lemma integral_comp_mul_right (hc : c ≠ 0) : ∫ x in a..b, f (x * c) = c⁻¹ • ∫ x in a*c..b*c, f x := begin have A : measurable_embedding (λ x, x * c) := (homeomorph.mul_right₀ c hc).closed_embedding.measurable_embedding, conv_rhs { rw [← real.smul_map_volume_mul_right hc] }, simp_rw [integral_smul_measure, interval_integral, A.set_integral_map, ennreal.to_real_of_real (abs_nonneg c)], cases hc.lt_or_lt, { simp [h, mul_div_cancel, hc, abs_of_neg, measure.restrict_congr_set Ico_ae_eq_Ioc] }, { simp [h, mul_div_cancel, hc, abs_of_pos] } end @[simp] lemma smul_integral_comp_mul_right (c) : c • ∫ x in a..b, f (x * c) = ∫ x in a*c..b*c, f x := by by_cases hc : c = 0; simp [hc] @[simp] lemma integral_comp_mul_left (hc : c ≠ 0) : ∫ x in a..b, f (c * x) = c⁻¹ • ∫ x in c*a..c*b, f x := by simpa only [mul_comm c] using integral_comp_mul_right f hc @[simp] lemma smul_integral_comp_mul_left (c) : c • ∫ x in a..b, f (c * x) = ∫ x in c*a..c*b, f x := by by_cases hc : c = 0; simp [hc] @[simp] lemma integral_comp_div (hc : c ≠ 0) : ∫ x in a..b, f (x / c) = c • ∫ x in a/c..b/c, f x := by simpa only [inv_inv] using integral_comp_mul_right f (inv_ne_zero hc) @[simp] lemma inv_smul_integral_comp_div (c) : c⁻¹ • ∫ x in a..b, f (x / c) = ∫ x in a/c..b/c, f x := by by_cases hc : c = 0; simp [hc] @[simp] lemma integral_comp_add_right (d) : ∫ x in a..b, f (x + d) = ∫ x in a+d..b+d, f x := have A : measurable_embedding (λ x, x + d) := (homeomorph.add_right d).closed_embedding.measurable_embedding, calc ∫ x in a..b, f (x + d) = ∫ x in a+d..b+d, f x ∂(measure.map (λ x, x + d) volume) : by simp [interval_integral, A.set_integral_map] ... = ∫ x in a+d..b+d, f x : by rw [map_add_right_eq_self] @[simp] lemma integral_comp_add_left (d) : ∫ x in a..b, f (d + x) = ∫ x in d+a..d+b, f x := by simpa only [add_comm] using integral_comp_add_right f d @[simp] lemma integral_comp_mul_add (hc : c ≠ 0) (d) : ∫ x in a..b, f (c * x + d) = c⁻¹ • ∫ x in c*a+d..c*b+d, f x := by rw [← integral_comp_add_right, ← integral_comp_mul_left _ hc] @[simp] lemma smul_integral_comp_mul_add (c d) : c • ∫ x in a..b, f (c * x + d) = ∫ x in c*a+d..c*b+d, f x := by by_cases hc : c = 0; simp [hc] @[simp] lemma integral_comp_add_mul (hc : c ≠ 0) (d) : ∫ x in a..b, f (d + c * x) = c⁻¹ • ∫ x in d+c*a..d+c*b, f x := by rw [← integral_comp_add_left, ← integral_comp_mul_left _ hc] @[simp] lemma smul_integral_comp_add_mul (c d) : c • ∫ x in a..b, f (d + c * x) = ∫ x in d+c*a..d+c*b, f x := by by_cases hc : c = 0; simp [hc] @[simp] lemma integral_comp_div_add (hc : c ≠ 0) (d) : ∫ x in a..b, f (x / c + d) = c • ∫ x in a/c+d..b/c+d, f x := by simpa only [div_eq_inv_mul, inv_inv] using integral_comp_mul_add f (inv_ne_zero hc) d @[simp] lemma inv_smul_integral_comp_div_add (c d) : c⁻¹ • ∫ x in a..b, f (x / c + d) = ∫ x in a/c+d..b/c+d, f x := by by_cases hc : c = 0; simp [hc] @[simp] lemma integral_comp_add_div (hc : c ≠ 0) (d) : ∫ x in a..b, f (d + x / c) = c • ∫ x in d+a/c..d+b/c, f x := by simpa only [div_eq_inv_mul, inv_inv] using integral_comp_add_mul f (inv_ne_zero hc) d @[simp] lemma inv_smul_integral_comp_add_div (c d) : c⁻¹ • ∫ x in a..b, f (d + x / c) = ∫ x in d+a/c..d+b/c, f x := by by_cases hc : c = 0; simp [hc] @[simp] lemma integral_comp_mul_sub (hc : c ≠ 0) (d) : ∫ x in a..b, f (c * x - d) = c⁻¹ • ∫ x in c*a-d..c*b-d, f x := by simpa only [sub_eq_add_neg] using integral_comp_mul_add f hc (-d) @[simp] lemma smul_integral_comp_mul_sub (c d) : c • ∫ x in a..b, f (c * x - d) = ∫ x in c*a-d..c*b-d, f x := by by_cases hc : c = 0; simp [hc] @[simp] lemma integral_comp_sub_mul (hc : c ≠ 0) (d) : ∫ x in a..b, f (d - c * x) = c⁻¹ • ∫ x in d-c*b..d-c*a, f x := begin simp only [sub_eq_add_neg, neg_mul_eq_neg_mul], rw [integral_comp_add_mul f (neg_ne_zero.mpr hc) d, integral_symm], simp only [inv_neg, smul_neg, neg_neg, neg_smul], end @[simp] lemma smul_integral_comp_sub_mul (c d) : c • ∫ x in a..b, f (d - c * x) = ∫ x in d-c*b..d-c*a, f x := by by_cases hc : c = 0; simp [hc] @[simp] lemma integral_comp_div_sub (hc : c ≠ 0) (d) : ∫ x in a..b, f (x / c - d) = c • ∫ x in a/c-d..b/c-d, f x := by simpa only [div_eq_inv_mul, inv_inv] using integral_comp_mul_sub f (inv_ne_zero hc) d @[simp] lemma inv_smul_integral_comp_div_sub (c d) : c⁻¹ • ∫ x in a..b, f (x / c - d) = ∫ x in a/c-d..b/c-d, f x := by by_cases hc : c = 0; simp [hc] @[simp] lemma integral_comp_sub_div (hc : c ≠ 0) (d) : ∫ x in a..b, f (d - x / c) = c • ∫ x in d-b/c..d-a/c, f x := by simpa only [div_eq_inv_mul, inv_inv] using integral_comp_sub_mul f (inv_ne_zero hc) d @[simp] lemma inv_smul_integral_comp_sub_div (c d) : c⁻¹ • ∫ x in a..b, f (d - x / c) = ∫ x in d-b/c..d-a/c, f x := by by_cases hc : c = 0; simp [hc] @[simp] lemma integral_comp_sub_right (d) : ∫ x in a..b, f (x - d) = ∫ x in a-d..b-d, f x := by simpa only [sub_eq_add_neg] using integral_comp_add_right f (-d) @[simp] lemma integral_comp_sub_left (d) : ∫ x in a..b, f (d - x) = ∫ x in d-b..d-a, f x := by simpa only [one_mul, one_smul, inv_one] using integral_comp_sub_mul f one_ne_zero d @[simp] lemma integral_comp_neg : ∫ x in a..b, f (-x) = ∫ x in -b..-a, f x := by simpa only [zero_sub] using integral_comp_sub_left f 0 end comp /-! ### Integral is an additive function of the interval In this section we prove that `∫ x in a..b, f x ∂μ + ∫ x in b..c, f x ∂μ = ∫ x in a..c, f x ∂μ` as well as a few other identities trivially equivalent to this one. We also prove that `∫ x in a..b, f x ∂μ = ∫ x, f x ∂μ` provided that `support f ⊆ Ioc a b`. -/ section order_closed_topology variables {a b c d : ℝ} {f g : ℝ → E} {μ : measure ℝ} /-- If two functions are equal in the relevant interval, their interval integrals are also equal. -/ lemma integral_congr {a b : ℝ} (h : eq_on f g [a, b]) : ∫ x in a..b, f x ∂μ = ∫ x in a..b, g x ∂μ := by cases le_total a b with hab hab; simpa [hab, integral_of_le, integral_of_ge] using set_integral_congr measurable_set_Ioc (h.mono Ioc_subset_Icc_self) lemma integral_add_adjacent_intervals_cancel (hab : interval_integrable f μ a b) (hbc : interval_integrable f μ b c) : ∫ x in a..b, f x ∂μ + ∫ x in b..c, f x ∂μ + ∫ x in c..a, f x ∂μ = 0 := begin have hac := hab.trans hbc, simp only [interval_integral, sub_add_sub_comm, sub_eq_zero], iterate 4 { rw ← integral_union }, { suffices : Ioc a b ∪ Ioc b c ∪ Ioc c a = Ioc b a ∪ Ioc c b ∪ Ioc a c, by rw this, rw [Ioc_union_Ioc_union_Ioc_cycle, union_right_comm, Ioc_union_Ioc_union_Ioc_cycle, min_left_comm, max_left_comm] }, all_goals { simp [*, measurable_set.union, measurable_set_Ioc, Ioc_disjoint_Ioc_same, Ioc_disjoint_Ioc_same.symm, hab.1, hab.2, hbc.1, hbc.2, hac.1, hac.2] } end lemma integral_add_adjacent_intervals (hab : interval_integrable f μ a b) (hbc : interval_integrable f μ b c) : ∫ x in a..b, f x ∂μ + ∫ x in b..c, f x ∂μ = ∫ x in a..c, f x ∂μ := by rw [← add_neg_eq_zero, ← integral_symm, integral_add_adjacent_intervals_cancel hab hbc] lemma sum_integral_adjacent_intervals_Ico {a : ℕ → ℝ} {m n : ℕ} (hmn : m ≤ n) (hint : ∀ k ∈ Ico m n, interval_integrable f μ (a k) (a $ k+1)) : ∑ (k : ℕ) in finset.Ico m n, ∫ x in (a k)..(a $ k+1), f x ∂μ = ∫ x in (a m)..(a n), f x ∂μ := begin revert hint, refine nat.le_induction _ _ n hmn, { simp }, { assume p hmp IH h, rw [finset.sum_Ico_succ_top hmp, IH, integral_add_adjacent_intervals], { apply interval_integrable.trans_iterate_Ico hmp (λ k hk, h k _), exact (Ico_subset_Ico le_rfl (nat.le_succ _)) hk }, { apply h, simp [hmp] }, { assume k hk, exact h _ (Ico_subset_Ico_right p.le_succ hk) } } end lemma sum_integral_adjacent_intervals {a : ℕ → ℝ} {n : ℕ} (hint : ∀ k < n, interval_integrable f μ (a k) (a $ k+1)) : ∑ (k : ℕ) in finset.range n, ∫ x in (a k)..(a $ k+1), f x ∂μ = ∫ x in (a 0)..(a n), f x ∂μ := begin rw ← nat.Ico_zero_eq_range, exact sum_integral_adjacent_intervals_Ico (zero_le n) (λ k hk, hint k hk.2), end lemma integral_interval_sub_left (hab : interval_integrable f μ a b) (hac : interval_integrable f μ a c) : ∫ x in a..b, f x ∂μ - ∫ x in a..c, f x ∂μ = ∫ x in c..b, f x ∂μ := sub_eq_of_eq_add' $ eq.symm $ integral_add_adjacent_intervals hac (hac.symm.trans hab) lemma integral_interval_add_interval_comm (hab : interval_integrable f μ a b) (hcd : interval_integrable f μ c d) (hac : interval_integrable f μ a c) : ∫ x in a..b, f x ∂μ + ∫ x in c..d, f x ∂μ = ∫ x in a..d, f x ∂μ + ∫ x in c..b, f x ∂μ := by rw [← integral_add_adjacent_intervals hac hcd, add_assoc, add_left_comm, integral_add_adjacent_intervals hac (hac.symm.trans hab), add_comm] lemma integral_interval_sub_interval_comm (hab : interval_integrable f μ a b) (hcd : interval_integrable f μ c d) (hac : interval_integrable f μ a c) : ∫ x in a..b, f x ∂μ - ∫ x in c..d, f x ∂μ = ∫ x in a..c, f x ∂μ - ∫ x in b..d, f x ∂μ := by simp only [sub_eq_add_neg, ← integral_symm, integral_interval_add_interval_comm hab hcd.symm (hac.trans hcd)] lemma integral_interval_sub_interval_comm' (hab : interval_integrable f μ a b) (hcd : interval_integrable f μ c d) (hac : interval_integrable f μ a c) : ∫ x in a..b, f x ∂μ - ∫ x in c..d, f x ∂μ = ∫ x in d..b, f x ∂μ - ∫ x in c..a, f x ∂μ := by { rw [integral_interval_sub_interval_comm hab hcd hac, integral_symm b d, integral_symm a c, sub_neg_eq_add, sub_eq_neg_add], } lemma integral_Iic_sub_Iic (ha : integrable_on f (Iic a) μ) (hb : integrable_on f (Iic b) μ) : ∫ x in Iic b, f x ∂μ - ∫ x in Iic a, f x ∂μ = ∫ x in a..b, f x ∂μ := begin wlog hab : a ≤ b using [a b] tactic.skip, { rw [sub_eq_iff_eq_add', integral_of_le hab, ← integral_union (Iic_disjoint_Ioc le_rfl), Iic_union_Ioc_eq_Iic hab], exacts [measurable_set_Ioc, ha, hb.mono_set (λ _, and.right)] }, { intros ha hb, rw [integral_symm, ← this hb ha, neg_sub] } end /-- If `μ` is a finite measure then `∫ x in a..b, c ∂μ = (μ (Iic b) - μ (Iic a)) • c`. -/ lemma integral_const_of_cdf [is_finite_measure μ] (c : E) : ∫ x in a..b, c ∂μ = ((μ (Iic b)).to_real - (μ (Iic a)).to_real) • c := begin simp only [sub_smul, ← set_integral_const], refine (integral_Iic_sub_Iic _ _).symm; simp only [integrable_on_const, measure_lt_top, or_true] end lemma integral_eq_integral_of_support_subset {a b} (h : support f ⊆ Ioc a b) : ∫ x in a..b, f x ∂μ = ∫ x, f x ∂μ := begin cases le_total a b with hab hab, { rw [integral_of_le hab, ← integral_indicator measurable_set_Ioc, indicator_eq_self.2 h]; apply_instance }, { rw [Ioc_eq_empty hab.not_lt, subset_empty_iff, support_eq_empty_iff] at h, simp [h] } end lemma integral_congr_ae' (h : ∀ᵐ x ∂μ, x ∈ Ioc a b → f x = g x) (h' : ∀ᵐ x ∂μ, x ∈ Ioc b a → f x = g x) : ∫ x in a..b, f x ∂μ = ∫ x in a..b, g x ∂μ := by simp only [interval_integral, set_integral_congr_ae (measurable_set_Ioc) h, set_integral_congr_ae (measurable_set_Ioc) h'] lemma integral_congr_ae (h : ∀ᵐ x ∂μ, x ∈ Ι a b → f x = g x) : ∫ x in a..b, f x ∂μ = ∫ x in a..b, g x ∂μ := integral_congr_ae' (ae_interval_oc_iff.mp h).1 (ae_interval_oc_iff.mp h).2 lemma integral_zero_ae (h : ∀ᵐ x ∂μ, x ∈ Ι a b → f x = 0) : ∫ x in a..b, f x ∂μ = 0 := calc ∫ x in a..b, f x ∂μ = ∫ x in a..b, 0 ∂μ : integral_congr_ae h ... = 0 : integral_zero lemma integral_indicator {a₁ a₂ a₃ : ℝ} (h : a₂ ∈ Icc a₁ a₃) : ∫ x in a₁..a₃, indicator {x | x ≤ a₂} f x ∂ μ = ∫ x in a₁..a₂, f x ∂ μ := begin have : {x | x ≤ a₂} ∩ Ioc a₁ a₃ = Ioc a₁ a₂, from Iic_inter_Ioc_of_le h.2, rw [integral_of_le h.1, integral_of_le (h.1.trans h.2), integral_indicator, measure.restrict_restrict, this], exact measurable_set_Iic, all_goals { apply measurable_set_Iic }, end /-- Lebesgue dominated convergence theorem for filters with a countable basis -/ lemma tendsto_integral_filter_of_dominated_convergence {ι} {l : filter ι} [l.is_countably_generated] {F : ι → ℝ → E} (bound : ℝ → ℝ) (hF_meas : ∀ᶠ n in l, ae_strongly_measurable (F n) (μ.restrict (Ι a b))) (h_bound : ∀ᶠ n in l, ∀ᵐ x ∂μ, x ∈ Ι a b → ∥F n x∥ ≤ bound x) (bound_integrable : interval_integrable bound μ a b) (h_lim : ∀ᵐ x ∂μ, x ∈ Ι a b → tendsto (λ n, F n x) l (𝓝 (f x))) : tendsto (λn, ∫ x in a..b, F n x ∂μ) l (𝓝 $ ∫ x in a..b, f x ∂μ) := begin simp only [interval_integrable_iff, interval_integral_eq_integral_interval_oc, ← ae_restrict_iff' measurable_set_interval_oc] at *, exact tendsto_const_nhds.smul (tendsto_integral_filter_of_dominated_convergence bound hF_meas h_bound bound_integrable h_lim) end /-- Lebesgue dominated convergence theorem for series. -/ lemma has_sum_integral_of_dominated_convergence {ι} [encodable ι] {F : ι → ℝ → E} (bound : ι → ℝ → ℝ) (hF_meas : ∀ n, ae_strongly_measurable (F n) (μ.restrict (Ι a b))) (h_bound : ∀ n, ∀ᵐ t ∂μ, t ∈ Ι a b → ∥F n t∥ ≤ bound n t) (bound_summable : ∀ᵐ t ∂μ, t ∈ Ι a b → summable (λ n, bound n t)) (bound_integrable : interval_integrable (λ t, ∑' n, bound n t) μ a b) (h_lim : ∀ᵐ t ∂μ, t ∈ Ι a b → has_sum (λ n, F n t) (f t)) : has_sum (λn, ∫ t in a..b, F n t ∂μ) (∫ t in a..b, f t ∂μ) := begin simp only [interval_integrable_iff, interval_integral_eq_integral_interval_oc, ← ae_restrict_iff' measurable_set_interval_oc] at *, exact (has_sum_integral_of_dominated_convergence bound hF_meas h_bound bound_summable bound_integrable h_lim).const_smul end open topological_space variables {X : Type*} [topological_space X] [first_countable_topology X] /-- Continuity of interval integral with respect to a parameter, at a point within a set. Given `F : X → ℝ → E`, assume `F x` is ae-measurable on `[a, b]` for `x` in a neighborhood of `x₀` within `s` and at `x₀`, and assume it is bounded by a function integrable on `[a, b]` independent of `x` in a neighborhood of `x₀` within `s`. If `(λ x, F x t)` is continuous at `x₀` within `s` for almost every `t` in `[a, b]` then the same holds for `(λ x, ∫ t in a..b, F x t ∂μ) s x₀`. -/ lemma continuous_within_at_of_dominated_interval {F : X → ℝ → E} {x₀ : X} {bound : ℝ → ℝ} {a b : ℝ} {s : set X} (hF_meas : ∀ᶠ x in 𝓝[s] x₀, ae_strongly_measurable (F x) (μ.restrict $ Ι a b)) (h_bound : ∀ᶠ x in 𝓝[s] x₀, ∀ᵐ t ∂μ, t ∈ Ι a b → ∥F x t∥ ≤ bound t) (bound_integrable : interval_integrable bound μ a b) (h_cont : ∀ᵐ t ∂μ, t ∈ Ι a b → continuous_within_at (λ x, F x t) s x₀) : continuous_within_at (λ x, ∫ t in a..b, F x t ∂μ) s x₀ := tendsto_integral_filter_of_dominated_convergence bound hF_meas h_bound bound_integrable h_cont /-- Continuity of interval integral with respect to a parameter at a point. Given `F : X → ℝ → E`, assume `F x` is ae-measurable on `[a, b]` for `x` in a neighborhood of `x₀`, and assume it is bounded by a function integrable on `[a, b]` independent of `x` in a neighborhood of `x₀`. If `(λ x, F x t)` is continuous at `x₀` for almost every `t` in `[a, b]` then the same holds for `(λ x, ∫ t in a..b, F x t ∂μ) s x₀`. -/ lemma continuous_at_of_dominated_interval {F : X → ℝ → E} {x₀ : X} {bound : ℝ → ℝ} {a b : ℝ} (hF_meas : ∀ᶠ x in 𝓝 x₀, ae_strongly_measurable (F x) (μ.restrict $ Ι a b)) (h_bound : ∀ᶠ x in 𝓝 x₀, ∀ᵐ t ∂μ, t ∈ Ι a b → ∥F x t∥ ≤ bound t) (bound_integrable : interval_integrable bound μ a b) (h_cont : ∀ᵐ t ∂μ, t ∈ Ι a b → continuous_at (λ x, F x t) x₀) : continuous_at (λ x, ∫ t in a..b, F x t ∂μ) x₀ := tendsto_integral_filter_of_dominated_convergence bound hF_meas h_bound bound_integrable h_cont /-- Continuity of interval integral with respect to a parameter. Given `F : X → ℝ → E`, assume each `F x` is ae-measurable on `[a, b]`, and assume it is bounded by a function integrable on `[a, b]` independent of `x`. If `(λ x, F x t)` is continuous for almost every `t` in `[a, b]` then the same holds for `(λ x, ∫ t in a..b, F x t ∂μ) s x₀`. -/ lemma continuous_of_dominated_interval {F : X → ℝ → E} {bound : ℝ → ℝ} {a b : ℝ} (hF_meas : ∀ x, ae_strongly_measurable (F x) $ μ.restrict $ Ι a b) (h_bound : ∀ x, ∀ᵐ t ∂μ, t ∈ Ι a b → ∥F x t∥ ≤ bound t) (bound_integrable : interval_integrable bound μ a b) (h_cont : ∀ᵐ t ∂μ, t ∈ Ι a b → continuous (λ x, F x t)) : continuous (λ x, ∫ t in a..b, F x t ∂μ) := continuous_iff_continuous_at.mpr (λ x₀, continuous_at_of_dominated_interval (eventually_of_forall hF_meas) (eventually_of_forall h_bound) bound_integrable $ h_cont.mono $ λ x himp hx, (himp hx).continuous_at) end order_closed_topology section continuous_primitive open topological_space variables {a b b₀ b₁ b₂ : ℝ} {μ : measure ℝ} {f g : ℝ → E} lemma continuous_within_at_primitive (hb₀ : μ {b₀} = 0) (h_int : interval_integrable f μ (min a b₁) (max a b₂)) : continuous_within_at (λ b, ∫ x in a .. b, f x ∂ μ) (Icc b₁ b₂) b₀ := begin by_cases h₀ : b₀ ∈ Icc b₁ b₂, { have h₁₂ : b₁ ≤ b₂ := h₀.1.trans h₀.2, have min₁₂ : min b₁ b₂ = b₁ := min_eq_left h₁₂, have h_int' : ∀ {x}, x ∈ Icc b₁ b₂ → interval_integrable f μ b₁ x, { rintros x ⟨h₁, h₂⟩, apply h_int.mono_set, apply interval_subset_interval, { exact ⟨min_le_of_left_le (min_le_right a b₁), h₁.trans (h₂.trans $ le_max_of_le_right $ le_max_right _ _)⟩ }, { exact ⟨min_le_of_left_le $ (min_le_right _ _).trans h₁, le_max_of_le_right $ h₂.trans $ le_max_right _ _⟩ } }, have : ∀ b ∈ Icc b₁ b₂, ∫ x in a..b, f x ∂μ = ∫ x in a..b₁, f x ∂μ + ∫ x in b₁..b, f x ∂μ, { rintros b ⟨h₁, h₂⟩, rw ← integral_add_adjacent_intervals _ (h_int' ⟨h₁, h₂⟩), apply h_int.mono_set, apply interval_subset_interval, { exact ⟨min_le_of_left_le (min_le_left a b₁), le_max_of_le_right (le_max_left _ _)⟩ }, { exact ⟨min_le_of_left_le (min_le_right _ _), le_max_of_le_right (h₁.trans $ h₂.trans (le_max_right a b₂))⟩ } }, apply continuous_within_at.congr _ this (this _ h₀), clear this, refine continuous_within_at_const.add _, have : (λ b, ∫ x in b₁..b, f x ∂μ) =ᶠ[𝓝[Icc b₁ b₂] b₀] λ b, ∫ x in b₁..b₂, indicator {x | x ≤ b} f x ∂ μ, { apply eventually_eq_of_mem self_mem_nhds_within, exact λ b b_in, (integral_indicator b_in).symm }, apply continuous_within_at.congr_of_eventually_eq _ this (integral_indicator h₀).symm, have : interval_integrable (λ x, ∥f x∥) μ b₁ b₂, from interval_integrable.norm (h_int' $ right_mem_Icc.mpr h₁₂), refine continuous_within_at_of_dominated_interval _ _ this _ ; clear this, { apply eventually.mono (self_mem_nhds_within), intros x hx, erw [ae_strongly_measurable_indicator_iff, measure.restrict_restrict, Iic_inter_Ioc_of_le], { rw min₁₂, exact (h_int' hx).1.ae_strongly_measurable }, { exact le_max_of_le_right hx.2 }, exacts [measurable_set_Iic, measurable_set_Iic] }, { refine eventually_of_forall (λ x, eventually_of_forall (λ t, _)), dsimp [indicator], split_ifs ; simp }, { have : ∀ᵐ t ∂μ, t < b₀ ∨ b₀ < t, { apply eventually.mono (compl_mem_ae_iff.mpr hb₀), intros x hx, exact ne.lt_or_lt hx }, apply this.mono, rintros x₀ (hx₀ | hx₀) -, { have : ∀ᶠ x in 𝓝[Icc b₁ b₂] b₀, {t : ℝ | t ≤ x}.indicator f x₀ = f x₀, { apply mem_nhds_within_of_mem_nhds, apply eventually.mono (Ioi_mem_nhds hx₀), intros x hx, simp [hx.le] }, apply continuous_within_at_const.congr_of_eventually_eq this, simp [hx₀.le] }, { have : ∀ᶠ x in 𝓝[Icc b₁ b₂] b₀, {t : ℝ | t ≤ x}.indicator f x₀ = 0, { apply mem_nhds_within_of_mem_nhds, apply eventually.mono (Iio_mem_nhds hx₀), intros x hx, simp [hx] }, apply continuous_within_at_const.congr_of_eventually_eq this, simp [hx₀] } } }, { apply continuous_within_at_of_not_mem_closure, rwa [closure_Icc] } end lemma continuous_on_primitive [has_no_atoms μ] (h_int : integrable_on f (Icc a b) μ) : continuous_on (λ x, ∫ t in Ioc a x, f t ∂ μ) (Icc a b) := begin by_cases h : a ≤ b, { have : ∀ x ∈ Icc a b, ∫ t in Ioc a x, f t ∂μ = ∫ t in a..x, f t ∂μ, { intros x x_in, simp_rw [← interval_oc_of_le h, integral_of_le x_in.1] }, rw continuous_on_congr this, intros x₀ hx₀, refine continuous_within_at_primitive (measure_singleton x₀) _, simp only [interval_integrable_iff_integrable_Ioc_of_le, min_eq_left, max_eq_right, h], exact h_int.mono Ioc_subset_Icc_self le_rfl }, { rw Icc_eq_empty h, exact continuous_on_empty _ }, end lemma continuous_on_primitive_Icc [has_no_atoms μ] (h_int : integrable_on f (Icc a b) μ) : continuous_on (λ x, ∫ t in Icc a x, f t ∂ μ) (Icc a b) := begin rw show (λ x, ∫ t in Icc a x, f t ∂μ) = λ x, ∫ t in Ioc a x, f t ∂μ, by { ext x, exact integral_Icc_eq_integral_Ioc }, exact continuous_on_primitive h_int end /-- Note: this assumes that `f` is `interval_integrable`, in contrast to some other lemmas here. -/ lemma continuous_on_primitive_interval' [has_no_atoms μ] (h_int : interval_integrable f μ b₁ b₂) (ha : a ∈ [b₁, b₂]) : continuous_on (λ b, ∫ x in a..b, f x ∂ μ) [b₁, b₂] := begin intros b₀ hb₀, refine continuous_within_at_primitive (measure_singleton _) _, rw [min_eq_right ha.1, max_eq_right ha.2], simpa [interval_integrable_iff, interval_oc] using h_int, end lemma continuous_on_primitive_interval [has_no_atoms μ] (h_int : integrable_on f (interval a b) μ) : continuous_on (λ x, ∫ t in a..x, f t ∂ μ) (interval a b) := continuous_on_primitive_interval' h_int.interval_integrable left_mem_interval lemma continuous_on_primitive_interval_left [has_no_atoms μ] (h_int : integrable_on f (interval a b) μ) : continuous_on (λ x, ∫ t in x..b, f t ∂ μ) (interval a b) := begin rw interval_swap a b at h_int ⊢, simp only [integral_symm b], exact (continuous_on_primitive_interval h_int).neg, end variables [has_no_atoms μ] lemma continuous_primitive (h_int : ∀ a b, interval_integrable f μ a b) (a : ℝ) : continuous (λ b, ∫ x in a..b, f x ∂ μ) := begin rw continuous_iff_continuous_at, intro b₀, cases exists_lt b₀ with b₁ hb₁, cases exists_gt b₀ with b₂ hb₂, apply continuous_within_at.continuous_at _ (Icc_mem_nhds hb₁ hb₂), exact continuous_within_at_primitive (measure_singleton b₀) (h_int _ _) end lemma _root_.measure_theory.integrable.continuous_primitive (h_int : integrable f μ) (a : ℝ) : continuous (λ b, ∫ x in a..b, f x ∂ μ) := continuous_primitive (λ _ _, h_int.interval_integrable) a end continuous_primitive section variables {f g : ℝ → ℝ} {a b : ℝ} {μ : measure ℝ} lemma integral_eq_zero_iff_of_le_of_nonneg_ae (hab : a ≤ b) (hf : 0 ≤ᵐ[μ.restrict (Ioc a b)] f) (hfi : interval_integrable f μ a b) : ∫ x in a..b, f x ∂μ = 0 ↔ f =ᵐ[μ.restrict (Ioc a b)] 0 := by rw [integral_of_le hab, integral_eq_zero_iff_of_nonneg_ae hf hfi.1] lemma integral_eq_zero_iff_of_nonneg_ae (hf : 0 ≤ᵐ[μ.restrict (Ioc a b ∪ Ioc b a)] f) (hfi : interval_integrable f μ a b) : ∫ x in a..b, f x ∂μ = 0 ↔ f =ᵐ[μ.restrict (Ioc a b ∪ Ioc b a)] 0 := begin cases le_total a b with hab hab; simp only [Ioc_eq_empty hab.not_lt, empty_union, union_empty] at hf ⊢, { exact integral_eq_zero_iff_of_le_of_nonneg_ae hab hf hfi }, { rw [integral_symm, neg_eq_zero, integral_eq_zero_iff_of_le_of_nonneg_ae hab hf hfi.symm] } end /-- If `f` is nonnegative and integrable on the unordered interval `set.interval_oc a b`, then its integral over `a..b` is positive if and only if `a < b` and the measure of `function.support f ∩ set.Ioc a b` is positive. -/ lemma integral_pos_iff_support_of_nonneg_ae' (hf : 0 ≤ᵐ[μ.restrict (Ι a b)] f) (hfi : interval_integrable f μ a b) : 0 < ∫ x in a..b, f x ∂μ ↔ a < b ∧ 0 < μ (support f ∩ Ioc a b) := begin cases lt_or_le a b with hab hba, { rw interval_oc_of_le hab.le at hf, simp only [hab, true_and, integral_of_le hab.le, set_integral_pos_iff_support_of_nonneg_ae hf hfi.1] }, { suffices : ∫ x in a..b, f x ∂μ ≤ 0, by simp only [this.not_lt, hba.not_lt, false_and], rw [integral_of_ge hba, neg_nonpos], rw [interval_oc_swap, interval_oc_of_le hba] at hf, exact integral_nonneg_of_ae hf } end /-- If `f` is nonnegative a.e.-everywhere and it is integrable on the unordered interval `set.interval_oc a b`, then its integral over `a..b` is positive if and only if `a < b` and the measure of `function.support f ∩ set.Ioc a b` is positive. -/ lemma integral_pos_iff_support_of_nonneg_ae (hf : 0 ≤ᵐ[μ] f) (hfi : interval_integrable f μ a b) : 0 < ∫ x in a..b, f x ∂μ ↔ a < b ∧ 0 < μ (support f ∩ Ioc a b) := integral_pos_iff_support_of_nonneg_ae' (ae_mono measure.restrict_le_self hf) hfi /-- If `f : ℝ → ℝ` is strictly positive and integrable on `(a, b]` for real numbers `a < b`, then its integral over `a..b` is strictly positive. -/ lemma interval_integral_pos_of_pos {f : ℝ → ℝ} {a b : ℝ} (hfi : interval_integrable f measure_space.volume a b) (h : ∀ x, 0 < f x) (hab : a < b) : 0 < ∫ x in a..b, f x := begin have hsupp : support f = univ := eq_univ_iff_forall.mpr (λ t, (h t).ne.symm), replace h₀ : 0 ≤ᵐ[volume] f := eventually_of_forall (λ x, (h x).le), rw integral_pos_iff_support_of_nonneg_ae h₀ hfi, exact ⟨hab, by simp [hsupp, hab]⟩, end /-- If `f` and `g` are two functions that are interval integrable on `a..b`, `a ≤ b`, `f x ≤ g x` for a.e. `x ∈ set.Ioc a b`, and `f x < g x` on a subset of `set.Ioc a b` of nonzero measure, then `∫ x in a..b, f x ∂μ < ∫ x in a..b, g x ∂μ`. -/ lemma integral_lt_integral_of_ae_le_of_measure_set_of_lt_ne_zero (hab : a ≤ b) (hfi : interval_integrable f μ a b) (hgi : interval_integrable g μ a b) (hle : f ≤ᵐ[μ.restrict (Ioc a b)] g) (hlt : μ.restrict (Ioc a b) {x | f x < g x} ≠ 0) : ∫ x in a..b, f x ∂μ < ∫ x in a..b, g x ∂μ := begin rw [← sub_pos, ← integral_sub hgi hfi, integral_of_le hab, measure_theory.integral_pos_iff_support_of_nonneg_ae], { refine pos_iff_ne_zero.2 (mt (measure_mono_null _) hlt), exact λ x hx, (sub_pos.2 hx).ne' }, exacts [hle.mono (λ x, sub_nonneg.2), hgi.1.sub hfi.1] end /-- If `f` and `g` are continuous on `[a, b]`, `a < b`, `f x ≤ g x` on this interval, and `f c < g c` at some point `c ∈ [a, b]`, then `∫ x in a..b, f x < ∫ x in a..b, g x`. -/ lemma integral_lt_integral_of_continuous_on_of_le_of_exists_lt {f g : ℝ → ℝ} {a b : ℝ} (hab : a < b) (hfc : continuous_on f (Icc a b)) (hgc : continuous_on g (Icc a b)) (hle : ∀ x ∈ Ioc a b, f x ≤ g x) (hlt : ∃ c ∈ Icc a b, f c < g c) : ∫ x in a..b, f x < ∫ x in a..b, g x := begin refine integral_lt_integral_of_ae_le_of_measure_set_of_lt_ne_zero hab.le (hfc.interval_integrable_of_Icc hab.le) (hgc.interval_integrable_of_Icc hab.le) ((ae_restrict_mem measurable_set_Ioc).mono hle) _, contrapose! hlt, have h_eq : f =ᵐ[volume.restrict (Ioc a b)] g, { simp only [← not_le, ← ae_iff] at hlt, exact eventually_le.antisymm ((ae_restrict_iff' measurable_set_Ioc).2 $ eventually_of_forall hle) hlt }, simp only [measure.restrict_congr_set Ioc_ae_eq_Icc] at h_eq, exact λ c hc, (measure.eq_on_Icc_of_ae_eq volume hab.ne h_eq hfc hgc hc).ge end lemma integral_nonneg_of_ae_restrict (hab : a ≤ b) (hf : 0 ≤ᵐ[μ.restrict (Icc a b)] f) : 0 ≤ (∫ u in a..b, f u ∂μ) := let H := ae_restrict_of_ae_restrict_of_subset Ioc_subset_Icc_self hf in by simpa only [integral_of_le hab] using set_integral_nonneg_of_ae_restrict H lemma integral_nonneg_of_ae (hab : a ≤ b) (hf : 0 ≤ᵐ[μ] f) : 0 ≤ (∫ u in a..b, f u ∂μ) := integral_nonneg_of_ae_restrict hab $ ae_restrict_of_ae hf lemma integral_nonneg_of_forall (hab : a ≤ b) (hf : ∀ u, 0 ≤ f u) : 0 ≤ (∫ u in a..b, f u ∂μ) := integral_nonneg_of_ae hab $ eventually_of_forall hf lemma integral_nonneg (hab : a ≤ b) (hf : ∀ u, u ∈ Icc a b → 0 ≤ f u) : 0 ≤ (∫ u in a..b, f u ∂μ) := integral_nonneg_of_ae_restrict hab $ (ae_restrict_iff' measurable_set_Icc).mpr $ ae_of_all μ hf lemma abs_integral_le_integral_abs (hab : a ≤ b) : |∫ x in a..b, f x ∂μ| ≤ ∫ x in a..b, |f x| ∂μ := by simpa only [← real.norm_eq_abs] using norm_integral_le_integral_norm hab section mono variables (hab : a ≤ b) (hf : interval_integrable f μ a b) (hg : interval_integrable g μ a b) include hab hf hg lemma integral_mono_ae_restrict (h : f ≤ᵐ[μ.restrict (Icc a b)] g) : ∫ u in a..b, f u ∂μ ≤ ∫ u in a..b, g u ∂μ := let H := h.filter_mono $ ae_mono $ measure.restrict_mono Ioc_subset_Icc_self $ le_refl μ in by simpa only [integral_of_le hab] using set_integral_mono_ae_restrict hf.1 hg.1 H lemma integral_mono_ae (h : f ≤ᵐ[μ] g) : ∫ u in a..b, f u ∂μ ≤ ∫ u in a..b, g u ∂μ := by simpa only [integral_of_le hab] using set_integral_mono_ae hf.1 hg.1 h lemma integral_mono_on (h : ∀ x ∈ Icc a b, f x ≤ g x) : ∫ u in a..b, f u ∂μ ≤ ∫ u in a..b, g u ∂μ := let H := λ x hx, h x $ Ioc_subset_Icc_self hx in by simpa only [integral_of_le hab] using set_integral_mono_on hf.1 hg.1 measurable_set_Ioc H lemma integral_mono (h : f ≤ g) : ∫ u in a..b, f u ∂μ ≤ ∫ u in a..b, g u ∂μ := integral_mono_ae hab hf hg $ ae_of_all _ h omit hg hab lemma integral_mono_interval {c d} (hca : c ≤ a) (hab : a ≤ b) (hbd : b ≤ d) (hf : 0 ≤ᵐ[μ.restrict (Ioc c d)] f) (hfi : interval_integrable f μ c d): ∫ x in a..b, f x ∂μ ≤ ∫ x in c..d, f x ∂μ := begin rw [integral_of_le hab, integral_of_le (hca.trans (hab.trans hbd))], exact set_integral_mono_set hfi.1 hf (Ioc_subset_Ioc hca hbd).eventually_le end lemma abs_integral_mono_interval {c d } (h : Ι a b ⊆ Ι c d) (hf : 0 ≤ᵐ[μ.restrict (Ι c d)] f) (hfi : interval_integrable f μ c d) : |∫ x in a..b, f x ∂μ| ≤ |∫ x in c..d, f x ∂μ| := have hf' : 0 ≤ᵐ[μ.restrict (Ι a b)] f, from ae_mono (measure.restrict_mono h le_rfl) hf, calc |∫ x in a..b, f x ∂μ| = |∫ x in Ι a b, f x ∂μ| : abs_integral_eq_abs_integral_interval_oc f ... = ∫ x in Ι a b, f x ∂μ : abs_of_nonneg (measure_theory.integral_nonneg_of_ae hf') ... ≤ ∫ x in Ι c d, f x ∂μ : set_integral_mono_set hfi.def hf h.eventually_le ... ≤ |∫ x in Ι c d, f x ∂μ| : le_abs_self _ ... = |∫ x in c..d, f x ∂μ| : (abs_integral_eq_abs_integral_interval_oc f).symm end mono end /-! ### Fundamental theorem of calculus, part 1, for any measure In this section we prove a few lemmas that can be seen as versions of FTC-1 for interval integrals w.r.t. any measure. Many theorems are formulated for one or two pairs of filters related by `FTC_filter a l l'`. This typeclass has exactly four “real” instances: `(a, pure a, ⊥)`, `(a, 𝓝[≥] a, 𝓝[>] a)`, `(a, 𝓝[≤] a, 𝓝[≤] a)`, `(a, 𝓝 a, 𝓝 a)`, and two instances that are equal to the first and last “real” instances: `(a, 𝓝[{a}] a, ⊥)` and `(a, 𝓝[univ] a, 𝓝[univ] a)`. We use this approach to avoid repeating arguments in many very similar cases. Lean can automatically find both `a` and `l'` based on `l`. The most general theorem `measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae` can be seen as a generalization of lemma `integral_has_strict_fderiv_at` below which states strict differentiability of `∫ x in u..v, f x` in `(u, v)` at `(a, b)` for a measurable function `f` that is integrable on `a..b` and is continuous at `a` and `b`. The lemma is generalized in three directions: first, `measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae` deals with any locally finite measure `μ`; second, it works for one-sided limits/derivatives; third, it assumes only that `f` has finite limits almost surely at `a` and `b`. Namely, let `f` be a measurable function integrable on `a..b`. Let `(la, la')` be a pair of `FTC_filter`s around `a`; let `(lb, lb')` be a pair of `FTC_filter`s around `b`. Suppose that `f` has finite limits `ca` and `cb` at `la' ⊓ μ.ae` and `lb' ⊓ μ.ae`, respectively. Then `∫ x in va..vb, f x ∂μ - ∫ x in ua..ub, f x ∂μ = ∫ x in ub..vb, cb ∂μ - ∫ x in ua..va, ca ∂μ + o(∥∫ x in ua..va, (1:ℝ) ∂μ∥ + ∥∫ x in ub..vb, (1:ℝ) ∂μ∥)` as `ua` and `va` tend to `la` while `ub` and `vb` tend to `lb`. This theorem is formulated with integral of constants instead of measures in the right hand sides for two reasons: first, this way we avoid `min`/`max` in the statements; second, often it is possible to write better `simp` lemmas for these integrals, see `integral_const` and `integral_const_of_cdf`. In the next subsection we apply this theorem to prove various theorems about differentiability of the integral w.r.t. Lebesgue measure. -/ /-- An auxiliary typeclass for the Fundamental theorem of calculus, part 1. It is used to formulate theorems that work simultaneously for left and right one-sided derivatives of `∫ x in u..v, f x`. -/ class FTC_filter (a : out_param ℝ) (outer : filter ℝ) (inner : out_param $ filter ℝ) extends tendsto_Ixx_class Ioc outer inner : Prop := (pure_le : pure a ≤ outer) (le_nhds : inner ≤ 𝓝 a) [meas_gen : is_measurably_generated inner] /- The `dangerous_instance` linter doesn't take `out_param`s into account, so it thinks that `FTC_filter.to_tendsto_Ixx_class` is dangerous. Disable this linter using `nolint`. -/ attribute [nolint dangerous_instance] FTC_filter.to_tendsto_Ixx_class namespace FTC_filter instance pure (a : ℝ) : FTC_filter a (pure a) ⊥ := { pure_le := le_rfl, le_nhds := bot_le } instance nhds_within_singleton (a : ℝ) : FTC_filter a (𝓝[{a}] a) ⊥ := by { rw [nhds_within, principal_singleton, inf_eq_right.2 (pure_le_nhds a)], apply_instance } lemma finite_at_inner {a : ℝ} (l : filter ℝ) {l'} [h : FTC_filter a l l'] {μ : measure ℝ} [is_locally_finite_measure μ] : μ.finite_at_filter l' := (μ.finite_at_nhds a).filter_mono h.le_nhds instance nhds (a : ℝ) : FTC_filter a (𝓝 a) (𝓝 a) := { pure_le := pure_le_nhds a, le_nhds := le_rfl } instance nhds_univ (a : ℝ) : FTC_filter a (𝓝[univ] a) (𝓝 a) := by { rw nhds_within_univ, apply_instance } instance nhds_left (a : ℝ) : FTC_filter a (𝓝[≤] a) (𝓝[≤] a) := { pure_le := pure_le_nhds_within right_mem_Iic, le_nhds := inf_le_left } instance nhds_right (a : ℝ) : FTC_filter a (𝓝[≥] a) (𝓝[>] a) := { pure_le := pure_le_nhds_within left_mem_Ici, le_nhds := inf_le_left } instance nhds_Icc {x a b : ℝ} [h : fact (x ∈ Icc a b)] : FTC_filter x (𝓝[Icc a b] x) (𝓝[Icc a b] x) := { pure_le := pure_le_nhds_within h.out, le_nhds := inf_le_left } instance nhds_interval {x a b : ℝ} [h : fact (x ∈ [a, b])] : FTC_filter x (𝓝[[a, b]] x) (𝓝[[a, b]] x) := by { haveI : fact (x ∈ set.Icc (min a b) (max a b)) := h, exact FTC_filter.nhds_Icc } end FTC_filter open asymptotics section variables {f : ℝ → E} {a b : ℝ} {c ca cb : E} {l l' la la' lb lb' : filter ℝ} {lt : filter ι} {μ : measure ℝ} {u v ua va ub vb : ι → ℝ} /-- Fundamental theorem of calculus-1, local version for any measure. Let filters `l` and `l'` be related by `tendsto_Ixx_class Ioc`. If `f` has a finite limit `c` at `l' ⊓ μ.ae`, where `μ` is a measure finite at `l'`, then `∫ x in u..v, f x ∂μ = ∫ x in u..v, c ∂μ + o(∫ x in u..v, 1 ∂μ)` as both `u` and `v` tend to `l`. See also `measure_integral_sub_linear_is_o_of_tendsto_ae` for a version assuming `[FTC_filter a l l']` and `[is_locally_finite_measure μ]`. If `l` is one of `𝓝[≥] a`, `𝓝[≤] a`, `𝓝 a`, then it's easier to apply the non-primed version. The primed version also works, e.g., for `l = l' = at_top`. We use integrals of constants instead of measures because this way it is easier to formulate a statement that works in both cases `u ≤ v` and `v ≤ u`. -/ lemma measure_integral_sub_linear_is_o_of_tendsto_ae' [is_measurably_generated l'] [tendsto_Ixx_class Ioc l l'] (hfm : strongly_measurable_at_filter f l' μ) (hf : tendsto f (l' ⊓ μ.ae) (𝓝 c)) (hl : μ.finite_at_filter l') (hu : tendsto u lt l) (hv : tendsto v lt l) : (λ t, ∫ x in u t..v t, f x ∂μ - ∫ x in u t..v t, c ∂μ) =o[lt] (λ t, ∫ x in u t..v t, (1:ℝ) ∂μ) := begin have A := hf.integral_sub_linear_is_o_ae hfm hl (hu.Ioc hv), have B := hf.integral_sub_linear_is_o_ae hfm hl (hv.Ioc hu), simp only [integral_const'], convert (A.trans_le _).sub (B.trans_le _), { ext t, simp_rw [interval_integral, sub_smul], abel }, all_goals { intro t, cases le_total (u t) (v t) with huv huv; simp [huv] } end /-- Fundamental theorem of calculus-1, local version for any measure. Let filters `l` and `l'` be related by `tendsto_Ixx_class Ioc`. If `f` has a finite limit `c` at `l ⊓ μ.ae`, where `μ` is a measure finite at `l`, then `∫ x in u..v, f x ∂μ = μ (Ioc u v) • c + o(μ(Ioc u v))` as both `u` and `v` tend to `l` so that `u ≤ v`. See also `measure_integral_sub_linear_is_o_of_tendsto_ae_of_le` for a version assuming `[FTC_filter a l l']` and `[is_locally_finite_measure μ]`. If `l` is one of `𝓝[≥] a`, `𝓝[≤] a`, `𝓝 a`, then it's easier to apply the non-primed version. The primed version also works, e.g., for `l = l' = at_top`. -/ lemma measure_integral_sub_linear_is_o_of_tendsto_ae_of_le' [is_measurably_generated l'] [tendsto_Ixx_class Ioc l l'] (hfm : strongly_measurable_at_filter f l' μ) (hf : tendsto f (l' ⊓ μ.ae) (𝓝 c)) (hl : μ.finite_at_filter l') (hu : tendsto u lt l) (hv : tendsto v lt l) (huv : u ≤ᶠ[lt] v) : (λ t, ∫ x in u t..v t, f x ∂μ - (μ (Ioc (u t) (v t))).to_real • c) =o[lt] (λ t, (μ $ Ioc (u t) (v t)).to_real) := (measure_integral_sub_linear_is_o_of_tendsto_ae' hfm hf hl hu hv).congr' (huv.mono $ λ x hx, by simp [integral_const', hx]) (huv.mono $ λ x hx, by simp [integral_const', hx]) /-- Fundamental theorem of calculus-1, local version for any measure. Let filters `l` and `l'` be related by `tendsto_Ixx_class Ioc`. If `f` has a finite limit `c` at `l ⊓ μ.ae`, where `μ` is a measure finite at `l`, then `∫ x in u..v, f x ∂μ = -μ (Ioc v u) • c + o(μ(Ioc v u))` as both `u` and `v` tend to `l` so that `v ≤ u`. See also `measure_integral_sub_linear_is_o_of_tendsto_ae_of_ge` for a version assuming `[FTC_filter a l l']` and `[is_locally_finite_measure μ]`. If `l` is one of `𝓝[≥] a`, `𝓝[≤] a`, `𝓝 a`, then it's easier to apply the non-primed version. The primed version also works, e.g., for `l = l' = at_top`. -/ lemma measure_integral_sub_linear_is_o_of_tendsto_ae_of_ge' [is_measurably_generated l'] [tendsto_Ixx_class Ioc l l'] (hfm : strongly_measurable_at_filter f l' μ) (hf : tendsto f (l' ⊓ μ.ae) (𝓝 c)) (hl : μ.finite_at_filter l') (hu : tendsto u lt l) (hv : tendsto v lt l) (huv : v ≤ᶠ[lt] u) : (λ t, ∫ x in u t..v t, f x ∂μ + (μ (Ioc (v t) (u t))).to_real • c) =o[lt] (λ t, (μ $ Ioc (v t) (u t)).to_real) := (measure_integral_sub_linear_is_o_of_tendsto_ae_of_le' hfm hf hl hv hu huv).neg_left.congr_left $ λ t, by simp [integral_symm (u t), add_comm] section variables [is_locally_finite_measure μ] [FTC_filter a l l'] include a local attribute [instance] FTC_filter.meas_gen /-- Fundamental theorem of calculus-1, local version for any measure. Let filters `l` and `l'` be related by `[FTC_filter a l l']`; let `μ` be a locally finite measure. If `f` has a finite limit `c` at `l' ⊓ μ.ae`, then `∫ x in u..v, f x ∂μ = ∫ x in u..v, c ∂μ + o(∫ x in u..v, 1 ∂μ)` as both `u` and `v` tend to `l`. See also `measure_integral_sub_linear_is_o_of_tendsto_ae'` for a version that also works, e.g., for `l = l' = at_top`. We use integrals of constants instead of measures because this way it is easier to formulate a statement that works in both cases `u ≤ v` and `v ≤ u`. -/ lemma measure_integral_sub_linear_is_o_of_tendsto_ae (hfm : strongly_measurable_at_filter f l' μ) (hf : tendsto f (l' ⊓ μ.ae) (𝓝 c)) (hu : tendsto u lt l) (hv : tendsto v lt l) : (λ t, ∫ x in u t..v t, f x ∂μ - ∫ x in u t..v t, c ∂μ) =o[lt] (λ t, ∫ x in u t..v t, (1:ℝ) ∂μ) := measure_integral_sub_linear_is_o_of_tendsto_ae' hfm hf (FTC_filter.finite_at_inner l) hu hv /-- Fundamental theorem of calculus-1, local version for any measure. Let filters `l` and `l'` be related by `[FTC_filter a l l']`; let `μ` be a locally finite measure. If `f` has a finite limit `c` at `l' ⊓ μ.ae`, then `∫ x in u..v, f x ∂μ = μ (Ioc u v) • c + o(μ(Ioc u v))` as both `u` and `v` tend to `l`. See also `measure_integral_sub_linear_is_o_of_tendsto_ae_of_le'` for a version that also works, e.g., for `l = l' = at_top`. -/ lemma measure_integral_sub_linear_is_o_of_tendsto_ae_of_le (hfm : strongly_measurable_at_filter f l' μ) (hf : tendsto f (l' ⊓ μ.ae) (𝓝 c)) (hu : tendsto u lt l) (hv : tendsto v lt l) (huv : u ≤ᶠ[lt] v) : (λ t, ∫ x in u t..v t, f x ∂μ - (μ (Ioc (u t) (v t))).to_real • c) =o[lt] (λ t, (μ $ Ioc (u t) (v t)).to_real) := measure_integral_sub_linear_is_o_of_tendsto_ae_of_le' hfm hf (FTC_filter.finite_at_inner l) hu hv huv /-- Fundamental theorem of calculus-1, local version for any measure. Let filters `l` and `l'` be related by `[FTC_filter a l l']`; let `μ` be a locally finite measure. If `f` has a finite limit `c` at `l' ⊓ μ.ae`, then `∫ x in u..v, f x ∂μ = -μ (Ioc v u) • c + o(μ(Ioc v u))` as both `u` and `v` tend to `l`. See also `measure_integral_sub_linear_is_o_of_tendsto_ae_of_ge'` for a version that also works, e.g., for `l = l' = at_top`. -/ lemma measure_integral_sub_linear_is_o_of_tendsto_ae_of_ge (hfm : strongly_measurable_at_filter f l' μ) (hf : tendsto f (l' ⊓ μ.ae) (𝓝 c)) (hu : tendsto u lt l) (hv : tendsto v lt l) (huv : v ≤ᶠ[lt] u) : (λ t, ∫ x in u t..v t, f x ∂μ + (μ (Ioc (v t) (u t))).to_real • c) =o[lt] (λ t, (μ $ Ioc (v t) (u t)).to_real) := measure_integral_sub_linear_is_o_of_tendsto_ae_of_ge' hfm hf (FTC_filter.finite_at_inner l) hu hv huv end local attribute [instance] FTC_filter.meas_gen variables [FTC_filter a la la'] [FTC_filter b lb lb'] [is_locally_finite_measure μ] /-- Fundamental theorem of calculus-1, strict derivative in both limits for a locally finite measure. Let `f` be a measurable function integrable on `a..b`. Let `(la, la')` be a pair of `FTC_filter`s around `a`; let `(lb, lb')` be a pair of `FTC_filter`s around `b`. Suppose that `f` has finite limits `ca` and `cb` at `la' ⊓ μ.ae` and `lb' ⊓ μ.ae`, respectively. Then `∫ x in va..vb, f x ∂μ - ∫ x in ua..ub, f x ∂μ = ∫ x in ub..vb, cb ∂μ - ∫ x in ua..va, ca ∂μ + o(∥∫ x in ua..va, (1:ℝ) ∂μ∥ + ∥∫ x in ub..vb, (1:ℝ) ∂μ∥)` as `ua` and `va` tend to `la` while `ub` and `vb` tend to `lb`. -/ lemma measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae (hab : interval_integrable f μ a b) (hmeas_a : strongly_measurable_at_filter f la' μ) (hmeas_b : strongly_measurable_at_filter f lb' μ) (ha_lim : tendsto f (la' ⊓ μ.ae) (𝓝 ca)) (hb_lim : tendsto f (lb' ⊓ μ.ae) (𝓝 cb)) (hua : tendsto ua lt la) (hva : tendsto va lt la) (hub : tendsto ub lt lb) (hvb : tendsto vb lt lb) : (λ t, (∫ x in va t..vb t, f x ∂μ) - (∫ x in ua t..ub t, f x ∂μ) - (∫ x in ub t..vb t, cb ∂μ - ∫ x in ua t..va t, ca ∂μ)) =o[lt] (λ t, ∥∫ x in ua t..va t, (1:ℝ) ∂μ∥ + ∥∫ x in ub t..vb t, (1:ℝ) ∂μ∥) := begin refine ((measure_integral_sub_linear_is_o_of_tendsto_ae hmeas_a ha_lim hua hva).neg_left.add_add (measure_integral_sub_linear_is_o_of_tendsto_ae hmeas_b hb_lim hub hvb)).congr' _ eventually_eq.rfl, have A : ∀ᶠ t in lt, interval_integrable f μ (ua t) (va t) := ha_lim.eventually_interval_integrable_ae hmeas_a (FTC_filter.finite_at_inner la) hua hva, have A' : ∀ᶠ t in lt, interval_integrable f μ a (ua t) := ha_lim.eventually_interval_integrable_ae hmeas_a (FTC_filter.finite_at_inner la) (tendsto_const_pure.mono_right FTC_filter.pure_le) hua, have B : ∀ᶠ t in lt, interval_integrable f μ (ub t) (vb t) := hb_lim.eventually_interval_integrable_ae hmeas_b (FTC_filter.finite_at_inner lb) hub hvb, have B' : ∀ᶠ t in lt, interval_integrable f μ b (ub t) := hb_lim.eventually_interval_integrable_ae hmeas_b (FTC_filter.finite_at_inner lb) (tendsto_const_pure.mono_right FTC_filter.pure_le) hub, filter_upwards [A, A', B, B'] with _ ua_va a_ua ub_vb b_ub, rw [← integral_interval_sub_interval_comm'], { dsimp only [], abel, }, exacts [ub_vb, ua_va, b_ub.symm.trans $ hab.symm.trans a_ua] end /-- Fundamental theorem of calculus-1, strict derivative in right endpoint for a locally finite measure. Let `f` be a measurable function integrable on `a..b`. Let `(lb, lb')` be a pair of `FTC_filter`s around `b`. Suppose that `f` has a finite limit `c` at `lb' ⊓ μ.ae`. Then `∫ x in a..v, f x ∂μ - ∫ x in a..u, f x ∂μ = ∫ x in u..v, c ∂μ + o(∫ x in u..v, (1:ℝ) ∂μ)` as `u` and `v` tend to `lb`. -/ lemma measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae_right (hab : interval_integrable f μ a b) (hmeas : strongly_measurable_at_filter f lb' μ) (hf : tendsto f (lb' ⊓ μ.ae) (𝓝 c)) (hu : tendsto u lt lb) (hv : tendsto v lt lb) : (λ t, ∫ x in a..v t, f x ∂μ - ∫ x in a..u t, f x ∂μ - ∫ x in u t..v t, c ∂μ) =o[lt] (λ t, ∫ x in u t..v t, (1:ℝ) ∂μ) := by simpa using measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae hab strongly_measurable_at_bot hmeas ((tendsto_bot : tendsto _ ⊥ (𝓝 0)).mono_left inf_le_left) hf (tendsto_const_pure : tendsto _ _ (pure a)) tendsto_const_pure hu hv /-- Fundamental theorem of calculus-1, strict derivative in left endpoint for a locally finite measure. Let `f` be a measurable function integrable on `a..b`. Let `(la, la')` be a pair of `FTC_filter`s around `a`. Suppose that `f` has a finite limit `c` at `la' ⊓ μ.ae`. Then `∫ x in v..b, f x ∂μ - ∫ x in u..b, f x ∂μ = -∫ x in u..v, c ∂μ + o(∫ x in u..v, (1:ℝ) ∂μ)` as `u` and `v` tend to `la`. -/ lemma measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae_left (hab : interval_integrable f μ a b) (hmeas : strongly_measurable_at_filter f la' μ) (hf : tendsto f (la' ⊓ μ.ae) (𝓝 c)) (hu : tendsto u lt la) (hv : tendsto v lt la) : (λ t, ∫ x in v t..b, f x ∂μ - ∫ x in u t..b, f x ∂μ + ∫ x in u t..v t, c ∂μ) =o[lt] (λ t, ∫ x in u t..v t, (1:ℝ) ∂μ) := by simpa using measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae hab hmeas strongly_measurable_at_bot hf ((tendsto_bot : tendsto _ ⊥ (𝓝 0)).mono_left inf_le_left) hu hv (tendsto_const_pure : tendsto _ _ (pure b)) tendsto_const_pure end /-! ### Fundamental theorem of calculus-1 for Lebesgue measure In this section we restate theorems from the previous section for Lebesgue measure. In particular, we prove that `∫ x in u..v, f x` is strictly differentiable in `(u, v)` at `(a, b)` provided that `f` is integrable on `a..b` and is continuous at `a` and `b`. -/ variables {f : ℝ → E} {c ca cb : E} {l l' la la' lb lb' : filter ℝ} {lt : filter ι} {a b z : ℝ} {u v ua ub va vb : ι → ℝ} [FTC_filter a la la'] [FTC_filter b lb lb'] /-! #### Auxiliary `is_o` statements In this section we prove several lemmas that can be interpreted as strict differentiability of `(u, v) ↦ ∫ x in u..v, f x ∂μ` in `u` and/or `v` at a filter. The statements use `is_o` because we have no definition of `has_strict_(f)deriv_at_filter` in the library. -/ /-- Fundamental theorem of calculus-1, local version. If `f` has a finite limit `c` almost surely at `l'`, where `(l, l')` is an `FTC_filter` pair around `a`, then `∫ x in u..v, f x ∂μ = (v - u) • c + o (v - u)` as both `u` and `v` tend to `l`. -/ lemma integral_sub_linear_is_o_of_tendsto_ae [FTC_filter a l l'] (hfm : strongly_measurable_at_filter f l') (hf : tendsto f (l' ⊓ volume.ae) (𝓝 c)) {u v : ι → ℝ} (hu : tendsto u lt l) (hv : tendsto v lt l) : (λ t, (∫ x in u t..v t, f x) - (v t - u t) • c) =o[lt] (v - u) := by simpa [integral_const] using measure_integral_sub_linear_is_o_of_tendsto_ae hfm hf hu hv /-- Fundamental theorem of calculus-1, strict differentiability at filter in both endpoints. If `f` is a measurable function integrable on `a..b`, `(la, la')` is an `FTC_filter` pair around `a`, and `(lb, lb')` is an `FTC_filter` pair around `b`, and `f` has finite limits `ca` and `cb` almost surely at `la'` and `lb'`, respectively, then `(∫ x in va..vb, f x) - ∫ x in ua..ub, f x = (vb - ub) • cb - (va - ua) • ca + o(∥va - ua∥ + ∥vb - ub∥)` as `ua` and `va` tend to `la` while `ub` and `vb` tend to `lb`. This lemma could've been formulated using `has_strict_fderiv_at_filter` if we had this definition. -/ lemma integral_sub_integral_sub_linear_is_o_of_tendsto_ae (hab : interval_integrable f volume a b) (hmeas_a : strongly_measurable_at_filter f la') (hmeas_b : strongly_measurable_at_filter f lb') (ha_lim : tendsto f (la' ⊓ volume.ae) (𝓝 ca)) (hb_lim : tendsto f (lb' ⊓ volume.ae) (𝓝 cb)) (hua : tendsto ua lt la) (hva : tendsto va lt la) (hub : tendsto ub lt lb) (hvb : tendsto vb lt lb) : (λ t, (∫ x in va t..vb t, f x) - (∫ x in ua t..ub t, f x) - ((vb t - ub t) • cb - (va t - ua t) • ca)) =o[lt] (λ t, ∥va t - ua t∥ + ∥vb t - ub t∥) := by simpa [integral_const] using measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae hab hmeas_a hmeas_b ha_lim hb_lim hua hva hub hvb /-- Fundamental theorem of calculus-1, strict differentiability at filter in both endpoints. If `f` is a measurable function integrable on `a..b`, `(lb, lb')` is an `FTC_filter` pair around `b`, and `f` has a finite limit `c` almost surely at `lb'`, then `(∫ x in a..v, f x) - ∫ x in a..u, f x = (v - u) • c + o(∥v - u∥)` as `u` and `v` tend to `lb`. This lemma could've been formulated using `has_strict_deriv_at_filter` if we had this definition. -/ lemma integral_sub_integral_sub_linear_is_o_of_tendsto_ae_right (hab : interval_integrable f volume a b) (hmeas : strongly_measurable_at_filter f lb') (hf : tendsto f (lb' ⊓ volume.ae) (𝓝 c)) (hu : tendsto u lt lb) (hv : tendsto v lt lb) : (λ t, (∫ x in a..v t, f x) - (∫ x in a..u t, f x) - (v t - u t) • c) =o[lt] (v - u) := by simpa only [integral_const, smul_eq_mul, mul_one] using measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae_right hab hmeas hf hu hv /-- Fundamental theorem of calculus-1, strict differentiability at filter in both endpoints. If `f` is a measurable function integrable on `a..b`, `(la, la')` is an `FTC_filter` pair around `a`, and `f` has a finite limit `c` almost surely at `la'`, then `(∫ x in v..b, f x) - ∫ x in u..b, f x = -(v - u) • c + o(∥v - u∥)` as `u` and `v` tend to `la`. This lemma could've been formulated using `has_strict_deriv_at_filter` if we had this definition. -/ lemma integral_sub_integral_sub_linear_is_o_of_tendsto_ae_left (hab : interval_integrable f volume a b) (hmeas : strongly_measurable_at_filter f la') (hf : tendsto f (la' ⊓ volume.ae) (𝓝 c)) (hu : tendsto u lt la) (hv : tendsto v lt la) : (λ t, (∫ x in v t..b, f x) - (∫ x in u t..b, f x) + (v t - u t) • c) =o[lt] (v - u) := by simpa only [integral_const, smul_eq_mul, mul_one] using measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae_left hab hmeas hf hu hv open continuous_linear_map (fst snd smul_right sub_apply smul_right_apply coe_fst' coe_snd' map_sub) /-! #### Strict differentiability In this section we prove that for a measurable function `f` integrable on `a..b`, * `integral_has_strict_fderiv_at_of_tendsto_ae`: the function `(u, v) ↦ ∫ x in u..v, f x` has derivative `(u, v) ↦ v • cb - u • ca` at `(a, b)` in the sense of strict differentiability provided that `f` tends to `ca` and `cb` almost surely as `x` tendsto to `a` and `b`, respectively; * `integral_has_strict_fderiv_at`: the function `(u, v) ↦ ∫ x in u..v, f x` has derivative `(u, v) ↦ v • f b - u • f a` at `(a, b)` in the sense of strict differentiability provided that `f` is continuous at `a` and `b`; * `integral_has_strict_deriv_at_of_tendsto_ae_right`: the function `u ↦ ∫ x in a..u, f x` has derivative `c` at `b` in the sense of strict differentiability provided that `f` tends to `c` almost surely as `x` tends to `b`; * `integral_has_strict_deriv_at_right`: the function `u ↦ ∫ x in a..u, f x` has derivative `f b` at `b` in the sense of strict differentiability provided that `f` is continuous at `b`; * `integral_has_strict_deriv_at_of_tendsto_ae_left`: the function `u ↦ ∫ x in u..b, f x` has derivative `-c` at `a` in the sense of strict differentiability provided that `f` tends to `c` almost surely as `x` tends to `a`; * `integral_has_strict_deriv_at_left`: the function `u ↦ ∫ x in u..b, f x` has derivative `-f a` at `a` in the sense of strict differentiability provided that `f` is continuous at `a`. -/ /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f x` has finite limits `ca` and `cb` almost surely as `x` tends to `a` and `b`, respectively, then `(u, v) ↦ ∫ x in u..v, f x` has derivative `(u, v) ↦ v • cb - u • ca` at `(a, b)` in the sense of strict differentiability. -/ lemma integral_has_strict_fderiv_at_of_tendsto_ae (hf : interval_integrable f volume a b) (hmeas_a : strongly_measurable_at_filter f (𝓝 a)) (hmeas_b : strongly_measurable_at_filter f (𝓝 b)) (ha : tendsto f (𝓝 a ⊓ volume.ae) (𝓝 ca)) (hb : tendsto f (𝓝 b ⊓ volume.ae) (𝓝 cb)) : has_strict_fderiv_at (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x) ((snd ℝ ℝ ℝ).smul_right cb - (fst ℝ ℝ ℝ).smul_right ca) (a, b) := begin have := integral_sub_integral_sub_linear_is_o_of_tendsto_ae hf hmeas_a hmeas_b ha hb ((continuous_fst.comp continuous_snd).tendsto ((a, b), (a, b))) ((continuous_fst.comp continuous_fst).tendsto ((a, b), (a, b))) ((continuous_snd.comp continuous_snd).tendsto ((a, b), (a, b))) ((continuous_snd.comp continuous_fst).tendsto ((a, b), (a, b))), refine (this.congr_left _).trans_is_O _, { intro x, simp [sub_smul] }, { exact is_O_fst_prod.norm_left.add is_O_snd_prod.norm_left } end /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous at `a` and `b`, then `(u, v) ↦ ∫ x in u..v, f x` has derivative `(u, v) ↦ v • cb - u • ca` at `(a, b)` in the sense of strict differentiability. -/ lemma integral_has_strict_fderiv_at (hf : interval_integrable f volume a b) (hmeas_a : strongly_measurable_at_filter f (𝓝 a)) (hmeas_b : strongly_measurable_at_filter f (𝓝 b)) (ha : continuous_at f a) (hb : continuous_at f b) : has_strict_fderiv_at (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x) ((snd ℝ ℝ ℝ).smul_right (f b) - (fst ℝ ℝ ℝ).smul_right (f a)) (a, b) := integral_has_strict_fderiv_at_of_tendsto_ae hf hmeas_a hmeas_b (ha.mono_left inf_le_left) (hb.mono_left inf_le_left) /-- **First Fundamental Theorem of Calculus**: if `f : ℝ → E` is integrable on `a..b` and `f x` has a finite limit `c` almost surely at `b`, then `u ↦ ∫ x in a..u, f x` has derivative `c` at `b` in the sense of strict differentiability. -/ lemma integral_has_strict_deriv_at_of_tendsto_ae_right (hf : interval_integrable f volume a b) (hmeas : strongly_measurable_at_filter f (𝓝 b)) (hb : tendsto f (𝓝 b ⊓ volume.ae) (𝓝 c)) : has_strict_deriv_at (λ u, ∫ x in a..u, f x) c b := integral_sub_integral_sub_linear_is_o_of_tendsto_ae_right hf hmeas hb continuous_at_snd continuous_at_fst /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous at `b`, then `u ↦ ∫ x in a..u, f x` has derivative `f b` at `b` in the sense of strict differentiability. -/ lemma integral_has_strict_deriv_at_right (hf : interval_integrable f volume a b) (hmeas : strongly_measurable_at_filter f (𝓝 b)) (hb : continuous_at f b) : has_strict_deriv_at (λ u, ∫ x in a..u, f x) (f b) b := integral_has_strict_deriv_at_of_tendsto_ae_right hf hmeas (hb.mono_left inf_le_left) /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f x` has a finite limit `c` almost surely at `a`, then `u ↦ ∫ x in u..b, f x` has derivative `-c` at `a` in the sense of strict differentiability. -/ lemma integral_has_strict_deriv_at_of_tendsto_ae_left (hf : interval_integrable f volume a b) (hmeas : strongly_measurable_at_filter f (𝓝 a)) (ha : tendsto f (𝓝 a ⊓ volume.ae) (𝓝 c)) : has_strict_deriv_at (λ u, ∫ x in u..b, f x) (-c) a := by simpa only [← integral_symm] using (integral_has_strict_deriv_at_of_tendsto_ae_right hf.symm hmeas ha).neg /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous at `a`, then `u ↦ ∫ x in u..b, f x` has derivative `-f a` at `a` in the sense of strict differentiability. -/ lemma integral_has_strict_deriv_at_left (hf : interval_integrable f volume a b) (hmeas : strongly_measurable_at_filter f (𝓝 a)) (ha : continuous_at f a) : has_strict_deriv_at (λ u, ∫ x in u..b, f x) (-f a) a := by simpa only [← integral_symm] using (integral_has_strict_deriv_at_right hf.symm hmeas ha).neg /-! #### Fréchet differentiability In this subsection we restate results from the previous subsection in terms of `has_fderiv_at`, `has_deriv_at`, `fderiv`, and `deriv`. -/ /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f x` has finite limits `ca` and `cb` almost surely as `x` tends to `a` and `b`, respectively, then `(u, v) ↦ ∫ x in u..v, f x` has derivative `(u, v) ↦ v • cb - u • ca` at `(a, b)`. -/ lemma integral_has_fderiv_at_of_tendsto_ae (hf : interval_integrable f volume a b) (hmeas_a : strongly_measurable_at_filter f (𝓝 a)) (hmeas_b : strongly_measurable_at_filter f (𝓝 b)) (ha : tendsto f (𝓝 a ⊓ volume.ae) (𝓝 ca)) (hb : tendsto f (𝓝 b ⊓ volume.ae) (𝓝 cb)) : has_fderiv_at (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x) ((snd ℝ ℝ ℝ).smul_right cb - (fst ℝ ℝ ℝ).smul_right ca) (a, b) := (integral_has_strict_fderiv_at_of_tendsto_ae hf hmeas_a hmeas_b ha hb).has_fderiv_at /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous at `a` and `b`, then `(u, v) ↦ ∫ x in u..v, f x` has derivative `(u, v) ↦ v • cb - u • ca` at `(a, b)`. -/ lemma integral_has_fderiv_at (hf : interval_integrable f volume a b) (hmeas_a : strongly_measurable_at_filter f (𝓝 a)) (hmeas_b : strongly_measurable_at_filter f (𝓝 b)) (ha : continuous_at f a) (hb : continuous_at f b) : has_fderiv_at (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x) ((snd ℝ ℝ ℝ).smul_right (f b) - (fst ℝ ℝ ℝ).smul_right (f a)) (a, b) := (integral_has_strict_fderiv_at hf hmeas_a hmeas_b ha hb).has_fderiv_at /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f x` has finite limits `ca` and `cb` almost surely as `x` tends to `a` and `b`, respectively, then `fderiv` derivative of `(u, v) ↦ ∫ x in u..v, f x` at `(a, b)` equals `(u, v) ↦ v • cb - u • ca`. -/ lemma fderiv_integral_of_tendsto_ae (hf : interval_integrable f volume a b) (hmeas_a : strongly_measurable_at_filter f (𝓝 a)) (hmeas_b : strongly_measurable_at_filter f (𝓝 b)) (ha : tendsto f (𝓝 a ⊓ volume.ae) (𝓝 ca)) (hb : tendsto f (𝓝 b ⊓ volume.ae) (𝓝 cb)) : fderiv ℝ (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x) (a, b) = (snd ℝ ℝ ℝ).smul_right cb - (fst ℝ ℝ ℝ).smul_right ca := (integral_has_fderiv_at_of_tendsto_ae hf hmeas_a hmeas_b ha hb).fderiv /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous at `a` and `b`, then `fderiv` derivative of `(u, v) ↦ ∫ x in u..v, f x` at `(a, b)` equals `(u, v) ↦ v • cb - u • ca`. -/ lemma fderiv_integral (hf : interval_integrable f volume a b) (hmeas_a : strongly_measurable_at_filter f (𝓝 a)) (hmeas_b : strongly_measurable_at_filter f (𝓝 b)) (ha : continuous_at f a) (hb : continuous_at f b) : fderiv ℝ (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x) (a, b) = (snd ℝ ℝ ℝ).smul_right (f b) - (fst ℝ ℝ ℝ).smul_right (f a) := (integral_has_fderiv_at hf hmeas_a hmeas_b ha hb).fderiv /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f x` has a finite limit `c` almost surely at `b`, then `u ↦ ∫ x in a..u, f x` has derivative `c` at `b`. -/ lemma integral_has_deriv_at_of_tendsto_ae_right (hf : interval_integrable f volume a b) (hmeas : strongly_measurable_at_filter f (𝓝 b)) (hb : tendsto f (𝓝 b ⊓ volume.ae) (𝓝 c)) : has_deriv_at (λ u, ∫ x in a..u, f x) c b := (integral_has_strict_deriv_at_of_tendsto_ae_right hf hmeas hb).has_deriv_at /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous at `b`, then `u ↦ ∫ x in a..u, f x` has derivative `f b` at `b`. -/ lemma integral_has_deriv_at_right (hf : interval_integrable f volume a b) (hmeas : strongly_measurable_at_filter f (𝓝 b)) (hb : continuous_at f b) : has_deriv_at (λ u, ∫ x in a..u, f x) (f b) b := (integral_has_strict_deriv_at_right hf hmeas hb).has_deriv_at /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f` has a finite limit `c` almost surely at `b`, then the derivative of `u ↦ ∫ x in a..u, f x` at `b` equals `c`. -/ lemma deriv_integral_of_tendsto_ae_right (hf : interval_integrable f volume a b) (hmeas : strongly_measurable_at_filter f (𝓝 b)) (hb : tendsto f (𝓝 b ⊓ volume.ae) (𝓝 c)) : deriv (λ u, ∫ x in a..u, f x) b = c := (integral_has_deriv_at_of_tendsto_ae_right hf hmeas hb).deriv /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous at `b`, then the derivative of `u ↦ ∫ x in a..u, f x` at `b` equals `f b`. -/ lemma deriv_integral_right (hf : interval_integrable f volume a b) (hmeas : strongly_measurable_at_filter f (𝓝 b)) (hb : continuous_at f b) : deriv (λ u, ∫ x in a..u, f x) b = f b := (integral_has_deriv_at_right hf hmeas hb).deriv /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f x` has a finite limit `c` almost surely at `a`, then `u ↦ ∫ x in u..b, f x` has derivative `-c` at `a`. -/ lemma integral_has_deriv_at_of_tendsto_ae_left (hf : interval_integrable f volume a b) (hmeas : strongly_measurable_at_filter f (𝓝 a)) (ha : tendsto f (𝓝 a ⊓ volume.ae) (𝓝 c)) : has_deriv_at (λ u, ∫ x in u..b, f x) (-c) a := (integral_has_strict_deriv_at_of_tendsto_ae_left hf hmeas ha).has_deriv_at /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous at `a`, then `u ↦ ∫ x in u..b, f x` has derivative `-f a` at `a`. -/ lemma integral_has_deriv_at_left (hf : interval_integrable f volume a b) (hmeas : strongly_measurable_at_filter f (𝓝 a)) (ha : continuous_at f a) : has_deriv_at (λ u, ∫ x in u..b, f x) (-f a) a := (integral_has_strict_deriv_at_left hf hmeas ha).has_deriv_at /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f` has a finite limit `c` almost surely at `a`, then the derivative of `u ↦ ∫ x in u..b, f x` at `a` equals `-c`. -/ lemma deriv_integral_of_tendsto_ae_left (hf : interval_integrable f volume a b) (hmeas : strongly_measurable_at_filter f (𝓝 a)) (hb : tendsto f (𝓝 a ⊓ volume.ae) (𝓝 c)) : deriv (λ u, ∫ x in u..b, f x) a = -c := (integral_has_deriv_at_of_tendsto_ae_left hf hmeas hb).deriv /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous at `a`, then the derivative of `u ↦ ∫ x in u..b, f x` at `a` equals `-f a`. -/ lemma deriv_integral_left (hf : interval_integrable f volume a b) (hmeas : strongly_measurable_at_filter f (𝓝 a)) (hb : continuous_at f a) : deriv (λ u, ∫ x in u..b, f x) a = -f a := (integral_has_deriv_at_left hf hmeas hb).deriv /-! #### One-sided derivatives -/ /-- Let `f` be a measurable function integrable on `a..b`. The function `(u, v) ↦ ∫ x in u..v, f x` has derivative `(u, v) ↦ v • cb - u • ca` within `s × t` at `(a, b)`, where `s ∈ {Iic a, {a}, Ici a, univ}` and `t ∈ {Iic b, {b}, Ici b, univ}` provided that `f` tends to `ca` and `cb` almost surely at the filters `la` and `lb` from the following table. | `s` | `la` | `t` | `lb` | | ------- | ---- | --- | ---- | | `Iic a` | `𝓝[≤] a` | `Iic b` | `𝓝[≤] b` | | `Ici a` | `𝓝[>] a` | `Ici b` | `𝓝[>] b` | | `{a}` | `⊥` | `{b}` | `⊥` | | `univ` | `𝓝 a` | `univ` | `𝓝 b` | -/ lemma integral_has_fderiv_within_at_of_tendsto_ae (hf : interval_integrable f volume a b) {s t : set ℝ} [FTC_filter a (𝓝[s] a) la] [FTC_filter b (𝓝[t] b) lb] (hmeas_a : strongly_measurable_at_filter f la) (hmeas_b : strongly_measurable_at_filter f lb) (ha : tendsto f (la ⊓ volume.ae) (𝓝 ca)) (hb : tendsto f (lb ⊓ volume.ae) (𝓝 cb)) : has_fderiv_within_at (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x) ((snd ℝ ℝ ℝ).smul_right cb - (fst ℝ ℝ ℝ).smul_right ca) (s ×ˢ t) (a, b) := begin rw [has_fderiv_within_at, nhds_within_prod_eq], have := integral_sub_integral_sub_linear_is_o_of_tendsto_ae hf hmeas_a hmeas_b ha hb (tendsto_const_pure.mono_right FTC_filter.pure_le : tendsto _ _ (𝓝[s] a)) tendsto_fst (tendsto_const_pure.mono_right FTC_filter.pure_le : tendsto _ _ (𝓝[t] b)) tendsto_snd, refine (this.congr_left _).trans_is_O _, { intro x, simp [sub_smul] }, { exact is_O_fst_prod.norm_left.add is_O_snd_prod.norm_left } end /-- Let `f` be a measurable function integrable on `a..b`. The function `(u, v) ↦ ∫ x in u..v, f x` has derivative `(u, v) ↦ v • f b - u • f a` within `s × t` at `(a, b)`, where `s ∈ {Iic a, {a}, Ici a, univ}` and `t ∈ {Iic b, {b}, Ici b, univ}` provided that `f` tends to `f a` and `f b` at the filters `la` and `lb` from the following table. In most cases this assumption is definitionally equal `continuous_at f _` or `continuous_within_at f _ _`. | `s` | `la` | `t` | `lb` | | ------- | ---- | --- | ---- | | `Iic a` | `𝓝[≤] a` | `Iic b` | `𝓝[≤] b` | | `Ici a` | `𝓝[>] a` | `Ici b` | `𝓝[>] b` | | `{a}` | `⊥` | `{b}` | `⊥` | | `univ` | `𝓝 a` | `univ` | `𝓝 b` | -/ lemma integral_has_fderiv_within_at (hf : interval_integrable f volume a b) (hmeas_a : strongly_measurable_at_filter f la) (hmeas_b : strongly_measurable_at_filter f lb) {s t : set ℝ} [FTC_filter a (𝓝[s] a) la] [FTC_filter b (𝓝[t] b) lb] (ha : tendsto f la (𝓝 $ f a)) (hb : tendsto f lb (𝓝 $ f b)) : has_fderiv_within_at (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x) ((snd ℝ ℝ ℝ).smul_right (f b) - (fst ℝ ℝ ℝ).smul_right (f a)) (s ×ˢ t) (a, b) := integral_has_fderiv_within_at_of_tendsto_ae hf hmeas_a hmeas_b (ha.mono_left inf_le_left) (hb.mono_left inf_le_left) /-- An auxiliary tactic closing goals `unique_diff_within_at ℝ s a` where `s ∈ {Iic a, Ici a, univ}`. -/ meta def unique_diff_within_at_Ici_Iic_univ : tactic unit := `[apply_rules [unique_diff_on.unique_diff_within_at, unique_diff_on_Ici, unique_diff_on_Iic, left_mem_Ici, right_mem_Iic, unique_diff_within_at_univ]] /-- Let `f` be a measurable function integrable on `a..b`. Choose `s ∈ {Iic a, Ici a, univ}` and `t ∈ {Iic b, Ici b, univ}`. Suppose that `f` tends to `ca` and `cb` almost surely at the filters `la` and `lb` from the table below. Then `fderiv_within ℝ (λ p, ∫ x in p.1..p.2, f x) (s ×ˢ t)` is equal to `(u, v) ↦ u • cb - v • ca`. | `s` | `la` | `t` | `lb` | | ------- | ---- | --- | ---- | | `Iic a` | `𝓝[≤] a` | `Iic b` | `𝓝[≤] b` | | `Ici a` | `𝓝[>] a` | `Ici b` | `𝓝[>] b` | | `{a}` | `⊥` | `{b}` | `⊥` | | `univ` | `𝓝 a` | `univ` | `𝓝 b` | -/ lemma fderiv_within_integral_of_tendsto_ae (hf : interval_integrable f volume a b) (hmeas_a : strongly_measurable_at_filter f la) (hmeas_b : strongly_measurable_at_filter f lb) {s t : set ℝ} [FTC_filter a (𝓝[s] a) la] [FTC_filter b (𝓝[t] b) lb] (ha : tendsto f (la ⊓ volume.ae) (𝓝 ca)) (hb : tendsto f (lb ⊓ volume.ae) (𝓝 cb)) (hs : unique_diff_within_at ℝ s a . unique_diff_within_at_Ici_Iic_univ) (ht : unique_diff_within_at ℝ t b . unique_diff_within_at_Ici_Iic_univ) : fderiv_within ℝ (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x) (s ×ˢ t) (a, b) = ((snd ℝ ℝ ℝ).smul_right cb - (fst ℝ ℝ ℝ).smul_right ca) := (integral_has_fderiv_within_at_of_tendsto_ae hf hmeas_a hmeas_b ha hb).fderiv_within $ hs.prod ht /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f x` has a finite limit `c` almost surely as `x` tends to `b` from the right or from the left, then `u ↦ ∫ x in a..u, f x` has right (resp., left) derivative `c` at `b`. -/ lemma integral_has_deriv_within_at_of_tendsto_ae_right (hf : interval_integrable f volume a b) {s t : set ℝ} [FTC_filter b (𝓝[s] b) (𝓝[t] b)] (hmeas : strongly_measurable_at_filter f (𝓝[t] b)) (hb : tendsto f (𝓝[t] b ⊓ volume.ae) (𝓝 c)) : has_deriv_within_at (λ u, ∫ x in a..u, f x) c s b := integral_sub_integral_sub_linear_is_o_of_tendsto_ae_right hf hmeas hb (tendsto_const_pure.mono_right FTC_filter.pure_le) tendsto_id /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f x` is continuous from the left or from the right at `b`, then `u ↦ ∫ x in a..u, f x` has left (resp., right) derivative `f b` at `b`. -/ lemma integral_has_deriv_within_at_right (hf : interval_integrable f volume a b) {s t : set ℝ} [FTC_filter b (𝓝[s] b) (𝓝[t] b)] (hmeas : strongly_measurable_at_filter f (𝓝[t] b)) (hb : continuous_within_at f t b) : has_deriv_within_at (λ u, ∫ x in a..u, f x) (f b) s b := integral_has_deriv_within_at_of_tendsto_ae_right hf hmeas (hb.mono_left inf_le_left) /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f x` has a finite limit `c` almost surely as `x` tends to `b` from the right or from the left, then the right (resp., left) derivative of `u ↦ ∫ x in a..u, f x` at `b` equals `c`. -/ lemma deriv_within_integral_of_tendsto_ae_right (hf : interval_integrable f volume a b) {s t : set ℝ} [FTC_filter b (𝓝[s] b) (𝓝[t] b)] (hmeas: strongly_measurable_at_filter f (𝓝[t] b)) (hb : tendsto f (𝓝[t] b ⊓ volume.ae) (𝓝 c)) (hs : unique_diff_within_at ℝ s b . unique_diff_within_at_Ici_Iic_univ) : deriv_within (λ u, ∫ x in a..u, f x) s b = c := (integral_has_deriv_within_at_of_tendsto_ae_right hf hmeas hb).deriv_within hs /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f x` is continuous on the right or on the left at `b`, then the right (resp., left) derivative of `u ↦ ∫ x in a..u, f x` at `b` equals `f b`. -/ lemma deriv_within_integral_right (hf : interval_integrable f volume a b) {s t : set ℝ} [FTC_filter b (𝓝[s] b) (𝓝[t] b)] (hmeas : strongly_measurable_at_filter f (𝓝[t] b)) (hb : continuous_within_at f t b) (hs : unique_diff_within_at ℝ s b . unique_diff_within_at_Ici_Iic_univ) : deriv_within (λ u, ∫ x in a..u, f x) s b = f b := (integral_has_deriv_within_at_right hf hmeas hb).deriv_within hs /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f x` has a finite limit `c` almost surely as `x` tends to `a` from the right or from the left, then `u ↦ ∫ x in u..b, f x` has right (resp., left) derivative `-c` at `a`. -/ lemma integral_has_deriv_within_at_of_tendsto_ae_left (hf : interval_integrable f volume a b) {s t : set ℝ} [FTC_filter a (𝓝[s] a) (𝓝[t] a)] (hmeas : strongly_measurable_at_filter f (𝓝[t] a)) (ha : tendsto f (𝓝[t] a ⊓ volume.ae) (𝓝 c)) : has_deriv_within_at (λ u, ∫ x in u..b, f x) (-c) s a := by { simp only [integral_symm b], exact (integral_has_deriv_within_at_of_tendsto_ae_right hf.symm hmeas ha).neg } /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f x` is continuous from the left or from the right at `a`, then `u ↦ ∫ x in u..b, f x` has left (resp., right) derivative `-f a` at `a`. -/ lemma integral_has_deriv_within_at_left (hf : interval_integrable f volume a b) {s t : set ℝ} [FTC_filter a (𝓝[s] a) (𝓝[t] a)] (hmeas : strongly_measurable_at_filter f (𝓝[t] a)) (ha : continuous_within_at f t a) : has_deriv_within_at (λ u, ∫ x in u..b, f x) (-f a) s a := integral_has_deriv_within_at_of_tendsto_ae_left hf hmeas (ha.mono_left inf_le_left) /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f x` has a finite limit `c` almost surely as `x` tends to `a` from the right or from the left, then the right (resp., left) derivative of `u ↦ ∫ x in u..b, f x` at `a` equals `-c`. -/ lemma deriv_within_integral_of_tendsto_ae_left (hf : interval_integrable f volume a b) {s t : set ℝ} [FTC_filter a (𝓝[s] a) (𝓝[t] a)] (hmeas : strongly_measurable_at_filter f (𝓝[t] a)) (ha : tendsto f (𝓝[t] a ⊓ volume.ae) (𝓝 c)) (hs : unique_diff_within_at ℝ s a . unique_diff_within_at_Ici_Iic_univ) : deriv_within (λ u, ∫ x in u..b, f x) s a = -c := (integral_has_deriv_within_at_of_tendsto_ae_left hf hmeas ha).deriv_within hs /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f x` is continuous on the right or on the left at `a`, then the right (resp., left) derivative of `u ↦ ∫ x in u..b, f x` at `a` equals `-f a`. -/ lemma deriv_within_integral_left (hf : interval_integrable f volume a b) {s t : set ℝ} [FTC_filter a (𝓝[s] a) (𝓝[t] a)] (hmeas : strongly_measurable_at_filter f (𝓝[t] a)) (ha : continuous_within_at f t a) (hs : unique_diff_within_at ℝ s a . unique_diff_within_at_Ici_Iic_univ) : deriv_within (λ u, ∫ x in u..b, f x) s a = -f a := (integral_has_deriv_within_at_left hf hmeas ha).deriv_within hs /-- The integral of a continuous function is differentiable on a real set `s`. -/ theorem differentiable_on_integral_of_continuous {s : set ℝ} (hintg : ∀ x ∈ s, interval_integrable f volume a x) (hcont : continuous f) : differentiable_on ℝ (λ u, ∫ x in a..u, f x) s := λ y hy, (integral_has_deriv_at_right (hintg y hy) hcont.ae_strongly_measurable.strongly_measurable_at_filter hcont.continuous_at) .differentiable_at.differentiable_within_at /-! ### Fundamental theorem of calculus, part 2 This section contains theorems pertaining to FTC-2 for interval integrals, i.e., the assertion that `∫ x in a..b, f' x = f b - f a` under suitable assumptions. The most classical version of this theorem assumes that `f'` is continuous. However, this is unnecessarily strong: the result holds if `f'` is just integrable. We prove the strong version, following [Rudin, *Real and Complex Analysis* (Theorem 7.21)][rudin2006real]. The proof is first given for real-valued functions, and then deduced for functions with a general target space. For a real-valued function `g`, it suffices to show that `g b - g a ≤ (∫ x in a..b, g' x) + ε` for all positive `ε`. To prove this, choose a lower-semicontinuous function `G'` with `g' < G'` and with integral close to that of `g'` (its existence is guaranteed by the Vitali-Carathéodory theorem). It satisfies `g t - g a ≤ ∫ x in a..t, G' x` for all `t ∈ [a, b]`: this inequality holds at `a`, and if it holds at `t` then it holds for `u` close to `t` on its right, as the left hand side increases by `g u - g t ∼ (u -t) g' t`, while the right hand side increases by `∫ x in t..u, G' x` which is roughly at least `∫ x in t..u, G' t = (u - t) G' t`, by lower semicontinuity. As `g' t < G' t`, this gives the conclusion. One can therefore push progressively this inequality to the right until the point `b`, where it gives the desired conclusion. -/ variables {g' g φ : ℝ → ℝ} /-- Hard part of FTC-2 for integrable derivatives, real-valued functions: one has `g b - g a ≤ ∫ y in a..b, g' y` when `g'` is integrable. Auxiliary lemma in the proof of `integral_eq_sub_of_has_deriv_right_of_le`. We give the slightly more general version that `g b - g a ≤ ∫ y in a..b, φ y` when `g' ≤ φ` and `φ` is integrable (even if `g'` is not known to be integrable). Version assuming that `g` is differentiable on `[a, b)`. -/ lemma sub_le_integral_of_has_deriv_right_of_le_Ico (hab : a ≤ b) (hcont : continuous_on g (Icc a b)) (hderiv : ∀ x ∈ Ico a b, has_deriv_within_at g (g' x) (Ioi x) x) (φint : integrable_on φ (Icc a b)) (hφg : ∀ x ∈ Ico a b, g' x ≤ φ x) : g b - g a ≤ ∫ y in a..b, φ y := begin refine le_of_forall_pos_le_add (λ ε εpos, _), -- Bound from above `g'` by a lower-semicontinuous function `G'`. rcases exists_lt_lower_semicontinuous_integral_lt φ φint εpos with ⟨G', f_lt_G', G'cont, G'int, G'lt_top, hG'⟩, -- we will show by "induction" that `g t - g a ≤ ∫ u in a..t, G' u` for all `t ∈ [a, b]`. set s := {t | g t - g a ≤ ∫ u in a..t, (G' u).to_real} ∩ Icc a b, -- the set `s` of points where this property holds is closed. have s_closed : is_closed s, { have : continuous_on (λ t, (g t - g a, ∫ u in a..t, (G' u).to_real)) (Icc a b), { rw ← interval_of_le hab at G'int ⊢ hcont, exact (hcont.sub continuous_on_const).prod (continuous_on_primitive_interval G'int) }, simp only [s, inter_comm], exact this.preimage_closed_of_closed is_closed_Icc order_closed_topology.is_closed_le' }, have main : Icc a b ⊆ {t | g t - g a ≤ ∫ u in a..t, (G' u).to_real }, { -- to show that the set `s` is all `[a, b]`, it suffices to show that any point `t` in `s` -- with `t < b` admits another point in `s` slightly to its right -- (this is a sort of real induction). apply s_closed.Icc_subset_of_forall_exists_gt (by simp only [integral_same, mem_set_of_eq, sub_self]) (λ t ht v t_lt_v, _), obtain ⟨y, g'_lt_y', y_lt_G'⟩ : ∃ (y : ℝ), (g' t : ereal) < y ∧ (y : ereal) < G' t := ereal.lt_iff_exists_real_btwn.1 ((ereal.coe_le_coe_iff.2 (hφg t ht.2)).trans_lt (f_lt_G' t)), -- bound from below the increase of `∫ x in a..u, G' x` on the right of `t`, using the lower -- semicontinuity of `G'`. have I1 : ∀ᶠ u in 𝓝[>] t, (u - t) * y ≤ ∫ w in t..u, (G' w).to_real, { have B : ∀ᶠ u in 𝓝 t, (y : ereal) < G' u := G'cont.lower_semicontinuous_at _ _ y_lt_G', rcases mem_nhds_iff_exists_Ioo_subset.1 B with ⟨m, M, ⟨hm, hM⟩, H⟩, have : Ioo t (min M b) ∈ 𝓝[>] t := mem_nhds_within_Ioi_iff_exists_Ioo_subset.2 ⟨min M b, by simp only [hM, ht.right.right, lt_min_iff, mem_Ioi, and_self], subset.refl _⟩, filter_upwards [this] with u hu, have I : Icc t u ⊆ Icc a b := Icc_subset_Icc ht.2.1 (hu.2.le.trans (min_le_right _ _)), calc (u - t) * y = ∫ v in Icc t u, y : by simp only [hu.left.le, measure_theory.integral_const, algebra.id.smul_eq_mul, sub_nonneg, measurable_set.univ, real.volume_Icc, measure.restrict_apply, univ_inter, ennreal.to_real_of_real] ... ≤ ∫ w in t..u, (G' w).to_real : begin rw [interval_integral.integral_of_le hu.1.le, ← integral_Icc_eq_integral_Ioc], apply set_integral_mono_ae_restrict, { simp only [integrable_on_const, real.volume_Icc, ennreal.of_real_lt_top, or_true] }, { exact integrable_on.mono_set G'int I }, { have C1 : ∀ᵐ (x : ℝ) ∂volume.restrict (Icc t u), G' x < ∞ := ae_mono (measure.restrict_mono I le_rfl) G'lt_top, have C2 : ∀ᵐ (x : ℝ) ∂volume.restrict (Icc t u), x ∈ Icc t u := ae_restrict_mem measurable_set_Icc, filter_upwards [C1, C2] with x G'x hx, apply ereal.coe_le_coe_iff.1, have : x ∈ Ioo m M, by simp only [hm.trans_le hx.left, (hx.right.trans_lt hu.right).trans_le (min_le_left M b), mem_Ioo, and_self], convert le_of_lt (H this), exact ereal.coe_to_real G'x.ne (ne_bot_of_gt (f_lt_G' x)) } end }, -- bound from above the increase of `g u - g a` on the right of `t`, using the derivative at `t` have I2 : ∀ᶠ u in 𝓝[>] t, g u - g t ≤ (u - t) * y, { have g'_lt_y : g' t < y := ereal.coe_lt_coe_iff.1 g'_lt_y', filter_upwards [(hderiv t ⟨ht.2.1, ht.2.2⟩).limsup_slope_le' (not_mem_Ioi.2 le_rfl) g'_lt_y, self_mem_nhds_within] with u hu t_lt_u, have := mul_le_mul_of_nonneg_left hu.le (sub_pos.2 t_lt_u).le, rwa [← smul_eq_mul, sub_smul_slope] at this }, -- combine the previous two bounds to show that `g u - g a` increases less quickly than -- `∫ x in a..u, G' x`. have I3 : ∀ᶠ u in 𝓝[>] t, g u - g t ≤ ∫ w in t..u, (G' w).to_real, { filter_upwards [I1, I2] with u hu1 hu2 using hu2.trans hu1, }, have I4 : ∀ᶠ u in 𝓝[>] t, u ∈ Ioc t (min v b), { refine mem_nhds_within_Ioi_iff_exists_Ioc_subset.2 ⟨min v b, _, subset.refl _⟩, simp only [lt_min_iff, mem_Ioi], exact ⟨t_lt_v, ht.2.2⟩ }, -- choose a point `x` slightly to the right of `t` which satisfies the above bound rcases (I3.and I4).exists with ⟨x, hx, h'x⟩, -- we check that it belongs to `s`, essentially by construction refine ⟨x, _, Ioc_subset_Ioc le_rfl (min_le_left _ _) h'x⟩, calc g x - g a = (g t - g a) + (g x - g t) : by abel ... ≤ (∫ w in a..t, (G' w).to_real) + ∫ w in t..x, (G' w).to_real : add_le_add ht.1 hx ... = ∫ w in a..x, (G' w).to_real : begin apply integral_add_adjacent_intervals, { rw interval_integrable_iff_integrable_Ioc_of_le ht.2.1, exact integrable_on.mono_set G'int (Ioc_subset_Icc_self.trans (Icc_subset_Icc le_rfl ht.2.2.le)) }, { rw interval_integrable_iff_integrable_Ioc_of_le h'x.1.le, apply integrable_on.mono_set G'int, refine Ioc_subset_Icc_self.trans (Icc_subset_Icc ht.2.1 (h'x.2.trans (min_le_right _ _))) } end }, -- now that we know that `s` contains `[a, b]`, we get the desired result by applying this to `b`. calc g b - g a ≤ ∫ y in a..b, (G' y).to_real : main (right_mem_Icc.2 hab) ... ≤ (∫ y in a..b, φ y) + ε : begin convert hG'.le; { rw interval_integral.integral_of_le hab, simp only [integral_Icc_eq_integral_Ioc', real.volume_singleton] }, end end /-- Hard part of FTC-2 for integrable derivatives, real-valued functions: one has `g b - g a ≤ ∫ y in a..b, g' y` when `g'` is integrable. Auxiliary lemma in the proof of `integral_eq_sub_of_has_deriv_right_of_le`. We give the slightly more general version that `g b - g a ≤ ∫ y in a..b, φ y` when `g' ≤ φ` and `φ` is integrable (even if `g'` is not known to be integrable). Version assuming that `g` is differentiable on `(a, b)`. -/ lemma sub_le_integral_of_has_deriv_right_of_le (hab : a ≤ b) (hcont : continuous_on g (Icc a b)) (hderiv : ∀ x ∈ Ioo a b, has_deriv_within_at g (g' x) (Ioi x) x) (φint : integrable_on φ (Icc a b)) (hφg : ∀ x ∈ Ioo a b, g' x ≤ φ x) : g b - g a ≤ ∫ y in a..b, φ y := begin -- This follows from the version on a closed-open interval (applied to `[t, b)` for `t` close to -- `a`) and a continuity argument. obtain rfl|a_lt_b := hab.eq_or_lt, { simp }, set s := {t | g b - g t ≤ ∫ u in t..b, φ u} ∩ Icc a b, have s_closed : is_closed s, { have : continuous_on (λ t, (g b - g t, ∫ u in t..b, φ u)) (Icc a b), { rw ← interval_of_le hab at ⊢ hcont φint, exact (continuous_on_const.sub hcont).prod (continuous_on_primitive_interval_left φint) }, simp only [s, inter_comm], exact this.preimage_closed_of_closed is_closed_Icc is_closed_le_prod, }, have A : closure (Ioc a b) ⊆ s, { apply s_closed.closure_subset_iff.2, assume t ht, refine ⟨_, ⟨ht.1.le, ht.2⟩⟩, exact sub_le_integral_of_has_deriv_right_of_le_Ico ht.2 (hcont.mono (Icc_subset_Icc ht.1.le le_rfl)) (λ x hx, hderiv x ⟨ht.1.trans_le hx.1, hx.2⟩) (φint.mono_set (Icc_subset_Icc ht.1.le le_rfl)) (λ x hx, hφg x ⟨ht.1.trans_le hx.1, hx.2⟩) }, rw closure_Ioc a_lt_b.ne at A, exact (A (left_mem_Icc.2 hab)).1, end /-- Auxiliary lemma in the proof of `integral_eq_sub_of_has_deriv_right_of_le`. -/ lemma integral_le_sub_of_has_deriv_right_of_le (hab : a ≤ b) (hcont : continuous_on g (Icc a b)) (hderiv : ∀ x ∈ Ioo a b, has_deriv_within_at g (g' x) (Ioi x) x) (φint : integrable_on φ (Icc a b)) (hφg : ∀ x ∈ Ioo a b, φ x ≤ g' x) : ∫ y in a..b, φ y ≤ g b - g a := begin rw ← neg_le_neg_iff, convert sub_le_integral_of_has_deriv_right_of_le hab hcont.neg (λ x hx, (hderiv x hx).neg) φint.neg (λ x hx, neg_le_neg (hφg x hx)), { abel }, { simp only [← integral_neg], refl }, end /-- Auxiliary lemma in the proof of `integral_eq_sub_of_has_deriv_right_of_le`: real version -/ lemma integral_eq_sub_of_has_deriv_right_of_le_real (hab : a ≤ b) (hcont : continuous_on g (Icc a b)) (hderiv : ∀ x ∈ Ioo a b, has_deriv_within_at g (g' x) (Ioi x) x) (g'int : integrable_on g' (Icc a b)) : ∫ y in a..b, g' y = g b - g a := le_antisymm (integral_le_sub_of_has_deriv_right_of_le hab hcont hderiv g'int (λ x hx, le_rfl)) (sub_le_integral_of_has_deriv_right_of_le hab hcont hderiv g'int (λ x hx, le_rfl)) variable {f' : ℝ → E} /-- **Fundamental theorem of calculus-2**: If `f : ℝ → E` is continuous on `[a, b]` (where `a ≤ b`) and has a right derivative at `f' x` for all `x` in `(a, b)`, and `f'` is integrable on `[a, b]`, then `∫ y in a..b, f' y` equals `f b - f a`. -/ theorem integral_eq_sub_of_has_deriv_right_of_le (hab : a ≤ b) (hcont : continuous_on f (Icc a b)) (hderiv : ∀ x ∈ Ioo a b, has_deriv_within_at f (f' x) (Ioi x) x) (f'int : interval_integrable f' volume a b) : ∫ y in a..b, f' y = f b - f a := begin refine (normed_space.eq_iff_forall_dual_eq ℝ).2 (λ g, _), rw [← g.interval_integral_comp_comm f'int, g.map_sub], exact integral_eq_sub_of_has_deriv_right_of_le_real hab (g.continuous.comp_continuous_on hcont) (λ x hx, g.has_fderiv_at.comp_has_deriv_within_at x (hderiv x hx)) (g.integrable_comp ((interval_integrable_iff_integrable_Icc_of_le hab).1 f'int)) end /-- Fundamental theorem of calculus-2: If `f : ℝ → E` is continuous on `[a, b]` and has a right derivative at `f' x` for all `x` in `[a, b)`, and `f'` is integrable on `[a, b]` then `∫ y in a..b, f' y` equals `f b - f a`. -/ theorem integral_eq_sub_of_has_deriv_right (hcont : continuous_on f (interval a b)) (hderiv : ∀ x ∈ Ioo (min a b) (max a b), has_deriv_within_at f (f' x) (Ioi x) x) (hint : interval_integrable f' volume a b) : ∫ y in a..b, f' y = f b - f a := begin cases le_total a b with hab hab, { simp only [interval_of_le, min_eq_left, max_eq_right, hab] at hcont hderiv hint, apply integral_eq_sub_of_has_deriv_right_of_le hab hcont hderiv hint }, { simp only [interval_of_ge, min_eq_right, max_eq_left, hab] at hcont hderiv, rw [integral_symm, integral_eq_sub_of_has_deriv_right_of_le hab hcont hderiv hint.symm, neg_sub] } end /-- Fundamental theorem of calculus-2: If `f : ℝ → E` is continuous on `[a, b]` (where `a ≤ b`) and has a derivative at `f' x` for all `x` in `(a, b)`, and `f'` is integrable on `[a, b]`, then `∫ y in a..b, f' y` equals `f b - f a`. -/ theorem integral_eq_sub_of_has_deriv_at_of_le (hab : a ≤ b) (hcont : continuous_on f (Icc a b)) (hderiv : ∀ x ∈ Ioo a b, has_deriv_at f (f' x) x) (hint : interval_integrable f' volume a b) : ∫ y in a..b, f' y = f b - f a := integral_eq_sub_of_has_deriv_right_of_le hab hcont (λ x hx, (hderiv x hx).has_deriv_within_at) hint /-- Fundamental theorem of calculus-2: If `f : ℝ → E` has a derivative at `f' x` for all `x` in `[a, b]` and `f'` is integrable on `[a, b]`, then `∫ y in a..b, f' y` equals `f b - f a`. -/ theorem integral_eq_sub_of_has_deriv_at (hderiv : ∀ x ∈ interval a b, has_deriv_at f (f' x) x) (hint : interval_integrable f' volume a b) : ∫ y in a..b, f' y = f b - f a := integral_eq_sub_of_has_deriv_right (has_deriv_at.continuous_on hderiv) (λ x hx, (hderiv _ (mem_Icc_of_Ioo hx)).has_deriv_within_at) hint theorem integral_eq_sub_of_has_deriv_at_of_tendsto (hab : a < b) {fa fb} (hderiv : ∀ x ∈ Ioo a b, has_deriv_at f (f' x) x) (hint : interval_integrable f' volume a b) (ha : tendsto f (𝓝[>] a) (𝓝 fa)) (hb : tendsto f (𝓝[<] b) (𝓝 fb)) : ∫ y in a..b, f' y = fb - fa := begin set F : ℝ → E := update (update f a fa) b fb, have Fderiv : ∀ x ∈ Ioo a b, has_deriv_at F (f' x) x, { refine λ x hx, (hderiv x hx).congr_of_eventually_eq _, filter_upwards [Ioo_mem_nhds hx.1 hx.2] with _ hy, simp only [F], rw [update_noteq hy.2.ne, update_noteq hy.1.ne'], }, have hcont : continuous_on F (Icc a b), { rw [continuous_on_update_iff, continuous_on_update_iff, Icc_diff_right, Ico_diff_left], refine ⟨⟨λ z hz, (hderiv z hz).continuous_at.continuous_within_at, _⟩, _⟩, { exact λ _, ha.mono_left (nhds_within_mono _ Ioo_subset_Ioi_self) }, { rintro -, refine (hb.congr' _).mono_left (nhds_within_mono _ Ico_subset_Iio_self), filter_upwards [Ioo_mem_nhds_within_Iio (right_mem_Ioc.2 hab)] with _ hz using (update_noteq hz.1.ne' _ _).symm } }, simpa [F, hab.ne, hab.ne'] using integral_eq_sub_of_has_deriv_at_of_le hab.le hcont Fderiv hint end /-- Fundamental theorem of calculus-2: If `f : ℝ → E` is differentiable at every `x` in `[a, b]` and its derivative is integrable on `[a, b]`, then `∫ y in a..b, deriv f y` equals `f b - f a`. -/ theorem integral_deriv_eq_sub (hderiv : ∀ x ∈ interval a b, differentiable_at ℝ f x) (hint : interval_integrable (deriv f) volume a b) : ∫ y in a..b, deriv f y = f b - f a := integral_eq_sub_of_has_deriv_at (λ x hx, (hderiv x hx).has_deriv_at) hint theorem integral_deriv_eq_sub' (f) (hderiv : deriv f = f') (hdiff : ∀ x ∈ interval a b, differentiable_at ℝ f x) (hcont : continuous_on f' (interval a b)) : ∫ y in a..b, f' y = f b - f a := begin rw [← hderiv, integral_deriv_eq_sub hdiff], rw hderiv, exact hcont.interval_integrable end /-! ### Automatic integrability for nonnegative derivatives -/ /-- When the right derivative of a function is nonnegative, then it is automatically integrable. -/ lemma integrable_on_deriv_right_of_nonneg (hab : a ≤ b) (hcont : continuous_on g (Icc a b)) (hderiv : ∀ x ∈ Ioo a b, has_deriv_within_at g (g' x) (Ioi x) x) (g'pos : ∀ x ∈ Ioo a b, 0 ≤ g' x) : integrable_on g' (Ioc a b) := begin rw integrable_on_Ioc_iff_integrable_on_Ioo, have meas_g' : ae_measurable g' (volume.restrict (Ioo a b)), { apply (ae_measurable_deriv_within_Ioi g _).congr, refine (ae_restrict_mem measurable_set_Ioo).mono (λ x hx, _), exact (hderiv x hx).deriv_within (unique_diff_within_at_Ioi _) }, suffices H : ∫⁻ x in Ioo a b, ∥g' x∥₊ ≤ ennreal.of_real (g b - g a), from ⟨meas_g'.ae_strongly_measurable, H.trans_lt ennreal.of_real_lt_top⟩, by_contra' H, obtain ⟨f, fle, fint, hf⟩ : ∃ (f : simple_func ℝ ℝ≥0), (∀ x, f x ≤ ∥g' x∥₊) ∧ ∫⁻ (x : ℝ) in Ioo a b, f x < ∞ ∧ ennreal.of_real (g b - g a) < ∫⁻ (x : ℝ) in Ioo a b, f x := exists_lt_lintegral_simple_func_of_lt_lintegral H, let F : ℝ → ℝ := coe ∘ f, have intF : integrable_on F (Ioo a b), { refine ⟨f.measurable.coe_nnreal_real.ae_strongly_measurable, _⟩, simpa only [has_finite_integral, nnreal.nnnorm_eq] using fint }, have A : ∫⁻ (x : ℝ) in Ioo a b, f x = ennreal.of_real (∫ x in Ioo a b, F x) := lintegral_coe_eq_integral _ intF, rw A at hf, have B : ∫ (x : ℝ) in Ioo a b, F x ≤ g b - g a, { rw [← integral_Ioc_eq_integral_Ioo, ← interval_integral.integral_of_le hab], apply integral_le_sub_of_has_deriv_right_of_le hab hcont hderiv _ (λ x hx, _), { rwa integrable_on_Icc_iff_integrable_on_Ioo }, { convert nnreal.coe_le_coe.2 (fle x), simp only [real.norm_of_nonneg (g'pos x hx), coe_nnnorm] } }, exact lt_irrefl _ (hf.trans_le (ennreal.of_real_le_of_real B)), end /-- When the derivative of a function is nonnegative, then it is automatically integrable, Ioc version. -/ lemma integrable_on_deriv_of_nonneg (hab : a ≤ b) (hcont : continuous_on g (Icc a b)) (hderiv : ∀ x ∈ Ioo a b, has_deriv_at g (g' x) x) (g'pos : ∀ x ∈ Ioo a b, 0 ≤ g' x) : integrable_on g' (Ioc a b) := integrable_on_deriv_right_of_nonneg hab hcont (λ x hx, (hderiv x hx).has_deriv_within_at) g'pos /-- When the derivative of a function is nonnegative, then it is automatically integrable, interval version. -/ theorem interval_integrable_deriv_of_nonneg (hcont : continuous_on g (interval a b)) (hderiv : ∀ x ∈ Ioo (min a b) (max a b), has_deriv_at g (g' x) x) (hpos : ∀ x ∈ Ioo (min a b) (max a b), 0 ≤ g' x) : interval_integrable g' volume a b := begin cases le_total a b with hab hab, { simp only [interval_of_le, min_eq_left, max_eq_right, hab, interval_integrable, hab, Ioc_eq_empty_of_le, integrable_on_empty, and_true] at hcont hderiv hpos ⊢, exact integrable_on_deriv_of_nonneg hab hcont hderiv hpos, }, { simp only [interval_of_ge, min_eq_right, max_eq_left, hab, interval_integrable, Ioc_eq_empty_of_le, integrable_on_empty, true_and] at hcont hderiv hpos ⊢, exact integrable_on_deriv_of_nonneg hab hcont hderiv hpos } end /-! ### Integration by parts -/ theorem integral_deriv_mul_eq_sub {u v u' v' : ℝ → ℝ} (hu : ∀ x ∈ interval a b, has_deriv_at u (u' x) x) (hv : ∀ x ∈ interval a b, has_deriv_at v (v' x) x) (hu' : interval_integrable u' volume a b) (hv' : interval_integrable v' volume a b) : ∫ x in a..b, u' x * v x + u x * v' x = u b * v b - u a * v a := integral_eq_sub_of_has_deriv_at (λ x hx, (hu x hx).mul (hv x hx)) $ (hu'.mul_continuous_on (has_deriv_at.continuous_on hv)).add (hv'.continuous_on_mul ((has_deriv_at.continuous_on hu))) theorem integral_mul_deriv_eq_deriv_mul {u v u' v' : ℝ → ℝ} (hu : ∀ x ∈ interval a b, has_deriv_at u (u' x) x) (hv : ∀ x ∈ interval a b, has_deriv_at v (v' x) x) (hu' : interval_integrable u' volume a b) (hv' : interval_integrable v' volume a b) : ∫ x in a..b, u x * v' x = u b * v b - u a * v a - ∫ x in a..b, v x * u' x := begin rw [← integral_deriv_mul_eq_sub hu hv hu' hv', ← integral_sub], { exact integral_congr (λ x hx, by simp only [mul_comm, add_sub_cancel']) }, { exact ((hu'.mul_continuous_on (has_deriv_at.continuous_on hv)).add (hv'.continuous_on_mul (has_deriv_at.continuous_on hu))) }, { exact hu'.continuous_on_mul (has_deriv_at.continuous_on hv) }, end /-! ### Integration by substitution / Change of variables -/ section smul /-- Change of variables, general form. If `f` is continuous on `[a, b]` and has continuous right-derivative `f'` in `(a, b)`, and `g` is continuous on `f '' [a, b]` then we can substitute `u = f x` to get `∫ x in a..b, f' x • (g ∘ f) x = ∫ u in f a..f b, g u`. We could potentially slightly weaken the conditions, by not requiring that `f'` and `g` are continuous on the endpoints of these intervals, but in that case we need to additionally assume that the functions are integrable on that interval. -/ theorem integral_comp_smul_deriv'' {f f' : ℝ → ℝ} {g : ℝ → E} (hf : continuous_on f [a, b]) (hff' : ∀ x ∈ Ioo (min a b) (max a b), has_deriv_within_at f (f' x) (Ioi x) x) (hf' : continuous_on f' [a, b]) (hg : continuous_on g (f '' [a, b])) : ∫ x in a..b, f' x • (g ∘ f) x= ∫ u in f a..f b, g u := begin have h_cont : continuous_on (λ u, ∫ t in f a..f u, g t) [a, b], { rw [hf.image_interval] at hg, refine (continuous_on_primitive_interval' hg.interval_integrable _).comp hf _, { rw ← hf.image_interval, exact mem_image_of_mem f left_mem_interval }, { rw ← hf.image_interval, exact maps_to_image _ _ } }, have h_der : ∀ x ∈ Ioo (min a b) (max a b), has_deriv_within_at (λ u, ∫ t in f a..f u, g t) (f' x • ((g ∘ f) x)) (Ioi x) x, { intros x hx, let I := [Inf (f '' [a, b]), Sup (f '' [a, b])], have hI : f '' [a, b] = I := hf.image_interval, have h2x : f x ∈ I, { rw [← hI], exact mem_image_of_mem f (Ioo_subset_Icc_self hx) }, have h2g : interval_integrable g volume (f a) (f x), { refine (hg.mono $ _).interval_integrable, exact hf.surj_on_interval left_mem_interval (Ioo_subset_Icc_self hx) }, rw [hI] at hg, have h3g : strongly_measurable_at_filter g (𝓝[I] f x) volume := hg.strongly_measurable_at_filter_nhds_within measurable_set_Icc (f x), haveI : fact (f x ∈ I) := ⟨h2x⟩, have : has_deriv_within_at (λ u, ∫ x in f a..u, g x) (g (f x)) I (f x) := integral_has_deriv_within_at_right h2g h3g (hg (f x) h2x), refine (this.scomp x ((hff' x hx).Ioo_of_Ioi hx.2) _).Ioi_of_Ioo hx.2, rw ← hI, exact (maps_to_image _ _).mono (Ioo_subset_Icc_self.trans $ Icc_subset_Icc_left hx.1.le) subset.rfl }, have h_int : interval_integrable (λ (x : ℝ), f' x • (g ∘ f) x) volume a b := (hf'.smul (hg.comp hf $ subset_preimage_image f _)).interval_integrable, simp_rw [integral_eq_sub_of_has_deriv_right h_cont h_der h_int, integral_same, sub_zero], end /-- Change of variables. If `f` is has continuous derivative `f'` on `[a, b]`, and `g` is continuous on `f '' [a, b]`, then we can substitute `u = f x` to get `∫ x in a..b, f' x • (g ∘ f) x = ∫ u in f a..f b, g u`. Compared to `interval_integral.integral_comp_smul_deriv` we only require that `g` is continuous on `f '' [a, b]`. -/ theorem integral_comp_smul_deriv' {f f' : ℝ → ℝ} {g : ℝ → E} (h : ∀ x ∈ interval a b, has_deriv_at f (f' x) x) (h' : continuous_on f' (interval a b)) (hg : continuous_on g (f '' [a, b])) : ∫ x in a..b, f' x • (g ∘ f) x = ∫ x in f a..f b, g x := integral_comp_smul_deriv'' (λ x hx, (h x hx).continuous_at.continuous_within_at) (λ x hx, (h x $ Ioo_subset_Icc_self hx).has_deriv_within_at) h' hg /-- Change of variables, most common version. If `f` is has continuous derivative `f'` on `[a, b]`, and `g` is continuous, then we can substitute `u = f x` to get `∫ x in a..b, f' x • (g ∘ f) x = ∫ u in f a..f b, g u`. -/ theorem integral_comp_smul_deriv {f f' : ℝ → ℝ} {g : ℝ → E} (h : ∀ x ∈ interval a b, has_deriv_at f (f' x) x) (h' : continuous_on f' (interval a b)) (hg : continuous g) : ∫ x in a..b, f' x • (g ∘ f) x = ∫ x in f a..f b, g x := integral_comp_smul_deriv' h h' hg.continuous_on theorem integral_deriv_comp_smul_deriv' {f f' : ℝ → ℝ} {g g' : ℝ → E} (hf : continuous_on f [a, b]) (hff' : ∀ x ∈ Ioo (min a b) (max a b), has_deriv_within_at f (f' x) (Ioi x) x) (hf' : continuous_on f' [a, b]) (hg : continuous_on g [f a, f b]) (hgg' : ∀ x ∈ Ioo (min (f a) (f b)) (max (f a) (f b)), has_deriv_within_at g (g' x) (Ioi x) x) (hg' : continuous_on g' (f '' [a, b])) : ∫ x in a..b, f' x • (g' ∘ f) x = (g ∘ f) b - (g ∘ f) a := begin rw [integral_comp_smul_deriv'' hf hff' hf' hg', integral_eq_sub_of_has_deriv_right hg hgg' (hg'.mono _).interval_integrable], exact intermediate_value_interval hf end theorem integral_deriv_comp_smul_deriv {f f' : ℝ → ℝ} {g g' : ℝ → E} (hf : ∀ x ∈ interval a b, has_deriv_at f (f' x) x) (hg : ∀ x ∈ interval a b, has_deriv_at g (g' (f x)) (f x)) (hf' : continuous_on f' (interval a b)) (hg' : continuous g') : ∫ x in a..b, f' x • (g' ∘ f) x = (g ∘ f) b - (g ∘ f) a := integral_eq_sub_of_has_deriv_at (λ x hx, (hg x hx).scomp x $ hf x hx) (hf'.smul (hg'.comp_continuous_on $ has_deriv_at.continuous_on hf)).interval_integrable end smul section mul /-- Change of variables, general form for scalar functions. If `f` is continuous on `[a, b]` and has continuous right-derivative `f'` in `(a, b)`, and `g` is continuous on `f '' [a, b]` then we can substitute `u = f x` to get `∫ x in a..b, (g ∘ f) x * f' x = ∫ u in f a..f b, g u`. -/ theorem integral_comp_mul_deriv'' {f f' g : ℝ → ℝ} (hf : continuous_on f [a, b]) (hff' : ∀ x ∈ Ioo (min a b) (max a b), has_deriv_within_at f (f' x) (Ioi x) x) (hf' : continuous_on f' [a, b]) (hg : continuous_on g (f '' [a, b])) : ∫ x in a..b, (g ∘ f) x * f' x = ∫ u in f a..f b, g u := by simpa [mul_comm] using integral_comp_smul_deriv'' hf hff' hf' hg /-- Change of variables. If `f` is has continuous derivative `f'` on `[a, b]`, and `g` is continuous on `f '' [a, b]`, then we can substitute `u = f x` to get `∫ x in a..b, (g ∘ f) x * f' x = ∫ u in f a..f b, g u`. Compared to `interval_integral.integral_comp_mul_deriv` we only require that `g` is continuous on `f '' [a, b]`. -/ theorem integral_comp_mul_deriv' {f f' g : ℝ → ℝ} (h : ∀ x ∈ interval a b, has_deriv_at f (f' x) x) (h' : continuous_on f' (interval a b)) (hg : continuous_on g (f '' [a, b])) : ∫ x in a..b, (g ∘ f) x * f' x = ∫ x in f a..f b, g x := by simpa [mul_comm] using integral_comp_smul_deriv' h h' hg /-- Change of variables, most common version. If `f` is has continuous derivative `f'` on `[a, b]`, and `g` is continuous, then we can substitute `u = f x` to get `∫ x in a..b, (g ∘ f) x * f' x = ∫ u in f a..f b, g u`. -/ theorem integral_comp_mul_deriv {f f' g : ℝ → ℝ} (h : ∀ x ∈ interval a b, has_deriv_at f (f' x) x) (h' : continuous_on f' (interval a b)) (hg : continuous g) : ∫ x in a..b, (g ∘ f) x * f' x = ∫ x in f a..f b, g x := integral_comp_mul_deriv' h h' hg.continuous_on theorem integral_deriv_comp_mul_deriv' {f f' g g' : ℝ → ℝ} (hf : continuous_on f [a, b]) (hff' : ∀ x ∈ Ioo (min a b) (max a b), has_deriv_within_at f (f' x) (Ioi x) x) (hf' : continuous_on f' [a, b]) (hg : continuous_on g [f a, f b]) (hgg' : ∀ x ∈ Ioo (min (f a) (f b)) (max (f a) (f b)), has_deriv_within_at g (g' x) (Ioi x) x) (hg' : continuous_on g' (f '' [a, b])) : ∫ x in a..b, (g' ∘ f) x * f' x = (g ∘ f) b - (g ∘ f) a := by simpa [mul_comm] using integral_deriv_comp_smul_deriv' hf hff' hf' hg hgg' hg' theorem integral_deriv_comp_mul_deriv {f f' g g' : ℝ → ℝ} (hf : ∀ x ∈ interval a b, has_deriv_at f (f' x) x) (hg : ∀ x ∈ interval a b, has_deriv_at g (g' (f x)) (f x)) (hf' : continuous_on f' (interval a b)) (hg' : continuous g') : ∫ x in a..b, (g' ∘ f) x * f' x = (g ∘ f) b - (g ∘ f) a := by simpa [mul_comm] using integral_deriv_comp_smul_deriv hf hg hf' hg' end mul end interval_integral
119aee038fe0376a945f6cefabd527ca51d5f623
ddf69e0b8ad10bfd251aa1fb492bd92f064768ec
/src/field_theory/minimal_polynomial.lean
3e615846e0ad4bf97e0cf729a99d4486593608bb
[ "Apache-2.0" ]
permissive
MaboroshiChan/mathlib
db1c1982df384a2604b19a5e1f5c6464c7c76de1
7f74e6b35f6bac86b9218250e83441ac3e17264c
refs/heads/master
1,671,993,587,476
1,601,911,102,000
1,601,911,102,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
8,502
lean
/- Copyright (c) 2019 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Johan Commelin -/ import ring_theory.integral_closure import data.polynomial.field_division /-! # Minimal polynomials This file defines the minimal polynomial of an element x of an A-algebra B, under the assumption that x is integral over A. After stating the defining property we specialize to the setting of field extensions and derive some well-known properties, amongst which the fact that minimal polynomials are irreducible, and uniquely determined by their defining property. -/ universes u v w open_locale classical open polynomial set function variables {α : Type u} {β : Type v} section min_poly_def variables [comm_ring α] [ring β] [algebra α β] /-- Let B be an A-algebra, and x an element of B that is integral over A. The minimal polynomial of x is a monic polynomial of smallest degree that has x as its root. -/ noncomputable def minimal_polynomial {x : β} (hx : is_integral α x) : polynomial α := well_founded.min degree_lt_wf _ hx end min_poly_def namespace minimal_polynomial section ring variables [comm_ring α] [ring β] [algebra α β] variables {x : β} (hx : is_integral α x) /--A minimal polynomial is monic.-/ lemma monic : monic (minimal_polynomial hx) := (well_founded.min_mem degree_lt_wf _ hx).1 /--An element is a root of its minimal polynomial.-/ @[simp] lemma aeval : aeval x (minimal_polynomial hx) = 0 := (well_founded.min_mem degree_lt_wf _ hx).2 /--The defining property of the minimal polynomial of an element x: it is the monic polynomial with smallest degree that has x as its root.-/ lemma min {p : polynomial α} (pmonic : p.monic) (hp : polynomial.aeval x p = 0) : degree (minimal_polynomial hx) ≤ degree p := le_of_not_lt $ well_founded.not_lt_min degree_lt_wf _ hx ⟨pmonic, hp⟩ end ring section field variables [field α] section ring variables [ring β] [algebra α β] variables {x : β} (hx : is_integral α x) /--A minimal polynomial is nonzero.-/ lemma ne_zero : (minimal_polynomial hx) ≠ 0 := ne_zero_of_monic (monic hx) /--If an element x is a root of a nonzero polynomial p, then the degree of p is at least the degree of the minimal polynomial of x.-/ lemma degree_le_of_ne_zero {p : polynomial α} (pnz : p ≠ 0) (hp : polynomial.aeval x p = 0) : degree (minimal_polynomial hx) ≤ degree p := calc degree (minimal_polynomial hx) ≤ degree (p * C (leading_coeff p)⁻¹) : min _ (monic_mul_leading_coeff_inv pnz) (by simp [hp]) ... = degree p : degree_mul_leading_coeff_inv p pnz /--The minimal polynomial of an element x is uniquely characterized by its defining property: if there is another monic polynomial of minimal degree that has x as a root, then this polynomial is equal to the minimal polynomial of x.-/ lemma unique {p : polynomial α} (pmonic : p.monic) (hp : polynomial.aeval x p = 0) (pmin : ∀ q : polynomial α, q.monic → polynomial.aeval x q = 0 → degree p ≤ degree q) : p = minimal_polynomial hx := begin symmetry, apply eq_of_sub_eq_zero, by_contra hnz, have := degree_le_of_ne_zero hx hnz (by simp [hp]), contrapose! this, apply degree_sub_lt _ (ne_zero hx), { rw [(monic hx).leading_coeff, pmonic.leading_coeff] }, { exact le_antisymm (min hx pmonic hp) (pmin (minimal_polynomial hx) (monic hx) (aeval hx)) }, end /--If an element x is a root of a polynomial p, then the minimal polynomial of x divides p.-/ lemma dvd {p : polynomial α} (hp : polynomial.aeval x p = 0) : minimal_polynomial hx ∣ p := begin rw ← dvd_iff_mod_by_monic_eq_zero (monic hx), by_contra hnz, have := degree_le_of_ne_zero hx hnz _, { contrapose! this, exact degree_mod_by_monic_lt _ (monic hx) (ne_zero hx) }, { rw ← mod_by_monic_add_div p (monic hx) at hp, simpa using hp } end lemma dvd_map_of_is_scalar_tower {α γ : Type*} (β : Type*) [comm_ring α] [field β] [comm_ring γ] [algebra α β] [algebra α γ] [algebra β γ] [is_scalar_tower α β γ] {x : γ} (hx : is_integral α x) : minimal_polynomial (is_integral_of_is_scalar_tower x hx) ∣ (minimal_polynomial hx).map (algebra_map α β) := by { apply minimal_polynomial.dvd, rw [← is_scalar_tower.aeval_apply, minimal_polynomial.aeval] } variables [nontrivial β] /--The degree of a minimal polynomial is nonzero.-/ lemma degree_ne_zero : degree (minimal_polynomial hx) ≠ 0 := begin assume deg_eq_zero, have ndeg_eq_zero : nat_degree (minimal_polynomial hx) = 0, { simpa using congr_arg nat_degree (eq_C_of_degree_eq_zero deg_eq_zero) }, have eq_one : minimal_polynomial hx = 1, { rw eq_C_of_degree_eq_zero deg_eq_zero, convert C_1, simpa [ndeg_eq_zero.symm] using (monic hx).leading_coeff }, simpa [eq_one, aeval_def] using aeval hx end /--A minimal polynomial is not a unit.-/ lemma not_is_unit : ¬ is_unit (minimal_polynomial hx) := assume H, degree_ne_zero hx $ degree_eq_zero_of_is_unit H /--The degree of a minimal polynomial is positive.-/ lemma degree_pos : 0 < degree (minimal_polynomial hx) := degree_pos_of_ne_zero_of_nonunit (ne_zero hx) (not_is_unit hx) theorem unique' {p : polynomial α} (hp1 : _root_.irreducible p) (hp2 : polynomial.aeval x p = 0) (hp3 : p.monic) : p = minimal_polynomial hx := let ⟨q, hq⟩ := dvd hx hp2 in eq_of_monic_of_associated hp3 (monic hx) $ mul_one (minimal_polynomial hx) ▸ hq.symm ▸ associated_mul_mul (associated.refl _) $ associated_one_iff_is_unit.2 $ (hp1.is_unit_or_is_unit hq).resolve_left $ not_is_unit hx /--If L/K is a field extension, and x is an element of L in the image of K, then the minimal polynomial of x is X - C x.-/ @[simp] protected lemma algebra_map (a : α) (ha : is_integral α (algebra_map α β a)) : minimal_polynomial ha = X - C a := eq.symm $ unique' ha (irreducible_X_sub_C a) (by rw [alg_hom.map_sub, aeval_X, aeval_C, sub_self]) (monic_X_sub_C a) variable (β) /--If L/K is a field extension, and x is an element of L in the image of K, then the minimal polynomial of x is X - C x.-/ lemma algebra_map' (a : α) : minimal_polynomial (@is_integral_algebra_map α β _ _ _ a) = X - C a := minimal_polynomial.algebra_map _ _ variable {β} /--The minimal polynomial of 0 is X.-/ @[simp] lemma zero {h₀ : is_integral α (0:β)} : minimal_polynomial h₀ = X := by simpa only [add_zero, C_0, sub_eq_add_neg, neg_zero, ring_hom.map_zero] using algebra_map' β (0:α) /--The minimal polynomial of 1 is X - 1.-/ @[simp] lemma one {h₁ : is_integral α (1:β)} : minimal_polynomial h₁ = X - 1 := by simpa only [ring_hom.map_one, C_1, sub_eq_add_neg] using algebra_map' β (1:α) end ring section domain variables [domain β] [algebra α β] variables {x : β} (hx : is_integral α x) /--A minimal polynomial is prime.-/ lemma prime : prime (minimal_polynomial hx) := begin refine ⟨ne_zero hx, not_is_unit hx, _⟩, rintros p q ⟨d, h⟩, have : polynomial.aeval x (p*q) = 0 := by simp [h, aeval hx], replace : polynomial.aeval x p = 0 ∨ polynomial.aeval x q = 0 := by simpa, exact or.imp (dvd hx) (dvd hx) this end /--A minimal polynomial is irreducible.-/ lemma irreducible : irreducible (minimal_polynomial hx) := irreducible_of_prime (prime hx) /--If L/K is a field extension and an element y of K is a root of the minimal polynomial of an element x ∈ L, then y maps to x under the field embedding.-/ lemma root {x : β} (hx : is_integral α x) {y : α} (h : is_root (minimal_polynomial hx) y) : algebra_map α β y = x := have key : minimal_polynomial hx = X - C y := eq_of_monic_of_associated (monic hx) (monic_X_sub_C y) (associated_of_dvd_dvd (dvd_symm_of_irreducible (irreducible_X_sub_C y) (irreducible hx) (dvd_iff_is_root.2 h)) (dvd_iff_is_root.2 h)), by { have := aeval hx, rwa [key, alg_hom.map_sub, aeval_X, aeval_C, sub_eq_zero, eq_comm] at this } /--The constant coefficient of the minimal polynomial of x is 0 if and only if x = 0.-/ @[simp] lemma coeff_zero_eq_zero : coeff (minimal_polynomial hx) 0 = 0 ↔ x = 0 := begin split, { intro h, have zero_root := zero_is_root_of_coeff_zero_eq_zero h, rw ← root hx zero_root, exact ring_hom.map_zero _ }, { rintro rfl, simp } end /--The minimal polynomial of a nonzero element has nonzero constant coefficient.-/ lemma coeff_zero_ne_zero (h : x ≠ 0) : coeff (minimal_polynomial hx) 0 ≠ 0 := by { contrapose! h, simpa using h } end domain end field end minimal_polynomial
4138b929fad6c5aaee19cdc26385df75c4912d84
9dc8cecdf3c4634764a18254e94d43da07142918
/src/linear_algebra/linear_pmap.lean
0151edba7d05144eab1b032269a2aa5b447de413
[ "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
29,603
lean
/- Copyright (c) 2020 Yury Kudryashov All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov, Moritz Doll -/ import linear_algebra.basic import linear_algebra.prod /-! # Partially defined linear maps A `linear_pmap R E F` or `E →ₗ.[R] F` is a linear map from a submodule of `E` to `F`. We define a `semilattice_inf` with `order_bot` instance on this this, and define three operations: * `mk_span_singleton` defines a partial linear map defined on the span of a singleton. * `sup` takes two partial linear maps `f`, `g` that agree on the intersection of their domains, and returns the unique partial linear map on `f.domain ⊔ g.domain` that extends both `f` and `g`. * `Sup` takes a `directed_on (≤)` set of partial linear maps, and returns the unique partial linear map on the `Sup` of their domains that extends all these maps. Moreover, we define * `linear_pmap.graph` is the graph of the partial linear map viewed as a submodule of `E × F`. Partially defined maps are currently used in `mathlib` to prove Hahn-Banach theorem and its variations. Namely, `linear_pmap.Sup` implies that every chain of `linear_pmap`s is bounded above. They are also the basis for the theory of unbounded operators. -/ open set universes u v w /-- A `linear_pmap R E F` or `E →ₗ.[R] F` is a linear map from a submodule of `E` to `F`. -/ structure linear_pmap (R : Type u) [ring R] (E : Type v) [add_comm_group E] [module R E] (F : Type w) [add_comm_group F] [module R F] := (domain : submodule R E) (to_fun : domain →ₗ[R] F) notation E ` →ₗ.[`:25 R:25 `] `:0 F:0 := linear_pmap R E F variables {R : Type*} [ring R] {E : Type*} [add_comm_group E] [module R E] {F : Type*} [add_comm_group F] [module R F] {G : Type*} [add_comm_group G] [module R G] namespace linear_pmap open submodule instance : has_coe_to_fun (E →ₗ.[R] F) (λ f : E →ₗ.[R] F, f.domain → F) := ⟨λ f, f.to_fun⟩ @[simp] lemma to_fun_eq_coe (f : E →ₗ.[R] F) (x : f.domain) : f.to_fun x = f x := rfl @[ext] lemma ext {f g : E →ₗ.[R] F} (h : f.domain = g.domain) (h' : ∀ ⦃x : f.domain⦄ ⦃y : g.domain⦄ (h : (x:E) = y), f x = g y) : f = g := begin rcases f with ⟨f_dom, f⟩, rcases g with ⟨g_dom, g⟩, obtain rfl : f_dom = g_dom := h, obtain rfl : f = g := linear_map.ext (λ x, h' rfl), refl, end @[simp] lemma map_zero (f : E →ₗ.[R] F) : f 0 = 0 := f.to_fun.map_zero lemma ext_iff {f g : E →ₗ.[R] F} : f = g ↔ ∃ (domain_eq : f.domain = g.domain), ∀ ⦃x : f.domain⦄ ⦃y : g.domain⦄ (h : (x:E) = y), f x = g y := ⟨λ EQ, EQ ▸ ⟨rfl, λ x y h, by { congr, exact_mod_cast h }⟩, λ ⟨deq, feq⟩, ext deq feq⟩ lemma ext' {s : submodule R E} {f g : s →ₗ[R] F} (h : f = g) : mk s f = mk s g := h ▸ rfl lemma map_add (f : E →ₗ.[R] F) (x y : f.domain) : f (x + y) = f x + f y := f.to_fun.map_add x y lemma map_neg (f : E →ₗ.[R] F) (x : f.domain) : f (-x) = -f x := f.to_fun.map_neg x lemma map_sub (f : E →ₗ.[R] F) (x y : f.domain) : f (x - y) = f x - f y := f.to_fun.map_sub x y lemma map_smul (f : E →ₗ.[R] F) (c : R) (x : f.domain) : f (c • x) = c • f x := f.to_fun.map_smul c x @[simp] lemma mk_apply (p : submodule R E) (f : p →ₗ[R] F) (x : p) : mk p f x = f x := rfl /-- The unique `linear_pmap` on `R ∙ x` that sends `x` to `y`. This version works for modules over rings, and requires a proof of `∀ c, c • x = 0 → c • y = 0`. -/ noncomputable def mk_span_singleton' (x : E) (y : F) (H : ∀ c : R, c • x = 0 → c • y = 0) : E →ₗ.[R] F := { domain := R ∙ x, to_fun := have H : ∀ c₁ c₂ : R, c₁ • x = c₂ • x → c₁ • y = c₂ • y, { intros c₁ c₂ h, rw [← sub_eq_zero, ← sub_smul] at h ⊢, exact H _ h }, { to_fun := λ z, (classical.some (mem_span_singleton.1 z.prop) • y), map_add' := λ y z, begin rw [← add_smul], apply H, simp only [add_smul, sub_smul, classical.some_spec (mem_span_singleton.1 _)], apply coe_add end, map_smul' := λ c z, begin rw [smul_smul], apply H, simp only [mul_smul, classical.some_spec (mem_span_singleton.1 _)], apply coe_smul end } } @[simp] lemma domain_mk_span_singleton (x : E) (y : F) (H : ∀ c : R, c • x = 0 → c • y = 0) : (mk_span_singleton' x y H).domain = R ∙ x := rfl @[simp] lemma mk_span_singleton'_apply (x : E) (y : F) (H : ∀ c : R, c • x = 0 → c • y = 0) (c : R) (h) : mk_span_singleton' x y H ⟨c • x, h⟩ = c • y := begin dsimp [mk_span_singleton'], rw [← sub_eq_zero, ← sub_smul], apply H, simp only [sub_smul, one_smul, sub_eq_zero], apply classical.some_spec (mem_span_singleton.1 h), end @[simp] lemma mk_span_singleton'_apply_self (x : E) (y : F) (H : ∀ c : R, c • x = 0 → c • y = 0) (h) : mk_span_singleton' x y H ⟨x, h⟩ = y := by convert mk_span_singleton'_apply x y H 1 _; rwa one_smul /-- The unique `linear_pmap` on `span R {x}` that sends a non-zero vector `x` to `y`. This version works for modules over division rings. -/ @[reducible] noncomputable def mk_span_singleton {K E F : Type*} [division_ring K] [add_comm_group E] [module K E] [add_comm_group F] [module K F] (x : E) (y : F) (hx : x ≠ 0) : E →ₗ.[K] F := mk_span_singleton' x y $ λ c hc, (smul_eq_zero.1 hc).elim (λ hc, by rw [hc, zero_smul]) (λ hx', absurd hx' hx) lemma mk_span_singleton_apply (K : Type*) {E F : Type*} [division_ring K] [add_comm_group E] [module K E] [add_comm_group F] [module K F] {x : E} (hx : x ≠ 0) (y : F) : mk_span_singleton x y hx ⟨x, (submodule.mem_span_singleton_self x : x ∈ submodule.span K {x})⟩ = y := linear_pmap.mk_span_singleton'_apply_self _ _ _ _ /-- Projection to the first coordinate as a `linear_pmap` -/ protected def fst (p : submodule R E) (p' : submodule R F) : (E × F) →ₗ.[R] E := { domain := p.prod p', to_fun := (linear_map.fst R E F).comp (p.prod p').subtype } @[simp] lemma fst_apply (p : submodule R E) (p' : submodule R F) (x : p.prod p') : linear_pmap.fst p p' x = (x : E × F).1 := rfl /-- Projection to the second coordinate as a `linear_pmap` -/ protected def snd (p : submodule R E) (p' : submodule R F) : (E × F) →ₗ.[R] F := { domain := p.prod p', to_fun := (linear_map.snd R E F).comp (p.prod p').subtype } @[simp] lemma snd_apply (p : submodule R E) (p' : submodule R F) (x : p.prod p') : linear_pmap.snd p p' x = (x : E × F).2 := rfl instance : has_neg (E →ₗ.[R] F) := ⟨λ f, ⟨f.domain, -f.to_fun⟩⟩ @[simp] lemma neg_apply (f : E →ₗ.[R] F) (x) : (-f) x = -(f x) := rfl instance : has_le (E →ₗ.[R] F) := ⟨λ f g, f.domain ≤ g.domain ∧ ∀ ⦃x : f.domain⦄ ⦃y : g.domain⦄ (h : (x:E) = y), f x = g y⟩ lemma eq_of_le_of_domain_eq {f g : E →ₗ.[R] F} (hle : f ≤ g) (heq : f.domain = g.domain) : f = g := ext heq hle.2 /-- Given two partial linear maps `f`, `g`, the set of points `x` such that both `f` and `g` are defined at `x` and `f x = g x` form a submodule. -/ def eq_locus (f g : E →ₗ.[R] F) : submodule R E := { carrier := {x | ∃ (hf : x ∈ f.domain) (hg : x ∈ g.domain), f ⟨x, hf⟩ = g ⟨x, hg⟩}, zero_mem' := ⟨zero_mem _, zero_mem _, f.map_zero.trans g.map_zero.symm⟩, add_mem' := λ x y ⟨hfx, hgx, hx⟩ ⟨hfy, hgy, hy⟩, ⟨add_mem hfx hfy, add_mem hgx hgy, by erw [f.map_add ⟨x, hfx⟩ ⟨y, hfy⟩, g.map_add ⟨x, hgx⟩ ⟨y, hgy⟩, hx, hy]⟩, smul_mem' := λ c x ⟨hfx, hgx, hx⟩, ⟨smul_mem _ c hfx, smul_mem _ c hgx, by erw [f.map_smul c ⟨x, hfx⟩, g.map_smul c ⟨x, hgx⟩, hx]⟩ } instance : has_inf (E →ₗ.[R] F) := ⟨λ f g, ⟨f.eq_locus g, f.to_fun.comp $ of_le $ λ x hx, hx.fst⟩⟩ instance : has_bot (E →ₗ.[R] F) := ⟨⟨⊥, 0⟩⟩ instance : inhabited (E →ₗ.[R] F) := ⟨⊥⟩ instance : semilattice_inf (E →ₗ.[R] F) := { le := (≤), le_refl := λ f, ⟨le_refl f.domain, λ x y h, subtype.eq h ▸ rfl⟩, le_trans := λ f g h ⟨fg_le, fg_eq⟩ ⟨gh_le, gh_eq⟩, ⟨le_trans fg_le gh_le, λ x z hxz, have hxy : (x:E) = of_le fg_le x, from rfl, (fg_eq hxy).trans (gh_eq $ hxy.symm.trans hxz)⟩, le_antisymm := λ f g fg gf, eq_of_le_of_domain_eq fg (le_antisymm fg.1 gf.1), inf := (⊓), le_inf := λ f g h ⟨fg_le, fg_eq⟩ ⟨fh_le, fh_eq⟩, ⟨λ x hx, ⟨fg_le hx, fh_le hx, by refine (fg_eq _).symm.trans (fh_eq _); [exact ⟨x, hx⟩, refl, refl]⟩, λ x ⟨y, yg, hy⟩ h, by { apply fg_eq, exact h }⟩, inf_le_left := λ f g, ⟨λ x hx, hx.fst, λ x y h, congr_arg f $ subtype.eq $ by exact h⟩, inf_le_right := λ f g, ⟨λ x hx, hx.snd.fst, λ ⟨x, xf, xg, hx⟩ y h, hx.trans $ congr_arg g $ subtype.eq $ by exact h⟩ } instance : order_bot (E →ₗ.[R] F) := { bot := ⊥, bot_le := λ f, ⟨bot_le, λ x y h, have hx : x = 0, from subtype.eq ((mem_bot R).1 x.2), have hy : y = 0, from subtype.eq (h.symm.trans (congr_arg _ hx)), by rw [hx, hy, map_zero, map_zero]⟩ } lemma le_of_eq_locus_ge {f g : E →ₗ.[R] F} (H : f.domain ≤ f.eq_locus g) : f ≤ g := suffices f ≤ f ⊓ g, from le_trans this inf_le_right, ⟨H, λ x y hxy, ((inf_le_left : f ⊓ g ≤ f).2 hxy.symm).symm⟩ lemma domain_mono : strict_mono (@domain R _ E _ _ F _ _) := λ f g hlt, lt_of_le_of_ne hlt.1.1 $ λ heq, ne_of_lt hlt $ eq_of_le_of_domain_eq (le_of_lt hlt) heq private lemma sup_aux (f g : E →ₗ.[R] F) (h : ∀ (x : f.domain) (y : g.domain), (x:E) = y → f x = g y) : ∃ fg : ↥(f.domain ⊔ g.domain) →ₗ[R] F, ∀ (x : f.domain) (y : g.domain) (z), (x:E) + y = ↑z → fg z = f x + g y := begin choose x hx y hy hxy using λ z : f.domain ⊔ g.domain, mem_sup.1 z.prop, set fg := λ z, f ⟨x z, hx z⟩ + g ⟨y z, hy z⟩, have fg_eq : ∀ (x' : f.domain) (y' : g.domain) (z' : f.domain ⊔ g.domain) (H : (x':E) + y' = z'), fg z' = f x' + g y', { intros x' y' z' H, dsimp [fg], rw [add_comm, ← sub_eq_sub_iff_add_eq_add, eq_comm, ← map_sub, ← map_sub], apply h, simp only [← eq_sub_iff_add_eq] at hxy, simp only [add_subgroup_class.coe_sub, coe_mk, coe_mk, hxy, ← sub_add, ← sub_sub, sub_self, zero_sub, ← H], apply neg_add_eq_sub }, refine ⟨{ to_fun := fg, .. }, fg_eq⟩, { rintros ⟨z₁, hz₁⟩ ⟨z₂, hz₂⟩, rw [← add_assoc, add_right_comm (f _), ← map_add, add_assoc, ← map_add], apply fg_eq, simp only [coe_add, coe_mk, ← add_assoc], rw [add_right_comm (x _), hxy, add_assoc, hxy, coe_mk, coe_mk] }, { intros c z, rw [smul_add, ← map_smul, ← map_smul], apply fg_eq, simp only [coe_smul, coe_mk, ← smul_add, hxy, ring_hom.id_apply] }, end /-- Given two partial linear maps that agree on the intersection of their domains, `f.sup g h` is the unique partial linear map on `f.domain ⊔ g.domain` that agrees with `f` and `g`. -/ protected noncomputable def sup (f g : E →ₗ.[R] F) (h : ∀ (x : f.domain) (y : g.domain), (x:E) = y → f x = g y) : E →ₗ.[R] F := ⟨_, classical.some (sup_aux f g h)⟩ @[simp] lemma domain_sup (f g : E →ₗ.[R] F) (h : ∀ (x : f.domain) (y : g.domain), (x:E) = y → f x = g y) : (f.sup g h).domain = f.domain ⊔ g.domain := rfl lemma sup_apply {f g : E →ₗ.[R] F} (H : ∀ (x : f.domain) (y : g.domain), (x:E) = y → f x = g y) (x y z) (hz : (↑x:E) + ↑y = ↑z) : f.sup g H z = f x + g y := classical.some_spec (sup_aux f g H) x y z hz protected lemma left_le_sup (f g : E →ₗ.[R] F) (h : ∀ (x : f.domain) (y : g.domain), (x:E) = y → f x = g y) : f ≤ f.sup g h := begin refine ⟨le_sup_left, λ z₁ z₂ hz, _⟩, rw [← add_zero (f _), ← g.map_zero], refine (sup_apply h _ _ _ _).symm, simpa end protected lemma right_le_sup (f g : E →ₗ.[R] F) (h : ∀ (x : f.domain) (y : g.domain), (x:E) = y → f x = g y) : g ≤ f.sup g h := begin refine ⟨le_sup_right, λ z₁ z₂ hz, _⟩, rw [← zero_add (g _), ← f.map_zero], refine (sup_apply h _ _ _ _).symm, simpa end protected lemma sup_le {f g h : E →ₗ.[R] F} (H : ∀ (x : f.domain) (y : g.domain), (x:E) = y → f x = g y) (fh : f ≤ h) (gh : g ≤ h) : f.sup g H ≤ h := have Hf : f ≤ (f.sup g H) ⊓ h, from le_inf (f.left_le_sup g H) fh, have Hg : g ≤ (f.sup g H) ⊓ h, from le_inf (f.right_le_sup g H) gh, le_of_eq_locus_ge $ sup_le Hf.1 Hg.1 /-- Hypothesis for `linear_pmap.sup` holds, if `f.domain` is disjoint with `g.domain`. -/ lemma sup_h_of_disjoint (f g : E →ₗ.[R] F) (h : disjoint f.domain g.domain) (x : f.domain) (y : g.domain) (hxy : (x:E) = y) : f x = g y := begin rw [disjoint_def] at h, have hy : y = 0, from subtype.eq (h y (hxy ▸ x.2) y.2), have hx : x = 0, from subtype.eq (hxy.trans $ congr_arg _ hy), simp [*] end section smul variables {M N : Type*} [monoid M] [distrib_mul_action M F] [smul_comm_class R M F] variables [monoid N] [distrib_mul_action N F] [smul_comm_class R N F] instance : has_smul M (E →ₗ.[R] F) := ⟨λ a f, { domain := f.domain, to_fun := a • f.to_fun }⟩ @[simp] lemma smul_domain (a : M) (f : E →ₗ.[R] F) : (a • f).domain = f.domain := rfl lemma smul_apply (a : M) (f : E →ₗ.[R] F) (x : ((a • f).domain)) : (a • f) x = a • f x := rfl @[simp] lemma coe_smul (a : M) (f : E →ₗ.[R] F) : ⇑(a • f) = a • f := rfl instance [smul_comm_class M N F] : smul_comm_class M N (E →ₗ.[R] F) := ⟨λ a b f, ext' $ smul_comm a b f.to_fun⟩ instance [has_smul M N] [is_scalar_tower M N F] : is_scalar_tower M N (E →ₗ.[R] F) := ⟨λ a b f, ext' $ smul_assoc a b f.to_fun⟩ instance : mul_action M (E →ₗ.[R] F) := { smul := (•), one_smul := λ ⟨s, f⟩, ext' $ one_smul M f, mul_smul := λ a b f, ext' $ mul_smul a b f.to_fun } end smul section vadd instance : has_vadd (E →ₗ[R] F) (E →ₗ.[R] F) := ⟨λ f g, { domain := g.domain, to_fun := f.comp g.domain.subtype + g.to_fun }⟩ @[simp] lemma vadd_domain (f : E →ₗ[R] F) (g : E →ₗ.[R] F) : (f +ᵥ g).domain = g.domain := rfl lemma vadd_apply (f : E →ₗ[R] F) (g : E →ₗ.[R] F) (x : (f +ᵥ g).domain) : (f +ᵥ g) x = f x + g x := rfl @[simp] lemma coe_vadd (f : E →ₗ[R] F) (g : E →ₗ.[R] F) : ⇑(f +ᵥ g) = f.comp g.domain.subtype + g := rfl instance : add_action (E →ₗ[R] F) (E →ₗ.[R] F) := { vadd := (+ᵥ), zero_vadd := λ ⟨s, f⟩, ext' $ zero_add _, add_vadd := λ f₁ f₂ ⟨s, g⟩, ext' $ linear_map.ext $ λ x, add_assoc _ _ _ } end vadd section variables {K : Type*} [division_ring K] [module K E] [module K F] /-- Extend a `linear_pmap` to `f.domain ⊔ K ∙ x`. -/ noncomputable def sup_span_singleton (f : E →ₗ.[K] F) (x : E) (y : F) (hx : x ∉ f.domain) : E →ₗ.[K] F := f.sup (mk_span_singleton x y (λ h₀, hx $ h₀.symm ▸ f.domain.zero_mem)) $ sup_h_of_disjoint _ _ $ by simpa [disjoint_span_singleton] @[simp] lemma domain_sup_span_singleton (f : E →ₗ.[K] F) (x : E) (y : F) (hx : x ∉ f.domain) : (f.sup_span_singleton x y hx).domain = f.domain ⊔ K ∙ x := rfl @[simp] lemma sup_span_singleton_apply_mk (f : E →ₗ.[K] F) (x : E) (y : F) (hx : x ∉ f.domain) (x' : E) (hx' : x' ∈ f.domain) (c : K) : f.sup_span_singleton x y hx ⟨x' + c • x, mem_sup.2 ⟨x', hx', _, mem_span_singleton.2 ⟨c, rfl⟩, rfl⟩⟩ = f ⟨x', hx'⟩ + c • y := begin erw [sup_apply _ ⟨x', hx'⟩ ⟨c • x, _⟩, mk_span_singleton'_apply], refl, exact mem_span_singleton.2 ⟨c, rfl⟩ end end private lemma Sup_aux (c : set (E →ₗ.[R] F)) (hc : directed_on (≤) c) : ∃ f : ↥(Sup (domain '' c)) →ₗ[R] F, (⟨_, f⟩ : E →ₗ.[R] F) ∈ upper_bounds c := begin cases c.eq_empty_or_nonempty with ceq cne, { subst c, simp }, have hdir : directed_on (≤) (domain '' c), from directed_on_image.2 (hc.mono domain_mono.monotone), have P : Π x : Sup (domain '' c), {p : c // (x : E) ∈ p.val.domain }, { rintros x, apply classical.indefinite_description, have := (mem_Sup_of_directed (cne.image _) hdir).1 x.2, rwa [bex_image_iff, set_coe.exists'] at this }, set f : Sup (domain '' c) → F := λ x, (P x).val.val ⟨x, (P x).property⟩, have f_eq : ∀ (p : c) (x : Sup (domain '' c)) (y : p.1.1) (hxy : (x : E) = y), f x = p.1 y, { intros p x y hxy, rcases hc (P x).1.1 (P x).1.2 p.1 p.2 with ⟨q, hqc, hxq, hpq⟩, refine (hxq.2 _).trans (hpq.2 _).symm, exacts [of_le hpq.1 y, hxy, rfl] }, refine ⟨{ to_fun := f, .. }, _⟩, { intros x y, rcases hc (P x).1.1 (P x).1.2 (P y).1.1 (P y).1.2 with ⟨p, hpc, hpx, hpy⟩, set x' := of_le hpx.1 ⟨x, (P x).2⟩, set y' := of_le hpy.1 ⟨y, (P y).2⟩, rw [f_eq ⟨p, hpc⟩ x x' rfl, f_eq ⟨p, hpc⟩ y y' rfl, f_eq ⟨p, hpc⟩ (x + y) (x' + y') rfl, map_add] }, { intros c x, simp [f_eq (P x).1 (c • x) (c • ⟨x, (P x).2⟩) rfl, ← map_smul] }, { intros p hpc, refine ⟨le_Sup $ mem_image_of_mem domain hpc, λ x y hxy, eq.symm _⟩, exact f_eq ⟨p, hpc⟩ _ _ hxy.symm } end /-- Glue a collection of partially defined linear maps to a linear map defined on `Sup` of these submodules. -/ protected noncomputable def Sup (c : set (E →ₗ.[R] F)) (hc : directed_on (≤) c) : E →ₗ.[R] F := ⟨_, classical.some $ Sup_aux c hc⟩ protected lemma le_Sup {c : set (E →ₗ.[R] F)} (hc : directed_on (≤) c) {f : E →ₗ.[R] F} (hf : f ∈ c) : f ≤ linear_pmap.Sup c hc := classical.some_spec (Sup_aux c hc) hf protected lemma Sup_le {c : set (E →ₗ.[R] F)} (hc : directed_on (≤) c) {g : E →ₗ.[R] F} (hg : ∀ f ∈ c, f ≤ g) : linear_pmap.Sup c hc ≤ g := le_of_eq_locus_ge $ Sup_le $ λ _ ⟨f, hf, eq⟩, eq ▸ have f ≤ (linear_pmap.Sup c hc) ⊓ g, from le_inf (linear_pmap.le_Sup _ hf) (hg f hf), this.1 protected lemma Sup_apply {c : set (E →ₗ.[R] F)} (hc : directed_on (≤) c) {l : E →ₗ.[R] F} (hl : l ∈ c) (x : l.domain) : (linear_pmap.Sup c hc) ⟨x, (linear_pmap.le_Sup hc hl).1 x.2⟩ = l x := begin symmetry, apply (classical.some_spec (Sup_aux c hc) hl).2, refl, end end linear_pmap namespace linear_map /-- Restrict a linear map to a submodule, reinterpreting the result as a `linear_pmap`. -/ def to_pmap (f : E →ₗ[R] F) (p : submodule R E) : E →ₗ.[R] F := ⟨p, f.comp p.subtype⟩ @[simp] lemma to_pmap_apply (f : E →ₗ[R] F) (p : submodule R E) (x : p) : f.to_pmap p x = f x := rfl /-- Compose a linear map with a `linear_pmap` -/ def comp_pmap (g : F →ₗ[R] G) (f : E →ₗ.[R] F) : E →ₗ.[R] G := { domain := f.domain, to_fun := g.comp f.to_fun } @[simp] lemma comp_pmap_apply (g : F →ₗ[R] G) (f : E →ₗ.[R] F) (x) : g.comp_pmap f x = g (f x) := rfl end linear_map namespace linear_pmap /-- Restrict codomain of a `linear_pmap` -/ def cod_restrict (f : E →ₗ.[R] F) (p : submodule R F) (H : ∀ x, f x ∈ p) : E →ₗ.[R] p := { domain := f.domain, to_fun := f.to_fun.cod_restrict p H } /-- Compose two `linear_pmap`s -/ def comp (g : F →ₗ.[R] G) (f : E →ₗ.[R] F) (H : ∀ x : f.domain, f x ∈ g.domain) : E →ₗ.[R] G := g.to_fun.comp_pmap $ f.cod_restrict _ H /-- `f.coprod g` is the partially defined linear map defined on `f.domain × g.domain`, and sending `p` to `f p.1 + g p.2`. -/ def coprod (f : E →ₗ.[R] G) (g : F →ₗ.[R] G) : (E × F) →ₗ.[R] G := { domain := f.domain.prod g.domain, to_fun := (f.comp (linear_pmap.fst f.domain g.domain) (λ x, x.2.1)).to_fun + (g.comp (linear_pmap.snd f.domain g.domain) (λ x, x.2.2)).to_fun } @[simp] lemma coprod_apply (f : E →ₗ.[R] G) (g : F →ₗ.[R] G) (x) : f.coprod g x = f ⟨(x : E × F).1, x.2.1⟩ + g ⟨(x : E × F).2, x.2.2⟩ := rfl /-- Restrict a partially defined linear map to a submodule of `E` contained in `f.domain`. -/ def dom_restrict (f : E →ₗ.[R] F) (S : submodule R E) : E →ₗ.[R] F := ⟨S ⊓ f.domain, f.to_fun.comp (submodule.of_le (by simp))⟩ @[simp] lemma dom_restrict_domain (f : E →ₗ.[R] F) {S : submodule R E} : (f.dom_restrict S).domain = S ⊓ f.domain := rfl lemma dom_restrict_apply {f : E →ₗ.[R] F} {S : submodule R E} ⦃x : S ⊓ f.domain⦄ ⦃y : f.domain⦄ (h : (x : E) = y) : f.dom_restrict S x = f y := begin have : submodule.of_le (by simp) x = y := by { ext, simp[h] }, rw ←this, exact linear_pmap.mk_apply _ _ _, end lemma dom_restrict_le {f : E →ₗ.[R] F} {S : submodule R E} : f.dom_restrict S ≤ f := ⟨by simp, λ x y hxy, dom_restrict_apply hxy⟩ /-! ### Graph -/ section graph /-- The graph of a `linear_pmap` viewed as a submodule on `E × F`. -/ def graph (f : E →ₗ.[R] F) : submodule R (E × F) := f.to_fun.graph.map (f.domain.subtype.prod_map (linear_map.id : F →ₗ[R] F)) lemma mem_graph_iff' (f : E →ₗ.[R] F) {x : E × F} : x ∈ f.graph ↔ ∃ y : f.domain, (↑y, f y) = x := by simp [graph] @[simp] lemma mem_graph_iff (f : E →ₗ.[R] F) {x : E × F} : x ∈ f.graph ↔ ∃ y : f.domain, (↑y : E) = x.1 ∧ f y = x.2 := by { cases x, simp_rw [mem_graph_iff', prod.mk.inj_iff] } /-- The tuple `(x, f x)` is contained in the graph of `f`. -/ lemma mem_graph (f : E →ₗ.[R] F) (x : domain f) : ((x : E), f x) ∈ f.graph := by simp variables {M : Type*} [monoid M] [distrib_mul_action M F] [smul_comm_class R M F] (y : M) /-- The graph of `z • f` as a pushforward. -/ lemma smul_graph (f : E →ₗ.[R] F) (z : M) : (z • f).graph = f.graph.map ((linear_map.id : E →ₗ[R] E).prod_map (z • (linear_map.id : F →ₗ[R] F))) := begin ext x, cases x, split; intros h, { rw mem_graph_iff at h, rcases h with ⟨y, hy, h⟩, rw linear_pmap.smul_apply at h, rw submodule.mem_map, simp only [mem_graph_iff, linear_map.prod_map_apply, linear_map.id_coe, id.def, linear_map.smul_apply, prod.mk.inj_iff, prod.exists, exists_exists_and_eq_and], use [x_fst, y], simp [hy, h] }, rw submodule.mem_map at h, rcases h with ⟨x', hx', h⟩, cases x', simp only [linear_map.prod_map_apply, linear_map.id_coe, id.def, linear_map.smul_apply, prod.mk.inj_iff] at h, rw mem_graph_iff at hx' ⊢, rcases hx' with ⟨y, hy, hx'⟩, use y, rw [←h.1, ←h.2], simp[hy, hx'], end /-- The graph of `-f` as a pushforward. -/ lemma neg_graph (f : E →ₗ.[R] F) : (-f).graph = f.graph.map ((linear_map.id : E →ₗ[R] E).prod_map (-(linear_map.id : F →ₗ[R] F))) := begin ext, cases x, split; intros h, { rw mem_graph_iff at h, rcases h with ⟨y, hy, h⟩, rw linear_pmap.neg_apply at h, rw submodule.mem_map, simp only [mem_graph_iff, linear_map.prod_map_apply, linear_map.id_coe, id.def, linear_map.neg_apply, prod.mk.inj_iff, prod.exists, exists_exists_and_eq_and], use [x_fst, y], simp [hy, h] }, rw submodule.mem_map at h, rcases h with ⟨x', hx', h⟩, cases x', simp only [linear_map.prod_map_apply, linear_map.id_coe, id.def, linear_map.neg_apply, prod.mk.inj_iff] at h, rw mem_graph_iff at hx' ⊢, rcases hx' with ⟨y, hy, hx'⟩, use y, rw [←h.1, ←h.2], simp [hy, hx'], end lemma mem_graph_snd_inj (f : E →ₗ.[R] F) {x y : E} {x' y' : F} (hx : (x,x') ∈ f.graph) (hy : (y,y') ∈ f.graph) (hxy : x = y) : x' = y' := begin rw [mem_graph_iff] at hx hy, rcases hx with ⟨x'', hx1, hx2⟩, rcases hy with ⟨y'', hy1, hy2⟩, simp only at hx1 hx2 hy1 hy2, rw [←hx1, ←hy1, set_like.coe_eq_coe] at hxy, rw [←hx2, ←hy2, hxy], end lemma mem_graph_snd_inj' (f : E →ₗ.[R] F) {x y : E × F} (hx : x ∈ f.graph) (hy : y ∈ f.graph) (hxy : x.1 = y.1) : x.2 = y.2 := by { cases x, cases y, exact f.mem_graph_snd_inj hx hy hxy } /-- The property that `f 0 = 0` in terms of the graph. -/ lemma graph_fst_eq_zero_snd (f : E →ₗ.[R] F) {x : E} {x' : F} (h : (x,x') ∈ f.graph) (hx : x = 0) : x' = 0 := f.mem_graph_snd_inj h f.graph.zero_mem hx lemma mem_domain_iff {f : E →ₗ.[R] F} {x : E} : x ∈ f.domain ↔ ∃ y : F, (x,y) ∈ f.graph := begin split; intro h, { use f ⟨x, h⟩, exact f.mem_graph ⟨x, h⟩ }, cases h with y h, rw mem_graph_iff at h, cases h with x' h, simp only at h, rw ←h.1, simp, end lemma mem_domain_of_mem_graph {f : E →ₗ.[R] F} {x : E} {y : F} (h : (x,y) ∈ f.graph) : x ∈ f.domain := by { rw mem_domain_iff, exact ⟨y, h⟩ } lemma image_iff {f : E →ₗ.[R] F} {x : E} {y : F} (hx : x ∈ f.domain) : y = f ⟨x, hx⟩ ↔ (x, y) ∈ f.graph := begin rw mem_graph_iff, split; intro h, { use ⟨x, hx⟩, simp [h] }, rcases h with ⟨⟨x', hx'⟩, ⟨h1, h2⟩⟩, simp only [submodule.coe_mk] at h1 h2, simp only [←h2, h1], end lemma mem_range_iff {f : E →ₗ.[R] F} {y : F} : y ∈ set.range f ↔ ∃ x : E, (x,y) ∈ f.graph := begin split; intro h, { rw set.mem_range at h, rcases h with ⟨⟨x, hx⟩, h⟩, use x, rw ←h, exact f.mem_graph ⟨x, hx⟩ }, cases h with x h, rw mem_graph_iff at h, cases h with x h, rw set.mem_range, use x, simp only at h, rw h.2, end lemma mem_domain_iff_of_eq_graph {f g : E →ₗ.[R] F} (h : f.graph = g.graph) {x : E} : x ∈ f.domain ↔ x ∈ g.domain := by simp_rw [mem_domain_iff, h] lemma le_of_le_graph {f g : E →ₗ.[R] F} (h : f.graph ≤ g.graph) : f ≤ g := begin split, { intros x hx, rw mem_domain_iff at hx ⊢, cases hx with y hx, use y, exact h hx }, rintros ⟨x, hx⟩ ⟨y, hy⟩ hxy, rw image_iff, refine h _, simp only [submodule.coe_mk] at hxy, rw hxy at hx, rw ←image_iff hx, simp [hxy], end lemma le_graph_of_le {f g : E →ₗ.[R] F} (h : f ≤ g) : f.graph ≤ g.graph := begin intros x hx, rw mem_graph_iff at hx ⊢, cases hx with y hx, use y, { exact h.1 y.2 }, simp only [hx, submodule.coe_mk, eq_self_iff_true, true_and], convert hx.2, refine (h.2 _).symm, simp only [hx.1, submodule.coe_mk], end lemma le_graph_iff {f g : E →ₗ.[R] F} : f.graph ≤ g.graph ↔ f ≤ g := ⟨le_of_le_graph, le_graph_of_le⟩ lemma eq_of_eq_graph {f g : E →ₗ.[R] F} (h : f.graph = g.graph) : f = g := by {ext, exact mem_domain_iff_of_eq_graph h, exact (le_of_le_graph h.le).2 } end graph end linear_pmap namespace submodule section submodule_to_linear_pmap lemma exists_unique_from_graph {g : submodule R (E × F)} (hg : ∀ {x : E × F} (hx : x ∈ g) (hx' : x.fst = 0), x.snd = 0) {a : E} (ha : a ∈ g.map (linear_map.fst R E F)) : ∃! (b : F), (a,b) ∈ g := begin refine exists_unique_of_exists_of_unique _ _, { convert ha, simp }, intros y₁ y₂ hy₁ hy₂, have hy : ((0 : E), y₁ - y₂) ∈ g := begin convert g.sub_mem hy₁ hy₂, exact (sub_self _).symm, end, exact sub_eq_zero.mp (hg hy (by simp)), end /-- Auxiliary definition to unfold the existential quantifier. -/ noncomputable def val_from_graph {g : submodule R (E × F)} (hg : ∀ (x : E × F) (hx : x ∈ g) (hx' : x.fst = 0), x.snd = 0) {a : E} (ha : a ∈ g.map (linear_map.fst R E F)) : F := (exists_of_exists_unique (exists_unique_from_graph hg ha)).some lemma val_from_graph_mem {g : submodule R (E × F)} (hg : ∀ (x : E × F) (hx : x ∈ g) (hx' : x.fst = 0), x.snd = 0) {a : E} (ha : a ∈ g.map (linear_map.fst R E F)) : (a, val_from_graph hg ha) ∈ g := (exists_of_exists_unique (exists_unique_from_graph hg ha)).some_spec /-- Define a `linear_pmap` from its graph. -/ noncomputable def to_linear_pmap (g : submodule R (E × F)) (hg : ∀ (x : E × F) (hx : x ∈ g) (hx' : x.fst = 0), x.snd = 0) : E →ₗ.[R] F := { domain := g.map (linear_map.fst R E F), to_fun := { to_fun := λ x, val_from_graph hg x.2, map_add' := λ v w, begin have hadd := (g.map (linear_map.fst R E F)).add_mem v.2 w.2, have hvw := val_from_graph_mem hg hadd, have hvw' := g.add_mem (val_from_graph_mem hg v.2) (val_from_graph_mem hg w.2), rw [prod.mk_add_mk] at hvw', exact (exists_unique_from_graph hg hadd).unique hvw hvw', end, map_smul' := λ a v, begin have hsmul := (g.map (linear_map.fst R E F)).smul_mem a v.2, have hav := val_from_graph_mem hg hsmul, have hav' := g.smul_mem a (val_from_graph_mem hg v.2), rw [prod.smul_mk] at hav', exact (exists_unique_from_graph hg hsmul).unique hav hav', end } } lemma mem_graph_to_linear_pmap (g : submodule R (E × F)) (hg : ∀ (x : E × F) (hx : x ∈ g) (hx' : x.fst = 0), x.snd = 0) (x : g.map (linear_map.fst R E F)) : (x.val, g.to_linear_pmap hg x) ∈ g := val_from_graph_mem hg x.2 @[simp] lemma to_linear_pmap_graph_eq (g : submodule R (E × F)) (hg : ∀ (x : E × F) (hx : x ∈ g) (hx' : x.fst = 0), x.snd = 0) : (g.to_linear_pmap hg).graph = g := begin ext, split; intro hx, { rw [linear_pmap.mem_graph_iff] at hx, rcases hx with ⟨y,hx1,hx2⟩, convert g.mem_graph_to_linear_pmap hg y, rw [subtype.val_eq_coe], exact prod.ext hx1.symm hx2.symm }, rw linear_pmap.mem_graph_iff, cases x, have hx_fst : x_fst ∈ g.map (linear_map.fst R E F) := begin simp only [mem_map, linear_map.fst_apply, prod.exists, exists_and_distrib_right, exists_eq_right], exact ⟨x_snd, hx⟩, end, refine ⟨⟨x_fst, hx_fst⟩, subtype.coe_mk x_fst hx_fst, _⟩, exact (exists_unique_from_graph hg hx_fst).unique (val_from_graph_mem hg hx_fst) hx, end end submodule_to_linear_pmap end submodule
339da99f62a435d8099f271efa9a337d07cc0fcb
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/algebra/lie/nilpotent.lean
6d4b290ac18164dfede5226c91f3088c8b0d2fcd
[ "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
27,844
lean
/- Copyright (c) 2021 Oliver Nash. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Oliver Nash -/ import algebra.lie.solvable import algebra.lie.quotient import algebra.lie.normalizer import linear_algebra.eigenspace.basic import ring_theory.nilpotent /-! # Nilpotent Lie algebras > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. Like groups, Lie algebras admit a natural concept of nilpotency. More generally, any Lie module carries a natural concept of nilpotency. We define these here via the lower central series. ## Main definitions * `lie_module.lower_central_series` * `lie_module.is_nilpotent` ## Tags lie algebra, lower central series, nilpotent -/ universes u v w w₁ w₂ section nilpotent_modules variables {R : Type u} {L : Type v} {M : Type w} variables [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] variables [lie_ring_module L M] [lie_module R L M] variables (k : ℕ) (N : lie_submodule R L M) namespace lie_submodule /-- A generalisation of the lower central series. The zeroth term is a specified Lie submodule of a Lie module. In the case when we specify the top ideal `⊤` of the Lie algebra, regarded as a Lie module over itself, we get the usual lower central series of a Lie algebra. It can be more convenient to work with this generalisation when considering the lower central series of a Lie submodule, regarded as a Lie module in its own right, since it provides a type-theoretic expression of the fact that the terms of the Lie submodule's lower central series are also Lie submodules of the enclosing Lie module. See also `lie_module.lower_central_series_eq_lcs_comap` and `lie_module.lower_central_series_map_eq_lcs` below, as well as `lie_submodule.ucs`. -/ def lcs : lie_submodule R L M → lie_submodule R L M := (λ N, ⁅(⊤ : lie_ideal R L), N⁆)^[k] @[simp] lemma lcs_zero (N : lie_submodule R L M) : N.lcs 0 = N := rfl @[simp] lemma lcs_succ : N.lcs (k + 1) = ⁅(⊤ : lie_ideal R L), N.lcs k⁆ := function.iterate_succ_apply' (λ N', ⁅⊤, N'⁆) k N end lie_submodule namespace lie_module variables (R L M) /-- The lower central series of Lie submodules of a Lie module. -/ def lower_central_series : lie_submodule R L M := (⊤ : lie_submodule R L M).lcs k @[simp] lemma lower_central_series_zero : lower_central_series R L M 0 = ⊤ := rfl @[simp] lemma lower_central_series_succ : lower_central_series R L M (k + 1) = ⁅(⊤ : lie_ideal R L), lower_central_series R L M k⁆ := (⊤ : lie_submodule R L M).lcs_succ k end lie_module namespace lie_submodule open lie_module variables {R L M} lemma lcs_le_self : N.lcs k ≤ N := begin induction k with k ih, { simp, }, { simp only [lcs_succ], exact (lie_submodule.mono_lie_right _ _ ⊤ ih).trans (N.lie_le_right ⊤), }, end lemma lower_central_series_eq_lcs_comap : lower_central_series R L N k = (N.lcs k).comap N.incl := begin induction k with k ih, { simp, }, { simp only [lcs_succ, lower_central_series_succ] at ⊢ ih, have : N.lcs k ≤ N.incl.range, { rw N.range_incl, apply lcs_le_self, }, rw [ih, lie_submodule.comap_bracket_eq _ _ N.incl N.ker_incl this], }, end lemma lower_central_series_map_eq_lcs : (lower_central_series R L N k).map N.incl = N.lcs k := begin rw [lower_central_series_eq_lcs_comap, lie_submodule.map_comap_incl, inf_eq_right], apply lcs_le_self, end end lie_submodule namespace lie_module variables (R L M) lemma antitone_lower_central_series : antitone $ lower_central_series R L M := begin intros l k, induction k with k ih generalizing l; intros h, { exact (le_zero_iff.mp h).symm ▸ le_rfl, }, { rcases nat.of_le_succ h with hk | hk, { rw lower_central_series_succ, exact (lie_submodule.mono_lie_right _ _ ⊤ (ih hk)).trans (lie_submodule.lie_le_right _ _), }, { exact hk.symm ▸ le_rfl, }, }, end lemma trivial_iff_lower_central_eq_bot : is_trivial L M ↔ lower_central_series R L M 1 = ⊥ := begin split; intros h, { erw [eq_bot_iff, lie_submodule.lie_span_le], rintros m ⟨x, n, hn⟩, rw [← hn, h.trivial], simp,}, { rw lie_submodule.eq_bot_iff at h, apply is_trivial.mk, intros x m, apply h, apply lie_submodule.subset_lie_span, use [x, m], refl, }, end lemma iterate_to_endomorphism_mem_lower_central_series (x : L) (m : M) (k : ℕ) : (to_endomorphism R L M x)^[k] m ∈ lower_central_series R L M k := begin induction k with k ih, { simp only [function.iterate_zero], }, { simp only [lower_central_series_succ, function.comp_app, function.iterate_succ', to_endomorphism_apply_apply], exact lie_submodule.lie_mem_lie _ _ (lie_submodule.mem_top x) ih, }, end variables {R L M} lemma map_lower_central_series_le {M₂ : Type w₁} [add_comm_group M₂] [module R M₂] [lie_ring_module L M₂] [lie_module R L M₂] (k : ℕ) (f : M →ₗ⁅R,L⁆ M₂) : lie_submodule.map f (lower_central_series R L M k) ≤ lower_central_series R L M₂ k := begin induction k with k ih, { simp only [lie_module.lower_central_series_zero, le_top], }, { simp only [lie_module.lower_central_series_succ, lie_submodule.map_bracket_eq], exact lie_submodule.mono_lie_right _ _ ⊤ ih, }, end variables (R L M) open lie_algebra lemma derived_series_le_lower_central_series (k : ℕ) : derived_series R L k ≤ lower_central_series R L L k := begin induction k with k h, { rw [derived_series_def, derived_series_of_ideal_zero, lower_central_series_zero], exact le_rfl, }, { have h' : derived_series R L k ≤ ⊤, { simp only [le_top], }, rw [derived_series_def, derived_series_of_ideal_succ, lower_central_series_succ], exact lie_submodule.mono_lie _ _ _ _ h' h, }, end /-- A Lie module is nilpotent if its lower central series reaches 0 (in a finite number of steps). -/ class is_nilpotent : Prop := (nilpotent : ∃ k, lower_central_series R L M k = ⊥) /-- See also `lie_module.is_nilpotent_iff_exists_ucs_eq_top`. -/ lemma is_nilpotent_iff : is_nilpotent R L M ↔ ∃ k, lower_central_series R L M k = ⊥ := ⟨λ h, h.nilpotent, λ h, ⟨h⟩⟩ variables {R L M} lemma _root_.lie_submodule.is_nilpotent_iff_exists_lcs_eq_bot (N : lie_submodule R L M) : lie_module.is_nilpotent R L N ↔ ∃ k, N.lcs k = ⊥ := begin rw is_nilpotent_iff, refine exists_congr (λ k, _), rw [N.lower_central_series_eq_lcs_comap k, lie_submodule.comap_incl_eq_bot, inf_eq_right.mpr (N.lcs_le_self k)], end variables (R L M) @[priority 100] instance trivial_is_nilpotent [is_trivial L M] : is_nilpotent R L M := ⟨by { use 1, change ⁅⊤, ⊤⁆ = ⊥, simp, }⟩ lemma nilpotent_endo_of_nilpotent_module [hM : is_nilpotent R L M] : ∃ (k : ℕ), ∀ (x : L), (to_endomorphism R L M x)^k = 0 := begin unfreezingI { obtain ⟨k, hM⟩ := hM, }, use k, intros x, ext m, rw [linear_map.pow_apply, linear_map.zero_apply, ← @lie_submodule.mem_bot R L M, ← hM], exact iterate_to_endomorphism_mem_lower_central_series R L M x m k, end /-- For a nilpotent Lie module, the weight space of the 0 weight is the whole module. This result will be used downstream to show that weight spaces are Lie submodules, at which time it will be possible to state it in the language of weight spaces. -/ lemma infi_max_gen_zero_eigenspace_eq_top_of_nilpotent [is_nilpotent R L M] : (⨅ (x : L), (to_endomorphism R L M x).maximal_generalized_eigenspace 0) = ⊤ := begin ext m, simp only [module.End.mem_maximal_generalized_eigenspace, submodule.mem_top, sub_zero, iff_true, zero_smul, submodule.mem_infi], intros x, obtain ⟨k, hk⟩ := nilpotent_endo_of_nilpotent_module R L M, use k, rw hk, exact linear_map.zero_apply m, end /-- If the quotient of a Lie module `M` by a Lie submodule on which the Lie algebra acts trivially is nilpotent then `M` is nilpotent. This is essentially the Lie module equivalent of the fact that a central extension of nilpotent Lie algebras is nilpotent. See `lie_algebra.nilpotent_of_nilpotent_quotient` below for the corresponding result for Lie algebras. -/ lemma nilpotent_of_nilpotent_quotient {N : lie_submodule R L M} (h₁ : N ≤ max_triv_submodule R L M) (h₂ : is_nilpotent R L (M ⧸ N)) : is_nilpotent R L M := begin unfreezingI { obtain ⟨k, hk⟩ := h₂, }, use k+1, simp only [lower_central_series_succ], suffices : lower_central_series R L M k ≤ N, { replace this := lie_submodule.mono_lie_right _ _ ⊤ (le_trans this h₁), rwa [ideal_oper_max_triv_submodule_eq_bot, le_bot_iff] at this, }, rw [← lie_submodule.quotient.map_mk'_eq_bot_le, ← le_bot_iff, ← hk], exact map_lower_central_series_le k (lie_submodule.quotient.mk' N), end /-- Given a nilpotent Lie module `M` with lower central series `M = C₀ ≥ C₁ ≥ ⋯ ≥ Cₖ = ⊥`, this is the natural number `k` (the number of inclusions). For a non-nilpotent module, we use the junk value 0. -/ noncomputable def nilpotency_length : ℕ := Inf { k | lower_central_series R L M k = ⊥ } lemma nilpotency_length_eq_zero_iff [is_nilpotent R L M] : nilpotency_length R L M = 0 ↔ subsingleton M := begin let s := { k | lower_central_series R L M k = ⊥ }, have hs : s.nonempty, { unfreezingI { obtain ⟨k, hk⟩ := (by apply_instance : is_nilpotent R L M), }, exact ⟨k, hk⟩, }, change Inf s = 0 ↔ _, rw [← lie_submodule.subsingleton_iff R L M, ← subsingleton_iff_bot_eq_top, ← lower_central_series_zero, @eq_comm (lie_submodule R L M)], refine ⟨λ h, h ▸ nat.Inf_mem hs, λ h, _⟩, rw nat.Inf_eq_zero, exact or.inl h, end lemma nilpotency_length_eq_succ_iff (k : ℕ) : nilpotency_length R L M = k + 1 ↔ lower_central_series R L M (k + 1) = ⊥ ∧ lower_central_series R L M k ≠ ⊥ := begin let s := { k | lower_central_series R L M k = ⊥ }, change Inf s = k + 1 ↔ k + 1 ∈ s ∧ k ∉ s, have hs : ∀ k₁ k₂, k₁ ≤ k₂ → k₁ ∈ s → k₂ ∈ s, { rintros k₁ k₂ h₁₂ (h₁ : lower_central_series R L M k₁ = ⊥), exact eq_bot_iff.mpr (h₁ ▸ antitone_lower_central_series R L M h₁₂), }, exact nat.Inf_upward_closed_eq_succ_iff hs k, end /-- Given a non-trivial nilpotent Lie module `M` with lower central series `M = C₀ ≥ C₁ ≥ ⋯ ≥ Cₖ = ⊥`, this is the `k-1`th term in the lower central series (the last non-trivial term). For a trivial or non-nilpotent module, this is the bottom submodule, `⊥`. -/ noncomputable def lower_central_series_last : lie_submodule R L M := match nilpotency_length R L M with | 0 := ⊥ | k + 1 := lower_central_series R L M k end lemma lower_central_series_last_le_max_triv : lower_central_series_last R L M ≤ max_triv_submodule R L M := begin rw lower_central_series_last, cases h : nilpotency_length R L M with k, { exact bot_le, }, { rw le_max_triv_iff_bracket_eq_bot, rw [nilpotency_length_eq_succ_iff, lower_central_series_succ] at h, exact h.1, }, end lemma nontrivial_lower_central_series_last [nontrivial M] [is_nilpotent R L M] : nontrivial (lower_central_series_last R L M) := begin rw [lie_submodule.nontrivial_iff_ne_bot, lower_central_series_last], cases h : nilpotency_length R L M, { rw [nilpotency_length_eq_zero_iff, ← not_nontrivial_iff_subsingleton] at h, contradiction, }, { rw nilpotency_length_eq_succ_iff at h, exact h.2, }, end lemma nontrivial_max_triv_of_is_nilpotent [nontrivial M] [is_nilpotent R L M] : nontrivial (max_triv_submodule R L M) := set.nontrivial_mono (lower_central_series_last_le_max_triv R L M) (nontrivial_lower_central_series_last R L M) @[simp] lemma coe_lcs_range_to_endomorphism_eq (k : ℕ) : (lower_central_series R (to_endomorphism R L M).range M k : submodule R M) = lower_central_series R L M k := begin induction k with k ih, { simp, }, { simp only [lower_central_series_succ, lie_submodule.lie_ideal_oper_eq_linear_span', ← (lower_central_series R (to_endomorphism R L M).range M k).mem_coe_submodule, ih], congr, ext m, split, { rintros ⟨⟨-, ⟨y, rfl⟩⟩, -, n, hn, rfl⟩, exact ⟨y, lie_submodule.mem_top _, n, hn, rfl⟩, }, { rintros ⟨x, hx, n, hn, rfl⟩, exact ⟨⟨to_endomorphism R L M x, lie_hom.mem_range_self _ x⟩, lie_submodule.mem_top _, n, hn, rfl⟩, }, }, end @[simp] lemma is_nilpotent_range_to_endomorphism_iff : is_nilpotent R (to_endomorphism R L M).range M ↔ is_nilpotent R L M := begin split; rintros ⟨k, hk⟩; use k; rw ← lie_submodule.coe_to_submodule_eq_iff at ⊢ hk; simpa using hk, end end lie_module namespace lie_submodule variables {N₁ N₂ : lie_submodule R L M} /-- The upper (aka ascending) central series. See also `lie_submodule.lcs`. -/ def ucs (k : ℕ) : lie_submodule R L M → lie_submodule R L M := normalizer^[k] @[simp] lemma ucs_zero : N.ucs 0 = N := rfl @[simp] lemma ucs_succ (k : ℕ) : N.ucs (k + 1) = (N.ucs k).normalizer := function.iterate_succ_apply' normalizer k N lemma ucs_add (k l : ℕ) : N.ucs (k + l) = (N.ucs l).ucs k := function.iterate_add_apply normalizer k l N @[mono] lemma ucs_mono (k : ℕ) (h : N₁ ≤ N₂) : N₁.ucs k ≤ N₂.ucs k := begin induction k with k ih, { simpa, }, simp only [ucs_succ], mono, end lemma ucs_eq_self_of_normalizer_eq_self (h : N₁.normalizer = N₁) (k : ℕ) : N₁.ucs k = N₁ := by { induction k with k ih, { simp, }, { rwa [ucs_succ, ih], }, } /-- If a Lie module `M` contains a self-normalizing Lie submodule `N`, then all terms of the upper central series of `M` are contained in `N`. An important instance of this situation arises from a Cartan subalgebra `H ⊆ L` with the roles of `L`, `M`, `N` played by `H`, `L`, `H`, respectively. -/ lemma ucs_le_of_normalizer_eq_self (h : N₁.normalizer = N₁) (k : ℕ) : (⊥ : lie_submodule R L M).ucs k ≤ N₁ := by { rw ← ucs_eq_self_of_normalizer_eq_self h k, mono, simp, } lemma lcs_add_le_iff (l k : ℕ) : N₁.lcs (l + k) ≤ N₂ ↔ N₁.lcs l ≤ N₂.ucs k := begin revert l, induction k with k ih, { simp, }, intros l, rw [(by abel : l + (k + 1) = l + 1 + k), ih, ucs_succ, lcs_succ, top_lie_le_iff_le_normalizer], end lemma lcs_le_iff (k : ℕ) : N₁.lcs k ≤ N₂ ↔ N₁ ≤ N₂.ucs k := by { convert lcs_add_le_iff 0 k, rw zero_add, } lemma gc_lcs_ucs (k : ℕ): galois_connection (λ (N : lie_submodule R L M), N.lcs k) (λ (N : lie_submodule R L M), N.ucs k) := λ N₁ N₂, lcs_le_iff k lemma ucs_eq_top_iff (k : ℕ) : N.ucs k = ⊤ ↔ lie_module.lower_central_series R L M k ≤ N := by { rw [eq_top_iff, ← lcs_le_iff], refl, } lemma _root_.lie_module.is_nilpotent_iff_exists_ucs_eq_top : lie_module.is_nilpotent R L M ↔ ∃ k, (⊥ : lie_submodule R L M).ucs k = ⊤ := by { rw lie_module.is_nilpotent_iff, exact exists_congr (λ k, by simp [ucs_eq_top_iff]), } lemma ucs_comap_incl (k : ℕ) : ((⊥ : lie_submodule R L M).ucs k).comap N.incl = (⊥ : lie_submodule R L N).ucs k := by { induction k with k ih, { exact N.ker_incl, }, { simp [← ih], }, } lemma is_nilpotent_iff_exists_self_le_ucs : lie_module.is_nilpotent R L N ↔ ∃ k, N ≤ (⊥ : lie_submodule R L M).ucs k := by simp_rw [lie_module.is_nilpotent_iff_exists_ucs_eq_top, ← ucs_comap_incl, comap_incl_eq_top] end lie_submodule section morphisms open lie_module function variables {L₂ M₂ : Type*} [lie_ring L₂] [lie_algebra R L₂] variables [add_comm_group M₂] [module R M₂] [lie_ring_module L₂ M₂] [lie_module R L₂ M₂] variables {f : L →ₗ⁅R⁆ L₂} {g : M →ₗ[R] M₂} variables (hf : surjective f) (hg : surjective g) (hfg : ∀ x m, ⁅f x, g m⁆ = g ⁅x, m⁆) include hf hg hfg lemma function.surjective.lie_module_lcs_map_eq (k : ℕ) : (lower_central_series R L M k : submodule R M).map g = lower_central_series R L₂ M₂ k := begin induction k with k ih, { simp [linear_map.range_eq_top, hg], }, { suffices : g '' {m | ∃ (x : L) n, n ∈ lower_central_series R L M k ∧ ⁅x, n⁆ = m} = {m | ∃ (x : L₂) n, n ∈ lower_central_series R L M k ∧ ⁅x, g n⁆ = m}, { simp only [← lie_submodule.mem_coe_submodule] at this, simp [← lie_submodule.mem_coe_submodule, ← ih, lie_submodule.lie_ideal_oper_eq_linear_span', submodule.map_span, -submodule.span_image, this], }, ext m₂, split, { rintros ⟨m, ⟨x, n, hn, rfl⟩, rfl⟩, exact ⟨f x, n, hn, hfg x n⟩, }, { rintros ⟨x, n, hn, rfl⟩, obtain ⟨y, rfl⟩ := hf x, exact ⟨⁅y, n⁆, ⟨y, n, hn, rfl⟩, (hfg y n).symm⟩, }, }, end lemma function.surjective.lie_module_is_nilpotent [is_nilpotent R L M] : is_nilpotent R L₂ M₂ := begin obtain ⟨k, hk⟩ := id (by apply_instance : is_nilpotent R L M), use k, rw ← lie_submodule.coe_to_submodule_eq_iff at ⊢ hk, simp [← hf.lie_module_lcs_map_eq hg hfg k, hk], end omit hf hg hfg lemma equiv.lie_module_is_nilpotent_iff (f : L ≃ₗ⁅R⁆ L₂) (g : M ≃ₗ[R] M₂) (hfg : ∀ x m, ⁅f x, g m⁆ = g ⁅x, m⁆) : is_nilpotent R L M ↔ is_nilpotent R L₂ M₂ := begin split; introsI h, { have hg : surjective (g : M →ₗ[R] M₂) := g.surjective, exact f.surjective.lie_module_is_nilpotent hg hfg, }, { have hg : surjective (g.symm : M₂ →ₗ[R] M) := g.symm.surjective, refine f.symm.surjective.lie_module_is_nilpotent hg (λ x m, _), rw [linear_equiv.coe_coe, lie_equiv.coe_to_lie_hom, ← g.symm_apply_apply ⁅f.symm x, g.symm m⁆, ← hfg, f.apply_symm_apply, g.apply_symm_apply], }, end @[simp] lemma lie_module.is_nilpotent_of_top_iff : is_nilpotent R (⊤ : lie_subalgebra R L) M ↔ is_nilpotent R L M := equiv.lie_module_is_nilpotent_iff lie_subalgebra.top_equiv (1 : M ≃ₗ[R] M) (λ x m, rfl) end morphisms end nilpotent_modules @[priority 100] instance lie_algebra.is_solvable_of_is_nilpotent (R : Type u) (L : Type v) [comm_ring R] [lie_ring L] [lie_algebra R L] [hL : lie_module.is_nilpotent R L L] : lie_algebra.is_solvable R L := begin obtain ⟨k, h⟩ : ∃ k, lie_module.lower_central_series R L L k = ⊥ := hL.nilpotent, use k, rw ← le_bot_iff at h ⊢, exact le_trans (lie_module.derived_series_le_lower_central_series R L k) h, end section nilpotent_algebras variables (R : Type u) (L : Type v) (L' : Type w) variables [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L'] /-- We say a Lie algebra is nilpotent when it is nilpotent as a Lie module over itself via the adjoint representation. -/ abbreviation lie_algebra.is_nilpotent (R : Type u) (L : Type v) [comm_ring R] [lie_ring L] [lie_algebra R L] : Prop := lie_module.is_nilpotent R L L open lie_algebra lemma lie_algebra.nilpotent_ad_of_nilpotent_algebra [is_nilpotent R L] : ∃ (k : ℕ), ∀ (x : L), (ad R L x)^k = 0 := lie_module.nilpotent_endo_of_nilpotent_module R L L /-- See also `lie_algebra.zero_root_space_eq_top_of_nilpotent`. -/ lemma lie_algebra.infi_max_gen_zero_eigenspace_eq_top_of_nilpotent [is_nilpotent R L] : (⨅ (x : L), (ad R L x).maximal_generalized_eigenspace 0) = ⊤ := lie_module.infi_max_gen_zero_eigenspace_eq_top_of_nilpotent R L L -- TODO Generalise the below to Lie modules if / when we define morphisms, equivs of Lie modules -- covering a Lie algebra morphism of (possibly different) Lie algebras. variables {R L L'} open lie_module (lower_central_series) /-- Given an ideal `I` of a Lie algebra `L`, the lower central series of `L ⧸ I` is the same whether we regard `L ⧸ I` as an `L` module or an `L ⧸ I` module. TODO: This result obviously generalises but the generalisation requires the missing definition of morphisms between Lie modules over different Lie algebras. -/ lemma coe_lower_central_series_ideal_quot_eq {I : lie_ideal R L} (k : ℕ) : (lower_central_series R L (L ⧸ I) k : submodule R (L ⧸ I)) = lower_central_series R (L ⧸ I) (L ⧸ I) k := begin induction k with k ih, { simp only [lie_submodule.top_coe_submodule, lie_module.lower_central_series_zero], }, { simp only [lie_module.lower_central_series_succ, lie_submodule.lie_ideal_oper_eq_linear_span], congr, ext x, split, { rintros ⟨⟨y, -⟩, ⟨z, hz⟩, rfl : ⁅y, z⁆ = x⟩, erw [← lie_submodule.mem_coe_submodule, ih, lie_submodule.mem_coe_submodule] at hz, exact ⟨⟨lie_submodule.quotient.mk y, lie_submodule.mem_top _⟩, ⟨z, hz⟩, rfl⟩, }, { rintros ⟨⟨⟨y⟩, -⟩, ⟨z, hz⟩, rfl : ⁅y, z⁆ = x⟩, erw [← lie_submodule.mem_coe_submodule, ← ih, lie_submodule.mem_coe_submodule] at hz, exact ⟨⟨y, lie_submodule.mem_top _⟩, ⟨z, hz⟩, rfl⟩, }, }, end /-- Note that the below inequality can be strict. For example the ideal of strictly-upper-triangular 2x2 matrices inside the Lie algebra of upper-triangular 2x2 matrices with `k = 1`. -/ lemma lie_module.coe_lower_central_series_ideal_le {I : lie_ideal R L} (k : ℕ) : (lower_central_series R I I k : submodule R I) ≤ lower_central_series R L I k := begin induction k with k ih, { simp, }, { simp only [lie_module.lower_central_series_succ, lie_submodule.lie_ideal_oper_eq_linear_span], apply submodule.span_mono, rintros x ⟨⟨y, -⟩, ⟨z, hz⟩, rfl : ⁅y, z⁆ = x⟩, exact ⟨⟨y.val, lie_submodule.mem_top _⟩, ⟨z, ih hz⟩, rfl⟩, }, end /-- A central extension of nilpotent Lie algebras is nilpotent. -/ lemma lie_algebra.nilpotent_of_nilpotent_quotient {I : lie_ideal R L} (h₁ : I ≤ center R L) (h₂ : is_nilpotent R (L ⧸ I)) : is_nilpotent R L := begin suffices : lie_module.is_nilpotent R L (L ⧸ I), { exact lie_module.nilpotent_of_nilpotent_quotient R L L h₁ this, }, unfreezingI { obtain ⟨k, hk⟩ := h₂, }, use k, simp [← lie_submodule.coe_to_submodule_eq_iff, coe_lower_central_series_ideal_quot_eq, hk], end lemma lie_algebra.non_trivial_center_of_is_nilpotent [nontrivial L] [is_nilpotent R L] : nontrivial $ center R L := lie_module.nontrivial_max_triv_of_is_nilpotent R L L lemma lie_ideal.map_lower_central_series_le (k : ℕ) {f : L →ₗ⁅R⁆ L'} : lie_ideal.map f (lower_central_series R L L k) ≤ lower_central_series R L' L' k := begin induction k with k ih, { simp only [lie_module.lower_central_series_zero, le_top], }, { simp only [lie_module.lower_central_series_succ], exact le_trans (lie_ideal.map_bracket_le f) (lie_submodule.mono_lie _ _ _ _ le_top ih), }, end lemma lie_ideal.lower_central_series_map_eq (k : ℕ) {f : L →ₗ⁅R⁆ L'} (h : function.surjective f) : lie_ideal.map f (lower_central_series R L L k) = lower_central_series R L' L' k := begin have h' : (⊤ : lie_ideal R L).map f = ⊤, { rw ←f.ideal_range_eq_map, exact f.ideal_range_eq_top_of_surjective h, }, induction k with k ih, { simp only [lie_module.lower_central_series_zero], exact h', }, { simp only [lie_module.lower_central_series_succ, lie_ideal.map_bracket_eq f h, ih, h'], }, end lemma function.injective.lie_algebra_is_nilpotent [h₁ : is_nilpotent R L'] {f : L →ₗ⁅R⁆ L'} (h₂ : function.injective f) : is_nilpotent R L := { nilpotent := begin obtain ⟨k, hk⟩ := id h₁, use k, apply lie_ideal.bot_of_map_eq_bot h₂, rw [eq_bot_iff, ← hk], apply lie_ideal.map_lower_central_series_le, end, } lemma function.surjective.lie_algebra_is_nilpotent [h₁ : is_nilpotent R L] {f : L →ₗ⁅R⁆ L'} (h₂ : function.surjective f) : is_nilpotent R L' := { nilpotent := begin obtain ⟨k, hk⟩ := id h₁, use k, rw [← lie_ideal.lower_central_series_map_eq k h₂, hk], simp only [lie_ideal.map_eq_bot_iff, bot_le], end, } lemma lie_equiv.nilpotent_iff_equiv_nilpotent (e : L ≃ₗ⁅R⁆ L') : is_nilpotent R L ↔ is_nilpotent R L' := begin split; introsI h, { exact e.symm.injective.lie_algebra_is_nilpotent, }, { exact e.injective.lie_algebra_is_nilpotent, }, end lemma lie_hom.is_nilpotent_range [is_nilpotent R L] (f : L →ₗ⁅R⁆ L') : is_nilpotent R f.range := f.surjective_range_restrict.lie_algebra_is_nilpotent /-- Note that this result is not quite a special case of `lie_module.is_nilpotent_range_to_endomorphism_iff` which concerns nilpotency of the `(ad R L).range`-module `L`, whereas this result concerns nilpotency of the `(ad R L).range`-module `(ad R L).range`. -/ @[simp] lemma lie_algebra.is_nilpotent_range_ad_iff : is_nilpotent R (ad R L).range ↔ is_nilpotent R L := begin refine ⟨λ h, _, _⟩, { have : (ad R L).ker = center R L, { simp, }, exact lie_algebra.nilpotent_of_nilpotent_quotient (le_of_eq this) ((ad R L).quot_ker_equiv_range.nilpotent_iff_equiv_nilpotent.mpr h), }, { introsI h, exact (ad R L).is_nilpotent_range, }, end instance [h : lie_algebra.is_nilpotent R L] : lie_algebra.is_nilpotent R (⊤ : lie_subalgebra R L) := lie_subalgebra.top_equiv.nilpotent_iff_equiv_nilpotent.mpr h end nilpotent_algebras namespace lie_ideal open lie_module variables {R L : Type*} [comm_ring R] [lie_ring L] [lie_algebra R L] (I : lie_ideal R L) variables (M : Type*) [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] variables (k : ℕ) /-- Given a Lie module `M` over a Lie algebra `L` together with an ideal `I` of `L`, this is the lower central series of `M` as an `I`-module. The advantage of using this definition instead of `lie_module.lower_central_series R I M` is that its terms are Lie submodules of `M` as an `L`-module, rather than just as an `I`-module. See also `lie_ideal.coe_lcs_eq`. -/ def lcs : lie_submodule R L M := (λ N, ⁅I, N⁆)^[k] ⊤ @[simp] lemma lcs_zero : I.lcs M 0 = ⊤ := rfl @[simp] lemma lcs_succ : I.lcs M (k + 1) = ⁅I, I.lcs M k⁆ := function.iterate_succ_apply' (λ N, ⁅I, N⁆) k ⊤ lemma lcs_top : (⊤ : lie_ideal R L).lcs M k = lower_central_series R L M k := rfl lemma coe_lcs_eq : (I.lcs M k : submodule R M) = lower_central_series R I M k := begin induction k with k ih, { simp, }, { simp_rw [lower_central_series_succ, lcs_succ, lie_submodule.lie_ideal_oper_eq_linear_span', ← (I.lcs M k).mem_coe_submodule, ih, lie_submodule.mem_coe_submodule, lie_submodule.mem_top, exists_true_left, (I : lie_subalgebra R L).coe_bracket_of_module], congr, ext m, split, { rintros ⟨x, hx, m, hm, rfl⟩, exact ⟨⟨x, hx⟩, m, hm, rfl⟩, }, { rintros ⟨⟨x, hx⟩, m, hm, rfl⟩, exact ⟨x, hx, m, hm, rfl⟩, }, }, end end lie_ideal section of_associative variables (R : Type u) {A : Type v} [comm_ring R] [ring A] [algebra R A] lemma lie_algebra.ad_nilpotent_of_nilpotent {a : A} (h : is_nilpotent a) : is_nilpotent (lie_algebra.ad R A a) := begin rw lie_algebra.ad_eq_lmul_left_sub_lmul_right, have hl : is_nilpotent (linear_map.mul_left R a), { rwa linear_map.is_nilpotent_mul_left_iff, }, have hr : is_nilpotent (linear_map.mul_right R a), { rwa linear_map.is_nilpotent_mul_right_iff, }, have := @linear_map.commute_mul_left_right R A _ _ _ _ _ a a, exact this.is_nilpotent_sub hl hr, end variables {R} lemma lie_subalgebra.is_nilpotent_ad_of_is_nilpotent_ad {L : Type v} [lie_ring L] [lie_algebra R L] (K : lie_subalgebra R L) {x : K} (h : is_nilpotent (lie_algebra.ad R L ↑x)) : is_nilpotent (lie_algebra.ad R K x) := begin obtain ⟨n, hn⟩ := h, use n, exact linear_map.submodule_pow_eq_zero_of_pow_eq_zero (K.ad_comp_incl_eq x) hn, end lemma lie_algebra.is_nilpotent_ad_of_is_nilpotent {L : lie_subalgebra R A} {x : L} (h : is_nilpotent (x : A)) : is_nilpotent (lie_algebra.ad R L x) := L.is_nilpotent_ad_of_is_nilpotent_ad $ lie_algebra.ad_nilpotent_of_nilpotent R h end of_associative
ac01680b53b3d8e10bab76734168ddc2d9f6e767
4bcaca5dc83d49803f72b7b5920b75b6e7d9de2d
/stage0/src/Lean/Elab/DefView.lean
91d7de14d3afe902cef4a10c1edd3d02341e76b7
[ "Apache-2.0" ]
permissive
subfish-zhou/leanprover-zh_CN.github.io
30b9fba9bd790720bd95764e61ae796697d2f603
8b2985d4a3d458ceda9361ac454c28168d920d3f
refs/heads/master
1,689,709,967,820
1,632,503,056,000
1,632,503,056,000
409,962,097
1
0
null
null
null
null
UTF-8
Lean
false
false
7,161
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 Std.ShareCommon import Lean.Parser.Command import Lean.Util.CollectLevelParams import Lean.Util.FoldConsts import Lean.Meta.CollectFVars import Lean.Elab.Command import Lean.Elab.SyntheticMVars import Lean.Elab.Binders import Lean.Elab.DeclUtil namespace Lean.Elab inductive DefKind where | «def» | «theorem» | «example» | «opaque» | «abbrev» deriving Inhabited, BEq def DefKind.isTheorem : DefKind → Bool | «theorem» => true | _ => false def DefKind.isDefOrAbbrevOrOpaque : DefKind → Bool | «def» => true | «opaque» => true | «abbrev» => true | _ => false def DefKind.isExample : DefKind → Bool | «example» => true | _ => false structure DefView where kind : DefKind ref : Syntax modifiers : Modifiers declId : Syntax binders : Syntax type? : Option Syntax value : Syntax deriving? : Option (Array Syntax) := none deriving Inhabited namespace Command open Meta def mkDefViewOfAbbrev (modifiers : Modifiers) (stx : Syntax) : DefView := -- leading_parser "abbrev " >> declId >> optDeclSig >> declVal let (binders, type) := expandOptDeclSig stx[2] let modifiers := modifiers.addAttribute { name := `inline } let modifiers := modifiers.addAttribute { name := `reducible } { ref := stx, kind := DefKind.abbrev, modifiers, declId := stx[1], binders, type? := type, value := stx[3] } def mkDefViewOfDef (modifiers : Modifiers) (stx : Syntax) : DefView := -- leading_parser "def " >> declId >> optDeclSig >> declVal >> optDefDeriving let (binders, type) := expandOptDeclSig stx[2] let deriving? := if stx[4].isNone then none else some stx[4][1].getSepArgs { ref := stx, kind := DefKind.def, modifiers, declId := stx[1], binders, type? := type, value := stx[3], deriving? } def mkDefViewOfTheorem (modifiers : Modifiers) (stx : Syntax) : DefView := -- leading_parser "theorem " >> declId >> declSig >> declVal let (binders, type) := expandDeclSig stx[2] { ref := stx, kind := DefKind.theorem, modifiers, declId := stx[1], binders, type? := some type, value := stx[3] } namespace MkInstanceName -- Table for `mkInstanceName` private def kindReplacements : NameMap String := Std.RBMap.ofList [ (``Parser.Term.depArrow, "DepArrow"), (``Parser.Term.«forall», "Forall"), (``Parser.Term.arrow, "Arrow"), (``Parser.Term.prop, "Prop"), (``Parser.Term.sort, "Sort"), (``Parser.Term.type, "Type") ] abbrev M := StateRefT String CommandElabM def isFirst : M Bool := return (← get) == "" def append (str : String) : M Unit := modify fun s => s ++ str partial def collect (stx : Syntax) : M Unit := do match stx with | Syntax.node k args => unless (← isFirst) do match kindReplacements.find? k with | some r => append r | none => pure () for arg in args do collect arg | Syntax.ident (preresolved := preresolved) .. => unless preresolved.isEmpty && (← resolveGlobalName stx.getId).isEmpty do match stx.getId.eraseMacroScopes with | Name.str _ str _ => if str[0].isLower then append str.capitalize else append str | _ => pure () | _ => pure () def mkFreshInstanceName : CommandElabM Name := do let s ← get let idx := s.nextInstIdx modify fun s => { s with nextInstIdx := s.nextInstIdx + 1 } return Lean.Elab.mkFreshInstanceName s.env idx partial def main (type : Syntax) : CommandElabM Name := do /- We use `expandMacros` to expand notation such as `x < y` into `LT.lt x y` -/ let type ← liftMacroM <| expandMacros type let (_, str) ← collect type |>.run "" if str.isEmpty then mkFreshInstanceName else liftMacroM <| mkUnusedBaseName <| Name.mkSimple ("inst" ++ str) end MkInstanceName def mkDefViewOfConstant (modifiers : Modifiers) (stx : Syntax) : CommandElabM DefView := do -- leading_parser "constant " >> declId >> declSig >> optional declValSimple let (binders, type) := expandDeclSig stx[2] let val ← match stx[3].getOptional? with | some val => pure val | none => let val ← `(arbitrary) pure $ Syntax.node ``Parser.Command.declValSimple #[ mkAtomFrom stx ":=", val ] return { ref := stx, kind := DefKind.opaque, modifiers := modifiers, declId := stx[1], binders := binders, type? := some type, value := val } def mkDefViewOfInstance (modifiers : Modifiers) (stx : Syntax) : CommandElabM DefView := do -- leading_parser Term.attrKind >> "instance " >> optNamedPrio >> optional declId >> declSig >> declVal let attrKind ← liftMacroM <| toAttributeKind stx[0] let prio ← liftMacroM <| expandOptNamedPrio stx[2] let attrStx ← `(attr| instance $(quote prio):numLit) let (binders, type) := expandDeclSig stx[4] let modifiers := modifiers.addAttribute { kind := attrKind, name := `instance, stx := attrStx } let declId ← match stx[3].getOptional? with | some declId => pure declId | none => let id ← MkInstanceName.main type pure <| Syntax.node ``Parser.Command.declId #[mkIdentFrom stx id, mkNullNode] return { ref := stx, kind := DefKind.def, modifiers := modifiers, declId := declId, binders := binders, type? := type, value := stx[5] } def mkDefViewOfExample (modifiers : Modifiers) (stx : Syntax) : DefView := -- leading_parser "example " >> declSig >> declVal let (binders, type) := expandDeclSig stx[1] let id := mkIdentFrom stx `_example let declId := Syntax.node ``Parser.Command.declId #[id, mkNullNode] { ref := stx, kind := DefKind.example, modifiers := modifiers, declId := declId, binders := binders, type? := some type, value := stx[2] } def isDefLike (stx : Syntax) : Bool := let declKind := stx.getKind declKind == ``Parser.Command.«abbrev» || declKind == ``Parser.Command.«def» || declKind == ``Parser.Command.«theorem» || declKind == ``Parser.Command.«constant» || declKind == ``Parser.Command.«instance» || declKind == ``Parser.Command.«example» def mkDefView (modifiers : Modifiers) (stx : Syntax) : CommandElabM DefView := let declKind := stx.getKind if declKind == ``Parser.Command.«abbrev» then pure $ mkDefViewOfAbbrev modifiers stx else if declKind == ``Parser.Command.«def» then pure $ mkDefViewOfDef modifiers stx else if declKind == ``Parser.Command.«theorem» then pure $ mkDefViewOfTheorem modifiers stx else if declKind == ``Parser.Command.«constant» then mkDefViewOfConstant modifiers stx else if declKind == ``Parser.Command.«instance» then mkDefViewOfInstance modifiers stx else if declKind == ``Parser.Command.«example» then pure $ mkDefViewOfExample modifiers stx else throwError "unexpected kind of definition" builtin_initialize registerTraceClass `Elab.definition end Command end Lean.Elab
8d7c0dfac6016b62912428c4cadf8808274edcec
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/ring_theory/algebraic.lean
1bf5d63677a2995206d494a8126edefce8728337
[ "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
13,030
lean
/- Copyright (c) 2019 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 ring_theory.integral_closure import data.polynomial.integral_normalization /-! # Algebraic elements and algebraic extensions An element of an R-algebra is algebraic over R if it is the root of a nonzero polynomial. An R-algebra is algebraic over R if and only if all its elements are algebraic over R. The main result in this file proves transitivity of algebraicity: a tower of algebraic field extensions is algebraic. -/ universes u v w open_locale classical open polynomial section variables (R : Type u) {A : Type v} [comm_ring R] [ring A] [algebra R A] /-- An element of an R-algebra is algebraic over R if it is the root of a nonzero polynomial. -/ def is_algebraic (x : A) : Prop := ∃ p : polynomial R, p ≠ 0 ∧ aeval x p = 0 /-- An element of an R-algebra is transcendental over R if it is not algebraic over R. -/ def transcendental (x : A) : Prop := ¬ is_algebraic R x variables {R} /-- A subalgebra is algebraic if all its elements are algebraic. -/ def subalgebra.is_algebraic (S : subalgebra R A) : Prop := ∀ x ∈ S, is_algebraic R x variables (R A) /-- An algebra is algebraic if all its elements are algebraic. -/ def algebra.is_algebraic : Prop := ∀ x : A, is_algebraic R x variables {R A} /-- A subalgebra is algebraic if and only if it is algebraic an algebra. -/ lemma subalgebra.is_algebraic_iff (S : subalgebra R A) : S.is_algebraic ↔ @algebra.is_algebraic R S _ _ (S.algebra) := begin delta algebra.is_algebraic subalgebra.is_algebraic, rw [subtype.forall'], apply forall_congr, rintro ⟨x, hx⟩, apply exists_congr, intro p, apply and_congr iff.rfl, have h : function.injective (S.val) := subtype.val_injective, conv_rhs { rw [← h.eq_iff, alg_hom.map_zero], }, rw [← aeval_alg_hom_apply, S.val_apply] end /-- An algebra is algebraic if and only if it is algebraic as a subalgebra. -/ lemma algebra.is_algebraic_iff : algebra.is_algebraic R A ↔ (⊤ : subalgebra R A).is_algebraic := begin delta algebra.is_algebraic subalgebra.is_algebraic, simp only [algebra.mem_top, forall_prop_of_true, iff_self], end lemma is_algebraic_iff_not_injective {x : A} : is_algebraic R x ↔ ¬ function.injective (polynomial.aeval x : polynomial R →ₐ[R] A) := by simp only [is_algebraic, alg_hom.injective_iff, not_forall, and.comm, exists_prop] end section zero_ne_one variables (R : Type u) {S : Type*} {A : Type v} [comm_ring R] variables [comm_ring S] [ring A] [algebra R A] [algebra R S] [algebra S A] variables [is_scalar_tower R S A] /-- An integral element of an algebra is algebraic.-/ lemma is_integral.is_algebraic [nontrivial R] {x : A} (h : is_integral R x) : is_algebraic R x := by { rcases h with ⟨p, hp, hpx⟩, exact ⟨p, hp.ne_zero, hpx⟩ } variables {R} /-- An element of `R` is algebraic, when viewed as an element of the `R`-algebra `A`. -/ lemma is_algebraic_algebra_map [nontrivial R] (a : R) : is_algebraic R (algebra_map R A a) := ⟨X - C a, X_sub_C_ne_zero a, by simp only [aeval_C, aeval_X, alg_hom.map_sub, sub_self]⟩ lemma is_algebraic_algebra_map_of_is_algebraic {a : S} (h : is_algebraic R a) : is_algebraic R (algebra_map S A a) := begin obtain ⟨f, hf₁, hf₂⟩ := h, use [f, hf₁], rw [← is_scalar_tower.algebra_map_aeval R S A, hf₂, ring_hom.map_zero] end end zero_ne_one section field variables {K : Type u} {A : Type v} [field K] [ring A] [algebra K A] /-- An element of an algebra over a field is algebraic if and only if it is integral.-/ lemma is_algebraic_iff_is_integral {x : A} : is_algebraic K x ↔ is_integral K x := begin refine ⟨_, is_integral.is_algebraic K⟩, rintro ⟨p, hp, hpx⟩, refine ⟨_, monic_mul_leading_coeff_inv hp, _⟩, rw [← aeval_def, alg_hom.map_mul, hpx, zero_mul], end protected lemma algebra.is_algebraic_iff_is_integral : algebra.is_algebraic K A ↔ algebra.is_integral K A := ⟨λ h x, is_algebraic_iff_is_integral.mp (h x), λ h x, is_algebraic_iff_is_integral.mpr (h x)⟩ end field namespace algebra variables {K : Type*} {L : Type*} {R : Type*} {S : Type*} {A : Type*} variables [field K] [field L] [comm_ring R] [comm_ring S] [comm_ring A] variables [algebra K L] [algebra L A] [algebra K A] [is_scalar_tower K L A] variables [algebra R S] [algebra S A] [algebra R A] [is_scalar_tower R S A] /-- If L is an algebraic field extension of K and A is an algebraic algebra over L, then A is algebraic over K. -/ lemma is_algebraic_trans (L_alg : is_algebraic K L) (A_alg : is_algebraic L A) : is_algebraic K A := begin simp only [is_algebraic, is_algebraic_iff_is_integral] at L_alg A_alg ⊢, exact is_integral_trans L_alg A_alg, end variables (K L) /-- If x is algebraic over R, then x is algebraic over S when S is an extension of R, and the map from `R` to `S` is injective. -/ lemma _root_.is_algebraic_of_larger_base_of_injective (hinj : function.injective (algebra_map R S)) {x : A} (A_alg : _root_.is_algebraic R x) : _root_.is_algebraic S x := let ⟨p, hp₁, hp₂⟩ := A_alg in ⟨p.map (algebra_map _ _), by rwa [ne.def, ← degree_eq_bot, degree_map_eq_of_injective hinj, degree_eq_bot], by simpa⟩ /-- If A is an algebraic algebra over R, then A is algebraic over S when S is an extension of R, and the map from `R` to `S` is injective. -/ lemma is_algebraic_of_larger_base_of_injective (hinj : function.injective (algebra_map R S)) (A_alg : is_algebraic R A) : is_algebraic S A := λ x, is_algebraic_of_larger_base_of_injective hinj (A_alg x) /-- If x is a algebraic over K, then x is algebraic over L when L is an extension of K -/ lemma _root_.is_algebraic_of_larger_base {x : A} (A_alg : _root_.is_algebraic K x) : _root_.is_algebraic L x := _root_.is_algebraic_of_larger_base_of_injective (algebra_map K L).injective A_alg /-- If A is an algebraic algebra over K, then A is algebraic over L when L is an extension of K -/ lemma is_algebraic_of_larger_base (A_alg : is_algebraic K A) : is_algebraic L A := is_algebraic_of_larger_base_of_injective (algebra_map K L).injective A_alg variables {R S} (K L) /-- A field extension is integral if it is finite. -/ lemma is_integral_of_finite [finite_dimensional K L] : algebra.is_integral K L := λ x, is_integral_of_submodule_noetherian ⊤ (is_noetherian.iff_fg.2 infer_instance) x algebra.mem_top /-- A field extension is algebraic if it is finite. -/ lemma is_algebraic_of_finite [finite : finite_dimensional K L] : is_algebraic K L := algebra.is_algebraic_iff_is_integral.mpr (is_integral_of_finite K L) end algebra variables {R S : Type*} [comm_ring R] [is_domain R] [comm_ring S] lemma exists_integral_multiple [algebra R S] {z : S} (hz : is_algebraic R z) (inj : ∀ x, algebra_map R S x = 0 → x = 0) : ∃ (x : integral_closure R S) (y ≠ (0 : R)), z * algebra_map R S y = x := begin rcases hz with ⟨p, p_ne_zero, px⟩, set a := p.leading_coeff with a_def, have a_ne_zero : a ≠ 0 := mt polynomial.leading_coeff_eq_zero.mp p_ne_zero, have y_integral : is_integral R (algebra_map R S a) := is_integral_algebra_map, have x_integral : is_integral R (z * algebra_map R S a) := ⟨p.integral_normalization, monic_integral_normalization p_ne_zero, integral_normalization_aeval_eq_zero px inj⟩, exact ⟨⟨_, x_integral⟩, a, a_ne_zero, rfl⟩ end /-- A fraction `(a : S) / (b : S)` can be reduced to `(c : S) / (d : R)`, if `S` is the integral closure of `R` in an algebraic extension `L` of `R`. -/ lemma is_integral_closure.exists_smul_eq_mul {L : Type*} [field L] [algebra R S] [algebra S L] [algebra R L] [is_scalar_tower R S L] [is_integral_closure S R L] (h : algebra.is_algebraic R L) (inj : function.injective (algebra_map R L)) (a : S) {b : S} (hb : b ≠ 0) : ∃ (c : S) (d ≠ (0 : R)), d • a = b * c := begin obtain ⟨c, d, d_ne, hx⟩ := exists_integral_multiple (h (algebra_map _ L a / algebra_map _ L b)) ((ring_hom.injective_iff _).mp inj), refine ⟨is_integral_closure.mk' S (c : L) c.2, d, d_ne, is_integral_closure.algebra_map_injective S R L _⟩, simp only [algebra.smul_def, ring_hom.map_mul, is_integral_closure.algebra_map_mk', ← hx, ← is_scalar_tower.algebra_map_apply], rw [← mul_assoc _ (_ / _), mul_div_cancel' (algebra_map S L a), mul_comm], exact mt ((ring_hom.injective_iff _).mp (is_integral_closure.algebra_map_injective S R L) _) hb end section field variables {K L : Type*} [field K] [field L] [algebra K L] (A : subalgebra K L) lemma inv_eq_of_aeval_div_X_ne_zero {x : L} {p : polynomial K} (aeval_ne : aeval x (div_X p) ≠ 0) : x⁻¹ = aeval x (div_X p) / (aeval x p - algebra_map _ _ (p.coeff 0)) := begin rw [inv_eq_iff, inv_div, div_eq_iff, sub_eq_iff_eq_add, mul_comm], conv_lhs { rw ← div_X_mul_X_add p }, rw [alg_hom.map_add, alg_hom.map_mul, aeval_X, aeval_C], exact aeval_ne end lemma inv_eq_of_root_of_coeff_zero_ne_zero {x : L} {p : polynomial K} (aeval_eq : aeval x p = 0) (coeff_zero_ne : p.coeff 0 ≠ 0) : x⁻¹ = - (aeval x (div_X p) / algebra_map _ _ (p.coeff 0)) := begin convert inv_eq_of_aeval_div_X_ne_zero (mt (λ h, (algebra_map K L).injective _) coeff_zero_ne), { rw [aeval_eq, zero_sub, div_neg] }, rw ring_hom.map_zero, convert aeval_eq, conv_rhs { rw ← div_X_mul_X_add p }, rw [alg_hom.map_add, alg_hom.map_mul, h, zero_mul, zero_add, aeval_C] end lemma subalgebra.inv_mem_of_root_of_coeff_zero_ne_zero {x : A} {p : polynomial K} (aeval_eq : aeval x p = 0) (coeff_zero_ne : p.coeff 0 ≠ 0) : (x⁻¹ : L) ∈ A := begin have : (x⁻¹ : L) = aeval x (div_X p) / (aeval x p - algebra_map _ _ (p.coeff 0)), { rw [aeval_eq, subalgebra.coe_zero, zero_sub, div_neg], convert inv_eq_of_root_of_coeff_zero_ne_zero _ coeff_zero_ne, { rw subalgebra.aeval_coe }, { simpa using aeval_eq } }, rw [this, div_eq_mul_inv, aeval_eq, subalgebra.coe_zero, zero_sub, ← ring_hom.map_neg, ← ring_hom.map_inv], exact A.mul_mem (aeval x p.div_X).2 (A.algebra_map_mem _), end lemma subalgebra.inv_mem_of_algebraic {x : A} (hx : is_algebraic K (x : L)) : (x⁻¹ : L) ∈ A := begin obtain ⟨p, ne_zero, aeval_eq⟩ := hx, rw [subalgebra.aeval_coe, subalgebra.coe_eq_zero] at aeval_eq, revert ne_zero aeval_eq, refine p.rec_on_horner _ _ _, { intro h, contradiction }, { intros p a hp ha ih ne_zero aeval_eq, refine A.inv_mem_of_root_of_coeff_zero_ne_zero aeval_eq _, rwa [coeff_add, hp, zero_add, coeff_C, if_pos rfl] }, { intros p hp ih ne_zero aeval_eq, rw [alg_hom.map_mul, aeval_X, mul_eq_zero] at aeval_eq, cases aeval_eq with aeval_eq x_eq, { exact ih hp aeval_eq }, { rw [x_eq, subalgebra.coe_zero, inv_zero], exact A.zero_mem } } end /-- In an algebraic extension L/K, an intermediate subalgebra is a field. -/ lemma subalgebra.is_field_of_algebraic (hKL : algebra.is_algebraic K L) : is_field A := { mul_inv_cancel := λ a ha, ⟨ ⟨a⁻¹, A.inv_mem_of_algebraic (hKL a)⟩, subtype.ext (mul_inv_cancel (mt (subalgebra.coe_eq_zero _).mp ha))⟩, .. show nontrivial A, by apply_instance, .. subalgebra.to_comm_ring A } end field section pi variables (R' : Type u) (S' : Type v) (T' : Type w) instance polynomial.has_scalar_pi [semiring R'] [has_scalar R' S'] : has_scalar (polynomial R') (R' → S') := ⟨λ p f x, eval x p • f x⟩ noncomputable instance polynomial.has_scalar_pi' [comm_semiring R'] [semiring S'] [algebra R' S'] [has_scalar S' T'] : has_scalar (polynomial R') (S' → T') := ⟨λ p f x, aeval x p • f x⟩ variables {R} {S} @[simp] lemma polynomial_smul_apply [semiring R'] [has_scalar R' S'] (p : polynomial R') (f : R' → S') (x : R') : (p • f) x = eval x p • f x := rfl @[simp] lemma polynomial_smul_apply' [comm_semiring R'] [semiring S'] [algebra R' S'] [has_scalar S' T'] (p : polynomial R') (f : S' → T') (x : S') : (p • f) x = aeval x p • f x := rfl variables [comm_semiring R'] [comm_semiring S'] [comm_semiring T'] [algebra R' S'] [algebra S' T'] noncomputable instance polynomial.algebra_pi : algebra (polynomial R') (S' → T') := { to_fun := λ p z, algebra_map S' T' (aeval z p), map_one' := funext $ λ z, by simp, map_mul' := λ f g, funext $ λ z, by simp, map_zero' := funext $ λ z, by simp, map_add' := λ f g, funext $ λ z, by simp, commutes' := λ p f, funext $ λ z, mul_comm _ _, smul_def' := λ p f, funext $ λ z, by simp [algebra.algebra_map_eq_smul_one], ..polynomial.has_scalar_pi' R' S' T' } @[simp] lemma polynomial.algebra_map_pi_eq_aeval : (algebra_map (polynomial R') (S' → T') : polynomial R' → (S' → T')) = λ p z, algebra_map _ _ (aeval z p) := rfl @[simp] lemma polynomial.algebra_map_pi_self_eq_eval : (algebra_map (polynomial R') (R' → R') : polynomial R' → (R' → R')) = λ p z, eval z p := rfl end pi
e82d4bfdd1bad10082810272cad05585fe036ce7
63abd62053d479eae5abf4951554e1064a4c45b4
/src/category_theory/limits/shapes/equalizers.lean
f1a0c606ef6b2adc03390b9ff807307dfa2f4cfd
[ "Apache-2.0" ]
permissive
Lix0120/mathlib
0020745240315ed0e517cbf32e738d8f9811dd80
e14c37827456fc6707f31b4d1d16f1f3a3205e91
refs/heads/master
1,673,102,855,024
1,604,151,044,000
1,604,151,044,000
308,930,245
0
0
Apache-2.0
1,604,164,710,000
1,604,163,547,000
null
UTF-8
Lean
false
false
29,869
lean
/- Copyright (c) 2018 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Markus Himmel -/ import category_theory.epi_mono import category_theory.limits.limits /-! # Equalizers and coequalizers This file defines (co)equalizers as special cases of (co)limits. An equalizer is the categorical generalization of the subobject {a ∈ A | f(a) = g(a)} known from abelian groups or modules. It is a limit cone over the diagram formed by `f` and `g`. A coequalizer is the dual concept. ## Main definitions * `walking_parallel_pair` is the indexing category used for (co)equalizer_diagrams * `parallel_pair` is a functor from `walking_parallel_pair` to our category `C`. * a `fork` is a cone over a parallel pair. * there is really only one interesting morphism in a fork: the arrow from the vertex of the fork to the domain of f and g. It is called `fork.ι`. * an `equalizer` is now just a `limit (parallel_pair f g)` Each of these has a dual. ## Main statements * `equalizer.ι_mono` states that every equalizer map is a monomorphism * `is_iso_limit_cone_parallel_pair_of_self` states that the identity on the domain of `f` is an equalizer of `f` and `f`. ## Implementation notes As with the other special shapes in the limits library, all the definitions here are given as `abbreviation`s of the general statements for limits, so all the `simp` lemmas and theorems about general limits can be used. ## References * [F. Borceux, *Handbook of Categorical Algebra 1*][borceux-vol1] -/ noncomputable theory open category_theory namespace category_theory.limits local attribute [tidy] tactic.case_bash universes v u /-- The type of objects for the diagram indexing a (co)equalizer. -/ @[derive decidable_eq, derive inhabited] inductive walking_parallel_pair : Type v | zero | one open walking_parallel_pair /-- The type family of morphisms for the diagram indexing a (co)equalizer. -/ @[derive decidable_eq] inductive walking_parallel_pair_hom : walking_parallel_pair → walking_parallel_pair → Type v | left : walking_parallel_pair_hom zero one | right : walking_parallel_pair_hom zero one | id : Π X : walking_parallel_pair.{v}, walking_parallel_pair_hom X X /-- Satisfying the inhabited linter -/ instance : inhabited (walking_parallel_pair_hom zero one) := { default := walking_parallel_pair_hom.left } open walking_parallel_pair_hom /-- Composition of morphisms in the indexing diagram for (co)equalizers. -/ def walking_parallel_pair_hom.comp : Π (X Y Z : walking_parallel_pair) (f : walking_parallel_pair_hom X Y) (g : walking_parallel_pair_hom Y Z), walking_parallel_pair_hom X Z | _ _ _ (id _) h := h | _ _ _ left (id one) := left | _ _ _ right (id one) := right . instance walking_parallel_pair_hom_category : small_category walking_parallel_pair := { hom := walking_parallel_pair_hom, id := walking_parallel_pair_hom.id, comp := walking_parallel_pair_hom.comp } @[simp] lemma walking_parallel_pair_hom_id (X : walking_parallel_pair) : walking_parallel_pair_hom.id X = 𝟙 X := rfl variables {C : Type u} [category.{v} C] variables {X Y : C} /-- `parallel_pair f g` is the diagram in `C` consisting of the two morphisms `f` and `g` with common domain and codomain. -/ def parallel_pair (f g : X ⟶ Y) : walking_parallel_pair.{v} ⥤ C := { obj := λ x, match x with | zero := X | one := Y end, map := λ x y h, match x, y, h with | _, _, (id _) := 𝟙 _ | _, _, left := f | _, _, right := g end, -- `tidy` can cope with this, but it's too slow: map_comp' := begin rintros (⟨⟩|⟨⟩) (⟨⟩|⟨⟩) (⟨⟩|⟨⟩) ⟨⟩⟨⟩; { unfold_aux, simp; refl }, end, }. @[simp] lemma parallel_pair_obj_zero (f g : X ⟶ Y) : (parallel_pair f g).obj zero = X := rfl @[simp] lemma parallel_pair_obj_one (f g : X ⟶ Y) : (parallel_pair f g).obj one = Y := rfl @[simp] lemma parallel_pair_map_left (f g : X ⟶ Y) : (parallel_pair f g).map left = f := rfl @[simp] lemma parallel_pair_map_right (f g : X ⟶ Y) : (parallel_pair f g).map right = g := rfl @[simp] lemma parallel_pair_functor_obj {F : walking_parallel_pair ⥤ C} (j : walking_parallel_pair) : (parallel_pair (F.map left) (F.map right)).obj j = F.obj j := begin cases j; refl end /-- Every functor indexing a (co)equalizer is naturally isomorphic (actually, equal) to a `parallel_pair` -/ @[simps {rhs_md := semireducible}] def diagram_iso_parallel_pair (F : walking_parallel_pair ⥤ C) : F ≅ parallel_pair (F.map left) (F.map right) := nat_iso.of_components (λ j, eq_to_iso $ by cases j; tidy) $ by tidy /-- A fork on `f` and `g` is just a `cone (parallel_pair f g)`. -/ abbreviation fork (f g : X ⟶ Y) := cone (parallel_pair f g) /-- A cofork on `f` and `g` is just a `cocone (parallel_pair f g)`. -/ abbreviation cofork (f g : X ⟶ Y) := cocone (parallel_pair f g) variables {f g : X ⟶ Y} @[simp, reassoc] lemma fork.app_zero_left (s : fork f g) : s.π.app zero ≫ f = s.π.app one := by rw [←s.w left, parallel_pair_map_left] @[simp, reassoc] lemma fork.app_zero_right (s : fork f g) : s.π.app zero ≫ g = s.π.app one := by rw [←s.w right, parallel_pair_map_right] @[simp, reassoc] lemma cofork.left_app_one (s : cofork f g) : f ≫ s.ι.app one = s.ι.app zero := by rw [←s.w left, parallel_pair_map_left] @[simp, reassoc] lemma cofork.right_app_one (s : cofork f g) : g ≫ s.ι.app one = s.ι.app zero := by rw [←s.w right, parallel_pair_map_right] /-- A fork on `f g : X ⟶ Y` is determined by the morphism `ι : P ⟶ X` satisfying `ι ≫ f = ι ≫ g`. -/ @[simps] def fork.of_ι {P : C} (ι : P ⟶ X) (w : ι ≫ f = ι ≫ g) : fork f g := { X := P, π := { app := λ X, begin cases X, exact ι, exact ι ≫ f, end, naturality' := λ X Y f, begin cases X; cases Y; cases f; dsimp; simp, { dsimp, simp, }, -- See note [dsimp, simp]. { exact w }, { dsimp, simp, }, end } } /-- A cofork on `f g : X ⟶ Y` is determined by the morphism `π : Y ⟶ P` satisfying `f ≫ π = g ≫ π`. -/ @[simps] def cofork.of_π {P : C} (π : Y ⟶ P) (w : f ≫ π = g ≫ π) : cofork f g := { X := P, ι := { app := λ X, begin cases X, exact f ≫ π, exact π, end, naturality' := λ X Y f, begin cases X; cases Y; cases f; dsimp; simp, { dsimp, simp, }, { exact w.symm }, { dsimp, simp, }, end } } /-- A fork `t` on the parallel pair `f g : X ⟶ Y` consists of two morphisms `t.π.app zero : t.X ⟶ X` and `t.π.app one : t.X ⟶ Y`. Of these, only the first one is interesting, and we give it the shorter name `fork.ι t`. -/ abbreviation fork.ι (t : fork f g) := t.π.app zero /-- A cofork `t` on the parallel_pair `f g : X ⟶ Y` consists of two morphisms `t.ι.app zero : X ⟶ t.X` and `t.ι.app one : Y ⟶ t.X`. Of these, only the second one is interesting, and we give it the shorter name `cofork.π t`. -/ abbreviation cofork.π (t : cofork f g) := t.ι.app one @[simp] lemma fork.ι_of_ι {P : C} (ι : P ⟶ X) (w : ι ≫ f = ι ≫ g) : fork.ι (fork.of_ι ι w) = ι := rfl @[simp] lemma cofork.π_of_π {P : C} (π : Y ⟶ P) (w : f ≫ π = g ≫ π) : cofork.π (cofork.of_π π w) = π := rfl lemma fork.ι_eq_app_zero (t : fork f g) : fork.ι t = t.π.app zero := rfl lemma cofork.π_eq_app_one (t : cofork f g) : cofork.π t = t.ι.app one := rfl @[reassoc] lemma fork.condition (t : fork f g) : fork.ι t ≫ f = fork.ι t ≫ g := by rw [t.app_zero_left, t.app_zero_right] @[reassoc] lemma cofork.condition (t : cofork f g) : f ≫ cofork.π t = g ≫ cofork.π t := by rw [t.left_app_one, t.right_app_one] /-- To check whether two maps are equalized by both maps of a fork, it suffices to check it for the first map -/ lemma fork.equalizer_ext (s : fork f g) {W : C} {k l : W ⟶ s.X} (h : k ≫ fork.ι s = l ≫ fork.ι s) : ∀ (j : walking_parallel_pair), k ≫ s.π.app j = l ≫ s.π.app j | zero := h | one := by rw [←fork.app_zero_left, reassoc_of h] /-- To check whether two maps are coequalized by both maps of a cofork, it suffices to check it for the second map -/ lemma cofork.coequalizer_ext (s : cofork f g) {W : C} {k l : s.X ⟶ W} (h : cofork.π s ≫ k = cofork.π s ≫ l) : ∀ (j : walking_parallel_pair), s.ι.app j ≫ k = s.ι.app j ≫ l | zero := by simp only [←cofork.left_app_one, category.assoc, h] | one := h lemma fork.is_limit.hom_ext {s : fork f g} (hs : is_limit s) {W : C} {k l : W ⟶ s.X} (h : k ≫ fork.ι s = l ≫ fork.ι s) : k = l := hs.hom_ext $ fork.equalizer_ext _ h lemma cofork.is_colimit.hom_ext {s : cofork f g} (hs : is_colimit s) {W : C} {k l : s.X ⟶ W} (h : cofork.π s ≫ k = cofork.π s ≫ l) : k = l := hs.hom_ext $ cofork.coequalizer_ext _ h /-- If `s` is a limit fork over `f` and `g`, then a morphism `k : W ⟶ X` satisfying `k ≫ f = k ≫ g` induces a morphism `l : W ⟶ s.X` such that `l ≫ fork.ι s = k`. -/ def fork.is_limit.lift' {s : fork f g} (hs : is_limit s) {W : C} (k : W ⟶ X) (h : k ≫ f = k ≫ g) : {l : W ⟶ s.X // l ≫ fork.ι s = k} := ⟨hs.lift $ fork.of_ι _ h, hs.fac _ _⟩ /-- If `s` is a colimit cofork over `f` and `g`, then a morphism `k : Y ⟶ W` satisfying `f ≫ k = g ≫ k` induces a morphism `l : s.X ⟶ W` such that `cofork.π s ≫ l = k`. -/ def cofork.is_colimit.desc' {s : cofork f g} (hs : is_colimit s) {W : C} (k : Y ⟶ W) (h : f ≫ k = g ≫ k) : {l : s.X ⟶ W // cofork.π s ≫ l = k} := ⟨hs.desc $ cofork.of_π _ h, hs.fac _ _⟩ /-- This is a slightly more convenient method to verify that a fork is a limit cone. It only asks for a proof of facts that carry any mathematical content -/ def fork.is_limit.mk (t : fork f g) (lift : Π (s : fork f g), s.X ⟶ t.X) (fac : ∀ (s : fork f g), lift s ≫ fork.ι t = fork.ι s) (uniq : ∀ (s : fork f g) (m : s.X ⟶ t.X) (w : ∀ j : walking_parallel_pair, m ≫ t.π.app j = s.π.app j), m = lift s) : is_limit t := { lift := lift, fac' := λ s j, walking_parallel_pair.cases_on j (fac s) $ by erw [←s.w left, ←t.w left, ←category.assoc, fac]; refl, uniq' := uniq } /-- This is another convenient method to verify that a fork is a limit cone. It only asks for a proof of facts that carry any mathematical content, and allows access to the same `s` for all parts. -/ def fork.is_limit.mk' {X Y : C} {f g : X ⟶ Y} (t : fork f g) (create : Π (s : fork f g), {l // l ≫ t.ι = s.ι ∧ ∀ {m}, m ≫ t.ι = s.ι → m = l}) : is_limit t := fork.is_limit.mk t (λ s, (create s).1) (λ s, (create s).2.1) (λ s m w, (create s).2.2 (w zero)) /-- This is a slightly more convenient method to verify that a cofork is a colimit cocone. It only asks for a proof of facts that carry any mathematical content -/ def cofork.is_colimit.mk (t : cofork f g) (desc : Π (s : cofork f g), t.X ⟶ s.X) (fac : ∀ (s : cofork f g), cofork.π t ≫ desc s = cofork.π s) (uniq : ∀ (s : cofork f g) (m : t.X ⟶ s.X) (w : ∀ j : walking_parallel_pair, t.ι.app j ≫ m = s.ι.app j), m = desc s) : is_colimit t := { desc := desc, fac' := λ s j, walking_parallel_pair.cases_on j (by erw [←s.w left, ←t.w left, category.assoc, fac]; refl) (fac s), uniq' := uniq } /-- This is another convenient method to verify that a fork is a limit cone. It only asks for a proof of facts that carry any mathematical content, and allows access to the same `s` for all parts. -/ def cofork.is_colimit.mk' {X Y : C} {f g : X ⟶ Y} (t : cofork f g) (create : Π (s : cofork f g), {l : t.X ⟶ s.X // t.π ≫ l = s.π ∧ ∀ {m}, t.π ≫ m = s.π → m = l}) : is_colimit t := cofork.is_colimit.mk t (λ s, (create s).1) (λ s, (create s).2.1) (λ s m w, (create s).2.2 (w one)) /-- This is a helper construction that can be useful when verifying that a category has all equalizers. Given `F : walking_parallel_pair ⥤ C`, which is really the same as `parallel_pair (F.map left) (F.map right)`, and a fork on `F.map left` and `F.map right`, we get a cone on `F`. If you're thinking about using this, have a look at `has_equalizers_of_has_limit_parallel_pair`, which you may find to be an easier way of achieving your goal. -/ def cone.of_fork {F : walking_parallel_pair ⥤ C} (t : fork (F.map left) (F.map right)) : cone F := { X := t.X, π := { app := λ X, t.π.app X ≫ eq_to_hom (by tidy), naturality' := λ j j' g, by { cases j; cases j'; cases g; dsimp; simp } } } /-- This is a helper construction that can be useful when verifying that a category has all coequalizers. Given `F : walking_parallel_pair ⥤ C`, which is really the same as `parallel_pair (F.map left) (F.map right)`, and a cofork on `F.map left` and `F.map right`, we get a cocone on `F`. If you're thinking about using this, have a look at `has_coequalizers_of_has_colimit_parallel_pair`, which you may find to be an easier way of achieving your goal. -/ def cocone.of_cofork {F : walking_parallel_pair ⥤ C} (t : cofork (F.map left) (F.map right)) : cocone F := { X := t.X, ι := { app := λ X, eq_to_hom (by tidy) ≫ t.ι.app X, naturality' := λ j j' g, by { cases j; cases j'; cases g; dsimp; simp } } } @[simp] lemma cone.of_fork_π {F : walking_parallel_pair ⥤ C} (t : fork (F.map left) (F.map right)) (j) : (cone.of_fork t).π.app j = t.π.app j ≫ eq_to_hom (by tidy) := rfl @[simp] lemma cocone.of_cofork_ι {F : walking_parallel_pair ⥤ C} (t : cofork (F.map left) (F.map right)) (j) : (cocone.of_cofork t).ι.app j = eq_to_hom (by tidy) ≫ t.ι.app j := rfl /-- Given `F : walking_parallel_pair ⥤ C`, which is really the same as `parallel_pair (F.map left) (F.map right)` and a cone on `F`, we get a fork on `F.map left` and `F.map right`. -/ def fork.of_cone {F : walking_parallel_pair ⥤ C} (t : cone F) : fork (F.map left) (F.map right) := { X := t.X, π := { app := λ X, t.π.app X ≫ eq_to_hom (by tidy) } } /-- Given `F : walking_parallel_pair ⥤ C`, which is really the same as `parallel_pair (F.map left) (F.map right)` and a cocone on `F`, we get a cofork on `F.map left` and `F.map right`. -/ def cofork.of_cocone {F : walking_parallel_pair ⥤ C} (t : cocone F) : cofork (F.map left) (F.map right) := { X := t.X, ι := { app := λ X, eq_to_hom (by tidy) ≫ t.ι.app X } } @[simp] lemma fork.of_cone_π {F : walking_parallel_pair ⥤ C} (t : cone F) (j) : (fork.of_cone t).π.app j = t.π.app j ≫ eq_to_hom (by tidy) := rfl @[simp] lemma cofork.of_cocone_ι {F : walking_parallel_pair ⥤ C} (t : cocone F) (j) : (cofork.of_cocone t).ι.app j = eq_to_hom (by tidy) ≫ t.ι.app j := rfl /-- Helper function for constructing morphisms between equalizer forks. -/ @[simps] def fork.mk_hom {s t : fork f g} (k : s.X ⟶ t.X) (w : k ≫ t.ι = s.ι) : s ⟶ t := { hom := k, w' := begin rintro ⟨_|_⟩, exact w, simpa using w =≫ f, end } /-- To construct an isomorphism between forks, it suffices to give an isomorphism between the cone points and check that it commutes with the `ι` morphisms. -/ @[simps] def fork.ext {s t : fork f g} (i : s.X ≅ t.X) (w : i.hom ≫ t.ι = s.ι) : s ≅ t := { hom := fork.mk_hom i.hom w, inv := fork.mk_hom i.inv (by rw [← w, iso.inv_hom_id_assoc]) } /-- Helper function for constructing morphisms between coequalizer coforks. -/ @[simps] def cofork.mk_hom {s t : cofork f g} (k : s.X ⟶ t.X) (w : s.π ≫ k = t.π) : s ⟶ t := { hom := k, w' := begin rintro ⟨_|_⟩, simpa using f ≫= w, exact w, end } /-- To construct an isomorphism between coforks, it suffices to give an isomorphism between the cocone points and check that it commutes with the `π` morphisms. -/ def cofork.ext {s t : cofork f g} (i : s.X ≅ t.X) (w : s.π ≫ i.hom = t.π) : s ≅ t := { hom := cofork.mk_hom i.hom w, inv := cofork.mk_hom i.inv (by rw [iso.comp_inv_eq, w]) } variables (f g) section /-- `has_equalizer f g` represents a particular choice of limiting cone for the parallel pair of morphisms `f` and `g`. -/ abbreviation has_equalizer := has_limit (parallel_pair f g) variables [has_equalizer f g] /-- If an equalizer of `f` and `g` exists, we can access an arbitrary choice of such by saying `equalizer f g`. -/ abbreviation equalizer : C := limit (parallel_pair f g) /-- If an equalizer of `f` and `g` exists, we can access the inclusion `equalizer f g ⟶ X` by saying `equalizer.ι f g`. -/ abbreviation equalizer.ι : equalizer f g ⟶ X := limit.π (parallel_pair f g) zero /-- An equalizer cone for a parallel pair `f` and `g`. -/ abbreviation equalizer.fork : fork f g := limit.cone (parallel_pair f g) @[simp] lemma equalizer.fork_ι : (equalizer.fork f g).ι = equalizer.ι f g := rfl @[simp] lemma equalizer.fork_π_app_zero : (equalizer.fork f g).π.app zero = equalizer.ι f g := rfl @[reassoc] lemma equalizer.condition : equalizer.ι f g ≫ f = equalizer.ι f g ≫ g := fork.condition $ limit.cone $ parallel_pair f g /-- The equalizer built from `equalizer.ι f g` is limiting. -/ def equalizer_is_equalizer : is_limit (fork.of_ι (equalizer.ι f g) (equalizer.condition f g)) := is_limit.of_iso_limit (limit.is_limit _) (fork.ext (iso.refl _) (by tidy)) variables {f g} /-- A morphism `k : W ⟶ X` satisfying `k ≫ f = k ≫ g` factors through the equalizer of `f` and `g` via `equalizer.lift : W ⟶ equalizer f g`. -/ abbreviation equalizer.lift {W : C} (k : W ⟶ X) (h : k ≫ f = k ≫ g) : W ⟶ equalizer f g := limit.lift (parallel_pair f g) (fork.of_ι k h) @[simp, reassoc] lemma equalizer.lift_ι {W : C} (k : W ⟶ X) (h : k ≫ f = k ≫ g) : equalizer.lift k h ≫ equalizer.ι f g = k := limit.lift_π _ _ /-- A morphism `k : W ⟶ X` satisfying `k ≫ f = k ≫ g` induces a morphism `l : W ⟶ equalizer f g` satisfying `l ≫ equalizer.ι f g = k`. -/ def equalizer.lift' {W : C} (k : W ⟶ X) (h : k ≫ f = k ≫ g) : {l : W ⟶ equalizer f g // l ≫ equalizer.ι f g = k} := ⟨equalizer.lift k h, equalizer.lift_ι _ _⟩ /-- Two maps into an equalizer are equal if they are are equal when composed with the equalizer map. -/ @[ext] lemma equalizer.hom_ext {W : C} {k l : W ⟶ equalizer f g} (h : k ≫ equalizer.ι f g = l ≫ equalizer.ι f g) : k = l := fork.is_limit.hom_ext (limit.is_limit _) h /-- An equalizer morphism is a monomorphism -/ instance equalizer.ι_mono : mono (equalizer.ι f g) := { right_cancellation := λ Z h k w, equalizer.hom_ext w } end section variables {f g} /-- The equalizer morphism in any limit cone is a monomorphism. -/ lemma mono_of_is_limit_parallel_pair {c : cone (parallel_pair f g)} (i : is_limit c) : mono (fork.ι c) := { right_cancellation := λ Z h k w, fork.is_limit.hom_ext i w } end section variables {f g} /-- The identity determines a cone on the equalizer diagram of `f` and `g` if `f = g`. -/ def id_fork (h : f = g) : fork f g := fork.of_ι (𝟙 X) $ h ▸ rfl /-- The identity on `X` is an equalizer of `(f, g)`, if `f = g`. -/ def is_limit_id_fork (h : f = g) : is_limit (id_fork h) := fork.is_limit.mk _ (λ s, fork.ι s) (λ s, category.comp_id _) (λ s m h, by { convert h zero, exact (category.comp_id _).symm }) /-- Every equalizer of `(f, g)`, where `f = g`, is an isomorphism. -/ def is_iso_limit_cone_parallel_pair_of_eq (h₀ : f = g) {c : cone (parallel_pair f g)} (h : is_limit c) : is_iso (c.π.app zero) := is_iso.of_iso $ is_limit.cone_point_unique_up_to_iso h $ is_limit_id_fork h₀ /-- The equalizer of `(f, g)`, where `f = g`, is an isomorphism. -/ def equalizer.ι_of_eq [has_equalizer f g] (h : f = g) : is_iso (equalizer.ι f g) := is_iso_limit_cone_parallel_pair_of_eq h $ limit.is_limit _ /-- Every equalizer of `(f, f)` is an isomorphism. -/ def is_iso_limit_cone_parallel_pair_of_self {c : cone (parallel_pair f f)} (h : is_limit c) : is_iso (c.π.app zero) := is_iso_limit_cone_parallel_pair_of_eq rfl h /-- An equalizer that is an epimorphism is an isomorphism. -/ def is_iso_limit_cone_parallel_pair_of_epi {c : cone (parallel_pair f g)} (h : is_limit c) [epi (c.π.app zero)] : is_iso (c.π.app zero) := is_iso_limit_cone_parallel_pair_of_eq ((cancel_epi _).1 (fork.condition c)) h end instance has_equalizer_of_self : has_equalizer f f := has_limit.mk { cone := id_fork rfl, is_limit := is_limit_id_fork rfl } /-- The equalizer inclusion for `(f, f)` is an isomorphism. -/ instance equalizer.ι_of_self : is_iso (equalizer.ι f f) := equalizer.ι_of_eq rfl /-- The equalizer of a morphism with itself is isomorphic to the source. -/ def equalizer.iso_source_of_self : equalizer f f ≅ X := as_iso (equalizer.ι f f) @[simp] lemma equalizer.iso_source_of_self_hom : (equalizer.iso_source_of_self f).hom = equalizer.ι f f := rfl @[simp] lemma equalizer.iso_source_of_self_inv : (equalizer.iso_source_of_self f).inv = equalizer.lift (𝟙 X) (by simp) := rfl section /-- `has_coequalizer f g` represents a particular choice of colimiting cocone for the parallel pair of morphisms `f` and `g`. -/ abbreviation has_coequalizer := has_colimit (parallel_pair f g) variables [has_coequalizer f g] /-- If a coequalizer of `f` and `g` exists, we can access an arbitrary choice of such by saying `coequalizer f g`. -/ abbreviation coequalizer : C := colimit (parallel_pair f g) /-- If a coequalizer of `f` and `g` exists, we can access the corresponding projection by saying `coequalizer.π f g`. -/ abbreviation coequalizer.π : Y ⟶ coequalizer f g := colimit.ι (parallel_pair f g) one /-- An arbitrary choice of coequalizer cocone for a parallel pair `f` and `g`. -/ abbreviation coequalizer.cofork : cofork f g := colimit.cocone (parallel_pair f g) @[simp] lemma coequalizer.cofork_π : (coequalizer.cofork f g).π = coequalizer.π f g := rfl @[simp] lemma coequalizer.cofork_ι_app_one : (coequalizer.cofork f g).ι.app one = coequalizer.π f g := rfl @[reassoc] lemma coequalizer.condition : f ≫ coequalizer.π f g = g ≫ coequalizer.π f g := cofork.condition $ colimit.cocone $ parallel_pair f g /-- The cofork built from `coequalizer.π f g` is colimiting. -/ def coequalizer_is_coequalizer : is_colimit (cofork.of_π (coequalizer.π f g) (coequalizer.condition f g)) := is_colimit.of_iso_colimit (colimit.is_colimit _) (cofork.ext (iso.refl _) (by tidy)) variables {f g} /-- Any morphism `k : Y ⟶ W` satisfying `f ≫ k = g ≫ k` factors through the coequalizer of `f` and `g` via `coequalizer.desc : coequalizer f g ⟶ W`. -/ abbreviation coequalizer.desc {W : C} (k : Y ⟶ W) (h : f ≫ k = g ≫ k) : coequalizer f g ⟶ W := colimit.desc (parallel_pair f g) (cofork.of_π k h) @[simp, reassoc] lemma coequalizer.π_desc {W : C} (k : Y ⟶ W) (h : f ≫ k = g ≫ k) : coequalizer.π f g ≫ coequalizer.desc k h = k := colimit.ι_desc _ _ /-- Any morphism `k : Y ⟶ W` satisfying `f ≫ k = g ≫ k` induces a morphism `l : coequalizer f g ⟶ W` satisfying `coequalizer.π ≫ g = l`. -/ def coequalizer.desc' {W : C} (k : Y ⟶ W) (h : f ≫ k = g ≫ k) : {l : coequalizer f g ⟶ W // coequalizer.π f g ≫ l = k} := ⟨coequalizer.desc k h, coequalizer.π_desc _ _⟩ /-- Two maps from a coequalizer are equal if they are equal when composed with the coequalizer map -/ @[ext] lemma coequalizer.hom_ext {W : C} {k l : coequalizer f g ⟶ W} (h : coequalizer.π f g ≫ k = coequalizer.π f g ≫ l) : k = l := cofork.is_colimit.hom_ext (colimit.is_colimit _) h /-- A coequalizer morphism is an epimorphism -/ instance coequalizer.π_epi : epi (coequalizer.π f g) := { left_cancellation := λ Z h k w, coequalizer.hom_ext w } end section variables {f g} /-- The coequalizer morphism in any colimit cocone is an epimorphism. -/ lemma epi_of_is_colimit_parallel_pair {c : cocone (parallel_pair f g)} (i : is_colimit c) : epi (c.ι.app one) := { left_cancellation := λ Z h k w, cofork.is_colimit.hom_ext i w } end section variables {f g} /-- The identity determines a cocone on the coequalizer diagram of `f` and `g`, if `f = g`. -/ def id_cofork (h : f = g) : cofork f g := cofork.of_π (𝟙 Y) $ h ▸ rfl /-- The identity on `Y` is a coequalizer of `(f, g)`, where `f = g`. -/ def is_colimit_id_cofork (h : f = g) : is_colimit (id_cofork h) := cofork.is_colimit.mk _ (λ s, cofork.π s) (λ s, category.id_comp _) (λ s m h, by { convert h one, exact (category.id_comp _).symm }) /-- Every coequalizer of `(f, g)`, where `f = g`, is an isomorphism. -/ def is_iso_colimit_cocone_parallel_pair_of_eq (h₀ : f = g) {c : cocone (parallel_pair f g)} (h : is_colimit c) : is_iso (c.ι.app one) := is_iso.of_iso $ is_colimit.cocone_point_unique_up_to_iso (is_colimit_id_cofork h₀) h /-- The coequalizer of `(f, g)`, where `f = g`, is an isomorphism. -/ def coequalizer.π_of_eq [has_coequalizer f g] (h : f = g) : is_iso (coequalizer.π f g) := is_iso_colimit_cocone_parallel_pair_of_eq h $ colimit.is_colimit _ /-- Every coequalizer of `(f, f)` is an isomorphism. -/ def is_iso_colimit_cocone_parallel_pair_of_self {c : cocone (parallel_pair f f)} (h : is_colimit c) : is_iso (c.ι.app one) := is_iso_colimit_cocone_parallel_pair_of_eq rfl h /-- A coequalizer that is a monomorphism is an isomorphism. -/ def is_iso_limit_cocone_parallel_pair_of_epi {c : cocone (parallel_pair f g)} (h : is_colimit c) [mono (c.ι.app one)] : is_iso (c.ι.app one) := is_iso_colimit_cocone_parallel_pair_of_eq ((cancel_mono _).1 (cofork.condition c)) h end instance has_coequalizer_of_self : has_coequalizer f f := has_colimit.mk { cocone := id_cofork rfl, is_colimit := is_colimit_id_cofork rfl } /-- The coequalizer projection for `(f, f)` is an isomorphism. -/ instance coequalizer.π_of_self : is_iso (coequalizer.π f f) := coequalizer.π_of_eq rfl /-- The coequalizer of a morphism with itself is isomorphic to the target. -/ def coequalizer.iso_target_of_self : coequalizer f f ≅ Y := (as_iso (coequalizer.π f f)).symm @[simp] lemma coequalizer.iso_target_of_self_hom : (coequalizer.iso_target_of_self f).hom = coequalizer.desc (𝟙 Y) (by simp) := rfl @[simp] lemma coequalizer.iso_target_of_self_inv : (coequalizer.iso_target_of_self f).inv = coequalizer.π f f := rfl variables (C) /-- `has_equalizers` represents a choice of equalizer for every pair of morphisms -/ abbreviation has_equalizers := has_limits_of_shape walking_parallel_pair C /-- `has_coequalizers` represents a choice of coequalizer for every pair of morphisms -/ abbreviation has_coequalizers := has_colimits_of_shape walking_parallel_pair C /-- If `C` has all limits of diagrams `parallel_pair f g`, then it has all equalizers -/ lemma has_equalizers_of_has_limit_parallel_pair [Π {X Y : C} {f g : X ⟶ Y}, has_limit (parallel_pair f g)] : has_equalizers C := { has_limit := λ F, has_limit_of_iso (diagram_iso_parallel_pair F).symm } /-- If `C` has all colimits of diagrams `parallel_pair f g`, then it has all coequalizers -/ lemma has_coequalizers_of_has_colimit_parallel_pair [Π {X Y : C} {f g : X ⟶ Y}, has_colimit (parallel_pair f g)] : has_coequalizers C := { has_colimit := λ F, has_colimit_of_iso (diagram_iso_parallel_pair F) } section -- In this section we show that a split mono `f` equalizes `(retraction f ≫ f)` and `(𝟙 Y)`. variables {C} [split_mono f] /-- A split mono `f` equalizes `(retraction f ≫ f)` and `(𝟙 Y)`. Here we build the cone, and show in `split_mono_equalizes` that it is a limit cone. -/ def cone_of_split_mono : cone (parallel_pair (𝟙 Y) (retraction f ≫ f)) := fork.of_ι f (by tidy) @[simp] lemma cone_of_split_mono_π_app_zero : (cone_of_split_mono f).π.app zero = f := rfl @[simp] lemma cone_of_split_mono_π_app_one : (cone_of_split_mono f).π.app one = f ≫ 𝟙 Y := rfl /-- A split mono `f` equalizes `(retraction f ≫ f)` and `(𝟙 Y)`. -/ def split_mono_equalizes {X Y : C} (f : X ⟶ Y) [split_mono f] : is_limit (cone_of_split_mono f) := { lift := λ s, s.π.app zero ≫ retraction f, fac' := λ s, begin rintros (⟨⟩|⟨⟩), { rw [cone_of_split_mono_π_app_zero], erw [category.assoc, ← s.π.naturality right, s.π.naturality left, category.comp_id], }, { erw [cone_of_split_mono_π_app_one, category.comp_id, category.assoc, ← s.π.naturality right, category.id_comp], } end, uniq' := λ s m w, begin rw ←(w zero), simp, end, } end section -- In this section we show that a split epi `f` coequalizes `(f ≫ section_ f)` and `(𝟙 X)`. variables {C} [split_epi f] /-- A split epi `f` coequalizes `(f ≫ section_ f)` and `(𝟙 X)`. Here we build the cocone, and show in `split_epi_coequalizes` that it is a colimit cocone. -/ def cocone_of_split_epi : cocone (parallel_pair (𝟙 X) (f ≫ section_ f)) := cofork.of_π f (by tidy) @[simp] lemma cocone_of_split_epi_ι_app_one : (cocone_of_split_epi f).ι.app one = f := rfl @[simp] lemma cocone_of_split_epi_ι_app_zero : (cocone_of_split_epi f).ι.app zero = 𝟙 X ≫ f := rfl /-- A split epi `f` coequalizes `(f ≫ section_ f)` and `(𝟙 X)`. -/ def split_epi_coequalizes {X Y : C} (f : X ⟶ Y) [split_epi f] : is_colimit (cocone_of_split_epi f) := { desc := λ s, section_ f ≫ s.ι.app one, fac' := λ s, begin rintros (⟨⟩|⟨⟩), { erw [cocone_of_split_epi_ι_app_zero, category.assoc, category.id_comp, ←category.assoc, s.ι.naturality right, functor.const.obj_map, category.comp_id], }, { erw [cocone_of_split_epi_ι_app_one, ←category.assoc, s.ι.naturality right, ←s.ι.naturality left, category.id_comp] } end, uniq' := λ s m w, begin rw ←(w one), simp, end, } end end category_theory.limits
975d0ef5aabb1c56d5dd61de279942019b1d5cab
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/tests/lean/1363.lean
543a6db5e99efc9d7f8bfd2a88abe39a4a8cb86f
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
EdAyers/lean4
57ac632d6b0789cb91fab2170e8c9e40441221bd
37ba0df5841bde51dbc2329da81ac23d4f6a4de4
refs/heads/master
1,676,463,245,298
1,660,619,433,000
1,660,619,433,000
183,433,437
1
0
Apache-2.0
1,657,612,672,000
1,556,196,574,000
Lean
UTF-8
Lean
false
false
512
lean
import Lean.Data.Parsec open Lean Parsec @[macroInline] -- Error def f : Nat → Nat | 0 => 0 | n + 1 => f n @[macroInline] -- Error def g : Nat → Nat | 0 => 0 | n + 1 => g n termination_by g x => x @[macroInline] -- Error def h : Nat → Nat → Nat | 0, _ => 0 | n + 1, m => h n m termination_by h x y => x @[macroInline] -- Error partial def skipMany (p : Parsec α) (it : String.Iterator) : Parsec PUnit := do match p it with | .success it _ => skipMany p it | .error _ _ => pure ()
851c5000bd4ffc959a0b584b0df28362942df104
de91c42b87530c3bdcc2b138ef1a3c3d9bee0d41
/expressions/time_series/geom3d_expr.lean
01259d06ac12287e955eb8d16cd412961d59401d
[]
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
25,854
lean
--import ....phys.geom.geom3d import ..geom3d_expr --import .....phys.series.geom3d import ..time_expr import ......phys.geom.geom3d import ......phys.time_series.geom3d namespace lang.series.geom3d universes u open lang.geom3d open lang.time variables {fr : time_frame_expr} (ts : time_space_expr fr) structure timestamped_geom3d_transform_var {fr : time_frame_expr} (ts : time_space_expr fr) {f1 : geom3d_frame_expr} (sp1 : geom3d_space_expr f1) {f2 : geom3d_frame_expr} (sp2 : geom3d_space_expr f2) extends var inductive timestamped_geom3d_transform_expr : Π {fr : time_frame_expr} (ts : time_space_expr fr), Π {f1 : geom3d_frame_expr} (sp1 : geom3d_space_expr f1), Π {f2 : geom3d_frame_expr} (sp2 : geom3d_space_expr f2), Type 1 | lit {fr : time_frame_expr} {ts : time_space_expr fr} {f1 : geom3d_frame_expr} {sp1 : geom3d_space_expr f1} {f2 : geom3d_frame_expr} {sp2 : geom3d_space_expr f2} (l : timestamped ts.value (geom3d_transform sp1.value sp2.value)): timestamped_geom3d_transform_expr ts sp1 sp2 | lit_time {fr : time_frame_expr} {ts : time_space_expr fr} {f1 : geom3d_frame_expr} {sp1 : geom3d_space_expr f1} {f2 : geom3d_frame_expr} {sp2 : geom3d_space_expr f2} (te : time_expr ts) (pe : geom3d_transform_expr sp1 sp2): timestamped_geom3d_transform_expr ts sp1 sp2 class timestamped_geom3d_transform_has_lit {fr : time_frame_expr} (ts : time_space_expr fr) {f1 : geom3d_frame_expr} (sp1 : geom3d_space_expr f1) {f2 : geom3d_frame_expr} (sp2 : geom3d_space_expr f2) := (cast : timestamped ts.value (geom3d_transform sp1.value sp2.value) → timestamped_geom3d_transform_expr ts sp1 sp2) notation `|`tlit`|` := timestamped_geom3d_transform_has_lit.cast tlit instance timestamped_geom3d_transform_lit {fr : time_frame_expr} (ts : time_space_expr fr) {f1 : geom3d_frame_expr} (sp1 : geom3d_space_expr f1) {f2 : geom3d_frame_expr} (sp2 : geom3d_space_expr f2) : timestamped_geom3d_transform_has_lit ts sp1 sp2 := ⟨λt, timestamped_geom3d_transform_expr.lit t⟩ abbreviation timestamped_geom3d_transform_env {fr : time_frame_expr} (ts : time_space_expr fr) {f1 : geom3d_frame_expr} (sp1 : geom3d_space_expr f1) {f2 : geom3d_frame_expr} (sp2 : geom3d_space_expr f2) := timestamped_geom3d_transform_var ts sp1 sp2 → timestamped ts.value (geom3d_transform sp1.value sp2.value) abbreviation timestamped_geom3d_transform_eval := Π{fr : time_frame_expr} (ts : time_space_expr fr), Π{f1 : geom3d_frame_expr} (sp1 : geom3d_space_expr f1) {f2 : geom3d_frame_expr} (sp2 : geom3d_space_expr f2) , timestamped_geom3d_transform_env ts sp1 sp2 → timestamped_geom3d_transform_expr ts sp1 sp2 → timestamped ts.value (geom3d_transform sp1.value sp2.value) @[simp] noncomputable def default_timestamped_geom3d_transform_env {fr : time_frame_expr} (ts : time_space_expr fr) {f1 : geom3d_frame_expr} (sp1 : geom3d_space_expr f1) {f2 : geom3d_frame_expr} (sp2 : geom3d_space_expr f2) : timestamped_geom3d_transform_env ts sp1 sp2 := λv, @mk_default_timestamped fr.value ts.value (geom3d_transform sp1.value sp2.value) _ --set_option eqn_compiler.max_steps 16384 @[simp] noncomputable def default_timestamped_geom3d_transform_eval : timestamped_geom3d_transform_eval := λtf ts gf gs gf2 gs2, λenv_, λexpr_, @mk_default_timestamped tf.value ts.value (geom3d_transform gs.value gs2.value) _ @[simp] noncomputable def static_timestamped_geom3d_transform_eval : timestamped_geom3d_transform_eval | tf ts gf gs gf2 gs2 env_ (timestamped_geom3d_transform_expr.lit p) := p | tf ts gf gs gf2 gs2 env_ (timestamped_geom3d_transform_expr.lit_time te pe) := ⟨te.value, pe.value⟩ @[simp] noncomputable def timestamped_geom3d_transform_expr.value {fr : time_frame_expr} {ts : time_space_expr fr} {f1 : geom3d_frame_expr} {sp1 : geom3d_space_expr f1} {f2 : geom3d_frame_expr} {sp2 : geom3d_space_expr f2} (expr_ : timestamped_geom3d_transform_expr ts sp1 sp2) : timestamped ts.value (geom3d_transform sp1.value sp2.value) := (static_timestamped_geom3d_transform_eval ts sp1 sp2) (default_timestamped_geom3d_transform_env ts sp1 sp2) expr_ structure geom3d_transform_series_var {fr : time_frame_expr} (ts : time_space_expr fr) {f1 : geom3d_frame_expr} (sp1 : geom3d_space_expr f1) {f2 : geom3d_frame_expr} (sp2 : geom3d_space_expr f2) extends var #check geom3d_transform_discrete inductive geom3d_transform_series_expr : Π {fr : time_frame_expr} (ts : time_space_expr fr), Π {f1 : geom3d_frame_expr} (sp1 : geom3d_space_expr f1), Π {f2 : geom3d_frame_expr} (sp2 : geom3d_space_expr f2), Type 1 | lit {fr : time_frame_expr} {ts : time_space_expr fr} {f1 : geom3d_frame_expr} {sp1 : geom3d_space_expr f1} {f2 : geom3d_frame_expr} {sp2 : geom3d_space_expr f2} (p : geom3d_transform_discrete ts.value sp1.value sp2.value) : geom3d_transform_series_expr ts sp1 sp2 | var {fr : time_frame_expr} {ts : time_space_expr fr} {f1 : geom3d_frame_expr} {sp1 : geom3d_space_expr f1} {f2 : geom3d_frame_expr} {sp2 : geom3d_space_expr f2} (v : geom3d_transform_series_var ts sp1 sp2) : geom3d_transform_series_expr ts sp1 sp2 | update {fr : time_frame_expr} {ts : time_space_expr fr} {f1 : geom3d_frame_expr} {sp1 : geom3d_space_expr f1} {f2 : geom3d_frame_expr} {sp2 : geom3d_space_expr f2} (ges : geom3d_transform_series_expr ts sp1 sp2) (te : time_expr ts) (ge : geom3d_transform_expr sp1 sp2) : geom3d_transform_series_expr ts sp1 sp2 | update_ts {fr : time_frame_expr} {ts : time_space_expr fr} {f1 : geom3d_frame_expr} {sp1 : geom3d_space_expr f1} {f2 : geom3d_frame_expr} {sp2 : geom3d_space_expr f2} (ges : geom3d_transform_series_expr ts sp1 sp2) (te : timestamped_geom3d_transform_expr ts sp1 sp2) : geom3d_transform_series_expr ts sp1 sp2 class geom3d_transform_series_has_lit {fr : time_frame_expr} (ts : time_space_expr fr) {f1 : geom3d_frame_expr} (sp1 : geom3d_space_expr f1) {f2 : geom3d_frame_expr} (sp2 : geom3d_space_expr f2) := (cast : geom3d_transform_discrete ts.value sp1.value sp2.value → geom3d_transform_series_expr ts sp1 sp2) notation `|`tlit`|` := geom3d_transform_series_has_lit.cast tlit instance geom3d_transform_series_lit {fr : time_frame_expr} (ts : time_space_expr fr) {f1 : geom3d_frame_expr} (sp1 : geom3d_space_expr f1) {f2 : geom3d_frame_expr} (sp2 : geom3d_space_expr f2) : geom3d_transform_series_has_lit ts sp1 sp2 := ⟨λt, geom3d_transform_series_expr.lit t⟩ class timestamped_geom3d_transform_expr_has_maplit {tf : time_frame_expr} (ts : time_space_expr tf) {f1 : geom3d_frame_expr} (sp1 : geom3d_space_expr f1) {f2 : geom3d_frame_expr} (sp2 : geom3d_space_expr f2):= (cast : time_expr ts → geom3d_transform_expr sp1 sp2 → timestamped_geom3d_transform_expr ts sp1 sp2) notation T`↦`E := timestamped_geom3d_transform_expr_has_maplit.cast T E notation |T,E| := timestamped_geom3d_transform_expr_has_maplit.case T E instance timestamped_geom3d_transform_expr_map_lit {tf : time_frame_expr} (ts : time_space_expr tf) {f1 : geom3d_frame_expr} (sp1 : geom3d_space_expr f1) {f2 : geom3d_frame_expr} (sp2 : geom3d_space_expr f2) : timestamped_geom3d_transform_expr_has_maplit ts sp1 sp2 := ⟨λte pe , timestamped_geom3d_transform_expr.lit_time te pe⟩ class geom3d_transform_series_has_updatets{tf : time_frame_expr} (ts : time_space_expr tf) {f1 : geom3d_frame_expr} (sp1 : geom3d_space_expr f1) {f2 : geom3d_frame_expr} (sp2 : geom3d_space_expr f2) := (cast : geom3d_transform_series_expr ts sp1 sp2 → timestamped_geom3d_transform_expr ts sp1 sp2 → geom3d_transform_series_expr ts sp1 sp2) notation S`[`GES`]` := geom3d_transform_series_has_updatets.cast S GES instance geom3d_transform_series_update {tf : time_frame_expr} (ts : time_space_expr tf) {f1 : geom3d_frame_expr} (sp1 : geom3d_space_expr f1) {f2 : geom3d_frame_expr} (sp2 : geom3d_space_expr f2) : geom3d_transform_series_has_updatets ts sp1 sp2 := ⟨λpes ges, geom3d_transform_series_expr.update_ts pes ges⟩ abbreviation geom3d_transform_series_env {fr : time_frame_expr} (ts : time_space_expr fr) {f1 : geom3d_frame_expr} (sp1 : geom3d_space_expr f1) {f2 : geom3d_frame_expr} (sp2 : geom3d_space_expr f2) := geom3d_transform_series_var ts sp1 sp2 → geom3d_transform_discrete ts.value sp1.value sp2.value abbreviation geom3d_transform_series_eval := Π{fr : time_frame_expr} (ts : time_space_expr fr), Π{f1 : geom3d_frame_expr} (sp1 : geom3d_space_expr f1) {f2 : geom3d_frame_expr} (sp2 : geom3d_space_expr f2) , geom3d_transform_series_env ts sp1 sp2 → geom3d_transform_series_expr ts sp1 sp2 → geom3d_transform_discrete ts.value sp1.value sp2.value @[simp] noncomputable def default_geom3d_transform_series_env {fr : time_frame_expr} (ts : time_space_expr fr) {f1 : geom3d_frame_expr} (sp1 : geom3d_space_expr f1) {f2 : geom3d_frame_expr} (sp2 : geom3d_space_expr f2) : geom3d_transform_series_env ts sp1 sp2:= λv, @mk_geom3d_transform_discrete_empty fr.value ts.value f1.value sp1.value f2.value sp2.value-- (sp1.value).mk_geom3d_transform_to sp2.value @[simp] noncomputable def default_geom3d_transform_series_eval : geom3d_transform_series_eval := λtf ts gf gs gf2 gs2, λenv_, λexpr_, @mk_geom3d_transform_discrete_empty _ ts.value _ gs.value _ gs2.value @[simp,reducible] noncomputable def static_geom3d_transform_series_eval -- {fr : time_frame_expr} (ts : time_space_expr fr) -- {f1 : geom3d_frame_expr} (sp1 : geom3d_space_expr f1) {f2 : geom3d_frame_expr} (sp2 : geom3d_space_expr f2) : geom3d_transform_series_eval --ts sp1 sp2 | tf ts gf gs gf2 gs2 env_ (geom3d_transform_series_expr.lit tr) := tr | tf ts gf gs gf2 gs2 env_ (geom3d_transform_series_expr.var v) := env_ v | tf ts gf gs gf2 gs2 env_ (geom3d_transform_series_expr.update ges te ge) := ((static_geom3d_transform_series_eval ts gs gs2) (default_geom3d_transform_series_env ts gs gs2) ges) .update ⟨te.value, ge.value⟩ | tf ts gf gs gf2 gs2 env_ (geom3d_transform_series_expr.update_ts ges tse) := ((static_geom3d_transform_series_eval ts gs gs2) (default_geom3d_transform_series_env ts gs gs2) ges) .update tse.value @[simp] noncomputable def geom3d_transform_series_expr.value {fr : time_frame_expr} {ts : time_space_expr fr} {f1 : geom3d_frame_expr} {sp1 : geom3d_space_expr f1} {f2 : geom3d_frame_expr} {sp2 : geom3d_space_expr f2} (expr_ : geom3d_transform_series_expr ts sp1 sp2) --: geom3d_transform_discrete ts.value sp1.value sp2.value := ((static_geom3d_transform_series_eval ts sp1 sp2) (default_geom3d_transform_series_env ts sp1 sp2) expr_) variables {f1 : geom3d_frame_expr} {sp1 : geom3d_space_expr f1} {f2 : geom3d_frame_expr} {sp2 : geom3d_space_expr f2} (expr_ : geom3d_transform_series_expr ts sp1 sp2) structure displacement3d_var {f : geom3d_frame_expr} (sp : geom3d_space_expr f) extends var structure position3d_var {f : geom3d_frame_expr} (sp : geom3d_space_expr f) extends var mutual inductive displacement3d_series_expr, position3d_series_expr {fr : time_frame_expr} (ts : time_space_expr fr) {f : geom3d_frame_expr} (sp : geom3d_space_expr f) with displacement3d_series_expr : Type 1 | lit (v : displacement3d_discrete ts.value sp.value) : displacement3d_series_expr | var (v : displacement3d_var sp) : displacement3d_series_expr | update (se : displacement3d_series_expr) (te : time_expr ts) (de : displacement3d_expr sp) : displacement3d_series_expr with position3d_series_expr : Type 1 | lit (p : position3d_discrete ts.value sp.value) : position3d_series_expr | var (v : position3d_var sp) : position3d_series_expr | update (se : position3d_series_expr) (te : time_expr ts) (de : position3d_expr sp) : position3d_series_expr /- abbreviation displacement3d_env {f : geom3d_frame_expr} (sp : geom3d_space_expr f) := displacement3d_var sp → displacement3d_discrete sp.value attribute [elab_as_eliminator] abbreviation position3d_env {f : geom3d_frame_expr} (sp : geom3d_space_expr f) := position3d_var sp → position3d_discrete sp.value abbreviation displacement3d_eval := Π{f : geom3d_frame_expr} (sp : geom3d_space_expr f), position3d_env sp → displacement3d_env sp → displacement3d_series_expr sp → displacement3d_discrete sp.value abbreviation position3d_eval := Π{f : geom3d_frame_expr} (sp : geom3d_space_expr f), position3d_env sp → displacement3d_env sp → position3d_series_expr sp → position3d_discrete sp.value noncomputable def default_displacement3d_env {f : geom3d_frame_expr} (sp : geom3d_space_expr f) : displacement3d_env sp := λv, (mk_displacement3d_discrete sp.value 1 1 1) noncomputable def default_displacement3d_eval : displacement3d_eval := λf sp, λtenv_, λdenv_, λexpr_, begin repeat {exact (mk_displacement3d_discrete sp.value 1 1 1)} end noncomputable def default_position3d_env {f : geom3d_frame_expr} (sp : geom3d_space_expr f) : position3d_env sp := (λv, (mk_position3d_discrete sp.value 1 1 1)) set_option eqn_compiler.max_steps 8192 noncomputable mutual def static_displacement3d_eval, static_position3d_eval with static_displacement3d_eval : displacement3d_eval | f sp tenv_ denv_ (displacement3d_series_expr.zero) := 0 | f sp tenv_ denv_ (displacement3d_series_expr.one) := mk_displacement3d_discrete sp.value 1 1 1 | f sp tenv_ denv_ (displacement3d_series_expr.lit d) := d | f sp tenv_ denv_ (displacement3d_series_expr.var v) := denv_ v | f sp tenv_ denv_ (displacement3d_series_expr.add_displacement3d_displacement3d_discrete d1 d2) := (static_displacement3d_eval sp tenv_ denv_ d1) +ᵥ (static_displacement3d_eval sp tenv_ denv_ d2) | f sp tenv_ denv_ (displacement3d_series_expr.neg_displacement3d_discrete d) := -(static_displacement3d_eval sp tenv_ denv_ d) | f sp tenv_ denv_ (displacement3d_series_expr.sub_displacement3d_displacement3d_discrete d1 d2) := (static_displacement3d_eval sp tenv_ denv_ d1) -ᵥ (static_displacement3d_eval sp tenv_ denv_ d2) | f sp tenv_ denv_ (displacement3d_series_expr.sub_position3d_position3d_discrete t1 t2) := (static_position3d_eval sp tenv_ denv_ t1) -ᵥ (static_position3d_eval sp tenv_ denv_ t2) | f sp tenv_ denv_ (displacement3d_series_expr.smul_displacement3d_discrete s d) := (static_real_scalar_eval default_real_scalar_env s)•(static_displacement3d_eval sp tenv_ denv_ d) | f sp tenv_ denv_ (displacement3d_series_expr.apply_displacement3d_lit t d) := t.value.transform_displacement3d_discrete d with static_position3d_eval : position3d_eval | f sp tenv_ denv_ (position3d_series_expr.lit p) := p | f sp tenv_ denv_ (position3d_series_expr.var v) := tenv_ v | f sp tenv_ denv_ (position3d_series_expr.add_displacement3d_position3d_discrete d t) := (static_displacement3d_eval sp tenv_ denv_ d) +ᵥ (static_position3d_eval sp tenv_ denv_ t) | f sp tenv_ denv_ (position3d_series_expr.apply_position3d_lit tr t) := tr.value.transform_position3d_discrete t noncomputable def default_position3d_eval : position3d_eval := λf sp, λtenv_, λdenv_, λexpr_, begin cases expr_, exact expr_, exact default_position3d_env sp expr_, repeat {exact (mk_position3d_discrete sp.value 1 1 1)} end noncomputable def position3d_series_expr.value {f : geom3d_frame_expr} {sp : geom3d_space_expr f} (expr_ : position3d_series_expr sp) : position3d_discrete sp.value := (static_position3d_eval sp) (default_position3d_env sp) (default_displacement3d_env sp) expr_ noncomputable def displacement3d_series_expr.value {f : geom3d_frame_expr} {sp : geom3d_space_expr f} (expr_ : displacement3d_series_expr sp) : displacement3d_discrete sp.value := (static_displacement3d_eval sp) (default_position3d_env sp) (default_displacement3d_env sp) expr_ class position3d_has_lit {f : geom3d_frame_expr} (sp : geom3d_space_expr f) := (cast : position3d_discrete sp.value → position3d_series_expr sp) notation `|`tlit`|` := position3d_has_lit.cast tlit instance position3d_lit {f : geom3d_frame_expr} (sp : geom3d_space_expr f) : position3d_has_lit sp := ⟨λt : position3d_discrete sp.value, position3d_series_expr.lit t⟩ class displacement3d_has_lit {f : geom3d_frame_expr} (sp : geom3d_space_expr f) := (cast : displacement3d_discrete sp.value → displacement3d_series_expr sp) notation `|`tlit`|` := displacement3d_has_lit.cast tlit instance displacement3d_lit {f : geom3d_frame_expr} (sp : geom3d_space_expr f) : displacement3d_has_lit sp := ⟨λt : displacement3d_discrete sp.value, displacement3d_series_expr.lit t⟩ class position3d_frame_has_lit := (cast : geom3d_frame → geom3d_frame_expr) notation `|`flit`|` := position3d_frame_has_lit.cast flit instance position3d_frame_lit : position3d_frame_has_lit := ⟨λf, geom3d_frame_expr.lit f⟩ class position3d_space_has_lit (f : geom3d_frame_expr ) := (cast : geom3d_space f.value → geom3d_space_expr f) notation `|`slit`|` := position3d_space_has_lit.cast slit instance position3d_space_lit {f : geom3d_frame_expr} : position3d_space_has_lit f := ⟨λs, geom3d_space_expr.lit s⟩ -/ structure timestamped_pose3d_var {tf : time_frame_expr} (ts : time_space_expr tf) {f : geom3d_frame_expr} (sp : geom3d_space_expr f) extends var inductive timestamped_pose3d_expr : Π {tf : time_frame_expr} (ts : time_space_expr tf), Π {f : geom3d_frame_expr} (sp : geom3d_space_expr f), Type 1 | lit {tf : time_frame_expr} {ts : time_space_expr tf} {f : geom3d_frame_expr}{sp : geom3d_space_expr f} (l : timestamped ts.value (pose3d sp.value)): timestamped_pose3d_expr ts sp | lit_time {tf : time_frame_expr} {ts : time_space_expr tf} {f : geom3d_frame_expr}{sp : geom3d_space_expr f} (te : time_expr ts) (pe : pose3d_expr sp): timestamped_pose3d_expr ts sp class timestamped_pose3d_expr_has_lit {fr : time_frame_expr} (ts : time_space_expr fr) {f1 : geom3d_frame_expr} (sp1 : geom3d_space_expr f1) := (cast : timestamped ts.value (pose3d sp1.value) → timestamped_pose3d_expr ts sp1) notation `|`tlit`|` := timestamped_pose3d_expr_has_lit.cast tlit instance timestamped_pose3d_expr_lit {fr : time_frame_expr} (ts : time_space_expr fr) {f1 : geom3d_frame_expr} (sp1 : geom3d_space_expr f1) : timestamped_pose3d_expr_has_lit ts sp1 := ⟨λt, timestamped_pose3d_expr.lit t⟩ abbreviation timestamped_pose3d_env {tf : time_frame_expr} (ts : time_space_expr tf) {f : geom3d_frame_expr} (sp : geom3d_space_expr f) := timestamped_pose3d_var ts sp → timestamped ts.value (pose3d sp.value) abbreviation timestamped_pose3d_eval := Π {tf : time_frame_expr} (ts : time_space_expr tf), Π{f : geom3d_frame_expr} (sp : geom3d_space_expr f), timestamped_pose3d_env ts sp → timestamped_pose3d_expr ts sp → timestamped ts.value (pose3d sp.value) @[simp] noncomputable def default_timestamped_pose3d_env {tf : time_frame_expr} (ts : time_space_expr tf) {f : geom3d_frame_expr} (sp : geom3d_space_expr f) : timestamped_pose3d_env ts sp := λv, @mk_default_timestamped tf.value ts.value (pose3d sp.value) _ --set_option eqn_compiler.max_steps 16384 @[simp] noncomputable def default_timestamped_pose3d_eval : timestamped_pose3d_eval := λtf ts gf gs, λenv_, λexpr_, @mk_default_timestamped tf.value ts.value (pose3d gs.value) _ @[simp] noncomputable def static_timestamped_pose3d_eval : timestamped_pose3d_eval | tf ts gf gs env_ (timestamped_pose3d_expr.lit p) := p | tf ts gf gs env_ (timestamped_pose3d_expr.lit_time te pe) := ⟨te.value, pe.value⟩ @[simp] noncomputable def timestamped_pose3d_expr.value {tf : time_frame_expr} {ts : time_space_expr tf} {f : geom3d_frame_expr} {sp : geom3d_space_expr f} (expr_ : timestamped_pose3d_expr ts sp) : timestamped ts.value (pose3d sp.value) := (static_timestamped_pose3d_eval ts sp) (default_timestamped_pose3d_env ts sp) expr_ structure pose3d_series_var {tf : time_frame_expr} (ts : time_space_expr tf) {f : geom3d_frame_expr} (sp : geom3d_space_expr f) extends var inductive pose3d_series_expr : Π {tf : time_frame_expr} (ts : time_space_expr tf), Π {f : geom3d_frame_expr} (sp : geom3d_space_expr f), Type 1 | lit {tf : time_frame_expr} {ts : time_space_expr tf} {f : geom3d_frame_expr}{sp : geom3d_space_expr f} (v : pose3d_discrete ts.value sp.value) : pose3d_series_expr ts sp | update {tf : time_frame_expr} {ts : time_space_expr tf} {f : geom3d_frame_expr}{sp : geom3d_space_expr f} (pes : pose3d_series_expr ts sp) (te : time_expr ts) (pe : pose3d_expr sp) : pose3d_series_expr ts sp | update_ts {tf : time_frame_expr} {ts : time_space_expr tf} {f : geom3d_frame_expr}{sp : geom3d_space_expr f} (pes : pose3d_series_expr ts sp) (tse : timestamped_pose3d_expr ts sp) : pose3d_series_expr ts sp --| apply_timestamped_transform_to_lit -- {tf : time_frame_expr} {ts : time_space_expr tf} -- {f1 : geom3d_frame_expr}{sp1 : geom3d_space_expr f1} -- {f2 : geom3d_frame_expr}{sp2 : geom3d_space_expr f2} -- (pes : pose3d_series_expr ts sp) (tse : timestamped_pose3d_expr ts sp) : pose3d_series_expr ts sp --| apply_latest_pose3d_lit {f2 : geom3d_frame_expr} {sp2 : geom3d_space_expr f2} -- (pe : pose3d_series_expr) -- (v : geom3d_transform_series_expr ts sp2 sp) -- (t : pose3d_discrete ts.value sp2.value) : pose3d_series_expr --| apply_pose3d_lit {f2 : geom3d_frame_expr} {sp2 : geom3d_space_expr f2} -- (pe : pose3d_series_expr) -- (v : geom3d_transform_series_expr ts sp2 sp) -- (t : pose3d_discrete ts.value sp2.value) : pose3d_series_expr class pose3d_series_has_lit{tf : time_frame_expr} (ts : time_space_expr tf) {f : geom3d_frame_expr} (sp : geom3d_space_expr f):= (cast : pose3d_discrete ts.value sp.value → pose3d_series_expr ts sp) notation `|`tlit`|` := pose3d_series_has_lit.cast tlit instance pose3d_series_lit {tf : time_frame_expr} (ts : time_space_expr tf) {f : geom3d_frame_expr} (sp : geom3d_space_expr f) : pose3d_series_has_lit ts sp := ⟨λt : pose3d_discrete ts.value sp.value, pose3d_series_expr.lit t⟩ class pose3d_series_has_update{tf : time_frame_expr} (ts : time_space_expr tf) {f : geom3d_frame_expr} (sp : geom3d_space_expr f):= (cast : pose3d_series_expr ts sp → timestamped_pose3d_expr ts sp → pose3d_series_expr ts sp) notation S`[`TE`]` := pose3d_series_has_update.cast S TE --notation S`[`T↦E`]`:= pose3d_series_has_update.cast S ⟨T,E\> instance pose3d_series_update {tf : time_frame_expr} (ts : time_space_expr tf) {f : geom3d_frame_expr} (sp : geom3d_space_expr f) : pose3d_series_has_update ts sp := ⟨λpes tes, pose3d_series_expr.update_ts pes tes⟩ class timestamped_pose3d_expr_has_maplit {tf : time_frame_expr} (ts : time_space_expr tf) {f : geom3d_frame_expr} (sp : geom3d_space_expr f):= (cast : time_expr ts → pose3d_expr sp → timestamped_pose3d_expr ts sp) notation T`↦`E := timestamped_pose3d_expr_has_maplit.cast T E notation `|`T`,`E`|` := timestamped_pose3d_expr_has_maplit.cast T E instance timestamped_pose3d_expr_map_lit {tf : time_frame_expr} (ts : time_space_expr tf) {f : geom3d_frame_expr} (sp : geom3d_space_expr f) : timestamped_pose3d_expr_has_maplit ts sp := ⟨λte pe , timestamped_pose3d_expr.lit_time te pe⟩ class timestamped_pose3d_expr_has_litlit {tf : time_frame_expr} (ts : time_space_expr tf) {f : geom3d_frame_expr} (sp : geom3d_space_expr f):= (cast : time ts.value → pose3d sp.value → timestamped_pose3d_expr ts sp) notation T`↦`E := timestamped_pose3d_expr_has_litlit.cast T E notation `|`T`,`E`|` := timestamped_pose3d_expr_has_litlit.cast T E instance timestamped_pose3d_expr_litlit {tf : time_frame_expr} (ts : time_space_expr tf) {f : geom3d_frame_expr} (sp : geom3d_space_expr f) : timestamped_pose3d_expr_has_litlit ts sp := ⟨λt p , timestamped_pose3d_expr.lit_time (|t|) (|p|)⟩ variables {f : geom3d_frame_expr} (sp : geom3d_space_expr f) (pes : pose3d_series_expr ts sp) (te: time_expr ts) (pe:pose3d_expr sp)-- pose3d_series_expr ts sp) #check pes[(te↦pe)] abbreviation pose3d_series_env {tf : time_frame_expr} (ts : time_space_expr tf) {f : geom3d_frame_expr} (sp : geom3d_space_expr f) := pose3d_series_var ts sp → pose3d_discrete ts.value sp.value abbreviation pose3d_series_eval := Π {tf : time_frame_expr} (ts : time_space_expr tf), Π{f : geom3d_frame_expr} (sp : geom3d_space_expr f), pose3d_series_env ts sp → pose3d_series_expr ts sp → pose3d_discrete ts.value sp.value @[simp] noncomputable def default_pose3d_series_env {tf : time_frame_expr} (ts : time_space_expr tf) {f : geom3d_frame_expr} (sp : geom3d_space_expr f) : pose3d_series_env ts sp := λv, @mk_pose3d_discrete_empty tf.value ts.value f.value sp.value --set_option eqn_compiler.max_steps 16384 @[simp] noncomputable def default_pose3d_series_eval : pose3d_series_eval := λtf ts gf gs, λenv_, λexpr_, @mk_pose3d_discrete_empty tf.value ts.value gf.value gs.value @[simp] noncomputable def static_pose3d_series_eval : pose3d_series_eval | tf ts gf gs env_ (pose3d_series_expr.lit p) := p | tf ts gf gs env_ (pose3d_series_expr.update ges te ge) := ((static_pose3d_series_eval ts gs) (default_pose3d_series_env ts gs) ges) .update ⟨te.value,ge.value⟩ | tf ts gf gs env_ (pose3d_series_expr.update_ts ges tse) := ((static_pose3d_series_eval ts gs) (default_pose3d_series_env ts gs) ges) .update tse.value @[simp] noncomputable def pose3d_series_expr.value {tf : time_frame_expr} {ts : time_space_expr tf} {f : geom3d_frame_expr} {sp : geom3d_space_expr f} (expr_ : pose3d_series_expr ts sp) : pose3d_discrete ts.value sp.value := (static_pose3d_series_eval ts sp) (default_pose3d_series_env ts sp) expr_ end lang.series.geom3d
d48f792dc82e3a91179fbc42a592e7807d8b6d02
c777c32c8e484e195053731103c5e52af26a25d1
/src/number_theory/cyclotomic/rat.lean
b7f1fdd8223741e99083d2c4409027254a20cbf9
[ "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
13,651
lean
/- Copyright (c) 2022 Riccardo Brasca. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Riccardo Brasca -/ import number_theory.cyclotomic.discriminant import ring_theory.polynomial.eisenstein.is_integral /-! # Ring of integers of `p ^ n`-th cyclotomic fields We gather results about cyclotomic extensions of `ℚ`. In particular, we compute the ring of integers of a `p ^ n`-th cyclotomic extension of `ℚ`. ## Main results * `is_cyclotomic_extension.rat.is_integral_closure_adjoin_singleton_of_prime_pow`: if `K` is a `p ^ k`-th cyclotomic extension of `ℚ`, then `(adjoin ℤ {ζ})` is the integral closure of `ℤ` in `K`. * `is_cyclotomic_extension.rat.cyclotomic_ring_is_integral_closure_of_prime_pow`: the integral closure of `ℤ` inside `cyclotomic_field (p ^ k) ℚ` is `cyclotomic_ring (p ^ k) ℤ ℚ`. -/ universes u open algebra is_cyclotomic_extension polynomial number_field open_locale cyclotomic number_field nat variables {p : ℕ+} {k : ℕ} {K : Type u} [field K] [char_zero K] {ζ : K} [hp : fact (p : ℕ).prime] include hp namespace is_cyclotomic_extension.rat /-- The discriminant of the power basis given by `ζ - 1`. -/ lemma discr_prime_pow_ne_two' [is_cyclotomic_extension {p ^ (k + 1)} ℚ K] (hζ : is_primitive_root ζ ↑(p ^ (k + 1))) (hk : p ^ (k + 1) ≠ 2) : discr ℚ (hζ.sub_one_power_basis ℚ).basis = (-1) ^ (((p ^ (k + 1) : ℕ).totient) / 2) * p ^ ((p : ℕ) ^ k * ((p - 1) * (k + 1) - 1)) := begin rw [← discr_prime_pow_ne_two hζ (cyclotomic.irreducible_rat (p ^ (k + 1)).pos) hk], exact hζ.discr_zeta_eq_discr_zeta_sub_one.symm end lemma discr_odd_prime' [is_cyclotomic_extension {p} ℚ K] (hζ : is_primitive_root ζ p) (hodd : p ≠ 2) : discr ℚ (hζ.sub_one_power_basis ℚ).basis = (-1) ^ (((p : ℕ) - 1) / 2) * p ^ ((p : ℕ) - 2) := begin rw [← discr_odd_prime hζ (cyclotomic.irreducible_rat hp.out.pos) hodd], exact hζ.discr_zeta_eq_discr_zeta_sub_one.symm end /-- The discriminant of the power basis given by `ζ - 1`. Beware that in the cases `p ^ k = 1` and `p ^ k = 2` the formula uses `1 / 2 = 0` and `0 - 1 = 0`. It is useful only to have a uniform result. See also `is_cyclotomic_extension.rat.discr_prime_pow_eq_unit_mul_pow'`. -/ lemma discr_prime_pow' [is_cyclotomic_extension {p ^ k} ℚ K] (hζ : is_primitive_root ζ ↑(p ^ k)) : discr ℚ (hζ.sub_one_power_basis ℚ).basis = (-1) ^ (((p ^ k : ℕ).totient) / 2) * p ^ ((p : ℕ) ^ (k - 1) * ((p - 1) * k - 1)) := begin rw [← discr_prime_pow hζ (cyclotomic.irreducible_rat (p ^ k).pos)], exact hζ.discr_zeta_eq_discr_zeta_sub_one.symm end /-- If `p` is a prime and `is_cyclotomic_extension {p ^ k} K L`, then there are `u : ℤˣ` and `n : ℕ` such that the discriminant of the power basis given by `ζ - 1` is `u * p ^ n`. Often this is enough and less cumbersome to use than `is_cyclotomic_extension.rat.discr_prime_pow'`. -/ lemma discr_prime_pow_eq_unit_mul_pow' [is_cyclotomic_extension {p ^ k} ℚ K] (hζ : is_primitive_root ζ ↑(p ^ k)) : ∃ (u : ℤˣ) (n : ℕ), discr ℚ (hζ.sub_one_power_basis ℚ).basis = u * p ^ n := begin rw [hζ.discr_zeta_eq_discr_zeta_sub_one.symm], exact discr_prime_pow_eq_unit_mul_pow hζ (cyclotomic.irreducible_rat (p ^ k).pos) end /-- If `K` is a `p ^ k`-th cyclotomic extension of `ℚ`, then `(adjoin ℤ {ζ})` is the integral closure of `ℤ` in `K`. -/ lemma is_integral_closure_adjoin_singleton_of_prime_pow [hcycl : is_cyclotomic_extension {p ^ k} ℚ K] (hζ : is_primitive_root ζ ↑(p ^ k)) : is_integral_closure (adjoin ℤ ({ζ} : set K)) ℤ K := begin refine ⟨subtype.val_injective, λ x, ⟨λ h, ⟨⟨x, _⟩, rfl⟩, _⟩⟩, swap, { rintro ⟨y, rfl⟩, exact is_integral.algebra_map (le_integral_closure_iff_is_integral.1 (adjoin_le_integral_closure (hζ.is_integral (p ^ k).pos)) _) }, let B := hζ.sub_one_power_basis ℚ, have hint : is_integral ℤ B.gen := is_integral_sub (hζ.is_integral (p ^ k).pos) is_integral_one, have H := discr_mul_is_integral_mem_adjoin ℚ hint h, obtain ⟨u, n, hun⟩ := discr_prime_pow_eq_unit_mul_pow' hζ, rw [hun] at H, replace H := subalgebra.smul_mem _ H u.inv, rw [← smul_assoc, ← smul_mul_assoc, units.inv_eq_coe_inv, coe_coe, zsmul_eq_mul, ← int.cast_mul, units.inv_mul, int.cast_one, one_mul, show (p : ℚ) ^ n • x = ((p : ℕ) : ℤ) ^ n • x, by simp [smul_def]] at H, unfreezingI { cases k }, { haveI : is_cyclotomic_extension {1} ℚ K := by simpa using hcycl, have : x ∈ (⊥ : subalgebra ℚ K), { rw [singleton_one ℚ K], exact mem_top }, obtain ⟨y, rfl⟩ := mem_bot.1 this, replace h := (is_integral_algebra_map_iff (algebra_map ℚ K).injective).1 h, obtain ⟨z, hz⟩ := is_integrally_closed.is_integral_iff.1 h, rw [← hz, ← is_scalar_tower.algebra_map_apply], exact subalgebra.algebra_map_mem _ _ }, { have hmin : (minpoly ℤ B.gen).is_eisenstein_at (submodule.span ℤ {((p : ℕ) : ℤ)}), { have h₁ := minpoly.is_integrally_closed_eq_field_fractions' ℚ hint, have h₂ := hζ.minpoly_sub_one_eq_cyclotomic_comp (cyclotomic.irreducible_rat (p ^ _).pos), rw [is_primitive_root.sub_one_power_basis_gen] at h₁, rw [h₁, ← map_cyclotomic_int, show int.cast_ring_hom ℚ = algebra_map ℤ ℚ, by refl, show ((X + 1)) = map (algebra_map ℤ ℚ) (X + 1), by simp, ← map_comp] at h₂, haveI : char_zero ℚ := strict_ordered_semiring.to_char_zero, rw [is_primitive_root.sub_one_power_basis_gen, map_injective (algebra_map ℤ ℚ) ((algebra_map ℤ ℚ).injective_int) h₂], exact cyclotomic_prime_pow_comp_X_add_one_is_eisenstein_at _ _ }, refine adjoin_le _ (mem_adjoin_of_smul_prime_pow_smul_of_minpoly_is_eiseinstein_at (nat.prime_iff_prime_int.1 hp.out) hint h H hmin), simp only [set.singleton_subset_iff, set_like.mem_coe], exact subalgebra.sub_mem _ (self_mem_adjoin_singleton ℤ _) (subalgebra.one_mem _) } end lemma is_integral_closure_adjoin_singleton_of_prime [hcycl : is_cyclotomic_extension {p} ℚ K] (hζ : is_primitive_root ζ ↑p) : is_integral_closure (adjoin ℤ ({ζ} : set K)) ℤ K := begin rw [← pow_one p] at hζ hcycl, exactI is_integral_closure_adjoin_singleton_of_prime_pow hζ, end local attribute [-instance] cyclotomic_field.algebra /-- The integral closure of `ℤ` inside `cyclotomic_field (p ^ k) ℚ` is `cyclotomic_ring (p ^ k) ℤ ℚ`. -/ lemma cyclotomic_ring_is_integral_closure_of_prime_pow : is_integral_closure (cyclotomic_ring (p ^ k) ℤ ℚ) ℤ (cyclotomic_field (p ^ k) ℚ) := begin haveI : char_zero ℚ := strict_ordered_semiring.to_char_zero, haveI : is_cyclotomic_extension {p ^ k} ℚ (cyclotomic_field (p ^ k) ℚ), { convert cyclotomic_field.is_cyclotomic_extension (p ^ k) _, { exact subsingleton.elim _ _ }, { exact ne_zero.char_zero } }, have hζ := zeta_spec (p ^ k) ℚ (cyclotomic_field (p ^ k) ℚ), refine ⟨is_fraction_ring.injective _ _, λ x, ⟨λ h, ⟨⟨x, _⟩, rfl⟩, _⟩⟩, { have := (is_integral_closure_adjoin_singleton_of_prime_pow hζ).is_integral_iff, obtain ⟨y, rfl⟩ := this.1 h, convert adjoin_mono _ y.2, { simp only [eq_iff_true_of_subsingleton] }, { simp only [eq_iff_true_of_subsingleton] }, { simp only [pnat.pow_coe, set.singleton_subset_iff, set.mem_set_of_eq], exact hζ.pow_eq_one } }, { haveI : is_cyclotomic_extension {p ^ k} ℤ (cyclotomic_ring (p ^ k) ℤ ℚ), { convert cyclotomic_ring.is_cyclotomic_extension _ ℤ ℚ, { exact subsingleton.elim _ _ }, { exact ne_zero.char_zero } }, rintro ⟨y, rfl⟩, exact is_integral.algebra_map ((is_cyclotomic_extension.integral {p ^ k} ℤ _) _) } end lemma cyclotomic_ring_is_integral_closure_of_prime : is_integral_closure (cyclotomic_ring p ℤ ℚ) ℤ (cyclotomic_field p ℚ) := begin rw [← pow_one p], exact cyclotomic_ring_is_integral_closure_of_prime_pow end end is_cyclotomic_extension.rat section power_basis open is_cyclotomic_extension.rat namespace is_primitive_root /-- The algebra isomorphism `adjoin ℤ {ζ} ≃ₐ[ℤ] (𝓞 K)`, where `ζ` is a primitive `p ^ k`-th root of unity and `K` is a `p ^ k`-th cyclotomic extension of `ℚ`. -/ @[simps] noncomputable def _root_.is_primitive_root.adjoin_equiv_ring_of_integers [hcycl : is_cyclotomic_extension {p ^ k} ℚ K] (hζ : is_primitive_root ζ ↑(p ^ k)) : adjoin ℤ ({ζ} : set K) ≃ₐ[ℤ] (𝓞 K) := let _ := is_integral_closure_adjoin_singleton_of_prime_pow hζ in by exactI (is_integral_closure.equiv ℤ (adjoin ℤ ({ζ} : set K)) K (𝓞 K)) /-- The ring of integers of a `p ^ k`-th cyclotomic extension of `ℚ` is a cyclotomic extension. -/ instance _root_.is_cyclotomic_extension.ring_of_integers [is_cyclotomic_extension {p ^ k} ℚ K] : is_cyclotomic_extension {p ^ k} ℤ (𝓞 K) := let _ := (zeta_spec (p ^ k) ℚ K).adjoin_is_cyclotomic_extension ℤ in by exactI is_cyclotomic_extension.equiv _ ℤ _ ((zeta_spec (p ^ k) ℚ K).adjoin_equiv_ring_of_integers) /-- The integral `power_basis` of `𝓞 K` given by a primitive root of unity, where `K` is a `p ^ k` cyclotomic extension of `ℚ`. -/ noncomputable def integral_power_basis [hcycl : is_cyclotomic_extension {p ^ k} ℚ K] (hζ : is_primitive_root ζ ↑(p ^ k)) : power_basis ℤ (𝓞 K) := (adjoin.power_basis' (hζ.is_integral (p ^ k).pos)).map hζ.adjoin_equiv_ring_of_integers @[simp] lemma integral_power_basis_gen [hcycl : is_cyclotomic_extension {p ^ k} ℚ K] (hζ : is_primitive_root ζ ↑(p ^ k)) : hζ.integral_power_basis.gen = ⟨ζ, hζ.is_integral (p ^ k).pos⟩ := subtype.ext $ show algebra_map _ K hζ.integral_power_basis.gen = _, by simpa [integral_power_basis] @[simp] lemma integral_power_basis_dim [hcycl : is_cyclotomic_extension {p ^ k} ℚ K] (hζ : is_primitive_root ζ ↑(p ^ k)) : hζ.integral_power_basis.dim = φ (p ^ k) := by simp [integral_power_basis, ←cyclotomic_eq_minpoly hζ, nat_degree_cyclotomic] /-- The algebra isomorphism `adjoin ℤ {ζ} ≃ₐ[ℤ] (𝓞 K)`, where `ζ` is a primitive `p`-th root of unity and `K` is a `p`-th cyclotomic extension of `ℚ`. -/ @[simps] noncomputable def _root_.is_primitive_root.adjoin_equiv_ring_of_integers' [hcycl : is_cyclotomic_extension {p} ℚ K] (hζ : is_primitive_root ζ p) : adjoin ℤ ({ζ} : set K) ≃ₐ[ℤ] (𝓞 K) := @adjoin_equiv_ring_of_integers p 1 K _ _ _ _ (by { convert hcycl, rw pow_one }) (by rwa pow_one) /-- The ring of integers of a `p`-th cyclotomic extension of `ℚ` is a cyclotomic extension. -/ instance _root_.is_cyclotomic_extension.ring_of_integers' [is_cyclotomic_extension {p} ℚ K] : is_cyclotomic_extension {p} ℤ (𝓞 K) := let _ := (zeta_spec p ℚ K).adjoin_is_cyclotomic_extension ℤ in by exactI is_cyclotomic_extension.equiv _ ℤ _ ((zeta_spec p ℚ K).adjoin_equiv_ring_of_integers') /-- The integral `power_basis` of `𝓞 K` given by a primitive root of unity, where `K` is a `p`-th cyclotomic extension of `ℚ`. -/ noncomputable def integral_power_basis' [hcycl : is_cyclotomic_extension {p} ℚ K] (hζ : is_primitive_root ζ p) : power_basis ℤ (𝓞 K) := @integral_power_basis p 1 K _ _ _ _ (by { convert hcycl, rw pow_one }) (by rwa pow_one) @[simp] lemma integral_power_basis'_gen [hcycl : is_cyclotomic_extension {p} ℚ K] (hζ : is_primitive_root ζ p) : hζ.integral_power_basis'.gen = ⟨ζ, hζ.is_integral p.pos⟩ := @integral_power_basis_gen p 1 K _ _ _ _ (by { convert hcycl, rw pow_one }) (by rwa pow_one) @[simp] lemma power_basis_int'_dim [hcycl : is_cyclotomic_extension {p} ℚ K] (hζ : is_primitive_root ζ p) : hζ.integral_power_basis'.dim = φ p := by erw [@integral_power_basis_dim p 1 K _ _ _ _ (by { convert hcycl, rw pow_one }) (by rwa pow_one), pow_one] /-- The integral `power_basis` of `𝓞 K` given by `ζ - 1`, where `K` is a `p ^ k` cyclotomic extension of `ℚ`. -/ noncomputable def sub_one_integral_power_basis [is_cyclotomic_extension {p ^ k} ℚ K] (hζ : is_primitive_root ζ ↑(p ^ k)) : power_basis ℤ (𝓞 K) := power_basis.of_gen_mem_adjoin' hζ.integral_power_basis (is_integral_of_mem_ring_of_integers $ subalgebra.sub_mem _ (hζ.is_integral (p ^ k).pos) (subalgebra.one_mem _)) begin simp only [integral_power_basis_gen], convert subalgebra.add_mem _ (self_mem_adjoin_singleton ℤ (⟨ζ - 1, _⟩ : 𝓞 K)) (subalgebra.one_mem _), simp end @[simp] lemma sub_one_integral_power_basis_gen [is_cyclotomic_extension {p ^ k} ℚ K] (hζ : is_primitive_root ζ ↑(p ^ k)) : hζ.sub_one_integral_power_basis.gen = ⟨ζ - 1, subalgebra.sub_mem _ (hζ.is_integral (p ^ k).pos) (subalgebra.one_mem _)⟩ := by simp [sub_one_integral_power_basis] /-- The integral `power_basis` of `𝓞 K` given by `ζ - 1`, where `K` is a `p`-th cyclotomic extension of `ℚ`. -/ noncomputable def sub_one_integral_power_basis' [hcycl : is_cyclotomic_extension {p} ℚ K] (hζ : is_primitive_root ζ p) : power_basis ℤ (𝓞 K) := @sub_one_integral_power_basis p 1 K _ _ _ _ (by { convert hcycl, rw pow_one }) (by rwa pow_one) @[simp] lemma sub_one_integral_power_basis'_gen [hcycl : is_cyclotomic_extension {p} ℚ K] (hζ : is_primitive_root ζ p) : hζ.sub_one_integral_power_basis'.gen = ⟨ζ - 1, subalgebra.sub_mem _ (hζ.is_integral p.pos) (subalgebra.one_mem _)⟩ := @sub_one_integral_power_basis_gen p 1 K _ _ _ _ (by { convert hcycl, rw pow_one }) (by rwa pow_one) end is_primitive_root end power_basis
b2271045d6a8c5f6c4096b7dab65284895954ea6
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/955.lean
a4169f7bc976afecafc6b6411ad5c2b968120be2
[ "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
34,597
lean
-*- mode: compilation; default-directory: "~/projects/lean/tests/lean/run/" -*- Compilation started at Thu Feb 4 14:53:07 /home/leo/projects/lean/bin/lean -Dpp.width=120 /home/leo/projects/lean/tests/lean/run/955.lean /home/leo/projects/lean/tests/lean/run/955.lean:31:32: error: type mismatch at definition 'append_auxH', has type @vectorH.{?M_1} (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (… …)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) ?M_4 but is expected to have type @vectorH.{?M_1} (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (… …)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) (@vector.append_aux.{?M_1+1} Type.{?M_1} nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (nat.addl nat.zero (… …)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) (@vector.nil.{?M_1+1} Type.{?M_1}) ?M_3) The following identifier(s) are introduced as free variables by the left-hand-side of the equation: v' Compilation exited abnormally with code 1 at Thu Feb 4 14:53:09
4a9bddd8bfeeaab4ca4852ee357dc46c25c55fd7
57aec6ee746bc7e3a3dd5e767e53bd95beb82f6d
/stage0/src/Lean/Meta.lean
5d453b3a1095cbe1a2eeaf8ed5d7f8ece8764c13
[ "Apache-2.0" ]
permissive
collares/lean4
861a9269c4592bce49b71059e232ff0bfe4594cc
52a4f535d853a2c7c7eea5fee8a4fa04c682c1ee
refs/heads/master
1,691,419,031,324
1,618,678,138,000
1,618,678,138,000
358,989,750
0
0
Apache-2.0
1,618,696,333,000
1,618,696,333,000
null
UTF-8
Lean
false
false
995
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.LevelDefEq import Lean.Meta.WHNF import Lean.Meta.InferType import Lean.Meta.FunInfo import Lean.Meta.ExprDefEq import Lean.Meta.DiscrTree import Lean.Meta.Reduce import Lean.Meta.Instances import Lean.Meta.AbstractMVars import Lean.Meta.SynthInstance import Lean.Meta.AppBuilder import Lean.Meta.Tactic import Lean.Meta.KAbstract import Lean.Meta.RecursorInfo import Lean.Meta.GeneralizeTelescope import Lean.Meta.Match import Lean.Meta.ReduceEval import Lean.Meta.Closure import Lean.Meta.AbstractNestedProofs import Lean.Meta.ForEachExpr import Lean.Meta.Transform import Lean.Meta.PPGoal import Lean.Meta.UnificationHint import Lean.Meta.Inductive import Lean.Meta.SizeOf import Lean.Meta.Coe import Lean.Meta.SortLocalDecls import Lean.Meta.CollectFVars import Lean.Meta.GeneralizeVars
5ec90a55ca2e1110d2c012722e0d8c78afbc9f80
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/439.lean
d50a96ba50732f6b02479b965459d3f6942a82ff
[ "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
770
lean
universe u structure Fn (E I : Sort u) := (exp : E) (imp : I) instance (E I : Sort u) : CoeFun (Fn E I) (fun _ => I) := {coe := fun K => K.imp} class Bar.{w} (P : Sort u) := fn : P -> Sort w variable {P : Sort u} (B : Bar P) variable (fn : Fn ((p : P) -> B.fn p) ({p : P} -> B.fn p)) #check (@fn : {p : P} → Bar.fn p) -- Result is as expected (implicit) /- fn.imp : {p : P} → Bar.fn p -/ variable (p : P) variable (Bp : Bar.fn p) #check fn Bp /- application type mismatch Fn.imp fn Bp argument Bp has type Bar.fn p : Sort ?u.635 but is expected to have type P : Sort u -/ #check fn p #check fn (p := p) variable (fn' : Fn ((p : P) -> B.fn p -> B.fn p) ({p : P} -> B.fn p -> B.fn p)) -- Expect no error #check fn' Bp -- Expect error #check fn' p
6c17df5c87beaa7d6a4ac1ee5828c9ccbd316f25
f5f7e6fae601a5fe3cac7cc3ed353ed781d62419
/src/analysis/normed_space/operator_norm.lean
a847a757148a1ebf794bba609c4b2b45f1b2d0fe
[ "Apache-2.0" ]
permissive
EdAyers/mathlib
9ecfb2f14bd6caad748b64c9c131befbff0fb4e0
ca5d4c1f16f9c451cf7170b10105d0051db79e1b
refs/heads/master
1,626,189,395,845
1,555,284,396,000
1,555,284,396,000
144,004,030
0
0
Apache-2.0
1,533,727,664,000
1,533,727,663,000
null
UTF-8
Lean
false
false
7,468
lean
/- Copyright (c) 2019 Jan-David Salchow. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jan-David Salchow, Sébastien Gouëzel The space of bounded linear maps Define the set of bounded linear maps between normed spaces and show basic facts about it. In particular (*) define a set L(E,F) of bounded linear maps between k normed spaces, (*) show that L(E,F) is a vector subspace of E → F, (*) define the 'operator norm' on L(E,F) and show that it induces the structure of a normed space on L(E,F). -/ import algebra.module import analysis.normed_space.bounded_linear_maps variables {k : Type*} variables {E : Type*} {F : Type*} -- Define the subspace of bounded linear maps. section bounded_linear_maps set_option class.instance_max_depth 50 variables [hnfk : normed_field k] [normed_space k E] [normed_space k F] include hnfk def bounded_linear_maps : subspace k (E → F) := { carrier := {A : E → F | is_bounded_linear_map k A}, zero := is_bounded_linear_map.zero, add := assume A B, is_bounded_linear_map.add, smul := assume c A, is_bounded_linear_map.smul c } -- introduce the local notation L(E,F) for the set of bounded linear maps. local notation `L(` E `,` F `)` := @bounded_linear_maps k E F _ _ _ /-- Coerce bounded linear maps to functions. -/ instance bounded_linear_maps.to_fun : has_coe_to_fun $ L(E,F) := {F := λ _, (E → F), coe := (λ f, f.val)} @[extensionality] theorem ext {A B : L(E,F)} (H : ∀ x, A x = B x) : A = B := set_coe.ext $ funext H def to_linear_map (A : L(E, F)) : linear_map _ E F := {to_fun := A.val, ..A.property} @[simp] lemma bounded_linear_maps.map_zero {A : L(E, F)} : A (0 : E) = 0 := (to_linear_map A).map_zero @[simp] lemma bounded_linear_maps.coe_zero {x : E} : (0 : L(E, F)) x = 0 := rfl @[simp] lemma bounded_linear_maps.smul_coe {A : L(E, F)} {x : E} {c : k} : (c • A) x = c • (A x) := rfl @[simp] lemma bounded_linear_maps.zero_smul {A : L(E, F)} : (0 : k) • A = 0 := by ext; simp @[simp] lemma bounded_linear_maps.one_smul {A : L(E, F)} : (1 : k) • A = A := by ext; simp /-- Bounded linear maps are ... bounded -/ lemma exists_bound (A : L(E,F)) : ∃ c, 0 < c ∧ ∀ x : E, ∥A x∥ ≤ c * ∥x∥ := A.property.bound end bounded_linear_maps /- Now define the operator norm. The main task is to show that the operator norm is definite, homogeneous, and satisfies the triangle inequality. This is done after a few preliminary lemmas necessary to deal with csupr. -/ section operator_norm variables [normed_field k] [normed_space k E] [normed_space k F] open lattice set local notation `L(` E `,` F `)` := @bounded_linear_maps k E F _ _ _ /-- The operator norm of a bounded linear map A : E → F is the sup of ∥A x∥/∥x∥. -/ noncomputable def operator_norm (A : L(E, F)) : ℝ := supr (λ x, ∥A x∥/∥x∥) noncomputable instance bounded_linear_maps.to_has_norm : has_norm L(E,F) := {norm := operator_norm} lemma bdd_above_range_norm_image_div_norm (A : L(E,F)) : bdd_above (range (λ x, ∥A x∥/∥x∥)) := let ⟨c, _, H⟩ := (exists_bound A : ∃ c, 0 < c ∧ ∀ x : E, ∥A x∥ ≤ c * ∥x∥) in bdd_above.mk c $ forall_range_iff.2 $ λx, begin by_cases h : ∥x∥ = 0, { simp [h, le_of_lt ‹0 < c›] }, { refine (div_le_iff _).2 (H x), simpa [ne.symm h] using le_iff_eq_or_lt.1 (norm_nonneg x) } end lemma operator_norm_nonneg (A : L(E,F)) : 0 ≤ ∥A∥ := begin have : (0 : ℝ) = (λ x, ∥A x∥/∥x∥) 0, by simp, rw this, exact le_csupr (bdd_above_range_norm_image_div_norm A), end set_option class.instance_max_depth 34 /-- This is the fundamental property of the operator norm: ∥A x∥ ≤ ∥A∥ * ∥x∥. -/ theorem bounded_by_operator_norm {A : L(E,F)} {x : E} : ∥A x∥ ≤ ∥A∥ * ∥x∥ := begin classical, by_cases h : x = 0, { simp [h] }, { have : ∥A x∥/∥x∥ ≤ ∥A∥, from le_csupr (bdd_above_range_norm_image_div_norm A), rwa (div_le_iff _) at this, have : ∥x∥ ≠ 0, from ne_of_gt ((norm_pos_iff x).mpr h), have I := le_iff_eq_or_lt.1 (norm_nonneg x), simpa [ne.symm this] using I } end /-- If one controls the norm of every A x, then one controls the norm of A. -/ lemma operator_norm_bounded_by {A : L(E,F)} {c : ℝ} (hc : 0 ≤ c) (H : ∀ x, ∥A x∥ ≤ c * ∥x∥) : ∥A∥ ≤ c := begin haveI : nonempty E := ⟨0⟩, refine csupr_le (λx, _), by_cases h : ∥x∥ = 0, { simp [h, hc] }, { refine (div_le_iff _).2 (H x), simpa [ne.symm h] using le_iff_eq_or_lt.1 (norm_nonneg x) } end /-- The operator norm satisfies the triangle inequality. -/ theorem operator_norm_triangle (A B : L(E,F)) : ∥A + B∥ ≤ ∥A∥ + ∥B∥ := operator_norm_bounded_by (add_nonneg (operator_norm_nonneg A) (operator_norm_nonneg B)) (assume x, calc ∥(A + B) x∥ = ∥A x + B x∥ : by refl ... ≤ ∥A x∥ + ∥B x∥ : by exact norm_triangle _ _ ... ≤ ∥A∥ * ∥x∥ + ∥B∥ * ∥x∥ : add_le_add bounded_by_operator_norm bounded_by_operator_norm ... = (∥A∥ + ∥B∥) * ∥x∥ : by ring) /-- An operator is zero iff its norm vanishes. -/ theorem operator_norm_zero_iff (A : L(E,F)) : ∥A∥ = 0 ↔ A = 0 := iff.intro (assume : ∥A∥ = 0, suffices ∀ x, A x = 0, from ext this, assume x, have ∥A x∥ ≤ 0, from calc ∥A x∥ ≤ ∥A∥ * ∥x∥ : bounded_by_operator_norm ... = 0 : by rw[‹∥A∥ = 0›]; ring, (norm_le_zero_iff (A x)).mp this) (assume : A = 0, have ∥A∥ ≤ 0, from operator_norm_bounded_by (le_refl 0) $ by rw this; simp [le_refl], le_antisymm this (operator_norm_nonneg A)) section instance_70 -- one needs to increase the max_depth for the following proof set_option class.instance_max_depth 70 /-- Auxiliary lemma to prove that the operator norm is homogeneous. -/ lemma operator_norm_homogeneous_le (c : k) (A : L(E, F)) : ∥c • A∥ ≤ ∥c∥ * ∥A∥ := begin apply operator_norm_bounded_by (mul_nonneg (norm_nonneg _) (operator_norm_nonneg _)) (λx, _), erw [norm_smul _ _, mul_assoc], exact mul_le_mul_of_nonneg_left bounded_by_operator_norm (norm_nonneg _) end theorem operator_norm_homogeneous (c : k) (A : L(E, F)) : ∥c • A∥ = ∥c∥ * ∥A∥ := begin refine le_antisymm (operator_norm_homogeneous_le c A) _, by_cases h : ∥c∥ = 0, { have : c = 0, from (norm_eq_zero _).1 h, have I : ∥(0 : L(E, F))∥ = 0, by rw operator_norm_zero_iff, simp only [h], simp [this, I] }, { have h' : c ≠ 0, by simpa [norm_eq_zero] using h, have : ∥A∥ ≤ ∥c • A∥ / ∥c∥, from calc ∥A∥ = ∥(1 : k) • A∥ : by rw bounded_linear_maps.one_smul ... = ∥c⁻¹ • c • A∥ : by rw [smul_smul, inv_mul_cancel h'] ... ≤ ∥c⁻¹∥ * ∥c • A∥ : operator_norm_homogeneous_le _ _ ... = ∥c • A∥ / ∥c∥ : by rw [norm_inv, div_eq_inv_mul], rwa [le_div_iff, mul_comm] at this, simpa [ne.symm h] using le_iff_eq_or_lt.1 (norm_nonneg c) } end end instance_70 /-- Expose L(E,F) equipped with the operator norm as normed space. -/ noncomputable instance bounded_linear_maps.to_normed_space : normed_space k L(E,F) := normed_space.of_core k L(E,F) { norm_eq_zero_iff := operator_norm_zero_iff, norm_smul := operator_norm_homogeneous, triangle := operator_norm_triangle } end operator_norm
ed904f0a83ca0a73bf4f99f3f3bf1fe4833c8026
de91c42b87530c3bdcc2b138ef1a3c3d9bee0d41
/old/eval/geometryEval.lean
42b57a7fd52b85ac282ce9a58efb5470d3b09fe3
[]
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
2,471
lean
import ..imperative_DSL.environment open lang.euclideanGeometry3 attribute [reducible] def euclideanGeometry3Eval : lang.euclideanGeometry3.spaceExpr → environment.env → euclideanGeometry3 | (lang.euclideanGeometry3.spaceExpr.lit V) i := V | (lang.euclideanGeometry3.spaceExpr.var v) i := i.g.sp v attribute [reducible] def euclideanGeometry3FrameEval : lang.euclideanGeometry3.frameExpr → environment.env → euclideanGeometry3Frame | (lang.euclideanGeometry3.frameExpr.lit V) i := V | (lang.euclideanGeometry3.frameExpr.var v) i := i.g.fr v attribute [reducible] def euclideanGeometry3TransformEval : lang.euclideanGeometry3.TransformExpr → environment.env → euclideanGeometry3Transform | (lang.euclideanGeometry3.TransformExpr.lit V) i := V | (lang.euclideanGeometry3.TransformExpr.var v) i := i.g.tr v attribute [reducible] def euclideanGeometry3ScalarEval : lang.euclideanGeometry3.ScalarExpr → environment.env → euclideanGeometry3Scalar | (lang.euclideanGeometry3.ScalarExpr.lit V) i := V | (lang.euclideanGeometry3.ScalarExpr.var v) i := i.g.s v attribute [reducible] def euclideanGeometry3AngleEval : lang.euclideanGeometry3.AngleExpr → environment.env → euclideanGeometry3Angle | (lang.euclideanGeometry3.AngleExpr.lit V) i := V | (lang.euclideanGeometry3.AngleExpr.var v) i := i.g.a v attribute [reducible] def euclideanGeometry3OrientationEval : lang.euclideanGeometry3.OrientationExpr → environment.env → euclideanGeometry3Orientation | (lang.euclideanGeometry3.OrientationExpr.lit V) i := V | (lang.euclideanGeometry3.OrientationExpr.var v) i := i.g.or v attribute [reducible] def euclideanGeometry3RotationEval : lang.euclideanGeometry3.RotationExpr → environment.env → euclideanGeometry3Rotation | (lang.euclideanGeometry3.RotationExpr.lit V) i := V | (lang.euclideanGeometry3.RotationExpr.var v) i := i.g.r v attribute [reducible] def euclideanGeometry3CoordinateVectorEval : lang.euclideanGeometry3.CoordinateVectorExpr → environment.env → euclideanGeometry3CoordinateVector | (lang.euclideanGeometry3.CoordinateVectorExpr.lit V) i := V | (lang.euclideanGeometry3.CoordinateVectorExpr.var v) i := i.g.vec v attribute [reducible] def euclideanGeometry3CoordinatePointEval : lang.euclideanGeometry3.CoordinatePointExpr → environment.env → euclideanGeometry3CoordinatePoint | (lang.euclideanGeometry3.CoordinatePointExpr.lit V) i := V | (lang.euclideanGeometry3.CoordinatePointExpr.var v) i := i.g.pt v
f77a229052a88c419c06959ce2de6e669ec36763
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/well_founded_tactics.lean
f4fbe7b94b21cd5e3d67c6c53ebc92d8f7dac209
[ "Apache-2.0" ]
permissive
leanprover-community/lean
12b87f69d92e614daea8bcc9d4de9a9ace089d0e
cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0
refs/heads/master
1,687,508,156,644
1,684,951,104,000
1,684,951,104,000
169,960,991
457
107
Apache-2.0
1,686,744,372,000
1,549,790,268,000
C++
UTF-8
Lean
false
false
397
lean
variables {α : Type} {lt : α → α → Prop} [decidable_rel lt] def merge : list α → list α → list α | [] l' := l' | l [] := l | (a :: l) (b :: l') := if lt a b then a :: merge l (b :: l') else b :: merge (a :: l) l' def foo : Π (s : ℕ), ℕ | 0 := 1 | (nat.succ s) := foo s + 1 using_well_founded { rel_tac := λ _ _, `[exact ⟨_, measure_wf id⟩] }
5c4bb3b37a1778f50818cfc8881aba03eca6735c
1dd0001f48991684032999e18f88be3ece4261d7
/src/instructor/review_9_13_1.lean
f0690d70efd2bbaf2e33d735b7cd09175edc0c8d
[]
no_license
rayouness/cs2120f21
e0a3fa89a6ac50c8a83e85bd7795a2710aceb7e2
5dfa57f111ec7f322474be1c87992fe1fedd6610
refs/heads/main
1,690,957,987,242
1,632,333,173,000
1,632,333,173,000
399,947,440
0
0
null
null
null
null
UTF-8
Lean
false
false
854
lean
axioms (P Q : Prop) def if_P_is_true_then_so_is_Q : Prop := P → Q axiom p: P -- assume P is true -- assume we have a proof of P (p) axiom pq : P → Q -- assume that we have a proof, pq, of P → Q --intro for implies : assume premise , show conclusion -- elimination rule for implies: #check pq #check p #check (pq p) /- suppose P and Q are propositions and you know that P → Q and that P are both true show that Q is true proof : applaying the truth of P→ Q to the truth of p to dreive the truth of Q. ptoof : by the elimination tule for → with pq applied to p. proof : by "modues ponones ". QED -/ /- FORALL -/ namespace all axiom (T : Type ) (p : T → Prop) (t:T) (a:∀ (x:T),P x) -- does t have proprety P? a t: P t #check a t end all /- AND & → A-/ axiom (P Q : Prop) /- want a proof of P^Q -/
404b96823ed82b10767b33636735f44c4f995f73
b328e8ebb2ba923140e5137c83f09fa59516b793
/stage0/src/Lean/Elab/SyntheticMVars.lean
009890033c0a751d5fd97353c4daffeb7c9544f4
[ "Apache-2.0" ]
permissive
DrMaxis/lean4
a781bcc095511687c56ab060e816fd948553e162
5a02c4facc0658aad627cfdcc3db203eac0cb544
refs/heads/master
1,677,051,517,055
1,611,876,226,000
1,611,876,226,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
15,494
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.Util.ForEachExpr import Lean.Elab.Term import Lean.Elab.Tactic.Basic namespace Lean.Elab.Term open Tactic (TacticM evalTactic getUnsolvedGoals) open Meta def liftTacticElabM {α} (mvarId : MVarId) (x : TacticM α) : TermElabM α := withMVarContext mvarId do let s ← get let savedSyntheticMVars := s.syntheticMVars modify fun s => { s with syntheticMVars := [] } try x.run' { main := mvarId } { goals := [mvarId] } finally modify fun s => { s with syntheticMVars := savedSyntheticMVars } def runTactic (mvarId : MVarId) (tacticCode : Syntax) : TermElabM Unit := do /- Recall, `tacticCode` is the whole `by ...` expression. We store the `by` because in the future we want to save the initial state information at the `by` position. -/ let code := tacticCode[1] modifyThe Meta.State fun s => { s with mctx := s.mctx.instantiateMVarDeclMVars mvarId } let remainingGoals ← withInfoHole mvarId do liftTacticElabM mvarId do evalTactic code; getUnsolvedGoals unless remainingGoals.isEmpty do reportUnsolvedGoals remainingGoals /-- Auxiliary function used to implement `synthesizeSyntheticMVars`. -/ private def resumeElabTerm (stx : Syntax) (expectedType? : Option Expr) (errToSorry := true) : TermElabM Expr := -- Remark: if `ctx.errToSorry` is already false, then we don't enable it. Recall tactics disable `errToSorry` withReader (fun ctx => { ctx with errToSorry := ctx.errToSorry && errToSorry }) do elabTerm stx expectedType? false /-- Try to elaborate `stx` that was postponed by an elaboration method using `Expection.postpone`. It returns `true` if it succeeded, and `false` otherwise. It is used to implement `synthesizeSyntheticMVars`. -/ private def resumePostponed (macroStack : MacroStack) (declName? : Option Name) (stx : Syntax) (mvarId : MVarId) (postponeOnError : Bool) : TermElabM Bool := withRef stx $ withMVarContext mvarId do let s ← get try withReader (fun ctx => { ctx with macroStack := macroStack, declName? := declName? }) do let mvarDecl ← getMVarDecl mvarId let expectedType ← instantiateMVars mvarDecl.type withInfoHole mvarId do let result ← resumeElabTerm stx expectedType (!postponeOnError) /- We must ensure `result` has the expected type because it is the one expected by the method that postponed stx. That is, the method does not have an opportunity to check whether `result` has the expected type or not. -/ let result ← withRef stx $ ensureHasType expectedType result assignExprMVar mvarId result pure true catch | ex@(Exception.internal id _) => if id == postponeExceptionId then set s pure false else throw ex | ex@(Exception.error _ _) => if postponeOnError then set s; pure false else logException ex pure true /-- Similar to `synthesizeInstMVarCore`, but makes sure that `instMVar` local context and instances are used. It also logs any error message produced. -/ private def synthesizePendingInstMVar (instMVar : MVarId) : TermElabM Bool := withMVarContext instMVar do try synthesizeInstMVarCore instMVar catch | ex@(Exception.error _ _) => logException ex; pure true | _ => unreachable! /-- Similar to `synthesizePendingInstMVar`, but generates type mismatch error message. -/ private def synthesizePendingCoeInstMVar (instMVar : MVarId) (errorMsgHeader? : Option String) (expectedType : Expr) (eType : Expr) (e : Expr) (f? : Option Expr) : TermElabM Bool := withMVarContext instMVar do try synthesizeCoeInstMVarCore instMVar catch | Exception.error _ msg => throwTypeMismatchError errorMsgHeader? expectedType eType e f? msg | _ => unreachable! /-- Try to synthesize the given pending synthetic metavariable. -/ private def synthesizeSyntheticMVar (mvarSyntheticDecl : SyntheticMVarDecl) (postponeOnError : Bool) (runTactics : Bool) : TermElabM Bool := withRef mvarSyntheticDecl.stx do match mvarSyntheticDecl.kind with | SyntheticMVarKind.typeClass => synthesizePendingInstMVar mvarSyntheticDecl.mvarId | SyntheticMVarKind.coe header? expectedType eType e f? => synthesizePendingCoeInstMVar mvarSyntheticDecl.mvarId header? expectedType eType e f? -- NOTE: actual processing at `synthesizeSyntheticMVarsAux` | SyntheticMVarKind.postponed macroStack declName? => resumePostponed macroStack declName? mvarSyntheticDecl.stx mvarSyntheticDecl.mvarId postponeOnError | SyntheticMVarKind.tactic declName? tacticCode => withReader (fun ctx => { ctx with declName? := declName? }) do if runTactics then runTactic mvarSyntheticDecl.mvarId tacticCode pure true else pure false /-- Try to synthesize the current list of pending synthetic metavariables. Return `true` if at least one of them was synthesized. -/ private def synthesizeSyntheticMVarsStep (postponeOnError : Bool) (runTactics : Bool) : TermElabM Bool := do let ctx ← read traceAtCmdPos `Elab.resuming fun _ => m!"resuming synthetic metavariables, mayPostpone: {ctx.mayPostpone}, postponeOnError: {postponeOnError}" let s ← get let syntheticMVars := s.syntheticMVars let numSyntheticMVars := syntheticMVars.length -- We reset `syntheticMVars` because new synthetic metavariables may be created by `synthesizeSyntheticMVar`. modify fun s => { s with syntheticMVars := [] } -- Recall that `syntheticMVars` is a list where head is the most recent pending synthetic metavariable. -- We use `filterRevM` instead of `filterM` to make sure we process the synthetic metavariables using the order they were created. -- It would not be incorrect to use `filterM`. let remainingSyntheticMVars ← syntheticMVars.filterRevM fun mvarDecl => do -- We use `traceM` because we want to make sure the metavar local context is used to trace the message traceM `Elab.postpone (withMVarContext mvarDecl.mvarId do addMessageContext m!"resuming {mkMVar mvarDecl.mvarId}") let succeeded ← synthesizeSyntheticMVar mvarDecl postponeOnError runTactics trace[Elab.postpone]! if succeeded then fmt "succeeded" else fmt "not ready yet" pure !succeeded -- Merge new synthetic metavariables with `remainingSyntheticMVars`, i.e., metavariables that still couldn't be synthesized modify fun s => { s with syntheticMVars := s.syntheticMVars ++ remainingSyntheticMVars } pure $ numSyntheticMVars != remainingSyntheticMVars.length private def tryToSynthesizeUsingDefaultInstance (mvarId : MVarId) (defaultInstance : Name) : MetaM (Option (List SyntheticMVarDecl)) := commitWhenSome? do let constInfo ← getConstInfo defaultInstance let candidate := Lean.mkConst defaultInstance (← mkFreshLevelMVars constInfo.lparams.length) let (mvars, bis, _) ← forallMetaTelescopeReducing (← inferType candidate) let candidate := mkAppN candidate mvars trace[Elab.resume]! "trying default instance for {mkMVar mvarId} := {candidate}" if (← isDefEqGuarded (mkMVar mvarId) candidate) then -- Succeeded. Collect new TC problems let mut result := [] for i in [:bis.size] do if bis[i] == BinderInfo.instImplicit then result := { mvarId := mvars[i].mvarId!, stx := (← getRef), kind := SyntheticMVarKind.typeClass } :: result trace[Elab.resume]! "worked" return some result else return none private def tryToSynthesizeUsingDefaultInstances (mvarId : MVarId) (prio : Nat) : MetaM (Option (List SyntheticMVarDecl)) := withMVarContext mvarId do let mvarType := (← Meta.getMVarDecl mvarId).type match (← isClass? mvarType) with | none => return none | some className => match (← getDefaultInstances className) with | [] => return none | defaultInstances => for (defaultInstance, instPrio) in defaultInstances do if instPrio == prio then match (← tryToSynthesizeUsingDefaultInstance mvarId defaultInstance) with | some newMVarDecls => return some newMVarDecls | none => continue return none /- Used to implement `synthesizeUsingDefault`. This method only consider default instances with the given priority. -/ private def synthesizeUsingDefaultPrio (prio : Nat) : TermElabM Bool := do let rec visit (syntheticMVars : List SyntheticMVarDecl) (syntheticMVarsNew : List SyntheticMVarDecl) : TermElabM Bool := do match syntheticMVars with | [] => return false | mvarDecl :: mvarDecls => match mvarDecl.kind with | SyntheticMVarKind.typeClass => match (← withRef mvarDecl.stx <| tryToSynthesizeUsingDefaultInstances mvarDecl.mvarId prio) with | none => visit mvarDecls (mvarDecl :: syntheticMVarsNew) | some newMVarDecls => let syntheticMVarsNew := newMVarDecls ++ syntheticMVarsNew let syntheticMVarsNew := mvarDecls.reverse ++ syntheticMVarsNew modify fun s => { s with syntheticMVars := syntheticMVarsNew } return true | _ => visit mvarDecls (mvarDecl :: syntheticMVarsNew) /- Recall that s.syntheticMVars is essentially a stack. The first metavariable was the last one created. We want to apply the default instance in reverse creation order. Otherwise, `toString 0` will produce a `OfNat String _` cannot be synthesized error. -/ visit (← get).syntheticMVars.reverse [] /-- Apply default value to any pending synthetic metavariable of kind `SyntheticMVarKind.withDefault` Return true if something was synthesized. -/ private def synthesizeUsingDefault : TermElabM Bool := do let prioSet ← getDefaultInstancesPriorities /- Recall that `prioSet` is stored in descending order -/ for prio in prioSet do if (← synthesizeUsingDefaultPrio prio) then return true return false /-- Report an error for each synthetic metavariable that could not be resolved. -/ private def reportStuckSyntheticMVars : TermElabM Unit := do let s ← get for mvarSyntheticDecl in s.syntheticMVars do withRef mvarSyntheticDecl.stx do match mvarSyntheticDecl.kind with | SyntheticMVarKind.typeClass => withMVarContext mvarSyntheticDecl.mvarId do let mvarDecl ← getMVarDecl mvarSyntheticDecl.mvarId unless (← get).messages.hasErrors do logError <| "typeclass instance problem contains metavariables" ++ indentExpr mvarDecl.type | SyntheticMVarKind.coe header expectedType eType e f? => withMVarContext mvarSyntheticDecl.mvarId do let mvarDecl ← getMVarDecl mvarSyntheticDecl.mvarId throwTypeMismatchError header expectedType eType e f? (some ("failed to create type class instance for " ++ indentExpr mvarDecl.type)) | _ => unreachable! -- TODO handle other cases. private def getSomeSynthethicMVarsRef : TermElabM Syntax := do let s ← get match s.syntheticMVars.find? $ fun (mvarDecl : SyntheticMVarDecl) => !mvarDecl.stx.getPos?.isNone with | some mvarDecl => pure mvarDecl.stx | none => pure Syntax.missing /-- Try to process pending synthetic metavariables. If `mayPostpone == false`, then `syntheticMVars` is `[]` after executing this method. It keeps executing `synthesizeSyntheticMVarsStep` while progress is being made. If `mayPostpone == false`, then it applies default instances to `SyntheticMVarKind.typeClass` (if available) metavariables that are still unresolved, and then tries to resolve metavariables with `mayPostpone == false`. That is, we force them to produce error messages and/or commit to a "best option". If, after that, we still haven't made progress, we report "stuck" errors. -/ partial def synthesizeSyntheticMVars (mayPostpone := true) : TermElabM Unit := let rec loop (u : Unit) : TermElabM Unit := do let ref ← getSomeSynthethicMVarsRef withRef ref <| withIncRecDepth do let s ← get unless s.syntheticMVars.isEmpty do if ← synthesizeSyntheticMVarsStep false false then loop () else if !mayPostpone then /- Resume pending metavariables with "elaboration postponement" disabled. We postpone elaboration errors in this step by setting `postponeOnError := true`. Example: ``` #check let x := ⟨1, 2⟩; Prod.fst x ``` The term `⟨1, 2⟩` can't be elaborated because the expected type is not know. The `x` at `Prod.fst x` is not elaborated because the type of `x` is not known. When we execute the following step with "elaboration postponement" disabled, the elaborator fails at `⟨1, 2⟩` and postpones it, and succeeds at `x` and learns that its type must be of the form `Prod ?α ?β`. Recall that we postponed `x` at `Prod.fst x` because its type it is not known. We the type of `x` may learn later its type and it may contain implicit and/or auto arguments. By disabling postponement, we are essentially giving up the opportunity of learning `x`s type and assume it does not have implict and/or auto arguments. -/ if ← withoutPostponing (synthesizeSyntheticMVarsStep true false) then loop () else if ← synthesizeUsingDefault then loop () else if ← withoutPostponing (synthesizeSyntheticMVarsStep false false) then loop () else if ← synthesizeSyntheticMVarsStep false true then loop () else reportStuckSyntheticMVars loop () def synthesizeSyntheticMVarsNoPostponing : TermElabM Unit := synthesizeSyntheticMVars (mayPostpone := false) /- Keep invoking `synthesizeUsingDefault` until it returns false. -/ private partial def synthesizeUsingDefaultLoop : TermElabM Unit := do if (← synthesizeUsingDefault) then synthesizeSyntheticMVars (mayPostpone := true) synthesizeUsingDefaultLoop def synthesizeSyntheticMVarsUsingDefault : TermElabM Unit := do synthesizeSyntheticMVars (mayPostpone := true) synthesizeUsingDefaultLoop /-- Execute `k`, and synthesize pending synthetic metavariables created while executing `k` are solved. If `mayPostpone == false`, then all of them must be synthesized. Remark: even if `mayPostpone == true`, the method still uses `synthesizeUsingDefault` -/ partial def withSynthesize {α} (k : TermElabM α) (mayPostpone := false) : TermElabM α := do let s ← get let syntheticMVarsSaved := s.syntheticMVars modify fun s => { s with syntheticMVars := [] } try let a ← k synthesizeSyntheticMVars mayPostpone if mayPostpone then synthesizeUsingDefaultLoop pure a finally modify fun s => { s with syntheticMVars := s.syntheticMVars ++ syntheticMVarsSaved } /-- Elaborate `stx`, and make sure all pending synthetic metavariables created while elaborating `stx` are solved. -/ def elabTermAndSynthesize (stx : Syntax) (expectedType? : Option Expr) : TermElabM Expr := withRef stx do let v ← withSynthesize $ elabTerm stx expectedType? instantiateMVars v end Lean.Elab.Term
ff251bd5c780f2fd057ccdf7be8cbc6a2569c698
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/probability/conditional_probability.lean
d790fe4a5720814ed6cd55767b70edad09c85f6d
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
6,661
lean
/- Copyright (c) 2022 Rishikesh Vaishnav. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Rishikesh Vaishnav -/ import measure_theory.measure.measure_space /-! # Conditional Probability This file defines conditional probability and includes basic results relating to it. Given some measure `μ` defined on a measure space on some type `Ω` and some `s : set Ω`, we define the measure of `μ` conditioned on `s` as the restricted measure scaled by the inverse of the measure of `s`: `cond μ s = (μ s)⁻¹ • μ.restrict s`. The scaling ensures that this is a probability measure (when `μ` is a finite measure). From this definition, we derive the "axiomatic" definition of conditional probability based on application: for any `s t : set Ω`, we have `μ[t|s] = (μ s)⁻¹ * μ (s ∩ t)`. ## Main Statements * `cond_cond_eq_cond_inter`: conditioning on one set and then another is equivalent to conditioning on their intersection. * `cond_eq_inv_mul_cond_mul`: Bayes' Theorem, `μ[t|s] = (μ s)⁻¹ * μ[s|t] * (μ t)`. ## Notations This file uses the notation `μ[|s]` the measure of `μ` conditioned on `s`, and `μ[t|s]` for the probability of `t` given `s` under `μ` (equivalent to the application `μ[|s] t`). These notations are contained in the locale `probability_theory`. ## Implementation notes Because we have the alternative measure restriction application principles `measure.restrict_apply` and `measure.restrict_apply'`, which require measurability of the restricted and restricting sets, respectively, many of the theorems here will have corresponding alternatives as well. For the sake of brevity, we've chosen to only go with `measure.restrict_apply'` for now, but the alternative theorems can be added if needed. Use of `@[simp]` generally follows the rule of removing conditions on a measure when possible. Hypotheses that are used to "define" a conditional distribution by requiring that the conditioning set has non-zero measure should be named using the abbreviation "c" (which stands for "conditionable") rather than "nz". For example `(hci : μ (s ∩ t) ≠ 0)` (rather than `hnzi`) should be used for a hypothesis ensuring that `μ[|s ∩ t]` is defined. ## Tags conditional, conditioned, bayes -/ noncomputable theory open_locale ennreal open measure_theory measurable_space variables {Ω : Type*} {m : measurable_space Ω} (μ : measure Ω) {s t : set Ω} namespace probability_theory section definitions /-- The conditional probability measure of measure `μ` on set `s` is `μ` restricted to `s` and scaled by the inverse of `μ s` (to make it a probability measure): `(μ s)⁻¹ • μ.restrict s`. -/ def cond (s : set Ω) : measure Ω := (μ s)⁻¹ • μ.restrict s end definitions localized "notation (name := probability_theory.cond) μ `[` s `|` t `]` := probability_theory.cond μ t s" in probability_theory localized "notation (name := probability_theory.cond_fn) μ `[|`:60 t`]` := probability_theory.cond μ t" in probability_theory /-- The conditional probability measure of any finite measure on any set of positive measure is a probability measure. -/ lemma cond_is_probability_measure [is_finite_measure μ] (hcs : μ s ≠ 0) : is_probability_measure $ μ[|s] := ⟨by { rw [cond, measure.smul_apply, measure.restrict_apply measurable_set.univ, set.univ_inter], exact ennreal.inv_mul_cancel hcs (measure_ne_top _ s) }⟩ section bayes @[simp] lemma cond_empty : μ[|∅] = 0 := by simp [cond] @[simp] lemma cond_univ [is_probability_measure μ] : μ[|set.univ] = μ := by simp [cond, measure_univ, measure.restrict_univ] /-- The axiomatic definition of conditional probability derived from a measure-theoretic one. -/ lemma cond_apply (hms : measurable_set s) (t : set Ω) : μ[t|s] = (μ s)⁻¹ * μ (s ∩ t) := by { rw [cond, measure.smul_apply, measure.restrict_apply' hms, set.inter_comm], refl } lemma cond_inter_self (hms : measurable_set s) (t : set Ω) : μ[s ∩ t|s] = μ[t|s] := by rw [cond_apply _ hms, ← set.inter_assoc, set.inter_self, ← cond_apply _ hms] lemma inter_pos_of_cond_ne_zero (hms : measurable_set s) (hcst : μ[t|s] ≠ 0) : 0 < μ (s ∩ t) := begin refine pos_iff_ne_zero.mpr (right_ne_zero_of_mul _), { exact (μ s)⁻¹ }, convert hcst, simp [hms, set.inter_comm] end lemma cond_pos_of_inter_ne_zero [is_finite_measure μ] (hms : measurable_set s) (hci : μ (s ∩ t) ≠ 0) : 0 < μ[|s] t := begin rw cond_apply _ hms, refine ennreal.mul_pos _ hci, exact ennreal.inv_ne_zero.mpr (measure_ne_top _ _), end lemma cond_cond_eq_cond_inter' (hms : measurable_set s) (hmt : measurable_set t) (hcs : μ s ≠ ∞) (hci : μ (s ∩ t) ≠ 0) : μ[|s][|t] = μ[|s ∩ t] := begin have hcs : μ s ≠ 0 := (μ.to_outer_measure.pos_of_subset_ne_zero (set.inter_subset_left _ _) hci).ne', ext u, simp [*, hms.inter hmt, cond_apply, ← mul_assoc, ← set.inter_assoc, ennreal.mul_inv, mul_comm, ← mul_assoc, ennreal.inv_mul_cancel], end /-- Conditioning first on `s` and then on `t` results in the same measure as conditioning on `s ∩ t`. -/ lemma cond_cond_eq_cond_inter [is_finite_measure μ] (hms : measurable_set s) (hmt : measurable_set t) (hci : μ (s ∩ t) ≠ 0) : μ[|s][|t] = μ[|s ∩ t] := cond_cond_eq_cond_inter' μ hms hmt (measure_ne_top μ s) hci lemma cond_mul_eq_inter' (hms : measurable_set s) (hcs : μ s ≠ 0) (hcs' : μ s ≠ ∞) (t : set Ω) : μ[t|s] * μ s = μ (s ∩ t) := by rw [cond_apply μ hms t, mul_comm, ←mul_assoc, ennreal.mul_inv_cancel hcs hcs', one_mul] lemma cond_mul_eq_inter [is_finite_measure μ] (hms : measurable_set s) (hcs : μ s ≠ 0) (t : set Ω) : μ[t|s] * μ s = μ (s ∩ t) := cond_mul_eq_inter' μ hms hcs (measure_ne_top _ s) t /-- A version of the law of total probability. -/ lemma cond_add_cond_compl_eq [is_finite_measure μ] (hms : measurable_set s) (hcs : μ s ≠ 0) (hcs' : μ sᶜ ≠ 0) : μ[t|s] * μ s + μ[t|sᶜ] * μ sᶜ = μ t := begin rw [cond_mul_eq_inter μ hms hcs, cond_mul_eq_inter μ hms.compl hcs', set.inter_comm _ t, set.inter_comm _ t], exact measure_inter_add_diff t hms, end /-- **Bayes' Theorem** -/ theorem cond_eq_inv_mul_cond_mul [is_finite_measure μ] (hms : measurable_set s) (hmt : measurable_set t) : μ[t|s] = (μ s)⁻¹ * μ[s|t] * (μ t) := begin by_cases ht : μ t = 0, { simp [cond, ht, measure.restrict_apply hmt, or.inr (measure_inter_null_of_null_left s ht)] }, { rw [mul_assoc, cond_mul_eq_inter μ hmt ht s, set.inter_comm, cond_apply _ hms] } end end bayes end probability_theory
8b562a861c866d817ac8d6b5a2acf496291b3853
96338d06deb5f54f351493a71d6ecf6c546089a2
/priv/Lean/Pi.lean
6ead88eafd4712bec7267e33c9df5f299b7a57c9
[]
no_license
silky/exe
5f9e4eea772d74852a1a2fac57d8d20588282d2b
e81690d6e16f2a83c105cce446011af6ae905b81
refs/heads/master
1,609,385,766,412
1,472,164,223,000
1,472,164,223,000
66,610,224
1
0
null
1,472,178,919,000
1,472,178,919,000
null
UTF-8
Lean
false
false
628
lean
/- Pi.lean -/ namespace EXE universe variable a universe variable b constant ImperdicativePi : Π {A : Type.{a}}, (A → Type.{b}) → Type.{b} abbreviation ℿ {A : Type.{a}} := @ImperdicativePi A definition Sigma := λ (A: Type), λ (B: A -> Type), ∀ (DepProd: Type), ∀ (Make: (∀ (_1: A), ∀ (_2: B _1), DepProd) ), DepProd definition snd := λ (A: Type), λ (B: A -> Type), λ (_1: A), λ (_2: B _1), λ (P: Sigma A B), _2 print snd end EXE
d22288044bef1beb5de7629cbf83fc6826b16314
59a4b050600ed7b3d5826a8478db0a9bdc190252
/src/category_theory/tactics/obviously.lean
06a45a4a625058715fcf9b591f0b53d6d07b76a5
[]
no_license
rwbarton/lean-category-theory
f720268d800b62a25d69842ca7b5d27822f00652
00df814d463406b7a13a56f5dcda67758ba1b419
refs/heads/master
1,585,366,296,767
1,536,151,349,000
1,536,151,349,000
147,652,096
0
0
null
1,536,226,960,000
1,536,226,960,000
null
UTF-8
Lean
false
false
1,074
lean
import category_theory.functor_category import category_theory.natural_transformation import category_theory.products import category_theory.types import category_theory.isomorphism import category_theory.embedding import tidy.tidy open category_theory attribute [search] category.id_comp category.comp_id category.assoc attribute [search] category_theory.functor.map_id category_theory.functor.map_comp attribute [search] nat_trans.naturality nat_trans.vcomp_app nat_trans.vcomp_assoc nat_trans.hcomp_app nat_trans.exchange attribute [search] prod_id prod_comp functor.prod_obj functor.prod_map nat_trans.prod_app attribute [search] functor.category.id_app functor.category.comp_app functor.category.nat_trans.app_naturality functor.category.nat_trans.naturality_app attribute [search] functor_to_types.map_comp functor_to_types.map_id functor_to_types.naturality attribute [search] iso.hom_inv_id iso.inv_hom_id is_iso.hom_inv_id is_iso.inv_hom_id attribute [back'] full.preimage attribute [search] full.witness image_preimage attribute [forward] faithful.injectivity
bd054079c9fdd11431e0fa44c496ab0da549320a
3f7026ea8bef0825ca0339a275c03b911baef64d
/src/data/padics/padic_norm.lean
f41c688820c03161f110d297f374639f84ccc437
[ "Apache-2.0" ]
permissive
rspencer01/mathlib
b1e3afa5c121362ef0881012cc116513ab09f18c
c7d36292c6b9234dc40143c16288932ae38fdc12
refs/heads/master
1,595,010,346,708
1,567,511,503,000
1,567,511,503,000
206,071,681
0
0
Apache-2.0
1,567,513,643,000
1,567,513,643,000
null
UTF-8
Lean
false
false
17,029
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.rat algebra.gcd_domain algebra.field_power import ring_theory.multiplicity tactic.ring import data.real.cau_seq import tactic.norm_cast /-! # 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 (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 attribute [class] nat.prime local infix `/.`:70 := rat.mk 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 : p.prime] {q : ℚ} (hq : q ≠ 0) : padic_val_rat p q = (multiplicity (p : ℤ) q.num).get (finite_int_iff.2 ⟨hp.ne_one, rat.num_ne_zero_of_ne_zero hq⟩) - (multiplicity (p : ℤ) q.denom).get (finite_int_iff.2 ⟨hp.ne_one, by exact_mod_cast rat.denom_ne_zero _⟩) := dif_pos ⟨hq, hp.ne_one⟩ namespace padic_val_rat open multiplicity section padic_val_rat 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 section padic_val_rat open multiplicity variables (p : ℕ) [p_prime : nat.prime p] include p_prime /-- The multiplicity of `p : ℕ` in `a : ℤ` is finite exactly when `a ≠ 0`. -/ lemma finite_int_prime_iff {p : ℕ} [p_prime : p.prime] {a : ℤ} : finite (p : ℤ) a ↔ a ≠ 0 := by simp [finite_int_iff, ne.symm (ne_of_lt (p_prime.gt_one))] /-- 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.gt_one, λ hn, by simp * at *⟩) - (multiplicity (p : ℤ) d).get (finite_int_iff.2 ⟨ne.symm $ ne_of_lt p_prime.gt_one, λ 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), (ne.symm (ne_of_lt p_prime.gt_one)), 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, 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 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), _root_.pow_succ, add_mul] /-- 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) hf1, add_comm, ← multiplicity.mul' (nat.prime_iff_prime_int.1 p_prime) 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 hqdv : q.num /. q.denom ≠ 0, from rat.mk_ne_zero_of_ne_zero hqn hqd, have hrdv : r.num /. r.denom ≠ 0, from rat.mk_ne_zero_of_ne_zero hrn hrd, 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), 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), add_comm]) (by rw [mul_assoc, @multiplicity.mul _ _ _ _ (q.denom : ℤ) (_ * _) (nat.prime_iff_prime_int.1 p_prime)]; 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) end padic_val_rat end padic_val_rat /-- 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 : ℚ) : padic_norm p q ≥ 0 := if hq : q = 0 then by simp [hq] else begin unfold padic_norm; split_ifs, apply fpow_nonneg_of_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 image of `padic_norm p` is {0} ∪ {p^(-n) | n ∈ ℤ}. -/ protected theorem image {q : ℚ} (hq : q ≠ 0) : ∃ n : ℤ, padic_norm p q = p ^ (-n) := ⟨ (padic_val_rat p q), by simp [padic_norm, hq] ⟩ variable [hp : 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.pos end /-- `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, hp.gt_one] /-- 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.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.ne_zero], by simp [padic_norm, *, padic_val_rat.mul, fpow_add this] /-- 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] else begin unfold padic_norm, rw [if_neg _], { refine fpow_le_one_of_nonpos _ _, { exact_mod_cast le_of_lt hp.gt_one, }, { rw [padic_val_rat_of_int _ hp.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.gt_one }, { 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 } /-- If `p^n` divides an integer `z`, then the p-adic norm of `z` is at most `p^(-n)`. -/ lemma le_of_dvd {n : ℕ} {z : ℤ} (hd : ↑(p^n) ∣ z) : padic_norm p z ≤ ↑p ^ (-n : ℤ) := begin unfold padic_norm, split_ifs with hz hz, { apply fpow_nonneg_of_nonneg, exact_mod_cast le_of_lt hp.pos }, { apply fpow_le_of_le, exact_mod_cast le_of_lt hp.gt_one, apply neg_le_neg, rw padic_val_rat_of_int _ hp.ne_one _, { norm_cast, rw [← enat.coe_le_coe, enat.coe_get], apply multiplicity.le_multiplicity_of_pow_dvd, exact_mod_cast hd }, { exact_mod_cast hz }} end end padic_norm end padic_norm
f22d3eab931e50b7d621f56db54d2208fe50e60a
4efff1f47634ff19e2f786deadd394270a59ecd2
/src/ring_theory/subring.lean
2c1e19537707260aed97f28b2876fe3da9f539af
[ "Apache-2.0" ]
permissive
agjftucker/mathlib
d634cd0d5256b6325e3c55bb7fb2403548371707
87fe50de17b00af533f72a102d0adefe4a2285e8
refs/heads/master
1,625,378,131,941
1,599,166,526,000
1,599,166,526,000
160,748,509
0
0
Apache-2.0
1,544,141,789,000
1,544,141,789,000
null
UTF-8
Lean
false
false
29,729
lean
/- Copyright (c) 2020 Ashvni Narayanan. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors : Ashvni Narayanan -/ import group_theory.subgroup import ring_theory.subsemiring /-! # Subrings Let `R` be a ring. This file defines the "bundled" subring type `subring R`, a type whose terms correspond to subrings of `R`. This is the preferred way to talk about subrings in mathlib. Unbundled subrings (`s : set R` and `is_subring s`) are not in this file, and they will ultimately be deprecated. We prove that subrings are a complete lattice, and that you can `map` (pushforward) and `comap` (pull back) them along ring homomorphisms. We define the `closure` construction from `set R` to `subring R`, sending a subset of `R` to the subring it generates, and prove that it is a Galois insertion. ## Main definitions Notation used here: `(R : Type u) [ring R] (S : Type u) [ring S] (f g : R →+* S)` `(A : subring R) (B : subring S) (s : set R)` * `subring R` : the type of subrings of a ring `R`. * `instance : complete_lattice (subring R)` : the complete lattice structure on the subrings. * `subring.closure` : subring closure of a set, i.e., the smallest subring that includes the set. * `subring.gi` : `closure : set M → subring M` and coercion `coe : subring M → set M` form a `galois_insertion`. * `comap f B : subring A` : the preimage of a subring `B` along the ring homomorphism `f` * `map f A : subring B` : the image of a subring `A` along the ring homomorphism `f`. * `prod A B : subring (R × S)` : the product of subrings * `f.range : subring B` : the range of the ring homomorphism `f`. * `eq_locus f g : subring R` : given ring homomorphisms `f g : R →+* S`, the subring of `R` where `f x = g x` ## Implementation notes A subring is implemented as a subsemiring which is also an additive subgroup. The initial PR was as a submonoid which is also an additive subgroup. Lattice inclusion (e.g. `≤` and `⊓`) is used rather than set notation (`⊆` and `∩`), although `∈` is defined as membership of a subring's underlying set. ## Tags subring, subrings -/ open_locale big_operators universes u v w open group variables {R : Type u} {S : Type v} {T : Type w} [ring R] [ring S] [ring T] set_option old_structure_cmd true /-- `subring R` is the type of subrings of `R`. A subring of `R` is a subset `s` that is a multiplicative submonoid and an additive subgroup. Note in particular that it shares the same 0 and 1 as R. -/ structure subring (R : Type u) [ring R] extends subsemiring R, add_subgroup R /-- Reinterpret a `subring` as a `subsemiring`. -/ add_decl_doc subring.to_subsemiring /-- Reinterpret a `subring` as an `add_subgroup`. -/ add_decl_doc subring.to_add_subgroup namespace subring /-- The underlying submonoid of a subring. -/ def to_submonoid (s : subring R) : submonoid R := { carrier := s.carrier, ..s.to_subsemiring.to_submonoid } instance : has_coe (subring R) (set R) := ⟨subring.carrier⟩ instance : has_coe_to_sort (subring R) := ⟨Type*, λ S, S.carrier⟩ instance : has_mem R (subring R) := ⟨λ m S, m ∈ (S:set R)⟩ /-- Construct a `subring R` from a set `s`, a submonoid `sm`, and an additive subgroup `sa` such that `x ∈ s ↔ x ∈ sm ↔ x ∈ sa`. -/ protected def mk' (s : set R) (sm : submonoid R) (sa : add_subgroup R) (hm : ↑sm = s) (ha : ↑sa = s) : subring R := { carrier := s, zero_mem' := ha ▸ sa.zero_mem, one_mem' := hm ▸ sm.one_mem, add_mem' := λ x y, by simpa only [← ha] using sa.add_mem, mul_mem' := λ x y, by simpa only [← hm] using sm.mul_mem, neg_mem' := λ x, by simpa only [← ha] using sa.neg_mem, } @[simp] lemma coe_mk' {s : set R} {sm : submonoid R} (hm : ↑sm = s) {sa : add_subgroup R} (ha : ↑sa = s) : (subring.mk' s sm sa hm ha : set R) = s := rfl @[simp] lemma mem_mk' {s : set R} {sm : submonoid R} (hm : ↑sm = s) {sa : add_subgroup R} (ha : ↑sa = s) {x : R} : x ∈ subring.mk' s sm sa hm ha ↔ x ∈ s := iff.rfl @[simp] lemma mk'_to_submonoid {s : set R} {sm : submonoid R} (hm : ↑sm = s) {sa : add_subgroup R} (ha : ↑sa = s) : (subring.mk' s sm sa hm ha).to_submonoid = sm := submonoid.ext' hm.symm @[simp] lemma mk'_to_add_subgroup {s : set R} {sm : submonoid R} (hm : ↑sm = s) {sa : add_subgroup R} (ha : ↑sa =s) : (subring.mk' s sm sa hm ha).to_add_subgroup = sa := add_subgroup.ext' ha.symm end subring protected lemma subring.exists {s : subring R} {p : s → Prop} : (∃ x : s, p x) ↔ ∃ x ∈ s, p ⟨x, ‹x ∈ s›⟩ := set_coe.exists protected lemma subring.forall {s : subring R} {p : s → Prop} : (∀ x : s, p x) ↔ ∀ x ∈ s, p ⟨x, ‹x ∈ s›⟩ := set_coe.forall /-- A `subsemiring` containing -1 is a `subring`. -/ def subsemiring.to_subring (s : subsemiring R) (hneg : (-1 : R) ∈ s) : subring R := { neg_mem' := by { rintros x, rw <-neg_one_mul, apply subsemiring.mul_mem, exact hneg, } ..s.to_submonoid, ..s.to_add_submonoid } namespace subring variables (s : subring R) /-- Two subrings are equal if the underlying subsets are equal. -/ theorem ext' ⦃s t : subring R⦄ (h : (s : set R) = t) : s = t := by { cases s, cases t, congr' } /-- Two subrings are equal if and only if the underlying subsets are equal. -/ protected theorem ext'_iff {s t : subring R} : s = t ↔ (s : set R) = t := ⟨λ h, h ▸ rfl, λ h, ext' h⟩ /-- Two subrings are equal if they have the same elements. -/ @[ext] theorem ext {S T : subring R} (h : ∀ x, x ∈ S ↔ x ∈ T) : S = T := ext' $ set.ext h /-- A subring contains the ring's 1. -/ theorem one_mem : (1 : R) ∈ s := s.one_mem' /-- A subring contains the ring's 0. -/ theorem zero_mem : (0 : R) ∈ s := s.zero_mem' /-- A subring is closed under multiplication. -/ theorem mul_mem : ∀ {x y : R}, x ∈ s → y ∈ s → x * y ∈ s := s.mul_mem' /-- A subring is closed under addition. -/ theorem add_mem : ∀ {x y : R}, x ∈ s → y ∈ s → x + y ∈ s := s.add_mem' /-- A subring is closed under negation. -/ theorem neg_mem : ∀ {x : R}, x ∈ s → -x ∈ s := s.neg_mem' /-- Product of a list of elements in a subring is in the subring. -/ lemma list_prod_mem {l : list R} : (∀x ∈ l, x ∈ s) → l.prod ∈ s := s.to_submonoid.list_prod_mem /-- Sum of a list of elements in a subring is in the subring. -/ lemma list_sum_mem {l : list R} : (∀x ∈ l, x ∈ s) → l.sum ∈ s := s.to_add_subgroup.list_sum_mem /-- Product of a multiset of elements in a subring of a `comm_ring` is in the subring. -/ lemma multiset_prod_mem {R} [comm_ring R] (s : subring R) (m : multiset R) : (∀a ∈ m, a ∈ s) → m.prod ∈ s := s.to_submonoid.multiset_prod_mem m /-- Sum of a multiset of elements in an `subring` of a `ring` is in the `subring`. -/ lemma multiset_sum_mem {R} [ring R] (s : subring R) (m : multiset R) : (∀a ∈ m, a ∈ s) → m.sum ∈ s := s.to_add_subgroup.multiset_sum_mem m /-- Product of elements of a subring of a `comm_ring` indexed by a `finset` is in the subring. -/ lemma prod_mem {R : Type*} [comm_ring R] (s : subring R) {ι : Type*} {t : finset ι} {f : ι → R} (h : ∀c ∈ t, f c ∈ s) : ∏ i in t, f i ∈ s := s.to_submonoid.prod_mem h /-- Sum of elements in a `subring` of a `ring` indexed by a `finset` is in the `subring`. -/ lemma sum_mem {R : Type*} [ring R] (s : subring R) {ι : Type*} {t : finset ι} {f : ι → R} (h : ∀c ∈ t, f c ∈ s) : ∑ i in t, f i ∈ s := s.to_add_subgroup.sum_mem h lemma pow_mem {x : R} (hx : x ∈ s) (n : ℕ) : x^n ∈ s := s.to_submonoid.pow_mem hx n lemma gsmul_mem {x : R} (hx : x ∈ s) (n : ℤ) : n •ℤ x ∈ s := s.to_add_subgroup.gsmul_mem hx n lemma coe_int_mem (n : ℤ) : (n : R) ∈ s := by simp only [← gsmul_one, gsmul_mem, one_mem] /-- A subring of a ring inherits a ring structure -/ instance to_ring : ring s := { right_distrib := λ x y z, subtype.eq $ right_distrib x y z, left_distrib := λ x y z, subtype.eq $ left_distrib x y z, .. s.to_submonoid.to_monoid, .. s.to_add_subgroup.to_add_comm_group } @[simp, norm_cast] lemma coe_add (x y : s) : (↑(x + y) : R) = ↑x + ↑y := rfl @[simp, norm_cast] lemma coe_neg (x : s) : (↑(-x) : R) = -↑x := rfl @[simp, norm_cast] lemma coe_mul (x y : s) : (↑(x * y) : R) = ↑x * ↑y := rfl @[simp, norm_cast] lemma coe_zero : ((0 : s) : R) = 0 := rfl @[simp, norm_cast] lemma coe_one : ((1 : s) : R) = 1 := rfl /-- A subring of a `comm_ring` is a `comm_ring`. -/ def to_comm_ring {R} [comm_ring R] (s : subring R) : comm_ring s := { mul_comm := λ _ _, subtype.eq $ mul_comm _ _, ..subring.to_ring s} /-- The natural ring hom from a subring of ring `R` to `R`. -/ def subtype (s : subring R) : s →+* R := { to_fun := coe, .. s.to_submonoid.subtype, .. s.to_add_subgroup.subtype } @[simp] theorem coe_subtype : ⇑s.subtype = coe := rfl /-! # Partial order -/ instance : partial_order (subring R) := { le := λ s t, ∀ ⦃x⦄, x ∈ s → x ∈ t, .. partial_order.lift (coe : subring R → set R) ext' } lemma le_def {s t : subring R} : s ≤ t ↔ ∀ ⦃x : R⦄, x ∈ s → x ∈ t := iff.rfl @[simp, norm_cast] lemma coe_subset_coe {s t : subring R} : (s : set R) ⊆ t ↔ s ≤ t := iff.rfl @[simp, norm_cast] lemma coe_ssubset_coe {s t : subring R} : (s : set R) ⊂ t ↔ s < t := iff.rfl @[simp, norm_cast] lemma mem_coe {S : subring R} {m : R} : m ∈ (S : set R) ↔ m ∈ S := iff.rfl @[simp, norm_cast] lemma coe_coe (s : subring R) : ↥(s : set R) = s := rfl @[simp] lemma mem_to_submonoid {s : subring R} {x : R} : x ∈ s.to_submonoid ↔ x ∈ s := iff.rfl @[simp] lemma coe_to_submonoid (s : subring R) : (s.to_submonoid : set R) = s := rfl @[simp] lemma mem_to_add_subgroup {s : subring R} {x : R} : x ∈ s.to_add_subgroup ↔ x ∈ s := iff.rfl @[simp] lemma coe_to_add_subgroup (s : subring R) : (s.to_add_subgroup : set R) = s := rfl /-! # top -/ /-- The subring `R` of the ring `R`. -/ instance : has_top (subring R) := ⟨{ .. (⊤ : submonoid R), .. (⊤ : add_subgroup R) }⟩ @[simp] lemma mem_top (x : R) : x ∈ (⊤ : subring R) := set.mem_univ x @[simp] lemma coe_top : ((⊤ : subring R) : set R) = set.univ := rfl /-! # comap -/ /-- The preimage of a subring along a ring homomorphism is a subring. -/ def comap {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) (s : subring S) : subring R := { carrier := f ⁻¹' s.carrier, .. s.to_submonoid.comap (f : R →* S), .. s.to_add_subgroup.comap (f : R →+ S) } @[simp] lemma coe_comap (s : subring S) (f : R →+* S) : (s.comap f : set R) = f ⁻¹' s := rfl @[simp] lemma mem_comap {s : subring S} {f : R →+* S} {x : R} : x ∈ s.comap f ↔ f x ∈ s := iff.rfl lemma comap_comap (s : subring T) (g : S →+* T) (f : R →+* S) : (s.comap g).comap f = s.comap (g.comp f) := rfl /-! # map -/ /-- The image of a subring along a ring homomorphism is a subring. -/ def map {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) (s : subring R) : subring S := { carrier := f '' s.carrier, .. s.to_submonoid.map (f : R →* S), .. s.to_add_subgroup.map (f : R →+ S) } @[simp] lemma coe_map (f : R →+* S) (s : subring R) : (s.map f : set S) = f '' s := rfl @[simp] lemma mem_map {f : R →+* S} {s : subring R} {y : S} : y ∈ s.map f ↔ ∃ x ∈ s, f x = y := set.mem_image_iff_bex lemma map_map (g : S →+* T) (f : R →+* S) : (s.map f).map g = s.map (g.comp f) := ext' $ set.image_image _ _ _ lemma map_le_iff_le_comap {f : R →+* S} {s : subring R} {t : subring S} : s.map f ≤ t ↔ s ≤ t.comap f := set.image_subset_iff lemma gc_map_comap (f : R →+* S) : galois_connection (map f) (comap f) := λ S T, map_le_iff_le_comap end subring namespace ring_hom variables (g : S →+* T) (f : R →+* S) /-! # range -/ /-- The range of a ring homomorphism, as a subring of the target. -/ def range {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) : subring S := (⊤ : subring R).map f @[simp] lemma coe_range : (f.range : set S) = set.range f := set.image_univ @[simp] lemma mem_range {f : R →+* S} {y : S} : y ∈ f.range ↔ ∃ x, f x = y := by simp [range] lemma map_range : f.range.map g = (g.comp f).range := (⊤ : subring R).map_map g f -- TODO -- rename to `cod_restrict` when is_ring_hom is deprecated /-- Restrict the codomain of a ring homomorphism to a subring that includes the range. -/ def cod_restrict' {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) (s : subring S) (h : ∀ x, f x ∈ s) : R →+* s := { to_fun := λ x, ⟨f x, h x⟩, map_add' := λ x y, subtype.eq $ f.map_add x y, map_zero' := subtype.eq f.map_zero, map_mul' := λ x y, subtype.eq $ f.map_mul x y, map_one' := subtype.eq f.map_one } end ring_hom namespace subring variables {cR : Type u} [comm_ring cR] /-- A subring of a commutative ring is a commutative ring. -/ def subset_comm_ring (S : subring cR) : comm_ring S := {mul_comm := λ _ _, subtype.eq $ mul_comm _ _, ..subring.to_ring S} /-- A subring of an integral domain is an integral domain. -/ instance subring.domain {D : Type*} [integral_domain D] (S : subring D) : integral_domain S := { exists_pair_ne := ⟨0, 1, mt subtype.ext_iff_val.1 zero_ne_one⟩, eq_zero_or_eq_zero_of_mul_eq_zero := λ ⟨x, hx⟩ ⟨y, hy⟩, by { simp only [subtype.ext_iff_val, subtype.coe_mk], exact eq_zero_or_eq_zero_of_mul_eq_zero }, .. S.subset_comm_ring, } /-! # bot -/ instance : has_bot (subring R) := ⟨(int.cast_ring_hom R).range⟩ instance : inhabited (subring R) := ⟨⊥⟩ lemma coe_bot : ((⊥ : subring R) : set R) = set.range (coe : ℤ → R) := ring_hom.coe_range (int.cast_ring_hom R) lemma mem_bot {x : R} : x ∈ (⊥ : subring R) ↔ ∃ (n : ℤ), ↑n = x := ring_hom.mem_range /-! # inf -/ /-- The inf of two subrings is their intersection. -/ instance : has_inf (subring R) := ⟨λ s t, { carrier := s ∩ t, .. s.to_submonoid ⊓ t.to_submonoid, .. s.to_add_subgroup ⊓ t.to_add_subgroup }⟩ @[simp] lemma coe_inf (p p' : subring R) : ((p ⊓ p' : subring R) : set R) = p ∩ p' := rfl @[simp] lemma mem_inf {p p' : subring R} {x : R} : x ∈ p ⊓ p' ↔ x ∈ p ∧ x ∈ p' := iff.rfl instance : has_Inf (subring R) := ⟨λ s, subring.mk' (⋂ t ∈ s, ↑t) (⨅ t ∈ s, subring.to_submonoid t ) (⨅ t ∈ s, subring.to_add_subgroup t) (by simp) (by simp)⟩ @[simp, norm_cast] lemma coe_Inf (S : set (subring R)) : ((Inf S : subring R) : set R) = ⋂ s ∈ S, ↑s := rfl lemma mem_Inf {S : set (subring R)} {x : R} : x ∈ Inf S ↔ ∀ p ∈ S, x ∈ p := set.mem_bInter_iff @[simp] lemma Inf_to_submonoid (s : set (subring R)) : (Inf s).to_submonoid = ⨅ t ∈ s, subring.to_submonoid t := mk'_to_submonoid _ _ @[simp] lemma Inf_to_add_subgroup (s : set (subring R)) : (Inf s).to_add_subgroup = ⨅ t ∈ s, subring.to_add_subgroup t := mk'_to_add_subgroup _ _ /-- Subrings of a ring form a complete lattice. -/ instance : complete_lattice (subring R) := { bot := (⊥), bot_le := λ s x hx, let ⟨n, hn⟩ := mem_bot.1 hx in hn ▸ s.coe_int_mem n, top := (⊤), le_top := λ s x hx, trivial, inf := (⊓), inf_le_left := λ s t x, and.left, inf_le_right := λ s t x, and.right, le_inf := λ s t₁ t₂ h₁ h₂ x hx, ⟨h₁ hx, h₂ hx⟩, .. complete_lattice_of_Inf (subring R) (λ s, is_glb.of_image (λ s t, show (s : set R) ≤ t ↔ s ≤ t, from coe_subset_coe) is_glb_binfi)} /-! # subring closure of a subset -/ /-- The `subring` generated by a set. -/ def closure (s : set R) : subring R := Inf {S | s ⊆ S} lemma mem_closure {x : R} {s : set R} : x ∈ closure s ↔ ∀ S : subring R, s ⊆ S → x ∈ S := mem_Inf /-- The subring generated by a set includes the set. -/ @[simp] lemma subset_closure {s : set R} : s ⊆ closure s := λ x hx, mem_closure.2 $ λ S hS, hS hx /-- A subring `t` includes `closure s` if and only if it includes `s`. -/ @[simp] lemma closure_le {s : set R} {t : subring R} : closure s ≤ t ↔ s ⊆ t := ⟨set.subset.trans subset_closure, λ h, Inf_le h⟩ /-- Subring closure of a set is monotone in its argument: if `s ⊆ t`, then `closure s ≤ closure t`. -/ lemma closure_mono ⦃s t : set R⦄ (h : s ⊆ t) : closure s ≤ closure t := closure_le.2 $ set.subset.trans h subset_closure lemma closure_eq_of_le {s : set R} {t : subring R} (h₁ : s ⊆ t) (h₂ : t ≤ closure s) : closure s = t := le_antisymm (closure_le.2 h₁) h₂ /-- An induction principle for closure membership. If `p` holds for `0`, `1`, and all elements of `s`, and is preserved under addition, negation, and multiplication, then `p` holds for all elements of the closure of `s`. -/ @[elab_as_eliminator] lemma closure_induction {s : set R} {p : R → Prop} {x} (h : x ∈ closure s) (Hs : ∀ x ∈ s, p x) (H0 : p 0) (H1 : p 1) (Hadd : ∀ x y, p x → p y → p (x + y)) (Hneg : ∀ (x : R), p x → p (-x)) (Hmul : ∀ x y, p x → p y → p (x * y)) : p x := (@closure_le _ _ _ ⟨p, H1, Hmul, H0, Hadd, Hneg⟩).2 Hs h lemma mem_closure_iff {s : set R} {x} : x ∈ closure s ↔ x ∈ add_subgroup.closure (submonoid.closure s : set R) := ⟨ λ h, closure_induction h (λ x hx, add_subgroup.subset_closure $ submonoid.subset_closure hx ) (add_subgroup.zero_mem _) (add_subgroup.subset_closure ( submonoid.one_mem (submonoid.closure s)) ) (λ x y hx hy, add_subgroup.add_mem _ hx hy ) (λ x hx, add_subgroup.neg_mem _ hx ) ( λ x y hx hy, add_subgroup.closure_induction hy (λ q hq, add_subgroup.closure_induction hx ( λ p hp, add_subgroup.subset_closure ((submonoid.closure s).mul_mem hp hq) ) ( begin rw zero_mul q, apply add_subgroup.zero_mem _, end ) ( λ p₁ p₂ ihp₁ ihp₂, begin rw add_mul p₁ p₂ q, apply add_subgroup.add_mem _ ihp₁ ihp₂, end ) ( λ x hx, begin have f : -x * q = -(x*q) := by simp, rw f, apply add_subgroup.neg_mem _ hx, end ) ) ( begin rw mul_zero x, apply add_subgroup.zero_mem _, end ) ( λ q₁ q₂ ihq₁ ihq₂, begin rw mul_add x q₁ q₂, apply add_subgroup.add_mem _ ihq₁ ihq₂ end ) ( λ z hz, begin have f : x * -z = -(x*z) := by simp, rw f, apply add_subgroup.neg_mem _ hz, end ) ), λ h, add_subgroup.closure_induction h ( λ x hx, submonoid.closure_induction hx ( λ x hx, subset_closure hx ) ( one_mem _ ) ( λ x y hx hy, mul_mem _ hx hy ) ) ( zero_mem _ ) (λ x y hx hy, add_mem _ hx hy) ( λ x hx, neg_mem _ hx ) ⟩ theorem exists_list_of_mem_closure {s : set R} {x : R} (h : x ∈ closure s) : (∃ L : list (list R), (∀ t ∈ L, ∀ y ∈ t, y ∈ s ∨ y = (-1:R)) ∧ (L.map list.prod).sum = x) := add_subgroup.closure_induction (mem_closure_iff.1 h) (λ x hx, let ⟨l, hl, h⟩ :=submonoid.exists_list_of_mem_closure hx in ⟨[l], by simp [h]; clear_aux_decl; tauto!⟩) ⟨[], by simp⟩ (λ x y ⟨l, hl1, hl2⟩ ⟨m, hm1, hm2⟩, ⟨l ++ m, λ t ht, (list.mem_append.1 ht).elim (hl1 t) (hm1 t), by simp [hl2, hm2]⟩) (λ x ⟨L, hL⟩, ⟨L.map (list.cons (-1)), list.forall_mem_map_iff.2 $ λ j hj, list.forall_mem_cons.2 ⟨or.inr rfl, hL.1 j hj⟩, hL.2 ▸ list.rec_on L (by simp) (by simp [list.map_cons, add_comm] {contextual := tt})⟩) variable (R) /-- `closure` forms a Galois insertion with the coercion to set. -/ protected def gi : galois_insertion (@closure R _) coe := { choice := λ s _, closure s, gc := λ s t, closure_le, le_l_u := λ s, subset_closure, choice_eq := λ s h, rfl } variable {R} /-- Closure of a subring `S` equals `S`. -/ lemma closure_eq (s : subring R) : closure (s : set R) = s := (subring.gi R).l_u_eq s @[simp] lemma closure_empty : closure (∅ : set R) = ⊥ := (subring.gi R).gc.l_bot @[simp] lemma closure_univ : closure (set.univ : set R) = ⊤ := @coe_top R _ ▸ closure_eq ⊤ lemma closure_union (s t : set R) : closure (s ∪ t) = closure s ⊔ closure t := (subring.gi R).gc.l_sup lemma closure_Union {ι} (s : ι → set R) : closure (⋃ i, s i) = ⨆ i, closure (s i) := (subring.gi R).gc.l_supr lemma closure_sUnion (s : set (set R)) : closure (⋃₀ s) = ⨆ t ∈ s, closure t := (subring.gi R).gc.l_Sup lemma map_sup (s t : subring R) (f : R →+* S) : (s ⊔ t).map f = s.map f ⊔ t.map f := (gc_map_comap f).l_sup lemma map_supr {ι : Sort*} (f : R →+* S) (s : ι → subring R) : (supr s).map f = ⨆ i, (s i).map f := (gc_map_comap f).l_supr lemma comap_inf (s t : subring S) (f : R →+* S) : (s ⊓ t).comap f = s.comap f ⊓ t.comap f := (gc_map_comap f).u_inf lemma comap_infi {ι : Sort*} (f : R →+* S) (s : ι → subring S) : (infi s).comap f = ⨅ i, (s i).comap f := (gc_map_comap f).u_infi @[simp] lemma map_bot (f : R →+* S) : (⊥ : subring R).map f = ⊥ := (gc_map_comap f).l_bot @[simp] lemma comap_top (f : R →+* S) : (⊤ : subring S).comap f = ⊤ := (gc_map_comap f).u_top /-- Given `subring`s `s`, `t` of rings `R`, `S` respectively, `s.prod t` is `s × t` as a subring of `R × S`. -/ def prod (s : subring R) (t : subring S) : subring (R × S) := { carrier := (s : set R).prod t, .. s.to_submonoid.prod t.to_submonoid, .. s.to_add_subgroup.prod t.to_add_subgroup} @[norm_cast] lemma coe_prod (s : subring R) (t : subring S) : (s.prod t : set (R × S)) = (s : set R).prod (t : set S) := rfl lemma mem_prod {s : subring R} {t : subring S} {p : R × S} : p ∈ s.prod t ↔ p.1 ∈ s ∧ p.2 ∈ t := iff.rfl @[mono] lemma prod_mono ⦃s₁ s₂ : subring R⦄ (hs : s₁ ≤ s₂) ⦃t₁ t₂ : subring S⦄ (ht : t₁ ≤ t₂) : s₁.prod t₁ ≤ s₂.prod t₂ := set.prod_mono hs ht lemma prod_mono_right (s : subring R) : monotone (λ t : subring S, s.prod t) := prod_mono (le_refl s) lemma prod_mono_left (t : subring S) : monotone (λ s : subring R, s.prod t) := λ s₁ s₂ hs, prod_mono hs (le_refl t) lemma prod_top (s : subring R) : s.prod (⊤ : subring S) = s.comap (ring_hom.fst R S) := ext $ λ x, by simp [mem_prod, monoid_hom.coe_fst] lemma top_prod (s : subring S) : (⊤ : subring R).prod s = s.comap (ring_hom.snd R S) := ext $ λ x, by simp [mem_prod, monoid_hom.coe_snd] @[simp] lemma top_prod_top : (⊤ : subring R).prod (⊤ : subring S) = ⊤ := (top_prod _).trans $ comap_top _ /-- Product of subrings is isomorphic to their product as rings. -/ def prod_equiv (s : subring R) (t : subring S) : s.prod t ≃+* s × t := { map_mul' := λ x y, rfl, map_add' := λ x y, rfl, .. equiv.set.prod ↑s ↑t } /-- The underlying set of a non-empty directed Sup of subrings is just a union of the subrings. Note that this fails without the directedness assumption (the union of two subrings is typically not a subring) -/ lemma mem_supr_of_directed {ι} [hι : nonempty ι] {S : ι → subring R} (hS : directed (≤) S) {x : R} : x ∈ (⨆ i, S i) ↔ ∃ i, x ∈ S i := begin refine ⟨_, λ ⟨i, hi⟩, (le_def.1 $ le_supr S i) hi⟩, let U : subring R := subring.mk' (⋃ i, (S i : set R)) (⨆ i, (S i).to_submonoid) (⨆ i, (S i).to_add_subgroup) (submonoid.coe_supr_of_directed $ hS.mono_comp _ (λ _ _, id)) (add_subgroup.coe_supr_of_directed $ hS.mono_comp _ (λ _ _, id)), suffices : (⨆ i, S i) ≤ U, by simpa using @this x, exact supr_le (λ i x hx, set.mem_Union.2 ⟨i, hx⟩), end lemma coe_supr_of_directed {ι} [hι : nonempty ι] {S : ι → subring R} (hS : directed (≤) S) : ((⨆ i, S i : subring R) : set R) = ⋃ i, ↑(S i) := set.ext $ λ x, by simp [mem_supr_of_directed hS] lemma mem_Sup_of_directed_on {S : set (subring R)} (Sne : S.nonempty) (hS : directed_on (≤) S) {x : R} : x ∈ Sup S ↔ ∃ s ∈ S, x ∈ s := begin haveI : nonempty S := Sne.to_subtype, simp only [Sup_eq_supr', mem_supr_of_directed hS.directed_coe, set_coe.exists, subtype.coe_mk] end lemma coe_Sup_of_directed_on {S : set (subring R)} (Sne : S.nonempty) (hS : directed_on (≤) S) : (↑(Sup S) : set R) = ⋃ s ∈ S, ↑s := set.ext $ λ x, by simp [mem_Sup_of_directed_on Sne hS] end subring namespace ring_hom variables [ring T] {s : subring R} open subring /-- Restriction of a ring homomorphism to a subring of the domain. -/ def restrict (f : R →+* S) (s : subring R) : s →+* S := f.comp s.subtype @[simp] lemma restrict_apply (f : R →+* S) (x : s) : f.restrict s x = f x := rfl /-- Restriction of a ring homomorphism to its range interpreted as a subsemiring. -/ def range_restrict (f : R →+* S) : R →+* f.range := f.cod_restrict' f.range $ λ x, ⟨x, subring.mem_top x, rfl⟩ @[simp] lemma coe_range_restrict (f : R →+* S) (x : R) : (f.range_restrict x : S) = f x := rfl lemma range_top_iff_surjective {f : R →+* S} : f.range = (⊤ : subring S) ↔ function.surjective f := subring.ext'_iff.trans $ iff.trans (by rw [coe_range, coe_top]) set.range_iff_surjective /-- The range of a surjective ring homomorphism is the whole of the codomain. -/ lemma range_top_of_surjective (f : R →+* S) (hf : function.surjective f) : f.range = (⊤ : subring S) := range_top_iff_surjective.2 hf /-- The subring of elements `x : R` such that `f x = g x`, i.e., the equalizer of f and g as a subring of R -/ def eq_locus (f g : R →+* S) : subring R := { carrier := {x | f x = g x}, .. (f : R →* S).eq_mlocus g, .. (f : R →+ S).eq_locus g } /-- If two ring homomorphisms are equal on a set, then they are equal on its subring closure. -/ lemma eq_on_set_closure {f g : R →+* S} {s : set R} (h : set.eq_on f g s) : set.eq_on f g (closure s) := show closure s ≤ f.eq_locus g, from closure_le.2 h lemma eq_of_eq_on_set_top {f g : R →+* S} (h : set.eq_on f g (⊤ : subring R)) : f = g := ext $ λ x, h trivial lemma eq_of_eq_on_set_dense {s : set R} (hs : closure s = ⊤) {f g : R →+* S} (h : s.eq_on f g) : f = g := eq_of_eq_on_set_top $ hs ▸ eq_on_set_closure h lemma closure_preimage_le (f : R →+* S) (s : set S) : closure (f ⁻¹' s) ≤ (closure s).comap f := closure_le.2 $ λ x hx, mem_coe.2 $ mem_comap.2 $ subset_closure hx /-- The image under a ring homomorphism of the subring generated by a set equals the subring generated by the image of the set. -/ lemma map_closure (f : R →+* S) (s : set R) : (closure s).map f = closure (f '' s) := le_antisymm (map_le_iff_le_comap.2 $ le_trans (closure_mono $ set.subset_preimage_image _ _) (closure_preimage_le _ _)) (closure_le.2 $ set.image_subset _ subset_closure) end ring_hom namespace subring open ring_hom /-- The ring homomorphism associated to an inclusion of subrings. -/ def inclusion {S T : subring R} (h : S ≤ T) : S →* T := S.subtype.cod_restrict' _ (λ x, h x.2) @[simp] lemma range_subtype (s : subring R) : s.subtype.range = s := ext' $ (coe_srange _).trans subtype.range_coe @[simp] lemma range_fst : (fst R S).srange = ⊤ := (fst R S).srange_top_of_surjective $ prod.fst_surjective @[simp] lemma range_snd : (snd R S).srange = ⊤ := (snd R S).srange_top_of_surjective $ prod.snd_surjective @[simp] lemma prod_bot_sup_bot_prod (s : subring R) (t : subring S) : (s.prod ⊥) ⊔ (prod ⊥ t) = s.prod t := le_antisymm (sup_le (prod_mono_right s bot_le) (prod_mono_left t bot_le)) $ assume p hp, prod.fst_mul_snd p ▸ mul_mem _ ((le_sup_left : s.prod ⊥ ≤ s.prod ⊥ ⊔ prod ⊥ t) ⟨hp.1, mem_coe.2 $ one_mem ⊥⟩) ((le_sup_right : prod ⊥ t ≤ s.prod ⊥ ⊔ prod ⊥ t) ⟨mem_coe.2 $ one_mem ⊥, hp.2⟩) end subring namespace ring_equiv variables {s t : subring R} /-- Makes the identity isomorphism from a proof two subrings of a multiplicative monoid are equal. -/ def subring_congr (h : s = t) : s ≃+* t := { map_mul' := λ _ _, rfl, map_add' := λ _ _, rfl, ..equiv.set_congr $ subring.ext'_iff.1 h } end ring_equiv namespace subring variables {s : set R} local attribute [reducible] closure @[elab_as_eliminator] protected theorem in_closure.rec_on {C : R → Prop} {x : R} (hx : x ∈ closure s) (h1 : C 1) (hneg1 : C (-1)) (hs : ∀ z ∈ s, ∀ n, C n → C (z * n)) (ha : ∀ {x y}, C x → C y → C (x + y)) : C x := begin have h0 : C 0 := add_neg_self (1:R) ▸ ha h1 hneg1, rcases exists_list_of_mem_closure hx with ⟨L, HL, rfl⟩, clear hx, induction L with hd tl ih, { exact h0 }, rw list.forall_mem_cons at HL, suffices : C (list.prod hd), { rw [list.map_cons, list.sum_cons], exact ha this (ih HL.2) }, replace HL := HL.1, clear ih tl, suffices : ∃ L : list R, (∀ x ∈ L, x ∈ s) ∧ (list.prod hd = list.prod L ∨ list.prod hd = -list.prod L), { rcases this with ⟨L, HL', HP | HP⟩, { rw HP, clear HP HL hd, induction L with hd tl ih, { exact h1 }, rw list.forall_mem_cons at HL', rw list.prod_cons, exact hs _ HL'.1 _ (ih HL'.2) }, rw HP, clear HP HL hd, induction L with hd tl ih, { exact hneg1 }, rw [list.prod_cons, neg_mul_eq_mul_neg], rw list.forall_mem_cons at HL', exact hs _ HL'.1 _ (ih HL'.2) }, induction hd with hd tl ih, { exact ⟨[], list.forall_mem_nil _, or.inl rfl⟩ }, rw list.forall_mem_cons at HL, rcases ih HL.2 with ⟨L, HL', HP | HP⟩; cases HL.1 with hhd hhd, { exact ⟨hd :: L, list.forall_mem_cons.2 ⟨hhd, HL'⟩, or.inl $ by rw [list.prod_cons, list.prod_cons, HP]⟩ }, { exact ⟨L, HL', or.inr $ by rw [list.prod_cons, hhd, neg_one_mul, HP]⟩ }, { exact ⟨hd :: L, list.forall_mem_cons.2 ⟨hhd, HL'⟩, or.inr $ by rw [list.prod_cons, list.prod_cons, HP, neg_mul_eq_mul_neg]⟩ }, { exact ⟨L, HL', or.inl $ by rw [list.prod_cons, hhd, HP, neg_one_mul, neg_neg]⟩ } end lemma closure_preimage_le (f : R →+* S) (s : set S) : closure (f ⁻¹' s) ≤ (closure s).comap f := closure_le.2 $ λ x hx, mem_coe.2 $ mem_comap.2 $ subset_closure hx end subring
f8a6787669f979eedc7c43cff323ad1dc26082a9
32da3d0f92cab08875472ef6cacc1931c2b3eafa
/src/set_theory/ordinal_arithmetic.lean
0ca44c40fa746ffb875d2a38fae1c347191c5ac4
[ "Apache-2.0" ]
permissive
karthiknadig/mathlib
b6073c3748860bfc9a3e55da86afcddba62dc913
33a86cfff12d7f200d0010cd03b95e9b69a6c1a5
refs/heads/master
1,676,389,371,851
1,610,061,127,000
1,610,061,127,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
69,921
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro -/ import set_theory.ordinal /-! # Ordinal arithmetic Ordinals have an addition (corresponding to disjoint union) that turns them into an additive monoid, and a multiplication (corresponding to the lexicographic order on the product) that turns them into a monoid. One can also define correspondingly a subtraction, a division, a successor function, a power function and a logarithm function. We also define limit ordinals and prove the basic induction principle on ordinals separating successor ordinals and limit ordinals, in `limit_rec_on`. ## Main definitions and results * `o₁ + o₂` is the order on the disjoint union of `o₁` and `o₂` obtained by declaring that every element of `o₁` is smaller than every element of `o₂`. * `o₁ - o₂` is the unique ordinal `o` such that `o₂ + o = o₁`, when `o₂ ≤ o₁`. * `o₁ * o₂` is the lexicographic order on `o₂ × o₁`. * `o₁ / o₂` is the ordinal `o` such that `o₁ = o₂ * o + o'` with `o' < o₂`. We also define the divisibility predicate, and a modulo operation. * `succ o = o + 1` is the successor of `o`. * `pred o` if the predecessor of `o`. If `o` is not a successor, we set `pred o = o`. We also define the power function and the logarithm function on ordinals, and discuss the properties of casts of natural numbers of and of `omega` with respect to these operations. Some properties of the operations are also used to discuss general tools on ordinals: * `is_limit o`: an ordinal is a limit ordinal if it is neither `0` nor a successor. * `limit_rec_on` is the main induction principle of ordinals: if one can prove a property by induction at successor ordinals and at limit ordinals, then it holds for all ordinals. * `is_normal`: a function `f : ordinal → ordinal` satisfies `is_normal` if it is strictly increasing and order-continuous, i.e., the image `f o` of a limit ordinal `o` is the sup of `f a` for `a < o`. * `nfp f a`: the next fixed point of a function `f` on ordinals, above `a`. It behaves well for normal functions. * `CNF b o` is the Cantor normal form of the ordinal `o` in base `b`. * `sup`: the supremum of an indexed family of ordinals in `Type u`, as an ordinal in `Type u`. * `bsup`: the supremum of a set of ordinals indexed by ordinals less than a given ordinal `o`. -/ noncomputable theory open function cardinal set equiv open_locale classical cardinal universes u v w variables {α : Type*} {β : Type*} {γ : Type*} {r : α → α → Prop} {s : β → β → Prop} {t : γ → γ → Prop} namespace ordinal /-! ### Further properties of addition on ordinals -/ @[simp] theorem lift_add (a b) : lift (a + b) = lift a + lift b := quotient.induction_on₂ a b $ λ ⟨α, r, _⟩ ⟨β, s, _⟩, quotient.sound ⟨(rel_iso.preimage equiv.ulift _).trans (rel_iso.sum_lex_congr (rel_iso.preimage equiv.ulift _) (rel_iso.preimage equiv.ulift _)).symm⟩ @[simp] theorem lift_succ (a) : lift (succ a) = succ (lift a) := by unfold succ; simp only [lift_add, lift_one] theorem add_le_add_iff_left (a) {b c : ordinal} : a + b ≤ a + c ↔ b ≤ c := ⟨induction_on a $ λ α r hr, induction_on b $ λ β₁ s₁ hs₁, induction_on c $ λ β₂ s₂ hs₂ ⟨f⟩, ⟨ have fl : ∀ a, f (sum.inl a) = sum.inl a := λ a, by simpa only [initial_seg.trans_apply, initial_seg.le_add_apply] using @initial_seg.eq _ _ _ _ (@sum.lex.is_well_order _ _ _ _ hr hs₂) ((initial_seg.le_add r s₁).trans f) (initial_seg.le_add r s₂) a, have ∀ b, {b' // f (sum.inr b) = sum.inr b'}, begin intro b, cases e : f (sum.inr b), { rw ← fl at e, have := f.inj' e, contradiction }, { exact ⟨_, rfl⟩ } end, let g (b) := (this b).1 in have fr : ∀ b, f (sum.inr b) = sum.inr (g b), from λ b, (this b).2, ⟨⟨⟨g, λ x y h, by injection f.inj' (by rw [fr, fr, h] : f (sum.inr x) = f (sum.inr y))⟩, λ a b, by simpa only [sum.lex_inr_inr, fr, rel_embedding.coe_fn_to_embedding, initial_seg.coe_fn_to_rel_embedding, function.embedding.coe_fn_mk] using @rel_embedding.map_rel_iff _ _ _ _ f.to_rel_embedding (sum.inr a) (sum.inr b)⟩, λ a b H, begin rcases f.init' (by rw fr; exact sum.lex_inr_inr.2 H) with ⟨a'|a', h⟩, { rw fl at h, cases h }, { rw fr at h, exact ⟨a', sum.inr.inj h⟩ } end⟩⟩, λ h, add_le_add_left h _⟩ theorem add_succ (o₁ o₂ : ordinal) : o₁ + succ o₂ = succ (o₁ + o₂) := (add_assoc _ _ _).symm @[simp] theorem succ_zero : succ 0 = 1 := zero_add _ theorem one_le_iff_pos {o : ordinal} : 1 ≤ o ↔ 0 < o := by rw [← succ_zero, succ_le] theorem one_le_iff_ne_zero {o : ordinal} : 1 ≤ o ↔ o ≠ 0 := by rw [one_le_iff_pos, pos_iff_ne_zero] theorem succ_pos (o : ordinal) : 0 < succ o := lt_of_le_of_lt (zero_le _) (lt_succ_self _) theorem succ_ne_zero (o : ordinal) : succ o ≠ 0 := ne_of_gt $ succ_pos o @[simp] theorem card_succ (o : ordinal) : card (succ o) = card o + 1 := by simp only [succ, card_add, card_one] theorem nat_cast_succ (n : ℕ) : (succ n : ordinal) = n.succ := rfl theorem add_left_cancel (a) {b c : ordinal} : a + b = a + c ↔ b = c := by simp only [le_antisymm_iff, add_le_add_iff_left] theorem lt_succ {a b : ordinal} : a < succ b ↔ a ≤ b := by rw [← not_le, succ_le, not_lt] theorem add_lt_add_iff_left (a) {b c : ordinal} : a + b < a + c ↔ b < c := by rw [← not_le, ← not_le, add_le_add_iff_left] theorem lt_of_add_lt_add_right {a b c : ordinal} : a + b < c + b → a < c := lt_imp_lt_of_le_imp_le (λ h, add_le_add_right h _) @[simp] theorem succ_lt_succ {a b : ordinal} : succ a < succ b ↔ a < b := by rw [lt_succ, succ_le] @[simp] theorem succ_le_succ {a b : ordinal} : succ a ≤ succ b ↔ a ≤ b := le_iff_le_iff_lt_iff_lt.2 succ_lt_succ theorem succ_inj {a b : ordinal} : succ a = succ b ↔ a = b := by simp only [le_antisymm_iff, succ_le_succ] theorem add_le_add_iff_right {a b : ordinal} (n : ℕ) : a + n ≤ b + n ↔ a ≤ b := by induction n with n ih; [rw [nat.cast_zero, add_zero, add_zero], rw [← nat_cast_succ, add_succ, add_succ, succ_le_succ, ih]] theorem add_right_cancel {a b : ordinal} (n : ℕ) : a + n = b + n ↔ a = b := by simp only [le_antisymm_iff, add_le_add_iff_right] /-! ### The zero ordinal -/ @[simp] theorem card_eq_zero {o} : card o = 0 ↔ o = 0 := ⟨induction_on o $ λ α r _ h, begin refine le_antisymm (le_of_not_lt $ λ hn, ne_zero_iff_nonempty.2 _ h) (zero_le _), rw [← succ_le, succ_zero] at hn, cases hn with f, exact ⟨f punit.star⟩ end, λ e, by simp only [e, card_zero]⟩ theorem type_ne_zero_iff_nonempty [is_well_order α r] : type r ≠ 0 ↔ nonempty α := (not_congr (@card_eq_zero (type r))).symm.trans ne_zero_iff_nonempty @[simp] theorem type_eq_zero_iff_empty [is_well_order α r] : type r = 0 ↔ ¬ nonempty α := (not_iff_comm.1 type_ne_zero_iff_nonempty).symm protected lemma one_ne_zero : (1 : ordinal) ≠ 0 := type_ne_zero_iff_nonempty.2 ⟨punit.star⟩ instance : nontrivial ordinal.{u} := ⟨⟨1, 0, ordinal.one_ne_zero⟩⟩ theorem zero_lt_one : (0 : ordinal) < 1 := lt_iff_le_and_ne.2 ⟨zero_le _, ne.symm $ ordinal.one_ne_zero⟩ /-! ### The predecessor of an ordinal -/ /-- The ordinal predecessor of `o` is `o'` if `o = succ o'`, and `o` otherwise. -/ def pred (o : ordinal.{u}) : ordinal.{u} := if h : ∃ a, o = succ a then classical.some h else o @[simp] theorem pred_succ (o) : pred (succ o) = o := by have h : ∃ a, succ o = succ a := ⟨_, rfl⟩; simpa only [pred, dif_pos h] using (succ_inj.1 $ classical.some_spec h).symm theorem pred_le_self (o) : pred o ≤ o := if h : ∃ a, o = succ a then let ⟨a, e⟩ := h in by rw [e, pred_succ]; exact le_of_lt (lt_succ_self _) else by rw [pred, dif_neg h] theorem pred_eq_iff_not_succ {o} : pred o = o ↔ ¬ ∃ a, o = succ a := ⟨λ e ⟨a, e'⟩, by rw [e', pred_succ] at e; exact ne_of_lt (lt_succ_self _) e, λ h, dif_neg h⟩ theorem pred_lt_iff_is_succ {o} : pred o < o ↔ ∃ a, o = succ a := iff.trans (by simp only [le_antisymm_iff, pred_le_self, true_and, not_le]) (iff_not_comm.1 pred_eq_iff_not_succ).symm theorem succ_pred_iff_is_succ {o} : succ (pred o) = o ↔ ∃ a, o = succ a := ⟨λ e, ⟨_, e.symm⟩, λ ⟨a, e⟩, by simp only [e, pred_succ]⟩ theorem succ_lt_of_not_succ {o} (h : ¬ ∃ a, o = succ a) {b} : succ b < o ↔ b < o := ⟨lt_trans (lt_succ_self _), λ l, lt_of_le_of_ne (succ_le.2 l) (λ e, h ⟨_, e.symm⟩)⟩ theorem lt_pred {a b} : a < pred b ↔ succ a < b := if h : ∃ a, b = succ a then let ⟨c, e⟩ := h in by rw [e, pred_succ, succ_lt_succ] else by simp only [pred, dif_neg h, succ_lt_of_not_succ h] theorem pred_le {a b} : pred a ≤ b ↔ a ≤ succ b := le_iff_le_iff_lt_iff_lt.2 lt_pred @[simp] theorem lift_is_succ {o} : (∃ a, lift o = succ a) ↔ (∃ a, o = succ a) := ⟨λ ⟨a, h⟩, let ⟨b, e⟩ := lift_down $ show a ≤ lift o, from le_of_lt $ h.symm ▸ lt_succ_self _ in ⟨b, lift_inj.1 $ by rw [h, ← e, lift_succ]⟩, λ ⟨a, h⟩, ⟨lift a, by simp only [h, lift_succ]⟩⟩ @[simp] theorem lift_pred (o) : lift (pred o) = pred (lift o) := if h : ∃ a, o = succ a then by cases h with a e; simp only [e, pred_succ, lift_succ] else by rw [pred_eq_iff_not_succ.2 h, pred_eq_iff_not_succ.2 (mt lift_is_succ.1 h)] /-! ### Limit ordinals -/ /-- A limit ordinal is an ordinal which is not zero and not a successor. -/ def is_limit (o : ordinal) : Prop := o ≠ 0 ∧ ∀ a < o, succ a < o theorem not_zero_is_limit : ¬ is_limit 0 | ⟨h, _⟩ := h rfl theorem not_succ_is_limit (o) : ¬ is_limit (succ o) | ⟨_, h⟩ := lt_irrefl _ (h _ (lt_succ_self _)) theorem not_succ_of_is_limit {o} (h : is_limit o) : ¬ ∃ a, o = succ a | ⟨a, e⟩ := not_succ_is_limit a (e ▸ h) theorem succ_lt_of_is_limit {o} (h : is_limit o) {a} : succ a < o ↔ a < o := ⟨lt_trans (lt_succ_self _), h.2 _⟩ theorem le_succ_of_is_limit {o} (h : is_limit o) {a} : o ≤ succ a ↔ o ≤ a := le_iff_le_iff_lt_iff_lt.2 $ succ_lt_of_is_limit h theorem limit_le {o} (h : is_limit o) {a} : o ≤ a ↔ ∀ x < o, x ≤ a := ⟨λ h x l, le_trans (le_of_lt l) h, λ H, (le_succ_of_is_limit h).1 $ le_of_not_lt $ λ hn, not_lt_of_le (H _ hn) (lt_succ_self _)⟩ theorem lt_limit {o} (h : is_limit o) {a} : a < o ↔ ∃ x < o, a < x := by simpa only [not_ball, not_le] using not_congr (@limit_le _ h a) @[simp] theorem lift_is_limit (o) : is_limit (lift o) ↔ is_limit o := and_congr (not_congr $ by simpa only [lift_zero] using @lift_inj o 0) ⟨λ H a h, lift_lt.1 $ by simpa only [lift_succ] using H _ (lift_lt.2 h), λ H a h, let ⟨a', e⟩ := lift_down (le_of_lt h) in by rw [← e, ← lift_succ, lift_lt]; rw [← e, lift_lt] at h; exact H a' h⟩ theorem is_limit.pos {o : ordinal} (h : is_limit o) : 0 < o := lt_of_le_of_ne (zero_le _) h.1.symm theorem is_limit.one_lt {o : ordinal} (h : is_limit o) : 1 < o := by simpa only [succ_zero] using h.2 _ h.pos theorem is_limit.nat_lt {o : ordinal} (h : is_limit o) : ∀ n : ℕ, (n : ordinal) < o | 0 := h.pos | (n+1) := h.2 _ (is_limit.nat_lt n) theorem zero_or_succ_or_limit (o : ordinal) : o = 0 ∨ (∃ a, o = succ a) ∨ is_limit o := if o0 : o = 0 then or.inl o0 else if h : ∃ a, o = succ a then or.inr (or.inl h) else or.inr $ or.inr ⟨o0, λ a, (succ_lt_of_not_succ h).2⟩ /-- Main induction principle of ordinals: if one can prove a property by induction at successor ordinals and at limit ordinals, then it holds for all ordinals. -/ @[elab_as_eliminator] def limit_rec_on {C : ordinal → Sort*} (o : ordinal) (H₁ : C 0) (H₂ : ∀ o, C o → C (succ o)) (H₃ : ∀ o, is_limit o → (∀ o' < o, C o') → C o) : C o := wf.fix (λ o IH, if o0 : o = 0 then by rw o0; exact H₁ else if h : ∃ a, o = succ a then by rw ← succ_pred_iff_is_succ.2 h; exact H₂ _ (IH _ $ pred_lt_iff_is_succ.2 h) else H₃ _ ⟨o0, λ a, (succ_lt_of_not_succ h).2⟩ IH) o @[simp] theorem limit_rec_on_zero {C} (H₁ H₂ H₃) : @limit_rec_on C 0 H₁ H₂ H₃ = H₁ := by rw [limit_rec_on, well_founded.fix_eq, dif_pos rfl]; refl @[simp] theorem limit_rec_on_succ {C} (o H₁ H₂ H₃) : @limit_rec_on C (succ o) H₁ H₂ H₃ = H₂ o (@limit_rec_on C o H₁ H₂ H₃) := begin have h : ∃ a, succ o = succ a := ⟨_, rfl⟩, rw [limit_rec_on, well_founded.fix_eq, dif_neg (succ_ne_zero o), dif_pos h], generalize : limit_rec_on._proof_2 (succ o) h = h₂, generalize : limit_rec_on._proof_3 (succ o) h = h₃, revert h₂ h₃, generalize e : pred (succ o) = o', intros, rw pred_succ at e, subst o', refl end @[simp] theorem limit_rec_on_limit {C} (o H₁ H₂ H₃ h) : @limit_rec_on C o H₁ H₂ H₃ = H₃ o h (λ x h, @limit_rec_on C x H₁ H₂ H₃) := by rw [limit_rec_on, well_founded.fix_eq, dif_neg h.1, dif_neg (not_succ_of_is_limit h)]; refl lemma has_succ_of_is_limit {α} {r : α → α → Prop} [wo : is_well_order α r] (h : (type r).is_limit) (x : α) : ∃y, r x y := begin use enum r (typein r x).succ (h.2 _ (typein_lt_type r x)), convert (enum_lt (typein_lt_type r x) _).mpr (lt_succ_self _), rw [enum_typein] end lemma type_subrel_lt (o : ordinal.{u}) : type (subrel (<) {o' : ordinal | o' < o}) = ordinal.lift.{u u+1} o := begin refine quotient.induction_on o _, rintro ⟨α, r, wo⟩, resetI, apply quotient.sound, constructor, symmetry, refine (rel_iso.preimage equiv.ulift r).trans (typein_iso r) end lemma mk_initial_seg (o : ordinal.{u}) : #{o' : ordinal | o' < o} = cardinal.lift.{u u+1} o.card := by rw [lift_card, ←type_subrel_lt, card_type] /-! ### Normal ordinal functions -/ /-- A normal ordinal function is a strictly increasing function which is order-continuous, i.e., the image `f o` of a limit ordinal `o` is the sup of `f a` for `a < o`. -/ def is_normal (f : ordinal → ordinal) : Prop := (∀ o, f o < f (succ o)) ∧ ∀ o, is_limit o → ∀ a, f o ≤ a ↔ ∀ b < o, f b ≤ a theorem is_normal.limit_le {f} (H : is_normal f) : ∀ {o}, is_limit o → ∀ {a}, f o ≤ a ↔ ∀ b < o, f b ≤ a := H.2 theorem is_normal.limit_lt {f} (H : is_normal f) {o} (h : is_limit o) {a} : a < f o ↔ ∃ b < o, a < f b := not_iff_not.1 $ by simpa only [exists_prop, not_exists, not_and, not_lt] using H.2 _ h a theorem is_normal.lt_iff {f} (H : is_normal f) {a b} : f a < f b ↔ a < b := strict_mono.lt_iff_lt $ λ a b, limit_rec_on b (not.elim (not_lt_of_le $ zero_le _)) (λ b IH h, (lt_or_eq_of_le (lt_succ.1 h)).elim (λ h, lt_trans (IH h) (H.1 _)) (λ e, e ▸ H.1 _)) (λ b l IH h, lt_of_lt_of_le (H.1 a) ((H.2 _ l _).1 (le_refl _) _ (l.2 _ h))) theorem is_normal.le_iff {f} (H : is_normal f) {a b} : f a ≤ f b ↔ a ≤ b := le_iff_le_iff_lt_iff_lt.2 H.lt_iff theorem is_normal.inj {f} (H : is_normal f) {a b} : f a = f b ↔ a = b := by simp only [le_antisymm_iff, H.le_iff] theorem is_normal.le_self {f} (H : is_normal f) (a) : a ≤ f a := limit_rec_on a (zero_le _) (λ a IH, succ_le.2 $ lt_of_le_of_lt IH (H.1 _)) (λ a l IH, (limit_le l).2 $ λ b h, le_trans (IH b h) $ H.le_iff.2 $ le_of_lt h) theorem is_normal.le_set {f} (H : is_normal f) (p : ordinal → Prop) (p0 : ∃ x, p x) (S) (H₂ : ∀ o, S ≤ o ↔ ∀ a, p a → a ≤ o) {o} : f S ≤ o ↔ ∀ a, p a → f a ≤ o := ⟨λ h a pa, le_trans (H.le_iff.2 ((H₂ _).1 (le_refl _) _ pa)) h, λ h, begin revert H₂, apply limit_rec_on S, { intro H₂, cases p0 with x px, have := le_zero.1 ((H₂ _).1 (zero_le _) _ px), rw this at px, exact h _ px }, { intros S _ H₂, rcases not_ball.1 (mt (H₂ S).2 $ not_le_of_lt $ lt_succ_self _) with ⟨a, h₁, h₂⟩, exact le_trans (H.le_iff.2 $ succ_le.2 $ not_le.1 h₂) (h _ h₁) }, { intros S L _ H₂, apply (H.2 _ L _).2, intros a h', rcases not_ball.1 (mt (H₂ a).2 (not_le.2 h')) with ⟨b, h₁, h₂⟩, exact le_trans (H.le_iff.2 $ le_of_lt $ not_le.1 h₂) (h _ h₁) } end⟩ theorem is_normal.le_set' {f} (H : is_normal f) (p : α → Prop) (g : α → ordinal) (p0 : ∃ x, p x) (S) (H₂ : ∀ o, S ≤ o ↔ ∀ a, p a → g a ≤ o) {o} : f S ≤ o ↔ ∀ a, p a → f (g a) ≤ o := (H.le_set (λ x, ∃ y, p y ∧ x = g y) (let ⟨x, px⟩ := p0 in ⟨_, _, px, rfl⟩) _ (λ o, (H₂ o).trans ⟨λ H a ⟨y, h1, h2⟩, h2.symm ▸ H y h1, λ H a h1, H (g a) ⟨a, h1, rfl⟩⟩)).trans ⟨λ H a h, H (g a) ⟨a, h, rfl⟩, λ H a ⟨y, h1, h2⟩, h2.symm ▸ H y h1⟩ theorem is_normal.refl : is_normal id := ⟨λ x, lt_succ_self _, λ o l a, limit_le l⟩ theorem is_normal.trans {f g} (H₁ : is_normal f) (H₂ : is_normal g) : is_normal (λ x, f (g x)) := ⟨λ x, H₁.lt_iff.2 (H₂.1 _), λ o l a, H₁.le_set' (< o) g ⟨_, l.pos⟩ _ (λ c, H₂.2 _ l _)⟩ theorem is_normal.is_limit {f} (H : is_normal f) {o} (l : is_limit o) : is_limit (f o) := ⟨ne_of_gt $ lt_of_le_of_lt (zero_le _) $ H.lt_iff.2 l.pos, λ a h, let ⟨b, h₁, h₂⟩ := (H.limit_lt l).1 h in lt_of_le_of_lt (succ_le.2 h₂) (H.lt_iff.2 h₁)⟩ theorem add_le_of_limit {a b c : ordinal.{u}} (h : is_limit b) : a + b ≤ c ↔ ∀ b' < b, a + b' ≤ c := ⟨λ h b' l, le_trans (add_le_add_left (le_of_lt l) _) h, λ H, le_of_not_lt $ induction_on a (λ α r _, induction_on b $ λ β s _ h H l, begin resetI, suffices : ∀ x : β, sum.lex r s (sum.inr x) (enum _ _ l), { cases enum _ _ l with x x, { cases this (enum s 0 h.pos) }, { exact irrefl _ (this _) } }, intros x, rw [← typein_lt_typein (sum.lex r s), typein_enum], have := H _ (h.2 _ (typein_lt_type s x)), rw [add_succ, succ_le] at this, refine lt_of_le_of_lt (type_le'.2 ⟨rel_embedding.of_monotone (λ a, _) (λ a b, _)⟩) this, { rcases a with ⟨a | b, h⟩, { exact sum.inl a }, { exact sum.inr ⟨b, by cases h; assumption⟩ } }, { rcases a with ⟨a | a, h₁⟩; rcases b with ⟨b | b, h₂⟩; cases h₁; cases h₂; rintro ⟨⟩; constructor; assumption } end) h H⟩ theorem add_is_normal (a : ordinal) : is_normal ((+) a) := ⟨λ b, (add_lt_add_iff_left a).2 (lt_succ_self _), λ b l c, add_le_of_limit l⟩ theorem add_is_limit (a) {b} : is_limit b → is_limit (a + b) := (add_is_normal a).is_limit /-! ### Subtraction on ordinals-/ /-- `a - b` is the unique ordinal satisfying `b + (a - b) = a` when `b ≤ a`. -/ def sub (a b : ordinal.{u}) : ordinal.{u} := omin {o | a ≤ b+o} ⟨a, le_add_left _ _⟩ instance : has_sub ordinal := ⟨sub⟩ theorem le_add_sub (a b : ordinal) : a ≤ b + (a - b) := omin_mem {o | a ≤ b+o} _ theorem sub_le {a b c : ordinal} : a - b ≤ c ↔ a ≤ b + c := ⟨λ h, le_trans (le_add_sub a b) (add_le_add_left h _), λ h, omin_le h⟩ theorem lt_sub {a b c : ordinal} : a < b - c ↔ c + a < b := lt_iff_lt_of_le_iff_le sub_le theorem add_sub_cancel (a b : ordinal) : a + b - a = b := le_antisymm (sub_le.2 $ le_refl _) ((add_le_add_iff_left a).1 $ le_add_sub _ _) theorem sub_eq_of_add_eq {a b c : ordinal} (h : a + b = c) : c - a = b := h ▸ add_sub_cancel _ _ theorem sub_le_self (a b : ordinal) : a - b ≤ a := sub_le.2 $ le_add_left _ _ theorem add_sub_cancel_of_le {a b : ordinal} (h : b ≤ a) : b + (a - b) = a := le_antisymm begin rcases zero_or_succ_or_limit (a-b) with e|⟨c,e⟩|l, { simp only [e, add_zero, h] }, { rw [e, add_succ, succ_le, ← lt_sub, e], apply lt_succ_self }, { exact (add_le_of_limit l).2 (λ c l, le_of_lt (lt_sub.1 l)) } end (le_add_sub _ _) @[simp] theorem sub_zero (a : ordinal) : a - 0 = a := by simpa only [zero_add] using add_sub_cancel 0 a @[simp] theorem zero_sub (a : ordinal) : 0 - a = 0 := by rw ← le_zero; apply sub_le_self @[simp] theorem sub_self (a : ordinal) : a - a = 0 := by simpa only [add_zero] using add_sub_cancel a 0 theorem sub_eq_zero_iff_le {a b : ordinal} : a - b = 0 ↔ a ≤ b := ⟨λ h, by simpa only [h, add_zero] using le_add_sub a b, λ h, by rwa [← le_zero, sub_le, add_zero]⟩ theorem sub_sub (a b c : ordinal) : a - b - c = a - (b + c) := eq_of_forall_ge_iff $ λ d, by rw [sub_le, sub_le, sub_le, add_assoc] theorem add_sub_add_cancel (a b c : ordinal) : a + b - (a + c) = b - c := by rw [← sub_sub, add_sub_cancel] theorem sub_is_limit {a b} (l : is_limit a) (h : b < a) : is_limit (a - b) := ⟨ne_of_gt $ lt_sub.2 $ by rwa add_zero, λ c h, by rw [lt_sub, add_succ]; exact l.2 _ (lt_sub.1 h)⟩ @[simp] theorem one_add_omega : 1 + omega.{u} = omega := begin refine le_antisymm _ (le_add_left _ _), rw [omega, one_eq_lift_type_unit, ← lift_add, lift_le, type_add], have : is_well_order unit empty_relation := by apply_instance, refine ⟨rel_embedding.collapse (rel_embedding.of_monotone _ _)⟩, { apply sum.rec, exact λ _, 0, exact nat.succ }, { intros a b, cases a; cases b; intro H; cases H with _ _ H _ _ H; [cases H, exact nat.succ_pos _, exact nat.succ_lt_succ H] } end @[simp, priority 990] theorem one_add_of_omega_le {o} (h : omega ≤ o) : 1 + o = o := by rw [← add_sub_cancel_of_le h, ← add_assoc, one_add_omega] /-! ### Multiplication of ordinals-/ /-- The multiplication of ordinals `o₁` and `o₂` is the (well founded) lexicographic order on `o₂ × o₁`. -/ instance : monoid ordinal.{u} := { mul := λ a b, quotient.lift_on₂ a b (λ ⟨α, r, wo⟩ ⟨β, s, wo'⟩, ⟦⟨β × α, prod.lex s r, by exactI prod.lex.is_well_order⟩⟧ : Well_order → Well_order → ordinal) $ λ ⟨α₁, r₁, o₁⟩ ⟨α₂, r₂, o₂⟩ ⟨β₁, s₁, p₁⟩ ⟨β₂, s₂, p₂⟩ ⟨f⟩ ⟨g⟩, quot.sound ⟨rel_iso.prod_lex_congr g f⟩, one := 1, mul_assoc := λ a b c, quotient.induction_on₃ a b c $ λ ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨γ, t, _⟩, eq.symm $ quotient.sound ⟨⟨prod_assoc _ _ _, λ a b, begin rcases a with ⟨⟨a₁, a₂⟩, a₃⟩, rcases b with ⟨⟨b₁, b₂⟩, b₃⟩, simp [prod.lex_def, and_or_distrib_left, or_assoc, and_assoc] end⟩⟩, mul_one := λ a, induction_on a $ λ α r _, quotient.sound ⟨⟨punit_prod _, λ a b, by rcases a with ⟨⟨⟨⟩⟩, a⟩; rcases b with ⟨⟨⟨⟩⟩, b⟩; simp only [prod.lex_def, empty_relation, false_or]; simp only [eq_self_iff_true, true_and]; refl⟩⟩, one_mul := λ a, induction_on a $ λ α r _, quotient.sound ⟨⟨prod_punit _, λ a b, by rcases a with ⟨a, ⟨⟨⟩⟩⟩; rcases b with ⟨b, ⟨⟨⟩⟩⟩; simp only [prod.lex_def, empty_relation, and_false, or_false]; refl⟩⟩ } @[simp] theorem type_mul {α β : Type u} (r : α → α → Prop) (s : β → β → Prop) [is_well_order α r] [is_well_order β s] : type r * type s = type (prod.lex s r) := rfl @[simp] theorem lift_mul (a b) : lift (a * b) = lift a * lift b := quotient.induction_on₂ a b $ λ ⟨α, r, _⟩ ⟨β, s, _⟩, quotient.sound ⟨(rel_iso.preimage equiv.ulift _).trans (rel_iso.prod_lex_congr (rel_iso.preimage equiv.ulift _) (rel_iso.preimage equiv.ulift _)).symm⟩ @[simp] theorem card_mul (a b) : card (a * b) = card a * card b := quotient.induction_on₂ a b $ λ ⟨α, r, _⟩ ⟨β, s, _⟩, mul_comm (mk β) (mk α) @[simp] theorem mul_zero (a : ordinal) : a * 0 = 0 := induction_on a $ λ α _ _, by exactI type_eq_zero_iff_empty.2 (λ ⟨⟨e, _⟩⟩, e.elim) @[simp] theorem zero_mul (a : ordinal) : 0 * a = 0 := induction_on a $ λ α _ _, by exactI type_eq_zero_iff_empty.2 (λ ⟨⟨_, e⟩⟩, e.elim) theorem mul_add (a b c : ordinal) : a * (b + c) = a * b + a * c := quotient.induction_on₃ a b c $ λ ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨γ, t, _⟩, quotient.sound ⟨⟨sum_prod_distrib _ _ _, begin rintro ⟨a₁|a₁, a₂⟩ ⟨b₁|b₁, b₂⟩; simp only [prod.lex_def, sum.lex_inl_inl, sum.lex.sep, sum.lex_inr_inl, sum.lex_inr_inr, sum_prod_distrib_apply_left, sum_prod_distrib_apply_right]; simp only [sum.inl.inj_iff, true_or, false_and, false_or] end⟩⟩ @[simp] theorem mul_add_one (a b : ordinal) : a * (b + 1) = a * b + a := by simp only [mul_add, mul_one] @[simp] theorem mul_succ (a b : ordinal) : a * succ b = a * b + a := mul_add_one _ _ theorem mul_le_mul_left {a b} (c : ordinal) : a ≤ b → c * a ≤ c * b := quotient.induction_on₃ a b c $ λ ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨γ, t, _⟩ ⟨f⟩, begin resetI, refine type_le'.2 ⟨rel_embedding.of_monotone (λ a, (f a.1, a.2)) (λ a b h, _)⟩, clear_, cases h with a₁ b₁ a₂ b₂ h' a b₁ b₂ h', { exact prod.lex.left _ _ (f.to_rel_embedding.map_rel_iff.2 h') }, { exact prod.lex.right _ h' } end theorem mul_le_mul_right {a b} (c : ordinal) : a ≤ b → a * c ≤ b * c := quotient.induction_on₃ a b c $ λ ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨γ, t, _⟩ ⟨f⟩, begin resetI, refine type_le'.2 ⟨rel_embedding.of_monotone (λ a, (a.1, f a.2)) (λ a b h, _)⟩, cases h with a₁ b₁ a₂ b₂ h' a b₁ b₂ h', { exact prod.lex.left _ _ h' }, { exact prod.lex.right _ (f.to_rel_embedding.map_rel_iff.2 h') } end theorem mul_le_mul {a b c d : ordinal} (h₁ : a ≤ c) (h₂ : b ≤ d) : a * b ≤ c * d := le_trans (mul_le_mul_left _ h₂) (mul_le_mul_right _ h₁) private lemma mul_le_of_limit_aux {α β r s} [is_well_order α r] [is_well_order β s] {c} (h : is_limit (type s)) (H : ∀ b' < type s, type r * b' ≤ c) (l : c < type r * type s) : false := begin suffices : ∀ a b, prod.lex s r (b, a) (enum _ _ l), { cases enum _ _ l with b a, exact irrefl _ (this _ _) }, intros a b, rw [← typein_lt_typein (prod.lex s r), typein_enum], have := H _ (h.2 _ (typein_lt_type s b)), rw [mul_succ] at this, have := lt_of_lt_of_le ((add_lt_add_iff_left _).2 (typein_lt_type _ a)) this, refine lt_of_le_of_lt _ this, refine (type_le'.2 _), constructor, refine rel_embedding.of_monotone (λ a, _) (λ a b, _), { rcases a with ⟨⟨b', a'⟩, h⟩, by_cases e : b = b', { refine sum.inr ⟨a', _⟩, subst e, cases h with _ _ _ _ h _ _ _ h, { exact (irrefl _ h).elim }, { exact h } }, { refine sum.inl (⟨b', _⟩, a'), cases h with _ _ _ _ h _ _ _ h, { exact h }, { exact (e rfl).elim } } }, { rcases a with ⟨⟨b₁, a₁⟩, h₁⟩, rcases b with ⟨⟨b₂, a₂⟩, h₂⟩, intro h, by_cases e₁ : b = b₁; by_cases e₂ : b = b₂, { substs b₁ b₂, simpa only [subrel_val, prod.lex_def, @irrefl _ s _ b, true_and, false_or, eq_self_iff_true, dif_pos, sum.lex_inr_inr] using h }, { subst b₁, simp only [subrel_val, prod.lex_def, e₂, prod.lex_def, dif_pos, subrel_val, eq_self_iff_true, or_false, dif_neg, not_false_iff, sum.lex_inr_inl, false_and] at h ⊢, cases h₂; [exact asymm h h₂_h, exact e₂ rfl] }, { simp only [e₂, dif_pos, eq_self_iff_true, dif_neg e₁, not_false_iff, sum.lex.sep] }, { simpa only [dif_neg e₁, dif_neg e₂, prod.lex_def, subrel_val, subtype.mk_eq_mk, sum.lex_inl_inl] using h } } end theorem mul_le_of_limit {a b c : ordinal.{u}} (h : is_limit b) : a * b ≤ c ↔ ∀ b' < b, a * b' ≤ c := ⟨λ h b' l, le_trans (mul_le_mul_left _ (le_of_lt l)) h, λ H, le_of_not_lt $ induction_on a (λ α r _, induction_on b $ λ β s _, by exactI mul_le_of_limit_aux) h H⟩ theorem mul_is_normal {a : ordinal} (h : 0 < a) : is_normal ((*) a) := ⟨λ b, by rw mul_succ; simpa only [add_zero] using (add_lt_add_iff_left (a*b)).2 h, λ b l c, mul_le_of_limit l⟩ theorem lt_mul_of_limit {a b c : ordinal.{u}} (h : is_limit c) : a < b * c ↔ ∃ c' < c, a < b * c' := by simpa only [not_ball, not_le] using not_congr (@mul_le_of_limit b c a h) theorem mul_lt_mul_iff_left {a b c : ordinal} (a0 : 0 < a) : a * b < a * c ↔ b < c := (mul_is_normal a0).lt_iff theorem mul_le_mul_iff_left {a b c : ordinal} (a0 : 0 < a) : a * b ≤ a * c ↔ b ≤ c := (mul_is_normal a0).le_iff theorem mul_lt_mul_of_pos_left {a b c : ordinal} (h : a < b) (c0 : 0 < c) : c * a < c * b := (mul_lt_mul_iff_left c0).2 h theorem mul_pos {a b : ordinal} (h₁ : 0 < a) (h₂ : 0 < b) : 0 < a * b := by simpa only [mul_zero] using mul_lt_mul_of_pos_left h₂ h₁ theorem mul_ne_zero {a b : ordinal} : a ≠ 0 → b ≠ 0 → a * b ≠ 0 := by simpa only [pos_iff_ne_zero] using mul_pos theorem le_of_mul_le_mul_left {a b c : ordinal} (h : c * a ≤ c * b) (h0 : 0 < c) : a ≤ b := le_imp_le_of_lt_imp_lt (λ h', mul_lt_mul_of_pos_left h' h0) h theorem mul_right_inj {a b c : ordinal} (a0 : 0 < a) : a * b = a * c ↔ b = c := (mul_is_normal a0).inj theorem mul_is_limit {a b : ordinal} (a0 : 0 < a) : is_limit b → is_limit (a * b) := (mul_is_normal a0).is_limit theorem mul_is_limit_left {a b : ordinal} (l : is_limit a) (b0 : 0 < b) : is_limit (a * b) := begin rcases zero_or_succ_or_limit b with rfl|⟨b,rfl⟩|lb, { exact (lt_irrefl _).elim b0 }, { rw mul_succ, exact add_is_limit _ l }, { exact mul_is_limit l.pos lb } end /-! ### Division on ordinals -/ protected lemma div_aux (a b : ordinal.{u}) (h : b ≠ 0) : set.nonempty {o | a < b * succ o} := ⟨a, succ_le.1 $ by simpa only [succ_zero, one_mul] using mul_le_mul_right (succ a) (succ_le.2 (pos_iff_ne_zero.2 h))⟩ /-- `a / b` is the unique ordinal `o` satisfying `a = b * o + o'` with `o' < b`. -/ protected def div (a b : ordinal.{u}) : ordinal.{u} := if h : b = 0 then 0 else omin {o | a < b * succ o} (ordinal.div_aux a b h) instance : has_div ordinal := ⟨ordinal.div⟩ @[simp] theorem div_zero (a : ordinal) : a / 0 = 0 := dif_pos rfl lemma div_def (a) {b : ordinal} (h : b ≠ 0) : a / b = omin {o | a < b * succ o} (ordinal.div_aux a b h) := dif_neg h theorem lt_mul_succ_div (a) {b : ordinal} (h : b ≠ 0) : a < b * succ (a / b) := by rw div_def a h; exact omin_mem {o | a < b * succ o} _ theorem lt_mul_div_add (a) {b : ordinal} (h : b ≠ 0) : a < b * (a / b) + b := by simpa only [mul_succ] using lt_mul_succ_div a h theorem div_le {a b c : ordinal} (b0 : b ≠ 0) : a / b ≤ c ↔ a < b * succ c := ⟨λ h, lt_of_lt_of_le (lt_mul_succ_div a b0) (mul_le_mul_left _ $ succ_le_succ.2 h), λ h, by rw div_def a b0; exact omin_le h⟩ theorem lt_div {a b c : ordinal} (c0 : c ≠ 0) : a < b / c ↔ c * succ a ≤ b := by rw [← not_le, div_le c0, not_lt] theorem le_div {a b c : ordinal} (c0 : c ≠ 0) : a ≤ b / c ↔ c * a ≤ b := begin apply limit_rec_on a, { simp only [mul_zero, zero_le] }, { intros, rw [succ_le, lt_div c0] }, { simp only [mul_le_of_limit, limit_le, iff_self, forall_true_iff] {contextual := tt} } end theorem div_lt {a b c : ordinal} (b0 : b ≠ 0) : a / b < c ↔ a < b * c := lt_iff_lt_of_le_iff_le $ le_div b0 theorem div_le_of_le_mul {a b c : ordinal} (h : a ≤ b * c) : a / b ≤ c := if b0 : b = 0 then by simp only [b0, div_zero, zero_le] else (div_le b0).2 $ lt_of_le_of_lt h $ mul_lt_mul_of_pos_left (lt_succ_self _) (pos_iff_ne_zero.2 b0) theorem mul_lt_of_lt_div {a b c : ordinal} : a < b / c → c * a < b := lt_imp_lt_of_le_imp_le div_le_of_le_mul @[simp] theorem zero_div (a : ordinal) : 0 / a = 0 := le_zero.1 $ div_le_of_le_mul $ zero_le _ theorem mul_div_le (a b : ordinal) : b * (a / b) ≤ a := if b0 : b = 0 then by simp only [b0, zero_mul, zero_le] else (le_div b0).1 (le_refl _) theorem mul_add_div (a) {b : ordinal} (b0 : b ≠ 0) (c) : (b * a + c) / b = a + c / b := begin apply le_antisymm, { apply (div_le b0).2, rw [mul_succ, mul_add, add_assoc, add_lt_add_iff_left], apply lt_mul_div_add _ b0 }, { rw [le_div b0, mul_add, add_le_add_iff_left], apply mul_div_le } end theorem div_eq_zero_of_lt {a b : ordinal} (h : a < b) : a / b = 0 := by rw [← le_zero, div_le $ pos_iff_ne_zero.1 $ lt_of_le_of_lt (zero_le _) h]; simpa only [succ_zero, mul_one] using h @[simp] theorem mul_div_cancel (a) {b : ordinal} (b0 : b ≠ 0) : b * a / b = a := by simpa only [add_zero, zero_div] using mul_add_div a b0 0 @[simp] theorem div_one (a : ordinal) : a / 1 = a := by simpa only [one_mul] using mul_div_cancel a ordinal.one_ne_zero @[simp] theorem div_self {a : ordinal} (h : a ≠ 0) : a / a = 1 := by simpa only [mul_one] using mul_div_cancel 1 h theorem mul_sub (a b c : ordinal) : a * (b - c) = a * b - a * c := if a0 : a = 0 then by simp only [a0, zero_mul, sub_self] else eq_of_forall_ge_iff $ λ d, by rw [sub_le, ← le_div a0, sub_le, ← le_div a0, mul_add_div _ a0] theorem is_limit_add_iff {a b} : is_limit (a + b) ↔ is_limit b ∨ (b = 0 ∧ is_limit a) := begin split; intro h, { by_cases h' : b = 0, { rw [h', add_zero] at h, right, exact ⟨h', h⟩ }, left, rw [←add_sub_cancel a b], apply sub_is_limit h, suffices : a + 0 < a + b, simpa only [add_zero], rwa [add_lt_add_iff_left, pos_iff_ne_zero] }, rcases h with h|⟨rfl, h⟩, exact add_is_limit a h, simpa only [add_zero] end theorem dvd_add_iff : ∀ {a b c : ordinal}, a ∣ b → (a ∣ b + c ↔ a ∣ c) | a _ c ⟨b, rfl⟩ := ⟨λ ⟨d, e⟩, ⟨d - b, by rw [mul_sub, ← e, add_sub_cancel]⟩, λ ⟨d, e⟩, by { rw [e, ← mul_add], apply dvd_mul_right }⟩ theorem dvd_add {a b c : ordinal} (h₁ : a ∣ b) : a ∣ c → a ∣ b + c := (dvd_add_iff h₁).2 theorem dvd_zero (a : ordinal) : a ∣ 0 := ⟨_, (mul_zero _).symm⟩ theorem zero_dvd {a : ordinal} : 0 ∣ a ↔ a = 0 := ⟨λ ⟨h, e⟩, by simp only [e, zero_mul], λ e, e.symm ▸ dvd_zero _⟩ theorem one_dvd (a : ordinal) : 1 ∣ a := ⟨a, (one_mul _).symm⟩ theorem div_mul_cancel : ∀ {a b : ordinal}, a ≠ 0 → a ∣ b → a * (b / a) = b | a _ a0 ⟨b, rfl⟩ := by rw [mul_div_cancel _ a0] theorem le_of_dvd : ∀ {a b : ordinal}, b ≠ 0 → a ∣ b → a ≤ b | a _ b0 ⟨b, rfl⟩ := by simpa only [mul_one] using mul_le_mul_left a (one_le_iff_ne_zero.2 (λ h : b = 0, by simpa only [h, mul_zero] using b0)) theorem dvd_antisymm {a b : ordinal} (h₁ : a ∣ b) (h₂ : b ∣ a) : a = b := if a0 : a = 0 then by subst a; exact (zero_dvd.1 h₁).symm else if b0 : b = 0 then by subst b; exact zero_dvd.1 h₂ else le_antisymm (le_of_dvd b0 h₁) (le_of_dvd a0 h₂) /-- `a % b` is the unique ordinal `o'` satisfying `a = b * o + o'` with `o' < b`. -/ instance : has_mod ordinal := ⟨λ a b, a - b * (a / b)⟩ theorem mod_def (a b : ordinal) : a % b = a - b * (a / b) := rfl @[simp] theorem mod_zero (a : ordinal) : a % 0 = a := by simp only [mod_def, div_zero, zero_mul, sub_zero] theorem mod_eq_of_lt {a b : ordinal} (h : a < b) : a % b = a := by simp only [mod_def, div_eq_zero_of_lt h, mul_zero, sub_zero] @[simp] theorem zero_mod (b : ordinal) : 0 % b = 0 := by simp only [mod_def, zero_div, mul_zero, sub_self] theorem div_add_mod (a b : ordinal) : b * (a / b) + a % b = a := add_sub_cancel_of_le $ mul_div_le _ _ theorem mod_lt (a) {b : ordinal} (h : b ≠ 0) : a % b < b := (add_lt_add_iff_left (b * (a / b))).1 $ by rw div_add_mod; exact lt_mul_div_add a h @[simp] theorem mod_self (a : ordinal) : a % a = 0 := if a0 : a = 0 then by simp only [a0, zero_mod] else by simp only [mod_def, div_self a0, mul_one, sub_self] @[simp] theorem mod_one (a : ordinal) : a % 1 = 0 := by simp only [mod_def, div_one, one_mul, sub_self] /-! ### Supremum of a family of ordinals -/ /-- The supremum of a family of ordinals -/ def sup {ι} (f : ι → ordinal) : ordinal := omin {c | ∀ i, f i ≤ c} ⟨(sup (cardinal.succ ∘ card ∘ f)).ord, λ i, le_of_lt $ cardinal.lt_ord.2 (lt_of_lt_of_le (cardinal.lt_succ_self _) (le_sup _ _))⟩ theorem le_sup {ι} (f : ι → ordinal) : ∀ i, f i ≤ sup f := omin_mem {c | ∀ i, f i ≤ c} _ theorem sup_le {ι} {f : ι → ordinal} {a} : sup f ≤ a ↔ ∀ i, f i ≤ a := ⟨λ h i, le_trans (le_sup _ _) h, λ h, omin_le h⟩ theorem lt_sup {ι} {f : ι → ordinal} {a} : a < sup f ↔ ∃ i, a < f i := by simpa only [not_forall, not_le] using not_congr (@sup_le _ f a) theorem is_normal.sup {f} (H : is_normal f) {ι} {g : ι → ordinal} (h : nonempty ι) : f (sup g) = sup (f ∘ g) := eq_of_forall_ge_iff $ λ a, by rw [sup_le, comp, H.le_set' (λ_:ι, true) g (let ⟨i⟩ := h in ⟨i, ⟨⟩⟩)]; intros; simp only [sup_le, true_implies_iff] theorem sup_ord {ι} (f : ι → cardinal) : sup (λ i, (f i).ord) = (cardinal.sup f).ord := eq_of_forall_ge_iff $ λ a, by simp only [sup_le, cardinal.ord_le, cardinal.sup_le] lemma sup_succ {ι} (f : ι → ordinal) : sup (λ i, succ (f i)) ≤ succ (sup f) := by { rw [ordinal.sup_le], intro i, rw ordinal.succ_le_succ, apply ordinal.le_sup } lemma unbounded_range_of_sup_ge {α β : Type u} (r : α → α → Prop) [is_well_order α r] (f : β → α) (h : type r ≤ sup.{u u} (typein r ∘ f)) : unbounded r (range f) := begin apply (not_bounded_iff _).mp, rintro ⟨x, hx⟩, apply not_lt_of_ge h, refine lt_of_le_of_lt _ (typein_lt_type r x), rw [sup_le], intro y, apply le_of_lt, rw typein_lt_typein, apply hx, apply mem_range_self end /-- The supremum of a family of ordinals indexed by the set of ordinals less than some `o : ordinal.{u}`. (This is not a special case of `sup` over the subtype, because `{a // a < o} : Type (u+1)` and `sup` only works over families in `Type u`.) -/ def bsup (o : ordinal.{u}) : (Π a < o, ordinal.{max u v}) → ordinal.{max u v} := match o, o.out, o.out_eq with | _, ⟨α, r, _⟩, rfl, f := by exactI sup (λ a, f (typein r a) (typein_lt_type _ _)) end theorem bsup_le {o f a} : bsup.{u v} o f ≤ a ↔ ∀ i h, f i h ≤ a := match o, o.out, o.out_eq, f : ∀ o w (e : ⟦w⟧ = o) (f : Π (a : ordinal.{u}), a < o → ordinal.{(max u v)}), bsup._match_1 o w e f ≤ a ↔ ∀ i h, f i h ≤ a with | _, ⟨α, r, _⟩, rfl, f := by rw [bsup._match_1, sup_le]; exactI ⟨λ H i h, by simpa only [typein_enum] using H (enum r i h), λ H b, H _ _⟩ end theorem bsup_type (r : α → α → Prop) [is_well_order α r] (f) : bsup (type r) f = sup (λ a, f (typein r a) (typein_lt_type _ _)) := eq_of_forall_ge_iff $ λ o, by rw [bsup_le, sup_le]; exact ⟨λ H b, H _ _, λ H i h, by simpa only [typein_enum] using H (enum r i h)⟩ theorem le_bsup {o} (f : Π a < o, ordinal) (i h) : f i h ≤ bsup o f := bsup_le.1 (le_refl _) _ _ theorem lt_bsup {o : ordinal} {f : Π a < o, ordinal} (hf : ∀{a a'} (ha : a < o) (ha' : a' < o), a < a' → f a ha < f a' ha') (ho : o.is_limit) (i h) : f i h < bsup o f := lt_of_lt_of_le (hf _ _ $ lt_succ_self i) (le_bsup f i.succ $ ho.2 _ h) theorem bsup_id {o} (ho : is_limit o) : bsup.{u u} o (λ x _, x) = o := begin apply le_antisymm, rw [bsup_le], intro i, apply le_of_lt, rw [←not_lt], intro h, apply lt_irrefl (bsup.{u u} o (λ x _, x)), apply lt_of_le_of_lt _ (lt_bsup _ ho _ h), refl, intros, assumption end theorem is_normal.bsup {f} (H : is_normal f) {o : ordinal} : ∀ (g : Π a < o, ordinal) (h : o ≠ 0), f (bsup o g) = bsup o (λ a h, f (g a h)) := induction_on o $ λ α r _ g h, by resetI; rw [bsup_type, H.sup (type_ne_zero_iff_nonempty.1 h), bsup_type] theorem is_normal.bsup_eq {f} (H : is_normal f) {o : ordinal} (h : is_limit o) : bsup.{u} o (λx _, f x) = f o := by { rw [←is_normal.bsup.{u u} H (λ x _, x) h.1, bsup_id h] } /-! ### Ordinal exponential -/ /-- The ordinal exponential, defined by transfinite recursion. -/ def power (a b : ordinal) : ordinal := if a = 0 then 1 - b else limit_rec_on b 1 (λ _ IH, IH * a) (λ b _, bsup.{u u} b) instance : has_pow ordinal ordinal := ⟨power⟩ local infixr ^ := @pow ordinal ordinal ordinal.has_pow theorem zero_power' (a : ordinal) : 0 ^ a = 1 - a := by simp only [pow, power, if_pos rfl] @[simp] theorem zero_power {a : ordinal} (a0 : a ≠ 0) : 0 ^ a = 0 := by rwa [zero_power', sub_eq_zero_iff_le, one_le_iff_ne_zero] @[simp] theorem power_zero (a : ordinal) : a ^ 0 = 1 := by by_cases a = 0; [simp only [pow, power, if_pos h, sub_zero], simp only [pow, power, if_neg h, limit_rec_on_zero]] @[simp] theorem power_succ (a b : ordinal) : a ^ succ b = a ^ b * a := if h : a = 0 then by subst a; simp only [zero_power (succ_ne_zero _), mul_zero] else by simp only [pow, power, limit_rec_on_succ, if_neg h] theorem power_limit {a b : ordinal} (a0 : a ≠ 0) (h : is_limit b) : a ^ b = bsup.{u u} b (λ c _, a ^ c) := by simp only [pow, power, if_neg a0]; rw limit_rec_on_limit _ _ _ _ h; refl theorem power_le_of_limit {a b c : ordinal} (a0 : a ≠ 0) (h : is_limit b) : a ^ b ≤ c ↔ ∀ b' < b, a ^ b' ≤ c := by rw [power_limit a0 h, bsup_le] theorem lt_power_of_limit {a b c : ordinal} (b0 : b ≠ 0) (h : is_limit c) : a < b ^ c ↔ ∃ c' < c, a < b ^ c' := by rw [← not_iff_not, not_exists]; simp only [not_lt, power_le_of_limit b0 h, exists_prop, not_and] @[simp] theorem power_one (a : ordinal) : a ^ 1 = a := by rw [← succ_zero, power_succ]; simp only [power_zero, one_mul] @[simp] theorem one_power (a : ordinal) : 1 ^ a = 1 := begin apply limit_rec_on a, { simp only [power_zero] }, { intros _ ih, simp only [power_succ, ih, mul_one] }, refine λ b l IH, eq_of_forall_ge_iff (λ c, _), rw [power_le_of_limit ordinal.one_ne_zero l], exact ⟨λ H, by simpa only [power_zero] using H 0 l.pos, λ H b' h, by rwa IH _ h⟩, end theorem power_pos {a : ordinal} (b) (a0 : 0 < a) : 0 < a ^ b := begin have h0 : 0 < a ^ 0, {simp only [power_zero, zero_lt_one]}, apply limit_rec_on b, { exact h0 }, { intros b IH, rw [power_succ], exact mul_pos IH a0 }, { exact λ b l _, (lt_power_of_limit (pos_iff_ne_zero.1 a0) l).2 ⟨0, l.pos, h0⟩ }, end theorem power_ne_zero {a : ordinal} (b) (a0 : a ≠ 0) : a ^ b ≠ 0 := pos_iff_ne_zero.1 $ power_pos b $ pos_iff_ne_zero.2 a0 theorem power_is_normal {a : ordinal} (h : 1 < a) : is_normal ((^) a) := have a0 : 0 < a, from lt_trans zero_lt_one h, ⟨λ b, by simpa only [mul_one, power_succ] using (mul_lt_mul_iff_left (power_pos b a0)).2 h, λ b l c, power_le_of_limit (ne_of_gt a0) l⟩ theorem power_lt_power_iff_right {a b c : ordinal} (a1 : 1 < a) : a ^ b < a ^ c ↔ b < c := (power_is_normal a1).lt_iff theorem power_le_power_iff_right {a b c : ordinal} (a1 : 1 < a) : a ^ b ≤ a ^ c ↔ b ≤ c := (power_is_normal a1).le_iff theorem power_right_inj {a b c : ordinal} (a1 : 1 < a) : a ^ b = a ^ c ↔ b = c := (power_is_normal a1).inj theorem power_is_limit {a b : ordinal} (a1 : 1 < a) : is_limit b → is_limit (a ^ b) := (power_is_normal a1).is_limit theorem power_is_limit_left {a b : ordinal} (l : is_limit a) (hb : b ≠ 0) : is_limit (a ^ b) := begin rcases zero_or_succ_or_limit b with e|⟨b,rfl⟩|l', { exact absurd e hb }, { rw power_succ, exact mul_is_limit (power_pos _ l.pos) l }, { exact power_is_limit l.one_lt l' } end theorem power_le_power_right {a b c : ordinal} (h₁ : 0 < a) (h₂ : b ≤ c) : a ^ b ≤ a ^ c := begin cases lt_or_eq_of_le (one_le_iff_pos.2 h₁) with h₁ h₁, { exact (power_le_power_iff_right h₁).2 h₂ }, { subst a, simp only [one_power] } end theorem power_le_power_left {a b : ordinal} (c) (ab : a ≤ b) : a ^ c ≤ b ^ c := begin by_cases a0 : a = 0, { subst a, by_cases c0 : c = 0, { subst c, simp only [power_zero] }, { simp only [zero_power c0, zero_le] } }, { apply limit_rec_on c, { simp only [power_zero] }, { intros c IH, simpa only [power_succ] using mul_le_mul IH ab }, { exact λ c l IH, (power_le_of_limit a0 l).2 (λ b' h, le_trans (IH _ h) (power_le_power_right (lt_of_lt_of_le (pos_iff_ne_zero.2 a0) ab) (le_of_lt h))) } } end theorem le_power_self {a : ordinal} (b) (a1 : 1 < a) : b ≤ a ^ b := (power_is_normal a1).le_self _ theorem power_lt_power_left_of_succ {a b c : ordinal} (ab : a < b) : a ^ succ c < b ^ succ c := by rw [power_succ, power_succ]; exact lt_of_le_of_lt (mul_le_mul_right _ $ power_le_power_left _ $ le_of_lt ab) (mul_lt_mul_of_pos_left ab (power_pos _ (lt_of_le_of_lt (zero_le _) ab))) theorem power_add (a b c : ordinal) : a ^ (b + c) = a ^ b * a ^ c := begin by_cases a0 : a = 0, { subst a, by_cases c0 : c = 0, {simp only [c0, add_zero, power_zero, mul_one]}, have : b+c ≠ 0 := ne_of_gt (lt_of_lt_of_le (pos_iff_ne_zero.2 c0) (le_add_left _ _)), simp only [zero_power c0, zero_power this, mul_zero] }, cases eq_or_lt_of_le (one_le_iff_ne_zero.2 a0) with a1 a1, { subst a1, simp only [one_power, mul_one] }, apply limit_rec_on c, { simp only [add_zero, power_zero, mul_one] }, { intros c IH, rw [add_succ, power_succ, IH, power_succ, mul_assoc] }, { intros c l IH, refine eq_of_forall_ge_iff (λ d, (((power_is_normal a1).trans (add_is_normal b)).limit_le l).trans _), simp only [IH] {contextual := tt}, exact (((mul_is_normal $ power_pos b (pos_iff_ne_zero.2 a0)).trans (power_is_normal a1)).limit_le l).symm } end theorem power_dvd_power (a) {b c : ordinal} (h : b ≤ c) : a ^ b ∣ a ^ c := by { rw [← add_sub_cancel_of_le h, power_add], apply dvd_mul_right } theorem power_dvd_power_iff {a b c : ordinal} (a1 : 1 < a) : a ^ b ∣ a ^ c ↔ b ≤ c := ⟨λ h, le_of_not_lt $ λ hn, not_le_of_lt ((power_lt_power_iff_right a1).2 hn) $ le_of_dvd (power_ne_zero _ $ one_le_iff_ne_zero.1 $ le_of_lt a1) h, power_dvd_power _⟩ theorem power_mul (a b c : ordinal) : a ^ (b * c) = (a ^ b) ^ c := begin by_cases b0 : b = 0, {simp only [b0, zero_mul, power_zero, one_power]}, by_cases a0 : a = 0, { subst a, by_cases c0 : c = 0, {simp only [c0, mul_zero, power_zero]}, simp only [zero_power b0, zero_power c0, zero_power (mul_ne_zero b0 c0)] }, cases eq_or_lt_of_le (one_le_iff_ne_zero.2 a0) with a1 a1, { subst a1, simp only [one_power] }, apply limit_rec_on c, { simp only [mul_zero, power_zero] }, { intros c IH, rw [mul_succ, power_add, IH, power_succ] }, { intros c l IH, refine eq_of_forall_ge_iff (λ d, (((power_is_normal a1).trans (mul_is_normal (pos_iff_ne_zero.2 b0))).limit_le l).trans _), simp only [IH] {contextual := tt}, exact (power_le_of_limit (power_ne_zero _ a0) l).symm } end /-! ### Ordinal logarithm -/ /-- The ordinal logarithm is the solution `u` to the equation `x = b ^ u * v + w` where `v < b` and `w < b`. -/ def log (b : ordinal) (x : ordinal) : ordinal := if h : 1 < b then pred $ omin {o | x < b^o} ⟨succ x, succ_le.1 (le_power_self _ h)⟩ else 0 @[simp] theorem log_not_one_lt {b : ordinal} (b1 : ¬ 1 < b) (x : ordinal) : log b x = 0 := by simp only [log, dif_neg b1] theorem log_def {b : ordinal} (b1 : 1 < b) (x : ordinal) : log b x = pred (omin {o | x < b^o} (log._proof_1 b x b1)) := by simp only [log, dif_pos b1] @[simp] theorem log_zero (b : ordinal) : log b 0 = 0 := if b1 : 1 < b then by rw [log_def b1, ← le_zero, pred_le]; apply omin_le; change 0<b^succ 0; rw [succ_zero, power_one]; exact lt_trans zero_lt_one b1 else by simp only [log_not_one_lt b1] theorem succ_log_def {b x : ordinal} (b1 : 1 < b) (x0 : 0 < x) : succ (log b x) = omin {o | x < b^o} (log._proof_1 b x b1) := begin let t := omin {o | x < b^o} (log._proof_1 b x b1), have : x < b ^ t := omin_mem {o | x < b^o} _, rcases zero_or_succ_or_limit t with h|h|h, { refine (not_lt_of_le (one_le_iff_pos.2 x0) _).elim, simpa only [h, power_zero] }, { rw [show log b x = pred t, from log_def b1 x, succ_pred_iff_is_succ.2 h] }, { rcases (lt_power_of_limit (ne_of_gt $ lt_trans zero_lt_one b1) h).1 this with ⟨a, h₁, h₂⟩, exact (not_le_of_lt h₁).elim (le_omin.1 (le_refl t) a h₂) } end theorem lt_power_succ_log {b : ordinal} (b1 : 1 < b) (x : ordinal) : x < b ^ succ (log b x) := begin cases lt_or_eq_of_le (zero_le x) with x0 x0, { rw [succ_log_def b1 x0], exact omin_mem {o | x < b^o} _ }, { subst x, apply power_pos _ (lt_trans zero_lt_one b1) } end theorem power_log_le (b) {x : ordinal} (x0 : 0 < x) : b ^ log b x ≤ x := begin by_cases b0 : b = 0, { rw [b0, zero_power'], refine le_trans (sub_le_self _ _) (one_le_iff_pos.2 x0) }, cases lt_or_eq_of_le (one_le_iff_ne_zero.2 b0) with b1 b1, { refine le_of_not_lt (λ h, not_le_of_lt (lt_succ_self (log b x)) _), have := @omin_le {o | x < b^o} _ _ h, rwa ← succ_log_def b1 x0 at this }, { rw [← b1, one_power], exact one_le_iff_pos.2 x0 } end theorem le_log {b x c : ordinal} (b1 : 1 < b) (x0 : 0 < x) : c ≤ log b x ↔ b ^ c ≤ x := ⟨λ h, le_trans ((power_le_power_iff_right b1).2 h) (power_log_le b x0), λ h, le_of_not_lt $ λ hn, not_le_of_lt (lt_power_succ_log b1 x) $ le_trans ((power_le_power_iff_right b1).2 (succ_le.2 hn)) h⟩ theorem log_lt {b x c : ordinal} (b1 : 1 < b) (x0 : 0 < x) : log b x < c ↔ x < b ^ c := lt_iff_lt_of_le_iff_le (le_log b1 x0) theorem log_le_log (b) {x y : ordinal} (xy : x ≤ y) : log b x ≤ log b y := if x0 : x = 0 then by simp only [x0, log_zero, zero_le] else have x0 : 0 < x, from pos_iff_ne_zero.2 x0, if b1 : 1 < b then (le_log b1 (lt_of_lt_of_le x0 xy)).2 $ le_trans (power_log_le _ x0) xy else by simp only [log_not_one_lt b1, zero_le] theorem log_le_self (b x : ordinal) : log b x ≤ x := if x0 : x = 0 then by simp only [x0, log_zero, zero_le] else if b1 : 1 < b then le_trans (le_power_self _ b1) (power_log_le b (pos_iff_ne_zero.2 x0)) else by simp only [log_not_one_lt b1, zero_le] /-! ### The Cantor normal form -/ theorem CNF_aux {b o : ordinal} (b0 : b ≠ 0) (o0 : o ≠ 0) : o % b ^ log b o < o := lt_of_lt_of_le (mod_lt _ $ power_ne_zero _ b0) (power_log_le _ $ pos_iff_ne_zero.2 o0) /-- Proving properties of ordinals by induction over their Cantor normal form. -/ @[elab_as_eliminator] noncomputable def CNF_rec {b : ordinal} (b0 : b ≠ 0) {C : ordinal → Sort*} (H0 : C 0) (H : ∀ o, o ≠ 0 → o % b ^ log b o < o → C (o % b ^ log b o) → C o) : ∀ o, C o | o := if o0 : o = 0 then by rw o0; exact H0 else have _, from CNF_aux b0 o0, H o o0 this (CNF_rec (o % b ^ log b o)) using_well_founded {dec_tac := `[assumption]} @[simp] theorem CNF_rec_zero {b} (b0) {C H0 H} : @CNF_rec b b0 C H0 H 0 = H0 := by rw [CNF_rec, dif_pos rfl]; refl @[simp] theorem CNF_rec_ne_zero {b} (b0) {C H0 H o} (o0) : @CNF_rec b b0 C H0 H o = H o o0 (CNF_aux b0 o0) (@CNF_rec b b0 C H0 H _) := by rw [CNF_rec, dif_neg o0] /-- The Cantor normal form of an ordinal is the list of coefficients in the base-`b` expansion of `o`. CNF b (b ^ u₁ * v₁ + b ^ u₂ * v₂) = [(u₁, v₁), (u₂, v₂)] -/ noncomputable def CNF (b := omega) (o : ordinal) : list (ordinal × ordinal) := if b0 : b = 0 then [] else CNF_rec b0 [] (λ o o0 h IH, (log b o, o / b ^ log b o) :: IH) o @[simp] theorem zero_CNF (o) : CNF 0 o = [] := dif_pos rfl @[simp] theorem CNF_zero (b) : CNF b 0 = [] := if b0 : b = 0 then dif_pos b0 else (dif_neg b0).trans $ CNF_rec_zero _ theorem CNF_ne_zero {b o : ordinal} (b0 : b ≠ 0) (o0 : o ≠ 0) : CNF b o = (log b o, o / b ^ log b o) :: CNF b (o % b ^ log b o) := by unfold CNF; rw [dif_neg b0, dif_neg b0, CNF_rec_ne_zero b0 o0] theorem one_CNF {o : ordinal} (o0 : o ≠ 0) : CNF 1 o = [(0, o)] := by rw [CNF_ne_zero ordinal.one_ne_zero o0, log_not_one_lt (lt_irrefl _), power_zero, mod_one, CNF_zero, div_one] theorem CNF_foldr {b : ordinal} (b0 : b ≠ 0) (o) : (CNF b o).foldr (λ p r, b ^ p.1 * p.2 + r) 0 = o := CNF_rec b0 (by rw CNF_zero; refl) (λ o o0 h IH, by rw [CNF_ne_zero b0 o0, list.foldr_cons, IH, div_add_mod]) o theorem CNF_pairwise_aux (b := omega) (o) : (∀ p ∈ CNF b o, prod.fst p ≤ log b o) ∧ (CNF b o).pairwise (λ p q, q.1 < p.1) := begin by_cases b0 : b = 0, { simp only [b0, zero_CNF, list.pairwise.nil, and_true], exact λ _, false.elim }, cases lt_or_eq_of_le (one_le_iff_ne_zero.2 b0) with b1 b1, { refine CNF_rec b0 _ _ o, { simp only [CNF_zero, list.pairwise.nil, and_true], exact λ _, false.elim }, intros o o0 H IH, cases IH with IH₁ IH₂, simp only [CNF_ne_zero b0 o0, list.forall_mem_cons, list.pairwise_cons, IH₂, and_true], refine ⟨⟨le_refl _, λ p m, _⟩, λ p m, _⟩, { exact le_trans (IH₁ p m) (log_le_log _ $ le_of_lt H) }, { refine lt_of_le_of_lt (IH₁ p m) ((log_lt b1 _).2 _), { rw pos_iff_ne_zero, intro e, rw e at m, simpa only [CNF_zero] using m }, { exact mod_lt _ (power_ne_zero _ b0) } } }, { by_cases o0 : o = 0, { simp only [o0, CNF_zero, list.pairwise.nil, and_true], exact λ _, false.elim }, rw [← b1, one_CNF o0], simp only [list.mem_singleton, log_not_one_lt (lt_irrefl _), forall_eq, le_refl, true_and, list.pairwise_singleton] } end theorem CNF_pairwise (b := omega) (o) : (CNF b o).pairwise (λ p q, prod.fst q < p.1) := (CNF_pairwise_aux _ _).2 theorem CNF_fst_le_log (b := omega) (o) : ∀ p ∈ CNF b o, prod.fst p ≤ log b o := (CNF_pairwise_aux _ _).1 theorem CNF_fst_le (b := omega) (o) (p ∈ CNF b o) : prod.fst p ≤ o := le_trans (CNF_fst_le_log _ _ p H) (log_le_self _ _) theorem CNF_snd_lt {b : ordinal} (b1 : 1 < b) (o) : ∀ p ∈ CNF b o, prod.snd p < b := begin have b0 := ne_of_gt (lt_trans zero_lt_one b1), refine CNF_rec b0 (λ _, by rw [CNF_zero]; exact false.elim) _ o, intros o o0 H IH, simp only [CNF_ne_zero b0 o0, list.mem_cons_iff, forall_eq_or_imp, iff_true_intro IH, and_true], rw [div_lt (power_ne_zero _ b0), ← power_succ], exact lt_power_succ_log b1 _, end theorem CNF_sorted (b := omega) (o) : ((CNF b o).map prod.fst).sorted (>) := by rw [list.sorted, list.pairwise_map]; exact CNF_pairwise b o /-! ### Casting naturals into ordinals, compatibility with operations -/ @[simp] theorem nat_cast_mul {m n : ℕ} : ((m * n : ℕ) : ordinal) = m * n := by induction n with n IH; [simp only [nat.cast_zero, nat.mul_zero, mul_zero], rw [nat.mul_succ, nat.cast_add, IH, nat.cast_succ, mul_add_one]] @[simp] theorem nat_cast_power {m n : ℕ} : ((pow m n : ℕ) : ordinal) = m ^ n := by induction n with n IH; [simp only [pow_zero, nat.cast_zero, power_zero, nat.cast_one], rw [pow_succ', nat_cast_mul, IH, nat.cast_succ, ← succ_eq_add_one, power_succ]] @[simp] theorem nat_cast_le {m n : ℕ} : (m : ordinal) ≤ n ↔ m ≤ n := by rw [← cardinal.ord_nat, ← cardinal.ord_nat, cardinal.ord_le_ord, cardinal.nat_cast_le] @[simp] theorem nat_cast_lt {m n : ℕ} : (m : ordinal) < n ↔ m < n := by simp only [lt_iff_le_not_le, nat_cast_le] @[simp] theorem nat_cast_inj {m n : ℕ} : (m : ordinal) = n ↔ m = n := by simp only [le_antisymm_iff, nat_cast_le] @[simp] theorem nat_cast_eq_zero {n : ℕ} : (n : ordinal) = 0 ↔ n = 0 := @nat_cast_inj n 0 theorem nat_cast_ne_zero {n : ℕ} : (n : ordinal) ≠ 0 ↔ n ≠ 0 := not_congr nat_cast_eq_zero @[simp] theorem nat_cast_pos {n : ℕ} : (0 : ordinal) < n ↔ 0 < n := @nat_cast_lt 0 n @[simp] theorem nat_cast_sub {m n : ℕ} : ((m - n : ℕ) : ordinal) = m - n := (_root_.le_total m n).elim (λ h, by rw [nat.sub_eq_zero_iff_le.2 h, sub_eq_zero_iff_le.2 (nat_cast_le.2 h)]; refl) (λ h, (add_left_cancel n).1 $ by rw [← nat.cast_add, nat.add_sub_cancel' h, add_sub_cancel_of_le (nat_cast_le.2 h)]) @[simp] theorem nat_cast_div {m n : ℕ} : ((m / n : ℕ) : ordinal) = m / n := if n0 : n = 0 then by simp only [n0, nat.div_zero, nat.cast_zero, div_zero] else have n0':_, from nat_cast_ne_zero.2 n0, le_antisymm (by rw [le_div n0', ← nat_cast_mul, nat_cast_le, mul_comm]; apply nat.div_mul_le_self) (by rw [div_le n0', succ, ← nat.cast_succ, ← nat_cast_mul, nat_cast_lt, mul_comm, ← nat.div_lt_iff_lt_mul _ _ (nat.pos_of_ne_zero n0)]; apply nat.lt_succ_self) @[simp] theorem nat_cast_mod {m n : ℕ} : ((m % n : ℕ) : ordinal) = m % n := by rw [← add_left_cancel (n*(m/n)), div_add_mod, ← nat_cast_div, ← nat_cast_mul, ← nat.cast_add, add_comm, nat.mod_add_div] @[simp] theorem nat_le_card {o} {n : ℕ} : (n : cardinal) ≤ card o ↔ (n : ordinal) ≤ o := ⟨λ h, by rwa [← cardinal.ord_le, cardinal.ord_nat] at h, λ h, card_nat n ▸ card_le_card h⟩ @[simp] theorem nat_lt_card {o} {n : ℕ} : (n : cardinal) < card o ↔ (n : ordinal) < o := by rw [← succ_le, ← cardinal.succ_le, ← cardinal.nat_succ, nat_le_card]; refl @[simp] theorem card_lt_nat {o} {n : ℕ} : card o < n ↔ o < n := lt_iff_lt_of_le_iff_le nat_le_card @[simp] theorem card_le_nat {o} {n : ℕ} : card o ≤ n ↔ o ≤ n := le_iff_le_iff_lt_iff_lt.2 nat_lt_card @[simp] theorem card_eq_nat {o} {n : ℕ} : card o = n ↔ o = n := by simp only [le_antisymm_iff, card_le_nat, nat_le_card] @[simp] theorem type_fin (n : ℕ) : @type (fin n) (<) _ = n := by rw [← card_eq_nat, card_type, mk_fin] @[simp] theorem lift_nat_cast (n : ℕ) : lift n = n := by induction n with n ih; [simp only [nat.cast_zero, lift_zero], simp only [nat.cast_succ, lift_add, ih, lift_one]] theorem lift_type_fin (n : ℕ) : lift (@type (fin n) (<) _) = n := by simp only [type_fin, lift_nat_cast] theorem fintype_card (r : α → α → Prop) [is_well_order α r] [fintype α] : type r = fintype.card α := by rw [← card_eq_nat, card_type, fintype_card] end ordinal /-! ### Properties of `omega` -/ namespace cardinal open ordinal @[simp] theorem ord_omega : ord.{u} omega = ordinal.omega := le_antisymm (ord_le.2 $ le_refl _) $ le_of_forall_lt $ λ o h, begin rcases ordinal.lt_lift_iff.1 h with ⟨o, rfl, h'⟩, rw [lt_ord, ← lift_card, ← lift_omega.{0 u}, lift_lt, ← typein_enum (<) h'], exact lt_omega_iff_fintype.2 ⟨set.fintype_lt_nat _⟩ end @[simp] theorem add_one_of_omega_le {c} (h : omega ≤ c) : c + 1 = c := by rw [add_comm, ← card_ord c, ← card_one, ← card_add, one_add_of_omega_le]; rwa [← ord_omega, ord_le_ord] end cardinal namespace ordinal theorem lt_omega {o : ordinal.{u}} : o < omega ↔ ∃ n : ℕ, o = n := by rw [← cardinal.ord_omega, cardinal.lt_ord, lt_omega]; simp only [card_eq_nat] theorem nat_lt_omega (n : ℕ) : (n : ordinal) < omega := lt_omega.2 ⟨_, rfl⟩ theorem omega_pos : 0 < omega := nat_lt_omega 0 theorem omega_ne_zero : omega ≠ 0 := ne_of_gt omega_pos theorem one_lt_omega : 1 < omega := by simpa only [nat.cast_one] using nat_lt_omega 1 theorem omega_is_limit : is_limit omega := ⟨omega_ne_zero, λ o h, let ⟨n, e⟩ := lt_omega.1 h in by rw [e]; exact nat_lt_omega (n+1)⟩ theorem omega_le {o : ordinal.{u}} : omega ≤ o ↔ ∀ n : ℕ, (n : ordinal) ≤ o := ⟨λ h n, le_trans (le_of_lt (nat_lt_omega _)) h, λ H, le_of_forall_lt $ λ a h, let ⟨n, e⟩ := lt_omega.1 h in by rw [e, ← succ_le]; exact H (n+1)⟩ theorem nat_lt_limit {o} (h : is_limit o) : ∀ n : ℕ, (n : ordinal) < o | 0 := lt_of_le_of_ne (zero_le o) h.1.symm | (n+1) := h.2 _ (nat_lt_limit n) theorem omega_le_of_is_limit {o} (h : is_limit o) : omega ≤ o := omega_le.2 $ λ n, le_of_lt $ nat_lt_limit h n theorem add_omega {a : ordinal} (h : a < omega) : a + omega = omega := begin rcases lt_omega.1 h with ⟨n, rfl⟩, clear h, induction n with n IH, { rw [nat.cast_zero, zero_add] }, { rw [nat.cast_succ, add_assoc, one_add_of_omega_le (le_refl _), IH] } end theorem add_lt_omega {a b : ordinal} (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 theorem mul_lt_omega {a b : ordinal} (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 theorem is_limit_iff_omega_dvd {a : ordinal} : is_limit a ↔ a ≠ 0 ∧ omega ∣ a := begin refine ⟨λ l, ⟨l.1, ⟨a / omega, le_antisymm _ (mul_div_le _ _)⟩⟩, λ h, _⟩, { refine (limit_le l).2 (λ x hx, le_of_lt _), rw [← div_lt omega_ne_zero, ← succ_le, le_div omega_ne_zero, mul_succ, add_le_of_limit omega_is_limit], intros b hb, rcases lt_omega.1 hb with ⟨n, rfl⟩, exact le_trans (add_le_add_right (mul_div_le _ _) _) (le_of_lt $ lt_sub.1 $ nat_lt_limit (sub_is_limit l hx) _) }, { rcases h with ⟨a0, b, rfl⟩, refine mul_is_limit_left omega_is_limit (pos_iff_ne_zero.2 $ mt _ a0), intro e, simp only [e, mul_zero] } end local infixr ^ := @pow ordinal ordinal ordinal.has_pow theorem power_lt_omega {a b : ordinal} (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_power]; apply nat_lt_omega end theorem add_omega_power {a b : ordinal} (h : a < omega ^ b) : a + omega ^ b = omega ^ b := begin refine le_antisymm _ (le_add_left _ _), revert h, apply limit_rec_on b, { intro h, rw [power_zero, ← succ_zero, lt_succ, le_zero] at h, rw [h, zero_add] }, { intros b _ h, rw [power_succ] at h, rcases (lt_mul_of_limit omega_is_limit).1 h with ⟨x, xo, ax⟩, refine le_trans (add_le_add_right (le_of_lt ax) _) _, rw [power_succ, ← mul_add, add_omega xo] }, { intros b l IH h, rcases (lt_power_of_limit omega_ne_zero l).1 h with ⟨x, xb, ax⟩, refine (((add_is_normal a).trans (power_is_normal one_lt_omega)) .limit_le l).2 (λ y yb, _), let z := max x y, have := IH z (max_lt xb yb) (lt_of_lt_of_le ax $ power_le_power_right omega_pos (le_max_left _ _)), exact le_trans (add_le_add_left (power_le_power_right omega_pos (le_max_right _ _)) _) (le_trans this (power_le_power_right omega_pos $ le_of_lt $ max_lt xb yb)) } end theorem add_lt_omega_power {a b c : ordinal} (h₁ : a < omega ^ c) (h₂ : b < omega ^ c) : a + b < omega ^ c := by rwa [← add_omega_power h₁, add_lt_add_iff_left] theorem add_absorp {a b c : ordinal} (h₁ : a < omega ^ b) (h₂ : omega ^ b ≤ c) : a + c = c := by rw [← add_sub_cancel_of_le h₂, ← add_assoc, add_omega_power h₁] theorem add_absorp_iff {o : ordinal} (o0 : 0 < o) : (∀ a < o, a + o = o) ↔ ∃ a, o = omega ^ a := ⟨λ H, ⟨log omega o, begin refine ((lt_or_eq_of_le (power_log_le _ o0)) .resolve_left $ λ h, _).symm, have := H _ h, have := lt_power_succ_log one_lt_omega o, rw [power_succ, lt_mul_of_limit omega_is_limit] at this, rcases this with ⟨a, ao, h'⟩, rcases lt_omega.1 ao with ⟨n, rfl⟩, clear ao, revert h', apply not_lt_of_le, suffices e : omega ^ log omega o * ↑n + o = o, { simpa only [e] using le_add_right (omega ^ log omega o * ↑n) o }, induction n with n IH, {simp only [nat.cast_zero, mul_zero, zero_add]}, simp only [nat.cast_succ, mul_add_one, add_assoc, this, IH] end⟩, λ ⟨b, e⟩, e.symm ▸ λ a, add_omega_power⟩ theorem add_mul_limit_aux {a b c : ordinal} (ba : b + a = a) (l : is_limit c) (IH : ∀ c' < c, (a + b) * succ c' = a * succ c' + b) : (a + b) * c = a * c := le_antisymm ((mul_le_of_limit l).2 $ λ c' h, begin apply le_trans (mul_le_mul_left _ (le_of_lt $ lt_succ_self _)), rw IH _ h, apply le_trans (add_le_add_left _ _), { rw ← mul_succ, exact mul_le_mul_left _ (succ_le.2 $ l.2 _ h) }, { rw ← ba, exact le_add_right _ _ } end) (mul_le_mul_right _ (le_add_right _ _)) theorem add_mul_succ {a b : ordinal} (c) (ba : b + a = a) : (a + b) * succ c = a * succ c + b := begin apply limit_rec_on c, { simp only [succ_zero, mul_one] }, { intros c IH, rw [mul_succ, IH, ← add_assoc, add_assoc _ b, ba, ← mul_succ] }, { intros c l IH, have := add_mul_limit_aux ba l IH, rw [mul_succ, add_mul_limit_aux ba l IH, mul_succ, add_assoc] } end theorem add_mul_limit {a b c : ordinal} (ba : b + a = a) (l : is_limit c) : (a + b) * c = a * c := add_mul_limit_aux ba l (λ c' _, add_mul_succ c' ba) theorem mul_omega {a : ordinal} (a0 : 0 < a) (ha : a < omega) : a * omega = omega := le_antisymm ((mul_le_of_limit omega_is_limit).2 $ λ b hb, le_of_lt (mul_lt_omega ha hb)) (by simpa only [one_mul] using mul_le_mul_right omega (one_le_iff_pos.2 a0)) theorem mul_lt_omega_power {a b c : ordinal} (c0 : 0 < c) (ha : a < omega ^ c) (hb : b < omega) : a * b < omega ^ c := if b0 : b = 0 then by simp only [b0, mul_zero, power_pos _ omega_pos] else begin rcases zero_or_succ_or_limit c with rfl|⟨c,rfl⟩|l, { exact (lt_irrefl _).elim c0 }, { rw power_succ at ha, rcases ((mul_is_normal $ power_pos _ omega_pos).limit_lt omega_is_limit).1 ha with ⟨n, hn, an⟩, refine lt_of_le_of_lt (mul_le_mul_right _ (le_of_lt an)) _, rw [power_succ, mul_assoc, mul_lt_mul_iff_left (power_pos _ omega_pos)], exact mul_lt_omega hn hb }, { rcases ((power_is_normal one_lt_omega).limit_lt l).1 ha with ⟨x, hx, ax⟩, refine lt_of_le_of_lt (mul_le_mul (le_of_lt ax) (le_of_lt hb)) _, rw [← power_succ, power_lt_power_iff_right one_lt_omega], exact l.2 _ hx } end theorem mul_omega_dvd {a : ordinal} (a0 : 0 < a) (ha : a < omega) : ∀ {b}, omega ∣ b → a * b = b | _ ⟨b, rfl⟩ := by rw [← mul_assoc, mul_omega a0 ha] theorem mul_omega_power_power {a b : ordinal} (a0 : 0 < a) (h : a < omega ^ omega ^ b) : a * omega ^ omega ^ b = omega ^ omega ^ b := begin by_cases b0 : b = 0, {rw [b0, power_zero, power_one] at h ⊢, exact mul_omega a0 h}, refine le_antisymm _ (by simpa only [one_mul] using mul_le_mul_right (omega^omega^b) (one_le_iff_pos.2 a0)), rcases (lt_power_of_limit omega_ne_zero (power_is_limit_left omega_is_limit b0)).1 h with ⟨x, xb, ax⟩, refine le_trans (mul_le_mul_right _ (le_of_lt ax)) _, rw [← power_add, add_omega_power xb] end theorem power_omega {a : ordinal} (a1 : 1 < a) (h : a < omega) : a ^ omega = omega := le_antisymm ((power_le_of_limit (one_le_iff_ne_zero.1 $ le_of_lt a1) omega_is_limit).2 (λ b hb, le_of_lt (power_lt_omega h hb))) (le_power_self _ a1) /-! ### Fixed points of normal functions -/ /-- The next fixed point function, the least fixed point of the normal function `f` above `a`. -/ def nfp (f : ordinal → ordinal) (a : ordinal) := sup (λ n : ℕ, f^[n] a) theorem iterate_le_nfp (f a n) : f^[n] a ≤ nfp f a := le_sup _ n theorem le_nfp_self (f a) : a ≤ nfp f a := iterate_le_nfp f a 0 theorem is_normal.lt_nfp {f} (H : is_normal f) {a b} : f b < nfp f a ↔ b < nfp f a := lt_sup.trans $ iff.trans (by exact ⟨λ ⟨n, h⟩, ⟨n, lt_of_le_of_lt (H.le_self _) h⟩, λ ⟨n, h⟩, ⟨n+1, by rw iterate_succ'; exact H.lt_iff.2 h⟩⟩) lt_sup.symm theorem is_normal.nfp_le {f} (H : is_normal f) {a b} : nfp f a ≤ f b ↔ nfp f a ≤ b := le_iff_le_iff_lt_iff_lt.2 H.lt_nfp theorem is_normal.nfp_le_fp {f} (H : is_normal f) {a b} (ab : a ≤ b) (h : f b ≤ b) : nfp f a ≤ b := sup_le.2 $ λ i, begin induction i with i IH generalizing a, {exact ab}, exact IH (le_trans (H.le_iff.2 ab) h), end theorem is_normal.nfp_fp {f} (H : is_normal f) (a) : f (nfp f a) = nfp f a := begin refine le_antisymm _ (H.le_self _), cases le_or_lt (f a) a with aa aa, { rwa le_antisymm (H.nfp_le_fp (le_refl _) aa) (le_nfp_self _ _) }, rcases zero_or_succ_or_limit (nfp f a) with e|⟨b, e⟩|l, { refine @le_trans _ _ _ (f a) _ (H.le_iff.2 _) (iterate_le_nfp f a 1), simp only [e, zero_le] }, { have : f b < nfp f a := H.lt_nfp.2 (by simp only [e, lt_succ_self]), rw [e, lt_succ] at this, have ab : a ≤ b, { rw [← lt_succ, ← e], exact lt_of_lt_of_le aa (iterate_le_nfp f a 1) }, refine le_trans (H.le_iff.2 (H.nfp_le_fp ab this)) (le_trans this (le_of_lt _)), simp only [e, lt_succ_self] }, { exact (H.2 _ l _).2 (λ b h, le_of_lt (H.lt_nfp.2 h)) } end theorem is_normal.le_nfp {f} (H : is_normal f) {a b} : f b ≤ nfp f a ↔ b ≤ nfp f a := ⟨le_trans (H.le_self _), λ h, by simpa only [H.nfp_fp] using H.le_iff.2 h⟩ theorem nfp_eq_self {f : ordinal → ordinal} {a} (h : f a = a) : nfp f a = a := le_antisymm (sup_le.mpr $ λ i, by rw [iterate_fixed h]) (le_nfp_self f a) /-- The derivative of a normal function `f` is the sequence of fixed points of `f`. -/ def deriv (f : ordinal → ordinal) (o : ordinal) : ordinal := limit_rec_on o (nfp f 0) (λ a IH, nfp f (succ IH)) (λ a l, bsup.{u u} a) @[simp] theorem deriv_zero (f) : deriv f 0 = nfp f 0 := limit_rec_on_zero _ _ _ @[simp] theorem deriv_succ (f o) : deriv f (succ o) = nfp f (succ (deriv f o)) := limit_rec_on_succ _ _ _ _ theorem deriv_limit (f) {o} : is_limit o → deriv f o = bsup.{u u} o (λ a _, deriv f a) := limit_rec_on_limit _ _ _ _ theorem deriv_is_normal (f) : is_normal (deriv f) := ⟨λ o, by rw [deriv_succ, ← succ_le]; apply le_nfp_self, λ o l a, by rw [deriv_limit _ l, bsup_le]⟩ theorem is_normal.deriv_fp {f} (H : is_normal f) (o) : f (deriv.{u} f o) = deriv f o := begin apply limit_rec_on o, { rw [deriv_zero, H.nfp_fp] }, { intros o ih, rw [deriv_succ, H.nfp_fp] }, intros o l IH, rw [deriv_limit _ l, is_normal.bsup.{u u u} H _ l.1], refine eq_of_forall_ge_iff (λ c, _), simp only [bsup_le, IH] {contextual:=tt} end theorem is_normal.fp_iff_deriv {f} (H : is_normal f) {a} : f a ≤ a ↔ ∃ o, a = deriv f o := ⟨λ ha, begin suffices : ∀ o (_:a ≤ deriv f o), ∃ o, a = deriv f o, from this a ((deriv_is_normal _).le_self _), intro o, apply limit_rec_on o, { intros h₁, refine ⟨0, le_antisymm h₁ _⟩, rw deriv_zero, exact H.nfp_le_fp (zero_le _) ha }, { intros o IH h₁, cases le_or_lt a (deriv f o), {exact IH h}, refine ⟨succ o, le_antisymm h₁ _⟩, rw deriv_succ, exact H.nfp_le_fp (succ_le.2 h) ha }, { intros o l IH h₁, cases eq_or_lt_of_le h₁, {exact ⟨_, h⟩}, rw [deriv_limit _ l, ← not_le, bsup_le, not_ball] at h, exact let ⟨o', h, hl⟩ := h in IH o' h (le_of_not_le hl) } end, λ ⟨o, e⟩, e.symm ▸ le_of_eq (H.deriv_fp _)⟩ end ordinal
405cdd53131f600f601b6fd7b1a814bf7492a4e9
69d4931b605e11ca61881fc4f66db50a0a875e39
/src/algebra/polynomial/group_ring_action.lean
179304517553f5b20d85838a8e5b333d63dad735
[ "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
5,359
lean
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import data.polynomial.monic import algebra.group_ring_action import algebra.group_action_hom /-! # Group action on rings applied to polynomials This file contains instances and definitions relating `mul_semiring_action` to `polynomial`. -/ variables (M : Type*) [monoid M] namespace polynomial variables (R : Type*) [semiring R] noncomputable instance [mul_semiring_action M R] : mul_semiring_action M (polynomial R) := { smul := λ m, map $ mul_semiring_action.to_semiring_hom M R m, one_smul := λ p, by { ext n, erw coeff_map, exact one_smul M (p.coeff n) }, mul_smul := λ m n p, by { ext i, iterate 3 { rw coeff_map (mul_semiring_action.to_semiring_hom M R _) }, exact mul_smul m n (p.coeff i) }, smul_add := λ m p q, map_add (mul_semiring_action.to_semiring_hom M R m), smul_zero := λ m, map_zero (mul_semiring_action.to_semiring_hom M R m), smul_one := λ m, map_one (mul_semiring_action.to_semiring_hom M R m), smul_mul := λ m p q, map_mul (mul_semiring_action.to_semiring_hom M R m), } noncomputable instance [faithful_mul_semiring_action M R] : faithful_mul_semiring_action M (polynomial R) := { eq_of_smul_eq_smul' := λ m₁ m₂ h, eq_of_smul_eq_smul R $ λ s, C_inj.1 $ calc C (m₁ • s) = m₁ • C s : (map_C $ mul_semiring_action.to_semiring_hom M R m₁).symm ... = m₂ • C s : h (C s) ... = C (m₂ • s) : map_C _, .. polynomial.mul_semiring_action M R } variables {M R} variables [mul_semiring_action M R] @[simp] lemma coeff_smul' (m : M) (p : polynomial R) (n : ℕ) : (m • p).coeff n = m • p.coeff n := coeff_map _ _ @[simp] lemma smul_C (m : M) (r : R) : m • C r = C (m • r) := map_C _ @[simp] lemma smul_X (m : M) : (m • X : polynomial R) = X := map_X _ variables (S : Type*) [comm_semiring S] [mul_semiring_action M S] theorem smul_eval_smul (m : M) (f : polynomial S) (x : S) : (m • f).eval (m • x) = m • f.eval x := polynomial.induction_on f (λ r, by rw [smul_C, eval_C, eval_C]) (λ f g ihf ihg, by rw [smul_add, eval_add, ihf, ihg, eval_add, smul_add]) (λ n r ih, by rw [smul_mul', smul_pow, smul_C, smul_X, eval_mul, eval_C, eval_pow, eval_X, eval_mul, eval_C, eval_pow, eval_X, smul_mul', smul_pow]) variables (G : Type*) [group G] theorem eval_smul' [mul_semiring_action G S] (g : G) (f : polynomial S) (x : S) : f.eval (g • x) = g • (g⁻¹ • f).eval x := by rw [← smul_eval_smul, smul_inv_smul] theorem smul_eval [mul_semiring_action G S] (g : G) (f : polynomial S) (x : S) : (g • f).eval x = g • f.eval (g⁻¹ • x) := by rw [← smul_eval_smul, smul_inv_smul] end polynomial section comm_ring variables (G : Type*) [group G] [fintype G] variables (R : Type*) [comm_ring R] [mul_semiring_action G R] open mul_action open_locale classical /-- the product of `(X - g • x)` over distinct `g • x`. -/ noncomputable def prod_X_sub_smul (x : R) : polynomial R := (finset.univ : finset (quotient_group.quotient $ mul_action.stabilizer G x)).prod $ λ g, polynomial.X - polynomial.C (of_quotient_stabilizer G x g) theorem prod_X_sub_smul.monic (x : R) : (prod_X_sub_smul G R x).monic := polynomial.monic_prod_of_monic _ _ $ λ g _, polynomial.monic_X_sub_C _ theorem prod_X_sub_smul.eval (x : R) : (prod_X_sub_smul G R x).eval x = 0 := (finset.prod_hom _ (polynomial.eval x)).symm.trans $ finset.prod_eq_zero (finset.mem_univ $ quotient_group.mk 1) $ by rw [of_quotient_stabilizer_mk, one_smul, polynomial.eval_sub, polynomial.eval_X, polynomial.eval_C, sub_self] theorem prod_X_sub_smul.smul (x : R) (g : G) : g • prod_X_sub_smul G R x = prod_X_sub_smul G R x := (smul_prod _ _ _ _ _).trans $ fintype.prod_bijective _ (mul_action.bijective g) _ _ (λ g', by rw [of_quotient_stabilizer_smul, smul_sub, polynomial.smul_X, polynomial.smul_C]) theorem prod_X_sub_smul.coeff (x : R) (g : G) (n : ℕ) : g • (prod_X_sub_smul G R x).coeff n = (prod_X_sub_smul G R x).coeff n := by rw [← polynomial.coeff_smul', prod_X_sub_smul.smul] end comm_ring namespace mul_semiring_action_hom variables {M} variables {P : Type*} [comm_semiring P] [mul_semiring_action M P] variables {Q : Type*} [comm_semiring Q] [mul_semiring_action M Q] open polynomial /-- An equivariant map induces an equivariant map on polynomials. -/ protected noncomputable def polynomial (g : P →+*[M] Q) : polynomial P →+*[M] polynomial Q := { to_fun := map g, map_smul' := λ m p, polynomial.induction_on p (λ b, by rw [smul_C, map_C, coe_fn_coe, g.map_smul, map_C, coe_fn_coe, smul_C]) (λ p q ihp ihq, by rw [smul_add, polynomial.map_add, ihp, ihq, polynomial.map_add, smul_add]) (λ n b ih, by rw [smul_mul', smul_C, smul_pow, smul_X, polynomial.map_mul, map_C, polynomial.map_pow, map_X, coe_fn_coe, g.map_smul, polynomial.map_mul, map_C, polynomial.map_pow, map_X, smul_mul', smul_C, smul_pow, smul_X, coe_fn_coe]), map_zero' := polynomial.map_zero g, map_add' := λ p q, polynomial.map_add g, map_one' := polynomial.map_one g, map_mul' := λ p q, polynomial.map_mul g } @[simp] theorem coe_polynomial (g : P →+*[M] Q) : (g.polynomial : polynomial P → polynomial Q) = map g := rfl end mul_semiring_action_hom