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
70241cce72f24ba369a5c26abd6d2e1f4fde004b
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/analysis/normed/order/upper_lower.lean
c834d2d671ddc7cb2207b78baede262196385999
[ "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
5,416
lean
/- Copyright (c) 2022 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import algebra.order.field.pi import analysis.normed.group.pointwise import analysis.normed.order.basic import topology.algebra.order.upper_lower /-! # Upper/lower/order-connected sets in normed groups > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. The topological closure and interior of an upper/lower/order-connected set is an upper/lower/order-connected set (with the notable exception of the closure of an order-connected set). We also prove lemmas specific to `ℝⁿ`. Those are helpful to prove that order-connected sets in `ℝⁿ` are measurable. -/ open function metric set variables {α ι : Type*} section metric_space variables [normed_ordered_group α] {s : set α} @[to_additive is_upper_set.thickening] protected lemma is_upper_set.thickening' (hs : is_upper_set s) (ε : ℝ) : is_upper_set (thickening ε s) := by { rw ←ball_mul_one, exact hs.mul_left } @[to_additive is_lower_set.thickening] protected lemma is_lower_set.thickening' (hs : is_lower_set s) (ε : ℝ) : is_lower_set (thickening ε s) := by { rw ←ball_mul_one, exact hs.mul_left } @[to_additive is_upper_set.cthickening] protected lemma is_upper_set.cthickening' (hs : is_upper_set s) (ε : ℝ) : is_upper_set (cthickening ε s) := by { rw cthickening_eq_Inter_thickening'', exact is_upper_set_Inter₂ (λ δ hδ, hs.thickening' _) } @[to_additive is_lower_set.cthickening] protected lemma is_lower_set.cthickening' (hs : is_lower_set s) (ε : ℝ) : is_lower_set (cthickening ε s) := by { rw cthickening_eq_Inter_thickening'', exact is_lower_set_Inter₂ (λ δ hδ, hs.thickening' _) } end metric_space /-! ### `ℝⁿ` -/ section finite variables [finite ι] {s : set (ι → ℝ)} {x y : ι → ℝ} {δ : ℝ} lemma is_upper_set.mem_interior_of_forall_lt (hs : is_upper_set s) (hx : x ∈ closure s) (h : ∀ i, x i < y i) : y ∈ interior s := begin casesI nonempty_fintype ι, obtain ⟨ε, hε, hxy⟩ := pi.exists_forall_pos_add_lt h, obtain ⟨z, hz, hxz⟩ := metric.mem_closure_iff.1 hx _ hε, rw dist_pi_lt_iff hε at hxz, have hyz : ∀ i, z i < y i, { refine λ i, (hxy _).trans_le' (sub_le_iff_le_add'.1 $ (le_abs_self _).trans _), rw [←real.norm_eq_abs, ←dist_eq_norm'], exact (hxz _).le }, obtain ⟨δ, hδ, hyz⟩ := pi.exists_forall_pos_add_lt hyz, refine mem_interior.2 ⟨ball y δ, _, is_open_ball, mem_ball_self hδ⟩, rintro w hw, refine hs (λ i, _) hz, simp_rw [ball_pi _ hδ, real.ball_eq_Ioo] at hw, exact ((lt_sub_iff_add_lt.2 $ hyz _).trans (hw _ $ mem_univ _).1).le, end lemma is_lower_set.mem_interior_of_forall_lt (hs : is_lower_set s) (hx : x ∈ closure s) (h : ∀ i, y i < x i) : y ∈ interior s := begin casesI nonempty_fintype ι, obtain ⟨ε, hε, hxy⟩ := pi.exists_forall_pos_add_lt h, obtain ⟨z, hz, hxz⟩ := metric.mem_closure_iff.1 hx _ hε, rw dist_pi_lt_iff hε at hxz, have hyz : ∀ i, y i < z i, { refine λ i, (lt_sub_iff_add_lt.2 $ hxy _).trans_le (sub_le_comm.1 $ (le_abs_self _).trans _), rw [←real.norm_eq_abs, ←dist_eq_norm], exact (hxz _).le }, obtain ⟨δ, hδ, hyz⟩ := pi.exists_forall_pos_add_lt hyz, refine mem_interior.2 ⟨ball y δ, _, is_open_ball, mem_ball_self hδ⟩, rintro w hw, refine hs (λ i, _) hz, simp_rw [ball_pi _ hδ, real.ball_eq_Ioo] at hw, exact ((hw _ $ mem_univ _).2.trans $ hyz _).le, end end finite section fintype variables [fintype ι] {s : set (ι → ℝ)} {x y : ι → ℝ} {δ : ℝ} lemma is_upper_set.exists_subset_ball (hs : is_upper_set s) (hx : x ∈ closure s) (hδ : 0 < δ) : ∃ y, closed_ball y (δ/4) ⊆ closed_ball x δ ∧ closed_ball y (δ/4) ⊆ interior s := begin refine ⟨x + const _ (3/4*δ), closed_ball_subset_closed_ball' _, _⟩, { rw dist_self_add_left, refine (add_le_add_left (pi_norm_const_le $ 3 / 4 * δ) _).trans_eq _, simp [real.norm_of_nonneg, hδ.le, zero_le_three], ring_nf }, obtain ⟨y, hy, hxy⟩ := metric.mem_closure_iff.1 hx _ (div_pos hδ zero_lt_four), refine λ z hz, hs.mem_interior_of_forall_lt (subset_closure hy) (λ i, _), rw [mem_closed_ball, dist_eq_norm'] at hz, rw dist_eq_norm at hxy, replace hxy := (norm_le_pi_norm _ i).trans hxy.le, replace hz := (norm_le_pi_norm _ i).trans hz, dsimp at hxy hz, rw abs_sub_le_iff at hxy hz, linarith, end lemma is_lower_set.exists_subset_ball (hs : is_lower_set s) (hx : x ∈ closure s) (hδ : 0 < δ) : ∃ y, closed_ball y (δ/4) ⊆ closed_ball x δ ∧ closed_ball y (δ/4) ⊆ interior s := begin refine ⟨x - const _ (3/4*δ), closed_ball_subset_closed_ball' _, _⟩, { rw dist_self_sub_left, refine (add_le_add_left (pi_norm_const_le $ 3 / 4 * δ) _).trans_eq _, simp [real.norm_of_nonneg, hδ.le, zero_le_three], ring_nf }, obtain ⟨y, hy, hxy⟩ := metric.mem_closure_iff.1 hx _ (div_pos hδ zero_lt_four), refine λ z hz, hs.mem_interior_of_forall_lt (subset_closure hy) (λ i, _), rw [mem_closed_ball, dist_eq_norm'] at hz, rw dist_eq_norm at hxy, replace hxy := (norm_le_pi_norm _ i).trans hxy.le, replace hz := (norm_le_pi_norm _ i).trans hz, dsimp at hxy hz, rw abs_sub_le_iff at hxy hz, linarith, end end fintype
83be8cc25bd21a475fa38a51139529072b9bc5b4
a4673261e60b025e2c8c825dfa4ab9108246c32e
/tests/lean/binsearch.lean
b4b41f0cf0db998b54e38f131565cbe31619c792
[ "Apache-2.0" ]
permissive
jcommelin/lean4
c02dec0cc32c4bccab009285475f265f17d73228
2909313475588cc20ac0436e55548a4502050d0a
refs/heads/master
1,674,129,550,893
1,606,415,348,000
1,606,415,348,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
448
lean
partial def mkAssocArray : Nat → Array (Nat × Bool) → Array (Nat × Bool) | 0, as => as | i+1, as => mkAssocArray i (as.push (i, i % 2 == 0)) def tst (n : Nat) : IO Unit := do let as := mkAssocArray n Array.empty; IO.println as; let as := as.qsort (fun a b => a.1 < b.1); (2*n).forM $ fun i => do let entry := as.binSearch (i, false) (fun a b => a.1 < b.1); IO.println (">> " ++ toString i ++ " ==> " ++ toString entry) #eval tst 10
061d199c8625df12ce2bf424eb156a8584399473
f5f7e6fae601a5fe3cac7cc3ed353ed781d62419
/src/logic/embedding.lean
d134cd7c41eca0ba3a1ad79703de9f1102713db9
[ "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
5,865
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 Injective functions. -/ import data.equiv.basic data.option.basic universes u v w x namespace function structure embedding (α : Sort*) (β : Sort*) := (to_fun : α → β) (inj : injective to_fun) infixr ` ↪ `:25 := embedding instance {α : Sort u} {β : Sort v} : has_coe_to_fun (α ↪ β) := ⟨_, embedding.to_fun⟩ end function protected def equiv.to_embedding {α : Sort u} {β : Sort v} (f : α ≃ β) : α ↪ β := ⟨f, f.injective⟩ @[simp] theorem equiv.to_embedding_coe_fn {α : Sort u} {β : Sort v} (f : α ≃ β) : (f.to_embedding : α → β) = f := rfl namespace function namespace embedding @[simp] theorem to_fun_eq_coe {α β} (f : α ↪ β) : to_fun f = f := rfl @[simp] theorem coe_fn_mk {α β} (f : α → β) (i) : (@mk _ _ f i : α → β) = f := rfl theorem inj' {α β} : ∀ (f : α ↪ β), injective f | ⟨f, hf⟩ := hf @[refl] protected def refl (α : Sort*) : α ↪ α := ⟨id, injective_id⟩ @[trans] protected def trans {α β γ} (f : α ↪ β) (g : β ↪ γ) : α ↪ γ := ⟨_, injective_comp g.inj' f.inj'⟩ @[simp] theorem refl_apply {α} (x : α) : embedding.refl α x = x := rfl @[simp] theorem trans_apply {α β γ} (f : α ↪ β) (g : β ↪ γ) (a : α) : (f.trans g) a = g (f a) := rfl protected def congr {α : Sort u} {β : Sort v} {γ : Sort w} {δ : Sort x} (e₁ : α ≃ β) (e₂ : γ ≃ δ) (f : α ↪ γ) : (β ↪ δ) := (equiv.to_embedding e₁.symm).trans (f.trans e₂.to_embedding) protected noncomputable def of_surjective {α β} {f : β → α} (hf : surjective f) : α ↪ β := ⟨surj_inv hf, injective_surj_inv _⟩ protected noncomputable def equiv_of_surjective {α β} (f : α ↪ β) (hf : surjective f) : α ≃ β := equiv.of_bijective ⟨f.inj, hf⟩ protected def of_not_nonempty {α β} (hα : ¬ nonempty α) : α ↪ β := ⟨λa, (hα ⟨a⟩).elim, assume a, (hα ⟨a⟩).elim⟩ noncomputable def set_value {α β} (f : α ↪ β) (a : α) (b : β) : α ↪ β := by haveI := classical.dec; exact if h : ∃ a', f a' = b then (equiv.swap a (classical.some h)).to_embedding.trans f else ⟨λ a', if a' = a then b else f a', λ a₁ a₂ e, begin simp at e, split_ifs at e with h₁ h₂, { cc }, { cases h ⟨_, e.symm⟩ }, { cases h ⟨_, e⟩ }, { exact f.2 e } end⟩ theorem set_value_eq {α β} (f : α ↪ β) (a : α) (b : β) : set_value f a b a = b := begin rw [set_value], cases classical.dec (∃ a', f a' = b); dsimp [dite], {simp}, simp [equiv.swap_apply_left], apply classical.some_spec h end /-- Embedding into `option` -/ protected def some {α} : α ↪ option α := ⟨some, option.injective_some α⟩ def subtype {α} (p : α → Prop) : subtype p ↪ α := ⟨subtype.val, λ _ _, subtype.eq'⟩ /-- Restrict the codomain of an embedding. -/ def cod_restrict {α β} (p : set β) (f : α ↪ β) (H : ∀ a, f a ∈ p) : α ↪ p := ⟨λ a, ⟨f a, H a⟩, λ a b h, f.inj (@congr_arg _ _ _ _ subtype.val h)⟩ @[simp] theorem cod_restrict_apply {α β} (p) (f : α ↪ β) (H a) : cod_restrict p f H a = ⟨f a, H a⟩ := rfl def prod_congr {α β γ δ : Type*} (e₁ : α ↪ β) (e₂ : γ ↪ δ) : α × γ ↪ β × δ := ⟨assume ⟨a, b⟩, (e₁ a, e₂ b), assume ⟨a₁, b₁⟩ ⟨a₂, b₂⟩ h, have a₁ = a₂ ∧ b₁ = b₂, from (prod.mk.inj h).imp (assume h, e₁.inj h) (assume h, e₂.inj h), this.left ▸ this.right ▸ rfl⟩ section sum open sum def sum_congr {α β γ δ : Type*} (e₁ : α ↪ β) (e₂ : γ ↪ δ) : α ⊕ γ ↪ β ⊕ δ := ⟨assume s, match s with inl a := inl (e₁ a) | inr b := inr (e₂ b) end, assume s₁ s₂ h, match s₁, s₂, h with | inl a₁, inl a₂, h := congr_arg inl $ e₁.inj $ inl.inj h | inr b₁, inr b₂, h := congr_arg inr $ e₂.inj $ inr.inj h end⟩ @[simp] theorem sum_congr_apply_inl {α β γ δ} (e₁ : α ↪ β) (e₂ : γ ↪ δ) (a) : sum_congr e₁ e₂ (inl a) = inl (e₁ a) := rfl @[simp] theorem sum_congr_apply_inr {α β γ δ} (e₁ : α ↪ β) (e₂ : γ ↪ δ) (b) : sum_congr e₁ e₂ (inr b) = inr (e₂ b) := rfl end sum section sigma open sigma def sigma_congr_right {α : Type*} {β γ : α → Type*} (e : ∀ a, β a ↪ γ a) : sigma β ↪ sigma γ := ⟨λ ⟨a, b⟩, ⟨a, e a b⟩, λ ⟨a₁, b₁⟩ ⟨a₂, b₂⟩ h, begin injection h with h₁ h₂, subst a₂, congr, exact (e a₁).2 (eq_of_heq h₂) end⟩ end sigma def Pi_congr_right {α : Sort*} {β γ : α → Sort*} (e : ∀ a, β a ↪ γ a) : (Π a, β a) ↪ (Π a, γ a) := ⟨λf a, e a (f a), λ f₁ f₂ h, funext $ λ a, (e a).inj (congr_fun h a)⟩ def arrow_congr_left {α : Sort u} {β : Sort v} {γ : Sort w} (e : α ↪ β) : (γ → α) ↪ (γ → β) := Pi_congr_right (λ _, e) noncomputable def arrow_congr_right {α : Sort u} {β : Sort v} {γ : Sort w} [inhabited γ] (e : α ↪ β) : (α → γ) ↪ (β → γ) := by haveI := classical.prop_decidable; exact let f' : (α → γ) → (β → γ) := λf b, if h : ∃c, e c = b then f (classical.some h) else default γ in ⟨f', assume f₁ f₂ h, funext $ assume c, have ∃c', e c' = e c, from ⟨c, rfl⟩, have eq' : f' f₁ (e c) = f' f₂ (e c), from congr_fun h _, have eq_b : classical.some this = c, from e.inj $ classical.some_spec this, by simp [f', this, if_pos, eq_b] at eq'; assumption⟩ end embedding end function namespace set /-- The injection map is an embedding between subsets. -/ def embedding_of_subset {α} {s t : set α} (h : s ⊆ t) : s ↪ t := ⟨λ x, ⟨x.1, h x.2⟩, λ ⟨x, hx⟩ ⟨y, hy⟩ h, by congr; injection h⟩ end set
4dfe75930d06a4aba6e6959722302639ac4f63a9
ef47cc39429c8f578004338bf040c9c309b26766
/src/for_mathlib/category_theory/limits/shapes/kernels.lean
422bb23388aac70147ab7eaa17abbd6ebe8fa6f0
[]
no_license
farmanb/lftcm2020
29f1c646437a8f8b00ee2e07c8b663ca04bfa16f
1948bfaf58e3b340b39cd9b8366aa4f13e5ffa7d
refs/heads/master
1,669,697,152,764
1,595,275,412,000
1,595,275,412,000
285,654,242
0
0
null
1,596,742,086,000
1,596,742,085,000
null
UTF-8
Lean
false
false
3,983
lean
import category_theory.limits.shapes.kernels open category_theory namespace category_theory.limits universes v u variables {C : Type u} [category.{v} C] [has_zero_morphisms C] @[simps] def cokernel_comp_is_iso {X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z) [has_cokernel (f ≫ g)] [has_cokernel f] [is_iso g] : cokernel (f ≫ g) ≅ cokernel f := { hom := cokernel.desc _ (inv g ≫ cokernel.π f) (by simp), inv := cokernel.desc _ (g ≫ cokernel.π (f ≫ g)) (by rw [←category.assoc, cokernel.condition]), } @[simps] def cokernel_is_iso_comp {X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z) [has_cokernel (f ≫ g)] [is_iso f] [has_cokernel g] : cokernel (f ≫ g) ≅ cokernel g := { hom := cokernel.desc _ (cokernel.π g) (by simp), inv := cokernel.desc _ (cokernel.π (f ≫ g)) (by { rw [←cancel_epi f, ←category.assoc], simp, }), } @[simps] def kernel_comp_is_iso {X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z) [has_kernel (f ≫ g)] [has_kernel f] [is_iso g] : kernel (f ≫ g) ≅ kernel f := { hom := kernel.lift _ (kernel.ι _) begin rw [←cancel_mono g], simp, end, inv := kernel.lift _ (kernel.ι _) (by simp), } @[simps] def kernel_is_iso_comp {X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z) [has_kernel (f ≫ g)] [is_iso f] [has_kernel g] : kernel (f ≫ g) ≅ kernel g := { hom := kernel.lift _ (kernel.ι _ ≫ f) (by simp), inv := kernel.lift _ (kernel.ι _ ≫ inv f) (by simp), } namespace kernel_fork variables {X Y : C} {f : X ⟶ Y} /-- This is a slightly more convenient method to verify that a kernel fork is a limit cone. It only asks for a proof of facts that carry any mathematical content -/ def is_limit_aux (t : kernel_fork f) (lift : Π (s : kernel_fork f), s.X ⟶ t.X) (fac : ∀ (s : kernel_fork f), lift s ≫ t.ι = s.ι) (uniq : ∀ (s : kernel_fork f) (m : s.X ⟶ t.X) (w : m ≫ t.ι = s.ι), m = lift s) : is_limit t := { lift := lift, fac' := λ s j, by { cases j, { exact fac s, }, { simp, }, }, uniq' := λ s m w, uniq s m (w limits.walking_parallel_pair.zero), } /-- This is a more convenient formulation to show that a `kernel_fork` constructed using `kernel_fork.of_ι` is a limit cone. -/ def is_limit.of_ι {W : C} (g : W ⟶ X) (eq : g ≫ f = 0) (lift : Π {W' : C} (g' : W' ⟶ X) (eq' : g' ≫ f = 0), W' ⟶ W) (fac : ∀ {W' : C} (g' : W' ⟶ X) (eq' : g' ≫ f = 0), lift g' eq' ≫ g = g') (uniq : ∀ {W' : C} (g' : W' ⟶ X) (eq' : g' ≫ f = 0) (m : W' ⟶ W) (w : m ≫ g = g'), m = lift g' eq') : is_limit (kernel_fork.of_ι g eq) := is_limit_aux _ (λ s, lift s.ι s.condition) (λ s, fac s.ι s.condition) (λ s, uniq s.ι s.condition) end kernel_fork namespace cokernel_cofork variables {X Y : C} {f : X ⟶ Y} /-- This is a slightly more convenient method to verify that a cokernel cofork is a colimit cocone. It only asks for a proof of facts that carry any mathematical content -/ def is_colimit_aux (t : cokernel_cofork f) (desc : Π (s : cokernel_cofork f), t.X ⟶ s.X) (fac : ∀ (s : cokernel_cofork f), t.π ≫ desc s = s.π) (uniq : ∀ (s : cokernel_cofork f) (m : t.X ⟶ s.X) (w : t.π ≫ m = s.π), m = desc s) : is_colimit t := { desc := desc, fac' := λ s j, by { cases j, { simp, }, { exact fac s, }, }, uniq' := λ s m w, uniq s m (w limits.walking_parallel_pair.one), } /-- This is a more convenient formulation to show that a `cokernel_cofork` constructed using `cokernel_cofork.of_π` is a limit cone. -/ def is_colimit.of_π {Z : C} (g : Y ⟶ Z) (eq : f ≫ g = 0) (desc : Π {Z' : C} (g' : Y ⟶ Z') (eq' : f ≫ g' = 0), Z ⟶ Z') (fac : ∀ {Z' : C} (g' : Y ⟶ Z') (eq' : f ≫ g' = 0), g ≫ desc g' eq' = g') (uniq : ∀ {Z' : C} (g' : Y ⟶ Z') (eq' : f ≫ g' = 0) (m : Z ⟶ Z') (w : g ≫ m = g'), m = desc g' eq') : is_colimit (cokernel_cofork.of_π g eq) := is_colimit_aux _ (λ s, desc s.π s.condition) (λ s, fac s.π s.condition) (λ s, uniq s.π s.condition) end cokernel_cofork end category_theory.limits
cbda939a20961be0a39283044a2e8dbbbf50eace
aa5a655c05e5359a70646b7154e7cac59f0b4132
/src/Lean/Elab/SyntheticMVars.lean
dab641a8faf8b9bde020ef69fae55578cb0ceec5
[ "Apache-2.0" ]
permissive
lambdaxymox/lean4
ae943c960a42247e06eff25c35338268d07454cb
278d47c77270664ef29715faab467feac8a0f446
refs/heads/master
1,677,891,867,340
1,612,500,005,000
1,612,500,005,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
15,736
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 /- We must perform `occursCheck` here since `result` may contain `mvarId` when it has synthetic `sorry`s. -/ if (← occursCheck mvarId result) then assignExprMVar mvarId result return true else return false catch | ex@(Exception.internal id _) => if id == postponeExceptionId then set s return false else throw ex | ex@(Exception.error _ _) => if postponeOnError then set s return false else logException ex return 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; return 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 return true else return 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 } return 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 => return mvarDecl.stx | none => return 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 return 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
8f9b2e017b2e7d123bcf57587aa1d5ad72a635bd
2fbe653e4bc441efde5e5d250566e65538709888
/src/analysis/calculus/tangent_cone.lean
a1de6d5aab419aa6bba0d75e8f70f6ffaaf2191b
[ "Apache-2.0" ]
permissive
aceg00/mathlib
5e15e79a8af87ff7eb8c17e2629c442ef24e746b
8786ea6d6d46d6969ac9a869eb818bf100802882
refs/heads/master
1,649,202,698,930
1,580,924,783,000
1,580,924,783,000
149,197,272
0
0
Apache-2.0
1,537,224,208,000
1,537,224,207,000
null
UTF-8
Lean
false
false
18,836
lean
/- Copyright (c) 2019 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import analysis.convex.basic analysis.normed_space.bounded_linear_maps analysis.specific_limits /-! # Tangent cone In this file, we define two predicates `unique_diff_within_at 𝕜 s x` and `unique_diff_on 𝕜 s` ensuring that, if a function has two derivatives, then they have to coincide. As a direct definition of this fact (quantifying on all target types and all functions) would depend on universes, we use a more intrinsic definition: if all the possible tangent directions to the set `s` at the point `x` span a dense subset of the whole subset, it is easy to check that the derivative has to be unique. Therefore, we introduce the set of all tangent directions, named `tangent_cone_at`, and express `unique_diff_within_at` and `unique_diff_on` in terms of it. One should however think of this definition as an implementation detail: the only reason to introduce the predicates `unique_diff_within_at` and `unique_diff_on` is to ensure the uniqueness of the derivative. This is why their names reflect their uses, and not how they are defined. ## Implementation details Note that this file is imported by `fderiv.lean`. Hence, derivatives are not defined yet. The property of uniqueness of the derivative is therefore proved in `fderiv.lean`, but based on the properties of the tangent cone we prove here. -/ variables (𝕜 : Type*) [nondiscrete_normed_field 𝕜] variables {E : Type*} [normed_group E] [normed_space 𝕜 E] variables {F : Type*} [normed_group F] [normed_space 𝕜 F] variables {G : Type*} [normed_group G] [normed_space ℝ G] set_option class.instance_max_depth 50 open filter set open_locale topological_space /-- The set of all tangent directions to the set `s` at the point `x`. -/ def tangent_cone_at (s : set E) (x : E) : set E := {y : E | ∃(c : ℕ → 𝕜) (d : ℕ → E), (∀ᶠ n in at_top, x + d n ∈ s) ∧ (tendsto (λn, ∥c n∥) at_top at_top) ∧ (tendsto (λn, c n • d n) at_top (𝓝 y))} /-- A property ensuring that the tangent cone to `s` at `x` spans a dense subset of the whole space. The main role of this property is to ensure that the differential within `s` at `x` is unique, hence this name. The uniqueness it asserts is proved in `unique_diff_within_at.eq` in `fderiv.lean`. To avoid pathologies in dimension 0, we also require that `x` belongs to the closure of `s` (which is automatic when `E` is not `0`-dimensional). -/ def unique_diff_within_at (s : set E) (x : E) : Prop := closure ((submodule.span 𝕜 (tangent_cone_at 𝕜 s x)) : set E) = univ ∧ x ∈ closure s /-- A property ensuring that the tangent cone to `s` at any of its points spans a dense subset of the whole space. The main role of this property is to ensure that the differential along `s` is unique, hence this name. The uniqueness it asserts is proved in `unique_diff_on.eq` in `fderiv.lean`. -/ def unique_diff_on (s : set E) : Prop := ∀x ∈ s, unique_diff_within_at 𝕜 s x variables {𝕜} {x y : E} {s t : set E} section tangent_cone /- This section is devoted to the properties of the tangent cone. -/ open normed_field lemma tangent_cone_univ : tangent_cone_at 𝕜 univ x = univ := begin refine univ_subset_iff.1 (λy hy, _), rcases exists_one_lt_norm 𝕜 with ⟨w, hw⟩, refine ⟨λn, w^n, λn, (w^n)⁻¹ • y, univ_mem_sets' (λn, mem_univ _), _, _⟩, { simp only [norm_pow], exact tendsto_pow_at_top_at_top_of_gt_1 hw }, { convert tendsto_const_nhds, ext n, have : w ^ n * (w ^ n)⁻¹ = 1, { apply mul_inv_cancel, apply pow_ne_zero, simpa [norm_eq_zero] using (ne_of_lt (lt_trans zero_lt_one hw)).symm }, rw [smul_smul, this, one_smul] } end lemma tangent_cone_mono (h : s ⊆ t) : tangent_cone_at 𝕜 s x ⊆ tangent_cone_at 𝕜 t x := begin rintros y ⟨c, d, ds, ctop, clim⟩, exact ⟨c, d, mem_sets_of_superset ds (λn hn, h hn), ctop, clim⟩ end /-- Auxiliary lemma ensuring that, under the assumptions defining the tangent cone, the sequence `d` tends to 0 at infinity. -/ lemma tangent_cone_at.lim_zero {α : Type*} (l : filter α) {c : α → 𝕜} {d : α → E} (hc : tendsto (λn, ∥c n∥) l at_top) (hd : tendsto (λn, c n • d n) l (𝓝 y)) : tendsto d l (𝓝 0) := begin have A : tendsto (λn, ∥c n∥⁻¹) l (𝓝 0) := tendsto_inv_at_top_zero.comp hc, have B : tendsto (λn, ∥c n • d n∥) l (𝓝 ∥y∥) := (continuous_norm.tendsto _).comp hd, have C : tendsto (λn, ∥c n∥⁻¹ * ∥c n • d n∥) l (𝓝 (0 * ∥y∥)) := A.mul B, rw zero_mul at C, have : ∀ᶠ n in l, ∥c n∥⁻¹ * ∥c n • d n∥ = ∥d n∥, { apply mem_sets_of_superset (ne_mem_of_tendsto_norm_at_top hc 0) (λn hn, _), rw [mem_set_of_eq, norm_smul, ← mul_assoc, inv_mul_cancel, one_mul], rwa [ne.def, norm_eq_zero] }, have D : tendsto (λ n, ∥d n∥) l (𝓝 0) := tendsto.congr' this C, rw tendsto_zero_iff_norm_tendsto_zero, exact D end lemma tangent_cone_mono_nhds (h : nhds_within x s ≤ nhds_within x t) : tangent_cone_at 𝕜 s x ⊆ tangent_cone_at 𝕜 t x := begin rintros y ⟨c, d, ds, ctop, clim⟩, refine ⟨c, d, _, ctop, clim⟩, suffices : tendsto (λ n, x + d n) at_top (nhds_within x t), from tendsto_principal.1 (tendsto_inf.1 this).2, apply tendsto_le_right h, refine tendsto_inf.2 ⟨_, tendsto_principal.2 ds⟩, simpa only [add_zero] using tendsto_const_nhds.add (tangent_cone_at.lim_zero at_top ctop clim) end /-- Tangent cone of `s` at `x` depends only on `nhds_within x s`. -/ lemma tangent_cone_congr (h : nhds_within x s = nhds_within x t) : tangent_cone_at 𝕜 s x = tangent_cone_at 𝕜 t x := subset.antisymm (tangent_cone_mono_nhds $ le_of_eq h) (tangent_cone_mono_nhds $ le_of_eq h.symm) /-- Intersecting with a neighborhood of the point does not change the tangent cone. -/ lemma tangent_cone_inter_nhds (ht : t ∈ 𝓝 x) : tangent_cone_at 𝕜 (s ∩ t) x = tangent_cone_at 𝕜 s x := tangent_cone_congr (nhds_within_restrict' _ ht).symm /-- The tangent cone of a product contains the tangent cone of its left factor. -/ lemma subset_tangent_cone_prod_left {t : set F} {y : F} (ht : y ∈ closure t) : set.prod (tangent_cone_at 𝕜 s x) {(0 : F)} ⊆ tangent_cone_at 𝕜 (set.prod s t) (x, y) := begin rintros ⟨v, w⟩ ⟨⟨c, d, hd, hc, hy⟩, hw⟩, have : w = 0, by simpa using hw, rw this, have : ∀n, ∃d', y + d' ∈ t ∧ ∥c n • d'∥ ≤ ((1:ℝ)/2)^n, { assume n, have c_pos : 0 < 1 + ∥c n∥ := add_pos_of_pos_of_nonneg zero_lt_one (norm_nonneg _), rcases metric.mem_closure_iff'.1 ht ((1 + ∥c n∥)⁻¹ * (1/2)^n) _ with ⟨z, z_pos, hz⟩, refine ⟨z - y, _, _⟩, { convert z_pos, abel }, { rw [norm_smul, ← dist_eq_norm, dist_comm], calc ∥c n∥ * dist y z ≤ (1 + ∥c n∥) * ((1 + ∥c n∥)⁻¹ * (1/2)^n) : begin apply mul_le_mul _ (le_of_lt hz) dist_nonneg (le_of_lt c_pos), simp only [zero_le_one, le_add_iff_nonneg_left] end ... = (1/2)^n : begin rw [← mul_assoc, mul_inv_cancel, one_mul], exact ne_of_gt c_pos end }, { apply mul_pos (inv_pos c_pos) (pow_pos _ _), norm_num } }, choose d' hd' using this, refine ⟨c, λn, (d n, d' n), _, hc, _⟩, show ∀ᶠ n in at_top, (x, y) + (d n, d' n) ∈ set.prod s t, { apply filter.mem_sets_of_superset hd, assume n hn, simp at hn, simp [hn, (hd' n).1] }, { apply tendsto_prod_mk_nhds hy, change tendsto (λ (n : ℕ), c n • d' n) at_top (𝓝 0), rw tendsto_zero_iff_norm_tendsto_zero, refine squeeze_zero (λn, norm_nonneg _) (λn, (hd' n).2) _, apply tendsto_pow_at_top_nhds_0_of_lt_1; norm_num } end /-- The tangent cone of a product contains the tangent cone of its right factor. -/ lemma subset_tangent_cone_prod_right {t : set F} {y : F} (hs : x ∈ closure s) : set.prod {(0 : E)} (tangent_cone_at 𝕜 t y) ⊆ tangent_cone_at 𝕜 (set.prod s t) (x, y) := begin rintros ⟨v, w⟩ ⟨hv, ⟨c, d, hd, hc, hy⟩⟩, have : v = 0, by simpa using hv, rw this, have : ∀n, ∃d', x + d' ∈ s ∧ ∥c n • d'∥ ≤ ((1:ℝ)/2)^n, { assume n, have c_pos : 0 < 1 + ∥c n∥ := add_pos_of_pos_of_nonneg zero_lt_one (norm_nonneg _), rcases metric.mem_closure_iff'.1 hs ((1 + ∥c n∥)⁻¹ * (1/2)^n) _ with ⟨z, z_pos, hz⟩, refine ⟨z - x, _, _⟩, { convert z_pos, abel }, { rw [norm_smul, ← dist_eq_norm, dist_comm], calc ∥c n∥ * dist x z ≤ (1 + ∥c n∥) * ((1 + ∥c n∥)⁻¹ * (1/2)^n) : begin apply mul_le_mul _ (le_of_lt hz) dist_nonneg (le_of_lt c_pos), simp only [zero_le_one, le_add_iff_nonneg_left] end ... = (1/2)^n : begin rw [← mul_assoc, mul_inv_cancel, one_mul], exact ne_of_gt c_pos end }, { apply mul_pos (inv_pos c_pos) (pow_pos _ _), norm_num } }, choose d' hd' using this, refine ⟨c, λn, (d' n, d n), _, hc, _⟩, show ∀ᶠ n in at_top, (x, y) + (d' n, d n) ∈ set.prod s t, { apply filter.mem_sets_of_superset hd, assume n hn, simp at hn, simp [hn, (hd' n).1] }, { apply tendsto_prod_mk_nhds _ hy, change tendsto (λ (n : ℕ), c n • d' n) at_top (𝓝 0), rw tendsto_zero_iff_norm_tendsto_zero, refine squeeze_zero (λn, norm_nonneg _) (λn, (hd' n).2) _, apply tendsto_pow_at_top_nhds_0_of_lt_1; norm_num } end /-- If a subset of a real vector space contains a segment, then the direction of this segment belongs to the tangent cone at its endpoints. -/ lemma mem_tangent_cone_of_segment_subset {s : set G} {x y : G} (h : segment x y ⊆ s) : y - x ∈ tangent_cone_at ℝ s x := begin let w : ℝ := 2, let c := λn:ℕ, (2:ℝ)^n, let d := λn:ℕ, (c n)⁻¹ • (y-x), refine ⟨c, d, filter.univ_mem_sets' (λn, h _), _, _⟩, show x + d n ∈ segment x y, { rw segment_eq_image, refine ⟨(c n)⁻¹, ⟨_, _⟩, _⟩, { rw inv_nonneg, apply pow_nonneg, norm_num }, { apply inv_le_one, apply one_le_pow_of_one_le, norm_num }, { simp only [d, sub_smul, smul_sub, one_smul], abel } }, show filter.tendsto (λ (n : ℕ), ∥c n∥) filter.at_top filter.at_top, { have : (λ (n : ℕ), ∥c n∥) = c, by { ext n, exact abs_of_nonneg (pow_nonneg (by norm_num) _) }, rw this, exact tendsto_pow_at_top_at_top_of_gt_1 (by norm_num) }, show filter.tendsto (λ (n : ℕ), c n • d n) filter.at_top (𝓝 (y - x)), { have : (λ (n : ℕ), c n • d n) = (λn, y - x), { ext n, simp only [d, smul_smul], rw [mul_inv_cancel, one_smul], exact pow_ne_zero _ (by norm_num) }, rw this, apply tendsto_const_nhds } end end tangent_cone section unique_diff /- This section is devoted to properties of the predicates `unique_diff_within_at` and `unique_diff_on`. -/ lemma unique_diff_within_at_univ : unique_diff_within_at 𝕜 univ x := by { rw [unique_diff_within_at, tangent_cone_univ], simp } lemma unique_diff_on_univ : unique_diff_on 𝕜 (univ : set E) := λx hx, unique_diff_within_at_univ lemma unique_diff_within_at.mono_nhds (h : unique_diff_within_at 𝕜 s x) (st : nhds_within x s ≤ nhds_within x t) : unique_diff_within_at 𝕜 t x := begin unfold unique_diff_within_at at *, rw [← univ_subset_iff, ← h.1], rw [mem_closure_iff_nhds_within_ne_bot] at h ⊢, exact ⟨closure_mono (submodule.span_mono (tangent_cone_mono_nhds st)), lattice.ne_bot_of_le_ne_bot h.2 st⟩ end lemma unique_diff_within_at.mono (h : unique_diff_within_at 𝕜 s x) (st : s ⊆ t) : unique_diff_within_at 𝕜 t x := h.mono_nhds $ nhds_within_mono _ st lemma unique_diff_within_at_congr (st : nhds_within x s = nhds_within x t) : unique_diff_within_at 𝕜 s x ↔ unique_diff_within_at 𝕜 t x := ⟨λ h, h.mono_nhds $ le_of_eq st, λ h, h.mono_nhds $ le_of_eq st.symm⟩ lemma unique_diff_within_at_inter (ht : t ∈ 𝓝 x) : unique_diff_within_at 𝕜 (s ∩ t) x ↔ unique_diff_within_at 𝕜 s x := unique_diff_within_at_congr $ (nhds_within_restrict' _ ht).symm lemma unique_diff_within_at.inter (hs : unique_diff_within_at 𝕜 s x) (ht : t ∈ 𝓝 x) : unique_diff_within_at 𝕜 (s ∩ t) x := (unique_diff_within_at_inter ht).2 hs lemma unique_diff_within_at_inter' (ht : t ∈ nhds_within x s) : unique_diff_within_at 𝕜 (s ∩ t) x ↔ unique_diff_within_at 𝕜 s x := unique_diff_within_at_congr $ (nhds_within_restrict'' _ ht).symm lemma unique_diff_within_at.inter' (hs : unique_diff_within_at 𝕜 s x) (ht : t ∈ nhds_within x s) : unique_diff_within_at 𝕜 (s ∩ t) x := (unique_diff_within_at_inter' ht).2 hs lemma is_open.unique_diff_within_at (hs : is_open s) (xs : x ∈ s) : unique_diff_within_at 𝕜 s x := begin have := unique_diff_within_at_univ.inter (mem_nhds_sets hs xs), rwa univ_inter at this end lemma unique_diff_on.inter (hs : unique_diff_on 𝕜 s) (ht : is_open t) : unique_diff_on 𝕜 (s ∩ t) := λx hx, (hs x hx.1).inter (mem_nhds_sets ht hx.2) lemma is_open.unique_diff_on (hs : is_open s) : unique_diff_on 𝕜 s := λx hx, is_open.unique_diff_within_at hs hx /-- The product of two sets of unique differentiability at points `x` and `y` has unique differentiability at `(x, y)`. -/ lemma unique_diff_within_at.prod {t : set F} {y : F} (hs : unique_diff_within_at 𝕜 s x) (ht : unique_diff_within_at 𝕜 t y) : unique_diff_within_at 𝕜 (set.prod s t) (x, y) := begin rw [unique_diff_within_at, ← univ_subset_iff] at ⊢ hs ht, split, { assume v _, rw metric.mem_closure_iff', assume ε ε_pos, rcases v with ⟨v₁, v₂⟩, rcases metric.mem_closure_iff'.1 (hs.1 (mem_univ v₁)) ε ε_pos with ⟨w₁, w₁_mem, h₁⟩, rcases metric.mem_closure_iff'.1 (ht.1 (mem_univ v₂)) ε ε_pos with ⟨w₂, w₂_mem, h₂⟩, have I₁ : (w₁, (0 : F)) ∈ submodule.span 𝕜 (tangent_cone_at 𝕜 (set.prod s t) (x, y)), { apply submodule.span_induction w₁_mem, { assume w hw, have : (w, (0 : F)) ∈ (set.prod (tangent_cone_at 𝕜 s x) {(0 : F)}), { rw mem_prod, simp [hw], apply mem_insert }, have : (w, (0 : F)) ∈ tangent_cone_at 𝕜 (set.prod s t) (x, y) := subset_tangent_cone_prod_left ht.2 this, exact submodule.subset_span this }, { exact submodule.zero_mem _ }, { assume a b ha hb, have : (a, (0 : F)) + (b, (0 : F)) = (a + b, (0 : F)), by simp, rw ← this, exact submodule.add_mem _ ha hb }, { assume c a ha, have : c • (0 : F) = (0 : F), by simp, rw ← this, exact submodule.smul_mem _ _ ha } }, have I₂ : ((0 : E), w₂) ∈ submodule.span 𝕜 (tangent_cone_at 𝕜 (set.prod s t) (x, y)), { apply submodule.span_induction w₂_mem, { assume w hw, have : ((0 : E), w) ∈ (set.prod {(0 : E)} (tangent_cone_at 𝕜 t y)), { rw mem_prod, simp [hw], apply mem_insert }, have : ((0 : E), w) ∈ tangent_cone_at 𝕜 (set.prod s t) (x, y) := subset_tangent_cone_prod_right hs.2 this, exact submodule.subset_span this }, { exact submodule.zero_mem _ }, { assume a b ha hb, have : ((0 : E), a) + ((0 : E), b) = ((0 : E), a + b), by simp, rw ← this, exact submodule.add_mem _ ha hb }, { assume c a ha, have : c • (0 : E) = (0 : E), by simp, rw ← this, exact submodule.smul_mem _ _ ha } }, have I : (w₁, w₂) ∈ submodule.span 𝕜 (tangent_cone_at 𝕜 (set.prod s t) (x, y)), { have : (w₁, (0 : F)) + ((0 : E), w₂) = (w₁, w₂), by simp, rw ← this, exact submodule.add_mem _ I₁ I₂ }, refine ⟨(w₁, w₂), I, _⟩, simp [dist, h₁, h₂] }, { simp [closure_prod_eq, mem_prod_iff, hs.2, ht.2] } end /-- The product of two sets of unique differentiability is a set of unique differentiability. -/ lemma unique_diff_on.prod {t : set F} (hs : unique_diff_on 𝕜 s) (ht : unique_diff_on 𝕜 t) : unique_diff_on 𝕜 (set.prod s t) := λ ⟨x, y⟩ h, unique_diff_within_at.prod (hs x h.1) (ht y h.2) /-- In a real vector space, a convex set with nonempty interior is a set of unique differentiability. -/ theorem unique_diff_on_convex {s : set G} (conv : convex s) (hs : (interior s).nonempty) : unique_diff_on ℝ s := begin assume x xs, have A : ∀v, ∃a∈ tangent_cone_at ℝ s x, ∃b∈ tangent_cone_at ℝ s x, ∃δ>(0:ℝ), δ • v = b-a, { assume v, rcases hs with ⟨y, hy⟩, have ys : y ∈ s := interior_subset hy, have : ∃(δ : ℝ), 0<δ ∧ y + δ • v ∈ s, { by_cases h : ∥v∥ = 0, { exact ⟨1, zero_lt_one, by simp [(norm_eq_zero _).1 h, ys]⟩ }, { rcases mem_interior.1 hy with ⟨u, us, u_open, yu⟩, rcases metric.is_open_iff.1 u_open y yu with ⟨ε, εpos, hε⟩, let δ := (ε/2) / ∥v∥, have δpos : 0 < δ := div_pos (half_pos εpos) (lt_of_le_of_ne (norm_nonneg _) (ne.symm h)), have : y + δ • v ∈ s, { apply us (hε _), rw [metric.mem_ball, dist_eq_norm], calc ∥(y + δ • v) - y ∥ = ∥δ • v∥ : by {congr' 1, abel } ... = ∥δ∥ * ∥v∥ : norm_smul _ _ ... = δ * ∥v∥ : by simp only [norm, abs_of_nonneg (le_of_lt δpos)] ... = ε /2 : div_mul_cancel _ h ... < ε : half_lt_self εpos }, exact ⟨δ, δpos, this⟩ } }, rcases this with ⟨δ, δpos, hδ⟩, refine ⟨y-x, _, (y + δ • v) - x, _, δ, δpos, by abel⟩, exact mem_tangent_cone_of_segment_subset (conv.segment_subset xs ys), exact mem_tangent_cone_of_segment_subset (conv.segment_subset xs hδ) }, have B : ∀v:G, v ∈ submodule.span ℝ (tangent_cone_at ℝ s x), { assume v, rcases A v with ⟨a, ha, b, hb, δ, hδ, h⟩, have : v = δ⁻¹ • (b - a), by { rw [← h, smul_smul, inv_mul_cancel, one_smul], exact (ne_of_gt hδ) }, rw this, exact submodule.smul_mem _ _ (submodule.sub_mem _ (submodule.subset_span hb) (submodule.subset_span ha)) }, refine ⟨univ_subset_iff.1 (λv hv, subset_closure (B v)), subset_closure xs⟩ end /-- The real interval `[0, 1]` is a set of unique differentiability. -/ lemma unique_diff_on_Icc_zero_one : unique_diff_on ℝ (Icc (0:ℝ) 1) := begin apply unique_diff_on_convex (convex_Icc 0 1), have : (1/(2:ℝ)) ∈ interior (Icc (0:ℝ) 1) := mem_interior.2 ⟨Ioo (0:ℝ) 1, Ioo_subset_Icc_self, is_open_Ioo, by norm_num, by norm_num⟩, exact ⟨_, this⟩ end end unique_diff
fcb753f05c0817ec025bd7ba46dd41178f1435ee
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/group_theory/schreier.lean
8cc1440ee1d67f9431768573b2bea3184f6de91a
[ "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
10,469
lean
/- Copyright (c) 2022 Thomas Browning. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Thomas Browning -/ import group_theory.abelianization import group_theory.exponent import group_theory.transfer /-! # Schreier's Lemma In this file we prove Schreier's lemma. ## Main results - `closure_mul_image_eq` : **Schreier's Lemma**: If `R : set G` is a right_transversal of `H : subgroup G` with `1 ∈ R`, and if `G` is generated by `S : set G`, then `H` is generated by the `set` `(R * S).image (λ g, g * (to_fun hR g)⁻¹)`. - `fg_of_index_ne_zero` : **Schreier's Lemma**: A finite index subgroup of a finitely generated group is finitely generated. - `card_commutator_le_of_finite_commutator_set`: A theorem of Schur: The size of the commutator subgroup is bounded in terms of the number of commutators. -/ open_locale pointwise namespace subgroup open mem_right_transversals variables {G : Type*} [group G] {H : subgroup G} {R S : set G} lemma closure_mul_image_mul_eq_top (hR : R ∈ right_transversals (H : set G)) (hR1 : (1 : G) ∈ R) (hS : closure S = ⊤) : (closure ((R * S).image (λ g, g * (to_fun hR g)⁻¹)) : set G) * R = ⊤ := begin let f : G → R := λ g, to_fun hR g, let U : set G := (R * S).image (λ g, g * (f g)⁻¹), change (closure U : set G) * R = ⊤, refine top_le_iff.mp (λ g hg, _), apply closure_induction_right (eq_top_iff.mp hS (mem_top g)), { exact ⟨1, 1, (closure U).one_mem, hR1, one_mul 1⟩ }, { rintros - s hs ⟨u, r, hu, hr, rfl⟩, rw show u * r * s = u * ((r * s) * (f (r * s))⁻¹) * f (r * s), by group, refine set.mul_mem_mul ((closure U).mul_mem hu _) (f (r * s)).coe_prop, exact subset_closure ⟨r * s, set.mul_mem_mul hr hs, rfl⟩ }, { rintros - s hs ⟨u, r, hu, hr, rfl⟩, rw show u * r * s⁻¹ = u * (f (r * s⁻¹) * s * r⁻¹)⁻¹ * f (r * s⁻¹), by group, refine set.mul_mem_mul ((closure U).mul_mem hu ((closure U).inv_mem _)) (f (r * s⁻¹)).2, refine subset_closure ⟨f (r * s⁻¹) * s, set.mul_mem_mul (f (r * s⁻¹)).2 hs, _⟩, rw [mul_right_inj, inv_inj, ←subtype.coe_mk r hr, ←subtype.ext_iff, subtype.coe_mk], apply (mem_right_transversals_iff_exists_unique_mul_inv_mem.mp hR (f (r * s⁻¹) * s)).unique (mul_inv_to_fun_mem hR (f (r * s⁻¹) * s)), rw [mul_assoc, ←inv_inv s, ←mul_inv_rev, inv_inv], exact to_fun_mul_inv_mem hR (r * s⁻¹) }, end /-- **Schreier's Lemma**: If `R : set G` is a right_transversal of `H : subgroup G` with `1 ∈ R`, and if `G` is generated by `S : set G`, then `H` is generated by the `set` `(R * S).image (λ g, g * (to_fun hR g)⁻¹)`. -/ lemma closure_mul_image_eq (hR : R ∈ right_transversals (H : set G)) (hR1 : (1 : G) ∈ R) (hS : closure S = ⊤) : closure ((R * S).image (λ g, g * (to_fun hR g)⁻¹)) = H := begin have hU : closure ((R * S).image (λ g, g * (to_fun hR g)⁻¹)) ≤ H, { rw closure_le, rintros - ⟨g, -, rfl⟩, exact mul_inv_to_fun_mem hR g }, refine le_antisymm hU (λ h hh, _), obtain ⟨g, r, hg, hr, rfl⟩ := show h ∈ _, from eq_top_iff.mp (closure_mul_image_mul_eq_top hR hR1 hS) (mem_top h), suffices : (⟨r, hr⟩ : R) = (⟨1, hR1⟩ : R), { rwa [show r = 1, from subtype.ext_iff.mp this, mul_one] }, apply (mem_right_transversals_iff_exists_unique_mul_inv_mem.mp hR r).unique, { rw [subtype.coe_mk, mul_inv_self], exact H.one_mem }, { rw [subtype.coe_mk, inv_one, mul_one], exact (H.mul_mem_cancel_left (hU hg)).mp hh }, end /-- **Schreier's Lemma**: If `R : set G` is a right_transversal of `H : subgroup G` with `1 ∈ R`, and if `G` is generated by `S : set G`, then `H` is generated by the `set` `(R * S).image (λ g, g * (to_fun hR g)⁻¹)`. -/ lemma closure_mul_image_eq_top (hR : R ∈ right_transversals (H : set G)) (hR1 : (1 : G) ∈ R) (hS : closure S = ⊤) : closure ((R * S).image (λ g, ⟨g * (to_fun hR g)⁻¹, mul_inv_to_fun_mem hR g⟩) : set H) = ⊤ := begin rw [eq_top_iff, ←map_subtype_le_map_subtype, monoid_hom.map_closure, set.image_image], exact (map_subtype_le ⊤).trans (ge_of_eq (closure_mul_image_eq hR hR1 hS)), end /-- **Schreier's Lemma**: If `R : finset G` is a right_transversal of `H : subgroup G` with `1 ∈ R`, and if `G` is generated by `S : finset G`, then `H` is generated by the `finset` `(R * S).image (λ g, g * (to_fun hR g)⁻¹)`. -/ lemma closure_mul_image_eq_top' [decidable_eq G] {R S : finset G} (hR : (R : set G) ∈ right_transversals (H : set G)) (hR1 : (1 : G) ∈ R) (hS : closure (S : set G) = ⊤) : closure ((((R * S).image (λ g, ⟨_, mul_inv_to_fun_mem hR g⟩)) : finset H) : set H) = ⊤ := begin rw [finset.coe_image, finset.coe_mul], exact closure_mul_image_eq_top hR hR1 hS, end variables (H) lemma exists_finset_card_le_mul [finite_index H] {S : finset G} (hS : closure (S : set G) = ⊤) : ∃ T : finset H, T.card ≤ H.index * S.card ∧ closure (T : set H) = ⊤ := begin letI := H.fintype_quotient_of_finite_index, haveI : decidable_eq G := classical.dec_eq G, obtain ⟨R₀, hR : R₀ ∈ right_transversals (H : set G), hR1⟩ := exists_right_transversal (1 : G), haveI : fintype R₀ := fintype.of_equiv _ (mem_right_transversals.to_equiv hR), let R : finset G := set.to_finset R₀, replace hR : (R : set G) ∈ right_transversals (H : set G) := by rwa set.coe_to_finset, replace hR1 : (1 : G) ∈ R := by rwa set.mem_to_finset, refine ⟨_, _, closure_mul_image_eq_top' hR hR1 hS⟩, calc _ ≤ (R * S).card : finset.card_image_le ... ≤ (R ×ˢ S).card : finset.card_image_le ... = R.card * S.card : R.card_product S ... = H.index * S.card : congr_arg (* S.card) _, calc R.card = fintype.card R : (fintype.card_coe R).symm ... = _ : (fintype.card_congr (mem_right_transversals.to_equiv hR)).symm ... = fintype.card (G ⧸ H) : quotient_group.card_quotient_right_rel H ... = H.index : H.index_eq_card.symm, end /-- **Schreier's Lemma**: A finite index subgroup of a finitely generated group is finitely generated. -/ instance fg_of_index_ne_zero [hG : group.fg G] [finite_index H] : group.fg H := begin obtain ⟨S, hS⟩ := hG.1, obtain ⟨T, -, hT⟩ := exists_finset_card_le_mul H hS, exact ⟨⟨T, hT⟩⟩, end lemma rank_le_index_mul_rank [hG : group.fg G] [finite_index H] : group.rank H ≤ H.index * group.rank G := begin haveI := H.fg_of_index_ne_zero, obtain ⟨S, hS₀, hS⟩ := group.rank_spec G, obtain ⟨T, hT₀, hT⟩ := exists_finset_card_le_mul H hS, calc group.rank H ≤ T.card : group.rank_le H hT ... ≤ H.index * S.card : hT₀ ... = H.index * group.rank G : congr_arg ((*) H.index) hS₀, end variables (G) /-- If `G` has `n` commutators `[g₁, g₂]`, then `|G'| ∣ [G : Z(G)] ^ ([G : Z(G)] * n + 1)`, where `G'` denotes the commutator of `G`. -/ lemma card_commutator_dvd_index_center_pow [finite (commutator_set G)] : nat.card (commutator G) ∣ (center G).index ^ ((center G).index * nat.card (commutator_set G) + 1) := begin -- First handle the case when `Z(G)` has infinite index and `[G : Z(G)]` is defined to be `0` by_cases hG : (center G).index = 0, { simp_rw [hG, zero_mul, zero_add, pow_one, dvd_zero] }, haveI : finite_index (center G) := ⟨hG⟩, -- Rewrite as `|Z(G) ∩ G'| * [G' : Z(G) ∩ G'] ∣ [G : Z(G)] ^ ([G : Z(G)] * n) * [G : Z(G)]` rw [←((center G).subgroup_of (commutator G)).card_mul_index, pow_succ'], -- We have `h1 : [G' : Z(G) ∩ G'] ∣ [G : Z(G)]` have h1 := relindex_dvd_index_of_normal (center G) (commutator G), -- So we can reduce to proving `|Z(G) ∩ G'| ∣ [G : Z(G)] ^ ([G : Z(G)] * n)` refine mul_dvd_mul _ h1, -- We know that `[G' : Z(G) ∩ G'] < ∞` by `h1` and `hG` haveI : finite_index ((center G).subgroup_of (commutator G)) := ⟨ne_zero_of_dvd_ne_zero hG h1⟩, -- We have `h2 : rank (Z(G) ∩ G') ≤ [G' : Z(G) ∩ G'] * rank G'` by Schreier's lemma have h2 := rank_le_index_mul_rank ((center G).subgroup_of (commutator G)), -- We have `h3 : [G' : Z(G) ∩ G'] * rank G' ≤ [G : Z(G)] * n` by `h1` and `rank G' ≤ n` have h3 := nat.mul_le_mul (nat.le_of_dvd (nat.pos_of_ne_zero hG) h1) (rank_commutator_le_card G), -- So we can reduce to proving `|Z(G) ∩ G'| ∣ [G : Z(G)] ^ rank (Z(G) ∩ G')` refine dvd_trans _ (pow_dvd_pow (center G).index (h2.trans h3)), -- `Z(G) ∩ G'` is abelian, so it enough to prove that `g ^ [G : Z(G)] = 1` for `g ∈ Z(G) ∩ G'` apply card_dvd_exponent_pow_rank' _ (λ g, _), -- `Z(G)` is abelian, so `g ∈ Z(G) ∩ G' ≤ G' ≤ ker (transfer : G → Z(G))` have := abelianization.commutator_subset_ker (monoid_hom.transfer_center_pow G) g.1.2, -- `transfer g` is defeq to `g ^ [G : Z(G)]`, so we are done simpa only [monoid_hom.mem_ker, subtype.ext_iff] using this, end /-- A bound for the size of the commutator subgroup in terms of the number of commutators. -/ def card_commutator_bound (n : ℕ) := (n ^ (2 * n)) ^ (n ^ (2 * n + 1) + 1) /-- A theorem of Schur: The size of the commutator subgroup is bounded in terms of the number of commutators. -/ lemma card_commutator_le_of_finite_commutator_set [finite (commutator_set G)] : nat.card (commutator G) ≤ card_commutator_bound (nat.card (commutator_set G)) := begin have h1 := index_center_le_pow (closure_commutator_representatives G), have h2 := card_commutator_dvd_index_center_pow (closure_commutator_representatives G), rw card_commutator_set_closure_commutator_representatives at h1 h2, rw card_commutator_closure_commutator_representatives at h2, replace h1 := h1.trans (nat.pow_le_pow_of_le_right finite.card_pos (rank_closure_commutator_representations_le G)), replace h2 := h2.trans (pow_dvd_pow _ (add_le_add_right (mul_le_mul_right' h1 _) 1)), rw ← pow_succ' at h2, refine (nat.le_of_dvd _ h2).trans (nat.pow_le_pow_of_le_left h1 _), exact pow_pos (nat.pos_of_ne_zero finite_index.finite_index) _, end /-- A theorem of Schur: A group with finitely many commutators has finite commutator subgroup. -/ instance [finite (commutator_set G)] : finite (commutator G) := begin have h2 := card_commutator_dvd_index_center_pow (closure_commutator_representatives G), refine nat.finite_of_card_ne_zero (λ h, _), rw [card_commutator_closure_commutator_representatives, h, zero_dvd_iff] at h2, exact finite_index.finite_index (pow_eq_zero h2), end end subgroup
886bb80e5d05916ec6d6c98246c2a3ae1eb8ed83
b2fe74b11b57d362c13326bc5651244f111fa6f4
/src/topology/list.lean
e3c32d28735804cd5e6913c0563bd4b8bb33d044
[ "Apache-2.0" ]
permissive
midfield/mathlib
c4db5fa898b5ac8f2f80ae0d00c95eb6f745f4c7
775edc615ecec631d65b6180dbcc7bc26c3abc26
refs/heads/master
1,675,330,551,921
1,608,304,514,000
1,608,304,514,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
8,889
lean
/- Copyright (c) 2019 Reid Barton. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl -/ import topology.constructions import topology.algebra.group /-! # Topology on lists and vectors -/ open topological_space set filter open_locale topological_space filter variables {α : Type*} {β : Type*} [topological_space α] [topological_space β] instance : topological_space (list α) := topological_space.mk_of_nhds (traverse nhds) lemma nhds_list (as : list α) : 𝓝 as = traverse 𝓝 as := begin refine nhds_mk_of_nhds _ _ _ _, { assume l, induction l, case list.nil { exact le_refl _ }, case list.cons : a l ih { suffices : list.cons <$> pure a <*> pure l ≤ list.cons <$> 𝓝 a <*> traverse 𝓝 l, { simpa only [] with functor_norm using this }, exact filter.seq_mono (filter.map_mono $ pure_le_nhds a) ih } }, { assume l s hs, rcases (mem_traverse_sets_iff _ _).1 hs with ⟨u, hu, hus⟩, clear as hs, have : ∃v:list (set α), l.forall₂ (λa s, is_open s ∧ a ∈ s) v ∧ sequence v ⊆ s, { induction hu generalizing s, case list.forall₂.nil : hs this { existsi [], simpa only [list.forall₂_nil_left_iff, exists_eq_left] }, case list.forall₂.cons : a s as ss ht h ih t hts { rcases mem_nhds_sets_iff.1 ht with ⟨u, hut, hu⟩, rcases ih (subset.refl _) with ⟨v, hv, hvss⟩, exact ⟨u::v, list.forall₂.cons hu hv, subset.trans (set.seq_mono (set.image_subset _ hut) hvss) hts⟩ } }, rcases this with ⟨v, hv, hvs⟩, refine ⟨sequence v, mem_traverse_sets _ _ _, hvs, _⟩, { exact hv.imp (assume a s ⟨hs, ha⟩, mem_nhds_sets hs ha) }, { assume u hu, have hu := (list.mem_traverse _ _).1 hu, have : list.forall₂ (λa s, is_open s ∧ a ∈ s) u v, { refine list.forall₂.flip _, replace hv := hv.flip, simp only [list.forall₂_and_left, flip] at ⊢ hv, exact ⟨hv.1, hu.flip⟩ }, refine mem_sets_of_superset _ hvs, exact mem_traverse_sets _ _ (this.imp $ assume a s ⟨hs, ha⟩, mem_nhds_sets hs ha) } } end lemma nhds_nil : 𝓝 ([] : list α) = pure [] := by rw [nhds_list, list.traverse_nil _]; apply_instance lemma nhds_cons (a : α) (l : list α) : 𝓝 (a :: l) = list.cons <$> 𝓝 a <*> 𝓝 l := by rw [nhds_list, list.traverse_cons _, ← nhds_list]; apply_instance lemma list.tendsto_cons {a : α} {l : list α} : tendsto (λp:α×list α, list.cons p.1 p.2) (𝓝 a ×ᶠ 𝓝 l) (𝓝 (a :: l)) := by rw [nhds_cons, tendsto, map_prod]; exact le_refl _ lemma filter.tendsto.cons {α : Type*} {f : α → β} {g : α → list β} {a : _root_.filter α} {b : β} {l : list β} (hf : tendsto f a (𝓝 b)) (hg : tendsto g a (𝓝 l)) : tendsto (λa, list.cons (f a) (g a)) a (𝓝 (b :: l)) := list.tendsto_cons.comp (tendsto.prod_mk hf hg) namespace list lemma tendsto_cons_iff {β : Type*} {f : list α → β} {b : _root_.filter β} {a : α} {l : list α} : tendsto f (𝓝 (a :: l)) b ↔ tendsto (λp:α×list α, f (p.1 :: p.2)) (𝓝 a ×ᶠ 𝓝 l) b := have 𝓝 (a :: l) = (𝓝 a ×ᶠ 𝓝 l).map (λp:α×list α, (p.1 :: p.2)), begin simp only [nhds_cons, filter.prod_eq, (filter.map_def _ _).symm, (filter.seq_eq_filter_seq _ _).symm], simp [-filter.seq_eq_filter_seq, -filter.map_def, (∘)] with functor_norm, end, by rw [this, filter.tendsto_map'_iff] lemma continuous_cons : continuous (λ x : α × list α, (x.1 :: x.2 : list α)) := continuous_iff_continuous_at.mpr $ λ ⟨x, y⟩, continuous_at_fst.cons continuous_at_snd lemma tendsto_nhds {β : Type*} {f : list α → β} {r : list α → _root_.filter β} (h_nil : tendsto f (pure []) (r [])) (h_cons : ∀l a, tendsto f (𝓝 l) (r l) → tendsto (λp:α×list α, f (p.1 :: p.2)) (𝓝 a ×ᶠ 𝓝 l) (r (a::l))) : ∀l, tendsto f (𝓝 l) (r l) | [] := by rwa [nhds_nil] | (a::l) := by rw [tendsto_cons_iff]; exact h_cons l a (tendsto_nhds l) lemma continuous_at_length : ∀(l : list α), continuous_at list.length l := begin simp only [continuous_at, nhds_discrete], refine tendsto_nhds _ _, { exact tendsto_pure_pure _ _ }, { assume l a ih, dsimp only [list.length], refine tendsto.comp (tendsto_pure_pure (λx, x + 1) _) _, refine tendsto.comp ih tendsto_snd } end lemma tendsto_insert_nth' {a : α} : ∀{n : ℕ} {l : list α}, tendsto (λp:α×list α, insert_nth n p.1 p.2) (𝓝 a ×ᶠ 𝓝 l) (𝓝 (insert_nth n a l)) | 0 l := tendsto_cons | (n+1) [] := suffices tendsto (λa, []) (𝓝 a) (𝓝 ([] : list α)), by simpa [nhds_nil, tendsto, map_prod, (∘), insert_nth], tendsto_const_nhds | (n+1) (a'::l) := have 𝓝 a ×ᶠ 𝓝 (a' :: l) = (𝓝 a ×ᶠ (𝓝 a' ×ᶠ 𝓝 l)).map (λp:α×α×list α, (p.1, p.2.1 :: p.2.2)), begin simp only [nhds_cons, filter.prod_eq, (filter.map_def _ _).symm, (filter.seq_eq_filter_seq _ _).symm], simp [-filter.seq_eq_filter_seq, -filter.map_def, (∘)] with functor_norm end, begin rw [this, tendsto_map'_iff], exact (tendsto_fst.comp tendsto_snd).cons ((@tendsto_insert_nth' n l).comp $ tendsto_fst.prod_mk $ tendsto_snd.comp tendsto_snd) end lemma tendsto_insert_nth {β} {n : ℕ} {a : α} {l : list α} {f : β → α} {g : β → list α} {b : _root_.filter β} (hf : tendsto f b (𝓝 a)) (hg : tendsto g b (𝓝 l)) : tendsto (λb:β, insert_nth n (f b) (g b)) b (𝓝 (insert_nth n a l)) := tendsto_insert_nth'.comp (tendsto.prod_mk hf hg) lemma continuous_insert_nth {n : ℕ} : continuous (λp:α×list α, insert_nth n p.1 p.2) := continuous_iff_continuous_at.mpr $ assume ⟨a, l⟩, by rw [continuous_at, nhds_prod_eq]; exact tendsto_insert_nth' lemma tendsto_remove_nth : ∀{n : ℕ} {l : list α}, tendsto (λl, remove_nth l n) (𝓝 l) (𝓝 (remove_nth l n)) | _ [] := by rw [nhds_nil]; exact tendsto_pure_nhds _ _ | 0 (a::l) := by rw [tendsto_cons_iff]; exact tendsto_snd | (n+1) (a::l) := begin rw [tendsto_cons_iff], dsimp [remove_nth], exact tendsto_fst.cons ((@tendsto_remove_nth n l).comp tendsto_snd) end lemma continuous_remove_nth {n : ℕ} : continuous (λl : list α, remove_nth l n) := continuous_iff_continuous_at.mpr $ assume a, tendsto_remove_nth @[to_additive] lemma tendsto_prod [monoid α] [has_continuous_mul α] {l : list α} : tendsto list.prod (𝓝 l) (𝓝 l.prod) := begin induction l with x l ih, { simp [nhds_nil, mem_of_nhds, tendsto_pure_left] {contextual := tt} }, simp_rw [tendsto_cons_iff, prod_cons], have := continuous_iff_continuous_at.mp continuous_mul (x, l.prod), rw [continuous_at, nhds_prod_eq] at this, exact this.comp (tendsto_id.prod_map ih) end @[to_additive] lemma continuous_prod [monoid α] [has_continuous_mul α] : continuous (prod : list α → α) := continuous_iff_continuous_at.mpr $ λ l, tendsto_prod end list namespace vector open list instance (n : ℕ) : topological_space (vector α n) := by unfold vector; apply_instance lemma tendsto_cons {n : ℕ} {a : α} {l : vector α n}: tendsto (λp:α×vector α n, p.1 ::ᵥ p.2) (𝓝 a ×ᶠ 𝓝 l) (𝓝 (a ::ᵥ l)) := by { simp [tendsto_subtype_rng, ←subtype.val_eq_coe, cons_val], exact tendsto_fst.cons (tendsto.comp continuous_at_subtype_coe tendsto_snd) } lemma tendsto_insert_nth {n : ℕ} {i : fin (n+1)} {a:α} : ∀{l:vector α n}, tendsto (λp:α×vector α n, insert_nth p.1 i p.2) (𝓝 a ×ᶠ 𝓝 l) (𝓝 (insert_nth a i l)) | ⟨l, hl⟩ := begin rw [insert_nth, tendsto_subtype_rng], simp [insert_nth_val], exact list.tendsto_insert_nth tendsto_fst (tendsto.comp continuous_at_subtype_coe tendsto_snd : _) end lemma continuous_insert_nth' {n : ℕ} {i : fin (n+1)} : continuous (λp:α×vector α n, insert_nth p.1 i p.2) := continuous_iff_continuous_at.mpr $ assume ⟨a, l⟩, by rw [continuous_at, nhds_prod_eq]; exact tendsto_insert_nth lemma continuous_insert_nth {n : ℕ} {i : fin (n+1)} {f : β → α} {g : β → vector α n} (hf : continuous f) (hg : continuous g) : continuous (λb, insert_nth (f b) i (g b)) := continuous_insert_nth'.comp (hf.prod_mk hg : _) lemma continuous_at_remove_nth {n : ℕ} {i : fin (n+1)} : ∀{l:vector α (n+1)}, continuous_at (remove_nth i) l | ⟨l, hl⟩ := -- ∀{l:vector α (n+1)}, tendsto (remove_nth i) (𝓝 l) (𝓝 (remove_nth i l)) --| ⟨l, hl⟩ := begin rw [continuous_at, remove_nth, tendsto_subtype_rng], simp only [← subtype.val_eq_coe, vector.remove_nth_val], exact tendsto.comp list.tendsto_remove_nth continuous_at_subtype_coe, end lemma continuous_remove_nth {n : ℕ} {i : fin (n+1)} : continuous (remove_nth i : vector α (n+1) → vector α n) := continuous_iff_continuous_at.mpr $ assume ⟨a, l⟩, continuous_at_remove_nth end vector
f5309461e81ee7de002cbeb7ae550d56c2db97a8
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/playground/moddata.lean
0badfdfd8a2a7181eec2d37bb6b0bbbb62a5a3cc
[ "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
478
lean
import init.lean.environment open Lean def saveTestFile (fn : String) : IO Unit := saveModuleData fn { imports := [`foo, `bla].toArray, constants := Array.empty, entries := Array.empty, serialized := [1, 2, 3, 4].toByteArray } def openTestFile (fn : String) : IO Unit := do m ← readModuleData fn, IO.println m.imports, IO.println m.serialized, pure () def main (xs : List String) : IO Unit := let fn := xs.head in saveTestFile fn *> openTestFile fn
2cb9d6f869c188b04b675fdae6484c6c32b6235d
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/src/data/finsupp/antidiagonal.lean
098888c0e40d73769c6b5bcdca970e0446e8977f
[ "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
4,499
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, Yury Kudryashov -/ import data.finsupp.basic import data.multiset.antidiagonal /-! # The `finsupp` counterpart of `multiset.antidiagonal`. The antidiagonal of `s : α →₀ ℕ` consists of all pairs `(t₁, t₂) : (α →₀ ℕ) × (α →₀ ℕ)` such that `t₁ + t₂ = s`. -/ noncomputable theory open_locale classical big_operators namespace finsupp open finset variables {α : Type*} /-- The `finsupp` counterpart of `multiset.antidiagonal`: the antidiagonal of `s : α →₀ ℕ` consists of all pairs `(t₁, t₂) : (α →₀ ℕ) × (α →₀ ℕ)` such that `t₁ + t₂ = s`. The finitely supported function `antidiagonal s` is equal to the multiplicities of these pairs. -/ def antidiagonal' (f : α →₀ ℕ) : ((α →₀ ℕ) × (α →₀ ℕ)) →₀ ℕ := (f.to_multiset.antidiagonal.map (prod.map multiset.to_finsupp multiset.to_finsupp)).to_finsupp /-- The antidiagonal of `s : α →₀ ℕ` is the finset of all pairs `(t₁, t₂) : (α →₀ ℕ) × (α →₀ ℕ)` such that `t₁ + t₂ = s`. -/ def antidiagonal (f : α →₀ ℕ) : finset ((α →₀ ℕ) × (α →₀ ℕ)) := f.antidiagonal'.support @[simp] lemma mem_antidiagonal {f : α →₀ ℕ} {p : (α →₀ ℕ) × (α →₀ ℕ)} : p ∈ antidiagonal f ↔ p.1 + p.2 = f := begin rcases p with ⟨p₁, p₂⟩, simp [antidiagonal, antidiagonal', ← and.assoc, ← finsupp.to_multiset.apply_eq_iff_eq] end lemma swap_mem_antidiagonal {n : α →₀ ℕ} {f : (α →₀ ℕ) × (α →₀ ℕ)} : f.swap ∈ antidiagonal n ↔ f ∈ antidiagonal n := by simp only [mem_antidiagonal, add_comm, prod.swap] lemma antidiagonal_filter_fst_eq (f g : α →₀ ℕ) [D : Π (p : (α →₀ ℕ) × (α →₀ ℕ)), decidable (p.1 = g)] : (antidiagonal f).filter (λ p, p.1 = g) = if g ≤ f then {(g, f - g)} else ∅ := begin ext ⟨a, b⟩, suffices : a = g → (a + b = f ↔ g ≤ f ∧ b = f - g), { simpa [apply_ite ((∈) (a, b)), ← and.assoc, @and.right_comm _ (a = _), and.congr_left_iff] }, unfreezingI {rintro rfl}, split, { rintro rfl, exact ⟨le_add_right le_rfl, (add_sub_cancel_left _ _).symm⟩ }, { rintro ⟨h, rfl⟩, exact add_sub_cancel_of_le h } end lemma antidiagonal_filter_snd_eq (f g : α →₀ ℕ) [D : Π (p : (α →₀ ℕ) × (α →₀ ℕ)), decidable (p.2 = g)] : (antidiagonal f).filter (λ p, p.2 = g) = if g ≤ f then {(f - g, g)} else ∅ := begin ext ⟨a, b⟩, suffices : b = g → (a + b = f ↔ g ≤ f ∧ a = f - g), { simpa [apply_ite ((∈) (a, b)), ← and.assoc, and.congr_left_iff] }, unfreezingI {rintro rfl}, split, { rintro rfl, exact ⟨le_add_left le_rfl, (add_sub_cancel_right _ _).symm⟩ }, { rintro ⟨h, rfl⟩, exact sub_add_cancel_of_le h } end @[simp] lemma antidiagonal_zero : antidiagonal (0 : α →₀ ℕ) = singleton (0,0) := by rw [antidiagonal, antidiagonal', multiset.to_finsupp_support]; refl @[to_additive] lemma prod_antidiagonal_swap {M : Type*} [comm_monoid M] (n : α →₀ ℕ) (f : (α →₀ ℕ) → (α →₀ ℕ) → M) : ∏ p in antidiagonal n, f p.1 p.2 = ∏ p in antidiagonal n, f p.2 p.1 := finset.prod_bij (λ p hp, p.swap) (λ p, swap_mem_antidiagonal.2) (λ p hp, rfl) (λ p₁ p₂ _ _ h, prod.swap_injective h) (λ p hp, ⟨p.swap, swap_mem_antidiagonal.2 hp, p.swap_swap.symm⟩) /-- The set `{m : α →₀ ℕ | m ≤ n}` as a `finset`. -/ def Iic_finset (n : α →₀ ℕ) : finset (α →₀ ℕ) := (antidiagonal n).image prod.fst @[simp] lemma mem_Iic_finset {m n : α →₀ ℕ} : m ∈ Iic_finset n ↔ m ≤ n := by simp [Iic_finset, le_iff_exists_add, eq_comm] @[simp] lemma coe_Iic_finset (n : α →₀ ℕ) : ↑(Iic_finset n) = set.Iic n := by { ext, simp } /-- Let `n : α →₀ ℕ` be a finitely supported function. The set of `m : α →₀ ℕ` that are coordinatewise less than or equal to `n`, is a finite set. -/ lemma finite_le_nat (n : α →₀ ℕ) : set.finite {m | m ≤ n} := by simpa using (Iic_finset n).finite_to_set /-- Let `n : α →₀ ℕ` be a finitely supported function. The set of `m : α →₀ ℕ` that are coordinatewise less than or equal to `n`, but not equal to `n` everywhere, is a finite set. -/ lemma finite_lt_nat (n : α →₀ ℕ) : set.finite {m | m < n} := (finite_le_nat n).subset $ λ m, le_of_lt end finsupp
fb9d1ee73de003d7138b3af5fb14a50569664706
63abd62053d479eae5abf4951554e1064a4c45b4
/test/rcases.lean
07c159e5bdc429f08ab91907a300f37a94daa137
[ "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
4,195
lean
/- Copyright (c) 2018 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon -/ import tactic.rcases universe u variables {α β γ : Type u} example (x : α × β × γ) : true := begin rcases x with ⟨a, b, c⟩, { guard_hyp a : α, guard_hyp b : β, guard_hyp c : γ, trivial } end example (x : α × β × γ) : true := begin rcases x with ⟨a, ⟨-, c⟩⟩, { guard_hyp a : α, success_if_fail { guard_hyp x_snd_fst : β }, guard_hyp c : γ, trivial } end example (x : (α × β) × γ) : true := begin rcases x with ⟨⟨a:α, b⟩, c⟩, { guard_hyp a : α, guard_hyp b : β, guard_hyp c : γ, trivial } end example : inhabited α × option β ⊕ γ → true := begin rintro (⟨⟨a⟩, _ | b⟩ | c), { guard_hyp a : α, trivial }, { guard_hyp a : α, guard_hyp b : β, trivial }, { guard_hyp c : γ, trivial } end example : cond ff ℕ ℤ → cond tt ℤ ℕ → (ℕ ⊕ unit) → true := begin rintro (x y : ℤ) (z | u), { guard_hyp x : ℤ, guard_hyp y : ℤ, guard_hyp z : ℕ, trivial }, { guard_hyp x : ℤ, guard_hyp y : ℤ, guard_hyp u : unit, trivial } end example (x y : ℕ) (h : x = y) : true := begin rcases x with _|⟨⟩|z, { guard_hyp h : nat.zero = y, trivial }, { guard_hyp h : nat.succ nat.zero = y, trivial }, { guard_hyp z : ℕ, guard_hyp h : z.succ.succ = y, trivial }, end -- from equiv.sum_empty example (s : α ⊕ empty) : true := begin rcases s with _ | ⟨⟨⟩⟩, { guard_hyp s : α, trivial } end example : true := begin obtain ⟨n : ℕ, h : n = n, -⟩ : ∃ n : ℕ, n = n ∧ true, { existsi 0, simp }, guard_hyp n : ℕ, guard_hyp h : n = n, success_if_fail {assumption}, trivial end example : true := begin obtain : ∃ n : ℕ, n = n ∧ true, { existsi 0, simp }, trivial end example : true := begin obtain (h : true) | ⟨⟨⟩⟩ : true ∨ false, { left, trivial }, guard_hyp h : true, trivial end example : true := begin obtain h | ⟨⟨⟩⟩ : true ∨ false := or.inl trivial, guard_hyp h : true, trivial end example : true := begin obtain ⟨h, h2⟩ := and.intro trivial trivial, guard_hyp h : true, guard_hyp h2 : true, trivial end example : true := begin success_if_fail {obtain ⟨h, h2⟩}, trivial end example (x y : α × β) : true := begin rcases ⟨x, y⟩ with ⟨⟨a, b⟩, c, d⟩, { guard_hyp a : α, guard_hyp b : β, guard_hyp c : α, guard_hyp d : β, trivial } end example (x y : α ⊕ β) : true := begin obtain ⟨a|b, c|d⟩ := ⟨x, y⟩, { guard_hyp a : α, guard_hyp c : α, trivial }, { guard_hyp a : α, guard_hyp d : β, trivial }, { guard_hyp b : β, guard_hyp c : α, trivial }, { guard_hyp b : β, guard_hyp d : β, trivial }, end example {i j : ℕ} : (Σ' x, i ≤ x ∧ x ≤ j) → i ≤ j := begin intro h, rcases h' : h with ⟨x,h₀,h₁⟩, guard_hyp h' : h = ⟨x,h₀,h₁⟩, apply le_trans h₀ h₁, end protected def set.foo {α β} (s : set α) (t : set β) : set (α × β) := ∅ example {α} (V : set α) (w : true → ∃ p, p ∈ (V.foo V) ∩ (V.foo V)) : true := begin obtain ⟨a, h⟩ : ∃ p, p ∈ (V.foo V) ∩ (V.foo V) := w trivial, trivial, end example (n : ℕ) : true := begin obtain one_lt_n | n_le_one : 1 < n + 1 ∨ n + 1 ≤ 1 := nat.lt_or_ge 1 (n + 1), trivial, trivial, end example (n : ℕ) : true := begin obtain one_lt_n | (n_le_one : n + 1 ≤ 1) := nat.lt_or_ge 1 (n + 1), trivial, trivial, end example (h : ∃ x : ℕ, x = x ∧ 1 = 1) : true := begin rcases h with ⟨-, _⟩, (do lc ← tactic.local_context, guard lc.empty), trivial end example (h : ∃ x : ℕ, x = x ∧ 1 = 1) : true := begin rcases h with ⟨-, _, h⟩, (do lc ← tactic.local_context, guard (lc.length = 1)), guard_hyp h : 1 = 1, trivial end example (h : true ∨ true ∨ true) : true := begin rcases h with -|-|-, iterate 3 { (do lc ← tactic.local_context, guard lc.empty), trivial }, end example : bool → false → true | ff := by rintro ⟨⟩ | tt := by rintro ⟨⟩
716fc8c134fee17afa28862b37ce1b55b9d08155
a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940
/src/Lean/Elab/Frontend.lean
c2b85a511e14aee7574078e5adbccb0811b8b114
[ "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
4,718
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Sebastian Ullrich -/ import Lean.Elab.Import import Lean.Elab.Command import Lean.Util.Profile namespace Lean.Elab.Frontend structure State where commandState : Command.State parserState : Parser.ModuleParserState cmdPos : String.Pos commands : Array Syntax := #[] structure Context where inputCtx : Parser.InputContext abbrev FrontendM := ReaderT Context $ StateRefT State IO def setCommandState (commandState : Command.State) : FrontendM Unit := modify fun s => { s with commandState := commandState } @[inline] def runCommandElabM (x : Command.CommandElabM α) : FrontendM α := do let ctx ← read let s ← get let cmdCtx : Command.Context := { cmdPos := s.cmdPos, fileName := ctx.inputCtx.fileName, fileMap := ctx.inputCtx.fileMap } match (← liftM <| EIO.toIO' <| (x cmdCtx).run s.commandState) with | Except.error e => throw <| IO.Error.userError s!"unexpected internal error: {← e.toMessageData.toString}" | Except.ok (a, sNew) => setCommandState sNew; return a def elabCommandAtFrontend (stx : Syntax) : FrontendM Unit := do runCommandElabM do let infoTreeEnabled := (← getInfoState).enabled if checkTraceOption (← getOptions) `Elab.info then enableInfoTree Command.elabCommandTopLevel stx enableInfoTree infoTreeEnabled def updateCmdPos : FrontendM Unit := do modify fun s => { s with cmdPos := s.parserState.pos } def getParserState : FrontendM Parser.ModuleParserState := do pure (← get).parserState def getCommandState : FrontendM Command.State := do pure (← get).commandState def setParserState (ps : Parser.ModuleParserState) : FrontendM Unit := modify fun s => { s with parserState := ps } def setMessages (msgs : MessageLog) : FrontendM Unit := modify fun s => { s with commandState := { s.commandState with messages := msgs } } def getInputContext : FrontendM Parser.InputContext := do pure (← read).inputCtx def processCommand : FrontendM Bool := do updateCmdPos let cmdState ← getCommandState let ictx ← getInputContext let pstate ← getParserState let scope := cmdState.scopes.head! let pmctx := { env := cmdState.env, options := scope.opts, currNamespace := scope.currNamespace, openDecls := scope.openDecls } let pos := ictx.fileMap.toPosition pstate.pos match profileit "parsing" scope.opts fun _ => Parser.parseCommand ictx pmctx pstate cmdState.messages with | (cmd, ps, messages) => modify fun s => { s with commands := s.commands.push cmd } setParserState ps setMessages messages if Parser.isEOI cmd || Parser.isExitCommand cmd then pure true -- Done else profileitM IO.Error "elaboration" scope.opts <| elabCommandAtFrontend cmd pure false partial def processCommands : FrontendM Unit := do let done ← processCommand unless done do processCommands end Frontend open Frontend def IO.processCommands (inputCtx : Parser.InputContext) (parserState : Parser.ModuleParserState) (commandState : Command.State) : IO State := do let (_, s) ← (Frontend.processCommands.run { inputCtx := inputCtx }).run { commandState := commandState, parserState := parserState, cmdPos := parserState.pos } pure s def process (input : String) (env : Environment) (opts : Options) (fileName : Option String := none) : IO (Environment × MessageLog) := do let fileName := fileName.getD "<input>" let inputCtx := Parser.mkInputContext input fileName let s ← IO.processCommands inputCtx { : Parser.ModuleParserState } (Command.mkState env {} opts) pure (s.commandState.env, s.commandState.messages) builtin_initialize registerOption `printMessageEndPos { defValue := false, descr := "print end position of each message in addition to start position" } registerTraceClass `Elab.info def getPrintMessageEndPos (opts : Options) : Bool := opts.getBool `printMessageEndPos false @[export lean_run_frontend] def runFrontend (input : String) (opts : Options) (fileName : String) (mainModuleName : Name) : IO (Environment × Bool) := do let inputCtx := Parser.mkInputContext input fileName let (header, parserState, messages) ← Parser.parseHeader inputCtx let (env, messages) ← processHeader header opts messages inputCtx let env := env.setMainModule mainModuleName let s ← IO.processCommands inputCtx parserState (Command.mkState env messages opts) for msg in s.commandState.messages.toList do IO.print (← msg.toString (includeEndPos := getPrintMessageEndPos opts)) pure (s.commandState.env, !s.commandState.messages.hasErrors) end Lean.Elab
fd87ded03421cdc6a1e04eeed484fb5d0c79615a
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/data/list/big_operators/lemmas.lean
e42d4982a4244278b45a2707ccd8dcffeddc97ec
[ "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,717
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, Floris van Doorn, Sébastien Gouëzel, Alex J. Best -/ import data.list.big_operators.basic import algebra.group.opposite import algebra.group_power.basic import algebra.group_with_zero.commute import algebra.group_with_zero.divisibility import algebra.order.with_zero import algebra.ring.basic import algebra.ring.divisibility import algebra.ring.commute import data.int.units import data.set.basic /-! # Lemmas about `list.sum` and `list.prod` requiring extra algebra imports -/ open mul_opposite list variables {ι α M N P M₀ G R : Type*} namespace commute lemma list_sum_right [non_unital_non_assoc_semiring R] (a : R) (l : list R) (h : ∀ b ∈ l, commute a b) : commute a l.sum := begin induction l with x xs ih, { exact commute.zero_right _, }, { rw list.sum_cons, exact (h _ $ mem_cons_self _ _).add_right (ih $ λ j hj, h _ $ mem_cons_of_mem _ hj) } end lemma list_sum_left [non_unital_non_assoc_semiring R] (b : R) (l : list R) (h : ∀ a ∈ l, commute a b) : commute l.sum b := (commute.list_sum_right _ _ $ λ x hx, (h _ hx).symm).symm end commute namespace list @[to_additive card_nsmul_le_sum] lemma pow_card_le_prod [monoid M] [preorder M] [covariant_class M M (function.swap (*)) (≤)] [covariant_class M M (*) (≤)] (l : list M) (n : M) (h : ∀ (x ∈ l), n ≤ x) : n ^ l.length ≤ l.prod := @prod_le_pow_card Mᵒᵈ _ _ _ _ l n h @[to_additive] lemma prod_eq_one_iff [canonically_ordered_monoid M] (l : list M) : l.prod = 1 ↔ ∀ x ∈ l, x = (1 : M) := ⟨all_one_of_le_one_le_of_prod_eq_one (λ _ _, one_le _), λ h, by rw [eq_repeat.2 ⟨rfl, h⟩, prod_repeat, one_pow]⟩ /-- If a product of integers is `-1`, then at least one factor must be `-1`. -/ lemma neg_one_mem_of_prod_eq_neg_one {l : list ℤ} (h : l.prod = -1) : (-1 : ℤ) ∈ l := begin obtain ⟨x, h₁, h₂⟩ := exists_mem_ne_one_of_prod_ne_one (ne_of_eq_of_ne h dec_trivial), exact or.resolve_left (int.is_unit_iff.mp (prod_is_unit_iff.mp (h.symm ▸ is_unit.neg is_unit_one : is_unit l.prod) x h₁)) h₂ ▸ h₁, end /-- If all elements in a list are bounded below by `1`, then the length of the list is bounded by the sum of the elements. -/ lemma length_le_sum_of_one_le (L : list ℕ) (h : ∀ i ∈ L, 1 ≤ i) : L.length ≤ L.sum := begin induction L with j L IH h, { simp }, rw [sum_cons, length, add_comm], exact add_le_add (h _ (set.mem_insert _ _)) (IH (λ i hi, h i (set.mem_union_right _ hi))) end lemma dvd_prod [comm_monoid M] {a} {l : list M} (ha : a ∈ l) : a ∣ l.prod := let ⟨s, t, h⟩ := mem_split ha in by { rw [h, prod_append, prod_cons, mul_left_comm], exact dvd_mul_right _ _ } lemma dvd_sum [semiring R] {a} {l : list R} (h : ∀ x ∈ l, a ∣ x) : a ∣ l.sum := begin induction l with x l ih, { exact dvd_zero _ }, { rw [list.sum_cons], exact dvd_add (h _ (mem_cons_self _ _)) (ih (λ x hx, h x (mem_cons_of_mem _ hx))) } end section alternating variables [comm_group α] @[to_additive] lemma alternating_prod_append : ∀ l₁ l₂ : list α, alternating_prod (l₁ ++ l₂) = alternating_prod l₁ * alternating_prod l₂ ^ (-1 : ℤ) ^ length l₁ | [] l₂ := by simp | (a :: l₁) l₂ := by simp_rw [cons_append, alternating_prod_cons, alternating_prod_append, length_cons, pow_succ, neg_mul, one_mul, zpow_neg, ←div_eq_mul_inv, div_div] @[to_additive] lemma alternating_prod_reverse : ∀ l : list α, alternating_prod (reverse l) = alternating_prod l ^ (-1 : ℤ) ^ (length l + 1) | [] := by simp only [alternating_prod_nil, one_zpow, reverse_nil] | (a :: l) := begin simp_rw [reverse_cons, alternating_prod_append, alternating_prod_reverse, alternating_prod_singleton, alternating_prod_cons, length_reverse, length, pow_succ, neg_mul, one_mul, zpow_neg, inv_inv], rw [mul_comm, ←div_eq_mul_inv, div_zpow], end end alternating lemma sum_map_mul_left [non_unital_non_assoc_semiring R] (L : list ι) (f : ι → R) (r : R) : (L.map (λ b, r * f b)).sum = r * (L.map f).sum := sum_map_hom L f $ add_monoid_hom.mul_left r lemma sum_map_mul_right [non_unital_non_assoc_semiring R] (L : list ι) (f : ι → R) (r : R) : (L.map (λ b, f b * r)).sum = (L.map f).sum * r := sum_map_hom L f $ add_monoid_hom.mul_right r end list namespace mul_opposite open list variables [monoid M] lemma op_list_prod : ∀ (l : list M), op (l.prod) = (l.map op).reverse.prod | [] := rfl | (x :: xs) := by rw [list.prod_cons, list.map_cons, list.reverse_cons', list.prod_concat, op_mul, op_list_prod] lemma _root_.mul_opposite.unop_list_prod (l : list Mᵐᵒᵖ) : (l.prod).unop = (l.map unop).reverse.prod := by rw [← op_inj, op_unop, mul_opposite.op_list_prod, map_reverse, map_map, reverse_reverse, op_comp_unop, map_id] end mul_opposite section monoid_hom variables [monoid M] [monoid N] /-- A morphism into the opposite monoid acts on the product by acting on the reversed elements. -/ lemma unop_map_list_prod {F : Type*} [monoid_hom_class F M Nᵐᵒᵖ] (f : F) (l : list M) : (f l.prod).unop = (l.map (mul_opposite.unop ∘ f)).reverse.prod := by rw [map_list_prod f l, mul_opposite.unop_list_prod, list.map_map] namespace monoid_hom /-- A morphism into the opposite monoid acts on the product by acting on the reversed elements. Deprecated, use `_root_.unop_map_list_prod` instead. -/ protected lemma unop_map_list_prod (f : M →* Nᵐᵒᵖ) (l : list M) : (f l.prod).unop = (l.map (mul_opposite.unop ∘ f)).reverse.prod := unop_map_list_prod f l end monoid_hom end monoid_hom
3b1cef67e760d85d4a09fd90ad0a704911cace11
63abd62053d479eae5abf4951554e1064a4c45b4
/src/algebra/char_p.lean
8320afb50877c8ab0eaeaad35e7fb65e49957693
[ "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
14,447
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Kenny Lau, Joey van Langen, Casper Putz -/ import data.fintype.basic import data.nat.choose import data.int.modeq import algebra.module.basic import algebra.iterate_hom import group_theory.order_of_element import algebra.group.type_tags /-! # Characteristic of semirings -/ universes u v /-- The generator of the kernel of the unique homomorphism ℕ → α for a semiring α -/ class char_p (α : Type u) [semiring α] (p : ℕ) : Prop := (cast_eq_zero_iff [] : ∀ x:ℕ, (x:α) = 0 ↔ p ∣ x) theorem char_p.cast_eq_zero (α : Type u) [semiring α] (p : ℕ) [char_p α p] : (p:α) = 0 := (char_p.cast_eq_zero_iff α p p).2 (dvd_refl p) @[simp] lemma char_p.cast_card_eq_zero (R : Type*) [ring R] [fintype R] : (fintype.card R : R) = 0 := begin have : fintype.card R •ℕ (1 : R) = 0 := @pow_card_eq_one (multiplicative R) _ _ (multiplicative.of_add 1), simpa only [mul_one, nsmul_eq_mul] end lemma char_p.int_cast_eq_zero_iff (R : Type u) [ring R] (p : ℕ) [char_p R p] (a : ℤ) : (a : R) = 0 ↔ (p:ℤ) ∣ a := begin rcases lt_trichotomy a 0 with h|rfl|h, { rw [← neg_eq_zero, ← int.cast_neg, ← dvd_neg], lift -a to ℕ using neg_nonneg.mpr (le_of_lt h) with b, rw [int.cast_coe_nat, char_p.cast_eq_zero_iff R p, int.coe_nat_dvd] }, { simp only [int.cast_zero, eq_self_iff_true, dvd_zero] }, { lift a to ℕ using (le_of_lt h) with b, rw [int.cast_coe_nat, char_p.cast_eq_zero_iff R p, int.coe_nat_dvd] } end lemma char_p.int_coe_eq_int_coe_iff (R : Type*) [ring R] (p : ℕ) [char_p R p] (a b : ℤ) : (a : R) = (b : R) ↔ a ≡ b [ZMOD p] := by rw [eq_comm, ←sub_eq_zero, ←int.cast_sub, char_p.int_cast_eq_zero_iff R p, int.modeq.modeq_iff_dvd] theorem char_p.eq (α : Type u) [semiring α] {p q : ℕ} (c1 : char_p α p) (c2 : char_p α q) : p = q := nat.dvd_antisymm ((char_p.cast_eq_zero_iff α p q).1 (char_p.cast_eq_zero _ _)) ((char_p.cast_eq_zero_iff α q p).1 (char_p.cast_eq_zero _ _)) instance char_p.of_char_zero (α : Type u) [semiring α] [char_zero α] : char_p α 0 := ⟨λ x, by rw [zero_dvd_iff, ← nat.cast_zero, nat.cast_inj]⟩ theorem char_p.exists (α : Type u) [semiring α] : ∃ p, char_p α p := by letI := classical.dec_eq α; exact classical.by_cases (assume H : ∀ p:ℕ, (p:α) = 0 → p = 0, ⟨0, ⟨λ x, by rw [zero_dvd_iff]; exact ⟨H x, by rintro rfl; refl⟩⟩⟩) (λ H, ⟨nat.find (not_forall.1 H), ⟨λ x, ⟨λ H1, nat.dvd_of_mod_eq_zero (by_contradiction $ λ H2, nat.find_min (not_forall.1 H) (nat.mod_lt x $ nat.pos_of_ne_zero $ not_of_not_imp $ nat.find_spec (not_forall.1 H)) (not_imp_of_and_not ⟨by rwa [← nat.mod_add_div x (nat.find (not_forall.1 H)), nat.cast_add, nat.cast_mul, of_not_not (not_not_of_not_imp $ nat.find_spec (not_forall.1 H)), zero_mul, add_zero] at H1, H2⟩)), λ H1, by rw [← nat.mul_div_cancel' H1, nat.cast_mul, of_not_not (not_not_of_not_imp $ nat.find_spec (not_forall.1 H)), zero_mul]⟩⟩⟩) theorem char_p.exists_unique (α : Type u) [semiring α] : ∃! p, char_p α p := let ⟨c, H⟩ := char_p.exists α in ⟨c, H, λ y H2, char_p.eq α H2 H⟩ /-- Noncomputable function that outputs the unique characteristic of a semiring. -/ noncomputable def ring_char (α : Type u) [semiring α] : ℕ := classical.some (char_p.exists_unique α) theorem ring_char.spec (α : Type u) [semiring α] : ∀ x:ℕ, (x:α) = 0 ↔ ring_char α ∣ x := by letI := (classical.some_spec (char_p.exists_unique α)).1; unfold ring_char; exact char_p.cast_eq_zero_iff α (ring_char α) theorem ring_char.eq (α : Type u) [semiring α] {p : ℕ} (C : char_p α p) : p = ring_char α := (classical.some_spec (char_p.exists_unique α)).2 p C theorem add_pow_char_of_commute (R : Type u) [ring R] {p : ℕ} [fact p.prime] [char_p R p] (x y : R) (h : commute x y) : (x + y)^p = x^p + y^p := begin rw [commute.add_pow h, finset.sum_range_succ, nat.sub_self, pow_zero, nat.choose_self], rw [nat.cast_one, mul_one, mul_one, add_right_inj], convert finset.sum_eq_single 0 _ _, { simp }, swap, { intro h1, contrapose! h1, rw finset.mem_range, apply nat.prime.pos, assumption }, intros b h1 h2, suffices : (p.choose b : R) = 0, { rw this, simp }, rw char_p.cast_eq_zero_iff R p, apply nat.prime.dvd_choose_self, assumption', { omega }, rwa ← finset.mem_range end theorem add_pow_char_pow_of_commute (R : Type u) [ring R] {p : ℕ} [fact p.prime] [char_p R p] {n : ℕ} (x y : R) (h : commute x y) : (x + y) ^ (p ^ n) = x ^ (p ^ n) + y ^ (p ^ n) := begin induction n, { simp, }, rw [pow_succ', pow_mul, pow_mul, pow_mul, n_ih], apply add_pow_char_of_commute, apply commute.pow_pow h, end theorem sub_pow_char_of_commute (R : Type u) [ring R] {p : ℕ} [fact p.prime] [char_p R p] (x y : R) (h : commute x y) : (x - y)^p = x^p - y^p := begin rw [eq_sub_iff_add_eq, ← add_pow_char_of_commute _ _ _ (commute.sub_left h rfl)], simp, repeat {apply_instance}, end theorem sub_pow_char_pow_of_commute (R : Type u) [ring R] {p : ℕ} [fact p.prime] [char_p R p] {n : ℕ} (x y : R) (h : commute x y) : (x - y) ^ (p ^ n) = x ^ (p ^ n) - y ^ (p ^ n) := begin induction n, { simp, }, rw [pow_succ', pow_mul, pow_mul, pow_mul, n_ih], apply sub_pow_char_of_commute, apply commute.pow_pow h, end theorem add_pow_char (α : Type u) [comm_ring α] {p : ℕ} [fact p.prime] [char_p α p] (x y : α) : (x + y)^p = x^p + y^p := add_pow_char_of_commute _ _ _ (commute.all _ _) theorem add_pow_char_pow (R : Type u) [comm_ring R] {p : ℕ} [fact p.prime] [char_p R p] {n : ℕ} (x y : R) : (x + y) ^ (p ^ n) = x ^ (p ^ n) + y ^ (p ^ n) := add_pow_char_pow_of_commute _ _ _ (commute.all _ _) theorem sub_pow_char (α : Type u) [comm_ring α] {p : ℕ} [fact p.prime] [char_p α p] (x y : α) : (x - y)^p = x^p - y^p := sub_pow_char_of_commute _ _ _ (commute.all _ _) theorem sub_pow_char_pow (R : Type u) [comm_ring R] {p : ℕ} [fact p.prime] [char_p R p] {n : ℕ} (x y : R) : (x - y) ^ (p ^ n) = x ^ (p ^ n) - y ^ (p ^ n) := sub_pow_char_pow_of_commute _ _ _ (commute.all _ _) lemma eq_iff_modeq_int (R : Type*) [ring R] (p : ℕ) [char_p R p] (a b : ℤ) : (a : R) = b ↔ a ≡ b [ZMOD p] := by rw [eq_comm, ←sub_eq_zero, ←int.cast_sub, char_p.int_cast_eq_zero_iff R p, int.modeq.modeq_iff_dvd] lemma char_p.neg_one_ne_one (R : Type*) [ring R] (p : ℕ) [char_p R p] [fact (2 < p)] : (-1 : R) ≠ (1 : R) := begin suffices : (2 : R) ≠ 0, { symmetry, rw [ne.def, ← sub_eq_zero, sub_neg_eq_add], exact this }, assume h, rw [show (2 : R) = (2 : ℕ), by norm_cast] at h, have := (char_p.cast_eq_zero_iff R p 2).mp h, have := nat.le_of_dvd dec_trivial this, rw fact at *, linarith, end lemma ring_hom.char_p_iff_char_p {K L : Type*} [field K] [field L] (f : K →+* L) (p : ℕ) : char_p K p ↔ char_p L p := begin split; { introI _c, constructor, intro n, rw [← @char_p.cast_eq_zero_iff _ _ p _c n, ← f.injective.eq_iff, f.map_nat_cast, f.map_zero] } end section frobenius variables (R : Type u) [comm_ring R] {S : Type v} [comm_ring S] (f : R →* S) (g : R →+* S) (p : ℕ) [fact p.prime] [char_p R p] [char_p S p] (x y : R) /-- The frobenius map that sends x to x^p -/ def frobenius : R →+* R := { to_fun := λ x, x^p, map_one' := one_pow p, map_mul' := λ x y, mul_pow x y p, map_zero' := zero_pow (lt_trans zero_lt_one ‹nat.prime p›.one_lt), map_add' := add_pow_char R } variable {R} theorem frobenius_def : frobenius R p x = x ^ p := rfl theorem iterate_frobenius (n : ℕ) : (frobenius R p)^[n] x = x ^ p ^ n := begin induction n, {simp}, rw [function.iterate_succ', pow_succ', pow_mul, function.comp_apply, frobenius_def, n_ih] end theorem frobenius_mul : frobenius R p (x * y) = frobenius R p x * frobenius R p y := (frobenius R p).map_mul x y theorem frobenius_one : frobenius R p 1 = 1 := one_pow _ variable {R} theorem monoid_hom.map_frobenius : f (frobenius R p x) = frobenius S p (f x) := f.map_pow x p theorem ring_hom.map_frobenius : g (frobenius R p x) = frobenius S p (g x) := g.map_pow x p theorem monoid_hom.map_iterate_frobenius (n : ℕ) : f (frobenius R p^[n] x) = (frobenius S p^[n] (f x)) := function.semiconj.iterate_right (f.map_frobenius p) n x theorem ring_hom.map_iterate_frobenius (n : ℕ) : g (frobenius R p^[n] x) = (frobenius S p^[n] (g x)) := g.to_monoid_hom.map_iterate_frobenius p x n theorem monoid_hom.iterate_map_frobenius (f : R →* R) (p : ℕ) [fact p.prime] [char_p R p] (n : ℕ) : f^[n] (frobenius R p x) = frobenius R p (f^[n] x) := f.iterate_map_pow _ _ _ theorem ring_hom.iterate_map_frobenius (f : R →+* R) (p : ℕ) [fact p.prime] [char_p R p] (n : ℕ) : f^[n] (frobenius R p x) = frobenius R p (f^[n] x) := f.iterate_map_pow _ _ _ variable (R) theorem frobenius_zero : frobenius R p 0 = 0 := (frobenius R p).map_zero theorem frobenius_add : frobenius R p (x + y) = frobenius R p x + frobenius R p y := (frobenius R p).map_add x y theorem frobenius_neg : frobenius R p (-x) = -frobenius R p x := (frobenius R p).map_neg x theorem frobenius_sub : frobenius R p (x - y) = frobenius R p x - frobenius R p y := (frobenius R p).map_sub x y theorem frobenius_nat_cast (n : ℕ) : frobenius R p n = n := (frobenius R p).map_nat_cast n end frobenius theorem frobenius_inj (α : Type u) [integral_domain α] (p : ℕ) [fact p.prime] [char_p α p] : function.injective (frobenius α p) := λ x h H, by { rw ← sub_eq_zero at H ⊢, rw ← frobenius_sub at H, exact pow_eq_zero H } namespace char_p section variables (α : Type u) [ring α] lemma char_p_to_char_zero [char_p α 0] : char_zero α := char_zero_of_inj_zero $ λ n h0, eq_zero_of_zero_dvd ((cast_eq_zero_iff α 0 n).mp h0) lemma cast_eq_mod (p : ℕ) [char_p α p] (k : ℕ) : (k : α) = (k % p : ℕ) := calc (k : α) = ↑(k % p + p * (k / p)) : by rw [nat.mod_add_div] ... = ↑(k % p) : by simp[cast_eq_zero] theorem char_ne_zero_of_fintype (p : ℕ) [hc : char_p α p] [fintype α] : p ≠ 0 := assume h : p = 0, have char_zero α := @char_p_to_char_zero α _ (h ▸ hc), absurd (@nat.cast_injective α _ _ this) (not_injective_infinite_fintype coe) end section integral_domain open nat variables (α : Type u) [integral_domain α] theorem char_ne_one (p : ℕ) [hc : char_p α p] : p ≠ 1 := assume hp : p = 1, have ( 1 : α) = 0, by simpa using (cast_eq_zero_iff α p 1).mpr (hp ▸ dvd_refl p), absurd this one_ne_zero theorem char_is_prime_of_two_le (p : ℕ) [hc : char_p α p] (hp : 2 ≤ p) : nat.prime p := suffices ∀d ∣ p, d = 1 ∨ d = p, from ⟨hp, this⟩, assume (d : ℕ) (hdvd : ∃ e, p = d * e), let ⟨e, hmul⟩ := hdvd in have (p : α) = 0, from (cast_eq_zero_iff α p p).mpr (dvd_refl p), have (d : α) * e = 0, from (@cast_mul α _ d e) ▸ (hmul ▸ this), or.elim (eq_zero_or_eq_zero_of_mul_eq_zero this) (assume hd : (d : α) = 0, have p ∣ d, from (cast_eq_zero_iff α p d).mp hd, show d = 1 ∨ d = p, from or.inr (dvd_antisymm ⟨e, hmul⟩ this)) (assume he : (e : α) = 0, have p ∣ e, from (cast_eq_zero_iff α p e).mp he, have e ∣ p, from dvd_of_mul_left_eq d (eq.symm hmul), have e = p, from dvd_antisymm ‹e ∣ p› ‹p ∣ e›, have h₀ : p > 0, from gt_of_ge_of_gt hp (nat.zero_lt_succ 1), have d * p = 1 * p, by rw ‹e = p› at hmul; rw [one_mul]; exact eq.symm hmul, show d = 1 ∨ d = p, from or.inl (eq_of_mul_eq_mul_right h₀ this)) theorem char_is_prime_or_zero (p : ℕ) [hc : char_p α p] : nat.prime p ∨ p = 0 := match p, hc with | 0, _ := or.inr rfl | 1, hc := absurd (eq.refl (1 : ℕ)) (@char_ne_one α _ (1 : ℕ) hc) | (m+2), hc := or.inl (@char_is_prime_of_two_le α _ (m+2) hc (nat.le_add_left 2 m)) end lemma char_is_prime_of_pos (p : ℕ) [h : fact (0 < p)] [char_p α p] : fact p.prime := (char_p.char_is_prime_or_zero α _).resolve_right (nat.pos_iff_ne_zero.1 h) theorem char_is_prime [fintype α] (p : ℕ) [char_p α p] : p.prime := or.resolve_right (char_is_prime_or_zero α p) (char_ne_zero_of_fintype α p) end integral_domain section char_one variables {R : Type*} @[priority 100] -- see Note [lower instance priority] instance [semiring R] [char_p R 1] : subsingleton R := subsingleton.intro $ suffices ∀ (r : R), r = 0, from assume a b, show a = b, by rw [this a, this b], assume r, calc r = 1 * r : by rw one_mul ... = (1 : ℕ) * r : by rw nat.cast_one ... = 0 * r : by rw char_p.cast_eq_zero ... = 0 : by rw zero_mul lemma false_of_nontrivial_of_char_one [semiring R] [nontrivial R] [char_p R 1] : false := false_of_nontrivial_of_subsingleton R lemma ring_char_ne_one [semiring R] [nontrivial R] : ring_char R ≠ 1 := by { intros h, apply @zero_ne_one R, symmetry, rw [←nat.cast_one, ring_char.spec, h], } lemma nontrivial_of_char_ne_one {v : ℕ} (hv : v ≠ 1) {R : Type*} [semiring R] [hr : char_p R v] : nontrivial R := ⟨⟨(1 : ℕ), 0, λ h, hv $ by rwa [char_p.cast_eq_zero_iff _ v, nat.dvd_one] at h; assumption ⟩⟩ end char_one end char_p section variables (n : ℕ) (R : Type*) [comm_ring R] [fintype R] lemma char_p_of_ne_zero (hn : fintype.card R = n) (hR : ∀ i < n, (i : R) = 0 → i = 0) : char_p R n := { cast_eq_zero_iff := begin have H : (n : R) = 0, by { rw [← hn, char_p.cast_card_eq_zero] }, intro k, split, { intro h, rw [← nat.mod_add_div k n, nat.cast_add, nat.cast_mul, H, zero_mul, add_zero] at h, rw nat.dvd_iff_mod_eq_zero, apply hR _ (nat.mod_lt _ _) h, rw [← hn, gt, fintype.card_pos_iff], exact ⟨0⟩, }, { rintro ⟨k, rfl⟩, rw [nat.cast_mul, H, zero_mul] } end } lemma char_p_of_prime_pow_injective (p : ℕ) [hp : fact p.prime] (n : ℕ) (hn : fintype.card R = p ^ n) (hR : ∀ i ≤ n, (p ^ i : R) = 0 → i = n) : char_p R (p ^ n) := begin obtain ⟨c, hc⟩ := char_p.exists R, resetI, have hcpn : c ∣ p ^ n, { rw [← char_p.cast_eq_zero_iff R c, ← hn, char_p.cast_card_eq_zero], }, obtain ⟨i, hi, hc⟩ : ∃ i ≤ n, c = p ^ i, by rwa nat.dvd_prime_pow hp at hcpn, obtain rfl : i = n, { apply hR i hi, rw [← nat.cast_pow, ← hc, char_p.cast_eq_zero] }, rwa ← hc end end
d56babb07efd3fc2d58cff5215bd4960e209ca1a
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/topology/uniform_space/absolute_value.lean
6232d4f0bc56116b88d872d6be1a73dae500ddbf
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
2,108
lean
/- Copyright (c) 2019 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.real.cau_seq import Mathlib.topology.uniform_space.basic import Mathlib.PostPort universes u_1 u_2 namespace Mathlib /-! # Uniform structure induced by an absolute value We build a uniform space structure on a commutative ring `R` equipped with an absolute value into a linear ordered field `𝕜`. Of course in the case `R` is `ℚ`, `ℝ` or `ℂ` and `𝕜 = ℝ`, we get the same thing as the metric space construction, and the general construction follows exactly the same path. ## Implementation details Note that we import `data.real.cau_seq` because this is where absolute values are defined, but the current file does not depend on real numbers. TODO: extract absolute values from that `data.real` folder. ## References * [N. Bourbaki, *Topologie générale*][bourbaki1966] ## Tags absolute value, uniform spaces -/ namespace is_absolute_value /-- The uniformity coming from an absolute value. -/ def uniform_space_core {𝕜 : Type u_1} [linear_ordered_field 𝕜] {R : Type u_2} [comm_ring R] (abv : R → 𝕜) [is_absolute_value abv] : uniform_space.core R := uniform_space.core.mk (infi fun (ε : 𝕜) => infi fun (H : ε > 0) => filter.principal (set_of fun (p : R × R) => abv (prod.snd p - prod.fst p) < ε)) sorry sorry sorry /-- The uniform structure coming from an absolute value. -/ def uniform_space {𝕜 : Type u_1} [linear_ordered_field 𝕜] {R : Type u_2} [comm_ring R] (abv : R → 𝕜) [is_absolute_value abv] : uniform_space R := uniform_space.of_core (uniform_space_core abv) theorem mem_uniformity {𝕜 : Type u_1} [linear_ordered_field 𝕜] {R : Type u_2} [comm_ring R] (abv : R → 𝕜) [is_absolute_value abv] {s : set (R × R)} : s ∈ uniform_space.core.uniformity (uniform_space_core abv) ↔ ∃ (ε : 𝕜), ∃ (H : ε > 0), ∀ {a b : R}, abv (b - a) < ε → (a, b) ∈ s := sorry
d75f7b92752ff230b3fc9ad01818d29d99e9f789
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/src/category_theory/Fintype.lean
437e171a4c9bcd9a7b08b4077b435ba5b8ed4292
[ "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
4,304
lean
/- Copyright (c) 2020 Adam Topaz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta, Adam Topaz -/ import data.fintype.basic import data.fin.basic import category_theory.concrete_category.bundled import category_theory.concrete_category import category_theory.full_subcategory import category_theory.skeletal /-! # The category of finite types. We define the category of finite types, denoted `Fintype` as (bundled) types with a `fintype` instance. We also define `Fintype.skeleton`, the standard skeleton of `Fintype` whose objects are `fin n` for `n : ℕ`. We prove that the obvious inclusion functor `Fintype.skeleton ⥤ Fintype` is an equivalence of categories in `Fintype.skeleton.equivalence`. We prove that `Fintype.skeleton` is a skeleton of `Fintype` in `Fintype.is_skeleton`. -/ open_locale classical open category_theory /-- The category of finite types. -/ @[derive has_coe_to_sort] def Fintype := bundled fintype namespace Fintype /-- Construct a bundled `Fintype` from the underlying type and typeclass. -/ def of (X : Type*) [fintype X] : Fintype := bundled.of X instance : inhabited Fintype := ⟨⟨pempty⟩⟩ instance {X : Fintype} : fintype X := X.2 instance : category Fintype := induced_category.category bundled.α /-- The fully faithful embedding of `Fintype` into the category of types. -/ @[derive [full, faithful], simps] def incl : Fintype ⥤ Type* := induced_functor _ instance : concrete_category Fintype := ⟨incl⟩ @[simp] lemma id_apply (X : Fintype) (x : X) : (𝟙 X : X → X) x = x := rfl @[simp] lemma comp_apply {X Y Z : Fintype} (f : X ⟶ Y) (g : Y ⟶ Z) (x : X) : (f ≫ g) x = g (f x) := rfl universe u /-- The "standard" skeleton for `Fintype`. This is the full subcategory of `Fintype` spanned by objects of the form `ulift (fin n)` for `n : ℕ`. We parameterize the objects of `Fintype.skeleton` directly as `ulift ℕ`, as the type `ulift (fin m) ≃ ulift (fin n)` is nonempty if and only if `n = m`. Specifying universes, `skeleton : Type u` is a small skeletal category equivalent to `Fintype.{u}`. -/ def skeleton : Type u := ulift ℕ namespace skeleton /-- Given any natural number `n`, this creates the associated object of `Fintype.skeleton`. -/ def mk : ℕ → skeleton := ulift.up instance : inhabited skeleton := ⟨mk 0⟩ /-- Given any object of `Fintype.skeleton`, this returns the associated natural number. -/ def len : skeleton → ℕ := ulift.down @[ext] lemma ext (X Y : skeleton) : X.len = Y.len → X = Y := ulift.ext _ _ instance : small_category skeleton.{u} := { hom := λ X Y, ulift.{u} (fin X.len) → ulift.{u} (fin Y.len), id := λ _, id, comp := λ _ _ _ f g, g ∘ f } lemma is_skeletal : skeletal skeleton.{u} := λ X Y ⟨h⟩, ext _ _ $ fin.equiv_iff_eq.mp $ nonempty.intro $ { to_fun := λ x, (h.hom ⟨x⟩).down, inv_fun := λ x, (h.inv ⟨x⟩).down, left_inv := begin intro a, change ulift.down _ = _, rw ulift.up_down, change ((h.hom ≫ h.inv) _).down = _, simpa, end, right_inv := begin intro a, change ulift.down _ = _, rw ulift.up_down, change ((h.inv ≫ h.hom) _).down = _, simpa, end } /-- The canonical fully faithful embedding of `Fintype.skeleton` into `Fintype`. -/ def incl : skeleton.{u} ⥤ Fintype.{u} := { obj := λ X, Fintype.of (ulift (fin X.len)), map := λ _ _ f, f } instance : full incl := { preimage := λ _ _ f, f } instance : faithful incl := {} instance : ess_surj incl := ess_surj.mk $ λ X, let F := fintype.equiv_fin X in ⟨mk (fintype.card X), nonempty.intro { hom := F.symm ∘ ulift.down, inv := ulift.up ∘ F }⟩ noncomputable instance : is_equivalence incl := equivalence.of_fully_faithfully_ess_surj _ /-- The equivalence between `Fintype.skeleton` and `Fintype`. -/ noncomputable def equivalence : skeleton ≌ Fintype := incl.as_equivalence @[simp] lemma incl_mk_nat_card (n : ℕ) : fintype.card (incl.obj (mk n)) = n := begin convert finset.card_fin n, apply fintype.of_equiv_card, end end skeleton /-- `Fintype.skeleton` is a skeleton of `Fintype`. -/ noncomputable def is_skeleton : is_skeleton_of Fintype skeleton skeleton.incl := { skel := skeleton.is_skeletal, eqv := by apply_instance } end Fintype
32e6c6e180de6240bb10cf549aa79af98e023d4a
2a70b774d16dbdf5a533432ee0ebab6838df0948
/_target/deps/mathlib/src/logic/embedding.lean
ceb1442030beb51296089a7837bf9e8855c3fb1d
[ "Apache-2.0" ]
permissive
hjvromen/lewis
40b035973df7c77ebf927afab7878c76d05ff758
105b675f73630f028ad5d890897a51b3c1146fb0
refs/heads/master
1,677,944,636,343
1,676,555,301,000
1,676,555,301,000
327,553,599
0
0
null
null
null
null
UTF-8
Lean
false
false
9,772
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro -/ import data.equiv.basic import data.sigma.basic import algebra.group.defs /-! # Injective functions -/ universes u v w x namespace function /-- `α ↪ β` is a bundled injective function. -/ @[nolint has_inhabited_instance] -- depending on cardinalities, an injective function may not exist structure embedding (α : Sort*) (β : Sort*) := (to_fun : α → β) (inj' : injective to_fun) infixr ` ↪ `:25 := embedding instance {α : Sort u} {β : Sort v} : has_coe_to_fun (α ↪ β) := ⟨_, embedding.to_fun⟩ initialize_simps_projections embedding (to_fun → apply) end function /-- Convert an `α ≃ β` to `α ↪ β`. -/ @[simps] protected def equiv.to_embedding {α : Sort u} {β : Sort v} (f : α ≃ β) : α ↪ β := ⟨f, f.injective⟩ namespace function namespace embedding @[ext] lemma ext {α β} {f g : embedding α β} (h : ∀ x, f x = g x) : f = g := by cases f; cases g; simpa using funext h lemma ext_iff {α β} {f g : embedding α β} : (∀ x, f x = g x) ↔ f = g := ⟨ext, λ h _, by rw h⟩ @[simp] theorem to_fun_eq_coe {α β} (f : α ↪ β) : to_fun f = f := rfl @[simp] theorem coe_fn_mk {α β} (f : α → β) (i) : (@mk _ _ f i : α → β) = f := rfl theorem injective {α β} (f : α ↪ β) : injective f := f.inj' @[refl, simps {simp_rhs := tt}] protected def refl (α : Sort*) : α ↪ α := ⟨id, injective_id⟩ @[trans, simps {simp_rhs := tt}] protected def trans {α β γ} (f : α ↪ β) (g : β ↪ γ) : α ↪ γ := ⟨g ∘ f, g.injective.comp f.injective⟩ @[simp] lemma equiv_to_embedding_trans_symm_to_embedding {α β : Sort*} (e : α ≃ β) : e.to_embedding.trans e.symm.to_embedding = embedding.refl _ := by { ext, simp, } @[simp] lemma equiv_symm_to_embedding_trans_to_embedding {α β : Sort*} (e : α ≃ β) : e.symm.to_embedding.trans e.to_embedding = embedding.refl _ := by { ext, simp, } protected def congr {α : Sort u} {β : Sort v} {γ : Sort w} {δ : Sort x} (e₁ : α ≃ β) (e₂ : γ ≃ δ) (f : α ↪ γ) : (β ↪ δ) := (equiv.to_embedding e₁.symm).trans (f.trans e₂.to_embedding) /-- A right inverse `surj_inv` of a surjective function as an `embedding`. -/ protected noncomputable def of_surjective {α β} (f : β → α) (hf : surjective f) : α ↪ β := ⟨surj_inv hf, injective_surj_inv _⟩ /-- Convert a surjective `embedding` to an `equiv` -/ protected noncomputable def equiv_of_surjective {α β} (f : α ↪ β) (hf : surjective f) : α ≃ β := equiv.of_bijective f ⟨f.injective, hf⟩ protected def of_not_nonempty {α β} (hα : ¬ nonempty α) : α ↪ β := ⟨λa, (hα ⟨a⟩).elim, assume a, (hα ⟨a⟩).elim⟩ /-- Change the value of an embedding `f` at one point. If the prescribed image is already occupied by some `f a'`, then swap the values at these two points. -/ def set_value {α β} (f : α ↪ β) (a : α) (b : β) [∀ a', decidable (a' = a)] [∀ a', decidable (f a' = b)] : α ↪ β := ⟨λ a', if a' = a then b else if f a' = b then f a else f a', begin intros x y h, dsimp at h, split_ifs at h; try { substI b }; try { simp only [f.injective.eq_iff] at * }; cc end⟩ theorem set_value_eq {α β} (f : α ↪ β) (a : α) (b : β) [∀ a', decidable (a' = a)] [∀ a', decidable (f a' = b)] : set_value f a b a = b := by simp [set_value] /-- Embedding into `option` -/ protected def some {α} : α ↪ option α := ⟨some, option.some_injective α⟩ /-- Embedding of a `subtype`. -/ def subtype {α} (p : α → Prop) : subtype p ↪ α := ⟨coe, λ _ _, subtype.ext_val⟩ @[simp] lemma coe_subtype {α} (p : α → Prop) : ⇑(subtype p) = coe := rfl /-- Choosing an element `b : β` gives an embedding of `punit` into `β`. -/ def punit {β : Sort*} (b : β) : punit ↪ β := ⟨λ _, b, by { rintros ⟨⟩ ⟨⟩ _, refl, }⟩ /-- Fixing an element `b : β` gives an embedding `α ↪ α × β`. -/ def sectl (α : Sort*) {β : Sort*} (b : β) : α ↪ α × β := ⟨λ a, (a, b), λ a a' h, congr_arg prod.fst h⟩ /-- Fixing an element `a : α` gives an embedding `β ↪ α × β`. -/ def sectr {α : Sort*} (a : α) (β : Sort*): β ↪ α × β := ⟨λ b, (a, b), λ b b' h, congr_arg prod.snd h⟩ /-- Restrict the codomain of an embedding. -/ def cod_restrict {α β} (p : set β) (f : α ↪ β) (H : ∀ a, f a ∈ p) : α ↪ p := ⟨λ a, ⟨f a, H a⟩, λ a b h, f.injective (@congr_arg _ _ _ _ subtype.val h)⟩ @[simp] theorem cod_restrict_apply {α β} (p) (f : α ↪ β) (H a) : cod_restrict p f H a = ⟨f a, H a⟩ := rfl /-- If `e₁` and `e₂` are embeddings, then so is `prod.map e₁ e₂ : (a, b) ↦ (e₁ a, e₂ b)`. -/ def prod_map {α β γ δ : Type*} (e₁ : α ↪ β) (e₂ : γ ↪ δ) : α × γ ↪ β × δ := ⟨prod.map e₁ e₂, e₁.injective.prod_map e₂.injective⟩ @[simp] lemma coe_prod_map {α β γ δ : Type*} (e₁ : α ↪ β) (e₂ : γ ↪ δ) : ⇑(e₁.prod_map e₂) = prod.map e₁ e₂ := rfl section sum open sum /-- If `e₁` and `e₂` are embeddings, then so is `sum.map e₁ e₂`. -/ def sum_map {α β γ δ : Type*} (e₁ : α ↪ β) (e₂ : γ ↪ δ) : α ⊕ γ ↪ β ⊕ δ := ⟨sum.map e₁ e₂, assume s₁ s₂ h, match s₁, s₂, h with | inl a₁, inl a₂, h := congr_arg inl $ e₁.injective $ inl.inj h | inr b₁, inr b₂, h := congr_arg inr $ e₂.injective $ inr.inj h end⟩ @[simp] theorem coe_sum_map {α β γ δ} (e₁ : α ↪ β) (e₂ : γ ↪ δ) : ⇑(sum_map e₁ e₂) = sum.map e₁ e₂ := rfl /-- The embedding of `α` into the sum `α ⊕ β`. -/ def inl {α β : Type*} : α ↪ α ⊕ β := ⟨sum.inl, λ a b, sum.inl.inj⟩ /-- The embedding of `β` into the sum `α ⊕ β`. -/ def inr {α β : Type*} : β ↪ α ⊕ β := ⟨sum.inr, λ a b, sum.inr.inj⟩ end sum section sigma variables {α α' : Type*} {β : α → Type*} {β' : α' → Type*} /-- `sigma.mk` as an `function.embedding`. -/ @[simps apply] def sigma_mk (a : α) : β a ↪ Σ x, β x := ⟨sigma.mk a, sigma_mk_injective⟩ /-- If `f : α ↪ α'` is an embedding and `g : Π a, β α ↪ β' (f α)` is a family of embeddings, then `sigma.map f g` is an embedding. -/ @[simps apply] def sigma_map (f : α ↪ α') (g : Π a, β a ↪ β' (f a)) : (Σ a, β a) ↪ Σ a', β' a' := ⟨sigma.map f (λ a, g a), f.injective.sigma_map (λ a, (g a).injective)⟩ end sigma def Pi_congr_right {α : Sort*} {β γ : α → Sort*} (e : ∀ a, β a ↪ γ a) : (Π a, β a) ↪ (Π a, γ a) := ⟨λf a, e a (f a), λ f₁ f₂ h, funext $ λ a, (e a).injective (congr_fun h a)⟩ def arrow_congr_left {α : Sort u} {β : Sort v} {γ : Sort w} (e : α ↪ β) : (γ → α) ↪ (γ → β) := Pi_congr_right (λ _, e) noncomputable def arrow_congr_right {α : Sort u} {β : Sort v} {γ : Sort w} [inhabited γ] (e : α ↪ β) : (α → γ) ↪ (β → γ) := by haveI := classical.prop_decidable; exact let f' : (α → γ) → (β → γ) := λf b, if h : ∃c, e c = b then f (classical.some h) else default γ in ⟨f', assume f₁ f₂ h, funext $ assume c, have ∃c', e c' = e c, from ⟨c, rfl⟩, have eq' : f' f₁ (e c) = f' f₂ (e c), from congr_fun h _, have eq_b : classical.some this = c, from e.injective $ classical.some_spec this, by simp [f', this, if_pos, eq_b] at eq'; assumption⟩ protected def subtype_map {α β} {p : α → Prop} {q : β → Prop} (f : α ↪ β) (h : ∀{{x}}, p x → q (f x)) : {x : α // p x} ↪ {y : β // q y} := ⟨subtype.map f h, subtype.map_injective h f.2⟩ open set /-- `set.image` as an embedding `set α ↪ set β`. -/ @[simps apply] protected def image {α β} (f : α ↪ β) : set α ↪ set β := ⟨image f, f.2.image_injective⟩ lemma swap_apply {α β : Type*} [decidable_eq α] [decidable_eq β] (f : α ↪ β) (x y z : α) : equiv.swap (f x) (f y) (f z) = f (equiv.swap x y z) := f.injective.swap_apply x y z lemma swap_comp {α β : Type*} [decidable_eq α] [decidable_eq β] (f : α ↪ β) (x y : α) : equiv.swap (f x) (f y) ∘ f = f ∘ equiv.swap x y := f.injective.swap_comp x y end embedding end function namespace equiv @[simp] lemma refl_to_embedding {α : Type*} : (equiv.refl α).to_embedding = function.embedding.refl α := rfl @[simp] lemma trans_to_embedding {α β γ : Type*} (e : α ≃ β) (f : β ≃ γ) : (e.trans f).to_embedding = e.to_embedding.trans f.to_embedding := rfl end equiv namespace set /-- The injection map is an embedding between subsets. -/ @[simps apply] def embedding_of_subset {α} (s t : set α) (h : s ⊆ t) : s ↪ t := ⟨λ x, ⟨x.1, h x.2⟩, λ ⟨x, hx⟩ ⟨y, hy⟩ h, by { congr, injection h }⟩ end set -- TODO: these two definitions probably belong somewhere else, so that we can remove the -- `algebra.group.defs` import. /-- The embedding of a left cancellative semigroup into itself by left multiplication by a fixed element. -/ @[to_additive "The embedding of a left cancellative additive semigroup into itself by left translation by a fixed element.", simps] def mul_left_embedding {G : Type u} [left_cancel_semigroup G] (g : G) : G ↪ G := { to_fun := λ h, g * h, inj' := mul_right_injective g } /-- The embedding of a right cancellative semigroup into itself by right multiplication by a fixed element. -/ @[to_additive "The embedding of a right cancellative additive semigroup into itself by right translation by a fixed element.", simps] def mul_right_embedding {G : Type u} [right_cancel_semigroup G] (g : G) : G ↪ G := { to_fun := λ h, h * g, inj' := mul_left_injective g }
f1383d9dc8f9c16ec33a8925110d67ae7c3581fd
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/unification_hints.lean
968d0a057dc7be42f265a6eb3a08f0aac370f852
[ "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
990
lean
open list nat namespace toy constants (A : Type.{1}) (f h : A → A) (x y z : A) attribute [irreducible] noncomputable definition g (x y : A) : A := f z @[unify] noncomputable definition toy_hint (x y : A) : unification_hint := { pattern := g x y ≟ f z, constraints := [] } open tactic set_option trace.type_context.unification_hint true definition ex1 (a : A) (H : g x y = a) : f z = a := by do {trace_state, assumption} #print ex1 end toy namespace add constants (n : ℕ) @[unify] definition add_zero_hint (m n : ℕ) [has_add ℕ] [has_one ℕ] [has_zero ℕ] : unification_hint := { pattern := m + 1 ≟ succ n, constraints := [m ≟ n] } attribute [irreducible] has_add.add open tactic definition ex2 (H : n + 1 = 0) : succ n = 0 := by assumption #print ex2 end add /- Basic unification hints -/ @[unify] def add_succ_defeq_succ_add_hint (x y z : nat) : unification_hint := { pattern := x + nat.succ y ≟ nat.succ z, constraints := [z ≟ x + y] }
058a84ce774746e03351db4e35be0c6a11ff8da8
37a833c924892ee3ecb911484775a6d6ebb8984d
/src/category_theory/small.lean
c9536b78c4bf6d3299c7a5b60de70e7d571a0e01
[]
no_license
silky/lean-category-theory
28126e80564a1f99e9c322d86b3f7d750da0afa1
0f029a2364975f56ac727d31d867a18c95c22fd8
refs/heads/master
1,589,555,811,646
1,554,673,665,000
1,554,673,665,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
740
lean
/- Categories which are small relative to a cardinal κ. κ-filtered categories. Normally we care about these concepts for categories which are used to index (co)limits, so we work with small_categories. -/ import category_theory.category import category_theory.functor import category_theory.limits.cones import set_theory.cardinal universe u namespace category_theory variables (κ : cardinal.{u}) def is_kappa_small (I : Type u) [small_category.{u} I] : Prop := cardinal.mk (Σ (a b : I), a ⟶ b) < κ structure kappa_filtered (C : Type u) [small_category.{u} C] : Prop := (has_cocones : ∀ (I : Type u) [small_category.{u} I] (hI : is_kappa_small κ I) (F : I ⥤ C), nonempty (limits.cocone F)) end category_theory
2867e422cf47c6cd2d94237e229a9fa0a3eeed8e
367134ba5a65885e863bdc4507601606690974c1
/src/linear_algebra/char_poly/coeff.lean
0a43c46648814562dc1ce130a065914383e05dd4
[ "Apache-2.0" ]
permissive
kodyvajjha/mathlib
9bead00e90f68269a313f45f5561766cfd8d5cad
b98af5dd79e13a38d84438b850a2e8858ec21284
refs/heads/master
1,624,350,366,310
1,615,563,062,000
1,615,563,062,000
162,666,963
0
0
Apache-2.0
1,545,367,651,000
1,545,367,651,000
null
UTF-8
Lean
false
false
8,992
lean
/- Copyright (c) 2020 Aaron Anderson, Jalex Stark. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Aaron Anderson, Jalex Stark. -/ import data.matrix.char_p import linear_algebra.char_poly.basic import linear_algebra.matrix import ring_theory.polynomial.basic import algebra.polynomial.big_operators import group_theory.perm.cycles import field_theory.finite.basic /-! # Characteristic polynomials We give methods for computing coefficients of the characteristic polynomial. ## Main definitions - `char_poly_degree_eq_dim` proves that the degree of the characteristic polynomial over a nonzero ring is the dimension of the matrix - `det_eq_sign_char_poly_coeff` proves that the determinant is the constant term of the characteristic polynomial, up to sign. - `trace_eq_neg_char_poly_coeff` proves that the trace is the negative of the (d-1)th coefficient of the characteristic polynomial, where d is the dimension of the matrix. For a nonzero ring, this is the second-highest coefficient. -/ noncomputable theory universes u v w z open polynomial matrix open_locale big_operators variables {R : Type u} [comm_ring R] variables {n G : Type v} [decidable_eq n] [fintype n] variables {α β : Type v} [decidable_eq α] open finset open polynomial variable {M : matrix n n R} lemma char_matrix_apply_nat_degree [nontrivial R] (i j : n) : (char_matrix M i j).nat_degree = ite (i = j) 1 0 := by { by_cases i = j; simp [h, ← degree_eq_iff_nat_degree_eq_of_pos (nat.succ_pos 0)], } lemma char_matrix_apply_nat_degree_le (i j : n) : (char_matrix M i j).nat_degree ≤ ite (i = j) 1 0 := by split_ifs; simp [h, nat_degree_X_sub_C_le] variable (M) lemma char_poly_sub_diagonal_degree_lt : (char_poly M - ∏ (i : n), (X - C (M i i))).degree < ↑(fintype.card n - 1) := begin rw [char_poly, det, ← insert_erase (mem_univ (equiv.refl n)), sum_insert (not_mem_erase (equiv.refl n) univ), add_comm], simp only [char_matrix_apply_eq, one_mul, equiv.perm.sign_refl, id.def, int.cast_one, units.coe_one, add_sub_cancel, equiv.coe_refl], rw ← mem_degree_lt, apply submodule.sum_mem (degree_lt R (fintype.card n - 1)), intros c hc, rw [← C_eq_int_cast, C_mul'], apply submodule.smul_mem (degree_lt R (fintype.card n - 1)) ↑↑(equiv.perm.sign c), rw mem_degree_lt, apply lt_of_le_of_lt degree_le_nat_degree _, rw with_bot.coe_lt_coe, apply lt_of_le_of_lt _ (equiv.perm.fixed_point_card_lt_of_ne_one (ne_of_mem_erase hc)), apply le_trans (polynomial.nat_degree_prod_le univ (λ i : n, (char_matrix M (c i) i))) _, rw card_eq_sum_ones, rw sum_filter, apply sum_le_sum, intros, apply char_matrix_apply_nat_degree_le, end lemma char_poly_coeff_eq_prod_coeff_of_le {k : ℕ} (h : fintype.card n - 1 ≤ k) : (char_poly M).coeff k = (∏ i : n, (X - C (M i i))).coeff k := begin apply eq_of_sub_eq_zero, rw ← coeff_sub, apply polynomial.coeff_eq_zero_of_degree_lt, apply lt_of_lt_of_le (char_poly_sub_diagonal_degree_lt M) _, rw with_bot.coe_le_coe, apply h, end lemma det_of_card_zero (h : fintype.card n = 0) (M : matrix n n R) : M.det = 1 := by { rw fintype.card_eq_zero_iff at h, suffices : M = 1, { simp [this] }, ext, tauto } theorem char_poly_degree_eq_dim [nontrivial R] (M : matrix n n R) : (char_poly M).degree = fintype.card n := begin by_cases fintype.card n = 0, { rw h, unfold char_poly, rw det_of_card_zero, {simp}, {assumption} }, rw ← sub_add_cancel (char_poly M) (∏ (i : n), (X - C (M i i))), have h1 : (∏ (i : n), (X - C (M i i))).degree = fintype.card n, { rw degree_eq_iff_nat_degree_eq_of_pos, swap, apply nat.pos_of_ne_zero h, rw nat_degree_prod', simp_rw nat_degree_X_sub_C, unfold fintype.card, simp, simp_rw (monic_X_sub_C _).leading_coeff, simp, }, rw degree_add_eq_right_of_degree_lt, exact h1, rw h1, apply lt_trans (char_poly_sub_diagonal_degree_lt M), rw with_bot.coe_lt_coe, rw ← nat.pred_eq_sub_one, apply nat.pred_lt, apply h, end theorem char_poly_nat_degree_eq_dim [nontrivial R] (M : matrix n n R) : (char_poly M).nat_degree = fintype.card n := nat_degree_eq_of_degree_eq_some (char_poly_degree_eq_dim M) lemma char_poly_monic (M : matrix n n R) : monic (char_poly M) := begin nontriviality, by_cases fintype.card n = 0, {rw [char_poly, det_of_card_zero h], apply monic_one}, have mon : (∏ (i : n), (X - C (M i i))).monic, { apply monic_prod_of_monic univ (λ i : n, (X - C (M i i))), simp [monic_X_sub_C], }, rw ← sub_add_cancel (∏ (i : n), (X - C (M i i))) (char_poly M) at mon, rw monic at *, rw leading_coeff_add_of_degree_lt at mon, rw ← mon, rw char_poly_degree_eq_dim, rw ← neg_sub, rw degree_neg, apply lt_trans (char_poly_sub_diagonal_degree_lt M), rw with_bot.coe_lt_coe, rw ← nat.pred_eq_sub_one, apply nat.pred_lt, apply h, end theorem trace_eq_neg_char_poly_coeff [nonempty n] (M : matrix n n R) : (matrix.trace n R R) M = -(char_poly M).coeff (fintype.card n - 1) := begin nontriviality, rw char_poly_coeff_eq_prod_coeff_of_le, swap, refl, rw [fintype.card, prod_X_sub_C_coeff_card_pred univ (λ i : n, M i i)], simp, rw [← fintype.card, fintype.card_pos_iff], apply_instance, end -- I feel like this should use polynomial.alg_hom_eval₂_algebra_map lemma mat_poly_equiv_eval (M : matrix n n (polynomial R)) (r : R) (i j : n) : (mat_poly_equiv M).eval ((scalar n) r) i j = (M i j).eval r := begin unfold polynomial.eval, unfold eval₂, transitivity finsupp.sum (mat_poly_equiv M) (λ (e : ℕ) (a : matrix n n R), (a * (scalar n) r ^ e) i j), { unfold finsupp.sum, rw sum_apply, rw sum_apply, dsimp, refl, }, { simp_rw ← (scalar n).map_pow, simp_rw ← (matrix.scalar.commute _ _).eq, simp only [coe_scalar, matrix.one_mul, ring_hom.id_apply, smul_apply, mul_eq_mul, algebra.smul_mul_assoc], have h : ∀ x : ℕ, (λ (e : ℕ) (a : R), r ^ e * a) x 0 = 0 := by simp, symmetry, rw ← finsupp.sum_map_range_index h, swap, refl, refine congr (congr rfl _) (by {ext, rw mul_comm}), ext, rw finsupp.map_range_apply, simpa [coeff] using (mat_poly_equiv_coeff_apply M a i j).symm } end lemma eval_det (M : matrix n n (polynomial R)) (r : R) : polynomial.eval r M.det = (polynomial.eval (matrix.scalar n r) (mat_poly_equiv M)).det := begin rw [polynomial.eval, ← coe_eval₂_ring_hom, ring_hom.map_det], apply congr_arg det, ext, symmetry, convert mat_poly_equiv_eval _ _ _ _, end theorem det_eq_sign_char_poly_coeff (M : matrix n n R) : M.det = (-1)^(fintype.card n) * (char_poly M).coeff 0:= begin rw [coeff_zero_eq_eval_zero, char_poly, eval_det, mat_poly_equiv_char_matrix, ← det_smul], simp end variables {p : ℕ} [fact p.prime] @[simp] lemma finite_field.char_poly_pow_card {K : Type*} [field K] [fintype K] (M : matrix n n K) : char_poly (M ^ (fintype.card K)) = char_poly M := begin by_cases hn : nonempty n, { letI := hn, cases char_p.exists K with p hp, letI := hp, rcases finite_field.card K p with ⟨⟨k, kpos⟩, ⟨hp, hk⟩⟩, letI : fact p.prime := hp, dsimp at hk, rw hk at *, apply (frobenius_inj (polynomial K) p).iterate k, repeat { rw iterate_frobenius, rw ← hk }, rw ← finite_field.expand_card, unfold char_poly, rw [alg_hom.map_det, ← is_monoid_hom.map_pow], apply congr_arg det, apply mat_poly_equiv.injective, swap, { apply_instance }, rw [← mat_poly_equiv.coe_alg_hom, alg_hom.map_pow, mat_poly_equiv.coe_alg_hom, mat_poly_equiv_char_matrix, hk, sub_pow_char_pow_of_commute, ← C_pow], swap, { apply polynomial.commute_X }, -- the following is a nasty case bash that should be abstracted as a lemma -- (and maybe it can be proven more... algebraically?) ext, rw [coeff_sub, coeff_C], by_cases hij : i = j; simp [char_matrix, hij, coeff_X_pow]; simp only [coeff_C]; split_ifs; simp *, }, { congr, apply @subsingleton.elim _ (subsingleton_of_empty_left hn) _ _, }, end @[simp] lemma zmod.char_poly_pow_card (M : matrix n n (zmod p)) : char_poly (M ^ p) = char_poly M := by { have h := finite_field.char_poly_pow_card M, rwa zmod.card at h, } lemma finite_field.trace_pow_card {K : Type*} [field K] [fintype K] [nonempty n] (M : matrix n n K) : trace n K K (M ^ (fintype.card K)) = (trace n K K M) ^ (fintype.card K) := by rw [trace_eq_neg_char_poly_coeff, trace_eq_neg_char_poly_coeff, finite_field.char_poly_pow_card, finite_field.pow_card] lemma zmod.trace_pow_card {p:ℕ} [fact p.prime] [nonempty n] (M : matrix n n (zmod p)) : trace n (zmod p) (zmod p) (M ^ p) = (trace n (zmod p) (zmod p) M)^p := by { have h := finite_field.trace_pow_card M, rwa zmod.card at h, } namespace matrix theorem is_integral : is_integral R M := ⟨char_poly M, ⟨char_poly_monic M, aeval_self_char_poly M⟩⟩ theorem min_poly_dvd_char_poly {K : Type*} [field K] (M : matrix n n K) : (minpoly K M) ∣ char_poly M := minpoly.dvd _ _ (aeval_self_char_poly M) end matrix
22bfe62c0fb1470037087df78bd51374fedd0d67
36938939954e91f23dec66a02728db08a7acfcf9
/lean4/deps/galois_stdlib/src/Galois/Init/Io.lean
b9b8b8ada53c58a19092e930bc79cfffe9483a25
[]
no_license
pnwamk/reopt-vcg
f8b56dd0279392a5e1c6aee721be8138e6b558d3
c9f9f185fbefc25c36c4b506bbc85fd1a03c3b6d
refs/heads/master
1,631,145,017,772
1,593,549,019,000
1,593,549,143,000
254,191,418
0
0
null
1,586,377,077,000
1,586,377,077,000
null
UTF-8
Lean
false
false
1,445
lean
/- Low-level API doing file IO -/ namespace Galois /- c.f. Prim.Fs.handle in library/init/io.lean -/ inductive Fs.Mode | read | write | readWrite | append inductive Fs.Whence | set | cur | seek_end | hole | data constant Fs.handle : Type := Unit namespace IO namespace Prim open Fs @[extern 4 "galois_io_prim_handle_mk"] constant handle.mk (s : @& String) (m : Mode) (bin : Bool := false) : IO handle := arbitrary _ -- @[extern 2 cpp "galois_io_prim_handle_do_is_eof"] -- constant handle.isEof (h : @& handle) : IO Bool := arbitrary _ -- @[extern 2 cpp "galois_io_prim_handle_do_flush"] -- constant handle.flush (h : @& handle) : IO Unit := arbitrary _ @[extern 2 "galois_io_prim_handle_do_close"] constant handle.close (h : @& handle) : IO Unit := arbitrary _ @[extern 3 "galois_io_prim_handle_do_read"] constant handle.read (h : @& handle) (len : Nat) : IO ByteArray := arbitrary _ @[extern 3 "galois_io_prim_handle_do_write"] constant handle.write (h : @& handle) (data : @& ByteArray) : IO Unit := arbitrary _ @[extern 4 "galois_io_prim_handle_do_lseek"] constant handle.lseek (h : @& handle) (off : Int) (whence : Whence): IO Nat := arbitrary _ @[extern 2 "galois_io_prim_system"] constant system (command : @& String) : IO Unit := arbitrary _ end Prim section variables {m : Type → Type} [Monad m] [MonadIO m] @[inline] def system (command : String) : m Unit := IO.Prim.liftIO (IO.Prim.system command) end end IO end Galois
429c5c127f79b56181c78a339b80b29c1c168b4a
77c5b91fae1b966ddd1db969ba37b6f0e4901e88
/src/testing/slim_check/gen.lean
21d5c68e8d04289ffa6f9c3785a6993821308615
[ "Apache-2.0" ]
permissive
dexmagic/mathlib
ff48eefc56e2412429b31d4fddd41a976eb287ce
7a5d15a955a92a90e1d398b2281916b9c41270b2
refs/heads/master
1,693,481,322,046
1,633,360,193,000
1,633,360,193,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
5,715
lean
/- Copyright (c) 2020 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon -/ import control.uliftable import system.random import system.random.basic /-! # `gen` Monad This monad is used to formulate randomized computations with a parameter to specify the desired size of the result. This is a port of the Haskell QuickCheck library. ## Main definitions * `gen` monad ## Local notation * `i .. j` : `Icc i j`, the set of values between `i` and `j` inclusively; ## Tags random testing ## References * https://hackage.haskell.org/package/QuickCheck -/ universes u v namespace slim_check /-- Monad to generate random examples to test properties with. It has a `nat` parameter so that the caller can decide on the size of the examples. -/ @[reducible, derive [monad, is_lawful_monad]] def gen (α : Type u) := reader_t (ulift ℕ) rand α variable (α : Type u) local infix ` .. `:41 := set.Icc /-- Execute a `gen` inside the `io` monad using `i` as the example size and with a fresh random number generator. -/ def io.run_gen {α} (x : gen α) (i : ℕ) : io α := io.run_rand (x.run ⟨i⟩) namespace gen section rand /-- Lift `random.random` to the `gen` monad. -/ def choose_any [random α] : gen α := ⟨ λ _, rand.random α ⟩ variables {α} [preorder α] /-- Lift `random.random_r` to the `gen` monad. -/ def choose [bounded_random α] (x y : α) (p : x ≤ y) : gen (x .. y) := ⟨ λ _, rand.random_r x y p ⟩ end rand open nat (hiding choose) /-- Generate a `nat` example between `x` and `y`. -/ def choose_nat (x y : ℕ) (p : x ≤ y) : gen (x .. y) := choose x y p /-- Generate a `nat` example between `x` and `y`. -/ def choose_nat' (x y : ℕ) (p : x < y) : gen (set.Ico x y) := have ∀ i, x < i → i ≤ y → i.pred < y, from λ i h₀ h₁, show i.pred.succ ≤ y, by rwa succ_pred_eq_of_pos; apply lt_of_le_of_lt (nat.zero_le _) h₀, subtype.map pred (λ i (h : x+1 ≤ i ∧ i ≤ y), ⟨le_pred_of_lt h.1, this _ h.1 h.2⟩) <$> choose (x+1) y p open nat instance : uliftable gen.{u} gen.{v} := reader_t.uliftable' (equiv.ulift.trans equiv.ulift.symm) instance : has_orelse gen.{u} := ⟨ λ α x y, do b ← uliftable.up $ choose_any bool, if b.down then x else y ⟩ variable {α} /-- Get access to the size parameter of the `gen` monad. For reasons of universe polymorphism, it is specified in continuation passing style. -/ def sized (cmd : ℕ → gen α) : gen α := ⟨ λ ⟨sz⟩, reader_t.run (cmd sz) ⟨sz⟩ ⟩ /-- Apply a function to the size parameter. -/ def resize (f : ℕ → ℕ) (cmd : gen α) : gen α := ⟨ λ ⟨sz⟩, reader_t.run cmd ⟨f sz⟩ ⟩ /-- Create `n` examples using `cmd`. -/ def vector_of : ∀ (n : ℕ) (cmd : gen α), gen (vector α n) | 0 _ := return vector.nil | (succ n) cmd := vector.cons <$> cmd <*> vector_of n cmd /-- Create a list of examples using `cmd`. The size is controlled by the size parameter of `gen`. -/ def list_of (cmd : gen α) : gen (list α) := sized $ λ sz, do do ⟨ n ⟩ ← uliftable.up $ choose_nat 0 (sz + 1) dec_trivial, v ← vector_of n.val cmd, return v.to_list open ulift /-- Given a list of example generators, choose one to create an example. -/ def one_of (xs : list (gen α)) (pos : 0 < xs.length) : gen α := do ⟨⟨n, h, h'⟩⟩ ← uliftable.up $ choose_nat' 0 xs.length pos, list.nth_le xs n h' /-- Given a list of example generators, choose one to create an example. -/ def elements (xs : list α) (pos : 0 < xs.length) : gen α := do ⟨⟨n,h₀,h₁⟩⟩ ← uliftable.up $ choose_nat' 0 xs.length pos, pure $ list.nth_le xs n h₁ /-- `freq_aux xs i _` takes a weighted list of generator and a number meant to select one of the generators. If we consider `freq_aux [(1, gena), (3, genb), (5, genc)] 4 _`, we choose a generator by splitting the interval 1-9 into 1-1, 2-4, 5-9 so that the width of each interval corresponds to one of the number in the list of generators. Then, we check which interval 4 falls into: it selects `genb`. -/ def freq_aux : Π (xs : list (ℕ+ × gen α)) i, i < (xs.map (subtype.val ∘ prod.fst)).sum → gen α | [] i h := false.elim (nat.not_lt_zero _ h) | ((i, x) :: xs) j h := if h' : j < i then x else freq_aux xs (j - i) (by { rw nat.sub_lt_right_iff_lt_add (le_of_not_gt h'), simpa [list.sum_cons, add_comm] using h }) /-- `freq [(1, gena), (3, genb), (5, genc)] _` will choose one of `gena`, `genb`, `genc` with probabilities proportional to the number accompanying them. In this example, the sum of those numbers is 9, `gena` will be chosen with probability ~1/9, `genb` with ~3/9 (i.e. 1/3) and `genc` with probability 5/9. -/ def freq (xs : list (ℕ+ × gen α)) (pos : 0 < xs.length) : gen α := let s := (xs.map (subtype.val ∘ prod.fst)).sum in have ha : 1 ≤ s, from (le_trans pos $ list.length_map (subtype.val ∘ prod.fst) xs ▸ (list.length_le_sum_of_one_le _ (λ i, by { simp, intros, assumption }))), have 0 ≤ s - 1, from nat.le_sub_right_of_add_le ha, uliftable.adapt_up gen.{0} gen.{u} (choose_nat 0 (s-1) this) $ λ i, freq_aux xs i.1 (by rcases i with ⟨i,h₀,h₁⟩; rwa nat.le_sub_right_iff_add_le at h₁; exact ha) /-- Generate a random permutation of a given list. -/ def permutation_of {α : Type u} : Π xs : list α, gen (subtype $ list.perm xs) | [] := pure ⟨[], list.perm.nil ⟩ | (x :: xs) := do ⟨xs',h⟩ ← permutation_of xs, ⟨⟨n,_,h'⟩⟩ ← uliftable.up $ choose_nat 0 xs'.length dec_trivial, pure ⟨list.insert_nth n x xs', list.perm.trans (list.perm.cons _ h) (list.perm_insert_nth _ _ h').symm ⟩ end gen end slim_check
e86c7c1a198eb24a0980d2c3f6b1bc41cb6af433
367134ba5a65885e863bdc4507601606690974c1
/src/linear_algebra/basis.lean
cbea01ccea537b5cc6707f574fd6823390a223db
[ "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
21,639
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, Alexander Bentkamp -/ import linear_algebra.linear_independent import linear_algebra.projection import linear_algebra.linear_pmap import data.fintype.card /-! # Bases This file defines bases in a module or vector space. It is inspired by Isabelle/HOL's linear algebra, and hence indirectly by HOL Light. ## Main definitions All definitions are given for families of vectors, i.e. `v : ι → M` where `M` is the module or vector space and `ι : Type*` is an arbitrary indexing type. * `is_basis R v` states that the vector family `v` is a basis, i.e. it is linearly independent and spans the entire space. * `is_basis.repr hv x` is the basis version of `linear_independent.repr hv x`. It returns the linear combination representing `x : M` on a basis `v` of `M` (using classical choice). The argument `hv` must be a proof that `is_basis R v`. `is_basis.repr hv` is given as a linear map as well. * `is_basis.constr hv f` constructs a linear map `M₁ →ₗ[R] M₂` given the values `f : ι → M₂` at the basis `v : ι → M₁`, given `hv : is_basis R v`. ## Main statements * `is_basis.ext` states that two linear maps are equal if they coincide on a basis. * `exists_is_basis` states that every vector space has a basis. ## Implementation notes We use families instead of sets because it allows us to say that two identical vectors are linearly dependent. For bases, this is useful as well because we can easily derive ordered bases by using an ordered index type `ι`. ## Tags basis, bases -/ noncomputable theory universe u open function set submodule open_locale classical big_operators variables {ι : Type*} {ι' : Type*} {R : Type*} {K : Type*} variables {M : Type*} {M' M'' : Type*} {V : Type u} {V' : Type*} section module open linear_map variables {v : ι → M} variables [ring R] [add_comm_group M] [add_comm_group M'] [add_comm_group M''] variables [module R M] [module R M'] [module R M''] variables {a b : R} {x y : M} variables (R) (v) /-- A family of vectors is a basis if it is linearly independent and all vectors are in the span. -/ def is_basis := linear_independent R v ∧ span R (range v) = ⊤ variables {R} {v} section is_basis variables {s t : set M} (hv : is_basis R v) lemma is_basis.mem_span (hv : is_basis R v) : ∀ x, x ∈ span R (range v) := eq_top_iff'.1 hv.2 lemma is_basis.comp (hv : is_basis R v) (f : ι' → ι) (hf : bijective f) : is_basis R (v ∘ f) := begin split, { apply hv.1.comp f hf.1 }, { rw[set.range_comp, range_iff_surjective.2 hf.2, image_univ, hv.2] } end lemma is_basis.injective [nontrivial R] (hv : is_basis R v) : injective v := λ x y h, linear_independent.injective hv.1 h lemma is_basis.range (hv : is_basis R v) : is_basis R (λ x, x : range v → M) := ⟨hv.1.to_subtype_range, by { convert hv.2, ext i, exact ⟨λ ⟨p, hp⟩, hp ▸ p.2, λ hi, ⟨⟨i, hi⟩, rfl⟩⟩ }⟩ /-- Given a basis, any vector can be written as a linear combination of the basis vectors. They are given by this linear map. This is one direction of `module_equiv_finsupp`. -/ def is_basis.repr : M →ₗ (ι →₀ R) := (hv.1.repr).comp (linear_map.id.cod_restrict _ hv.mem_span) lemma is_basis.total_repr (x) : finsupp.total ι M R v (hv.repr x) = x := hv.1.total_repr ⟨x, _⟩ lemma is_basis.total_comp_repr : (finsupp.total ι M R v).comp hv.repr = linear_map.id := linear_map.ext hv.total_repr lemma is_basis.ext {f g : M →ₗ[R] M'} (hv : is_basis R v) (h : ∀i, f (v i) = g (v i)) : f = g := linear_map.ext_on_range hv.2 h lemma is_basis.repr_ker : hv.repr.ker = ⊥ := linear_map.ker_eq_bot.2 $ left_inverse.injective hv.total_repr lemma is_basis.repr_range : hv.repr.range = finsupp.supported R R univ := by rw [is_basis.repr, linear_map.range, submodule.map_comp, linear_map.map_cod_restrict, submodule.map_id, comap_top, map_top, hv.1.repr_range, finsupp.supported_univ] lemma is_basis.repr_total (x : ι →₀ R) (hx : x ∈ finsupp.supported R R (univ : set ι)) : hv.repr (finsupp.total ι M R v x) = x := begin rw [← hv.repr_range, linear_map.mem_range] at hx, cases hx with w hw, rw [← hw, hv.total_repr], end lemma is_basis.repr_eq_single {i} : hv.repr (v i) = finsupp.single i 1 := by apply hv.1.repr_eq_single; simp @[simp] lemma is_basis.repr_self_apply (i j : ι) : hv.repr (v i) j = if i = j then 1 else 0 := by rw [hv.repr_eq_single, finsupp.single_apply] lemma is_basis.repr_eq_iff {f : M →ₗ[R] (ι →₀ R)} : hv.repr = f ↔ ∀ i, f (v i) = finsupp.single i 1 := begin split, { rintros rfl i, exact hv.repr_eq_single }, intro h, refine hv.ext (λ _, _), rw [h, hv.repr_eq_single] end lemma is_basis.repr_apply_eq {f : M → ι → R} (hadd : ∀ x y, f (x + y) = f x + f y) (hsmul : ∀ (c : R) (x : M), f (c • x) = c • f x) (f_eq : ∀ i, f (v i) = finsupp.single i 1) (x : M) (i : ι) : hv.repr x i = f x i := begin let f_i : M →ₗ[R] R := { to_fun := λ x, f x i, map_add' := λ _ _, by rw [hadd, pi.add_apply], map_smul' := λ _ _, by rw [hsmul, pi.smul_apply] }, show (finsupp.lapply i).comp hv.repr x = f_i x, congr' 1, refine hv.ext (λ j, _), show hv.repr (v j) i = f (v j) i, rw [hv.repr_eq_single, f_eq] end lemma is_basis.range_repr_self (i : ι) : hv.range.repr (v i) = finsupp.single ⟨v i, mem_range_self i⟩ 1 := hv.1.to_subtype_range.repr_eq_single _ _ rfl @[simp] lemma is_basis.range_repr (i : ι) : hv.range.repr x ⟨v i, mem_range_self i⟩ = hv.repr x i := begin by_cases H : (0 : R) = 1, { exact eq_of_zero_eq_one H _ _ }, refine (hv.repr_apply_eq _ _ _ x i).symm, { intros x y, ext j, rw [linear_map.map_add, finsupp.add_apply], refl }, { intros c x, ext j, rw [linear_map.map_smul, finsupp.smul_apply], refl }, { intro i, ext j, haveI : nontrivial R := ⟨⟨0, 1, H⟩⟩, simp [hv.range_repr_self, finsupp.single_apply, hv.injective.eq_iff] } end /-- Construct a linear map given the value at the basis. -/ def is_basis.constr (f : ι → M') : M →ₗ[R] M' := (finsupp.total M' M' R id).comp $ (finsupp.lmap_domain R R f).comp hv.repr theorem is_basis.constr_apply (f : ι → M') (x : M) : (hv.constr f : M → M') x = (hv.repr x).sum (λb a, a • f b) := by dsimp [is_basis.constr] ; rw [finsupp.total_apply, finsupp.sum_map_domain_index]; simp [add_smul] @[simp] lemma constr_basis {f : ι → M'} {i : ι} (hv : is_basis R v) : (hv.constr f : M → M') (v i) = f i := by simp [is_basis.constr_apply, hv.repr_eq_single, finsupp.sum_single_index] lemma constr_eq {g : ι → M'} {f : M →ₗ[R] M'} (hv : is_basis R v) (h : ∀i, g i = f (v i)) : hv.constr g = f := hv.ext $ λ i, (constr_basis hv).trans (h i) lemma constr_self (f : M →ₗ[R] M') : hv.constr (λ i, f (v i)) = f := constr_eq hv $ λ x, rfl lemma constr_zero (hv : is_basis R v) : hv.constr (λi, (0 : M')) = 0 := constr_eq hv $ λ x, rfl lemma constr_add {g f : ι → M'} (hv : is_basis R v) : hv.constr (λi, f i + g i) = hv.constr f + hv.constr g := constr_eq hv $ λ b, by simp lemma constr_neg {f : ι → M'} (hv : is_basis R v) : hv.constr (λi, - f i) = - hv.constr f := constr_eq hv $ λ b, by simp lemma constr_sub {g f : ι → M'} (hs : is_basis R v) : hv.constr (λi, f i - g i) = hs.constr f - hs.constr g := by simp [sub_eq_add_neg, constr_add, constr_neg] -- this only works on functions if `R` is a commutative ring lemma constr_smul {ι R M} [comm_ring R] [add_comm_group M] [module R M] {v : ι → R} {f : ι → M} {a : R} (hv : is_basis R v) : hv.constr (λb, a • f b) = a • hv.constr f := constr_eq hv $ by simp [constr_basis hv] {contextual := tt} lemma constr_range [nonempty ι] (hv : is_basis R v) {f : ι → M'} : (hv.constr f).range = span R (range f) := by rw [is_basis.constr, linear_map.range_comp, linear_map.range_comp, is_basis.repr_range, finsupp.lmap_domain_supported, ←set.image_univ, ←finsupp.span_eq_map_total, image_id] /-- Canonical equivalence between a module and the linear combinations of basis vectors. -/ def module_equiv_finsupp (hv : is_basis R v) : M ≃ₗ[R] ι →₀ R := (hv.1.total_equiv.trans (linear_equiv.of_top _ hv.2)).symm @[simp] theorem module_equiv_finsupp_apply_basis (hv : is_basis R v) (i : ι) : module_equiv_finsupp hv (v i) = finsupp.single i 1 := (linear_equiv.symm_apply_eq _).2 $ by simp [linear_independent.total_equiv] /-- Isomorphism between the two modules, given two modules `M` and `M'` with respective bases `v` and `v'` and a bijection between the indexing sets of the two bases. -/ def linear_equiv_of_is_basis {v : ι → M} {v' : ι' → M'} (hv : is_basis R v) (hv' : is_basis R v') (e : ι ≃ ι') : M ≃ₗ[R] M' := { inv_fun := hv'.constr (v ∘ e.symm), left_inv := have (hv'.constr (v ∘ e.symm)).comp (hv.constr (v' ∘ e)) = linear_map.id, from hv.ext $ by simp, λ x, congr_arg (λ h : M →ₗ[R] M, h x) this, right_inv := have (hv.constr (v' ∘ e)).comp (hv'.constr (v ∘ e.symm)) = linear_map.id, from hv'.ext $ by simp, λ y, congr_arg (λ h : M' →ₗ[R] M', h y) this, ..hv.constr (v' ∘ e) } /-- Isomorphism between the two modules, given two modules `M` and `M'` with respective bases `v` and `v'` and a bijection between the two bases. -/ def linear_equiv_of_is_basis' {v : ι → M} {v' : ι' → M'} (f : M → M') (g : M' → M) (hv : is_basis R v) (hv' : is_basis R v') (hf : ∀i, f (v i) ∈ range v') (hg : ∀i, g (v' i) ∈ range v) (hgf : ∀i, g (f (v i)) = v i) (hfg : ∀i, f (g (v' i)) = v' i) : M ≃ₗ M' := { inv_fun := hv'.constr (g ∘ v'), left_inv := have (hv'.constr (g ∘ v')).comp (hv.constr (f ∘ v)) = linear_map.id, from hv.ext $ λ i, exists.elim (hf i) (λ i' hi', by simp [constr_basis, hi'.symm]; rw [hi', hgf]), λ x, congr_arg (λ h:M →ₗ[R] M, h x) this, right_inv := have (hv.constr (f ∘ v)).comp (hv'.constr (g ∘ v')) = linear_map.id, from hv'.ext $ λ i', exists.elim (hg i') (λ i hi, by simp [constr_basis, hi.symm]; rw [hi, hfg]), λ y, congr_arg (λ h:M' →ₗ[R] M', h y) this, ..hv.constr (f ∘ v) } @[simp] lemma linear_equiv_of_is_basis_comp {ι'' : Type*} {v : ι → M} {v' : ι' → M'} {v'' : ι'' → M''} (hv : is_basis R v) (hv' : is_basis R v') (hv'' : is_basis R v'') (e : ι ≃ ι') (f : ι' ≃ ι'' ) : (linear_equiv_of_is_basis hv hv' e).trans (linear_equiv_of_is_basis hv' hv'' f) = linear_equiv_of_is_basis hv hv'' (e.trans f) := begin apply linear_equiv.injective_to_linear_map, apply hv.ext, intros i, simp [linear_equiv_of_is_basis] end @[simp] lemma linear_equiv_of_is_basis_refl : linear_equiv_of_is_basis hv hv (equiv.refl ι) = linear_equiv.refl R M := begin apply linear_equiv.injective_to_linear_map, apply hv.ext, intros i, simp [linear_equiv_of_is_basis] end lemma linear_equiv_of_is_basis_trans_symm (e : ι ≃ ι') {v' : ι' → M'} (hv' : is_basis R v') : (linear_equiv_of_is_basis hv hv' e).trans (linear_equiv_of_is_basis hv' hv e.symm) = linear_equiv.refl R M := by simp lemma linear_equiv_of_is_basis_symm_trans (e : ι ≃ ι') {v' : ι' → M'} (hv' : is_basis R v') : (linear_equiv_of_is_basis hv' hv e.symm).trans (linear_equiv_of_is_basis hv hv' e) = linear_equiv.refl R M' := by simp lemma is_basis_inl_union_inr {v : ι → M} {v' : ι' → M'} (hv : is_basis R v) (hv' : is_basis R v') : is_basis R (sum.elim (inl R M M' ∘ v) (inr R M M' ∘ v')) := begin split, apply linear_independent_inl_union_inr' hv.1 hv'.1, rw [sum.elim_range, span_union, set.range_comp, span_image (inl R M M'), hv.2, map_top, set.range_comp, span_image (inr R M M'), hv'.2, map_top], exact linear_map.sup_range_inl_inr end @[simp] lemma is_basis.repr_eq_zero {x : M} : hv.repr x = 0 ↔ x = 0 := ⟨λ h, (hv.total_repr x).symm.trans (h.symm ▸ (finsupp.total _ _ _ _).map_zero), λ h, h.symm ▸ hv.repr.map_zero⟩ lemma is_basis.ext_elem {x y : M} (h : ∀ i, hv.repr x i = hv.repr y i) : x = y := by { rw [← hv.total_repr x, ← hv.total_repr y], congr' 1, ext i, exact h i } section include hv -- Can't be an instance because the basis can't be inferred. lemma is_basis.no_zero_smul_divisors [no_zero_divisors R] : no_zero_smul_divisors R M := ⟨λ c x hcx, or_iff_not_imp_right.mpr (λ hx, begin rw [← hv.total_repr x, ← linear_map.map_smul] at hcx, have := linear_independent_iff.mp hv.1 (c • hv.repr x) hcx, rw smul_eq_zero at this, exact this.resolve_right (λ hr, hx (hv.repr_eq_zero.mp hr)) end)⟩ lemma is_basis.smul_eq_zero [no_zero_divisors R] {c : R} {x : M} : c • x = 0 ↔ c = 0 ∨ x = 0 := @smul_eq_zero _ _ _ _ _ hv.no_zero_smul_divisors _ _ end end is_basis lemma is_basis_singleton_one (R : Type*) [unique ι] [ring R] : is_basis R (λ (_ : ι), (1 : R)) := begin split, { refine linear_independent_iff.2 (λ l hl, _), rw [finsupp.total_unique, smul_eq_mul, mul_one] at hl, exact finsupp.unique_ext hl }, { refine top_unique (λ _ _, _), simp only [mem_span_singleton, range_const, mul_one, exists_eq, smul_eq_mul] } end protected lemma linear_equiv.is_basis (hs : is_basis R v) (f : M ≃ₗ[R] M') : is_basis R (f ∘ v) := begin split, { simpa only using hs.1.map' (f : M →ₗ[R] M') f.ker }, { rw [set.range_comp, ← linear_equiv.coe_coe, span_image, hs.2, map_top, f.range] } end lemma is_basis_span (hs : linear_independent R v) : @is_basis ι R (span R (range v)) (λ i : ι, ⟨v i, subset_span (mem_range_self _)⟩) _ _ _ := begin split, { apply linear_independent_span hs }, { rw eq_top_iff', intro x, have h₁ : subtype.val '' set.range (λ i, subtype.mk (v i) _) = range v, by rw ←set.range_comp, have h₂ : map (submodule.subtype _) (span R (set.range (λ i, subtype.mk (v i) _))) = span R (range v), by rw [←span_image, submodule.subtype_eq_val, h₁], have h₃ : (x : M) ∈ map (submodule.subtype _) (span R (set.range (λ i, subtype.mk (v i) _))), by rw h₂; apply subtype.mem x, rcases mem_map.1 h₃ with ⟨y, hy₁, hy₂⟩, have h_x_eq_y : x = y, by rw [subtype.ext_iff, ← hy₂]; simp, rw h_x_eq_y, exact hy₁ } end variables (M) lemma is_basis_empty [subsingleton M] (h_empty : ¬ nonempty ι) : is_basis R (λ x : ι, (0 : M)) := ⟨ linear_independent_empty_type h_empty, subsingleton.elim _ _ ⟩ variables {M} open fintype variables [fintype ι] (h : is_basis R v) /-- A module over `R` with a finite basis is linearly equivalent to functions from its basis to `R`. -/ def is_basis.equiv_fun : M ≃ₗ[R] (ι → R) := linear_equiv.trans (module_equiv_finsupp h) { to_fun := coe_fn, map_add' := finsupp.coe_add, map_smul' := finsupp.coe_smul, ..finsupp.equiv_fun_on_fintype } /-- A module over a finite ring that admits a finite basis is finite. -/ def module.fintype_of_fintype [fintype R] : fintype M := fintype.of_equiv _ h.equiv_fun.to_equiv.symm theorem module.card_fintype [fintype R] [fintype M] : card M = (card R) ^ (card ι) := calc card M = card (ι → R) : card_congr h.equiv_fun.to_equiv ... = card R ^ card ι : card_fun /-- Given a basis `v` indexed by `ι`, the canonical linear equivalence between `ι → R` and `M` maps a function `x : ι → R` to the linear combination `∑_i x i • v i`. -/ @[simp] lemma is_basis.equiv_fun_symm_apply (x : ι → R) : h.equiv_fun.symm x = ∑ i, x i • v i := begin change finsupp.sum ((finsupp.equiv_fun_on_fintype.symm : (ι → R) ≃ (ι →₀ R)) x) (λ (i : ι) (a : R), a • v i) = ∑ i, x i • v i, dsimp [finsupp.equiv_fun_on_fintype, finsupp.sum], rw finset.sum_filter, refine finset.sum_congr rfl (λi hi, _), by_cases H : x i = 0; simp [H] end lemma is_basis.equiv_fun_apply (u : M) : h.equiv_fun u = h.repr u := rfl lemma is_basis.equiv_fun_total (u : M) : ∑ i, h.equiv_fun u i • v i = u:= begin conv_rhs { rw ← h.total_repr u }, simp [finsupp.total_apply, finsupp.sum_fintype, h.equiv_fun_apply] end @[simp] lemma is_basis.equiv_fun_self (i j : ι) : h.equiv_fun (v i) j = if i = j then 1 else 0 := by { rw [h.equiv_fun_apply, h.repr_self_apply] } @[simp] theorem is_basis.constr_apply_fintype (f : ι → M') (x : M) : (h.constr f : M → M') x = ∑ i, (h.equiv_fun x i) • f i := by simp [h.constr_apply, h.equiv_fun_apply, finsupp.sum_fintype] end module section vector_space variables [field K] [add_comm_group V] [add_comm_group V'] [vector_space K V] [vector_space K V'] variables {v : ι → V} {s t : set V} {x y z : V} include K open submodule lemma exists_subset_is_basis (hs : linear_independent K (λ x, x : s → V)) : ∃b, s ⊆ b ∧ is_basis K (coe : b → V) := let ⟨b, hb₀, hx, hb₂, hb₃⟩ := exists_linear_independent hs (@subset_univ _ _) in ⟨ b, hx, @linear_independent.restrict_of_comp_subtype _ _ _ id _ _ _ _ hb₃, by simp; exact eq_top_iff.2 hb₂⟩ lemma exists_sum_is_basis (hs : linear_independent K v) : ∃ (ι' : Type u) (v' : ι' → V), is_basis K (sum.elim v v') := begin -- This is a hack: we jump through hoops to reuse `exists_subset_is_basis`. let s := set.range v, let e : ι ≃ s := equiv.set.range v hs.injective, have : (λ x, x : s → V) = v ∘ e.symm := by { funext, dsimp, rw [equiv.set.apply_range_symm v], }, have : linear_independent K (λ x, x : s → V), { rw this, exact linear_independent.comp hs _ (e.symm.injective), }, obtain ⟨b, ss, is⟩ := exists_subset_is_basis this, let e' : ι ⊕ (b \ s : set V) ≃ b := calc ι ⊕ (b \ s : set V) ≃ s ⊕ (b \ s : set V) : equiv.sum_congr e (equiv.refl _) ... ≃ b : equiv.set.sum_diff_subset ss, refine ⟨(b \ s : set V), λ x, x.1, _⟩, convert is_basis.comp is e' _, { funext x, cases x; simp; refl, }, { exact e'.bijective, }, end variables (K V) lemma exists_is_basis : ∃b : set V, is_basis K (λ i, i : b → V) := let ⟨b, _, hb⟩ := exists_subset_is_basis (linear_independent_empty K V : _) in ⟨b, hb⟩ variables {K V} lemma linear_map.exists_left_inverse_of_injective (f : V →ₗ[K] V') (hf_inj : f.ker = ⊥) : ∃g:V' →ₗ V, g.comp f = linear_map.id := begin rcases exists_is_basis K V with ⟨B, hB⟩, have hB₀ : _ := hB.1.to_subtype_range, have : linear_independent K (λ x, x : f '' B → V'), { have h₁ := hB₀.image_subtype (show disjoint (span K (range (λ i : B, i.val))) (linear_map.ker f), by simp [hf_inj]), rwa subtype.range_coe at h₁ }, rcases exists_subset_is_basis this with ⟨C, BC, hC⟩, haveI : inhabited V := ⟨0⟩, use hC.constr (C.restrict (inv_fun f)), refine hB.ext (λ b, _), rw image_subset_iff at BC, have : f b = (⟨f b, BC b.2⟩ : C) := rfl, dsimp, rw [this, constr_basis hC], exact left_inverse_inv_fun (linear_map.ker_eq_bot.1 hf_inj) _ end lemma submodule.exists_is_compl (p : submodule K V) : ∃ q : submodule K V, is_compl p q := let ⟨f, hf⟩ := p.subtype.exists_left_inverse_of_injective p.ker_subtype in ⟨f.ker, linear_map.is_compl_of_proj $ linear_map.ext_iff.1 hf⟩ instance vector_space.submodule.is_complemented : is_complemented (submodule K V) := ⟨submodule.exists_is_compl⟩ lemma linear_map.exists_right_inverse_of_surjective (f : V →ₗ[K] V') (hf_surj : f.range = ⊤) : ∃g:V' →ₗ V, f.comp g = linear_map.id := begin rcases exists_is_basis K V' with ⟨C, hC⟩, haveI : inhabited V := ⟨0⟩, use hC.constr (C.restrict (inv_fun f)), refine hC.ext (λ c, _), simp [constr_basis hC, right_inverse_inv_fun (linear_map.range_eq_top.1 hf_surj) c] end /-- Any linear map `f : p →ₗ[K] V'` defined on a subspace `p` can be extended to the whole space. -/ lemma linear_map.exists_extend {p : submodule K V} (f : p →ₗ[K] V') : ∃ g : V →ₗ[K] V', g.comp p.subtype = f := let ⟨g, hg⟩ := p.subtype.exists_left_inverse_of_injective p.ker_subtype in ⟨f.comp g, by rw [linear_map.comp_assoc, hg, f.comp_id]⟩ open submodule linear_map /-- If `p < ⊤` is a subspace of a vector space `V`, then there exists a nonzero linear map `f : V →ₗ[K] K` such that `p ≤ ker f`. -/ lemma submodule.exists_le_ker_of_lt_top (p : submodule K V) (hp : p < ⊤) : ∃ f ≠ (0 : V →ₗ[K] K), p ≤ ker f := begin rcases submodule.exists_of_lt hp with ⟨v, -, hpv⟩, clear hp, rcases (linear_pmap.sup_span_singleton ⟨p, 0⟩ v (1 : K) hpv).to_fun.exists_extend with ⟨f, hf⟩, refine ⟨f, _, _⟩, { rintro rfl, rw [linear_map.zero_comp] at hf, have := linear_pmap.sup_span_singleton_apply_mk ⟨p, 0⟩ v (1 : K) hpv 0 p.zero_mem 1, simpa using (linear_map.congr_fun hf _).trans this }, { refine λ x hx, mem_ker.2 _, have := linear_pmap.sup_span_singleton_apply_mk ⟨p, 0⟩ v (1 : K) hpv x hx 0, simpa using (linear_map.congr_fun hf _).trans this } end theorem quotient_prod_linear_equiv (p : submodule K V) : nonempty ((p.quotient × p) ≃ₗ[K] V) := let ⟨q, hq⟩ := p.exists_is_compl in nonempty.intro $ ((quotient_equiv_of_is_compl p q hq).prod (linear_equiv.refl _ _)).trans (prod_equiv_of_is_compl q p hq.symm) open fintype variables (K) (V) theorem vector_space.card_fintype [fintype K] [fintype V] : ∃ n : ℕ, card V = (card K) ^ n := exists.elim (exists_is_basis K V) $ λ b hb, ⟨card b, module.card_fintype hb⟩ end vector_space
8c3e876c166a3103d5163d4ed9e144316386912d
77b92c2594ee6c8822c41bc409f414deb757b28b
/src/week04.lean
669091ace63c76e3cb47127d4ca9f4259769ae6e
[]
no_license
UVM-M52/week04-fgdorais
93165eda8d65b0f726025317bfdb9520a2cc062c
92271a8cd22a3654b1a2833dfa86a8de21e0d2d3
refs/heads/master
1,609,158,415,360
1,580,860,763,000
1,580,860,763,000
238,332,133
0
0
null
null
null
null
UTF-8
Lean
false
false
3,276
lean
-- Math 52: Week 4 import .utils.int_refl -- Lakins Definition 1.2.1: definition is_even (n : ℤ) := ∃ (k : ℤ), n = 2 * k definition is_odd (n : ℤ) := ∃ (k : ℤ), n = 2 * k + 1 -- Lakins Definition 2.1.1: Let a, b ∈ Z. -- a divides b if there exists n ∈ ℤ such that b = an. -- We write a ∣ b for "a divides b" and say that a is -- a divisor of b. definition divides (a b : ℤ) : Prop := ∃ (n : ℤ), a * n = b -- The notation `a ∣ b` can be used for `divides a b` local infix ∣ := divides -- Lakins Example 2.1.2: example : 3 ∣ 12 := begin unfold divides, existsi (4:ℤ), refl, end -- Theorem: For every integer a, a ∣ a. theorem divides_refl : ∀ (a : ℤ), a ∣ a := begin intro a, unfold divides, existsi (1:ℤ), calc a * 1 = a : by rw mul_one, end -- Proof: Let a ∈ ℤ be arbitrary. We must show that a ∣ a; -- i.e., we must find an integer k such that a = a * k. -- Since a = a * 1 and 1 is an integer, we see that -- a ∣ a is true. □ -- Lakins Proposition 2.1.3: For all integers a, b, c, -- if a ∣ b and b ∣ c, then a ∣ c. theorem divides_trans : ∀ (a b c : ℤ), a ∣ b ∧ b ∣ c → a ∣ c := begin sorry end -- Proof: Let a,b,c ∈ ℤ be arbitrary and assume that -- a ∣ b and b ∣ c. We must show that a ∣ c; i.e., we -- must find an integer k such that c = a * k. -- -- Since a ∣ b, by Definition 2.1.1 we may fix n ∈ ℤ -- such that b = a * n. Similarly, since b | c, we may -- fix m ∈ ℤ such that c = b * m, again by Definition -- 2.1.1. Then -- c = b * m = (a * n) * m = a * (n * m), -- since multiplication of integers is associative -- (Basic Properties of Integers 1.2.3). Since -- n * m ∈ ℤ, we have proved that a ∣ c, by -- Definition 2.1.1, as desired. 􏰟 -- Lakins Exercise 2.1.1: Let a,b, and c be integers. -- For all integers m and n, if a ∣ b and a ∣ c, then -- a ∣ (bm + cn). theorem L211 : ∀ (a b c m n : ℤ), a ∣ b ∧ a ∣ c → a ∣ (b * m + c * n) := begin sorry end -- Theorem: For every integer a, a is even if and only if 2 ∣ a. theorem is_even_iff_two_divides : ∀ (a : ℤ), is_even a ↔ 2 ∣ a := begin sorry end -- We will prove this fact later after we discuss induction. -- For now we take it as an axiom, i.e., as statement that we -- take as true without proof. axiom even_or_odd (a : ℤ) : is_even a ∨ is_odd a -- Lakins Theorem 2.1.9: For all integers a, a(a + 1) is even. theorem even_product : ∀ (a : ℤ), is_even (a * (a + 1)) := begin sorry end -- Proof: Let a ∈ ℤ. We show that a(a + 1) is even by -- considering two cases. -- -- Case I: a is even. -- Then 2 ∣ a, by Definition 1.2.1. Since a ∣ a(a + 1) -- by Definition 2.1.1, we have that 2 ∣ a(a + 1) since -- the divisibility relation is transitive (Proposition -- 2.1.3). Hence a(a + 1) is even. -- -- Case II: a is not even. -- Since a is not even, we know that a is odd. Then -- a+1 is even by Exercise 1.2.2b. Then, using an -- argument similar to that of Case I, we have that -- 2 ∣ (a+1) and (a+1) ∣ a(a+1), and hence 2 ∣ a(a+1) -- by Proposition 2.1.3. Thus a(a + 1) is even. -- -- Hence, since we have considered all possible cases -- for the integer a, we have proved that for all -- integers a, a(a + 1) is even. 􏰟
50a019333ee71bebb731e86aab550aaeb168344c
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/tests/compiler/phashmap3.lean
481b7b0944664f69ddcd57fd52c0459a502419a7
[ "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,830
lean
import Std.Data.PersistentHashMap import Lean.Data.Format open Lean Std Std.PersistentHashMap abbrev Map := PersistentHashMap Nat Nat partial def formatMap : Node Nat Nat → Format | Node.collision keys vals _ => Format.sbracket $ keys.size.fold (fun i fmt => let k := keys.get! i; let v := vals.get! i; let p := if i > 0 then fmt ++ format "," ++ Format.line else fmt; p ++ "c@" ++ Format.paren (format k ++ " => " ++ format v)) Format.nil | Node.entries entries => Format.sbracket $ entries.size.fold (fun i fmt => let entry := entries.get! i; let p := if i > 0 then fmt ++ format "," ++ Format.line else fmt; p ++ match entry with | Entry.null => "<null>" | Entry.ref node => formatMap node | Entry.entry k v => Format.paren (format k ++ " => " ++ format v)) Format.nil def checkState (m : Map) : IO Unit := do unless (m.stats.maxDepth == 1) do (IO.println "unexpected max depth"); unless (m.stats.numCollisions == 0) do (IO.println "unexpected number of collisions") def main : IO Unit := do let m : Map := PersistentHashMap.empty; let m := m.insert 1 1; let m := m.insert (32^5 + 1) 2; let max := PersistentHashMap.maxDepth.toNat; let m := m.insert (32^max + 1) 3; let m := m.insert (32^(max+1) + 1) 4; let m := m.insert (32^(max+2) + 1) 5; unless (m.stats.maxDepth == PersistentHashMap.maxDepth.toNat) do (IO.println "unexpected max depth"); unless (m.stats.numCollisions == 3) do (IO.println "unexpected number of collisions"); IO.println m.stats; let m := m.erase (32^(max+1) + 1); let m := m.erase (32^(max+2) + 1); let m := m.erase (32^max + 1); unless (m.stats.maxDepth == PersistentHashMap.maxDepth.toNat - 1) do (IO.println "unexpected max depth"); let m := m.erase (32^5 + 1); checkState m; IO.println m.stats
68d5dbff3913b49449140315cda79eff6b056bda
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/1426.lean
0fda8ca1efa5a26ec158c4a5df8975e400b96067
[ "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
365
lean
theorem ex1 : a * 2 = 2 * a := by have h : ∀ (a b : Nat), a * b = a * b := by intros; rfl conv at h => intro a b; lhs; rw [Nat.mul_comm] exact h 2 a #print ex1 theorem ex2 : a * 2 = 2 * a := by have h : (fun x y => x * y) = Nat.mul := by rfl conv at h => lhs; intro x y; rw [Nat.mul_comm] exact congrFun (congrFun h 2) a #print ex2
34880f432709f21966dc94d73865f09cbdf471cc
522405df6e2c9cbc59a292c30fe49b32bc7f2781
/src/hahn.lean
b9315dfd2f20b09b9bedf37bfe9cf6a3348e0ac2
[]
no_license
Ruben-VandeVelde/premathlib
539735aa9ac073cd62f979bdbe61ea8056d282d2
c126ad6fdbd04a225e4e4f76520ce711846cca23
refs/heads/master
1,662,351,323,440
1,590,959,549,000
1,590,959,549,000
267,713,324
0
0
null
null
null
null
UTF-8
Lean
false
false
2,132
lean
import analysis.complex.basic import analysis.normed_space.operator_norm import analysis.normed_space.hahn_banach import .extend open complex variables {F : Type*} [normed_group F] [normed_space ℂ F] noncomputable def restrict_scalars (p: subspace ℂ F) : subspace ℝ F := p.restrict_scalars ℝ private lemma apply_real (p : subspace ℂ F) (f' : p →L[ℝ] ℝ) : ∃ g : F →L[ℝ] ℝ, (∀ x : restrict_scalars p, g x = f' x) ∧ ∥g∥ = ∥f'∥ := exists_extension_norm_eq (p.restrict_scalars ℝ) f' theorem complex.exists_extension_norm_eq (p : subspace ℂ F) (f : p →L[ℂ] ℂ) : ∃ g : F →L[ℂ] ℂ, (∀ x : p, g x = f x) ∧ ∥g∥ = ∥f∥ := begin -- Let `fr: p →L[ℝ] ℝ` be the real part of `f`. let fr := continuous_linear_map.re.comp (f.restrict_scalars ℝ), have fr_apply : ∀ x, fr x = (f x).re := λ x, rfl, -- Use the real version to get a norm-preserving extension of `fr`, which we'll call `g: F →L[ℝ] ℝ`. rcases apply_real p fr with ⟨g, ⟨hextends, hnormeq⟩⟩, -- Now `g` can be extended to the `F →L[ℂ] ℂ` we need. use g.extend_to_C, -- It is an extension of `f`. have : ∀ x : p, g.extend_to_C x = f x, { intros, change (g x : ℂ) - I * g ((I • x) : p) = f x, rw [sub_eq_add_neg, neg_mul_eq_mul_neg, ←of_real_neg, mul_comm, ←mk_eq_add_mul_I], ext, { rw [hextends, fr_apply] }, rw [hextends (I • x : p), fr_apply, continuous_linear_map.map_smul, algebra.id.smul_eq_mul, mul_re, I_re, zero_mul, zero_sub, neg_neg, I_im, one_mul], }, split, assumption, -- And we derive the equality of the norms by bounding on both sides. refine le_antisymm _ _, { calc ∥g.extend_to_C∥ ≤ ∥g∥ : g.extend_to_C.op_norm_le_bound g.op_norm_nonneg (norm_bound _) ... = ∥fr∥ : hnormeq ... ≤ ∥continuous_linear_map.re∥ * ∥f∥ : continuous_linear_map.op_norm_comp_le _ _ ... = ∥f∥ : by rw [complex.continuous_linear_map.re_norm, one_mul], }, exact f.op_norm_le_bound g.extend_to_C.op_norm_nonneg (λ x, this x ▸ g.extend_to_C.le_op_norm x), end
3040d6025877e92b7a6b629760e2ba33c46e461d
f9a7d3a53167ab6bc77459dc7157a73c60f8aa5c
/grid.lean
9eb5a27a95f245d9c6cd4228fcdec3744eacb477
[]
no_license
Leangrids/grids
4ab67d0eaade70992ecf8eeea497ccaecfef881e
7ccc15b3051465c9cceedfbc57a3e06ddb1c8209
refs/heads/master
1,585,185,592,980
1,533,970,440,000
1,533,970,440,000
144,362,506
0
0
null
null
null
null
UTF-8
Lean
false
false
31,823
lean
import utils import data.vector data.list open utils section grids class relative_grid (α : Type*) := (carrier : Type) (rows : α → ℕ) (cols : α → ℕ) (unempty : Πg, rows g * cols g > 0) (data : Πg, bounded 0 (rows g) → bounded 0 (cols g) → carrier) class grid (α : Type*) extends relative_grid α := (bottom_left : α → point) variables {α : Type*} [grid α] (g : α) open grid relative_grid def size := rows g * cols g def gsize := @size α def top_right (bottom_left : point) (r c : ℕ) : point := ⟨bottom_left.x + c, bottom_left.y - r⟩ def top_right' (bottom_left : point) {α : Type*} [relative_grid α] (g : α) := top_right bottom_left (rows g) (cols g) lemma linear_bounds {a b : ℤ} {c : ℕ} (h₁ : a < b) (h₂ : b - ↑c ≤ a) : int.nat_abs(a + ↑c - b) < c := begin rw [← sub_nonneg, ← sub_add, sub_add_eq_add_sub] at h₂, rw [ ← int.coe_nat_lt, int.nat_abs_of_nonneg h₂, sub_lt_iff_lt_add', add_lt_add_iff_right ], exact h₁ end structure bounding_box := (p₁ : point) (p₂ : point) (h : p₁ ↗ p₂) def bound_box_str : bounding_box → string | ⟨p₁, p₂, _⟩ := "<(" ++ to_string p₁ ++ ", " ++ to_string p₂ ++ ")>" instance : has_to_string bounding_box := ⟨bound_box_str⟩ instance : has_repr bounding_box := ⟨bound_box_str⟩ def points_of_box (bb : bounding_box) : point × point := ⟨bb.p₁, bb.p₂⟩ def rows_of_box : bounding_box → ℕ | ⟨⟨_, y₁⟩, ⟨_, y₂⟩, _⟩ := int.nat_abs (y₁ - y₂) def cols_of_box : bounding_box → ℕ | ⟨⟨x₁, _⟩, ⟨x₂, _⟩, _⟩ := int.nat_abs (x₂ - x₁) def rows_of_box_pos {bb : bounding_box} : rows_of_box bb > 0 := let ⟨⟨_, y₁⟩, ⟨_, y₂⟩, h⟩ := bb in begin simp only [rows_of_box], simp [(↗)] at h, simp only [(>)], rw ← int.coe_nat_lt_coe_nat_iff, rw int.nat_abs_of_nonneg, rw lt_sub_iff, simp, exact h.right, simp only [(≥)], apply le_of_lt, rw lt_sub_iff, simp, exact h.right end def cols_of_box_pos {bb : bounding_box} : cols_of_box bb > 0 := let ⟨⟨x₁, _⟩, ⟨x₂, _⟩, h⟩ := bb in begin simp only [cols_of_box], simp [(↗)] at h, simp only [(>)], rw ← int.coe_nat_lt_coe_nat_iff, rw int.nat_abs_of_nonneg, rw lt_sub_iff, simp, exact h.left, simp only [(≥)], apply le_of_lt, rw lt_sub_iff, simp, exact h.left end attribute [simp] def grid_rows := rows g attribute [simp] def grid_cols := cols g attribute [simp] def grid_bottom_left := bottom_left g attribute [simp] def grid_top_right := top_right (bottom_left g) (rows g) (cols g) def rows_cols_bounded {rows cols : ℕ} (h₁ : rows > 0) (h₂ : cols > 0) (o : point) : o↗{x := o.x + ↑cols, y := o.y - ↑rows} := begin cases o, simp [(↗)], exact and.intro h₂ (sub_lt_self _ begin simp [(>)], exact h₁ end) end def grid_bounding_boxes : bottom_left g ↗ grid_top_right g := begin simp [grid_bounded_iff, (↗), top_right, grid.bottom_left], have h := gt_both_of_mult (relative_grid.unempty g), exact and.intro h.right (sub_lt_self _ (by simp [(>)]; exact h.left)) end def bbox_of_grid (g : α) : bounding_box := ⟨bottom_left g, grid_top_right g, grid_bounding_boxes g⟩ structure relative_point := (x : bounded 0 (rows g)) (y : bounded 0 (cols g)) def relative_point_str : relative_point g → string | ⟨x, y⟩ := "[" ++ to_string x ++ ", " ++ to_string y ++ "]" instance has_to_string_rp : has_to_string (relative_point g) := ⟨relative_point_str g⟩ instance has_repr_rp : has_repr (relative_point g) := ⟨relative_point_str g⟩ structure grid_point := (x : bounded ((grid_top_right g).y) ((bottom_left g).y)) (y : bounded ((bottom_left g).x) ((grid_top_right g).x)) def grid_point_str : grid_point g → string | ⟨x, y⟩ := "[" ++ to_string x ++ ", " ++ to_string y ++ "] - " ++ to_string (bottom_left g) instance has_to_string_gp : has_to_string (grid_point g) := ⟨grid_point_str g⟩ instance has_repr_gp : has_repr (grid_point g) := ⟨grid_point_str g⟩ def apoint_of_gpoint {g : α} : grid_point g → relative_point g | ⟨⟨x, ⟨lbx, ubx⟩⟩, ⟨y, ⟨lby, uby⟩⟩⟩ := let top_left_x := (bottom_left g).x in let top_left_y := (bottom_left g).y - rows g in ⟨ ⟨int.nat_abs (x - top_left_y), have h : x - top_left_y ≥ 0, from iff.elim_right le_sub_iff_add_le begin simp [grid_top_right, top_right] at lbx, simp [top_left_y], exact lbx end, and.intro (int.le_of_coe_nat_le_coe_nat $ eq.symm (int.nat_abs_of_nonneg h) ▸ h) (iff.elim_left (int.coe_nat_lt_coe_nat_iff _ _) $ eq.symm (int.nat_abs_of_nonneg h) ▸ begin simp only [top_left_y], rw ← sub_add, rw ← zero_add (↑(rows g)), conv { to_lhs, simp only [zero_add]}, rw add_lt_add_iff_right, rw sub_lt_iff, simp, exact ubx end)⟩, ⟨int.nat_abs (y - top_left_x), have h : y - top_left_x ≥ 0, from iff.elim_right le_sub_iff_add_le begin simp [top_left_y], exact lby end, and.intro (int.le_of_coe_nat_le_coe_nat $ eq.symm (int.nat_abs_of_nonneg h) ▸ h) (iff.elim_left (int.coe_nat_lt_coe_nat_iff _ _) $ eq.symm (int.nat_abs_of_nonneg h) ▸ begin simp only [top_left_x], simp only [grid_top_right, top_right] at uby, rw sub_lt_iff, rw add_comm, exact uby end )⟩ ⟩ def gpoint_of_apoint {g : α} : relative_point g → grid_point g | ⟨⟨x, ⟨lbx, ubx⟩⟩, ⟨y, ⟨lby, uby⟩⟩⟩ := let top_left_x := (bottom_left g).x in let top_left_y := (bottom_left g).y - rows g in ⟨ ⟨top_left_y + x, and.intro (by simp [top_left_y, top_right]) begin simp [top_left_y, top_right], conv { to_rhs, rw ← add_zero (bottom_left g).y}, rw add_lt_add_iff_left, rw ← sub_eq_add_neg, rw sub_lt_iff, simp, rw int.coe_nat_lt, exact ubx end⟩, ⟨top_left_x + y, and.intro (by simp [top_left_x]) begin simp [top_left_x, grid_top_right, top_right], rw int.coe_nat_lt, exact uby end⟩ ⟩ def prod_of_rel_point {g : α} (rp : relative_point g) := (rp.x, rp.y) def prod_of_grid_point {g : α} (ap : grid_point g) := (ap.x, ap.y) def grid_point_of_prod {g : α} (p : bounded ((bottom_left g).x) ((grid_top_right g).x) × bounded ((grid_top_right g).y) ((bottom_left g).y)) : grid_point g := ⟨p.snd, p.fst⟩ def grid_point_of_prod' {g : α} (p : bounded ((grid_top_right g).y) ((bottom_left g).y) × bounded ((bottom_left g).x) ((grid_top_right g).x)) : grid_point g := ⟨p.fst, p.snd⟩ def abs_data (g : α) := (function.uncurry (data g)) ∘ prod_of_rel_point ∘ apoint_of_gpoint lemma grid_rows_eq_bly_sub_try : grid_rows g = int.nat_abs ((bottom_left g).y - (grid_top_right g).y) := begin unfold grid_rows grid_top_right top_right, dsimp, repeat {rw ← sub_eq_add_neg}, rw [← sub_add, sub_self, zero_add], refl end lemma grid_cols_eq_trx_sub_blx : grid_cols g = int.nat_abs ((grid_top_right g).x - (bottom_left g).x) := begin unfold grid_cols grid_top_right top_right, dsimp, rw [← sub_eq_add_neg, add_comm, add_sub_cancel], refl end lemma bounded_establishes_bounds {a b : ℤ} (h : a < b) (x : bounded 0 (int.nat_abs (b - a))) : a ≤ a + ↑x ∧ a + ↑x < b := have xpos : ↑x ≥ 0, from positive_bounded _, have xmax : ↑x < int.nat_abs (b - a), from bounded_lt _, and.intro begin apply le_add_of_nonneg_right, unfold coe at * end begin rw [add_comm, ← lt_sub_iff], unfold_coes at *, rw [← int.coe_nat_lt, int.nat_abs_of_nonneg] at xmax, exact xmax, simp [(≥)], rw [ ← sub_eq_add_neg, ← add_le_add_iff_right a, zero_add, sub_add_cancel ], apply int.le_of_lt, exact h end end grids section grid_impls structure grid₀ (α : Type) := (r : ℕ) (c : ℕ) (h : r * c > 0) (data : vector α (r * c)) structure agrid₀ (α : Type) extends grid₀ α := (o : point) structure fgrid (α : Type) := (r : ℕ) (c : ℕ) (h : r * c > 0) (o : point) (data : bounded (o.y - r) o.y → bounded o.x (o.x + c) → α) end grid_impls section grid_instances lemma linearize_array {x y r c : ℕ} (xb : is_bounded 0 c x) (yb : is_bounded 0 r y) : y * c + x < r * c := let ⟨xl, xu⟩ := xb in let ⟨yl, yu⟩ := yb in have rows_pos : 0 < r, from lt_of_le_of_lt yl yu, have cols_pos : 0 < c, from lt_of_le_of_lt xl xu, have h₁ : y * c < r * c, from mul_lt_mul yu (le_refl _) cols_pos (le_of_lt rows_pos), have h₂ : ∃n, nat.succ y + n = r, from nat_le_dest yu, let ⟨n, h₂⟩ := h₂ in by rw [← h₂, right_distrib, nat.succ_mul, add_assoc]; exact add_lt_add_of_le_of_lt (le_refl _) (@nat.lt_add_right _ _ _ xu) instance rg_grid₀ {α : Type} : relative_grid (grid₀ α) := { carrier := α, rows := λg, g.r, cols := λg, g.c, unempty := λg, g.h, data := λg ⟨y, ⟨yl, yu⟩⟩ ⟨x, ⟨xl, xu⟩⟩, g.data.nth ⟨ y * g.c + x, linearize_array (is_bounded_of_bounds xl xu) (is_bounded_of_bounds yl yu) ⟩ } instance rg_agrid₀ {α : Type} : relative_grid (agrid₀ α) := { carrier := α, rows := λg, g.r, cols := λg, g.c, unempty := λ⟨⟨_, _, p, _⟩, _⟩, p, data := λg ⟨y, ⟨yl, yu⟩⟩ ⟨x, ⟨xl, xu⟩⟩, g.data.nth ⟨ y * g.c + x, linearize_array (is_bounded_of_bounds xl xu) (is_bounded_of_bounds yl yu) ⟩ } lemma absolute_bounds (o : ℤ) (r : ℕ) (x : bounded 0 r) : o - ↑r + ↑x < o := begin simp, conv {to_rhs, rw ← add_zero o}, rw add_lt_add_iff_left, apply add_lt_of_lt_sub_right, rw zero_sub, apply neg_lt_neg, cases x, rw ← coe_is_z_of_bounded, simp [z_of_bounded], rw int.coe_nat_lt, exact x_property.right end instance rg_fgrid {α : Type} : relative_grid (fgrid α) := { carrier := α, rows := λg, g.r, cols := λg, g.c, unempty := λg, g.h, data := λg x y, g.data ⟨g.o.y - ↑g.r + x, and.intro (le_add_of_nonneg_right $ by cases x; unfold_coes) (absolute_bounds g.o.y g.r x)⟩ ⟨g.o.x + y, and.intro (le_add_of_nonneg_right $ by cases x; unfold_coes) (add_lt_add_left ( let ⟨y, ⟨_, h⟩⟩ := y in iff.elim_right int.coe_nat_lt h) g.o.x)⟩ } instance ag_agrid₀ {α : Type} : grid (agrid₀ α) := { bottom_left := λg, g.o } instance ag_f {α : Type} : grid (fgrid α) := { bottom_left := λg, g.o } def point_of_grid_point {α : Type*} [grid α] (g : α) : grid_point g → point | ⟨b₁, b₂⟩ := ⟨b₁, b₂⟩ instance point_grid_point_coe {α : Type*} [grid α] (g : α) : has_coe (grid_point g) point := ⟨point_of_grid_point g⟩ end grid_instances section finite_grid open list int notation `[|`:max x `|]`:0 := nat_abs x variables {α : Type*} [grid α] (g : α) def grid_row {c d : ℤ} (a b : ℤ) (row : bounded c d) : list (bounded a b × bounded c d) := zip (range a b) (repeat row [|b - a|]) def grid_row_pure (a b row : ℤ) : list point := map (function.uncurry point.mk) $ zip (range_pure a b) (repeat row [|b - a|]) lemma grid_row_pure_bounds {a b row : ℤ} : ∀{c : point}, c ∈ grid_row_pure a b row → is_bounded a b c.x ∧ is_bounded row (row + 1) c.y := assume c h, begin simp [grid_row_pure] at h, cases h with a₁ h₁, cases h₁ with b₁ h₂, cases h₂ with h₂ h₃, have h₄ : a₁ ∈ range_pure a b, from pair_in_zip_l h₂, have h₅ : b₁ ∈ repeat row [|b + -a|], from pair_in_zip_r h₂, split; split, { rw ← h₃, have h₆ : (function.uncurry point.mk (a₁, b₁)).x = a₁, by simp [function.uncurry], rw h₆, have h₇ : is_bounded a b a₁, from range_pure_bounded h₄, cases h₇ with h₇ _, exact h₇ }, { rw ← h₃, have h₆ : (function.uncurry point.mk (a₁, b₁)).x = a₁, by simp [function.uncurry], rw h₆, have h₇ : is_bounded a b a₁, from range_pure_bounded h₄, cases h₇ with _ h₇, exact h₇ }, { rw ← h₃, have h₆ : (function.uncurry point.mk (a₁, b₁)).y = b₁, by simp [function.uncurry], rw h₆, have h₇ : b₁ = row, from repeat_bounded h₅, rw h₇ }, { rw ← h₃, have h₆ : (function.uncurry point.mk (a₁, b₁)).y = b₁, by simp [function.uncurry], rw h₆, have h₇ : b₁ = row, from repeat_bounded h₅, rw h₇, exact lt_add_succ _ _ } end lemma grid_row_empty_iff {c d : ℤ} (a b : ℤ) (row : bounded c d) : grid_row a b row = [] ↔ b ≤ a := by unfold grid_row; exact iff.intro (λh, begin rw zip_nil_iff at h, cases h, {rw range_empty_iff at h, exact h}, {rw repeat_nil at h, rw nat_abs_zero_iff at h, finish} end) (λh, have h₁ : range a b = [], from iff.elim_right (range_empty_iff _ _) h, by rw h₁; refl) lemma grid_row_length {c d : ℤ} {a b : ℤ} (h : a < b) {x : bounded c d} : length (grid_row a b x) = nat_abs (b - a) := begin unfold grid_row, rw length_zip_left, exact range_length (int.le_of_lt h), rw length_repeat, exact range_length (int.le_of_lt h) end lemma grid_row_pure_length {a b : ℤ} (h : a < b) {x : ℤ} : length (grid_row_pure a b x) = nat_abs (b - a) := begin unfold grid_row_pure, rw length_map, rw length_zip_left, exact range_length_pure (int.le_of_lt h), rw length_repeat, exact range_length_pure (int.le_of_lt h) end def grid_indices (p₁ p₂ : point) : list (bounded p₁.x p₂.x × bounded p₂.y p₁.y) := join (map (grid_row p₁.x p₂.x) (range p₂.y p₁.y)) def grid_indices_pure (p₁ p₂ : point) : list point := join (map (grid_row_pure p₁.x p₂.x) (range_pure p₂.y p₁.y)) open relative_grid grid def grid_indices_g := grid_indices (bottom_left g) (grid_top_right g) def grid_indices_g_pure := grid_indices_pure (bottom_left g) (grid_top_right g) def is_in_grid' (xy : point) := is_bounded (grid_top_right g).y (grid_bottom_left g).y xy.y ∧ is_bounded (grid_bottom_left g).x (grid_top_right g).x xy.x def is_in_grid (bb : bounding_box) (xy : point) := is_bounded bb.p₂.y bb.p₁.y xy.y ∧ is_bounded bb.p₁.x bb.p₂.x xy.x lemma grid_indices_pure_in_grid {p₁ p₂ : point} {h : p₁ ↗ p₂} : ∀{a}, a ∈ grid_indices_pure p₁ p₂ → is_in_grid ⟨p₁, p₂, h⟩ a := assume a h, begin cases a with al ar, simp [grid_indices_pure] at h, cases h with l h, cases h with h h₁, cases h with a₁ h₂, cases h₂ with h₂ h₃, have h₄ := range_pure_bounded h₂, rw ← h₃ at h₁, have h₅ := grid_row_pure_bounds h₁, split; split, { simp [bounding_box.p₁], cases h₅ with h₅l h₅r, cases h₅l with h₅l₁ h₅l₂, cases h₅r with h₅r₁ h₅r₂, simp [point.x, point.y] at *, cases h₄ with h₄l h₄r, transitivity a₁; assumption }, { have h₄ := range_pure_bounded h₂, exact lt_of_le_of_lt (le_of_lt_add_one h₅.right.right) h₄.right }, {exact h₅.left.left}, {exact h₅.left.right} end def grid_indices_g_pure_in_grid {g : α} : ∀{a}, a ∈ grid_indices_g_pure g → is_in_grid (bbox_of_grid g) a := assume a h, grid_indices_pure_in_grid h def decide_grid := map (abs_data g ∘ grid_point_of_prod) (grid_indices_g g) lemma attach_length {α : Type} {l : list α} : length (list.attach l) = length l := begin induction l with x xs ih, { refl }, { simp [attach] } end def make_bounded_idx {g : α} (p : point) (h : is_in_grid (bbox_of_grid g) p) : bounded ((bottom_left g).x) ((grid_top_right g).x) × bounded ((grid_top_right g).y) ((bottom_left g).y) := (make_bounded h.right, make_bounded h.left) def make_bounded_indices (is : list point) (h : ∀p, p ∈ is → is_in_grid (bbox_of_grid g) p) : list ( bounded ((bottom_left g).x) ((grid_top_right g).x) × bounded ((grid_top_right g).y) ((bottom_left g).y) ) := map (λp : {x // x ∈ is}, (⟨p.val.x, (h p.val p.property).right⟩, ⟨p.val.y, (h p.val p.property).left⟩)) (attach is) def decide_grid_pure := map (abs_data g ∘ grid_point_of_prod ∘ (λidx : {x // x ∈ grid_indices_g_pure g}, make_bounded_idx idx.val (grid_indices_g_pure_in_grid idx.property))) (attach $ grid_indices_g_pure g) lemma grid_indices_ignores_data {α : Type*} [grid α] {g : α} : grid_indices_g g = grid_indices (grid_bottom_left g) (grid_top_right g) := rfl def fgrid_iso_grid [decidable_eq (carrier α)] (g₁ g₂ : α) : bool := rows g₁ = rows g₂ ∧ cols g₁ = cols g₂ ∧ bottom_left g₁ = bottom_left g₂ ∧ let l₁ := decide_grid g₁ in let l₂ := decide_grid g₂ in if list_iso l₁ l₂ then tt else ff infix `~ₘ`:100 := fgrid_iso_grid instance gdec [decidable_eq (carrier α)] (g₁ g₂ : α) : decidable (g₁ ~ₘ g₂) := by apply_instance lemma fgrid_iso_refl [decidable_eq (carrier α)] : g ~ₘ g := by simp [fgrid_iso_grid, list_iso_refl] lemma fgrid_iso_symm [decidable_eq (carrier α)] {g₁ g₂ : α} (h : g₁ ~ₘ g₂) : g₂ ~ₘ g₁ := begin simp [fgrid_iso_grid] at h, simp [fgrid_iso_grid], split; try {split}, {exact eq.symm h.left}, {exact eq.symm h.right.left}, {split, exact eq.symm h.right.right.left, apply list_iso_symm, exact h.right.right.right} end lemma fgrid_iso_trans [decidable_eq (carrier α)] {g₁ g₂ g₃ : α} (h : g₁ ~ₘ g₂) (h₁ : g₂ ~ₘ g₃) : g₁ ~ₘ g₃ := begin simp [fgrid_iso_grid], simp [fgrid_iso_grid] at h h₁, split; try {split}, {cc}, {transitivity cols g₂, exact h.right.left, exact h₁.right.left}, {split, transitivity bottom_left g₂, exact h.right.right.left, exact h₁.right.right.left, exact list_iso_trans h.right.right.right h₁.right.right.right} end lemma caut_iso_size [decidable_eq (carrier α)] {g₁ g₂ : α} (h : g₁ ~ₘ g₂) : gsize g₁ = gsize g₂ := begin simp [fgrid_iso_grid] at h, by_cases h₁ : rows g₁ = rows g₂; by_cases h₂ : cols g₁ = cols g₂; try {cc}, {unfold gsize size relative_grid.rows relative_grid.cols, cc} end section grid_instances instance fgrid_functor : functor fgrid := { map := λα β f g, ⟨g.r, g.c, g.h, g.o, λx y, f (g.data x y)⟩ } end grid_instances def point_of_bounded_prod {a b c d : ℤ} : bounded a b × bounded c d → point | ⟨⟨a, _⟩, ⟨c, _⟩⟩ := ⟨a, c⟩ lemma grid_indices_bad_horizontal {p₁ p₂ : point} (h : p₂.x ≤ p₁.x) : grid_indices p₁ p₂ = [] := begin unfold grid_indices, cases p₁, cases p₂, unfold_projs at *, apply join_empty_of_all_empty, intros x h₁, rw list.mem_map at h₁, cases h₁, cases h₁_h, rw [← h₁_h_right, grid_row_empty_iff], exact h end lemma grid_indices_bad_vertical {p₁ p₂ : point} (h : p₁.y ≤ p₂.y) : grid_indices p₁ p₂ = [] := begin unfold grid_indices, cases p₁, cases p₂, unfold_projs at *, have h₁ : range p₂_y p₁_y = [], from iff.elim_right (range_empty_iff _ _) h, rw h₁, refl end -- This takes forever to typecheck due to generalize -- lemma range_ex {a b : ℤ} (h : a < b) : -- range a b = ⟨a, and.intro (le_refl _) h⟩ :: -- list.map range_weaken (range (a + 1) b) := -- begin -- generalize hack : range (a + 1) b = l, -- unfold1 utils.range, simp [h], -- rw ← hack -- end lemma range_ex {a b : ℤ} (h : a < b) : ∀l, range (a + 1) b = l → range a b = ⟨a, and.intro (le_refl _) h⟩ :: list.map range_weaken l := assume l hack, begin unfold1 utils.range, rw [dif_pos h, ← hack] end lemma grid_indices_bad_iff (p₁ p₂ : point) : grid_indices p₁ p₂ = [] ↔ ¬p₁ ↗ p₂ := iff.intro (λh, begin intros contra, cases p₁; cases p₂, simp [(↗)] at contra, cases contra with l r, unfold grid_indices at h, dsimp at h, revert h, rw range_ex r (range (p₂_y + 1) p₁_y) rfl, unfold map join grid_row, rw range_ex l (range (p₁_x + 1) p₂_x) rfl, have h₂ : nat_abs (p₂_x - p₁_x) ≥ 1, from nat_abs_of_lt l, rw repeat_more, unfold zip zip_with, intros contra, cases contra, exact h₂ end ) (λh, begin rw not_grid_bounded_iff at h, cases h, exact grid_indices_bad_horizontal h, exact grid_indices_bad_vertical h end ) lemma grid_indices_length {p₁ p₂ : point} (h : p₁ ↗ p₂) : length (grid_indices p₁ p₂) = [|p₁.y - p₂.y|] * [|p₂.x - p₁.x|] := begin rw [← int.coe_nat_eq_coe_nat_iff, ← nat_abs_mul], rw grid_bounded_iff at h, have h₁ : (p₁.y - p₂.y) * (p₂.x - p₁.x) > 0, from mul_pos begin simp [(>)], rw ← add_lt_add_iff_right p₂.y, rw zero_add, simp, exact h.right end begin simp [(>)], rw ← add_lt_add_iff_right p₁.x, rw zero_add, simp, exact h.left end, rw nat_abs_of_nonneg (int.le_of_lt h₁), unfold grid_indices, rw [length_join, map_map], simp [(∘)], simp [grid_row_length h.left ], rw range_length (int.le_of_lt h.right), repeat {rw ← sub_eq_add_neg}, repeat {rw nat_abs_of_nonneg}, rw mul_comm, simp [(≥)], rw [ ← sub_eq_add_neg, ← add_le_add_iff_right p₂.y, zero_add, sub_add_cancel ], exact (int.le_of_lt h.right), simp [(≥)], rw [ ← sub_eq_add_neg, ← add_le_add_iff_right p₁.x, zero_add, sub_add_cancel ], exact (int.le_of_lt h.left) end lemma grid_indices_pure_length {p₁ p₂ : point} (h : p₁ ↗ p₂) : length (grid_indices_pure p₁ p₂) = [|p₁.y - p₂.y|] * [|p₂.x - p₁.x|] := begin rw [← int.coe_nat_eq_coe_nat_iff, ← nat_abs_mul], rw grid_bounded_iff at h, have h₁ : (p₁.y - p₂.y) * (p₂.x - p₁.x) > 0, from mul_pos begin simp [(>)], rw ← add_lt_add_iff_right p₂.y, rw zero_add, simp, exact h.right end begin simp [(>)], rw ← add_lt_add_iff_right p₁.x, rw zero_add, simp, exact h.left end, rw nat_abs_of_nonneg (int.le_of_lt h₁), unfold grid_indices_pure, rw [length_join, map_map], simp [(∘)], simp [grid_row_pure_length h.left], rw range_length_pure (int.le_of_lt h.right), repeat {rw ← sub_eq_add_neg}, repeat {rw nat_abs_of_nonneg}, rw mul_comm, simp [(≥)], rw [ ← sub_eq_add_neg, ← add_le_add_iff_right p₂.y, zero_add, sub_add_cancel ], exact (int.le_of_lt h.right), simp [(≥)], rw [ ← sub_eq_add_neg, ← add_le_add_iff_right p₁.x, zero_add, sub_add_cancel ], exact (int.le_of_lt h.left) end lemma grid_indices_len_g_pure : length (grid_indices_g_pure g) = length (grid_indices_pure (bottom_left g) (grid_top_right g)) := by simp [grid_indices_g_pure] theorem decide_grid_length {α : Type*} [grid α] (g : α) : length (decide_grid g) = grid_rows g * grid_cols g := begin unfold decide_grid grid_indices_g, rw [ length_map, grid_rows_eq_bly_sub_try, grid_cols_eq_trx_sub_blx, grid_indices_length ], exact grid_bounding_boxes g end theorem decide_grid_pure_length {α : Type*} [grid α] (g : α) : length (decide_grid_pure g) = grid_rows g * grid_cols g := begin unfold decide_grid_pure grid_indices_g_pure, rw length_map, rw grid_rows_eq_bly_sub_try, rw grid_cols_eq_trx_sub_blx, rw attach_length, rw grid_indices_len_g_pure, rw grid_indices_pure_length, exact grid_bounding_boxes g end def agrid_of_fgrid {α : Type} (g : fgrid α) : agrid₀ α := ⟨⟨g.r, g.c, g.h, ⟨decide_grid g, decide_grid_length g⟩⟩, g.o⟩ def fgrid_of_agrid {α : Type} (g : agrid₀ α) : fgrid α := ⟨g.r, g.c, g.h, g.o, λx y, abs_data g ⟨x, y⟩⟩ instance f_a_coe {α : Type} : has_coe (fgrid α) (agrid₀ α) := ⟨agrid_of_fgrid⟩ instance a_f_coe {α : Type} : has_coe (agrid₀ α) (fgrid α) := ⟨fgrid_of_agrid⟩ def row (n : bounded 0 (relative_grid.rows g)) : (bounded 0 (relative_grid.cols g)) → relative_grid.carrier α := relative_grid.data g n def col (n : bounded 0 (relative_grid.cols g)) : (bounded 0 (relative_grid.rows g)) → relative_grid.carrier α := flip (relative_grid.data g) n def top := row g ⟨0, and.intro (le_refl _) begin exact and.elim_left (gt_both_of_mult (relative_grid.unempty g)) end⟩ def bot := row g ⟨nat.pred (relative_grid.rows g), and.intro (nat.zero_le _) begin apply nat.pred_lt, have c_unempty := relative_grid.unempty g, unfold_projs, cases relative_grid.rows g, rw zero_mul at c_unempty, have : ¬0 > 0, from gt_irrefl _, contradiction, trivial end⟩ def left := have h : relative_grid.cols g > 0, from and.elim_right (gt_both_of_mult (relative_grid.unempty g)), col g ⟨0, and.intro (le_refl _) h⟩ def right := have h : relative_grid.cols g > 0, from and.elim_right (gt_both_of_mult (relative_grid.unempty g)), col g ⟨nat.pred (relative_grid.cols g), and.intro (nat.zero_le _) begin apply nat.pred_lt, cases relative_grid.cols g, have : ¬0 > 0, from gt_irrefl _, contradiction, trivial end⟩ def grid_bounds' (h : relative_grid.rows g * relative_grid.cols g > 0) : bounding_box := ⟨grid_bottom_left g, grid_top_right g, have h : _ := gt_both_of_mult h, rows_cols_bounded h.left h.right _⟩ def grid_bounds : bounding_box := ⟨grid_bottom_left g, grid_top_right g, have h : _ := gt_both_of_mult (relative_grid.unempty g), rows_cols_bounded h.left h.right _⟩ instance decidable_is_in_grid' {xy : point} : decidable (is_in_grid' g xy) := by simp [is_in_grid']; apply_instance instance decidable_is_in_grid (bb : bounding_box) {xy : point} : decidable (is_in_grid bb xy) := by simp [is_in_grid]; apply_instance lemma is_in_grid_grid'_iff (xy : point) : is_in_grid' g xy ↔ is_in_grid ⟨grid.bottom_left g, grid_top_right g, grid_bounding_boxes g⟩ xy := iff.intro (λh, and.intro h.left h.right) (λh, and.intro h.left h.right) def overlaid_by (p₁ p₂ : bounding_box) := (p₂.p₁.x ≤ p₁.p₁.x ∧ p₁.p₂.x ≤ p₂.p₂.x) ∧ (p₂.p₂.y ≤ p₁.p₂.y ∧ p₁.p₁.y ≤ p₂.p₁.y) def in_grid_bounded (p : point) (h : is_in_grid' g p) := let ⟨left, right⟩ := h in (make_bounded left, make_bounded right) instance overlaid_decidable (p₁ p₂ : bounding_box) : decidable (overlaid_by p₁ p₂) := by simp [overlaid_by]; apply_instance lemma overlaid_by_refl (p : bounding_box) : overlaid_by p p := by simp [overlaid_by]; repeat {split}; refl lemma overlaid_by_trans {p₁ p₂ p₃ : bounding_box} (h : overlaid_by p₁ p₂) (h₁ : overlaid_by p₂ p₃) : overlaid_by p₁ p₃ := begin simp [overlaid_by] at *; repeat {split}; transitivity, exact h₁.left.left, exact h.left.left, exact h.left.right, exact h₁.left.right, exact h₁.right.left, exact h.right.left, exact h.right.right, exact h₁.right.right end lemma overlaid_by_antisymm {p₁ p₂ : bounding_box} (h : overlaid_by p₁ p₂) (h₁ : overlaid_by p₂ p₁) : p₁ = p₂ := begin simp [overlaid_by] at *, cases h, cases h₁, cases h_left, cases h_right, cases h₁_left, cases h₁_right, cases p₁, cases p₂, congr; unfold bounding_box.p₁ bounding_box.p₂ at *; cases p₁_p₁; cases p₂_p₂; cases p₁_p₂; cases p₂_p₁; unfold point.x point.y at *; congr; apply le_antisymm; assumption end lemma is_in_larger {bb₁ bb₂ : bounding_box} {xy : point} (h : is_in_grid bb₁ xy) (h₁ : overlaid_by bb₁ bb₂) : is_in_grid bb₂ xy := begin unfold overlaid_by is_in_grid is_bounded at *, exact and.intro (and.intro (le_trans h₁.right.left h.left.left) (lt_of_lt_of_le h.left.right h₁.right.right)) (and.intro (le_trans h₁.left.left h.right.left) (lt_of_lt_of_le h.right.right h₁.left.right)) end end finite_grid section grid_instances def split_rows_cols : ℕ → ℕ → list string → list string | cols 0 ls := [""] | cols (k + 1) ls := list.take cols ls ++ ["\n"] ++ split_rows_cols cols k (list.drop cols ls) def grid_str {α : Type*} [grid α] [has_to_string (relative_grid.carrier α)] (g : α) : string := let points := list.map to_string $ decide_grid g in " " ++ (list.foldr append "" $ list.intersperse " " $ split_rows_cols (relative_grid.cols g) (relative_grid.rows g) points) instance grid_repr {α : Type*} [grid α] [has_to_string (relative_grid.carrier α)] : has_repr α := ⟨grid_str⟩ instance grid_to_string {α : Type*} [grid α] [has_to_string (relative_grid.carrier α)] : has_to_string α := ⟨grid_str⟩ end grid_instances
1a38dea13e1b0cc1df1e2ac0e640011e6b238100
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/order/game_add.lean
443a2df46ee4530c4cb42556cf7250ca9b6fdbb3
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
9,582
lean
/- Copyright (c) 2022 Junyan Xu. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Junyan Xu -/ import data.sym.sym2 import logic.relation /-! # Game addition relation > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines, given relations `rα : α → α → Prop` and `rβ : β → β → Prop`, a relation `prod.game_add` on pairs, such that `game_add rα rβ x y` iff `x` can be reached from `y` by decreasing either entry (with respect to `rα` and `rβ`). It is so called since it models the subsequency relation on the addition of combinatorial games. We also define `sym2.game_add`, which is the unordered pair analog of `prod.game_add`. ## Main definitions and results - `prod.game_add`: the game addition relation on ordered pairs. - `well_founded.prod_game_add`: formalizes induction on ordered pairs, where exactly one entry decreases at a time. - `sym2.game_add`: the game addition relation on unordered pairs. - `well_founded.sym2_game_add`: formalizes induction on unordered pairs, where exactly one entry decreases at a time. -/ variables {α β : Type*} {rα : α → α → Prop} {rβ : β → β → Prop} /-! ### `prod.game_add` -/ namespace prod variables (rα rβ) /-- `prod.game_add rα rβ x y` means that `x` can be reached from `y` by decreasing either entry with respect to the relations `rα` and `rβ`. It is so called, as it models game addition within combinatorial game theory. If `rα a₁ a₂` means that `a₂ ⟶ a₁` is a valid move in game `α`, and `rβ b₁ b₂` means that `b₂ ⟶ b₁` is a valid move in game `β`, then `game_add rα rβ` specifies the valid moves in the juxtaposition of `α` and `β`: the player is free to choose one of the games and make a move in it, while leaving the other game unchanged. See `sym2.game_add` for the unordered pair analog. -/ inductive game_add : α × β → α × β → Prop | fst {a₁ a₂ b} : rα a₁ a₂ → game_add (a₁, b) (a₂, b) | snd {a b₁ b₂} : rβ b₁ b₂ → game_add (a, b₁) (a, b₂) lemma game_add_iff {rα rβ} {x y : α × β} : game_add rα rβ x y ↔ rα x.1 y.1 ∧ x.2 = y.2 ∨ rβ x.2 y.2 ∧ x.1 = y.1 := begin split, { rintro (@⟨a₁, a₂, b, h⟩ | @⟨a, b₁, b₂, h⟩), exacts [or.inl ⟨h, rfl⟩, or.inr ⟨h, rfl⟩] }, { revert x y, rintro ⟨a₁, b₁⟩ ⟨a₂, b₂⟩ (⟨h, rfl : b₁ = b₂⟩ | ⟨h, rfl : a₁ = a₂⟩), exacts [game_add.fst h, game_add.snd h] } end lemma game_add_mk_iff {rα rβ} {a₁ a₂ : α} {b₁ b₂ : β} : game_add rα rβ (a₁, b₁) (a₂, b₂) ↔ rα a₁ a₂ ∧ b₁ = b₂ ∨ rβ b₁ b₂ ∧ a₁ = a₂ := game_add_iff @[simp] lemma game_add_swap_swap : ∀ a b : α × β, game_add rβ rα a.swap b.swap ↔ game_add rα rβ a b := λ ⟨a₁, b₁⟩ ⟨a₂, b₂⟩, by rw [prod.swap, game_add_mk_iff, game_add_mk_iff, or_comm] lemma game_add_swap_swap_mk (a₁ a₂ : α) (b₁ b₂ : β) : game_add rα rβ (a₁, b₁) (a₂, b₂) ↔ game_add rβ rα (b₁, a₁) (b₂, a₂) := game_add_swap_swap rβ rα (b₁, a₁) (b₂, a₂) /-- `prod.game_add` is a `subrelation` of `prod.lex`. -/ lemma game_add_le_lex : game_add rα rβ ≤ prod.lex rα rβ := λ _ _ h, h.rec (λ _ _ b, prod.lex.left b b) (λ a _ _, prod.lex.right a) /-- `prod.rprod` is a subrelation of the transitive closure of `prod.game_add`. -/ lemma rprod_le_trans_gen_game_add : rprod rα rβ ≤ relation.trans_gen (game_add rα rβ) := λ _ _ h, h.rec begin intros _ _ _ _ hα hβ, exact relation.trans_gen.tail (relation.trans_gen.single $ game_add.fst hα) (game_add.snd hβ), end end prod /-- If `a` is accessible under `rα` and `b` is accessible under `rβ`, then `(a, b)` is accessible under `prod.game_add rα rβ`. Notice that `prod.lex_accessible` requires the stronger condition `∀ b, acc rβ b`. -/ lemma acc.prod_game_add {a b} (ha : acc rα a) (hb : acc rβ b) : acc (prod.game_add rα rβ) (a, b) := begin induction ha with a ha iha generalizing b, induction hb with b hb ihb, refine acc.intro _ (λ h, _), rintro (⟨ra⟩ | ⟨rb⟩), exacts [iha _ ra (acc.intro b hb), ihb _ rb], end /-- The `prod.game_add` relation on well-founded inputs is well-founded. In particular, the sum of two well-founded games is well-founded. -/ lemma well_founded.prod_game_add (hα : well_founded rα) (hβ : well_founded rβ) : well_founded (prod.game_add rα rβ) := ⟨λ ⟨a, b⟩, (hα.apply a).prod_game_add (hβ.apply b)⟩ namespace prod /-- Recursion on the well-founded `prod.game_add` relation. Note that it's strictly more general to recurse on the lexicographic order instead. -/ def game_add.fix {C : α → β → Sort*} (hα : well_founded rα) (hβ : well_founded rβ) (IH : Π a₁ b₁, (Π a₂ b₂, game_add rα rβ (a₂, b₂) (a₁, b₁) → C a₂ b₂) → C a₁ b₁) (a : α) (b : β) : C a b := @well_founded.fix (α × β) (λ x, C x.1 x.2) _ (hα.prod_game_add hβ) (λ ⟨x₁, x₂⟩ IH', IH x₁ x₂ $ λ a' b', IH' ⟨a', b'⟩) ⟨a, b⟩ lemma game_add.fix_eq {C : α → β → Sort*} (hα : well_founded rα) (hβ : well_founded rβ) (IH : Π a₁ b₁, (Π a₂ b₂, game_add rα rβ (a₂, b₂) (a₁, b₁) → C a₂ b₂) → C a₁ b₁) (a : α) (b : β) : game_add.fix hα hβ IH a b = IH a b (λ a' b' h, game_add.fix hα hβ IH a' b') := well_founded.fix_eq _ _ _ /-- Induction on the well-founded `prod.game_add` relation. Note that it's strictly more general to induct on the lexicographic order instead. -/ lemma game_add.induction {C : α → β → Prop} : well_founded rα → well_founded rβ → (∀ a₁ b₁, (∀ a₂ b₂, game_add rα rβ (a₂, b₂) (a₁, b₁) → C a₂ b₂) → C a₁ b₁) → ∀ a b, C a b := game_add.fix end prod /-! ### `sym2.game_add` -/ namespace sym2 /-- `sym2.game_add rα x y` means that `x` can be reached from `y` by decreasing either entry with respect to the relation `rα`. See `prod.game_add` for the ordered pair analog. -/ def game_add (rα : α → α → Prop): sym2 α → sym2 α → Prop := sym2.lift₂ ⟨λ a₁ b₁ a₂ b₂, prod.game_add rα rα (a₁, b₁) (a₂, b₂) ∨ prod.game_add rα rα (b₁, a₁) (a₂, b₂), λ a₁ b₁ a₂ b₂, begin rw [prod.game_add_swap_swap_mk _ _ b₁ b₂ a₁ a₂, prod.game_add_swap_swap_mk _ _ a₁ b₂ b₁ a₂], simp [or_comm] end⟩ variable {rα} lemma game_add_iff : ∀ {x y : α × α}, game_add rα ⟦x⟧ ⟦y⟧ ↔ prod.game_add rα rα x y ∨ prod.game_add rα rα x.swap y := by { rintros ⟨_, _⟩ ⟨_, _⟩, refl } lemma game_add_mk_iff {a₁ a₂ b₁ b₂ : α} : game_add rα ⟦(a₁, b₁)⟧ ⟦(a₂, b₂)⟧ ↔ prod.game_add rα rα (a₁, b₁) (a₂, b₂) ∨ prod.game_add rα rα (b₁, a₁) (a₂, b₂) := iff.rfl lemma _root_.prod.game_add.to_sym2 {a₁ a₂ b₁ b₂ : α} (h : prod.game_add rα rα (a₁, b₁) (a₂, b₂)) : sym2.game_add rα ⟦(a₁, b₁)⟧ ⟦(a₂, b₂)⟧ := game_add_mk_iff.2 $ or.inl $ h lemma game_add.fst {a₁ a₂ b : α} (h : rα a₁ a₂) : game_add rα ⟦(a₁, b)⟧ ⟦(a₂, b)⟧ := (prod.game_add.fst h).to_sym2 lemma game_add.snd {a b₁ b₂ : α} (h : rα b₁ b₂) : game_add rα ⟦(a, b₁)⟧ ⟦(a, b₂)⟧ := (prod.game_add.snd h).to_sym2 lemma game_add.fst_snd {a₁ a₂ b : α} (h : rα a₁ a₂) : game_add rα ⟦(a₁, b)⟧ ⟦(b, a₂)⟧ := by { rw sym2.eq_swap, exact game_add.snd h } lemma game_add.snd_fst {a₁ a₂ b : α} (h : rα a₁ a₂) : game_add rα ⟦(b, a₁)⟧ ⟦(a₂, b)⟧ := by { rw sym2.eq_swap, exact game_add.fst h } end sym2 lemma acc.sym2_game_add {a b} (ha : acc rα a) (hb : acc rα b) : acc (sym2.game_add rα) ⟦(a, b)⟧ := begin induction ha with a ha iha generalizing b, induction hb with b hb ihb, refine acc.intro _ (λ s, _), induction s using sym2.induction_on with c d, rintros ((rc | rd) | (rd | rc)), { exact iha c rc ⟨b, hb⟩ }, { exact ihb d rd }, { rw sym2.eq_swap, exact iha d rd ⟨b, hb⟩ }, { rw sym2.eq_swap, exact ihb c rc } end /-- The `sym2.game_add` relation on well-founded inputs is well-founded. -/ lemma well_founded.sym2_game_add (h : well_founded rα) : well_founded (sym2.game_add rα) := ⟨λ i, sym2.induction_on i $ λ x y, (h.apply x).sym2_game_add (h.apply y)⟩ namespace sym2 /-- Recursion on the well-founded `sym2.game_add` relation. -/ def game_add.fix {C : α → α → Sort*} (hr : well_founded rα) (IH : Π a₁ b₁, (Π a₂ b₂, sym2.game_add rα ⟦(a₂, b₂)⟧ ⟦(a₁, b₁)⟧ → C a₂ b₂) → C a₁ b₁) (a b : α) : C a b := @well_founded.fix (α × α) (λ x, C x.1 x.2) _ hr.sym2_game_add.of_quotient_lift₂ (λ ⟨x₁, x₂⟩ IH', IH x₁ x₂ $ λ a' b', IH' ⟨a', b'⟩) (a, b) lemma game_add.fix_eq {C : α → α → Sort*} (hr : well_founded rα) (IH : Π a₁ b₁, (Π a₂ b₂, sym2.game_add rα ⟦(a₂, b₂)⟧ ⟦(a₁, b₁)⟧ → C a₂ b₂) → C a₁ b₁) (a b : α) : game_add.fix hr IH a b = IH a b (λ a' b' h, game_add.fix hr IH a' b') := well_founded.fix_eq _ _ _ /-- Induction on the well-founded `sym2.game_add` relation. -/ lemma game_add.induction {C : α → α → Prop} : well_founded rα → (∀ a₁ b₁, (∀ a₂ b₂, sym2.game_add rα ⟦(a₂, b₂)⟧ ⟦(a₁, b₁)⟧ → C a₂ b₂) → C a₁ b₁) → ∀ a b, C a b := game_add.fix end sym2
7d368eebfca72a20852b7893b74e33fd0c20bb44
86d328c7fd9114507f3e8380e2b3d2f83ef6c647
/lambda/types.lean
4a4e6fe5f13726c4158d83694b92e955c7186b4f
[]
no_license
forked-from-1kasper/lambda
05ada15e7abdaa5f453bfe3792c086f04f363f76
696363514f3730951a90e86474940135a2339c0b
refs/heads/master
1,623,388,766,920
1,607,249,080,000
1,607,249,080,000
128,524,636
2
1
null
null
null
null
UTF-8
Lean
false
false
2,104
lean
namespace types inductive term : Type | var : string → term | app : term → term → term | lam : string → term → term open term def multi_lam (names : list string) (body : term) : term := list.foldr lam body names def multi_app (t : term) (b : list term) : term := list.foldl app t b def term_to_string : term → string | (var n) := sformat! "{n}" | (app (var e₁) (var e₂)) := sformat! "{e₁} {e₂}" | (app e₁ (var e₂)) := sformat! "({term_to_string e₁}) {e₂}" | (app (var e₁) e₂) := sformat! "{e₁} ({term_to_string e₂})" | (app e₁ e₂) := sformat! "({term_to_string e₁}) ({term_to_string e₂})" | (lam n t) := sformat! "λ{n}, {term_to_string t}" instance term_has_to_string : has_to_string term := ⟨term_to_string⟩ instance term_has_repr : has_repr term := ⟨term_to_string⟩ def subst : string → term → term → term | x newVal (lam y body) := if x ≠ y then lam y (subst x newVal body) else lam y body | x newVal (app e₁ e₂) := app (subst x newVal e₁) (subst x newVal e₂) | x newVal (var y) := if x = y then newVal else var y inductive eval_result | limit | normal def result_to_string : eval_result → string | eval_result.limit := "maximum recursion depth exceeded" | eval_result.normal := "normal form" instance result_has_to_string : has_to_string eval_result := ⟨result_to_string⟩ instance result_has_repr : has_repr eval_result := ⟨result_to_string⟩ def eval : nat → term → term × eval_result | 0 r := (r, eval_result.limit) | _ (lam x e) := (lam x e, eval_result.normal) | _ (var n) := (var n, eval_result.normal) | (nat.succ n) (app e₁ e₂) := match eval n e₁ with | (lam x body, _) := eval n (subst x e₂ body) | (res, r) := (app res e₂, r) end inductive repl_command : Type | term : term → repl_command | quit | help | env | show_depth | clear_env | nothing | show_import_depth | load : string → repl_command | depth : nat → repl_command | import_depth : nat → repl_command | bind : string → term → repl_command end types
669ac9aae1876f3e5d1d8c59f7797391d608ee20
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/tactic/lint/default.lean
a3669a5862a11a72c4fcab9d40da713f35602358
[ "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
4,348
lean
/- Copyright (c) 2020 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Robert Y. Lewis, Gabriel Ebner -/ import tactic.lint.frontend import tactic.lint.simp import tactic.lint.type_classes import tactic.lint.misc open tactic add_tactic_doc { name := "linting commands", category := doc_category.cmd, decl_names := [`lint_cmd, `lint_mathlib_cmd, `lint_all_cmd, `list_linters], tags := ["linting"], description := "User commands to spot common mistakes in the code * `#lint`: check all declarations in the current file * `#lint_mathlib`: check all declarations in mathlib (so excluding core or other projects, and also excluding the current file) * `#lint_all`: check all declarations in the environment (the current file and all imported files) The following linters are run by default: 1. `unused_arguments` checks for unused arguments in declarations. 2. `def_lemma` checks whether a declaration is incorrectly marked as a def/lemma. 3. `dup_namespce` checks whether a namespace is duplicated in the name of a declaration. 4. `ge_or_gt` checks whether ≥/> is used in the declaration. 5. `instance_priority` checks that instances that always apply have priority below default. 6. `doc_blame` checks for missing doc strings on definitions and constants. 7. `has_inhabited_instance` checks whether every type has an associated `inhabited` instance. 8. `impossible_instance` checks for instances that can never fire. 9. `incorrect_type_class_argument` checks for arguments in [square brackets] that are not classes. 10. `dangerous_instance` checks for instances that generate type-class problems with metavariables. 11. `fails_quickly` tests that type-class inference ends (relatively) quickly when applied to variables. 12. `has_coe_variable` tests that there are no instances of type `has_coe α t` for a variable `α`. 13. `inhabited_nonempty` checks for `inhabited` instance arguments that should be changed to `nonempty`. 14. `simp_nf` checks that the left-hand side of simp lemmas is in simp-normal form. 15. `simp_var_head` checks that there are no variables as head symbol of left-hand sides of simp lemmas. 16. `simp_comm` checks that no commutativity lemmas (such as `add_comm`) are marked simp. 17. `decidable_classical` checks for `decidable` hypotheses that are used in the proof of a proposition but not in the statement, and could be removed using `classical`. Theorems in the `decidable` namespace are exempt. 18. `has_coe_to_fun` checks that every type that coerces to a function has a direct `has_coe_to_fun` instance. 19. `check_type` checks that the statement of a declaration is well-typed. Another linter, `doc_blame_thm`, checks for missing doc strings on lemmas and theorems. This is not run by default. The command `#list_linters` prints a list of the names of all available linters. You can append a `*` to any command (e.g. `#lint_mathlib*`) to omit the slow tests (4). You can append a `-` to any command (e.g. `#lint_mathlib-`) to run a silent lint that suppresses the output if all checks pass. A silent lint will fail if any test fails. You can append a `+` to any command (e.g. `#lint_mathlib+`) to run a verbose lint that reports the result of each linter, including the successes. You can append a sequence of linter names to any command to run extra tests, in addition to the default ones. e.g. `#lint doc_blame_thm` will run all default tests and `doc_blame_thm`. You can append `only name1 name2 ...` to any command to run a subset of linters, e.g. `#lint only unused_arguments` You can add custom linters by defining a term of type `linter` in the `linter` namespace. A linter defined with the name `linter.my_new_check` can be run with `#lint my_new_check` or `lint only my_new_check`. If you add the attribute `@[linter]` to `linter.my_new_check` it will run by default. Adding the attribute `@[nolint doc_blame unused_arguments]` to a declaration omits it from only the specified linter checks." } /-- The default linters used in mathlib CI. -/ meta def mathlib_linters : list name := by do ls ← get_checks tt [] ff, let ls := ls.map (λ ⟨n, _⟩, `linter ++ n), exact (reflect ls)
6f9b52fd164b54ebafea639727ef89dc97ac1ff5
13133fade54057ee588bc056e4eaa14a24773d23
/report/fibonacci_succ.lean
427ebf855267e5df788139c4a82e20482ee271bc
[]
no_license
lkloh/lean-project-15815
444cbdca1d1a2dfa258c76c41a6ff846392e13d1
2cb657c0e41baa318193f7dce85974ff37d80883
refs/heads/master
1,611,402,038,933
1,432,020,760,000
1,432,020,760,000
33,372,120
0
0
null
1,431,932,928,000
1,428,078,840,000
Lean
UTF-8
Lean
false
false
2,816
lean
import data.nat open nat -- source: the official tutorial for lean definition fib : nat → nat | fib 0 := 0 | fib (succ 0) := 1 | fib (succ (succ n)) := fib n + fib (succ n) eval fib(5) -- ****************************************************************** -- -- 0 <= fib(n) theorem fib_pos : ∀ n, 0 ≤ fib n, fib_pos 0 := show 0 ≤ 0, from le.refl (0), fib_pos (succ 0) := calc 0 ≤ 1 : zero_le_one, fib_pos (succ (succ n)) := calc 0 = 0 + 0 : rfl ... ≤ fib n + 0 : add_le_add_right (fib_pos n) 0 ... ≤ fib n + fib (succ n) : add_le_add_left (fib_pos (succ n)) (fib n) ... = fib (succ (succ n)) : rfl -- ****************************************************************** -- -- "fib (n + k + 1) = fib (k + 1) * fib (n + 1) + fib k * fib n" theorem fib_add (n k : ℕ) : fib(succ (n + k)) = fib (succ k) * fib (succ n) + fib k * fib n := nat.induction_on k (calc fib(succ (n + 0)) = fib (succ n) : rfl ... = 1 * fib (succ n) : one_mul ... = fib (succ 0) * fib (succ n) : rfl ... = fib (succ 0) * fib (succ n) + 0 : add_zero ... = fib (succ 0) * fib (succ n) + 0 * fib n : zero_mul ... = fib (succ 0) * fib (succ n) + fib 0 * fib n : rfl) (take k' IH, calc fib(succ (n + succ k')) = fib(succ (succ (n + k'))) : add_succ ... = fib (succ (succ k')) * fib (succ n) + fib (succ k') * fib n : sorry) -- ****************************************************************** -- -- "gcd (m::nat) (m + n) = gcd m n" -- taken from isabelle theorem gcd_add2_nat (m n: ℕ) : gcd m (m + n) = gcd m n := sorry -- ****************************************************************** -- theorem gcd_succ_fib : ∀ n, gcd (fib n) (fib (succ n)) = 1 := sorry -- ****************************************************************** -- -- gcd (fib n) (fib (n + 1)) = 1 theorem gcd_fib_Suc_eq_1 : ∀ n, gcd (fib n) (fib (n + 1)) = 1, gcd_fib_Suc_eq_1 0 := calc gcd (fib 0) (fib(0 + 1)) = gcd 0 (fib(0 + 1)) : rfl ... = fib(0 + 1) : gcd_zero_left ... = fib 1 : zero_add ... = fib (succ 0) : rfl ... = 1 : rfl, gcd_fib_Suc_eq_1 (succ n) := calc gcd (fib (succ n)) (fib (succ n + 1)) = gcd (fib (succ n)) (fib (succ (n + 1))) : succ_add ... = gcd (fib (succ n)) ( (fib (succ 1)) * (fib (succ n)) + (fib 1) * (fib n)) : fib_add ... = gcd (fib (succ n)) ( 1 * (fib (succ n)) + (fib 1) * (fib n)) : rfl ... = gcd (fib (succ n)) ( 1 * (fib (succ n)) + 1 * (fib n)) : rfl ... = gcd (fib (succ n)) ( (fib (succ n)) + 1 * (fib n)) : one_mul ... = gcd (fib (succ n)) ( (fib (succ n)) + (fib n) ) : one_mul ... = gcd (fib (succ n)) (fib n) : gcd_add2_nat ... = gcd (fib n) (fib (succ n)) : gcd.comm ... = 1 : gcd_succ_fib -- ****************************************************************** --
bdc2acfd8d3d6100a1414ebd8663f6b21456f2e8
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/logic/pairwise.lean
4835ef323ddfcc6d65acace90d69edf19c9f13f7
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
2,632
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 logic.function.basic import tactic.basic /-! # Relations holding pairwise > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines pairwise relations. ## Main declarations * `pairwise`: `pairwise r` states that `r i j` for all `i ≠ j`. * `set.pairwise`: `s.pairwise r` states that `r i j` for all `i ≠ j` with `i, j ∈ s`. -/ open set function variables {α β γ ι ι' : Type*} {r p q : α → α → Prop} section pairwise variables {f g : ι → α} {s t u : set α} {a b : α} /-- A relation `r` holds pairwise if `r i j` for all `i ≠ j`. -/ def pairwise (r : α → α → Prop) := ∀ ⦃i j⦄, i ≠ j → r i j lemma pairwise.mono (hr : pairwise r) (h : ∀ ⦃i j⦄, r i j → p i j) : pairwise p := λ i j hij, h $ hr hij protected lemma pairwise.eq (h : pairwise r) : ¬ r a b → a = b := not_imp_comm.1 $ @h _ _ lemma function.injective_iff_pairwise_ne : injective f ↔ pairwise ((≠) on f) := forall₂_congr $ λ i j, not_imp_not.symm alias function.injective_iff_pairwise_ne ↔ function.injective.pairwise_ne _ namespace set /-- The relation `r` holds pairwise on the set `s` if `r x y` for all *distinct* `x y ∈ s`. -/ protected def pairwise (s : set α) (r : α → α → Prop) := ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, y ∈ s → x ≠ y → r x y lemma pairwise_of_forall (s : set α) (r : α → α → Prop) (h : ∀ a b, r a b) : s.pairwise r := λ a _ b _ _, h a b lemma pairwise.imp_on (h : s.pairwise r) (hrp : s.pairwise (λ ⦃a b : α⦄, r a b → p a b)) : s.pairwise p := λ a ha b hb hab, hrp ha hb hab $ h ha hb hab lemma pairwise.imp (h : s.pairwise r) (hpq : ∀ ⦃a b : α⦄, r a b → p a b) : s.pairwise p := h.imp_on $ pairwise_of_forall s _ hpq protected lemma pairwise.eq (hs : s.pairwise r) (ha : a ∈ s) (hb : b ∈ s) (h : ¬ r a b) : a = b := of_not_not $ λ hab, h $ hs ha hb hab lemma _root_.reflexive.set_pairwise_iff (hr : reflexive r) : s.pairwise r ↔ ∀ ⦃a⦄, a ∈ s → ∀ ⦃b⦄, b ∈ s → r a b := forall₄_congr $ λ a _ b _, or_iff_not_imp_left.symm.trans $ or_iff_right_of_imp $ eq.rec $ hr a lemma pairwise.on_injective (hs : s.pairwise r) (hf : function.injective f) (hfs : ∀ x, f x ∈ s) : pairwise (r on f) := λ i j hij, hs (hfs i) (hfs j) (hf.ne hij) end set lemma pairwise.set_pairwise (h : pairwise r) (s : set α) : s.pairwise r := λ x hx y hy w, h w end pairwise
d404f9d14edc3655a2a8088a7c682de6201e3635
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/geometry/manifold/algebra/left_invariant_derivation.lean
d25e5845f7cd4973ee7cffd80644ad248c908753
[ "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
8,749
lean
/- Copyright © 2020 Nicolò Cavalleri. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Nicolò Cavalleri -/ import geometry.manifold.derivation_bundle /-! # Left invariant derivations In this file we define the concept of left invariant derivation for a Lie group. The concept is analogous to the more classical concept of left invariant vector fields, and it holds that the derivation associated to a vector field is left invariant iff the field is. Moreover we prove that `left_invariant_derivation I G` has the structure of a Lie algebra, hence implementing one of the possible definitions of the Lie algebra attached to a Lie group. -/ noncomputable theory open_locale lie_group manifold derivation variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H) (G : Type*) [topological_space G] [charted_space H G] [monoid G] [has_smooth_mul I G] (g h : G) -- Generate trivial has_sizeof instance. It prevents weird type class inference timeout problems local attribute [nolint instance_priority, instance, priority 10000] private def disable_has_sizeof {α} : has_sizeof α := ⟨λ _, 0⟩ /-- Left-invariant global derivations. A global derivation is left-invariant if it is equal to its pullback along left multiplication by an arbitrary element of `G`. -/ structure left_invariant_derivation extends derivation 𝕜 C^∞⟮I, G; 𝕜⟯ C^∞⟮I, G; 𝕜⟯ := (left_invariant'' : ∀ g, 𝒅ₕ(smooth_left_mul_one I g) (derivation.eval_at 1 to_derivation) = derivation.eval_at g to_derivation) variables {I G} namespace left_invariant_derivation instance : has_coe (left_invariant_derivation I G) (derivation 𝕜 C^∞⟮I, G; 𝕜⟯ C^∞⟮I, G; 𝕜⟯) := ⟨λ X, X.to_derivation⟩ instance : has_coe_to_fun (left_invariant_derivation I G) (λ _, C^∞⟮I, G; 𝕜⟯ → C^∞⟮I, G; 𝕜⟯) := ⟨λ X, X.to_derivation.to_fun⟩ variables {M : Type*} [topological_space M] [charted_space H M] {x : M} {r : 𝕜} {X Y : left_invariant_derivation I G} {f f' : C^∞⟮I, G; 𝕜⟯} lemma to_fun_eq_coe : X.to_fun = ⇑X := rfl lemma coe_to_linear_map : ⇑(X : C^∞⟮I, G; 𝕜⟯ →ₗ[𝕜] C^∞⟮I, G; 𝕜⟯) = X := rfl @[simp] lemma to_derivation_eq_coe : X.to_derivation = X := rfl lemma coe_injective : @function.injective (left_invariant_derivation I G) (_ → C^⊤⟮I, G; 𝕜⟯) coe_fn := λ X Y h, by { cases X, cases Y, congr', exact derivation.coe_injective h } @[ext] theorem ext (h : ∀ f, X f = Y f) : X = Y := coe_injective $ funext h variables (X Y f) lemma coe_derivation : ⇑(X : derivation 𝕜 C^∞⟮I, G; 𝕜⟯ C^∞⟮I, G; 𝕜⟯) = (X : C^∞⟮I, G; 𝕜⟯ → C^∞⟮I, G; 𝕜⟯) := rfl lemma coe_derivation_injective : function.injective (coe : left_invariant_derivation I G → derivation 𝕜 C^∞⟮I, G; 𝕜⟯ C^∞⟮I, G; 𝕜⟯) := λ X Y h, by { cases X, cases Y, congr, exact h } /-- Premature version of the lemma. Prefer using `left_invariant` instead. -/ lemma left_invariant' : 𝒅ₕ(smooth_left_mul_one I g) (derivation.eval_at (1 : G) ↑X) = derivation.eval_at g ↑X := by rw [←to_derivation_eq_coe]; exact left_invariant'' X g @[simp] lemma map_add : X (f + f') = X f + X f' := derivation.map_add X f f' @[simp] lemma map_zero : X 0 = 0 := derivation.map_zero X @[simp] lemma map_neg : X (-f) = -X f := derivation.map_neg X f @[simp] lemma map_sub : X (f - f') = X f - X f' := derivation.map_sub X f f' @[simp] lemma map_smul : X (r • f) = r • X f := derivation.map_smul X r f @[simp] lemma leibniz : X (f * f') = f • X f' + f' • X f := X.leibniz' _ _ instance : has_zero (left_invariant_derivation I G) := ⟨⟨0, λ g, by simp only [linear_map.map_zero, derivation.coe_zero]⟩⟩ instance : inhabited (left_invariant_derivation I G) := ⟨0⟩ instance : has_add (left_invariant_derivation I G) := { add := λ X Y, ⟨X + Y, λ g, by simp only [linear_map.map_add, derivation.coe_add, left_invariant', pi.add_apply]⟩ } instance : has_neg (left_invariant_derivation I G) := { neg := λ X, ⟨-X, λ g, by simp only [linear_map.map_neg, derivation.coe_neg, left_invariant', pi.neg_apply]⟩ } instance : has_sub (left_invariant_derivation I G) := { sub := λ X Y, ⟨X - Y, λ g, by simp only [linear_map.map_sub, derivation.coe_sub, left_invariant', pi.sub_apply]⟩ } @[simp] lemma coe_add : ⇑(X + Y) = X + Y := rfl @[simp] lemma coe_zero : ⇑(0 : left_invariant_derivation I G) = 0 := rfl @[simp] lemma coe_neg : ⇑(-X) = -X := rfl @[simp] lemma coe_sub : ⇑(X - Y) = X - Y := rfl @[simp, norm_cast] lemma lift_add : (↑(X + Y) : derivation 𝕜 C^∞⟮I, G; 𝕜⟯ C^∞⟮I, G; 𝕜⟯) = X + Y := rfl @[simp, norm_cast] lemma lift_zero : (↑(0 : left_invariant_derivation I G) : derivation 𝕜 C^∞⟮I, G; 𝕜⟯ C^∞⟮I, G; 𝕜⟯) = 0 := rfl instance : add_comm_group (left_invariant_derivation I G) := coe_injective.add_comm_group _ coe_zero coe_add coe_neg coe_sub instance : has_scalar 𝕜 (left_invariant_derivation I G) := { smul := λ r X, ⟨r • X, λ g, by simp only [derivation.Rsmul_apply, algebra.id.smul_eq_mul, mul_eq_mul_left_iff, linear_map.map_smul, left_invariant']⟩ } variables (r X) @[simp] lemma coe_smul : ⇑(r • X) = r • X := rfl @[simp] lemma lift_smul (k : 𝕜) : (↑(k • X) : derivation 𝕜 C^∞⟮I, G; 𝕜⟯ C^∞⟮I, G; 𝕜⟯) = k • X := rfl variables (I G) /-- The coercion to function is a monoid homomorphism. -/ @[simps] def coe_fn_add_monoid_hom : (left_invariant_derivation I G) →+ (C^∞⟮I, G; 𝕜⟯ → C^∞⟮I, G; 𝕜⟯) := ⟨λ X, X.to_derivation.to_fun, coe_zero, coe_add⟩ variables {I G} instance : module 𝕜 (left_invariant_derivation I G) := coe_injective.module _ (coe_fn_add_monoid_hom I G) coe_smul /-- Evaluation at a point for left invariant derivation. Same thing as for generic global derivations (`derivation.eval_at`). -/ def eval_at : (left_invariant_derivation I G) →ₗ[𝕜] (point_derivation I g) := { to_fun := λ X, derivation.eval_at g ↑X, map_add' := λ X Y, rfl, map_smul' := λ k X, rfl } lemma eval_at_apply : eval_at g X f = (X f) g := rfl @[simp] lemma eval_at_coe : derivation.eval_at g ↑X = eval_at g X := rfl lemma left_invariant : 𝒅ₕ(smooth_left_mul_one I g) (eval_at (1 : G) X) = eval_at g X := (X.left_invariant'' g) lemma eval_at_mul : eval_at (g * h) X = 𝒅ₕ(L_apply I g h) (eval_at h X) := by { ext f, rw [←left_invariant, apply_hfdifferential, apply_hfdifferential, L_mul, fdifferential_comp, apply_fdifferential, linear_map.comp_apply, apply_fdifferential, ←apply_hfdifferential, left_invariant] } lemma comp_L : (X f).comp (𝑳 I g) = X (f.comp (𝑳 I g)) := by ext h; rw [times_cont_mdiff_map.comp_apply, L_apply, ←eval_at_apply, eval_at_mul, apply_hfdifferential, apply_fdifferential, eval_at_apply] instance : has_bracket (left_invariant_derivation I G) (left_invariant_derivation I G) := { bracket := λ X Y, ⟨⁅(X : derivation 𝕜 C^∞⟮I, G; 𝕜⟯ C^∞⟮I, G; 𝕜⟯), Y⁆, λ g, begin ext f, have hX := derivation.congr_fun (left_invariant' g X) (Y f), have hY := derivation.congr_fun (left_invariant' g Y) (X f), rw [apply_hfdifferential, apply_fdifferential, derivation.eval_at_apply] at hX hY ⊢, rw comp_L at hX hY, rw [derivation.commutator_apply, smooth_map.coe_sub, pi.sub_apply, coe_derivation], rw coe_derivation at hX hY ⊢, rw [hX, hY], refl end⟩ } @[simp] lemma commutator_coe_derivation : ⇑⁅X, Y⁆ = (⁅(X : derivation 𝕜 C^∞⟮I, G; 𝕜⟯ C^∞⟮I, G; 𝕜⟯), Y⁆ : derivation 𝕜 C^∞⟮I, G; 𝕜⟯ C^∞⟮I, G; 𝕜⟯) := rfl lemma commutator_apply : ⁅X, Y⁆ f = X (Y f) - Y (X f) := rfl instance : lie_ring (left_invariant_derivation I G) := { add_lie := λ X Y Z, by { ext1, simp only [commutator_apply, coe_add, pi.add_apply, linear_map.map_add, left_invariant_derivation.map_add], ring }, lie_add := λ X Y Z, by { ext1, simp only [commutator_apply, coe_add, pi.add_apply, linear_map.map_add, left_invariant_derivation.map_add], ring }, lie_self := λ X, by { ext1, simp only [commutator_apply, sub_self], refl }, leibniz_lie := λ X Y Z, by { ext1, simp only [commutator_apply, coe_add, coe_sub, map_sub, pi.add_apply], ring, } } instance : lie_algebra 𝕜 (left_invariant_derivation I G) := { lie_smul := λ r Y Z, by { ext1, simp only [commutator_apply, map_smul, smul_sub, coe_smul, pi.smul_apply] } } end left_invariant_derivation
a117aabb0bf2c09ca551f957b32a0c3f5fea2ea4
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/group_theory/submonoid/center.lean
b14fc116cda81becbb3a03a786e32b2fca229707
[ "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,480
lean
/- Copyright (c) 2021 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser -/ import group_theory.submonoid.operations /-! # Centers of magmas and monoids ## Main definitions * `set.center`: the center of a magma * `submonoid.center`: the center of a monoid * `set.add_center`: the center of an additive magma * `add_submonoid.center`: the center of an additive monoid We provide `subgroup.center`, `add_subgroup.center`, `subsemiring.center`, and `subring.center` in other files. -/ variables {M : Type*} namespace set variables (M) /-- The center of a magma. -/ @[to_additive add_center /-" The center of an additive magma. "-/] def center [has_mul M] : set M := {z | ∀ m, m * z = z * m} @[to_additive mem_add_center] lemma mem_center_iff [has_mul M] {z : M} : z ∈ center M ↔ ∀ g, g * z = z * g := iff.rfl @[simp, to_additive zero_mem_add_center] lemma one_mem_center [mul_one_class M] : (1 : M) ∈ set.center M := by simp [mem_center_iff] @[simp] lemma zero_mem_center [mul_zero_class M] : (0 : M) ∈ set.center M := by simp [mem_center_iff] variables {M} @[simp, to_additive add_mem_add_center] lemma mul_mem_center [semigroup M] {a b : M} (ha : a ∈ set.center M) (hb : b ∈ set.center M) : a * b ∈ set.center M := λ g, by rw [mul_assoc, ←hb g, ← mul_assoc, ha g, mul_assoc] @[simp, to_additive neg_mem_add_center] lemma inv_mem_center [group M] {a : M} (ha : a ∈ set.center M) : a⁻¹ ∈ set.center M := λ g, by rw [← inv_inj, mul_inv_rev, inv_inv, ← ha, mul_inv_rev, inv_inv] @[simp] lemma add_mem_center [distrib M] {a b : M} (ha : a ∈ set.center M) (hb : b ∈ set.center M) : a + b ∈ set.center M := λ c, by rw [add_mul, mul_add, ha c, hb c] @[simp] lemma neg_mem_center [ring M] {a : M} (ha : a ∈ set.center M) : -a ∈ set.center M := λ c, by rw [←neg_mul_comm, ha (-c), neg_mul_comm] @[to_additive subset_add_center_add_units] lemma subset_center_units [monoid M] : (coe : units M → M) ⁻¹' center M ⊆ set.center (units M) := λ a ha b, units.ext $ ha _ lemma center_units_subset [group_with_zero M] : set.center (units M) ⊆ (coe : units M → M) ⁻¹' center M := λ a ha b, begin obtain rfl | hb := eq_or_ne b 0, { rw [zero_mul, mul_zero], }, { exact units.ext_iff.mp (ha (units.mk0 _ hb)) } end /-- In a group with zero, the center of the units is the preimage of the center. -/ lemma center_units_eq [group_with_zero M] : set.center (units M) = (coe : units M → M) ⁻¹' center M := subset.antisymm center_units_subset subset_center_units @[simp] lemma inv_mem_center' [group_with_zero M] {a : M} (ha : a ∈ set.center M) : a⁻¹ ∈ set.center M := begin obtain rfl | ha0 := eq_or_ne a 0, { rw inv_zero, exact zero_mem_center M }, lift a to units M using ha0, rw ←units.coe_inv', exact center_units_subset (inv_mem_center (subset_center_units ha)), end @[simp, to_additive sub_mem_add_center] lemma div_mem_center [group M] {a b : M} (ha : a ∈ set.center M) (hb : b ∈ set.center M) : a / b ∈ set.center M := begin rw [div_eq_mul_inv], exact mul_mem_center ha (inv_mem_center hb), end @[simp] lemma div_mem_center' [group_with_zero M] {a b : M} (ha : a ∈ set.center M) (hb : b ∈ set.center M) : a / b ∈ set.center M := begin rw div_eq_mul_inv, exact mul_mem_center ha (inv_mem_center' hb), end variables (M) @[simp, to_additive add_center_eq_univ] lemma center_eq_univ [comm_semigroup M] : center M = set.univ := subset.antisymm (subset_univ _) $ λ x _ y, mul_comm y x end set namespace submonoid section variables (M) [monoid M] /-- The center of a monoid `M` is the set of elements that commute with everything in `M` -/ @[to_additive "The center of a monoid `M` is the set of elements that commute with everything in `M`"] def center : submonoid M := { carrier := set.center M, one_mem' := set.one_mem_center M, mul_mem' := λ a b, set.mul_mem_center } @[to_additive] lemma coe_center : ↑(center M) = set.center M := rfl variables {M} @[to_additive] lemma mem_center_iff {z : M} : z ∈ center M ↔ ∀ g, g * z = z * g := iff.rfl /-- The center of a monoid is commutative. -/ instance : comm_monoid (center M) := { mul_comm := λ a b, subtype.ext $ b.prop _, .. (center M).to_monoid } end section variables (M) [comm_monoid M] @[simp] lemma center_eq_top : center M = ⊤ := set_like.coe_injective (set.center_eq_univ M) end end submonoid
026e89586c05770c768aae3d86af940146c67757
94e33a31faa76775069b071adea97e86e218a8ee
/src/algebra/module/submodule/pointwise.lean
3fba1a558e6584a37cbac3f3ef62d7f9b2549a1a
[ "Apache-2.0" ]
permissive
urkud/mathlib
eab80095e1b9f1513bfb7f25b4fa82fa4fd02989
6379d39e6b5b279df9715f8011369a301b634e41
refs/heads/master
1,658,425,342,662
1,658,078,703,000
1,658,078,703,000
186,910,338
0
0
Apache-2.0
1,568,512,083,000
1,557,958,709,000
Lean
UTF-8
Lean
false
false
7,354
lean
/- Copyright (c) 2021 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser -/ import group_theory.subgroup.pointwise import linear_algebra.span /-! # Pointwise instances on `submodule`s This file provides: * `submodule.has_pointwise_neg` and the actions * `submodule.pointwise_distrib_mul_action` * `submodule.pointwise_mul_action_with_zero` which matches the action of `mul_action_set`. These actions are available in the `pointwise` locale. ## Implementation notes Most of the lemmas in this file are direct copies of lemmas from `group_theory/submonoid/pointwise.lean`. -/ variables {α : Type*} {R : Type*} {M : Type*} open_locale pointwise namespace submodule section neg section semiring variables [semiring R] [add_comm_group M] [module R M] /-- The submodule with every element negated. Note if `R` is a ring and not just a semiring, this is a no-op, as shown by `submodule.neg_eq_self`. Recall that When `R` is the semiring corresponding to the nonnegative elements of `R'`, `submodule R' M` is the type of cones of `M`. This instance reflects such cones about `0`. This is available as an instance in the `pointwise` locale. -/ protected def has_pointwise_neg : has_neg (submodule R M) := { neg := λ p, { carrier := -(p : set M), smul_mem' := λ r m hm, set.mem_neg.2 $ smul_neg r m ▸ p.smul_mem r $ set.mem_neg.1 hm, ..(- p.to_add_submonoid) } } localized "attribute [instance] submodule.has_pointwise_neg" in pointwise open_locale pointwise @[simp] lemma coe_set_neg (S : submodule R M) : ↑(-S) = -(S : set M) := rfl @[simp] lemma neg_to_add_submonoid (S : submodule R M) : (-S).to_add_submonoid = -S.to_add_submonoid := rfl @[simp] lemma mem_neg {g : M} {S : submodule R M} : g ∈ -S ↔ -g ∈ S := iff.rfl /-- `submodule.has_pointwise_neg` is involutive. This is available as an instance in the `pointwise` locale. -/ protected def has_involutive_pointwise_neg : has_involutive_neg (submodule R M) := { neg := has_neg.neg, neg_neg := λ S, set_like.coe_injective $ neg_neg _ } localized "attribute [instance] submodule.has_involutive_pointwise_neg" in pointwise @[simp] lemma neg_le_neg (S T : submodule R M) : -S ≤ -T ↔ S ≤ T := set_like.coe_subset_coe.symm.trans set.neg_subset_neg lemma neg_le (S T : submodule R M) : -S ≤ T ↔ S ≤ -T := set_like.coe_subset_coe.symm.trans set.neg_subset /-- `submodule.has_pointwise_neg` as an order isomorphism. -/ def neg_order_iso : submodule R M ≃o submodule R M := { to_equiv := equiv.neg _, map_rel_iff' := neg_le_neg } lemma closure_neg (s : set M) : span R (-s) = -(span R s) := begin apply le_antisymm, { rw [span_le, coe_set_neg, ←set.neg_subset, neg_neg], exact subset_span }, { rw [neg_le, span_le, coe_set_neg, ←set.neg_subset], exact subset_span } end @[simp] lemma neg_inf (S T : submodule R M) : -(S ⊓ T) = (-S) ⊓ (-T) := set_like.coe_injective set.inter_neg @[simp] lemma neg_sup (S T : submodule R M) : -(S ⊔ T) = (-S) ⊔ (-T) := (neg_order_iso : submodule R M ≃o submodule R M).map_sup S T @[simp] lemma neg_bot : -(⊥ : submodule R M) = ⊥ := set_like.coe_injective $ (set.neg_singleton 0).trans $ congr_arg _ neg_zero @[simp] lemma neg_top : -(⊤ : submodule R M) = ⊤ := set_like.coe_injective $ set.neg_univ @[simp] lemma neg_infi {ι : Sort*} (S : ι → submodule R M) : -(⨅ i, S i) = ⨅ i, -S i := (neg_order_iso : submodule R M ≃o submodule R M).map_infi _ @[simp] lemma neg_supr {ι : Sort*} (S : ι → submodule R M) : -(⨆ i, S i) = ⨆ i, -(S i) := (neg_order_iso : submodule R M ≃o submodule R M).map_supr _ end semiring open_locale pointwise @[simp] lemma neg_eq_self [ring R] [add_comm_group M] [module R M] (p : submodule R M) : -p = p := ext $ λ _, p.neg_mem_iff end neg variables [semiring R] [add_comm_monoid M] [module R M] instance pointwise_add_comm_monoid : add_comm_monoid (submodule R M) := { add := (⊔), add_assoc := λ _ _ _, sup_assoc, zero := ⊥, zero_add := λ _, bot_sup_eq, add_zero := λ _, sup_bot_eq, add_comm := λ _ _, sup_comm } @[simp] lemma add_eq_sup (p q : submodule R M) : p + q = p ⊔ q := rfl @[simp] lemma zero_eq_bot : (0 : submodule R M) = ⊥ := rfl instance : canonically_ordered_add_monoid (submodule R M) := { zero := 0, bot := ⊥, add := (+), add_le_add_left := λ a b, sup_le_sup_left, exists_add_of_le := λ a b h, ⟨b, (sup_eq_right.2 h).symm⟩, le_self_add := λ a b, le_sup_left, ..submodule.pointwise_add_comm_monoid, ..submodule.complete_lattice } section variables [monoid α] [distrib_mul_action α M] [smul_comm_class α R M] /-- The action on a submodule corresponding to applying the action to every element. This is available as an instance in the `pointwise` locale. -/ protected def pointwise_distrib_mul_action : distrib_mul_action α (submodule R M) := { smul := λ a S, S.map (distrib_mul_action.to_linear_map _ _ a), one_smul := λ S, (congr_arg (λ f, S.map f) (linear_map.ext $ by exact one_smul α)).trans S.map_id, mul_smul := λ a₁ a₂ S, (congr_arg (λ f : M →ₗ[R] M, S.map f) (linear_map.ext $ by exact mul_smul _ _)).trans (S.map_comp _ _), smul_zero := λ a, map_bot _, smul_add := λ a S₁ S₂, map_sup _ _ _ } localized "attribute [instance] submodule.pointwise_distrib_mul_action" in pointwise open_locale pointwise @[simp] lemma coe_pointwise_smul (a : α) (S : submodule R M) : ↑(a • S) = a • (S : set M) := rfl @[simp] lemma pointwise_smul_to_add_submonoid (a : α) (S : submodule R M) : (a • S).to_add_submonoid = a • S.to_add_submonoid := rfl @[simp] lemma pointwise_smul_to_add_subgroup {R M : Type*} [ring R] [add_comm_group M] [distrib_mul_action α M] [module R M] [smul_comm_class α R M] (a : α) (S : submodule R M) : (a • S).to_add_subgroup = a • S.to_add_subgroup := rfl lemma smul_mem_pointwise_smul (m : M) (a : α) (S : submodule R M) : m ∈ S → a • m ∈ a • S := (set.smul_mem_smul_set : _ → _ ∈ a • (S : set M)) instance pointwise_central_scalar [distrib_mul_action αᵐᵒᵖ M] [smul_comm_class αᵐᵒᵖ R M] [is_central_scalar α M] : is_central_scalar α (submodule R M) := ⟨λ a S, congr_arg (λ f, S.map f) $ linear_map.ext $ by exact op_smul_eq_smul _⟩ @[simp] lemma smul_le_self_of_tower {α : Type*} [semiring α] [module α R] [module α M] [smul_comm_class α R M] [is_scalar_tower α R M] (a : α) (S : submodule R M) : a • S ≤ S := begin rintro y ⟨x, hx, rfl⟩, exact smul_of_tower_mem _ a hx, end end section variables [semiring α] [module α M] [smul_comm_class α R M] /-- The action on a submodule corresponding to applying the action to every element. This is available as an instance in the `pointwise` locale. This is a stronger version of `submodule.pointwise_distrib_mul_action`. Note that `add_smul` does not hold so this cannot be stated as a `module`. -/ protected def pointwise_mul_action_with_zero : mul_action_with_zero α (submodule R M) := { zero_smul := λ S, (congr_arg (λ f : M →ₗ[R] M, S.map f) (linear_map.ext $ by exact zero_smul α)).trans S.map_zero, .. submodule.pointwise_distrib_mul_action } localized "attribute [instance] submodule.pointwise_mul_action_with_zero" in pointwise end end submodule
6fa810094efe9c4f6c0755f32594871b9f3ea0f9
46125763b4dbf50619e8846a1371029346f4c3db
/src/category_theory/limits/shapes/zero.lean
85c77410a3db30bb2905fc9a45152ca0a5a3ef37
[ "Apache-2.0" ]
permissive
thjread/mathlib
a9d97612cedc2c3101060737233df15abcdb9eb1
7cffe2520a5518bba19227a107078d83fa725ddc
refs/heads/master
1,615,637,696,376
1,583,953,063,000
1,583,953,063,000
246,680,271
0
0
Apache-2.0
1,583,960,875,000
1,583,960,875,000
null
UTF-8
Lean
false
false
3,913
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 category_theory.limits.shapes.terminal /-! # Zero morphisms and zero objects A category "has zero morphisms" if there is a designated "zero morphism" in each morphism space, and compositions of zero morphisms with anything give the zero morphism. (Notice this is extra structure, not merely a property.) A category "has a zero object" if it has an object which is both initial and terminal. Having a zero object provides zero morphisms, as the unique morphisms factoring through the zero object. ## References * https://en.wikipedia.org/wiki/Zero_morphism * [F. Borceux, *Handbook of Categorical Algebra 2*][borceux-vol2] -/ universes v u open category_theory namespace category_theory.limits variables (C : Type u) [𝒞 : category.{v} C] include 𝒞 /-- A category "has zero morphisms" if there is a designated "zero morphism" in each morphism space, and compositions of zero morphisms with anything give the zero morphism. -/ class has_zero_morphisms := [has_zero : Π X Y : C, has_zero (X ⟶ Y)] (comp_zero' : ∀ {X Y : C} (f : X ⟶ Y) (Z : C), f ≫ (0 : Y ⟶ Z) = (0 : X ⟶ Z) . obviously) (zero_comp' : ∀ (X : C) {Y Z : C} (f : Y ⟶ Z), (0 : X ⟶ Y) ≫ f = (0 : X ⟶ Z) . obviously) attribute [instance] has_zero_morphisms.has_zero restate_axiom has_zero_morphisms.comp_zero' attribute [simp] has_zero_morphisms.comp_zero restate_axiom has_zero_morphisms.zero_comp' attribute [simp, reassoc] has_zero_morphisms.zero_comp section variables {C} [has_zero_morphisms.{v} C] lemma zero_of_comp_mono {X Y Z : C} {f : X ⟶ Y} {g : Y ⟶ Z} [mono g] (h : f ≫ g = 0) : f = 0 := by { rw [←has_zero_morphisms.zero_comp.{v} C X g, cancel_mono] at h, exact h } lemma zero_of_comp_epi {X Y Z : C} {f : X ⟶ Y} {g : Y ⟶ Z} [epi f] (h : f ≫ g = 0) : g = 0 := by { rw [←has_zero_morphisms.comp_zero.{v} C f Z, cancel_epi] at h, exact h } end /-- A category "has a zero object" if it has an object which is both initial and terminal. -/ class has_zero_object := (zero : C) (unique_to : Π X : C, unique (zero ⟶ X)) (unique_from : Π X : C, unique (X ⟶ zero)) variables [has_zero_object.{v} C] variables {C} /-- Construct a `has_zero C` for a category with a zero object. This can not be a global instance as it will trigger for every `has_zero C` typeclass search. -/ def zero_of_zero_object : has_zero C := { zero := has_zero_object.zero.{v} C } local attribute [instance] zero_of_zero_object local attribute [instance] has_zero_object.unique_to has_zero_object.unique_from namespace has_zero_object /-- A category with a zero object has zero morphisms. -/ def zero_morphisms_of_zero_object : has_zero_morphisms.{v} C := { has_zero := λ X Y, { zero := inhabited.default (X ⟶ 0) ≫ inhabited.default (0 ⟶ Y) }, zero_comp' := λ X Y Z f, by { dunfold has_zero.zero, rw category.assoc, congr, }, comp_zero' := λ X Y Z f, by { dunfold has_zero.zero, rw ←category.assoc, congr, }} local attribute [instance] zero_morphisms_of_zero_object /-- An arrow ending in the zero object is zero -/ lemma zero_of_to_zero {X : C} (f : X ⟶ 0) : f = 0 := begin rw (has_zero_object.unique_from.{v} X).uniq f, rw (has_zero_object.unique_from.{v} X).uniq (0 : X ⟶ 0) end /-- An arrow starting at the zero object is zero -/ lemma zero_of_from_zero {X : C} (f : 0 ⟶ X) : f = 0 := begin rw (has_zero_object.unique_to.{v} X).uniq f, rw (has_zero_object.unique_to.{v} X).uniq (0 : 0 ⟶ X) end /-- A zero object is in particular initial. -/ def has_initial_of_has_zero_object : has_initial.{v} C := has_initial_of_unique 0 /-- A zero object is in particular terminal. -/ def has_terminal_of_has_zero_object : has_terminal.{v} C := has_terminal_of_unique 0 end has_zero_object end category_theory.limits
62fc83bbafc51fa6ef4b6e857dccfd7dbcfe96d5
82b86ba2ae0d5aed0f01f49c46db5afec0eb2bd7
/src/Lean/Data/JsonRpc.lean
45b61ea434b57b309f34a1dfc7cca92c43594444
[ "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
7,232
lean
/- Copyright (c) 2020 Marc Huisinga. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Marc Huisinga, Wojciech Nawrocki -/ import Init.Control import Init.System.IO import Std.Data.RBTree import Lean.Data.Json /-! Implementation of JSON-RPC 2.0 (https://www.jsonrpc.org/specification) for use in the LSP server. -/ namespace Lean namespace JsonRpc open Json open Std (RBNode) inductive RequestID | str (s : String) | num (n : JsonNumber) | null /-- Error codes defined by JSON-RPC and LSP. -/ inductive ErrorCode | parseError | invalidRequest | methodNotFound | invalidParams | internalError | serverErrorStart | serverErrorEnd | serverNotInitialized | unknownErrorCode -- LSP-specific codes below. | requestCancelled | contentModified instance : FromJson ErrorCode := ⟨fun j => match j with | num (-32700 : Int) => ErrorCode.parseError | num (-32600 : Int) => ErrorCode.invalidRequest | num (-32601 : Int) => ErrorCode.methodNotFound | num (-32602 : Int) => ErrorCode.invalidParams | num (-32603 : Int) => ErrorCode.internalError | num (-32099 : Int) => ErrorCode.serverErrorStart | num (-32000 : Int) => ErrorCode.serverErrorEnd | num (-32002 : Int) => ErrorCode.serverNotInitialized | num (-32001 : Int) => ErrorCode.unknownErrorCode | num (-32800 : Int) => ErrorCode.requestCancelled | num (-32801 : Int) => ErrorCode.contentModified | _ => none⟩ instance : ToJson ErrorCode := ⟨fun e => match e with | ErrorCode.parseError => (-32700 : Int) | ErrorCode.invalidRequest => (-32600 : Int) | ErrorCode.methodNotFound => (-32601 : Int) | ErrorCode.invalidParams => (-32602 : Int) | ErrorCode.internalError => (-32603 : Int) | ErrorCode.serverErrorStart => (-32099 : Int) | ErrorCode.serverErrorEnd => (-32000 : Int) | ErrorCode.serverNotInitialized => (-32002 : Int) | ErrorCode.unknownErrorCode => (-32001 : Int) | ErrorCode.requestCancelled => (-32800 : Int) | ErrorCode.contentModified => (-32801 : Int)⟩ /- Uses separate constructors for notifications and errors because client and server behavior is expected to be wildly different for both. -/ inductive Message | request (id : RequestID) (method : String) (params? : Option Structured) | notification (method : String) (params? : Option Structured) | response (id : RequestID) (result : Json) | responseError (id : RequestID) (code : ErrorCode) (message : String) (data? : Option Json) def Batch := Array Message -- data types for reading expected messages structure Request (α : Type) := (id : RequestID) (param : α) structure Response (α : Type) := (id : RequestID) (result : α) structure Error := (id : RequestID) (code : JsonNumber) (message : String) (data? : Option Json) instance : Coe String RequestID := ⟨RequestID.str⟩ instance : Coe JsonNumber RequestID := ⟨RequestID.num⟩ instance : FromJson RequestID := ⟨fun j => match j with | str s => RequestID.str s | num n => RequestID.num n | _ => none⟩ instance : ToJson RequestID := ⟨fun rid => match rid with | RequestID.str s => s | RequestID.num n => num n | RequestID.null => null⟩ instance : ToJson Message := ⟨fun m => mkObj $ ⟨"jsonrpc", "2.0"⟩ :: match m with | Message.request id method params? => [ ⟨"id", toJson id⟩, ⟨"method", method⟩ ] ++ opt "params" params? | Message.notification method params? => ⟨"method", method⟩ :: opt "params" params? | Message.response id result => [ ⟨"id", toJson id⟩, ⟨"result", result⟩] | Message.responseError id code message data? => [ ⟨"id", toJson id⟩, ⟨"error", mkObj $ [ ⟨"code", toJson code⟩, ⟨"message", message⟩ ] ++ opt "data" data?⟩ ]⟩ def aux1 (j : Json) : Option Message := do let id ← j.getObjValAs? RequestID "id" let method ← j.getObjValAs? String "method" let params? := j.getObjValAs? Structured "params" pure (Message.request id method params?) def aux2 (j : Json) : Option Message := do let method ← j.getObjValAs? String "method" let params? := j.getObjValAs? Structured "params" pure (Message.notification method params?) def aux3 (j : Json) : Option Message := do let id ← j.getObjValAs? RequestID "id" let result ← j.getObjVal? "result" pure (Message.response id result) def aux4 (j : Json) : Option Message := do let id ← j.getObjValAs? RequestID "id" let err ← j.getObjVal? "error" let code ← err.getObjValAs? ErrorCode "code" let message ← err.getObjValAs? String "message" let data? := err.getObjVal? "data"; pure (Message.responseError id code message data?) -- HACK: The implementation must be made up of several `auxN`s instead -- of one large block because of a bug in the compiler. instance : FromJson Message := ⟨fun j => do let "2.0" ← j.getObjVal? "jsonrpc" | none aux1 j <|> aux2 j <|> aux3 j <|> aux4 j⟩ end JsonRpc end Lean namespace IO.FS.Stream open Lean open Lean.JsonRpc open IO def readMessage (h : FS.Stream) (nBytes : Nat) : IO Message := do let j ← h.readJson nBytes match fromJson? j with | some m => pure m | none => throw (userError ("JSON '" ++ j.compress ++ "' did not have the format of a JSON-RPC message")) def readRequestAs (h : FS.Stream) (nBytes : Nat) (expectedMethod : String) (α : Type) [FromJson α] : IO (Request α) := do let m ← h.readMessage nBytes match m with | Message.request id method params? => if method = expectedMethod then match params? with | some params => let j := toJson params match fromJson? j with | some v => pure ⟨id, v⟩ | none => throw (userError ("unexpected param '" ++ j.compress ++ "' for method '" ++ expectedMethod ++ "'")) | none => throw (userError ("unexpected lack of param for method '" ++ expectedMethod ++ "'")) else throw (userError ("expected method '" ++ expectedMethod ++ "', got method '" ++ method ++ "'")) | _ => throw (userError "expected request, got other type of message") def readNotificationAs (h : FS.Stream) (nBytes : Nat) (expectedMethod : String) (α : Type) [FromJson α] : IO α := do let m ← h.readMessage nBytes match m with | Message.notification method params? => if method = expectedMethod then match params? with | some params => let j := toJson params; match fromJson? j with | some v => pure v | none => throw (userError ("unexpected param '" ++ j.compress ++ "' for method '" ++ expectedMethod ++ "'")) | none => throw (userError ("unexpected lack of param for method '" ++ expectedMethod ++ "'")) else throw (userError ("expected method '" ++ expectedMethod ++ "', got method '" ++ method ++ "'")) | _ => throw (userError "expected notification, got other type of message") def writeMessage (h : FS.Stream) (m : Message) : IO Unit := h.writeJson (toJson m) def writeResponse {α} [ToJson α] (h : FS.Stream) (id : RequestID) (r : α) : IO Unit := h.writeMessage (Message.response id (toJson r)) end IO.FS.Stream
b4c6a58436c7bffe0c2392d011c586c17aa92696
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/order/partial_sups.lean
fa4a0a5b400b3f0d71ace76c7af6277cf583d8a5
[ "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,744
lean
/- Copyright (c) 2021 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import data.finset.lattice import order.hom.basic /-! # The monotone sequence of partial supremums of a sequence We define `partial_sups : (ℕ → α) → ℕ →o α` inductively. For `f : ℕ → α`, `partial_sups f` is the sequence `f 0 `, `f 0 ⊔ f 1`, `f 0 ⊔ f 1 ⊔ f 2`, ... The point of this definition is that * it doesn't need a `⨆`, as opposed to `⨆ (i ≤ n), f i`. * it doesn't need a `⊥`, as opposed to `(finset.range (n + 1)).sup f`. * it avoids needing to prove that `finset.range (n + 1)` is nonempty to use `finset.sup'`. Equivalence with those definitions is shown by `partial_sups_eq_bsupr`, `partial_sups_eq_sup_range`, `partial_sups_eq_sup'_range` and respectively. ## Notes One might dispute whether this sequence should start at `f 0` or `⊥`. We choose the former because : * Starting at `⊥` requires... having a bottom element. * `λ f n, (finset.range n).sup f` is already effectively the sequence starting at `⊥`. * If we started at `⊥` we wouldn't have the Galois insertion. See `partial_sups.gi`. ## TODO One could generalize `partial_sups` to any locally finite bot preorder domain, in place of `ℕ`. Necessary for the TODO in the module docstring of `order.disjointed`. -/ variables {α : Type*} section semilattice_sup variables [semilattice_sup α] /-- The monotone sequence whose value at `n` is the supremum of the `f m` where `m ≤ n`. -/ def partial_sups (f : ℕ → α) : ℕ →o α := ⟨@nat.rec (λ _, α) (f 0) (λ (n : ℕ) (a : α), a ⊔ f (n + 1)), monotone_nat_of_le_succ (λ n, le_sup_left)⟩ @[simp] lemma partial_sups_zero (f : ℕ → α) : partial_sups f 0 = f 0 := rfl @[simp] lemma partial_sups_succ (f : ℕ → α) (n : ℕ) : partial_sups f (n + 1) = partial_sups f n ⊔ f (n + 1) := rfl lemma le_partial_sups_of_le (f : ℕ → α) {m n : ℕ} (h : m ≤ n) : f m ≤ partial_sups f n := begin induction n with n ih, { cases h, exact le_rfl, }, { cases h with h h, { exact le_sup_right, }, { exact (ih h).trans le_sup_left, } }, end lemma le_partial_sups (f : ℕ → α) : f ≤ partial_sups f := λ n, le_partial_sups_of_le f le_rfl lemma partial_sups_le (f : ℕ → α) (n : ℕ) (a : α) (w : ∀ m, m ≤ n → f m ≤ a) : partial_sups f n ≤ a := begin induction n with n ih, { apply w 0 le_rfl, }, { exact sup_le (ih (λ m p, w m (nat.le_succ_of_le p))) (w (n + 1) le_rfl) } end lemma monotone.partial_sups_eq {f : ℕ → α} (hf : monotone f) : (partial_sups f : ℕ → α) = f := begin ext n, induction n with n ih, { refl }, { rw [partial_sups_succ, ih, sup_eq_right.2 (hf (nat.le_succ _))] } end lemma partial_sups_mono : monotone (partial_sups : (ℕ → α) → ℕ →o α) := begin rintro f g h n, induction n with n ih, { exact h 0 }, { exact sup_le_sup ih (h _) } end /-- `partial_sups` forms a Galois insertion with the coercion from monotone functions to functions. -/ def partial_sups.gi : galois_insertion (partial_sups : (ℕ → α) → ℕ →o α) coe_fn := { choice := λ f h, ⟨f, begin convert (partial_sups f).monotone, exact (le_partial_sups f).antisymm h, end⟩, gc := λ f g, begin refine ⟨(le_partial_sups f).trans, λ h, _⟩, convert partial_sups_mono h, exact order_hom.ext _ _ g.monotone.partial_sups_eq.symm, end, le_l_u := λ f, le_partial_sups f, choice_eq := λ f h, order_hom.ext _ _ ((le_partial_sups f).antisymm h) } lemma partial_sups_eq_sup'_range (f : ℕ → α) (n : ℕ) : partial_sups f n = (finset.range (n + 1)).sup' ⟨n, finset.self_mem_range_succ n⟩ f := begin induction n with n ih, { simp }, { dsimp [partial_sups] at ih ⊢, simp_rw @finset.range_succ n.succ, rw [ih, finset.sup'_insert, sup_comm] } end end semilattice_sup lemma partial_sups_eq_sup_range [semilattice_sup α] [order_bot α] (f : ℕ → α) (n : ℕ) : partial_sups f n = (finset.range (n + 1)).sup f := begin induction n with n ih, { simp }, { dsimp [partial_sups] at ih ⊢, rw [finset.range_succ, finset.sup_insert, sup_comm, ih] } end /- Note this lemma requires a distributive lattice, so is not useful (or true) in situations such as submodules. -/ lemma partial_sups_disjoint_of_disjoint [distrib_lattice α] [order_bot α] (f : ℕ → α) (h : pairwise (disjoint on f)) {m n : ℕ} (hmn : m < n) : disjoint (partial_sups f m) (f n) := begin induction m with m ih, { exact h hmn.ne, }, { rw [partial_sups_succ, disjoint_sup_left], exact ⟨ih (nat.lt_of_succ_lt hmn), h hmn.ne⟩ } end section complete_lattice variables [complete_lattice α] lemma partial_sups_eq_bsupr (f : ℕ → α) (n : ℕ) : partial_sups f n = ⨆ (i ≤ n), f i := begin rw [partial_sups_eq_sup_range, finset.sup_eq_supr], congr, ext a, exact supr_congr_Prop (by rw [finset.mem_range, nat.lt_succ_iff]) (λ _, rfl), end @[simp] lemma supr_partial_sups_eq (f : ℕ → α) : (⨆ n, partial_sups f n) = ⨆ n, f n := begin refine (supr_le $ λ n, _).antisymm (supr_mono $ le_partial_sups f), rw partial_sups_eq_bsupr, exact supr₂_le_supr _ _, end lemma supr_le_supr_of_partial_sups_le_partial_sups {f g : ℕ → α} (h : partial_sups f ≤ partial_sups g) : (⨆ n, f n) ≤ ⨆ n, g n := begin rw [←supr_partial_sups_eq f, ←supr_partial_sups_eq g], exact supr_mono h, end lemma supr_eq_supr_of_partial_sups_eq_partial_sups {f g : ℕ → α} (h : partial_sups f = partial_sups g) : (⨆ n, f n) = ⨆ n, g n := by simp_rw [←supr_partial_sups_eq f, ←supr_partial_sups_eq g, h] end complete_lattice
59ab21a4bc4e83a82a346e42aeb80a4b5b38f728
bd12a817ba941113eb7fdb7ddf0979d9ed9386a0
/src/order/filter/basic.lean
ba5975f3a605d49acc0868382d2d76357510adf3
[ "Apache-2.0" ]
permissive
flypitch/mathlib
563d9c3356c2885eb6cefaa704d8d86b89b74b15
70cd00bc20ad304f2ac0886b2291b44261787607
refs/heads/master
1,590,167,818,658
1,557,762,121,000
1,557,762,121,000
186,450,076
0
0
Apache-2.0
1,557,762,289,000
1,557,762,288,000
null
UTF-8
Lean
false
false
82,064
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, Jeremy Avigad Theory of filters on sets. -/ import order.galois_connection order.zorn import data.set.finite data.list data.pfun import algebra.pi_instances import category.applicative open lattice set universes u v w x y local attribute [instance] classical.prop_decidable namespace lattice variables {α : Type u} {ι : Sort v} def complete_lattice.copy (c : complete_lattice α) (le : α → α → Prop) (eq_le : le = @complete_lattice.le α c) (top : α) (eq_top : top = @complete_lattice.top α c) (bot : α) (eq_bot : bot = @complete_lattice.bot α c) (sup : α → α → α) (eq_sup : sup = @complete_lattice.sup α c) (inf : α → α → α) (eq_inf : inf = @complete_lattice.inf α c) (Sup : set α → α) (eq_Sup : Sup = @complete_lattice.Sup α c) (Inf : set α → α) (eq_Inf : Inf = @complete_lattice.Inf α c) : complete_lattice α := begin refine { le := le, top := top, bot := bot, sup := sup, inf := inf, Sup := Sup, Inf := Inf, ..}; subst_vars, exact @complete_lattice.le_refl α c, exact @complete_lattice.le_trans α c, exact @complete_lattice.le_antisymm α c, exact @complete_lattice.le_sup_left α c, exact @complete_lattice.le_sup_right α c, exact @complete_lattice.sup_le α c, exact @complete_lattice.inf_le_left α c, exact @complete_lattice.inf_le_right α c, exact @complete_lattice.le_inf α c, exact @complete_lattice.le_top α c, exact @complete_lattice.bot_le α c, exact @complete_lattice.le_Sup α c, exact @complete_lattice.Sup_le α c, exact @complete_lattice.Inf_le α c, exact @complete_lattice.le_Inf α c end end lattice open set lattice section order variables {α : Type u} (r : α → α → Prop) local infix `≼` : 50 := r lemma directed_on_Union {r} {ι : Sort v} {f : ι → set α} (hd : directed (⊆) f) (h : ∀x, directed_on r (f x)) : directed_on r (⋃x, f x) := by simp only [directed_on, exists_prop, mem_Union, exists_imp_distrib]; exact assume a₁ b₁ fb₁ a₂ b₂ fb₂, let ⟨z, zb₁, zb₂⟩ := hd b₁ b₂, ⟨x, xf, xa₁, xa₂⟩ := h z a₁ (zb₁ fb₁) a₂ (zb₂ fb₂) in ⟨x, ⟨z, xf⟩, xa₁, xa₂⟩ end order theorem directed_of_chain {α β r} [is_refl β r] {f : α → β} {c : set α} (h : zorn.chain (f ⁻¹'o r) c) : directed r (λx:{a:α // a ∈ c}, f (x.val)) := assume ⟨a, ha⟩ ⟨b, hb⟩, classical.by_cases (assume : a = b, by simp only [this, exists_prop, and_self, subtype.exists]; exact ⟨b, hb, refl _⟩) (assume : a ≠ b, (h a ha b hb this).elim (λ h : r (f a) (f b), ⟨⟨b, hb⟩, h, refl _⟩) (λ h : r (f b) (f a), ⟨⟨a, ha⟩, refl _, h⟩)) structure filter (α : Type*) := (sets : set (set α)) (univ_sets : set.univ ∈ sets) (sets_of_superset {x y} : x ∈ sets → x ⊆ y → y ∈ sets) (inter_sets {x y} : x ∈ sets → y ∈ sets → x ∩ y ∈ sets) /-- If `F` is a filter on `α`, and `U` a subset of `α` then we can write `U ∈ F` as on paper. -/ @[reducible] instance {α : Type*}: has_mem (set α) (filter α) := ⟨λ U F, U ∈ F.sets⟩ namespace filter variables {α : Type u} {f g : filter α} {s t : set α} lemma filter_eq : ∀{f g : filter α}, f.sets = g.sets → f = g | ⟨a, _, _, _⟩ ⟨._, _, _, _⟩ rfl := rfl lemma filter_eq_iff : f = g ↔ f.sets = g.sets := ⟨congr_arg _, filter_eq⟩ protected lemma ext_iff : f = g ↔ ∀ s, s ∈ f ↔ s ∈ g := by rw [filter_eq_iff, ext_iff] @[extensionality] protected lemma ext : (∀ s, s ∈ f ↔ s ∈ g) → f = g := filter.ext_iff.2 lemma univ_mem_sets : univ ∈ f := f.univ_sets lemma mem_sets_of_superset : ∀{x y : set α}, x ∈ f → x ⊆ y → y ∈ f := f.sets_of_superset lemma inter_mem_sets : ∀{s t}, s ∈ f → t ∈ f → s ∩ t ∈ f := f.inter_sets lemma univ_mem_sets' (h : ∀ a, a ∈ s): s ∈ f := mem_sets_of_superset univ_mem_sets (assume x _, h x) lemma mp_sets (hs : s ∈ f) (h : {x | x ∈ s → x ∈ t} ∈ f) : t ∈ f := mem_sets_of_superset (inter_mem_sets hs h) $ assume x ⟨h₁, h₂⟩, h₂ h₁ lemma congr_sets (h : {x | x ∈ s ↔ x ∈ t} ∈ f) : s ∈ f ↔ t ∈ f := ⟨λ hs, mp_sets hs (mem_sets_of_superset h (λ x, iff.mp)), λ hs, mp_sets hs (mem_sets_of_superset h (λ x, iff.mpr))⟩ lemma Inter_mem_sets {β : Type v} {s : β → set α} {is : set β} (hf : finite is) : (∀i∈is, s i ∈ f) → (⋂i∈is, s i) ∈ f := finite.induction_on hf (assume hs, by simp only [univ_mem_sets, mem_empty_eq, Inter_neg, Inter_univ, not_false_iff]) (assume i is _ hf hi hs, have h₁ : s i ∈ f, from hs i (by simp), have h₂ : (⋂x∈is, s x) ∈ f, from hi $ assume a ha, hs _ $ by simp only [ha, mem_insert_iff, or_true], by simp [inter_mem_sets h₁ h₂]) lemma exists_sets_subset_iff : (∃t ∈ f, t ⊆ s) ↔ s ∈ f := ⟨assume ⟨t, ht, ts⟩, mem_sets_of_superset ht ts, assume hs, ⟨s, hs, subset.refl _⟩⟩ lemma monotone_mem_sets {f : filter α} : monotone (λs, s ∈ f) := assume s t hst h, mem_sets_of_superset h hst end filter namespace tactic.interactive open tactic interactive /-- `filter_upwards [h1, ⋯, hn]` replaces a goal of the form `s ∈ f` and terms `h1 : t1 ∈ f, ⋯, hn : tn ∈ f` with `∀x, x ∈ t1 → ⋯ → x ∈ tn → x ∈ s`. `filter_upwards [h1, ⋯, hn] e` is a short form for `{ filter_upwards [h1, ⋯, hn], exact e }`. -/ meta def filter_upwards (s : parse types.pexpr_list) (e' : parse $ optional types.texpr) : tactic unit := do s.reverse.mmap (λ e, eapplyc `filter.mp_sets >> eapply e), eapplyc `filter.univ_mem_sets', match e' with | some e := interactive.exact e | none := skip end end tactic.interactive namespace filter variables {α : Type u} {β : Type v} {γ : Type w} {ι : Sort x} section principal /-- The principal filter of `s` is the collection of all supersets of `s`. -/ def principal (s : set α) : filter α := { sets := {t | s ⊆ t}, univ_sets := subset_univ s, sets_of_superset := assume x y hx hy, subset.trans hx hy, inter_sets := assume x y, subset_inter } instance : inhabited (filter α) := ⟨principal ∅⟩ @[simp] lemma mem_principal_sets {s t : set α} : s ∈ principal t ↔ t ⊆ s := iff.rfl lemma mem_principal_self (s : set α) : s ∈ principal s := subset.refl _ end principal section join /-- The join of a filter of filters is defined by the relation `s ∈ join f ↔ {t | s ∈ t} ∈ f`. -/ def join (f : filter (filter α)) : filter α := { sets := {s | {t : filter α | s ∈ t} ∈ f}, univ_sets := by simp only [univ_mem_sets, mem_set_of_eq]; exact univ_mem_sets, sets_of_superset := assume x y hx xy, mem_sets_of_superset hx $ assume f h, mem_sets_of_superset h xy, inter_sets := assume x y hx hy, mem_sets_of_superset (inter_mem_sets hx hy) $ assume f ⟨h₁, h₂⟩, inter_mem_sets h₁ h₂ } @[simp] lemma mem_join_sets {s : set α} {f : filter (filter α)} : s ∈ join f ↔ {t | s ∈ filter.sets t} ∈ f := iff.rfl end join section lattice instance : partial_order (filter α) := { le := λf g, g.sets ⊆ f.sets, le_antisymm := assume a b h₁ h₂, filter_eq $ subset.antisymm h₂ h₁, le_refl := assume a, subset.refl _, le_trans := assume a b c h₁ h₂, subset.trans h₂ h₁ } theorem le_def {f g : filter α} : f ≤ g ↔ ∀ x ∈ g, x ∈ f := iff.rfl /-- `generate_sets g s`: `s` is in the filter closure of `g`. -/ inductive generate_sets (g : set (set α)) : set α → Prop | basic {s : set α} : s ∈ g → generate_sets s | univ {} : generate_sets univ | superset {s t : set α} : generate_sets s → s ⊆ t → generate_sets t | inter {s t : set α} : generate_sets s → generate_sets t → generate_sets (s ∩ t) /-- `generate g` is the smallest filter containing the sets `g`. -/ def generate (g : set (set α)) : filter α := { sets := {s | generate_sets g s}, univ_sets := generate_sets.univ, sets_of_superset := assume x y, generate_sets.superset, inter_sets := assume s t, generate_sets.inter } lemma sets_iff_generate {s : set (set α)} {f : filter α} : f ≤ filter.generate s ↔ s ⊆ f.sets := iff.intro (assume h u hu, h $ generate_sets.basic $ hu) (assume h u hu, hu.rec_on h univ_mem_sets (assume x y _ hxy hx, mem_sets_of_superset hx hxy) (assume x y _ _ hx hy, inter_mem_sets hx hy)) protected def mk_of_closure (s : set (set α)) (hs : (generate s).sets = s) : filter α := { sets := s, univ_sets := hs ▸ (univ_mem_sets : univ ∈ generate s), sets_of_superset := assume x y, hs ▸ (mem_sets_of_superset : x ∈ generate s → x ⊆ y → y ∈ generate s), inter_sets := assume x y, hs ▸ (inter_mem_sets : x ∈ generate s → y ∈ generate s → x ∩ y ∈ generate s) } lemma mk_of_closure_sets {s : set (set α)} {hs : (generate s).sets = s} : filter.mk_of_closure s hs = generate s := filter.ext $ assume u, show u ∈ (filter.mk_of_closure s hs).sets ↔ u ∈ (generate s).sets, from hs.symm ▸ iff.refl _ /- Galois insertion from sets of sets into a filters. -/ def gi_generate (α : Type*) : @galois_insertion (set (set α)) (order_dual (filter α)) _ _ filter.generate filter.sets := { gc := assume s f, sets_iff_generate, le_l_u := assume f u, generate_sets.basic, choice := λs hs, filter.mk_of_closure s (le_antisymm hs $ sets_iff_generate.1 $ le_refl _), choice_eq := assume s hs, mk_of_closure_sets } /-- The infimum of filters is the filter generated by intersections of elements of the two filters. -/ instance : has_inf (filter α) := ⟨λf g : filter α, { sets := {s | ∃ (a ∈ f) (b ∈ g), a ∩ b ⊆ s }, univ_sets := ⟨_, univ_mem_sets, _, univ_mem_sets, inter_subset_left _ _⟩, sets_of_superset := assume x y ⟨a, ha, b, hb, h⟩ xy, ⟨a, ha, b, hb, subset.trans h xy⟩, inter_sets := assume x y ⟨a, ha, b, hb, hx⟩ ⟨c, hc, d, hd, hy⟩, ⟨_, inter_mem_sets ha hc, _, inter_mem_sets hb hd, calc a ∩ c ∩ (b ∩ d) = (a ∩ b) ∩ (c ∩ d) : by ac_refl ... ⊆ x ∩ y : inter_subset_inter hx hy⟩ }⟩ @[simp] lemma mem_inf_sets {f g : filter α} {s : set α} : s ∈ f ⊓ g ↔ ∃t₁∈f.sets, ∃t₂∈g.sets, t₁ ∩ t₂ ⊆ s := iff.rfl lemma mem_inf_sets_of_left {f g : filter α} {s : set α} (h : s ∈ f) : s ∈ f ⊓ g := ⟨s, h, univ, univ_mem_sets, inter_subset_left _ _⟩ lemma mem_inf_sets_of_right {f g : filter α} {s : set α} (h : s ∈ g) : s ∈ f ⊓ g := ⟨univ, univ_mem_sets, s, h, inter_subset_right _ _⟩ lemma inter_mem_inf_sets {α : Type u} {f g : filter α} {s t : set α} (hs : s ∈ f) (ht : t ∈ g) : s ∩ t ∈ f ⊓ g := inter_mem_sets (mem_inf_sets_of_left hs) (mem_inf_sets_of_right ht) instance : has_top (filter α) := ⟨{ sets := {s | ∀x, x ∈ s}, univ_sets := assume x, mem_univ x, sets_of_superset := assume x y hx hxy a, hxy (hx a), inter_sets := assume x y hx hy a, mem_inter (hx _) (hy _) }⟩ lemma mem_top_sets_iff_forall {s : set α} : s ∈ (⊤ : filter α) ↔ (∀x, x ∈ s) := iff.refl _ @[simp] lemma mem_top_sets {s : set α} : s ∈ (⊤ : filter α) ↔ s = univ := by rw [mem_top_sets_iff_forall, eq_univ_iff_forall] section complete_lattice /- We lift the complete lattice along the Galois connection `generate` / `sets`. Unfortunately, we want to have different definitional equalities for the lattice operations. So we define them upfront and change the lattice operations for the complete lattice instance. -/ private def original_complete_lattice : complete_lattice (filter α) := @order_dual.lattice.complete_lattice _ (gi_generate α).lift_complete_lattice local attribute [instance] original_complete_lattice instance : complete_lattice (filter α) := original_complete_lattice.copy /- le -/ filter.partial_order.le rfl /- top -/ (filter.lattice.has_top).1 (top_unique $ assume s hs, by have := univ_mem_sets ; finish) /- bot -/ _ rfl /- sup -/ _ rfl /- inf -/ (filter.lattice.has_inf).1 begin ext f g : 2, exact le_antisymm (le_inf (assume s, mem_inf_sets_of_left) (assume s, mem_inf_sets_of_right)) (assume s ⟨a, ha, b, hb, hs⟩, show s ∈ complete_lattice.inf f g, from mem_sets_of_superset (inter_mem_sets (@inf_le_left (filter α) _ _ _ _ ha) (@inf_le_right (filter α) _ _ _ _ hb)) hs) end /- Sup -/ (join ∘ principal) (by ext s x; exact (@mem_bInter_iff _ _ s filter.sets x).symm) /- Inf -/ _ rfl end complete_lattice lemma bot_sets_eq : (⊥ : filter α).sets = univ := rfl lemma sup_sets_eq {f g : filter α} : (f ⊔ g).sets = f.sets ∩ g.sets := (gi_generate α).gc.u_inf lemma Sup_sets_eq {s : set (filter α)} : (Sup s).sets = (⋂f∈s, (f:filter α).sets) := (gi_generate α).gc.u_Inf lemma supr_sets_eq {f : ι → filter α} : (supr f).sets = (⋂i, (f i).sets) := (gi_generate α).gc.u_infi lemma generate_empty : filter.generate ∅ = (⊤ : filter α) := (gi_generate α).gc.l_bot lemma generate_univ : filter.generate univ = (⊥ : filter α) := mk_of_closure_sets.symm lemma generate_union {s t : set (set α)} : filter.generate (s ∪ t) = filter.generate s ⊓ filter.generate t := (gi_generate α).gc.l_sup lemma generate_Union {s : ι → set (set α)} : filter.generate (⋃ i, s i) = (⨅ i, filter.generate (s i)) := (gi_generate α).gc.l_supr @[simp] lemma mem_bot_sets {s : set α} : s ∈ (⊥ : filter α) := trivial @[simp] lemma mem_sup_sets {f g : filter α} {s : set α} : s ∈ f ⊔ g ↔ s ∈ f ∧ s ∈ g := iff.rfl @[simp] lemma mem_Sup_sets {x : set α} {s : set (filter α)} : x ∈ Sup s ↔ (∀f∈s, x ∈ (f:filter α)) := iff.rfl @[simp] lemma mem_supr_sets {x : set α} {f : ι → filter α} : x ∈ supr f ↔ (∀i, x ∈ f i) := by simp only [supr_sets_eq, iff_self, mem_Inter] @[simp] lemma le_principal_iff {s : set α} {f : filter α} : f ≤ principal s ↔ s ∈ f := show (∀{t}, s ⊆ t → t ∈ f) ↔ s ∈ f, from ⟨assume h, h (subset.refl s), assume hs t ht, mem_sets_of_superset hs ht⟩ lemma principal_mono {s t : set α} : principal s ≤ principal t ↔ s ⊆ t := by simp only [le_principal_iff, iff_self, mem_principal_sets] lemma monotone_principal : monotone (principal : set α → filter α) := by simp only [monotone, principal_mono]; exact assume a b h, h @[simp] lemma principal_eq_iff_eq {s t : set α} : principal s = principal t ↔ s = t := by simp only [le_antisymm_iff, le_principal_iff, mem_principal_sets]; refl @[simp] lemma join_principal_eq_Sup {s : set (filter α)} : join (principal s) = Sup s := rfl /- lattice equations -/ lemma empty_in_sets_eq_bot {f : filter α} : ∅ ∈ f ↔ f = ⊥ := ⟨assume h, bot_unique $ assume s _, mem_sets_of_superset h (empty_subset s), assume : f = ⊥, this.symm ▸ mem_bot_sets⟩ lemma inhabited_of_mem_sets {f : filter α} {s : set α} (hf : f ≠ ⊥) (hs : s ∈ f) : ∃x, x ∈ s := have ∅ ∉ f.sets, from assume h, hf $ empty_in_sets_eq_bot.mp h, have s ≠ ∅, from assume h, this (h ▸ hs), exists_mem_of_ne_empty this lemma filter_eq_bot_of_not_nonempty {f : filter α} (ne : ¬ nonempty α) : f = ⊥ := empty_in_sets_eq_bot.mp $ univ_mem_sets' $ assume x, false.elim (ne ⟨x⟩) lemma forall_sets_neq_empty_iff_neq_bot {f : filter α} : (∀ (s : set α), s ∈ f → s ≠ ∅) ↔ f ≠ ⊥ := by simp only [(@empty_in_sets_eq_bot α f).symm, ne.def]; exact ⟨assume h hs, h _ hs rfl, assume h s hs eq, h $ eq ▸ hs⟩ lemma mem_sets_of_neq_bot {f : filter α} {s : set α} (h : f ⊓ principal (-s) = ⊥) : s ∈ f := have ∅ ∈ f ⊓ principal (- s), from h.symm ▸ mem_bot_sets, let ⟨s₁, hs₁, s₂, (hs₂ : -s ⊆ s₂), (hs : s₁ ∩ s₂ ⊆ ∅)⟩ := this in by filter_upwards [hs₁] assume a ha, classical.by_contradiction $ assume ha', hs ⟨ha, hs₂ ha'⟩ lemma infi_sets_eq {f : ι → filter α} (h : directed (≥) f) (ne : nonempty ι) : (infi f).sets = (⋃ i, (f i).sets) := let ⟨i⟩ := ne, u := { filter . sets := (⋃ i, (f i).sets), univ_sets := by simp only [mem_Union]; exact ⟨i, univ_mem_sets⟩, sets_of_superset := by simp only [mem_Union, exists_imp_distrib]; intros x y i hx hxy; exact ⟨i, mem_sets_of_superset hx hxy⟩, inter_sets := begin simp only [mem_Union, exists_imp_distrib], assume x y a hx b hy, rcases h a b with ⟨c, ha, hb⟩, exact ⟨c, inter_mem_sets (ha hx) (hb hy)⟩ end } in subset.antisymm (show u ≤ infi f, from le_infi $ assume i, le_supr (λi, (f i).sets) i) (Union_subset $ assume i, infi_le f i) lemma mem_infi {f : ι → filter α} (h : directed (≥) f) (ne : nonempty ι) (s): s ∈ infi f ↔ s ∈ ⋃ i, (f i).sets := show s ∈ (infi f).sets ↔ s ∈ ⋃ i, (f i).sets, by rw infi_sets_eq h ne lemma infi_sets_eq' {f : β → filter α} {s : set β} (h : directed_on (f ⁻¹'o (≥)) s) (ne : ∃i, i ∈ s) : (⨅ i∈s, f i).sets = (⋃ i ∈ s, (f i).sets) := let ⟨i, hi⟩ := ne in calc (⨅ i ∈ s, f i).sets = (⨅ t : {t // t ∈ s}, (f t.val)).sets : by rw [infi_subtype]; refl ... = (⨆ t : {t // t ∈ s}, (f t.val).sets) : infi_sets_eq (assume ⟨x, hx⟩ ⟨y, hy⟩, match h x hx y hy with ⟨z, h₁, h₂, h₃⟩ := ⟨⟨z, h₁⟩, h₂, h₃⟩ end) ⟨⟨i, hi⟩⟩ ... = (⨆ t ∈ {t | t ∈ s}, (f t).sets) : by rw [supr_subtype]; refl lemma infi_sets_eq_finite (f : ι → filter α) : (⨅i, f i).sets = (⋃t:finset (plift ι), (⨅i∈t, f (plift.down i)).sets) := begin rw [infi_eq_infi_finset, infi_sets_eq], exact (directed_of_sup $ λs₁ s₂ hs, infi_le_infi $ λi, infi_le_infi_const $ λh, hs h), apply_instance end lemma mem_infi_finite {f : ι → filter α} (s): s ∈ infi f ↔ s ∈ ⋃t:finset (plift ι), (⨅i∈t, f (plift.down i)).sets := show s ∈ (infi f).sets ↔ s ∈ ⋃t:finset (plift ι), (⨅i∈t, f (plift.down i)).sets, by rw infi_sets_eq_finite @[simp] lemma sup_join {f₁ f₂ : filter (filter α)} : (join f₁ ⊔ join f₂) = join (f₁ ⊔ f₂) := filter_eq $ set.ext $ assume x, by simp only [supr_sets_eq, join, mem_sup_sets, iff_self, mem_set_of_eq] @[simp] lemma supr_join {ι : Sort w} {f : ι → filter (filter α)} : (⨆x, join (f x)) = join (⨆x, f x) := filter_eq $ set.ext $ assume x, by simp only [supr_sets_eq, join, iff_self, mem_Inter, mem_set_of_eq] instance : bounded_distrib_lattice (filter α) := { le_sup_inf := begin assume x y z s, simp only [and_assoc, mem_inf_sets, mem_sup_sets, exists_prop, exists_imp_distrib, and_imp], intros hs t₁ ht₁ t₂ ht₂ hts, exact ⟨s ∪ t₁, x.sets_of_superset hs $ subset_union_left _ _, y.sets_of_superset ht₁ $ subset_union_right _ _, s ∪ t₂, x.sets_of_superset hs $ subset_union_left _ _, z.sets_of_superset ht₂ $ subset_union_right _ _, subset.trans (@le_sup_inf (set α) _ _ _ _) (union_subset (subset.refl _) hts)⟩ end, ..filter.lattice.complete_lattice } /- the complementary version with ⨆i, f ⊓ g i does not hold! -/ lemma infi_sup_eq {f : filter α} {g : ι → filter α} : (⨅ x, f ⊔ g x) = f ⊔ infi g := begin refine le_antisymm _ (le_infi $ assume i, sup_le_sup (le_refl f) $ infi_le _ _), rintros t ⟨h₁, h₂⟩, rw [infi_sets_eq_finite] at h₂, simp only [mem_Union, (finset.inf_eq_infi _ _).symm] at h₂, rcases h₂ with ⟨s, hs⟩, suffices : (⨅i, f ⊔ g i) ≤ f ⊔ s.inf (λi, g i.down), { exact this ⟨h₁, hs⟩ }, refine finset.induction_on s _ _, { exact le_sup_right_of_le le_top }, { rintros ⟨i⟩ s his ih, rw [finset.inf_insert, sup_inf_left], exact le_inf (infi_le _ _) ih } end lemma mem_infi_sets_finset {s : finset α} {f : α → filter β} : ∀t, t ∈ (⨅a∈s, f a) ↔ (∃p:α → set β, (∀a∈s, p a ∈ f a) ∧ (⋂a∈s, p a) ⊆ t) := show ∀t, t ∈ (⨅a∈s, f a) ↔ (∃p:α → set β, (∀a∈s, p a ∈ f a) ∧ (⨅a∈s, p a) ≤ t), begin simp only [(finset.inf_eq_infi _ _).symm], refine finset.induction_on s _ _, { simp only [finset.not_mem_empty, false_implies_iff, finset.inf_empty, top_le_iff, imp_true_iff, mem_top_sets, true_and, exists_const], intros; refl }, { intros a s has ih t, simp only [ih, finset.forall_mem_insert, finset.inf_insert, mem_inf_sets, exists_prop, iff_iff_implies_and_implies, exists_imp_distrib, and_imp, and_assoc] {contextual := tt}, split, { intros t₁ ht₁ t₂ p hp ht₂ ht, existsi function.update p a t₁, have : ∀a'∈s, function.update p a t₁ a' = p a', from assume a' ha', have a' ≠ a, from assume h, has $ h ▸ ha', function.update_noteq this, have eq : s.inf (λj, function.update p a t₁ j) = s.inf (λj, p j) := finset.inf_congr rfl this, simp only [this, ht₁, hp, function.update_same, true_and, imp_true_iff, eq] {contextual := tt}, exact subset.trans (inter_subset_inter (subset.refl _) ht₂) ht }, assume p hpa hp ht, exact ⟨p a, hpa, (s.inf p), ⟨⟨p, hp, le_refl _⟩, ht⟩⟩ } end /- principal equations -/ @[simp] lemma inf_principal {s t : set α} : principal s ⊓ principal t = principal (s ∩ t) := le_antisymm (by simp; exact ⟨s, subset.refl s, t, subset.refl t, by simp⟩) (by simp [le_inf_iff, inter_subset_left, inter_subset_right]) @[simp] lemma sup_principal {s t : set α} : principal s ⊔ principal t = principal (s ∪ t) := filter_eq $ set.ext $ by simp only [union_subset_iff, union_subset_iff, mem_sup_sets, forall_const, iff_self, mem_principal_sets] @[simp] lemma supr_principal {ι : Sort w} {s : ι → set α} : (⨆x, principal (s x)) = principal (⋃i, s i) := filter_eq $ set.ext $ assume x, by simp only [supr_sets_eq, mem_principal_sets, mem_Inter]; exact (@supr_le_iff (set α) _ _ _ _).symm lemma principal_univ : principal (univ : set α) = ⊤ := top_unique $ by simp only [le_principal_iff, mem_top_sets, eq_self_iff_true] lemma principal_empty : principal (∅ : set α) = ⊥ := bot_unique $ assume s _, empty_subset _ @[simp] lemma principal_eq_bot_iff {s : set α} : principal s = ⊥ ↔ s = ∅ := ⟨assume h, principal_eq_iff_eq.mp $ by simp only [principal_empty, h, eq_self_iff_true], assume h, by simp only [h, principal_empty, eq_self_iff_true]⟩ lemma inf_principal_eq_bot {f : filter α} {s : set α} (hs : -s ∈ f) : f ⊓ principal s = ⊥ := empty_in_sets_eq_bot.mp ⟨_, hs, s, mem_principal_self s, assume x ⟨h₁, h₂⟩, h₁ h₂⟩ theorem mem_inf_principal (f : filter α) (s t : set α) : s ∈ f ⊓ principal t ↔ { x | x ∈ t → x ∈ s } ∈ f := begin simp only [mem_inf_sets, mem_principal_sets, exists_prop], split, { rintros ⟨u, ul, v, tsubv, uvinter⟩, apply filter.mem_sets_of_superset ul, intros x xu xt, exact uvinter ⟨xu, tsubv xt⟩ }, intro h, refine ⟨_, h, t, set.subset.refl t, _⟩, rintros x ⟨hx, xt⟩, exact hx xt end end lattice section map /-- The forward map of a filter -/ def map (m : α → β) (f : filter α) : filter β := { sets := preimage m ⁻¹' f.sets, univ_sets := univ_mem_sets, sets_of_superset := assume s t hs st, mem_sets_of_superset hs $ preimage_mono st, inter_sets := assume s t hs ht, inter_mem_sets hs ht } @[simp] lemma map_principal {s : set α} {f : α → β} : map f (principal s) = principal (set.image f s) := filter_eq $ set.ext $ assume a, image_subset_iff.symm variables {f : filter α} {m : α → β} {m' : β → γ} {s : set α} {t : set β} @[simp] lemma mem_map : t ∈ map m f ↔ {x | m x ∈ t} ∈ f := iff.rfl lemma image_mem_map (hs : s ∈ f) : m '' s ∈ map m f := f.sets_of_superset hs $ subset_preimage_image m s lemma range_mem_map : range m ∈ map m f := by rw ←image_univ; exact image_mem_map univ_mem_sets lemma mem_map_sets_iff : t ∈ map m f ↔ (∃s∈f, m '' s ⊆ t) := iff.intro (assume ht, ⟨set.preimage m t, ht, image_preimage_subset _ _⟩) (assume ⟨s, hs, ht⟩, mem_sets_of_superset (image_mem_map hs) ht) @[simp] lemma map_id : filter.map id f = f := filter_eq $ rfl @[simp] lemma map_compose : filter.map m' ∘ filter.map m = filter.map (m' ∘ m) := funext $ assume _, filter_eq $ rfl @[simp] lemma map_map : filter.map m' (filter.map m f) = filter.map (m' ∘ m) f := congr_fun (@@filter.map_compose m m') f end map section comap /-- The inverse map of a filter -/ def comap (m : α → β) (f : filter β) : filter α := { sets := { s | ∃t∈ f, m ⁻¹' t ⊆ s }, univ_sets := ⟨univ, univ_mem_sets, by simp only [subset_univ, preimage_univ]⟩, sets_of_superset := assume a b ⟨a', ha', ma'a⟩ ab, ⟨a', ha', subset.trans ma'a ab⟩, inter_sets := assume a b ⟨a', ha₁, ha₂⟩ ⟨b', hb₁, hb₂⟩, ⟨a' ∩ b', inter_mem_sets ha₁ hb₁, inter_subset_inter ha₂ hb₂⟩ } end comap /-- The cofinite filter is the filter of subsets whose complements are finite. -/ def cofinite : filter α := { sets := {s | finite (- s)}, univ_sets := by simp only [compl_univ, finite_empty, mem_set_of_eq], sets_of_superset := assume s t (hs : finite (-s)) (st: s ⊆ t), finite_subset hs $ @lattice.neg_le_neg (set α) _ _ _ st, inter_sets := assume s t (hs : finite (-s)) (ht : finite (-t)), by simp only [compl_inter, finite_union, ht, hs, mem_set_of_eq] } lemma cofinite_ne_bot (hi : set.infinite (@set.univ α)) : @cofinite α ≠ ⊥ := forall_sets_neq_empty_iff_neq_bot.mp $ λ s hs hn, by change set.finite _ at hs; rw [hn, set.compl_empty] at hs; exact hi hs /-- The monadic bind operation on filter is defined the usual way in terms of `map` and `join`. Unfortunately, this `bind` does not result in the expected applicative. See `filter.seq` for the applicative instance. -/ def bind (f : filter α) (m : α → filter β) : filter β := join (map m f) /-- The applicative sequentiation operation. This is not induced by the bind operation. -/ def seq (f : filter (α → β)) (g : filter α) : filter β := ⟨{ s | ∃u∈ f, ∃t∈ g, (∀m∈u, ∀x∈t, (m : α → β) x ∈ s) }, ⟨univ, univ_mem_sets, univ, univ_mem_sets, by simp only [forall_prop_of_true, mem_univ, forall_true_iff]⟩, assume s₀ s₁ ⟨t₀, t₁, h₀, h₁, h⟩ hst, ⟨t₀, t₁, h₀, h₁, assume x hx y hy, hst $ h _ hx _ hy⟩, assume s₀ s₁ ⟨t₀, ht₀, t₁, ht₁, ht⟩ ⟨u₀, hu₀, u₁, hu₁, hu⟩, ⟨t₀ ∩ u₀, inter_mem_sets ht₀ hu₀, t₁ ∩ u₁, inter_mem_sets ht₁ hu₁, assume x ⟨hx₀, hx₁⟩ x ⟨hy₀, hy₁⟩, ⟨ht _ hx₀ _ hy₀, hu _ hx₁ _ hy₁⟩⟩⟩ instance : has_pure filter := ⟨λ(α : Type u) x, principal {x}⟩ instance : has_bind filter := ⟨@filter.bind⟩ instance : has_seq filter := ⟨@filter.seq⟩ instance : functor filter := { map := @filter.map } section -- this section needs to be before applicative, otherwise the wrong instance will be chosen protected def monad : monad filter := { map := @filter.map } local attribute [instance] filter.monad protected def is_lawful_monad : is_lawful_monad filter := { id_map := assume α f, filter_eq rfl, pure_bind := assume α β a f, by simp only [bind, Sup_image, image_singleton, join_principal_eq_Sup, lattice.Sup_singleton, map_principal, eq_self_iff_true], bind_assoc := assume α β γ f m₁ m₂, filter_eq rfl, bind_pure_comp_eq_map := assume α β f x, filter_eq $ by simp only [bind, join, map, preimage, principal, set.subset_univ, eq_self_iff_true, function.comp_app, mem_set_of_eq, singleton_subset_iff] } end instance : applicative filter := { map := @filter.map, seq := @filter.seq } instance : alternative filter := { failure := λα, ⊥, orelse := λα x y, x ⊔ y } @[simp] lemma pure_def (x : α) : pure x = principal {x} := rfl @[simp] lemma mem_pure {a : α} {s : set α} : a ∈ s → s ∈ (pure a : filter α) := by simp only [imp_self, pure_def, mem_principal_sets, singleton_subset_iff]; exact id @[simp] lemma mem_pure_iff {a : α} {s : set α} : s ∈ (pure a : filter α) ↔ a ∈ s := by rw [pure_def, mem_principal_sets, set.singleton_subset_iff] @[simp] lemma map_def {α β} (m : α → β) (f : filter α) : m <$> f = map m f := rfl @[simp] lemma bind_def {α β} (f : filter α) (m : α → filter β) : f >>= m = bind f m := rfl /- map and comap equations -/ section map variables {f f₁ f₂ : filter α} {g g₁ g₂ : filter β} {m : α → β} {m' : β → γ} {s : set α} {t : set β} @[simp] theorem mem_comap_sets : s ∈ comap m g ↔ ∃t∈ g, m ⁻¹' t ⊆ s := iff.rfl theorem preimage_mem_comap (ht : t ∈ g) : m ⁻¹' t ∈ comap m g := ⟨t, ht, subset.refl _⟩ lemma comap_id : comap id f = f := le_antisymm (assume s, preimage_mem_comap) (assume s ⟨t, ht, hst⟩, mem_sets_of_superset ht hst) lemma comap_comap_comp {m : γ → β} {n : β → α} : comap m (comap n f) = comap (n ∘ m) f := le_antisymm (assume c ⟨b, hb, (h : preimage (n ∘ m) b ⊆ c)⟩, ⟨preimage n b, preimage_mem_comap hb, h⟩) (assume c ⟨b, ⟨a, ha, (h₁ : preimage n a ⊆ b)⟩, (h₂ : preimage m b ⊆ c)⟩, ⟨a, ha, show preimage m (preimage n a) ⊆ c, from subset.trans (preimage_mono h₁) h₂⟩) @[simp] theorem comap_principal {t : set β} : comap m (principal t) = principal (m ⁻¹' t) := filter_eq $ set.ext $ assume s, ⟨assume ⟨u, (hu : t ⊆ u), (b : preimage m u ⊆ s)⟩, subset.trans (preimage_mono hu) b, assume : preimage m t ⊆ s, ⟨t, subset.refl t, this⟩⟩ lemma map_le_iff_le_comap : map m f ≤ g ↔ f ≤ comap m g := ⟨assume h s ⟨t, ht, hts⟩, mem_sets_of_superset (h ht) hts, assume h s ht, h ⟨_, ht, subset.refl _⟩⟩ lemma gc_map_comap (m : α → β) : galois_connection (map m) (comap m) := assume f g, map_le_iff_le_comap lemma map_mono (h : f₁ ≤ f₂) : map m f₁ ≤ map m f₂ := (gc_map_comap m).monotone_l h lemma monotone_map : monotone (map m) | a b := map_mono lemma comap_mono (h : g₁ ≤ g₂) : comap m g₁ ≤ comap m g₂ := (gc_map_comap m).monotone_u h lemma monotone_comap : monotone (comap m) | a b := comap_mono @[simp] lemma map_bot : map m ⊥ = ⊥ := (gc_map_comap m).l_bot @[simp] lemma map_sup : map m (f₁ ⊔ f₂) = map m f₁ ⊔ map m f₂ := (gc_map_comap m).l_sup @[simp] lemma map_supr {f : ι → filter α} : map m (⨆i, f i) = (⨆i, map m (f i)) := (gc_map_comap m).l_supr @[simp] lemma comap_top : comap m ⊤ = ⊤ := (gc_map_comap m).u_top @[simp] lemma comap_inf : comap m (g₁ ⊓ g₂) = comap m g₁ ⊓ comap m g₂ := (gc_map_comap m).u_inf @[simp] lemma comap_infi {f : ι → filter β} : comap m (⨅i, f i) = (⨅i, comap m (f i)) := (gc_map_comap m).u_infi lemma le_comap_top (f : α → β) (l : filter α) : l ≤ comap f ⊤ := by rw [comap_top]; exact le_top lemma map_comap_le : map m (comap m g) ≤ g := (gc_map_comap m).l_u_le _ lemma le_comap_map : f ≤ comap m (map m f) := (gc_map_comap m).le_u_l _ @[simp] lemma comap_bot : comap m ⊥ = ⊥ := bot_unique $ assume s _, ⟨∅, by simp only [mem_bot_sets], by simp only [empty_subset, preimage_empty]⟩ lemma comap_supr {ι} {f : ι → filter β} {m : α → β} : comap m (supr f) = (⨆i, comap m (f i)) := le_antisymm (assume s hs, have ∀i, ∃t, t ∈ f i ∧ m ⁻¹' t ⊆ s, by simpa only [mem_comap_sets, exists_prop, mem_supr_sets] using mem_supr_sets.1 hs, let ⟨t, ht⟩ := classical.axiom_of_choice this in ⟨⋃i, t i, mem_supr_sets.2 $ assume i, (f i).sets_of_superset (ht i).1 (subset_Union _ _), begin rw [preimage_Union, Union_subset_iff], assume i, exact (ht i).2 end⟩) (supr_le $ assume i, monotone_comap $ le_supr _ _) lemma comap_Sup {s : set (filter β)} {m : α → β} : comap m (Sup s) = (⨆f∈s, comap m f) := by simp only [Sup_eq_supr, comap_supr, eq_self_iff_true] lemma comap_sup : comap m (g₁ ⊔ g₂) = comap m g₁ ⊔ comap m g₂ := le_antisymm (assume s ⟨⟨t₁, ht₁, hs₁⟩, ⟨t₂, ht₂, hs₂⟩⟩, ⟨t₁ ∪ t₂, ⟨g₁.sets_of_superset ht₁ (subset_union_left _ _), g₂.sets_of_superset ht₂ (subset_union_right _ _)⟩, union_subset hs₁ hs₂⟩) (sup_le (comap_mono le_sup_left) (comap_mono le_sup_right)) lemma map_comap {f : filter β} {m : α → β} (hf : range m ∈ f) : (f.comap m).map m = f := le_antisymm map_comap_le (assume t' ⟨t, ht, sub⟩, by filter_upwards [ht, hf]; rintros x hxt ⟨y, rfl⟩; exact sub hxt) lemma comap_map {f : filter α} {m : α → β} (h : ∀ x y, m x = m y → x = y) : comap m (map m f) = f := have ∀s, preimage m (image m s) = s, from assume s, preimage_image_eq s h, le_antisymm (assume s hs, ⟨ image m s, f.sets_of_superset hs $ by simp only [this, subset.refl], by simp only [this, subset.refl]⟩) le_comap_map lemma le_of_map_le_map_inj' {f g : filter α} {m : α → β} {s : set α} (hsf : s ∈ f) (hsg : s ∈ g) (hm : ∀x∈s, ∀y∈s, m x = m y → x = y) (h : map m f ≤ map m g) : f ≤ g := assume t ht, by filter_upwards [hsf, h $ image_mem_map (inter_mem_sets hsg ht)] assume a has ⟨b, ⟨hbs, hb⟩, h⟩, have b = a, from hm _ hbs _ has h, this ▸ hb lemma le_of_map_le_map_inj_iff {f g : filter α} {m : α → β} {s : set α} (hsf : s ∈ f) (hsg : s ∈ g) (hm : ∀x∈s, ∀y∈s, m x = m y → x = y) : map m f ≤ map m g ↔ f ≤ g := iff.intro (le_of_map_le_map_inj' hsf hsg hm) map_mono lemma eq_of_map_eq_map_inj' {f g : filter α} {m : α → β} {s : set α} (hsf : s ∈ f) (hsg : s ∈ g) (hm : ∀x∈s, ∀y∈s, m x = m y → x = y) (h : map m f = map m g) : f = g := le_antisymm (le_of_map_le_map_inj' hsf hsg hm $ le_of_eq h) (le_of_map_le_map_inj' hsg hsf hm $ le_of_eq h.symm) lemma map_inj {f g : filter α} {m : α → β} (hm : ∀ x y, m x = m y → x = y) (h : map m f = map m g) : f = g := have comap m (map m f) = comap m (map m g), by rw h, by rwa [comap_map hm, comap_map hm] at this theorem le_map_comap_of_surjective' {f : α → β} {l : filter β} {u : set β} (ul : u ∈ l) (hf : ∀ y ∈ u, ∃ x, f x = y) : l ≤ map f (comap f l) := assume s ⟨t, tl, ht⟩, have t ∩ u ⊆ s, from assume x ⟨xt, xu⟩, exists.elim (hf x xu) $ λ a faeq, by { rw ←faeq, apply ht, change f a ∈ t, rw faeq, exact xt }, mem_sets_of_superset (inter_mem_sets tl ul) this theorem map_comap_of_surjective' {f : α → β} {l : filter β} {u : set β} (ul : u ∈ l) (hf : ∀ y ∈ u, ∃ x, f x = y) : map f (comap f l) = l := le_antisymm map_comap_le (le_map_comap_of_surjective' ul hf) theorem le_map_comap_of_surjective {f : α → β} (hf : function.surjective f) (l : filter β) : l ≤ map f (comap f l) := le_map_comap_of_surjective' univ_mem_sets (λ y _, hf y) theorem map_comap_of_surjective {f : α → β} (hf : function.surjective f) (l : filter β) : map f (comap f l) = l := le_antisymm map_comap_le (le_map_comap_of_surjective hf l) lemma comap_neq_bot {f : filter β} {m : α → β} (hm : ∀t∈ f, ∃a, m a ∈ t) : comap m f ≠ ⊥ := forall_sets_neq_empty_iff_neq_bot.mp $ assume s ⟨t, ht, t_s⟩, let ⟨a, (ha : a ∈ preimage m t)⟩ := hm t ht in neq_bot_of_le_neq_bot (ne_empty_of_mem ha) t_s lemma comap_neq_bot_of_surj {f : filter β} {m : α → β} (hf : f ≠ ⊥) (hm : ∀b, ∃a, m a = b) : comap m f ≠ ⊥ := comap_neq_bot $ assume t ht, let ⟨b, (hx : b ∈ t)⟩ := inhabited_of_mem_sets hf ht, ⟨a, (ha : m a = b)⟩ := hm b in ⟨a, ha.symm ▸ hx⟩ @[simp] lemma map_eq_bot_iff : map m f = ⊥ ↔ f = ⊥ := ⟨by rw [←empty_in_sets_eq_bot, ←empty_in_sets_eq_bot]; exact id, assume h, by simp only [h, eq_self_iff_true, map_bot]⟩ lemma map_ne_bot (hf : f ≠ ⊥) : map m f ≠ ⊥ := assume h, hf $ by rwa [map_eq_bot_iff] at h lemma sInter_comap_sets (f : α → β) (F : filter β) : ⋂₀(comap f F).sets = ⋂ U ∈ F, f ⁻¹' U := begin ext x, suffices : (∀ (A : set α) (B : set β), B ∈ F → f ⁻¹' B ⊆ A → x ∈ A) ↔ ∀ (B : set β), B ∈ F → f x ∈ B, by simp only [mem_sInter, mem_Inter, mem_comap_sets, this, and_imp, mem_comap_sets, exists_prop, mem_sInter, iff_self, mem_Inter, mem_preimage_eq, exists_imp_distrib], split, { intros h U U_in, simpa only [set.subset.refl, forall_prop_of_true, mem_preimage_eq] using h (f ⁻¹' U) U U_in }, { intros h V U U_in f_U_V, exact f_U_V (h U U_in) }, end end map lemma map_cong {m₁ m₂ : α → β} {f : filter α} (h : {x | m₁ x = m₂ x} ∈ f) : map m₁ f = map m₂ f := have ∀(m₁ m₂ : α → β) (h : {x | m₁ x = m₂ x} ∈ f), map m₁ f ≤ map m₂ f, begin intros m₁ m₂ h s hs, show {x | m₁ x ∈ s} ∈ f, filter_upwards [h, hs], simp only [subset_def, mem_preimage_eq, mem_set_of_eq, forall_true_iff] {contextual := tt} end, le_antisymm (this m₁ m₂ h) (this m₂ m₁ $ mem_sets_of_superset h $ assume x, eq.symm) -- this is a generic rule for monotone functions: lemma map_infi_le {f : ι → filter α} {m : α → β} : map m (infi f) ≤ (⨅ i, map m (f i)) := le_infi $ assume i, map_mono $ infi_le _ _ lemma map_infi_eq {f : ι → filter α} {m : α → β} (hf : directed (≥) f) (hι : nonempty ι) : map m (infi f) = (⨅ i, map m (f i)) := le_antisymm map_infi_le (assume s (hs : preimage m s ∈ infi f), have ∃i, preimage m s ∈ f i, by simp only [infi_sets_eq hf hι, mem_Union] at hs; assumption, let ⟨i, hi⟩ := this in have (⨅ i, map m (f i)) ≤ principal s, from infi_le_of_le i $ by simp only [le_principal_iff, mem_map]; assumption, by simp only [filter.le_principal_iff] at this; assumption) lemma map_binfi_eq {ι : Type w} {f : ι → filter α} {m : α → β} {p : ι → Prop} (h : directed_on (f ⁻¹'o (≥)) {x | p x}) (ne : ∃i, p i) : map m (⨅i (h : p i), f i) = (⨅i (h: p i), map m (f i)) := let ⟨i, hi⟩ := ne in calc map m (⨅i (h : p i), f i) = map m (⨅i:subtype p, f i.val) : by simp only [infi_subtype, eq_self_iff_true] ... = (⨅i:subtype p, map m (f i.val)) : map_infi_eq (assume ⟨x, hx⟩ ⟨y, hy⟩, match h x hx y hy with ⟨z, h₁, h₂, h₃⟩ := ⟨⟨z, h₁⟩, h₂, h₃⟩ end) ⟨⟨i, hi⟩⟩ ... = (⨅i (h : p i), map m (f i)) : by simp only [infi_subtype, eq_self_iff_true] lemma map_inf' {f g : filter α} {m : α → β} {t : set α} (htf : t ∈ f) (htg : t ∈ g) (h : ∀x∈t, ∀y∈t, m x = m y → x = y) : map m (f ⊓ g) = map m f ⊓ map m g := begin refine le_antisymm (le_inf (map_mono inf_le_left) (map_mono inf_le_right)) (assume s hs, _), simp only [map, mem_inf_sets, exists_prop, mem_map, mem_preimage_eq, mem_inf_sets] at hs ⊢, rcases hs with ⟨t₁, h₁, t₂, h₂, hs⟩, refine ⟨m '' (t₁ ∩ t), _, m '' (t₂ ∩ t), _, _⟩, { filter_upwards [h₁, htf] assume a h₁ h₂, mem_image_of_mem _ ⟨h₁, h₂⟩ }, { filter_upwards [h₂, htg] assume a h₁ h₂, mem_image_of_mem _ ⟨h₁, h₂⟩ }, { rw [image_inter_on], { refine image_subset_iff.2 _, exact λ x ⟨⟨h₁, _⟩, h₂, _⟩, hs ⟨h₁, h₂⟩ }, { exact λ x ⟨_, hx⟩ y ⟨_, hy⟩, h x hx y hy } } end lemma map_inf {f g : filter α} {m : α → β} (h : ∀ x y, m x = m y → x = y) : map m (f ⊓ g) = map m f ⊓ map m g := map_inf' univ_mem_sets univ_mem_sets (assume x _ y _, h x y) lemma map_eq_comap_of_inverse {f : filter α} {m : α → β} {n : β → α} (h₁ : m ∘ n = id) (h₂ : n ∘ m = id) : map m f = comap n f := le_antisymm (assume b ⟨a, ha, (h : preimage n a ⊆ b)⟩, f.sets_of_superset ha $ calc a = preimage (n ∘ m) a : by simp only [h₂, preimage_id, eq_self_iff_true] ... ⊆ preimage m b : preimage_mono h) (assume b (hb : preimage m b ∈ f), ⟨preimage m b, hb, show preimage (m ∘ n) b ⊆ b, by simp only [h₁]; apply subset.refl⟩) lemma map_swap_eq_comap_swap {f : filter (α × β)} : prod.swap <$> f = comap prod.swap f := map_eq_comap_of_inverse prod.swap_swap_eq prod.swap_swap_eq lemma le_map {f : filter α} {m : α → β} {g : filter β} (h : ∀s∈ f, m '' s ∈ g) : g ≤ f.map m := assume s hs, mem_sets_of_superset (h _ hs) $ image_preimage_subset _ _ section applicative @[simp] lemma mem_pure_sets {a : α} {s : set α} : s ∈ (pure a : filter α) ↔ a ∈ s := by simp only [iff_self, pure_def, mem_principal_sets, singleton_subset_iff] lemma singleton_mem_pure_sets {a : α} : {a} ∈ (pure a : filter α) := by simp only [mem_singleton, pure_def, mem_principal_sets, singleton_subset_iff] @[simp] lemma pure_neq_bot {α : Type u} {a : α} : pure a ≠ (⊥ : filter α) := by simp only [pure, has_pure.pure, ne.def, not_false_iff, singleton_ne_empty, principal_eq_bot_iff] lemma mem_seq_sets_def {f : filter (α → β)} {g : filter α} {s : set β} : s ∈ f.seq g ↔ (∃u ∈ f, ∃t ∈ g, ∀x∈u, ∀y∈t, (x : α → β) y ∈ s) := iff.refl _ lemma mem_seq_sets_iff {f : filter (α → β)} {g : filter α} {s : set β} : s ∈ f.seq g ↔ (∃u ∈ f, ∃t ∈ g, set.seq u t ⊆ s) := by simp only [mem_seq_sets_def, seq_subset, exists_prop, iff_self] lemma mem_map_seq_iff {f : filter α} {g : filter β} {m : α → β → γ} {s : set γ} : s ∈ (f.map m).seq g ↔ (∃t u, t ∈ g ∧ u ∈ f ∧ ∀x∈u, ∀y∈t, m x y ∈ s) := iff.intro (assume ⟨t, ht, s, hs, hts⟩, ⟨s, m ⁻¹' t, hs, ht, assume a, hts _⟩) (assume ⟨t, s, ht, hs, hts⟩, ⟨m '' s, image_mem_map hs, t, ht, assume f ⟨a, has, eq⟩, eq ▸ hts _ has⟩) lemma seq_mem_seq_sets {f : filter (α → β)} {g : filter α} {s : set (α → β)} {t : set α} (hs : s ∈ f) (ht : t ∈ g): s.seq t ∈ f.seq g := ⟨s, hs, t, ht, assume f hf a ha, ⟨f, hf, a, ha, rfl⟩⟩ lemma le_seq {f : filter (α → β)} {g : filter α} {h : filter β} (hh : ∀t ∈ f, ∀u ∈ g, set.seq t u ∈ h) : h ≤ seq f g := assume s ⟨t, ht, u, hu, hs⟩, mem_sets_of_superset (hh _ ht _ hu) $ assume b ⟨m, hm, a, ha, eq⟩, eq ▸ hs _ hm _ ha lemma seq_mono {f₁ f₂ : filter (α → β)} {g₁ g₂ : filter α} (hf : f₁ ≤ f₂) (hg : g₁ ≤ g₂) : f₁.seq g₁ ≤ f₂.seq g₂ := le_seq $ assume s hs t ht, seq_mem_seq_sets (hf hs) (hg ht) @[simp] lemma pure_seq_eq_map (g : α → β) (f : filter α) : seq (pure g) f = f.map g := begin refine le_antisymm (le_map $ assume s hs, _) (le_seq $ assume s hs t ht, _), { rw ← singleton_seq, apply seq_mem_seq_sets _ hs, simp only [mem_singleton, pure_def, mem_principal_sets, singleton_subset_iff] }, { rw mem_pure_sets at hs, refine sets_of_superset (map g f) (image_mem_map ht) _, rintros b ⟨a, ha, rfl⟩, exact ⟨g, hs, a, ha, rfl⟩ } end @[simp] lemma map_pure (f : α → β) (a : α) : map f (pure a) = pure (f a) := le_antisymm (le_principal_iff.2 $ sets_of_superset (map f (pure a)) (image_mem_map singleton_mem_pure_sets) $ by simp only [image_singleton, mem_singleton, singleton_subset_iff]) (le_map $ assume s, begin simp only [mem_image, pure_def, mem_principal_sets, singleton_subset_iff], exact assume has, ⟨a, has, rfl⟩ end) @[simp] lemma seq_pure (f : filter (α → β)) (a : α) : seq f (pure a) = map (λg:α → β, g a) f := begin refine le_antisymm (le_map $ assume s hs, _) (le_seq $ assume s hs t ht, _), { rw ← seq_singleton, exact seq_mem_seq_sets hs (by simp only [mem_singleton, pure_def, mem_principal_sets, singleton_subset_iff]) }, { rw mem_pure_sets at ht, refine sets_of_superset (map (λg:α→β, g a) f) (image_mem_map hs) _, rintros b ⟨g, hg, rfl⟩, exact ⟨g, hg, a, ht, rfl⟩ } end @[simp] lemma seq_assoc (x : filter α) (g : filter (α → β)) (h : filter (β → γ)) : seq h (seq g x) = seq (seq (map (∘) h) g) x := begin refine le_antisymm (le_seq $ assume s hs t ht, _) (le_seq $ assume s hs t ht, _), { rcases mem_seq_sets_iff.1 hs with ⟨u, hu, v, hv, hs⟩, rcases mem_map_sets_iff.1 hu with ⟨w, hw, hu⟩, refine mem_sets_of_superset _ (set.seq_mono (subset.trans (set.seq_mono hu (subset.refl _)) hs) (subset.refl _)), rw ← set.seq_seq, exact seq_mem_seq_sets hw (seq_mem_seq_sets hv ht) }, { rcases mem_seq_sets_iff.1 ht with ⟨u, hu, v, hv, ht⟩, refine mem_sets_of_superset _ (set.seq_mono (subset.refl _) ht), rw set.seq_seq, exact seq_mem_seq_sets (seq_mem_seq_sets (image_mem_map hs) hu) hv } end lemma prod_map_seq_comm (f : filter α) (g : filter β) : (map prod.mk f).seq g = seq (map (λb a, (a, b)) g) f := begin refine le_antisymm (le_seq $ assume s hs t ht, _) (le_seq $ assume s hs t ht, _), { rcases mem_map_sets_iff.1 hs with ⟨u, hu, hs⟩, refine mem_sets_of_superset _ (set.seq_mono hs (subset.refl _)), rw ← set.prod_image_seq_comm, exact seq_mem_seq_sets (image_mem_map ht) hu }, { rcases mem_map_sets_iff.1 hs with ⟨u, hu, hs⟩, refine mem_sets_of_superset _ (set.seq_mono hs (subset.refl _)), rw set.prod_image_seq_comm, exact seq_mem_seq_sets (image_mem_map ht) hu } end instance : is_lawful_functor (filter : Type u → Type u) := { id_map := assume α f, map_id, comp_map := assume α β γ f g a, map_map.symm } instance : is_lawful_applicative (filter : Type u → Type u) := { pure_seq_eq_map := assume α β, pure_seq_eq_map, map_pure := assume α β, map_pure, seq_pure := assume α β, seq_pure, seq_assoc := assume α β γ, seq_assoc } instance : is_comm_applicative (filter : Type u → Type u) := ⟨assume α β f g, prod_map_seq_comm f g⟩ lemma {l} seq_eq_filter_seq {α β : Type l} (f : filter (α → β)) (g : filter α) : f <*> g = seq f g := rfl end applicative /- bind equations -/ section bind @[simp] lemma mem_bind_sets {s : set β} {f : filter α} {m : α → filter β} : s ∈ bind f m ↔ ∃t ∈ f, ∀x ∈ t, s ∈ m x := calc s ∈ bind f m ↔ {a | s ∈ m a} ∈ f : by simp only [bind, mem_map, iff_self, mem_join_sets, mem_set_of_eq] ... ↔ (∃t ∈ f, t ⊆ {a | s ∈ m a}) : exists_sets_subset_iff.symm ... ↔ (∃t ∈ f, ∀x ∈ t, s ∈ m x) : iff.refl _ lemma bind_mono {f : filter α} {g h : α → filter β} (h₁ : {a | g a ≤ h a} ∈ f) : bind f g ≤ bind f h := assume x h₂, show (_ ∈ f), by filter_upwards [h₁, h₂] assume s gh' h', gh' h' lemma bind_sup {f g : filter α} {h : α → filter β} : bind (f ⊔ g) h = bind f h ⊔ bind g h := by simp only [bind, sup_join, map_sup, eq_self_iff_true] lemma bind_mono2 {f g : filter α} {h : α → filter β} (h₁ : f ≤ g) : bind f h ≤ bind g h := assume s h', h₁ h' lemma principal_bind {s : set α} {f : α → filter β} : (bind (principal s) f) = (⨆x ∈ s, f x) := show join (map f (principal s)) = (⨆x ∈ s, f x), by simp only [Sup_image, join_principal_eq_Sup, map_principal, eq_self_iff_true] end bind lemma infi_neq_bot_of_directed {f : ι → filter α} (hn : nonempty α) (hd : directed (≥) f) (hb : ∀i, f i ≠ ⊥) : (infi f) ≠ ⊥ := let ⟨x⟩ := hn in assume h, have he: ∅ ∈ (infi f), from h.symm ▸ (mem_bot_sets : ∅ ∈ (⊥ : filter α)), classical.by_cases (assume : nonempty ι, have ∃i, ∅ ∈ f i, by rw [mem_infi hd this] at he; simp only [mem_Union] at he; assumption, let ⟨i, hi⟩ := this in hb i $ bot_unique $ assume s _, (f i).sets_of_superset hi $ empty_subset _) (assume : ¬ nonempty ι, have univ ⊆ (∅ : set α), begin rw [←principal_mono, principal_univ, principal_empty, ←h], exact (le_infi $ assume i, false.elim $ this ⟨i⟩) end, this $ mem_univ x) lemma infi_neq_bot_iff_of_directed {f : ι → filter α} (hn : nonempty α) (hd : directed (≥) f) : (infi f) ≠ ⊥ ↔ (∀i, f i ≠ ⊥) := ⟨assume neq_bot i eq_bot, neq_bot $ bot_unique $ infi_le_of_le i $ eq_bot ▸ le_refl _, infi_neq_bot_of_directed hn hd⟩ lemma mem_infi_sets {f : ι → filter α} (i : ι) : ∀{s}, s ∈ f i → s ∈ ⨅i, f i := show (⨅i, f i) ≤ f i, from infi_le _ _ @[elab_as_eliminator] lemma infi_sets_induct {f : ι → filter α} {s : set α} (hs : s ∈ infi f) {p : set α → Prop} (uni : p univ) (ins : ∀{i s₁ s₂}, s₁ ∈ f i → p s₂ → p (s₁ ∩ s₂)) (upw : ∀{s₁ s₂}, s₁ ⊆ s₂ → p s₁ → p s₂) : p s := begin rw [mem_infi_finite] at hs, simp only [mem_Union, (finset.inf_eq_infi _ _).symm] at hs, rcases hs with ⟨is, his⟩, revert s, refine finset.induction_on is _ _, { assume s hs, rwa [mem_top_sets.1 hs] }, { rintros ⟨i⟩ js his ih s hs, rw [finset.inf_insert, mem_inf_sets] at hs, rcases hs with ⟨s₁, hs₁, s₂, hs₂, hs⟩, exact upw hs (ins hs₁ (ih hs₂)) } end /- tendsto -/ /-- `tendsto` is the generic "limit of a function" predicate. `tendsto f l₁ l₂` asserts that for every `l₂` neighborhood `a`, the `f`-preimage of `a` is an `l₁` neighborhood. -/ def tendsto (f : α → β) (l₁ : filter α) (l₂ : filter β) := l₁.map f ≤ l₂ lemma tendsto_def {f : α → β} {l₁ : filter α} {l₂ : filter β} : tendsto f l₁ l₂ ↔ ∀ s ∈ l₂, f ⁻¹' s ∈ l₁ := iff.rfl lemma tendsto_iff_comap {f : α → β} {l₁ : filter α} {l₂ : filter β} : tendsto f l₁ l₂ ↔ l₁ ≤ l₂.comap f := map_le_iff_le_comap lemma tendsto.congr' {f₁ f₂ : α → β} {l₁ : filter α} {l₂ : filter β} (hl : {x | f₁ x = f₂ x} ∈ l₁) (h : tendsto f₁ l₁ l₂) : tendsto f₂ l₁ l₂ := by rwa [tendsto, ←map_cong hl] theorem tendsto.congr'r {f₁ f₂ : α → β} {l₁ : filter α} {l₂ : filter β} (h : ∀ x, f₁ x = f₂ x) : tendsto f₁ l₁ l₂ ↔ tendsto f₂ l₁ l₂ := iff_of_eq (by congr'; exact funext h) theorem tendsto.congr {f₁ f₂ : α → β} {l₁ : filter α} {l₂ : filter β} (h : ∀ x, f₁ x = f₂ x) : tendsto f₁ l₁ l₂ → tendsto f₂ l₁ l₂ := (tendsto.congr'r h).1 lemma tendsto_id' {x y : filter α} : x ≤ y → tendsto id x y := by simp only [tendsto, map_id, forall_true_iff] {contextual := tt} lemma tendsto_id {x : filter α} : tendsto id x x := tendsto_id' $ le_refl x lemma tendsto.comp {f : α → β} {g : β → γ} {x : filter α} {y : filter β} {z : filter γ} (hf : tendsto f x y) (hg : tendsto g y z) : tendsto (g ∘ f) x z := calc map (g ∘ f) x = map g (map f x) : by rw [map_map] ... ≤ map g y : map_mono hf ... ≤ z : hg lemma tendsto_le_left {f : α → β} {x y : filter α} {z : filter β} (h : y ≤ x) : tendsto f x z → tendsto f y z := le_trans (map_mono h) lemma tendsto_le_right {f : α → β} {x : filter α} {y z : filter β} (h₁ : y ≤ z) (h₂ : tendsto f x y) : tendsto f x z := le_trans h₂ h₁ lemma tendsto_map {f : α → β} {x : filter α} : tendsto f x (map f x) := le_refl (map f x) lemma tendsto_map' {f : β → γ} {g : α → β} {x : filter α} {y : filter γ} (h : tendsto (f ∘ g) x y) : tendsto f (map g x) y := by rwa [tendsto, map_map] lemma tendsto_map'_iff {f : β → γ} {g : α → β} {x : filter α} {y : filter γ} : tendsto f (map g x) y ↔ tendsto (f ∘ g) x y := by rw [tendsto, map_map]; refl lemma tendsto_comap {f : α → β} {x : filter β} : tendsto f (comap f x) x := map_comap_le lemma tendsto_comap_iff {f : α → β} {g : β → γ} {a : filter α} {c : filter γ} : tendsto f a (c.comap g) ↔ tendsto (g ∘ f) a c := ⟨assume h, h.comp tendsto_comap, assume h, map_le_iff_le_comap.mp $ by rwa [map_map]⟩ lemma tendsto_comap'_iff {m : α → β} {f : filter α} {g : filter β} {i : γ → α} (h : range i ∈ f) : tendsto (m ∘ i) (comap i f) g ↔ tendsto m f g := by rw [tendsto, ← map_compose]; simp only [(∘), map_comap h, tendsto] lemma comap_eq_of_inverse {f : filter α} {g : filter β} {φ : α → β} (ψ : β → α) (eq : ψ ∘ φ = id) (hφ : tendsto φ f g) (hψ : tendsto ψ g f) : comap φ g = f := begin refine le_antisymm (le_trans (comap_mono $ map_le_iff_le_comap.1 hψ) _) (map_le_iff_le_comap.1 hφ), rw [comap_comap_comp, eq, comap_id], exact le_refl _ end lemma map_eq_of_inverse {f : filter α} {g : filter β} {φ : α → β} (ψ : β → α) (eq : φ ∘ ψ = id) (hφ : tendsto φ f g) (hψ : tendsto ψ g f) : map φ f = g := begin refine le_antisymm hφ (le_trans _ (map_mono hψ)), rw [map_map, eq, map_id], exact le_refl _ end lemma tendsto_inf {f : α → β} {x : filter α} {y₁ y₂ : filter β} : tendsto f x (y₁ ⊓ y₂) ↔ tendsto f x y₁ ∧ tendsto f x y₂ := by simp only [tendsto, lattice.le_inf_iff, iff_self] lemma tendsto_inf_left {f : α → β} {x₁ x₂ : filter α} {y : filter β} (h : tendsto f x₁ y) : tendsto f (x₁ ⊓ x₂) y := le_trans (map_mono inf_le_left) h lemma tendsto_inf_right {f : α → β} {x₁ x₂ : filter α} {y : filter β} (h : tendsto f x₂ y) : tendsto f (x₁ ⊓ x₂) y := le_trans (map_mono inf_le_right) h lemma tendsto_infi {f : α → β} {x : filter α} {y : ι → filter β} : tendsto f x (⨅i, y i) ↔ ∀i, tendsto f x (y i) := by simp only [tendsto, iff_self, lattice.le_infi_iff] lemma tendsto_infi' {f : α → β} {x : ι → filter α} {y : filter β} (i : ι) : tendsto f (x i) y → tendsto f (⨅i, x i) y := tendsto_le_left (infi_le _ _) lemma tendsto_principal {f : α → β} {a : filter α} {s : set β} : tendsto f a (principal s) ↔ {a | f a ∈ s} ∈ a := by simp only [tendsto, le_principal_iff, mem_map, iff_self] lemma tendsto_principal_principal {f : α → β} {s : set α} {t : set β} : tendsto f (principal s) (principal t) ↔ ∀a∈s, f a ∈ t := by simp only [tendsto, image_subset_iff, le_principal_iff, map_principal, mem_principal_sets]; refl lemma tendsto_pure_pure (f : α → β) (a : α) : tendsto f (pure a) (pure (f a)) := show filter.map f (pure a) ≤ pure (f a), by rw [filter.map_pure]; exact le_refl _ lemma tendsto_const_pure {a : filter α} {b : β} : tendsto (λa, b) a (pure b) := by simp [tendsto]; exact univ_mem_sets lemma tendsto_if {l₁ : filter α} {l₂ : filter β} {f g : α → β} {p : α → Prop} [decidable_pred p] (h₀ : tendsto f (l₁ ⊓ principal p) l₂) (h₁ : tendsto g (l₁ ⊓ principal { x | ¬ p x }) l₂) : tendsto (λ x, if p x then f x else g x) l₁ l₂ := begin revert h₀ h₁, simp only [tendsto_def, mem_inf_principal], intros h₀ h₁ s hs, apply mem_sets_of_superset (inter_mem_sets (h₀ s hs) (h₁ s hs)), rintros x ⟨hp₀, hp₁⟩, dsimp, by_cases h : p x, { rw if_pos h, exact hp₀ h }, rw if_neg h, exact hp₁ h end section prod variables {s : set α} {t : set β} {f : filter α} {g : filter β} /- The product filter cannot be defined using the monad structure on filters. For example: F := do {x <- seq, y <- top, return (x, y)} hence: s ∈ F <-> ∃n, [n..∞] × univ ⊆ s G := do {y <- top, x <- seq, return (x, y)} hence: s ∈ G <-> ∀i:ℕ, ∃n, [n..∞] × {i} ⊆ s Now ⋃i, [i..∞] × {i} is in G but not in F. As product filter we want to have F as result. -/ /-- Product of filters. This is the filter generated by cartesian products of elements of the component filters. -/ protected def prod (f : filter α) (g : filter β) : filter (α × β) := f.comap prod.fst ⊓ g.comap prod.snd lemma prod_mem_prod {s : set α} {t : set β} {f : filter α} {g : filter β} (hs : s ∈ f) (ht : t ∈ g) : set.prod s t ∈ filter.prod f g := inter_mem_inf_sets (preimage_mem_comap hs) (preimage_mem_comap ht) lemma mem_prod_iff {s : set (α×β)} {f : filter α} {g : filter β} : s ∈ filter.prod f g ↔ (∃ t₁ ∈ f, ∃ t₂ ∈ g, set.prod t₁ t₂ ⊆ s) := begin simp only [filter.prod], split, exact assume ⟨t₁, ⟨s₁, hs₁, hts₁⟩, t₂, ⟨s₂, hs₂, hts₂⟩, h⟩, ⟨s₁, hs₁, s₂, hs₂, subset.trans (inter_subset_inter hts₁ hts₂) h⟩, exact assume ⟨t₁, ht₁, t₂, ht₂, h⟩, ⟨prod.fst ⁻¹' t₁, ⟨t₁, ht₁, subset.refl _⟩, prod.snd ⁻¹' t₂, ⟨t₂, ht₂, subset.refl _⟩, h⟩ end lemma tendsto_fst {f : filter α} {g : filter β} : tendsto prod.fst (filter.prod f g) f := tendsto_inf_left tendsto_comap lemma tendsto_snd {f : filter α} {g : filter β} : tendsto prod.snd (filter.prod f g) g := tendsto_inf_right tendsto_comap lemma tendsto.prod_mk {f : filter α} {g : filter β} {h : filter γ} {m₁ : α → β} {m₂ : α → γ} (h₁ : tendsto m₁ f g) (h₂ : tendsto m₂ f h) : tendsto (λx, (m₁ x, m₂ x)) f (filter.prod g h) := tendsto_inf.2 ⟨tendsto_comap_iff.2 h₁, tendsto_comap_iff.2 h₂⟩ lemma prod_infi_left {f : ι → filter α} {g : filter β} (i : ι) : filter.prod (⨅i, f i) g = (⨅i, filter.prod (f i) g) := by rw [filter.prod, comap_infi, infi_inf i]; simp only [filter.prod, eq_self_iff_true] lemma prod_infi_right {f : filter α} {g : ι → filter β} (i : ι) : filter.prod f (⨅i, g i) = (⨅i, filter.prod f (g i)) := by rw [filter.prod, comap_infi, inf_infi i]; simp only [filter.prod, eq_self_iff_true] lemma prod_mono {f₁ f₂ : filter α} {g₁ g₂ : filter β} (hf : f₁ ≤ f₂) (hg : g₁ ≤ g₂) : filter.prod f₁ g₁ ≤ filter.prod f₂ g₂ := inf_le_inf (comap_mono hf) (comap_mono hg) lemma prod_comap_comap_eq {α₁ : Type u} {α₂ : Type v} {β₁ : Type w} {β₂ : Type x} {f₁ : filter α₁} {f₂ : filter α₂} {m₁ : β₁ → α₁} {m₂ : β₂ → α₂} : filter.prod (comap m₁ f₁) (comap m₂ f₂) = comap (λp:β₁×β₂, (m₁ p.1, m₂ p.2)) (filter.prod f₁ f₂) := by simp only [filter.prod, comap_comap_comp, eq_self_iff_true, comap_inf] lemma prod_comm' : filter.prod f g = comap (prod.swap) (filter.prod g f) := by simp only [filter.prod, comap_comap_comp, (∘), inf_comm, prod.fst_swap, eq_self_iff_true, prod.snd_swap, comap_inf] lemma prod_comm : filter.prod f g = map (λp:β×α, (p.2, p.1)) (filter.prod g f) := by rw [prod_comm', ← map_swap_eq_comap_swap]; refl lemma prod_map_map_eq {α₁ : Type u} {α₂ : Type v} {β₁ : Type w} {β₂ : Type x} {f₁ : filter α₁} {f₂ : filter α₂} {m₁ : α₁ → β₁} {m₂ : α₂ → β₂} : filter.prod (map m₁ f₁) (map m₂ f₂) = map (λp:α₁×α₂, (m₁ p.1, m₂ p.2)) (filter.prod f₁ f₂) := le_antisymm (assume s hs, let ⟨s₁, hs₁, s₂, hs₂, h⟩ := mem_prod_iff.mp hs in filter.sets_of_superset _ (prod_mem_prod (image_mem_map hs₁) (image_mem_map hs₂)) $ calc set.prod (m₁ '' s₁) (m₂ '' s₂) = (λp:α₁×α₂, (m₁ p.1, m₂ p.2)) '' set.prod s₁ s₂ : set.prod_image_image_eq ... ⊆ _ : by rwa [image_subset_iff]) ((tendsto_fst.comp (le_refl _)).prod_mk (tendsto_snd.comp (le_refl _))) lemma map_prod (m : α × β → γ) (f : filter α) (g : filter β) : map m (f.prod g) = (f.map (λa b, m (a, b))).seq g := begin simp [filter.ext_iff, mem_prod_iff, mem_map_seq_iff], assume s, split, exact assume ⟨t, ht, s, hs, h⟩, ⟨s, hs, t, ht, assume x hx y hy, @h ⟨x, y⟩ ⟨hx, hy⟩⟩, exact assume ⟨s, hs, t, ht, h⟩, ⟨t, ht, s, hs, assume ⟨x, y⟩ ⟨hx, hy⟩, h x hx y hy⟩ end lemma prod_eq {f : filter α} {g : filter β} : f.prod g = (f.map prod.mk).seq g := have h : _ := map_prod id f g, by rwa [map_id] at h lemma prod_inf_prod {f₁ f₂ : filter α} {g₁ g₂ : filter β} : filter.prod f₁ g₁ ⊓ filter.prod f₂ g₂ = filter.prod (f₁ ⊓ f₂) (g₁ ⊓ g₂) := by simp only [filter.prod, comap_inf, inf_comm, inf_assoc, lattice.inf_left_comm] @[simp] lemma prod_bot {f : filter α} : filter.prod f (⊥ : filter β) = ⊥ := by simp [filter.prod] @[simp] lemma bot_prod {g : filter β} : filter.prod (⊥ : filter α) g = ⊥ := by simp [filter.prod] @[simp] lemma prod_principal_principal {s : set α} {t : set β} : filter.prod (principal s) (principal t) = principal (set.prod s t) := by simp only [filter.prod, comap_principal, principal_eq_iff_eq, comap_principal, inf_principal]; refl @[simp] lemma prod_pure_pure {a : α} {b : β} : filter.prod (pure a) (pure b) = pure (a, b) := by simp lemma prod_eq_bot {f : filter α} {g : filter β} : filter.prod f g = ⊥ ↔ (f = ⊥ ∨ g = ⊥) := begin split, { assume h, rcases mem_prod_iff.1 (empty_in_sets_eq_bot.2 h) with ⟨s, hs, t, ht, hst⟩, rw [subset_empty_iff, set.prod_eq_empty_iff] at hst, cases hst with s_eq t_eq, { left, exact empty_in_sets_eq_bot.1 (s_eq ▸ hs) }, { right, exact empty_in_sets_eq_bot.1 (t_eq ▸ ht) } }, { rintros (rfl | rfl), exact bot_prod, exact prod_bot } end lemma prod_neq_bot {f : filter α} {g : filter β} : filter.prod f g ≠ ⊥ ↔ (f ≠ ⊥ ∧ g ≠ ⊥) := by rw [(≠), prod_eq_bot, not_or_distrib] lemma tendsto_prod_iff {f : α × β → γ} {x : filter α} {y : filter β} {z : filter γ} : filter.tendsto f (filter.prod x y) z ↔ ∀ W ∈ z, ∃ U ∈ x, ∃ V ∈ y, ∀ x y, x ∈ U → y ∈ V → f (x, y) ∈ W := by simp only [tendsto_def, mem_prod_iff, prod_sub_preimage_iff, exists_prop, iff_self] end prod /- at_top and at_bot -/ /-- `at_top` is the filter representing the limit `→ ∞` on an ordered set. It is generated by the collection of up-sets `{b | a ≤ b}`. (The preorder need not have a top element for this to be well defined, and indeed is trivial when a top element exists.) -/ def at_top [preorder α] : filter α := ⨅ a, principal {b | a ≤ b} /-- `at_bot` is the filter representing the limit `→ -∞` on an ordered set. It is generated by the collection of down-sets `{b | b ≤ a}`. (The preorder need not have a bottom element for this to be well defined, and indeed is trivial when a bottom element exists.) -/ def at_bot [preorder α] : filter α := ⨅ a, principal {b | b ≤ a} lemma mem_at_top [preorder α] (a : α) : {b : α | a ≤ b} ∈ @at_top α _ := mem_infi_sets a $ subset.refl _ @[simp] lemma at_top_ne_bot [nonempty α] [semilattice_sup α] : (at_top : filter α) ≠ ⊥ := infi_neq_bot_of_directed (by apply_instance) (assume a b, ⟨a ⊔ b, by simp only [ge, le_principal_iff, forall_const, set_of_subset_set_of, mem_principal_sets, and_self, sup_le_iff, forall_true_iff] {contextual := tt}⟩) (assume a, by simp only [principal_eq_bot_iff, ne.def, principal_eq_bot_iff]; exact ne_empty_of_mem (le_refl a)) @[simp] lemma mem_at_top_sets [nonempty α] [semilattice_sup α] {s : set α} : s ∈ (at_top : filter α) ↔ ∃a:α, ∀b≥a, b ∈ s := let ⟨a⟩ := ‹nonempty α› in iff.intro (assume h, infi_sets_induct h ⟨a, by simp only [forall_const, mem_univ, forall_true_iff]⟩ (assume a s₁ s₂ ha ⟨b, hb⟩, ⟨a ⊔ b, assume c hc, ⟨ha $ le_trans le_sup_left hc, hb _ $ le_trans le_sup_right hc⟩⟩) (assume s₁ s₂ h ⟨a, ha⟩, ⟨a, assume b hb, h $ ha _ hb⟩)) (assume ⟨a, h⟩, mem_infi_sets a $ assume x, h x) lemma map_at_top_eq [nonempty α] [semilattice_sup α] {f : α → β} : at_top.map f = (⨅a, principal $ f '' {a' | a ≤ a'}) := calc map f (⨅a, principal {a' | a ≤ a'}) = (⨅a, map f $ principal {a' | a ≤ a'}) : map_infi_eq (assume a b, ⟨a ⊔ b, by simp only [ge, le_principal_iff, forall_const, set_of_subset_set_of, mem_principal_sets, and_self, sup_le_iff, forall_true_iff] {contextual := tt}⟩) (by apply_instance) ... = (⨅a, principal $ f '' {a' | a ≤ a'}) : by simp only [map_principal, eq_self_iff_true] lemma tendsto_at_top [preorder β] (m : α → β) (f : filter α) : tendsto m f at_top ↔ (∀b, {a | b ≤ m a} ∈ f) := by simp only [at_top, tendsto_infi, tendsto_principal]; refl lemma tendsto_at_top' [nonempty α] [semilattice_sup α] (f : α → β) (l : filter β) : tendsto f at_top l ↔ (∀s ∈ l, ∃a, ∀b≥a, f b ∈ s) := by simp only [tendsto_def, mem_at_top_sets]; refl theorem tendsto_at_top_principal [nonempty β] [semilattice_sup β] {f : β → α} {s : set α} : tendsto f at_top (principal s) ↔ ∃N, ∀n≥N, f n ∈ s := by rw [tendsto_iff_comap, comap_principal, le_principal_iff, mem_at_top_sets]; refl /-- A function `f` grows to infinity independent of an order-preserving embedding `e`. -/ lemma tendsto_at_top_embedding {α β γ : Type*} [preorder β] [preorder γ] {f : α → β} {e : β → γ} {l : filter α} (hm : ∀b₁ b₂, e b₁ ≤ e b₂ ↔ b₁ ≤ b₂) (hu : ∀c, ∃b, c ≤ e b) : tendsto (e ∘ f) l at_top ↔ tendsto f l at_top := begin rw [tendsto_at_top, tendsto_at_top], split, { assume hc b, filter_upwards [hc (e b)] assume a, (hm b (f a)).1 }, { assume hb c, rcases hu c with ⟨b, hc⟩, filter_upwards [hb b] assume a ha, le_trans hc ((hm b (f a)).2 ha) } end lemma tendsto_at_top_at_top [nonempty α] [semilattice_sup α] [preorder β] (f : α → β) : tendsto f at_top at_top ↔ ∀ b : β, ∃ i : α, ∀ a : α, i ≤ a → b ≤ f a := iff.trans tendsto_infi $ forall_congr $ assume b, tendsto_at_top_principal lemma tendsto_at_top_at_bot [nonempty α] [decidable_linear_order α] [preorder β] (f : α → β) : tendsto f at_top at_bot ↔ ∀ (b : β), ∃ (i : α), ∀ (a : α), i ≤ a → b ≥ f a := @tendsto_at_top_at_top α (order_dual β) _ _ _ f lemma tendsto_finset_image_at_top_at_top {i : β → γ} {j : γ → β} (h : ∀x, j (i x) = x) : tendsto (λs:finset γ, s.image j) at_top at_top := tendsto_infi.2 $ assume s, tendsto_infi' (s.image i) $ tendsto_principal_principal.2 $ assume t (ht : s.image i ⊆ t), calc s = (s.image i).image j : by simp only [finset.image_image, (∘), h]; exact finset.image_id.symm ... ⊆ t.image j : finset.image_subset_image ht lemma prod_at_top_at_top_eq {β₁ β₂ : Type*} [inhabited β₁] [inhabited β₂] [semilattice_sup β₁] [semilattice_sup β₂] : filter.prod (@at_top β₁ _) (@at_top β₂ _) = @at_top (β₁ × β₂) _ := by simp [at_top, prod_infi_left (default β₁), prod_infi_right (default β₂), infi_prod]; exact infi_comm lemma prod_map_at_top_eq {α₁ α₂ β₁ β₂ : Type*} [inhabited β₁] [inhabited β₂] [semilattice_sup β₁] [semilattice_sup β₂] (u₁ : β₁ → α₁) (u₂ : β₂ → α₂) : filter.prod (map u₁ at_top) (map u₂ at_top) = map (prod.map u₁ u₂) at_top := by rw [prod_map_map_eq, prod_at_top_at_top_eq, prod.map_def] /-- A function `f` maps upwards closed sets (at_top sets) to upwards closed sets when it is a Galois insertion. The Galois "insertion" and "connection" is weakened to only require it to be an insertion and a connetion above `b'`. -/ lemma map_at_top_eq_of_gc [semilattice_sup α] [semilattice_sup β] {f : α → β} (g : β → α) (b' : β)(hf : monotone f) (gc : ∀a, ∀b≥b', f a ≤ b ↔ a ≤ g b) (hgi : ∀b≥b', b ≤ f (g b)) : map f at_top = at_top := begin rw [@map_at_top_eq α _ ⟨g b'⟩], refine le_antisymm (le_infi $ assume b, infi_le_of_le (g (b ⊔ b')) $ principal_mono.2 $ image_subset_iff.2 _) (le_infi $ assume a, infi_le_of_le (f a ⊔ b') $ principal_mono.2 _), { assume a ha, exact (le_trans le_sup_left $ le_trans (hgi _ le_sup_right) $ hf ha) }, { assume b hb, have hb' : b' ≤ b := le_trans le_sup_right hb, exact ⟨g b, (gc _ _ hb').1 (le_trans le_sup_left hb), le_antisymm ((gc _ _ hb').2 (le_refl _)) (hgi _ hb')⟩ } end lemma map_add_at_top_eq_nat (k : ℕ) : map (λa, a + k) at_top = at_top := map_at_top_eq_of_gc (λa, a - k) k (assume a b h, add_le_add_right h k) (assume a b h, (nat.le_sub_right_iff_add_le h).symm) (assume a h, by rw [nat.sub_add_cancel h]) lemma map_sub_at_top_eq_nat (k : ℕ) : map (λa, a - k) at_top = at_top := map_at_top_eq_of_gc (λa, a + k) 0 (assume a b h, nat.sub_le_sub_right h _) (assume a b _, nat.sub_le_right_iff_le_add) (assume b _, by rw [nat.add_sub_cancel]) lemma tendso_add_at_top_nat (k : ℕ) : tendsto (λa, a + k) at_top at_top := le_of_eq (map_add_at_top_eq_nat k) lemma tendso_sub_at_top_nat (k : ℕ) : tendsto (λa, a - k) at_top at_top := le_of_eq (map_sub_at_top_eq_nat k) lemma tendsto_add_at_top_iff_nat {f : ℕ → α} {l : filter α} (k : ℕ) : tendsto (λn, f (n + k)) at_top l ↔ tendsto f at_top l := show tendsto (f ∘ (λn, n + k)) at_top l ↔ tendsto f at_top l, by rw [← tendsto_map'_iff, map_add_at_top_eq_nat] lemma map_div_at_top_eq_nat (k : ℕ) (hk : k > 0) : map (λa, a / k) at_top = at_top := map_at_top_eq_of_gc (λb, b * k + (k - 1)) 1 (assume a b h, nat.div_le_div_right h) (assume a b _, calc a / k ≤ b ↔ a / k < b + 1 : by rw [← nat.succ_eq_add_one, nat.lt_succ_iff] ... ↔ a < (b + 1) * k : nat.div_lt_iff_lt_mul _ _ hk ... ↔ _ : begin cases k, exact (lt_irrefl _ hk).elim, simp [mul_add, add_mul, nat.succ_add, nat.lt_succ_iff] end) (assume b _, calc b = (b * k) / k : by rw [nat.mul_div_cancel b hk] ... ≤ (b * k + (k - 1)) / k : nat.div_le_div_right $ nat.le_add_right _ _) /- ultrafilter -/ section ultrafilter open zorn variables {f g : filter α} /-- An ultrafilter is a minimal (maximal in the set order) proper filter. -/ def is_ultrafilter (f : filter α) := f ≠ ⊥ ∧ ∀g, g ≠ ⊥ → g ≤ f → f ≤ g lemma ultrafilter_unique (hg : is_ultrafilter g) (hf : f ≠ ⊥) (h : f ≤ g) : f = g := le_antisymm h (hg.right _ hf h) lemma le_of_ultrafilter {g : filter α} (hf : is_ultrafilter f) (h : f ⊓ g ≠ ⊥) : f ≤ g := le_of_inf_eq $ ultrafilter_unique hf h inf_le_left /-- Equivalent characterization of ultrafilters: A filter f is an ultrafilter if and only if for each set s, -s belongs to f if and only if s does not belong to f. -/ lemma ultrafilter_iff_compl_mem_iff_not_mem : is_ultrafilter f ↔ (∀ s, -s ∈ f ↔ s ∉ f) := ⟨assume hf s, ⟨assume hns hs, hf.1 $ empty_in_sets_eq_bot.mp $ by convert f.inter_sets hs hns; rw [inter_compl_self], assume hs, have f ≤ principal (-s), from le_of_ultrafilter hf $ assume h, hs $ mem_sets_of_neq_bot $ by simp only [h, eq_self_iff_true, lattice.neg_neg], by simp only [le_principal_iff] at this; assumption⟩, assume hf, ⟨mt empty_in_sets_eq_bot.mpr ((hf ∅).mp (by convert f.univ_sets; rw [compl_empty])), assume g hg g_le s hs, classical.by_contradiction $ mt (hf s).mpr $ assume : - s ∈ f, have s ∩ -s ∈ g, from inter_mem_sets hs (g_le this), by simp only [empty_in_sets_eq_bot, hg, inter_compl_self] at this; contradiction⟩⟩ lemma mem_or_compl_mem_of_ultrafilter (hf : is_ultrafilter f) (s : set α) : s ∈ f ∨ - s ∈ f := classical.or_iff_not_imp_left.2 (ultrafilter_iff_compl_mem_iff_not_mem.mp hf s).mpr lemma mem_or_mem_of_ultrafilter {s t : set α} (hf : is_ultrafilter f) (h : s ∪ t ∈ f) : s ∈ f ∨ t ∈ f := (mem_or_compl_mem_of_ultrafilter hf s).imp_right (assume : -s ∈ f, by filter_upwards [this, h] assume x hnx hx, hx.resolve_left hnx) lemma mem_of_finite_sUnion_ultrafilter {s : set (set α)} (hf : is_ultrafilter f) (hs : finite s) : ⋃₀ s ∈ f → ∃t∈s, t ∈ f := finite.induction_on hs (by simp only [empty_in_sets_eq_bot, hf.left, mem_empty_eq, sUnion_empty, forall_prop_of_false, exists_false, not_false_iff, exists_prop_of_false]) $ λ t s' ht' hs' ih, by simp only [exists_prop, mem_insert_iff, set.sUnion_insert]; exact assume h, (mem_or_mem_of_ultrafilter hf h).elim (assume : t ∈ f, ⟨t, or.inl rfl, this⟩) (assume h, let ⟨t, hts', ht⟩ := ih h in ⟨t, or.inr hts', ht⟩) lemma mem_of_finite_Union_ultrafilter {is : set β} {s : β → set α} (hf : is_ultrafilter f) (his : finite is) (h : (⋃i∈is, s i) ∈ f) : ∃i∈is, s i ∈ f := have his : finite (image s is), from finite_image s his, have h : (⋃₀ image s is) ∈ f, from by simp only [sUnion_image, set.sUnion_image]; assumption, let ⟨t, ⟨i, hi, h_eq⟩, (ht : t ∈ f)⟩ := mem_of_finite_sUnion_ultrafilter hf his h in ⟨i, hi, h_eq.symm ▸ ht⟩ lemma ultrafilter_map {f : filter α} {m : α → β} (h : is_ultrafilter f) : is_ultrafilter (map m f) := by rw ultrafilter_iff_compl_mem_iff_not_mem at ⊢ h; exact assume s, h (m ⁻¹' s) lemma ultrafilter_pure {a : α} : is_ultrafilter (pure a) := begin rw ultrafilter_iff_compl_mem_iff_not_mem, intro s, rw [mem_pure_sets, mem_pure_sets], exact iff.rfl end lemma ultrafilter_bind {f : filter α} (hf : is_ultrafilter f) {m : α → filter β} (hm : ∀ a, is_ultrafilter (m a)) : is_ultrafilter (f.bind m) := begin simp only [ultrafilter_iff_compl_mem_iff_not_mem] at ⊢ hf hm, intro s, dsimp [bind, join, map], simp only [hm], apply hf end /-- The ultrafilter lemma: Any proper filter is contained in an ultrafilter. -/ lemma exists_ultrafilter (h : f ≠ ⊥) : ∃u, u ≤ f ∧ is_ultrafilter u := let τ := {f' // f' ≠ ⊥ ∧ f' ≤ f}, r : τ → τ → Prop := λt₁ t₂, t₂.val ≤ t₁.val, ⟨a, ha⟩ := inhabited_of_mem_sets h univ_mem_sets, top : τ := ⟨f, h, le_refl f⟩, sup : Π(c:set τ), chain r c → τ := λc hc, ⟨⨅a:{a:τ // a ∈ insert top c}, a.val.val, infi_neq_bot_of_directed ⟨a⟩ (directed_of_chain $ chain_insert hc $ assume ⟨b, _, hb⟩ _ _, or.inl hb) (assume ⟨⟨a, ha, _⟩, _⟩, ha), infi_le_of_le ⟨top, mem_insert _ _⟩ (le_refl _)⟩ in have ∀c (hc: chain r c) a (ha : a ∈ c), r a (sup c hc), from assume c hc a ha, infi_le_of_le ⟨a, mem_insert_of_mem _ ha⟩ (le_refl _), have (∃ (u : τ), ∀ (a : τ), r u a → r a u), from zorn (assume c hc, ⟨sup c hc, this c hc⟩) (assume f₁ f₂ f₃ h₁ h₂, le_trans h₂ h₁), let ⟨uτ, hmin⟩ := this in ⟨uτ.val, uτ.property.right, uτ.property.left, assume g hg₁ hg₂, hmin ⟨g, hg₁, le_trans hg₂ uτ.property.right⟩ hg₂⟩ /-- Construct an ultrafilter extending a given filter. The ultrafilter lemma is the assertion that such a filter exists; we use the axiom of choice to pick one. -/ noncomputable def ultrafilter_of (f : filter α) : filter α := if h : f = ⊥ then ⊥ else classical.epsilon (λu, u ≤ f ∧ is_ultrafilter u) lemma ultrafilter_of_spec (h : f ≠ ⊥) : ultrafilter_of f ≤ f ∧ is_ultrafilter (ultrafilter_of f) := begin have h' := classical.epsilon_spec (exists_ultrafilter h), simp only [ultrafilter_of, dif_neg, h, dif_neg, not_false_iff], simp only at h', assumption end lemma ultrafilter_of_le : ultrafilter_of f ≤ f := if h : f = ⊥ then by simp only [ultrafilter_of, dif_pos, h, dif_pos, eq_self_iff_true, le_bot_iff]; exact le_refl _ else (ultrafilter_of_spec h).left lemma ultrafilter_ultrafilter_of (h : f ≠ ⊥) : is_ultrafilter (ultrafilter_of f) := (ultrafilter_of_spec h).right lemma ultrafilter_of_ultrafilter (h : is_ultrafilter f) : ultrafilter_of f = f := ultrafilter_unique h (ultrafilter_ultrafilter_of h.left).left ultrafilter_of_le /-- A filter equals the intersection of all the ultrafilters which contain it. -/ lemma sup_of_ultrafilters (f : filter α) : f = ⨆ (g) (u : is_ultrafilter g) (H : g ≤ f), g := begin refine le_antisymm _ (supr_le $ λ g, supr_le $ λ u, supr_le $ λ H, H), intros s hs, -- If s ∉ f.sets, we'll apply the ultrafilter lemma to the restriction of f to -s. by_contradiction hs', let j : (-s) → α := subtype.val, have j_inv_s : j ⁻¹' s = ∅, by erw [←preimage_inter_range, subtype.val_range, inter_compl_self, preimage_empty], let f' := comap j f, have : f' ≠ ⊥, { apply mt empty_in_sets_eq_bot.mpr, rintro ⟨t, htf, ht⟩, suffices : t ⊆ s, from absurd (f.sets_of_superset htf this) hs', rw [subset_empty_iff] at ht, have : j '' (j ⁻¹' t) = ∅, by rw [ht, image_empty], erw [image_preimage_eq_inter_range, subtype.val_range, ←subset_compl_iff_disjoint, set.compl_compl] at this, exact this }, rcases exists_ultrafilter this with ⟨g', g'f', u'⟩, simp only [supr_sets_eq, mem_Inter] at hs, have := hs (g'.map subtype.val) (ultrafilter_map u') (map_le_iff_le_comap.mpr g'f'), rw [←le_principal_iff, map_le_iff_le_comap, comap_principal, j_inv_s, principal_empty, le_bot_iff] at this, exact absurd this u'.1 end /-- The `tendsto` relation can be checked on ultrafilters. -/ lemma tendsto_iff_ultrafilter (f : α → β) (l₁ : filter α) (l₂ : filter β) : tendsto f l₁ l₂ ↔ ∀ g, is_ultrafilter g → g ≤ l₁ → g.map f ≤ l₂ := ⟨assume h g u gx, le_trans (map_mono gx) h, assume h, by rw [sup_of_ultrafilters l₁]; simpa only [tendsto, map_supr, supr_le_iff]⟩ /- The ultrafilter monad. The monad structure on ultrafilters is the restriction of the one on filters. -/ def ultrafilter (α : Type u) : Type u := {f : filter α // is_ultrafilter f} def ultrafilter.map (m : α → β) (u : ultrafilter α) : ultrafilter β := ⟨u.val.map m, ultrafilter_map u.property⟩ def ultrafilter.pure (x : α) : ultrafilter α := ⟨pure x, ultrafilter_pure⟩ def ultrafilter.bind (u : ultrafilter α) (m : α → ultrafilter β) : ultrafilter β := ⟨u.val.bind (λ a, (m a).val), ultrafilter_bind u.property (λ a, (m a).property)⟩ instance ultrafilter.has_pure : has_pure ultrafilter := ⟨@ultrafilter.pure⟩ instance ultrafilter.has_bind : has_bind ultrafilter := ⟨@ultrafilter.bind⟩ instance ultrafilter.functor : functor ultrafilter := { map := @ultrafilter.map } instance ultrafilter.monad : monad ultrafilter := { map := @ultrafilter.map } noncomputable def hyperfilter : filter α := ultrafilter_of cofinite lemma hyperfilter_le_cofinite (hi : set.infinite (@set.univ α)) : @hyperfilter α ≤ cofinite := (ultrafilter_of_spec (cofinite_ne_bot hi)).1 lemma is_ultrafilter_hyperfilter (hi : set.infinite (@set.univ α)) : is_ultrafilter (@hyperfilter α) := (ultrafilter_of_spec (cofinite_ne_bot hi)).2 theorem nmem_hyperfilter_of_finite (hi : set.infinite (@set.univ α)) {s : set α} (hf : set.finite s) : s ∉ @hyperfilter α := λ hy, have hx : -s ∉ hyperfilter := λ hs, (ultrafilter_iff_compl_mem_iff_not_mem.mp (is_ultrafilter_hyperfilter hi) s).mp hs hy, have ht : -s ∈ cofinite.sets := by show -s ∈ {s | _}; rwa [set.mem_set_of_eq, lattice.neg_neg], hx $ hyperfilter_le_cofinite hi ht theorem compl_mem_hyperfilter_of_finite (hi : set.infinite (@set.univ α)) {s : set α} (hf : set.finite s) : -s ∈ @hyperfilter α := (ultrafilter_iff_compl_mem_iff_not_mem.mp (is_ultrafilter_hyperfilter hi) s).mpr $ nmem_hyperfilter_of_finite hi hf theorem mem_hyperfilter_of_finite_compl (hi : set.infinite (@set.univ α)) {s : set α} (hf : set.finite (-s)) : s ∈ @hyperfilter α := have h : _ := compl_mem_hyperfilter_of_finite hi hf, by rwa [lattice.neg_neg] at h section local attribute [instance] filter.monad filter.is_lawful_monad instance ultrafilter.is_lawful_monad : is_lawful_monad ultrafilter := { id_map := assume α f, subtype.eq (id_map f.val), pure_bind := assume α β a f, subtype.eq (pure_bind a (subtype.val ∘ f)), bind_assoc := assume α β γ f m₁ m₂, subtype.eq (filter_eq rfl), bind_pure_comp_eq_map := assume α β f x, subtype.eq (bind_pure_comp_eq_map _ f x.val) } end lemma ultrafilter.eq_iff_val_le_val {u v : ultrafilter α} : u = v ↔ u.val ≤ v.val := ⟨assume h, by rw h; exact le_refl _, assume h, by rw subtype.ext; apply ultrafilter_unique v.property u.property.1 h⟩ lemma exists_ultrafilter_iff (f : filter α) : (∃ (u : ultrafilter α), u.val ≤ f) ↔ f ≠ ⊥ := ⟨assume ⟨u, uf⟩, lattice.neq_bot_of_le_neq_bot u.property.1 uf, assume h, let ⟨u, uf, hu⟩ := exists_ultrafilter h in ⟨⟨u, hu⟩, uf⟩⟩ end ultrafilter end filter namespace filter variables {α β γ : Type u} {f : β → filter α} {s : γ → set α} open list lemma mem_traverse_sets : ∀(fs : list β) (us : list γ), forall₂ (λb c, s c ∈ f b) fs us → traverse s us ∈ traverse f fs | [] [] forall₂.nil := mem_pure_sets.2 $ mem_singleton _ | (f::fs) (u::us) (forall₂.cons h hs) := seq_mem_seq_sets (image_mem_map h) (mem_traverse_sets fs us hs) lemma mem_traverse_sets_iff (fs : list β) (t : set (list α)) : t ∈ traverse f fs ↔ (∃us:list (set α), forall₂ (λb (s : set α), s ∈ f b) fs us ∧ sequence us ⊆ t) := begin split, { induction fs generalizing t, case nil { simp only [sequence, pure_def, imp_self, forall₂_nil_left_iff, pure_def, exists_eq_left, mem_principal_sets, set.pure_def, singleton_subset_iff, traverse_nil] }, case cons : b fs ih t { assume ht, rcases mem_seq_sets_iff.1 ht with ⟨u, hu, v, hv, ht⟩, rcases mem_map_sets_iff.1 hu with ⟨w, hw, hwu⟩, rcases ih v hv with ⟨us, hus, hu⟩, exact ⟨w :: us, forall₂.cons hw hus, subset.trans (set.seq_mono hwu hu) ht⟩ } }, { rintros ⟨us, hus, hs⟩, exact mem_sets_of_superset (mem_traverse_sets _ _ hus) hs } end lemma sequence_mono : ∀(as bs : list (filter α)), forall₂ (≤) as bs → sequence as ≤ sequence bs | [] [] forall₂.nil := le_refl _ | (a::as) (b::bs) (forall₂.cons h hs) := seq_mono (map_mono h) (sequence_mono as bs hs) end filter
aaf125deb237851c0f7feccc3689fbc4d19d13ad
9dc8cecdf3c4634764a18254e94d43da07142918
/src/analysis/normed_space/matrix_exponential.lean
1a1aea1e5afa50d2bbc18f15c5f3604f11576839
[ "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
9,062
lean
/- Copyright (c) 2022 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser -/ import analysis.normed_space.exponential import analysis.matrix import linear_algebra.matrix.zpow import topology.uniform_space.matrix /-! # Lemmas about the matrix exponential In this file, we provide results about `exp` on `matrix`s over a topological or normed algebra. Note that generic results over all topological spaces such as `exp_zero` can be used on matrices without issue, so are not repeated here. The topological results specific to matrices are: * `matrix.exp_transpose` * `matrix.exp_conj_transpose` * `matrix.exp_diagonal` * `matrix.exp_block_diagonal` * `matrix.exp_block_diagonal'` Lemmas like `exp_add_of_commute` require a canonical norm on the type; while there are multiple sensible choices for the norm of a `matrix` (`matrix.normed_add_comm_group`, `matrix.frobenius_normed_add_comm_group`, `matrix.linfty_op_normed_add_comm_group`), none of them are canonical. In an application where a particular norm is chosen using `local attribute [instance]`, then the usual lemmas about `exp` are fine. When choosing a norm is undesirable, the results in this file can be used. In this file, we copy across the lemmas about `exp`, but hide the requirement for a norm inside the proof. * `matrix.exp_add_of_commute` * `matrix.exp_sum_of_commute` * `matrix.exp_nsmul` * `matrix.is_unit_exp` * `matrix.exp_units_conj` * `matrix.exp_units_conj'` Additionally, we prove some results about `matrix.has_inv` and `matrix.div_inv_monoid`, as the results for general rings are instead stated about `ring.inverse`: * `matrix.exp_neg` * `matrix.exp_zsmul` * `matrix.exp_conj` * `matrix.exp_conj'` ## Implementation notes This file runs into some sharp edges on typeclass search in lean 3, especially regarding pi types. To work around this, we copy a handful of instances for when lean can't find them by itself. Hopefully we will be able to remove these in Lean 4. ## TODO * Show that `matrix.det (exp 𝕂 A) = exp 𝕂 (matrix.trace A)` ## References * https://en.wikipedia.org/wiki/Matrix_exponential -/ open_locale matrix big_operators section hacks_for_pi_instance_search /-- A special case of `pi.topological_ring` for when `R` is not dependently typed. -/ instance function.topological_ring (I : Type*) (R : Type*) [non_unital_ring R] [topological_space R] [topological_ring R] : topological_ring (I → R) := pi.topological_ring /-- A special case of `function.algebra` for when A is a `ring` not a `semiring` -/ instance function.algebra_ring (I : Type*) {R : Type*} (A : Type*) [comm_semiring R] [ring A] [algebra R A] : algebra R (I → A) := pi.algebra _ _ /-- A special case of `pi.algebra` for when `f = λ i, matrix (m i) (m i) A`. -/ instance pi.matrix_algebra (I R A : Type*) (m : I → Type*) [comm_semiring R] [semiring A] [algebra R A] [Π i, fintype (m i)] [Π i, decidable_eq (m i)] : algebra R (Π i, matrix (m i) (m i) A) := @pi.algebra I R (λ i, matrix (m i) (m i) A) _ _ (λ i, matrix.algebra) /-- A special case of `pi.topological_ring` for when `f = λ i, matrix (m i) (m i) A`. -/ instance pi.matrix_topological_ring (I A : Type*) (m : I → Type*) [ring A] [topological_space A] [topological_ring A] [Π i, fintype (m i)] : topological_ring (Π i, matrix (m i) (m i) A) := @pi.topological_ring _ (λ i, matrix (m i) (m i) A) _ _ (λ i, matrix.topological_ring) end hacks_for_pi_instance_search variables (𝕂 : Type*) {m n p : Type*} {n' : m → Type*} {𝔸 : Type*} namespace matrix section topological section ring variables [fintype m] [decidable_eq m] [fintype n] [decidable_eq n] [Π i, fintype (n' i)] [Π i, decidable_eq (n' i)] [field 𝕂] [ring 𝔸] [topological_space 𝔸] [topological_ring 𝔸] [algebra 𝕂 𝔸] [t2_space 𝔸] lemma exp_diagonal (v : m → 𝔸) : exp 𝕂 (diagonal v) = diagonal (exp 𝕂 v) := by simp_rw [exp_eq_tsum, diagonal_pow, ←diagonal_smul, ←diagonal_tsum] lemma exp_block_diagonal (v : m → matrix n n 𝔸) : exp 𝕂 (block_diagonal v) = block_diagonal (exp 𝕂 v) := by simp_rw [exp_eq_tsum, ←block_diagonal_pow, ←block_diagonal_smul, ←block_diagonal_tsum] lemma exp_block_diagonal' (v : Π i, matrix (n' i) (n' i) 𝔸) : exp 𝕂 (block_diagonal' v) = block_diagonal' (exp 𝕂 v) := by simp_rw [exp_eq_tsum, ←block_diagonal'_pow, ←block_diagonal'_smul, ←block_diagonal'_tsum] lemma exp_conj_transpose [star_ring 𝔸] [has_continuous_star 𝔸] (A : matrix m m 𝔸) : exp 𝕂 Aᴴ = (exp 𝕂 A)ᴴ := (star_exp A).symm end ring section comm_ring variables [fintype m] [decidable_eq m] [field 𝕂] [comm_ring 𝔸] [topological_space 𝔸] [topological_ring 𝔸] [algebra 𝕂 𝔸] [t2_space 𝔸] lemma exp_transpose (A : matrix m m 𝔸) : exp 𝕂 Aᵀ = (exp 𝕂 A)ᵀ := by simp_rw [exp_eq_tsum, transpose_tsum, transpose_smul, transpose_pow] end comm_ring end topological section normed variables [is_R_or_C 𝕂] [fintype m] [decidable_eq m] [fintype n] [decidable_eq n] [Π i, fintype (n' i)] [Π i, decidable_eq (n' i)] [normed_ring 𝔸] [normed_algebra 𝕂 𝔸] [complete_space 𝔸] lemma exp_add_of_commute (A B : matrix m m 𝔸) (h : commute A B) : exp 𝕂 (A + B) = exp 𝕂 A ⬝ exp 𝕂 B := begin letI : semi_normed_ring (matrix m m 𝔸) := matrix.linfty_op_semi_normed_ring, letI : normed_ring (matrix m m 𝔸) := matrix.linfty_op_normed_ring, letI : normed_algebra 𝕂 (matrix m m 𝔸) := matrix.linfty_op_normed_algebra, exact exp_add_of_commute h, end lemma exp_sum_of_commute {ι} (s : finset ι) (f : ι → matrix m m 𝔸) (h : ∀ (i ∈ s) (j ∈ s), commute (f i) (f j)) : exp 𝕂 (∑ i in s, f i) = s.noncomm_prod (λ i, exp 𝕂 (f i)) (λ i hi j hj, (h i hi j hj).exp 𝕂) := begin letI : semi_normed_ring (matrix m m 𝔸) := matrix.linfty_op_semi_normed_ring, letI : normed_ring (matrix m m 𝔸) := matrix.linfty_op_normed_ring, letI : normed_algebra 𝕂 (matrix m m 𝔸) := matrix.linfty_op_normed_algebra, exact exp_sum_of_commute s f h, end lemma exp_nsmul (n : ℕ) (A : matrix m m 𝔸) : exp 𝕂 (n • A) = exp 𝕂 A ^ n := begin letI : semi_normed_ring (matrix m m 𝔸) := matrix.linfty_op_semi_normed_ring, letI : normed_ring (matrix m m 𝔸) := matrix.linfty_op_normed_ring, letI : normed_algebra 𝕂 (matrix m m 𝔸) := matrix.linfty_op_normed_algebra, exact exp_nsmul n A, end lemma is_unit_exp (A : matrix m m 𝔸) : is_unit (exp 𝕂 A) := begin letI : semi_normed_ring (matrix m m 𝔸) := matrix.linfty_op_semi_normed_ring, letI : normed_ring (matrix m m 𝔸) := matrix.linfty_op_normed_ring, letI : normed_algebra 𝕂 (matrix m m 𝔸) := matrix.linfty_op_normed_algebra, exact is_unit_exp _ A, end lemma exp_units_conj (U : (matrix m m 𝔸)ˣ) (A : matrix m m 𝔸) : exp 𝕂 (↑U ⬝ A ⬝ ↑(U⁻¹) : matrix m m 𝔸) = ↑U ⬝ exp 𝕂 A ⬝ ↑(U⁻¹) := begin letI : semi_normed_ring (matrix m m 𝔸) := matrix.linfty_op_semi_normed_ring, letI : normed_ring (matrix m m 𝔸) := matrix.linfty_op_normed_ring, letI : normed_algebra 𝕂 (matrix m m 𝔸) := matrix.linfty_op_normed_algebra, exact exp_units_conj _ U A, end lemma exp_units_conj' (U : (matrix m m 𝔸)ˣ) (A : matrix m m 𝔸) : exp 𝕂 (↑(U⁻¹) ⬝ A ⬝ U : matrix m m 𝔸) = ↑(U⁻¹) ⬝ exp 𝕂 A ⬝ U := exp_units_conj 𝕂 U⁻¹ A end normed section normed_comm variables [is_R_or_C 𝕂] [fintype m] [decidable_eq m] [fintype n] [decidable_eq n] [Π i, fintype (n' i)] [Π i, decidable_eq (n' i)] [normed_comm_ring 𝔸] [normed_algebra 𝕂 𝔸] [complete_space 𝔸] lemma exp_neg (A : matrix m m 𝔸) : exp 𝕂 (-A) = (exp 𝕂 A)⁻¹ := begin rw nonsing_inv_eq_ring_inverse, letI : semi_normed_ring (matrix m m 𝔸) := matrix.linfty_op_semi_normed_ring, letI : normed_ring (matrix m m 𝔸) := matrix.linfty_op_normed_ring, letI : normed_algebra 𝕂 (matrix m m 𝔸) := matrix.linfty_op_normed_algebra, exact (ring.inverse_exp _ A).symm, end lemma exp_zsmul (z : ℤ) (A : matrix m m 𝔸) : exp 𝕂 (z • A) = exp 𝕂 A ^ z := begin obtain ⟨n, rfl | rfl⟩ := z.eq_coe_or_neg, { rw [zpow_coe_nat, coe_nat_zsmul, exp_nsmul] }, { have : is_unit (exp 𝕂 A).det := (matrix.is_unit_iff_is_unit_det _).mp (is_unit_exp _ _), rw [matrix.zpow_neg this, zpow_coe_nat, neg_smul, exp_neg, coe_nat_zsmul, exp_nsmul] }, end lemma exp_conj (U : matrix m m 𝔸) (A : matrix m m 𝔸) (hy : is_unit U) : exp 𝕂 (U ⬝ A ⬝ U⁻¹) = U ⬝ exp 𝕂 A ⬝ U⁻¹ := let ⟨u, hu⟩ := hy in hu ▸ by simpa only [matrix.coe_units_inv] using exp_units_conj 𝕂 u A lemma exp_conj' (U : matrix m m 𝔸) (A : matrix m m 𝔸) (hy : is_unit U) : exp 𝕂 (U⁻¹ ⬝ A ⬝ U) = U⁻¹ ⬝ exp 𝕂 A ⬝ U := let ⟨u, hu⟩ := hy in hu ▸ by simpa only [matrix.coe_units_inv] using exp_units_conj' 𝕂 u A end normed_comm end matrix
ff7c3571ccf4ceb542bb2de1cb6c030ff17911b2
d4df7c538bbc0b06771d7b7a6f46fbadebb8c5eb
/src/comonad_transformers/env_t.lean
77b6b93539246ad5f53a353babe2f4ee6482df1b
[]
no_license
breakerzirconia/LeanTransformers
11bb924b59ac589d0c6b39586d27cccc8f9e939c
3c52441b8990bab7d8cf1817df6e04f657a14e1c
refs/heads/main
1,682,610,526,027
1,620,477,129,000
1,620,477,129,000
365,507,838
0
0
null
null
null
null
UTF-8
Lean
false
false
1,010
lean
import comonad_transformers.comonad import comonad_transformers.comonad_trans ---------------------------- -- Definition + instances -- ---------------------------- structure EnvT (e : Type) (w : Type → Type) (a : Type) := (env : e) (val : w a) open EnvT instance {e : Type} {w : Type → Type} [functor w] : functor (EnvT e w) := { map := λ α β f env, mk env.env (f <$> env.val) } instance {e : Type} {w : Type → Type} [has_extract w] : has_extract (EnvT e w) := { extract := λ α env, *env.val } instance {e : Type} {w : Type → Type} [functor w] [has_extend w] : has_extend (EnvT e w) := { extend := λ α β f env, mk env.env (f <$> (<<=) _ _ (mk env.env) env.val) } instance {e : Type} {w : Type → Type} [comonad w] : comonad (EnvT e w) := {} protected def EnvT_lower {e : Type} {w : Type → Type} [comonad w] {a : Type} : EnvT e w a → w a := λ env, env.val instance {e : Type} : comonad_trans (EnvT e) := ⟨@EnvT_lower e⟩
4d286082d2831b654f09404681fa96c1d69511ac
b7f22e51856f4989b970961f794f1c435f9b8f78
/library/theories/topology/continuous.lean
b4966213784f70a63079226b74bdf973415e8cd1
[ "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
14,363
lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jacob Gross, Jeremy Avigad Continuous functions. -/ import theories.topology.basic algebra.category ..move .limit open algebra eq.ops set topology function category sigma.ops namespace topology /- continuity on a set -/ variables {X Y Z : Type} [topology X] [topology Y] [topology Z] definition continuous_on (f : X → Y) (s : set X) : Prop := ∀ ⦃t : set Y⦄, Open t → (∃ u : set X, Open u ∧ u ∩ s = f '- t ∩ s) theorem exists_Open_of_continous_on {f : X → Y} {s : set X} {t : set Y} (Ot : Open t) (H : continuous_on f s) : ∃ u : set X, Open u ∧ u ∩ s = f '- t ∩ s := H Ot theorem Open_preimage_inter_of_continuous_on {f : X → Y} {s : set X} (Os : Open s) (Hcont : continuous_on f s) {t : set Y} (Ot : Open t) : Open (f '- t ∩ s) := obtain u [Ou Hu], from Hcont Ot, by rewrite[-Hu]; exact Open_inter Ou Os theorem continuous_on_of_forall_open {f : X → Y} {s : set X} (H : ∀ t, Open t → Open (f '- t ∩ s)) : continuous_on f s := take t, assume Ot, have f '- t ∩ s ∩ s = f '- t ∩ s, by rewrite [inter_assoc, inter_self], exists.intro (f '- t ∩ s) (and.intro (H t Ot) this) theorem Open_preimage_of_continuous_on {f : X → Y} {s : set X} (Opens : Open s) (contfs : continuous_on f s) {t : set Y} (Ot : Open t) (Hpre : f '- t ⊆ s) : Open (f '- t) := have f '- t ∩ s = f '- t, from inter_eq_self_of_subset Hpre, show Open (f '- t), by rewrite -this; apply Open_preimage_inter_of_continuous_on Opens contfs Ot theorem exists_closed_of_continuous_on {f : X → Y} {s : set X} (contfs : continuous_on f s) {t : set Y} (clt : closed t) : ∃ u, closed u ∧ u ∩ s = f '- t ∩ s := obtain v [Ov (Hv : v ∩ s = f '- -t ∩ s)], from contfs clt, have -v ∩ s = f '- t ∩ s, from inter_eq_inter_of_compl_inter_eq_compl_inter (by rewrite [compl_compl, Hv]), show ∃ u, closed u ∧ u ∩ s = f '- t ∩ s, from exists.intro (-v) (and.intro (closed_compl Ov) this) theorem continuous_on_of_forall_closed' {f : X → Y} {s : set X} (H : ∀ t, closed t → ∃ u, closed u ∧ u ∩ s = f '- t ∩ s) : continuous_on f s := take t : set Y, assume Ot : Open t, obtain (v : set X) [(clv : closed v) (Hv : v ∩ s = f '- (-t) ∩ s)], from H (-t) (closed_compl Ot), have (-v) ∩ s = f '- t ∩ s, from inter_eq_inter_of_compl_inter_eq_compl_inter (by rewrite [compl_compl, Hv]), show ∃ u, Open u ∧ u ∩ s = f '- t ∩ s, from exists.intro (-v) (and.intro clv this) theorem continuous_on_of_forall_closed {f : X → Y} {s : set X} (closeds : closed s) (H : ∀ B, closed B → closed (f '- B ∩ s)) : continuous_on f s := continuous_on_of_forall_closed' (λ B HB, exists.intro _ (and.intro (H B HB) (by rewrite [inter_assoc, inter_self]))) theorem closed_preimage_inter_of_continuous_on {f : X → Y} {s : set Y} (cls : closed s) {t : set X} (clt : closed t) (contft : continuous_on f t) : closed (f '- s ∩ t) := obtain u [clu Hu], from exists_closed_of_continuous_on contft cls, by rewrite [-Hu]; exact (closed_inter clu clt) theorem continuous_on_subset {s t : set X} {f : X → Y} (Hs : continuous_on f s) (ts : t ⊆ s) : continuous_on f t := take u, assume Ou, obtain v [Ov Hv], from Hs Ou, have v ∩ t = f '- u ∩ t, by rewrite [-inter_eq_self_of_subset_right ts, -*inter_assoc, Hv], show ∃ v, Open v ∧ v ∩ t = f '- u ∩ t, from exists.intro v (and.intro Ov this) theorem continous_on_union_of_closed {f : X → Y} {s t : set X} (cls : closed s) (clt : closed t) (contsf : continuous_on f s) (conttf : continuous_on f t) : continuous_on f (s ∪ t) := have ∀ u, closed u → closed (f '- u ∩ (s ∪ t)), from begin intro u clu, rewrite [inter_distrib_left], exact closed_union (closed_preimage_inter_of_continuous_on clu cls contsf) (closed_preimage_inter_of_continuous_on clu clt conttf) end, show continuous_on f (s ∪ t), from continuous_on_of_forall_closed (closed_union cls clt) this theorem continuous_on_empty (f : X → Y) : continuous_on f ∅ := continuous_on_of_forall_open (take B, assume OpenB, by rewrite[inter_empty]; apply Open_empty) theorem continuous_on_union {f : X → Y} {s t : set X} (Opens : Open s) (Opent : Open t) (contsf : continuous_on f s) (conttf : continuous_on f t) : continuous_on f (s ∪ t) := continuous_on_of_forall_open (take B, assume OpenB, have Open (f '- B ∩ s), from Open_preimage_inter_of_continuous_on Opens contsf OpenB, have Open (f '- B ∩ t), from Open_preimage_inter_of_continuous_on Opent conttf OpenB, show Open (f '- B ∩ (s ∪ t)), by rewrite [inter_distrib_left]; apply Open_union; assumption; assumption) theorem continuous_on_id (s : set X) : continuous_on (@id X) s := λ B OpB, exists.intro B (and.intro OpB (by rewrite preimage_id)) theorem continuous_on_comp {s : set X} {f : X → Y} {g : Y → Z} (Hf : continuous_on f s) (Hg : continuous_on g (f ' s)) : continuous_on (g ∘ f) s := take t, assume Ot, obtain (u : set Y) [(Ou : Open u) (Hu : u ∩ f ' s = g '- t ∩ f ' s)], from Hg Ot, obtain (v : set X) [(Ov : Open v) (Hv : v ∩ s = f '- u ∩ s)], from Hf Ou, have s ⊆ f '- (f ' s), from subset_preimage_image s f, have f '- (u ∩ f ' s) ∩ s = f '- (g '- t ∩ f ' s) ∩ s, by rewrite Hu, have f '- u ∩ s = f '- (g '- t) ∩ s, begin revert this, rewrite [*preimage_inter, *inter_assoc, *inter_eq_self_of_subset_right `s ⊆ f '- (f ' s)`], intro H, exact H end, show ∃ v, Open v ∧ v ∩ s = (g ∘ f) '- t ∩ s, from exists.intro v (and.intro Ov (eq.trans Hv this)) theorem continuous_on_comp' {s : set X} {t : set Y} {f : X → Y} {g : Y → Z} (Hf : continuous_on f s) (Hg : continuous_on g t) (H : f ' s ⊆ t) : continuous_on (g ∘ f) s := continuous_on_comp Hf (continuous_on_subset Hg H) section open classical theorem continuous_on_singleton (f : X → Y) (x : X) : continuous_on f '{x} := take s, assume Ops, if Hx : x ∈ f '- s then have '{x} ⊆ f '- s, from singleton_subset_of_mem Hx, exists.intro univ (and.intro Open_univ (by rewrite [univ_inter, inter_eq_self_of_subset_right this])) else have f '- s ∩ '{x} = ∅, from eq_empty_of_forall_not_mem (take y, assume ymem, obtain (Hy : y ∈ f '- s) (Hy' : y ∈ '{x}), from ymem, have y = x, from eq_of_mem_singleton Hy', show false, from Hx (by rewrite -this; apply Hy)), exists.intro ∅ (and.intro Open_empty (by rewrite [this, empty_inter])) theorem continuous_on_const (c : Y) (s : set X) : continuous_on (λ x : X, c) s := take s, assume Ops, if cs : c ∈ s then have (λx, c) '- s = @univ X, from eq_univ_of_forall (take x, mem_preimage cs), exists.intro univ (and.intro Open_univ (by rewrite this)) else have (λx, c) '- s = (∅ : set X), from eq_empty_of_forall_not_mem (take x, assume H, cs (mem_of_mem_preimage H)), exists.intro ∅ (and.intro Open_empty (by rewrite this)) end /- pointwise continuity on a set -/ definition continuous_at_on (f : X → Y) (x : X) (s : set X) : Prop := ∀ ⦃t : set Y⦄, Open t → f x ∈ t → ∃ u, Open u ∧ x ∈ u ∧ u ∩ s ⊆ f '- t theorem continuous_at_on_of_continuous_on {f : X → Y} {s : set X} (H : continuous_on f s) ⦃x : X⦄ (xs : x ∈ s) : continuous_at_on f x s := take u, assume (Ou : Open u) (fxu : f x ∈ u), obtain (t : set X) [(Ot : Open t) (Ht : t ∩ s = f '- u ∩ s)], from H Ou, have x ∈ f '- u ∩ s, from and.intro fxu xs, have x ∈ t, by rewrite [-Ht at this]; exact and.left this, exists.intro t (and.intro Ot (and.intro this (by rewrite Ht; apply inter_subset_left))) section open classical theorem continuous_on_of_forall_continuous_at_on {f : X → Y} {s : set X} (H : ∀ x, continuous_at_on f x s) : continuous_on f s := take t, assume Ot : Open t, have H₁ : ∀₀ x ∈ f '- t, ∃ u', Open u' ∧ x ∈ u' ∧ u' ∩ s ⊆ f '- t, from λ x xmem, H x Ot (mem_of_mem_preimage xmem), let u := ⋃₀ {u' | ∃ x (Hx : x ∈ f '- t), u' = some (H₁ Hx) } in have Open u, from Open_sUnion (take u', assume Hu', obtain x (Hx : x ∈ f '- t) (u'eq : u' = some (H₁ Hx)), from Hu', show Open u', by rewrite u'eq; apply and.left (some_spec (H₁ Hx))), have Hu₁ : u ∩ s ⊆ f '- t, from take x, assume Hx, obtain xu xs, from Hx, obtain u' [[x' (Hx' : x' ∈ f '- t) (u'eq : u' = some (H₁ Hx'))] (xu' : x ∈ u')], from xu, have u' ∩ s ⊆ f '- t, by rewrite u'eq; exact and.right (and.right (some_spec (H₁ Hx'))), show x ∈ f '- t, from this (and.intro xu' xs), have Hu₂ : f '- t ∩ s ⊆ u, from take x, assume Hx : x ∈ f '- t ∩ s, obtain xft xs, from Hx, let u' := some (H₁ xft) in have x ∈ u', from and.left (and.right (some_spec (H₁ xft))), show x ∈ u, from exists.intro u' (and.intro (exists.intro x (exists.intro xft rfl)) this), show ∃ u, Open u ∧ u ∩ s = f '- t ∩ s, from exists.intro u (and.intro `Open u` (inter_eq_inter_right Hu₁ Hu₂)) end /- continuity -/ definition continuous (f : X → Y) : Prop := ∀ ⦃s : set Y⦄, Open s → Open (f '- s) theorem continuous_of_continuous_on_univ {f : X → Y} (H : continuous_on f univ) : continuous f := λ s Os, by rewrite [-inter_univ]; exact Open_preimage_inter_of_continuous_on Open_univ H Os theorem continuous_on_of_continuous {f : X → Y} (s : set X) (H : continuous f) : continuous_on f s := take t, assume Ot, exists.intro (f '- t) (and.intro (H Ot) rfl) theorem continuous_on_univ_of_continuous {f : X → Y} (H : continuous f) : continuous_on f univ := continuous_on_of_continuous univ H theorem continuous_iff (f : X → Y) : continuous f ↔ continuous_on f univ := iff.intro continuous_on_univ_of_continuous continuous_of_continuous_on_univ theorem Open_preimage_of_continuous {f : X → Y} (H : continuous f) ⦃s : set Y⦄ (Os : Open s) : Open (f '- s) := H Os theorem closed_preimage_of_continuous {f : X → Y} (H : continuous f) {s : set Y} (cls : closed s) : closed (f '- s) := by rewrite [↑closed, -preimage_compl]; exact H cls theorem continuous_id : continuous (@id X) := λ s Os, Os theorem continuous_comp {f : X → Y} {g : Y → Z} (Hf : continuous f) (Hg : continuous g) : continuous (g ∘ f) := λ s Os, Hf (Hg Os) theorem continuous_const (c : Y) : continuous (λ x : X, c) := continuous_of_continuous_on_univ (continuous_on_const c univ) /- continuity at a point -/ definition continuous_at (f : X → Y) (x : X) : Prop := ∀ ⦃t : set Y⦄, Open t → f x ∈ t → ∃ u, Open u ∧ x ∈ u ∧ u ⊆ f '- t theorem continuous_at_of_continuous_at_on {f : X → Y} {x : X} {s : set X} (Os : Open s) (xs : x ∈ s) (H : continuous_at_on f x s) : continuous_at f x := take t, assume Ot fxt, obtain u Ou xu xssub, from H Ot fxt, exists.intro (u ∩ s) (and.intro (Open_inter Ou Os) (and.intro (and.intro xu xs) xssub)) theorem continuous_at_of_continuous_at_on_univ {f : X → Y} {x : X} (H : continuous_at_on f x univ) : continuous_at f x := continuous_at_of_continuous_at_on Open_univ !mem_univ H theorem continuous_at_on_univ_of_continuous_at {f : X → Y} {x : X} (H : continuous_at f x) : continuous_at_on f x univ := take t, assume Ot fxt, obtain u Ou xu usub, from H Ot fxt, have u ∩ univ ⊆ f '- t, by rewrite inter_univ; apply usub, exists.intro u (and.intro Ou (and.intro xu this)) theorem continuous_at_iff_continuous_at_on_univ (f : X → Y) (x : X) : continuous_at f x ↔ continuous_at_on f x univ := iff.intro continuous_at_on_univ_of_continuous_at continuous_at_of_continuous_at_on_univ theorem continuous_of_forall_continuous_at {f : X → Y} (H : ∀ x : X, continuous_at f x) : continuous f := begin apply continuous_of_continuous_on_univ, apply continuous_on_of_forall_continuous_at_on, intro, apply continuous_at_on_univ_of_continuous_at, apply H end theorem forall_continuous_at_of_continuous {f : X → Y} (H : continuous f) : ∀ x : X, continuous_at f x := begin intro, apply continuous_at_of_continuous_at_on_univ, apply continuous_at_on_of_continuous_on, apply continuous_on_univ_of_continuous H, apply mem_univ end section limit open set theorem tendsto_at_of_continuous_at {f : X → Y} {x : X} (H : continuous_at f x) : (f ⟶ f x) (nhds x) := begin apply approaches_intro, intro s HOs Hfxs, cases H HOs Hfxs with u Hu, apply eventually_nhds_intro, exact and.left Hu, exact and.left (and.right Hu), intro x' Hx', apply @mem_of_mem_preimage _ _ f, apply and.right (and.right Hu), exact Hx' end theorem continuous_at_of_tendsto_at {f : X → Y} {x : X} (H : (f ⟶ f x) (nhds x)) : continuous_at f x := begin intro s HOs Hfxs, cases eventually_nhds_dest (approaches_elim H HOs Hfxs) with u Hu, existsi u, split, exact and.left Hu, split, exact and.left (and.right Hu), intro x Hx, apply mem_preimage, apply and.right (and.right Hu), apply Hx end end limit /- The Category TOP -/ section TOP open subtype private definition TOP_hom (A B : TopologicalSpace) : Type := {f : A → B | continuous f} private definition TOP_ID {A : TopologicalSpace} : TOP_hom A A := subtype.tag (@id A) continuous_id private definition TOP_comp ⦃ A B C : TopologicalSpace ⦄ (g : TOP_hom B C) (f : TOP_hom A B) : TOP_hom A C := subtype.tag (elt_of g ∘ elt_of f) (continuous_comp (subtype.has_property f) (subtype.has_property g)) private theorem TOP_assoc ⦃A B C D : TopologicalSpace⦄ (h : TOP_hom C D) (g : TOP_hom B C) (f : TOP_hom A B) : TOP_comp h (TOP_comp g f) = TOP_comp (TOP_comp h g) f := subtype.eq rfl private theorem id_left ⦃A B : TopologicalSpace ⦄ (f : TOP_hom A B) : TOP_comp TOP_ID f = f := subtype.eq rfl private theorem id_right ⦃A B : TopologicalSpace ⦄ (f : TOP_hom A B) : TOP_comp f TOP_ID = f := subtype.eq rfl definition TOP [reducible] [trans_instance] : category TopologicalSpace := ⦃ category, hom := TOP_hom, comp := TOP_comp, ID := @TOP_ID, assoc := TOP_assoc, id_left := id_left, id_right := id_right ⦄ end TOP end topology
1257afb1e5f1357fdaa63df5f61eff8aea72d5d4
02fbe05a45fda5abde7583464416db4366eedfbf
/library/init/meta/tactic.lean
1b84109e93f62aa6b90e4f0f9ebce5f75334e129
[ "Apache-2.0" ]
permissive
jasonrute/lean
cc12807e11f9ac6b01b8951a8bfb9c2eb35a0154
4be962c167ca442a0ec5e84472d7ff9f5302788f
refs/heads/master
1,672,036,664,637
1,601,642,826,000
1,601,642,826,000
260,777,966
0
0
Apache-2.0
1,588,454,819,000
1,588,454,818,000
null
UTF-8
Lean
false
false
74,709
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.function init.data.option.basic init.util import init.control.combinators init.control.monad init.control.alternative init.control.monad_fail import init.data.nat.div init.meta.exceptional init.meta.format init.meta.environment import init.meta.pexpr init.data.repr init.data.string.basic init.meta.interaction_monad import init.classical open native meta constant tactic_state : Type universes u v namespace tactic_state meta constant env : tactic_state → environment /-- Format the given tactic state. If `target_lhs_only` is true and the target is of the form `lhs ~ rhs`, where `~` is a simplification relation, then only the `lhs` is displayed. Remark: the parameter `target_lhs_only` is a temporary hack used to implement the `conv` monad. It will be removed in the future. -/ meta constant to_format (s : tactic_state) (target_lhs_only : bool := ff) : format /-- Format expression with respect to the main goal in the tactic state. If the tactic state does not contain any goals, then format expression using an empty local context. -/ meta constant format_expr : tactic_state → expr → format meta constant get_options : tactic_state → options meta constant set_options : tactic_state → options → tactic_state end tactic_state meta instance : has_to_format tactic_state := ⟨tactic_state.to_format⟩ meta instance : has_to_string tactic_state := ⟨λ s, (to_fmt s).to_string s.get_options⟩ /-- `tactic` is the monad for building tactics. You use this to: - View and modify the local goals and hypotheses in the prover's state. - Invoke type checking and elaboration of terms. - View and modify the environment. - Build new tactics out of existing ones such as `simp` and `rewrite`. -/ @[reducible] meta def tactic := interaction_monad tactic_state @[reducible] meta def tactic_result := interaction_monad.result tactic_state namespace tactic export interaction_monad (hiding failed fail) /-- Cause the tactic to fail with no error message. -/ meta def failed {α : Type} : tactic α := interaction_monad.failed meta def fail {α : Type u} {β : Type v} [has_to_format β] (msg : β) : tactic α := interaction_monad.fail msg end tactic namespace tactic_result export interaction_monad.result end tactic_result open tactic open tactic_result infixl ` >>=[tactic] `:2 := interaction_monad_bind infixl ` >>[tactic] `:2 := interaction_monad_seq meta instance : alternative tactic := { failure := @interaction_monad.failed _, orelse := @interaction_monad_orelse _, ..interaction_monad.monad } meta def {u₁ u₂} tactic.up {α : Type u₂} (t : tactic α) : tactic (ulift.{u₁} α) := λ s, match t s with | success a s' := success (ulift.up a) s' | exception t ref s := exception t ref s end meta def {u₁ u₂} tactic.down {α : Type u₂} (t : tactic (ulift.{u₁} α)) : tactic α := λ s, match t s with | success (ulift.up a) s' := success a s' | exception t ref s := exception t ref s end namespace interactive /-- Typeclass for custom interaction monads, which provides the information required to convert an interactive-mode construction to a `tactic` which can actually be executed. Given a `[monad m]`, `execute_with` explains how to turn a `begin ... end` block, or a `by ...` statement into a `tactic α` which can actually be executed. The `inhabited` first argument facilitates the passing of an optional configuration parameter `config`, using the syntax: ``` begin [custom_monad] with config, ... end ``` -/ meta class executor (m : Type → Type u) [monad m] := (config_type : Type) [inhabited : inhabited config_type] (execute_with : config_type → m unit → tactic unit) attribute [inline] executor.execute_with @[inline] meta def executor.execute_explicit (m : Type → Type u) [monad m] [e : executor m] : m unit → tactic unit := executor.execute_with e.inhabited.default @[inline] meta def executor.execute_with_explicit (m : Type → Type u) [monad m] [executor m] : executor.config_type m → m unit → tactic unit := executor.execute_with /-- Default `executor` instance for `tactic`s themselves -/ meta instance : executor tactic := { config_type := unit, inhabited := ⟨()⟩, execute_with := λ _, id } end interactive namespace tactic open interaction_monad.result variables {α : Type u} /-- Does nothing. -/ meta def skip : tactic unit := success () /-- `try_core t` acts like `t`, but succeeds even if `t` fails. It returns the result of `t` if `t` succeeded and `none` otherwise. -/ meta def try_core (t : tactic α) : tactic (option α) := λ s, match t s with | (exception _ _ _) := success none s | (success a s') := success (some a) s' end /-- `try t` acts like `t`, but succeeds even if `t` fails. -/ meta def try (t : tactic α) : tactic unit := λ s, match t s with | (exception _ _ _) := success () s | (success _ s') := success () s' end meta def try_lst : list (tactic unit) → tactic unit | [] := failed | (tac :: tacs) := λ s, match tac s with | success _ s' := try (try_lst tacs) s' | exception e p s' := match try_lst tacs s' with | exception _ _ _ := exception e p s' | r := r end end /-- `fail_if_success t` acts like `t`, but succeeds if `t` fails and fails if `t` succeeds. Changes made by `t` to the `tactic_state` are preserved only if `t` succeeds. -/ meta def fail_if_success {α : Type u} (t : tactic α) : tactic unit := λ s, match (t s) with | (success a s) := mk_exception "fail_if_success combinator failed, given tactic succeeded" none s | (exception _ _ _) := success () s end /-- `success_if_fail t` acts like `t`, but succeeds if `t` fails and fails if `t` succeeds. Changes made by `t` to the `tactic_state` are preserved only if `t` succeeds. -/ meta def success_if_fail {α : Type u} (t : tactic α) : tactic unit := λ s, match t s with | (success a s) := mk_exception "success_if_fail combinator failed, given tactic succeeded" none s | (exception _ _ _) := success () s end open nat /-- `iterate_at_most n t` iterates `t` `n` times or until `t` fails, returning the result of each successful iteration. -/ meta def iterate_at_most : nat → tactic α → tactic (list α) | 0 t := pure [] | (n + 1) t := do (some a) ← try_core t | pure [], as ← iterate_at_most n t, pure $ a :: as /-- `iterate_at_most' n t` repeats `t` `n` times or until `t` fails. -/ meta def iterate_at_most' : nat → tactic unit → tactic unit | 0 t := skip | (succ n) t := do (some _) ← try_core t | skip, iterate_at_most' n t /-- `iterate_exactly n t` iterates `t` `n` times, returning the result of each iteration. If any iteration fails, the whole tactic fails. -/ meta def iterate_exactly : nat → tactic α → tactic (list α) | 0 t := pure [] | (n + 1) t := do a ← t, as ← iterate_exactly n t, pure $ a ::as /-- `iterate_exactly' n t` executes `t` `n` times. If any iteration fails, the whole tactic fails. -/ meta def iterate_exactly' : nat → tactic unit → tactic unit | 0 t := skip | (n + 1) t := t *> iterate_exactly' n t /-- `iterate t` repeats `t` 100.000 times or until `t` fails, returning the result of each iteration. -/ meta def iterate : tactic α → tactic (list α) := iterate_at_most 100000 /-- `iterate' t` repeats `t` 100.000 times or until `t` fails. -/ meta def iterate' : tactic unit → tactic unit := iterate_at_most' 100000 meta def returnopt (e : option α) : tactic α := λ s, match e with | (some a) := success a s | none := mk_exception "failed" none s end meta instance opt_to_tac : has_coe (option α) (tactic α) := ⟨returnopt⟩ /-- Decorate t's exceptions with msg. -/ meta def decorate_ex (msg : format) (t : tactic α) : tactic α := λ s, result.cases_on (t s) success (λ opt_thunk, match opt_thunk with | some e := exception (some (λ u, msg ++ format.nest 2 (format.line ++ e u))) | none := exception none end) /-- Set the tactic_state. -/ @[inline] meta def write (s' : tactic_state) : tactic unit := λ s, success () s' /-- Get the tactic_state. -/ @[inline] meta def read : tactic tactic_state := λ s, success s s meta def get_options : tactic options := do s ← read, return s.get_options meta def set_options (o : options) : tactic unit := do s ← read, write (s.set_options o) meta def save_options {α : Type} (t : tactic α) : tactic α := do o ← get_options, a ← t, set_options o, return a meta def returnex {α : Type} (e : exceptional α) : tactic α := λ s, match e with | exceptional.success a := success a s | exceptional.exception f := match get_options s with | success opt _ := exception (some (λ u, f opt)) none s | exception _ _ _ := exception (some (λ u, f options.mk)) none s end end meta instance ex_to_tac {α : Type} : has_coe (exceptional α) (tactic α) := ⟨returnex⟩ end tactic meta def tactic_format_expr (e : expr) : tactic format := do s ← tactic.read, return (tactic_state.format_expr s e) meta class has_to_tactic_format (α : Type u) := (to_tactic_format : α → tactic format) meta instance : has_to_tactic_format expr := ⟨tactic_format_expr⟩ meta def tactic.pp {α : Type u} [has_to_tactic_format α] : α → tactic format := has_to_tactic_format.to_tactic_format open tactic format meta instance {α : Type u} [has_to_tactic_format α] : has_to_tactic_format (list α) := ⟨λ l, to_fmt <$> l.mmap pp⟩ meta instance (α : Type u) (β : Type v) [has_to_tactic_format α] [has_to_tactic_format β] : has_to_tactic_format (α × β) := ⟨λ ⟨a, b⟩, to_fmt <$> (prod.mk <$> pp a <*> pp b)⟩ meta def option_to_tactic_format {α : Type u} [has_to_tactic_format α] : option α → tactic format | (some a) := do fa ← pp a, return (to_fmt "(some " ++ fa ++ ")") | none := return "none" meta instance {α : Type u} [has_to_tactic_format α] : has_to_tactic_format (option α) := ⟨option_to_tactic_format⟩ meta instance {α} (a : α) : has_to_tactic_format (reflected a) := ⟨λ h, pp h.to_expr⟩ @[priority 10] meta instance has_to_format_to_has_to_tactic_format (α : Type) [has_to_format α] : has_to_tactic_format α := ⟨(λ x, return x) ∘ to_fmt⟩ namespace tactic open tactic_state meta def get_env : tactic environment := do s ← read, return $ env s meta def get_decl (n : name) : tactic declaration := do s ← read, (env s).get n meta constant get_trace_msg_pos : tactic pos meta def trace {α : Type u} [has_to_tactic_format α] (a : α) : tactic unit := do fmt ← pp a, return $ _root_.trace_fmt fmt (λ u, ()) meta def trace_call_stack : tactic unit := assume state, _root_.trace_call_stack (success () state) meta def timetac {α : Type u} (desc : string) (t : thunk (tactic α)) : tactic α := λ s, timeit desc (t () s) meta def trace_state : tactic unit := do s ← read, trace $ to_fmt s /-- A parameter representing how aggressively definitions should be unfolded when trying to decide if two terms match, unify or are definitionally equal. By default, theorem declarations are never unfolded. - `all` will unfold everything, including macros and theorems. Except projection macros. - `semireducible` will unfold everything except theorems and definitions tagged as irreducible. - `instances` will unfold all class instance definitions and definitions tagged with reducible. - `reducible` will only unfold definitions tagged with the `reducible` attribute. - `none` will never unfold anything. [NOTE] You are not allowed to tag a definition with more than one of `reducible`, `irreducible`, `semireducible` attributes. [NOTE] there is a config flag `m_unfold_lemmas`that will make it unfold theorems. -/ inductive transparency | all | semireducible | instances | reducible | none export transparency (reducible semireducible) /-- (eval_expr α e) evaluates 'e' IF 'e' has type 'α'. -/ meta constant eval_expr (α : Type u) [reflected α] : expr → tactic α /-- Return the partial term/proof constructed so far. Note that the resultant expression may contain variables that are not declarate in the current main goal. -/ meta constant result : tactic expr /-- Display the partial term/proof constructed so far. This tactic is *not* equivalent to `do { r ← result, s ← read, return (format_expr s r) }` because this one will format the result with respect to the current goal, and trace_result will do it with respect to the initial goal. -/ meta constant format_result : tactic format /-- Return target type of the main goal. Fail if tactic_state does not have any goal left. -/ meta constant target : tactic expr meta constant intro_core : name → tactic expr meta constant intron : nat → tactic unit /-- Clear the given local constant. The tactic fails if the given expression is not a local constant. -/ meta constant clear : expr → tactic unit /-- `revert_lst : list expr → tactic nat` is the reverse of `intron`. It takes a local constant `c` and puts it back as bound by a `pi` or `elet` of the main target. If there are other local constants that depend on `c`, these are also reverted. Because of this, the `nat` that is returned is the actual number of reverted local constants. Example: with `x : ℕ, h : P(x) ⊢ T(x)`, `revert_lst [x]` returns `2` and produces the state ` ⊢ Π x, P(x) → T(x)`. -/ meta constant revert_lst : list expr → tactic nat /-- Return `e` in weak head normal form with respect to the given transparency setting. If `unfold_ginductive` is `tt`, then nested and/or mutually recursive inductive datatype constructors and types are unfolded. Recall that nested and mutually recursive inductive datatype declarations are compiled into primitive datatypes accepted by the Kernel. -/ meta constant whnf (e : expr) (md := semireducible) (unfold_ginductive := tt) : tactic expr /-- (head) eta expand the given expression. `f : α → β` head-eta-expands to `λ a, f a`. If `f` isn't a function then it just returns `f`. -/ meta constant head_eta_expand : expr → tactic expr /-- (head) beta reduction. `(λ x, B) c` reduces to `B[x/c]`. -/ meta constant head_beta : expr → tactic expr /-- (head) zeta reduction. Reduction of let bindings at the head of the expression. `let x : a := b in c` reduces to `c[x/b]`. -/ meta constant head_zeta : expr → tactic expr /-- Zeta reduction. Reduction of let bindings. `let x : a := b in c` reduces to `c[x/b]`. -/ meta constant zeta : expr → tactic expr /-- (head) eta reduction. `(λ x, f x)` reduces to `f`. -/ meta constant head_eta : expr → tactic expr /-- Succeeds if `t` and `s` can be unified using the given transparency setting. -/ meta constant unify (t s : expr) (md := semireducible) (approx := ff) : tactic unit /-- Similar to `unify`, but it treats metavariables as constants. -/ meta constant is_def_eq (t s : expr) (md := semireducible) (approx := ff) : tactic unit /-- Infer the type of the given expression. Remark: transparency does not affect type inference -/ meta constant infer_type : expr → tactic expr /-- Get the `local_const` expr for the given `name`. -/ meta constant get_local : name → tactic expr /-- Resolve a name using the current local context, environment, aliases, etc. -/ meta constant resolve_name : name → tactic pexpr /-- Return the hypothesis in the main goal. Fail if tactic_state does not have any goal left. -/ meta constant local_context : tactic (list expr) /-- Get a fresh name that is guaranteed to not be in use in the local context. If `n` is provided and `n` is not in use, then `n` is returned. Otherwise a number `i` is appended to give `"n_i"`. -/ meta constant get_unused_name (n : name := `_x) (i : option nat := none) : tactic name /-- Helper tactic for creating simple applications where some arguments are inferred using type inference. Example, given ``` rel.{l_1 l_2} : Pi (α : Type.{l_1}) (β : α -> Type.{l_2}), (Pi x : α, β x) -> (Pi x : α, β x) -> , Prop nat : Type real : Type vec.{l} : Pi (α : Type l) (n : nat), Type.{l1} f g : Pi (n : nat), vec real n ``` then ``` mk_app_core semireducible "rel" [f, g] ``` returns the application ``` rel.{1 2} nat (fun n : nat, vec real n) f g ``` The unification constraints due to type inference are solved using the transparency `md`. -/ meta constant mk_app (fn : name) (args : list expr) (md := semireducible) : tactic expr /-- Similar to `mk_app`, but allows to specify which arguments are explicit/implicit. Example, given `(a b : nat)` then ``` mk_mapp "ite" [some (a > b), none, none, some a, some b] ``` returns the application ``` @ite.{1} (a > b) (nat.decidable_gt a b) nat a b ``` -/ meta constant mk_mapp (fn : name) (args : list (option expr)) (md := semireducible) : tactic expr /-- (mk_congr_arg h₁ h₂) is a more efficient version of (mk_app `congr_arg [h₁, h₂]) -/ meta constant mk_congr_arg : expr → expr → tactic expr /-- (mk_congr_fun h₁ h₂) is a more efficient version of (mk_app `congr_fun [h₁, h₂]) -/ meta constant mk_congr_fun : expr → expr → tactic expr /-- (mk_congr h₁ h₂) is a more efficient version of (mk_app `congr [h₁, h₂]) -/ meta constant mk_congr : expr → expr → tactic expr /-- (mk_eq_refl h) is a more efficient version of (mk_app `eq.refl [h]) -/ meta constant mk_eq_refl : expr → tactic expr /-- (mk_eq_symm h) is a more efficient version of (mk_app `eq.symm [h]) -/ meta constant mk_eq_symm : expr → tactic expr /-- (mk_eq_trans h₁ h₂) is a more efficient version of (mk_app `eq.trans [h₁, h₂]) -/ meta constant mk_eq_trans : expr → expr → tactic expr /-- (mk_eq_mp h₁ h₂) is a more efficient version of (mk_app `eq.mp [h₁, h₂]) -/ meta constant mk_eq_mp : expr → expr → tactic expr /-- (mk_eq_mpr h₁ h₂) is a more efficient version of (mk_app `eq.mpr [h₁, h₂]) -/ meta constant mk_eq_mpr : expr → expr → tactic expr /- Given a local constant t, if t has type (lhs = rhs) apply substitution. Otherwise, try to find a local constant that has type of the form (t = t') or (t' = t). The tactic fails if the given expression is not a local constant. -/ meta constant subst_core : expr → tactic unit /-- Close the current goal using `e`. Fail if the type of `e` is not definitionally equal to the target type. -/ meta constant exact (e : expr) (md := semireducible) : tactic unit /-- Elaborate the given quoted expression with respect to the current main goal. Note that this means that any implicit arguments for the given `pexpr` will be applied with fresh metavariables. If `allow_mvars` is tt, then metavariables are tolerated and become new goals if `subgoals` is tt. -/ meta constant to_expr (q : pexpr) (allow_mvars := tt) (subgoals := tt) : tactic expr /-- Return true if the given expression is a type class. -/ meta constant is_class : expr → tactic bool /-- Try to create an instance of the given type class. -/ meta constant mk_instance : expr → tactic expr /-- Change the target of the main goal. The input expression must be definitionally equal to the current target. If `check` is `ff`, then the tactic does not check whether `e` is definitionally equal to the current target. If it is not, then the error will only be detected by the kernel type checker. -/ meta constant change (e : expr) (check : bool := tt): tactic unit /-- `assert_core H T`, adds a new goal for T, and change target to `T -> target`. -/ meta constant assert_core : name → expr → tactic unit /-- `assertv_core H T P`, change target to (T -> target) if P has type T. -/ meta constant assertv_core : name → expr → expr → tactic unit /-- `define_core H T`, adds a new goal for T, and change target to `let H : T := ?M in target` in the current goal. -/ meta constant define_core : name → expr → tactic unit /-- `definev_core H T P`, change target to `let H : T := P in target` if P has type T. -/ meta constant definev_core : name → expr → expr → tactic unit /-- Rotate goals to the left. That is, `rotate_left 1` takes the main goal and puts it to the back of the subgoal list. -/ meta constant rotate_left : nat → tactic unit /-- Gets a list of metavariables, one for each goal. -/ meta constant get_goals : tactic (list expr) /-- Replace the current list of goals with the given one. Each expr in the list should be a metavariable. Any assigned metavariables will be ignored.-/ meta constant set_goals : list expr → tactic unit /-- How to order the new goals made from an `apply` tactic. Supposing we were applying `e : ∀ (a:α) (p : P(a)), Q` - `non_dep_first` would produce goals `⊢ P(?m)`, `⊢ α`. It puts the P goal at the front because none of the arguments after `p` in `e` depend on `p`. It doesn't matter what the result `Q` depends on. - `non_dep_only` would produce goal `⊢ P(?m)`. - `all` would produce goals `⊢ α`, `⊢ P(?m)`. -/ inductive new_goals | non_dep_first | non_dep_only | all /-- Configuration options for the `apply` tactic. - `md` sets how aggressively definitions are unfolded. - `new_goals` is the strategy for ordering new goals. - `instances` if `tt`, then `apply` tries to synthesize unresolved `[...]` arguments using type class resolution. - `auto_param` if `tt`, then `apply` tries to synthesize unresolved `(h : p . tac_id)` arguments using tactic `tac_id`. - `opt_param` if `tt`, then `apply` tries to synthesize unresolved `(a : t := v)` arguments by setting them to `v`. - `unify` if `tt`, then `apply` is free to assign existing metavariables in the goal when solving unification constraints. For example, in the goal `|- ?x < succ 0`, the tactic `apply succ_lt_succ` succeeds with the default configuration, but `apply_with succ_lt_succ {unify := ff}` doesn't since it would require Lean to assign `?x` to `succ ?y` where `?y` is a fresh metavariable. -/ structure apply_cfg := (md := semireducible) (approx := tt) (new_goals := new_goals.non_dep_first) (instances := tt) (auto_param := tt) (opt_param := tt) (unify := tt) /-- Apply the expression `e` to the main goal, the unification is performed using the transparency mode in `cfg`. Supposing `e : Π (a₁:α₁) ... (aₙ:αₙ), P(a₁,...,aₙ)` and the target is `Q`, `apply` will attempt to unify `Q` with `P(?a₁,...?aₙ)`. All of the metavariables that are not assigned are added as new metavariables. If `cfg.approx` is `tt`, then fallback to first-order unification, and approximate context during unification. `cfg.new_goals` specifies which unassigned metavariables become new goals, and their order. If `cfg.instances` is `tt`, then use type class resolution to instantiate unassigned meta-variables. The fields `cfg.auto_param` and `cfg.opt_param` are ignored by this tactic (See `tactic.apply`). It returns a list of all introduced meta variables and the parameter name associated with them, even the assigned ones. -/ meta constant apply_core (e : expr) (cfg : apply_cfg := {}) : tactic (list (name × expr)) /- Create a fresh meta universe variable. -/ meta constant mk_meta_univ : tactic level /- Create a fresh meta-variable with the given type. The scope of the new meta-variable is the local context of the main goal. -/ meta constant mk_meta_var : expr → tactic expr /-- Return the value assigned to the given universe meta-variable. Fail if argument is not an universe meta-variable or if it is not assigned. -/ meta constant get_univ_assignment : level → tactic level /-- Return the value assigned to the given meta-variable. Fail if argument is not a meta-variable or if it is not assigned. -/ meta constant get_assignment : expr → tactic expr /-- Return true if the given meta-variable is assigned. Fail if argument is not a meta-variable. -/ meta constant is_assigned : expr → tactic bool /-- Make a name that is guaranteed to be unique. Eg `_fresh.1001.4667`. These will be different for each run of the tactic. -/ meta constant mk_fresh_name : tactic name /-- Induction on `h` using recursor `rec`, names for the new hypotheses are retrieved from `ns`. If `ns` does not have sufficient names, then use the internal binder names in the recursor. It returns for each new goal the name of the constructor (if `rec_name` is a builtin recursor), a list of new hypotheses, and a list of substitutions for hypotheses depending on `h`. The substitutions map internal names to their replacement terms. If the replacement is again a hypothesis the user name stays the same. The internal names are only valid in the original goal, not in the type context of the new goal. Remark: if `rec_name` is not a builtin recursor, we use parameter names of `rec_name` instead of constructor names. If `rec` is none, then the type of `h` is inferred, if it is of the form `C ...`, tactic uses `C.rec` -/ meta constant induction (h : expr) (ns : list name := []) (rec : option name := none) (md := semireducible) : tactic (list (name × list expr × list (name × expr))) /-- Apply `cases_on` recursor, names for the new hypotheses are retrieved from `ns`. `h` must be a local constant. It returns for each new goal the name of the constructor, a list of new hypotheses, and a list of substitutions for hypotheses depending on `h`. The number of new goals may be smaller than the number of constructors. Some goals may be discarded when the indices to not match. See `induction` for information on the list of substitutions. The `cases` tactic is implemented using this one, and it relaxes the restriction of `h`. Note: There is one "new hypothesis" for every constructor argument. These are usually local constants, but due to dependent pattern matching, they can also be arbitrary terms. -/ meta constant cases_core (h : expr) (ns : list name := []) (md := semireducible) : tactic (list (name × list expr × list (name × expr))) /-- Similar to cases tactic, but does not revert/intro/clear hypotheses. -/ meta constant destruct (e : expr) (md := semireducible) : tactic unit /-- Generalizes the target with respect to `e`. -/ meta constant generalize (e : expr) (n : name := `_x) (md := semireducible) : tactic unit /-- instantiate assigned metavariables in the given expression -/ meta constant instantiate_mvars : expr → tactic expr /-- Add the given declaration to the environment -/ meta constant add_decl : declaration → tactic unit /-- Changes the environment to the `new_env`. The new environment does not need to be a descendant of the old one. Use with care. -/ meta constant set_env_core : environment → tactic unit /-- Changes the environment to the `new_env`. `new_env` needs to be a descendant from the current environment. -/ meta constant set_env : environment → tactic unit /-- `doc_string env d k` returns the doc string for `d` (if available) -/ meta constant doc_string : name → tactic string /-- Set the docstring for the given declaration. -/ meta constant add_doc_string : name → string → tactic unit /-- Create an auxiliary definition with name `c` where `type` and `value` may contain local constants and meta-variables. This function collects all dependencies (universe parameters, universe metavariables, local constants (aka hypotheses) and metavariables). It updates the environment in the tactic_state, and returns an expression of the form (c.{l_1 ... l_n} a_1 ... a_m) where l_i's and a_j's are the collected dependencies. -/ meta constant add_aux_decl (c : name) (type : expr) (val : expr) (is_lemma : bool) : tactic expr /-- Returns a list of all top-level (`/-! ... -/`) docstrings in the active module and imported ones. The returned object is a list of modules, indexed by `(some filename)` for imported modules and `none` for the active one, where each module in the list is paired with a list of `(position_in_file, docstring)` pairs. -/ meta constant olean_doc_strings : tactic (list (option string × (list (pos × string)))) /-- Returns a list of docstrings in the active module. An entry in the list can be either: - a top-level (`/-! ... -/`) docstring, represented as `(none, docstring)` - a declaration-specific (`/-- ... -/`) docstring, represented as `(some decl_name, docstring)` -/ meta def module_doc_strings : tactic (list (option name × string)) := do /- Obtain a list of top-level docs in current module. -/ mod_docs ← olean_doc_strings, let mod_docs: list (list (option name × string)) := mod_docs.filter_map (λ d, if d.1.is_none then some (d.2.map (λ pos_doc, ⟨none, pos_doc.2⟩)) else none), let mod_docs := mod_docs.join, /- Obtain list of declarations in current module. -/ e ← get_env, let decls := environment.fold e ([]: list name) (λ d acc, let n := d.to_name in if (environment.decl_olean e n).is_none then n::acc else acc), /- Map declarations to those which have docstrings. -/ decls ← decls.mfoldl (λa n, (doc_string n >>= λ doc, pure $ (some n, doc) :: a) <|> pure a) [], pure (mod_docs ++ decls) /-- Set attribute `attr_name` for constant `c_name` with the given priority. If the priority is none, then use default -/ meta constant set_basic_attribute (attr_name : name) (c_name : name) (persistent := ff) (prio : option nat := none) : tactic unit /-- `unset_attribute attr_name c_name` -/ meta constant unset_attribute : name → name → tactic unit /-- `has_attribute attr_name c_name` succeeds if the declaration `decl_name` has the attribute `attr_name`. The result is the priority and whether or not the attribute is persistent. -/ meta constant has_attribute : name → name → tactic (bool × nat) /-- `copy_attribute attr_name c_name p d_name` copy attribute `attr_name` from `src` to `tgt` if it is defined for `src`; make it persistent if `p` is `tt`; if `p` is `none`, the copied attribute is made persistent iff it is persistent on `src` -/ meta def copy_attribute (attr_name : name) (src : name) (tgt : name) (p : option bool := none) : tactic unit := try $ do (p', prio) ← has_attribute attr_name src, let p := p.get_or_else p', set_basic_attribute attr_name tgt p (some prio) /-- Name of the declaration currently being elaborated. -/ meta constant decl_name : tactic name /-- `save_type_info e ref` save (typeof e) at position associated with ref -/ meta constant save_type_info {elab : bool} : expr → expr elab → tactic unit meta constant save_info_thunk : pos → (unit → format) → tactic unit /-- Return list of currently open namespaces -/ meta constant open_namespaces : tactic (list name) /-- Return tt iff `t` "occurs" in `e`. The occurrence checking is performed using keyed matching with the given transparency setting. We say `t` occurs in `e` by keyed matching iff there is a subterm `s` s.t. `t` and `s` have the same head, and `is_def_eq t s md` The main idea is to minimize the number of `is_def_eq` checks performed. -/ meta constant kdepends_on (e t : expr) (md := reducible) : tactic bool /-- Abstracts all occurrences of the term `t` in `e` using keyed matching. If `unify` is `ff`, then matching is used instead of unification. That is, metavariables occurring in `e` are not assigned. -/ meta constant kabstract (e t : expr) (md := reducible) (unify := tt) : tactic expr /-- Blocks the execution of the current thread for at least `msecs` milliseconds. This tactic is used mainly for debugging purposes. -/ meta constant sleep (msecs : nat) : tactic unit /-- Type check `e` with respect to the current goal. Fails if `e` is not type correct. -/ meta constant type_check (e : expr) (md := semireducible) : tactic unit open list nat /-- A `tag` is a list of `names`. These are attached to goals to help tactics track them.-/ def tag : Type := list name /-- Enable/disable goal tagging. -/ meta constant enable_tags (b : bool) : tactic unit /-- Return tt iff goal tagging is enabled. -/ meta constant tags_enabled : tactic bool /-- Tag goal `g` with tag `t`. It does nothing if goal tagging is disabled. Remark: `set_goal g []` removes the tag -/ meta constant set_tag (g : expr) (t : tag) : tactic unit /-- Return tag associated with `g`. Return `[]` if there is no tag. -/ meta constant get_tag (g : expr) : tactic tag /-- By default, Lean only considers local instances in the header of declarations. This has two main benefits. 1- Results produced by the type class resolution procedure can be easily cached. 2- The set of local instances does not have to be recomputed. This approach has the following disadvantages: 1- Frozen local instances cannot be reverted. 2- Local instances defined inside of a declaration are not considered during type class resolution. This tactic resets the set of local instances. After executing this tactic, the set of local instances will be recomputed and the cache will be frequently reset. Note that, the cache is still used when executing a single tactic that may generate many type class resolution problems (e.g., `simp`). -/ meta constant unfreeze_local_instances : tactic unit /-- Freeze the current set of local instances. -/ meta constant freeze_local_instances : tactic unit /- Return the list of frozen local instances. Return `none` if local instances were not frozen. -/ meta constant frozen_local_instances : tactic (option (list expr)) meta def induction' (h : expr) (ns : list name := []) (rec : option name := none) (md := semireducible) : tactic unit := induction h ns rec md >> return () /-- Remark: set_goals will erase any solved goal -/ meta def cleanup : tactic unit := get_goals >>= set_goals /-- Auxiliary definition used to implement begin ... end blocks -/ meta def step {α : Type u} (t : tactic α) : tactic unit := t >>[tactic] cleanup meta def istep {α : Type u} (line0 col0 : ℕ) (line col : ℕ) (t : tactic α) : tactic unit := λ s, (@scope_trace _ line col (λ _, step t s)).clamp_pos line0 line col meta def is_prop (e : expr) : tactic bool := do t ← infer_type e, return (t = `(Prop)) /-- Return true iff n is the name of declaration that is a proposition. -/ meta def is_prop_decl (n : name) : tactic bool := do env ← get_env, d ← env.get n, t ← return $ d.type, is_prop t meta def is_proof (e : expr) : tactic bool := infer_type e >>= is_prop meta def whnf_no_delta (e : expr) : tactic expr := whnf e transparency.none /-- Return `e` in weak head normal form with respect to the given transparency setting, or `e` head is a generalized constructor or inductive datatype. -/ meta def whnf_ginductive (e : expr) (md := semireducible) : tactic expr := whnf e md ff meta def whnf_target : tactic unit := target >>= whnf >>= change /-- Change the target of the main goal. The input expression must be definitionally equal to the current target. The tactic does not check whether `e` is definitionally equal to the current target. The error will only be detected by the kernel type checker. -/ meta def unsafe_change (e : expr) : tactic unit := change e ff /-- Pi or elet introduction. Given the tactic state `⊢ Π x : α, Y`, ``intro `hello`` will produce the state `hello : α ⊢ Y[x/hello]`. Returns the new local constant. Similarly for `elet` expressions. If the target is not a Pi or elet it will try to put it in WHNF. -/ meta def intro (n : name) : tactic expr := do t ← target, if expr.is_pi t ∨ expr.is_let t then intro_core n else whnf_target >> intro_core n /-- A variant of `intro` which makes sure that the introduced hypothesis's name is unique in the context. If there is no hypothesis named `n` in the context yet, `intro_fresh n` is the same as `intro n`. If there is already a hypothesis named `n`, the new hypothesis is named `n_1` (or `n_2` if `n_1` already exists, etc.). If `offset` is given, the new names are `n_offset`, `n_offset+1` etc. If `n` is `_`, `intro_fresh n` is the same as `intro1`. The `offset` is ignored in this case. -/ meta def intro_fresh (n : name) (offset : option nat := none) : tactic expr := if n = `_ then intro `_ else do n ← get_unused_name n offset, intro n /-- Like `intro` except the name is derived from the bound name in the Π. -/ meta def intro1 : tactic expr := intro `_ /-- Repeatedly apply `intro1` and return the list of new local constants in order of introduction. -/ meta def intros : tactic (list expr) := do t ← target, match t with | expr.pi _ _ _ _ := do H ← intro1, Hs ← intros, return (H :: Hs) | expr.elet _ _ _ _ := do H ← intro1, Hs ← intros, return (H :: Hs) | _ := return [] end /-- Same as `intros`, except with the given names for the new hypotheses. Use the name ```_``` to instead use the binder's name.-/ meta def intro_lst (ns : list name) : tactic (list expr) := ns.mmap intro /-- A variant of `intro_lst` which makes sure that the introduced hypotheses' names are unique in the context. See `intro_fresh`. -/ meta def intro_lst_fresh (ns : list name) : tactic (list expr) := ns.mmap intro_fresh /-- Introduces new hypotheses with forward dependencies. -/ meta def intros_dep : tactic (list expr) := do t ← target, let proc (b : expr) := if b.has_var_idx 0 then do h ← intro1, hs ← intros_dep, return (h::hs) else -- body doesn't depend on new hypothesis return [], match t with | expr.pi _ _ _ b := proc b | expr.elet _ _ _ b := proc b | _ := return [] end meta def introv : list name → tactic (list expr) | [] := intros_dep | (n::ns) := do hs ← intros_dep, h ← intro n, hs' ← introv ns, return (hs ++ h :: hs') /-- `intron' n` introduces `n` hypotheses and returns the resulting local constants. Fails if there are not at least `n` arguments to introduce. If you do not need the return value, use `intron`. -/ meta def intron' (n : ℕ) : tactic (list expr) := iterate_exactly n intro1 /-- Like `intron'` but the introduced hypotheses' names are derived from `base`, i.e. `base`, `base_1` etc. The new names are unique in the context. If `offset` is given, the new names will be `base_offset`, `base_offset+1` etc. -/ meta def intron_base (n : ℕ) (base : name) (offset : option nat := none) : tactic (list expr) := iterate_exactly n (intro_fresh base offset) /-- `intron_with i ns base offset` introduces `i` hypotheses using the names from `ns`. If `ns` contains less than `i` names, the remaining hypotheses' names are derived from `base` and `offset` (as with `intron_base`). If `base` is `_`, the names are derived from the Π binder names. Returns the introduced local constants and the remaining names from `ns` (if `ns` contains more than `i` names). -/ meta def intron_with : ℕ → list name → opt_param name `_ → opt_param (option ℕ) none → tactic (list expr × list name) | 0 ns _ _ := pure ([], ns) | (i + 1) [] base offset := do hs ← intron_base (i + 1) base offset, pure (hs, []) | (i + 1) (n :: ns) base offset := do h ← intro n, ⟨hs, rest⟩ ← intron_with i ns base offset, pure (h :: hs, rest) /-- Returns n fully qualified if it refers to a constant, or else fails. -/ meta def resolve_constant (n : name) : tactic name := do e ← resolve_name n, match e with | expr.const n _ := pure n | _ := do e ← to_expr e tt ff, expr.const n _ ← pure $ e.get_app_fn, pure n end meta def to_expr_strict (q : pexpr) : tactic expr := to_expr q /-- Example: with `x : ℕ, h : P(x) ⊢ T(x)`, `revert x` returns `2` and produces the state ` ⊢ Π x, P(x) → T(x)`. -/ meta def revert (l : expr) : tactic nat := revert_lst [l] /- Revert "all" hypotheses. Actually, the tactic only reverts hypotheses occurring after the last frozen local instance. Recall that frozen local instances cannot be reverted. We can use `unfreeze_local_instances` to workaround this limitation. -/ meta def revert_all : tactic nat := do lctx ← local_context, lis ← frozen_local_instances, match lis with | none := revert_lst lctx | some [] := revert_lst lctx /- `hi` is the last local instance. We shoul truncate `lctx` at `hi`. -/ | some (hi::his) := revert_lst $ lctx.foldl (λ r h, if h.local_uniq_name = hi.local_uniq_name then [] else h :: r) [] end meta def clear_lst : list name → tactic unit | [] := skip | (n::ns) := do H ← get_local n, clear H, clear_lst ns meta def match_not (e : expr) : tactic expr := match (expr.is_not e) with | (some a) := return a | none := fail "expression is not a negation" end meta def match_and (e : expr) : tactic (expr × expr) := match (expr.is_and e) with | (some (α, β)) := return (α, β) | none := fail "expression is not a conjunction" end meta def match_or (e : expr) : tactic (expr × expr) := match (expr.is_or e) with | (some (α, β)) := return (α, β) | none := fail "expression is not a disjunction" end meta def match_iff (e : expr) : tactic (expr × expr) := match (expr.is_iff e) with | (some (lhs, rhs)) := return (lhs, rhs) | none := fail "expression is not an iff" end meta def match_eq (e : expr) : tactic (expr × expr) := match (expr.is_eq e) with | (some (lhs, rhs)) := return (lhs, rhs) | none := fail "expression is not an equality" end meta def match_ne (e : expr) : tactic (expr × expr) := match (expr.is_ne e) with | (some (lhs, rhs)) := return (lhs, rhs) | none := fail "expression is not a disequality" end meta def match_heq (e : expr) : tactic (expr × expr × expr × expr) := do match (expr.is_heq e) with | (some (α, lhs, β, rhs)) := return (α, lhs, β, rhs) | none := fail "expression is not a heterogeneous equality" end meta def match_refl_app (e : expr) : tactic (name × expr × expr) := do env ← get_env, match (environment.is_refl_app env e) with | (some (R, lhs, rhs)) := return (R, lhs, rhs) | none := fail "expression is not an application of a reflexive relation" end meta def match_app_of (e : expr) (n : name) : tactic (list expr) := guard (expr.is_app_of e n) >> return e.get_app_args meta def get_local_type (n : name) : tactic expr := get_local n >>= infer_type meta def trace_result : tactic unit := format_result >>= trace meta def rexact (e : expr) : tactic unit := exact e reducible meta def any_hyp_aux {α : Type} (f : expr → tactic α) : list expr → tactic α | [] := failed | (h :: hs) := f h <|> any_hyp_aux hs meta def any_hyp {α : Type} (f : expr → tactic α) : tactic α := local_context >>= any_hyp_aux f /-- `find_same_type t es` tries to find in es an expression with type definitionally equal to t -/ meta def find_same_type : expr → list expr → tactic expr | e [] := failed | e (H :: Hs) := do t ← infer_type H, (unify e t >> return H) <|> find_same_type e Hs meta def find_assumption (e : expr) : tactic expr := do ctx ← local_context, find_same_type e ctx meta def assumption : tactic unit := do { ctx ← local_context, t ← target, H ← find_same_type t ctx, exact H } <|> fail "assumption tactic failed" meta def save_info (p : pos) : tactic unit := do s ← read, tactic.save_info_thunk p (λ _, tactic_state.to_format s) notation `‹` p `›` := (by assumption : p) /-- Swap first two goals, do nothing if tactic state does not have at least two goals. -/ meta def swap : tactic unit := do gs ← get_goals, match gs with | (g₁ :: g₂ :: rs) := set_goals (g₂ :: g₁ :: rs) | e := skip end /-- `assert h t`, adds a new goal for t, and the hypothesis `h : t` in the current goal. -/ meta def assert (h : name) (t : expr) : tactic expr := do assert_core h t, swap, e ← intro h, swap, return e /-- `assertv h t v`, adds the hypothesis `h : t` in the current goal if v has type t. -/ meta def assertv (h : name) (t : expr) (v : expr) : tactic expr := assertv_core h t v >> intro h /-- `define h t`, adds a new goal for t, and the hypothesis `h : t := ?M` in the current goal. -/ meta def define (h : name) (t : expr) : tactic expr := do define_core h t, swap, e ← intro h, swap, return e /-- `definev h t v`, adds the hypothesis (h : t := v) in the current goal if v has type t. -/ meta def definev (h : name) (t : expr) (v : expr) : tactic expr := definev_core h t v >> intro h /-- Add `h : t := pr` to the current goal -/ meta def pose (h : name) (t : option expr := none) (pr : expr) : tactic expr := let dv := λt, definev h t pr in option.cases_on t (infer_type pr >>= dv) dv /-- Add `h : t` to the current goal, given a proof `pr : t` -/ meta def note (h : name) (t : option expr := none) (pr : expr) : tactic expr := let dv := λt, assertv h t pr in option.cases_on t (infer_type pr >>= dv) dv /-- Return the number of goals that need to be solved -/ meta def num_goals : tactic nat := do gs ← get_goals, return (length gs) /-- Rotate the goals to the right by `n`. That is, take the goal at the back and push it to the front `n` times. [NOTE] We have to provide the instance argument `[has_mod nat]` because mod for nat was not defined yet -/ meta def rotate_right (n : nat) [has_mod nat] : tactic unit := do ng ← num_goals, if ng = 0 then skip else rotate_left (ng - n % ng) /-- Rotate the goals to the left by `n`. That is, put the main goal to the back `n` times. -/ meta def rotate : nat → tactic unit := rotate_left private meta def repeat_aux (t : tactic unit) : list expr → list expr → tactic unit | [] r := set_goals r.reverse | (g::gs) r := do ok ← try_core (set_goals [g] >> t), match ok with | none := repeat_aux gs (g::r) | _ := do gs' ← get_goals, repeat_aux (gs' ++ gs) r end /-- This tactic is applied to each goal. If the application succeeds, the tactic is applied recursively to all the generated subgoals until it eventually fails. The recursion stops in a subgoal when the tactic has failed to make progress. The tactic `repeat` never fails. -/ meta def repeat (t : tactic unit) : tactic unit := do gs ← get_goals, repeat_aux t gs [] /-- `first [t_1, ..., t_n]` applies the first tactic that doesn't fail. The tactic fails if all t_i's fail. -/ meta def first {α : Type u} : list (tactic α) → tactic α | [] := fail "first tactic failed, no more alternatives" | (t::ts) := t <|> first ts /-- Applies the given tactic to the main goal and fails if it is not solved. -/ meta def solve1 {α} (tac : tactic α) : tactic α := do gs ← get_goals, match gs with | [] := fail "solve1 tactic failed, there isn't any goal left to focus" | (g::rs) := do set_goals [g], a ← tac, gs' ← get_goals, match gs' with | [] := set_goals rs >> pure a | gs := fail "solve1 tactic failed, focused goal has not been solved" end end /-- `solve [t_1, ... t_n]` applies the first tactic that solves the main goal. -/ meta def solve {α} (ts : list (tactic α)) : tactic α := first $ map solve1 ts private meta def focus_aux {α} : list (tactic α) → list expr → list expr → tactic (list α) | [] [] rs := set_goals rs *> pure [] | (t::ts) [] rs := fail "focus' tactic failed, insufficient number of goals" | tts (g::gs) rs := mcond (is_assigned g) (focus_aux tts gs rs) $ do set_goals [g], t::ts ← pure tts | fail "focus' tactic failed, insufficient number of tactics", a ← t, rs' ← get_goals, as ← focus_aux ts gs (rs ++ rs'), pure $ a :: as /-- `focus [t_1, ..., t_n]` applies t_i to the i-th goal. Fails if the number of goals is not n. Returns the results of t_i (one per goal). -/ meta def focus {α} (ts : list (tactic α)) : tactic (list α) := do gs ← get_goals, focus_aux ts gs [] private meta def focus'_aux : list (tactic unit) → list expr → list expr → tactic unit | [] [] rs := set_goals rs | (t::ts) [] rs := fail "focus tactic failed, insufficient number of goals" | tts (g::gs) rs := mcond (is_assigned g) (focus'_aux tts gs rs) $ do set_goals [g], t::ts ← pure tts | fail "focus tactic failed, insufficient number of tactics", t, rs' ← get_goals, focus'_aux ts gs (rs ++ rs') /-- `focus' [t_1, ..., t_n]` applies t_i to the i-th goal. Fails if the number of goals is not n. -/ meta def focus' (ts : list (tactic unit)) : tactic unit := do gs ← get_goals, focus'_aux ts gs [] meta def focus1 {α} (tac : tactic α) : tactic α := do g::gs ← get_goals, match gs with | [] := tac | _ := do set_goals [g], a ← tac, gs' ← get_goals, set_goals (gs' ++ gs), return a end private meta def all_goals_core {α} (tac : tactic α) : list expr → list expr → tactic (list α) | [] ac := set_goals ac *> pure [] | (g :: gs) ac := mcond (is_assigned g) (all_goals_core gs ac) $ do set_goals [g], a ← tac, new_gs ← get_goals, as ← all_goals_core gs (ac ++ new_gs), pure $ a :: as /-- Apply the given tactic to all goals. Return one result per goal. -/ meta def all_goals {α} (tac : tactic α) : tactic (list α) := do gs ← get_goals, all_goals_core tac gs [] private meta def all_goals'_core (tac : tactic unit) : list expr → list expr → tactic unit | [] ac := set_goals ac | (g :: gs) ac := mcond (is_assigned g) (all_goals'_core gs ac) $ do set_goals [g], tac, new_gs ← get_goals, all_goals'_core gs (ac ++ new_gs) /-- Apply the given tactic to all goals. -/ meta def all_goals' (tac : tactic unit) : tactic unit := do gs ← get_goals, all_goals'_core tac gs [] private meta def any_goals_core {α} (tac : tactic α) : list expr → list expr → bool → tactic (list (option α)) | [] ac progress := guard progress *> set_goals ac *> pure [] | (g :: gs) ac progress := mcond (is_assigned g) (any_goals_core gs ac progress) $ do set_goals [g], res ← try_core tac, new_gs ← get_goals, ress ← any_goals_core gs (ac ++ new_gs) (res.is_some || progress), pure $ res :: ress /-- Apply `tac` to any goal where it succeeds. The tactic succeeds if `tac` succeeds for at least one goal. The returned list contains the result of `tac` for each goal: `some a` if tac succeeded, or `none` if it did not. -/ meta def any_goals {α} (tac : tactic α) : tactic (list (option α)) := do gs ← get_goals, any_goals_core tac gs [] ff private meta def any_goals'_core (tac : tactic unit) : list expr → list expr → bool → tactic unit | [] ac progress := guard progress >> set_goals ac | (g :: gs) ac progress := mcond (is_assigned g) (any_goals'_core gs ac progress) $ do set_goals [g], succeeded ← try_core tac, new_gs ← get_goals, any_goals'_core gs (ac ++ new_gs) (succeeded.is_some || progress) /-- Apply the given tactic to any goal where it succeeds. The tactic succeeds only if tac succeeds for at least one goal. -/ meta def any_goals' (tac : tactic unit) : tactic unit := do gs ← get_goals, any_goals'_core tac gs [] ff /-- LCF-style AND_THEN tactic. It applies `tac1` to the main goal, then applies `tac2` to each goal produced by `tac1`. -/ meta def seq {α β} (tac1 : tactic α) (tac2 : α → tactic β) : tactic (list β) := do g::gs ← get_goals, set_goals [g], a ← tac1, bs ← all_goals $ tac2 a, gs' ← get_goals, set_goals (gs' ++ gs), pure bs /-- LCF-style AND_THEN tactic. It applies tac1, and if succeed applies tac2 to each subgoal produced by tac1 -/ meta def seq' (tac1 : tactic unit) (tac2 : tactic unit) : tactic unit := do g::gs ← get_goals, set_goals [g], tac1, all_goals' tac2, gs' ← get_goals, set_goals (gs' ++ gs) /-- Applies `tac1` to the main goal, then applies each of the tactics in `tacs2` to one of the produced subgoals (like `focus'`). -/ meta def seq_focus {α β} (tac1 : tactic α) (tacs2 : α → list (tactic β)) : tactic (list β) := do g::gs ← get_goals, set_goals [g], a ← tac1, bs ← focus $ tacs2 a, gs' ← get_goals, set_goals (gs' ++ gs), pure bs /-- Applies `tac1` to the main goal, then applies each of the tactics in `tacs2` to one of the produced subgoals (like `focus`). -/ meta def seq_focus' (tac1 : tactic unit) (tacs2 : list (tactic unit)) : tactic unit := do g::gs ← get_goals, set_goals [g], tac1, focus tacs2, gs' ← get_goals, set_goals (gs' ++ gs) meta instance andthen_seq : has_andthen (tactic unit) (tactic unit) (tactic unit) := ⟨seq'⟩ meta instance andthen_seq_focus : has_andthen (tactic unit) (list (tactic unit)) (tactic unit) := ⟨seq_focus'⟩ meta constant is_trace_enabled_for : name → bool /-- Execute tac only if option trace.n is set to true. -/ meta def when_tracing (n : name) (tac : tactic unit) : tactic unit := when (is_trace_enabled_for n = tt) tac /-- Fail if there are no remaining goals. -/ meta def fail_if_no_goals : tactic unit := do n ← num_goals, when (n = 0) (fail "tactic failed, there are no goals to be solved") /-- Fail if there are unsolved goals. -/ meta def done : tactic unit := do n ← num_goals, when (n ≠ 0) (fail "done tactic failed, there are unsolved goals") meta def apply_opt_param : tactic unit := do `(opt_param %%t %%v) ← target, exact v meta def apply_auto_param : tactic unit := do `(auto_param %%type %%tac_name_expr) ← target, change type, tac_name ← eval_expr name tac_name_expr, tac ← eval_expr (tactic unit) (expr.const tac_name []), tac meta def has_opt_auto_param (ms : list expr) : tactic bool := ms.mfoldl (λ r m, do type ← infer_type m, return $ r || type.is_napp_of `opt_param 2 || type.is_napp_of `auto_param 2) ff meta def try_apply_opt_auto_param (cfg : apply_cfg) (ms : list expr) : tactic unit := when (cfg.auto_param || cfg.opt_param) $ mwhen (has_opt_auto_param ms) $ do gs ← get_goals, ms.mmap' (λ m, mwhen (bnot <$> is_assigned m) $ set_goals [m] >> when cfg.opt_param (try apply_opt_param) >> when cfg.auto_param (try apply_auto_param)), set_goals gs meta def has_opt_auto_param_for_apply (ms : list (name × expr)) : tactic bool := ms.mfoldl (λ r m, do type ← infer_type m.2, return $ r || type.is_napp_of `opt_param 2 || type.is_napp_of `auto_param 2) ff meta def try_apply_opt_auto_param_for_apply (cfg : apply_cfg) (ms : list (name × expr)) : tactic unit := mwhen (has_opt_auto_param_for_apply ms) $ do gs ← get_goals, ms.mmap' (λ m, mwhen (bnot <$> (is_assigned m.2)) $ set_goals [m.2] >> when cfg.opt_param (try apply_opt_param) >> when cfg.auto_param (try apply_auto_param)), set_goals gs meta def apply (e : expr) (cfg : apply_cfg := {}) : tactic (list (name × expr)) := do r ← apply_core e cfg, try_apply_opt_auto_param_for_apply cfg r, return r /-- Same as `apply` but __all__ arguments that weren't inferred are added to goal list. -/ meta def fapply (e : expr) : tactic (list (name × expr)) := apply e {new_goals := new_goals.all} /-- Same as `apply` but only goals that don't depend on other goals are added to goal list. -/ meta def eapply (e : expr) : tactic (list (name × expr)) := apply e {new_goals := new_goals.non_dep_only} /-- Try to solve the main goal using type class resolution. -/ meta def apply_instance : tactic unit := do tgt ← target >>= instantiate_mvars, b ← is_class tgt, if b then mk_instance tgt >>= exact else fail "apply_instance tactic fail, target is not a type class" /-- Create a list of universe meta-variables of the given size. -/ meta def mk_num_meta_univs : nat → tactic (list level) | 0 := return [] | (succ n) := do l ← mk_meta_univ, ls ← mk_num_meta_univs n, return (l::ls) /-- Return `expr.const c [l_1, ..., l_n]` where l_i's are fresh universe meta-variables. -/ meta def mk_const (c : name) : tactic expr := do env ← get_env, decl ← env.get c, let num := decl.univ_params.length, ls ← mk_num_meta_univs num, return (expr.const c ls) /-- Apply the constant `c` -/ meta def applyc (c : name) (cfg : apply_cfg := {}) : tactic unit := do c ← mk_const c, apply c cfg, skip meta def eapplyc (c : name) : tactic unit := do c ← mk_const c, eapply c, skip meta def save_const_type_info (n : name) {elab : bool} (ref : expr elab) : tactic unit := try (do c ← mk_const n, save_type_info c ref) /-- Create a fresh universe `?u`, a metavariable `?T : Type.{?u}`, and return metavariable `?M : ?T`. This action can be used to create a meta-variable when we don't know its type at creation time -/ meta def mk_mvar : tactic expr := do u ← mk_meta_univ, t ← mk_meta_var (expr.sort u), mk_meta_var t /-- Makes a sorry macro with a meta-variable as its type. -/ meta def mk_sorry : tactic expr := do u ← mk_meta_univ, t ← mk_meta_var (expr.sort u), return $ expr.mk_sorry t /-- Closes the main goal using sorry. -/ meta def admit : tactic unit := target >>= exact ∘ expr.mk_sorry meta def mk_local' (pp_name : name) (bi : binder_info) (type : expr) : tactic expr := do uniq_name ← mk_fresh_name, return $ expr.local_const uniq_name pp_name bi type meta def mk_local_def (pp_name : name) (type : expr) : tactic expr := mk_local' pp_name binder_info.default type meta def mk_local_pis : expr → tactic (list expr × expr) | (expr.pi n bi d b) := do p ← mk_local' n bi d, (ps, r) ← mk_local_pis (expr.instantiate_var b p), return ((p :: ps), r) | e := return ([], e) private meta def get_pi_arity_aux : expr → tactic nat | (expr.pi n bi d b) := do m ← mk_fresh_name, let l := expr.local_const m n bi d, new_b ← whnf (expr.instantiate_var b l), r ← get_pi_arity_aux new_b, return (r + 1) | e := return 0 /-- Compute the arity of the given (Pi-)type -/ meta def get_pi_arity (type : expr) : tactic nat := whnf type >>= get_pi_arity_aux /-- Compute the arity of the given function -/ meta def get_arity (fn : expr) : tactic nat := infer_type fn >>= get_pi_arity meta def triv : tactic unit := mk_const `trivial >>= exact notation `dec_trivial` := of_as_true (by tactic.triv) meta def by_contradiction (H : option name := none) : tactic expr := do tgt : expr ← target, (match_not tgt >> return ()) <|> (mk_mapp `decidable.by_contradiction [some tgt, none] >>= eapply >> skip) <|> fail "tactic by_contradiction failed, target is not a negation nor a decidable proposition (remark: when 'local attribute [instance] classical.prop_decidable' is used, all propositions are decidable)", match H with | some n := intro n | none := intro1 end private meta def generalizes_aux (md : transparency) : list expr → tactic unit | [] := skip | (e::es) := generalize e `x md >> generalizes_aux es meta def generalizes (es : list expr) (md := semireducible) : tactic unit := generalizes_aux md es private meta def kdependencies_core (e : expr) (md : transparency) : list expr → list expr → tactic (list expr) | [] r := return r | (h::hs) r := do type ← infer_type h, d ← kdepends_on type e md, if d then kdependencies_core hs (h::r) else kdependencies_core hs r /-- Return all hypotheses that depends on `e` The dependency test is performed using `kdepends_on` with the given transparency setting. -/ meta def kdependencies (e : expr) (md := reducible) : tactic (list expr) := do ctx ← local_context, kdependencies_core e md ctx [] /-- Revert all hypotheses that depend on `e` -/ meta def revert_kdependencies (e : expr) (md := reducible) : tactic nat := kdependencies e md >>= revert_lst meta def revert_kdeps (e : expr) (md := reducible) := revert_kdependencies e md /-- Postprocess the output of `cases_core`: - The third component of each tuple in the input list (the list of substitutions) is dropped since we don't use it anywhere. - The second component (the list of new hypotheses) is filtered: any expression that is not a local constant is dropped. We only use the new hypotheses for the renaming functionality of `case`, so we want to keep only those "new hypotheses" that are, in fact, local constants. -/ private meta def cases_postprocess (hs : list (name × list expr × list (name × expr))) : list (name × list expr) := hs.map $ λ ⟨n, hs, _⟩, (n, hs.filter (λ h, h.is_local_constant)) /-- Similar to `cases_core`, but `e` doesn't need to be a hypothesis. Remark, it reverts dependencies using `revert_kdeps`. Two different transparency modes are used `md` and `dmd`. The mode `md` is used with `cases_core` and `dmd` with `generalize` and `revert_kdeps`. It returns the constructor names associated with each new goal and the newly introduced hypotheses. Note that while `cases_core` may return "new hypotheses" that are not local constants, this tactic only returns local constants. -/ meta def cases (e : expr) (ids : list name := []) (md := semireducible) (dmd := semireducible) : tactic (list (name × list expr)) := if e.is_local_constant then do r ← cases_core e ids md, return $ cases_postprocess r else do n ← revert_kdependencies e dmd, x ← get_unused_name, (tactic.generalize e x dmd) <|> (do t ← infer_type e, tactic.assertv x t e, get_local x >>= tactic.revert, return ()), h ← tactic.intro1, focus1 $ do r ← cases_core h ids md, hs' ← all_goals (intron' n), return $ cases_postprocess $ r.map₂ (λ ⟨n, hs, x⟩ hs', (n, hs ++ hs', x)) hs' /-- The same as `exact` except you can add proof holes. -/ meta def refine (e : pexpr) : tactic unit := do tgt : expr ← target, to_expr ``(%%e : %%tgt) tt >>= exact /-- `by_cases p h` splits the main goal into two cases, assuming `h : p` in the first branch, and `h : ¬ p` in the second branch. The expression `p` needs to be a proposition. The produced proof term is `dite p ?m_1 ?m_2`. -/ meta def by_cases (e : expr) (h : name) : tactic unit := do dec_e ← mk_app ``decidable [e] <|> fail "by_cases tactic failed, type is not a proposition", inst ← mk_instance dec_e <|> pure `(classical.prop_decidable %%e), tgt ← target, expr.sort tgt_u ← infer_type tgt >>= whnf, g1 ← mk_meta_var (e.imp tgt), g2 ← mk_meta_var (`(¬ %%e).imp tgt), focus1 $ do exact $ expr.const ``dite [tgt_u] e inst tgt g1 g2, set_goals [g1, g2], all_goals' $ intro h >> skip meta def funext_core : list name → bool → tactic unit | [] tt := return () | ids only_ids := try $ do some (lhs, rhs) ← expr.is_eq <$> (target >>= whnf), applyc `funext, id ← if ids.empty ∨ ids.head = `_ then do (expr.lam n _ _ _) ← whnf lhs | pure `_, return n else return ids.head, intro id, funext_core ids.tail only_ids meta def funext : tactic unit := funext_core [] ff meta def funext_lst (ids : list name) : tactic unit := funext_core ids tt private meta def get_undeclared_const (env : environment) (base : name) : ℕ → name | i := let n := base <.> ("_aux_" ++ repr i) in if ¬env.contains n then n else get_undeclared_const (i+1) meta def new_aux_decl_name : tactic name := do env ← get_env, n ← decl_name, return $ get_undeclared_const env n 1 private meta def mk_aux_decl_name : option name → tactic name | none := new_aux_decl_name | (some suffix) := do p ← decl_name, return $ p ++ suffix meta def abstract (tac : tactic unit) (suffix : option name := none) (zeta_reduce := tt) : tactic unit := do fail_if_no_goals, gs ← get_goals, type ← if zeta_reduce then target >>= zeta else target, is_lemma ← is_prop type, m ← mk_meta_var type, set_goals [m], tac, n ← num_goals, when (n ≠ 0) (fail "abstract tactic failed, there are unsolved goals"), set_goals gs, val ← instantiate_mvars m, val ← if zeta_reduce then zeta val else return val, c ← mk_aux_decl_name suffix, e ← add_aux_decl c type val is_lemma, exact e /-- `solve_aux type tac` synthesize an element of 'type' using tactic 'tac' -/ meta def solve_aux {α : Type} (type : expr) (tac : tactic α) : tactic (α × expr) := do m ← mk_meta_var type, gs ← get_goals, set_goals [m], a ← tac, set_goals gs, return (a, m) /-- Return tt iff 'd' is a declaration in one of the current open namespaces -/ meta def in_open_namespaces (d : name) : tactic bool := do ns ← open_namespaces, env ← get_env, return $ ns.any (λ n, n.is_prefix_of d) && env.contains d /-- Execute tac for 'max' "heartbeats". The heartbeat is approx. the maximum number of memory allocations (in thousands) performed by 'tac'. This is a deterministic way of interrupting long running tactics. -/ meta def try_for {α} (max : nat) (tac : tactic α) : tactic α := λ s, match _root_.try_for max (tac s) with | some r := r | none := mk_exception "try_for tactic failed, timeout" none s end meta def updateex_env (f : environment → exceptional environment) : tactic unit := do env ← get_env, env ← returnex $ f env, set_env env /- Add a new inductive datatype to the environment name, universe parameters, number of parameters, type, constructors (name and type), is_meta -/ meta def add_inductive (n : name) (ls : list name) (p : nat) (ty : expr) (is : list (name × expr)) (is_meta : bool := ff) : tactic unit := updateex_env $ λe, e.add_inductive n ls p ty is is_meta meta def add_meta_definition (n : name) (lvls : list name) (type value : expr) : tactic unit := add_decl (declaration.defn n lvls type value reducibility_hints.abbrev ff) /-- add declaration `d` as a protected declaration -/ meta def add_protected_decl (d : declaration) : tactic unit := updateex_env $ λ e, e.add_protected d /-- check if `n` is the name of a protected declaration -/ meta def is_protected_decl (n : name) : tactic bool := do env ← get_env, return $ env.is_protected n /-- `add_defn_equations` adds a definition specified by a list of equations. The arguments: * `lp`: list of universe parameters * `params`: list of parameters (binders before the colon); * `fn`: a local constant giving the name and type of the declaration (with `params` in the local context); * `eqns`: a list of equations, each of which is a list of patterns (constructors applied to new local constants) and the branch expression; * `is_meta`: is the definition meta? `add_defn_equations` can be used as: do my_add ← mk_local_def `my_add `(ℕ → ℕ), a ← mk_local_def `a ℕ, b ← mk_local_def `b ℕ, add_defn_equations [a] my_add [ ([``(nat.zero)], a), ([``(nat.succ %%b)], my_add b) ]) ff -- non-meta to create the following definition: def my_add (a : ℕ) : ℕ → ℕ | nat.zero := a | (nat.succ b) := my_add b -/ meta def add_defn_equations (lp : list name) (params : list expr) (fn : expr) (eqns : list (list pexpr × expr)) (is_meta : bool) : tactic unit := do opt ← get_options, updateex_env $ λ e, e.add_defn_eqns opt lp params fn eqns is_meta /-- Get the revertible part of the local context. These are the hypotheses that appear after the last frozen local instance in the local context. We call them revertible because `revert` can revert them, unlike those hypotheses which occur before a frozen instance. -/ meta def revertible_local_context : tactic (list expr) := do ctx ← local_context, frozen ← frozen_local_instances, pure $ match frozen with | none := ctx | some [] := ctx | some (h :: _) := ctx.after (eq h) end /-- Rename local hypotheses according to the given `name_map`. The `name_map` contains as keys those hypotheses that should be renamed; the associated values are the new names. This tactic can only rename hypotheses which occur after the last frozen local instance. If you need to rename earlier hypotheses, try `unfreeze_local_instances`. If `strict` is true, we fail if `name_map` refers to hypotheses that do not appear in the local context or that appear before a frozen local instance. Conversely, if `strict` is false, some entries of `name_map` may be silently ignored. If `use_unique_names` is true, the keys of `name_map` should be the unique names of hypotheses to be renamed. Otherwise, the keys should be display names. Note that we allow shadowing, so renamed hypotheses may have the same name as other hypotheses in the context. If `use_unique_names` is false and there are multiple hypotheses with the same display name in the context, they are all renamed. -/ meta def rename_many (renames : name_map name) (strict := tt) (use_unique_names := ff) : tactic unit := do let hyp_name : expr → name := if use_unique_names then expr.local_uniq_name else expr.local_pp_name, ctx ← revertible_local_context, -- The part of the context after (but including) the first hypthesis that -- must be renamed. let ctx_suffix := ctx.drop_while (λ h, (renames.find $ hyp_name h).is_none), when strict $ do { let ctx_names := rb_map.set_of_list (ctx_suffix.map hyp_name), let invalid_renames := (renames.to_list.map prod.fst).filter (λ h, ¬ ctx_names.contains h), when ¬ invalid_renames.empty $ fail $ format.join [ "Cannot rename these hypotheses:\n" , format.join $ (invalid_renames.map to_fmt).intersperse ", " , format.line , "This is because these hypotheses either do not occur in the\n" , "context or they occur before a frozen local instance.\n" , "In the latter case, try `tactic.unfreeze_local_instances`." ] }, -- The new names for all hypotheses in ctx_suffix. let new_names := ctx_suffix.map $ λ h, (renames.find $ hyp_name h).get_or_else h.local_pp_name, revert_lst ctx_suffix, intro_lst new_names, pure () /-- Rename a local hypothesis. This is a special case of `rename_many`; see there for caveats. -/ meta def rename (curr : name) (new : name) : tactic unit := rename_many (rb_map.of_list [⟨curr, new⟩]) /-- Rename a local hypothesis. Unlike `rename` and `rename_many`, this tactic does not preserve the order of hypotheses. Its implementation is simpler (and therefore probably faster) than that of `rename`. -/ meta def rename_unstable (curr : name) (new : name) : tactic unit := do h ← get_local curr, n ← revert h, intro new, intron (n - 1) /-- "Replace" hypothesis `h : type` with `h : new_type` where `eq_pr` is a proof that (type = new_type). The tactic actually creates a new hypothesis with the same user facing name, and (tries to) clear `h`. The `clear` step fails if `h` has forward dependencies. In this case, the old `h` will remain in the local context. The tactic returns the new hypothesis. -/ meta def replace_hyp (h : expr) (new_type : expr) (eq_pr : expr) : tactic expr := do h_type ← infer_type h, new_h ← assert h.local_pp_name new_type, mk_eq_mp eq_pr h >>= exact, try $ clear h, return new_h meta def main_goal : tactic expr := do g::gs ← get_goals, return g /- Goal tagging support -/ meta def with_enable_tags {α : Type} (t : tactic α) (b := tt) : tactic α := do old ← tags_enabled, enable_tags b, r ← t, enable_tags old, return r meta def get_main_tag : tactic tag := main_goal >>= get_tag meta def set_main_tag (t : tag) : tactic unit := do g ← main_goal, set_tag g t meta def subst (h : expr) : tactic unit := (do guard h.is_local_constant, some (α, lhs, β, rhs) ← expr.is_heq <$> infer_type h, is_def_eq α β, new_h_type ← mk_app `eq [lhs, rhs], new_h_pr ← mk_app `eq_of_heq [h], new_h ← assertv h.local_pp_name new_h_type new_h_pr, try (clear h), subst_core new_h) <|> subst_core h end tactic notation [parsing_only] `command`:max := tactic unit open tactic namespace list meta def for_each {α} : list α → (α → tactic unit) → tactic unit | [] fn := skip | (e::es) fn := do fn e, for_each es fn meta def any_of {α β} : list α → (α → tactic β) → tactic β | [] fn := failed | (e::es) fn := do opt_b ← try_core (fn e), match opt_b with | some b := return b | none := any_of es fn end end list /- Install monad laws tactic and use it to prove some instances. -/ /-- Try to prove with `iff.refl`.-/ meta def order_laws_tac := whnf_target >> intros >> to_expr ``(iff.refl _) >>= exact meta def monad_from_pure_bind {m : Type u → Type v} (pure : Π {α : Type u}, α → m α) (bind : Π {α β : Type u}, m α → (α → m β) → m β) : monad m := {pure := @pure, bind := @bind} meta instance : monad task := {map := @task.map, bind := @task.bind, pure := @task.pure} namespace tactic meta def mk_id_proof (prop : expr) (pr : expr) : expr := expr.app (expr.app (expr.const ``id [level.zero]) prop) pr meta def mk_id_eq (lhs : expr) (rhs : expr) (pr : expr) : tactic expr := do prop ← mk_app `eq [lhs, rhs], return $ mk_id_proof prop pr meta def replace_target (new_target : expr) (pr : expr) : tactic unit := do t ← target, assert `htarget new_target, swap, ht ← get_local `htarget, locked_pr ← mk_id_eq t new_target pr, mk_eq_mpr locked_pr ht >>= exact meta def eval_pexpr (α) [reflected α] (e : pexpr) : tactic α := to_expr ``(%%e : %%(reflect α)) ff ff >>= eval_expr α meta def run_simple {α} : tactic_state → tactic α → option α | ts t := match t ts with | (interaction_monad.result.success a ts') := some a | (interaction_monad.result.exception _ _ _) := none end end tactic
0edc99e189842691339fb75126e3bfe30221b3b7
6fca17f8d5025f89be1b2d9d15c9e0c4b4900cbf
/src/game/world8/level1.lean
87d3829d0beac1555285e53efef439cb00da31da
[ "Apache-2.0" ]
permissive
arolihas/natural_number_game
4f0c93feefec93b8824b2b96adff8b702b8b43ce
8e4f7b4b42888a3b77429f90cce16292bd288138
refs/heads/master
1,621,872,426,808
1,586,270,467,000
1,586,270,467,000
253,648,466
0
0
null
1,586,219,694,000
1,586,219,694,000
null
UTF-8
Lean
false
false
1,823
lean
import mynat.definition -- hide import mynat.add -- hide import game.world2.level6 -- hide namespace mynat -- hide /- Axiom : succ_inj {a b : mynat} : succ(a) = succ(b) → a = b -/ /- # Advanced Addition World ## Level 1: `succ_inj`. A function. Peano's original collection of axioms for the natural numbers contained two further assumptions, which have not yet been mentioned in the game: ``` succ_inj {a b : mynat} : succ(a) = succ(b) → a = b zero_ne_succ (a : mynat) : zero ≠ succ(a) ``` The reason they have not been used yet is that they are both implications, that is, of the form $P\implies Q$. This is clear for `succ_inj a b`, which says that for all $a$ and $b$ we have $succ(a)=succ(b)\implies a=b$. For `zero_ne_succ` the trick is that $X\ne Y$ is *defined to mean* $X = Y\implies{\tt false}$. If you have played through Proposition world, you now have the required Lean skills (i.e., you know the required tactics) to work with these implications. Let's finally learn how to use `succ_inj`. You should know a couple of ways to prove the below -- one directly using an `exact`, and one which uses an `apply` first. -/ /- Theorem : no-side-bar For all naturals $a$ and $b$, if we assume $succ(a)=succ(b)$, then we can deduce $a=b$. -/ theorem succ_inj' {a b : mynat} (hs : succ(a) = succ(b)) : a = b := begin [nat_num_game] end /- ## Important thing. You can rewrite proofs of *equalities*. If `h : A = B` then `rw h` changes `A`s to `B`s. But you *cannot rewrite proofs of implications*. `rw succ_inj` will *never work* because `succ_inj` isn't of the form $A = B$, it's of the form $A\implies B$. This is one of the most common mistakes I see from beginners. $\implies$ and $=$ are *two different things* and you need to be clear about which one you are using. -/ end mynat -- hide
c777b0d26b5cfb2a1e13b2c7f85cf4b6a8c89622
13133fade54057ee588bc056e4eaa14a24773d23
/Definitions/fibonacci_def.lean
bc7d24c6a91c6fea26a4dacb1799c937267cc33a
[]
no_license
lkloh/lean-project-15815
444cbdca1d1a2dfa258c76c41a6ff846392e13d1
2cb657c0e41baa318193f7dce85974ff37d80883
refs/heads/master
1,611,402,038,933
1,432,020,760,000
1,432,020,760,000
33,372,120
0
0
null
1,431,932,928,000
1,428,078,840,000
Lean
UTF-8
Lean
false
false
714
lean
import data.nat open nat -- source: the official tutorial for lean definition fib : nat → nat | fib 0 := 1 | fib 1 := 1 | fib (a+2) := fib (a+1) + fib a -- The defining equations hold definitionally example : fib 0 = 1 := rfl example : fib 1 = 1 := rfl example (a : nat) : fib (a+2) = fib (a+1) + fib a := rfl -- fib is always positive theorem fib_pos : ∀ n, 0 < fib n | fib_pos 0 := show 0 < 1, from zero_lt_succ 0 | fib_pos 1 := show 0 < 1, from zero_lt_succ 0 | fib_pos (a+2) := calc 0 = 0 + 0 : rfl ... < fib (a+1) + 0 : add_lt_add_right (fib_pos (a+1)) 0 ... < fib (a+1) + fib a : add_lt_add_left (fib_pos a) (fib (a+1)) ... = fib (a+2) : rfl
54e49c43a9c238366092903c66ff2b583deed2f5
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/simp_except.lean
c53f70dbbcf51578e4a91a8ac7350b3701e49717
[ "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
848
lean
constant f : nat → nat namespace foo axiom fax : ∀ x, f x = x end foo attribute [simp] foo.fax example (a : nat) : f a = a := by simp -- works example (a : nat) : f a = a := by simp [-fax] -- Error: unknown identifier 'fax' example (a : nat) : f a = a := by simp [-foo.fax] -- Error: simplify failed to simplify section open foo example (a : nat) : f a = a := by simp [-fax] -- Error: simplify failed to simplify end example (a : nat) (h : a = 0) : a = 0 := by simp [-h] -- Error: invalid local exception h, '*' was not used example (a : nat) (h : a = 0) : a = 0 := by simp [*, -h] -- Error: simplify failed to simplify example (a : nat) (h : a = 0) : a = 0 := by simp [*] -- works example (a b : nat) (h : b = 0) (h₁ : a = b) (h₂ : b = a) : b = 0 := by simp [*, -h₁, -h₂] -- we can prevent loops by removing "bad" hypotheses
97e5952397fa0594360170fa01b5ecf19c799312
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/ring_theory/subring_auto.lean
a0f70ed496a31144cefcf3bcf183226b89bd596f
[]
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
32,682
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 Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.deprecated.subring import Mathlib.group_theory.subgroup import Mathlib.ring_theory.subsemiring import Mathlib.PostPort universes u l u_1 u_2 v w namespace Mathlib /-! # 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 -/ /-- `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 where /-- Reinterpret a `subring` as a `subsemiring`. -/ /-- Reinterpret a `subring` as an `add_subgroup`. -/ namespace subring /-- The underlying submonoid of a subring. -/ def to_submonoid {R : Type u} [ring R] (s : subring R) : submonoid R := submonoid.mk (carrier s) sorry sorry protected instance set.has_coe {R : Type u} [ring R] : has_coe (subring R) (set R) := has_coe.mk carrier protected instance has_mem {R : Type u} [ring R] : has_mem R (subring R) := has_mem.mk fun (m : R) (S : subring R) => m ∈ ↑S protected instance has_coe_to_sort {R : Type u} [ring R] : has_coe_to_sort (subring R) := has_coe_to_sort.mk (Type u) fun (S : subring R) => Subtype fun (x : R) => x ∈ S /-- 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' {R : Type u} [ring R] (s : set R) (sm : submonoid R) (sa : add_subgroup R) (hm : ↑sm = s) (ha : ↑sa = s) : subring R := mk s sorry sorry sorry sorry sorry @[simp] theorem coe_mk' {R : Type u} [ring R] {s : set R} {sm : submonoid R} (hm : ↑sm = s) {sa : add_subgroup R} (ha : ↑sa = s) : ↑(subring.mk' s sm sa hm ha) = s := rfl @[simp] theorem mem_mk' {R : Type u} [ring R] {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] theorem mk'_to_submonoid {R : Type u} [ring R] {s : set R} {sm : submonoid R} (hm : ↑sm = s) {sa : add_subgroup R} (ha : ↑sa = s) : to_submonoid (subring.mk' s sm sa hm ha) = sm := submonoid.ext' (Eq.symm hm) @[simp] theorem mk'_to_add_subgroup {R : Type u} [ring R] {s : set R} {sm : submonoid R} (hm : ↑sm = s) {sa : add_subgroup R} (ha : ↑sa = s) : to_add_subgroup (subring.mk' s sm sa hm ha) = sa := add_subgroup.ext' (Eq.symm ha) end subring /-- Construct a `subring` from a set satisfying `is_subring`. -/ def set.to_subring {R : Type u} [ring R] (S : set R) [is_subring S] : subring R := subring.mk S sorry sorry sorry sorry sorry protected theorem subring.exists {R : Type u} [ring R] {s : subring R} {p : ↥s → Prop} : (∃ (x : ↥s), p x) ↔ ∃ (x : R), ∃ (H : x ∈ s), p { val := x, property := H } := set_coe.exists protected theorem subring.forall {R : Type u} [ring R] {s : subring R} {p : ↥s → Prop} : (∀ (x : ↥s), p x) ↔ ∀ (x : R) (H : x ∈ s), p { val := x, property := H } := set_coe.forall /-- A `subsemiring` containing -1 is a `subring`. -/ def subsemiring.to_subring {R : Type u} [ring R] (s : subsemiring R) (hneg : -1 ∈ s) : subring R := subring.mk (submonoid.carrier (subsemiring.to_submonoid s)) sorry sorry sorry sorry sorry namespace subring /-- Two subrings are equal if the underlying subsets are equal. -/ theorem ext' {R : Type u} [ring R] {s : subring R} {t : subring R} (h : ↑s = ↑t) : s = t := sorry /-- Two subrings are equal if and only if the underlying subsets are equal. -/ protected theorem ext'_iff {R : Type u} [ring R] {s : subring R} {t : subring R} : s = t ↔ ↑s = ↑t := { mp := fun (h : s = t) => h ▸ rfl, mpr := fun (h : ↑s = ↑t) => ext' h } /-- Two subrings are equal if they have the same elements. -/ theorem ext {R : Type u} [ring R] {S : subring R} {T : subring R} (h : ∀ (x : R), x ∈ S ↔ x ∈ T) : S = T := ext' (set.ext h) /-- A subring contains the ring's 1. -/ theorem one_mem {R : Type u} [ring R] (s : subring R) : 1 ∈ s := one_mem' s /-- A subring contains the ring's 0. -/ theorem zero_mem {R : Type u} [ring R] (s : subring R) : 0 ∈ s := zero_mem' s /-- A subring is closed under multiplication. -/ theorem mul_mem {R : Type u} [ring R] (s : subring R) {x : R} {y : R} : x ∈ s → y ∈ s → x * y ∈ s := mul_mem' s /-- A subring is closed under addition. -/ theorem add_mem {R : Type u} [ring R] (s : subring R) {x : R} {y : R} : x ∈ s → y ∈ s → x + y ∈ s := add_mem' s /-- A subring is closed under negation. -/ theorem neg_mem {R : Type u} [ring R] (s : subring R) {x : R} : x ∈ s → -x ∈ s := neg_mem' s /-- A subring is closed under subtraction -/ theorem sub_mem {R : Type u} [ring R] (s : subring R) {x : R} {y : R} (hx : x ∈ s) (hy : y ∈ s) : x - y ∈ s := eq.mpr (id (Eq._oldrec (Eq.refl (x - y ∈ s)) (sub_eq_add_neg x y))) (add_mem s hx (neg_mem s hy)) /-- Product of a list of elements in a subring is in the subring. -/ theorem list_prod_mem {R : Type u} [ring R] (s : subring R) {l : List R} : (∀ (x : R), x ∈ l → x ∈ s) → list.prod l ∈ s := submonoid.list_prod_mem (to_submonoid s) /-- Sum of a list of elements in a subring is in the subring. -/ theorem list_sum_mem {R : Type u} [ring R] (s : subring R) {l : List R} : (∀ (x : R), x ∈ l → x ∈ s) → list.sum l ∈ s := add_subgroup.list_sum_mem (to_add_subgroup s) /-- Product of a multiset of elements in a subring of a `comm_ring` is in the subring. -/ theorem multiset_prod_mem {R : Type u_1} [comm_ring R] (s : subring R) (m : multiset R) : (∀ (a : R), a ∈ m → a ∈ s) → multiset.prod m ∈ s := submonoid.multiset_prod_mem (to_submonoid s) m /-- Sum of a multiset of elements in an `subring` of a `ring` is in the `subring`. -/ theorem multiset_sum_mem {R : Type u_1} [ring R] (s : subring R) (m : multiset R) : (∀ (a : R), a ∈ m → a ∈ s) → multiset.sum m ∈ s := add_subgroup.multiset_sum_mem (to_add_subgroup s) m /-- Product of elements of a subring of a `comm_ring` indexed by a `finset` is in the subring. -/ theorem prod_mem {R : Type u_1} [comm_ring R] (s : subring R) {ι : Type u_2} {t : finset ι} {f : ι → R} (h : ∀ (c : ι), c ∈ t → f c ∈ s) : (finset.prod t fun (i : ι) => f i) ∈ s := submonoid.prod_mem (to_submonoid s) h /-- Sum of elements in a `subring` of a `ring` indexed by a `finset` is in the `subring`. -/ theorem sum_mem {R : Type u_1} [ring R] (s : subring R) {ι : Type u_2} {t : finset ι} {f : ι → R} (h : ∀ (c : ι), c ∈ t → f c ∈ s) : (finset.sum t fun (i : ι) => f i) ∈ s := add_subgroup.sum_mem (to_add_subgroup s) h theorem pow_mem {R : Type u} [ring R] (s : subring R) {x : R} (hx : x ∈ s) (n : ℕ) : x ^ n ∈ s := submonoid.pow_mem (to_submonoid s) hx n theorem gsmul_mem {R : Type u} [ring R] (s : subring R) {x : R} (hx : x ∈ s) (n : ℤ) : n •ℤ x ∈ s := add_subgroup.gsmul_mem (to_add_subgroup s) hx n theorem coe_int_mem {R : Type u} [ring R] (s : subring R) (n : ℤ) : ↑n ∈ s := sorry /-- A subring of a ring inherits a ring structure -/ protected instance to_ring {R : Type u} [ring R] (s : subring R) : ring ↥s := ring.mk add_comm_group.add sorry add_comm_group.zero sorry sorry add_comm_group.neg add_comm_group.sub sorry sorry monoid.mul sorry monoid.one sorry sorry sorry sorry @[simp] theorem coe_add {R : Type u} [ring R] (s : subring R) (x : ↥s) (y : ↥s) : ↑(x + y) = ↑x + ↑y := rfl @[simp] theorem coe_neg {R : Type u} [ring R] (s : subring R) (x : ↥s) : ↑(-x) = -↑x := rfl @[simp] theorem coe_mul {R : Type u} [ring R] (s : subring R) (x : ↥s) (y : ↥s) : ↑(x * y) = ↑x * ↑y := rfl @[simp] theorem coe_zero {R : Type u} [ring R] (s : subring R) : ↑0 = 0 := rfl @[simp] theorem coe_one {R : Type u} [ring R] (s : subring R) : ↑1 = 1 := rfl @[simp] theorem coe_eq_zero_iff {R : Type u} [ring R] (s : subring R) {x : ↥s} : ↑x = 0 ↔ x = 0 := { mp := fun (h : ↑x = 0) => subtype.ext (trans h (Eq.symm (coe_zero s))), mpr := fun (h : x = 0) => Eq.symm h ▸ coe_zero s } /-- A subring of a `comm_ring` is a `comm_ring`. -/ protected instance to_comm_ring {R : Type u_1} [comm_ring R] (s : subring R) : comm_ring ↥s := comm_ring.mk ring.add sorry ring.zero sorry sorry ring.neg ring.sub sorry sorry ring.mul sorry ring.one sorry sorry sorry sorry sorry /-- The natural ring hom from a subring of ring `R` to `R`. -/ def subtype {R : Type u} [ring R] (s : subring R) : ↥s →+* R := ring_hom.mk coe sorry sorry sorry sorry @[simp] theorem coe_subtype {R : Type u} [ring R] (s : subring R) : ⇑(subtype s) = coe := rfl /-! # Partial order -/ protected instance partial_order {R : Type u} [ring R] : partial_order (subring R) := partial_order.mk (fun (s t : subring R) => ∀ {x : R}, x ∈ s → x ∈ t) partial_order.lt sorry sorry sorry theorem le_def {R : Type u} [ring R] {s : subring R} {t : subring R} : s ≤ t ↔ ∀ {x : R}, x ∈ s → x ∈ t := iff.rfl @[simp] theorem coe_subset_coe {R : Type u} [ring R] {s : subring R} {t : subring R} : ↑s ⊆ ↑t ↔ s ≤ t := iff.rfl @[simp] theorem coe_ssubset_coe {R : Type u} [ring R] {s : subring R} {t : subring R} : ↑s ⊂ ↑t ↔ s < t := iff.rfl @[simp] theorem mem_coe {R : Type u} [ring R] {S : subring R} {m : R} : m ∈ ↑S ↔ m ∈ S := iff.rfl @[simp] theorem coe_coe {R : Type u} [ring R] (s : subring R) : ↥↑s = ↥s := rfl @[simp] theorem mem_to_submonoid {R : Type u} [ring R] {s : subring R} {x : R} : x ∈ to_submonoid s ↔ x ∈ s := iff.rfl @[simp] theorem coe_to_submonoid {R : Type u} [ring R] (s : subring R) : ↑(to_submonoid s) = ↑s := rfl @[simp] theorem mem_to_add_subgroup {R : Type u} [ring R] {s : subring R} {x : R} : x ∈ to_add_subgroup s ↔ x ∈ s := iff.rfl @[simp] theorem coe_to_add_subgroup {R : Type u} [ring R] (s : subring R) : ↑(to_add_subgroup s) = ↑s := rfl /-! # top -/ /-- The subring `R` of the ring `R`. -/ protected instance has_top {R : Type u} [ring R] : has_top (subring R) := has_top.mk (mk (submonoid.carrier ⊤) sorry sorry sorry sorry sorry) @[simp] theorem mem_top {R : Type u} [ring R] (x : R) : x ∈ ⊤ := set.mem_univ x @[simp] theorem coe_top {R : Type u} [ring 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 := mk (⇑f ⁻¹' carrier s) sorry sorry sorry sorry sorry @[simp] theorem coe_comap {R : Type u} {S : Type v} [ring R] [ring S] (s : subring S) (f : R →+* S) : ↑(comap f s) = ⇑f ⁻¹' ↑s := rfl @[simp] theorem mem_comap {R : Type u} {S : Type v} [ring R] [ring S] {s : subring S} {f : R →+* S} {x : R} : x ∈ comap f s ↔ coe_fn f x ∈ s := iff.rfl theorem comap_comap {R : Type u} {S : Type v} {T : Type w} [ring R] [ring S] [ring T] (s : subring T) (g : S →+* T) (f : R →+* S) : comap f (comap g s) = comap (ring_hom.comp g f) s := 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 := mk (⇑f '' carrier s) sorry sorry sorry sorry sorry @[simp] theorem coe_map {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) (s : subring R) : ↑(map f s) = ⇑f '' ↑s := rfl @[simp] theorem mem_map {R : Type u} {S : Type v} [ring R] [ring S] {f : R →+* S} {s : subring R} {y : S} : y ∈ map f s ↔ ∃ (x : R), ∃ (H : x ∈ s), coe_fn f x = y := set.mem_image_iff_bex theorem map_map {R : Type u} {S : Type v} {T : Type w} [ring R] [ring S] [ring T] (s : subring R) (g : S →+* T) (f : R →+* S) : map g (map f s) = map (ring_hom.comp g f) s := ext' (set.image_image (fun (a : S) => coe_fn g a) (fun (a : R) => coe_fn f a) (carrier s)) theorem map_le_iff_le_comap {R : Type u} {S : Type v} [ring R] [ring S] {f : R →+* S} {s : subring R} {t : subring S} : map f s ≤ t ↔ s ≤ comap f t := set.image_subset_iff theorem gc_map_comap {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) : galois_connection (map f) (comap f) := fun (S_1 : subring R) (T : subring S) => map_le_iff_le_comap end subring namespace ring_hom /-! # 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.map f ⊤ @[simp] theorem coe_range {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) : ↑(range f) = set.range ⇑f := set.image_univ @[simp] theorem mem_range {R : Type u} {S : Type v} [ring R] [ring S] {f : R →+* S} {y : S} : y ∈ range f ↔ ∃ (x : R), coe_fn f x = y := sorry theorem mem_range_self {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) (x : R) : coe_fn f x ∈ range f := iff.mpr mem_range (Exists.intro x rfl) theorem map_range {R : Type u} {S : Type v} {T : Type w} [ring R] [ring S] [ring T] (g : S →+* T) (f : R →+* S) : subring.map g (range f) = range (comp g f) := subring.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 : R), coe_fn f x ∈ s) : R →+* ↥s := mk (fun (x : R) => { val := coe_fn f x, property := h x }) sorry sorry sorry sorry theorem surjective_onto_range {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) : function.surjective ⇑(cod_restrict' f (range f) (mem_range_self f)) := sorry end ring_hom namespace subring /-- A subring of a commutative ring is a commutative ring. -/ def subset_comm_ring {cR : Type u} [comm_ring cR] (S : subring cR) : comm_ring ↥S := comm_ring.mk ring.add sorry ring.zero sorry sorry ring.neg ring.sub sorry sorry ring.mul sorry ring.one sorry sorry sorry sorry sorry /-- A subring of a non-trivial ring is non-trivial. -/ protected instance nontrivial {D : Type u_1} [ring D] [nontrivial D] (S : subring D) : nontrivial ↥S := subsemiring.nontrivial (to_subsemiring S) /-- A subring of a ring with no zero divisors has no zero divisors. -/ protected instance no_zero_divisors {D : Type u_1} [ring D] [no_zero_divisors D] (S : subring D) : no_zero_divisors ↥S := subsemiring.no_zero_divisors (to_subsemiring S) /-- A subring of an integral domain is an integral domain. -/ protected instance subring.domain {D : Type u_1} [integral_domain D] (S : subring D) : integral_domain ↥S := integral_domain.mk comm_ring.add sorry comm_ring.zero sorry sorry comm_ring.neg comm_ring.sub sorry sorry comm_ring.mul sorry comm_ring.one sorry sorry sorry sorry sorry sorry sorry /-! # bot -/ protected instance has_bot {R : Type u} [ring R] : has_bot (subring R) := has_bot.mk (ring_hom.range (int.cast_ring_hom R)) protected instance inhabited {R : Type u} [ring R] : Inhabited (subring R) := { default := ⊥ } theorem coe_bot {R : Type u} [ring R] : ↑⊥ = set.range coe := ring_hom.coe_range (int.cast_ring_hom R) theorem mem_bot {R : Type u} [ring R] {x : R} : x ∈ ⊥ ↔ ∃ (n : ℤ), ↑n = x := ring_hom.mem_range /-! # inf -/ /-- The inf of two subrings is their intersection. -/ protected instance has_inf {R : Type u} [ring R] : has_inf (subring R) := has_inf.mk fun (s t : subring R) => mk (↑s ∩ ↑t) sorry sorry sorry sorry sorry @[simp] theorem coe_inf {R : Type u} [ring R] (p : subring R) (p' : subring R) : ↑(p ⊓ p') = ↑p ∩ ↑p' := rfl @[simp] theorem mem_inf {R : Type u} [ring R] {p : subring R} {p' : subring R} {x : R} : x ∈ p ⊓ p' ↔ x ∈ p ∧ x ∈ p' := iff.rfl protected instance has_Inf {R : Type u} [ring R] : has_Inf (subring R) := has_Inf.mk fun (s : set (subring R)) => subring.mk' (set.Inter fun (t : subring R) => set.Inter fun (H : t ∈ s) => ↑t) (infi fun (t : subring R) => infi fun (H : t ∈ s) => to_submonoid t) (infi fun (t : subring R) => infi fun (H : t ∈ s) => to_add_subgroup t) sorry sorry @[simp] theorem coe_Inf {R : Type u} [ring R] (S : set (subring R)) : ↑(Inf S) = set.Inter fun (s : subring R) => set.Inter fun (H : s ∈ S) => ↑s := rfl theorem mem_Inf {R : Type u} [ring R] {S : set (subring R)} {x : R} : x ∈ Inf S ↔ ∀ (p : subring R), p ∈ S → x ∈ p := set.mem_bInter_iff @[simp] theorem Inf_to_submonoid {R : Type u} [ring R] (s : set (subring R)) : to_submonoid (Inf s) = infi fun (t : subring R) => infi fun (H : t ∈ s) => to_submonoid t := mk'_to_submonoid (has_Inf._proof_1 s) (has_Inf._proof_2 s) @[simp] theorem Inf_to_add_subgroup {R : Type u} [ring R] (s : set (subring R)) : to_add_subgroup (Inf s) = infi fun (t : subring R) => infi fun (H : t ∈ s) => to_add_subgroup t := mk'_to_add_subgroup (has_Inf._proof_1 s) (has_Inf._proof_2 s) /-- Subrings of a ring form a complete lattice. -/ protected instance complete_lattice {R : Type u} [ring R] : complete_lattice (subring R) := complete_lattice.mk complete_lattice.sup complete_lattice.le complete_lattice.lt sorry sorry sorry sorry sorry sorry has_inf.inf sorry sorry sorry ⊤ sorry ⊥ sorry complete_lattice.Sup complete_lattice.Inf sorry sorry sorry sorry /-! # subring closure of a subset -/ /-- The `subring` generated by a set. -/ def closure {R : Type u} [ring R] (s : set R) : subring R := Inf (set_of fun (S : subring R) => s ⊆ ↑S) theorem mem_closure {R : Type u} [ring R] {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] theorem subset_closure {R : Type u} [ring R] {s : set R} : s ⊆ ↑(closure s) := fun (x : R) (hx : x ∈ s) => iff.mpr mem_closure fun (S : subring R) (hS : s ⊆ ↑S) => hS hx /-- A subring `t` includes `closure s` if and only if it includes `s`. -/ @[simp] theorem closure_le {R : Type u} [ring R] {s : set R} {t : subring R} : closure s ≤ t ↔ s ⊆ ↑t := { mp := set.subset.trans subset_closure, mpr := fun (h : s ⊆ ↑t) => Inf_le h } /-- Subring closure of a set is monotone in its argument: if `s ⊆ t`, then `closure s ≤ closure t`. -/ theorem closure_mono {R : Type u} [ring R] {s : set R} {t : set R} (h : s ⊆ t) : closure s ≤ closure t := iff.mpr closure_le (set.subset.trans h subset_closure) theorem closure_eq_of_le {R : Type u} [ring R] {s : set R} {t : subring R} (h₁ : s ⊆ ↑t) (h₂ : t ≤ closure s) : closure s = t := le_antisymm (iff.mpr closure_le 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`. -/ theorem closure_induction {R : Type u} [ring R] {s : set R} {p : R → Prop} {x : R} (h : x ∈ closure s) (Hs : ∀ (x : R), x ∈ s → p x) (H0 : p 0) (H1 : p 1) (Hadd : ∀ (x y : R), p x → p y → p (x + y)) (Hneg : ∀ (x : R), p x → p (-x)) (Hmul : ∀ (x y : R), p x → p y → p (x * y)) : p x := iff.mpr closure_le Hs x h theorem mem_closure_iff {R : Type u} [ring R] {s : set R} {x : R} : x ∈ closure s ↔ x ∈ add_subgroup.closure ↑(submonoid.closure s) := sorry theorem exists_list_of_mem_closure {R : Type u} [ring R] {s : set R} {x : R} (h : x ∈ closure s) : ∃ (L : List (List R)), (∀ (t : List R), t ∈ L → ∀ (y : R), y ∈ t → y ∈ s ∨ y = -1) ∧ list.sum (list.map list.prod L) = x := sorry /-- `closure` forms a Galois insertion with the coercion to set. -/ protected def gi (R : Type u) [ring R] : galois_insertion closure coe := galois_insertion.mk (fun (s : set R) (_x : ↑(closure s) ≤ s) => closure s) sorry sorry sorry /-- Closure of a subring `S` equals `S`. -/ theorem closure_eq {R : Type u} [ring R] (s : subring R) : closure ↑s = s := galois_insertion.l_u_eq (subring.gi R) s @[simp] theorem closure_empty {R : Type u} [ring R] : closure ∅ = ⊥ := galois_connection.l_bot (galois_insertion.gc (subring.gi R)) @[simp] theorem closure_univ {R : Type u} [ring R] : closure set.univ = ⊤ := coe_top ▸ closure_eq ⊤ theorem closure_union {R : Type u} [ring R] (s : set R) (t : set R) : closure (s ∪ t) = closure s ⊔ closure t := galois_connection.l_sup (galois_insertion.gc (subring.gi R)) theorem closure_Union {R : Type u} [ring R] {ι : Sort u_1} (s : ι → set R) : closure (set.Union fun (i : ι) => s i) = supr fun (i : ι) => closure (s i) := galois_connection.l_supr (galois_insertion.gc (subring.gi R)) theorem closure_sUnion {R : Type u} [ring R] (s : set (set R)) : closure (⋃₀s) = supr fun (t : set R) => supr fun (H : t ∈ s) => closure t := galois_connection.l_Sup (galois_insertion.gc (subring.gi R)) theorem map_sup {R : Type u} {S : Type v} [ring R] [ring S] (s : subring R) (t : subring R) (f : R →+* S) : map f (s ⊔ t) = map f s ⊔ map f t := galois_connection.l_sup (gc_map_comap f) theorem map_supr {R : Type u} {S : Type v} [ring R] [ring S] {ι : Sort u_1} (f : R →+* S) (s : ι → subring R) : map f (supr s) = supr fun (i : ι) => map f (s i) := galois_connection.l_supr (gc_map_comap f) theorem comap_inf {R : Type u} {S : Type v} [ring R] [ring S] (s : subring S) (t : subring S) (f : R →+* S) : comap f (s ⊓ t) = comap f s ⊓ comap f t := galois_connection.u_inf (gc_map_comap f) theorem comap_infi {R : Type u} {S : Type v} [ring R] [ring S] {ι : Sort u_1} (f : R →+* S) (s : ι → subring S) : comap f (infi s) = infi fun (i : ι) => comap f (s i) := galois_connection.u_infi (gc_map_comap f) @[simp] theorem map_bot {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) : map f ⊥ = ⊥ := galois_connection.l_bot (gc_map_comap f) @[simp] theorem comap_top {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) : comap f ⊤ = ⊤ := galois_connection.u_top (gc_map_comap f) /-- Given `subring`s `s`, `t` of rings `R`, `S` respectively, `s.prod t` is `s × t` as a subring of `R × S`. -/ def prod {R : Type u} {S : Type v} [ring R] [ring S] (s : subring R) (t : subring S) : subring (R × S) := mk (set.prod ↑s ↑t) sorry sorry sorry sorry sorry theorem coe_prod {R : Type u} {S : Type v} [ring R] [ring S] (s : subring R) (t : subring S) : ↑(prod s t) = set.prod ↑s ↑t := rfl theorem mem_prod {R : Type u} {S : Type v} [ring R] [ring S] {s : subring R} {t : subring S} {p : R × S} : p ∈ prod s t ↔ prod.fst p ∈ s ∧ prod.snd p ∈ t := iff.rfl theorem prod_mono {R : Type u} {S : Type v} [ring R] [ring S] {s₁ : subring R} {s₂ : subring R} (hs : s₁ ≤ s₂) {t₁ : subring S} {t₂ : subring S} (ht : t₁ ≤ t₂) : prod s₁ t₁ ≤ prod s₂ t₂ := set.prod_mono hs ht theorem prod_mono_right {R : Type u} {S : Type v} [ring R] [ring S] (s : subring R) : monotone fun (t : subring S) => prod s t := prod_mono (le_refl s) theorem prod_mono_left {R : Type u} {S : Type v} [ring R] [ring S] (t : subring S) : monotone fun (s : subring R) => prod s t := fun (s₁ s₂ : subring R) (hs : s₁ ≤ s₂) => prod_mono hs (le_refl t) theorem prod_top {R : Type u} {S : Type v} [ring R] [ring S] (s : subring R) : prod s ⊤ = comap (ring_hom.fst R S) s := sorry theorem top_prod {R : Type u} {S : Type v} [ring R] [ring S] (s : subring S) : prod ⊤ s = comap (ring_hom.snd R S) s := sorry @[simp] theorem top_prod_top {R : Type u} {S : Type v} [ring R] [ring S] : prod ⊤ ⊤ = ⊤ := Eq.trans (top_prod ⊤) (comap_top (ring_hom.snd R S)) /-- Product of subrings is isomorphic to their product as rings. -/ def prod_equiv {R : Type u} {S : Type v} [ring R] [ring S] (s : subring R) (t : subring S) : ↥(prod s t) ≃+* ↥s × ↥t := ring_equiv.mk (equiv.to_fun (equiv.set.prod ↑s ↑t)) (equiv.inv_fun (equiv.set.prod ↑s ↑t)) sorry sorry sorry sorry /-- 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) -/ theorem mem_supr_of_directed {R : Type u} [ring R] {ι : Sort u_1} [hι : Nonempty ι] {S : ι → subring R} (hS : directed LessEq S) {x : R} : (x ∈ supr fun (i : ι) => S i) ↔ ∃ (i : ι), x ∈ S i := sorry theorem coe_supr_of_directed {R : Type u} [ring R] {ι : Sort u_1} [hι : Nonempty ι] {S : ι → subring R} (hS : directed LessEq S) : ↑(supr fun (i : ι) => S i) = set.Union fun (i : ι) => ↑(S i) := sorry theorem mem_Sup_of_directed_on {R : Type u} [ring R] {S : set (subring R)} (Sne : set.nonempty S) (hS : directed_on LessEq S) {x : R} : x ∈ Sup S ↔ ∃ (s : subring R), ∃ (H : s ∈ S), x ∈ s := sorry theorem coe_Sup_of_directed_on {R : Type u} [ring R] {S : set (subring R)} (Sne : set.nonempty S) (hS : directed_on LessEq S) : ↑(Sup S) = set.Union fun (s : subring R) => set.Union fun (H : s ∈ S) => ↑s := sorry end subring namespace ring_hom /-- Restriction of a ring homomorphism to a subring of the domain. -/ def restrict {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) (s : subring R) : ↥s →+* S := comp f (subring.subtype s) @[simp] theorem restrict_apply {R : Type u} {S : Type v} [ring R] [ring S] {s : subring R} (f : R →+* S) (x : ↥s) : coe_fn (restrict f s) x = coe_fn f ↑x := rfl /-- Restriction of a ring homomorphism to its range interpreted as a subsemiring. -/ def range_restrict {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) : R →+* ↥(range f) := cod_restrict' f (range f) sorry @[simp] theorem coe_range_restrict {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) (x : R) : ↑(coe_fn (range_restrict f) x) = coe_fn f x := rfl theorem range_top_iff_surjective {R : Type u} {S : Type v} [ring R] [ring S] {f : R →+* S} : range f = ⊤ ↔ function.surjective ⇑f := sorry /-- The range of a surjective ring homomorphism is the whole of the codomain. -/ theorem range_top_of_surjective {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) (hf : function.surjective ⇑f) : range f = ⊤ := iff.mpr range_top_iff_surjective 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 {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) (g : R →+* S) : subring R := subring.mk (set_of fun (x : R) => coe_fn f x = coe_fn g x) sorry sorry sorry sorry sorry /-- If two ring homomorphisms are equal on a set, then they are equal on its subring closure. -/ theorem eq_on_set_closure {R : Type u} {S : Type v} [ring R] [ring S] {f : R →+* S} {g : R →+* S} {s : set R} (h : set.eq_on (⇑f) (⇑g) s) : set.eq_on ⇑f ⇑g ↑(subring.closure s) := (fun (this : subring.closure s ≤ eq_locus f g) => this) (iff.mpr subring.closure_le h) theorem eq_of_eq_on_set_top {R : Type u} {S : Type v} [ring R] [ring S] {f : R →+* S} {g : R →+* S} (h : set.eq_on ⇑f ⇑g ↑⊤) : f = g := ext fun (x : R) => h trivial theorem eq_of_eq_on_set_dense {R : Type u} {S : Type v} [ring R] [ring S] {s : set R} (hs : subring.closure s = ⊤) {f : R →+* S} {g : R →+* S} (h : set.eq_on (⇑f) (⇑g) s) : f = g := eq_of_eq_on_set_top (hs ▸ eq_on_set_closure h) theorem closure_preimage_le {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) (s : set S) : subring.closure (⇑f ⁻¹' s) ≤ subring.comap f (subring.closure s) := iff.mpr subring.closure_le fun (x : R) (hx : x ∈ ⇑f ⁻¹' s) => iff.mpr subring.mem_coe (iff.mpr subring.mem_comap (subring.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. -/ theorem map_closure {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) (s : set R) : subring.map f (subring.closure s) = subring.closure (⇑f '' s) := sorry end ring_hom namespace subring /-- The ring homomorphism associated to an inclusion of subrings. -/ def inclusion {R : Type u} [ring R] {S : subring R} {T : subring R} (h : S ≤ T) : ↥S →* ↥T := ↑(ring_hom.cod_restrict' (subtype S) T sorry) @[simp] theorem range_subtype {R : Type u} [ring R] (s : subring R) : ring_hom.range (subtype s) = s := ext' (Eq.trans (ring_hom.coe_srange (subtype s)) subtype.range_coe) @[simp] theorem range_fst {R : Type u} {S : Type v} [ring R] [ring S] : ring_hom.srange (ring_hom.fst R S) = ⊤ := ring_hom.srange_top_of_surjective (ring_hom.fst R S) prod.fst_surjective @[simp] theorem range_snd {R : Type u} {S : Type v} [ring R] [ring S] : ring_hom.srange (ring_hom.snd R S) = ⊤ := ring_hom.srange_top_of_surjective (ring_hom.snd R S) prod.snd_surjective @[simp] theorem prod_bot_sup_bot_prod {R : Type u} {S : Type v} [ring R] [ring S] (s : subring R) (t : subring S) : prod s ⊥ ⊔ prod ⊥ t = prod s t := sorry end subring namespace ring_equiv /-- Makes the identity isomorphism from a proof two subrings of a multiplicative monoid are equal. -/ def subring_congr {R : Type u} [ring R] {s : subring R} {t : subring R} (h : s = t) : ↥s ≃+* ↥t := mk (equiv.to_fun (equiv.set_congr sorry)) (equiv.inv_fun (equiv.set_congr sorry)) sorry sorry sorry sorry end ring_equiv namespace subring protected theorem in_closure.rec_on {R : Type u} [ring R] {s : set R} {C : R → Prop} {x : R} (hx : x ∈ closure s) (h1 : C 1) (hneg1 : C (-1)) (hs : ∀ (z : R), z ∈ s → ∀ (n : R), C n → C (z * n)) (ha : ∀ {x y : R}, C x → C y → C (x + y)) : C x := sorry theorem closure_preimage_le {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) (s : set S) : closure (⇑f ⁻¹' s) ≤ comap f (closure s) := iff.mpr closure_le fun (x : R) (hx : x ∈ ⇑f ⁻¹' s) => iff.mpr mem_coe (iff.mpr mem_comap (subset_closure hx)) end subring theorem add_subgroup.int_mul_mem {R : Type u} [ring R] {G : add_subgroup R} (k : ℤ) {g : R} (h : g ∈ G) : ↑k * g ∈ G := sorry end Mathlib
60d6c27342da7833eb2a9d904b21200501f46681
dd0f5513e11c52db157d2fcc8456d9401a6cd9da
/09_Type_Classes.org.17.lean
c170a9855fe653304d6969971afb2b06b192447c
[]
no_license
cjmazey/lean-tutorial
ba559a49f82aa6c5848b9bf17b7389bf7f4ba645
381f61c9fcac56d01d959ae0fa6e376f2c4e3b34
refs/heads/master
1,610,286,098,832
1,447,124,923,000
1,447,124,923,000
43,082,433
0
0
null
null
null
null
UTF-8
Lean
false
false
214
lean
import standard import standard inductive has_add [class] (A : Type) : Type := mk : (A → A → A) → has_add A definition add {A : Type} [s : has_add A] := has_add.rec (λ x, x) s notation a `+` b := add a b
9ec398b8d5946a58fa7c4e3b621c2219b9a7bfd5
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/measure_theory/group/measurable_equiv.lean
1b508cc59c573143ed84bedd91ef213ab40ba48c
[ "Apache-2.0" ]
permissive
robertylewis/mathlib
3d16e3e6daf5ddde182473e03a1b601d2810952c
1d13f5b932f5e40a8308e3840f96fc882fae01f0
refs/heads/master
1,651,379,945,369
1,644,276,960,000
1,644,276,960,000
98,875,504
0
0
Apache-2.0
1,644,253,514,000
1,501,495,700,000
Lean
UTF-8
Lean
false
false
7,588
lean
/- Copyright (c) 2021 Yury G. Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury G. Kudryashov -/ import measure_theory.group.arithmetic /-! # (Scalar) multiplication and (vector) addition as measurable equivalences In this file we define the following measurable equivalences: * `measurable_equiv.smul`: if a group `G` acts on `α` by measurable maps, then each element `c : G` defines a measurable automorphism of `α`; * `measurable_equiv.vadd`: additive version of `measurable_equiv.smul`; * `measurable_equiv.smul₀`: if a group with zero `G` acts on `α` by measurable maps, then each nonzero element `c : G` defines a measurable automorphism of `α`; * `measurable_equiv.mul_left`: if `G` is a group with measurable multiplication, then left multiplication by `g : G` is a measurable automorphism of `G`; * `measurable_equiv.add_left`: additive version of `measurable_equiv.mul_left`; * `measurable_equiv.mul_right`: if `G` is a group with measurable multiplication, then right multiplication by `g : G` is a measurable automorphism of `G`; * `measurable_equiv.add_right`: additive version of `measurable_equiv.mul_right`; * `measurable_equiv.mul_left₀`, `measurable_equiv.mul_right₀`: versions of `measurable_equiv.mul_left` and `measurable_equiv.mul_right` for groups with zero; * `measurable_equiv.inv`, `measurable_equiv.inv₀`: `has_inv.inv` as a measurable automorphism of a group (or a group with zero); * `measurable_equiv.neg`: negation as a measurable automorphism of an additive group. We also deduce that the corresponding maps are measurable embeddings. ## Tags measurable, equivalence, group action -/ namespace measurable_equiv variables {G G₀ α : Type*} [measurable_space G] [measurable_space G₀] [measurable_space α] [group G] [group_with_zero G₀] [mul_action G α] [mul_action G₀ α] [has_measurable_smul G α] [has_measurable_smul G₀ α] /-- If a group `G` acts on `α` by measurable maps, then each element `c : G` defines a measurable automorphism of `α`. -/ @[to_additive "If an additive group `G` acts on `α` by measurable maps, then each element `c : G` defines a measurable automorphism of `α`.", simps to_equiv apply { fully_applied := ff }] def smul (c : G) : α ≃ᵐ α := { to_equiv := mul_action.to_perm c, measurable_to_fun := measurable_const_smul c, measurable_inv_fun := measurable_const_smul c⁻¹ } @[to_additive] lemma _root_.measurable_embedding_const_smul (c : G) : measurable_embedding ((•) c : α → α) := (smul c).measurable_embedding @[simp, to_additive] lemma symm_smul (c : G) : (smul c : α ≃ᵐ α).symm = smul c⁻¹ := ext rfl /-- If a group with zero `G₀` acts on `α` by measurable maps, then each nonzero element `c : G₀` defines a measurable automorphism of `α` -/ def smul₀ (c : G₀) (hc : c ≠ 0) : α ≃ᵐ α := measurable_equiv.smul (units.mk0 c hc) @[simp] lemma coe_smul₀ {c : G₀} (hc : c ≠ 0) : ⇑(smul₀ c hc : α ≃ᵐ α) = (•) c := rfl @[simp] lemma symm_smul₀ {c : G₀} (hc : c ≠ 0) : (smul₀ c hc : α ≃ᵐ α).symm = smul₀ c⁻¹ (inv_ne_zero hc) := ext rfl lemma _root_.measurable_embedding_const_smul₀ {c : G₀} (hc : c ≠ 0) : measurable_embedding ((•) c : α → α) := (smul₀ c hc).measurable_embedding section mul variables [has_measurable_mul G] [has_measurable_mul G₀] /-- If `G` is a group with measurable multiplication, then left multiplication by `g : G` is a measurable automorphism of `G`. -/ @[to_additive "If `G` is an additive group with measurable addition, then addition of `g : G` on the left is a measurable automorphism of `G`."] def mul_left (g : G) : G ≃ᵐ G := smul g @[simp, to_additive] lemma coe_mul_left (g : G) : ⇑(mul_left g) = (*) g := rfl @[simp, to_additive] lemma symm_mul_left (g : G) : (mul_left g).symm = mul_left g⁻¹ := ext rfl @[simp, to_additive] lemma to_equiv_mul_left (g : G) : (mul_left g).to_equiv = equiv.mul_left g := rfl @[to_additive] lemma _root_.measurable_embedding_mul_left (g : G) : measurable_embedding ((*) g) := (mul_left g).measurable_embedding /-- If `G` is a group with measurable multiplication, then right multiplication by `g : G` is a measurable automorphism of `G`. -/ @[to_additive "If `G` is an additive group with measurable addition, then addition of `g : G` on the right is a measurable automorphism of `G`."] def mul_right (g : G) : G ≃ᵐ G := { to_equiv := equiv.mul_right g, measurable_to_fun := measurable_mul_const g, measurable_inv_fun := measurable_mul_const g⁻¹ } @[to_additive] lemma _root_.measurable_embedding_mul_right (g : G) : measurable_embedding (λ x, x * g) := (mul_right g).measurable_embedding @[simp, to_additive] lemma coe_mul_right (g : G) : ⇑(mul_right g) = (λ x, x * g) := rfl @[simp, to_additive] lemma symm_mul_right (g : G) : (mul_right g).symm = mul_right g⁻¹ := ext rfl @[simp, to_additive] lemma to_equiv_mul_right (g : G) : (mul_right g).to_equiv = equiv.mul_right g := rfl /-- If `G₀` is a group with zero with measurable multiplication, then left multiplication by a nonzero element `g : G₀` is a measurable automorphism of `G₀`. -/ def mul_left₀ (g : G₀) (hg : g ≠ 0) : G₀ ≃ᵐ G₀ := smul₀ g hg lemma _root_.measurable_embedding_mul_left₀ {g : G₀} (hg : g ≠ 0) : measurable_embedding ((*) g) := (mul_left₀ g hg).measurable_embedding @[simp] lemma coe_mul_left₀ {g : G₀} (hg : g ≠ 0) : ⇑(mul_left₀ g hg) = (*) g := rfl @[simp] lemma symm_mul_left₀ {g : G₀} (hg : g ≠ 0) : (mul_left₀ g hg).symm = mul_left₀ g⁻¹ (inv_ne_zero hg) := ext rfl @[simp] lemma to_equiv_mul_left₀ {g : G₀} (hg : g ≠ 0) : (mul_left₀ g hg).to_equiv = equiv.mul_left₀ g hg := rfl /-- If `G₀` is a group with zero with measurable multiplication, then right multiplication by a nonzero element `g : G₀` is a measurable automorphism of `G₀`. -/ def mul_right₀ (g : G₀) (hg : g ≠ 0) : G₀ ≃ᵐ G₀ := { to_equiv := equiv.mul_right₀ g hg, measurable_to_fun := measurable_mul_const g, measurable_inv_fun := measurable_mul_const g⁻¹ } lemma _root_.measurable_embedding_mul_right₀ {g : G₀} (hg : g ≠ 0) : measurable_embedding (λ x, x * g) := (mul_right₀ g hg).measurable_embedding @[simp] lemma coe_mul_right₀ {g : G₀} (hg : g ≠ 0) : ⇑(mul_right₀ g hg) = λ x, x * g := rfl @[simp] lemma symm_mul_right₀ {g : G₀} (hg : g ≠ 0) : (mul_right₀ g hg).symm = mul_right₀ g⁻¹ (inv_ne_zero hg) := ext rfl @[simp] lemma to_equiv_mul_right₀ {g : G₀} (hg : g ≠ 0) : (mul_right₀ g hg).to_equiv = equiv.mul_right₀ g hg := rfl end mul variables (G G₀) /-- Inversion as a measurable automorphism of a group. -/ @[to_additive "Negation as a measurable automorphism of an additive group.", simps to_equiv apply { fully_applied := ff }] def inv [has_measurable_inv G] : G ≃ᵐ G := { to_equiv := equiv.inv G, measurable_to_fun := measurable_inv, measurable_inv_fun := measurable_inv } /-- Inversion as a measurable automorphism of a group with zero. -/ @[simps to_equiv apply { fully_applied := ff }] def inv₀ [has_measurable_inv G₀] : G₀ ≃ᵐ G₀ := { to_equiv := equiv.inv₀ G₀, measurable_to_fun := measurable_inv, measurable_inv_fun := measurable_inv } variables {G G₀} @[simp] lemma symm_inv [has_measurable_inv G] : (inv G).symm = inv G := rfl @[simp] lemma symm_inv₀ [has_measurable_inv G₀] : (inv₀ G₀).symm = inv₀ G₀ := rfl end measurable_equiv
acd0917c1c6c7376bd197181c2436062059c005d
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/test/mk_slim_check_test.lean
a34cebdf0f34afa8d389d5da6778f291e701e6db
[ "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
494
lean
import tactic.slim_check section open tactic @[interactive] meta def mk_slim_check_test : tactic unit := do tgt ← target, msg ← (λ s, match interactive.slim_check { random_seed := some 257 } s with | result.success x _ := fail "expecting error" s | result.exception msg _ _ := result.success (msg.iget ()).to_string s end ), trace!"Try this: have : {tgt}, success_if_fail_with_msg {{ slim_check {{ random_seed := some 257 } } \"{msg}\", admit, trivial " end
aaa6d031a0ddd18b31aa422eb0e375dd1406d122
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/model_theory/substructures.lean
8228ffe11dfbfd7211848b7606c4523bc0ab5211
[ "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
29,578
lean
/- Copyright (c) 2021 Aaron Anderson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Aaron Anderson -/ import order.closure import model_theory.semantics import model_theory.encoding /-! # First-Order Substructures This file defines substructures of first-order structures in a similar manner to the various substructures appearing in the algebra library. ## Main Definitions * A `first_order.language.substructure` is defined so that `L.substructure M` is the type of all substructures of the `L`-structure `M`. * `first_order.language.substructure.closure` is defined so that if `s : set M`, `closure L s` is the least substructure of `M` containing `s`. * `first_order.language.substructure.comap` is defined so that `s.comap f` is the preimage of the substructure `s` under the homomorphism `f`, as a substructure. * `first_order.language.substructure.map` is defined so that `s.map f` is the image of the substructure `s` under the homomorphism `f`, as a substructure. * `first_order.language.hom.range` is defined so that `f.map` is the range of the the homomorphism `f`, as a substructure. * `first_order.language.hom.dom_restrict` and `first_order.language.hom.cod_restrict` restrict the domain and codomain respectively of first-order homomorphisms to substructures. * `first_order.language.embedding.dom_restrict` and `first_order.language.embedding.cod_restrict` restrict the domain and codomain respectively of first-order embeddings to substructures. * `first_order.language.substructure.inclusion` is the inclusion embedding between substructures. ## Main Results * `L.substructure M` forms a `complete_lattice`. -/ universes u v w namespace first_order namespace language variables {L : language.{u v}} {M : Type w} {N P : Type*} variables [L.Structure M] [L.Structure N] [L.Structure P] open_locale first_order cardinal open Structure cardinal section closed_under open set variables {n : ℕ} (f : L.functions n) (s : set M) /-- Indicates that a set in a given structure is a closed under a function symbol. -/ def closed_under : Prop := ∀ (x : fin n → M), (∀ i : fin n, x i ∈ s) → fun_map f x ∈ s variable (L) @[simp] lemma closed_under_univ : closed_under f (univ : set M) := λ _ _, mem_univ _ variables {L f s} {t : set M} namespace closed_under lemma inter (hs : closed_under f s) (ht : closed_under f t) : closed_under f (s ∩ t) := λ x h, mem_inter (hs x (λ i, mem_of_mem_inter_left (h i))) (ht x (λ i, mem_of_mem_inter_right (h i))) lemma inf (hs : closed_under f s) (ht : closed_under f t) : closed_under f (s ⊓ t) := hs.inter ht variables {S : set (set M)} lemma Inf (hS : ∀ s, s ∈ S → closed_under f s) : closed_under f (Inf S) := λ x h s hs, hS s hs x (λ i, h i s hs) end closed_under end closed_under variables (L) (M) /-- A substructure of a structure `M` is a set closed under application of function symbols. -/ structure substructure := (carrier : set M) (fun_mem : ∀{n}, ∀ (f : L.functions n), closed_under f carrier) variables {L} {M} namespace substructure instance : set_like (L.substructure M) M := ⟨substructure.carrier, λ p q h, by cases p; cases q; congr'⟩ /-- See Note [custom simps projection] -/ def simps.coe (S : L.substructure M) : set M := S initialize_simps_projections substructure (carrier → coe) @[simp] lemma mem_carrier {s : L.substructure M} {x : M} : x ∈ s.carrier ↔ x ∈ s := iff.rfl /-- Two substructures are equal if they have the same elements. -/ @[ext] theorem ext {S T : L.substructure M} (h : ∀ x, x ∈ S ↔ x ∈ T) : S = T := set_like.ext h /-- Copy a substructure replacing `carrier` with a set that is equal to it. -/ protected def copy (S : L.substructure M) (s : set M) (hs : s = S) : L.substructure M := { carrier := s, fun_mem := λ n f, hs.symm ▸ (S.fun_mem f) } end substructure variable {S : L.substructure M} lemma term.realize_mem {α : Type*} (t : L.term α) (xs : α → M) (h : ∀ a, xs a ∈ S) : t.realize xs ∈ S := begin induction t with a n f ts ih, { exact h a }, { exact substructure.fun_mem _ _ _ ih } end namespace substructure @[simp] lemma coe_copy {s : set M} (hs : s = S) : (S.copy s hs : set M) = s := rfl lemma copy_eq {s : set M} (hs : s = S) : S.copy s hs = S := set_like.coe_injective hs lemma constants_mem (c : L.constants) : ↑c ∈ S := mem_carrier.2 (S.fun_mem c _ fin.elim0) /-- The substructure `M` of the structure `M`. -/ instance : has_top (L.substructure M) := ⟨{ carrier := set.univ, fun_mem := λ n f x h, set.mem_univ _ }⟩ instance : inhabited (L.substructure M) := ⟨⊤⟩ @[simp] lemma mem_top (x : M) : x ∈ (⊤ : L.substructure M) := set.mem_univ x @[simp] lemma coe_top : ((⊤ : L.substructure M) : set M) = set.univ := rfl /-- The inf of two substructures is their intersection. -/ instance : has_inf (L.substructure M) := ⟨λ S₁ S₂, { carrier := S₁ ∩ S₂, fun_mem := λ n f, (S₁.fun_mem f).inf (S₂.fun_mem f) }⟩ @[simp] lemma coe_inf (p p' : L.substructure M) : ((p ⊓ p' : L.substructure M) : set M) = p ∩ p' := rfl @[simp] lemma mem_inf {p p' : L.substructure M} {x : M} : x ∈ p ⊓ p' ↔ x ∈ p ∧ x ∈ p' := iff.rfl instance : has_Inf (L.substructure M) := ⟨λ s, { carrier := ⋂ t ∈ s, ↑t, fun_mem := λ n f, closed_under.Inf begin rintro _ ⟨t, rfl⟩, by_cases h : t ∈ s, { simpa [h] using t.fun_mem f }, { simp [h] } end }⟩ @[simp, norm_cast] lemma coe_Inf (S : set (L.substructure M)) : ((Inf S : L.substructure M) : set M) = ⋂ s ∈ S, ↑s := rfl lemma mem_Inf {S : set (L.substructure M)} {x : M} : x ∈ Inf S ↔ ∀ p ∈ S, x ∈ p := set.mem_Inter₂ lemma mem_infi {ι : Sort*} {S : ι → L.substructure M} {x : M} : (x ∈ ⨅ i, S i) ↔ ∀ i, x ∈ S i := by simp only [infi, mem_Inf, set.forall_range_iff] @[simp, norm_cast] lemma coe_infi {ι : Sort*} {S : ι → L.substructure M} : (↑(⨅ i, S i) : set M) = ⋂ i, S i := by simp only [infi, coe_Inf, set.bInter_range] /-- Substructures of a structure form a complete lattice. -/ instance : complete_lattice (L.substructure M) := { le := (≤), lt := (<), top := (⊤), le_top := λ S x hx, mem_top x, inf := (⊓), Inf := has_Inf.Inf, le_inf := λ a b c ha hb x hx, ⟨ha hx, hb hx⟩, inf_le_left := λ a b x, and.left, inf_le_right := λ a b x, and.right, .. complete_lattice_of_Inf (L.substructure M) $ λ s, is_glb.of_image (λ S T, show (S : set M) ≤ T ↔ S ≤ T, from set_like.coe_subset_coe) is_glb_binfi } variable (L) /-- The `L.substructure` generated by a set. -/ def closure : lower_adjoint (coe : L.substructure M → set M) := ⟨λ s, Inf {S | s ⊆ S}, λ s S, ⟨set.subset.trans (λ x hx, mem_Inf.2 $ λ S hS, hS hx), λ h, Inf_le h⟩⟩ variables {L} {s : set M} lemma mem_closure {x : M} : x ∈ closure L s ↔ ∀ S : L.substructure M, s ⊆ S → x ∈ S := mem_Inf /-- The substructure generated by a set includes the set. -/ @[simp] lemma subset_closure : s ⊆ closure L s := (closure L).le_closure s lemma not_mem_of_not_mem_closure {P : M} (hP : P ∉ closure L s) : P ∉ s := λ h, hP (subset_closure h) @[simp] lemma closed (S : L.substructure M) : (closure L).closed (S : set M) := congr rfl ((closure L).eq_of_le set.subset.rfl (λ x xS, mem_closure.2 (λ T hT, hT xS))) open set /-- A substructure `S` includes `closure L s` if and only if it includes `s`. -/ @[simp] lemma closure_le : closure L s ≤ S ↔ s ⊆ S := (closure L).closure_le_closed_iff_le s S.closed /-- Substructure closure of a set is monotone in its argument: if `s ⊆ t`, then `closure L s ≤ closure L t`. -/ lemma closure_mono ⦃s t : set M⦄ (h : s ⊆ t) : closure L s ≤ closure L t := (closure L).monotone h lemma closure_eq_of_le (h₁ : s ⊆ S) (h₂ : S ≤ closure L s) : closure L s = S := (closure L).eq_of_le h₁ h₂ lemma coe_closure_eq_range_term_realize : (closure L s : set M) = range (@term.realize L _ _ _ (coe : s → M)) := begin let S : L.substructure M := ⟨range (term.realize coe), λ n f x hx, _⟩, { change _ = (S : set M), rw ← set_like.ext'_iff, refine closure_eq_of_le (λ x hx, ⟨var ⟨x, hx⟩, rfl⟩) (le_Inf (λ S' hS', _)), { rintro _ ⟨t, rfl⟩, exact t.realize_mem _ (λ i, hS' i.2) } }, { simp only [mem_range] at *, refine ⟨func f (λ i, (classical.some (hx i))), _⟩, simp only [term.realize, λ i, classical.some_spec (hx i)] } end instance small_closure [small.{u} s] : small.{u} (closure L s) := begin rw [← set_like.coe_sort_coe, substructure.coe_closure_eq_range_term_realize], exact small_range _, end lemma mem_closure_iff_exists_term {x : M} : x ∈ closure L s ↔ ∃ (t : L.term s), t.realize (coe : s → M) = x := by rw [← set_like.mem_coe, coe_closure_eq_range_term_realize, mem_range] lemma lift_card_closure_le_card_term : cardinal.lift.{max u w} (# (closure L s)) ≤ # (L.term s) := begin rw [← set_like.coe_sort_coe, coe_closure_eq_range_term_realize], rw [← cardinal.lift_id'.{w (max u w)} (# (L.term s))], exact cardinal.mk_range_le_lift, end theorem lift_card_closure_le : cardinal.lift.{u w} (# (closure L s)) ≤ max ℵ₀ (cardinal.lift.{u w} (#s) + cardinal.lift.{w u} (#(Σ i, L.functions i))) := begin rw ←lift_umax, refine lift_card_closure_le_card_term.trans (term.card_le.trans _), rw [mk_sum, lift_umax], end variable (L) lemma _root_.set.countable.substructure_closure [countable (Σl, L.functions l)] (h : s.countable) : countable.{w + 1} (closure L s) := begin haveI : countable s := h.to_subtype, rw [← mk_le_aleph_0_iff, ← lift_le_aleph_0], exact lift_card_closure_le_card_term.trans mk_le_aleph_0 end variables {L} (S) /-- An induction principle for closure membership. If `p` holds for all elements of `s`, and is preserved under function symbols, then `p` holds for all elements of the closure of `s`. -/ @[elab_as_eliminator] lemma closure_induction {p : M → Prop} {x} (h : x ∈ closure L s) (Hs : ∀ x ∈ s, p x) (Hfun : ∀ {n : ℕ} (f : L.functions n), closed_under f (set_of p)) : p x := (@closure_le L M _ ⟨set_of p, λ n, Hfun⟩ _).2 Hs h /-- If `s` is a dense set in a structure `M`, `substructure.closure L s = ⊤`, then in order to prove that some predicate `p` holds for all `x : M` it suffices to verify `p x` for `x ∈ s`, and verify that `p` is preserved under function symbols. -/ @[elab_as_eliminator] lemma dense_induction {p : M → Prop} (x : M) {s : set M} (hs : closure L s = ⊤) (Hs : ∀ x ∈ s, p x) (Hfun : ∀ {n : ℕ} (f : L.functions n), closed_under f (set_of p)) : p x := have ∀ x ∈ closure L s, p x, from λ x hx, closure_induction hx Hs (λ n, Hfun), by simpa [hs] using this x variables (L) (M) /-- `closure` forms a Galois insertion with the coercion to set. -/ protected def gi : galois_insertion (@closure L M _) coe := { choice := λ s _, closure L s, gc := (closure L).gc, le_l_u := λ s, subset_closure, choice_eq := λ s h, rfl } variables {L} {M} /-- Closure of a substructure `S` equals `S`. -/ @[simp] lemma closure_eq : closure L (S : set M) = S := (substructure.gi L M).l_u_eq S @[simp] lemma closure_empty : closure L (∅ : set M) = ⊥ := (substructure.gi L M).gc.l_bot @[simp] lemma closure_univ : closure L (univ : set M) = ⊤ := @coe_top L M _ ▸ closure_eq ⊤ lemma closure_union (s t : set M) : closure L (s ∪ t) = closure L s ⊔ closure L t := (substructure.gi L M).gc.l_sup lemma closure_Union {ι} (s : ι → set M) : closure L (⋃ i, s i) = ⨆ i, closure L (s i) := (substructure.gi L M).gc.l_supr instance small_bot : small.{u} (⊥ : L.substructure M) := begin rw ← closure_empty, exact substructure.small_closure end /-! ### `comap` and `map` -/ /-- The preimage of a substructure along a homomorphism is a substructure. -/ @[simps] def comap (φ : M →[L] N) (S : L.substructure N) : L.substructure M := { carrier := (φ ⁻¹' S), fun_mem := λ n f x hx, begin rw [mem_preimage, φ.map_fun], exact S.fun_mem f (φ ∘ x) hx, end } @[simp] lemma mem_comap {S : L.substructure N} {f : M →[L] N} {x : M} : x ∈ S.comap f ↔ f x ∈ S := iff.rfl lemma comap_comap (S : L.substructure P) (g : N →[L] P) (f : M →[L] N) : (S.comap g).comap f = S.comap (g.comp f) := rfl @[simp] lemma comap_id (S : L.substructure P) : S.comap (hom.id _ _) = S := ext (by simp) /-- The image of a substructure along a homomorphism is a substructure. -/ @[simps] def map (φ : M →[L] N) (S : L.substructure M) : L.substructure N := { carrier := (φ '' S), fun_mem := λ n f x hx, (mem_image _ _ _).1 ⟨fun_map f (λ i, classical.some (hx i)), S.fun_mem f _ (λ i, (classical.some_spec (hx i)).1), begin simp only [hom.map_fun, set_like.mem_coe], exact congr rfl (funext (λ i, (classical.some_spec (hx i)).2)), end⟩ } @[simp] lemma mem_map {f : M →[L] N} {S : L.substructure M} {y : N} : y ∈ S.map f ↔ ∃ x ∈ S, f x = y := mem_image_iff_bex lemma mem_map_of_mem (f : M →[L] N) {S : L.substructure M} {x : M} (hx : x ∈ S) : f x ∈ S.map f := mem_image_of_mem f hx lemma apply_coe_mem_map (f : M →[L] N) (S : L.substructure M) (x : S) : f x ∈ S.map f := mem_map_of_mem f x.prop lemma map_map (g : N →[L] P) (f : M →[L] N) : (S.map f).map g = S.map (g.comp f) := set_like.coe_injective $ image_image _ _ _ lemma map_le_iff_le_comap {f : M →[L] N} {S : L.substructure M} {T : L.substructure N} : S.map f ≤ T ↔ S ≤ T.comap f := image_subset_iff lemma gc_map_comap (f : M →[L] N) : galois_connection (map f) (comap f) := λ S T, map_le_iff_le_comap lemma map_le_of_le_comap {T : L.substructure N} {f : M →[L] N} : S ≤ T.comap f → S.map f ≤ T := (gc_map_comap f).l_le lemma le_comap_of_map_le {T : L.substructure N} {f : M →[L] N} : S.map f ≤ T → S ≤ T.comap f := (gc_map_comap f).le_u lemma le_comap_map {f : M →[L] N} : S ≤ (S.map f).comap f := (gc_map_comap f).le_u_l _ lemma map_comap_le {S : L.substructure N} {f : M →[L] N} : (S.comap f).map f ≤ S := (gc_map_comap f).l_u_le _ lemma monotone_map {f : M →[L] N} : monotone (map f) := (gc_map_comap f).monotone_l lemma monotone_comap {f : M →[L] N} : monotone (comap f) := (gc_map_comap f).monotone_u @[simp] lemma map_comap_map {f : M →[L] N} : ((S.map f).comap f).map f = S.map f := (gc_map_comap f).l_u_l_eq_l _ @[simp] lemma comap_map_comap {S : L.substructure N} {f : M →[L] N} : ((S.comap f).map f).comap f = S.comap f := (gc_map_comap f).u_l_u_eq_u _ lemma map_sup (S T : L.substructure M) (f : M →[L] N) : (S ⊔ T).map f = S.map f ⊔ T.map f := (gc_map_comap f).l_sup lemma map_supr {ι : Sort*} (f : M →[L] N) (s : ι → L.substructure M) : (supr s).map f = ⨆ i, (s i).map f := (gc_map_comap f).l_supr lemma comap_inf (S T : L.substructure N) (f : M →[L] N) : (S ⊓ T).comap f = S.comap f ⊓ T.comap f := (gc_map_comap f).u_inf lemma comap_infi {ι : Sort*} (f : M →[L] N) (s : ι → L.substructure N) : (infi s).comap f = ⨅ i, (s i).comap f := (gc_map_comap f).u_infi @[simp] lemma map_bot (f : M →[L] N) : (⊥ : L.substructure M).map f = ⊥ := (gc_map_comap f).l_bot @[simp] lemma comap_top (f : M →[L] N) : (⊤ : L.substructure N).comap f = ⊤ := (gc_map_comap f).u_top @[simp] lemma map_id (S : L.substructure M) : S.map (hom.id L M) = S := ext (λ x, ⟨λ ⟨_, h, rfl⟩, h, λ h, ⟨_, h, rfl⟩⟩) lemma map_closure (f : M →[L] N) (s : set M) : (closure L s).map f = closure L (f '' s) := eq.symm $ closure_eq_of_le (set.image_subset f subset_closure) $ map_le_iff_le_comap.2 $ closure_le.2 $ λ x hx, subset_closure ⟨x, hx, rfl⟩ @[simp] lemma closure_image (f : M →[L] N) : closure L (f '' s) = map f (closure L s) := (map_closure f s).symm section galois_coinsertion variables {ι : Type*} {f : M →[L] N} (hf : function.injective f) include hf /-- `map f` and `comap f` form a `galois_coinsertion` when `f` is injective. -/ def gci_map_comap : galois_coinsertion (map f) (comap f) := (gc_map_comap f).to_galois_coinsertion (λ S x, by simp [mem_comap, mem_map, hf.eq_iff]) lemma comap_map_eq_of_injective (S : L.substructure M) : (S.map f).comap f = S := (gci_map_comap hf).u_l_eq _ lemma comap_surjective_of_injective : function.surjective (comap f) := (gci_map_comap hf).u_surjective lemma map_injective_of_injective : function.injective (map f) := (gci_map_comap hf).l_injective lemma comap_inf_map_of_injective (S T : L.substructure M) : (S.map f ⊓ T.map f).comap f = S ⊓ T := (gci_map_comap hf).u_inf_l _ _ lemma comap_infi_map_of_injective (S : ι → L.substructure M) : (⨅ i, (S i).map f).comap f = infi S := (gci_map_comap hf).u_infi_l _ lemma comap_sup_map_of_injective (S T : L.substructure M) : (S.map f ⊔ T.map f).comap f = S ⊔ T := (gci_map_comap hf).u_sup_l _ _ lemma comap_supr_map_of_injective (S : ι → L.substructure M) : (⨆ i, (S i).map f).comap f = supr S := (gci_map_comap hf).u_supr_l _ lemma map_le_map_iff_of_injective {S T : L.substructure M} : S.map f ≤ T.map f ↔ S ≤ T := (gci_map_comap hf).l_le_l_iff lemma map_strict_mono_of_injective : strict_mono (map f) := (gci_map_comap hf).strict_mono_l end galois_coinsertion section galois_insertion variables {ι : Type*} {f : M →[L] N} (hf : function.surjective f) include hf /-- `map f` and `comap f` form a `galois_insertion` when `f` is surjective. -/ def gi_map_comap : galois_insertion (map f) (comap f) := (gc_map_comap f).to_galois_insertion (λ S x h, let ⟨y, hy⟩ := hf x in mem_map.2 ⟨y, by simp [hy, h]⟩) lemma map_comap_eq_of_surjective (S : L.substructure N) : (S.comap f).map f = S := (gi_map_comap hf).l_u_eq _ lemma map_surjective_of_surjective : function.surjective (map f) := (gi_map_comap hf).l_surjective lemma comap_injective_of_surjective : function.injective (comap f) := (gi_map_comap hf).u_injective lemma map_inf_comap_of_surjective (S T : L.substructure N) : (S.comap f ⊓ T.comap f).map f = S ⊓ T := (gi_map_comap hf).l_inf_u _ _ lemma map_infi_comap_of_surjective (S : ι → L.substructure N) : (⨅ i, (S i).comap f).map f = infi S := (gi_map_comap hf).l_infi_u _ lemma map_sup_comap_of_surjective (S T : L.substructure N) : (S.comap f ⊔ T.comap f).map f = S ⊔ T := (gi_map_comap hf).l_sup_u _ _ lemma map_supr_comap_of_surjective (S : ι → L.substructure N) : (⨆ i, (S i).comap f).map f = supr S := (gi_map_comap hf).l_supr_u _ lemma comap_le_comap_iff_of_surjective {S T : L.substructure N} : S.comap f ≤ T.comap f ↔ S ≤ T := (gi_map_comap hf).u_le_u_iff lemma comap_strict_mono_of_surjective : strict_mono (comap f) := (gi_map_comap hf).strict_mono_u end galois_insertion instance induced_Structure {S : L.substructure M} : L.Structure S := { fun_map := λ n f x, ⟨fun_map f (λ i, x i), S.fun_mem f (λ i, x i) (λ i, (x i).2)⟩, rel_map := λ n r x, rel_map r (λ i, (x i : M)) } /-- The natural embedding of an `L.substructure` of `M` into `M`. -/ def subtype (S : L.substructure M) : S ↪[L] M := { to_fun := coe, inj' := subtype.coe_injective } @[simp] theorem coe_subtype : ⇑S.subtype = coe := rfl /-- The equivalence between the maximal substructure of a structure and the structure itself. -/ def top_equiv : (⊤ : L.substructure M) ≃[L] M := { to_fun := subtype ⊤, inv_fun := λ m, ⟨m, mem_top m⟩, left_inv := λ m, by simp, right_inv := λ m, rfl } @[simp] lemma coe_top_equiv : ⇑(top_equiv : (⊤ : L.substructure M) ≃[L] M) = coe := rfl /-- A dependent version of `substructure.closure_induction`. -/ @[elab_as_eliminator] lemma closure_induction' (s : set M) {p : Π x, x ∈ closure L s → Prop} (Hs : ∀ x (h : x ∈ s), p x (subset_closure h)) (Hfun : ∀ {n : ℕ} (f : L.functions n), closed_under f {x | ∃ hx, p x hx}) {x} (hx : x ∈ closure L s) : p x hx := begin refine exists.elim _ (λ (hx : x ∈ closure L s) (hc : p x hx), hc), exact closure_induction hx (λ x hx, ⟨subset_closure hx, Hs x hx⟩) @Hfun end end substructure namespace Lhom open substructure variables {L' : language} [L'.Structure M] (φ : L →ᴸ L') [φ.is_expansion_on M] include φ /-- Reduces the language of a substructure along a language hom. -/ def substructure_reduct : L'.substructure M ↪o L.substructure M := { to_fun := λ S, { carrier := S, fun_mem := λ n f x hx, begin have h := S.fun_mem (φ.on_function f) x hx, simp only [Lhom.map_on_function, substructure.mem_carrier] at h, exact h, end }, inj' := λ S T h, begin simp only [set_like.coe_set_eq] at h, exact h, end, map_rel_iff' := λ S T, iff.rfl } @[simp] lemma mem_substructure_reduct {x : M} {S : L'.substructure M} : x ∈ φ.substructure_reduct S ↔ x ∈ S := iff.rfl @[simp] lemma coe_substructure_reduct {S : L'.substructure M} : (φ.substructure_reduct S : set M) = ↑S := rfl end Lhom namespace substructure /-- Turns any substructure containing a constant set `A` into a `L[[A]]`-substructure. -/ def with_constants (S : L.substructure M) {A : set M} (h : A ⊆ S) : L[[A]].substructure M := { carrier := S, fun_mem := λ n f, begin cases f, { exact S.fun_mem f }, { cases n, { exact λ _ _, h f.2 }, { exact is_empty_elim f } } end } variables {A : set M} {s : set M} (h : A ⊆ S) @[simp] lemma mem_with_constants {x : M} : x ∈ S.with_constants h ↔ x ∈ S := iff.rfl @[simp] lemma coe_with_constants : (S.with_constants h : set M) = ↑S := rfl @[simp] lemma reduct_with_constants : (L.Lhom_with_constants A).substructure_reduct (S.with_constants h) = S := by { ext, simp } lemma subset_closure_with_constants : A ⊆ (closure (L[[A]]) s) := begin intros a ha, simp only [set_like.mem_coe], let a' : (L[[A]]).constants := sum.inr ⟨a, ha⟩, exact constants_mem a', end lemma closure_with_constants_eq : (closure (L[[A]]) s) = (closure L (A ∪ s)).with_constants ((A.subset_union_left s).trans subset_closure) := begin refine closure_eq_of_le ((A.subset_union_right s).trans subset_closure) _, rw ← ((L.Lhom_with_constants A).substructure_reduct).le_iff_le, simp only [subset_closure, reduct_with_constants, closure_le, Lhom.coe_substructure_reduct, set.union_subset_iff, and_true], { exact subset_closure_with_constants }, { apply_instance }, { apply_instance }, end end substructure namespace hom open substructure /-- The restriction of a first-order hom to a substructure `s ⊆ M` gives a hom `s → N`. -/ @[simps] def dom_restrict (f : M →[L] N) (p : L.substructure M) : p →[L] N := f.comp p.subtype.to_hom /-- A first-order hom `f : M → N` whose values lie in a substructure `p ⊆ N` can be restricted to a hom `M → p`. -/ @[simps] def cod_restrict (p : L.substructure N) (f : M →[L] N) (h : ∀c, f c ∈ p) : M →[L] p := { to_fun := λc, ⟨f c, h c⟩, map_rel' := λ n R x h, f.map_rel R x h } @[simp] lemma comp_cod_restrict (f : M →[L] N) (g : N →[L] P) (p : L.substructure P) (h : ∀b, g b ∈ p) : ((cod_restrict p g h).comp f : M →[L] p) = cod_restrict p (g.comp f) (assume b, h _) := ext $ assume b, rfl @[simp] lemma subtype_comp_cod_restrict (f : M →[L] N) (p : L.substructure N) (h : ∀b, f b ∈ p) : p.subtype.to_hom.comp (cod_restrict p f h) = f := ext $ assume b, rfl /-- The range of a first-order hom `f : M → N` is a submodule of `N`. See Note [range copy pattern]. -/ def range (f : M →[L] N) : L.substructure N := (map f ⊤).copy (set.range f) set.image_univ.symm theorem range_coe (f : M →[L] N) : (range f : set N) = set.range f := rfl @[simp] theorem mem_range {f : M →[L] N} {x} : x ∈ range f ↔ ∃ y, f y = x := iff.rfl lemma range_eq_map (f : M →[L] N) : f.range = map f ⊤ := by { ext, simp } theorem mem_range_self (f : M →[L] N) (x : M) : f x ∈ f.range := ⟨x, rfl⟩ @[simp] theorem range_id : range (id L M) = ⊤ := set_like.coe_injective set.range_id theorem range_comp (f : M →[L] N) (g : N →[L] P) : range (g.comp f : M →[L] P) = map g (range f) := set_like.coe_injective (set.range_comp g f) theorem range_comp_le_range (f : M →[L] N) (g : N →[L] P) : range (g.comp f : M →[L] P) ≤ range g := set_like.coe_mono (set.range_comp_subset_range f g) theorem range_eq_top {f : M →[L] N} : range f = ⊤ ↔ function.surjective f := by rw [set_like.ext'_iff, range_coe, coe_top, set.range_iff_surjective] lemma range_le_iff_comap {f : M →[L] N} {p : L.substructure N} : range f ≤ p ↔ comap f p = ⊤ := by rw [range_eq_map, map_le_iff_le_comap, eq_top_iff] lemma map_le_range {f : M →[L] N} {p : L.substructure M} : map f p ≤ range f := set_like.coe_mono (set.image_subset_range f p) /-- The substructure of elements `x : M` such that `f x = g x` -/ def eq_locus (f g : M →[L] N) : substructure L M := { carrier := {x : M | f x = g x}, fun_mem := λ n fn x hx, by { have h : f ∘ x = g ∘ x := by { ext, repeat {rw function.comp_apply}, apply hx, }, simp [h], } } /-- If two `L.hom`s are equal on a set, then they are equal on its substructure closure. -/ lemma eq_on_closure {f g : M →[L] N} {s : set M} (h : set.eq_on f g s) : set.eq_on f g (closure L s) := show closure L s ≤ f.eq_locus g, from closure_le.2 h lemma eq_of_eq_on_top {f g : M →[L] N} (h : set.eq_on f g (⊤ : substructure L M)) : f = g := ext $ λ x, h trivial variable {s : set M} lemma eq_of_eq_on_dense (hs : closure L s = ⊤) {f g : M →[L] N} (h : s.eq_on f g) : f = g := eq_of_eq_on_top $ hs ▸ eq_on_closure h end hom namespace embedding open substructure /-- The restriction of a first-order embedding to a substructure `s ⊆ M` gives an embedding `s → N`. -/ def dom_restrict (f : M ↪[L] N) (p : L.substructure M) : p ↪[L] N := f.comp p.subtype @[simp] lemma dom_restrict_apply (f : M ↪[L] N) (p : L.substructure M) (x : p) : f.dom_restrict p x = f x := rfl /-- A first-order embedding `f : M → N` whose values lie in a substructure `p ⊆ N` can be restricted to an embedding `M → p`. -/ def cod_restrict (p : L.substructure N) (f : M ↪[L] N) (h : ∀c, f c ∈ p) : M ↪[L] p := { to_fun := f.to_hom.cod_restrict p h, inj' := λ a b ab, f.injective (subtype.mk_eq_mk.1 ab), map_fun' := λ n F x, (f.to_hom.cod_restrict p h).map_fun' F x, map_rel' := λ n r x, begin simp only, rw [← p.subtype.map_rel, function.comp.assoc], change rel_map r ((hom.comp p.subtype.to_hom (f.to_hom.cod_restrict p h)) ∘ x) ↔ _, rw [hom.subtype_comp_cod_restrict, ← f.map_rel], refl, end } @[simp] theorem cod_restrict_apply (p : L.substructure N) (f : M ↪[L] N) {h} (x : M) : (cod_restrict p f h x : N) = f x := rfl @[simp] lemma comp_cod_restrict (f : M ↪[L] N) (g : N ↪[L] P) (p : L.substructure P) (h : ∀b, g b ∈ p) : ((cod_restrict p g h).comp f : M ↪[L] p) = cod_restrict p (g.comp f) (assume b, h _) := ext $ assume b, rfl @[simp] lemma subtype_comp_cod_restrict (f : M ↪[L] N) (p : L.substructure N) (h : ∀b, f b ∈ p) : p.subtype.comp (cod_restrict p f h) = f := ext $ assume b, rfl /-- The equivalence between a substructure `s` and its image `s.map f.to_hom`, where `f` is an embedding. -/ noncomputable def substructure_equiv_map (f : M ↪[L] N) (s : L.substructure M) : s ≃[L] s.map f.to_hom := { to_fun := cod_restrict (s.map f.to_hom) (f.dom_restrict s) (λ ⟨m, hm⟩, ⟨m, hm, rfl⟩), inv_fun := λ n, ⟨classical.some n.2, (classical.some_spec n.2).1⟩, left_inv := λ ⟨m, hm⟩, subtype.mk_eq_mk.2 (f.injective ((classical.some_spec (cod_restrict (s.map f.to_hom) (f.dom_restrict s) (λ ⟨m, hm⟩, ⟨m, hm, rfl⟩) ⟨m, hm⟩).2).2)), right_inv := λ ⟨n, hn⟩, subtype.mk_eq_mk.2 (classical.some_spec hn).2 } @[simp] lemma substructure_equiv_map_apply (f : M ↪[L] N) (p : L.substructure M) (x : p) : (f.substructure_equiv_map p x : N) = f x := rfl /-- The equivalence between the domain and the range of an embedding `f`. -/ noncomputable def equiv_range (f : M ↪[L] N) : M ≃[L] f.to_hom.range := { to_fun := cod_restrict f.to_hom.range f f.to_hom.mem_range_self, inv_fun := λ n, classical.some n.2, left_inv := λ m, f.injective (classical.some_spec (cod_restrict f.to_hom.range f f.to_hom.mem_range_self m).2), right_inv := λ ⟨n, hn⟩, subtype.mk_eq_mk.2 (classical.some_spec hn) } @[simp] lemma equiv_range_apply (f : M ↪[L] N) (x : M) : (f.equiv_range x : N) = f x := rfl end embedding namespace equiv lemma to_hom_range (f : M ≃[L] N) : f.to_hom.range = ⊤ := begin ext n, simp only [hom.mem_range, coe_to_hom, substructure.mem_top, iff_true], exact ⟨f.symm n, apply_symm_apply _ _⟩ end end equiv namespace substructure /-- The embedding associated to an inclusion of substructures. -/ def inclusion {S T : L.substructure M} (h : S ≤ T) : S ↪[L] T := S.subtype.cod_restrict _ (λ x, h x.2) @[simp] lemma coe_inclusion {S T : L.substructure M} (h : S ≤ T) : (inclusion h : S → T) = set.inclusion h := rfl lemma range_subtype (S : L.substructure M) : S.subtype.to_hom.range = S := begin ext x, simp only [hom.mem_range, embedding.coe_to_hom, coe_subtype], refine ⟨_, λ h, ⟨⟨x, h⟩, rfl⟩⟩, rintros ⟨⟨y, hy⟩, rfl⟩, exact hy, end end substructure end language end first_order
ac77badc5b3d2414f24e50936fecd28a6d96e5ca
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/omit.lean
db426fa19283e5e1a9316b9140830ca1d50f2b1b
[ "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
293
lean
prelude section variable A : Type variable a : A variable c : A omit A include A include A omit A variable B : Type variable b : B variable d : B include A include a include c definition foo := b inductive tst (C : Type) := mk : tst C end check foo check tst
df593a541bc92b3bcf4f3791450285adfc1d699a
e967488e2008f07b0aa7b6b18d4398d02c7460e2
/src/game/sup_inf/02temp.lean
febff3b72c273c4d1b422c1de267b1d0df08bc4c
[]
no_license
thyrgle/real-number-game
5c53eefaa815051f3d70ca5782e41d2adb76fd6e
a420eecef62209b729c910c2170d1dd27b74bc9f
refs/heads/master
1,606,405,407,612
1,574,956,743,000
1,574,956,743,000
229,126,103
0
0
null
1,576,784,273,000
1,576,784,272,000
null
UTF-8
Lean
false
false
1,543
lean
import data.real.basic -- imports the real numbers ℝ namespace xena -- hide -- World name : Sup and Inf /- # Chapter 1 : Sup and Inf # Level 1 : Introduction to sets of reals. Let $X$ be a set of real numbers. We say a real number $b$ is an *upper bound* for $X$ if every $x\in X$ is at most $b$. -/ def is_upper_bound (X : set ℝ) (b : ℝ) := ∀ x : ℝ, x ∈ X → x ≤ b /- Here is an easy fact about upper bounds, which we shall prove below: If $Xsubseteq Y$ are two sets of reals, and $b$ is an upper bound for $Y$, then it's also an upper bound for $X$. To prove this in Lean, it helps to understand a bit more about definitions. Initially our goal is `is_upper_bound Y b → is_upper_bound X b` -/ /- Lemma If $Xsubseteq Y$ are two sets of reals, and $b$ is an upper bound for $Y$, then it's also an upper bound for $X$. -/ lemma upper_bounds_mono (X Y : set ℝ) (h1 : X ⊆ Y) (b : ℝ) : is_upper_bound Y b → is_upper_bound X b := begin intro h2, intro a, intro ha, apply h2, change ∀ a, a ∈ X → a ∈ Y at h1, -- unfold has_subset.subset set.subset at h1, apply h1, exact ha, end /- Tactic : refl ## Summary `refl` proves goals of the form `X = X`. ## Details The `refl` tactic will close any goal of the form `A = B` where `A` and `B` are *exactly the same thing*. ### Example: If it looks like this in the top right hand box: ``` a b c d : mynat ⊢ (a + b) * (c + d) = (a + b) * (c + d) ``` then `refl,` will close the goal and solve the level. Don't forget the comma. -/ end xena
35ce60704709451ae7a5ab191acd70f58bf05505
4727251e0cd73359b15b664c3170e5d754078599
/test/lint_coe_t.lean
49c51e08fb61bea92143e1a8d2c043a2e4dc46ad
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
1,081
lean
import tactic.lint open tactic -- bad, because every iteration of tc search will loop, generating nested quotients section local attribute [instance] def a_to_quot {α} (R : setoid α) : has_coe α (quotient R) := ⟨quotient.mk⟩ run_cmd do d ← get_decl ``a_to_quot, some _ ← linter.has_coe_variable.test d, d ← get_decl ``coe_trans, some s ← fails_quickly 3000 d, guard $ "maximum class-instance resolution depth has been reached".is_prefix_of s end -- good, because the term gets smaller in every iteration noncomputable instance quot_to_a {α} (R : setoid α) : has_coe (quotient R) α := ⟨λ q, quot.rec_on q (λ a, classical.choice ⟨a⟩) (by cc)⟩ run_cmd do decl ← get_decl ``quot_to_a, -- linter does not complain none ← linter.has_coe_variable.test decl, skip -- bad, because it introduces a metavariable section local attribute [instance] def int_to_a {α} [inhabited α] : has_coe ℤ α := ⟨λ _, default⟩ run_cmd do decl ← get_decl ``int_to_a, -- linter does not complain some _ ← linter.has_coe_variable.test decl, skip end
7ebd7303225ad0476bb189aaf517e83684cc4d39
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/algebra/support.lean
fa9525752543c439751475d86bbf35d51c3fb407
[ "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
10,734
lean
/- Copyright (c) 2020 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import order.conditionally_complete_lattice import algebra.big_operators.basic import algebra.group.prod import algebra.group.pi import algebra.module.pi /-! # Support of a function In this file we define `function.support f = {x | f x ≠ 0}` and prove its basic properties. We also define `function.mul_support f = {x | f x ≠ 1}`. -/ open set open_locale big_operators namespace function variables {α β A B M N P R S G M₀ G₀ : Type*} {ι : Sort*} section has_one variables [has_one M] [has_one N] [has_one P] /-- `support` of a function is the set of points `x` such that `f x ≠ 0`. -/ def support [has_zero A] (f : α → A) : set α := {x | f x ≠ 0} /-- `mul_support` of a function is the set of points `x` such that `f x ≠ 1`. -/ @[to_additive] def mul_support (f : α → M) : set α := {x | f x ≠ 1} @[to_additive] lemma nmem_mul_support {f : α → M} {x : α} : x ∉ mul_support f ↔ f x = 1 := not_not @[to_additive] lemma compl_mul_support {f : α → M} : (mul_support f)ᶜ = {x | f x = 1} := ext $ λ x, nmem_mul_support @[simp, to_additive] lemma mem_mul_support {f : α → M} {x : α} : x ∈ mul_support f ↔ f x ≠ 1 := iff.rfl @[simp, to_additive] lemma mul_support_subset_iff {f : α → M} {s : set α} : mul_support f ⊆ s ↔ ∀ x, f x ≠ 1 → x ∈ s := iff.rfl @[to_additive] lemma mul_support_subset_iff' {f : α → M} {s : set α} : mul_support f ⊆ s ↔ ∀ x ∉ s, f x = 1 := forall_congr $ λ x, not_imp_comm @[simp, to_additive] lemma mul_support_eq_empty_iff {f : α → M} : mul_support f = ∅ ↔ f = 1 := by { simp_rw [← subset_empty_iff, mul_support_subset_iff', funext_iff], simp } @[simp, to_additive] lemma mul_support_one' : mul_support (1 : α → M) = ∅ := mul_support_eq_empty_iff.2 rfl @[simp, to_additive] lemma mul_support_one : mul_support (λ x : α, (1 : M)) = ∅ := mul_support_one' @[to_additive] lemma mul_support_binop_subset (op : M → N → P) (op1 : op 1 1 = 1) (f : α → M) (g : α → N) : mul_support (λ x, op (f x) (g x)) ⊆ mul_support f ∪ mul_support g := λ x hx, classical.by_cases (λ hf : f x = 1, or.inr $ λ hg, hx $ by simp only [hf, hg, op1]) or.inl @[to_additive] lemma mul_support_sup [semilattice_sup M] (f g : α → M) : mul_support (λ x, f x ⊔ g x) ⊆ mul_support f ∪ mul_support g := mul_support_binop_subset (⊔) sup_idem f g @[to_additive] lemma mul_support_inf [semilattice_inf M] (f g : α → M) : mul_support (λ x, f x ⊓ g x) ⊆ mul_support f ∪ mul_support g := mul_support_binop_subset (⊓) inf_idem f g @[to_additive] lemma mul_support_max [linear_order M] (f g : α → M) : mul_support (λ x, max (f x) (g x)) ⊆ mul_support f ∪ mul_support g := mul_support_sup f g @[to_additive] lemma mul_support_min [linear_order M] (f g : α → M) : mul_support (λ x, min (f x) (g x)) ⊆ mul_support f ∪ mul_support g := mul_support_inf f g @[to_additive] lemma mul_support_supr [conditionally_complete_lattice M] [nonempty ι] (f : ι → α → M) : mul_support (λ x, ⨆ i, f i x) ⊆ ⋃ i, mul_support (f i) := begin rw mul_support_subset_iff', simp only [mem_Union, not_exists, nmem_mul_support], intros x hx, simp only [hx, csupr_const] end @[to_additive] lemma mul_support_infi [conditionally_complete_lattice M] [nonempty ι] (f : ι → α → M) : mul_support (λ x, ⨅ i, f i x) ⊆ ⋃ i, mul_support (f i) := @mul_support_supr _ (order_dual M) ι ⟨(1:M)⟩ _ _ f @[to_additive] lemma mul_support_comp_subset {g : M → N} (hg : g 1 = 1) (f : α → M) : mul_support (g ∘ f) ⊆ mul_support f := λ x, mt $ λ h, by simp only [(∘), *] @[to_additive] lemma mul_support_subset_comp {g : M → N} (hg : ∀ {x}, g x = 1 → x = 1) (f : α → M) : mul_support f ⊆ mul_support (g ∘ f) := λ x, mt hg @[to_additive] lemma mul_support_comp_eq (g : M → N) (hg : ∀ {x}, g x = 1 ↔ x = 1) (f : α → M) : mul_support (g ∘ f) = mul_support f := set.ext $ λ x, not_congr hg @[to_additive] lemma mul_support_comp_eq_preimage (g : β → M) (f : α → β) : mul_support (g ∘ f) = f ⁻¹' mul_support g := rfl @[to_additive support_prod_mk] lemma mul_support_prod_mk (f : α → M) (g : α → N) : mul_support (λ x, (f x, g x)) = mul_support f ∪ mul_support g := set.ext $ λ x, by simp only [mul_support, not_and_distrib, mem_union_eq, mem_set_of_eq, prod.mk_eq_one, ne.def] @[to_additive support_prod_mk'] lemma mul_support_prod_mk' (f : α → M × N) : mul_support f = mul_support (λ x, (f x).1) ∪ mul_support (λ x, (f x).2) := by simp only [← mul_support_prod_mk, prod.mk.eta] @[to_additive] lemma mul_support_along_fiber_subset (f : α × β → M) (a : α) : mul_support (λ b, f (a, b)) ⊆ (mul_support f).image prod.snd := by tidy @[simp, to_additive] lemma mul_support_along_fiber_finite_of_finite (f : α × β → M) (a : α) (h : (mul_support f).finite) : (mul_support (λ b, f (a, b))).finite := (h.image prod.snd).subset (mul_support_along_fiber_subset f a) end has_one @[to_additive] lemma mul_support_mul [monoid M] (f g : α → M) : mul_support (λ x, f x * g x) ⊆ mul_support f ∪ mul_support g := mul_support_binop_subset (*) (one_mul _) f g @[simp, to_additive] lemma mul_support_inv [group G] (f : α → G) : mul_support (λ x, (f x)⁻¹) = mul_support f := set.ext $ λ x, not_congr inv_eq_one @[simp, to_additive support_neg'] lemma mul_support_inv'' [group G] (f : α → G) : mul_support (f⁻¹) = mul_support f := mul_support_inv f @[simp] lemma mul_support_inv' [group_with_zero G₀] (f : α → G₀) : mul_support (λ x, (f x)⁻¹) = mul_support f := set.ext $ λ x, not_congr inv_eq_one' @[to_additive] lemma mul_support_mul_inv [group G] (f g : α → G) : mul_support (λ x, f x * (g x)⁻¹) ⊆ mul_support f ∪ mul_support g := mul_support_binop_subset (λ a b, a * b⁻¹) (by simp) f g @[to_additive support_sub] lemma mul_support_group_div [group G] (f g : α → G) : mul_support (λ x, f x / g x) ⊆ mul_support f ∪ mul_support g := mul_support_binop_subset (/) (by simp only [one_div, one_inv]) f g lemma mul_support_div [group_with_zero G₀] (f g : α → G₀) : mul_support (λ x, f x / g x) ⊆ mul_support f ∪ mul_support g := mul_support_binop_subset (/) (by simp only [div_one]) f g @[simp] lemma support_mul [mul_zero_class R] [no_zero_divisors R] (f g : α → R) : support (λ x, f x * g x) = support f ∩ support g := set.ext $ λ x, by simp only [mem_support, mul_ne_zero_iff, mem_inter_eq, not_or_distrib] lemma support_smul_subset_right [add_monoid A] [monoid B] [distrib_mul_action B A] (b : B) (f : α → A) : support (b • f) ⊆ support f := λ x hbf hf, hbf $ by rw [pi.smul_apply, hf, smul_zero] lemma support_smul_subset_left [semiring R] [add_comm_monoid M] [module R M] (f : α → R) (g : α → M) : support (f • g) ⊆ support f := λ x hfg hf, hfg $ by rw [pi.smul_apply', hf, zero_smul] lemma support_smul [semiring R] [add_comm_monoid M] [module R M] [no_zero_smul_divisors R M] (f : α → R) (g : α → M) : support (f • g) = support f ∩ support g := ext $ λ x, smul_ne_zero @[simp] lemma support_inv [group_with_zero G₀] (f : α → G₀) : support (λ x, (f x)⁻¹) = support f := set.ext $ λ x, not_congr inv_eq_zero @[simp] lemma support_div [group_with_zero G₀] (f g : α → G₀) : support (λ x, f x / g x) = support f ∩ support g := by simp [div_eq_mul_inv] @[to_additive] lemma mul_support_prod [comm_monoid M] (s : finset α) (f : α → β → M) : mul_support (λ x, ∏ i in s, f i x) ⊆ ⋃ i ∈ s, mul_support (f i) := begin rw mul_support_subset_iff', simp only [mem_Union, not_exists, nmem_mul_support], exact λ x, finset.prod_eq_one end lemma support_prod_subset [comm_monoid_with_zero A] (s : finset α) (f : α → β → A) : support (λ x, ∏ i in s, f i x) ⊆ ⋂ i ∈ s, support (f i) := λ x hx, mem_bInter_iff.2 $ λ i hi H, hx $ finset.prod_eq_zero hi H lemma support_prod [comm_monoid_with_zero A] [no_zero_divisors A] [nontrivial A] (s : finset α) (f : α → β → A) : support (λ x, ∏ i in s, f i x) = ⋂ i ∈ s, support (f i) := set.ext $ λ x, by simp only [support, ne.def, finset.prod_eq_zero_iff, mem_set_of_eq, set.mem_Inter, not_exists] lemma mul_support_one_add [has_one R] [add_left_cancel_monoid R] (f : α → R) : mul_support (λ x, 1 + f x) = support f := set.ext $ λ x, not_congr add_right_eq_self lemma mul_support_one_add' [has_one R] [add_left_cancel_monoid R] (f : α → R) : mul_support (1 + f) = support f := mul_support_one_add f lemma mul_support_add_one [has_one R] [add_right_cancel_monoid R] (f : α → R) : mul_support (λ x, f x + 1) = support f := set.ext $ λ x, not_congr add_left_eq_self lemma mul_support_add_one' [has_one R] [add_right_cancel_monoid R] (f : α → R) : mul_support (f + 1) = support f := mul_support_add_one f lemma mul_support_one_sub' [has_one R] [add_group R] (f : α → R) : mul_support (1 - f) = support f := by rw [sub_eq_add_neg, mul_support_one_add', support_neg'] lemma mul_support_one_sub [has_one R] [add_group R] (f : α → R) : mul_support (λ x, 1 - f x) = support f := mul_support_one_sub' f end function namespace set open function variables {α β M : Type*} [has_one M] {f : α → M} @[to_additive] lemma image_inter_mul_support_eq {s : set β} {g : β → α} : (g '' s ∩ mul_support f) = g '' (s ∩ mul_support (f ∘ g)) := by rw [mul_support_comp_eq_preimage f g, image_inter_preimage] end set namespace pi variables {A : Type*} {B : Type*} [decidable_eq A] [has_zero B] {a : A} {b : B} lemma support_single_zero : function.support (pi.single a (0 : B)) = ∅ := by simp @[simp] lemma support_single_of_ne (h : b ≠ 0) : function.support (pi.single a b) = {a} := begin ext, simp only [mem_singleton_iff, ne.def, function.mem_support], split, { contrapose!, exact λ h', single_eq_of_ne h' b }, { rintro rfl, rw single_eq_same, exact h } end lemma support_single [decidable_eq B] : function.support (pi.single a b) = if b = 0 then ∅ else {a} := by { split_ifs with h; simp [h] } lemma support_single_subset : function.support (pi.single a b) ⊆ {a} := begin classical, rw support_single, split_ifs; simp end lemma support_single_disjoint {b' : B} (hb : b ≠ 0) (hb' : b' ≠ 0) {i j : A} : disjoint (function.support (single i b)) (function.support (single j b')) ↔ i ≠ j := by simpa [support_single, hb, hb'] using ne_comm end pi
2fd624901e028d1c888e0ad8d36e0adfa61ec12d
8d65764a9e5f0923a67fc435eb1a5a1d02fd80e3
/src/analysis/normed_space/bounded_linear_maps.lean
c3952efd434ddb23a7cb7d786ae967d816c151a1
[ "Apache-2.0" ]
permissive
troyjlee/mathlib
e18d4b8026e32062ab9e89bc3b003a5d1cfec3f5
45e7eb8447555247246e3fe91c87066506c14875
refs/heads/master
1,689,248,035,046
1,629,470,528,000
1,629,470,528,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
22,049
lean
/- Copyright (c) 2018 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot, Johannes Hölzl -/ import analysis.normed_space.multilinear /-! # Continuous linear maps This file defines a class stating that a map between normed vector spaces is (bi)linear and continuous. Instead of asking for continuity, the definition takes the equivalent condition (because the space is normed) that `∥f x∥` is bounded by a multiple of `∥x∥`. Hence the "bounded" in the name refers to `∥f x∥/∥x∥` rather than `∥f x∥` itself. ## Main declarations * `is_bounded_linear_map`: Class stating that a map `f : E → F` is linear and has `∥f x∥` bounded by a multiple of `∥x∥`. * `is_bounded_bilinear_map`: Class stating that a map `f : E × F → G` is bilinear and continuous, but through the simpler to provide statement that `∥f (x, y)∥` is bounded by a multiple of `∥x∥ * ∥y∥` * `is_bounded_bilinear_map.linear_deriv`: Derivative of a continuous bilinear map as a linear map. * `is_bounded_bilinear_map.deriv`: Derivative of a continuous bilinear map as a continuous linear map. The proof that it is indeed the derivative is `is_bounded_bilinear_map.has_fderiv_at` in `analysis.calculus.fderiv`. * `linear_map.norm_apply_of_isometry`: A linear isometry preserves the norm. ## Notes The main use of this file is `is_bounded_bilinear_map`. The file `analysis.normed_space.multilinear` already expounds the theory of multilinear maps, but the `2`-variables case is sufficiently simpler to currently deserve its own treatment. `is_bounded_linear_map` is effectively an unbundled version of `continuous_linear_map` (defined in `topology.algebra.module`, theory over normed spaces developed in `analysis.normed_space.operator_norm`), albeit the name disparity. A bundled `continuous_linear_map` is to be preferred over a `is_bounded_linear_map` hypothesis. Historical artifact, really. -/ noncomputable theory open_locale classical big_operators topological_space open filter (tendsto) metric variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {F : Type*} [normed_group F] [normed_space 𝕜 F] {G : Type*} [normed_group G] [normed_space 𝕜 G] /-- A function `f` satisfies `is_bounded_linear_map 𝕜 f` if it is linear and satisfies the inequality `∥f x∥ ≤ M * ∥x∥` for some positive constant `M`. -/ structure is_bounded_linear_map (𝕜 : Type*) [normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {F : Type*} [normed_group F] [normed_space 𝕜 F] (f : E → F) extends is_linear_map 𝕜 f : Prop := (bound : ∃ M, 0 < M ∧ ∀ x : E, ∥f x∥ ≤ M * ∥x∥) lemma is_linear_map.with_bound {f : E → F} (hf : is_linear_map 𝕜 f) (M : ℝ) (h : ∀ x : E, ∥f x∥ ≤ M * ∥x∥) : is_bounded_linear_map 𝕜 f := ⟨ hf, classical.by_cases (assume : M ≤ 0, ⟨1, zero_lt_one, λ x, (h x).trans $ mul_le_mul_of_nonneg_right (this.trans zero_le_one) (norm_nonneg x)⟩) (assume : ¬ M ≤ 0, ⟨M, lt_of_not_ge this, h⟩)⟩ /-- A continuous linear map satisfies `is_bounded_linear_map` -/ lemma continuous_linear_map.is_bounded_linear_map (f : E →L[𝕜] F) : is_bounded_linear_map 𝕜 f := { bound := f.bound, ..f.to_linear_map.is_linear } namespace is_bounded_linear_map /-- Construct a linear map from a function `f` satisfying `is_bounded_linear_map 𝕜 f`. -/ def to_linear_map (f : E → F) (h : is_bounded_linear_map 𝕜 f) : E →ₗ[𝕜] F := (is_linear_map.mk' _ h.to_is_linear_map) /-- Construct a continuous linear map from is_bounded_linear_map -/ def to_continuous_linear_map {f : E → F} (hf : is_bounded_linear_map 𝕜 f) : E →L[𝕜] F := { cont := let ⟨C, Cpos, hC⟩ := hf.bound in linear_map.continuous_of_bound _ C hC, ..to_linear_map f hf} lemma zero : is_bounded_linear_map 𝕜 (λ (x:E), (0:F)) := (0 : E →ₗ F).is_linear.with_bound 0 $ by simp [le_refl] lemma id : is_bounded_linear_map 𝕜 (λ (x:E), x) := linear_map.id.is_linear.with_bound 1 $ by simp [le_refl] lemma fst : is_bounded_linear_map 𝕜 (λ x : E × F, x.1) := begin refine (linear_map.fst 𝕜 E F).is_linear.with_bound 1 (λ x, _), rw one_mul, exact le_max_left _ _ end lemma snd : is_bounded_linear_map 𝕜 (λ x : E × F, x.2) := begin refine (linear_map.snd 𝕜 E F).is_linear.with_bound 1 (λ x, _), rw one_mul, exact le_max_right _ _ end variables {f g : E → F} lemma smul (c : 𝕜) (hf : is_bounded_linear_map 𝕜 f) : is_bounded_linear_map 𝕜 (c • f) := let ⟨hlf, M, hMp, hM⟩ := hf in (c • hlf.mk' f).is_linear.with_bound (∥c∥ * M) $ λ x, calc ∥c • f x∥ = ∥c∥ * ∥f x∥ : norm_smul c (f x) ... ≤ ∥c∥ * (M * ∥x∥) : mul_le_mul_of_nonneg_left (hM _) (norm_nonneg _) ... = (∥c∥ * M) * ∥x∥ : (mul_assoc _ _ _).symm lemma neg (hf : is_bounded_linear_map 𝕜 f) : is_bounded_linear_map 𝕜 (λ e, -f e) := begin rw show (λ e, -f e) = (λ e, (-1 : 𝕜) • f e), { funext, simp }, exact smul (-1) hf end lemma add (hf : is_bounded_linear_map 𝕜 f) (hg : is_bounded_linear_map 𝕜 g) : is_bounded_linear_map 𝕜 (λ e, f e + g e) := let ⟨hlf, Mf, hMfp, hMf⟩ := hf in let ⟨hlg, Mg, hMgp, hMg⟩ := hg in (hlf.mk' _ + hlg.mk' _).is_linear.with_bound (Mf + Mg) $ λ x, calc ∥f x + g x∥ ≤ Mf * ∥x∥ + Mg * ∥x∥ : norm_add_le_of_le (hMf x) (hMg x) ... ≤ (Mf + Mg) * ∥x∥ : by rw add_mul lemma sub (hf : is_bounded_linear_map 𝕜 f) (hg : is_bounded_linear_map 𝕜 g) : is_bounded_linear_map 𝕜 (λ e, f e - g e) := by simpa [sub_eq_add_neg] using add hf (neg hg) lemma comp {g : F → G} (hg : is_bounded_linear_map 𝕜 g) (hf : is_bounded_linear_map 𝕜 f) : is_bounded_linear_map 𝕜 (g ∘ f) := (hg.to_continuous_linear_map.comp hf.to_continuous_linear_map).is_bounded_linear_map protected lemma tendsto (x : E) (hf : is_bounded_linear_map 𝕜 f) : tendsto f (𝓝 x) (𝓝 (f x)) := let ⟨hf, M, hMp, hM⟩ := hf in tendsto_iff_norm_tendsto_zero.2 $ squeeze_zero (λ e, norm_nonneg _) (λ e, calc ∥f e - f x∥ = ∥hf.mk' f (e - x)∥ : by rw (hf.mk' _).map_sub e x; refl ... ≤ M * ∥e - x∥ : hM (e - x)) (suffices tendsto (λ (e : E), M * ∥e - x∥) (𝓝 x) (𝓝 (M * 0)), by simpa, tendsto_const_nhds.mul (tendsto_norm_sub_self _)) lemma continuous (hf : is_bounded_linear_map 𝕜 f) : continuous f := continuous_iff_continuous_at.2 $ λ _, hf.tendsto _ lemma lim_zero_bounded_linear_map (hf : is_bounded_linear_map 𝕜 f) : tendsto f (𝓝 0) (𝓝 0) := (hf.1.mk' _).map_zero ▸ continuous_iff_continuous_at.1 hf.continuous 0 section open asymptotics filter theorem is_O_id {f : E → F} (h : is_bounded_linear_map 𝕜 f) (l : filter E) : is_O f (λ x, x) l := let ⟨M, hMp, hM⟩ := h.bound in is_O.of_bound _ (mem_of_superset univ_mem (λ x _, hM x)) theorem is_O_comp {E : Type*} {g : F → G} (hg : is_bounded_linear_map 𝕜 g) {f : E → F} (l : filter E) : is_O (λ x', g (f x')) f l := (hg.is_O_id ⊤).comp_tendsto le_top theorem is_O_sub {f : E → F} (h : is_bounded_linear_map 𝕜 f) (l : filter E) (x : E) : is_O (λ x', f (x' - x)) (λ x', x' - x) l := is_O_comp h l end end is_bounded_linear_map section variables {ι : Type*} [decidable_eq ι] [fintype ι] /-- Taking the cartesian product of two continuous multilinear maps is a bounded linear operation. -/ lemma is_bounded_linear_map_prod_multilinear {E : ι → Type*} [∀ i, normed_group (E i)] [∀ i, normed_space 𝕜 (E i)] : is_bounded_linear_map 𝕜 (λ p : (continuous_multilinear_map 𝕜 E F) × (continuous_multilinear_map 𝕜 E G), p.1.prod p.2) := { map_add := λ p₁ p₂, by { ext1 m, refl }, map_smul := λ c p, by { ext1 m, refl }, bound := ⟨1, zero_lt_one, λ p, begin rw one_mul, apply continuous_multilinear_map.op_norm_le_bound _ (norm_nonneg _) (λ m, _), rw [continuous_multilinear_map.prod_apply, norm_prod_le_iff], split, { exact (p.1.le_op_norm m).trans (mul_le_mul_of_nonneg_right (norm_fst_le p) (finset.prod_nonneg (λ i hi, norm_nonneg _))) }, { exact (p.2.le_op_norm m).trans (mul_le_mul_of_nonneg_right (norm_snd_le p) (finset.prod_nonneg (λ i hi, norm_nonneg _))) }, end⟩ } /-- Given a fixed continuous linear map `g`, associating to a continuous multilinear map `f` the continuous multilinear map `f (g m₁, ..., g mₙ)` is a bounded linear operation. -/ lemma is_bounded_linear_map_continuous_multilinear_map_comp_linear (g : G →L[𝕜] E) : is_bounded_linear_map 𝕜 (λ f : continuous_multilinear_map 𝕜 (λ (i : ι), E) F, f.comp_continuous_linear_map (λ _, g)) := begin refine is_linear_map.with_bound ⟨λ f₁ f₂, by { ext m, refl }, λ c f, by { ext m, refl }⟩ (∥g∥ ^ (fintype.card ι)) (λ f, _), apply continuous_multilinear_map.op_norm_le_bound _ _ (λ m, _), { apply_rules [mul_nonneg, pow_nonneg, norm_nonneg] }, calc ∥f (g ∘ m)∥ ≤ ∥f∥ * ∏ i, ∥g (m i)∥ : f.le_op_norm _ ... ≤ ∥f∥ * ∏ i, (∥g∥ * ∥m i∥) : begin apply mul_le_mul_of_nonneg_left _ (norm_nonneg _), exact finset.prod_le_prod (λ i hi, norm_nonneg _) (λ i hi, g.le_op_norm _) end ... = ∥g∥ ^ fintype.card ι * ∥f∥ * ∏ i, ∥m i∥ : by { simp [finset.prod_mul_distrib, finset.card_univ], ring } end end section bilinear_map variable (𝕜) /-- A map `f : E × F → G` satisfies `is_bounded_bilinear_map 𝕜 f` if it is bilinear and continuous. -/ structure is_bounded_bilinear_map (f : E × F → G) : Prop := (add_left : ∀ (x₁ x₂ : E) (y : F), f (x₁ + x₂, y) = f (x₁, y) + f (x₂, y)) (smul_left : ∀ (c : 𝕜) (x : E) (y : F), f (c • x, y) = c • f (x, y)) (add_right : ∀ (x : E) (y₁ y₂ : F), f (x, y₁ + y₂) = f (x, y₁) + f (x, y₂)) (smul_right : ∀ (c : 𝕜) (x : E) (y : F), f (x, c • y) = c • f (x,y)) (bound : ∃ C > 0, ∀ (x : E) (y : F), ∥f (x, y)∥ ≤ C * ∥x∥ * ∥y∥) variable {𝕜} variable {f : E × F → G} lemma continuous_linear_map.is_bounded_bilinear_map (f : E →L[𝕜] F →L[𝕜] G) : is_bounded_bilinear_map 𝕜 (λ x : E × F, f x.1 x.2) := { add_left := λ x₁ x₂ y, by rw [f.map_add, continuous_linear_map.add_apply], smul_left := λ c x y, by rw [f.map_smul _, continuous_linear_map.smul_apply], add_right := λ x, (f x).map_add, smul_right := λ c x y, (f x).map_smul c y, bound := ⟨max ∥f∥ 1, zero_lt_one.trans_le (le_max_right _ _), λ x y, (f.le_op_norm₂ x y).trans $ by apply_rules [mul_le_mul_of_nonneg_right, norm_nonneg, le_max_left]⟩ } protected lemma is_bounded_bilinear_map.is_O (h : is_bounded_bilinear_map 𝕜 f) : asymptotics.is_O f (λ p : E × F, ∥p.1∥ * ∥p.2∥) ⊤ := let ⟨C, Cpos, hC⟩ := h.bound in asymptotics.is_O.of_bound _ $ filter.eventually_of_forall $ λ ⟨x, y⟩, by simpa [mul_assoc] using hC x y lemma is_bounded_bilinear_map.is_O_comp {α : Type*} (H : is_bounded_bilinear_map 𝕜 f) {g : α → E} {h : α → F} {l : filter α} : asymptotics.is_O (λ x, f (g x, h x)) (λ x, ∥g x∥ * ∥h x∥) l := H.is_O.comp_tendsto le_top protected lemma is_bounded_bilinear_map.is_O' (h : is_bounded_bilinear_map 𝕜 f) : asymptotics.is_O f (λ p : E × F, ∥p∥ * ∥p∥) ⊤ := h.is_O.trans (asymptotics.is_O_fst_prod'.norm_norm.mul asymptotics.is_O_snd_prod'.norm_norm) lemma is_bounded_bilinear_map.map_sub_left (h : is_bounded_bilinear_map 𝕜 f) {x y : E} {z : F} : f (x - y, z) = f (x, z) - f(y, z) := calc f (x - y, z) = f (x + (-1 : 𝕜) • y, z) : by simp [sub_eq_add_neg] ... = f (x, z) + (-1 : 𝕜) • f (y, z) : by simp only [h.add_left, h.smul_left] ... = f (x, z) - f (y, z) : by simp [sub_eq_add_neg] lemma is_bounded_bilinear_map.map_sub_right (h : is_bounded_bilinear_map 𝕜 f) {x : E} {y z : F} : f (x, y - z) = f (x, y) - f (x, z) := calc f (x, y - z) = f (x, y + (-1 : 𝕜) • z) : by simp [sub_eq_add_neg] ... = f (x, y) + (-1 : 𝕜) • f (x, z) : by simp only [h.add_right, h.smul_right] ... = f (x, y) - f (x, z) : by simp [sub_eq_add_neg] lemma is_bounded_bilinear_map.is_bounded_linear_map_left (h : is_bounded_bilinear_map 𝕜 f) (y : F) : is_bounded_linear_map 𝕜 (λ x, f (x, y)) := { map_add := λ x x', h.add_left _ _ _, map_smul := λ c x, h.smul_left _ _ _, bound := begin rcases h.bound with ⟨C, C_pos, hC⟩, refine ⟨C * (∥y∥ + 1), mul_pos C_pos (lt_of_lt_of_le (zero_lt_one) (by simp)), λ x, _⟩, have : ∥y∥ ≤ ∥y∥ + 1, by simp [zero_le_one], calc ∥f (x, y)∥ ≤ C * ∥x∥ * ∥y∥ : hC x y ... ≤ C * ∥x∥ * (∥y∥ + 1) : by apply_rules [norm_nonneg, mul_le_mul_of_nonneg_left, le_of_lt C_pos, mul_nonneg] ... = C * (∥y∥ + 1) * ∥x∥ : by ring end } lemma is_bounded_bilinear_map.is_bounded_linear_map_right (h : is_bounded_bilinear_map 𝕜 f) (x : E) : is_bounded_linear_map 𝕜 (λ y, f (x, y)) := { map_add := λ y y', h.add_right _ _ _, map_smul := λ c y, h.smul_right _ _ _, bound := begin rcases h.bound with ⟨C, C_pos, hC⟩, refine ⟨C * (∥x∥ + 1), mul_pos C_pos (lt_of_lt_of_le (zero_lt_one) (by simp)), λ y, _⟩, have : ∥x∥ ≤ ∥x∥ + 1, by simp [zero_le_one], calc ∥f (x, y)∥ ≤ C * ∥x∥ * ∥y∥ : hC x y ... ≤ C * (∥x∥ + 1) * ∥y∥ : by apply_rules [mul_le_mul_of_nonneg_right, norm_nonneg, mul_le_mul_of_nonneg_left, le_of_lt C_pos] end } lemma is_bounded_bilinear_map_smul {𝕜' : Type*} [normed_field 𝕜'] [normed_algebra 𝕜 𝕜'] {E : Type*} [normed_group E] [normed_space 𝕜 E] [normed_space 𝕜' E] [is_scalar_tower 𝕜 𝕜' E] : is_bounded_bilinear_map 𝕜 (λ (p : 𝕜' × E), p.1 • p.2) := { add_left := add_smul, smul_left := λ c x y, by simp [smul_assoc], add_right := smul_add, smul_right := λ c x y, by simp [smul_assoc, smul_algebra_smul_comm], bound := ⟨1, zero_lt_one, λ x y, by simp [norm_smul] ⟩ } lemma is_bounded_bilinear_map_mul : is_bounded_bilinear_map 𝕜 (λ (p : 𝕜 × 𝕜), p.1 * p.2) := by simp_rw ← smul_eq_mul; exact is_bounded_bilinear_map_smul lemma is_bounded_bilinear_map_comp : is_bounded_bilinear_map 𝕜 (λ (p : (E →L[𝕜] F) × (F →L[𝕜] G)), p.2.comp p.1) := { add_left := λ x₁ x₂ y, begin ext z, change y (x₁ z + x₂ z) = y (x₁ z) + y (x₂ z), rw y.map_add end, smul_left := λ c x y, begin ext z, change y (c • (x z)) = c • y (x z), rw continuous_linear_map.map_smul end, add_right := λ x y₁ y₂, rfl, smul_right := λ c x y, rfl, bound := ⟨1, zero_lt_one, λ x y, calc ∥continuous_linear_map.comp ((x, y).snd) ((x, y).fst)∥ ≤ ∥y∥ * ∥x∥ : continuous_linear_map.op_norm_comp_le _ _ ... = 1 * ∥x∥ * ∥ y∥ : by ring ⟩ } lemma continuous_linear_map.is_bounded_linear_map_comp_left (g : F →L[𝕜] G) : is_bounded_linear_map 𝕜 (λ (f : E →L[𝕜] F), continuous_linear_map.comp g f) := is_bounded_bilinear_map_comp.is_bounded_linear_map_left _ lemma continuous_linear_map.is_bounded_linear_map_comp_right (f : E →L[𝕜] F) : is_bounded_linear_map 𝕜 (λ (g : F →L[𝕜] G), continuous_linear_map.comp g f) := is_bounded_bilinear_map_comp.is_bounded_linear_map_right _ lemma is_bounded_bilinear_map_apply : is_bounded_bilinear_map 𝕜 (λ p : (E →L[𝕜] F) × E, p.1 p.2) := { add_left := by simp, smul_left := by simp, add_right := by simp, smul_right := by simp, bound := ⟨1, zero_lt_one, by simp [continuous_linear_map.le_op_norm]⟩ } /-- The function `continuous_linear_map.smul_right`, associating to a continuous linear map `f : E → 𝕜` and a scalar `c : F` the tensor product `f ⊗ c` as a continuous linear map from `E` to `F`, is a bounded bilinear map. -/ lemma is_bounded_bilinear_map_smul_right : is_bounded_bilinear_map 𝕜 (λ p, (continuous_linear_map.smul_right : (E →L[𝕜] 𝕜) → F → (E →L[𝕜] F)) p.1 p.2) := { add_left := λ m₁ m₂ f, by { ext z, simp [add_smul] }, smul_left := λ c m f, by { ext z, simp [mul_smul] }, add_right := λ m f₁ f₂, by { ext z, simp [smul_add] }, smul_right := λ c m f, by { ext z, simp [smul_smul, mul_comm] }, bound := ⟨1, zero_lt_one, λ m f, by simp⟩ } /-- The composition of a continuous linear map with a continuous multilinear map is a bounded bilinear operation. -/ lemma is_bounded_bilinear_map_comp_multilinear {ι : Type*} {E : ι → Type*} [decidable_eq ι] [fintype ι] [∀ i, normed_group (E i)] [∀ i, normed_space 𝕜 (E i)] : is_bounded_bilinear_map 𝕜 (λ p : (F →L[𝕜] G) × (continuous_multilinear_map 𝕜 E F), p.1.comp_continuous_multilinear_map p.2) := { add_left := λ g₁ g₂ f, by { ext m, refl }, smul_left := λ c g f, by { ext m, refl }, add_right := λ g f₁ f₂, by { ext m, simp }, smul_right := λ c g f, by { ext m, simp }, bound := ⟨1, zero_lt_one, λ g f, begin apply continuous_multilinear_map.op_norm_le_bound _ _ (λ m, _), { apply_rules [mul_nonneg, zero_le_one, norm_nonneg] }, calc ∥g (f m)∥ ≤ ∥g∥ * ∥f m∥ : g.le_op_norm _ ... ≤ ∥g∥ * (∥f∥ * ∏ i, ∥m i∥) : mul_le_mul_of_nonneg_left (f.le_op_norm _) (norm_nonneg _) ... = 1 * ∥g∥ * ∥f∥ * ∏ i, ∥m i∥ : by ring end⟩ } /-- Definition of the derivative of a bilinear map `f`, given at a point `p` by `q ↦ f(p.1, q.2) + f(q.1, p.2)` as in the standard formula for the derivative of a product. We define this function here as a linear map `E × F →ₗ[𝕜] G`, then `is_bounded_bilinear_map.deriv` strengthens it to a continuous linear map `E × F →L[𝕜] G`. ``. -/ def is_bounded_bilinear_map.linear_deriv (h : is_bounded_bilinear_map 𝕜 f) (p : E × F) : E × F →ₗ[𝕜] G := { to_fun := λ q, f (p.1, q.2) + f (q.1, p.2), map_add' := λ q₁ q₂, begin change f (p.1, q₁.2 + q₂.2) + f (q₁.1 + q₂.1, p.2) = f (p.1, q₁.2) + f (q₁.1, p.2) + (f (p.1, q₂.2) + f (q₂.1, p.2)), simp [h.add_left, h.add_right], abel end, map_smul' := λ c q, begin change f (p.1, c • q.2) + f (c • q.1, p.2) = c • (f (p.1, q.2) + f (q.1, p.2)), simp [h.smul_left, h.smul_right, smul_add] end } /-- The derivative of a bounded bilinear map at a point `p : E × F`, as a continuous linear map from `E × F` to `G`. The statement that this is indeed the derivative of `f` is `is_bounded_bilinear_map.has_fderiv_at` in `analysis.calculus.fderiv`. -/ def is_bounded_bilinear_map.deriv (h : is_bounded_bilinear_map 𝕜 f) (p : E × F) : E × F →L[𝕜] G := (h.linear_deriv p).mk_continuous_of_exists_bound $ begin rcases h.bound with ⟨C, Cpos, hC⟩, refine ⟨C * ∥p.1∥ + C * ∥p.2∥, λ q, _⟩, calc ∥f (p.1, q.2) + f (q.1, p.2)∥ ≤ C * ∥p.1∥ * ∥q.2∥ + C * ∥q.1∥ * ∥p.2∥ : norm_add_le_of_le (hC _ _) (hC _ _) ... ≤ C * ∥p.1∥ * ∥q∥ + C * ∥q∥ * ∥p.2∥ : begin apply add_le_add, exact mul_le_mul_of_nonneg_left (le_max_right _ _) (mul_nonneg (le_of_lt Cpos) (norm_nonneg _)), apply mul_le_mul_of_nonneg_right _ (norm_nonneg _), exact mul_le_mul_of_nonneg_left (le_max_left _ _) (le_of_lt Cpos), end ... = (C * ∥p.1∥ + C * ∥p.2∥) * ∥q∥ : by ring end @[simp] lemma is_bounded_bilinear_map_deriv_coe (h : is_bounded_bilinear_map 𝕜 f) (p q : E × F) : h.deriv p q = f (p.1, q.2) + f (q.1, p.2) := rfl variables (𝕜) /-- The function `lmul_left_right : 𝕜' × 𝕜' → (𝕜' →L[𝕜] 𝕜')` is a bounded bilinear map. -/ lemma continuous_linear_map.lmul_left_right_is_bounded_bilinear (𝕜' : Type*) [normed_ring 𝕜'] [normed_algebra 𝕜 𝕜'] : is_bounded_bilinear_map 𝕜 (λ p : 𝕜' × 𝕜', continuous_linear_map.lmul_left_right 𝕜 𝕜' p.1 p.2) := (continuous_linear_map.lmul_left_right 𝕜 𝕜').is_bounded_bilinear_map variables {𝕜} /-- Given a bounded bilinear map `f`, the map associating to a point `p` the derivative of `f` at `p` is itself a bounded linear map. -/ lemma is_bounded_bilinear_map.is_bounded_linear_map_deriv (h : is_bounded_bilinear_map 𝕜 f) : is_bounded_linear_map 𝕜 (λ p : E × F, h.deriv p) := begin rcases h.bound with ⟨C, Cpos : 0 < C, hC⟩, refine is_linear_map.with_bound ⟨λ p₁ p₂, _, λ c p, _⟩ (C + C) (λ p, _), { ext; simp [h.add_left, h.add_right]; abel }, { ext; simp [h.smul_left, h.smul_right, smul_add] }, { refine continuous_linear_map.op_norm_le_bound _ (mul_nonneg (add_nonneg Cpos.le Cpos.le) (norm_nonneg _)) (λ q, _), calc ∥f (p.1, q.2) + f (q.1, p.2)∥ ≤ C * ∥p.1∥ * ∥q.2∥ + C * ∥q.1∥ * ∥p.2∥ : norm_add_le_of_le (hC _ _) (hC _ _) ... ≤ C * ∥p∥ * ∥q∥ + C * ∥q∥ * ∥p∥ : by apply_rules [add_le_add, mul_le_mul, norm_nonneg, Cpos.le, le_refl, le_max_left, le_max_right, mul_nonneg] ... = (C + C) * ∥p∥ * ∥q∥ : by ring }, end end bilinear_map /-- A linear isometry preserves the norm. -/ lemma linear_map.norm_apply_of_isometry (f : E →ₗ[𝕜] F) {x : E} (hf : isometry f) : ∥f x∥ = ∥x∥ := by { simp_rw [←dist_zero_right, ←f.map_zero], exact isometry.dist_eq hf _ _ } /-- Construct a continuous linear equiv from a linear map that is also an isometry with full range. -/ def continuous_linear_equiv.of_isometry (f : E →ₗ[𝕜] F) (hf : isometry f) (hfr : f.range = ⊤) : E ≃L[𝕜] F := continuous_linear_equiv.of_homothety (linear_equiv.of_bijective f (linear_map.ker_eq_bot.mpr (isometry.injective hf)) hfr) 1 zero_lt_one (λ _, by simp [one_mul, f.norm_apply_of_isometry hf])
99592485be878cc9621b2f91e280a584622b0034
07c76fbd96ea1786cc6392fa834be62643cea420
/hott/prop_trunc.hlean
a470d4d59205f6b3c6da5f549e0465880e7537be
[ "Apache-2.0" ]
permissive
fpvandoorn/lean2
5a430a153b570bf70dc8526d06f18fc000a60ad9
0889cf65b7b3cebfb8831b8731d89c2453dd1e9f
refs/heads/master
1,592,036,508,364
1,545,093,958,000
1,545,093,958,000
75,436,854
0
0
null
1,480,718,780,000
1,480,718,780,000
null
UTF-8
Lean
false
false
1,869
hlean
/- Copyright (c) 2015 Jakob von Raumer. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jakob von Raumer, Floris van Doorn Proof of the theorem that (is_trunc n A) is a mere proposition We prove this here to avoid circular dependency of files We want to use this in .equiv; .equiv is imported by .function and .function is imported by .trunc -/ import types.pi open equiv sigma sigma.ops eq function pi namespace is_trunc definition is_contr.sigma_char (A : Type) : (Σ (center : A), Π (a : A), center = a) ≃ (is_contr A) := begin fapply equiv.MK, { intro S, exact (is_contr.mk S.1 S.2)}, { intro H, cases H with H', cases H' with ce co, exact ⟨ce, co⟩}, { intro H, cases H with H', cases H' with ce co, exact idp}, { intro S, cases S, apply idp} end definition is_trunc.pi_char (n : trunc_index) (A : Type) : (Π (x y : A), is_trunc n (x = y)) ≃ (is_trunc (n .+1) A) := begin fapply equiv.MK, { intro H, apply is_trunc_succ_intro}, { intro H x y, apply is_trunc_eq}, { intro H, cases H, apply idp}, { intro P, apply eq_of_homotopy, intro a, apply eq_of_homotopy, intro b, change is_trunc.mk (to_internal n (a = b)) = P a b, induction (P a b), apply idp}, end definition is_prop_is_trunc (n : trunc_index) : Π (A : Type), is_prop (is_trunc n A) := begin induction n, { intro A, apply is_trunc_equiv_closed _ !is_contr.sigma_char, apply is_prop.mk, intros, fapply sigma_eq, apply x.2, apply is_prop.elimo }, { intro A, exact is_trunc_equiv_closed _ !is_trunc.pi_char _ }, end local attribute is_prop_is_trunc [instance] definition is_trunc_succ_is_trunc [instance] (n m : ℕ₋₂) (A : Type) : is_trunc (n.+1) (is_trunc m A) := is_trunc_succ_of_is_prop _ _ _ end is_trunc
350df7a3949cbe1c007223734692523c8d81e8cf
e4d500be102d7cc2cf7c341f360db71d9125c19b
/src/data/nat/cast.lean
70ddfbd739b826f76de48da70878b897194e9c46
[ "Apache-2.0" ]
permissive
mgrabovsky/mathlib
3cbc6c54dab5f277f0abf4195a1b0e6e39b9971f
e397b4c1266ee241e9412e17b1dd8724f56fba09
refs/heads/master
1,664,687,987,155
1,591,255,329,000
1,591,255,329,000
269,361,264
0
0
Apache-2.0
1,591,275,784,000
1,591,275,783,000
null
UTF-8
Lean
false
false
8,555
lean
/- Copyright (c) 2014 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro Natural homomorphism from the natural numbers into a monoid with one. -/ import algebra.ordered_field import data.nat.basic namespace nat variables {α : Type*} section variables [has_zero α] [has_one α] [has_add α] /-- Canonical homomorphism from `ℕ` to a type `α` with `0`, `1` and `+`. -/ protected def cast : ℕ → α | 0 := 0 | (n+1) := cast n + 1 /-- Coercions such as `nat.cast_coe` that go from a concrete structure such as `ℕ` to an arbitrary ring `α` should be set up as follows: ```lean @[priority 900] instance : has_coe_t ℕ α := ⟨...⟩ ``` It needs to be `has_coe_t` instead of `has_coe` because otherwise type-class inference would loop when constructing the transitive coercion `ℕ → ℕ → ℕ → ...`. The reduced priority is necessary so that it doesn't conflict with instances such as `has_coe_t α (option α)`. For this to work, we reduce the priority of the `coe_base` and `coe_trans` instances because we want the instances for `has_coe_t` to be tried in the following order: 1. `has_coe_t` instances declared in mathlib (such as `has_coe_t α (with_top α)`, etc.) 2. `coe_base`, which contains instances such as `has_coe (fin n) n` 3. `nat.cast_coe : has_coe_t ℕ α` etc. 4. `coe_trans` If `coe_trans` is tried first, then `nat.cast_coe` doesn't get a chance to apply. -/ library_note "coercion into rings" attribute [instance, priority 950] coe_base attribute [instance, priority 500] coe_trans -- see note [coercion into rings] @[priority 900] instance cast_coe : has_coe_t ℕ α := ⟨nat.cast⟩ @[simp, norm_cast] theorem cast_zero : ((0 : ℕ) : α) = 0 := rfl theorem cast_add_one (n : ℕ) : ((n + 1 : ℕ) : α) = n + 1 := rfl @[simp, norm_cast, priority 500] theorem cast_succ (n : ℕ) : ((succ n : ℕ) : α) = n + 1 := rfl @[simp, norm_cast] theorem cast_ite (P : Prop) [decidable P] (m n : ℕ) : (((ite P m n) : ℕ) : α) = ite P (m : α) (n : α) := by { split_ifs; refl, } end @[simp, norm_cast] theorem cast_one [add_monoid α] [has_one α] : ((1 : ℕ) : α) = 1 := zero_add _ @[simp, norm_cast] theorem cast_add [add_monoid α] [has_one α] (m) : ∀ n, ((m + n : ℕ) : α) = m + n | 0 := (add_zero _).symm | (n+1) := show ((m + n : ℕ) : α) + 1 = m + (n + 1), by rw [cast_add n, add_assoc] /-- `coe : ℕ → α` as an `add_monoid_hom`. -/ def cast_add_monoid_hom (α : Type*) [add_monoid α] [has_one α] : ℕ →+ α := { to_fun := coe, map_add' := cast_add, map_zero' := cast_zero } lemma coe_cast_add_monoid_hom [add_monoid α] [has_one α] : (cast_add_monoid_hom α : ℕ → α) = coe := rfl @[simp, norm_cast] theorem cast_bit0 [add_monoid α] [has_one α] (n : ℕ) : ((bit0 n : ℕ) : α) = bit0 n := cast_add _ _ @[simp, norm_cast] theorem cast_bit1 [add_monoid α] [has_one α] (n : ℕ) : ((bit1 n : ℕ) : α) = bit1 n := by rw [bit1, cast_add_one, cast_bit0]; refl lemma cast_two {α : Type*} [semiring α] : ((2 : ℕ) : α) = 2 := by simp @[simp, norm_cast] theorem cast_pred [add_group α] [has_one α] : ∀ {n}, 0 < n → ((n - 1 : ℕ) : α) = n - 1 | (n+1) h := (add_sub_cancel (n:α) 1).symm @[simp, norm_cast] theorem cast_sub [add_group α] [has_one α] {m n} (h : m ≤ n) : ((n - m : ℕ) : α) = n - m := eq_sub_of_add_eq $ by rw [← cast_add, nat.sub_add_cancel h] @[simp, norm_cast] theorem cast_mul [semiring α] (m) : ∀ n, ((m * n : ℕ) : α) = m * n | 0 := (mul_zero _).symm | (n+1) := (cast_add _ _).trans $ show ((m * n : ℕ) : α) + m = m * (n + 1), by rw [cast_mul n, left_distrib, mul_one] /-- `coe : ℕ → α` as a `ring_hom` -/ def cast_ring_hom (α : Type*) [semiring α] : ℕ →+* α := { to_fun := coe, map_one' := cast_one, map_mul' := cast_mul, .. cast_add_monoid_hom α } lemma coe_cast_ring_hom [semiring α] : (cast_ring_hom α : ℕ → α) = coe := rfl theorem mul_cast_comm [semiring α] (a : α) (n : ℕ) : a * n = n * a := by induction n; simp [left_distrib, right_distrib, *] @[simp] theorem cast_nonneg [linear_ordered_semiring α] : ∀ n : ℕ, 0 ≤ (n : α) | 0 := le_refl _ | (n+1) := add_nonneg (cast_nonneg n) zero_le_one @[simp, norm_cast] theorem cast_le [linear_ordered_semiring α] : ∀ {m n : ℕ}, (m : α) ≤ n ↔ m ≤ n | 0 n := by simp [zero_le] | (m+1) 0 := by simpa [not_succ_le_zero] using lt_add_of_nonneg_of_lt (@cast_nonneg α _ m) zero_lt_one | (m+1) (n+1) := (add_le_add_iff_right 1).trans $ (@cast_le m n).trans $ (add_le_add_iff_right 1).symm @[simp, norm_cast] theorem cast_lt [linear_ordered_semiring α] {m n : ℕ} : (m : α) < n ↔ m < n := by simpa [-cast_le] using not_congr (@cast_le α _ n m) @[simp] theorem cast_pos [linear_ordered_semiring α] {n : ℕ} : (0 : α) < n ↔ 0 < n := by rw [← cast_zero, cast_lt] lemma cast_add_one_pos [linear_ordered_semiring α] (n : ℕ) : 0 < (n : α) + 1 := add_pos_of_nonneg_of_pos n.cast_nonneg zero_lt_one @[simp, norm_cast] theorem cast_min [decidable_linear_ordered_semiring α] {a b : ℕ} : (↑(min a b) : α) = min a b := by by_cases a ≤ b; simp [h, min] @[simp, norm_cast] theorem cast_max [decidable_linear_ordered_semiring α] {a b : ℕ} : (↑(max a b) : α) = max a b := by by_cases a ≤ b; simp [h, max] @[simp, norm_cast] theorem abs_cast [decidable_linear_ordered_comm_ring α] (a : ℕ) : abs (a : α) = a := abs_of_nonneg (cast_nonneg a) section linear_ordered_field variables [linear_ordered_field α] lemma inv_pos_of_nat {n : ℕ} : 0 < ((n : α) + 1)⁻¹ := inv_pos.2 $ add_pos_of_nonneg_of_pos n.cast_nonneg zero_lt_one lemma one_div_pos_of_nat {n : ℕ} : 0 < 1 / ((n : α) + 1) := by { rw one_div_eq_inv, exact inv_pos_of_nat } lemma one_div_le_one_div {n m : ℕ} (h : n ≤ m) : 1 / ((m : α) + 1) ≤ 1 / ((n : α) + 1) := by { refine one_div_le_one_div_of_le _ _, exact nat.cast_add_one_pos _, simpa } lemma one_div_lt_one_div {n m : ℕ} (h : n < m) : 1 / ((m : α) + 1) < 1 / ((n : α) + 1) := by { refine one_div_lt_one_div_of_lt _ _, exact nat.cast_add_one_pos _, simpa } end linear_ordered_field end nat lemma add_monoid_hom.eq_nat_cast {A} [add_monoid A] [has_one A] (f : ℕ →+ A) (h1 : f 1 = 1) : ∀ n : ℕ, f n = n | 0 := by simp only [nat.cast_zero, f.map_zero] | (n+1) := by simp only [nat.cast_succ, f.map_add, add_monoid_hom.eq_nat_cast n, h1] @[simp] lemma ring_hom.eq_nat_cast {R} [semiring R] (f : ℕ →+* R) (n : ℕ) : f n = n := f.to_add_monoid_hom.eq_nat_cast f.map_one n @[simp] lemma ring_hom.map_nat_cast {R S} [semiring R] [semiring S] (f : R →+* S) (n : ℕ) : f n = n := (f.comp (nat.cast_ring_hom R)).eq_nat_cast n @[simp, norm_cast] theorem nat.cast_id (n : ℕ) : ↑n = n := ((ring_hom.id ℕ).eq_nat_cast n).symm @[simp] theorem nat.cast_with_bot : ∀ (n : ℕ), @coe ℕ (with_bot ℕ) (@coe_to_lift _ _ nat.cast_coe) n = n | 0 := rfl | (n+1) := by rw [with_bot.coe_add, nat.cast_add, nat.cast_with_bot n]; refl lemma ring_hom.ext_nat {R : Type*} [semiring R] (f g : ℕ →+* R) : f = g := begin ext n, calc f n = nat.cast_ring_hom R n : ring_hom.eq_nat_cast f n ... = g n : (ring_hom.eq_nat_cast g n).symm, end instance nat.subsingleton_ring_hom {R : Type*} [semiring R] : subsingleton (ℕ →+* R) := ⟨ring_hom.ext_nat⟩ namespace with_top variables {α : Type*} [canonically_ordered_comm_semiring α] [decidable_eq α] @[simp] lemma coe_nat : ∀(n : nat), ((n : α) : with_top α) = n | 0 := rfl | (n+1) := have (((1 : nat) : α) : with_top α) = ((1 : nat) : with_top α) := rfl, by rw [nat.cast_add, coe_add, nat.cast_add, coe_nat n, this] @[simp] lemma nat_ne_top (n : nat) : (n : with_top α) ≠ ⊤ := by rw [←coe_nat n]; apply coe_ne_top @[simp] lemma top_ne_nat (n : nat) : (⊤ : with_top α) ≠ n := by rw [←coe_nat n]; apply top_ne_coe lemma add_one_le_of_lt {i n : with_top ℕ} (h : i < n) : i + 1 ≤ n := begin cases n, { exact le_top }, cases i, { exact (not_le_of_lt h le_top).elim }, exact with_top.coe_le_coe.2 (with_top.coe_lt_coe.1 h) end @[elab_as_eliminator] lemma nat_induction {P : with_top ℕ → Prop} (a : with_top ℕ) (h0 : P 0) (hsuc : ∀n:ℕ, P n → P n.succ) (htop : (∀n : ℕ, P n) → P ⊤) : P a := begin have A : ∀n:ℕ, P n, { assume n, induction n with n IH, { exact h0 }, { exact hsuc n IH } }, cases a, { exact htop A }, { exact A a } end end with_top
970bfd74ca1dbaad8d83419c88ffa36ab1165cc0
7cef822f3b952965621309e88eadf618da0c8ae9
/src/algebra/char_zero.lean
b4272aec60632947dc65cd92d42d2e418ef499d8
[ "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
3,187
lean
/- Copyright (c) 2014 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro Natural homomorphism from the natural numbers into a monoid with one. -/ import data.nat.cast algebra.group algebra.field /-- Typeclass for monoids with characteristic zero. (This is usually stated on fields but it makes sense for any additive monoid with 1.) -/ class char_zero (α : Type*) [add_monoid α] [has_one α] : Prop := (cast_inj : ∀ {m n : ℕ}, (m : α) = n ↔ m = n) theorem char_zero_of_inj_zero {α : Type*} [add_monoid α] [has_one α] (add_left_cancel : ∀ a b c : α, a + b = a + c → b = c) (H : ∀ n:ℕ, (n:α) = 0 → n = 0) : char_zero α := ⟨λ m n, ⟨suffices ∀ {m n : ℕ}, (m:α) = n → m ≤ n, from λ h, le_antisymm (this h) (this h.symm), λ m n h, (le_total m n).elim id $ λ h2, le_of_eq $ begin cases nat.le.dest h2 with k e, suffices : k = 0, {rw [← e, this, add_zero]}, apply H, apply add_left_cancel n, rw [← nat.cast_add, e, add_zero, h] end, congr_arg _⟩⟩ theorem add_group.char_zero_of_inj_zero {α : Type*} [add_group α] [has_one α] (H : ∀ n:ℕ, (n:α) = 0 → n = 0) : char_zero α := char_zero_of_inj_zero (@add_left_cancel _ _) H theorem ordered_cancel_comm_monoid.char_zero_of_inj_zero {α : Type*} [ordered_cancel_comm_monoid α] [has_one α] (H : ∀ n:ℕ, (n:α) = 0 → n = 0) : char_zero α := char_zero_of_inj_zero (@add_left_cancel _ _) H @[priority 100] -- see Note [lower instance priority] instance linear_ordered_semiring.to_char_zero {α : Type*} [linear_ordered_semiring α] : char_zero α := ordered_cancel_comm_monoid.char_zero_of_inj_zero $ λ n h, nat.eq_zero_of_le_zero $ (@nat.cast_le α _ _ _).1 (le_of_eq h) namespace nat variables {α : Type*} [add_monoid α] [has_one α] [char_zero α] @[simp, elim_cast] theorem cast_inj {m n : ℕ} : (m : α) = n ↔ m = n := char_zero.cast_inj _ theorem cast_injective : function.injective (coe : ℕ → α) | m n := cast_inj.1 @[simp] theorem cast_eq_zero {n : ℕ} : (n : α) = 0 ↔ n = 0 := by rw [← cast_zero, cast_inj] @[simp] theorem cast_ne_zero {n : ℕ} : (n : α) ≠ 0 ↔ n ≠ 0 := not_congr cast_eq_zero end nat @[field_simps] lemma two_ne_zero' {α : Type*} [add_monoid α] [has_one α] [char_zero α] : (2:α) ≠ 0 := have ((2:ℕ):α) ≠ 0, from nat.cast_ne_zero.2 dec_trivial, by rwa [nat.cast_succ, nat.cast_one] at this section variables {α : Type*} [domain α] [char_zero α] lemma add_self_eq_zero {a : α} : a + a = 0 ↔ a = 0 := by simp only [(two_mul a).symm, mul_eq_zero, two_ne_zero', false_or] lemma bit0_eq_zero {a : α} : bit0 a = 0 ↔ a = 0 := add_self_eq_zero end section variables {α : Type*} [division_ring α] [char_zero α] @[simp] lemma half_add_self (a : α) : (a + a) / 2 = a := by rw [← mul_two, mul_div_cancel a two_ne_zero'] @[simp] lemma add_halves' (a : α) : a / 2 + a / 2 = a := by rw [← add_div, half_add_self] lemma sub_half (a : α) : a - a / 2 = a / 2 := by rw [sub_eq_iff_eq_add, add_halves'] lemma half_sub (a : α) : a / 2 - a = - (a / 2) := by rw [← neg_sub, sub_half] end
1ba056c7d6ffd899ab583ad32a25bad15204f5c1
9dc8cecdf3c4634764a18254e94d43da07142918
/src/analysis/normed/group/SemiNormedGroup.lean
cb87e2c2ba58f99af16515e0c557607d173bc6b8
[ "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,891
lean
/- Copyright (c) 2021 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Riccardo Brasca -/ import analysis.normed.group.hom import category_theory.limits.shapes.zero_morphisms import category_theory.concrete_category.bundled_hom import category_theory.elementwise /-! # The category of seminormed groups We define `SemiNormedGroup`, the category of seminormed groups and normed group homs between them, as well as `SemiNormedGroup₁`, the subcategory of norm non-increasing morphisms. -/ noncomputable theory universes u open category_theory /-- The category of seminormed abelian groups and bounded group homomorphisms. -/ def SemiNormedGroup : Type (u+1) := bundled seminormed_add_comm_group namespace SemiNormedGroup instance bundled_hom : bundled_hom @normed_add_group_hom := ⟨@normed_add_group_hom.to_fun, @normed_add_group_hom.id, @normed_add_group_hom.comp, @normed_add_group_hom.coe_inj⟩ attribute [derive [large_category, concrete_category]] SemiNormedGroup instance : has_coe_to_sort SemiNormedGroup (Type u) := bundled.has_coe_to_sort /-- Construct a bundled `SemiNormedGroup` from the underlying type and typeclass. -/ def of (M : Type u) [seminormed_add_comm_group M] : SemiNormedGroup := bundled.of M instance (M : SemiNormedGroup) : seminormed_add_comm_group M := M.str @[simp] lemma coe_of (V : Type u) [seminormed_add_comm_group V] : (SemiNormedGroup.of V : Type u) = V := rfl @[simp] lemma coe_id (V : SemiNormedGroup) : ⇑(𝟙 V) = id := rfl @[simp] lemma coe_comp {M N K : SemiNormedGroup} (f : M ⟶ N) (g : N ⟶ K) : ((f ≫ g) : M → K) = g ∘ f := rfl instance : inhabited SemiNormedGroup := ⟨of punit⟩ instance of_unique (V : Type u) [seminormed_add_comm_group V] [i : unique V] : unique (SemiNormedGroup.of V) := i instance : limits.has_zero_morphisms.{u (u+1)} SemiNormedGroup := {} @[simp] lemma zero_apply {V W : SemiNormedGroup} (x : V) : (0 : V ⟶ W) x = 0 := rfl lemma is_zero_of_subsingleton (V : SemiNormedGroup) [subsingleton V] : limits.is_zero V := begin refine ⟨λ X, ⟨⟨⟨0⟩, λ f, _⟩⟩, λ X, ⟨⟨⟨0⟩, λ f, _⟩⟩⟩, { ext, have : x = 0 := subsingleton.elim _ _, simp only [this, map_zero], }, { ext, apply subsingleton.elim } end instance has_zero_object : limits.has_zero_object SemiNormedGroup.{u} := ⟨⟨of punit, is_zero_of_subsingleton _⟩⟩ lemma iso_isometry_of_norm_noninc {V W : SemiNormedGroup} (i : V ≅ W) (h1 : i.hom.norm_noninc) (h2 : i.inv.norm_noninc) : isometry i.hom := begin apply add_monoid_hom_class.isometry_of_norm, intro v, apply le_antisymm (h1 v), calc ∥v∥ = ∥i.inv (i.hom v)∥ : by rw [iso.hom_inv_id_apply] ... ≤ ∥i.hom v∥ : h2 _, end end SemiNormedGroup /-- `SemiNormedGroup₁` is a type synonym for `SemiNormedGroup`, which we shall equip with the category structure consisting only of the norm non-increasing maps. -/ def SemiNormedGroup₁ : Type (u+1) := bundled seminormed_add_comm_group namespace SemiNormedGroup₁ instance : has_coe_to_sort SemiNormedGroup₁ (Type u) := bundled.has_coe_to_sort instance : large_category.{u} SemiNormedGroup₁ := { hom := λ X Y, { f : normed_add_group_hom X Y // f.norm_noninc }, id := λ X, ⟨normed_add_group_hom.id X, normed_add_group_hom.norm_noninc.id⟩, comp := λ X Y Z f g, ⟨(g : normed_add_group_hom Y Z).comp (f : normed_add_group_hom X Y), g.2.comp f.2⟩ } @[ext] lemma hom_ext {M N : SemiNormedGroup₁} (f g : M ⟶ N) (w : (f : M → N) = (g : M → N)) : f = g := subtype.eq (normed_add_group_hom.ext (congr_fun w)) instance : concrete_category.{u} SemiNormedGroup₁ := { forget := { obj := λ X, X, map := λ X Y f, f, }, forget_faithful := {} } /-- Construct a bundled `SemiNormedGroup₁` from the underlying type and typeclass. -/ def of (M : Type u) [seminormed_add_comm_group M] : SemiNormedGroup₁ := bundled.of M instance (M : SemiNormedGroup₁) : seminormed_add_comm_group M := M.str /-- Promote a morphism in `SemiNormedGroup` to a morphism in `SemiNormedGroup₁`. -/ def mk_hom {M N : SemiNormedGroup} (f : M ⟶ N) (i : f.norm_noninc) : SemiNormedGroup₁.of M ⟶ SemiNormedGroup₁.of N := ⟨f, i⟩ @[simp] lemma mk_hom_apply {M N : SemiNormedGroup} (f : M ⟶ N) (i : f.norm_noninc) (x) : mk_hom f i x = f x := rfl /-- Promote an isomorphism in `SemiNormedGroup` to an isomorphism in `SemiNormedGroup₁`. -/ @[simps] def mk_iso {M N : SemiNormedGroup} (f : M ≅ N) (i : f.hom.norm_noninc) (i' : f.inv.norm_noninc) : SemiNormedGroup₁.of M ≅ SemiNormedGroup₁.of N := { hom := mk_hom f.hom i, inv := mk_hom f.inv i', hom_inv_id' := by { apply subtype.eq, exact f.hom_inv_id, }, inv_hom_id' := by { apply subtype.eq, exact f.inv_hom_id, }, } instance : has_forget₂ SemiNormedGroup₁ SemiNormedGroup := { forget₂ := { obj := λ X, X, map := λ X Y f, f.1, }, } @[simp] lemma coe_of (V : Type u) [seminormed_add_comm_group V] : (SemiNormedGroup₁.of V : Type u) = V := rfl @[simp] lemma coe_id (V : SemiNormedGroup₁) : ⇑(𝟙 V) = id := rfl @[simp] lemma coe_comp {M N K : SemiNormedGroup₁} (f : M ⟶ N) (g : N ⟶ K) : ((f ≫ g) : M → K) = g ∘ f := rfl -- If `coe_fn_coe_base` fires before `coe_comp`, `coe_comp'` puts us back in normal form. @[simp] lemma coe_comp' {M N K : SemiNormedGroup₁} (f : M ⟶ N) (g : N ⟶ K) : ((f ≫ g) : normed_add_group_hom M K) = (↑g : normed_add_group_hom N K).comp ↑f := rfl instance : inhabited SemiNormedGroup₁ := ⟨of punit⟩ instance of_unique (V : Type u) [seminormed_add_comm_group V] [i : unique V] : unique (SemiNormedGroup₁.of V) := i instance : limits.has_zero_morphisms.{u (u+1)} SemiNormedGroup₁ := { has_zero := λ X Y, { zero := ⟨0, normed_add_group_hom.norm_noninc.zero⟩, }, comp_zero' := λ X Y f Z, by { ext, refl, }, zero_comp' := λ X Y Z f, by { ext, simp [coe_fn_coe_base'] } } @[simp] lemma zero_apply {V W : SemiNormedGroup₁} (x : V) : (0 : V ⟶ W) x = 0 := rfl lemma is_zero_of_subsingleton (V : SemiNormedGroup₁) [subsingleton V] : limits.is_zero V := begin refine ⟨λ X, ⟨⟨⟨0⟩, λ f, _⟩⟩, λ X, ⟨⟨⟨0⟩, λ f, _⟩⟩⟩, { ext, have : x = 0 := subsingleton.elim _ _, simp only [this, map_zero], exact map_zero f.1 }, { ext, apply subsingleton.elim } end instance has_zero_object : limits.has_zero_object SemiNormedGroup₁.{u} := ⟨⟨of punit, is_zero_of_subsingleton _⟩⟩ lemma iso_isometry {V W : SemiNormedGroup₁} (i : V ≅ W) : isometry i.hom := begin change isometry (i.hom : V →+ W), refine add_monoid_hom_class.isometry_of_norm i.hom _, intro v, apply le_antisymm (i.hom.2 v), calc ∥v∥ = ∥i.inv (i.hom v)∥ : by rw [iso.hom_inv_id_apply] ... ≤ ∥i.hom v∥ : i.inv.2 _, end end SemiNormedGroup₁
be45d6847710fbdf279946bd0a1ecf56f28d7474
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/Lean/Elab/Tactic/Conv/Delta.lean
55b0a5e9d76e8caeb7768b24dfceeb918781cd37
[ "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
584
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Elab.Tactic.Delta import Lean.Elab.Tactic.Conv.Basic namespace Lean.Elab.Tactic.Conv open Meta @[builtin_tactic Lean.Parser.Tactic.Conv.delta] def evalDelta : Tactic := fun stx => withMainContext do let declNames ← stx[1].getArgs.mapM resolveGlobalConstNoOverloadWithInfo let lhsNew ← deltaExpand (← instantiateMVars (← getLhs)) declNames.contains changeLhs lhsNew end Lean.Elab.Tactic.Conv
0839585523e09dc7053f98bf5ee713229b48bffc
dd4e652c749fea9ac77e404005cb3470e5f75469
/src/alex_playground/uncurr.lean
70fdeaf3530a42c3daf380f51de096597cb98248
[]
no_license
skbaek/cvx
e32822ad5943541539966a37dee162b0a5495f55
c50c790c9116f9fac8dfe742903a62bdd7292c15
refs/heads/master
1,623,803,010,339
1,618,058,958,000
1,618,058,958,000
176,293,135
3
2
null
null
null
null
UTF-8
Lean
false
false
1,399
lean
import data.real.basic import tactic.interactive import order.liminf_limsup -- copied from current mathlib section uncurry variables {α β γ δ : Type*} /-- Records a way to turn an element of `α` into a function from `β` to `γ`. The most generic use is to recursively uncurry. For instance `f : α → β → γ → δ` will be turned into `↿f : α × β × γ → δ`. One can also add instances for bundled maps. -/ class has_uncurry (α : Type*) (β : out_param Type*) (γ : out_param Type*) := (uncurry : α → (β → γ)) notation `↿`:max x:max := has_uncurry.uncurry x instance has_uncurry_base : has_uncurry (α → β) α β := ⟨id⟩ instance has_uncurry_induction [has_uncurry β γ δ] : has_uncurry (α → β) (α × γ) δ := ⟨λ f p, ↿(f p.1) p.2⟩ end uncurry def solution {α α' β γ : Type*} [has_uncurry α β γ] [has_uncurry α' β Prop] [has_le γ] (objfun : α) (feasable : α') := { x : β // ↿feasable x ∧ ∀ y : β, ↿feasable y → ↿objfun y ≤ ↿objfun x } notation `over ` binders ` maximize ` f:(scoped Q, Q) ` subject ` p:(scoped P, P) := solution f p def mysolution : over (x y : ℝ) maximize x + y subject x ≤ 0 ∧ y ≤ 0 := begin use (0, 0), split, split, simp, simp, intros y hy, change y.1 + y.2 ≤ 0 + 0, change y.1 ≤ 0 ∧ y.2 ≤ 0 at hy, linarith, end #print mysolution
63c098e3c7866aee1ef6aaa31cf346cdcf182ea0
4727251e0cd73359b15b664c3170e5d754078599
/src/category_theory/limits/shapes/regular_mono.lean
5713672ddc2bcb0852f54fc24f3487edf56b2ea3
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
11,076
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Bhavik Mehta -/ import category_theory.limits.shapes.pullbacks import category_theory.limits.shapes.strong_epi import category_theory.limits.shapes.equalizers /-! # Definitions and basic properties of regular monomorphisms and epimorphisms. A regular monomorphism is a morphism that is the equalizer of some parallel pair. We give the constructions * `split_mono → regular_mono` and * `regular_mono → mono` as well as the dual constructions for regular epimorphisms. Additionally, we give the construction * `regular_epi ⟶ strong_epi`. We also define classes `regular_mono_category` and `regular_epi_category` for categories in which every monomorphism or epimorphism is regular, and deduce that these categories are `strong_mono_category`s resp. `strong_epi_category`s. -/ noncomputable theory namespace category_theory open category_theory.limits universes v₁ u₁ u₂ variables {C : Type u₁} [category.{v₁} C] variables {X Y : C} /-- A regular monomorphism is a morphism which is the equalizer of some parallel pair. -/ class regular_mono (f : X ⟶ Y) := (Z : C) (left right : Y ⟶ Z) (w : f ≫ left = f ≫ right) (is_limit : is_limit (fork.of_ι f w)) attribute [reassoc] regular_mono.w /-- Every regular monomorphism is a monomorphism. -/ @[priority 100] instance regular_mono.mono (f : X ⟶ Y) [regular_mono f] : mono f := mono_of_is_limit_fork regular_mono.is_limit instance equalizer_regular (g h : X ⟶ Y) [has_limit (parallel_pair g h)] : regular_mono (equalizer.ι g h) := { Z := Y, left := g, right := h, w := equalizer.condition g h, is_limit := fork.is_limit.mk _ (λ s, limit.lift _ s) (by simp) (λ s m w, by { ext1, simp [←w] }) } /-- Every split monomorphism is a regular monomorphism. -/ @[priority 100] instance regular_mono.of_split_mono (f : X ⟶ Y) [split_mono f] : regular_mono f := { Z := Y, left := 𝟙 Y, right := retraction f ≫ f, w := by tidy, is_limit := split_mono_equalizes f } /-- If `f` is a regular mono, then any map `k : W ⟶ Y` equalizing `regular_mono.left` and `regular_mono.right` induces a morphism `l : W ⟶ X` such that `l ≫ f = k`. -/ def regular_mono.lift' {W : C} (f : X ⟶ Y) [regular_mono f] (k : W ⟶ Y) (h : k ≫ (regular_mono.left : Y ⟶ @regular_mono.Z _ _ _ _ f _) = k ≫ regular_mono.right) : {l : W ⟶ X // l ≫ f = k} := fork.is_limit.lift' regular_mono.is_limit _ h /-- The second leg of a pullback cone is a regular monomorphism if the right component is too. See also `pullback.snd_of_mono` for the basic monomorphism version, and `regular_of_is_pullback_fst_of_regular` for the flipped version. -/ def regular_of_is_pullback_snd_of_regular {P Q R S : C} {f : P ⟶ Q} {g : P ⟶ R} {h : Q ⟶ S} {k : R ⟶ S} [hr : regular_mono h] (comm : f ≫ h = g ≫ k) (t : is_limit (pullback_cone.mk _ _ comm)) : regular_mono g := { Z := hr.Z, left := k ≫ hr.left, right := k ≫ hr.right, w := by rw [← reassoc_of comm, ← reassoc_of comm, hr.w], is_limit := begin apply fork.is_limit.mk' _ _, intro s, have l₁ : (fork.ι s ≫ k) ≫ regular_mono.left = (fork.ι s ≫ k) ≫ regular_mono.right, rw [category.assoc, s.condition, category.assoc], obtain ⟨l, hl⟩ := fork.is_limit.lift' hr.is_limit _ l₁, obtain ⟨p, hp₁, hp₂⟩ := pullback_cone.is_limit.lift' t _ _ hl, refine ⟨p, hp₂, _⟩, intros m w, have z : m ≫ g = p ≫ g := w.trans hp₂.symm, apply t.hom_ext, apply (pullback_cone.mk f g comm).equalizer_ext, { erw [← cancel_mono h, category.assoc, category.assoc, comm, reassoc_of z] }, { exact z }, end } /-- The first leg of a pullback cone is a regular monomorphism if the left component is too. See also `pullback.fst_of_mono` for the basic monomorphism version, and `regular_of_is_pullback_snd_of_regular` for the flipped version. -/ def regular_of_is_pullback_fst_of_regular {P Q R S : C} {f : P ⟶ Q} {g : P ⟶ R} {h : Q ⟶ S} {k : R ⟶ S} [hr : regular_mono k] (comm : f ≫ h = g ≫ k) (t : is_limit (pullback_cone.mk _ _ comm)) : regular_mono f := regular_of_is_pullback_snd_of_regular comm.symm (pullback_cone.flip_is_limit t) @[priority 100] instance strong_mono_of_regular_mono (f : X ⟶ Y) [regular_mono f] : strong_mono f := { mono := by apply_instance, has_lift := begin introsI, have : v ≫ (regular_mono.left : Y ⟶ regular_mono.Z f) = v ≫ regular_mono.right, { apply (cancel_epi z).1, simp only [regular_mono.w, ← reassoc_of h] }, obtain ⟨t, ht⟩ := regular_mono.lift' _ _ this, refine arrow.has_lift.mk ⟨t, (cancel_mono f).1 _, ht⟩, simp only [arrow.mk_hom, arrow.hom_mk'_left, category.assoc, ht, h] end } /-- A regular monomorphism is an isomorphism if it is an epimorphism. -/ lemma is_iso_of_regular_mono_of_epi (f : X ⟶ Y) [regular_mono f] [e : epi f] : is_iso f := is_iso_of_epi_of_strong_mono _ section variables (C) /-- A regular mono category is a category in which every monomorphism is regular. -/ class regular_mono_category := (regular_mono_of_mono : ∀ {X Y : C} (f : X ⟶ Y) [mono f], regular_mono f) end /-- In a category in which every monomorphism is regular, we can express every monomorphism as an equalizer. This is not an instance because it would create an instance loop. -/ def regular_mono_of_mono [regular_mono_category C] (f : X ⟶ Y) [mono f] : regular_mono f := regular_mono_category.regular_mono_of_mono _ @[priority 100] instance regular_mono_category_of_split_mono_category [split_mono_category C] : regular_mono_category C := { regular_mono_of_mono := λ _ _ f _, by { haveI := by exactI split_mono_of_mono f, apply_instance } } @[priority 100] instance strong_mono_category_of_regular_mono_category [regular_mono_category C] : strong_mono_category C := { strong_mono_of_mono := λ _ _ f _, by { haveI := by exactI regular_mono_of_mono f, apply_instance } } /-- A regular epimorphism is a morphism which is the coequalizer of some parallel pair. -/ class regular_epi (f : X ⟶ Y) := (W : C) (left right : W ⟶ X) (w : left ≫ f = right ≫ f) (is_colimit : is_colimit (cofork.of_π f w)) attribute [reassoc] regular_epi.w /-- Every regular epimorphism is an epimorphism. -/ @[priority 100] instance regular_epi.epi (f : X ⟶ Y) [regular_epi f] : epi f := epi_of_is_colimit_cofork regular_epi.is_colimit instance coequalizer_regular (g h : X ⟶ Y) [has_colimit (parallel_pair g h)] : regular_epi (coequalizer.π g h) := { W := X, left := g, right := h, w := coequalizer.condition g h, is_colimit := cofork.is_colimit.mk _ (λ s, colimit.desc _ s) (by simp) (λ s m w, by { ext1, simp [←w] }) } /-- Every split epimorphism is a regular epimorphism. -/ @[priority 100] instance regular_epi.of_split_epi (f : X ⟶ Y) [split_epi f] : regular_epi f := { W := X, left := 𝟙 X, right := f ≫ section_ f, w := by tidy, is_colimit := split_epi_coequalizes f } /-- If `f` is a regular epi, then every morphism `k : X ⟶ W` coequalizing `regular_epi.left` and `regular_epi.right` induces `l : Y ⟶ W` such that `f ≫ l = k`. -/ def regular_epi.desc' {W : C} (f : X ⟶ Y) [regular_epi f] (k : X ⟶ W) (h : (regular_epi.left : regular_epi.W f ⟶ X) ≫ k = regular_epi.right ≫ k) : {l : Y ⟶ W // f ≫ l = k} := cofork.is_colimit.desc' (regular_epi.is_colimit) _ h /-- The second leg of a pushout cocone is a regular epimorphism if the right component is too. See also `pushout.snd_of_epi` for the basic epimorphism version, and `regular_of_is_pushout_fst_of_regular` for the flipped version. -/ def regular_of_is_pushout_snd_of_regular {P Q R S : C} {f : P ⟶ Q} {g : P ⟶ R} {h : Q ⟶ S} {k : R ⟶ S} [gr : regular_epi g] (comm : f ≫ h = g ≫ k) (t : is_colimit (pushout_cocone.mk _ _ comm)) : regular_epi h := { W := gr.W, left := gr.left ≫ f, right := gr.right ≫ f, w := by rw [category.assoc, category.assoc, comm, reassoc_of gr.w], is_colimit := begin apply cofork.is_colimit.mk' _ _, intro s, have l₁ : gr.left ≫ f ≫ s.π = gr.right ≫ f ≫ s.π, rw [← category.assoc, ← category.assoc, s.condition], obtain ⟨l, hl⟩ := cofork.is_colimit.desc' gr.is_colimit (f ≫ cofork.π s) l₁, obtain ⟨p, hp₁, hp₂⟩ := pushout_cocone.is_colimit.desc' t _ _ hl.symm, refine ⟨p, hp₁, _⟩, intros m w, have z := w.trans hp₁.symm, apply t.hom_ext, apply (pushout_cocone.mk _ _ comm).coequalizer_ext, { exact z }, { erw [← cancel_epi g, ← reassoc_of comm, ← reassoc_of comm, z], refl }, end } /-- The first leg of a pushout cocone is a regular epimorphism if the left component is too. See also `pushout.fst_of_epi` for the basic epimorphism version, and `regular_of_is_pushout_snd_of_regular` for the flipped version. -/ def regular_of_is_pushout_fst_of_regular {P Q R S : C} {f : P ⟶ Q} {g : P ⟶ R} {h : Q ⟶ S} {k : R ⟶ S} [fr : regular_epi f] (comm : f ≫ h = g ≫ k) (t : is_colimit (pushout_cocone.mk _ _ comm)) : regular_epi k := regular_of_is_pushout_snd_of_regular comm.symm (pushout_cocone.flip_is_colimit t) @[priority 100] instance strong_epi_of_regular_epi (f : X ⟶ Y) [regular_epi f] : strong_epi f := { epi := by apply_instance, has_lift := begin introsI, have : (regular_epi.left : regular_epi.W f ⟶ X) ≫ u = regular_epi.right ≫ u, { apply (cancel_mono z).1, simp only [category.assoc, h, regular_epi.w_assoc] }, obtain ⟨t, ht⟩ := regular_epi.desc' f u this, exact arrow.has_lift.mk ⟨t, ht, (cancel_epi f).1 (by simp only [←category.assoc, ht, ←h, arrow.mk_hom, arrow.hom_mk'_right])⟩, end } /-- A regular epimorphism is an isomorphism if it is a monomorphism. -/ lemma is_iso_of_regular_epi_of_mono (f : X ⟶ Y) [regular_epi f] [m : mono f] : is_iso f := is_iso_of_mono_of_strong_epi _ section variables (C) /-- A regular epi category is a category in which every epimorphism is regular. -/ class regular_epi_category := (regular_epi_of_epi : ∀ {X Y : C} (f : X ⟶ Y) [epi f], regular_epi f) end /-- In a category in which every epimorphism is regular, we can express every epimorphism as a coequalizer. This is not an instance because it would create an instance loop. -/ def regular_epi_of_epi [regular_epi_category C] (f : X ⟶ Y) [epi f] : regular_epi f := regular_epi_category.regular_epi_of_epi _ @[priority 100] instance regular_epi_category_of_split_epi_category [split_epi_category C] : regular_epi_category C := { regular_epi_of_epi := λ _ _ f _, by { haveI := by exactI split_epi_of_epi f, apply_instance } } @[priority 100] instance strong_epi_category_of_regular_epi_category [regular_epi_category C] : strong_epi_category C := { strong_epi_of_epi := λ _ _ f _, by { haveI := by exactI regular_epi_of_epi f, apply_instance } } end category_theory
155276d603674e77cba1699bb2866e5f0e46f079
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/interactive/simple3.lean
0168fee1a9315f7342aa7c87b7ec427657d05ba6
[ "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
189
lean
import logic namespace tst definition foo {A B : Type} (a : A) (b : B) := a definition boo {A : Type} (a : A) := foo a a end tst theorem tst {A : Type} (a b : A) : tst.foo a b = a := rfl
421f8f79043798eb3b21a23a7d69c0558f81c2d7
76ce87faa6bc3c2aa9af5962009e01e04f2a074a
/HW/HW4.lean
77395a781a11cdc1052fc112af2729f3db0e7a76
[]
no_license
Mnormansell/Discrete-Notes
db423dd9206bbe7080aecb84b4c2d275b758af97
61f13b98be590269fc4822be7b47924a6ddc1261
refs/heads/master
1,585,412,435,424
1,540,919,483,000
1,540,919,483,000
148,684,638
0
0
null
null
null
null
UTF-8
Lean
false
false
6,964
lean
/- THIS ASSIGNMENT IS DUE BEFORE NEXT CLASS. WE WILL THEN REVIEW IT IN CLASS. FOLLOWING THAT REVIEW WE WILL GIVE A BRIEF IN-CLASS, GRADED EXERCISE TO TEST YOUR UNDERSTANDING. This file comes in two parts. The first part presents an example that you are to read and understand. It's an example of a proof of a bi-implication between two equivalent disjunctions. The key lesson it teaches is proof by *case analysis*. When you are asked to prove that P ∨ Q implies some conclusion, you do it by case analysis. The idea is that if P ∨ Q is true, there are two ways in which it can be true: either P is true, or Q is true. You have prove the goal follows in either case. If the goal follows in either case, then it follows from the disjunction, P ∨ Q, as a whole. We use the "cases" tactic in lean to reason about the two possible cases of a proof of P ∨ Q. Using this tactic produces two subgoals. In one case, P is assumed to have a proof, p. In the other case, Q is assumed to have a proof, q. The aim then is to show that the main goal follows in each case, as that will then prove that the goal follows from the disjunction as a whole. The second part of this file presents a problem for you to solve. One again it's a bi-implication involving a disjunction as a premise (in one direction but not in the other direction). In both of these parts, we, or you will, start by applying iff.intro to split the bi-implication into two implications to be proved. Then in the subproblems with disjunctions as premises, you will use case analysis. A hint: Remember that ¬ X means X → false, and the way to prove this is to assume X and show this leads to a contradiction. It is this maneuver that gets you a disjunction as a premise in the problem you're to solve. -/ -- PART I: A WORKED EXAMPLE /- We first show you how to prove P ∨ Q ↔ Q ∨ P. At the highest level, the proof is by showing the implication in each direction. Within each direction, the proof is by case analysis for the disjunction that forms the premise of the implication. These ideas are explained in more detail in the proof that follows. -/ theorem or_commutes : ∀ P Q: Prop, P ∨ Q ↔ Q ∨ P := begin /- Introduce the assumptions that P and Q are arbitrary propositions. -/ assume P Q: Prop, /- To prove a bi-implication, we prove the implications in each direction. That is, we break the overall goal into subgoals, one for the forward implication and one for its converse (in the other direction). -/ apply iff.intro, -- now we prove the forward implication assume pq : P ∨ Q, show Q ∨ P, from begin /- The key proof technique for reasoning from a proof of a disjunction, P ∨ Q, is to consider two possible cases for this proof, that either P is true or Q is, and to show that the goal is true in either case. This shows that no matter why P ∨ Q is true, the goal follows. So, the proof strategy is first to assumes that P ∨ Q is true because P is true, which is to say you assume you have a proof of P, and then show that the goal follows. Then you assume P ∨ Q is true because Q is true, and you show that given an assumed proof of Q, the goal follows. The goal thus follows in either case. The tactic that we use in Lean to reason by cases from a disjunction is called "cases." As arguments you give it (1) the name of the disjunction to reason from, and (2) names to use for the assumed proofs of P and Q in the respective cases (after keyword "with"). So here we go. Make sure you have the Lean Messages window open so that you can see how "cases" changes your tactic state. Note that it introduces two new subgoals. In the first, there is an assumed proof, p : P. In the second, there is an an assumed proof q : Q. -/ cases pq with p q, -- now prove each case separately -- proof for case where P is true show Q ∨ P, from or.intro_right Q p, -- proof for case where Q is true show Q ∨ P, from or.intro_left P q, end, -- and now we prove the converse assume qp : Q ∨ P, show P ∨ Q, from begin cases qp with q p, show P ∨ Q, from or.intro_right P q, show P ∨ Q, from or.intro_left Q p, end, end -- PART II: YOUR HOMEWORK ASSIGNMENT /- Homework: Prove, ∀ P Q: Prop, ¬ P ∧ ¬ Q ↔ ¬ (P ∨ Q) -/ theorem aDemorganLaw : ∀ P Q: Prop, ¬ P ∧ ¬ Q ↔ ¬ (P ∨ Q) := begin assume P Q : Prop, apply iff.intro, -- forward assume npnq : ¬ P ∧ ¬ Q, show ¬ (P ∨ Q), from begin assume pq : (P ∨ Q), cases pq with p q, show false, from npnq.1 p, show false, from npnq.2 q, end, --backwards assume nporq : ¬ (P ∨ Q), --show not p have np : ¬ P, from begin assume p : P, have pq : (P ∨ Q), from begin apply or.intro_left, show P, from p, end, <<<<<<< HEAD --left side -- to get a proof of ¬ P ∧ ¬ Q we need -- both ¬ P and ¬ Q and then use and.intro -- Show a contratradiction by first assuming -- P and Q are true, then show they must be -- false, then use and.intro assume nporq : ¬ (P ∨ Q), have np : ¬ P, from begin assume p : P, have porq : P ∨ Q, from begin apply or.intro_left, show P, from p, end, show false, from nporq porq, end, have nq : ¬ Q, from begin assume q : Q, have porq : P ∨ Q, from begin apply or.intro_right, show Q, from q, end, show false, from nporq porq, end, exact and.intro np nq ======= show false, from nporq pq, end, --show not have nq : ¬ Q, from begin assume q : Q, have pq : (P ∨ Q), from begin apply or.intro_right, show Q, from q, end, show false, from nporq pq, end, >>>>>>> 7bf877fc487881e7123ecc6a6cc372d320cd5eed end
51400210745e407d716d87b306d60fae4c99cc88
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/matrix/char_p.lean
85e4b06549949306f7aa62c586f53dc4c982fdca
[ "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
697
lean
/- Copyright (c) 2020 Aaron Anderson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Aaron Anderson -/ import data.matrix.basic import algebra.char_p.basic /-! # Matrices in prime characteristic > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. -/ open matrix variables {n : Type*} [fintype n] {R : Type*} [ring R] instance matrix.char_p [decidable_eq n] [nonempty n] (p : ℕ) [char_p R p] : char_p (matrix n n R) p := ⟨begin intro k, rw [← char_p.cast_eq_zero_iff R p k, ← nat.cast_zero, ← map_nat_cast $ scalar n], convert scalar_inj, {simp}, {assumption} end⟩
d18aa01594c46559acb8c376b540dac8825ff0a8
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/deprecated/group.lean
92263d10de9abe5cbb160cb2c73ca8fd0f1bd536
[ "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
13,641
lean
/- Copyright (c) 2019 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import algebra.group.type_tags import algebra.group.units_hom import algebra.ring.basic import data.equiv.mul_add /-! # Unbundled monoid and group homomorphisms (deprecated) This file defines typeclasses for unbundled monoid and group homomorphisms. Though these classes are deprecated, they are still widely used in mathlib, and probably will not go away before Lean 4 because Lean 3 often fails to coerce a bundled homomorphism to a function. ## main definitions is_monoid_hom (deprecated), is_group_hom (deprecated) ## implementation notes There's a coercion from bundled homs to fun, and the canonical notation is to use the bundled hom as a function via this coercion. There is no `group_hom` -- the idea is that `monoid_hom` is used. The constructor for `monoid_hom` needs a proof of `map_one` as well as `map_mul`; a separate constructor `monoid_hom.mk'` will construct group homs (i.e. monoid homs between groups) given only a proof that multiplication is preserved, ## Tags is_group_hom, is_monoid_hom, monoid_hom -/ /-- We have lemmas stating that the composition of two morphisms is again a morphism. Since composition is reducible, type class inference will always succeed in applying these instances. For example when the goal is just `⊢ is_mul_hom f` the instance `is_mul_hom.comp` will still succeed, unifying `f` with `f ∘ (λ x, x)`. This causes type class inference to loop. To avoid this, we do not make these lemmas instances. -/ library_note "no instance on morphisms" universes u v variables {α : Type u} {β : Type v} /-- Predicate for maps which preserve an addition. -/ class is_add_hom {α β : Type*} [has_add α] [has_add β] (f : α → β) : Prop := (map_add [] : ∀ x y, f (x + y) = f x + f y) /-- Predicate for maps which preserve a multiplication. -/ @[to_additive] class is_mul_hom {α β : Type*} [has_mul α] [has_mul β] (f : α → β) : Prop := (map_mul [] : ∀ x y, f (x * y) = f x * f y) namespace is_mul_hom variables [has_mul α] [has_mul β] {γ : Type*} [has_mul γ] /-- The identity map preserves multiplication. -/ @[to_additive "The identity map preserves addition"] instance id : is_mul_hom (id : α → α) := {map_mul := λ _ _, rfl} /-- The composition of maps which preserve multiplication, also preserves multiplication. -/ -- see Note [no instance on morphisms] @[to_additive "The composition of addition preserving maps also preserves addition"] lemma comp (f : α → β) (g : β → γ) [is_mul_hom f] [hg : is_mul_hom g] : is_mul_hom (g ∘ f) := { map_mul := λ x y, by simp only [function.comp, map_mul f, map_mul g] } /-- A product of maps which preserve multiplication, preserves multiplication when the target is commutative. -/ @[instance, priority 10, to_additive] lemma mul {α β} [semigroup α] [comm_semigroup β] (f g : α → β) [is_mul_hom f] [is_mul_hom g] : is_mul_hom (λa, f a * g a) := { map_mul := assume a b, by simp only [map_mul f, map_mul g, mul_comm, mul_assoc, mul_left_comm] } /-- The inverse of a map which preserves multiplication, preserves multiplication when the target is commutative. -/ @[instance, to_additive] lemma inv {α β} [has_mul α] [comm_group β] (f : α → β) [is_mul_hom f] : is_mul_hom (λa, (f a)⁻¹) := { map_mul := assume a b, (map_mul f a b).symm ▸ mul_inv _ _ } end is_mul_hom /-- Predicate for add_monoid homomorphisms (deprecated -- use the bundled `monoid_hom` version). -/ class is_add_monoid_hom [add_zero_class α] [add_zero_class β] (f : α → β) extends is_add_hom f : Prop := (map_zero [] : f 0 = 0) /-- Predicate for monoid homomorphisms (deprecated -- use the bundled `monoid_hom` version). -/ @[to_additive] class is_monoid_hom [mul_one_class α] [mul_one_class β] (f : α → β) extends is_mul_hom f : Prop := (map_one [] : f 1 = 1) namespace monoid_hom /-! Throughout this section, some `mul_one_class` arguments are specified with `{}` instead of `[]`. See note [implicit instance arguments]. -/ variables {M : Type*} {N : Type*} [mM : mul_one_class M] [mN : mul_one_class N] include mM mN /-- Interpret a map `f : M → N` as a homomorphism `M →* N`. -/ @[to_additive "Interpret a map `f : M → N` as a homomorphism `M →+ N`."] def of (f : M → N) [h : is_monoid_hom f] : M →* N := { to_fun := f, map_one' := h.2, map_mul' := h.1.1 } variables {mM mN} @[simp, to_additive] lemma coe_of (f : M → N) [is_monoid_hom f] : ⇑ (monoid_hom.of f) = f := rfl @[to_additive] instance (f : M →* N) : is_monoid_hom (f : M → N) := { map_mul := f.map_mul, map_one := f.map_one } end monoid_hom namespace mul_equiv variables {M : Type*} {N : Type*} [mul_one_class M] [mul_one_class N] /-- A multiplicative isomorphism preserves multiplication (deprecated). -/ @[to_additive] instance (h : M ≃* N) : is_mul_hom h := ⟨h.map_mul⟩ /-- A multiplicative bijection between two monoids is a monoid hom (deprecated -- use `mul_equiv.to_monoid_hom`). -/ @[to_additive] instance (h : M ≃* N) : is_monoid_hom h := ⟨h.map_one⟩ end mul_equiv namespace is_monoid_hom variables [mul_one_class α] [mul_one_class β] (f : α → β) [is_monoid_hom f] /-- A monoid homomorphism preserves multiplication. -/ @[to_additive] lemma map_mul (x y) : f (x * y) = f x * f y := is_mul_hom.map_mul f x y end is_monoid_hom /-- A map to a group preserving multiplication is a monoid homomorphism. -/ @[to_additive] theorem is_monoid_hom.of_mul [mul_one_class α] [group β] (f : α → β) [is_mul_hom f] : is_monoid_hom f := { map_one := mul_right_eq_self.1 $ by rw [← is_mul_hom.map_mul f, one_mul] } namespace is_monoid_hom variables [mul_one_class α] [mul_one_class β] (f : α → β) [is_monoid_hom f] /-- The identity map is a monoid homomorphism. -/ @[to_additive] instance id : is_monoid_hom (@id α) := { map_one := rfl } /-- The composite of two monoid homomorphisms is a monoid homomorphism. -/ @[to_additive] -- see Note [no instance on morphisms] lemma comp {γ} [mul_one_class γ] (g : β → γ) [is_monoid_hom g] : is_monoid_hom (g ∘ f) := { map_one := show g _ = 1, by rw [map_one f, map_one g], ..is_mul_hom.comp _ _ } end is_monoid_hom namespace is_add_monoid_hom /-- Left multiplication in a ring is an additive monoid morphism. -/ instance is_add_monoid_hom_mul_left {γ : Type*} [non_unital_non_assoc_semiring γ] (x : γ) : is_add_monoid_hom (λ y : γ, x * y) := { map_zero := mul_zero x, map_add := λ y z, mul_add x y z } /-- Right multiplication in a ring is an additive monoid morphism. -/ instance is_add_monoid_hom_mul_right {γ : Type*} [non_unital_non_assoc_semiring γ] (x : γ) : is_add_monoid_hom (λ y : γ, y * x) := { map_zero := zero_mul x, map_add := λ y z, add_mul y z x } end is_add_monoid_hom /-- Predicate for additive group homomorphism (deprecated -- use bundled `monoid_hom`). -/ class is_add_group_hom [add_group α] [add_group β] (f : α → β) extends is_add_hom f : Prop /-- Predicate for group homomorphisms (deprecated -- use bundled `monoid_hom`). -/ @[to_additive] class is_group_hom [group α] [group β] (f : α → β) extends is_mul_hom f : Prop @[to_additive] instance monoid_hom.is_group_hom {G H : Type*} {_ : group G} {_ : group H} (f : G →* H) : is_group_hom (f : G → H) := { map_mul := f.map_mul } @[to_additive] instance mul_equiv.is_group_hom {G H : Type*} {_ : group G} {_ : group H} (h : G ≃* H) : is_group_hom h := { map_mul := h.map_mul } /-- Construct `is_group_hom` from its only hypothesis. The default constructor tries to get `is_mul_hom` from class instances, and this makes some proofs fail. -/ @[to_additive] lemma is_group_hom.mk' [group α] [group β] {f : α → β} (hf : ∀ x y, f (x * y) = f x * f y) : is_group_hom f := { map_mul := hf } namespace is_group_hom variables [group α] [group β] (f : α → β) [is_group_hom f] open is_mul_hom (map_mul) /-- A group homomorphism is a monoid homomorphism. -/ @[priority 100, to_additive] -- see Note [lower instance priority] instance to_is_monoid_hom : is_monoid_hom f := is_monoid_hom.of_mul f /-- A group homomorphism sends 1 to 1. -/ @[to_additive] lemma map_one : f 1 = 1 := is_monoid_hom.map_one f /-- A group homomorphism sends inverses to inverses. -/ @[to_additive] theorem map_inv (a : α) : f a⁻¹ = (f a)⁻¹ := eq_inv_of_mul_eq_one $ by rw [← map_mul f, inv_mul_self, map_one f] /-- The identity is a group homomorphism. -/ @[to_additive] instance id : is_group_hom (@id α) := { } /-- The composition of two group homomorphisms is a group homomorphism. -/ @[to_additive] -- see Note [no instance on morphisms] lemma comp {γ} [group γ] (g : β → γ) [is_group_hom g] : is_group_hom (g ∘ f) := { ..is_mul_hom.comp _ _ } /-- A group homomorphism is injective iff its kernel is trivial. -/ @[to_additive] lemma injective_iff (f : α → β) [is_group_hom f] : function.injective f ↔ (∀ a, f a = 1 → a = 1) := ⟨λ h _, by rw ← is_group_hom.map_one f; exact @h _ _, λ h x y hxy, by rw [← inv_inv (f x), inv_eq_iff_mul_eq_one, ← map_inv f, ← map_mul f] at hxy; simpa using inv_eq_of_mul_eq_one (h _ hxy)⟩ /-- The product of group homomorphisms is a group homomorphism if the target is commutative. -/ @[instance, priority 10, to_additive] lemma mul {α β} [group α] [comm_group β] (f g : α → β) [is_group_hom f] [is_group_hom g] : is_group_hom (λa, f a * g a) := { } /-- The inverse of a group homomorphism is a group homomorphism if the target is commutative. -/ @[instance, to_additive] lemma inv {α β} [group α] [comm_group β] (f : α → β) [is_group_hom f] : is_group_hom (λa, (f a)⁻¹) := { } end is_group_hom namespace ring_hom /-! These instances look redundant, because `deprecated.ring` provides `is_ring_hom` for a `→+*`. Nevertheless these are harmless, and helpful for stripping out dependencies on `deprecated.ring`. -/ variables {R : Type*} {S : Type*} section variables [non_assoc_semiring R] [non_assoc_semiring S] instance (f : R →+* S) : is_monoid_hom f := { map_one := f.map_one, map_mul := f.map_mul } instance (f : R →+* S) : is_add_monoid_hom f := { map_zero := f.map_zero, map_add := f.map_add } end section variables [ring R] [ring S] instance (f : R →+* S) : is_add_group_hom f := { map_add := f.map_add } end end ring_hom /-- Inversion is a group homomorphism if the group is commutative. -/ @[instance, to_additive neg.is_add_group_hom "Negation is an `add_group` homomorphism if the `add_group` is commutative."] lemma inv.is_group_hom [comm_group α] : is_group_hom (has_inv.inv : α → α) := { map_mul := mul_inv } namespace is_add_group_hom variables [add_group α] [add_group β] (f : α → β) [is_add_group_hom f] /-- Additive group homomorphisms commute with subtraction. -/ lemma map_sub (a b) : f (a - b) = f a - f b := calc f (a - b) = f (a + -b) : congr_arg f (sub_eq_add_neg a b) ... = f a + f (-b) : is_add_hom.map_add f _ _ ... = f a + -f b : by rw [map_neg f] ... = f a - f b : (sub_eq_add_neg _ _).symm end is_add_group_hom /-- The difference of two additive group homomorphisms is an additive group homomorphism if the target is commutative. -/ @[instance] lemma is_add_group_hom.sub {α β} [add_group α] [add_comm_group β] (f g : α → β) [is_add_group_hom f] [is_add_group_hom g] : is_add_group_hom (λa, f a - g a) := by { simp only [sub_eq_add_neg], exact is_add_group_hom.add f (λa, - g a) } namespace units variables {M : Type*} {N : Type*} [monoid M] [monoid N] /-- The group homomorphism on units induced by a multiplicative morphism. -/ @[reducible] def map' (f : M → N) [is_monoid_hom f] : units M →* units N := map (monoid_hom.of f) @[simp] lemma coe_map' (f : M → N) [is_monoid_hom f] (x : units M) : ↑((map' f : units M → units N) x) = f x := rfl instance coe_is_monoid_hom : is_monoid_hom (coe : units M → M) := (coe_hom M).is_monoid_hom end units namespace is_unit variables {M : Type*} {N : Type*} [monoid M] [monoid N] {x : M} lemma map' (f : M → N) {x : M} (h : is_unit x) [is_monoid_hom f] : is_unit (f x) := h.map (monoid_hom.of f) end is_unit lemma additive.is_add_hom [has_mul α] [has_mul β] (f : α → β) [is_mul_hom f] : @is_add_hom (additive α) (additive β) _ _ f := { map_add := @is_mul_hom.map_mul α β _ _ f _ } lemma multiplicative.is_mul_hom [has_add α] [has_add β] (f : α → β) [is_add_hom f] : @is_mul_hom (multiplicative α) (multiplicative β) _ _ f := { map_mul := @is_add_hom.map_add α β _ _ f _ } lemma additive.is_add_monoid_hom [mul_one_class α] [mul_one_class β] (f : α → β) [is_monoid_hom f] : @is_add_monoid_hom (additive α) (additive β) _ _ f := { map_zero := @is_monoid_hom.map_one α β _ _ f _, ..additive.is_add_hom f } lemma multiplicative.is_monoid_hom [add_zero_class α] [add_zero_class β] (f : α → β) [is_add_monoid_hom f] : @is_monoid_hom (multiplicative α) (multiplicative β) _ _ f := { map_one := @is_add_monoid_hom.map_zero α β _ _ f _, ..multiplicative.is_mul_hom f } lemma additive.is_add_group_hom [group α] [group β] (f : α → β) [is_group_hom f] : @is_add_group_hom (additive α) (additive β) _ _ f := { map_add := @is_mul_hom.map_mul α β _ _ f _ } lemma multiplicative.is_group_hom [add_group α] [add_group β] (f : α → β) [is_add_group_hom f] : @is_group_hom (multiplicative α) (multiplicative β) _ _ f := { map_mul := @is_add_hom.map_add α β _ _ f _ }
8569386b37150a758b3ce2e938d4bcfb480abcb7
b70031c8e2c5337b91d7e70f1e0c5f528f7b0e77
/src/order/rel_iso.lean
7e41a4dfa95bdc4b8104a0178833299982aac105
[ "Apache-2.0" ]
permissive
molodiuc/mathlib
cae2ba3ef1601c1f42ca0b625c79b061b63fef5b
98ebe5a6739fbe254f9ee9d401882d4388f91035
refs/heads/master
1,674,237,127,059
1,606,353,533,000
1,606,353,533,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
24,917
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 logic.embedding import order.rel_classes import data.fin open function universes u v w variables {α : Type*} {β : Type*} {γ : Type*} {r : α → α → Prop} {s : β → β → Prop} {t : γ → γ → Prop} /-- A relation homomorphism with respect to a given pair of relations `r` and `s` is a function `f : α → β` such that `r a b → s (f a) (f b)`. -/ @[nolint has_inhabited_instance] structure rel_hom {α β : Type*} (r : α → α → Prop) (s : β → β → Prop) := (to_fun : α → β) (map_rel' : ∀ {a b}, r a b → s (to_fun a) (to_fun b)) infix ` →r `:25 := rel_hom namespace rel_hom instance : has_coe_to_fun (r →r s) := ⟨λ _, α → β, λ o, o.to_fun⟩ theorem map_rel (f : r →r s) : ∀ {a b}, r a b → s (f a) (f b) := f.map_rel' @[simp] theorem coe_fn_mk (f : α → β) (o) : (@rel_hom.mk _ _ r s f o : α → β) = f := rfl @[simp] theorem coe_fn_to_fun (f : r →r s) : (f.to_fun : α → β) = f := rfl /-- The map `coe_fn : (r →r s) → (α → β)` is injective. We can't use `function.injective` here but mimic its signature by using `⦃e₁ e₂⦄`. -/ theorem coe_fn_inj : ∀ ⦃e₁ e₂ : r →r s⦄, (e₁ : α → β) = e₂ → e₁ = e₂ | ⟨f₁, o₁⟩ ⟨f₂, o₂⟩ h := by { congr, exact h } @[ext] theorem ext ⦃f g : r →r s⦄ (h : ∀ x, f x = g x) : f = g := coe_fn_inj (funext h) theorem ext_iff {f g : r →r s} : f = g ↔ ∀ x, f x = g x := ⟨λ h x, h ▸ rfl, λ h, ext h⟩ /-- Identity map is a relation homomorphism. -/ @[refl] protected def id (r : α → α → Prop) : r →r r := ⟨id, λ a b, id⟩ /-- Composition of two relation homomorphisms is a relation homomorphism. -/ @[trans] protected def comp (g : s →r t) (f : r →r s) : r →r t := ⟨g.1 ∘ f.1, λ a b h, g.2 (f.2 h)⟩ @[simp] theorem id_apply (x : α) : rel_hom.id r x = x := rfl @[simp] theorem comp_apply (g : s →r t) (f : r →r s) (a : α) : (g.comp f) a = g (f a) := rfl /-- A relation homomorphism is also a relation homomorphism between dual relations. -/ protected def swap (f : r →r s) : swap r →r swap s := ⟨f, λ a b, f.map_rel⟩ /-- A function is a relation homomorphism from the preimage relation of `s` to `s`. -/ def preimage (f : α → β) (s : β → β → Prop) : f ⁻¹'o s →r s := ⟨f, λ a b, id⟩ protected theorem is_irrefl : ∀ (f : r →r s) [is_irrefl β s], is_irrefl α r | ⟨f, o⟩ ⟨H⟩ := ⟨λ a h, H _ (o h)⟩ protected theorem is_asymm : ∀ (f : r →r s) [is_asymm β s], is_asymm α r | ⟨f, o⟩ ⟨H⟩ := ⟨λ a b h₁ h₂, H _ _ (o h₁) (o h₂)⟩ protected theorem acc (f : r →r s) (a : α) : acc s (f a) → acc r a := begin generalize h : f a = b, intro ac, induction ac with _ H IH generalizing a, subst h, exact ⟨_, λ a' h, IH (f a') (f.map_rel h) _ rfl⟩ end protected theorem well_founded : ∀ (f : r →r s) (h : well_founded s), well_founded r | f ⟨H⟩ := ⟨λ a, f.acc _ (H _)⟩ end rel_hom /-- An increasing function is injective -/ lemma injective_of_increasing (r : α → α → Prop) (s : β → β → Prop) [is_trichotomous α r] [is_irrefl β s] (f : α → β) (hf : ∀{x y}, r x y → s (f x) (f y)) : injective f := begin intros x y hxy, rcases trichotomous_of r x y with h | h | h, have := hf h, rw hxy at this, exfalso, exact irrefl_of s (f y) this, exact h, have := hf h, rw hxy at this, exfalso, exact irrefl_of s (f y) this end /-- An increasing function is injective -/ lemma rel_hom.injective_of_increasing [is_trichotomous α r] [is_irrefl β s] (f : r →r s) : injective f := injective_of_increasing r s f (λ x y, f.map_rel) theorem surjective.well_founded_iff {f : α → β} (hf : surjective f) (o : ∀ {a b}, r a b ↔ s (f a) (f b)) : well_founded r ↔ well_founded s := iff.intro (begin apply rel_hom.well_founded, refine rel_hom.mk _ _, {exact classical.some hf.has_right_inverse}, intros a b h, apply o.2, convert h, iterate 2 { apply classical.some_spec hf.has_right_inverse }, end) (rel_hom.well_founded ⟨f, λ _ _, o.1⟩) /-- A relation embedding with respect to a given pair of relations `r` and `s` is an embedding `f : α ↪ β` such that `r a b ↔ s (f a) (f b)`. -/ structure rel_embedding {α β : Type*} (r : α → α → Prop) (s : β → β → Prop) extends α ↪ β := (map_rel_iff' : ∀ {a b}, r a b ↔ s (to_embedding a) (to_embedding b)) infix ` ↪r `:25 := rel_embedding /-- An order embedding is an embedding `f : α ↪ β` such that `a ≤ b ↔ (f a) ≤ (f b)`. This definition is an abbreviation of `rel_embedding (≤) (≤)`. -/ abbreviation order_embedding (α β : Type*) [has_le α] [has_le β] := @rel_embedding α β (≤) (≤) infix ` ↪o `:25 := order_embedding /-- The induced relation on a subtype is an embedding under the natural inclusion. -/ definition subtype.rel_embedding {X : Type*} (r : X → X → Prop) (p : X → Prop) : ((subtype.val : subtype p → X) ⁻¹'o r) ↪r r := ⟨⟨subtype.val,subtype.val_injective⟩,by intros;refl⟩ theorem preimage_equivalence {α β} (f : α → β) {s : β → β → Prop} (hs : equivalence s) : equivalence (f ⁻¹'o s) := ⟨λ a, hs.1 _, λ a b h, hs.2.1 h, λ a b c h₁ h₂, hs.2.2 h₁ h₂⟩ namespace rel_embedding /-- A relation embedding is also a relation homomorphism -/ def to_rel_hom (f : r ↪r s) : (r →r s) := { to_fun := f.to_embedding.to_fun, map_rel' := λ x y, (map_rel_iff' f).mp } instance : has_coe (r ↪r s) (r →r s) := ⟨to_rel_hom⟩ -- see Note [function coercion] instance : has_coe_to_fun (r ↪r s) := ⟨λ _, α → β, λ o, o.to_embedding⟩ @[simp] lemma to_rel_hom_eq_coe (f : r ↪r s) : f.to_rel_hom = f := rfl @[simp] lemma coe_coe_fn (f : r ↪r s) : ((f : r →r s) : α → β) = f := rfl theorem injective (f : r ↪r s) : injective f := f.inj' theorem map_rel_iff (f : r ↪r s) : ∀ {a b}, r a b ↔ s (f a) (f b) := f.map_rel_iff' @[simp] theorem coe_fn_mk (f : α ↪ β) (o) : (@rel_embedding.mk _ _ r s f o : α → β) = f := rfl @[simp] theorem coe_fn_to_embedding (f : r ↪r s) : (f.to_embedding : α → β) = f := rfl /-- The map `coe_fn : (r ↪r s) → (α → β)` is injective. We can't use `function.injective` here but mimic its signature by using `⦃e₁ e₂⦄`. -/ theorem coe_fn_inj : ∀ ⦃e₁ e₂ : r ↪r s⦄, (e₁ : α → β) = e₂ → e₁ = e₂ | ⟨⟨f₁, h₁⟩, o₁⟩ ⟨⟨f₂, h₂⟩, o₂⟩ h := by { congr, exact h } @[ext] theorem ext ⦃f g : r ↪r s⦄ (h : ∀ x, f x = g x) : f = g := coe_fn_inj (funext h) theorem ext_iff {f g : r ↪r s} : f = g ↔ ∀ x, f x = g x := ⟨λ h x, h ▸ rfl, λ h, ext h⟩ /-- Identity map is a relation embedding. -/ @[refl] protected def refl (r : α → α → Prop) : r ↪r r := ⟨embedding.refl _, λ a b, iff.rfl⟩ /-- Composition of two relation embeddings is a relation embedding. -/ @[trans] protected def trans (f : r ↪r s) (g : s ↪r t) : r ↪r t := ⟨f.1.trans g.1, λ a b, by rw [f.2, g.2]; simp⟩ instance (r : α → α → Prop) : inhabited (r ↪r r) := ⟨rel_embedding.refl _⟩ @[simp] theorem refl_apply (x : α) : rel_embedding.refl r x = x := rfl @[simp] theorem trans_apply (f : r ↪r s) (g : s ↪r t) (a : α) : (f.trans g) a = g (f a) := rfl /-- A relation embedding is also a relation embedding between dual relations. -/ protected def swap (f : r ↪r s) : swap r ↪r swap s := ⟨f.to_embedding, λ a b, f.map_rel_iff⟩ /-- If `f` is injective, then it is a relation embedding from the preimage relation of `s` to `s`. -/ def preimage (f : α ↪ β) (s : β → β → Prop) : f ⁻¹'o s ↪r s := ⟨f, λ a b, iff.rfl⟩ theorem eq_preimage (f : r ↪r s) : r = f ⁻¹'o s := by { ext a b, exact f.map_rel_iff } protected theorem is_irrefl : ∀ (f : r ↪r s) [is_irrefl β s], is_irrefl α r | ⟨f, o⟩ ⟨H⟩ := ⟨λ a h, H _ (o.1 h)⟩ protected theorem is_refl : ∀ (f : r ↪r s) [is_refl β s], is_refl α r | ⟨f, o⟩ ⟨H⟩ := ⟨λ a, o.2 (H _)⟩ protected theorem is_symm : ∀ (f : r ↪r s) [is_symm β s], is_symm α r | ⟨f, o⟩ ⟨H⟩ := ⟨λ a b h, o.2 (H _ _ (o.1 h))⟩ protected theorem is_asymm : ∀ (f : r ↪r s) [is_asymm β s], is_asymm α r | ⟨f, o⟩ ⟨H⟩ := ⟨λ a b h₁ h₂, H _ _ (o.1 h₁) (o.1 h₂)⟩ protected theorem is_antisymm : ∀ (f : r ↪r s) [is_antisymm β s], is_antisymm α r | ⟨f, o⟩ ⟨H⟩ := ⟨λ a b h₁ h₂, f.inj' (H _ _ (o.1 h₁) (o.1 h₂))⟩ protected theorem is_trans : ∀ (f : r ↪r s) [is_trans β s], is_trans α r | ⟨f, o⟩ ⟨H⟩ := ⟨λ a b c h₁ h₂, o.2 (H _ _ _ (o.1 h₁) (o.1 h₂))⟩ protected theorem is_total : ∀ (f : r ↪r s) [is_total β s], is_total α r | ⟨f, o⟩ ⟨H⟩ := ⟨λ a b, (or_congr o o).2 (H _ _)⟩ protected theorem is_preorder : ∀ (f : r ↪r s) [is_preorder β s], is_preorder α r | f H := by exactI {..f.is_refl, ..f.is_trans} protected theorem is_partial_order : ∀ (f : r ↪r s) [is_partial_order β s], is_partial_order α r | f H := by exactI {..f.is_preorder, ..f.is_antisymm} protected theorem is_linear_order : ∀ (f : r ↪r s) [is_linear_order β s], is_linear_order α r | f H := by exactI {..f.is_partial_order, ..f.is_total} protected theorem is_strict_order : ∀ (f : r ↪r s) [is_strict_order β s], is_strict_order α r | f H := by exactI {..f.is_irrefl, ..f.is_trans} protected theorem is_trichotomous : ∀ (f : r ↪r s) [is_trichotomous β s], is_trichotomous α r | ⟨f, o⟩ ⟨H⟩ := ⟨λ a b, (or_congr o (or_congr f.inj'.eq_iff.symm o)).2 (H _ _)⟩ protected theorem is_strict_total_order' : ∀ (f : r ↪r s) [is_strict_total_order' β s], is_strict_total_order' α r | f H := by exactI {..f.is_trichotomous, ..f.is_strict_order} protected theorem acc (f : r ↪r s) (a : α) : acc s (f a) → acc r a := begin generalize h : f a = b, intro ac, induction ac with _ H IH generalizing a, subst h, exact ⟨_, λ a' h, IH (f a') (f.map_rel_iff.1 h) _ rfl⟩ end protected theorem well_founded : ∀ (f : r ↪r s) (h : well_founded s), well_founded r | f ⟨H⟩ := ⟨λ a, f.acc _ (H _)⟩ protected theorem is_well_order : ∀ (f : r ↪r s) [is_well_order β s], is_well_order α r | f H := by exactI {wf := f.well_founded H.wf, ..f.is_strict_total_order'} /-- It suffices to prove `f` is monotone between strict relations to show it is a relation embedding. -/ def of_monotone [is_trichotomous α r] [is_asymm β s] (f : α → β) (H : ∀ a b, r a b → s (f a) (f b)) : r ↪r s := begin haveI := @is_asymm.is_irrefl β s _, refine ⟨⟨f, λ a b e, _⟩, λ a b, ⟨H _ _, λ h, _⟩⟩, { refine ((@trichotomous _ r _ a b).resolve_left _).resolve_right _; exact λ h, @irrefl _ s _ _ (by simpa [e] using H _ _ h) }, { refine (@trichotomous _ r _ a b).resolve_right (or.rec (λ e, _) (λ h', _)), { subst e, exact irrefl _ h }, { exact asymm (H _ _ h') h } } end @[simp] theorem of_monotone_coe [is_trichotomous α r] [is_asymm β s] (f : α → β) (H) : (@of_monotone _ _ r s _ _ f H : α → β) = f := rfl /-- Embeddings of partial orders that preserve `<` also preserve `≤` -/ def order_embedding_of_lt_embedding [partial_order α] [partial_order β] (f : ((<) : α → α → Prop) ↪r ((<) : β → β → Prop)) : α ↪o β := { map_rel_iff' := by { intros, simp [le_iff_lt_or_eq,f.map_rel_iff, f.injective] }, .. f } end rel_embedding namespace order_embedding variables [preorder α] [preorder β] (f : α ↪o β) /-- lt is preserved by order embeddings of preorders -/ def lt_embedding : ((<) : α → α → Prop) ↪r ((<) : β → β → Prop) := { map_rel_iff' := by intros; simp [lt_iff_le_not_le,f.map_rel_iff], .. f } @[simp] lemma lt_embedding_apply (x : α) : f.lt_embedding x = f x := rfl theorem map_le_iff : ∀ {a b}, a ≤ b ↔ (f a) ≤ (f b) := f.map_rel_iff' theorem map_lt_iff : ∀ {a b}, a < b ↔ (f a) < (f b) := f.lt_embedding.map_rel_iff' protected theorem monotone : monotone f := λ x y, f.map_le_iff.1 protected theorem strict_mono : strict_mono f := λ x y, f.map_lt_iff.1 protected theorem acc (a : α) : acc (<) (f a) → acc (<) a := f.lt_embedding.acc a protected theorem well_founded : well_founded ((<) : β → β → Prop) → well_founded ((<) : α → α → Prop) := f.lt_embedding.well_founded protected theorem is_well_order [is_well_order β (<)] : is_well_order α (<) := f.lt_embedding.is_well_order /-- An order embedding is also an order embedding between dual orders. -/ protected def dual : order_dual α ↪o order_dual β := ⟨f.to_embedding, λ a b, f.map_rel_iff⟩ /-- A sctrictly monotone map from a linear order is an order embedding. --/ def of_strict_mono {α β} [linear_order α] [preorder β] (f : α → β) (h : strict_mono f) : α ↪o β := { to_fun := f, inj' := strict_mono.injective h, map_rel_iff' := begin intros, rcases lt_trichotomy a b with lt | eq | gt, { exact iff_of_true (le_of_lt lt) (le_of_lt (h lt)) }, { rw eq, apply iff_of_true, refl, refl }, { apply iff_of_false (not_le_of_gt gt) (not_le_of_gt (h gt)) } end } end order_embedding /-- The inclusion map `fin n → ℕ` is a relation embedding. -/ def fin.val.rel_embedding (n) : (fin n) ↪o ℕ := ⟨⟨coe, @fin.eq_of_veq _⟩, λ a b, iff.rfl⟩ /-- The inclusion map `fin m → fin n` is an order embedding. -/ def fin_fin.rel_embedding {m n} (h : m ≤ n) : (fin m) ↪o (fin n) := ⟨⟨λ ⟨x, h'⟩, ⟨x, lt_of_lt_of_le h' h⟩, λ ⟨a, _⟩ ⟨b, _⟩ h, by congr; injection h⟩, by intros; cases a; cases b; refl⟩ /-- The ordering on `fin n` is a well order. -/ instance fin.lt.is_well_order (n) : is_well_order (fin n) (<) := (fin.val.rel_embedding n).is_well_order /-- A relation isomorphism is an equivalence that is also a relation embedding. -/ structure rel_iso {α β : Type*} (r : α → α → Prop) (s : β → β → Prop) extends α ≃ β := (map_rel_iff' : ∀ {a b}, r a b ↔ s (to_equiv a) (to_equiv b)) infix ` ≃r `:25 := rel_iso /-- An order isomorphism is an equivalence such that `a ≤ b ↔ (f a) ≤ (f b)`. This definition is an abbreviation of `rel_iso (≤) (≤)`. -/ abbreviation order_iso (α β : Type*) [has_le α] [has_le β] := @rel_iso α β (≤) (≤) infix ` ≃o `:25 := order_iso namespace rel_iso /-- Convert an `rel_iso` to an `rel_embedding`. This function is also available as a coercion but often it is easier to write `f.to_rel_embedding` than to write explicitly `r` and `s` in the target type. -/ def to_rel_embedding (f : r ≃r s) : r ↪r s := ⟨f.to_equiv.to_embedding, f.map_rel_iff'⟩ instance : has_coe (r ≃r s) (r ↪r s) := ⟨to_rel_embedding⟩ -- see Note [function coercion] instance : has_coe_to_fun (r ≃r s) := ⟨λ _, α → β, λ f, f⟩ @[simp] lemma to_rel_embedding_eq_coe (f : r ≃r s) : f.to_rel_embedding = f := rfl @[simp] lemma coe_coe_fn (f : r ≃r s) : ((f : r ↪r s) : α → β) = f := rfl theorem map_rel_iff (f : r ≃r s) : ∀ {a b}, r a b ↔ s (f a) (f b) := f.map_rel_iff' lemma map_rel_iff'' {r : α → α → Prop} {s : β → β → Prop} (f : r ≃r s) {x y : α} : r x y ↔ s ((↑f : r ↪r s) x) ((↑f : r ↪r s) y) := f.map_rel_iff @[simp] theorem coe_fn_mk (f : α ≃ β) (o : ∀ ⦃a b⦄, r a b ↔ s (f a) (f b)) : (rel_iso.mk f o : α → β) = f := rfl @[simp] theorem coe_fn_to_equiv (f : r ≃r s) : (f.to_equiv : α → β) = f := rfl theorem injective_to_equiv : injective (to_equiv : (r ≃r s) → α ≃ β) | ⟨e₁, o₁⟩ ⟨e₂, o₂⟩ h := by { congr, exact h } /-- The map `coe_fn : (r ≃r s) → (α → β)` is injective. Lean fails to parse `function.injective (λ e : r ≃r s, (e : α → β))`, so we use a trick to say the same. -/ theorem injective_coe_fn : function.injective (λ (e : r ≃r s) (x : α), e x) := equiv.injective_coe_fn.comp injective_to_equiv @[ext] theorem ext ⦃f g : r ≃r s⦄ (h : ∀ x, f x = g x) : f = g := injective_coe_fn (funext h) theorem ext_iff {f g : r ≃r s} : f = g ↔ ∀ x, f x = g x := ⟨λ h x, h ▸ rfl, λ h, ext h⟩ /-- Identity map is a relation isomorphism. -/ @[refl] protected def refl (r : α → α → Prop) : r ≃r r := ⟨equiv.refl _, λ a b, iff.rfl⟩ /-- Inverse map of a relation isomorphism is a relation isomorphism. -/ @[symm] protected def symm (f : r ≃r s) : s ≃r r := ⟨f.to_equiv.symm, λ a b, by cases f with f o; rw o; simp⟩ /-- Composition of two relation isomorphisms is a relation isomorphism. -/ @[trans] protected def trans (f₁ : r ≃r s) (f₂ : s ≃r t) : r ≃r t := ⟨f₁.to_equiv.trans f₂.to_equiv, λ a b, f₁.map_rel_iff.trans f₂.map_rel_iff⟩ instance (r : α → α → Prop) : inhabited (r ≃r r) := ⟨rel_iso.refl _⟩ /-- a relation isomorphism is also a relation isomorphism between dual relations. -/ protected def swap (f : r ≃r s) : (swap r) ≃r (swap s) := ⟨f.to_equiv, λ _ _, f.map_rel_iff⟩ @[simp] theorem coe_fn_symm_mk (f o) : ((@rel_iso.mk _ _ r s f o).symm : β → α) = f.symm := rfl @[simp] theorem refl_apply (x : α) : rel_iso.refl r x = x := rfl @[simp] theorem trans_apply (f : r ≃r s) (g : s ≃r t) (a : α) : (f.trans g) a = g (f a) := rfl @[simp] theorem apply_symm_apply (e : r ≃r s) (x : β) : e (e.symm x) = x := e.to_equiv.apply_symm_apply x @[simp] theorem symm_apply_apply (e : r ≃r s) (x : α) : e.symm (e x) = x := e.to_equiv.symm_apply_apply x theorem rel_symm_apply (e : r ≃r s) {x y} : r x (e.symm y) ↔ s (e x) y := by rw [e.map_rel_iff, e.apply_symm_apply] theorem symm_apply_rel (e : r ≃r s) {x y} : r (e.symm x) y ↔ s x (e y) := by rw [e.map_rel_iff, e.apply_symm_apply] protected lemma bijective (e : r ≃r s) : bijective e := e.to_equiv.bijective protected lemma injective (e : r ≃r s) : injective e := e.to_equiv.injective protected lemma surjective (e : r ≃r s) : surjective e := e.to_equiv.surjective /-- Any equivalence lifts to a relation isomorphism between `s` and its preimage. -/ protected def preimage (f : α ≃ β) (s : β → β → Prop) : f ⁻¹'o s ≃r s := ⟨f, λ a b, iff.rfl⟩ /-- A surjective relation embedding is a relation isomorphism. -/ noncomputable def of_surjective (f : r ↪r s) (H : surjective f) : r ≃r s := ⟨equiv.of_bijective f ⟨f.injective, H⟩, by simp [f.map_rel_iff']⟩ @[simp] theorem of_surjective_coe (f : r ↪r s) (H) : (of_surjective f H : α → β) = f := rfl /-- Given relation isomorphisms `r₁ ≃r r₂` and `s₁ ≃r s₂`, construct a relation isomorphism for the lexicographic orders on the sum. -/ def sum_lex_congr {α₁ α₂ β₁ β₂ r₁ r₂ s₁ s₂} (e₁ : @rel_iso α₁ α₂ r₁ r₂) (e₂ : @rel_iso β₁ β₂ s₁ s₂) : sum.lex r₁ s₁ ≃r sum.lex r₂ s₂ := ⟨equiv.sum_congr e₁.to_equiv e₂.to_equiv, λ a b, by cases e₁ with f hf; cases e₂ with g hg; cases a; cases b; simp [hf, hg]⟩ /-- Given relation isomorphisms `r₁ ≃r r₂` and `s₁ ≃r s₂`, construct a relation isomorphism for the lexicographic orders on the product. -/ def prod_lex_congr {α₁ α₂ β₁ β₂ r₁ r₂ s₁ s₂} (e₁ : @rel_iso α₁ α₂ r₁ r₂) (e₂ : @rel_iso β₁ β₂ s₁ s₂) : prod.lex r₁ s₁ ≃r prod.lex r₂ s₂ := ⟨equiv.prod_congr e₁.to_equiv e₂.to_equiv, λ a b, begin cases e₁ with f hf; cases e₂ with g hg, cases a with a₁ a₂; cases b with b₁ b₂, suffices : prod.lex r₁ s₁ (a₁, a₂) (b₁, b₂) ↔ prod.lex r₂ s₂ (f a₁, g a₂) (f b₁, g b₂), {simpa [hf, hg]}, split, { intro h, cases h with _ _ _ _ h _ _ _ h, { left, exact hf.1 h }, { right, exact hg.1 h } }, { generalize e : f b₁ = fb₁, intro h, cases h with _ _ _ _ h _ _ _ h, { subst e, left, exact hf.2 h }, { have := f.injective e, subst b₁, right, exact hg.2 h } } end⟩ instance : group (r ≃r r) := { one := rel_iso.refl r, mul := λ f₁ f₂, f₂.trans f₁, inv := rel_iso.symm, mul_assoc := λ f₁ f₂ f₃, rfl, one_mul := λ f, ext $ λ _, rfl, mul_one := λ f, ext $ λ _, rfl, mul_left_inv := λ f, ext f.symm_apply_apply } @[simp] lemma coe_one : ⇑(1 : r ≃r r) = id := rfl @[simp] lemma coe_mul (e₁ e₂ : r ≃r r) : ⇑(e₁ * e₂) = e₁ ∘ e₂ := rfl lemma mul_apply (e₁ e₂ : r ≃r r) (x : α) : (e₁ * e₂) x = e₁ (e₂ x) := rfl @[simp] lemma inv_apply_self (e : r ≃r r) (x) : e⁻¹ (e x) = x := e.symm_apply_apply x @[simp] lemma apply_inv_self (e : r ≃r r) (x) : e (e⁻¹ x) = x := e.apply_symm_apply x end rel_iso namespace order_iso /-- Reinterpret an order isomorphism as an order embedding. -/ def to_order_embedding [has_le α] [has_le β] (e : α ≃o β) : α ↪o β := e.to_rel_embedding @[simp] lemma coe_to_order_embedding [has_le α] [has_le β] (e : α ≃o β) : ⇑(e.to_order_embedding) = e := rfl variables [preorder α] [preorder β] protected lemma monotone (e : α ≃o β) : monotone e := e.to_order_embedding.monotone protected lemma strict_mono (e : α ≃o β) : strict_mono e := e.to_order_embedding.strict_mono /-- To show that `f : α → β`, `g : β → α` make up an order isomorphism of linear orders, it suffices to prove `cmp a (g b) = cmp (f a) b`. --/ def of_cmp_eq_cmp {α β} [linear_order α] [linear_order β] (f : α → β) (g : β → α) (h : ∀ (a : α) (b : β), cmp a (g b) = cmp (f a) b) : α ≃o β := have gf : ∀ (a : α), a = g (f a) := by { intro, rw [←cmp_eq_eq_iff, h, cmp_self_eq_eq] }, { to_fun := f, inv_fun := g, left_inv := λ a, (gf a).symm, right_inv := by { intro, rw [←cmp_eq_eq_iff, ←h, cmp_self_eq_eq] }, map_rel_iff' := by { intros, apply le_iff_le_of_cmp_eq_cmp, convert h _ _, apply gf } } end order_iso /-- A subset `p : set α` embeds into `α` -/ def set_coe_embedding {α : Type*} (p : set α) : p ↪ α := ⟨subtype.val, @subtype.eq _ _⟩ /-- `subrel r p` is the inherited relation on a subset. -/ def subrel (r : α → α → Prop) (p : set α) : p → p → Prop := @subtype.val _ p ⁻¹'o r @[simp] theorem subrel_val (r : α → α → Prop) (p : set α) {a b} : subrel r p a b ↔ r a.1 b.1 := iff.rfl namespace subrel /-- The relation embedding from the inherited relation on a subset. -/ protected def rel_embedding (r : α → α → Prop) (p : set α) : subrel r p ↪r r := ⟨set_coe_embedding _, λ a b, iff.rfl⟩ @[simp] theorem rel_embedding_apply (r : α → α → Prop) (p a) : subrel.rel_embedding r p a = a.1 := rfl instance (r : α → α → Prop) [is_well_order α r] (p : set α) : is_well_order p (subrel r p) := rel_embedding.is_well_order (subrel.rel_embedding r p) end subrel /-- Restrict the codomain of a relation embedding. -/ def rel_embedding.cod_restrict (p : set β) (f : r ↪r s) (H : ∀ a, f a ∈ p) : r ↪r subrel s p := ⟨f.to_embedding.cod_restrict p H, f.map_rel_iff'⟩ @[simp] theorem rel_embedding.cod_restrict_apply (p) (f : r ↪r s) (H a) : rel_embedding.cod_restrict p f H a = ⟨f a, H a⟩ := rfl /-- An order isomorphism is also an order isomorphism between dual orders. -/ protected def order_iso.dual [preorder α] [preorder β] (f : α ≃o β) : order_dual α ≃o order_dual β := ⟨f.to_equiv, λ _ _, f.map_rel_iff⟩ section lattice_isos lemma order_iso.map_bot [order_bot α] [order_bot β] (f : α ≃o β) : f ⊥ = ⊥ := by { rw [eq_bot_iff, ← f.apply_symm_apply ⊥, ← f.map_rel_iff], apply bot_le, } lemma rel_iso.map_top [order_top α] [order_top β] (f : α ≃o β) : f ⊤ = ⊤ := by { rw [eq_top_iff, ← f.apply_symm_apply ⊤, ← f.map_rel_iff], apply le_top, } variables {a₁ a₂ : α} lemma rel_embedding.map_inf_le [semilattice_inf α] [semilattice_inf β] (f : α ↪o β) : f (a₁ ⊓ a₂) ≤ f a₁ ⊓ f a₂ := by simp [← f.map_rel_iff] lemma rel_iso.map_inf [semilattice_inf α] [semilattice_inf β] (f : α ≃o β) : f (a₁ ⊓ a₂) = f a₁ ⊓ f a₂ := begin apply le_antisymm, { apply f.to_rel_embedding.map_inf_le }, rw [f.symm.map_rel_iff, rel_iso.symm_apply_apply], convert f.symm.to_rel_embedding.map_inf_le; simp, end lemma rel_embedding.le_map_sup [semilattice_sup α] [semilattice_sup β] (f : α ↪o β) : f a₁ ⊔ f a₂ ≤ f (a₁ ⊔ a₂) := by simp [← f.map_rel_iff] lemma rel_iso.map_sup [semilattice_sup α] [semilattice_sup β] (f : α ≃o β) : f (a₁ ⊔ a₂) = f a₁ ⊔ f a₂ := begin apply le_antisymm, swap, { apply f.to_rel_embedding.le_map_sup }, rw [f.symm.map_rel_iff, rel_iso.symm_apply_apply], convert f.symm.to_rel_embedding.le_map_sup; simp, end end lattice_isos
4e0c5c49d6a1503f9817a956e581ec50f0a2c4b2
90bd8c2a52dbaaba588bdab57b155a7ec1532de0
/src/homotopy/tactic.lean
348688e35786c7c140e9d587de745b92edeabf5c
[ "Apache-2.0" ]
permissive
shingtaklam1324/alg-top
fd434f1478925a232af45f18f370ee3a22811c54
4c88e28df6f0a329f26eab32bae023789193990e
refs/heads/master
1,689,447,024,947
1,630,073,400,000
1,630,073,400,000
381,528,689
2
0
null
null
null
null
UTF-8
Lean
false
false
794
lean
import homotopy.path /-! # Tactics In this file we define some basic tactics which make constructing `path_homotopy`s easier. -/ namespace path_homotopy.tactic /-- Associate the left hand side of a homotopy to the left -/ meta def assocl : tactic unit := `[refine homotopy_with.trans path_homotopy.assoc _] /-- Associate the left hand side of a homotopy to the right -/ meta def assocr : tactic unit := `[refine homotopy_with.trans path_homotopy.assoc.symm _] /-- Associate the right hand side of a homotopy to the left -/ meta def assocl' : tactic unit := `[refine homotopy_with.trans _ path_homotopy.assoc] /-- Associate the right hand side of a homotopy to the right -/ meta def assocr' : tactic unit := `[refine homotopy_with.trans _ path_homotopy.assoc.symm] end path_homotopy.tactic
cd4006fb2bf5e24968b175fbf0c3ef405fc4bab3
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/group_theory/coset.lean
51c2e0bbc248c0cd7e337412704c38fa9b7e38aa
[ "Apache-2.0" ]
permissive
gebner/mathlib
eab0150cc4f79ec45d2016a8c21750244a2e7ff0
cc6a6edc397c55118df62831e23bfbd6e6c6b4ab
refs/heads/master
1,625,574,853,976
1,586,712,827,000
1,586,712,827,000
99,101,412
1
0
Apache-2.0
1,586,716,389,000
1,501,667,958,000
Lean
UTF-8
Lean
false
false
9,169
lean
/- Copyright (c) 2018 Mitchell Rowett. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mitchell Rowett, Scott Morrison -/ import group_theory.subgroup data.equiv.basic data.quot open set function variable {α : Type*} @[to_additive left_add_coset] def left_coset [has_mul α] (a : α) (s : set α) : set α := (λ x, a * x) '' s @[to_additive right_add_coset] def right_coset [has_mul α] (s : set α) (a : α) : set α := (λ x, x * a) '' s localized "infix ` *l `:70 := left_coset" in coset localized "infix ` +l `:70 := left_add_coset" in coset localized "infix ` *r `:70 := right_coset" in coset localized "infix ` +r `:70 := right_add_coset" in coset section coset_mul variable [has_mul α] @[to_additive mem_left_add_coset] lemma mem_left_coset {s : set α} {x : α} (a : α) (hxS : x ∈ s) : a * x ∈ a *l s := mem_image_of_mem (λ b : α, a * b) hxS @[to_additive mem_right_add_coset] lemma mem_right_coset {s : set α} {x : α} (a : α) (hxS : x ∈ s) : x * a ∈ s *r a := mem_image_of_mem (λ b : α, b * a) hxS @[to_additive left_add_coset_equiv] def left_coset_equiv (s : set α) (a b : α) := a *l s = b *l s @[to_additive left_add_coset_equiv_rel] lemma left_coset_equiv_rel (s : set α) : equivalence (left_coset_equiv s) := mk_equivalence (left_coset_equiv s) (λ a, rfl) (λ a b, eq.symm) (λ a b c, eq.trans) end coset_mul section coset_semigroup variable [semigroup α] @[simp] lemma left_coset_assoc (s : set α) (a b : α) : a *l (b *l s) = (a * b) *l s := by simp [left_coset, right_coset, (image_comp _ _ _).symm, function.comp, mul_assoc] attribute [to_additive left_add_coset_assoc] left_coset_assoc @[simp] lemma right_coset_assoc (s : set α) (a b : α) : s *r a *r b = s *r (a * b) := by simp [left_coset, right_coset, (image_comp _ _ _).symm, function.comp, mul_assoc] attribute [to_additive right_add_coset_assoc] right_coset_assoc @[to_additive left_add_coset_right_add_coset] lemma left_coset_right_coset (s : set α) (a b : α) : a *l s *r b = a *l (s *r b) := by simp [left_coset, right_coset, (image_comp _ _ _).symm, function.comp, mul_assoc] end coset_semigroup section coset_monoid variables [monoid α] (s : set α) @[simp] lemma one_left_coset : 1 *l s = s := set.ext $ by simp [left_coset] attribute [to_additive zero_left_add_coset] one_left_coset @[simp] lemma right_coset_one : s *r 1 = s := set.ext $ by simp [right_coset] attribute [to_additive right_add_coset_zero] right_coset_one end coset_monoid section coset_submonoid open submonoid variables [monoid α] (s : submonoid α) @[to_additive mem_own_left_add_coset] lemma mem_own_left_coset (a : α) : a ∈ a *l s := suffices a * 1 ∈ a *l s, by simpa, mem_left_coset a (one_mem s) @[to_additive mem_own_right_add_coset] lemma mem_own_right_coset (a : α) : a ∈ (s : set α) *r a := suffices 1 * a ∈ (s : set α) *r a, by simpa, mem_right_coset a (one_mem s) @[to_additive mem_left_add_coset_left_add_coset] lemma mem_left_coset_left_coset {a : α} (ha : a *l s = s) : a ∈ s := by rw [←submonoid.mem_coe, ←ha]; exact mem_own_left_coset s a @[to_additive mem_right_add_coset_right_add_coset] lemma mem_right_coset_right_coset {a : α} (ha : (s : set α) *r a = s) : a ∈ s := by rw [←submonoid.mem_coe, ←ha]; exact mem_own_right_coset s a end coset_submonoid section coset_group variables [group α] {s : set α} {x : α} @[to_additive mem_left_add_coset_iff] lemma mem_left_coset_iff (a : α) : x ∈ a *l s ↔ a⁻¹ * x ∈ s := iff.intro (assume ⟨b, hb, eq⟩, by simp [eq.symm, hb]) (assume h, ⟨a⁻¹ * x, h, by simp⟩) @[to_additive mem_right_add_coset_iff] lemma mem_right_coset_iff (a : α) : x ∈ s *r a ↔ x * a⁻¹ ∈ s := iff.intro (assume ⟨b, hb, eq⟩, by simp [eq.symm, hb]) (assume h, ⟨x * a⁻¹, h, by simp⟩) end coset_group section coset_subgroup open submonoid open is_subgroup variables [group α] (s : set α) [is_subgroup s] @[to_additive left_add_coset_mem_left_add_coset] lemma left_coset_mem_left_coset {a : α} (ha : a ∈ s) : a *l s = s := set.ext $ by simp [mem_left_coset_iff, mul_mem_cancel_right s (inv_mem ha)] @[to_additive right_add_coset_mem_right_add_coset] lemma right_coset_mem_right_coset {a : α} (ha : a ∈ s) : s *r a = s := set.ext $ assume b, by simp [mem_right_coset_iff, mul_mem_cancel_left s (inv_mem ha)] @[to_additive normal_of_eq_add_cosets] theorem normal_of_eq_cosets [normal_subgroup s] (g : α) : g *l s = s *r g := set.ext $ assume a, by simp [mem_left_coset_iff, mem_right_coset_iff]; rw [mem_norm_comm_iff] @[to_additive eq_add_cosets_of_normal] theorem eq_cosets_of_normal (h : ∀ g, g *l s = s *r g) : normal_subgroup s := ⟨assume a ha g, show g * a * g⁻¹ ∈ s, by rw [← mem_right_coset_iff, ← h]; exact mem_left_coset g ha⟩ @[to_additive normal_iff_eq_add_cosets] theorem normal_iff_eq_cosets : normal_subgroup s ↔ ∀ g, g *l s = s *r g := ⟨@normal_of_eq_cosets _ _ s _, eq_cosets_of_normal s⟩ end coset_subgroup run_cmd to_additive.map_namespace `quotient_group `quotient_add_group namespace quotient_group @[to_additive] def left_rel [group α] (s : set α) [is_subgroup s] : setoid α := ⟨λ x y, x⁻¹ * y ∈ s, assume x, by simp [is_submonoid.one_mem], assume x y hxy, have (x⁻¹ * y)⁻¹ ∈ s, from is_subgroup.inv_mem hxy, by simpa using this, assume x y z hxy hyz, have x⁻¹ * y * (y⁻¹ * z) ∈ s, from is_submonoid.mul_mem hxy hyz, by simpa [mul_assoc] using this⟩ /-- `quotient s` is the quotient type representing the left cosets of `s`. If `s` is a normal subgroup, `quotient s` is a group -/ @[to_additive] def quotient [group α] (s : set α) [is_subgroup s] : Type* := quotient (left_rel s) variables [group α] {s : set α} [is_subgroup s] @[to_additive] def mk (a : α) : quotient s := quotient.mk' a @[elab_as_eliminator, to_additive] lemma induction_on {C : quotient s → Prop} (x : quotient s) (H : ∀ z, C (quotient_group.mk z)) : C x := quotient.induction_on' x H @[to_additive] instance : has_coe_t α (quotient s) := ⟨mk⟩ -- note [use has_coe_t] @[elab_as_eliminator, to_additive] lemma induction_on' {C : quotient s → Prop} (x : quotient s) (H : ∀ z : α, C z) : C x := quotient.induction_on' x H @[to_additive] instance [group α] (s : set α) [is_subgroup s] : inhabited (quotient s) := ⟨((1 : α) : quotient s)⟩ @[to_additive quotient_add_group.eq] protected lemma eq {a b : α} : (a : quotient s) = b ↔ a⁻¹ * b ∈ s := quotient.eq' @[to_additive] lemma eq_class_eq_left_coset [group α] (s : set α) [is_subgroup s] (g : α) : {x : α | (x : quotient s) = g} = left_coset g s := set.ext $ λ z, by rw [mem_left_coset_iff, set.mem_set_of_eq, eq_comm, quotient_group.eq] end quotient_group namespace is_subgroup open quotient_group variables [group α] {s : set α} @[to_additive] def left_coset_equiv_subgroup (g : α) : left_coset g s ≃ s := ⟨λ x, ⟨g⁻¹ * x.1, (mem_left_coset_iff _).1 x.2⟩, λ x, ⟨g * x.1, x.1, x.2, rfl⟩, λ ⟨x, hx⟩, subtype.eq $ by simp, λ ⟨g, hg⟩, subtype.eq $ by simp⟩ @[to_additive] noncomputable def group_equiv_quotient_times_subgroup (hs : is_subgroup s) : α ≃ quotient s × s := calc α ≃ Σ L : quotient s, {x : α // (x : quotient s)= L} : (equiv.sigma_preimage_equiv quotient_group.mk).symm ... ≃ Σ L : quotient s, left_coset (quotient.out' L) s : equiv.sigma_congr_right (λ L, begin rw ← eq_class_eq_left_coset, show {x // quotient.mk' x = L} ≃ {x : α // quotient.mk' x = quotient.mk' _}, simp [-quotient.eq'] end) ... ≃ Σ L : quotient s, s : equiv.sigma_congr_right (λ L, left_coset_equiv_subgroup _) ... ≃ quotient s × s : equiv.sigma_equiv_prod _ _ end is_subgroup namespace quotient_group variables [group α] noncomputable def preimage_mk_equiv_subgroup_times_set (s : set α) [is_subgroup s] (t : set (quotient s)) : quotient_group.mk ⁻¹' t ≃ s × t := have h : ∀ {x : quotient s} {a : α}, x ∈ t → a ∈ s → (quotient.mk' (quotient.out' x * a) : quotient s) = quotient.mk' (quotient.out' x) := λ x a hx ha, quotient.sound' (show (quotient.out' x * a)⁻¹ * quotient.out' x ∈ s, from (is_subgroup.inv_mem_iff _).1 $ by rwa [mul_inv_rev, inv_inv, ← mul_assoc, inv_mul_self, one_mul]), { to_fun := λ ⟨a, ha⟩, ⟨⟨(quotient.out' (quotient.mk' a))⁻¹ * a, @quotient.exact' _ (left_rel s) _ _ $ (quotient.out_eq' _)⟩, ⟨quotient.mk' a, ha⟩⟩, inv_fun := λ ⟨⟨a, ha⟩, ⟨x, hx⟩⟩, ⟨quotient.out' x * a, show quotient.mk' _ ∈ t, by simp [h hx ha, hx]⟩, left_inv := λ ⟨a, ha⟩, subtype.eq $ show _ * _ = a, by simp, right_inv := λ ⟨⟨a, ha⟩, ⟨x, hx⟩⟩, show (_, _) = _, by simp [h hx ha] } end quotient_group /-- We use the class `has_coe_t` instead of `has_coe` if the first-argument is a variable. Using `has_coe` would cause looping of type-class inference. See <https://leanprover.zulipchat.com/#narrow/stream/113488-general/topic/remove.20all.20instances.20with.20variable.20domain> -/ library_note "use has_coe_t"
58860c16fda93c2163502e98a93765109167c25c
bf532e3e865883a676110e756f800e0ddeb465be
/data/seq/parallel.lean
da926c6ffa6efb50352388e94921adf4ee05c4b8
[ "Apache-2.0" ]
permissive
aqjune/mathlib
da42a97d9e6670d2efaa7d2aa53ed3585dafc289
f7977ff5a6bcf7e5c54eec908364ceb40dafc795
refs/heads/master
1,631,213,225,595
1,521,089,840,000
1,521,089,840,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
12,208
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro Parallel computation of a computable sequence of computations by a diagonal enumeration. The important theorems of this operation are proven as terminates_parallel and exists_of_mem_parallel. (This operation is nondeterministic in the sense that it does not honor sequence equivalence (irrelevance of computation time).) -/ import data.seq.wseq universes u v namespace computation open wseq variables {α : Type u} {β : Type v} def parallel.aux2 : list (computation α) → α ⊕ list (computation α) := list.foldr (λc o, match o with | sum.inl a := sum.inl a | sum.inr ls := rmap (λ c', c' :: ls) (destruct c) end) (sum.inr []) def parallel.aux1 : list (computation α) × wseq (computation α) → α ⊕ list (computation α) × wseq (computation α) | (l, S) := rmap (λ l', match seq.destruct S with | none := (l', nil) | some (none, S') := (l', S') | some (some c, S') := (c::l', S') end) (parallel.aux2 l) /-- Parallel computation of an infinite stream of computations, taking the first result -/ def parallel (S : wseq (computation α)) : computation α := corec parallel.aux1 ([], S) theorem terminates_parallel.aux : ∀ {l : list (computation α)} {S c}, c ∈ l → terminates c → terminates (corec parallel.aux1 (l, S)) := begin have lem1 : ∀ l S, (∃ (a : α), parallel.aux2 l = sum.inl a) → terminates (corec parallel.aux1 (l, S)), { intros l S e, cases e with a e, have this : corec parallel.aux1 (l, S) = return a, { apply destruct_eq_ret, simp [parallel.aux1], rw e, simp [rmap] }, rw this, apply_instance }, intros l S c m T, revert l S, apply @terminates_rec_on _ _ c T _ _, { intros a l S m, apply lem1, induction l with c l IH generalizing m; simp at m, { contradiction }, cases m with e m, { rw ←e, simp [parallel.aux2], cases list.foldr parallel.aux2._match_1 (sum.inr list.nil) l with a' ls, exacts [⟨a', rfl⟩, ⟨a, rfl⟩] }, { cases IH m with a' e, simp [parallel.aux2], simp [parallel.aux2] at e, rw e, exact ⟨a', rfl⟩ } }, { intros s IH l S m, have H1 : ∀ l', parallel.aux2 l = sum.inr l' → s ∈ l', { induction l with c l IH' generalizing m; intros l' e'; simp at m, { contradiction }, cases m with e m; simp [parallel.aux2] at e', { rw ←e at e', cases list.foldr parallel.aux2._match_1 (sum.inr list.nil) l with a' ls; injection e' with e', rw ←e', simp }, { induction e : list.foldr parallel.aux2._match_1 (sum.inr list.nil) l with a' ls; rw e at e', { contradiction }, have := IH' m _ e, simp [parallel.aux2] at e', cases destruct c; injection e' with h', rw ←h', simp [this] } }, induction h : parallel.aux2 l with a l', { exact lem1 _ _ ⟨a, h⟩ }, { have H2 : corec parallel.aux1 (l, S) = think _, { apply destruct_eq_think, simp [parallel.aux1], rw h, simp [rmap] }, rw H2, apply @computation.think_terminates _ _ _, have := H1 _ h, rcases seq.destruct S with _ | ⟨_|c, S'⟩; simp [parallel.aux1]; apply IH; simp [this] } } end theorem terminates_parallel {S : wseq (computation α)} {c} (h : c ∈ S) [T : terminates c] : terminates (parallel S) := suffices ∀ n (l : list (computation α)) S c, c ∈ l ∨ some (some c) = seq.nth S n → terminates c → terminates (corec parallel.aux1 (l, S)), from let ⟨n, h⟩ := h in this n [] S c (or.inr h) T, begin intro n, induction n with n IH; intros l S c o T, { cases o with a a, { exact terminates_parallel.aux a T }, have H : seq.destruct S = some (some c, _), { unfold seq.destruct has_map.map, rw ← a, simp [option.map, option.bind] }, induction h : parallel.aux2 l with a l'; have C : corec parallel.aux1 (l, S) = _, { apply destruct_eq_ret, simp [parallel.aux1], rw [h], simp [rmap] }, { rw C, resetI, apply_instance }, { apply destruct_eq_think, simp [parallel.aux1], rw [h, H], simp [rmap] }, { rw C, apply @computation.think_terminates _ _ _, apply terminates_parallel.aux _ T, simp } }, { cases o with a a, { exact terminates_parallel.aux a T }, induction h : parallel.aux2 l with a l'; have C : corec parallel.aux1 (l, S) = _, { apply destruct_eq_ret, simp [parallel.aux1], rw [h], simp [rmap] }, { rw C, resetI, apply_instance }, { apply destruct_eq_think, simp [parallel.aux1], rw [h], simp [rmap] }, { rw C, apply @computation.think_terminates _ _ _, have TT : ∀ l', terminates (corec parallel.aux1 (l', S.tail)), { intro, apply IH _ _ _ (or.inr _) T, rw a, cases S with f al, refl }, induction e : seq.nth S 0 with o, { have D : seq.destruct S = none, { dsimp [seq.destruct], rw e, refl }, rw D, simp [parallel.aux1], have TT := TT l', rwa [seq.destruct_eq_nil D, seq.tail_nil] at TT }, { have D : seq.destruct S = some (o, S.tail), { dsimp [seq.destruct], rw e, refl }, rw D, cases o with c; simp [parallel.aux1, TT] } } } end theorem exists_of_mem_parallel {S : wseq (computation α)} {a} (h : a ∈ parallel S) : ∃ c ∈ S, a ∈ c := suffices ∀ C, a ∈ C → ∀ (l : list (computation α)) S, corec parallel.aux1 (l, S) = C → ∃ c, (c ∈ l ∨ c ∈ S) ∧ a ∈ c, from let ⟨c, h1, h2⟩ := this _ h [] S rfl in ⟨c, h1.resolve_left id, h2⟩, begin let F : list (computation α) → α ⊕ list (computation α) → Prop, { intros l a, cases a with a l', exact ∃ c ∈ l, a ∈ c, exact ∀ a', (∃ c ∈ l', a' ∈ c) → (∃ c ∈ l, a' ∈ c) }, have lem1 : ∀ (l : list (computation α)), F l (parallel.aux2 l), { intro l, induction l with c l IH; simp [parallel.aux2], { intros a h, cases h with c h, cases h with hn, exact false.elim hn }, { simp [parallel.aux2] at IH, cases list.foldr parallel.aux2._match_1 (sum.inr list.nil) l with a ls; simp [parallel.aux2], { cases IH with c' cl, cases cl with cl ac, refine ⟨c', or.inr cl, ac⟩ }, { induction h : destruct c with a c'; simp [rmap], { refine ⟨c, list.mem_cons_self _ _, _⟩, rw destruct_eq_ret h, apply ret_mem }, { intros a' h, cases h with d h, cases h with dm ad, simp at dm, cases dm with e dl, { rw e at ad, refine ⟨c, list.mem_cons_self _ _, _⟩, rw destruct_eq_think h, exact think_mem ad }, { cases IH a' ⟨d, dl, ad⟩ with d dm, cases dm with dm ad, exact ⟨d, or.inr dm, ad⟩ } } } } }, intros C aC, refine mem_rec_on aC _ (λ C' IH, _); intros l S e; have e' := congr_arg destruct e; have := lem1 l; simp [parallel.aux1] at e'; cases parallel.aux2 l with a' l'; injection e' with h', { rw h' at this, cases this with c cl, cases cl with cl ac, exact ⟨c, or.inl cl, ac⟩ }, { induction e : seq.destruct S with a; rw e at h', { exact let ⟨d, o, ad⟩ := IH _ _ h', ⟨c, cl, ac⟩ := this a ⟨d, o.resolve_right (not_mem_nil _), ad⟩ in ⟨c, or.inl cl, ac⟩ }, { cases a with o S', cases o with c; simp [parallel.aux1] at h'; cases IH _ _ h' with d dm; cases dm with o ad; cases o with dl dS', { exact let ⟨c, cl, ac⟩ := this a ⟨d, dl, ad⟩ in ⟨c, or.inl cl, ac⟩ }, { refine ⟨d, or.inr _, ad⟩, rw seq.destruct_eq_cons e, exact seq.mem_cons_of_mem _ dS' }, { simp at dl, cases dl with dc dl, { rw dc at ad, refine ⟨c, or.inr _, ad⟩, rw seq.destruct_eq_cons e, apply seq.mem_cons }, { exact let ⟨c, cl, ac⟩ := this a ⟨d, dl, ad⟩ in ⟨c, or.inl cl, ac⟩ } }, { refine ⟨d, or.inr _, ad⟩, rw seq.destruct_eq_cons e, exact seq.mem_cons_of_mem _ dS' } } } end theorem map_parallel (f : α → β) (S) : map f (parallel S) = parallel (S.map (map f)) := begin refine eq_of_bisim (λ c1 c2, ∃ l S, c1 = map f (corec parallel.aux1 (l, S)) ∧ c2 = corec parallel.aux1 (l.map (map f), S.map (map f))) _ ⟨[], S, rfl, rfl⟩, intros c1 c2 h, exact match c1, c2, h with ._, ._, ⟨l, S, rfl, rfl⟩ := begin clear _match, have : parallel.aux2 (l.map (map f)) = lmap f (rmap (list.map (map f)) (parallel.aux2 l)), { simp [parallel.aux2], induction l with c l IH; simp, rw [IH], cases list.foldr parallel.aux2._match_1 (sum.inr list.nil) l; simp [parallel.aux2], cases destruct c; simp }, simp [parallel.aux1], rw this, cases parallel.aux2 l with a l'; simp, apply S.cases_on _ (λ c S, _) (λ S, _); simp; simp [parallel.aux1]; exact ⟨_, _, rfl, rfl⟩ end end end theorem parallel_empty (S : wseq (computation α)) (h : S.head ~> none) : parallel S = empty _ := eq_empty_of_not_terminates $ λ ⟨a, m⟩, let ⟨c, cs, ac⟩ := exists_of_mem_parallel m, ⟨n, nm⟩ := exists_nth_of_mem cs, ⟨c', h'⟩ := head_some_of_nth_some nm in by injection h h' -- The reason this isn't trivial from exists_of_mem_parallel is because it eliminates to Sort def parallel_rec {S : wseq (computation α)} (C : α → Sort v) (H : ∀ s ∈ S, ∀ a ∈ s, C a) {a} (h : a ∈ parallel S) : C a := begin let T : wseq (computation (α × computation α)) := S.map (λc, c.map (λ a, (a, c))), have : S = T.map (map (λ c, c.1)), { rw [←wseq.map_comp], refine (wseq.map_id _).symm.trans (congr_arg (λ f, wseq.map f S) _), funext c, dsimp [id, function.comp], rw [←map_comp], exact (map_id _).symm }, have pe := congr_arg parallel this, rw ←map_parallel at pe, have h' := h, rw pe at h', haveI : terminates (parallel T) := (terminates_map_iff _ _).1 ⟨_, h'⟩, induction e : get (parallel T) with a' c, have : a ∈ c ∧ c ∈ S, { cases exists_of_mem_map h' with d h, cases h with dT cd, rw get_eq_of_mem _ dT at e, cases e, dsimp at cd, cases cd, cases exists_of_mem_parallel dT with d' h, cases h with dT' ad', cases wseq.exists_of_mem_map dT' with c' h, cases h with cs' e', rw ←e' at ad', cases exists_of_mem_map ad' with a' h, cases h with ac' e', injection e' with i1 i2, constructor, rwa [i1, i2] at ac', rwa i2 at cs' }, cases this with ac cs, apply H _ cs _ ac end theorem parallel_promises {S : wseq (computation α)} {a} (H : ∀ s ∈ S, s ~> a) : parallel S ~> a := λ a' ma', let ⟨c, cs, ac⟩ := exists_of_mem_parallel ma' in H _ cs ac theorem mem_parallel {S : wseq (computation α)} {a} (H : ∀ s ∈ S, s ~> a) {c} (cs : c ∈ S) (ac : a ∈ c) : a ∈ parallel S := by haveI := terminates_of_mem ac; have := terminates_parallel cs; exact mem_of_promises _ (parallel_promises H) theorem parallel_congr_lem {S T : wseq (computation α)} {a} (H : S.lift_rel equiv T) : (∀ s ∈ S, s ~> a) ↔ (∀ t ∈ T, t ~> a) := ⟨λ h1 t tT, let ⟨s, sS, se⟩ := wseq.exists_of_lift_rel_right H tT in (promises_congr se _).1 (h1 _ sS), λ h2 s sS, let ⟨t, tT, se⟩ := wseq.exists_of_lift_rel_left H sS in (promises_congr se _).2 (h2 _ tT)⟩ -- The parallel operation is only deterministic when all computation paths lead to the same value theorem parallel_congr_left {S T : wseq (computation α)} {a} (h1 : ∀ s ∈ S, s ~> a) (H : S.lift_rel equiv T) : parallel S ~ parallel T := let h2 := (parallel_congr_lem H).1 h1 in λ a', ⟨λh, by have aa := parallel_promises h1 h; rw ←aa; rw ←aa at h; exact let ⟨s, sS, as⟩ := exists_of_mem_parallel h, ⟨t, tT, st⟩ := wseq.exists_of_lift_rel_left H sS, aT := (st _).1 as in mem_parallel h2 tT aT, λh, by have aa := parallel_promises h2 h; rw ←aa; rw ←aa at h; exact let ⟨s, sS, as⟩ := exists_of_mem_parallel h, ⟨t, tT, st⟩ := wseq.exists_of_lift_rel_right H sS, aT := (st _).2 as in mem_parallel h1 tT aT⟩ theorem parallel_congr_right {S T : wseq (computation α)} {a} (h2 : ∀ t ∈ T, t ~> a) (H : S.lift_rel equiv T) : parallel S ~ parallel T := parallel_congr_left ((parallel_congr_lem H).2 h2) H end computation
6d3e639c6c9c0d2ab8ac4e9acf8469f23bd871d0
d642a6b1261b2cbe691e53561ac777b924751b63
/src/order/filter/pointwise.lean
fbc5faeb53df26b24ee16171ae2d072de8fdab11
[ "Apache-2.0" ]
permissive
cipher1024/mathlib
fee56b9954e969721715e45fea8bcb95f9dc03fe
d077887141000fefa5a264e30fa57520e9f03522
refs/heads/master
1,651,806,490,504
1,573,508,694,000
1,573,508,694,000
107,216,176
0
0
Apache-2.0
1,647,363,136,000
1,508,213,014,000
Lean
UTF-8
Lean
false
false
7,682
lean
/- Copyright (c) 2019 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou The pointwise operations on filters have nice properties, such as • map m (f₁ * f₂) = map m f₁ * map m f₂ • nhds x * nhds y = nhds (x * y) -/ import algebra.pointwise import order.filter.basic open classical set lattice universes u v w variables {α : Type u} {β : Type v} {γ : Type w} open_locale classical local attribute [instance] pointwise_one pointwise_mul pointwise_add namespace filter open set @[to_additive] def pointwise_one [has_one α] : has_one (filter α) := ⟨principal {1}⟩ local attribute [instance] pointwise_one @[simp, to_additive] lemma mem_pointwise_one [has_one α] (s : set α) : s ∈ (1 : filter α) ↔ (1:α) ∈ s := calc s ∈ (1:filter α) ↔ {(1:α)} ⊆ s : iff.rfl ... ↔ (1:α) ∈ s : by simp @[to_additive] def pointwise_mul [monoid α] : has_mul (filter α) := ⟨λf g, { sets := { s | ∃t₁∈f, ∃t₂∈g, t₁ * t₂ ⊆ s }, univ_sets := begin have h₁ : (∃x, x ∈ f.sets) := ⟨univ, univ_sets f⟩, have h₂ : (∃x, x ∈ g.sets) := ⟨univ, univ_sets g⟩, simpa using and.intro h₁ h₂ end, sets_of_superset := λx y hx hxy, begin rcases hx with ⟨t₁, ht₁, t₂, ht₂, t₁t₂⟩, exact ⟨t₁, ht₁, t₂, ht₂, subset.trans t₁t₂ hxy⟩ end, inter_sets := λx y, begin simp only [exists_prop, mem_set_of_eq, subset_inter_iff], rintros ⟨s₁, hs₁, s₂, hs₂, s₁s₂⟩ ⟨t₁, ht₁, t₂, ht₂, t₁t₂⟩, exact ⟨s₁ ∩ t₁, inter_sets f hs₁ ht₁, s₂ ∩ t₂, inter_sets g hs₂ ht₂, subset.trans (pointwise_mul_subset_mul (inter_subset_left _ _) (inter_subset_left _ _)) s₁s₂, subset.trans (pointwise_mul_subset_mul (inter_subset_right _ _) (inter_subset_right _ _)) t₁t₂⟩, end }⟩ local attribute [instance] pointwise_mul pointwise_add @[to_additive] lemma mem_pointwise_mul [monoid α] {f g : filter α} {s : set α} : s ∈ f * g ↔ ∃ t₁ ∈ f, ∃ t₂ ∈ g, t₁ * t₂ ⊆ s := iff.rfl @[to_additive] lemma mul_mem_pointwise_mul [monoid α] {f g : filter α} {s t : set α} (hs : s ∈ f) (ht : t ∈ g) : s * t ∈ f * g := ⟨_, hs, _, ht, subset.refl _⟩ @[to_additive] lemma pointwise_mul_le_mul [monoid α] {f₁ f₂ g₁ g₂ : filter α} (hf : f₁ ≤ f₂) (hg : g₁ ≤ g₂) : f₁ * g₁ ≤ f₂ * g₂ := assume _ ⟨s, hs, t, ht, hst⟩, ⟨s, hf hs, t, hg ht, hst⟩ @[to_additive] lemma pointwise_mul_ne_bot [monoid α] {f g : filter α} : f ≠ ⊥ → g ≠ ⊥ → f * g ≠ ⊥ := begin simp only [forall_sets_neq_empty_iff_neq_bot.symm], rintros hf hg s ⟨a, ha, b, hb, ab⟩, rcases ne_empty_iff_exists_mem.1 (pointwise_mul_ne_empty (hf a ha) (hg b hb)) with ⟨x, hx⟩, exact ne_empty_iff_exists_mem.2 ⟨x, ab hx⟩ end @[to_additive] lemma pointwise_mul_assoc [monoid α] (f g h : filter α) : f * g * h = f * (g * h) := begin ext s, split, { rintros ⟨a, ⟨a₁, ha₁, a₂, ha₂, a₁a₂⟩, b, hb, ab⟩, refine ⟨a₁, ha₁, a₂ * b, mul_mem_pointwise_mul ha₂ hb, _⟩, rw [← pointwise_mul_semigroup.mul_assoc], exact calc a₁ * a₂ * b ⊆ a * b : pointwise_mul_subset_mul a₁a₂ (subset.refl _) ... ⊆ s : ab }, { rintros ⟨a, ha, b, ⟨b₁, hb₁, b₂, hb₂, b₁b₂⟩, ab⟩, refine ⟨a * b₁, mul_mem_pointwise_mul ha hb₁, b₂, hb₂, _⟩, rw [pointwise_mul_semigroup.mul_assoc], exact calc a * (b₁ * b₂) ⊆ a * b : pointwise_mul_subset_mul (subset.refl _) b₁b₂ ... ⊆ s : ab } end local attribute [instance] pointwise_mul_monoid @[to_additive] lemma pointwise_one_mul [monoid α] (f : filter α) : 1 * f = f := begin ext s, split, { rintros ⟨t₁, ht₁, t₂, ht₂, t₁t₂⟩, refine mem_sets_of_superset (mem_sets_of_superset ht₂ _) t₁t₂, assume x hx, exact ⟨1, by rwa [← mem_pointwise_one], x, hx, (one_mul _).symm⟩ }, { assume hs, refine ⟨(1:set α), mem_principal_self _, s, hs, by simp only [one_mul]⟩ } end @[to_additive] lemma pointwise_mul_one [monoid α] (f : filter α) : f * 1 = f := begin ext s, split, { rintros ⟨t₁, ht₁, t₂, ht₂, t₁t₂⟩, refine mem_sets_of_superset (mem_sets_of_superset ht₁ _) t₁t₂, assume x hx, exact ⟨x, hx, 1, by rwa [← mem_pointwise_one], (mul_one _).symm⟩ }, { assume hs, refine ⟨s, hs, (1:set α), mem_principal_self _, by simp only [mul_one]⟩ } end @[to_additive pointwise_add_add_monoid] def pointwise_mul_monoid [monoid α] : monoid (filter α) := { mul_assoc := pointwise_mul_assoc, one_mul := pointwise_one_mul, mul_one := pointwise_mul_one, .. pointwise_mul, .. pointwise_one } local attribute [instance] filter.pointwise_mul_monoid filter.pointwise_add_add_monoid section map open is_mul_hom variables [monoid α] [monoid β] {f : filter α} (m : α → β) @[to_additive] lemma map_pointwise_mul [is_mul_hom m] {f₁ f₂ : filter α} : map m (f₁ * f₂) = map m f₁ * map m f₂ := filter_eq $ set.ext $ assume s, begin simp only [mem_pointwise_mul], split, { rintro ⟨t₁, ht₁, t₂, ht₂, t₁t₂⟩, have : m '' (t₁ * t₂) ⊆ s := subset.trans (image_subset m t₁t₂) (image_preimage_subset _ _), refine ⟨m '' t₁, image_mem_map ht₁, m '' t₂, image_mem_map ht₂, _⟩, rwa ← image_pointwise_mul m t₁ t₂ }, { rintro ⟨t₁, ht₁, t₂, ht₂, t₁t₂⟩, refine ⟨m ⁻¹' t₁, ht₁, m ⁻¹' t₂, ht₂, image_subset_iff.1 _⟩, rw image_pointwise_mul m, exact subset.trans (pointwise_mul_subset_mul (image_preimage_subset _ _) (image_preimage_subset _ _)) t₁t₂ }, end @[to_additive] lemma map_pointwise_one [is_monoid_hom m] : map m (1:filter α) = 1 := le_antisymm (le_principal_iff.2 $ mem_map_sets_iff.2 ⟨(1:set α), by simp, by { assume x, simp [is_monoid_hom.map_one m], rintros rfl, refl }⟩) (le_map $ assume s hs, begin simp only [mem_pointwise_one], exact ⟨(1:α), (mem_pointwise_one s).1 hs, is_monoid_hom.map_one _⟩ end) -- TODO: prove similar statements when `m` is group homomorphism etc. lemma pointwise_mul_map_is_monoid_hom [is_monoid_hom m] : is_monoid_hom (map m) := { map_one := map_pointwise_one m, map_mul := λ _ _, map_pointwise_mul m } lemma pointwise_add_map_is_add_monoid_hom {α : Type*} {β : Type*} [add_monoid α] [add_monoid β] (m : α → β) [is_add_monoid_hom m] : is_add_monoid_hom (map m) := { map_zero := map_pointwise_zero m, map_add := λ _ _, map_pointwise_add m } attribute [to_additive pointwise_add_map_is_add_monoid_hom] pointwise_mul_map_is_monoid_hom -- The other direction does not hold in general. @[to_additive] lemma comap_mul_comap_le [is_mul_hom m] {f₁ f₂ : filter β} : comap m f₁ * comap m f₂ ≤ comap m (f₁ * f₂) := begin rintros s ⟨t, ⟨t₁, ht₁, t₂, ht₂, t₁t₂⟩, mt⟩, refine ⟨m ⁻¹' t₁, ⟨t₁, ht₁, subset.refl _⟩, m ⁻¹' t₂, ⟨t₂, ht₂, subset.refl _⟩, _⟩, have := subset.trans (preimage_mono t₁t₂) mt, exact subset.trans (preimage_pointwise_mul_preimage_subset m _ _) this end variables {m} @[to_additive] lemma tendsto_mul_mul [is_mul_hom m] {f₁ g₁ : filter α} {f₂ g₂ : filter β} : tendsto m f₁ f₂ → tendsto m g₁ g₂ → tendsto m (f₁ * g₁) (f₂ * g₂) := assume hf hg, by { rw [tendsto, map_pointwise_mul m], exact pointwise_mul_le_mul hf hg } end map end filter
f5a7f4ee1affc547df5e6c46631011905a5a7e7b
995658a556256a6694fe5c98b91f7f6f6c11bb77
/src/dirichlet_eta.lean
0865e1003b228b5a3bb5eacc40757918edfbc87c
[ "MIT" ]
permissive
bhgomes/lean-riemann-hypothesis
d803047f27ea21f03414bbfb59395608ed0bc05f
c36b744a2dc4a7a50c7de770096bd9a051f42ab9
refs/heads/master
1,678,533,614,591
1,614,755,910,000
1,614,755,910,000
275,204,877
16
3
null
null
null
null
UTF-8
Lean
false
false
13,906
lean
/- ------------------------------------------------------------------------- -| | @project: riemann_hypothesis | | @file: dirichlet_eta.lean | | @authors: Brandon H. Gomes, Alex Kontorovich | | @affil: Rutgers University | |- ------------------------------------------------------------------------- -/ import .complex import .riemann_zeta /-! -/ namespace riemann_hypothesis --——————————————————————————————————————————————————————————-- variables {ℂ : Type*} {ℝ : Type*} [has_lift_t nat ℝ] [has_lift_t ℝ ℂ] [preorder ℝ] [Algebra ℝ] [Algebra ℂ] (ℭ : Complex ℂ ℝ) namespace dirichlet_eta --———————————————————————————————————————————————————————————————-- open algebra /-- -/ def term_pair [has_lift_zero_same nat ℝ] [has_lift_lt_comm nat ℝ] (s n) := riemann_zeta.term ℭ.pow s (2 * n) - riemann_zeta.term ℭ.pow s (2 * n).succ namespace term_pair --———————————————————————————————————————————————————————————————————-- /-- -/ def riemann_zeta_bound (s) := ↑4 * ℭ.real_exp (ℭ.abs s) + ↑3 * ℭ.abs s namespace riemann_zeta_bound --——————————————————————————————————————————————————————————-- variables [has_zero_mul_is_zero ℝ] [has_nonneg_mul_nonneg_is_nonneg ℝ] [has_lt_add_of_le_of_pos ℝ] [has_lt_pos_mul_preserves_right ℝ] [has_lift_zero_same nat ℝ] [has_lift_one_same nat ℝ] [has_lift_add_comm nat ℝ] [has_lift_lt_comm nat ℝ] /-- -/ def positive (s) : 0 < riemann_zeta_bound ℭ s := begin refine has_lt_add_of_le_of_pos.lt _ (has_nonneg_mul_nonneg_is_nonneg.le (le_of_lt (nat.lift.succ_pos ℝ _)) (ℭ.abs_nonneg _)), let four_exponentials_are_positive := has_lt_pos_mul_preserves_right.lt (ℭ.real_explog.exp_positive _) (nat.lift.succ_pos ℝ _), rw has_zero_mul_is_zero.eq at four_exponentials_are_positive, refine four_exponentials_are_positive, end /-- -/ def abs_positive (ge_zero_to_abs : Π z, 0 ≤ z → ℭ.abs ↑z = z) (s) : 0 < ℭ.abs ↑(riemann_zeta_bound ℭ s) := begin rw ge_zero_to_abs _ (le_of_lt (positive ℭ _)), refine positive ℭ _, end end riemann_zeta_bound --————————————————————————————————————————————————————————————————-- /-- -/ def bounded_by_riemann_zeta [has_add_le_add ℝ] [has_inv_mul_reverse ℝ] [has_inv_mul_right_cancel_self ℝ] [has_inv_reverses_le ℝ] [has_le_nonneg_mul_preserves_left ℝ] [has_le_nonneg_mul_preserves_right ℝ] [has_le_pos_mul_preserves_left ℝ] [has_le_pos_mul_preserves_right ℝ] [has_left_add_distributivity ℝ] [has_left_sub_distributivity ℝ] [has_left_unit ℝ] [has_lt_add_of_le_of_pos ℝ] [has_lt_pos_mul_preserves_right ℝ] [has_mul_assoc ℝ] [has_nonneg_mul_nonneg_is_nonneg ℝ] [has_right_add_distributivity ℝ] [has_right_inv_pos_lt ℝ] [has_right_sub_distributivity ℝ] [has_right_unit ℝ] [has_squared_le_monotonic ℝ] [has_zero_mul_is_zero ℝ] [has_mul_zero_is_zero ℝ] [has_zero_right_add_cancel ℝ] [has_zero_sub_is_neg ℝ] [has_mul_pos ℝ] [has_inv_pos ℝ] [has_lift_add_comm nat ℝ] [has_lift_le_comm nat ℝ] [has_lift_lt_comm nat ℝ] [has_lift_ne_comm nat ℝ] [has_lift_sub_comm nat ℝ] [has_lift_one_same nat ℝ] [has_lift_zero_same nat ℝ] [has_add_sub_assoc ℂ] [has_inv_mul_left_cancel_self ℂ] [has_left_sub_distributivity ℂ] [has_left_unit ℂ] [has_mul_assoc ℂ] [has_right_sub_distributivity ℂ] [has_right_unit ℂ] [has_sub_add_sub_cancel ℂ] [has_sub_self_is_zero ℂ] [has_mul_zero_is_zero ℂ] [has_zero_right_add_cancel ℂ] [has_zero_sub_is_neg ℂ] [has_lift_sub_comm ℝ ℂ] (abs_mul : Π a b, ℭ.abs (a * b) = ℭ.abs (a) * ℭ.abs (b)) (abs_sub : Π a b, ℭ.abs (a - b) = ℭ.abs (b - a)) (abs_inv : Π z, ℭ.abs (z⁻¹) = (ℭ.abs z)⁻¹) (abs_add : Π a b, ℭ.abs (a + b) ≤ ℭ.abs a + ℭ.abs b) (ge_zero_to_abs : Π x, 0 ≤ x → ℭ.abs ↑x = x) (s n) : ℭ.abs (term_pair ℭ s n) ≤ riemann_zeta.term.on_reals ℭ.real_explog (1 + ℭ.real_part s) (2 * n) * riemann_zeta_bound ℭ s := begin rw [term_pair, riemann_zeta.term.on_reals], rw [riemann_zeta.term, riemann_zeta.term, riemann_zeta.term], rw [← has_zero_sub_is_neg.eq, ← has_zero_sub_is_neg.eq], rw ExpLog.pow_neg_exponent_inverts, rw ExpLog.pow_homomorphism, rw has_inv_mul_reverse.eq, rw ℭ.real_explog.pow_id_at_one _ (nat.lift.succ_pos ℝ _), rw Complex.pow_neg_exponent_inverts, rw Complex.pow_neg_exponent_inverts, rw inv_sub_inv_lemma' (ℭ.pow_nonzero _ _ _), rw abs_mul, rw abs_inv, rw Complex.abs_pow_is_real_pow, rw has_mul_assoc.eq, rw ← ExpLog.pow_neg_exponent_inverts, let K := (↑(2 * n).succ.succ : ℝ), have Kinv_ge_zero : K⁻¹ ≥ 0, { let ineq := has_right_inv_pos_lt.lt (nat.lift.succ_pos ℝ _) (nat.lift.zero_lt_one ℝ), rw [has_zero_mul_is_zero.eq, has_left_unit.eq] at ineq, refine le_of_lt ineq, }, let remove_abs := ge_zero_to_abs _ Kinv_ge_zero, refine has_le_pos_mul_preserves_left.le (ℭ.real_explog.pow_positivity _ (nat.lift.succ_pos ℝ _) _) _, rw [Complex.pow, Complex.pow], rw ← Complex.exp_homomorphism_inv, rw ← has_right_sub_distributivity.eq, rw ← has_lift_sub_comm.eq, rw ← ℭ.real_explog.log_homomorphism_inv (nat.lift.succ_pos ℝ _) (nat.lift.succ_pos ℝ _), rw ← Complex.pow, rw ℭ.pow_domain_irrel (↑((2 * n).succ) * (↑((2 * n).succ.succ))⁻¹) _ (1 - (↑(2 * n).succ.succ)⁻¹) _ _ (by rw mul_inv_add_one_lemma), refine le_trans _ (has_le_pos_mul_preserves_right.le (riemann_zeta_bound.positive _ _) (has_inv_reverses_le.le (le_of_lt (has_lift_lt_comm.lt (nat.lt_succ_self _))))), rw ← mul_inv_add_one_lemma, refine has_mul_pos.lt (nat.lift.succ_pos ℝ _) (has_inv_pos.lt (nat.lift.succ_pos ℝ _)), rw abs_sub, rw riemann_zeta_bound, rw has_left_add_distributivity.eq, refine le_trans (Complex.one_minus_pow_bound _ _ _ abs_mul abs_add _ _ _ _) _, rw two_is_lifted_two_lemma nat ℝ, refine nat.lift.succ_pos ℝ _, rw two_is_lifted_two_lemma nat ℝ, rw one_is_lifted_one_lemma nat ℝ, refine has_lift_le_comm.le (le_of_lt (nat.lt_succ_self _)), rw remove_abs, rw two_is_lifted_two_lemma nat ℝ, refine has_inv_reverses_le.le (has_lift_le_comm.le (nat.smallest_positive_even _)), rw remove_abs, rw ← has_left_add_distributivity.eq, rw four_is_lifted_four_lemma nat ℝ, rw ← two_mul_lemma, refine has_le_nonneg_mul_preserves_left.le Kinv_ge_zero (has_add_le_add.le (le_refl _) (has_le_nonneg_mul_preserves_right.le (ℭ.abs_nonneg _) _)), rw two_is_lifted_two_lemma nat ℝ, refine has_lift_le_comm.le (le_of_lt (nat.lt_succ_self _)), end end term_pair --—————————————————————————————————————————————————————————————————————————-- /-- -/ def partial_pairs [has_lift_lt_comm nat ℝ] [has_lift_zero_same nat ℝ] (s) := partial_sum (term_pair ℭ s) /-- -/ def partial_pairs.is_cauchy [has_add_le_add ℝ] [has_add_left_lt ℝ] [has_add_lt_add ℝ] [has_add_nonneg ℝ] [has_add_sub_assoc ℝ] [has_add_sub_exchange ℝ] [has_inv_mul_left_cancel_self ℝ] [has_inv_mul_reverse ℝ] [has_inv_mul_right_cancel_self ℝ] [has_inv_reverses_le ℝ] [has_inv_reverses_lt ℝ] [has_inv_right_mul_lt_pos ℝ] [has_le_add_of_nonneg_of_le ℝ] [has_le_nonneg_mul_preserves_left ℝ] [has_le_nonneg_mul_preserves_right ℝ] [has_le_pos_mul_preserves_left ℝ] [has_le_pos_mul_preserves_right ℝ] [has_le_sub_add_le ℝ] [has_left_add_distributivity ℝ] [has_left_mul_inv_lt_neg ℝ] [has_left_mul_inv_lt_pos ℝ] [has_left_sub_distributivity ℝ] [has_left_unit ℝ] [has_lt_add_of_le_of_pos ℝ] [has_lt_pos_mul_preserves_right ℝ] [has_lt_sub_neg ℝ] [has_mul_assoc ℝ] [has_nonneg_mul_nonneg_is_nonneg ℝ] [has_pos_mul_neg_is_neg ℝ] [has_right_add_distributivity ℝ] [has_right_inv_pos_lt ℝ] [has_right_sub_distributivity ℝ] [has_right_unit ℝ] [has_squared_le_monotonic ℝ] [has_sub_add_sub_cancel ℝ] [has_sub_self_is_zero ℝ] [has_sub_ne_zero_of_ne ℝ] [has_sub_sub ℝ] [has_zero_mul_is_zero ℝ] [has_mul_zero_is_zero ℝ] [has_zero_left_add_cancel ℝ] [has_zero_right_add_cancel ℝ] [has_zero_sub_is_neg ℝ] [has_mul_pos ℝ] [has_inv_pos ℝ] [has_sub_pos ℝ] [has_lift_zero_same nat ℝ] [has_lift_one_same nat ℝ] [has_lift_add_comm nat ℝ] [has_lift_mul_comm nat ℝ] [has_lift_sub_comm nat ℝ] [has_lift_le_comm nat ℝ] [has_lift_lt_comm nat ℝ] [has_lift_ne_comm nat ℝ] [has_add_sub_assoc ℂ] [has_inv_mul_left_cancel_self ℂ] [has_left_sub_distributivity ℂ] [has_left_unit ℂ] [has_mul_assoc ℂ] [has_right_sub_distributivity ℂ] [has_right_unit ℂ] [has_sub_add_sub_cancel ℂ] [has_sub_self_is_zero ℂ] [has_mul_zero_is_zero ℂ] [has_zero_right_add_cancel ℂ] [has_zero_sub_is_neg ℂ] [has_lift_zero_same ℝ ℂ] [has_lift_one_same ℝ ℂ] [has_lift_add_comm ℝ ℂ] [has_lift_sub_comm ℝ ℂ] [has_lift_mul_comm ℝ ℂ] [has_lift_inv_comm ℝ ℂ] (abs_zero abs_one) (abs_add : Π w z, ℭ.abs (w + z) ≤ ℭ.abs w + ℭ.abs z) (abs_sub : Π w z, ℭ.abs (w - z) = ℭ.abs (z - w)) (abs_mul : Π w z, ℭ.abs (w * z) = ℭ.abs w * ℭ.abs z) (abs_inv : Π z, ℭ.abs (z⁻¹) = (ℭ.abs z)⁻¹) (abs_ge_zero) (pos_to_absℝ abs_monoℝ) (half ceil) (s) (σpos : 0 < ℭ.real_part s) : is_cauchy ℭ.abs (partial_pairs ℭ s) := begin let one_plus_σ_gt_one := has_add_left_lt.lt _ _ 1 σpos, rw has_zero_right_add_cancel.eq at one_plus_σ_gt_one, have abs_zeroℝ : ℭ.abs ↑(0 : ℝ) = 0, rw has_lift_zero_same.eq, refine abs_zero, have abs_oneℝ : ℭ.abs ↑(1 : ℝ) = 1, rw has_lift_one_same.eq, refine abs_one, have abs_addℝ : Π x y : ℝ, ℭ.abs ↑(x + y) ≤ ℭ.abs ↑x + ℭ.abs ↑y, intros, rw has_lift_add_comm.eq, refine abs_add _ _, have abs_subℝ : Π x y : ℝ, ℭ.abs ↑(x - y) = ℭ.abs ↑(y - x), intros, rw [has_lift_sub_comm.eq, has_lift_sub_comm.eq], refine abs_sub _ _, have abs_mulℝ : Π x y : ℝ, ℭ.abs ↑(x * y) = ℭ.abs ↑x * ℭ.abs ↑y, intros, rw has_lift_mul_comm.eq, refine abs_mul _ _, have abs_invℝ : Π x : ℝ, ℭ.abs ↑(x⁻¹) = (ℭ.abs ↑x)⁻¹, intros, rw has_lift_inv_comm.eq, refine abs_inv _, refine is_cauchy.comparison _ pos_to_absℝ _ abs_zero abs_add abs_ge_zero _ _ (term_pair.bounded_by_riemann_zeta _ abs_mul abs_sub abs_inv abs_add pos_to_absℝ _) (is_cauchy.closed.partial_sum_right_mul _ abs_mulℝ _ _ (term_pair.riemann_zeta_bound.abs_positive _ pos_to_absℝ _) (is_cauchy.closed.scaled_sequence_partial_sum _ abs_monoℝ _ (riemann_zeta.term.on_reals.nonneg _ _) _ (nat.zero_lt_one_add _) (riemann_zeta.partial.on_reals.is_cauchy _ _ abs_zeroℝ abs_oneℝ abs_addℝ abs_subℝ abs_mulℝ abs_invℝ (λ _, abs_ge_zero _) pos_to_absℝ half ceil _ one_plus_σ_gt_one))), end end dirichlet_eta --—————————————————————————————————————————————————————————————————————-- end riemann_hypothesis --————————————————————————————————————————————————————————————————--
90263afd95a98eb2e21aa701fc3a239755d7ce97
4727251e0cd73359b15b664c3170e5d754078599
/src/linear_algebra/projection.lean
5d6657a36b8fe3292c9760c911e4e7dc1699d321
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
13,656
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 linear_algebra.quotient import linear_algebra.prod /-! # Projection to a subspace In this file we define * `linear_proj_of_is_compl (p q : submodule R E) (h : is_compl p q)`: the projection of a module `E` to a submodule `p` along its complement `q`; it is the unique linear map `f : E → p` such that `f x = x` for `x ∈ p` and `f x = 0` for `x ∈ q`. * `is_compl_equiv_proj p`: equivalence between submodules `q` such that `is_compl p q` and projections `f : E → p`, `∀ x ∈ p, f x = x`. We also provide some lemmas justifying correctness of our definitions. ## Tags projection, complement subspace -/ 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] (p q : submodule R E) noncomputable theory namespace linear_map variable {p} open submodule lemma ker_id_sub_eq_of_proj {f : E →ₗ[R] p} (hf : ∀ x : p, f x = x) : ker (id - p.subtype.comp f) = p := begin ext x, simp only [comp_apply, mem_ker, subtype_apply, sub_apply, id_apply, sub_eq_zero], exact ⟨λ h, h.symm ▸ submodule.coe_mem _, λ hx, by erw [hf ⟨x, hx⟩, subtype.coe_mk]⟩ end lemma range_eq_of_proj {f : E →ₗ[R] p} (hf : ∀ x : p, f x = x) : range f = ⊤ := range_eq_top.2 $ λ x, ⟨x, hf x⟩ lemma is_compl_of_proj {f : E →ₗ[R] p} (hf : ∀ x : p, f x = x) : is_compl p f.ker := begin split, { rintros x ⟨hpx, hfx⟩, erw [set_like.mem_coe, mem_ker, hf ⟨x, hpx⟩, mk_eq_zero] at hfx, simp only [hfx, set_like.mem_coe, zero_mem] }, { intros x hx, rw [mem_sup'], refine ⟨f x, ⟨x - f x, _⟩, add_sub_cancel'_right _ _⟩, rw [mem_ker, linear_map.map_sub, hf, sub_self] } end end linear_map namespace submodule open linear_map /-- If `q` is a complement of `p`, then `M/p ≃ q`. -/ def quotient_equiv_of_is_compl (h : is_compl p q) : (E ⧸ p) ≃ₗ[R] q := linear_equiv.symm $ linear_equiv.of_bijective (p.mkq.comp q.subtype) (by simp only [← ker_eq_bot, ker_comp, ker_mkq, disjoint_iff_comap_eq_bot.1 h.symm.disjoint]) (by simp only [← range_eq_top, range_comp, range_subtype, map_mkq_eq_top, h.sup_eq_top]) @[simp] lemma quotient_equiv_of_is_compl_symm_apply (h : is_compl p q) (x : q) : (quotient_equiv_of_is_compl p q h).symm x = quotient.mk x := rfl @[simp] lemma quotient_equiv_of_is_compl_apply_mk_coe (h : is_compl p q) (x : q) : quotient_equiv_of_is_compl p q h (quotient.mk x) = x := (quotient_equiv_of_is_compl p q h).apply_symm_apply x @[simp] lemma mk_quotient_equiv_of_is_compl_apply (h : is_compl p q) (x : E ⧸ p) : (quotient.mk (quotient_equiv_of_is_compl p q h x) : E ⧸ p) = x := (quotient_equiv_of_is_compl p q h).symm_apply_apply x /-- If `q` is a complement of `p`, then `p × q` is isomorphic to `E`. It is the unique linear map `f : E → p` such that `f x = x` for `x ∈ p` and `f x = 0` for `x ∈ q`. -/ def prod_equiv_of_is_compl (h : is_compl p q) : (p × q) ≃ₗ[R] E := begin apply linear_equiv.of_bijective (p.subtype.coprod q.subtype), { simp only [←ker_eq_bot, ker_eq_bot', prod.forall, subtype_apply, prod.mk_eq_zero, coprod_apply], -- TODO: if I add `submodule.forall`, it unfolds the outer `∀` but not the inner one. rintros ⟨x, hx⟩ ⟨y, hy⟩, simp only [coe_mk, mk_eq_zero, ← eq_neg_iff_add_eq_zero], rintro rfl, rw [neg_mem_iff] at hx, simp [disjoint_def.1 h.disjoint y hx hy] }, { rw [← range_eq_top, ← sup_eq_range, h.sup_eq_top] } end @[simp] lemma coe_prod_equiv_of_is_compl (h : is_compl p q) : (prod_equiv_of_is_compl p q h : (p × q) →ₗ[R] E) = p.subtype.coprod q.subtype := rfl @[simp] lemma coe_prod_equiv_of_is_compl' (h : is_compl p q) (x : p × q) : prod_equiv_of_is_compl p q h x = x.1 + x.2 := rfl @[simp] lemma prod_equiv_of_is_compl_symm_apply_left (h : is_compl p q) (x : p) : (prod_equiv_of_is_compl p q h).symm x = (x, 0) := (prod_equiv_of_is_compl p q h).symm_apply_eq.2 $ by simp @[simp] lemma prod_equiv_of_is_compl_symm_apply_right (h : is_compl p q) (x : q) : (prod_equiv_of_is_compl p q h).symm x = (0, x) := (prod_equiv_of_is_compl p q h).symm_apply_eq.2 $ by simp @[simp] lemma prod_equiv_of_is_compl_symm_apply_fst_eq_zero (h : is_compl p q) {x : E} : ((prod_equiv_of_is_compl p q h).symm x).1 = 0 ↔ x ∈ q := begin conv_rhs { rw [← (prod_equiv_of_is_compl p q h).apply_symm_apply x] }, rw [coe_prod_equiv_of_is_compl', submodule.add_mem_iff_left _ (submodule.coe_mem _), mem_right_iff_eq_zero_of_disjoint h.disjoint] end @[simp] lemma prod_equiv_of_is_compl_symm_apply_snd_eq_zero (h : is_compl p q) {x : E} : ((prod_equiv_of_is_compl p q h).symm x).2 = 0 ↔ x ∈ p := begin conv_rhs { rw [← (prod_equiv_of_is_compl p q h).apply_symm_apply x] }, rw [coe_prod_equiv_of_is_compl', submodule.add_mem_iff_right _ (submodule.coe_mem _), mem_left_iff_eq_zero_of_disjoint h.disjoint] end @[simp] lemma prod_comm_trans_prod_equiv_of_is_compl (h : is_compl p q) : linear_equiv.prod_comm R q p ≪≫ₗ prod_equiv_of_is_compl p q h = prod_equiv_of_is_compl q p h.symm := linear_equiv.ext $ λ _, add_comm _ _ /-- Projection to a submodule along its complement. -/ def linear_proj_of_is_compl (h : is_compl p q) : E →ₗ[R] p := (linear_map.fst R p q) ∘ₗ ↑(prod_equiv_of_is_compl p q h).symm variables {p q} @[simp] lemma linear_proj_of_is_compl_apply_left (h : is_compl p q) (x : p) : linear_proj_of_is_compl p q h x = x := by simp [linear_proj_of_is_compl] @[simp] lemma linear_proj_of_is_compl_range (h : is_compl p q) : (linear_proj_of_is_compl p q h).range = ⊤ := range_eq_of_proj (linear_proj_of_is_compl_apply_left h) @[simp] lemma linear_proj_of_is_compl_apply_eq_zero_iff (h : is_compl p q) {x : E} : linear_proj_of_is_compl p q h x = 0 ↔ x ∈ q:= by simp [linear_proj_of_is_compl] lemma linear_proj_of_is_compl_apply_right' (h : is_compl p q) (x : E) (hx : x ∈ q) : linear_proj_of_is_compl p q h x = 0 := (linear_proj_of_is_compl_apply_eq_zero_iff h).2 hx @[simp] lemma linear_proj_of_is_compl_apply_right (h : is_compl p q) (x : q) : linear_proj_of_is_compl p q h x = 0 := linear_proj_of_is_compl_apply_right' h x x.2 @[simp] lemma linear_proj_of_is_compl_ker (h : is_compl p q) : (linear_proj_of_is_compl p q h).ker = q := ext $ λ x, mem_ker.trans (linear_proj_of_is_compl_apply_eq_zero_iff h) lemma linear_proj_of_is_compl_comp_subtype (h : is_compl p q) : (linear_proj_of_is_compl p q h).comp p.subtype = id := linear_map.ext $ linear_proj_of_is_compl_apply_left h lemma linear_proj_of_is_compl_idempotent (h : is_compl p q) (x : E) : linear_proj_of_is_compl p q h (linear_proj_of_is_compl p q h x) = linear_proj_of_is_compl p q h x := linear_proj_of_is_compl_apply_left h _ lemma exists_unique_add_of_is_compl_prod (hc : is_compl p q) (x : E) : ∃! (u : p × q), (u.fst : E) + u.snd = x := (prod_equiv_of_is_compl _ _ hc).to_equiv.bijective.exists_unique _ lemma exists_unique_add_of_is_compl (hc : is_compl p q) (x : E) : ∃ (u : p) (v : q), ((u : E) + v = x ∧ ∀ (r : p) (s : q), (r : E) + s = x → r = u ∧ s = v) := let ⟨u, hu₁, hu₂⟩ := exists_unique_add_of_is_compl_prod hc x in ⟨u.1, u.2, hu₁, λ r s hrs, prod.eq_iff_fst_eq_snd_eq.1 (hu₂ ⟨r, s⟩ hrs)⟩ lemma linear_proj_add_linear_proj_of_is_compl_eq_self (hpq : is_compl p q) (x : E) : (p.linear_proj_of_is_compl q hpq x + q.linear_proj_of_is_compl p hpq.symm x : E) = x := begin dunfold linear_proj_of_is_compl, rw ←prod_comm_trans_prod_equiv_of_is_compl _ _ hpq, exact (prod_equiv_of_is_compl _ _ hpq).apply_symm_apply x, end end submodule namespace linear_map open submodule /-- Given linear maps `φ` and `ψ` from complement submodules, `of_is_compl` is the induced linear map over the entire module. -/ def of_is_compl {p q : submodule R E} (h : is_compl p q) (φ : p →ₗ[R] F) (ψ : q →ₗ[R] F) : E →ₗ[R] F := (linear_map.coprod φ ψ) ∘ₗ ↑(submodule.prod_equiv_of_is_compl _ _ h).symm variables {p q} @[simp] lemma of_is_compl_left_apply (h : is_compl p q) {φ : p →ₗ[R] F} {ψ : q →ₗ[R] F} (u : p) : of_is_compl h φ ψ (u : E) = φ u := by simp [of_is_compl] @[simp] lemma of_is_compl_right_apply (h : is_compl p q) {φ : p →ₗ[R] F} {ψ : q →ₗ[R] F} (v : q) : of_is_compl h φ ψ (v : E) = ψ v := by simp [of_is_compl] lemma of_is_compl_eq (h : is_compl p q) {φ : p →ₗ[R] F} {ψ : q →ₗ[R] F} {χ : E →ₗ[R] F} (hφ : ∀ u, φ u = χ u) (hψ : ∀ u, ψ u = χ u) : of_is_compl h φ ψ = χ := begin ext x, obtain ⟨_, _, rfl, _⟩ := exists_unique_add_of_is_compl h x, simp [of_is_compl, hφ, hψ] end lemma of_is_compl_eq' (h : is_compl p q) {φ : p →ₗ[R] F} {ψ : q →ₗ[R] F} {χ : E →ₗ[R] F} (hφ : φ = χ.comp p.subtype) (hψ : ψ = χ.comp q.subtype) : of_is_compl h φ ψ = χ := of_is_compl_eq h (λ _, hφ.symm ▸ rfl) (λ _, hψ.symm ▸ rfl) @[simp] lemma of_is_compl_zero (h : is_compl p q) : (of_is_compl h 0 0 : E →ₗ[R] F) = 0 := of_is_compl_eq _ (λ _, rfl) (λ _, rfl) @[simp] lemma of_is_compl_add (h : is_compl p q) {φ₁ φ₂ : p →ₗ[R] F} {ψ₁ ψ₂ : q →ₗ[R] F} : of_is_compl h (φ₁ + φ₂) (ψ₁ + ψ₂) = of_is_compl h φ₁ ψ₁ + of_is_compl h φ₂ ψ₂ := of_is_compl_eq _ (by simp) (by simp) @[simp] lemma of_is_compl_smul {R : Type*} [comm_ring R] {E : Type*} [add_comm_group E] [module R E] {F : Type*} [add_comm_group F] [module R F] {p q : submodule R E} (h : is_compl p q) {φ : p →ₗ[R] F} {ψ : q →ₗ[R] F} (c : R) : of_is_compl h (c • φ) (c • ψ) = c • of_is_compl h φ ψ := of_is_compl_eq _ (by simp) (by simp) section variables {R₁ : Type*} [comm_ring R₁] [module R₁ E] [module R₁ F] /-- The linear map from `(p →ₗ[R₁] F) × (q →ₗ[R₁] F)` to `E →ₗ[R₁] F`. -/ def of_is_compl_prod {p q : submodule R₁ E} (h : is_compl p q) : ((p →ₗ[R₁] F) × (q →ₗ[R₁] F)) →ₗ[R₁] (E →ₗ[R₁] F) := { to_fun := λ φ, of_is_compl h φ.1 φ.2, map_add' := by { intros φ ψ, rw [prod.snd_add, prod.fst_add, of_is_compl_add] }, map_smul' := by { intros c φ, simp [prod.smul_snd, prod.smul_fst, of_is_compl_smul] } } @[simp] lemma of_is_compl_prod_apply {p q : submodule R₁ E} (h : is_compl p q) (φ : (p →ₗ[R₁] F) × (q →ₗ[R₁] F)) : of_is_compl_prod h φ = of_is_compl h φ.1 φ.2 := rfl /-- The natural linear equivalence between `(p →ₗ[R₁] F) × (q →ₗ[R₁] F)` and `E →ₗ[R₁] F`. -/ def of_is_compl_prod_equiv {p q : submodule R₁ E} (h : is_compl p q) : ((p →ₗ[R₁] F) × (q →ₗ[R₁] F)) ≃ₗ[R₁] (E →ₗ[R₁] F) := { inv_fun := λ φ, ⟨φ.dom_restrict p, φ.dom_restrict q⟩, left_inv := begin intros φ, ext, { exact of_is_compl_left_apply h x }, { exact of_is_compl_right_apply h x } end, right_inv := begin intro φ, ext, obtain ⟨a, b, hab, _⟩ := exists_unique_add_of_is_compl h x, rw [← hab], simp, end, .. of_is_compl_prod h } end @[simp] lemma linear_proj_of_is_compl_of_proj (f : E →ₗ[R] p) (hf : ∀ x : p, f x = x) : p.linear_proj_of_is_compl f.ker (is_compl_of_proj hf) = f := begin ext x, have : x ∈ p ⊔ f.ker, { simp only [(is_compl_of_proj hf).sup_eq_top, mem_top] }, rcases mem_sup'.1 this with ⟨x, y, rfl⟩, simp [hf] end /-- If `f : E →ₗ[R] F` and `g : E →ₗ[R] G` are two surjective linear maps and their kernels are complement of each other, then `x ↦ (f x, g x)` defines a linear equivalence `E ≃ₗ[R] F × G`. -/ def equiv_prod_of_surjective_of_is_compl (f : E →ₗ[R] F) (g : E →ₗ[R] G) (hf : f.range = ⊤) (hg : g.range = ⊤) (hfg : is_compl f.ker g.ker) : E ≃ₗ[R] F × G := linear_equiv.of_bijective (f.prod g) (by simp [← ker_eq_bot, hfg.inf_eq_bot]) (by simp [← range_eq_top, range_prod_eq hfg.sup_eq_top, *]) @[simp] lemma coe_equiv_prod_of_surjective_of_is_compl {f : E →ₗ[R] F} {g : E →ₗ[R] G} (hf : f.range = ⊤) (hg : g.range = ⊤) (hfg : is_compl f.ker g.ker) : (equiv_prod_of_surjective_of_is_compl f g hf hg hfg : E →ₗ[R] F × G) = f.prod g := rfl @[simp] lemma equiv_prod_of_surjective_of_is_compl_apply {f : E →ₗ[R] F} {g : E →ₗ[R] G} (hf : f.range = ⊤) (hg : g.range = ⊤) (hfg : is_compl f.ker g.ker) (x : E): equiv_prod_of_surjective_of_is_compl f g hf hg hfg x = (f x, g x) := rfl end linear_map namespace submodule open linear_map /-- Equivalence between submodules `q` such that `is_compl p q` and linear maps `f : E →ₗ[R] p` such that `∀ x : p, f x = x`. -/ def is_compl_equiv_proj : {q // is_compl p q} ≃ {f : E →ₗ[R] p // ∀ x : p, f x = x} := { to_fun := λ q, ⟨linear_proj_of_is_compl p q q.2, linear_proj_of_is_compl_apply_left q.2⟩, inv_fun := λ f, ⟨(f : E →ₗ[R] p).ker, is_compl_of_proj f.2⟩, left_inv := λ ⟨q, hq⟩, by simp only [linear_proj_of_is_compl_ker, subtype.coe_mk], right_inv := λ ⟨f, hf⟩, subtype.eq $ f.linear_proj_of_is_compl_of_proj hf } @[simp] lemma coe_is_compl_equiv_proj_apply (q : {q // is_compl p q}) : (p.is_compl_equiv_proj q : E →ₗ[R] p) = linear_proj_of_is_compl p q q.2 := rfl @[simp] lemma coe_is_compl_equiv_proj_symm_apply (f : {f : E →ₗ[R] p // ∀ x : p, f x = x}) : (p.is_compl_equiv_proj.symm f : submodule R E) = (f : E →ₗ[R] p).ker := rfl end submodule
a73580685c980bd30eaa5e778a1f9c5cab7233c7
75db7e3219bba2fbf41bf5b905f34fcb3c6ca3f2
/hott/homotopy/susp.hlean
6d27a32f3e808c4ecacb37e691daab12d4af79ee
[ "Apache-2.0" ]
permissive
jroesch/lean
30ef0860fa905d35b9ad6f76de1a4f65c9af6871
3de4ec1a6ce9a960feb2a48eeea8b53246fa34f2
refs/heads/master
1,586,090,835,348
1,455,142,203,000
1,455,142,277,000
51,536,958
1
0
null
1,455,215,811,000
1,455,215,811,000
null
UTF-8
Lean
false
false
7,550
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 Declaration of suspension -/ import hit.pushout types.pointed cubical.square open pushout unit eq equiv equiv.ops definition susp (A : Type) : Type := pushout (λ(a : A), star) (λ(a : A), star) namespace susp variable {A : Type} definition north {A : Type} : susp A := inl star definition south {A : Type} : susp A := inr star definition merid (a : A) : @north A = @south A := glue a protected definition rec {P : susp A → Type} (PN : P north) (PS : P south) (Pm : Π(a : A), PN =[merid a] PS) (x : susp A) : P x := begin induction x with u u, { cases u, exact PN}, { cases u, exact PS}, { apply Pm}, end protected definition rec_on [reducible] {P : susp A → Type} (y : susp A) (PN : P north) (PS : P south) (Pm : Π(a : A), PN =[merid a] PS) : P y := susp.rec PN PS Pm y theorem rec_merid {P : susp A → Type} (PN : P north) (PS : P south) (Pm : Π(a : A), PN =[merid a] PS) (a : A) : apdo (susp.rec PN PS Pm) (merid a) = Pm a := !rec_glue protected definition elim {P : Type} (PN : P) (PS : P) (Pm : A → PN = PS) (x : susp A) : P := susp.rec PN PS (λa, pathover_of_eq (Pm a)) x protected definition elim_on [reducible] {P : Type} (x : susp A) (PN : P) (PS : P) (Pm : A → PN = PS) : P := susp.elim PN PS Pm x theorem elim_merid {P : Type} {PN PS : P} (Pm : A → PN = PS) (a : A) : ap (susp.elim PN PS Pm) (merid a) = Pm a := begin apply eq_of_fn_eq_fn_inv !(pathover_constant (merid a)), rewrite [▸*,-apdo_eq_pathover_of_eq_ap,↑susp.elim,rec_merid], end protected definition elim_type (PN : Type) (PS : Type) (Pm : A → PN ≃ PS) (x : susp A) : Type := susp.elim PN PS (λa, ua (Pm a)) x protected definition elim_type_on [reducible] (x : susp A) (PN : Type) (PS : Type) (Pm : A → PN ≃ PS) : Type := susp.elim_type PN PS Pm x theorem elim_type_merid (PN : Type) (PS : Type) (Pm : A → PN ≃ PS) (a : A) : transport (susp.elim_type PN PS Pm) (merid a) = Pm a := by rewrite [tr_eq_cast_ap_fn,↑susp.elim_type,elim_merid];apply cast_ua_fn end susp attribute susp.north susp.south [constructor] attribute susp.rec susp.elim [unfold 6] [recursor 6] attribute susp.elim_type [unfold 5] attribute susp.rec_on susp.elim_on [unfold 3] attribute susp.elim_type_on [unfold 2] namespace susp open pointed variables {X Y Z : Pointed} definition pointed_susp [instance] [constructor] (X : Type) : pointed (susp X) := pointed.mk north definition Susp [constructor] (X : Type) : Pointed := pointed.mk' (susp X) definition Susp_functor (f : X →* Y) : Susp X →* Susp Y := begin fconstructor, { intro x, induction x, apply north, apply south, exact merid (f a)}, { reflexivity} end definition Susp_functor_compose (g : Y →* Z) (f : X →* Y) : Susp_functor (g ∘* f) ~* Susp_functor g ∘* Susp_functor f := begin fconstructor, { intro a, induction a, { reflexivity}, { reflexivity}, { apply eq_pathover, apply hdeg_square, rewrite [▸*,ap_compose' _ (Susp_functor f),↑Susp_functor,+elim_merid]}}, { reflexivity} end -- adjunction from Coq-HoTT definition loop_susp_unit [constructor] (X : Pointed) : X →* Ω(Susp X) := begin fconstructor, { intro x, exact merid x ⬝ (merid pt)⁻¹}, { apply con.right_inv}, end definition loop_susp_unit_natural (f : X →* Y) : loop_susp_unit Y ∘* f ~* ap1 (Susp_functor f) ∘* loop_susp_unit X := begin induction X with X x, induction Y with Y y, induction f with f pf, esimp at *, induction pf, fconstructor, { intro x', esimp [Susp_functor], symmetry, exact !idp_con ⬝ (!ap_con ⬝ whisker_left _ !ap_inv) ⬝ (!elim_merid ◾ (inverse2 !elim_merid)) }, { rewrite [▸*,idp_con (con.right_inv _)], apply inv_con_eq_of_eq_con, refine _ ⬝ !con.assoc', rewrite inverse2_right_inv, refine _ ⬝ !con.assoc', rewrite [ap_con_right_inv], unfold Susp_functor, xrewrite [idp_con_idp,-ap_compose], }, end definition loop_susp_counit [constructor] (X : Pointed) : Susp (Ω X) →* X := begin fconstructor, { intro x, induction x, exact pt, exact pt, exact a}, { reflexivity}, end definition loop_susp_counit_natural (f : X →* Y) : f ∘* loop_susp_counit X ~* loop_susp_counit Y ∘* (Susp_functor (ap1 f)) := begin induction X with X x, induction Y with Y y, induction f with f pf, esimp at *, induction pf, fconstructor, { intro x', induction x' with p, { reflexivity}, { reflexivity}, { esimp, apply eq_pathover, apply hdeg_square, xrewrite [ap_compose f,ap_compose (susp.elim (f x) (f x) (λ (a : f x = f x), a)),▸*], xrewrite [+elim_merid,▸*,idp_con]}}, { reflexivity} end definition loop_susp_counit_unit (X : Pointed) : ap1 (loop_susp_counit X) ∘* loop_susp_unit (Ω X) ~* pid (Ω X) := begin induction X with X x, fconstructor, { intro p, esimp, refine !idp_con ⬝ (!ap_con ⬝ whisker_left _ !ap_inv) ⬝ (!elim_merid ◾ inverse2 !elim_merid)}, { rewrite [▸*,inverse2_right_inv (elim_merid id idp)], refine !con.assoc ⬝ _, xrewrite [ap_con_right_inv (susp.elim x x (λa, a)) (merid idp),idp_con_idp,-ap_compose]} end definition loop_susp_unit_counit (X : Pointed) : loop_susp_counit (Susp X) ∘* Susp_functor (loop_susp_unit X) ~* pid (Susp X) := begin induction X with X x, fconstructor, { intro x', induction x', { reflexivity}, { exact merid pt}, { apply eq_pathover, xrewrite [▸*, ap_id, ap_compose (susp.elim north north (λa, a)), +elim_merid,▸*], apply square_of_eq, exact !idp_con ⬝ !inv_con_cancel_right⁻¹}}, { reflexivity} end definition susp_adjoint_loop (X Y : Pointed) : map₊ (pointed.mk' (susp X)) Y ≃ map₊ X (Ω Y) := begin fapply equiv.MK, { intro f, exact ap1 f ∘* loop_susp_unit X}, { intro g, exact loop_susp_counit Y ∘* Susp_functor g}, { intro g, apply eq_of_phomotopy, esimp, refine !pwhisker_right !ap1_compose ⬝* _, refine !passoc ⬝* _, refine !pwhisker_left !loop_susp_unit_natural⁻¹* ⬝* _, refine !passoc⁻¹* ⬝* _, refine !pwhisker_right !loop_susp_counit_unit ⬝* _, apply pid_comp}, { intro f, apply eq_of_phomotopy, esimp, refine !pwhisker_left !Susp_functor_compose ⬝* _, refine !passoc⁻¹* ⬝* _, refine !pwhisker_right !loop_susp_counit_natural⁻¹* ⬝* _, refine !passoc ⬝* _, refine !pwhisker_left !loop_susp_unit_counit ⬝* _, apply comp_pid}, end definition susp_adjoint_loop_nat_right (f : Susp X →* Y) (g : Y →* Z) : susp_adjoint_loop X Z (g ∘* f) ~* ap1 g ∘* susp_adjoint_loop X Y f := begin esimp [susp_adjoint_loop], refine _ ⬝* !passoc, apply pwhisker_right, apply ap1_compose end definition susp_adjoint_loop_nat_left (f : Y →* Ω Z) (g : X →* Y) : (susp_adjoint_loop X Z)⁻¹ (f ∘* g) ~* (susp_adjoint_loop Y Z)⁻¹ f ∘* Susp_functor g := begin esimp [susp_adjoint_loop], refine _ ⬝* !passoc⁻¹*, apply pwhisker_left, apply Susp_functor_compose end end susp
cfb7fdf5978acc143727b177ad12ebab2769f127
f5373ccdc976e6390397d9f4220a74c76f706f4a
/src/examples/parser_entry.lean
638d208b183d4390411d6d381a8d5bece73f3710
[]
no_license
jasonrute/lean_gym_prototype
fcd91fdc454f9e351bbe258c765f50276407547e
ab29624d14e4e069e15afe0b1d90248b5b394b86
refs/heads/master
1,682,628,526,780
1,590,539,315,000
1,590,539,315,000
264,938,525
3
0
null
null
null
null
UTF-8
Lean
false
false
795
lean
import lean_gym.server open lean.parser open interactive -- set up server meta def json_config : json_server lean_server_request lean_server_response := { read_write := io_streams.stdin_stdout_streams, get_json := json_server.get_custom_json, -- use custom format since faster put_json := json_server.put_standard_json, -- use standard format } @[user_command] meta def run_lean_gym_server_with_goal_cmd (meta_info : interactive.decl_meta_info) (_ : interactive.parse (tk "run_lean_gym_server_with_goal")) : lean.parser unit := do goal <- interactive.types.texpr, result <- lean_gym.run_server_from_parser json_config goal, match result with | except.ok (some p) := return () | _ := return () end, return () . run_lean_gym_server_with_goal 1 + 1 = 2 . -- will succeed
32da94ba9106b2a956533137827a0a64311abbaf
302c785c90d40ad3d6be43d33bc6a558354cc2cf
/src/order/rel_iso.lean
f2e505a5dba56942c67c428a849227e502b7562a
[ "Apache-2.0" ]
permissive
ilitzroth/mathlib
ea647e67f1fdfd19a0f7bdc5504e8acec6180011
5254ef14e3465f6504306132fe3ba9cec9ffff16
refs/heads/master
1,680,086,661,182
1,617,715,647,000
1,617,715,647,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
31,277
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 logic.embedding import order.rel_classes import data.set.intervals.basic open function universes u v w variables {α : Type*} {β : Type*} {γ : Type*} {r : α → α → Prop} {s : β → β → Prop} {t : γ → γ → Prop} /-- A relation homomorphism with respect to a given pair of relations `r` and `s` is a function `f : α → β` such that `r a b → s (f a) (f b)`. -/ @[nolint has_inhabited_instance] structure rel_hom {α β : Type*} (r : α → α → Prop) (s : β → β → Prop) := (to_fun : α → β) (map_rel' : ∀ {a b}, r a b → s (to_fun a) (to_fun b)) infix ` →r `:25 := rel_hom namespace rel_hom instance : has_coe_to_fun (r →r s) := ⟨λ _, α → β, λ o, o.to_fun⟩ theorem map_rel (f : r →r s) : ∀ {a b}, r a b → s (f a) (f b) := f.map_rel' @[simp] theorem coe_fn_mk (f : α → β) (o) : (@rel_hom.mk _ _ r s f o : α → β) = f := rfl @[simp] theorem coe_fn_to_fun (f : r →r s) : (f.to_fun : α → β) = f := rfl /-- The map `coe_fn : (r →r s) → (α → β)` is injective. We can't use `function.injective` here but mimic its signature by using `⦃e₁ e₂⦄`. -/ theorem coe_fn_inj : ∀ ⦃e₁ e₂ : r →r s⦄, (e₁ : α → β) = e₂ → e₁ = e₂ | ⟨f₁, o₁⟩ ⟨f₂, o₂⟩ h := by { congr, exact h } @[ext] theorem ext ⦃f g : r →r s⦄ (h : ∀ x, f x = g x) : f = g := coe_fn_inj (funext h) theorem ext_iff {f g : r →r s} : f = g ↔ ∀ x, f x = g x := ⟨λ h x, h ▸ rfl, λ h, ext h⟩ /-- Identity map is a relation homomorphism. -/ @[refl] protected def id (r : α → α → Prop) : r →r r := ⟨id, λ a b, id⟩ /-- Composition of two relation homomorphisms is a relation homomorphism. -/ @[trans] protected def comp (g : s →r t) (f : r →r s) : r →r t := ⟨g.1 ∘ f.1, λ a b h, g.2 (f.2 h)⟩ @[simp] theorem id_apply (x : α) : rel_hom.id r x = x := rfl @[simp] theorem comp_apply (g : s →r t) (f : r →r s) (a : α) : (g.comp f) a = g (f a) := rfl /-- A relation homomorphism is also a relation homomorphism between dual relations. -/ protected def swap (f : r →r s) : swap r →r swap s := ⟨f, λ a b, f.map_rel⟩ /-- A function is a relation homomorphism from the preimage relation of `s` to `s`. -/ def preimage (f : α → β) (s : β → β → Prop) : f ⁻¹'o s →r s := ⟨f, λ a b, id⟩ protected theorem is_irrefl : ∀ (f : r →r s) [is_irrefl β s], is_irrefl α r | ⟨f, o⟩ ⟨H⟩ := ⟨λ a h, H _ (o h)⟩ protected theorem is_asymm : ∀ (f : r →r s) [is_asymm β s], is_asymm α r | ⟨f, o⟩ ⟨H⟩ := ⟨λ a b h₁ h₂, H _ _ (o h₁) (o h₂)⟩ protected theorem acc (f : r →r s) (a : α) : acc s (f a) → acc r a := begin generalize h : f a = b, intro ac, induction ac with _ H IH generalizing a, subst h, exact ⟨_, λ a' h, IH (f a') (f.map_rel h) _ rfl⟩ end protected theorem well_founded : ∀ (f : r →r s) (h : well_founded s), well_founded r | f ⟨H⟩ := ⟨λ a, f.acc _ (H _)⟩ lemma map_inf {α β : Type*} [semilattice_inf α] [linear_order β] (a : ((<) : β → β → Prop) →r ((<) : α → α → Prop)) (m n : β) : a (m ⊓ n) = a m ⊓ a n := begin symmetry, cases le_or_lt n m with h, { rw [inf_eq_right.mpr h, inf_eq_right], exact strict_mono.monotone (λ x y, a.map_rel) h, }, { rw [inf_eq_left.mpr (le_of_lt h), inf_eq_left], exact le_of_lt (a.map_rel h), }, end lemma map_sup {α β : Type*} [semilattice_sup α] [linear_order β] (a : ((>) : β → β → Prop) →r ((>) : α → α → Prop)) (m n : β) : a (m ⊔ n) = a m ⊔ a n := begin symmetry, cases le_or_lt m n with h, { rw [sup_eq_right.mpr h, sup_eq_right], exact strict_mono.monotone (λ x y, a.swap.map_rel) h, }, { rw [sup_eq_left.mpr (le_of_lt h), sup_eq_left], exact le_of_lt (a.map_rel h), }, end end rel_hom /-- An increasing function is injective -/ lemma injective_of_increasing (r : α → α → Prop) (s : β → β → Prop) [is_trichotomous α r] [is_irrefl β s] (f : α → β) (hf : ∀{x y}, r x y → s (f x) (f y)) : injective f := begin intros x y hxy, rcases trichotomous_of r x y with h | h | h, have := hf h, rw hxy at this, exfalso, exact irrefl_of s (f y) this, exact h, have := hf h, rw hxy at this, exfalso, exact irrefl_of s (f y) this end /-- An increasing function is injective -/ lemma rel_hom.injective_of_increasing [is_trichotomous α r] [is_irrefl β s] (f : r →r s) : injective f := injective_of_increasing r s f (λ x y, f.map_rel) theorem surjective.well_founded_iff {f : α → β} (hf : surjective f) (o : ∀ {a b}, r a b ↔ s (f a) (f b)) : well_founded r ↔ well_founded s := iff.intro (begin apply rel_hom.well_founded, refine rel_hom.mk _ _, {exact classical.some hf.has_right_inverse}, intros a b h, apply o.2, convert h, iterate 2 { apply classical.some_spec hf.has_right_inverse }, end) (rel_hom.well_founded ⟨f, λ _ _, o.1⟩) /-- A relation embedding with respect to a given pair of relations `r` and `s` is an embedding `f : α ↪ β` such that `r a b ↔ s (f a) (f b)`. -/ structure rel_embedding {α β : Type*} (r : α → α → Prop) (s : β → β → Prop) extends α ↪ β := (map_rel_iff' : ∀ {a b}, s (to_embedding a) (to_embedding b) ↔ r a b) infix ` ↪r `:25 := rel_embedding /-- An order embedding is an embedding `f : α ↪ β` such that `a ≤ b ↔ (f a) ≤ (f b)`. This definition is an abbreviation of `rel_embedding (≤) (≤)`. -/ abbreviation order_embedding (α β : Type*) [has_le α] [has_le β] := @rel_embedding α β (≤) (≤) infix ` ↪o `:25 := order_embedding /-- The induced relation on a subtype is an embedding under the natural inclusion. -/ definition subtype.rel_embedding {X : Type*} (r : X → X → Prop) (p : X → Prop) : ((subtype.val : subtype p → X) ⁻¹'o r) ↪r r := ⟨embedding.subtype p, λ x y, iff.rfl⟩ theorem preimage_equivalence {α β} (f : α → β) {s : β → β → Prop} (hs : equivalence s) : equivalence (f ⁻¹'o s) := ⟨λ a, hs.1 _, λ a b h, hs.2.1 h, λ a b c h₁ h₂, hs.2.2 h₁ h₂⟩ namespace rel_embedding /-- A relation embedding is also a relation homomorphism -/ def to_rel_hom (f : r ↪r s) : (r →r s) := { to_fun := f.to_embedding.to_fun, map_rel' := λ x y, (map_rel_iff' f).mpr } instance : has_coe (r ↪r s) (r →r s) := ⟨to_rel_hom⟩ -- see Note [function coercion] instance : has_coe_to_fun (r ↪r s) := ⟨λ _, α → β, λ o, o.to_embedding⟩ @[simp] lemma to_rel_hom_eq_coe (f : r ↪r s) : f.to_rel_hom = f := rfl @[simp] lemma coe_coe_fn (f : r ↪r s) : ((f : r →r s) : α → β) = f := rfl theorem injective (f : r ↪r s) : injective f := f.inj' theorem map_rel_iff (f : r ↪r s) : ∀ {a b}, s (f a) (f b) ↔ r a b := f.map_rel_iff' @[simp] theorem coe_fn_mk (f : α ↪ β) (o) : (@rel_embedding.mk _ _ r s f o : α → β) = f := rfl @[simp] theorem coe_fn_to_embedding (f : r ↪r s) : (f.to_embedding : α → β) = f := rfl /-- The map `coe_fn : (r ↪r s) → (α → β)` is injective. We can't use `function.injective` here but mimic its signature by using `⦃e₁ e₂⦄`. -/ theorem coe_fn_inj : ∀ ⦃e₁ e₂ : r ↪r s⦄, (e₁ : α → β) = e₂ → e₁ = e₂ | ⟨⟨f₁, h₁⟩, o₁⟩ ⟨⟨f₂, h₂⟩, o₂⟩ h := by { congr, exact h } @[ext] theorem ext ⦃f g : r ↪r s⦄ (h : ∀ x, f x = g x) : f = g := coe_fn_inj (funext h) theorem ext_iff {f g : r ↪r s} : f = g ↔ ∀ x, f x = g x := ⟨λ h x, h ▸ rfl, λ h, ext h⟩ /-- Identity map is a relation embedding. -/ @[refl] protected def refl (r : α → α → Prop) : r ↪r r := ⟨embedding.refl _, λ a b, iff.rfl⟩ /-- Composition of two relation embeddings is a relation embedding. -/ @[trans] protected def trans (f : r ↪r s) (g : s ↪r t) : r ↪r t := ⟨f.1.trans g.1, λ a b, by simp [f.map_rel_iff, g.map_rel_iff]⟩ instance (r : α → α → Prop) : inhabited (r ↪r r) := ⟨rel_embedding.refl _⟩ @[simp] theorem refl_apply (x : α) : rel_embedding.refl r x = x := rfl theorem trans_apply (f : r ↪r s) (g : s ↪r t) (a : α) : (f.trans g) a = g (f a) := rfl @[simp] theorem coe_trans (f : r ↪r s) (g : s ↪r t) : ⇑(f.trans g) = g ∘ f := rfl /-- A relation embedding is also a relation embedding between dual relations. -/ protected def swap (f : r ↪r s) : swap r ↪r swap s := ⟨f.to_embedding, λ a b, f.map_rel_iff⟩ /-- If `f` is injective, then it is a relation embedding from the preimage relation of `s` to `s`. -/ def preimage (f : α ↪ β) (s : β → β → Prop) : f ⁻¹'o s ↪r s := ⟨f, λ a b, iff.rfl⟩ theorem eq_preimage (f : r ↪r s) : r = f ⁻¹'o s := by { ext a b, exact f.map_rel_iff.symm } protected theorem is_irrefl (f : r ↪r s) [is_irrefl β s] : is_irrefl α r := ⟨λ a, mt f.map_rel_iff.2 (irrefl (f a))⟩ protected theorem is_refl (f : r ↪r s) [is_refl β s] : is_refl α r := ⟨λ a, f.map_rel_iff.1 $ refl _⟩ protected theorem is_symm (f : r ↪r s) [is_symm β s] : is_symm α r := ⟨λ a b, imp_imp_imp f.map_rel_iff.2 f.map_rel_iff.1 symm⟩ protected theorem is_asymm (f : r ↪r s) [is_asymm β s] : is_asymm α r := ⟨λ a b h₁ h₂, asymm (f.map_rel_iff.2 h₁) (f.map_rel_iff.2 h₂)⟩ protected theorem is_antisymm : ∀ (f : r ↪r s) [is_antisymm β s], is_antisymm α r | ⟨f, o⟩ ⟨H⟩ := ⟨λ a b h₁ h₂, f.inj' (H _ _ (o.2 h₁) (o.2 h₂))⟩ protected theorem is_trans : ∀ (f : r ↪r s) [is_trans β s], is_trans α r | ⟨f, o⟩ ⟨H⟩ := ⟨λ a b c h₁ h₂, o.1 (H _ _ _ (o.2 h₁) (o.2 h₂))⟩ protected theorem is_total : ∀ (f : r ↪r s) [is_total β s], is_total α r | ⟨f, o⟩ ⟨H⟩ := ⟨λ a b, (or_congr o o).1 (H _ _)⟩ protected theorem is_preorder : ∀ (f : r ↪r s) [is_preorder β s], is_preorder α r | f H := by exactI {..f.is_refl, ..f.is_trans} protected theorem is_partial_order : ∀ (f : r ↪r s) [is_partial_order β s], is_partial_order α r | f H := by exactI {..f.is_preorder, ..f.is_antisymm} protected theorem is_linear_order : ∀ (f : r ↪r s) [is_linear_order β s], is_linear_order α r | f H := by exactI {..f.is_partial_order, ..f.is_total} protected theorem is_strict_order : ∀ (f : r ↪r s) [is_strict_order β s], is_strict_order α r | f H := by exactI {..f.is_irrefl, ..f.is_trans} protected theorem is_trichotomous : ∀ (f : r ↪r s) [is_trichotomous β s], is_trichotomous α r | ⟨f, o⟩ ⟨H⟩ := ⟨λ a b, (or_congr o (or_congr f.inj'.eq_iff o)).1 (H _ _)⟩ protected theorem is_strict_total_order' : ∀ (f : r ↪r s) [is_strict_total_order' β s], is_strict_total_order' α r | f H := by exactI {..f.is_trichotomous, ..f.is_strict_order} protected theorem acc (f : r ↪r s) (a : α) : acc s (f a) → acc r a := begin generalize h : f a = b, intro ac, induction ac with _ H IH generalizing a, subst h, exact ⟨_, λ a' h, IH (f a') (f.map_rel_iff.2 h) _ rfl⟩ end protected theorem well_founded : ∀ (f : r ↪r s) (h : well_founded s), well_founded r | f ⟨H⟩ := ⟨λ a, f.acc _ (H _)⟩ protected theorem is_well_order : ∀ (f : r ↪r s) [is_well_order β s], is_well_order α r | f H := by exactI {wf := f.well_founded H.wf, ..f.is_strict_total_order'} /-- It suffices to prove `f` is monotone between strict relations to show it is a relation embedding. -/ def of_monotone [is_trichotomous α r] [is_asymm β s] (f : α → β) (H : ∀ a b, r a b → s (f a) (f b)) : r ↪r s := begin haveI := @is_asymm.is_irrefl β s _, refine ⟨⟨f, λ a b e, _⟩, λ a b, ⟨λ h, _, H _ _⟩⟩, { refine ((@trichotomous _ r _ a b).resolve_left _).resolve_right _; exact λ h, @irrefl _ s _ _ (by simpa [e] using H _ _ h) }, { refine (@trichotomous _ r _ a b).resolve_right (or.rec (λ e, _) (λ h', _)), { subst e, exact irrefl _ h }, { exact asymm (H _ _ h') h } } end @[simp] theorem of_monotone_coe [is_trichotomous α r] [is_asymm β s] (f : α → β) (H) : (@of_monotone _ _ r s _ _ f H : α → β) = f := rfl /-- Embeddings of partial orders that preserve `<` also preserve `≤` -/ def order_embedding_of_lt_embedding [partial_order α] [partial_order β] (f : ((<) : α → α → Prop) ↪r ((<) : β → β → Prop)) : α ↪o β := { map_rel_iff' := by { intros, simp [le_iff_lt_or_eq,f.map_rel_iff, f.injective.eq_iff] }, .. f } @[simp] lemma order_embedding_of_lt_embedding_apply [partial_order α] [partial_order β] {f : ((<) : α → α → Prop) ↪r ((<) : β → β → Prop)} {x : α} : order_embedding_of_lt_embedding f x = f x := rfl end rel_embedding namespace order_embedding variables [preorder α] [preorder β] (f : α ↪o β) /-- lt is preserved by order embeddings of preorders -/ def lt_embedding : ((<) : α → α → Prop) ↪r ((<) : β → β → Prop) := { map_rel_iff' := by intros; simp [lt_iff_le_not_le, f.map_rel_iff], .. f } @[simp] lemma lt_embedding_apply (x : α) : f.lt_embedding x = f x := rfl @[simp] theorem le_iff_le {a b} : (f a) ≤ (f b) ↔ a ≤ b := f.map_rel_iff @[simp] theorem lt_iff_lt {a b} : f a < f b ↔ a < b := f.lt_embedding.map_rel_iff @[simp] lemma eq_iff_eq {a b} : f a = f b ↔ a = b := f.injective.eq_iff protected theorem monotone : monotone f := λ x y, f.le_iff_le.2 protected theorem strict_mono : strict_mono f := λ x y, f.lt_iff_lt.2 protected theorem acc (a : α) : acc (<) (f a) → acc (<) a := f.lt_embedding.acc a protected theorem well_founded : well_founded ((<) : β → β → Prop) → well_founded ((<) : α → α → Prop) := f.lt_embedding.well_founded protected theorem is_well_order [is_well_order β (<)] : is_well_order α (<) := f.lt_embedding.is_well_order /-- An order embedding is also an order embedding between dual orders. -/ protected def dual : order_dual α ↪o order_dual β := ⟨f.to_embedding, λ a b, f.map_rel_iff⟩ /-- A sctrictly monotone map from a linear order is an order embedding. --/ def of_strict_mono {α β} [linear_order α] [preorder β] (f : α → β) (h : strict_mono f) : α ↪o β := { to_fun := f, inj' := strict_mono.injective h, map_rel_iff' := λ a b, h.le_iff_le } @[simp] lemma coe_of_strict_mono {α β} [linear_order α] [preorder β] {f : α → β} (h : strict_mono f) : ⇑(of_strict_mono f h) = f := rfl /-- Embedding of a subtype into the ambient type as an `order_embedding`. -/ def subtype (p : α → Prop) : subtype p ↪o α := ⟨embedding.subtype p, λ x y, iff.rfl⟩ @[simp] lemma coe_subtype (p : α → Prop) : ⇑(subtype p) = coe := rfl end order_embedding /-- A relation isomorphism is an equivalence that is also a relation embedding. -/ structure rel_iso {α β : Type*} (r : α → α → Prop) (s : β → β → Prop) extends α ≃ β := (map_rel_iff' : ∀ {a b}, s (to_equiv a) (to_equiv b) ↔ r a b) infix ` ≃r `:25 := rel_iso /-- An order isomorphism is an equivalence such that `a ≤ b ↔ (f a) ≤ (f b)`. This definition is an abbreviation of `rel_iso (≤) (≤)`. -/ abbreviation order_iso (α β : Type*) [has_le α] [has_le β] := @rel_iso α β (≤) (≤) infix ` ≃o `:25 := order_iso namespace rel_iso /-- Convert an `rel_iso` to an `rel_embedding`. This function is also available as a coercion but often it is easier to write `f.to_rel_embedding` than to write explicitly `r` and `s` in the target type. -/ def to_rel_embedding (f : r ≃r s) : r ↪r s := ⟨f.to_equiv.to_embedding, f.map_rel_iff'⟩ instance : has_coe (r ≃r s) (r ↪r s) := ⟨to_rel_embedding⟩ -- see Note [function coercion] instance : has_coe_to_fun (r ≃r s) := ⟨λ _, α → β, λ f, f⟩ @[simp] lemma to_rel_embedding_eq_coe (f : r ≃r s) : f.to_rel_embedding = f := rfl @[simp] lemma coe_coe_fn (f : r ≃r s) : ((f : r ↪r s) : α → β) = f := rfl theorem map_rel_iff (f : r ≃r s) : ∀ {a b}, s (f a) (f b) ↔ r a b := f.map_rel_iff' @[simp] theorem coe_fn_mk (f : α ≃ β) (o : ∀ ⦃a b⦄, s (f a) (f b) ↔ r a b) : (rel_iso.mk f o : α → β) = f := rfl @[simp] theorem coe_fn_to_equiv (f : r ≃r s) : (f.to_equiv : α → β) = f := rfl theorem to_equiv_injective : injective (to_equiv : (r ≃r s) → α ≃ β) | ⟨e₁, o₁⟩ ⟨e₂, o₂⟩ h := by { congr, exact h } /-- The map `coe_fn : (r ≃r s) → (α → β)` is injective. Lean fails to parse `function.injective (λ e : r ≃r s, (e : α → β))`, so we use a trick to say the same. -/ theorem coe_fn_injective : function.injective (λ (e : r ≃r s) (x : α), e x) := equiv.coe_fn_injective.comp to_equiv_injective @[ext] theorem ext ⦃f g : r ≃r s⦄ (h : ∀ x, f x = g x) : f = g := coe_fn_injective (funext h) theorem ext_iff {f g : r ≃r s} : f = g ↔ ∀ x, f x = g x := ⟨λ h x, h ▸ rfl, λ h, ext h⟩ /-- Identity map is a relation isomorphism. -/ @[refl] protected def refl (r : α → α → Prop) : r ≃r r := ⟨equiv.refl _, λ a b, iff.rfl⟩ /-- Inverse map of a relation isomorphism is a relation isomorphism. -/ @[symm] protected def symm (f : r ≃r s) : s ≃r r := ⟨f.to_equiv.symm, λ a b, by erw [← f.map_rel_iff, f.1.apply_symm_apply, f.1.apply_symm_apply]⟩ /-- Composition of two relation isomorphisms is a relation isomorphism. -/ @[trans] protected def trans (f₁ : r ≃r s) (f₂ : s ≃r t) : r ≃r t := ⟨f₁.to_equiv.trans f₂.to_equiv, λ a b, f₂.map_rel_iff.trans f₁.map_rel_iff⟩ instance (r : α → α → Prop) : inhabited (r ≃r r) := ⟨rel_iso.refl _⟩ @[simp] lemma default_def (r : α → α → Prop) : default (r ≃r r) = rel_iso.refl r := rfl /-- a relation isomorphism is also a relation isomorphism between dual relations. -/ protected def swap (f : r ≃r s) : (swap r) ≃r (swap s) := ⟨f.to_equiv, λ _ _, f.map_rel_iff⟩ @[simp] theorem coe_fn_symm_mk (f o) : ((@rel_iso.mk _ _ r s f o).symm : β → α) = f.symm := rfl @[simp] theorem refl_apply (x : α) : rel_iso.refl r x = x := rfl @[simp] theorem trans_apply (f : r ≃r s) (g : s ≃r t) (a : α) : (f.trans g) a = g (f a) := rfl @[simp] theorem apply_symm_apply (e : r ≃r s) (x : β) : e (e.symm x) = x := e.to_equiv.apply_symm_apply x @[simp] theorem symm_apply_apply (e : r ≃r s) (x : α) : e.symm (e x) = x := e.to_equiv.symm_apply_apply x theorem rel_symm_apply (e : r ≃r s) {x y} : r x (e.symm y) ↔ s (e x) y := by rw [← e.map_rel_iff, e.apply_symm_apply] theorem symm_apply_rel (e : r ≃r s) {x y} : r (e.symm x) y ↔ s x (e y) := by rw [← e.map_rel_iff, e.apply_symm_apply] protected lemma bijective (e : r ≃r s) : bijective e := e.to_equiv.bijective protected lemma injective (e : r ≃r s) : injective e := e.to_equiv.injective protected lemma surjective (e : r ≃r s) : surjective e := e.to_equiv.surjective @[simp] lemma range_eq (e : r ≃r s) : set.range e = set.univ := e.surjective.range_eq @[simp] lemma eq_iff_eq (f : r ≃r s) {a b} : f a = f b ↔ a = b := f.injective.eq_iff /-- Any equivalence lifts to a relation isomorphism between `s` and its preimage. -/ protected def preimage (f : α ≃ β) (s : β → β → Prop) : f ⁻¹'o s ≃r s := ⟨f, λ a b, iff.rfl⟩ /-- A surjective relation embedding is a relation isomorphism. -/ noncomputable def of_surjective (f : r ↪r s) (H : surjective f) : r ≃r s := ⟨equiv.of_bijective f ⟨f.injective, H⟩, λ a b, f.map_rel_iff⟩ @[simp] theorem of_surjective_coe (f : r ↪r s) (H) : (of_surjective f H : α → β) = f := rfl /-- Given relation isomorphisms `r₁ ≃r r₂` and `s₁ ≃r s₂`, construct a relation isomorphism for the lexicographic orders on the sum. -/ def sum_lex_congr {α₁ α₂ β₁ β₂ r₁ r₂ s₁ s₂} (e₁ : @rel_iso α₁ α₂ r₁ r₂) (e₂ : @rel_iso β₁ β₂ s₁ s₂) : sum.lex r₁ s₁ ≃r sum.lex r₂ s₂ := ⟨equiv.sum_congr e₁.to_equiv e₂.to_equiv, λ a b, by cases e₁ with f hf; cases e₂ with g hg; cases a; cases b; simp [hf, hg]⟩ /-- Given relation isomorphisms `r₁ ≃r r₂` and `s₁ ≃r s₂`, construct a relation isomorphism for the lexicographic orders on the product. -/ def prod_lex_congr {α₁ α₂ β₁ β₂ r₁ r₂ s₁ s₂} (e₁ : @rel_iso α₁ α₂ r₁ r₂) (e₂ : @rel_iso β₁ β₂ s₁ s₂) : prod.lex r₁ s₁ ≃r prod.lex r₂ s₂ := ⟨equiv.prod_congr e₁.to_equiv e₂.to_equiv, λ a b, by simp [prod.lex_def, e₁.map_rel_iff, e₂.map_rel_iff]⟩ instance : group (r ≃r r) := { one := rel_iso.refl r, mul := λ f₁ f₂, f₂.trans f₁, inv := rel_iso.symm, mul_assoc := λ f₁ f₂ f₃, rfl, one_mul := λ f, ext $ λ _, rfl, mul_one := λ f, ext $ λ _, rfl, mul_left_inv := λ f, ext f.symm_apply_apply } @[simp] lemma coe_one : ⇑(1 : r ≃r r) = id := rfl @[simp] lemma coe_mul (e₁ e₂ : r ≃r r) : ⇑(e₁ * e₂) = e₁ ∘ e₂ := rfl lemma mul_apply (e₁ e₂ : r ≃r r) (x : α) : (e₁ * e₂) x = e₁ (e₂ x) := rfl @[simp] lemma inv_apply_self (e : r ≃r r) (x) : e⁻¹ (e x) = x := e.symm_apply_apply x @[simp] lemma apply_inv_self (e : r ≃r r) (x) : e (e⁻¹ x) = x := e.apply_symm_apply x end rel_iso namespace order_iso section has_le variables [has_le α] [has_le β] [has_le γ] /-- Reinterpret an order isomorphism as an order embedding. -/ def to_order_embedding (e : α ≃o β) : α ↪o β := e.to_rel_embedding @[simp] lemma coe_to_order_embedding (e : α ≃o β) : ⇑(e.to_order_embedding) = e := rfl protected lemma bijective (e : α ≃o β) : bijective e := e.to_equiv.bijective protected lemma injective (e : α ≃o β) : injective e := e.to_equiv.injective protected lemma surjective (e : α ≃o β) : surjective e := e.to_equiv.surjective @[simp] lemma range_eq (e : α ≃o β) : set.range e = set.univ := e.surjective.range_eq @[simp] lemma apply_eq_iff_eq (e : α ≃o β) {x y : α} : e x = e y ↔ x = y := e.to_equiv.apply_eq_iff_eq /-- Inverse of an order isomorphism. -/ def symm (e : α ≃o β) : β ≃o α := e.symm @[simp] lemma apply_symm_apply (e : α ≃o β) (x : β) : e (e.symm x) = x := e.to_equiv.apply_symm_apply x @[simp] lemma symm_apply_apply (e : α ≃o β) (x : α) : e.symm (e x) = x := e.to_equiv.symm_apply_apply x lemma apply_eq_iff_eq_symm_apply (e : α ≃o β) (x : α) (y : β) : e x = y ↔ x = e.symm y := e.to_equiv.apply_eq_iff_eq_symm_apply theorem symm_apply_eq (e : α ≃o β) {x : α} {y : β} : e.symm y = x ↔ y = e x := e.to_equiv.symm_apply_eq @[simp] lemma symm_symm (e : α ≃o β) : e.symm.symm = e := by { ext, refl } lemma symm_injective : injective (symm : (α ≃o β) → (β ≃o α)) := λ e e' h, by rw [← e.symm_symm, h, e'.symm_symm] @[simp] lemma to_equiv_symm (e : α ≃o β) : e.to_equiv.symm = e.symm.to_equiv := rfl /-- Composition of two order isomorphisms is an order isomorphism. -/ @[trans] def trans (e : α ≃o β) (e' : β ≃o γ) : α ≃o γ := e.trans e' @[simp] lemma coe_trans (e : α ≃o β) (e' : β ≃o γ) : ⇑(e.trans e') = e' ∘ e := rfl lemma trans_apply (e : α ≃o β) (e' : β ≃o γ) (x : α) : e.trans e' x = e' (e x) := rfl end has_le open set variables [preorder α] [preorder β] [preorder γ] protected lemma monotone (e : α ≃o β) : monotone e := e.to_order_embedding.monotone protected lemma strict_mono (e : α ≃o β) : strict_mono e := e.to_order_embedding.strict_mono @[simp] lemma le_iff_le (e : α ≃o β) {x y : α} : e x ≤ e y ↔ x ≤ y := e.map_rel_iff @[simp] lemma lt_iff_lt (e : α ≃o β) {x y : α} : e x < e y ↔ x < y := e.to_order_embedding.lt_iff_lt @[simp] lemma preimage_Iic (e : α ≃o β) (b : β) : e ⁻¹' (Iic b) = Iic (e.symm b) := by { ext x, simp [← e.le_iff_le] } @[simp] lemma preimage_Ici (e : α ≃o β) (b : β) : e ⁻¹' (Ici b) = Ici (e.symm b) := by { ext x, simp [← e.le_iff_le] } @[simp] lemma preimage_Iio (e : α ≃o β) (b : β) : e ⁻¹' (Iio b) = Iio (e.symm b) := by { ext x, simp [← e.lt_iff_lt] } @[simp] lemma preimage_Ioi (e : α ≃o β) (b : β) : e ⁻¹' (Ioi b) = Ioi (e.symm b) := by { ext x, simp [← e.lt_iff_lt] } @[simp] lemma preimage_Icc (e : α ≃o β) (a b : β) : e ⁻¹' (Icc a b) = Icc (e.symm a) (e.symm b) := by simp [← Ici_inter_Iic] @[simp] lemma preimage_Ico (e : α ≃o β) (a b : β) : e ⁻¹' (Ico a b) = Ico (e.symm a) (e.symm b) := by simp [← Ici_inter_Iio] @[simp] lemma preimage_Ioc (e : α ≃o β) (a b : β) : e ⁻¹' (Ioc a b) = Ioc (e.symm a) (e.symm b) := by simp [← Ioi_inter_Iic] @[simp] lemma preimage_Ioo (e : α ≃o β) (a b : β) : e ⁻¹' (Ioo a b) = Ioo (e.symm a) (e.symm b) := by simp [← Ioi_inter_Iio] /-- To show that `f : α → β`, `g : β → α` make up an order isomorphism of linear orders, it suffices to prove `cmp a (g b) = cmp (f a) b`. --/ def of_cmp_eq_cmp {α β} [linear_order α] [linear_order β] (f : α → β) (g : β → α) (h : ∀ (a : α) (b : β), cmp a (g b) = cmp (f a) b) : α ≃o β := have gf : ∀ (a : α), a = g (f a) := by { intro, rw [←cmp_eq_eq_iff, h, cmp_self_eq_eq] }, { to_fun := f, inv_fun := g, left_inv := λ a, (gf a).symm, right_inv := by { intro, rw [←cmp_eq_eq_iff, ←h, cmp_self_eq_eq] }, map_rel_iff' := by { intros, apply le_iff_le_of_cmp_eq_cmp, convert (h _ _).symm, apply gf } } /-- Order isomorphism between two equal sets. -/ def set_congr (s t : set α) (h : s = t) : s ≃o t := { to_equiv := equiv.set_congr h, map_rel_iff' := λ x y, iff.rfl } /-- Order isomorphism between `univ : set α` and `α`. -/ def set.univ : (set.univ : set α) ≃o α := { to_equiv := equiv.set.univ α, map_rel_iff' := λ x y, iff.rfl } end order_iso /-- If a function `f` is strictly monotone on a set `s`, then it defines an order isomorphism between `s` and its image. -/ protected noncomputable def strict_mono_incr_on.order_iso {α β} [linear_order α] [preorder β] (f : α → β) (s : set α) (hf : strict_mono_incr_on f s) : s ≃o f '' s := { to_equiv := hf.inj_on.bij_on_image.equiv _, map_rel_iff' := λ x y, hf.le_iff_le x.2 y.2 } /-- A strictly monotone function from a linear order is an order isomorphism between its domain and its range. -/ protected noncomputable def strict_mono.order_iso {α β} [linear_order α] [preorder β] (f : α → β) (h_mono : strict_mono f) : α ≃o set.range f := { to_equiv := equiv.of_injective f h_mono.injective, map_rel_iff' := λ a b, h_mono.le_iff_le } /-- A strictly monotone surjective function from a linear order is an order isomorphism. -/ noncomputable def strict_mono.order_iso_of_surjective {α β} [linear_order α] [preorder β] (f : α → β) (h_mono : strict_mono f) (h_surj : surjective f) : α ≃o β := (h_mono.order_iso f).trans $ (order_iso.set_congr _ _ h_surj.range_eq).trans order_iso.set.univ /-- `subrel r p` is the inherited relation on a subset. -/ def subrel (r : α → α → Prop) (p : set α) : p → p → Prop := (coe : p → α) ⁻¹'o r @[simp] theorem subrel_val (r : α → α → Prop) (p : set α) {a b} : subrel r p a b ↔ r a.1 b.1 := iff.rfl namespace subrel /-- The relation embedding from the inherited relation on a subset. -/ protected def rel_embedding (r : α → α → Prop) (p : set α) : subrel r p ↪r r := ⟨embedding.subtype _, λ a b, iff.rfl⟩ @[simp] theorem rel_embedding_apply (r : α → α → Prop) (p a) : subrel.rel_embedding r p a = a.1 := rfl instance (r : α → α → Prop) [is_well_order α r] (p : set α) : is_well_order p (subrel r p) := rel_embedding.is_well_order (subrel.rel_embedding r p) end subrel /-- Restrict the codomain of a relation embedding. -/ def rel_embedding.cod_restrict (p : set β) (f : r ↪r s) (H : ∀ a, f a ∈ p) : r ↪r subrel s p := ⟨f.to_embedding.cod_restrict p H, f.map_rel_iff'⟩ @[simp] theorem rel_embedding.cod_restrict_apply (p) (f : r ↪r s) (H a) : rel_embedding.cod_restrict p f H a = ⟨f a, H a⟩ := rfl /-- An order isomorphism is also an order isomorphism between dual orders. -/ protected def order_iso.dual [preorder α] [preorder β] (f : α ≃o β) : order_dual α ≃o order_dual β := ⟨f.to_equiv, λ _ _, f.map_rel_iff⟩ section lattice_isos lemma order_iso.map_bot' [partial_order α] [partial_order β] (f : α ≃o β) {x : α} {y : β} (hx : ∀ x', x ≤ x') (hy : ∀ y', y ≤ y') : f x = y := by { refine le_antisymm _ (hy _), rw [← f.apply_symm_apply y, f.map_rel_iff], apply hx } lemma order_iso.map_bot [order_bot α] [order_bot β] (f : α ≃o β) : f ⊥ = ⊥ := f.map_bot' (λ _, bot_le) (λ _, bot_le) lemma order_iso.map_top' [partial_order α] [partial_order β] (f : α ≃o β) {x : α} {y : β} (hx : ∀ x', x' ≤ x) (hy : ∀ y', y' ≤ y) : f x = y := f.dual.map_bot' hx hy lemma order_iso.map_top [order_top α] [order_top β] (f : α ≃o β) : f ⊤ = ⊤ := f.dual.map_bot lemma order_embedding.map_inf_le [semilattice_inf α] [semilattice_inf β] (f : α ↪o β) (x y : α) : f (x ⊓ y) ≤ f x ⊓ f y := f.monotone.map_inf_le x y lemma order_iso.map_inf [semilattice_inf α] [semilattice_inf β] (f : α ≃o β) (x y : α) : f (x ⊓ y) = f x ⊓ f y := begin refine (f.to_order_embedding.map_inf_le x y).antisymm _, simpa [← f.symm.le_iff_le] using f.symm.to_order_embedding.map_inf_le (f x) (f y) end lemma order_embedding.le_map_sup [semilattice_sup α] [semilattice_sup β] (f : α ↪o β) (x y : α) : f x ⊔ f y ≤ f (x ⊔ y) := f.monotone.le_map_sup x y lemma order_iso.map_sup [semilattice_sup α] [semilattice_sup β] (f : α ≃o β) (x y : α) : f (x ⊔ y) = f x ⊔ f y := f.dual.map_inf x y /-- Order isomorphism between `Iic (⊤ : α)` and `α` when `α` has a top element -/ def order_iso.Iic_top [order_top α] : set.Iic (⊤ : α) ≃o α := { map_rel_iff' := λ x y, by refl, .. (@equiv.subtype_univ_equiv α (set.Iic (⊤ : α)) (λ x, le_top)), } /-- Order isomorphism between `Ici (⊥ : α)` and `α` when `α` has a bottom element -/ def order_iso.Ici_bot [order_bot α] : set.Ici (⊥ : α) ≃o α := { map_rel_iff' := λ x y, by refl, .. (@equiv.subtype_univ_equiv α (set.Ici (⊥ : α)) (λ x, bot_le)) } section bounded_lattice variables [bounded_lattice α] [bounded_lattice β] (f : α ≃o β) include f lemma order_iso.is_compl {x y : α} (h : is_compl x y) : is_compl (f x) (f y) := ⟨by { rw [← f.map_bot, ← f.map_inf, f.map_rel_iff], exact h.1 }, by { rw [← f.map_top, ← f.map_sup, f.map_rel_iff], exact h.2 }⟩ theorem order_iso.is_compl_iff {x y : α} : is_compl x y ↔ is_compl (f x) (f y) := ⟨f.is_compl, λ h, begin rw [← f.symm_apply_apply x, ← f.symm_apply_apply y], exact f.symm.is_compl h, end⟩ lemma order_iso.is_complemented [is_complemented α] : is_complemented β := ⟨λ x, begin obtain ⟨y, hy⟩ := exists_is_compl (f.symm x), rw ← f.symm_apply_apply y at hy, refine ⟨f y, f.symm.is_compl_iff.2 hy⟩, end⟩ theorem order_iso.is_complemented_iff : is_complemented α ↔ is_complemented β := ⟨by { introI, exact f.is_complemented }, by { introI, exact f.symm.is_complemented }⟩ end bounded_lattice end lattice_isos
a36291e2bf05e1ffa8e917074edb1ecf7748922b
6a0e083a886c678aa3899e8704b88613bb310ad0
/src/commutative_monad.lean
a387cf6be61e09a6212a49449bd916c870c6ce15
[]
no_license
adamtopaz/hyperalgebra-experiments
96566821cd32eda13602a06cf5657e7805127492
54a0dd63d3a5c59576441671c9ad2e7ba12b9a2e
refs/heads/master
1,686,042,050,049
1,625,018,844,000
1,625,018,844,000
381,545,014
0
0
null
null
null
null
UTF-8
Lean
false
false
510
lean
import control.monad.basic import data.set.constructions class commutative_monad (M : Type* → Type*) [monad M] [is_lawful_monad M] := (commutative : Π {α β γ} (ma : M α) (mb : M β) (f : α → β → M γ), (do { a ← ma, b ← mb, f a b }) = (do { b ← mb, a ← ma, f a b }) . obviously) -- For regular operations instance : commutative_monad id := {} -- For hyperoperations instance : commutative_monad set := {} -- For partially defined operations instance : commutative_monad option := {}
5ce7d43610857b4128ca1cebd39290bdb712eb36
1dd482be3f611941db7801003235dc84147ec60a
/src/algebra/group.lean
4f5acace902ac73337e67f129b448e3a91469707
[ "Apache-2.0" ]
permissive
sanderdahmen/mathlib
479039302bd66434bb5672c2a4cecf8d69981458
8f0eae75cd2d8b7a083cf935666fcce4565df076
refs/heads/master
1,587,491,322,775
1,549,672,060,000
1,549,672,060,000
169,748,224
0
0
Apache-2.0
1,549,636,694,000
1,549,636,694,000
null
UTF-8
Lean
false
false
31,103
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura Various multiplicative and additive structures. -/ import tactic.interactive data.option.defs section pending_1857 /- Transport multiplicative to additive -/ section transport open tactic @[user_attribute] meta def to_additive_attr : user_attribute (name_map name) name := { name := `to_additive, descr := "Transport multiplicative to additive", cache_cfg := ⟨λ ns, ns.mfoldl (λ dict n, do val ← to_additive_attr.get_param n, pure $ dict.insert n val) mk_name_map, []⟩, parser := lean.parser.ident, after_set := some $ λ src _ _, do env ← get_env, dict ← to_additive_attr.get_cache, tgt ← to_additive_attr.get_param src, (get_decl tgt >> skip) <|> transport_with_dict dict src tgt } end transport /- map operations -/ attribute [to_additive has_add.add] has_mul.mul attribute [to_additive has_zero.zero] has_one.one attribute [to_additive has_neg.neg] has_inv.inv attribute [to_additive has_add] has_mul attribute [to_additive has_zero] has_one attribute [to_additive has_neg] has_inv /- map constructors -/ attribute [to_additive has_add.mk] has_mul.mk attribute [to_additive has_zero.mk] has_one.mk attribute [to_additive has_neg.mk] has_inv.mk /- map structures -/ attribute [to_additive add_semigroup] semigroup attribute [to_additive add_semigroup.mk] semigroup.mk attribute [to_additive add_semigroup.to_has_add] semigroup.to_has_mul attribute [to_additive add_semigroup.add_assoc] semigroup.mul_assoc attribute [to_additive add_semigroup.add] semigroup.mul attribute [to_additive add_comm_semigroup] comm_semigroup attribute [to_additive add_comm_semigroup.mk] comm_semigroup.mk attribute [to_additive add_comm_semigroup.to_add_semigroup] comm_semigroup.to_semigroup attribute [to_additive add_comm_semigroup.add_comm] comm_semigroup.mul_comm attribute [to_additive add_left_cancel_semigroup] left_cancel_semigroup attribute [to_additive add_left_cancel_semigroup.mk] left_cancel_semigroup.mk attribute [to_additive add_left_cancel_semigroup.to_add_semigroup] left_cancel_semigroup.to_semigroup attribute [to_additive add_left_cancel_semigroup.add_left_cancel] left_cancel_semigroup.mul_left_cancel attribute [to_additive add_right_cancel_semigroup] right_cancel_semigroup attribute [to_additive add_right_cancel_semigroup.mk] right_cancel_semigroup.mk attribute [to_additive add_right_cancel_semigroup.to_add_semigroup] right_cancel_semigroup.to_semigroup attribute [to_additive add_right_cancel_semigroup.add_right_cancel] right_cancel_semigroup.mul_right_cancel attribute [to_additive add_monoid] monoid attribute [to_additive add_monoid.mk] monoid.mk attribute [to_additive add_monoid.to_has_zero] monoid.to_has_one attribute [to_additive add_monoid.to_add_semigroup] monoid.to_semigroup attribute [to_additive add_monoid.add] monoid.mul attribute [to_additive add_monoid.add_assoc] monoid.mul_assoc attribute [to_additive add_monoid.zero] monoid.one attribute [to_additive add_monoid.zero_add] monoid.one_mul attribute [to_additive add_monoid.add_zero] monoid.mul_one attribute [to_additive add_comm_monoid] comm_monoid attribute [to_additive add_comm_monoid.mk] comm_monoid.mk attribute [to_additive add_comm_monoid.to_add_monoid] comm_monoid.to_monoid attribute [to_additive add_comm_monoid.to_add_comm_semigroup] comm_monoid.to_comm_semigroup attribute [to_additive add_group] group attribute [to_additive add_group.mk] group.mk attribute [to_additive add_group.to_has_neg] group.to_has_inv attribute [to_additive add_group.to_add_monoid] group.to_monoid attribute [to_additive add_group.add_left_neg] group.mul_left_inv attribute [to_additive add_group.add] group.mul attribute [to_additive add_group.add_assoc] group.mul_assoc attribute [to_additive add_group.zero] group.one attribute [to_additive add_group.zero_add] group.one_mul attribute [to_additive add_group.add_zero] group.mul_one attribute [to_additive add_group.neg] group.inv attribute [to_additive add_comm_group] comm_group attribute [to_additive add_comm_group.mk] comm_group.mk attribute [to_additive add_comm_group.to_add_group] comm_group.to_group attribute [to_additive add_comm_group.to_add_comm_monoid] comm_group.to_comm_monoid /- map theorems -/ attribute [to_additive add_assoc] mul_assoc attribute [to_additive add_semigroup_to_is_associative] semigroup_to_is_associative attribute [to_additive add_comm] mul_comm attribute [to_additive add_comm_semigroup_to_is_commutative] comm_semigroup_to_is_commutative attribute [to_additive add_left_comm] mul_left_comm attribute [to_additive add_right_comm] mul_right_comm attribute [to_additive add_left_cancel] mul_left_cancel attribute [to_additive add_right_cancel] mul_right_cancel attribute [to_additive add_left_cancel_iff] mul_left_cancel_iff attribute [to_additive add_right_cancel_iff] mul_right_cancel_iff attribute [to_additive zero_add] one_mul attribute [to_additive add_zero] mul_one attribute [to_additive add_left_neg] mul_left_inv attribute [to_additive neg_add_self] inv_mul_self attribute [to_additive neg_add_cancel_left] inv_mul_cancel_left attribute [to_additive neg_add_cancel_right] inv_mul_cancel_right attribute [to_additive neg_eq_of_add_eq_zero] inv_eq_of_mul_eq_one attribute [to_additive neg_zero] one_inv attribute [to_additive neg_neg] inv_inv attribute [to_additive add_right_neg] mul_right_inv attribute [to_additive add_neg_self] mul_inv_self attribute [to_additive neg_inj] inv_inj attribute [to_additive add_group.add_left_cancel] group.mul_left_cancel attribute [to_additive add_group.add_right_cancel] group.mul_right_cancel attribute [to_additive add_group.to_left_cancel_add_semigroup] group.to_left_cancel_semigroup attribute [to_additive add_group.to_right_cancel_add_semigroup] group.to_right_cancel_semigroup attribute [to_additive add_neg_cancel_left] mul_inv_cancel_left attribute [to_additive add_neg_cancel_right] mul_inv_cancel_right attribute [to_additive neg_add_rev] mul_inv_rev attribute [to_additive eq_neg_of_eq_neg] eq_inv_of_eq_inv attribute [to_additive eq_neg_of_add_eq_zero] eq_inv_of_mul_eq_one attribute [to_additive eq_add_neg_of_add_eq] eq_mul_inv_of_mul_eq attribute [to_additive eq_neg_add_of_add_eq] eq_inv_mul_of_mul_eq attribute [to_additive neg_add_eq_of_eq_add] inv_mul_eq_of_eq_mul attribute [to_additive add_neg_eq_of_eq_add] mul_inv_eq_of_eq_mul attribute [to_additive eq_add_of_add_neg_eq] eq_mul_of_mul_inv_eq attribute [to_additive eq_add_of_neg_add_eq] eq_mul_of_inv_mul_eq attribute [to_additive add_eq_of_eq_neg_add] mul_eq_of_eq_inv_mul attribute [to_additive add_eq_of_eq_add_neg] mul_eq_of_eq_mul_inv attribute [to_additive neg_add] mul_inv end pending_1857 instance monoid_to_is_left_id {α : Type*} [monoid α] : is_left_id α (*) 1 := ⟨ monoid.one_mul ⟩ instance monoid_to_is_right_id {α : Type*} [monoid α] : is_right_id α (*) 1 := ⟨ monoid.mul_one ⟩ instance add_monoid_to_is_left_id {α : Type*} [add_monoid α] : is_left_id α (+) 0 := ⟨ add_monoid.zero_add ⟩ instance add_monoid_to_is_right_id {α : Type*} [add_monoid α] : is_right_id α (+) 0 := ⟨ add_monoid.add_zero ⟩ universes u v variables {α : Type u} {β : Type v} def additive (α : Type*) := α def multiplicative (α : Type*) := α instance [semigroup α] : add_semigroup (additive α) := { add := ((*) : α → α → α), add_assoc := @mul_assoc _ _ } instance [add_semigroup α] : semigroup (multiplicative α) := { mul := ((+) : α → α → α), mul_assoc := @add_assoc _ _ } instance [comm_semigroup α] : add_comm_semigroup (additive α) := { add_comm := @mul_comm _ _, ..additive.add_semigroup } instance [add_comm_semigroup α] : comm_semigroup (multiplicative α) := { mul_comm := @add_comm _ _, ..multiplicative.semigroup } instance [left_cancel_semigroup α] : add_left_cancel_semigroup (additive α) := { add_left_cancel := @mul_left_cancel _ _, ..additive.add_semigroup } instance [add_left_cancel_semigroup α] : left_cancel_semigroup (multiplicative α) := { mul_left_cancel := @add_left_cancel _ _, ..multiplicative.semigroup } instance [right_cancel_semigroup α] : add_right_cancel_semigroup (additive α) := { add_right_cancel := @mul_right_cancel _ _, ..additive.add_semigroup } instance [add_right_cancel_semigroup α] : right_cancel_semigroup (multiplicative α) := { mul_right_cancel := @add_right_cancel _ _, ..multiplicative.semigroup } @[simp, to_additive add_left_inj] theorem mul_left_inj [left_cancel_semigroup α] (a : α) {b c : α} : a * b = a * c ↔ b = c := ⟨mul_left_cancel, congr_arg _⟩ @[simp, to_additive add_right_inj] theorem mul_right_inj [right_cancel_semigroup α] (a : α) {b c : α} : b * a = c * a ↔ b = c := ⟨mul_right_cancel, congr_arg _⟩ structure units (α : Type u) [monoid α] := (val : α) (inv : α) (val_inv : val * inv = 1) (inv_val : inv * val = 1) namespace units variables [monoid α] {a b c : units α} instance : has_coe (units α) α := ⟨val⟩ @[extensionality] theorem ext : ∀ {a b : units α}, (a : α) = b → a = b | ⟨v, i₁, vi₁, iv₁⟩ ⟨v', i₂, vi₂, iv₂⟩ e := by change v = v' at e; subst v'; congr; simpa only [iv₂, vi₁, one_mul, mul_one] using mul_assoc i₂ v i₁ theorem ext_iff {a b : units α} : a = b ↔ (a : α) = b := ⟨congr_arg _, ext⟩ instance [decidable_eq α] : decidable_eq (units α) | a b := decidable_of_iff' _ ext_iff protected def mul (u₁ u₂ : units α) : units α := ⟨u₁.val * u₂.val, u₂.inv * u₁.inv, have u₁.val * (u₂.val * u₂.inv) * u₁.inv = 1, by rw [u₂.val_inv]; rw [mul_one, u₁.val_inv], by simpa only [mul_assoc], have u₂.inv * (u₁.inv * u₁.val) * u₂.val = 1, by rw [u₁.inv_val]; rw [mul_one, u₂.inv_val], by simpa only [mul_assoc]⟩ protected def inv' (u : units α) : units α := ⟨u.inv, u.val, u.inv_val, u.val_inv⟩ instance : has_mul (units α) := ⟨units.mul⟩ instance : has_one (units α) := ⟨⟨1, 1, mul_one 1, one_mul 1⟩⟩ instance : has_inv (units α) := ⟨units.inv'⟩ variables (a b) @[simp] lemma coe_mul : (↑(a * b) : α) = a * b := rfl @[simp] lemma coe_one : ((1 : units α) : α) = 1 := rfl lemma val_coe : (↑a : α) = a.val := rfl lemma coe_inv : ((a⁻¹ : units α) : α) = a.inv := rfl @[simp] lemma inv_mul : (↑a⁻¹ * a : α) = 1 := inv_val _ @[simp] lemma mul_inv : (a * ↑a⁻¹ : α) = 1 := val_inv _ @[simp] lemma mul_inv_cancel_left (a : units α) (b : α) : (a:α) * (↑a⁻¹ * b) = b := by rw [← mul_assoc, mul_inv, one_mul] @[simp] lemma inv_mul_cancel_left (a : units α) (b : α) : (↑a⁻¹:α) * (a * b) = b := by rw [← mul_assoc, inv_mul, one_mul] @[simp] lemma mul_inv_cancel_right (a : α) (b : units α) : a * b * ↑b⁻¹ = a := by rw [mul_assoc, mul_inv, mul_one] @[simp] lemma inv_mul_cancel_right (a : α) (b : units α) : a * ↑b⁻¹ * b = a := by rw [mul_assoc, inv_mul, mul_one] instance : group (units α) := by refine {mul := (*), one := 1, inv := has_inv.inv, ..}; { intros, apply ext, simp only [coe_mul, coe_one, mul_assoc, one_mul, mul_one, inv_mul] } instance {α} [comm_monoid α] : comm_group (units α) := { mul_comm := λ u₁ u₂, ext $ mul_comm _ _, ..units.group } instance [has_repr α] : has_repr (units α) := ⟨repr ∘ val⟩ @[simp] theorem mul_left_inj (a : units α) {b c : α} : (a:α) * b = a * c ↔ b = c := ⟨λ h, by simpa only [inv_mul_cancel_left] using congr_arg ((*) ↑(a⁻¹ : units α)) h, congr_arg _⟩ @[simp] theorem mul_right_inj (a : units α) {b c : α} : b * a = c * a ↔ b = c := ⟨λ h, by simpa only [mul_inv_cancel_right] using congr_arg (* ↑(a⁻¹ : units α)) h, congr_arg _⟩ end units theorem nat.units_eq_one (u : units ℕ) : u = 1 := units.ext $ nat.eq_one_of_dvd_one ⟨u.inv, u.val_inv.symm⟩ def units.mk_of_mul_eq_one [comm_monoid α] (a b : α) (hab : a * b = 1) : units α := ⟨a, b, hab, (mul_comm b a).trans hab⟩ @[to_additive with_zero] def with_one (α) := option α @[to_additive with_zero.has_coe_t] instance : has_coe_t α (with_one α) := ⟨some⟩ instance [semigroup α] : monoid (with_one α) := { one := none, mul := option.lift_or_get (*), mul_assoc := (option.lift_or_get_assoc _).1, one_mul := (option.lift_or_get_is_left_id _).1, mul_one := (option.lift_or_get_is_right_id _).1 } attribute [to_additive with_zero.add_monoid._proof_1] with_one.monoid._proof_1 attribute [to_additive with_zero.add_monoid._proof_2] with_one.monoid._proof_2 attribute [to_additive with_zero.add_monoid._proof_3] with_one.monoid._proof_3 attribute [to_additive with_zero.add_monoid] with_one.monoid instance [semigroup α] : mul_zero_class (with_zero α) := { zero := none, mul := λ o₁ o₂, o₁.bind (λ a, o₂.map (λ b, a * b)), zero_mul := λ a, rfl, mul_zero := λ a, by cases a; refl, ..with_zero.add_monoid } instance [semigroup α] : semigroup (with_zero α) := { mul_assoc := λ a b c, match a, b, c with | none, _, _ := rfl | some a, none, _ := rfl | some a, some b, none := rfl | some a, some b, some c := congr_arg some (mul_assoc _ _ _) end, ..with_zero.mul_zero_class } instance [comm_semigroup α] : comm_semigroup (with_zero α) := { mul_comm := λ a b, match a, b with | none, _ := (mul_zero _).symm | some a, none := rfl | some a, some b := congr_arg some (mul_comm _ _) end, ..with_zero.semigroup } instance [monoid α] : monoid (with_zero α) := { one := some 1, one_mul := λ a, match a with | none := rfl | some a := congr_arg some $ one_mul _ end, mul_one := λ a, match a with | none := rfl | some a := congr_arg some $ mul_one _ end, ..with_zero.semigroup } instance [comm_monoid α] : comm_monoid (with_zero α) := { ..with_zero.monoid, ..with_zero.comm_semigroup } instance [monoid α] : add_monoid (additive α) := { zero := (1 : α), zero_add := @one_mul _ _, add_zero := @mul_one _ _, ..additive.add_semigroup } instance [add_monoid α] : monoid (multiplicative α) := { one := (0 : α), one_mul := @zero_add _ _, mul_one := @add_zero _ _, ..multiplicative.semigroup } def free_monoid (α) := list α instance {α} : monoid (free_monoid α) := { one := [], mul := λ x y, (x ++ y : list α), mul_one := by intros; apply list.append_nil, one_mul := by intros; refl, mul_assoc := by intros; apply list.append_assoc } @[simp] lemma free_monoid.one_def {α} : (1 : free_monoid α) = [] := rfl @[simp] lemma free_monoid.mul_def {α} (xs ys : list α) : (xs * ys : free_monoid α) = (xs ++ ys : list α) := rfl def free_add_monoid (α) := list α instance {α} : add_monoid (free_add_monoid α) := { zero := [], add := λ x y, (x ++ y : list α), add_zero := by intros; apply list.append_nil, zero_add := by intros; refl, add_assoc := by intros; apply list.append_assoc } @[simp] lemma free_add_monoid.zero_def {α} : (1 : free_monoid α) = [] := rfl @[simp] lemma free_add_monoid.add_def {α} (xs ys : list α) : (xs * ys : free_monoid α) = (xs ++ ys : list α) := rfl section monoid variables [monoid α] {a b c : α} /-- Partial division. It is defined when the second argument is invertible, and unlike the division operator in `division_ring` it is not totalized at zero. -/ def divp (a : α) (u) : α := a * (u⁻¹ : units α) infix ` /ₚ `:70 := divp @[simp] theorem divp_self (u : units α) : (u : α) /ₚ u = 1 := units.mul_inv _ @[simp] theorem divp_one (a : α) : a /ₚ 1 = a := mul_one _ theorem divp_assoc (a b : α) (u : units α) : a * b /ₚ u = a * (b /ₚ u) := mul_assoc _ _ _ @[simp] theorem divp_mul_cancel (a : α) (u : units α) : a /ₚ u * u = a := (mul_assoc _ _ _).trans $ by rw [units.inv_mul, mul_one] @[simp] theorem mul_divp_cancel (a : α) (u : units α) : (a * u) /ₚ u = a := (mul_assoc _ _ _).trans $ by rw [units.mul_inv, mul_one] @[simp] theorem divp_right_inj (u : units α) {a b : α} : a /ₚ u = b /ₚ u ↔ a = b := units.mul_right_inj _ theorem divp_eq_one (a : α) (u : units α) : a /ₚ u = 1 ↔ a = u := (units.mul_right_inj u).symm.trans $ by rw [divp_mul_cancel, one_mul] @[simp] theorem one_divp (u : units α) : 1 /ₚ u = ↑u⁻¹ := one_mul _ end monoid instance [comm_semigroup α] : comm_monoid (with_one α) := { mul_comm := (option.lift_or_get_comm _).1, ..with_one.monoid } instance [add_comm_semigroup α] : add_comm_monoid (with_zero α) := { add_comm := (option.lift_or_get_comm _).1, ..with_zero.add_monoid } attribute [to_additive with_zero.add_comm_monoid] with_one.comm_monoid instance [comm_monoid α] : add_comm_monoid (additive α) := { add_comm := @mul_comm α _, ..additive.add_monoid } instance [add_comm_monoid α] : comm_monoid (multiplicative α) := { mul_comm := @add_comm α _, ..multiplicative.monoid } instance [group α] : add_group (additive α) := { neg := @has_inv.inv α _, add_left_neg := @mul_left_inv _ _, ..additive.add_monoid } instance [add_group α] : group (multiplicative α) := { inv := @has_neg.neg α _, mul_left_inv := @add_left_neg _ _, ..multiplicative.monoid } section group variables [group α] {a b c : α} instance : has_lift α (units α) := ⟨λ a, ⟨a, a⁻¹, mul_inv_self _, inv_mul_self _⟩⟩ @[simp, to_additive neg_inj'] theorem inv_inj' : a⁻¹ = b⁻¹ ↔ a = b := ⟨λ h, by rw [← inv_inv a, h, inv_inv], congr_arg _⟩ @[to_additive eq_of_neg_eq_neg] theorem eq_of_inv_eq_inv : a⁻¹ = b⁻¹ → a = b := inv_inj'.1 @[simp, to_additive add_self_iff_eq_zero] theorem mul_self_iff_eq_one : a * a = a ↔ a = 1 := by have := @mul_left_inj _ _ a a 1; rwa mul_one at this @[simp, to_additive neg_eq_zero] theorem inv_eq_one : a⁻¹ = 1 ↔ a = 1 := by rw [← @inv_inj' _ _ a 1, one_inv] @[simp, to_additive neg_ne_zero] theorem inv_ne_one : a⁻¹ ≠ 1 ↔ a ≠ 1 := not_congr inv_eq_one @[to_additive left_inverse_neg] theorem left_inverse_inv (α) [group α] : function.left_inverse (λ a : α, a⁻¹) (λ a, a⁻¹) := assume a, inv_inv a attribute [simp] mul_inv_cancel_left add_neg_cancel_left mul_inv_cancel_right add_neg_cancel_right @[to_additive eq_neg_iff_eq_neg] theorem eq_inv_iff_eq_inv : a = b⁻¹ ↔ b = a⁻¹ := ⟨eq_inv_of_eq_inv, eq_inv_of_eq_inv⟩ @[to_additive neg_eq_iff_neg_eq] theorem inv_eq_iff_inv_eq : a⁻¹ = b ↔ b⁻¹ = a := by rw [eq_comm, @eq_comm _ _ a, eq_inv_iff_eq_inv] @[to_additive add_eq_zero_iff_eq_neg] theorem mul_eq_one_iff_eq_inv : a * b = 1 ↔ a = b⁻¹ := by simpa [mul_left_inv, -mul_right_inj] using @mul_right_inj _ _ b a (b⁻¹) @[to_additive add_eq_zero_iff_neg_eq] theorem mul_eq_one_iff_inv_eq : a * b = 1 ↔ a⁻¹ = b := by rw [mul_eq_one_iff_eq_inv, eq_inv_iff_eq_inv, eq_comm] @[to_additive eq_neg_iff_add_eq_zero] theorem eq_inv_iff_mul_eq_one : a = b⁻¹ ↔ a * b = 1 := mul_eq_one_iff_eq_inv.symm @[to_additive neg_eq_iff_add_eq_zero] theorem inv_eq_iff_mul_eq_one : a⁻¹ = b ↔ a * b = 1 := mul_eq_one_iff_inv_eq.symm @[to_additive eq_add_neg_iff_add_eq] theorem eq_mul_inv_iff_mul_eq : a = b * c⁻¹ ↔ a * c = b := ⟨λ h, by rw [h, inv_mul_cancel_right], λ h, by rw [← h, mul_inv_cancel_right]⟩ @[to_additive eq_neg_add_iff_add_eq] theorem eq_inv_mul_iff_mul_eq : a = b⁻¹ * c ↔ b * a = c := ⟨λ h, by rw [h, mul_inv_cancel_left], λ h, by rw [← h, inv_mul_cancel_left]⟩ @[to_additive neg_add_eq_iff_eq_add] theorem inv_mul_eq_iff_eq_mul : a⁻¹ * b = c ↔ b = a * c := ⟨λ h, by rw [← h, mul_inv_cancel_left], λ h, by rw [h, inv_mul_cancel_left]⟩ @[to_additive add_neg_eq_iff_eq_add] theorem mul_inv_eq_iff_eq_mul : a * b⁻¹ = c ↔ a = c * b := ⟨λ h, by rw [← h, inv_mul_cancel_right], λ h, by rw [h, mul_inv_cancel_right]⟩ @[to_additive add_neg_eq_zero] theorem mul_inv_eq_one {a b : α} : a * b⁻¹ = 1 ↔ a = b := by rw [mul_eq_one_iff_eq_inv, inv_inv] @[to_additive neg_comm_of_comm] theorem inv_comm_of_comm {a b : α} (H : a * b = b * a) : a⁻¹ * b = b * a⁻¹ := begin have : a⁻¹ * (b * a) * a⁻¹ = a⁻¹ * (a * b) * a⁻¹ := congr_arg (λ x:α, a⁻¹ * x * a⁻¹) H.symm, rwa [inv_mul_cancel_left, mul_assoc, mul_inv_cancel_right] at this end end group instance [comm_group α] : add_comm_group (additive α) := { add_comm := @mul_comm α _, ..additive.add_group } instance [add_comm_group α] : comm_group (multiplicative α) := { mul_comm := @add_comm α _, ..multiplicative.group } section add_monoid variables [add_monoid α] {a b c : α} @[simp] lemma bit0_zero : bit0 (0 : α) = 0 := add_zero _ @[simp] lemma bit1_zero [has_one α] : bit1 (0 : α) = 1 := show 0+0+1=(1:α), by rw [zero_add, zero_add] end add_monoid section add_group variables [add_group α] {a b c : α} local attribute [simp] sub_eq_add_neg def sub_sub_cancel := @sub_sub_self @[simp] lemma sub_left_inj : a - b = a - c ↔ b = c := (add_left_inj _).trans neg_inj' @[simp] lemma sub_right_inj : b - a = c - a ↔ b = c := add_right_inj _ lemma sub_add_sub_cancel (a b c : α) : (a - b) + (b - c) = a - c := by rw [← add_sub_assoc, sub_add_cancel] lemma sub_sub_sub_cancel_right (a b c : α) : (a - c) - (b - c) = a - b := by rw [← neg_sub c b, sub_neg_eq_add, sub_add_sub_cancel] theorem sub_eq_zero : a - b = 0 ↔ a = b := ⟨eq_of_sub_eq_zero, λ h, by rw [h, sub_self]⟩ theorem sub_ne_zero : a - b ≠ 0 ↔ a ≠ b := not_congr sub_eq_zero theorem eq_sub_iff_add_eq : a = b - c ↔ a + c = b := eq_add_neg_iff_add_eq theorem sub_eq_iff_eq_add : a - b = c ↔ a = c + b := add_neg_eq_iff_eq_add theorem eq_iff_eq_of_sub_eq_sub {a b c d : α} (H : a - b = c - d) : a = b ↔ c = d := by rw [← sub_eq_zero, H, sub_eq_zero] theorem left_inverse_sub_add_left (c : α) : function.left_inverse (λ x, x - c) (λ x, x + c) := assume x, add_sub_cancel x c theorem left_inverse_add_left_sub (c : α) : function.left_inverse (λ x, x + c) (λ x, x - c) := assume x, sub_add_cancel x c theorem left_inverse_add_right_neg_add (c : α) : function.left_inverse (λ x, c + x) (λ x, - c + x) := assume x, add_neg_cancel_left c x theorem left_inverse_neg_add_add_right (c : α) : function.left_inverse (λ x, - c + x) (λ x, c + x) := assume x, neg_add_cancel_left c x end add_group section add_comm_group variables [add_comm_group α] {a b c : α} lemma sub_eq_neg_add (a b : α) : a - b = -b + a := add_comm _ _ theorem neg_add' (a b : α) : -(a + b) = -a - b := neg_add a b lemma neg_sub_neg (a b : α) : -a - -b = b - a := by simp lemma eq_sub_iff_add_eq' : a = b - c ↔ c + a = b := by rw [eq_sub_iff_add_eq, add_comm] lemma sub_eq_iff_eq_add' : a - b = c ↔ a = b + c := by rw [sub_eq_iff_eq_add, add_comm] lemma add_sub_cancel' (a b : α) : a + b - a = b := by rw [sub_eq_neg_add, neg_add_cancel_left] lemma add_sub_cancel'_right (a b : α) : a + (b - a) = b := by rw [← add_sub_assoc, add_sub_cancel'] lemma sub_right_comm (a b c : α) : a - b - c = a - c - b := add_right_comm _ _ _ lemma sub_add_sub_cancel' (a b c : α) : (a - b) + (c - a) = c - b := by rw add_comm; apply sub_add_sub_cancel lemma sub_sub_sub_cancel_left (a b c : α) : (c - a) - (c - b) = b - a := by rw [← neg_sub b c, sub_neg_eq_add, add_comm, sub_add_sub_cancel] end add_comm_group section is_conj variables [group α] [group β] def is_conj (a b : α) := ∃ c : α, c * a * c⁻¹ = b @[refl] lemma is_conj_refl (a : α) : is_conj a a := ⟨1, by rw [one_mul, one_inv, mul_one]⟩ @[symm] lemma is_conj_symm {a b : α} : is_conj a b → is_conj b a | ⟨c, hc⟩ := ⟨c⁻¹, by rw [← hc, mul_assoc, mul_inv_cancel_right, inv_mul_cancel_left]⟩ @[trans] lemma is_conj_trans {a b c : α} : is_conj a b → is_conj b c → is_conj a c | ⟨c₁, hc₁⟩ ⟨c₂, hc₂⟩ := ⟨c₂ * c₁, by rw [← hc₂, ← hc₁, mul_inv_rev]; simp only [mul_assoc]⟩ @[simp] lemma is_conj_one_right {a : α} : is_conj 1 a ↔ a = 1 := ⟨by simp [is_conj, is_conj_refl] {contextual := tt}, by simp [is_conj_refl] {contextual := tt}⟩ @[simp] lemma is_conj_one_left {a : α} : is_conj a 1 ↔ a = 1 := calc is_conj a 1 ↔ is_conj 1 a : ⟨is_conj_symm, is_conj_symm⟩ ... ↔ a = 1 : is_conj_one_right @[simp] lemma is_conj_iff_eq {α : Type*} [comm_group α] {a b : α} : is_conj a b ↔ a = b := ⟨λ ⟨c, hc⟩, by rw [← hc, mul_right_comm, mul_inv_self, one_mul], λ h, by rw h⟩ end is_conj class is_monoid_hom [monoid α] [monoid β] (f : α → β) : Prop := (map_one : f 1 = 1) (map_mul : ∀ {x y}, f (x * y) = f x * f y) class is_add_monoid_hom [add_monoid α] [add_monoid β] (f : α → β) : Prop := (map_zero : f 0 = 0) (map_add : ∀ {x y}, f (x + y) = f x + f y) attribute [to_additive is_add_monoid_hom] is_monoid_hom attribute [to_additive is_add_monoid_hom.map_add] is_monoid_hom.map_mul attribute [to_additive is_add_monoid_hom.mk] is_monoid_hom.mk namespace is_monoid_hom variables [monoid α] [monoid β] (f : α → β) [is_monoid_hom f] @[to_additive is_add_monoid_hom.id] instance id : is_monoid_hom (@id α) := by refine {..}; intros; refl @[to_additive is_add_monoid_hom.id] instance comp {γ} [monoid γ] (g : β → γ) [is_monoid_hom g] : is_monoid_hom (g ∘ f) := { map_mul := λ x y, show g _ = g _ * g _, by rw [map_mul f, map_mul g], map_one := show g _ = 1, by rw [map_one f, map_one g] } instance is_add_monoid_hom_mul_left {γ : Type*} [semiring γ] (x : γ) : is_add_monoid_hom (λ y : γ, x * y) := by refine_struct {..}; simp [mul_add] instance is_add_monoid_hom_mul_right {γ : Type*} [semiring γ] (x : γ) : is_add_monoid_hom (λ y : γ, y * x) := by refine_struct {..}; simp [add_mul] end is_monoid_hom -- TODO rename fields of is_group_hom: mul ↝ map_mul? /-- Predicate for group homomorphism. -/ class is_group_hom [group α] [group β] (f : α → β) : Prop := (mul : ∀ a b : α, f (a * b) = f a * f b) class is_add_group_hom [add_group α] [add_group β] (f : α → β) : Prop := (add : ∀ a b, f (a + b) = f a + f b) attribute [to_additive is_add_group_hom] is_group_hom attribute [to_additive is_add_group_hom.add] is_group_hom.mul attribute [to_additive is_add_group_hom.mk] is_group_hom.mk instance additive.is_add_group_hom [group α] [group β] (f : α → β) [is_group_hom f] : @is_add_group_hom (additive α) (additive β) _ _ f := ⟨@is_group_hom.mul α β _ _ f _⟩ instance multiplicative.is_group_hom [add_group α] [add_group β] (f : α → β) [is_add_group_hom f] : @is_group_hom (multiplicative α) (multiplicative β) _ _ f := ⟨@is_add_group_hom.add α β _ _ f _⟩ namespace is_group_hom variables [group α] [group β] (f : α → β) [is_group_hom f] @[to_additive is_add_group_hom.zero] theorem one : f 1 = 1 := mul_self_iff_eq_one.1 $ by rw [← mul f, one_mul] @[to_additive is_add_group_hom.neg] theorem inv (a : α) : f a⁻¹ = (f a)⁻¹ := eq_inv_of_mul_eq_one $ by rw [← mul f, inv_mul_self, one f] @[to_additive is_add_group_hom.id] instance id : is_group_hom (@id α) := ⟨λ _ _, rfl⟩ @[to_additive is_add_group_hom.comp] instance comp {γ} [group γ] (g : β → γ) [is_group_hom g] : is_group_hom (g ∘ f) := ⟨λ x y, show g _ = g _ * g _, by rw [mul f, mul g]⟩ protected lemma is_conj (f : α → β) [is_group_hom f] {a b : α} : is_conj a b → is_conj (f a) (f b) | ⟨c, hc⟩ := ⟨f c, by rw [← is_group_hom.mul f, ← is_group_hom.inv f, ← is_group_hom.mul f, hc]⟩ @[to_additive is_add_group_hom.to_is_add_monoid_hom] lemma to_is_monoid_hom (f : α → β) [is_group_hom f] : is_monoid_hom f := ⟨is_group_hom.one f, is_group_hom.mul f⟩ @[to_additive is_add_group_hom.injective_iff] lemma injective_iff (f : α → β) [is_group_hom f] : function.injective f ↔ (∀ a, f a = 1 → a = 1) := ⟨λ h _, by rw ← is_group_hom.one f; exact @h _ _, λ h x y hxy, by rw [← inv_inv (f x), inv_eq_iff_mul_eq_one, ← is_group_hom.inv f, ← is_group_hom.mul f] at hxy; simpa using inv_eq_of_mul_eq_one (h _ hxy)⟩ attribute [instance] is_group_hom.to_is_monoid_hom is_add_group_hom.to_is_add_monoid_hom end is_group_hom @[to_additive is_add_group_hom_add] lemma is_group_hom_mul {α β} [group α] [comm_group β] (f g : α → β) [is_group_hom f] [is_group_hom g] : is_group_hom (λa, f a * g a) := ⟨assume a b, by simp only [is_group_hom.mul f, is_group_hom.mul g, mul_comm, mul_assoc, mul_left_comm]⟩ attribute [instance] is_group_hom_mul is_add_group_hom_add @[to_additive is_add_group_hom_neg] lemma is_group_hom_inv {α β} [group α] [comm_group β] (f : α → β) [is_group_hom f] : is_group_hom (λa, (f a)⁻¹) := ⟨assume a b, by rw [is_group_hom.mul f, mul_inv]⟩ attribute [instance] is_group_hom_inv is_add_group_hom_neg @[to_additive neg.is_add_group_hom] lemma inv.is_group_hom [comm_group α] : is_group_hom (has_inv.inv : α → α) := ⟨by simp [mul_inv_rev, mul_comm]⟩ attribute [instance] inv.is_group_hom neg.is_add_group_hom /-- Predicate for group anti-homomorphism, or a homomorphism into the opposite group. -/ class is_group_anti_hom {β : Type*} [group α] [group β] (f : α → β) : Prop := (mul : ∀ a b : α, f (a * b) = f b * f a) namespace is_group_anti_hom variables [group α] [group β] (f : α → β) [w : is_group_anti_hom f] include w theorem one : f 1 = 1 := mul_self_iff_eq_one.1 $ by rw [← mul f, one_mul] theorem inv (a : α) : f a⁻¹ = (f a)⁻¹ := eq_inv_of_mul_eq_one $ by rw [← mul f, mul_inv_self, one f] end is_group_anti_hom theorem inv_is_group_anti_hom [group α] : is_group_anti_hom (λ x : α, x⁻¹) := ⟨mul_inv_rev⟩ namespace is_add_group_hom variables [add_group α] [add_group β] (f : α → β) [is_add_group_hom f] lemma sub (a b) : f (a - b) = f a - f b := calc f (a - b) = f (a + -b) : rfl ... = f a + f (-b) : add f _ _ ... = f a - f b : by simp[neg f] end is_add_group_hom lemma is_add_group_hom_sub {α β} [add_group α] [add_comm_group β] (f g : α → β) [is_add_group_hom f] [is_add_group_hom g] : is_add_group_hom (λa, f a - g a) := is_add_group_hom_add f (λa, - g a) attribute [instance] is_add_group_hom_sub namespace units variables [monoid α] [monoid β] (f : α → β) [is_monoid_hom f] definition map : units α → units β := λ u, ⟨f u.val, f u.inv, by rw [← is_monoid_hom.map_mul f, u.val_inv, is_monoid_hom.map_one f], by rw [← is_monoid_hom.map_mul f, u.inv_val, is_monoid_hom.map_one f] ⟩ instance : is_group_hom (units.map f) := ⟨λ a b, by ext; exact is_monoid_hom.map_mul f ⟩ instance : is_monoid_hom (coe : units α → α) := ⟨by simp, by simp⟩ end units
24338526250c263986c084c8a4843dbd0ab0f4d4
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/interactive/run.lean
b640fe3399e95793ca0cb59d7494a28c5ddf3010
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
leanprover/lean4
4bdf9790294964627eb9be79f5e8f6157780b4cc
f1f9dc0f2f531af3312398999d8b8303fa5f096b
refs/heads/master
1,693,360,665,786
1,693,350,868,000
1,693,350,868,000
129,571,436
2,827
311
Apache-2.0
1,694,716,156,000
1,523,760,560,000
Lean
UTF-8
Lean
false
false
7,482
lean
import Lean.Data.Lsp import Lean.Widget open Lean open Lean.Lsp open Lean.JsonRpc namespace Client /- Client-side types for showing interactive goals. -/ structure SubexprInfo where subexprPos : String diffStatus? : Option String deriving FromJson, Repr structure Hyp where type : Widget.TaggedText SubexprInfo names : Array String isInserted?: Option Bool isRemoved?: Option Bool deriving FromJson, Repr structure InteractiveGoal where type : Widget.TaggedText SubexprInfo isInserted?: Option Bool := none isRemoved?: Option Bool := none hyps : Array Hyp deriving FromJson, Repr structure InteractiveGoals where goals : Array InteractiveGoal deriving FromJson, Repr end Client def word : Parsec String := Parsec.many1Chars <| Parsec.digit <|> Parsec.asciiLetter <|> Parsec.pchar '_' def ident : Parsec Name := do let head ← word let xs ← Parsec.many1 (Parsec.pchar '.' *> word) return xs.foldl Name.mkStr $ head partial def main (args : List String) : IO Unit := do let uri := s!"file://{args.head!}" Ipc.runWith (←IO.appPath) #["--server"] do let capabilities := { textDocument? := some { completion? := some { completionItem? := some { insertReplaceSupport? := true } } } } Ipc.writeRequest ⟨0, "initialize", { capabilities : InitializeParams }⟩ let _ ← Ipc.readResponseAs 0 InitializeResult Ipc.writeNotification ⟨"initialized", InitializedParams.mk⟩ let text ← IO.FS.readFile args.head! Ipc.writeNotification ⟨"textDocument/didOpen", { textDocument := { uri := uri, languageId := "lean", version := 1, text := text } : DidOpenTextDocumentParams }⟩ let _ ← Ipc.collectDiagnostics 1 uri 1 let mut lineNo := 0 let mut lastActualLineNo := 0 let mut versionNo : Nat := 2 let mut requestNo : Nat := 2 let mut rpcSessionId : Option UInt64 := none for line in text.splitOn "\n" do match line.splitOn "--" with | [ws, directive] => let line ← match directive.front with | 'v' => pure <| lineNo + 1 -- TODO: support subsequent 'v'... or not | '^' => pure <| lastActualLineNo | _ => lastActualLineNo := lineNo lineNo := lineNo + 1 continue let directive := directive.drop 1 let colon := directive.posOf ':' let method := directive.extract 0 colon |>.trim -- TODO: correctly compute in presence of Unicode let column := ws.endPos + "--" let pos : Lsp.Position := { line := line, character := column.byteIdx } let params := if colon < directive.endPos then directive.extract (colon + ':') directive.endPos |>.trim else "{}" match method with | "insert" => let params : DidChangeTextDocumentParams := { textDocument := { uri := uri version? := versionNo } contentChanges := #[TextDocumentContentChangeEvent.rangeChange { start := pos «end» := { pos with character := pos.character + params.endPos.byteIdx } } params] } let params := toJson params IO.eprintln params Ipc.writeNotification ⟨"textDocument/didChange", params⟩ -- We don't want to wait for changes to be processed so we can test concurrency --let _ ← Ipc.collectDiagnostics requestNo uri versionNo requestNo := requestNo + 1 versionNo := versionNo + 1 | "collectDiagnostics" => let diags ← Ipc.collectDiagnostics requestNo uri (versionNo - 1) for diag in diags do IO.eprintln (toJson diag.param) requestNo := requestNo + 1 | "codeAction" => let params : CodeActionParams := { textDocument := {uri := uri}, range := ⟨pos, pos⟩ } Ipc.writeRequest ⟨requestNo, "textDocument/codeAction", params⟩ let r ← Ipc.readResponseAs requestNo (Array Json) for x in r.result do IO.eprintln x requestNo := requestNo + 1 | "goals" => if rpcSessionId.isNone then Ipc.writeRequest ⟨requestNo, "$/lean/rpc/connect", RpcConnectParams.mk uri⟩ let r ← Ipc.readResponseAs requestNo RpcConnected rpcSessionId := some r.result.sessionId requestNo := requestNo + 1 let params : Lsp.PlainGoalParams := { textDocument := { uri } position := pos, } let ps : RpcCallParams := { params := toJson params textDocument := { uri } position := pos, sessionId := rpcSessionId.get!, method := `Lean.Widget.getInteractiveGoals } Ipc.writeRequest ⟨requestNo, "$/lean/rpc/call", ps⟩ let response ← Ipc.readResponseAs requestNo Client.InteractiveGoals requestNo := requestNo + 1 IO.eprintln (repr response.result) IO.eprintln "" | "widgets" => if rpcSessionId.isNone then Ipc.writeRequest ⟨requestNo, "$/lean/rpc/connect", RpcConnectParams.mk uri⟩ let r ← Ipc.readResponseAs requestNo RpcConnected rpcSessionId := some r.result.sessionId requestNo := requestNo + 1 let ps : RpcCallParams := { textDocument := {uri := uri}, position := pos, sessionId := rpcSessionId.get!, method := `Lean.Widget.getWidgets, params := toJson pos, } Ipc.writeRequest ⟨requestNo, "$/lean/rpc/call", ps⟩ let response ← Ipc.readResponseAs requestNo Lean.Widget.GetWidgetsResponse requestNo := requestNo + 1 IO.eprintln (toJson response.result) for w in response.result.widgets do let params : Lean.Widget.GetWidgetSourceParams := { pos, hash := w.javascriptHash } let ps : RpcCallParams := { ps with method := `Lean.Widget.getWidgetSource, params := toJson params, } Ipc.writeRequest ⟨requestNo, "$/lean/rpc/call", ps⟩ let resp ← Ipc.readResponseAs requestNo Lean.Widget.WidgetSource IO.eprintln (toJson resp.result) requestNo := requestNo + 1 | _ => let Except.ok params ← pure <| Json.parse params | throw <| IO.userError s!"failed to parse {params}" let params := params.setObjVal! "textDocument" (toJson { uri := uri : TextDocumentIdentifier }) -- TODO: correctly compute in presence of Unicode let params := params.setObjVal! "position" (toJson pos) IO.eprintln params Ipc.writeRequest ⟨requestNo, method, params⟩ let rec readFirstResponse := do match ← Ipc.readMessage with | Message.response id r => assert! id == requestNo return r | Message.notification .. => readFirstResponse | msg => throw <| IO.userError s!"unexpected message {toJson msg}" let resp ← readFirstResponse IO.eprintln resp requestNo := requestNo + 1 | _ => lastActualLineNo := lineNo lineNo := lineNo + 1 Ipc.shutdown requestNo discard <| Ipc.waitForExit
2236ecd89a9d8add43673d3d31affea025641a9f
d450724ba99f5b50b57d244eb41fef9f6789db81
/src/instructor/lectures/lecture_5.lean
8d85ec164a16afee88bbd4c48436785704810322
[]
no_license
jakekauff/CS2120F21
4f009adeb4ce4a148442b562196d66cc6c04530c
e69529ec6f5d47a554291c4241a3d8ec4fe8f5ad
refs/heads/main
1,693,841,880,030
1,637,604,848,000
1,637,604,848,000
399,946,698
0
0
null
null
null
null
UTF-8
Lean
false
false
4,509
lean
/- INTRODUCTION and ELIMINATION RULES -/ /- For ∀ x, P x (every x has property P) - introduction rule: assume arbitrary x, then show P x - elimination rule: *apply* a proof of ∀ x, P x, as a kind of function to a specific value of x, say k, to produce a proof of P k. -/ /- Now we need a short detour, so as to understand the examples of the preceding point that we're about to present. On this detour, we meet the proposition, true. As an AXIOM of our logic, we now accept that the proposition true is always logically true. That means that there has to be a proof of true. By means we don't explain yet, the definition of our logic in Lean gives us the axiom (true.intro : true). I.e., true.intro is a proof of true. It's always defined, and so we can use it at anytime to prove the propositions, true. That in fact is the introduction rule for true: that true has a proof (and is thus invariably logically true). In the end, a proof of true is worthless because it carries no information at all, not even one binary digit (bit). There is thus no use for an elimination rule for true, and there isn't one. Summary. If we're really being rigorous we'd say there are actually two axioms for true: first it is a proposition; second, there is a proof of it (so it is logically true without any conditions). There is no elimination rule for true. -/ /- Please now merge back onto the yellow brick road. -/ /- What that detour on the proposition true and its truth, and proof, values, we can now give a very simple example of a "forall proposition", and both the construction and the use of a proof of such a proposition. -/ /- Let's first conjecture that no matter what natural number we might be given, our only obligation is to return a proof of true. It's an almost Alice-in-Wonderland idea. And of course it's true. You just ignore the value that you're given and invariable return the value, true.intro. Here then is the formal statement for which we'll first construct and the use a proof of this proposition: ∀ (n : ℕ), true. -/ theorem silly : ∀ (n : ℕ), true := begin assume (n : ℕ), -- ∀ introduction exact true.intro, -- true.introduction end /- Having produced a proof, and bound it as the value of the identifier, silly, we can now *use* the proof by applying it (by name) to any value of the right type (here ℕ) as an argument, and then getting a proof of true as a return value. -/ #reduce silly 7 /- The check command will tell you the type of any expression (aka term) in Lean. Here we can see that silly is like a function, and that when we apply it to the specific argument, 7, we get back a proof of the resulting proposition (which is just, "true"). We'll soon be equipped to deal with more interesting "return types". -/ /- For P → Q (if P is true then Q must also be true) - introduction rule: assume arbitrary P, then show Q - elimination rule: *apply* a proof of P → Q, as a kind of function, to any proof of P to derive a proof of Q! -/ lemma foo : ∀ (x : ℕ), x = 0 → x + 1 = 1 := begin assume x h, rw h, end /- Wow! ∀ and → sure do seem similar. Indeed they're the same! They define function types. We construct a proof of ∀ or → by assuming the premise and showing that in that context we can derive a result of the conclusion type. We can then use a proof of a ∀ or → by treating it as a function that can be applied to a specific value to derive a proof *for that specific value. Indeed, in Lean, → is really just another notation for forall! -/ /- Introduction rule for and ∧ Give a proof of P and a proof of Q get back a proof of (P ∧ Q). -/ axioms (P Q : Prop) #check P #check (P ∧ Q) axioms (p :P) (q : Q) example : P ∧ Q := and.intro p q /- Prove that if arbitary propositions P and Q are true (which is to say that we have a proof of each of them), that the proposition P ∧ Q is also true. Proof: The conjecture that P ∧ Q is true is proved by application the introduction rule for and. -/ example : 0 = 0 ∧ 1 = 1 := begin apply and.intro _ _, apply eq.refl 0, apply eq.refl 1, end theorem bar : 0 = 0 ∧ 1 = 1 := begin apply and.intro (eq.refl 0) (eq.refl 1), end #check bar #check and.elim_left bar #check and.elim_right bar /- Now we have a major theorem. -/ theorem and_commutative : ∀ (P Q : Prop), P ∧ Q → Q ∧ P := begin assume P Q h, apply and.intro _ _, apply and.elim_right h, apply and.elim_left h, end
c40ef8b50e8a924efe84389c6452a25ab4116c27
592ee40978ac7604005a4e0d35bbc4b467389241
/Library/generated/mathscheme-lean/RingoidWithAddAntiDistrib.lean
03a4b80f8160c8f18c6d31f0a2c510894ca90674
[]
no_license
ysharoda/Deriving-Definitions
3e149e6641fae440badd35ac110a0bd705a49ad2
dfecb27572022de3d4aa702cae8db19957523a59
refs/heads/master
1,679,127,857,700
1,615,939,007,000
1,615,939,007,000
229,785,731
4
0
null
null
null
null
UTF-8
Lean
false
false
12,905
lean
import init.data.nat.basic import init.data.fin.basic import data.vector import .Prelude open Staged open nat open fin open vector section RingoidWithAddAntiDistrib structure RingoidWithAddAntiDistrib (A : Type) : Type := (plus : (A → (A → A))) (prim : (A → A)) (antidis_prim_plus : (∀ {x y : A} , (prim (plus x y)) = (plus (prim y) (prim x)))) (times : (A → (A → A))) (leftDistributive_times_plus : (∀ {x y z : A} , (times x (plus y z)) = (plus (times x y) (times x z)))) (rightDistributive_times_plus : (∀ {x y z : A} , (times (plus y z) x) = (plus (times y x) (times z x)))) open RingoidWithAddAntiDistrib structure Sig (AS : Type) : Type := (plusS : (AS → (AS → AS))) (primS : (AS → AS)) (timesS : (AS → (AS → AS))) structure Product (A : Type) : Type := (plusP : ((Prod A A) → ((Prod A A) → (Prod A A)))) (primP : ((Prod A A) → (Prod A A))) (timesP : ((Prod A A) → ((Prod A A) → (Prod A A)))) (antidis_prim_plusP : (∀ {xP yP : (Prod A A)} , (primP (plusP xP yP)) = (plusP (primP yP) (primP xP)))) (leftDistributive_times_plusP : (∀ {xP yP zP : (Prod A A)} , (timesP xP (plusP yP zP)) = (plusP (timesP xP yP) (timesP xP zP)))) (rightDistributive_times_plusP : (∀ {xP yP zP : (Prod A A)} , (timesP (plusP yP zP) xP) = (plusP (timesP yP xP) (timesP zP xP)))) structure Hom {A1 : Type} {A2 : Type} (Ri1 : (RingoidWithAddAntiDistrib A1)) (Ri2 : (RingoidWithAddAntiDistrib A2)) : Type := (hom : (A1 → A2)) (pres_plus : (∀ {x1 x2 : A1} , (hom ((plus Ri1) x1 x2)) = ((plus Ri2) (hom x1) (hom x2)))) (pres_prim : (∀ {x1 : A1} , (hom ((prim Ri1) x1)) = ((prim Ri2) (hom x1)))) (pres_times : (∀ {x1 x2 : A1} , (hom ((times Ri1) x1 x2)) = ((times Ri2) (hom x1) (hom x2)))) structure RelInterp {A1 : Type} {A2 : Type} (Ri1 : (RingoidWithAddAntiDistrib A1)) (Ri2 : (RingoidWithAddAntiDistrib A2)) : Type 1 := (interp : (A1 → (A2 → Type))) (interp_plus : (∀ {x1 x2 : A1} {y1 y2 : A2} , ((interp x1 y1) → ((interp x2 y2) → (interp ((plus Ri1) x1 x2) ((plus Ri2) y1 y2)))))) (interp_prim : (∀ {x1 : A1} {y1 : A2} , ((interp x1 y1) → (interp ((prim Ri1) x1) ((prim Ri2) y1))))) (interp_times : (∀ {x1 x2 : A1} {y1 y2 : A2} , ((interp x1 y1) → ((interp x2 y2) → (interp ((times Ri1) x1 x2) ((times Ri2) y1 y2)))))) inductive RingoidWithAddAntiDistribTerm : Type | plusL : (RingoidWithAddAntiDistribTerm → (RingoidWithAddAntiDistribTerm → RingoidWithAddAntiDistribTerm)) | primL : (RingoidWithAddAntiDistribTerm → RingoidWithAddAntiDistribTerm) | timesL : (RingoidWithAddAntiDistribTerm → (RingoidWithAddAntiDistribTerm → RingoidWithAddAntiDistribTerm)) open RingoidWithAddAntiDistribTerm inductive ClRingoidWithAddAntiDistribTerm (A : Type) : Type | sing : (A → ClRingoidWithAddAntiDistribTerm) | plusCl : (ClRingoidWithAddAntiDistribTerm → (ClRingoidWithAddAntiDistribTerm → ClRingoidWithAddAntiDistribTerm)) | primCl : (ClRingoidWithAddAntiDistribTerm → ClRingoidWithAddAntiDistribTerm) | timesCl : (ClRingoidWithAddAntiDistribTerm → (ClRingoidWithAddAntiDistribTerm → ClRingoidWithAddAntiDistribTerm)) open ClRingoidWithAddAntiDistribTerm inductive OpRingoidWithAddAntiDistribTerm (n : ℕ) : Type | v : ((fin n) → OpRingoidWithAddAntiDistribTerm) | plusOL : (OpRingoidWithAddAntiDistribTerm → (OpRingoidWithAddAntiDistribTerm → OpRingoidWithAddAntiDistribTerm)) | primOL : (OpRingoidWithAddAntiDistribTerm → OpRingoidWithAddAntiDistribTerm) | timesOL : (OpRingoidWithAddAntiDistribTerm → (OpRingoidWithAddAntiDistribTerm → OpRingoidWithAddAntiDistribTerm)) open OpRingoidWithAddAntiDistribTerm inductive OpRingoidWithAddAntiDistribTerm2 (n : ℕ) (A : Type) : Type | v2 : ((fin n) → OpRingoidWithAddAntiDistribTerm2) | sing2 : (A → OpRingoidWithAddAntiDistribTerm2) | plusOL2 : (OpRingoidWithAddAntiDistribTerm2 → (OpRingoidWithAddAntiDistribTerm2 → OpRingoidWithAddAntiDistribTerm2)) | primOL2 : (OpRingoidWithAddAntiDistribTerm2 → OpRingoidWithAddAntiDistribTerm2) | timesOL2 : (OpRingoidWithAddAntiDistribTerm2 → (OpRingoidWithAddAntiDistribTerm2 → OpRingoidWithAddAntiDistribTerm2)) open OpRingoidWithAddAntiDistribTerm2 def simplifyCl {A : Type} : ((ClRingoidWithAddAntiDistribTerm A) → (ClRingoidWithAddAntiDistribTerm A)) | (plusCl (primCl y) (primCl x)) := (primCl (plusCl x y)) | (plusCl x1 x2) := (plusCl (simplifyCl x1) (simplifyCl x2)) | (primCl x1) := (primCl (simplifyCl x1)) | (timesCl x1 x2) := (timesCl (simplifyCl x1) (simplifyCl x2)) | (sing x1) := (sing x1) def simplifyOpB {n : ℕ} : ((OpRingoidWithAddAntiDistribTerm n) → (OpRingoidWithAddAntiDistribTerm n)) | (plusOL (primOL y) (primOL x)) := (primOL (plusOL x y)) | (plusOL x1 x2) := (plusOL (simplifyOpB x1) (simplifyOpB x2)) | (primOL x1) := (primOL (simplifyOpB x1)) | (timesOL x1 x2) := (timesOL (simplifyOpB x1) (simplifyOpB x2)) | (v x1) := (v x1) def simplifyOp {n : ℕ} {A : Type} : ((OpRingoidWithAddAntiDistribTerm2 n A) → (OpRingoidWithAddAntiDistribTerm2 n A)) | (plusOL2 (primOL2 y) (primOL2 x)) := (primOL2 (plusOL2 x y)) | (plusOL2 x1 x2) := (plusOL2 (simplifyOp x1) (simplifyOp x2)) | (primOL2 x1) := (primOL2 (simplifyOp x1)) | (timesOL2 x1 x2) := (timesOL2 (simplifyOp x1) (simplifyOp x2)) | (v2 x1) := (v2 x1) | (sing2 x1) := (sing2 x1) def evalB {A : Type} : ((RingoidWithAddAntiDistrib A) → (RingoidWithAddAntiDistribTerm → A)) | Ri (plusL x1 x2) := ((plus Ri) (evalB Ri x1) (evalB Ri x2)) | Ri (primL x1) := ((prim Ri) (evalB Ri x1)) | Ri (timesL x1 x2) := ((times Ri) (evalB Ri x1) (evalB Ri x2)) def evalCl {A : Type} : ((RingoidWithAddAntiDistrib A) → ((ClRingoidWithAddAntiDistribTerm A) → A)) | Ri (sing x1) := x1 | Ri (plusCl x1 x2) := ((plus Ri) (evalCl Ri x1) (evalCl Ri x2)) | Ri (primCl x1) := ((prim Ri) (evalCl Ri x1)) | Ri (timesCl x1 x2) := ((times Ri) (evalCl Ri x1) (evalCl Ri x2)) def evalOpB {A : Type} {n : ℕ} : ((RingoidWithAddAntiDistrib A) → ((vector A n) → ((OpRingoidWithAddAntiDistribTerm n) → A))) | Ri vars (v x1) := (nth vars x1) | Ri vars (plusOL x1 x2) := ((plus Ri) (evalOpB Ri vars x1) (evalOpB Ri vars x2)) | Ri vars (primOL x1) := ((prim Ri) (evalOpB Ri vars x1)) | Ri vars (timesOL x1 x2) := ((times Ri) (evalOpB Ri vars x1) (evalOpB Ri vars x2)) def evalOp {A : Type} {n : ℕ} : ((RingoidWithAddAntiDistrib A) → ((vector A n) → ((OpRingoidWithAddAntiDistribTerm2 n A) → A))) | Ri vars (v2 x1) := (nth vars x1) | Ri vars (sing2 x1) := x1 | Ri vars (plusOL2 x1 x2) := ((plus Ri) (evalOp Ri vars x1) (evalOp Ri vars x2)) | Ri vars (primOL2 x1) := ((prim Ri) (evalOp Ri vars x1)) | Ri vars (timesOL2 x1 x2) := ((times Ri) (evalOp Ri vars x1) (evalOp Ri vars x2)) def inductionB {P : (RingoidWithAddAntiDistribTerm → Type)} : ((∀ (x1 x2 : RingoidWithAddAntiDistribTerm) , ((P x1) → ((P x2) → (P (plusL x1 x2))))) → ((∀ (x1 : RingoidWithAddAntiDistribTerm) , ((P x1) → (P (primL x1)))) → ((∀ (x1 x2 : RingoidWithAddAntiDistribTerm) , ((P x1) → ((P x2) → (P (timesL x1 x2))))) → (∀ (x : RingoidWithAddAntiDistribTerm) , (P x))))) | pplusl ppriml ptimesl (plusL x1 x2) := (pplusl _ _ (inductionB pplusl ppriml ptimesl x1) (inductionB pplusl ppriml ptimesl x2)) | pplusl ppriml ptimesl (primL x1) := (ppriml _ (inductionB pplusl ppriml ptimesl x1)) | pplusl ppriml ptimesl (timesL x1 x2) := (ptimesl _ _ (inductionB pplusl ppriml ptimesl x1) (inductionB pplusl ppriml ptimesl x2)) def inductionCl {A : Type} {P : ((ClRingoidWithAddAntiDistribTerm A) → Type)} : ((∀ (x1 : A) , (P (sing x1))) → ((∀ (x1 x2 : (ClRingoidWithAddAntiDistribTerm A)) , ((P x1) → ((P x2) → (P (plusCl x1 x2))))) → ((∀ (x1 : (ClRingoidWithAddAntiDistribTerm A)) , ((P x1) → (P (primCl x1)))) → ((∀ (x1 x2 : (ClRingoidWithAddAntiDistribTerm A)) , ((P x1) → ((P x2) → (P (timesCl x1 x2))))) → (∀ (x : (ClRingoidWithAddAntiDistribTerm A)) , (P x)))))) | psing ppluscl pprimcl ptimescl (sing x1) := (psing x1) | psing ppluscl pprimcl ptimescl (plusCl x1 x2) := (ppluscl _ _ (inductionCl psing ppluscl pprimcl ptimescl x1) (inductionCl psing ppluscl pprimcl ptimescl x2)) | psing ppluscl pprimcl ptimescl (primCl x1) := (pprimcl _ (inductionCl psing ppluscl pprimcl ptimescl x1)) | psing ppluscl pprimcl ptimescl (timesCl x1 x2) := (ptimescl _ _ (inductionCl psing ppluscl pprimcl ptimescl x1) (inductionCl psing ppluscl pprimcl ptimescl x2)) def inductionOpB {n : ℕ} {P : ((OpRingoidWithAddAntiDistribTerm n) → Type)} : ((∀ (fin : (fin n)) , (P (v fin))) → ((∀ (x1 x2 : (OpRingoidWithAddAntiDistribTerm n)) , ((P x1) → ((P x2) → (P (plusOL x1 x2))))) → ((∀ (x1 : (OpRingoidWithAddAntiDistribTerm n)) , ((P x1) → (P (primOL x1)))) → ((∀ (x1 x2 : (OpRingoidWithAddAntiDistribTerm n)) , ((P x1) → ((P x2) → (P (timesOL x1 x2))))) → (∀ (x : (OpRingoidWithAddAntiDistribTerm n)) , (P x)))))) | pv pplusol pprimol ptimesol (v x1) := (pv x1) | pv pplusol pprimol ptimesol (plusOL x1 x2) := (pplusol _ _ (inductionOpB pv pplusol pprimol ptimesol x1) (inductionOpB pv pplusol pprimol ptimesol x2)) | pv pplusol pprimol ptimesol (primOL x1) := (pprimol _ (inductionOpB pv pplusol pprimol ptimesol x1)) | pv pplusol pprimol ptimesol (timesOL x1 x2) := (ptimesol _ _ (inductionOpB pv pplusol pprimol ptimesol x1) (inductionOpB pv pplusol pprimol ptimesol x2)) def inductionOp {n : ℕ} {A : Type} {P : ((OpRingoidWithAddAntiDistribTerm2 n A) → Type)} : ((∀ (fin : (fin n)) , (P (v2 fin))) → ((∀ (x1 : A) , (P (sing2 x1))) → ((∀ (x1 x2 : (OpRingoidWithAddAntiDistribTerm2 n A)) , ((P x1) → ((P x2) → (P (plusOL2 x1 x2))))) → ((∀ (x1 : (OpRingoidWithAddAntiDistribTerm2 n A)) , ((P x1) → (P (primOL2 x1)))) → ((∀ (x1 x2 : (OpRingoidWithAddAntiDistribTerm2 n A)) , ((P x1) → ((P x2) → (P (timesOL2 x1 x2))))) → (∀ (x : (OpRingoidWithAddAntiDistribTerm2 n A)) , (P x))))))) | pv2 psing2 pplusol2 pprimol2 ptimesol2 (v2 x1) := (pv2 x1) | pv2 psing2 pplusol2 pprimol2 ptimesol2 (sing2 x1) := (psing2 x1) | pv2 psing2 pplusol2 pprimol2 ptimesol2 (plusOL2 x1 x2) := (pplusol2 _ _ (inductionOp pv2 psing2 pplusol2 pprimol2 ptimesol2 x1) (inductionOp pv2 psing2 pplusol2 pprimol2 ptimesol2 x2)) | pv2 psing2 pplusol2 pprimol2 ptimesol2 (primOL2 x1) := (pprimol2 _ (inductionOp pv2 psing2 pplusol2 pprimol2 ptimesol2 x1)) | pv2 psing2 pplusol2 pprimol2 ptimesol2 (timesOL2 x1 x2) := (ptimesol2 _ _ (inductionOp pv2 psing2 pplusol2 pprimol2 ptimesol2 x1) (inductionOp pv2 psing2 pplusol2 pprimol2 ptimesol2 x2)) def stageB : (RingoidWithAddAntiDistribTerm → (Staged RingoidWithAddAntiDistribTerm)) | (plusL x1 x2) := (stage2 plusL (codeLift2 plusL) (stageB x1) (stageB x2)) | (primL x1) := (stage1 primL (codeLift1 primL) (stageB x1)) | (timesL x1 x2) := (stage2 timesL (codeLift2 timesL) (stageB x1) (stageB x2)) def stageCl {A : Type} : ((ClRingoidWithAddAntiDistribTerm A) → (Staged (ClRingoidWithAddAntiDistribTerm A))) | (sing x1) := (Now (sing x1)) | (plusCl x1 x2) := (stage2 plusCl (codeLift2 plusCl) (stageCl x1) (stageCl x2)) | (primCl x1) := (stage1 primCl (codeLift1 primCl) (stageCl x1)) | (timesCl x1 x2) := (stage2 timesCl (codeLift2 timesCl) (stageCl x1) (stageCl x2)) def stageOpB {n : ℕ} : ((OpRingoidWithAddAntiDistribTerm n) → (Staged (OpRingoidWithAddAntiDistribTerm n))) | (v x1) := (const (code (v x1))) | (plusOL x1 x2) := (stage2 plusOL (codeLift2 plusOL) (stageOpB x1) (stageOpB x2)) | (primOL x1) := (stage1 primOL (codeLift1 primOL) (stageOpB x1)) | (timesOL x1 x2) := (stage2 timesOL (codeLift2 timesOL) (stageOpB x1) (stageOpB x2)) def stageOp {n : ℕ} {A : Type} : ((OpRingoidWithAddAntiDistribTerm2 n A) → (Staged (OpRingoidWithAddAntiDistribTerm2 n A))) | (sing2 x1) := (Now (sing2 x1)) | (v2 x1) := (const (code (v2 x1))) | (plusOL2 x1 x2) := (stage2 plusOL2 (codeLift2 plusOL2) (stageOp x1) (stageOp x2)) | (primOL2 x1) := (stage1 primOL2 (codeLift1 primOL2) (stageOp x1)) | (timesOL2 x1 x2) := (stage2 timesOL2 (codeLift2 timesOL2) (stageOp x1) (stageOp x2)) structure StagedRepr (A : Type) (Repr : (Type → Type)) : Type := (plusT : ((Repr A) → ((Repr A) → (Repr A)))) (primT : ((Repr A) → (Repr A))) (timesT : ((Repr A) → ((Repr A) → (Repr A)))) end RingoidWithAddAntiDistrib
69a672c9a8166155d820ab0407940f0f9116f50c
626e312b5c1cb2d88fca108f5933076012633192
/src/data/option/basic.lean
81e50356db688fb3373b2ba7b5e7570a3575b3b6
[ "Apache-2.0" ]
permissive
Bioye97/mathlib
9db2f9ee54418d29dd06996279ba9dc874fd6beb
782a20a27ee83b523f801ff34efb1a9557085019
refs/heads/master
1,690,305,956,488
1,631,067,774,000
1,631,067,774,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
17,165
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 end option
2bd1b032d6a5cff6b9a79472fc2ad705002cb905
9dd3f3912f7321eb58ee9aa8f21778ad6221f87c
/tests/lean/run/basic.lean
63c25112ae4e37da7e9379bf89f60a7e8ba9eddc
[ "Apache-2.0" ]
permissive
bre7k30/lean
de893411bcfa7b3c5572e61b9e1c52951b310aa4
5a924699d076dab1bd5af23a8f910b433e598d7a
refs/heads/master
1,610,900,145,817
1,488,006,845,000
1,488,006,845,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,793
lean
prelude constant {l1 l2} A : Type l1 → Type l2 check A definition {l} tst (A : Type*) (B : Type*) (C : Type l) : Type* := A → B → C check tst constant {l} group : Type (l+1) constant {l} carrier : group.{l} → Type l noncomputable definition to_carrier (g : group) := carrier g check to_carrier.{1} section variable A : Type* check A definition B := A → A end constant N : Type 1 check B N constant f : B N check f constant a : N check f a section variable T1 : Type* variable T2 : Type* variable f : T1 → T2 → T2 definition double (a : T1) (b : T2) := f a (f a b) end check double check double.{1 2} definition Prop := Type 0 constant Eq : Π {A : Type*}, A → A → Prop infix `=`:50 := Eq check Eq.{1} section universe variable l universe variable u variable {T1 : Type l} variable {T2 : Type l} variable {T3 : Type u} variable f : T1 → T2 → T2 noncomputable definition is_proj2 := ∀ x y, f x y = y noncomputable definition is_proj3 (f : T1 → T2 → T3 → T3) := ∀ x y z, f x y z = z end check @is_proj2.{1} check @is_proj3.{1 2} namespace foo section universe variables u v variables {T1 T2 : Type u} variable {T3 : Type v} variable f : T1 → T2 → T2 noncomputable definition is_proj2 := ∀ x y, f x y = y noncomputable definition is_proj3 (f : T1 → T2 → T3 → T3) := ∀ x y z, f x y z = z end check @foo.is_proj2.{1} check @foo.is_proj3.{1 2} end foo namespace bla section variable {T1 : Type*} variable {T2 : Type*} variable {T3 : Type*} variable f : T1 → T2 → T2 noncomputable definition is_proj2 := ∀ x y, f x y = y noncomputable definition is_proj3 (f : T1 → T2 → T3 → T3) := ∀ x y z, f x y z = z end check @bla.is_proj2.{1 2} check @bla.is_proj3.{1 2 3} end bla
bab149057916bf63b08eab85811070bb8e785ab5
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/nestedrec.lean
0f7f1b500c20f1e259ce1d653aca5b5510695d29
[ "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
261
lean
def f : Nat → Nat → Nat | 0, b => b+1 | a+1, b => f a (f a b) theorem ex1 (b) : f 0 b = b+1 := rfl theorem ex2 (b) : f 1 b = (b+1)+1 := rfl theorem ex3 (b) : f 2 b = b+1+1+1+1 := rfl theorem ex4 (a b) : f (a+1) b = f a (f a b) := rfl #eval f 2 5
672b846aeab8fcdbce485f8e002293bac06cece5
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/algebra/char_p/pi_auto.lean
f987ed9c6e7f43a0855ce9e40aab68e1c09d7975
[]
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
742
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 Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.algebra.char_p.basic import Mathlib.algebra.ring.pi import Mathlib.PostPort universes u v namespace Mathlib /-! # Characteristic of semirings of functions -/ namespace char_p protected instance pi (ι : Type u) [hi : Nonempty ι] (R : Type v) [semiring R] (p : ℕ) [char_p R p] : char_p (ι → R) p := mk fun (x : ℕ) => sorry -- diamonds protected instance pi' (ι : Type u) [hi : Nonempty ι] (R : Type v) [comm_ring R] (p : ℕ) [char_p R p] : char_p (ι → R) p := char_p.pi ι R p end Mathlib
04f8c494dfea832398db0b5a612382e7e2a5229c
41ebf3cb010344adfa84907b3304db00e02db0a6
/uexp/src/uexp/rules/transitiveInferenceConstantEquiPredicate.lean
79a82b647c59a0e8468ac83ceaaa566e839996f3
[ "BSD-2-Clause" ]
permissive
ReinierKoops/Cosette
e061b2ba58b26f4eddf4cd052dcf7abd16dfe8fb
eb8dadd06ee05fe7b6b99de431dd7c4faef5cb29
refs/heads/master
1,686,483,953,198
1,624,293,498,000
1,624,293,498,000
378,997,885
0
0
BSD-2-Clause
1,624,293,485,000
1,624,293,484,000
null
UTF-8
Lean
false
false
1,908
lean
import ..sql import ..tactics import ..u_semiring import ..extra_constants import ..meta.ucongr import ..meta.TDP set_option profiler true open Expr open Proj open Pred open SQL open tree notation `int` := datatypes.int variable integer_1: const datatypes.int theorem rule: forall ( Γ scm_t scm_account scm_bonus scm_dept scm_emp: Schema) (rel_t: relation scm_t) (rel_account: relation scm_account) (rel_bonus: relation scm_bonus) (rel_dept: relation scm_dept) (rel_emp: relation scm_emp) (t_k0 : Column int scm_t) (t_c1 : Column int scm_t) (t_f1_a0 : Column int scm_t) (t_f2_a0 : Column int scm_t) (t_f0_c0 : Column int scm_t) (t_f1_c0 : Column int scm_t) (t_f0_c1 : Column int scm_t) (t_f1_c2 : Column int scm_t) (t_f2_c3 : Column int scm_t) (account_acctno : Column int scm_account) (account_type : Column int scm_account) (account_balance : Column int scm_account) (bonus_ename : Column int scm_bonus) (bonus_job : Column int scm_bonus) (bonus_sal : Column int scm_bonus) (bonus_comm : Column int scm_bonus) (dept_deptno : Column int scm_dept) (dept_name : Column int scm_dept) (emp_empno : Column int scm_emp) (emp_ename : Column int scm_emp) (emp_job : Column int scm_emp) (emp_mgr : Column int scm_emp) (emp_hiredate : Column int scm_emp) (emp_comm : Column int scm_emp) (emp_sal : Column int scm_emp) (emp_deptno : Column int scm_emp) (emp_slacker : Column int scm_emp), denoteSQL ((SELECT1 (e2p (constantExpr integer_1)) FROM1 (product (table rel_emp) (table rel_emp)) WHERE (equal (uvariable (right⋅left⋅emp_deptno)) (uvariable (right⋅right⋅emp_deptno)))) :SQL Γ _) = denoteSQL ((SELECT1 (e2p (constantExpr integer_1)) FROM1 (product (table rel_emp) (table rel_emp)) WHERE (equal (uvariable (right⋅left⋅emp_deptno)) (uvariable (right⋅right⋅emp_deptno)))) :SQL Γ _) := begin intros, print_size, unfold_all_denotations, funext, try {simp}, try {TDP' ucongr}, end
d72edfad81372da1e4c97765cab93a342411d163
a45212b1526d532e6e83c44ddca6a05795113ddc
/src/tactic/where.lean
ab9b7086603d56f032330ba12977042fa73b23f1
[ "Apache-2.0" ]
permissive
fpvandoorn/mathlib
b21ab4068db079cbb8590b58fda9cc4bc1f35df4
b3433a51ea8bc07c4159c1073838fc0ee9b8f227
refs/heads/master
1,624,791,089,608
1,556,715,231,000
1,556,715,231,000
165,722,980
5
0
Apache-2.0
1,552,657,455,000
1,547,494,646,000
Lean
UTF-8
Lean
false
false
7,504
lean
import data.list.defs tactic.basic open lean.parser tactic namespace where meta def mk_flag (let_var : option name := none) : lean.parser (name × ℕ) := do n ← mk_user_fresh_name, emit_code_here $ match let_var with | none := sformat!"def {n} := ()" | some v := sformat!"def {n} := let {v} := {v} in ()" end, nfull ← resolve_constant n, return (nfull, n.components.length) meta def get_namespace_core : name × ℕ → name | (nfull, l) := nfull.get_nth_prefix l meta def resolve_var : list name → ℕ → expr | [] _ := default expr | (n :: rest) 0 := expr.const n [] | (v :: rest) (n + 1) := resolve_var rest n meta def resolve_vars_aux : list name → expr → expr | head (expr.var n) := resolve_var head n | head (expr.app f a) := expr.app (resolve_vars_aux head f) (resolve_vars_aux head a) | head (expr.macro m e) := expr.macro m $ e.map (resolve_vars_aux head) | head (expr.mvar n m e) := expr.mvar n m $ resolve_vars_aux head e | head (expr.pi n bi t v) := expr.pi n bi (resolve_vars_aux head t) (resolve_vars_aux (n :: head) v) | head (expr.lam n bi t v) := expr.lam n bi (resolve_vars_aux head t) (resolve_vars_aux (n :: head) v) | head e := e meta def resolve_vars : expr → expr := resolve_vars_aux [] meta def strip_pi_binders_aux : expr → list (name × binder_info × expr) | (expr.pi n bi t b) := (n, bi, t) :: strip_pi_binders_aux b | _ := [] meta def strip_pi_binders : expr → list (name × binder_info × expr) := strip_pi_binders_aux ∘ resolve_vars meta def get_def_variables (n : name) : tactic (list (name × binder_info × expr)) := (strip_pi_binders ∘ declaration.type) <$> get_decl n meta def get_includes_core (flag : name) : tactic (list (name × binder_info × expr)) := get_def_variables flag meta def binder_brackets : binder_info → string × string | binder_info.implicit := ("{", "}") | binder_info.strict_implicit := ("{", "}") | binder_info.inst_implicit := ("[", "]") | _ := ("(", ")") meta def binder_priority : binder_info → ℕ | binder_info.implicit := 1 | binder_info.strict_implicit := 2 | binder_info.default := 3 | binder_info.inst_implicit := 4 | binder_info.aux_decl := 5 meta def binder_less_important (u v : binder_info) : bool := (binder_priority u) < (binder_priority v) meta def is_in_namespace_nonsynthetic (ns n : name) : bool := ns.is_prefix_of n ∧ ¬(ns.append `user__).is_prefix_of n meta def get_all_in_namespace (ns : name) : tactic (list name) := do e ← get_env, return $ e.fold [] $ λ d l, if is_in_namespace_nonsynthetic ns d.to_name then d.to_name :: l else l meta def fetch_potential_variable_names (ns : name) : tactic (list name) := do l ← get_all_in_namespace ns, l ← l.mmap get_def_variables, return $ list.erase_dup $ l.join.map prod.fst meta def find_var (n' : name) : list (name × binder_info × expr) → option (name × binder_info × expr) | [] := none | ((n, bi, e) :: rest) := if n = n' then some (n, bi, e) else find_var rest meta def is_variable_name (n : name) : lean.parser (option (name × binder_info × expr)) := do { (f, _) ← mk_flag n, l ← get_def_variables f, return $ l.find $ λ v, n = v.1 } <|> return none meta def get_variables_core (ns : name) : lean.parser (list (name × binder_info × expr)) := do l ← fetch_potential_variable_names ns, list.filter_map id <$> l.mmap is_variable_name def select_for_which {α β γ : Type} (p : α → β × γ) [decidable_eq β] (b' : β) : list α → list γ × list α | [] := ([], []) | (a :: rest) := let (cs, others) := select_for_which rest, (b, c) := p a in if b = b' then (c :: cs, others) else (cs, a :: others) meta def collect_by_aux {α β γ : Type} (p : α → β × γ) [decidable_eq β] : list β → list α → list (β × list γ) | [] [] := [] | [] _ := undefined_core "didn't find every key entry!" | (b :: rest) as := let (cs, as) := select_for_which p b as in (b, cs) :: collect_by_aux rest as meta def collect_by {α β γ : Type} (l : list α) (p : α → β × γ) [decidable_eq β] : list (β × list γ) := collect_by_aux p (l.map $ prod.fst ∘ p).erase_dup l def inflate {α β γ : Type} : list (α × list (β × γ)) → list (α × β × γ) | [] := [] | ((a, l) :: rest) := (l.map $ λ e, (a, e.1, e.2)) ++ inflate rest meta def sort_variable_list (l : list (name × binder_info × expr)) : list (expr × binder_info × list name) := let l := collect_by l $ λ v, (v.2.2, (v.1, v.2.1)) in let l := l.map $ λ el, (el.1, collect_by el.2 $ λ v, (v.2, v.1)) in (inflate l).qsort (λ v u, binder_less_important v.2.1 u.2.1) meta def collect_implicit_names : list name → list string × list string | [] := ([], []) | (n :: ns) := let n := to_string n, (ns, ins) := collect_implicit_names ns in if n.front = '_' then (ns, n :: ins) else (n :: ns, ins) meta def format_variable : expr × binder_info × list name → tactic string | (e, bi, ns) := do let (l, r) := binder_brackets bi, e ← pp e, let (ns, ins) := collect_implicit_names ns, let ns := " ".intercalate $ ns.map to_string, let ns := if ns.length = 0 then [] else [sformat!"{l}{ns} : {e}{r}"], let ins := ins.map $ λ _, sformat!"{l}{e}{r}", return $ " ".intercalate $ ns ++ ins meta def compile_variable_list (l : list (name × binder_info × expr)) : tactic string := " ".intercalate <$> (sort_variable_list l).mmap format_variable meta def trace_namespace (ns : name) : lean.parser unit := do let str := match ns with | name.anonymous := "[root namespace]" | ns := to_string ns end, trace format!"namespace {str}" meta def strip_namespace (ns n : name) : name := n.replace_prefix ns name.anonymous meta def get_opens (ns : name) : tactic (list name) := do opens ← list.erase_dup <$> open_namespaces, return $ (opens.erase ns).map $ strip_namespace ns meta def trace_opens (ns : name) : tactic unit := do l ← get_opens ns, let str := " ".intercalate $ l.map to_string, if l.empty then skip else trace format!"open {str}" meta def trace_variables (ns : name) : lean.parser unit := do l ← get_variables_core ns, str ← compile_variable_list l, if l.empty then skip else trace format!"variables {str}" meta def trace_includes (f : name) : tactic unit := do l ← get_includes_core f, let str := " ".intercalate $ l.map $ λ n, to_string n.1, if l.empty then skip else trace format!"include {str}" meta def trace_nl : ℕ → tactic unit | 0 := skip | (n + 1) := trace "" >> trace_nl n meta def trace_end (ns : name) : tactic unit := trace format!"end {ns}" meta def trace_where : lean.parser unit := do (f, n) ← mk_flag, let ns := get_namespace_core (f, n), trace_namespace ns, trace_nl 1, trace_opens ns, trace_variables ns, trace_includes f, trace_nl 3, trace_end ns open interactive reserve prefix `#where`:max @[user_command] meta def where_cmd (_ : decl_meta_info) (_ : parse $ tk "#where") : lean.parser unit := trace_where end where namespace lean.parser open where meta def get_namespace : lean.parser name := get_namespace_core <$> mk_flag meta def get_includes : lean.parser (list (name × binder_info × expr)) := do (f, _) ← mk_flag, get_includes_core f meta def get_variables : lean.parser (list (name × binder_info × expr)) := do (f, _) ← mk_flag, get_variables_core f end lean.parser
78380df0c554079e32b1ffd6f47dc3377bf7661b
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/tests/lean/run/860.lean
f19f89173fbf9a0e4b38635794f8ffb387dec407
[ "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
996
lean
def evenq (n: Nat) : Bool := Nat.mod n 2 = 0 private theorem pack_loop_terminates : (n : Nat) → n / 2 < n.succ | 0 => by decide | 1 => by decide | n+2 => by rw [Nat.div_eq] split · rw [Nat.add_sub_self_right] have := pack_loop_terminates n calc n/2 + 1 < Nat.succ n + 1 := Nat.add_le_add_right this 1 _ < Nat.succ (n + 2) := Nat.succ_lt_succ (Nat.succ_lt_succ (Nat.lt_succ_self _)) · apply Nat.zero_lt_succ def pack (n: Nat) : List Nat := let rec loop (n : Nat) (acc : Nat) (accs: List Nat) : List Nat := let next (n: Nat) := n / 2; match n with | Nat.zero => List.cons acc accs | n+1 => match evenq n with | true => loop (next n) 0 (List.cons acc accs) | false => loop (next n) (acc+1) accs loop n 0 [] termination_by' invImage (fun ⟨n, _, _⟩ => n) Nat.lt_wfRel decreasing_by simp [invImage, InvImage, Prod.lex, sizeOfWFRel, measure, Nat.lt_wfRel] apply pack_loop_terminates #eval pack 27
76d10b07ec6c4c1d116d00be1b0a3f4f3687b068
8d65764a9e5f0923a67fc435eb1a5a1d02fd80e3
/src/measure_theory/function/conditional_expectation.lean
88fc746125b3990650285f0a7fcb57e8e2a335e3
[ "Apache-2.0" ]
permissive
troyjlee/mathlib
e18d4b8026e32062ab9e89bc3b003a5d1cfec3f5
45e7eb8447555247246e3fe91c87066506c14875
refs/heads/master
1,689,248,035,046
1,629,470,528,000
1,629,470,528,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
15,572
lean
/- Copyright (c) 2021 Rémy Degenne. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Rémy Degenne -/ import measure_theory.function.l2_space /-! # Conditional expectation The conditional expectation will be defined for functions in `L²` by an orthogonal projection into a complete subspace of `L²`. It will then be extended to `L¹`. For now, this file contains only the definition of the subspace of `Lᵖ` containing functions which are measurable with respect to a sub-σ-algebra, as well as a proof that it is complete. -/ noncomputable theory open topological_space measure_theory.Lp filter open_locale nnreal ennreal topological_space big_operators measure_theory namespace measure_theory /-- A function `f` verifies `ae_measurable' m f μ` if it is `μ`-a.e. equal to an `m`-measurable function. This is similar to `ae_measurable`, but the `measurable_space` structures used for the measurability statement and for the measure are different. -/ def ae_measurable' {α β} [measurable_space β] (m : measurable_space α) {m0 : measurable_space α} (f : α → β) (μ : measure α) : Prop := ∃ g : α → β, @measurable α β m _ g ∧ f =ᵐ[μ] g namespace ae_measurable' variables {α β 𝕜 : Type*} {m m0 : measurable_space α} {μ : measure α} [measurable_space β] [measurable_space 𝕜] {f g : α → β} lemma congr (hf : ae_measurable' m f μ) (hfg : f =ᵐ[μ] g) : ae_measurable' m g μ := by { obtain ⟨f', hf'_meas, hff'⟩ := hf, exact ⟨f', hf'_meas, hfg.symm.trans hff'⟩, } lemma add [has_add β] [has_measurable_add₂ β] (hf : ae_measurable' m f μ) (hg : ae_measurable' m g μ) : ae_measurable' m (f+g) μ := begin rcases hf with ⟨f', h_f'_meas, hff'⟩, rcases hg with ⟨g', h_g'_meas, hgg'⟩, exact ⟨f' + g', @measurable.add _ _ _ _ m _ f' g' h_f'_meas h_g'_meas, hff'.add hgg'⟩, end lemma const_smul [has_scalar 𝕜 β] [has_measurable_smul 𝕜 β] (c : 𝕜) (hf : ae_measurable' m f μ) : ae_measurable' m (c • f) μ := begin rcases hf with ⟨f', h_f'_meas, hff'⟩, refine ⟨c • f', @measurable.const_smul _ _ _ _ _ _ m _ f' h_f'_meas c, _⟩, exact eventually_eq.fun_comp hff' (λ x, c • x), end end ae_measurable' lemma ae_measurable'_of_ae_measurable'_trim {α β} {m m0 m0' : measurable_space α} [measurable_space β] (hm0 : m0 ≤ m0') {μ : measure α} {f : α → β} (hf : ae_measurable' m f (μ.trim hm0)) : ae_measurable' m f μ := by { obtain ⟨g, hg_meas, hfg⟩ := hf, exact ⟨g, hg_meas, ae_eq_of_ae_eq_trim hfg⟩, } variables {α β γ E E' F F' G G' H 𝕜 : Type*} {p : ℝ≥0∞} [is_R_or_C 𝕜] [measurable_space 𝕜] -- 𝕜 for ℝ or ℂ, together with a measurable_space [measurable_space β] -- β for a generic measurable space -- E for an inner product space [inner_product_space 𝕜 E] [measurable_space E] [borel_space E] [second_countable_topology E] -- E' for an inner product space on which we compute integrals [inner_product_space 𝕜 E'] [measurable_space E'] [borel_space E'] [second_countable_topology E'] [complete_space E'] [normed_space ℝ E'] -- F for a Lp submodule [normed_group F] [normed_space 𝕜 F] [measurable_space F] [borel_space F] [second_countable_topology F] -- F' for integrals on a Lp submodule [normed_group F'] [normed_space 𝕜 F'] [measurable_space F'] [borel_space F'] [second_countable_topology F'] [normed_space ℝ F'] [complete_space F'] -- G for a Lp add_subgroup [normed_group G] [measurable_space G] [borel_space G] [second_countable_topology G] -- G' for integrals on a Lp add_subgroup [normed_group G'] [measurable_space G'] [borel_space G'] [second_countable_topology G'] [normed_space ℝ G'] [complete_space G'] -- H for measurable space and normed group (hypotheses of mem_ℒp) [measurable_space H] [normed_group H] section Lp_meas variables (F 𝕜) /-- `Lp_meas F 𝕜 m p μ` is the subspace of `Lp F p μ` containing functions `f` verifying `ae_measurable' m f μ`, i.e. functions which are `μ`-a.e. equal to an `m`-measurable function. -/ def Lp_meas [opens_measurable_space 𝕜] (m : measurable_space α) [measurable_space α] (p : ℝ≥0∞) (μ : measure α) : submodule 𝕜 (Lp F p μ) := { carrier := {f : (Lp F p μ) | ae_measurable' m f μ} , zero_mem' := ⟨(0 : α → F), @measurable_zero _ α _ m _, Lp.coe_fn_zero _ _ _⟩, add_mem' := λ f g hf hg, (hf.add hg).congr (Lp.coe_fn_add f g).symm, smul_mem' := λ c f hf, (hf.const_smul c).congr (Lp.coe_fn_smul c f).symm, } variables {F 𝕜} variables [opens_measurable_space 𝕜] lemma mem_Lp_meas_iff_ae_measurable' {m m0 : measurable_space α} {μ : measure α} {f : Lp F p μ} : f ∈ Lp_meas F 𝕜 m p μ ↔ ae_measurable' m f μ := by simp_rw [← set_like.mem_coe, ← submodule.mem_carrier, Lp_meas, set.mem_set_of_eq] lemma Lp_meas.ae_measurable' {m m0 : measurable_space α} {μ : measure α} (f : Lp_meas F 𝕜 m p μ) : ae_measurable' m f μ := mem_Lp_meas_iff_ae_measurable'.mp f.mem lemma mem_Lp_meas_self {m0 : measurable_space α} (μ : measure α) (f : Lp F p μ) : f ∈ Lp_meas F 𝕜 m0 p μ := mem_Lp_meas_iff_ae_measurable'.mpr (Lp.ae_measurable f) lemma Lp_meas_coe {m m0 : measurable_space α} {μ : measure α} {f : Lp_meas F 𝕜 m p μ} : ⇑f = (f : Lp F p μ) := coe_fn_coe_base f lemma mem_Lp_meas_indicator_const_Lp {m m0 : measurable_space α} (hm : m ≤ m0) {μ : measure α} {s : set α} (hs : measurable_set[m] s) (hμs : μ s ≠ ∞) {c : F} : indicator_const_Lp p (hm s hs) hμs c ∈ Lp_meas F 𝕜 m p μ := ⟨s.indicator (λ x : α, c), @measurable.indicator α _ m _ _ s (λ x, c) (@measurable_const _ α _ m _) hs, indicator_const_Lp_coe_fn⟩ section complete_subspace /-! ## The subspace `Lp_meas` is complete. We define a `linear_isometry_equiv` between `Lp_meas` and the `Lp` space corresponding to the measure `μ.trim hm`. As a consequence, the completeness of `Lp` implies completeness of `Lp_meas`. -/ variables {ι : Type*} {m m0 : measurable_space α} {μ : measure α} /-- If `f` belongs to `Lp_meas F 𝕜 m p μ`, then the measurable function it is almost everywhere equal to (given by `ae_measurable.mk`) belongs to `ℒp` for the measure `μ.trim hm`. -/ lemma mem_ℒp_trim_of_mem_Lp_meas (hm : m ≤ m0) (f : Lp F p μ) (hf_meas : f ∈ Lp_meas F 𝕜 m p μ) : mem_ℒp (mem_Lp_meas_iff_ae_measurable'.mp hf_meas).some p (μ.trim hm) := begin have hf : ae_measurable' m f μ, from (mem_Lp_meas_iff_ae_measurable'.mp hf_meas), let g := hf.some, obtain ⟨hg, hfg⟩ := hf.some_spec, change mem_ℒp g p (μ.trim hm), refine ⟨hg.ae_measurable, _⟩, have h_snorm_fg : snorm g p (μ.trim hm) = snorm f p μ, by { rw snorm_trim hm hg, exact snorm_congr_ae hfg.symm, }, rw h_snorm_fg, exact Lp.snorm_lt_top f, end /-- If `f` belongs to `Lp` for the measure `μ.trim hm`, then it belongs to the subspace `Lp_meas F 𝕜 m p μ`. -/ lemma mem_Lp_meas_to_Lp_of_trim (hm : m ≤ m0) (f : Lp F p (μ.trim hm)) : (mem_ℒp_of_mem_ℒp_trim hm (Lp.mem_ℒp f)).to_Lp f ∈ Lp_meas F 𝕜 m p μ := begin let hf_mem_ℒp := mem_ℒp_of_mem_ℒp_trim hm (Lp.mem_ℒp f), rw mem_Lp_meas_iff_ae_measurable', refine ae_measurable'.congr _ (mem_ℒp.coe_fn_to_Lp hf_mem_ℒp).symm, refine ae_measurable'_of_ae_measurable'_trim hm _, exact (Lp.ae_measurable f), end variables (F 𝕜 p μ) /-- Map from `Lp_meas` to `Lp F p (μ.trim hm)`. -/ def Lp_meas_to_Lp_trim (hm : m ≤ m0) (f : Lp_meas F 𝕜 m p μ) : Lp F p (μ.trim hm) := mem_ℒp.to_Lp (mem_Lp_meas_iff_ae_measurable'.mp f.mem).some (mem_ℒp_trim_of_mem_Lp_meas hm f f.mem) /-- Map from `Lp F p (μ.trim hm)` to `Lp_meas`, inverse of `Lp_meas_to_Lp_trim`. -/ def Lp_trim_to_Lp_meas (hm : m ≤ m0) (f : Lp F p (μ.trim hm)) : Lp_meas F 𝕜 m p μ := ⟨(mem_ℒp_of_mem_ℒp_trim hm (Lp.mem_ℒp f)).to_Lp f, mem_Lp_meas_to_Lp_of_trim hm f⟩ variables {F 𝕜 p μ} lemma Lp_meas_to_Lp_trim_ae_eq (hm : m ≤ m0) (f : Lp_meas F 𝕜 m p μ) : Lp_meas_to_Lp_trim F 𝕜 p μ hm f =ᵐ[μ] f := (ae_eq_of_ae_eq_trim (mem_ℒp.coe_fn_to_Lp (mem_ℒp_trim_of_mem_Lp_meas hm ↑f f.mem))).trans (mem_Lp_meas_iff_ae_measurable'.mp f.mem).some_spec.2.symm lemma Lp_trim_to_Lp_meas_ae_eq (hm : m ≤ m0) (f : Lp F p (μ.trim hm)) : Lp_trim_to_Lp_meas F 𝕜 p μ hm f =ᵐ[μ] f := mem_ℒp.coe_fn_to_Lp _ /-- `Lp_trim_to_Lp_meas` is a right inverse of `Lp_meas_to_Lp_trim`. -/ lemma Lp_meas_to_Lp_trim_right_inv (hm : m ≤ m0) : function.right_inverse (Lp_trim_to_Lp_meas F 𝕜 p μ hm) (Lp_meas_to_Lp_trim F 𝕜 p μ hm) := begin intro f, ext1, refine ae_eq_trim_of_measurable hm (Lp.measurable _) (Lp.measurable _) _, exact (Lp_meas_to_Lp_trim_ae_eq hm _).trans (Lp_trim_to_Lp_meas_ae_eq hm _), end /-- `Lp_trim_to_Lp_meas` is a left inverse of `Lp_meas_to_Lp_trim`. -/ lemma Lp_meas_to_Lp_trim_left_inv (hm : m ≤ m0) : function.left_inverse (Lp_trim_to_Lp_meas F 𝕜 p μ hm) (Lp_meas_to_Lp_trim F 𝕜 p μ hm) := begin intro f, ext1, ext1, rw ← Lp_meas_coe, exact (Lp_trim_to_Lp_meas_ae_eq hm _).trans (Lp_meas_to_Lp_trim_ae_eq hm _), end lemma Lp_meas_to_Lp_trim_add (hm : m ≤ m0) (f g : Lp_meas F 𝕜 m p μ) : Lp_meas_to_Lp_trim F 𝕜 p μ hm (f + g) = Lp_meas_to_Lp_trim F 𝕜 p μ hm f + Lp_meas_to_Lp_trim F 𝕜 p μ hm g := begin ext1, refine eventually_eq.trans _ (Lp.coe_fn_add _ _).symm, refine ae_eq_trim_of_measurable hm (Lp.measurable _) _ _, { exact @measurable.add _ _ _ _ m _ _ _ (Lp.measurable _) (Lp.measurable _), }, refine (Lp_meas_to_Lp_trim_ae_eq hm _).trans _, refine eventually_eq.trans _ (eventually_eq.add (Lp_meas_to_Lp_trim_ae_eq hm f).symm (Lp_meas_to_Lp_trim_ae_eq hm g).symm), refine (Lp.coe_fn_add _ _).trans _, simp_rw Lp_meas_coe, exact eventually_of_forall (λ x, by refl), end lemma Lp_meas_to_Lp_trim_smul (hm : m ≤ m0) (c : 𝕜) (f : Lp_meas F 𝕜 m p μ) : Lp_meas_to_Lp_trim F 𝕜 p μ hm (c • f) = c • Lp_meas_to_Lp_trim F 𝕜 p μ hm f := begin ext1, refine eventually_eq.trans _ (Lp.coe_fn_smul _ _).symm, refine ae_eq_trim_of_measurable hm (Lp.measurable _) _ _, { exact @measurable.const_smul _ _ _ _ _ _ m _ _ (Lp.measurable _) c, }, refine (Lp_meas_to_Lp_trim_ae_eq hm _).trans _, refine (Lp.coe_fn_smul c _).trans _, refine (Lp_meas_to_Lp_trim_ae_eq hm f).mono (λ x hx, _), rw [pi.smul_apply, pi.smul_apply, hx, Lp_meas_coe], refl, end /-- `Lp_meas_to_Lp_trim` preserves the norm. -/ lemma Lp_meas_to_Lp_trim_norm_map [hp : fact (1 ≤ p)] (hm : m ≤ m0) (f : Lp_meas F 𝕜 m p μ) : ∥Lp_meas_to_Lp_trim F 𝕜 p μ hm f∥ = ∥f∥ := begin rw [norm_def, snorm_trim hm (Lp.measurable _)], swap, { apply_instance, }, rw [snorm_congr_ae (Lp_meas_to_Lp_trim_ae_eq hm _), Lp_meas_coe, ← norm_def], congr, end variables (F 𝕜 p μ) /-- A linear isometry equivalence between `Lp_meas` and `Lp F p (μ.trim hm)`. -/ def Lp_meas_to_Lp_trim_lie [hp : fact (1 ≤ p)] (hm : m ≤ m0) : Lp_meas F 𝕜 m p μ ≃ₗᵢ[𝕜] Lp F p (μ.trim hm) := { to_fun := Lp_meas_to_Lp_trim F 𝕜 p μ hm, map_add' := Lp_meas_to_Lp_trim_add hm, map_smul' := Lp_meas_to_Lp_trim_smul hm, inv_fun := Lp_trim_to_Lp_meas F 𝕜 p μ hm, left_inv := Lp_meas_to_Lp_trim_left_inv hm, right_inv := Lp_meas_to_Lp_trim_right_inv hm, norm_map' := Lp_meas_to_Lp_trim_norm_map hm, } variables {F 𝕜 p μ} instance [hm : fact (m ≤ m0)] [complete_space F] [hp : fact (1 ≤ p)] : complete_space (Lp_meas F 𝕜 m p μ) := by { rw (Lp_meas_to_Lp_trim_lie F 𝕜 p μ hm.elim).to_isometric.complete_space_iff, apply_instance, } end complete_subspace end Lp_meas /-! ## Conditional expectation in L2 We define a conditional expectation in `L2`: it is the orthogonal projection on the subspace `Lp_meas`. -/ section condexp_L2 local attribute [instance] fact_one_le_two_ennreal variables [complete_space E] [borel_space 𝕜] {m m0 : measurable_space α} {μ : measure α} {s t : set α} local notation `⟪`x`, `y`⟫` := @inner 𝕜 E _ x y local notation `⟪`x`, `y`⟫₂` := @inner 𝕜 (α →₂[μ] E) _ x y variables (𝕜) /-- Conditional expectation of a function in L2 with respect to a sigma-algebra -/ def condexp_L2 (hm : m ≤ m0) : (α →₂[μ] E) →L[𝕜] (Lp_meas E 𝕜 m 2 μ) := @orthogonal_projection 𝕜 (α →₂[μ] E) _ _ (Lp_meas E 𝕜 m 2 μ) (by { haveI : fact (m ≤ m0) := ⟨hm⟩, exact infer_instance, }) variables {𝕜} lemma integrable_on_condexp_L2_of_measure_ne_top (hm : m ≤ m0) (hμs : μ s ≠ ∞) (f : α →₂[μ] E) : integrable_on (condexp_L2 𝕜 hm f) s μ := integrable_on_Lp_of_measure_ne_top ((condexp_L2 𝕜 hm f) : α →₂[μ] E) fact_one_le_two_ennreal.elim hμs lemma integrable_condexp_L2_of_finite_measure (hm : m ≤ m0) [finite_measure μ] {f : α →₂[μ] E} : integrable (condexp_L2 𝕜 hm f) μ := integrable_on_univ.mp $ integrable_on_condexp_L2_of_measure_ne_top hm (measure_ne_top _ _) f lemma norm_condexp_L2_le_one (hm : m ≤ m0) : ∥@condexp_L2 α E 𝕜 _ _ _ _ _ _ _ _ _ _ μ hm∥ ≤ 1 := by { haveI : fact (m ≤ m0) := ⟨hm⟩, exact orthogonal_projection_norm_le _, } lemma norm_condexp_L2_le (hm : m ≤ m0) (f : α →₂[μ] E) : ∥condexp_L2 𝕜 hm f∥ ≤ ∥f∥ := ((@condexp_L2 _ E 𝕜 _ _ _ _ _ _ _ _ _ _ μ hm).le_op_norm f).trans (mul_le_of_le_one_left (norm_nonneg _) (norm_condexp_L2_le_one hm)) lemma snorm_condexp_L2_le (hm : m ≤ m0) (f : α →₂[μ] E) : snorm (condexp_L2 𝕜 hm f) 2 μ ≤ snorm f 2 μ := begin rw [Lp_meas_coe, ← ennreal.to_real_le_to_real (Lp.snorm_ne_top _) (Lp.snorm_ne_top _), ← norm_def, ← norm_def, submodule.norm_coe], exact norm_condexp_L2_le hm f, end lemma norm_condexp_L2_coe_le (hm : m ≤ m0) (f : α →₂[μ] E) : ∥(condexp_L2 𝕜 hm f : α →₂[μ] E)∥ ≤ ∥f∥ := begin rw [norm_def, norm_def, ← Lp_meas_coe], refine (ennreal.to_real_le_to_real _ (Lp.snorm_ne_top _)).mpr (snorm_condexp_L2_le hm f), exact Lp.snorm_ne_top _, end lemma inner_condexp_L2_left_eq_right (hm : m ≤ m0) {f g : α →₂[μ] E} : ⟪(condexp_L2 𝕜 hm f : α →₂[μ] E), g⟫₂ = ⟪f, (condexp_L2 𝕜 hm g : α →₂[μ] E)⟫₂ := by { haveI : fact (m ≤ m0) := ⟨hm⟩, exact inner_orthogonal_projection_left_eq_right _ f g, } lemma condexp_L2_indicator_of_measurable (hm : m ≤ m0) (hs : measurable_set[m] s) (hμs : μ s ≠ ∞) (c : E) : (condexp_L2 𝕜 hm (indicator_const_Lp 2 (hm s hs) hμs c) : α →₂[μ] E) = indicator_const_Lp 2 (hm s hs) hμs c := begin rw condexp_L2, haveI : fact (m ≤ m0) := ⟨hm⟩, have h_mem : indicator_const_Lp 2 (hm s hs) hμs c ∈ Lp_meas E 𝕜 m 2 μ, from mem_Lp_meas_indicator_const_Lp hm hs hμs, let ind := (⟨indicator_const_Lp 2 (hm s hs) hμs c, h_mem⟩ : Lp_meas E 𝕜 m 2 μ), have h_coe_ind : (ind : α →₂[μ] E) = indicator_const_Lp 2 (hm s hs) hμs c, by refl, have h_orth_mem := orthogonal_projection_mem_subspace_eq_self ind, rw [← h_coe_ind, h_orth_mem], end lemma inner_condexp_L2_eq_inner_fun (hm : m ≤ m0) (f g : α →₂[μ] E) (hg : ae_measurable' m g μ) : ⟪(condexp_L2 𝕜 hm f : α →₂[μ] E), g⟫₂ = ⟪f, g⟫₂ := begin symmetry, rw [← sub_eq_zero, ← inner_sub_left, condexp_L2], simp only [mem_Lp_meas_iff_ae_measurable'.mpr hg, orthogonal_projection_inner_eq_zero], end end condexp_L2 end measure_theory
8f4766a9da480fc71e5da29c567ccde3fddea200
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/hott/init/relation.hlean
4cdeee4dfd7ac163085f4ef7ba9fe5136ef422f0
[ "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
1,443
hlean
/- 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.logic -- TODO(Leo): remove duplication between this file and algebra/relation.lean -- We need some of the following definitions asap when "initializing" Lean. variables {A B : Type} (R : B → B → Type) local infix `≺`:50 := R definition reflexive := ∀x, x ≺ x definition symmetric := ∀⦃x y⦄, x ≺ y → y ≺ x definition transitive := ∀⦃x y z⦄, x ≺ y → y ≺ z → x ≺ z definition irreflexive := ∀x, ¬ x ≺ x definition anti_symmetric := ∀⦃x y⦄, x ≺ y → y ≺ x → x = y definition empty_relation := λa₁ a₂ : A, empty definition subrelation (Q R : B → B → Type) := ∀⦃x y⦄, Q x y → R x y definition inv_image (f : A → B) : A → A → Type := λa₁ a₂, f a₁ ≺ f a₂ definition inv_image.trans (f : A → B) (H : transitive R) : transitive (inv_image R f) := λ (a₁ a₂ a₃ : A) (H₁ : inv_image R f a₁ a₂) (H₂ : inv_image R f a₂ a₃), H H₁ H₂ definition inv_image.irreflexive (f : A → B) (H : irreflexive R) : irreflexive (inv_image R f) := λ (a : A) (H₁ : inv_image R f a a), H (f a) H₁ inductive tc {A : Type} (R : A → A → Type) : A → A → Type := | base : ∀a b, R a b → tc R a b | trans : ∀a b c, tc R a b → tc R b c → tc R a c
593ed552a8cb789f16050c08d3a500c154a541fd
2c41ae31b2b771ad5646ad880201393f5269a7f0
/Lean/Qualities/Accurate.lean
59227d30b95273058bfa1256e5d39ea5ef4a6a17
[]
no_license
kevinsullivan/Boehm
926f25bc6f1a8b6bd47d333d936fdfc278228312
55208395bff20d48a598b7fa33a4d55a2447a9cf
refs/heads/master
1,586,127,134,302
1,488,252,326,000
1,488,252,326,000
32,836,930
0
0
null
null
null
null
UTF-8
Lean
false
false
660
lean
-- Accurate /- [Accurate] is parameterized by an instance of type [SystemType], and it's a sub-attribute to [MissionEffective]. An instance of type [SystemType] is deemed [Accurate] if and only if all the requirements are satisfied. -/ import SystemModel.System inductive Accurate (sys_type: SystemType): Prop | intro : (exists accurate: sys_type ^.Contexts -> sys_type ^.Phases -> sys_type ^.Stakeholders -> @SystemInstance sys_type -> Prop, forall c: sys_type ^.Contexts, forall p: sys_type ^.Phases, forall s: sys_type ^.Stakeholders, forall st: @SystemInstance sys_type, accurate c p s st) -> Accurate
b80bd78f80251614d736143dddf8c27a809048d9
a7602958ab456501ff85db8cf5553f7bcab201d7
/Assignment7/3.7-homework.lean
30eecdc31eb650ebe4c76d974e9cf7dc32a47b1b
[]
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
949
lean
-- Assignment 7 -- Dom Farolino, farolidm@mail.uc.edu -- Math Logic -- Exercise 1 namespace exercise1 open classical variable A : Prop theorem dne (h: ¬¬ A) : A := by_contradiction (assume h1 : ¬ A, show false, from h h1) example : A ∨ ¬ A := have h1: ¬ (¬ (A ∨ ¬ A)), from -- Just proving h1. assume h2: (¬ (A ∨ ¬ A)), show false, from have h4: ¬ A, from assume ha: A, show false, from h2 (or.inl ha), have h3: (A ∨ ¬ A), from or.inr h4, show false, from h2 h3, show A ∨ ¬ A, from (dne (A ∨ ¬ A)) h1 end exercise1 -- Exercise 2 namespace exercise2 variable P : Prop example : ¬ (P ↔ ¬ P) := assume h1: (P ↔ ¬ P), have hNotP: ¬ P, from assume hp: P, have hNotP2: ¬ P, from (iff.elim_left h1) hp, show false, from hNotP2 hp, have hp: P, from (iff.elim_right h1) hNotP, show false, from hNotP hp end exercise2
359b1b12ed507baf6add3af1b10f9afe4b7d339f
ecad13897fdb44984cf1968424224d1750040236
/lean/lean/examples/example02.lean
b9c885d9c213f43533d29fb0ffe6bf0ea04d5c4d
[]
no_license
MetaBorgCube/sdf3-demo
4899159b1cfb0a95ae3af325035bbba8a1255477
e831606d5b404eba75b087916a1162923143b98a
refs/heads/master
1,609,472,086,310
1,553,380,857,000
1,553,380,857,000
59,577,395
0
0
null
null
null
null
UTF-8
Lean
false
false
21
lean
(a : A)(z : S) -> x
c3cbc3ec00148bbbc76abf7fd1be63186228f8ac
6fca17f8d5025f89be1b2d9d15c9e0c4b4900cbf
/src/game/world6/level2.lean
14e2bc95ba228198914c873d9c811c55e807a9c4
[ "Apache-2.0" ]
permissive
arolihas/natural_number_game
4f0c93feefec93b8824b2b96adff8b702b8b43ce
8e4f7b4b42888a3b77429f90cce16292bd288138
refs/heads/master
1,621,872,426,808
1,586,270,467,000
1,586,270,467,000
253,648,466
0
0
null
1,586,219,694,000
1,586,219,694,000
null
UTF-8
Lean
false
false
1,900
lean
/- # Proposition world. ## Level 2: `intro`. Let's prove an implication. Let $P$ be a true/false statement, and let's prove that $P\implies P$. If you delete the `sorry` you will see that our goal is `P → P`. Constructing a term of type `P → P` (which is what solving this goal *means*) in this case amounts to proving that $P\implies P$, and computer scientists think of this as coming up with a function which sends proofs of $P$ to proofs of $P$. To define an implication $P\implies Q$ we need to choose an arbitrary proof $p : P$ of $P$ and then, perhaps using $p$, construct a proof of $Q$. The Lean way to say "let's assume $P$ is true" is `intro p`, i.e., "let's assume we have a proof of $P$". ## Note for worriers. Those of you who know something about the subtle differences between truth and provability discovered by Goedel -- these are not relevant here. Imagine we are working in a fixed model of mathematics, and when I say "proof" I actually mean "truth in the model", or "proof in the metatheory". ## Rule of thumb: If your goal is to prove `P → Q` (i.e. that $P\implies Q$) then `intro p`, meaning "assume $p$ is a proof of $P$", will make progress. To solve the goal below, you have to come up with a function from `P` (thought of as the set of proofs of $P$!) to itself. Start with `intro p,` (i.e. "let $p$ be a proof of $P$") and note that our local context now looks like this: ``` P : Prop, p : P ⊢ P ``` Our job now is to construct a proof of $P$. But $p$ is a proof of $P$. So `exact p,` will close the goal. Note that `exact P` will not work -- don't confuse a true/false statement (which could be false!) with a proof. We will stick with the convention of capital letters for propositions and small letters for proofs. -/ /- Lemma : no-side-bar If $P$ is a proposition then $P\implies P$. -/ lemma imp_self (P : Prop) : P → P := begin end
e2c474eb6aa0a9b5655616c33b6229d7f3253c7d
82e44445c70db0f03e30d7be725775f122d72f3e
/src/measure_theory/conditional_expectation.lean
15c5fdeda25fef8b961847cc2a2b28d64f0942eb
[ "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
16,109
lean
/- Copyright (c) 2021 Rémy Degenne. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Rémy Degenne -/ import measure_theory.l2_space /-! # Conditional expectation The conditional expectation will be defined for functions in `L²` by an orthogonal projection into a complete subspace of `L²`. It will then be extended to `L¹`. For now, this file contains only the definition of the subspace of `Lᵖ` containing functions which are measurable with respect to a sub-σ-algebra, as well as a proof that it is complete. -/ noncomputable theory open topological_space measure_theory.Lp filter open_locale nnreal ennreal topological_space big_operators measure_theory namespace measure_theory /-- A function `f` verifies `ae_measurable' m f μ` if it is `μ`-a.e. equal to an `m`-measurable function. This is similar to `ae_measurable`, but the `measurable_space` structures used for the measurability statement and for the measure are different. -/ def ae_measurable' {α β} [measurable_space β] (m : measurable_space α) {m0 : measurable_space α} (f : α → β) (μ : measure α) : Prop := ∃ g : α → β, @measurable α β m _ g ∧ f =ᵐ[μ] g namespace ae_measurable' variables {α β 𝕜 : Type*} {m m0 : measurable_space α} {μ : measure α} [measurable_space β] [measurable_space 𝕜] {f g : α → β} lemma congr (hf : ae_measurable' m f μ) (hfg : f =ᵐ[μ] g) : ae_measurable' m g μ := by { obtain ⟨f', hf'_meas, hff'⟩ := hf, exact ⟨f', hf'_meas, hfg.symm.trans hff'⟩, } lemma add [has_add β] [has_measurable_add₂ β] (hf : ae_measurable' m f μ) (hg : ae_measurable' m g μ) : ae_measurable' m (f+g) μ := begin rcases hf with ⟨f', h_f'_meas, hff'⟩, rcases hg with ⟨g', h_g'_meas, hgg'⟩, exact ⟨f' + g', @measurable.add _ _ _ _ m _ f' g' h_f'_meas h_g'_meas, hff'.add hgg'⟩, end lemma const_smul [has_scalar 𝕜 β] [has_measurable_smul 𝕜 β] (c : 𝕜) (hf : ae_measurable' m f μ) : ae_measurable' m (c • f) μ := begin rcases hf with ⟨f', h_f'_meas, hff'⟩, refine ⟨c • f', @measurable.const_smul _ _ _ _ _ _ m _ f' h_f'_meas c, _⟩, exact eventually_eq.fun_comp hff' (λ x, c • x), end end ae_measurable' lemma ae_measurable'_of_ae_measurable'_trim {α β} {m m0 m0' : measurable_space α} [measurable_space β] (hm0 : m0 ≤ m0') {μ : measure α} {f : α → β} (hf : ae_measurable' m f (μ.trim hm0)) : ae_measurable' m f μ := by { obtain ⟨g, hg_meas, hfg⟩ := hf, exact ⟨g, hg_meas, ae_eq_of_ae_eq_trim hfg⟩, } variables {α β γ E E' F F' G G' H 𝕜 : Type*} {p : ℝ≥0∞} [is_R_or_C 𝕜] [measurable_space 𝕜] -- 𝕜 for ℝ or ℂ, together with a measurable_space [measurable_space β] -- β for a generic measurable space -- E for an inner product space [inner_product_space 𝕜 E] [measurable_space E] [borel_space E] [second_countable_topology E] -- E' for an inner product space on which we compute integrals [inner_product_space 𝕜 E'] [measurable_space E'] [borel_space E'] [second_countable_topology E'] [complete_space E'] [normed_space ℝ E'] -- F for a Lp submodule [normed_group F] [normed_space 𝕜 F] [measurable_space F] [borel_space F] [second_countable_topology F] -- F' for integrals on a Lp submodule [normed_group F'] [normed_space 𝕜 F'] [measurable_space F'] [borel_space F'] [second_countable_topology F'] [normed_space ℝ F'] [complete_space F'] -- G for a Lp add_subgroup [normed_group G] [measurable_space G] [borel_space G] [second_countable_topology G] -- G' for integrals on a Lp add_subgroup [normed_group G'] [measurable_space G'] [borel_space G'] [second_countable_topology G'] [normed_space ℝ G'] [complete_space G'] -- H for measurable space and normed group (hypotheses of mem_ℒp) [measurable_space H] [normed_group H] section Lp_meas variables (F 𝕜) /-- `Lp_meas F 𝕜 m p μ` is the subspace of `Lp F p μ` containing functions `f` verifying `ae_measurable' m f μ`, i.e. functions which are `μ`-a.e. equal to an `m`-measurable function. -/ def Lp_meas [opens_measurable_space 𝕜] (m : measurable_space α) [measurable_space α] (p : ℝ≥0∞) (μ : measure α) : submodule 𝕜 (Lp F p μ) := { carrier := {f : (Lp F p μ) | ae_measurable' m f μ} , zero_mem' := ⟨(0 : α → F), @measurable_zero _ α _ m _, Lp.coe_fn_zero _ _ _⟩, add_mem' := λ f g hf hg, (hf.add hg).congr (Lp.coe_fn_add f g).symm, smul_mem' := λ c f hf, (hf.const_smul c).congr (Lp.coe_fn_smul c f).symm, } variables {F 𝕜} variables [opens_measurable_space 𝕜] lemma mem_Lp_meas_iff_ae_measurable' {m m0 : measurable_space α} {μ : measure α} {f : Lp F p μ} : f ∈ Lp_meas F 𝕜 m p μ ↔ ae_measurable' m f μ := by simp_rw [← set_like.mem_coe, ← submodule.mem_carrier, Lp_meas, set.mem_set_of_eq] lemma Lp_meas.ae_measurable' {m m0 : measurable_space α} {μ : measure α} (f : Lp_meas F 𝕜 m p μ) : ae_measurable' m f μ := mem_Lp_meas_iff_ae_measurable'.mp f.mem lemma mem_Lp_meas_self {m0 : measurable_space α} (μ : measure α) (f : Lp F p μ) : f ∈ Lp_meas F 𝕜 m0 p μ := mem_Lp_meas_iff_ae_measurable'.mpr (Lp.ae_measurable f) lemma Lp_meas_coe {m m0 : measurable_space α} {μ : measure α} {f : Lp_meas F 𝕜 m p μ} : ⇑f = (f : Lp F p μ) := coe_fn_coe_base f lemma mem_Lp_meas_indicator_const_Lp {m m0 : measurable_space α} (hm : m ≤ m0) {μ : measure α} {s : set α} (hs : measurable_set[m] s) (hμs : μ s ≠ ∞) {c : F} : indicator_const_Lp p (hm s hs) hμs c ∈ Lp_meas F 𝕜 m p μ := ⟨s.indicator (λ x : α, c), @measurable.indicator α _ m _ _ s (λ x, c) (@measurable_const _ α _ m _) hs, indicator_const_Lp_coe_fn⟩ section complete_subspace /-! ## The subspace `Lp_meas` is complete. We define a `linear_isometry_equiv` between `Lp_meas` and the `Lp` space corresponding to the measure `μ.trim hm`. As a consequence, the completeness of `Lp` implies completeness of `Lp_meas`. -/ variables {ι : Type*} {m m0 : measurable_space α} {μ : measure α} /-- If `f` belongs to `Lp_meas F 𝕜 m p μ`, then the measurable function it is almost everywhere equal to (given by `ae_measurable.mk`) belongs to `ℒp` for the measure `μ.trim hm`. -/ lemma mem_ℒp_trim_of_mem_Lp_meas (hm : m ≤ m0) (f : Lp F p μ) (hf_meas : f ∈ Lp_meas F 𝕜 m p μ) : @mem_ℒp α F m _ _ (mem_Lp_meas_iff_ae_measurable'.mp hf_meas).some p (μ.trim hm) := begin have hf : ae_measurable' m f μ, from (mem_Lp_meas_iff_ae_measurable'.mp hf_meas), let g := hf.some, obtain ⟨hg, hfg⟩ := hf.some_spec, change @mem_ℒp α F m _ _ g p (μ.trim hm), refine ⟨@measurable.ae_measurable _ _ m _ g (μ.trim hm) hg, _⟩, have h_snorm_fg : @snorm α _ m _ g p (μ.trim hm) = snorm f p μ, by { rw snorm_trim hm hg, exact snorm_congr_ae hfg.symm, }, rw h_snorm_fg, exact Lp.snorm_lt_top f, end /-- If `f` belongs to `Lp` for the measure `μ.trim hm`, then it belongs to the subspace `Lp_meas F 𝕜 m p μ`. -/ lemma mem_Lp_meas_to_Lp_of_trim (hm : m ≤ m0) (f : @Lp α F m _ _ _ _ p (μ.trim hm)) : (mem_ℒp_of_mem_ℒp_trim hm (@Lp.mem_ℒp _ _ m _ _ _ _ _ _ f)).to_Lp f ∈ Lp_meas F 𝕜 m p μ := begin let hf_mem_ℒp := mem_ℒp_of_mem_ℒp_trim hm (@Lp.mem_ℒp _ _ m _ _ _ _ _ _ f), rw mem_Lp_meas_iff_ae_measurable', refine ae_measurable'.congr _ (mem_ℒp.coe_fn_to_Lp hf_mem_ℒp).symm, refine ae_measurable'_of_ae_measurable'_trim hm _, exact (@Lp.ae_measurable _ _ m _ _ _ _ _ _ f), end variables (F 𝕜 p μ) /-- Map from `Lp_meas` to `Lp F p (μ.trim hm)`. -/ def Lp_meas_to_Lp_trim (hm : m ≤ m0) (f : Lp_meas F 𝕜 m p μ) : @Lp α F m _ _ _ _ p (μ.trim hm) := @mem_ℒp.to_Lp _ _ m p (μ.trim hm) _ _ _ _ (mem_Lp_meas_iff_ae_measurable'.mp f.mem).some (mem_ℒp_trim_of_mem_Lp_meas hm f f.mem) /-- Map from `Lp F p (μ.trim hm)` to `Lp_meas`, inverse of `Lp_meas_to_Lp_trim`. -/ def Lp_trim_to_Lp_meas (hm : m ≤ m0) (f : @Lp α F m _ _ _ _ p (μ.trim hm)) : Lp_meas F 𝕜 m p μ := ⟨(mem_ℒp_of_mem_ℒp_trim hm (@Lp.mem_ℒp _ _ m _ _ _ _ _ _ f)).to_Lp f, mem_Lp_meas_to_Lp_of_trim hm f⟩ variables {F 𝕜 p μ} lemma Lp_meas_to_Lp_trim_ae_eq (hm : m ≤ m0) (f : Lp_meas F 𝕜 m p μ) : Lp_meas_to_Lp_trim F 𝕜 p μ hm f =ᵐ[μ] f := (ae_eq_of_ae_eq_trim (@mem_ℒp.coe_fn_to_Lp _ _ m _ _ _ _ _ _ _ (mem_ℒp_trim_of_mem_Lp_meas hm ↑f f.mem))).trans (mem_Lp_meas_iff_ae_measurable'.mp f.mem).some_spec.2.symm lemma Lp_trim_to_Lp_meas_ae_eq (hm : m ≤ m0) (f : @Lp α F m _ _ _ _ p (μ.trim hm)) : Lp_trim_to_Lp_meas F 𝕜 p μ hm f =ᵐ[μ] f := mem_ℒp.coe_fn_to_Lp _ /-- `Lp_trim_to_Lp_meas` is a right inverse of `Lp_meas_to_Lp_trim`. -/ lemma Lp_meas_to_Lp_trim_right_inv (hm : m ≤ m0) : function.right_inverse (Lp_trim_to_Lp_meas F 𝕜 p μ hm) (Lp_meas_to_Lp_trim F 𝕜 p μ hm) := begin intro f, ext1, refine ae_eq_trim_of_measurable hm _ _ _, { exact @Lp.measurable _ _ m _ _ _ _ _ _ _, }, { exact @Lp.measurable _ _ m _ _ _ _ _ _ _, }, { exact (Lp_meas_to_Lp_trim_ae_eq hm _).trans (Lp_trim_to_Lp_meas_ae_eq hm _), }, end /-- `Lp_trim_to_Lp_meas` is a left inverse of `Lp_meas_to_Lp_trim`. -/ lemma Lp_meas_to_Lp_trim_left_inv (hm : m ≤ m0) : function.left_inverse (Lp_trim_to_Lp_meas F 𝕜 p μ hm) (Lp_meas_to_Lp_trim F 𝕜 p μ hm) := begin intro f, ext1, ext1, rw ← Lp_meas_coe, exact (Lp_trim_to_Lp_meas_ae_eq hm _).trans (Lp_meas_to_Lp_trim_ae_eq hm _), end lemma Lp_meas_to_Lp_trim_add (hm : m ≤ m0) (f g : Lp_meas F 𝕜 m p μ) : Lp_meas_to_Lp_trim F 𝕜 p μ hm (f + g) = Lp_meas_to_Lp_trim F 𝕜 p μ hm f + Lp_meas_to_Lp_trim F 𝕜 p μ hm g := begin ext1, refine eventually_eq.trans _ (@Lp.coe_fn_add _ _ m _ _ _ _ _ _ _ _).symm, refine ae_eq_trim_of_measurable hm _ _ _, { exact @Lp.measurable _ _ m _ _ _ _ _ _ _, }, { exact @measurable.add _ _ _ _ m _ _ _ (@Lp.measurable _ _ m _ _ _ _ _ _ _) (@Lp.measurable _ _ m _ _ _ _ _ _ _), }, refine (Lp_meas_to_Lp_trim_ae_eq hm _).trans _, refine eventually_eq.trans _ (eventually_eq.add (Lp_meas_to_Lp_trim_ae_eq hm f).symm (Lp_meas_to_Lp_trim_ae_eq hm g).symm), refine (Lp.coe_fn_add _ _).trans _, simp_rw Lp_meas_coe, refine eventually_of_forall (λ x, _), refl, end lemma Lp_meas_to_Lp_trim_smul (hm : m ≤ m0) (c : 𝕜) (f : Lp_meas F 𝕜 m p μ) : Lp_meas_to_Lp_trim F 𝕜 p μ hm (c • f) = c • Lp_meas_to_Lp_trim F 𝕜 p μ hm f := begin ext1, refine eventually_eq.trans _ (@Lp.coe_fn_smul _ _ m _ _ _ _ _ _ _ _ _ _ _ _ _).symm, refine ae_eq_trim_of_measurable hm _ _ _, { exact @Lp.measurable _ _ m _ _ _ _ _ _ _, }, { exact @measurable.const_smul _ _ _ _ _ _ m _ _ (@Lp.measurable _ _ m _ _ _ _ _ _ _) c, }, refine (Lp_meas_to_Lp_trim_ae_eq hm _).trans _, refine (Lp.coe_fn_smul c _).trans _, refine (Lp_meas_to_Lp_trim_ae_eq hm f).mono (λ x hx, _), rw [pi.smul_apply, pi.smul_apply, hx, Lp_meas_coe], refl, end /-- `Lp_meas_to_Lp_trim` preserves the norm. -/ lemma Lp_meas_to_Lp_trim_norm_map [hp : fact (1 ≤ p)] (hm : m ≤ m0) (f : Lp_meas F 𝕜 m p μ) : ∥Lp_meas_to_Lp_trim F 𝕜 p μ hm f∥ = ∥f∥ := begin rw [norm_def, snorm_trim hm (@Lp.measurable _ _ m _ _ _ _ _ _ _)], swap, { apply_instance, }, rw [snorm_congr_ae (Lp_meas_to_Lp_trim_ae_eq hm _), Lp_meas_coe, ← norm_def], congr, end variables (F 𝕜 p μ) /-- A linear isometry equivalence between `Lp_meas` and `Lp F p (μ.trim hm)`. -/ def Lp_meas_to_Lp_trim_lie [hp : fact (1 ≤ p)] (hm : m ≤ m0) : Lp_meas F 𝕜 m p μ ≃ₗᵢ[𝕜] @Lp α F m _ _ _ _ p (μ.trim hm) := { to_fun := Lp_meas_to_Lp_trim F 𝕜 p μ hm, map_add' := Lp_meas_to_Lp_trim_add hm, map_smul' := Lp_meas_to_Lp_trim_smul hm, inv_fun := Lp_trim_to_Lp_meas F 𝕜 p μ hm, left_inv := Lp_meas_to_Lp_trim_left_inv hm, right_inv := Lp_meas_to_Lp_trim_right_inv hm, norm_map' := Lp_meas_to_Lp_trim_norm_map hm, } variables {F 𝕜 p μ} instance [hm : fact (m ≤ m0)] [complete_space F] [hp : fact (1 ≤ p)] : complete_space (Lp_meas F 𝕜 m p μ) := by { rw (Lp_meas_to_Lp_trim_lie F 𝕜 p μ hm.elim).to_isometric.complete_space_iff, apply_instance, } end complete_subspace end Lp_meas /-! ## Conditional expectation in L2 We define a conditional expectation in `L2`: it is the orthogonal projection on the subspace `Lp_meas`. -/ section condexp_L2 local attribute [instance] fact_one_le_two_ennreal variables [complete_space E] [borel_space 𝕜] {m m0 : measurable_space α} {μ : measure α} {s t : set α} local notation `⟪`x`, `y`⟫` := @inner 𝕜 E _ x y local notation `⟪`x`, `y`⟫₂` := @inner 𝕜 (α →₂[μ] E) _ x y variables (𝕜) /-- Conditional expectation of a function in L2 with respect to a sigma-algebra -/ def condexp_L2 (hm : m ≤ m0) : (α →₂[μ] E) →L[𝕜] (Lp_meas E 𝕜 m 2 μ) := @orthogonal_projection 𝕜 (α →₂[μ] E) _ _ (Lp_meas E 𝕜 m 2 μ) (by { haveI : fact (m ≤ m0) := ⟨hm⟩, exact infer_instance, }) variables {𝕜} lemma integrable_on_condexp_L2_of_measure_ne_top (hm : m ≤ m0) (hμs : μ s ≠ ∞) (f : α →₂[μ] E) : integrable_on (condexp_L2 𝕜 hm f) s μ := integrable_on_Lp_of_measure_ne_top ((condexp_L2 𝕜 hm f) : α →₂[μ] E) fact_one_le_two_ennreal.elim hμs lemma integrable_condexp_L2_of_finite_measure (hm : m ≤ m0) [finite_measure μ] {f : α →₂[μ] E} : integrable (condexp_L2 𝕜 hm f) μ := integrable_on_univ.mp $ integrable_on_condexp_L2_of_measure_ne_top hm (measure_ne_top _ _) f lemma norm_condexp_L2_le_one (hm : m ≤ m0) : ∥@condexp_L2 α E 𝕜 _ _ _ _ _ _ _ _ _ _ μ hm∥ ≤ 1 := by { haveI : fact (m ≤ m0) := ⟨hm⟩, exact orthogonal_projection_norm_le _, } lemma norm_condexp_L2_le (hm : m ≤ m0) (f : α →₂[μ] E) : ∥condexp_L2 𝕜 hm f∥ ≤ ∥f∥ := ((@condexp_L2 _ E 𝕜 _ _ _ _ _ _ _ _ _ _ μ hm).le_op_norm f).trans (mul_le_of_le_one_left (norm_nonneg _) (norm_condexp_L2_le_one hm)) lemma snorm_condexp_L2_le (hm : m ≤ m0) (f : α →₂[μ] E) : snorm (condexp_L2 𝕜 hm f) 2 μ ≤ snorm f 2 μ := begin rw [Lp_meas_coe, ← ennreal.to_real_le_to_real (Lp.snorm_ne_top _) (Lp.snorm_ne_top _), ← norm_def, ← norm_def, submodule.norm_coe], exact norm_condexp_L2_le hm f, end lemma norm_condexp_L2_coe_le (hm : m ≤ m0) (f : α →₂[μ] E) : ∥(condexp_L2 𝕜 hm f : α →₂[μ] E)∥ ≤ ∥f∥ := begin rw [norm_def, norm_def, ← Lp_meas_coe], refine (ennreal.to_real_le_to_real _ (Lp.snorm_ne_top _)).mpr (snorm_condexp_L2_le hm f), exact Lp.snorm_ne_top _, end lemma inner_condexp_L2_left_eq_right (hm : m ≤ m0) {f g : α →₂[μ] E} : ⟪(condexp_L2 𝕜 hm f : α →₂[μ] E), g⟫₂ = ⟪f, (condexp_L2 𝕜 hm g : α →₂[μ] E)⟫₂ := by { haveI : fact (m ≤ m0) := ⟨hm⟩, exact inner_orthogonal_projection_left_eq_right _ f g, } lemma condexp_L2_indicator_of_measurable (hm : m ≤ m0) (hs : measurable_set[m] s) (hμs : μ s ≠ ∞) (c : E) : (condexp_L2 𝕜 hm (indicator_const_Lp 2 (hm s hs) hμs c) : α →₂[μ] E) = indicator_const_Lp 2 (hm s hs) hμs c := begin rw condexp_L2, haveI : fact (m ≤ m0) := ⟨hm⟩, have h_mem : indicator_const_Lp 2 (hm s hs) hμs c ∈ Lp_meas E 𝕜 m 2 μ, from mem_Lp_meas_indicator_const_Lp hm hs hμs, let ind := (⟨indicator_const_Lp 2 (hm s hs) hμs c, h_mem⟩ : Lp_meas E 𝕜 m 2 μ), have h_coe_ind : (ind : α →₂[μ] E) = indicator_const_Lp 2 (hm s hs) hμs c, by refl, have h_orth_mem := orthogonal_projection_mem_subspace_eq_self ind, rw [← h_coe_ind, h_orth_mem], end lemma inner_condexp_L2_eq_inner_fun (hm : m ≤ m0) (f g : α →₂[μ] E) (hg : ae_measurable' m g μ) : ⟪(condexp_L2 𝕜 hm f : α →₂[μ] E), g⟫₂ = ⟪f, g⟫₂ := begin symmetry, rw [← sub_eq_zero, ← inner_sub_left, condexp_L2], simp only [mem_Lp_meas_iff_ae_measurable'.mpr hg, orthogonal_projection_inner_eq_zero], end end condexp_L2 end measure_theory
219daa73fc02babf0d9174b1c4cf1efa976034ec
8c02fed42525b65813b55c064afe2484758d6d09
/src/langparser.lean
8204c7f7504ca5d7dc8ebfb6174c96128dcfa47a
[ "LicenseRef-scancode-generic-cla", "MIT" ]
permissive
microsoft/AliveInLean
3eac351a34154efedd3ffc4fe2fa4ec01b219e0d
4b739dd6e4266b26a045613849df221374119871
refs/heads/master
1,691,419,737,939
1,689,365,567,000
1,689,365,568,000
131,156,103
23
18
NOASSERTION
1,660,342,040,000
1,524,747,538,000
Lean
UTF-8
Lean
false
false
5,566
lean
-- Copyright (c) Microsoft Corporation. All rights reserved. -- Licensed under the MIT license. import .lang import .lang_tostr import data.buffer.parser open parser -- ReadLinebreaks reads zero or more '\n's. def ReadLinebreaks: parser unit := many' (ch '\n') -- ReadLine reads a line. def ReadLine: parser string := many_char1 $ sat $ λ c, c ≠ '\n' -- ReadWhitespaces reads zero or more whitespaces. def ReadWhitespaces: parser unit := decorate_error "Whitespace expected" $ many' $ sat $ λ c, c.is_whitespace -- Reads a register. def ReadReg: parser reg := do ch '%', cs ← many_char1 $ sat $ λ c, c.is_alpha ∨ c.is_digit ∨ c = '_' ∨ c = '.', return (reg.r ("%" ++ cs)) -- Reads one constant. -- Only positive number is supported currently. def ReadIntConst: parser const := do is_neg ← ((do ch '-', return tt) <|> return ff), cs ← many_char1 $ sat $ λ c, c.is_digit, -- coercion from nat to int return (const.int (if is_neg then -1 * (cs.to_nat) else cs.to_nat)) -- Reads a value. -- Either ReadReg or ReadConst. def ReadVal: parser operand := (do r ← ReadReg, return (operand.reg r)) <|> (do str "true", return (operand.const (const.int (-1)))) <|> (do str "false", return (operand.const (const.int 0))) <|> (do c ← ReadIntConst, return (operand.const c)) <|> failure def ReadBinaryInstFlag: parser bopflag := (do str "nsw", ReadWhitespaces, return bopflag.nsw) <|> (do str "nuw", ReadWhitespaces, return bopflag.nuw) <|> (do str "exact", ReadWhitespaces, return bopflag.exact) def ReadType: parser ty := do ch 'i', sz ← many_char1 $ sat $ λ c, c.is_digit, return (ty.int sz.to_nat) -- Reads a binary instruction. def ReadBinaryInst (lhs:reg) (opname:string) (bopc:bopcode) : parser instruction := do str opname, ReadWhitespaces, flags ← many ReadBinaryInstFlag, ReadWhitespaces, retty ← (ReadType <|> return (ty.arbitrary_int)), ReadWhitespaces, v1 ← ReadVal, ReadWhitespaces, ch ',', ReadWhitespaces, v2 ← ReadVal, ReadWhitespaces, ReadLinebreaks, return (instruction.binop retty lhs bopc flags v1 v2) -- Reads "cast" instructions. def ReadCastInst (lhs:reg) (opname:string) (uopc:uopcode): parser instruction := do str opname, ReadWhitespaces, fromty ← (ReadType <|> return (ty.arbitrary_int)), ReadWhitespaces, v ← ReadVal, ReadWhitespaces, str "to", ReadWhitespaces, toty ← ReadType, ReadLinebreaks, return (instruction.unaryop lhs uopc fromty v toty) def ReadICmpCond: parser icmpcond := let ls := [icmpcond.eq, icmpcond.ne, icmpcond.ugt, icmpcond.uge, icmpcond.ult, icmpcond.ule, icmpcond.sgt, icmpcond.sge, icmpcond.slt, icmpcond.sle] in let fs := ls.map (λ (cond:icmpcond), do str (to_string cond), return cond) in list.foldl (λ a b, a <|> b) fs.head fs.tail -- Reads "icmp" instruction. def ReadSelectInst (lhs:reg): parser instruction := do str "select", ReadWhitespaces, condty ← (ReadType <|> return (ty.arbitrary_int)), ReadWhitespaces, cond ← ReadVal, ReadWhitespaces, ch ',', ReadWhitespaces, valty ← (ReadType <|> return (ty.arbitrary_int)), ReadWhitespaces, v1 ← ReadVal, ReadWhitespaces, ch ',', ReadWhitespaces, valty ← (ReadType <|> return (ty.arbitrary_int)), ReadWhitespaces, v2 ← ReadVal, ReadWhitespaces, ReadLinebreaks, return (instruction.selectop lhs condty cond valty v1 v2) -- Reads "icmp" instruction. def ReadICmpInst (lhs:reg): parser instruction := do str "icmp", ReadWhitespaces, cond ← ReadICmpCond, ReadWhitespaces, thety ← (ReadType <|> return (ty.arbitrary_int)), ReadWhitespaces, v1 ← ReadVal, ReadWhitespaces, ch ',', ReadWhitespaces, v2 ← ReadVal, ReadWhitespaces, ReadLinebreaks, return (instruction.icmpop thety lhs cond v1 v2) -- Reads "freeze" instruction. def ReadFreezeInst (lhs:reg): parser instruction := do str "freeze", ReadWhitespaces, v ← ReadVal, ReadWhitespaces, ReadLinebreaks, return (instruction.unaryop lhs uopcode.freeze (ty.arbitrary_int) v (ty.arbitrary_int)) -- Reads an instruction. def ReadInstruction: parser instruction := do ReadWhitespaces, lhs ← ReadReg, ReadWhitespaces, str "=", ReadWhitespaces, (ReadBinaryInst lhs "add" bopcode.add) <|> (ReadBinaryInst lhs "sub" bopcode.sub) <|> (ReadBinaryInst lhs "mul" bopcode.mul) <|> (ReadBinaryInst lhs "udiv" bopcode.udiv) <|> (ReadBinaryInst lhs "urem" bopcode.urem) <|> (ReadBinaryInst lhs "sdiv" bopcode.sdiv) <|> (ReadBinaryInst lhs "srem" bopcode.srem) <|> (ReadBinaryInst lhs "shl" bopcode.shl) <|> (ReadBinaryInst lhs "ashr" bopcode.ashr) <|> (ReadBinaryInst lhs "lshr" bopcode.lshr) <|> (ReadBinaryInst lhs "and" bopcode.and) <|> (ReadBinaryInst lhs "or" bopcode.or) <|> (ReadBinaryInst lhs "xor" bopcode.xor) <|> (ReadCastInst lhs "zext" uopcode.zext) <|> (ReadCastInst lhs "sext" uopcode.sext) <|> (ReadCastInst lhs "trunc" uopcode.trunc) <|> (ReadSelectInst lhs) <|> (ReadICmpInst lhs) <|> (ReadFreezeInst lhs) <|> failure -- Reads a program. def ReadProgram: parser program := do ReadWhitespaces, insts ← many ReadInstruction, return (program.mk insts) def ReadTransformation: parser transformation := do ReadWhitespaces, str "Name:", name ← ReadLine, p1 ← ReadProgram, str "=>", p2 ← ReadProgram, return (transformation.mk name p1 p2) def ReadTransformations: parser (list transformation) := do tfs ← many ReadTransformation, return tfs
9c8898f99c733c7c379f00892f3e2741a2bb2e56
5ae26df177f810c5006841e9c73dc56e01b978d7
/src/order/basic.lean
32d18f7b5e6a29187204c13ddd2161df5f7ee8c4
[ "Apache-2.0" ]
permissive
ChrisHughes24/mathlib
98322577c460bc6b1fe5c21f42ce33ad1c3e5558
a2a867e827c2a6702beb9efc2b9282bd801d5f9a
refs/heads/master
1,583,848,251,477
1,565,164,247,000
1,565,164,247,000
129,409,993
0
1
Apache-2.0
1,565,164,817,000
1,523,628,059,000
Lean
UTF-8
Lean
false
false
25,742
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Mario Carneiro -/ import logic.basic data.sum data.set.basic algebra.order open function /- TODO: automatic construction of dual definitions / theorems -/ universes u v w variables {α : Type u} {β : Type v} {γ : Type w} {r : α → α → Prop} protected noncomputable def classical.decidable_linear_order [I : linear_order α] : decidable_linear_order α := { decidable_le := classical.dec_rel _, ..I } theorem ge_of_eq [preorder α] {a b : α} : a = b → a ≥ b := λ h, h ▸ le_refl a theorem is_refl.swap (r) [is_refl α r] : is_refl α (swap r) := ⟨refl_of r⟩ theorem is_irrefl.swap (r) [is_irrefl α r] : is_irrefl α (swap r) := ⟨irrefl_of r⟩ theorem is_trans.swap (r) [is_trans α r] : is_trans α (swap r) := ⟨λ a b c h₁ h₂, trans_of r h₂ h₁⟩ theorem is_antisymm.swap (r) [is_antisymm α r] : is_antisymm α (swap r) := ⟨λ a b h₁ h₂, antisymm h₂ h₁⟩ theorem is_asymm.swap (r) [is_asymm α r] : is_asymm α (swap r) := ⟨λ a b h₁ h₂, asymm_of r h₂ h₁⟩ theorem is_total.swap (r) [is_total α r] : is_total α (swap r) := ⟨λ a b, (total_of r a b).swap⟩ theorem is_trichotomous.swap (r) [is_trichotomous α r] : is_trichotomous α (swap r) := ⟨λ a b, by simpa [swap, or.comm, or.left_comm] using trichotomous_of r a b⟩ theorem is_preorder.swap (r) [is_preorder α r] : is_preorder α (swap r) := {..@is_refl.swap α r _, ..@is_trans.swap α r _} theorem is_strict_order.swap (r) [is_strict_order α r] : is_strict_order α (swap r) := {..@is_irrefl.swap α r _, ..@is_trans.swap α r _} theorem is_partial_order.swap (r) [is_partial_order α r] : is_partial_order α (swap r) := {..@is_preorder.swap α r _, ..@is_antisymm.swap α r _} theorem is_total_preorder.swap (r) [is_total_preorder α r] : is_total_preorder α (swap r) := {..@is_preorder.swap α r _, ..@is_total.swap α r _} theorem is_linear_order.swap (r) [is_linear_order α r] : is_linear_order α (swap r) := {..@is_partial_order.swap α r _, ..@is_total.swap α r _} def antisymm_of_asymm (r) [is_asymm α r] : is_antisymm α r := ⟨λ x y h₁ h₂, (asymm h₁ h₂).elim⟩ /- Convert algebraic structure style to explicit relation style typeclasses -/ instance [preorder α] : is_refl α (≤) := ⟨le_refl⟩ instance [preorder α] : is_refl α (≥) := is_refl.swap _ instance [preorder α] : is_trans α (≤) := ⟨@le_trans _ _⟩ instance [preorder α] : is_trans α (≥) := is_trans.swap _ instance [preorder α] : is_preorder α (≤) := {} instance [preorder α] : is_preorder α (≥) := {} instance [preorder α] : is_irrefl α (<) := ⟨lt_irrefl⟩ instance [preorder α] : is_irrefl α (>) := is_irrefl.swap _ instance [preorder α] : is_trans α (<) := ⟨@lt_trans _ _⟩ instance [preorder α] : is_trans α (>) := is_trans.swap _ instance [preorder α] : is_asymm α (<) := ⟨@lt_asymm _ _⟩ instance [preorder α] : is_asymm α (>) := is_asymm.swap _ instance [preorder α] : is_antisymm α (<) := antisymm_of_asymm _ instance [preorder α] : is_antisymm α (>) := antisymm_of_asymm _ instance [preorder α] : is_strict_order α (<) := {} instance [preorder α] : is_strict_order α (>) := {} instance preorder.is_total_preorder [preorder α] [is_total α (≤)] : is_total_preorder α (≤) := {} instance [partial_order α] : is_antisymm α (≤) := ⟨@le_antisymm _ _⟩ instance [partial_order α] : is_antisymm α (≥) := is_antisymm.swap _ instance [partial_order α] : is_partial_order α (≤) := {} instance [partial_order α] : is_partial_order α (≥) := {} instance [linear_order α] : is_total α (≤) := ⟨le_total⟩ instance [linear_order α] : is_total α (≥) := is_total.swap _ instance linear_order.is_total_preorder [linear_order α] : is_total_preorder α (≤) := by apply_instance instance [linear_order α] : is_total_preorder α (≥) := {} instance [linear_order α] : is_linear_order α (≤) := {} instance [linear_order α] : is_linear_order α (≥) := {} instance [linear_order α] : is_trichotomous α (<) := ⟨lt_trichotomy⟩ instance [linear_order α] : is_trichotomous α (>) := is_trichotomous.swap _ theorem preorder.ext {α} {A B : preorder α} (H : ∀ x y : α, (by haveI := A; exact x ≤ y) ↔ x ≤ y) : A = B := begin resetI, cases A, cases B, congr, { funext x y, exact propext (H x y) }, { funext x y, dsimp [(≤)] at A_lt_iff_le_not_le B_lt_iff_le_not_le H, simp [A_lt_iff_le_not_le, B_lt_iff_le_not_le, H] }, end theorem partial_order.ext {α} {A B : partial_order α} (H : ∀ x y : α, (by haveI := A; exact x ≤ y) ↔ x ≤ y) : A = B := by haveI this := preorder.ext H; cases A; cases B; injection this; congr' theorem linear_order.ext {α} {A B : linear_order α} (H : ∀ x y : α, (by haveI := A; exact x ≤ y) ↔ x ≤ y) : A = B := by haveI this := partial_order.ext H; cases A; cases B; injection this; congr' /-- Given an order `R` on `β` and a function `f : α → β`, the preimage order on `α` is defined by `x ≤ y ↔ f x ≤ f y`. It is the unique order on `α` making `f` an order embedding (assuming `f` is injective). -/ @[simp] def order.preimage {α β} (f : α → β) (s : β → β → Prop) (x y : α) := s (f x) (f y) infix ` ⁻¹'o `:80 := order.preimage section monotone variables [preorder α] [preorder β] [preorder γ] /-- A function between preorders is monotone if `a ≤ b` implies `f a ≤ f b`. -/ def monotone (f : α → β) := ∀⦃a b⦄, a ≤ b → f a ≤ f b theorem monotone_id : @monotone α α _ _ id := assume x y h, h theorem monotone_const {b : β} : monotone (λ(a:α), b) := assume x y h, le_refl b theorem monotone_comp {f : α → β} {g : β → γ} (m_f : monotone f) (m_g : monotone g) : monotone (g ∘ f) := assume a b h, m_g (m_f h) lemma monotone_of_monotone_nat {f : ℕ → α} (hf : ∀n, f n ≤ f (n + 1)) : monotone f | n m h := begin induction h, { refl }, { transitivity, assumption, exact hf _ } end end monotone def order_dual (α : Type*) := α namespace order_dual instance (α : Type*) [has_le α] : has_le (order_dual α) := ⟨λx y:α, y ≤ x⟩ instance (α : Type*) [has_lt α] : has_lt (order_dual α) := ⟨λx y:α, y < x⟩ instance (α : Type*) [preorder α] : preorder (order_dual α) := { le_refl := le_refl, le_trans := assume a b c hab hbc, le_trans hbc hab, lt_iff_le_not_le := λ _ _, lt_iff_le_not_le, .. order_dual.has_le α, .. order_dual.has_lt α } instance (α : Type*) [partial_order α] : partial_order (order_dual α) := { le_antisymm := assume a b hab hba, @le_antisymm α _ a b hba hab, .. order_dual.preorder α } instance (α : Type*) [linear_order α] : linear_order (order_dual α) := { le_total := assume a b:α, le_total b a, .. order_dual.partial_order α } instance (α : Type*) [decidable_linear_order α] : decidable_linear_order (order_dual α) := { decidable_le := show decidable_rel (λa b:α, b ≤ a), by apply_instance, decidable_lt := show decidable_rel (λa b:α, b < a), by apply_instance, .. order_dual.linear_order α } instance : Π [inhabited α], inhabited (order_dual α) := id end order_dual /- order instances on the function space -/ instance pi.preorder {ι : Type u} {α : ι → Type v} [∀i, preorder (α i)] : preorder (Πi, α i) := { le := λx y, ∀i, x i ≤ y i, le_refl := assume a i, le_refl (a i), le_trans := assume a b c h₁ h₂ i, le_trans (h₁ i) (h₂ i) } instance pi.partial_order {ι : Type u} {α : ι → Type v} [∀i, partial_order (α i)] : partial_order (Πi, α i) := { le_antisymm := λf g h1 h2, funext (λb, le_antisymm (h1 b) (h2 b)), ..pi.preorder } theorem comp_le_comp_left_of_monotone [preorder α] [preorder β] [preorder γ] {f : β → α} {g h : γ → β} (m_f : monotone f) (le_gh : g ≤ h) : has_le.le.{max w u} (f ∘ g) (f ∘ h) := assume x, m_f (le_gh x) section monotone variables [preorder α] [preorder γ] theorem monotone_lam {f : α → β → γ} (m : ∀b, monotone (λa, f a b)) : monotone f := assume a a' h b, m b h theorem monotone_app (f : β → α → γ) (b : β) (m : monotone (λa b, f b a)) : monotone (f b) := assume a a' h, m h b end monotone def preorder.lift {α β} (f : α → β) (i : preorder β) : preorder α := by exactI { le := λx y, f x ≤ f y, le_refl := λ a, le_refl _, le_trans := λ a b c, le_trans, lt := λx y, f x < f y, lt_iff_le_not_le := λ a b, lt_iff_le_not_le } def partial_order.lift {α β} (f : α → β) (inj : injective f) (i : partial_order β) : partial_order α := by exactI { le_antisymm := λ a b h₁ h₂, inj (le_antisymm h₁ h₂), .. preorder.lift f (by apply_instance) } def linear_order.lift {α β} (f : α → β) (inj : injective f) (i : linear_order β) : linear_order α := by exactI { le_total := λx y, le_total (f x) (f y), .. partial_order.lift f inj (by apply_instance) } def decidable_linear_order.lift {α β} (f : α → β) (inj : injective f) (i : decidable_linear_order β) : decidable_linear_order α := by exactI { decidable_le := λ x y, show decidable (f x ≤ f y), by apply_instance, decidable_lt := λ x y, show decidable (f x < f y), by apply_instance, decidable_eq := λ x y, decidable_of_iff _ ⟨@inj x y, congr_arg f⟩, .. linear_order.lift f inj (by apply_instance) } instance subtype.preorder {α} [i : preorder α] (p : α → Prop) : preorder (subtype p) := preorder.lift subtype.val i instance subtype.partial_order {α} [i : partial_order α] (p : α → Prop) : partial_order (subtype p) := partial_order.lift subtype.val subtype.val_injective i instance subtype.linear_order {α} [i : linear_order α] (p : α → Prop) : linear_order (subtype p) := linear_order.lift subtype.val subtype.val_injective i instance subtype.decidable_linear_order {α} [i : decidable_linear_order α] (p : α → Prop) : decidable_linear_order (subtype p) := decidable_linear_order.lift subtype.val subtype.val_injective i instance prod.has_le (α : Type u) (β : Type v) [has_le α] [has_le β] : has_le (α × β) := ⟨λp q, p.1 ≤ q.1 ∧ p.2 ≤ q.2⟩ instance prod.preorder (α : Type u) (β : Type v) [preorder α] [preorder β] : preorder (α × β) := { le_refl := assume ⟨a, b⟩, ⟨le_refl a, le_refl b⟩, le_trans := assume ⟨a, b⟩ ⟨c, d⟩ ⟨e, f⟩ ⟨hac, hbd⟩ ⟨hce, hdf⟩, ⟨le_trans hac hce, le_trans hbd hdf⟩, .. prod.has_le α β } /-- The pointwise partial order on a product. (The lexicographic ordering is defined in order/lexicographic.lean, and the instances are available via the type synonym `lex α β = α × β`.) -/ instance prod.partial_order (α : Type u) (β : Type v) [partial_order α] [partial_order β] : partial_order (α × β) := { le_antisymm := assume ⟨a, b⟩ ⟨c, d⟩ ⟨hac, hbd⟩ ⟨hca, hdb⟩, prod.ext (le_antisymm hac hca) (le_antisymm hbd hdb), .. prod.preorder α β } /- additional order classes -/ /-- order without a top element; somtimes called cofinal -/ class no_top_order (α : Type u) [preorder α] : Prop := (no_top : ∀a:α, ∃a', a < a') lemma no_top [preorder α] [no_top_order α] : ∀a:α, ∃a', a < a' := no_top_order.no_top /-- order without a bottom element; somtimes called coinitial or dense -/ class no_bot_order (α : Type u) [preorder α] : Prop := (no_bot : ∀a:α, ∃a', a' < a) lemma no_bot [preorder α] [no_bot_order α] : ∀a:α, ∃a', a' < a := no_bot_order.no_bot /-- An order is dense if there is an element between any pair of distinct elements. -/ class densely_ordered (α : Type u) [preorder α] : Prop := (dense : ∀a₁ a₂:α, a₁ < a₂ → ∃a, a₁ < a ∧ a < a₂) lemma dense [preorder α] [densely_ordered α] : ∀{a₁ a₂:α}, a₁ < a₂ → ∃a, a₁ < a ∧ a < a₂ := densely_ordered.dense lemma le_of_forall_le_of_dense [linear_order α] [densely_ordered α] {a₁ a₂ : α} (h : ∀a₃>a₂, a₁ ≤ a₃) : a₁ ≤ a₂ := le_of_not_gt $ assume ha, let ⟨a, ha₁, ha₂⟩ := dense ha in lt_irrefl a $ lt_of_lt_of_le ‹a < a₁› (h _ ‹a₂ < a›) lemma eq_of_le_of_forall_le_of_dense [linear_order α] [densely_ordered α] {a₁ a₂ : α} (h₁ : a₂ ≤ a₁) (h₂ : ∀a₃>a₂, a₁ ≤ a₃) : a₁ = a₂ := le_antisymm (le_of_forall_le_of_dense h₂) h₁ lemma le_of_forall_ge_of_dense [linear_order α] [densely_ordered α] {a₁ a₂ : α}(h : ∀a₃<a₁, a₂ ≥ a₃) : a₁ ≤ a₂ := le_of_not_gt $ assume ha, let ⟨a, ha₁, ha₂⟩ := dense ha in lt_irrefl a $ lt_of_le_of_lt (h _ ‹a < a₁›) ‹a₂ < a› lemma eq_of_le_of_forall_ge_of_dense [linear_order α] [densely_ordered α] {a₁ a₂ : α} (h₁ : a₂ ≤ a₁) (h₂ : ∀a₃<a₁, a₂ ≥ a₃) : a₁ = a₂ := le_antisymm (le_of_forall_ge_of_dense h₂) h₁ lemma dense_or_discrete [linear_order α] {a₁ a₂ : α} (h : a₁ < a₂) : (∃a, a₁ < a ∧ a < a₂) ∨ ((∀a>a₁, a ≥ a₂) ∧ (∀a<a₂, a ≤ a₁)) := classical.or_iff_not_imp_left.2 $ assume h, ⟨assume a ha₁, le_of_not_gt $ assume ha₂, h ⟨a, ha₁, ha₂⟩, assume a ha₂, le_of_not_gt $ assume ha₁, h ⟨a, ha₁, ha₂⟩⟩ lemma trans_trichotomous_left [is_trans α r] [is_trichotomous α r] {a b c : α} : ¬r b a → r b c → r a c := begin intros h₁ h₂, rcases trichotomous_of r a b with h₃|h₃|h₃, exact trans h₃ h₂, rw h₃, exact h₂, exfalso, exact h₁ h₃ end lemma trans_trichotomous_right [is_trans α r] [is_trichotomous α r] {a b c : α} : r a b → ¬r c b → r a c := begin intros h₁ h₂, rcases trichotomous_of r b c with h₃|h₃|h₃, exact trans h₁ h₃, rw ←h₃, exact h₁, exfalso, exact h₂ h₃ end variables {s : β → β → Prop} {t : γ → γ → Prop} theorem is_irrefl_of_is_asymm [is_asymm α r] : is_irrefl α r := ⟨λ a h, asymm h h⟩ /-- Construct a partial order from a `is_strict_order` relation -/ def partial_order_of_SO (r) [is_strict_order α r] : partial_order α := { le := λ x y, x = y ∨ r x y, lt := r, le_refl := λ x, or.inl rfl, le_trans := λ x y z h₁ h₂, match y, z, h₁, h₂ with | _, _, or.inl rfl, h₂ := h₂ | _, _, h₁, or.inl rfl := h₁ | _, _, or.inr h₁, or.inr h₂ := or.inr (trans h₁ h₂) end, le_antisymm := λ x y h₁ h₂, match y, h₁, h₂ with | _, or.inl rfl, h₂ := rfl | _, h₁, or.inl rfl := rfl | _, or.inr h₁, or.inr h₂ := (asymm h₁ h₂).elim end, lt_iff_le_not_le := λ x y, ⟨λ h, ⟨or.inr h, not_or (λ e, by rw e at h; exact irrefl _ h) (asymm h)⟩, λ ⟨h₁, h₂⟩, h₁.resolve_left (λ e, h₂ $ e ▸ or.inl rfl)⟩ } /-- This is basically the same as `is_strict_total_order`, but that definition is in Type (probably by mistake) and also has redundant assumptions. -/ @[algebra] class is_strict_total_order' (α : Type u) (lt : α → α → Prop) extends is_trichotomous α lt, is_strict_order α lt : Prop. /-- Construct a linear order from a `is_strict_total_order'` relation -/ def linear_order_of_STO' (r) [is_strict_total_order' α r] : linear_order α := { le_total := λ x y, match y, trichotomous_of r x y with | y, or.inl h := or.inl (or.inr h) | _, or.inr (or.inl rfl) := or.inl (or.inl rfl) | _, or.inr (or.inr h) := or.inr (or.inr h) end, ..partial_order_of_SO r } /-- Construct a decidable linear order from a `is_strict_total_order'` relation -/ def decidable_linear_order_of_STO' (r) [is_strict_total_order' α r] [decidable_rel r] : decidable_linear_order α := by letI LO := linear_order_of_STO' r; exact { decidable_le := λ x y, decidable_of_iff (¬ r y x) (@not_lt _ _ y x), ..LO } noncomputable def classical.DLO (α) [LO : linear_order α] : decidable_linear_order α := { decidable_le := classical.dec_rel _, ..LO } theorem is_strict_total_order'.swap (r) [is_strict_total_order' α r] : is_strict_total_order' α (swap r) := {..is_trichotomous.swap r, ..is_strict_order.swap r} instance [linear_order α] : is_strict_total_order' α (<) := {} /-- A connected order is one satisfying the condition `a < c → a < b ∨ b < c`. This is recognizable as an intuitionistic substitute for `a ≤ b ∨ b ≤ a` on the constructive reals, and is also known as negative transitivity, since the contrapositive asserts transitivity of the relation `¬ a < b`. -/ @[algebra] class is_order_connected (α : Type u) (lt : α → α → Prop) : Prop := (conn : ∀ a b c, lt a c → lt a b ∨ lt b c) theorem is_order_connected.neg_trans {r : α → α → Prop} [is_order_connected α r] {a b c} (h₁ : ¬ r a b) (h₂ : ¬ r b c) : ¬ r a c := mt (is_order_connected.conn a b c) $ by simp [h₁, h₂] theorem is_strict_weak_order_of_is_order_connected [is_asymm α r] [is_order_connected α r] : is_strict_weak_order α r := { trans := λ a b c h₁ h₂, (is_order_connected.conn _ c _ h₁).resolve_right (asymm h₂), incomp_trans := λ a b c ⟨h₁, h₂⟩ ⟨h₃, h₄⟩, ⟨is_order_connected.neg_trans h₁ h₃, is_order_connected.neg_trans h₄ h₂⟩, ..@is_irrefl_of_is_asymm α r _ } instance is_order_connected_of_is_strict_total_order' [is_strict_total_order' α r] : is_order_connected α r := ⟨λ a b c h, (trichotomous _ _).imp_right (λ o, o.elim (λ e, e ▸ h) (λ h', trans h' h))⟩ instance is_strict_total_order_of_is_strict_total_order' [is_strict_total_order' α r] : is_strict_total_order α r := {..is_strict_weak_order_of_is_order_connected} instance [linear_order α] : is_strict_total_order α (<) := by apply_instance instance [linear_order α] : is_order_connected α (<) := by apply_instance instance [linear_order α] : is_incomp_trans α (<) := by apply_instance instance [linear_order α] : is_strict_weak_order α (<) := by apply_instance /-- An extensional relation is one in which an element is determined by its set of predecessors. It is named for the `x ∈ y` relation in set theory, whose extensionality is one of the first axioms of ZFC. -/ @[algebra] class is_extensional (α : Type u) (r : α → α → Prop) : Prop := (ext : ∀ a b, (∀ x, r x a ↔ r x b) → a = b) instance is_extensional_of_is_strict_total_order' [is_strict_total_order' α r] : is_extensional α r := ⟨λ a b H, ((@trichotomous _ r _ a b) .resolve_left $ mt (H _).2 (irrefl a)) .resolve_right $ mt (H _).1 (irrefl b)⟩ /-- A well order is a well-founded linear order. -/ @[algebra] class is_well_order (α : Type u) (r : α → α → Prop) extends is_strict_total_order' α r : Prop := (wf : well_founded r) instance is_well_order.is_strict_total_order {α} (r : α → α → Prop) [is_well_order α r] : is_strict_total_order α r := by apply_instance instance is_well_order.is_extensional {α} (r : α → α → Prop) [is_well_order α r] : is_extensional α r := by apply_instance instance is_well_order.is_trichotomous {α} (r : α → α → Prop) [is_well_order α r] : is_trichotomous α r := by apply_instance instance is_well_order.is_trans {α} (r : α → α → Prop) [is_well_order α r] : is_trans α r := by apply_instance instance is_well_order.is_irrefl {α} (r : α → α → Prop) [is_well_order α r] : is_irrefl α r := by apply_instance instance is_well_order.is_asymm {α} (r : α → α → Prop) [is_well_order α r] : is_asymm α r := by apply_instance noncomputable def decidable_linear_order_of_is_well_order (r : α → α → Prop) [is_well_order α r] : decidable_linear_order α := by { haveI := linear_order_of_STO' r, exact classical.DLO α } instance empty_relation.is_well_order [subsingleton α] : is_well_order α empty_relation := { trichotomous := λ a b, or.inr $ or.inl $ subsingleton.elim _ _, irrefl := λ a, id, trans := λ a b c, false.elim, wf := ⟨λ a, ⟨_, λ y, false.elim⟩⟩ } instance nat.lt.is_well_order : is_well_order ℕ (<) := ⟨nat.lt_wf⟩ instance sum.lex.is_well_order [is_well_order α r] [is_well_order β s] : is_well_order (α ⊕ β) (sum.lex r s) := { trichotomous := λ a b, by cases a; cases b; simp; apply trichotomous, irrefl := λ a, by cases a; simp; apply irrefl, trans := λ a b c, by cases a; cases b; simp; cases c; simp; apply trans, wf := sum.lex_wf (is_well_order.wf r) (is_well_order.wf s) } instance prod.lex.is_well_order [is_well_order α r] [is_well_order β s] : is_well_order (α × β) (prod.lex r s) := { trichotomous := λ ⟨a₁, a₂⟩ ⟨b₁, b₂⟩, match @trichotomous _ r _ a₁ b₁ with | or.inl h₁ := or.inl $ prod.lex.left _ _ _ h₁ | or.inr (or.inr h₁) := or.inr $ or.inr $ prod.lex.left _ _ _ h₁ | or.inr (or.inl e) := e ▸ match @trichotomous _ s _ a₂ b₂ with | or.inl h := or.inl $ prod.lex.right _ _ h | or.inr (or.inr h) := or.inr $ or.inr $ prod.lex.right _ _ h | or.inr (or.inl e) := e ▸ or.inr $ or.inl rfl end end, irrefl := λ ⟨a₁, a₂⟩ h, by cases h with _ _ _ _ h _ _ _ h; [exact irrefl _ h, exact irrefl _ h], trans := λ a b c h₁ h₂, begin cases h₁ with a₁ a₂ b₁ b₂ ab a₁ b₁ b₂ ab; cases h₂ with _ _ c₁ c₂ bc _ _ c₂ bc, { exact prod.lex.left _ _ _ (trans ab bc) }, { exact prod.lex.left _ _ _ ab }, { exact prod.lex.left _ _ _ bc }, { exact prod.lex.right _ _ (trans ab bc) } end, wf := prod.lex_wf (is_well_order.wf r) (is_well_order.wf s) } /-- An unbounded or cofinal set -/ def unbounded (r : α → α → Prop) (s : set α) : Prop := ∀ a, ∃ b ∈ s, ¬ r b a /-- A bounded or final set -/ def bounded (r : α → α → Prop) (s : set α) : Prop := ∃a, ∀ b ∈ s, r b a @[simp] lemma not_bounded_iff {r : α → α → Prop} (s : set α) : ¬bounded r s ↔ unbounded r s := begin classical, simp only [bounded, unbounded, not_forall, not_exists, exists_prop, not_and, not_not] end @[simp] lemma not_unbounded_iff {r : α → α → Prop} (s : set α) : ¬unbounded r s ↔ bounded r s := by { classical, rw [not_iff_comm, not_bounded_iff] } namespace well_founded theorem has_min {α} {r : α → α → Prop} (H : well_founded r) (p : set α) : p ≠ ∅ → ∃ a ∈ p, ∀ x ∈ p, ¬ r x a := by classical; exact not_imp_comm.1 (λ he, set.eq_empty_iff_forall_not_mem.2 $ λ a, acc.rec_on (H.apply a) $ λ a H IH h, he ⟨_, h, λ y, imp_not_comm.1 (IH y)⟩) /-- The minimum element of a nonempty set in a well-founded order -/ noncomputable def min {α} {r : α → α → Prop} (H : well_founded r) (p : set α) (h : p ≠ ∅) : α := classical.some (H.has_min p h) theorem min_mem {α} {r : α → α → Prop} (H : well_founded r) (p : set α) (h : p ≠ ∅) : H.min p h ∈ p := let ⟨h, _⟩ := classical.some_spec (H.has_min p h) in h theorem not_lt_min {α} {r : α → α → Prop} (H : well_founded r) (p : set α) (h : p ≠ ∅) {x} (xp : x ∈ p) : ¬ r x (H.min p h) := let ⟨_, h'⟩ := classical.some_spec (H.has_min p h) in h' _ xp open set protected noncomputable def sup {α} {r : α → α → Prop} (wf : well_founded r) (s : set α) (h : bounded r s) : α := wf.min { x | ∀a ∈ s, r a x } (ne_empty_iff_exists_mem.mpr h) protected def lt_sup {α} {r : α → α → Prop} (wf : well_founded r) {s : set α} (h : bounded r s) {x} (hx : x ∈ s) : r x (wf.sup s h) := min_mem wf { x | ∀a ∈ s, r a x } (ne_empty_iff_exists_mem.mpr h) x hx section local attribute [instance, priority 0] classical.prop_decidable protected noncomputable def succ {α} {r : α → α → Prop} (wf : well_founded r) (x : α) : α := if h : ∃y, r x y then wf.min { y | r x y } (ne_empty_iff_exists_mem.mpr h) else x protected lemma lt_succ {α} {r : α → α → Prop} (wf : well_founded r) {x : α} (h : ∃y, r x y) : r x (wf.succ x) := by { rw [well_founded.succ, dif_pos h], apply min_mem } end protected lemma lt_succ_iff {α} {r : α → α → Prop} [wo : is_well_order α r] {x : α} (h : ∃y, r x y) (y : α) : r y (wo.wf.succ x) ↔ r y x ∨ y = x := begin split, { intro h', have : ¬r x y, { intro hy, rw [well_founded.succ, dif_pos] at h', exact wo.wf.not_lt_min _ (ne_empty_iff_exists_mem.mpr h) hy h' }, rcases trichotomous_of r x y with hy | hy | hy, exfalso, exact this hy, right, exact hy.symm, left, exact hy }, rintro (hy | rfl), exact trans hy (wo.wf.lt_succ h), exact wo.wf.lt_succ h end end well_founded variable (r) local infix ` ≼ ` : 50 := r /-- A family of elements of α is directed (with respect to a relation `≼` on α) if there is a member of the family `≼`-above any pair in the family. -/ def directed {ι : Sort v} (f : ι → α) := ∀x y, ∃z, f x ≼ f z ∧ f y ≼ f z /-- A subset of α is directed if there is an element of the set `≼`-above any pair of elements in the set. -/ def directed_on (s : set α) := ∀ (x ∈ s) (y ∈ s), ∃z ∈ s, x ≼ z ∧ y ≼ z theorem directed_on_iff_directed {s} : @directed_on α r s ↔ directed r (coe : s → α) := by simp [directed, directed_on]; refine ball_congr (λ x hx, by simp; refl) theorem directed_comp {ι} (f : ι → β) (g : β → α) : directed r (g ∘ f) ↔ directed (g ⁻¹'o r) f := iff.rfl theorem directed_mono {s : α → α → Prop} {ι} (f : ι → α) (H : ∀ a b, r a b → s a b) (h : directed r f) : directed s f := λ a b, let ⟨c, h₁, h₂⟩ := h a b in ⟨c, H _ _ h₁, H _ _ h₂⟩ class directed_order (α : Type u) extends preorder α := (directed : ∀ i j : α, ∃ k, i ≤ k ∧ j ≤ k)
4c61876cd5cd7e249ae2cde4fe995c23b8df8ad9
2a70b774d16dbdf5a533432ee0ebab6838df0948
/_target/deps/mathlib/src/field_theory/mv_polynomial.lean
e46087b8b61c84461b2fd98c19552074a7ccfaf3
[ "Apache-2.0" ]
permissive
hjvromen/lewis
40b035973df7c77ebf927afab7878c76d05ff758
105b675f73630f028ad5d890897a51b3c1146fb0
refs/heads/master
1,677,944,636,343
1,676,555,301,000
1,676,555,301,000
327,553,599
0
0
null
null
null
null
UTF-8
Lean
false
false
3,911
lean
/- Copyright (c) 2019 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Johannes Hölzl Multivariate functions of the form `α^n → α` are isomorphic to multivariate polynomials in `n` variables. -/ import ring_theory.ideal.operations import linear_algebra.finsupp_vector_space import algebra.char_p.basic noncomputable theory open_locale classical open set linear_map submodule open_locale big_operators namespace mv_polynomial universes u v variables {σ : Type u} {α : Type v} section variables (σ α) [field α] (m : ℕ) lemma quotient_mk_comp_C_injective (I : ideal (mv_polynomial σ α)) (hI : I ≠ ⊤) : function.injective ((ideal.quotient.mk I).comp mv_polynomial.C) := begin refine (ring_hom.injective_iff _).2 (λ x hx, _), rw [ring_hom.comp_apply, ideal.quotient.eq_zero_iff_mem] at hx, refine classical.by_contradiction (λ hx0, absurd (I.eq_top_iff_one.2 _) hI), have := I.smul_mem (mv_polynomial.C x⁻¹) hx, rwa [smul_eq_mul, ← mv_polynomial.C.map_mul, inv_mul_cancel hx0, mv_polynomial.C_1] at this, end def restrict_total_degree : submodule α (mv_polynomial σ α) := finsupp.supported _ _ {n | n.sum (λn e, e) ≤ m } lemma mem_restrict_total_degree (p : mv_polynomial σ α) : p ∈ restrict_total_degree σ α m ↔ p.total_degree ≤ m := begin rw [total_degree, finset.sup_le_iff], refl end end section variables (σ α) def restrict_degree (m : ℕ) [field α] : submodule α (mv_polynomial σ α) := finsupp.supported _ _ {n | ∀i, n i ≤ m } end lemma mem_restrict_degree [field α] (p : mv_polynomial σ α) (n : ℕ) : p ∈ restrict_degree σ α n ↔ (∀s ∈ p.support, ∀i, (s : σ →₀ ℕ) i ≤ n) := begin rw [restrict_degree, finsupp.mem_supported], refl end lemma mem_restrict_degree_iff_sup [field α] (p : mv_polynomial σ α) (n : ℕ) : p ∈ restrict_degree σ α n ↔ ∀i, p.degrees.count i ≤ n := begin simp only [mem_restrict_degree, degrees, multiset.count_sup, finsupp.count_to_multiset, finset.sup_le_iff], exact ⟨assume h n s hs, h s hs n, assume h s hs n, h n s hs⟩ end lemma map_range_eq_map {β : Type*} [comm_ring α] [comm_ring β] (p : mv_polynomial σ α) (f : α →+* β) : finsupp.map_range f f.map_zero p = map f p := begin rw [← finsupp.sum_single p, finsupp.sum], -- It's not great that we need to use an `erw` here, -- but hopefully it will become smoother when we move entirely away from `is_semiring_hom`. erw [finsupp.map_range_finset_sum (f : α →+ β)], rw [← p.support.sum_hom (map f)], { refine finset.sum_congr rfl (assume n _, _), rw [finsupp.map_range_single, ← monomial, ← monomial, map_monomial], refl, }, apply_instance end section variables (σ α) lemma is_basis_monomials [field α] : is_basis α ((λs, (monomial s 1 : mv_polynomial σ α))) := suffices is_basis α (λ (sa : Σ _, unit), (monomial sa.1 1 : mv_polynomial σ α)), begin apply is_basis.comp this (λ (s : σ →₀ ℕ), ⟨s, punit.star⟩), split, { intros x y hxy, simpa using hxy }, { intros x, rcases x with ⟨x₁, x₂⟩, use x₁, rw punit_eq punit.star x₂ } end, begin apply finsupp.is_basis_single (λ _ _, (1 : α)), intro _, apply is_basis_singleton_one, end end end mv_polynomial namespace mv_polynomial universe u variables (σ : Type u) (α : Type u) [field α] open_locale classical lemma dim_mv_polynomial : vector_space.dim α (mv_polynomial σ α) = cardinal.mk (σ →₀ ℕ) := by rw [← cardinal.lift_inj, ← (is_basis_monomials σ α).mk_eq_dim] end mv_polynomial namespace mv_polynomial variables (σ : Type*) (R : Type*) [comm_ring R] (p : ℕ) instance [char_p R p] : char_p (mv_polynomial σ R) p := { cast_eq_zero_iff := λ n, by rw [← C_eq_coe_nat, ← C_0, C_inj, char_p.cast_eq_zero_iff R p] } end mv_polynomial