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
3517983137be1d7ea8d20272db061ad35296b315
d5ff69c5608a867046609101d89910f1257aaf8c
/src/2020/functions/bijection_game.lean
58a3427700bdc15f0302747d4961919519508189
[]
no_license
jiaminglimjm/M40001_lean
c299ff574a22d3a636a2b9720dc9b5e853a3bab0
c8bd8922f37f3e10e2d448f226798ebd0a2af232
refs/heads/master
1,672,312,761,737
1,603,095,035,000
1,603,095,035,000
304,831,347
0
0
null
1,603,094,893,000
1,602,922,986,000
null
UTF-8
Lean
false
false
1,454
lean
import tactic -- Let's make bijections between β„•, β„€ and β„š. #check infinite #print prefix infinite #print infinite #print int.infinite #check infinite.of_injective example : infinite β„• := by apply_instance example : infinite β„€ := by show_term {apply_instance} #check rat.of_int example : infinite β„š := infinite.of_injective (coe : β„€ β†’ β„š) begin intros a b, intro h, exact (rat.coe_int_inj a b).mp h, end namespace countably_infinite example (a b : β„•) : 2 * a = 2 * b β†’ a = b := by begin intro h, apply mul_left_cancel' _ h, norm_num, end def bool_times_nat : bool Γ— β„• ≃ β„• := { to_fun := Ξ» bn, if bn.1 = tt then bn.2 * 2 else bn.2 * 2 + 1, inv_fun := Ξ» d, ⟨d % 2 = 0, d / 2⟩, left_inv := begin intro bn, cases bn with b n, cases b, -- TODO -- make snippet { suffices : Β¬(1 + 2 * n) % 2 = 0 ∧ (1 + 2 * n) / 2 = n, simpa [mul_comm, add_comm], have h : (1 + 2 * n) % 2 = 1, simp, split, { simp }, { have h2 := nat.mod_add_div (1 + 2 * n) 2, have h3 : 2 * ((1 + 2 * n) / 2) = 2 * n β†’ (1 + 2 * n) / 2 = n := Ξ» h, mul_left_cancel' _ h, simp * at *, cc } }, { simp } end , right_inv := begin intro n, suffices : ite (n % 2 = 0) (n / 2 * 2) (n / 2 * 2 + 1) = n, simpa, split_ifs, end } example (X : Type) (h : X ≃ β„•) : X ≃ X Γ— bool := sorry end countably_infinite
e3c389e3ea1ce80e8f8a0c06afffada5c90feb06
b7f22e51856f4989b970961f794f1c435f9b8f78
/library/data/nat/examples/fib2.lean
2ff8e444f0274206a485a572cbf3575c0ff728bc
[ "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
1,166
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura Show that tail recursive fib is equal to standard one. -/ import data.nat open nat definition fib : nat β†’ nat | 0 := 1 | 1 := 1 | (n+2) := fib (n+1) + fib n private definition fib_fast_aux : nat β†’ nat β†’ nat β†’ nat | 0 i j := j | (n+1) i j := fib_fast_aux n j (j+i) lemma fib_fast_aux_lemma : βˆ€ n m, fib_fast_aux n (fib m) (fib (succ m)) = fib (succ (n + m)) | 0 m := by rewrite zero_add | (succ n) m := begin have ih : fib_fast_aux n (fib (succ m)) (fib (succ (succ m))) = fib (succ (n + succ m)), from fib_fast_aux_lemma n (succ m), have h₁ : fib (succ m) + fib m = fib (succ (succ m)), from rfl, unfold fib_fast_aux, rewrite [h₁, ih, succ_add, add_succ] end definition fib_fast (n: nat) := fib_fast_aux n 0 1 lemma fib_fast_eq_fib : βˆ€ n, fib_fast n = fib n | 0 := rfl | (succ n) := begin have h₁ : fib_fast_aux n (fib 0) (fib 1) = fib (succ n), from !fib_fast_aux_lemma, unfold fib_fast, unfold fib_fast_aux, krewrite h₁ end
be2db7a90e3f73c5c746e270fcdeb45ddde7b6dc
b3fced0f3ff82d577384fe81653e47df68bb2fa1
/src/topology/local_homeomorph.lean
42bbe797d9e0ef0477f933e7961197ecc1923995
[ "Apache-2.0" ]
permissive
ratmice/mathlib
93b251ef5df08b6fd55074650ff47fdcc41a4c75
3a948a6a4cd5968d60e15ed914b1ad2f4423af8d
refs/heads/master
1,599,240,104,318
1,572,981,183,000
1,572,981,183,000
219,830,178
0
0
Apache-2.0
1,572,980,897,000
1,572,980,896,000
null
UTF-8
Lean
false
false
27,695
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 data.equiv.local_equiv topology.continuous_on topology.homeomorph /-! # Local homeomorphisms This file defines homeomorphisms between open subsets of topological spaces. An element `e` of `local_homeomorph Ξ± Ξ²` is an extension of `local_equiv Ξ± Ξ²`, i.e., it is a pair of functions `e.to_fun` and `e.inv_fun`, inverse of each other on the sets `e.source` and `e.target`. Additionally, we require that these sets are open, and that the functions are continuous on them. Equivalently, they are homeomorphisms there. Contrary to equivs, we do not register the coercion to functions and we use explicitly to_fun and inv_fun: coercions create unification problems for manifolds. ## Main definitions `homeomorph.to_local_homeomorph`: associating a local homeomorphism to a homeomorphism, with source = target = univ `local_homeomorph.symm` : the inverse of a local homeomorphism `local_homeomorph.trans` : the composition of two local homeomorphisms `local_homeomorph.refl` : the identity local homeomorphism `local_homeomorph.of_set`: the identity on a set `s` `eq_on_source` : equivalence relation describing the "right" notion of equality for local homeomorphisms ## Implementation notes Most statements are copied from their local_equiv versions, although some care is required especially when restricting to subsets, as these should be open subsets. For design notes, see `local_equiv.lean`. -/ open function set variables {Ξ± : Type*} {Ξ² : Type*} {Ξ³ : Type*} {Ξ΄ : Type*} [topological_space Ξ±] [topological_space Ξ²] [topological_space Ξ³] [topological_space Ξ΄] /-- local homeomorphisms, defined on open subsets of the space -/ structure local_homeomorph (Ξ± : Type*) (Ξ² : Type*) [topological_space Ξ±] [topological_space Ξ²] extends local_equiv Ξ± Ξ² := (open_source : is_open source) (open_target : is_open target) (continuous_to_fun : continuous_on to_fun source) (continuous_inv_fun : continuous_on inv_fun target) /-- A homeomorphism induces a local homeomorphism on the whole space -/ def homeomorph.to_local_homeomorph (e : homeomorph Ξ± Ξ²) : local_homeomorph Ξ± Ξ² := { open_source := is_open_univ, open_target := is_open_univ, continuous_to_fun := by { erw ← continuous_iff_continuous_on_univ, exact e.continuous_to_fun }, continuous_inv_fun := by { erw ← continuous_iff_continuous_on_univ, exact e.continuous_inv_fun }, ..e.to_equiv.to_local_equiv } namespace local_homeomorph variables (e : local_homeomorph Ξ± Ξ²) (e' : local_homeomorph Ξ² Ξ³) lemma eq_of_local_equiv_eq {e e' : local_homeomorph Ξ± Ξ²} (h : e.to_local_equiv = e'.to_local_equiv) : e = e' := begin cases e, cases e', dsimp at *, induction h, refl end /-- Two local homeomorphisms are equal when they have equal to_fun, inv_fun and source. It is not sufficient to have equal to_fun and source, as this only determines inv_fun on the target. This would only be true for a weaker notion of equality, arguably the right one, called `eq_on_source`. -/ @[extensionality] protected lemma ext (e' : local_homeomorph Ξ± Ξ²) (h : βˆ€x, e.to_fun x = e'.to_fun x) (hinv: βˆ€x, e.inv_fun x = e'.inv_fun x) (hs : e.source = e'.source) : e = e' := eq_of_local_equiv_eq (local_equiv.ext e.to_local_equiv e'.to_local_equiv h hinv hs) /-- The inverse of a local homeomorphism -/ protected def symm : local_homeomorph Ξ² Ξ± := { open_source := e.open_target, open_target := e.open_source, continuous_to_fun := e.continuous_inv_fun, continuous_inv_fun := e.continuous_to_fun, ..e.to_local_equiv.symm } @[simp] lemma symm_to_local_equiv : e.symm.to_local_equiv = e.to_local_equiv.symm := rfl @[simp] lemma symm_to_fun : e.symm.to_fun = e.inv_fun := rfl @[simp] lemma symm_inv_fun : e.symm.inv_fun = e.to_fun := rfl @[simp] lemma symm_source : e.symm.source = e.target := rfl @[simp] lemma symm_target : e.symm.target = e.source := rfl @[simp] lemma symm_symm : e.symm.symm = e := eq_of_local_equiv_eq $ by simp /-- A local homeomorphism is continuous at any point of its source -/ lemma continuous_at_to_fun {x : Ξ±} (h : x ∈ e.source) : continuous_at e.to_fun x := (e.continuous_to_fun x h).continuous_at (mem_nhds_sets e.open_source h) /-- A local homeomorphism inverse is continuous at any point of its target -/ lemma continuous_at_inv_fun {x : Ξ²} (h : x ∈ e.target) : continuous_at e.inv_fun x := e.symm.continuous_at_to_fun h /-- Preimage of interior or interior of preimage coincide for local homeomorphisms, when restricted to the source. -/ lemma preimage_interior (s : set Ξ²) : e.source ∩ e.to_fun ⁻¹' (interior s) = e.source ∩ interior (e.to_fun ⁻¹' s) := begin apply subset.antisymm, { exact e.continuous_to_fun.preimage_interior_subset_interior_preimage e.open_source }, { calc e.source ∩ interior (e.to_fun ⁻¹' s) = (e.source ∩ e.to_fun ⁻¹' e.target) ∩ interior (e.to_fun ⁻¹' s) : begin congr, apply (inter_eq_self_of_subset_left _).symm, apply e.to_local_equiv.source_subset_preimage_target, end ... = (e.source ∩ interior (e.to_fun ⁻¹' s)) ∩ (e.to_fun ⁻¹' e.target) : by simp [inter_comm, inter_assoc] ... = (e.source ∩ e.to_fun ⁻¹' (e.inv_fun ⁻¹' (interior (e.to_fun ⁻¹' s)))) ∩ (e.to_fun ⁻¹' e.target) : by rw e.to_local_equiv.source_inter_preimage_inv_preimage ... = e.source ∩ e.to_fun ⁻¹' (e.target ∩ e.inv_fun ⁻¹' (interior (e.to_fun ⁻¹' s))) : by rw [inter_comm e.target, preimage_inter, inter_assoc] ... βŠ† e.source ∩ e.to_fun ⁻¹' (e.target ∩ interior (e.inv_fun ⁻¹' (e.to_fun ⁻¹' s))) : begin apply inter_subset_inter (subset.refl _) (preimage_mono _), exact e.continuous_inv_fun.preimage_interior_subset_interior_preimage e.open_target end ... = e.source ∩ e.to_fun ⁻¹' (interior (e.target ∩ e.inv_fun ⁻¹' (e.to_fun ⁻¹' s))) : by rw [interior_inter, interior_eq_of_open e.open_target] ... = e.source ∩ e.to_fun ⁻¹' (interior (e.target ∩ s)) : by rw e.to_local_equiv.target_inter_inv_preimage_preimage ... = e.source ∩ e.to_fun ⁻¹' e.target ∩ e.to_fun ⁻¹' (interior s) : by rw [interior_inter, preimage_inter, interior_eq_of_open e.open_target, inter_assoc] ... = e.source ∩ e.to_fun ⁻¹' (interior s) : begin congr, apply inter_eq_self_of_subset_left, apply e.to_local_equiv.source_subset_preimage_target end } end /-- The image of an open set in the source is open. -/ lemma image_open_of_open {s : set Ξ±} (hs : is_open s) (h : s βŠ† e.source) : is_open (e.to_fun '' s) := begin have : e.to_fun '' s = e.target ∩ e.inv_fun ⁻¹' s := e.to_local_equiv.image_eq_target_inter_inv_preimage h, rw this, exact e.continuous_inv_fun.preimage_open_of_open e.open_target hs end /-- Restricting a local homeomorphism `e` to `e.source ∩ s` when `s` is open. This is sometimes hard to use because of the openness assumption, but it has the advantage that when it can be used then its local_equiv is defeq to local_equiv.restr -/ protected def restr_open (s : set Ξ±) (hs : is_open s) : local_homeomorph Ξ± Ξ² := { open_source := is_open_inter e.open_source hs, open_target := (continuous_on_open_iff e.open_target).1 e.continuous_inv_fun s hs, continuous_to_fun := e.continuous_to_fun.mono (inter_subset_left _ _), continuous_inv_fun := e.continuous_inv_fun.mono (inter_subset_left _ _), ..e.to_local_equiv.restr s} @[simp] lemma restr_open_source (s : set Ξ±) (hs : is_open s) : (e.restr_open s hs).source = e.source ∩ s := rfl @[simp] lemma restr_open_to_local_equiv (s : set Ξ±) (hs : is_open s) : (e.restr_open s hs).to_local_equiv = e.to_local_equiv.restr s := rfl /-- Restricting a local homeomorphism `e` to `e.source ∩ interior s`. We use the interior to make sure that the restriction is well defined whatever the set s, since local homeomorphisms are by definition defined on open sets. In applications where `s` is open, this coincides with the restriction of local equivalences -/ protected def restr (s : set Ξ±) : local_homeomorph Ξ± Ξ² := e.restr_open (interior s) is_open_interior @[simp] lemma restr_to_fun (s : set Ξ±) : (e.restr s).to_fun = e.to_fun := rfl @[simp] lemma restr_inv_fun (s : set Ξ±) : (e.restr s).inv_fun = e.inv_fun := rfl @[simp] lemma restr_source (s : set Ξ±) : (e.restr s).source = e.source ∩ interior s := rfl @[simp] lemma restr_target (s : set Ξ±) : (e.restr s).target = e.target ∩ e.inv_fun ⁻¹' (interior s) := rfl @[simp] lemma restr_to_local_equiv (s : set Ξ±) : (e.restr s).to_local_equiv = (e.to_local_equiv).restr (interior s) := rfl lemma restr_source' (s : set Ξ±) (hs : is_open s) : (e.restr s).source = e.source ∩ s := by rw [e.restr_source, interior_eq_of_open hs] lemma restr_to_local_equiv' (s : set Ξ±) (hs : is_open s): (e.restr s).to_local_equiv = e.to_local_equiv.restr s := by rw [e.restr_to_local_equiv, interior_eq_of_open hs] lemma restr_eq_of_source_subset {e : local_homeomorph Ξ± Ξ²} {s : set Ξ±} (h : e.source βŠ† s) : e.restr s = e := begin apply eq_of_local_equiv_eq, rw restr_to_local_equiv, apply local_equiv.restr_eq_of_source_subset, have := interior_mono h, rwa interior_eq_of_open (e.open_source) at this end @[simp] lemma restr_univ {e : local_homeomorph Ξ± Ξ²} : e.restr univ = e := restr_eq_of_source_subset (subset_univ _) lemma restr_source_inter (s : set Ξ±) : e.restr (e.source ∩ s) = e.restr s := begin refine local_homeomorph.ext _ _ (Ξ»x, rfl) (Ξ»x, rfl) _, simp [interior_eq_of_open e.open_source], rw [← inter_assoc, inter_self] end /-- The identity on the whole space as a local homeomorphism. -/ protected def refl (Ξ± : Type*) [topological_space Ξ±] : local_homeomorph Ξ± Ξ± := (homeomorph.refl Ξ±).to_local_homeomorph @[simp] lemma refl_source : (local_homeomorph.refl Ξ±).source = univ := rfl @[simp] lemma refl_target : (local_homeomorph.refl Ξ±).target = univ := rfl @[simp] lemma refl_symm : (local_homeomorph.refl Ξ±).symm = local_homeomorph.refl Ξ± := rfl @[simp] lemma refl_to_fun : (local_homeomorph.refl Ξ±).to_fun = id := rfl @[simp] lemma refl_inv_fun : (local_homeomorph.refl Ξ±).inv_fun = id := rfl @[simp] lemma refl_local_equiv : (local_homeomorph.refl Ξ±).to_local_equiv = local_equiv.refl Ξ± := rfl section variables {s : set Ξ±} (hs : is_open s) /-- The identity local equiv on a set `s` -/ def of_set (s : set Ξ±) (hs : is_open s) : local_homeomorph Ξ± Ξ± := { open_source := hs, open_target := hs, continuous_to_fun := continuous_id.continuous_on, continuous_inv_fun := continuous_id.continuous_on, ..local_equiv.of_set s } @[simp] lemma of_set_source : (of_set s hs).source = s := rfl @[simp] lemma of_set_target : (of_set s hs).target = s := rfl @[simp] lemma of_set_to_fun : (of_set s hs).to_fun = id := rfl @[simp] lemma of_set_inv_fun : (of_set s hs).inv_fun = id := rfl @[simp] lemma of_set_symm : (of_set s hs).symm = of_set s hs := rfl @[simp] lemma of_set_to_local_equiv : (of_set s hs).to_local_equiv = local_equiv.of_set s := rfl end /-- Composition of two local homeomorphisms when the target of the first and the source of the second coincide. -/ protected def trans' (h : e.target = e'.source) : local_homeomorph Ξ± Ξ³ := { open_source := e.open_source, open_target := e'.open_target, continuous_to_fun := begin apply continuous_on.comp e'.continuous_to_fun e.continuous_to_fun, rw ← h, exact e.to_local_equiv.source_subset_preimage_target end, continuous_inv_fun := begin apply continuous_on.comp e.continuous_inv_fun e'.continuous_inv_fun, rw h, exact e'.to_local_equiv.target_subset_preimage_source end, ..local_equiv.trans' e.to_local_equiv e'.to_local_equiv h } /-- Composing two local homeomorphisms, by restricting to the maximal domain where their composition is well defined. -/ protected def trans : local_homeomorph Ξ± Ξ³ := local_homeomorph.trans' (e.symm.restr_open e'.source e'.open_source).symm (e'.restr_open e.target e.open_target) (by simp [inter_comm]) @[simp] lemma trans_to_local_equiv : (e.trans e').to_local_equiv = e.to_local_equiv.trans e'.to_local_equiv := rfl @[simp] lemma trans_to_fun : (e.trans e').to_fun = e'.to_fun ∘ e.to_fun := rfl @[simp] lemma trans_inv_fun : (e.trans e').inv_fun = e.inv_fun ∘ e'.inv_fun := rfl lemma trans_symm_eq_symm_trans_symm : (e.trans e').symm = e'.symm.trans e.symm := by cases e; cases e'; refl /- This could be considered as a simp lemma, but there are many situations where it makes something simple into something more complicated. -/ lemma trans_source : (e.trans e').source = e.source ∩ e.to_fun ⁻¹' e'.source := local_equiv.trans_source e.to_local_equiv e'.to_local_equiv lemma trans_source' : (e.trans e').source = e.source ∩ e.to_fun ⁻¹' (e.target ∩ e'.source) := local_equiv.trans_source' e.to_local_equiv e'.to_local_equiv lemma trans_source'' : (e.trans e').source = e.inv_fun '' (e.target ∩ e'.source) := local_equiv.trans_source'' e.to_local_equiv e'.to_local_equiv lemma image_trans_source : e.to_fun '' (e.trans e').source = e.target ∩ e'.source := local_equiv.image_trans_source e.to_local_equiv e'.to_local_equiv lemma trans_target : (e.trans e').target = e'.target ∩ e'.inv_fun ⁻¹' e.target := rfl lemma trans_target' : (e.trans e').target = e'.target ∩ e'.inv_fun ⁻¹' (e'.source ∩ e.target) := trans_source' e'.symm e.symm lemma trans_target'' : (e.trans e').target = e'.to_fun '' (e'.source ∩ e.target) := trans_source'' e'.symm e.symm lemma inv_image_trans_target : e'.inv_fun '' (e.trans e').target = e'.source ∩ e.target := image_trans_source e'.symm e.symm lemma trans_assoc (e'' : local_homeomorph Ξ³ Ξ΄) : (e.trans e').trans e'' = e.trans (e'.trans e'') := eq_of_local_equiv_eq $ local_equiv.trans_assoc e.to_local_equiv e'.to_local_equiv e''.to_local_equiv @[simp] lemma trans_refl : e.trans (local_homeomorph.refl Ξ²) = e := eq_of_local_equiv_eq $ local_equiv.trans_refl e.to_local_equiv @[simp] lemma refl_trans : (local_homeomorph.refl Ξ±).trans e = e := eq_of_local_equiv_eq $ local_equiv.refl_trans e.to_local_equiv lemma trans_of_set {s : set Ξ²} (hs : is_open s) : e.trans (of_set s hs) = e.restr (e.to_fun ⁻¹' s) := local_homeomorph.ext _ _ (Ξ»x, rfl) (Ξ»x, rfl) $ by simp [local_equiv.trans_source, (e.preimage_interior _).symm, interior_eq_of_open hs] lemma trans_of_set' {s : set Ξ²} (hs : is_open s) : e.trans (of_set s hs) = e.restr (e.source ∩ e.to_fun ⁻¹' s) := by rw [trans_of_set, restr_source_inter] lemma of_set_trans {s : set Ξ±} (hs : is_open s) : (of_set s hs).trans e = e.restr s := local_homeomorph.ext _ _ (Ξ»x, rfl) (Ξ»x, rfl) $ by simp [local_equiv.trans_source, interior_eq_of_open hs, inter_comm] lemma of_set_trans' {s : set Ξ±} (hs : is_open s) : (of_set s hs).trans e = e.restr (e.source ∩ s) := by rw [of_set_trans, restr_source_inter] lemma restr_trans (s : set Ξ±) : (e.restr s).trans e' = (e.trans e').restr s := eq_of_local_equiv_eq $ local_equiv.restr_trans e.to_local_equiv e'.to_local_equiv (interior s) /-- `eq_on_source e e'` means that `e` and `e'` have the same source, and coincide there. They should really be considered the same local equiv. -/ def eq_on_source (e e' : local_homeomorph Ξ± Ξ²) : Prop := e.source = e'.source ∧ (βˆ€x ∈ e.source, e.to_fun x = e'.to_fun x) lemma eq_on_source_iff (e e' : local_homeomorph Ξ± Ξ²) : eq_on_source e e' ↔ local_equiv.eq_on_source e.to_local_equiv e'.to_local_equiv := by refl /-- `eq_on_source` is an equivalence relation -/ instance : setoid (local_homeomorph Ξ± Ξ²) := { r := eq_on_source, iseqv := ⟨ Ξ»e, (@local_equiv.eq_on_source_setoid Ξ± Ξ²).iseqv.1 e.to_local_equiv, Ξ»e e' h, (@local_equiv.eq_on_source_setoid Ξ± Ξ²).iseqv.2.1 ((eq_on_source_iff e e').1 h), Ξ»e e' e'' h h', (@local_equiv.eq_on_source_setoid Ξ± Ξ²).iseqv.2.2 ((eq_on_source_iff e e').1 h) ((eq_on_source_iff e' e'').1 h')⟩ } lemma eq_on_source_refl : e β‰ˆ e := setoid.refl _ /-- If two local homeomorphisms are equivalent, so are their inverses -/ lemma eq_on_source_symm {e e' : local_homeomorph Ξ± Ξ²} (h : e β‰ˆ e') : e.symm β‰ˆ e'.symm := local_equiv.eq_on_source_symm h /-- Two equivalent local homeomorphisms have the same source -/ lemma source_eq_of_eq_on_source {e e' : local_homeomorph Ξ± Ξ²} (h : e β‰ˆ e') : e.source = e'.source := h.1 /-- Two equivalent local homeomorphisms have the same target -/ lemma target_eq_of_eq_on_source {e e' : local_homeomorph Ξ± Ξ²} (h : e β‰ˆ e') : e.target = e'.target := (eq_on_source_symm h).1 /-- Two equivalent local homeomorphisms have coinciding `to_fun` on the source -/ lemma apply_eq_of_eq_on_source {e e' : local_homeomorph Ξ± Ξ²} (h : e β‰ˆ e') {x : Ξ±} (hx : x ∈ e.source) : e.to_fun x = e'.to_fun x := h.2 x hx /-- Two equivalent local homeomorphisms have coinciding `inv_fun` on the target -/ lemma inv_apply_eq_of_eq_on_source {e e' : local_homeomorph Ξ± Ξ²} (h : e β‰ˆ e') {x : Ξ²} (hx : x ∈ e.target) : e.inv_fun x = e'.inv_fun x := (eq_on_source_symm h).2 x hx /-- Composition of local homeomorphisms respects equivalence -/ lemma eq_on_source_trans {e e' : local_homeomorph Ξ± Ξ²} {f f' : local_homeomorph Ξ² Ξ³} (he : e β‰ˆ e') (hf : f β‰ˆ f') : e.trans f β‰ˆ e'.trans f' := begin change local_equiv.eq_on_source (e.trans f).to_local_equiv (e'.trans f').to_local_equiv, simp only [trans_to_local_equiv], apply local_equiv.eq_on_source_trans, exact he, exact hf end /-- Restriction of local homeomorphisms respects equivalence -/ lemma eq_on_source_restr {e e' : local_homeomorph Ξ± Ξ²} (he : e β‰ˆ e') (s : set Ξ±) : e.restr s β‰ˆ e'.restr s := local_equiv.eq_on_source_restr he _ /-- Composition of a local homeomorphism and its inverse is equivalent to the restriction of the identity to the source -/ lemma trans_self_symm : e.trans e.symm β‰ˆ local_homeomorph.of_set e.source e.open_source := local_equiv.trans_self_symm _ lemma trans_symm_self : e.symm.trans e β‰ˆ local_homeomorph.of_set e.target e.open_target := e.symm.trans_self_symm lemma eq_of_eq_on_source_univ {e e' : local_homeomorph Ξ± Ξ²} (h : e β‰ˆ e') (s : e.source = univ) (t : e.target = univ) : e = e' := eq_of_local_equiv_eq $ local_equiv.eq_of_eq_on_source_univ _ _ h s t section prod /-- The product of two local homeomorphisms, as a local homeomorphism on the product space. -/ def prod (e : local_homeomorph Ξ± Ξ²) (e' : local_homeomorph Ξ³ Ξ΄) : local_homeomorph (Ξ± Γ— Ξ³) (Ξ² Γ— Ξ΄) := { open_source := is_open_prod e.open_source e'.open_source, open_target := is_open_prod e.open_target e'.open_target, continuous_to_fun := continuous_on.prod (continuous_on.comp e.continuous_to_fun continuous_fst.continuous_on (prod_subset_preimage_fst _ _)) (continuous_on.comp e'.continuous_to_fun continuous_snd.continuous_on (prod_subset_preimage_snd _ _)), continuous_inv_fun := continuous_on.prod (continuous_on.comp e.continuous_inv_fun continuous_fst.continuous_on (prod_subset_preimage_fst _ _)) (continuous_on.comp e'.continuous_inv_fun continuous_snd.continuous_on (prod_subset_preimage_snd _ _)), ..e.to_local_equiv.prod e'.to_local_equiv } @[simp] lemma prod_to_local_equiv (e : local_homeomorph Ξ± Ξ²) (e' : local_homeomorph Ξ³ Ξ΄) : (e.prod e').to_local_equiv = e.to_local_equiv.prod e'.to_local_equiv := rfl @[simp] lemma prod_source (e : local_homeomorph Ξ± Ξ²) (e' : local_homeomorph Ξ³ Ξ΄) : (e.prod e').source = set.prod e.source e'.source := rfl @[simp] lemma prod_target (e : local_homeomorph Ξ± Ξ²) (e' : local_homeomorph Ξ³ Ξ΄) : (e.prod e').target = set.prod e.target e'.target := rfl @[simp] lemma prod_to_fun (e : local_homeomorph Ξ± Ξ²) (e' : local_homeomorph Ξ³ Ξ΄) : (e.prod e').to_fun = (Ξ»p, (e.to_fun p.1, e'.to_fun p.2)) := rfl @[simp] lemma prod_inv_fun (e : local_homeomorph Ξ± Ξ²) (e' : local_homeomorph Ξ³ Ξ΄) : (e.prod e').inv_fun = (Ξ»p, (e.inv_fun p.1, e'.inv_fun p.2)) := rfl end prod section continuity /-- Continuity within a set at a point can be read under right composition with a local homeomorphism, if the point is in its target -/ lemma continuous_within_at_iff_continuous_within_at_comp_right {f : Ξ² β†’ Ξ³} {s : set Ξ²} {x : Ξ²} (h : x ∈ e.target) : continuous_within_at f s x ↔ continuous_within_at (f ∘ e.to_fun) (e.to_fun ⁻¹' s) (e.inv_fun x) := begin split, { assume f_cont, have : e.to_fun (e.inv_fun x) = x := e.right_inv h, rw ← this at f_cont, have : e.source ∈ nhds (e.inv_fun x) := mem_nhds_sets e.open_source (e.map_target h), rw [← continuous_within_at_inter this, inter_comm], exact continuous_within_at.comp f_cont ((e.continuous_at_to_fun (e.map_target h)).continuous_within_at) (inter_subset_right _ _) }, { assume fe_cont, have : continuous_within_at ((f ∘ e.to_fun) ∘ e.inv_fun) (s ∩ e.target) x, { apply continuous_within_at.comp fe_cont, apply (e.continuous_at_inv_fun h).continuous_within_at, assume x hx, simp [hx.1, hx.2, e.map_target] }, have : continuous_within_at f (s ∩ e.target) x := continuous_within_at.congr this (Ξ»y hy, by simp [hy.2]) (by simp [h]), rwa continuous_within_at_inter at this, exact mem_nhds_sets e.open_target h } end /-- Continuity at a point can be read under right composition with a local homeomorphism, if the point is in its target -/ lemma continuous_at_iff_continuous_at_comp_right {f : Ξ² β†’ Ξ³} {x : Ξ²} (h : x ∈ e.target) : continuous_at f x ↔ continuous_at (f ∘ e.to_fun) (e.inv_fun x) := by rw [← continuous_within_at_univ, e.continuous_within_at_iff_continuous_within_at_comp_right h, preimage_univ, continuous_within_at_univ] /-- A function is continuous on a set if and only if its composition with a local homeomorphism on the right is continuous on the corresponding set. -/ lemma continuous_on_iff_continuous_on_comp_right {f : Ξ² β†’ Ξ³} {s : set Ξ²} (h : s βŠ† e.target) : continuous_on f s ↔ continuous_on (f ∘ e.to_fun) (e.source ∩ e.to_fun ⁻¹' s) := begin split, { assume f_cont x hx, have := e.continuous_within_at_iff_continuous_within_at_comp_right (e.map_source hx.1), rw e.left_inv hx.1 at this, have A := f_cont _ hx.2, rw this at A, exact A.mono (inter_subset_right _ _), }, { assume fe_cont x hx, have := e.continuous_within_at_iff_continuous_within_at_comp_right (h hx), rw this, have : e.source ∈ nhds (e.inv_fun x) := mem_nhds_sets e.open_source (e.map_target (h hx)), rw [← continuous_within_at_inter this, inter_comm], exact fe_cont _ (by simp [hx, h hx, e.map_target (h hx)]) } end /-- Continuity within a set at a point can be read under left composition with a local homeomorphism if a neighborhood of the initial point is sent to the source of the local homeomorphism-/ lemma continuous_within_at_iff_continuous_within_at_comp_left {f : Ξ³ β†’ Ξ±} {s : set Ξ³} {x : Ξ³} (hx : f x ∈ e.source) (h : f ⁻¹' e.source ∈ nhds_within x s) : continuous_within_at f s x ↔ continuous_within_at (e.to_fun ∘ f) s x := begin rw [← continuous_within_at_inter' h, ← continuous_within_at_inter' h], split, { assume f_cont, have : e.source ∈ nhds (f x) := mem_nhds_sets e.open_source hx, apply continuous_within_at.comp (e.continuous_to_fun (f x) hx) f_cont (inter_subset_right _ _) }, { assume fe_cont, have : continuous_within_at (e.inv_fun ∘ (e.to_fun ∘ f)) (s ∩ f ⁻¹' e.source) x, { have : continuous_within_at e.inv_fun univ (e.to_fun (f x)) := (e.continuous_at_inv_fun (e.map_source hx)).continuous_within_at, exact continuous_within_at.comp this fe_cont (subset_univ _) }, exact this.congr (Ξ»y hy, by simp [e.left_inv hy.2]) (by simp [e.left_inv hx]) } end /-- Continuity at a point can be read under left composition with a local homeomorphism if a neighborhood of the initial point is sent to the source of the local homeomorphism-/ lemma continuous_at_iff_continuous_at_comp_left {f : Ξ³ β†’ Ξ±} {x : Ξ³} (h : f ⁻¹' e.source ∈ nhds x) : continuous_at f x ↔ continuous_at (e.to_fun ∘ f) x := begin have hx : f x ∈ e.source := (mem_of_nhds h : _), have h' : f ⁻¹' e.source ∈ nhds_within x univ, by rwa nhds_within_univ, rw [← continuous_within_at_univ, ← continuous_within_at_univ, e.continuous_within_at_iff_continuous_within_at_comp_left hx h'] end /-- A function is continuous on a set if and only if its composition with a local homeomorphism on the left is continuous on the corresponding set. -/ lemma continuous_on_iff_continuous_on_comp_left {f : Ξ³ β†’ Ξ±} {s : set Ξ³} (h : s βŠ† f ⁻¹' e.source) : continuous_on f s ↔ continuous_on (e.to_fun ∘ f) s := begin split, { assume f_cont, exact continuous_on.comp e.continuous_to_fun f_cont h }, { assume fe_cont, have : continuous_on (e.inv_fun ∘ e.to_fun ∘ f) s, { apply continuous_on.comp e.continuous_inv_fun fe_cont, assume x hx, have : f x ∈ e.source := h hx, simp [this] }, refine continuous_on.congr_mono this (Ξ»x hx, _) (subset.refl _), have : f x ∈ e.source := h hx, simp [this] } end end continuity /-- If a local homeomorphism has source and target equal to univ, then it induces a homeomorphism between the whole spaces, expressed in this definition. -/ def to_homeomorph_of_source_eq_univ_target_eq_univ (h : e.source = (univ : set Ξ±)) (h' : e.target = univ) : homeomorph Ξ± Ξ² := { to_fun := e.to_fun, inv_fun := e.inv_fun, left_inv := Ξ»x, e.left_inv $ by { rw h, exact mem_univ _ }, right_inv := Ξ»x, e.right_inv $ by { rw h', exact mem_univ _ }, continuous_to_fun := begin rw [continuous_iff_continuous_on_univ], convert e.continuous_to_fun, rw h end, continuous_inv_fun := begin rw [continuous_iff_continuous_on_univ], convert e.continuous_inv_fun, rw h' end } @[simp] lemma to_homeomorph_to_fun (h : e.source = (univ : set Ξ±)) (h' : e.target = univ) : (e.to_homeomorph_of_source_eq_univ_target_eq_univ h h').to_fun = e.to_fun := rfl @[simp] lemma to_homeomorph_inv_fun (h : e.source = (univ : set Ξ±)) (h' : e.target = univ) : (e.to_homeomorph_of_source_eq_univ_target_eq_univ h h').inv_fun = e.inv_fun := rfl end local_homeomorph namespace homeomorph variables (e : homeomorph Ξ± Ξ²) (e' : homeomorph Ξ² Ξ³) /- Register as simp lemmas that the fields of a local homeomorphism built from a homeomorphism correspond to the fields of the original homeomorphism. -/ @[simp] lemma to_local_homeomorph_source : e.to_local_homeomorph.source = univ := rfl @[simp] lemma to_local_homeomorph_target : e.to_local_homeomorph.target = univ := rfl @[simp] lemma to_local_homeomorph_to_fun : e.to_local_homeomorph.to_fun = e.to_fun := rfl @[simp] lemma to_local_homeomorph_inv_fun : e.to_local_homeomorph.inv_fun = e.inv_fun := rfl @[simp] lemma refl_to_local_homeomorph : (homeomorph.refl Ξ±).to_local_homeomorph = local_homeomorph.refl Ξ± := rfl @[simp] lemma symm_to_local_homeomorph : e.symm.to_local_homeomorph = e.to_local_homeomorph.symm := rfl @[simp] lemma trans_to_local_homeomorph : (e.trans e').to_local_homeomorph = e.to_local_homeomorph.trans e'.to_local_homeomorph := local_homeomorph.eq_of_local_equiv_eq $ equiv.trans_to_local_equiv _ _ end homeomorph
5ec5dd4e283d1ccd194d42257e9528c60fe8f7a5
ae1e94c332e17c7dc7051ce976d5a9eebe7ab8a5
/src/Leanpkg/Resolve.lean
fc379eeefe78093557b474c918cf7a051d72db05
[ "Apache-2.0" ]
permissive
dupuisf/lean4
d082d13b01243e1de29ae680eefb476961221eef
6a39c65bd28eb0e28c3870188f348c8914502718
refs/heads/master
1,676,948,755,391
1,610,665,114,000
1,610,665,114,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,329
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Gabriel Ebner, Sebastian Ullrich -/ import Leanpkg.Manifest import Leanpkg.Proc import Leanpkg.Git namespace Leanpkg def Assignment := List (String Γ— String) namespace Assignment def empty : Assignment := [] def contains (a : Assignment) (s : String) : Bool := (a.lookup s).isSome def insert (a : Assignment) (k v : String) : Assignment := if a.contains k then a else (k, v) :: a def fold {Ξ±} (i : Ξ±) (f : Ξ± β†’ String β†’ String β†’ Ξ±) : Assignment β†’ Ξ± := List.foldl (fun a ⟨k, v⟩ => f a k v) i end Assignment abbrev Solver := StateT Assignment IO def notYetAssigned (d : String) : Solver Bool := do Β¬ (← get).contains d def resolvedPath (d : String) : Solver String := do let some path ← pure ((← get).lookup d) | unreachable! path -- TODO(gabriel): windows? def resolveDir (absOrRel : String) (base : String) : String := if absOrRel.front = '/' then absOrRel -- absolute else base ++ "/" ++ absOrRel def materialize (relpath : String) (dep : Dependency) : Solver Unit := match dep.src with | Source.path dir => do let depdir := resolveDir dir relpath IO.println s!"{dep.name}: using local path {depdir}" modify (Β·.insert dep.name depdir) | Source.git url rev branch => do let depdir := "build/deps/" ++ dep.name let alreadyThere ← IO.isDir depdir if alreadyThere then IO.print s!"{dep.name}: trying to update {depdir} to revision {rev}" IO.println (match branch with | none => "" | some branch => "@" ++ branch) let hash ← gitParseOriginRevision depdir rev let revEx ← gitRevisionExists depdir hash unless revEx do execCmd {cmd := "git", args := #["fetch"], cwd := depdir} else IO.println s!"{dep.name}: cloning {url} to {depdir}" execCmd {cmd := "git", args := #["clone", url, depdir]} let hash ← gitParseOriginRevision depdir rev execCmd {cmd := "git", args := #["checkout", "--detach", hash], cwd := depdir} modify (Β·.insert dep.name depdir) def solveDepsCore (relPath : String) (d : Manifest) : (maxDepth : Nat) β†’ Solver Unit | 0 => throw <| IO.userError "maximum dependency resolution depth reached" | maxDepth + 1 => do let deps ← d.dependencies.filterM (notYetAssigned Β·.name) deps.forM (materialize relPath) for dep in deps do let p ← resolvedPath dep.name let d' ← Manifest.fromFile $ p ++ "/" ++ "leanpkg.toml" unless d'.name = dep.name do throw <| IO.userError <| d.name ++ " (in " ++ relPath ++ ") depends on " ++ d'.name ++ ", but resolved dependency has name " ++ dep.name ++ " (in " ++ p ++ ")" solveDepsCore p d' maxDepth def solveDeps (d : Manifest) : IO Assignment := do let (_, assg) ← (solveDepsCore "." d 1024).run <| Assignment.empty.insert d.name "." assg def constructPathCore (depname : String) (dirname : String) : IO String := do let path ← Manifest.effectivePath (← Manifest.fromFile $ dirname ++ "/" ++ leanpkgTomlFn) return dirname ++ "/" ++ path def constructPath (assg : Assignment) : IO (List String) := do assg.reverse.mapM fun ⟨depname, dirname⟩ => constructPathCore depname dirname end Leanpkg
af3e87af711e68fc0e2e6daae6f8925769cbd687
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/data/equiv/embedding.lean
666ab82e4d65283dedc7533cc9225c893998eb0f
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
3,779
lean
/- Copyright (c) 2021 Eric Rodriguez. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Rodriguez -/ import logic.embedding import data.set.lattice /-! # Equivalences on embeddings This file shows some advanced equivalences on embeddings, useful for constructing larger embeddings from smaller ones. -/ open function.embedding namespace equiv /-- Embeddings from a sum type are equivalent to two separate embeddings with disjoint ranges. -/ def sum_embedding_equiv_prod_embedding_disjoint {Ξ± Ξ² Ξ³ : Type*} : ((Ξ± βŠ• Ξ²) β†ͺ Ξ³) ≃ {f : (Ξ± β†ͺ Ξ³) Γ— (Ξ² β†ͺ Ξ³) // disjoint (set.range f.1) (set.range f.2)} := { to_fun := Ξ» f, ⟨(inl.trans f, inr.trans f), begin rintros _ ⟨⟨a, h⟩, ⟨b, rfl⟩⟩, simp only [trans_apply, inl_apply, inr_apply] at h, have : sum.inl a = sum.inr b := f.injective h, simp only at this, assumption end⟩, inv_fun := Ξ» ⟨⟨f, g⟩, disj⟩, ⟨λ x, match x with | (sum.inl a) := f a | (sum.inr b) := g b end, begin rintros (a₁|b₁) (aβ‚‚|bβ‚‚) f_eq; simp only [equiv.coe_fn_symm_mk, sum.elim_inl, sum.elim_inr] at f_eq, { rw f.injective f_eq }, { simp! only at f_eq, exfalso, exact disj ⟨⟨a₁, by simp⟩, ⟨bβ‚‚, by simp [f_eq]⟩⟩ }, { simp! only at f_eq, exfalso, exact disj ⟨⟨aβ‚‚, by simp⟩, ⟨b₁, by simp [f_eq]⟩⟩ }, { rw g.injective f_eq } end⟩, left_inv := Ξ» f, by { dsimp only, ext, cases x; simp! }, right_inv := Ξ» ⟨⟨f, g⟩, _⟩, by { simp only [prod.mk.inj_iff], split; ext; simp! } } /-- Embeddings whose range lies within a set are equivalent to embeddings to that set. This is `function.embedding.cod_restrict` as an equiv. -/ def cod_restrict (Ξ± : Type*) {Ξ² : Type*} (bs : set Ξ²) : {f : Ξ± β†ͺ Ξ² // βˆ€ a, f a ∈ bs} ≃ (Ξ± β†ͺ bs) := { to_fun := Ξ» f, (f : Ξ± β†ͺ Ξ²).cod_restrict bs f.prop, inv_fun := Ξ» f, ⟨f.trans (function.embedding.subtype _), Ξ» a, (f a).prop⟩, left_inv := Ξ» x, by ext; refl, right_inv := Ξ» x, by ext; refl } /-- Pairs of embeddings with disjoint ranges are equivalent to a dependent sum of embeddings, in which the second embedding cannot take values in the range of the first. -/ def prod_embedding_disjoint_equiv_sigma_embedding_restricted {Ξ± Ξ² Ξ³ : Type*} : {f : (Ξ± β†ͺ Ξ³) Γ— (Ξ² β†ͺ Ξ³) // disjoint (set.range f.1) (set.range f.2)} ≃ (Ξ£ f : Ξ± β†ͺ Ξ³, Ξ² β†ͺ β†₯((set.range f)ᢜ)) := (subtype_prod_equiv_sigma_subtype $ Ξ» (a : Ξ± β†ͺ Ξ³) (b : Ξ² β†ͺ _), disjoint (set.range a) (set.range b)).trans $ equiv.sigma_congr_right $ Ξ» a, (subtype_equiv_prop begin ext f, rw [←set.range_subset_iff, set.subset_compl_iff_disjoint], exact disjoint.comm.trans disjoint_iff, end).trans (cod_restrict _ _) /-- A combination of the above results, allowing us to turn one embedding over a sum type into two dependent embeddings, the second of which avoids any members of the range of the first. This is helpful for constructing larger embeddings out of smaller ones. -/ def sum_embedding_equiv_sigma_embedding_restricted {Ξ± Ξ² Ξ³ : Type*} : ((Ξ± βŠ• Ξ²) β†ͺ Ξ³) ≃ (Ξ£ f : Ξ± β†ͺ Ξ³, Ξ² β†ͺ β†₯((set.range f)ᢜ)) := equiv.trans sum_embedding_equiv_prod_embedding_disjoint prod_embedding_disjoint_equiv_sigma_embedding_restricted /-- Embeddings from a single-member type are equivalent to members of the target type. -/ def unique_embedding_equiv_result {Ξ± Ξ² : Type*} [unique Ξ±] : (Ξ± β†ͺ Ξ²) ≃ Ξ² := { to_fun := Ξ» f, f (default Ξ±), inv_fun := Ξ» x, ⟨λ _, x, Ξ» _ _ _, subsingleton.elim _ _⟩, left_inv := Ξ» _, by { ext, simp_rw [function.embedding.coe_fn_mk], congr }, right_inv := Ξ» _, by simp } end equiv
8314ed4003181108bbfebedcecc39fd3d78687eb
37336e9dcf558f325cf43bdf45bab5d8e455a3b4
/M1F/2017-18/Example_Sheet_01/Question_07/M1F_sheet01_question07.lean
e8663d80199f3628202321a6cc930ebc556f6bdf
[]
no_license
rafaelbailo/xena
bfeab41b3b401c812689a4879d58f2ad046240b5
fad865c3d1bac6279f9a77e8aa6ba7e0cd572e06
refs/heads/master
1,626,002,809,060
1,507,793,086,000
1,507,793,086,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
590
lean
import topology.real -- real numbers live in here in Lean 3.3.0 mathlib -- NB you need mathlib installed to get this working. -- of_rat is the injection from the rationals to the reals. def A : set ℝ := { x | x^2 < 3} def B : set ℝ := {x | (βˆƒ y : β„€, x = of_rat y) ∧ x^2 < 3} def C : set ℝ := {x | x^3 < 3} theorem part_a : of_rat (1/2) ∈ A ∩ B := sorry theorem part_b : of_rat (1/2) ∈ A βˆͺ B := sorry theorem part_c : A βŠ† C := sorry theorem part_d : B βŠ† C := sorry theorem part_e : C βŠ† A βˆͺ B := sorry theorem part_f : (A ∩ B) βˆͺ C = (A βˆͺ B) ∩ C := sorry
a945a11253288a4f246428dd8c5073c7cc30f330
618003631150032a5676f229d13a079ac875ff77
/src/data/list/zip.lean
1539eab5120d39790002956f9ca8389cd2a19b76
[ "Apache-2.0" ]
permissive
awainverse/mathlib
939b68c8486df66cfda64d327ad3d9165248c777
ea76bd8f3ca0a8bf0a166a06a475b10663dec44a
refs/heads/master
1,659,592,962,036
1,590,987,592,000
1,590,987,592,000
268,436,019
1
0
Apache-2.0
1,590,990,500,000
1,590,990,500,000
null
UTF-8
Lean
false
false
5,453
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Kenny Lau -/ import data.list.basic universes u v w z variables {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} {Ξ΄ : Type z} open nat namespace list /- zip & unzip -/ @[simp] theorem zip_cons_cons (a : Ξ±) (b : Ξ²) (l₁ : list Ξ±) (lβ‚‚ : list Ξ²) : zip (a :: l₁) (b :: lβ‚‚) = (a, b) :: zip l₁ lβ‚‚ := rfl @[simp] theorem zip_nil_left (l : list Ξ±) : zip ([] : list Ξ²) l = [] := rfl @[simp] theorem zip_nil_right (l : list Ξ±) : zip l ([] : list Ξ²) = [] := by cases l; refl @[simp] theorem zip_swap : βˆ€ (l₁ : list Ξ±) (lβ‚‚ : list Ξ²), (zip l₁ lβ‚‚).map prod.swap = zip lβ‚‚ l₁ | [] lβ‚‚ := (zip_nil_right _).symm | l₁ [] := by rw zip_nil_right; refl | (a::l₁) (b::lβ‚‚) := by simp only [zip_cons_cons, map_cons, zip_swap l₁ lβ‚‚, prod.swap_prod_mk]; split; refl @[simp] theorem length_zip : βˆ€ (l₁ : list Ξ±) (lβ‚‚ : list Ξ²), length (zip l₁ lβ‚‚) = min (length l₁) (length lβ‚‚) | [] lβ‚‚ := rfl | l₁ [] := by simp only [length, zip_nil_right, min_zero] | (a::l₁) (b::lβ‚‚) := by by simp only [length, zip_cons_cons, length_zip l₁ lβ‚‚, min_add_add_right] theorem zip_append : βˆ€ {l₁ lβ‚‚ r₁ rβ‚‚ : list Ξ±} (h : length l₁ = length lβ‚‚), zip (l₁ ++ r₁) (lβ‚‚ ++ rβ‚‚) = zip l₁ lβ‚‚ ++ zip r₁ rβ‚‚ | [] lβ‚‚ r₁ rβ‚‚ h := by simp only [eq_nil_of_length_eq_zero h.symm]; refl | l₁ [] r₁ rβ‚‚ h := by simp only [eq_nil_of_length_eq_zero h]; refl | (a::l₁) (b::lβ‚‚) r₁ rβ‚‚ h := by simp only [cons_append, zip_cons_cons, zip_append (succ_inj h)]; split; refl theorem zip_map (f : Ξ± β†’ Ξ³) (g : Ξ² β†’ Ξ΄) : βˆ€ (l₁ : list Ξ±) (lβ‚‚ : list Ξ²), zip (l₁.map f) (lβ‚‚.map g) = (zip l₁ lβ‚‚).map (prod.map f g) | [] lβ‚‚ := rfl | l₁ [] := by simp only [map, zip_nil_right] | (a::l₁) (b::lβ‚‚) := by simp only [map, zip_cons_cons, zip_map l₁ lβ‚‚, prod.map]; split; refl theorem zip_map_left (f : Ξ± β†’ Ξ³) (l₁ : list Ξ±) (lβ‚‚ : list Ξ²) : zip (l₁.map f) lβ‚‚ = (zip l₁ lβ‚‚).map (prod.map f id) := by rw [← zip_map, map_id] theorem zip_map_right (f : Ξ² β†’ Ξ³) (l₁ : list Ξ±) (lβ‚‚ : list Ξ²) : zip l₁ (lβ‚‚.map f) = (zip l₁ lβ‚‚).map (prod.map id f) := by rw [← zip_map, map_id] theorem zip_map' (f : Ξ± β†’ Ξ²) (g : Ξ± β†’ Ξ³) : βˆ€ (l : list Ξ±), zip (l.map f) (l.map g) = l.map (Ξ» a, (f a, g a)) | [] := rfl | (a::l) := by simp only [map, zip_cons_cons, zip_map' l]; split; refl theorem mem_zip {a b} : βˆ€ {l₁ : list Ξ±} {lβ‚‚ : list Ξ²}, (a, b) ∈ zip l₁ lβ‚‚ β†’ a ∈ l₁ ∧ b ∈ lβ‚‚ | (_::l₁) (_::lβ‚‚) (or.inl rfl) := ⟨or.inl rfl, or.inl rfl⟩ | (a'::l₁) (b'::lβ‚‚) (or.inr h) := by split; simp only [mem_cons_iff, or_true, mem_zip h] @[simp] theorem unzip_nil : unzip (@nil (Ξ± Γ— Ξ²)) = ([], []) := rfl @[simp] theorem unzip_cons (a : Ξ±) (b : Ξ²) (l : list (Ξ± Γ— Ξ²)) : unzip ((a, b) :: l) = (a :: (unzip l).1, b :: (unzip l).2) := by rw unzip; cases unzip l; refl theorem unzip_eq_map : βˆ€ (l : list (Ξ± Γ— Ξ²)), unzip l = (l.map prod.fst, l.map prod.snd) | [] := rfl | ((a, b) :: l) := by simp only [unzip_cons, map_cons, unzip_eq_map l] theorem unzip_left (l : list (Ξ± Γ— Ξ²)) : (unzip l).1 = l.map prod.fst := by simp only [unzip_eq_map] theorem unzip_right (l : list (Ξ± Γ— Ξ²)) : (unzip l).2 = l.map prod.snd := by simp only [unzip_eq_map] theorem unzip_swap (l : list (Ξ± Γ— Ξ²)) : unzip (l.map prod.swap) = (unzip l).swap := by simp only [unzip_eq_map, map_map]; split; refl theorem zip_unzip : βˆ€ (l : list (Ξ± Γ— Ξ²)), zip (unzip l).1 (unzip l).2 = l | [] := rfl | ((a, b) :: l) := by simp only [unzip_cons, zip_cons_cons, zip_unzip l]; split; refl theorem unzip_zip_left : βˆ€ {l₁ : list Ξ±} {lβ‚‚ : list Ξ²}, length l₁ ≀ length lβ‚‚ β†’ (unzip (zip l₁ lβ‚‚)).1 = l₁ | [] lβ‚‚ h := rfl | l₁ [] h := by rw eq_nil_of_length_eq_zero (eq_zero_of_le_zero h); refl | (a::l₁) (b::lβ‚‚) h := by simp only [zip_cons_cons, unzip_cons, unzip_zip_left (le_of_succ_le_succ h)]; split; refl theorem unzip_zip_right {l₁ : list Ξ±} {lβ‚‚ : list Ξ²} (h : length lβ‚‚ ≀ length l₁) : (unzip (zip l₁ lβ‚‚)).2 = lβ‚‚ := by rw [← zip_swap, unzip_swap]; exact unzip_zip_left h theorem unzip_zip {l₁ : list Ξ±} {lβ‚‚ : list Ξ²} (h : length l₁ = length lβ‚‚) : unzip (zip l₁ lβ‚‚) = (l₁, lβ‚‚) := by rw [← @prod.mk.eta _ _ (unzip (zip l₁ lβ‚‚)), unzip_zip_left (le_of_eq h), unzip_zip_right (ge_of_eq h)] @[simp] theorem length_revzip (l : list Ξ±) : length (revzip l) = length l := by simp only [revzip, length_zip, length_reverse, min_self] @[simp] theorem unzip_revzip (l : list Ξ±) : (revzip l).unzip = (l, l.reverse) := unzip_zip (length_reverse l).symm @[simp] theorem revzip_map_fst (l : list Ξ±) : (revzip l).map prod.fst = l := by rw [← unzip_left, unzip_revzip] @[simp] theorem revzip_map_snd (l : list Ξ±) : (revzip l).map prod.snd = l.reverse := by rw [← unzip_right, unzip_revzip] theorem reverse_revzip (l : list Ξ±) : reverse l.revzip = revzip l.reverse := by rw [← zip_unzip.{u u} (revzip l).reverse, unzip_eq_map]; simp; simp [revzip] theorem revzip_swap (l : list Ξ±) : (revzip l).map prod.swap = revzip l.reverse := by simp [revzip] end list
e56356f4d638df8df14b0b8ee119db5526effbd9
36938939954e91f23dec66a02728db08a7acfcf9
/lean/deps/galois_stdlib/src/galois/data/buffer.lean
29af7a83d4a72d12494a049825bb8edae36c0133
[ "Apache-2.0" ]
permissive
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
12,974
lean
/- Provides a decidable_eq instance for buffer. -/ import data.buffer import galois.algebra.ordered_group import galois.data.array import galois.data.char import galois.data.fin import galois.data.nat namespace galois namespace buffer open buffer section variable {Ξ±:Type _} open galois -- Taken from mathlib @[simp] lemma to_list_append_list (b : buffer Ξ±) (xs : list Ξ±) : to_list (append_list b xs) = to_list b ++ xs := begin induction xs generalizing b, case list.nil { simp [append_list], }, case list.cons : h r ind { cases b, simp! [to_list, to_array, ind], }, end -- Taken from mathlib lemma ext : βˆ€ (b₁ bβ‚‚ : buffer Ξ±), to_list b₁ = to_list bβ‚‚ β†’ b₁ = bβ‚‚ | ⟨n₁, aβ‚βŸ© ⟨nβ‚‚, aβ‚‚βŸ© h := begin simp [to_list, to_array, array] at h, have e : n₁ = nβ‚‚, { rw [←array.to_list_length a₁, ←array.to_list_length aβ‚‚, h], }, subst e, have g : a₁ == list.to_array (aβ‚‚.to_list) := h β–Έ (array.to_list_to_array a₁).symm, rw eq_of_heq (g.trans (array.to_list_to_array aβ‚‚)) end end @[simp] theorem size_append_array {Ξ±} {n : nat} (nz : n > 0) (x : buffer Ξ±) (y : array n Ξ±) (j : nat) (j_lt : j < n) : buffer.size (buffer.append_array nz x y j j_lt) = x.size + j + 1 := begin revert x, induction j, case nat.zero { intro x, cases x, simp [append_array, size], }, case nat.succ : j ind { intro x, cases x with m b, simp [append_array, ind], simp [size, nat.succ_add, nat.add_succ], }, end /- Size of appending to buffers is sum of their sizes.-/ @[simp] theorem size_append {Ξ±} (x y : buffer Ξ±) : (x ++ y).size = x.size + y.size := begin cases y with n b, cases n, case nat.zero { simp [size, append, buffer.append], }, case nat.succ : n { simp [size, append, buffer.append, size_append_array, nat.succ_add], } end @[simp] theorem to_list_length {Ξ±} (b: buffer Ξ±) : list.length (buffer.to_list b) = b.size := begin simp only [buffer.to_list, size, array.to_list_length], end /-- Replace nil.to_list with empty list. -/ @[simp] theorem nil_to_list (Ξ±:Type _) : (@buffer.nil Ξ±).to_list = [] := refl [] /-- Convert read to list read so we can use list lemmas -/ theorem read_to_nth_le {Ξ±} (b: buffer Ξ±) (i:fin b.size) : buffer.read b i = list.nth_le b.to_list i.val ((to_list_length b).symm β–Έ i.is_lt) := begin cases b with n a, simp only [buffer.read, to_list, array.to_list_nth_le', to_array], end /-- Empty buffer converted to array is same as array.nil -/ @[simp] theorem to_array_mk_buffer {Ξ±} : to_array mk_buffer = @array.nil Ξ± := begin apply array.ext, intro i, have h : false := fin.elim0 i, contradiction, end /-- Converting list to bufer and back is round trip. -/ theorem to_list_to_buffer {Ξ±} (l:list Ξ±) : buffer.to_list (list.to_buffer l) = l := begin unfold list.to_buffer, simp only [to_list_append_list], simp only [to_list, to_array_mk_buffer, array.to_list_nil, list.nil_append], end /- Simplify functions calling through as_string -/ theorem to_char_buffer_as_string (l:list char) : l.as_string.to_char_buffer = l.to_buffer := eq.refl l.to_buffer @[simp] theorem size_push_back {Ξ±} (b:buffer Ξ±) (c:Ξ±) : (buffer.push_back b c).size = b.size + 1 := begin cases b, trivial, end @[simp] theorem size_append_list {Ξ±} (b:buffer Ξ±) (l:list Ξ±) : (buffer.append_list b l).size = l.length + b.size := begin revert b, induction l, case list.nil { intro b, simp [buffer.append_list, nat.zero_add], }, case list.cons : x r ind { intro b, simp only [buffer.append_list, ind, size_push_back, list.length_cons, nat.add_succ, nat.add_zero, nat.succ_add], }, end /- Prove definition of reading from concatenation of two buffers. -/ theorem read_append_array {Ξ±} {n:β„•} (nz : n > 0) (x : buffer Ξ±) (b : array n Ξ±) (j:β„•) (j_lt : j < n) (i : fin (append_array nz x b j j_lt).size) : read (append_array nz x b j j_lt) i = if h : i.val < x.size then x.read ⟨i.val, h⟩ else let q : n + (i.val - x.size) - (j + 1) < n := begin cases i with i i_lt, let nz_lt : 0 < n := nz, simp only [nat.sub_lt_to_add, nz_lt, or_true, and_true], apply nat.add_lt_add_left, have x_le := le_of_not_lt h, simp [nat.sub_lt_to_add, x_le], simp [size_append_array] at i_lt, exact i_lt, end in array.read b ⟨n + (i.val - x.size) - (j + 1), q⟩ := begin revert x, induction j, case nat.zero { intros x i, cases i with i i_lt, cases x with m a, dsimp [append_array, buffer.read, size], simp [array.read_push_back_lt_iff], cases (decide (i < m)), case decidable.is_true : lt { simp [lt], }, case decidable.is_false : not_lt { simp [not_lt], dsimp [append_array, size] at i_lt, have p : i ≀ m := nat.le_of_lt_succ i_lt, have q : i-m=0 := nat.sub_eq_zero_of_le p, simp [q], }, }, case nat.succ : j ind { intros x i, cases i with i i_outer_lt, cases x with m a, dsimp [append_array, size], simp [ind], dsimp [size], cases nat.lt_trichotomy i m, case or.inl : i_lt { have i_lt_m_p_1 : i < m + 1 := nat.lt_succ_of_lt i_lt, simp [i_lt, i_lt_m_p_1, buffer.read, array.read_push_back_lt_iff], }, case or.inr : i_geq { cases i_geq, case or.inl : i_eq { have i_lt_m_p_1 : i < m + 1, { simp [i_eq, nat.zero_lt_succ, nat.sub_self], }, simp [i_lt_m_p_1, i_eq, buffer.read, array.read_push_back_lt_iff], apply (congr_arg (array.read b)), apply fin.eq_of_veq, simp [nat.succ_add, nat.add_succ, nat.sub_sub, nat.sub_self], }, case or.inr : i_gt { have not_i_lt_m : Β¬ (i < m) := not_lt_of_lt i_gt, have not_i_lt_m_p_1 : Β¬ (i < m + 1), { apply not_lt_of_le, exact i_gt, }, simp [not_i_lt_m, not_i_lt_m_p_1], apply (congr_arg (array.read b)), apply fin.eq_of_veq, have not_i_m_zero : Β¬(i - m = 0), { intro eq_zero, have not_i_gt := not_lt_of_ge (nat.le_of_sub_eq_zero eq_zero), exact (not_i_gt i_gt), }, simp [fin.val, nat.succ_add, nat.add_succ, nat.sub_succ , nat.add_pred, nat.pred_sub, not_i_m_zero], }, }, }, end /-- Proof needed for starting theorem about read of append. -/ theorem read_append.pr1 {Ξ±} (x y : buffer Ξ±) (i : fin ((x++y).size)) (h : Β¬(i.val < x.size )) : i.val - buffer.size x < buffer.size y := begin have q : buffer.size x ≀ i.val := le_of_not_lt h, simp [nat.sub_lt_left_iff_lt_add q], exact trans_rel_left _ i.is_lt (size_append _ _), end /- Prove definition of reading from concatenation of two buffers. -/ theorem read_append {Ξ±} (x y : buffer Ξ±) (i : fin ((x++y).size)) : (x ++ y).read i = if h : i.val < x.size then x.read ⟨i.val, h⟩ else y.read ⟨i.val - x.size, read_append.pr1 x y i h⟩ := begin cases y with n b, cases n, case nat.zero { cases i with i i_lt, dsimp [has_append.append, buffer.append] at i_lt, -- Simplify left-hand side dsimp [has_append.append, buffer.append], -- Simplify right-hand side simp [i_lt], }, case nat.succ : n { cases i with i i_lt, dsimp [has_append.append, buffer.append], simp [read_append_array ], dsimp [fin.val], cases decide (i < size x), case decidable.is_true : i_lt { simp [i_lt], }, case decidable.is_false : not_i_lt { simp [not_i_lt, buffer.read, nat.add_sub_cancel_left], apply congr_arg (array.read b), exact fin.eq_of_veq (eq.refl _), }, }, end /- Lexicographic reflexive ordering of buffers -/ protected def le {Ξ±} [h:linear_order Ξ±] : buffer Ξ± β†’ buffer Ξ± β†’ Prop | ⟨m,a⟩ ⟨n,b⟩ := array.lex_le a b instance {Ξ±} [h:linear_order Ξ±] : has_le (buffer Ξ±) := ⟨buffer.le⟩ instance decidable_le {Ξ±} [h:linear_order Ξ±] [r:decidable_rel (h.lt)] : Ξ (x y : buffer Ξ±), decidable (x ≀ y) | ⟨m,a⟩ ⟨n,b⟩ := begin simp [has_le.le, buffer.le], apply_instance, end /- Lexicographic strict ordering of buffers -/ protected def lt {Ξ±} [h:linear_order Ξ±] : buffer Ξ± β†’ buffer Ξ± β†’ Prop | ⟨m,a⟩ ⟨n,b⟩ := array.lex_lt a b instance {Ξ±} [h:linear_order Ξ±] : has_lt (buffer Ξ±) := ⟨buffer.lt⟩ instance decidable_lt {Ξ±} [h:linear_order Ξ±] [r:decidable_rel (h.lt)] : Ξ (x y : buffer Ξ±), decidable (x < y) | ⟨m,a⟩ ⟨n,b⟩ := begin simp [has_lt.lt, buffer.lt], apply_instance, end /- Take a specific range of elements out of a buffer. -/ def slice {Ξ±} : Ξ (b : buffer Ξ±) (s e : β„•) (s_le_e : s ≀ e), buffer Ξ± | ⟨n, a⟩ s e s_le_e := if e_le_n : e ≀ n then ⟨e - s, a.slice s e s_le_e e_le_n⟩ else if s_le_n : s ≀ n then ⟨n - s, a.slice s n s_le_n (nat.le_refl _)⟩ else ⟨0, array.nil⟩ /- Take a specific range of elements out of a buffer. -/ @[simp] theorem size_slice {Ξ±} (b : buffer Ξ±) (s e : β„•) (s_le_e : s ≀ e) : size (slice b s e s_le_e) = min e b.size - s := begin cases b with n a, cases (decide (e ≀ n)), case decidable.is_true : e_le_n { simp [slice, e_le_n, size, min_eq_left], }, case decidable.is_false : not_e_le_n { have n_le_e : n ≀ e := le_of_not_ge not_e_le_n, simp [slice, not_e_le_n, size, min_eq_right n_le_e], cases (decide (s ≀ n)), case decidable.is_true : s_le_n { simp [s_le_n, n_le_e, min_eq_right], }, case decidable.is_false : not_s_le_n { simp [not_s_le_n], have n_le_s : n ≀ s := le_of_not_ge not_s_le_n, apply (eq.symm (nat.sub_eq_zero_of_le n_le_s)), }, }, end /-- Introduce a non-dependent function for reading. -/ def try_read {Ξ±} (b:buffer Ξ±) (i:β„•) : option Ξ± := if i_lt : i < b.size then option.some (b.read ⟨i, i_lt⟩) else option.none /- Simplify some (read ...) -/ theorem some_read_is_try_read {Ξ±} (x:buffer Ξ±) (i:fin x.size) : option.some (read x i) = try_read x i.val := begin simp [try_read, i.is_lt], end /-- Lemma to force simplification of size, but only when buffer uses explicit constructor. -/ theorem size_ctor {Ξ±} (m:β„•) (a:array m Ξ±) : size (⟨m,a⟩) = m := by trivial /- Lemma that uses the index to a slice to show it is bounded by size of buffer. -/ theorem slice_index_bound {Ξ±} {b:buffer Ξ±} {s e :β„•} {pr : s ≀ e} (i:fin (size (slice b s e pr))) : s + i.val < b.size := begin cases b with m a, cases i with i i_lt, simp [size_slice, nat.lt_sub_right_iff_add_lt] at i_lt, exact calc s + i < min e m : i_lt ... ≀ m : min_le_right _ _, end /- Simplify reading from slice -/ theorem read_slice {Ξ±} {b:buffer Ξ±} {s e :β„•} {s_le_e : s ≀ e} (i:fin (size (slice b s e s_le_e))) : read (slice b s e s_le_e) i = read b ⟨s + i.val, slice_index_bound i⟩ := begin cases b with m a, cases i with i i_lt, apply option.some.inj, simp [some_read_is_try_read], cases (decide (e ≀ m)), case decidable.is_true : e_le_m { simp [size_slice, size_ctor, min_eq_left e_le_m] at i_lt, -- Reduce to array slice simp [slice, e_le_m, buffer.read], -- Simplify away try_read have s_i_lt_m : s+i < m := calc s + i < e : nat.add_lt_of_lt_sub_left i_lt ... ≀ m : e_le_m, simp [try_read, size_ctor, i_lt, s_i_lt_m, buffer.read], -- Simplify array theorem simp [array.read_slice], apply congr_arg _ (fin.eq_of_veq (eq.refl _)), }, case decidable.is_false : not_e_le_m { have m_le_e : m ≀ e := le_of_not_ge not_e_le_m, simp [size_slice, size_ctor, min_eq_right m_le_e] at i_lt, simp [slice, not_e_le_m, buffer.read], cases (decide (s ≀ m)), case decidable.is_true : s_le_m { -- Reduce to array slice simp [s_le_m], -- Simplify away try_read have s_i_lt_m : s+i < m := nat.add_lt_of_lt_sub_left i_lt, simp [try_read, size_ctor, i_lt, s_i_lt_m, buffer.read], -- Simplify array theorem simp [array.read_slice], apply congr_arg _ (fin.eq_of_veq (eq.refl _)), }, case decidable.is_false : not_s_le_m { -- Show i must be less than zero. have m_le_s : m ≀ s := le_of_not_ge not_s_le_m, have m_sub_s_eq_0 := nat.sub_eq_zero_of_le m_le_s, simp [m_sub_s_eq_0] at i_lt, -- Use i_lt : i < 0 to discharge proof exact (false.elim (nat.not_lt_zero _ i_lt)), }, }, end end buffer namespace string open string /- Simplify functions calling through as_string -/ theorem to_char_buffer_as_string (l:list char) : to_char_buffer (list.as_string l) = list.to_buffer l := eq.refl l.to_buffer theorem to_char_buffer_buffer_to_string (b:char_buffer) : to_char_buffer (buffer.to_string b) = b := begin apply buffer.ext, simp [buffer.to_string], simp [to_char_buffer_as_string], simp [buffer.to_list_to_buffer], simp [buffer.to_list], end end string end galois
2c5c64d90d489a4e04b35f1e5345fcd56dd1d28f
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/analysis/complex/basic.lean
41c059ef38ddc574ee6d4c5a71d745a487ec76a7
[ "Apache-2.0" ]
permissive
lacker/mathlib
f2439c743c4f8eb413ec589430c82d0f73b2d539
ddf7563ac69d42cfa4a1bfe41db1fed521bd795f
refs/heads/master
1,671,948,326,773
1,601,479,268,000
1,601,479,268,000
298,686,743
0
0
Apache-2.0
1,601,070,794,000
1,601,070,794,000
null
UTF-8
Lean
false
false
7,781
lean
/- Copyright (c) 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.calculus.deriv import analysis.normed_space.finite_dimension /-! # Normed space structure on `β„‚`. This file gathers basic facts on complex numbers of an analytic nature. ## Main results This file registers `β„‚` as a normed field, expresses basic properties of the norm, and gives tools on the real vector space structure of `β„‚`. Notably, in the namespace `complex`, it defines functions: * `continuous_linear_map.re` * `continuous_linear_map.im` * `continuous_linear_map.of_real` They are bundled versions of the real part, the imaginary part, and the embedding of `ℝ` in `β„‚`, as continuous `ℝ`-linear maps. `has_deriv_at_real_of_complex` expresses that, if a function on `β„‚` is differentiable (over `β„‚`), then its restriction to `ℝ` is differentiable over `ℝ`, with derivative the real part of the complex derivative. -/ noncomputable theory namespace complex instance : normed_field β„‚ := { norm := abs, dist_eq := Ξ» _ _, rfl, norm_mul' := abs_mul, .. complex.field } instance : nondiscrete_normed_field β„‚ := { non_trivial := ⟨2, by simp [norm]; norm_num⟩ } instance normed_algebra_over_reals : normed_algebra ℝ β„‚ := { norm_algebra_map_eq := abs_of_real, ..complex.algebra_over_reals } @[simp] lemma norm_eq_abs (z : β„‚) : βˆ₯zβˆ₯ = abs z := rfl @[simp] lemma norm_real (r : ℝ) : βˆ₯(r : β„‚)βˆ₯ = βˆ₯rβˆ₯ := abs_of_real _ @[simp] lemma norm_rat (r : β„š) : βˆ₯(r : β„‚)βˆ₯ = _root_.abs (r : ℝ) := suffices βˆ₯((r : ℝ) : β„‚)βˆ₯ = _root_.abs r, by simpa, by rw [norm_real, real.norm_eq_abs] @[simp] lemma norm_nat (n : β„•) : βˆ₯(n : β„‚)βˆ₯ = n := abs_of_nat _ @[simp] lemma norm_int {n : β„€} : βˆ₯(n : β„‚)βˆ₯ = _root_.abs n := suffices βˆ₯((n : ℝ) : β„‚)βˆ₯ = _root_.abs n, by simpa, by rw [norm_real, real.norm_eq_abs] lemma norm_int_of_nonneg {n : β„€} (hn : 0 ≀ n) : βˆ₯(n : β„‚)βˆ₯ = n := by rw [norm_int, _root_.abs_of_nonneg]; exact int.cast_nonneg.2 hn /-- Over the complex numbers, any finite-dimensional spaces is proper (and therefore complete). We can register this as an instance, as it will not cause problems in instance resolution since the properness of `β„‚` is already known and there is no metavariable. -/ instance finite_dimensional.proper (E : Type) [normed_group E] [normed_space β„‚ E] [finite_dimensional β„‚ E] : proper_space E := finite_dimensional.proper β„‚ E attribute [instance, priority 900] complex.finite_dimensional.proper /-- A complex normed vector space is also a real normed vector space. -/ @[priority 900] instance normed_space.restrict_scalars_real (E : Type*) [normed_group E] [normed_space β„‚ E] : normed_space ℝ E := normed_space.restrict_scalars' ℝ β„‚ E /-- The space of continuous linear maps over `ℝ`, from a real vector space to a complex vector space, is a normed vector space over `β„‚`. -/ instance continuous_linear_map.real_smul_complex (E : Type*) [normed_group E] [normed_space ℝ E] (F : Type*) [normed_group F] [normed_space β„‚ F] : normed_space β„‚ (E β†’L[ℝ] F) := continuous_linear_map.normed_space_extend_scalars /-- Continuous linear map version of the real part function, from `β„‚` to `ℝ`. -/ def continuous_linear_map.re : β„‚ β†’L[ℝ] ℝ := linear_map.re.mk_continuous 1 $ Ξ»x, begin change _root_.abs (x.re) ≀ 1 * abs x, rw one_mul, exact abs_re_le_abs x end @[simp] lemma continuous_linear_map.re_coe : (coe (continuous_linear_map.re) : β„‚ β†’β‚—[ℝ] ℝ) = linear_map.re := rfl @[simp] lemma continuous_linear_map.re_apply (z : β„‚) : (continuous_linear_map.re : β„‚ β†’ ℝ) z = z.re := rfl @[simp] lemma continuous_linear_map.re_norm : βˆ₯continuous_linear_map.reβˆ₯ = 1 := begin apply le_antisymm (linear_map.mk_continuous_norm_le _ zero_le_one _), calc 1 = βˆ₯continuous_linear_map.re (1 : β„‚)βˆ₯ : by simp ... ≀ βˆ₯continuous_linear_map.reβˆ₯ : by { apply continuous_linear_map.unit_le_op_norm, simp } end /-- Continuous linear map version of the real part function, from `β„‚` to `ℝ`. -/ def continuous_linear_map.im : β„‚ β†’L[ℝ] ℝ := linear_map.im.mk_continuous 1 $ Ξ»x, begin change _root_.abs (x.im) ≀ 1 * abs x, rw one_mul, exact complex.abs_im_le_abs x end @[simp] lemma continuous_linear_map.im_coe : (coe (continuous_linear_map.im) : β„‚ β†’β‚—[ℝ] ℝ) = linear_map.im := rfl @[simp] lemma continuous_linear_map.im_apply (z : β„‚) : (continuous_linear_map.im : β„‚ β†’ ℝ) z = z.im := rfl @[simp] lemma continuous_linear_map.im_norm : βˆ₯continuous_linear_map.imβˆ₯ = 1 := begin apply le_antisymm (linear_map.mk_continuous_norm_le _ zero_le_one _), calc 1 = βˆ₯continuous_linear_map.im (I : β„‚)βˆ₯ : by simp ... ≀ βˆ₯continuous_linear_map.imβˆ₯ : by { apply continuous_linear_map.unit_le_op_norm, rw ← abs_I, exact le_refl _ } end /-- Continuous linear map version of the canonical embedding of `ℝ` in `β„‚`. -/ def continuous_linear_map.of_real : ℝ β†’L[ℝ] β„‚ := linear_map.of_real.mk_continuous 1 $ Ξ»x, by simp @[simp] lemma continuous_linear_map.of_real_coe : (coe (continuous_linear_map.of_real) : ℝ β†’β‚—[ℝ] β„‚) = linear_map.of_real := rfl @[simp] lemma continuous_linear_map.of_real_apply (x : ℝ) : (continuous_linear_map.of_real : ℝ β†’ β„‚) x = x := rfl @[simp] lemma continuous_linear_map.of_real_norm : βˆ₯continuous_linear_map.of_realβˆ₯ = 1 := begin apply le_antisymm (linear_map.mk_continuous_norm_le _ zero_le_one _), calc 1 = βˆ₯continuous_linear_map.of_real (1 : ℝ)βˆ₯ : by simp ... ≀ βˆ₯continuous_linear_map.of_realβˆ₯ : by { apply continuous_linear_map.unit_le_op_norm, simp } end lemma continuous_linear_map.of_real_isometry : isometry continuous_linear_map.of_real := continuous_linear_map.isometry_iff_norm_image_eq_norm.2 (Ξ»x, by simp) end complex section real_deriv_of_complex /-! ### Differentiability of the restriction to `ℝ` of complex functions -/ open complex variables {e : β„‚ β†’ β„‚} {e' : β„‚} {z : ℝ} /-- A preliminary lemma for `has_deriv_at_real_of_complex`, which we only separate out to keep the maximum compile time per declaration low. -/ lemma has_deriv_at_real_of_complex_aux (h : has_deriv_at e e' z) : has_deriv_at (⇑continuous_linear_map.re ∘ Ξ» {z : ℝ}, e (continuous_linear_map.of_real z)) (((continuous_linear_map.re.comp ((continuous_linear_map.smul_right (1 : β„‚ β†’L[β„‚] β„‚) e').restrict_scalars ℝ)).comp continuous_linear_map.of_real) (1 : ℝ)) z := begin have A : has_fderiv_at continuous_linear_map.of_real continuous_linear_map.of_real z := continuous_linear_map.of_real.has_fderiv_at, have B : has_fderiv_at e ((continuous_linear_map.smul_right 1 e' : β„‚ β†’L[β„‚] β„‚).restrict_scalars ℝ) (continuous_linear_map.of_real z) := (has_deriv_at_iff_has_fderiv_at.1 h).restrict_scalars ℝ, have C : has_fderiv_at continuous_linear_map.re continuous_linear_map.re (e (continuous_linear_map.of_real z)) := continuous_linear_map.re.has_fderiv_at, exact has_fderiv_at_iff_has_deriv_at.1 (C.comp z (B.comp z A)), end /-- If a complex function is differentiable at a real point, then the induced real function is also differentiable at this point, with a derivative equal to the real part of the complex derivative. -/ theorem has_deriv_at_real_of_complex (h : has_deriv_at e e' z) : has_deriv_at (Ξ»x:ℝ, (e x).re) e'.re z := begin rw (show (Ξ»x:ℝ, (e x).re) = (continuous_linear_map.re : β„‚ β†’ ℝ) ∘ e ∘ (continuous_linear_map.of_real : ℝ β†’ β„‚), by { ext x, refl }), simpa using has_deriv_at_real_of_complex_aux h, end end real_deriv_of_complex
3090a57e7233041737554d8214ed3ae83a87d56a
7a76361040c55ae1eba5856c1a637593117a6556
/src/lectures/love00_preface_demo.lean
ad912da09bee71e3c729623cdea20ff2335c1850
[]
no_license
rgreenblatt/fpv2021
c2cbe7b664b648cef7d240a654d6bdf97a559272
c65d72e48c8fa827d2040ed6ea86c2be62db36fa
refs/heads/main
1,692,245,693,819
1,633,364,621,000
1,633,364,621,000
407,231,487
0
0
null
1,631,808,608,000
1,631,808,608,000
null
UTF-8
Lean
false
false
3,273
lean
import data.nat.prime import tactic.linarith /-! # LoVe Preface ## Proof Assistants Proof assistants (also called interactive theorem provers) * check and help develop formal proofs; * can be used to prove big theorems, not only logic puzzles; * can be tedious to use; * are highly addictive (think video games). A selection of proof assistants, classified by logical foundations: * set theory: Isabelle/ZF, Metamath, Mizar; * simple type theory: HOL4, HOL Light, Isabelle/HOL; * **dependent type theory**: Agda, Coq, **Lean**, Matita, PVS. ## Success Stories Mathematics: * the four-color theorem (in Coq); * the odd-order theorem (in Coq); * the Kepler conjecture (in HOL Light and Isabelle/HOL). Computer science: * hardware * operating systems * programming language theory * compilers * security ## Lean Lean is a proof assistant developed primarily by Leonardo de Moura (Microsoft Research) since 2012. Its mathematical library, `mathlib`, is developed by a user community. We use community version 3.20.0. We use its basic libraries, `mathlib`, and `LoVelib`. Lean is a research project. Strengths: * highly expressive logic based on a dependent type theory called the **calculus of inductive constructions**; * extended with classical axioms and quotient types; * metaprogramming framework; * modern user interface; * documentation; * open source; * wonderful user community. ## This Course ### Web Site https://cs.brown.edu/courses/cs1951x/ ### Repository (Demos, Exercises, Homework) https://github.com/BrownCS1951x/fpv2021 The file you are currently looking at is a demo. For each chapter of the Hitchhiker's Guide, there will be approximately one demo, one exercise sheet, and one homework. * Demos will be covered in class. These are "lecture notes." We'll post skeletons of the demos before class, and completed demos after class. * Exercises are ungraded practice problems for you to use to learn. Sometimes we'll cover exercise problems in class. Occasionally we may run class like a lab, giving you time to work on exercise problems with us around. * Homeworks are for you to do on your own, and submit via Gradescope. ### The Hitchhiker's Guide to Logical Verification https://cs.brown.edu/courses/cs1951x/static_files/main.pdf The lecture notes consist of a preface and 13 chapters. They cover the same material as the corresponding lectures but with more details. Sometimes there will not be enough time to cover everything in class, so reading the lecture notes will be necessary. Download this version, not others that you might find online! ## Our Goal We want you to * master fundamental theory and techniques in interactive theorem proving; * familiarize yourselves with some application areas; * develop some practical skills you can apply on a larger project (as a hobby, for an MSc or PhD, or in industry); * feel ready to move to another proof assistant and apply what you have learned; * understand the domain well enough to start reading scientific papers. This course is neither a pure logical foundations course nor a Lean tutorial. Lean is our vehicle, not an end in itself. -/ open nat open_locale nat theorem infinitude_of_primes : βˆ€ N, βˆƒ p β‰₯ N, prime p := sorry
6972065e8678d485cf8a01cb9d2113d9e7006215
c3f2fcd060adfa2ca29f924839d2d925e8f2c685
/library/data/sigma.lean
2f1b811266c6d789aa5ab5dd0eba84ebcb758001
[ "Apache-2.0" ]
permissive
respu/lean
6582d19a2f2838a28ecd2b3c6f81c32d07b5341d
8c76419c60b63d0d9f7bc04ebb0b99812d0ec654
refs/heads/master
1,610,882,451,231
1,427,747,084,000
1,427,747,429,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,481
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Module: data.sigma Author: Leonardo de Moura, Jeremy Avigad, Floris van Doorn Sigma types, aka dependent sum. -/ import logic.cast open inhabited eq.ops sigma.ops namespace sigma universe variables u v variables {A A' : Type.{u}} {B : A β†’ Type.{v}} {B' : A' β†’ Type.{v}} definition unpack {C : (Ξ£a, B a) β†’ Type} {u : Ξ£a, B a} (H : C ⟨u.1 , u.2⟩) : C u := destruct u (Ξ»x y H, H) H theorem dpair_eq {a₁ aβ‚‚ : A} {b₁ : B a₁} {bβ‚‚ : B aβ‚‚} (H₁ : a₁ = aβ‚‚) (Hβ‚‚ : eq.rec_on H₁ b₁ = bβ‚‚) : ⟨a₁, bβ‚βŸ© = ⟨aβ‚‚, bβ‚‚βŸ© := dcongr_arg2 mk H₁ Hβ‚‚ theorem dpair_heq {a : A} {a' : A'} {b : B a} {b' : B' a'} (HB : B == B') (Ha : a == a') (Hb : b == b') : ⟨a, b⟩ == ⟨a', b'⟩ := hcongr_arg4 @mk (heq.type_eq Ha) HB Ha Hb protected theorem equal {p₁ pβ‚‚ : Ξ£a : A, B a} : βˆ€(H₁ : p₁.1 = pβ‚‚.1) (Hβ‚‚ : eq.rec_on H₁ p₁.2 = pβ‚‚.2), p₁ = pβ‚‚ := destruct p₁ (take a₁ b₁, destruct pβ‚‚ (take aβ‚‚ bβ‚‚ H₁ Hβ‚‚, dpair_eq H₁ Hβ‚‚)) protected theorem hequal {p : Ξ£a : A, B a} {p' : Ξ£a' : A', B' a'} (HB : B == B') : βˆ€(H₁ : p.1 == p'.1) (Hβ‚‚ : p.2 == p'.2), p == p' := destruct p (take a₁ b₁, destruct p' (take aβ‚‚ bβ‚‚ H₁ Hβ‚‚, dpair_heq HB H₁ Hβ‚‚)) protected definition is_inhabited [instance] [H₁ : inhabited A] [Hβ‚‚ : inhabited (B (default A))] : inhabited (sigma B) := inhabited.destruct H₁ (Ξ»a, inhabited.destruct Hβ‚‚ (Ξ»b, inhabited.mk ⟨default A, b⟩)) theorem eq_rec_dpair_commute {C : Ξ a, B a β†’ Type} {a a' : A} (H : a = a') (b : B a) (c : C a b) : eq.rec_on H ⟨b, c⟩ = ⟨eq.rec_on H b, eq.rec_on (dcongr_arg2 C H rfl) c⟩ := eq.drec_on H (dpair_eq rfl (!eq.rec_on_id⁻¹)) variables {C : Ξ a, B a β†’ Type} {D : Ξ a b, C a b β†’ Type} definition dtrip (a : A) (b : B a) (c : C a b) := ⟨a, b, c⟩ definition dquad (a : A) (b : B a) (c : C a b) (d : D a b c) := ⟨a, b, c, d⟩ definition pr1' [reducible] (x : Ξ£ a, B a) := x.1 definition pr2' [reducible] (x : Ξ£ a b, C a b) := x.2.1 definition pr3 [reducible] (x : Ξ£ a b, C a b) := x.2.2 definition pr3' [reducible] (x : Ξ£ a b c, D a b c) := x.2.2.1 definition pr4 [reducible] (x : Ξ£ a b c, D a b c) := x.2.2.2 theorem dtrip_eq {a₁ aβ‚‚ : A} {b₁ : B a₁} {bβ‚‚ : B aβ‚‚} {c₁ : C a₁ b₁} {cβ‚‚ : C aβ‚‚ bβ‚‚} (H₁ : a₁ = aβ‚‚) (Hβ‚‚ : eq.rec_on H₁ b₁ = bβ‚‚) (H₃ : cast (dcongr_arg2 C H₁ Hβ‚‚) c₁ = cβ‚‚) : ⟨a₁, b₁, cβ‚βŸ© = ⟨aβ‚‚, bβ‚‚, cβ‚‚βŸ© := dcongr_arg3 dtrip H₁ Hβ‚‚ H₃ theorem ndtrip_eq {A B : Type} {C : A β†’ B β†’ Type} {a₁ aβ‚‚ : A} {b₁ bβ‚‚ : B} {c₁ : C a₁ b₁} {cβ‚‚ : C aβ‚‚ bβ‚‚} (H₁ : a₁ = aβ‚‚) (Hβ‚‚ : b₁ = bβ‚‚) (H₃ : cast (congr_arg2 C H₁ Hβ‚‚) c₁ = cβ‚‚) : ⟨a₁, b₁, cβ‚βŸ© = ⟨aβ‚‚, bβ‚‚, cβ‚‚βŸ© := hdcongr_arg3 dtrip H₁ (heq.of_eq Hβ‚‚) H₃ theorem ndtrip_equal {A B : Type} {C : A β†’ B β†’ Type} {p₁ pβ‚‚ : Ξ£a b, C a b} : βˆ€(H₁ : pr1 p₁ = pr1 pβ‚‚) (Hβ‚‚ : pr2' p₁ = pr2' pβ‚‚) (H₃ : eq.rec_on (congr_arg2 C H₁ Hβ‚‚) (pr3 p₁) = pr3 pβ‚‚), p₁ = pβ‚‚ := destruct p₁ (take a₁ q₁, destruct q₁ (take b₁ c₁, destruct pβ‚‚ (take aβ‚‚ qβ‚‚, destruct qβ‚‚ (take bβ‚‚ cβ‚‚ H₁ Hβ‚‚ H₃, ndtrip_eq H₁ Hβ‚‚ H₃)))) end sigma
52f233acc29075cace1beca7eeda1b73ca2b1eb7
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/io_fs_2.lean
4a0cdc9be3612e34e77184788c07219c11008b8d
[ "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
2,115
lean
import system.io open io io.fs def TEST_DIR : string := "_test_dir" def TEST_DIR1 : string := "_test_dir1" def TEST_DIR2 : string := "_test_dir2" def TEST_FILE_1 : string := "_test_file1" def TEST_FILE_2 : string := "_test_file2" meta def ycheck (s : string) (t : io bool) : io unit := do r ← t, if r then return () else io.fail sformat!"ycheck: {s}" meta def ncheck (s : string) (t : io bool) : io unit := do r ← t, if Β¬r then return () else io.fail sformat!"ncheck: {s}" meta def go_dir : io unit := do rmdir TEST_DIR, ncheck "dir exists now" $ dir_exists TEST_DIR, ycheck "mkdir" $ mkdir TEST_DIR, ycheck "dir exists now" $ dir_exists TEST_DIR, ncheck "file exists now" $ file_exists TEST_DIR, ycheck "rmdir" $ rmdir TEST_DIR, ncheck "dir exists now'" $ dir_exists TEST_DIR, return () meta def go_dir_recursive : io unit := do let combined := TEST_DIR1 ++ "/" ++ TEST_DIR2, rmdir $ combined, rmdir TEST_DIR1, ycheck "mkdir rec" $ mkdir combined tt, ycheck "dir exists now1" $ dir_exists TEST_DIR1, ycheck "dir exists now2" $ dir_exists combined, ycheck "rmdir2" $ rmdir $ combined, ycheck "dir exists now1'" $ dir_exists TEST_DIR1, ncheck "dir exists now2'" $ dir_exists combined, ycheck "rmdir" $ rmdir $ TEST_DIR1, ncheck "dir exists now1''" $ dir_exists TEST_DIR1, ncheck "dir exists now2''" $ dir_exists combined, return () meta def go_file : io unit := do remove TEST_FILE_1 <|> return (), remove TEST_FILE_2 <|> return (), ncheck "file1 exists now" $ file_exists TEST_FILE_1, mk_file_handle TEST_FILE_1 io.mode.write >>= close, ycheck "file1 exists now'" $ file_exists TEST_FILE_1, ncheck "dir exists now" $ dir_exists TEST_FILE_1, rename TEST_FILE_1 TEST_FILE_2, ncheck "file1 exists now''" $ file_exists TEST_FILE_1, ycheck "file2 exists now" $ file_exists TEST_FILE_2, remove TEST_FILE_2, ncheck "file2 exists now" $ file_exists TEST_FILE_2, return () run_cmd (tactic.unsafe_run_io go_dir) run_cmd (tactic.unsafe_run_io go_dir_recursive) run_cmd (tactic.unsafe_run_io go_file)
05287f9b3b3f8e1eeef84a6f1f6bd707cfbb3653
ae1e94c332e17c7dc7051ce976d5a9eebe7ab8a5
/tests/lean/run/anonymousCtor.lean
d9f8427c86776b243e50157e8db73858efcefb5b
[ "Apache-2.0" ]
permissive
dupuisf/lean4
d082d13b01243e1de29ae680eefb476961221eef
6a39c65bd28eb0e28c3870188f348c8914502718
refs/heads/master
1,676,948,755,391
1,610,665,114,000
1,610,665,114,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
95
lean
#lang lean4 inductive S | mk : List S β†’ String β†’ S def f (s : String) : S := ⟨[], s⟩
59caef5f9f1ba5771b289fa6587ea03560b490ab
efd582de089592b7c646b48c90e85d8db560e581
/src/matrix_cookbook/2_derivatives.lean
50c71b5eeff218318ddc074df9279cb8faa3141a
[ "MIT" ]
permissive
Daniel-Packer/lean-matrix-cookbook
3e44ce059a16e5334d6c95f8e9b87acbdeb838e9
d7d5079e2513c2800f76915f1f30953c9676673c
refs/heads/master
1,693,182,725,966
1,636,125,196,000
1,636,125,196,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
6,418
lean
import linear_algebra.matrix.nonsingular_inverse import linear_algebra.matrix.trace import analysis.calculus.deriv import data.matrix.kronecker import data.matrix.hadamard import analysis.special_functions.exp_log /-! # Derivatives -/ variables {ΞΉ : Type*} {R : Type*} {m n p : Type*} variables [fintype m] [fintype n] [fintype p] variables [decidable_eq m] [decidable_eq n] [decidable_eq p] namespace matrix_cookbook variables [nondiscrete_normed_field R] attribute [instance] matrix.normed_group matrix.normed_space local notation `Tr` := matrix.trace _ R _ local notation `Tr'` := matrix.trace _ ℝ _ open_locale matrix kronecker open matrix /-! TODO: is this what is actually meant by `βˆ‚(XY) = (βˆ‚X)Y + X(βˆ‚Y)`? -/ lemma eq_33 (A : matrix m n R) : deriv (Ξ» x : R, A) = 0 := deriv_const' _ lemma eq_34 (c : R) (X : R β†’ matrix m n R) (hX : differentiable R X) : deriv (c β€’ X) = c β€’ deriv X := funext $ Ξ» _, deriv_const_smul c (hX _) lemma eq_35 (X Y : R β†’ matrix m n R) (hX : differentiable R X) (hY : differentiable R Y) : deriv (X + Y) = deriv X + deriv Y := funext $ Ξ» _, deriv_add (hX _) (hY _) lemma eq_36 (X : R β†’ matrix m m R) (hX : differentiable R X) : deriv (Ξ» a, Tr (X a)) = Ξ» a, Tr (deriv X a) := sorry lemma eq_37 (X : R β†’ matrix m n R) (Y : R β†’ matrix n p R) (hX : differentiable R X) (hY : differentiable R Y) : deriv (Ξ» a, (X a) ⬝ (Y a)) = Ξ» a, deriv X a ⬝ Y a + X a ⬝ deriv Y a := sorry lemma eq_38 (X Y : R β†’ matrix n p R) (hX : differentiable R X) (hY : differentiable R Y) : deriv (Ξ» a, (X a) βŠ™ (Y a)) = Ξ» a, deriv X a βŠ™ Y a + X a βŠ™ deriv Y a := sorry lemma eq_39 (X Y : R β†’ matrix n p R) (hX : differentiable R X) (hY : differentiable R Y) : deriv (Ξ» a, (X a) βŠ—β‚– (Y a)) = Ξ» a, deriv X a βŠ—β‚– Y a + X a βŠ—β‚– deriv Y a := sorry lemma eq_40 (X : R β†’ matrix n n R) (hX : differentiable R X) : deriv (Ξ» a, (X a)⁻¹) = Ξ» a, -(X a)⁻¹ * deriv X a * (X a)⁻¹ := sorry lemma eq_41 (X : R β†’ matrix n n R) (hX : differentiable R X) : deriv (Ξ» a, det (X a)) = Ξ» a, Tr (adjugate (X a) * deriv X a) := sorry lemma eq_42 (X : R β†’ matrix n n R) (hX : differentiable R X) : deriv (Ξ» a, det (X a)) = Ξ» a, det (X a) β€’ Tr ((X a)⁻¹ * deriv X a) := sorry lemma eq_43 (X : ℝ β†’ matrix n n ℝ) (hX : differentiable ℝ X) : deriv (Ξ» a, real.log (det (X a))) = Ξ» a, Tr' ((X a)⁻¹ * deriv X a) := sorry lemma eq_44 (X : R β†’ matrix m n R) (hX : differentiable R X) : deriv (Ξ» a, (X a)α΅€) = Ξ» a, (deriv X a)α΅€ := sorry lemma eq_45 [star_ring R] (X : R β†’ matrix m n R) (hX : differentiable R X) : deriv (Ξ» a, (X a)α΄΄) = Ξ» a, (deriv X a)α΄΄ := sorry /-! ## Derivatives of a Determinant -/ /-! ### General form -/ lemma eq_46 (X : R β†’ matrix n n R) (hX : differentiable R X) : deriv (Ξ» a, det (X a)) = Ξ» a, det (X a) β€’ Tr ((X a)⁻¹ * deriv X a) := eq_42 X hX -- this suggests we might have 42 stated strangely -- lemma eq_47 : sorry := sorry -- lemma eq_48 : sorry := sorry /-! ### Linear forms -/ -- lemma eq_49 : sorry := sorry -- lemma eq_50 : sorry := sorry -- lemma eq_51 : sorry := sorry /-! ### Square forms -/ -- lemma eq_52 : sorry := sorry -- lemma eq_53 : sorry := sorry -- lemma eq_54 : sorry := sorry /-! ### Other nonlinear forms -/ -- lemma eq_55 : sorry := sorry -- lemma eq_56 : sorry := sorry -- lemma eq_57 : sorry := sorry -- lemma eq_58 : sorry := sorry /-! ## Derivatives of an Inverse -/ -- lemma eq_59 : sorry := sorry -- lemma eq_60 : sorry := sorry -- lemma eq_61 : sorry := sorry -- lemma eq_62 : sorry := sorry -- lemma eq_63 : sorry := sorry -- lemma eq_64 : sorry := sorry /-! ## Derivatives of Eigenvalues -/ -- lemma eq_65 : sorry := sorry -- lemma eq_66 : sorry := sorry -- lemma eq_67 : sorry := sorry -- lemma eq_68 : sorry := sorry /-! ## Derivatives of Matrices, Vectors, and Scalar forms -/ /-! ### First order -/ -- lemma eq_69 : sorry := sorry -- lemma eq_70 : sorry := sorry -- lemma eq_71 : sorry := sorry -- lemma eq_72 : sorry := sorry -- lemma eq_73 : sorry := sorry -- lemma eq_74 : sorry := sorry -- lemma eq_75 : sorry := sorry /-! ### Second order -/ -- lemma eq_76 : sorry := sorry -- lemma eq_77 : sorry := sorry -- lemma eq_78 : sorry := sorry -- lemma eq_79 : sorry := sorry -- lemma eq_80 : sorry := sorry -- lemma eq_81 : sorry := sorry -- lemma eq_82 : sorry := sorry -- lemma eq_83 : sorry := sorry -- lemma eq_84 : sorry := sorry -- lemma eq_85 : sorry := sorry -- lemma eq_86 : sorry := sorry -- lemma eq_87 : sorry := sorry -- lemma eq_88 : sorry := sorry -- lemma eq_89 : sorry := sorry /-! ### Higher order and non-linear -/ -- lemma eq_90 : sorry := sorry -- lemma eq_91 : sorry := sorry -- lemma eq_92 : sorry := sorry -- lemma eq_93 : sorry := sorry -- lemma eq_94 : sorry := sorry -- lemma eq_95 : sorry := sorry /-! ### Gradient and hessian -/ -- lemma eq_96 : sorry := sorry -- lemma eq_97 : sorry := sorry -- lemma eq_98 : sorry := sorry /-! ## Derivatives of Traces -/ /-! ### First order -/ -- lemma eq_99 : sorry := sorry -- lemma eq_100 : sorry := sorry -- lemma eq_101 : sorry := sorry -- lemma eq_102 : sorry := sorry -- lemma eq_103 : sorry := sorry -- lemma eq_104 : sorry := sorry -- lemma eq_105 : sorry := sorry /-! ### Second order -/ -- eqs 106-120 /-! ### Higher order -/ -- lemma eq_121 : sorry := sorry -- lemma eq_122 : sorry := sorry -- lemma eq_123 : sorry := sorry /-! ### Other -/ -- lemma eq_124 : sorry := sorry -- lemma eq_125 : sorry := sorry -- lemma eq_126 : sorry := sorry -- lemma eq_127 : sorry := sorry -- lemma eq_128 : sorry := sorry /-! ## Derivatives of Vector norms -/ /-! ### Two-norm -/ -- lemma eq_129 : sorry := sorry -- lemma eq_130 : sorry := sorry -- lemma eq_131 : sorry := sorry /-! ## Derivatives of matrix norms -/ /-! ### Frobenius norm -/ -- lemma eq_132 : sorry := sorry /-! ## Derivatives of structured matrices -/ -- lemma eq_133 : sorry := sorry -- lemma eq_134 : sorry := sorry /-! ### The Chain Rule -/ -- lemma eq_135 : sorry := sorry -- lemma eq_136 : sorry := sorry -- lemma eq_147 : sorry := sorry /-! ### Symmetric -/ -- lemma eq_138 : sorry := sorry -- lemma eq_139 : sorry := sorry -- lemma eq_140 : sorry := sorry -- lemma eq_141 : sorry := sorry /-! ### Diagonal -/ -- lemma eq_142 : sorry := sorry /-! ### Toeplitz -/ -- lemma eq_143 : sorry := sorry -- lemma eq_144 : sorry := sorry end matrix_cookbook
93af760e01e59fbcbe8228f339c5ccd54cb084a5
94e33a31faa76775069b071adea97e86e218a8ee
/src/group_theory/specific_groups/cyclic.lean
3499df0fee3b921ff0698655436b649c25ab70d1
[ "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
22,218
lean
/- Copyright (c) 2018 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl -/ import algebra.big_operators.order import data.nat.totient import group_theory.order_of_element import tactic.group import group_theory.exponent /-! # Cyclic groups A group `G` is called cyclic if there exists an element `g : G` such that every element of `G` is of the form `g ^ n` for some `n : β„•`. This file only deals with the predicate on a group to be cyclic. For the concrete cyclic group of order `n`, see `data.zmod.basic`. ## Main definitions * `is_cyclic` is a predicate on a group stating that the group is cyclic. ## Main statements * `is_cyclic_of_prime_card` proves that a finite group of prime order is cyclic. * `is_simple_group_of_prime_card`, `is_simple_group.is_cyclic`, and `is_simple_group.prime_card` classify finite simple abelian groups. * `is_cyclic.exponent_eq_card`: For a finite cyclic group `G`, the exponent is equal to the group's cardinality. * `is_cyclic.exponent_eq_zero_of_infinite`: Infinite cyclic groups have exponent zero. * `is_cyclic.iff_exponent_eq_card`: A finite commutative group is cyclic iff its exponent is equal to its cardinality. ## Tags cyclic group -/ universe u variables {Ξ± : Type u} {a : Ξ±} section cyclic open_locale big_operators local attribute [instance] set_fintype open subgroup /-- A group is called *cyclic* if it is generated by a single element. -/ class is_add_cyclic (Ξ± : Type u) [add_group Ξ±] : Prop := (exists_generator [] : βˆƒ g : Ξ±, βˆ€ x, x ∈ add_subgroup.zmultiples g) /-- A group is called *cyclic* if it is generated by a single element. -/ @[to_additive is_add_cyclic] class is_cyclic (Ξ± : Type u) [group Ξ±] : Prop := (exists_generator [] : βˆƒ g : Ξ±, βˆ€ x, x ∈ zpowers g) @[priority 100, to_additive is_add_cyclic_of_subsingleton] instance is_cyclic_of_subsingleton [group Ξ±] [subsingleton Ξ±] : is_cyclic Ξ± := ⟨⟨1, Ξ» x, by { rw subsingleton.elim x 1, exact mem_zpowers 1 }⟩⟩ /-- A cyclic group is always commutative. This is not an `instance` because often we have a better proof of `comm_group`. -/ @[to_additive "A cyclic group is always commutative. This is not an `instance` because often we have a better proof of `add_comm_group`."] def is_cyclic.comm_group [hg : group Ξ±] [is_cyclic Ξ±] : comm_group Ξ± := { mul_comm := Ξ» x y, let ⟨g, hg⟩ := is_cyclic.exists_generator Ξ±, ⟨n, hn⟩ := hg x, ⟨m, hm⟩ := hg y in hm β–Έ hn β–Έ zpow_mul_comm _ _ _, ..hg } variables [group Ξ±] @[to_additive monoid_add_hom.map_add_cyclic] lemma monoid_hom.map_cyclic {G : Type*} [group G] [h : is_cyclic G] (Οƒ : G β†’* G) : βˆƒ m : β„€, βˆ€ g : G, Οƒ g = g ^ m := begin obtain ⟨h, hG⟩ := is_cyclic.exists_generator G, obtain ⟨m, hm⟩ := hG (Οƒ h), refine ⟨m, Ξ» g, _⟩, obtain ⟨n, rfl⟩ := hG g, rw [monoid_hom.map_zpow, ←hm, ←zpow_mul, ←zpow_mul'], end @[to_additive is_add_cyclic_of_order_of_eq_card] lemma is_cyclic_of_order_of_eq_card [fintype Ξ±] (x : Ξ±) (hx : order_of x = fintype.card Ξ±) : is_cyclic Ξ± := begin classical, use x, simp_rw [← set_like.mem_coe, ← set.eq_univ_iff_forall], rw [←fintype.card_congr (equiv.set.univ Ξ±), order_eq_card_zpowers] at hx, exact set.eq_of_subset_of_card_le (set.subset_univ _) (ge_of_eq hx), end /-- A finite group of prime order is cyclic. -/ @[to_additive is_add_cyclic_of_prime_card] lemma is_cyclic_of_prime_card {Ξ± : Type u} [group Ξ±] [fintype Ξ±] {p : β„•} [hp : fact p.prime] (h : fintype.card Ξ± = p) : is_cyclic Ξ± := ⟨begin obtain ⟨g, hg⟩ : βˆƒ g : Ξ±, g β‰  1 := fintype.exists_ne_of_one_lt_card (h.symm β–Έ hp.1.one_lt) 1, classical, -- for fintype (subgroup.zpowers g) have : fintype.card (subgroup.zpowers g) ∣ p, { rw ←h, apply card_subgroup_dvd_card }, rw nat.dvd_prime hp.1 at this, cases this, { rw fintype.card_eq_one_iff at this, cases this with t ht, suffices : g = 1, { contradiction }, have hgt := ht ⟨g, by { change g ∈ subgroup.zpowers g, exact subgroup.mem_zpowers g }⟩, rw [←ht 1] at hgt, change (⟨_, _⟩ : subgroup.zpowers g) = ⟨_, _⟩ at hgt, simpa using hgt }, { use g, intro x, rw [←h] at this, rw subgroup.eq_top_of_card_eq _ this, exact subgroup.mem_top _ } end⟩ @[to_additive add_order_of_eq_card_of_forall_mem_zmultiples] lemma order_of_eq_card_of_forall_mem_zpowers [fintype Ξ±] {g : Ξ±} (hx : βˆ€ x, x ∈ zpowers g) : order_of g = fintype.card Ξ± := begin classical, rw order_eq_card_zpowers, apply fintype.card_of_finset', simpa using hx end @[to_additive infinite.add_order_of_eq_zero_of_forall_mem_zmultiples] lemma infinite.order_of_eq_zero_of_forall_mem_zpowers [infinite Ξ±] {g : Ξ±} (h : βˆ€ x, x ∈ zpowers g) : order_of g = 0 := begin classical, rw order_of_eq_zero_iff', refine Ξ» n hn hgn, _, have ho := order_of_pos' ((is_of_fin_order_iff_pow_eq_one g).mpr ⟨n, hn, hgn⟩), obtain ⟨x, hx⟩ := infinite.exists_not_mem_finset (finset.image (pow g) $ finset.range $ order_of g), apply hx, rw [←mem_powers_iff_mem_range_order_of' g x ho, submonoid.mem_powers_iff], obtain ⟨k, hk⟩ := h x, obtain ⟨k, rfl | rfl⟩ := k.eq_coe_or_neg, { exact ⟨k, by exact_mod_cast hk⟩ }, let t : β„€ := -k % (order_of g), rw zpow_eq_mod_order_of at hk, have : 0 ≀ t := int.mod_nonneg (-k) (by exact_mod_cast ho.ne'), refine ⟨t.to_nat, _⟩, rwa [←zpow_coe_nat, int.to_nat_of_nonneg this] end @[to_additive bot.is_add_cyclic] instance bot.is_cyclic {Ξ± : Type u} [group Ξ±] : is_cyclic (βŠ₯ : subgroup Ξ±) := ⟨⟨1, Ξ» x, ⟨0, subtype.eq $ (zpow_zero (1 : Ξ±)).trans $ eq.symm (subgroup.mem_bot.1 x.2)⟩⟩⟩ @[to_additive add_subgroup.is_add_cyclic] instance subgroup.is_cyclic {Ξ± : Type u} [group Ξ±] [is_cyclic Ξ±] (H : subgroup Ξ±) : is_cyclic H := by haveI := classical.prop_decidable; exact let ⟨g, hg⟩ := is_cyclic.exists_generator Ξ± in if hx : βˆƒ (x : Ξ±), x ∈ H ∧ x β‰  (1 : Ξ±) then let ⟨x, hx₁, hxβ‚‚βŸ© := hx in let ⟨k, hk⟩ := hg x in have hex : βˆƒ n : β„•, 0 < n ∧ g ^ n ∈ H, from ⟨k.nat_abs, nat.pos_of_ne_zero (Ξ» h, hxβ‚‚ $ by rw [← hk, int.eq_zero_of_nat_abs_eq_zero h, zpow_zero]), match k, hk with | (k : β„•), hk := by rw [int.nat_abs_of_nat, ← zpow_coe_nat, hk]; exact hx₁ | -[1+ k], hk := by rw [int.nat_abs_of_neg_succ_of_nat, ← subgroup.inv_mem_iff H]; simp * at * end⟩, ⟨⟨⟨g ^ nat.find hex, (nat.find_spec hex).2⟩, Ξ» ⟨x, hx⟩, let ⟨k, hk⟩ := hg x in have hk₁ : g ^ ((nat.find hex : β„€) * (k / nat.find hex)) ∈ zpowers (g ^ nat.find hex), from ⟨k / nat.find hex, by rw [← zpow_coe_nat, zpow_mul]⟩, have hkβ‚‚ : g ^ ((nat.find hex : β„€) * (k / nat.find hex)) ∈ H, by { rw zpow_mul, apply H.zpow_mem, exact_mod_cast (nat.find_spec hex).2 }, have hk₃ : g ^ (k % nat.find hex) ∈ H, from (subgroup.mul_mem_cancel_right H hkβ‚‚).1 $ by rw [← zpow_add, int.mod_add_div, hk]; exact hx, have hkβ‚„ : k % nat.find hex = (k % nat.find hex).nat_abs, by rw int.nat_abs_of_nonneg (int.mod_nonneg _ (int.coe_nat_ne_zero_iff_pos.2 (nat.find_spec hex).1)), have hkβ‚… : g ^ (k % nat.find hex ).nat_abs ∈ H, by rwa [← zpow_coe_nat, ← hkβ‚„], have hk₆ : (k % (nat.find hex : β„€)).nat_abs = 0, from by_contradiction (Ξ» h, nat.find_min hex (int.coe_nat_lt.1 $ by rw [← hkβ‚„]; exact int.mod_lt_of_pos _ (int.coe_nat_pos.2 (nat.find_spec hex).1)) ⟨nat.pos_of_ne_zero h, hkβ‚…βŸ©), ⟨k / (nat.find hex : β„€), subtype.ext_iff_val.2 begin suffices : g ^ ((nat.find hex : β„€) * (k / nat.find hex)) = x, { simpa [zpow_mul] }, rw [int.mul_div_cancel' (int.dvd_of_mod_eq_zero (int.eq_zero_of_nat_abs_eq_zero hk₆)), hk] end⟩⟩⟩ else have H = (βŠ₯ : subgroup Ξ±), from subgroup.ext $ Ξ» x, ⟨λ h, by simp at *; tauto, Ξ» h, by rw [subgroup.mem_bot.1 h]; exact H.one_mem⟩, by clear _let_match; substI this; apply_instance open finset nat section classical open_locale classical @[to_additive is_add_cyclic.card_pow_eq_one_le] lemma is_cyclic.card_pow_eq_one_le [decidable_eq Ξ±] [fintype Ξ±] [is_cyclic Ξ±] {n : β„•} (hn0 : 0 < n) : (univ.filter (Ξ» a : Ξ±, a ^ n = 1)).card ≀ n := let ⟨g, hg⟩ := is_cyclic.exists_generator Ξ± in calc (univ.filter (Ξ» a : Ξ±, a ^ n = 1)).card ≀ ((zpowers (g ^ (fintype.card Ξ± / (nat.gcd n (fintype.card Ξ±))))) : set Ξ±).to_finset.card : card_le_of_subset (Ξ» x hx, let ⟨m, hm⟩ := show x ∈ submonoid.powers g, from mem_powers_iff_mem_zpowers.2 $ hg x in set.mem_to_finset.2 ⟨(m / (fintype.card Ξ± / (nat.gcd n (fintype.card Ξ±))) : β„•), have hgmn : g ^ (m * nat.gcd n (fintype.card Ξ±)) = 1, by rw [pow_mul, hm, ← pow_gcd_card_eq_one_iff]; exact (mem_filter.1 hx).2, begin rw [zpow_coe_nat, ← pow_mul, nat.mul_div_cancel_left', hm], refine dvd_of_mul_dvd_mul_right (gcd_pos_of_pos_left (fintype.card Ξ±) hn0) _, conv_lhs { rw [nat.div_mul_cancel (nat.gcd_dvd_right _ _), ←order_of_eq_card_of_forall_mem_zpowers hg] }, exact order_of_dvd_of_pow_eq_one hgmn end⟩) ... ≀ n : let ⟨m, hm⟩ := nat.gcd_dvd_right n (fintype.card Ξ±) in have hm0 : 0 < m, from nat.pos_of_ne_zero $ Ξ» hm0, by { rw [hm0, mul_zero, fintype.card_eq_zero_iff] at hm, exact hm.elim' 1 }, begin simp only [set.to_finset_card, set_like.coe_sort_coe], rw [←order_eq_card_zpowers, order_of_pow g, order_of_eq_card_of_forall_mem_zpowers hg], rw [hm] {occs := occurrences.pos [2,3]}, rw [nat.mul_div_cancel_left _ (gcd_pos_of_pos_left _ hn0), gcd_mul_left_left, hm, nat.mul_div_cancel _ hm0], exact le_of_dvd hn0 (nat.gcd_dvd_left _ _) end end classical @[to_additive] lemma is_cyclic.exists_monoid_generator [fintype Ξ±] [is_cyclic Ξ±] : βˆƒ x : Ξ±, βˆ€ y : Ξ±, y ∈ submonoid.powers x := by { simp_rw [mem_powers_iff_mem_zpowers], exact is_cyclic.exists_generator Ξ± } section variables [decidable_eq Ξ±] [fintype Ξ±] @[to_additive] lemma is_cyclic.image_range_order_of (ha : βˆ€ x : Ξ±, x ∈ zpowers a) : finset.image (Ξ» i, a ^ i) (range (order_of a)) = univ := begin simp_rw [←set_like.mem_coe] at ha, simp only [image_range_order_of, set.eq_univ_iff_forall.mpr ha, set.to_finset_univ], end @[to_additive] lemma is_cyclic.image_range_card (ha : βˆ€ x : Ξ±, x ∈ zpowers a) : finset.image (Ξ» i, a ^ i) (range (fintype.card Ξ±)) = univ := by rw [← order_of_eq_card_of_forall_mem_zpowers ha, is_cyclic.image_range_order_of ha] end section totient variables [decidable_eq Ξ±] [fintype Ξ±] (hn : βˆ€ n : β„•, 0 < n β†’ (univ.filter (Ξ» a : Ξ±, a ^ n = 1)).card ≀ n) include hn private lemma card_pow_eq_one_eq_order_of_aux (a : Ξ±) : (finset.univ.filter (Ξ» b : Ξ±, b ^ order_of a = 1)).card = order_of a := le_antisymm (hn _ (order_of_pos a)) (calc order_of a = @fintype.card (zpowers a) (id _) : order_eq_card_zpowers ... ≀ @fintype.card (↑(univ.filter (Ξ» b : Ξ±, b ^ order_of a = 1)) : set Ξ±) (fintype.of_finset _ (Ξ» _, iff.rfl)) : @fintype.card_le_of_injective (zpowers a) (↑(univ.filter (Ξ» b : Ξ±, b ^ order_of a = 1)) : set Ξ±) (id _) (id _) (Ξ» b, ⟨b.1, mem_filter.2 ⟨mem_univ _, let ⟨i, hi⟩ := b.2 in by rw [← hi, ← zpow_coe_nat, ← zpow_mul, mul_comm, zpow_mul, zpow_coe_nat, pow_order_of_eq_one, one_zpow]⟩⟩) (Ξ» _ _ h, subtype.eq (subtype.mk.inj h)) ... = (univ.filter (Ξ» b : Ξ±, b ^ order_of a = 1)).card : fintype.card_of_finset _ _) open_locale nat -- use Ο† for nat.totient private lemma card_order_of_eq_totient_aux₁ : βˆ€ {d : β„•}, d ∣ fintype.card Ξ± β†’ 0 < (univ.filter (Ξ» a : Ξ±, order_of a = d)).card β†’ (univ.filter (Ξ» a : Ξ±, order_of a = d)).card = Ο† d := begin intros d hd hd0, induction d using nat.strong_rec' with d IH, rcases d.eq_zero_or_pos with rfl | hd_pos, { cases fintype.card_ne_zero (eq_zero_of_zero_dvd hd) }, rcases card_pos.1 hd0 with ⟨a, ha'⟩, have ha : order_of a = d := (mem_filter.1 ha').2, have h1 : βˆ‘ m in d.proper_divisors, (univ.filter (Ξ» a : Ξ±, order_of a = m)).card = βˆ‘ m in d.proper_divisors, Ο† m, { refine finset.sum_congr rfl (Ξ» m hm, _), simp only [mem_filter, mem_range, mem_proper_divisors] at hm, refine IH m hm.2 (hm.1.trans hd) (finset.card_pos.2 ⟨a ^ (d / m), _⟩), simp only [mem_filter, mem_univ, order_of_pow a, ha, true_and, nat.gcd_eq_right (div_dvd_of_dvd hm.1), nat.div_div_self hm.1 hd_pos] }, have h2 : βˆ‘ m in d.divisors, (univ.filter (Ξ» a : Ξ±, order_of a = m)).card = βˆ‘ m in d.divisors, Ο† m, { rw [←filter_dvd_eq_divisors hd_pos.ne', sum_card_order_of_eq_card_pow_eq_one hd_pos, filter_dvd_eq_divisors hd_pos.ne', sum_totient, ←ha, card_pow_eq_one_eq_order_of_aux hn a] }, simpa [divisors_eq_proper_divisors_insert_self_of_pos hd_pos, ←h1] using h2, end lemma card_order_of_eq_totient_auxβ‚‚ {d : β„•} (hd : d ∣ fintype.card Ξ±) : (univ.filter (Ξ» a : Ξ±, order_of a = d)).card = Ο† d := begin let c := fintype.card Ξ±, have hc0 : 0 < c := fintype.card_pos_iff.2 ⟨1⟩, apply card_order_of_eq_totient_aux₁ hn hd, by_contradiction h0, simp only [not_lt, _root_.le_zero_iff, card_eq_zero] at h0, apply lt_irrefl c, calc c = βˆ‘ m in c.divisors, (univ.filter (Ξ» a : Ξ±, order_of a = m)).card : by { simp only [←filter_dvd_eq_divisors hc0.ne', sum_card_order_of_eq_card_pow_eq_one hc0], apply congr_arg card, simp } ... = βˆ‘ m in c.divisors.erase d, (univ.filter (Ξ» a : Ξ±, order_of a = m)).card : by { rw eq_comm, refine (sum_subset (erase_subset _ _) (Ξ» m hm₁ hmβ‚‚, _)), have : m = d, by { contrapose! hmβ‚‚, exact mem_erase_of_ne_of_mem hmβ‚‚ hm₁ }, simp [this, h0] } ... ≀ βˆ‘ m in c.divisors.erase d, Ο† m : by { refine sum_le_sum (Ξ» m hm, _), have hmc : m ∣ c, { simp only [mem_erase, mem_divisors] at hm, tauto }, rcases (filter (Ξ» (a : Ξ±), order_of a = m) univ).card.eq_zero_or_pos with h1 | h1, { simp [h1] }, { simp [card_order_of_eq_totient_aux₁ hn hmc h1] } } ... < βˆ‘ m in c.divisors, Ο† m : sum_erase_lt_of_pos (mem_divisors.2 ⟨hd, hc0.ne'⟩) (totient_pos (pos_of_dvd_of_pos hd hc0)) ... = c : sum_totient _ end lemma is_cyclic_of_card_pow_eq_one_le : is_cyclic Ξ± := have (univ.filter (Ξ» a : Ξ±, order_of a = fintype.card Ξ±)).nonempty, from (card_pos.1 $ by rw [card_order_of_eq_totient_auxβ‚‚ hn dvd_rfl]; exact totient_pos (fintype.card_pos_iff.2 ⟨1⟩)), let ⟨x, hx⟩ := this in is_cyclic_of_order_of_eq_card x (finset.mem_filter.1 hx).2 lemma is_add_cyclic_of_card_pow_eq_one_le {Ξ±} [add_group Ξ±] [decidable_eq Ξ±] [fintype Ξ±] (hn : βˆ€ n : β„•, 0 < n β†’ (univ.filter (Ξ» a : Ξ±, n β€’ a = 0)).card ≀ n) : is_add_cyclic Ξ± := begin obtain ⟨g, hg⟩ := @is_cyclic_of_card_pow_eq_one_le (multiplicative Ξ±) _ _ _ hn, exact ⟨⟨g, hg⟩⟩ end attribute [to_additive is_cyclic_of_card_pow_eq_one_le] is_add_cyclic_of_card_pow_eq_one_le end totient lemma is_cyclic.card_order_of_eq_totient [is_cyclic Ξ±] [fintype Ξ±] {d : β„•} (hd : d ∣ fintype.card Ξ±) : (univ.filter (Ξ» a : Ξ±, order_of a = d)).card = totient d := begin classical, apply card_order_of_eq_totient_auxβ‚‚ (Ξ» n, is_cyclic.card_pow_eq_one_le) hd end lemma is_add_cyclic.card_order_of_eq_totient {Ξ±} [add_group Ξ±] [is_add_cyclic Ξ±] [fintype Ξ±] {d : β„•} (hd : d ∣ fintype.card Ξ±) : (univ.filter (Ξ» a : Ξ±, add_order_of a = d)).card = totient d := begin obtain ⟨g, hg⟩ := id β€Ήis_add_cyclic Ξ±β€Ί, exact @is_cyclic.card_order_of_eq_totient (multiplicative Ξ±) _ ⟨⟨g, hg⟩⟩ _ _ hd end attribute [to_additive is_cyclic.card_order_of_eq_totient] is_add_cyclic.card_order_of_eq_totient /-- A finite group of prime order is simple. -/ @[to_additive] lemma is_simple_group_of_prime_card {Ξ± : Type u} [group Ξ±] [fintype Ξ±] {p : β„•} [hp : fact p.prime] (h : fintype.card Ξ± = p) : is_simple_group Ξ± := ⟨begin have h' := nat.prime.one_lt (fact.out p.prime), rw ← h at h', haveI := fintype.one_lt_card_iff_nontrivial.1 h', apply exists_pair_ne Ξ±, end, Ξ» H Hn, begin classical, have hcard := card_subgroup_dvd_card H, rw [h, dvd_prime (fact.out p.prime)] at hcard, refine hcard.imp (Ξ» h1, _) (Ξ» hp, _), { haveI := fintype.card_le_one_iff_subsingleton.1 (le_of_eq h1), apply eq_bot_of_subsingleton }, { exact eq_top_of_card_eq _ (hp.trans h.symm) } end⟩ end cyclic section quotient_center open subgroup variables {G : Type*} {H : Type*} [group G] [group H] /-- A group is commutative if the quotient by the center is cyclic. Also see `comm_group_of_cycle_center_quotient` for the `comm_group` instance. -/ @[to_additive commutative_of_add_cyclic_center_quotient "A group is commutative if the quotient by the center is cyclic. Also see `add_comm_group_of_cycle_center_quotient` for the `add_comm_group` instance."] lemma commutative_of_cyclic_center_quotient [is_cyclic H] (f : G β†’* H) (hf : f.ker ≀ center G) (a b : G) : a * b = b * a := let ⟨⟨x, y, (hxy : f y = x)⟩, (hx : βˆ€ a : f.range, a ∈ zpowers _)⟩ := is_cyclic.exists_generator f.range in let ⟨m, hm⟩ := hx ⟨f a, a, rfl⟩ in let ⟨n, hn⟩ := hx ⟨f b, b, rfl⟩ in have hm : x ^ m = f a, by simpa [subtype.ext_iff] using hm, have hn : x ^ n = f b, by simpa [subtype.ext_iff] using hn, have ha : y ^ (-m) * a ∈ center G, from hf (by rw [f.mem_ker, f.map_mul, f.map_zpow, hxy, zpow_neg, hm, inv_mul_self]), have hb : y ^ (-n) * b ∈ center G, from hf (by rw [f.mem_ker, f.map_mul, f.map_zpow, hxy, zpow_neg, hn, inv_mul_self]), calc a * b = y ^ m * ((y ^ (-m) * a) * y ^ n) * (y ^ (-n) * b) : by simp [mul_assoc] ... = y ^ m * (y ^ n * (y ^ (-m) * a)) * (y ^ (-n) * b) : by rw [mem_center_iff.1 ha] ... = y ^ m * y ^ n * y ^ (-m) * (a * (y ^ (-n) * b)) : by simp [mul_assoc] ... = y ^ m * y ^ n * y ^ (-m) * ((y ^ (-n) * b) * a) : by rw [mem_center_iff.1 hb] ... = b * a : by group /-- A group is commutative if the quotient by the center is cyclic. -/ @[to_additive commutative_of_add_cycle_center_quotient "A group is commutative if the quotient by the center is cyclic."] def comm_group_of_cycle_center_quotient [is_cyclic H] (f : G β†’* H) (hf : f.ker ≀ center G) : comm_group G := { mul_comm := commutative_of_cyclic_center_quotient f hf, ..show group G, by apply_instance } end quotient_center namespace is_simple_group section comm_group variables [comm_group Ξ±] [is_simple_group Ξ±] @[priority 100, to_additive is_simple_add_group.is_add_cyclic] instance : is_cyclic Ξ± := begin cases subsingleton_or_nontrivial Ξ± with hi hi; haveI := hi, { apply is_cyclic_of_subsingleton }, { obtain ⟨g, hg⟩ := exists_ne (1 : Ξ±), refine ⟨⟨g, Ξ» x, _⟩⟩, cases is_simple_order.eq_bot_or_eq_top (subgroup.zpowers g) with hb ht, { exfalso, apply hg, rw [← subgroup.mem_bot, ← hb], apply subgroup.mem_zpowers }, { rw ht, apply subgroup.mem_top } } end @[to_additive] theorem prime_card [fintype Ξ±] : (fintype.card Ξ±).prime := begin have h0 : 0 < fintype.card Ξ± := fintype.card_pos_iff.2 (by apply_instance), obtain ⟨g, hg⟩ := is_cyclic.exists_generator Ξ±, rw nat.prime_def_lt'', refine ⟨fintype.one_lt_card_iff_nontrivial.2 infer_instance, Ξ» n hn, _⟩, refine (is_simple_order.eq_bot_or_eq_top (subgroup.zpowers (g ^ n))).symm.imp _ _, { intro h, have hgo := order_of_pow g, rw [order_of_eq_card_of_forall_mem_zpowers hg, nat.gcd_eq_right_iff_dvd.1 hn, order_of_eq_card_of_forall_mem_zpowers, eq_comm, nat.div_eq_iff_eq_mul_left (nat.pos_of_dvd_of_pos hn h0) hn] at hgo, { exact (mul_left_cancelβ‚€ (ne_of_gt h0) ((mul_one (fintype.card Ξ±)).trans hgo)).symm }, { intro x, rw h, exact subgroup.mem_top _ } }, { intro h, apply le_antisymm (nat.le_of_dvd h0 hn), rw ← order_of_eq_card_of_forall_mem_zpowers hg, apply order_of_le_of_pow_eq_one (nat.pos_of_dvd_of_pos hn h0), rw [← subgroup.mem_bot, ← h], exact subgroup.mem_zpowers _ } end end comm_group end is_simple_group @[to_additive add_comm_group.is_simple_iff_is_add_cyclic_and_prime_card] theorem comm_group.is_simple_iff_is_cyclic_and_prime_card [fintype Ξ±] [comm_group Ξ±] : is_simple_group Ξ± ↔ is_cyclic Ξ± ∧ (fintype.card Ξ±).prime := begin split, { introI h, exact ⟨is_simple_group.is_cyclic, is_simple_group.prime_card⟩ }, { rintro ⟨hc, hp⟩, haveI : fact (fintype.card Ξ±).prime := ⟨hp⟩, exact is_simple_group_of_prime_card rfl } end section exponent open monoid @[to_additive] lemma is_cyclic.exponent_eq_card [group Ξ±] [is_cyclic Ξ±] [fintype Ξ±] : exponent Ξ± = fintype.card Ξ± := begin obtain ⟨g, hg⟩ := is_cyclic.exists_generator Ξ±, apply nat.dvd_antisymm, { rw [←lcm_order_eq_exponent, finset.lcm_dvd_iff], exact Ξ» b _, order_of_dvd_card_univ }, rw ←order_of_eq_card_of_forall_mem_zpowers hg, exact order_dvd_exponent _ end @[to_additive] lemma is_cyclic.of_exponent_eq_card [comm_group Ξ±] [fintype Ξ±] (h : exponent Ξ± = fintype.card Ξ±) : is_cyclic Ξ± := let ⟨g, _, hg⟩ := finset.mem_image.mp (finset.max'_mem _ _) in is_cyclic_of_order_of_eq_card g $ hg.trans $ exponent_eq_max'_order_of.symm.trans h @[to_additive] lemma is_cyclic.iff_exponent_eq_card [comm_group Ξ±] [fintype Ξ±] : is_cyclic Ξ± ↔ exponent Ξ± = fintype.card Ξ± := ⟨λ h, by exactI is_cyclic.exponent_eq_card, is_cyclic.of_exponent_eq_card⟩ @[to_additive] lemma is_cyclic.exponent_eq_zero_of_infinite [group Ξ±] [is_cyclic Ξ±] [infinite Ξ±] : exponent Ξ± = 0 := let ⟨g, hg⟩ := is_cyclic.exists_generator Ξ± in exponent_eq_zero_of_order_zero $ infinite.order_of_eq_zero_of_forall_mem_zpowers hg end exponent
74396b833ef3d3a98d51e2a888d884c91665a957
f57749ca63d6416f807b770f67559503fdb21001
/hott/types/nat/hott.hlean
81fd9102755f0c8eb7fb64ff80ddcd39a612a3cd
[ "Apache-2.0" ]
permissive
aliassaf/lean
bd54e85bed07b1ff6f01396551867b2677cbc6ac
f9b069b6a50756588b309b3d716c447004203152
refs/heads/master
1,610,982,152,948
1,438,916,029,000
1,438,916,029,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,325
hlean
/- Copyright (c) 2015 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Floris van Doorn Theorems about the natural numbers specific to HoTT -/ import .basic open is_trunc unit empty eq equiv namespace nat definition is_hprop_le [instance] (n m : β„•) : is_hprop (n ≀ m) := begin assert lem : Ξ {n m : β„•} (p : n ≀ m) (q : n = m), p = q β–Έ le.refl n, { intros, cases p, { assert H' : q = idp, apply is_hset.elim, cases H', reflexivity}, { cases q, exfalso, apply not_succ_le_self a}}, apply is_hprop.mk, intro H1 H2, induction H2, { apply lem}, { cases H1, { exfalso, apply not_succ_le_self a}, { exact ap le.step !v_0}}, end definition le_equiv_succ_le_succ (n m : β„•) : (n ≀ m) ≃ (succ n ≀ succ m) := equiv_of_is_hprop succ_le_succ le_of_succ_le_succ definition le_succ_equiv_pred_le (n m : β„•) : (n ≀ succ m) ≃ (pred n ≀ m) := equiv_of_is_hprop pred_le_pred le_succ_of_pred_le set_option pp.beta false set_option pp.implicit true set_option pp.coercions true theorem lt_by_cases_lt {a b : β„•} {P : Type} (H1 : a < b β†’ P) (H2 : a = b β†’ P) (H3 : a > b β†’ P) (H : a < b) : lt.by_cases H1 H2 H3 = H1 H := begin unfold lt.by_cases, induction (lt.trichotomy a b) with H' H', { esimp, exact ap H1 !is_hprop.elim}, { exfalso, cases H' with H' H', apply lt.irrefl, exact H' β–Έ H, exact lt.asymm H H'} end theorem lt_by_cases_eq {a b : β„•} {P : Type} (H1 : a < b β†’ P) (H2 : a = b β†’ P) (H3 : a > b β†’ P) (H : a = b) : lt.by_cases H1 H2 H3 = H2 H := begin unfold lt.by_cases, induction (lt.trichotomy a b) with H' H', { exfalso, apply lt.irrefl, exact H β–Έ H'}, { cases H' with H' H', esimp, exact ap H2 !is_hprop.elim, exfalso, apply lt.irrefl, exact H β–Έ H'} end theorem lt_by_cases_ge {a b : β„•} {P : Type} (H1 : a < b β†’ P) (H2 : a = b β†’ P) (H3 : a > b β†’ P) (H : a > b) : lt.by_cases H1 H2 H3 = H3 H := begin unfold lt.by_cases, induction (lt.trichotomy a b) with H' H', { exfalso, exact lt.asymm H H'}, { cases H' with H' H', exfalso, apply lt.irrefl, exact H' β–Έ H, esimp, exact ap H3 !is_hprop.elim} end theorem lt_ge_by_cases_lt {n m : β„•} {P : Type} (H1 : n < m β†’ P) (H2 : n β‰₯ m β†’ P) (H : n < m) : lt_ge_by_cases H1 H2 = H1 H := by apply lt_by_cases_lt theorem lt_ge_by_cases_ge {n m : β„•} {P : Type} (H1 : n < m β†’ P) (H2 : n β‰₯ m β†’ P) (H : n β‰₯ m) : lt_ge_by_cases H1 H2 = H2 H := begin unfold [lt_ge_by_cases,lt.by_cases], induction (lt.trichotomy n m) with H' H', { exfalso, apply lt.irrefl, exact lt_of_le_of_lt H H'}, { cases H' with H' H'; all_goals (esimp; apply ap H2 !is_hprop.elim)} end theorem lt_ge_by_cases_le {n m : β„•} {P : Type} {H1 : n ≀ m β†’ P} {H2 : n β‰₯ m β†’ P} (H : n ≀ m) (Heq : Ξ (p : n = m), H1 (le_of_eq p) = H2 (le_of_eq p⁻¹)) : lt_ge_by_cases (Ξ»H', H1 (le_of_lt H')) H2 = H1 H := begin unfold [lt_ge_by_cases,lt.by_cases], induction (lt.trichotomy n m) with H' H', { esimp, apply ap H1 !is_hprop.elim}, { cases H' with H' H', esimp, exact !Heq⁻¹ ⬝ ap H1 !is_hprop.elim, exfalso, apply lt.irrefl, apply lt_of_le_of_lt H H'} end end nat
809e2575841f4836bdceea7bae93fa399de1f330
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/missingSizeOfArrayGetThm.lean
058e020c0a26b9b15c308932f56fc27bfc74e686
[ "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
430
lean
inductive Node (Data : Type) : Type where | empty : Node Data | node (children : Array (Node Data)) : Node Data | leaf (data : Data) : Node Data def Node.FixedBranching (n : Nat) : Node Data β†’ Prop | empty => True | node children => children.size = n ∧ βˆ€ i, (children.get i).FixedBranching n | leaf _ => True structure MNode (Data : Type) (m : Nat) where node : Node Data fix_branching : node.FixedBranching m
caf7eb1ad6c525f5ec244dc55a12b1c7ace0525c
ff5230333a701471f46c57e8c115a073ebaaa448
/tests/lean/run/mrw.lean
6b8130151b0c5fd869e13d895243b3693db9275d
[ "Apache-2.0" ]
permissive
stanford-cs242/lean
f81721d2b5d00bc175f2e58c57b710d465e6c858
7bd861261f4a37326dcf8d7a17f1f1f330e4548c
refs/heads/master
1,600,957,431,849
1,576,465,093,000
1,576,465,093,000
225,779,423
0
3
Apache-2.0
1,575,433,936,000
1,575,433,935,000
null
UTF-8
Lean
false
false
134
lean
example (n : nat) : βˆƒ x, x + n = n + 1 := begin constructor, fail_if_success {rw [zero_add] {unify := ff}}, rw [add_comm] end
961402fc91389c363001e6f5f00215bad040c6fa
618003631150032a5676f229d13a079ac875ff77
/src/analysis/mean_inequalities.lean
82e15ed80cc9b42a28fab77437651f986ec4bf4f
[ "Apache-2.0" ]
permissive
awainverse/mathlib
939b68c8486df66cfda64d327ad3d9165248c777
ea76bd8f3ca0a8bf0a166a06a475b10663dec44a
refs/heads/master
1,659,592,962,036
1,590,987,592,000
1,590,987,592,000
268,436,019
1
0
Apache-2.0
1,590,990,500,000
1,590,990,500,000
null
UTF-8
Lean
false
false
6,816
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 analysis.convex.specific_functions import analysis.special_functions.pow /-! # Mean value inequalities In this file we prove various inequalities between mean values: arithmetic mean, geometric mean, generalized mean (natural and integer cases). For generalized means we only prove $\left( βˆ‘_j w_j z_j \right)^n ≀ βˆ‘_j w_j z_j^n$ because standard versions would require $\sqrt[n]{x}$ which is not implemented in `mathlib` yet. Probably a better approach to the generalized means inequality is to prove `convex_on_rpow` in `analysis/convex/specific_functions` first, then apply it. It is not yet clear which versions will be useful in the future, so we provide two different forms of most inequalities : for `ℝ` and for `ℝβ‰₯0`. For the AM-GM inequality we also prove special cases for `n=2` and `n=3`. -/ universes u v open real finset open_locale classical nnreal variables {ΞΉ : Type u} (s : finset ΞΉ) /-- Geometric mean is less than or equal to the arithmetic mean, weighted version for functions on `finset`s. -/ theorem real.am_gm_weighted (w z : ΞΉ β†’ ℝ) (hw : βˆ€ i ∈ s, 0 ≀ w i) (hw' : s.sum w = 1) (hz : βˆ€ i ∈ s, 0 ≀ z i) : s.prod (Ξ» i, (z i) ^ (w i)) ≀ s.sum (Ξ» i, w i * z i) := begin let s' := s.filter (Ξ» i, w i β‰  0), rw [← sum_filter_ne_zero] at hw', suffices : s'.prod (Ξ» i, (z i) ^ (w i)) ≀ s'.sum (Ξ» i, w i * z i), { have A : βˆ€ i ∈ s, i βˆ‰ s' β†’ w i = 0, { intros i hi hi', simpa only [hi, mem_filter, ne.def, true_and, not_not] using hi' }, have B : βˆ€ i ∈ s, i βˆ‰ s' β†’ (z i) ^ (w i) = 1, from Ξ» i hi hi', by rw [A i hi hi', rpow_zero], have C : βˆ€ i ∈ s, i βˆ‰ s' β†’ w i * z i = 0, from Ξ» i hi hi', by rw [A i hi hi', zero_mul], rwa [← prod_subset s.filter_subset B, ← sum_subset s.filter_subset C] }, have A : βˆ€ i ∈ s', i ∈ s ∧ w i β‰  0, from Ξ» i hi, mem_filter.1 hi, replace hz : βˆ€ i ∈ s', 0 ≀ z i := Ξ» i hi, hz i (A i hi).1, replace hw : βˆ€ i ∈ s', 0 ≀ w i := Ξ» i hi, hw i (A i hi).1, by_cases B : βˆƒ i ∈ s', z i = 0, { rcases B with ⟨i, imem, hzi⟩, rw [prod_eq_zero imem], { exact sum_nonneg (Ξ» j hj, mul_nonneg (hw j hj) (hz j hj)) }, { rw hzi, exact zero_rpow (A i imem).2 } }, { replace hz : βˆ€ i ∈ s', 0 < z i, from Ξ» i hi, lt_of_le_of_ne (hz _ hi) (Ξ» h, B ⟨i, hi, h.symm⟩), have := convex_on_exp.map_sum_le hw hw' (Ξ» i _, set.mem_univ $ log (z i)), simp only [exp_sum, (∘), smul_eq_mul, mul_comm (w _) (log _)] at this, convert this using 1, { exact prod_congr rfl (Ξ» i hi, rpow_def_of_pos (hz i hi) _) }, { exact sum_congr rfl (Ξ» i hi, congr_arg _ (exp_log $ hz i hi).symm) } } end theorem nnreal.am_gm_weighted (w z : ΞΉ β†’ ℝβ‰₯0) (hw' : s.sum w = 1) : s.prod (Ξ» i, (z i) ^ (w i:ℝ)) ≀ s.sum (Ξ» i, w i * z i) := begin rw [← nnreal.coe_le_coe, nnreal.coe_prod, nnreal.coe_sum], refine real.am_gm_weighted _ _ _ (Ξ» i _, (w i).coe_nonneg) _ (Ξ» i _, (z i).coe_nonneg), assumption_mod_cast end theorem nnreal.am_gm2_weighted (w₁ wβ‚‚ p₁ pβ‚‚ : ℝβ‰₯0) (hw : w₁ + wβ‚‚ = 1) : p₁ ^ (w₁:ℝ) * pβ‚‚ ^ (wβ‚‚:ℝ) ≀ w₁ * p₁ + wβ‚‚ * pβ‚‚ := begin have := nnreal.am_gm_weighted (univ : finset (fin 2)) (fin.cons w₁ $ fin.cons wβ‚‚ fin_zero_elim) (fin.cons p₁ $ fin.cons pβ‚‚ $ fin_zero_elim), simp only [fin.prod_univ_succ, fin.sum_univ_succ, fin.prod_univ_zero, fin.sum_univ_zero, fin.cons_succ, fin.cons_zero, add_zero, mul_one] at this, exact this hw end theorem real.am_gm2_weighted {w₁ wβ‚‚ p₁ pβ‚‚ : ℝ} (hw₁ : 0 ≀ w₁) (hwβ‚‚ : 0 ≀ wβ‚‚) (hp₁ : 0 ≀ p₁) (hpβ‚‚ : 0 ≀ pβ‚‚) (hw : w₁ + wβ‚‚ = 1) : p₁ ^ w₁ * pβ‚‚ ^ wβ‚‚ ≀ w₁ * p₁ + wβ‚‚ * pβ‚‚ := nnreal.am_gm2_weighted ⟨w₁, hwβ‚βŸ© ⟨wβ‚‚, hwβ‚‚βŸ© ⟨p₁, hpβ‚βŸ© ⟨pβ‚‚, hpβ‚‚βŸ© $ nnreal.coe_eq.1 $ by assumption theorem nnreal.am_gm3_weighted (w₁ wβ‚‚ w₃ p₁ pβ‚‚ p₃ : ℝβ‰₯0) (hw : w₁ + wβ‚‚ + w₃ = 1) : p₁ ^ (w₁:ℝ) * pβ‚‚ ^ (wβ‚‚:ℝ) * p₃ ^ (w₃:ℝ) ≀ w₁ * p₁ + wβ‚‚ * pβ‚‚ + w₃ * p₃:= begin have := nnreal.am_gm_weighted (univ : finset (fin 3)) (fin.cons w₁ $ fin.cons wβ‚‚ $ fin.cons w₃ fin_zero_elim) (fin.cons p₁ $ fin.cons pβ‚‚ $ fin.cons p₃ fin_zero_elim), simp only [fin.prod_univ_succ, fin.sum_univ_succ, fin.prod_univ_zero, fin.sum_univ_zero, fin.cons_succ, fin.cons_zero, add_zero, mul_one, (add_assoc _ _ _).symm, (mul_assoc _ _ _).symm] at this, exact this hw end /-- Young's inequality, `ℝβ‰₯0` version -/ theorem nnreal.young_inequality (a b : ℝβ‰₯0) {p q : ℝβ‰₯0} (hp : 1 < p) (hq : 1 < q) (hpq : 1/p + 1/q = 1) : a * b ≀ a^(p:ℝ) / p + b^(q:ℝ) / q := begin have := nnreal.am_gm2_weighted (1/p) (1/q) (a^(p:ℝ)) (b^(q:ℝ)) hpq, simp only [← nnreal.rpow_mul, one_div_eq_inv, nnreal.coe_div, nnreal.coe_one] at this, rw [mul_inv_cancel, mul_inv_cancel, nnreal.rpow_one, nnreal.rpow_one] at this, { ring at ⊒ this, convert this; { rw [nnreal.div_def, nnreal.div_def], ring } }, { exact ne_of_gt (lt_trans zero_lt_one hq) }, { exact ne_of_gt (lt_trans zero_lt_one hp) } end /-- Young's inequality, `ℝ` version -/ theorem real.young_inequality {a b : ℝ} (ha : 0 ≀ a) (hb : 0 ≀ b) {p q : ℝ} (hp : 1 < p) (hq : 1 < q) (hpq : 1/p + 1/q = 1) : a * b ≀ a^p / p + b^q / q := @nnreal.young_inequality ⟨a, ha⟩ ⟨b, hb⟩ ⟨p, le_trans zero_le_one (le_of_lt hp)⟩ ⟨q, le_trans zero_le_one (le_of_lt hq)⟩ hp hq (nnreal.coe_eq.1 hpq) theorem real.pow_am_le_am_pow (w z : ΞΉ β†’ ℝ) (hw : βˆ€ i ∈ s, 0 ≀ w i) (hw' : s.sum w = 1) (hz : βˆ€ i ∈ s, 0 ≀ z i) (n : β„•) : (s.sum (Ξ» i, w i * z i)) ^ n ≀ s.sum (Ξ» i, w i * z i ^ n) := (convex_on_pow n).map_sum_le hw hw' hz theorem nnreal.pow_am_le_am_pow (w z : ΞΉ β†’ ℝβ‰₯0) (hw' : s.sum w = 1) (n : β„•) : (s.sum (Ξ» i, w i * z i)) ^ n ≀ s.sum (Ξ» i, w i * z i ^ n) := begin rw [← nnreal.coe_le_coe], push_cast, refine (convex_on_pow n).map_sum_le (Ξ» i _, (w i).coe_nonneg) _ (Ξ» i _, (z i).coe_nonneg), assumption_mod_cast end theorem real.pow_am_le_am_pow_of_even (w z : ΞΉ β†’ ℝ) (hw : βˆ€ i ∈ s, 0 ≀ w i) (hw' : s.sum w = 1) {n : β„•} (hn : n.even) : (s.sum (Ξ» i, w i * z i)) ^ n ≀ s.sum (Ξ» i, w i * z i ^ n) := (convex_on_pow_of_even hn).map_sum_le hw hw' (Ξ» _ _, trivial) theorem real.fpow_am_le_am_fpow (w z : ΞΉ β†’ ℝ) (hw : βˆ€ i ∈ s, 0 ≀ w i) (hw' : s.sum w = 1) (hz : βˆ€ i ∈ s, 0 < z i) (m : β„€) : (s.sum (Ξ» i, w i * z i)) ^ m ≀ s.sum (Ξ» i, w i * z i ^ m) := (convex_on_fpow m).map_sum_le hw hw' hz
136c51a8a961daa9467854d064e33fc23634403f
c777c32c8e484e195053731103c5e52af26a25d1
/src/linear_algebra/span.lean
54c850ab6fabd557be8d03803544af90dbd401cb
[ "Apache-2.0" ]
permissive
kbuzzard/mathlib
2ff9e85dfe2a46f4b291927f983afec17e946eb8
58537299e922f9c77df76cb613910914a479c1f7
refs/heads/master
1,685,313,702,744
1,683,974,212,000
1,683,974,212,000
128,185,277
1
0
null
1,522,920,600,000
1,522,920,600,000
null
UTF-8
Lean
false
false
35,886
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, Kevin Buzzard, Yury Kudryashov, FrΓ©dΓ©ric Dupuis, Heather Macbeth -/ import linear_algebra.basic import order.compactly_generated import order.omega_complete_partial_order /-! # The span of a set of vectors, as a submodule > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. * `submodule.span s` is defined to be the smallest submodule containing the set `s`. ## Notations * We introduce the notation `R βˆ™ v` for the span of a singleton, `submodule.span R {v}`. This is `\.`, not the same as the scalar multiplication `β€’`/`\bub`. -/ variables {R Rβ‚‚ K M Mβ‚‚ V S : Type*} namespace submodule open function set open_locale pointwise section add_comm_monoid variables [semiring R] [add_comm_monoid M] [module R M] variables {x : M} (p p' : submodule R M) variables [semiring Rβ‚‚] {σ₁₂ : R β†’+* Rβ‚‚} variables [add_comm_monoid Mβ‚‚] [module Rβ‚‚ Mβ‚‚] section variables (R) /-- The span of a set `s βŠ† M` is the smallest submodule of M that contains `s`. -/ def span (s : set M) : submodule R M := Inf {p | s βŠ† p} end variables {s t : set M} lemma mem_span : x ∈ span R s ↔ βˆ€ p : submodule R M, s βŠ† p β†’ x ∈ p := mem_Interβ‚‚ lemma subset_span : s βŠ† span R s := Ξ» x h, mem_span.2 $ Ξ» p hp, hp h lemma span_le {p} : span R s ≀ p ↔ s βŠ† p := ⟨subset.trans subset_span, Ξ» ss x h, mem_span.1 h _ ss⟩ lemma span_mono (h : s βŠ† t) : span R s ≀ span R t := span_le.2 $ subset.trans h subset_span lemma span_monotone : monotone (span R : set M β†’ submodule R M) := Ξ» _ _, span_mono lemma span_eq_of_le (h₁ : s βŠ† p) (hβ‚‚ : p ≀ span R s) : span R s = p := le_antisymm (span_le.2 h₁) hβ‚‚ lemma span_eq : span R (p : set M) = p := span_eq_of_le _ (subset.refl _) subset_span lemma span_eq_span (hs : s βŠ† span R t) (ht : t βŠ† span R s) : span R s = span R t := le_antisymm (span_le.2 hs) (span_le.2 ht) /-- A version of `submodule.span_eq` for when the span is by a smaller ring. -/ @[simp] lemma span_coe_eq_restrict_scalars [semiring S] [has_smul S R] [module S M] [is_scalar_tower S R M] : span S (p : set M) = p.restrict_scalars S := span_eq (p.restrict_scalars S) lemma map_span [ring_hom_surjective σ₁₂] (f : M β†’β‚›β‚—[σ₁₂] Mβ‚‚) (s : set M) : (span R s).map f = span Rβ‚‚ (f '' s) := eq.symm $ span_eq_of_le _ (set.image_subset f subset_span) $ map_le_iff_le_comap.2 $ span_le.2 $ Ξ» x hx, subset_span ⟨x, hx, rfl⟩ alias submodule.map_span ← _root_.linear_map.map_span lemma map_span_le [ring_hom_surjective σ₁₂] (f : M β†’β‚›β‚—[σ₁₂] Mβ‚‚) (s : set M) (N : submodule Rβ‚‚ Mβ‚‚) : map f (span R s) ≀ N ↔ βˆ€ m ∈ s, f m ∈ N := begin rw [f.map_span, span_le, set.image_subset_iff], exact iff.rfl end alias submodule.map_span_le ← _root_.linear_map.map_span_le @[simp] lemma span_insert_zero : span R (insert (0 : M) s) = span R s := begin refine le_antisymm _ (submodule.span_mono (set.subset_insert 0 s)), rw [span_le, set.insert_subset], exact ⟨by simp only [set_like.mem_coe, submodule.zero_mem], submodule.subset_span⟩, end /- See also `span_preimage_eq` below. -/ lemma span_preimage_le (f : M β†’β‚›β‚—[σ₁₂] Mβ‚‚) (s : set Mβ‚‚) : span R (f ⁻¹' s) ≀ (span Rβ‚‚ s).comap f := by { rw [span_le, comap_coe], exact preimage_mono (subset_span), } alias submodule.span_preimage_le ← _root_.linear_map.span_preimage_le lemma closure_subset_span {s : set M} : (add_submonoid.closure s : set M) βŠ† span R s := (@add_submonoid.closure_le _ _ _ (span R s).to_add_submonoid).mpr subset_span lemma closure_le_to_add_submonoid_span {s : set M} : add_submonoid.closure s ≀ (span R s).to_add_submonoid := closure_subset_span @[simp] lemma span_closure {s : set M} : span R (add_submonoid.closure s : set M) = span R s := le_antisymm (span_le.mpr closure_subset_span) (span_mono add_submonoid.subset_closure) /-- An induction principle for span membership. If `p` holds for 0 and all elements of `s`, and is preserved under addition and scalar multiplication, then `p` holds for all elements of the span of `s`. -/ @[elab_as_eliminator] lemma span_induction {p : M β†’ Prop} (h : x ∈ span R s) (Hs : βˆ€ x ∈ s, p x) (H0 : p 0) (H1 : βˆ€ x y, p x β†’ p y β†’ p (x + y)) (H2 : βˆ€ (a:R) x, p x β†’ p (a β€’ x)) : p x := (@span_le _ _ _ _ _ _ ⟨p, H1, H0, H2⟩).2 Hs h /-- A dependent version of `submodule.span_induction`. -/ lemma span_induction' {p : Ξ  x, x ∈ span R s β†’ Prop} (Hs : βˆ€ x (h : x ∈ s), p x (subset_span h)) (H0 : p 0 (submodule.zero_mem _)) (H1 : βˆ€ x hx y hy, p x hx β†’ p y hy β†’ p (x + y) (submodule.add_mem _ β€Ή_β€Ί β€Ή_β€Ί)) (H2 : βˆ€ (a : R) x hx, p x hx β†’ p (a β€’ x) (submodule.smul_mem _ _ β€Ή_β€Ί)) {x} (hx : x ∈ span R s) : p x hx := begin refine exists.elim _ (Ξ» (hx : x ∈ span R s) (hc : p x hx), hc), refine span_induction hx (Ξ» m hm, ⟨subset_span hm, Hs m hm⟩) ⟨zero_mem _, H0⟩ (Ξ» x y hx hy, exists.elim hx $ Ξ» hx' hx, exists.elim hy $ Ξ» hy' hy, ⟨add_mem hx' hy', H1 _ _ _ _ hx hy⟩) (Ξ» r x hx, exists.elim hx $ Ξ» hx' hx, ⟨smul_mem _ _ hx', H2 r _ _ hx⟩) end @[simp] lemma span_span_coe_preimage : span R ((coe : span R s β†’ M) ⁻¹' s) = ⊀ := eq_top_iff.2 $ Ξ» x, subtype.rec_on x $ Ξ» x hx _, begin refine span_induction' (Ξ» x hx, _) _ (Ξ» x y _ _, _) (Ξ» r x _, _) hx, { exact subset_span hx }, { exact zero_mem _ }, { exact add_mem }, { exact smul_mem _ _ } end lemma span_nat_eq_add_submonoid_closure (s : set M) : (span β„• s).to_add_submonoid = add_submonoid.closure s := begin refine eq.symm (add_submonoid.closure_eq_of_le subset_span _), apply add_submonoid.to_nat_submodule.symm.to_galois_connection.l_le _, rw span_le, exact add_submonoid.subset_closure, end @[simp] lemma span_nat_eq (s : add_submonoid M) : (span β„• (s : set M)).to_add_submonoid = s := by rw [span_nat_eq_add_submonoid_closure, s.closure_eq] lemma span_int_eq_add_subgroup_closure {M : Type*} [add_comm_group M] (s : set M) : (span β„€ s).to_add_subgroup = add_subgroup.closure s := eq.symm $ add_subgroup.closure_eq_of_le _ subset_span $ Ξ» x hx, span_induction hx (Ξ» x hx, add_subgroup.subset_closure hx) (add_subgroup.zero_mem _) (Ξ» _ _, add_subgroup.add_mem _) (Ξ» _ _ _, add_subgroup.zsmul_mem _ β€Ή_β€Ί _) @[simp] lemma span_int_eq {M : Type*} [add_comm_group M] (s : add_subgroup M) : (span β„€ (s : set M)).to_add_subgroup = s := by rw [span_int_eq_add_subgroup_closure, s.closure_eq] section variables (R M) /-- `span` forms a Galois insertion with the coercion from submodule to set. -/ protected def gi : galois_insertion (@span R M _ _ _) coe := { choice := Ξ» s _, span R s, gc := Ξ» s t, span_le, le_l_u := Ξ» s, subset_span, choice_eq := Ξ» s h, rfl } end @[simp] lemma span_empty : span R (βˆ… : set M) = βŠ₯ := (submodule.gi R M).gc.l_bot @[simp] lemma span_univ : span R (univ : set M) = ⊀ := eq_top_iff.2 $ set_like.le_def.2 $ subset_span lemma span_union (s t : set M) : span R (s βˆͺ t) = span R s βŠ” span R t := (submodule.gi R M).gc.l_sup lemma span_Union {ΞΉ} (s : ΞΉ β†’ set M) : span R (⋃ i, s i) = ⨆ i, span R (s i) := (submodule.gi R M).gc.l_supr lemma span_Unionβ‚‚ {ΞΉ} {ΞΊ : ΞΉ β†’ Sort*} (s : Ξ  i, ΞΊ i β†’ set M) : span R (⋃ i j, s i j) = ⨆ i j, span R (s i j) := (submodule.gi R M).gc.l_suprβ‚‚ lemma span_attach_bUnion [decidable_eq M] {Ξ± : Type*} (s : finset Ξ±) (f : s β†’ finset M) : span R (s.attach.bUnion f : set M) = ⨆ x, span R (f x) := by simpa [span_Union] lemma sup_span : p βŠ” span R s = span R (p βˆͺ s) := by rw [submodule.span_union, p.span_eq] lemma span_sup : span R s βŠ” p = span R (s βˆͺ p) := by rw [submodule.span_union, p.span_eq] /- Note that the character `βˆ™` U+2219 used below is different from the scalar multiplication character `β€’` U+2022 and the matrix multiplication character `⬝` U+2B1D. -/ notation R` βˆ™ `:1000 x := span R (@singleton _ _ set.has_singleton x) lemma span_eq_supr_of_singleton_spans (s : set M) : span R s = ⨆ x ∈ s, R βˆ™ x := by simp only [←span_Union, set.bUnion_of_singleton s] lemma span_range_eq_supr {ΞΉ : Type*} {v : ΞΉ β†’ M} : span R (range v) = ⨆ i, R βˆ™ v i := by rw [span_eq_supr_of_singleton_spans, supr_range] lemma span_smul_le (s : set M) (r : R) : span R (r β€’ s) ≀ span R s := begin rw span_le, rintros _ ⟨x, hx, rfl⟩, exact smul_mem (span R s) r (subset_span hx), end lemma subset_span_trans {U V W : set M} (hUV : U βŠ† submodule.span R V) (hVW : V βŠ† submodule.span R W) : U βŠ† submodule.span R W := (submodule.gi R M).gc.le_u_l_trans hUV hVW /-- See `submodule.span_smul_eq` (in `ring_theory.ideal.operations`) for `span R (r β€’ s) = r β€’ span R s` that holds for arbitrary `r` in a `comm_semiring`. -/ lemma span_smul_eq_of_is_unit (s : set M) (r : R) (hr : is_unit r) : span R (r β€’ s) = span R s := begin apply le_antisymm, { apply span_smul_le }, { convert span_smul_le (r β€’ s) ((hr.unit ⁻¹ : _) : R), rw smul_smul, erw hr.unit.inv_val, rw one_smul } end @[simp] theorem coe_supr_of_directed {ΞΉ} [hΞΉ : nonempty ΞΉ] (S : ΞΉ β†’ submodule R M) (H : directed (≀) S) : ((supr S : submodule R M) : set M) = ⋃ i, S i := begin refine subset.antisymm _ (Union_subset $ le_supr S), suffices : (span R (⋃ i, (S i : set M)) : set M) βŠ† ⋃ (i : ΞΉ), ↑(S i), by simpa only [span_Union, span_eq] using this, refine (Ξ» x hx, span_induction hx (Ξ» _, id) _ _ _); simp only [mem_Union, exists_imp_distrib], { exact hΞΉ.elim (Ξ» i, ⟨i, (S i).zero_mem⟩) }, { intros x y i hi j hj, rcases H i j with ⟨k, ik, jk⟩, exact ⟨k, add_mem (ik hi) (jk hj)⟩ }, { exact Ξ» a x i hi, ⟨i, smul_mem _ a hi⟩ }, end @[simp] theorem mem_supr_of_directed {ΞΉ} [nonempty ΞΉ] (S : ΞΉ β†’ submodule R M) (H : directed (≀) S) {x} : x ∈ supr S ↔ βˆƒ i, x ∈ S i := by { rw [← set_like.mem_coe, coe_supr_of_directed S H, mem_Union], refl } theorem mem_Sup_of_directed {s : set (submodule R M)} {z} (hs : s.nonempty) (hdir : directed_on (≀) s) : z ∈ Sup s ↔ βˆƒ y ∈ s, z ∈ y := begin haveI : nonempty s := hs.to_subtype, simp only [Sup_eq_supr', mem_supr_of_directed _ hdir.directed_coe, set_coe.exists, subtype.coe_mk] end @[norm_cast, simp] lemma coe_supr_of_chain (a : β„• β†’o submodule R M) : (↑(⨆ k, a k) : set M) = ⋃ k, (a k : set M) := coe_supr_of_directed a a.monotone.directed_le /-- We can regard `coe_supr_of_chain` as the statement that `coe : (submodule R M) β†’ set M` is Scott continuous for the Ο‰-complete partial order induced by the complete lattice structures. -/ lemma coe_scott_continuous : omega_complete_partial_order.continuous' (coe : submodule R M β†’ set M) := ⟨set_like.coe_mono, coe_supr_of_chain⟩ @[simp] lemma mem_supr_of_chain (a : β„• β†’o submodule R M) (m : M) : m ∈ (⨆ k, a k) ↔ βˆƒ k, m ∈ a k := mem_supr_of_directed a a.monotone.directed_le section variables {p p'} lemma mem_sup : x ∈ p βŠ” p' ↔ βˆƒ (y ∈ p) (z ∈ p'), y + z = x := ⟨λ h, begin rw [← span_eq p, ← span_eq p', ← span_union] at h, apply span_induction h, { rintro y (h | h), { exact ⟨y, h, 0, by simp, by simp⟩ }, { exact ⟨0, by simp, y, h, by simp⟩ } }, { exact ⟨0, by simp, 0, by simp⟩ }, { rintro _ _ ⟨y₁, hy₁, z₁, hz₁, rfl⟩ ⟨yβ‚‚, hyβ‚‚, zβ‚‚, hzβ‚‚, rfl⟩, exact ⟨_, add_mem hy₁ hyβ‚‚, _, add_mem hz₁ hzβ‚‚, by simp [add_assoc]; cc⟩ }, { rintro a _ ⟨y, hy, z, hz, rfl⟩, exact ⟨_, smul_mem _ a hy, _, smul_mem _ a hz, by simp [smul_add]⟩ } end, by rintro ⟨y, hy, z, hz, rfl⟩; exact add_mem ((le_sup_left : p ≀ p βŠ” p') hy) ((le_sup_right : p' ≀ p βŠ” p') hz)⟩ lemma mem_sup' : x ∈ p βŠ” p' ↔ βˆƒ (y : p) (z : p'), (y:M) + z = x := mem_sup.trans $ by simp only [set_like.exists, coe_mk] variables (p p') lemma coe_sup : ↑(p βŠ” p') = (p + p' : set M) := by { ext, rw [set_like.mem_coe, mem_sup, set.mem_add], simp, } lemma sup_to_add_submonoid : (p βŠ” p').to_add_submonoid = p.to_add_submonoid βŠ” p'.to_add_submonoid := begin ext x, rw [mem_to_add_submonoid, mem_sup, add_submonoid.mem_sup], refl, end lemma sup_to_add_subgroup {R M : Type*} [ring R] [add_comm_group M] [module R M] (p p' : submodule R M) : (p βŠ” p').to_add_subgroup = p.to_add_subgroup βŠ” p'.to_add_subgroup := begin ext x, rw [mem_to_add_subgroup, mem_sup, add_subgroup.mem_sup], refl, end end lemma mem_span_singleton_self (x : M) : x ∈ R βˆ™ x := subset_span rfl lemma nontrivial_span_singleton {x : M} (h : x β‰  0) : nontrivial (R βˆ™ x) := ⟨begin use [0, x, submodule.mem_span_singleton_self x], intros H, rw [eq_comm, submodule.mk_eq_zero] at H, exact h H end⟩ lemma mem_span_singleton {y : M} : x ∈ (R βˆ™ y) ↔ βˆƒ a:R, a β€’ y = x := ⟨λ h, begin apply span_induction h, { rintro y (rfl|⟨⟨⟩⟩), exact ⟨1, by simp⟩ }, { exact ⟨0, by simp⟩ }, { rintro _ _ ⟨a, rfl⟩ ⟨b, rfl⟩, exact ⟨a + b, by simp [add_smul]⟩ }, { rintro a _ ⟨b, rfl⟩, exact ⟨a * b, by simp [smul_smul]⟩ } end, by rintro ⟨a, y, rfl⟩; exact smul_mem _ _ (subset_span $ by simp)⟩ lemma le_span_singleton_iff {s : submodule R M} {vβ‚€ : M} : s ≀ (R βˆ™ vβ‚€) ↔ βˆ€ v ∈ s, βˆƒ r : R, r β€’ vβ‚€ = v := by simp_rw [set_like.le_def, mem_span_singleton] variables (R) lemma span_singleton_eq_top_iff (x : M) : (R βˆ™ x) = ⊀ ↔ βˆ€ v, βˆƒ r : R, r β€’ x = v := by { rw [eq_top_iff, le_span_singleton_iff], tauto } @[simp] lemma span_zero_singleton : (R βˆ™ (0:M)) = βŠ₯ := by { ext, simp [mem_span_singleton, eq_comm] } lemma span_singleton_eq_range (y : M) : ↑(R βˆ™ y) = range ((β€’ y) : R β†’ M) := set.ext $ Ξ» x, mem_span_singleton lemma span_singleton_smul_le {S} [monoid S] [has_smul S R] [mul_action S M] [is_scalar_tower S R M] (r : S) (x : M) : (R βˆ™ (r β€’ x)) ≀ R βˆ™ x := begin rw [span_le, set.singleton_subset_iff, set_like.mem_coe], exact smul_of_tower_mem _ _ (mem_span_singleton_self _) end lemma span_singleton_group_smul_eq {G} [group G] [has_smul G R] [mul_action G M] [is_scalar_tower G R M] (g : G) (x : M) : (R βˆ™ (g β€’ x)) = R βˆ™ x := begin refine le_antisymm (span_singleton_smul_le R g x) _, convert span_singleton_smul_le R (g⁻¹) (g β€’ x), exact (inv_smul_smul g x).symm end variables {R} lemma span_singleton_smul_eq {r : R} (hr : is_unit r) (x : M) : (R βˆ™ (r β€’ x)) = R βˆ™ x := begin lift r to RΛ£ using hr, rw ←units.smul_def, exact span_singleton_group_smul_eq R r x, end lemma disjoint_span_singleton {K E : Type*} [division_ring K] [add_comm_group E] [module K E] {s : submodule K E} {x : E} : disjoint s (K βˆ™ x) ↔ (x ∈ s β†’ x = 0) := begin refine disjoint_def.trans ⟨λ H hx, H x hx $ subset_span $ mem_singleton x, _⟩, assume H y hy hyx, obtain ⟨c, rfl⟩ := mem_span_singleton.1 hyx, by_cases hc : c = 0, { rw [hc, zero_smul] }, { rw [s.smul_mem_iff hc] at hy, rw [H hy, smul_zero] } end lemma disjoint_span_singleton' {K E : Type*} [division_ring K] [add_comm_group E] [module K E] {p : submodule K E} {x : E} (x0 : x β‰  0) : disjoint p (K βˆ™ x) ↔ x βˆ‰ p := disjoint_span_singleton.trans ⟨λ h₁ hβ‚‚, x0 (h₁ hβ‚‚), Ξ» h₁ hβ‚‚, (h₁ hβ‚‚).elim⟩ lemma mem_span_singleton_trans {x y z : M} (hxy : x ∈ R βˆ™ y) (hyz : y ∈ R βˆ™ z) : x ∈ R βˆ™ z := begin rw [← set_like.mem_coe, ← singleton_subset_iff] at *, exact submodule.subset_span_trans hxy hyz end lemma mem_span_insert {y} : x ∈ span R (insert y s) ↔ βˆƒ (a:R) (z ∈ span R s), x = a β€’ y + z := begin simp only [← union_singleton, span_union, mem_sup, mem_span_singleton, exists_prop, exists_exists_eq_and], rw [exists_comm], simp only [eq_comm, add_comm, exists_and_distrib_left] end lemma mem_span_pair {x y z : M} : z ∈ span R ({x, y} : set M) ↔ βˆƒ a b : R, a β€’ x + b β€’ y = z := by simp_rw [mem_span_insert, mem_span_singleton, exists_prop, exists_exists_eq_and, eq_comm] lemma span_insert (x) (s : set M) : span R (insert x s) = span R ({x} : set M) βŠ” span R s := by rw [insert_eq, span_union] lemma span_insert_eq_span (h : x ∈ span R s) : span R (insert x s) = span R s := span_eq_of_le _ (set.insert_subset.mpr ⟨h, subset_span⟩) (span_mono $ subset_insert _ _) lemma span_span : span R (span R s : set M) = span R s := span_eq _ variables (R S s) /-- If `R` is "smaller" ring than `S` then the span by `R` is smaller than the span by `S`. -/ lemma span_le_restrict_scalars [semiring S] [has_smul R S] [module S M] [is_scalar_tower R S M] : span R s ≀ (span S s).restrict_scalars R := submodule.span_le.2 submodule.subset_span /-- A version of `submodule.span_le_restrict_scalars` with coercions. -/ @[simp] lemma span_subset_span [semiring S] [has_smul R S] [module S M] [is_scalar_tower R S M] : ↑(span R s) βŠ† (span S s : set M) := span_le_restrict_scalars R S s /-- Taking the span by a large ring of the span by the small ring is the same as taking the span by just the large ring. -/ lemma span_span_of_tower [semiring S] [has_smul R S] [module S M] [is_scalar_tower R S M] : span S (span R s : set M) = span S s := le_antisymm (span_le.2 $ span_subset_span R S s) (span_mono subset_span) variables {R S s} lemma span_eq_bot : span R (s : set M) = βŠ₯ ↔ βˆ€ x ∈ s, (x:M) = 0 := eq_bot_iff.trans ⟨ Ξ» H x h, (mem_bot R).1 $ H $ subset_span h, Ξ» H, span_le.2 (Ξ» x h, (mem_bot R).2 $ H x h)⟩ @[simp] lemma span_singleton_eq_bot : (R βˆ™ x) = βŠ₯ ↔ x = 0 := span_eq_bot.trans $ by simp @[simp] lemma span_zero : span R (0 : set M) = βŠ₯ := by rw [←singleton_zero, span_singleton_eq_bot] lemma span_singleton_eq_span_singleton {R M : Type*} [ring R] [add_comm_group M] [module R M] [no_zero_smul_divisors R M] {x y : M} : (R βˆ™ x) = (R βˆ™ y) ↔ βˆƒ z : RΛ£, z β€’ x = y := begin by_cases hx : x = 0, { rw [hx, span_zero_singleton, eq_comm, span_singleton_eq_bot], exact ⟨λ hy, ⟨1, by rw [hy, smul_zero]⟩, Ξ» ⟨_, hz⟩, by rw [← hz, smul_zero]⟩ }, by_cases hy : y = 0, { rw [hy, span_zero_singleton, span_singleton_eq_bot], exact ⟨λ hx, ⟨1, by rw [hx, smul_zero]⟩, Ξ» ⟨z, hz⟩, (smul_eq_zero_iff_eq z).mp hz⟩ }, split, { intro hxy, cases mem_span_singleton.mp (by { rw [hxy], apply mem_span_singleton_self }) with v hv, cases mem_span_singleton.mp (by { rw [← hxy], apply mem_span_singleton_self }) with i hi, have vi : v * i = 1 := by { rw [← one_smul R y, ← hi, smul_smul] at hv, exact smul_left_injective R hy hv }, have iv : i * v = 1 := by { rw [← one_smul R x, ← hv, smul_smul] at hi, exact smul_left_injective R hx hi }, exact ⟨⟨v, i, vi, iv⟩, hv⟩ }, { rintro ⟨v, rfl⟩, rw span_singleton_group_smul_eq } end @[simp] lemma span_image [ring_hom_surjective σ₁₂] (f : M β†’β‚›β‚—[σ₁₂] Mβ‚‚) : span Rβ‚‚ (f '' s) = map f (span R s) := (map_span f s).symm lemma apply_mem_span_image_of_mem_span [ring_hom_surjective σ₁₂] (f : M β†’β‚›β‚—[σ₁₂] Mβ‚‚) {x : M} {s : set M} (h : x ∈ submodule.span R s) : f x ∈ submodule.span Rβ‚‚ (f '' s) := begin rw submodule.span_image, exact submodule.mem_map_of_mem h end @[simp] lemma map_subtype_span_singleton {p : submodule R M} (x : p) : map p.subtype (R βˆ™ x) = R βˆ™ (x : M) := by simp [← span_image] /-- `f` is an explicit argument so we can `apply` this theorem and obtain `h` as a new goal. -/ lemma not_mem_span_of_apply_not_mem_span_image [ring_hom_surjective σ₁₂] (f : M β†’β‚›β‚—[σ₁₂] Mβ‚‚) {x : M} {s : set M} (h : f x βˆ‰ submodule.span Rβ‚‚ (f '' s)) : x βˆ‰ submodule.span R s := h.imp (apply_mem_span_image_of_mem_span f) lemma supr_span {ΞΉ : Sort*} (p : ΞΉ β†’ set M) : (⨆ i, span R (p i)) = span R (⋃ i, p i) := le_antisymm (supr_le $ Ξ» i, span_mono $ subset_Union _ i) $ span_le.mpr $ Union_subset $ Ξ» i m hm, mem_supr_of_mem i $ subset_span hm lemma supr_eq_span {ΞΉ : Sort*} (p : ΞΉ β†’ submodule R M) : (⨆ i, p i) = span R (⋃ i, ↑(p i)) := by simp_rw [← supr_span, span_eq] lemma supr_to_add_submonoid {ΞΉ : Sort*} (p : ΞΉ β†’ submodule R M) : (⨆ i, p i).to_add_submonoid = ⨆ i, (p i).to_add_submonoid := begin refine le_antisymm (Ξ» x, _) (supr_le $ Ξ» i, to_add_submonoid_mono $ le_supr _ i), simp_rw [supr_eq_span, add_submonoid.supr_eq_closure, mem_to_add_submonoid, coe_to_add_submonoid], intros hx, refine submodule.span_induction hx (Ξ» x hx, _) _ (Ξ» x y hx hy, _) (Ξ» r x hx, _), { exact add_submonoid.subset_closure hx }, { exact add_submonoid.zero_mem _ }, { exact add_submonoid.add_mem _ hx hy }, { apply add_submonoid.closure_induction hx, { rintros x ⟨_, ⟨i, rfl⟩, hix : x ∈ p i⟩, apply add_submonoid.subset_closure (set.mem_Union.mpr ⟨i, _⟩), exact smul_mem _ r hix }, { rw smul_zero, exact add_submonoid.zero_mem _ }, { intros x y hx hy, rw smul_add, exact add_submonoid.add_mem _ hx hy, } } end /-- An induction principle for elements of `⨆ i, p i`. If `C` holds for `0` and all elements of `p i` for all `i`, and is preserved under addition, then it holds for all elements of the supremum of `p`. -/ @[elab_as_eliminator] lemma supr_induction {ΞΉ : Sort*} (p : ΞΉ β†’ submodule R M) {C : M β†’ Prop} {x : M} (hx : x ∈ ⨆ i, p i) (hp : βˆ€ i (x ∈ p i), C x) (h0 : C 0) (hadd : βˆ€ x y, C x β†’ C y β†’ C (x + y)) : C x := begin rw [←mem_to_add_submonoid, supr_to_add_submonoid] at hx, exact add_submonoid.supr_induction _ hx hp h0 hadd, end /-- A dependent version of `submodule.supr_induction`. -/ @[elab_as_eliminator] lemma supr_induction' {ΞΉ : Sort*} (p : ΞΉ β†’ submodule R M) {C : Ξ  x, (x ∈ ⨆ i, p i) β†’ Prop} (hp : βˆ€ i (x ∈ p i), C x (mem_supr_of_mem i β€Ή_β€Ί)) (h0 : C 0 (zero_mem _)) (hadd : βˆ€ x y hx hy, C x hx β†’ C y hy β†’ C (x + y) (add_mem β€Ή_β€Ί β€Ή_β€Ί)) {x : M} (hx : x ∈ ⨆ i, p i) : C x hx := begin refine exists.elim _ (Ξ» (hx : x ∈ ⨆ i, p i) (hc : C x hx), hc), refine supr_induction p hx (Ξ» i x hx, _) _ (Ξ» x y, _), { exact ⟨_, hp _ _ hx⟩ }, { exact ⟨_, h0⟩ }, { rintro ⟨_, Cx⟩ ⟨_, Cy⟩, refine ⟨_, hadd _ _ _ _ Cx Cy⟩ }, end @[simp] lemma span_singleton_le_iff_mem (m : M) (p : submodule R M) : (R βˆ™ m) ≀ p ↔ m ∈ p := by rw [span_le, singleton_subset_iff, set_like.mem_coe] lemma singleton_span_is_compact_element (x : M) : complete_lattice.is_compact_element (span R {x} : submodule R M) := begin rw complete_lattice.is_compact_element_iff_le_of_directed_Sup_le, intros d hemp hdir hsup, have : x ∈ Sup d, from (set_like.le_def.mp hsup) (mem_span_singleton_self x), obtain ⟨y, ⟨hyd, hxy⟩⟩ := (mem_Sup_of_directed hemp hdir).mp this, exact ⟨y, ⟨hyd, by simpa only [span_le, singleton_subset_iff]⟩⟩, end /-- The span of a finite subset is compact in the lattice of submodules. -/ lemma finset_span_is_compact_element (S : finset M) : complete_lattice.is_compact_element (span R S : submodule R M) := begin rw span_eq_supr_of_singleton_spans, simp only [finset.mem_coe], rw ←finset.sup_eq_supr, exact complete_lattice.finset_sup_compact_of_compact S (Ξ» x _, singleton_span_is_compact_element x), end /-- The span of a finite subset is compact in the lattice of submodules. -/ lemma finite_span_is_compact_element (S : set M) (h : S.finite) : complete_lattice.is_compact_element (span R S : submodule R M) := finite.coe_to_finset h β–Έ (finset_span_is_compact_element h.to_finset) instance : is_compactly_generated (submodule R M) := ⟨λ s, ⟨(Ξ» x, span R {x}) '' s, ⟨λ t ht, begin rcases (set.mem_image _ _ _).1 ht with ⟨x, hx, rfl⟩, apply singleton_span_is_compact_element, end, by rw [Sup_eq_supr, supr_image, ←span_eq_supr_of_singleton_spans, span_eq]⟩⟩⟩ /-- A submodule is equal to the supremum of the spans of the submodule's nonzero elements. -/ lemma submodule_eq_Sup_le_nonzero_spans (p : submodule R M) : p = Sup {T : submodule R M | βˆƒ (m : M) (hm : m ∈ p) (hz : m β‰  0), T = span R {m}} := begin let S := {T : submodule R M | βˆƒ (m : M) (hm : m ∈ p) (hz : m β‰  0), T = span R {m}}, apply le_antisymm, { intros m hm, by_cases h : m = 0, { rw h, simp }, { exact @le_Sup _ _ S _ ⟨m, ⟨hm, ⟨h, rfl⟩⟩⟩ m (mem_span_singleton_self m) } }, { rw Sup_le_iff, rintros S ⟨_, ⟨_, ⟨_, rfl⟩⟩⟩, rwa span_singleton_le_iff_mem } end lemma lt_sup_iff_not_mem {I : submodule R M} {a : M} : I < I βŠ” (R βˆ™ a) ↔ a βˆ‰ I := begin split, { intro h, by_contra akey, have h1 : I βŠ” (R βˆ™ a) ≀ I, { simp only [sup_le_iff], split, { exact le_refl I, }, { exact (span_singleton_le_iff_mem a I).mpr akey, } }, have h2 := gt_of_ge_of_gt h1 h, exact lt_irrefl I h2, }, { intro h, apply set_like.lt_iff_le_and_exists.mpr, split, simp only [le_sup_left], use a, split, swap, { assumption, }, { have : (R βˆ™ a) ≀ I βŠ” (R βˆ™ a) := le_sup_right, exact this (mem_span_singleton_self a), } }, end lemma mem_supr {ΞΉ : Sort*} (p : ΞΉ β†’ submodule R M) {m : M} : (m ∈ ⨆ i, p i) ↔ (βˆ€ N, (βˆ€ i, p i ≀ N) β†’ m ∈ N) := begin rw [← span_singleton_le_iff_mem, le_supr_iff], simp only [span_singleton_le_iff_mem], end section open_locale classical /-- For every element in the span of a set, there exists a finite subset of the set such that the element is contained in the span of the subset. -/ lemma mem_span_finite_of_mem_span {S : set M} {x : M} (hx : x ∈ span R S) : βˆƒ T : finset M, ↑T βŠ† S ∧ x ∈ span R (T : set M) := begin refine span_induction hx (Ξ» x hx, _) _ _ _, { refine ⟨{x}, _, _⟩, { rwa [finset.coe_singleton, set.singleton_subset_iff] }, { rw finset.coe_singleton, exact submodule.mem_span_singleton_self x } }, { use βˆ…, simp }, { rintros x y ⟨X, hX, hxX⟩ ⟨Y, hY, hyY⟩, refine ⟨X βˆͺ Y, _, _⟩, { rw finset.coe_union, exact set.union_subset hX hY }, rw [finset.coe_union, span_union, mem_sup], exact ⟨x, hxX, y, hyY, rfl⟩, }, { rintros a x ⟨T, hT, h2⟩, exact ⟨T, hT, smul_mem _ _ h2⟩ } end end variables {M' : Type*} [add_comm_monoid M'] [module R M'] (q₁ q₁' : submodule R M') /-- The product of two submodules is a submodule. -/ def prod : submodule R (M Γ— M') := { carrier := p Γ—Λ’ q₁, smul_mem' := by rintro a ⟨x, y⟩ ⟨hx, hy⟩; exact ⟨smul_mem _ a hx, smul_mem _ a hy⟩, .. p.to_add_submonoid.prod q₁.to_add_submonoid } @[simp] lemma prod_coe : (prod p q₁ : set (M Γ— M')) = p Γ—Λ’ q₁ := rfl @[simp] lemma mem_prod {p : submodule R M} {q : submodule R M'} {x : M Γ— M'} : x ∈ prod p q ↔ x.1 ∈ p ∧ x.2 ∈ q := set.mem_prod lemma span_prod_le (s : set M) (t : set M') : span R (s Γ—Λ’ t) ≀ prod (span R s) (span R t) := span_le.2 $ set.prod_mono subset_span subset_span @[simp] lemma prod_top : (prod ⊀ ⊀ : submodule R (M Γ— M')) = ⊀ := by ext; simp @[simp] lemma prod_bot : (prod βŠ₯ βŠ₯ : submodule R (M Γ— M')) = βŠ₯ := by ext ⟨x, y⟩; simp [prod.zero_eq_mk] lemma prod_mono {p p' : submodule R M} {q q' : submodule R M'} : p ≀ p' β†’ q ≀ q' β†’ prod p q ≀ prod p' q' := prod_mono @[simp] lemma prod_inf_prod : prod p q₁ βŠ“ prod p' q₁' = prod (p βŠ“ p') (q₁ βŠ“ q₁') := set_like.coe_injective set.prod_inter_prod @[simp] lemma prod_sup_prod : prod p q₁ βŠ” prod p' q₁' = prod (p βŠ” p') (q₁ βŠ” q₁') := begin refine le_antisymm (sup_le (prod_mono le_sup_left le_sup_left) (prod_mono le_sup_right le_sup_right)) _, simp [set_like.le_def], intros xx yy hxx hyy, rcases mem_sup.1 hxx with ⟨x, hx, x', hx', rfl⟩, rcases mem_sup.1 hyy with ⟨y, hy, y', hy', rfl⟩, refine mem_sup.2 ⟨(x, y), ⟨hx, hy⟩, (x', y'), ⟨hx', hy'⟩, rfl⟩ end end add_comm_monoid section add_comm_group variables [ring R] [add_comm_group M] [module R M] @[simp] lemma span_neg (s : set M) : span R (-s) = span R s := calc span R (-s) = span R ((-linear_map.id : M β†’β‚—[R] M) '' s) : by simp ... = map (-linear_map.id) (span R s) : ((-linear_map.id).map_span _).symm ... = span R s : by simp lemma mem_span_insert' {x y} {s : set M} : x ∈ span R (insert y s) ↔ βˆƒ(a:R), x + a β€’ y ∈ span R s := begin rw mem_span_insert, split, { rintro ⟨a, z, hz, rfl⟩, exact ⟨-a, by simp [hz, add_assoc]⟩ }, { rintro ⟨a, h⟩, exact ⟨-a, _, h, by simp [add_comm, add_left_comm]⟩ } end instance : is_modular_lattice (submodule R M) := ⟨λ x y z xz a ha, begin rw [mem_inf, mem_sup] at ha, rcases ha with ⟨⟨b, hb, c, hc, rfl⟩, haz⟩, rw mem_sup, refine ⟨b, hb, c, mem_inf.2 ⟨hc, _⟩, rfl⟩, rw [← add_sub_cancel c b, add_comm], apply z.sub_mem haz (xz hb), end⟩ end add_comm_group section add_comm_group variables [semiring R] [semiring Rβ‚‚] variables [add_comm_group M] [module R M] [add_comm_group Mβ‚‚] [module Rβ‚‚ Mβ‚‚] variables {τ₁₂ : R β†’+* Rβ‚‚} [ring_hom_surjective τ₁₂] variables {F : Type*} [sc : semilinear_map_class F τ₁₂ M Mβ‚‚] include sc lemma comap_map_eq (f : F) (p : submodule R M) : comap f (map f p) = p βŠ” (linear_map.ker f) := begin refine le_antisymm _ (sup_le (le_comap_map _ _) (comap_mono bot_le)), rintro x ⟨y, hy, e⟩, exact mem_sup.2 ⟨y, hy, x - y, by simpa using sub_eq_zero.2 e.symm, by simp⟩ end lemma comap_map_eq_self {f : F} {p : submodule R M} (h : linear_map.ker f ≀ p) : comap f (map f p) = p := by rw [submodule.comap_map_eq, sup_of_le_left h] omit sc end add_comm_group end submodule namespace linear_map open submodule function section add_comm_group variables [semiring R] [semiring Rβ‚‚] variables [add_comm_group M] [add_comm_group Mβ‚‚] variables [module R M] [module Rβ‚‚ Mβ‚‚] variables {τ₁₂ : R β†’+* Rβ‚‚} [ring_hom_surjective τ₁₂] variables {F : Type*} [sc : semilinear_map_class F τ₁₂ M Mβ‚‚] include R include sc protected lemma map_le_map_iff (f : F) {p p'} : map f p ≀ map f p' ↔ p ≀ p' βŠ” ker f := by rw [map_le_iff_le_comap, submodule.comap_map_eq] theorem map_le_map_iff' {f : F} (hf : ker f = βŠ₯) {p p'} : map f p ≀ map f p' ↔ p ≀ p' := by rw [linear_map.map_le_map_iff, hf, sup_bot_eq] theorem map_injective {f : F} (hf : ker f = βŠ₯) : injective (map f) := Ξ» p p' h, le_antisymm ((map_le_map_iff' hf).1 (le_of_eq h)) ((map_le_map_iff' hf).1 (ge_of_eq h)) theorem map_eq_top_iff {f : F} (hf : range f = ⊀) {p : submodule R M} : p.map f = ⊀ ↔ p βŠ” linear_map.ker f = ⊀ := by simp_rw [← top_le_iff, ← hf, range_eq_map, linear_map.map_le_map_iff] end add_comm_group section variables (R) (M) [semiring R] [add_comm_monoid M] [module R M] /-- Given an element `x` of a module `M` over `R`, the natural map from `R` to scalar multiples of `x`.-/ @[simps] def to_span_singleton (x : M) : R β†’β‚—[R] M := linear_map.id.smul_right x /-- The range of `to_span_singleton x` is the span of `x`.-/ lemma span_singleton_eq_range (x : M) : (R βˆ™ x) = (to_span_singleton R M x).range := submodule.ext $ Ξ» y, by {refine iff.trans _ linear_map.mem_range.symm, exact mem_span_singleton } @[simp] lemma to_span_singleton_one (x : M) : to_span_singleton R M x 1 = x := one_smul _ _ @[simp] lemma to_span_singleton_zero : to_span_singleton R M 0 = 0 := by { ext, simp, } end section add_comm_monoid variables [semiring R] [add_comm_monoid M] [module R M] variables [semiring Rβ‚‚] [add_comm_monoid Mβ‚‚] [module Rβ‚‚ Mβ‚‚] variables {σ₁₂ : R β†’+* Rβ‚‚} /-- If two linear maps are equal on a set `s`, then they are equal on `submodule.span s`. See also `linear_map.eq_on_span'` for a version using `set.eq_on`. -/ lemma eq_on_span {s : set M} {f g : M β†’β‚›β‚—[σ₁₂] Mβ‚‚} (H : set.eq_on f g s) ⦃x⦄ (h : x ∈ span R s) : f x = g x := by apply span_induction h H; simp {contextual := tt} /-- If two linear maps are equal on a set `s`, then they are equal on `submodule.span s`. This version uses `set.eq_on`, and the hidden argument will expand to `h : x ∈ (span R s : set M)`. See `linear_map.eq_on_span` for a version that takes `h : x ∈ span R s` as an argument. -/ lemma eq_on_span' {s : set M} {f g : M β†’β‚›β‚—[σ₁₂] Mβ‚‚} (H : set.eq_on f g s) : set.eq_on f g (span R s : set M) := eq_on_span H /-- If `s` generates the whole module and linear maps `f`, `g` are equal on `s`, then they are equal. -/ lemma ext_on {s : set M} {f g : M β†’β‚›β‚—[σ₁₂] Mβ‚‚} (hv : span R s = ⊀) (h : set.eq_on f g s) : f = g := linear_map.ext (Ξ» x, eq_on_span h (eq_top_iff'.1 hv _)) /-- If the range of `v : ΞΉ β†’ M` generates the whole module and linear maps `f`, `g` are equal at each `v i`, then they are equal. -/ lemma ext_on_range {ΞΉ : Type*} {v : ΞΉ β†’ M} {f g : M β†’β‚›β‚—[σ₁₂] Mβ‚‚} (hv : span R (set.range v) = ⊀) (h : βˆ€i, f (v i) = g (v i)) : f = g := ext_on hv (set.forall_range_iff.2 h) end add_comm_monoid section field variables {K V} [field K] [add_comm_group V] [module K V] noncomputable theory open_locale classical lemma span_singleton_sup_ker_eq_top (f : V β†’β‚—[K] K) {x : V} (hx : f x β‰  0) : (K βˆ™ x) βŠ” f.ker = ⊀ := eq_top_iff.2 (Ξ» y hy, submodule.mem_sup.2 ⟨(f y * (f x)⁻¹) β€’ x, submodule.mem_span_singleton.2 ⟨f y * (f x)⁻¹, rfl⟩, ⟨y - (f y * (f x)⁻¹) β€’ x, by rw [linear_map.mem_ker, f.map_sub, f.map_smul, smul_eq_mul, mul_assoc, inv_mul_cancel hx, mul_one, sub_self], by simp only [add_sub_cancel'_right]⟩⟩) variables (K V) lemma ker_to_span_singleton {x : V} (h : x β‰  0) : (to_span_singleton K V x).ker = βŠ₯ := begin ext c, split, { intros hc, rw submodule.mem_bot, rw mem_ker at hc, by_contra hc', have : x = 0, calc x = c⁻¹ β€’ (c β€’ x) : by rw [← mul_smul, inv_mul_cancel hc', one_smul] ... = c⁻¹ β€’ ((to_span_singleton K V x) c) : rfl ... = 0 : by rw [hc, smul_zero], tauto }, { rw [mem_ker, submodule.mem_bot], intros h, rw h, simp } end end field end linear_map open linear_map namespace linear_equiv section field variables (K V) [field K] [add_comm_group V] [module K V] /-- Given a nonzero element `x` of a vector space `V` over a field `K`, the natural map from `K` to the span of `x`, with invertibility check to consider it as an isomorphism.-/ def to_span_nonzero_singleton (x : V) (h : x β‰  0) : K ≃ₗ[K] (K βˆ™ x) := linear_equiv.trans (linear_equiv.of_injective (linear_map.to_span_singleton K V x) (ker_eq_bot.1 $ linear_map.ker_to_span_singleton K V h)) (linear_equiv.of_eq (to_span_singleton K V x).range (K βˆ™ x) (span_singleton_eq_range K V x).symm) lemma to_span_nonzero_singleton_one (x : V) (h : x β‰  0) : linear_equiv.to_span_nonzero_singleton K V x h 1 = (⟨x, submodule.mem_span_singleton_self x⟩ : K βˆ™ x) := begin apply set_like.coe_eq_coe.mp, have : ↑(to_span_nonzero_singleton K V x h 1) = to_span_singleton K V x 1 := rfl, rw [this, to_span_singleton_one, submodule.coe_mk], end /-- Given a nonzero element `x` of a vector space `V` over a field `K`, the natural map from the span of `x` to `K`.-/ abbreviation coord (x : V) (h : x β‰  0) : (K βˆ™ x) ≃ₗ[K] K := (to_span_nonzero_singleton K V x h).symm lemma coord_self (x : V) (h : x β‰  0) : (coord K V x h) (⟨x, submodule.mem_span_singleton_self x⟩ : K βˆ™ x) = 1 := by rw [← to_span_nonzero_singleton_one K V x h, linear_equiv.symm_apply_apply] end field end linear_equiv
74805452e23aca135cf3a12dddb0e9bbee3ef919
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/linear_algebra/dual.lean
d89b4bf590847629b02f497ebf82151bfdaaab12
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
24,717
lean
/- Copyright (c) 2019 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Fabian GlΓΆckle -/ import linear_algebra.finite_dimensional import linear_algebra.projection /-! # Dual vector spaces The dual space of an R-module M is the R-module of linear maps `M β†’ R`. ## Main definitions * `dual R M` defines the dual space of M over R. * Given a basis for an `R`-module `M`, `basis.to_dual` produces a map from `M` to `dual R M`. * Given families of vectors `e` and `Ξ΅`, `dual_pair e Ξ΅` states that these families have the characteristic properties of a basis and a dual. * `dual_annihilator W` is the submodule of `dual R M` where every element annihilates `W`. ## Main results * `to_dual_equiv` : the linear equivalence between the dual module and primal module, given a finite basis. * `dual_pair.basis` and `dual_pair.eq_dual`: if `e` and `Ξ΅` form a dual pair, `e` is a basis and `Ξ΅` is its dual basis. * `quot_equiv_annihilator`: the quotient by a subspace is isomorphic to its dual annihilator. ## Notation We sometimes use `V'` as local notation for `dual K V`. ## TODO ErdΓΆs-Kaplansky theorem about the dimension of a dual vector space in case of infinite dimension. -/ noncomputable theory namespace module variables (R : Type*) (M : Type*) variables [comm_semiring R] [add_comm_monoid M] [module R M] /-- The dual space of an R-module M is the R-module of linear maps `M β†’ R`. -/ @[derive [add_comm_monoid, module R]] def dual := M β†’β‚—[R] R instance {S : Type*} [comm_ring S] {N : Type*} [add_comm_group N] [module S N] : add_comm_group (dual S N) := by {unfold dual, apply_instance} namespace dual instance : inhabited (dual R M) := by dunfold dual; apply_instance instance : has_coe_to_fun (dual R M) := ⟨_, linear_map.to_fun⟩ /-- Maps a module M to the dual of the dual of M. See `module.erange_coe` and `module.eval_equiv`. -/ def eval : M β†’β‚—[R] (dual R (dual R M)) := linear_map.flip linear_map.id @[simp] lemma eval_apply (v : M) (a : dual R M) : eval R M v a = a v := begin dunfold eval, rw [linear_map.flip_apply, linear_map.id_apply] end variables {R M} {M' : Type*} [add_comm_monoid M'] [module R M'] /-- The transposition of linear maps, as a linear map from `M β†’β‚—[R] M'` to `dual R M' β†’β‚—[R] dual R M`. -/ def transpose : (M β†’β‚—[R] M') β†’β‚—[R] (dual R M' β†’β‚—[R] dual R M) := (linear_map.llcomp R M M' R).flip lemma transpose_apply (u : M β†’β‚—[R] M') (l : dual R M') : transpose u l = l.comp u := rfl variables {M'' : Type*} [add_comm_monoid M''] [module R M''] lemma transpose_comp (u : M' β†’β‚—[R] M'') (v : M β†’β‚—[R] M') : transpose (u.comp v) = (transpose v).comp (transpose u) := rfl end dual end module namespace basis universes u v w open module module.dual submodule linear_map cardinal function variables {R M K V ΞΉ : Type*} section comm_semiring variables [comm_semiring R] [add_comm_monoid M] [module R M] [decidable_eq ΞΉ] variables (b : basis ΞΉ R M) /-- The linear map from a vector space equipped with basis to its dual vector space, taking basis elements to corresponding dual basis elements. -/ def to_dual : M β†’β‚—[R] module.dual R M := b.constr β„• $ Ξ» v, b.constr β„• $ Ξ» w, if w = v then (1 : R) else 0 lemma to_dual_apply (i j : ΞΉ) : b.to_dual (b i) (b j) = if i = j then 1 else 0 := by { erw [constr_basis b, constr_basis b], ac_refl } @[simp] lemma to_dual_total_left (f : ΞΉ β†’β‚€ R) (i : ΞΉ) : b.to_dual (finsupp.total ΞΉ M R b f) (b i) = f i := begin rw [finsupp.total_apply, finsupp.sum, linear_map.map_sum, linear_map.sum_apply], simp_rw [linear_map.map_smul, linear_map.smul_apply, to_dual_apply, smul_eq_mul, mul_boole, finset.sum_ite_eq'], split_ifs with h, { refl }, { rw finsupp.not_mem_support_iff.mp h } end @[simp] lemma to_dual_total_right (f : ΞΉ β†’β‚€ R) (i : ΞΉ) : b.to_dual (b i) (finsupp.total ΞΉ M R b f) = f i := begin rw [finsupp.total_apply, finsupp.sum, linear_map.map_sum], simp_rw [linear_map.map_smul, to_dual_apply, smul_eq_mul, mul_boole, finset.sum_ite_eq], split_ifs with h, { refl }, { rw finsupp.not_mem_support_iff.mp h } end lemma to_dual_apply_left (m : M) (i : ΞΉ) : b.to_dual m (b i) = b.repr m i := by rw [← b.to_dual_total_left, b.total_repr] lemma to_dual_apply_right (i : ΞΉ) (m : M) : b.to_dual (b i) m = b.repr m i := by rw [← b.to_dual_total_right, b.total_repr] lemma coe_to_dual_self (i : ΞΉ) : b.to_dual (b i) = b.coord i := by { ext, apply to_dual_apply_right } /-- `h.to_dual_flip v` is the linear map sending `w` to `h.to_dual w v`. -/ def to_dual_flip (m : M) : (M β†’β‚—[R] R) := b.to_dual.flip m lemma to_dual_flip_apply (m₁ mβ‚‚ : M) : b.to_dual_flip m₁ mβ‚‚ = b.to_dual mβ‚‚ m₁ := rfl lemma to_dual_eq_repr (m : M) (i : ΞΉ) : b.to_dual m (b i) = b.repr m i := b.to_dual_apply_left m i lemma to_dual_eq_equiv_fun [fintype ΞΉ] (m : M) (i : ΞΉ) : b.to_dual m (b i) = b.equiv_fun m i := by rw [b.equiv_fun_apply, to_dual_eq_repr] lemma to_dual_inj (m : M) (a : b.to_dual m = 0) : m = 0 := begin rw [← mem_bot R, ← b.repr.ker, mem_ker, linear_equiv.coe_coe], apply finsupp.ext, intro b, rw [← to_dual_eq_repr, a], refl end theorem to_dual_ker : b.to_dual.ker = βŠ₯ := ker_eq_bot'.mpr b.to_dual_inj theorem to_dual_range [fin : fintype ΞΉ] : b.to_dual.range = ⊀ := begin rw eq_top_iff', intro f, rw linear_map.mem_range, let lin_comb : ΞΉ β†’β‚€ R := finsupp.on_finset fin.elems (Ξ» i, f.to_fun (b i)) _, { use finsupp.total ΞΉ M R b lin_comb, apply b.ext, { intros i, rw [b.to_dual_eq_repr _ i, repr_total b], { refl } } }, { intros a _, apply fin.complete } end end comm_semiring section comm_ring variables [comm_ring R] [add_comm_group M] [module R M] [decidable_eq ΞΉ] variables (b : basis ΞΉ R M) /-- A vector space is linearly equivalent to its dual space. -/ @[simps] def to_dual_equiv [fintype ΞΉ] : M ≃ₗ[R] (dual R M) := linear_equiv.of_bijective b.to_dual b.to_dual_ker b.to_dual_range /-- Maps a basis for `V` to a basis for the dual space. -/ def dual_basis [fintype ΞΉ] : basis ΞΉ R (dual R M) := b.map b.to_dual_equiv -- We use `j = i` to match `basis.repr_self` lemma dual_basis_apply_self [fintype ΞΉ] (i j : ΞΉ) : b.dual_basis i (b j) = if j = i then 1 else 0 := by { convert b.to_dual_apply i j using 2, rw @eq_comm _ j i } lemma total_dual_basis [fintype ΞΉ] (f : ΞΉ β†’β‚€ R) (i : ΞΉ) : finsupp.total ΞΉ (dual R M) R b.dual_basis f (b i) = f i := begin rw [finsupp.total_apply, finsupp.sum_fintype, linear_map.sum_apply], { simp_rw [linear_map.smul_apply, smul_eq_mul, dual_basis_apply_self, mul_boole, finset.sum_ite_eq, if_pos (finset.mem_univ i)] }, { intro, rw zero_smul }, end lemma dual_basis_repr [fintype ΞΉ] (l : dual R M) (i : ΞΉ) : b.dual_basis.repr l i = l (b i) := by rw [← total_dual_basis b, basis.total_repr b.dual_basis l] lemma dual_basis_equiv_fun [fintype ΞΉ] (l : dual R M) (i : ΞΉ) : b.dual_basis.equiv_fun l i = l (b i) := by rw [basis.equiv_fun_apply, dual_basis_repr] lemma dual_basis_apply [fintype ΞΉ] (i : ΞΉ) (m : M) : b.dual_basis i m = b.repr m i := b.to_dual_apply_right i m @[simp] lemma coe_dual_basis [fintype ΞΉ] : ⇑b.dual_basis = b.coord := by { ext i x, apply dual_basis_apply } @[simp] lemma to_dual_to_dual [fintype ΞΉ] : b.dual_basis.to_dual.comp b.to_dual = dual.eval R M := begin refine b.ext (Ξ» i, b.dual_basis.ext (Ξ» j, _)), rw [linear_map.comp_apply, to_dual_apply_left, coe_to_dual_self, ← coe_dual_basis, dual.eval_apply, basis.repr_self, finsupp.single_apply, dual_basis_apply_self] end theorem eval_ker {ΞΉ : Type*} (b : basis ΞΉ R M) : (dual.eval R M).ker = βŠ₯ := begin rw ker_eq_bot', intros m hm, simp_rw [linear_map.ext_iff, dual.eval_apply, zero_apply] at hm, exact (basis.forall_coord_eq_zero_iff _).mp (Ξ» i, hm (b.coord i)) end lemma eval_range {ΞΉ : Type*} [fintype ΞΉ] (b : basis ΞΉ R M) : (eval R M).range = ⊀ := begin classical, rw [← b.to_dual_to_dual, range_comp, b.to_dual_range, map_top, to_dual_range _], apply_instance end /-- A module with a basis is linearly equivalent to the dual of its dual space. -/ def eval_equiv {ΞΉ : Type*} [fintype ΞΉ] (b : basis ΞΉ R M) : M ≃ₗ[R] dual R (dual R M) := linear_equiv.of_bijective (eval R M) b.eval_ker b.eval_range @[simp] lemma eval_equiv_to_linear_map {ΞΉ : Type*} [fintype ΞΉ] (b : basis ΞΉ R M) : (b.eval_equiv).to_linear_map = dual.eval R M := rfl end comm_ring /-- `simp` normal form version of `total_dual_basis` -/ @[simp] lemma total_coord [comm_ring R] [add_comm_group M] [module R M] [fintype ΞΉ] (b : basis ΞΉ R M) (f : ΞΉ β†’β‚€ R) (i : ΞΉ) : finsupp.total ΞΉ (dual R M) R b.coord f (b i) = f i := by { haveI := classical.dec_eq ΞΉ, rw [← coe_dual_basis, total_dual_basis] } -- TODO(jmc): generalize to rings, once `module.rank` is generalized theorem dual_dim_eq [field K] [add_comm_group V] [module K V] [fintype ΞΉ] (b : basis ΞΉ K V) : cardinal.lift (module.rank K V) = module.rank K (dual K V) := begin classical, have := linear_equiv.dim_eq_lift b.to_dual_equiv, simp only [cardinal.lift_umax] at this, rw [this, ← cardinal.lift_umax], apply cardinal.lift_id, end end basis namespace module variables {K V : Type*} variables [field K] [add_comm_group V] [module K V] open module module.dual submodule linear_map cardinal basis finite_dimensional theorem eval_ker : (eval K V).ker = βŠ₯ := by { classical, exact (basis.of_vector_space K V).eval_ker } -- TODO(jmc): generalize to rings, once `module.rank` is generalized theorem dual_dim_eq [finite_dimensional K V] : cardinal.lift (module.rank K V) = module.rank K (dual K V) := (basis.of_vector_space K V).dual_dim_eq lemma erange_coe [finite_dimensional K V] : (eval K V).range = ⊀ := by { classical, exact (basis.of_vector_space K V).eval_range } variables (K V) /-- A vector space is linearly equivalent to the dual of its dual space. -/ def eval_equiv [finite_dimensional K V] : V ≃ₗ[K] dual K (dual K V) := linear_equiv.of_bijective (eval K V) eval_ker (erange_coe) variables {K V} @[simp] lemma eval_equiv_to_linear_map [finite_dimensional K V] : (eval_equiv K V).to_linear_map = dual.eval K V := rfl end module section dual_pair open module variables {R M ΞΉ : Type*} variables [comm_ring R] [add_comm_group M] [module R M] [decidable_eq ΞΉ] /-- `e` and `Ξ΅` have characteristic properties of a basis and its dual -/ @[nolint has_inhabited_instance] structure dual_pair (e : ΞΉ β†’ M) (Ξ΅ : ΞΉ β†’ (dual R M)) := (eval : βˆ€ i j : ΞΉ, Ξ΅ i (e j) = if i = j then 1 else 0) (total : βˆ€ {m : M}, (βˆ€ i, Ξ΅ i m = 0) β†’ m = 0) [finite : βˆ€ m : M, fintype {i | Ξ΅ i m β‰  0}] end dual_pair namespace dual_pair open module module.dual linear_map function variables {R M ΞΉ : Type*} variables [comm_ring R] [add_comm_group M] [module R M] variables {e : ΞΉ β†’ M} {Ξ΅ : ΞΉ β†’ dual R M} /-- The coefficients of `v` on the basis `e` -/ def coeffs [decidable_eq ΞΉ] (h : dual_pair e Ξ΅) (m : M) : ΞΉ β†’β‚€ R := { to_fun := Ξ» i, Ξ΅ i m, support := by { haveI := h.finite m, exact {i : ΞΉ | Ξ΅ i m β‰  0}.to_finset }, mem_support_to_fun := by {intro i, rw set.mem_to_finset, exact iff.rfl } } @[simp] lemma coeffs_apply [decidable_eq ΞΉ] (h : dual_pair e Ξ΅) (m : M) (i : ΞΉ) : h.coeffs m i = Ξ΅ i m := rfl /-- linear combinations of elements of `e`. This is a convenient abbreviation for `finsupp.total _ M R e l` -/ def lc {ΞΉ} (e : ΞΉ β†’ M) (l : ΞΉ β†’β‚€ R) : M := l.sum (Ξ» (i : ΞΉ) (a : R), a β€’ (e i)) lemma lc_def (e : ΞΉ β†’ M) (l : ΞΉ β†’β‚€ R) : lc e l = finsupp.total _ _ _ e l := rfl variables [decidable_eq ΞΉ] (h : dual_pair e Ξ΅) include h lemma dual_lc (l : ΞΉ β†’β‚€ R) (i : ΞΉ) : Ξ΅ i (dual_pair.lc e l) = l i := begin erw linear_map.map_sum, simp only [h.eval, map_smul, smul_eq_mul], rw finset.sum_eq_single i, { simp }, { intros q q_in q_ne, simp [q_ne.symm] }, { intro p_not_in, simp [finsupp.not_mem_support_iff.1 p_not_in] }, end @[simp] lemma coeffs_lc (l : ΞΉ β†’β‚€ R) : h.coeffs (dual_pair.lc e l) = l := by { ext i, rw [h.coeffs_apply, h.dual_lc] } /-- For any m : M n, \sum_{p ∈ Q n} (Ξ΅ p m) β€’ e p = m -/ @[simp] lemma lc_coeffs (m : M) : dual_pair.lc e (h.coeffs m) = m := begin refine eq_of_sub_eq_zero (h.total _), intros i, simp [-sub_eq_add_neg, linear_map.map_sub, h.dual_lc, sub_eq_zero] end /-- `(h : dual_pair e Ξ΅).basis` shows the family of vectors `e` forms a basis. -/ @[simps] def basis : basis ΞΉ R M := basis.of_repr { to_fun := coeffs h, inv_fun := lc e, left_inv := lc_coeffs h, right_inv := coeffs_lc h, map_add' := Ξ» v w, by { ext i, exact (Ξ΅ i).map_add v w }, map_smul' := Ξ» c v, by { ext i, exact (Ξ΅ i).map_smul c v } } @[simp] lemma coe_basis : ⇑h.basis = e := by { ext i, rw basis.apply_eq_iff, ext j, rw [h.basis_repr_apply, coeffs_apply, h.eval, finsupp.single_apply], convert if_congr eq_comm rfl rfl } -- `convert` to get rid of a `decidable_eq` mismatch lemma mem_of_mem_span {H : set ΞΉ} {x : M} (hmem : x ∈ submodule.span R (e '' H)) : βˆ€ i : ΞΉ, Ξ΅ i x β‰  0 β†’ i ∈ H := begin intros i hi, rcases (finsupp.mem_span_image_iff_total _).mp hmem with ⟨l, supp_l, rfl⟩, apply not_imp_comm.mp ((finsupp.mem_supported' _ _).mp supp_l i), rwa [← lc_def, h.dual_lc] at hi end lemma coe_dual_basis [fintype ΞΉ] : ⇑h.basis.dual_basis = Ξ΅ := funext (Ξ» i, h.basis.ext (Ξ» j, by rw [h.basis.dual_basis_apply_self, h.coe_basis, h.eval, if_congr eq_comm rfl rfl])) end dual_pair namespace submodule universes u v w variables {R : Type u} {M : Type v} [comm_ring R] [add_comm_group M] [module R M] variable {W : submodule R M} /-- The `dual_restrict` of a submodule `W` of `M` is the linear map from the dual of `M` to the dual of `W` such that the domain of each linear map is restricted to `W`. -/ def dual_restrict (W : submodule R M) : module.dual R M β†’β‚—[R] module.dual R W := linear_map.dom_restrict' W @[simp] lemma dual_restrict_apply (W : submodule R M) (Ο† : module.dual R M) (x : W) : W.dual_restrict Ο† x = Ο† (x : M) := rfl /-- The `dual_annihilator` of a submodule `W` is the set of linear maps `Ο†` such that `Ο† w = 0` for all `w ∈ W`. -/ def dual_annihilator {R : Type u} {M : Type v} [comm_ring R] [add_comm_group M] [module R M] (W : submodule R M) : submodule R $ module.dual R M := W.dual_restrict.ker @[simp] lemma mem_dual_annihilator (Ο† : module.dual R M) : Ο† ∈ W.dual_annihilator ↔ βˆ€ w ∈ W, Ο† w = 0 := begin refine linear_map.mem_ker.trans _, simp_rw [linear_map.ext_iff, dual_restrict_apply], exact ⟨λ h w hw, h ⟨w, hw⟩, Ξ» h w, h w.1 w.2⟩ end lemma dual_restrict_ker_eq_dual_annihilator (W : submodule R M) : W.dual_restrict.ker = W.dual_annihilator := rfl lemma dual_annihilator_sup_eq_inf_dual_annihilator (U V : submodule R M) : (U βŠ” V).dual_annihilator = U.dual_annihilator βŠ“ V.dual_annihilator := begin ext Ο†, rw [mem_inf, mem_dual_annihilator, mem_dual_annihilator, mem_dual_annihilator], split; intro h, { refine ⟨_, _⟩; intros x hx, exact h x (mem_sup.2 ⟨x, hx, 0, zero_mem _, add_zero _⟩), exact h x (mem_sup.2 ⟨0, zero_mem _, x, hx, zero_add _⟩) }, { simp_rw mem_sup, rintro _ ⟨x, hx, y, hy, rfl⟩, rw [linear_map.map_add, h.1 _ hx, h.2 _ hy, add_zero] } end /-- The pullback of a submodule in the dual space along the evaluation map. -/ def dual_annihilator_comap (Ξ¦ : submodule R (module.dual R M)) : submodule R M := Ξ¦.dual_annihilator.comap (module.dual.eval R M) lemma mem_dual_annihilator_comap_iff {Ξ¦ : submodule R (module.dual R M)} (x : M) : x ∈ Ξ¦.dual_annihilator_comap ↔ βˆ€ Ο† ∈ Ξ¦, (Ο† x : R) = 0 := by simp_rw [dual_annihilator_comap, mem_comap, mem_dual_annihilator, module.dual.eval_apply] end submodule namespace subspace open submodule linear_map universes u v w -- We work in vector spaces because `exists_is_compl` only hold for vector spaces variables {K : Type u} {V : Type v} [field K] [add_comm_group V] [module K V] /-- Given a subspace `W` of `V` and an element of its dual `Ο†`, `dual_lift W Ο†` is the natural extension of `Ο†` to an element of the dual of `V`. That is, `dual_lift W Ο†` sends `w ∈ W` to `Ο† x` and `x` in the complement of `W` to `0`. -/ noncomputable def dual_lift (W : subspace K V) : module.dual K W β†’β‚—[K] module.dual K V := let h := classical.indefinite_description _ W.exists_is_compl in (linear_map.of_is_compl_prod h.2).comp (linear_map.inl _ _ _) variable {W : subspace K V} @[simp] lemma dual_lift_of_subtype {Ο† : module.dual K W} (w : W) : W.dual_lift Ο† (w : V) = Ο† w := by { erw of_is_compl_left_apply _ w, refl } lemma dual_lift_of_mem {Ο† : module.dual K W} {w : V} (hw : w ∈ W) : W.dual_lift Ο† w = Ο† ⟨w, hw⟩ := dual_lift_of_subtype ⟨w, hw⟩ @[simp] lemma dual_restrict_comp_dual_lift (W : subspace K V) : W.dual_restrict.comp W.dual_lift = 1 := by { ext Ο† x, simp } lemma dual_restrict_left_inverse (W : subspace K V) : function.left_inverse W.dual_restrict W.dual_lift := Ξ» x, show W.dual_restrict.comp W.dual_lift x = x, by { rw [dual_restrict_comp_dual_lift], refl } lemma dual_lift_right_inverse (W : subspace K V) : function.right_inverse W.dual_lift W.dual_restrict := W.dual_restrict_left_inverse lemma dual_restrict_surjective : function.surjective W.dual_restrict := W.dual_lift_right_inverse.surjective lemma dual_lift_injective : function.injective W.dual_lift := W.dual_restrict_left_inverse.injective /-- The quotient by the `dual_annihilator` of a subspace is isomorphic to the dual of that subspace. -/ noncomputable def quot_annihilator_equiv (W : subspace K V) : W.dual_annihilator.quotient ≃ₗ[K] module.dual K W := (quot_equiv_of_eq _ _ W.dual_restrict_ker_eq_dual_annihilator).symm.trans $ W.dual_restrict.quot_ker_equiv_of_surjective dual_restrict_surjective /-- The natural isomorphism forom the dual of a subspace `W` to `W.dual_lift.range`. -/ noncomputable def dual_equiv_dual (W : subspace K V) : module.dual K W ≃ₗ[K] W.dual_lift.range := linear_equiv.of_injective _ $ ker_eq_bot.2 dual_lift_injective lemma dual_equiv_dual_def (W : subspace K V) : W.dual_equiv_dual.to_linear_map = W.dual_lift.range_restrict := rfl @[simp] lemma dual_equiv_dual_apply (Ο† : module.dual K W) : W.dual_equiv_dual Ο† = ⟨W.dual_lift Ο†, mem_range.2 βŸ¨Ο†, rfl⟩⟩ := rfl section open_locale classical open finite_dimensional variables {V₁ : Type*} [add_comm_group V₁] [module K V₁] instance [H : finite_dimensional K V] : finite_dimensional K (module.dual K V) := linear_equiv.finite_dimensional (basis.of_vector_space K V).to_dual_equiv variables [finite_dimensional K V] [finite_dimensional K V₁] @[simp] lemma dual_finrank_eq : finrank K (module.dual K V) = finrank K V := linear_equiv.finrank_eq (basis.of_vector_space K V).to_dual_equiv.symm /-- The quotient by the dual is isomorphic to its dual annihilator. -/ noncomputable def quot_dual_equiv_annihilator (W : subspace K V) : W.dual_lift.range.quotient ≃ₗ[K] W.dual_annihilator := linear_equiv.quot_equiv_of_quot_equiv $ linear_equiv.trans W.quot_annihilator_equiv W.dual_equiv_dual /-- The quotient by a subspace is isomorphic to its dual annihilator. -/ noncomputable def quot_equiv_annihilator (W : subspace K V) : W.quotient ≃ₗ[K] W.dual_annihilator := begin refine linear_equiv.trans _ W.quot_dual_equiv_annihilator, refine linear_equiv.quot_equiv_of_equiv _ (basis.of_vector_space K V).to_dual_equiv, exact (basis.of_vector_space K W).to_dual_equiv.trans W.dual_equiv_dual end open finite_dimensional @[simp] lemma finrank_dual_annihilator_comap_eq {Ξ¦ : subspace K (module.dual K V)} : finrank K Ξ¦.dual_annihilator_comap = finrank K Ξ¦.dual_annihilator := begin rw [submodule.dual_annihilator_comap, ← module.eval_equiv_to_linear_map], exact linear_equiv.finrank_eq (linear_equiv.of_submodule' _ _), end lemma finrank_add_finrank_dual_annihilator_comap_eq (W : subspace K (module.dual K V)) : finrank K W + finrank K W.dual_annihilator_comap = finrank K V := begin rw [finrank_dual_annihilator_comap_eq, W.quot_equiv_annihilator.finrank_eq.symm, add_comm, submodule.finrank_quotient_add_finrank, subspace.dual_finrank_eq], end end end subspace variables {R : Type*} [comm_ring R] {M₁ : Type*} {Mβ‚‚ : Type*} variables [add_comm_group M₁] [module R M₁] [add_comm_group Mβ‚‚] [module R Mβ‚‚] open module /-- Given a linear map `f : M₁ β†’β‚—[R] Mβ‚‚`, `f.dual_map` is the linear map between the dual of `Mβ‚‚` and `M₁` such that it maps the functional `Ο†` to `Ο† ∘ f`. -/ def linear_map.dual_map (f : M₁ β†’β‚—[R] Mβ‚‚) : dual R Mβ‚‚ β†’β‚—[R] dual R M₁ := linear_map.lcomp R R f @[simp] lemma linear_map.dual_map_apply (f : M₁ β†’β‚—[R] Mβ‚‚) (g : dual R Mβ‚‚) (x : M₁) : f.dual_map g x = g (f x) := linear_map.lcomp_apply f g x @[simp] lemma linear_map.dual_map_id : (linear_map.id : M₁ β†’β‚—[R] M₁).dual_map = linear_map.id := by { ext, refl } lemma linear_map.dual_map_comp_dual_map {M₃ : Type*} [add_comm_group M₃] [module R M₃] (f : M₁ β†’β‚—[R] Mβ‚‚) (g : Mβ‚‚ β†’β‚—[R] M₃) : f.dual_map.comp g.dual_map = (g.comp f).dual_map := rfl /-- The `linear_equiv` version of `linear_map.dual_map`. -/ def linear_equiv.dual_map (f : M₁ ≃ₗ[R] Mβ‚‚) : dual R Mβ‚‚ ≃ₗ[R] dual R M₁ := { inv_fun := f.symm.to_linear_map.dual_map, left_inv := begin intro Ο†, ext x, simp only [linear_map.dual_map_apply, linear_equiv.coe_to_linear_map, linear_map.to_fun_eq_coe, linear_equiv.apply_symm_apply] end, right_inv := begin intro Ο†, ext x, simp only [linear_map.dual_map_apply, linear_equiv.coe_to_linear_map, linear_map.to_fun_eq_coe, linear_equiv.symm_apply_apply] end, .. f.to_linear_map.dual_map } @[simp] lemma linear_equiv.dual_map_apply (f : M₁ ≃ₗ[R] Mβ‚‚) (g : dual R Mβ‚‚) (x : M₁) : f.dual_map g x = g (f x) := linear_map.lcomp_apply f g x @[simp] lemma linear_equiv.dual_map_refl : (linear_equiv.refl R M₁).dual_map = linear_equiv.refl R (dual R M₁) := by { ext, refl } @[simp] lemma linear_equiv.dual_map_symm {f : M₁ ≃ₗ[R] Mβ‚‚} : (linear_equiv.dual_map f).symm = linear_equiv.dual_map f.symm := rfl lemma linear_equiv.dual_map_trans {M₃ : Type*} [add_comm_group M₃] [module R M₃] (f : M₁ ≃ₗ[R] Mβ‚‚) (g : Mβ‚‚ ≃ₗ[R] M₃) : g.dual_map.trans f.dual_map = (f.trans g).dual_map := rfl namespace linear_map variable (f : M₁ β†’β‚—[R] Mβ‚‚) lemma ker_dual_map_eq_dual_annihilator_range : f.dual_map.ker = f.range.dual_annihilator := begin ext Ο†, split; intro hΟ†, { rw mem_ker at hΟ†, rw submodule.mem_dual_annihilator, rintro y ⟨x, rfl⟩, rw [← dual_map_apply, hΟ†, zero_apply] }, { ext x, rw dual_map_apply, rw submodule.mem_dual_annihilator at hΟ†, exact hΟ† (f x) ⟨x, rfl⟩ } end lemma range_dual_map_le_dual_annihilator_ker : f.dual_map.range ≀ f.ker.dual_annihilator := begin rintro _ ⟨ψ, rfl⟩, simp_rw [submodule.mem_dual_annihilator, mem_ker], rintro x hx, rw [dual_map_apply, hx, map_zero] end section finite_dimensional variables {K : Type*} [field K] {V₁ : Type*} {Vβ‚‚ : Type*} variables [add_comm_group V₁] [module K V₁] [add_comm_group Vβ‚‚] [module K Vβ‚‚] open finite_dimensional variable [finite_dimensional K Vβ‚‚] @[simp] lemma finrank_range_dual_map_eq_finrank_range (f : V₁ β†’β‚—[K] Vβ‚‚) : finrank K f.dual_map.range = finrank K f.range := begin have := submodule.finrank_quotient_add_finrank f.range, rw [(subspace.quot_equiv_annihilator f.range).finrank_eq, ← ker_dual_map_eq_dual_annihilator_range] at this, conv_rhs at this { rw ← subspace.dual_finrank_eq }, refine add_left_injective (finrank K f.dual_map.ker) _, change _ + _ = _ + _, rw [finrank_range_add_finrank_ker f.dual_map, add_comm, this], end lemma range_dual_map_eq_dual_annihilator_ker [finite_dimensional K V₁] (f : V₁ β†’β‚—[K] Vβ‚‚) : f.dual_map.range = f.ker.dual_annihilator := begin refine eq_of_le_of_finrank_eq f.range_dual_map_le_dual_annihilator_ker _, have := submodule.finrank_quotient_add_finrank f.ker, rw (subspace.quot_equiv_annihilator f.ker).finrank_eq at this, refine add_left_injective (finrank K f.ker) _, simp_rw [this, finrank_range_dual_map_eq_finrank_range], exact finrank_range_add_finrank_ker f, end end finite_dimensional end linear_map
7bf4e45c52715a76fbae151efcd019e4160eec96
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/computability/language.lean
da3e23981ef011b1cf67829972decd95e53827f3
[ "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
6,747
lean
/- Copyright (c) 2020 Fox Thomson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Fox Thomson -/ import algebra.group_power.order import data.list.join import data.set.lattice /-! # Languages This file contains the definition and operations on formal languages over an alphabet. Note strings are implemented as lists over the alphabet. The operations in this file define a [Kleene algebra](https://en.wikipedia.org/wiki/Kleene_algebra) over the languages. -/ universes u v variables {Ξ± : Type u} /-- A language is a set of strings over an alphabet. -/ @[derive [has_mem (list Ξ±), has_singleton (list Ξ±), has_insert (list Ξ±), complete_boolean_algebra]] def language (Ξ±) := set (list Ξ±) namespace language local attribute [reducible] language /-- Zero language has no elements. -/ instance : has_zero (language Ξ±) := ⟨(βˆ… : set _)⟩ /-- `1 : language Ξ±` contains only one element `[]`. -/ instance : has_one (language Ξ±) := ⟨{[]}⟩ instance : inhabited (language Ξ±) := ⟨0⟩ /-- The sum of two languages is the union of -/ instance : has_add (language Ξ±) := ⟨set.union⟩ instance : has_mul (language Ξ±) := ⟨set.image2 (++)⟩ lemma zero_def : (0 : language Ξ±) = (βˆ… : set _) := rfl lemma one_def : (1 : language Ξ±) = {[]} := rfl lemma add_def (l m : language Ξ±) : l + m = l βˆͺ m := rfl lemma mul_def (l m : language Ξ±) : l * m = set.image2 (++) l m := rfl /-- The star of a language `L` is the set of all strings which can be written by concatenating strings from `L`. -/ def star (l : language Ξ±) : language Ξ± := { x | βˆƒ S : list (list Ξ±), x = S.join ∧ βˆ€ y ∈ S, y ∈ l} lemma star_def (l : language Ξ±) : l.star = { x | βˆƒ S : list (list Ξ±), x = S.join ∧ βˆ€ y ∈ S, y ∈ l} := rfl @[simp] lemma mem_one (x : list Ξ±) : x ∈ (1 : language Ξ±) ↔ x = [] := by refl @[simp] lemma mem_add (l m : language Ξ±) (x : list Ξ±) : x ∈ l + m ↔ x ∈ l ∨ x ∈ m := by simp [add_def] lemma mem_mul (l m : language Ξ±) (x : list Ξ±) : x ∈ l * m ↔ βˆƒ a b, a ∈ l ∧ b ∈ m ∧ a ++ b = x := by simp [mul_def] lemma mem_star (l : language Ξ±) (x : list Ξ±) : x ∈ l.star ↔ βˆƒ S : list (list Ξ±), x = S.join ∧ βˆ€ y ∈ S, y ∈ l := iff.rfl instance : semiring (language Ξ±) := { add := (+), add_assoc := set.union_assoc, zero := 0, zero_add := set.empty_union, add_zero := set.union_empty, add_comm := set.union_comm, mul := (*), mul_assoc := Ξ» l m n, by simp only [mul_def, set.image2_image2_left, set.image2_image2_right, list.append_assoc], zero_mul := by simp [zero_def, mul_def], mul_zero := by simp [zero_def, mul_def], one := 1, one_mul := Ξ» l, by simp [mul_def, one_def], mul_one := Ξ» l, by simp [mul_def, one_def], left_distrib := Ξ» l m n, by simp only [mul_def, add_def, set.image2_union_right], right_distrib := Ξ» l m n, by simp only [mul_def, add_def, set.image2_union_left] } @[simp] lemma add_self (l : language Ξ±) : l + l = l := sup_idem lemma star_def_nonempty (l : language Ξ±) : l.star = {x | βˆƒ S : list (list Ξ±), x = S.join ∧ βˆ€ y ∈ S, y ∈ l ∧ y β‰  []} := begin ext x, split, { rintro ⟨S, rfl, h⟩, refine ⟨S.filter (Ξ» l, Β¬list.empty l), by simp, Ξ» y hy, _⟩, rw [list.mem_filter, list.empty_iff_eq_nil] at hy, exact ⟨h y hy.1, hy.2⟩ }, { rintro ⟨S, hx, h⟩, exact ⟨S, hx, Ξ» y hy, (h y hy).1⟩ } end lemma le_iff (l m : language Ξ±) : l ≀ m ↔ l + m = m := sup_eq_right.symm lemma le_mul_congr {l₁ lβ‚‚ m₁ mβ‚‚ : language Ξ±} : l₁ ≀ m₁ β†’ lβ‚‚ ≀ mβ‚‚ β†’ l₁ * lβ‚‚ ≀ m₁ * mβ‚‚ := begin intros h₁ hβ‚‚ x hx, simp only [mul_def, exists_and_distrib_left, set.mem_image2, set.image_prod] at hx ⊒, tauto end lemma le_add_congr {l₁ lβ‚‚ m₁ mβ‚‚ : language Ξ±} : l₁ ≀ m₁ β†’ lβ‚‚ ≀ mβ‚‚ β†’ l₁ + lβ‚‚ ≀ m₁ + mβ‚‚ := sup_le_sup lemma mem_supr {ΞΉ : Sort v} {l : ΞΉ β†’ language Ξ±} {x : list Ξ±} : x ∈ (⨆ i, l i) ↔ βˆƒ i, x ∈ l i := set.mem_Union lemma supr_mul {ΞΉ : Sort v} (l : ΞΉ β†’ language Ξ±) (m : language Ξ±) : (⨆ i, l i) * m = ⨆ i, l i * m := set.image2_Union_left _ _ _ lemma mul_supr {ΞΉ : Sort v} (l : ΞΉ β†’ language Ξ±) (m : language Ξ±) : m * (⨆ i, l i) = ⨆ i, m * l i := set.image2_Union_right _ _ _ lemma supr_add {ΞΉ : Sort v} [nonempty ΞΉ] (l : ΞΉ β†’ language Ξ±) (m : language Ξ±) : (⨆ i, l i) + m = ⨆ i, l i + m := supr_sup lemma add_supr {ΞΉ : Sort v} [nonempty ΞΉ] (l : ΞΉ β†’ language Ξ±) (m : language Ξ±) : m + (⨆ i, l i) = ⨆ i, m + l i := sup_supr lemma mem_pow {l : language Ξ±} {x : list Ξ±} {n : β„•} : x ∈ l ^ n ↔ βˆƒ S : list (list Ξ±), x = S.join ∧ S.length = n ∧ βˆ€ y ∈ S, y ∈ l := begin induction n with n ihn generalizing x, { simp only [mem_one, pow_zero, list.length_eq_zero], split, { rintro rfl, exact ⟨[], rfl, rfl, Ξ» y h, h.elim⟩ }, { rintro ⟨_, rfl, rfl, _⟩, refl } }, { simp only [pow_succ, mem_mul, ihn], split, { rintro ⟨a, b, ha, ⟨S, rfl, rfl, hS⟩, rfl⟩, exact ⟨a :: S, rfl, rfl, list.forall_mem_cons.2 ⟨ha, hS⟩⟩ }, { rintro ⟨_|⟨a, S⟩, rfl, hn, hS⟩; cases hn, rw list.forall_mem_cons at hS, exact ⟨a, _, hS.1, ⟨S, rfl, rfl, hS.2⟩, rfl⟩ } } end lemma star_eq_supr_pow (l : language Ξ±) : l.star = ⨆ i : β„•, l ^ i := begin ext x, simp only [mem_star, mem_supr, mem_pow], split, { rintro ⟨S, rfl, hS⟩, exact ⟨_, S, rfl, rfl, hS⟩ }, { rintro ⟨_, S, rfl, rfl, hS⟩, exact ⟨S, rfl, hS⟩ } end lemma mul_self_star_comm (l : language Ξ±) : l.star * l = l * l.star := by simp only [star_eq_supr_pow, mul_supr, supr_mul, ← pow_succ, ← pow_succ'] @[simp] lemma one_add_self_mul_star_eq_star (l : language Ξ±) : 1 + l * l.star = l.star := begin simp only [star_eq_supr_pow, mul_supr, ← pow_succ, ← pow_zero l], exact sup_supr_nat_succ _ end @[simp] lemma one_add_star_mul_self_eq_star (l : language Ξ±) : 1 + l.star * l = l.star := by rw [mul_self_star_comm, one_add_self_mul_star_eq_star] lemma star_mul_le_right_of_mul_le_right (l m : language Ξ±) : l * m ≀ m β†’ l.star * m ≀ m := begin intro h, rw [star_eq_supr_pow, supr_mul], refine supr_le _, intro n, induction n with n ih, { simp }, rw [pow_succ', mul_assoc (l^n) l m], exact le_trans (le_mul_congr (le_refl _) h) ih, end lemma star_mul_le_left_of_mul_le_left (l m : language Ξ±) : m * l ≀ m β†’ m * l.star ≀ m := begin intro h, rw [star_eq_supr_pow, mul_supr], refine supr_le _, intro n, induction n with n ih, { simp }, rw [pow_succ, ←mul_assoc m l (l^n)], exact le_trans (le_mul_congr h (le_refl _)) ih end end language
17ccf4b2387a994e651bb976f473799735a7e9c8
624f6f2ae8b3b1adc5f8f67a365c51d5126be45a
/tests/lean/run/meta2.lean
092de79beece71870661eaae814cddf7cb221710
[ "Apache-2.0" ]
permissive
mhuisi/lean4
28d35a4febc2e251c7f05492e13f3b05d6f9b7af
dda44bc47f3e5d024508060dac2bcb59fd12e4c0
refs/heads/master
1,621,225,489,283
1,585,142,689,000
1,585,142,689,000
250,590,438
0
2
Apache-2.0
1,602,443,220,000
1,585,327,814,000
C
UTF-8
Lean
false
false
16,649
lean
import Init.Lean.Meta open Lean open Lean.Meta set_option trace.Meta true set_option trace.Meta.isDefEq.step false set_option trace.Meta.isDefEq.delta false set_option trace.Meta.isDefEq.assign false def print (msg : MessageData) : MetaM Unit := trace! `Meta.debug msg def check (x : MetaM Bool) : MetaM Unit := unlessM x $ throw $ Exception.other "check failed" def getAssignment (m : Expr) : MetaM Expr := do v? ← getExprMVarAssignment? m.mvarId!; match v? with | some v => pure v | none => throw $ Exception.other "metavariable is not assigned" def nat := mkConst `Nat def boolE := mkConst `Bool def succ := mkConst `Nat.succ def zero := mkConst `Nat.zero def add := mkConst `Nat.add def io := mkConst `IO def type := mkSort levelOne def tst1 : MetaM Unit := do print "----- tst1 -----"; mvar ← mkFreshExprMVar nat; check $ isExprDefEq mvar (mkNatLit 10); check $ isExprDefEq mvar (mkNatLit 10); pure () #eval tst1 def tst2 : MetaM Unit := do print "----- tst2 -----"; mvar ← mkFreshExprMVar nat; check $ isExprDefEq (mkApp succ mvar) (mkApp succ (mkNatLit 10)); check $ isExprDefEq mvar (mkNatLit 10); pure () #eval tst2 def tst3 : MetaM Unit := do print "----- tst3 -----"; let t := mkLambda `x BinderInfo.default nat $ mkBVar 0; mvar ← mkFreshExprMVar (mkForall `x BinderInfo.default nat nat); lambdaTelescope t $ fun xs _ => do { let x := xs.get! 0; check $ isExprDefEq (mkApp mvar x) (mkAppN add #[x, mkAppN add #[mkNatLit 10, x]]); pure () }; v ← getAssignment mvar; print v; pure () #eval tst3 def tst4 : MetaM Unit := do print "----- tst4 -----"; let t := mkLambda `x BinderInfo.default nat $ mkBVar 0; lambdaTelescope t $ fun xs _ => do { let x := xs.get! 0; mvar ← mkFreshExprMVar (mkForall `x BinderInfo.default nat nat); -- the following `isExprDefEq` fails because `x` is also in the context of `mvar` check $ not <$> isExprDefEq (mkApp mvar x) (mkAppN add #[x, mkAppN add #[mkNatLit 10, x]]); check $ approxDefEq $ isExprDefEq (mkApp mvar x) (mkAppN add #[x, mkAppN add #[mkNatLit 10, x]]); v ← getAssignment mvar; print v; pure () }; pure () #eval tst4 def mkAppC (c : Name) (xs : Array Expr) : MetaM Expr := do r ← mkAppM c xs; check r; pure r def mkProd (a b : Expr) : MetaM Expr := mkAppC `Prod #[a, b] def mkPair (a b : Expr) : MetaM Expr := mkAppC `Prod.mk #[a, b] def mkFst (s : Expr) : MetaM Expr := mkAppC `Prod.fst #[s] def mkSnd (s : Expr) : MetaM Expr := mkAppC `Prod.snd #[s] def mkId (a : Expr) : MetaM Expr := mkAppC `id #[a] def tst5 : MetaM Unit := do print "----- tst5 -----"; p₁ ← mkPair (mkNatLit 1) (mkNatLit 2); x ← mkFst p₁; print x; v ← whnf x; print v; v ← withTransparency TransparencyMode.reducible $ whnf x; print v; x ← mkId x; print x; prod ← mkProd nat nat; m ← mkFreshExprMVar prod; y ← mkFst m; check $ isExprDefEq y x; print y #eval tst5 def tst6 : MetaM Unit := do print "----- tst6 -----"; withLocalDecl `x nat BinderInfo.default $ fun x => do m ← mkFreshExprMVar nat; let t := mkAppN add #[m, mkNatLit 4]; let s := mkAppN add #[x, mkNatLit 3]; check $ not <$> isExprDefEq t s; let s := mkAppN add #[x, mkNatLit 6]; check $ isExprDefEq t s; v ← getAssignment m; check $ pure $ v == mkAppN add #[x, mkNatLit 2]; print v; m ← mkFreshExprMVar nat; let t := mkAppN add #[m, mkNatLit 4]; let s := mkNatLit 3; check $ not <$> isExprDefEq t s; let s := mkNatLit 10; check $ isExprDefEq t s; v ← getAssignment m; check $ pure $ v == mkNatLit 6; print v; pure () #eval tst6 def mkArrow (d b : Expr) : Expr := mkForall `_ BinderInfo.default d b def tst7 : MetaM Unit := do print "----- tst7 -----"; withLocalDecl `x type BinderInfo.default $ fun x => do m1 ← mkFreshExprMVar (mkArrow type type); m2 ← mkFreshExprMVar type; let t := mkApp io x; -- we need to use foApprox to solve `?m1 ?m2 =?= IO x` check $ not <$> isDefEq (mkApp m1 m2) t; check $ approxDefEq $ isDefEq (mkApp m1 m2) t; v ← getAssignment m1; check $ pure $ v == io; v ← getAssignment m2; check $ pure $ v == x; pure () #eval tst7 def tst8 : MetaM Unit := do print "----- tst8 -----"; let add := mkAppN (mkConst `HasAdd.add [levelOne]) #[nat, mkConst `Nat.HasAdd]; let t := mkAppN add #[mkNatLit 2, mkNatLit 3]; t ← withTransparency TransparencyMode.reducible $ whnf t; print t; t ← whnf t; print t; t ← reduce t; print t; pure () #eval tst8 def tst9 : MetaM Unit := do print "----- tst9 -----"; env ← getEnv; print (toString $ Lean.isReducible env `Prod.fst); print (toString $ Lean.isReducible env `HasAdd.add); pure () #eval tst9 def tst10 : MetaM Unit := do print "----- tst10 -----"; t ← withLocalDecl `x nat BinderInfo.default $ fun x => do { let b := mkAppN add #[x, mkAppN add #[mkNatLit 2, mkNatLit 3]]; mkLambda #[x] b }; print t; t ← reduce t; print t; pure () #eval tst10 def tst11 : MetaM Unit := do print "----- tst11 -----"; check $ isType nat; check $ isType (mkArrow nat nat); check $ not <$> isType add; check $ not <$> isType (mkNatLit 1); withLocalDecl `x nat BinderInfo.default $ fun x => do { check $ not <$> isType x; check $ not <$> (mkLambda #[x] x >>= isType); check $ not <$> (mkLambda #[x] nat >>= isType); t ← mkEq x (mkNatLit 0) >>= mkForall #[x]; print t; check $ isType t; pure () }; pure () #eval tst11 def tst12 : MetaM Unit := do print "----- tst12 -----"; withLocalDecl `x nat BinderInfo.default $ fun x => do { t ← mkEqRefl x >>= mkLambda #[x]; print t; type ← inferType t; print type; isProofQuick t >>= fun b => print (toString b); isProofQuick nat >>= fun b => print (toString b); isProofQuick type >>= fun b => print (toString b); pure () }; pure () #eval tst12 def tst13 : MetaM Unit := do print "----- tst13 -----"; m₁ ← mkFreshExprMVar (mkArrow type type); mβ‚‚ ← mkFreshExprMVar (mkApp m₁ nat); t ← mkId mβ‚‚; print t; r ← abstractMVars t; print r.expr; (_, _, e) ← openAbstractMVarsResult r; print e; pure () def mkDecEq (type : Expr) : MetaM Expr := mkAppC `DecidableEq #[type] def mkStateM (Οƒ : Expr) : MetaM Expr := mkAppC `StateM #[Οƒ] def mkMonad (m : Expr) : MetaM Expr := mkAppC `Monad #[m] def mkMonadState (Οƒ m : Expr) : MetaM Expr := mkAppC `MonadState #[Οƒ, m] def mkHasAdd (a : Expr) : MetaM Expr := mkAppC `HasAdd #[a] def mkHasToString (a : Expr) : MetaM Expr := mkAppC `HasToString #[a] def tst14 : MetaM Unit := do print "----- tst14 -----"; stateM ← mkStateM nat; print stateM; monad ← mkMonad stateM; globalInsts ← getGlobalInstances; insts ← globalInsts.getUnify monad; print insts; pure () #eval tst14 def tst15 : MetaM Unit := do print "----- tst15 -----"; inst ← mkHasAdd nat; r ← synthInstance inst; print r; pure () #eval tst15 def tst16 : MetaM Unit := do print "----- tst16 -----"; prod ← mkProd nat nat; inst ← mkHasToString prod; print inst; r ← synthInstance inst; print r; pure () #eval tst16 def tst17 : MetaM Unit := do print "----- tst17 -----"; prod ← mkProd nat nat; prod ← mkProd boolE prod; inst ← mkHasToString prod; print inst; r ← synthInstance inst; print r; pure () #eval tst17 def tst18 : MetaM Unit := do print "----- tst18 -----"; decEqNat ← mkDecEq nat; r ← synthInstance decEqNat; print r; pure () #eval tst18 def tst19 : MetaM Unit := do print "----- tst19 -----"; stateM ← mkStateM nat; print stateM; monad ← mkMonad stateM; print monad; r ← synthInstance monad; print r; pure () #eval tst19 def tst20 : MetaM Unit := do print "----- tst20 -----"; stateM ← mkStateM nat; print stateM; monadState ← mkMonadState nat stateM; print monadState; r ← synthInstance monadState; print r; pure () #eval tst20 def tst21 : MetaM Unit := do print "----- tst21 -----"; withLocalDeclD `x nat $ fun x => do withLocalDeclD `y nat $ fun y => do withLocalDeclD `z nat $ fun z => do eq₁ ← mkEq x y; eqβ‚‚ ← mkEq y z; withLocalDeclD `h₁ eq₁ $ fun h₁ => do withLocalDeclD `hβ‚‚ eqβ‚‚ $ fun hβ‚‚ => do h ← mkEqTrans h₁ hβ‚‚; h ← mkEqSymm h; h ← mkCongrArg succ h; hβ‚‚ ← mkEqRefl succ; h ← mkCongr hβ‚‚ h; t ← inferType h; check h; print h; print t; h ← mkCongrFun hβ‚‚ x; t ← inferType h; check h; print t; pure () #eval tst21 def tst22 : MetaM Unit := do print "----- tst22 -----"; withLocalDeclD `x nat $ fun x => do withLocalDeclD `y nat $ fun y => do t ← mkAppC `HasAdd.add #[x, y]; print t; t ← mkAppC `HasAdd.add #[y, x]; print t; t ← mkAppC `HasToString.toString #[x]; print t; pure () #eval tst22 def test1 : Nat := (fun x y => x + y) 0 1 def tst23 : MetaM Unit := do print "----- tst23 -----"; cinfo ← getConstInfo `test1; let v := cinfo.value?.get!; print v; print v.headBeta #eval tst23 def tst24 : MetaM Unit := do print "----- tst24 -----"; m1 ← mkFreshExprMVar (mkArrow nat (mkArrow type type)); m2 ← mkFreshExprMVar type; zero ← mkAppM `HasZero.zero #[nat]; idNat ← mkAppM `Id #[nat]; let lhs := mkAppB m1 zero m2; print zero; print idNat; print lhs; check $ fullApproxDefEq $ isDefEq lhs idNat; pure () #eval tst24 def tst25 : MetaM Unit := do print "----- tst25 -----"; withLocalDecl `Ξ± type BinderInfo.default $ fun Ξ± => withLocalDecl `Ξ² type BinderInfo.default $ fun Ξ² => withLocalDecl `Οƒ type BinderInfo.default $ fun Οƒ => do { (t1, n) ← mkForallUsedOnly #[Ξ±, Ξ², Οƒ] $ mkArrow Ξ± Ξ²; print t1; check $ pure $ n == 2; (t2, n) ← mkForallUsedOnly #[Ξ±, Ξ², Οƒ] $ mkArrow Ξ± (mkArrow Ξ² Οƒ); check $ pure $ n == 3; print t2; (t3, n) ← mkForallUsedOnly #[Ξ±, Ξ², Οƒ] $ Ξ±; check $ pure $ n == 1; print t3; (t4, n) ← mkForallUsedOnly #[Ξ±, Ξ², Οƒ] $ Οƒ; check $ pure $ n == 1; print t4; (t5, n) ← mkForallUsedOnly #[Ξ±, Ξ², Οƒ] $ nat; check $ pure $ n == 0; print t5; pure () }; pure () #eval tst25 def tst26 : MetaM Unit := do print "----- tst26 -----"; m1 ← mkFreshExprMVar (mkArrow nat nat); m2 ← mkFreshExprMVar nat; m3 ← mkFreshExprMVar nat; check $ approxDefEq $ isDefEq (mkApp m1 m2) m3; check $ do { b ← isExprMVarAssigned $ m1.mvarId!; pure (!b) }; check $ isExprMVarAssigned $ m3.mvarId!; pure () section set_option ppOld false #eval tst26 end section set_option trace.Meta.isDefEq.step true set_option trace.Meta.isDefEq.delta true set_option trace.Meta.isDefEq.assign true def tst27 : MetaM Unit := do print "----- tst27 -----"; m ← mkFreshExprMVar nat; check $ isDefEq (mkNatLit 1) (mkApp (mkConst `Nat.succ) m); pure () #eval tst27 end def tst28 : MetaM Unit := do print "----- tst28 -----"; withLocalDecl `x nat BinderInfo.default $ fun x => withLocalDecl `y nat BinderInfo.default $ fun y => withLocalDecl `z nat BinderInfo.default $ fun z => do t1 ← mkAppM `HasAdd.add #[x, y]; t1 ← mkAppM `HasAdd.add #[x, t1]; t1 ← mkAppM `HasAdd.add #[t1, t1]; t2 ← mkAppM `HasAdd.add #[z, y]; t3 ← mkAppM `Eq #[t2, t1]; t3 ← mkForall #[z] t3; m ← mkFreshExprMVar nat; p ← mkAppM `HasAdd.add #[x, m]; print t3; r ← kabstract t3 p; print r; p ← mkAppM `HasAdd.add #[x, y]; r ← kabstract t3 p; print r; pure () #eval tst28 def norm : Level β†’ Level := @Lean.Level.normalize def tst29 : MetaM Unit := do print "----- tst29 -----"; let u := mkLevelParam `u; let v := mkLevelParam `v; let u1 := mkLevelSucc u; let m := mkLevelMax levelOne u1; print (norm m); check $ pure $ norm m == u1; let m := mkLevelMax u1 levelOne; print (norm m); check $ pure $ norm m == u1; let m := mkLevelMax (mkLevelMax levelOne (mkLevelSucc u1)) (mkLevelSucc levelOne); check $ pure $ norm m == mkLevelSucc u1; print m; print (norm m); let m := mkLevelMax (mkLevelMax (mkLevelSucc (mkLevelSucc u1)) (mkLevelSucc u1)) (mkLevelSucc levelOne); print m; print (norm m); check $ pure $ norm m == mkLevelSucc (mkLevelSucc u1); let m := mkLevelMax (mkLevelMax (mkLevelSucc v) (mkLevelSucc u1)) (mkLevelSucc levelOne); print m; print (norm m); pure () #eval tst29 def tst30 : MetaM Unit := do print "----- tst30 -----"; m1 ← mkFreshExprMVar nat; m2 ← mkFreshExprMVar (mkArrow nat nat); withLocalDecl `x nat BinderInfo.default $ fun x => do let t := mkApp succ $ mkApp m2 x; print t; check $ approxDefEq $ isDefEq m1 t; r ← instantiateMVars m1; print r; r ← instantiateMVars m2; print r; pure () #eval tst30 def tst31 : MetaM Unit := do print "----- tst31 -----"; m ← mkFreshExprMVar nat; let t := mkLet `x nat zero m; print t; check $ isDefEq t m; pure () def tst32 : MetaM Unit := do print "----- tst32 -----"; withLocalDecl `a nat BinderInfo.default $ fun a => do withLocalDecl `b nat BinderInfo.default $ fun b => do aeqb ← mkEq a b; withLocalDecl `h2 aeqb BinderInfo.default $ fun h2 => do t ← mkEq (mkApp2 add a a) a; print t; let motive := Lean.mkLambda `x BinderInfo.default nat (mkApp3 (mkConst `Eq [levelOne]) nat (mkApp2 add a (mkBVar 0)) a); withLocalDecl `h1 t BinderInfo.default $ fun h1 => do r ← mkEqNDRec motive h1 h2; print r; rType ← inferType r >>= whnf; print rType; check r; pure () #eval tst32 def tst33 : MetaM Unit := do print "----- tst33 -----"; withLocalDecl `a nat BinderInfo.default $ fun a => do withLocalDecl `b nat BinderInfo.default $ fun b => do aeqb ← mkEq a b; withLocalDecl `h2 aeqb BinderInfo.default $ fun h2 => do t ← mkEq (mkApp2 add a a) a; let motive := Lean.mkLambda `x BinderInfo.default nat $ Lean.mkLambda `h BinderInfo.default (mkApp3 (mkConst `Eq [levelOne]) nat a (mkBVar 0)) $ (mkApp3 (mkConst `Eq [levelOne]) nat (mkApp2 add a (mkBVar 1)) a); withLocalDecl `h1 t BinderInfo.default $ fun h1 => do r ← mkEqRec motive h1 h2; print r; rType ← inferType r >>= whnf; print rType; check r; pure () #eval tst33 def tst34 : MetaM Unit := do print "----- tst34 -----"; let type := mkSort levelOne; withLocalDecl `Ξ± type BinderInfo.default $ fun Ξ± => do m ← mkFreshExprMVar type; t ← mkLambda #[Ξ±] (mkArrow m m); print t; pure () set_option pp.purify_metavars false #eval tst34 def tst35 : MetaM Unit := do print "----- tst35 -----"; let type := mkSort levelOne; withLocalDecl `Ξ± type BinderInfo.default $ fun Ξ± => do m1 ← mkFreshExprMVar type; m2 ← mkFreshExprMVar (mkArrow nat type); let v := mkLambda `x BinderInfo.default nat m1; assignExprMVar m2.mvarId! v; let w := mkApp m2 zero; t1 ← mkLambda #[Ξ±] (mkArrow w w); print t1; m3 ← mkFreshExprMVar type; t2 ← mkLambda #[Ξ±] (mkArrow (mkBVar 0) (mkBVar 1)); print t2; check $ isDefEq t1 t2; pure () #eval tst35 #check @Id def tst36 : MetaM Unit := do print "----- tst36 -----"; let type := mkSort levelOne; m1 ← mkFreshExprMVar (mkArrow type type); withLocalDecl `Ξ± type BinderInfo.default $ fun Ξ± => do m2 ← mkFreshExprMVar type; t ← mkAppM `Id #[m2]; check $ approxDefEq $ isDefEq (mkApp m1 Ξ±) t; check $ approxDefEq $ isDefEq m1 (mkConst `Id [levelZero]); pure () #eval tst36 def tst37 : MetaM Unit := do print "----- tst37 -----"; m1 ← mkFreshExprMVar (mkArrow nat (mkArrow type type)); m2 ← mkFreshExprMVar (mkArrow nat type); withLocalDecl `v nat BinderInfo.default $ fun v => do let lhs := mkApp2 m1 v (mkApp m2 v); rhs ← mkAppM `StateM #[nat, nat]; print lhs; print rhs; check $ approxDefEq $ isDefEq lhs rhs; pure () #eval tst37 def tst38 : MetaM Unit := do print "----- tst37 -----"; m1 ← mkFreshExprMVar nat; withLocalDecl `x nat BinderInfo.default $ fun x => do m2 ← mkFreshExprMVar type; withLocalDecl `y m2 BinderInfo.default $ fun y => do m3 ← mkFreshExprMVar (mkArrow m2 nat); let rhs := mkApp m3 y; check $ approxDefEq $ isDefEq m2 nat; print m2; check $ getAssignment m2 >>= fun v => pure $ v == nat; check $ approxDefEq $ isDefEq m1 rhs; print m2; check $ getAssignment m2 >>= fun v => pure $ v == nat; pure () set_option pp.all true set_option trace.Meta.isDefEq.step true set_option trace.Meta.isDefEq.delta true set_option trace.Meta.isDefEq.assign true #eval tst38
6bf4660bcb8144e6a3efd6ad73fdd4e865001e6e
94e33a31faa76775069b071adea97e86e218a8ee
/src/data/polynomial/cardinal.lean
6d74d7eb4df8ec86989c0471d061c838e8bb2705
[ "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
798
lean
/- Copyright (c) 2021 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import data.mv_polynomial.cardinal import data.mv_polynomial.equiv /-! # Cardinality of Polynomial Ring The reuslt in this file is that the cardinality of `polynomial R` is at most the maximum of `#R` and `β„΅β‚€`. -/ universe u open_locale cardinal polynomial open cardinal namespace polynomial lemma cardinal_mk_le_max {R : Type u} [comm_semiring R] : #R[X] ≀ max (#R) β„΅β‚€ := calc #R[X] = #(mv_polynomial punit.{u + 1} R) : cardinal.eq.2 ⟨(mv_polynomial.punit_alg_equiv.{u u} R).to_equiv.symm⟩ ... ≀ _ : mv_polynomial.cardinal_mk_le_max ... ≀ _ : by rw [max_assoc, max_eq_right (lt_aleph_0_of_finite punit).le] end polynomial
699646e923def490a165e7b14272e355e48e9571
d1a52c3f208fa42c41df8278c3d280f075eb020c
/stage0/src/Lean/Parser/Attr.lean
69785a09dc71401a5e905c5b437f5dbf8c48064b
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach" ]
permissive
cipher1024/lean4
6e1f98bb58e7a92b28f5364eb38a14c8d0aae393
69114d3b50806264ef35b57394391c3e738a9822
refs/heads/master
1,642,227,983,603
1,642,011,696,000
1,642,011,696,000
228,607,691
0
0
Apache-2.0
1,576,584,269,000
1,576,584,268,000
null
UTF-8
Lean
false
false
2,048
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Parser.Basic import Lean.Parser.Extra namespace Lean.Parser builtin_initialize registerBuiltinParserAttribute `builtinPrioParser `prio LeadingIdentBehavior.both registerBuiltinDynamicParserAttribute `prioParser `prio builtin_initialize registerBuiltinParserAttribute `builtinAttrParser `attr LeadingIdentBehavior.symbol registerBuiltinDynamicParserAttribute `attrParser `attr @[inline] def priorityParser (rbp : Nat := 0) : Parser := categoryParser `prio rbp @[inline] def attrParser (rbp : Nat := 0) : Parser := categoryParser `attr rbp attribute [runBuiltinParserAttributeHooks] priorityParser attrParser namespace Priority @[builtinPrioParser] def numPrio := checkPrec maxPrec >> numLit attribute [runBuiltinParserAttributeHooks] numPrio end Priority namespace Attr @[builtinAttrParser] def simple := leading_parser ident >> optional (priorityParser <|> ident) /- Remark, We can't use `simple` for `class`, `instance`, `export`, and `macro` because they are keywords. -/ @[builtinAttrParser] def Β«macroΒ» := leading_parser "macro " >> ident @[builtinAttrParser] def Β«exportΒ» := leading_parser "export " >> ident /- We don't use `simple` for recursor because the argument is not a priority-/ @[builtinAttrParser] def recursor := leading_parser nonReservedSymbol "recursor " >> numLit @[builtinAttrParser] def Β«classΒ» := leading_parser "class" @[builtinAttrParser] def Β«instanceΒ» := leading_parser "instance" >> optional priorityParser @[builtinAttrParser] def defaultInstance := leading_parser nonReservedSymbol "defaultInstance " >> optional priorityParser def externEntry := leading_parser optional ident >> optional (nonReservedSymbol "inline ") >> strLit @[builtinAttrParser] def extern := leading_parser nonReservedSymbol "extern " >> optional numLit >> many externEntry end Attr end Lean.Parser
5266d4df8a1c9b6863dda66781f7a553d7c7c58e
be30445afb85fcb2041b437059d21f9a84dff894
/abstract.hlean
6daaf00e19db6e31375f6063e4e6cdb72557f0c1
[ "Apache-2.0" ]
permissive
EgbertRijke/GraphModel
784efde97299c4f3cb1760369e8a260e02caafd5
2a473880764093dd151554a913292ed465e80e62
refs/heads/master
1,609,477,731,119
1,476,299,056,000
1,476,299,056,000
58,213,801
6
0
null
null
null
null
UTF-8
Lean
false
false
20,772
hlean
import types open eq funext universe variable u -- We attempt to formalize a weak notion of model of type theory using only homotopy-invariant -- tools. We have two examples in mind: -- - it should be possible to obtain such a model from a univalent universe, -- - given a univalent universe, there should be a model or (reflexive) graphs. -- -- It turns out that what we formalize here is a theory of structures and dependent structures, -- containing all the ways in which one could manipulate these. From another point of view, this -- is a theory of contexts and dependent contexts. It is a direct generalization of Cartmell's -- contextual categories, which were reformulated as C-systems and B-systems by Voevodsky. The -- weak notion of model we formalize here is a homotopified notion of E-system, which was -- developed by the author. B-systems form a subcategory of E-systems, for which the inclusion -- functor is full and faithful. namespace model -- open the name space model -- The base structure of a model consists of -- - a type of contexts, -- - a family of dependent stuctures over the type of contexts, -- - a family of terms of dependent structures -- In other words, it looks like -- -- base.tm ---->> base.fam ---->> base.ctx. structure base : Type := -- contexts ( ctx : Type.{u}) -- dependent structures ( fam : ctx β†’ Type.{u}) -- terms ( tm : Ξ  ⦃Γ : ctx⦄, fam Ξ“ β†’ Type.{u}) namespace base -- Open the namespace model.base -- One operation we can always define on bases, is that if AA is an indexed base, then we can take -- the total space at the level of contexts to obtain a new base ∫ AA. definition total {I : Type.{u}} (AA : I β†’ base) : base := mk -- contexts ( Ξ£ (i : I), @base.ctx (AA i)) -- families ( sigma.rec (Ξ» i, @base.fam (AA i))) -- terms ( sigma.rec (Ξ» i, @base.tm (AA i))) -- A homomorphism of bases, is simply a three-fold map sending -- - the contexts to the contexts, -- - the dependent structures to the dependent structures -- - the terms to the terms -- In a diagram, a homomorphism of bases can be indicated as -- -- base.tm AA -----> base.tm BB -- | | -- | | -- V V -- base.fam AA -----> base.fam BB -- | | -- | | -- V V -- base.ctx AA -----> base.ctx BB -- We add the notion of homomorphism of bases to the namespace model.base structure hom (AA BB : base) : Type := -- action on contexts ( action_ctx : base.ctx AA β†’ base.ctx BB) -- action on dependent structures ( action_fam : Ξ  ⦃Γ : base.ctx AA⦄, base.fam AA Ξ“ β†’ base.fam BB (action_ctx Ξ“)) -- action on terms ( action_tm : Ξ  ⦃Γ : base.ctx AA⦄ ⦃A : base.fam AA Γ⦄, base.tm AA A β†’ base.tm BB (action_fam A)) -- infixr ` β†’base ` : 50 := base.hom namespace hom -- Open the namespace model.base.hom -- We add the identity homomorphism and composition to the namespace base.hom of base homomorphisms. definition id (AA : base) : hom AA AA := mk -- action on contexts ( id) -- action on families ( Ξ» Ξ“, id) -- action on terms ( Ξ» Ξ“ A, id) definition compose {AA BB CC : base} (f : hom AA BB) (g : hom BB CC) : hom AA CC := mk -- action on contexts ( Ξ» Ξ“, action_ctx g (action_ctx f Ξ“)) -- action on families ( Ξ» Ξ“ A, action_fam g (action_fam f A)) -- action on terms ( Ξ» Ξ“ A x, action_tm g (action_tm f x)) definition total {I J : Type} (k : I β†’ J) {AA : I β†’ base} {BB : J β†’ base} ( f : Ξ  (i : I), hom (AA i) (BB (k i))) : hom (total AA) (total BB) := mk -- action on contexts ( sigma.rec (Ξ» i Ξ“, sigma.mk (k i) (action_ctx (f i) Ξ“))) -- action on families ( sigma.rec (Ξ» i Ξ“, @action_fam _ _ (f i) Ξ“)) -- action on terms ( sigma.rec (Ξ» i Ξ“, @action_tm _ _ (f i) Ξ“)) end hom -- Before we extend the base structure to a base structure with context extension, we define the type of the operation of context extension. definition has_ctxext (AA : base) : Type := Ξ  (Ξ“ : ctx AA), (fam AA Ξ“) β†’ (ctx AA) end base -- We start adding ingredients to the base structure, by adding context extension into the mix. structure e0base extends AA : base := -- context extension ( ctxext : base.has_ctxext AA) -- when base is a class, the following should work -- ( ctxext : base.has_ctxext) namespace e0base -- Open the namespace model.e0base -- If we are given a base, we can make it a base with context extension by adding context extension. definition from_base (AA : base) : base.has_ctxext AA β†’ e0base := e0base.mk ( base.ctx AA) ( base.fam AA) ( base.tm AA) -- A homomorphism of bases with context extension is simply a homomorphism of bases which preserves context extension. We first define the type that witnesses whether context extension is preserved, and then we will define homomorphisms of bases with context extension. definition ty_pres_ctxext {AA BB : e0base} (f : base.hom AA BB) : Type := Ξ  (Ξ“ : ctx AA) (A : fam AA Ξ“), base.hom.action_ctx f (ctxext AA Ξ“ A) = ctxext BB (base.hom.action_ctx f Ξ“) (base.hom.action_fam f A) structure hom (AA BB : e0base) extends f : base.hom AA BB := ( pres_ctxext : ty_pres_ctxext f) definition hom.from_basehom {AA BB : e0base} (f : base.hom AA BB) : ty_pres_ctxext f β†’ hom AA BB := hom.mk -- action on contexts ( base.hom.action_ctx f) -- action on families ( base.hom.action_fam f) -- action on terms ( base.hom.action_tm f) -- notation AA `β†’e0base` BB := hom AA BB -- Given a context Ξ“ in a base with context extension, we can find the base for a new model. definition slice {AA : e0base} (Ξ“ : e0base.ctx AA) : base := base.mk -- contexts ( fam AA Ξ“) -- families ( Ξ» A, fam AA (ctxext AA Ξ“ A)) -- terms ( Ξ» A P, tm AA P) definition slice.total (AA : e0base) : e0base := e0base.from_base (base.total (@slice AA)) -- context extension (sigma.rec (Ξ» Ξ“ A P, @sigma.mk (ctx AA) (fam AA) (ctxext AA Ξ“ A) P)) namespace hom -- Open the namespace model.e0base.hom -- Given a base homomorphism between bases with context extension, we define a slice operation on homomorphisms. definition slice {AA BB : e0base} (f : hom AA BB) (Ξ“ : ctx AA) : base.hom (slice Ξ“) (slice (action_ctx f Ξ“)) := base.hom.mk -- action on contexts (Ξ» A, action_fam f A) -- action on families (Ξ» A P, transport (fam BB) (pres_ctxext f Ξ“ A) (action_fam f P)) -- action on terms (Ξ» A P x, transportD (tm BB) (pres_ctxext f Ξ“ A) _ (action_tm f x)) -- Furthermore, we can extend context extension to a base homomorphism. definition ctxext (AA : e0base) : base.hom (@base.total (ctx AA) (@e0base.slice AA)) AA := base.hom.mk -- action on contexts (sigma.rec (ctxext AA)) -- action on families (sigma.rec (Ξ» Ξ“ A, id)) -- action on terms (sigma.rec (Ξ» Ξ“ A P, id)) end hom end e0base definition has_famext (AA : e0base) : Type := Ξ  {Ξ“ : e0base.ctx AA} (A : e0base.fam AA Ξ“), (e0base.fam AA (e0base.ctxext AA Ξ“ A)) β†’ (e0base.fam AA Ξ“) definition has_empstr (AA : e0base) : Type := Ξ  (Ξ“ : e0base.ctx AA), e0base.fam AA Ξ“ structure pre_ebase extends AA : e0base := -- family extension ( famext : has_famext AA) -- empty structure ( empstr : has_empstr AA) definition pre_ebase.from_e0base (AA : e0base) : has_famext AA β†’ has_empstr AA β†’ pre_ebase := pre_ebase.mk -- contexts ( e0base.ctx AA) -- families ( e0base.fam AA) -- terms ( e0base.tm AA) -- context extension ( e0base.ctxext AA) namespace pre_ebase -- opens the namespace model.pre_ebase definition e0slice (AA : pre_ebase) (Ξ“ : pre_ebase.ctx AA) : e0base := e0base.from_base (@e0base.slice AA Ξ“) (@pre_ebase.famext AA Ξ“) -- We will now formalize what it means to preserve family extension. -- The idea behind this definition, is that f : AA β†’ BB preserves -- family extension precisely when we can define for each Ξ“ : ctx AA -- a base homomorphism -- -- slice (f/Ξ“) : slice (AA/Ξ“) β†’ slice (BB/(f Ξ“)), -- -- such that the square of base homomorphisms -- -- ∫ (slice (f/Ξ“)) -- ∫ (slice (AA/Ξ“)) ----------------> ∫ (slice (BB/(f Ξ“))) -- | | -- hom.famext | | hom.famext -- V V -- AA/Ξ“ -------------------------> BB/(f Ξ“) -- f/Ξ“ -- -- commutes. We also formalize this below. definition ty_pres_famext {AA BB : pre_ebase} (f : e0base.hom AA BB) : Type := Ξ  (Ξ“ : ctx AA), -- Since family extension is context extension in the slice model, -- we give a specification in terms of context extension @e0base.ty_pres_ctxext -- The domain e0base (e0slice AA Ξ“) -- The codomain e0base (e0slice BB (e0base.hom.action_ctx f Ξ“)) -- The base homomorphism between them (e0base.hom.slice f Ξ“) definition ty_pres_empstr {AA BB : pre_ebase} (f : e0base.hom AA BB) : Type := Ξ  (Ξ“ : ctx AA), e0base.hom.action_fam f (empstr AA Ξ“) = empstr BB (e0base.hom.action_ctx f Ξ“) structure hom (AA BB : pre_ebase) extends f : e0base.hom AA BB := -- preserves family extension ( pres_famext : ty_pres_famext f) -- preserves the empty structure ( pres_empstr : ty_pres_empstr f) definition hom.from_e0basehom {AA BB : pre_ebase} (f : e0base.hom AA BB) : ty_pres_famext f β†’ ty_pres_empstr f β†’ hom AA BB := hom.mk -- action on contexts ( e0base.hom.action_ctx f) -- action on families ( e0base.hom.action_fam f) -- action on terms ( e0base.hom.action_tm f) -- preserves context extension ( e0base.hom.pres_ctxext f) definition slice_of_e0slice (AA : pre_ebase) (Ξ“ : ctx AA) : (e0base.ctx (e0slice AA Ξ“)) β†’ base := @e0base.slice (e0slice AA Ξ“) namespace hom -- open the namespace model.pre_ebase.hom -- We extend the definition of pre_ebase.e0slice to homomorphisms of e0bases, -- so that the slice of a pre-ebase homomorphism gets the structure of an -- e0base homomorphism. definition e0slice {AA BB : pre_ebase} (f : hom AA BB) (Ξ“ : ctx AA) : e0base.hom (pre_ebase.e0slice AA Ξ“) (pre_ebase.e0slice BB (action_ctx f Ξ“)) := e0base.hom.from_basehom ( e0base.hom.slice f Ξ“) ( pres_famext f Ξ“) end hom end pre_ebase /-- -- We now extend the notion of base to the notion of extension base, by adding -- - context extension -- - family extension -- - an empty structure, which is a right unit for context extension and a unit (both left and -- right) for family extension, -- - associativity for context extension and family extension -- We do not (yet) require further coherence laws for associativity. -- -- Note that the equations are formulated as equations between dependent functions, as opposed -- to families of equations. This makes it slightly easier, in my experience, to formulate the -- requirement that these equations should be preserved, in the formalization of later aspects -- of models of type theory. structure ebase extends e0base := -- family extension ( famext : Ξ  ⦃Γ : ctx⦄ (A : fam Ξ“), fam (ctxext Ξ“ A) β†’ fam Ξ“) -- empty structure ( empstr : Ξ  (Ξ“ : ctx), fam Ξ“) -- context extension is associative ( c_assoc : (Ξ» Ξ“ A P, ctxext (ctxext Ξ“ A) P) = (Ξ» Ξ“ A P, ctxext Ξ“ (famext A P))) -- family extension is associative ( f_assoc : pathover ( Ξ» (e : Ξ  {Ξ“ : ctx} (A : fam Ξ“), fam (ctxext Ξ“ A) β†’ ctx), Ξ  {Ξ“ : ctx} (A : fam Ξ“) (P: fam (ctxext Ξ“ A)), fam (e A P) β†’ fam Ξ“) ( Ξ» Ξ“ A P (Q : fam (ctxext (ctxext Ξ“ A) P)), famext A (famext P Q)) ( c_assoc) ( Ξ» Ξ“ A P (Q' : fam (ctxext Ξ“ (famext A P))), famext (famext A P) Q')) -- the empty structure is neutral for context extension ( c_unit : (Ξ» (Ξ“ : ctx), Ξ“) = (Ξ» Ξ“, ctxext Ξ“ (empstr Ξ“))) -- the empty structure is neutral for family extension from the left ( f_unit_left : pathover ( Ξ» (e : ctx β†’ ctx), Ξ  {Ξ“ : ctx}, fam (e Ξ“) β†’ fam Ξ“) ( Ξ» Ξ“ A, A ) ( c_unit) ( Ξ» Ξ“ A', famext (empstr Ξ“) A')) -- the empty structure is neutral for family extension from the right ( f_unit_right : (Ξ» Ξ“ A, famext A (empstr (ctxext Ξ“ A))) = (Ξ» Ξ“ A, A)) open ebase -- We open the name space model.ebase, so that we can freely use its ingredients. -- A pre-homomorphism of ebases is a homomorphism which preserves the operations of context -- extension, family extension and the empty structure. However, a pre-homomorphism of ebases is -- not yet required to preserve the further equations in the structure of an ebase. This -- requires a more elaborate formalization, in which it is useful to have the auxilary notion of -- pre-homomorphism of ebases available. structure prehom_ebase (AA BB : ebase) extends e0base.hom AA BB := -- preservation of family extension ( pres_famext : Ξ  ⦃Γ : ctx AA⦄ (A : fam AA Ξ“) (P : fam AA (ctxext AA Ξ“ A)), action_fam (famext AA A P) = famext BB (action_fam A) ((pres_ctxext Ξ“ A) β–Έ (action_fam P))) -- preservation of the empty structure ( pres_empstr : Ξ  (Ξ“ : ctx AA), action_fam (empstr AA Ξ“) = empstr BB (action_ctx Ξ“)) -- Our current goal is to formalize what it means for a pre-homomorphism of open prehom_ebase -- Note that, if Ξ” and Ξ“ are equal contexts, and P is a dependent structure over Ξ”, then we can transport P along the equality p : Ξ” = Ξ“. This results in a dependent structure p β–Έ P over Ξ“. In the following definition, we formalize that ctxext Ξ” P and ctxext Ξ“ (p β–Έ P) are again equal contexts, in this situation. definition pres_ext_c_1c_aux (BB : ebase) : Ξ  (Ξ” Ξ“ : ctx BB) (p : Ξ” = Ξ“) (P : fam BB Ξ”), ctxext BB Ξ” P = ctxext BB Ξ“ (p β–Έ P) := begin intros Ξ” Ξ“ p, induction p, intro P, reflexivity end -- In the following definition we formalize what it means for a pre-homomorphism of ebases to preserve definition pres_ext_c_1c {AA BB : ebase} (f : prehom_ebase AA BB) : Ξ  (Ξ“ : ctx AA) (A : fam AA Ξ“) (P : fam AA (ctxext AA Ξ“ A)), action_ctx f (ctxext AA (ctxext AA Ξ“ A) P) = ctxext BB (ctxext BB (action_ctx f Ξ“) (action_fam f A)) ( (pres_ctxext f Ξ“ A) β–Έ (action_fam f P)) := begin intros Ξ“ A P, refine (pres_ctxext f (ctxext AA Ξ“ A) P) ⬝ _, apply pres_ext_c_1c_aux BB end definition pres_ext_c_2f {AA BB : ebase} (f : prehom_ebase AA BB) : Ξ  (Ξ“ : ctx AA) (A : fam AA Ξ“) (P : fam AA (ctxext AA Ξ“ A)), action_ctx f (ctxext AA Ξ“ (famext AA A P)) = ctxext BB (action_ctx f Ξ“) (famext BB (action_fam f A) ((pres_ctxext f Ξ“ A) β–Έ (action_fam f P))) := begin intros Ξ“ A P, exact (pres_ctxext f Ξ“ (famext AA A P)) ⬝ (ap (ctxext BB (action_ctx f Ξ“)) (pres_famext f A P)) end -- In the following structure we extend the notion of pre-homomorphism of extension bases to the notion of homomorphism of extension bases. In other words, we formalize the conditions that a pre-homomorphism of extension bases preserves the equalities that are part of the structure of extension bases. -- -- We use function extensionality to express preservation of associativity conveniently. structure hom_ebase (AA BB : ebase) extends prehom_ebase AA BB := -- preservation of c_assoc ( pres_c_assoc : pathover ( Ξ» (e : Ξ  (XX : ebase) (Ξ“ : ctx XX) (A : fam XX Ξ“), fam XX (ctxext XX Ξ“ A) β†’ ctx XX), Ξ  (Ξ“ : ctx AA) (A : fam AA Ξ“) (P : fam AA (ctxext AA Ξ“ A)), action_ctx (e AA Ξ“ A P) = e BB (action_ctx Ξ“) (action_fam A) ((pres_ctxext Ξ“ A) β–Έ action_fam P)) ( pres_ext_c_1c ⦃ prehom_ebase, action_ctx := action_ctx, action_fam := action_fam, action_tm := action_tm, pres_ctxext := pres_ctxext, pres_famext := pres_famext, pres_empstr := pres_empstr ⦄ ) ( eq_of_homotopy c_assoc) ( pres_ext_c_2f ⦃ prehom_ebase, action_ctx := action_ctx, action_fam := action_fam, action_tm := action_tm, pres_ctxext := pres_ctxext, pres_famext := pres_famext, pres_empstr := pres_empstr ⦄ ) ) --there should be a better way to do this. /- Ξ  (Ξ“ : base.ctx AA) (A : base.fam AA Ξ“) (P : base.fam AA (ebase.ctxext AA Ξ“ A)), @pathover -- base type ( Ξ  (XX : ebase) (Ξ“' : ebase.ctx XX) (A' : ebase.fam XX Ξ“'), ebase.fam XX (ebase.ctxext XX Ξ“' A') β†’ ebase.ctx XX) -- base point domain ( Ξ» XX Ξ“' A' P', ebase.ctxext XX (ebase.ctxext XX Ξ“' A') P') -- family ( Ξ» e, action_ctx (e AA Ξ“ A P) = e BB (action_ctx Ξ“) (action_fam A) ((pres_ctxext Ξ“ A) β–Έ (action_fam P))) -- fiber point domain ( (pres_ctxext (ctxext Ξ“ A) P) ⬝ _ ) -- base point codomain ( Ξ» XX Ξ“' A' P', ebase.ctxext XX Ξ“' (ebase.famext XX A' P')) -- base path ( eq_of_homotopy (Ξ» (XX : ebase), eq_of_homotopy3 (ebase.c_assoc XX))) -- fiber point codomain ( (pres_ctxext Ξ“ (ebase.famext A P)) ⬝ _ )) -/ -- preservation of f_assoc ( pres_f_assoc : Ξ  ⦃Γ : base.ctx AA⦄ (A : base.fam AA Ξ“) (P : base.fam AA (ebase.ctxext AA Ξ“ A)) (Q : base.fam AA (ebase.ctxext AA (ebase.ctxext AA Ξ“ A) P)), ap (action_fam (ebase.f_assoc AA A P Q)) = ebase.f_assoc BB (action_fam A) ((pres_ctxext Ξ“ A) β–Έ P) (Q) ) definition slice_ebase.{v} (AA : ebase.{v}) (Ξ“ : base.ctx AA) : ebase.{v} := begin fapply ebase.mk, -- contexts exact ebase.fam AA Ξ“, -- dependent structures intro A, exact ebase.fam AA (ebase.ctxext AA Ξ“ A), -- terms intros A P, exact base.tm AA P, repeat exact sorry end definition slice_ext (AA : base) (ee : is_pre_ebase AA) (Ξ“ : base.ctx AA) : hom_base (slice_base AA ee Ξ“) AA := begin fapply hom_base.mk, -- action on contexts (is context extension) intro A, exact is_pre_ebase.ctxext ee Ξ“ A, -- action on dependent structures (is identity) intro A P, exact P, -- action on terms (is identity) intro A P f, exact f, end structure is_ebase (AA : base) (ee : is_pre_ebase AA) : Type := ( is_hom_ctxext : Ξ  (Ξ“ : base.ctx AA), is_hom_pre_ebase (slice_ext AA ee Ξ“)) -- We introduce the Ξ -constructor definition ty_pi0 (AA : e0base) : Type := Ξ  {Ξ“ : e0base.ctx AA} (A : e0base.fam AA Ξ“), base.hom (e0base.slice (e0base.ctxext AA Ξ“ A)) (e0base.slice Ξ“) structure pi0base extends AA : e0base := (pi : ty_pi0 AA) definition pi0base.from_e0base (AA : e0base) : ty_pi0 AA β†’ pi0base := pi0base.mk (e0base.ctx AA) (e0base.fam AA) (e0base.tm AA) (e0base.ctxext AA) --/ end model
74863ca41b59ff3c0e6639015a65f331361a6152
5756a081670ba9c1d1d3fca7bd47cb4e31beae66
/Mathport/Util/Heartbeats.lean
275eb4c09fdba59e5ca7bcb8dbc5599381b62e6e
[ "Apache-2.0" ]
permissive
leanprover-community/mathport
2c9bdc8292168febf59799efdc5451dbf0450d4a
13051f68064f7638970d39a8fecaede68ffbf9e1
refs/heads/master
1,693,841,364,079
1,693,813,111,000
1,693,813,111,000
379,357,010
27
10
Apache-2.0
1,691,309,132,000
1,624,384,521,000
Lean
UTF-8
Lean
false
false
840
lean
/- Copyright (c) 2023 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Gabriel Ebner -/ namespace Mathport @[extern "lean_set_max_heartbeat"] opaque setMaxHeartbeat (max : USize) : BaseIO Unit @[extern "lean_reset_heartbeat"] opaque resetHeartbeat : BaseIO Unit def withMaxHeartbeat [Monad m] [MonadFinally m] [MonadLiftT BaseIO m] (max : USize) (k : m Ξ±) : m Ξ± := do resetHeartbeat setMaxHeartbeat max try k finally setMaxHeartbeat 0 @[noinline] private unsafe def withMaxHeartbeatPureImpl (max : USize) (k : {_ : Unit} β†’ Ξ±) : Ξ± := unsafeBaseIO do withMaxHeartbeat max do return @k (← pure ()) @[implemented_by withMaxHeartbeatPureImpl] opaque withMaxHeartbeatPure {Ξ± : Type} (max : USize) (k : {_ : Unit} β†’ Ξ±) : Ξ± := @k ()
7d3e1d63944bd2231eb6a598184fed6848f045ed
947fa6c38e48771ae886239b4edce6db6e18d0fb
/src/linear_algebra/basis.lean
fda0c874741b2b7254601f69ccd8ed206b38804d
[ "Apache-2.0" ]
permissive
ramonfmir/mathlib
c5dc8b33155473fab97c38bd3aa6723dc289beaa
14c52e990c17f5a00c0cc9e09847af16fabbed25
refs/heads/master
1,661,979,343,526
1,660,830,384,000
1,660,830,384,000
182,072,989
0
0
null
1,555,585,876,000
1,555,585,876,000
null
UTF-8
Lean
false
false
52,160
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 algebra.big_operators.finsupp import algebra.big_operators.finprod import data.fintype.card import linear_algebra.finsupp import linear_algebra.linear_independent import linear_algebra.linear_pmap import linear_algebra.projection /-! # 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. * `basis ΞΉ R M` is the type of `ΞΉ`-indexed `R`-bases for a module `M`, represented by a linear equiv `M ≃ₗ[R] ΞΉ β†’β‚€ R`. * the basis vectors of a basis `b : basis ΞΉ R M` are available as `b i`, where `i : ΞΉ` * `basis.repr` is the isomorphism sending `x : M` to its coordinates `basis.repr x : ΞΉ β†’β‚€ R`. The converse, turning this isomorphism into a basis, is called `basis.of_repr`. * If `ΞΉ` is finite, there is a variant of `repr` called `basis.equiv_fun b : M ≃ₗ[R] ΞΉ β†’ R` (saving you from having to work with `finsupp`). The converse, turning this isomorphism into a basis, is called `basis.of_equiv_fun`. * `basis.constr hv f` constructs a linear map `M₁ β†’β‚—[R] Mβ‚‚` given the values `f : ΞΉ β†’ Mβ‚‚` at the basis elements `⇑b : ΞΉ β†’ M₁`. * `basis.reindex` uses an equiv to map a basis to a different indexing set. * `basis.map` uses a linear equiv to map a basis to a different module. ## Main statements * `basis.mk`: a linear independent set of vectors spanning the whole module determines a basis * `basis.ext` states that two linear maps are equal if they coincide on a basis. Similar results are available for linear equivs (if they coincide on the basis vectors), elements (if their coordinates coincide) and the functions `b.repr` and `⇑b`. * `basis.of_vector_space` 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 variables [semiring R] variables [add_comm_monoid M] [module R M] [add_comm_monoid M'] [module R M'] section variables (ΞΉ) (R) (M) /-- A `basis ΞΉ R M` for a module `M` is the type of `ΞΉ`-indexed `R`-bases of `M`. The basis vectors are available as `coe_fn (b : basis ΞΉ R M) : ΞΉ β†’ M`. To turn a linear independent family of vectors spanning `M` into a basis, use `basis.mk`. They are internally represented as linear equivs `M ≃ₗ[R] (ΞΉ β†’β‚€ R)`, available as `basis.repr`. -/ structure basis := of_repr :: (repr : M ≃ₗ[R] (ΞΉ β†’β‚€ R)) end namespace basis instance : inhabited (basis ΞΉ R (ΞΉ β†’β‚€ R)) := ⟨basis.of_repr (linear_equiv.refl _ _)⟩ variables (b b₁ : basis ΞΉ R M) (i : ΞΉ) (c : R) (x : M) section repr /-- `b i` is the `i`th basis vector. -/ instance : has_coe_to_fun (basis ΞΉ R M) (Ξ» _, ΞΉ β†’ M) := { coe := Ξ» b i, b.repr.symm (finsupp.single i 1) } @[simp] lemma coe_of_repr (e : M ≃ₗ[R] (ΞΉ β†’β‚€ R)) : ⇑(of_repr e) = Ξ» i, e.symm (finsupp.single i 1) := rfl protected lemma injective [nontrivial R] : injective b := b.repr.symm.injective.comp (Ξ» _ _, (finsupp.single_left_inj (one_ne_zero : (1 : R) β‰  0)).mp) lemma repr_symm_single_one : b.repr.symm (finsupp.single i 1) = b i := rfl lemma repr_symm_single : b.repr.symm (finsupp.single i c) = c β€’ b i := calc b.repr.symm (finsupp.single i c) = b.repr.symm (c β€’ finsupp.single i 1) : by rw [finsupp.smul_single', mul_one] ... = c β€’ b i : by rw [linear_equiv.map_smul, repr_symm_single_one] @[simp] lemma repr_self : b.repr (b i) = finsupp.single i 1 := linear_equiv.apply_symm_apply _ _ lemma repr_self_apply (j) [decidable (i = j)] : b.repr (b i) j = if i = j then 1 else 0 := by rw [repr_self, finsupp.single_apply] @[simp] lemma repr_symm_apply (v) : b.repr.symm v = finsupp.total ΞΉ M R b v := calc b.repr.symm v = b.repr.symm (v.sum finsupp.single) : by simp ... = βˆ‘ i in v.support, b.repr.symm (finsupp.single i (v i)) : by rw [finsupp.sum, linear_equiv.map_sum] ... = finsupp.total ΞΉ M R b v : by simp [repr_symm_single, finsupp.total_apply, finsupp.sum] @[simp] lemma coe_repr_symm : ↑b.repr.symm = finsupp.total ΞΉ M R b := linear_map.ext (Ξ» v, b.repr_symm_apply v) @[simp] lemma repr_total (v) : b.repr (finsupp.total _ _ _ b v) = v := by { rw ← b.coe_repr_symm, exact b.repr.apply_symm_apply v } @[simp] lemma total_repr : finsupp.total _ _ _ b (b.repr x) = x := by { rw ← b.coe_repr_symm, exact b.repr.symm_apply_apply x } lemma repr_range : (b.repr : M β†’β‚—[R] (ΞΉ β†’β‚€ R)).range = finsupp.supported R R univ := by rw [linear_equiv.range, finsupp.supported_univ] lemma mem_span_repr_support {ΞΉ : Type*} (b : basis ΞΉ R M) (m : M) : m ∈ span R (b '' (b.repr m).support) := (finsupp.mem_span_image_iff_total _).2 ⟨b.repr m, (by simp [finsupp.mem_supported_support])⟩ lemma repr_support_subset_of_mem_span {ΞΉ : Type*} (b : basis ΞΉ R M) (s : set ΞΉ) {m : M} (hm : m ∈ span R (b '' s)) : ↑(b.repr m).support βŠ† s := begin rcases (finsupp.mem_span_image_iff_total _).1 hm with ⟨l, hl, hlm⟩, rwa [←hlm, repr_total, ←finsupp.mem_supported R l] end end repr section coord /-- `b.coord i` is the linear function giving the `i`'th coordinate of a vector with respect to the basis `b`. `b.coord i` is an element of the dual space. In particular, for finite-dimensional spaces it is the `ΞΉ`th basis vector of the dual space. -/ @[simps] def coord : M β†’β‚—[R] R := (finsupp.lapply i) βˆ˜β‚— ↑b.repr lemma forall_coord_eq_zero_iff {x : M} : (βˆ€ i, b.coord i x = 0) ↔ x = 0 := iff.trans (by simp only [b.coord_apply, finsupp.ext_iff, finsupp.zero_apply]) b.repr.map_eq_zero_iff /-- The sum of the coordinates of an element `m : M` with respect to a basis. -/ noncomputable def sum_coords : M β†’β‚—[R] R := finsupp.lsum β„• (Ξ» i, linear_map.id) βˆ˜β‚— (b.repr : M β†’β‚—[R] ΞΉ β†’β‚€ R) @[simp] lemma coe_sum_coords : (b.sum_coords : M β†’ R) = Ξ» m, (b.repr m).sum (Ξ» i, id) := rfl lemma coe_sum_coords_eq_finsum : (b.sum_coords : M β†’ R) = Ξ» m, βˆ‘αΆ  i, b.coord i m := begin ext m, simp only [basis.sum_coords, basis.coord, finsupp.lapply_apply, linear_map.id_coe, linear_equiv.coe_coe, function.comp_app, finsupp.coe_lsum, linear_map.coe_comp, finsum_eq_sum _ (b.repr m).finite_support, finsupp.sum, finset.finite_to_set_to_finset, id.def, finsupp.fun_support_eq], end @[simp] lemma coe_sum_coords_of_fintype [fintype ΞΉ] : (b.sum_coords : M β†’ R) = βˆ‘ i, b.coord i := begin ext m, simp only [sum_coords, finsupp.sum_fintype, linear_map.id_coe, linear_equiv.coe_coe, coord_apply, id.def, fintype.sum_apply, implies_true_iff, eq_self_iff_true, finsupp.coe_lsum, linear_map.coe_comp], end @[simp] lemma sum_coords_self_apply : b.sum_coords (b i) = 1 := by simp only [basis.sum_coords, linear_map.id_coe, linear_equiv.coe_coe, id.def, basis.repr_self, function.comp_app, finsupp.coe_lsum, linear_map.coe_comp, finsupp.sum_single_index] end coord section ext variables {R₁ : Type*} [semiring R₁] {Οƒ : R β†’+* R₁} {Οƒ' : R₁ β†’+* R} variables [ring_hom_inv_pair Οƒ Οƒ'] [ring_hom_inv_pair Οƒ' Οƒ] variables {M₁ : Type*} [add_comm_monoid M₁] [module R₁ M₁] /-- Two linear maps are equal if they are equal on basis vectors. -/ theorem ext {f₁ fβ‚‚ : M β†’β‚›β‚—[Οƒ] M₁} (h : βˆ€ i, f₁ (b i) = fβ‚‚ (b i)) : f₁ = fβ‚‚ := by { ext x, rw [← b.total_repr x, finsupp.total_apply, finsupp.sum], simp only [linear_map.map_sum, linear_map.map_smulβ‚›β‚—, h] } include Οƒ' /-- Two linear equivs are equal if they are equal on basis vectors. -/ theorem ext' {f₁ fβ‚‚ : M ≃ₛₗ[Οƒ] M₁} (h : βˆ€ i, f₁ (b i) = fβ‚‚ (b i)) : f₁ = fβ‚‚ := by { ext x, rw [← b.total_repr x, finsupp.total_apply, finsupp.sum], simp only [linear_equiv.map_sum, linear_equiv.map_smulβ‚›β‚—, h] } omit Οƒ' /-- Two elements are equal if their coordinates are equal. -/ theorem ext_elem {x y : M} (h : βˆ€ i, b.repr x i = b.repr y i) : x = y := by { rw [← b.total_repr x, ← b.total_repr y], congr' 1, ext i, exact h i } lemma repr_eq_iff {b : basis ΞΉ R M} {f : M β†’β‚—[R] ΞΉ β†’β‚€ R} : ↑b.repr = f ↔ βˆ€ i, f (b i) = finsupp.single i 1 := ⟨λ h i, h β–Έ b.repr_self i, Ξ» h, b.ext (Ξ» i, (b.repr_self i).trans (h i).symm)⟩ lemma repr_eq_iff' {b : basis ΞΉ R M} {f : M ≃ₗ[R] ΞΉ β†’β‚€ R} : b.repr = f ↔ βˆ€ i, f (b i) = finsupp.single i 1 := ⟨λ h i, h β–Έ b.repr_self i, Ξ» h, b.ext' (Ξ» i, (b.repr_self i).trans (h i).symm)⟩ lemma apply_eq_iff {b : basis ΞΉ R M} {x : M} {i : ΞΉ} : b i = x ↔ b.repr x = finsupp.single i 1 := ⟨λ h, h β–Έ b.repr_self i, Ξ» h, b.repr.injective ((b.repr_self i).trans h.symm)⟩ /-- An unbundled version of `repr_eq_iff` -/ lemma 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 (b i) = finsupp.single i 1) (x : M) (i : ΞΉ) : b.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 { simp [hsmul, pi.smul_apply] } }, have : (finsupp.lapply i) βˆ˜β‚— ↑b.repr = f_i, { refine b.ext (Ξ» j, _), show b.repr (b j) i = f (b j) i, rw [b.repr_self, f_eq] }, calc b.repr x i = f_i x : by { rw ← this, refl } ... = f x i : rfl end /-- Two bases are equal if they assign the same coordinates. -/ lemma eq_of_repr_eq_repr {b₁ bβ‚‚ : basis ΞΉ R M} (h : βˆ€ x i, b₁.repr x i = bβ‚‚.repr x i) : b₁ = bβ‚‚ := have b₁.repr = bβ‚‚.repr, by { ext, apply h }, by { cases b₁, cases bβ‚‚, simpa } /-- Two bases are equal if their basis vectors are the same. -/ @[ext] lemma eq_of_apply_eq {b₁ bβ‚‚ : basis ΞΉ R M} (h : βˆ€ i, b₁ i = bβ‚‚ i) : b₁ = bβ‚‚ := suffices b₁.repr = bβ‚‚.repr, by { cases b₁, cases bβ‚‚, simpa }, repr_eq_iff'.mpr (Ξ» i, by rw [h, bβ‚‚.repr_self]) end ext section map variables (f : M ≃ₗ[R] M') /-- Apply the linear equivalence `f` to the basis vectors. -/ @[simps] protected def map : basis ΞΉ R M' := of_repr (f.symm.trans b.repr) @[simp] lemma map_apply (i) : b.map f i = f (b i) := rfl end map section map_coeffs variables {R' : Type*} [semiring R'] [module R' M] (f : R ≃+* R') (h : βˆ€ c (x : M), f c β€’ x = c β€’ x) include f h b local attribute [instance] has_smul.comp.is_scalar_tower /-- If `R` and `R'` are isomorphic rings that act identically on a module `M`, then a basis for `M` as `R`-module is also a basis for `M` as `R'`-module. See also `basis.algebra_map_coeffs` for the case where `f` is equal to `algebra_map`. -/ @[simps {simp_rhs := tt}] def map_coeffs : basis ΞΉ R' M := begin letI : module R' R := module.comp_hom R (↑f.symm : R' β†’+* R), haveI : is_scalar_tower R' R M := { smul_assoc := Ξ» x y z, begin dsimp [(β€’)], rw [mul_smul, ←h, f.apply_symm_apply], end }, exact (of_repr $ (b.repr.restrict_scalars R').trans $ finsupp.map_range.linear_equiv (module.comp_hom.to_linear_equiv f.symm).symm ) end lemma map_coeffs_apply (i : ΞΉ) : b.map_coeffs f h i = b i := apply_eq_iff.mpr $ by simp [f.to_add_equiv_eq_coe] @[simp] lemma coe_map_coeffs : (b.map_coeffs f h : ΞΉ β†’ M) = b := funext $ b.map_coeffs_apply f h end map_coeffs section reindex variables (b' : basis ΞΉ' R M') variables (e : ΞΉ ≃ ΞΉ') /-- `b.reindex (e : ΞΉ ≃ ΞΉ')` is a basis indexed by `ΞΉ'` -/ def reindex : basis ΞΉ' R M := basis.of_repr (b.repr.trans (finsupp.dom_lcongr e)) lemma reindex_apply (i' : ΞΉ') : b.reindex e i' = b (e.symm i') := show (b.repr.trans (finsupp.dom_lcongr e)).symm (finsupp.single i' 1) = b.repr.symm (finsupp.single (e.symm i') 1), by rw [linear_equiv.symm_trans_apply, finsupp.dom_lcongr_symm, finsupp.dom_lcongr_single] @[simp] lemma coe_reindex : (b.reindex e : ΞΉ' β†’ M) = b ∘ e.symm := funext (b.reindex_apply e) @[simp] lemma coe_reindex_repr : ((b.reindex e).repr x : ΞΉ' β†’ R) = b.repr x ∘ e.symm := funext $ Ξ» i', show (finsupp.dom_lcongr e : _ ≃ₗ[R] _) (b.repr x) i' = _, by simp @[simp] lemma reindex_repr (i' : ΞΉ') : (b.reindex e).repr x i' = b.repr x (e.symm i') := by rw coe_reindex_repr @[simp] lemma reindex_refl : b.reindex (equiv.refl ΞΉ) = b := eq_of_apply_eq $ Ξ» i, by simp /-- `simp` normal form version of `range_reindex` -/ @[simp] lemma range_reindex' : set.range (b ∘ e.symm) = set.range b := by rw [range_comp, equiv.range_eq_univ, set.image_univ] lemma range_reindex : set.range (b.reindex e) = set.range b := by rw [coe_reindex, range_reindex'] /-- `b.reindex_range` is a basis indexed by `range b`, the basis vectors themselves. -/ def reindex_range : basis (range b) R M := if h : nontrivial R then by letI := h; exact b.reindex (equiv.of_injective b (basis.injective b)) else by letI : subsingleton R := not_nontrivial_iff_subsingleton.mp h; exact basis.of_repr (module.subsingleton_equiv R M (range b)) lemma finsupp.single_apply_left {Ξ± Ξ² Ξ³ : Type*} [has_zero Ξ³] {f : Ξ± β†’ Ξ²} (hf : function.injective f) (x z : Ξ±) (y : Ξ³) : finsupp.single (f x) y (f z) = finsupp.single x y z := by simp [finsupp.single_apply, hf.eq_iff] lemma reindex_range_self (i : ΞΉ) (h := set.mem_range_self i) : b.reindex_range ⟨b i, h⟩ = b i := begin by_cases htr : nontrivial R, { letI := htr, simp [htr, reindex_range, reindex_apply, equiv.apply_of_injective_symm b.injective, subtype.coe_mk] }, { letI : subsingleton R := not_nontrivial_iff_subsingleton.mp htr, letI := module.subsingleton R M, simp [reindex_range] } end lemma reindex_range_repr_self (i : ΞΉ) : b.reindex_range.repr (b i) = finsupp.single ⟨b i, mem_range_self i⟩ 1 := calc b.reindex_range.repr (b i) = b.reindex_range.repr (b.reindex_range ⟨b i, mem_range_self i⟩) : congr_arg _ (b.reindex_range_self _ _).symm ... = finsupp.single ⟨b i, mem_range_self i⟩ 1 : b.reindex_range.repr_self _ @[simp] lemma reindex_range_apply (x : range b) : b.reindex_range x = x := by { rcases x with ⟨bi, ⟨i, rfl⟩⟩, exact b.reindex_range_self i, } lemma reindex_range_repr' (x : M) {bi : M} {i : ΞΉ} (h : b i = bi) : b.reindex_range.repr x ⟨bi, ⟨i, h⟩⟩ = b.repr x i := begin nontriviality, subst h, refine (b.repr_apply_eq (Ξ» x i, b.reindex_range.repr x ⟨b i, _⟩) _ _ _ x i).symm, { intros x y, ext i, simp only [pi.add_apply, linear_equiv.map_add, finsupp.coe_add] }, { intros c x, ext i, simp only [pi.smul_apply, linear_equiv.map_smul, finsupp.coe_smul] }, { intros i, ext j, simp only [reindex_range_repr_self], refine @finsupp.single_apply_left _ _ _ _ (Ξ» i, (⟨b i, _⟩ : set.range b)) _ _ _ _, exact Ξ» i j h, b.injective (subtype.mk.inj h) } end @[simp] lemma reindex_range_repr (x : M) (i : ΞΉ) (h := set.mem_range_self i) : b.reindex_range.repr x ⟨b i, h⟩ = b.repr x i := b.reindex_range_repr' _ rfl section fintype variables [fintype ΞΉ] /-- `b.reindex_finset_range` is a basis indexed by `finset.univ.image b`, the finite set of basis vectors themselves. -/ def reindex_finset_range : basis (finset.univ.image b) R M := b.reindex_range.reindex ((equiv.refl M).subtype_equiv (by simp)) lemma reindex_finset_range_self (i : ΞΉ) (h := finset.mem_image_of_mem b (finset.mem_univ i)) : b.reindex_finset_range ⟨b i, h⟩ = b i := by { rw [reindex_finset_range, reindex_apply, reindex_range_apply], refl } @[simp] lemma reindex_finset_range_apply (x : finset.univ.image b) : b.reindex_finset_range x = x := by { rcases x with ⟨bi, hbi⟩, rcases finset.mem_image.mp hbi with ⟨i, -, rfl⟩, exact b.reindex_finset_range_self i } lemma reindex_finset_range_repr_self (i : ΞΉ) : b.reindex_finset_range.repr (b i) = finsupp.single ⟨b i, finset.mem_image_of_mem b (finset.mem_univ i)⟩ 1 := begin ext ⟨bi, hbi⟩, rw [reindex_finset_range, reindex_repr, reindex_range_repr_self], convert finsupp.single_apply_left ((equiv.refl M).subtype_equiv _).symm.injective _ _ _, refl end @[simp] lemma reindex_finset_range_repr (x : M) (i : ΞΉ) (h := finset.mem_image_of_mem b (finset.mem_univ i)) : b.reindex_finset_range.repr x ⟨b i, h⟩ = b.repr x i := by simp [reindex_finset_range] end fintype end reindex protected lemma linear_independent : linear_independent R b := linear_independent_iff.mpr $ Ξ» l hl, calc l = b.repr (finsupp.total _ _ _ b l) : (b.repr_total l).symm ... = 0 : by rw [hl, linear_equiv.map_zero] protected lemma ne_zero [nontrivial R] (i) : b i β‰  0 := b.linear_independent.ne_zero i protected lemma mem_span (x : M) : x ∈ span R (range b) := by { rw [← b.total_repr x, finsupp.total_apply, finsupp.sum], exact submodule.sum_mem _ (Ξ» i hi, submodule.smul_mem _ _ (submodule.subset_span ⟨i, rfl⟩)) } protected lemma span_eq : span R (range b) = ⊀ := eq_top_iff.mpr $ Ξ» x _, b.mem_span x lemma index_nonempty (b : basis ΞΉ R M) [nontrivial M] : nonempty ΞΉ := begin obtain ⟨x, y, ne⟩ : βˆƒ (x y : M), x β‰  y := nontrivial.exists_pair_ne, obtain ⟨i, _⟩ := not_forall.mp (mt b.ext_elem ne), exact ⟨i⟩ end section constr variables (S : Type*) [semiring S] [module S M'] variables [smul_comm_class R S M'] /-- Construct a linear map given the value at the basis. This definition is parameterized over an extra `semiring S`, such that `smul_comm_class R S M'` holds. If `R` is commutative, you can set `S := R`; if `R` is not commutative, you can recover an `add_equiv` by setting `S := β„•`. See library note [bundled maps over different rings]. -/ def constr : (ΞΉ β†’ M') ≃ₗ[S] (M β†’β‚—[R] M') := { to_fun := Ξ» f, (finsupp.total M' M' R id).comp $ (finsupp.lmap_domain R R f) βˆ˜β‚— ↑b.repr, inv_fun := Ξ» f i, f (b i), left_inv := Ξ» f, by { ext, simp }, right_inv := Ξ» f, by { refine b.ext (Ξ» i, _), simp }, map_add' := Ξ» f g, by { refine b.ext (Ξ» i, _), simp }, map_smul' := Ξ» c f, by { refine b.ext (Ξ» i, _), simp } } theorem constr_def (f : ΞΉ β†’ M') : b.constr S f = (finsupp.total M' M' R id) βˆ˜β‚— ((finsupp.lmap_domain R R f) βˆ˜β‚— ↑b.repr) := rfl theorem constr_apply (f : ΞΉ β†’ M') (x : M) : b.constr S f x = (b.repr x).sum (Ξ» b a, a β€’ f b) := by { simp only [constr_def, linear_map.comp_apply, finsupp.lmap_domain_apply, finsupp.total_apply], rw finsupp.sum_map_domain_index; simp [add_smul] } @[simp] lemma constr_basis (f : ΞΉ β†’ M') (i : ΞΉ) : (b.constr S f : M β†’ M') (b i) = f i := by simp [basis.constr_apply, b.repr_self] lemma constr_eq {g : ΞΉ β†’ M'} {f : M β†’β‚—[R] M'} (h : βˆ€i, g i = f (b i)) : b.constr S g = f := b.ext $ Ξ» i, (b.constr_basis S g i).trans (h i) lemma constr_self (f : M β†’β‚—[R] M') : b.constr S (Ξ» i, f (b i)) = f := b.constr_eq S $ Ξ» x, rfl lemma constr_range [nonempty ΞΉ] {f : ΞΉ β†’ M'} : (b.constr S f).range = span R (range f) := by rw [b.constr_def S f, linear_map.range_comp, linear_map.range_comp, linear_equiv.range, ← finsupp.supported_univ, finsupp.lmap_domain_supported, ←set.image_univ, ← finsupp.span_image_eq_map_total, set.image_id] @[simp] lemma constr_comp (f : M' β†’β‚—[R] M') (v : ΞΉ β†’ M') : b.constr S (f ∘ v) = f.comp (b.constr S v) := b.ext (Ξ» i, by simp only [basis.constr_basis, linear_map.comp_apply]) end constr section equiv variables (b' : basis ΞΉ' R M') (e : ΞΉ ≃ ΞΉ') variables [add_comm_monoid M''] [module R M''] /-- If `b` is a basis for `M` and `b'` a basis for `M'`, and the index types are equivalent, `b.equiv b' e` is a linear equivalence `M ≃ₗ[R] M'`, mapping `b i` to `b' (e i)`. -/ protected def equiv : M ≃ₗ[R] M' := b.repr.trans (b'.reindex e.symm).repr.symm @[simp] lemma equiv_apply : b.equiv b' e (b i) = b' (e i) := by simp [basis.equiv] @[simp] lemma equiv_refl : b.equiv b (equiv.refl ΞΉ) = linear_equiv.refl R M := b.ext' (Ξ» i, by simp) @[simp] lemma equiv_symm : (b.equiv b' e).symm = b'.equiv b e.symm := b'.ext' $ Ξ» i, (b.equiv b' e).injective (by simp) @[simp] lemma equiv_trans {ΞΉ'' : Type*} (b'' : basis ΞΉ'' R M'') (e : ΞΉ ≃ ΞΉ') (e' : ΞΉ' ≃ ΞΉ'') : (b.equiv b' e).trans (b'.equiv b'' e') = b.equiv b'' (e.trans e') := b.ext' (Ξ» i, by simp) @[simp] lemma map_equiv (b : basis ΞΉ R M) (b' : basis ΞΉ' R M') (e : ΞΉ ≃ ΞΉ') : b.map (b.equiv b' e) = b'.reindex e.symm := by { ext i, simp } end equiv section prod variables (b' : basis ΞΉ' R M') /-- `basis.prod` maps a `ΞΉ`-indexed basis for `M` and a `ΞΉ'`-indexed basis for `M'` to a `ΞΉ βŠ• ΞΉ'`-index basis for `M Γ— M'`. For the specific case of `R Γ— R`, see also `basis.fin_two_prod`. -/ protected def prod : basis (ΞΉ βŠ• ΞΉ') R (M Γ— M') := of_repr ((b.repr.prod b'.repr).trans (finsupp.sum_finsupp_lequiv_prod_finsupp R).symm) @[simp] lemma prod_repr_inl (x) (i) : (b.prod b').repr x (sum.inl i) = b.repr x.1 i := rfl @[simp] lemma prod_repr_inr (x) (i) : (b.prod b').repr x (sum.inr i) = b'.repr x.2 i := rfl lemma prod_apply_inl_fst (i) : (b.prod b' (sum.inl i)).1 = b i := b.repr.injective $ by { ext j, simp only [basis.prod, basis.coe_of_repr, linear_equiv.symm_trans_apply, linear_equiv.prod_symm, linear_equiv.prod_apply, b.repr.apply_symm_apply, linear_equiv.symm_symm, repr_self, equiv.to_fun_as_coe, finsupp.fst_sum_finsupp_lequiv_prod_finsupp], apply finsupp.single_apply_left sum.inl_injective } lemma prod_apply_inr_fst (i) : (b.prod b' (sum.inr i)).1 = 0 := b.repr.injective $ by { ext i, simp only [basis.prod, basis.coe_of_repr, linear_equiv.symm_trans_apply, linear_equiv.prod_symm, linear_equiv.prod_apply, b.repr.apply_symm_apply, linear_equiv.symm_symm, repr_self, equiv.to_fun_as_coe, finsupp.fst_sum_finsupp_lequiv_prod_finsupp, linear_equiv.map_zero, finsupp.zero_apply], apply finsupp.single_eq_of_ne sum.inr_ne_inl } lemma prod_apply_inl_snd (i) : (b.prod b' (sum.inl i)).2 = 0 := b'.repr.injective $ by { ext j, simp only [basis.prod, basis.coe_of_repr, linear_equiv.symm_trans_apply, linear_equiv.prod_symm, linear_equiv.prod_apply, b'.repr.apply_symm_apply, linear_equiv.symm_symm, repr_self, equiv.to_fun_as_coe, finsupp.snd_sum_finsupp_lequiv_prod_finsupp, linear_equiv.map_zero, finsupp.zero_apply], apply finsupp.single_eq_of_ne sum.inl_ne_inr } lemma prod_apply_inr_snd (i) : (b.prod b' (sum.inr i)).2 = b' i := b'.repr.injective $ by { ext i, simp only [basis.prod, basis.coe_of_repr, linear_equiv.symm_trans_apply, linear_equiv.prod_symm, linear_equiv.prod_apply, b'.repr.apply_symm_apply, linear_equiv.symm_symm, repr_self, equiv.to_fun_as_coe, finsupp.snd_sum_finsupp_lequiv_prod_finsupp], apply finsupp.single_apply_left sum.inr_injective } @[simp] lemma prod_apply (i) : b.prod b' i = sum.elim (linear_map.inl R M M' ∘ b) (linear_map.inr R M M' ∘ b') i := by { ext; cases i; simp only [prod_apply_inl_fst, sum.elim_inl, linear_map.inl_apply, prod_apply_inr_fst, sum.elim_inr, linear_map.inr_apply, prod_apply_inl_snd, prod_apply_inr_snd, comp_app] } end prod section no_zero_smul_divisors -- Can't be an instance because the basis can't be inferred. protected lemma no_zero_smul_divisors [no_zero_divisors R] (b : basis ΞΉ R M) : no_zero_smul_divisors R M := ⟨λ c x hcx, or_iff_not_imp_right.mpr (Ξ» hx, begin rw [← b.total_repr x, ← linear_map.map_smul] at hcx, have := linear_independent_iff.mp b.linear_independent (c β€’ b.repr x) hcx, rw smul_eq_zero at this, exact this.resolve_right (Ξ» hr, hx (b.repr.map_eq_zero_iff.mp hr)) end)⟩ protected lemma smul_eq_zero [no_zero_divisors R] (b : basis ΞΉ R M) {c : R} {x : M} : c β€’ x = 0 ↔ c = 0 ∨ x = 0 := @smul_eq_zero _ _ _ _ _ b.no_zero_smul_divisors _ _ lemma _root_.eq_bot_of_rank_eq_zero [no_zero_divisors R] (b : basis ΞΉ R M) (N : submodule R M) (rank_eq : βˆ€ {m : β„•} (v : fin m β†’ N), linear_independent R (coe ∘ v : fin m β†’ M) β†’ m = 0) : N = βŠ₯ := begin rw submodule.eq_bot_iff, intros x hx, contrapose! rank_eq with x_ne, refine ⟨1, Ξ» _, ⟨x, hx⟩, _, one_ne_zero⟩, rw fintype.linear_independent_iff, rintros g sum_eq i, cases i, simp only [function.const_apply, fin.default_eq_zero, submodule.coe_mk, finset.univ_unique, function.comp_const, finset.sum_singleton] at sum_eq, convert (b.smul_eq_zero.mp sum_eq).resolve_right x_ne end end no_zero_smul_divisors section singleton /-- `basis.singleton ΞΉ R` is the basis sending the unique element of `ΞΉ` to `1 : R`. -/ protected def singleton (ΞΉ R : Type*) [unique ΞΉ] [semiring R] : basis ΞΉ R R := of_repr { to_fun := Ξ» x, finsupp.single default x, inv_fun := Ξ» f, f default, left_inv := Ξ» x, by simp, right_inv := Ξ» f, finsupp.unique_ext (by simp), map_add' := Ξ» x y, by simp, map_smul' := Ξ» c x, by simp } @[simp] lemma singleton_apply (ΞΉ R : Type*) [unique ΞΉ] [semiring R] (i) : basis.singleton ΞΉ R i = 1 := apply_eq_iff.mpr (by simp [basis.singleton]) @[simp] lemma singleton_repr (ΞΉ R : Type*) [unique ΞΉ] [semiring R] (x i) : (basis.singleton ΞΉ R).repr x i = x := by simp [basis.singleton, unique.eq_default i] lemma basis_singleton_iff {R M : Type*} [ring R] [nontrivial R] [add_comm_group M] [module R M] [no_zero_smul_divisors R M] (ΞΉ : Type*) [unique ΞΉ] : nonempty (basis ΞΉ R M) ↔ βˆƒ x β‰  0, βˆ€ y : M, βˆƒ r : R, r β€’ x = y := begin fsplit, { rintro ⟨b⟩, refine ⟨b default, b.linear_independent.ne_zero _, _⟩, simpa [span_singleton_eq_top_iff, set.range_unique] using b.span_eq }, { rintro ⟨x, nz, w⟩, refine ⟨of_repr $ linear_equiv.symm { to_fun := Ξ» f, f default β€’ x, inv_fun := Ξ» y, finsupp.single default (w y).some, left_inv := Ξ» f, finsupp.unique_ext _, right_inv := Ξ» y, _, map_add' := Ξ» y z, _, map_smul' := Ξ» c y, _ }⟩, { rw [finsupp.add_apply, add_smul] }, { rw [finsupp.smul_apply, smul_assoc], simp }, { refine smul_left_injective _ nz _, simp only [finsupp.single_eq_same], exact (w (f default β€’ x)).some_spec }, { simp only [finsupp.single_eq_same], exact (w y).some_spec } } end end singleton section empty variables (M) /-- If `M` is a subsingleton and `ΞΉ` is empty, this is the unique `ΞΉ`-indexed basis for `M`. -/ protected def empty [subsingleton M] [is_empty ΞΉ] : basis ΞΉ R M := of_repr 0 instance empty_unique [subsingleton M] [is_empty ΞΉ] : unique (basis ΞΉ R M) := { default := basis.empty M, uniq := Ξ» ⟨x⟩, congr_arg of_repr $ subsingleton.elim _ _ } end empty end basis section fintype open basis open fintype variables [fintype ΞΉ] (b : basis ΞΉ R M) /-- A module over `R` with a finite basis is linearly equivalent to functions from its basis to `R`. -/ def basis.equiv_fun : M ≃ₗ[R] (ΞΉ β†’ R) := linear_equiv.trans b.repr ({ to_fun := coe_fn, map_add' := finsupp.coe_add, map_smul' := finsupp.coe_smul, ..finsupp.equiv_fun_on_fintype } : (ΞΉ β†’β‚€ R) ≃ₗ[R] (ΞΉ β†’ R)) /-- 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 _ b.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 b.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 basis.equiv_fun_symm_apply (x : ΞΉ β†’ R) : b.equiv_fun.symm x = βˆ‘ i, x i β€’ b i := by simp [basis.equiv_fun, finsupp.total_apply, finsupp.sum_fintype] @[simp] lemma basis.equiv_fun_apply (u : M) : b.equiv_fun u = b.repr u := rfl @[simp] lemma basis.map_equiv_fun (f : M ≃ₗ[R] M') : (b.map f).equiv_fun = f.symm.trans b.equiv_fun := rfl lemma basis.sum_equiv_fun (u : M) : βˆ‘ i, b.equiv_fun u i β€’ b i = u := begin conv_rhs { rw ← b.total_repr u }, simp [finsupp.total_apply, finsupp.sum_fintype, b.equiv_fun_apply] end lemma basis.sum_repr (u : M) : βˆ‘ i, b.repr u i β€’ b i = u := b.sum_equiv_fun u @[simp] lemma basis.equiv_fun_self (i j : ΞΉ) : b.equiv_fun (b i) j = if i = j then 1 else 0 := by { rw [b.equiv_fun_apply, b.repr_self_apply] } /-- Define a basis by mapping each vector `x : M` to its coordinates `e x : ΞΉ β†’ R`, as long as `ΞΉ` is finite. -/ def basis.of_equiv_fun (e : M ≃ₗ[R] (ΞΉ β†’ R)) : basis ΞΉ R M := basis.of_repr $ e.trans $ linear_equiv.symm $ finsupp.linear_equiv_fun_on_fintype R R ΞΉ @[simp] lemma basis.of_equiv_fun_repr_apply (e : M ≃ₗ[R] (ΞΉ β†’ R)) (x : M) (i : ΞΉ) : (basis.of_equiv_fun e).repr x i = e x i := rfl @[simp] lemma basis.coe_of_equiv_fun (e : M ≃ₗ[R] (ΞΉ β†’ R)) : (basis.of_equiv_fun e : ΞΉ β†’ M) = Ξ» i, e.symm (function.update 0 i 1) := funext $ Ξ» i, e.injective $ funext $ Ξ» j, by simp [basis.of_equiv_fun, ←finsupp.single_eq_pi_single, finsupp.single_eq_update] @[simp] lemma basis.of_equiv_fun_equiv_fun (v : basis ΞΉ R M) : basis.of_equiv_fun v.equiv_fun = v := begin ext j, simp only [basis.equiv_fun_symm_apply, basis.coe_of_equiv_fun], simp_rw [function.update_apply, ite_smul], simp only [finset.mem_univ, if_true, pi.zero_apply, one_smul, finset.sum_ite_eq', zero_smul], end variables (S : Type*) [semiring S] [module S M'] variables [smul_comm_class R S M'] @[simp] theorem basis.constr_apply_fintype (f : ΞΉ β†’ M') (x : M) : (b.constr S f : M β†’ M') x = βˆ‘ i, (b.equiv_fun x i) β€’ f i := by simp [b.constr_apply, b.equiv_fun_apply, finsupp.sum_fintype] end fintype end module section comm_semiring namespace basis variables [comm_semiring R] variables [add_comm_monoid M] [module R M] [add_comm_monoid M'] [module R M'] variables (b : basis ΞΉ R M) (b' : basis ΞΉ' R M') /-- If `b` is a basis for `M` and `b'` a basis for `M'`, and `f`, `g` form a bijection between the basis vectors, `b.equiv' b' f g hf hg hgf hfg` is a linear equivalence `M ≃ₗ[R] M'`, mapping `b i` to `f (b i)`. -/ def equiv' (f : M β†’ M') (g : M' β†’ M) (hf : βˆ€ i, f (b i) ∈ range b') (hg : βˆ€ i, g (b' i) ∈ range b) (hgf : βˆ€i, g (f (b i)) = b i) (hfg : βˆ€i, f (g (b' i)) = b' i) : M ≃ₗ[R] M' := { inv_fun := b'.constr R (g ∘ b'), left_inv := have (b'.constr R (g ∘ b')).comp (b.constr R (f ∘ b)) = linear_map.id, from (b.ext $ Ξ» i, exists.elim (hf i) (Ξ» i' hi', by rw [linear_map.comp_apply, b.constr_basis, function.comp_apply, ← hi', b'.constr_basis, function.comp_apply, hi', hgf, linear_map.id_apply])), Ξ» x, congr_arg (Ξ» (h : M β†’β‚—[R] M), h x) this, right_inv := have (b.constr R (f ∘ b)).comp (b'.constr R (g ∘ b')) = linear_map.id, from (b'.ext $ Ξ» i, exists.elim (hg i) (Ξ» i' hi', by rw [linear_map.comp_apply, b'.constr_basis, function.comp_apply, ← hi', b.constr_basis, function.comp_apply, hi', hfg, linear_map.id_apply])), Ξ» x, congr_arg (Ξ» (h : M' β†’β‚—[R] M'), h x) this, .. b.constr R (f ∘ b) } @[simp] lemma equiv'_apply (f : M β†’ M') (g : M' β†’ M) (hf hg hgf hfg) (i : ΞΉ) : b.equiv' b' f g hf hg hgf hfg (b i) = f (b i) := b.constr_basis R _ _ @[simp] lemma equiv'_symm_apply (f : M β†’ M') (g : M' β†’ M) (hf hg hgf hfg) (i : ΞΉ') : (b.equiv' b' f g hf hg hgf hfg).symm (b' i) = g (b' i) := b'.constr_basis R _ _ lemma sum_repr_mul_repr {ΞΉ'} [fintype ΞΉ'] (b' : basis ΞΉ' R M) (x : M) (i : ΞΉ) : βˆ‘ (j : ΞΉ'), b.repr (b' j) i * b'.repr x j = b.repr x i := begin conv_rhs { rw [← b'.sum_repr x] }, simp_rw [linear_equiv.map_sum, linear_equiv.map_smul, finset.sum_apply'], refine finset.sum_congr rfl (Ξ» j _, _), rw [finsupp.smul_apply, smul_eq_mul, mul_comm] end end basis end comm_semiring 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 {c d : R} {x y : M} variables (b : basis ΞΉ R M) namespace basis /-- Any basis is a maximal linear independent set. -/ lemma maximal [nontrivial R] (b : basis ΞΉ R M) : b.linear_independent.maximal := Ξ» w hi h, begin -- If `range w` is strictly bigger than `range b`, apply le_antisymm h, -- then choose some `x ∈ range w \ range b`, intros x p, by_contradiction q, -- and write it in terms of the basis. have e := b.total_repr x, -- This then expresses `x` as a linear combination -- of elements of `w` which are in the range of `b`, let u : ΞΉ β†ͺ w := ⟨λ i, ⟨b i, h ⟨i, rfl⟩⟩, Ξ» i i' r, b.injective (by simpa only [subtype.mk_eq_mk] using r)⟩, have r : βˆ€ i, b i = u i := Ξ» i, rfl, simp_rw [finsupp.total_apply, r] at e, change (b.repr x).sum (Ξ» (i : ΞΉ) (a : R), (Ξ» (x : w) (r : R), r β€’ (x : M)) (u i) a) = ((⟨x, p⟩ : w) : M) at e, rw [←finsupp.sum_emb_domain, ←finsupp.total_apply] at e, -- Now we can contradict the linear independence of `hi` refine hi.total_ne_of_not_mem_support _ _ e, simp only [finset.mem_map, finsupp.support_emb_domain], rintro ⟨j, -, W⟩, simp only [embedding.coe_fn_mk, subtype.mk_eq_mk, ←r] at W, apply q ⟨j, W⟩, end section mk variables (hli : linear_independent R v) (hsp : ⊀ ≀ span R (range v)) /-- A linear independent family of vectors spanning the whole module is a basis. -/ protected noncomputable def mk : basis ΞΉ R M := basis.of_repr { inv_fun := finsupp.total _ _ _ v, left_inv := Ξ» x, hli.total_repr ⟨x, _⟩, right_inv := Ξ» x, hli.repr_eq rfl, .. hli.repr.comp (linear_map.id.cod_restrict _ (Ξ» h, hsp submodule.mem_top)) } @[simp] lemma mk_repr : (basis.mk hli hsp).repr x = hli.repr ⟨x, hsp submodule.mem_top⟩ := rfl lemma mk_apply (i : ΞΉ) : basis.mk hli hsp i = v i := show finsupp.total _ _ _ v _ = v i, by simp @[simp] lemma coe_mk : ⇑(basis.mk hli hsp) = v := funext (mk_apply _ _) variables {hli hsp} /-- Given a basis, the `i`th element of the dual basis evaluates to 1 on the `i`th element of the basis. -/ lemma mk_coord_apply_eq (i : ΞΉ) : (basis.mk hli hsp).coord i (v i) = 1 := show hli.repr ⟨v i, submodule.subset_span (mem_range_self i)⟩ i = 1, by simp [hli.repr_eq_single i] /-- Given a basis, the `i`th element of the dual basis evaluates to 0 on the `j`th element of the basis if `j β‰  i`. -/ lemma mk_coord_apply_ne {i j : ΞΉ} (h : j β‰  i) : (basis.mk hli hsp).coord i (v j) = 0 := show hli.repr ⟨v j, submodule.subset_span (mem_range_self j)⟩ i = 0, by simp [hli.repr_eq_single j, h] /-- Given a basis, the `i`th element of the dual basis evaluates to the Kronecker delta on the `j`th element of the basis. -/ lemma mk_coord_apply {i j : ΞΉ} : (basis.mk hli hsp).coord i (v j) = if j = i then 1 else 0 := begin cases eq_or_ne j i, { simp only [h, if_true, eq_self_iff_true, mk_coord_apply_eq i], }, { simp only [h, if_false, mk_coord_apply_ne h], }, end end mk section span variables (hli : linear_independent R v) /-- A linear independent family of vectors is a basis for their span. -/ protected noncomputable def span : basis ΞΉ R (span R (range v)) := basis.mk (linear_independent_span hli) $ begin intros x _, have h₁ : (coe : span R (range v) β†’ M) '' set.range (Ξ» i, subtype.mk (v i) _) = range v, { rw ← set.range_comp, refl }, have hβ‚‚ : map (submodule.subtype _) (span R (set.range (Ξ» i, subtype.mk (v i) _))) = span R (range v), { rw [← span_image, submodule.coe_subtype, h₁] }, have h₃ : (x : M) ∈ map (submodule.subtype _) (span R (set.range (Ξ» i, subtype.mk (v i) _))), { rw hβ‚‚, apply subtype.mem x }, rcases mem_map.1 h₃ with ⟨y, hy₁, hyβ‚‚βŸ©, have h_x_eq_y : x = y, { rw [subtype.ext_iff, ← hyβ‚‚], simp }, rwa h_x_eq_y end protected lemma span_apply (i : ΞΉ) : (basis.span hli i : M) = v i := congr_arg (coe : span R (range v) β†’ M) $ basis.mk_apply (linear_independent_span hli) _ i end span lemma group_smul_span_eq_top {G : Type*} [group G] [distrib_mul_action G R] [distrib_mul_action G M] [is_scalar_tower G R M] {v : ΞΉ β†’ M} (hv : submodule.span R (set.range v) = ⊀) {w : ΞΉ β†’ G} : submodule.span R (set.range (w β€’ v)) = ⊀ := begin rw eq_top_iff, intros j hj, rw ← hv at hj, rw submodule.mem_span at hj ⊒, refine Ξ» p hp, hj p (Ξ» u hu, _), obtain ⟨i, rfl⟩ := hu, have : ((w i)⁻¹ β€’ 1 : R) β€’ w i β€’ v i ∈ p := p.smul_mem ((w i)⁻¹ β€’ 1 : R) (hp ⟨i, rfl⟩), rwa [smul_one_smul, inv_smul_smul] at this, end /-- Given a basis `v` and a map `w` such that for all `i`, `w i` are elements of a group, `group_smul` provides the basis corresponding to `w β€’ v`. -/ def group_smul {G : Type*} [group G] [distrib_mul_action G R] [distrib_mul_action G M] [is_scalar_tower G R M] [smul_comm_class G R M] (v : basis ΞΉ R M) (w : ΞΉ β†’ G) : basis ΞΉ R M := @basis.mk ΞΉ R M (w β€’ v) _ _ _ (v.linear_independent.group_smul w) (group_smul_span_eq_top v.span_eq).ge lemma group_smul_apply {G : Type*} [group G] [distrib_mul_action G R] [distrib_mul_action G M] [is_scalar_tower G R M] [smul_comm_class G R M] {v : basis ΞΉ R M} {w : ΞΉ β†’ G} (i : ΞΉ) : v.group_smul w i = (w β€’ v : ΞΉ β†’ M) i := mk_apply (v.linear_independent.group_smul w) (group_smul_span_eq_top v.span_eq).ge i lemma units_smul_span_eq_top {v : ΞΉ β†’ M} (hv : submodule.span R (set.range v) = ⊀) {w : ΞΉ β†’ RΛ£} : submodule.span R (set.range (w β€’ v)) = ⊀ := group_smul_span_eq_top hv /-- Given a basis `v` and a map `w` such that for all `i`, `w i` is a unit, `smul_of_is_unit` provides the basis corresponding to `w β€’ v`. -/ def units_smul (v : basis ΞΉ R M) (w : ΞΉ β†’ RΛ£) : basis ΞΉ R M := @basis.mk ΞΉ R M (w β€’ v) _ _ _ (v.linear_independent.units_smul w) (units_smul_span_eq_top v.span_eq).ge lemma units_smul_apply {v : basis ΞΉ R M} {w : ΞΉ β†’ RΛ£} (i : ΞΉ) : v.units_smul w i = w i β€’ v i := mk_apply (v.linear_independent.units_smul w) (units_smul_span_eq_top v.span_eq).ge i /-- A version of `smul_of_units` that uses `is_unit`. -/ def is_unit_smul (v : basis ΞΉ R M) {w : ΞΉ β†’ R} (hw : βˆ€ i, is_unit (w i)): basis ΞΉ R M := units_smul v (Ξ» i, (hw i).unit) lemma is_unit_smul_apply {v : basis ΞΉ R M} {w : ΞΉ β†’ R} (hw : βˆ€ i, is_unit (w i)) (i : ΞΉ) : v.is_unit_smul hw i = w i β€’ v i := units_smul_apply i section fin /-- Let `b` be a basis for a submodule `N` of `M`. If `y : M` is linear independent of `N` and `y` and `N` together span the whole of `M`, then there is a basis for `M` whose basis vectors are given by `fin.cons y b`. -/ noncomputable def mk_fin_cons {n : β„•} {N : submodule R M} (y : M) (b : basis (fin n) R N) (hli : βˆ€ (c : R) (x ∈ N), c β€’ y + x = 0 β†’ c = 0) (hsp : βˆ€ (z : M), βˆƒ (c : R), z + c β€’ y ∈ N) : basis (fin (n + 1)) R M := have span_b : submodule.span R (set.range (N.subtype ∘ b)) = N, { rw [set.range_comp, submodule.span_image, b.span_eq, submodule.map_subtype_top] }, @basis.mk _ _ _ (fin.cons y (N.subtype ∘ b) : fin (n + 1) β†’ M) _ _ _ ((b.linear_independent.map' N.subtype (submodule.ker_subtype _)) .fin_cons' _ _ $ by { rintros c ⟨x, hx⟩ hc, rw span_b at hx, exact hli c x hx hc }) (Ξ» x _, by { rw [fin.range_cons, submodule.mem_span_insert', span_b], exact hsp x }) @[simp] lemma coe_mk_fin_cons {n : β„•} {N : submodule R M} (y : M) (b : basis (fin n) R N) (hli : βˆ€ (c : R) (x ∈ N), c β€’ y + x = 0 β†’ c = 0) (hsp : βˆ€ (z : M), βˆƒ (c : R), z + c β€’ y ∈ N) : (mk_fin_cons y b hli hsp : fin (n + 1) β†’ M) = fin.cons y (coe ∘ b) := coe_mk _ _ /-- Let `b` be a basis for a submodule `N ≀ O`. If `y ∈ O` is linear independent of `N` and `y` and `N` together span the whole of `O`, then there is a basis for `O` whose basis vectors are given by `fin.cons y b`. -/ noncomputable def mk_fin_cons_of_le {n : β„•} {N O : submodule R M} (y : M) (yO : y ∈ O) (b : basis (fin n) R N) (hNO : N ≀ O) (hli : βˆ€ (c : R) (x ∈ N), c β€’ y + x = 0 β†’ c = 0) (hsp : βˆ€ (z ∈ O), βˆƒ (c : R), z + c β€’ y ∈ N) : basis (fin (n + 1)) R O := mk_fin_cons ⟨y, yO⟩ (b.map (submodule.comap_subtype_equiv_of_le hNO).symm) (Ξ» c x hc hx, hli c x (submodule.mem_comap.mp hc) (congr_arg coe hx)) (Ξ» z, hsp z z.2) @[simp] lemma coe_mk_fin_cons_of_le {n : β„•} {N O : submodule R M} (y : M) (yO : y ∈ O) (b : basis (fin n) R N) (hNO : N ≀ O) (hli : βˆ€ (c : R) (x ∈ N), c β€’ y + x = 0 β†’ c = 0) (hsp : βˆ€ (z ∈ O), βˆƒ (c : R), z + c β€’ y ∈ N) : (mk_fin_cons_of_le y yO b hNO hli hsp : fin (n + 1) β†’ O) = fin.cons ⟨y, yO⟩ (submodule.of_le hNO ∘ b) := coe_mk_fin_cons _ _ _ _ /-- The basis of `R Γ— R` given by the two vectors `(1, 0)` and `(0, 1)`. -/ protected def fin_two_prod (R : Type*) [semiring R] : basis (fin 2) R (R Γ— R) := basis.of_equiv_fun (linear_equiv.fin_two_arrow R R).symm @[simp] lemma fin_two_prod_zero (R : Type*) [semiring R] : basis.fin_two_prod R 0 = (1, 0) := by simp [basis.fin_two_prod] @[simp] lemma fin_two_prod_one (R : Type*) [semiring R] : basis.fin_two_prod R 1 = (0, 1) := by simp [basis.fin_two_prod] @[simp] lemma coe_fin_two_prod_repr {R : Type*} [semiring R] (x : R Γ— R) : ⇑((basis.fin_two_prod R).repr x) = ![x.fst, x.snd] := rfl end fin end basis end module section induction variables [ring R] [is_domain R] variables [add_comm_group M] [module R M] {b : ΞΉ β†’ M} /-- If `N` is a submodule with finite rank, do induction on adjoining a linear independent element to a submodule. -/ def submodule.induction_on_rank_aux (b : basis ΞΉ R M) (P : submodule R M β†’ Sort*) (ih : βˆ€ (N : submodule R M), (βˆ€ (N' ≀ N) (x ∈ N), (βˆ€ (c : R) (y ∈ N'), c β€’ x + y = (0 : M) β†’ c = 0) β†’ P N') β†’ P N) (n : β„•) (N : submodule R M) (rank_le : βˆ€ {m : β„•} (v : fin m β†’ N), linear_independent R (coe ∘ v : fin m β†’ M) β†’ m ≀ n) : P N := begin haveI : decidable_eq M := classical.dec_eq M, have Pbot : P βŠ₯, { apply ih, intros N N_le x x_mem x_ortho, exfalso, simpa using x_ortho 1 0 N.zero_mem }, induction n with n rank_ih generalizing N, { suffices : N = βŠ₯, { rwa this }, apply eq_bot_of_rank_eq_zero b _ (Ξ» m v hv, nat.le_zero_iff.mp (rank_le v hv)) }, apply ih, intros N' N'_le x x_mem x_ortho, apply rank_ih, intros m v hli, refine nat.succ_le_succ_iff.mp (rank_le (fin.cons ⟨x, x_mem⟩ (Ξ» i, ⟨v i, N'_le (v i).2⟩)) _), convert hli.fin_cons' x _ _, { ext i, refine fin.cases _ _ i; simp }, { intros c y hcy, refine x_ortho c y (submodule.span_le.mpr _ y.2) hcy, rintros _ ⟨z, rfl⟩, exact (v z).2 } end end induction section division_ring variables [division_ring K] [add_comm_group V] [add_comm_group V'] [module K V] [module K V'] variables {v : ΞΉ β†’ V} {s t : set V} {x y z : V} include K open submodule namespace basis section exists_basis /-- If `s` is a linear independent set of vectors, we can extend it to a basis. -/ noncomputable def extend (hs : linear_independent K (coe : s β†’ V)) : basis _ K V := basis.mk (@linear_independent.restrict_of_comp_subtype _ _ _ id _ _ _ _ (hs.linear_independent_extend _)) (set_like.coe_subset_coe.mp $ by simpa using hs.subset_span_extend (subset_univ s)) lemma extend_apply_self (hs : linear_independent K (coe : s β†’ V)) (x : hs.extend _) : basis.extend hs x = x := basis.mk_apply _ _ _ @[simp] lemma coe_extend (hs : linear_independent K (coe : s β†’ V)) : ⇑(basis.extend hs) = coe := funext (extend_apply_self hs) lemma range_extend (hs : linear_independent K (coe : s β†’ V)) : range (basis.extend hs) = hs.extend (subset_univ _) := by rw [coe_extend, subtype.range_coe_subtype, set_of_mem_eq] /-- If `v` is a linear independent family of vectors, extend it to a basis indexed by a sum type. -/ noncomputable def sum_extend (hs : linear_independent K v) : basis (ΞΉ βŠ• _) K V := let s := set.range v, e : ΞΉ ≃ s := equiv.of_injective v hs.injective, b := hs.to_subtype_range.extend (subset_univ (set.range v)) in (basis.extend hs.to_subtype_range).reindex $ equiv.symm $ calc ΞΉ βŠ• (b \ s : set V) ≃ s βŠ• (b \ s : set V) : equiv.sum_congr e (equiv.refl _) ... ≃ b : equiv.set.sum_diff_subset (hs.to_subtype_range.subset_extend _) lemma subset_extend {s : set V} (hs : linear_independent K (coe : s β†’ V)) : s βŠ† hs.extend (set.subset_univ _) := hs.subset_extend _ section variables (K V) /-- A set used to index `basis.of_vector_space`. -/ noncomputable def of_vector_space_index : set V := (linear_independent_empty K V).extend (subset_univ _) /-- Each vector space has a basis. -/ noncomputable def of_vector_space : basis (of_vector_space_index K V) K V := basis.extend (linear_independent_empty K V) lemma of_vector_space_apply_self (x : of_vector_space_index K V) : of_vector_space K V x = x := basis.mk_apply _ _ _ @[simp] lemma coe_of_vector_space : ⇑(of_vector_space K V) = coe := funext (Ξ» x, of_vector_space_apply_self K V x) lemma of_vector_space_index.linear_independent : linear_independent K (coe : of_vector_space_index K V β†’ V) := by { convert (of_vector_space K V).linear_independent, ext x, rw of_vector_space_apply_self } lemma range_of_vector_space : range (of_vector_space K V) = of_vector_space_index K V := range_extend _ lemma exists_basis : βˆƒ s : set V, nonempty (basis s K V) := ⟨of_vector_space_index K V, ⟨of_vector_space K V⟩⟩ end end exists_basis end basis open fintype variables (K V) theorem vector_space.card_fintype [fintype K] [fintype V] : βˆƒ n : β„•, card V = (card K) ^ n := ⟨card (basis.of_vector_space_index K V), module.card_fintype (basis.of_vector_space K V)⟩ section atoms_of_submodule_lattice variables {K V} /-- For a module over a division ring, the span of a nonzero element is an atom of the lattice of submodules. -/ lemma nonzero_span_atom (v : V) (hv : v β‰  0) : is_atom (span K {v} : submodule K V) := begin split, { rw submodule.ne_bot_iff, exact ⟨v, ⟨mem_span_singleton_self v, hv⟩⟩ }, { intros T hT, by_contra, apply hT.2, change (span K {v}) ≀ T, simp_rw [span_singleton_le_iff_mem, ← ne.def, submodule.ne_bot_iff] at *, rcases h with ⟨s, ⟨hs, hz⟩⟩, cases (mem_span_singleton.1 (hT.1 hs)) with a ha, have h : a β‰  0, by { intro h, rw [h, zero_smul] at ha, exact hz ha.symm }, apply_fun (Ξ» x, a⁻¹ β€’ x) at ha, simp_rw [← mul_smul, inv_mul_cancel h, one_smul, ha] at *, exact smul_mem T _ hs}, end /-- The atoms of the lattice of submodules of a module over a division ring are the submodules equal to the span of a nonzero element of the module. -/ lemma atom_iff_nonzero_span (W : submodule K V) : is_atom W ↔ βˆƒ (v : V) (hv : v β‰  0), W = span K {v} := begin refine ⟨λ h, _, Ξ» h, _ ⟩, { cases h with hbot h, rcases ((submodule.ne_bot_iff W).1 hbot) with ⟨v, ⟨hW, hv⟩⟩, refine ⟨v, ⟨hv, _⟩⟩, by_contra heq, specialize h (span K {v}), rw [span_singleton_eq_bot, lt_iff_le_and_ne] at h, exact hv (h ⟨(span_singleton_le_iff_mem v W).2 hW, ne.symm heq⟩) }, { rcases h with ⟨v, ⟨hv, rfl⟩⟩, exact nonzero_span_atom v hv }, end /-- The lattice of submodules of a module over a division ring is atomistic. -/ instance : is_atomistic (submodule K V) := { eq_Sup_atoms := begin intro W, use {T : submodule K V | βˆƒ (v : V) (hv : v ∈ W) (hz : v β‰  0), T = span K {v}}, refine ⟨submodule_eq_Sup_le_nonzero_spans W, _⟩, rintros _ ⟨w, ⟨_, ⟨hw, rfl⟩⟩⟩, exact nonzero_span_atom w hw end } end atoms_of_submodule_lattice end division_ring section field variables [field K] [add_comm_group V] [add_comm_group V'] [module K V] [module K V'] variables {v : ΞΉ β†’ V} {s t : set V} {x y z : V} lemma linear_map.exists_left_inverse_of_injective (f : V β†’β‚—[K] V') (hf_inj : f.ker = βŠ₯) : βˆƒg:V' β†’β‚—[K] V, g.comp f = linear_map.id := begin let B := basis.of_vector_space_index K V, let hB := basis.of_vector_space K V, have hBβ‚€ : _ := hB.linear_independent.to_subtype_range, have : linear_independent K (Ξ» x, x : f '' B β†’ V'), { have h₁ : linear_independent K (Ξ» (x : β†₯(⇑f '' range (basis.of_vector_space _ _))), ↑x) := @linear_independent.image_subtype _ _ _ _ _ _ _ _ _ f hBβ‚€ (show disjoint _ _, by simp [hf_inj]), rwa [basis.range_of_vector_space K V] at h₁ }, let C := this.extend (subset_univ _), have BC := this.subset_extend (subset_univ _), let hC := basis.extend this, haveI : inhabited V := ⟨0⟩, refine ⟨hC.constr K (C.restrict (inv_fun f)), hB.ext (Ξ» b, _)⟩, rw image_subset_iff at BC, have fb_eq : f b = hC ⟨f b, BC b.2⟩, { change f b = basis.extend this _, rw [basis.extend_apply_self, subtype.coe_mk] }, dsimp [hB], rw [basis.of_vector_space_apply_self, fb_eq, hC.constr_basis], 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 module.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' β†’β‚—[K] V, f.comp g = linear_map.id := begin let C := basis.of_vector_space_index K V', let hC := basis.of_vector_space K V', haveI : inhabited V := ⟨0⟩, use hC.constr K (C.restrict (inv_fun f)), refine hC.ext (Ξ» c, _), rw [linear_map.comp_apply, hC.constr_basis], simp [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 set_like.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 (((V β§Έ p) Γ— 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) end field
61ab22257e4163ea7ff26767f60d2ee53b56e011
4fa161becb8ce7378a709f5992a594764699e268
/src/measure_theory/set_integral.lean
4ba1b93072c392ee2c60069d1d00bc372d2796dc
[ "Apache-2.0" ]
permissive
laughinggas/mathlib
e4aa4565ae34e46e834434284cb26bd9d67bc373
86dcd5cda7a5017c8b3c8876c89a510a19d49aad
refs/heads/master
1,669,496,232,688
1,592,831,995,000
1,592,831,995,000
274,155,979
0
0
Apache-2.0
1,592,835,190,000
1,592,835,189,000
null
UTF-8
Lean
false
false
14,976
lean
/- Copyright (c) 2020 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou -/ import measure_theory.bochner_integration import measure_theory.indicator_function import measure_theory.lebesgue_measure /-! # Set integral Integrate a function over a subset of a measure space. ## Main definitions `measurable_on`, `integrable_on`, `integral_on` ## Notation `∫ a in s, f a` is `measure_theory.integral (s.indicator f)` -/ noncomputable theory open set filter topological_space measure_theory measure_theory.simple_func open_locale classical topological_space interval big_operators universes u v w variables {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} section measurable_on variables [measurable_space Ξ±] [measurable_space Ξ²] [has_zero Ξ²] {s : set Ξ±} {f : Ξ± β†’ Ξ²} /-- `measurable_on s f` means `f` is measurable over the set `s`. -/ def measurable_on (s : set Ξ±) (f : Ξ± β†’ Ξ²) : Prop := measurable (s.indicator f) @[simp] lemma measurable_on_empty (f : Ξ± β†’ Ξ²) : measurable_on βˆ… f := by { rw [measurable_on, indicator_empty], exact measurable_const } @[simp] lemma measurable.measurable_on_univ (hf : measurable f) : measurable_on univ f := hf.if is_measurable.univ measurable_const @[simp] lemma measurable_on_singleton {Ξ±} [topological_space Ξ±] [t1_space Ξ±] [measurable_space Ξ±] [opens_measurable_space Ξ±] {a : Ξ±} {f : Ξ± β†’ Ξ²} : measurable_on {a} f := Ξ» s hs, show is_measurable ((indicator {a} f)⁻¹' s), begin rw indicator_preimage, refine is_measurable.union _ (is_measurable_singleton.compl.inter $ measurable_const.preimage hs), by_cases h : a ∈ f⁻¹' s, { rw inter_eq_self_of_subset_left, { exact is_measurable_singleton }, rwa singleton_subset_iff }, rw [singleton_inter_eq_empty.2 h], exact is_measurable.empty end lemma is_measurable.inter_preimage {B : set Ξ²} (hs : is_measurable s) (hB : is_measurable B) (hf : measurable_on s f): is_measurable (s ∩ f ⁻¹' B) := begin replace hf : is_measurable ((indicator s f)⁻¹' B) := hf B hB, rw indicator_preimage at hf, replace hf := hf.diff _, rwa union_diff_cancel_right at hf, { assume a, simp {contextual := tt} }, exact hs.compl.inter (measurable_const.preimage hB) end lemma measurable.measurable_on (hs : is_measurable s) (hf : measurable f) : measurable_on s f := hf.if hs measurable_const lemma measurable_on.subset {t : set Ξ±} (hs : is_measurable s) (h : s βŠ† t) (hf : measurable_on t f) : measurable_on s f := begin have : measurable_on s (indicator t f) := measurable.measurable_on hs hf, simp only [measurable_on, indicator_indicator] at this, rwa [inter_eq_self_of_subset_left h] at this end lemma measurable_on.union {t : set Ξ±} {f : Ξ± β†’ Ξ²} (hs : is_measurable s) (ht : is_measurable t) (hsm : measurable_on s f) (htm : measurable_on t f) : measurable_on (s βˆͺ t) f := begin assume B hB, show is_measurable ((indicator (s βˆͺ t) f)⁻¹' B), rw indicator_preimage, refine is_measurable.union _ ((hs.union ht).compl.inter (measurable_const.preimage hB)), simp only [union_inter_distrib_right], exact (hs.inter_preimage hB hsm).union (ht.inter_preimage hB htm) end end measurable_on section integrable_on variables [measure_space Ξ±] [normed_group Ξ²] {s t : set Ξ±} {f g : Ξ± β†’ Ξ²} /-- `integrable_on s f` means `f` is integrable over the set `s`. -/ def integrable_on (s : set Ξ±) (f : Ξ± β†’ Ξ²) : Prop := integrable (s.indicator f) lemma integrable_on_congr (h : βˆ€x, x ∈ s β†’ f x = g x) : integrable_on s f ↔ integrable_on s g := by simp only [integrable_on, indicator_congr h] lemma integrable_on_congr_ae (h : βˆ€β‚˜ x, x ∈ s β†’ f x = g x) : integrable_on s f ↔ integrable_on s g := by { apply integrable_congr_ae, exact indicator_congr_ae h } @[simp] lemma integrable_on_empty (f : Ξ± β†’ Ξ²) : integrable_on βˆ… f := by { simp only [integrable_on, indicator_empty], apply integrable_zero } lemma measure_theory.integrable.integrable_on (s : set Ξ±) (hf : integrable f) : integrable_on s f := by { refine integrable_of_le (Ξ»a, _) hf, apply norm_indicator_le_norm_self } lemma integrable_on.subset (h : s βŠ† t) : integrable_on t f β†’ integrable_on s f := by { apply integrable_of_le_ae, filter_upwards [] norm_indicator_le_of_subset h _ } variables {π•œ : Type*} [normed_field π•œ] [normed_space π•œ Ξ²] lemma integrable_on.smul (s : set Ξ±) (c : π•œ) {f : Ξ± β†’ Ξ²} : integrable_on s f β†’ integrable_on s (Ξ»a, c β€’ f a) := by { simp only [integrable_on, indicator_smul], apply integrable.smul } lemma integrable_on.mul_left (s : set Ξ±) (r : ℝ) {f : Ξ± β†’ ℝ} (hf : integrable_on s f) : integrable_on s (Ξ»a, r * f a) := by { simp only [smul_eq_mul.symm], exact hf.smul s r } lemma integrable_on.mul_right (s : set Ξ±) (r : ℝ) {f : Ξ± β†’ ℝ} (hf : integrable_on s f) : integrable_on s (Ξ»a, f a * r) := by { simp only [mul_comm], exact hf.mul_left _ _ } lemma integrable_on.divide (s : set Ξ±) (r : ℝ) {f : Ξ± β†’ ℝ} (hf : integrable_on s f) : integrable_on s (Ξ»a, f a / r) := by { simp only [div_eq_mul_inv], exact hf.mul_right _ _ } lemma integrable_on.add [measurable_space Ξ²] [opens_measurable_space Ξ²] (hfm : measurable_on s f) (hfi : integrable_on s f) (hgm : measurable_on s g) (hgi : integrable_on s g) : integrable_on s (Ξ»a, f a + g a) := by { rw [integrable_on, indicator_add], exact hfi.add hfm hgm hgi } lemma integrable_on.neg (hf : integrable_on s f) : integrable_on s (Ξ»a, -f a) := by { rw [integrable_on, indicator_neg], exact hf.neg } lemma integrable_on.sub [measurable_space Ξ²] [opens_measurable_space Ξ²] (hfm : measurable_on s f) (hfi : integrable_on s f) (hgm : measurable_on s g) (hgi : integrable_on s g) : integrable_on s (Ξ»a, f a - g a) := by { rw [integrable_on, indicator_sub], exact hfi.sub hfm hgm hgi } lemma integrable_on.union [measurable_space Ξ²] [opens_measurable_space Ξ²] (hs : is_measurable s) (ht : is_measurable t) (hsm : measurable_on s f) (hsi : integrable_on s f) (htm : measurable_on t f) (hti : integrable_on t f) : integrable_on (s βˆͺ t) f := begin rw ← union_diff_self, rw [integrable_on, indicator_union_of_disjoint], { refine integrable.add hsm hsi (htm.subset _ _) (hti.subset _), { exact ht.diff hs }, { exact diff_subset _ _ }, { exact diff_subset _ _ } }, exact disjoint_diff end lemma integrable_on_norm_iff (s : set Ξ±) (f : Ξ± β†’ Ξ²) : integrable_on s (Ξ»a, βˆ₯f aβˆ₯) ↔ integrable_on s f := begin simp only [integrable_on], convert ← integrable_norm_iff (indicator s f), funext, apply norm_indicator_eq_indicator_norm end end integrable_on section integral_on variables [measure_space Ξ±] [normed_group Ξ²] [second_countable_topology Ξ²] [normed_space ℝ Ξ²] [complete_space Ξ²] [measurable_space Ξ²] [borel_space Ξ²] {s t : set Ξ±} {f g : Ξ± β†’ Ξ²} open set notation `∫` binders ` in ` s `, ` r:(scoped f, measure_theory.integral (set.indicator s f)) := r lemma integral_on_undef (h : Β¬ (measurable_on s f ∧ integrable_on s f)) : (∫ a in s, f a) = 0 := integral_undef h lemma integral_on_non_measurable (h : Β¬ measurable_on s f) : (∫ a in s, f a) = 0 := integral_non_measurable h lemma integral_on_non_integrable (h : Β¬ integrable_on s f) : (∫ a in s, f a) = 0 := integral_non_integrable h variables (Ξ²) lemma integral_on_zero (s : set Ξ±) : (∫ a in s, (0:Ξ²)) = 0 := by simp variables {Ξ²} lemma integral_on_congr (h : βˆ€ a ∈ s, f a = g a) : (∫ a in s, f a) = (∫ a in s, g a) := by simp only [indicator_congr h] lemma integral_on_congr_of_ae_eq (hf : measurable_on s f) (hg : measurable_on s g) (h : βˆ€β‚˜ a, a ∈ s β†’ f a = g a) : (∫ a in s, f a) = (∫ a in s, g a) := integral_congr_ae hf hg (indicator_congr_ae h) lemma integral_on_congr_of_set (hsm : measurable_on s f) (htm : measurable_on t f) (h : βˆ€β‚˜ a, a ∈ s ↔ a ∈ t) : (∫ a in s, f a) = (∫ a in t, f a) := integral_congr_ae hsm htm $ indicator_congr_of_set h variables (s t) lemma integral_on_smul (r : ℝ) (f : Ξ± β†’ Ξ²) : (∫ a in s, r β€’ (f a)) = r β€’ (∫ a in s, f a) := by rw [← integral_smul, indicator_smul] lemma integral_on_mul_left (r : ℝ) (f : Ξ± β†’ ℝ) : (∫ a in s, r * (f a)) = r * (∫ a in s, f a) := integral_on_smul s r f lemma integral_on_mul_right (r : ℝ) (f : Ξ± β†’ ℝ) : (∫ a in s, (f a) * r) = (∫ a in s, f a) * r := by { simp only [mul_comm], exact integral_on_mul_left s r f } lemma integral_on_div (r : ℝ) (f : Ξ± β†’ ℝ) : (∫ a in s, (f a) / r) = (∫ a in s, f a) / r := by { simp only [div_eq_mul_inv], apply integral_on_mul_right } lemma integral_on_neg (f : Ξ± β†’ Ξ²) : (∫ a in s, -f a) = - (∫ a in s, f a) := by { simp only [indicator_neg], exact integral_neg _ } variables {s t} lemma integral_on_add {s : set Ξ±} (hfm : measurable_on s f) (hfi : integrable_on s f) (hgm : measurable_on s g) (hgi : integrable_on s g) : (∫ a in s, f a + g a) = (∫ a in s, f a) + (∫ a in s, g a) := by { simp only [indicator_add], exact integral_add hfm hfi hgm hgi } lemma integral_on_sub (hfm : measurable_on s f) (hfi : integrable_on s f) (hgm : measurable_on s g) (hgi : integrable_on s g) : (∫ a in s, f a - g a) = (∫ a in s, f a) - (∫ a in s, g a) := by { simp only [indicator_sub], exact integral_sub hfm hfi hgm hgi } lemma integral_on_le_integral_on_ae {f g : Ξ± β†’ ℝ} (hfm : measurable_on s f) (hfi : integrable_on s f) (hgm : measurable_on s g) (hgi : integrable_on s g) (h : βˆ€β‚˜ a, a ∈ s β†’ f a ≀ g a) : (∫ a in s, f a) ≀ (∫ a in s, g a) := begin apply integral_le_integral_ae hfm hfi hgm hgi, apply indicator_le_indicator_ae, exact h end lemma integral_on_le_integral_on {f g : Ξ± β†’ ℝ} (hfm : measurable_on s f) (hfi : integrable_on s f) (hgm : measurable_on s g) (hgi : integrable_on s g) (h : βˆ€ a, a ∈ s β†’ f a ≀ g a) : (∫ a in s, f a) ≀ (∫ a in s, g a) := integral_on_le_integral_on_ae hfm hfi hgm hgi $ by filter_upwards [] h lemma integral_on_union (hsm : measurable_on s f) (hsi : integrable_on s f) (htm : measurable_on t f) (hti : integrable_on t f) (h : disjoint s t) : (∫ a in (s βˆͺ t), f a) = (∫ a in s, f a) + (∫ a in t, f a) := by { rw [indicator_union_of_disjoint h, integral_add hsm hsi htm hti] } lemma integral_on_union_ae (hs : is_measurable s) (ht : is_measurable t) (hsm : measurable_on s f) (hsi : integrable_on s f) (htm : measurable_on t f) (hti : integrable_on t f) (h : βˆ€β‚˜ a, a βˆ‰ s ∩ t) : (∫ a in (s βˆͺ t), f a) = (∫ a in s, f a) + (∫ a in t, f a) := begin have := integral_congr_ae _ _ (indicator_union_ae h f), rw [this, integral_add hsm hsi htm hti], { exact hsm.union hs ht htm }, { exact measurable.add hsm htm } end lemma integral_on_nonneg_of_ae {f : Ξ± β†’ ℝ} (hf : βˆ€β‚˜ a, a ∈ s β†’ 0 ≀ f a) : (0:ℝ) ≀ (∫ a in s, f a) := integral_nonneg_of_ae $ by { filter_upwards [hf] Ξ» a h, indicator_nonneg' h } lemma integral_on_nonneg {f : Ξ± β†’ ℝ} (hf : βˆ€ a, a ∈ s β†’ 0 ≀ f a) : (0:ℝ) ≀ (∫ a in s, f a) := integral_on_nonneg_of_ae $ univ_mem_sets' hf lemma integral_on_nonpos_of_ae {f : Ξ± β†’ ℝ} (hf : βˆ€β‚˜ a, a ∈ s β†’ f a ≀ 0) : (∫ a in s, f a) ≀ 0 := integral_nonpos_of_nonpos_ae $ by { filter_upwards [hf] Ξ» a h, indicator_nonpos' h } lemma integral_on_nonpos {f : Ξ± β†’ ℝ} (hf : βˆ€ a, a ∈ s β†’ f a ≀ 0) : (∫ a in s, f a) ≀ 0 := integral_on_nonpos_of_ae $ univ_mem_sets' hf lemma tendsto_integral_on_of_monotone {s : β„• β†’ set Ξ±} {f : Ξ± β†’ Ξ²} (hsm : βˆ€i, is_measurable (s i)) (h_mono : monotone s) (hfm : measurable_on (Union s) f) (hfi : integrable_on (Union s) f) : tendsto (Ξ»i, ∫ a in (s i), f a) at_top (nhds (∫ a in (Union s), f a)) := let bound : Ξ± β†’ ℝ := indicator (Union s) (Ξ»a, βˆ₯f aβˆ₯) in begin apply tendsto_integral_of_dominated_convergence, { assume i, exact hfm.subset (hsm i) (subset_Union _ _) }, { assumption }, { show integrable_on (Union s) (Ξ»a, βˆ₯f aβˆ₯), rwa integrable_on_norm_iff }, { assume i, apply ae_of_all, assume a, rw [norm_indicator_eq_indicator_norm], exact indicator_le_indicator_of_subset (subset_Union _ _) (Ξ»a, norm_nonneg _) _ }, { filter_upwards [] Ξ»a, le_trans (tendsto_indicator_of_monotone _ h_mono _ _) (pure_le_nhds _) } end lemma tendsto_integral_on_of_antimono (s : β„• β†’ set Ξ±) (f : Ξ± β†’ Ξ²) (hsm : βˆ€i, is_measurable (s i)) (h_mono : βˆ€i j, i ≀ j β†’ s j βŠ† s i) (hfm : measurable_on (s 0) f) (hfi : integrable_on (s 0) f) : tendsto (Ξ»i, ∫ a in (s i), f a) at_top (nhds (∫ a in (Inter s), f a)) := let bound : Ξ± β†’ ℝ := indicator (s 0) (Ξ»a, βˆ₯f aβˆ₯) in begin apply tendsto_integral_of_dominated_convergence, { assume i, refine hfm.subset (hsm i) (h_mono _ _ (zero_le _)) }, { exact hfm.subset (is_measurable.Inter hsm) (Inter_subset _ _) }, { show integrable_on (s 0) (Ξ»a, βˆ₯f aβˆ₯), rwa integrable_on_norm_iff }, { assume i, apply ae_of_all, assume a, rw [norm_indicator_eq_indicator_norm], refine indicator_le_indicator_of_subset (h_mono _ _ (zero_le _)) (Ξ»a, norm_nonneg _) _ }, { filter_upwards [] Ξ»a, le_trans (tendsto_indicator_of_antimono _ h_mono _ _) (pure_le_nhds _) } end -- TODO : prove this for an encodable type -- by proving an encodable version of `filter.is_countably_generated_at_top_finset_nat ` lemma integral_on_Union (s : β„• β†’ set Ξ±) (f : Ξ± β†’ Ξ²) (hm : βˆ€i, is_measurable (s i)) (hd : βˆ€ i j, i β‰  j β†’ s i ∩ s j = βˆ…) (hfm : measurable_on (Union s) f) (hfi : integrable_on (Union s) f) : (∫ a in (Union s), f a) = βˆ‘'i, ∫ a in s i, f a := suffices h : tendsto (Ξ»n:finset β„•, βˆ‘ i in n, ∫ a in s i, f a) at_top (𝓝 $ (∫ a in (Union s), f a)), by { rwa tsum_eq_has_sum }, begin have : (Ξ»n:finset β„•, βˆ‘ i in n, ∫ a in s i, f a) = Ξ»n:finset β„•, ∫ a in (⋃i∈n, s i), f a, { funext, rw [← integral_finset_sum, indicator_finset_bUnion], { assume i hi j hj hij, exact hd i j hij }, { assume i, refine hfm.subset (hm _) (subset_Union _ _) }, { assume i, refine hfi.subset (subset_Union _ _) } }, rw this, refine tendsto_integral_filter_of_dominated_convergence _ _ _ _ _ _ _, { exact indicator (Union s) (Ξ» a, βˆ₯f aβˆ₯) }, { exact is_countably_generated_at_top_finset_nat }, { refine univ_mem_sets' (Ξ» n, _), simp only [mem_set_of_eq], refine hfm.subset (is_measurable.Union (Ξ» i, is_measurable.Union_Prop (Ξ»h, hm _))) (bUnion_subset_Union _ _), }, { assumption }, { refine univ_mem_sets' (Ξ» n, univ_mem_sets' $ _), simp only [mem_set_of_eq], assume a, rw ← norm_indicator_eq_indicator_norm, refine norm_indicator_le_of_subset (bUnion_subset_Union _ _) _ _ }, { rw [← integrable_on, integrable_on_norm_iff], assumption }, { filter_upwards [] Ξ»a, le_trans (tendsto_indicator_bUnion_finset _ _ _) (pure_le_nhds _) } end end integral_on
f9f56aa338fd70563b0a21d297a10d27d8e23a5b
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/unfold_rec3.lean
8ee62ebf2aa29a0a20ab86401d5bb218e6a1a47d
[ "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
244
lean
open nat definition nrec [recursor] := @nat.rec definition myadd x y := nrec y (Ξ» n r, succ r) x theorem myadd_zero : βˆ€ n, myadd n 0 = n := begin intro n, induction n with n ih, reflexivity, esimp [myadd], state, rewrite ih end
4afb46ee3ebbb8aa34eb53c1733a2c7681407d17
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/algebra/group_power/identities.lean
ad41dc3407a13aaa8278eaf69ec1517218b069d7
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
3,195
lean
/- Copyright (c) 2020 Bryan Gin-ge Chen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bryan Gin-ge Chen, Kevin Lacker -/ import tactic.ring /-! # Identities This file contains some "named" commutative ring identities. -/ variables {R : Type*} [comm_ring R] {a b x₁ xβ‚‚ x₃ xβ‚„ xβ‚… x₆ x₇ xβ‚ˆ y₁ yβ‚‚ y₃ yβ‚„ yβ‚… y₆ y₇ yβ‚ˆ n : R} /-- Brahmagupta-Fibonacci identity or Diophantus identity, see <https://en.wikipedia.org/wiki/Brahmagupta%E2%80%93Fibonacci_identity>. This sign choice here corresponds to the signs obtained by multiplying two complex numbers. -/ theorem sq_add_sq_mul_sq_add_sq : (x₁^2 + xβ‚‚^2) * (y₁^2 + yβ‚‚^2) = (x₁*y₁ - xβ‚‚*yβ‚‚)^2 + (x₁*yβ‚‚ + xβ‚‚*y₁)^2 := by ring /-- Brahmagupta's identity, see <https://en.wikipedia.org/wiki/Brahmagupta%27s_identity> -/ theorem sq_add_mul_sq_mul_sq_add_mul_sq : (x₁^2 + n*xβ‚‚^2) * (y₁^2 + n*yβ‚‚^2) = (x₁*y₁ - n*xβ‚‚*yβ‚‚)^2 + n*(x₁*yβ‚‚ + xβ‚‚*y₁)^2 := by ring /-- Sophie Germain's identity, see <https://www.cut-the-knot.org/blue/SophieGermainIdentity.shtml>. -/ theorem pow_four_add_four_mul_pow_four : a^4 + 4*b^4 = ((a - b)^2 + b^2) * ((a + b)^2 + b^2) := by ring /-- Sophie Germain's identity, see <https://www.cut-the-knot.org/blue/SophieGermainIdentity.shtml>. -/ theorem pow_four_add_four_mul_pow_four' : a^4 + 4*b^4 = (a^2 - 2*a*b + 2*b^2) * (a^2 + 2*a*b + 2*b^2) := by ring /-- Euler's four-square identity, see <https://en.wikipedia.org/wiki/Euler%27s_four-square_identity>. This sign choice here corresponds to the signs obtained by multiplying two quaternions. -/ theorem sum_four_sq_mul_sum_four_sq : (x₁^2 + xβ‚‚^2 + x₃^2 + xβ‚„^2) * (y₁^2 + yβ‚‚^2 + y₃^2 + yβ‚„^2) = (x₁*y₁ - xβ‚‚*yβ‚‚ - x₃*y₃ - xβ‚„*yβ‚„)^2 + (x₁*yβ‚‚ + xβ‚‚*y₁ + x₃*yβ‚„ - xβ‚„*y₃)^2 + (x₁*y₃ - xβ‚‚*yβ‚„ + x₃*y₁ + xβ‚„*yβ‚‚)^2 + (x₁*yβ‚„ + xβ‚‚*y₃ - x₃*yβ‚‚ + xβ‚„*y₁)^2 := by ring /-- Degen's eight squares identity, see <https://en.wikipedia.org/wiki/Degen%27s_eight-square_identity>. This sign choice here corresponds to the signs obtained by multiplying two octonions. -/ theorem sum_eight_sq_mul_sum_eight_sq : (x₁^2 + xβ‚‚^2 + x₃^2 + xβ‚„^2 + xβ‚…^2 + x₆^2 + x₇^2 + xβ‚ˆ^2) * (y₁^2 + yβ‚‚^2 + y₃^2 + yβ‚„^2 + yβ‚…^2 + y₆^2 + y₇^2 + yβ‚ˆ^2) = (x₁*y₁ - xβ‚‚*yβ‚‚ - x₃*y₃ - xβ‚„*yβ‚„ - xβ‚…*yβ‚… - x₆*y₆ - x₇*y₇ - xβ‚ˆ*yβ‚ˆ)^2 + (x₁*yβ‚‚ + xβ‚‚*y₁ + x₃*yβ‚„ - xβ‚„*y₃ + xβ‚…*y₆ - x₆*yβ‚… - x₇*yβ‚ˆ + xβ‚ˆ*y₇)^2 + (x₁*y₃ - xβ‚‚*yβ‚„ + x₃*y₁ + xβ‚„*yβ‚‚ + xβ‚…*y₇ + x₆*yβ‚ˆ - x₇*yβ‚… - xβ‚ˆ*y₆)^2 + (x₁*yβ‚„ + xβ‚‚*y₃ - x₃*yβ‚‚ + xβ‚„*y₁ + xβ‚…*yβ‚ˆ - x₆*y₇ + x₇*y₆ - xβ‚ˆ*yβ‚…)^2 + (x₁*yβ‚… - xβ‚‚*y₆ - x₃*y₇ - xβ‚„*yβ‚ˆ + xβ‚…*y₁ + x₆*yβ‚‚ + x₇*y₃ + xβ‚ˆ*yβ‚„)^2 + (x₁*y₆ + xβ‚‚*yβ‚… - x₃*yβ‚ˆ + xβ‚„*y₇ - xβ‚…*yβ‚‚ + x₆*y₁ - x₇*yβ‚„ + xβ‚ˆ*y₃)^2 + (x₁*y₇ + xβ‚‚*yβ‚ˆ + x₃*yβ‚… - xβ‚„*y₆ - xβ‚…*y₃ + x₆*yβ‚„ + x₇*y₁ - xβ‚ˆ*yβ‚‚)^2 + (x₁*yβ‚ˆ - xβ‚‚*y₇ + x₃*y₆ + xβ‚„*yβ‚… - xβ‚…*yβ‚„ - x₆*y₃ + x₇*yβ‚‚ + xβ‚ˆ*y₁)^2 := by ring
65e188ba795e24620270f63d3cebc92a5a389acb
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/group_theory/group_action/quotient.lean
a0c81928181a78ee3d3a20525a488cd2fffea77b
[ "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
15,479
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Thomas Browning -/ import algebra.hom.group_action import data.fintype.big_operators import dynamics.periodic_pts import group_theory.group_action.conj_act import group_theory.commutator import group_theory.coset /-! # Properties of group actions involving quotient groups This file proves properties of group actions which use the quotient group construction, notably * the orbit-stabilizer theorem `card_orbit_mul_card_stabilizer_eq_card_group` * the class formula `card_eq_sum_card_group_div_card_stabilizer'` * Burnside's lemma `sum_card_fixed_by_eq_card_orbits_mul_card_group` -/ universes u v w variables {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} open function open_locale big_operators namespace mul_action variables [group Ξ±] section quotient_action open subgroup mul_opposite quotient_group variables (Ξ²) [monoid Ξ²] [mul_action Ξ² Ξ±] (H : subgroup Ξ±) /-- A typeclass for when a `mul_action Ξ² Ξ±` descends to the quotient `Ξ± β§Έ H`. -/ class quotient_action : Prop := (inv_mul_mem : βˆ€ (b : Ξ²) {a a' : Ξ±}, a⁻¹ * a' ∈ H β†’ (b β€’ a)⁻¹ * (b β€’ a') ∈ H) /-- A typeclass for when an `add_action Ξ² Ξ±` descends to the quotient `Ξ± β§Έ H`. -/ class _root_.add_action.quotient_action {Ξ± : Type*} (Ξ² : Type*) [add_group Ξ±] [add_monoid Ξ²] [add_action Ξ² Ξ±] (H : add_subgroup Ξ±) : Prop := (inv_mul_mem : βˆ€ (b : Ξ²) {a a' : Ξ±}, -a + a' ∈ H β†’ -(b +α΅₯ a) + (b +α΅₯ a') ∈ H) attribute [to_additive add_action.quotient_action] mul_action.quotient_action @[to_additive] instance left_quotient_action : quotient_action Ξ± H := ⟨λ _ _ _ _, by rwa [smul_eq_mul, smul_eq_mul, mul_inv_rev, mul_assoc, inv_mul_cancel_left]⟩ @[to_additive] instance right_quotient_action : quotient_action H.normalizer.opposite H := ⟨λ b c _ _, by rwa [smul_def, smul_def, smul_eq_mul_unop, smul_eq_mul_unop, mul_inv_rev, ←mul_assoc, mem_normalizer_iff'.mp b.prop, mul_assoc, mul_inv_cancel_left]⟩ @[to_additive] instance right_quotient_action' [hH : H.normal] : quotient_action αᡐᡒᡖ H := ⟨λ _ _ _ _, by rwa [smul_eq_mul_unop, smul_eq_mul_unop, mul_inv_rev, mul_assoc, hH.mem_comm_iff, mul_assoc, mul_inv_cancel_right]⟩ @[to_additive] instance quotient [quotient_action Ξ² H] : mul_action Ξ² (Ξ± β§Έ H) := { smul := Ξ» b, quotient.map' ((β€’) b) (Ξ» a a' h, left_rel_apply.mpr $ quotient_action.inv_mul_mem b $ left_rel_apply.mp h), one_smul := Ξ» q, quotient.induction_on' q (Ξ» a, congr_arg quotient.mk' (one_smul Ξ² a)), mul_smul := Ξ» b b' q, quotient.induction_on' q (Ξ» a, congr_arg quotient.mk' (mul_smul b b' a)) } variables {Ξ²} @[simp, to_additive] lemma quotient.smul_mk [quotient_action Ξ² H] (b : Ξ²) (a : Ξ±) : (b β€’ quotient_group.mk a : Ξ± β§Έ H) = quotient_group.mk (b β€’ a) := rfl @[simp, to_additive] lemma quotient.smul_coe [quotient_action Ξ² H] (b : Ξ²) (a : Ξ±) : (b β€’ a : Ξ± β§Έ H) = ↑(b β€’ a) := rfl @[simp, to_additive] lemma quotient.mk_smul_out' [quotient_action Ξ² H] (b : Ξ²) (q : Ξ± β§Έ H) : quotient_group.mk (b β€’ q.out') = b β€’ q := by rw [←quotient.smul_mk, quotient_group.out_eq'] @[simp, to_additive] lemma quotient.coe_smul_out' [quotient_action Ξ² H] (b : Ξ²) (q : Ξ± β§Έ H) : ↑(b β€’ q.out') = b β€’ q := quotient.mk_smul_out' H b q lemma _root_.quotient_group.out'_conj_pow_minimal_period_mem (a : Ξ±) (q : Ξ± β§Έ H) : q.out'⁻¹ * a ^ function.minimal_period ((β€’) a) q * q.out' ∈ H := by rw [mul_assoc, ←quotient_group.eq', quotient_group.out_eq', ←smul_eq_mul, quotient.mk_smul_out', eq_comm, pow_smul_eq_iff_minimal_period_dvd] end quotient_action open quotient_group /-- The canonical map to the left cosets. -/ def _root_.mul_action_hom.to_quotient (H : subgroup Ξ±) : Ξ± β†’[Ξ±] Ξ± β§Έ H := ⟨coe, quotient.smul_coe H⟩ @[simp] lemma _root_.mul_action_hom.to_quotient_apply (H : subgroup Ξ±) (g : Ξ±) : mul_action_hom.to_quotient H g = g := rfl @[to_additive] instance mul_left_cosets_comp_subtype_val (H I : subgroup Ξ±) : mul_action I (Ξ± β§Έ H) := mul_action.comp_hom (Ξ± β§Έ H) (subgroup.subtype I) variables (Ξ±) {Ξ²} [mul_action Ξ± Ξ²] (x : Ξ²) /-- The canonical map from the quotient of the stabilizer to the set. -/ @[to_additive "The canonical map from the quotient of the stabilizer to the set. "] def of_quotient_stabilizer (g : Ξ± β§Έ (mul_action.stabilizer Ξ± x)) : Ξ² := quotient.lift_on' g (β€’x) $ Ξ» g1 g2 H, calc g1 β€’ x = g1 β€’ (g1⁻¹ * g2) β€’ x : congr_arg _ ((left_rel_apply.mp H).symm) ... = g2 β€’ x : by rw [smul_smul, mul_inv_cancel_left] @[simp, to_additive] theorem of_quotient_stabilizer_mk (g : Ξ±) : of_quotient_stabilizer Ξ± x (quotient_group.mk g) = g β€’ x := rfl @[to_additive] theorem of_quotient_stabilizer_mem_orbit (g) : of_quotient_stabilizer Ξ± x g ∈ orbit Ξ± x := quotient.induction_on' g $ Ξ» g, ⟨g, rfl⟩ @[to_additive] theorem of_quotient_stabilizer_smul (g : Ξ±) (g' : Ξ± β§Έ (mul_action.stabilizer Ξ± x)) : of_quotient_stabilizer Ξ± x (g β€’ g') = g β€’ of_quotient_stabilizer Ξ± x g' := quotient.induction_on' g' $ Ξ» _, mul_smul _ _ _ @[to_additive] theorem injective_of_quotient_stabilizer : function.injective (of_quotient_stabilizer Ξ± x) := Ξ» y₁ yβ‚‚, quotient.induction_onβ‚‚' y₁ yβ‚‚ $ Ξ» g₁ gβ‚‚ (H : g₁ β€’ x = gβ‚‚ β€’ x), quotient.sound' $ by { rw [left_rel_apply], show (g₁⁻¹ * gβ‚‚) β€’ x = x, rw [mul_smul, ← H, inv_smul_smul] } /-- Orbit-stabilizer theorem. -/ @[to_additive "Orbit-stabilizer theorem."] noncomputable def orbit_equiv_quotient_stabilizer (b : Ξ²) : orbit Ξ± b ≃ Ξ± β§Έ (stabilizer Ξ± b) := equiv.symm $ equiv.of_bijective (Ξ» g, ⟨of_quotient_stabilizer Ξ± b g, of_quotient_stabilizer_mem_orbit Ξ± b g⟩) ⟨λ x y hxy, injective_of_quotient_stabilizer Ξ± b (by convert congr_arg subtype.val hxy), Ξ» ⟨b, ⟨g, hgb⟩⟩, ⟨g, subtype.eq hgb⟩⟩ /-- Orbit-stabilizer theorem. -/ @[to_additive "Orbit-stabilizer theorem."] noncomputable def orbit_prod_stabilizer_equiv_group (b : Ξ²) : orbit Ξ± b Γ— stabilizer Ξ± b ≃ Ξ± := (equiv.prod_congr (orbit_equiv_quotient_stabilizer Ξ± _) (equiv.refl _)).trans subgroup.group_equiv_quotient_times_subgroup.symm /-- Orbit-stabilizer theorem. -/ @[to_additive "Orbit-stabilizer theorem."] lemma card_orbit_mul_card_stabilizer_eq_card_group (b : Ξ²) [fintype Ξ±] [fintype $ orbit Ξ± b] [fintype $ stabilizer Ξ± b] : fintype.card (orbit Ξ± b) * fintype.card (stabilizer Ξ± b) = fintype.card Ξ± := by rw [← fintype.card_prod, fintype.card_congr (orbit_prod_stabilizer_equiv_group Ξ± b)] @[simp, to_additive] theorem orbit_equiv_quotient_stabilizer_symm_apply (b : Ξ²) (a : Ξ±) : ((orbit_equiv_quotient_stabilizer Ξ± b).symm a : Ξ²) = a β€’ b := rfl @[simp, to_additive] lemma stabilizer_quotient {G} [group G] (H : subgroup G) : mul_action.stabilizer G ((1 : G) : G β§Έ H) = H := by { ext, simp [quotient_group.eq] } variable (Ξ²) local notation `Ξ©` := (quotient $ orbit_rel Ξ± Ξ²) /-- **Class formula** : given `G` a group acting on `X` and `Ο†` a function mapping each orbit of `X` under this action (that is, each element of the quotient of `X` by the relation `orbit_rel G X`) to an element in this orbit, this gives a (noncomputable) bijection between `X` and the disjoint union of `G/Stab(Ο†(Ο‰))` over all orbits `Ο‰`. In most cases you'll want `Ο†` to be `quotient.out'`, so we provide `mul_action.self_equiv_sigma_orbits_quotient_stabilizer` as a special case. -/ @[to_additive "**Class formula** : given `G` an additive group acting on `X` and `Ο†` a function mapping each orbit of `X` under this action (that is, each element of the quotient of `X` by the relation `orbit_rel G X`) to an element in this orbit, this gives a (noncomputable) bijection between `X` and the disjoint union of `G/Stab(Ο†(Ο‰))` over all orbits `Ο‰`. In most cases you'll want `Ο†` to be `quotient.out'`, so we provide `add_action.self_equiv_sigma_orbits_quotient_stabilizer` as a special case. "] noncomputable def self_equiv_sigma_orbits_quotient_stabilizer' {Ο† : Ξ© β†’ Ξ²} (hΟ† : left_inverse quotient.mk' Ο†) : Ξ² ≃ Ξ£ (Ο‰ : Ξ©), Ξ± β§Έ (stabilizer Ξ± (Ο† Ο‰)) := calc Ξ² ≃ Ξ£ (Ο‰ : Ξ©), orbit_rel.quotient.orbit Ο‰ : self_equiv_sigma_orbits' Ξ± Ξ² ... ≃ Ξ£ (Ο‰ : Ξ©), Ξ± β§Έ (stabilizer Ξ± (Ο† Ο‰)) : equiv.sigma_congr_right (Ξ» Ο‰, (equiv.set.of_eq $ orbit_rel.quotient.orbit_eq_orbit_out _ hΟ†).trans $ orbit_equiv_quotient_stabilizer Ξ± (Ο† Ο‰)) /-- **Class formula** for a finite group acting on a finite type. See `mul_action.card_eq_sum_card_group_div_card_stabilizer` for a specialized version using `quotient.out'`. -/ @[to_additive "**Class formula** for a finite group acting on a finite type. See `add_action.card_eq_sum_card_add_group_div_card_stabilizer` for a specialized version using `quotient.out'`."] lemma card_eq_sum_card_group_div_card_stabilizer' [fintype Ξ±] [fintype Ξ²] [fintype Ξ©] [Ξ  (b : Ξ²), fintype $ stabilizer Ξ± b] {Ο† : Ξ© β†’ Ξ²} (hΟ† : left_inverse quotient.mk' Ο†) : fintype.card Ξ² = βˆ‘ (Ο‰ : Ξ©), fintype.card Ξ± / fintype.card (stabilizer Ξ± (Ο† Ο‰)) := begin classical, have : βˆ€ Ο‰ : Ξ©, fintype.card Ξ± / fintype.card β†₯(stabilizer Ξ± (Ο† Ο‰)) = fintype.card (Ξ± β§Έ stabilizer Ξ± (Ο† Ο‰)), { intro Ο‰, rw [fintype.card_congr (@subgroup.group_equiv_quotient_times_subgroup Ξ± _ (stabilizer Ξ± $ Ο† Ο‰)), fintype.card_prod, nat.mul_div_cancel], exact fintype.card_pos_iff.mpr (by apply_instance) }, simp_rw [this, ← fintype.card_sigma, fintype.card_congr (self_equiv_sigma_orbits_quotient_stabilizer' Ξ± Ξ² hΟ†)], end /-- **Class formula**. This is a special case of `mul_action.self_equiv_sigma_orbits_quotient_stabilizer'` with `Ο† = quotient.out'`. -/ @[to_additive "**Class formula**. This is a special case of `add_action.self_equiv_sigma_orbits_quotient_stabilizer'` with `Ο† = quotient.out'`. "] noncomputable def self_equiv_sigma_orbits_quotient_stabilizer : Ξ² ≃ Ξ£ (Ο‰ : Ξ©), Ξ± β§Έ (stabilizer Ξ± Ο‰.out') := self_equiv_sigma_orbits_quotient_stabilizer' Ξ± Ξ² quotient.out_eq' /-- **Class formula** for a finite group acting on a finite type. -/ @[to_additive "**Class formula** for a finite group acting on a finite type."] lemma card_eq_sum_card_group_div_card_stabilizer [fintype Ξ±] [fintype Ξ²] [fintype Ξ©] [Ξ  (b : Ξ²), fintype $ stabilizer Ξ± b] : fintype.card Ξ² = βˆ‘ (Ο‰ : Ξ©), fintype.card Ξ± / fintype.card (stabilizer Ξ± Ο‰.out') := card_eq_sum_card_group_div_card_stabilizer' Ξ± Ξ² quotient.out_eq' /-- **Burnside's lemma** : a (noncomputable) bijection between the disjoint union of all `{x ∈ X | g β€’ x = x}` for `g ∈ G` and the product `G Γ— X/G`, where `G` is a group acting on `X` and `X/G`denotes the quotient of `X` by the relation `orbit_rel G X`. -/ @[to_additive "**Burnside's lemma** : a (noncomputable) bijection between the disjoint union of all `{x ∈ X | g β€’ x = x}` for `g ∈ G` and the product `G Γ— X/G`, where `G` is an additive group acting on `X` and `X/G`denotes the quotient of `X` by the relation `orbit_rel G X`. "] noncomputable def sigma_fixed_by_equiv_orbits_prod_group : (Ξ£ (a : Ξ±), (fixed_by Ξ± Ξ² a)) ≃ Ξ© Γ— Ξ± := calc (Ξ£ (a : Ξ±), fixed_by Ξ± Ξ² a) ≃ {ab : Ξ± Γ— Ξ² // ab.1 β€’ ab.2 = ab.2} : (equiv.subtype_prod_equiv_sigma_subtype _).symm ... ≃ {ba : Ξ² Γ— Ξ± // ba.2 β€’ ba.1 = ba.1} : (equiv.prod_comm Ξ± Ξ²).subtype_equiv (Ξ» ab, iff.rfl) ... ≃ Ξ£ (b : Ξ²), stabilizer Ξ± b : equiv.subtype_prod_equiv_sigma_subtype (Ξ» (b : Ξ²) a, a ∈ stabilizer Ξ± b) ... ≃ Ξ£ (Ο‰b : (Ξ£ (Ο‰ : Ξ©), orbit Ξ± Ο‰.out')), stabilizer Ξ± (Ο‰b.2 : Ξ²) : (self_equiv_sigma_orbits Ξ± Ξ²).sigma_congr_left' ... ≃ Ξ£ (Ο‰ : Ξ©), (Ξ£ (b : orbit Ξ± Ο‰.out'), stabilizer Ξ± (b : Ξ²)) : equiv.sigma_assoc (Ξ» (Ο‰ : Ξ©) (b : orbit Ξ± Ο‰.out'), stabilizer Ξ± (b : Ξ²)) ... ≃ Ξ£ (Ο‰ : Ξ©), (Ξ£ (b : orbit Ξ± Ο‰.out'), stabilizer Ξ± Ο‰.out') : equiv.sigma_congr_right (Ξ» Ο‰, equiv.sigma_congr_right $ Ξ» ⟨b, hb⟩, (stabilizer_equiv_stabilizer_of_orbit_rel hb).to_equiv) ... ≃ Ξ£ (Ο‰ : Ξ©), orbit Ξ± Ο‰.out' Γ— stabilizer Ξ± Ο‰.out' : equiv.sigma_congr_right (Ξ» Ο‰, equiv.sigma_equiv_prod _ _) ... ≃ Ξ£ (Ο‰ : Ξ©), Ξ± : equiv.sigma_congr_right (Ξ» Ο‰, orbit_prod_stabilizer_equiv_group Ξ± Ο‰.out') ... ≃ Ξ© Γ— Ξ± : equiv.sigma_equiv_prod Ξ© Ξ± /-- **Burnside's lemma** : given a finite group `G` acting on a set `X`, the average number of elements fixed by each `g ∈ G` is the number of orbits. -/ @[to_additive "**Burnside's lemma** : given a finite additive group `G` acting on a set `X`, the average number of elements fixed by each `g ∈ G` is the number of orbits. "] lemma sum_card_fixed_by_eq_card_orbits_mul_card_group [fintype Ξ±] [Ξ  a, fintype $ fixed_by Ξ± Ξ² a] [fintype Ξ©] : βˆ‘ (a : Ξ±), fintype.card (fixed_by Ξ± Ξ² a) = fintype.card Ξ© * fintype.card Ξ± := by rw [← fintype.card_prod, ← fintype.card_sigma, fintype.card_congr (sigma_fixed_by_equiv_orbits_prod_group Ξ± Ξ²)] @[to_additive] instance is_pretransitive_quotient (G) [group G] (H : subgroup G) : is_pretransitive G (G β§Έ H) := { exists_smul_eq := begin rintros ⟨x⟩ ⟨y⟩, refine ⟨y * x⁻¹, quotient_group.eq.mpr _⟩, simp only [smul_eq_mul, H.one_mem, mul_left_inv, inv_mul_cancel_right], end } end mul_action namespace subgroup variables {G : Type*} [group G] (H : subgroup G) lemma normal_core_eq_ker : H.normal_core = (mul_action.to_perm_hom G (G β§Έ H)).ker := begin refine le_antisymm (Ξ» g hg, equiv.perm.ext (Ξ» q, quotient_group.induction_on q (Ξ» g', (mul_action.quotient.smul_mk H g g').trans (quotient_group.eq.mpr _)))) (subgroup.normal_le_normal_core.mpr (Ξ» g hg, _)), { rw [smul_eq_mul, mul_inv_rev, ←inv_inv g', inv_inv], exact H.normal_core.inv_mem hg g'⁻¹ }, { rw [←H.inv_mem_iff, ←mul_one g⁻¹, ←quotient_group.eq, ←mul_one g], exact (mul_action.quotient.smul_mk H g 1).symm.trans (equiv.perm.ext_iff.mp hg (1 : G)) }, end open quotient_group /-- Cosets of the centralizer of an element embed into the set of commutators. -/ noncomputable def quotient_centralizer_embedding (g : G) : G β§Έ centralizer (zpowers (g : G)) β†ͺ commutator_set G := ((mul_action.orbit_equiv_quotient_stabilizer (conj_act G) g).trans (quotient_equiv_of_eq (conj_act.stabilizer_eq_centralizer g))).symm.to_embedding.trans ⟨λ x, ⟨x * g⁻¹, let ⟨_, x, rfl⟩ := x in ⟨x, g, rfl⟩⟩, Ξ» x y, subtype.ext ∘ mul_right_cancel ∘ subtype.ext_iff.mp⟩ lemma quotient_centralizer_embedding_apply (g : G) (x : G) : quotient_centralizer_embedding g x = βŸ¨β…x, g⁆, x, g, rfl⟩ := rfl /-- If `G` is generated by `S`, then the quotient by the center embeds into `S`-indexed sequences of commutators. -/ noncomputable def quotient_center_embedding {S : set G} (hS : closure S = ⊀) : G β§Έ center G β†ͺ S β†’ commutator_set G := (quotient_equiv_of_eq (center_eq_infi' S hS)).to_embedding.trans ((quotient_infi_embedding _).trans (function.embedding.Pi_congr_right (Ξ» g, quotient_centralizer_embedding g))) lemma quotient_center_embedding_apply {S : set G} (hS : closure S = ⊀) (g : G) (s : S) : quotient_center_embedding hS g s = βŸ¨β…g, s⁆, g, s, rfl⟩ := rfl end subgroup
cfc1df44ba2ce6246f6ace257b1cee098532b59a
9dc8cecdf3c4634764a18254e94d43da07142918
/src/data/holor.lean
db5bee610d7f07b0c5bd07b4d1f43425440547f0
[ "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
14,759
lean
/- Copyright (c) 2018 Alexander Bentkamp. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Alexander Bentkamp -/ import algebra.module.pi import algebra.big_operators.basic /-! # Basic properties of holors Holors are indexed collections of tensor coefficients. Confusingly, they are often called tensors in physics and in the neural network community. A holor is simply a multidimensional array of values. The size of a holor is specified by a `list β„•`, whose length is called the dimension of the holor. The tensor product of `x₁ : holor Ξ± ds₁` and `xβ‚‚ : holor Ξ± dsβ‚‚` is the holor given by `(x₁ βŠ— xβ‚‚) (i₁ ++ iβ‚‚) = x₁ i₁ * xβ‚‚ iβ‚‚`. A holor is "of rank at most 1" if it is a tensor product of one-dimensional holors. The CP rank of a holor `x` is the smallest N such that `x` is the sum of N holors of rank at most 1. Based on the tensor library found in <https://www.isa-afp.org/entries/Deep_Learning.html> ## References * <https://en.wikipedia.org/wiki/Tensor_rank_decomposition> -/ universes u open list open_locale big_operators /-- `holor_index ds` is the type of valid index tuples used to identify an entry of a holor of dimensions `ds`. -/ def holor_index (ds : list β„•) : Type := {is : list β„• // forallβ‚‚ (<) is ds} namespace holor_index variables {ds₁ dsβ‚‚ ds₃ : list β„•} def take : Ξ  {ds₁ : list β„•}, holor_index (ds₁ ++ dsβ‚‚) β†’ holor_index ds₁ | ds is := ⟨ list.take (length ds) is.1, forallβ‚‚_take_append is.1 ds dsβ‚‚ is.2 ⟩ def drop : Ξ  {ds₁ : list β„•}, holor_index (ds₁ ++ dsβ‚‚) β†’ holor_index dsβ‚‚ | ds is := ⟨ list.drop (length ds) is.1, forallβ‚‚_drop_append is.1 ds dsβ‚‚ is.2 ⟩ lemma cast_type (is : list β„•) (eq : ds₁ = dsβ‚‚) (h : forallβ‚‚ (<) is ds₁) : (cast (congr_arg holor_index eq) ⟨is, h⟩).val = is := by subst eq; refl def assoc_right : holor_index (ds₁ ++ dsβ‚‚ ++ ds₃) β†’ holor_index (ds₁ ++ (dsβ‚‚ ++ ds₃)) := cast (congr_arg holor_index (append_assoc ds₁ dsβ‚‚ ds₃)) def assoc_left : holor_index (ds₁ ++ (dsβ‚‚ ++ ds₃)) β†’ holor_index (ds₁ ++ dsβ‚‚ ++ ds₃) := cast (congr_arg holor_index (append_assoc ds₁ dsβ‚‚ ds₃).symm) lemma take_take : βˆ€ t : holor_index (ds₁ ++ dsβ‚‚ ++ ds₃), t.assoc_right.take = t.take.take | ⟨ is , h ⟩ := subtype.eq $ by simp [assoc_right,take, cast_type, list.take_take, nat.le_add_right, min_eq_left] lemma drop_take : βˆ€ t : holor_index (ds₁ ++ dsβ‚‚ ++ ds₃), t.assoc_right.drop.take = t.take.drop | ⟨ is , h ⟩ := subtype.eq (by simp [assoc_right, take, drop, cast_type, list.drop_take]) lemma drop_drop : βˆ€ t : holor_index (ds₁ ++ dsβ‚‚ ++ ds₃), t.assoc_right.drop.drop = t.drop | ⟨ is , h ⟩ := subtype.eq (by simp [add_comm, assoc_right, drop, cast_type, list.drop_drop]) end holor_index /-- Holor (indexed collections of tensor coefficients) -/ def holor (Ξ± : Type u) (ds : list β„•) := holor_index ds β†’ Ξ± namespace holor variables {Ξ± : Type} {d : β„•} {ds : list β„•} {ds₁ : list β„•} {dsβ‚‚ : list β„•} {ds₃ : list β„•} instance [inhabited Ξ±] : inhabited (holor Ξ± ds) := ⟨λ t, default⟩ instance [has_zero Ξ±] : has_zero (holor Ξ± ds) := ⟨λ t, 0⟩ instance [has_add Ξ±] : has_add (holor Ξ± ds) := ⟨λ x y t, x t + y t⟩ instance [has_neg Ξ±] : has_neg (holor Ξ± ds) := ⟨λ a t, - a t⟩ instance [add_semigroup Ξ±] : add_semigroup (holor Ξ± ds) := by refine_struct { add := (+), .. }; tactic.pi_instance_derive_field instance [add_comm_semigroup Ξ±] : add_comm_semigroup (holor Ξ± ds) := by refine_struct { add := (+), .. }; tactic.pi_instance_derive_field instance [add_monoid Ξ±] : add_monoid (holor Ξ± ds) := by refine_struct { zero := (0 : holor Ξ± ds), add := (+), nsmul := Ξ» n x i, n β€’ (x i) }; tactic.pi_instance_derive_field instance [add_comm_monoid Ξ±] : add_comm_monoid (holor Ξ± ds) := by refine_struct { zero := (0 : holor Ξ± ds), add := (+), nsmul := add_monoid.nsmul }; tactic.pi_instance_derive_field instance [add_group Ξ±] : add_group (holor Ξ± ds) := by refine_struct { zero := (0 : holor Ξ± ds), add := (+), nsmul := add_monoid.nsmul, zsmul := Ξ» n x i, n β€’ (x i) }; tactic.pi_instance_derive_field instance [add_comm_group Ξ±] : add_comm_group (holor Ξ± ds) := by refine_struct { zero := (0 : holor Ξ± ds), add := (+), nsmul := add_monoid.nsmul, zsmul := sub_neg_monoid.zsmul }; tactic.pi_instance_derive_field /- scalar product -/ instance [has_mul Ξ±] : has_smul Ξ± (holor Ξ± ds) := ⟨λ a x, Ξ» t, a * x t⟩ instance [semiring Ξ±] : module Ξ± (holor Ξ± ds) := pi.module _ _ _ /-- The tensor product of two holors. -/ def mul [s : has_mul Ξ±] (x : holor Ξ± ds₁) (y : holor Ξ± dsβ‚‚) : holor Ξ± (ds₁ ++ dsβ‚‚) := Ξ» t, x (t.take) * y (t.drop) local infix ` βŠ— ` : 70 := mul lemma cast_type (eq : ds₁ = dsβ‚‚) (a : holor Ξ± ds₁) : cast (congr_arg (holor Ξ±) eq) a = (Ξ» t, a (cast (congr_arg holor_index eq.symm) t)) := by subst eq; refl def assoc_right : holor Ξ± (ds₁ ++ dsβ‚‚ ++ ds₃) β†’ holor Ξ± (ds₁ ++ (dsβ‚‚ ++ ds₃)) := cast (congr_arg (holor Ξ±) (append_assoc ds₁ dsβ‚‚ ds₃)) def assoc_left : holor Ξ± (ds₁ ++ (dsβ‚‚ ++ ds₃)) β†’ holor Ξ± (ds₁ ++ dsβ‚‚ ++ ds₃) := cast (congr_arg (holor Ξ±) (append_assoc ds₁ dsβ‚‚ ds₃).symm) lemma mul_assoc0 [semigroup Ξ±] (x : holor Ξ± ds₁) (y : holor Ξ± dsβ‚‚) (z : holor Ξ± ds₃) : x βŠ— y βŠ— z = (x βŠ— (y βŠ— z)).assoc_left := funext (assume t : holor_index (ds₁ ++ dsβ‚‚ ++ ds₃), begin rw assoc_left, unfold mul, rw mul_assoc, rw [←holor_index.take_take, ←holor_index.drop_take, ←holor_index.drop_drop], rw cast_type, refl, rw append_assoc end) lemma mul_assoc [semigroup Ξ±] (x : holor Ξ± ds₁) (y : holor Ξ± dsβ‚‚) (z : holor Ξ± ds₃) : mul (mul x y) z == (mul x (mul y z)) := by simp [cast_heq, mul_assoc0, assoc_left]. lemma mul_left_distrib [distrib Ξ±] (x : holor Ξ± ds₁) (y : holor Ξ± dsβ‚‚) (z : holor Ξ± dsβ‚‚) : x βŠ— (y + z) = x βŠ— y + x βŠ— z := funext (Ξ»t, left_distrib (x (holor_index.take t)) (y (holor_index.drop t)) (z (holor_index.drop t))) lemma mul_right_distrib [distrib Ξ±] (x : holor Ξ± ds₁) (y : holor Ξ± ds₁) (z : holor Ξ± dsβ‚‚) : (x + y) βŠ— z = x βŠ— z + y βŠ— z := funext $ Ξ»t, add_mul (x (holor_index.take t)) (y (holor_index.take t)) (z (holor_index.drop t)) @[simp] lemma zero_mul {Ξ± : Type} [ring Ξ±] (x : holor Ξ± dsβ‚‚) : (0 : holor Ξ± ds₁) βŠ— x = 0 := funext (Ξ» t, zero_mul (x (holor_index.drop t))) @[simp] lemma mul_zero {Ξ± : Type} [ring Ξ±] (x : holor Ξ± ds₁) : x βŠ— (0 :holor Ξ± dsβ‚‚) = 0 := funext (Ξ» t, mul_zero (x (holor_index.take t))) lemma mul_scalar_mul [monoid Ξ±] (x : holor Ξ± []) (y : holor Ξ± ds) : x βŠ— y = x ⟨[], forallβ‚‚.nil⟩ β€’ y := by simp [mul, has_smul.smul, holor_index.take, holor_index.drop] /- holor slices -/ /-- A slice is a subholor consisting of all entries with initial index i. -/ def slice (x : holor Ξ± (d :: ds)) (i : β„•) (h : i < d) : holor Ξ± ds := (Ξ» is : holor_index ds, x ⟨ i :: is.1, forallβ‚‚.cons h is.2⟩) /-- The 1-dimensional "unit" holor with 1 in the `j`th position. -/ def unit_vec [monoid Ξ±] [add_monoid Ξ±] (d : β„•) (j : β„•) : holor Ξ± [d] := Ξ» ti, if ti.1 = [j] then 1 else 0 lemma holor_index_cons_decomp (p: holor_index (d :: ds) β†’ Prop) : Ξ  (t : holor_index (d :: ds)), (βˆ€ i is, Ξ  h : t.1 = i :: is, p ⟨ i :: is, begin rw [←h], exact t.2 end ⟩ ) β†’ p t | ⟨[], hforallβ‚‚βŸ© hp := absurd (forallβ‚‚_nil_left_iff.1 hforallβ‚‚) (cons_ne_nil d ds) | ⟨(i :: is), hforallβ‚‚βŸ© hp := hp i is rfl /-- Two holors are equal if all their slices are equal. -/ lemma slice_eq (x : holor Ξ± (d :: ds)) (y : holor Ξ± (d :: ds)) (h : slice x = slice y) : x = y := funext $ Ξ» t : holor_index (d :: ds), holor_index_cons_decomp (Ξ» t, x t = y t) t $ Ξ» i is hiis, have hiisdds: forallβ‚‚ (<) (i :: is) (d :: ds), begin rw [←hiis], exact t.2 end, have hid: i<d, from (forallβ‚‚_cons.1 hiisdds).1, have hisds: forallβ‚‚ (<) is ds, from (forallβ‚‚_cons.1 hiisdds).2, calc x ⟨i :: is, _⟩ = slice x i hid ⟨is, hisds⟩ : congr_arg (Ξ» t, x t) (subtype.eq rfl) ... = slice y i hid ⟨is, hisds⟩ : by rw h ... = y ⟨i :: is, _⟩ : congr_arg (Ξ» t, y t) (subtype.eq rfl) lemma slice_unit_vec_mul [ring Ξ±] {i : β„•} {j : β„•} (hid : i < d) (x : holor Ξ± ds) : slice (unit_vec d j βŠ— x) i hid = if i=j then x else 0 := funext $ Ξ» t : holor_index ds, if h : i = j then by simp [slice, mul, holor_index.take, unit_vec, holor_index.drop, h] else by simp [slice, mul, holor_index.take, unit_vec, holor_index.drop, h]; refl lemma slice_add [has_add Ξ±] (i : β„•) (hid : i < d) (x : holor Ξ± (d :: ds)) (y : holor Ξ± (d :: ds)) : slice x i hid + slice y i hid = slice (x + y) i hid := funext (Ξ» t, by simp [slice,(+)]) lemma slice_zero [has_zero Ξ±] (i : β„•) (hid : i < d) : slice (0 : holor Ξ± (d :: ds)) i hid = 0 := rfl lemma slice_sum [add_comm_monoid Ξ±] {Ξ² : Type} (i : β„•) (hid : i < d) (s : finset Ξ²) (f : Ξ² β†’ holor Ξ± (d :: ds)) : βˆ‘ x in s, slice (f x) i hid = slice (βˆ‘ x in s, f x) i hid := begin letI := classical.dec_eq Ξ², refine finset.induction_on s _ _, { simp [slice_zero] }, { intros _ _ h_not_in ih, rw [finset.sum_insert h_not_in, ih, slice_add, finset.sum_insert h_not_in] } end /-- The original holor can be recovered from its slices by multiplying with unit vectors and summing up. -/ @[simp] lemma sum_unit_vec_mul_slice [ring Ξ±] (x : holor Ξ± (d :: ds)) : βˆ‘ i in (finset.range d).attach, unit_vec d i βŠ— slice x i (nat.succ_le_of_lt (finset.mem_range.1 i.prop)) = x := begin apply slice_eq _ _ _, ext i hid, rw [←slice_sum], simp only [slice_unit_vec_mul hid], rw finset.sum_eq_single (subtype.mk i $ finset.mem_range.2 hid), { simp }, { assume (b : {x // x ∈ finset.range d}) (hb : b ∈ (finset.range d).attach) (hbi : b β‰  ⟨i, _⟩), have hbi' : i β‰  b, { simpa only [ne.def, subtype.ext_iff, subtype.coe_mk] using hbi.symm }, simp [hbi'] }, { assume hid' : subtype.mk i _ βˆ‰ finset.attach (finset.range d), exfalso, exact absurd (finset.mem_attach _ _) hid' } end /- CP rank -/ /-- `cprank_max1 x` means `x` has CP rank at most 1, that is, it is the tensor product of 1-dimensional holors. -/ inductive cprank_max1 [has_mul Ξ±]: Ξ  {ds}, holor Ξ± ds β†’ Prop | nil (x : holor Ξ± []) : cprank_max1 x | cons {d} {ds} (x : holor Ξ± [d]) (y : holor Ξ± ds) : cprank_max1 y β†’ cprank_max1 (x βŠ— y) /-- `cprank_max N x` means `x` has CP rank at most `N`, that is, it can be written as the sum of N holors of rank at most 1. -/ inductive cprank_max [has_mul Ξ±] [add_monoid Ξ±] : β„• β†’ Ξ  {ds}, holor Ξ± ds β†’ Prop | zero {ds} : cprank_max 0 (0 : holor Ξ± ds) | succ n {ds} (x : holor Ξ± ds) (y : holor Ξ± ds) : cprank_max1 x β†’ cprank_max n y β†’ cprank_max (n+1) (x + y) lemma cprank_max_nil [monoid Ξ±] [add_monoid Ξ±] (x : holor Ξ± nil) : cprank_max 1 x := have h : _, from cprank_max.succ 0 x 0 (cprank_max1.nil x) (cprank_max.zero), by rwa [add_zero x, zero_add] at h lemma cprank_max_1 [monoid Ξ±] [add_monoid Ξ±] {x : holor Ξ± ds} (h : cprank_max1 x) : cprank_max 1 x := have h' : _, from cprank_max.succ 0 x 0 h cprank_max.zero, by rwa [zero_add, add_zero] at h' lemma cprank_max_add [monoid Ξ±] [add_monoid Ξ±]: βˆ€ {m : β„•} {n : β„•} {x : holor Ξ± ds} {y : holor Ξ± ds}, cprank_max m x β†’ cprank_max n y β†’ cprank_max (m + n) (x + y) | 0 n x y (cprank_max.zero) hy := by simp [hy] | (m+1) n _ y (cprank_max.succ k x₁ xβ‚‚ hx₁ hxβ‚‚) hy := begin simp only [add_comm, add_assoc], apply cprank_max.succ, { assumption }, { exact cprank_max_add hxβ‚‚ hy } end lemma cprank_max_mul [ring Ξ±] : βˆ€ (n : β„•) (x : holor Ξ± [d]) (y : holor Ξ± ds), cprank_max n y β†’ cprank_max n (x βŠ— y) | 0 x _ (cprank_max.zero) := by simp [mul_zero x, cprank_max.zero] | (n+1) x _ (cprank_max.succ k y₁ yβ‚‚ hy₁ hyβ‚‚) := begin rw mul_left_distrib, rw nat.add_comm, apply cprank_max_add, { exact cprank_max_1 (cprank_max1.cons _ _ hy₁) }, { exact cprank_max_mul k x yβ‚‚ hyβ‚‚ } end lemma cprank_max_sum [ring Ξ±] {Ξ²} {n : β„•} (s : finset Ξ²) (f : Ξ² β†’ holor Ξ± ds) : (βˆ€ x ∈ s, cprank_max n (f x)) β†’ cprank_max (s.card * n) (βˆ‘ x in s, f x) := by letI := classical.dec_eq Ξ²; exact finset.induction_on s (by simp [cprank_max.zero]) (begin assume x s (h_x_notin_s : x βˆ‰ s) ih h_cprank, simp only [finset.sum_insert h_x_notin_s,finset.card_insert_of_not_mem h_x_notin_s], rw nat.right_distrib, simp only [nat.one_mul, nat.add_comm], have ih' : cprank_max (finset.card s * n) (βˆ‘ x in s, f x), { apply ih, assume (x : Ξ²) (h_x_in_s: x ∈ s), simp only [h_cprank, finset.mem_insert_of_mem, h_x_in_s] }, exact (cprank_max_add (h_cprank x (finset.mem_insert_self x s)) ih') end) lemma cprank_max_upper_bound [ring Ξ±] : Ξ  {ds}, βˆ€ x : holor Ξ± ds, cprank_max ds.prod x | [] x := cprank_max_nil x | (d :: ds) x := have h_summands : Ξ  (i : {x // x ∈ finset.range d}), cprank_max ds.prod (unit_vec d i.1 βŠ— slice x i.1 (mem_range.1 i.2)), from Ξ» i, cprank_max_mul _ _ _ (cprank_max_upper_bound (slice x i.1 (mem_range.1 i.2))), have h_dds_prod : (list.cons d ds).prod = finset.card (finset.range d) * prod ds, by simp [finset.card_range], have cprank_max (finset.card (finset.attach (finset.range d)) * prod ds) (βˆ‘ i in finset.attach (finset.range d), unit_vec d (i.val)βŠ—slice x (i.val) (mem_range.1 i.2)), from cprank_max_sum (finset.range d).attach _ (Ξ» i _, h_summands i), have h_cprank_max_sum : cprank_max (finset.card (finset.range d) * prod ds) (βˆ‘ i in finset.attach (finset.range d), unit_vec d (i.val)βŠ—slice x (i.val) (mem_range.1 i.2)), by rwa [finset.card_attach] at this, begin rw [←sum_unit_vec_mul_slice x], rw [h_dds_prod], exact h_cprank_max_sum, end /-- The CP rank of a holor `x`: the smallest N such that `x` can be written as the sum of N holors of rank at most 1. -/ noncomputable def cprank [ring Ξ±] (x : holor Ξ± ds) : nat := @nat.find (Ξ» n, cprank_max n x) (classical.dec_pred _) ⟨ds.prod, cprank_max_upper_bound x⟩ lemma cprank_upper_bound [ring Ξ±] : Ξ  {ds}, βˆ€ x : holor Ξ± ds, cprank x ≀ ds.prod := Ξ» ds (x : holor Ξ± ds), by letI := classical.dec_pred (Ξ» (n : β„•), cprank_max n x); exact nat.find_min' ⟨ds.prod, show (Ξ» n, cprank_max n x) ds.prod, from cprank_max_upper_bound x⟩ (cprank_max_upper_bound x) end holor
bc3ccabcbcd48b8a833af5c6315014f5bf8aa62a
63abd62053d479eae5abf4951554e1064a4c45b4
/src/linear_algebra/adic_completion.lean
0bf620df633aa5fbc6627e473a0a229a566d0d3b
[ "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
7,702
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 linear_algebra.smodeq import ring_theory.ideal.operations /-! # Completion of a module with respect to an ideal. In this file we define the notions of Hausdorff, precomplete, and complete for an `R`-module `M` with respect to an ideal `I`: ## Main definitions - `is_Hausdorff I M`: this says that the intersection of `I^n M` is `0`. - `is_precomplete I M`: this says that every Cauchy sequence converges. - `is_adic_complete I M`: this says that `M` is Hausdorff and precomplete. - `Hausdorffification I M`: this is the universal Hausdorff module with a map from `M`. - `completion I M`: if `I` is finitely generated, then this is the universal complete module (TODO) with a map from `M`. This map is injective iff `M` is Hausdorff and surjective iff `M` is precomplete. -/ open submodule variables {R : Type*} [comm_ring R] (I : ideal R) variables (M : Type*) [add_comm_group M] [module R M] variables {N : Type*} [add_comm_group N] [module R N] /-- A module `M` is Hausdorff with respect to an ideal `I` if `β‹‚ I^n M = 0`. -/ @[class] def is_Hausdorff : Prop := βˆ€ x : M, (βˆ€ n : β„•, x ≑ 0 [SMOD (I ^ n β€’ ⊀ : submodule R M)]) β†’ x = 0 /-- A module `M` is precomplete with respect to an ideal `I` if every Cauchy sequence converges. -/ @[class] def is_precomplete : Prop := βˆ€ f : β„• β†’ M, (βˆ€ {m n}, m ≀ n β†’ f m ≑ f n [SMOD (I ^ m β€’ ⊀ : submodule R M)]) β†’ βˆƒ L : M, βˆ€ n, f n ≑ L [SMOD (I ^ n β€’ ⊀ : submodule R M)] /-- A module `M` is `I`-adically complete if it is Hausdorff and precomplete. -/ @[class] def is_adic_complete : Prop := is_Hausdorff I M ∧ is_precomplete I M /-- The Hausdorffification of a module with respect to an ideal. -/ @[reducible] def Hausdorffification : Type* := (β¨… n : β„•, I ^ n β€’ ⊀ : submodule R M).quotient /-- The completion of a module with respect to an ideal. This is not necessarily Hausdorff. In fact, this is only complete if the ideal is finitely generated. -/ def adic_completion : submodule R (Ξ  n : β„•, (I ^ n β€’ ⊀ : submodule R M).quotient) := { carrier := { f | βˆ€ {m n} (h : m ≀ n), liftq _ (mkq _) (by { rw ker_mkq, exact smul_mono (ideal.pow_le_pow h) (le_refl _) }) (f n) = f m }, zero_mem' := Ξ» m n hmn, by rw [pi.zero_apply, pi.zero_apply, linear_map.map_zero], add_mem' := Ξ» f g hf hg m n hmn, by rw [pi.add_apply, pi.add_apply, linear_map.map_add, hf hmn, hg hmn], smul_mem' := Ξ» c f hf m n hmn, by rw [pi.smul_apply, pi.smul_apply, linear_map.map_smul, hf hmn] } namespace is_Hausdorff instance bot : is_Hausdorff (βŠ₯ : ideal R) M := Ξ» x hx, by simpa only [pow_one βŠ₯, bot_smul, smodeq.bot] using hx 1 variables {M} protected theorem subsingleton (h : is_Hausdorff (⊀ : ideal R) M) : subsingleton M := ⟨λ x y, eq_of_sub_eq_zero $ h (x - y) $ Ξ» n, by { rw [ideal.top_pow, top_smul], exact smodeq.top }⟩ variables (M) @[priority 100] instance of_subsingleton [subsingleton M] : is_Hausdorff I M := Ξ» x _, subsingleton.elim _ _ variables {I M} theorem infi_pow_smul (h : is_Hausdorff I M) : (β¨… n : β„•, I ^ n β€’ ⊀ : submodule R M) = βŠ₯ := eq_bot_iff.2 $ Ξ» x hx, (mem_bot _).2 $ h x $ Ξ» n, smodeq.zero.2 $ (mem_infi $ Ξ» n : β„•, I ^ n β€’ ⊀).1 hx n end is_Hausdorff namespace Hausdorffification /-- The canonical linear map to the Hausdorffification. -/ def of : M β†’β‚—[R] Hausdorffification I M := mkq _ variables {I M} @[elab_as_eliminator] lemma induction_on {C : Hausdorffification I M β†’ Prop} (x : Hausdorffification I M) (ih : βˆ€ x, C (of I M x)) : C x := quotient.induction_on' x ih variables (I M) instance : is_Hausdorff I (Hausdorffification I M) := Ξ» x, quotient.induction_on' x $ Ξ» x hx, (quotient.mk_eq_zero _).2 $ (mem_infi _).2 $ Ξ» n, begin have := comap_map_mkq (β¨… n : β„•, I ^ n β€’ ⊀ : submodule R M) (I ^ n β€’ ⊀), simp only [sup_of_le_right (infi_le (Ξ» n, (I ^ n β€’ ⊀ : submodule R M)) n)] at this, rw [← this, map_smul'', mem_comap, map_top, range_mkq, ← smodeq.zero], exact hx n end variables {M} [h : is_Hausdorff I N] include h /-- universal property of Hausdorffification: any linear map to a Hausdorff module extends to a unique map from the Hausdorffification. -/ def lift (f : M β†’β‚—[R] N) : Hausdorffification I M β†’β‚—[R] N := liftq _ f $ map_le_iff_le_comap.1 $ h.infi_pow_smul β–Έ le_infi (Ξ» n, le_trans (map_mono $ infi_le _ n) $ by { rw map_smul'', exact smul_mono (le_refl _) le_top }) theorem lift_of (f : M β†’β‚—[R] N) (x : M) : lift I f (of I M x) = f x := rfl theorem lift_comp_of (f : M β†’β‚—[R] N) : (lift I f).comp (of I M) = f := linear_map.ext $ Ξ» _, rfl /-- Uniqueness of lift. -/ theorem lift_eq (f : M β†’β‚—[R] N) (g : Hausdorffification I M β†’β‚—[R] N) (hg : g.comp (of I M) = f) : g = lift I f := linear_map.ext $ Ξ» x, induction_on x $ Ξ» x, by rw [lift_of, ← hg, linear_map.comp_apply] end Hausdorffification namespace is_precomplete instance bot : is_precomplete (βŠ₯ : ideal R) M := begin refine Ξ» f hf, ⟨f 1, Ξ» n, _⟩, cases n, { rw [pow_zero, ideal.one_eq_top, top_smul], exact smodeq.top }, specialize hf (nat.le_add_left 1 n), rw [pow_one, bot_smul, smodeq.bot] at hf, rw hf end instance top : is_precomplete (⊀ : ideal R) M := Ξ» f hf, ⟨0, Ξ» n, by { rw [ideal.top_pow, top_smul], exact smodeq.top }⟩ @[priority 100] instance of_subsingleton [subsingleton M] : is_precomplete I M := Ξ» f hf, ⟨0, Ξ» n, by rw subsingleton.elim (f n) 0⟩ end is_precomplete namespace adic_completion /-- The canonical linear map to the completion. -/ def of : M β†’β‚—[R] adic_completion I M := { to_fun := Ξ» x, ⟨λ n, mkq _ x, Ξ» m n hmn, rfl⟩, map_add' := Ξ» x y, rfl, map_smul' := Ξ» c x, rfl } @[simp] lemma of_apply (x : M) (n : β„•) : (of I M x).1 n = mkq _ x := rfl /-- Linearly evaluating a sequence in the completion at a given input. -/ def eval (n : β„•) : adic_completion I M β†’β‚—[R] (I ^ n β€’ ⊀ : submodule R M).quotient := { to_fun := Ξ» f, f.1 n, map_add' := Ξ» f g, rfl, map_smul' := Ξ» c f, rfl } @[simp] lemma coe_eval (n : β„•) : (eval I M n : adic_completion I M β†’ (I ^ n β€’ ⊀ : submodule R M).quotient) = Ξ» f, f.1 n := rfl lemma eval_apply (n : β„•) (f : adic_completion I M) : eval I M n f = f.1 n := rfl lemma eval_of (n : β„•) (x : M) : eval I M n (of I M x) = mkq _ x := rfl @[simp] lemma eval_comp_of (n : β„•) : (eval I M n).comp (of I M) = mkq _ := rfl @[simp] lemma range_eval (n : β„•) : (eval I M n).range = ⊀ := linear_map.range_eq_top.2 $ Ξ» x, quotient.induction_on' x $ Ξ» x, ⟨of I M x, rfl⟩ variables {I M} @[ext] lemma ext {x y : adic_completion I M} (h : βˆ€ n, eval I M n x = eval I M n y) : x = y := subtype.eq $ funext h variables (I M) instance : is_Hausdorff I (adic_completion I M) := Ξ» x hx, ext $ Ξ» n, smul_induction_on (smodeq.zero.1 $ hx n) (Ξ» r hr x _, ((eval I M n).map_smul r x).symm β–Έ quotient.induction_on' (eval I M n x) (Ξ» x, smodeq.zero.2 $ smul_mem_smul hr mem_top)) rfl (Ξ» _ _ ih1 ih2, by rw [linear_map.map_add, ih1, ih2, linear_map.map_zero, add_zero]) (Ξ» c _ ih, by rw [linear_map.map_smul, ih, linear_map.map_zero, smul_zero]) end adic_completion namespace is_adic_complete instance bot : is_adic_complete (βŠ₯ : ideal R) M := ⟨by apply_instance, by apply_instance⟩ protected theorem subsingleton (h : is_adic_complete (⊀ : ideal R) M) : subsingleton M := h.1.subsingleton @[priority 100] instance of_subsingleton [subsingleton M] : is_adic_complete I M := ⟨by apply_instance, by apply_instance⟩ end is_adic_complete
2dc3034e0fce3d806a1fe26571e328ea5bb25832
95dcf8dea2baf2b4b0a60d438f27c35ae3dd3990
/src/category_theory/instances/topological_spaces.lean
77ff2856e1f1278646f93121fa2b3e6a5764cbff
[ "Apache-2.0" ]
permissive
uniformity1/mathlib
829341bad9dfa6d6be9adaacb8086a8a492e85a4
dd0e9bd8f2e5ec267f68e72336f6973311909105
refs/heads/master
1,588,592,015,670
1,554,219,842,000
1,554,219,842,000
179,110,702
0
0
Apache-2.0
1,554,220,076,000
1,554,220,076,000
null
UTF-8
Lean
false
false
5,122
lean
-- Copyright (c) 2017 Scott Morrison. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Patrick Massot, Scott Morrison, Mario Carneiro import category_theory.concrete_category import category_theory.full_subcategory import category_theory.functor_category import category_theory.adjunction import category_theory.limits.types import category_theory.natural_isomorphism import category_theory.eq_to_hom import topology.basic import topology.opens import order.galois_connection open category_theory open category_theory.nat_iso open topological_space universe u namespace category_theory.instances /-- The category of topological spaces and continuous maps. -/ @[reducible] def Top : Type (u+1) := bundled topological_space instance (x : Top) : topological_space x := x.str namespace Top instance : concrete_category @continuous := ⟨@continuous_id, @continuous.comp⟩ -- local attribute [class] continuous -- instance {R S : Top} (f : R ⟢ S) : continuous (f : R β†’ S) := f.2 section open category_theory.limits variables {J : Type u} [small_category J] def limit (F : J β₯€ Top.{u}) : cone F := { X := ⟨limit (F β‹™ forget), ⨆ j, (F.obj j).str.induced (limit.Ο€ (F β‹™ forget) j)⟩, Ο€ := { app := Ξ» j, ⟨limit.Ο€ (F β‹™ forget) j, continuous_iff_induced_le.mpr (lattice.le_supr _ j)⟩, naturality' := Ξ» j j' f, subtype.eq ((limit.cone (F β‹™ forget)).Ο€.naturality f) } } def limit_is_limit (F : J β₯€ Top.{u}) : is_limit (limit F) := by refine is_limit.of_faithful forget (limit.is_limit _) (Ξ» s, ⟨_, _⟩) (Ξ» s, rfl); exact continuous_iff_le_coinduced.mpr (lattice.supr_le $ Ξ» j, induced_le_iff_le_coinduced.mpr $ continuous_iff_le_coinduced.mp (s.Ο€.app j).property) instance : has_limits.{u} Top.{u} := Ξ» J π’₯ F, by exactI { cone := limit F, is_limit := limit_is_limit F } instance : preserves_limits (forget : Top.{u} β₯€ Type u) := Ξ» J π’₯ F, by exactI preserves_limit_of_preserves_limit_cone (limit.is_limit F) (limit.is_limit (F β‹™ forget)) def colimit (F : J β₯€ Top.{u}) : cocone F := { X := ⟨colimit (F β‹™ forget), β¨… j, (F.obj j).str.coinduced (colimit.ΞΉ (F β‹™ forget) j)⟩, ΞΉ := { app := Ξ» j, ⟨colimit.ΞΉ (F β‹™ forget) j, continuous_iff_le_coinduced.mpr (lattice.infi_le _ j)⟩, naturality' := Ξ» j j' f, subtype.eq ((colimit.cocone (F β‹™ forget)).ΞΉ.naturality f) } } def colimit_is_colimit (F : J β₯€ Top.{u}) : is_colimit (colimit F) := by refine is_colimit.of_faithful forget (colimit.is_colimit _) (Ξ» s, ⟨_, _⟩) (Ξ» s, rfl); exact continuous_iff_induced_le.mpr (lattice.le_infi $ Ξ» j, induced_le_iff_le_coinduced.mpr $ continuous_iff_le_coinduced.mp (s.ΞΉ.app j).property) instance : has_colimits.{u} Top.{u} := Ξ» J π’₯ F, by exactI { cocone := colimit F, is_colimit := colimit_is_colimit F } instance : preserves_colimits (forget : Top.{u} β₯€ Type u) := Ξ» J π’₯ F, by exactI preserves_colimit_of_preserves_colimit_cocone (colimit.is_colimit F) (colimit.is_colimit (F β‹™ forget)) end def discrete : Type u β₯€ Top.{u} := { obj := Ξ» X, ⟨X, ⊀⟩, map := Ξ» X Y f, ⟨f, continuous_top⟩ } def trivial : Type u β₯€ Top.{u} := { obj := Ξ» X, ⟨X, βŠ₯⟩, map := Ξ» X Y f, ⟨f, continuous_bot⟩ } def adj₁ : adjunction discrete forget := { hom_equiv := Ξ» X Y, { to_fun := Ξ» f, f, inv_fun := Ξ» f, ⟨f, continuous_top⟩, left_inv := by tidy, right_inv := by tidy }, unit := { app := Ξ» X, id }, counit := { app := Ξ» X, ⟨id, continuous_top⟩ } } def adjβ‚‚ : adjunction forget trivial := { hom_equiv := Ξ» X Y, { to_fun := Ξ» f, ⟨f, continuous_bot⟩, inv_fun := Ξ» f, f, left_inv := by tidy, right_inv := by tidy }, unit := { app := Ξ» X, ⟨id, continuous_bot⟩ }, counit := { app := Ξ» X, id } } end Top variables {X : Top.{u}} instance : small_category (opens X) := by apply_instance def nbhd (x : X.Ξ±) := { U : opens X // x ∈ U } def nbhds (x : X.Ξ±) : small_category (nbhd x) := begin unfold nbhd, apply_instance end end category_theory.instances open category_theory.instances namespace topological_space.opens /-- `opens.map f` gives the functor from open sets in Y to open set in X, given by taking preimages under f. -/ def map {X Y : Top.{u}} (f : X ⟢ Y) : opens Y β₯€ opens X := { obj := Ξ» U, ⟨ f.val ⁻¹' U, f.property _ U.property ⟩, map := Ξ» U V i, ⟨ ⟨ Ξ» a b, i.down.down b ⟩ ⟩ }. @[simp] lemma map_id_obj (X : Top.{u}) (U : opens X) : (map (πŸ™ X)).obj U = U := by tidy @[simp] def map_id (X : Top.{u}) : map (πŸ™ X) β‰… functor.id (opens X) := { hom := { app := Ξ» U, πŸ™ U }, inv := { app := Ξ» U, πŸ™ U } } -- We could make f g implicit here, but it's nice to be able to see when -- they are the identity (often!) def map_iso {X Y : Top.{u}} (f g : X ⟢ Y) (h : f = g) : map f β‰… map g := nat_iso.of_components (Ξ» U, eq_to_iso (congr_fun (congr_arg _ (congr_arg _ h)) _) ) (by obviously) @[simp] def map_iso_id {X : Top.{u}} (h) : map_iso (πŸ™ X) (πŸ™ X) h = iso.refl (map _) := rfl end topological_space.opens
6e32e5c2964bec3bc2af1e6c722e84814244d9b3
6df8d5ae3acf20ad0d7f0247d2cee1957ef96df1
/Exams/exam_1.lean
8011a9c6a14579ef1f6e7a460918aa3bd8ec47e2
[]
no_license
derekjohnsonva/CS2102
8ed45daa6658e6121bac0f6691eac6147d08246d
b3f507d4be824a2511838a1054d04fc9aef3304c
refs/heads/master
1,648,529,162,527
1,578,851,859,000
1,578,851,859,000
233,433,207
0
0
null
null
null
null
UTF-8
Lean
false
false
7,628
lean
-- CS2102 F19 Exam #1 --Derek Johnson dej3tc --10/10/19 /- #1 [10 points] Complete the following definitions to give examples of literal values of specified types. Use lambda expressions to complete the questions involving function types. Make functions (lambda expressions) simple: we don't care what the functions do, just that they are of the right types. -/ open nat def x := Ξ» (n:β„•), n def n : β„• := 1 def s : string := "CS" def b : bool := tt def f1 : β„• β†’ bool := Ξ» (n: β„•), tt def f2 : (β„• β†’ β„•) β†’ bool := Ξ» x,tt def f3 : (β„• β†’ β„•) β†’ (β„• β†’ β„•) := Ξ» x,x def t1 : Type := nat def t2 : Type β†’ Type := Ξ» (Ξ± : Type), Ξ± /- #2 [10 points] Complete the following recursive function definition so that the function computes the sum of the natural numbers from 0 to (and including) a given value, n. -/ def sumto : β„• β†’ β„• | 0 := 0 | (nat.succ n') := nat.succ n' + sumto(n') /- #3. [5 points] We have seen that we can write function *specifications* in the language of predicate logic, and specifically in the language of pure functional programming. We also know we can, and generally do, write *implementations* in the language of imperative programming (e.g., in Python or in Java). Complete the following sentence by filling in the blanks to explain the esssential tradeoff between function specifications, in the langugae of predicate logic, and function implementations written in imperative programming languages, respectively. Specifications are generally understandable but less efficient while implementations are generally the opposite: less understandable but more efficient. -/ /- # 4. [10 points] Natural languages, such as English and Mandarin, are very powerful, but they have some fundamental weaknesses when it comes to writing and verifying precise specifications and claims about properties of algorithms and programs. It is for this reason that computer scientists often prefer to write express such things using mathematical logic instead of natural language. Name three fundamental weaknesses of natural language when it comes to carrying out such tasks. You may given one-word answers if you wish. A. Ambiguous B. Not Machine Checkable C. Too Verbose (in some situations) -/ /- #5. [10 points] What logical proposition expresses the claim that a given implementation, I, of a function of type β„• β†’ β„•, is correct with respect to a specification, S, of the same function? Answer: For all natural numbers, implementation I will hold to / be true for specification S. -/ /- #6. [10 points] What Boolean functions do the following definitions define? -/ def mystery1 : bool β†’ bool β†’ bool | tt tt := ff | ff ff := ff | _ _ := tt /- Answer: Not Equal To -/ def mystery2 : bool β†’ bool β†’ bool | tt ff := ff | _ _ := tt /- Answer: Not (a, not b) -/ /- #7. [10 points] Define a function that takes a string, s, and a natural number, n, and that returns value of type (list string) in which s is repeated n times. Give you answer by completing the following definition: fill in underscores with the answers that are needed. Note that the list namespace is not open by default, so prefix constructor names with "list." as we do for the first (base) case. -/ def repeat : string β†’ β„• β†’ list string | s nat.zero := list.nil | s (nat.succ n') := list.cons s (repeat s n') #eval repeat "hello" 3 /- #8. [10 points] Definea a polymorphic function that takes (1) a type, Ξ±, (2) a value, s : Ξ±, and (3) a natural number, n, and that returns a list in which the value, a, is repeated n times. Make the type argument to this function implicit. Replace underscores as necessary to give a complete answer. Note again that the list namespace is not open, so use "fully qualified" constructor names. -/ def poly_repeat {Ξ± : Type} : Ξ± β†’ β„• β†’ list Ξ± | s nat.zero := list.nil | s (nat.succ n') := list.cons s (poly_repeat s n') #eval poly_repeat "hello" 3 /- #9. [10 points] Define a data type, an enumerated type, friend_or_foe, with just two terms, one called friend, one called foe. Then define a function called eval that takes two terms, F1 and F2, of this type and returns a term of this type, where the function implements the following table: F1 F2 result friend friend friend friend foe foe foe friend foe foe foe friend -/ -- Answer here inductive friend_or_foe : Type | friend : friend_or_foe | foe : friend_or_foe open friend_or_foe def eval : friend_or_foe β†’ friend_or_foe β†’ friend_or_foe | friend friend := friend | friend foe := foe | foe friend := foe | foe foe := friend /- #10. [10 points] We studied the higher-order function, map. In particular, we implemented a version of it, which we called mmap, for functions of type β„• β†’ β„•, and for lists of type (list β„•). The function is reproduced next for your reference. Read and recall how the function works, then continue on to the questions that follow. -/ def mmap : (β„• β†’ β„•) β†’ list β„• β†’ list β„• | f [] := [] | f (list.cons h t) := list.cons (f h) (mmap f t) -- An example application of this function #eval mmap (Ξ» n, n + 1) [1, 2, 3, 4, 5] /- A. Write a polymorphic version of this function, called pmap, that takes (1) two type arguments, Ξ± and Ξ², (2) a function of type Ξ± β†’ Ξ², and (3) a list of values of type Ξ±, and that returns the list of values obtained by applying the given function to each value in the given list. Make Ξ± and Ξ² implicit arguments. -/ -- Answer here def pmap {Ξ± Ξ² : Type} : (Ξ± β†’ Ξ²) β†’ list Ξ± β†’ list Ξ² | f [] := [] | f (list.cons h t) := list.cons (f h) (pmap f t) /- B. Use #eval to evaluate an application of pmap to a function of type β„• to bool and a non-empty list of natural numbers. Use a lambda abstraction to give the function argument. It does not matter to us what value the function returns. -/ -- Answer here def tttt:= Ξ» (n:β„•), tt def list1 := [1,2,3,4,5] #eval pmap (Ξ» (n:β„•), tt) [1,2,3,4,5] /- #11. [10 points] Define a data type, prod3_nat, with one constructor, triple, that takes three natural numbers as arguments, yielding a term of type prod3_nat. Then write three "projection functions", prod3_nat_fst, prod3_nat_snd, and prod3_nat_thd, each of which takes a prod3_nat value and returns its corresponding component element. Hint: look to see how we defined the prod type, its pair constructor, and its two projection functions. -/ inductive prod3_nat : Type | triple (a :β„• ) (b:β„• ) (c : β„• ) : prod3_nat def prod3_nat_fst : prod3_nat β†’ β„• | (prod3_nat.triple a b c) := a def prod3_nat_snd : prod3_nat β†’ β„• | (prod3_nat.triple a b c) := b def prod3_nat_thd : prod3_nat β†’ β„• | (prod3_nat.triple a b c) := c def ss := prod3_nat.triple 2 3 4 #eval prod3_nat_fst ss /- Extra credit. Define prod3 as a version of prod3_nat that is polymorphic in each of its three components; define polymorphic projection functions; and then use them to define a function, rotate_right, that takes a triple, (a, b, c), and returns the triple (c, a, b). (Call your type arguments Ξ±, Ξ², and Ξ³ - alpha, beta, and gamma). -/ inductive prod3 (Ξ± Ξ² Ξ³ : Type) : Type | triple (a : Ξ±) (b : Ξ²) (y : Ξ³) : prod3 def prod3_fst {Ξ± Ξ² Ξ³ : Type}: prod3 Ξ± Ξ² Ξ³ β†’ Ξ± | (prod3.triple a b c) := a def prod3_snd {Ξ± Ξ² Ξ³ : Type} : prod3 Ξ± Ξ² Ξ³ β†’ Ξ² | (prod3.triple a b c) := b def prod3_thd {Ξ± Ξ² Ξ³ : Type} : prod3 Ξ± Ξ² Ξ³ β†’ Ξ³ | (prod3.triple a b c) := c def rotate_right {Ξ± Ξ² Ξ³ : Type} : prod3 Ξ± Ξ² Ξ³ β†’ prod3 Ξ³ Ξ± Ξ² | (prod3.triple a b c) := prod3.triple c a b
185847859f5bbaa3fbd272d3ad6c0cd8c2c04680
63abd62053d479eae5abf4951554e1064a4c45b4
/src/algebra/lie/classical.lean
0427cc686d174634e62a8bec1d613e93ad691fd0
[ "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
13,507
lean
/- Copyright (c) 2020 Oliver Nash. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Oliver Nash -/ import algebra.invertible import algebra.lie.basic import linear_algebra.matrix /-! # Classical Lie algebras This file is the place to find definitions and basic properties of the classical Lie algebras: * Aβ‚— = sl(l+1) * Bβ‚— ≃ so(l+1, l) ≃ so(2l+1) * Cβ‚— = sp(l) * Dβ‚— ≃ so(l, l) ≃ so(2l) ## Main definitions * `lie_algebra.special_linear.sl` * `lie_algebra.symplectic.sp` * `lie_algebra.orthogonal.so` * `lie_algebra.orthogonal.so'` * `lie_algebra.orthogonal.so_indefinite_equiv` * `lie_algebra.orthogonal.type_D` * `lie_algebra.orthogonal.type_B` * `lie_algebra.orthogonal.type_D_equiv_so'` * `lie_algebra.orthogonal.type_B_equiv_so'` ## Implementation notes ### Matrices or endomorphisms Given a finite type and a commutative ring, the corresponding square matrices are equivalent to the endomorphisms of the corresponding finite-rank free module as Lie algebras, see `lie_equiv_matrix'`. We can thus define the classical Lie algebras as Lie subalgebras either of matrices or of endomorphisms. We have opted for the former. At the time of writing (August 2020) it is unclear which approach should be preferred so the choice should be assumed to be somewhat arbitrary. ### Diagonal quadratic form or diagonal Cartan subalgebra For the algebras of type `B` and `D`, there are two natural definitions. For example since the the `2l Γ— 2l` matrix: $$ J = \left[\begin{array}{cc} 0_l & 1_l\\\\ 1_l & 0_l \end{array}\right] $$ defines a symmetric bilinear form equivalent to that defined by the identity matrix `I`, we can define the algebras of type `D` to be the Lie subalgebra of skew-adjoint matrices either for `J` or for `I`. Both definitions have their advantages (in particular the `J`-skew-adjoint matrices define a Lie algebra for which the diagonal matrices form a Cartan subalgebra) and so we provide both. We thus also provide equivalences `type_D_equiv_so'`, `so_indefinite_equiv` which show the two definitions are equivalent. Similarly for the algebras of type `B`. ## Tags classical lie algebra, special linear, symplectic, orthogonal -/ universes u₁ uβ‚‚ namespace lie_algebra open_locale matrix variables (n p q l : Type*) (R : Type uβ‚‚) variables [fintype n] [fintype l] [fintype p] [fintype q] variables [decidable_eq n] [decidable_eq p] [decidable_eq q] [decidable_eq l] variables [comm_ring R] @[simp] lemma matrix_trace_commutator_zero (X Y : matrix n n R) : matrix.trace n R R ⁅X, Y⁆ = 0 := begin -- TODO: if we use matrix.mul here, we get a timeout change matrix.trace n R R (X * Y - Y * X) = 0, erw [linear_map.map_sub, matrix.trace_mul_comm, sub_self] end namespace special_linear /-- The special linear Lie algebra: square matrices of trace zero. -/ def sl : lie_subalgebra R (matrix n n R) := { lie_mem := Ξ» X Y _ _, linear_map.mem_ker.2 $ matrix_trace_commutator_zero _ _ _ _, ..linear_map.ker (matrix.trace n R R) } lemma sl_bracket (A B : sl n R) : ⁅A, B⁆.val = A.val ⬝ B.val - B.val ⬝ A.val := rfl section elementary_basis variables {n} (i j : n) /-- It is useful to define these matrices for explicit calculations in sl n R. -/ abbreviation E : matrix n n R := Ξ» i' j', if i = i' ∧ j = j' then 1 else 0 @[simp] lemma E_apply_one : E R i j i j = 1 := if_pos (and.intro rfl rfl) @[simp] lemma E_apply_zero (i' j' : n) (h : Β¬(i = i' ∧ j = j')) : E R i j i' j' = 0 := if_neg h @[simp] lemma E_diag_zero (h : j β‰  i) : matrix.diag n R R (E R i j) = 0 := begin ext k, rw matrix.diag_apply, suffices : Β¬(i = k ∧ j = k), by exact if_neg this, rintros ⟨e₁, eβ‚‚βŸ©, apply h, subst e₁, exact eβ‚‚, end lemma E_trace_zero (h : j β‰  i) : matrix.trace n R R (E R i j) = 0 := by simp [h] /-- When j β‰  i, the elementary matrices are elements of sl n R, in fact they are part of a natural basis of sl n R. -/ def Eb (h : j β‰  i) : sl n R := ⟨E R i j, by { change E R i j ∈ linear_map.ker (matrix.trace n R R), simp [E_trace_zero R i j h], }⟩ @[simp] lemma Eb_val (h : j β‰  i) : (Eb R i j h).val = E R i j := rfl end elementary_basis lemma sl_non_abelian [nontrivial R] (h : 1 < fintype.card n) : Β¬lie_algebra.is_abelian β†₯(sl n R) := begin rcases fintype.exists_pair_of_one_lt_card h with ⟨j, i, hij⟩, let A := Eb R i j hij, let B := Eb R j i hij.symm, intros c, have c' : A.val ⬝ B.val = B.val ⬝ A.val := by { rw [←sub_eq_zero, ←sl_bracket, c.abelian], refl, }, have : (1 : R) = 0 := by simpa [matrix.mul_apply, hij] using (congr_fun (congr_fun c' i) i), exact one_ne_zero this, end end special_linear namespace symplectic /-- The matrix defining the canonical skew-symmetric bilinear form. -/ def J : matrix (l βŠ• l) (l βŠ• l) R := matrix.from_blocks 0 (-1) 1 0 /-- The symplectic Lie algebra: skew-adjoint matrices with respect to the canonical skew-symmetric bilinear form. -/ def sp : lie_subalgebra R (matrix (l βŠ• l) (l βŠ• l) R) := skew_adjoint_matrices_lie_subalgebra (J l R) end symplectic namespace orthogonal /-- The definite orthogonal Lie subalgebra: skew-adjoint matrices with respect to the symmetric bilinear form defined by the identity matrix. -/ def so : lie_subalgebra R (matrix n n R) := skew_adjoint_matrices_lie_subalgebra (1 : matrix n n R) @[simp] lemma mem_so (A : matrix n n R) : A ∈ so n R ↔ Aα΅€ = -A := begin erw mem_skew_adjoint_matrices_submodule, simp only [matrix.is_skew_adjoint, matrix.is_adjoint_pair, matrix.mul_one, matrix.one_mul], end /-- The indefinite diagonal matrix with `p` 1s and `q` -1s. -/ def indefinite_diagonal : matrix (p βŠ• q) (p βŠ• q) R := matrix.diagonal $ sum.elim (Ξ» _, 1) (Ξ» _, -1) /-- The indefinite orthogonal Lie subalgebra: skew-adjoint matrices with respect to the symmetric bilinear form defined by the indefinite diagonal matrix. -/ def so' : lie_subalgebra R (matrix (p βŠ• q) (p βŠ• q) R) := skew_adjoint_matrices_lie_subalgebra $ indefinite_diagonal p q R /-- A matrix for transforming the indefinite diagonal bilinear form into the definite one, provided the parameter `i` is a square root of -1. -/ def Pso (i : R) : matrix (p βŠ• q) (p βŠ• q) R := matrix.diagonal $ sum.elim (Ξ» _, 1) (Ξ» _, i) lemma Pso_inv {i : R} (hi : i*i = -1) : (Pso p q R i) * (Pso p q R (-i)) = 1 := begin ext x y, rcases x; rcases y, { -- x y : p by_cases h : x = y; simp [Pso, indefinite_diagonal, h], }, { -- x : p, y : q simp [Pso, indefinite_diagonal], }, { -- x : q, y : p simp [Pso, indefinite_diagonal], }, { -- x y : q by_cases h : x = y; simp [Pso, indefinite_diagonal, h, hi], }, end lemma is_unit_Pso {i : R} (hi : i*i = -1) : is_unit (Pso p q R i) := ⟨{ val := Pso p q R i, inv := Pso p q R (-i), val_inv := Pso_inv p q R hi, inv_val := by { apply matrix.nonsing_inv_left_right, exact Pso_inv p q R hi, }, }, rfl⟩ lemma indefinite_diagonal_transform {i : R} (hi : i*i = -1) : (Pso p q R i)α΅€ ⬝ (indefinite_diagonal p q R) ⬝ (Pso p q R i) = 1 := begin ext x y, rcases x; rcases y, { -- x y : p by_cases h : x = y; simp [Pso, indefinite_diagonal, h], }, { -- x : p, y : q simp [Pso, indefinite_diagonal], }, { -- x : q, y : p simp [Pso, indefinite_diagonal], }, { -- x y : q by_cases h : x = y; simp [Pso, indefinite_diagonal, h, hi], }, end /-- An equivalence between the indefinite and definite orthogonal Lie algebras, over a ring containing a square root of -1. -/ noncomputable def so_indefinite_equiv {i : R} (hi : i*i = -1) : so' p q R ≃ₗ⁅R⁆ so (p βŠ• q) R := begin apply (skew_adjoint_matrices_lie_subalgebra_equiv (indefinite_diagonal p q R) (Pso p q R i) (is_unit_Pso p q R hi)).trans, apply lie_algebra.equiv.of_eq, ext A, rw indefinite_diagonal_transform p q R hi, refl, end lemma so_indefinite_equiv_apply {i : R} (hi : i*i = -1) (A : so' p q R) : (so_indefinite_equiv p q R hi A : matrix (p βŠ• q) (p βŠ• q) R) = (Pso p q R i)⁻¹ ⬝ (A : matrix (p βŠ• q) (p βŠ• q) R) ⬝ (Pso p q R i) := by erw [lie_algebra.equiv.trans_apply, lie_algebra.equiv.of_eq_apply, skew_adjoint_matrices_lie_subalgebra_equiv_apply] /-- A matrix defining a canonical even-rank symmetric bilinear form. It looks like this as a `2l x 2l` matrix of `l x l` blocks: [ 0 1 ] [ 1 0 ] -/ def JD : matrix (l βŠ• l) (l βŠ• l) R := matrix.from_blocks 0 1 1 0 /-- The classical Lie algebra of type D as a Lie subalgebra of matrices associated to the matrix `JD`. -/ def type_D := skew_adjoint_matrices_lie_subalgebra (JD l R) /-- A matrix transforming the bilinear form defined by the matrix `JD` into a split-signature diagonal matrix. It looks like this as a `2l x 2l` matrix of `l x l` blocks: [ 1 -1 ] [ 1 1 ] -/ def PD : matrix (l βŠ• l) (l βŠ• l) R := matrix.from_blocks 1 (-1) 1 1 /-- The split-signature diagonal matrix. -/ def S := indefinite_diagonal l l R lemma S_as_blocks : S l R = matrix.from_blocks 1 0 0 (-1) := begin rw [← matrix.diagonal_one, matrix.diagonal_neg, matrix.from_blocks_diagonal], refl, end lemma JD_transform : (PD l R)α΅€ ⬝ (JD l R) ⬝ (PD l R) = (2 : R) β€’ (S l R) := begin have h : (PD l R)α΅€ ⬝ (JD l R) = matrix.from_blocks 1 1 1 (-1) := by { simp [PD, JD, matrix.from_blocks_transpose, matrix.from_blocks_multiply], }, erw [h, S_as_blocks, matrix.from_blocks_multiply, matrix.from_blocks_smul], congr; simp [two_smul], end lemma PD_inv [invertible (2 : R)] : (PD l R) * (β…Ÿ(2 : R) β€’ (PD l R)α΅€) = 1 := begin have h : β…Ÿ(2 : R) β€’ (1 : matrix l l R) + β…Ÿ(2 : R) β€’ 1 = 1 := by rw [← smul_add, ← (two_smul R _), smul_smul, inv_of_mul_self, one_smul], erw [matrix.from_blocks_transpose, matrix.from_blocks_smul, matrix.mul_eq_mul, matrix.from_blocks_multiply], simp [h], end lemma is_unit_PD [invertible (2 : R)] : is_unit (PD l R) := ⟨{ val := PD l R, inv := β…Ÿ(2 : R) β€’ (PD l R)α΅€, val_inv := PD_inv l R, inv_val := by { apply matrix.nonsing_inv_left_right, exact PD_inv l R, }, }, rfl⟩ /-- An equivalence between two possible definitions of the classical Lie algebra of type D. -/ noncomputable def type_D_equiv_so' [invertible (2 : R)] : type_D l R ≃ₗ⁅R⁆ so' l l R := begin apply (skew_adjoint_matrices_lie_subalgebra_equiv (JD l R) (PD l R) (is_unit_PD l R)).trans, apply lie_algebra.equiv.of_eq, ext A, rw [JD_transform, ← unit_of_invertible_val (2 : R), lie_subalgebra.mem_coe, mem_skew_adjoint_matrices_lie_subalgebra_unit_smul], refl, end /-- A matrix defining a canonical odd-rank symmetric bilinear form. It looks like this as a `(2l+1) x (2l+1)` matrix of blocks: [ 2 0 0 ] [ 0 0 1 ] [ 0 1 0 ] where sizes of the blocks are: [`1 x 1` `1 x l` `1 x l`] [`l x 1` `l x l` `l x l`] [`l x 1` `l x l` `l x l`] -/ def JB := matrix.from_blocks ((2 : R) β€’ 1 : matrix unit unit R) 0 0 (JD l R) /-- The classical Lie algebra of type B as a Lie subalgebra of matrices associated to the matrix `JB`. -/ def type_B := skew_adjoint_matrices_lie_subalgebra (JB l R) /-- A matrix transforming the bilinear form defined by the matrix `JB` into an almost-split-signature diagonal matrix. It looks like this as a `(2l+1) x (2l+1)` matrix of blocks: [ 1 0 0 ] [ 0 1 -1 ] [ 0 1 1 ] where sizes of the blocks are: [`1 x 1` `1 x l` `1 x l`] [`l x 1` `l x l` `l x l`] [`l x 1` `l x l` `l x l`] -/ def PB := matrix.from_blocks (1 : matrix unit unit R) 0 0 (PD l R) lemma PB_inv [invertible (2 : R)] : (PB l R) * (matrix.from_blocks 1 0 0 (PD l R)⁻¹) = 1 := begin simp [PB, matrix.from_blocks_multiply, (PD l R).mul_nonsing_inv, is_unit_PD, ← (PD l R).is_unit_iff_is_unit_det] end lemma is_unit_PB [invertible (2 : R)] : is_unit (PB l R) := ⟨{ val := PB l R, inv := matrix.from_blocks 1 0 0 (PD l R)⁻¹, val_inv := PB_inv l R, inv_val := by { apply matrix.nonsing_inv_left_right, exact PB_inv l R, }, }, rfl⟩ lemma JB_transform : (PB l R)α΅€ ⬝ (JB l R) ⬝ (PB l R) = (2 : R) β€’ matrix.from_blocks 1 0 0 (S l R) := by simp [PB, JB, JD_transform, matrix.from_blocks_transpose, matrix.from_blocks_multiply, matrix.from_blocks_smul] lemma indefinite_diagonal_assoc : indefinite_diagonal (unit βŠ• l) l R = matrix.reindex_lie_equiv (equiv.sum_assoc unit l l).symm (matrix.from_blocks 1 0 0 (indefinite_diagonal l l R)) := begin ext i j, rcases i with ⟨⟨i₁ | iβ‚‚βŸ© | iβ‚ƒβŸ©; rcases j with ⟨⟨j₁ | jβ‚‚βŸ© | jβ‚ƒβŸ©; simp [indefinite_diagonal, matrix.diagonal], end /-- An equivalence between two possible definitions of the classical Lie algebra of type B. -/ noncomputable def type_B_equiv_so' [invertible (2 : R)] : type_B l R ≃ₗ⁅R⁆ so' (unit βŠ• l) l R := begin apply (skew_adjoint_matrices_lie_subalgebra_equiv (JB l R) (PB l R) (is_unit_PB l R)).trans, symmetry, apply (skew_adjoint_matrices_lie_subalgebra_equiv_transpose (indefinite_diagonal (unit βŠ• l) l R) (matrix.reindex_alg_equiv (equiv.sum_assoc punit l l)) (matrix.reindex_transpose _ _)).trans, apply lie_algebra.equiv.of_eq, ext A, rw [JB_transform, ← unit_of_invertible_val (2 : R), lie_subalgebra.mem_coe, lie_subalgebra.mem_coe, mem_skew_adjoint_matrices_lie_subalgebra_unit_smul], simpa [indefinite_diagonal_assoc], end end orthogonal end lie_algebra
67f89994ce6ad78b850bfcabf98b3ccd9a632708
cc060cf567f81c404a13ee79bf21f2e720fa6db0
/lean/20171217-issue-1552.lean
1b1dde552e3472fb2f4480bec10668739ff173d3
[ "Apache-2.0" ]
permissive
semorrison/proof
cf0a8c6957153bdb206fd5d5a762a75958a82bca
5ee398aa239a379a431190edbb6022b1a0aa2c70
refs/heads/master
1,610,414,502,842
1,518,696,851,000
1,518,696,851,000
78,375,937
2
1
null
null
null
null
UTF-8
Lean
false
false
2,136
lean
structure Category := ( Obj : Type ) ( Hom : Obj β†’ Obj β†’ Type ) (compose : Ξ  { X Y Z : Obj }, Hom X Y β†’ Hom Y Z β†’ Hom X Z) (associativity : βˆ€ { W X Y Z : Obj } (f : Hom W X) (g : Hom X Y) (h : Hom Y Z), compose (compose f g) h = compose f (compose g h)) attribute [ematch] Category.associativity structure Functor (C : Category) (D : Category) := (onObjects : C.Obj β†’ D.Obj) (onMorphisms : Ξ  { X Y : C.Obj }, C.Hom X Y β†’ D.Hom (onObjects X) (onObjects Y)) (functoriality : βˆ€ { X Y Z : C.Obj } (f : C.Hom X Y) (g : C.Hom Y Z), onMorphisms (C.compose f g) = D.compose (onMorphisms f) (onMorphisms g)) attribute [simp,ematch] Functor.functoriality instance Functor_to_onObjects { C D : Category }: has_coe_to_fun (Functor C D) := { F := Ξ» f, C.Obj β†’ D.Obj, coe := Functor.onObjects } -- attribute [reducible] lift_t coe_t coe_b has_coe_to_fun.coe set_option pp.all true @[ematch] def FX { C D : Category } { F : Functor C D } { X : C.Obj } : F X = F.onObjects X := begin unfold coe_fn, unfold has_coe_to_fun.coe, end structure NaturalTransformation { C : Category } { D : Category } ( F G : Functor C D ) := (components: Ξ  X : C.Obj, D.Hom (F X) (G X)) -- With this definition of components (not using the coercion) the `eblast` below works. -- (components: Ξ  X : C.Obj, D.Hom (F.onObjects X) (G.onObjects X)) (naturality: βˆ€ { X Y : C.Obj } (f : C.Hom X Y), D.compose (F.onMorphisms f) (components Y) = D.compose (components X) (G.onMorphisms f)) attribute [ematch] NaturalTransformation.naturality definition vertical_composition_of_NaturalTransformations { C : Category } { D : Category } { F G H : Functor C D } ( Ξ± : NaturalTransformation F G ) ( Ξ² : NaturalTransformation G H ) : NaturalTransformation F H := { components := Ξ» X, D.compose (Ξ±.components X) (Ξ².components X), naturality := begin intros, dunfold coe_fn, dunfold has_coe_to_fun.coe, begin[smt] eblast, end end }
720259c257d46e22a941d8aa8c8f920d31030f5f
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/529.lean
2cef8973f8b39df7a87468b239ab70bcb28e5bf3
[ "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
422
lean
import Lean namespace Foo def f (x y : Nat) := x + y + x scoped infix:65 " ++ " => f end Foo open Foo in def f1 (x : Nat) := x ++ x #print f1 def f2 (x : Nat) := open Foo in x ++ x #print f2 open Foo in #print f1 open Lean.Parser.Command in def syntaxMatcher : Unit := match Lean.Syntax.missing with | `(declId|$id:ident$[.{$ls,*}]?) => () -- unknown parser declId | _ => ()
5582ef9f0bcd034d6c81014379f9ae3a06810f3b
f4bff2062c030df03d65e8b69c88f79b63a359d8
/src/game/series/tempLevel01.lean
f94acbebfd28a4be734b6731bc8aab820020f509
[ "Apache-2.0" ]
permissive
adastra7470/real-number-game
776606961f52db0eb824555ed2f8e16f92216ea3
f9dcb7d9255a79b57e62038228a23346c2dc301b
refs/heads/master
1,669,221,575,893
1,594,669,800,000
1,594,669,800,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,117
lean
import game.series.L01defs namespace xena -- begin hide -- if we want to use sigma notation, use -- import algebra.big_operators -- open_locale big_operators -- https://leanprover-community.github.io/mathlib_docs/algebra/big_operators.html -- end hide /- If $\sum a_n$ converges, then $a_n \to 0$. We take the approach of showing that $(S_n) β†’ M$ then $(S_{n+1}) β†’ M$, and then using the fact that $a_{n+1} = S_{n+1} - S_n$. -/ def kth_partial_sum (a : β„• β†’ ℝ) (k : β„•) := (finset.range (k+1)).sum a def seq_partials_over (a : β„• β†’ ℝ ) : β„• β†’ ℝ := (Ξ» (n : β„•), kth_partial_sum a n ) def series_converges (a : β„• β†’ ℝ) := is_convergent (seq_partials_over a) /- Lemma If partial sum sequence of $a_n$ convergent, $a_n β†’ 0$. -/ lemma seqlim_0_if_sum_converges (a : β„• β†’ ℝ) : series_converges a β†’ is_limit a 0 := begin intro h, cases h with M Mislimit, -- shift_rule to show that shifted sequence of partial sums also tends to M have fact := shift_rule (seq_partials_over a) 1 M, have fact2 := iff.mp fact Mislimit, -- express `a (m+1)` using partial sums, sum_range_succ seems best way have fact3 : βˆ€ m : β„•, kth_partial_sum a (m+1) = a (m+1) + kth_partial_sum a (m), intro m, from finset.sum_range_succ a (m+1), --we really want fact4, but sum_range_succ couldn't do it directly? have fact4 : βˆ€ (m : β„•), a (m + 1) = kth_partial_sum a (m+1) - kth_partial_sum a (m), intro n, specialize fact3 n, -- do I need to do this to reorganise inside quantiifer? linarith, -- we can rewrite our goal in terms of `a` shifted by +1 have fact5 : is_limit a 0 ↔ is_limit (Ξ» (m : β„•), a (m+1)) 0, from shift_rule a 1 0, rw fact5, have fact6: (Ξ» (n : β„•), a (n + 1)) = Ξ» (n: β„•), (kth_partial_sum a (n + 1) - kth_partial_sum a n), exact funext fact4, -- suggest gave me this! rw fact6, unfold seq_partials_over at Mislimit fact2, -- just for clarity have fact7 := lim_linear (Ξ» (n : β„•), kth_partial_sum a (n + 1)) (Ξ» (n : β„•), kth_partial_sum a n) M M 1 (-1) fact2 Mislimit, simp at fact7, exact fact7, end end xena
46d686936d98b1140576cfc85e68949a1ca756e9
aa3f8992ef7806974bc1ffd468baa0c79f4d6643
/library/logic/axioms/classical.lean
26a8b3f4a5865f330a6f7c1ed9482ef224497bec
[ "Apache-2.0" ]
permissive
codyroux/lean
7f8dff750722c5382bdd0a9a9275dc4bb2c58dd3
0cca265db19f7296531e339192e9b9bae4a31f8b
refs/heads/master
1,610,909,964,159
1,407,084,399,000
1,416,857,075,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,813
lean
-- Copyright (c) 2014 Microsoft Corporation. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Author: Leonardo de Moura -- logic.axioms.classical -- ====================== import logic.quantifiers logic.cast algebra.relation open eq.ops axiom prop_complete (a : Prop) : a = true ∨ a = false theorem cases (P : Prop β†’ Prop) (H1 : P true) (H2 : P false) (a : Prop) : P a := or.elim (prop_complete a) (assume Ht : a = true, Ht⁻¹ β–Έ H1) (assume Hf : a = false, Hf⁻¹ β–Έ H2) theorem cases_on (a : Prop) {P : Prop β†’ Prop} (H1 : P true) (H2 : P false) : P a := cases P H1 H2 a -- this supercedes the em in decidable theorem em (a : Prop) : a ∨ Β¬a := or.elim (prop_complete a) (assume Ht : a = true, or.inl (eq_true_elim Ht)) (assume Hf : a = false, or.inr (eq_false_elim Hf)) theorem prop_complete_swapped (a : Prop) : a = false ∨ a = true := cases (Ξ» x, x = false ∨ x = true) (or.inr rfl) (or.inl rfl) a theorem propext {a b : Prop} (Hab : a β†’ b) (Hba : b β†’ a) : a = b := or.elim (prop_complete a) (assume Hat, or.elim (prop_complete b) (assume Hbt, Hat ⬝ Hbt⁻¹) (assume Hbf, false_elim (Hbf β–Έ (Hab (eq_true_elim Hat))))) (assume Haf, or.elim (prop_complete b) (assume Hbt, false_elim (Haf β–Έ (Hba (eq_true_elim Hbt)))) (assume Hbf, Haf ⬝ Hbf⁻¹)) theorem iff_to_eq {a b : Prop} (H : a ↔ b) : a = b := iff.elim (assume H1 H2, propext H1 H2) H theorem iff_eq_eq {a b : Prop} : (a ↔ b) = (a = b) := propext (assume H, iff_to_eq H) (assume H, eq_to_iff H) open relation theorem iff_congruence [instance] (P : Prop β†’ Prop) : congruence iff iff P := congruence.mk (take (a b : Prop), assume H : a ↔ b, show P a ↔ P b, from eq_to_iff (iff_to_eq H β–Έ eq.refl (P a)))
b4f4889ec6eec31c56c9dc1de01f3fe8768977d2
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/Lean3Lib/init/data/string/basic_auto.lean
81ea8e834121accec7b4c242fb98e56a5b08c47a
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
5,198
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.data.list.basic import Mathlib.Lean3Lib.init.data.char.basic universes l u_1 namespace Mathlib /- In the VM, strings are implemented using a dynamic array and UTF-8 encoding. TODO: we currently cannot mark string_imp as private because we need to bind string_imp.mk and string_imp.cases_on in the VM. -/ structure string_imp where data : List char def string := string_imp def list.as_string (s : List char) : string := string_imp.mk s namespace string protected instance has_lt : HasLess string := { Less := fun (s₁ sβ‚‚ : string) => string_imp.data s₁ < string_imp.data sβ‚‚ } /- Remark: this function has a VM builtin efficient implementation. -/ protected instance has_decidable_lt (s₁ : string) (sβ‚‚ : string) : Decidable (s₁ < sβ‚‚) := list.has_decidable_lt (string_imp.data s₁) (string_imp.data sβ‚‚) protected instance has_decidable_eq : DecidableEq string := fun (_x : string) => sorry def empty : string := string_imp.mk [] def length : string β†’ β„• := sorry /- The internal implementation uses dynamic arrays and will perform destructive updates if the string is not shared. -/ def push : string β†’ char β†’ string := sorry /- The internal implementation uses dynamic arrays and will perform destructive updates if the string is not shared. -/ def append : string β†’ string β†’ string := sorry /- O(n) in the VM, where n is the length of the string -/ def to_list : string β†’ List char := sorry def fold {Ξ± : Type u_1} (a : Ξ±) (f : Ξ± β†’ char β†’ Ξ±) (s : string) : Ξ± := list.foldl f a (to_list s) /- In the VM, the string iterator is implemented as a pointer to the string being iterated + index. TODO: we currently cannot mark interator_imp as private because we need to bind string_imp.mk and string_imp.cases_on in the VM. -/ structure iterator_imp where fst : List char snd : List char def iterator := iterator_imp def mk_iterator : string β†’ iterator := sorry namespace iterator def curr : iterator β†’ char := sorry /- In the VM, `set_curr` is constant time if the string being iterated is not shared and linear time if it is. -/ def set_curr : iterator β†’ char β†’ iterator := sorry def next : iterator β†’ iterator := sorry def prev : iterator β†’ iterator := sorry def has_next : iterator β†’ Bool := sorry def has_prev : iterator β†’ Bool := sorry def insert : iterator β†’ string β†’ iterator := sorry def remove : iterator β†’ β„• β†’ iterator := sorry /- In the VM, `to_string` is a constant time operation. -/ def to_string : iterator β†’ string := sorry def to_end : iterator β†’ iterator := sorry def next_to_string : iterator β†’ string := sorry def prev_to_string : iterator β†’ string := sorry protected def extract_core : List char β†’ List char β†’ Option (List char) := sorry def extract : iterator β†’ iterator β†’ Option string := sorry end iterator end string /- The following definitions do not have builtin support in the VM -/ protected instance string.inhabited : Inhabited string := { default := string.empty } protected instance string.has_sizeof : SizeOf string := { sizeOf := string.length } protected instance string.has_append : Append string := { append := string.append } namespace string def str : string β†’ char β†’ string := push def is_empty (s : string) : Bool := to_bool (length s = 0) def front (s : string) : char := iterator.curr (mk_iterator s) def back (s : string) : char := iterator.curr (iterator.prev (iterator.to_end (mk_iterator s))) def join (l : List string) : string := list.foldl (fun (r s : string) => r ++ s) empty l def singleton (c : char) : string := push empty c def intercalate (s : string) (ss : List string) : string := list.as_string (list.intercalate (to_list s) (list.map to_list ss)) namespace iterator def nextn : iterator β†’ β„• β†’ iterator := sorry def prevn : iterator β†’ β„• β†’ iterator := sorry end iterator def pop_back (s : string) : string := iterator.prev_to_string (iterator.prev (iterator.to_end (mk_iterator s))) def popn_back (s : string) (n : β„•) : string := iterator.prev_to_string (iterator.prevn (iterator.to_end (mk_iterator s)) n) def backn (s : string) (n : β„•) : string := iterator.next_to_string (iterator.prevn (iterator.to_end (mk_iterator s)) n) end string protected def char.to_string (c : char) : string := string.singleton c def string.to_nat (s : string) : β„• := to_nat_core (string.mk_iterator s) (string.length s) 0 namespace string theorem empty_ne_str (c : char) (s : string) : empty β‰  str s c := sorry theorem str_ne_empty (c : char) (s : string) : str s c β‰  empty := ne.symm (empty_ne_str c s) theorem str_ne_str_left {c₁ : char} {cβ‚‚ : char} (s₁ : string) (sβ‚‚ : string) : c₁ β‰  cβ‚‚ β†’ str s₁ c₁ β‰  str sβ‚‚ cβ‚‚ := sorry theorem str_ne_str_right (c₁ : char) (cβ‚‚ : char) {s₁ : string} {sβ‚‚ : string} : s₁ β‰  sβ‚‚ β†’ str s₁ c₁ β‰  str sβ‚‚ cβ‚‚ := sorry end Mathlib
19ed879ec59a617175139616f73703fc334ead98
82e44445c70db0f03e30d7be725775f122d72f3e
/src/geometry/manifold/conformal_groupoid.lean
423283f79a1ec155c2ffbe5ba45f8fdf4b12f96b
[ "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
1,056
lean
/- Copyright (c) 2021 Yourong Zang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yourong Zang -/ import analysis.calculus.conformal import geometry.manifold.charted_space /-! # Conformal Groupoid In this file we define the groupoid of conformal maps on normed spaces. ## Main definitions * `conformal_groupoid`: the groupoid of conformal local homeomorphisms. ## Tags conformal, groupoid -/ variables {X : Type*} [normed_group X] [normed_space ℝ X] /-- The pregroupoid of conformal maps. -/ def conformal_pregroupoid : pregroupoid X := { property := Ξ» f u, βˆ€ x, x ∈ u β†’ conformal_at f x, comp := Ξ» f g u v hf hg hu hv huv x hx, (hg (f x) hx.2).comp x (hf x hx.1), id_mem := Ξ» x hx, conformal_at_id x, locality := Ξ» f u hu h x hx, let ⟨v, h₁, hβ‚‚, hβ‚ƒβŸ© := h x hx in h₃ x ⟨hx, hβ‚‚βŸ©, congr := Ξ» f g u hu h hf x hx, (hf x hx).congr hx hu h, } /-- The groupoid of conformal maps. -/ def conformal_groupoid : structure_groupoid X := conformal_pregroupoid.groupoid
9d90053d907fa808ea2c6e94e5e6b33970cc5b88
da3a76c514d38801bae19e8a9e496dc31f8e5866
/library/init/meta/interactive.lean
3b6616f5962463a7ce12f016e8a99ebaefc69888
[ "Apache-2.0" ]
permissive
cipher1024/lean
270c1ac5781e6aee12f5c8d720d267563a164beb
f5cbdff8932dd30c6dd8eec68f3059393b4f8b3a
refs/heads/master
1,611,223,459,029
1,487,566,573,000
1,487,566,573,000
83,356,543
0
0
null
1,488,229,336,000
1,488,229,336,000
null
UTF-8
Lean
false
false
23,658
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.meta.tactic init.meta.rewrite_tactic init.meta.simp_tactic import init.meta.smt.congruence_closure init.category.combinators import init.meta.lean.parser init.meta.quote open lean open lean.parser local postfix ?:9001 := optional local postfix *:9001 := many local notation p ` ?: `:100 d := (Ξ» o, option.get_or_else o d) <$> p? namespace interactive /-- (parse p) as the parameter type of an interactive tactic will instruct the Lean parser to run `p` when parsing the parameter and to pass the parsed value as an argument to the tactic. -/ @[reducible] meta def parse {Ξ± : Type} [has_quote Ξ±] (p : parser Ξ±) : Type := Ξ± namespace types variables {Ξ± Ξ² : Type} meta def list_of (p : parser Ξ±) := tk "[" *> sep_by "," p <* tk "]" /-- A 'tactic expression', which uses right-binding power 2 so that it is terminated by '<|>' (rbp 2), ';' (rbp 1), and ',' (rbp 0). It should be used for any (potentially) trailing expression parameters. -/ meta def texpr := qexpr 2 meta def using_ident := (tk "using" *> ident)? meta def with_ident_list := (tk "with" *> ident*) ?: [] meta def without_ident_list := (tk "without" *> ident*) ?: [] meta def location := (tk "at" *> ident*) ?: [] meta def qexpr_list := list_of (qexpr 0) meta def opt_qexpr_list := qexpr_list ?: [] meta def qexpr_list_or_texpr := qexpr_list <|> return <$> texpr end types end interactive namespace tactic meta def report_resolve_name_failure {Ξ± : Type} (e : expr) (n : name) : tactic Ξ± := if e^.is_choice_macro then fail ("failed to resolve name '" ++ to_string n ++ "', it is overloaded") else fail ("failed to resolve name '" ++ to_string n ++ "', unexpected result") /- allows metavars and report errors -/ meta def i_to_expr (q : pexpr) : tactic expr := to_expr q tt tt /- doesn't allows metavars and report errors -/ meta def i_to_expr_strict (q : pexpr) : tactic expr := to_expr q ff tt namespace interactive open interactive interactive.types expr /- itactic: parse a nested "interactive" tactic. That is, parse `(` tactic `)` -/ meta def itactic : Type := tactic unit /- irtactic: parse a nested "interactive" tactic. That is, parse `(` tactic `)` It is similar to itactic, but the interactive mode will report errors when any of the nested tactics fail. This is good for tactics such as async and abstract. -/ meta def irtactic : Type := tactic unit /-- This tactic applies to a goal that is either a Pi/forall or starts with a let binder. If the current goal is a Pi/forall `βˆ€ x:T, U` (resp `let x:=t in U`) then intro puts `x:T` (resp `x:=t`) in the local context. The new subgoal target is `U`. If the goal is an arrow `T β†’ U`, then it puts in the local context either `h:T`, and the new goal target is `U`. If the goal is neither a Pi/forall nor starting with a let definition, the tactic `intro` applies the tactic `whnf` until the tactic `intro` can be applied or the goal is not `head-reducible`. -/ meta def intro : parse ident? β†’ tactic unit | none := intro1 >> skip | (some h) := tactic.intro h >> skip /-- Similar to `intro` tactic. The tactic `intros` will keep introducing new hypotheses until the goal target is not a Pi/forall or let binder. The variant `intros h_1 ... h_n` introduces `n` new hypotheses using the given identifiers to name them. -/ meta def intros : parse ident* β†’ tactic unit | [] := tactic.intros >> skip | hs := intro_lst hs >> skip /-- The tactic `rename h₁ hβ‚‚` renames hypothesis `h₁` into `hβ‚‚` in the current local context. -/ meta def rename : parse ident β†’ parse ident β†’ tactic unit := tactic.rename /-- This tactic applies to any goal. The argument term is a term well-formed in the local context of the main goal. The tactic apply tries to match the current goal against the conclusion of the type of term. If it succeeds, then the tactic returns as many subgoals as the number of non-dependent premises that have not been fixed by type inference or type class resolution. The tactic `apply` uses higher-order pattern matching, type class resolution, and first-order unification with dependent types. -/ meta def apply (q : parse texpr) : tactic unit := i_to_expr q >>= tactic.apply /-- Similar to the `apply` tactic, but it also creates subgoals for dependent premises that have not been fixed by type inference or type class resolution. -/ meta def fapply (q : parse texpr) : tactic unit := i_to_expr q >>= tactic.fapply /-- This tactic tries to close the main goal `... |- U` using type class resolution. It succeeds if it generates a term of type `U` using type class resolution. -/ meta def apply_instance : tactic unit := tactic.apply_instance /-- This tactic applies to any goal. It behaves like `exact` with a big difference: the user can leave some holes `_` in the term. `refine` will generate as many subgoals as there are holes in the term. Note that some holes may be implicit. The type of holes must be either synthesized by the system or declared by an explicit type ascription like (e.g., `(_ : nat β†’ Prop)`). -/ meta def refine (q : parse texpr) : tactic unit := tactic.refine q tt /-- This tactic looks in the local context for an hypothesis which type is equal to the goal target. If it is the case, the subgoal is proved. Otherwise, it fails. -/ meta def assumption : tactic unit := tactic.assumption /-- This tactic applies to any goal. `change U` replaces the main goal target `T` with `U` providing that `U` is well-formed with respect to the main goal local context, and `T` and `U` are definitionally equal. -/ meta def change (q : parse texpr) : tactic unit := i_to_expr q >>= tactic.change /-- This tactic applies to any goal. It gives directly the exact proof term of the goal. Let `T` be our goal, let `p` be a term of type `U` then `exact p` succeeds iff `T` and `U` are definitionally equal. -/ meta def exact (q : parse texpr) : tactic unit := do tgt : expr ← target, i_to_expr_strict ``(%%q : %%tgt) >>= tactic.exact private meta def get_locals : list name β†’ tactic (list expr) | [] := return [] | (n::ns) := do h ← get_local n, hs ← get_locals ns, return (h::hs) /-- `revert h₁ ... hβ‚™` applies to any goal with hypotheses `h₁` ... `hβ‚™`. It moves the hypotheses and its dependencies to the goal target. This tactic is the inverse of `intro`. -/ meta def revert (ids : parse ident*) : tactic unit := do hs ← get_locals ids, revert_lst hs, skip /- Return (some a) iff p is of the form (- a) -/ private meta def is_neg (p : pexpr) : option pexpr := /- Remark: we use the low-level to_raw_expr and of_raw_expr to be able to pattern match pre-terms. This is a low-level trick (aka hack). -/ match pexpr.to_raw_expr p with | (app (const c []) arg) := if c = `neg then some (pexpr.of_raw_expr arg) else none | _ := none end private meta def resolve_name' (n : name) : tactic expr := do { e ← resolve_name n, match e with | expr.const n _ := mk_const n -- create metavars for universe levels | _ := return e end } /- Version of to_expr that tries to bypass the elaborator if `p` is just a constant or local constant. This is not an optimization, by skipping the elaborator we make sure that unwanted resolution is used. Example: the elaborator will force any unassigned ?A that must have be an instance of (has_one ?A) to nat. Remark: another benefit is that auxiliary temporary metavariables do not appear in error messages. -/ private meta def to_expr' (p : pexpr) : tactic expr := let e := pexpr.to_raw_expr p in match e with | (const c []) := do new_e ← resolve_name' c, save_type_info new_e e, return new_e | (local_const c _ _ _) := do new_e ← resolve_name' c, save_type_info new_e e, return new_e | _ := i_to_expr p end private meta def maybe_save_info : option pos β†’ tactic unit | (some p) := save_info p.1 p.2 | none := skip private meta def symm_expr := bool Γ— expr Γ— option pos private meta def to_symm_expr_list : list pexpr β†’ tactic (list symm_expr) | [] := return [] | (p::ps) := match is_neg p with | some a := do r ← to_expr' a, rs ← to_symm_expr_list ps, return ((tt, r, pexpr.pos p) :: rs) | none := do r ← to_expr' p, rs ← to_symm_expr_list ps, return ((ff, r, pexpr.pos p) :: rs) end private meta def rw_goal : transparency β†’ list symm_expr β†’ tactic unit | m [] := return () | m ((symm, e, pos)::es) := maybe_save_info pos >> rewrite_core m tt tt occurrences.all symm e >> rw_goal m es private meta def rw_hyp : transparency β†’ list symm_expr β†’ name β†’ tactic unit | m [] hname := return () | m ((symm, e, pos)::es) hname := do h ← get_local hname, maybe_save_info pos, rewrite_at_core m tt tt occurrences.all symm e h, rw_hyp m es hname private meta def rw_hyps : transparency β†’ list symm_expr β†’ list name β†’ tactic unit | m es [] := return () | m es (h::hs) := rw_hyp m es h >> rw_hyps m es hs private meta def rw_core (m : transparency) (hs : parse qexpr_list_or_texpr) (loc : parse location) : tactic unit := do hlist ← to_symm_expr_list hs, match loc with | [] := rw_goal m hlist >> try (reflexivity reducible) | hs := rw_hyps m hlist hs >> try (reflexivity reducible) end meta def rewrite : parse qexpr_list_or_texpr β†’ parse location β†’ tactic unit := rw_core reducible meta def rw : parse qexpr_list_or_texpr β†’ parse location β†’ tactic unit := rewrite /- rewrite followed by assumption -/ meta def rwa (q : parse qexpr_list_or_texpr) (l : parse location) : tactic unit := rewrite q l >> try assumption meta def erewrite : parse qexpr_list_or_texpr β†’ parse location β†’ tactic unit := rw_core semireducible meta def erw : parse qexpr_list_or_texpr β†’ parse location β†’ tactic unit := erewrite private meta def get_type_name (e : expr) : tactic name := do e_type ← infer_type e >>= whnf, (const I ls) ← return $ get_app_fn e_type, return I meta def induction (p : parse texpr) (rec_name : parse using_ident) (ids : parse with_ident_list) : tactic unit := do e ← i_to_expr p, tactic.induction e ids rec_name, return () meta def cases (p : parse texpr) (ids : parse with_ident_list) : tactic unit := do e ← i_to_expr p, tactic.cases e ids meta def destruct (p : parse texpr) : tactic unit := i_to_expr p >>= tactic.destruct meta def generalize (p : parse qexpr) (x : parse ident) : tactic unit := do e ← i_to_expr p, tactic.generalize e x meta def trivial : tactic unit := tactic.triv <|> tactic.reflexivity <|> tactic.contradiction <|> fail "trivial tactic failed" /-- Closes the main goal using sorry. -/ meta def admit : tactic unit := tactic.admit /-- This tactic applies to any goal. The contradiction tactic attempts to find in the current local context an hypothesis that is equivalent to an empty inductive type (e.g. `false`), a hypothesis of the form `c_1 ... = c_2 ...` where `c_1` and `c_2` are distinct constructors, or two contradictory hypotheses. -/ meta def contradiction : tactic unit := tactic.contradiction meta def repeat : itactic β†’ tactic unit := tactic.repeat meta def try : itactic β†’ tactic unit := tactic.try meta def solve1 : itactic β†’ tactic unit := tactic.solve1 meta def abstract (id : parse ident? ) (tac : irtactic) : tactic unit := tactic.abstract tac id meta def all_goals : itactic β†’ tactic unit := tactic.all_goals meta def any_goals : itactic β†’ tactic unit := tactic.any_goals meta def focus (tac : irtactic) : tactic unit := tactic.focus [tac] /-- This tactic applies to any goal. `assert h : T` adds a new hypothesis of name `h` and type `T` to the current goal and opens a new subgoal with target `T`. The new subgoal becomes the main goal. -/ meta def assert (h : parse ident) (q : parse $ tk ":" *> texpr) : tactic unit := do e ← i_to_expr_strict q, tactic.assert h e meta def define (h : parse ident) (q : parse $ tk ":" *> texpr) : tactic unit := do e ← i_to_expr_strict q, tactic.define h e /-- This tactic applies to any goal. `assertv h : T := p` adds a new hypothesis of name `h` and type `T` to the current goal if `p` a term of type `T`. -/ meta def assertv (h : parse ident) (q₁ : parse $ tk ":" *> texpr) (qβ‚‚ : parse $ tk ":=" *> texpr) : tactic unit := do t ← i_to_expr_strict q₁, v ← i_to_expr_strict ``(%%qβ‚‚ : %%t), tactic.assertv h t v meta def definev (h : parse ident) (q₁ : parse $ tk ":" *> texpr) (qβ‚‚ : parse $ tk ":=" *> texpr) : tactic unit := do t ← i_to_expr_strict q₁, v ← i_to_expr_strict ``(%%qβ‚‚ : %%t), tactic.definev h t v meta def note (h : parse ident) (q : parse $ tk ":=" *> texpr) : tactic unit := do p ← i_to_expr_strict q, tactic.note h p meta def pose (h : parse ident) (q : parse $ tk ":=" *> texpr) : tactic unit := do p ← i_to_expr_strict q, tactic.pose h p /-- This tactic displays the current state in the tracing buffer. -/ meta def trace_state : tactic unit := tactic.trace_state /-- `trace a` displays `a` in the tracing buffer. -/ meta def trace {Ξ± : Type} [has_to_tactic_format Ξ±] (a : Ξ±) : tactic unit := tactic.trace a meta def existsi (e : parse texpr) : tactic unit := i_to_expr e >>= tactic.existsi /-- This tactic applies to a goal such that its conclusion is an inductive type (say `I`). It tries to apply each constructor of `I` until it succeeds. -/ meta def constructor : tactic unit := tactic.constructor meta def left : tactic unit := tactic.left meta def right : tactic unit := tactic.right meta def split : tactic unit := tactic.split meta def exfalso : tactic unit := tactic.exfalso /-- The injection tactic is based on the fact that constructors of inductive datatypes are injections. That means that if `c` is a constructor of an inductive datatype, and if `(c t₁)` and `(c tβ‚‚)` are two terms that are equal then `t₁` and `tβ‚‚` are equal too. If `q` is a proof of a statement of conclusion `t₁ = tβ‚‚`, then injection applies injectivity to derive the equality of all arguments of `t₁` and `tβ‚‚` placed in the same positions. For example, from `(a::b) = (c::d)` we derive `a=c` and `b=d`. To use this tactic `t₁` and `tβ‚‚` should be constructor applications of the same constructor. Given `h : a::b = c::d`, the tactic `injection h` adds to new hypothesis with types `a = c` and `b = d` to the main goal. The tactic `injection h with h₁ hβ‚‚` uses the names `h₁` an `hβ‚‚` to name the new hypotheses. -/ meta def injection (q : parse texpr) (hs : parse with_ident_list) : tactic unit := do e ← i_to_expr q, tactic.injection_with e hs private meta def add_simps : simp_lemmas β†’ list name β†’ tactic simp_lemmas | s [] := return s | s (n::ns) := do s' ← s^.add_simp n, add_simps s' ns private meta def report_invalid_simp_lemma {Ξ± : Type} (n : name): tactic Ξ± := fail ("invalid simplification lemma '" ++ to_string n ++ "' (use command 'set_option trace.simp_lemmas true' for more details)") private meta def simp_lemmas.resolve_and_add (s : simp_lemmas) (n : name) (ref : expr) : tactic simp_lemmas := do e ← resolve_name n, match e with | expr.const n _ := (do b ← is_valid_simp_lemma_cnst reducible n, guard b, save_const_type_info n ref, s^.add_simp n) <|> (do eqns ← get_eqn_lemmas_for tt n, guard (eqns^.length > 0), save_const_type_info n ref, add_simps s eqns) <|> report_invalid_simp_lemma n | _ := (do b ← is_valid_simp_lemma reducible e, guard b, try (save_type_info e ref), s^.add e) <|> report_invalid_simp_lemma n end private meta def simp_lemmas.add_pexpr (s : simp_lemmas) (p : pexpr) : tactic simp_lemmas := let e := pexpr.to_raw_expr p in match e with | (const c []) := simp_lemmas.resolve_and_add s c e | (local_const c _ _ _) := simp_lemmas.resolve_and_add s c e | _ := do new_e ← i_to_expr p, s^.add new_e end private meta def simp_lemmas.append_pexprs : simp_lemmas β†’ list pexpr β†’ tactic simp_lemmas | s [] := return s | s (l::ls) := do new_s ← simp_lemmas.add_pexpr s l, simp_lemmas.append_pexprs new_s ls private meta def mk_simp_set (attr_names : list name) (hs : list pexpr) (ex : list name) : tactic simp_lemmas := do sβ‚€ ← join_user_simp_lemmas attr_names, s₁ ← simp_lemmas.append_pexprs sβ‚€ hs, return $ simp_lemmas.erase s₁ ex private meta def simp_goal (cfg : simp_config) : simp_lemmas β†’ tactic unit | s := do (new_target, Heq) ← target >>= simplify_core cfg s `eq, tactic.assert `Htarget new_target, swap, Ht ← get_local `Htarget, mk_eq_mpr Heq Ht >>= tactic.exact private meta def simp_hyp (cfg : simp_config) (s : simp_lemmas) (h_name : name) : tactic unit := do h ← get_local h_name, htype ← infer_type h, (new_htype, eqpr) ← simplify_core cfg s `eq htype, tactic.assert (expr.local_pp_name h) new_htype, mk_eq_mp eqpr h >>= tactic.exact, try $ tactic.clear h private meta def simp_hyps (cfg : simp_config) : simp_lemmas β†’ list name β†’ tactic unit | s [] := skip | s (h::hs) := simp_hyp cfg s h >> simp_hyps s hs private meta def simp_core (cfg : simp_config) (ctx : list expr) (hs : list pexpr) (attr_names : list name) (ids : list name) (loc : list name) : tactic unit := do s ← mk_simp_set attr_names hs ids, s ← s^.append ctx, match loc : _ β†’ tactic unit with | [] := simp_goal cfg s | _ := simp_hyps cfg s loc end, try tactic.triv, try (tactic.reflexivity reducible) /-- This tactic uses lemmas and hypotheses to simplify the main goal target or non-dependent hypotheses. It has many variants. - `simp` simplifies the main goal target using lemmas tagged with the attribute `[simp]`. - `simp [h_1, ..., h_n]` simplifies the main goal target using the lemmas tagged with the attribute `[simp]` and the given `h_i`s. The `h_i`'s are terms. If a `h_i` is a definition `f`, then the equational lemmas associated with `f` are used. This is a convenient way to "unfold" `f`. - `simp without id_1 ... id_n` simplifies the main goal target using the lemmas tagged with the attribute `[simp]`, but removes the ones named `id_i`s. - `simp at h` simplifies the non dependent hypothesis `h : T`. The tactic fails if the target or another hypothesis depends on `h`. - `simp with attr` simplifies the main goal target using the lemmas tagged with the attribute `[attr]`. -/ meta def simp (hs : parse opt_qexpr_list) (attr_names : parse with_ident_list) (ids : parse without_ident_list) (loc : parse location) (cfg : simp_config := {}) : tactic unit := simp_core cfg [] hs attr_names ids loc /-- Similar to the `simp` tactic, but adds all applicable hypotheses as simplification rules. -/ meta def simp_using_hs (hs : parse opt_qexpr_list) (attr_names : parse with_ident_list) (ids : parse without_ident_list) (cfg : simp_config := {}) : tactic unit := do ctx ← collect_ctx_simps, simp_core cfg ctx hs attr_names ids [] meta def simph (hs : parse opt_qexpr_list) (attr_names : parse with_ident_list) (ids : parse without_ident_list) (cfg : simp_config := {}) : tactic unit := simp_using_hs hs attr_names ids cfg meta def simp_intros (ids : parse ident*) (hs : parse opt_qexpr_list) (attr_names : parse with_ident_list) (wo_ids : parse without_ident_list) (cfg : simp_config := {}) : tactic unit := do s ← mk_simp_set attr_names hs wo_ids, match ids with | [] := simp_intros_using s cfg | ns := simp_intro_lst_using ns s cfg end, try triv >> try (reflexivity reducible) meta def simph_intros (ids : parse ident*) (hs : parse opt_qexpr_list) (attr_names : parse with_ident_list) (wo_ids : parse without_ident_list) (cfg : simp_config := {}) : tactic unit := do s ← mk_simp_set attr_names hs wo_ids, match ids with | [] := simph_intros_using s cfg | ns := simph_intro_lst_using ns s cfg end, try triv >> try (reflexivity reducible) private meta def dsimp_hyps (s : simp_lemmas) : list name β†’ tactic unit | [] := skip | (h::hs) := get_local h >>= dsimp_at_core s meta def dsimp (es : parse opt_qexpr_list) (attr_names : parse with_ident_list) (ids : parse without_ident_list) : parse location β†’ tactic unit | [] := do s ← mk_simp_set attr_names es ids, tactic.dsimp_core s | hs := do s ← mk_simp_set attr_names es ids, dsimp_hyps s hs /-- This tactic applies to a goal that has the form `t ~ u` where `~` is a reflexive relation. That is, a relation which has a reflexivity lemma tagged with the attribute `[refl]`. The tactic checks whether `t` and `u` are definitionally equal and then solves the goal. -/ meta def reflexivity : tactic unit := tactic.reflexivity /-- Shorter name for the tactic `reflexivity`. -/ meta def refl : tactic unit := tactic.reflexivity meta def symmetry : tactic unit := tactic.symmetry meta def transitivity : tactic unit := tactic.transitivity meta def ac_reflexivity : tactic unit := tactic.ac_refl meta def ac_refl : tactic unit := tactic.ac_refl meta def cc : tactic unit := tactic.cc meta def subst (q : parse texpr) : tactic unit := i_to_expr q >>= tactic.subst >> try (tactic.reflexivity reducible) meta def clear : parse ident* β†’ tactic unit := tactic.clear_lst private meta def to_qualified_name_core : name β†’ list name β†’ tactic name | n [] := fail $ "unknown declaration '" ++ to_string n ++ "'" | n (ns::nss) := do curr ← return $ ns ++ n, env ← get_env, if env^.contains curr then return curr else to_qualified_name_core n nss private meta def to_qualified_name (n : name) : tactic name := do env ← get_env, if env^.contains n then return n else do ns ← open_namespaces, to_qualified_name_core n ns private meta def to_qualified_names : list name β†’ tactic (list name) | [] := return [] | (c::cs) := do new_c ← to_qualified_name c, new_cs ← to_qualified_names cs, return (new_c::new_cs) private meta def dunfold_hyps : list name β†’ list name β†’ tactic unit | cs [] := skip | cs (h::hs) := get_local h >>= dunfold_at cs >> dunfold_hyps cs hs meta def dunfold : parse ident* β†’ parse location β†’ tactic unit | cs [] := do new_cs ← to_qualified_names cs, tactic.dunfold new_cs | cs hs := do new_cs ← to_qualified_names cs, dunfold_hyps new_cs hs /- TODO(Leo): add support for non-refl lemmas -/ meta def unfold : parse ident* β†’ parse location β†’ tactic unit := dunfold private meta def dunfold_hyps_occs : name β†’ occurrences β†’ list name β†’ tactic unit | c occs [] := skip | c occs (h::hs) := get_local h >>= dunfold_core_at occs [c] >> dunfold_hyps_occs c occs hs meta def dunfold_occs : parse ident β†’ parse location β†’ list nat β†’ tactic unit | c [] ps := do new_c ← to_qualified_name c, tactic.dunfold_occs_of ps new_c | c hs ps := do new_c ← to_qualified_name c, dunfold_hyps_occs new_c (occurrences.pos ps) hs /- TODO(Leo): add support for non-refl lemmas -/ meta def unfold_occs : parse ident β†’ parse location β†’ list nat β†’ tactic unit := dunfold_occs private meta def delta_hyps : list name β†’ list name β†’ tactic unit | cs [] := skip | cs (h::hs) := get_local h >>= delta_at cs >> dunfold_hyps cs hs meta def delta : parse ident* β†’ parse location β†’ tactic unit | cs [] := do new_cs ← to_qualified_names cs, tactic.delta new_cs | cs hs := do new_cs ← to_qualified_names cs, delta_hyps new_cs hs end interactive end tactic
7ae15ab595e5d9a12a4b9a304b9794c2cd92304f
e00ea76a720126cf9f6d732ad6216b5b824d20a7
/src/tactic/omega/int/main.lean
7fa0b5ae7bd6f840b804f985b0151106ece08b41
[ "Apache-2.0" ]
permissive
vaibhavkarve/mathlib
a574aaf68c0a431a47fa82ce0637f0f769826bfe
17f8340912468f49bdc30acdb9a9fa02eeb0473a
refs/heads/master
1,621,263,802,637
1,585,399,588,000
1,585,399,588,000
250,833,447
0
0
Apache-2.0
1,585,410,341,000
1,585,410,341,000
null
UTF-8
Lean
false
false
6,297
lean
/- Copyright (c) 2019 Seul Baek. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Seul Baek Main procedure for linear integer arithmetic. -/ import tactic.omega.prove_unsats import tactic.omega.int.dnf import tactic.omega.misc open tactic namespace omega namespace int open_locale omega.int run_cmd mk_simp_attr `sugar attribute [sugar] ne not_le not_lt int.lt_iff_add_one_le or_false false_or and_true true_and ge gt mul_add add_mul one_mul mul_one mul_comm sub_eq_add_neg classical.imp_iff_not_or classical.iff_iff_not_or_and_or_not meta def desugar := `[try {simp only with sugar}] lemma univ_close_of_unsat_clausify (m : nat) (p : preform) : clauses.unsat (dnf (Β¬* p)) β†’ univ_close p (Ξ» x, 0) m | h1 := begin apply univ_close_of_valid, apply valid_of_unsat_not, apply unsat_of_clauses_unsat, exact h1 end /-- Given a (p : preform), return the expr of a (t : univ_close m p) -/ meta def prove_univ_close (m : nat) (p : preform) : tactic expr := do x ← prove_unsats (dnf (Β¬*p)), return `(univ_close_of_unsat_clausify %%`(m) %%`(p) %%x) /-- Reification to imtermediate shadow syntax that retains exprs -/ meta def to_exprterm : expr β†’ tactic exprterm | `(- %%x) := --return (exprterm.exp (-1 : int) x) ( do z ← eval_expr' int x, return (exprterm.cst (-z : int)) ) <|> ( return $ exprterm.exp (-1 : int) x ) | `(%%mx * %%zx) := do z ← eval_expr' int zx, return (exprterm.exp z mx) | `(%%t1x + %%t2x) := do t1 ← to_exprterm t1x, t2 ← to_exprterm t2x, return (exprterm.add t1 t2) | x := ( do z ← eval_expr' int x, return (exprterm.cst z) ) <|> ( return $ exprterm.exp 1 x ) /-- Reification to imtermediate shadow syntax that retains exprs -/ meta def to_exprform : expr β†’ tactic exprform | `(%%tx1 = %%tx2) := do t1 ← to_exprterm tx1, t2 ← to_exprterm tx2, return (exprform.eq t1 t2) | `(%%tx1 ≀ %%tx2) := do t1 ← to_exprterm tx1, t2 ← to_exprterm tx2, return (exprform.le t1 t2) | `(Β¬ %%px) := do p ← to_exprform px, return (exprform.not p) | `(%%px ∨ %%qx) := do p ← to_exprform px, q ← to_exprform qx, return (exprform.or p q) | `(%%px ∧ %%qx) := do p ← to_exprform px, q ← to_exprform qx, return (exprform.and p q) | `(_ β†’ %%px) := to_exprform px | x := trace "Cannot reify expr : " >> trace x >> failed /-- List of all unreified exprs -/ meta def exprterm.exprs : exprterm β†’ list expr | (exprterm.cst _) := [] | (exprterm.exp _ x) := [x] | (exprterm.add t s) := list.union t.exprs s.exprs /-- List of all unreified exprs -/ meta def exprform.exprs : exprform β†’ list expr | (exprform.eq t s) := list.union t.exprs s.exprs | (exprform.le t s) := list.union t.exprs s.exprs | (exprform.not p) := p.exprs | (exprform.or p q) := list.union p.exprs q.exprs | (exprform.and p q) := list.union p.exprs q.exprs /-- Reification to an intermediate shadow syntax which eliminates exprs, but still includes non-canonical terms -/ meta def exprterm.to_preterm (xs : list expr) : exprterm β†’ tactic preterm | (exprterm.cst k) := return & k | (exprterm.exp k x) := let m := xs.index_of x in if m < xs.length then return (k ** m) else failed | (exprterm.add xa xb) := do a ← xa.to_preterm, b ← xb.to_preterm, return (a +* b) /-- Reification to an intermediate shadow syntax which eliminates exprs, but still includes non-canonical terms -/ meta def exprform.to_preform (xs : list expr) : exprform β†’ tactic preform | (exprform.eq xa xb) := do a ← xa.to_preterm xs, b ← xb.to_preterm xs, return (a =* b) | (exprform.le xa xb) := do a ← xa.to_preterm xs, b ← xb.to_preterm xs, return (a ≀* b) | (exprform.not xp) := do p ← xp.to_preform, return Β¬* p | (exprform.or xp xq) := do p ← xp.to_preform, q ← xq.to_preform, return (p ∨* q) | (exprform.and xp xq) := do p ← xp.to_preform, q ← xq.to_preform, return (p ∧* q) /-- Reification to an intermediate shadow syntax which eliminates exprs, but still includes non-canonical terms. -/ meta def to_preform (x : expr) : tactic (preform Γ— nat) := do xf ← to_exprform x, let xs := xf.exprs, f ← xf.to_preform xs, return (f, xs.length) /-- Return expr of proof of current LIA goal -/ meta def prove : tactic expr := do (p,m) ← target >>= to_preform, prove_univ_close m p /-- Succeed iff argument is the expr of β„€ -/ meta def eq_int (x : expr) : tactic unit := if x = `(int) then skip else failed /-- Check whether argument is expr of a well-formed formula of LIA-/ meta def wff : expr β†’ tactic unit | `(Β¬ %%px) := wff px | `(%%px ∨ %%qx) := wff px >> wff qx | `(%%px ∧ %%qx) := wff px >> wff qx | `(%%px ↔ %%qx) := wff px >> wff qx | `(%%(expr.pi _ _ px qx)) := monad.cond (if expr.has_var px then return tt else is_prop px) (wff px >> wff qx) (eq_int px >> wff qx) | `(@has_lt.lt %%dx %%h _ _) := eq_int dx | `(@has_le.le %%dx %%h _ _) := eq_int dx | `(@eq %%dx _ _) := eq_int dx | `(@ge %%dx %%h _ _) := eq_int dx | `(@gt %%dx %%h _ _) := eq_int dx | `(@ne %%dx _ _) := eq_int dx | `(true) := skip | `(false) := skip | _ := failed /-- Succeed iff argument is expr of term whose type is wff -/ meta def wfx (x : expr) : tactic unit := infer_type x >>= wff /-- Intro all universal quantifiers over β„€ -/ meta def intro_ints_core : tactic unit := do x ← target, match x with | (expr.pi _ _ `(int) _) := intro_fresh >> intro_ints_core | _ := skip end meta def intro_ints : tactic unit := do (expr.pi _ _ `(int) _) ← target, intro_ints_core /-- If the goal has universal quantifiers over integers, introduce all of them. Otherwise, revert all hypotheses that are formulas of linear integer arithmetic. -/ meta def preprocess : tactic unit := intro_ints <|> (revert_cond_all wfx >> desugar) end int end omega open omega.int /-- The core omega tactic for integers. -/ meta def omega_int (is_manual : bool) : tactic unit := desugar ; (if is_manual then skip else preprocess) ; prove >>= apply >> skip
657065186feebc9dffc2bc19821d2eec53dd19bb
9cba98daa30c0804090f963f9024147a50292fa0
/geom/spacetime.lean
f77dc0de1a7792099b64c4140175d9c6ed8f904b
[]
no_license
kevinsullivan/phys
dcb192f7b3033797541b980f0b4a7e75d84cea1a
ebc2df3779d3605ff7a9b47eeda25c2a551e011f
refs/heads/master
1,637,490,575,500
1,629,899,064,000
1,629,899,064,000
168,012,884
0
3
null
1,629,644,436,000
1,548,699,832,000
Lean
UTF-8
Lean
false
false
19,483
lean
import .geom3d import ..time.time open_locale affine section foo universes u #check add_maps abbreviation geom3d_stamped_frame := (mk_prod_spc (geom3d_std_space) time_std_space).frame_type abbreviation geom3d_stamped_space (f : geom3d_stamped_frame) := spc scalar f def geom3d_stamped_std_frame := (mk_prod_spc (geom3d_std_space) time_std_space).frame def geom3d_stamped_std_space : geom3d_stamped_space geom3d_stamped_std_frame := (mk_prod_spc (geom3d_std_space) time_std_space) structure position3d_stamped {f : geom3d_stamped_frame} (s : geom3d_stamped_space f ) extends point s @[ext] lemma position3d_stamped.ext : βˆ€ {f : geom3d_stamped_frame} {s : geom3d_stamped_space f } (x y : position3d_stamped s), x.to_point = y.to_point β†’ x = y := begin intros f s x y e, cases x, cases y, simp *, have h₁ : ({to_point := x} : position3d_stamped s).to_point = x := rfl, simp [h₁] at e, exact e end def position3d_stamped.coords {f : geom3d_stamped_frame} {s : geom3d_stamped_space f } (t :position3d_stamped s) := t.to_point.coords def position3d_stamped.x {f : geom3d_stamped_frame} {s : geom3d_stamped_space f } (t :position3d_stamped s) : scalar := (t.to_point.coords 0).coord def position3d_stamped.y {f : geom3d_stamped_frame} {s : geom3d_stamped_space f } (t :position3d_stamped s) : scalar := (t.to_point.coords 1).coord def position3d_stamped.z {f : geom3d_stamped_frame} {s : geom3d_stamped_space f } (t :position3d_stamped s) : scalar := (t.to_point.coords 2).coord @[simp] def mk_position3d_stamped' {f : geom3d_stamped_frame} (s : geom3d_stamped_space f ) (p : point s) : position3d_stamped s := position3d_stamped.mk p @[simp] def mk_position3d_stamped {f : geom3d_stamped_frame} (s : geom3d_stamped_space f ) (k₁ kβ‚‚ k₃ kβ‚„ : scalar) : position3d_stamped s := position3d_stamped.mk (mk_point s ⟨[k₁,kβ‚‚,k₃, kβ‚„],rfl⟩) @[simp] def mk_position3d_stamped'' {f1 f2 f3 : geom1d_frame } { s1 : geom1d_space f1} {s2 : geom1d_space f2} { s3 : geom1d_space f3} {f4 : time_frame} {s4 : time_space f4} (p1 : position1d s1) (p2 : position1d s2) (p3 : position1d s3 ) (p4 : time s4) : position3d_stamped (mk_prod_spc (mk_prod_spc (mk_prod_spc s1 s2) s3) s4) := ⟨mk_point_prod (mk_point_prod (mk_point_prod p1.to_point p2.to_point) p3.to_point) p4.to_point⟩ structure displacement3d_stamped {f : geom3d_stamped_frame} (s : geom3d_stamped_space f ) extends vectr s @[ext] lemma displacement3d_stamped.ext : βˆ€ {f : geom3d_stamped_frame} {s : geom3d_stamped_space f } (x y : displacement3d_stamped s), x.to_vectr = y.to_vectr β†’ x = y := begin intros f s x y e, cases x, cases y, simp *, have h₁ : ({to_vectr := x} : displacement3d_stamped s).to_vectr = x := rfl, simp [h₁] at e, exact e end def displacement3d_stamped.coords {f : geom3d_stamped_frame} {s : geom3d_stamped_space f } (d :displacement3d_stamped s) := d.to_vectr.coords def displacement3d_stamped.x {f : geom3d_stamped_frame} {s : geom3d_stamped_space f } (t :displacement3d_stamped s) : scalar := (t.to_vectr.coords 0).coord def displacement3d_stamped.y {f : geom3d_stamped_frame} {s : geom3d_stamped_space f } (t :displacement3d_stamped s) : scalar := (t.to_vectr.coords 1).coord def displacement3d_stamped.z {f : geom3d_stamped_frame} {s : geom3d_stamped_space f } (t :displacement3d_stamped s) : scalar := (t.to_vectr.coords 2).coord @[simp] def mk_displacement3d_stamped' {f : geom3d_stamped_frame} (s : geom3d_stamped_space f ) (v : vectr s) : displacement3d_stamped s := displacement3d_stamped.mk v @[simp] def mk_displacement3d_stamped {f : geom3d_stamped_frame} (s : geom3d_stamped_space f ) (k₁ kβ‚‚ k₃ kβ‚„ : scalar) : displacement3d_stamped s := displacement3d_stamped.mk (mk_vectr s ⟨[k₁,kβ‚‚,k₃,kβ‚„],rfl⟩) @[simp] def mk_displacement3d_stamped'' {f1 f2 f3 : geom1d_frame } { s1 : geom1d_space f1} {s2 : geom1d_space f2} { s3 : geom1d_space f3} {f4 : time_frame} {s4 : time_space f4} (p1 : displacement1d s1) (p2 : displacement1d s2) (p3 : displacement1d s3 ) (p4 : time s4) : displacement3d_stamped (mk_prod_spc (mk_prod_spc (mk_prod_spc s1 s2) s3) s4) := ⟨mk_vectr_prod (mk_vectr_prod (mk_vectr_prod p1.to_vectr p2.to_vectr) p3.to_vectr) (mk_vectr s4 ⟨[(p4.coords 0).coord],rfl⟩)⟩ @[simp] def mk_geom3d_stamped_frame {parent : geom3d_stamped_frame} {s : spc scalar parent} (p : position3d_stamped s) (v0 : displacement3d s) (v1 : displacement3d s) (v2 : displacement3d s) : geom3d_stamped_frame := (mk_frame p.to_point ⟨(Ξ»i, if i = 0 then (mk_displacement3d_stamped v0.x v0.y v0.z 0) else if i = 1 then v1.to_vectr else v2.to_vectr),sorry,sorry⟩) end foo section bar #check quot #check quotient /- ************************************* Instantiate module scalar (vector scalar) ************************************* -/ namespace geom3d variables {f : geom3d_stamped_frame} {s : geom3d_stamped_space f } @[simp] def add_displacement3d_stamped_displacement3d_stamped (v3 v2 : displacement3d_stamped s) : displacement3d_stamped s := mk_displacement3d_stamped' s (v3.to_vectr + v2.to_vectr) @[simp] def smul_displacement3d_stamped (k : scalar) (v : displacement3d_stamped s) : displacement3d_stamped s := mk_displacement3d_stamped' s (k β€’ v.to_vectr) @[simp] def neg_displacement3d_stamped (v : displacement3d_stamped s) : displacement3d_stamped s := mk_displacement3d_stamped' s ((-1 : scalar) β€’ v.to_vectr) @[simp] def sub_displacement3d_stamped_displacement3d_stamped (v3 v2 : displacement3d_stamped s) : displacement3d_stamped s := -- v3-v2 add_displacement3d_stamped_displacement3d_stamped v3 (neg_displacement3d_stamped v2) instance has_add_displacement3d_stamped : has_add (displacement3d_stamped s) := ⟨ add_displacement3d_stamped_displacement3d_stamped ⟩ lemma add_assoc_displacement3d_stamped : βˆ€ a b c : displacement3d_stamped s, a + b + c = a + (b + c) := begin intros, ext, --cases a, repeat { have p3 : (a + b + c).to_vec = a.to_vec + b.to_vec + c.to_vec := rfl, have p2 : (a + (b + c)).to_vec = a.to_vec + (b.to_vec + c.to_vec) := rfl, rw [p3,p2], cc }, admit end instance add_semigroup_displacement3d_stamped : add_semigroup (displacement3d_stamped s) := ⟨ add_displacement3d_stamped_displacement3d_stamped, add_assoc_displacement3d_stamped⟩ @[simp] def displacement3d_stamped_zero := mk_displacement3d_stamped s 0 0 0 0 instance has_zero_displacement3d_stamped : has_zero (displacement3d_stamped s) := ⟨displacement3d_stamped_zero⟩ lemma zero_add_displacement3d_stamped : βˆ€ a : displacement3d_stamped s, 0 + a = a := begin intros,--ext, ext, admit, -- let h0 : (0 + a).to_vec = (0 : vectr s).to_vec + a.to_vec := rfl, --simp [h0], --exact zero_add _, --exact zero_add _, end lemma add_zero_displacement3d_stamped : βˆ€ a : displacement3d_stamped s, a + 0 = a := begin intros,ext, admit, --exact add_zero _, --exact add_zero _, end @[simp] def nsmul_displacement3d_stamped : β„• β†’ (displacement3d_stamped s) β†’ (displacement3d_stamped s) | nat.zero v := displacement3d_stamped_zero --| 3 v := v | (nat.succ n) v := (add_displacement3d_stamped_displacement3d_stamped) v (nsmul_displacement3d_stamped n v) instance add_monoid_displacement3d_stamped : add_monoid (displacement3d_stamped s) := ⟨ -- add_semigroup add_displacement3d_stamped_displacement3d_stamped, add_assoc_displacement3d_stamped, -- has_zero displacement3d_stamped_zero, -- new structure @zero_add_displacement3d_stamped f s, add_zero_displacement3d_stamped, nsmul_displacement3d_stamped ⟩ instance has_neg_displacement3d_stamped : has_neg (displacement3d_stamped s) := ⟨neg_displacement3d_stamped⟩ instance has_sub_displacement3d_stamped : has_sub (displacement3d_stamped s) := ⟨ sub_displacement3d_stamped_displacement3d_stamped⟩ lemma sub_eq_add_neg_displacement3d_stamped : βˆ€ a b : displacement3d_stamped s, a - b = a + -b := begin intros,ext, refl, end instance sub_neg_monoid_displacement3d_stamped : sub_neg_monoid (displacement3d_stamped s) := { neg := neg_displacement3d_stamped , ..(show add_monoid (displacement3d_stamped s), by apply_instance) } lemma add_left_neg_displacement3d_stamped : βˆ€ a : displacement3d_stamped s, -a + a = 0 := begin intros, ext, /- repeat { have h0 : (-a + a).to_vec = -a.to_vec + a.to_vec := rfl, simp [h0], have : (0:vec scalar) = (0:displacement3d_stamped s).to_vectr.to_vec := rfl, simp *, }-/ admit, end instance : add_group (displacement3d_stamped s) := { add_left_neg := begin exact add_left_neg_displacement3d_stamped, end, ..(show sub_neg_monoid (displacement3d_stamped s), by apply_instance), } lemma add_comm_displacement3d_stamped : βˆ€ a b : displacement3d_stamped s, a + b = b + a := begin intros, ext, /-repeat { have p3 : (a + b).to_vec = a.to_vec + b.to_vec:= rfl, have p2 : (b + a).to_vec = b.to_vec + a.to_vec := rfl, rw [p3,p2], cc } -/ admit, end instance add_comm_semigroup_displacement3d_stamped : add_comm_semigroup (displacement3d_stamped s) := ⟨ -- add_semigroup add_displacement3d_stamped_displacement3d_stamped, add_assoc_displacement3d_stamped, add_comm_displacement3d_stamped, ⟩ instance add_comm_monoid_displacement3d_stamped : add_comm_monoid (displacement3d_stamped s) := { add_comm := begin exact add_comm_displacement3d_stamped end, ..(show add_monoid (displacement3d_stamped s), by apply_instance) } instance has_scalar_displacement3d_stamped : has_scalar scalar (displacement3d_stamped s) := ⟨ smul_displacement3d_stamped, ⟩ lemma one_smul_displacement3d_stamped : βˆ€ b : displacement3d_stamped s, (1 : scalar) β€’ b = b := begin intros,ext, /-repeat { have h0 : ((3:scalar) β€’ b).to_vec = ((3:scalar)β€’(b.to_vec)) := rfl, rw [h0], simp *, }-/ admit, end lemma mul_smul_displacement3d_stamped : βˆ€ (x y : scalar) (b : displacement3d_stamped s), (x * y) β€’ b = x β€’ y β€’ b := begin intros, cases b, ext, exact mul_assoc x y _, end instance mul_action_displacement3d_stamped : mul_action scalar (displacement3d_stamped s) := ⟨ one_smul_displacement3d_stamped, mul_smul_displacement3d_stamped, ⟩ lemma smul_add_displacement3d_stamped : βˆ€(r : scalar) (x y : displacement3d_stamped s), r β€’ (x + y) = r β€’ x + r β€’ y := begin intros, ext, repeat { have h0 : (r β€’ (x + y)).to_vec = (r β€’ (x.to_vec + y.to_vec)) := rfl, have h3 : (rβ€’x + rβ€’y).to_vec = (rβ€’x.to_vec + rβ€’y.to_vec) := rfl, rw [h0,h3], simp *, } ,admit, end lemma smul_zero_displacement3d_stamped : βˆ€(r : scalar), r β€’ (0 : displacement3d_stamped s) = 0 := begin admit--intros, ext, exact mul_zero _, exact mul_zero _ end instance distrib_mul_action_K_displacement3d_stamped : distrib_mul_action scalar (displacement3d_stamped s) := ⟨ smul_add_displacement3d_stamped, smul_zero_displacement3d_stamped, ⟩ -- renaming vs template due to clash with name "s" for prevailing variable lemma add_smul_displacement3d_stamped : βˆ€ (a b : scalar) (x : displacement3d_stamped s), (a + b) β€’ x = a β€’ x + b β€’ x := begin intros, ext, exact right_distrib _ _ _, end lemma zero_smul_displacement3d_stamped : βˆ€ (x : displacement3d_stamped s), (0 : scalar) β€’ x = 0 := begin intros, ext, admit,--exact zero_mul _, exact zero_mul _ end instance module_K_displacement3d_stamped : module scalar (displacement3d_stamped s) := ⟨ add_smul_displacement3d_stamped, zero_smul_displacement3d_stamped ⟩ instance add_comm_group_displacement3d_stamped : add_comm_group (displacement3d_stamped s) := { add_comm := begin exact add_comm_displacement3d_stamped end, ..(show add_group (displacement3d_stamped s), by apply_instance) } instance : module scalar (displacement3d_stamped s) := @geom3d.module_K_displacement3d_stamped f s /- ******************** *** Affine space *** ******************** -/ /- Affine operations -/ instance : has_add (displacement3d_stamped s) := ⟨add_displacement3d_stamped_displacement3d_stamped⟩ instance : has_zero (displacement3d_stamped s) := ⟨displacement3d_stamped_zero⟩ instance : has_neg (displacement3d_stamped s) := ⟨neg_displacement3d_stamped⟩ /- Lemmas needed to implement affine space API -/ @[simp] def sub_position3d_stamped_position3d_stamped {f : geom3d_stamped_frame} {s : geom3d_stamped_space f } (p3 p2 : position3d_stamped s) : displacement3d_stamped s := mk_displacement3d_stamped' s (p3.to_point -α΅₯ p2.to_point) @[simp] def add_position3d_stamped_displacement3d_stamped {f : geom3d_stamped_frame} {s : geom3d_stamped_space f } (p : position3d_stamped s) (v : displacement3d_stamped s) : position3d_stamped s := mk_position3d_stamped' s (v.to_vectr +α΅₯ p.to_point) -- reorder assumes order is irrelevant @[simp] def add_displacement3d_stamped_position3d_stamped {f : geom3d_stamped_frame} {s : geom3d_stamped_space f } (v : displacement3d_stamped s) (p : position3d_stamped s) : position3d_stamped s := mk_position3d_stamped' s (v.to_vectr +α΅₯ p.to_point) --@[simp] --def aff_displacement3d_stamped_group_action : displacement3d_stamped s β†’ position3d_stamped s β†’ position3d_stamped s := add_displacement3d_stamped_position3d_stamped scalar instance : has_vadd (displacement3d_stamped s) (position3d_stamped s) := ⟨add_displacement3d_stamped_position3d_stamped⟩ lemma zero_displacement3d_stamped_vadd'_a3 : βˆ€ p : position3d_stamped s, (0 : displacement3d_stamped s) +α΅₯ p = p := begin intros, ext,--exact zero_add _, admit--exact add_zero _ end lemma displacement3d_stamped_add_assoc'_a3 : βˆ€ (g3 g2 : displacement3d_stamped s) (p : position3d_stamped s), g3 +α΅₯ (g2 +α΅₯ p) = (g3 + g2) +α΅₯ p := begin intros, ext, repeat { have h0 : (g3 +α΅₯ (g2 +α΅₯ p)).to_pt = (g3.to_vec +α΅₯ (g2.to_vec +α΅₯ p.to_pt)) := rfl, have h3 : (g3 + g2 +α΅₯ p).to_pt = (g3.to_vec +α΅₯ g2.to_vec +α΅₯ p.to_pt) := rfl, rw [h0,h3], simp *, simp [has_vadd.vadd, has_add.add, add_semigroup.add, add_zero_class.add, add_monoid.add, sub_neg_monoid.add, add_group.add, distrib.add, ring.add, division_ring.add], cc, }, admit, end instance displacement3d_stamped_add_action: add_action (displacement3d_stamped s) (position3d_stamped s) := ⟨ zero_displacement3d_stamped_vadd'_a3, begin let h0 := displacement3d_stamped_add_assoc'_a3, intros, exact (h0 g₁ gβ‚‚ p).symm end⟩ --@[simp] --def aff_geom3d_group_sub : position3d_stamped s β†’ position3d_stamped s β†’ displacement3d_stamped s := sub_geom3d_position3d_stamped scalar instance position3d_stamped_has_vsub : has_vsub (displacement3d_stamped s) (position3d_stamped s) := ⟨ sub_position3d_stamped_position3d_stamped⟩ instance : nonempty (position3d_stamped s) := ⟨mk_position3d_stamped s 0 0 0 0⟩ lemma position3d_stamped_vsub_vadd_a3 : βˆ€ (p3 p2 : (position3d_stamped s)), (p3 -α΅₯ p2) +α΅₯ p2 = p3 := begin /-intros, ext, --repeat { have h0 : (p3 -α΅₯ p2 +α΅₯ p2).to_pt = (p3.to_pt -α΅₯ p2.to_pt +α΅₯ p2.to_pt) := rfl, rw h0, simp [has_vsub.vsub, has_sub.sub, sub_neg_monoid.sub, add_group.sub, add_comm_group.sub, ring.sub, division_ring.sub], simp [has_vadd.vadd, has_add.add, distrib.add, ring.add, division_ring.add], let h0 : field.add p2.to_pt.to_prod.fst (field.sub p3.to_pt.to_prod.fst p2.to_pt.to_prod.fst) = field.add (field.sub p3.to_pt.to_prod.fst p2.to_pt.to_prod.fst) p2.to_pt.to_prod.fst := add_comm _ _, rw h0, exact sub_add_cancel _ _, have h0 : (p3 -α΅₯ p2 +α΅₯ p2).to_pt = (p3.to_pt -α΅₯ p2.to_pt +α΅₯ p2.to_pt) := rfl, rw h0, simp [has_vsub.vsub, has_sub.sub, sub_neg_monoid.sub, add_group.sub, add_comm_group.sub, ring.sub, division_ring.sub], simp [has_vadd.vadd, has_add.add, distrib.add, ring.add, division_ring.add], let h0 : field.add p2.to_pt.to_prod.snd (field.sub p3.to_pt.to_prod.snd p2.to_pt.to_prod.snd) = field.add (field.sub p3.to_pt.to_prod.snd p2.to_pt.to_prod.snd) p2.to_pt.to_prod.snd := add_comm _ _, rw h0, exact sub_add_cancel _ _,-/ admit end lemma position3d_stamped_vadd_vsub_a3 : βˆ€ (g : displacement3d_stamped s) (p : position3d_stamped s), g +α΅₯ p -α΅₯ p = g := begin intros, ext, repeat { have h0 : ((g +α΅₯ p -α΅₯ p) : displacement3d_stamped s).to_vectr = (g.to_vectr +α΅₯ p.to_point -α΅₯ p.to_point) := rfl, rw h0, simp *, } end instance aff_geom3d_stamped_torsor : add_torsor (displacement3d_stamped s) (position3d_stamped s) := ⟨ begin exact position3d_stamped_vsub_vadd_a3, end, begin exact position3d_stamped_vadd_vsub_a3, end, ⟩ open_locale affine --instance : affine_space (displacement3d_stamped s) (position3d_stamped s) := @geom3d.aff_geom3d_torsor f s end geom3d -- ha ha end bar /- Newer version Tradeoff - Does not directly extend from affine equiv. Base class is an equiv on points and vectrs Extension methods are provided to directly transform Times and Duration between frames -/ @[ext] structure geom3d_stamped_transform {f3 : geom3d_stamped_frame} {f2 : geom3d_stamped_frame} (sp3 : geom3d_stamped_space f3) (sp2 : geom3d_stamped_space f2) extends fm_tr sp3 sp2 def geom3d_stamped_space.mk_geom3d_stamped_transform_to {f3 : geom3d_stamped_frame} (s3 : geom3d_stamped_space f3) : Ξ  {f2 : geom3d_stamped_frame} (s2 : geom3d_stamped_space f2), geom3d_stamped_transform s3 s2 := --(position3d_stamped s2) ≃ᡃ[scalar] (position3d_stamped s3) := Ξ» f2 s2, ⟨s3.fm_tr s2⟩ def geom3d_stamped_transform.symm {f3 : geom3d_stamped_frame} {f2 : geom3d_stamped_frame} {sp3 : geom3d_stamped_space f3} {sp2 : geom3d_stamped_space f2} (ttr : geom3d_stamped_transform sp3 sp2) : geom3d_stamped_transform sp2 sp3 := ⟨(ttr.1).symm⟩ def geom3d_stamped_transform.trans {f3 : geom3d_stamped_frame} {f2 : geom3d_stamped_frame} {f3 : geom3d_stamped_frame} {sp3 : geom3d_stamped_space f3} {sp2 : geom3d_stamped_space f2} {sp3 : geom3d_stamped_space f3} (ttr : geom3d_stamped_transform sp3 sp2) : geom3d_stamped_transform sp2 sp3 β†’ geom3d_stamped_transform sp3 sp3 := Ξ»ttr_, ⟨(ttr.1).trans ttr_.1⟩ def geom3d_stamped_transform.transform_position3d_stamped {f3 : geom3d_stamped_frame} {s3 : geom3d_stamped_space f3} {f2 : geom3d_stamped_frame} {s2 : geom3d_stamped_space f2} (tr: geom3d_stamped_transform s3 s2 ) : position3d_stamped s3 β†’ position3d_stamped s2 := Ξ»t : position3d_stamped s3, ⟨tr.to_fm_tr.to_equiv t.to_point⟩ def geom3d_stamped_transform.transform_displacement3d_stamped {f3 : geom3d_stamped_frame} {s3 : geom3d_stamped_space f3} {f2 : geom3d_stamped_frame} {s2 : geom3d_stamped_space f2} (tr: geom3d_stamped_transform s3 s2 ) : displacement3d_stamped s3 β†’ displacement3d_stamped s2 := Ξ»d, let as_pt : point s3 := ⟨λi, mk_pt scalar (d.coords i).coord⟩ in let tr_pt := (tr.to_equiv as_pt) in ⟨⟨λi, mk_vec scalar (tr_pt.coords i).coord⟩⟩
d99b2a8a05de7414c9bba338c55fe266da5188bf
d1a52c3f208fa42c41df8278c3d280f075eb020c
/src/Lean/Elab/PreDefinition.lean
f8a296e3e2438e8af02a24156226d4a684a2e415
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach" ]
permissive
cipher1024/lean4
6e1f98bb58e7a92b28f5364eb38a14c8d0aae393
69114d3b50806264ef35b57394391c3e738a9822
refs/heads/master
1,642,227,983,603
1,642,011,696,000
1,642,011,696,000
228,607,691
0
0
Apache-2.0
1,576,584,269,000
1,576,584,268,000
null
UTF-8
Lean
false
false
323
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Elab.PreDefinition.Basic import Lean.Elab.PreDefinition.Structural import Lean.Elab.PreDefinition.Main import Lean.Elab.PreDefinition.MkInhabitant
0c34c8aec9eb69b8c9ae17874762f3ac04110c9f
26ac254ecb57ffcb886ff709cf018390161a9225
/src/data/sym2.lean
cfe2cf630195ca570f66fc0d4ecb6db18eee5040
[ "Apache-2.0" ]
permissive
eric-wieser/mathlib
42842584f584359bbe1fc8b88b3ff937c8acd72d
d0df6b81cd0920ad569158c06a3fd5abb9e63301
refs/heads/master
1,669,546,404,255
1,595,254,668,000
1,595,254,668,000
281,173,504
0
0
Apache-2.0
1,595,263,582,000
1,595,263,581,000
null
UTF-8
Lean
false
false
10,271
lean
/- Copyright (c) 2020 Kyle Miller All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Kyle Miller. -/ import tactic.linarith import data.sym open function open sym /-! # The symmetric square This file defines the symmetric square, which is `Ξ± Γ— Ξ±` modulo swapping. This is also known as the type of unordered pairs. More generally, the symmetric square is the second symmetric power (see `data.sym`). The equivalence is `sym2.equiv_sym`. From the point of view that an unordered pair is equivalent to a multiset of cardinality two (see `sym2.equiv_multiset`), there is a `has_mem` instance `sym2.mem`, which is a `Prop`-valued membership test. Given `a ∈ z` for `z : sym2 Ξ±`, it does not appear to be possible, in general, to *computably* give the other element in the pair. For this, `sym2.vmem a z` is a `Type`-valued membership test that gives a way to obtain the other element with `sym2.vmem.other`. Recall that an undirected graph (allowing self loops, but no multiple edges) is equivalent to a symmetric relation on the vertex type `Ξ±`. Given a symmetric relation on `Ξ±`, the corresponding edge set is constructed by `sym2.from_rel`. ## Notation The symmetric square has a setoid instance, so `⟦(a, b)⟧` denotes a term of the symmetric square. ## Tags symmetric square, unordered pairs, symmetric powers -/ namespace sym2 variables {Ξ± : Type*} /-- This is the relation capturing the notion of pairs equivalent up to permutations. -/ inductive rel (Ξ± : Type*) : (Ξ± Γ— Ξ±) β†’ (Ξ± Γ— Ξ±) β†’ Prop | refl (x y : Ξ±) : rel (x, y) (x, y) | swap (x y : Ξ±) : rel (x, y) (y, x) attribute [refl] rel.refl @[symm] lemma rel.symm {x y : Ξ± Γ— Ξ±} : rel Ξ± x y β†’ rel Ξ± y x := by { intro a, cases a, exact a, apply rel.swap } @[trans] lemma rel.trans {x y z : Ξ± Γ— Ξ±} : rel Ξ± x y β†’ rel Ξ± y z β†’ rel Ξ± x z := by { intros a b, cases_matching* rel _ _ _; apply rel.refl <|> apply rel.swap } lemma rel.is_equivalence : equivalence (rel Ξ±) := begin split, { intros x, cases x, refl }, split, { apply rel.symm }, { intros x y z a b, apply rel.trans a b }, end instance rel.setoid (Ξ± : Type*) : setoid (Ξ± Γ— Ξ±) := ⟨rel Ξ±, rel.is_equivalence⟩ end sym2 /-- `sym2 Ξ±` is the symmetric square of `Ξ±`, which, in other words, is the type of unordered pairs. It is equivalent in a natural way to multisets of cardinality 2 (see `sym2.equiv_multiset`). -/ @[reducible] def sym2 (Ξ± : Type*) := quotient (sym2.rel.setoid Ξ±) namespace sym2 universe u variables {Ξ± : Type u} lemma eq_swap {a b : Ξ±} : ⟦(a, b)⟧ = ⟦(b, a)⟧ := by { rw quotient.eq, apply rel.swap } lemma congr_right (a b c : Ξ±) : ⟦(a, b)⟧ = ⟦(a, c)⟧ ↔ b = c := begin split, intro h, rw quotient.eq at h, cases h; refl, intro h, apply congr_arg _ h, end /-- The functor `sym2` is functorial, and this function constructs the induced maps. -/ def map {Ξ± Ξ² : Type*} (f : Ξ± β†’ Ξ²) : sym2 Ξ± β†’ sym2 Ξ² := quotient.map (prod.map f f) begin rintros ⟨a₁, aβ‚‚βŸ© ⟨b₁, bβ‚‚βŸ© h, cases h, apply rel.refl, apply rel.swap, end @[simp] lemma map_id : sym2.map (@id Ξ±) = id := by tidy lemma map_comp {Ξ± Ξ² Ξ³: Type*} {g : Ξ² β†’ Ξ³} {f : Ξ± β†’ Ξ²} : sym2.map (g ∘ f) = sym2.map g ∘ sym2.map f := by tidy section membership /-! ### Declarations about membership -/ /-- This is a predicate that determines whether a given term is a member of a term of the symmetric square. From this point of view, the symmetric square is the subtype of cardinality-two multisets on `Ξ±`. -/ def mem (x : Ξ±) (z : sym2 Ξ±) : Prop := βˆƒ (y : Ξ±), z = ⟦(x, y)⟧ instance : has_mem Ξ± (sym2 Ξ±) := ⟨mem⟩ lemma mk_has_mem (x y : Ξ±) : x ∈ ⟦(x, y)⟧ := ⟨y, rfl⟩ /-- This is a type-valued version of the membership predicate `mem` that contains the other element `y` of `z` such that `z = ⟦(x, y)⟧`. It is a subsingleton already, so there is no need to apply `trunc` to the type. -/ @[nolint has_inhabited_instance] def vmem (x : Ξ±) (z : sym2 Ξ±) : Type u := {y : Ξ± // z = ⟦(x, y)⟧} instance (x : Ξ±) (z : sym2 Ξ±) : subsingleton {y : Ξ± // z = ⟦(x, y)⟧} := ⟨by { rintros ⟨a, ha⟩ ⟨b, hb⟩, rw ha at hb, rw congr_right at hb, tidy, }⟩ /-- The `vmem` version of `mk_has_mem`. -/ def mk_has_vmem (x y : Ξ±) : vmem x ⟦(x, y)⟧ := ⟨y, rfl⟩ instance {a : Ξ±} {z : sym2 Ξ±} : has_lift (vmem a z) (mem a z) := ⟨λ h, ⟨h.val, h.property⟩⟩ /-- Given an element of a term of the symmetric square (using `vmem`), retrieve the other element. -/ def vmem.other {a : Ξ±} {p : sym2 Ξ±} (h : vmem a p) : Ξ± := h.val /-- The defining property of the other element is that it can be used to reconstruct the term of the symmetric square. -/ lemma vmem_other_spec {a : Ξ±} {z : sym2 Ξ±} (h : vmem a z) : z = ⟦(a, h.other)⟧ := by { dunfold vmem.other, tidy, } /-- This is the `mem`-based version of `other`. -/ noncomputable def mem.other {a : Ξ±} {z : sym2 Ξ±} (h : a ∈ z) : Ξ± := classical.some h lemma mem_other_spec {a : Ξ±} {z : sym2 Ξ±} (h : a ∈ z) : ⟦(a, h.other)⟧ = z := begin dunfold mem.other, exact (classical.some_spec h).symm, end lemma other_is_mem_other {a : Ξ±} {z : sym2 Ξ±} (h : vmem a z) (h' : a ∈ z) : h.other = mem.other h' := by rw [←congr_right a, ←vmem_other_spec h, mem_other_spec] lemma eq_iff {x y z w : Ξ±} : ⟦(x, y)⟧ = ⟦(z, w)⟧ ↔ (x = z ∧ y = w) ∨ (x = w ∧ y = z) := begin split; intro h, { rw quotient.eq at h, cases h; tidy, }, { cases h; rw [h.1, h.2], rw eq_swap, } end lemma mem_iff {a b c : Ξ±} : a ∈ ⟦(b, c)⟧ ↔ a = b ∨ a = c := begin split, { intro h, cases h, rw eq_iff at h_h, tidy }, { intro h, cases h, rw h, apply mk_has_mem, rw h, rw eq_swap, apply mk_has_mem, } end end membership /-- A type `Ξ±` is naturally included in the diagonal of `Ξ± Γ— Ξ±`, and this function gives the image of this diagonal in `sym2 Ξ±`. -/ def diag (x : Ξ±) : sym2 Ξ± := ⟦(x, x)⟧ /-- A predicate for testing whether an element of `sym2 Ξ±` is on the diagonal. -/ def is_diag (z : sym2 Ξ±) : Prop := z ∈ set.range (@diag Ξ±) lemma is_diag_iff_proj_eq (z : Ξ± Γ— Ξ±) : is_diag ⟦z⟧ ↔ z.1 = z.2 := begin cases z with a b, split, { intro h, cases h with x h, dsimp [diag] at h, cases eq_iff.mp h with h h; rw [←h.1, ←h.2], }, { intro h, dsimp at h, rw h, use b, simp [diag], }, end instance is_diag.decidable_pred (Ξ± : Type u) [decidable_eq Ξ±] : decidable_pred (@is_diag Ξ±) := begin intro z, induction z, change decidable (is_diag ⟦z⟧), rw is_diag_iff_proj_eq, apply_instance, apply subsingleton.elim, end section relations /-! ### Declarations about symmetric relations -/ variables {r : Ξ± β†’ Ξ± β†’ Prop} /-- Symmetric relations define a set on `sym2 Ξ±` by taking all those pairs of elements that are related. -/ def from_rel (sym : symmetric r) : set (sym2 Ξ±) := Ξ» z, quotient.rec_on z (Ξ» z, r z.1 z.2) begin intros z w p, cases p, simp, simp, split; apply sym, end @[simp] lemma from_rel_proj_prop {sym : symmetric r} {z : Ξ± Γ— Ξ±} : ⟦z⟧ ∈ from_rel sym ↔ r z.1 z.2 := by tidy @[simp] lemma from_rel_prop {sym : symmetric r} {a b : Ξ±} : ⟦(a, b)⟧ ∈ from_rel sym ↔ r a b := by simp only [from_rel_proj_prop] lemma from_rel_irreflexive {sym : symmetric r} : irreflexive r ↔ βˆ€ {z}, z ∈ from_rel sym β†’ Β¬is_diag z := begin split, { intros h z hr hd, induction z, have hd' := (is_diag_iff_proj_eq _).mp hd, have hr' := from_rel_proj_prop.mp hr, rw hd' at hr', exact h _ hr', refl, }, { intros h x hr, rw ← @from_rel_prop _ _ sym at hr, exact h hr ⟨x, rfl⟩, } end end relations section sym_equiv /-! ### Equivalence to the second symmetric power -/ local attribute [instance] vector.perm.is_setoid private def from_vector {Ξ± : Type*} : vector Ξ± 2 β†’ Ξ± Γ— Ξ± | ⟨[a, b], h⟩ := (a, b) private lemma perm_card_two_iff {Ξ± : Type*} {a₁ b₁ aβ‚‚ bβ‚‚ : Ξ±} : [a₁, b₁].perm [aβ‚‚, bβ‚‚] ↔ (a₁ = aβ‚‚ ∧ b₁ = bβ‚‚) ∨ (a₁ = bβ‚‚ ∧ b₁ = aβ‚‚) := begin split, swap, intro h, cases h; rw [h.1, h.2], apply list.perm.swap', refl, intro h, rw ←multiset.coe_eq_coe at h, repeat {rw ←multiset.cons_coe at h}, repeat {rw multiset.cons_eq_cons at h}, cases h, { cases h, cases h_right, left, simp [h_left, h_right.1], simp at h_right, tauto, }, { rcases h.2 with ⟨cs, h'⟩, repeat {rw multiset.cons_eq_cons at h'}, simp only [multiset.zero_ne_cons, multiset.coe_nil_eq_zero, exists_false, or_false, and_false, false_and] at h', right, simp [h'.1.1, h'.2.1], }, end /-- The symmetric square is equivalent to length-2 vectors up to permutations. -/ def sym2_equiv_sym' {Ξ± : Type*} : equiv (sym2 Ξ±) (sym' Ξ± 2) := { to_fun := quotient.map (Ξ» (x : Ξ± Γ— Ξ±), ⟨[x.1, x.2], rfl⟩) (begin intros x y h, cases h, refl, apply list.perm.swap', refl, end), inv_fun := quotient.map from_vector (begin rintros ⟨x, hx⟩ ⟨y, hy⟩ h, cases x with x0 x, simp at hx, tauto, cases x with x1 x, simp at hx, exfalso, linarith [hx], cases x with x2 x, swap, simp at hx, exfalso, linarith [hx], cases y with y0 y, simp at hy, tauto, cases y with y1 y, simp at hy, exfalso, linarith [hy], cases y with y2 y, swap, simp at hy, exfalso, linarith [hy], cases perm_card_two_iff.mp h; rw [h_1.1, h_1.2], refl, apply sym2.rel.swap, end), left_inv := by tidy, right_inv := begin intro x, induction x, cases x with x hx, cases x with x0 x, simp at hx, tauto, cases x with x1 x, simp at hx, exfalso, linarith [hx], cases x with x2 x, swap, simp at hx, exfalso, linarith [hx], refl, refl, end } /-- The symmetric square is equivalent to the second symmetric power. -/ def equiv_sym (Ξ± : Type*) : sym2 Ξ± ≃ sym Ξ± 2 := equiv.trans sym2_equiv_sym' (sym_equiv_sym' Ξ± 2).symm /-- The symmetric square is equivalent to multisets of cardinality two. (This is currently a synonym for `equiv_sym`, but it's provided in case the definition for `sym` changes.) -/ def equiv_multiset (Ξ± : Type*) : sym2 Ξ± ≃ {s : multiset Ξ± // s.card = 2} := equiv_sym Ξ± end sym_equiv end sym2
193280ee396c44dfbeba9c783fd7ffeb4fe581d2
b7fc5b86b12212bea5542eb2c9d9f0988fd78697
/src/solutions/wednesday/topological_spaces.lean
f55ec7ba2f18ccddfab0c1e2748a60d74c0368c1
[]
no_license
stjordanis/lftcm2020
3b16591aec853c8546d9c8b69c0bf3f5f3956fee
1f3485e4dafdc587b451ec5144a1d8d3ec9b411e
refs/heads/master
1,675,958,865,413
1,609,901,722,000
1,609,901,722,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
19,529
lean
import tactic import data.set.finite import data.real.basic -- for metrics /- # (Re)-Building topological spaces in Lean Mathlib has a large library of results on topological spaces, including various constructions, separation axioms, Tychonoff's theorem, sheaves, Stone-Čech compactification, Heine-Cantor, to name but a few. See https://leanprover-community.github.io/theories/topology.html which for a (subset) of what's in library. But today we will ignore all that, and build our own version of topological spaces from scratch! (On Friday morning Patrick Massot will lead a session exploring the existing mathlib library in more detail) To get this file run either `leanproject get lftcm2020`, if you didn't already or cd to that folder and run `git pull; leanproject get-mathlib-cache`, this is `src/exercise_sources/wednesday/topological_spaces.lean`. The exercises are spread throughout, you needn't do them in order! They are marked as short, medium and long, so I suggest you try some short ones first. First a little setup, we will be making definitions involving the real numbers, the theory of which is not computable, and we'll use sets. -/ noncomputable theory open set /-! ## What is a topological space: There are many definitions: one from Wikipedia: A topological space is an ordered pair (X, Ο„), where X is a set and Ο„ is a collection of subsets of X, satisfying the following axioms: - The empty set and X itself belong to Ο„. - Any arbitrary (finite or infinite) union of members of Ο„ still belongs to Ο„. - The intersection of any finite number of members of Ο„ still belongs to Ο„. We can formalize this as follows: -/ class topological_space_wiki := (X : Type) -- the underlying Type that the topology will be on (Ο„ : set (set X)) -- the set of open subsets of X (empty_mem : βˆ… ∈ Ο„) -- empty set is open (univ_mem : univ ∈ Ο„) -- whole space is open (union : βˆ€ B βŠ† Ο„, ⋃₀ B ∈ Ο„) -- arbitrary unions (sUnions) of members of Ο„ are open (inter : βˆ€ (B βŠ† Ο„) (h : finite B), β‹‚β‚€ B ∈ Ο„) -- finite intersections of -- members of Ο„ are open /- Before we go on we should be sure we want to use this as our definition. -/ -- omit /- (Changing your definitions later can be less of a hassle in formalized mathematics than pen and paper maths as the proof assistant will tell you exactly which steps in which proofs you broke, it can also be more of a hassle as you actually have to fix things, so its still best to get it right the first time!) -/ -- omit @[ext] class topological_space (X : Type) := (is_open : set X β†’ Prop) -- why set X β†’ Prop not set (set X)? former plays -- nicer with typeclasses later (empty_mem : is_open βˆ…) (univ_mem : is_open univ) (union : βˆ€ (B : set (set X)) (h : βˆ€ b ∈ B, is_open b), is_open (⋃₀ B)) (inter : βˆ€ (A B : set X) (hA : is_open A) (hB : is_open B), is_open (A ∩ B)) namespace topological_space /- We can now work with topological spaces like this. -/ example (X : Type) [topological_space X] (U V W : set X) (hU : is_open U) (hV : is_open V) (hW : is_open W) : is_open (U ∩ V ∩ W) := begin apply inter _ _ _ hW, exact inter _ _ hU hV, end /- ## Exercise 0 [short]: One of the axioms of a topological space we have here is unnecessary, it follows from the others. If we remove it we'll have less work to do each time we want to create a new topological space so: 1. Identify and remove the unneeded axiom, make sure to remove it throughout the file. 2. Add the axiom back as a lemma with the same name and prove it based on the others, so that the _interface_ is the same. -/ -- omit lemma empty_mem' (X : Type) [topological_space X] : is_open (βˆ… : set X) := begin convert union (βˆ… : set (set X)) _; simp, end -- omit /- Defining a basic topology now works like so: -/ def discrete (X : Type) : topological_space X := { is_open := Ξ» U, true, -- everything is open empty_mem := trivial, univ_mem := trivial, union := begin intros B h, trivial, end, inter := begin intros A hA B hB, trivial, end } /- ## Exercise 1 [medium]: One way me might want to create topological spaces in practice is to take the coarsest possible topological space containing a given set of is_open. To define this we might say we want to define what `is_open` is given the set of generators. So we want to define the predicate `is_open` by declaring that each generator will be open, the intersection of two opens will be open, and each union of a set of opens will be open, and finally the empty and whole space (`univ`) must be open. The cleanest way to do this is as an inductive definition. The exercise is to make this definition of the topological space generated by a given set in Lean. ### Hint: As a hint for this exercise take a look at the following definition of a constructible set of a topological space, defined by saying that an intersection of an open and a closed set is constructible and that the union of any pair of constructible sets is constructible. (Bonus exercise: mathlib doesn't have any theory of constructible sets, make one and PR it! [arbitrarily long!], or just prove that open and closed sets are constructible for now) -/ inductive is_constructible {X : Type} (T : topological_space X) : set X β†’ Prop /- Given two open sets in `T`, the intersection of one with the complement of the other open is locally closed, hence constructible: -/ | locally_closed : βˆ€ (A B : set X), is_open A β†’ is_open B β†’ is_constructible (A ∩ Bᢜ) -- Given two constructible sets their union is constructible: | union : βˆ€ A B, is_constructible A β†’ is_constructible B β†’ is_constructible (A βˆͺ B) -- For example we can now use this definition to prove the empty set is constructible example {X : Type} (T : topological_space X) : is_constructible T βˆ… := begin -- The intersection of the whole space (open) with the empty set (closed) is -- locally closed, hence constructible have := is_constructible.locally_closed univ univ T.univ_mem T.univ_mem, -- but simp knows that's just the empty set (`simp` uses `this` automatically) simpa, end /-- The open sets of the least topology containing a collection of basic sets. -/ inductive generated_open (X : Type) (g : set (set X)) : set X β†’ Prop -- The exercise: Add a definition here defining which sets are generated by `g` like the -- `is_constructible` definition above. -- omit | basic : βˆ€ s ∈ g, generated_open s | univ : generated_open univ | inter : βˆ€s t, generated_open s β†’ generated_open t β†’ generated_open (s ∩ t) | sUnion : βˆ€k, (βˆ€ s ∈ k, generated_open s) β†’ generated_open (⋃₀ k) lemma generated_open.empty (X : Type) (g : set (set X)) : generated_open X g βˆ… := begin have := generated_open.sUnion (βˆ… : set (set X)), simpa, end -- omit /-- The smallest topological space containing the collection `g` of basic sets -/ def generate_from (X : Type) (g : set (set X)) : topological_space X := { is_open := generated_open X g, empty_mem := /- inline sorry -/generated_open.empty X g/- inline sorry -/, univ_mem := /- inline sorry -/generated_open.univ/- inline sorry -/, inter := /- inline sorry -/generated_open.inter/- inline sorry -/, union := /- inline sorry -/generated_open.sUnion/- inline sorry -/ } /- ## Exercise 2 [short]: Define the indiscrete topology on any type using this. (To do it without this it is surprisingly fiddly to prove that the set `{βˆ…, univ}` actually forms a topology) -/ def indiscrete (X : Type) : topological_space X := /- inline sorry -/ generate_from X βˆ… /- inline sorry -/ end topological_space open topological_space /- Now it is quite easy to give a topology on the product of a pair of topological spaces. -/ instance prod.topological_space (X Y : Type) [topological_space X] [topological_space Y] : topological_space (X Γ— Y) := topological_space.generate_from (X Γ— Y) {U | βˆƒ (Ux : set X) (Uy : set Y) (hx : is_open Ux) (hy : is_open Uy), U = set.prod Ux Uy} -- the proof of this is bit long so I've left it out for the purpose of this file! lemma is_open_prod_iff (X Y : Type) [topological_space X] [topological_space Y] {s : set (X Γ— Y)} : is_open s ↔ (βˆ€a b, (a, b) ∈ s β†’ βˆƒu v, is_open u ∧ is_open v ∧ a ∈ u ∧ b ∈ v ∧ set.prod u v βŠ† s) := sorry /- # Metric spaces -/ open_locale big_operators class metric_space_basic (X : Type) := (dist : X β†’ X β†’ ℝ) (dist_eq_zero_iff : βˆ€ x y, dist x y = 0 ↔ x = y) (dist_symm : βˆ€ x y, dist x y = dist y x) (triangle : βˆ€ x y z, dist x z ≀ dist x y + dist y z) namespace metric_space_basic open topological_space /- ## Exercise 3 [short]: We have defined a metric space with a metric landing in ℝ, and made no mention of nonnegativity, (this is in line with the philosophy of using the easiest axioms for our definitions as possible, to make it easier to define individual metrics). Show that we really did define the usual notion of metric space. -/ lemma dist_nonneg {X : Type} [metric_space_basic X] (x y : X) : 0 ≀ dist x y := -- sorry begin have := calc 0 = dist x x : by rw (dist_eq_zero_iff x x).mpr rfl ... ≀ dist x y + dist y x : triangle x y x ... = dist x y + dist x y : by rw dist_symm ... = 2 * dist x y : by rw two_mul (dist x y), linarith, end -- sorry /- From a metric space we get an induced topological space structure like so: -/ instance {X : Type} [metric_space_basic X] : topological_space X := generate_from X { B | βˆƒ (x : X) r, B = {y | dist x y < r} } end metric_space_basic open metric_space_basic /- So far so good, now lets define the product of two metric spaces: ## Exercise 4 [medium]: Fill in the proofs here. Hint: the computer can do boring casework you would never dream of in real life. `max` is defined as `if x < y then y else x` and the `split_ifs` tactic will break apart if statements. -/ instance prod.metric_space_basic (X Y : Type) [metric_space_basic X] [metric_space_basic Y] : metric_space_basic (X Γ— Y) := { dist := Ξ» u v, max (dist u.fst v.fst) (dist u.snd v.snd), dist_eq_zero_iff := -- sorry begin intros u v, split; intro h, { have hf := dist_nonneg u.fst v.fst, have hs := dist_nonneg u.snd v.snd, have := max_le_iff.mp (le_of_eq h), ext; rw ← dist_eq_zero_iff; linarith, }, { rw [h, (dist_eq_zero_iff _ _).mpr, (dist_eq_zero_iff _ _).mpr, max_self]; refl, }, end -- sorry , dist_symm := /- inline sorry -/ begin intros u v, simp [metric_space_basic.dist_symm], end /- inline sorry -/, triangle := -- sorry begin intros x y z, have hf := triangle x.fst y.fst z.fst, have hs := triangle x.snd y.snd z.snd, simp only [max], split_ifs; linarith, end -- sorry } /- ☑ Let's try to prove a simple lemma involving the product topology: ☑ -/ -- omit set_option pp.all false set_option trace.class_instances false set_option trace.type_context.is_def_eq_detail false -- omit set_option trace.type_context.is_def_eq false example (X : Type) [metric_space_basic X] : is_open {xy : X Γ— X | dist xy.fst xy.snd < 100 } := begin -- rw is_open_prod_iff X X, -- this fails, why? Because we have two subtly different topologies on the product -- they are equal but the proof that they are equal is nontrivial and the -- typeclass mechanism can't see that they automatically to apply. We need to change -- our set-up. sorry, end /- Note that lemma works fine when there is only one topology involved. -/ lemma diag_closed (X : Type) [topological_space X] : is_open {xy : X Γ— X | xy.fst β‰  xy.snd } := begin rw is_open_prod_iff X X, sorry, -- Don't try and fill this in: see below! end /- ## Exercise 5 [short]: The previous lemma isn't true! It requires a separation axiom. Define a `class` that posits that the topology on a type `X` satisfies this axiom. Mathlib uses `T_i` naming scheme for these axioms. -/ class t2_space (X : Type) [topological_space X] := (t2 : /- inline sorry -/ βˆ€ (x y : X) (h : x β‰  y), βˆƒ (Ux Uy : set X) (hUx : is_open Ux) (hUy : is_open Uy) (hx : x ∈ Ux) (hy : y ∈ Uy), Ux ∩ Uy = βˆ… /- inline sorry -/) /- (Bonus exercises [medium], the world is your oyster: prove the correct version of the above lemma `diag_closed`, prove that the discrete topology is t2, or that any metric topology is t2, ). -/ -- omit example (X : Type) [topological_space X] [t2_space X] : is_open {xy : X Γ— X | xy.fst β‰  xy.snd } := begin rw is_open_prod_iff X X, intros x y h, obtain ⟨Ux, Uy, hUx, hUy, hx, hy, hxy⟩ := t2_space.t2 x y h, use [Ux, Uy, hUx, hUy, hx, hy], rintros ⟨tx, ty⟩ ⟨ht1, ht2⟩, dsimp, intro txy, rw txy at *, have : ty ∈ Ux ∩ Uy := ⟨ht1, ht2⟩, rw hxy at this, exact this, -- exact not_mem_empty ty this, end -- omit /- Let's fix the broken example from earlier, by redefining the topology on a metric space. We have unfortunately created two topologies on `X Γ— Y`, one via `prod.topology` that we defined earlier as the product of the two topologies coming from the respective metric space structures. And one coming from the metric on the product. These are equal, i.e. the same topology (otherwise mathematically the product would not be a good definition). However they are not definitionally equal, there is as nontrivial proof to show they are the same. The typeclass system (which finds the relevant topological space instance when we use lemmas involving topological spaces) isn't able to check that topological space structures which are equal for some nontrivial reason are equal on the fly so it gets stuck. We can use `extends` to say that a metric space is an extra structure on top of being a topological space so we are making a choice of topology for each metric space. This may not be *definitionally* equal to the induced topology, but we should add the axiom that the metric and the topology are equal to stop us from creating a metric inducing a different topology to the topological structure we chose. -/ class metric_space (X : Type) extends topological_space X, metric_space_basic X := (compatible : βˆ€ U, is_open U ↔ generated_open X { B | βˆƒ (x : X) r, B = {y | dist x y < r}} U) namespace metric_space open topological_space /- This might seem a bit inconvenient to have to define a topological space each time we want a metric space. We would still like a way of making a `metric_space` just given a metric and some properties it satisfies, i.e. a `metric_space_basic`, so we should setup a metric space constructor from a `metric_space_basic` by setting the topology to be the induced one. -/ def of_basic {X : Type} (m : metric_space_basic X) : metric_space X := { compatible := begin intros, refl, /- this should work when the above parts are complete -/ end, ..m, ..@metric_space_basic.topological_space X m } /- Now lets define the product of two metric spaces properly -/ instance {X Y : Type} [metric_space X] [metric_space Y] : metric_space (X Γ— Y) := { compatible := begin -- Let's not fill this in for the demo, let me know if you do it! -- sorry rintros U, rw is_open_prod_iff, split; intro h, sorry, sorry, -- sorry end, ..prod.topological_space X Y, ..prod.metric_space_basic X Y, } /- unregister the bad instance we defined earlier -/ local attribute [-instance] metric_space_basic.topological_space /- Now this will work, there is only one topological space on the product, we can rewrite like we tried to before a lemma about topologies our result on metric spaces, as there is only one topology here. ## Exercise 6 [long?]: Complete the proof of the example (you can generalise the 100 too if it makes it feel less silly). -/ example (X : Type) [metric_space X] : is_open {xy : X Γ— X | dist xy.fst xy.snd < 100 } := begin rw is_open_prod_iff X X, -- sorry intros x y h, use [{t | dist x t < 50 - dist x y / 2}, {t | dist y t < 50 - dist x y / 2}], split, { rw compatible, apply generated_open.basic, use [x, 50 - dist x y / 2], }, split, { rw compatible, apply generated_open.basic, use [y, 50 - dist x y / 2], }, split, { dsimp, rw (metric_space_basic.dist_eq_zero_iff x x).mpr rfl, change dist x y < 100 at h, linarith, }, split, { dsimp, rw (metric_space_basic.dist_eq_zero_iff y y).mpr rfl, change dist x y < 100 at h, linarith, }, rintros ⟨tx, ty⟩ ⟨htx, hty⟩, dsimp at *, calc dist tx ty ≀ dist tx x + dist x ty : triangle tx x ty ... ≀ dist tx x + dist x y + dist y ty : by linarith [triangle x y ty] ... = dist x tx + dist x y + dist y ty : by rw dist_symm ... < 100 : by linarith, -- sorry end end metric_space namespace topological_space /- As mentioned, there are many definitions of a topological space, for instance one can define them via specifying a set of closed sets satisfying various axioms, this is equivalent and sometimes more convenient. We _could_ create two distinct Types defined by different data and provide an equivalence between theses types, e.g. `topological_space_via_open_sets` and `topological_space_via_closed_sets`, but this would quickly get unwieldy. What's better is to make an alternative _constructor_ for our original topological space. This is a function takes a set of subsets satisfying the axioms to be the closed sets of a topological space and creates the topological space defined by the corresponding set of open sets. ## Exercise 7 [medium]: Complete the following constructor of a topological space from a set of subsets of a given type `X` satisfying the axioms for the closed sets of a topology. Hint: there are many useful lemmas about complements in mathlib, with names involving `compl`, like `compl_empty`, `compl_univ`, `compl_compl`, `compl_sUnion`, `mem_compl_image`, `compl_inter`, `compl_compl'`, `you can #check them to see what they say. -/ def mk_closed_sets (X : Type) (Οƒ : set (set X)) (empty_mem : βˆ… ∈ Οƒ) (univ_mem : univ ∈ Οƒ) (inter : βˆ€ B βŠ† Οƒ, β‹‚β‚€ B ∈ Οƒ) (union : βˆ€ (A ∈ Οƒ) (B ∈ Οƒ), A βˆͺ B ∈ Οƒ) : topological_space X := { is_open := Ξ» U, U ∈ compl '' Οƒ, -- the corresponding `is_open` empty_mem := -- sorry begin use univ, split, assumption, exact compl_univ, end -- sorry , univ_mem := -- sorry begin use βˆ…, split, assumption, exact compl_empty, end -- sorry , union := -- sorry begin intros B hB, use (⋃₀ B)ᢜ, split, { rw compl_sUnion, apply inter, intros b hb, have := hB bᢜ ((mem_compl_image b B).mp hb), rw mem_compl_image at this, simpa, }, exact compl_compl', end -- sorry , inter := -- sorry begin rintros A B ⟨Ac, hA, rfl⟩ ⟨Bc, hB, rfl⟩, rw [mem_compl_image, compl_inter, compl_compl, compl_compl], exact union _ hA _ hB, end -- sorry } /- Here are some more exercises: ## Exercise 8 [medium/long]: Define the cofinite topology on any type (PR it to mathlib?). ## Exercise 9 [medium/long]: Define a normed space? ## Exercise 10 [medium/long]: Define more separation axioms? -/ end topological_space
0ca02b89eda87e824be88da10b138c840c499bc9
9dc8cecdf3c4634764a18254e94d43da07142918
/src/order/category/NonemptyFinLinOrd.lean
b8a1b4a25f0f19fe548fff7126c5436472700d88
[ "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
3,251
lean
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import data.fintype.order import order.category.LinearOrder /-! # Nonempty finite linear orders This defines `NonemptyFinLinOrd`, the category of nonempty finite linear orders with monotone maps. This is the index category for simplicial objects. -/ universes u v open category_theory /-- A typeclass for nonempty finite linear orders. -/ class nonempty_fin_lin_ord (Ξ± : Type*) extends fintype Ξ±, linear_order Ξ± := (nonempty : nonempty Ξ± . tactic.apply_instance) attribute [instance] nonempty_fin_lin_ord.nonempty @[priority 100] instance nonempty_fin_lin_ord.to_bounded_order (Ξ± : Type*) [nonempty_fin_lin_ord Ξ±] : bounded_order Ξ± := fintype.to_bounded_order Ξ± instance punit.nonempty_fin_lin_ord : nonempty_fin_lin_ord punit := { } instance fin.nonempty_fin_lin_ord (n : β„•) : nonempty_fin_lin_ord (fin (n+1)) := { } instance ulift.nonempty_fin_lin_ord (Ξ± : Type u) [nonempty_fin_lin_ord Ξ±] : nonempty_fin_lin_ord (ulift.{v} Ξ±) := { .. linear_order.lift' equiv.ulift (equiv.injective _) } instance (Ξ± : Type*) [nonempty_fin_lin_ord Ξ±] : nonempty_fin_lin_ord Ξ±α΅’α΅ˆ := { ..order_dual.fintype Ξ± } /-- The category of nonempty finite linear orders. -/ def NonemptyFinLinOrd := bundled nonempty_fin_lin_ord namespace NonemptyFinLinOrd instance : bundled_hom.parent_projection @nonempty_fin_lin_ord.to_linear_order := ⟨⟩ attribute [derive [large_category, concrete_category]] NonemptyFinLinOrd instance : has_coe_to_sort NonemptyFinLinOrd Type* := bundled.has_coe_to_sort /-- Construct a bundled `NonemptyFinLinOrd` from the underlying type and typeclass. -/ def of (Ξ± : Type*) [nonempty_fin_lin_ord Ξ±] : NonemptyFinLinOrd := bundled.of Ξ± @[simp] lemma coe_of (Ξ± : Type*) [nonempty_fin_lin_ord Ξ±] : β†₯(of Ξ±) = Ξ± := rfl instance : inhabited NonemptyFinLinOrd := ⟨of punit⟩ instance (Ξ± : NonemptyFinLinOrd) : nonempty_fin_lin_ord Ξ± := Ξ±.str instance has_forget_to_LinearOrder : has_forgetβ‚‚ NonemptyFinLinOrd LinearOrder := bundled_hom.forgetβ‚‚ _ _ /-- Constructs an equivalence between nonempty finite linear orders from an order isomorphism between them. -/ @[simps] def iso.mk {Ξ± Ξ² : NonemptyFinLinOrd.{u}} (e : Ξ± ≃o Ξ²) : Ξ± β‰… Ξ² := { hom := e, inv := e.symm, hom_inv_id' := by { ext, exact e.symm_apply_apply x }, inv_hom_id' := by { ext, exact e.apply_symm_apply x } } /-- `order_dual` as a functor. -/ @[simps] def dual : NonemptyFinLinOrd β₯€ NonemptyFinLinOrd := { obj := Ξ» X, of Xα΅’α΅ˆ, map := Ξ» X Y, order_hom.dual } /-- The equivalence between `FinPartialOrder` and itself induced by `order_dual` both ways. -/ @[simps functor inverse] def dual_equiv : NonemptyFinLinOrd β‰Œ NonemptyFinLinOrd := equivalence.mk dual dual (nat_iso.of_components (Ξ» X, iso.mk $ order_iso.dual_dual X) $ Ξ» X Y f, rfl) (nat_iso.of_components (Ξ» X, iso.mk $ order_iso.dual_dual X) $ Ξ» X Y f, rfl) end NonemptyFinLinOrd lemma NonemptyFinLinOrd_dual_comp_forget_to_LinearOrder : NonemptyFinLinOrd.dual β‹™ forgetβ‚‚ NonemptyFinLinOrd LinearOrder = forgetβ‚‚ NonemptyFinLinOrd LinearOrder β‹™ LinearOrder.dual := rfl
f9ba55fcbe44b1f3ad383e48bc17d9ee0ad79d23
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/src/number_theory/function_field.lean
745e5f77ad3f2a9dfaef2536d8902b377c176c65
[ "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,283
lean
/- Copyright (c) 2021 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen, Ashvni Narayanan -/ import ring_theory.algebraic import ring_theory.localization import ring_theory.integrally_closed /-! # Function fields This file defines a function field and the ring of integers corresponding to it. ## Main definitions - `function_field Fq F` states that `F` is a function field over the (finite) field `Fq`, i.e. it is a finite extension of the field of rational polynomials in one variable over `Fq`. - `function_field.ring_of_integers` defines the ring of integers corresponding to a function field as the integral closure of `polynomial Fq` in the function field. ## Implementation notes The definitions that involve a field of fractions choose a canonical field of fractions, but are independent of that choice. We also omit assumptions like `finite Fq` or `is_scalar_tower (polynomial Fq) (fraction_ring (polynomial Fq)) F` in definitions, adding them back in lemmas when they are needed. ## References * [D. Marcus, *Number Fields*][marcus1977number] * [J.W.S. Cassels, A. FrΓΆlich, *Algebraic Number Theory*][cassels1967algebraic] * [P. Samuel, *Algebraic Theory of Numbers*][samuel1970algebraic] ## Tags function field, ring of integers -/ noncomputable theory variables (Fq F : Type) [field Fq] [field F] /-- `F` is a function field over the finite field `Fq` if it is a finite extension of the field of rational polynomials in one variable over `Fq`. Note that `F` can be a function field over multiple, non-isomorphic, `Fq`. -/ abbreviation function_field [algebra (fraction_ring (polynomial Fq)) F] : Prop := finite_dimensional (fraction_ring (polynomial Fq)) F /-- `F` is a function field over `Fq` iff it is a finite extension of `Fq(t)`. -/ protected lemma function_field_iff (Fqt : Type*) [field Fqt] [algebra (polynomial Fq) Fqt] [is_fraction_ring (polynomial Fq) Fqt] [algebra (fraction_ring (polynomial Fq)) F] [algebra Fqt F] [algebra (polynomial Fq) F] [is_scalar_tower (polynomial Fq) Fqt F] [is_scalar_tower (polynomial Fq) (fraction_ring (polynomial Fq)) F] : function_field Fq F ↔ finite_dimensional Fqt F := begin let e := fraction_ring.alg_equiv (polynomial Fq) Fqt, have : βˆ€ c (x : F), e c β€’ x = c β€’ x, { intros c x, rw [algebra.smul_def, algebra.smul_def], congr, refine congr_fun _ c, refine is_localization.ext (non_zero_divisors (polynomial Fq)) _ _ _ _ _ _ _; intros; simp only [alg_equiv.map_one, ring_hom.map_one, alg_equiv.map_mul, ring_hom.map_mul, alg_equiv.commutes, ← is_scalar_tower.algebra_map_apply], }, split; intro h; resetI, { let b := finite_dimensional.fin_basis (fraction_ring (polynomial Fq)) F, exact finite_dimensional.of_fintype_basis (b.map_coeffs e this) }, { let b := finite_dimensional.fin_basis Fqt F, refine finite_dimensional.of_fintype_basis (b.map_coeffs e.symm _), intros c x, convert (this (e.symm c) x).symm; simp }, end namespace function_field /-- The function field analogue of `number_field.ring_of_integers`: `function_field.ring_of_integers Fq Fqt F` is the integral closure of `Fq[t]` in `F`. We don't actually assume `F` is a function field over `Fq` in the definition, only when proving its properties. -/ def ring_of_integers [algebra (polynomial Fq) F] := integral_closure (polynomial Fq) F namespace ring_of_integers variables [algebra (polynomial Fq) F] instance : integral_domain (ring_of_integers Fq F) := (ring_of_integers Fq F).integral_domain instance : is_integral_closure (ring_of_integers Fq F) (polynomial Fq) F := integral_closure.is_integral_closure _ _ variables [algebra (fraction_ring (polynomial Fq)) F] [function_field Fq F] variables [is_scalar_tower (polynomial Fq) (fraction_ring (polynomial Fq)) F] instance : is_fraction_ring (ring_of_integers Fq F) F := integral_closure.is_fraction_ring_of_finite_extension (fraction_ring (polynomial Fq)) F instance : is_integrally_closed (ring_of_integers Fq F) := integral_closure.is_integrally_closed_of_finite_extension (fraction_ring (polynomial Fq)) -- TODO: show `ring_of_integers Fq F` is a Dedekind domain end ring_of_integers end function_field
98885f5a0b5f88e5e95296a4326e3f5836853db5
2a70b774d16dbdf5a533432ee0ebab6838df0948
/_target/deps/mathlib/src/linear_algebra/matrix.lean
01350ea12c5f6c5160909cec235f772ee7cdb652
[ "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
38,431
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, Patrick Massot, Casper Putz -/ import linear_algebra.finite_dimensional import linear_algebra.nonsingular_inverse import linear_algebra.multilinear import linear_algebra.dual /-! # Linear maps and matrices This file defines the maps to send matrices to a linear map, and to send linear maps between modules with a finite bases to matrices. This defines a linear equivalence between linear maps between finite-dimensional vector spaces and matrices indexed by the respective bases. It also defines the trace of an endomorphism, and the determinant of a family of vectors with respect to some basis. Some results are proved about the linear map corresponding to a diagonal matrix (`range`, `ker` and `rank`). ## Main definitions In the list below, and in all this file, `R` is a commutative ring (semiring is sometimes enough), `M` and its variations are `R`-modules, `ΞΉ`, `ΞΊ`, `n` and `m` are finite types used for indexing. * `linear_map.to_matrix`: given bases `v₁ : ΞΉ β†’ M₁` and `vβ‚‚ : ΞΊ β†’ Mβ‚‚`, the `R`-linear equivalence from `M₁ β†’β‚—[R] Mβ‚‚` to `matrix ΞΊ ΞΉ R` * `matrix.to_lin`: the inverse of `linear_map.to_matrix` * `linear_map.to_matrix'`: the `R`-linear equivalence from `(n β†’ R) β†’β‚—[R] (m β†’ R)` to `matrix n m R` (with the standard basis on `n β†’ R` and `m β†’ R`) * `matrix.to_lin'`: the inverse of `linear_map.to_matrix'` * `alg_equiv_matrix`: given a basis indexed by `n`, the `R`-algebra equivalence between `R`-endomorphisms of `M` and `matrix n n R` * `matrix.trace`: the trace of a square matrix * `linear_map.trace`: the trace of an endomorphism * `is_basis.to_matrix`: the matrix whose columns are a given family of vectors in a given basis * `is_basis.to_matrix_equiv`: given a basis, the linear equivalence between families of vectors and matrices arising from `is_basis.to_matrix` * `is_basis.det`: the determinant of a family of vectors with respect to a basis, as a multilinear map ## Tags linear_map, matrix, linear_equiv, diagonal, det, trace -/ noncomputable theory open linear_map matrix set submodule open_locale big_operators open_locale matrix universes u v w section to_matrix' variables {R : Type*} [comm_ring R] variables {l m n : Type*} [fintype l] [fintype m] [fintype n] instance [decidable_eq m] [decidable_eq n] (R) [fintype R] : fintype (matrix m n R) := by unfold matrix; apply_instance /-- `matrix.mul_vec M` is a linear map. -/ def matrix.mul_vec_lin (M : matrix m n R) : (n β†’ R) β†’β‚—[R] (m β†’ R) := { to_fun := M.mul_vec, map_add' := Ξ» v w, funext (Ξ» i, dot_product_add _ _ _), map_smul' := Ξ» c v, funext (Ξ» i, dot_product_smul _ _ _) } @[simp] lemma matrix.mul_vec_lin_apply (M : matrix m n R) (v : n β†’ R) : matrix.mul_vec_lin M v = M.mul_vec v := rfl variables [decidable_eq n] @[simp] lemma matrix.mul_vec_std_basis (M : matrix m n R) (i j) : M.mul_vec (std_basis R (Ξ» _, R) j 1) i = M i j := begin have : (βˆ‘ j', M i j' * if j = j' then 1 else 0) = M i j, { simp_rw [mul_boole, finset.sum_ite_eq, finset.mem_univ, if_true] }, convert this, ext, split_ifs with h; simp only [std_basis_apply], { rw [h, function.update_same] }, { rw [function.update_noteq (ne.symm h), pi.zero_apply] } end /-- Linear maps `(n β†’ R) β†’β‚—[R] (m β†’ R)` are linearly equivalent to `matrix m n R`. -/ def linear_map.to_matrix' : ((n β†’ R) β†’β‚—[R] (m β†’ R)) ≃ₗ[R] matrix m n R := { to_fun := Ξ» f i j, f (std_basis R (Ξ» _, R) j 1) i, inv_fun := matrix.mul_vec_lin, right_inv := Ξ» M, by { ext i j, simp only [matrix.mul_vec_std_basis, matrix.mul_vec_lin_apply] }, left_inv := Ξ» f, begin apply (pi.is_basis_fun R n).ext, intro j, ext i, simp only [matrix.mul_vec_std_basis, matrix.mul_vec_lin_apply] end, map_add' := Ξ» f g, by { ext i j, simp only [pi.add_apply, linear_map.add_apply] }, map_smul' := Ξ» c f, by { ext i j, simp only [pi.smul_apply, linear_map.smul_apply] } } /-- A `matrix m n R` is linearly equivalent to a linear map `(n β†’ R) β†’β‚—[R] (m β†’ R)`. -/ def matrix.to_lin' : matrix m n R ≃ₗ[R] ((n β†’ R) β†’β‚—[R] (m β†’ R)) := linear_map.to_matrix'.symm @[simp] lemma linear_map.to_matrix'_symm : (linear_map.to_matrix'.symm : matrix m n R ≃ₗ[R] _) = matrix.to_lin' := rfl @[simp] lemma matrix.to_lin'_symm : (matrix.to_lin'.symm : ((n β†’ R) β†’β‚—[R] (m β†’ R)) ≃ₗ[R] _) = linear_map.to_matrix' := rfl @[simp] lemma linear_map.to_matrix'_to_lin' (M : matrix m n R) : linear_map.to_matrix' (matrix.to_lin' M) = M := linear_map.to_matrix'.apply_symm_apply M @[simp] lemma matrix.to_lin'_to_matrix' (f : (n β†’ R) β†’β‚—[R] (m β†’ R)) : matrix.to_lin' (linear_map.to_matrix' f) = f := matrix.to_lin'.apply_symm_apply f @[simp] lemma linear_map.to_matrix'_apply (f : (n β†’ R) β†’β‚—[R] (m β†’ R)) (i j) : linear_map.to_matrix' f i j = f (Ξ» j', if j' = j then 1 else 0) i := begin simp only [linear_map.to_matrix', linear_equiv.mk_apply], congr, ext j', split_ifs with h, { rw [h, std_basis_same] }, apply std_basis_ne _ _ _ _ h end @[simp] lemma matrix.to_lin'_apply (M : matrix m n R) (v : n β†’ R) : matrix.to_lin' M v = M.mul_vec v := rfl @[simp] lemma matrix.to_lin'_one : matrix.to_lin' (1 : matrix n n R) = id := by { ext, simp } @[simp] lemma linear_map.to_matrix'_id : (linear_map.to_matrix' (linear_map.id : (n β†’ R) β†’β‚—[R] (n β†’ R))) = 1 := by { ext, rw [matrix.one_apply, linear_map.to_matrix'_apply, id_apply] } @[simp] lemma matrix.to_lin'_mul [decidable_eq m] (M : matrix l m R) (N : matrix m n R) : matrix.to_lin' (M ⬝ N) = (matrix.to_lin' M).comp (matrix.to_lin' N) := by { ext, simp } lemma linear_map.to_matrix'_comp [decidable_eq l] (f : (n β†’ R) β†’β‚—[R] (m β†’ R)) (g : (l β†’ R) β†’β‚—[R] (n β†’ R)) : (f.comp g).to_matrix' = f.to_matrix' ⬝ g.to_matrix' := suffices (f.comp g) = (f.to_matrix' ⬝ g.to_matrix').to_lin', by rw [this, linear_map.to_matrix'_to_lin'], by rw [matrix.to_lin'_mul, matrix.to_lin'_to_matrix', matrix.to_lin'_to_matrix'] lemma linear_map.to_matrix'_mul [decidable_eq m] (f g : (m β†’ R) β†’β‚—[R] (m β†’ R)) : (f * g).to_matrix' = f.to_matrix' ⬝ g.to_matrix' := linear_map.to_matrix'_comp f g end to_matrix' section to_matrix variables {R : Type*} [comm_ring R] variables {l m n : Type*} [fintype l] [fintype m] [fintype n] [decidable_eq n] variables {M₁ Mβ‚‚ : Type*} [add_comm_group M₁] [add_comm_group Mβ‚‚] [module R M₁] [module R Mβ‚‚] variables {v₁ : n β†’ M₁} (hv₁ : is_basis R v₁) {vβ‚‚ : m β†’ Mβ‚‚} (hvβ‚‚ : is_basis R vβ‚‚) /-- Given bases of two modules `M₁` and `Mβ‚‚` over a commutative ring `R`, we get a linear equivalence between linear maps `M₁ β†’β‚— Mβ‚‚` and matrices over `R` indexed by the bases. -/ def linear_map.to_matrix : (M₁ β†’β‚—[R] Mβ‚‚) ≃ₗ[R] matrix m n R := linear_equiv.trans (linear_equiv.arrow_congr hv₁.equiv_fun hvβ‚‚.equiv_fun) linear_map.to_matrix' /-- Given bases of two modules `M₁` and `Mβ‚‚` over a commutative ring `R`, we get a linear equivalence between matrices over `R` indexed by the bases and linear maps `M₁ β†’β‚— Mβ‚‚`. -/ def matrix.to_lin : matrix m n R ≃ₗ[R] (M₁ β†’β‚—[R] Mβ‚‚) := (linear_map.to_matrix hv₁ hvβ‚‚).symm @[simp] lemma linear_map.to_matrix_symm : (linear_map.to_matrix hv₁ hvβ‚‚).symm = matrix.to_lin hv₁ hvβ‚‚ := rfl @[simp] lemma matrix.to_lin_symm : (matrix.to_lin hv₁ hvβ‚‚).symm = linear_map.to_matrix hv₁ hvβ‚‚ := rfl @[simp] lemma matrix.to_lin_to_matrix (f : M₁ β†’β‚—[R] Mβ‚‚) : matrix.to_lin hv₁ hvβ‚‚ (linear_map.to_matrix hv₁ hvβ‚‚ f) = f := by rw [← matrix.to_lin_symm, linear_equiv.apply_symm_apply] @[simp] lemma linear_map.to_matrix_to_lin (M : matrix m n R) : linear_map.to_matrix hv₁ hvβ‚‚ (matrix.to_lin hv₁ hvβ‚‚ M) = M := by rw [← matrix.to_lin_symm, linear_equiv.symm_apply_apply] lemma linear_map.to_matrix_apply (f : M₁ β†’β‚—[R] Mβ‚‚) (i : m) (j : n) : linear_map.to_matrix hv₁ hvβ‚‚ f i j = hvβ‚‚.equiv_fun (f (v₁ j)) i := begin rw [linear_map.to_matrix, linear_equiv.trans_apply, linear_map.to_matrix'_apply, linear_equiv.arrow_congr_apply, is_basis.equiv_fun_symm_apply, finset.sum_eq_single j, if_pos rfl, one_smul], { intros j' _ hj', rw [if_neg hj', zero_smul] }, { intro hj, have := finset.mem_univ j, contradiction } end lemma linear_map.to_matrix_transpose_apply (f : M₁ β†’β‚—[R] Mβ‚‚) (j : n) : (linear_map.to_matrix hv₁ hvβ‚‚ f)α΅€ j = hvβ‚‚.equiv_fun (f (v₁ j)) := funext $ Ξ» i, f.to_matrix_apply _ _ i j lemma linear_map.to_matrix_apply' (f : M₁ β†’β‚—[R] Mβ‚‚) (i : m) (j : n) : linear_map.to_matrix hv₁ hvβ‚‚ f i j = hvβ‚‚.repr (f (v₁ j)) i := linear_map.to_matrix_apply hv₁ hvβ‚‚ f i j lemma linear_map.to_matrix_transpose_apply' (f : M₁ β†’β‚—[R] Mβ‚‚) (j : n) : (linear_map.to_matrix hv₁ hvβ‚‚ f)α΅€ j = hvβ‚‚.repr (f (v₁ j)) := linear_map.to_matrix_transpose_apply hv₁ hvβ‚‚ f j lemma matrix.to_lin_apply (M : matrix m n R) (v : M₁) : matrix.to_lin hv₁ hvβ‚‚ M v = βˆ‘ j, M.mul_vec (hv₁.equiv_fun v) j β€’ vβ‚‚ j := show hvβ‚‚.equiv_fun.symm (matrix.to_lin' M (hv₁.equiv_fun v)) = _, by rw [matrix.to_lin'_apply, hvβ‚‚.equiv_fun_symm_apply] @[simp] lemma matrix.to_lin_self (M : matrix m n R) (i : n) : matrix.to_lin hv₁ hvβ‚‚ M (v₁ i) = βˆ‘ j, M j i β€’ vβ‚‚ j := by simp only [matrix.to_lin_apply, matrix.mul_vec, dot_product, hv₁.equiv_fun_self, mul_boole, finset.sum_ite_eq, finset.mem_univ, if_true] @[simp] lemma linear_map.to_matrix_id : linear_map.to_matrix hv₁ hv₁ id = 1 := begin ext i j, simp [linear_map.to_matrix_apply, is_basis.equiv_fun, matrix.one_apply, finsupp.single, eq_comm] end @[simp] lemma matrix.to_lin_one : matrix.to_lin hv₁ hv₁ 1 = id := by rw [← linear_map.to_matrix_id hv₁, matrix.to_lin_to_matrix] theorem linear_map.to_matrix_range [decidable_eq M₁] [decidable_eq Mβ‚‚] (f : M₁ β†’β‚—[R] Mβ‚‚) (k : m) (i : n) : linear_map.to_matrix hv₁.range hvβ‚‚.range f ⟨vβ‚‚ k, mem_range_self k⟩ ⟨v₁ i, mem_range_self i⟩ = linear_map.to_matrix hv₁ hvβ‚‚ f k i := by simp_rw [linear_map.to_matrix_apply, subtype.coe_mk, is_basis.equiv_fun_apply, hvβ‚‚.range_repr] variables {M₃ : Type*} [add_comm_group M₃] [module R M₃] {v₃ : l β†’ M₃} (hv₃ : is_basis R v₃) lemma linear_map.to_matrix_comp [decidable_eq m] (f : Mβ‚‚ β†’β‚—[R] M₃) (g : M₁ β†’β‚—[R] Mβ‚‚) : linear_map.to_matrix hv₁ hv₃ (f.comp g) = linear_map.to_matrix hvβ‚‚ hv₃ f ⬝ linear_map.to_matrix hv₁ hvβ‚‚ g := by simp_rw [linear_map.to_matrix, linear_equiv.trans_apply, linear_equiv.arrow_congr_comp _ hvβ‚‚.equiv_fun, linear_map.to_matrix'_comp] lemma linear_map.to_matrix_mul (f g : M₁ β†’β‚—[R] M₁) : linear_map.to_matrix hv₁ hv₁ (f * g) = linear_map.to_matrix hv₁ hv₁ f ⬝ linear_map.to_matrix hv₁ hv₁ g := by { rw [show (@has_mul.mul (M₁ β†’β‚—[R] M₁) _) = linear_map.comp, from rfl, linear_map.to_matrix_comp hv₁ hv₁ hv₁ f g] } lemma matrix.to_lin_mul [decidable_eq m] (A : matrix l m R) (B : matrix m n R) : matrix.to_lin hv₁ hv₃ (A ⬝ B) = (matrix.to_lin hvβ‚‚ hv₃ A).comp (matrix.to_lin hv₁ hvβ‚‚ B) := begin apply (linear_map.to_matrix hv₁ hv₃).injective, haveI : decidable_eq l := Ξ» _ _, classical.prop_decidable _, rw linear_map.to_matrix_comp hv₁ hvβ‚‚ hv₃, repeat { rw linear_map.to_matrix_to_lin }, end end to_matrix section is_basis_to_matrix variables {ΞΉ ΞΉ' : Type*} [fintype ΞΉ] [fintype ΞΉ'] variables {R M : Type*} [comm_ring R] [add_comm_group M] [module R M] open function matrix /-- From a basis `e : ΞΉ β†’ M` and a family of vectors `v : ΞΉ' β†’ M`, make the matrix whose columns are the vectors `v i` written in the basis `e`. -/ def is_basis.to_matrix {e : ΞΉ β†’ M} (he : is_basis R e) (v : ΞΉ' β†’ M) : matrix ΞΉ ΞΉ' R := Ξ» i j, he.equiv_fun (v j) i variables {e : ΞΉ β†’ M} (he : is_basis R e) (v : ΞΉ' β†’ M) (i : ΞΉ) (j : ΞΉ') namespace is_basis lemma to_matrix_apply : he.to_matrix v i j = he.equiv_fun (v j) i := rfl lemma to_matrix_transpose_apply : (he.to_matrix v)α΅€ j = he.repr (v j) := funext $ (Ξ» _, rfl) lemma to_matrix_eq_to_matrix_constr [decidable_eq ΞΉ] (v : ΞΉ β†’ M) : he.to_matrix v = linear_map.to_matrix he he (he.constr v) := by { ext, simp [is_basis.to_matrix_apply, linear_map.to_matrix_apply] } @[simp] lemma to_matrix_self [decidable_eq ΞΉ] : he.to_matrix e = 1 := begin rw is_basis.to_matrix, ext i j, simp [is_basis.equiv_fun, matrix.one_apply, finsupp.single, eq_comm] end lemma to_matrix_update [decidable_eq ΞΉ'] (x : M) : he.to_matrix (function.update v j x) = matrix.update_column (he.to_matrix v) j (he.repr x) := begin ext i' k, rw [is_basis.to_matrix, matrix.update_column_apply, he.to_matrix_apply], split_ifs, { rw [h, update_same j x v, he.equiv_fun_apply] }, { rw update_noteq h }, end @[simp] lemma sum_to_matrix_smul_self : βˆ‘ (i : ΞΉ), he.to_matrix v i j β€’ e i = v j := begin conv_rhs { rw ← he.total_repr (v j) }, rw [finsupp.total_apply, finsupp.sum_fintype], { refl }, simp end @[simp] lemma to_lin_to_matrix [decidable_eq ΞΉ'] (hv : is_basis R v) : matrix.to_lin hv he (he.to_matrix v) = id := hv.ext (Ξ» i, by rw [to_lin_self, id_apply, he.sum_to_matrix_smul_self]) /-- From a basis `e : ΞΉ β†’ M`, build a linear equivalence between families of vectors `v : ΞΉ β†’ M`, and matrices, making the matrix whose columns are the vectors `v i` written in the basis `e`. -/ def to_matrix_equiv {e : ΞΉ β†’ M} (he : is_basis R e) : (ΞΉ β†’ M) ≃ₗ[R] matrix ΞΉ ΞΉ R := { to_fun := he.to_matrix, map_add' := Ξ» v w, begin ext i j, change _ = _ + _, simp [he.to_matrix_apply] end, map_smul' := begin intros c v, ext i j, simp [he.to_matrix_apply] end, inv_fun := Ξ» m j, βˆ‘ i, (m i j) β€’ e i, left_inv := begin intro v, ext j, simp [he.to_matrix_apply, he.equiv_fun_total (v j)] end, right_inv := begin intros x, ext k l, simp [he.to_matrix_apply, he.equiv_fun.map_sum, he.equiv_fun.map_smul, fintype.sum_apply k (Ξ» i, x i l β€’ he.equiv_fun (e i)), he.equiv_fun_self] end } end is_basis section mul_linear_map_to_matrix variables {N : Type*} [add_comm_group N] [module R N] variables {b : ΞΉ β†’ M} {b' : ΞΉ' β†’ M} {c : ΞΉ β†’ N} {c' : ΞΉ' β†’ N} variables (hb : is_basis R b) (hb' : is_basis R b') (hc : is_basis R c) (hc' : is_basis R c') variables (f : M β†’β‚—[R] N) @[simp] lemma is_basis_to_matrix_mul_linear_map_to_matrix [decidable_eq ΞΉ'] : hc.to_matrix c' ⬝ linear_map.to_matrix hb' hc' f = linear_map.to_matrix hb' hc f := (matrix.to_lin hb' hc).injective (by rw [to_lin_to_matrix, to_lin_mul hb' hc' hc, to_lin_to_matrix, hc.to_lin_to_matrix, id_comp]) @[simp] lemma linear_map_to_matrix_mul_is_basis_to_matrix [decidable_eq ΞΉ] [decidable_eq ΞΉ'] : linear_map.to_matrix hb' hc' f ⬝ hb'.to_matrix b = linear_map.to_matrix hb hc' f := (matrix.to_lin hb hc').injective (by rw [to_lin_to_matrix, to_lin_mul hb hb' hc', to_lin_to_matrix, hb'.to_lin_to_matrix, comp_id]) end mul_linear_map_to_matrix end is_basis_to_matrix open_locale matrix section det open linear_map matrix variables {R : Type} [comm_ring R] variables {M : Type*} [add_comm_group M] [module R M] variables {M' : Type*} [add_comm_group M'] [module R M'] variables {ΞΉ : Type*} [decidable_eq ΞΉ] [fintype ΞΉ] {v : ΞΉ β†’ M} {v' : ΞΉ β†’ M'} lemma linear_equiv.is_unit_det (f : M ≃ₗ[R] M') (hv : is_basis R v) (hv' : is_basis R v') : is_unit (linear_map.to_matrix hv hv' f).det := begin apply is_unit_det_of_left_inverse, simpa using (linear_map.to_matrix_comp hv hv' hv f.symm f).symm end /-- Builds a linear equivalence from a linear map whose determinant in some bases is a unit. -/ def linear_equiv.of_is_unit_det {f : M β†’β‚—[R] M'} {hv : is_basis R v} {hv' : is_basis R v'} (h : is_unit (linear_map.to_matrix hv hv' f).det) : M ≃ₗ[R] M' := { to_fun := f, map_add' := f.map_add, map_smul' := f.map_smul, inv_fun := to_lin hv' hv (to_matrix hv hv' f)⁻¹, left_inv := Ξ» x, calc to_lin hv' hv (to_matrix hv hv' f)⁻¹ (f x) = to_lin hv hv ((to_matrix hv hv' f)⁻¹ ⬝ to_matrix hv hv' f) x : by { rw [to_lin_mul hv hv' hv, to_lin_to_matrix, linear_map.comp_apply] } ... = x : by simp [h], right_inv := Ξ» x, calc f (to_lin hv' hv (to_matrix hv hv' f)⁻¹ x) = to_lin hv' hv' (to_matrix hv hv' f ⬝ (to_matrix hv hv' f)⁻¹) x : by { rw [to_lin_mul hv' hv hv', linear_map.comp_apply, to_lin_to_matrix hv hv'] } ... = x : by simp [h], } variables {e : ΞΉ β†’ M} (he : is_basis R e) /-- The determinant of a family of vectors with respect to some basis, as an alternating multilinear map. -/ def is_basis.det : alternating_map R M R ΞΉ := { to_fun := Ξ» v, det (he.to_matrix v), map_add' := begin intros v i x y, simp only [he.to_matrix_update, linear_map.map_add], apply det_update_column_add end, map_smul' := begin intros u i c x, simp only [he.to_matrix_update, algebra.id.smul_eq_mul, map_smul_of_tower], apply det_update_column_smul end, map_eq_zero_of_eq' := begin intros v i j h hij, rw [←function.update_eq_self i v, h, ←det_transpose, he.to_matrix_update, ←update_row_transpose, ←he.to_matrix_transpose_apply], apply det_zero_of_row_eq hij, rw [update_row_ne hij.symm, update_row_self], end } lemma is_basis.det_apply (v : ΞΉ β†’ M) : he.det v = det (he.to_matrix v) := rfl lemma is_basis.det_self : he.det e = 1 := by simp [he.det_apply] lemma is_basis.iff_det {v : ΞΉ β†’ M} : is_basis R v ↔ is_unit (he.det v) := begin split, { intro hv, suffices : is_unit (linear_map.to_matrix he he (linear_equiv_of_is_basis he hv $ equiv.refl ΞΉ)).det, { rw [is_basis.det_apply, is_basis.to_matrix_eq_to_matrix_constr], exact this }, apply linear_equiv.is_unit_det }, { intro h, rw [is_basis.det_apply, is_basis.to_matrix_eq_to_matrix_constr] at h, convert linear_equiv.is_basis he (linear_equiv.of_is_unit_det h), ext i, exact (constr_basis he).symm }, end end det section transpose variables {K V₁ Vβ‚‚ ι₁ ΞΉβ‚‚ : Type*} [field K] [add_comm_group V₁] [vector_space K V₁] [add_comm_group Vβ‚‚] [vector_space K Vβ‚‚] [fintype ι₁] [fintype ΞΉβ‚‚] [decidable_eq ι₁] [decidable_eq ΞΉβ‚‚] {B₁ : ι₁ β†’ V₁} (h₁ : is_basis K B₁) {Bβ‚‚ : ΞΉβ‚‚ β†’ Vβ‚‚} (hβ‚‚ : is_basis K Bβ‚‚) @[simp] lemma linear_map.to_matrix_transpose (u : V₁ β†’β‚—[K] Vβ‚‚) : linear_map.to_matrix hβ‚‚.dual_basis_is_basis h₁.dual_basis_is_basis (module.dual.transpose u) = (linear_map.to_matrix h₁ hβ‚‚ u)α΅€ := begin ext i j, simp only [linear_map.to_matrix_apply, module.dual.transpose_apply, h₁.dual_basis_equiv_fun, hβ‚‚.dual_basis_apply, matrix.transpose_apply, linear_map.comp_apply] end lemma linear_map.to_matrix_symm_transpose (M : matrix ι₁ ΞΉβ‚‚ K) : (linear_map.to_matrix h₁.dual_basis_is_basis hβ‚‚.dual_basis_is_basis).symm Mα΅€ = module.dual.transpose (matrix.to_lin hβ‚‚ h₁ M) := begin apply (linear_map.to_matrix h₁.dual_basis_is_basis hβ‚‚.dual_basis_is_basis).injective, rw [linear_equiv.apply_symm_apply], ext i j, simp only [linear_map.to_matrix_apply, module.dual.transpose_apply, hβ‚‚.dual_basis_equiv_fun, h₁.dual_basis_apply, matrix.transpose_apply, linear_map.comp_apply, if_true, matrix.to_lin_apply, linear_equiv.map_smul, mul_boole, algebra.id.smul_eq_mul, linear_equiv.map_sum, is_basis.equiv_fun_self, fintype.sum_apply, finset.sum_ite_eq', finset.sum_ite_eq, is_basis.equiv_fun_symm_apply, pi.smul_apply, matrix.to_lin_apply, matrix.mul_vec, matrix.dot_product, is_basis.equiv_fun_self, finset.mem_univ] end end transpose namespace matrix section trace variables {m : Type*} [fintype m] (n : Type*) [fintype n] variables (R : Type v) (M : Type w) [semiring R] [add_comm_monoid M] [semimodule R M] /-- The diagonal of a square matrix. -/ def diag : (matrix n n M) β†’β‚—[R] n β†’ M := { to_fun := Ξ» A i, A i i, map_add' := by { intros, ext, refl, }, map_smul' := by { intros, ext, refl, } } variables {n} {R} {M} @[simp] lemma diag_apply (A : matrix n n M) (i : n) : diag n R M A i = A i i := rfl @[simp] lemma diag_one [decidable_eq n] : diag n R R 1 = Ξ» i, 1 := by { dunfold diag, ext, simp [one_apply_eq] } @[simp] lemma diag_transpose (A : matrix n n M) : diag n R M Aα΅€ = diag n R M A := rfl variables (n) (R) (M) /-- The trace of a square matrix. -/ def trace : (matrix n n M) β†’β‚—[R] M := { to_fun := Ξ» A, βˆ‘ i, diag n R M A i, map_add' := by { intros, apply finset.sum_add_distrib, }, map_smul' := by { intros, simp [finset.smul_sum], } } variables {n} {R} {M} @[simp] lemma trace_diag (A : matrix n n M) : trace n R M A = βˆ‘ i, diag n R M A i := rfl @[simp] lemma trace_one [decidable_eq n] : trace n R R 1 = fintype.card n := have h : trace n R R 1 = βˆ‘ i, diag n R R 1 i := rfl, by simp_rw [h, diag_one, finset.sum_const, nsmul_one]; refl @[simp] lemma trace_transpose (A : matrix n n M) : trace n R M Aα΅€ = trace n R M A := rfl @[simp] lemma trace_transpose_mul (A : matrix m n R) (B : matrix n m R) : trace n R R (Aα΅€ ⬝ Bα΅€) = trace m R R (A ⬝ B) := finset.sum_comm lemma trace_mul_comm {S : Type v} [comm_ring S] (A : matrix m n S) (B : matrix n m S) : trace n S S (B ⬝ A) = trace m S S (A ⬝ B) := by rw [←trace_transpose, ←trace_transpose_mul, transpose_mul] end trace section ring variables {n : Type*} [fintype n] [decidable_eq n] {R : Type v} [comm_ring R] open linear_map matrix lemma proj_diagonal (i : n) (w : n β†’ R) : (proj i).comp (to_lin' (diagonal w)) = (w i) β€’ proj i := by ext j; simp [mul_vec_diagonal] lemma diagonal_comp_std_basis (w : n β†’ R) (i : n) : (diagonal w).to_lin'.comp (std_basis R (Ξ»_:n, R) i) = (w i) β€’ std_basis R (Ξ»_:n, R) i := begin ext j, simp_rw [linear_map.comp_apply, to_lin'_apply, mul_vec_diagonal, linear_map.smul_apply, pi.smul_apply, algebra.id.smul_eq_mul], by_cases i = j, { subst h }, { rw [std_basis_ne R (Ξ»_:n, R) _ _ (ne.symm h), _root_.mul_zero, _root_.mul_zero] } end lemma diagonal_to_lin' (w : n β†’ R) : (diagonal w).to_lin' = linear_map.pi (Ξ»i, w i β€’ linear_map.proj i) := by ext v j; simp [mul_vec_diagonal] /-- An invertible matrix yields a linear equivalence from the free module to itself. -/ def to_linear_equiv (P : matrix n n R) (h : is_unit P) : (n β†’ R) ≃ₗ[R] (n β†’ R) := have h' : is_unit P.det := P.is_unit_iff_is_unit_det.mp h, { inv_fun := P⁻¹.to_lin', left_inv := Ξ» v, show (P⁻¹.to_lin'.comp P.to_lin') v = v, by rw [← matrix.to_lin'_mul, P.nonsing_inv_mul h', matrix.to_lin'_one, linear_map.id_apply], right_inv := Ξ» v, show (P.to_lin'.comp P⁻¹.to_lin') v = v, by rw [← matrix.to_lin'_mul, P.mul_nonsing_inv h', matrix.to_lin'_one, linear_map.id_apply], ..P.to_lin' } @[simp] lemma to_linear_equiv_apply (P : matrix n n R) (h : is_unit P) : (↑(P.to_linear_equiv h) : module.End R (n β†’ R)) = P.to_lin' := rfl @[simp] lemma to_linear_equiv_symm_apply (P : matrix n n R) (h : is_unit P) : (↑(P.to_linear_equiv h).symm : module.End R (n β†’ R)) = P⁻¹.to_lin' := rfl end ring section vector_space variables {m n : Type*} [fintype m] [fintype n] variables {K : Type u} [field K] -- maybe try to relax the universe constraint open linear_map matrix lemma rank_vec_mul_vec {m n : Type u} [fintype m] [fintype n] [decidable_eq n] (w : m β†’ K) (v : n β†’ K) : rank (vec_mul_vec w v).to_lin' ≀ 1 := begin rw [vec_mul_vec_eq, to_lin'_mul], refine le_trans (rank_comp_le1 _ _) _, refine le_trans (rank_le_domain _) _, rw [dim_fun', ← cardinal.lift_eq_nat_iff.mpr (cardinal.fintype_card unit), cardinal.mk_unit], exact le_of_eq (cardinal.lift_one) end lemma ker_diagonal_to_lin' [decidable_eq m] (w : m β†’ K) : ker (diagonal w).to_lin' = (⨆i∈{i | w i = 0 }, range (std_basis K (Ξ»i, K) i)) := begin rw [← comap_bot, ← infi_ker_proj], simp only [comap_infi, (ker_comp _ _).symm, proj_diagonal, ker_smul'], have : univ βŠ† {i : m | w i = 0} βˆͺ {i : m | w i = 0}ᢜ, { rw set.union_compl_self }, exact (supr_range_std_basis_eq_infi_ker_proj K (Ξ»i:m, K) disjoint_compl_right this (finite.of_fintype _)).symm end lemma range_diagonal [decidable_eq m] (w : m β†’ K) : (diagonal w).to_lin'.range = (⨆ i ∈ {i | w i β‰  0}, (std_basis K (Ξ»i, K) i).range) := begin dsimp only [mem_set_of_eq], rw [← map_top, ← supr_range_std_basis, map_supr], congr, funext i, rw [← linear_map.range_comp, diagonal_comp_std_basis, ← range_smul'] end lemma rank_diagonal [decidable_eq m] [decidable_eq K] (w : m β†’ K) : rank (diagonal w).to_lin' = fintype.card { i // w i β‰  0 } := begin have hu : univ βŠ† {i : m | w i = 0}ᢜ βˆͺ {i : m | w i = 0}, { rw set.compl_union_self }, have hd : disjoint {i : m | w i β‰  0} {i : m | w i = 0} := disjoint_compl_left, have h₁ := supr_range_std_basis_eq_infi_ker_proj K (Ξ»i:m, K) hd hu (finite.of_fintype _), have hβ‚‚ := @infi_ker_proj_equiv K _ _ (Ξ»i:m, K) _ _ _ _ (by simp; apply_instance) hd hu, rw [rank, range_diagonal, h₁, ←@dim_fun' K], apply linear_equiv.dim_eq, apply hβ‚‚, end end vector_space section finite_dimensional variables {m n : Type*} [fintype m] [fintype n] variables {R : Type v} [field R] instance : finite_dimensional R (matrix m n R) := linear_equiv.finite_dimensional (linear_equiv.uncurry R m n).symm /-- The dimension of the space of finite dimensional matrices is the product of the number of rows and columns. -/ @[simp] lemma findim_matrix : finite_dimensional.findim R (matrix m n R) = fintype.card m * fintype.card n := by rw [@linear_equiv.findim_eq R (matrix m n R) _ _ _ _ _ _ (linear_equiv.uncurry R m n), finite_dimensional.findim_fintype_fun_eq_card, fintype.card_prod] end finite_dimensional section reindexing variables {l m n : Type*} [fintype l] [fintype m] [fintype n] variables {l' m' n' : Type*} [fintype l'] [fintype m'] [fintype n'] variables {R : Type v} /-- The natural map that reindexes a matrix's rows and columns with equivalent types is an equivalence. -/ def reindex (eβ‚˜ : m ≃ m') (eβ‚™ : n ≃ n') : matrix m n R ≃ matrix m' n' R := { to_fun := Ξ» M i j, M (eβ‚˜.symm i) (eβ‚™.symm j), inv_fun := Ξ» M i j, M (eβ‚˜ i) (eβ‚™ j), left_inv := Ξ» M, by simp, right_inv := Ξ» M, by simp, } @[simp] lemma reindex_apply (eβ‚˜ : m ≃ m') (eβ‚™ : n ≃ n') (M : matrix m n R) : reindex eβ‚˜ eβ‚™ M = Ξ» i j, M (eβ‚˜.symm i) (eβ‚™.symm j) := rfl @[simp] lemma reindex_symm_apply (eβ‚˜ : m ≃ m') (eβ‚™ : n ≃ n') (M : matrix m' n' R) : (reindex eβ‚˜ eβ‚™).symm M = Ξ» i j, M (eβ‚˜ i) (eβ‚™ j) := rfl /-- The natural map that reindexes a matrix's rows and columns with equivalent types is a linear equivalence. -/ def reindex_linear_equiv [semiring R] (eβ‚˜ : m ≃ m') (eβ‚™ : n ≃ n') : matrix m n R ≃ₗ[R] matrix m' n' R := { map_add' := Ξ» M N, rfl, map_smul' := Ξ» M N, rfl, ..(reindex eβ‚˜ eβ‚™)} @[simp] lemma reindex_linear_equiv_apply [semiring R] (eβ‚˜ : m ≃ m') (eβ‚™ : n ≃ n') (M : matrix m n R) : reindex_linear_equiv eβ‚˜ eβ‚™ M = Ξ» i j, M (eβ‚˜.symm i) (eβ‚™.symm j) := rfl @[simp] lemma reindex_linear_equiv_symm_apply [semiring R] (eβ‚˜ : m ≃ m') (eβ‚™ : n ≃ n') (M : matrix m' n' R) : (reindex_linear_equiv eβ‚˜ eβ‚™).symm M = Ξ» i j, M (eβ‚˜ i) (eβ‚™ j) := rfl lemma reindex_mul [semiring R] (eβ‚˜ : m ≃ m') (eβ‚™ : n ≃ n') (eβ‚— : l ≃ l') (M : matrix m n R) (N : matrix n l R) : (reindex_linear_equiv eβ‚˜ eβ‚™ M) ⬝ (reindex_linear_equiv eβ‚™ eβ‚— N) = reindex_linear_equiv eβ‚˜ eβ‚— (M ⬝ N) := begin ext i j, dsimp only [matrix.mul, matrix.dot_product], rw [←finset.univ_map_equiv_to_embedding eβ‚™, finset.sum_map finset.univ eβ‚™.to_embedding], simp, end /-- For square matrices, the natural map that reindexes a matrix's rows and columns with equivalent types is an equivalence of algebras. -/ def reindex_alg_equiv [comm_semiring R] [decidable_eq m] [decidable_eq n] (e : m ≃ n) : matrix m m R ≃ₐ[R] matrix n n R := { map_mul' := Ξ» M N, by simp only [reindex_mul, linear_equiv.to_fun_apply, mul_eq_mul], commutes' := Ξ» r, by { ext, simp [algebra_map, algebra.to_ring_hom], by_cases h : i = j; simp [h], }, ..(reindex_linear_equiv e e) } @[simp] lemma reindex_alg_equiv_apply [comm_semiring R] [decidable_eq m] [decidable_eq n] (e : m ≃ n) (M : matrix m m R) : reindex_alg_equiv e M = Ξ» i j, M (e.symm i) (e.symm j) := rfl @[simp] lemma reindex_alg_equiv_symm_apply [comm_semiring R] [decidable_eq m] [decidable_eq n] (e : m ≃ n) (M : matrix n n R) : (reindex_alg_equiv e).symm M = Ξ» i j, M (e i) (e j) := rfl lemma reindex_transpose (eβ‚˜ : m ≃ m') (eβ‚™ : n ≃ n') (M : matrix m n R) : (reindex eβ‚˜ eβ‚™ M)α΅€ = (reindex eβ‚™ eβ‚˜ Mα΅€) := rfl /-- `simp` version of `det_reindex_self` `det_reindex_self` is not a good simp lemma because `reindex_apply` fires before. So we have this lemma to continue from there. -/ @[simp] lemma det_reindex_self' [decidable_eq m] [decidable_eq n] [comm_ring R] (e : m ≃ n) (A : matrix m m R) : det (Ξ» i j, A (e.symm i) (e.symm j)) = det A := begin unfold det, apply finset.sum_bij' (Ξ» Οƒ _, equiv.perm_congr e.symm Οƒ) _ _ (Ξ» Οƒ _, equiv.perm_congr e Οƒ), { intros Οƒ _, ext, simp only [equiv.symm_symm, equiv.perm_congr_apply, equiv.apply_symm_apply] }, { intros Οƒ _, ext, simp only [equiv.symm_symm, equiv.perm_congr_apply, equiv.symm_apply_apply] }, { intros Οƒ _, apply finset.mem_univ }, { intros Οƒ _, apply finset.mem_univ }, intros Οƒ _, simp_rw [equiv.perm_congr_apply, equiv.symm_symm], congr, { convert (equiv.perm.sign_perm_congr e.symm Οƒ).symm }, apply finset.prod_bij' (Ξ» i _, e.symm i) _ _ (Ξ» i _, e i), { intros, simp_rw equiv.apply_symm_apply }, { intros, simp_rw equiv.symm_apply_apply }, { intros, apply finset.mem_univ }, { intros, apply finset.mem_univ }, { intros, simp_rw equiv.apply_symm_apply }, end /-- Reindexing both indices along the same equivalence preserves the determinant. For the `simp` version of this lemma, see `det_reindex_self'`. -/ lemma det_reindex_self [decidable_eq m] [decidable_eq n] [comm_ring R] (e : m ≃ n) (A : matrix m m R) : det (reindex e e A) = det A := det_reindex_self' e A /-- Reindexing both indices along the same equivalence preserves the determinant. For the `simp` version of this lemma, see `det_reindex_self'`. -/ lemma det_reindex_linear_equiv_self [decidable_eq m] [decidable_eq n] [comm_ring R] (e : m ≃ n) (A : matrix m m R) : det (reindex_linear_equiv e e A) = det A := det_reindex_self' e A /-- Reindexing both indices along the same equivalence preserves the determinant. For the `simp` version of this lemma, see `det_reindex_self'`. -/ lemma det_reindex_alg_equiv [decidable_eq m] [decidable_eq n] [comm_ring R] (e : m ≃ n) (A : matrix m m R) : det (reindex_alg_equiv e A) = det A := det_reindex_self' e A end reindexing end matrix namespace linear_map open_locale matrix /-- The trace of an endomorphism given a basis. -/ def trace_aux (R : Type u) [comm_ring R] {M : Type v} [add_comm_group M] [module R M] {ΞΉ : Type w} [decidable_eq ΞΉ] [fintype ΞΉ] {b : ΞΉ β†’ M} (hb : is_basis R b) : (M β†’β‚—[R] M) β†’β‚—[R] R := (matrix.trace ΞΉ R R).comp $ linear_map.to_matrix hb hb @[simp] lemma trace_aux_def (R : Type u) [comm_ring R] {M : Type v} [add_comm_group M] [module R M] {ΞΉ : Type w} [decidable_eq ΞΉ] [fintype ΞΉ] {b : ΞΉ β†’ M} (hb : is_basis R b) (f : M β†’β‚—[R] M) : trace_aux R hb f = matrix.trace ΞΉ R R (linear_map.to_matrix hb hb f) := rfl theorem trace_aux_eq' (R : Type u) [comm_ring R] {M : Type v} [add_comm_group M] [module R M] {ΞΉ : Type w} [decidable_eq ΞΉ] [fintype ΞΉ] {b : ΞΉ β†’ M} (hb : is_basis R b) {ΞΊ : Type w} [decidable_eq ΞΊ] [fintype ΞΊ] {c : ΞΊ β†’ M} (hc : is_basis R c) : trace_aux R hb = trace_aux R hc := linear_map.ext $ Ξ» f, calc matrix.trace ΞΉ R R (linear_map.to_matrix hb hb f) = matrix.trace ΞΉ R R (linear_map.to_matrix hb hb ((linear_map.id.comp f).comp linear_map.id)) : by rw [linear_map.id_comp, linear_map.comp_id] ... = matrix.trace ΞΉ R R (linear_map.to_matrix hc hb linear_map.id ⬝ linear_map.to_matrix hc hc f ⬝ linear_map.to_matrix hb hc linear_map.id) : by rw [linear_map.to_matrix_comp _ hc, linear_map.to_matrix_comp _ hc] ... = matrix.trace ΞΊ R R (linear_map.to_matrix hc hc f ⬝ linear_map.to_matrix hb hc linear_map.id ⬝ linear_map.to_matrix hc hb linear_map.id) : by rw [matrix.mul_assoc, matrix.trace_mul_comm] ... = matrix.trace ΞΊ R R (linear_map.to_matrix hc hc ((f.comp linear_map.id).comp linear_map.id)) : by rw [linear_map.to_matrix_comp _ hb, linear_map.to_matrix_comp _ hc] ... = matrix.trace ΞΊ R R (linear_map.to_matrix hc hc f) : by rw [linear_map.comp_id, linear_map.comp_id] open_locale classical theorem trace_aux_range (R : Type u) [comm_ring R] {M : Type v} [add_comm_group M] [module R M] {ΞΉ : Type w} [decidable_eq ΞΉ] [fintype ΞΉ] {b : ΞΉ β†’ M} (hb : is_basis R b) : trace_aux R hb.range = trace_aux R hb := linear_map.ext $ Ξ» f, if H : 0 = 1 then eq_of_zero_eq_one H _ _ else begin haveI : nontrivial R := ⟨⟨0, 1, H⟩⟩, change βˆ‘ i : set.range b, _ = βˆ‘ i : ΞΉ, _, simp_rw [matrix.diag_apply], symmetry, convert (equiv.of_injective _ hb.injective).sum_comp _, ext i, exact (linear_map.to_matrix_range hb hb f i i).symm end /-- where `ΞΉ` and `ΞΊ` can reside in different universes -/ theorem trace_aux_eq (R : Type u) [comm_ring R] {M : Type v} [add_comm_group M] [module R M] {ΞΉ : Type*} [decidable_eq ΞΉ] [fintype ΞΉ] {b : ΞΉ β†’ M} (hb : is_basis R b) {ΞΊ : Type*} [decidable_eq ΞΊ] [fintype ΞΊ] {c : ΞΊ β†’ M} (hc : is_basis R c) : trace_aux R hb = trace_aux R hc := calc trace_aux R hb = trace_aux R hb.range : by rw trace_aux_range R hb ... = trace_aux R hc.range : trace_aux_eq' _ _ _ ... = trace_aux R hc : by rw trace_aux_range R hc /-- Trace of an endomorphism independent of basis. -/ def trace (R : Type u) [comm_ring R] (M : Type v) [add_comm_group M] [module R M] : (M β†’β‚—[R] M) β†’β‚—[R] R := if H : βˆƒ s : finset M, is_basis R (Ξ» x, x : (↑s : set M) β†’ M) then trace_aux R (classical.some_spec H) else 0 theorem trace_eq_matrix_trace (R : Type u) [comm_ring R] {M : Type v} [add_comm_group M] [module R M] {ΞΉ : Type w} [fintype ΞΉ] [decidable_eq ΞΉ] {b : ΞΉ β†’ M} (hb : is_basis R b) (f : M β†’β‚—[R] M) : trace R M f = matrix.trace ΞΉ R R (linear_map.to_matrix hb hb f) := have βˆƒ s : finset M, is_basis R (Ξ» x, x : (↑s : set M) β†’ M), from ⟨finset.univ.image b, by { rw [finset.coe_image, finset.coe_univ, set.image_univ], exact hb.range }⟩, by { rw [trace, dif_pos this, ← trace_aux_def], congr' 1, apply trace_aux_eq } theorem trace_mul_comm (R : Type u) [comm_ring R] {M : Type v} [add_comm_group M] [module R M] (f g : M β†’β‚—[R] M) : trace R M (f * g) = trace R M (g * f) := if H : βˆƒ s : finset M, is_basis R (Ξ» x, x : (↑s : set M) β†’ M) then let ⟨s, hb⟩ := H in by { simp_rw [trace_eq_matrix_trace R hb, linear_map.to_matrix_mul], apply matrix.trace_mul_comm } else by rw [trace, dif_neg H, linear_map.zero_apply, linear_map.zero_apply] section finite_dimensional variables {K : Type*} [field K] variables {V : Type*} [add_comm_group V] [vector_space K V] [finite_dimensional K V] variables {W : Type*} [add_comm_group W] [vector_space K W] [finite_dimensional K W] instance : finite_dimensional K (V β†’β‚—[K] W) := begin classical, cases finite_dimensional.exists_is_basis_finset K V with bV hbV, cases finite_dimensional.exists_is_basis_finset K W with bW hbW, apply linear_equiv.finite_dimensional (linear_map.to_matrix hbV hbW).symm, end /-- The dimension of the space of linear transformations is the product of the dimensions of the domain and codomain. -/ @[simp] lemma findim_linear_map : finite_dimensional.findim K (V β†’β‚—[K] W) = (finite_dimensional.findim K V) * (finite_dimensional.findim K W) := begin classical, cases finite_dimensional.exists_is_basis_finset K V with bV hbV, cases finite_dimensional.exists_is_basis_finset K W with bW hbW, rw [linear_equiv.findim_eq (linear_map.to_matrix hbV hbW), matrix.findim_matrix, finite_dimensional.findim_eq_card_basis hbV, finite_dimensional.findim_eq_card_basis hbW, mul_comm], end end finite_dimensional end linear_map /-- The natural equivalence between linear endomorphisms of finite free modules and square matrices is compatible with the algebra structures. -/ def alg_equiv_matrix' {R : Type v} [comm_ring R] {n : Type*} [fintype n] [decidable_eq n] : module.End R (n β†’ R) ≃ₐ[R] matrix n n R := { map_mul' := linear_map.to_matrix'_comp, map_add' := linear_map.to_matrix'.map_add, commutes' := Ξ» r, by { change (r β€’ (linear_map.id : module.End R _)).to_matrix' = r β€’ 1, rw ←linear_map.to_matrix'_id, refl, }, ..linear_map.to_matrix' } /-- A linear equivalence of two modules induces an equivalence of algebras of their endomorphisms. -/ def linear_equiv.alg_conj {R : Type v} [comm_ring R] {M₁ Mβ‚‚ : Type*} [add_comm_group M₁] [module R M₁] [add_comm_group Mβ‚‚] [module R Mβ‚‚] (e : M₁ ≃ₗ[R] Mβ‚‚) : module.End R M₁ ≃ₐ[R] module.End R Mβ‚‚ := { map_mul' := Ξ» f g, by apply e.arrow_congr_comp, map_add' := e.conj.map_add, commutes' := Ξ» r, by { change e.conj (r β€’ linear_map.id) = r β€’ linear_map.id, rw [linear_equiv.map_smul, linear_equiv.conj_id], }, ..e.conj } /-- A basis of a module induces an equivalence of algebras from the endomorphisms of the module to square matrices. -/ def alg_equiv_matrix {R : Type v} {M : Type w} {n : Type*} [fintype n] [comm_ring R] [add_comm_group M] [module R M] [decidable_eq n] {b : n β†’ M} (h : is_basis R b) : module.End R M ≃ₐ[R] matrix n n R := h.equiv_fun.alg_conj.trans alg_equiv_matrix'
3b7adf3b0ce13f18f8acda910edbdabcc2191daf
947b78d97130d56365ae2ec264df196ce769371a
/tests/lean/run/intromacro.lean
d62157ee14f96a37997073e7718da9efbae055c4
[ "Apache-2.0" ]
permissive
shyamalschandra/lean4
27044812be8698f0c79147615b1d5090b9f4b037
6e7a883b21eaf62831e8111b251dc9b18f40e604
refs/heads/master
1,671,417,126,371
1,601,859,995,000
1,601,860,020,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
571
lean
new_frontend structure S := (x y z : Nat := 0) def f1 : Nat Γ— Nat β†’ S β†’ Nat := by { intro (x, y); intro ⟨a, b, c⟩; exact x+y+a } theorem ex1 : f1 (10, 20) { x := 10 } = 40 := rfl def f2 : Nat Γ— Nat β†’ S β†’ Nat := by { intro (a, b) { y := y, .. }; exact a+b+y } #eval f2 (10, 20) { y := 5 } theorem ex2 : f2 (10, 20) { y := 5 } = 35 := rfl def f3 : Nat Γ— Nat β†’ S β†’ S β†’ Nat := by { intro (a, b) { y := y, .. } s; exact a+b+y+s.x } #eval f3 (10, 20) { y := 5 } { x := 1 } theorem ex3 : f3 (10, 20) { y := 5 } { x := 1 } = 36 := rfl
180e43aad29d13f2aa0240db03b827107b41e5fc
3dd1b66af77106badae6edb1c4dea91a146ead30
/tests/lean/run/let1.lean
38cb8b8dffdac511f76be480b728fb9cfc2e8be2
[ "Apache-2.0" ]
permissive
silky/lean
79c20c15c93feef47bb659a2cc139b26f3614642
df8b88dca2f8da1a422cb618cd476ef5be730546
refs/heads/master
1,610,737,587,697
1,406,574,534,000
1,406,574,534,000
22,362,176
1
0
null
null
null
null
UTF-8
Lean
false
false
177
lean
import standard check let f x y := x ∧ y, g x := f x x, a := g true in λ (x : a), let h x y := f x (g y), b := h in b
398914ded65320cb82b95f1180093794af72d331
1fbca480c1574e809ae95a3eda58188ff42a5e41
/src/util/control/monad/state.lean
f0449e5f521f276ee3fb959ae08b2c70e8fc6f4a
[]
no_license
unitb/lean-lib
560eea0acf02b1fd4bcaac9986d3d7f1a4290e7e
439b80e606b4ebe4909a08b1d77f4f5c0ee3dee9
refs/heads/master
1,610,706,025,400
1,570,144,245,000
1,570,144,245,000
99,579,229
5
0
null
null
null
null
UTF-8
Lean
false
false
374
lean
universes u v namespace state_t variables {Οƒ Ξ± : Type u} variables {m : Type u β†’ Type u} variables [monad m] variables [is_lawful_monad m] open is_lawful_monad lemma get_bind (s : Οƒ) (f : Οƒ β†’ state_t Οƒ m Ξ±) : (get >>= f).run s = (f s).run s := by { simp [bind,state_t.bind,get,state_t.get,monad_state.lift,pure_bind,has_pure.pure,bind._match_1] } end state_t
c78b342881c086ca47568e2b13a66bd41ad8ca1b
f3a5af2927397cf346ec0e24312bfff077f00425
/src/game/world10/level4.lean
04e0c686e9a233e44bcb4047003ce5b6501860a6
[ "Apache-2.0" ]
permissive
ImperialCollegeLondon/natural_number_game
05c39e1586408cfb563d1a12e1085a90726ab655
f29b6c2884299fc63fdfc81ae5d7daaa3219f9fd
refs/heads/master
1,688,570,964,990
1,636,908,242,000
1,636,908,242,000
195,403,790
277
84
Apache-2.0
1,694,547,955,000
1,562,328,792,000
Lean
UTF-8
Lean
false
false
294
lean
import game.world10.level3 -- hide namespace mynat -- hide /- # Inequality world. ## Level 4: `zero_le` Another easy one. -/ /- Lemma For all naturals $a$, $0\leq a$. -/ lemma zero_le (a : mynat) : 0 ≀ a := begin [nat_num_game] use a, rw zero_add, refl, end end mynat -- hide
a925e70d835005673fd7332333ef42b7ff5c3ab4
da3a76c514d38801bae19e8a9e496dc31f8e5866
/library/init/meta/vm.lean
e752435a83738fcc6e8b4f99ee63f686adb987cb
[ "Apache-2.0" ]
permissive
cipher1024/lean
270c1ac5781e6aee12f5c8d720d267563a164beb
f5cbdff8932dd30c6dd8eec68f3059393b4f8b3a
refs/heads/master
1,611,223,459,029
1,487,566,573,000
1,487,566,573,000
83,356,543
0
0
null
1,488,229,336,000
1,488,229,336,000
null
UTF-8
Lean
false
false
6,394
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.meta.tactic init.data.option.basic meta constant vm_obj : Type inductive vm_obj_kind | simple | constructor | closure | native_closure | mpz | name | level | expr | declaration | environment | tactic_state | format | options | other namespace vm_obj meta constant kind : vm_obj β†’ vm_obj_kind /- For simple and constructor vm_obj's, it returns the constructor tag/index. Return 0 otherwise. -/ meta constant cidx : vm_obj β†’ nat /- For closure vm_obj's, it returns the internal function index. -/ meta constant fn_idx : vm_obj β†’ nat /- For constructor vm_obj's, it returns the data stored in the object. For closure vm_obj's, it returns the local arguments captured by the closure. -/ meta constant fields : vm_obj β†’ list vm_obj /- For simple and mpz vm_obj's -/ meta constant to_nat : vm_obj β†’ nat /- For name vm_obj's, it returns the name wrapped by the vm_obj. -/ meta constant to_name : vm_obj β†’ name /- For level vm_obj's, it returns the universe level wrapped by the vm_obj. -/ meta constant to_level : vm_obj β†’ level /- For expr vm_obj's, it returns the expression wrapped by the vm_obj. -/ meta constant to_expr : vm_obj β†’ expr /- For declaration vm_obj's, it returns the declaration wrapped by the vm_obj. -/ meta constant to_declaration : vm_obj β†’ declaration /- For environment vm_obj's, it returns the environment wrapped by the vm_obj. -/ meta constant to_environment : vm_obj β†’ environment /- For tactic_state vm_obj's, it returns the tactic_state object wrapped by the vm_obj. -/ meta constant to_tactic_state : vm_obj β†’ tactic_state /- For format vm_obj's, it returns the format object wrapped by the vm_obj. -/ meta constant to_format : vm_obj β†’ format end vm_obj meta constant vm_decl : Type inductive vm_decl_kind | bytecode | builtin | cfun /- Information for local variables and arguments on the VM stack. Remark: type is only available if it is a closed term at compilation time. -/ meta structure vm_local_info := (id : name) (type : option expr) namespace vm_decl meta constant kind : vm_decl β†’ vm_decl_kind meta constant to_name : vm_decl β†’ name /- Internal function index associated with the given VM declaration. -/ meta constant idx : vm_decl β†’ nat /- Number of arguments needed to execute the given VM declaration. -/ meta constant arity : vm_decl β†’ nat /- Return (line, column) if available -/ meta constant pos : vm_decl β†’ option (nat Γ— nat) /- Return .olean file where the given VM declaration was imported from. -/ meta constant olean : vm_decl β†’ option string /- Return names .olean file where the given VM declaration was imported from. -/ meta constant args_info : vm_decl β†’ list vm_local_info end vm_decl meta constant vm_core : Type β†’ Type meta constant vm_core.map {Ξ± Ξ² : Type} : (Ξ± β†’ Ξ²) β†’ vm_core Ξ± β†’ vm_core Ξ² meta constant vm_core.ret {Ξ± : Type} : Ξ± β†’ vm_core Ξ± meta constant vm_core.bind {Ξ± Ξ² : Type} : vm_core Ξ± β†’ (Ξ± β†’ vm_core Ξ²) β†’ vm_core Ξ² meta instance : monad vm_core := {map := @vm_core.map, ret := @vm_core.ret, bind := @vm_core.bind} @[reducible] meta def vm (Ξ± : Type) : Type := option_t vm_core Ξ± namespace vm meta constant get_env : vm environment meta constant get_decl : name β†’ vm vm_decl meta constant get_options : vm options meta constant stack_size : vm nat /- Return the vm_obj stored at the given position on the execution stack. It fails if position >= vm.stack_size -/ meta constant stack_obj : nat β†’ vm vm_obj /- Return (name, type) for the object at the given position on the execution stack. It fails if position >= vm.stack_size. The name is anonymous if vm_obj is a transient value created by the compiler. Type information is only recorded if the type is a closed term at compilation time. -/ meta constant stack_obj_info : nat β†’ vm (name Γ— option expr) /- Pretty print the vm_obj at the given position on the execution stack. -/ meta constant pp_stack_obj : nat β†’ vm format /- Pretty print the given expression. -/ meta constant pp_expr : expr β†’ vm format /- Number of frames on the call stack. -/ meta constant call_stack_size : vm nat /- Return the function name at the given stack frame. Action fails if position >= vm.call_stack_size. -/ meta constant call_stack_fn : nat β†’ vm name /- Return the range [start, end) for the given stack frame. Action fails if position >= vm.call_stack_size. The values start and end correspond to positions at the execution stack. We have that 0 <= start < end <= vm.stack_size -/ meta constant call_stack_var_range : nat β†’ vm (nat Γ— nat) /- Return the name of the function on top of the call stack. -/ meta constant curr_fn : vm name /- Return the base stack pointer for the frame on top of the call stack. -/ meta constant bp : vm nat /- Return the program counter. -/ meta constant pc : vm nat /- Convert the given vm_obj into a string -/ meta constant obj_to_string : vm_obj β†’ vm string meta constant put_str : string β†’ vm unit meta constant get_line : vm string /- Return tt if end of the input stream has been reached. For example, this can happen if the user presses Ctrl-D -/ meta constant eof : vm bool /- Return the list of declarations tagged with the given attribute. -/ meta constant get_attribute : name β†’ vm (list name) meta def trace {Ξ± : Type} [has_to_format Ξ±] (a : Ξ±) : vm unit := do fmt ← return $ to_fmt a, return $ _root_.trace_fmt fmt (Ξ» u, ()) end vm meta structure vm_monitor (s : Type) := (init : s) (step : s β†’ vm s) /- Registers a new virtual machine monitor. The argument must be the name of a definition of type `vm_monitor S`. The command will override the last monitor. If option 'debugger' is true, then the VM will initialize the vm_monitor state using the 'init' field, and will invoke the function 'step' before each instruction is invoked. -/ meta constant vm_monitor.register : name β†’ command
8f73b3abea4848b848f3ec659708ded51fd3ce6b
bb31430994044506fa42fd667e2d556327e18dfe
/src/topology/algebra/order/floor.lean
be5a80187cd7a3eecf444a3bc9c8939999596123
[ "Apache-2.0" ]
permissive
sgouezel/mathlib
0cb4e5335a2ba189fa7af96d83a377f83270e503
00638177efd1b2534fc5269363ebf42a7871df9a
refs/heads/master
1,674,527,483,042
1,673,665,568,000
1,673,665,568,000
119,598,202
0
0
null
1,517,348,647,000
1,517,348,646,000
null
UTF-8
Lean
false
false
9,543
lean
/- Copyright (c) 2020 Anatole Dedecker. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anatole Dedecker -/ import algebra.order.floor import topology.order.basic /-! # Topological facts about `int.floor`, `int.ceil` and `int.fract` This file proves statements about limits and continuity of functions involving `floor`, `ceil` and `fract`. ## Main declarations * `tendsto_floor_at_top`, `tendsto_floor_at_bot`, `tendsto_ceil_at_top`, `tendsto_ceil_at_bot`: `int.floor` and `int.ceil` tend to +-∞ in +-∞. * `continuous_on_floor`: `int.floor` is continuous on `Ico n (n + 1)`, because constant. * `continuous_on_ceil`: `int.ceil` is continuous on `Ioc n (n + 1)`, because constant. * `continuous_on_fract`: `int.fract` is continuous on `Ico n (n + 1)`. * `continuous_on.comp_fract`: Precomposing a continuous function satisfying `f 0 = f 1` with `int.fract` yields another continuous function. -/ open filter function int set open_locale topological_space variables {Ξ± Ξ² Ξ³ : Type*} [linear_ordered_ring Ξ±] [floor_ring Ξ±] lemma tendsto_floor_at_top : tendsto (floor : Ξ± β†’ β„€) at_top at_top := floor_mono.tendsto_at_top_at_top $ Ξ» b, ⟨(b + 1 : β„€), by { rw floor_int_cast, exact (lt_add_one _).le }⟩ lemma tendsto_floor_at_bot : tendsto (floor : Ξ± β†’ β„€) at_bot at_bot := floor_mono.tendsto_at_bot_at_bot $ Ξ» b, ⟨b, (floor_int_cast _).le⟩ lemma tendsto_ceil_at_top : tendsto (ceil : Ξ± β†’ β„€) at_top at_top := ceil_mono.tendsto_at_top_at_top $ Ξ» b, ⟨b, (ceil_int_cast _).ge⟩ lemma tendsto_ceil_at_bot : tendsto (ceil : Ξ± β†’ β„€) at_bot at_bot := ceil_mono.tendsto_at_bot_at_bot $ Ξ» b, ⟨(b - 1 : β„€), by { rw ceil_int_cast, exact (sub_one_lt _).le }⟩ variables [topological_space Ξ±] lemma continuous_on_floor (n : β„€) : continuous_on (Ξ» x, floor x : Ξ± β†’ Ξ±) (Ico n (n+1) : set Ξ±) := (continuous_on_congr $ floor_eq_on_Ico' n).mpr continuous_on_const lemma continuous_on_ceil (n : β„€) : continuous_on (Ξ» x, ceil x : Ξ± β†’ Ξ±) (Ioc (n-1) n : set Ξ±) := (continuous_on_congr $ ceil_eq_on_Ioc' n).mpr continuous_on_const lemma tendsto_floor_right' [order_closed_topology Ξ±] (n : β„€) : tendsto (Ξ» x, floor x : Ξ± β†’ Ξ±) (𝓝[β‰₯] n) (𝓝 n) := begin rw ← nhds_within_Ico_eq_nhds_within_Ici (lt_add_one (n : Ξ±)), simpa only [floor_int_cast] using (continuous_on_floor n _ (left_mem_Ico.mpr $ lt_add_one (_ : Ξ±))).tendsto end lemma tendsto_ceil_left' [order_closed_topology Ξ±] (n : β„€) : tendsto (Ξ» x, ceil x : Ξ± β†’ Ξ±) (𝓝[≀] n) (𝓝 n) := begin rw ← nhds_within_Ioc_eq_nhds_within_Iic (sub_one_lt (n : Ξ±)), simpa only [ceil_int_cast] using (continuous_on_ceil _ _ (right_mem_Ioc.mpr $ sub_one_lt (_ : Ξ±))).tendsto end lemma tendsto_floor_right [order_closed_topology Ξ±] (n : β„€) : tendsto (Ξ» x, floor x : Ξ± β†’ Ξ±) (𝓝[β‰₯] n) (𝓝[β‰₯] n) := tendsto_nhds_within_of_tendsto_nhds_of_eventually_within _ (tendsto_floor_right' _) begin refine (eventually_nhds_within_of_forall $ Ξ» x (hx : (n : Ξ±) ≀ x), _), change _ ≀ _, norm_cast, convert ← floor_mono hx, rw floor_eq_iff, exact ⟨le_rfl, lt_add_one _⟩ end lemma tendsto_ceil_left [order_closed_topology Ξ±] (n : β„€) : tendsto (Ξ» x, ceil x : Ξ± β†’ Ξ±) (𝓝[≀] n) (𝓝[≀] n) := tendsto_nhds_within_of_tendsto_nhds_of_eventually_within _ (tendsto_ceil_left' _) begin refine (eventually_nhds_within_of_forall $ Ξ» x (hx : x ≀ (n : Ξ±)), _), change _ ≀ _, norm_cast, convert ← ceil_mono hx, rw ceil_eq_iff, exact ⟨sub_one_lt _, le_rfl⟩ end lemma tendsto_floor_left [order_closed_topology Ξ±] (n : β„€) : tendsto (Ξ» x, floor x : Ξ± β†’ Ξ±) (𝓝[<] n) (𝓝[≀] (n-1)) := begin rw ← nhds_within_Ico_eq_nhds_within_Iio (sub_one_lt (n : Ξ±)), convert (tendsto_nhds_within_congr $ (Ξ» x hx, (floor_eq_on_Ico' (n-1) x hx).symm)) (tendsto_nhds_within_of_tendsto_nhds_of_eventually_within _ tendsto_const_nhds (eventually_of_forall (Ξ» _, mem_Iic.mpr $ le_rfl))); norm_cast <|> apply_instance, ring end lemma tendsto_ceil_right [order_closed_topology Ξ±] (n : β„€) : tendsto (Ξ» x, ceil x : Ξ± β†’ Ξ±) (𝓝[>] n) (𝓝[β‰₯] (n+1)) := begin rw ← nhds_within_Ioc_eq_nhds_within_Ioi (lt_add_one (n : Ξ±)), convert (tendsto_nhds_within_congr $ (Ξ» x hx, (ceil_eq_on_Ioc' (n+1) x hx).symm)) (tendsto_nhds_within_of_tendsto_nhds_of_eventually_within _ tendsto_const_nhds (eventually_of_forall (Ξ» _, mem_Ici.mpr $ le_rfl))); norm_cast <|> apply_instance, ring end lemma tendsto_floor_left' [order_closed_topology Ξ±] (n : β„€) : tendsto (Ξ» x, floor x : Ξ± β†’ Ξ±) (𝓝[<] n) (𝓝 (n-1)) := begin rw ← nhds_within_univ, exact tendsto_nhds_within_mono_right (subset_univ _) (tendsto_floor_left n), end lemma tendsto_ceil_right' [order_closed_topology Ξ±] (n : β„€) : tendsto (Ξ» x, ceil x : Ξ± β†’ Ξ±) (𝓝[>] n) (𝓝 (n+1)) := begin rw ← nhds_within_univ, exact tendsto_nhds_within_mono_right (subset_univ _) (tendsto_ceil_right n), end lemma continuous_on_fract [topological_add_group Ξ±] (n : β„€) : continuous_on (fract : Ξ± β†’ Ξ±) (Ico n (n+1) : set Ξ±) := continuous_on_id.sub (continuous_on_floor n) lemma tendsto_fract_left' [order_closed_topology Ξ±] [topological_add_group Ξ±] (n : β„€) : tendsto (fract : Ξ± β†’ Ξ±) (𝓝[<] n) (𝓝 1) := begin convert (tendsto_nhds_within_of_tendsto_nhds tendsto_id).sub (tendsto_floor_left' n); [{norm_cast, ring}, apply_instance, apply_instance] end lemma tendsto_fract_left [order_closed_topology Ξ±] [topological_add_group Ξ±] (n : β„€) : tendsto (fract : Ξ± β†’ Ξ±) (𝓝[<] n) (𝓝[<] 1) := tendsto_nhds_within_of_tendsto_nhds_of_eventually_within _ (tendsto_fract_left' _) (eventually_of_forall fract_lt_one) lemma tendsto_fract_right' [order_closed_topology Ξ±] [topological_add_group Ξ±] (n : β„€) : tendsto (fract : Ξ± β†’ Ξ±) (𝓝[β‰₯] n) (𝓝 0) := begin convert (tendsto_nhds_within_of_tendsto_nhds tendsto_id).sub (tendsto_floor_right' n); [exact (sub_self _).symm, apply_instance, apply_instance] end lemma tendsto_fract_right [order_closed_topology Ξ±] [topological_add_group Ξ±] (n : β„€) : tendsto (fract : Ξ± β†’ Ξ±) (𝓝[β‰₯] n) (𝓝[β‰₯] 0) := tendsto_nhds_within_of_tendsto_nhds_of_eventually_within _ (tendsto_fract_right' _) (eventually_of_forall fract_nonneg) local notation `I` := (Icc 0 1 : set Ξ±) variables [order_topology Ξ±] [topological_add_group Ξ±] [topological_space Ξ²] [topological_space Ξ³] /-- Do not use this, use `continuous_on.comp_fract` instead. -/ lemma continuous_on.comp_fract' {f : Ξ² β†’ Ξ± β†’ Ξ³} (h : continuous_on (uncurry f) $ univ Γ—Λ’ I) (hf : βˆ€ s, f s 0 = f s 1) : continuous (Ξ» st : Ξ² Γ— Ξ±, f st.1 $ fract st.2) := begin change continuous ((uncurry f) ∘ (prod.map id (fract))), rw continuous_iff_continuous_at, rintro ⟨s, t⟩, by_cases ht : t = floor t, { rw ht, rw ← continuous_within_at_univ, have : (univ : set (Ξ² Γ— Ξ±)) βŠ† (univ Γ—Λ’ Iio β†‘βŒŠtβŒ‹) βˆͺ (univ Γ—Λ’ Ici β†‘βŒŠtβŒ‹), { rintros p -, rw ← prod_union, exact ⟨trivial, lt_or_le p.2 _⟩ }, refine continuous_within_at.mono _ this, refine continuous_within_at.union _ _, { simp only [continuous_within_at, fract_int_cast, nhds_within_prod_eq, nhds_within_univ, id.def, comp_app, prod.map_mk], have : (uncurry f) (s, 0) = (uncurry f) (s, (1 : Ξ±)), by simp [uncurry, hf], rw this, refine (h _ ⟨⟨⟩, by exact_mod_cast right_mem_Icc.2 (zero_le_one' Ξ±)⟩).tendsto.comp _, rw [nhds_within_prod_eq, nhds_within_univ], rw nhds_within_Icc_eq_nhds_within_Iic (zero_lt_one' Ξ±), exact tendsto_id.prod_map (tendsto_nhds_within_mono_right Iio_subset_Iic_self $ tendsto_fract_left _) }, { simp only [continuous_within_at, fract_int_cast, nhds_within_prod_eq, nhds_within_univ, id.def, comp_app, prod.map_mk], refine (h _ ⟨⟨⟩, by exact_mod_cast left_mem_Icc.2 (zero_le_one' Ξ±)⟩).tendsto.comp _, rw [nhds_within_prod_eq, nhds_within_univ, nhds_within_Icc_eq_nhds_within_Ici (zero_lt_one' Ξ±)], exact tendsto_id.prod_map (tendsto_fract_right _) } }, { have : t ∈ Ioo (floor t : Ξ±) ((floor t : Ξ±) + 1), from ⟨lt_of_le_of_ne (floor_le t) (ne.symm ht), lt_floor_add_one _⟩, apply (h ((prod.map _ fract) _) ⟨trivial, ⟨fract_nonneg _, (fract_lt_one _).le⟩⟩).tendsto.comp, simp only [nhds_prod_eq, nhds_within_prod_eq, nhds_within_univ, id.def, prod.map_mk], exact continuous_at_id.tendsto.prod_map (tendsto_nhds_within_of_tendsto_nhds_of_eventually_within _ (((continuous_on_fract _ _ (Ioo_subset_Ico_self this)).mono Ioo_subset_Ico_self).continuous_at (Ioo_mem_nhds this.1 this.2)) (eventually_of_forall (Ξ» x, ⟨fract_nonneg _, (fract_lt_one _).le⟩)) ) } end lemma continuous_on.comp_fract {s : Ξ² β†’ Ξ±} {f : Ξ² β†’ Ξ± β†’ Ξ³} (h : continuous_on (uncurry f) $ univ Γ—Λ’ Icc 0 1) (hs : continuous s) (hf : βˆ€ s, f s 0 = f s 1) : continuous (Ξ» x : Ξ², f x $ int.fract (s x)) := (h.comp_fract' hf).comp (continuous_id.prod_mk hs) /-- A special case of `continuous_on.comp_fract`. -/ lemma continuous_on.comp_fract'' {f : Ξ± β†’ Ξ²} (h : continuous_on f I) (hf : f 0 = f 1) : continuous (f ∘ fract) := continuous_on.comp_fract (h.comp continuous_on_snd $ Ξ» x hx, (mem_prod.mp hx).2) continuous_id (Ξ» _, hf)
218e771b921761af8438910de79e6b7657fb6eeb
63abd62053d479eae5abf4951554e1064a4c45b4
/src/ring_theory/valuation/basic.lean
6d4bc91c8e0d14cca52390033c2c73c44651be2d
[ "Apache-2.0" ]
permissive
Lix0120/mathlib
0020745240315ed0e517cbf32e738d8f9811dd80
e14c37827456fc6707f31b4d1d16f1f3a3205e91
refs/heads/master
1,673,102,855,024
1,604,151,044,000
1,604,151,044,000
308,930,245
0
0
Apache-2.0
1,604,164,710,000
1,604,163,547,000
null
UTF-8
Lean
false
false
15,590
lean
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin Buzzard, Johan Commelin, Patrick Massot -/ import algebra.linear_ordered_comm_group_with_zero import algebra.group_power import ring_theory.ideal.operations import algebra.punit_instances /-! # The basics of valuation theory. The basic theory of valuations (non-archimedean norms) on a commutative ring, following T. Wedhorn's unpublished notes β€œAdic Spaces” ([wedhorn_adic]). The definition of a valuation we use here is Definition 1.22 of [wedhorn_adic]. A valuation on a ring `R` is a monoid homomorphism `v` to a linearly ordered commutative group with zero, that in addition satisfies the following two axioms: * `v 0 = 0` * `βˆ€ x y, v (x + y) ≀ max (v x) (v y)` `valuation R Ξ“β‚€`is the type of valuations `R β†’ Ξ“β‚€`, with a coercion to the underlying function. If `v` is a valuation from `R` to `Ξ“β‚€` then the induced group homomorphism `units(R) β†’ Ξ“β‚€` is called `unit_map v`. The equivalence "relation" `is_equiv v₁ vβ‚‚ : Prop` defined in 1.27 of [wedhorn_adic] is not strictly speaking a relation, because `v₁ : valuation R Γ₁` and `vβ‚‚ : valuation R Ξ“β‚‚` might not have the same type. This corresponds in ZFC to the set-theoretic difficulty that the class of all valuations (as `Ξ“β‚€` varies) on a ring `R` is not a set. The "relation" is however reflexive, symmetric and transitive in the obvious sense. Note that we use 1.27(iii) of [wedhorn_adic] as the definition of equivalence. The support of a valuation `v : valuation R Ξ“β‚€` is `supp v`. If `J` is an ideal of `R` with `h : J βŠ† supp v` then the induced valuation on R / J = `ideal.quotient J` is `on_quot v h`. ## Main definitions * `valuation R Ξ“β‚€`, the type of valuations on `R` with values in `Ξ“β‚€` * `valuation.is_equiv`, the heterogeneous equivalence relation on valuations * `valuation.supp`, the support of a valuation -/ open_locale classical big_operators noncomputable theory open function ideal -- universes u uβ‚€ u₁ uβ‚‚ -- v is used for valuations variables {R : Type*} -- This will be a ring, assumed commutative in some sections variables {Ξ“β‚€ : Type*} [linear_ordered_comm_group_with_zero Ξ“β‚€] variables {Ξ“'β‚€ : Type*} [linear_ordered_comm_group_with_zero Ξ“'β‚€] variables {Ξ“''β‚€ : Type*} [linear_ordered_comm_group_with_zero Ξ“''β‚€] set_option old_structure_cmd true section variables (R) (Ξ“β‚€) [ring R] /-- The type of Ξ“β‚€-valued valuations on R. -/ @[nolint has_inhabited_instance] structure valuation extends R β†’* Ξ“β‚€ := (map_zero' : to_fun 0 = 0) (map_add' : βˆ€ x y, to_fun (x + y) ≀ max (to_fun x) (to_fun y)) run_cmd tactic.add_doc_string `valuation.to_monoid_hom "The `monoid_hom` underlying a valuation." end namespace valuation section basic variables (R) (Ξ“β‚€) [ring R] /-- A valuation is coerced to the underlying function R β†’ Ξ“β‚€. -/ instance : has_coe_to_fun (valuation R Ξ“β‚€) := { F := Ξ» _, R β†’ Ξ“β‚€, coe := valuation.to_fun } /-- A valuation is coerced to a monoid morphism R β†’ Ξ“β‚€. -/ instance : has_coe (valuation R Ξ“β‚€) (R β†’* Ξ“β‚€) := ⟨valuation.to_monoid_hom⟩ variables {R} {Ξ“β‚€} (v : valuation R Ξ“β‚€) {x y z : R} @[simp, norm_cast] lemma coe_coe : ((v : R β†’* Ξ“β‚€) : R β†’ Ξ“β‚€) = v := rfl @[simp] lemma map_zero : v 0 = 0 := v.map_zero' @[simp] lemma map_one : v 1 = 1 := v.map_one' @[simp] lemma map_mul : βˆ€ x y, v (x * y) = v x * v y := v.map_mul' @[simp] lemma map_add : βˆ€ x y, v (x + y) ≀ max (v x) (v y) := v.map_add' lemma map_add_le {x y g} (hx : v x ≀ g) (hy : v y ≀ g) : v (x + y) ≀ g := le_trans (v.map_add x y) $ max_le hx hy lemma map_add_lt {x y g} (hx : v x < g) (hy : v y < g) : v (x + y) < g := lt_of_le_of_lt (v.map_add x y) $ max_lt hx hy lemma map_sum_le {ΞΉ : Type*} {s : finset ΞΉ} {f : ΞΉ β†’ R} {g : Ξ“β‚€} (hf : βˆ€ i ∈ s, v (f i) ≀ g) : v (βˆ‘ i in s, f i) ≀ g := begin refine finset.induction_on s (Ξ» _, trans_rel_right (≀) v.map_zero zero_le') (Ξ» a s has ih hf, _) hf, rw finset.forall_mem_insert at hf, rw finset.sum_insert has, exact v.map_add_le hf.1 (ih hf.2) end lemma map_sum_lt {ΞΉ : Type*} {s : finset ΞΉ} {f : ΞΉ β†’ R} {g : Ξ“β‚€} (hg : g β‰  0) (hf : βˆ€ i ∈ s, v (f i) < g) : v (βˆ‘ i in s, f i) < g := begin refine finset.induction_on s (Ξ» _, trans_rel_right (<) v.map_zero (zero_lt_iff.2 hg)) (Ξ» a s has ih hf, _) hf, rw finset.forall_mem_insert at hf, rw finset.sum_insert has, exact v.map_add_lt hf.1 (ih hf.2) end lemma map_sum_lt' {ΞΉ : Type*} {s : finset ΞΉ} {f : ΞΉ β†’ R} {g : Ξ“β‚€} (hg : 0 < g) (hf : βˆ€ i ∈ s, v (f i) < g) : v (βˆ‘ i in s, f i) < g := v.map_sum_lt (ne_of_gt hg) hf @[simp] lemma map_pow : βˆ€ x (n:β„•), v (x^n) = (v x)^n := v.to_monoid_hom.map_pow @[ext] lemma ext {v₁ vβ‚‚ : valuation R Ξ“β‚€} (h : βˆ€ r, v₁ r = vβ‚‚ r) : v₁ = vβ‚‚ := by { cases v₁, cases vβ‚‚, congr, funext r, exact h r } lemma ext_iff {v₁ vβ‚‚ : valuation R Ξ“β‚€} : v₁ = vβ‚‚ ↔ βˆ€ r, v₁ r = vβ‚‚ r := ⟨λ h r, congr_arg _ h, ext⟩ -- The following definition is not an instance, because we have more than one `v` on a given `R`. -- In addition, type class inference would not be able to infer `v`. /-- A valuation gives a preorder on the underlying ring. -/ def to_preorder : preorder R := preorder.lift v /-- If `v` is a valuation on a division ring then `v(x) = 0` iff `x = 0`. -/ @[simp] lemma zero_iff {K : Type*} [division_ring K] (v : valuation K Ξ“β‚€) {x : K} : v x = 0 ↔ x = 0 := begin split ; intro h, { contrapose! h, exact ((is_unit.mk0 _ h).map (v : K β†’* Ξ“β‚€)).ne_zero }, { exact h.symm β–Έ v.map_zero }, end lemma ne_zero_iff {K : Type*} [division_ring K] (v : valuation K Ξ“β‚€) {x : K} : v x β‰  0 ↔ x β‰  0 := not_iff_not_of_iff v.zero_iff @[simp] lemma map_inv {K : Type*} [division_ring K] (v : valuation K Ξ“β‚€) {x : K} : v x⁻¹ = (v x)⁻¹ := begin by_cases h : x = 0, { subst h, rw [inv_zero, v.map_zero, inv_zero] }, { apply eq_inv_of_mul_right_eq_one, rw [← v.map_mul, mul_inv_cancel h, v.map_one] } end lemma map_units_inv (x : units R) : v (x⁻¹ : units R) = (v x)⁻¹ := eq_inv_of_mul_right_eq_one $ by rw [← v.map_mul, units.mul_inv, v.map_one] @[simp] theorem unit_map_eq (u : units R) : (units.map (v : R β†’* Ξ“β‚€) u : Ξ“β‚€) = v u := rfl theorem map_neg_one : v (-1) = 1 := begin apply eq_one_of_pow_eq_one (nat.succ_ne_zero 1) (_ : _ ^ 2 = _), rw [pow_two, ← v.map_mul, neg_one_mul, neg_neg, v.map_one], end @[simp] lemma map_neg (x : R) : v (-x) = v x := calc v (-x) = v (-1 * x) : by rw [neg_one_mul] ... = v (-1) * v x : map_mul _ _ _ ... = v x : by rw [v.map_neg_one, one_mul] lemma map_sub_swap (x y : R) : v (x - y) = v (y - x) := calc v (x - y) = v (-(y - x)) : by rw show x - y = -(y-x), by abel ... = _ : map_neg _ _ lemma map_sub_le_max (x y : R) : v (x - y) ≀ max (v x) (v y) := calc v (x - y) = v (x + -y) : by rw [sub_eq_add_neg] ... ≀ max (v x) (v $ -y) : v.map_add _ _ ... = max (v x) (v y) : by rw map_neg lemma map_add_of_distinct_val (h : v x β‰  v y) : v (x + y) = max (v x) (v y) := begin suffices : Β¬v (x + y) < max (v x) (v y), from or_iff_not_imp_right.1 (le_iff_eq_or_lt.1 (v.map_add x y)) this, intro h', wlog vyx : v y < v x using x y, { apply lt_or_gt_of_ne h.symm }, { rw max_eq_left_of_lt vyx at h', apply lt_irrefl (v x), calc v x = v ((x+y) - y) : by simp ... ≀ max (v $ x + y) (v y) : map_sub_le_max _ _ _ ... < v x : max_lt h' vyx }, { apply this h.symm, rwa [add_comm, max_comm] at h' } end lemma map_eq_of_sub_lt (h : v (y - x) < v x) : v y = v x := begin have := valuation.map_add_of_distinct_val v (ne_of_gt h).symm, rw max_eq_right (le_of_lt h) at this, simpa using this end /-- A ring homomorphism S β†’ R induces a map valuation R Ξ“β‚€ β†’ valuation S Ξ“β‚€ -/ def comap {S : Type*} [ring S] (f : S β†’+* R) (v : valuation R Ξ“β‚€) : valuation S Ξ“β‚€ := by refine_struct { to_fun := v ∘ f, .. }; intros; simp only [comp_app, map_one, map_mul, map_zero, map_add, f.map_one, f.map_mul, f.map_zero, f.map_add] @[simp] lemma comap_id : v.comap (ring_hom.id R) = v := ext $ Ξ» r, rfl lemma comap_comp {S₁ : Type*} {Sβ‚‚ : Type*} [ring S₁] [ring Sβ‚‚] (f : S₁ β†’+* Sβ‚‚) (g : Sβ‚‚ β†’+* R) : v.comap (g.comp f) = (v.comap g).comap f := ext $ Ξ» r, rfl /-- A ≀-preserving group homomorphism Ξ“β‚€ β†’ Ξ“'β‚€ induces a map valuation R Ξ“β‚€ β†’ valuation R Ξ“'β‚€. -/ def map (f : Ξ“β‚€ β†’* Ξ“'β‚€) (hβ‚€ : f 0 = 0) (hf : monotone f) (v : valuation R Ξ“β‚€) : valuation R Ξ“'β‚€ := { to_fun := f ∘ v, map_zero' := show f (v 0) = 0, by rw [v.map_zero, hβ‚€], map_add' := Ξ» r s, calc f (v (r + s)) ≀ f (max (v r) (v s)) : hf (v.map_add r s) ... = max (f (v r)) (f (v s)) : hf.map_max, .. monoid_hom.comp f (v : R β†’* Ξ“β‚€) } /-- Two valuations on R are defined to be equivalent if they induce the same preorder on R. -/ def is_equiv (v₁ : valuation R Ξ“β‚€) (vβ‚‚ : valuation R Ξ“'β‚€) : Prop := βˆ€ r s, v₁ r ≀ v₁ s ↔ vβ‚‚ r ≀ vβ‚‚ s end basic -- end of section namespace is_equiv variables [ring R] variables {v : valuation R Ξ“β‚€} variables {v₁ : valuation R Ξ“β‚€} {vβ‚‚ : valuation R Ξ“'β‚€} {v₃ : valuation R Ξ“''β‚€} @[refl] lemma refl : v.is_equiv v := Ξ» _ _, iff.refl _ @[symm] lemma symm (h : v₁.is_equiv vβ‚‚) : vβ‚‚.is_equiv v₁ := Ξ» _ _, iff.symm (h _ _) @[trans] lemma trans (h₁₂ : v₁.is_equiv vβ‚‚) (h₂₃ : vβ‚‚.is_equiv v₃) : v₁.is_equiv v₃ := Ξ» _ _, iff.trans (h₁₂ _ _) (h₂₃ _ _) lemma of_eq {v' : valuation R Ξ“β‚€} (h : v = v') : v.is_equiv v' := by { subst h } lemma map {v' : valuation R Ξ“β‚€} (f : Ξ“β‚€ β†’* Ξ“'β‚€) (hβ‚€ : f 0 = 0) (hf : monotone f) (inf : injective f) (h : v.is_equiv v') : (v.map f hβ‚€ hf).is_equiv (v'.map f hβ‚€ hf) := let H : strict_mono f := strict_mono_of_monotone_of_injective hf inf in Ξ» r s, calc f (v r) ≀ f (v s) ↔ v r ≀ v s : by rw H.le_iff_le ... ↔ v' r ≀ v' s : h r s ... ↔ f (v' r) ≀ f (v' s) : by rw H.le_iff_le /-- `comap` preserves equivalence. -/ lemma comap {S : Type*} [ring S] (f : S β†’+* R) (h : v₁.is_equiv vβ‚‚) : (v₁.comap f).is_equiv (vβ‚‚.comap f) := Ξ» r s, h (f r) (f s) lemma val_eq (h : v₁.is_equiv vβ‚‚) {r s : R} : v₁ r = v₁ s ↔ vβ‚‚ r = vβ‚‚ s := by simpa only [le_antisymm_iff] using and_congr (h r s) (h s r) lemma ne_zero (h : v₁.is_equiv vβ‚‚) {r : R} : v₁ r β‰  0 ↔ vβ‚‚ r β‰  0 := begin have : v₁ r β‰  v₁ 0 ↔ vβ‚‚ r β‰  vβ‚‚ 0 := not_iff_not_of_iff h.val_eq, rwa [v₁.map_zero, vβ‚‚.map_zero] at this, end end is_equiv -- end of namespace lemma is_equiv_of_map_strict_mono [ring R] {v : valuation R Ξ“β‚€} (f : Ξ“β‚€ β†’* Ξ“'β‚€) (hβ‚€ : f 0 = 0) (H : strict_mono f) : is_equiv (v.map f hβ‚€ (H.monotone)) v := Ξ» x y, ⟨H.le_iff_le.mp, Ξ» h, H.monotone h⟩ lemma is_equiv_of_val_le_one {K : Type*} [division_ring K] (v : valuation K Ξ“β‚€) (v' : valuation K Ξ“'β‚€) (h : βˆ€ {x:K}, v x ≀ 1 ↔ v' x ≀ 1) : v.is_equiv v' := begin intros x y, by_cases hy : y = 0, { simp [hy, zero_iff], }, rw show y = 1 * y, by rw one_mul, rw [← (inv_mul_cancel_right' hy x)], iterate 2 {rw [v.map_mul _ y, v'.map_mul _ y]}, rw [v.map_one, v'.map_one], split; intro H, { apply mul_le_mul_right', replace hy := v.ne_zero_iff.mpr hy, replace H := le_of_le_mul_right hy H, rwa h at H, }, { apply mul_le_mul_right', replace hy := v'.ne_zero_iff.mpr hy, replace H := le_of_le_mul_right hy H, rwa h, }, end section supp variables [comm_ring R] variables (v : valuation R Ξ“β‚€) /-- The support of a valuation `v : R β†’ Ξ“β‚€` is the ideal of `R` where `v` vanishes. -/ def supp : ideal R := { carrier := {x | v x = 0}, zero_mem' := map_zero v, add_mem' := Ξ» x y hx hy, le_zero_iff.mp $ calc v (x + y) ≀ max (v x) (v y) : v.map_add x y ... ≀ 0 : max_le (le_zero_iff.mpr hx) (le_zero_iff.mpr hy), smul_mem' := Ξ» c x hx, calc v (c * x) = v c * v x : map_mul v c x ... = v c * 0 : congr_arg _ hx ... = 0 : mul_zero _ } @[simp] lemma mem_supp_iff (x : R) : x ∈ supp v ↔ v x = 0 := iff.rfl -- @[simp] lemma mem_supp_iff' (x : R) : x ∈ (supp v : set R) ↔ v x = 0 := iff.rfl /-- The support of a valuation is a prime ideal. -/ instance : ideal.is_prime (supp v) := ⟨λ (h : v.supp = ⊀), one_ne_zero $ show (1 : Ξ“β‚€) = 0, from calc 1 = v 1 : v.map_one.symm ... = 0 : show (1:R) ∈ supp v, by { rw h, trivial }, Ξ» x y hxy, begin show v x = 0 ∨ v y = 0, change v (x * y) = 0 at hxy, rw [v.map_mul x y] at hxy, exact eq_zero_or_eq_zero_of_mul_eq_zero hxy end⟩ lemma map_add_supp (a : R) {s : R} (h : s ∈ supp v) : v (a + s) = v a := begin have aux : βˆ€ a s, v s = 0 β†’ v (a + s) ≀ v a, { intros a' s' h', refine le_trans (v.map_add a' s') (max_le (le_refl _) _), simp [h'], }, apply le_antisymm (aux a s h), calc v a = v (a + s + -s) : by simp ... ≀ v (a + s) : aux (a + s) (-s) (by rwa ←ideal.neg_mem_iff at h) end /-- If `hJ : J βŠ† supp v` then `on_quot_val hJ` is the induced function on R/J as a function. Note: it's just the function; the valuation is `on_quot hJ`. -/ def on_quot_val {J : ideal R} (hJ : J ≀ supp v) : J.quotient β†’ Ξ“β‚€ := Ξ» q, quotient.lift_on' q v $ Ξ» a b h, calc v a = v (b + (a - b)) : by simp ... = v b : v.map_add_supp b (hJ h) /-- The extension of valuation v on R to valuation on R/J if J βŠ† supp v -/ def on_quot {J : ideal R} (hJ : J ≀ supp v) : valuation J.quotient Ξ“β‚€ := { to_fun := v.on_quot_val hJ, map_zero' := v.map_zero, map_one' := v.map_one, map_mul' := Ξ» xbar ybar, quotient.indβ‚‚' v.map_mul xbar ybar, map_add' := Ξ» xbar ybar, quotient.indβ‚‚' v.map_add xbar ybar } @[simp] lemma on_quot_comap_eq {J : ideal R} (hJ : J ≀ supp v) : (v.on_quot hJ).comap (ideal.quotient.mk J) = v := ext $ Ξ» r, begin refine @quotient.lift_on_beta _ _ (J.quotient_rel) v (Ξ» a b h, _) _, calc v a = v (b + (a - b)) : by simp ... = v b : v.map_add_supp b (hJ h) end lemma comap_supp {S : Type*} [comm_ring S] (f : S β†’+* R) : supp (v.comap f) = ideal.comap f v.supp := ideal.ext $ Ξ» x, begin rw [mem_supp_iff, ideal.mem_comap, mem_supp_iff], refl, end lemma self_le_supp_comap (J : ideal R) (v : valuation (quotient J) Ξ“β‚€) : J ≀ (v.comap (ideal.quotient.mk J)).supp := by { rw [comap_supp, ← ideal.map_le_iff_le_comap], simp } @[simp] lemma comap_on_quot_eq (J : ideal R) (v : valuation J.quotient Ξ“β‚€) : (v.comap (ideal.quotient.mk J)).on_quot (v.self_le_supp_comap J) = v := ext $ by { rintro ⟨x⟩, refl } /-- The quotient valuation on R/J has support supp(v)/J if J βŠ† supp v. -/ lemma supp_quot {J : ideal R} (hJ : J ≀ supp v) : supp (v.on_quot hJ) = (supp v).map (ideal.quotient.mk J) := begin apply le_antisymm, { rintro ⟨x⟩ hx, apply ideal.subset_span, exact ⟨x, hx, rfl⟩ }, { rw ideal.map_le_iff_le_comap, intros x hx, exact hx } end lemma supp_quot_supp : supp (v.on_quot (le_refl _)) = 0 := by { rw supp_quot, exact ideal.map_quotient_self _ } end supp -- end of section end valuation
a068e3c98a399bf631942423bc0ff220dfef520f
624f6f2ae8b3b1adc5f8f67a365c51d5126be45a
/stage0/src/Init/Lean/Elab/StrategyAttrs.lean
12709cf1d9f38069f5fbe0906bd03930033d4b35
[ "Apache-2.0" ]
permissive
mhuisi/lean4
28d35a4febc2e251c7f05492e13f3b05d6f9b7af
dda44bc47f3e5d024508060dac2bcb59fd12e4c0
refs/heads/master
1,621,225,489,283
1,585,142,689,000
1,585,142,689,000
250,590,438
0
2
Apache-2.0
1,602,443,220,000
1,585,327,814,000
C
UTF-8
Lean
false
false
1,649
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import Init.Lean.Attributes namespace Lean /- Elaborator strategies available in the Lean3 elaborator. We want to support a more general approach, but we need to provide the strategy selection attributes while we rely on the Lean3 elaborator. -/ inductive ElaboratorStrategy | simple | withExpectedType | asEliminator instance ElaboratorStrategy.inhabited : Inhabited ElaboratorStrategy := ⟨ElaboratorStrategy.withExpectedType⟩ def mkElaboratorStrategyAttrs : IO (EnumAttributes ElaboratorStrategy) := registerEnumAttributes `elaboratorStrategy [(`elabWithExpectedType, "instructs elaborator that the arguments of the function application (f ...) should be elaborated using information about the expected type", ElaboratorStrategy.withExpectedType), (`elabSimple, "instructs elaborator that the arguments of the function application (f ...) should be elaborated from left to right, and without propagating information from the expected type to its arguments", ElaboratorStrategy.simple), (`elabAsEliminator, "instructs elaborator that the arguments of the function application (f ...) should be elaborated as f were an eliminator", ElaboratorStrategy.asEliminator)] @[init mkElaboratorStrategyAttrs] constant elaboratorStrategyAttrs : EnumAttributes ElaboratorStrategy := arbitrary _ @[export lean_get_elaborator_strategy] def getElaboratorStrategy (env : Environment) (n : Name) : Option ElaboratorStrategy := elaboratorStrategyAttrs.getValue env n end Lean
05c4786c150eae4a7e2ccae8f3ea89bb16422998
9dd3f3912f7321eb58ee9aa8f21778ad6221f87c
/tests/lean/run/e4.lean
bba6aba47bffe57f4a230012327da86484cdfd38
[ "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
665
lean
prelude definition Prop := Sort.{0} definition false : Prop := βˆ€x : Prop, x check false theorem false.elim (C : Prop) (H : false) : C := H C definition Eq {A : Type} (a b : A) := βˆ€ P : A β†’ Prop, P a β†’ P b check Eq infix `=`:50 := Eq theorem refl {A : Type} (a : A) : a = a := Ξ» P H, H definition true : Prop := false = false theorem trivial : true := refl false attribute [elab_as_eliminator] theorem subst {A : Type} {P : A -> Prop} {a b : A} (H1 : a = b) (H2 : P a) : P b := H1 _ H2 theorem symm {A : Type} {a b : A} (H : a = b) : b = a := subst H (refl a) theorem trans {A : Type} {a b c : A} (H1 : a = b) (H2 : b = c) : a = c := subst H2 H1
a1b50cc5d09b66ab9bfcb2f2bad04b108c58e09b
c45b34bfd44d8607a2e8762c926e3cfaa7436201
/uexp/src/uexp/rules/pushJoinThroughUnionOnRight.lean
ef36d7190af50de50a93c1329022c00fcc4d4616
[ "BSD-2-Clause" ]
permissive
Shamrock-Frost/Cosette
b477c442c07e45082348a145f19ebb35a7f29392
24cbc4adebf627f13f5eac878f04ffa20d1209af
refs/heads/master
1,619,721,304,969
1,526,082,841,000
1,526,082,841,000
121,695,605
1
0
null
1,518,737,210,000
1,518,737,210,000
null
UTF-8
Lean
false
false
1,804
lean
import ..sql import ..tactics import ..u_semiring import ..extra_constants import ..UDP set_option profiler true open Expr open Proj open Pred open SQL open tree notation `int` := datatypes.int definition 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 (rightβ‹…leftβ‹…emp_sal) FROM1 (product (table rel_emp) (((SELECT * FROM1 (table rel_emp) )) UNION ALL ((SELECT * FROM1 (table rel_emp) )))) ) : SQL Ξ“ _) = denoteSQL ((SELECT1 (rightβ‹…rightβ‹…emp_sal) FROM1 (((SELECT * FROM1 (product (table rel_emp) (table rel_emp)) )) UNION ALL ((SELECT * FROM1 (product (table rel_emp) (table rel_emp)) ))) ) : SQL Ξ“ _) := begin intros, unfold_all_denotations, funext, simp, UDP, end
aefd61c1cef32b14a165d4caa3886042c59e44af
3af272061d36e7f3f0521cceaa3a847ed4e03af9
/src/hecke_operator.lean
f983bcf9e89e0146d3e448db5516119a5e85612f
[]
no_license
semorrison/kbb
fdab0929d21dca880d835081814225a95f946187
229bd06e840bc7a7438b8fee6802a4f8024419e3
refs/heads/master
1,585,351,834,355
1,539,848,241,000
1,539,848,241,000
147,323,315
2
1
null
null
null
null
UTF-8
Lean
false
false
3,071
lean
import .SL2Z_generators import .modular_forms local notation `ℍ` := upper_half_space local notation `Mat` := integral_matrices_with_determinant lemma pos_det' {m : β„€} (h : m > 0) {A : Mat m} : ↑(A.a) * ↑(A.d) - ↑(A.b) * ↑(A.c) > (0 : ℝ) := begin cases A with a b c d det, rw ←det at h, rw [←int.cast_mul, ←int.cast_mul, ←int.cast_sub], suffices : (0 : ℝ) < ↑(a * d - b * c), exact this, rwa int.cast_pos end lemma aux_10 {m : β„€} (h : m > 0) (A : Mat m) (z : ℍ) : (↑(A.c) * ↑z + ↑(A.d)) β‰  (0 : β„‚) := have H1 : (↑(A.a) : ℝ) * ↑(A.d) - ↑(A.b) * ↑(A.c) > 0, by rw [← int.cast_mul, ← int.cast_mul, ← int.cast_sub]; from int.cast_pos.2 (trans_rel_left _ h A.det.symm), have _ := preserve_ℍ.aux H1 z.2, by simpa only [complex.of_real_int_cast] theorem M_trans_SL2Z_M {m : β„€} {h : m > 0} {M : SL2Z} {A : Mat m} : M_trans h (SL2Z_M m M A) = SL2Z_H M ∘ (M_trans h A) := begin funext z, apply subtype.eq, change _/_=_/_, have H3 := aux_10 h A z, have H4 := aux_10 zero_lt_one M (M_trans h A z), unfold M_trans Β«MΓΆbius_transformΒ» at H4 ⊒, simp only [subtype.coe_mk, complex.of_real_int_cast] with SL2Z at H3 H4 ⊒, rw ← mul_div_mul_right _ H4 H3, conv { to_rhs, rw [add_mul, add_mul, mul_assoc, mul_assoc], rw [div_mul_cancel _ H3] }, simp only [int.cast_add, int.cast_mul], congr' 1; simp only [add_mul, mul_add, mul_assoc, add_left_comm, add_assoc] end set_option eqn_compiler.zeta true noncomputable def Hecke_operator {k : β„•} (m : β„€) (h : m > 0) (f : is_Petersson_weight_ k) : is_Petersson_weight_ k := begin let orbits := quotient (action_rel (SL2Z_M m)), letI h_orbits : fintype orbits := SL2Z_M.finiteness m (ne_of_gt h), refine ⟨λz:ℍ, (m^k : β„‚) * (finset.univ : finset orbits).sum (Ξ»o, quotient.lift_on' o _ _), _⟩, refine Ξ»A, 1 / (A.c * z + A.d)^k * f.1 (M_trans h A z), { rcases f with ⟨f, weight_f⟩, rintros A B ⟨M, H⟩, change 1 / (↑(A.c) * ↑z + ↑(A.d)) ^ k * f (M_trans h A z) = 1 / (↑(B.c) * ↑z + ↑(B.d)) ^ k * f (M_trans h B z), rw ← H, rw M_trans_SL2Z_M, simp [-one_div_eq_inv], rw (weight_f M _), rw ← mul_assoc, congr' 1, dsimp[M_trans, Β«MΓΆbius_transformΒ»], ring, rw pow_inv, rw pow_inv, rw ← mul_pow, congr' 1, -- Patrick claims this goal is true -- and so does Johan repeat {sorry} }, { dsimp [is_Petersson_weight_], intros M z, conv { to_rhs, rw ← mul_assoc, congr, rw mul_comm }, conv { to_rhs, rw mul_assoc, rw finset.mul_sum, }, congr' 1, refine finset.sum_bij _ _ _ _ _, -- Define the function given by right multiplication with M -- The other goals should be straightforward -- funext o, -- rcases o with ⟨A⟩, -- dsimp [quotient.lift_on',quotient.lift_on,quot.lift_on], -- rcases f with ⟨f, weight_f⟩, -- dsimp [is_Petersson_weight_] at weight_f, -- simp, -- dsimp [SL2Z_H,M_trans,Β«MΓΆbius_transformΒ»], -- simp, sorry } end
7792c70ac2e8129aca98a5c4336b63ea237a9348
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/match_pattern2.lean
f0e3c6cee3e74b751f3694683d6f1a7f593b67f4
[ "Apache-2.0" ]
permissive
leanprover-community/lean
12b87f69d92e614daea8bcc9d4de9a9ace089d0e
cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0
refs/heads/master
1,687,508,156,644
1,684,951,104,000
1,684,951,104,000
169,960,991
457
107
Apache-2.0
1,686,744,372,000
1,549,790,268,000
C++
UTF-8
Lean
false
false
1,423
lean
open tactic list expr private meta definition pattern_telescope : expr β†’ list expr β†’ tactic (list expr Γ— expr Γ— expr) | e ps := if expr.is_pi e = tt then do n ← mk_fresh_name, p ← return $ local_const n (binding_name e) (binding_info e) (binding_domain e), new_e ← return $ instantiate_var (binding_body e) p, pattern_telescope new_e (p :: ps) else do (lhs, rhs) ← match_eq e, return (reverse ps, lhs, rhs) meta definition mk_pattern_for_constant : name β†’ tactic pattern | n := do env ← get_env, d : declaration ← returnex $ environment.get env n, ls : list level ← return $ map level.param (declaration.univ_params d), (some type) ← return $ declaration.instantiate_type_univ_params d ls | failed, (es, lhs, rhs) ← pattern_telescope type [], p : pattern ← mk_pattern ls es lhs [] [rhs, app_of_list (expr.const n ls) es], return p open nat constant add.comm (a b : nat) : a + b = b + a example (a b : nat) (H : a + b + a + b = 0) : true := by do a ← get_local `a, b ← get_local `b, H ← get_local `H >>= infer_type, (lhs, rhs) ← match_eq H, p ← mk_pattern_for_constant $ `add.comm, (_, [rhs_inst, prf]) ← match_pattern p lhs | failed, trace "match rhs", trace rhs_inst, trace "proof lhs = rhs", trace prf, prf_type ← infer_type prf, trace "proof type:", trace prf_type, constructor
6b2983a4cc1e0e79660a4a9ba7136eaa43c18efc
a4673261e60b025e2c8c825dfa4ab9108246c32e
/stage0/src/Lean/Elab/Tactic/Match.lean
2d856a4b6e8350ec01dfb6d12d4e6ff3e7eb50b0
[ "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
2,170
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Elab.Match import Lean.Elab.Tactic.Basic import Lean.Elab.Tactic.Induction namespace Lean.Elab.Tactic structure AuxMatchTermState := (nextIdx : Nat := 1) (cases : Array Syntax := #[]) private def mkAuxiliaryMatchTermAux (parentTag : Name) (matchTac : Syntax) : StateT AuxMatchTermState MacroM Syntax := do let matchAlts := matchTac[4] let alts := matchAlts[1].getArgs let newAlts ← alts.mapSepElemsM fun alt => do let alt := alt.setKind `Lean.Parser.Term.matchAlt let holeOrTacticSeq := alt[2] if holeOrTacticSeq.isOfKind `Lean.Parser.Term.syntheticHole then pure alt else if holeOrTacticSeq.isOfKind `Lean.Parser.Term.hole then let s ← get let tag := if alts.size > 1 then parentTag ++ (`match).appendIndexAfter s.nextIdx else parentTag let holeName := mkIdentFrom holeOrTacticSeq tag let newHole ← `(?$holeName:ident) modify fun s => { s with nextIdx := s.nextIdx + 1} pure $ alt.setArg 2 newHole else withFreshMacroScope do let newHole ← `(?rhs) let newHoleId := newHole[1] let newCase ← `(tactic| case $newHoleId => $holeOrTacticSeq:tacticSeq ) modify fun s => { s with cases := s.cases.push newCase } pure $ alt.setArg 2 newHole let result := matchTac.setKind `Lean.Parser.Term.Β«matchΒ» let result := result.setArg 4 (matchAlts.setArg 1 (mkNullNode newAlts)) pure result private def mkAuxiliaryMatchTerm (parentTag : Name) (matchTac : Syntax) : MacroM (Syntax Γ— Array Syntax) := do let (matchTerm, s) ← mkAuxiliaryMatchTermAux parentTag matchTac |>.run {} pure (matchTerm, s.cases) @[builtinTactic Lean.Parser.Tactic.match] def evalMatch : Tactic := fun stx => do let tag ← getMainTag let (matchTerm, cases) ← liftMacroM $ mkAuxiliaryMatchTerm tag stx let refineMatchTerm ← `(tactic| refine $matchTerm) let stxNew := mkNullNode (#[refineMatchTerm] ++ cases) withMacroExpansion stx stxNew $ evalTactic stxNew end Lean.Elab.Tactic
b448d9d854b37a85cae62fae86b88e6aecfd52e4
e00ea76a720126cf9f6d732ad6216b5b824d20a7
/src/measure_theory/set_integral.lean
53dc9298204dee5ad25e9b32132f015ecb3c076b
[ "Apache-2.0" ]
permissive
vaibhavkarve/mathlib
a574aaf68c0a431a47fa82ce0637f0f769826bfe
17f8340912468f49bdc30acdb9a9fa02eeb0473a
refs/heads/master
1,621,263,802,637
1,585,399,588,000
1,585,399,588,000
250,833,447
0
0
Apache-2.0
1,585,410,341,000
1,585,410,341,000
null
UTF-8
Lean
false
false
14,712
lean
/- Copyright (c) 2020 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou -/ import measure_theory.bochner_integration import measure_theory.indicator_function import measure_theory.lebesgue_measure /-! # Set integral Integrate a function over a subset of a measure space. ## Main definitions `measurable_on`, `integrable_on`, `integral_on` ## Notation `∫ a in s, f a` is `measure_theory.integral (s.indicator f)` -/ noncomputable theory open set filter topological_space measure_theory measure_theory.simple_func open_locale classical topological_space interval universes u v w variables {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} section measurable_on variables [measurable_space Ξ±] [measurable_space Ξ²] [has_zero Ξ²] {s : set Ξ±} {f : Ξ± β†’ Ξ²} /-- `measurable_on s f` means `f` is measurable over the set `s`. -/ def measurable_on (s : set Ξ±) (f : Ξ± β†’ Ξ²) : Prop := measurable (s.indicator f) @[simp] lemma measurable_on_empty (f : Ξ± β†’ Ξ²) : measurable_on βˆ… f := by { rw [measurable_on, indicator_empty], exact measurable_const } @[simp] lemma measurable.measurable_on_univ (hf : measurable f) : measurable_on univ f := hf.if is_measurable.univ measurable_const @[simp] lemma measurable_on_singleton {Ξ±} [topological_space Ξ±] [t1_space Ξ±] {a : Ξ±} {f : Ξ± β†’ Ξ²} : measurable_on {a} f := Ξ» s hs, show is_measurable ((indicator {a} f)⁻¹' s), begin rw indicator_preimage, refine is_measurable.union _ (is_measurable_singleton.compl.inter $ measurable_const.preimage hs), by_cases h : a ∈ f⁻¹' s, { rw inter_eq_self_of_subset_left, { exact is_measurable_singleton }, rwa singleton_subset_iff }, rw [singleton_inter_eq_empty.2 h], exact is_measurable.empty end lemma is_measurable.inter_preimage {B : set Ξ²} (hs : is_measurable s) (hB : is_measurable B) (hf : measurable_on s f): is_measurable (s ∩ f ⁻¹' B) := begin replace hf : is_measurable ((indicator s f)⁻¹' B) := hf B hB, rw indicator_preimage at hf, replace hf := hf.diff _, rwa union_diff_cancel_right at hf, { assume a, simp {contextual := tt} }, exact hs.compl.inter (measurable_const.preimage hB) end lemma measurable.measurable_on (hs : is_measurable s) (hf : measurable f) : measurable_on s f := hf.if hs measurable_const lemma measurable_on.subset {t : set Ξ±} (hs : is_measurable s) (h : s βŠ† t) (hf : measurable_on t f) : measurable_on s f := begin have : measurable_on s (indicator t f) := measurable.measurable_on hs hf, simp only [measurable_on, indicator_indicator] at this, rwa [inter_eq_self_of_subset_left h] at this end lemma measurable_on.union {t : set Ξ±} {f : Ξ± β†’ Ξ²} (hs : is_measurable s) (ht : is_measurable t) (hsm : measurable_on s f) (htm : measurable_on t f) : measurable_on (s βˆͺ t) f := begin assume B hB, show is_measurable ((indicator (s βˆͺ t) f)⁻¹' B), rw indicator_preimage, refine is_measurable.union _ ((hs.union ht).compl.inter (measurable_const.preimage hB)), simp only [union_inter_distrib_right], exact (hs.inter_preimage hB hsm).union (ht.inter_preimage hB htm) end end measurable_on section integrable_on variables [measure_space Ξ±] [normed_group Ξ²] {s t : set Ξ±} {f g : Ξ± β†’ Ξ²} /-- `integrable_on s f` means `f` is integrable over the set `s`. -/ def integrable_on (s : set Ξ±) (f : Ξ± β†’ Ξ²) : Prop := integrable (s.indicator f) lemma integrable_on_congr (h : βˆ€x, x ∈ s β†’ f x = g x) : integrable_on s f ↔ integrable_on s g := by simp only [integrable_on, indicator_congr h] lemma integrable_on_congr_ae (h : βˆ€β‚˜ x, x ∈ s β†’ f x = g x) : integrable_on s f ↔ integrable_on s g := by { apply integrable_congr_ae, exact indicator_congr_ae h } @[simp] lemma integrable_on_empty (f : Ξ± β†’ Ξ²) : integrable_on βˆ… f := by { simp only [integrable_on, indicator_empty], apply integrable_zero } lemma measure_theory.integrable.integrable_on (s : set Ξ±) (hf : integrable f) : integrable_on s f := by { refine integrable_of_le (Ξ»a, _) hf, apply norm_indicator_le_norm_self } lemma integrable_on.subset (h : s βŠ† t) : integrable_on t f β†’ integrable_on s f := by { apply integrable_of_le_ae, filter_upwards [] norm_indicator_le_of_subset h _ } variables {π•œ : Type*} [normed_field π•œ] [normed_space π•œ Ξ²] lemma integrable_on.smul (s : set Ξ±) (c : π•œ) {f : Ξ± β†’ Ξ²} : integrable_on s f β†’ integrable_on s (Ξ»a, c β€’ f a) := by { simp only [integrable_on, indicator_smul], apply integrable.smul } lemma integrable_on.mul_left (s : set Ξ±) (r : ℝ) {f : Ξ± β†’ ℝ} (hf : integrable_on s f) : integrable_on s (Ξ»a, r * f a) := by { simp only [smul_eq_mul.symm], exact hf.smul s r } lemma integrable_on.mul_right (s : set Ξ±) (r : ℝ) {f : Ξ± β†’ ℝ} (hf : integrable_on s f) : integrable_on s (Ξ»a, f a * r) := by { simp only [mul_comm], exact hf.mul_left _ _ } lemma integrable_on.divide (s : set Ξ±) (r : ℝ) {f : Ξ± β†’ ℝ} (hf : integrable_on s f) : integrable_on s (Ξ»a, f a / r) := by { simp only [div_eq_mul_inv], exact hf.mul_right _ _ } lemma integrable_on.add (hfm : measurable_on s f) (hfi : integrable_on s f) (hgm : measurable_on s g) (hgi : integrable_on s g) : integrable_on s (Ξ»a, f a + g a) := by { rw [integrable_on, indicator_add], exact hfi.add hfm hgm hgi } lemma integrable_on.neg (hf : integrable_on s f) : integrable_on s (Ξ»a, -f a) := by { rw [integrable_on, indicator_neg], exact hf.neg } lemma integrable_on.sub (hfm : measurable_on s f) (hfi : integrable_on s f) (hgm : measurable_on s g) (hgi : integrable_on s g) : integrable_on s (Ξ»a, f a - g a) := by { rw [integrable_on, indicator_sub], exact hfi.sub hfm hgm hgi } lemma integrable_on.union (hs : is_measurable s) (ht : is_measurable t) (hsm : measurable_on s f) (hsi : integrable_on s f) (htm : measurable_on t f) (hti : integrable_on t f) : integrable_on (s βˆͺ t) f := begin rw ← union_diff_self, rw [integrable_on, indicator_union_of_disjoint], { refine integrable.add hsm hsi (htm.subset _ _) (hti.subset _), { exact ht.diff hs }, { exact diff_subset _ _ }, { exact diff_subset _ _ } }, exact disjoint_diff end lemma integrable_on_norm_iff (s : set Ξ±) (f : Ξ± β†’ Ξ²) : integrable_on s (Ξ»a, βˆ₯f aβˆ₯) ↔ integrable_on s f := begin simp only [integrable_on], convert integrable_norm_iff (indicator s f), funext, rw norm_indicator_eq_indicator_norm, end end integrable_on section integral_on variables [measure_space Ξ±] [normed_group Ξ²] [second_countable_topology Ξ²] [normed_space ℝ Ξ²] [complete_space Ξ²] {s t : set Ξ±} {f g : Ξ± β†’ Ξ²} open set notation `∫` binders ` in ` s `, ` r:(scoped f, measure_theory.integral (set.indicator s f)) := r lemma integral_on_undef (h : Β¬ (measurable_on s f ∧ integrable_on s f)) : (∫ a in s, f a) = 0 := integral_undef h lemma integral_on_non_measurable (h : Β¬ measurable_on s f) : (∫ a in s, f a) = 0 := integral_non_measurable h lemma integral_on_non_integrable (h : Β¬ integrable_on s f) : (∫ a in s, f a) = 0 := integral_non_integrable h variables (Ξ²) lemma integral_on_zero (s : set Ξ±) : (∫ a in s, (0:Ξ²)) = 0 := by simp variables {Ξ²} lemma integral_on_congr (h : βˆ€ a ∈ s, f a = g a) : (∫ a in s, f a) = (∫ a in s, g a) := by simp only [indicator_congr h] lemma integral_on_congr_of_ae_eq (hf : measurable_on s f) (hg : measurable_on s g) (h : βˆ€β‚˜ a, a ∈ s β†’ f a = g a) : (∫ a in s, f a) = (∫ a in s, g a) := integral_congr_ae hf hg (indicator_congr_ae h) lemma integral_on_congr_of_set (hsm : measurable_on s f) (htm : measurable_on t f) (h : βˆ€β‚˜ a, a ∈ s ↔ a ∈ t) : (∫ a in s, f a) = (∫ a in t, f a) := integral_congr_ae hsm htm $ indicator_congr_of_set h variables (s t) lemma integral_on_smul (r : ℝ) (f : Ξ± β†’ Ξ²) : (∫ a in s, r β€’ (f a)) = r β€’ (∫ a in s, f a) := by rw [← integral_smul, indicator_smul] lemma integral_on_mul_left (r : ℝ) (f : Ξ± β†’ ℝ) : (∫ a in s, r * (f a)) = r * (∫ a in s, f a) := integral_on_smul s r f lemma integral_on_mul_right (r : ℝ) (f : Ξ± β†’ ℝ) : (∫ a in s, (f a) * r) = (∫ a in s, f a) * r := by { simp only [mul_comm], exact integral_on_mul_left s r f } lemma integral_on_div (r : ℝ) (f : Ξ± β†’ ℝ) : (∫ a in s, (f a) / r) = (∫ a in s, f a) / r := by { simp only [div_eq_mul_inv], apply integral_on_mul_right } lemma integral_on_neg (f : Ξ± β†’ Ξ²) : (∫ a in s, -f a) = - (∫ a in s, f a) := by { simp only [indicator_neg], exact integral_neg _ } variables {s t} lemma integral_on_add {s : set Ξ±} (hfm : measurable_on s f) (hfi : integrable_on s f) (hgm : measurable_on s g) (hgi : integrable_on s g) : (∫ a in s, f a + g a) = (∫ a in s, f a) + (∫ a in s, g a) := by { simp only [indicator_add], exact integral_add hfm hfi hgm hgi } lemma integral_on_sub (hfm : measurable_on s f) (hfi : integrable_on s f) (hgm : measurable_on s g) (hgi : integrable_on s g) : (∫ a in s, f a - g a) = (∫ a in s, f a) - (∫ a in s, g a) := by { simp only [indicator_sub], exact integral_sub hfm hfi hgm hgi } lemma integral_on_le_integral_on_ae {f g : Ξ± β†’ ℝ} (hfm : measurable_on s f) (hfi : integrable_on s f) (hgm : measurable_on s g) (hgi : integrable_on s g) (h : βˆ€β‚˜ a, a ∈ s β†’ f a ≀ g a) : (∫ a in s, f a) ≀ (∫ a in s, g a) := begin apply integral_le_integral_ae hfm hfi hgm hgi, apply indicator_le_indicator_ae, exact h end lemma integral_on_le_integral_on {f g : Ξ± β†’ ℝ} (hfm : measurable_on s f) (hfi : integrable_on s f) (hgm : measurable_on s g) (hgi : integrable_on s g) (h : βˆ€ a, a ∈ s β†’ f a ≀ g a) : (∫ a in s, f a) ≀ (∫ a in s, g a) := integral_on_le_integral_on_ae hfm hfi hgm hgi $ by filter_upwards [] h lemma integral_on_union (hsm : measurable_on s f) (hsi : integrable_on s f) (htm : measurable_on t f) (hti : integrable_on t f) (h : disjoint s t) : (∫ a in (s βˆͺ t), f a) = (∫ a in s, f a) + (∫ a in t, f a) := by { rw [indicator_union_of_disjoint h, integral_add hsm hsi htm hti] } lemma integral_on_union_ae (hs : is_measurable s) (ht : is_measurable t) (hsm : measurable_on s f) (hsi : integrable_on s f) (htm : measurable_on t f) (hti : integrable_on t f) (h : βˆ€β‚˜ a, a βˆ‰ s ∩ t) : (∫ a in (s βˆͺ t), f a) = (∫ a in s, f a) + (∫ a in t, f a) := begin have := integral_congr_ae _ _ (indicator_union_ae h f), rw [this, integral_add hsm hsi htm hti], { exact hsm.union hs ht htm }, { exact measurable.add hsm htm } end lemma integral_on_nonneg_of_ae {f : Ξ± β†’ ℝ} (hf : βˆ€β‚˜ a, a ∈ s β†’ 0 ≀ f a) : (0:ℝ) ≀ (∫ a in s, f a) := integral_nonneg_of_ae $ by { filter_upwards [hf] Ξ» a h, indicator_nonneg' h } lemma integral_on_nonneg {f : Ξ± β†’ ℝ} (hf : βˆ€ a, a ∈ s β†’ 0 ≀ f a) : (0:ℝ) ≀ (∫ a in s, f a) := integral_on_nonneg_of_ae $ univ_mem_sets' hf lemma integral_on_nonpos_of_ae {f : Ξ± β†’ ℝ} (hf : βˆ€β‚˜ a, a ∈ s β†’ f a ≀ 0) : (∫ a in s, f a) ≀ 0 := integral_nonpos_of_nonpos_ae $ by { filter_upwards [hf] Ξ» a h, indicator_nonpos' h } lemma integral_on_nonpos {f : Ξ± β†’ ℝ} (hf : βˆ€ a, a ∈ s β†’ f a ≀ 0) : (∫ a in s, f a) ≀ 0 := integral_on_nonpos_of_ae $ univ_mem_sets' hf lemma tendsto_integral_on_of_monotone {s : β„• β†’ set Ξ±} {f : Ξ± β†’ Ξ²} (hsm : βˆ€i, is_measurable (s i)) (h_mono : monotone s) (hfm : measurable_on (Union s) f) (hfi : integrable_on (Union s) f) : tendsto (Ξ»i, ∫ a in (s i), f a) at_top (nhds (∫ a in (Union s), f a)) := let bound : Ξ± β†’ ℝ := indicator (Union s) (Ξ»a, βˆ₯f aβˆ₯) in begin apply tendsto_integral_of_dominated_convergence, { assume i, exact hfm.subset (hsm i) (subset_Union _ _) }, { assumption }, { show integrable_on (Union s) (Ξ»a, βˆ₯f aβˆ₯), rwa integrable_on_norm_iff }, { assume i, apply all_ae_of_all, assume a, rw [norm_indicator_eq_indicator_norm], exact indicator_le_indicator_of_subset (subset_Union _ _) (Ξ»a, norm_nonneg _) _ }, { filter_upwards [] Ξ»a, le_trans (tendsto_indicator_of_monotone _ h_mono _ _) (pure_le_nhds _) } end lemma tendsto_integral_on_of_antimono (s : β„• β†’ set Ξ±) (f : Ξ± β†’ Ξ²) (hsm : βˆ€i, is_measurable (s i)) (h_mono : βˆ€i j, i ≀ j β†’ s j βŠ† s i) (hfm : measurable_on (s 0) f) (hfi : integrable_on (s 0) f) : tendsto (Ξ»i, ∫ a in (s i), f a) at_top (nhds (∫ a in (Inter s), f a)) := let bound : Ξ± β†’ ℝ := indicator (s 0) (Ξ»a, βˆ₯f aβˆ₯) in begin apply tendsto_integral_of_dominated_convergence, { assume i, refine hfm.subset (hsm i) (h_mono _ _ (zero_le _)) }, { exact hfm.subset (is_measurable.Inter hsm) (Inter_subset _ _) }, { show integrable_on (s 0) (Ξ»a, βˆ₯f aβˆ₯), rwa integrable_on_norm_iff }, { assume i, apply all_ae_of_all, assume a, rw [norm_indicator_eq_indicator_norm], refine indicator_le_indicator_of_subset (h_mono _ _ (zero_le _)) (Ξ»a, norm_nonneg _) _ }, { filter_upwards [] Ξ»a, le_trans (tendsto_indicator_of_antimono _ h_mono _ _) (pure_le_nhds _) } end -- TODO : prove this for an encodable type -- by proving an encodable version of `filter.has_countable_basis_at_top_finset_nat` lemma integral_on_Union (s : β„• β†’ set Ξ±) (f : Ξ± β†’ Ξ²) (hm : βˆ€i, is_measurable (s i)) (hd : βˆ€ i j, i β‰  j β†’ s i ∩ s j = βˆ…) (hfm : measurable_on (Union s) f) (hfi : integrable_on (Union s) f) : (∫ a in (Union s), f a) = βˆ‘i, ∫ a in s i, f a := suffices h : tendsto (Ξ»n:finset β„•, n.sum (Ξ» i, ∫ a in s i, f a)) at_top (𝓝 $ (∫ a in (Union s), f a)), by { rwa tsum_eq_has_sum }, begin have : (Ξ»n:finset β„•, n.sum (Ξ» i, ∫ a in s i, f a)) = Ξ»n:finset β„•, ∫ a in (⋃i∈n, s i), f a, { funext, rw [← integral_finset_sum, indicator_finset_bUnion], { assume i hi j hj hij, exact hd i j hij }, { assume i, refine hfm.subset (hm _) (subset_Union _ _) }, { assume i, refine hfi.subset (subset_Union _ _) } }, rw this, refine tendsto_integral_filter_of_dominated_convergence _ _ _ _ _ _ _, { exact indicator (Union s) (Ξ» a, βˆ₯f aβˆ₯) }, { exact has_countable_basis_at_top_finset_nat }, { refine univ_mem_sets' (Ξ» n, _), simp only [mem_set_of_eq], refine hfm.subset (is_measurable.Union (Ξ» i, is_measurable.Union_Prop (Ξ»h, hm _))) (bUnion_subset_Union _ _), }, { assumption }, { refine univ_mem_sets' (Ξ» n, univ_mem_sets' $ _), simp only [mem_set_of_eq], assume a, rw ← norm_indicator_eq_indicator_norm, refine norm_indicator_le_of_subset (bUnion_subset_Union _ _) _ _ }, { rw [← integrable_on, integrable_on_norm_iff], assumption }, { filter_upwards [] Ξ»a, le_trans (tendsto_indicator_bUnion_finset _ _ _) (pure_le_nhds _) } end end integral_on
256d5d9828e87ec9364b0254e2d45509fa28c5b4
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/group_theory/specific_groups/dihedral.lean
8fb3cbccb2f80e110d697751deab2c68b19a3dcf
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
5,528
lean
/- Copyright (c) 2020 Shing Tak Lam. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Shing Tak Lam -/ import data.zmod.basic import group_theory.order_of_element /-! # Dihedral Groups We define the dihedral groups `dihedral_group n`, with elements `r i` and `sr i` for `i : zmod n`. For `n β‰  0`, `dihedral_group n` represents the symmetry group of the regular `n`-gon. `r i` represents the rotations of the `n`-gon by `2Ο€i/n`, and `sr i` represents the reflections of the `n`-gon. `dihedral_group 0` corresponds to the infinite dihedral group. -/ /-- For `n β‰  0`, `dihedral_group n` represents the symmetry group of the regular `n`-gon. `r i` represents the rotations of the `n`-gon by `2Ο€i/n`, and `sr i` represents the reflections of the `n`-gon. `dihedral_group 0` corresponds to the infinite dihedral group. -/ @[derive decidable_eq] inductive dihedral_group (n : β„•) : Type | r : zmod n β†’ dihedral_group | sr : zmod n β†’ dihedral_group namespace dihedral_group variables {n : β„•} /-- Multiplication of the dihedral group. -/ private def mul : dihedral_group n β†’ dihedral_group n β†’ dihedral_group n | (r i) (r j) := r (i + j) | (r i) (sr j) := sr (j - i) | (sr i) (r j) := sr (i + j) | (sr i) (sr j) := r (j - i) /-- The identity `1` is the rotation by `0`. -/ private def one : dihedral_group n := r 0 instance : inhabited (dihedral_group n) := ⟨one⟩ /-- The inverse of a an element of the dihedral group. -/ private def inv : dihedral_group n β†’ dihedral_group n | (r i) := r (-i) | (sr i) := sr i /-- The group structure on `dihedral_group n`. -/ instance : group (dihedral_group n) := { mul := mul, mul_assoc := begin rintros (a | a) (b | b) (c | c); simp only [mul]; ring, end, one := one, one_mul := begin rintros (a | a), exact congr_arg r (zero_add a), exact congr_arg sr (sub_zero a), end, mul_one := begin rintros (a | a), exact congr_arg r (add_zero a), exact congr_arg sr (add_zero a), end, inv := inv, mul_left_inv := begin rintros (a | a), exact congr_arg r (neg_add_self a), exact congr_arg r (sub_self a), end } @[simp] lemma r_mul_r (i j : zmod n) : r i * r j = r (i + j) := rfl @[simp] lemma r_mul_sr (i j : zmod n) : r i * sr j = sr (j - i) := rfl @[simp] lemma sr_mul_r (i j : zmod n) : sr i * r j = sr (i + j) := rfl @[simp] lemma sr_mul_sr (i j : zmod n) : sr i * sr j = r (j - i) := rfl lemma one_def : (1 : dihedral_group n) = r 0 := rfl private def fintype_helper : (zmod n βŠ• zmod n) ≃ dihedral_group n := { inv_fun := Ξ» i, match i with | (r j) := sum.inl j | (sr j) := sum.inr j end, to_fun := Ξ» i, match i with | (sum.inl j) := r j | (sum.inr j) := sr j end, left_inv := by rintro (x | x); refl, right_inv := by rintro (x | x); refl } /-- If `0 < n`, then `dihedral_group n` is a finite group. -/ instance [fact (0 < n)] : fintype (dihedral_group n) := fintype.of_equiv _ fintype_helper instance : nontrivial (dihedral_group n) := ⟨⟨r 0, sr 0, dec_trivial⟩⟩ /-- If `0 < n`, then `dihedral_group n` has `2n` elements. -/ lemma card [fact (0 < n)] : fintype.card (dihedral_group n) = 2 * n := by rw [← fintype.card_eq.mpr ⟨fintype_helper⟩, fintype.card_sum, zmod.card, two_mul] @[simp] lemma r_one_pow (k : β„•) : (r 1 : dihedral_group n) ^ k = r k := begin induction k with k IH, { refl }, { rw [pow_succ, IH, r_mul_r], congr' 1, norm_cast, rw nat.one_add } end @[simp] lemma r_one_pow_n : (r (1 : zmod n))^n = 1 := begin cases n, { rw pow_zero }, { rw [r_one_pow, one_def], congr' 1, exact zmod.nat_cast_self _, } end @[simp] lemma sr_mul_self (i : zmod n) : sr i * sr i = 1 := by rw [sr_mul_sr, sub_self, one_def] /-- If `0 < n`, then `sr i` has order 2. -/ @[simp] lemma order_of_sr (i : zmod n) : order_of (sr i) = 2 := begin rw order_of_eq_prime _ _, { exact ⟨nat.prime_two⟩ }, rw [sq, sr_mul_self], dec_trivial, end /-- If `0 < n`, then `r 1` has order `n`. -/ @[simp] lemma order_of_r_one : order_of (r 1 : dihedral_group n) = n := begin by_cases hnpos : 0 < n, { haveI : fact (0 < n) := ⟨hnpos⟩, cases lt_or_eq_of_le (nat.le_of_dvd hnpos (order_of_dvd_of_pow_eq_one (@r_one_pow_n n))) with h h, { have h1 : (r 1 : dihedral_group n)^(order_of (r 1)) = 1, { exact pow_order_of_eq_one _ }, rw r_one_pow at h1, injection h1 with h2, rw [← zmod.val_eq_zero, zmod.val_nat_cast, nat.mod_eq_of_lt h] at h2, apply absurd h2.symm, apply ne_of_lt, exact absurd h2.symm (ne_of_lt (order_of_pos _)) }, { exact h } }, { simp only [not_lt, nonpos_iff_eq_zero] at hnpos, rw hnpos, apply order_of_eq_zero, rw is_of_fin_order_iff_pow_eq_one, push_neg, intros m hm, rw [r_one_pow, one_def], by_contradiction, rw not_not at h, have h' : (m : zmod 0) = 0, { exact r.inj h, }, have h'' : m = 0, { simp only [int.coe_nat_eq_zero, int.nat_cast_eq_coe_nat] at h', exact h', }, rw h'' at hm, apply nat.lt_irrefl, exact hm }, end /-- If `0 < n`, then `i : zmod n` has order `n / gcd n i`. -/ lemma order_of_r [fact (0 < n)] (i : zmod n) : order_of (r i) = n / nat.gcd n i.val := begin conv_lhs { rw ←zmod.nat_cast_zmod_val i }, rw [←r_one_pow, order_of_pow, order_of_r_one] end end dihedral_group
fc22ab8236e74e372614014c6d0b019558dfbc73
737dc4b96c97368cb66b925eeea3ab633ec3d702
/src/Lean/Meta/Tactic/Cases.lean
3633caa6898706376b55cb11d48e097dbfb31f7b
[ "Apache-2.0" ]
permissive
Bioye97/lean4
1ace34638efd9913dc5991443777b01a08983289
bc3900cbb9adda83eed7e6affeaade7cfd07716d
refs/heads/master
1,690,589,820,211
1,631,051,000,000
1,631,067,598,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
16,995
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Meta.AppBuilder import Lean.Meta.Tactic.Induction import Lean.Meta.Tactic.Injection import Lean.Meta.Tactic.Assert import Lean.Meta.Tactic.Subst namespace Lean.Meta private def throwInductiveTypeExpected {Ξ±} (type : Expr) : MetaM Ξ± := do throwError "failed to compile pattern matching, inductive type expected{indentExpr type}" def getInductiveUniverseAndParams (type : Expr) : MetaM (List Level Γ— Array Expr) := do let type ← whnfD type matchConstInduct type.getAppFn (fun _ => throwInductiveTypeExpected type) fun val us => let I := type.getAppFn let Iargs := type.getAppArgs let params := Iargs.extract 0 val.numParams pure (us, params) private def mkEqAndProof (lhs rhs : Expr) : MetaM (Expr Γ— Expr) := do let lhsType ← inferType lhs let rhsType ← inferType rhs let u ← getLevel lhsType if (← isDefEq lhsType rhsType) then pure (mkApp3 (mkConst `Eq [u]) lhsType lhs rhs, mkApp2 (mkConst `Eq.refl [u]) lhsType lhs) else pure (mkApp4 (mkConst `HEq [u]) lhsType lhs rhsType rhs, mkApp2 (mkConst `HEq.refl [u]) lhsType lhs) private partial def withNewEqs {Ξ±} (targets targetsNew : Array Expr) (k : Array Expr β†’ Array Expr β†’ MetaM Ξ±) : MetaM Ξ± := let rec loop (i : Nat) (newEqs : Array Expr) (newRefls : Array Expr) := do if h : i < targets.size then let (newEqType, newRefl) ← mkEqAndProof targets[i] targetsNew[i] withLocalDeclD `h newEqType fun newEq => do loop (i+1) (newEqs.push newEq) (newRefls.push newRefl) else k newEqs newRefls loop 0 #[] #[] def generalizeTargetsEq (mvarId : MVarId) (motiveType : Expr) (targets : Array Expr) : MetaM MVarId := withMVarContext mvarId do checkNotAssigned mvarId `generalizeTargets let (typeNew, eqRefls) ← forallTelescopeReducing motiveType fun targetsNew _ => do unless targetsNew.size == targets.size do throwError "invalid number of targets #{targets.size}, motive expects #{targetsNew.size}" withNewEqs targets targetsNew fun eqs eqRefls => do let type ← getMVarType mvarId let typeNew ← mkForallFVars eqs type let typeNew ← mkForallFVars targetsNew typeNew pure (typeNew, eqRefls) let mvarNew ← mkFreshExprSyntheticOpaqueMVar typeNew (← getMVarTag mvarId) assignExprMVar mvarId (mkAppN (mkAppN mvarNew targets) eqRefls) pure mvarNew.mvarId! structure GeneralizeIndicesSubgoal where mvarId : MVarId indicesFVarIds : Array FVarId fvarId : FVarId numEqs : Nat /-- Similar to `generalizeTargets` but customized for the `casesOn` motive. Given a metavariable `mvarId` representing the ``` Ctx, h : I A j, D |- T ``` where `fvarId` is `h`s id, and the type `I A j` is an inductive datatype where `A` are parameters, and `j` the indices. Generate the goal ``` Ctx, h : I A j, D, j' : J, h' : I A j' |- j == j' -> h == h' -> T ``` Remark: `(j == j' -> h == h')` is a "telescopic" equality. Remark: `j` is sequence of terms, and `j'` a sequence of free variables. The result contains the fields - `mvarId`: the new goal - `indicesFVarIds`: `j'` ids - `fvarId`: `h'` id - `numEqs`: number of equations in the target -/ def generalizeIndices (mvarId : MVarId) (fvarId : FVarId) : MetaM GeneralizeIndicesSubgoal := withMVarContext mvarId do let lctx ← getLCtx let localInsts ← getLocalInstances checkNotAssigned mvarId `generalizeIndices let fvarDecl ← getLocalDecl fvarId let type ← whnf fvarDecl.type type.withApp fun f args => matchConstInduct f (fun _ => throwTacticEx `generalizeIndices mvarId "inductive type expected") fun val _ => do unless val.numIndices > 0 do throwTacticEx `generalizeIndices mvarId "indexed inductive type expected" unless args.size == val.numIndices + val.numParams do throwTacticEx `generalizeIndices mvarId "ill-formed inductive datatype" let indices := args.extract (args.size - val.numIndices) args.size let IA := mkAppN f (args.extract 0 val.numParams) -- `I A` let IAType ← inferType IA forallTelescopeReducing IAType fun newIndices _ => do let newType := mkAppN IA newIndices withLocalDeclD fvarDecl.userName newType fun h' => withNewEqs indices newIndices fun newEqs newRefls => do let (newEqType, newRefl) ← mkEqAndProof fvarDecl.toExpr h' let newRefls := newRefls.push newRefl withLocalDeclD `h newEqType fun newEq => do let newEqs := newEqs.push newEq /- auxType `forall (j' : J) (h' : I A j'), j == j' -> h == h' -> target -/ let target ← getMVarType mvarId let tag ← getMVarTag mvarId let auxType ← mkForallFVars newEqs target let auxType ← mkForallFVars #[h'] auxType let auxType ← mkForallFVars newIndices auxType let newMVar ← mkFreshExprMVarAt lctx localInsts auxType MetavarKind.syntheticOpaque tag /- assign mvarId := newMVar indices h refls -/ assignExprMVar mvarId (mkAppN (mkApp (mkAppN newMVar indices) fvarDecl.toExpr) newRefls) let (indicesFVarIds, newMVarId) ← introNP newMVar.mvarId! newIndices.size let (fvarId, newMVarId) ← intro1P newMVarId pure { mvarId := newMVarId, indicesFVarIds := indicesFVarIds, fvarId := fvarId, numEqs := newEqs.size } structure CasesSubgoal extends InductionSubgoal where ctorName : Name namespace Cases structure Context where inductiveVal : InductiveVal casesOnVal : DefinitionVal nminors : Nat := inductiveVal.ctors.length majorDecl : LocalDecl majorTypeFn : Expr majorTypeArgs : Array Expr majorTypeIndices : Array Expr := majorTypeArgs.extract (majorTypeArgs.size - inductiveVal.numIndices) majorTypeArgs.size private def mkCasesContext? (majorFVarId : FVarId) : MetaM (Option Context) := do let env ← getEnv if !env.contains `Eq || !env.contains `HEq then pure none else let majorDecl ← getLocalDecl majorFVarId let majorType ← whnf majorDecl.type majorType.withApp fun f args => matchConstInduct f (fun _ => pure none) fun ival _ => if args.size != ival.numIndices + ival.numParams then pure none else match env.find? (Name.mkStr ival.name "casesOn") with | ConstantInfo.defnInfo cval => pure $ some { inductiveVal := ival, casesOnVal := cval, majorDecl := majorDecl, majorTypeFn := f, majorTypeArgs := args } | _ => pure none /- We say the major premise has independent indices IF 1- its type is *not* an indexed inductive family, OR 2- its type is an indexed inductive family, but all indices are distinct free variables, and all local declarations different from the major and its indices do not depend on the indices. -/ private def hasIndepIndices (ctx : Context) : MetaM Bool := do if ctx.majorTypeIndices.isEmpty then pure true else if ctx.majorTypeIndices.any $ fun idx => !idx.isFVar then /- One of the indices is not a free variable. -/ pure false else if ctx.majorTypeIndices.size.any fun i => i.any fun j => ctx.majorTypeIndices[i] == ctx.majorTypeIndices[j] then /- An index ocurrs more than once -/ pure false else let lctx ← getLCtx let mctx ← getMCtx pure $ lctx.all fun decl => decl.fvarId == ctx.majorDecl.fvarId || -- decl is the major ctx.majorTypeIndices.any (fun index => decl.fvarId == index.fvarId!) || -- decl is one of the indices mctx.findLocalDeclDependsOn decl (fun fvarId => ctx.majorTypeIndices.all $ fun idx => idx.fvarId! != fvarId) -- or does not depend on any index private def elimAuxIndices (s₁ : GeneralizeIndicesSubgoal) (sβ‚‚ : Array CasesSubgoal) : MetaM (Array CasesSubgoal) := let indicesFVarIds := s₁.indicesFVarIds sβ‚‚.mapM fun s => do indicesFVarIds.foldlM (init := s) fun s indexFVarId => match s.subst.get indexFVarId with | Expr.fvar indexFVarId' _ => (do let mvarId ← clear s.mvarId indexFVarId'; pure { s with mvarId := mvarId, subst := s.subst.erase indexFVarId }) <|> (pure s) | _ => pure s /- Convert `s` into an array of `CasesSubgoal`, by attaching the corresponding constructor name, and adding the substitution `majorFVarId -> ctor_i us params fields` into each subgoal. -/ private def toCasesSubgoals (s : Array InductionSubgoal) (ctorNames : Array Name) (majorFVarId : FVarId) (us : List Level) (params : Array Expr) : Array CasesSubgoal := s.mapIdx fun i s => let ctorName := ctorNames[i] let ctorApp := mkAppN (mkAppN (mkConst ctorName us) params) s.fields let s := { s with subst := s.subst.insert majorFVarId ctorApp } { ctorName := ctorName, toInductionSubgoal := s } /- Convert heterogeneous equality into a homegeneous one -/ private def heqToEq (mvarId : MVarId) (eqDecl : LocalDecl) : MetaM MVarId := do /- Convert heterogeneous equality into a homegeneous one -/ let prf ← mkEqOfHEq (mkFVar eqDecl.fvarId) let aEqb ← whnf (← inferType prf) let mvarId ← assert mvarId eqDecl.userName aEqb prf clear mvarId eqDecl.fvarId partial def unifyEqs (numEqs : Nat) (mvarId : MVarId) (subst : FVarSubst) (caseName? : Option Name := none): MetaM (Option (MVarId Γ— FVarSubst)) := do if numEqs == 0 then pure (some (mvarId, subst)) else let (eqFVarId, mvarId) ← intro1 mvarId withMVarContext mvarId do let eqDecl ← getLocalDecl eqFVarId if eqDecl.type.isHEq then let mvarId ← heqToEq mvarId eqDecl unifyEqs numEqs mvarId subst caseName? else match eqDecl.type.eq? with | none => throwError "equality expected{indentExpr eqDecl.type}" | some (Ξ±, a, b) => /- Remark: we do not check `isDefeq` here because we would fail to substitute equalities such as `x = t` and `t = x` when `x` and `t` are proofs (proof irrelanvance). -/ /- Remark: we use `let rec` here because otherwise the compiler would generate an insane amount of code. We can remove the `rec` after we fix the eagerly inlining issue in the compiler. -/ let rec substEq (symm : Bool) := do /- TODO: support for acyclicity (e.g., `xs β‰  x :: xs`) -/ /- Remark: `substCore` fails if the equation is of the form `x = x` -/ if let some (substNew, mvarId) ← observing? (substCore mvarId eqFVarId symm subst) then unifyEqs (numEqs - 1) mvarId substNew caseName? else if (← isDefEq a b) then /- Skip equality -/ unifyEqs (numEqs - 1) (← clear mvarId eqFVarId) subst caseName? else throwError "dependent elimination failed, failed to solve equation{indentExpr eqDecl.type}" let rec injection (a b : Expr) := do let env ← getEnv if a.isConstructorApp env && b.isConstructorApp env then /- ctor_i ... = ctor_j ... -/ match (← injectionCore mvarId eqFVarId) with | InjectionResultCore.solved => pure none -- this alternative has been solved | InjectionResultCore.subgoal mvarId numEqsNew => unifyEqs (numEqs - 1 + numEqsNew) mvarId subst caseName? else let a' ← whnf a let b' ← whnf b if a' != a || b' != b then /- Reduced lhs/rhs of current equality -/ let prf := mkFVar eqFVarId let aEqb' ← mkEq a' b' let mvarId ← assert mvarId eqDecl.userName aEqb' prf let mvarId ← clear mvarId eqFVarId unifyEqs numEqs mvarId subst caseName? else match caseName? with | none => throwError "dependent elimination failed, failed to solve equation{indentExpr eqDecl.type}" | some caseName => throwError "dependent elimination failed, failed to solve equation{indentExpr eqDecl.type}\nat case {mkConst caseName}" let a ← instantiateMVars a let b ← instantiateMVars b match a, b with | Expr.fvar aFVarId _, Expr.fvar bFVarId _ => /- x = y -/ let aDecl ← getLocalDecl aFVarId let bDecl ← getLocalDecl bFVarId substEq (aDecl.index < bDecl.index) | Expr.fvar .., _ => /- x = t -/ substEq (symm := false) | _, Expr.fvar .. => /- t = x -/ substEq (symm := true) | a, b => if (← isDefEq a b) then /- Skip equality -/ unifyEqs (numEqs - 1) (← clear mvarId eqFVarId) subst caseName? else injection a b private def unifyCasesEqs (numEqs : Nat) (subgoals : Array CasesSubgoal) : MetaM (Array CasesSubgoal) := subgoals.foldlM (init := #[]) fun subgoals s => do match (← unifyEqs numEqs s.mvarId s.subst s.ctorName) with | none => pure subgoals | some (mvarId, subst) => pure $ subgoals.push { s with mvarId := mvarId, subst := subst, fields := s.fields.map (subst.apply Β·) } private def inductionCasesOn (mvarId : MVarId) (majorFVarId : FVarId) (givenNames : Array AltVarNames) (ctx : Context) : MetaM (Array CasesSubgoal) := do withMVarContext mvarId do let majorType ← inferType (mkFVar majorFVarId) let (us, params) ← getInductiveUniverseAndParams majorType let casesOn := mkCasesOnName ctx.inductiveVal.name let ctors := ctx.inductiveVal.ctors.toArray let s ← induction mvarId majorFVarId casesOn givenNames return toCasesSubgoals s ctors majorFVarId us params def cases (mvarId : MVarId) (majorFVarId : FVarId) (givenNames : Array AltVarNames := #[]) : MetaM (Array CasesSubgoal) := withMVarContext mvarId do checkNotAssigned mvarId `cases let context? ← mkCasesContext? majorFVarId match context? with | none => throwTacticEx `cases mvarId "not applicable to the given hypothesis" | some ctx => /- Remark: if caller does not need a `FVarSubst` (variable substitution), and `hasIndepIndices ctx` is true, then we can also use the simple case. This is a minor optimization, and we currently do not even allow callers to specify whether they want the `FVarSubst` or not. -/ if ctx.inductiveVal.numIndices == 0 then -- Simple case inductionCasesOn mvarId majorFVarId givenNames ctx else let s₁ ← generalizeIndices mvarId majorFVarId trace[Meta.Tactic.cases] "after generalizeIndices\n{MessageData.ofGoal s₁.mvarId}" let sβ‚‚ ← inductionCasesOn s₁.mvarId s₁.fvarId givenNames ctx let sβ‚‚ ← elimAuxIndices s₁ sβ‚‚ unifyCasesEqs s₁.numEqs sβ‚‚ end Cases def cases (mvarId : MVarId) (majorFVarId : FVarId) (givenNames : Array AltVarNames := #[]) : MetaM (Array CasesSubgoal) := Cases.cases mvarId majorFVarId givenNames def casesRec (mvarId : MVarId) (p : LocalDecl β†’ MetaM Bool) : MetaM (List MVarId) := saturate mvarId fun mvarId => withMVarContext mvarId do for localDecl in (← getLCtx) do if (← p localDecl) then let r? ← observing? do let r ← cases mvarId localDecl.fvarId return r.toList.map (Β·.mvarId) if r?.isSome then return r? return none def casesAnd (mvarId : MVarId) : MetaM MVarId := do let mvarIds ← casesRec mvarId fun localDecl => return (← instantiateMVars localDecl.type).isAppOfArity ``And 2 exactlyOne mvarIds def substEqs (mvarId : MVarId) : MetaM MVarId := do let mvarIds ← casesRec mvarId fun localDecl => do let type ← instantiateMVars localDecl.type return type.isEq || type.isHEq exactlyOne mvarIds structure ByCasesSubgoal where mvarId : MVarId fvarId : FVarId def byCases (mvarId : MVarId) (p : Expr) (hName : Name := `h) : MetaM (ByCasesSubgoal Γ— ByCasesSubgoal) := do let mvarId ← assert mvarId `hByCases (mkOr p (mkNot p)) (mkEM p) let (fvarId, mvarId) ← intro1 mvarId let #[s₁, sβ‚‚] ← cases mvarId fvarId #[{ varNames := [hName] }, { varNames := [hName] }] | throwError "'byCases' tactic failed, unexpected number of subgoals" return ((← toByCasesSubgoal s₁), (← toByCasesSubgoal sβ‚‚)) where toByCasesSubgoal (s : CasesSubgoal) : MetaM ByCasesSubgoal := do let #[Expr.fvar fvarId ..] ← pure s.fields | throwError "'byCases' tactic failed, unexpected new hypothesis" return { mvarId := s.mvarId, fvarId } builtin_initialize registerTraceClass `Meta.Tactic.cases end Lean.Meta
bf050da66c475f40b2c68700e96f290c77aecde6
31f556cdeb9239ffc2fad8f905e33987ff4feab9
/src/Lean/CoreM.lean
ab84d1d1c165073ea7e8ee4f56d0fbb2f22c35df
[ "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
tobiasgrosser/lean4
ce0fd9cca0feba1100656679bf41f0bffdbabb71
ebdbdc10436a4d9d6b66acf78aae7a23f5bd073f
refs/heads/master
1,673,103,412,948
1,664,930,501,000
1,664,930,501,000
186,870,185
0
0
Apache-2.0
1,665,129,237,000
1,557,939,901,000
Lean
UTF-8
Lean
false
false
13,437
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Util.RecDepth import Lean.Util.Trace import Lean.Log import Lean.Eval import Lean.ResolveName import Lean.Elab.InfoTree.Types import Lean.MonadEnv namespace Lean namespace Core register_builtin_option maxHeartbeats : Nat := { defValue := 200000 descr := "maximum amount of heartbeats per command. A heartbeat is number of (small) memory allocations (in thousands), 0 means no limit" } def getMaxHeartbeats (opts : Options) : Nat := maxHeartbeats.get opts * 1000 abbrev InstantiateLevelCache := PersistentHashMap Name (List Level Γ— Expr) /-- Cache for the `CoreM` monad -/ structure Cache where instLevelType : InstantiateLevelCache := {} instLevelValue : InstantiateLevelCache := {} deriving Inhabited /-- State for the CoreM monad. -/ structure State where /-- Current environment. -/ env : Environment /-- Next macro scope. We use macro scopes to avoid accidental name capture. -/ nextMacroScope : MacroScope := firstFrontendMacroScope + 1 /-- Name generator for producing unique `FVarId`s, `MVarId`s, and `LMVarId`s -/ ngen : NameGenerator := {} /-- Trace messages -/ traceState : TraceState := {} /-- Cache for instantiating universe polymorphic declarations. -/ cache : Cache := {} /-- Message log. -/ messages : MessageLog := {} /-- Info tree. We have the info tree here because we want to update it while adding attributes. -/ infoState : Elab.InfoState := {} deriving Inhabited /-- Context for the CoreM monad. -/ structure Context where /-- Name of the file being compiled. -/ fileName : String /-- Auxiliary datastructure for converting `String.Pos` into Line/Column number. -/ fileMap : FileMap options : Options := {} currRecDepth : Nat := 0 maxRecDepth : Nat := 1000 ref : Syntax := Syntax.missing currNamespace : Name := Name.anonymous openDecls : List OpenDecl := [] initHeartbeats : Nat := 0 maxHeartbeats : Nat := getMaxHeartbeats options currMacroScope : MacroScope := firstFrontendMacroScope /-- CoreM is a monad for manipulating the Lean environment. It is the base monad for `MetaM`. The main features it provides are: - name generator state - environment state - Lean options context - the current open namespace -/ abbrev CoreM := ReaderT Context <| StateRefT State (EIO Exception) -- Make the compiler generate specialized `pure`/`bind` so we do not have to optimize through the -- whole monad stack at every use site. May eventually be covered by `deriving`. instance : Monad CoreM := let i := inferInstanceAs (Monad CoreM); { pure := i.pure, bind := i.bind } instance : Inhabited (CoreM Ξ±) where default := fun _ _ => throw default instance : MonadRef CoreM where getRef := return (← read).ref withRef ref x := withReader (fun ctx => { ctx with ref := ref }) x instance : MonadEnv CoreM where getEnv := return (← get).env modifyEnv f := modify fun s => { s with env := f s.env, cache := {} } instance : MonadOptions CoreM where getOptions := return (← read).options instance : MonadWithOptions CoreM where withOptions f x := withReader (fun ctx => { ctx with options := f ctx.options }) x instance : AddMessageContext CoreM where addMessageContext := addMessageContextPartial instance : MonadNameGenerator CoreM where getNGen := return (← get).ngen setNGen ngen := modify fun s => { s with ngen := ngen } instance : MonadRecDepth CoreM where withRecDepth d x := withReader (fun ctx => { ctx with currRecDepth := d }) x getRecDepth := return (← read).currRecDepth getMaxRecDepth := return (← read).maxRecDepth instance : MonadResolveName CoreM where getCurrNamespace := return (← read).currNamespace getOpenDecls := return (← read).openDecls protected def withFreshMacroScope (x : CoreM Ξ±) : CoreM Ξ± := do let fresh ← modifyGetThe Core.State (fun st => (st.nextMacroScope, { st with nextMacroScope := st.nextMacroScope + 1 })) withReader (fun ctx => { ctx with currMacroScope := fresh }) x instance : MonadQuotation CoreM where getCurrMacroScope := return (← read).currMacroScope getMainModule := return (← get).env.mainModule withFreshMacroScope := Core.withFreshMacroScope instance : Elab.MonadInfoTree CoreM where getInfoState := return (← get).infoState modifyInfoState f := modify fun s => { s with infoState := f s.infoState } @[inline] def modifyCache (f : Cache β†’ Cache) : CoreM Unit := modify fun ⟨env, next, ngen, trace, cache, messages, infoState⟩ => ⟨env, next, ngen, trace, f cache, messages, infoState⟩ @[inline] def modifyInstLevelTypeCache (f : InstantiateLevelCache β†’ InstantiateLevelCache) : CoreM Unit := modifyCache fun ⟨c₁, cβ‚‚βŸ© => ⟨f c₁, cβ‚‚βŸ© @[inline] def modifyInstLevelValueCache (f : InstantiateLevelCache β†’ InstantiateLevelCache) : CoreM Unit := modifyCache fun ⟨c₁, cβ‚‚βŸ© => ⟨c₁, f cβ‚‚βŸ© def instantiateTypeLevelParams (c : ConstantInfo) (us : List Level) : CoreM Expr := do if let some (us', r) := (← get).cache.instLevelType.find? c.name then if us == us' then return r let r := c.instantiateTypeLevelParams us modifyInstLevelTypeCache fun s => s.insert c.name (us, r) return r def instantiateValueLevelParams (c : ConstantInfo) (us : List Level) : CoreM Expr := do if let some (us', r) := (← get).cache.instLevelValue.find? c.name then if us == us' then return r let r := c.instantiateValueLevelParams us modifyInstLevelValueCache fun s => s.insert c.name (us, r) return r @[inline] def liftIOCore (x : IO Ξ±) : CoreM Ξ± := do let ref ← getRef IO.toEIO (fun (err : IO.Error) => Exception.error ref (toString err)) x instance : MonadLift IO CoreM where monadLift := liftIOCore instance : MonadTrace CoreM where getTraceState := return (← get).traceState modifyTraceState f := modify fun s => { s with traceState := f s.traceState } /-- Restore backtrackable parts of the state. -/ def restore (b : State) : CoreM Unit := modify fun s => { s with env := b.env, messages := b.messages, infoState := b.infoState } private def mkFreshNameImp (n : Name) : CoreM Name := do let fresh ← modifyGet fun s => (s.nextMacroScope, { s with nextMacroScope := s.nextMacroScope + 1 }) return addMacroScope (← getEnv).mainModule n fresh def mkFreshUserName (n : Name) : CoreM Name := mkFreshNameImp n @[inline] def CoreM.run (x : CoreM Ξ±) (ctx : Context) (s : State) : EIO Exception (Ξ± Γ— State) := (x ctx).run s @[inline] def CoreM.run' (x : CoreM Ξ±) (ctx : Context) (s : State) : EIO Exception Ξ± := Prod.fst <$> x.run ctx s @[inline] def CoreM.toIO (x : CoreM Ξ±) (ctx : Context) (s : State) : IO (Ξ± Γ— State) := do match (← (x.run { ctx with initHeartbeats := (← IO.getNumHeartbeats) } s).toIO') with | Except.error (Exception.error _ msg) => throw <| IO.userError (← msg.toString) | Except.error (Exception.internal id _) => throw <| IO.userError <| "internal exception #" ++ toString id.idx | Except.ok a => return a instance [MetaEval Ξ±] : MetaEval (CoreM Ξ±) where eval env opts x _ := do let x : CoreM Ξ± := do try x finally printTraces let (a, s) ← x.toIO { maxRecDepth := maxRecDepth.get opts, options := opts, fileName := "<CoreM>", fileMap := default } { env := env } MetaEval.eval s.env opts a (hideUnit := true) -- withIncRecDepth for a monad `m` such that `[MonadControlT CoreM n]` protected def withIncRecDepth [Monad m] [MonadControlT CoreM m] (x : m Ξ±) : m Ξ± := controlAt CoreM fun runInBase => withIncRecDepth (runInBase x) def throwMaxHeartbeat (moduleName : Name) (optionName : Name) (max : Nat) : CoreM Unit := do let msg := s!"(deterministic) timeout at '{moduleName}', maximum number of heartbeats ({max/1000}) has been reached (use 'set_option {optionName} <num>' to set the limit)" throw <| Exception.error (← getRef) (MessageData.ofFormat (Std.Format.text msg)) def checkMaxHeartbeatsCore (moduleName : String) (optionName : Name) (max : Nat) : CoreM Unit := do unless max == 0 do let numHeartbeats ← IO.getNumHeartbeats if numHeartbeats - (← read).initHeartbeats > max then throwMaxHeartbeat moduleName optionName max def checkMaxHeartbeats (moduleName : String) : CoreM Unit := do checkMaxHeartbeatsCore moduleName `maxHeartbeats (← read).maxHeartbeats private def withCurrHeartbeatsImp (x : CoreM Ξ±) : CoreM Ξ± := do let heartbeats ← IO.getNumHeartbeats withReader (fun ctx => { ctx with initHeartbeats := heartbeats }) x def withCurrHeartbeats [Monad m] [MonadControlT CoreM m] (x : m Ξ±) : m Ξ± := controlAt CoreM fun runInBase => withCurrHeartbeatsImp (runInBase x) def setMessageLog (messages : MessageLog) : CoreM Unit := modify fun s => { s with messages := messages } def resetMessageLog : CoreM Unit := setMessageLog {} def getMessageLog : CoreM MessageLog := return (← get).messages instance : MonadLog CoreM where getRef := getRef getFileMap := return (← read).fileMap getFileName := return (← read).fileName hasErrors := return (← get).messages.hasErrors logMessage msg := do let ctx ← read let msg := { msg with data := MessageData.withNamingContext { currNamespace := ctx.currNamespace, openDecls := ctx.openDecls } msg.data }; modify fun s => { s with messages := s.messages.add msg } end Core export Core (CoreM mkFreshUserName checkMaxHeartbeats withCurrHeartbeats) @[inline] def withAtLeastMaxRecDepth [MonadFunctorT CoreM m] (max : Nat) : m Ξ± β†’ m Ξ± := monadMap (m := CoreM) <| withReader (fun ctx => { ctx with maxRecDepth := Nat.max max ctx.maxRecDepth }) @[inline] def catchInternalId [Monad m] [MonadExcept Exception m] (id : InternalExceptionId) (x : m Ξ±) (h : Exception β†’ m Ξ±) : m Ξ± := do try x catch ex => match ex with | .error .. => throw ex | .internal id' _ => if id == id' then h ex else throw ex @[inline] def catchInternalIds [Monad m] [MonadExcept Exception m] (ids : List InternalExceptionId) (x : m Ξ±) (h : Exception β†’ m Ξ±) : m Ξ± := do try x catch ex => match ex with | .error .. => throw ex | .internal id _ => if ids.contains id then h ex else throw ex /-- Return true if `ex` was generated by `throwMaxHeartbeat`. This function is a bit hackish. The heartbeat exception should probably be an internal exception. We used a similar hack at `Exception.isMaxRecDepth` -/ def Exception.isMaxHeartbeat (ex : Exception) : Bool := match ex with | Exception.error _ (MessageData.ofFormat (Std.Format.text msg)) => "(deterministic) timeout".isPrefixOf msg | _ => false /-- Creates the expression `d β†’ b` -/ def mkArrow (d b : Expr) : CoreM Expr := return Lean.mkForall (← mkFreshUserName `x) BinderInfo.default d b def addDecl (decl : Declaration) : CoreM Unit := do if !(← MonadLog.hasErrors) && decl.hasSorry then logWarning "declaration uses 'sorry'" match (← getEnv).addDecl decl with | Except.ok env => setEnv env | Except.error ex => throwKernelException ex private def supportedRecursors := #[``Empty.rec, ``False.rec, ``Eq.ndrec, ``Eq.rec, ``Eq.recOn, ``Eq.casesOn, ``False.casesOn, ``Empty.casesOn, ``And.rec, ``And.casesOn] /-- This is a temporary workaround for generating better error messages for the compiler. It can be deleted after we rewrite the remaining parts of the compiler in Lean. -/ private def checkUnsupported [Monad m] [MonadEnv m] [MonadError m] (decl : Declaration) : m Unit := do let env ← getEnv decl.forExprM fun e => let unsupportedRecursor? := e.find? fun | Expr.const declName .. => ((isAuxRecursor env declName && !isCasesOnRecursor env declName) || isRecCore env declName) && !supportedRecursors.contains declName | _ => false match unsupportedRecursor? with | some (Expr.const declName ..) => throwError "code generator does not support recursor '{declName}' yet, consider using 'match ... with' and/or structural recursion" | _ => pure () -- Forward declaration @[extern "lean_lcnf_compile_decls"] opaque compileDeclsNew (declNames : List Name) : CoreM Unit def compileDecl (decl : Declaration) : CoreM Unit := do compileDeclsNew (Compiler.getDeclNamesForCodeGen decl) match (← getEnv).compileDecl (← getOptions) decl with | Except.ok env => setEnv env | Except.error (KernelException.other msg) => checkUnsupported decl -- Generate nicer error message for unsupported recursors and axioms throwError msg | Except.error ex => throwKernelException ex def compileDecls (decls : List Name) : CoreM Unit := do compileDeclsNew decls match (← getEnv).compileDecls (← getOptions) decls with | Except.ok env => setEnv env | Except.error (KernelException.other msg) => throwError msg | Except.error ex => throwKernelException ex def addAndCompile (decl : Declaration) : CoreM Unit := do addDecl decl; compileDecl decl def ImportM.runCoreM (x : CoreM Ξ±) : ImportM Ξ± := do let ctx ← read let (a, _) ← x.toIO { options := ctx.opts, fileName := "<ImportM>", fileMap := default } { env := ctx.env } return a end Lean
1d345bb17a9560e96cd151899b9ee06a57a85710
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/order/rel_iso.lean
7abe2d404713da5ce927b42c5ee45a238e8d321e
[ "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
35,469
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 algebra.group.defs import data.equiv.set import logic.embedding import order.rel_classes /-! # Relation homomorphisms, embeddings, isomorphisms This file defines relation homomorphisms, embeddings, isomorphisms and order embeddings and isomorphisms. ## Main declarations * `rel_hom`: Relation homomorphism. A `rel_hom r s` is a function `f : Ξ± β†’ Ξ²` such that `r a b β†’ s (f a) (f b)`. * `rel_embedding`: Relation embedding. A `rel_embedding r s` is an embedding `f : Ξ± β†ͺ Ξ²` such that `r a b ↔ s (f a) (f b)`. * `rel_iso`: Relation isomorphism. A `rel_iso r s` is an equivalence `f : Ξ± ≃ Ξ²` such that `r a b ↔ s (f a) (f b)`. * `order_embedding`: Relation embedding. An `order_embedding Ξ± Ξ²` is an embedding `f : Ξ± β†ͺ Ξ²` such that `a ≀ b ↔ f a ≀ f b`. Defined as an abbreviation of `@rel_embedding Ξ± Ξ² (≀) (≀)`. * `order_iso`: Relation isomorphism. An `order_iso Ξ± Ξ²` is an equivalence `f : Ξ± ≃ Ξ²` such that `a ≀ b ↔ f a ≀ f b`. Defined as an abbreviation of `@rel_iso Ξ± Ξ² (≀) (≀)`. * `sum_lex_congr`, `prod_lex_congr`: Creates a relation homomorphism between two `sum_lex` or two `prod_lex` from relation homomorphisms between their arguments. ## Notation * `β†’r`: `rel_hom` * `β†ͺr`: `rel_embedding` * `≃r`: `rel_iso` * `β†ͺo`: `order_embedding` * `≃o`: `order_iso` -/ open function universes u v w variables {Ξ± Ξ² Ξ³ : 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⟩ initialize_simps_projections rel_hom (to_fun β†’ apply) 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. -/ theorem coe_fn_injective : @function.injective (r β†’r s) (Ξ± β†’ Ξ²) coe_fn | ⟨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_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 homomorphism. -/ @[refl, simps] protected def id (r : Ξ± β†’ Ξ± β†’ Prop) : r β†’r r := ⟨λ x, x, Ξ» a b x, x⟩ /-- Composition of two relation homomorphisms is a relation homomorphism. -/ @[trans, simps] protected def comp (g : s β†’r t) (f : r β†’r s) : r β†’r t := ⟨λ x, g (f x), Ξ» a b h, g.2 (f.2 h)⟩ /-- 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⟩ /-- See Note [custom simps projection]. We need to specify this projection explicitly in this case, because it is a composition of multiple projections. -/ def simps.apply (h : r β†ͺr s) : Ξ± β†’ Ξ² := h initialize_simps_projections rel_embedding (to_embedding_to_fun β†’ apply, -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. -/ theorem coe_fn_injective : @function.injective (r β†ͺr s) (Ξ± β†’ Ξ²) coe_fn | ⟨⟨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_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 embedding. -/ @[refl, simps] 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 _⟩ 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'} /-- To define an relation embedding from an antisymmetric relation `r` to a reflexive relation `s` it suffices to give a function together with a proof that it satisfies `s (f a) (f b) ↔ r a b`. -/ def of_map_rel_iff (f : Ξ± β†’ Ξ²) [is_antisymm Ξ± r] [is_refl Ξ² s] (hf : βˆ€ a b, s (f a) (f b) ↔ r a b) : r β†ͺr s := { to_fun := f, inj' := Ξ» x y h, antisymm ((hf _ _).1 (h β–Έ refl _)) ((hf _ _).1 (h β–Έ refl _)), map_rel_iff' := hf } @[simp] lemma of_map_rel_iff_coe (f : Ξ± β†’ Ξ²) [is_antisymm Ξ± r] [is_refl Ξ² s] (hf : βˆ€ a b, s (f a) (f b) ↔ r a b) : ⇑(of_map_rel_iff f hf : r β†ͺr s) = f := rfl /-- 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 Ξ²) /-- `<` 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⟩ /-- To define an order embedding from a partial order to a preorder it suffices to give a function together with a proof that it satisfies `f a ≀ f b ↔ a ≀ b`. -/ def of_map_le_iff {Ξ± Ξ²} [partial_order Ξ±] [preorder Ξ²] (f : Ξ± β†’ Ξ²) (hf : βˆ€ a b, f a ≀ f b ↔ a ≀ b) : Ξ± β†ͺo Ξ² := rel_embedding.of_map_rel_iff f hf @[simp] lemma coe_of_map_le_iff {Ξ± Ξ²} [partial_order Ξ±] [preorder Ξ²] {f : Ξ± β†’ Ξ²} (h) : ⇑(of_map_le_iff f h) = f := rfl /-- A strictly monotone map from a linear order is an order embedding. --/ def of_strict_mono {Ξ± Ξ²} [linear_order Ξ±] [preorder Ξ²] (f : Ξ± β†’ Ξ²) (h : strict_mono f) : Ξ± β†ͺo Ξ² := of_map_le_iff f (Ξ» _ _, 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`. -/ @[simps {fully_applied := ff}] def subtype (p : Ξ± β†’ Prop) : subtype p β†ͺo Ξ± := ⟨embedding.subtype p, Ξ» x y, iff.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 (r ≃r s) (Ξ± β†’ Ξ²) coe_fn := 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⟩ /-- 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]⟩ /-- See Note [custom simps projection]. We need to specify this projection explicitly in this case, because it is a composition of multiple projections. -/ def simps.apply (h : r ≃r s) : Ξ± β†’ Ξ² := h /-- See Note [custom simps projection]. -/ def simps.symm_apply (h : r ≃r s) : Ξ² β†’ Ξ± := h.symm initialize_simps_projections rel_iso (to_equiv_to_fun β†’ apply, to_equiv_inv_fun β†’ symm_apply, -to_equiv) /-- Identity map is a relation isomorphism. -/ @[refl, simps apply] protected def refl (r : Ξ± β†’ Ξ± β†’ Prop) : r ≃r r := ⟨equiv.refl _, Ξ» a b, iff.rfl⟩ /-- Composition of two relation isomorphisms is a relation isomorphism. -/ @[trans, simps apply] 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 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. -/ @[simps apply] 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⟩ /-- Given relation isomorphisms `r₁ ≃r s₁` and `rβ‚‚ ≃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₁ s₁) (eβ‚‚ : @rel_iso Ξ±β‚‚ Ξ²β‚‚ rβ‚‚ sβ‚‚) : sum.lex r₁ rβ‚‚ ≃r sum.lex s₁ 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 s₁` and `rβ‚‚ ≃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₁ s₁) (eβ‚‚ : @rel_iso Ξ±β‚‚ Ξ²β‚‚ rβ‚‚ sβ‚‚) : prod.lex r₁ rβ‚‚ ≃r prod.lex s₁ 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 /-- Identity order isomorphism. -/ def refl (Ξ± : Type*) [has_le Ξ±] : Ξ± ≃o Ξ± := rel_iso.refl (≀) @[simp] lemma coe_refl : ⇑(refl Ξ±) = id := rfl lemma refl_apply (x : Ξ±) : refl Ξ± x = x := rfl @[simp] lemma refl_to_equiv : (refl Ξ±).to_equiv = equiv.refl Ξ± := rfl /-- 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 @[simp] lemma symm_refl (Ξ± : Type*) [has_le Ξ±] : (refl Ξ±).symm = refl Ξ± := rfl 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 @[simp] lemma symm_image_image (e : Ξ± ≃o Ξ²) (s : set Ξ±) : e.symm '' (e '' s) = s := e.to_equiv.symm_image_image s @[simp] lemma image_symm_image (e : Ξ± ≃o Ξ²) (s : set Ξ²) : e '' (e.symm '' s) = s := e.to_equiv.image_symm_image s lemma image_eq_preimage (e : Ξ± ≃o Ξ²) (s : set Ξ±) : e '' s = e.symm ⁻¹' s := e.to_equiv.image_eq_preimage s @[simp] lemma preimage_symm_preimage (e : Ξ± ≃o Ξ²) (s : set Ξ±) : e ⁻¹' (e.symm ⁻¹' s) = s := e.to_equiv.preimage_symm_preimage s @[simp] lemma symm_preimage_preimage (e : Ξ± ≃o Ξ²) (s : set Ξ²) : e.symm ⁻¹' (e ⁻¹' s) = s := e.to_equiv.symm_preimage_preimage s @[simp] lemma image_preimage (e : Ξ± ≃o Ξ²) (s : set Ξ²) : e '' (e ⁻¹' s) = s := e.to_equiv.image_preimage s @[simp] lemma preimage_image (e : Ξ± ≃o Ξ²) (s : set Ξ±) : e ⁻¹' (e '' s) = s := e.to_equiv.preimage_image s /-- 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 @[simp] lemma refl_trans (e : Ξ± ≃o Ξ²) : (refl Ξ±).trans e = e := by { ext x, refl } @[simp] lemma trans_refl (e : Ξ± ≃o Ξ²) : e.trans (refl Ξ²) = e := by { ext x, refl } end has_le open set section le variables [has_le Ξ±] [has_le Ξ²] [has_le Ξ³] @[simp] lemma le_iff_le (e : Ξ± ≃o Ξ²) {x y : Ξ±} : e x ≀ e y ↔ x ≀ y := e.map_rel_iff lemma le_symm_apply (e : Ξ± ≃o Ξ²) {x : Ξ±} {y : Ξ²} : x ≀ e.symm y ↔ e x ≀ y := e.rel_symm_apply lemma symm_apply_le (e : Ξ± ≃o Ξ²) {x : Ξ±} {y : Ξ²} : e.symm y ≀ x ↔ y ≀ e x := e.symm_apply_rel end le 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 lt_iff_lt (e : Ξ± ≃o Ξ²) {x y : Ξ±} : e x < e y ↔ x < y := e.to_order_embedding.lt_iff_lt /-- 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 } /-- Order isomorphism between `Ξ± β†’ Ξ²` and `Ξ²`, where `Ξ±` has a unique element. -/ @[simps to_equiv apply] def fun_unique (Ξ± Ξ² : Type*) [unique Ξ±] [preorder Ξ²] : (Ξ± β†’ Ξ²) ≃o Ξ² := { to_equiv := equiv.fun_unique Ξ± Ξ², map_rel_iff' := Ξ» f g, by simp [pi.le_def, unique.forall_iff] } @[simp] lemma fun_unique_symm_apply {Ξ± Ξ² : Type*} [unique Ξ±] [preorder Ξ²] : ((fun_unique Ξ± Ξ²).symm : Ξ² β†’ Ξ± β†’ Ξ²) = function.const Ξ± := rfl end order_iso namespace equiv variables [preorder Ξ±] [preorder Ξ²] /-- If `e` is an equivalence with monotone forward and inverse maps, then `e` is an order isomorphism. -/ def to_order_iso (e : Ξ± ≃ Ξ²) (h₁ : monotone e) (hβ‚‚ : monotone e.symm) : Ξ± ≃o Ξ² := ⟨e, Ξ» x y, ⟨λ h, by simpa only [e.symm_apply_apply] using hβ‚‚ h, Ξ» h, h₁ h⟩⟩ @[simp] lemma coe_to_order_iso (e : Ξ± ≃ Ξ²) (h₁ : monotone e) (hβ‚‚ : monotone e.symm) : ⇑(e.to_order_iso h₁ hβ‚‚) = e := rfl @[simp] lemma to_order_iso_to_equiv (e : Ξ± ≃ Ξ²) (h₁ : monotone e) (hβ‚‚ : monotone e.symm) : (e.to_order_iso h₁ hβ‚‚).to_equiv = e := rfl end equiv /-- 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_on.order_iso {Ξ± Ξ²} [linear_order Ξ±] [preorder Ξ²] (f : Ξ± β†’ Ξ²) (s : set Ξ±) (hf : strict_mono_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 [has_le Ξ±] [has_le Ξ²] (f : Ξ± ≃o Ξ²) : order_dual Ξ± ≃o order_dual Ξ² := ⟨f.to_equiv, Ξ» _ _, f.map_rel_iff⟩ section lattice_isos lemma order_iso.map_bot' [has_le Ξ±] [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 [has_le Ξ±] [partial_order Ξ²] [order_bot Ξ±] [order_bot Ξ²] (f : Ξ± ≃o Ξ²) : f βŠ₯ = βŠ₯ := f.map_bot' (Ξ» _, bot_le) (Ξ» _, bot_le) lemma order_iso.map_top' [has_le Ξ±] [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 [has_le Ξ±] [partial_order Ξ²] [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 /-- Note that this goal could also be stated `(disjoint on f) a b` -/ lemma disjoint.map_order_iso [semilattice_inf_bot Ξ±] [semilattice_inf_bot Ξ²] {a b : Ξ±} (f : Ξ± ≃o Ξ²) (ha : disjoint a b) : disjoint (f a) (f b) := begin rw [disjoint, ←f.map_inf, ←f.map_bot], exact f.monotone ha, end @[simp] lemma disjoint_map_order_iso_iff [semilattice_inf_bot Ξ±] [semilattice_inf_bot Ξ²] {a b : Ξ±} (f : Ξ± ≃o Ξ²) : disjoint (f a) (f b) ↔ disjoint a b := ⟨λ h, f.symm_apply_apply a β–Έ f.symm_apply_apply b β–Έ h.map_order_iso f.symm, Ξ» h, h.map_order_iso f⟩ 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 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
99cf52574057fefad7e4db5b05008fd3a9da0251
22e97a5d648fc451e25a06c668dc03ac7ed7bc25
/src/order/fixed_points.lean
a403c99fdf957eb7bb904f22e3d08d0e0218a6dc
[ "Apache-2.0" ]
permissive
keeferrowan/mathlib
f2818da875dbc7780830d09bd4c526b0764a4e50
aad2dfc40e8e6a7e258287a7c1580318e865817e
refs/heads/master
1,661,736,426,952
1,590,438,032,000
1,590,438,032,000
266,892,663
0
0
Apache-2.0
1,590,445,835,000
1,590,445,835,000
null
UTF-8
Lean
false
false
8,788
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, Kenny Lau Fixed point construction on complete lattices. -/ import order.complete_lattice universes u v w variables {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} /-- The set of fixed points of a self-map -/ def fixed_points (f : Ξ± β†’ Ξ±) : set Ξ± := { x | f x = x } section fixedpoint variables [complete_lattice Ξ±] {f : Ξ± β†’ Ξ±} /-- Least fixed point of a monotone function -/ def lfp (f : Ξ± β†’ Ξ±) : Ξ± := Inf {a | f a ≀ a} /-- Greatest fixed point of a monotone function -/ def gfp (f : Ξ± β†’ Ξ±) : Ξ± := Sup {a | a ≀ f a} theorem lfp_le {a : Ξ±} (h : f a ≀ a) : lfp f ≀ a := Inf_le h theorem le_lfp {a : Ξ±} (h : βˆ€b, f b ≀ b β†’ a ≀ b) : a ≀ lfp f := le_Inf h theorem lfp_eq (m : monotone f) : lfp f = f (lfp f) := have f (lfp f) ≀ lfp f, from le_lfp $ assume b, assume h : f b ≀ b, le_trans (m (lfp_le h)) h, le_antisymm (lfp_le (m this)) this theorem lfp_induct {p : Ξ± β†’ Prop} (m : monotone f) (step : βˆ€a, p a β†’ a ≀ lfp f β†’ p (f a)) (sup : βˆ€s, (βˆ€a∈s, p a) β†’ p (Sup s)) : p (lfp f) := let s := {a | a ≀ lfp f ∧ p a} in have p_s : p (Sup s), from sup s (assume a ⟨_, h⟩, h), have Sup s ≀ lfp f, from le_lfp $ assume a, assume h : f a ≀ a, Sup_le $ assume b ⟨b_le, _⟩, le_trans b_le (lfp_le h), have Sup s = lfp f, from le_antisymm this $ lfp_le $ le_Sup ⟨le_trans (m this) $ ge_of_eq $ lfp_eq m, step _ p_s this⟩, this β–Έ p_s theorem monotone_lfp : monotone (@lfp Ξ± _) := assume f g, assume : f ≀ g, le_lfp $ assume a, assume : g a ≀ a, lfp_le $ le_trans (β€Ήf ≀ gβ€Ί a) this theorem le_gfp {a : Ξ±} (h : a ≀ f a) : a ≀ gfp f := le_Sup h theorem gfp_le {a : Ξ±} (h : βˆ€b, b ≀ f b β†’ b ≀ a) : gfp f ≀ a := Sup_le h theorem gfp_eq (m : monotone f) : gfp f = f (gfp f) := have gfp f ≀ f (gfp f), from gfp_le $ assume b, assume h : b ≀ f b, le_trans h (m (le_gfp h)), le_antisymm this (le_gfp (m this)) theorem gfp_induct {p : Ξ± β†’ Prop} (m : monotone f) (step : βˆ€a, p a β†’ gfp f ≀ a β†’ p (f a)) (inf : βˆ€s, (βˆ€a∈s, p a) β†’ p (Inf s)) : p (gfp f) := let s := {a | gfp f ≀ a ∧ p a} in have p_s : p (Inf s), from inf s (assume a ⟨_, h⟩, h), have gfp f ≀ Inf s, from gfp_le $ assume a, assume h : a ≀ f a, le_Inf $ assume b ⟨le_b, _⟩, le_trans (le_gfp h) le_b, have Inf s = gfp f, from le_antisymm (le_gfp $ Inf_le ⟨le_trans (le_of_eq $ gfp_eq m) (m this), step _ p_s this⟩) this, this β–Έ p_s theorem monotone_gfp : monotone (@gfp Ξ± _) := assume f g, assume : f ≀ g, gfp_le $ assume a, assume : a ≀ f a, le_gfp $ le_trans this (β€Ήf ≀ gβ€Ί a) end fixedpoint section fixedpoint_eqn variables [complete_lattice Ξ±] [complete_lattice Ξ²] {f : Ξ² β†’ Ξ±} {g : Ξ± β†’ Ξ²} -- Rolling rule theorem lfp_comp (m_f : monotone f) (m_g : monotone g) : lfp (f ∘ g) = f (lfp (g ∘ f)) := le_antisymm (lfp_le $ m_f $ ge_of_eq $ lfp_eq $ m_g.comp m_f) (le_lfp $ assume a fg_le, le_trans (m_f $ lfp_le $ show (g ∘ f) (g a) ≀ g a, from m_g fg_le) fg_le) theorem gfp_comp (m_f : monotone f) (m_g : monotone g) : gfp (f ∘ g) = f (gfp (g ∘ f)) := le_antisymm (gfp_le $ assume a fg_le, le_trans fg_le $ m_f $ le_gfp $ show g a ≀ (g ∘ f) (g a), from m_g fg_le) (le_gfp $ m_f $ le_of_eq $ gfp_eq $ m_g.comp m_f) -- Diagonal rule theorem lfp_lfp {h : Ξ± β†’ Ξ± β†’ Ξ±} (m : βˆ€β¦ƒa b c d⦄, a ≀ b β†’ c ≀ d β†’ h a c ≀ h b d) : lfp (lfp ∘ h) = lfp (Ξ»x, h x x) := let f := lfp (lfp ∘ h) in have f_eq : f = lfp (h f), from lfp_eq $ monotone.comp monotone_lfp (assume a b h x, m h (le_refl _)) , le_antisymm (lfp_le $ lfp_le $ ge_of_eq $ lfp_eq $ assume a b h, m h h) (lfp_le $ ge_of_eq $ calc f = lfp (h f) : f_eq ... = h f (lfp (h f)) : lfp_eq $ assume a b h, m (le_refl _) h ... = h f f : congr_arg (h f) f_eq.symm) theorem gfp_gfp {h : Ξ± β†’ Ξ± β†’ Ξ±} (m : βˆ€β¦ƒa b c d⦄, a ≀ b β†’ c ≀ d β†’ h a c ≀ h b d) : gfp (gfp ∘ h) = gfp (Ξ»x, h x x) := let f := gfp (gfp ∘ h) in have f_eq : f = gfp (h f), from gfp_eq $ monotone.comp monotone_gfp (assume a b h x, m h (le_refl _)), le_antisymm (le_gfp $ le_of_eq $ calc f = gfp (h f) : f_eq ... = h f (gfp (h f)) : gfp_eq $ assume a b h, m (le_refl _) h ... = h f f : congr_arg (h f) f_eq.symm) (le_gfp $ le_gfp $ le_of_eq $ gfp_eq $ assume a b h, m h h) end fixedpoint_eqn /- The complete lattice of fixed points of a function f -/ namespace fixed_points variables [complete_lattice Ξ±] (f : Ξ± β†’ Ξ±) (hf : monotone f) def prev (x : Ξ±) : Ξ± := gfp (Ξ» z, x βŠ“ f z) def next (x : Ξ±) : Ξ± := lfp (Ξ» z, x βŠ” f z) variable {f} theorem prev_le {x : Ξ±} : prev f x ≀ x := gfp_le $ Ξ» z hz, le_trans hz inf_le_left lemma prev_eq (hf : monotone f) {a : Ξ±} (h : f a ≀ a) : prev f a = f (prev f a) := calc prev f a = a βŠ“ f (prev f a) : gfp_eq $ show monotone (Ξ»z, a βŠ“ f z), from assume x y h, inf_le_inf_left _ (hf h) ... = f (prev f a) : inf_of_le_right $ le_trans (hf prev_le) h def prev_fixed (hf : monotone f) (a : Ξ±) (h : f a ≀ a) : fixed_points f := ⟨prev f a, (prev_eq hf h).symm⟩ theorem next_le {x : Ξ±} : x ≀ next f x := le_lfp $ Ξ» z hz, le_trans le_sup_left hz lemma next_eq (hf : monotone f) {a : Ξ±} (h : a ≀ f a) : next f a = f (next f a) := calc next f a = a βŠ” f (next f a) : lfp_eq $ show monotone (Ξ»z, a βŠ” f z), from assume x y h, sup_le_sup_left (hf h) _ ... = f (next f a) : sup_of_le_right $ le_trans h (hf next_le) def next_fixed (hf : monotone f) (a : Ξ±) (h : a ≀ f a) : fixed_points f := ⟨next f a, (next_eq hf h).symm⟩ variable f theorem sup_le_f_of_fixed_points (x y : fixed_points f) : x.1 βŠ” y.1 ≀ f (x.1 βŠ” y.1) := sup_le (x.2 β–Έ (hf $ show x.1 ≀ f x.1 βŠ” y.1, from x.2.symm β–Έ le_sup_left)) (y.2 β–Έ (hf $ show y.1 ≀ x.1 βŠ” f y.1, from y.2.symm β–Έ le_sup_right)) theorem f_le_inf_of_fixed_points (x y : fixed_points f) : f (x.1 βŠ“ y.1) ≀ x.1 βŠ“ y.1 := le_inf (x.2 β–Έ (hf $ show f (x.1) βŠ“ y.1 ≀ x.1, from x.2.symm β–Έ inf_le_left)) (y.2 β–Έ (hf $ show x.1 βŠ“ f (y.1) ≀ y.1, from y.2.symm β–Έ inf_le_right)) theorem Sup_le_f_of_fixed_points (A : set Ξ±) (HA : A βŠ† fixed_points f) : Sup A ≀ f (Sup A) := Sup_le $ Ξ» x hxA, (HA hxA) β–Έ (hf $ le_Sup hxA) theorem f_le_Inf_of_fixed_points (A : set Ξ±) (HA : A βŠ† fixed_points f) : f (Inf A) ≀ Inf A := le_Inf $ Ξ» x hxA, (HA hxA) β–Έ (hf $ Inf_le hxA) /-- The fixed points of `f` form a complete lattice. This cannot be an instance, since it depends on the monotonicity of `f`. -/ protected def complete_lattice : complete_lattice (fixed_points f) := { le := Ξ»x y, x.1 ≀ y.1, le_refl := Ξ» x, le_refl x, le_trans := Ξ» x y z, le_trans, le_antisymm := Ξ» x y hx hy, subtype.eq $ le_antisymm hx hy, sup := Ξ» x y, next_fixed hf (x.1 βŠ” y.1) (sup_le_f_of_fixed_points f hf x y), le_sup_left := Ξ» x y, show x.1 ≀ _, from le_trans le_sup_left next_le, le_sup_right := Ξ» x y, show y.1 ≀ _, from le_trans le_sup_right next_le, sup_le := Ξ» x y z hxz hyz, lfp_le $ sup_le (sup_le hxz hyz) (z.2.symm β–Έ le_refl z.1), inf := Ξ» x y, prev_fixed hf (x.1 βŠ“ y.1) (f_le_inf_of_fixed_points f hf x y), inf_le_left := Ξ» x y, show _ ≀ x.1, from le_trans prev_le inf_le_left, inf_le_right := Ξ» x y, show _ ≀ y.1, from le_trans prev_le inf_le_right, le_inf := Ξ» x y z hxy hxz, le_gfp $ le_inf (le_inf hxy hxz) (x.2.symm β–Έ le_refl x), top := prev_fixed hf ⊀ le_top, le_top := Ξ» ⟨x, H⟩, le_gfp $ le_inf le_top (H.symm β–Έ le_refl x), bot := next_fixed hf βŠ₯ bot_le, bot_le := Ξ» ⟨x, H⟩, lfp_le $ sup_le bot_le (H.symm β–Έ le_refl x), Sup := Ξ» A, next_fixed hf (Sup $ subtype.val '' A) (Sup_le_f_of_fixed_points f hf (subtype.val '' A) (Ξ» z ⟨x, hx⟩, hx.2 β–Έ x.2)), le_Sup := Ξ» A x hxA, show x.1 ≀ _, from le_trans (le_Sup $ show x.1 ∈ subtype.val '' A, from ⟨x, hxA, rfl⟩) next_le, Sup_le := Ξ» A x Hx, lfp_le $ sup_le (Sup_le $ Ξ» z ⟨y, hyA, hyz⟩, hyz β–Έ Hx y hyA) (x.2.symm β–Έ le_refl x), Inf := Ξ» A, prev_fixed hf (Inf $ subtype.val '' A) (f_le_Inf_of_fixed_points f hf (subtype.val '' A) (Ξ» z ⟨x, hx⟩, hx.2 β–Έ x.2)), le_Inf := Ξ» A x Hx, le_gfp $ le_inf (le_Inf $ Ξ» z ⟨y, hyA, hyz⟩, hyz β–Έ Hx y hyA) (x.2.symm β–Έ le_refl x.1), Inf_le := Ξ» A x hxA, show _ ≀ x.1, from le_trans prev_le (Inf_le $ show x.1 ∈ subtype.val '' A, from ⟨x, hxA, rfl⟩) } end fixed_points
095cfcb81fa34a950728755714f10b5154dec78c
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/algebra/order/to_interval_mod.lean
2f0e79874c36a25cfe0c1cd2b75951401a233b33
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
22,615
lean
/- Copyright (c) 2022 Joseph Myers. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Joseph Myers -/ import algebra.module.basic import algebra.order.archimedean import algebra.periodic import group_theory.quotient_group /-! # Reducing to an interval modulo its length This file defines operations that reduce a number (in an `archimedean` `linear_ordered_add_comm_group`) to a number in a given interval, modulo the length of that interval. ## Main definitions * `to_Ico_div a hb x` (where `hb : 0 < b`): The unique integer such that this multiple of `b`, added to `x`, is in `Ico a (a + b)`. * `to_Ico_mod a hb x` (where `hb : 0 < b`): Reduce `x` to the interval `Ico a (a + b)`. * `to_Ioc_div a hb x` (where `hb : 0 < b`): The unique integer such that this multiple of `b`, added to `x`, is in `Ioc a (a + b)`. * `to_Ioc_mod a hb x` (where `hb : 0 < b`): Reduce `x` to the interval `Ioc a (a + b)`. -/ noncomputable theory section linear_ordered_add_comm_group variables {Ξ± : Type*} [linear_ordered_add_comm_group Ξ±] [archimedean Ξ±] /-- The unique integer such that this multiple of `b`, added to `x`, is in `Ico a (a + b)`. -/ def to_Ico_div (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) : β„€ := (exists_unique_add_zsmul_mem_Ico hb x a).some lemma add_to_Ico_div_zsmul_mem_Ico (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) : x + to_Ico_div a hb x β€’ b ∈ set.Ico a (a + b) := (exists_unique_add_zsmul_mem_Ico hb x a).some_spec.1 lemma eq_to_Ico_div_of_add_zsmul_mem_Ico {a b x : Ξ±} (hb : 0 < b) {y : β„€} (hy : x + y β€’ b ∈ set.Ico a (a + b)) : y = to_Ico_div a hb x := (exists_unique_add_zsmul_mem_Ico hb x a).some_spec.2 y hy /-- The unique integer such that this multiple of `b`, added to `x`, is in `Ioc a (a + b)`. -/ def to_Ioc_div (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) : β„€ := (exists_unique_add_zsmul_mem_Ioc hb x a).some lemma add_to_Ioc_div_zsmul_mem_Ioc (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) : x + to_Ioc_div a hb x β€’ b ∈ set.Ioc a (a + b) := (exists_unique_add_zsmul_mem_Ioc hb x a).some_spec.1 lemma eq_to_Ioc_div_of_add_zsmul_mem_Ioc {a b x : Ξ±} (hb : 0 < b) {y : β„€} (hy : x + y β€’ b ∈ set.Ioc a (a + b)) : y = to_Ioc_div a hb x := (exists_unique_add_zsmul_mem_Ioc hb x a).some_spec.2 y hy /-- Reduce `x` to the interval `Ico a (a + b)`. -/ def to_Ico_mod (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) : Ξ± := x + to_Ico_div a hb x β€’ b /-- Reduce `x` to the interval `Ioc a (a + b)`. -/ def to_Ioc_mod (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) : Ξ± := x + to_Ioc_div a hb x β€’ b lemma to_Ico_mod_mem_Ico (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) : to_Ico_mod a hb x ∈ set.Ico a (a + b) := add_to_Ico_div_zsmul_mem_Ico a hb x lemma to_Ico_mod_mem_Ico' {b : Ξ±} (hb : 0 < b) (x : Ξ±) : to_Ico_mod 0 hb x ∈ set.Ico 0 b := by { convert to_Ico_mod_mem_Ico 0 hb x, exact (zero_add b).symm, } lemma to_Ioc_mod_mem_Ioc (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) : to_Ioc_mod a hb x ∈ set.Ioc a (a + b) := add_to_Ioc_div_zsmul_mem_Ioc a hb x lemma left_le_to_Ico_mod (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) : a ≀ to_Ico_mod a hb x := (set.mem_Ico.1 (to_Ico_mod_mem_Ico a hb x)).1 lemma left_lt_to_Ioc_mod (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) : a < to_Ioc_mod a hb x := (set.mem_Ioc.1 (to_Ioc_mod_mem_Ioc a hb x)).1 lemma to_Ico_mod_lt_right (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) : to_Ico_mod a hb x < a + b := (set.mem_Ico.1 (to_Ico_mod_mem_Ico a hb x)).2 lemma to_Ioc_mod_le_right (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) : to_Ioc_mod a hb x ≀ a + b := (set.mem_Ioc.1 (to_Ioc_mod_mem_Ioc a hb x)).2 @[simp] lemma self_add_to_Ico_div_zsmul (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) : x + to_Ico_div a hb x β€’ b = to_Ico_mod a hb x := rfl @[simp] lemma self_add_to_Ioc_div_zsmul (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) : x + to_Ioc_div a hb x β€’ b = to_Ioc_mod a hb x := rfl @[simp] lemma to_Ico_div_zsmul_add_self (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) : to_Ico_div a hb x β€’ b + x = to_Ico_mod a hb x := by rw [add_comm, to_Ico_mod] @[simp] lemma to_Ioc_div_zsmul_add_self (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) : to_Ioc_div a hb x β€’ b + x = to_Ioc_mod a hb x := by rw [add_comm, to_Ioc_mod] @[simp] lemma to_Ico_mod_sub_self (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) : to_Ico_mod a hb x - x = to_Ico_div a hb x β€’ b := by rw [to_Ico_mod, add_sub_cancel'] @[simp] lemma to_Ioc_mod_sub_self (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) : to_Ioc_mod a hb x - x = to_Ioc_div a hb x β€’ b := by rw [to_Ioc_mod, add_sub_cancel'] @[simp] lemma self_sub_to_Ico_mod (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) : x - to_Ico_mod a hb x = -to_Ico_div a hb x β€’ b := by rw [to_Ico_mod, sub_add_cancel', neg_smul] @[simp] lemma self_sub_to_Ioc_mod (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) : x - to_Ioc_mod a hb x = -to_Ioc_div a hb x β€’ b := by rw [to_Ioc_mod, sub_add_cancel', neg_smul] @[simp] lemma to_Ico_mod_sub_to_Ico_div_zsmul (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) : to_Ico_mod a hb x - to_Ico_div a hb x β€’ b = x := by rw [to_Ico_mod, add_sub_cancel] @[simp] lemma to_Ioc_mod_sub_to_Ioc_div_zsmul (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) : to_Ioc_mod a hb x - to_Ioc_div a hb x β€’ b = x := by rw [to_Ioc_mod, add_sub_cancel] @[simp] lemma to_Ico_div_zsmul_sub_to_Ico_mod (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) : to_Ico_div a hb x β€’ b - to_Ico_mod a hb x = -x := by rw [←neg_sub, to_Ico_mod_sub_to_Ico_div_zsmul] @[simp] lemma to_Ioc_div_zsmul_sub_to_Ioc_mod (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) : to_Ioc_div a hb x β€’ b - to_Ioc_mod a hb x = -x := by rw [←neg_sub, to_Ioc_mod_sub_to_Ioc_div_zsmul] lemma to_Ico_mod_eq_iff {a b x y : Ξ±} (hb : 0 < b) : to_Ico_mod a hb x = y ↔ a ≀ y ∧ y < a + b ∧ βˆƒ z : β„€, y - x = z β€’ b := begin refine ⟨λ h, ⟨h β–Έ left_le_to_Ico_mod a hb x, h β–Έ to_Ico_mod_lt_right a hb x, to_Ico_div a hb x, h β–Έ to_Ico_mod_sub_self a hb x⟩, Ξ» h, _⟩, rcases h with ⟨ha, hab, z, hz⟩, rw sub_eq_iff_eq_add' at hz, subst hz, rw eq_to_Ico_div_of_add_zsmul_mem_Ico hb (set.mem_Ico.2 ⟨ha, hab⟩), refl end lemma to_Ioc_mod_eq_iff {a b x y : Ξ±} (hb : 0 < b) : to_Ioc_mod a hb x = y ↔ a < y ∧ y ≀ a + b ∧ βˆƒ z : β„€, y - x = z β€’ b := begin refine ⟨λ h, ⟨h β–Έ left_lt_to_Ioc_mod a hb x, h β–Έ to_Ioc_mod_le_right a hb x, to_Ioc_div a hb x, h β–Έ to_Ioc_mod_sub_self a hb x⟩, Ξ» h, _⟩, rcases h with ⟨ha, hab, z, hz⟩, rw sub_eq_iff_eq_add' at hz, subst hz, rw eq_to_Ioc_div_of_add_zsmul_mem_Ioc hb (set.mem_Ioc.2 ⟨ha, hab⟩), refl end @[simp] lemma to_Ico_div_apply_left (a : Ξ±) {b : Ξ±} (hb : 0 < b) : to_Ico_div a hb a = 0 := begin refine (eq_to_Ico_div_of_add_zsmul_mem_Ico hb _).symm, simp [hb] end @[simp] lemma to_Ioc_div_apply_left (a : Ξ±) {b : Ξ±} (hb : 0 < b) : to_Ioc_div a hb a = 1 := begin refine (eq_to_Ioc_div_of_add_zsmul_mem_Ioc hb _).symm, simp [hb] end @[simp] lemma to_Ico_mod_apply_left (a : Ξ±) {b : Ξ±} (hb : 0 < b) : to_Ico_mod a hb a = a := begin rw to_Ico_mod_eq_iff hb, refine ⟨le_refl _, lt_add_of_pos_right _ hb, 0, _⟩, simp end @[simp] lemma to_Ioc_mod_apply_left (a : Ξ±) {b : Ξ±} (hb : 0 < b) : to_Ioc_mod a hb a = a + b := begin rw to_Ioc_mod_eq_iff hb, refine ⟨lt_add_of_pos_right _ hb, le_refl _, 1, _⟩, simp end lemma to_Ico_div_apply_right (a : Ξ±) {b : Ξ±} (hb : 0 < b) : to_Ico_div a hb (a + b) = -1 := begin refine (eq_to_Ico_div_of_add_zsmul_mem_Ico hb _).symm, simp [hb] end lemma to_Ioc_div_apply_right (a : Ξ±) {b : Ξ±} (hb : 0 < b) : to_Ioc_div a hb (a + b) = 0 := begin refine (eq_to_Ioc_div_of_add_zsmul_mem_Ioc hb _).symm, simp [hb] end lemma to_Ico_mod_apply_right (a : Ξ±) {b : Ξ±} (hb : 0 < b) : to_Ico_mod a hb (a + b) = a := begin rw to_Ico_mod_eq_iff hb, refine ⟨le_refl _, lt_add_of_pos_right _ hb, -1, _⟩, simp end lemma to_Ioc_mod_apply_right (a : Ξ±) {b : Ξ±} (hb : 0 < b) : to_Ioc_mod a hb (a + b) = a + b := begin rw to_Ioc_mod_eq_iff hb, refine ⟨lt_add_of_pos_right _ hb, le_refl _, 0, _⟩, simp end @[simp] lemma to_Ico_div_add_zsmul (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) (m : β„€) : to_Ico_div a hb (x + m β€’ b) = to_Ico_div a hb x - m := begin refine (eq_to_Ico_div_of_add_zsmul_mem_Ico hb _).symm, convert add_to_Ico_div_zsmul_mem_Ico a hb x using 1, simp [sub_smul] end @[simp] lemma to_Ioc_div_add_zsmul (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) (m : β„€) : to_Ioc_div a hb (x + m β€’ b) = to_Ioc_div a hb x - m := begin refine (eq_to_Ioc_div_of_add_zsmul_mem_Ioc hb _).symm, convert add_to_Ioc_div_zsmul_mem_Ioc a hb x using 1, simp [sub_smul] end @[simp] lemma to_Ico_div_zsmul_add (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) (m : β„€) : to_Ico_div a hb (m β€’ b + x) = to_Ico_div a hb x - m := by rw [add_comm, to_Ico_div_add_zsmul] @[simp] lemma to_Ioc_div_zsmul_add (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) (m : β„€) : to_Ioc_div a hb (m β€’ b + x) = to_Ioc_div a hb x - m := by rw [add_comm, to_Ioc_div_add_zsmul] @[simp] lemma to_Ico_div_sub_zsmul (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) (m : β„€) : to_Ico_div a hb (x - m β€’ b) = to_Ico_div a hb x + m := by rw [sub_eq_add_neg, ←neg_smul, to_Ico_div_add_zsmul, sub_neg_eq_add] @[simp] lemma to_Ioc_div_sub_zsmul (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) (m : β„€) : to_Ioc_div a hb (x - m β€’ b) = to_Ioc_div a hb x + m := by rw [sub_eq_add_neg, ←neg_smul, to_Ioc_div_add_zsmul, sub_neg_eq_add] @[simp] lemma to_Ico_div_add_right (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) : to_Ico_div a hb (x + b) = to_Ico_div a hb x - 1 := begin convert to_Ico_div_add_zsmul a hb x 1, exact (one_zsmul _).symm end @[simp] lemma to_Ioc_div_add_right (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) : to_Ioc_div a hb (x + b) = to_Ioc_div a hb x - 1 := begin convert to_Ioc_div_add_zsmul a hb x 1, exact (one_zsmul _).symm end @[simp] lemma to_Ico_div_add_left (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) : to_Ico_div a hb (b + x) = to_Ico_div a hb x - 1 := by rw [add_comm, to_Ico_div_add_right] @[simp] lemma to_Ioc_div_add_left (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) : to_Ioc_div a hb (b + x) = to_Ioc_div a hb x - 1 := by rw [add_comm, to_Ioc_div_add_right] @[simp] lemma to_Ico_div_sub (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) : to_Ico_div a hb (x - b) = to_Ico_div a hb x + 1 := begin convert to_Ico_div_sub_zsmul a hb x 1, exact (one_zsmul _).symm end @[simp] lemma to_Ioc_div_sub (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) : to_Ioc_div a hb (x - b) = to_Ioc_div a hb x + 1 := begin convert to_Ioc_div_sub_zsmul a hb x 1, exact (one_zsmul _).symm end lemma to_Ico_div_sub' (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x y : Ξ±) : to_Ico_div a hb (x - y) = to_Ico_div (a + y) hb x := begin rw eq_comm, apply eq_to_Ico_div_of_add_zsmul_mem_Ico, rw sub_add_eq_add_sub, obtain ⟨hc, ho⟩ := add_to_Ico_div_zsmul_mem_Ico (a + y) hb x, rw add_right_comm at ho, exact ⟨le_sub_iff_add_le.mpr hc, sub_lt_iff_lt_add.mpr ho⟩, end lemma to_Ioc_div_sub' (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x y : Ξ±) : to_Ioc_div a hb (x - y) = to_Ioc_div (a + y) hb x := begin rw eq_comm, apply eq_to_Ioc_div_of_add_zsmul_mem_Ioc, rw sub_add_eq_add_sub, obtain ⟨ho, hc⟩ := add_to_Ioc_div_zsmul_mem_Ioc (a + y) hb x, rw add_right_comm at hc, exact ⟨lt_sub_iff_add_lt.mpr ho, sub_le_iff_le_add.mpr hc⟩, end lemma to_Ico_div_add_right' (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x y : Ξ±) : to_Ico_div a hb (x + y) = to_Ico_div (a - y) hb x := by rw [←sub_neg_eq_add, to_Ico_div_sub', sub_eq_add_neg] lemma to_Ioc_div_add_right' (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x y : Ξ±) : to_Ioc_div a hb (x + y) = to_Ioc_div (a - y) hb x := by rw [←sub_neg_eq_add, to_Ioc_div_sub', sub_eq_add_neg] lemma to_Ico_div_neg (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) : to_Ico_div a hb (-x) = 1 - to_Ioc_div (-a) hb x := begin suffices : to_Ico_div a hb (-x) = -(to_Ioc_div (-(a + b)) hb x), { rwa [neg_add, ←sub_eq_add_neg, ←to_Ioc_div_add_right', to_Ioc_div_add_right, neg_sub] at this }, rw [eq_neg_iff_eq_neg, eq_comm], apply eq_to_Ioc_div_of_add_zsmul_mem_Ioc, obtain ⟨hc, ho⟩ := add_to_Ico_div_zsmul_mem_Ico a hb (-x), rw [←neg_lt_neg_iff, neg_add (-x), neg_neg, ←neg_smul] at ho, rw [←neg_le_neg_iff, neg_add (-x), neg_neg, ←neg_smul] at hc, refine ⟨ho, hc.trans_eq _⟩, rw [neg_add, neg_add_cancel_right], end lemma to_Ioc_div_neg (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) : to_Ioc_div a hb (-x) = 1 - to_Ico_div (-a) hb x := by rw [←neg_neg x, to_Ico_div_neg, neg_neg, neg_neg, sub_sub_cancel] @[simp] lemma to_Ico_mod_add_zsmul (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) (m : β„€) : to_Ico_mod a hb (x + m β€’ b) = to_Ico_mod a hb x := begin rw [to_Ico_mod, to_Ico_div_add_zsmul, to_Ico_mod, sub_smul], abel end @[simp] lemma to_Ioc_mod_add_zsmul (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) (m : β„€) : to_Ioc_mod a hb (x + m β€’ b) = to_Ioc_mod a hb x := begin rw [to_Ioc_mod, to_Ioc_div_add_zsmul, to_Ioc_mod, sub_smul], abel end @[simp] lemma to_Ico_mod_zsmul_add (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) (m : β„€) : to_Ico_mod a hb (m β€’ b + x) = to_Ico_mod a hb x := by rw [add_comm, to_Ico_mod_add_zsmul] @[simp] lemma to_Ioc_mod_zsmul_add (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) (m : β„€) : to_Ioc_mod a hb (m β€’ b + x) = to_Ioc_mod a hb x := by rw [add_comm, to_Ioc_mod_add_zsmul] @[simp] lemma to_Ico_mod_sub_zsmul (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) (m : β„€) : to_Ico_mod a hb (x - m β€’ b) = to_Ico_mod a hb x := by rw [sub_eq_add_neg, ←neg_smul, to_Ico_mod_add_zsmul] @[simp] lemma to_Ioc_mod_sub_zsmul (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) (m : β„€) : to_Ioc_mod a hb (x - m β€’ b) = to_Ioc_mod a hb x := by rw [sub_eq_add_neg, ←neg_smul, to_Ioc_mod_add_zsmul] @[simp] lemma to_Ico_mod_add_right (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) : to_Ico_mod a hb (x + b) = to_Ico_mod a hb x := begin convert to_Ico_mod_add_zsmul a hb x 1, exact (one_zsmul _).symm end @[simp] lemma to_Ioc_mod_add_right (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) : to_Ioc_mod a hb (x + b) = to_Ioc_mod a hb x := begin convert to_Ioc_mod_add_zsmul a hb x 1, exact (one_zsmul _).symm end @[simp] lemma to_Ico_mod_add_left (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) : to_Ico_mod a hb (b + x) = to_Ico_mod a hb x := by rw [add_comm, to_Ico_mod_add_right] @[simp] lemma to_Ioc_mod_add_left (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) : to_Ioc_mod a hb (b + x) = to_Ioc_mod a hb x := by rw [add_comm, to_Ioc_mod_add_right] @[simp] lemma to_Ico_mod_sub (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) : to_Ico_mod a hb (x - b) = to_Ico_mod a hb x := begin convert to_Ico_mod_sub_zsmul a hb x 1, exact (one_zsmul _).symm end @[simp] lemma to_Ioc_mod_sub (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) : to_Ioc_mod a hb (x - b) = to_Ioc_mod a hb x := begin convert to_Ioc_mod_sub_zsmul a hb x 1, exact (one_zsmul _).symm end lemma to_Ico_mod_sub' (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x y : Ξ±) : to_Ico_mod a hb (x - y) = to_Ico_mod (a + y) hb x - y := by simp_rw [to_Ico_mod, to_Ico_div_sub', sub_add_eq_add_sub] lemma to_Ioc_mod_sub' (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x y : Ξ±) : to_Ioc_mod a hb (x - y) = to_Ioc_mod (a + y) hb x - y := by simp_rw [to_Ioc_mod, to_Ioc_div_sub', sub_add_eq_add_sub] lemma to_Ico_mod_add_right' (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x y : Ξ±) : to_Ico_mod a hb (x + y) = to_Ico_mod (a - y) hb x + y := by simp_rw [to_Ico_mod, to_Ico_div_add_right', add_right_comm] lemma to_Ioc_mod_add_right' (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x y : Ξ±) : to_Ioc_mod a hb (x + y) = to_Ioc_mod (a - y) hb x + y := by simp_rw [to_Ioc_mod, to_Ioc_div_add_right', add_right_comm] lemma to_Ico_mod_neg (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) : to_Ico_mod a hb (-x) = b - to_Ioc_mod (-a) hb x := begin simp_rw [to_Ico_mod, to_Ioc_mod, to_Ico_div_neg, sub_smul, one_smul], abel, end lemma to_Ioc_mod_neg (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) : to_Ioc_mod a hb (-x) = b - to_Ico_mod (-a) hb x := begin simp_rw [to_Ioc_mod, to_Ico_mod, to_Ioc_div_neg, sub_smul, one_smul], abel, end lemma to_Ico_mod_eq_to_Ico_mod (a : Ξ±) {b x y : Ξ±} (hb : 0 < b) : to_Ico_mod a hb x = to_Ico_mod a hb y ↔ βˆƒ z : β„€, y - x = z β€’ b := begin refine ⟨λ h, ⟨to_Ico_div a hb x - to_Ico_div a hb y, _⟩, Ξ» h, _⟩, { conv_lhs { rw [←to_Ico_mod_sub_to_Ico_div_zsmul a hb x, ←to_Ico_mod_sub_to_Ico_div_zsmul a hb y] }, rw [h, sub_smul], abel }, { rcases h with ⟨z, hz⟩, rw sub_eq_iff_eq_add at hz, rw [hz, to_Ico_mod_zsmul_add] } end lemma to_Ioc_mod_eq_to_Ioc_mod (a : Ξ±) {b x y : Ξ±} (hb : 0 < b) : to_Ioc_mod a hb x = to_Ioc_mod a hb y ↔ βˆƒ z : β„€, y - x = z β€’ b := begin refine ⟨λ h, ⟨to_Ioc_div a hb x - to_Ioc_div a hb y, _⟩, Ξ» h, _⟩, { conv_lhs { rw [←to_Ioc_mod_sub_to_Ioc_div_zsmul a hb x, ←to_Ioc_mod_sub_to_Ioc_div_zsmul a hb y] }, rw [h, sub_smul], abel }, { rcases h with ⟨z, hz⟩, rw sub_eq_iff_eq_add at hz, rw [hz, to_Ioc_mod_zsmul_add] } end lemma to_Ico_mod_eq_self {a b x : Ξ±} (hb : 0 < b) : to_Ico_mod a hb x = x ↔ a ≀ x ∧ x < a + b := begin rw to_Ico_mod_eq_iff, refine ⟨λ h, ⟨h.1, h.2.1⟩, Ξ» h, ⟨h.1, h.2, 0, _⟩⟩, simp end lemma to_Ioc_mod_eq_self {a b x : Ξ±} (hb : 0 < b) : to_Ioc_mod a hb x = x ↔ a < x ∧ x ≀ a + b := begin rw to_Ioc_mod_eq_iff, refine ⟨λ h, ⟨h.1, h.2.1⟩, Ξ» h, ⟨h.1, h.2, 0, _⟩⟩, simp end @[simp] lemma to_Ico_mod_to_Ico_mod (a₁ aβ‚‚ : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) : to_Ico_mod a₁ hb (to_Ico_mod aβ‚‚ hb x) = to_Ico_mod a₁ hb x := begin rw to_Ico_mod_eq_to_Ico_mod, exact ⟨-to_Ico_div aβ‚‚ hb x, self_sub_to_Ico_mod aβ‚‚ hb x⟩ end @[simp] lemma to_Ico_mod_to_Ioc_mod (a₁ aβ‚‚ : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) : to_Ico_mod a₁ hb (to_Ioc_mod aβ‚‚ hb x) = to_Ico_mod a₁ hb x := begin rw to_Ico_mod_eq_to_Ico_mod, exact ⟨-to_Ioc_div aβ‚‚ hb x, self_sub_to_Ioc_mod aβ‚‚ hb x⟩ end @[simp] lemma to_Ioc_mod_to_Ioc_mod (a₁ aβ‚‚ : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) : to_Ioc_mod a₁ hb (to_Ioc_mod aβ‚‚ hb x) = to_Ioc_mod a₁ hb x := begin rw to_Ioc_mod_eq_to_Ioc_mod, exact ⟨-to_Ioc_div aβ‚‚ hb x, self_sub_to_Ioc_mod aβ‚‚ hb x⟩ end @[simp] lemma to_Ioc_mod_to_Ico_mod (a₁ aβ‚‚ : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) : to_Ioc_mod a₁ hb (to_Ico_mod aβ‚‚ hb x) = to_Ioc_mod a₁ hb x := begin rw to_Ioc_mod_eq_to_Ioc_mod, exact ⟨-to_Ico_div aβ‚‚ hb x, self_sub_to_Ico_mod aβ‚‚ hb x⟩ end lemma to_Ico_mod_periodic (a : Ξ±) {b : Ξ±} (hb : 0 < b) : function.periodic (to_Ico_mod a hb) b := to_Ico_mod_add_right a hb lemma to_Ioc_mod_periodic (a : Ξ±) {b : Ξ±} (hb : 0 < b) : function.periodic (to_Ioc_mod a hb) b := to_Ioc_mod_add_right a hb /-- `to_Ico_mod` as an equiv from the quotient. -/ @[simps symm_apply] def quotient_add_group.equiv_Ico_mod (a : Ξ±) {b : Ξ±} (hb : 0 < b) : (Ξ± β§Έ add_subgroup.zmultiples b) ≃ set.Ico a (a + b) := { to_fun := Ξ» x, ⟨(to_Ico_mod_periodic a hb).lift x, quotient_add_group.induction_on' x $ to_Ico_mod_mem_Ico a hb⟩, inv_fun := coe, right_inv := Ξ» x, subtype.ext $ (to_Ico_mod_eq_self hb).mpr x.prop, left_inv := Ξ» x, begin induction x using quotient_add_group.induction_on', dsimp, rw [quotient_add_group.eq_iff_sub_mem, to_Ico_mod_sub_self], apply add_subgroup.zsmul_mem_zmultiples, end } @[simp] lemma quotient_add_group.equiv_Ico_mod_coe (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) : quotient_add_group.equiv_Ico_mod a hb ↑x = ⟨to_Ico_mod a hb x, to_Ico_mod_mem_Ico a hb _⟩ := rfl /-- `to_Ioc_mod` as an equiv from the quotient. -/ @[simps symm_apply] def quotient_add_group.equiv_Ioc_mod (a : Ξ±) {b : Ξ±} (hb : 0 < b) : (Ξ± β§Έ add_subgroup.zmultiples b) ≃ set.Ioc a (a + b) := { to_fun := Ξ» x, ⟨(to_Ioc_mod_periodic a hb).lift x, quotient_add_group.induction_on' x $ to_Ioc_mod_mem_Ioc a hb⟩, inv_fun := coe, right_inv := Ξ» x, subtype.ext $ (to_Ioc_mod_eq_self hb).mpr x.prop, left_inv := Ξ» x, begin induction x using quotient_add_group.induction_on', dsimp, rw [quotient_add_group.eq_iff_sub_mem, to_Ioc_mod_sub_self], apply add_subgroup.zsmul_mem_zmultiples, end } @[simp] lemma quotient_add_group.equiv_Ioc_mod_coe (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) : quotient_add_group.equiv_Ioc_mod a hb ↑x = ⟨to_Ioc_mod a hb x, to_Ioc_mod_mem_Ioc a hb _⟩ := rfl end linear_ordered_add_comm_group section linear_ordered_field variables {Ξ± : Type*} [linear_ordered_field Ξ±] [floor_ring Ξ±] lemma to_Ico_div_eq_neg_floor (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) : to_Ico_div a hb x = -⌊(x - a) / bβŒ‹ := begin refine (eq_to_Ico_div_of_add_zsmul_mem_Ico hb _).symm, rw [set.mem_Ico, zsmul_eq_mul, int.cast_neg, neg_mul, ←sub_nonneg, add_comm, add_sub_assoc, add_comm, ←sub_eq_add_neg], refine ⟨int.sub_floor_div_mul_nonneg _ hb, _⟩, rw [add_comm a, ←sub_lt_iff_lt_add, add_sub_assoc, add_comm, ←sub_eq_add_neg], exact int.sub_floor_div_mul_lt _ hb end lemma to_Ioc_div_eq_floor (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) : to_Ioc_div a hb x = ⌊(a + b - x) / bβŒ‹ := begin refine (eq_to_Ioc_div_of_add_zsmul_mem_Ioc hb _).symm, rw [set.mem_Ioc, zsmul_eq_mul, ←sub_nonneg, sub_add_eq_sub_sub], refine ⟨_, int.sub_floor_div_mul_nonneg _ hb⟩, rw [←add_lt_add_iff_right b, add_assoc, add_comm x, ←sub_lt_iff_lt_add, add_comm (_ * _), ←sub_lt_iff_lt_add], exact int.sub_floor_div_mul_lt _ hb end lemma to_Ico_div_zero_one (x : Ξ±) : to_Ico_div (0 : Ξ±) zero_lt_one x = -⌊xβŒ‹ := by simp [to_Ico_div_eq_neg_floor] lemma to_Ico_mod_eq_add_fract_mul (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) : to_Ico_mod a hb x = a + int.fract ((x - a) / b) * b := begin rw [to_Ico_mod, to_Ico_div_eq_neg_floor, int.fract], field_simp [hb.ne.symm], ring end lemma to_Ico_mod_eq_fract_mul {b : Ξ±} (hb : 0 < b) (x : Ξ±) : to_Ico_mod 0 hb x = int.fract (x / b) * b := by simp [to_Ico_mod_eq_add_fract_mul] lemma to_Ioc_mod_eq_sub_fract_mul (a : Ξ±) {b : Ξ±} (hb : 0 < b) (x : Ξ±) : to_Ioc_mod a hb x = a + b - int.fract ((a + b - x) / b) * b := begin rw [to_Ioc_mod, to_Ioc_div_eq_floor, int.fract], field_simp [hb.ne.symm], ring end lemma to_Ico_mod_zero_one (x : Ξ±) : to_Ico_mod (0 : Ξ±) zero_lt_one x = int.fract x := by simp [to_Ico_mod_eq_add_fract_mul] end linear_ordered_field
8b89373c67da2bda60d29c1166f7be86bf943507
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/data/finsupp/lattice.lean
8144dd6568635b6e565eb87730e7e861f9c7f9d9
[ "Apache-2.0" ]
permissive
lacker/mathlib
f2439c743c4f8eb413ec589430c82d0f73b2d539
ddf7563ac69d42cfa4a1bfe41db1fed521bd795f
refs/heads/master
1,671,948,326,773
1,601,479,268,000
1,601,479,268,000
298,686,743
0
0
Apache-2.0
1,601,070,794,000
1,601,070,794,000
null
UTF-8
Lean
false
false
4,240
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.finsupp.basic import algebra.ordered_group /-! # Lattice structure on finsupps This file provides instances of ordered structures on finsupps. -/ open_locale classical noncomputable theory variables {Ξ± : Type*} {Ξ² : Type*} [has_zero Ξ²] {ΞΌ : Type*} [canonically_ordered_add_monoid ΞΌ] variables {Ξ³ : Type*} [canonically_linear_ordered_add_monoid Ξ³] namespace finsupp lemma le_def [partial_order Ξ²] {a b : Ξ± β†’β‚€ Ξ²} : a ≀ b ↔ βˆ€ (s : Ξ±), a s ≀ b s := by refl instance : order_bot (Ξ± β†’β‚€ ΞΌ) := { bot := 0, bot_le := by simp [finsupp.le_def, ← bot_eq_zero], .. finsupp.partial_order} instance [semilattice_inf Ξ²] : semilattice_inf (Ξ± β†’β‚€ Ξ²) := { inf := zip_with (βŠ“) inf_idem, inf_le_left := Ξ» a b c, inf_le_left, inf_le_right := Ξ» a b c, inf_le_right, le_inf := Ξ» a b c h1 h2 s, le_inf (h1 s) (h2 s), ..finsupp.partial_order, } @[simp] lemma inf_apply [semilattice_inf Ξ²] {a : Ξ±} {f g : Ξ± β†’β‚€ Ξ²} : (f βŠ“ g) a = f a βŠ“ g a := rfl @[simp] lemma support_inf {f g : Ξ± β†’β‚€ Ξ³} : (f βŠ“ g).support = f.support ∩ g.support := begin ext, simp only [inf_apply, mem_support_iff, ne.def, finset.mem_union, finset.mem_filter, finset.mem_inter], rw [← decidable.not_or_iff_and_not, ← not_congr], rw inf_eq_min, unfold min, split_ifs; { try {apply or_iff_left_of_imp}, try {apply or_iff_right_of_imp}, intro con, rw con at h, revert h, simp, } end instance [semilattice_sup Ξ²] : semilattice_sup (Ξ± β†’β‚€ Ξ²) := { sup := zip_with (βŠ”) sup_idem, le_sup_left := Ξ» a b c, le_sup_left, le_sup_right := Ξ» a b c, le_sup_right, sup_le := Ξ» a b c h1 h2 s, sup_le (h1 s) (h2 s), ..finsupp.partial_order, } @[simp] lemma sup_apply [semilattice_sup Ξ²] {a : Ξ±} {f g : Ξ± β†’β‚€ Ξ²} : (f βŠ” g) a = f a βŠ” g a := rfl @[simp] lemma support_sup {f g : Ξ± β†’β‚€ Ξ³} : (f βŠ” g).support = f.support βˆͺ g.support := begin ext, simp only [finset.mem_union, mem_support_iff, sup_apply, ne.def, ← bot_eq_zero], rw sup_eq_bot_iff, tauto, end instance lattice [lattice Ξ²] : lattice (Ξ± β†’β‚€ Ξ²) := { .. finsupp.semilattice_inf, .. finsupp.semilattice_sup} instance semilattice_inf_bot : semilattice_inf_bot (Ξ± β†’β‚€ Ξ³) := { ..finsupp.order_bot, ..finsupp.lattice} lemma of_multiset_strict_mono : strict_mono (@finsupp.of_multiset Ξ±) := begin unfold strict_mono, intros, rw lt_iff_le_and_ne at *, split, { rw finsupp.le_iff, intros s hs, repeat {rw finsupp.of_multiset_apply}, rw multiset.le_iff_count at a_1, apply a_1.left }, { have h := a_1.right, contrapose h, simp at *, apply finsupp.equiv_multiset.symm.injective h } end lemma bot_eq_zero : (βŠ₯ : Ξ± β†’β‚€ Ξ³) = 0 := rfl lemma disjoint_iff {x y : Ξ± β†’β‚€ Ξ³} : disjoint x y ↔ disjoint x.support y.support := begin unfold disjoint, repeat {rw le_bot_iff}, rw [finsupp.bot_eq_zero, ← finsupp.support_eq_empty, finsupp.support_inf], refl, end /-- The lattice of `finsupp`s to `β„•` is order-isomorphic to that of `multiset`s. -/ def order_iso_multiset : (Ξ± β†’β‚€ β„•) ≃o multiset Ξ± := ⟨finsupp.equiv_multiset, begin intros a b, unfold finsupp.equiv_multiset, dsimp, rw multiset.le_iff_count, simp only [finsupp.count_to_multiset], refl end ⟩ @[simp] lemma order_iso_multiset_apply {f : Ξ± β†’β‚€ β„•} : order_iso_multiset f = f.to_multiset := rfl @[simp] lemma order_iso_multiset_symm_apply {s : multiset Ξ±} : order_iso_multiset.symm s = s.to_finsupp := by { conv_rhs { rw ← (rel_iso.apply_symm_apply order_iso_multiset) s}, simp } variable [partial_order Ξ²] /-- The order on `finsupp`s over a partial order embeds into the order on functions -/ def order_embedding_to_fun : (Ξ± β†’β‚€ Ξ²) β†ͺo (Ξ± β†’ Ξ²) := ⟨⟨λ (f : Ξ± β†’β‚€ Ξ²) (a : Ξ±), f a, Ξ» f g h, finsupp.ext (Ξ» a, by { dsimp at h, rw h,} )⟩, Ξ» a b, le_def⟩ @[simp] lemma order_embedding_to_fun_apply {f : Ξ± β†’β‚€ Ξ²} {a : Ξ±} : order_embedding_to_fun f a = f a := rfl lemma monotone_to_fun : monotone (finsupp.to_fun : (Ξ± β†’β‚€ Ξ²) β†’ (Ξ± β†’ Ξ²)) := Ξ» f g h a, le_def.1 h a end finsupp
d81bd98998132476f272c0f2a3d3a5f5863f7b1b
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/order/filter/interval_auto.lean
c9b2b97cb7b019969bbe971d82e9ff3379fea76b
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
12,821
lean
/- Copyright (c) 2020 Yury G. Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Yury G. Kudryashov -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.set.intervals.ord_connected import Mathlib.order.filter.lift import Mathlib.order.filter.at_top_bot import Mathlib.PostPort universes u_1 l u_2 namespace Mathlib /-! # Convergence of intervals If both `a` and `b` tend to some filter `l₁`, sometimes this implies that `Ixx a b` tends to `lβ‚‚.lift' powerset`, i.e., for any `s ∈ lβ‚‚` eventually `Ixx a b` becomes a subset of `s`. Here and below `Ixx` is one of `Icc`, `Ico`, `Ioc`, and `Ioo`. We define `filter.tendsto_Ixx_class Ixx l₁ lβ‚‚` to be a typeclass representing this property. The instances provide the best `lβ‚‚` for a given `l₁`. In many cases `l₁ = lβ‚‚` but sometimes we can drop an endpoint from an interval: e.g., we prove `tendsto_Ixx_class Ico (π“Ÿ $ Iic a) (π“Ÿ $ Iio a)`, i.e., if `u₁ n` and `uβ‚‚ n` belong eventually to `Iic a`, then the interval `Ico (u₁ n) (uβ‚‚ n)` is eventually included in `Iio a`. The next table shows β€œoutput” filters `lβ‚‚` for different values of `Ixx` and `l₁`. The instances that need topology are defined in `topology/algebra/ordered`. | Input filter | `Ixx = Icc` | `Ixx = Ico` | `Ixx = Ioc` | `Ixx = Ioo` | | -----------: | :-----------: | :-----------: | :-----------: | :-----------: | | `at_top` | `at_top` | `at_top` | `at_top` | `at_top` | | `at_bot` | `at_bot` | `at_bot` | `at_bot` | `at_bot` | | `pure a` | `pure a` | `βŠ₯` | `βŠ₯` | `βŠ₯` | | `π“Ÿ (Iic a)` | `π“Ÿ (Iic a)` | `π“Ÿ (Iio a)` | `π“Ÿ (Iic a)` | `π“Ÿ (Iio a)` | | `π“Ÿ (Ici a)` | `π“Ÿ (Ici a)` | `π“Ÿ (Ici a)` | `π“Ÿ (Ioi a)` | `π“Ÿ (Ioi a)` | | `π“Ÿ (Ioi a)` | `π“Ÿ (Ioi a)` | `π“Ÿ (Ioi a)` | `π“Ÿ (Ioi a)` | `π“Ÿ (Ioi a)` | | `π“Ÿ (Iio a)` | `π“Ÿ (Iio a)` | `π“Ÿ (Iio a)` | `π“Ÿ (Iio a)` | `π“Ÿ (Iio a)` | | `𝓝 a` | `𝓝 a` | `𝓝 a` | `𝓝 a` | `𝓝 a` | | `𝓝[Iic a] b` | `𝓝[Iic a] b` | `𝓝[Iio a] b` | `𝓝[Iic a] b` | `𝓝[Iio a] b` | | `𝓝[Ici a] b` | `𝓝[Ici a] b` | `𝓝[Ici a] b` | `𝓝[Ioi a] b` | `𝓝[Ioi a] b` | | `𝓝[Ioi a] b` | `𝓝[Ioi a] b` | `𝓝[Ioi a] b` | `𝓝[Ioi a] b` | `𝓝[Ioi a] b` | | `𝓝[Iio a] b` | `𝓝[Iio a] b` | `𝓝[Iio a] b` | `𝓝[Iio a] b` | `𝓝[Iio a] b` | -/ namespace filter /-- A pair of filters `l₁`, `lβ‚‚` has `tendsto_Ixx_class Ixx` property if `Ixx a b` tends to `lβ‚‚.lift' powerset` as `a` and `b` tend to `l₁`. In all instances `Ixx` is one of `Icc`, `Ico`, `Ioc`, or `Ioo`. The instances provide the best `lβ‚‚` for a given `l₁`. In many cases `l₁ = lβ‚‚` but sometimes we can drop an endpoint from an interval: e.g., we prove `tendsto_Ixx_class Ico (π“Ÿ $ Iic a) (π“Ÿ $ Iio a)`, i.e., if `u₁ n` and `uβ‚‚ n` belong eventually to `Iic a`, then the interval `Ico (u₁ n) (uβ‚‚ n)` is eventually included in `Iio a`. We mark `lβ‚‚` as an `out_param` so that Lean can automatically find an appropriate `lβ‚‚` based on `Ixx` and `l₁`. This way, e.g., `tendsto.Ico h₁ hβ‚‚` works without specifying explicitly `lβ‚‚`. -/ class tendsto_Ixx_class {Ξ± : Type u_1} [preorder Ξ±] (Ixx : Ξ± β†’ Ξ± β†’ set Ξ±) (l₁ : filter Ξ±) (lβ‚‚ : outParam (filter Ξ±)) where tendsto_Ixx : tendsto (fun (p : Ξ± Γ— Ξ±) => Ixx (prod.fst p) (prod.snd p)) (filter.prod l₁ l₁) (filter.lift' lβ‚‚ set.powerset) theorem tendsto.Icc {Ξ± : Type u_1} {Ξ² : Type u_2} [preorder Ξ±] {l₁ : filter Ξ±} {lβ‚‚ : filter Ξ±} [tendsto_Ixx_class set.Icc l₁ lβ‚‚] {lb : filter Ξ²} {u₁ : Ξ² β†’ Ξ±} {uβ‚‚ : Ξ² β†’ Ξ±} (h₁ : tendsto u₁ lb l₁) (hβ‚‚ : tendsto uβ‚‚ lb l₁) : tendsto (fun (x : Ξ²) => set.Icc (u₁ x) (uβ‚‚ x)) lb (filter.lift' lβ‚‚ set.powerset) := tendsto.comp tendsto_Ixx_class.tendsto_Ixx (tendsto.prod_mk h₁ hβ‚‚) theorem tendsto.Ioc {Ξ± : Type u_1} {Ξ² : Type u_2} [preorder Ξ±] {l₁ : filter Ξ±} {lβ‚‚ : filter Ξ±} [tendsto_Ixx_class set.Ioc l₁ lβ‚‚] {lb : filter Ξ²} {u₁ : Ξ² β†’ Ξ±} {uβ‚‚ : Ξ² β†’ Ξ±} (h₁ : tendsto u₁ lb l₁) (hβ‚‚ : tendsto uβ‚‚ lb l₁) : tendsto (fun (x : Ξ²) => set.Ioc (u₁ x) (uβ‚‚ x)) lb (filter.lift' lβ‚‚ set.powerset) := tendsto.comp tendsto_Ixx_class.tendsto_Ixx (tendsto.prod_mk h₁ hβ‚‚) theorem tendsto.Ico {Ξ± : Type u_1} {Ξ² : Type u_2} [preorder Ξ±] {l₁ : filter Ξ±} {lβ‚‚ : filter Ξ±} [tendsto_Ixx_class set.Ico l₁ lβ‚‚] {lb : filter Ξ²} {u₁ : Ξ² β†’ Ξ±} {uβ‚‚ : Ξ² β†’ Ξ±} (h₁ : tendsto u₁ lb l₁) (hβ‚‚ : tendsto uβ‚‚ lb l₁) : tendsto (fun (x : Ξ²) => set.Ico (u₁ x) (uβ‚‚ x)) lb (filter.lift' lβ‚‚ set.powerset) := tendsto.comp tendsto_Ixx_class.tendsto_Ixx (tendsto.prod_mk h₁ hβ‚‚) theorem tendsto.Ioo {Ξ± : Type u_1} {Ξ² : Type u_2} [preorder Ξ±] {l₁ : filter Ξ±} {lβ‚‚ : filter Ξ±} [tendsto_Ixx_class set.Ioo l₁ lβ‚‚] {lb : filter Ξ²} {u₁ : Ξ² β†’ Ξ±} {uβ‚‚ : Ξ² β†’ Ξ±} (h₁ : tendsto u₁ lb l₁) (hβ‚‚ : tendsto uβ‚‚ lb l₁) : tendsto (fun (x : Ξ²) => set.Ioo (u₁ x) (uβ‚‚ x)) lb (filter.lift' lβ‚‚ set.powerset) := tendsto.comp tendsto_Ixx_class.tendsto_Ixx (tendsto.prod_mk h₁ hβ‚‚) theorem tendsto_Ixx_class_principal {Ξ± : Type u_1} [preorder Ξ±] {s : set Ξ±} {t : set Ξ±} {Ixx : Ξ± β†’ Ξ± β†’ set Ξ±} : tendsto_Ixx_class Ixx (principal s) (principal t) ↔ βˆ€ (x : Ξ±), x ∈ s β†’ βˆ€ (y : Ξ±), y ∈ s β†’ Ixx x y βŠ† t := sorry theorem tendsto_Ixx_class_inf {Ξ± : Type u_1} [preorder Ξ±] {l₁ : filter Ξ±} {l₁' : filter Ξ±} {lβ‚‚ : filter Ξ±} {lβ‚‚' : filter Ξ±} {Ixx : Ξ± β†’ Ξ± β†’ set Ξ±} [h : tendsto_Ixx_class Ixx l₁ lβ‚‚] [h' : tendsto_Ixx_class Ixx l₁' lβ‚‚'] : tendsto_Ixx_class Ixx (l₁ βŠ“ l₁') (lβ‚‚ βŠ“ lβ‚‚') := sorry theorem tendsto_Ixx_class_of_subset {Ξ± : Type u_1} [preorder Ξ±] {l₁ : filter Ξ±} {lβ‚‚ : filter Ξ±} {Ixx : Ξ± β†’ Ξ± β†’ set Ξ±} {Ixx' : Ξ± β†’ Ξ± β†’ set Ξ±} (h : βˆ€ (a b : Ξ±), Ixx a b βŠ† Ixx' a b) [h' : tendsto_Ixx_class Ixx' l₁ lβ‚‚] : tendsto_Ixx_class Ixx l₁ lβ‚‚ := tendsto_Ixx_class.mk (tendsto_lift'_powerset_mono tendsto_Ixx_class.tendsto_Ixx (eventually_of_forall (iff.mpr prod.forall h))) theorem has_basis.tendsto_Ixx_class {Ξ± : Type u_1} [preorder Ξ±] {ΞΉ : Type u_2} {p : ΞΉ β†’ Prop} {s : ΞΉ β†’ set Ξ±} {l : filter Ξ±} (hl : has_basis l p s) {Ixx : Ξ± β†’ Ξ± β†’ set Ξ±} (H : βˆ€ (i : ΞΉ), p i β†’ βˆ€ (x : Ξ±), x ∈ s i β†’ βˆ€ (y : Ξ±), y ∈ s i β†’ Ixx x y βŠ† s i) : tendsto_Ixx_class Ixx l l := sorry protected instance tendsto_Icc_at_top_at_top {Ξ± : Type u_1} [preorder Ξ±] : tendsto_Ixx_class set.Icc at_top at_top := has_basis.tendsto_Ixx_class (has_basis_infi_principal_finite fun (a : Ξ±) => set.Ici a) fun (s : set Ξ±) (hs : set.finite s) => set.ord_connected_bInter fun (i : Ξ±) (hi : i ∈ s) => set.ord_connected_Ici protected instance tendsto_Ico_at_top_at_top {Ξ± : Type u_1} [preorder Ξ±] : tendsto_Ixx_class set.Ico at_top at_top := tendsto_Ixx_class_of_subset fun (_x _x_1 : Ξ±) => set.Ico_subset_Icc_self protected instance tendsto_Ioc_at_top_at_top {Ξ± : Type u_1} [preorder Ξ±] : tendsto_Ixx_class set.Ioc at_top at_top := tendsto_Ixx_class_of_subset fun (_x _x_1 : Ξ±) => set.Ioc_subset_Icc_self protected instance tendsto_Ioo_at_top_at_top {Ξ± : Type u_1} [preorder Ξ±] : tendsto_Ixx_class set.Ioo at_top at_top := tendsto_Ixx_class_of_subset fun (_x _x_1 : Ξ±) => set.Ioo_subset_Icc_self protected instance tendsto_Icc_at_bot_at_bot {Ξ± : Type u_1} [preorder Ξ±] : tendsto_Ixx_class set.Icc at_bot at_bot := has_basis.tendsto_Ixx_class (has_basis_infi_principal_finite fun (a : Ξ±) => set.Iic a) fun (s : set Ξ±) (hs : set.finite s) => set.ord_connected_bInter fun (i : Ξ±) (hi : i ∈ s) => set.ord_connected_Iic protected instance tendsto_Ico_at_bot_at_bot {Ξ± : Type u_1} [preorder Ξ±] : tendsto_Ixx_class set.Ico at_bot at_bot := tendsto_Ixx_class_of_subset fun (_x _x_1 : Ξ±) => set.Ico_subset_Icc_self protected instance tendsto_Ioc_at_bot_at_bot {Ξ± : Type u_1} [preorder Ξ±] : tendsto_Ixx_class set.Ioc at_bot at_bot := tendsto_Ixx_class_of_subset fun (_x _x_1 : Ξ±) => set.Ioc_subset_Icc_self protected instance tendsto_Ioo_at_bot_at_bot {Ξ± : Type u_1} [preorder Ξ±] : tendsto_Ixx_class set.Ioo at_bot at_bot := tendsto_Ixx_class_of_subset fun (_x _x_1 : Ξ±) => set.Ioo_subset_Icc_self protected instance ord_connected.tendsto_Icc {Ξ± : Type u_1} [preorder Ξ±] {s : set Ξ±} [set.ord_connected s] : tendsto_Ixx_class set.Icc (principal s) (principal s) := iff.mpr tendsto_Ixx_class_principal _inst_2 protected instance tendsto_Ico_Ici_Ici {Ξ± : Type u_1} [preorder Ξ±] {a : Ξ±} : tendsto_Ixx_class set.Ico (principal (set.Ici a)) (principal (set.Ici a)) := tendsto_Ixx_class_of_subset fun (_x _x_1 : Ξ±) => set.Ico_subset_Icc_self protected instance tendsto_Ico_Ioi_Ioi {Ξ± : Type u_1} [preorder Ξ±] {a : Ξ±} : tendsto_Ixx_class set.Ico (principal (set.Ioi a)) (principal (set.Ioi a)) := tendsto_Ixx_class_of_subset fun (_x _x_1 : Ξ±) => set.Ico_subset_Icc_self protected instance tendsto_Ico_Iic_Iio {Ξ± : Type u_1} [preorder Ξ±] {a : Ξ±} : tendsto_Ixx_class set.Ico (principal (set.Iic a)) (principal (set.Iio a)) := iff.mpr tendsto_Ixx_class_principal fun (a_1 : Ξ±) (ha : a_1 ∈ set.Iic a) (b : Ξ±) (hb : b ∈ set.Iic a) (x : Ξ±) (hx : x ∈ set.Ico a_1 b) => lt_of_lt_of_le (and.right hx) hb protected instance tendsto_Ico_Iio_Iio {Ξ± : Type u_1} [preorder Ξ±] {a : Ξ±} : tendsto_Ixx_class set.Ico (principal (set.Iio a)) (principal (set.Iio a)) := tendsto_Ixx_class_of_subset fun (_x _x_1 : Ξ±) => set.Ico_subset_Icc_self protected instance tendsto_Ioc_Ici_Ioi {Ξ± : Type u_1} [preorder Ξ±] {a : Ξ±} : tendsto_Ixx_class set.Ioc (principal (set.Ici a)) (principal (set.Ioi a)) := iff.mpr tendsto_Ixx_class_principal fun (x : Ξ±) (hx : x ∈ set.Ici a) (y : Ξ±) (hy : y ∈ set.Ici a) (t : Ξ±) (ht : t ∈ set.Ioc x y) => lt_of_le_of_lt hx (and.left ht) protected instance tendsto_Ioc_Iic_Iic {Ξ± : Type u_1} [preorder Ξ±] {a : Ξ±} : tendsto_Ixx_class set.Ioc (principal (set.Iic a)) (principal (set.Iic a)) := tendsto_Ixx_class_of_subset fun (_x _x_1 : Ξ±) => set.Ioc_subset_Icc_self protected instance tendsto_Ioc_Iio_Iio {Ξ± : Type u_1} [preorder Ξ±] {a : Ξ±} : tendsto_Ixx_class set.Ioc (principal (set.Iio a)) (principal (set.Iio a)) := tendsto_Ixx_class_of_subset fun (_x _x_1 : Ξ±) => set.Ioc_subset_Icc_self protected instance tendsto_Ioc_Ioi_Ioi {Ξ± : Type u_1} [preorder Ξ±] {a : Ξ±} : tendsto_Ixx_class set.Ioc (principal (set.Ioi a)) (principal (set.Ioi a)) := tendsto_Ixx_class_of_subset fun (_x _x_1 : Ξ±) => set.Ioc_subset_Icc_self protected instance tendsto_Ioo_Ici_Ioi {Ξ± : Type u_1} [preorder Ξ±] {a : Ξ±} : tendsto_Ixx_class set.Ioo (principal (set.Ici a)) (principal (set.Ioi a)) := tendsto_Ixx_class_of_subset fun (_x _x_1 : Ξ±) => set.Ioo_subset_Ioc_self protected instance tendsto_Ioo_Iic_Iio {Ξ± : Type u_1} [preorder Ξ±] {a : Ξ±} : tendsto_Ixx_class set.Ioo (principal (set.Iic a)) (principal (set.Iio a)) := tendsto_Ixx_class_of_subset fun (_x _x_1 : Ξ±) => set.Ioo_subset_Ico_self protected instance tendsto_Ioo_Ioi_Ioi {Ξ± : Type u_1} [preorder Ξ±] {a : Ξ±} : tendsto_Ixx_class set.Ioo (principal (set.Ioi a)) (principal (set.Ioi a)) := tendsto_Ixx_class_of_subset fun (_x _x_1 : Ξ±) => set.Ioo_subset_Ioc_self protected instance tendsto_Ioo_Iio_Iio {Ξ± : Type u_1} [preorder Ξ±] {a : Ξ±} : tendsto_Ixx_class set.Ioo (principal (set.Iio a)) (principal (set.Iio a)) := tendsto_Ixx_class_of_subset fun (_x _x_1 : Ξ±) => set.Ioo_subset_Ioc_self protected instance tendsto_Icc_pure_pure {Ξ² : Type u_2} [partial_order Ξ²] {a : Ξ²} : tendsto_Ixx_class set.Icc (pure a) (pure a) := eq.mpr (id (Eq._oldrec (Eq.refl (tendsto_Ixx_class set.Icc (pure a) (pure a))) (Eq.symm (principal_singleton a)))) (iff.mpr tendsto_Ixx_class_principal set.ord_connected_singleton) protected instance tendsto_Ico_pure_bot {Ξ² : Type u_2} [partial_order Ξ²] {a : Ξ²} : tendsto_Ixx_class set.Ico (pure a) βŠ₯ := sorry protected instance tendsto_Ioc_pure_bot {Ξ² : Type u_2} [partial_order Ξ²] {a : Ξ²} : tendsto_Ixx_class set.Ioc (pure a) βŠ₯ := sorry protected instance tendsto_Ioo_pure_bot {Ξ² : Type u_2} [partial_order Ξ²] {a : Ξ²} : tendsto_Ixx_class set.Ioo (pure a) βŠ₯ := tendsto_Ixx_class_of_subset fun (_x _x_1 : Ξ²) => set.Ioo_subset_Ioc_self end Mathlib
fff4daed97953b63b9af5e16199aac24aaff3ee6
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/algebra/algebra/tower.lean
67678dd229c7fe2340f1795c19f07757d74e2799
[ "Apache-2.0" ]
permissive
AntoineChambert-Loir/mathlib
64aabb896129885f12296a799818061bc90da1ff
07be904260ab6e36a5769680b6012f03a4727134
refs/heads/master
1,693,187,631,771
1,636,719,886,000
1,636,719,886,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
11,909
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 algebra.algebra.subalgebra /-! # Towers of algebras In this file we prove basic facts about towers of algebra. An algebra tower A/S/R is expressed by having instances of `algebra A S`, `algebra R S`, `algebra R A` and `is_scalar_tower R S A`, the later asserting the compatibility condition `(r β€’ s) β€’ a = r β€’ (s β€’ a)`. An important definition is `to_alg_hom R S A`, the canonical `R`-algebra homomorphism `S →ₐ[R] A`. -/ open_locale pointwise universes u v w u₁ v₁ variables (R : Type u) (S : Type v) (A : Type w) (B : Type u₁) (M : Type v₁) namespace algebra variables [comm_semiring R] [semiring A] [algebra R A] variables [add_comm_monoid M] [module R M] [module A M] [is_scalar_tower R A M] variables {A} /-- The `R`-algebra morphism `A β†’ End (M)` corresponding to the representation of the algebra `A` on the `R`-module `M`. This is a stronger version of `distrib_mul_action.to_linear_map`, and could also have been called `algebra.to_module_End`. -/ def lsmul : A →ₐ[R] module.End R M := { to_fun := distrib_mul_action.to_linear_map R M, map_one' := linear_map.ext $ Ξ» _, one_smul A _, map_mul' := Ξ» a b, linear_map.ext $ smul_assoc a b, map_zero' := linear_map.ext $ Ξ» _, zero_smul A _, map_add' := Ξ» a b, linear_map.ext $ Ξ» _, add_smul _ _ _, commutes' := Ξ» r, linear_map.ext $ algebra_map_smul A r, } @[simp] lemma lsmul_coe (a : A) : (lsmul R M a : M β†’ M) = (β€’) a := rfl lemma lmul_algebra_map (x : R) : lmul R A (algebra_map R A x) = algebra.lsmul R A x := eq.symm $ linear_map.ext $ smul_def x end algebra namespace is_scalar_tower section module variables [comm_semiring R] [semiring A] [algebra R A] variables [add_comm_monoid M] [module R M] [module A M] [is_scalar_tower R A M] variables {R} (A) {M} theorem algebra_map_smul (r : R) (x : M) : algebra_map R A r β€’ x = r β€’ x := by rw [algebra.algebra_map_eq_smul_one, smul_assoc, one_smul] end module section semiring variables [comm_semiring R] [comm_semiring S] [semiring A] [semiring B] variables [algebra R S] [algebra S A] [algebra S B] variables {R S A} theorem of_algebra_map_eq [algebra R A] (h : βˆ€ x, algebra_map R A x = algebra_map S A (algebra_map R S x)) : is_scalar_tower R S A := ⟨λ x y z, by simp_rw [algebra.smul_def, ring_hom.map_mul, mul_assoc, h]⟩ /-- See note [partially-applied ext lemmas]. -/ theorem of_algebra_map_eq' [algebra R A] (h : algebra_map R A = (algebra_map S A).comp (algebra_map R S)) : is_scalar_tower R S A := of_algebra_map_eq $ ring_hom.ext_iff.1 h variables (R S A) instance subalgebra (Sβ‚€ : subalgebra R S) : is_scalar_tower Sβ‚€ S A := of_algebra_map_eq $ Ξ» x, rfl variables [algebra R A] [algebra R B] variables [is_scalar_tower R S A] [is_scalar_tower R S B] theorem algebra_map_eq : algebra_map R A = (algebra_map S A).comp (algebra_map R S) := ring_hom.ext $ Ξ» x, by simp_rw [ring_hom.comp_apply, algebra.algebra_map_eq_smul_one, smul_assoc, one_smul] theorem algebra_map_apply (x : R) : algebra_map R A x = algebra_map S A (algebra_map R S x) := by rw [algebra_map_eq R S A, ring_hom.comp_apply] instance subalgebra' (Sβ‚€ : subalgebra R S) : is_scalar_tower R Sβ‚€ A := @is_scalar_tower.of_algebra_map_eq R Sβ‚€ A _ _ _ _ _ _ $ Ξ» _, (is_scalar_tower.algebra_map_apply R S A _ : _) @[ext] lemma algebra.ext {S : Type u} {A : Type v} [comm_semiring S] [semiring A] (h1 h2 : algebra S A) (h : βˆ€ {r : S} {x : A}, (by haveI := h1; exact r β€’ x) = r β€’ x) : h1 = h2 := begin unfreezingI { cases h1 with f1 g1 h11 h12, cases h2 with f2 g2 h21 h22, cases f1, cases f2, }, congr', { ext r x, exact h }, { ext r, erw [← mul_one (g1 r), ← h12, ← mul_one (g2 r), ← h22, h], refl, }, end /-- In a tower, the canonical map from the middle element to the top element is an algebra homomorphism over the bottom element. -/ def to_alg_hom : S →ₐ[R] A := { commutes' := Ξ» _, (algebra_map_apply _ _ _ _).symm, .. algebra_map S A } lemma to_alg_hom_apply (y : S) : to_alg_hom R S A y = algebra_map S A y := rfl @[simp] lemma coe_to_alg_hom : ↑(to_alg_hom R S A) = algebra_map S A := ring_hom.ext $ Ξ» _, rfl @[simp] lemma coe_to_alg_hom' : (to_alg_hom R S A : S β†’ A) = algebra_map S A := rfl variables {R S A B} @[simp, priority 900] lemma _root_.alg_hom.map_algebra_map (f : A →ₐ[S] B) (r : R) : f (algebra_map R A r) = algebra_map R B r := by rw [algebra_map_apply R S A r, f.commutes, ← algebra_map_apply R S B] variables (R) @[simp, priority 900] lemma _root_.alg_hom.comp_algebra_map_of_tower (f : A →ₐ[S] B) : (f : A β†’+* B).comp (algebra_map R A) = algebra_map R B := ring_hom.ext f.map_algebra_map variables (R) {S A B} -- conflicts with is_scalar_tower.subalgebra @[priority 999] instance subsemiring (U : subsemiring S) : is_scalar_tower U S A := of_algebra_map_eq $ Ξ» x, rfl @[nolint instance_priority] instance of_ring_hom {R A B : Type*} [comm_semiring R] [comm_semiring A] [comm_semiring B] [algebra R A] [algebra R B] (f : A →ₐ[R] B) : @is_scalar_tower R A B _ (f.to_ring_hom.to_algebra.to_has_scalar) _ := by { letI := (f : A β†’+* B).to_algebra, exact of_algebra_map_eq (Ξ» x, (f.commutes x).symm) } end semiring end is_scalar_tower section homs variables [comm_semiring R] [comm_semiring S] [semiring A] [semiring B] variables [algebra R S] [algebra S A] [algebra S B] variables [algebra R A] [algebra R B] variables [is_scalar_tower R S A] [is_scalar_tower R S B] variables (R) {A S B} open is_scalar_tower namespace alg_hom /-- R ⟢ S induces S-Alg β₯€ R-Alg -/ def restrict_scalars (f : A →ₐ[S] B) : A →ₐ[R] B := { commutes' := Ξ» r, by { rw [algebra_map_apply R S A, algebra_map_apply R S B], exact f.commutes (algebra_map R S r) }, .. (f : A β†’+* B) } lemma restrict_scalars_apply (f : A →ₐ[S] B) (x : A) : f.restrict_scalars R x = f x := rfl @[simp] lemma coe_restrict_scalars (f : A →ₐ[S] B) : (f.restrict_scalars R : A β†’+* B) = f := rfl @[simp] lemma coe_restrict_scalars' (f : A →ₐ[S] B) : (restrict_scalars R f : A β†’ B) = f := rfl lemma restrict_scalars_injective : function.injective (restrict_scalars R : (A →ₐ[S] B) β†’ (A →ₐ[R] B)) := Ξ» f g h, alg_hom.ext (alg_hom.congr_fun h : _) end alg_hom namespace alg_equiv /-- R ⟢ S induces S-Alg β₯€ R-Alg -/ def restrict_scalars (f : A ≃ₐ[S] B) : A ≃ₐ[R] B := { commutes' := Ξ» r, by { rw [algebra_map_apply R S A, algebra_map_apply R S B], exact f.commutes (algebra_map R S r) }, .. (f : A ≃+* B) } lemma restrict_scalars_apply (f : A ≃ₐ[S] B) (x : A) : f.restrict_scalars R x = f x := rfl @[simp] lemma coe_restrict_scalars (f : A ≃ₐ[S] B) : (f.restrict_scalars R : A ≃+* B) = f := rfl @[simp] lemma coe_restrict_scalars' (f : A ≃ₐ[S] B) : (restrict_scalars R f : A β†’ B) = f := rfl lemma restrict_scalars_injective : function.injective (restrict_scalars R : (A ≃ₐ[S] B) β†’ (A ≃ₐ[R] B)) := Ξ» f g h, alg_equiv.ext (alg_equiv.congr_fun h : _) end alg_equiv end homs namespace subalgebra open is_scalar_tower section semiring variables (R) {S A B} [comm_semiring R] [comm_semiring S] [semiring A] [semiring B] variables [algebra R S] [algebra S A] [algebra R A] [algebra S B] [algebra R B] variables [is_scalar_tower R S A] [is_scalar_tower R S B] /-- Given a scalar tower `R`, `S`, `A` of algebras, reinterpret an `S`-subalgebra of `A` an as an `R`-subalgebra. -/ def restrict_scalars (U : subalgebra S A) : subalgebra R A := { algebra_map_mem' := Ξ» x, by { rw algebra_map_apply R S A, exact U.algebra_map_mem _ }, .. U } @[simp] lemma coe_restrict_scalars {U : subalgebra S A} : (restrict_scalars R U : set A) = (U : set A) := rfl @[simp] lemma restrict_scalars_top : restrict_scalars R (⊀ : subalgebra S A) = ⊀ := set_like.coe_injective rfl @[simp] lemma restrict_scalars_to_submodule {U : subalgebra S A} : (U.restrict_scalars R).to_submodule = U.to_submodule.restrict_scalars R := set_like.coe_injective rfl @[simp] lemma mem_restrict_scalars {U : subalgebra S A} {x : A} : x ∈ restrict_scalars R U ↔ x ∈ U := iff.rfl lemma restrict_scalars_injective : function.injective (restrict_scalars R : subalgebra S A β†’ subalgebra R A) := Ξ» U V H, ext $ Ξ» x, by rw [← mem_restrict_scalars R, H, mem_restrict_scalars] /-- Produces an `R`-algebra map from `U.restrict_scalars R` given an `S`-algebra map from `U`. This is a special case of `alg_hom.restrict_scalars` that can be helpful in elaboration. -/ @[simp] def of_restrict_scalars (U : subalgebra S A) (f : U →ₐ[S] B) : U.restrict_scalars R →ₐ[R] B := f.restrict_scalars R end semiring end subalgebra namespace is_scalar_tower open subalgebra variables [comm_semiring R] [comm_semiring S] [comm_semiring A] variables [algebra R S] [algebra S A] [algebra R A] [is_scalar_tower R S A] theorem adjoin_range_to_alg_hom (t : set A) : (algebra.adjoin (to_alg_hom R S A).range t).restrict_scalars R = (algebra.adjoin S t).restrict_scalars R := subalgebra.ext $ Ξ» z, show z ∈ subsemiring.closure (set.range (algebra_map (to_alg_hom R S A).range A) βˆͺ t : set A) ↔ z ∈ subsemiring.closure (set.range (algebra_map S A) βˆͺ t : set A), from suffices set.range (algebra_map (to_alg_hom R S A).range A) = set.range (algebra_map S A), by rw this, by { ext z, exact ⟨λ ⟨⟨x, y, h1⟩, h2⟩, ⟨y, h2 β–Έ h1⟩, Ξ» ⟨y, hy⟩, ⟨⟨z, y, hy⟩, rfl⟩⟩ } end is_scalar_tower section semiring variables {R S A} variables [comm_semiring R] [semiring S] [add_comm_monoid A] variables [algebra R S] [module S A] [module R A] [is_scalar_tower R S A] namespace submodule open is_scalar_tower theorem smul_mem_span_smul_of_mem {s : set S} {t : set A} {k : S} (hks : k ∈ span R s) {x : A} (hx : x ∈ t) : k β€’ x ∈ span R (s β€’ t) := span_induction hks (Ξ» c hc, subset_span $ set.mem_smul.2 ⟨c, x, hc, hx, rfl⟩) (by { rw zero_smul, exact zero_mem _ }) (Ξ» c₁ cβ‚‚ ih₁ ihβ‚‚, by { rw add_smul, exact add_mem _ ih₁ ihβ‚‚ }) (Ξ» b c hc, by { rw is_scalar_tower.smul_assoc, exact smul_mem _ _ hc }) theorem smul_mem_span_smul {s : set S} (hs : span R s = ⊀) {t : set A} {k : S} {x : A} (hx : x ∈ span R t) : k β€’ x ∈ span R (s β€’ t) := span_induction hx (Ξ» x hx, smul_mem_span_smul_of_mem (hs.symm β–Έ mem_top) hx) (by { rw smul_zero, exact zero_mem _ }) (Ξ» x y ihx ihy, by { rw smul_add, exact add_mem _ ihx ihy }) (Ξ» c x hx, smul_comm c k x β–Έ smul_mem _ _ hx) theorem smul_mem_span_smul' {s : set S} (hs : span R s = ⊀) {t : set A} {k : S} {x : A} (hx : x ∈ span R (s β€’ t)) : k β€’ x ∈ span R (s β€’ t) := span_induction hx (Ξ» x hx, let ⟨p, q, hp, hq, hpq⟩ := set.mem_smul.1 hx in by { rw [← hpq, smul_smul], exact smul_mem_span_smul_of_mem (hs.symm β–Έ mem_top) hq }) (by { rw smul_zero, exact zero_mem _ }) (Ξ» x y ihx ihy, by { rw smul_add, exact add_mem _ ihx ihy }) (Ξ» c x hx, smul_comm c k x β–Έ smul_mem _ _ hx) theorem span_smul {s : set S} (hs : span R s = ⊀) (t : set A) : span R (s β€’ t) = (span S t).restrict_scalars R := le_antisymm (span_le.2 $ Ξ» x hx, let ⟨p, q, hps, hqt, hpqx⟩ := set.mem_smul.1 hx in hpqx β–Έ (span S t).smul_mem p (subset_span hqt)) $ Ξ» p hp, span_induction hp (Ξ» x hx, one_smul S x β–Έ smul_mem_span_smul hs (subset_span hx)) (zero_mem _) (Ξ» _ _, add_mem _) (Ξ» k x hx, smul_mem_span_smul' hs hx) end submodule end semiring section ring namespace algebra variables [comm_semiring R] [ring A] [algebra R A] variables [add_comm_group M] [module A M] [module R M] [is_scalar_tower R A M] lemma lsmul_injective [no_zero_smul_divisors A M] {x : A} (hx : x β‰  0) : function.injective (lsmul R M x) := smul_right_injective _ hx end algebra end ring
08fedf41a3ce6e81ea2e4500f249347db29bc4cd
a8a8012e62ebc4c8550b92736c0b20faab882ba0
/ccat.hlean
20d484a440b33d511e19eddedee4b0ce8165c396
[]
no_license
fpvandoorn/Bergen-Lean
d02d173f79492498b0ee042ae6699a984108c6ef
40638f1e04b6c681858127e85546c9012b62ab63
refs/heads/master
1,610,398,839,774
1,484,840,344,000
1,484,840,344,000
79,229,751
0
2
null
null
null
null
UTF-8
Lean
false
false
3,231
hlean
-- A "concrete infinity-category", a subcategory of the inf-category of types -- An object consists of a type plus some data -- The arrows are the 'good' maps import types.equiv import .equiv import .typeclass open eq equiv is_equiv function typeclass structure cCat.{u v} extends CC:typeclass.{u v} : Type.{(max u v)+1} := (good : Ξ  {A B : obj CC} , (A β†’ B) β†’ Type.{u}) (idwd : Ξ  (A : obj CC), good (Ξ» (x : A) , x)) (invwd : Ξ  {A B : obj CC} (e : A ≃ B) , good e β†’ good e⁻¹) (compwd : Ξ  {A B C : obj CC} (g : B β†’ C) (f : A β†’ B), good g β†’ good f β†’ good (g ∘ f)) (coh_unitr : Ξ  {A B : obj CC} (g : A β†’ B) (p : good g) , compwd g (Ξ» x , x) p (idwd A) = p) namespace cCat variables {CC : cCat} structure arr (A B : obj CC) := (to_fun : A β†’ B) (wd : good CC to_fun) infix ` β†’* `:30 := arr attribute arr.to_fun [coercion] definition arr_inj {A B : obj CC} {f g : A β†’ B} {p : good CC f} {q : good CC g} (f_is_g : f = g) (p_is_q : p =[ f_is_g ] q) : arr.mk f p = arr.mk g q := begin induction f_is_g, apply congr_arg _ _ (arr.mk f), apply eq_of_pathover_idp p_is_q end definition arr_eta {A B : obj CC} (f : arr A B) : f = arr.mk (arr.to_fun f) (arr.wd f) := begin induction f, reflexivity end definition arr_inj' {A B : obj CC} {f g : A β†’* B} (p : arr.to_fun f = arr.to_fun g) (q : arr.wd f =[ p ] arr.wd g) : f = g := begin transitivity _, apply arr_eta, transitivity _, apply arr_inj, exact q, symmetry, exact arr_eta g end definition id (A : obj CC) : A β†’* A := arr.mk (Ξ» x , x) (idwd CC A) definition comp {A B C : obj CC} (g : B β†’* C) (f : A β†’* B) : A β†’* C := begin apply arr.mk (g ∘ f), apply compwd CC g f (arr.wd g) (arr.wd f) end infix ` ∘* `:50 := comp definition unitr {A B : obj CC} (f : A β†’* B) : f ∘* id A = f := begin fapply arr_inj', reflexivity, apply pathover_idp_of_eq, apply coh_unitr end structure cequiv (A B : obj CC) extends e:equiv A B, arr A B infix ` ≃* `:25 := cequiv namespace cequiv variables {A B : obj CC} protected definition cong {f g : A β†’ B} (is_equiv_f : is_equiv f) (is_equiv_g : is_equiv g) (good_f : good CC f) (good_g : good CC g) (p : f = g) : good_f =[ p ] good_g β†’ cequiv.mk f is_equiv_f good_f = cequiv.mk g is_equiv_g good_g := begin induction p, intros good_hom, refine congr_arg2 is_equiv_f is_equiv_g _ _ (cequiv.mk f) _ _, refine @eq_of_pathover_idp (A β†’ B) is_equiv f is_equiv_f is_equiv_g _, apply !is_trunc.is_prop.elimo, exact @eq_of_pathover_idp (A β†’ B) (@good CC A B) f good_f good_g good_hom end protected definition eta (f : A ≃* B) : cequiv.mk f (cequiv.to_is_equiv f) (cequiv.wd f) = f := begin induction f, reflexivity end protected definition symm [symm] [constructor] (f : A ≃* B) : B ≃* A := begin fconstructor, { exact f⁻¹ᡉ }, { apply is_equiv_inv }, { apply invwd, exact arr.wd f } end end cequiv end cCat open cCat
40413a2352c24e212f28bb8df1c3295a7addf75b
a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940
/src/Lean/Compiler/IR/EmitC.lean
81f40e17d916e7ee987c4983054f90144b6e5430
[ "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
25,512
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Runtime import Lean.Compiler.NameMangling import Lean.Compiler.ExportAttr import Lean.Compiler.InitAttr import Lean.Compiler.IR.CompilerM import Lean.Compiler.IR.EmitUtil import Lean.Compiler.IR.NormIds import Lean.Compiler.IR.SimpCase import Lean.Compiler.IR.Boxing namespace Lean.IR.EmitC open ExplicitBoxing (requiresBoxedVersion mkBoxedName isBoxedName) def leanMainFn := "_lean_main" structure Context where env : Environment modName : Name jpMap : JPParamsMap := {} mainFn : FunId := arbitrary mainParams : Array Param := #[] abbrev M := ReaderT Context (EStateM String String) def getEnv : M Environment := Context.env <$> read def getModName : M Name := Context.modName <$> read def getDecl (n : Name) : M Decl := do let env ← getEnv match findEnvDecl env n with | some d => pure d | none => throw s!"unknown declaration '{n}'" @[inline] def emit {Ξ± : Type} [ToString Ξ±] (a : Ξ±) : M Unit := modify fun out => out ++ toString a @[inline] def emitLn {Ξ± : Type} [ToString Ξ±] (a : Ξ±) : M Unit := do emit a; emit "\n" def emitLns {Ξ± : Type} [ToString Ξ±] (as : List Ξ±) : M Unit := as.forM fun a => emitLn a def argToCString (x : Arg) : String := match x with | Arg.var x => toString x | _ => "lean_box(0)" def emitArg (x : Arg) : M Unit := emit (argToCString x) def toCType : IRType β†’ String | IRType.float => "double" | IRType.uint8 => "uint8_t" | IRType.uint16 => "uint16_t" | IRType.uint32 => "uint32_t" | IRType.uint64 => "uint64_t" | IRType.usize => "size_t" | IRType.object => "lean_object*" | IRType.tobject => "lean_object*" | IRType.irrelevant => "lean_object*" | IRType.struct _ _ => panic! "not implemented yet" | IRType.union _ _ => panic! "not implemented yet" def throwInvalidExportName {Ξ± : Type} (n : Name) : M Ξ± := throw s!"invalid export name '{n}'" def toCName (n : Name) : M String := do let env ← getEnv; -- TODO: we should support simple export names only match getExportNameFor env n with | some (Name.str Name.anonymous s _) => pure s | some _ => throwInvalidExportName n | none => if n == `main then pure leanMainFn else pure n.mangle def emitCName (n : Name) : M Unit := toCName n >>= emit def toCInitName (n : Name) : M String := do let env ← getEnv; -- TODO: we should support simple export names only match getExportNameFor env n with | some (Name.str Name.anonymous s _) => pure $ "_init_" ++ s | some _ => throwInvalidExportName n | none => pure ("_init_" ++ n.mangle) def emitCInitName (n : Name) : M Unit := toCInitName n >>= emit def emitFnDeclAux (decl : Decl) (cppBaseName : String) (addExternForConsts : Bool) : M Unit := do let ps := decl.params let env ← getEnv if ps.isEmpty then if isClosedTermName env decl.name then emit "static " else if addExternForConsts then emit "extern " emit (toCType decl.resultType ++ " " ++ cppBaseName) unless ps.isEmpty do emit "(" -- We omit irrelevant parameters for extern constants let ps := if isExternC env decl.name then ps.filter (fun p => !p.ty.isIrrelevant) else ps if ps.size > closureMaxArgs && isBoxedName decl.name then emit "lean_object**" else ps.size.forM fun i => do if i > 0 then emit ", " emit (toCType ps[i].ty) emit ")" emitLn ";" def emitFnDecl (decl : Decl) (addExternForConsts : Bool) : M Unit := do let cppBaseName ← toCName decl.name emitFnDeclAux decl cppBaseName addExternForConsts def emitExternDeclAux (decl : Decl) (cNameStr : String) : M Unit := do let cName := Name.mkSimple cNameStr let env ← getEnv let extC := isExternC env decl.name emitFnDeclAux decl cNameStr (!extC) def emitFnDecls : M Unit := do let env ← getEnv let decls := getDecls env let modDecls : NameSet := decls.foldl (fun s d => s.insert d.name) {} let usedDecls : NameSet := decls.foldl (fun s d => collectUsedDecls env d (s.insert d.name)) {} let usedDecls := usedDecls.toList usedDecls.forM fun n => do let decl ← getDecl n; match getExternNameFor env `c decl.name with | some cName => emitExternDeclAux decl cName | none => emitFnDecl decl (!modDecls.contains n) def emitMainFn : M Unit := do let d ← getDecl `main match d with | Decl.fdecl (f := f) (xs := xs) (type := t) (body := b) .. => do unless xs.size == 2 || xs.size == 1 do throw "invalid main function, incorrect arity when generating code" let env ← getEnv let usesLeanAPI := usesModuleFrom env `Lean if usesLeanAPI then emitLn "void lean_initialize();" else emitLn "void lean_initialize_runtime_module();"; emitLn " #if defined(WIN32) || defined(_WIN32) #include <windows.h> #endif int main(int argc, char ** argv) { #if defined(WIN32) || defined(_WIN32) SetErrorMode(SEM_FAILCRITICALERRORS); #endif lean_object* in; lean_object* res;"; if usesLeanAPI then emitLn "lean_initialize();" else emitLn "lean_initialize_runtime_module();" let modName ← getModName /- We disable panic messages because they do not mesh well with extracted closed terms. See issue #534. We can remove this workaround after we implement issue #467. -/ emitLn "lean_set_panic_messages(false);" emitLn ("res = " ++ mkModuleInitializationFunctionName modName ++ "(lean_io_mk_world());") emitLn "lean_set_panic_messages(true);" emitLns ["lean_io_mark_end_initialization();", "if (lean_io_result_is_ok(res)) {", "lean_dec_ref(res);", "lean_init_task_manager();"]; if xs.size == 2 then emitLns ["in = lean_box(0);", "int i = argc;", "while (i > 1) {", " lean_object* n;", " i--;", " n = lean_alloc_ctor(1,2,0); lean_ctor_set(n, 0, lean_mk_string(argv[i])); lean_ctor_set(n, 1, in);", " in = n;", "}"] emitLn ("res = " ++ leanMainFn ++ "(in, lean_io_mk_world());") else emitLn ("res = " ++ leanMainFn ++ "(lean_io_mk_world());") emitLn "}" emitLns ["if (lean_io_result_is_ok(res)) {", " int ret = lean_unbox(lean_io_result_get_value(res));", " lean_dec_ref(res);", " return ret;", "} else {", " lean_io_result_show_error(res);", " lean_dec_ref(res);", " return 1;", "}"] emitLn "}" | other => throw "function declaration expected" def hasMainFn : M Bool := do let env ← getEnv let decls := getDecls env pure $ decls.any (fun d => d.name == `main) def emitMainFnIfNeeded : M Unit := do if (← hasMainFn) then emitMainFn def emitFileHeader : M Unit := do let env ← getEnv let modName ← getModName emitLn "// Lean compiler output" emitLn ("// Module: " ++ toString modName) emit "// Imports:" env.imports.forM fun m => emit (" " ++ toString m) emitLn "" emitLn "#include <lean/lean.h>" emitLns [ "#if defined(__clang__)", "#pragma clang diagnostic ignored \"-Wunused-parameter\"", "#pragma clang diagnostic ignored \"-Wunused-label\"", "#elif defined(__GNUC__) && !defined(__CLANG__)", "#pragma GCC diagnostic ignored \"-Wunused-parameter\"", "#pragma GCC diagnostic ignored \"-Wunused-label\"", "#pragma GCC diagnostic ignored \"-Wunused-but-set-variable\"", "#endif", "#ifdef __cplusplus", "extern \"C\" {", "#endif" ] def emitFileFooter : M Unit := emitLns [ "#ifdef __cplusplus", "}", "#endif" ] def throwUnknownVar {Ξ± : Type} (x : VarId) : M Ξ± := throw s!"unknown variable '{x}'" def getJPParams (j : JoinPointId) : M (Array Param) := do let ctx ← read; match ctx.jpMap.find? j with | some ps => pure ps | none => throw "unknown join point" def declareVar (x : VarId) (t : IRType) : M Unit := do emit (toCType t); emit " "; emit x; emit "; " def declareParams (ps : Array Param) : M Unit := ps.forM fun p => declareVar p.x p.ty partial def declareVars : FnBody β†’ Bool β†’ M Bool | e@(FnBody.vdecl x t _ b), d => do let ctx ← read if isTailCallTo ctx.mainFn e then pure d else declareVar x t; declareVars b true | FnBody.jdecl j xs _ b, d => do declareParams xs; declareVars b (d || xs.size > 0) | e, d => if e.isTerminal then pure d else declareVars e.body d def emitTag (x : VarId) (xType : IRType) : M Unit := do if xType.isObj then do emit "lean_obj_tag("; emit x; emit ")" else emit x def isIf (alts : Array Alt) : Option (Nat Γ— FnBody Γ— FnBody) := if alts.size != 2 then none else match alts[0] with | Alt.ctor c b => some (c.cidx, b, alts[1].body) | _ => none def emitInc (x : VarId) (n : Nat) (checkRef : Bool) : M Unit := do emit $ if checkRef then (if n == 1 then "lean_inc" else "lean_inc_n") else (if n == 1 then "lean_inc_ref" else "lean_inc_ref_n") emit "("; emit x if n != 1 then emit ", "; emit n emitLn ");" def emitDec (x : VarId) (n : Nat) (checkRef : Bool) : M Unit := do emit (if checkRef then "lean_dec" else "lean_dec_ref"); emit "("; emit x; if n != 1 then emit ", "; emit n emitLn ");" def emitDel (x : VarId) : M Unit := do emit "lean_free_object("; emit x; emitLn ");" def emitSetTag (x : VarId) (i : Nat) : M Unit := do emit "lean_ctor_set_tag("; emit x; emit ", "; emit i; emitLn ");" def emitSet (x : VarId) (i : Nat) (y : Arg) : M Unit := do emit "lean_ctor_set("; emit x; emit ", "; emit i; emit ", "; emitArg y; emitLn ");" def emitOffset (n : Nat) (offset : Nat) : M Unit := do if n > 0 then emit "sizeof(void*)*"; emit n; if offset > 0 then emit " + "; emit offset else emit offset def emitUSet (x : VarId) (n : Nat) (y : VarId) : M Unit := do emit "lean_ctor_set_usize("; emit x; emit ", "; emit n; emit ", "; emit y; emitLn ");" def emitSSet (x : VarId) (n : Nat) (offset : Nat) (y : VarId) (t : IRType) : M Unit := do match t with | IRType.float => emit "lean_ctor_set_float" | IRType.uint8 => emit "lean_ctor_set_uint8" | IRType.uint16 => emit "lean_ctor_set_uint16" | IRType.uint32 => emit "lean_ctor_set_uint32" | IRType.uint64 => emit "lean_ctor_set_uint64" | _ => throw "invalid instruction"; emit "("; emit x; emit ", "; emitOffset n offset; emit ", "; emit y; emitLn ");" def emitJmp (j : JoinPointId) (xs : Array Arg) : M Unit := do let ps ← getJPParams j unless xs.size == ps.size do throw "invalid goto" xs.size.forM fun i => do let p := ps[i] let x := xs[i] emit p.x; emit " = "; emitArg x; emitLn ";" emit "goto "; emit j; emitLn ";" def emitLhs (z : VarId) : M Unit := do emit z; emit " = " def emitArgs (ys : Array Arg) : M Unit := ys.size.forM fun i => do if i > 0 then emit ", " emitArg ys[i] def emitCtorScalarSize (usize : Nat) (ssize : Nat) : M Unit := do if usize == 0 then emit ssize else if ssize == 0 then emit "sizeof(size_t)*"; emit usize else emit "sizeof(size_t)*"; emit usize; emit " + "; emit ssize def emitAllocCtor (c : CtorInfo) : M Unit := do emit "lean_alloc_ctor("; emit c.cidx; emit ", "; emit c.size; emit ", " emitCtorScalarSize c.usize c.ssize; emitLn ");" def emitCtorSetArgs (z : VarId) (ys : Array Arg) : M Unit := ys.size.forM fun i => do emit "lean_ctor_set("; emit z; emit ", "; emit i; emit ", "; emitArg ys[i]; emitLn ");" def emitCtor (z : VarId) (c : CtorInfo) (ys : Array Arg) : M Unit := do emitLhs z; if c.size == 0 && c.usize == 0 && c.ssize == 0 then do emit "lean_box("; emit c.cidx; emitLn ");" else do emitAllocCtor c; emitCtorSetArgs z ys def emitReset (z : VarId) (n : Nat) (x : VarId) : M Unit := do emit "if (lean_is_exclusive("; emit x; emitLn ")) {"; n.forM fun i => do emit " lean_ctor_release("; emit x; emit ", "; emit i; emitLn ");" emit " "; emitLhs z; emit x; emitLn ";"; emitLn "} else {"; emit " lean_dec_ref("; emit x; emitLn ");"; emit " "; emitLhs z; emitLn "lean_box(0);"; emitLn "}" def emitReuse (z : VarId) (x : VarId) (c : CtorInfo) (updtHeader : Bool) (ys : Array Arg) : M Unit := do emit "if (lean_is_scalar("; emit x; emitLn ")) {"; emit " "; emitLhs z; emitAllocCtor c; emitLn "} else {"; emit " "; emitLhs z; emit x; emitLn ";"; if updtHeader then emit " lean_ctor_set_tag("; emit z; emit ", "; emit c.cidx; emitLn ");" emitLn "}"; emitCtorSetArgs z ys def emitProj (z : VarId) (i : Nat) (x : VarId) : M Unit := do emitLhs z; emit "lean_ctor_get("; emit x; emit ", "; emit i; emitLn ");" def emitUProj (z : VarId) (i : Nat) (x : VarId) : M Unit := do emitLhs z; emit "lean_ctor_get_usize("; emit x; emit ", "; emit i; emitLn ");" def emitSProj (z : VarId) (t : IRType) (n offset : Nat) (x : VarId) : M Unit := do emitLhs z; match t with | IRType.float => emit "lean_ctor_get_float" | IRType.uint8 => emit "lean_ctor_get_uint8" | IRType.uint16 => emit "lean_ctor_get_uint16" | IRType.uint32 => emit "lean_ctor_get_uint32" | IRType.uint64 => emit "lean_ctor_get_uint64" | _ => throw "invalid instruction" emit "("; emit x; emit ", "; emitOffset n offset; emitLn ");" def toStringArgs (ys : Array Arg) : List String := ys.toList.map argToCString def emitSimpleExternalCall (f : String) (ps : Array Param) (ys : Array Arg) : M Unit := do emit f; emit "(" -- We must remove irrelevant arguments to extern calls. discard <| ys.size.foldM (fun i (first : Bool) => if ps[i].ty.isIrrelevant then pure first else do unless first do emit ", " emitArg ys[i] pure false) true emitLn ");" pure () def emitExternCall (f : FunId) (ps : Array Param) (extData : ExternAttrData) (ys : Array Arg) : M Unit := match getExternEntryFor extData `c with | some (ExternEntry.standard _ extFn) => emitSimpleExternalCall extFn ps ys | some (ExternEntry.inline _ pat) => do emit (expandExternPattern pat (toStringArgs ys)); emitLn ";" | some (ExternEntry.foreign _ extFn) => emitSimpleExternalCall extFn ps ys | _ => throw s!"failed to emit extern application '{f}'" def emitFullApp (z : VarId) (f : FunId) (ys : Array Arg) : M Unit := do emitLhs z let decl ← getDecl f match decl with | Decl.extern _ ps _ extData => emitExternCall f ps extData ys | _ => emitCName f if ys.size > 0 then emit "("; emitArgs ys; emit ")" emitLn ";" def emitPartialApp (z : VarId) (f : FunId) (ys : Array Arg) : M Unit := do let decl ← getDecl f let arity := decl.params.size; emitLhs z; emit "lean_alloc_closure((void*)("; emitCName f; emit "), "; emit arity; emit ", "; emit ys.size; emitLn ");"; ys.size.forM fun i => do let y := ys[i] emit "lean_closure_set("; emit z; emit ", "; emit i; emit ", "; emitArg y; emitLn ");" def emitApp (z : VarId) (f : VarId) (ys : Array Arg) : M Unit := if ys.size > closureMaxArgs then do emit "{ lean_object* _aargs[] = {"; emitArgs ys; emitLn "};"; emitLhs z; emit "lean_apply_m("; emit f; emit ", "; emit ys.size; emitLn ", _aargs); }" else do emitLhs z; emit "lean_apply_"; emit ys.size; emit "("; emit f; emit ", "; emitArgs ys; emitLn ");" def emitBoxFn (xType : IRType) : M Unit := match xType with | IRType.usize => emit "lean_box_usize" | IRType.uint32 => emit "lean_box_uint32" | IRType.uint64 => emit "lean_box_uint64" | IRType.float => emit "lean_box_float" | other => emit "lean_box" def emitBox (z : VarId) (x : VarId) (xType : IRType) : M Unit := do emitLhs z; emitBoxFn xType; emit "("; emit x; emitLn ");" def emitUnbox (z : VarId) (t : IRType) (x : VarId) : M Unit := do emitLhs z; match t with | IRType.usize => emit "lean_unbox_usize" | IRType.uint32 => emit "lean_unbox_uint32" | IRType.uint64 => emit "lean_unbox_uint64" | IRType.float => emit "lean_unbox_float" | other => emit "lean_unbox"; emit "("; emit x; emitLn ");" def emitIsShared (z : VarId) (x : VarId) : M Unit := do emitLhs z; emit "!lean_is_exclusive("; emit x; emitLn ");" def emitIsTaggedPtr (z : VarId) (x : VarId) : M Unit := do emitLhs z; emit "!lean_is_scalar("; emit x; emitLn ");" def toHexDigit (c : Nat) : String := String.singleton c.digitChar def quoteString (s : String) : String := let q := "\""; let q := s.foldl (fun q c => q ++ if c == '\n' then "\\n" else if c == '\n' then "\\t" else if c == '\\' then "\\\\" else if c == '\"' then "\\\"" else if c.toNat <= 31 then "\\x" ++ toHexDigit (c.toNat / 16) ++ toHexDigit (c.toNat % 16) -- TODO(Leo): we should use `\unnnn` for escaping unicode characters. else String.singleton c) q; q ++ "\"" def emitNumLit (t : IRType) (v : Nat) : M Unit := do if t.isObj then if v < UInt32.size then emit "lean_unsigned_to_nat("; emit v; emit "u)" else emit "lean_cstr_to_nat(\""; emit v; emit "\")" else emit v def emitLit (z : VarId) (t : IRType) (v : LitVal) : M Unit := do emitLhs z; match v with | LitVal.num v => emitNumLit t v; emitLn ";" | LitVal.str v => emit "lean_mk_string("; emit (quoteString v); emitLn ");" def emitVDecl (z : VarId) (t : IRType) (v : Expr) : M Unit := match v with | Expr.ctor c ys => emitCtor z c ys | Expr.reset n x => emitReset z n x | Expr.reuse x c u ys => emitReuse z x c u ys | Expr.proj i x => emitProj z i x | Expr.uproj i x => emitUProj z i x | Expr.sproj n o x => emitSProj z t n o x | Expr.fap c ys => emitFullApp z c ys | Expr.pap c ys => emitPartialApp z c ys | Expr.ap x ys => emitApp z x ys | Expr.box t x => emitBox z x t | Expr.unbox x => emitUnbox z t x | Expr.isShared x => emitIsShared z x | Expr.isTaggedPtr x => emitIsTaggedPtr z x | Expr.lit v => emitLit z t v def isTailCall (x : VarId) (v : Expr) (b : FnBody) : M Bool := do let ctx ← read; match v, b with | Expr.fap f _, FnBody.ret (Arg.var y) => pure $ f == ctx.mainFn && x == y | _, _ => pure false def paramEqArg (p : Param) (x : Arg) : Bool := match x with | Arg.var x => p.x == x | _ => false /- Given `[p_0, ..., p_{n-1}]`, `[y_0, ..., y_{n-1}]`, representing the assignments ``` p_0 := y_0, ... p_{n-1} := y_{n-1} ``` Return true iff we have `(i, j)` where `j > i`, and `y_j == p_i`. That is, we have ``` p_i := y_i, ... p_j := p_i, -- p_i was overwritten above ``` -/ def overwriteParam (ps : Array Param) (ys : Array Arg) : Bool := let n := ps.size; n.any $ fun i => let p := ps[i] (i+1, n).anyI fun j => paramEqArg p ys[j] def emitTailCall (v : Expr) : M Unit := match v with | Expr.fap _ ys => do let ctx ← read let ps := ctx.mainParams unless ps.size == ys.size do throw "invalid tail call" if overwriteParam ps ys then emitLn "{" ps.size.forM fun i => do let p := ps[i] let y := ys[i] unless paramEqArg p y do emit (toCType p.ty); emit " _tmp_"; emit i; emit " = "; emitArg y; emitLn ";" ps.size.forM fun i => do let p := ps[i] let y := ys[i] unless paramEqArg p y do emit p.x; emit " = _tmp_"; emit i; emitLn ";" emitLn "}" else ys.size.forM fun i => do let p := ps[i] let y := ys[i] unless paramEqArg p y do emit p.x; emit " = "; emitArg y; emitLn ";" emitLn "goto _start;" | _ => throw "bug at emitTailCall" mutual partial def emitIf (x : VarId) (xType : IRType) (tag : Nat) (t : FnBody) (e : FnBody) : M Unit := do emit "if ("; emitTag x xType; emit " == "; emit tag; emitLn ")"; emitFnBody t; emitLn "else"; emitFnBody e partial def emitCase (x : VarId) (xType : IRType) (alts : Array Alt) : M Unit := match isIf alts with | some (tag, t, e) => emitIf x xType tag t e | _ => do emit "switch ("; emitTag x xType; emitLn ") {"; let alts := ensureHasDefault alts; alts.forM fun alt => do match alt with | Alt.ctor c b => emit "case "; emit c.cidx; emitLn ":"; emitFnBody b | Alt.default b => emitLn "default: "; emitFnBody b emitLn "}" partial def emitBlock (b : FnBody) : M Unit := do match b with | FnBody.jdecl j xs v b => emitBlock b | d@(FnBody.vdecl x t v b) => let ctx ← read if isTailCallTo ctx.mainFn d then emitTailCall v else emitVDecl x t v emitBlock b | FnBody.inc x n c p b => unless p do emitInc x n c emitBlock b | FnBody.dec x n c p b => unless p do emitDec x n c emitBlock b | FnBody.del x b => emitDel x; emitBlock b | FnBody.setTag x i b => emitSetTag x i; emitBlock b | FnBody.set x i y b => emitSet x i y; emitBlock b | FnBody.uset x i y b => emitUSet x i y; emitBlock b | FnBody.sset x i o y t b => emitSSet x i o y t; emitBlock b | FnBody.mdata _ b => emitBlock b | FnBody.ret x => emit "return "; emitArg x; emitLn ";" | FnBody.case _ x xType alts => emitCase x xType alts | FnBody.jmp j xs => emitJmp j xs | FnBody.unreachable => emitLn "lean_internal_panic_unreachable();" partial def emitJPs : FnBody β†’ M Unit | FnBody.jdecl j xs v b => do emit j; emitLn ":"; emitFnBody v; emitJPs b | e => do unless e.isTerminal do emitJPs e.body partial def emitFnBody (b : FnBody) : M Unit := do emitLn "{" let declared ← declareVars b false if declared then emitLn "" emitBlock b emitJPs b emitLn "}" end def emitDeclAux (d : Decl) : M Unit := do let env ← getEnv let (vMap, jpMap) := mkVarJPMaps d withReader (fun ctx => { ctx with jpMap := jpMap }) do unless hasInitAttr env d.name do match d with | Decl.fdecl (f := f) (xs := xs) (type := t) (body := b) .. => let baseName ← toCName f; if xs.size == 0 then emit "static " emit (toCType t); emit " "; if xs.size > 0 then emit baseName; emit "("; if xs.size > closureMaxArgs && isBoxedName d.name then emit "lean_object** _args" else xs.size.forM fun i => do if i > 0 then emit ", " let x := xs[i] emit (toCType x.ty); emit " "; emit x.x emit ")" else emit ("_init_" ++ baseName ++ "()") emitLn " {"; if xs.size > closureMaxArgs && isBoxedName d.name then xs.size.forM fun i => do let x := xs[i] emit "lean_object* "; emit x.x; emit " = _args["; emit i; emitLn "];" emitLn "_start:"; withReader (fun ctx => { ctx with mainFn := f, mainParams := xs }) (emitFnBody b); emitLn "}" | _ => pure () def emitDecl (d : Decl) : M Unit := do let d := d.normalizeIds; -- ensure we don't have gaps in the variable indices try emitDeclAux d catch err => throw s!"{err}\ncompiling:\n{d}" def emitFns : M Unit := do let env ← getEnv; let decls := getDecls env; decls.reverse.forM emitDecl def emitMarkPersistent (d : Decl) (n : Name) : M Unit := do if d.resultType.isObj then emit "lean_mark_persistent(" emitCName n emitLn ");" def emitDeclInit (d : Decl) : M Unit := do let env ← getEnv let n := d.name if isIOUnitInitFn env n then emit "res = "; emitCName n; emitLn "(lean_io_mk_world());" emitLn "if (lean_io_result_is_error(res)) return res;" emitLn "lean_dec_ref(res);" else if d.params.size == 0 then match getInitFnNameFor? env d.name with | some initFn => emit "res = "; emitCName initFn; emitLn "(lean_io_mk_world());" emitLn "if (lean_io_result_is_error(res)) return res;" emitCName n; emitLn " = lean_io_result_get_value(res);" emitMarkPersistent d n emitLn "lean_dec_ref(res);" | _ => emitCName n; emit " = "; emitCInitName n; emitLn "();"; emitMarkPersistent d n def emitInitFn : M Unit := do let env ← getEnv let modName ← getModName env.imports.forM fun imp => emitLn ("lean_object* " ++ mkModuleInitializationFunctionName imp.module ++ "(lean_object*);") emitLns [ "static bool _G_initialized = false;", "lean_object* " ++ mkModuleInitializationFunctionName modName ++ "(lean_object* w) {", "lean_object * res;", "if (_G_initialized) return lean_io_result_mk_ok(lean_box(0));", "_G_initialized = true;" ] env.imports.forM fun imp => emitLns [ "res = " ++ mkModuleInitializationFunctionName imp.module ++ "(lean_io_mk_world());", "if (lean_io_result_is_error(res)) return res;", "lean_dec_ref(res);"] let decls := getDecls env decls.reverse.forM emitDeclInit emitLns ["return lean_io_result_mk_ok(lean_box(0));", "}"] def main : M Unit := do emitFileHeader emitFnDecls emitFns emitInitFn emitMainFnIfNeeded emitFileFooter end EmitC @[export lean_ir_emit_c] def emitC (env : Environment) (modName : Name) : Except String String := match (EmitC.main { env := env, modName := modName }).run "" with | EStateM.Result.ok _ s => Except.ok s | EStateM.Result.error err _ => Except.error err end Lean.IR
6567599d52b8ceffc308a991182af486e9d57acd
4727251e0cd73359b15b664c3170e5d754078599
/src/algebra/quandle.lean
866d9c0752fe89d7d449250e0af588251cfabc0d
[ "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
24,691
lean
/- Copyright (c) 2020 Kyle Miller. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kyle Miller -/ import algebra.hom.equiv import data.zmod.basic import tactic.group /-! # Racks and Quandles This file defines racks and quandles, algebraic structures for sets that bijectively act on themselves with a self-distributivity property. If `R` is a rack and `act : R β†’ (R ≃ R)` is the self-action, then the self-distributivity is, equivalently, that ``` act (act x y) = act x * act y * (act x)⁻¹ ``` where multiplication is composition in `R ≃ R` as a group. Quandles are racks such that `act x x = x` for all `x`. One example of a quandle (not yet in mathlib) is the action of a Lie algebra on itself, defined by `act x y = Ad (exp x) y`. Quandles and racks were independently developed by multiple mathematicians. David Joyce introduced quandles in his thesis [Joyce1982] to define an algebraic invariant of knot and link complements that is analogous to the fundamental group of the exterior, and he showed that the quandle associated to an oriented knot is invariant up to orientation-reversed mirror image. Racks were used by Fenn and Rourke for framed codimension-2 knots and links.[FennRourke1992] The name "rack" came from wordplay by Conway and Wraith for the "wrack and ruin" of forgetting everything but the conjugation operation for a group. ## Main definitions * `shelf` is a type with a self-distributive action * `rack` is a shelf whose action for each element is invertible * `quandle` is a rack whose action for an element fixes that element * `quandle.conj` defines a quandle of a group acting on itself by conjugation. * `shelf_hom` is homomorphisms of shelves, racks, and quandles. * `rack.envel_group` gives the universal group the rack maps to as a conjugation quandle. * `rack.opp` gives the rack with the action replaced by its inverse. ## Main statements * `rack.envel_group` is left adjoint to `quandle.conj` (`to_envel_group.map`). The universality statements are `to_envel_group.univ` and `to_envel_group.univ_uniq`. ## Notation The following notation is localized in `quandles`: * `x β—ƒ y` is `shelf.act x y` * `x ◃⁻¹ y` is `rack.inv_act x y` * `S β†’β—ƒ S'` is `shelf_hom S S'` Use `open_locale quandles` to use these. ## Todo * If `g` is the Lie algebra of a Lie group `G`, then `(x β—ƒ y) = Ad (exp x) x` forms a quandle. * If `X` is a symmetric space, then each point has a corresponding involution that acts on `X`, forming a quandle. * Alexander quandle with `a β—ƒ b = t * b + (1 - t) * b`, with `a` and `b` elements of a module over `Z[t,t⁻¹]`. * If `G` is a group, `H` a subgroup, and `z` in `H`, then there is a quandle `(G/H;z)` defined by `yH β—ƒ xH = yzy⁻¹xH`. Every homogeneous quandle (i.e., a quandle `Q` whose automorphism group acts transitively on `Q` as a set) is isomorphic to such a quandle. There is a generalization to this arbitrary quandles in [Joyce's paper (Theorem 7.2)][Joyce1982]. ## Tags rack, quandle -/ open mul_opposite universes u v /-- A *shelf* is a structure with a self-distributive binary operation. The binary operation is regarded as a left action of the type on itself. -/ class shelf (Ξ± : Type u) := (act : Ξ± β†’ Ξ± β†’ Ξ±) (self_distrib : βˆ€ {x y z : Ξ±}, act x (act y z) = act (act x y) (act x z)) /-- The type of homomorphisms between shelves. This is also the notion of rack and quandle homomorphisms. -/ @[ext] structure shelf_hom (S₁ : Type*) (Sβ‚‚ : Type*) [shelf S₁] [shelf Sβ‚‚] := (to_fun : S₁ β†’ Sβ‚‚) (map_act' : βˆ€ {x y : S₁}, to_fun (shelf.act x y) = shelf.act (to_fun x) (to_fun y)) /-- A *rack* is an automorphic set (a set with an action on itself by bijections) that is self-distributive. It is a shelf such that each element's action is invertible. The notations `x β—ƒ y` and `x ◃⁻¹ y` denote the action and the inverse action, respectively, and they are right associative. -/ class rack (Ξ± : Type u) extends shelf Ξ± := (inv_act : Ξ± β†’ Ξ± β†’ Ξ±) (left_inv : βˆ€ x, function.left_inverse (inv_act x) (act x)) (right_inv : βˆ€ x, function.right_inverse (inv_act x) (act x)) localized "infixr ` β—ƒ `:65 := shelf.act" in quandles localized "infixr ` ◃⁻¹ `:65 := rack.inv_act" in quandles localized "infixr ` β†’β—ƒ `:25 := shelf_hom" in quandles open_locale quandles namespace rack variables {R : Type*} [rack R] lemma self_distrib {x y z : R} : x β—ƒ (y β—ƒ z) = (x β—ƒ y) β—ƒ (x β—ƒ z) := shelf.self_distrib /-- A rack acts on itself by equivalences. -/ def act (x : R) : R ≃ R := { to_fun := shelf.act x, inv_fun := inv_act x, left_inv := left_inv x, right_inv := right_inv x } @[simp] lemma act_apply (x y : R) : act x y = x β—ƒ y := rfl @[simp] lemma act_symm_apply (x y : R) : (act x).symm y = x ◃⁻¹ y := rfl @[simp] lemma inv_act_apply (x y : R) : (act x)⁻¹ y = x ◃⁻¹ y := rfl @[simp] lemma inv_act_act_eq (x y : R) : x ◃⁻¹ x β—ƒ y = y := left_inv x y @[simp] lemma act_inv_act_eq (x y : R) : x β—ƒ x ◃⁻¹ y = y := right_inv x y lemma left_cancel (x : R) {y y' : R} : x β—ƒ y = x β—ƒ y' ↔ y = y' := by { split, apply (act x).injective, rintro rfl, refl } lemma left_cancel_inv (x : R) {y y' : R} : x ◃⁻¹ y = x ◃⁻¹ y' ↔ y = y' := by { split, apply (act x).symm.injective, rintro rfl, refl } lemma self_distrib_inv {x y z : R} : x ◃⁻¹ y ◃⁻¹ z = (x ◃⁻¹ y) ◃⁻¹ (x ◃⁻¹ z) := begin rw [←left_cancel (x ◃⁻¹ y), right_inv, ←left_cancel x, right_inv, self_distrib], repeat {rw right_inv }, end /-- The *adjoint action* of a rack on itself is `op'`, and the adjoint action of `x β—ƒ y` is the conjugate of the action of `y` by the action of `x`. It is another way to understand the self-distributivity axiom. This is used in the natural rack homomorphism `to_conj` from `R` to `conj (R ≃ R)` defined by `op'`. -/ lemma ad_conj {R : Type*} [rack R] (x y : R) : act (x β—ƒ y) = act x * act y * (act x)⁻¹ := begin apply @mul_right_cancel _ _ _ (act x), ext z, simp only [inv_mul_cancel_right], apply self_distrib.symm, end /-- The opposite rack, swapping the roles of `β—ƒ` and `◃⁻¹`. -/ instance opposite_rack : rack Rᡐᡒᡖ := { act := Ξ» x y, op (inv_act (unop x) (unop y)), self_distrib := mul_opposite.rec $ Ξ» x, mul_opposite.rec $ Ξ» y, mul_opposite.rec $ Ξ» z, begin simp only [unop_op, op_inj], exact self_distrib_inv, end, inv_act := Ξ» x y, op (shelf.act (unop x) (unop y)), left_inv := mul_opposite.rec $ Ξ» x, mul_opposite.rec $ Ξ» y, by simp, right_inv := mul_opposite.rec $ Ξ» x, mul_opposite.rec $ Ξ» y, by simp } @[simp] lemma op_act_op_eq {x y : R} : (op x) β—ƒ (op y) = op (x ◃⁻¹ y) := rfl @[simp] lemma op_inv_act_op_eq {x y : R} : (op x) ◃⁻¹ (op y) = op (x β—ƒ y) := rfl @[simp] lemma self_act_act_eq {x y : R} : (x β—ƒ x) β—ƒ y = x β—ƒ y := by { rw [←right_inv x y, ←self_distrib] } @[simp] lemma self_inv_act_inv_act_eq {x y : R} : (x ◃⁻¹ x) ◃⁻¹ y = x ◃⁻¹ y := by { have h := @self_act_act_eq _ _ (op x) (op y), simpa using h } @[simp] lemma self_act_inv_act_eq {x y : R} : (x β—ƒ x) ◃⁻¹ y = x ◃⁻¹ y := by { rw ←left_cancel (x β—ƒ x), rw right_inv, rw self_act_act_eq, rw right_inv } @[simp] lemma self_inv_act_act_eq {x y : R} : (x ◃⁻¹ x) β—ƒ y = x β—ƒ y := by { have h := @self_act_inv_act_eq _ _ (op x) (op y), simpa using h } lemma self_act_eq_iff_eq {x y : R} : x β—ƒ x = y β—ƒ y ↔ x = y := begin split, swap, rintro rfl, refl, intro h, transitivity (x β—ƒ x) ◃⁻¹ (x β—ƒ x), rw [←left_cancel (x β—ƒ x), right_inv, self_act_act_eq], rw [h, ←left_cancel (y β—ƒ y), right_inv, self_act_act_eq], end lemma self_inv_act_eq_iff_eq {x y : R} : x ◃⁻¹ x = y ◃⁻¹ y ↔ x = y := by { have h := @self_act_eq_iff_eq _ _ (op x) (op y), simpa using h } /-- The map `x ↦ x β—ƒ x` is a bijection. (This has applications for the regular isotopy version of the Reidemeister I move for knot diagrams.) -/ def self_apply_equiv (R : Type*) [rack R] : R ≃ R := { to_fun := Ξ» x, x β—ƒ x, inv_fun := Ξ» x, x ◃⁻¹ x, left_inv := Ξ» x, by simp, right_inv := Ξ» x, by simp } /-- An involutory rack is one for which `rack.op R x` is an involution for every x. -/ def is_involutory (R : Type*) [rack R] : Prop := βˆ€ x : R, function.involutive (shelf.act x) lemma involutory_inv_act_eq_act {R : Type*} [rack R] (h : is_involutory R) (x y : R) : x ◃⁻¹ y = x β—ƒ y := begin rw [←left_cancel x, right_inv], exact ((h x).left_inverse y).symm, end /-- An abelian rack is one for which the mediality axiom holds. -/ def is_abelian (R : Type*) [rack R] : Prop := βˆ€ (x y z w : R), (x β—ƒ y) β—ƒ (z β—ƒ w) = (x β—ƒ z) β—ƒ (y β—ƒ w) /-- Associative racks are uninteresting. -/ lemma assoc_iff_id {R : Type*} [rack R] {x y z : R} : x β—ƒ y β—ƒ z = (x β—ƒ y) β—ƒ z ↔ x β—ƒ z = z := by { rw self_distrib, rw left_cancel } end rack namespace shelf_hom variables {S₁ : Type*} {Sβ‚‚ : Type*} {S₃ : Type*} [shelf S₁] [shelf Sβ‚‚] [shelf S₃] instance : has_coe_to_fun (S₁ β†’β—ƒ Sβ‚‚) (Ξ» _, S₁ β†’ Sβ‚‚) := ⟨shelf_hom.to_fun⟩ @[simp] lemma to_fun_eq_coe (f : S₁ β†’β—ƒ Sβ‚‚) : f.to_fun = f := rfl @[simp] lemma map_act (f : S₁ β†’β—ƒ Sβ‚‚) {x y : S₁} : f (x β—ƒ y) = f x β—ƒ f y := map_act' f /-- The identity homomorphism -/ def id (S : Type*) [shelf S] : S β†’β—ƒ S := { to_fun := id, map_act' := by simp } instance inhabited (S : Type*) [shelf S] : inhabited (S β†’β—ƒ S) := ⟨id S⟩ /-- The composition of shelf homomorphisms -/ def comp (g : Sβ‚‚ β†’β—ƒ S₃) (f : S₁ β†’β—ƒ Sβ‚‚) : S₁ β†’β—ƒ S₃ := { to_fun := g.to_fun ∘ f.to_fun, map_act' := by simp } @[simp] lemma comp_apply (g : Sβ‚‚ β†’β—ƒ S₃) (f : S₁ β†’β—ƒ Sβ‚‚) (x : S₁) : (g.comp f) x = g (f x) := rfl end shelf_hom /-- A quandle is a rack such that each automorphism fixes its corresponding element. -/ class quandle (Ξ± : Type*) extends rack Ξ± := (fix : βˆ€ {x : Ξ±}, act x x = x) namespace quandle open rack variables {Q : Type*} [quandle Q] attribute [simp] fix @[simp] lemma fix_inv {x : Q} : x ◃⁻¹ x = x := by { rw ←left_cancel x, simp } instance opposite_quandle : quandle Qᡐᡒᡖ := { fix := Ξ» x, by { induction x using mul_opposite.rec, simp } } /-- The conjugation quandle of a group. Each element of the group acts by the corresponding inner automorphism. -/ @[nolint has_inhabited_instance] def conj (G : Type*) := G instance conj.quandle (G : Type*) [group G] : quandle (conj G) := { act := (Ξ» x, @mul_aut.conj G _ x), self_distrib := Ξ» x y z, begin dsimp only [mul_equiv.coe_to_equiv, mul_aut.conj_apply, conj], group, end, inv_act := (Ξ» x, (@mul_aut.conj G _ x).symm), left_inv := Ξ» x y, by { dsimp [act, conj], group }, right_inv := Ξ» x y, by { dsimp [act, conj], group }, fix := Ξ» x, by simp } @[simp] lemma conj_act_eq_conj {G : Type*} [group G] (x y : conj G) : x β—ƒ y = ((x : G) * (y : G) * (x : G)⁻¹ : G) := rfl lemma conj_swap {G : Type*} [group G] (x y : conj G) : x β—ƒ y = y ↔ y β—ƒ x = x := begin dsimp [conj] at *, split, repeat { intro h, conv_rhs { rw eq_mul_inv_of_mul_eq (eq_mul_inv_of_mul_eq h) }, simp, }, end /-- `conj` is functorial -/ def conj.map {G : Type*} {H : Type*} [group G] [group H] (f : G β†’* H) : conj G β†’β—ƒ conj H := { to_fun := f, map_act' := by simp } instance {G : Type*} {H : Type*} [group G] [group H] : has_lift (G β†’* H) (conj G β†’β—ƒ conj H) := { lift := conj.map } /-- The dihedral quandle. This is the conjugation quandle of the dihedral group restrict to flips. Used for Fox n-colorings of knots. -/ @[nolint has_inhabited_instance] def dihedral (n : β„•) := zmod n /-- The operation for the dihedral quandle. It does not need to be an equivalence because it is an involution (see `dihedral_act.inv`). -/ def dihedral_act (n : β„•) (a : zmod n) : zmod n β†’ zmod n := Ξ» b, 2 * a - b lemma dihedral_act.inv (n : β„•) (a : zmod n) : function.involutive (dihedral_act n a) := by { intro b, dsimp [dihedral_act], ring } instance (n : β„•) : quandle (dihedral n) := { act := dihedral_act n, self_distrib := Ξ» x y z, begin dsimp [dihedral_act], ring, end, inv_act := dihedral_act n, left_inv := Ξ» x, (dihedral_act.inv n x).left_inverse, right_inv := Ξ» x, (dihedral_act.inv n x).right_inverse, fix := Ξ» x, begin dsimp [dihedral_act], ring, end } end quandle namespace rack /-- This is the natural rack homomorphism to the conjugation quandle of the group `R ≃ R` that acts on the rack. -/ def to_conj (R : Type*) [rack R] : R β†’β—ƒ quandle.conj (R ≃ R) := { to_fun := act, map_act' := ad_conj } section envel_group /-! ### Universal enveloping group of a rack The universal enveloping group `envel_group R` of a rack `R` is the universal group such that every rack homomorphism `R β†’β—ƒ conj G` is induced by a unique group homomorphism `envel_group R β†’* G`. For quandles, Joyce called this group `AdConj R`. The `envel_group` functor is left adjoint to the `conj` forgetful functor, and the way we construct the enveloping group is via a technique that should work for left adjoints of forgetful functors in general. It involves thinking a little about 2-categories, but the payoff is that the map `envel_group R β†’* G` has a nice description. Let's think of a group as being a one-object category. The first step is to define `pre_envel_group`, which gives formal expressions for all the 1-morphisms and includes the unit element, elements of `R`, multiplication, and inverses. To introduce relations, the second step is to define `pre_envel_group_rel'`, which gives formal expressions for all 2-morphisms between the 1-morphisms. The 2-morphisms include associativity, multiplication by the unit, multiplication by inverses, compatibility with multiplication and inverses (`congr_mul` and `congr_inv`), the axioms for an equivalence relation, and, importantly, the relationship between conjugation and the rack action (see `rack.ad_conj`). None of this forms a 2-category yet, for example due to lack of associativity of `trans`. The `pre_envel_group_rel` relation is a `Prop`-valued version of `pre_envel_group_rel'`, and making it `Prop`-valued essentially introduces enough 3-isomorphisms so that every pair of compatible 2-morphisms is isomorphic. Now, while composition in `pre_envel_group` does not strictly satisfy the category axioms, `pre_envel_group` and `pre_envel_group_rel'` do form a weak 2-category. Since we just want a 1-category, the last step is to quotient `pre_envel_group` by `pre_envel_group_rel'`, and the result is the group `envel_group`. For a homomorphism `f : R β†’β—ƒ conj G`, how does `envel_group.map f : envel_group R β†’* G` work? Let's think of `G` as being a 2-category with one object, a 1-morphism per element of `G`, and a single 2-morphism called `eq.refl` for each 1-morphism. We define the map using a "higher `quotient.lift`" -- not only do we evaluate elements of `pre_envel_group` as expressions in `G` (this is `to_envel_group.map_aux`), but we evaluate elements of `pre_envel_group'` as expressions of 2-morphisms of `G` (this is `to_envel_group.map_aux.well_def`). That is to say, `to_envel_group.map_aux.well_def` recursively evaluates formal expressions of 2-morphisms as equality proofs in `G`. Now that all morphisms are accounted for, the map descends to a homomorphism `envel_group R β†’* G`. Note: `Type`-valued relations are not common. The fact it is `Type`-valued is what makes `to_envel_group.map_aux.well_def` have well-founded recursion. -/ /-- Free generators of the enveloping group. -/ inductive pre_envel_group (R : Type u) : Type u | unit : pre_envel_group | incl (x : R) : pre_envel_group | mul (a b : pre_envel_group) : pre_envel_group | inv (a : pre_envel_group) : pre_envel_group instance pre_envel_group.inhabited (R : Type u) : inhabited (pre_envel_group R) := ⟨pre_envel_group.unit⟩ open pre_envel_group /-- Relations for the enveloping group. This is a type-valued relation because `to_envel_group.map_aux.well_def` inducts on it to show `to_envel_group.map` is well-defined. The relation `pre_envel_group_rel` is the `Prop`-valued version, which is used to define `envel_group` itself. -/ inductive pre_envel_group_rel' (R : Type u) [rack R] : pre_envel_group R β†’ pre_envel_group R β†’ Type u | refl {a : pre_envel_group R} : pre_envel_group_rel' a a | symm {a b : pre_envel_group R} (hab : pre_envel_group_rel' a b) : pre_envel_group_rel' b a | trans {a b c : pre_envel_group R} (hab : pre_envel_group_rel' a b) (hbc : pre_envel_group_rel' b c) : pre_envel_group_rel' a c | congr_mul {a b a' b' : pre_envel_group R} (ha : pre_envel_group_rel' a a') (hb : pre_envel_group_rel' b b') : pre_envel_group_rel' (mul a b) (mul a' b') | congr_inv {a a' : pre_envel_group R} (ha : pre_envel_group_rel' a a') : pre_envel_group_rel' (inv a) (inv a') | assoc (a b c : pre_envel_group R) : pre_envel_group_rel' (mul (mul a b) c) (mul a (mul b c)) | one_mul (a : pre_envel_group R) : pre_envel_group_rel' (mul unit a) a | mul_one (a : pre_envel_group R) : pre_envel_group_rel' (mul a unit) a | mul_left_inv (a : pre_envel_group R) : pre_envel_group_rel' (mul (inv a) a) unit | act_incl (x y : R) : pre_envel_group_rel' (mul (mul (incl x) (incl y)) (inv (incl x))) (incl (x β—ƒ y)) instance pre_envel_group_rel'.inhabited (R : Type u) [rack R] : inhabited (pre_envel_group_rel' R unit unit) := ⟨pre_envel_group_rel'.refl⟩ /-- The `pre_envel_group_rel` relation as a `Prop`. Used as the relation for `pre_envel_group.setoid`. -/ inductive pre_envel_group_rel (R : Type u) [rack R] : pre_envel_group R β†’ pre_envel_group R β†’ Prop | rel {a b : pre_envel_group R} (r : pre_envel_group_rel' R a b) : pre_envel_group_rel a b /-- A quick way to convert a `pre_envel_group_rel'` to a `pre_envel_group_rel`. -/ lemma pre_envel_group_rel'.rel {R : Type u} [rack R] {a b : pre_envel_group R} : pre_envel_group_rel' R a b β†’ pre_envel_group_rel R a b := pre_envel_group_rel.rel @[refl] lemma pre_envel_group_rel.refl {R : Type u} [rack R] {a : pre_envel_group R} : pre_envel_group_rel R a a := pre_envel_group_rel.rel pre_envel_group_rel'.refl @[symm] lemma pre_envel_group_rel.symm {R : Type u} [rack R] {a b : pre_envel_group R} : pre_envel_group_rel R a b β†’ pre_envel_group_rel R b a | ⟨r⟩ := r.symm.rel @[trans] lemma pre_envel_group_rel.trans {R : Type u} [rack R] {a b c : pre_envel_group R} : pre_envel_group_rel R a b β†’ pre_envel_group_rel R b c β†’ pre_envel_group_rel R a c | ⟨rab⟩ ⟨rbc⟩ := (rab.trans rbc).rel instance pre_envel_group.setoid (R : Type*) [rack R] : setoid (pre_envel_group R) := { r := pre_envel_group_rel R, iseqv := begin split, apply pre_envel_group_rel.refl, split, apply pre_envel_group_rel.symm, apply pre_envel_group_rel.trans end } /-- The universal enveloping group for the rack R. -/ def envel_group (R : Type*) [rack R] := quotient (pre_envel_group.setoid R) -- Define the `group` instances in two steps so `inv` can be inferred correctly. -- TODO: is there a non-invasive way of defining the instance directly? instance (R : Type*) [rack R] : div_inv_monoid (envel_group R) := { mul := Ξ» a b, quotient.lift_onβ‚‚ a b (Ξ» a b, ⟦pre_envel_group.mul a b⟧) (Ξ» a b a' b' ⟨ha⟩ ⟨hb⟩, quotient.sound (pre_envel_group_rel'.congr_mul ha hb).rel), one := ⟦unit⟧, inv := Ξ» a, quotient.lift_on a (Ξ» a, ⟦pre_envel_group.inv a⟧) (Ξ» a a' ⟨ha⟩, quotient.sound (pre_envel_group_rel'.congr_inv ha).rel), mul_assoc := Ξ» a b c, quotient.induction_on₃ a b c (Ξ» a b c, quotient.sound (pre_envel_group_rel'.assoc a b c).rel), one_mul := Ξ» a, quotient.induction_on a (Ξ» a, quotient.sound (pre_envel_group_rel'.one_mul a).rel), mul_one := Ξ» a, quotient.induction_on a (Ξ» a, quotient.sound (pre_envel_group_rel'.mul_one a).rel),} instance (R : Type*) [rack R] : group (envel_group R) := { mul_left_inv := Ξ» a, quotient.induction_on a (Ξ» a, quotient.sound (pre_envel_group_rel'.mul_left_inv a).rel), .. envel_group.div_inv_monoid _ } instance envel_group.inhabited (R : Type*) [rack R] : inhabited (envel_group R) := ⟨1⟩ /-- The canonical homomorphism from a rack to its enveloping group. Satisfies universal properties given by `to_envel_group.map` and `to_envel_group.univ`. -/ def to_envel_group (R : Type*) [rack R] : R β†’β—ƒ quandle.conj (envel_group R) := { to_fun := Ξ» x, ⟦incl x⟧, map_act' := Ξ» x y, quotient.sound (pre_envel_group_rel'.act_incl x y).symm.rel } /-- The preliminary definition of the induced map from the enveloping group. See `to_envel_group.map`. -/ def to_envel_group.map_aux {R : Type*} [rack R] {G : Type*} [group G] (f : R β†’β—ƒ quandle.conj G) : pre_envel_group R β†’ G | unit := 1 | (incl x) := f x | (mul a b) := to_envel_group.map_aux a * to_envel_group.map_aux b | (inv a) := (to_envel_group.map_aux a)⁻¹ namespace to_envel_group.map_aux open pre_envel_group_rel' /-- Show that `to_envel_group.map_aux` sends equivalent expressions to equal terms. -/ lemma well_def {R : Type*} [rack R] {G : Type*} [group G] (f : R β†’β—ƒ quandle.conj G) : Ξ  {a b : pre_envel_group R}, pre_envel_group_rel' R a b β†’ to_envel_group.map_aux f a = to_envel_group.map_aux f b | a b refl := rfl | a b (symm h) := (well_def h).symm | a b (trans hac hcb) := eq.trans (well_def hac) (well_def hcb) | _ _ (congr_mul ha hb) := by { simp [to_envel_group.map_aux, well_def ha, well_def hb] } | _ _ (congr_inv ha) := by { simp [to_envel_group.map_aux, well_def ha] } | _ _ (assoc a b c) := by { apply mul_assoc } | _ _ (one_mul a) := by { simp [to_envel_group.map_aux] } | _ _ (mul_one a) := by { simp [to_envel_group.map_aux] } | _ _ (mul_left_inv a) := by { simp [to_envel_group.map_aux] } | _ _ (act_incl x y) := by { simp [to_envel_group.map_aux] } end to_envel_group.map_aux /-- Given a map from a rack to a group, lift it to being a map from the enveloping group. More precisely, the `envel_group` functor is left adjoint to `quandle.conj`. -/ def to_envel_group.map {R : Type*} [rack R] {G : Type*} [group G] : (R β†’β—ƒ quandle.conj G) ≃ (envel_group R β†’* G) := { to_fun := Ξ» f, { to_fun := Ξ» x, quotient.lift_on x (to_envel_group.map_aux f) (Ξ» a b ⟨hab⟩, to_envel_group.map_aux.well_def f hab), map_one' := begin change quotient.lift_on ⟦rack.pre_envel_group.unit⟧ (to_envel_group.map_aux f) _ = 1, simp [to_envel_group.map_aux], end, map_mul' := Ξ» x y, quotient.induction_onβ‚‚ x y (Ξ» x y, begin change quotient.lift_on ⟦mul x y⟧ (to_envel_group.map_aux f) _ = _, simp [to_envel_group.map_aux], end) }, inv_fun := Ξ» F, (quandle.conj.map F).comp (to_envel_group R), left_inv := Ξ» f, by { ext, refl }, right_inv := Ξ» F, monoid_hom.ext $ Ξ» x, quotient.induction_on x $ Ξ» x, begin induction x, { exact F.map_one.symm, }, { refl, }, { have hm : ⟦x_a.mul x_b⟧ = @has_mul.mul (envel_group R) _ ⟦x_a⟧ ⟦x_b⟧ := rfl, rw [hm, F.map_mul, monoid_hom.map_mul, ←x_ih_a, ←x_ih_b] }, { have hm : ⟦x_a.inv⟧ = @has_inv.inv (envel_group R) _ ⟦x_a⟧ := rfl, rw [hm, F.map_inv, monoid_hom.map_inv, x_ih], } end, } /-- Given a homomorphism from a rack to a group, it factors through the enveloping group. -/ lemma to_envel_group.univ (R : Type*) [rack R] (G : Type*) [group G] (f : R β†’β—ƒ quandle.conj G) : (quandle.conj.map (to_envel_group.map f)).comp (to_envel_group R) = f := to_envel_group.map.symm_apply_apply f /-- The homomorphism `to_envel_group.map f` is the unique map that fits into the commutative triangle in `to_envel_group.univ`. -/ lemma to_envel_group.univ_uniq (R : Type*) [rack R] (G : Type*) [group G] (f : R β†’β—ƒ quandle.conj G) (g : envel_group R β†’* G) (h : f = (quandle.conj.map g).comp (to_envel_group R)) : g = to_envel_group.map f := h.symm β–Έ (to_envel_group.map.apply_symm_apply g).symm /-- The induced group homomorphism from the enveloping group into bijections of the rack, using `rack.to_conj`. Satisfies the property `envel_action_prop`. This gives the rack `R` the structure of an augmented rack over `envel_group R`. -/ def envel_action {R : Type*} [rack R] : envel_group R β†’* (R ≃ R) := to_envel_group.map (to_conj R) @[simp] lemma envel_action_prop {R : Type*} [rack R] (x y : R) : envel_action (to_envel_group R x) y = x β—ƒ y := rfl end envel_group end rack
dee4d0cacdf4fe1631fb7689ae3e7b95440d452d
9028d228ac200bbefe3a711342514dd4e4458bff
/src/data/real/pi.lean
2da53d93d1554e0c32ffd92650e3a7698d93eff0
[ "Apache-2.0" ]
permissive
mcncm/mathlib
8d25099344d9d2bee62822cb9ed43aa3e09fa05e
fde3d78cadeec5ef827b16ae55664ef115e66f57
refs/heads/master
1,672,743,316,277
1,602,618,514,000
1,602,618,514,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
7,382
lean
/- Copyright (c) 2019 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn -/ import analysis.special_functions.trigonometric namespace real lemma pi_gt_sqrt_two_add_series (n : β„•) : 2 ^ (n+1) * sqrt (2 - sqrt_two_add_series 0 n) < pi := begin have : sqrt (2 - sqrt_two_add_series 0 n) / 2 * 2 ^ (n+2) < pi, { rw [← lt_div_iff, ←sin_pi_over_two_pow_succ], apply sin_lt, apply div_pos pi_pos, all_goals { apply pow_pos, norm_num } }, apply lt_of_le_of_lt (le_of_eq _) this, rw [pow_succ _ (n+1), ←mul_assoc, div_mul_cancel, mul_comm], norm_num end lemma pi_lt_sqrt_two_add_series (n : β„•) : pi < 2 ^ (n+1) * sqrt (2 - sqrt_two_add_series 0 n) + 1 / 4 ^ n := begin have : pi < (sqrt (2 - sqrt_two_add_series 0 n) / 2 + 1 / (2 ^ n) ^ 3 / 4) * 2 ^ (n+2), { rw [← div_lt_iff, ← sin_pi_over_two_pow_succ], refine lt_of_lt_of_le (lt_add_of_sub_right_lt (sin_gt_sub_cube _ _)) _, { apply div_pos pi_pos, apply pow_pos, norm_num }, { rw div_le_iff', { refine le_trans pi_le_four _, simp only [show ((4 : ℝ) = 2 ^ 2), by norm_num, mul_one], apply pow_le_pow, norm_num, apply le_add_of_nonneg_left, apply nat.zero_le }, { apply pow_pos, norm_num }}, apply add_le_add_left, rw div_le_div_right, rw [le_div_iff, ←mul_pow], refine le_trans _ (le_of_eq (one_pow 3)), apply pow_le_pow_of_le_left, { apply le_of_lt, apply mul_pos, apply div_pos pi_pos, apply pow_pos, norm_num, apply pow_pos, norm_num }, rw ← le_div_iff, refine le_trans ((div_le_div_right _).mpr pi_le_four) _, apply pow_pos, norm_num, rw [pow_succ, pow_succ, ←mul_assoc, ←div_div_eq_div_mul], convert le_refl _, all_goals { repeat {apply pow_pos}, norm_num }}, apply lt_of_lt_of_le this (le_of_eq _), rw [add_mul], congr' 1, { rw [pow_succ _ (n+1), ←mul_assoc, div_mul_cancel, mul_comm], norm_num }, rw [pow_succ, ←pow_mul, mul_comm n 2, pow_mul, show (2 : ℝ) ^ 2 = 4, by norm_num, pow_succ, pow_succ, ←mul_assoc (2 : ℝ), show (2 : ℝ) * 2 = 4, by norm_num, ←mul_assoc, div_mul_cancel, mul_comm ((2 : ℝ) ^ n), ←div_div_eq_div_mul, div_mul_cancel], apply pow_ne_zero, norm_num, norm_num end /-- From an upper bound on `sqrt_two_add_series 0 n = 2 cos (pi / 2 ^ (n+1))` of the form `sqrt_two_add_series 0 n ≀ 2 - (a / 2 ^ (n + 1)) ^ 2)`, one can deduce the lower bound `a < pi` thanks to basic trigonometric inequalities as expressed in `pi_gt_sqrt_two_add_series`. -/ theorem pi_lower_bound_start (n : β„•) {a} (h : sqrt_two_add_series ((0:β„•) / (1:β„•)) n ≀ 2 - (a / 2 ^ (n + 1)) ^ 2) : a < pi := begin refine lt_of_le_of_lt _ (pi_gt_sqrt_two_add_series n), rw [mul_comm], refine (div_le_iff (pow_pos (by norm_num) _ : (0 : ℝ) < _)).mp (le_sqrt_of_sqr_le _), rwa [le_sub, show (0:ℝ) = (0:β„•)/(1:β„•), by rw [nat.cast_zero, zero_div]], end lemma sqrt_two_add_series_step_up (c d : β„•) {a b n : β„•} {z : ℝ} (hz : sqrt_two_add_series (c/d) n ≀ z) (hb : 0 < b) (hd : 0 < d) (h : (2 * b + a) * d ^ 2 ≀ c ^ 2 * b) : sqrt_two_add_series (a/b) (n+1) ≀ z := begin refine le_trans _ hz, rw sqrt_two_add_series_succ, apply sqrt_two_add_series_monotone_left, have hb' : 0 < (b:ℝ) := nat.cast_pos.2 hb, have hd' : 0 < (d:ℝ) := nat.cast_pos.2 hd, rw [sqrt_le_left (div_nonneg c.cast_nonneg d.cast_nonneg), div_pow, add_div_eq_mul_add_div _ _ (ne_of_gt hb'), div_le_div_iff hb' (pow_pos hd' _)], exact_mod_cast h end /-- Create a proof of `a < pi` for a fixed rational number `a`, given a witness, which is a sequence of rational numbers `sqrt 2 < r 1 < r 2 < ... < r n < 2` satisfying the property that `sqrt (2 + r i) ≀ r(i+1)`, where `r 0 = 0` and `sqrt (2 - r n) β‰₯ a/2^(n+1)`. -/ meta def pi_lower_bound (l : list β„š) : tactic unit := do let n := l.length, tactic.apply `(@pi_lower_bound_start %%(reflect n)), l.mmap' (Ξ» r, do let a := r.num.to_nat, let b := r.denom, (() <$ tactic.apply `(@sqrt_two_add_series_step_up %%(reflect a) %%(reflect b))); [tactic.skip, `[norm_num1], `[norm_num1], `[norm_num1]]), `[simp only [sqrt_two_add_series, nat.cast_bit0, nat.cast_bit1, nat.cast_one, nat.cast_zero]], `[norm_num1] /-- From a lower bound on `sqrt_two_add_series 0 n = 2 cos (pi / 2 ^ (n+1))` of the form `2 - ((a - 1 / 4 ^ n) / 2 ^ (n + 1)) ^ 2 ≀ sqrt_two_add_series 0 n`, one can deduce the upper bound `pi < a` thanks to basic trigonometric formulas as expressed in `pi_lt_sqrt_two_add_series`. -/ theorem pi_upper_bound_start (n : β„•) {a} (h : 2 - ((a - 1 / 4 ^ n) / 2 ^ (n + 1)) ^ 2 ≀ sqrt_two_add_series ((0:β„•) / (1:β„•)) n) (hβ‚‚ : 1 / 4 ^ n ≀ a) : pi < a := begin refine lt_of_lt_of_le (pi_lt_sqrt_two_add_series n) _, rw [← le_sub_iff_add_le, ← le_div_iff', sqrt_le_left, sub_le], { rwa [nat.cast_zero, zero_div] at h }, { exact div_nonneg (sub_nonneg.2 hβ‚‚) (pow_nonneg (le_of_lt zero_lt_two) _) }, { exact pow_pos zero_lt_two _ } end lemma sqrt_two_add_series_step_down (a b : β„•) {c d n : β„•} {z : ℝ} (hz : z ≀ sqrt_two_add_series (a/b) n) (hb : 0 < b) (hd : 0 < d) (h : a ^ 2 * d ≀ (2 * d + c) * b ^ 2) : z ≀ sqrt_two_add_series (c/d) (n+1) := begin apply le_trans hz, rw sqrt_two_add_series_succ, apply sqrt_two_add_series_monotone_left, apply le_sqrt_of_sqr_le, have hb' : 0 < (b:ℝ) := nat.cast_pos.2 hb, have hd' : 0 < (d:ℝ) := nat.cast_pos.2 hd, rw [div_pow, add_div_eq_mul_add_div _ _ (ne_of_gt hd'), div_le_div_iff (pow_pos hb' _) hd'], exact_mod_cast h end /-- Create a proof of `pi < a` for a fixed rational number `a`, given a witness, which is a sequence of rational numbers `sqrt 2 < r 1 < r 2 < ... < r n < 2` satisfying the property that `sqrt (2 + r i) β‰₯ r(i+1)`, where `r 0 = 0` and `sqrt (2 - r n) β‰₯ (a - 1/4^n) / 2^(n+1)`. -/ meta def pi_upper_bound (l : list β„š) : tactic unit := do let n := l.length, (() <$ tactic.apply `(@pi_upper_bound_start %%(reflect n))); [pure (), `[norm_num1]], l.mmap' (Ξ» r, do let a := r.num.to_nat, let b := r.denom, (() <$ tactic.apply `(@sqrt_two_add_series_step_down %%(reflect a) %%(reflect b))); [pure (), `[norm_num1], `[norm_num1], `[norm_num1]]), `[simp only [sqrt_two_add_series, nat.cast_bit0, nat.cast_bit1, nat.cast_one, nat.cast_zero]], `[norm_num] lemma pi_gt_three : 3 < pi := by pi_lower_bound [23/16] lemma pi_gt_314 : 3.14 < pi := by pi_lower_bound [99/70, 874/473, 1940/989, 1447/727] lemma pi_lt_315 : pi < 3.15 := by pi_upper_bound [140/99, 279/151, 51/26, 412/207] lemma pi_gt_31415 : 3.1415 < pi := by pi_lower_bound [ 11482/8119, 5401/2923, 2348/1197, 11367/5711, 25705/12868, 23235/11621] lemma pi_lt_31416 : pi < 3.1416 := by pi_upper_bound [ 4756/3363, 101211/54775, 505534/257719, 83289/41846, 411278/205887, 438142/219137, 451504/225769, 265603/132804, 849938/424971] lemma pi_gt_3141592 : 3.141592 < pi := by pi_lower_bound [ 11482/8119, 7792/4217, 54055/27557, 949247/476920, 3310126/1657059, 2635492/1318143, 1580265/790192, 1221775/610899, 3612247/1806132, 849943/424972] lemma pi_lt_3141593 : pi < 3.141593 := by pi_upper_bound [ 27720/19601, 56935/30813, 49359/25163, 258754/130003, 113599/56868, 1101994/551163, 8671537/4336095, 3877807/1938940, 52483813/26242030, 56946167/28473117, 23798415/11899211] end real
65bcabe36f25e05bf6bede2bf893a12a78ad44ac
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/1968.lean
4c82d8a6bafd7beb66f694d71ef578c846aafd23
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach" ]
permissive
leanprover/lean4
4bdf9790294964627eb9be79f5e8f6157780b4cc
f1f9dc0f2f531af3312398999d8b8303fa5f096b
refs/heads/master
1,693,360,665,786
1,693,350,868,000
1,693,350,868,000
129,571,436
2,827
311
Apache-2.0
1,694,716,156,000
1,523,760,560,000
Lean
UTF-8
Lean
false
false
1,316
lean
inductive type | bv : Nat β†’ type | bit : type open type -- This is a "parameterized List" where `plist f types` contains -- an element of Type `f tp` for each corresponding element `tp ∈ types`. inductive plist (f : type β†’ Type) : List type β†’ Type | nil : plist f [] | cons {h:type} {r:List type} : f h β†’ plist f r β†’ plist f (h::r) -- Operations on values; the first argument contains the types of -- inputs, and the second for the return Type. inductive op : List type β†’ type β†’ Type | neq (tp:type) : op [tp, tp] bit | mul (w : Nat) : op [bv w, bv w] (bv w) -- Denotes expressions that evaluate to a number given a memory State and register to value map. inductive value : type β†’ Type | const (w : Nat) : value (bv w) | op {args:List type} {tp:type} : op args tp β†’ plist value args β†’ value tp --- This creates a plist (borrowed from the List notation). open value -- This works -- #eval value.op (op.mul 32) [[[ const 32, const 32 ]]] -- This also works -- instance bvMul (w : Nat) : Mul (value (bv w)) := ⟨fun x y => w value.op (op.mul w) [[[x, y]]]⟩ -- #eval const 32 * const 32 -- This works -- #eval value.op (op.neq (bv 32)) [[[ const 32, const 32 ]]] -- def neq {tp:type} (x y : value tp) : value bit := value.op (op.neq tp) [[[x, y]]] -- #eval neq (const 32) (const 32)
128cba9597371e8ee8d3e11cd68e80e8706c278b
2a70b774d16dbdf5a533432ee0ebab6838df0948
/_target/deps/mathlib/src/data/finset/sort.lean
fe97168fd968c81914d201599596effe26faa2ef
[ "Apache-2.0" ]
permissive
hjvromen/lewis
40b035973df7c77ebf927afab7878c76d05ff758
105b675f73630f028ad5d890897a51b3c1146fb0
refs/heads/master
1,677,944,636,343
1,676,555,301,000
1,676,555,301,000
327,553,599
0
0
null
null
null
null
UTF-8
Lean
false
false
8,210
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro -/ import data.finset.lattice import data.multiset.sort import data.list.nodup_equiv_fin /-! # Construct a sorted list from a finset. -/ namespace finset open multiset nat variables {Ξ± Ξ² : Type*} /-! ### sort -/ section sort variables (r : Ξ± β†’ Ξ± β†’ Prop) [decidable_rel r] [is_trans Ξ± r] [is_antisymm Ξ± r] [is_total Ξ± r] /-- `sort s` constructs a sorted list from the unordered set `s`. (Uses merge sort algorithm.) -/ def sort (s : finset Ξ±) : list Ξ± := sort r s.1 @[simp] theorem sort_sorted (s : finset Ξ±) : list.sorted r (sort r s) := sort_sorted _ _ @[simp] theorem sort_eq (s : finset Ξ±) : ↑(sort r s) = s.1 := sort_eq _ _ @[simp] theorem sort_nodup (s : finset Ξ±) : (sort r s).nodup := (by rw sort_eq; exact s.2 : @multiset.nodup Ξ± (sort r s)) @[simp] theorem sort_to_finset [decidable_eq Ξ±] (s : finset Ξ±) : (sort r s).to_finset = s := list.to_finset_eq (sort_nodup r s) β–Έ eq_of_veq (sort_eq r s) @[simp] theorem mem_sort {s : finset Ξ±} {a : Ξ±} : a ∈ sort r s ↔ a ∈ s := multiset.mem_sort _ @[simp] theorem length_sort {s : finset Ξ±} : (sort r s).length = s.card := multiset.length_sort _ end sort section sort_linear_order variables [linear_order Ξ±] theorem sort_sorted_lt (s : finset Ξ±) : list.sorted (<) (sort (≀) s) := (sort_sorted _ _).impβ‚‚ (@lt_of_le_of_ne _ _) (sort_nodup _ _) lemma sorted_zero_eq_min'_aux (s : finset Ξ±) (h : 0 < (s.sort (≀)).length) (H : s.nonempty) : (s.sort (≀)).nth_le 0 h = s.min' H := begin let l := s.sort (≀), apply le_antisymm, { have : s.min' H ∈ l := (finset.mem_sort (≀)).mpr (s.min'_mem H), obtain ⟨i, i_lt, hi⟩ : βˆƒ i (hi : i < l.length), l.nth_le i hi = s.min' H := list.mem_iff_nth_le.1 this, rw ← hi, exact list.nth_le_of_sorted_of_le (s.sort_sorted (≀)) (nat.zero_le i) }, { have : l.nth_le 0 h ∈ s := (finset.mem_sort (≀)).1 (list.nth_le_mem l 0 h), exact s.min'_le _ this } end lemma sorted_zero_eq_min' {s : finset Ξ±} {h : 0 < (s.sort (≀)).length} : (s.sort (≀)).nth_le 0 h = s.min' (card_pos.1 $ by rwa length_sort at h) := sorted_zero_eq_min'_aux _ _ _ lemma min'_eq_sorted_zero {s : finset Ξ±} {h : s.nonempty} : s.min' h = (s.sort (≀)).nth_le 0 (by { rw length_sort, exact card_pos.2 h }) := (sorted_zero_eq_min'_aux _ _ _).symm lemma sorted_last_eq_max'_aux (s : finset Ξ±) (h : (s.sort (≀)).length - 1 < (s.sort (≀)).length) (H : s.nonempty) : (s.sort (≀)).nth_le ((s.sort (≀)).length - 1) h = s.max' H := begin let l := s.sort (≀), apply le_antisymm, { have : l.nth_le ((s.sort (≀)).length - 1) h ∈ s := (finset.mem_sort (≀)).1 (list.nth_le_mem l _ h), exact s.le_max' _ this }, { have : s.max' H ∈ l := (finset.mem_sort (≀)).mpr (s.max'_mem H), obtain ⟨i, i_lt, hi⟩ : βˆƒ i (hi : i < l.length), l.nth_le i hi = s.max' H := list.mem_iff_nth_le.1 this, rw ← hi, have : i ≀ l.length - 1 := nat.le_pred_of_lt i_lt, exact list.nth_le_of_sorted_of_le (s.sort_sorted (≀)) (nat.le_pred_of_lt i_lt) }, end lemma sorted_last_eq_max' {s : finset Ξ±} {h : (s.sort (≀)).length - 1 < (s.sort (≀)).length} : (s.sort (≀)).nth_le ((s.sort (≀)).length - 1) h = s.max' (by { rw length_sort at h, exact card_pos.1 (lt_of_le_of_lt bot_le h) }) := sorted_last_eq_max'_aux _ _ _ lemma max'_eq_sorted_last {s : finset Ξ±} {h : s.nonempty} : s.max' h = (s.sort (≀)).nth_le ((s.sort (≀)).length - 1) (by simpa using sub_lt (card_pos.mpr h) zero_lt_one) := (sorted_last_eq_max'_aux _ _ _).symm /-- Given a finset `s` of cardinality `k` in a linear order `Ξ±`, the map `order_iso_of_fin s h` is the increasing bijection between `fin k` and `s` as an `order_iso`. Here, `h` is a proof that the cardinality of `s` is `k`. We use this instead of an iso `fin s.card ≃o s` to avoid casting issues in further uses of this function. -/ def order_iso_of_fin (s : finset Ξ±) {k : β„•} (h : s.card = k) : fin k ≃o (s : set Ξ±) := order_iso.trans (fin.cast ((length_sort (≀)).trans h).symm) $ (s.sort_sorted_lt.nth_le_iso _).trans $ order_iso.set_congr _ _ $ set.ext $ Ξ» x, mem_sort _ /-- Given a finset `s` of cardinality `k` in a linear order `Ξ±`, the map `order_emb_of_fin s h` is the increasing bijection between `fin k` and `s` as an order embedding into `Ξ±`. Here, `h` is a proof that the cardinality of `s` is `k`. We use this instead of an embedding `fin s.card β†ͺo Ξ±` to avoid casting issues in further uses of this function. -/ def order_emb_of_fin (s : finset Ξ±) {k : β„•} (h : s.card = k) : fin k β†ͺo Ξ± := (order_iso_of_fin s h).to_order_embedding.trans (order_embedding.subtype _) @[simp] lemma coe_order_iso_of_fin_apply (s : finset Ξ±) {k : β„•} (h : s.card = k) (i : fin k) : ↑(order_iso_of_fin s h i) = order_emb_of_fin s h i := rfl lemma order_iso_of_fin_symm_apply (s : finset Ξ±) {k : β„•} (h : s.card = k) (x : (s : set Ξ±)) : ↑((s.order_iso_of_fin h).symm x) = (s.sort (≀)).index_of x := rfl lemma order_emb_of_fin_apply (s : finset Ξ±) {k : β„•} (h : s.card = k) (i : fin k) : s.order_emb_of_fin h i = (s.sort (≀)).nth_le i (by { rw [length_sort, h], exact i.2 }) := rfl @[simp] lemma range_order_emb_of_fin (s : finset Ξ±) {k : β„•} (h : s.card = k) : set.range (s.order_emb_of_fin h) = s := by simp [order_emb_of_fin, set.range_comp coe (s.order_iso_of_fin h)] /-- The bijection `order_emb_of_fin s h` sends `0` to the minimum of `s`. -/ lemma order_emb_of_fin_zero {s : finset Ξ±} {k : β„•} (h : s.card = k) (hz : 0 < k) : order_emb_of_fin s h ⟨0, hz⟩ = s.min' (card_pos.mp (h.symm β–Έ hz)) := by simp only [order_emb_of_fin_apply, subtype.coe_mk, sorted_zero_eq_min'] /-- The bijection `order_emb_of_fin s h` sends `k-1` to the maximum of `s`. -/ lemma order_emb_of_fin_last {s : finset Ξ±} {k : β„•} (h : s.card = k) (hz : 0 < k) : order_emb_of_fin s h ⟨k-1, buffer.lt_aux_2 hz⟩ = s.max' (card_pos.mp (h.symm β–Έ hz)) := by simp [order_emb_of_fin_apply, max'_eq_sorted_last, h] /-- `order_emb_of_fin {a} h` sends any argument to `a`. -/ @[simp] lemma order_emb_of_fin_singleton (a : Ξ±) (i : fin 1) : order_emb_of_fin {a} (card_singleton a) i = a := by rw [subsingleton.elim i ⟨0, zero_lt_one⟩, order_emb_of_fin_zero _ zero_lt_one, min'_singleton] /-- Any increasing map `f` from `fin k` to a finset of cardinality `k` has to coincide with the increasing bijection `order_emb_of_fin s h`. -/ lemma order_emb_of_fin_unique {s : finset Ξ±} {k : β„•} (h : s.card = k) {f : fin k β†’ Ξ±} (hfs : βˆ€ x, f x ∈ s) (hmono : strict_mono f) : f = s.order_emb_of_fin h := begin apply fin.strict_mono_unique hmono (s.order_emb_of_fin h).strict_mono, rw [range_order_emb_of_fin, ← set.image_univ, ← coe_fin_range, ← coe_image, coe_inj], refine eq_of_subset_of_card_le (Ξ» x hx, _) _, { rcases mem_image.1 hx with ⟨x, hx, rfl⟩, exact hfs x }, { rw [h, card_image_of_injective _ hmono.injective, fin_range_card] } end /-- An order embedding `f` from `fin k` to a finset of cardinality `k` has to coincide with the increasing bijection `order_emb_of_fin s h`. -/ lemma order_emb_of_fin_unique' {s : finset Ξ±} {k : β„•} (h : s.card = k) {f : fin k β†ͺo Ξ±} (hfs : βˆ€ x, f x ∈ s) : f = s.order_emb_of_fin h := rel_embedding.ext $ function.funext_iff.1 $ order_emb_of_fin_unique h hfs f.strict_mono /-- Two parametrizations `order_emb_of_fin` of the same set take the same value on `i` and `j` if and only if `i = j`. Since they can be defined on a priori not defeq types `fin k` and `fin l` (although necessarily `k = l`), the conclusion is rather written `(i : β„•) = (j : β„•)`. -/ @[simp] lemma order_emb_of_fin_eq_order_emb_of_fin_iff {k l : β„•} {s : finset Ξ±} {i : fin k} {j : fin l} {h : s.card = k} {h' : s.card = l} : s.order_emb_of_fin h i = s.order_emb_of_fin h' j ↔ (i : β„•) = (j : β„•) := begin substs k l, exact (s.order_emb_of_fin rfl).eq_iff_eq.trans (fin.ext_iff _ _) end end sort_linear_order instance [has_repr Ξ±] : has_repr (finset Ξ±) := ⟨λ s, repr s.1⟩ end finset
d469d2daa9cc336802a24e1224a1242cba1ba00e
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/def5.lean
954f13833b53394cd339118b385671b1c318269c
[ "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
97
lean
section parameter (A : Type) definition f : A β†’ A := Ξ» x, x #check f end #check f
d7b7bc4768b674a80327e0b89a6548779a4c8621
7cef822f3b952965621309e88eadf618da0c8ae9
/src/topology/algebra/group.lean
d01af58cfbc7cb1073c1f3119f09a276c9935d6c
[ "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
16,842
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, Patrick Massot Theory of topological groups. -/ import data.equiv.algebra import algebra.pointwise order.filter.pointwise import group_theory.quotient_group import topology.algebra.monoid topology.homeomorph open classical set lattice filter topological_space open_locale classical topological_space universes u v w variables {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} section topological_group section prio set_option default_priority 100 -- see Note [default priority] /-- A topological (additive) group is a group in which the addition and negation operations are continuous. -/ class topological_add_group (Ξ± : Type u) [topological_space Ξ±] [add_group Ξ±] extends topological_add_monoid Ξ± : Prop := (continuous_neg : continuous (Ξ»a:Ξ±, -a)) /-- A topological group is a group in which the multiplication and inversion operations are continuous. -/ @[to_additive topological_add_group] class topological_group (Ξ± : Type*) [topological_space Ξ±] [group Ξ±] extends topological_monoid Ξ± : Prop := (continuous_inv : continuous (Ξ»a:Ξ±, a⁻¹)) end prio variables [topological_space Ξ±] [group Ξ±] @[to_additive] lemma continuous_inv [topological_group Ξ±] : continuous (Ξ»x:Ξ±, x⁻¹) := topological_group.continuous_inv Ξ± @[to_additive] lemma continuous.inv [topological_group Ξ±] [topological_space Ξ²] {f : Ξ² β†’ Ξ±} (hf : continuous f) : continuous (Ξ»x, (f x)⁻¹) := continuous_inv.comp hf @[to_additive] lemma continuous_on.inv [topological_group Ξ±] [topological_space Ξ²] {f : Ξ² β†’ Ξ±} {s : set Ξ²} (hf : continuous_on f s) : continuous_on (Ξ»x, (f x)⁻¹) s := continuous_inv.comp_continuous_on hf /-- If a function converges to a value in a multiplicative topological group, then its inverse converges to the inverse of this value. For the version in normed fields assuming additionally that the limit is nonzero, use `tendsto.inv'`. -/ @[to_additive] lemma filter.tendsto.inv [topological_group Ξ±] {f : Ξ² β†’ Ξ±} {x : filter Ξ²} {a : Ξ±} (hf : tendsto f x (𝓝 a)) : tendsto (Ξ»x, (f x)⁻¹) x (𝓝 a⁻¹) := tendsto.comp (continuous_iff_continuous_at.mp (topological_group.continuous_inv Ξ±) a) hf @[to_additive topological_add_group] instance [topological_group Ξ±] [topological_space Ξ²] [group Ξ²] [topological_group Ξ²] : topological_group (Ξ± Γ— Ξ²) := { continuous_inv := continuous_fst.inv.prod_mk continuous_snd.inv } attribute [instance] prod.topological_add_group @[to_additive] protected def homeomorph.mul_left [topological_group Ξ±] (a : Ξ±) : Ξ± β‰ƒβ‚œ Ξ± := { continuous_to_fun := continuous_const.mul continuous_id, continuous_inv_fun := continuous_const.mul continuous_id, .. equiv.mul_left a } @[to_additive] lemma is_open_map_mul_left [topological_group Ξ±] (a : Ξ±) : is_open_map (Ξ» x, a * x) := (homeomorph.mul_left a).is_open_map @[to_additive] lemma is_closed_map_mul_left [topological_group Ξ±] (a : Ξ±) : is_closed_map (Ξ» x, a * x) := (homeomorph.mul_left a).is_closed_map @[to_additive] protected def homeomorph.mul_right {Ξ± : Type*} [topological_space Ξ±] [group Ξ±] [topological_group Ξ±] (a : Ξ±) : Ξ± β‰ƒβ‚œ Ξ± := { continuous_to_fun := continuous_id.mul continuous_const, continuous_inv_fun := continuous_id.mul continuous_const, .. equiv.mul_right a } @[to_additive] lemma is_open_map_mul_right [topological_group Ξ±] (a : Ξ±) : is_open_map (Ξ» x, x * a) := (homeomorph.mul_right a).is_open_map @[to_additive] lemma is_closed_map_mul_right [topological_group Ξ±] (a : Ξ±) : is_closed_map (Ξ» x, x * a) := (homeomorph.mul_right a).is_closed_map @[to_additive] protected def homeomorph.inv (Ξ± : Type*) [topological_space Ξ±] [group Ξ±] [topological_group Ξ±] : Ξ± β‰ƒβ‚œ Ξ± := { continuous_to_fun := continuous_inv, continuous_inv_fun := continuous_inv, .. equiv.inv Ξ± } @[to_additive exists_nhds_half] lemma exists_nhds_split [topological_group Ξ±] {s : set Ξ±} (hs : s ∈ 𝓝 (1 : Ξ±)) : βˆƒ V ∈ 𝓝 (1 : Ξ±), βˆ€ v w ∈ V, v * w ∈ s := begin have : ((Ξ»a:Ξ±Γ—Ξ±, a.1 * a.2) ⁻¹' s) ∈ 𝓝 ((1, 1) : Ξ± Γ— Ξ±) := tendsto_mul (by simpa using hs), rw nhds_prod_eq at this, rcases mem_prod_iff.1 this with ⟨V₁, H₁, Vβ‚‚, Hβ‚‚, H⟩, exact ⟨V₁ ∩ Vβ‚‚, inter_mem_sets H₁ Hβ‚‚, assume v w ⟨hv, _⟩ ⟨_, hw⟩, @H (v, w) ⟨hv, hw⟩⟩ end @[to_additive exists_nhds_half_neg] lemma exists_nhds_split_inv [topological_group Ξ±] {s : set Ξ±} (hs : s ∈ 𝓝 (1 : Ξ±)) : βˆƒ V ∈ 𝓝 (1 : Ξ±), βˆ€ v w ∈ V, v * w⁻¹ ∈ s := begin have : tendsto (Ξ»a:Ξ±Γ—Ξ±, a.1 * (a.2)⁻¹) ((𝓝 (1:Ξ±)).prod (𝓝 (1:Ξ±))) (𝓝 1), { simpa using (@tendsto_fst Ξ± Ξ± (𝓝 1) (𝓝 1)).mul tendsto_snd.inv }, have : ((Ξ»a:Ξ±Γ—Ξ±, a.1 * (a.2)⁻¹) ⁻¹' s) ∈ (𝓝 (1:Ξ±)).prod (𝓝 (1:Ξ±)) := this (by simpa using hs), rcases mem_prod_iff.1 this with ⟨V₁, H₁, Vβ‚‚, Hβ‚‚, H⟩, exact ⟨V₁ ∩ Vβ‚‚, inter_mem_sets H₁ Hβ‚‚, assume v w ⟨hv, _⟩ ⟨_, hw⟩, @H (v, w) ⟨hv, hw⟩⟩ end @[to_additive exists_nhds_quarter] lemma exists_nhds_split4 [topological_group Ξ±] {u : set Ξ±} (hu : u ∈ 𝓝 (1 : Ξ±)) : βˆƒ V ∈ 𝓝 (1 : Ξ±), βˆ€ {v w s t}, v ∈ V β†’ w ∈ V β†’ s ∈ V β†’ t ∈ V β†’ v * w * s * t ∈ u := begin rcases exists_nhds_split hu with ⟨W, W_nhd, h⟩, rcases exists_nhds_split W_nhd with ⟨V, V_nhd, h'⟩, existsi [V, V_nhd], intros v w s t v_in w_in s_in t_in, simpa [mul_assoc] using h _ _ (h' v w v_in w_in) (h' s t s_in t_in) end section variable (Ξ±) @[to_additive] lemma nhds_one_symm [topological_group Ξ±] : comap (Ξ»r:Ξ±, r⁻¹) (𝓝 (1 : Ξ±)) = 𝓝 (1 : Ξ±) := begin have lim : tendsto (Ξ»r:Ξ±, r⁻¹) (𝓝 1) (𝓝 1), { simpa using (@tendsto_id Ξ± (𝓝 1)).inv }, refine comap_eq_of_inverse _ _ lim lim, { funext x, simp }, end end @[to_additive] lemma nhds_translation_mul_inv [topological_group Ξ±] (x : Ξ±) : comap (Ξ»y:Ξ±, y * x⁻¹) (𝓝 1) = 𝓝 x := begin refine comap_eq_of_inverse (Ξ»y:Ξ±, y * x) _ _ _, { funext x; simp }, { suffices : tendsto (Ξ»y:Ξ±, y * x⁻¹) (𝓝 x) (𝓝 (x * x⁻¹)), { simpa }, exact tendsto_id.mul tendsto_const_nhds }, { suffices : tendsto (Ξ»y:Ξ±, y * x) (𝓝 1) (𝓝 (1 * x)), { simpa }, exact tendsto_id.mul tendsto_const_nhds } end @[to_additive] lemma topological_group.ext {G : Type*} [group G] {t t' : topological_space G} (tg : @topological_group G t _) (tg' : @topological_group G t' _) (h : @nhds G t 1 = @nhds G t' 1) : t = t' := eq_of_nhds_eq_nhds $ Ξ» x, by rw [← @nhds_translation_mul_inv G t _ _ x , ← @nhds_translation_mul_inv G t' _ _ x , ← h] end topological_group section quotient_topological_group variables [topological_space Ξ±] [group Ξ±] [topological_group Ξ±] (N : set Ξ±) [normal_subgroup N] @[to_additive] instance {Ξ± : Type u} [group Ξ±] [topological_space Ξ±] (N : set Ξ±) [normal_subgroup N] : topological_space (quotient_group.quotient N) := by dunfold quotient_group.quotient; apply_instance open quotient_group @[to_additive quotient_add_group_saturate] lemma quotient_group_saturate {Ξ± : Type u} [group Ξ±] (N : set Ξ±) [normal_subgroup N] (s : set Ξ±) : (coe : Ξ± β†’ quotient N) ⁻¹' ((coe : Ξ± β†’ quotient N) '' s) = (⋃ x : N, (Ξ» y, y*x.1) '' s) := begin ext x, simp only [mem_preimage, mem_image, mem_Union, quotient_group.eq], split, { exact assume ⟨a, a_in, h⟩, ⟨⟨_, h⟩, a, a_in, mul_inv_cancel_left _ _⟩ }, { exact assume ⟨⟨i, hi⟩, a, ha, eq⟩, ⟨a, ha, by simp only [eq.symm, (mul_assoc _ _ _).symm, inv_mul_cancel_left, hi]⟩ } end @[to_additive] lemma quotient_group.open_coe : is_open_map (coe : Ξ± β†’ quotient N) := begin intros s s_op, change is_open ((coe : Ξ± β†’ quotient N) ⁻¹' (coe '' s)), rw quotient_group_saturate N s, apply is_open_Union, rintro ⟨n, _⟩, exact is_open_map_mul_right n s s_op end @[to_additive topological_add_group_quotient] instance topological_group_quotient : topological_group (quotient N) := { continuous_mul := begin have cont : continuous ((coe : Ξ± β†’ quotient N) ∘ (Ξ» (p : Ξ± Γ— Ξ±), p.fst * p.snd)) := continuous_quot_mk.comp continuous_mul, have quot : quotient_map (Ξ» p : Ξ± Γ— Ξ±, ((p.1:quotient N), (p.2:quotient N))), { apply is_open_map.to_quotient_map, { exact is_open_map.prod (quotient_group.open_coe N) (quotient_group.open_coe N) }, { exact (continuous_quot_mk.comp continuous_fst).prod_mk (continuous_quot_mk.comp continuous_snd) }, { rintro ⟨⟨x⟩, ⟨y⟩⟩, exact ⟨(x, y), rfl⟩ } }, exact (quotient_map.continuous_iff quot).2 cont, end, continuous_inv := begin apply continuous_quotient_lift, change continuous ((coe : Ξ± β†’ quotient N) ∘ (Ξ» (a : Ξ±), a⁻¹)), exact continuous_quot_mk.comp continuous_inv end } attribute [instance] topological_add_group_quotient end quotient_topological_group section topological_add_group variables [topological_space Ξ±] [add_group Ξ±] lemma continuous.sub [topological_add_group Ξ±] [topological_space Ξ²] {f : Ξ² β†’ Ξ±} {g : Ξ² β†’ Ξ±} (hf : continuous f) (hg : continuous g) : continuous (Ξ»x, f x - g x) := by simp; exact hf.add hg.neg lemma continuous_sub [topological_add_group Ξ±] : continuous (Ξ»p:Ξ±Γ—Ξ±, p.1 - p.2) := continuous_fst.sub continuous_snd lemma continuous_on.sub [topological_add_group Ξ±] [topological_space Ξ²] {f : Ξ² β†’ Ξ±} {g : Ξ² β†’ Ξ±} {s : set Ξ²} (hf : continuous_on f s) (hg : continuous_on g s) : continuous_on (Ξ»x, f x - g x) s := continuous_sub.comp_continuous_on (hf.prod hg) lemma filter.tendsto.sub [topological_add_group Ξ±] {f : Ξ² β†’ Ξ±} {g : Ξ² β†’ Ξ±} {x : filter Ξ²} {a b : Ξ±} (hf : tendsto f x (𝓝 a)) (hg : tendsto g x (𝓝 b)) : tendsto (Ξ»x, f x - g x) x (𝓝 (a - b)) := by simp; exact hf.add hg.neg lemma nhds_translation [topological_add_group Ξ±] (x : Ξ±) : comap (Ξ»y:Ξ±, y - x) (𝓝 0) = 𝓝 x := nhds_translation_add_neg x end topological_add_group section prio set_option default_priority 100 -- see Note [default priority] /-- additive group with a neighbourhood around 0. Only used to construct a topology and uniform space. This is currently only available for commutative groups, but it can be extended to non-commutative groups too. -/ class add_group_with_zero_nhd (Ξ± : Type u) extends add_comm_group Ξ± := (Z : filter Ξ±) (zero_Z {} : pure 0 ≀ Z) (sub_Z {} : tendsto (Ξ»p:Ξ±Γ—Ξ±, p.1 - p.2) (Z.prod Z) Z) end prio namespace add_group_with_zero_nhd variables (Ξ±) [add_group_with_zero_nhd Ξ±] local notation `Z` := add_group_with_zero_nhd.Z @[priority 100] -- see Note [lower instance priority] instance : topological_space Ξ± := topological_space.mk_of_nhds $ Ξ»a, map (Ξ»x, x + a) (Z Ξ±) variables {Ξ±} lemma neg_Z : tendsto (Ξ»a:Ξ±, - a) (Z Ξ±) (Z Ξ±) := have tendsto (Ξ»a, (0:Ξ±)) (Z Ξ±) (Z Ξ±), by refine le_trans (assume h, _) zero_Z; simp [univ_mem_sets'] {contextual := tt}, have tendsto (Ξ»a:Ξ±, 0 - a) (Z Ξ±) (Z Ξ±), from sub_Z.comp (tendsto.prod_mk this tendsto_id), by simpa lemma add_Z : tendsto (Ξ»p:Ξ±Γ—Ξ±, p.1 + p.2) ((Z Ξ±).prod (Z Ξ±)) (Z Ξ±) := suffices tendsto (Ξ»p:Ξ±Γ—Ξ±, p.1 - -p.2) ((Z Ξ±).prod (Z Ξ±)) (Z Ξ±), by simpa, sub_Z.comp (tendsto.prod_mk tendsto_fst (neg_Z.comp tendsto_snd)) lemma exists_Z_half {s : set Ξ±} (hs : s ∈ Z Ξ±) : βˆƒ V ∈ Z Ξ±, βˆ€ v w ∈ V, v + w ∈ s := begin have : ((Ξ»a:Ξ±Γ—Ξ±, a.1 + a.2) ⁻¹' s) ∈ (Z Ξ±).prod (Z Ξ±) := add_Z (by simpa using hs), rcases mem_prod_iff.1 this with ⟨V₁, H₁, Vβ‚‚, Hβ‚‚, H⟩, exact ⟨V₁ ∩ Vβ‚‚, inter_mem_sets H₁ Hβ‚‚, assume v w ⟨hv, _⟩ ⟨_, hw⟩, @H (v, w) ⟨hv, hw⟩⟩ end lemma nhds_eq (a : Ξ±) : 𝓝 a = map (Ξ»x, x + a) (Z Ξ±) := topological_space.nhds_mk_of_nhds _ _ (assume a, calc pure a = map (Ξ»x, x + a) (pure 0) : by simp ... ≀ _ : map_mono zero_Z) (assume b s hs, let ⟨t, ht, eqt⟩ := exists_Z_half hs in have t0 : (0:Ξ±) ∈ t, by simpa using zero_Z ht, begin refine ⟨(Ξ»x:Ξ±, x + b) '' t, image_mem_map ht, _, _⟩, { refine set.image_subset_iff.2 (assume b hbt, _), simpa using eqt 0 b t0 hbt }, { rintros _ ⟨c, hb, rfl⟩, refine (Z Ξ±).sets_of_superset ht (assume x hxt, _), simpa using eqt _ _ hxt hb } end) lemma nhds_zero_eq_Z : 𝓝 0 = Z Ξ± := by simp [nhds_eq]; exact filter.map_id @[priority 100] -- see Note [lower instance priority] instance : topological_add_monoid Ξ± := ⟨ continuous_iff_continuous_at.2 $ assume ⟨a, b⟩, begin rw [continuous_at, nhds_prod_eq, nhds_eq, nhds_eq, nhds_eq, filter.prod_map_map_eq, tendsto_map'_iff], suffices : tendsto ((Ξ»x:Ξ±, (a + b) + x) ∘ (Ξ»p:Ξ±Γ—Ξ±,p.1 + p.2)) (filter.prod (Z Ξ±) (Z Ξ±)) (map (Ξ»x:Ξ±, (a + b) + x) (Z Ξ±)), { simpa [(∘)] }, exact tendsto_map.comp add_Z end⟩ @[priority 100] -- see Note [lower instance priority] instance : topological_add_group Ξ± := ⟨continuous_iff_continuous_at.2 $ assume a, begin rw [continuous_at, nhds_eq, nhds_eq, tendsto_map'_iff], suffices : tendsto ((Ξ»x:Ξ±, x - a) ∘ (Ξ»x:Ξ±, -x)) (Z Ξ±) (map (Ξ»x:Ξ±, x - a) (Z Ξ±)), { simpa [(∘)] }, exact tendsto_map.comp neg_Z end⟩ end add_group_with_zero_nhd section filter_mul local attribute [instance] set.pointwise_one set.pointwise_mul set.pointwise_add filter.pointwise_mul filter.pointwise_add filter.pointwise_one section variables [topological_space Ξ±] [group Ξ±] [topological_group Ξ±] @[to_additive] lemma is_open_pointwise_mul_left {s t : set Ξ±} : is_open t β†’ is_open (s * t) := Ξ» ht, begin have : βˆ€a, is_open ((Ξ» (x : Ξ±), a * x) '' t), assume a, apply is_open_map_mul_left, exact ht, rw pointwise_mul_eq_Union_mul_left, exact is_open_Union (Ξ»a, is_open_Union $ Ξ»ha, this _), end @[to_additive] lemma is_open_pointwise_mul_right {s t : set Ξ±} : is_open s β†’ is_open (s * t) := Ξ» hs, begin have : βˆ€a, is_open ((Ξ» (x : Ξ±), x * a) '' s), assume a, apply is_open_map_mul_right, exact hs, rw pointwise_mul_eq_Union_mul_right, exact is_open_Union (Ξ»a, is_open_Union $ Ξ»ha, this _), end variables (Ξ±) lemma topological_group.t1_space (h : @is_closed Ξ± _ {1}) : t1_space Ξ± := ⟨assume x, by { convert is_closed_map_mul_right x _ h, simp }⟩ lemma topological_group.regular_space [t1_space Ξ±] : regular_space Ξ± := ⟨assume s a hs ha, let f := Ξ» p : Ξ± Γ— Ξ±, p.1 * (p.2)⁻¹ in have hf : continuous f := continuous_mul.comp (continuous_fst.prod_mk (continuous_inv.comp continuous_snd)), -- a ∈ -s implies f (a, 1) ∈ -s, and so (a, 1) ∈ f⁻¹' (-s); -- and so can find t₁ tβ‚‚ open such that a ∈ t₁ Γ— tβ‚‚ βŠ† f⁻¹' (-s) let ⟨t₁, tβ‚‚, ht₁, htβ‚‚, a_mem_t₁, one_mem_tβ‚‚, t_subset⟩ := is_open_prod_iff.1 (hf _ (is_open_compl_iff.2 hs)) a (1:Ξ±) (by simpa [f]) in begin use s * tβ‚‚, use is_open_pointwise_mul_left htβ‚‚, use Ξ» x hx, ⟨x, hx, 1, one_mem_tβ‚‚, (mul_one _).symm⟩, apply inf_principal_eq_bot, rw mem_nhds_sets_iff, refine ⟨t₁, _, ht₁, a_mem_tβ‚βŸ©, rintros x hx ⟨y, hy, z, hz, yz⟩, have : x * z⁻¹ ∈ -s := (prod_subset_iff.1 t_subset) x hx z hz, have : x * z⁻¹ ∈ s, rw yz, simpa, contradiction end⟩ local attribute [instance] topological_group.regular_space lemma topological_group.t2_space [t1_space Ξ±] : t2_space Ξ± := regular_space.t2_space Ξ± end section variables [topological_space Ξ±] [comm_group Ξ±] [topological_group Ξ±] @[to_additive] lemma nhds_pointwise_mul (x y : Ξ±) : 𝓝 (x * y) = 𝓝 x * 𝓝 y := filter_eq $ set.ext $ assume s, begin rw [← nhds_translation_mul_inv x, ← nhds_translation_mul_inv y, ← nhds_translation_mul_inv (x*y)], split, { rintros ⟨t, ht, ts⟩, rcases exists_nhds_split ht with ⟨V, V_mem, h⟩, refine ⟨(Ξ»a, a * x⁻¹) ⁻¹' V, ⟨V, V_mem, subset.refl _⟩, (Ξ»a, a * y⁻¹) ⁻¹' V, ⟨V, V_mem, subset.refl _⟩, _⟩, rintros a ⟨v, v_mem, w, w_mem, rfl⟩, apply ts, simpa [mul_comm, mul_assoc, mul_left_comm] using h (v * x⁻¹) (w * y⁻¹) v_mem w_mem }, { rintros ⟨a, ⟨b, hb, ba⟩, c, ⟨d, hd, dc⟩, ac⟩, refine ⟨b ∩ d, inter_mem_sets hb hd, assume v, _⟩, simp only [preimage_subset_iff, mul_inv_rev, mem_preimage] at *, rintros ⟨vb, vd⟩, refine ac ⟨v * y⁻¹, _, y, _, _⟩, { rw ← mul_assoc _ _ _ at vb, exact ba _ vb }, { apply dc y, rw mul_right_inv, exact mem_of_nhds hd }, { simp only [inv_mul_cancel_right] } } end @[to_additive] lemma nhds_is_mul_hom : is_mul_hom (Ξ»x:Ξ±, 𝓝 x) := ⟨λ_ _, nhds_pointwise_mul _ _⟩ end end filter_mul
226ee6562c3bcccbc2bebf35af91ce9837cd866e
4a092885406df4e441e9bb9065d9405dacb94cd8
/src/Spa.lean
cd15b8f1eb19f620b680cb929083787c41380b5c
[ "Apache-2.0" ]
permissive
semorrison/lean-perfectoid-spaces
78c1572cedbfae9c3e460d8aaf91de38616904d8
bb4311dff45791170bcb1b6a983e2591bee88a19
refs/heads/master
1,588,841,765,494
1,554,805,620,000
1,554,805,620,000
180,353,546
0
1
null
1,554,809,880,000
1,554,809,880,000
null
UTF-8
Lean
false
false
22,522
lean
import ring_theory.localization import ring_theory.subring import continuous_valuations import Huber_pair import Huber_ring.localization import for_mathlib.nonarchimedean.basic import for_mathlib.data.set.finite import for_mathlib.uniform_space.ring -- need completions of rings plus UMP import for_mathlib.group -- some stupid lemma about units universes u₁ uβ‚‚ u₃ local attribute [instance, priority 0] classical.prop_decidable open set function Spv valuation variables {Ξ“ : Type*} [linear_ordered_comm_group Ξ“] -- Wedhorn def 7.23. definition Spa (A : Huber_pair) : set (Spv A) := {v | v.is_continuous ∧ βˆ€ r ∈ A⁺, v r ≀ 1} lemma mk_mem_Spa {A : Huber_pair} {v : valuation A Ξ“} : mk v ∈ Spa A ↔ v.is_continuous ∧ βˆ€ r ∈ A⁺, v r ≀ 1 := begin apply and_congr, { apply is_equiv.is_continuous_iff, apply out_mk, }, { apply forall_congr, intro r, apply forall_congr, intro hr, convert (out_mk v) r 1; rw [valuation.map_one] } end namespace Spa variable {A : Huber_pair} instance : has_coe (Spa A) (Spv A) := ⟨subtype.val⟩ definition basic_open (r s : A) : set (Spa A) := {v | v r ≀ v s ∧ v s β‰  0 } lemma mk_mem_basic_open {r s : A} {v : valuation A Ξ“} {hv : mk v ∈ Spa A} : (⟨mk v, hv⟩ : Spa A) ∈ basic_open r s ↔ v r ≀ v s ∧ v s β‰  0 := begin apply and_congr, { apply out_mk, }, { apply (out_mk v).ne_zero, }, end instance (A : Huber_pair) : topological_space (Spa A) := topological_space.generate_from {U : set (Spa A) | βˆƒ r s : A, U = basic_open r s} lemma basic_open.is_open (r s : A) : is_open (basic_open r s) := topological_space.generate_open.basic (basic_open r s) ⟨r, s, rfl⟩ lemma basic_open_eq (s : A) : basic_open s s = {v | v s β‰  0} := set.ext $ Ξ» v, ⟨λ h, h.right, Ξ» h, ⟨le_refl _, h⟩⟩ -- should only be applied with (Hfin : fintype T) and (Hopen: is_open (span T)) definition rational_open (s : A) (T : set A) : set (Spa A) := {v | (βˆ€ t ∈ T, (v t ≀ v s)) ∧ (v s β‰  0)} -- Here's everything in one package. structure rational_open_data (A : Huber_pair) := (s : A) (T : set A) (Tfin : fintype T) (Hopen : is_open ((ideal.span T) : set A)) instance (r : rational_open_data A) : fintype r.T := r.Tfin namespace rational_open_data def ext (r₁ rβ‚‚ : rational_open_data A) (hs : r₁.s = rβ‚‚.s) (hT : r₁.T = rβ‚‚.T) : r₁ = rβ‚‚ := begin cases r₁, cases rβ‚‚, congr; assumption end def rational_open (r : rational_open_data A) : set (Spa A) := rational_open r.s r.T def localization (r : rational_open_data A) := Huber_ring.away r.T r.s instance (r : rational_open_data A) : ring_with_zero_nhd (localization r) := Huber_ring.away.ring_with_zero_nhd r.T r.s r.Hopen instance (r : rational_open_data A) : comm_ring (localization r) := by unfold localization; apply_instance open algebra instance (r : rational_open_data A) : algebra A (localization r) := Huber_ring.away.algebra r.T r.s /- In this file, we are going to take a projective limit over a preordered set of rings, to make a presheaf. The underlying type of this preorder is `rational_open_data A`. The correct preorder on rational open data: def correct_preorder : preorder (rational_open_data A) := { le := Ξ» r1 r2, rational_open r1 βŠ† rational_open r2, le_refl := Ξ» _ _, id, le_trans := Ξ» _ _ _, subset.trans, } One can prove (in maths) that r1 ≀ r2 iff there's a continuous R-algebra morphism of Huber pairs localization r2 β†’ localization r1. I think the ← direction of this iff is straightforward (but I didn't think about it too carefully). However we definitely cannot prove the β†’ direction of this iff in this repo yet because we don't have enough API for cont. Here is an indication of part of the problem. localization r2 is just A[1/r2.s]. But we cannot prove yet r2.s is invertible in localization.r1, even though we know it doesn't canish anywhere on rational_open r2 and hence on rational_open r1, because the fact that it doesn't vanish anywhere on rational_open r1 only means that it's not in any prime ideal corresponding to a *continuous* valuation on localization r1; one would now need to prove that every maximal ideal is the support of a continuous valuation, which is Wedhorn 7.52(2). This is not too bad -- but it is work that we have not yet done. This is not the whole story though; we would also need that r1.T is power-bounded in localization.r2 and this looks worse: it's Wedhorn 7.52(1). Everything is do-able, but it's just *long*. Long as in "thousands more lines of code". We have to work with a weaker preorder then, because haven't made a good enough API for continuous valuations. We basically work with the preorder r1 ≀ r2 iff there's a continuous R-algebra map localization r2 β†’ localization r1, i.e, we define our way around the problem. We are fortunate in that we can prove (in maths) that the projective limit over this preorder agrees with the projective limit over the correct preorder. -/ -- note: I don't think we ever use le_refl or le_trans instance : preorder (rational_open_data A) := { le := Ξ» r1 r2, βˆƒ k : A, r1.s * k = r2.s ∧ βˆ€ t₁ ∈ r1.T, βˆƒ tβ‚‚ ∈ r2.T, βˆƒ N : β„•, r2.s ^ N * tβ‚‚ = r2.s ^ N * (t₁ * k), le_refl := Ξ» r, ⟨1, mul_one _, Ξ» t ht, ⟨t, ht, 0, by rw mul_one⟩⟩, le_trans := Ξ» a b c ⟨k, hk, hab⟩ ⟨l, hl, hbc⟩, ⟨k * l, by rw [←mul_assoc, hk, hl], Ξ» ta hta, begin rcases hab ta hta with ⟨tb, htb, Nab, h1⟩, rcases hbc tb htb with ⟨hc, htc, Nbc, h2⟩, use hc, use htc, use (Nab + Nbc), rw [←mul_assoc, pow_add, mul_assoc, h2, ←hl, mul_pow, mul_pow], rw (show b.s ^ Nab * l ^ Nab * (b.s ^ Nbc * l ^ Nbc * (tb * l)) = b.s ^ Nab * tb * (l ^ Nab * (b.s ^ Nbc * l ^ Nbc * l)), by ring), rw h1, ring end⟩ } -- our preorder is weaker than the preorder we're supposed to have but don't. However -- the projective limit we take over our preorder is provably (in maths) equal to -- the projective limit that we cannot even formalise. The thing we definitely need -- is that if r1 ≀ r2 then there's a map localization r1 β†’ localization r2 /-- This awful function produces r1.s as a unit in localization r2 -/ noncomputable def s_inv_aux (r1 r2 : rational_open_data A) (h : r1 ≀ r2) : units (localization r2) := @units.unit_of_mul_left_eq_unit _ _ ((of_id A (localization r2)).to_fun r1.s) ((of_id A (localization r2)).to_fun (classical.some h)) (localization.to_units (⟨r2.s, 1, by simp⟩ : powers r2.s)) (begin suffices : (of_id A (localization r2)).to_fun (r1.s * classical.some h) = (localization.to_units (⟨r2.s, 1, by simp⟩ : powers r2.s)).val, convert this, convert (is_ring_hom.map_mul _).symm, apply_instance, -- stupid type class inference rw (classical.some_spec h).1, refl, end) noncomputable def rational_open_subset.restriction {r1 r2 : rational_open_data A} (h : r1 ≀ r2) : localization r1 β†’ localization r2 := Huber_ring.away.lift r1.T r1.s ( show ((s_inv_aux r1 r2 h)⁻¹).inv = (of_id A (localization r2)).to_fun r1.s, from rfl) def localization.nonarchimedean (r : rational_open_data A) : topological_add_group.nonarchimedean (localization r) := of_submodules_comm.nonarchimedean section open localization submodule Huber_ring.away local attribute [instance] set.pointwise_mul_comm_semiring local attribute [instance] set.mul_action def localization.power_bounded_data (r : rational_open_data A) : set (localization r) := let s_inv : localization r := ((to_units ⟨r.s, ⟨1, by simp⟩⟩)⁻¹ : units (localization r)) in (s_inv β€’ of_id A (localization r) '' r.T) set_option class.instance_max_depth 50 theorem localization.power_bounded (r : rational_open_data A) : is_power_bounded_subset (localization.power_bounded_data r) := begin apply bounded.subset, work_on_goal 0 { apply add_group.subset_closure }, show is_bounded (ring.closure (localization.power_bounded_data r)), intros U hU, rw of_submodules_comm.nhds_zero at hU, cases hU with V hV, refine ⟨_, mem_nhds_sets (of_submodules_comm.is_open V) _, _⟩, { rw submodule.mem_coe, exact submodule.zero_mem _ }, { intros v hv b hb, apply hV, rw mul_comm, rw submodule.mem_coe at hv ⊒, convert submodule.smul_mem _ _ hv, work_on_goal 1 { exact ⟨b, hb⟩ }, refl } end end -- To prove continuity I need to check that something is power-bounded; that's where -- the work is here. The claim could be factored out, it's the 40 line begin/end block. lemma rational_open_subset.restriction_is_cts {r1 r2 : rational_open_data A} (h : r1 ≀ r2) : continuous (rational_open_subset.restriction h) := Huber_ring.away.lift_continuous r1.T r1.s (localization.nonarchimedean r2) (Huber_ring.away.of_continuous r2.T r2.s (show ((localization.to_units (⟨r2.s, 1, by simp⟩ : powers r2.s))⁻¹ : units (localization r2)).inv = (of_id A (localization r2)).to_fun r2.s, from rfl) r2.Hopen) _ _ (begin refine power_bounded.subset _ (localization.power_bounded r2), intros x hx, rcases hx with ⟨y, hy, hz, ⟨t₁, ht₁, rfl⟩, rfl⟩, rw mem_singleton_iff at hy, rw hy, clear hy, clear y, let h' := h, -- need it later rcases h with ⟨a, ha, hβ‚‚βŸ©, rcases hβ‚‚ t₁ ht₁ with ⟨tβ‚‚, htβ‚‚, N, hN⟩, show ↑(s_inv_aux r1 r2 _)⁻¹ * to_fun (localization r2) t₁ ∈ localization.mk 1 ⟨r2.s, _⟩ β€’ (of_id β†₯A (localization r2)).to_fun '' r2.T, rw mem_smul_set, use (of_id β†₯A (localization r2)).to_fun tβ‚‚, existsi _, swap, rw mem_image, use tβ‚‚, use htβ‚‚, rw [←units.mul_left_inj (s_inv_aux r1 r2 h'), units.mul_inv_cancel_left], show to_fun (localization r2) t₁ = to_fun (localization r2) (r1.s) * (localization.mk 1 ⟨r2.s, _⟩ * to_fun (localization r2) tβ‚‚), rw [mul_comm, mul_assoc], rw ←units.mul_left_inj (localization.to_units (⟨r2.s, 1, by simp⟩ : powers r2.s)), rw ←mul_assoc, -- t1=s1*(1/s2 * t2) in r2 have : ↑(localization.to_units (⟨r2.s, 1, by simp⟩ : powers r2.s)) * localization.mk (1 : A) (⟨r2.s, 1, by simp⟩ : powers r2.s) = 1, convert units.mul_inv _, rw [this, one_mul], clear this, show to_fun (localization r2) r2.s * _ = _, rw ←units.mul_left_inj (localization.to_units (⟨r2.s ^ N, N, rfl⟩ : powers r2.s)), show to_fun (localization r2) (r2.s ^ N) * _ = to_fun (localization r2) (r2.s ^ N) * _, have hrh : is_ring_hom (to_fun (localization r2)) := begin change is_ring_hom ((of_id β†₯A (localization r2)).to_fun), apply_instance, end, rw ←@is_ring_hom.map_mul _ _ _ _ (to_fun (localization r2)) hrh, rw ←@is_ring_hom.map_mul _ _ _ _ (to_fun (localization r2)) hrh, rw ←@is_ring_hom.map_mul _ _ _ _ (to_fun (localization r2)) hrh, rw ←@is_ring_hom.map_mul _ _ _ _ (to_fun (localization r2)) hrh, congr' 1, rw [←mul_assoc _ tβ‚‚, hN], rw ←ha, ring, end) -- Note: I don't think we ever use this. noncomputable def insert_s (r : rational_open_data A) : rational_open_data A := { s := r.s, T := insert r.s r.T, Tfin := set.fintype_insert r.s r.T, -- noncomputable! Hopen := submodule.is_open_of_open_submodule ⟨ideal.span (r.T), r.Hopen, ideal.span_mono $ set.subset_insert _ _⟩ } -- not sure we ever use this /- noncomputable def inter_aux (r1 r2 : rational_open_data A) : rational_open_data A := { s := r1.s * r2.s, T := r1.T * r2.T, Tfin := by apply_instance, Hopen := sorry /- need "product of open subgroups is open in a Huber ring" (because subgroup is open iff it contains I^N for some ideal of definition) -/ } -/ end rational_open_data lemma mk_mem_rational_open {s : A} {T : set A} {v : valuation A Ξ“} {hv : mk v ∈ Spa A} : (⟨mk v, hv⟩ : Spa A) ∈ rational_open s T ↔ (βˆ€ t ∈ T, (v t ≀ v s)) ∧ (v s β‰  0) := begin apply and_congr, { apply forall_congr, intro t, apply forall_congr, intro ht, apply out_mk }, { apply (out_mk v).ne_zero } end definition rational_open_bInter (s : A) (T : set A) : rational_open s T = (β‹‚ t ∈ T, basic_open t s) ∩ {v | v s β‰  0} := begin ext v, split; rintros ⟨h₁, hβ‚‚βŸ©; split; try { exact hβ‚‚ }, { erw set.mem_bInter_iff, intros t ht, split, { exact h₁ t ht, }, { exact hβ‚‚ } }, { intros t ht, erw set.mem_bInter_iff at h₁, exact (h₁ t ht).1 } end lemma rational_open_add_s (s : A) (T : set A) : rational_open s T = rational_open s (insert s T) := begin ext v, split; rintros ⟨h₁, hβ‚‚βŸ©; split; try { exact hβ‚‚ }; intros t ht, { cases ht, { rw ht, exact le_refl _ }, { exact h₁ t ht } }, { apply h₁ t, exact mem_insert_of_mem _ ht } end lemma rational_open.is_open (s : A) (T : set A) [h : fintype T] : is_open (rational_open s T) := begin rw rational_open_bInter, apply is_open_inter, { apply is_open_bInter ⟨h⟩, intros, apply basic_open.is_open }, { rw ← basic_open_eq s, apply basic_open.is_open }, end lemma rational_open_inter.aux₁ {s₁ sβ‚‚ : A} {T₁ Tβ‚‚ : set A} (h₁ : s₁ ∈ T₁) (hβ‚‚ : sβ‚‚ ∈ Tβ‚‚) : rational_open s₁ T₁ ∩ rational_open sβ‚‚ Tβ‚‚ βŠ† rational_open (s₁ * sβ‚‚) ((*) <$> T₁ <*> Tβ‚‚) := begin rintros v ⟨⟨hv₁, hsβ‚βŸ©, ⟨hvβ‚‚, hsβ‚‚βŸ©βŸ©, split, { rintros t ⟨_, ⟨t₁, ht₁, rfl⟩, ⟨tβ‚‚, htβ‚‚, ht⟩⟩, subst ht, convert le_trans (linear_ordered_comm_monoid.mul_le_mul_right (hv₁ t₁ ht₁) _) (linear_ordered_comm_monoid.mul_le_mul_left (hvβ‚‚ tβ‚‚ htβ‚‚) _); apply valuation.map_mul }, { rw with_zero.ne_zero_iff_exists at hs₁ hsβ‚‚, cases hs₁ with γ₁ hγ₁, cases hsβ‚‚ with Ξ³β‚‚ hΞ³β‚‚, erw [valuation.map_mul, hγ₁, hΞ³β‚‚], exact with_zero.coe_ne_zero }, end lemma rational_open_inter.auxβ‚‚ {s₁ sβ‚‚ : A} {T₁ Tβ‚‚ : set A} (h₁ : s₁ ∈ T₁) (hβ‚‚ : sβ‚‚ ∈ Tβ‚‚) : rational_open (s₁ * sβ‚‚) ((*) <$> T₁ <*> Tβ‚‚) βŠ† rational_open s₁ T₁ ∩ rational_open sβ‚‚ Tβ‚‚ := begin rintros v ⟨hv, hs⟩, have vmuls : v (s₁ * sβ‚‚) = v s₁ * v sβ‚‚ := valuation.map_mul _ _ _, have hs₁ : v s₁ β‰  0 := Ξ» H, by simpa [-coe_fn_coe_base, vmuls, H] using hs, have hsβ‚‚ : v sβ‚‚ β‰  0 := Ξ» H, by simpa [-coe_fn_coe_base, vmuls, H] using hs, split; split; try { assumption }; intros t ht; rw with_zero.ne_zero_iff_exists at hs₁ hsβ‚‚, { suffices H : v t * v sβ‚‚ ≀ v s₁ * v sβ‚‚, { cases hsβ‚‚ with Ξ³ hΞ³, rw hΞ³ at H, have := linear_ordered_comm_monoid.mul_le_mul_right H γ⁻¹, simp [mul_assoc, -coe_fn_coe_base] at this, erw [mul_one, mul_one] at this, exact this }, { erw [← valuation.map_mul, ← valuation.map_mul], exact hv (t * sβ‚‚) ⟨_, ⟨t, ht, rfl⟩, ⟨sβ‚‚, hβ‚‚, rfl⟩⟩, } }, { suffices H : v s₁ * v t ≀ v s₁ * v sβ‚‚, { cases hs₁ with Ξ³ hΞ³, rw hΞ³ at H, have := linear_ordered_comm_monoid.mul_le_mul_left H γ⁻¹, erw [← mul_assoc, ← mul_assoc] at this, simp [-coe_fn_coe_base] at this, erw [one_mul, one_mul] at this, exact this }, { erw [← valuation.map_mul, ← valuation.map_mul], exact hv _ ⟨_, ⟨s₁, h₁, rfl⟩, ⟨t, ht, rfl⟩⟩ } }, end lemma rational_open_inter {s₁ sβ‚‚ : A} {T₁ Tβ‚‚ : set A} (h₁ : s₁ ∈ T₁) (hβ‚‚ : sβ‚‚ ∈ Tβ‚‚) : rational_open s₁ T₁ ∩ rational_open sβ‚‚ Tβ‚‚ = rational_open (s₁ * sβ‚‚) ((*) <$> T₁ <*> Tβ‚‚) := le_antisymm (rational_open_inter.aux₁ h₁ hβ‚‚) (rational_open_inter.auxβ‚‚ h₁ hβ‚‚) @[simp] lemma rational_open_singleton {r s : A} : rational_open s {r} = basic_open r s := begin apply le_antisymm; rintros v ⟨h₁, hβ‚‚βŸ©; split; intros; simp [*] at *, end @[simp] lemma basic_open_eq_univ : basic_open (1 : A) (1 : A) = univ := univ_subset_iff.1 $ Ξ» v h, ⟨le_refl _,by erw valuation.map_one; exact one_ne_zero⟩ @[simp] lemma rational_open_eq_univ : rational_open (1 : A) {(1 : A)} = univ := by simp def rational_basis (A : Huber_pair) : set (set (Spa A)) := {U : set (Spa A) | βˆƒ {s : A} {T : set A} {hfin : fintype T} {hopen : is_open (ideal.span T).carrier}, U = rational_open s T } lemma rational_basis.is_basis.aux₁ (s₁ sβ‚‚ : A) (T₁ Tβ‚‚ : set A) : (*) <$> T₁ <*> Tβ‚‚ βŠ† (*) <$> (insert s₁ T₁) <*> (insert sβ‚‚ Tβ‚‚) := begin rintros t ⟨_, ⟨t₁, ht₁, rfl⟩, ⟨tβ‚‚, htβ‚‚, ht⟩⟩, exact ⟨_, ⟨t₁, mem_insert_of_mem _ ht₁, rfl⟩, ⟨tβ‚‚, mem_insert_of_mem _ htβ‚‚, ht⟩⟩ end -- Current status: proof is broken with 2 sorries. -- KMB remarks that we don't need this result yet so he's commenting it out for now. -- We might well need it though! /- lemma rational_basis.is_basis : topological_space.is_topological_basis (rational_basis A) := begin split, { rintros U₁ ⟨s₁, T₁, hfin₁, hopen₁, Hβ‚βŸ© Uβ‚‚ ⟨sβ‚‚, Tβ‚‚, hfinβ‚‚, hopenβ‚‚, Hβ‚‚βŸ© v hv, use U₁ ∩ Uβ‚‚, rw rational_open_add_s at H₁ Hβ‚‚, split, { simp only [H₁, Hβ‚‚, rational_open_inter, set.mem_insert_iff, true_or, eq_self_iff_true], resetI, refine ⟨_, _, infer_instance, _, rfl⟩, -- jmc: what follows is a sketch. We can fill the gaps once we know more about topological rings have foo := ideal.span_mono (rational_basis.is_basis.aux₁ s₁ sβ‚‚ T₁ Tβ‚‚), suffices : is_open (ideal.span ((*) <$> T₁ <*> Tβ‚‚)).carrier, { sorry /- use "foo" from two lines up -/ }, { -- See the remarks in Wedhorn 7.30.(5). sorry } }, { exact ⟨hv, subset.refl _⟩ } }, split, { apply le_antisymm, { exact subset_univ _ }, { apply subset_sUnion_of_mem, refine ⟨(1 : A), {(1 : A)}, infer_instance, _, by simp⟩, rw ideal.span_singleton_one, exact is_open_univ, } }, { apply le_antisymm, { delta Spa.topological_space, rw generate_from_le_iff_subset_is_open, rintros U ⟨r, s, H⟩, rw [H, ← rational_open_singleton], refine topological_space.generate_open.basic _ ⟨s, {r}, infer_instance, _, rfl⟩, sorry -- is this even true? I guess we shouldn't do the rw 2 lines up. -- Instead, we should use a smarter argument that I haven't cooked up yet. }, { rw generate_from_le_iff_subset_is_open, rintros U ⟨s, T, hT, hT', H⟩, subst H, haveI := hT, exact rational_open.is_open s T, } } end -/ -- KMB remarks that we don't need the presheaf on the basic either /- namespace rational_open def presheaf.aux (s : A) (T : set A) := localization.away s instance (s : A) (T : set A) : comm_ring (presheaf.aux s T) := by delta presheaf.aux; apply_instance -- Definition of A\left(\frac T s\right) as a topological ring def presheaf.topology (rod : rational_open_data A) : topological_space (presheaf.aux rod.s rod.T) := sorry /- let As := presheaf.aux s T in let S₁ : set As := localization.of '' A.RHuber.Aβ‚€ in let T' : set As := localization.of '' T in let Sβ‚‚ : set As := (*) (((localization.to_units s)⁻¹ : units As) : As) '' T' in -- need to update mathlib let S : set As := S₁ βˆͺ Sβ‚‚ in let D := ring.closure S in let I := classical.some A.RHuber.Aβ‚€_is_ring_of_definition.2 in adic_topology (I * D) -- let As := presheaf.aux s T in sorry /-let D := ring.closure ((localize s) '' A.RHuber.Aβ‚€ βˆͺ (((Ξ» x, x*s⁻¹) ∘ localize s) '' T)) in begin let nhd := Ξ» n : β„•, mul_ideal (pow_ideal ((localize s) '' A.Rplus) n) D, sorry end-/ -/ def presheaf' (s : A) (T : set A) [Hfin : fintype T] (Hopen : _root_.is_open ((ideal.span T) : set A)) : Type* := sorry -- ring_completion presheaf.aux s T end rational_open -/ section open topological_space def rational_open_data_subsets (U : opens (Spa A)) := { r : rational_open_data A // r.rational_open βŠ† U} def rational_open_data_subsets.map {U V : opens (Spa A)} (hUV : U ≀ V) (rd : rational_open_data_subsets U) : rational_open_data_subsets V := ⟨rd.val, set.subset.trans rd.property hUV⟩ instance (r : rational_open_data A) : uniform_space (rational_open_data.localization r) := topological_add_group.to_uniform_space _ def rational_open_data.presheaf (r : rational_open_data A) := ring_completion (rational_open_data.localization r) def presheaf (U : opens (Spa A)) := {f : Ξ  (rd : rational_open_data_subsets U), ring_completion (rational_open_data.localization rd.1) // βˆ€ (rd1 rd2 : rational_open_data_subsets U) (h : rd1.1 ≀ rd2.1), ring_completion.map (rational_open_data.rational_open_subset.restriction h) (f rd1) = (f rd2)} -- agrees on overlaps def presheaf.map {U V : opens (Spa A)} (hUV : U ≀ V) : presheaf V β†’ presheaf U := Ξ» f, ⟨λ rd, f.val ⟨rd.val, set.subset.trans rd.2 hUV⟩, begin intros, let X := f.2 (rational_open_data_subsets.map hUV rd1) (rational_open_data_subsets.map hUV rd2) h, exact X, end⟩ lemma presheaf.map_id (U : opens (Spa A)) : presheaf.map (le_refl U) = id := by { delta presheaf.map, tidy } lemma presheaf.map_comp {U V W : opens (Spa A)} (hUV : U ≀ V) (hVW : V ≀ W) : presheaf.map hUV ∘ presheaf.map hVW = presheaf.map (le_trans hUV hVW) := by { delta presheaf.map, tidy } end instance (rd : rational_open_data A): uniform_add_group (rational_open_data.localization rd) := topological_add_group_is_uniform noncomputable example (rd : rational_open_data A): ring (ring_completion (rational_open_data.localization rd)) := by apply_instance end Spa -- remember that a rational open is not actually `rational_open s T` in full -- generality -- we also need that T is finite and that T generates an open ideal in A. -- The construction on p73/74 (note typo in first line of p74 -- ideal should be I.D) -- gives A<T/s> (need completion) and A<T/s>^+ (need integral closure). -- Once we have this, we see mid way through p75 that the definition of the presheaf -- on V is proj lim of O_X(U) as U runs through rationals opens in V. This gets -- the projective limit topology and then we have a presheaf (hopefully this is -- straightforward) of complete topological rings (need proj lim of complete is complete) -- We then need the valuations on the stalks (this is direct limit in cat of rings, forget -- the topology). This will be fiddly but not impossible. -- We then have an object in V^pre and I think then everything should follow.
f1a822956f2782baa11a6bc28503a056225ab5b8
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/ring_theory/eisenstein_criterion.lean
f5966efa52132b9ae7058e4a91aa450c4cf7d754
[ "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
5,553
lean
/- Copyright (c) 2020 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import ring_theory.prime import ring_theory.polynomial.content /-! # Eisenstein's criterion A proof of a slight generalisation of Eisenstein's criterion for the irreducibility of a polynomial over an integral domain. -/ open polynomial ideal.quotient variables {R : Type*} [comm_ring R] namespace polynomial namespace eisenstein_criterion_aux /- Section for auxiliary lemmas used in the proof of `irreducible_of_eisenstein_criterion`-/ lemma map_eq_C_mul_X_pow_of_forall_coeff_mem {f : polynomial R} {P : ideal R} (hfP : βˆ€ (n : β„•), ↑n < f.degree β†’ f.coeff n ∈ P) (hf0 : f β‰  0) : map (mk P) f = C ((mk P) f.leading_coeff) * X ^ f.nat_degree := polynomial.ext (Ξ» n, begin rcases lt_trichotomy ↑n (degree f) with h | h | h, { erw [coeff_map, eq_zero_iff_mem.2 (hfP n h), coeff_C_mul, coeff_X_pow, if_neg, mul_zero], rintro rfl, exact not_lt_of_ge degree_le_nat_degree h }, { have : nat_degree f = n, from nat_degree_eq_of_degree_eq_some h.symm, rw [coeff_C_mul, coeff_X_pow, if_pos this.symm, mul_one, leading_coeff, this, coeff_map] }, { rw [coeff_eq_zero_of_degree_lt, coeff_eq_zero_of_degree_lt], { refine lt_of_le_of_lt (degree_C_mul_X_pow_le _ _) _, rwa ← degree_eq_nat_degree hf0 }, { exact lt_of_le_of_lt (degree_map_le _ _) h } } end) lemma le_nat_degree_of_map_eq_mul_X_pow {n : β„•} {P : ideal R} (hP : P.is_prime) {q : polynomial R} {c : polynomial P.quotient} (hq : map (mk P) q = c * X ^ n) (hc0 : c.degree = 0) : n ≀ q.nat_degree := with_bot.coe_le_coe.1 (calc ↑n = degree (q.map (mk P)) : by rw [hq, degree_mul, hc0, zero_add, degree_pow, degree_X, nsmul_one, nat.cast_with_bot] ... ≀ degree q : degree_map_le _ _ ... ≀ nat_degree q : degree_le_nat_degree) lemma eval_zero_mem_ideal_of_eq_mul_X_pow {n : β„•} {P : ideal R} {q : polynomial R} {c : polynomial P.quotient} (hq : map (mk P) q = c * X ^ n) (hn0 : 0 < n) : eval 0 q ∈ P := by rw [← coeff_zero_eq_eval_zero, ← eq_zero_iff_mem, ← coeff_map, coeff_zero_eq_eval_zero, hq, eval_mul, eval_pow, eval_X, zero_pow hn0, mul_zero] lemma is_unit_of_nat_degree_eq_zero_of_forall_dvd_is_unit {p q : polynomial R} (hu : βˆ€ (x : R), C x ∣ p * q β†’ is_unit x) (hpm : p.nat_degree = 0) : is_unit p := begin rw [eq_C_of_degree_le_zero (nat_degree_eq_zero_iff_degree_le_zero.1 hpm), is_unit_C], refine hu _ _, rw [← eq_C_of_degree_le_zero (nat_degree_eq_zero_iff_degree_le_zero.1 hpm)], exact dvd_mul_right _ _ end end eisenstein_criterion_aux open eisenstein_criterion_aux variables [is_domain R] /-- If `f` is a non constant polynomial with coefficients in `R`, and `P` is a prime ideal in `R`, then if every coefficient in `R` except the leading coefficient is in `P`, and the trailing coefficient is not in `P^2` and no non units in `R` divide `f`, then `f` is irreducible. -/ theorem irreducible_of_eisenstein_criterion {f : polynomial R} {P : ideal R} (hP : P.is_prime) (hfl : f.leading_coeff βˆ‰ P) (hfP : βˆ€ n : β„•, ↑n < degree f β†’ f.coeff n ∈ P) (hfd0 : 0 < degree f) (h0 : f.coeff 0 βˆ‰ P^2) (hu : f.is_primitive) : irreducible f := have hf0 : f β‰  0, from Ξ» _, by simp only [*, not_true, submodule.zero_mem, coeff_zero] at *, have hf : f.map (mk P) = C (mk P (leading_coeff f)) * X ^ nat_degree f, from map_eq_C_mul_X_pow_of_forall_coeff_mem hfP hf0, have hfd0 : 0 < f.nat_degree, from with_bot.coe_lt_coe.1 (lt_of_lt_of_le hfd0 degree_le_nat_degree), ⟨mt degree_eq_zero_of_is_unit (Ξ» h, by simp only [*, lt_irrefl] at *), begin rintros p q rfl, rw [map_mul] at hf, rcases mul_eq_mul_prime_pow (show prime (X : polynomial (ideal.quotient P)), from monic_X.prime_of_degree_eq_one degree_X) hf with ⟨m, n, b, c, hmnd, hbc, hp, hq⟩, have hmn : 0 < m β†’ 0 < n β†’ false, { assume hm0 hn0, refine h0 _, rw [coeff_zero_eq_eval_zero, eval_mul, sq], exact ideal.mul_mem_mul (eval_zero_mem_ideal_of_eq_mul_X_pow hp hm0) (eval_zero_mem_ideal_of_eq_mul_X_pow hq hn0) }, have hpql0 : (mk P) (p * q).leading_coeff β‰  0, { rwa [ne.def, eq_zero_iff_mem] }, have hp0 : p β‰  0, from Ξ» h, by simp only [*, zero_mul, eq_self_iff_true, not_true, ne.def] at *, have hq0 : q β‰  0, from Ξ» h, by simp only [*, eq_self_iff_true, not_true, ne.def, mul_zero] at *, have hbc0 : degree b = 0 ∧ degree c = 0, { apply_fun degree at hbc, rwa [degree_C hpql0, degree_mul, eq_comm, nat.with_bot.add_eq_zero_iff] at hbc }, have hmp : m ≀ nat_degree p, from le_nat_degree_of_map_eq_mul_X_pow hP hp hbc0.1, have hnq : n ≀ nat_degree q, from le_nat_degree_of_map_eq_mul_X_pow hP hq hbc0.2, have hpmqn : p.nat_degree = m ∧ q.nat_degree = n, { rw [nat_degree_mul hp0 hq0] at hmnd, clear_except hmnd hmp hnq, contrapose hmnd, apply ne_of_lt, rw not_and_distrib at hmnd, cases hmnd, { exact add_lt_add_of_lt_of_le (lt_of_le_of_ne hmp (ne.symm hmnd)) hnq }, { exact add_lt_add_of_le_of_lt hmp (lt_of_le_of_ne hnq (ne.symm hmnd)) } }, obtain rfl | rfl : m = 0 ∨ n = 0, { rwa [pos_iff_ne_zero, pos_iff_ne_zero, imp_false, not_not, ← or_iff_not_imp_left] at hmn }, { exact or.inl (is_unit_of_nat_degree_eq_zero_of_forall_dvd_is_unit hu hpmqn.1) }, { exact or.inr (is_unit_of_nat_degree_eq_zero_of_forall_dvd_is_unit (by simpa only [mul_comm] using hu) hpmqn.2) } end⟩ end polynomial
ec7d56357796a2fec53a1ba8bd7b0fdc3e9a66c6
3dc4623269159d02a444fe898d33e8c7e7e9461b
/.github/workflows/geo/Omega.lean
bdd9f18efee484f379c00c135c7e9a8f200cb246
[]
no_license
Or7ando/lean
cc003e6c41048eae7c34aa6bada51c9e9add9e66
d41169cf4e416a0d42092fb6bdc14131cee9dd15
refs/heads/master
1,650,600,589,722
1,587,262,906,000
1,587,262,906,000
255,387,160
0
0
null
null
null
null
UTF-8
Lean
false
false
1,820
lean
import .global import .ideals universes u local notation `Ring` := CommRing.{u} local notation `Set` := Type u namespace Omega def Ξ©_obj (A : Ring):= ideal A def Ξ©_map (A B :Ring)(ψ : A ⟢ B) : Ξ©_obj A β†’ Ξ©_obj B:= Ξ» I,begin exact ideal.map ψ I, end def Ξ© : Ring β₯€ Set := { obj := Ξ» A, ideal A, map := Ξ» A B ψ, ideal.map ψ, map_id' := Ξ» A, begin funext I, exact ideal.ideal_id A I, end, map_comp' := Ξ» X Y Z f g, begin funext I, exact ideal.ideal_comp X Y Z I f g, end, } -- instance (A : Ring) : has_mem(Ξ©.obj(A)) := ⟨λ x, ⟩ end Omega namespace Omega_2 open Omega variables (A :Ring) def square_ideal_app (A : Ring) : ideal A β†’ ideal A := Ξ» I, begin exact (I * I), end def square_ideal_naturality (A B :Ring) (ψ : A ⟢ B) : Ξ©.map ψ ≫ square_ideal_app B = square_ideal_app A ≫ Ξ©.map ψ := begin rw [category_theory.types_comp,category_theory.types_comp], funext I, rw [ show (Ξ©.map ψ ∘ square_ideal_app A) I = ideal.map ψ (I * I),from rfl,ideal.map_mul ψ I I], exact rfl, end def square_ideal : Ξ© ⟢ Ξ© := { app := Ξ» A : Ring, square_ideal_app A, naturality' := square_ideal_naturality, } end Omega_2 -- namespace ONE -- def ONE_map (A B : Ring) (ψ : A ⟢ B) : (βŠ₯ : ideal A) β†’ (βŠ₯ : ideal B) -- := Ξ» I, begin -- have Hyp : ideal.map ψ (βŠ₯) = βŠ₯ , -- exact ideal.map_bot ψ, -- exact ideal.map ψ I, -- end -- def ONE : Ring β₯€ Set := -- { -- obj := Ξ» A, {I : ideal A | I = βŠ₯ }, -- map := ONE_map, -- -- map_id' := Ξ» A, begin -- -- funext I, -- -- exact ideal.ideal_id A I, -- -- end, -- -- map_comp' := Ξ» X Y Z f g, begin -- -- funext I, -- -- exact ideal.ideal_comp X Y Z I f g, -- -- end, -- } -- end ONE
789989d77414204d40652a036f21b489765eff81
b7f22e51856f4989b970961f794f1c435f9b8f78
/library/init/default.lean
1f7bcbb7d07c2fbf70bce70ce26bffccff445ef9
[ "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
450
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.datatypes init.reserved_notation init.tactic init.logic import init.relation init.wf init.nat init.wf_k init.prod import init.bool init.num init.sigma init.measurable init.setoid init.quot import init.funext init.function init.subtype init.classical init.simplifier
2015af8982ac5c916f3226c31f33ecb94c564eac
c777c32c8e484e195053731103c5e52af26a25d1
/src/topology/algebra/module/multilinear.lean
efa86ed869ffc24e7a71c47c394f74073dd0ebf0
[ "Apache-2.0" ]
permissive
kbuzzard/mathlib
2ff9e85dfe2a46f4b291927f983afec17e946eb8
58537299e922f9c77df76cb613910914a479c1f7
refs/heads/master
1,685,313,702,744
1,683,974,212,000
1,683,974,212,000
128,185,277
1
0
null
1,522,920,600,000
1,522,920,600,000
null
UTF-8
Lean
false
false
22,458
lean
/- Copyright (c) 2020 SΓ©bastien GouΓ«zel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: SΓ©bastien GouΓ«zel -/ import topology.algebra.module.basic import linear_algebra.multilinear.basic /-! # Continuous multilinear maps > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. We define continuous multilinear maps as maps from `Ξ (i : ΞΉ), M₁ i` to `Mβ‚‚` which are multilinear and continuous, by extending the space of multilinear maps with a continuity assumption. Here, `M₁ i` and `Mβ‚‚` are modules over a ring `R`, and `ΞΉ` is an arbitrary type, and all these spaces are also topological spaces. ## Main definitions * `continuous_multilinear_map R M₁ Mβ‚‚` is the space of continuous multilinear maps from `Ξ (i : ΞΉ), M₁ i` to `Mβ‚‚`. We show that it is an `R`-module. ## Implementation notes We mostly follow the API of multilinear maps. ## Notation We introduce the notation `M [Γ—n]β†’L[R] M'` for the space of continuous `n`-multilinear maps from `M^n` to `M'`. This is a particular case of the general notion (where we allow varying dependent types as the arguments of our continuous multilinear maps), but arguably the most important one, especially when defining iterated derivatives. -/ open function fin set open_locale big_operators universes u v w w₁ w₁' wβ‚‚ w₃ wβ‚„ variables {R : Type u} {ΞΉ : Type v} {n : β„•} {M : fin n.succ β†’ Type w} {M₁ : ΞΉ β†’ Type w₁} {M₁' : ΞΉ β†’ Type w₁'} {Mβ‚‚ : Type wβ‚‚} {M₃ : Type w₃} {Mβ‚„ : Type wβ‚„} /-- Continuous multilinear maps over the ring `R`, from `Ξ i, M₁ i` to `Mβ‚‚` where `M₁ i` and `Mβ‚‚` are modules over `R` with a topological structure. In applications, there will be compatibility conditions between the algebraic and the topological structures, but this is not needed for the definition. -/ structure continuous_multilinear_map (R : Type u) {ΞΉ : Type v} (M₁ : ΞΉ β†’ Type w₁) (Mβ‚‚ : Type wβ‚‚) [semiring R] [βˆ€i, add_comm_monoid (M₁ i)] [add_comm_monoid Mβ‚‚] [βˆ€i, module R (M₁ i)] [module R Mβ‚‚] [βˆ€i, topological_space (M₁ i)] [topological_space Mβ‚‚] extends multilinear_map R M₁ Mβ‚‚ := (cont : continuous to_fun) notation M `[Γ—`:25 n `]β†’L[`:25 R `] ` M' := continuous_multilinear_map R (Ξ» (i : fin n), M) M' namespace continuous_multilinear_map section semiring variables [semiring R] [Ξ i, add_comm_monoid (M i)] [Ξ i, add_comm_monoid (M₁ i)] [Ξ i, add_comm_monoid (M₁' i)] [add_comm_monoid Mβ‚‚] [add_comm_monoid M₃] [add_comm_monoid Mβ‚„] [Ξ  i, module R (M i)] [Ξ  i, module R (M₁ i)] [Ξ  i, module R (M₁' i)] [module R Mβ‚‚] [module R M₃] [module R Mβ‚„] [Ξ  i, topological_space (M i)] [Ξ  i, topological_space (M₁ i)] [Ξ  i, topological_space (M₁' i)] [topological_space Mβ‚‚] [topological_space M₃] [topological_space Mβ‚„] (f f' : continuous_multilinear_map R M₁ Mβ‚‚) theorem to_multilinear_map_injective : function.injective (continuous_multilinear_map.to_multilinear_map : continuous_multilinear_map R M₁ Mβ‚‚ β†’ multilinear_map R M₁ Mβ‚‚) | ⟨f, hf⟩ ⟨g, hg⟩ rfl := rfl instance continuous_map_class : continuous_map_class (continuous_multilinear_map R M₁ Mβ‚‚) (Ξ  i, M₁ i) Mβ‚‚ := { coe := Ξ» f, f.to_fun, coe_injective' := Ξ» f g h, to_multilinear_map_injective $ multilinear_map.coe_injective h, map_continuous := continuous_multilinear_map.cont } instance : has_coe_to_fun (continuous_multilinear_map R M₁ Mβ‚‚) (Ξ» _, (Ξ  i, M₁ i) β†’ Mβ‚‚) := ⟨λ f, f⟩ /-- See Note [custom simps projection]. We need to specify this projection explicitly in this case, because it is a composition of multiple projections. -/ def simps.apply (L₁ : continuous_multilinear_map R M₁ Mβ‚‚) (v : Ξ  i, M₁ i) : Mβ‚‚ := L₁ v initialize_simps_projections continuous_multilinear_map (-to_multilinear_map, to_multilinear_map_to_fun β†’ apply) @[continuity] lemma coe_continuous : continuous (f : (Ξ  i, M₁ i) β†’ Mβ‚‚) := f.cont @[simp] lemma coe_coe : (f.to_multilinear_map : (Ξ  i, M₁ i) β†’ Mβ‚‚) = f := rfl @[ext] theorem ext {f f' : continuous_multilinear_map R M₁ Mβ‚‚} (H : βˆ€ x, f x = f' x) : f = f' := fun_like.ext _ _ H theorem ext_iff {f f' : continuous_multilinear_map R M₁ Mβ‚‚} : f = f' ↔ βˆ€ x, f x = f' x := by rw [← to_multilinear_map_injective.eq_iff, multilinear_map.ext_iff]; refl @[simp] lemma map_add [decidable_eq ΞΉ] (m : Ξ i, M₁ i) (i : ΞΉ) (x y : M₁ i) : f (update m i (x + y)) = f (update m i x) + f (update m i y) := f.map_add' m i x y @[simp] lemma map_smul [decidable_eq ΞΉ] (m : Ξ i, M₁ i) (i : ΞΉ) (c : R) (x : M₁ i) : f (update m i (c β€’ x)) = c β€’ f (update m i x) := f.map_smul' m i c x lemma map_coord_zero {m : Ξ i, M₁ i} (i : ΞΉ) (h : m i = 0) : f m = 0 := f.to_multilinear_map.map_coord_zero i h @[simp] lemma map_zero [nonempty ΞΉ] : f 0 = 0 := f.to_multilinear_map.map_zero instance : has_zero (continuous_multilinear_map R M₁ Mβ‚‚) := ⟨{ cont := continuous_const, ..(0 : multilinear_map R M₁ Mβ‚‚) }⟩ instance : inhabited (continuous_multilinear_map R M₁ Mβ‚‚) := ⟨0⟩ @[simp] lemma zero_apply (m : Ξ i, M₁ i) : (0 : continuous_multilinear_map R M₁ Mβ‚‚) m = 0 := rfl @[simp] lemma to_multilinear_map_zero : (0 : continuous_multilinear_map R M₁ Mβ‚‚).to_multilinear_map = 0 := rfl section has_smul variables {R' R'' A : Type*} [monoid R'] [monoid R''] [semiring A] [Ξ  i, module A (M₁ i)] [module A Mβ‚‚] [distrib_mul_action R' Mβ‚‚] [has_continuous_const_smul R' Mβ‚‚] [smul_comm_class A R' Mβ‚‚] [distrib_mul_action R'' Mβ‚‚] [has_continuous_const_smul R'' Mβ‚‚] [smul_comm_class A R'' Mβ‚‚] instance : has_smul R' (continuous_multilinear_map A M₁ Mβ‚‚) := ⟨λ c f, { cont := f.cont.const_smul c, .. c β€’ f.to_multilinear_map }⟩ @[simp] lemma smul_apply (f : continuous_multilinear_map A M₁ Mβ‚‚) (c : R') (m : Ξ i, M₁ i) : (c β€’ f) m = c β€’ f m := rfl @[simp] lemma to_multilinear_map_smul (c : R') (f : continuous_multilinear_map A M₁ Mβ‚‚) : (c β€’ f).to_multilinear_map = c β€’ f.to_multilinear_map := rfl instance [smul_comm_class R' R'' Mβ‚‚] : smul_comm_class R' R'' (continuous_multilinear_map A M₁ Mβ‚‚) := ⟨λ c₁ cβ‚‚ f, ext $ Ξ» x, smul_comm _ _ _⟩ instance [has_smul R' R''] [is_scalar_tower R' R'' Mβ‚‚] : is_scalar_tower R' R'' (continuous_multilinear_map A M₁ Mβ‚‚) := ⟨λ c₁ cβ‚‚ f, ext $ Ξ» x, smul_assoc _ _ _⟩ instance [distrib_mul_action R'ᡐᡒᡖ Mβ‚‚] [is_central_scalar R' Mβ‚‚] : is_central_scalar R' (continuous_multilinear_map A M₁ Mβ‚‚) := ⟨λ c₁ f, ext $ Ξ» x, op_smul_eq_smul _ _⟩ instance : mul_action R' (continuous_multilinear_map A M₁ Mβ‚‚) := function.injective.mul_action to_multilinear_map to_multilinear_map_injective (Ξ» _ _, rfl) end has_smul section has_continuous_add variable [has_continuous_add Mβ‚‚] instance : has_add (continuous_multilinear_map R M₁ Mβ‚‚) := ⟨λ f f', ⟨f.to_multilinear_map + f'.to_multilinear_map, f.cont.add f'.cont⟩⟩ @[simp] lemma add_apply (m : Ξ i, M₁ i) : (f + f') m = f m + f' m := rfl @[simp] lemma to_multilinear_map_add (f g : continuous_multilinear_map R M₁ Mβ‚‚) : (f + g).to_multilinear_map = f.to_multilinear_map + g.to_multilinear_map := rfl instance add_comm_monoid : add_comm_monoid (continuous_multilinear_map R M₁ Mβ‚‚) := to_multilinear_map_injective.add_comm_monoid _ rfl (Ξ» _ _, rfl) (Ξ» _ _, rfl) /-- Evaluation of a `continuous_multilinear_map` at a vector as an `add_monoid_hom`. -/ def apply_add_hom (m : Ξ  i, M₁ i) : continuous_multilinear_map R M₁ Mβ‚‚ β†’+ Mβ‚‚ := ⟨λ f, f m, rfl, Ξ» _ _, rfl⟩ @[simp] lemma sum_apply {Ξ± : Type*} (f : Ξ± β†’ continuous_multilinear_map R M₁ Mβ‚‚) (m : Ξ i, M₁ i) {s : finset Ξ±} : (βˆ‘ a in s, f a) m = βˆ‘ a in s, f a m := (apply_add_hom m).map_sum f s end has_continuous_add /-- If `f` is a continuous multilinear map, then `f.to_continuous_linear_map m i` is the continuous linear map obtained by fixing all coordinates but `i` equal to those of `m`, and varying the `i`-th coordinate. -/ def to_continuous_linear_map [decidable_eq ΞΉ] (m : Ξ i, M₁ i) (i : ΞΉ) : M₁ i β†’L[R] Mβ‚‚ := { cont := f.cont.comp (continuous_const.update i continuous_id), .. f.to_multilinear_map.to_linear_map m i } /-- The cartesian product of two continuous multilinear maps, as a continuous multilinear map. -/ def prod (f : continuous_multilinear_map R M₁ Mβ‚‚) (g : continuous_multilinear_map R M₁ M₃) : continuous_multilinear_map R M₁ (Mβ‚‚ Γ— M₃) := { cont := f.cont.prod_mk g.cont, .. f.to_multilinear_map.prod g.to_multilinear_map } @[simp] lemma prod_apply (f : continuous_multilinear_map R M₁ Mβ‚‚) (g : continuous_multilinear_map R M₁ M₃) (m : Ξ i, M₁ i) : (f.prod g) m = (f m, g m) := rfl /-- Combine a family of continuous multilinear maps with the same domain and codomains `M' i` into a continuous multilinear map taking values in the space of functions `Ξ  i, M' i`. -/ def pi {ΞΉ' : Type*} {M' : ΞΉ' β†’ Type*} [Ξ  i, add_comm_monoid (M' i)] [Ξ  i, topological_space (M' i)] [Ξ  i, module R (M' i)] (f : Ξ  i, continuous_multilinear_map R M₁ (M' i)) : continuous_multilinear_map R M₁ (Ξ  i, M' i) := { cont := continuous_pi $ Ξ» i, (f i).coe_continuous, to_multilinear_map := multilinear_map.pi (Ξ» i, (f i).to_multilinear_map) } @[simp] lemma coe_pi {ΞΉ' : Type*} {M' : ΞΉ' β†’ Type*} [Ξ  i, add_comm_monoid (M' i)] [Ξ  i, topological_space (M' i)] [Ξ  i, module R (M' i)] (f : Ξ  i, continuous_multilinear_map R M₁ (M' i)) : ⇑(pi f) = Ξ» m j, f j m := rfl lemma pi_apply {ΞΉ' : Type*} {M' : ΞΉ' β†’ Type*} [Ξ  i, add_comm_monoid (M' i)] [Ξ  i, topological_space (M' i)] [Ξ  i, module R (M' i)] (f : Ξ  i, continuous_multilinear_map R M₁ (M' i)) (m : Ξ  i, M₁ i) (j : ΞΉ') : pi f m j = f j m := rfl section variables (R Mβ‚‚) /-- The evaluation map from `ΞΉ β†’ Mβ‚‚` to `Mβ‚‚` is multilinear at a given `i` when `ΞΉ` is subsingleton. -/ @[simps to_multilinear_map apply] def of_subsingleton [subsingleton ΞΉ] (i' : ΞΉ) : continuous_multilinear_map R (Ξ» _ : ΞΉ, Mβ‚‚) Mβ‚‚ := { to_multilinear_map := multilinear_map.of_subsingleton R _ i', cont := continuous_apply _ } variables (M₁) {Mβ‚‚} /-- The constant map is multilinear when `ΞΉ` is empty. -/ @[simps to_multilinear_map apply] def const_of_is_empty [is_empty ΞΉ] (m : Mβ‚‚) : continuous_multilinear_map R M₁ Mβ‚‚ := { to_multilinear_map := multilinear_map.const_of_is_empty R _ m, cont := continuous_const } end /-- If `g` is continuous multilinear and `f` is a collection of continuous linear maps, then `g (f₁ m₁, ..., fβ‚™ mβ‚™)` is again a continuous multilinear map, that we call `g.comp_continuous_linear_map f`. -/ def comp_continuous_linear_map (g : continuous_multilinear_map R M₁' Mβ‚„) (f : Ξ  i : ΞΉ, M₁ i β†’L[R] M₁' i) : continuous_multilinear_map R M₁ Mβ‚„ := { cont := g.cont.comp $ continuous_pi $ Ξ»j, (f j).cont.comp $ continuous_apply _, .. g.to_multilinear_map.comp_linear_map (Ξ» i, (f i).to_linear_map) } @[simp] lemma comp_continuous_linear_map_apply (g : continuous_multilinear_map R M₁' Mβ‚„) (f : Ξ  i : ΞΉ, M₁ i β†’L[R] M₁' i) (m : Ξ  i, M₁ i) : g.comp_continuous_linear_map f m = g (Ξ» i, f i $ m i) := rfl /-- Composing a continuous multilinear map with a continuous linear map gives again a continuous multilinear map. -/ def _root_.continuous_linear_map.comp_continuous_multilinear_map (g : Mβ‚‚ β†’L[R] M₃) (f : continuous_multilinear_map R M₁ Mβ‚‚) : continuous_multilinear_map R M₁ M₃ := { cont := g.cont.comp f.cont, .. g.to_linear_map.comp_multilinear_map f.to_multilinear_map } @[simp] lemma _root_.continuous_linear_map.comp_continuous_multilinear_map_coe (g : Mβ‚‚ β†’L[R] M₃) (f : continuous_multilinear_map R M₁ Mβ‚‚) : ((g.comp_continuous_multilinear_map f) : (Ξ i, M₁ i) β†’ M₃) = (g : Mβ‚‚ β†’ M₃) ∘ (f : (Ξ i, M₁ i) β†’ Mβ‚‚) := by { ext m, refl } /-- `continuous_multilinear_map.pi` as an `equiv`. -/ @[simps] def pi_equiv {ΞΉ' : Type*} {M' : ΞΉ' β†’ Type*} [Ξ  i, add_comm_monoid (M' i)] [Ξ  i, topological_space (M' i)] [Ξ  i, module R (M' i)] : (Ξ  i, continuous_multilinear_map R M₁ (M' i)) ≃ continuous_multilinear_map R M₁ (Ξ  i, M' i) := { to_fun := continuous_multilinear_map.pi, inv_fun := Ξ» f i, (continuous_linear_map.proj i : _ β†’L[R] M' i).comp_continuous_multilinear_map f, left_inv := Ξ» f, by { ext, refl }, right_inv := Ξ» f, by { ext, refl } } /-- In the specific case of continuous multilinear maps on spaces indexed by `fin (n+1)`, where one can build an element of `Ξ (i : fin (n+1)), M i` using `cons`, one can express directly the additivity of a multilinear map along the first variable. -/ lemma cons_add (f : continuous_multilinear_map R M Mβ‚‚) (m : Ξ (i : fin n), M i.succ) (x y : M 0) : f (cons (x+y) m) = f (cons x m) + f (cons y m) := f.to_multilinear_map.cons_add m x y /-- In the specific case of continuous multilinear maps on spaces indexed by `fin (n+1)`, where one can build an element of `Ξ (i : fin (n+1)), M i` using `cons`, one can express directly the multiplicativity of a multilinear map along the first variable. -/ lemma cons_smul (f : continuous_multilinear_map R M Mβ‚‚) (m : Ξ (i : fin n), M i.succ) (c : R) (x : M 0) : f (cons (c β€’ x) m) = c β€’ f (cons x m) := f.to_multilinear_map.cons_smul m c x lemma map_piecewise_add [decidable_eq ΞΉ] (m m' : Ξ i, M₁ i) (t : finset ΞΉ) : f (t.piecewise (m + m') m') = βˆ‘ s in t.powerset, f (s.piecewise m m') := f.to_multilinear_map.map_piecewise_add _ _ _ /-- Additivity of a continuous multilinear map along all coordinates at the same time, writing `f (m + m')` as the sum of `f (s.piecewise m m')` over all sets `s`. -/ lemma map_add_univ [decidable_eq ΞΉ] [fintype ΞΉ] (m m' : Ξ i, M₁ i) : f (m + m') = βˆ‘ s : finset ΞΉ, f (s.piecewise m m') := f.to_multilinear_map.map_add_univ _ _ section apply_sum open fintype finset variables {Ξ± : ΞΉ β†’ Type*} [fintype ΞΉ] (g : Ξ  i, Ξ± i β†’ M₁ i) (A : Ξ  i, finset (Ξ± i)) /-- If `f` is continuous multilinear, then `f (Ξ£_{j₁ ∈ A₁} g₁ j₁, ..., Ξ£_{jβ‚™ ∈ Aβ‚™} gβ‚™ jβ‚™)` is the sum of `f (g₁ (r 1), ..., gβ‚™ (r n))` where `r` ranges over all functions with `r 1 ∈ A₁`, ..., `r n ∈ Aβ‚™`. This follows from multilinearity by expanding successively with respect to each coordinate. -/ lemma map_sum_finset [decidable_eq ΞΉ] : f (Ξ» i, βˆ‘ j in A i, g i j) = βˆ‘ r in pi_finset A, f (Ξ» i, g i (r i)) := f.to_multilinear_map.map_sum_finset _ _ /-- If `f` is continuous multilinear, then `f (Ξ£_{j₁} g₁ j₁, ..., Ξ£_{jβ‚™} gβ‚™ jβ‚™)` is the sum of `f (g₁ (r 1), ..., gβ‚™ (r n))` where `r` ranges over all functions `r`. This follows from multilinearity by expanding successively with respect to each coordinate. -/ lemma map_sum [decidable_eq ΞΉ] [βˆ€ i, fintype (Ξ± i)] : f (Ξ» i, βˆ‘ j, g i j) = βˆ‘ r : Ξ  i, Ξ± i, f (Ξ» i, g i (r i)) := f.to_multilinear_map.map_sum _ end apply_sum section restrict_scalar variables (R) {A : Type*} [semiring A] [has_smul R A] [Ξ  (i : ΞΉ), module A (M₁ i)] [module A Mβ‚‚] [βˆ€ i, is_scalar_tower R A (M₁ i)] [is_scalar_tower R A Mβ‚‚] /-- Reinterpret an `A`-multilinear map as an `R`-multilinear map, if `A` is an algebra over `R` and their actions on all involved modules agree with the action of `R` on `A`. -/ def restrict_scalars (f : continuous_multilinear_map A M₁ Mβ‚‚) : continuous_multilinear_map R M₁ Mβ‚‚ := { to_multilinear_map := f.to_multilinear_map.restrict_scalars R, cont := f.cont } @[simp] lemma coe_restrict_scalars (f : continuous_multilinear_map A M₁ Mβ‚‚) : ⇑(f.restrict_scalars R) = f := rfl end restrict_scalar end semiring section ring variables [ring R] [βˆ€i, add_comm_group (M₁ i)] [add_comm_group Mβ‚‚] [βˆ€i, module R (M₁ i)] [module R Mβ‚‚] [βˆ€i, topological_space (M₁ i)] [topological_space Mβ‚‚] (f f' : continuous_multilinear_map R M₁ Mβ‚‚) @[simp] lemma map_sub [decidable_eq ΞΉ] (m : Ξ i, M₁ i) (i : ΞΉ) (x y : M₁ i) : f (update m i (x - y)) = f (update m i x) - f (update m i y) := f.to_multilinear_map.map_sub _ _ _ _ section topological_add_group variable [topological_add_group Mβ‚‚] instance : has_neg (continuous_multilinear_map R M₁ Mβ‚‚) := ⟨λ f, {cont := f.cont.neg, ..(-f.to_multilinear_map)}⟩ @[simp] lemma neg_apply (m : Ξ i, M₁ i) : (-f) m = - (f m) := rfl instance : has_sub (continuous_multilinear_map R M₁ Mβ‚‚) := ⟨λ f g, { cont := f.cont.sub g.cont, .. (f.to_multilinear_map - g.to_multilinear_map) }⟩ @[simp] lemma sub_apply (m : Ξ i, M₁ i) : (f - f') m = f m - f' m := rfl instance : add_comm_group (continuous_multilinear_map R M₁ Mβ‚‚) := to_multilinear_map_injective.add_comm_group _ rfl (Ξ» _ _, rfl) (Ξ» _, rfl) (Ξ» _ _, rfl) (Ξ» _ _, rfl) (Ξ» _ _, rfl) end topological_add_group end ring section comm_semiring variables [comm_semiring R] [βˆ€i, add_comm_monoid (M₁ i)] [add_comm_monoid Mβ‚‚] [βˆ€i, module R (M₁ i)] [module R Mβ‚‚] [βˆ€i, topological_space (M₁ i)] [topological_space Mβ‚‚] (f : continuous_multilinear_map R M₁ Mβ‚‚) lemma map_piecewise_smul [decidable_eq ΞΉ] (c : ΞΉ β†’ R) (m : Ξ i, M₁ i) (s : finset ΞΉ) : f (s.piecewise (Ξ» i, c i β€’ m i) m) = (∏ i in s, c i) β€’ f m := f.to_multilinear_map.map_piecewise_smul _ _ _ /-- Multiplicativity of a continuous multilinear map along all coordinates at the same time, writing `f (Ξ» i, c i β€’ m i)` as `(∏ i, c i) β€’ f m`. -/ lemma map_smul_univ [fintype ΞΉ] (c : ΞΉ β†’ R) (m : Ξ i, M₁ i) : f (Ξ» i, c i β€’ m i) = (∏ i, c i) β€’ f m := f.to_multilinear_map.map_smul_univ _ _ end comm_semiring section distrib_mul_action variables {R' R'' A : Type*} [monoid R'] [monoid R''] [semiring A] [Ξ  i, add_comm_monoid (M₁ i)] [add_comm_monoid Mβ‚‚] [Ξ  i, topological_space (M₁ i)] [topological_space Mβ‚‚] [Ξ  i, module A (M₁ i)] [module A Mβ‚‚] [distrib_mul_action R' Mβ‚‚] [has_continuous_const_smul R' Mβ‚‚] [smul_comm_class A R' Mβ‚‚] [distrib_mul_action R'' Mβ‚‚] [has_continuous_const_smul R'' Mβ‚‚] [smul_comm_class A R'' Mβ‚‚] instance [has_continuous_add Mβ‚‚] : distrib_mul_action R' (continuous_multilinear_map A M₁ Mβ‚‚) := function.injective.distrib_mul_action ⟨to_multilinear_map, to_multilinear_map_zero, to_multilinear_map_add⟩ to_multilinear_map_injective (Ξ» _ _, rfl) end distrib_mul_action section module variables {R' A : Type*} [semiring R'] [semiring A] [Ξ  i, add_comm_monoid (M₁ i)] [add_comm_monoid Mβ‚‚] [Ξ  i, topological_space (M₁ i)] [topological_space Mβ‚‚] [has_continuous_add Mβ‚‚] [Ξ  i, module A (M₁ i)] [module A Mβ‚‚] [module R' Mβ‚‚] [has_continuous_const_smul R' Mβ‚‚] [smul_comm_class A R' Mβ‚‚] /-- The space of continuous multilinear maps over an algebra over `R` is a module over `R`, for the pointwise addition and scalar multiplication. -/ instance : module R' (continuous_multilinear_map A M₁ Mβ‚‚) := function.injective.module _ ⟨to_multilinear_map, to_multilinear_map_zero, to_multilinear_map_add⟩ to_multilinear_map_injective (Ξ» _ _, rfl) /-- Linear map version of the map `to_multilinear_map` associating to a continuous multilinear map the corresponding multilinear map. -/ @[simps] def to_multilinear_map_linear : continuous_multilinear_map A M₁ Mβ‚‚ β†’β‚—[R'] multilinear_map A M₁ Mβ‚‚ := { to_fun := to_multilinear_map, map_add' := to_multilinear_map_add, map_smul' := to_multilinear_map_smul } /-- `continuous_multilinear_map.pi` as a `linear_equiv`. -/ @[simps {simp_rhs := tt}] def pi_linear_equiv {ΞΉ' : Type*} {M' : ΞΉ' β†’ Type*} [Ξ  i, add_comm_monoid (M' i)] [Ξ  i, topological_space (M' i)] [βˆ€ i, has_continuous_add (M' i)] [Ξ  i, module R' (M' i)] [Ξ  i, module A (M' i)] [βˆ€ i, smul_comm_class A R' (M' i)] [Ξ  i, has_continuous_const_smul R' (M' i)] : (Ξ  i, continuous_multilinear_map A M₁ (M' i)) ≃ₗ[R'] continuous_multilinear_map A M₁ (Ξ  i, M' i) := { map_add' := Ξ» x y, rfl, map_smul' := Ξ» c x, rfl, .. pi_equiv } end module section comm_algebra variables (R ΞΉ) (A : Type*) [fintype ΞΉ] [comm_semiring R] [comm_semiring A] [algebra R A] [topological_space A] [has_continuous_mul A] /-- The continuous multilinear map on `A^ΞΉ`, where `A` is a normed commutative algebra over `π•œ`, associating to `m` the product of all the `m i`. See also `continuous_multilinear_map.mk_pi_algebra_fin`. -/ protected def mk_pi_algebra : continuous_multilinear_map R (Ξ» i : ΞΉ, A) A := { cont := continuous_finset_prod _ $ Ξ» i hi, continuous_apply _, to_multilinear_map := multilinear_map.mk_pi_algebra R ΞΉ A} @[simp] lemma mk_pi_algebra_apply (m : ΞΉ β†’ A) : continuous_multilinear_map.mk_pi_algebra R ΞΉ A m = ∏ i, m i := rfl end comm_algebra section algebra variables (R n) (A : Type*) [comm_semiring R] [semiring A] [algebra R A] [topological_space A] [has_continuous_mul A] /-- The continuous multilinear map on `A^n`, where `A` is a normed algebra over `π•œ`, associating to `m` the product of all the `m i`. See also: `continuous_multilinear_map.mk_pi_algebra`. -/ protected def mk_pi_algebra_fin : A [Γ—n]β†’L[R] A := { cont := begin change continuous (Ξ» m, (list.of_fn m).prod), simp_rw list.of_fn_eq_map, exact continuous_list_prod _ (Ξ» i hi, continuous_apply _), end, to_multilinear_map := multilinear_map.mk_pi_algebra_fin R n A} variables {R n A} @[simp] lemma mk_pi_algebra_fin_apply (m : fin n β†’ A) : continuous_multilinear_map.mk_pi_algebra_fin R n A m = (list.of_fn m).prod := rfl end algebra section smul_right variables [comm_semiring R] [Ξ  i, add_comm_monoid (M₁ i)] [add_comm_monoid Mβ‚‚] [Ξ  i, module R (M₁ i)] [module R Mβ‚‚] [topological_space R] [Ξ  i, topological_space (M₁ i)] [topological_space Mβ‚‚] [has_continuous_smul R Mβ‚‚] (f : continuous_multilinear_map R M₁ R) (z : Mβ‚‚) /-- Given a continuous `R`-multilinear map `f` taking values in `R`, `f.smul_right z` is the continuous multilinear map sending `m` to `f m β€’ z`. -/ @[simps to_multilinear_map apply] def smul_right : continuous_multilinear_map R M₁ Mβ‚‚ := { to_multilinear_map := f.to_multilinear_map.smul_right z, cont := f.cont.smul continuous_const } end smul_right end continuous_multilinear_map
4fd2d5c8cc559a66975f7267e5ba7151e5bdba9b
959e3de53426c90b7318223ba1036f0845387f67
/library/init/default.lean
c42e3d7b92ca1d587156d7cde86260aa9a2002d8
[ "Apache-2.0" ]
permissive
huma23/lean
2fdb93e43ac8708473f341a64d434802f1a42cdd
75602eca5e23d2c426ef9103fb6eeb3130e36f1e
refs/heads/master
1,586,375,625,362
1,543,501,870,000
1,543,501,870,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
555
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.core init.logic init.category init.data.basic import init.propext init.cc_lemmas init.funext init.category.combinators init.function init.classical import init.util init.coe init.wf init.meta init.meta.well_founded_tactics init.algebra init.data @[user_attribute] meta def debugger.attr : user_attribute := { name := `breakpoint, descr := "breakpoint for debugger" }
a86e0da5c75a5bd8fadf4495ae3c74d1ee14f34e
4727251e0cd73359b15b664c3170e5d754078599
/src/analysis/normed_space/dual.lean
d04111b91b3e1788cc9f8e7c7bac7f1e2fed206a
[ "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
10,130
lean
/- Copyright (c) 2020 Heather Macbeth. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Heather Macbeth -/ import analysis.normed_space.hahn_banach import analysis.normed_space.is_R_or_C import analysis.locally_convex.polar /-! # The topological dual of a normed space In this file we define the topological dual `normed_space.dual` of a normed space, and the continuous linear map `normed_space.inclusion_in_double_dual` from a normed space into its double dual. For base field `π•œ = ℝ` or `π•œ = β„‚`, this map is actually an isometric embedding; we provide a version `normed_space.inclusion_in_double_dual_li` of the map which is of type a bundled linear isometric embedding, `E β†’β‚—α΅’[π•œ] (dual π•œ (dual π•œ E))`. Since a lot of elementary properties don't require `eq_of_dist_eq_zero` we start setting up the theory for `semi_normed_group` and we specialize to `normed_group` when needed. ## Main definitions * `inclusion_in_double_dual` and `inclusion_in_double_dual_li` are the inclusion of a normed space in its double dual, considered as a bounded linear map and as a linear isometry, respectively. * `polar π•œ s` is the subset of `dual π•œ E` consisting of those functionals `x'` for which `βˆ₯x' zβˆ₯ ≀ 1` for every `z ∈ s`. ## Tags dual -/ noncomputable theory open_locale classical topological_space universes u v namespace normed_space section general variables (π•œ : Type*) [nondiscrete_normed_field π•œ] variables (E : Type*) [semi_normed_group E] [normed_space π•œ E] variables (F : Type*) [normed_group F] [normed_space π•œ F] /-- The topological dual of a seminormed space `E`. -/ @[derive [inhabited, semi_normed_group, normed_space π•œ]] def dual := E β†’L[π•œ] π•œ instance : add_monoid_hom_class (dual π•œ E) E π•œ := continuous_linear_map.add_monoid_hom_class instance : has_coe_to_fun (dual π•œ E) (Ξ» _, E β†’ π•œ) := continuous_linear_map.to_fun instance : normed_group (dual π•œ F) := continuous_linear_map.to_normed_group instance [finite_dimensional π•œ E] : finite_dimensional π•œ (dual π•œ E) := continuous_linear_map.finite_dimensional /-- The inclusion of a normed space in its double (topological) dual, considered as a bounded linear map. -/ def inclusion_in_double_dual : E β†’L[π•œ] (dual π•œ (dual π•œ E)) := continuous_linear_map.apply π•œ π•œ @[simp] lemma dual_def (x : E) (f : dual π•œ E) : inclusion_in_double_dual π•œ E x f = f x := rfl lemma inclusion_in_double_dual_norm_eq : βˆ₯inclusion_in_double_dual π•œ Eβˆ₯ = βˆ₯(continuous_linear_map.id π•œ (dual π•œ E))βˆ₯ := continuous_linear_map.op_norm_flip _ lemma inclusion_in_double_dual_norm_le : βˆ₯inclusion_in_double_dual π•œ Eβˆ₯ ≀ 1 := by { rw inclusion_in_double_dual_norm_eq, exact continuous_linear_map.norm_id_le } lemma double_dual_bound (x : E) : βˆ₯(inclusion_in_double_dual π•œ E) xβˆ₯ ≀ βˆ₯xβˆ₯ := by simpa using continuous_linear_map.le_of_op_norm_le _ (inclusion_in_double_dual_norm_le π•œ E) x /-- The dual pairing as a bilinear form. -/ def dual_pairing : (dual π•œ E) β†’β‚—[π•œ] E β†’β‚—[π•œ] π•œ := continuous_linear_map.coe_lm π•œ @[simp] lemma dual_pairing_apply {v : dual π•œ E} {x : E} : dual_pairing π•œ E v x = v x := rfl lemma dual_pairing_separating_left : (dual_pairing π•œ E).separating_left := begin rw [linear_map.separating_left_iff_ker_eq_bot, linear_map.ker_eq_bot], exact continuous_linear_map.coe_injective, end end general section bidual_isometry variables (π•œ : Type v) [is_R_or_C π•œ] {E : Type u} [normed_group E] [normed_space π•œ E] /-- If one controls the norm of every `f x`, then one controls the norm of `x`. Compare `continuous_linear_map.op_norm_le_bound`. -/ lemma norm_le_dual_bound (x : E) {M : ℝ} (hMp: 0 ≀ M) (hM : βˆ€ (f : dual π•œ E), βˆ₯f xβˆ₯ ≀ M * βˆ₯fβˆ₯) : βˆ₯xβˆ₯ ≀ M := begin classical, by_cases h : x = 0, { simp only [h, hMp, norm_zero] }, { obtain ⟨f, hf₁, hfx⟩ : βˆƒ f : E β†’L[π•œ] π•œ, βˆ₯fβˆ₯ = 1 ∧ f x = βˆ₯xβˆ₯ := exists_dual_vector π•œ x h, calc βˆ₯xβˆ₯ = βˆ₯(βˆ₯xβˆ₯ : π•œ)βˆ₯ : is_R_or_C.norm_coe_norm.symm ... = βˆ₯f xβˆ₯ : by rw hfx ... ≀ M * βˆ₯fβˆ₯ : hM f ... = M : by rw [hf₁, mul_one] } end lemma eq_zero_of_forall_dual_eq_zero {x : E} (h : βˆ€ f : dual π•œ E, f x = (0 : π•œ)) : x = 0 := norm_le_zero_iff.mp (norm_le_dual_bound π•œ x le_rfl (Ξ» f, by simp [h f])) lemma eq_zero_iff_forall_dual_eq_zero (x : E) : x = 0 ↔ βˆ€ g : dual π•œ E, g x = 0 := ⟨λ hx, by simp [hx], Ξ» h, eq_zero_of_forall_dual_eq_zero π•œ h⟩ lemma eq_iff_forall_dual_eq {x y : E} : x = y ↔ βˆ€ g : dual π•œ E, g x = g y := begin rw [← sub_eq_zero, eq_zero_iff_forall_dual_eq_zero π•œ (x - y)], simp [sub_eq_zero], end /-- The inclusion of a normed space in its double dual is an isometry onto its image.-/ def inclusion_in_double_dual_li : E β†’β‚—α΅’[π•œ] (dual π•œ (dual π•œ E)) := { norm_map' := begin intros x, apply le_antisymm, { exact double_dual_bound π•œ E x }, rw continuous_linear_map.norm_def, refine le_cInf continuous_linear_map.bounds_nonempty _, rintros c ⟨hc1, hc2⟩, exact norm_le_dual_bound π•œ x hc1 hc2 end, .. inclusion_in_double_dual π•œ E } end bidual_isometry section polar_sets open metric set normed_space /-- Given a subset `s` in a normed space `E` (over a field `π•œ`), the polar `polar π•œ s` is the subset of `dual π•œ E` consisting of those functionals which evaluate to something of norm at most one at all points `z ∈ s`. -/ def polar (π•œ : Type*) [nondiscrete_normed_field π•œ] {E : Type*} [semi_normed_group E] [normed_space π•œ E] : set E β†’ set (dual π•œ E) := (dual_pairing π•œ E).flip.polar variables (π•œ : Type*) [nondiscrete_normed_field π•œ] variables {E : Type*} [semi_normed_group E] [normed_space π•œ E] lemma mem_polar_iff {x' : dual π•œ E} (s : set E) : x' ∈ polar π•œ s ↔ βˆ€ z ∈ s, βˆ₯x' zβˆ₯ ≀ 1 := iff.rfl @[simp] lemma polar_univ : polar π•œ (univ : set E) = {(0 : dual π•œ E)} := (dual_pairing π•œ E).flip.polar_univ (linear_map.flip_separating_right.mpr (dual_pairing_separating_left π•œ E)) lemma is_closed_polar (s : set E) : is_closed (polar π•œ s) := begin dunfold normed_space.polar, simp only [linear_map.polar_eq_Inter, linear_map.flip_apply], refine is_closed_bInter (Ξ» z hz, _), exact is_closed_Iic.preimage (continuous_linear_map.apply π•œ π•œ z).continuous.norm end @[simp] lemma polar_closure (s : set E) : polar π•œ (closure s) = polar π•œ s := ((dual_pairing π•œ E).flip.polar_antitone subset_closure).antisymm $ (dual_pairing π•œ E).flip.polar_gc.l_le $ closure_minimal ((dual_pairing π•œ E).flip.polar_gc.le_u_l s) $ (is_closed_polar _ _).preimage (inclusion_in_double_dual π•œ E).continuous variables {π•œ} /-- If `x'` is a dual element such that the norms `βˆ₯x' zβˆ₯` are bounded for `z ∈ s`, then a small scalar multiple of `x'` is in `polar π•œ s`. -/ lemma smul_mem_polar {s : set E} {x' : dual π•œ E} {c : π•œ} (hc : βˆ€ z, z ∈ s β†’ βˆ₯ x' z βˆ₯ ≀ βˆ₯cβˆ₯) : c⁻¹ β€’ x' ∈ polar π•œ s := begin by_cases c_zero : c = 0, { simp only [c_zero, inv_zero, zero_smul], exact (dual_pairing π•œ E).flip.zero_mem_polar _ }, have eq : βˆ€ z, βˆ₯ c⁻¹ β€’ (x' z) βˆ₯ = βˆ₯ c⁻¹ βˆ₯ * βˆ₯ x' z βˆ₯ := Ξ» z, norm_smul c⁻¹ _, have le : βˆ€ z, z ∈ s β†’ βˆ₯ c⁻¹ β€’ (x' z) βˆ₯ ≀ βˆ₯ c⁻¹ βˆ₯ * βˆ₯ c βˆ₯, { intros z hzs, rw eq z, apply mul_le_mul (le_of_eq rfl) (hc z hzs) (norm_nonneg _) (norm_nonneg _), }, have cancel : βˆ₯ c⁻¹ βˆ₯ * βˆ₯ c βˆ₯ = 1, by simp only [c_zero, norm_eq_zero, ne.def, not_false_iff, inv_mul_cancel, norm_inv], rwa cancel at le, end lemma polar_ball_subset_closed_ball_div {c : π•œ} (hc : 1 < βˆ₯cβˆ₯) {r : ℝ} (hr : 0 < r) : polar π•œ (ball (0 : E) r) βŠ† closed_ball (0 : dual π•œ E) (βˆ₯cβˆ₯ / r) := begin intros x' hx', rw mem_polar_iff at hx', simp only [polar, mem_set_of_eq, mem_closed_ball_zero_iff, mem_ball_zero_iff] at *, have hcr : 0 < βˆ₯cβˆ₯ / r, from div_pos (zero_lt_one.trans hc) hr, refine continuous_linear_map.op_norm_le_of_shell hr hcr.le hc (Ξ» x h₁ hβ‚‚, _), calc βˆ₯x' xβˆ₯ ≀ 1 : hx' _ hβ‚‚ ... ≀ (βˆ₯cβˆ₯ / r) * βˆ₯xβˆ₯ : (inv_pos_le_iff_one_le_mul' hcr).1 (by rwa inv_div) end variables (π•œ) lemma closed_ball_inv_subset_polar_closed_ball {r : ℝ} : closed_ball (0 : dual π•œ E) r⁻¹ βŠ† polar π•œ (closed_ball (0 : E) r) := Ξ» x' hx' x hx, calc βˆ₯x' xβˆ₯ ≀ βˆ₯x'βˆ₯ * βˆ₯xβˆ₯ : x'.le_op_norm x ... ≀ r⁻¹ * r : mul_le_mul (mem_closed_ball_zero_iff.1 hx') (mem_closed_ball_zero_iff.1 hx) (norm_nonneg _) (dist_nonneg.trans hx') ... = r / r : inv_mul_eq_div _ _ ... ≀ 1 : div_self_le_one r /-- The `polar` of closed ball in a normed space `E` is the closed ball of the dual with inverse radius. -/ lemma polar_closed_ball {π•œ : Type*} [is_R_or_C π•œ] {E : Type*} [normed_group E] [normed_space π•œ E] {r : ℝ} (hr : 0 < r) : polar π•œ (closed_ball (0 : E) r) = closed_ball (0 : dual π•œ E) r⁻¹ := begin refine subset.antisymm _ (closed_ball_inv_subset_polar_closed_ball _), intros x' h, simp only [mem_closed_ball_zero_iff], refine continuous_linear_map.op_norm_le_of_ball hr (inv_nonneg.mpr hr.le) (Ξ» z hz, _), simpa only [one_div] using linear_map.bound_of_ball_bound' hr 1 x'.to_linear_map h z end /-- Given a neighborhood `s` of the origin in a normed space `E`, the dual norms of all elements of the polar `polar π•œ s` are bounded by a constant. -/ lemma bounded_polar_of_mem_nhds_zero {s : set E} (s_nhd : s ∈ 𝓝 (0 : E)) : bounded (polar π•œ s) := begin obtain ⟨a, ha⟩ : βˆƒ a : π•œ, 1 < βˆ₯aβˆ₯ := normed_field.exists_one_lt_norm π•œ, obtain ⟨r, r_pos, r_ball⟩ : βˆƒ (r : ℝ) (hr : 0 < r), ball 0 r βŠ† s := metric.mem_nhds_iff.1 s_nhd, exact bounded_closed_ball.mono (((dual_pairing π•œ E).flip.polar_antitone r_ball).trans $ polar_ball_subset_closed_ball_div ha r_pos) end end polar_sets end normed_space
b7402549876e5e37ee5905ac2d1cbaa25f264c61
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/measure_theory/integral/integral_eq_improper.lean
c0814a8b2b8301b83676b59f59ea275f6fa6ec70
[ "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
36,196
lean
/- Copyright (c) 2021 Anatole Dedecker. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anatole Dedecker, Bhavik Mehta -/ import measure_theory.integral.interval_integral import order.filter.at_top_bot import measure_theory.function.jacobian /-! # Links between an integral and its "improper" version In its current state, mathlib only knows how to talk about definite ("proper") integrals, in the sense that it treats integrals over `[x, +∞)` the same as it treats integrals over `[y, z]`. For example, the integral over `[1, +∞)` is **not** defined to be the limit of the integral over `[1, x]` as `x` tends to `+∞`, which is known as an **improper integral**. Indeed, the "proper" definition is stronger than the "improper" one. The usual counterexample is `x ↦ sin(x)/x`, which has an improper integral over `[1, +∞)` but no definite integral. Although definite integrals have better properties, they are hardly usable when it comes to computing integrals on unbounded sets, which is much easier using limits. Thus, in this file, we prove various ways of studying the proper integral by studying the improper one. ## Definitions The main definition of this file is `measure_theory.ae_cover`. It is a rather technical definition whose sole purpose is generalizing and factoring proofs. Given an index type `ΞΉ`, a countably generated filter `l` over `ΞΉ`, and an `ΞΉ`-indexed family `Ο†` of subsets of a measurable space `Ξ±` equipped with a measure `ΞΌ`, one should think of a hypothesis `hΟ† : ae_cover ΞΌ l Ο†` as a sufficient condition for being able to interpret `∫ x, f x βˆ‚ΞΌ` (if it exists) as the limit of `∫ x in Ο† i, f x βˆ‚ΞΌ` as `i` tends to `l`. When using this definition with a measure restricted to a set `s`, which happens fairly often, one should not try too hard to use a `ae_cover` of subsets of `s`, as it often makes proofs more complicated than necessary. See for example the proof of `measure_theory.integrable_on_Iic_of_interval_integral_norm_tendsto` where we use `(Ξ» x, Ioi x)` as an `ae_cover` w.r.t. `ΞΌ.restrict (Iic b)`, instead of using `(Ξ» x, Ioc x b)`. ## Main statements - `measure_theory.ae_cover.lintegral_tendsto_of_countably_generated` : if `Ο†` is a `ae_cover ΞΌ l`, where `l` is a countably generated filter, and if `f` is a measurable `ennreal`-valued function, then `∫⁻ x in Ο† n, f x βˆ‚ΞΌ` tends to `∫⁻ x, f x βˆ‚ΞΌ` as `n` tends to `l` - `measure_theory.ae_cover.integrable_of_integral_norm_tendsto` : if `Ο†` is a `ae_cover ΞΌ l`, where `l` is a countably generated filter, if `f` is measurable and integrable on each `Ο† n`, and if `∫ x in Ο† n, β€–f xβ€– βˆ‚ΞΌ` tends to some `I : ℝ` as n tends to `l`, then `f` is integrable - `measure_theory.ae_cover.integral_tendsto_of_countably_generated` : if `Ο†` is a `ae_cover ΞΌ l`, where `l` is a countably generated filter, and if `f` is measurable and integrable (globally), then `∫ x in Ο† n, f x βˆ‚ΞΌ` tends to `∫ x, f x βˆ‚ΞΌ` as `n` tends to `+∞`. We then specialize these lemmas to various use cases involving intervals, which are frequent in analysis. -/ open measure_theory filter set topological_space open_locale ennreal nnreal topological_space namespace measure_theory section ae_cover variables {Ξ± ΞΉ : Type*} [measurable_space Ξ±] (ΞΌ : measure Ξ±) (l : filter ΞΉ) /-- A sequence `Ο†` of subsets of `Ξ±` is a `ae_cover` w.r.t. a measure `ΞΌ` and a filter `l` if almost every point (w.r.t. `ΞΌ`) of `Ξ±` eventually belongs to `Ο† n` (w.r.t. `l`), and if each `Ο† n` is measurable. This definition is a technical way to avoid duplicating a lot of proofs. It should be thought of as a sufficient condition for being able to interpret `∫ x, f x βˆ‚ΞΌ` (if it exists) as the limit of `∫ x in Ο† n, f x βˆ‚ΞΌ` as `n` tends to `l`. See for example `measure_theory.ae_cover.lintegral_tendsto_of_countably_generated`, `measure_theory.ae_cover.integrable_of_integral_norm_tendsto` and `measure_theory.ae_cover.integral_tendsto_of_countably_generated`. -/ structure ae_cover (Ο† : ΞΉ β†’ set Ξ±) : Prop := (ae_eventually_mem : βˆ€α΅ x βˆ‚ΞΌ, βˆ€αΆ  i in l, x ∈ Ο† i) (measurable : βˆ€ i, measurable_set $ Ο† i) variables {ΞΌ} {l} section preorder_Ξ± variables [preorder Ξ±] [topological_space Ξ±] [order_closed_topology Ξ±] [opens_measurable_space Ξ±] {a b : ΞΉ β†’ Ξ±} (ha : tendsto a l at_bot) (hb : tendsto b l at_top) lemma ae_cover_Icc : ae_cover ΞΌ l (Ξ» i, Icc (a i) (b i)) := { ae_eventually_mem := ae_of_all ΞΌ (Ξ» x, (ha.eventually $ eventually_le_at_bot x).mp $ (hb.eventually $ eventually_ge_at_top x).mono $ Ξ» i hbi hai, ⟨hai, hbi⟩ ), measurable := Ξ» i, measurable_set_Icc } lemma ae_cover_Ici : ae_cover ΞΌ l (Ξ» i, Ici $ a i) := { ae_eventually_mem := ae_of_all ΞΌ (Ξ» x, (ha.eventually $ eventually_le_at_bot x).mono $ Ξ» i hai, hai ), measurable := Ξ» i, measurable_set_Ici } lemma ae_cover_Iic : ae_cover ΞΌ l (Ξ» i, Iic $ b i) := { ae_eventually_mem := ae_of_all ΞΌ (Ξ» x, (hb.eventually $ eventually_ge_at_top x).mono $ Ξ» i hbi, hbi ), measurable := Ξ» i, measurable_set_Iic } end preorder_Ξ± section linear_order_Ξ± variables [linear_order Ξ±] [topological_space Ξ±] [order_closed_topology Ξ±] [opens_measurable_space Ξ±] {a b : ΞΉ β†’ Ξ±} (ha : tendsto a l at_bot) (hb : tendsto b l at_top) lemma ae_cover_Ioo [no_min_order Ξ±] [no_max_order Ξ±] : ae_cover ΞΌ l (Ξ» i, Ioo (a i) (b i)) := { ae_eventually_mem := ae_of_all ΞΌ (Ξ» x, (ha.eventually $ eventually_lt_at_bot x).mp $ (hb.eventually $ eventually_gt_at_top x).mono $ Ξ» i hbi hai, ⟨hai, hbi⟩ ), measurable := Ξ» i, measurable_set_Ioo } lemma ae_cover_Ioc [no_min_order Ξ±] : ae_cover ΞΌ l (Ξ» i, Ioc (a i) (b i)) := { ae_eventually_mem := ae_of_all ΞΌ (Ξ» x, (ha.eventually $ eventually_lt_at_bot x).mp $ (hb.eventually $ eventually_ge_at_top x).mono $ Ξ» i hbi hai, ⟨hai, hbi⟩ ), measurable := Ξ» i, measurable_set_Ioc } lemma ae_cover_Ico [no_max_order Ξ±] : ae_cover ΞΌ l (Ξ» i, Ico (a i) (b i)) := { ae_eventually_mem := ae_of_all ΞΌ (Ξ» x, (ha.eventually $ eventually_le_at_bot x).mp $ (hb.eventually $ eventually_gt_at_top x).mono $ Ξ» i hbi hai, ⟨hai, hbi⟩ ), measurable := Ξ» i, measurable_set_Ico } lemma ae_cover_Ioi [no_min_order Ξ±] : ae_cover ΞΌ l (Ξ» i, Ioi $ a i) := { ae_eventually_mem := ae_of_all ΞΌ (Ξ» x, (ha.eventually $ eventually_lt_at_bot x).mono $ Ξ» i hai, hai ), measurable := Ξ» i, measurable_set_Ioi } lemma ae_cover_Iio [no_max_order Ξ±] : ae_cover ΞΌ l (Ξ» i, Iio $ b i) := { ae_eventually_mem := ae_of_all ΞΌ (Ξ» x, (hb.eventually $ eventually_gt_at_top x).mono $ Ξ» i hbi, hbi ), measurable := Ξ» i, measurable_set_Iio } end linear_order_Ξ± section finite_intervals variables [linear_order Ξ±] [topological_space Ξ±] [order_closed_topology Ξ±] [opens_measurable_space Ξ±] {a b : ΞΉ β†’ Ξ±} {A B : Ξ±} (ha : tendsto a l (𝓝 A)) (hb : tendsto b l (𝓝 B)) lemma ae_cover_Ioo_of_Icc : ae_cover (ΞΌ.restrict $ Ioo A B) l (Ξ» i, Icc (a i) (b i)) := { ae_eventually_mem := (ae_restrict_iff' measurable_set_Ioo).mpr ( ae_of_all ΞΌ (Ξ» x hx, (ha.eventually $ eventually_le_nhds hx.left).mp $ (hb.eventually $ eventually_ge_nhds hx.right).mono $ Ξ» i hbi hai, ⟨hai, hbi⟩)), measurable := Ξ» i, measurable_set_Icc, } lemma ae_cover_Ioo_of_Ico : ae_cover (ΞΌ.restrict $ Ioo A B) l (Ξ» i, Ico (a i) (b i)) := { ae_eventually_mem := (ae_restrict_iff' measurable_set_Ioo).mpr ( ae_of_all ΞΌ (Ξ» x hx, (ha.eventually $ eventually_le_nhds hx.left).mp $ (hb.eventually $ eventually_gt_nhds hx.right).mono $ Ξ» i hbi hai, ⟨hai, hbi⟩)), measurable := Ξ» i, measurable_set_Ico, } lemma ae_cover_Ioo_of_Ioc : ae_cover (ΞΌ.restrict $ Ioo A B) l (Ξ» i, Ioc (a i) (b i)) := { ae_eventually_mem := (ae_restrict_iff' measurable_set_Ioo).mpr ( ae_of_all ΞΌ (Ξ» x hx, (ha.eventually $ eventually_lt_nhds hx.left).mp $ (hb.eventually $ eventually_ge_nhds hx.right).mono $ Ξ» i hbi hai, ⟨hai, hbi⟩)), measurable := Ξ» i, measurable_set_Ioc, } lemma ae_cover_Ioo_of_Ioo : ae_cover (ΞΌ.restrict $ Ioo A B) l (Ξ» i, Ioo (a i) (b i)) := { ae_eventually_mem := (ae_restrict_iff' measurable_set_Ioo).mpr ( ae_of_all ΞΌ (Ξ» x hx, (ha.eventually $ eventually_lt_nhds hx.left).mp $ (hb.eventually $ eventually_gt_nhds hx.right).mono $ Ξ» i hbi hai, ⟨hai, hbi⟩)), measurable := Ξ» i, measurable_set_Ioo, } variables [has_no_atoms ΞΌ] lemma ae_cover_Ioc_of_Icc (ha : tendsto a l (𝓝 A)) (hb : tendsto b l (𝓝 B)) : ae_cover (ΞΌ.restrict $ Ioc A B) l (Ξ» i, Icc (a i) (b i)) := by simp [measure.restrict_congr_set Ioo_ae_eq_Ioc.symm, ae_cover_Ioo_of_Icc ha hb] lemma ae_cover_Ioc_of_Ico (ha : tendsto a l (𝓝 A)) (hb : tendsto b l (𝓝 B)) : ae_cover (ΞΌ.restrict $ Ioc A B) l (Ξ» i, Ico (a i) (b i)) := by simp [measure.restrict_congr_set Ioo_ae_eq_Ioc.symm, ae_cover_Ioo_of_Ico ha hb] lemma ae_cover_Ioc_of_Ioc (ha : tendsto a l (𝓝 A)) (hb : tendsto b l (𝓝 B)) : ae_cover (ΞΌ.restrict $ Ioc A B) l (Ξ» i, Ioc (a i) (b i)) := by simp [measure.restrict_congr_set Ioo_ae_eq_Ioc.symm, ae_cover_Ioo_of_Ioc ha hb] lemma ae_cover_Ioc_of_Ioo (ha : tendsto a l (𝓝 A)) (hb : tendsto b l (𝓝 B)) : ae_cover (ΞΌ.restrict $ Ioc A B) l (Ξ» i, Ioo (a i) (b i)) := by simp [measure.restrict_congr_set Ioo_ae_eq_Ioc.symm, ae_cover_Ioo_of_Ioo ha hb] lemma ae_cover_Ico_of_Icc (ha : tendsto a l (𝓝 A)) (hb : tendsto b l (𝓝 B)) : ae_cover (ΞΌ.restrict $ Ico A B) l (Ξ» i, Icc (a i) (b i)) := by simp [measure.restrict_congr_set Ioo_ae_eq_Ico.symm, ae_cover_Ioo_of_Icc ha hb] lemma ae_cover_Ico_of_Ico (ha : tendsto a l (𝓝 A)) (hb : tendsto b l (𝓝 B)) : ae_cover (ΞΌ.restrict $ Ico A B) l (Ξ» i, Ico (a i) (b i)) := by simp [measure.restrict_congr_set Ioo_ae_eq_Ico.symm, ae_cover_Ioo_of_Ico ha hb] lemma ae_cover_Ico_of_Ioc (ha : tendsto a l (𝓝 A)) (hb : tendsto b l (𝓝 B)) : ae_cover (ΞΌ.restrict $ Ico A B) l (Ξ» i, Ioc (a i) (b i)) := by simp [measure.restrict_congr_set Ioo_ae_eq_Ico.symm, ae_cover_Ioo_of_Ioc ha hb] lemma ae_cover_Ico_of_Ioo (ha : tendsto a l (𝓝 A)) (hb : tendsto b l (𝓝 B)) : ae_cover (ΞΌ.restrict $ Ico A B) l (Ξ» i, Ioo (a i) (b i)) := by simp [measure.restrict_congr_set Ioo_ae_eq_Ico.symm, ae_cover_Ioo_of_Ioo ha hb] lemma ae_cover_Icc_of_Icc (ha : tendsto a l (𝓝 A)) (hb : tendsto b l (𝓝 B)) : ae_cover (ΞΌ.restrict $ Icc A B) l (Ξ» i, Icc (a i) (b i)) := by simp [measure.restrict_congr_set Ioo_ae_eq_Icc.symm, ae_cover_Ioo_of_Icc ha hb] lemma ae_cover_Icc_of_Ico (ha : tendsto a l (𝓝 A)) (hb : tendsto b l (𝓝 B)) : ae_cover (ΞΌ.restrict $ Icc A B) l (Ξ» i, Ico (a i) (b i)) := by simp [measure.restrict_congr_set Ioo_ae_eq_Icc.symm, ae_cover_Ioo_of_Ico ha hb] lemma ae_cover_Icc_of_Ioc (ha : tendsto a l (𝓝 A)) (hb : tendsto b l (𝓝 B)) : ae_cover (ΞΌ.restrict $ Icc A B) l (Ξ» i, Ioc (a i) (b i)) := by simp [measure.restrict_congr_set Ioo_ae_eq_Icc.symm, ae_cover_Ioo_of_Ioc ha hb] lemma ae_cover_Icc_of_Ioo (ha : tendsto a l (𝓝 A)) (hb : tendsto b l (𝓝 B)) : ae_cover (ΞΌ.restrict $ Icc A B) l (Ξ» i, Ioo (a i) (b i)) := by simp [measure.restrict_congr_set Ioo_ae_eq_Icc.symm, ae_cover_Ioo_of_Ioo ha hb] end finite_intervals lemma ae_cover.restrict {Ο† : ΞΉ β†’ set Ξ±} (hΟ† : ae_cover ΞΌ l Ο†) {s : set Ξ±} : ae_cover (ΞΌ.restrict s) l Ο† := { ae_eventually_mem := ae_restrict_of_ae hΟ†.ae_eventually_mem, measurable := hΟ†.measurable } lemma ae_cover_restrict_of_ae_imp {s : set Ξ±} {Ο† : ΞΉ β†’ set Ξ±} (hs : measurable_set s) (ae_eventually_mem : βˆ€α΅ x βˆ‚ΞΌ, x ∈ s β†’ βˆ€αΆ  n in l, x ∈ Ο† n) (measurable : βˆ€ n, measurable_set $ Ο† n) : ae_cover (ΞΌ.restrict s) l Ο† := { ae_eventually_mem := by rwa ae_restrict_iff' hs, measurable := measurable } lemma ae_cover.inter_restrict {Ο† : ΞΉ β†’ set Ξ±} (hΟ† : ae_cover ΞΌ l Ο†) {s : set Ξ±} (hs : measurable_set s) : ae_cover (ΞΌ.restrict s) l (Ξ» i, Ο† i ∩ s) := ae_cover_restrict_of_ae_imp hs (hΟ†.ae_eventually_mem.mono (Ξ» x hx hxs, hx.mono $ Ξ» i hi, ⟨hi, hxs⟩)) (Ξ» i, (hΟ†.measurable i).inter hs) lemma ae_cover.ae_tendsto_indicator {Ξ² : Type*} [has_zero Ξ²] [topological_space Ξ²] (f : Ξ± β†’ Ξ²) {Ο† : ΞΉ β†’ set Ξ±} (hΟ† : ae_cover ΞΌ l Ο†) : βˆ€α΅ x βˆ‚ΞΌ, tendsto (Ξ» i, (Ο† i).indicator f x) l (𝓝 $ f x) := hΟ†.ae_eventually_mem.mono (Ξ» x hx, tendsto_const_nhds.congr' $ hx.mono $ Ξ» n hn, (indicator_of_mem hn _).symm) lemma ae_cover.ae_measurable {Ξ² : Type*} [measurable_space Ξ²] [l.is_countably_generated] [l.ne_bot] {f : Ξ± β†’ Ξ²} {Ο† : ΞΉ β†’ set Ξ±} (hΟ† : ae_cover ΞΌ l Ο†) (hfm : βˆ€ i, ae_measurable f (ΞΌ.restrict $ Ο† i)) : ae_measurable f ΞΌ := begin obtain ⟨u, hu⟩ := l.exists_seq_tendsto, have := ae_measurable_Union_iff.mpr (Ξ» (n : β„•), hfm (u n)), rwa measure.restrict_eq_self_of_ae_mem at this, filter_upwards [hΟ†.ae_eventually_mem] with x hx using let ⟨i, hi⟩ := (hu.eventually hx).exists in mem_Union.mpr ⟨i, hi⟩ end lemma ae_cover.ae_strongly_measurable {Ξ² : Type*} [topological_space Ξ²] [pseudo_metrizable_space Ξ²] [l.is_countably_generated] [l.ne_bot] {f : Ξ± β†’ Ξ²} {Ο† : ΞΉ β†’ set Ξ±} (hΟ† : ae_cover ΞΌ l Ο†) (hfm : βˆ€ i, ae_strongly_measurable f (ΞΌ.restrict $ Ο† i)) : ae_strongly_measurable f ΞΌ := begin obtain ⟨u, hu⟩ := l.exists_seq_tendsto, have := ae_strongly_measurable_Union_iff.mpr (Ξ» (n : β„•), hfm (u n)), rwa measure.restrict_eq_self_of_ae_mem at this, filter_upwards [hΟ†.ae_eventually_mem] with x hx using let ⟨i, hi⟩ := (hu.eventually hx).exists in mem_Union.mpr ⟨i, hi⟩ end end ae_cover lemma ae_cover.comp_tendsto {Ξ± ΞΉ ΞΉ' : Type*} [measurable_space Ξ±] {ΞΌ : measure Ξ±} {l : filter ΞΉ} {l' : filter ΞΉ'} {Ο† : ΞΉ β†’ set Ξ±} (hΟ† : ae_cover ΞΌ l Ο†) {u : ΞΉ' β†’ ΞΉ} (hu : tendsto u l' l) : ae_cover ΞΌ l' (Ο† ∘ u) := { ae_eventually_mem := hΟ†.ae_eventually_mem.mono (Ξ» x hx, hu.eventually hx), measurable := Ξ» i, hΟ†.measurable (u i) } section ae_cover_Union_Inter_countable variables {Ξ± ΞΉ : Type*} [countable ΞΉ] [measurable_space Ξ±] {ΞΌ : measure Ξ±} lemma ae_cover.bUnion_Iic_ae_cover [preorder ΞΉ] {Ο† : ΞΉ β†’ set Ξ±} (hΟ† : ae_cover ΞΌ at_top Ο†) : ae_cover ΞΌ at_top (Ξ» (n : ΞΉ), ⋃ k (h : k ∈ Iic n), Ο† k) := { ae_eventually_mem := hΟ†.ae_eventually_mem.mono (Ξ» x h, h.mono (Ξ» i hi, mem_bUnion right_mem_Iic hi)), measurable := Ξ» i, measurable_set.bUnion (to_countable _) (Ξ» n _, hΟ†.measurable n) } lemma ae_cover.bInter_Ici_ae_cover [semilattice_sup ΞΉ] [nonempty ΞΉ] {Ο† : ΞΉ β†’ set Ξ±} (hΟ† : ae_cover ΞΌ at_top Ο†) : ae_cover ΞΌ at_top (Ξ» (n : ΞΉ), β‹‚ k (h : k ∈ Ici n), Ο† k) := { ae_eventually_mem := hΟ†.ae_eventually_mem.mono begin intros x h, rw eventually_at_top at *, rcases h with ⟨i, hi⟩, use i, intros j hj, exact mem_bInter (Ξ» k hk, hi k (le_trans hj hk)), end, measurable := Ξ» i, measurable_set.bInter (to_countable _) (Ξ» n _, hΟ†.measurable n) } end ae_cover_Union_Inter_countable section lintegral variables {Ξ± ΞΉ : Type*} [measurable_space Ξ±] {ΞΌ : measure Ξ±} {l : filter ΞΉ} private lemma lintegral_tendsto_of_monotone_of_nat {Ο† : β„• β†’ set Ξ±} (hΟ† : ae_cover ΞΌ at_top Ο†) (hmono : monotone Ο†) {f : Ξ± β†’ ℝβ‰₯0∞} (hfm : ae_measurable f ΞΌ) : tendsto (Ξ» i, ∫⁻ x in Ο† i, f x βˆ‚ΞΌ) at_top (𝓝 $ ∫⁻ x, f x βˆ‚ΞΌ) := let F := Ξ» n, (Ο† n).indicator f in have key₁ : βˆ€ n, ae_measurable (F n) ΞΌ, from Ξ» n, hfm.indicator (hΟ†.measurable n), have keyβ‚‚ : βˆ€α΅ (x : Ξ±) βˆ‚ΞΌ, monotone (Ξ» n, F n x), from ae_of_all _ (Ξ» x i j hij, indicator_le_indicator_of_subset (hmono hij) (Ξ» x, zero_le $ f x) x), have key₃ : βˆ€α΅ (x : Ξ±) βˆ‚ΞΌ, tendsto (Ξ» n, F n x) at_top (𝓝 (f x)), from hΟ†.ae_tendsto_indicator f, (lintegral_tendsto_of_tendsto_of_monotone key₁ keyβ‚‚ key₃).congr (Ξ» n, lintegral_indicator f (hΟ†.measurable n)) lemma ae_cover.lintegral_tendsto_of_nat {Ο† : β„• β†’ set Ξ±} (hΟ† : ae_cover ΞΌ at_top Ο†) {f : Ξ± β†’ ℝβ‰₯0∞} (hfm : ae_measurable f ΞΌ) : tendsto (Ξ» i, ∫⁻ x in Ο† i, f x βˆ‚ΞΌ) at_top (𝓝 $ ∫⁻ x, f x βˆ‚ΞΌ) := begin have lim₁ := lintegral_tendsto_of_monotone_of_nat (hΟ†.bInter_Ici_ae_cover) (Ξ» i j hij, bInter_subset_bInter_left (Ici_subset_Ici.mpr hij)) hfm, have limβ‚‚ := lintegral_tendsto_of_monotone_of_nat (hΟ†.bUnion_Iic_ae_cover) (Ξ» i j hij, bUnion_subset_bUnion_left (Iic_subset_Iic.mpr hij)) hfm, have le₁ := Ξ» n, lintegral_mono_set (bInter_subset_of_mem left_mem_Ici), have leβ‚‚ := Ξ» n, lintegral_mono_set (subset_bUnion_of_mem right_mem_Iic), exact tendsto_of_tendsto_of_tendsto_of_le_of_le lim₁ limβ‚‚ le₁ leβ‚‚ end lemma ae_cover.lintegral_tendsto_of_countably_generated [l.is_countably_generated] {Ο† : ΞΉ β†’ set Ξ±} (hΟ† : ae_cover ΞΌ l Ο†) {f : Ξ± β†’ ℝβ‰₯0∞} (hfm : ae_measurable f ΞΌ) : tendsto (Ξ» i, ∫⁻ x in Ο† i, f x βˆ‚ΞΌ) l (𝓝 $ ∫⁻ x, f x βˆ‚ΞΌ) := tendsto_of_seq_tendsto (Ξ» u hu, (hΟ†.comp_tendsto hu).lintegral_tendsto_of_nat hfm) lemma ae_cover.lintegral_eq_of_tendsto [l.ne_bot] [l.is_countably_generated] {Ο† : ΞΉ β†’ set Ξ±} (hΟ† : ae_cover ΞΌ l Ο†) {f : Ξ± β†’ ℝβ‰₯0∞} (I : ℝβ‰₯0∞) (hfm : ae_measurable f ΞΌ) (htendsto : tendsto (Ξ» i, ∫⁻ x in Ο† i, f x βˆ‚ΞΌ) l (𝓝 I)) : ∫⁻ x, f x βˆ‚ΞΌ = I := tendsto_nhds_unique (hΟ†.lintegral_tendsto_of_countably_generated hfm) htendsto lemma ae_cover.supr_lintegral_eq_of_countably_generated [nonempty ΞΉ] [l.ne_bot] [l.is_countably_generated] {Ο† : ΞΉ β†’ set Ξ±} (hΟ† : ae_cover ΞΌ l Ο†) {f : Ξ± β†’ ℝβ‰₯0∞} (hfm : ae_measurable f ΞΌ) : (⨆ (i : ΞΉ), ∫⁻ x in Ο† i, f x βˆ‚ΞΌ) = ∫⁻ x, f x βˆ‚ΞΌ := begin have := hΟ†.lintegral_tendsto_of_countably_generated hfm, refine csupr_eq_of_forall_le_of_forall_lt_exists_gt (Ξ» i, lintegral_mono' measure.restrict_le_self le_rfl) (Ξ» w hw, _), rcases exists_between hw with ⟨m, hm₁, hmβ‚‚βŸ©, rcases (eventually_ge_of_tendsto_gt hmβ‚‚ this).exists with ⟨i, hi⟩, exact ⟨i, lt_of_lt_of_le hm₁ hi⟩, end end lintegral section integrable variables {Ξ± ΞΉ E : Type*} [measurable_space Ξ±] {ΞΌ : measure Ξ±} {l : filter ΞΉ} [normed_add_comm_group E] lemma ae_cover.integrable_of_lintegral_nnnorm_bounded [l.ne_bot] [l.is_countably_generated] {Ο† : ΞΉ β†’ set Ξ±} (hΟ† : ae_cover ΞΌ l Ο†) {f : Ξ± β†’ E} (I : ℝ) (hfm : ae_strongly_measurable f ΞΌ) (hbounded : βˆ€αΆ  i in l, ∫⁻ x in Ο† i, β€–f xβ€–β‚Š βˆ‚ΞΌ ≀ ennreal.of_real I) : integrable f ΞΌ := begin refine ⟨hfm, (le_of_tendsto _ hbounded).trans_lt ennreal.of_real_lt_top⟩, exact hΟ†.lintegral_tendsto_of_countably_generated hfm.ennnorm end lemma ae_cover.integrable_of_lintegral_nnnorm_tendsto [l.ne_bot] [l.is_countably_generated] {Ο† : ΞΉ β†’ set Ξ±} (hΟ† : ae_cover ΞΌ l Ο†) {f : Ξ± β†’ E} (I : ℝ) (hfm : ae_strongly_measurable f ΞΌ) (htendsto : tendsto (Ξ» i, ∫⁻ x in Ο† i, β€–f xβ€–β‚Š βˆ‚ΞΌ) l (𝓝 $ ennreal.of_real I)) : integrable f ΞΌ := begin refine hΟ†.integrable_of_lintegral_nnnorm_bounded (max 1 (I + 1)) hfm _, refine htendsto.eventually (ge_mem_nhds _), refine (ennreal.of_real_lt_of_real_iff (lt_max_of_lt_left zero_lt_one)).2 _, exact lt_max_of_lt_right (lt_add_one I), end lemma ae_cover.integrable_of_lintegral_nnnorm_bounded' [l.ne_bot] [l.is_countably_generated] {Ο† : ΞΉ β†’ set Ξ±} (hΟ† : ae_cover ΞΌ l Ο†) {f : Ξ± β†’ E} (I : ℝβ‰₯0) (hfm : ae_strongly_measurable f ΞΌ) (hbounded : βˆ€αΆ  i in l, ∫⁻ x in Ο† i, β€–f xβ€–β‚Š βˆ‚ΞΌ ≀ I) : integrable f ΞΌ := hΟ†.integrable_of_lintegral_nnnorm_bounded I hfm (by simpa only [ennreal.of_real_coe_nnreal] using hbounded) lemma ae_cover.integrable_of_lintegral_nnnorm_tendsto' [l.ne_bot] [l.is_countably_generated] {Ο† : ΞΉ β†’ set Ξ±} (hΟ† : ae_cover ΞΌ l Ο†) {f : Ξ± β†’ E} (I : ℝβ‰₯0) (hfm : ae_strongly_measurable f ΞΌ) (htendsto : tendsto (Ξ» i, ∫⁻ x in Ο† i, β€–f xβ€–β‚Š βˆ‚ΞΌ) l (𝓝 I)) : integrable f ΞΌ := hΟ†.integrable_of_lintegral_nnnorm_tendsto I hfm (by simpa only [ennreal.of_real_coe_nnreal] using htendsto) lemma ae_cover.integrable_of_integral_norm_bounded [l.ne_bot] [l.is_countably_generated] {Ο† : ΞΉ β†’ set Ξ±} (hΟ† : ae_cover ΞΌ l Ο†) {f : Ξ± β†’ E} (I : ℝ) (hfi : βˆ€ i, integrable_on f (Ο† i) ΞΌ) (hbounded : βˆ€αΆ  i in l, ∫ x in Ο† i, β€–f xβ€– βˆ‚ΞΌ ≀ I) : integrable f ΞΌ := begin have hfm : ae_strongly_measurable f ΞΌ := hΟ†.ae_strongly_measurable (Ξ» i, (hfi i).ae_strongly_measurable), refine hΟ†.integrable_of_lintegral_nnnorm_bounded I hfm _, conv at hbounded in (integral _ _) { rw integral_eq_lintegral_of_nonneg_ae (ae_of_all _ (Ξ» x, @norm_nonneg E _ (f x))) hfm.norm.restrict }, conv at hbounded in (ennreal.of_real _) { dsimp, rw ← coe_nnnorm, rw ennreal.of_real_coe_nnreal }, refine hbounded.mono (Ξ» i hi, _), rw ←ennreal.of_real_to_real (ne_top_of_lt (hfi i).2), apply ennreal.of_real_le_of_real hi, end lemma ae_cover.integrable_of_integral_norm_tendsto [l.ne_bot] [l.is_countably_generated] {Ο† : ΞΉ β†’ set Ξ±} (hΟ† : ae_cover ΞΌ l Ο†) {f : Ξ± β†’ E} (I : ℝ) (hfi : βˆ€ i, integrable_on f (Ο† i) ΞΌ) (htendsto : tendsto (Ξ» i, ∫ x in Ο† i, β€–f xβ€– βˆ‚ΞΌ) l (𝓝 I)) : integrable f ΞΌ := let ⟨I', hI'⟩ := htendsto.is_bounded_under_le in hΟ†.integrable_of_integral_norm_bounded I' hfi hI' lemma ae_cover.integrable_of_integral_bounded_of_nonneg_ae [l.ne_bot] [l.is_countably_generated] {Ο† : ΞΉ β†’ set Ξ±} (hΟ† : ae_cover ΞΌ l Ο†) {f : Ξ± β†’ ℝ} (I : ℝ) (hfi : βˆ€ i, integrable_on f (Ο† i) ΞΌ) (hnng : βˆ€α΅ x βˆ‚ΞΌ, 0 ≀ f x) (hbounded : βˆ€αΆ  i in l, ∫ x in Ο† i, f x βˆ‚ΞΌ ≀ I) : integrable f ΞΌ := hΟ†.integrable_of_integral_norm_bounded I hfi $ hbounded.mono $ Ξ» i hi, (integral_congr_ae $ ae_restrict_of_ae $ hnng.mono $ Ξ» x, real.norm_of_nonneg).le.trans hi lemma ae_cover.integrable_of_integral_tendsto_of_nonneg_ae [l.ne_bot] [l.is_countably_generated] {Ο† : ΞΉ β†’ set Ξ±} (hΟ† : ae_cover ΞΌ l Ο†) {f : Ξ± β†’ ℝ} (I : ℝ) (hfi : βˆ€ i, integrable_on f (Ο† i) ΞΌ) (hnng : βˆ€α΅ x βˆ‚ΞΌ, 0 ≀ f x) (htendsto : tendsto (Ξ» i, ∫ x in Ο† i, f x βˆ‚ΞΌ) l (𝓝 I)) : integrable f ΞΌ := let ⟨I', hI'⟩ := htendsto.is_bounded_under_le in hΟ†.integrable_of_integral_bounded_of_nonneg_ae I' hfi hnng hI' end integrable section integral variables {Ξ± ΞΉ E : Type*} [measurable_space Ξ±] {ΞΌ : measure Ξ±} {l : filter ΞΉ} [normed_add_comm_group E] [normed_space ℝ E] [complete_space E] lemma ae_cover.integral_tendsto_of_countably_generated [l.is_countably_generated] {Ο† : ΞΉ β†’ set Ξ±} (hΟ† : ae_cover ΞΌ l Ο†) {f : Ξ± β†’ E} (hfi : integrable f ΞΌ) : tendsto (Ξ» i, ∫ x in Ο† i, f x βˆ‚ΞΌ) l (𝓝 $ ∫ x, f x βˆ‚ΞΌ) := suffices h : tendsto (Ξ» i, ∫ (x : Ξ±), (Ο† i).indicator f x βˆ‚ΞΌ) l (𝓝 (∫ (x : Ξ±), f x βˆ‚ΞΌ)), by { convert h, ext n, rw integral_indicator (hΟ†.measurable n) }, tendsto_integral_filter_of_dominated_convergence (Ξ» x, β€–f xβ€–) (eventually_of_forall $ Ξ» i, hfi.ae_strongly_measurable.indicator $ hΟ†.measurable i) (eventually_of_forall $ Ξ» i, ae_of_all _ $ Ξ» x, norm_indicator_le_norm_self _ _) hfi.norm (hΟ†.ae_tendsto_indicator f) /-- Slight reformulation of `measure_theory.ae_cover.integral_tendsto_of_countably_generated`. -/ lemma ae_cover.integral_eq_of_tendsto [l.ne_bot] [l.is_countably_generated] {Ο† : ΞΉ β†’ set Ξ±} (hΟ† : ae_cover ΞΌ l Ο†) {f : Ξ± β†’ E} (I : E) (hfi : integrable f ΞΌ) (h : tendsto (Ξ» n, ∫ x in Ο† n, f x βˆ‚ΞΌ) l (𝓝 I)) : ∫ x, f x βˆ‚ΞΌ = I := tendsto_nhds_unique (hΟ†.integral_tendsto_of_countably_generated hfi) h lemma ae_cover.integral_eq_of_tendsto_of_nonneg_ae [l.ne_bot] [l.is_countably_generated] {Ο† : ΞΉ β†’ set Ξ±} (hΟ† : ae_cover ΞΌ l Ο†) {f : Ξ± β†’ ℝ} (I : ℝ) (hnng : 0 ≀ᡐ[ΞΌ] f) (hfi : βˆ€ n, integrable_on f (Ο† n) ΞΌ) (htendsto : tendsto (Ξ» n, ∫ x in Ο† n, f x βˆ‚ΞΌ) l (𝓝 I)) : ∫ x, f x βˆ‚ΞΌ = I := have hfi' : integrable f ΞΌ, from hΟ†.integrable_of_integral_tendsto_of_nonneg_ae I hfi hnng htendsto, hΟ†.integral_eq_of_tendsto I hfi' htendsto end integral section integrable_of_interval_integral variables {ΞΉ E : Type*} {ΞΌ : measure ℝ} {l : filter ΞΉ} [filter.ne_bot l] [is_countably_generated l] [normed_add_comm_group E] {a b : ΞΉ β†’ ℝ} {f : ℝ β†’ E} lemma integrable_of_interval_integral_norm_bounded (I : ℝ) (hfi : βˆ€ i, integrable_on f (Ioc (a i) (b i)) ΞΌ) (ha : tendsto a l at_bot) (hb : tendsto b l at_top) (h : βˆ€αΆ  i in l, ∫ x in a i .. b i, β€–f xβ€– βˆ‚ΞΌ ≀ I) : integrable f ΞΌ := begin have hΟ† : ae_cover ΞΌ l _ := ae_cover_Ioc ha hb, refine hΟ†.integrable_of_integral_norm_bounded I hfi (h.mp _), filter_upwards [ha.eventually (eventually_le_at_bot 0), hb.eventually (eventually_ge_at_top 0)] with i hai hbi ht, rwa ←interval_integral.integral_of_le (hai.trans hbi) end /-- If `f` is integrable on intervals `Ioc (a i) (b i)`, where `a i` tends to -∞ and `b i` tends to ∞, and `∫ x in a i .. b i, β€–f xβ€– βˆ‚ΞΌ` converges to `I : ℝ` along a filter `l`, then `f` is integrable on the interval (-∞, ∞) -/ lemma integrable_of_interval_integral_norm_tendsto (I : ℝ) (hfi : βˆ€ i, integrable_on f (Ioc (a i) (b i)) ΞΌ) (ha : tendsto a l at_bot) (hb : tendsto b l at_top) (h : tendsto (Ξ» i, ∫ x in a i .. b i, β€–f xβ€– βˆ‚ΞΌ) l (𝓝 I)) : integrable f ΞΌ := let ⟨I', hI'⟩ := h.is_bounded_under_le in integrable_of_interval_integral_norm_bounded I' hfi ha hb hI' lemma integrable_on_Iic_of_interval_integral_norm_bounded (I b : ℝ) (hfi : βˆ€ i, integrable_on f (Ioc (a i) b) ΞΌ) (ha : tendsto a l at_bot) (h : βˆ€αΆ  i in l, (∫ x in a i .. b, β€–f xβ€– βˆ‚ΞΌ) ≀ I) : integrable_on f (Iic b) ΞΌ := begin have hΟ† : ae_cover (ΞΌ.restrict $ Iic b) l _ := ae_cover_Ioi ha, have hfi : βˆ€ i, integrable_on f (Ioi (a i)) (ΞΌ.restrict $ Iic b), { intro i, rw [integrable_on, measure.restrict_restrict (hΟ†.measurable i)], exact hfi i }, refine hΟ†.integrable_of_integral_norm_bounded I hfi (h.mp _), filter_upwards [ha.eventually (eventually_le_at_bot b)] with i hai, rw [interval_integral.integral_of_le hai, measure.restrict_restrict (hΟ†.measurable i)], exact id end /-- If `f` is integrable on intervals `Ioc (a i) b`, where `a i` tends to -∞, and `∫ x in a i .. b, β€–f xβ€– βˆ‚ΞΌ` converges to `I : ℝ` along a filter `l`, then `f` is integrable on the interval (-∞, b) -/ lemma integrable_on_Iic_of_interval_integral_norm_tendsto (I b : ℝ) (hfi : βˆ€ i, integrable_on f (Ioc (a i) b) ΞΌ) (ha : tendsto a l at_bot) (h : tendsto (Ξ» i, ∫ x in a i .. b, β€–f xβ€– βˆ‚ΞΌ) l (𝓝 I)) : integrable_on f (Iic b) ΞΌ := let ⟨I', hI'⟩ := h.is_bounded_under_le in integrable_on_Iic_of_interval_integral_norm_bounded I' b hfi ha hI' lemma integrable_on_Ioi_of_interval_integral_norm_bounded (I a : ℝ) (hfi : βˆ€ i, integrable_on f (Ioc a (b i)) ΞΌ) (hb : tendsto b l at_top) (h : βˆ€αΆ  i in l, (∫ x in a .. b i, β€–f xβ€– βˆ‚ΞΌ) ≀ I) : integrable_on f (Ioi a) ΞΌ := begin have hΟ† : ae_cover (ΞΌ.restrict $ Ioi a) l _ := ae_cover_Iic hb, have hfi : βˆ€ i, integrable_on f (Iic (b i)) (ΞΌ.restrict $ Ioi a), { intro i, rw [integrable_on, measure.restrict_restrict (hΟ†.measurable i), inter_comm], exact hfi i }, refine hΟ†.integrable_of_integral_norm_bounded I hfi (h.mp _), filter_upwards [hb.eventually (eventually_ge_at_top a)] with i hbi, rw [interval_integral.integral_of_le hbi, measure.restrict_restrict (hΟ†.measurable i), inter_comm], exact id end /-- If `f` is integrable on intervals `Ioc a (b i)`, where `b i` tends to ∞, and `∫ x in a .. b i, β€–f xβ€– βˆ‚ΞΌ` converges to `I : ℝ` along a filter `l`, then `f` is integrable on the interval (a, ∞) -/ lemma integrable_on_Ioi_of_interval_integral_norm_tendsto (I a : ℝ) (hfi : βˆ€ i, integrable_on f (Ioc a (b i)) ΞΌ) (hb : tendsto b l at_top) (h : tendsto (Ξ» i, ∫ x in a .. b i, β€–f xβ€– βˆ‚ΞΌ) l (𝓝 $ I)) : integrable_on f (Ioi a) ΞΌ := let ⟨I', hI'⟩ := h.is_bounded_under_le in integrable_on_Ioi_of_interval_integral_norm_bounded I' a hfi hb hI' lemma integrable_on_Ioc_of_interval_integral_norm_bounded {I aβ‚€ bβ‚€ : ℝ} (hfi : βˆ€ i, integrable_on f $ Ioc (a i) (b i)) (ha : tendsto a l $ 𝓝 aβ‚€) (hb : tendsto b l $ 𝓝 bβ‚€) (h : βˆ€αΆ  i in l, (∫ x in Ioc (a i) (b i), β€–f xβ€–) ≀ I) : integrable_on f (Ioc aβ‚€ bβ‚€) := begin refine (ae_cover_Ioc_of_Ioc ha hb).integrable_of_integral_norm_bounded I (Ξ» i, (hfi i).restrict measurable_set_Ioc) (eventually.mono h _), intros i hi, simp only [measure.restrict_restrict measurable_set_Ioc], refine le_trans (set_integral_mono_set (hfi i).norm _ _) hi, { apply ae_of_all, simp only [pi.zero_apply, norm_nonneg, forall_const] }, { apply ae_of_all, intros c hc, exact hc.1 }, end lemma integrable_on_Ioc_of_interval_integral_norm_bounded_left {I aβ‚€ b : ℝ} (hfi : βˆ€ i, integrable_on f $ Ioc (a i) b) (ha : tendsto a l $ 𝓝 aβ‚€) (h : βˆ€αΆ  i in l, (∫ x in Ioc (a i) b, β€–f xβ€– ) ≀ I) : integrable_on f (Ioc aβ‚€ b) := integrable_on_Ioc_of_interval_integral_norm_bounded hfi ha tendsto_const_nhds h lemma integrable_on_Ioc_of_interval_integral_norm_bounded_right {I a bβ‚€ : ℝ} (hfi : βˆ€ i, integrable_on f $ Ioc a (b i)) (hb : tendsto b l $ 𝓝 bβ‚€) (h : βˆ€αΆ  i in l, (∫ x in Ioc a (b i), β€–f xβ€– ) ≀ I) : integrable_on f (Ioc a bβ‚€) := integrable_on_Ioc_of_interval_integral_norm_bounded hfi tendsto_const_nhds hb h end integrable_of_interval_integral section integral_of_interval_integral variables {ΞΉ E : Type*} {ΞΌ : measure ℝ} {l : filter ΞΉ} [is_countably_generated l] [normed_add_comm_group E] [normed_space ℝ E] [complete_space E] {a b : ΞΉ β†’ ℝ} {f : ℝ β†’ E} lemma interval_integral_tendsto_integral (hfi : integrable f ΞΌ) (ha : tendsto a l at_bot) (hb : tendsto b l at_top) : tendsto (Ξ» i, ∫ x in a i .. b i, f x βˆ‚ΞΌ) l (𝓝 $ ∫ x, f x βˆ‚ΞΌ) := begin let Ο† := Ξ» i, Ioc (a i) (b i), have hΟ† : ae_cover ΞΌ l Ο† := ae_cover_Ioc ha hb, refine (hΟ†.integral_tendsto_of_countably_generated hfi).congr' _, filter_upwards [ha.eventually (eventually_le_at_bot 0), hb.eventually (eventually_ge_at_top 0)] with i hai hbi, exact (interval_integral.integral_of_le (hai.trans hbi)).symm end lemma interval_integral_tendsto_integral_Iic (b : ℝ) (hfi : integrable_on f (Iic b) ΞΌ) (ha : tendsto a l at_bot) : tendsto (Ξ» i, ∫ x in a i .. b, f x βˆ‚ΞΌ) l (𝓝 $ ∫ x in Iic b, f x βˆ‚ΞΌ) := begin let Ο† := Ξ» i, Ioi (a i), have hΟ† : ae_cover (ΞΌ.restrict $ Iic b) l Ο† := ae_cover_Ioi ha, refine (hΟ†.integral_tendsto_of_countably_generated hfi).congr' _, filter_upwards [ha.eventually (eventually_le_at_bot $ b)] with i hai, rw [interval_integral.integral_of_le hai, measure.restrict_restrict (hΟ†.measurable i)], refl, end lemma interval_integral_tendsto_integral_Ioi (a : ℝ) (hfi : integrable_on f (Ioi a) ΞΌ) (hb : tendsto b l at_top) : tendsto (Ξ» i, ∫ x in a .. b i, f x βˆ‚ΞΌ) l (𝓝 $ ∫ x in Ioi a, f x βˆ‚ΞΌ) := begin let Ο† := Ξ» i, Iic (b i), have hΟ† : ae_cover (ΞΌ.restrict $ Ioi a) l Ο† := ae_cover_Iic hb, refine (hΟ†.integral_tendsto_of_countably_generated hfi).congr' _, filter_upwards [hb.eventually (eventually_ge_at_top $ a)] with i hbi, rw [interval_integral.integral_of_le hbi, measure.restrict_restrict (hΟ†.measurable i), inter_comm], refl, end end integral_of_interval_integral section Ioi_change_variables open real open_locale interval variables {E : Type*} {ΞΌ : measure ℝ} {f : ℝ β†’ E} [normed_add_comm_group E] [normed_space ℝ E] [complete_space E] /-- Change-of-variables formula for `Ioi` integrals of vector-valued functions, proved by taking limits from the result for finite intervals. -/ lemma integral_comp_smul_deriv_Ioi {f f' : ℝ β†’ ℝ} {g : ℝ β†’ E} {a : ℝ} (hf : continuous_on f $ Ici a) (hft : tendsto f at_top at_top) (hff' : βˆ€ x ∈ Ioi a, has_deriv_within_at f (f' x) (Ioi x) x) (hg_cont : continuous_on g $ f '' Ioi a) (hg1 : integrable_on g $ f '' Ici a) (hg2 : integrable_on (Ξ» x, f' x β€’ (g ∘ f) x) (Ici a)) : ∫ x in Ioi a, f' x β€’ (g ∘ f) x = ∫ u in Ioi (f a), g u := begin have eq : βˆ€ b : ℝ, a < b β†’ ∫ x in a..b, f' x β€’ (g ∘ f) x = ∫ u in f a .. f b, g u, { intros b hb, have i1 : Ioo (min a b) (max a b) βŠ† Ioi a, { rw min_eq_left hb.le, exact Ioo_subset_Ioi_self }, have i2 : [a, b] βŠ† Ici a, { rw interval_of_le hb.le, exact Icc_subset_Ici_self }, refine interval_integral.integral_comp_smul_deriv''' (hf.mono i2) (Ξ» x hx, hff' x $ mem_of_mem_of_subset hx i1) (hg_cont.mono $ image_subset _ _) (hg1.mono_set $ image_subset _ _) (hg2.mono_set i2), { rw min_eq_left hb.le, exact Ioo_subset_Ioi_self }, { rw interval_of_le hb.le, exact Icc_subset_Ici_self } }, rw integrable_on_Ici_iff_integrable_on_Ioi at hg2, have t2 := interval_integral_tendsto_integral_Ioi _ hg2 tendsto_id, have : Ioi (f a) βŠ† f '' Ici a := (Ioi_subset_Ici_self.trans $ is_preconnected.intermediate_value_Ici is_preconnected_Ici left_mem_Ici (le_principal_iff.mpr $ Ici_mem_at_top _) hf hft), have t1 := (interval_integral_tendsto_integral_Ioi _ (hg1.mono_set this) tendsto_id).comp hft, exact tendsto_nhds_unique (tendsto.congr' (eventually_eq_of_mem (Ioi_mem_at_top a) eq) t2) t1, end /-- Change-of-variables formula for `Ioi` integrals of scalar-valued functions -/ lemma integral_comp_mul_deriv_Ioi {f f' : ℝ β†’ ℝ} {g : ℝ β†’ ℝ} {a : ℝ} (hf : continuous_on f $ Ici a) (hft : tendsto f at_top at_top) (hff' : βˆ€ x ∈ Ioi a, has_deriv_within_at f (f' x) (Ioi x) x) (hg_cont : continuous_on g $ f '' Ioi a) (hg1 : integrable_on g $ f '' Ici a) (hg2 : integrable_on (Ξ» x, (g ∘ f) x * f' x) (Ici a)) : ∫ x in Ioi a, (g ∘ f) x * f' x = ∫ u in Ioi (f a), g u := begin have hg2' : integrable_on (Ξ» x, f' x β€’ (g ∘ f) x) (Ici a) := by simpa [mul_comm] using hg2, simpa [mul_comm] using integral_comp_smul_deriv_Ioi hf hft hff' hg_cont hg1 hg2', end /-- Substitution `y = x ^ p` in integrals over `Ioi 0` -/ lemma integral_comp_rpow_Ioi (g : ℝ β†’ E) {p : ℝ} (hp : p β‰  0) : ∫ x in Ioi 0, (|p| * x ^ (p - 1)) β€’ g (x ^ p) = ∫ y in Ioi 0, g y := begin let S := Ioi (0 : ℝ), have a1 : βˆ€ x:ℝ, x ∈ S β†’ has_deriv_within_at (Ξ» (t:ℝ), t ^ p) (p * x ^ (p - 1)) S x := Ξ» x hx, (has_deriv_at_rpow_const (or.inl (mem_Ioi.mp hx).ne')).has_deriv_within_at, have a2 : inj_on (Ξ» x:ℝ, x ^ p) S, { rcases lt_or_gt_of_ne hp, { apply strict_anti_on.inj_on, intros x hx y hy hxy, rw [←inv_lt_inv (rpow_pos_of_pos hx p) (rpow_pos_of_pos hy p), ←rpow_neg (le_of_lt hx), ←rpow_neg (le_of_lt hy)], exact rpow_lt_rpow (le_of_lt hx) hxy (neg_pos.mpr h), }, exact strict_mono_on.inj_on (Ξ» x hx y hy hxy, rpow_lt_rpow (mem_Ioi.mp hx).le hxy h),}, have a3 : (Ξ» (t : ℝ), t ^ p) '' S = S, { ext1, rw mem_image, split, { rintro ⟨y, hy, rfl⟩, exact rpow_pos_of_pos hy p }, { intro hx, refine ⟨x ^ (1 / p), rpow_pos_of_pos hx _, _⟩, rw [←rpow_mul (le_of_lt hx), one_div_mul_cancel hp, rpow_one], } }, have := integral_image_eq_integral_abs_deriv_smul measurable_set_Ioi a1 a2 g, rw a3 at this, rw this, refine set_integral_congr measurable_set_Ioi _, intros x hx, dsimp only, rw [abs_mul, abs_of_nonneg (rpow_nonneg_of_nonneg (le_of_lt hx) _)], end lemma integral_comp_rpow_Ioi_of_pos {g : ℝ β†’ E} {p : ℝ} (hp : 0 < p) : ∫ x in Ioi 0, (p * x ^ (p - 1)) β€’ g (x ^ p) = ∫ y in Ioi 0, g y := begin convert integral_comp_rpow_Ioi g hp.ne', funext, congr, rw abs_of_nonneg hp.le, end end Ioi_change_variables end measure_theory
e2033a2f495a6518420cec66ee05dfa198a8d1fb
a4673261e60b025e2c8c825dfa4ab9108246c32e
/stage0/src/Lean/CoreM.lean
d9d92813f8bb48dc337d69415f246aca4c6ab45d
[ "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
4,511
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Util.RecDepth import Lean.Util.Trace import Lean.Environment import Lean.Exception import Lean.InternalExceptionId import Lean.Eval import Lean.MonadEnv import Lean.ResolveName namespace Lean namespace Core structure State := (env : Environment) (nextMacroScope : MacroScope := firstFrontendMacroScope + 1) (ngen : NameGenerator := {}) (traceState : TraceState := {}) instance : Inhabited State := ⟨{ env := arbitrary }⟩ structure Context := (options : Options := {}) (currRecDepth : Nat := 0) (maxRecDepth : Nat := 1000) (ref : Syntax := Syntax.missing) (currNamespace : Name := Name.anonymous) (openDecls : List OpenDecl := []) abbrev CoreM := ReaderT Context $ StateRefT State (EIO Exception) instance : Inhabited (CoreM Ξ±) := ⟨fun _ _ => throw arbitrary⟩ instance : MonadRef CoreM := { getRef := do let ctx ← read; pure ctx.ref, withRef := fun ref x => withReader (fun ctx => { ctx with ref := ref }) x } instance : MonadEnv CoreM := { getEnv := do pure (← get).env, modifyEnv := fun f => modify fun s => { s with env := f s.env } } instance : MonadOptions CoreM := { getOptions := do pure (← read).options } instance : AddMessageContext CoreM := { addMessageContext := addMessageContextPartial } instance : MonadNameGenerator CoreM := { getNGen := do pure (← get).ngen, setNGen := fun ngen => modify fun s => { s with ngen := ngen } } instance : MonadRecDepth CoreM := { withRecDepth := fun d x => withReader (fun ctx => { ctx with currRecDepth := d }) x, getRecDepth := do pure (← read).currRecDepth, getMaxRecDepth := do pure (← read).maxRecDepth } instance : MonadResolveName CoreM := { getCurrNamespace := do pure (← read).currNamespace, getOpenDecls := do pure (← read).openDecls } @[inline] def liftIOCore (x : IO Ξ±) : CoreM Ξ± := do let ref ← getRef IO.toEIO (fun (err : IO.Error) => Exception.error ref (toString err)) x instance : MonadLift IO CoreM := { monadLift := liftIOCore } instance : MonadTrace CoreM := { getTraceState := do pure (← get).traceState, modifyTraceState := fun f => modify fun s => { s with traceState := f s.traceState } } private def mkFreshNameImp (n : Name) : CoreM Name := do let fresh ← modifyGet fun s => (s.nextMacroScope, { s with nextMacroScope := s.nextMacroScope + 1 }) let env ← getEnv pure $ addMacroScope env.mainModule n fresh def mkFreshUserName [MonadLiftT CoreM m] (n : Name) : m Name := liftM $ mkFreshNameImp n @[inline] def CoreM.run (x : CoreM Ξ±) (ctx : Context) (s : State) : EIO Exception (Ξ± Γ— State) := (x ctx).run s @[inline] def CoreM.run' (x : CoreM Ξ±) (ctx : Context) (s : State) : EIO Exception Ξ± := Prod.fst <$> x.run ctx s @[inline] def CoreM.toIO (x : CoreM Ξ±) (ctx : Context) (s : State) : IO (Ξ± Γ— State) := do match (← (x.run ctx s).toIO') with | Except.error (Exception.error _ msg) => do let e ← msg.toString; throw $ IO.userError e | Except.error (Exception.internal id _) => throw $ IO.userError $ "internal exception #" ++ toString id.idx | Except.ok a => pure a instance [MetaEval Ξ±] : MetaEval (CoreM Ξ±) := { eval := fun env opts x _ => do let x : CoreM Ξ± := do try x finally printTraces let (a, s) ← x.toIO { maxRecDepth := getMaxRecDepth opts, options := opts } { env := env } MetaEval.eval s.env opts a (hideUnit := true) } -- withIncRecDepth for a monad `m` such that `[MonadControlT CoreM n]` protected def withIncRecDepth [Monad m] [MonadControlT CoreM m] (x : m Ξ±) : m Ξ± := controlAt CoreM fun runInBase => withIncRecDepth (runInBase x) end Core export Core (CoreM mkFreshUserName) @[inline] def catchInternalId [Monad m] [MonadExcept Exception m] (id : InternalExceptionId) (x : m Ξ±) (h : Exception β†’ m Ξ±) : m Ξ± := do try x catch ex => match ex with | Exception.error _ _ => throw ex | Exception.internal id' _ => if id == id' then h ex else throw ex @[inline] def catchInternalIds [Monad m] [MonadExcept Exception m] (ids : List InternalExceptionId) (x : m Ξ±) (h : Exception β†’ m Ξ±) : m Ξ± := do try x catch ex => match ex with | Exception.error _ _ => throw ex | Exception.internal id _ => if ids.contains id then h ex else throw ex end Lean
53739bd0ddbadf0bb9192b1241b3bc2c1718ceb5
510e96af568b060ed5858226ad954c258549f143
/data/nat/sub.lean
fb7a836f37eae1da10a3c70503e606785392cc03
[]
no_license
Shamrock-Frost/library_dev
cb6d1739237d81e17720118f72ba0a6db8a5906b
0245c71e4931d3aceeacf0aea776454f6ee03c9c
refs/heads/master
1,609,481,034,595
1,500,165,215,000
1,500,165,347,000
97,350,162
0
0
null
1,500,164,969,000
1,500,164,969,000
null
UTF-8
Lean
false
false
6,258
lean
/- Copyright (c) 2014 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Jeremy Avigad Subtraction on the natural numbers, as well as min, max, and distance. -/ namespace nat /- interaction with inequalities -/ protected theorem le_sub_add (n m : β„•) : n ≀ n - m + m := or.elim (le_total n m) (suppose n ≀ m, begin rw [sub_eq_zero_of_le this, zero_add], exact this end) (suppose m ≀ n, begin rw (nat.sub_add_cancel this) end) protected theorem sub_eq_of_eq_add {n m k : β„•} (h : k = n + m) : k - n = m := begin rw [h, nat.add_sub_cancel_left] end protected theorem lt_of_sub_pos {m n : β„•} (h : n - m > 0) : m < n := lt_of_not_ge (suppose m β‰₯ n, have n - m = 0, from sub_eq_zero_of_le this, begin rw this at h, exact lt_irrefl _ h end) protected theorem lt_of_sub_lt_sub_right {n m k : β„•} (h : n - k < m - k) : n < m := lt_of_not_ge (suppose m ≀ n, have m - k ≀ n - k, from nat.sub_le_sub_right this _, not_le_of_gt h this) protected theorem lt_of_sub_lt_sub_left {n m k : β„•} (h : n - m < n - k) : k < m := lt_of_not_ge (suppose m ≀ k, have n - k ≀ n - m, from nat.sub_le_sub_left _ this, not_le_of_gt h this) protected theorem sub_lt_self {m n : β„•} (h₁ : m > 0) (hβ‚‚ : n > 0) : m - n < m := calc m - n = succ (pred m) - succ (pred n) : by rw [succ_pred_eq_of_pos h₁, succ_pred_eq_of_pos hβ‚‚] ... = pred m - pred n : by rw succ_sub_succ ... ≀ pred m : sub_le _ _ ... < succ (pred m) : lt_succ_self _ ... = m : succ_pred_eq_of_pos h₁ protected theorem le_sub_of_add_le {m n k : β„•} (h : m + k ≀ n) : m ≀ n - k := calc m = m + k - k : by rw nat.add_sub_cancel ... ≀ n - k : nat.sub_le_sub_right h k protected theorem lt_sub_of_add_lt {m n k : β„•} (h : m + k < n) : m < n - k := lt_of_succ_le (nat.le_sub_of_add_le (calc succ m + k = succ (m + k) : by rw succ_add ... ≀ n : succ_le_of_lt h)) protected theorem add_lt_of_lt_sub {m n k : β„•} (h : m < n - k) : m + k < n := @nat.lt_of_sub_lt_sub_right _ _ k (by rwa nat.add_sub_cancel) protected theorem sub_lt_of_lt_add {k n m : nat} (h₁ : k < n + m) (hβ‚‚ : n ≀ k) : k - n < m := have succ k ≀ n + m, from succ_le_of_lt h₁, have succ (k - n) ≀ m, from calc succ (k - n) = succ k - n : by rw (succ_sub hβ‚‚) ... ≀ n + m - n : nat.sub_le_sub_right this n ... = m : by rw nat.add_sub_cancel_left, lt_of_succ_le this /- distance -/ definition dist (n m : β„•) := (n - m) + (m - n) theorem dist.def (n m : β„•) : dist n m = (n - m) + (m - n) := rfl @[simp] theorem dist_comm (n m : β„•) : dist n m = dist m n := by simp [dist.def] @[simp] theorem dist_self (n : β„•) : dist n n = 0 := by simp [dist.def, nat.sub_self] theorem eq_of_dist_eq_zero {n m : β„•} (h : dist n m = 0) : n = m := have n - m = 0, from eq_zero_of_add_eq_zero_right h, have n ≀ m, from nat.le_of_sub_eq_zero this, have m - n = 0, from eq_zero_of_add_eq_zero_left h, have m ≀ n, from nat.le_of_sub_eq_zero this, le_antisymm β€Ήn ≀ mβ€Ί β€Ήm ≀ nβ€Ί theorem dist_eq_zero {n m : β„•} (h : n = m) : dist n m = 0 := begin rw [h, dist_self] end theorem dist_eq_sub_of_le {n m : β„•} (h : n ≀ m) : dist n m = m - n := begin rw [dist.def, sub_eq_zero_of_le h, zero_add] end theorem dist_eq_sub_of_ge {n m : β„•} (h : n β‰₯ m) : dist n m = n - m := begin rw [dist_comm], apply dist_eq_sub_of_le h end theorem dist_zero_right (n : β„•) : dist n 0 = n := eq.trans (dist_eq_sub_of_ge (zero_le n)) (nat.sub_zero n) theorem dist_zero_left (n : β„•) : dist 0 n = n := eq.trans (dist_eq_sub_of_le (zero_le n)) (nat.sub_zero n) theorem dist_add_add_right (n k m : β„•) : dist (n + k) (m + k) = dist n m := calc dist (n + k) (m + k) = ((n + k) - (m + k)) + ((m + k)-(n + k)) : rfl ... = (n - m) + ((m + k) - (n + k)) : by rw nat.add_sub_add_right ... = (n - m) + (m - n) : by rw nat.add_sub_add_right theorem dist_add_add_left (k n m : β„•) : dist (k + n) (k + m) = dist n m := begin rw [add_comm k n, add_comm k m], apply dist_add_add_right end theorem dist_eq_intro {n m k l : β„•} (h : n + m = k + l) : dist n k = dist l m := calc dist n k = dist (n + m) (k + m) : by rw dist_add_add_right ... = dist (k + l) (k + m) : by rw h ... = dist l m : by rw dist_add_add_left protected theorem sub_lt_sub_add_sub (n m k : β„•) : n - k ≀ (n - m) + (m - k) := or.elim (le_total k m) (suppose k ≀ m, begin rw ←nat.add_sub_assoc this, apply nat.sub_le_sub_right, apply nat.le_sub_add end) (suppose k β‰₯ m, begin rw [sub_eq_zero_of_le this, add_zero], apply nat.sub_le_sub_left, exact this end) theorem dist.triangle_inequality (n m k : β„•) : dist n k ≀ dist n m + dist m k := have dist n m + dist m k = (n - m) + (m - k) + ((k - m) + (m - n)), by simp [dist.def], begin rw [this, dist.def], apply add_le_add, repeat { apply nat.sub_lt_sub_add_sub } end theorem dist_mul_right (n k m : β„•) : dist (n * k) (m * k) = dist n m * k := by rw [dist.def, dist.def, right_distrib, nat.mul_sub_right_distrib, nat.mul_sub_right_distrib] theorem dist_mul_left (k n m : β„•) : dist (k * n) (k * m) = k * dist n m := by rw [mul_comm k n, mul_comm k m, dist_mul_right, mul_comm] -- TODO(Jeremy): do when we have max and minx --lemma dist_eq_max_sub_min {i j : nat} : dist i j = (max i j) - min i j := --sorry /- or.elim (lt_or_ge i j) (suppose i < j, by rw [max_eq_right_of_lt this, min_eq_left_of_lt this, dist_eq_sub_of_lt this]) (suppose i β‰₯ j, by rw [max_eq_left this , min_eq_right this, dist_eq_sub_of_ge this]) -/ lemma dist_succ_succ {i j : nat} : dist (succ i) (succ j) = dist i j := by simp [dist.def, succ_sub_succ] lemma dist_pos_of_ne {i j : nat} : i β‰  j β†’ dist i j > 0 := assume hne, nat.lt_by_cases (suppose i < j, begin rw [dist_eq_sub_of_le (le_of_lt this)], apply nat.sub_pos_of_lt this end) (suppose i = j, by contradiction) (suppose i > j, begin rw [dist_eq_sub_of_ge (le_of_lt this)], apply nat.sub_pos_of_lt this end) end nat
3f7282b89029a1de943e7d3c050291b568759799
2a70b774d16dbdf5a533432ee0ebab6838df0948
/_target/deps/mathlib/src/data/list/sort.lean
14c930c1c7ff9f90a31c7010e5ce4e7888751007
[ "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
13,206
lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Jeremy Avigad -/ import data.list.perm import data.list.chain /-! # Sorting algorithms on lists In this file we define `list.sorted r l` to be an alias for `pairwise r l`. This alias is preferred in the case that `r` is a `<` or `≀`-like relation. Then we define two sorting algorithms: `list.insertion_sort` and `list.merge_sort`, and prove their correctness. -/ open list.perm namespace list /-! ### The predicate `list.sorted` -/ section sorted universe variable uu variables {Ξ± : Type uu} {r : Ξ± β†’ Ξ± β†’ Prop} /-- `sorted r l` is the same as `pairwise r l`, preferred in the case that `r` is a `<` or `≀`-like relation (transitive and antisymmetric or asymmetric) -/ def sorted := @pairwise instance decidable_sorted [decidable_rel r] (l : list Ξ±) : decidable (sorted r l) := list.decidable_pairwise _ @[simp] theorem sorted_nil : sorted r [] := pairwise.nil theorem sorted_of_sorted_cons {a : Ξ±} {l : list Ξ±} : sorted r (a :: l) β†’ sorted r l := pairwise_of_pairwise_cons theorem sorted.tail {r : Ξ± β†’ Ξ± β†’ Prop} {l : list Ξ±} (h : sorted r l) : sorted r l.tail := h.tail theorem rel_of_sorted_cons {a : Ξ±} {l : list Ξ±} : sorted r (a :: l) β†’ βˆ€ b ∈ l, r a b := rel_of_pairwise_cons @[simp] theorem sorted_cons {a : Ξ±} {l : list Ξ±} : sorted r (a :: l) ↔ (βˆ€ b ∈ l, r a b) ∧ sorted r l := pairwise_cons protected theorem sorted.nodup {r : Ξ± β†’ Ξ± β†’ Prop} [is_irrefl Ξ± r] {l : list Ξ±} (h : sorted r l) : nodup l := h.nodup theorem eq_of_perm_of_sorted [is_antisymm Ξ± r] {l₁ lβ‚‚ : list Ξ±} (p : l₁ ~ lβ‚‚) (s₁ : sorted r l₁) (sβ‚‚ : sorted r lβ‚‚) : l₁ = lβ‚‚ := begin induction s₁ with a l₁ h₁ s₁ IH generalizing lβ‚‚, { exact p.nil_eq }, { have : a ∈ lβ‚‚ := p.subset (mem_cons_self _ _), rcases mem_split this with ⟨uβ‚‚, vβ‚‚, rfl⟩, have p' := (perm_cons a).1 (p.trans perm_middle), have := IH p' (pairwise_of_sublist (by simp) sβ‚‚), subst l₁, change a::uβ‚‚ ++ vβ‚‚ = uβ‚‚ ++ ([a] ++ vβ‚‚), rw ← append_assoc, congr, have : βˆ€ (x : Ξ±) (h : x ∈ uβ‚‚), x = a := Ξ» x m, antisymm ((pairwise_append.1 sβ‚‚).2.2 _ m a (mem_cons_self _ _)) (h₁ _ (by simp [m])), rw [(@eq_repeat _ a (length uβ‚‚ + 1) (a::uβ‚‚)).2, (@eq_repeat _ a (length uβ‚‚ + 1) (uβ‚‚++[a])).2]; split; simp [iff_true_intro this, or_comm] } end @[simp] theorem sorted_singleton (a : Ξ±) : sorted r [a] := pairwise_singleton _ _ lemma nth_le_of_sorted_of_le [is_refl Ξ± r] {l : list Ξ±} (h : l.sorted r) {a b : β„•} {ha : a < l.length} {hb : b < l.length} (hab : a ≀ b) : r (l.nth_le a ha) (l.nth_le b hb) := begin cases eq_or_lt_of_le hab with H H, { induction H, exact refl _ }, { exact list.pairwise_iff_nth_le.1 h a b hb H } end end sorted section sort universe variable uu variables {Ξ± : Type uu} (r : Ξ± β†’ Ξ± β†’ Prop) [decidable_rel r] local infix ` β‰Ό ` : 50 := r /-! ### Insertion sort -/ section insertion_sort /-- `ordered_insert a l` inserts `a` into `l` at such that `ordered_insert a l` is sorted if `l` is. -/ @[simp] def ordered_insert (a : Ξ±) : list Ξ± β†’ list Ξ± | [] := [a] | (b :: l) := if a β‰Ό b then a :: b :: l else b :: ordered_insert l /-- `insertion_sort l` returns `l` sorted using the insertion sort algorithm. -/ @[simp] def insertion_sort : list Ξ± β†’ list Ξ± | [] := [] | (b :: l) := ordered_insert r b (insertion_sort l) @[simp] lemma ordered_insert_nil (a : Ξ±) : [].ordered_insert r a = [a] := rfl theorem ordered_insert_length : Ξ  (L : list Ξ±) (a : Ξ±), (L.ordered_insert r a).length = L.length + 1 | [] a := rfl | (hd :: tl) a := by { dsimp [ordered_insert], split_ifs; simp [ordered_insert_length], } /-- An alternative definition of `ordered_insert` using `take_while` and `drop_while`. -/ lemma ordered_insert_eq_take_drop (a : Ξ±) : βˆ€ l : list Ξ±, l.ordered_insert r a = l.take_while (Ξ» b, Β¬(a β‰Ό b)) ++ (a :: l.drop_while (Ξ» b, Β¬(a β‰Ό b))) | [] := rfl | (b :: l) := by { dsimp only [ordered_insert], split_ifs; simp [take_while, drop_while, *] } lemma insertion_sort_cons_eq_take_drop (a : Ξ±) (l : list Ξ±) : insertion_sort r (a :: l) = (insertion_sort r l).take_while (Ξ» b, Β¬(a β‰Ό b)) ++ (a :: (insertion_sort r l).drop_while (Ξ» b, Β¬(a β‰Ό b))) := ordered_insert_eq_take_drop r a _ section correctness open perm theorem perm_ordered_insert (a) : βˆ€ l : list Ξ±, ordered_insert r a l ~ a :: l | [] := perm.refl _ | (b :: l) := by by_cases a β‰Ό b; [simp [ordered_insert, h], simpa [ordered_insert, h] using ((perm_ordered_insert l).cons _).trans (perm.swap _ _ _)] theorem ordered_insert_count [decidable_eq Ξ±] (L : list Ξ±) (a b : Ξ±) : count a (L.ordered_insert r b) = count a L + if (a = b) then 1 else 0 := begin rw [(L.perm_ordered_insert r b).count_eq, count_cons], split_ifs; simp only [nat.succ_eq_add_one, add_zero], end theorem perm_insertion_sort : βˆ€ l : list Ξ±, insertion_sort r l ~ l | [] := perm.nil | (b :: l) := by simpa [insertion_sort] using (perm_ordered_insert _ _ _).trans ((perm_insertion_sort l).cons b) variable {r} /-- If `l` is already `list.sorted` with respect to `r`, then `insertion_sort` does not change it. -/ lemma sorted.insertion_sort_eq : βˆ€ {l : list Ξ±} (h : sorted r l), insertion_sort r l = l | [] _ := rfl | [a] _ := rfl | (a :: b :: l) h := begin rw [insertion_sort, sorted.insertion_sort_eq, ordered_insert, if_pos], exacts [rel_of_sorted_cons h _ (or.inl rfl), h.tail] end section total_and_transitive variables [is_total Ξ± r] [is_trans Ξ± r] theorem sorted.ordered_insert (a : Ξ±) : βˆ€ l, sorted r l β†’ sorted r (ordered_insert r a l) | [] h := sorted_singleton a | (b :: l) h := begin by_cases h' : a β‰Ό b, { simpa [ordered_insert, h', h] using Ξ» b' bm, trans h' (rel_of_sorted_cons h _ bm) }, { suffices : βˆ€ (b' : Ξ±), b' ∈ ordered_insert r a l β†’ r b b', { simpa [ordered_insert, h', (sorted_of_sorted_cons h).ordered_insert l] }, intros b' bm, cases (show b' = a ∨ b' ∈ l, by simpa using (perm_ordered_insert _ _ _).subset bm) with be bm, { subst b', exact (total_of r _ _).resolve_left h' }, { exact rel_of_sorted_cons h _ bm } } end variable (r) /-- The list `list.insertion_sort r l` is `list.sorted` with respect to `r`. -/ theorem sorted_insertion_sort : βˆ€ l, sorted r (insertion_sort r l) | [] := sorted_nil | (a :: l) := (sorted_insertion_sort l).ordered_insert a _ end total_and_transitive end correctness end insertion_sort /-! ### Merge sort -/ section merge_sort -- TODO(Jeremy): observation: if instead we write (a :: (split l).1, b :: (split l).2), the -- equation compiler can't prove the third equation /-- Split `l` into two lists of approximately equal length. split [1, 2, 3, 4, 5] = ([1, 3, 5], [2, 4]) -/ @[simp] def split : list Ξ± β†’ list Ξ± Γ— list Ξ± | [] := ([], []) | (a :: l) := let (l₁, lβ‚‚) := split l in (a :: lβ‚‚, l₁) theorem split_cons_of_eq (a : Ξ±) {l l₁ lβ‚‚ : list Ξ±} (h : split l = (l₁, lβ‚‚)) : split (a :: l) = (a :: lβ‚‚, l₁) := by rw [split, h]; refl theorem length_split_le : βˆ€ {l l₁ lβ‚‚ : list Ξ±}, split l = (l₁, lβ‚‚) β†’ length l₁ ≀ length l ∧ length lβ‚‚ ≀ length l | [] ._ ._ rfl := ⟨nat.le_refl 0, nat.le_refl 0⟩ | (a::l) l₁' lβ‚‚' h := begin cases e : split l with l₁ lβ‚‚, injection (split_cons_of_eq _ e).symm.trans h, substs l₁' lβ‚‚', cases length_split_le e with h₁ hβ‚‚, exact ⟨nat.succ_le_succ hβ‚‚, nat.le_succ_of_le hβ‚βŸ© end theorem length_split_lt {a b} {l l₁ lβ‚‚ : list Ξ±} (h : split (a::b::l) = (l₁, lβ‚‚)) : length l₁ < length (a::b::l) ∧ length lβ‚‚ < length (a::b::l) := begin cases e : split l with l₁' lβ‚‚', injection (split_cons_of_eq _ (split_cons_of_eq _ e)).symm.trans h, substs l₁ lβ‚‚, cases length_split_le e with h₁ hβ‚‚, exact ⟨nat.succ_le_succ (nat.succ_le_succ h₁), nat.succ_le_succ (nat.succ_le_succ hβ‚‚)⟩ end theorem perm_split : βˆ€ {l l₁ lβ‚‚ : list Ξ±}, split l = (l₁, lβ‚‚) β†’ l ~ l₁ ++ lβ‚‚ | [] ._ ._ rfl := perm.refl _ | (a::l) l₁' lβ‚‚' h := begin cases e : split l with l₁ lβ‚‚, injection (split_cons_of_eq _ e).symm.trans h, substs l₁' lβ‚‚', exact ((perm_split e).trans perm_append_comm).cons a, end /-- Merge two sorted lists into one in linear time. merge [1, 2, 4, 5] [0, 1, 3, 4] = [0, 1, 1, 2, 3, 4, 4, 5] -/ def merge : list Ξ± β†’ list Ξ± β†’ list Ξ± | [] l' := l' | l [] := l | (a :: l) (b :: l') := if a β‰Ό b then a :: merge l (b :: l') else b :: merge (a :: l) l' include r /-- Implementation of a merge sort algorithm to sort a list. -/ def merge_sort : list Ξ± β†’ list Ξ± | [] := [] | [a] := [a] | (a::b::l) := begin cases e : split (a::b::l) with l₁ lβ‚‚, cases length_split_lt e with h₁ hβ‚‚, exact merge r (merge_sort l₁) (merge_sort lβ‚‚) end using_well_founded { rel_tac := Ξ»_ _, `[exact ⟨_, inv_image.wf length nat.lt_wf⟩], dec_tac := tactic.assumption } theorem merge_sort_cons_cons {a b} {l l₁ lβ‚‚ : list Ξ±} (h : split (a::b::l) = (l₁, lβ‚‚)) : merge_sort r (a::b::l) = merge r (merge_sort r l₁) (merge_sort r lβ‚‚) := begin suffices : βˆ€ (L : list Ξ±) h1, @@and.rec (Ξ» a a (_ : length l₁ < length l + 1 + 1 ∧ length lβ‚‚ < length l + 1 + 1), L) h1 h1 = L, { simp [merge_sort, h], apply this }, intros, cases h1, refl end section correctness theorem perm_merge : βˆ€ (l l' : list Ξ±), merge r l l' ~ l ++ l' | [] [] := perm.nil | [] (b :: l') := by simp [merge] | (a :: l) [] := by simp [merge] | (a :: l) (b :: l') := begin by_cases a β‰Ό b, { simpa [merge, h] using perm_merge _ _ }, { suffices : b :: merge r (a :: l) l' ~ a :: (l ++ b :: l'), {simpa [merge, h]}, exact ((perm_merge _ _).cons _).trans ((swap _ _ _).trans (perm_middle.symm.cons _)) } end theorem perm_merge_sort : βˆ€ l : list Ξ±, merge_sort r l ~ l | [] := perm.refl _ | [a] := perm.refl _ | (a::b::l) := begin cases e : split (a::b::l) with l₁ lβ‚‚, cases length_split_lt e with h₁ hβ‚‚, rw [merge_sort_cons_cons r e], apply (perm_merge r _ _).trans, exact ((perm_merge_sort l₁).append (perm_merge_sort lβ‚‚)).trans (perm_split e).symm end using_well_founded { rel_tac := Ξ»_ _, `[exact ⟨_, inv_image.wf length nat.lt_wf⟩], dec_tac := tactic.assumption } @[simp] lemma length_merge_sort (l : list Ξ±) : (merge_sort r l).length = l.length := (perm_merge_sort r _).length_eq section total_and_transitive variables {r} [is_total Ξ± r] [is_trans Ξ± r] theorem sorted.merge : βˆ€ {l l' : list Ξ±}, sorted r l β†’ sorted r l' β†’ sorted r (merge r l l') | [] [] h₁ hβ‚‚ := sorted_nil | [] (b :: l') h₁ hβ‚‚ := by simpa [merge] using hβ‚‚ | (a :: l) [] h₁ hβ‚‚ := by simpa [merge] using h₁ | (a :: l) (b :: l') h₁ hβ‚‚ := begin by_cases a β‰Ό b, { suffices : βˆ€ (b' : Ξ±) (_ : b' ∈ merge r l (b :: l')), r a b', { simpa [merge, h, (sorted_of_sorted_cons h₁).merge hβ‚‚] }, intros b' bm, rcases (show b' = b ∨ b' ∈ l ∨ b' ∈ l', by simpa [or.left_comm] using (perm_merge _ _ _).subset bm) with be | bl | bl', { subst b', assumption }, { exact rel_of_sorted_cons h₁ _ bl }, { exact trans h (rel_of_sorted_cons hβ‚‚ _ bl') } }, { suffices : βˆ€ (b' : Ξ±) (_ : b' ∈ merge r (a :: l) l'), r b b', { simpa [merge, h, h₁.merge (sorted_of_sorted_cons hβ‚‚)] }, intros b' bm, have ba : b β‰Ό a := (total_of r _ _).resolve_left h, rcases (show b' = a ∨ b' ∈ l ∨ b' ∈ l', by simpa using (perm_merge _ _ _).subset bm) with be | bl | bl', { subst b', assumption }, { exact trans ba (rel_of_sorted_cons h₁ _ bl) }, { exact rel_of_sorted_cons hβ‚‚ _ bl' } } end variable (r) theorem sorted_merge_sort : βˆ€ l : list Ξ±, sorted r (merge_sort r l) | [] := sorted_nil | [a] := sorted_singleton _ | (a::b::l) := begin cases e : split (a::b::l) with l₁ lβ‚‚, cases length_split_lt e with h₁ hβ‚‚, rw [merge_sort_cons_cons r e], exact (sorted_merge_sort l₁).merge (sorted_merge_sort lβ‚‚) end using_well_founded { rel_tac := Ξ»_ _, `[exact ⟨_, inv_image.wf length nat.lt_wf⟩], dec_tac := tactic.assumption } theorem merge_sort_eq_self [is_antisymm Ξ± r] {l : list Ξ±} : sorted r l β†’ merge_sort r l = l := eq_of_perm_of_sorted (perm_merge_sort _ _) (sorted_merge_sort _ _) theorem merge_sort_eq_insertion_sort [is_antisymm Ξ± r] (l : list Ξ±) : merge_sort r l = insertion_sort r l := eq_of_perm_of_sorted ((perm_merge_sort r l).trans (perm_insertion_sort r l).symm) (sorted_merge_sort r l) (sorted_insertion_sort r l) end total_and_transitive end correctness end merge_sort end sort /- try them out! -/ --#eval insertion_sort (Ξ» m n : β„•, m ≀ n) [5, 27, 221, 95, 17, 43, 7, 2, 98, 567, 23, 12] --#eval merge_sort (Ξ» m n : β„•, m ≀ n) [5, 27, 221, 95, 17, 43, 7, 2, 98, 567, 23, 12] end list
d1544727d381534fc86c80b74165b747fb8b6663
0c6b99c0daded009943e570c13367c8cc7d3bcae
/chapter16.lean
bb2bc62a5695bf3d5f8ae4809d74560eeb559cb3
[]
no_license
TateKennington/logic-and-proof-exercises
e3c5c5b12b6238b47b0c5acf8717923bd471a535
acca8882026e7b643453eb096d3021cd043005bd
refs/heads/main
1,687,638,489,616
1,626,858,474,000
1,626,858,474,000
371,871,374
4
0
null
null
null
null
UTF-8
Lean
false
false
1,403
lean
import data.set data.int.basic data.set open function int set section def g (x : β„€) : β„€ := -x def h (x : β„€) : β„€ := 2 * x + 3 example : injective h := assume x₁ xβ‚‚, assume :2 * x₁ + 3 = 2 * xβ‚‚ + 3, have 2 * x₁ = 2 * xβ‚‚, from add_right_cancel this, show x₁ = xβ‚‚, from mul_left_cancel' (dec_trivial) this example : surjective g := assume y, have g (-y) = y, from calc g (-y) = -(-y): rfl ... = y: neg_neg y, exists.intro (-y) this example (A B : Type) (u : A β†’ B) (v1 : B β†’ A) (v2 : B β†’ A) (h1 : left_inverse v1 u) (h2 : right_inverse v2 u) : v1 = v2 := funext (assume x, calc v1 x = v1 (u (v2 x)) : by rw h2 ... = v2 x : by rw h1) end section variables {X Y : Type} variable f : X β†’ Y variables A B : set X example : f '' (A ∩ B) βŠ† f '' A ∩ f '' B := assume y, assume h1 : y ∈ f '' (A ∩ B), show y ∈ f '' A ∩ f '' B, from exists.elim h1 $ assume x h, have feq: f x = y, from and.right h, have x ∈ A ∩ B, from and.left h, have x ∈ A, from and.left β€Ήx ∈ A ∩ Bβ€Ί, have y ∈ f '' A, from exists.intro x $ ⟨this, feq⟩, have x ∈ B, from and.right β€Ήx ∈ A ∩ Bβ€Ί, have y ∈ f '' B, from exists.intro x $ ⟨this, feq⟩, βŸ¨β€Ήy ∈ f '' Aβ€Ί, β€Ήy ∈ f '' Bβ€ΊβŸ© end
4e2e15d8db1d88502b83f944b5e362fdf787f272
d6124c8dbe5661dcc5b8c9da0a56fbf1f0480ad6
/Papyrus/IR/AddressSpace.lean
c3500e0edec08752d24c4b3aa9b76cf87bf9e7e0
[ "Apache-2.0" ]
permissive
xubaiw/lean4-papyrus
c3fbbf8ba162eb5f210155ae4e20feb2d32c8182
02e82973a5badda26fc0f9fd15b3d37e2eb309e0
refs/heads/master
1,691,425,756,824
1,632,122,825,000
1,632,123,075,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
739
lean
namespace Papyrus /-- A numerically indexed address space. -/ structure AddressSpace where index : UInt32 deriving BEq, Repr namespace AddressSpace /-- The default address space (i.e., 0). -/ def default := mk 0 /-- Make an address space from a `Nat`. -/ def ofNat (n : Nat) := mk n.toUInt32 /-- Make an address space from a `UInt32`. -/ def ofUInt32 (n : UInt32) := mk n /-- Convert an address space tp a `Nat`. -/ def toNat (self : AddressSpace) : Nat := self.index.toNat /-- Convert an address space to a `UInt32`. -/ def toUInt32 (self : AddressSpace) : UInt32 := self.index end AddressSpace instance : Inhabited AddressSpace := ⟨AddressSpace.default⟩ instance {n} : OfNat AddressSpace n := ⟨AddressSpace.ofNat n⟩
3d5a7450cbd283cabf2c1a5baf204c04eda7979e
4efff1f47634ff19e2f786deadd394270a59ecd2
/src/data/nat/choose.lean
5426c254164935a3056a4ad889c20472b05f26d6
[ "Apache-2.0" ]
permissive
agjftucker/mathlib
d634cd0d5256b6325e3c55bb7fb2403548371707
87fe50de17b00af533f72a102d0adefe4a2285e8
refs/heads/master
1,625,378,131,941
1,599,166,526,000
1,599,166,526,000
160,748,509
0
0
Apache-2.0
1,544,141,789,000
1,544,141,789,000
null
UTF-8
Lean
false
false
6,297
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Bhavik Mehta, Patrick Stevens -/ import tactic.linarith import tactic.omega import algebra.big_operators.ring import algebra.big_operators.intervals import algebra.big_operators.order open nat open_locale big_operators lemma nat.prime.dvd_choose_add {p a b : β„•} (hap : a < p) (hbp : b < p) (h : p ≀ a + b) (hp : prime p) : p ∣ choose (a + b) a := have h₁ : p ∣ fact (a + b), from hp.dvd_fact.2 h, have hβ‚‚ : Β¬p ∣ fact a, from mt hp.dvd_fact.1 (not_le_of_gt hap), have h₃ : Β¬p ∣ fact b, from mt hp.dvd_fact.1 (not_le_of_gt hbp), by rw [← choose_mul_fact_mul_fact (le.intro rfl), mul_assoc, hp.dvd_mul, hp.dvd_mul, norm_num.sub_nat_pos (a + b) a b rfl] at h₁; exact h₁.resolve_right (not_or_distrib.2 ⟨hβ‚‚, hβ‚ƒβŸ©) lemma nat.prime.dvd_choose_self {p k : β„•} (hk : 0 < k) (hkp : k < p) (hp : prime p) : p ∣ choose p k := begin have r : k + (p - k) = p, by rw [← nat.add_sub_assoc (nat.le_of_lt hkp) k, nat.add_sub_cancel_left], have e : p ∣ choose (k + (p - k)) k, by exact nat.prime.dvd_choose_add hkp (sub_lt (lt.trans hk hkp) hk) (by rw r) hp, rwa r at e, end /-- Show that choose is increasing for small values of the right argument. -/ lemma choose_le_succ_of_lt_half_left {r n : β„•} (h : r < n/2) : choose n r ≀ choose n (r+1) := begin refine le_of_mul_le_mul_right _ (nat.lt_sub_left_of_add_lt (lt_of_lt_of_le h (nat.div_le_self n 2))), rw ← choose_succ_right_eq, apply nat.mul_le_mul_left, rw [← nat.lt_iff_add_one_le, nat.lt_sub_left_iff_add_lt, ← mul_two], exact lt_of_lt_of_le (mul_lt_mul_of_pos_right h zero_lt_two) (nat.div_mul_le_self n 2), end /-- Show that for small values of the right argument, the middle value is largest. -/ private lemma choose_le_middle_of_le_half_left {n r : β„•} (hr : r ≀ n/2) : choose n r ≀ choose n (n/2) := decreasing_induction (Ξ» _ k a, (eq_or_lt_of_le a).elim (Ξ» t, t.symm β–Έ le_refl _) (Ξ» h, trans (choose_le_succ_of_lt_half_left h) (k h))) hr (Ξ» _, le_refl _) hr /-- `choose n r` is maximised when `r` is `n/2`. -/ lemma choose_le_middle (r n : β„•) : nat.choose n r ≀ nat.choose n (n/2) := begin cases le_or_gt r n with b b, { cases le_or_lt r (n/2) with a h, { apply choose_le_middle_of_le_half_left a }, { rw ← choose_symm b, apply choose_le_middle_of_le_half_left, rw [div_lt_iff_lt_mul' zero_lt_two] at h, rw [le_div_iff_mul_le' zero_lt_two, nat.mul_sub_right_distrib, nat.sub_le_iff, mul_two, nat.add_sub_cancel], exact le_of_lt h } }, { rw nat.choose_eq_zero_of_lt b, apply nat.zero_le } end section binomial open finset variables {Ξ± : Type*} /-- A version of the binomial theorem for noncommutative semirings. -/ theorem commute.add_pow [semiring Ξ±] {x y : Ξ±} (h : commute x y) (n : β„•) : (x + y) ^ n = βˆ‘ m in range (n + 1), x ^ m * y ^ (n - m) * choose n m := begin let t : β„• β†’ β„• β†’ Ξ± := Ξ» n m, x ^ m * (y ^ (n - m)) * (choose n m), change (x + y) ^ n = βˆ‘ m in range (n + 1), t n m, have h_first : βˆ€ n, t n 0 = y ^ n := Ξ» n, by { dsimp [t], rw[choose_zero_right, nat.cast_one, mul_one, one_mul] }, have h_last : βˆ€ n, t n n.succ = 0 := Ξ» n, by { dsimp [t], rw [choose_succ_self, nat.cast_zero, mul_zero] }, have h_middle : βˆ€ (n i : β„•), (i ∈ finset.range n.succ) β†’ ((t n.succ) ∘ nat.succ) i = x * (t n i) + y * (t n i.succ) := begin intros n i h_mem, have h_le : i ≀ n := nat.le_of_lt_succ (finset.mem_range.mp h_mem), dsimp [t], rw [choose_succ_succ, nat.cast_add, mul_add], congr' 1, { rw[pow_succ x, succ_sub_succ, mul_assoc, mul_assoc, mul_assoc] }, { rw[← mul_assoc y, ← mul_assoc y, (h.symm.pow_right i.succ).eq], by_cases h_eq : i = n, { rw [h_eq, choose_succ_self, nat.cast_zero, mul_zero, mul_zero] }, { rw[succ_sub (lt_of_le_of_ne h_le h_eq)], rw[pow_succ y, mul_assoc, mul_assoc, mul_assoc, mul_assoc] } } end, induction n with n ih, { rw [pow_zero, sum_range_succ, range_zero, sum_empty, add_zero], dsimp [t], rw [choose_self, nat.cast_one, mul_one, mul_one] }, { rw[sum_range_succ', h_first], rw[finset.sum_congr rfl (h_middle n), finset.sum_add_distrib, add_assoc], rw[pow_succ (x + y), ih, add_mul, finset.mul_sum, finset.mul_sum], congr' 1, rw[finset.sum_range_succ', finset.sum_range_succ, h_first, h_last, mul_zero, zero_add, pow_succ] } end /-- The binomial theorem-/ theorem add_pow [comm_semiring Ξ±] (x y : Ξ±) (n : β„•) : (x + y) ^ n = βˆ‘ m in range (n + 1), x ^ m * y ^ (n - m) * choose n m := (commute.all x y).add_pow n /-- The sum of entries in a row of Pascal's triangle -/ theorem sum_range_choose (n : β„•) : βˆ‘ m in range (n + 1), choose n m = 2 ^ n := by simpa using (add_pow 1 1 n).symm /-! # Specific facts about binomial coefficients and their sums -/ lemma sum_range_choose_halfway (m : nat) : βˆ‘ i in range (m + 1), nat.choose (2 * m + 1) i = 4 ^ m := have βˆ‘ i in range (m + 1), choose (2 * m + 1) (2 * m + 1 - i) = βˆ‘ i in range (m + 1), choose (2 * m + 1) i, from sum_congr rfl $ Ξ» i hi, choose_symm $ by linarith [mem_range.1 hi], (nat.mul_right_inj zero_lt_two).1 $ calc 2 * (βˆ‘ i in range (m + 1), nat.choose (2 * m + 1) i) = (βˆ‘ i in range (m + 1), nat.choose (2 * m + 1) i) + βˆ‘ i in range (m + 1), nat.choose (2 * m + 1) (2 * m + 1 - i) : by rw [two_mul, this] ... = (βˆ‘ i in range (m + 1), nat.choose (2 * m + 1) i) + βˆ‘ i in Ico (m + 1) (2 * m + 2), nat.choose (2 * m + 1) i : by { rw [range_eq_Ico, sum_Ico_reflect], { congr, omega }, omega } ... = βˆ‘ i in range (2 * m + 2), nat.choose (2 * m + 1) i : sum_range_add_sum_Ico _ (by omega) ... = 2^(2 * m + 1) : sum_range_choose (2 * m + 1) ... = 2 * 4^m : by { rw [nat.pow_succ, mul_comm, nat.pow_mul], refl } lemma choose_middle_le_pow (n : β„•) : choose (2 * n + 1) n ≀ 4 ^ n := begin have t : choose (2 * n + 1) n ≀ βˆ‘ i in finset.range (n + 1), choose (2 * n + 1) i := finset.single_le_sum (Ξ» x _, by linarith) (finset.self_mem_range_succ n), simpa [sum_range_choose_halfway n] using t end end binomial
f2fc9b7188bb483cfacff08aab09fe3e38f7ad6e
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/ring_theory/dedekind_domain/ideal.lean
be1aed2e252fddb8f7346bbf5958485a4e460aa4
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
66,275
lean
/- Copyright (c) 2020 Kenji Nakagawa. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenji Nakagawa, Anne Baanen, Filippo A. E. Nuccio -/ import algebra.algebra.subalgebra.pointwise import algebraic_geometry.prime_spectrum.maximal import algebraic_geometry.prime_spectrum.noetherian import order.hom.basic import ring_theory.dedekind_domain.basic import ring_theory.fractional_ideal import ring_theory.principal_ideal_domain import ring_theory.chain_of_divisors /-! # Dedekind domains and ideals > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file, we show a ring is a Dedekind domain iff all fractional ideals are invertible. Then we prove some results on the unique factorization monoid structure of the ideals. ## Main definitions - `is_dedekind_domain_inv` alternatively defines a Dedekind domain as an integral domain where every nonzero fractional ideal is invertible. - `is_dedekind_domain_inv_iff` shows that this does note depend on the choice of field of fractions. - `is_dedekind_domain.height_one_spectrum` defines the type of nonzero prime ideals of `R`. ## Main results: - `is_dedekind_domain_iff_is_dedekind_domain_inv` - `ideal.unique_factorization_monoid` ## Implementation notes The definitions that involve a field of fractions choose a canonical field of fractions, but are independent of that choice. The `..._iff` lemmas express this independence. Often, definitions assume that Dedekind domains are not fields. We found it more practical to add a `(h : Β¬ is_field A)` assumption whenever this is explicitly needed. ## References * [D. Marcus, *Number Fields*][marcus1977number] * [J.W.S. Cassels, A. FrΓΆlich, *Algebraic Number Theory*][cassels1967algebraic] * [J. Neukirch, *Algebraic Number Theory*][Neukirch1992] ## Tags dedekind domain, dedekind ring -/ variables (R A K : Type*) [comm_ring R] [comm_ring A] [field K] open_locale non_zero_divisors polynomial variables [is_domain A] section inverse namespace fractional_ideal variables {R₁ : Type*} [comm_ring R₁] [is_domain R₁] [algebra R₁ K] [is_fraction_ring R₁ K] variables {I J : fractional_ideal R₁⁰ K} noncomputable instance : has_inv (fractional_ideal R₁⁰ K) := ⟨λ I, 1 / I⟩ lemma inv_eq : I⁻¹ = 1 / I := rfl lemma inv_zero' : (0 : fractional_ideal R₁⁰ K)⁻¹ = 0 := div_zero lemma inv_nonzero {J : fractional_ideal R₁⁰ K} (h : J β‰  0) : J⁻¹ = ⟨(1 : fractional_ideal R₁⁰ K) / J, fractional_div_of_nonzero h⟩ := div_nonzero _ lemma coe_inv_of_nonzero {J : fractional_ideal R₁⁰ K} (h : J β‰  0) : (↑J⁻¹ : submodule R₁ K) = is_localization.coe_submodule K ⊀ / J := by { rwa inv_nonzero _, refl, assumption } variables {K} lemma mem_inv_iff (hI : I β‰  0) {x : K} : x ∈ I⁻¹ ↔ βˆ€ y ∈ I, x * y ∈ (1 : fractional_ideal R₁⁰ K) := mem_div_iff_of_nonzero hI lemma inv_anti_mono (hI : I β‰  0) (hJ : J β‰  0) (hIJ : I ≀ J) : J⁻¹ ≀ I⁻¹ := Ξ» x, by { simp only [mem_inv_iff hI, mem_inv_iff hJ], exact Ξ» h y hy, h y (hIJ hy) } lemma le_self_mul_inv {I : fractional_ideal R₁⁰ K} (hI : I ≀ (1 : fractional_ideal R₁⁰ K)) : I ≀ I * I⁻¹ := le_self_mul_one_div hI variables (K) lemma coe_ideal_le_self_mul_inv (I : ideal R₁) : (I : fractional_ideal R₁⁰ K) ≀ I * I⁻¹ := le_self_mul_inv coe_ideal_le_one /-- `I⁻¹` is the inverse of `I` if `I` has an inverse. -/ theorem right_inverse_eq (I J : fractional_ideal R₁⁰ K) (h : I * J = 1) : J = I⁻¹ := begin have hI : I β‰  0 := ne_zero_of_mul_eq_one I J h, suffices h' : I * (1 / I) = 1, { exact (congr_arg units.inv $ @units.ext _ _ (units.mk_of_mul_eq_one _ _ h) (units.mk_of_mul_eq_one _ _ h') rfl) }, apply le_antisymm, { apply mul_le.mpr _, intros x hx y hy, rw mul_comm, exact (mem_div_iff_of_nonzero hI).mp hy x hx }, rw ← h, apply mul_left_mono I, apply (le_div_iff_of_nonzero hI).mpr _, intros y hy x hx, rw mul_comm, exact mul_mem_mul hx hy end theorem mul_inv_cancel_iff {I : fractional_ideal R₁⁰ K} : I * I⁻¹ = 1 ↔ βˆƒ J, I * J = 1 := ⟨λ h, ⟨I⁻¹, h⟩, Ξ» ⟨J, hJ⟩, by rwa ← right_inverse_eq K I J hJ⟩ lemma mul_inv_cancel_iff_is_unit {I : fractional_ideal R₁⁰ K} : I * I⁻¹ = 1 ↔ is_unit I := (mul_inv_cancel_iff K).trans is_unit_iff_exists_inv.symm variables {K' : Type*} [field K'] [algebra R₁ K'] [is_fraction_ring R₁ K'] @[simp] lemma map_inv (I : fractional_ideal R₁⁰ K) (h : K ≃ₐ[R₁] K') : (I⁻¹).map (h : K →ₐ[R₁] K') = (I.map h)⁻¹ := by rw [inv_eq, map_div, map_one, inv_eq] open submodule submodule.is_principal @[simp] lemma span_singleton_inv (x : K) : (span_singleton R₁⁰ x)⁻¹ = span_singleton _ x⁻¹ := one_div_span_singleton x @[simp] lemma span_singleton_div_span_singleton (x y : K) : span_singleton R₁⁰ x / span_singleton R₁⁰ y = span_singleton R₁⁰ (x / y) := by rw [div_span_singleton, mul_comm, span_singleton_mul_span_singleton, div_eq_mul_inv] lemma span_singleton_div_self {x : K} (hx : x β‰  0) : span_singleton R₁⁰ x / span_singleton R₁⁰ x = 1 := by rw [span_singleton_div_span_singleton, div_self hx, span_singleton_one] lemma coe_ideal_span_singleton_div_self {x : R₁} (hx : x β‰  0) : (ideal.span ({x} : set R₁) : fractional_ideal R₁⁰ K) / ideal.span ({x} : set R₁) = 1 := by rw [coe_ideal_span_singleton, span_singleton_div_self K $ (map_ne_zero_iff _ $ no_zero_smul_divisors.algebra_map_injective R₁ K).mpr hx] lemma span_singleton_mul_inv {x : K} (hx : x β‰  0) : span_singleton R₁⁰ x * (span_singleton R₁⁰ x)⁻¹ = 1 := by rw [span_singleton_inv, span_singleton_mul_span_singleton, mul_inv_cancel hx, span_singleton_one] lemma coe_ideal_span_singleton_mul_inv {x : R₁} (hx : x β‰  0) : (ideal.span ({x} : set R₁) : fractional_ideal R₁⁰ K) * (ideal.span ({x} : set R₁))⁻¹ = 1 := by rw [coe_ideal_span_singleton, span_singleton_mul_inv K $ (map_ne_zero_iff _ $ no_zero_smul_divisors.algebra_map_injective R₁ K).mpr hx] lemma span_singleton_inv_mul {x : K} (hx : x β‰  0) : (span_singleton R₁⁰ x)⁻¹ * span_singleton R₁⁰ x = 1 := by rw [mul_comm, span_singleton_mul_inv K hx] lemma coe_ideal_span_singleton_inv_mul {x : R₁} (hx : x β‰  0) : (ideal.span ({x} : set R₁) : fractional_ideal R₁⁰ K)⁻¹ * ideal.span ({x} : set R₁) = 1 := by rw [mul_comm, coe_ideal_span_singleton_mul_inv K hx] lemma mul_generator_self_inv {R₁ : Type*} [comm_ring R₁] [algebra R₁ K] [is_localization R₁⁰ K] (I : fractional_ideal R₁⁰ K) [submodule.is_principal (I : submodule R₁ K)] (h : I β‰  0) : I * span_singleton _ (generator (I : submodule R₁ K))⁻¹ = 1 := begin -- Rewrite only the `I` that appears alone. conv_lhs { congr, rw eq_span_singleton_of_principal I }, rw [span_singleton_mul_span_singleton, mul_inv_cancel, span_singleton_one], intro generator_I_eq_zero, apply h, rw [eq_span_singleton_of_principal I, generator_I_eq_zero, span_singleton_zero] end lemma invertible_of_principal (I : fractional_ideal R₁⁰ K) [submodule.is_principal (I : submodule R₁ K)] (h : I β‰  0) : I * I⁻¹ = 1 := (mul_div_self_cancel_iff).mpr ⟨span_singleton _ (generator (I : submodule R₁ K))⁻¹, mul_generator_self_inv _ I h⟩ lemma invertible_iff_generator_nonzero (I : fractional_ideal R₁⁰ K) [submodule.is_principal (I : submodule R₁ K)] : I * I⁻¹ = 1 ↔ generator (I : submodule R₁ K) β‰  0 := begin split, { intros hI hg, apply ne_zero_of_mul_eq_one _ _ hI, rw [eq_span_singleton_of_principal I, hg, span_singleton_zero] }, { intro hg, apply invertible_of_principal, rw [eq_span_singleton_of_principal I], intro hI, have := mem_span_singleton_self _ (generator (I : submodule R₁ K)), rw [hI, mem_zero_iff] at this, contradiction } end lemma is_principal_inv (I : fractional_ideal R₁⁰ K) [submodule.is_principal (I : submodule R₁ K)] (h : I β‰  0) : submodule.is_principal (I⁻¹).1 := begin rw [val_eq_coe, is_principal_iff], use (generator (I : submodule R₁ K))⁻¹, have hI : I * span_singleton _ ((generator (I : submodule R₁ K))⁻¹) = 1, apply mul_generator_self_inv _ I h, exact (right_inverse_eq _ I (span_singleton _ ((generator (I : submodule R₁ K))⁻¹)) hI).symm end noncomputable instance : inv_one_class (fractional_ideal R₁⁰ K) := { inv_one := div_one, ..fractional_ideal.has_one, ..fractional_ideal.has_inv K } end fractional_ideal /-- A Dedekind domain is an integral domain such that every fractional ideal has an inverse. This is equivalent to `is_dedekind_domain`. In particular we provide a `fractional_ideal.comm_group_with_zero` instance, assuming `is_dedekind_domain A`, which implies `is_dedekind_domain_inv`. For **integral** ideals, `is_dedekind_domain`(`_inv`) implies only `ideal.cancel_comm_monoid_with_zero`. -/ def is_dedekind_domain_inv : Prop := βˆ€ I β‰  (βŠ₯ : fractional_ideal A⁰ (fraction_ring A)), I * I⁻¹ = 1 open fractional_ideal variables {R A K} lemma is_dedekind_domain_inv_iff [algebra A K] [is_fraction_ring A K] : is_dedekind_domain_inv A ↔ (βˆ€ I β‰  (βŠ₯ : fractional_ideal A⁰ K), I * I⁻¹ = 1) := begin let h := map_equiv (fraction_ring.alg_equiv A K), refine h.to_equiv.forall_congr (Ξ» I, _), rw ← h.to_equiv.apply_eq_iff_eq, simp [is_dedekind_domain_inv, show ⇑h.to_equiv = h, from rfl], end lemma fractional_ideal.adjoin_integral_eq_one_of_is_unit [algebra A K] [is_fraction_ring A K] (x : K) (hx : is_integral A x) (hI : is_unit (adjoin_integral A⁰ x hx)) : adjoin_integral A⁰ x hx = 1 := begin set I := adjoin_integral A⁰ x hx, have mul_self : I * I = I, { apply coe_to_submodule_injective, simp }, convert congr_arg (* I⁻¹) mul_self; simp only [(mul_inv_cancel_iff_is_unit K).mpr hI, mul_assoc, mul_one], end namespace is_dedekind_domain_inv variables [algebra A K] [is_fraction_ring A K] (h : is_dedekind_domain_inv A) include h lemma mul_inv_eq_one {I : fractional_ideal A⁰ K} (hI : I β‰  0) : I * I⁻¹ = 1 := is_dedekind_domain_inv_iff.mp h I hI lemma inv_mul_eq_one {I : fractional_ideal A⁰ K} (hI : I β‰  0) : I⁻¹ * I = 1 := (mul_comm _ _).trans (h.mul_inv_eq_one hI) protected lemma is_unit {I : fractional_ideal A⁰ K} (hI : I β‰  0) : is_unit I := is_unit_of_mul_eq_one _ _ (h.mul_inv_eq_one hI) lemma is_noetherian_ring : is_noetherian_ring A := begin refine is_noetherian_ring_iff.mpr ⟨λ (I : ideal A), _⟩, by_cases hI : I = βŠ₯, { rw hI, apply submodule.fg_bot }, have hI : (I : fractional_ideal A⁰ (fraction_ring A)) β‰  0 := coe_ideal_ne_zero.mpr hI, exact I.fg_of_is_unit (is_fraction_ring.injective A (fraction_ring A)) (h.is_unit hI) end lemma integrally_closed : is_integrally_closed A := begin -- It suffices to show that for integral `x`, -- `A[x]` (which is a fractional ideal) is in fact equal to `A`. refine ⟨λ x hx, _⟩, rw [← set.mem_range, ← algebra.mem_bot, ← subalgebra.mem_to_submodule, algebra.to_submodule_bot, ← coe_span_singleton A⁰ (1 : fraction_ring A), span_singleton_one, ← fractional_ideal.adjoin_integral_eq_one_of_is_unit x hx (h.is_unit _)], { exact mem_adjoin_integral_self A⁰ x hx }, { exact Ξ» h, one_ne_zero (eq_zero_iff.mp h 1 (subalgebra.one_mem _)) }, end open ring lemma dimension_le_one : dimension_le_one A := begin -- We're going to show that `P` is maximal because any (maximal) ideal `M` -- that is strictly larger would be `⊀`. rintros P P_ne hP, refine ideal.is_maximal_def.mpr ⟨hP.ne_top, Ξ» M hM, _⟩, -- We may assume `P` and `M` (as fractional ideals) are nonzero. have P'_ne : (P : fractional_ideal A⁰ (fraction_ring A)) β‰  0 := coe_ideal_ne_zero.mpr P_ne, have M'_ne : (M : fractional_ideal A⁰ (fraction_ring A)) β‰  0 := coe_ideal_ne_zero.mpr (lt_of_le_of_lt bot_le hM).ne', -- In particular, we'll show `M⁻¹ * P ≀ P` suffices : (M⁻¹ * P : fractional_ideal A⁰ (fraction_ring A)) ≀ P, { rw [eq_top_iff, ← coe_ideal_le_coe_ideal (fraction_ring A), coe_ideal_top], calc (1 : fractional_ideal A⁰ (fraction_ring A)) = _ * _ * _ : _ ... ≀ _ * _ : mul_right_mono (P⁻¹ * M : fractional_ideal A⁰ (fraction_ring A)) this ... = M : _, { rw [mul_assoc, ← mul_assoc ↑P, h.mul_inv_eq_one P'_ne, one_mul, h.inv_mul_eq_one M'_ne] }, { rw [← mul_assoc ↑P, h.mul_inv_eq_one P'_ne, one_mul] }, { apply_instance } }, -- Suppose we have `x ∈ M⁻¹ * P`, then in fact `x = algebra_map _ _ y` for some `y`. intros x hx, have le_one : (M⁻¹ * P : fractional_ideal A⁰ (fraction_ring A)) ≀ 1, { rw [← h.inv_mul_eq_one M'_ne], exact mul_left_mono _ ((coe_ideal_le_coe_ideal (fraction_ring A)).mpr hM.le) }, obtain ⟨y, hy, rfl⟩ := (mem_coe_ideal _).mp (le_one hx), -- Since `M` is strictly greater than `P`, let `z ∈ M \ P`. obtain ⟨z, hzM, hzp⟩ := set_like.exists_of_lt hM, -- We have `z * y ∈ M * (M⁻¹ * P) = P`. have zy_mem := mul_mem_mul (mem_coe_ideal_of_mem A⁰ hzM) hx, rw [← ring_hom.map_mul, ← mul_assoc, h.mul_inv_eq_one M'_ne, one_mul] at zy_mem, obtain ⟨zy, hzy, zy_eq⟩ := (mem_coe_ideal A⁰).mp zy_mem, rw is_fraction_ring.injective A (fraction_ring A) zy_eq at hzy, -- But `P` is a prime ideal, so `z βˆ‰ P` implies `y ∈ P`, as desired. exact mem_coe_ideal_of_mem A⁰ (or.resolve_left (hP.mem_or_mem hzy) hzp) end /-- Showing one side of the equivalence between the definitions `is_dedekind_domain_inv` and `is_dedekind_domain` of Dedekind domains. -/ theorem is_dedekind_domain : is_dedekind_domain A := ⟨h.is_noetherian_ring, h.dimension_le_one, h.integrally_closed⟩ end is_dedekind_domain_inv variables [algebra A K] [is_fraction_ring A K] /-- Specialization of `exists_prime_spectrum_prod_le_and_ne_bot_of_domain` to Dedekind domains: Let `I : ideal A` be a nonzero ideal, where `A` is a Dedekind domain that is not a field. Then `exists_prime_spectrum_prod_le_and_ne_bot_of_domain` states we can find a product of prime ideals that is contained within `I`. This lemma extends that result by making the product minimal: let `M` be a maximal ideal that contains `I`, then the product including `M` is contained within `I` and the product excluding `M` is not contained within `I`. -/ lemma exists_multiset_prod_cons_le_and_prod_not_le [is_dedekind_domain A] (hNF : Β¬ is_field A) {I M : ideal A} (hI0 : I β‰  βŠ₯) (hIM : I ≀ M) [hM : M.is_maximal] : βˆƒ (Z : multiset (prime_spectrum A)), (M ::β‚˜ (Z.map prime_spectrum.as_ideal)).prod ≀ I ∧ Β¬ (multiset.prod (Z.map prime_spectrum.as_ideal) ≀ I) := begin -- Let `Z` be a minimal set of prime ideals such that their product is contained in `J`. obtain ⟨Zβ‚€, hZβ‚€βŸ© := prime_spectrum.exists_prime_spectrum_prod_le_and_ne_bot_of_domain hNF hI0, obtain ⟨Z, ⟨hZI, hprodZ⟩, h_eraseZ⟩ := multiset.well_founded_lt.has_min (Ξ» Z, (Z.map prime_spectrum.as_ideal).prod ≀ I ∧ (Z.map prime_spectrum.as_ideal).prod β‰  βŠ₯) ⟨Zβ‚€, hZβ‚€βŸ©, have hZM : multiset.prod (Z.map prime_spectrum.as_ideal) ≀ M := le_trans hZI hIM, have hZ0 : Z β‰  0, { rintro rfl, simpa [hM.ne_top] using hZM }, obtain ⟨_, hPZ', hPM⟩ := (hM.is_prime.multiset_prod_le (mt multiset.map_eq_zero.mp hZ0)).mp hZM, -- Then in fact there is a `P ∈ Z` with `P ≀ M`. obtain ⟨P, hPZ, rfl⟩ := multiset.mem_map.mp hPZ', classical, have := multiset.map_erase prime_spectrum.as_ideal prime_spectrum.ext P Z, obtain ⟨hP0, hZP0⟩ : P.as_ideal β‰  βŠ₯ ∧ ((Z.erase P).map prime_spectrum.as_ideal).prod β‰  βŠ₯, { rwa [ne.def, ← multiset.cons_erase hPZ', multiset.prod_cons, ideal.mul_eq_bot, not_or_distrib, ← this] at hprodZ }, -- By maximality of `P` and `M`, we have that `P ≀ M` implies `P = M`. have hPM' := (is_dedekind_domain.dimension_le_one _ hP0 P.is_prime).eq_of_le hM.ne_top hPM, substI hPM', -- By minimality of `Z`, erasing `P` from `Z` is exactly what we need. refine ⟨Z.erase P, _, _⟩, { convert hZI, rw [this, multiset.cons_erase hPZ'] }, { refine Ξ» h, h_eraseZ (Z.erase P) ⟨h, _⟩ (multiset.erase_lt.mpr hPZ), exact hZP0 } end namespace fractional_ideal open ideal lemma exists_not_mem_one_of_ne_bot [is_dedekind_domain A] (hNF : Β¬ is_field A) {I : ideal A} (hI0 : I β‰  βŠ₯) (hI1 : I β‰  ⊀) : βˆƒ x : K, x ∈ (I⁻¹ : fractional_ideal A⁰ K) ∧ x βˆ‰ (1 : fractional_ideal A⁰ K) := begin -- WLOG, let `I` be maximal. suffices : βˆ€ {M : ideal A} (hM : M.is_maximal), βˆƒ x : K, x ∈ (M⁻¹ : fractional_ideal A⁰ K) ∧ x βˆ‰ (1 : fractional_ideal A⁰ K), { obtain ⟨M, hM, hIM⟩ : βˆƒ (M : ideal A), is_maximal M ∧ I ≀ M := ideal.exists_le_maximal I hI1, resetI, have hM0 := (M.bot_lt_of_maximal hNF).ne', obtain ⟨x, hxM, hx1⟩ := this hM, refine ⟨x, inv_anti_mono _ _ ((coe_ideal_le_coe_ideal _).mpr hIM) hxM, hx1⟩; rw coe_ideal_ne_zero; assumption }, -- Let `a` be a nonzero element of `M` and `J` the ideal generated by `a`. intros M hM, resetI, obtain ⟨⟨a, haM⟩, ha0⟩ := submodule.nonzero_mem_of_bot_lt (M.bot_lt_of_maximal hNF), replace ha0 : a β‰  0 := subtype.coe_injective.ne ha0, let J : ideal A := ideal.span {a}, have hJ0 : J β‰  βŠ₯ := mt ideal.span_singleton_eq_bot.mp ha0, have hJM : J ≀ M := ideal.span_le.mpr (set.singleton_subset_iff.mpr haM), have hM0 : βŠ₯ < M := M.bot_lt_of_maximal hNF, -- Then we can find a product of prime (hence maximal) ideals contained in `J`, -- such that removing element `M` from the product is not contained in `J`. obtain ⟨Z, hle, hnle⟩ := exists_multiset_prod_cons_le_and_prod_not_le hNF hJ0 hJM, -- Choose an element `b` of the product that is not in `J`. obtain ⟨b, hbZ, hbJ⟩ := set_like.not_le_iff_exists.mp hnle, have hnz_fa : algebra_map A K a β‰  0 := mt ((injective_iff_map_eq_zero _).mp (is_fraction_ring.injective A K) a) ha0, have hb0 : algebra_map A K b β‰  0 := mt ((injective_iff_map_eq_zero _).mp (is_fraction_ring.injective A K) b) (Ξ» h, hbJ $ h.symm β–Έ J.zero_mem), -- Then `b a⁻¹ : K` is in `M⁻¹` but not in `1`. refine ⟨algebra_map A K b * (algebra_map A K a)⁻¹, (mem_inv_iff _).mpr _, _⟩, { exact coe_ideal_ne_zero.mpr hM0.ne' }, { rintro yβ‚€ hyβ‚€, obtain ⟨y, h_Iy, rfl⟩ := (mem_coe_ideal _).mp hyβ‚€, rw [mul_comm, ← mul_assoc, ← ring_hom.map_mul], have h_yb : y * b ∈ J, { apply hle, rw multiset.prod_cons, exact submodule.smul_mem_smul h_Iy hbZ }, rw ideal.mem_span_singleton' at h_yb, rcases h_yb with ⟨c, hc⟩, rw [← hc, ring_hom.map_mul, mul_assoc, mul_inv_cancel hnz_fa, mul_one], apply coe_mem_one }, { refine mt (mem_one_iff _).mp _, rintros ⟨x', hβ‚‚_abs⟩, rw [← div_eq_mul_inv, eq_div_iff_mul_eq hnz_fa, ← ring_hom.map_mul] at hβ‚‚_abs, have := ideal.mem_span_singleton'.mpr ⟨x', is_fraction_ring.injective A K hβ‚‚_abs⟩, contradiction }, end lemma one_mem_inv_coe_ideal {I : ideal A} (hI : I β‰  βŠ₯) : (1 : K) ∈ (I : fractional_ideal A⁰ K)⁻¹ := begin rw mem_inv_iff (coe_ideal_ne_zero.mpr hI), intros y hy, rw one_mul, exact coe_ideal_le_one hy, assumption end lemma mul_inv_cancel_of_le_one [h : is_dedekind_domain A] {I : ideal A} (hI0 : I β‰  βŠ₯) (hI : ((I * I⁻¹)⁻¹ : fractional_ideal A⁰ K) ≀ 1) : (I * I⁻¹ : fractional_ideal A⁰ K) = 1 := begin -- Handle a few trivial cases. by_cases hI1 : I = ⊀, { rw [hI1, coe_ideal_top, one_mul, inv_one] }, by_cases hNF : is_field A, { letI := hNF.to_field, rcases hI1 (I.eq_bot_or_top.resolve_left hI0) }, -- We'll show a contradiction with `exists_not_mem_one_of_ne_bot`: -- `J⁻¹ = (I * I⁻¹)⁻¹` cannot have an element `x βˆ‰ 1`, so it must equal `1`. obtain ⟨J, hJ⟩ : βˆƒ (J : ideal A), (J : fractional_ideal A⁰ K) = I * I⁻¹ := le_one_iff_exists_coe_ideal.mp mul_one_div_le_one, by_cases hJ0 : J = βŠ₯, { subst hJ0, refine absurd _ hI0, rw [eq_bot_iff, ← coe_ideal_le_coe_ideal K, hJ], exact coe_ideal_le_self_mul_inv K I, apply_instance }, by_cases hJ1 : J = ⊀, { rw [← hJ, hJ1, coe_ideal_top] }, obtain ⟨x, hx, hx1⟩ : βˆƒ (x : K), x ∈ (J : fractional_ideal A⁰ K)⁻¹ ∧ x βˆ‰ (1 : fractional_ideal A⁰ K) := exists_not_mem_one_of_ne_bot hNF hJ0 hJ1, contrapose! hx1 with h_abs, rw hJ at hx, exact hI hx, end /-- Nonzero integral ideals in a Dedekind domain are invertible. We will use this to show that nonzero fractional ideals are invertible, and finally conclude that fractional ideals in a Dedekind domain form a group with zero. -/ lemma coe_ideal_mul_inv [h : is_dedekind_domain A] (I : ideal A) (hI0 : I β‰  βŠ₯) : (I * I⁻¹ : fractional_ideal A⁰ K) = 1 := begin -- We'll show `1 ≀ J⁻¹ = (I * I⁻¹)⁻¹ ≀ 1`. apply mul_inv_cancel_of_le_one hI0, by_cases hJ0 : (I * I⁻¹ : fractional_ideal A⁰ K) = 0, { rw [hJ0, inv_zero'], exact zero_le _ }, intros x hx, -- In particular, we'll show all `x ∈ J⁻¹` are integral. suffices : x ∈ integral_closure A K, { rwa [is_integrally_closed.integral_closure_eq_bot, algebra.mem_bot, set.mem_range, ← mem_one_iff] at this; assumption }, -- For that, we'll find a subalgebra that is f.g. as a module and contains `x`. -- `A` is a noetherian ring, so we just need to find a subalgebra between `{x}` and `I⁻¹`. rw mem_integral_closure_iff_mem_fg, have x_mul_mem : βˆ€ b ∈ (I⁻¹ : fractional_ideal A⁰ K), x * b ∈ (I⁻¹ : fractional_ideal A⁰ K), { intros b hb, rw mem_inv_iff at ⊒ hx, swap, { exact coe_ideal_ne_zero.mpr hI0 }, swap, { exact hJ0 }, simp only [mul_assoc, mul_comm b] at ⊒ hx, intros y hy, exact hx _ (mul_mem_mul hy hb) }, -- It turns out the subalgebra consisting of all `p(x)` for `p : A[X]` works. refine ⟨alg_hom.range (polynomial.aeval x : A[X] →ₐ[A] K), is_noetherian_submodule.mp (is_noetherian I⁻¹) _ (Ξ» y hy, _), ⟨polynomial.X, polynomial.aeval_X x⟩⟩, obtain ⟨p, rfl⟩ := (alg_hom.mem_range _).mp hy, rw polynomial.aeval_eq_sum_range, refine submodule.sum_mem _ (Ξ» i hi, submodule.smul_mem _ _ _), clear hi, induction i with i ih, { rw pow_zero, exact one_mem_inv_coe_ideal hI0 }, { show x ^ i.succ ∈ (I⁻¹ : fractional_ideal A⁰ K), rw pow_succ, exact x_mul_mem _ ih }, end /-- Nonzero fractional ideals in a Dedekind domain are units. This is also available as `_root_.mul_inv_cancel`, using the `comm_group_with_zero` instance defined below. -/ protected theorem mul_inv_cancel [is_dedekind_domain A] {I : fractional_ideal A⁰ K} (hne : I β‰  0) : I * I⁻¹ = 1 := begin obtain ⟨a, J, ha, hJ⟩ : βˆƒ (a : A) (aI : ideal A), a β‰  0 ∧ I = span_singleton A⁰ (algebra_map _ _ a)⁻¹ * aI := exists_eq_span_singleton_mul I, suffices hβ‚‚ : I * (span_singleton A⁰ (algebra_map _ _ a) * J⁻¹) = 1, { rw mul_inv_cancel_iff, exact ⟨span_singleton A⁰ (algebra_map _ _ a) * J⁻¹, hβ‚‚βŸ© }, subst hJ, rw [mul_assoc, mul_left_comm (J : fractional_ideal A⁰ K), coe_ideal_mul_inv, mul_one, span_singleton_mul_span_singleton, inv_mul_cancel, span_singleton_one], { exact mt ((injective_iff_map_eq_zero (algebra_map A K)).mp (is_fraction_ring.injective A K) _) ha }, { exact coe_ideal_ne_zero.mp (right_ne_zero_of_mul hne) } end lemma mul_right_le_iff [is_dedekind_domain A] {J : fractional_ideal A⁰ K} (hJ : J β‰  0) : βˆ€ {I I'}, I * J ≀ I' * J ↔ I ≀ I' := begin intros I I', split, { intros h, convert mul_right_mono J⁻¹ h; rw [mul_assoc, fractional_ideal.mul_inv_cancel hJ, mul_one] }, { exact Ξ» h, mul_right_mono J h } end lemma mul_left_le_iff [is_dedekind_domain A] {J : fractional_ideal A⁰ K} (hJ : J β‰  0) {I I'} : J * I ≀ J * I' ↔ I ≀ I' := by convert mul_right_le_iff hJ using 1; simp only [mul_comm] lemma mul_right_strict_mono [is_dedekind_domain A] {I : fractional_ideal A⁰ K} (hI : I β‰  0) : strict_mono (* I) := strict_mono_of_le_iff_le (Ξ» _ _, (mul_right_le_iff hI).symm) lemma mul_left_strict_mono [is_dedekind_domain A] {I : fractional_ideal A⁰ K} (hI : I β‰  0) : strict_mono ((*) I) := strict_mono_of_le_iff_le (Ξ» _ _, (mul_left_le_iff hI).symm) /-- This is also available as `_root_.div_eq_mul_inv`, using the `comm_group_with_zero` instance defined below. -/ protected lemma div_eq_mul_inv [is_dedekind_domain A] (I J : fractional_ideal A⁰ K) : I / J = I * J⁻¹ := begin by_cases hJ : J = 0, { rw [hJ, div_zero, inv_zero', mul_zero] }, refine le_antisymm ((mul_right_le_iff hJ).mp _) ((le_div_iff_mul_le hJ).mpr _), { rw [mul_assoc, mul_comm J⁻¹, fractional_ideal.mul_inv_cancel hJ, mul_one, mul_le], intros x hx y hy, rw [mem_div_iff_of_nonzero hJ] at hx, exact hx y hy }, rw [mul_assoc, mul_comm J⁻¹, fractional_ideal.mul_inv_cancel hJ, mul_one], exact le_refl I end end fractional_ideal /-- `is_dedekind_domain` and `is_dedekind_domain_inv` are equivalent ways to express that an integral domain is a Dedekind domain. -/ theorem is_dedekind_domain_iff_is_dedekind_domain_inv : is_dedekind_domain A ↔ is_dedekind_domain_inv A := ⟨λ h I hI, by exactI fractional_ideal.mul_inv_cancel hI, Ξ» h, h.is_dedekind_domain⟩ end inverse section is_dedekind_domain variables {R A} [is_dedekind_domain A] [algebra A K] [is_fraction_ring A K] open fractional_ideal open ideal noncomputable instance fractional_ideal.semifield : semifield (fractional_ideal A⁰ K) := { inv := Ξ» I, I⁻¹, inv_zero := inv_zero' _, div := (/), div_eq_mul_inv := fractional_ideal.div_eq_mul_inv, mul_inv_cancel := Ξ» I, fractional_ideal.mul_inv_cancel, .. fractional_ideal.comm_semiring, .. coe_ideal_injective.nontrivial } /-- Fractional ideals have cancellative multiplication in a Dedekind domain. Although this instance is a direct consequence of the instance `fractional_ideal.comm_group_with_zero`, we define this instance to provide a computable alternative. -/ instance fractional_ideal.cancel_comm_monoid_with_zero : cancel_comm_monoid_with_zero (fractional_ideal A⁰ K) := { .. fractional_ideal.comm_semiring, -- Project out the computable fields first. .. (by apply_instance : cancel_comm_monoid_with_zero (fractional_ideal A⁰ K)) } instance ideal.cancel_comm_monoid_with_zero : cancel_comm_monoid_with_zero (ideal A) := { .. ideal.idem_comm_semiring, .. function.injective.cancel_comm_monoid_with_zero (coe_ideal_hom A⁰ (fraction_ring A)) coe_ideal_injective (ring_hom.map_zero _) (ring_hom.map_one _) (ring_hom.map_mul _) (ring_hom.map_pow _) } instance ideal.is_domain : is_domain (ideal A) := { .. (infer_instance : is_cancel_mul_zero _), .. ideal.nontrivial } /-- For ideals in a Dedekind domain, to divide is to contain. -/ lemma ideal.dvd_iff_le {I J : ideal A} : (I ∣ J) ↔ J ≀ I := ⟨ideal.le_of_dvd, Ξ» h, begin by_cases hI : I = βŠ₯, { have hJ : J = βŠ₯, { rwa [hI, ← eq_bot_iff] at h }, rw [hI, hJ] }, have hI' : (I : fractional_ideal A⁰ (fraction_ring A)) β‰  0 := coe_ideal_ne_zero.mpr hI, have : (I : fractional_ideal A⁰ (fraction_ring A))⁻¹ * J ≀ 1 := le_trans (mul_left_mono (↑I)⁻¹ ((coe_ideal_le_coe_ideal _).mpr h)) (le_of_eq (inv_mul_cancel hI')), obtain ⟨H, hH⟩ := le_one_iff_exists_coe_ideal.mp this, use H, refine coe_ideal_injective (show (J : fractional_ideal A⁰ (fraction_ring A)) = ↑(I * H), from _), rw [coe_ideal_mul, hH, ← mul_assoc, mul_inv_cancel hI', one_mul] end⟩ lemma ideal.dvd_not_unit_iff_lt {I J : ideal A} : dvd_not_unit I J ↔ J < I := ⟨λ ⟨hI, H, hunit, hmul⟩, lt_of_le_of_ne (ideal.dvd_iff_le.mp ⟨H, hmul⟩) (mt (Ξ» h, have H = 1, from mul_left_cancelβ‚€ hI (by rw [← hmul, h, mul_one]), show is_unit H, from this.symm β–Έ is_unit_one) hunit), Ξ» h, dvd_not_unit_of_dvd_of_not_dvd (ideal.dvd_iff_le.mpr (le_of_lt h)) (mt ideal.dvd_iff_le.mp (not_le_of_lt h))⟩ instance : wf_dvd_monoid (ideal A) := { well_founded_dvd_not_unit := have well_founded ((>) : ideal A β†’ ideal A β†’ Prop) := is_noetherian_iff_well_founded.mp (is_noetherian_ring_iff.mp is_dedekind_domain.is_noetherian_ring), by { convert this, ext, rw ideal.dvd_not_unit_iff_lt } } instance ideal.unique_factorization_monoid : unique_factorization_monoid (ideal A) := { irreducible_iff_prime := Ξ» P, ⟨λ hirr, ⟨hirr.ne_zero, hirr.not_unit, Ξ» I J, begin have : P.is_maximal, { refine ⟨⟨mt ideal.is_unit_iff.mpr hirr.not_unit, _⟩⟩, intros J hJ, obtain ⟨J_ne, H, hunit, P_eq⟩ := ideal.dvd_not_unit_iff_lt.mpr hJ, exact ideal.is_unit_iff.mp ((hirr.is_unit_or_is_unit P_eq).resolve_right hunit) }, rw [ideal.dvd_iff_le, ideal.dvd_iff_le, ideal.dvd_iff_le, set_like.le_def, set_like.le_def, set_like.le_def], contrapose!, rintros ⟨⟨x, x_mem, x_not_mem⟩, ⟨y, y_mem, y_not_mem⟩⟩, exact ⟨x * y, ideal.mul_mem_mul x_mem y_mem, mt this.is_prime.mem_or_mem (not_or x_not_mem y_not_mem)⟩, end⟩, prime.irreducible⟩, .. ideal.wf_dvd_monoid } instance ideal.normalization_monoid : normalization_monoid (ideal A) := normalization_monoid_of_unique_units @[simp] lemma ideal.dvd_span_singleton {I : ideal A} {x : A} : I ∣ ideal.span {x} ↔ x ∈ I := ideal.dvd_iff_le.trans (ideal.span_le.trans set.singleton_subset_iff) lemma ideal.is_prime_of_prime {P : ideal A} (h : prime P) : is_prime P := begin refine ⟨_, Ξ» x y hxy, _⟩, { unfreezingI { rintro rfl }, rw ← ideal.one_eq_top at h, exact h.not_unit is_unit_one }, { simp only [← ideal.dvd_span_singleton, ← ideal.span_singleton_mul_span_singleton] at ⊒ hxy, exact h.dvd_or_dvd hxy } end theorem ideal.prime_of_is_prime {P : ideal A} (hP : P β‰  βŠ₯) (h : is_prime P) : prime P := begin refine ⟨hP, mt ideal.is_unit_iff.mp h.ne_top, Ξ» I J hIJ, _⟩, simpa only [ideal.dvd_iff_le] using (h.mul_le.mp (ideal.le_of_dvd hIJ)), end /-- In a Dedekind domain, the (nonzero) prime elements of the monoid with zero `ideal A` are exactly the prime ideals. -/ theorem ideal.prime_iff_is_prime {P : ideal A} (hP : P β‰  βŠ₯) : prime P ↔ is_prime P := ⟨ideal.is_prime_of_prime, ideal.prime_of_is_prime hP⟩ /-- In a Dedekind domain, the the prime ideals are the zero ideal together with the prime elements of the monoid with zero `ideal A`. -/ theorem ideal.is_prime_iff_bot_or_prime {P : ideal A} : is_prime P ↔ P = βŠ₯ ∨ prime P := ⟨λ hp, (eq_or_ne P βŠ₯).imp_right $ Ξ» hp0, (ideal.prime_of_is_prime hp0 hp), Ξ» hp, hp.elim (Ξ» h, h.symm β–Έ ideal.bot_prime) ideal.is_prime_of_prime⟩ lemma ideal.strict_anti_pow (I : ideal A) (hI0 : I β‰  βŠ₯) (hI1 : I β‰  ⊀) : strict_anti ((^) I : β„• β†’ ideal A) := strict_anti_nat_of_succ_lt $ Ξ» e, ideal.dvd_not_unit_iff_lt.mp ⟨pow_ne_zero _ hI0, I, mt is_unit_iff.mp hI1, pow_succ' I e⟩ lemma ideal.pow_lt_self (I : ideal A) (hI0 : I β‰  βŠ₯) (hI1 : I β‰  ⊀) (e : β„•) (he : 2 ≀ e) : I^e < I := by convert I.strict_anti_pow hI0 hI1 he; rw pow_one lemma ideal.exists_mem_pow_not_mem_pow_succ (I : ideal A) (hI0 : I β‰  βŠ₯) (hI1 : I β‰  ⊀) (e : β„•) : βˆƒ x ∈ I^e, x βˆ‰ I^(e+1) := set_like.exists_of_lt (I.strict_anti_pow hI0 hI1 e.lt_succ_self) open unique_factorization_monoid lemma ideal.eq_prime_pow_of_succ_lt_of_le {P I : ideal A} [P_prime : P.is_prime] (hP : P β‰  βŠ₯) {i : β„•} (hlt : P ^ (i + 1) < I) (hle : I ≀ P ^ i) : I = P ^ i := begin letI := classical.dec_eq (ideal A), refine le_antisymm hle _, have P_prime' := ideal.prime_of_is_prime hP P_prime, have : I β‰  βŠ₯ := (lt_of_le_of_lt bot_le hlt).ne', have := pow_ne_zero i hP, have := pow_ne_zero (i + 1) hP, rw [← ideal.dvd_not_unit_iff_lt, dvd_not_unit_iff_normalized_factors_lt_normalized_factors, normalized_factors_pow, normalized_factors_irreducible P_prime'.irreducible, multiset.nsmul_singleton, multiset.lt_replicate_succ] at hlt, rw [← ideal.dvd_iff_le, dvd_iff_normalized_factors_le_normalized_factors, normalized_factors_pow, normalized_factors_irreducible P_prime'.irreducible, multiset.nsmul_singleton], all_goals { assumption } end lemma ideal.pow_succ_lt_pow {P : ideal A} [P_prime : P.is_prime] (hP : P β‰  βŠ₯) (i : β„•) : P ^ (i + 1) < P ^ i := lt_of_le_of_ne (ideal.pow_le_pow (nat.le_succ _)) (mt (pow_eq_pow_iff hP (mt ideal.is_unit_iff.mp P_prime.ne_top)).mp i.succ_ne_self) lemma associates.le_singleton_iff (x : A) (n : β„•) (I : ideal A) : associates.mk I^n ≀ associates.mk (ideal.span {x}) ↔ x ∈ I^n := begin rw [← associates.dvd_eq_le, ← associates.mk_pow, associates.mk_dvd_mk, ideal.dvd_span_singleton], end open fractional_ideal variables {A K} /-- Strengthening of `is_localization.exist_integer_multiples`: Let `J β‰  ⊀` be an ideal in a Dedekind domain `A`, and `f β‰  0` a finite collection of elements of `K = Frac(A)`, then we can multiply the elements of `f` by some `a : K` to find a collection of elements of `A` that is not completely contained in `J`. -/ lemma ideal.exist_integer_multiples_not_mem {J : ideal A} (hJ : J β‰  ⊀) {ΞΉ : Type*} (s : finset ΞΉ) (f : ΞΉ β†’ K) {j} (hjs : j ∈ s) (hjf : f j β‰  0) : βˆƒ a : K, (βˆ€ i ∈ s, is_localization.is_integer A (a * f i)) ∧ βˆƒ i ∈ s, (a * f i) βˆ‰ (J : fractional_ideal A⁰ K) := begin -- Consider the fractional ideal `I` spanned by the `f`s. let I : fractional_ideal A⁰ K := span_finset A s f, have hI0 : I β‰  0 := span_finset_ne_zero.mpr ⟨j, hjs, hjf⟩, -- We claim the multiplier `a` we're looking for is in `I⁻¹ \ (J / I)`. suffices : ↑J / I < I⁻¹, { obtain ⟨_, a, hI, hpI⟩ := set_like.lt_iff_le_and_exists.mp this, rw mem_inv_iff hI0 at hI, refine ⟨a, Ξ» i hi, _, _⟩, -- By definition, `a ∈ I⁻¹` multiplies elements of `I` into elements of `1`, -- in other words, `a * f i` is an integer. { exact (mem_one_iff _).mp (hI (f i) (submodule.subset_span (set.mem_image_of_mem f hi))) }, { contrapose! hpI, -- And if all `a`-multiples of `I` are an element of `J`, -- then `a` is actually an element of `J / I`, contradiction. refine (mem_div_iff_of_nonzero hI0).mpr (Ξ» y hy, submodule.span_induction hy _ _ _ _), { rintros _ ⟨i, hi, rfl⟩, exact hpI i hi }, { rw mul_zero, exact submodule.zero_mem _ }, { intros x y hx hy, rw mul_add, exact submodule.add_mem _ hx hy }, { intros b x hx, rw mul_smul_comm, exact submodule.smul_mem _ b hx } } }, -- To show the inclusion of `J / I` into `I⁻¹ = 1 / I`, note that `J < I`. calc ↑J / I = ↑J * I⁻¹ : div_eq_mul_inv ↑J I ... < 1 * I⁻¹ : mul_right_strict_mono (inv_ne_zero hI0) _ ... = I⁻¹ : one_mul _, { rw [← coe_ideal_top], -- And multiplying by `I⁻¹` is indeed strictly monotone. exact strict_mono_of_le_iff_le (Ξ» _ _, (coe_ideal_le_coe_ideal K).symm) (lt_top_iff_ne_top.mpr hJ) }, end section gcd namespace ideal /-! ### GCD and LCM of ideals in a Dedekind domain We show that the gcd of two ideals in a Dedekind domain is just their supremum, and the lcm is their infimum, and use this to instantiate `normalized_gcd_monoid (ideal A)`. -/ @[simp] lemma sup_mul_inf (I J : ideal A) : (I βŠ” J) * (I βŠ“ J) = I * J := begin letI := classical.dec_eq (ideal A), letI := classical.dec_eq (associates (ideal A)), letI := unique_factorization_monoid.to_normalized_gcd_monoid (ideal A), have hgcd : gcd I J = I βŠ” J, { rw [gcd_eq_normalize _ _, normalize_eq], { rw [dvd_iff_le, sup_le_iff, ← dvd_iff_le, ← dvd_iff_le], exact ⟨gcd_dvd_left _ _, gcd_dvd_right _ _⟩ }, { rw [dvd_gcd_iff, dvd_iff_le, dvd_iff_le], simp } }, have hlcm : lcm I J = I βŠ“ J, { rw [lcm_eq_normalize _ _, normalize_eq], { rw [lcm_dvd_iff, dvd_iff_le, dvd_iff_le], simp }, { rw [dvd_iff_le, le_inf_iff, ← dvd_iff_le, ← dvd_iff_le], exact ⟨dvd_lcm_left _ _, dvd_lcm_right _ _⟩ } }, rw [← hgcd, ← hlcm, associated_iff_eq.mp (gcd_mul_lcm _ _)], apply_instance end /-- Ideals in a Dedekind domain have gcd and lcm operators that (trivially) are compatible with the normalization operator. -/ instance : normalized_gcd_monoid (ideal A) := { gcd := (βŠ”), gcd_dvd_left := Ξ» _ _, by simpa only [dvd_iff_le] using le_sup_left, gcd_dvd_right := Ξ» _ _, by simpa only [dvd_iff_le] using le_sup_right, dvd_gcd := Ξ» _ _ _, by simpa only [dvd_iff_le] using sup_le, lcm := (βŠ“), lcm_zero_left := Ξ» _, by simp only [zero_eq_bot, bot_inf_eq], lcm_zero_right := Ξ» _, by simp only [zero_eq_bot, inf_bot_eq], gcd_mul_lcm := Ξ» _ _, by rw [associated_iff_eq, sup_mul_inf], normalize_gcd := Ξ» _ _, normalize_eq _, normalize_lcm := Ξ» _ _, normalize_eq _, .. ideal.normalization_monoid } -- In fact, any lawful gcd and lcm would equal sup and inf respectively. @[simp] lemma gcd_eq_sup (I J : ideal A) : gcd I J = I βŠ” J := rfl @[simp] lemma lcm_eq_inf (I J : ideal A) : lcm I J = I βŠ“ J := rfl lemma inf_eq_mul_of_coprime {I J : ideal A} (coprime : I βŠ” J = ⊀) : I βŠ“ J = I * J := by rw [← associated_iff_eq.mp (gcd_mul_lcm I J), lcm_eq_inf I J, gcd_eq_sup, coprime, top_mul] end ideal end gcd end is_dedekind_domain section is_dedekind_domain variables {T : Type*} [comm_ring T] [is_domain T] [is_dedekind_domain T] {I J : ideal T} open_locale classical open multiset unique_factorization_monoid ideal lemma prod_normalized_factors_eq_self (hI : I β‰  βŠ₯) : (normalized_factors I).prod = I := associated_iff_eq.1 (normalized_factors_prod hI) lemma count_le_of_ideal_ge {I J : ideal T} (h : I ≀ J) (hI : I β‰  βŠ₯) (K : ideal T) : count K (normalized_factors J) ≀ count K (normalized_factors I) := le_iff_count.1 ((dvd_iff_normalized_factors_le_normalized_factors (ne_bot_of_le_ne_bot hI h) hI).1 (dvd_iff_le.2 h)) _ lemma sup_eq_prod_inf_factors (hI : I β‰  βŠ₯) (hJ : J β‰  βŠ₯) : I βŠ” J = (normalized_factors I ∩ normalized_factors J).prod := begin have H : normalized_factors (normalized_factors I ∩ normalized_factors J).prod = normalized_factors I ∩ normalized_factors J, { apply normalized_factors_prod_of_prime, intros p hp, rw mem_inter at hp, exact prime_of_normalized_factor p hp.left }, have := (multiset.prod_ne_zero_of_prime (normalized_factors I ∩ normalized_factors J) (Ξ» _ h, prime_of_normalized_factor _ (multiset.mem_inter.1 h).1)), apply le_antisymm, { rw [sup_le_iff, ← dvd_iff_le, ← dvd_iff_le], split, { rw [dvd_iff_normalized_factors_le_normalized_factors this hI, H], exact inf_le_left }, { rw [dvd_iff_normalized_factors_le_normalized_factors this hJ, H], exact inf_le_right } }, { rw [← dvd_iff_le, dvd_iff_normalized_factors_le_normalized_factors, normalized_factors_prod_of_prime, le_iff_count], { intro a, rw multiset.count_inter, exact le_min (count_le_of_ideal_ge le_sup_left hI a) (count_le_of_ideal_ge le_sup_right hJ a) }, { intros p hp, rw mem_inter at hp, exact prime_of_normalized_factor p hp.left }, { exact ne_bot_of_le_ne_bot hI le_sup_left }, { exact this } }, end lemma irreducible_pow_sup (hI : I β‰  βŠ₯) (hJ : irreducible J) (n : β„•) : J^n βŠ” I = J^(min ((normalized_factors I).count J) n) := by rw [sup_eq_prod_inf_factors (pow_ne_zero n hJ.ne_zero) hI, min_comm, normalized_factors_of_irreducible_pow hJ, normalize_eq J, replicate_inter, prod_replicate] lemma irreducible_pow_sup_of_le (hJ : irreducible J) (n : β„•) (hn : ↑n ≀ multiplicity J I) : J^n βŠ” I = J^n := begin by_cases hI : I = βŠ₯, { simp [*] at *, }, rw [irreducible_pow_sup hI hJ, min_eq_right], rwa [multiplicity_eq_count_normalized_factors hJ hI, part_enat.coe_le_coe, normalize_eq J] at hn end lemma irreducible_pow_sup_of_ge (hI : I β‰  βŠ₯) (hJ : irreducible J) (n : β„•) (hn : multiplicity J I ≀ n) : J^n βŠ” I = J ^ (multiplicity J I).get (part_enat.dom_of_le_coe hn) := begin rw [irreducible_pow_sup hI hJ, min_eq_left], congr, { rw [← part_enat.coe_inj, part_enat.coe_get, multiplicity_eq_count_normalized_factors hJ hI, normalize_eq J] }, { rwa [multiplicity_eq_count_normalized_factors hJ hI, part_enat.coe_le_coe, normalize_eq J] at hn } end end is_dedekind_domain /-! ### Height one spectrum of a Dedekind domain If `R` is a Dedekind domain of Krull dimension 1, the maximal ideals of `R` are exactly its nonzero prime ideals. We define `height_one_spectrum` and provide lemmas to recover the facts that prime ideals of height one are prime and irreducible. -/ namespace is_dedekind_domain variables [is_domain R] [is_dedekind_domain R] /-- The height one prime spectrum of a Dedekind domain `R` is the type of nonzero prime ideals of `R`. Note that this equals the maximal spectrum if `R` has Krull dimension 1. -/ @[ext, nolint has_nonempty_instance unused_arguments] structure height_one_spectrum := (as_ideal : ideal R) (is_prime : as_ideal.is_prime) (ne_bot : as_ideal β‰  βŠ₯) attribute [instance] height_one_spectrum.is_prime variables (v : height_one_spectrum R) {R} namespace height_one_spectrum instance is_maximal : v.as_ideal.is_maximal := dimension_le_one v.as_ideal v.ne_bot v.is_prime lemma prime : prime v.as_ideal := ideal.prime_of_is_prime v.ne_bot v.is_prime lemma irreducible : irreducible v.as_ideal := unique_factorization_monoid.irreducible_iff_prime.mpr v.prime lemma associates_irreducible : _root_.irreducible $ associates.mk v.as_ideal := (associates.irreducible_mk _).mpr v.irreducible /-- An equivalence between the height one and maximal spectra for rings of Krull dimension 1. -/ def equiv_maximal_spectrum (hR : Β¬is_field R) : height_one_spectrum R ≃ maximal_spectrum R := { to_fun := Ξ» v, ⟨v.as_ideal, dimension_le_one v.as_ideal v.ne_bot v.is_prime⟩, inv_fun := Ξ» v, ⟨v.as_ideal, v.is_maximal.is_prime, ring.ne_bot_of_is_maximal_of_not_is_field v.is_maximal hR⟩, left_inv := Ξ» ⟨_, _, _⟩, rfl, right_inv := Ξ» ⟨_, _⟩, rfl } variables (R K) /-- A Dedekind domain is equal to the intersection of its localizations at all its height one non-zero prime ideals viewed as subalgebras of its field of fractions. -/ theorem infi_localization_eq_bot [algebra R K] [hK : is_fraction_ring R K] : (β¨… v : height_one_spectrum R, localization.subalgebra.of_field K _ v.as_ideal.prime_compl_le_non_zero_divisors) = βŠ₯ := begin ext x, rw [algebra.mem_infi], split, by_cases hR : is_field R, { rcases function.bijective_iff_has_inverse.mp (is_field.localization_map_bijective (flip non_zero_divisors.ne_zero rfl : 0 βˆ‰ R⁰) hR) with ⟨algebra_map_inv, _, algebra_map_right_inv⟩, exact Ξ» _, algebra.mem_bot.mpr ⟨algebra_map_inv x, algebra_map_right_inv x⟩, exact hK }, all_goals { rw [← maximal_spectrum.infi_localization_eq_bot, algebra.mem_infi] }, { exact Ξ» hx ⟨v, hv⟩, hx ((equiv_maximal_spectrum hR).symm ⟨v, hv⟩) }, { exact Ξ» hx ⟨v, hv, hbot⟩, hx ⟨v, dimension_le_one v hbot hv⟩ } end end height_one_spectrum end is_dedekind_domain section open ideal variables {R} {A} [is_dedekind_domain A] {I : ideal R} {J : ideal A} /-- The map from ideals of `R` dividing `I` to the ideals of `A` dividing `J` induced by a homomorphism `f : R/I β†’+* A/J` -/ @[simps] def ideal_factors_fun_of_quot_hom {f : R β§Έ I β†’+* A β§Έ J} (hf : function.surjective f ) : {p : ideal R | p ∣ I} β†’o {p : ideal A | p ∣ J} := { to_fun := Ξ» X, ⟨comap J^.quotient.mk (map f (map I^.quotient.mk X)), begin have : (J^.quotient.mk).ker ≀ comap J^.quotient.mk (map f (map I^.quotient.mk X)), { exact ker_le_comap J^.quotient.mk }, rw mk_ker at this, exact dvd_iff_le.mpr this, end ⟩, monotone' := begin rintros ⟨X, hX⟩ ⟨Y, hY⟩ h, rw [← subtype.coe_le_coe, subtype.coe_mk, subtype.coe_mk] at h ⊒, rw [subtype.coe_mk, comap_le_comap_iff_of_surjective J^.quotient.mk quotient.mk_surjective, map_le_iff_le_comap, subtype.coe_mk, comap_map_of_surjective _ hf (map I^.quotient.mk Y)], suffices : map I^.quotient.mk X ≀ map I^.quotient.mk Y, { exact le_sup_of_le_left this }, rwa [map_le_iff_le_comap, comap_map_of_surjective I^.quotient.mk quotient.mk_surjective, ← ring_hom.ker_eq_comap_bot, mk_ker, sup_eq_left.mpr $ le_of_dvd hY], end } @[simp] lemma ideal_factors_fun_of_quot_hom_id : ideal_factors_fun_of_quot_hom (ring_hom.id (A β§Έ J)).is_surjective = order_hom.id := order_hom.ext _ _ (funext $ Ξ» X, by simp only [ideal_factors_fun_of_quot_hom, map_id, order_hom.coe_fun_mk, order_hom.id_coe, id.def, comap_map_of_surjective J^.quotient.mk quotient.mk_surjective, ← ring_hom.ker_eq_comap_bot J^.quotient.mk, mk_ker, sup_eq_left.mpr (dvd_iff_le.mp X.prop), subtype.coe_eta] ) variables {B : Type*} [comm_ring B] [is_domain B] [is_dedekind_domain B] {L : ideal B} lemma ideal_factors_fun_of_quot_hom_comp {f : R β§Έ I β†’+* A β§Έ J} {g : A β§Έ J β†’+* B β§Έ L} (hf : function.surjective f) (hg : function.surjective g) : (ideal_factors_fun_of_quot_hom hg).comp (ideal_factors_fun_of_quot_hom hf) = ideal_factors_fun_of_quot_hom (show function.surjective (g.comp f), from hg.comp hf) := begin refine order_hom.ext _ _ (funext $ Ξ» x, _), rw [ideal_factors_fun_of_quot_hom, ideal_factors_fun_of_quot_hom, order_hom.comp_coe, order_hom.coe_fun_mk, order_hom.coe_fun_mk, function.comp_app, ideal_factors_fun_of_quot_hom, order_hom.coe_fun_mk, subtype.mk_eq_mk, subtype.coe_mk, map_comap_of_surjective J^.quotient.mk quotient.mk_surjective, map_map], end variables [is_domain R] [is_dedekind_domain R] (f : R β§Έ I ≃+* A β§Έ J) /-- The bijection between ideals of `R` dividing `I` and the ideals of `A` dividing `J` induced by an isomorphism `f : R/I β‰… A/J`. -/ @[simps] def ideal_factors_equiv_of_quot_equiv : {p : ideal R | p ∣ I} ≃o {p : ideal A | p ∣ J} := order_iso.of_hom_inv (ideal_factors_fun_of_quot_hom (show function.surjective (f : R β§ΈI β†’+* A β§Έ J), from f.surjective)) (ideal_factors_fun_of_quot_hom (show function.surjective (f.symm : A β§ΈJ β†’+* R β§Έ I), from f.symm.surjective)) (by simp only [← ideal_factors_fun_of_quot_hom_id, order_hom.coe_eq, order_hom.coe_eq, ideal_factors_fun_of_quot_hom_comp, ← ring_equiv.to_ring_hom_eq_coe, ← ring_equiv.to_ring_hom_eq_coe, ← ring_equiv.to_ring_hom_trans, ring_equiv.symm_trans_self, ring_equiv.to_ring_hom_refl]) (by simp only [← ideal_factors_fun_of_quot_hom_id, order_hom.coe_eq, order_hom.coe_eq, ideal_factors_fun_of_quot_hom_comp, ← ring_equiv.to_ring_hom_eq_coe, ← ring_equiv.to_ring_hom_eq_coe, ← ring_equiv.to_ring_hom_trans, ring_equiv.self_trans_symm, ring_equiv.to_ring_hom_refl]) lemma ideal_factors_equiv_of_quot_equiv_symm : (ideal_factors_equiv_of_quot_equiv f).symm = ideal_factors_equiv_of_quot_equiv f.symm := rfl lemma ideal_factors_equiv_of_quot_equiv_is_dvd_iso {L M : ideal R} (hL : L ∣ I) (hM : M ∣ I) : (ideal_factors_equiv_of_quot_equiv f ⟨L, hL⟩ : ideal A) ∣ ideal_factors_equiv_of_quot_equiv f ⟨M, hM⟩ ↔ L ∣ M := begin suffices : ideal_factors_equiv_of_quot_equiv f ⟨M, hM⟩ ≀ ideal_factors_equiv_of_quot_equiv f ⟨L, hL⟩ ↔ (⟨M, hM⟩ : {p : ideal R | p ∣ I}) ≀ ⟨L, hL⟩, { rw [dvd_iff_le, dvd_iff_le, subtype.coe_le_coe, this, subtype.mk_le_mk] }, exact (ideal_factors_equiv_of_quot_equiv f).le_iff_le, end open unique_factorization_monoid variables [decidable_eq (ideal R)] [decidable_eq (ideal A)] lemma ideal_factors_equiv_of_quot_equiv_mem_normalized_factors_of_mem_normalized_factors (hJ : J β‰  βŠ₯) {L : ideal R} (hL : L ∈ normalized_factors I) : ↑(ideal_factors_equiv_of_quot_equiv f ⟨L, dvd_of_mem_normalized_factors hL⟩) ∈ normalized_factors J := begin by_cases hI : I = βŠ₯, { exfalso, rw [hI, bot_eq_zero, normalized_factors_zero, ← multiset.empty_eq_zero] at hL, exact hL, }, { apply mem_normalized_factors_factor_dvd_iso_of_mem_normalized_factors hI hJ hL _, rintros ⟨l, hl⟩ ⟨l', hl'⟩, rw [subtype.coe_mk, subtype.coe_mk], apply ideal_factors_equiv_of_quot_equiv_is_dvd_iso f } end /-- The bijection between the sets of normalized factors of I and J induced by a ring isomorphism `f : R/I β‰… A/J`. -/ @[simps apply] def normalized_factors_equiv_of_quot_equiv (hI : I β‰  βŠ₯) (hJ : J β‰  βŠ₯) : {L : ideal R | L ∈ normalized_factors I } ≃ {M : ideal A | M ∈ normalized_factors J } := { to_fun := Ξ» j, ⟨ideal_factors_equiv_of_quot_equiv f βŸ¨β†‘j, dvd_of_mem_normalized_factors j.prop⟩, ideal_factors_equiv_of_quot_equiv_mem_normalized_factors_of_mem_normalized_factors f hJ j.prop⟩, inv_fun := Ξ» j, ⟨(ideal_factors_equiv_of_quot_equiv f).symm βŸ¨β†‘j, dvd_of_mem_normalized_factors j.prop⟩, by { rw ideal_factors_equiv_of_quot_equiv_symm, exact ideal_factors_equiv_of_quot_equiv_mem_normalized_factors_of_mem_normalized_factors f.symm hI j.prop} ⟩, left_inv := Ξ» ⟨j, hj⟩, by simp, right_inv := Ξ» ⟨j, hj⟩, by simp } @[simp] lemma normalized_factors_equiv_of_quot_equiv_symm (hI : I β‰  βŠ₯) (hJ : J β‰  βŠ₯) : (normalized_factors_equiv_of_quot_equiv f hI hJ).symm = normalized_factors_equiv_of_quot_equiv f.symm hJ hI := rfl variable [decidable_rel ((∣) : ideal R β†’ ideal R β†’ Prop)] variable [decidable_rel ((∣) : ideal A β†’ ideal A β†’ Prop)] /-- The map `normalized_factors_equiv_of_quot_equiv` preserves multiplicities. -/ lemma normalized_factors_equiv_of_quot_equiv_multiplicity_eq_multiplicity (hI : I β‰  βŠ₯) (hJ : J β‰  βŠ₯) (L : ideal R) (hL : L ∈ normalized_factors I) : multiplicity ↑(normalized_factors_equiv_of_quot_equiv f hI hJ ⟨L, hL⟩) J = multiplicity L I := begin rw [normalized_factors_equiv_of_quot_equiv, equiv.coe_fn_mk, subtype.coe_mk], exact multiplicity_factor_dvd_iso_eq_multiplicity_of_mem_normalized_factor hI hJ hL (Ξ» ⟨l, hl⟩ ⟨l', hl'⟩, ideal_factors_equiv_of_quot_equiv_is_dvd_iso f hl hl'), end end section chinese_remainder open ideal unique_factorization_monoid open_locale big_operators variables {R} lemma ring.dimension_le_one.prime_le_prime_iff_eq (h : ring.dimension_le_one R) {P Q : ideal R} [hP : P.is_prime] [hQ : Q.is_prime] (hP0 : P β‰  βŠ₯) : P ≀ Q ↔ P = Q := ⟨(h P hP0 hP).eq_of_le hQ.ne_top, eq.le⟩ lemma ideal.coprime_of_no_prime_ge {I J : ideal R} (h : βˆ€ P, I ≀ P β†’ J ≀ P β†’ Β¬ is_prime P) : I βŠ” J = ⊀ := begin by_contra hIJ, obtain ⟨P, hP, hIJ⟩ := ideal.exists_le_maximal _ hIJ, exact h P (le_trans le_sup_left hIJ) (le_trans le_sup_right hIJ) hP.is_prime end section dedekind_domain variables {R} [is_domain R] [is_dedekind_domain R] lemma ideal.is_prime.mul_mem_pow (I : ideal R) [hI : I.is_prime] {a b : R} {n : β„•} (h : a * b ∈ I^n) : a ∈ I ∨ b ∈ I^n := begin cases n, { simp }, by_cases hI0 : I = βŠ₯, { simpa [pow_succ, hI0] using h }, simp only [← submodule.span_singleton_le_iff_mem, ideal.submodule_span_eq, ← ideal.dvd_iff_le, ← ideal.span_singleton_mul_span_singleton] at h ⊒, by_cases ha : I ∣ span {a}, { exact or.inl ha }, rw mul_comm at h, exact or.inr (prime.pow_dvd_of_dvd_mul_right ((ideal.prime_iff_is_prime hI0).mpr hI) _ ha h), end section open_locale classical lemma ideal.count_normalized_factors_eq {p x : ideal R} [hp : p.is_prime] {n : β„•} (hle : x ≀ p^n) (hlt : Β¬ (x ≀ p^(n+1))) : (normalized_factors x).count p = n := count_normalized_factors_eq' ((ideal.is_prime_iff_bot_or_prime.mp hp).imp_right prime.irreducible) (by { haveI : unique (ideal R)Λ£ := ideal.unique_units, apply normalize_eq }) (by convert ideal.dvd_iff_le.mpr hle) (by convert mt ideal.le_of_dvd hlt) /- Warning: even though a pure term-mode proof typechecks (the `by convert` can simply be removed), it's slower to the point of a possible timeout. -/ end lemma ideal.le_mul_of_no_prime_factors {I J K : ideal R} (coprime : βˆ€ P, J ≀ P β†’ K ≀ P β†’ Β¬ is_prime P) (hJ : I ≀ J) (hK : I ≀ K) : I ≀ J * K := begin simp only [← ideal.dvd_iff_le] at coprime hJ hK ⊒, by_cases hJ0 : J = 0, { simpa only [hJ0, zero_mul] using hJ }, obtain ⟨I', rfl⟩ := hK, rw mul_comm, exact mul_dvd_mul_left K (unique_factorization_monoid.dvd_of_dvd_mul_right_of_no_prime_factors hJ0 (Ξ» P hPJ hPK, mt ideal.is_prime_of_prime (coprime P hPJ hPK)) hJ) end lemma ideal.le_of_pow_le_prime {I P : ideal R} [hP : P.is_prime] {n : β„•} (h : I^n ≀ P) : I ≀ P := begin by_cases hP0 : P = βŠ₯, { simp only [hP0, le_bot_iff] at ⊒ h, exact pow_eq_zero h }, rw ← ideal.dvd_iff_le at ⊒ h, exact ((ideal.prime_iff_is_prime hP0).mpr hP).dvd_of_dvd_pow h end lemma ideal.pow_le_prime_iff {I P : ideal R} [hP : P.is_prime] {n : β„•} (hn : n β‰  0) : I^n ≀ P ↔ I ≀ P := ⟨ideal.le_of_pow_le_prime, Ξ» h, trans (ideal.pow_le_self hn) h⟩ lemma ideal.prod_le_prime {ΞΉ : Type*} {s : finset ΞΉ} {f : ΞΉ β†’ ideal R} {P : ideal R} [hP : P.is_prime] : ∏ i in s, f i ≀ P ↔ βˆƒ i ∈ s, f i ≀ P := begin by_cases hP0 : P = βŠ₯, { simp only [hP0, le_bot_iff], rw [← ideal.zero_eq_bot, finset.prod_eq_zero_iff] }, simp only [← ideal.dvd_iff_le], exact ((ideal.prime_iff_is_prime hP0).mpr hP).dvd_finset_prod_iff _ end /-- The intersection of distinct prime powers in a Dedekind domain is the product of these prime powers. -/ lemma is_dedekind_domain.inf_prime_pow_eq_prod {ΞΉ : Type*} (s : finset ΞΉ) (f : ΞΉ β†’ ideal R) (e : ΞΉ β†’ β„•) (prime : βˆ€ i ∈ s, prime (f i)) (coprime : βˆ€ i j ∈ s, i β‰  j β†’ f i β‰  f j) : s.inf (Ξ» i, f i ^ e i) = ∏ i in s, f i ^ e i := begin letI := classical.dec_eq ΞΉ, revert prime coprime, refine s.induction _ _, { simp }, intros a s ha ih prime coprime, specialize ih (Ξ» i hi, prime i (finset.mem_insert_of_mem hi)) (Ξ» i hi j hj, coprime i (finset.mem_insert_of_mem hi) j (finset.mem_insert_of_mem hj)), rw [finset.inf_insert, finset.prod_insert ha, ih], refine le_antisymm (ideal.le_mul_of_no_prime_factors _ inf_le_left inf_le_right) ideal.mul_le_inf, intros P hPa hPs hPp, haveI := hPp, obtain ⟨b, hb, hPb⟩ := ideal.prod_le_prime.mp hPs, haveI := ideal.is_prime_of_prime (prime a (finset.mem_insert_self a s)), haveI := ideal.is_prime_of_prime (prime b (finset.mem_insert_of_mem hb)), refine coprime a (finset.mem_insert_self a s) b (finset.mem_insert_of_mem hb) _ (((is_dedekind_domain.dimension_le_one.prime_le_prime_iff_eq _).mp (ideal.le_of_pow_le_prime hPa)).trans ((is_dedekind_domain.dimension_le_one.prime_le_prime_iff_eq _).mp (ideal.le_of_pow_le_prime hPb)).symm), { unfreezingI { rintro rfl }, contradiction }, { exact (prime a (finset.mem_insert_self a s)).ne_zero }, { exact (prime b (finset.mem_insert_of_mem hb)).ne_zero }, end /-- **Chinese remainder theorem** for a Dedekind domain: if the ideal `I` factors as `∏ i, P i ^ e i`, then `R β§Έ I` factors as `Ξ  i, R β§Έ (P i ^ e i)`. -/ noncomputable def is_dedekind_domain.quotient_equiv_pi_of_prod_eq {ΞΉ : Type*} [fintype ΞΉ] (I : ideal R) (P : ΞΉ β†’ ideal R) (e : ΞΉ β†’ β„•) (prime : βˆ€ i, prime (P i)) (coprime : βˆ€ i j, i β‰  j β†’ P i β‰  P j) (prod_eq : (∏ i, P i ^ e i) = I) : R β§Έ I ≃+* Ξ  i, R β§Έ (P i ^ e i) := (ideal.quot_equiv_of_eq (by { simp only [← prod_eq, finset.inf_eq_infi, finset.mem_univ, cinfi_pos, ← is_dedekind_domain.inf_prime_pow_eq_prod _ _ _ (Ξ» i _, prime i) (Ξ» i _ j _, coprime i j)] })) .trans $ ideal.quotient_inf_ring_equiv_pi_quotient _ (Ξ» i j hij, ideal.coprime_of_no_prime_ge (begin intros P hPi hPj hPp, haveI := hPp, haveI := ideal.is_prime_of_prime (prime i), haveI := ideal.is_prime_of_prime (prime j), exact coprime i j hij (((is_dedekind_domain.dimension_le_one.prime_le_prime_iff_eq (prime i).ne_zero).mp (ideal.le_of_pow_le_prime hPi)).trans ((is_dedekind_domain.dimension_le_one.prime_le_prime_iff_eq (prime j).ne_zero).mp (ideal.le_of_pow_le_prime hPj)).symm) end)) open_locale classical /-- **Chinese remainder theorem** for a Dedekind domain: `R β§Έ I` factors as `Ξ  i, R β§Έ (P i ^ e i)`, where `P i` ranges over the prime factors of `I` and `e i` over the multiplicities. -/ noncomputable def is_dedekind_domain.quotient_equiv_pi_factors {I : ideal R} (hI : I β‰  βŠ₯) : R β§Έ I ≃+* Ξ  (P : (factors I).to_finset), R β§Έ ((P : ideal R) ^ (factors I).count P) := is_dedekind_domain.quotient_equiv_pi_of_prod_eq _ _ _ (Ξ» (P : (factors I).to_finset), prime_of_factor _ (multiset.mem_to_finset.mp P.prop)) (Ξ» i j hij, subtype.coe_injective.ne hij) (calc ∏ (P : (factors I).to_finset), (P : ideal R) ^ (factors I).count (P : ideal R) = ∏ P in (factors I).to_finset, P ^ (factors I).count P : (factors I).to_finset.prod_coe_sort (Ξ» P, P ^ (factors I).count P) ... = ((factors I).map (Ξ» P, P)).prod : (finset.prod_multiset_map_count (factors I) id).symm ... = (factors I).prod : by rw multiset.map_id' ... = I : (@associated_iff_eq (ideal R) _ ideal.unique_units _ _).mp (factors_prod hI)) @[simp] lemma is_dedekind_domain.quotient_equiv_pi_factors_mk {I : ideal R} (hI : I β‰  βŠ₯) (x : R) : is_dedekind_domain.quotient_equiv_pi_factors hI (ideal.quotient.mk I x) = Ξ» P, ideal.quotient.mk _ x := rfl /-- **Chinese remainder theorem**, specialized to two ideals. -/ noncomputable def ideal.quotient_mul_equiv_quotient_prod (I J : ideal R) (coprime : I βŠ” J = ⊀) : (R β§Έ (I * J)) ≃+* (R β§Έ I) Γ— R β§Έ J := ring_equiv.trans (ideal.quot_equiv_of_eq (inf_eq_mul_of_coprime coprime).symm) (ideal.quotient_inf_equiv_quotient_prod I J coprime) /-- **Chinese remainder theorem** for a Dedekind domain: if the ideal `I` factors as `∏ i in s, P i ^ e i`, then `R β§Έ I` factors as `Ξ  (i : s), R β§Έ (P i ^ e i)`. This is a version of `is_dedekind_domain.quotient_equiv_pi_of_prod_eq` where we restrict the product to a finite subset `s` of a potentially infinite indexing type `ΞΉ`. -/ noncomputable def is_dedekind_domain.quotient_equiv_pi_of_finset_prod_eq {ΞΉ : Type*} {s : finset ΞΉ} (I : ideal R) (P : ΞΉ β†’ ideal R) (e : ΞΉ β†’ β„•) (prime : βˆ€ i ∈ s, prime (P i)) (coprime : βˆ€ (i j ∈ s), i β‰  j β†’ P i β‰  P j) (prod_eq : (∏ i in s, P i ^ e i) = I) : R β§Έ I ≃+* Ξ  (i : s), R β§Έ (P i ^ e i) := is_dedekind_domain.quotient_equiv_pi_of_prod_eq I (Ξ» (i : s), P i) (Ξ» (i : s), e i) (Ξ» i, prime i i.2) (Ξ» i j h, coprime i i.2 j j.2 (subtype.coe_injective.ne h)) (trans (finset.prod_coe_sort s (Ξ» i, P i ^ e i)) prod_eq) /-- Corollary of the Chinese remainder theorem: given elements `x i : R / P i ^ e i`, we can choose a representative `y : R` such that `y ≑ x i (mod P i ^ e i)`.-/ lemma is_dedekind_domain.exists_representative_mod_finset {ΞΉ : Type*} {s : finset ΞΉ} (P : ΞΉ β†’ ideal R) (e : ΞΉ β†’ β„•) (prime : βˆ€ i ∈ s, prime (P i)) (coprime : βˆ€ (i j ∈ s), i β‰  j β†’ P i β‰  P j) (x : Ξ  (i : s), R β§Έ (P i ^ e i)) : βˆƒ y, βˆ€ i (hi : i ∈ s), ideal.quotient.mk (P i ^ e i) y = x ⟨i, hi⟩ := begin let f := is_dedekind_domain.quotient_equiv_pi_of_finset_prod_eq _ P e prime coprime rfl, obtain ⟨y, rfl⟩ := f.surjective x, obtain ⟨z, rfl⟩ := ideal.quotient.mk_surjective y, exact ⟨z, Ξ» i hi, rfl⟩ end /-- Corollary of the Chinese remainder theorem: given elements `x i : R`, we can choose a representative `y : R` such that `y - x i ∈ P i ^ e i`.-/ lemma is_dedekind_domain.exists_forall_sub_mem_ideal {ΞΉ : Type*} {s : finset ΞΉ} (P : ΞΉ β†’ ideal R) (e : ΞΉ β†’ β„•) (prime : βˆ€ i ∈ s, prime (P i)) (coprime : βˆ€ (i j ∈ s), i β‰  j β†’ P i β‰  P j) (x : s β†’ R) : βˆƒ y, βˆ€ i (hi : i ∈ s), y - x ⟨i, hi⟩ ∈ P i ^ e i := begin obtain ⟨y, hy⟩ := is_dedekind_domain.exists_representative_mod_finset P e prime coprime (Ξ» i, ideal.quotient.mk _ (x i)), exact ⟨y, Ξ» i hi, ideal.quotient.eq.mp (hy i hi)⟩ end end dedekind_domain end chinese_remainder section PID open multiplicity unique_factorization_monoid ideal variables {R} [is_domain R] [is_principal_ideal_ring R] lemma span_singleton_dvd_span_singleton_iff_dvd {a b : R} : (ideal.span {a}) ∣ (ideal.span ({b} : set R)) ↔ a ∣ b := ⟨λ h, mem_span_singleton.mp (dvd_iff_le.mp h (mem_span_singleton.mpr (dvd_refl b))), Ξ» h, dvd_iff_le.mpr (Ξ» d hd, mem_span_singleton.mpr (dvd_trans h (mem_span_singleton.mp hd)))⟩ lemma singleton_span_mem_normalized_factors_of_mem_normalized_factors [normalization_monoid R] [decidable_eq R] [decidable_eq (ideal R)] {a b : R} (ha : a ∈ normalized_factors b) : ideal.span ({a} : set R) ∈ normalized_factors (ideal.span ({b} : set R)) := begin by_cases hb : b = 0, { rw [ideal.span_singleton_eq_bot.mpr hb, bot_eq_zero, normalized_factors_zero], rw [hb, normalized_factors_zero] at ha, simpa only [multiset.not_mem_zero] }, { suffices : prime (ideal.span ({a} : set R)), { obtain ⟨c, hc, hc'⟩ := exists_mem_normalized_factors_of_dvd _ this.irreducible (dvd_iff_le.mpr (span_singleton_le_span_singleton.mpr (dvd_of_mem_normalized_factors ha))), rwa associated_iff_eq.mp hc', { by_contra, exact hb (span_singleton_eq_bot.mp h) } }, rw prime_iff_is_prime, exact (span_singleton_prime (prime_of_normalized_factor a ha).ne_zero).mpr (prime_of_normalized_factor a ha), by_contra, exact (prime_of_normalized_factor a ha).ne_zero (span_singleton_eq_bot.mp h) }, end lemma multiplicity_eq_multiplicity_span [decidable_rel ((∣) : R β†’ R β†’ Prop)] [decidable_rel ((∣) : ideal R β†’ ideal R β†’ Prop)] {a b : R} : multiplicity (ideal.span {a}) (ideal.span ({b} : set R)) = multiplicity a b := begin by_cases h : finite a b, { rw ← part_enat.coe_get (finite_iff_dom.mp h), refine (multiplicity.unique (show (ideal.span {a})^(((multiplicity a b).get h)) ∣ (ideal.span {b}), from _) _).symm ; rw [ideal.span_singleton_pow, span_singleton_dvd_span_singleton_iff_dvd], exact pow_multiplicity_dvd h , { exact multiplicity.is_greatest ((part_enat.lt_coe_iff _ _).mpr (exists.intro (finite_iff_dom.mp h) (nat.lt_succ_self _))) } }, { suffices : Β¬ (finite (ideal.span ({a} : set R)) (ideal.span ({b} : set R))), { rw [finite_iff_dom, part_enat.not_dom_iff_eq_top] at h this, rw [h, this] }, refine not_finite_iff_forall.mpr (Ξ» n, by {rw [ideal.span_singleton_pow, span_singleton_dvd_span_singleton_iff_dvd], exact not_finite_iff_forall.mp h n }) } end variables [decidable_eq R] [decidable_eq (ideal R)] [normalization_monoid R] /-- The bijection between the (normalized) prime factors of `r` and the (normalized) prime factors of `span {r}` -/ @[simps] noncomputable def normalized_factors_equiv_span_normalized_factors {r : R} (hr : r β‰  0) : {d : R | d ∈ normalized_factors r} ≃ {I : ideal R | I ∈ normalized_factors (ideal.span ({r} : set R))} := equiv.of_bijective (Ξ» d, ⟨ideal.span {↑d}, singleton_span_mem_normalized_factors_of_mem_normalized_factors d.prop⟩) begin split, { rintros ⟨a, ha⟩ ⟨b, hb⟩ h, rw [subtype.mk_eq_mk, ideal.span_singleton_eq_span_singleton, subtype.coe_mk, subtype.coe_mk] at h, exact subtype.mk_eq_mk.mpr (mem_normalized_factors_eq_of_associated ha hb h) }, { rintros ⟨i, hi⟩, letI : i.is_principal := infer_instance, letI : i.is_prime := is_prime_of_prime (prime_of_normalized_factor i hi), obtain ⟨a, ha, ha'⟩ := exists_mem_normalized_factors_of_dvd hr (submodule.is_principal.prime_generator_of_is_prime i (prime_of_normalized_factor i hi).ne_zero).irreducible _, { use ⟨a, ha⟩, simp only [subtype.coe_mk, subtype.mk_eq_mk, ← span_singleton_eq_span_singleton.mpr ha', ideal.span_singleton_generator] }, {exact (submodule.is_principal.mem_iff_generator_dvd i).mp (((show ideal.span {r} ≀ i, from dvd_iff_le.mp (dvd_of_mem_normalized_factors hi))) (mem_span_singleton.mpr (dvd_refl r))) } } end variables [decidable_rel ((∣) : R β†’ R β†’ Prop)] [decidable_rel ((∣) : ideal R β†’ ideal R β†’ Prop)] /-- The bijection `normalized_factors_equiv_span_normalized_factors` between the set of prime factors of `r` and the set of prime factors of the ideal `⟨r⟩` preserves multiplicities. -/ lemma multiplicity_normalized_factors_equiv_span_normalized_factors_eq_multiplicity {r d: R} (hr : r β‰  0) (hd : d ∈ normalized_factors r) : multiplicity d r = multiplicity (normalized_factors_equiv_span_normalized_factors hr ⟨d, hd⟩ : ideal R) (ideal.span {r}) := by simp only [normalized_factors_equiv_span_normalized_factors, multiplicity_eq_multiplicity_span, subtype.coe_mk, equiv.of_bijective_apply] /-- The bijection `normalized_factors_equiv_span_normalized_factors.symm` between the set of prime factors of the ideal `⟨r⟩` and the set of prime factors of `r` preserves multiplicities. -/ lemma multiplicity_normalized_factors_equiv_span_normalized_factors_symm_eq_multiplicity {r : R} (hr : r β‰  0) (I : {I : ideal R | I ∈ normalized_factors (ideal.span ({r} : set R))}) : multiplicity ((normalized_factors_equiv_span_normalized_factors hr).symm I : R) r = multiplicity (I : ideal R) (ideal.span {r}) := begin obtain ⟨x, hx⟩ := (normalized_factors_equiv_span_normalized_factors hr).surjective I, obtain ⟨a, ha⟩ := x, rw [hx.symm, equiv.symm_apply_apply, subtype.coe_mk, multiplicity_normalized_factors_equiv_span_normalized_factors_eq_multiplicity hr ha, hx], end end PID
a359ba89b32423ac22a9933699e406a3e924d0ca
86f6f4f8d827a196a32bfc646234b73328aeb306
/examples/logic/unnamed_2179.lean
bcafa61e52cc52016215fe1129384d70ab2204e9
[]
no_license
jamescheuk91/mathematics_in_lean
09f1f87d2b0dce53464ff0cbe592c568ff59cf5e
4452499264e2975bca2f42565c0925506ba5dda3
refs/heads/master
1,679,716,410,967
1,613,957,947,000
1,613,957,947,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
138
lean
import data.real.basic -- BEGIN def converges_to (s : β„• β†’ ℝ) (a : ℝ) := βˆ€ Ξ΅ > 0, βˆƒ N, βˆ€ n β‰₯ N, abs (s n - a) < Ξ΅ -- END
ddbdea5b48a6c76e94efa5e97be7708010a35abe
c777c32c8e484e195053731103c5e52af26a25d1
/src/analysis/calculus/mean_value.lean
4238fa2836b406a1042067339034ebcec0f1d90c
[ "Apache-2.0" ]
permissive
kbuzzard/mathlib
2ff9e85dfe2a46f4b291927f983afec17e946eb8
58537299e922f9c77df76cb613910914a479c1f7
refs/heads/master
1,685,313,702,744
1,683,974,212,000
1,683,974,212,000
128,185,277
1
0
null
1,522,920,600,000
1,522,920,600,000
null
UTF-8
Lean
false
false
74,794
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, Yury Kudryashov -/ import analysis.calculus.local_extr import analysis.convex.slope import analysis.convex.normed import data.is_R_or_C.basic import topology.instances.real_vector_space /-! # The mean value inequality and equalities In this file we prove the following facts: * `convex.norm_image_sub_le_of_norm_deriv_le` : if `f` is differentiable on a convex set `s` and the norm of its derivative is bounded by `C`, then `f` is Lipschitz continuous on `s` with constant `C`; also a variant in which what is bounded by `C` is the norm of the difference of the derivative from a fixed linear map. This lemma and its versions are formulated using `is_R_or_C`, so they work both for real and complex derivatives. * `image_le_of*`, `image_norm_le_of_*` : several similar lemmas deducing `f x ≀ B x` or `β€–f xβ€– ≀ B x` from upper estimates on `f'` or `β€–f'β€–`, respectively. These lemmas differ by their assumptions: * `of_liminf_*` lemmas assume that limit inferior of some ratio is less than `B' x`; * `of_deriv_right_*`, `of_norm_deriv_right_*` lemmas assume that the right derivative or its norm is less than `B' x`; * `of_*_lt_*` lemmas assume a strict inequality whenever `f x = B x` or `β€–f xβ€– = B x`; * `of_*_le_*` lemmas assume a non-strict inequality everywhere on `[a, b)`; * name of a lemma ends with `'` if (1) it assumes that `B` is continuous on `[a, b]` and has a right derivative at every point of `[a, b)`, and (2) the lemma has a counterpart assuming that `B` is differentiable everywhere on `ℝ` * `norm_image_sub_le_*_segment` : if derivative of `f` on `[a, b]` is bounded above by a constant `C`, then `β€–f x - f aβ€– ≀ C * β€–x - aβ€–`; several versions deal with right derivative and derivative within `[a, b]` (`has_deriv_within_at` or `deriv_within`). * `convex.is_const_of_fderiv_within_eq_zero` : if a function has derivative `0` on a convex set `s`, then it is a constant on `s`. * `exists_ratio_has_deriv_at_eq_ratio_slope` and `exists_ratio_deriv_eq_ratio_slope` : Cauchy's Mean Value Theorem. * `exists_has_deriv_at_eq_slope` and `exists_deriv_eq_slope` : Lagrange's Mean Value Theorem. * `domain_mvt` : Lagrange's Mean Value Theorem, applied to a segment in a convex domain. * `convex.image_sub_lt_mul_sub_of_deriv_lt`, `convex.mul_sub_lt_image_sub_of_lt_deriv`, `convex.image_sub_le_mul_sub_of_deriv_le`, `convex.mul_sub_le_image_sub_of_le_deriv`, if `βˆ€ x, C (</≀/>/β‰₯) (f' x)`, then `C * (y - x) (</≀/>/β‰₯) (f y - f x)` whenever `x < y`. * `convex.monotone_on_of_deriv_nonneg`, `convex.antitone_on_of_deriv_nonpos`, `convex.strict_mono_of_deriv_pos`, `convex.strict_anti_of_deriv_neg` : if the derivative of a function is non-negative/non-positive/positive/negative, then the function is monotone/antitone/strictly monotone/strictly monotonically decreasing. * `convex_on_of_deriv_monotone_on`, `convex_on_of_deriv2_nonneg` : if the derivative of a function is increasing or its second derivative is nonnegative, then the original function is convex. * `strict_fderiv_of_cont_diff` : a C^1 function over the reals is strictly differentiable. (This is a corollary of the mean value inequality.) -/ variables {E : Type*} [normed_add_comm_group E] [normed_space ℝ E] {F : Type*} [normed_add_comm_group F] [normed_space ℝ F] open metric set asymptotics continuous_linear_map filter open_locale classical topology nnreal /-! ### One-dimensional fencing inequalities -/ /-- General fencing theorem for continuous functions with an estimate on the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `f a ≀ B a`; * `B` has right derivative `B'` at every point of `[a, b)`; * for each `x ∈ [a, b)` the right-side limit inferior of `(f z - f x) / (z - x)` is bounded above by a function `f'`; * we have `f' x < B' x` whenever `f x = B x`. Then `f x ≀ B x` everywhere on `[a, b]`. -/ lemma image_le_of_liminf_slope_right_lt_deriv_boundary' {f f' : ℝ β†’ ℝ} {a b : ℝ} (hf : continuous_on f (Icc a b)) -- `hf'` actually says `liminf (f z - f x) / (z - x) ≀ f' x` (hf' : βˆ€ x ∈ Ico a b, βˆ€ r, f' x < r β†’ βˆƒαΆ  z in 𝓝[>] x, slope f x z < r) {B B' : ℝ β†’ ℝ} (ha : f a ≀ B a) (hB : continuous_on B (Icc a b)) (hB' : βˆ€ x ∈ Ico a b, has_deriv_within_at B (B' x) (Ici x) x) (bound : βˆ€ x ∈ Ico a b, f x = B x β†’ f' x < B' x) : βˆ€ ⦃x⦄, x ∈ Icc a b β†’ f x ≀ B x := begin change Icc a b βŠ† {x | f x ≀ B x}, set s := {x | f x ≀ B x} ∩ Icc a b, have A : continuous_on (Ξ» x, (f x, B x)) (Icc a b), from hf.prod hB, have : is_closed s, { simp only [s, inter_comm], exact A.preimage_closed_of_closed is_closed_Icc order_closed_topology.is_closed_le' }, apply this.Icc_subset_of_forall_exists_gt ha, rintros x ⟨hxB : f x ≀ B x, xab⟩ y hy, cases hxB.lt_or_eq with hxB hxB, { -- If `f x < B x`, then all we need is continuity of both sides refine nonempty_of_mem (inter_mem _ (Ioc_mem_nhds_within_Ioi ⟨le_rfl, hy⟩)), have : βˆ€αΆ  x in 𝓝[Icc a b] x, f x < B x, from A x (Ico_subset_Icc_self xab) (is_open.mem_nhds (is_open_lt continuous_fst continuous_snd) hxB), have : βˆ€αΆ  x in 𝓝[>] x, f x < B x, from nhds_within_le_of_mem (Icc_mem_nhds_within_Ioi xab) this, exact this.mono (Ξ» y, le_of_lt) }, { rcases exists_between (bound x xab hxB) with ⟨r, hfr, hrB⟩, specialize hf' x xab r hfr, have HB : βˆ€αΆ  z in 𝓝[>] x, r < slope B x z, from (has_deriv_within_at_iff_tendsto_slope' $ lt_irrefl x).1 (hB' x xab).Ioi_of_Ici (Ioi_mem_nhds hrB), obtain ⟨z, hfz, hzB, hz⟩ : βˆƒ z, slope f x z < r ∧ r < slope B x z ∧ z ∈ Ioc x y, from (hf'.and_eventually (HB.and (Ioc_mem_nhds_within_Ioi ⟨le_rfl, hy⟩))).exists, refine ⟨z, _, hz⟩, have := (hfz.trans hzB).le, rwa [slope_def_field, slope_def_field, div_le_div_right (sub_pos.2 hz.1), hxB, sub_le_sub_iff_right] at this } end /-- General fencing theorem for continuous functions with an estimate on the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `f a ≀ B a`; * `B` has derivative `B'` everywhere on `ℝ`; * for each `x ∈ [a, b)` the right-side limit inferior of `(f z - f x) / (z - x)` is bounded above by a function `f'`; * we have `f' x < B' x` whenever `f x = B x`. Then `f x ≀ B x` everywhere on `[a, b]`. -/ lemma image_le_of_liminf_slope_right_lt_deriv_boundary {f f' : ℝ β†’ ℝ} {a b : ℝ} (hf : continuous_on f (Icc a b)) -- `hf'` actually says `liminf (f z - f x) / (z - x) ≀ f' x` (hf' : βˆ€ x ∈ Ico a b, βˆ€ r, f' x < r β†’ βˆƒαΆ  z in 𝓝[>] x, slope f x z < r) {B B' : ℝ β†’ ℝ} (ha : f a ≀ B a) (hB : βˆ€ x, has_deriv_at B (B' x) x) (bound : βˆ€ x ∈ Ico a b, f x = B x β†’ f' x < B' x) : βˆ€ ⦃x⦄, x ∈ Icc a b β†’ f x ≀ B x := image_le_of_liminf_slope_right_lt_deriv_boundary' hf hf' ha (Ξ» x hx, (hB x).continuous_at.continuous_within_at) (Ξ» x hx, (hB x).has_deriv_within_at) bound /-- General fencing theorem for continuous functions with an estimate on the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `f a ≀ B a`; * `B` has right derivative `B'` at every point of `[a, b)`; * for each `x ∈ [a, b)` the right-side limit inferior of `(f z - f x) / (z - x)` is bounded above by `B'`. Then `f x ≀ B x` everywhere on `[a, b]`. -/ lemma image_le_of_liminf_slope_right_le_deriv_boundary {f : ℝ β†’ ℝ} {a b : ℝ} (hf : continuous_on f (Icc a b)) {B B' : ℝ β†’ ℝ} (ha : f a ≀ B a) (hB : continuous_on B (Icc a b)) (hB' : βˆ€ x ∈ Ico a b, has_deriv_within_at B (B' x) (Ici x) x) -- `bound` actually says `liminf (f z - f x) / (z - x) ≀ B' x` (bound : βˆ€ x ∈ Ico a b, βˆ€ r, B' x < r β†’ βˆƒαΆ  z in 𝓝[>] x, slope f x z < r) : βˆ€ ⦃x⦄, x ∈ Icc a b β†’ f x ≀ B x := begin have Hr : βˆ€ x ∈ Icc a b, βˆ€ r > 0, f x ≀ B x + r * (x - a), { intros x hx r hr, apply image_le_of_liminf_slope_right_lt_deriv_boundary' hf bound, { rwa [sub_self, mul_zero, add_zero] }, { exact hB.add (continuous_on_const.mul (continuous_id.continuous_on.sub continuous_on_const)) }, { assume x hx, exact (hB' x hx).add (((has_deriv_within_at_id x (Ici x)).sub_const a).const_mul r) }, { assume x hx _, rw [mul_one], exact (lt_add_iff_pos_right _).2 hr }, exact hx }, assume x hx, have : continuous_within_at (Ξ» r, B x + r * (x - a)) (Ioi 0) 0, from continuous_within_at_const.add (continuous_within_at_id.mul continuous_within_at_const), convert continuous_within_at_const.closure_le _ this (Hr x hx); simp end /-- General fencing theorem for continuous functions with an estimate on the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `f a ≀ B a`; * `B` has right derivative `B'` at every point of `[a, b)`; * `f` has right derivative `f'` at every point of `[a, b)`; * we have `f' x < B' x` whenever `f x = B x`. Then `f x ≀ B x` everywhere on `[a, b]`. -/ lemma image_le_of_deriv_right_lt_deriv_boundary' {f f' : ℝ β†’ ℝ} {a b : ℝ} (hf : continuous_on f (Icc a b)) (hf' : βˆ€ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ici x) x) {B B' : ℝ β†’ ℝ} (ha : f a ≀ B a) (hB : continuous_on B (Icc a b)) (hB' : βˆ€ x ∈ Ico a b, has_deriv_within_at B (B' x) (Ici x) x) (bound : βˆ€ x ∈ Ico a b, f x = B x β†’ f' x < B' x) : βˆ€ ⦃x⦄, x ∈ Icc a b β†’ f x ≀ B x := image_le_of_liminf_slope_right_lt_deriv_boundary' hf (Ξ» x hx r hr, (hf' x hx).liminf_right_slope_le hr) ha hB hB' bound /-- General fencing theorem for continuous functions with an estimate on the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `f a ≀ B a`; * `B` has derivative `B'` everywhere on `ℝ`; * `f` has right derivative `f'` at every point of `[a, b)`; * we have `f' x < B' x` whenever `f x = B x`. Then `f x ≀ B x` everywhere on `[a, b]`. -/ lemma image_le_of_deriv_right_lt_deriv_boundary {f f' : ℝ β†’ ℝ} {a b : ℝ} (hf : continuous_on f (Icc a b)) (hf' : βˆ€ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ici x) x) {B B' : ℝ β†’ ℝ} (ha : f a ≀ B a) (hB : βˆ€ x, has_deriv_at B (B' x) x) (bound : βˆ€ x ∈ Ico a b, f x = B x β†’ f' x < B' x) : βˆ€ ⦃x⦄, x ∈ Icc a b β†’ f x ≀ B x := image_le_of_deriv_right_lt_deriv_boundary' hf hf' ha (Ξ» x hx, (hB x).continuous_at.continuous_within_at) (Ξ» x hx, (hB x).has_deriv_within_at) bound /-- General fencing theorem for continuous functions with an estimate on the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `f a ≀ B a`; * `B` has derivative `B'` everywhere on `ℝ`; * `f` has right derivative `f'` at every point of `[a, b)`; * we have `f' x ≀ B' x` on `[a, b)`. Then `f x ≀ B x` everywhere on `[a, b]`. -/ lemma image_le_of_deriv_right_le_deriv_boundary {f f' : ℝ β†’ ℝ} {a b : ℝ} (hf : continuous_on f (Icc a b)) (hf' : βˆ€ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ici x) x) {B B' : ℝ β†’ ℝ} (ha : f a ≀ B a) (hB : continuous_on B (Icc a b)) (hB' : βˆ€ x ∈ Ico a b, has_deriv_within_at B (B' x) (Ici x) x) (bound : βˆ€ x ∈ Ico a b, f' x ≀ B' x) : βˆ€ ⦃x⦄, x ∈ Icc a b β†’ f x ≀ B x := image_le_of_liminf_slope_right_le_deriv_boundary hf ha hB hB' $ assume x hx r hr, (hf' x hx).liminf_right_slope_le (lt_of_le_of_lt (bound x hx) hr) /-! ### Vector-valued functions `f : ℝ β†’ E` -/ section variables {f : ℝ β†’ E} {a b : ℝ} /-- General fencing theorem for continuous functions with an estimate on the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `β€–f aβ€– ≀ B a`; * `B` has right derivative at every point of `[a, b)`; * for each `x ∈ [a, b)` the right-side limit inferior of `(β€–f zβ€– - β€–f xβ€–) / (z - x)` is bounded above by a function `f'`; * we have `f' x < B' x` whenever `β€–f xβ€– = B x`. Then `β€–f xβ€– ≀ B x` everywhere on `[a, b]`. -/ lemma image_norm_le_of_liminf_right_slope_norm_lt_deriv_boundary {E : Type*} [normed_add_comm_group E] {f : ℝ β†’ E} {f' : ℝ β†’ ℝ} (hf : continuous_on f (Icc a b)) -- `hf'` actually says `liminf (β€–f zβ€– - β€–f xβ€–) / (z - x) ≀ f' x` (hf' : βˆ€ x ∈ Ico a b, βˆ€ r, f' x < r β†’ βˆƒαΆ  z in 𝓝[>] x, slope (norm ∘ f) x z < r) {B B' : ℝ β†’ ℝ} (ha : β€–f aβ€– ≀ B a) (hB : continuous_on B (Icc a b)) (hB' : βˆ€ x ∈ Ico a b, has_deriv_within_at B (B' x) (Ici x) x) (bound : βˆ€ x ∈ Ico a b, β€–f xβ€– = B x β†’ f' x < B' x) : βˆ€ ⦃x⦄, x ∈ Icc a b β†’ β€–f xβ€– ≀ B x := image_le_of_liminf_slope_right_lt_deriv_boundary' (continuous_norm.comp_continuous_on hf) hf' ha hB hB' bound /-- General fencing theorem for continuous functions with an estimate on the norm of the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `β€–f aβ€– ≀ B a`; * `f` and `B` have right derivatives `f'` and `B'` respectively at every point of `[a, b)`; * the norm of `f'` is strictly less than `B'` whenever `β€–f xβ€– = B x`. Then `β€–f xβ€– ≀ B x` everywhere on `[a, b]`. We use one-sided derivatives in the assumptions to make this theorem work for piecewise differentiable functions. -/ lemma image_norm_le_of_norm_deriv_right_lt_deriv_boundary' {f' : ℝ β†’ E} (hf : continuous_on f (Icc a b)) (hf' : βˆ€ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ici x) x) {B B' : ℝ β†’ ℝ} (ha : β€–f aβ€– ≀ B a) (hB : continuous_on B (Icc a b)) (hB' : βˆ€ x ∈ Ico a b, has_deriv_within_at B (B' x) (Ici x) x) (bound : βˆ€ x ∈ Ico a b, β€–f xβ€– = B x β†’ β€–f' xβ€– < B' x) : βˆ€ ⦃x⦄, x ∈ Icc a b β†’ β€–f xβ€– ≀ B x := image_norm_le_of_liminf_right_slope_norm_lt_deriv_boundary hf (Ξ» x hx r hr, (hf' x hx).liminf_right_slope_norm_le hr) ha hB hB' bound /-- General fencing theorem for continuous functions with an estimate on the norm of the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `β€–f aβ€– ≀ B a`; * `f` has right derivative `f'` at every point of `[a, b)`; * `B` has derivative `B'` everywhere on `ℝ`; * the norm of `f'` is strictly less than `B'` whenever `β€–f xβ€– = B x`. Then `β€–f xβ€– ≀ B x` everywhere on `[a, b]`. We use one-sided derivatives in the assumptions to make this theorem work for piecewise differentiable functions. -/ lemma image_norm_le_of_norm_deriv_right_lt_deriv_boundary {f' : ℝ β†’ E} (hf : continuous_on f (Icc a b)) (hf' : βˆ€ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ici x) x) {B B' : ℝ β†’ ℝ} (ha : β€–f aβ€– ≀ B a) (hB : βˆ€ x, has_deriv_at B (B' x) x) (bound : βˆ€ x ∈ Ico a b, β€–f xβ€– = B x β†’ β€–f' xβ€– < B' x) : βˆ€ ⦃x⦄, x ∈ Icc a b β†’ β€–f xβ€– ≀ B x := image_norm_le_of_norm_deriv_right_lt_deriv_boundary' hf hf' ha (Ξ» x hx, (hB x).continuous_at.continuous_within_at) (Ξ» x hx, (hB x).has_deriv_within_at) bound /-- General fencing theorem for continuous functions with an estimate on the norm of the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `β€–f aβ€– ≀ B a`; * `f` and `B` have right derivatives `f'` and `B'` respectively at every point of `[a, b)`; * we have `β€–f' xβ€– ≀ B x` everywhere on `[a, b)`. Then `β€–f xβ€– ≀ B x` everywhere on `[a, b]`. We use one-sided derivatives in the assumptions to make this theorem work for piecewise differentiable functions. -/ lemma image_norm_le_of_norm_deriv_right_le_deriv_boundary' {f' : ℝ β†’ E} (hf : continuous_on f (Icc a b)) (hf' : βˆ€ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ici x) x) {B B' : ℝ β†’ ℝ} (ha : β€–f aβ€– ≀ B a) (hB : continuous_on B (Icc a b)) (hB' : βˆ€ x ∈ Ico a b, has_deriv_within_at B (B' x) (Ici x) x) (bound : βˆ€ x ∈ Ico a b, β€–f' xβ€– ≀ B' x) : βˆ€ ⦃x⦄, x ∈ Icc a b β†’ β€–f xβ€– ≀ B x := image_le_of_liminf_slope_right_le_deriv_boundary (continuous_norm.comp_continuous_on hf) ha hB hB' $ (Ξ» x hx r hr, (hf' x hx).liminf_right_slope_norm_le (lt_of_le_of_lt (bound x hx) hr)) /-- General fencing theorem for continuous functions with an estimate on the norm of the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `β€–f aβ€– ≀ B a`; * `f` has right derivative `f'` at every point of `[a, b)`; * `B` has derivative `B'` everywhere on `ℝ`; * we have `β€–f' xβ€– ≀ B x` everywhere on `[a, b)`. Then `β€–f xβ€– ≀ B x` everywhere on `[a, b]`. We use one-sided derivatives in the assumptions to make this theorem work for piecewise differentiable functions. -/ lemma image_norm_le_of_norm_deriv_right_le_deriv_boundary {f' : ℝ β†’ E} (hf : continuous_on f (Icc a b)) (hf' : βˆ€ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ici x) x) {B B' : ℝ β†’ ℝ} (ha : β€–f aβ€– ≀ B a) (hB : βˆ€ x, has_deriv_at B (B' x) x) (bound : βˆ€ x ∈ Ico a b, β€–f' xβ€– ≀ B' x) : βˆ€ ⦃x⦄, x ∈ Icc a b β†’ β€–f xβ€– ≀ B x := image_norm_le_of_norm_deriv_right_le_deriv_boundary' hf hf' ha (Ξ» x hx, (hB x).continuous_at.continuous_within_at) (Ξ» x hx, (hB x).has_deriv_within_at) bound /-- A function on `[a, b]` with the norm of the right derivative bounded by `C` satisfies `β€–f x - f aβ€– ≀ C * (x - a)`. -/ theorem norm_image_sub_le_of_norm_deriv_right_le_segment {f' : ℝ β†’ E} {C : ℝ} (hf : continuous_on f (Icc a b)) (hf' : βˆ€ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ici x) x) (bound : βˆ€x ∈ Ico a b, β€–f' xβ€– ≀ C) : βˆ€ x ∈ Icc a b, β€–f x - f aβ€– ≀ C * (x - a) := begin let g := Ξ» x, f x - f a, have hg : continuous_on g (Icc a b), from hf.sub continuous_on_const, have hg' : βˆ€ x ∈ Ico a b, has_deriv_within_at g (f' x) (Ici x) x, { assume x hx, simpa using (hf' x hx).sub (has_deriv_within_at_const _ _ _) }, let B := Ξ» x, C * (x - a), have hB : βˆ€ x, has_deriv_at B C x, { assume x, simpa using (has_deriv_at_const x C).mul ((has_deriv_at_id x).sub (has_deriv_at_const x a)) }, convert image_norm_le_of_norm_deriv_right_le_deriv_boundary hg hg' _ hB bound, simp only [g, B], rw [sub_self, norm_zero, sub_self, mul_zero] end /-- A function on `[a, b]` with the norm of the derivative within `[a, b]` bounded by `C` satisfies `β€–f x - f aβ€– ≀ C * (x - a)`, `has_deriv_within_at` version. -/ theorem norm_image_sub_le_of_norm_deriv_le_segment' {f' : ℝ β†’ E} {C : ℝ} (hf : βˆ€ x ∈ Icc a b, has_deriv_within_at f (f' x) (Icc a b) x) (bound : βˆ€x ∈ Ico a b, β€–f' xβ€– ≀ C) : βˆ€ x ∈ Icc a b, β€–f x - f aβ€– ≀ C * (x - a) := begin refine norm_image_sub_le_of_norm_deriv_right_le_segment (Ξ» x hx, (hf x hx).continuous_within_at) (Ξ» x hx, _) bound, exact (hf x $ Ico_subset_Icc_self hx).nhds_within (Icc_mem_nhds_within_Ici hx) end /-- A function on `[a, b]` with the norm of the derivative within `[a, b]` bounded by `C` satisfies `β€–f x - f aβ€– ≀ C * (x - a)`, `deriv_within` version. -/ theorem norm_image_sub_le_of_norm_deriv_le_segment {C : ℝ} (hf : differentiable_on ℝ f (Icc a b)) (bound : βˆ€x ∈ Ico a b, β€–deriv_within f (Icc a b) xβ€– ≀ C) : βˆ€ x ∈ Icc a b, β€–f x - f aβ€– ≀ C * (x - a) := begin refine norm_image_sub_le_of_norm_deriv_le_segment' _ bound, exact Ξ» x hx, (hf x hx).has_deriv_within_at end /-- A function on `[0, 1]` with the norm of the derivative within `[0, 1]` bounded by `C` satisfies `β€–f 1 - f 0β€– ≀ C`, `has_deriv_within_at` version. -/ theorem norm_image_sub_le_of_norm_deriv_le_segment_01' {f' : ℝ β†’ E} {C : ℝ} (hf : βˆ€ x ∈ Icc (0:ℝ) 1, has_deriv_within_at f (f' x) (Icc (0:ℝ) 1) x) (bound : βˆ€x ∈ Ico (0:ℝ) 1, β€–f' xβ€– ≀ C) : β€–f 1 - f 0β€– ≀ C := by simpa only [sub_zero, mul_one] using norm_image_sub_le_of_norm_deriv_le_segment' hf bound 1 (right_mem_Icc.2 zero_le_one) /-- A function on `[0, 1]` with the norm of the derivative within `[0, 1]` bounded by `C` satisfies `β€–f 1 - f 0β€– ≀ C`, `deriv_within` version. -/ theorem norm_image_sub_le_of_norm_deriv_le_segment_01 {C : ℝ} (hf : differentiable_on ℝ f (Icc (0:ℝ) 1)) (bound : βˆ€x ∈ Ico (0:ℝ) 1, β€–deriv_within f (Icc (0:ℝ) 1) xβ€– ≀ C) : β€–f 1 - f 0β€– ≀ C := by simpa only [sub_zero, mul_one] using norm_image_sub_le_of_norm_deriv_le_segment hf bound 1 (right_mem_Icc.2 zero_le_one) theorem constant_of_has_deriv_right_zero (hcont : continuous_on f (Icc a b)) (hderiv : βˆ€ x ∈ Ico a b, has_deriv_within_at f 0 (Ici x) x) : βˆ€ x ∈ Icc a b, f x = f a := by simpa only [zero_mul, norm_le_zero_iff, sub_eq_zero] using Ξ» x hx, norm_image_sub_le_of_norm_deriv_right_le_segment hcont hderiv (Ξ» y hy, by rw norm_le_zero_iff) x hx theorem constant_of_deriv_within_zero (hdiff : differentiable_on ℝ f (Icc a b)) (hderiv : βˆ€ x ∈ Ico a b, deriv_within f (Icc a b) x = 0) : βˆ€ x ∈ Icc a b, f x = f a := begin have H : βˆ€ x ∈ Ico a b, β€–deriv_within f (Icc a b) xβ€– ≀ 0 := by simpa only [norm_le_zero_iff] using Ξ» x hx, hderiv x hx, simpa only [zero_mul, norm_le_zero_iff, sub_eq_zero] using Ξ» x hx, norm_image_sub_le_of_norm_deriv_le_segment hdiff H x hx, end variables {f' g : ℝ β†’ E} /-- If two continuous functions on `[a, b]` have the same right derivative and are equal at `a`, then they are equal everywhere on `[a, b]`. -/ theorem eq_of_has_deriv_right_eq (derivf : βˆ€ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ici x) x) (derivg : βˆ€ x ∈ Ico a b, has_deriv_within_at g (f' x) (Ici x) x) (fcont : continuous_on f (Icc a b)) (gcont : continuous_on g (Icc a b)) (hi : f a = g a) : βˆ€ y ∈ Icc a b, f y = g y := begin simp only [← @sub_eq_zero _ _ (f _)] at hi ⊒, exact hi β–Έ constant_of_has_deriv_right_zero (fcont.sub gcont) (Ξ» y hy, by simpa only [sub_self] using (derivf y hy).sub (derivg y hy)), end /-- If two differentiable functions on `[a, b]` have the same derivative within `[a, b]` everywhere on `[a, b)` and are equal at `a`, then they are equal everywhere on `[a, b]`. -/ theorem eq_of_deriv_within_eq (fdiff : differentiable_on ℝ f (Icc a b)) (gdiff : differentiable_on ℝ g (Icc a b)) (hderiv : eq_on (deriv_within f (Icc a b)) (deriv_within g (Icc a b)) (Ico a b)) (hi : f a = g a) : βˆ€ y ∈ Icc a b, f y = g y := begin have A : βˆ€ y ∈ Ico a b, has_deriv_within_at f (deriv_within f (Icc a b) y) (Ici y) y := Ξ» y hy, (fdiff y (mem_Icc_of_Ico hy)).has_deriv_within_at.nhds_within (Icc_mem_nhds_within_Ici hy), have B : βˆ€ y ∈ Ico a b, has_deriv_within_at g (deriv_within g (Icc a b) y) (Ici y) y := Ξ» y hy, (gdiff y (mem_Icc_of_Ico hy)).has_deriv_within_at.nhds_within (Icc_mem_nhds_within_Ici hy), exact eq_of_has_deriv_right_eq A (Ξ» y hy, (hderiv hy).symm β–Έ B y hy) fdiff.continuous_on gdiff.continuous_on hi end end /-! ### Vector-valued functions `f : E β†’ G` Theorems in this section work both for real and complex differentiable functions. We use assumptions `[is_R_or_C π•œ] [normed_space π•œ E] [normed_space π•œ G]` to achieve this result. For the domain `E` we also assume `[normed_space ℝ E]` to have a notion of a `convex` set. -/ section variables {π•œ G : Type*} [is_R_or_C π•œ] [normed_space π•œ E] [normed_add_comm_group G] [normed_space π•œ G] namespace convex variables {f : E β†’ G} {C : ℝ} {s : set E} {x y : E} {f' : E β†’ E β†’L[π•œ] G} {Ο† : E β†’L[π•œ] G} /-- The mean value theorem on a convex set: if the derivative of a function is bounded by `C`, then the function is `C`-Lipschitz. Version with `has_fderiv_within`. -/ theorem norm_image_sub_le_of_norm_has_fderiv_within_le (hf : βˆ€ x ∈ s, has_fderiv_within_at f (f' x) s x) (bound : βˆ€x∈s, β€–f' xβ€– ≀ C) (hs : convex ℝ s) (xs : x ∈ s) (ys : y ∈ s) : β€–f y - f xβ€– ≀ C * β€–y - xβ€– := begin letI : normed_space ℝ G := restrict_scalars.normed_space ℝ π•œ G, /- By composition with `t ↦ x + t β€’ (y-x)`, we reduce to a statement for functions defined on `[0,1]`, for which it is proved in `norm_image_sub_le_of_norm_deriv_le_segment`. We just have to check the differentiability of the composition and bounds on its derivative, which is straightforward but tedious for lack of automation. -/ have C0 : 0 ≀ C := le_trans (norm_nonneg _) (bound x xs), set g : ℝ β†’ E := Ξ» t, x + t β€’ (y - x), have Dg : βˆ€ t, has_deriv_at g (y-x) t, { assume t, simpa only [one_smul] using ((has_deriv_at_id t).smul_const (y - x)).const_add x }, have segm : Icc 0 1 βŠ† g ⁻¹' s, { rw [← image_subset_iff, ← segment_eq_image'], apply hs.segment_subset xs ys }, have : f x = f (g 0), by { simp only [g], rw [zero_smul, add_zero] }, rw this, have : f y = f (g 1), by { simp only [g], rw [one_smul, add_sub_cancel'_right] }, rw this, have D2: βˆ€ t ∈ Icc (0:ℝ) 1, has_deriv_within_at (f ∘ g) (f' (g t) (y - x)) (Icc 0 1) t, { intros t ht, have : has_fderiv_within_at f ((f' (g t)).restrict_scalars ℝ) s (g t), from hf (g t) (segm ht), exact this.comp_has_deriv_within_at _ (Dg t).has_deriv_within_at segm }, apply norm_image_sub_le_of_norm_deriv_le_segment_01' D2, refine Ξ» t ht, le_of_op_norm_le _ _ _, exact bound (g t) (segm $ Ico_subset_Icc_self ht) end /-- The mean value theorem on a convex set: if the derivative of a function is bounded by `C` on `s`, then the function is `C`-Lipschitz on `s`. Version with `has_fderiv_within` and `lipschitz_on_with`. -/ theorem lipschitz_on_with_of_nnnorm_has_fderiv_within_le {C : ℝβ‰₯0} (hf : βˆ€ x ∈ s, has_fderiv_within_at f (f' x) s x) (bound : βˆ€x∈s, β€–f' xβ€–β‚Š ≀ C) (hs : convex ℝ s) : lipschitz_on_with C f s := begin rw lipschitz_on_with_iff_norm_sub_le, intros x x_in y y_in, exact hs.norm_image_sub_le_of_norm_has_fderiv_within_le hf bound y_in x_in end /-- Let `s` be a convex set in a real normed vector space `E`, let `f : E β†’ G` be a function differentiable within `s` in a neighborhood of `x : E` with derivative `f'`. Suppose that `f'` is continuous within `s` at `x`. Then for any number `K : ℝβ‰₯0` larger than `β€–f' xβ€–β‚Š`, `f` is `K`-Lipschitz on some neighborhood of `x` within `s`. See also `convex.exists_nhds_within_lipschitz_on_with_of_has_fderiv_within_at` for a version that claims existence of `K` instead of an explicit estimate. -/ lemma exists_nhds_within_lipschitz_on_with_of_has_fderiv_within_at_of_nnnorm_lt (hs : convex ℝ s) {f : E β†’ G} (hder : βˆ€αΆ  y in 𝓝[s] x, has_fderiv_within_at f (f' y) s y) (hcont : continuous_within_at f' s x) (K : ℝβ‰₯0) (hK : β€–f' xβ€–β‚Š < K) : βˆƒ t ∈ 𝓝[s] x, lipschitz_on_with K f t := begin obtain ⟨Ρ, Ξ΅0, hΡ⟩ : βˆƒ Ξ΅ > 0, ball x Ξ΅ ∩ s βŠ† {y | has_fderiv_within_at f (f' y) s y ∧ β€–f' yβ€–β‚Š < K}, from mem_nhds_within_iff.1 (hder.and $ hcont.nnnorm.eventually (gt_mem_nhds hK)), rw inter_comm at hΞ΅, refine ⟨s ∩ ball x Ξ΅, inter_mem_nhds_within _ (ball_mem_nhds _ Ξ΅0), _⟩, exact (hs.inter (convex_ball _ _)).lipschitz_on_with_of_nnnorm_has_fderiv_within_le (Ξ» y hy, (hΞ΅ hy).1.mono (inter_subset_left _ _)) (Ξ» y hy, (hΞ΅ hy).2.le) end /-- Let `s` be a convex set in a real normed vector space `E`, let `f : E β†’ G` be a function differentiable within `s` in a neighborhood of `x : E` with derivative `f'`. Suppose that `f'` is continuous within `s` at `x`. Then for any number `K : ℝβ‰₯0` larger than `β€–f' xβ€–β‚Š`, `f` is Lipschitz on some neighborhood of `x` within `s`. See also `convex.exists_nhds_within_lipschitz_on_with_of_has_fderiv_within_at_of_nnnorm_lt` for a version with an explicit estimate on the Lipschitz constant. -/ lemma exists_nhds_within_lipschitz_on_with_of_has_fderiv_within_at (hs : convex ℝ s) {f : E β†’ G} (hder : βˆ€αΆ  y in 𝓝[s] x, has_fderiv_within_at f (f' y) s y) (hcont : continuous_within_at f' s x) : βˆƒ K (t ∈ 𝓝[s] x), lipschitz_on_with K f t := (exists_gt _).imp $ hs.exists_nhds_within_lipschitz_on_with_of_has_fderiv_within_at_of_nnnorm_lt hder hcont /-- The mean value theorem on a convex set: if the derivative of a function within this set is bounded by `C`, then the function is `C`-Lipschitz. Version with `fderiv_within`. -/ theorem norm_image_sub_le_of_norm_fderiv_within_le (hf : differentiable_on π•œ f s) (bound : βˆ€x∈s, β€–fderiv_within π•œ f s xβ€– ≀ C) (hs : convex ℝ s) (xs : x ∈ s) (ys : y ∈ s) : β€–f y - f xβ€– ≀ C * β€–y - xβ€– := hs.norm_image_sub_le_of_norm_has_fderiv_within_le (Ξ» x hx, (hf x hx).has_fderiv_within_at) bound xs ys /-- The mean value theorem on a convex set: if the derivative of a function is bounded by `C` on `s`, then the function is `C`-Lipschitz on `s`. Version with `fderiv_within` and `lipschitz_on_with`. -/ theorem lipschitz_on_with_of_nnnorm_fderiv_within_le {C : ℝβ‰₯0} (hf : differentiable_on π•œ f s) (bound : βˆ€ x ∈ s, β€–fderiv_within π•œ f s xβ€–β‚Š ≀ C) (hs : convex ℝ s) : lipschitz_on_with C f s:= hs.lipschitz_on_with_of_nnnorm_has_fderiv_within_le (Ξ» x hx, (hf x hx).has_fderiv_within_at) bound /-- The mean value theorem on a convex set: if the derivative of a function is bounded by `C`, then the function is `C`-Lipschitz. Version with `fderiv`. -/ theorem norm_image_sub_le_of_norm_fderiv_le (hf : βˆ€ x ∈ s, differentiable_at π•œ f x) (bound : βˆ€x∈s, β€–fderiv π•œ f xβ€– ≀ C) (hs : convex ℝ s) (xs : x ∈ s) (ys : y ∈ s) : β€–f y - f xβ€– ≀ C * β€–y - xβ€– := hs.norm_image_sub_le_of_norm_has_fderiv_within_le (Ξ» x hx, (hf x hx).has_fderiv_at.has_fderiv_within_at) bound xs ys /-- The mean value theorem on a convex set: if the derivative of a function is bounded by `C` on `s`, then the function is `C`-Lipschitz on `s`. Version with `fderiv` and `lipschitz_on_with`. -/ theorem lipschitz_on_with_of_nnnorm_fderiv_le {C : ℝβ‰₯0} (hf : βˆ€ x ∈ s, differentiable_at π•œ f x) (bound : βˆ€x∈s, β€–fderiv π•œ f xβ€–β‚Š ≀ C) (hs : convex ℝ s) : lipschitz_on_with C f s := hs.lipschitz_on_with_of_nnnorm_has_fderiv_within_le (Ξ» x hx, (hf x hx).has_fderiv_at.has_fderiv_within_at) bound /-- Variant of the mean value inequality on a convex set, using a bound on the difference between the derivative and a fixed linear map, rather than a bound on the derivative itself. Version with `has_fderiv_within`. -/ theorem norm_image_sub_le_of_norm_has_fderiv_within_le' (hf : βˆ€ x ∈ s, has_fderiv_within_at f (f' x) s x) (bound : βˆ€x∈s, β€–f' x - Ο†β€– ≀ C) (hs : convex ℝ s) (xs : x ∈ s) (ys : y ∈ s) : β€–f y - f x - Ο† (y - x)β€– ≀ C * β€–y - xβ€– := begin /- We subtract `Ο†` to define a new function `g` for which `g' = 0`, for which the previous theorem applies, `convex.norm_image_sub_le_of_norm_has_fderiv_within_le`. Then, we just need to glue together the pieces, expressing back `f` in terms of `g`. -/ let g := Ξ»y, f y - Ο† y, have hg : βˆ€ x ∈ s, has_fderiv_within_at g (f' x - Ο†) s x := Ξ» x xs, (hf x xs).sub Ο†.has_fderiv_within_at, calc β€–f y - f x - Ο† (y - x)β€– = β€–f y - f x - (Ο† y - Ο† x)β€– : by simp ... = β€–(f y - Ο† y) - (f x - Ο† x)β€– : by abel ... = β€–g y - g xβ€– : by simp ... ≀ C * β€–y - xβ€– : convex.norm_image_sub_le_of_norm_has_fderiv_within_le hg bound hs xs ys, end /-- Variant of the mean value inequality on a convex set. Version with `fderiv_within`. -/ theorem norm_image_sub_le_of_norm_fderiv_within_le' (hf : differentiable_on π•œ f s) (bound : βˆ€x∈s, β€–fderiv_within π•œ f s x - Ο†β€– ≀ C) (hs : convex ℝ s) (xs : x ∈ s) (ys : y ∈ s) : β€–f y - f x - Ο† (y - x)β€– ≀ C * β€–y - xβ€– := hs.norm_image_sub_le_of_norm_has_fderiv_within_le' (Ξ» x hx, (hf x hx).has_fderiv_within_at) bound xs ys /-- Variant of the mean value inequality on a convex set. Version with `fderiv`. -/ theorem norm_image_sub_le_of_norm_fderiv_le' (hf : βˆ€ x ∈ s, differentiable_at π•œ f x) (bound : βˆ€x∈s, β€–fderiv π•œ f x - Ο†β€– ≀ C) (hs : convex ℝ s) (xs : x ∈ s) (ys : y ∈ s) : β€–f y - f x - Ο† (y - x)β€– ≀ C * β€–y - xβ€– := hs.norm_image_sub_le_of_norm_has_fderiv_within_le' (Ξ» x hx, (hf x hx).has_fderiv_at.has_fderiv_within_at) bound xs ys /-- If a function has zero FrΓ©chet derivative at every point of a convex set, then it is a constant on this set. -/ theorem is_const_of_fderiv_within_eq_zero (hs : convex ℝ s) (hf : differentiable_on π•œ f s) (hf' : βˆ€ x ∈ s, fderiv_within π•œ f s x = 0) (hx : x ∈ s) (hy : y ∈ s) : f x = f y := have bound : βˆ€ x ∈ s, β€–fderiv_within π•œ f s xβ€– ≀ 0, from Ξ» x hx, by simp only [hf' x hx, norm_zero], by simpa only [(dist_eq_norm _ _).symm, zero_mul, dist_le_zero, eq_comm] using hs.norm_image_sub_le_of_norm_fderiv_within_le hf bound hx hy theorem _root_.is_const_of_fderiv_eq_zero (hf : differentiable π•œ f) (hf' : βˆ€ x, fderiv π•œ f x = 0) (x y : E) : f x = f y := convex_univ.is_const_of_fderiv_within_eq_zero hf.differentiable_on (Ξ» x _, by rw fderiv_within_univ; exact hf' x) trivial trivial end convex namespace convex variables {f f' : π•œ β†’ G} {s : set π•œ} {x y : π•œ} /-- The mean value theorem on a convex set in dimension 1: if the derivative of a function is bounded by `C`, then the function is `C`-Lipschitz. Version with `has_deriv_within`. -/ theorem norm_image_sub_le_of_norm_has_deriv_within_le {C : ℝ} (hf : βˆ€ x ∈ s, has_deriv_within_at f (f' x) s x) (bound : βˆ€x∈s, β€–f' xβ€– ≀ C) (hs : convex ℝ s) (xs : x ∈ s) (ys : y ∈ s) : β€–f y - f xβ€– ≀ C * β€–y - xβ€– := convex.norm_image_sub_le_of_norm_has_fderiv_within_le (Ξ» x hx, (hf x hx).has_fderiv_within_at) (Ξ» x hx, le_trans (by simp) (bound x hx)) hs xs ys /-- The mean value theorem on a convex set in dimension 1: if the derivative of a function is bounded by `C` on `s`, then the function is `C`-Lipschitz on `s`. Version with `has_deriv_within` and `lipschitz_on_with`. -/ theorem lipschitz_on_with_of_nnnorm_has_deriv_within_le {C : ℝβ‰₯0} (hs : convex ℝ s) (hf : βˆ€ x ∈ s, has_deriv_within_at f (f' x) s x) (bound : βˆ€x∈s, β€–f' xβ€–β‚Š ≀ C) : lipschitz_on_with C f s := convex.lipschitz_on_with_of_nnnorm_has_fderiv_within_le (Ξ» x hx, (hf x hx).has_fderiv_within_at) (Ξ» x hx, le_trans (by simp) (bound x hx)) hs /-- The mean value theorem on a convex set in dimension 1: if the derivative of a function within this set is bounded by `C`, then the function is `C`-Lipschitz. Version with `deriv_within` -/ theorem norm_image_sub_le_of_norm_deriv_within_le {C : ℝ} (hf : differentiable_on π•œ f s) (bound : βˆ€x∈s, β€–deriv_within f s xβ€– ≀ C) (hs : convex ℝ s) (xs : x ∈ s) (ys : y ∈ s) : β€–f y - f xβ€– ≀ C * β€–y - xβ€– := hs.norm_image_sub_le_of_norm_has_deriv_within_le (Ξ» x hx, (hf x hx).has_deriv_within_at) bound xs ys /-- The mean value theorem on a convex set in dimension 1: if the derivative of a function is bounded by `C` on `s`, then the function is `C`-Lipschitz on `s`. Version with `deriv_within` and `lipschitz_on_with`. -/ theorem lipschitz_on_with_of_nnnorm_deriv_within_le {C : ℝβ‰₯0} (hs : convex ℝ s) (hf : differentiable_on π•œ f s) (bound : βˆ€x∈s, β€–deriv_within f s xβ€–β‚Š ≀ C) : lipschitz_on_with C f s := hs.lipschitz_on_with_of_nnnorm_has_deriv_within_le (Ξ» x hx, (hf x hx).has_deriv_within_at) bound /-- The mean value theorem on a convex set in dimension 1: if the derivative of a function is bounded by `C`, then the function is `C`-Lipschitz. Version with `deriv`. -/ theorem norm_image_sub_le_of_norm_deriv_le {C : ℝ} (hf : βˆ€ x ∈ s, differentiable_at π•œ f x) (bound : βˆ€x∈s, β€–deriv f xβ€– ≀ C) (hs : convex ℝ s) (xs : x ∈ s) (ys : y ∈ s) : β€–f y - f xβ€– ≀ C * β€–y - xβ€– := hs.norm_image_sub_le_of_norm_has_deriv_within_le (Ξ» x hx, (hf x hx).has_deriv_at.has_deriv_within_at) bound xs ys /-- The mean value theorem on a convex set in dimension 1: if the derivative of a function is bounded by `C` on `s`, then the function is `C`-Lipschitz on `s`. Version with `deriv` and `lipschitz_on_with`. -/ theorem lipschitz_on_with_of_nnnorm_deriv_le {C : ℝβ‰₯0} (hf : βˆ€ x ∈ s, differentiable_at π•œ f x) (bound : βˆ€x∈s, β€–deriv f xβ€–β‚Š ≀ C) (hs : convex ℝ s) : lipschitz_on_with C f s := hs.lipschitz_on_with_of_nnnorm_has_deriv_within_le (Ξ» x hx, (hf x hx).has_deriv_at.has_deriv_within_at) bound /-- The mean value theorem set in dimension 1: if the derivative of a function is bounded by `C`, then the function is `C`-Lipschitz. Version with `deriv` and `lipschitz_with`. -/ theorem _root_.lipschitz_with_of_nnnorm_deriv_le {C : ℝβ‰₯0} (hf : differentiable π•œ f) (bound : βˆ€ x, β€–deriv f xβ€–β‚Š ≀ C) : lipschitz_with C f := lipschitz_on_univ.1 $ convex_univ.lipschitz_on_with_of_nnnorm_deriv_le (Ξ» x hx, hf x) (Ξ» x hx, bound x) /-- If `f : π•œ β†’ G`, `π•œ = R` or `π•œ = β„‚`, is differentiable everywhere and its derivative equal zero, then it is a constant function. -/ theorem _root_.is_const_of_deriv_eq_zero (hf : differentiable π•œ f) (hf' : βˆ€ x, deriv f x = 0) (x y : π•œ) : f x = f y := is_const_of_fderiv_eq_zero hf (Ξ» z, by { ext, simp [← deriv_fderiv, hf'] }) _ _ end convex end /-! ### Functions `[a, b] β†’ ℝ`. -/ section interval -- Declare all variables here to make sure they come in a correct order variables (f f' : ℝ β†’ ℝ) {a b : ℝ} (hab : a < b) (hfc : continuous_on f (Icc a b)) (hff' : βˆ€ x ∈ Ioo a b, has_deriv_at f (f' x) x) (hfd : differentiable_on ℝ f (Ioo a b)) (g g' : ℝ β†’ ℝ) (hgc : continuous_on g (Icc a b)) (hgg' : βˆ€ x ∈ Ioo a b, has_deriv_at g (g' x) x) (hgd : differentiable_on ℝ g (Ioo a b)) include hab hfc hff' hgc hgg' /-- Cauchy's **Mean Value Theorem**, `has_deriv_at` version. -/ lemma exists_ratio_has_deriv_at_eq_ratio_slope : βˆƒ c ∈ Ioo a b, (g b - g a) * f' c = (f b - f a) * g' c := begin let h := Ξ» x, (g b - g a) * f x - (f b - f a) * g x, have hI : h a = h b, { simp only [h], ring }, let h' := Ξ» x, (g b - g a) * f' x - (f b - f a) * g' x, have hhh' : βˆ€ x ∈ Ioo a b, has_deriv_at h (h' x) x, from Ξ» x hx, ((hff' x hx).const_mul (g b - g a)).sub ((hgg' x hx).const_mul (f b - f a)), have hhc : continuous_on h (Icc a b), from (continuous_on_const.mul hfc).sub (continuous_on_const.mul hgc), rcases exists_has_deriv_at_eq_zero h h' hab hhc hI hhh' with ⟨c, cmem, hc⟩, exact ⟨c, cmem, sub_eq_zero.1 hc⟩ end omit hfc hgc /-- Cauchy's **Mean Value Theorem**, extended `has_deriv_at` version. -/ lemma exists_ratio_has_deriv_at_eq_ratio_slope' {lfa lga lfb lgb : ℝ} (hff' : βˆ€ x ∈ Ioo a b, has_deriv_at f (f' x) x) (hgg' : βˆ€ x ∈ Ioo a b, has_deriv_at g (g' x) x) (hfa : tendsto f (𝓝[>] a) (𝓝 lfa)) (hga : tendsto g (𝓝[>] a) (𝓝 lga)) (hfb : tendsto f (𝓝[<] b) (𝓝 lfb)) (hgb : tendsto g (𝓝[<] b) (𝓝 lgb)) : βˆƒ c ∈ Ioo a b, (lgb - lga) * (f' c) = (lfb - lfa) * (g' c) := begin let h := Ξ» x, (lgb - lga) * f x - (lfb - lfa) * g x, have hha : tendsto h (𝓝[>] a) (𝓝 $ lgb * lfa - lfb * lga), { have : tendsto h (𝓝[>] a)(𝓝 $ (lgb - lga) * lfa - (lfb - lfa) * lga) := (tendsto_const_nhds.mul hfa).sub (tendsto_const_nhds.mul hga), convert this using 2, ring }, have hhb : tendsto h (𝓝[<] b) (𝓝 $ lgb * lfa - lfb * lga), { have : tendsto h (𝓝[<] b)(𝓝 $ (lgb - lga) * lfb - (lfb - lfa) * lgb) := (tendsto_const_nhds.mul hfb).sub (tendsto_const_nhds.mul hgb), convert this using 2, ring }, let h' := Ξ» x, (lgb - lga) * f' x - (lfb - lfa) * g' x, have hhh' : βˆ€ x ∈ Ioo a b, has_deriv_at h (h' x) x, { intros x hx, exact ((hff' x hx).const_mul _ ).sub (((hgg' x hx)).const_mul _) }, rcases exists_has_deriv_at_eq_zero' hab hha hhb hhh' with ⟨c, cmem, hc⟩, exact ⟨c, cmem, sub_eq_zero.1 hc⟩ end include hfc omit hgg' /-- Lagrange's Mean Value Theorem, `has_deriv_at` version -/ lemma exists_has_deriv_at_eq_slope : βˆƒ c ∈ Ioo a b, f' c = (f b - f a) / (b - a) := begin rcases exists_ratio_has_deriv_at_eq_ratio_slope f f' hab hfc hff' id 1 continuous_id.continuous_on (Ξ» x hx, has_deriv_at_id x) with ⟨c, cmem, hc⟩, use [c, cmem], simp only [_root_.id, pi.one_apply, mul_one] at hc, rw [← hc, mul_div_cancel_left], exact ne_of_gt (sub_pos.2 hab) end omit hff' /-- Cauchy's Mean Value Theorem, `deriv` version. -/ lemma exists_ratio_deriv_eq_ratio_slope : βˆƒ c ∈ Ioo a b, (g b - g a) * (deriv f c) = (f b - f a) * (deriv g c) := exists_ratio_has_deriv_at_eq_ratio_slope f (deriv f) hab hfc (Ξ» x hx, ((hfd x hx).differentiable_at $ is_open.mem_nhds is_open_Ioo hx).has_deriv_at) g (deriv g) hgc $ Ξ» x hx, ((hgd x hx).differentiable_at $ is_open.mem_nhds is_open_Ioo hx).has_deriv_at omit hfc /-- Cauchy's Mean Value Theorem, extended `deriv` version. -/ lemma exists_ratio_deriv_eq_ratio_slope' {lfa lga lfb lgb : ℝ} (hdf : differentiable_on ℝ f $ Ioo a b) (hdg : differentiable_on ℝ g $ Ioo a b) (hfa : tendsto f (𝓝[>] a) (𝓝 lfa)) (hga : tendsto g (𝓝[>] a) (𝓝 lga)) (hfb : tendsto f (𝓝[<] b) (𝓝 lfb)) (hgb : tendsto g (𝓝[<] b) (𝓝 lgb)) : βˆƒ c ∈ Ioo a b, (lgb - lga) * (deriv f c) = (lfb - lfa) * (deriv g c) := exists_ratio_has_deriv_at_eq_ratio_slope' _ _ hab _ _ (Ξ» x hx, ((hdf x hx).differentiable_at $ Ioo_mem_nhds hx.1 hx.2).has_deriv_at) (Ξ» x hx, ((hdg x hx).differentiable_at $ Ioo_mem_nhds hx.1 hx.2).has_deriv_at) hfa hga hfb hgb /-- Lagrange's **Mean Value Theorem**, `deriv` version. -/ lemma exists_deriv_eq_slope : βˆƒ c ∈ Ioo a b, deriv f c = (f b - f a) / (b - a) := exists_has_deriv_at_eq_slope f (deriv f) hab hfc (Ξ» x hx, ((hfd x hx).differentiable_at $ is_open.mem_nhds is_open_Ioo hx).has_deriv_at) end interval /-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D` of the real line. If `f` is differentiable on the interior of `D` and `C < f'`, then `f` grows faster than `C * x` on `D`, i.e., `C * (y - x) < f y - f x` whenever `x, y ∈ D`, `x < y`. -/ theorem convex.mul_sub_lt_image_sub_of_lt_deriv {D : set ℝ} (hD : convex ℝ D) {f : ℝ β†’ ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) {C} (hf'_gt : βˆ€ x ∈ interior D, C < deriv f x) : βˆ€ x y ∈ D, x < y β†’ C * (y - x) < f y - f x := begin assume x hx y hy hxy, have hxyD : Icc x y βŠ† D, from hD.ord_connected.out hx hy, have hxyD' : Ioo x y βŠ† interior D, from subset_sUnion_of_mem ⟨is_open_Ioo, subset.trans Ioo_subset_Icc_self hxyD⟩, obtain ⟨a, a_mem, ha⟩ : βˆƒ a ∈ Ioo x y, deriv f a = (f y - f x) / (y - x), from exists_deriv_eq_slope f hxy (hf.mono hxyD) (hf'.mono hxyD'), have : C < (f y - f x) / (y - x), by { rw [← ha], exact hf'_gt _ (hxyD' a_mem) }, exact (lt_div_iff (sub_pos.2 hxy)).1 this end /-- Let `f : ℝ β†’ ℝ` be a differentiable function. If `C < f'`, then `f` grows faster than `C * x`, i.e., `C * (y - x) < f y - f x` whenever `x < y`. -/ theorem mul_sub_lt_image_sub_of_lt_deriv {f : ℝ β†’ ℝ} (hf : differentiable ℝ f) {C} (hf'_gt : βˆ€ x, C < deriv f x) ⦃x y⦄ (hxy : x < y) : C * (y - x) < f y - f x := convex_univ.mul_sub_lt_image_sub_of_lt_deriv hf.continuous.continuous_on hf.differentiable_on (Ξ» x _, hf'_gt x) x trivial y trivial hxy /-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D` of the real line. If `f` is differentiable on the interior of `D` and `C ≀ f'`, then `f` grows at least as fast as `C * x` on `D`, i.e., `C * (y - x) ≀ f y - f x` whenever `x, y ∈ D`, `x ≀ y`. -/ theorem convex.mul_sub_le_image_sub_of_le_deriv {D : set ℝ} (hD : convex ℝ D) {f : ℝ β†’ ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) {C} (hf'_ge : βˆ€ x ∈ interior D, C ≀ deriv f x) : βˆ€ x y ∈ D, x ≀ y β†’ C * (y - x) ≀ f y - f x := begin assume x hx y hy hxy, cases eq_or_lt_of_le hxy with hxy' hxy', by rw [hxy', sub_self, sub_self, mul_zero], have hxyD : Icc x y βŠ† D, from hD.ord_connected.out hx hy, have hxyD' : Ioo x y βŠ† interior D, from subset_sUnion_of_mem ⟨is_open_Ioo, subset.trans Ioo_subset_Icc_self hxyD⟩, obtain ⟨a, a_mem, ha⟩ : βˆƒ a ∈ Ioo x y, deriv f a = (f y - f x) / (y - x), from exists_deriv_eq_slope f hxy' (hf.mono hxyD) (hf'.mono hxyD'), have : C ≀ (f y - f x) / (y - x), by { rw [← ha], exact hf'_ge _ (hxyD' a_mem) }, exact (le_div_iff (sub_pos.2 hxy')).1 this end /-- Let `f : ℝ β†’ ℝ` be a differentiable function. If `C ≀ f'`, then `f` grows at least as fast as `C * x`, i.e., `C * (y - x) ≀ f y - f x` whenever `x ≀ y`. -/ theorem mul_sub_le_image_sub_of_le_deriv {f : ℝ β†’ ℝ} (hf : differentiable ℝ f) {C} (hf'_ge : βˆ€ x, C ≀ deriv f x) ⦃x y⦄ (hxy : x ≀ y) : C * (y - x) ≀ f y - f x := convex_univ.mul_sub_le_image_sub_of_le_deriv hf.continuous.continuous_on hf.differentiable_on (Ξ» x _, hf'_ge x) x trivial y trivial hxy /-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D` of the real line. If `f` is differentiable on the interior of `D` and `f' < C`, then `f` grows slower than `C * x` on `D`, i.e., `f y - f x < C * (y - x)` whenever `x, y ∈ D`, `x < y`. -/ theorem convex.image_sub_lt_mul_sub_of_deriv_lt {D : set ℝ} (hD : convex ℝ D) {f : ℝ β†’ ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) {C} (lt_hf' : βˆ€ x ∈ interior D, deriv f x < C) : βˆ€ x y ∈ D, x < y β†’ f y - f x < C * (y - x) := begin assume x hx y hy hxy, have hf'_gt : βˆ€ x ∈ interior D, -C < deriv (Ξ» y, -f y) x, { assume x hx, rw [deriv.neg, neg_lt_neg_iff], exact lt_hf' x hx }, simpa [-neg_lt_neg_iff] using neg_lt_neg (hD.mul_sub_lt_image_sub_of_lt_deriv hf.neg hf'.neg hf'_gt x hx y hy hxy) end /-- Let `f : ℝ β†’ ℝ` be a differentiable function. If `f' < C`, then `f` grows slower than `C * x` on `D`, i.e., `f y - f x < C * (y - x)` whenever `x < y`. -/ theorem image_sub_lt_mul_sub_of_deriv_lt {f : ℝ β†’ ℝ} (hf : differentiable ℝ f) {C} (lt_hf' : βˆ€ x, deriv f x < C) ⦃x y⦄ (hxy : x < y) : f y - f x < C * (y - x) := convex_univ.image_sub_lt_mul_sub_of_deriv_lt hf.continuous.continuous_on hf.differentiable_on (Ξ» x _, lt_hf' x) x trivial y trivial hxy /-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D` of the real line. If `f` is differentiable on the interior of `D` and `f' ≀ C`, then `f` grows at most as fast as `C * x` on `D`, i.e., `f y - f x ≀ C * (y - x)` whenever `x, y ∈ D`, `x ≀ y`. -/ theorem convex.image_sub_le_mul_sub_of_deriv_le {D : set ℝ} (hD : convex ℝ D) {f : ℝ β†’ ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) {C} (le_hf' : βˆ€ x ∈ interior D, deriv f x ≀ C) : βˆ€ x y ∈ D, x ≀ y β†’ f y - f x ≀ C * (y - x) := begin assume x hx y hy hxy, have hf'_ge : βˆ€ x ∈ interior D, -C ≀ deriv (Ξ» y, -f y) x, { assume x hx, rw [deriv.neg, neg_le_neg_iff], exact le_hf' x hx }, simpa [-neg_le_neg_iff] using neg_le_neg (hD.mul_sub_le_image_sub_of_le_deriv hf.neg hf'.neg hf'_ge x hx y hy hxy) end /-- Let `f : ℝ β†’ ℝ` be a differentiable function. If `f' ≀ C`, then `f` grows at most as fast as `C * x`, i.e., `f y - f x ≀ C * (y - x)` whenever `x ≀ y`. -/ theorem image_sub_le_mul_sub_of_deriv_le {f : ℝ β†’ ℝ} (hf : differentiable ℝ f) {C} (le_hf' : βˆ€ x, deriv f x ≀ C) ⦃x y⦄ (hxy : x ≀ y) : f y - f x ≀ C * (y - x) := convex_univ.image_sub_le_mul_sub_of_deriv_le hf.continuous.continuous_on hf.differentiable_on (Ξ» x _, le_hf' x) x trivial y trivial hxy /-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D` of the real line. If `f` is differentiable on the interior of `D` and `f'` is positive, then `f` is a strictly monotone function on `D`. Note that we don't require differentiability explicitly as it already implied by the derivative being strictly positive. -/ theorem convex.strict_mono_on_of_deriv_pos {D : set ℝ} (hD : convex ℝ D) {f : ℝ β†’ ℝ} (hf : continuous_on f D) (hf' : βˆ€ x ∈ interior D, 0 < deriv f x) : strict_mono_on f D := begin rintro x hx y hy, simpa only [zero_mul, sub_pos] using hD.mul_sub_lt_image_sub_of_lt_deriv hf _ hf' x hx y hy, exact Ξ» z hz, (differentiable_at_of_deriv_ne_zero (hf' z hz).ne').differentiable_within_at, end /-- Let `f : ℝ β†’ ℝ` be a differentiable function. If `f'` is positive, then `f` is a strictly monotone function. Note that we don't require differentiability explicitly as it already implied by the derivative being strictly positive. -/ theorem strict_mono_of_deriv_pos {f : ℝ β†’ ℝ} (hf' : βˆ€ x, 0 < deriv f x) : strict_mono f := strict_mono_on_univ.1 $ convex_univ.strict_mono_on_of_deriv_pos (Ξ» z _, (differentiable_at_of_deriv_ne_zero (hf' z).ne').differentiable_within_at .continuous_within_at) (Ξ» x _, hf' x) /-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D` of the real line. If `f` is differentiable on the interior of `D` and `f'` is nonnegative, then `f` is a monotone function on `D`. -/ theorem convex.monotone_on_of_deriv_nonneg {D : set ℝ} (hD : convex ℝ D) {f : ℝ β†’ ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) (hf'_nonneg : βˆ€ x ∈ interior D, 0 ≀ deriv f x) : monotone_on f D := Ξ» x hx y hy hxy, by simpa only [zero_mul, sub_nonneg] using hD.mul_sub_le_image_sub_of_le_deriv hf hf' hf'_nonneg x hx y hy hxy /-- Let `f : ℝ β†’ ℝ` be a differentiable function. If `f'` is nonnegative, then `f` is a monotone function. -/ theorem monotone_of_deriv_nonneg {f : ℝ β†’ ℝ} (hf : differentiable ℝ f) (hf' : βˆ€ x, 0 ≀ deriv f x) : monotone f := monotone_on_univ.1 $ convex_univ.monotone_on_of_deriv_nonneg hf.continuous.continuous_on hf.differentiable_on (Ξ» x _, hf' x) /-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D` of the real line. If `f` is differentiable on the interior of `D` and `f'` is negative, then `f` is a strictly antitone function on `D`. -/ theorem convex.strict_anti_on_of_deriv_neg {D : set ℝ} (hD : convex ℝ D) {f : ℝ β†’ ℝ} (hf : continuous_on f D) (hf' : βˆ€ x ∈ interior D, deriv f x < 0) : strict_anti_on f D := Ξ» x hx y, by simpa only [zero_mul, sub_lt_zero] using hD.image_sub_lt_mul_sub_of_deriv_lt hf (Ξ» z hz, (differentiable_at_of_deriv_ne_zero (hf' z hz).ne).differentiable_within_at) hf' x hx y /-- Let `f : ℝ β†’ ℝ` be a differentiable function. If `f'` is negative, then `f` is a strictly antitone function. Note that we don't require differentiability explicitly as it already implied by the derivative being strictly negative. -/ theorem strict_anti_of_deriv_neg {f : ℝ β†’ ℝ} (hf' : βˆ€ x, deriv f x < 0) : strict_anti f := strict_anti_on_univ.1 $ convex_univ.strict_anti_on_of_deriv_neg (Ξ» z _, (differentiable_at_of_deriv_ne_zero (hf' z).ne).differentiable_within_at .continuous_within_at) (Ξ» x _, hf' x) /-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D` of the real line. If `f` is differentiable on the interior of `D` and `f'` is nonpositive, then `f` is an antitone function on `D`. -/ theorem convex.antitone_on_of_deriv_nonpos {D : set ℝ} (hD : convex ℝ D) {f : ℝ β†’ ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) (hf'_nonpos : βˆ€ x ∈ interior D, deriv f x ≀ 0) : antitone_on f D := Ξ» x hx y hy hxy, by simpa only [zero_mul, sub_nonpos] using hD.image_sub_le_mul_sub_of_deriv_le hf hf' hf'_nonpos x hx y hy hxy /-- Let `f : ℝ β†’ ℝ` be a differentiable function. If `f'` is nonpositive, then `f` is an antitone function. -/ theorem antitone_of_deriv_nonpos {f : ℝ β†’ ℝ} (hf : differentiable ℝ f) (hf' : βˆ€ x, deriv f x ≀ 0) : antitone f := antitone_on_univ.1 $ convex_univ.antitone_on_of_deriv_nonpos hf.continuous.continuous_on hf.differentiable_on (Ξ» x _, hf' x) /-- If a function `f` is continuous on a convex set `D βŠ† ℝ`, is differentiable on its interior, and `f'` is monotone on the interior, then `f` is convex on `D`. -/ theorem monotone_on.convex_on_of_deriv {D : set ℝ} (hD : convex ℝ D) {f : ℝ β†’ ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) (hf'_mono : monotone_on (deriv f) (interior D)) : convex_on ℝ D f := convex_on_of_slope_mono_adjacent hD begin intros x y z hx hz hxy hyz, -- First we prove some trivial inclusions have hxzD : Icc x z βŠ† D, from hD.ord_connected.out hx hz, have hxyD : Icc x y βŠ† D, from subset.trans (Icc_subset_Icc_right $ le_of_lt hyz) hxzD, have hxyD' : Ioo x y βŠ† interior D, from subset_sUnion_of_mem ⟨is_open_Ioo, subset.trans Ioo_subset_Icc_self hxyD⟩, have hyzD : Icc y z βŠ† D, from subset.trans (Icc_subset_Icc_left $ le_of_lt hxy) hxzD, have hyzD' : Ioo y z βŠ† interior D, from subset_sUnion_of_mem ⟨is_open_Ioo, subset.trans Ioo_subset_Icc_self hyzD⟩, -- Then we apply MVT to both `[x, y]` and `[y, z]` obtain ⟨a, ⟨hxa, hay⟩, ha⟩ : βˆƒ a ∈ Ioo x y, deriv f a = (f y - f x) / (y - x), from exists_deriv_eq_slope f hxy (hf.mono hxyD) (hf'.mono hxyD'), obtain ⟨b, ⟨hyb, hbz⟩, hb⟩ : βˆƒ b ∈ Ioo y z, deriv f b = (f z - f y) / (z - y), from exists_deriv_eq_slope f hyz (hf.mono hyzD) (hf'.mono hyzD'), rw [← ha, ← hb], exact hf'_mono (hxyD' ⟨hxa, hay⟩) (hyzD' ⟨hyb, hbz⟩) (hay.trans hyb).le end /-- If a function `f` is continuous on a convex set `D βŠ† ℝ`, is differentiable on its interior, and `f'` is antitone on the interior, then `f` is concave on `D`. -/ theorem antitone_on.concave_on_of_deriv {D : set ℝ} (hD : convex ℝ D) {f : ℝ β†’ ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) (h_anti : antitone_on (deriv f) (interior D)) : concave_on ℝ D f := begin have : monotone_on (deriv (-f)) (interior D), { intros x hx y hy hxy, convert neg_le_neg (h_anti hx hy hxy); convert deriv.neg }, exact neg_convex_on_iff.mp (this.convex_on_of_deriv hD hf.neg hf'.neg), end lemma strict_mono_on.exists_slope_lt_deriv_aux {x y : ℝ} {f : ℝ β†’ ℝ} (hf : continuous_on f (Icc x y)) (hxy : x < y) (hf'_mono : strict_mono_on (deriv f) (Ioo x y)) (h : βˆ€ w ∈ Ioo x y, deriv f w β‰  0) : βˆƒ a ∈ Ioo x y, (f y - f x) / (y - x) < deriv f a := begin have A : differentiable_on ℝ f (Ioo x y), from Ξ» w wmem, (differentiable_at_of_deriv_ne_zero (h w wmem)).differentiable_within_at, obtain ⟨a, ⟨hxa, hay⟩, ha⟩ : βˆƒ a ∈ Ioo x y, deriv f a = (f y - f x) / (y - x), from exists_deriv_eq_slope f hxy hf A, rcases nonempty_Ioo.2 hay with ⟨b, ⟨hab, hby⟩⟩, refine ⟨b, ⟨hxa.trans hab, hby⟩, _⟩, rw ← ha, exact hf'_mono ⟨hxa, hay⟩ ⟨hxa.trans hab, hby⟩ hab end lemma strict_mono_on.exists_slope_lt_deriv {x y : ℝ} {f : ℝ β†’ ℝ} (hf : continuous_on f (Icc x y)) (hxy : x < y) (hf'_mono : strict_mono_on (deriv f) (Ioo x y)) : βˆƒ a ∈ Ioo x y, (f y - f x) / (y - x) < deriv f a := begin by_cases h : βˆ€ w ∈ Ioo x y, deriv f w β‰  0, { apply strict_mono_on.exists_slope_lt_deriv_aux hf hxy hf'_mono h }, { push_neg at h, rcases h with ⟨w, ⟨hxw, hwy⟩, hw⟩, obtain ⟨a, ⟨hxa, haw⟩, ha⟩ : βˆƒ (a : ℝ) (H : a ∈ Ioo x w), (f w - f x) / (w - x) < deriv f a, { apply strict_mono_on.exists_slope_lt_deriv_aux _ hxw _ _, { exact hf.mono (Icc_subset_Icc le_rfl hwy.le) }, { exact hf'_mono.mono (Ioo_subset_Ioo le_rfl hwy.le) }, { assume z hz, rw ← hw, apply ne_of_lt, exact hf'_mono ⟨hz.1, hz.2.trans hwy⟩ ⟨hxw, hwy⟩ hz.2 } }, obtain ⟨b, ⟨hwb, hby⟩, hb⟩ : βˆƒ (b : ℝ) (H : b ∈ Ioo w y), (f y - f w) / (y - w) < deriv f b, { apply strict_mono_on.exists_slope_lt_deriv_aux _ hwy _ _, { refine hf.mono (Icc_subset_Icc hxw.le le_rfl), }, { exact hf'_mono.mono (Ioo_subset_Ioo hxw.le le_rfl) }, { assume z hz, rw ← hw, apply ne_of_gt, exact hf'_mono ⟨hxw, hwy⟩ ⟨hxw.trans hz.1, hz.2⟩ hz.1, } }, refine ⟨b, ⟨hxw.trans hwb, hby⟩, _⟩, simp only [div_lt_iff, hxy, hxw, hwy, sub_pos] at ⊒ ha hb, have : deriv f a * (w - x) < deriv f b * (w - x), { apply mul_lt_mul _ le_rfl (sub_pos.2 hxw) _, { exact hf'_mono ⟨hxa, haw.trans hwy⟩ ⟨hxw.trans hwb, hby⟩ (haw.trans hwb) }, { rw ← hw, exact (hf'_mono ⟨hxw, hwy⟩ ⟨hxw.trans hwb, hby⟩ hwb).le } }, linarith } end lemma strict_mono_on.exists_deriv_lt_slope_aux {x y : ℝ} {f : ℝ β†’ ℝ} (hf : continuous_on f (Icc x y)) (hxy : x < y) (hf'_mono : strict_mono_on (deriv f) (Ioo x y)) (h : βˆ€ w ∈ Ioo x y, deriv f w β‰  0) : βˆƒ a ∈ Ioo x y, deriv f a < (f y - f x) / (y - x) := begin have A : differentiable_on ℝ f (Ioo x y), from Ξ» w wmem, (differentiable_at_of_deriv_ne_zero (h w wmem)).differentiable_within_at, obtain ⟨a, ⟨hxa, hay⟩, ha⟩ : βˆƒ a ∈ Ioo x y, deriv f a = (f y - f x) / (y - x), from exists_deriv_eq_slope f hxy hf A, rcases nonempty_Ioo.2 hxa with ⟨b, ⟨hxb, hba⟩⟩, refine ⟨b, ⟨hxb, hba.trans hay⟩, _⟩, rw ← ha, exact hf'_mono ⟨hxb, hba.trans hay⟩ ⟨hxa, hay⟩ hba end lemma strict_mono_on.exists_deriv_lt_slope {x y : ℝ} {f : ℝ β†’ ℝ} (hf : continuous_on f (Icc x y)) (hxy : x < y) (hf'_mono : strict_mono_on (deriv f) (Ioo x y)) : βˆƒ a ∈ Ioo x y, deriv f a < (f y - f x) / (y - x) := begin by_cases h : βˆ€ w ∈ Ioo x y, deriv f w β‰  0, { apply strict_mono_on.exists_deriv_lt_slope_aux hf hxy hf'_mono h }, { push_neg at h, rcases h with ⟨w, ⟨hxw, hwy⟩, hw⟩, obtain ⟨a, ⟨hxa, haw⟩, ha⟩ : βˆƒ (a : ℝ) (H : a ∈ Ioo x w), deriv f a < (f w - f x) / (w - x), { apply strict_mono_on.exists_deriv_lt_slope_aux _ hxw _ _, { exact hf.mono (Icc_subset_Icc le_rfl hwy.le) }, { exact hf'_mono.mono (Ioo_subset_Ioo le_rfl hwy.le) }, { assume z hz, rw ← hw, apply ne_of_lt, exact hf'_mono ⟨hz.1, hz.2.trans hwy⟩ ⟨hxw, hwy⟩ hz.2 } }, obtain ⟨b, ⟨hwb, hby⟩, hb⟩ : βˆƒ (b : ℝ) (H : b ∈ Ioo w y), deriv f b < (f y - f w) / (y - w), { apply strict_mono_on.exists_deriv_lt_slope_aux _ hwy _ _, { refine hf.mono (Icc_subset_Icc hxw.le le_rfl), }, { exact hf'_mono.mono (Ioo_subset_Ioo hxw.le le_rfl) }, { assume z hz, rw ← hw, apply ne_of_gt, exact hf'_mono ⟨hxw, hwy⟩ ⟨hxw.trans hz.1, hz.2⟩ hz.1, } }, refine ⟨a, ⟨hxa, haw.trans hwy⟩, _⟩, simp only [lt_div_iff, hxy, hxw, hwy, sub_pos] at ⊒ ha hb, have : deriv f a * (y - w) < deriv f b * (y - w), { apply mul_lt_mul _ le_rfl (sub_pos.2 hwy) _, { exact hf'_mono ⟨hxa, haw.trans hwy⟩ ⟨hxw.trans hwb, hby⟩ (haw.trans hwb) }, { rw ← hw, exact (hf'_mono ⟨hxw, hwy⟩ ⟨hxw.trans hwb, hby⟩ hwb).le } }, linarith } end /-- If a function `f` is continuous on a convex set `D βŠ† ℝ`, and `f'` is strictly monotone on the interior, then `f` is strictly convex on `D`. Note that we don't require differentiability, since it is guaranteed at all but at most one point by the strict monotonicity of `f'`. -/ lemma strict_mono_on.strict_convex_on_of_deriv {D : set ℝ} (hD : convex ℝ D) {f : ℝ β†’ ℝ} (hf : continuous_on f D) (hf' : strict_mono_on (deriv f) (interior D)) : strict_convex_on ℝ D f := strict_convex_on_of_slope_strict_mono_adjacent hD begin intros x y z hx hz hxy hyz, -- First we prove some trivial inclusions have hxzD : Icc x z βŠ† D, from hD.ord_connected.out hx hz, have hxyD : Icc x y βŠ† D, from subset.trans (Icc_subset_Icc_right $ le_of_lt hyz) hxzD, have hxyD' : Ioo x y βŠ† interior D, from subset_sUnion_of_mem ⟨is_open_Ioo, subset.trans Ioo_subset_Icc_self hxyD⟩, have hyzD : Icc y z βŠ† D, from subset.trans (Icc_subset_Icc_left $ le_of_lt hxy) hxzD, have hyzD' : Ioo y z βŠ† interior D, from subset_sUnion_of_mem ⟨is_open_Ioo, subset.trans Ioo_subset_Icc_self hyzD⟩, -- Then we get points `a` and `b` in each interval `[x, y]` and `[y, z]` where the derivatives -- can be compared to the slopes between `x, y` and `y, z` respectively. obtain ⟨a, ⟨hxa, hay⟩, ha⟩ : βˆƒ a ∈ Ioo x y, (f y - f x) / (y - x) < deriv f a, from strict_mono_on.exists_slope_lt_deriv (hf.mono hxyD) hxy (hf'.mono hxyD'), obtain ⟨b, ⟨hyb, hbz⟩, hb⟩ : βˆƒ b ∈ Ioo y z, deriv f b < (f z - f y) / (z - y), from strict_mono_on.exists_deriv_lt_slope (hf.mono hyzD) hyz (hf'.mono hyzD'), apply ha.trans (lt_trans _ hb), exact hf' (hxyD' ⟨hxa, hay⟩) (hyzD' ⟨hyb, hbz⟩) (hay.trans hyb), end /-- If a function `f` is continuous on a convex set `D βŠ† ℝ` and `f'` is strictly antitone on the interior, then `f` is strictly concave on `D`. Note that we don't require differentiability, since it is guaranteed at all but at most one point by the strict antitonicity of `f'`. -/ lemma strict_anti_on.strict_concave_on_of_deriv {D : set ℝ} (hD : convex ℝ D) {f : ℝ β†’ ℝ} (hf : continuous_on f D) (h_anti : strict_anti_on (deriv f) (interior D)) : strict_concave_on ℝ D f := begin have : strict_mono_on (deriv (-f)) (interior D), { intros x hx y hy hxy, convert neg_lt_neg (h_anti hx hy hxy); convert deriv.neg }, exact neg_strict_convex_on_iff.mp (this.strict_convex_on_of_deriv hD hf.neg), end /-- If a function `f` is differentiable and `f'` is monotone on `ℝ` then `f` is convex. -/ theorem monotone.convex_on_univ_of_deriv {f : ℝ β†’ ℝ} (hf : differentiable ℝ f) (hf'_mono : monotone (deriv f)) : convex_on ℝ univ f := (hf'_mono.monotone_on _).convex_on_of_deriv convex_univ hf.continuous.continuous_on hf.differentiable_on /-- If a function `f` is differentiable and `f'` is antitone on `ℝ` then `f` is concave. -/ theorem antitone.concave_on_univ_of_deriv {f : ℝ β†’ ℝ} (hf : differentiable ℝ f) (hf'_anti : antitone (deriv f)) : concave_on ℝ univ f := (hf'_anti.antitone_on _).concave_on_of_deriv convex_univ hf.continuous.continuous_on hf.differentiable_on /-- If a function `f` is continuous and `f'` is strictly monotone on `ℝ` then `f` is strictly convex. Note that we don't require differentiability, since it is guaranteed at all but at most one point by the strict monotonicity of `f'`. -/ lemma strict_mono.strict_convex_on_univ_of_deriv {f : ℝ β†’ ℝ} (hf : continuous f) (hf'_mono : strict_mono (deriv f)) : strict_convex_on ℝ univ f := (hf'_mono.strict_mono_on _).strict_convex_on_of_deriv convex_univ hf.continuous_on /-- If a function `f` is continuous and `f'` is strictly antitone on `ℝ` then `f` is strictly concave. Note that we don't require differentiability, since it is guaranteed at all but at most one point by the strict antitonicity of `f'`. -/ lemma strict_anti.strict_concave_on_univ_of_deriv {f : ℝ β†’ ℝ} (hf : continuous f) (hf'_anti : strict_anti (deriv f)) : strict_concave_on ℝ univ f := (hf'_anti.strict_anti_on _).strict_concave_on_of_deriv convex_univ hf.continuous_on /-- If a function `f` is continuous on a convex set `D βŠ† ℝ`, is twice differentiable on its interior, and `f''` is nonnegative on the interior, then `f` is convex on `D`. -/ theorem convex_on_of_deriv2_nonneg {D : set ℝ} (hD : convex ℝ D) {f : ℝ β†’ ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) (hf'' : differentiable_on ℝ (deriv f) (interior D)) (hf''_nonneg : βˆ€ x ∈ interior D, 0 ≀ (deriv^[2] f x)) : convex_on ℝ D f := (hD.interior.monotone_on_of_deriv_nonneg hf''.continuous_on (by rwa interior_interior) $ by rwa interior_interior).convex_on_of_deriv hD hf hf' /-- If a function `f` is continuous on a convex set `D βŠ† ℝ`, is twice differentiable on its interior, and `f''` is nonpositive on the interior, then `f` is concave on `D`. -/ theorem concave_on_of_deriv2_nonpos {D : set ℝ} (hD : convex ℝ D) {f : ℝ β†’ ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) (hf'' : differentiable_on ℝ (deriv f) (interior D)) (hf''_nonpos : βˆ€ x ∈ interior D, deriv^[2] f x ≀ 0) : concave_on ℝ D f := (hD.interior.antitone_on_of_deriv_nonpos hf''.continuous_on (by rwa interior_interior) $ by rwa interior_interior).concave_on_of_deriv hD hf hf' /-- If a function `f` is continuous on a convex set `D βŠ† ℝ` and `f''` is strictly positive on the interior, then `f` is strictly convex on `D`. Note that we don't require twice differentiability explicitly as it is already implied by the second derivative being strictly positive, except at at most one point. -/ lemma strict_convex_on_of_deriv2_pos {D : set ℝ} (hD : convex ℝ D) {f : ℝ β†’ ℝ} (hf : continuous_on f D) (hf'' : βˆ€ x ∈ interior D, 0 < (deriv^[2] f) x) : strict_convex_on ℝ D f := (hD.interior.strict_mono_on_of_deriv_pos (Ξ» z hz, (differentiable_at_of_deriv_ne_zero (hf'' z hz).ne').differentiable_within_at .continuous_within_at) $ by rwa interior_interior).strict_convex_on_of_deriv hD hf /-- If a function `f` is continuous on a convex set `D βŠ† ℝ` and `f''` is strictly negative on the interior, then `f` is strictly concave on `D`. Note that we don't require twice differentiability explicitly as it already implied by the second derivative being strictly negative, except at at most one point. -/ lemma strict_concave_on_of_deriv2_neg {D : set ℝ} (hD : convex ℝ D) {f : ℝ β†’ ℝ} (hf : continuous_on f D) (hf'' : βˆ€ x ∈ interior D, deriv^[2] f x < 0) : strict_concave_on ℝ D f := (hD.interior.strict_anti_on_of_deriv_neg (Ξ» z hz, (differentiable_at_of_deriv_ne_zero (hf'' z hz).ne).differentiable_within_at .continuous_within_at) $ by rwa interior_interior).strict_concave_on_of_deriv hD hf /-- If a function `f` is twice differentiable on a open convex set `D βŠ† ℝ` and `f''` is nonnegative on `D`, then `f` is convex on `D`. -/ theorem convex_on_of_deriv2_nonneg' {D : set ℝ} (hD : convex ℝ D) {f : ℝ β†’ ℝ} (hf' : differentiable_on ℝ f D) (hf'' : differentiable_on ℝ (deriv f) D) (hf''_nonneg : βˆ€ x ∈ D, 0 ≀ (deriv^[2] f) x) : convex_on ℝ D f := convex_on_of_deriv2_nonneg hD hf'.continuous_on (hf'.mono interior_subset) (hf''.mono interior_subset) (Ξ» x hx, hf''_nonneg x (interior_subset hx)) /-- If a function `f` is twice differentiable on an open convex set `D βŠ† ℝ` and `f''` is nonpositive on `D`, then `f` is concave on `D`. -/ theorem concave_on_of_deriv2_nonpos' {D : set ℝ} (hD : convex ℝ D) {f : ℝ β†’ ℝ} (hf' : differentiable_on ℝ f D) (hf'' : differentiable_on ℝ (deriv f) D) (hf''_nonpos : βˆ€ x ∈ D, deriv^[2] f x ≀ 0) : concave_on ℝ D f := concave_on_of_deriv2_nonpos hD hf'.continuous_on (hf'.mono interior_subset) (hf''.mono interior_subset) (Ξ» x hx, hf''_nonpos x (interior_subset hx)) /-- If a function `f` is continuous on a convex set `D βŠ† ℝ` and `f''` is strictly positive on `D`, then `f` is strictly convex on `D`. Note that we don't require twice differentiability explicitly as it is already implied by the second derivative being strictly positive, except at at most one point. -/ lemma strict_convex_on_of_deriv2_pos' {D : set ℝ} (hD : convex ℝ D) {f : ℝ β†’ ℝ} (hf : continuous_on f D) (hf'' : βˆ€ x ∈ D, 0 < (deriv^[2] f) x) : strict_convex_on ℝ D f := strict_convex_on_of_deriv2_pos hD hf $ Ξ» x hx, hf'' x (interior_subset hx) /-- If a function `f` is continuous on a convex set `D βŠ† ℝ` and `f''` is strictly negative on `D`, then `f` is strictly concave on `D`. Note that we don't require twice differentiability explicitly as it is already implied by the second derivative being strictly negative, except at at most one point. -/ lemma strict_concave_on_of_deriv2_neg' {D : set ℝ} (hD : convex ℝ D) {f : ℝ β†’ ℝ} (hf : continuous_on f D) (hf'' : βˆ€ x ∈ D, deriv^[2] f x < 0) : strict_concave_on ℝ D f := strict_concave_on_of_deriv2_neg hD hf $ Ξ» x hx, hf'' x (interior_subset hx) /-- If a function `f` is twice differentiable on `ℝ`, and `f''` is nonnegative on `ℝ`, then `f` is convex on `ℝ`. -/ theorem convex_on_univ_of_deriv2_nonneg {f : ℝ β†’ ℝ} (hf' : differentiable ℝ f) (hf'' : differentiable ℝ (deriv f)) (hf''_nonneg : βˆ€ x, 0 ≀ (deriv^[2] f) x) : convex_on ℝ univ f := convex_on_of_deriv2_nonneg' convex_univ hf'.differentiable_on hf''.differentiable_on (Ξ» x _, hf''_nonneg x) /-- If a function `f` is twice differentiable on `ℝ`, and `f''` is nonpositive on `ℝ`, then `f` is concave on `ℝ`. -/ theorem concave_on_univ_of_deriv2_nonpos {f : ℝ β†’ ℝ} (hf' : differentiable ℝ f) (hf'' : differentiable ℝ (deriv f)) (hf''_nonpos : βˆ€ x, deriv^[2] f x ≀ 0) : concave_on ℝ univ f := concave_on_of_deriv2_nonpos' convex_univ hf'.differentiable_on hf''.differentiable_on (Ξ» x _, hf''_nonpos x) /-- If a function `f` is continuous on `ℝ`, and `f''` is strictly positive on `ℝ`, then `f` is strictly convex on `ℝ`. Note that we don't require twice differentiability explicitly as it is already implied by the second derivative being strictly positive, except at at most one point. -/ lemma strict_convex_on_univ_of_deriv2_pos {f : ℝ β†’ ℝ} (hf : continuous f) (hf'' : βˆ€ x, 0 < (deriv^[2] f) x) : strict_convex_on ℝ univ f := strict_convex_on_of_deriv2_pos' convex_univ hf.continuous_on $ Ξ» x _, hf'' x /-- If a function `f` is continuous on `ℝ`, and `f''` is strictly negative on `ℝ`, then `f` is strictly concave on `ℝ`. Note that we don't require twice differentiability explicitly as it is already implied by the second derivative being strictly negative, except at at most one point. -/ lemma strict_concave_on_univ_of_deriv2_neg {f : ℝ β†’ ℝ} (hf : continuous f) (hf'' : βˆ€ x, deriv^[2] f x < 0) : strict_concave_on ℝ univ f := strict_concave_on_of_deriv2_neg' convex_univ hf.continuous_on $ Ξ» x _, hf'' x /-! ### Functions `f : E β†’ ℝ` -/ /-- Lagrange's Mean Value Theorem, applied to convex domains. -/ theorem domain_mvt {f : E β†’ ℝ} {s : set E} {x y : E} {f' : E β†’ (E β†’L[ℝ] ℝ)} (hf : βˆ€ x ∈ s, has_fderiv_within_at f (f' x) s x) (hs : convex ℝ s) (xs : x ∈ s) (ys : y ∈ s) : βˆƒ z ∈ segment ℝ x y, f y - f x = f' z (y - x) := begin have hIccIoo := @Ioo_subset_Icc_self ℝ _ 0 1, -- parametrize segment set g : ℝ β†’ E := Ξ» t, x + t β€’ (y - x), have hseg : βˆ€ t ∈ Icc (0:ℝ) 1, g t ∈ segment ℝ x y, { rw segment_eq_image', simp only [mem_image, and_imp, add_right_inj], intros t ht, exact ⟨t, ht, rfl⟩ }, have hseg' : Icc 0 1 βŠ† g ⁻¹' s, { rw ← image_subset_iff, unfold image, change βˆ€ _, _, intros z Hz, rw mem_set_of_eq at Hz, rcases Hz with ⟨t, Ht, hgt⟩, rw ← hgt, exact hs.segment_subset xs ys (hseg t Ht) }, -- derivative of pullback of f under parametrization have hfg: βˆ€ t ∈ Icc (0:ℝ) 1, has_deriv_within_at (f ∘ g) ((f' (g t) : E β†’ ℝ) (y-x)) (Icc (0:ℝ) 1) t, { intros t Ht, have hg : has_deriv_at g (y-x) t, { have := ((has_deriv_at_id t).smul_const (y - x)).const_add x, rwa one_smul at this }, exact (hf (g t) $ hseg' Ht).comp_has_deriv_within_at _ hg.has_deriv_within_at hseg' }, -- apply 1-variable mean value theorem to pullback have hMVT : βˆƒ (t ∈ Ioo (0:ℝ) 1), ((f' (g t) : E β†’ ℝ) (y-x)) = (f (g 1) - f (g 0)) / (1 - 0), { refine exists_has_deriv_at_eq_slope (f ∘ g) _ (by norm_num) _ _, { exact Ξ» t Ht, (hfg t Ht).continuous_within_at }, { exact Ξ» t Ht, (hfg t $ hIccIoo Ht).has_deriv_at (Icc_mem_nhds Ht.1 Ht.2) } }, -- reinterpret on domain rcases hMVT with ⟨t, Ht, hMVT'⟩, use g t, refine ⟨hseg t $ hIccIoo Ht, _⟩, simp [g, hMVT'], end section is_R_or_C /-! ### Vector-valued functions `f : E β†’ F`. Strict differentiability. A `C^1` function is strictly differentiable, when the field is `ℝ` or `β„‚`. This follows from the mean value inequality on balls, which is a particular case of the above results after restricting the scalars to `ℝ`. Note that it does not make sense to talk of a convex set over `β„‚`, but balls make sense and are enough. Many formulations of the mean value inequality could be generalized to balls over `ℝ` or `β„‚`. For now, we only include the ones that we need. -/ variables {π•œ : Type*} [is_R_or_C π•œ] {G : Type*} [normed_add_comm_group G] [normed_space π•œ G] {H : Type*} [normed_add_comm_group H] [normed_space π•œ H] {f : G β†’ H} {f' : G β†’ G β†’L[π•œ] H} {x : G} /-- Over the reals or the complexes, a continuously differentiable function is strictly differentiable. -/ lemma has_strict_fderiv_at_of_has_fderiv_at_of_continuous_at (hder : βˆ€αΆ  y in 𝓝 x, has_fderiv_at f (f' y) y) (hcont : continuous_at f' x) : has_strict_fderiv_at f (f' x) x := begin -- turn little-o definition of strict_fderiv into an epsilon-delta statement refine is_o_iff.mpr (Ξ» c hc, metric.eventually_nhds_iff_ball.mpr _), -- the correct Ξ΅ is the modulus of continuity of f' rcases metric.mem_nhds_iff.mp (inter_mem hder (hcont $ ball_mem_nhds _ hc)) with ⟨Ρ, Ξ΅0, hΡ⟩, refine ⟨Ρ, Ξ΅0, _⟩, -- simplify formulas involving the product E Γ— E rintros ⟨a, b⟩ h, rw [← ball_prod_same, prod_mk_mem_set_prod_eq] at h, -- exploit the choice of Ξ΅ as the modulus of continuity of f' have hf' : βˆ€ x' ∈ ball x Ξ΅, β€–f' x' - f' xβ€– ≀ c, { intros x' H', rw ← dist_eq_norm, exact le_of_lt (hΞ΅ H').2 }, -- apply mean value theorem letI : normed_space ℝ G := restrict_scalars.normed_space ℝ π•œ G, refine (convex_ball _ _).norm_image_sub_le_of_norm_has_fderiv_within_le' _ hf' h.2 h.1, exact Ξ» y hy, (hΞ΅ hy).1.has_fderiv_within_at end /-- Over the reals or the complexes, a continuously differentiable function is strictly differentiable. -/ lemma has_strict_deriv_at_of_has_deriv_at_of_continuous_at {f f' : π•œ β†’ G} {x : π•œ} (hder : βˆ€αΆ  y in 𝓝 x, has_deriv_at f (f' y) y) (hcont : continuous_at f' x) : has_strict_deriv_at f (f' x) x := has_strict_fderiv_at_of_has_fderiv_at_of_continuous_at (hder.mono (Ξ» y hy, hy.has_fderiv_at)) $ (smul_rightL π•œ π•œ G 1).continuous.continuous_at.comp hcont end is_R_or_C