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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1ec3dd1ff70ba758e39f75d0976e1f2c817ae6b1 | bae21755a4a03bbe0a5c22e258db8633407711ad | /library/init/category/applicative.lean | 3538f85e0ac2461851ef416467b52d51f977e8a2 | [
"Apache-2.0"
] | permissive | nor-code/lean | f437357a8f85db0f06f186fa50fcb1bc75f6b122 | aa306af3d7c47de3c7937c98d3aa919eb8da6f34 | refs/heads/master | 1,662,613,329,886 | 1,586,696,014,000 | 1,586,696,014,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,114 | 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, Sebastian Ullrich
-/
prelude
import init.category.functor
open function
universes u v
class has_pure (f : Type u → Type v) :=
(pure {α : Type u} : α → f α)
export has_pure (pure)
class has_seq (f : Type u → Type v) : Type (max (u+1) v) :=
(seq : Π {α β : Type u}, f (α → β) → f α → f β)
infixl ` <*> `:60 := has_seq.seq
class has_seq_left (f : Type u → Type v) : Type (max (u+1) v) :=
(seq_left : Π {α β : Type u}, f α → f β → f α)
infixl ` <* `:60 := has_seq_left.seq_left
class has_seq_right (f : Type u → Type v) : Type (max (u+1) v) :=
(seq_right : Π {α β : Type u}, f α → f β → f β)
infixl ` *> `:60 := has_seq_right.seq_right
class applicative (f : Type u → Type v) extends functor f, has_pure f, has_seq f, has_seq_left f, has_seq_right f :=
(map := λ _ _ x y, pure x <*> y)
(seq_left := λ α β a b, const β <$> a <*> b)
(seq_right := λ α β a b, const α id <$> a <*> b)
|
c2ebc00420ff61eed66afd2acf6c01a0bc1c4bba | 4727251e0cd73359b15b664c3170e5d754078599 | /src/data/bool/set.lean | b911c3d8711dd4e4268a6d10e8c4bb70de60d94e | [
"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 | 587 | lean | /-
Copyright (c) 2021 Yury G. Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury G. Kudryashov
-/
import data.bool.basic
import data.set.basic
/-!
# Booleans and set operations
This file contains two trivial lemmas about `bool`, `set.univ`, and `set.range`.
-/
open set
namespace bool
@[simp] lemma univ_eq : (univ : set bool) = {ff, tt} :=
(eq_univ_of_forall bool.dichotomy).symm
@[simp] lemma range_eq {α : Type*} (f : bool → α) : range f = {f ff, f tt} :=
by rw [← image_univ, univ_eq, image_pair]
end bool
|
db2a59a5890de9dc4a12ea3a9f80caf23b338730 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/measure_theory/lebesgue_measure.lean | 12f8e39b18835acd3f0f45e47bf8bc7cb83eb358 | [] | 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 | 9,450 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Yury Kudryashov
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.measure_theory.pi
import Mathlib.PostPort
universes u_1
namespace Mathlib
/-!
# Lebesgue measure on the real line and on `ℝⁿ`
-/
namespace measure_theory
/-!
### Preliminary definitions
-/
/-- Length of an interval. This is the largest monotonic function which correctly
measures all intervals. -/
def lebesgue_length (s : set ℝ) : ennreal :=
infi fun (a : ℝ) => infi fun (b : ℝ) => infi fun (h : s ⊆ set.Ico a b) => ennreal.of_real (b - a)
@[simp] theorem lebesgue_length_empty : lebesgue_length ∅ = 0 := sorry
@[simp] theorem lebesgue_length_Ico (a : ℝ) (b : ℝ) : lebesgue_length (set.Ico a b) = ennreal.of_real (b - a) := sorry
theorem lebesgue_length_mono {s₁ : set ℝ} {s₂ : set ℝ} (h : s₁ ⊆ s₂) : lebesgue_length s₁ ≤ lebesgue_length s₂ := sorry
theorem lebesgue_length_eq_infi_Ioo (s : set ℝ) : lebesgue_length s = infi fun (a : ℝ) => infi fun (b : ℝ) => infi fun (h : s ⊆ set.Ioo a b) => ennreal.of_real (b - a) := sorry
@[simp] theorem lebesgue_length_Ioo (a : ℝ) (b : ℝ) : lebesgue_length (set.Ioo a b) = ennreal.of_real (b - a) := sorry
theorem lebesgue_length_eq_infi_Icc (s : set ℝ) : lebesgue_length s = infi fun (a : ℝ) => infi fun (b : ℝ) => infi fun (h : s ⊆ set.Icc a b) => ennreal.of_real (b - a) := sorry
@[simp] theorem lebesgue_length_Icc (a : ℝ) (b : ℝ) : lebesgue_length (set.Icc a b) = ennreal.of_real (b - a) := sorry
/-- The Lebesgue outer measure, as an outer measure of ℝ. -/
def lebesgue_outer : outer_measure ℝ :=
outer_measure.of_function lebesgue_length lebesgue_length_empty
theorem lebesgue_outer_le_length (s : set ℝ) : coe_fn lebesgue_outer s ≤ lebesgue_length s :=
outer_measure.of_function_le s
theorem lebesgue_length_subadditive {a : ℝ} {b : ℝ} {c : ℕ → ℝ} {d : ℕ → ℝ} (ss : set.Icc a b ⊆ set.Union fun (i : ℕ) => set.Ioo (c i) (d i)) : ennreal.of_real (b - a) ≤ tsum fun (i : ℕ) => ennreal.of_real (d i - c i) := sorry
@[simp] theorem lebesgue_outer_Icc (a : ℝ) (b : ℝ) : coe_fn lebesgue_outer (set.Icc a b) = ennreal.of_real (b - a) := sorry
@[simp] theorem lebesgue_outer_singleton (a : ℝ) : coe_fn lebesgue_outer (singleton a) = 0 := sorry
@[simp] theorem lebesgue_outer_Ico (a : ℝ) (b : ℝ) : coe_fn lebesgue_outer (set.Ico a b) = ennreal.of_real (b - a) := sorry
@[simp] theorem lebesgue_outer_Ioo (a : ℝ) (b : ℝ) : coe_fn lebesgue_outer (set.Ioo a b) = ennreal.of_real (b - a) := sorry
@[simp] theorem lebesgue_outer_Ioc (a : ℝ) (b : ℝ) : coe_fn lebesgue_outer (set.Ioc a b) = ennreal.of_real (b - a) := sorry
theorem is_lebesgue_measurable_Iio {c : ℝ} : measurable_space.is_measurable' (outer_measure.caratheodory lebesgue_outer) (set.Iio c) := sorry
theorem lebesgue_outer_trim : outer_measure.trim lebesgue_outer = lebesgue_outer := sorry
theorem borel_le_lebesgue_measurable : borel ℝ ≤ outer_measure.caratheodory lebesgue_outer := sorry
/-!
### Definition of the Lebesgue measure and lengths of intervals
-/
/-- Lebesgue measure on the Borel sets
The outer Lebesgue measure is the completion of this measure. (TODO: proof this)
-/
protected instance real.measure_space : measure_space ℝ :=
measure_space.mk (measure.mk lebesgue_outer sorry lebesgue_outer_trim)
@[simp] theorem lebesgue_to_outer_measure : measure.to_outer_measure volume = lebesgue_outer :=
rfl
end measure_theory
namespace real
theorem volume_val (s : set ℝ) : coe_fn volume s = coe_fn measure_theory.lebesgue_outer s :=
rfl
protected instance has_no_atoms_volume : measure_theory.has_no_atoms volume :=
measure_theory.has_no_atoms.mk measure_theory.lebesgue_outer_singleton
@[simp] theorem volume_Ico {a : ℝ} {b : ℝ} : coe_fn volume (set.Ico a b) = ennreal.of_real (b - a) :=
measure_theory.lebesgue_outer_Ico a b
@[simp] theorem volume_Icc {a : ℝ} {b : ℝ} : coe_fn volume (set.Icc a b) = ennreal.of_real (b - a) :=
measure_theory.lebesgue_outer_Icc a b
@[simp] theorem volume_Ioo {a : ℝ} {b : ℝ} : coe_fn volume (set.Ioo a b) = ennreal.of_real (b - a) :=
measure_theory.lebesgue_outer_Ioo a b
@[simp] theorem volume_Ioc {a : ℝ} {b : ℝ} : coe_fn volume (set.Ioc a b) = ennreal.of_real (b - a) :=
measure_theory.lebesgue_outer_Ioc a b
@[simp] theorem volume_singleton {a : ℝ} : coe_fn volume (singleton a) = 0 :=
measure_theory.lebesgue_outer_singleton a
@[simp] theorem volume_interval {a : ℝ} {b : ℝ} : coe_fn volume (set.interval a b) = ennreal.of_real (abs (b - a)) := sorry
@[simp] theorem volume_Ioi {a : ℝ} : coe_fn volume (set.Ioi a) = ⊤ := sorry
@[simp] theorem volume_Ici {a : ℝ} : coe_fn volume (set.Ici a) = ⊤ := sorry
@[simp] theorem volume_Iio {a : ℝ} : coe_fn volume (set.Iio a) = ⊤ := sorry
@[simp] theorem volume_Iic {a : ℝ} : coe_fn volume (set.Iic a) = ⊤ := sorry
protected instance locally_finite_volume : measure_theory.locally_finite_measure volume :=
measure_theory.locally_finite_measure.mk
fun (x : ℝ) =>
Exists.intro (set.Ioo (x - 1) (x + 1))
(Exists.intro
(mem_nhds_sets is_open_Ioo { left := sub_lt_self x zero_lt_one, right := lt_add_of_pos_right x zero_lt_one })
(eq.mpr
(id
(Eq.trans
((fun (ᾰ ᾰ_1 : ennreal) (e_2 : ᾰ = ᾰ_1) (ᾰ_2 ᾰ_3 : ennreal) (e_3 : ᾰ_2 = ᾰ_3) =>
congr (congr_arg Less e_2) e_3)
(coe_fn volume (set.Ioo (x - 1) (x + 1))) (ennreal.of_real (x + 1 - (x - 1))) volume_Ioo ⊤ ⊤
(Eq.refl ⊤))
(propext (iff_true_intro ennreal.of_real_lt_top))))
trivial))
/-!
### Volume of a box in `ℝⁿ`
-/
theorem volume_Icc_pi {ι : Type u_1} [fintype ι] {a : ι → ℝ} {b : ι → ℝ} : coe_fn volume (set.Icc a b) = finset.prod finset.univ fun (i : ι) => ennreal.of_real (b i - a i) := sorry
@[simp] theorem volume_Icc_pi_to_real {ι : Type u_1} [fintype ι] {a : ι → ℝ} {b : ι → ℝ} (h : a ≤ b) : ennreal.to_real (coe_fn volume (set.Icc a b)) = finset.prod finset.univ fun (i : ι) => b i - a i := sorry
theorem volume_pi_Ioo {ι : Type u_1} [fintype ι] {a : ι → ℝ} {b : ι → ℝ} : coe_fn volume (set.pi set.univ fun (i : ι) => set.Ioo (a i) (b i)) =
finset.prod finset.univ fun (i : ι) => ennreal.of_real (b i - a i) :=
Eq.trans (measure_theory.measure_congr measure_theory.measure.univ_pi_Ioo_ae_eq_Icc) volume_Icc_pi
@[simp] theorem volume_pi_Ioo_to_real {ι : Type u_1} [fintype ι] {a : ι → ℝ} {b : ι → ℝ} (h : a ≤ b) : ennreal.to_real (coe_fn volume (set.pi set.univ fun (i : ι) => set.Ioo (a i) (b i))) =
finset.prod finset.univ fun (i : ι) => b i - a i := sorry
theorem volume_pi_Ioc {ι : Type u_1} [fintype ι] {a : ι → ℝ} {b : ι → ℝ} : coe_fn volume (set.pi set.univ fun (i : ι) => set.Ioc (a i) (b i)) =
finset.prod finset.univ fun (i : ι) => ennreal.of_real (b i - a i) :=
Eq.trans (measure_theory.measure_congr measure_theory.measure.univ_pi_Ioc_ae_eq_Icc) volume_Icc_pi
@[simp] theorem volume_pi_Ioc_to_real {ι : Type u_1} [fintype ι] {a : ι → ℝ} {b : ι → ℝ} (h : a ≤ b) : ennreal.to_real (coe_fn volume (set.pi set.univ fun (i : ι) => set.Ioc (a i) (b i))) =
finset.prod finset.univ fun (i : ι) => b i - a i := sorry
theorem volume_pi_Ico {ι : Type u_1} [fintype ι] {a : ι → ℝ} {b : ι → ℝ} : coe_fn volume (set.pi set.univ fun (i : ι) => set.Ico (a i) (b i)) =
finset.prod finset.univ fun (i : ι) => ennreal.of_real (b i - a i) :=
Eq.trans (measure_theory.measure_congr measure_theory.measure.univ_pi_Ico_ae_eq_Icc) volume_Icc_pi
@[simp] theorem volume_pi_Ico_to_real {ι : Type u_1} [fintype ι] {a : ι → ℝ} {b : ι → ℝ} (h : a ≤ b) : ennreal.to_real (coe_fn volume (set.pi set.univ fun (i : ι) => set.Ico (a i) (b i))) =
finset.prod finset.univ fun (i : ι) => b i - a i := sorry
/-!
### Images of the Lebesgue measure under translation/multiplication/...
-/
theorem map_volume_add_left (a : ℝ) : coe_fn (measure_theory.measure.map (Add.add a)) volume = volume := sorry
theorem map_volume_add_right (a : ℝ) : coe_fn (measure_theory.measure.map fun (_x : ℝ) => _x + a) volume = volume := sorry
theorem smul_map_volume_mul_left {a : ℝ} (h : a ≠ 0) : ennreal.of_real (abs a) • coe_fn (measure_theory.measure.map (Mul.mul a)) volume = volume := sorry
theorem map_volume_mul_left {a : ℝ} (h : a ≠ 0) : coe_fn (measure_theory.measure.map (Mul.mul a)) volume = ennreal.of_real (abs (a⁻¹)) • volume := sorry
theorem smul_map_volume_mul_right {a : ℝ} (h : a ≠ 0) : ennreal.of_real (abs a) • coe_fn (measure_theory.measure.map fun (_x : ℝ) => _x * a) volume = volume := sorry
theorem map_volume_mul_right {a : ℝ} (h : a ≠ 0) : coe_fn (measure_theory.measure.map fun (_x : ℝ) => _x * a) volume = ennreal.of_real (abs (a⁻¹)) • volume := sorry
@[simp] theorem map_volume_neg : coe_fn (measure_theory.measure.map Neg.neg) volume = volume := sorry
end real
theorem filter.eventually.volume_pos_of_nhds_real {p : ℝ → Prop} {a : ℝ} (h : filter.eventually (fun (x : ℝ) => p x) (nhds a)) : 0 < coe_fn volume (set_of fun (x : ℝ) => p x) := sorry
|
e7659b4093f301302d2826803d57f5f0b20502b6 | a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940 | /stage0/src/Lean/Data/Lsp/Extra.lean | 2237e2c71d3e2848b9cff19d654359e536f46788 | [
"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 | 1,990 | lean | /-
Copyright (c) 2020 Marc Huisinga. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Marc Huisinga
-/
import Lean.Data.Json
import Lean.Data.JsonRpc
import Lean.Data.Lsp.Basic
/-!
This file contains Lean-specific extensions to LSP.
The following additional requests and notifications are supported:
- `textDocument/waitForDiagnostics`: Yields a response when all the diagnostics for a version of the document
greater or equal to the specified one have been emitted. If the request specifies a version above the most
recently processed one, the server will delay the response until it does receive the specified version.
Exists for synchronization purposes, e.g. during testing or when external tools might want to use our LSP server.
- `$/lean/fileProgress`: Notification that contains the ranges of the document that are still being processed
by the server.
- `$/lean/plainGoal`: Returns the goal(s) at the specified position, pretty-printed as a string.
-/
namespace Lean.Lsp
open Json
structure WaitForDiagnosticsParams where
uri : DocumentUri
version : Nat
deriving ToJson, FromJson
structure WaitForDiagnostics
instance : FromJson WaitForDiagnostics :=
⟨fun j => WaitForDiagnostics.mk⟩
instance : ToJson WaitForDiagnostics :=
⟨fun o => mkObj []⟩
structure LeanFileProgressProcessingInfo where
range : Range
deriving FromJson, ToJson
structure LeanFileProgressParams where
textDocument : VersionedTextDocumentIdentifier
processing : Array LeanFileProgressProcessingInfo
deriving FromJson, ToJson
structure PlainGoalParams extends TextDocumentPositionParams
deriving FromJson, ToJson
structure PlainGoal where
rendered : String
goals : Array String
deriving FromJson, ToJson
structure PlainTermGoalParams extends TextDocumentPositionParams
deriving FromJson, ToJson
structure PlainTermGoal where
goal : String
range : Range
deriving FromJson, ToJson
end Lean.Lsp
|
68e862789bef1daf446aa29058d6c8be49fbadd7 | 9028d228ac200bbefe3a711342514dd4e4458bff | /test/nontriviality.lean | 9a0db53c1c8272e79717f4d8a7f04c0f4b22ef9d | [
"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 | 920 | lean | import logic.nontrivial
import algebra.ordered_ring
import data.nat.basic
example {R : Type} [ordered_ring R] {a : R} (h : 0 < a) : 0 < a :=
begin
nontriviality,
guard_hyp _inst : nontrivial R,
assumption,
end
example {R : Type} [comm_ring R] {r s : R} : r * s = s * r :=
begin
nontriviality,
guard_hyp _inst : nontrivial R,
apply mul_comm,
end
example {R : Type} [ordered_ring R] : 0 ≤ (1 : R) :=
begin
nontriviality R,
guard_hyp _inst : nontrivial R,
exact zero_le_one,
end
example {R : Type} [ordered_ring R] : 0 ≤ (1 : R) :=
begin
nontriviality ℕ,
guard_hyp _inst : nontrivial ℕ,
exact zero_le_one,
end
example {R : Type} [ordered_ring R] : 0 ≤ (1 : R) :=
begin
success_if_fail { nontriviality punit },
exact zero_le_one,
end
example {R : Type} [ordered_ring R] {a : R} (h : 0 < a) : 2 ∣ 4 :=
begin
nontriviality R,
guard_hyp _inst : nontrivial R,
dec_trivial
end
|
39afaf0d1b8a90fda562e1c5d4cc1330031ce0a3 | bbecf0f1968d1fba4124103e4f6b55251d08e9c4 | /src/tactic/slice.lean | b97dd0e0eba05dc8c9d862a10d1a7842916d28b4 | [
"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 | 3,509 | lean | /-
Copyright (c) 2018 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import category_theory.category.basic
open category_theory
-- TODO someone might like to generalise this tactic to work with other associative structures.
namespace tactic
meta def repeat_with_results {α : Type} (t : tactic α) : tactic (list α) :=
(do r ← t,
s ← repeat_with_results,
return (r :: s)) <|> return []
meta def repeat_count {α : Type} (t : tactic α) : tactic ℕ :=
do r ← repeat_with_results t,
return r.length
end tactic
namespace conv
open tactic
meta def repeat_with_results {α : Type} (t : tactic α) : tactic (list α) :=
(do r ← t,
s ← repeat_with_results,
return (r :: s)) <|> return []
meta def repeat_count {α : Type} (t : tactic α) : tactic ℕ :=
do r ← repeat_with_results t,
return r.length
meta def slice (a b : ℕ) : conv unit :=
do repeat $ to_expr ``(category.assoc) >>= λ e, tactic.rewrite_target e {symm:=ff},
iterate_range (a-1) (a-1) (do conv.congr, conv.skip),
k ← repeat_count $ to_expr ``(category.assoc) >>= λ e, tactic.rewrite_target e {symm:=tt},
iterate_range (k+1+a-b) (k+1+a-b) conv.congr,
repeat $ to_expr ``(category.assoc) >>= λ e, tactic.rewrite_target e {symm:=ff},
rotate 1,
iterate_exactly' (k+1+a-b) conv.skip
meta def slice_lhs (a b : ℕ) (t : conv unit) : tactic unit :=
do conv.interactive.to_lhs,
slice a b,
t
meta def slice_rhs (a b : ℕ) (t : conv unit) : tactic unit :=
do conv.interactive.to_rhs,
slice a b,
t
namespace interactive
/--
`slice` is a conv tactic; if the current focus is a composition of several morphisms,
`slice a b` reassociates as needed, and zooms in on the `a`-th through `b`-th morphisms.
Thus if the current focus is `(a ≫ b) ≫ ((c ≫ d) ≫ e)`, then `slice 2 3` zooms to `b ≫ c`.
-/
meta def slice := conv.slice
end interactive
end conv
namespace tactic
open conv
private meta def conv_target' (c : conv unit) : tactic unit :=
do t ← target,
(new_t, pr) ← c.convert t,
replace_target new_t pr,
try tactic.triv, try (tactic.reflexivity reducible)
namespace interactive
open interactive
open lean.parser
/--
`slice_lhs a b { tac }` zooms to the left hand side, uses associativity for categorical
composition as needed, zooms in on the `a`-th through `b`-th morphisms, and invokes `tac`.
-/
meta def slice_lhs (a b : parse small_nat) (t : conv.interactive.itactic) : tactic unit :=
do conv_target' (conv.interactive.to_lhs >> slice a b >> t)
/--
`slice_rhs a b { tac }` zooms to the right hand side, uses associativity for categorical
composition as needed, zooms in on the `a`-th through `b`-th morphisms, and invokes `tac`.
-/
meta def slice_rhs (a b : parse small_nat) (t : conv.interactive.itactic) : tactic unit :=
do conv_target' (conv.interactive.to_rhs >> slice a b >> t)
end interactive
end tactic
/--
`slice_lhs a b { tac }` zooms to the left hand side, uses associativity for categorical
composition as needed, zooms in on the `a`-th through `b`-th morphisms, and invokes `tac`.
`slice_rhs a b { tac }` zooms to the right hand side, uses associativity for categorical
composition as needed, zooms in on the `a`-th through `b`-th morphisms, and invokes `tac`.
-/
add_tactic_doc
{ name := "slice",
category := doc_category.tactic,
decl_names := [`tactic.interactive.slice_lhs, `tactic.interactive.slice_rhs],
tags := ["category theory"] }
|
066367db8fd7b86bc8196007eb4f3156e162dd3c | 4d2583807a5ac6caaffd3d7a5f646d61ca85d532 | /src/tactic/rcases.lean | cb3dd3208d1c43a96a53a8f18599dbe3fb3196b0 | [
"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 | 39,969 | 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 data.dlist
import tactic.core
import tactic.clear
/-!
# Recursive cases (`rcases`) tactic and related tactics
`rcases` is a tactic that will perform `cases` recursively, according to a pattern. It is used to
destructure hypotheses or expressions composed of inductive types like `h1 : a ∧ b ∧ c ∨ d` or
`h2 : ∃ x y, trans_rel R x y`. Usual usage might be `rcases h1 with ⟨ha, hb, hc⟩ | hd` or
`rcases h2 with ⟨x, y, _ | ⟨z, hxz, hzy⟩⟩` for these examples.
Each element of an `rcases` pattern is matched against a particular local hypothesis (most of which
are generated during the execution of `rcases` and represent individual elements destructured from
the input expression). An `rcases` pattern has the following grammar:
* A name like `x`, which names the active hypothesis as `x`.
* A blank `_`, which does nothing (letting the automatic naming system used by `cases` name the
hypothesis).
* A hyphen `-`, which clears the active hypothesis and any dependents.
* The keyword `rfl`, which expects the hypothesis to be `h : a = b`, and calls `subst` on the
hypothesis (which has the effect of replacing `b` with `a` everywhere or vice versa).
* A type ascription `p : ty`, which sets the type of the hypothesis to `ty` and then matches it
against `p`. (Of course, `ty` must unify with the actual type of `h` for this to work.)
* A tuple pattern `⟨p1, p2, p3⟩`, which matches a constructor with many arguments, or a series
of nested conjunctions or existentials. For example if the active hypothesis is `a ∧ b ∧ c`,
then the conjunction will be destructured, and `p1` will be matched against `a`, `p2` against `b`
and so on.
* An alteration pattern `p1 | p2 | p3`, which matches an inductive type with multiple constructors,
or a nested disjunction like `a ∨ b ∨ c`.
The patterns are fairly liberal about the exact shape of the constructors, and will insert
additional alternation branches and tuple arguments if there are not enough arguments provided, and
reuse the tail for further matches if there are too many arguments provided to alternation and
tuple patterns.
This file also contains the `obtain` and `rintro` tactics, which use the same syntax of `rcases`
patterns but with a slightly different use case:
* `rintro` (or `rintros`) is used like `rintro x ⟨y, z⟩` and is the same as `intros` followed by
`rcases` on the newly introduced arguments.
* `obtain` is the same as `rcases` but with a syntax styled after `have` rather than `cases`.
`obtain ⟨hx, hy⟩ | hz := foo` is equivalent to `rcases foo with ⟨hx, hy⟩ | hz`. Unlike `rcases`,
`obtain` also allows one to omit `:= foo`, although a type must be provided in this case,
as in `obtain ⟨hx, hy⟩ | hz : a ∧ b ∨ c`, in which case it produces a subgoal for proving
`a ∧ b ∨ c` in addition to the subgoals `hx : a, hy : b |- goal` and `hz : c |- goal`.
## Tags
rcases, rintro, obtain, destructuring, cases, pattern matching, match
-/
open lean lean.parser
namespace tactic
/-!
These synonyms for `list` are used to clarify the meanings of the many
usages of lists in this module.
- `listΣ` is used where a list represents a disjunction, such as the
list of possible constructors of an inductive type.
- `listΠ` is used where a list represents a conjunction, such as the
list of arguments of an individual constructor.
These are merely type synonyms, and so are not checked for consistency
by the compiler.
The `def`/`local notation` combination makes Lean retain these
annotations in reported types.
-/
/-- A list, with a disjunctive meaning (like a list of inductive constructors, or subgoals) -/
@[reducible] def list_Sigma := list
/-- A list, with a conjunctive meaning (like a list of constructor arguments, or hypotheses) -/
@[reducible] def list_Pi := list
local notation `listΣ` := list_Sigma
local notation `listΠ` := list_Pi
/-- A metavariable representing a subgoal, together with a list of local constants to clear. -/
@[reducible] meta def uncleared_goal := list expr × expr
/--
An `rcases` pattern can be one of the following, in a nested combination:
* A name like `foo`
* The special keyword `rfl` (for pattern matching on equality using `subst`)
* A hyphen `-`, which clears the active hypothesis and any dependents.
* A type ascription like `pat : ty` (parentheses are optional)
* A tuple constructor like `⟨p1, p2, p3⟩`
* An alternation / variant pattern `p1 | p2 | p3`
Parentheses can be used for grouping; alternation is higher precedence than type ascription, so
`p1 | p2 | p3 : ty` means `(p1 | p2 | p3) : ty`.
N-ary alternations are treated as a group, so `p1 | p2 | p3` is not the same as `p1 | (p2 | p3)`,
and similarly for tuples. However, note that an n-ary alternation or tuple can match an n-ary
conjunction or disjunction, because if the number of patterns exceeds the number of constructors in
the type being destructed, the extra patterns will match on the last element, meaning that
`p1 | p2 | p3` will act like `p1 | (p2 | p3)` when matching `a1 ∨ a2 ∨ a3`. If matching against a
type with 3 constructors, `p1 | (p2 | p3)` will act like `p1 | (p2 | p3) | _` instead.
-/
meta inductive rcases_patt : Type
| one : name → rcases_patt
| clear : rcases_patt
| typed : rcases_patt → pexpr → rcases_patt
| tuple : listΠ rcases_patt → rcases_patt
| alts : listΣ rcases_patt → rcases_patt
namespace rcases_patt
meta instance inhabited : inhabited rcases_patt :=
⟨one `_⟩
/-- Get the name from a pattern, if provided -/
meta def name : rcases_patt → option name
| (one `_) := none
| (one `rfl) := none
| (one n) := some n
| (typed p _) := p.name
| (alts [p]) := p.name
| _ := none
/-- Interpret an rcases pattern as a tuple, where `p` becomes `⟨p⟩`
if `p` is not already a tuple. -/
meta def as_tuple : rcases_patt → listΠ rcases_patt
| (tuple ps) := ps
| p := [p]
/-- Interpret an rcases pattern as an alternation, where non-alternations are treated as one
alternative. -/
meta def as_alts : rcases_patt → listΣ rcases_patt
| (alts ps) := ps
| p := [p]
/-- Convert a list of patterns to a tuple pattern, but mapping `[p]` to `p` instead of `⟨p⟩`. -/
meta def tuple' : listΠ rcases_patt → rcases_patt
| [p] := p
| ps := tuple ps
/-- Convert a list of patterns to an alternation pattern, but mapping `[p]` to `p` instead of
a unary alternation `|p`. -/
meta def alts' : listΣ rcases_patt → rcases_patt
| [p] := p
| ps := alts ps
/-- This function is used for producing rcases patterns based on a case tree. Suppose that we have
a list of patterns `ps` that will match correctly against the branches of the case tree for one
constructor. This function will merge tuples at the end of the list, so that `[a, b, ⟨c, d⟩]`
becomes `⟨a, b, c, d⟩` instead of `⟨a, b, ⟨c, d⟩⟩`.
We must be careful to turn `[a, ⟨⟩]` into `⟨a, ⟨⟩⟩` instead of `⟨a⟩` (which will not perform the
nested match). -/
meta def tuple₁_core : listΠ rcases_patt → listΠ rcases_patt
| [] := []
| [tuple []] := [tuple []]
| [tuple ps] := ps
| (p :: ps) := p :: tuple₁_core ps
/-- This function is used for producing rcases patterns based on a case tree. This is like
`tuple₁_core` but it produces a pattern instead of a tuple pattern list, converting `[n]` to `n`
instead of `⟨n⟩` and `[]` to `_`, and otherwise just converting `[a, b, c]` to `⟨a, b, c⟩`. -/
meta def tuple₁ : listΠ rcases_patt → rcases_patt
| [] := default _
| [one n] := one n
| ps := tuple (tuple₁_core ps)
/-- This function is used for producing rcases patterns based on a case tree. Here we are given
the list of patterns to apply to each argument of each constructor after the main case, and must
produce a list of alternatives with the same effect. This function calls `tuple₁` to make the
individual alternatives, and handles merging `[a, b, c | d]` to `a | b | c | d` instead of
`a | b | (c | d)`. -/
meta def alts₁_core : listΣ (listΠ rcases_patt) → listΣ rcases_patt
| [] := []
| [[alts ps]] := ps
| (p :: ps) := tuple₁ p :: alts₁_core ps
/-- This function is used for producing rcases patterns based on a case tree. This is like
`alts₁_core`, but it produces a cases pattern directly instead of a list of alternatives. We
specially translate the empty alternation to `⟨⟩`, and translate `|(a | b)` to `⟨a | b⟩` (because we
don't have any syntax for unary alternation). Otherwise we can use the regular merging of
alternations at the last argument so that `a | b | (c | d)` becomes `a | b | c | d`. -/
meta def alts₁ : listΣ (listΠ rcases_patt) → rcases_patt
| [[]] := tuple []
| [[alts ps]] := tuple [alts ps]
| ps := alts' (alts₁_core ps)
meta instance has_reflect : has_reflect rcases_patt
| (one n) := `(_)
| clear := `(_)
| (typed l e) :=
(`(typed).subst (has_reflect l)).subst (reflect e)
| (tuple l) := `(λ l, tuple l).subst $
by haveI := has_reflect; exact list.reflect l
| (alts l) := `(λ l, alts l).subst $
by haveI := has_reflect; exact list.reflect l
/-- Formats an `rcases` pattern. If the `bracket` argument is true, then it will be
printed at high precedence, i.e. it will have parentheses around it if it is not already a tuple
or atomic name. -/
protected meta def format : ∀ bracket : bool, rcases_patt → tactic _root_.format
| _ (one n) := pure $ to_fmt n
| _ clear := pure "-"
| _ (tuple []) := pure "⟨⟩"
| _ (tuple ls) := do
fs ← ls.mmap $ format ff,
pure $ "⟨" ++ _root_.format.group (_root_.format.nest 1 $
_root_.format.join $ list.intersperse ("," ++ _root_.format.line) fs) ++ "⟩"
| br (alts ls) := do
fs ← ls.mmap $ format tt,
let fmt := _root_.format.join $ list.intersperse (↑" |" ++ _root_.format.space) fs,
pure $ if br then _root_.format.bracket "(" ")" fmt else fmt
| br (typed p e) := do
fp ← format ff p,
fe ← pp e,
let fmt := fp ++ " : " ++ fe,
pure $ if br then _root_.format.bracket "(" ")" fmt else fmt
meta instance has_to_tactic_format : has_to_tactic_format rcases_patt := ⟨rcases_patt.format ff⟩
end rcases_patt
/-- Takes the number of fields of a single constructor and patterns to match its fields against
(not necessarily the same number). The returned lists each contain one element per field of the
constructor. The `name` is the name which will be used in the top-level `cases` tactic, and the
`rcases_patt` is the pattern which the field will be matched against by subsequent `cases`
tactics. -/
meta def rcases.process_constructor :
nat → listΠ rcases_patt → listΠ name × listΠ rcases_patt
| 0 ps := ([], [])
| 1 [] := ([`_], [default _])
| 1 [p] := ([p.name.get_or_else `_], [p])
-- The interesting case: we matched the last field against multiple
-- patterns, so split off the remaining patterns into a subsequent
-- match. This handles matching `α × β × γ` against `⟨a, b, c⟩`.
| 1 ps := ([`_], [rcases_patt.tuple ps])
| (n+1) ps :=
let hd := ps.head, (ns, tl) := rcases.process_constructor n ps.tail in
(hd.name.get_or_else `_ :: ns, hd :: tl)
/-- Takes a list of constructor names, and an (alternation) list of patterns, and matches each
pattern against its constructor. It returns the list of names that will be passed to `cases`,
and the list of `(constructor name, patterns)` for each constructor, where `patterns` is the
(conjunctive) list of patterns to apply to each constructor argument. -/
meta def rcases.process_constructors (params : nat) :
listΣ name → listΣ rcases_patt →
tactic (dlist name × listΣ (name × listΠ rcases_patt))
| [] ps := pure (dlist.empty, [])
| (c::cs) ps := do
n ← mk_const c >>= get_arity,
let (h, t) := (match cs, ps.tail with
-- We matched the last constructor against multiple patterns,
-- so split off the remaining constructors. This handles matching
-- `α ⊕ β ⊕ γ` against `a|b|c`.
| [], _::_ := ([rcases_patt.alts ps], [])
| _, _ := (ps.head.as_tuple, ps.tail)
end : _),
let (ns, ps) := rcases.process_constructor (n - params) h,
(l, r) ← rcases.process_constructors cs t,
pure (dlist.of_list ns ++ l, (c, ps) :: r)
/-- Like `zip`, but only elements satisfying a matching predicate `p` will go in the list,
and elements of the first list that fail to match the second list will be skipped. -/
private def align {α β} (p : α → β → Prop) [∀ a b, decidable (p a b)] :
list α → list β → list (α × β)
| (a::as) (b::bs) :=
if p a b then (a, b) :: align as bs else align as (b::bs)
| _ _ := []
/-- Given a local constant `e`, get its type. *But* if `e` does not exist, go find a hypothesis
with the same pretty name as `e` and get it instead. This is needed because we can sometimes lose
track of the unique names of hypotheses when they are revert/intro'd by `change` and `cases`. (A
better solution would be for these tactics to return a map of renamed hypotheses so that we don't
lose track of them.) -/
private meta def get_local_and_type (e : expr) : tactic (expr × expr) :=
(do t ← infer_type e, pure (t, e)) <|> (do
e ← get_local e.local_pp_name,
t ← infer_type e, pure (t, e))
/--
* `rcases_core p e` will match a pattern `p` against a local hypothesis `e`.
It returns the list of subgoals that were produced.
* `rcases.continue pes` will match a (conjunctive) list of `(p, e)` pairs which refer to
patterns and local hypotheses to match against, and applies all of them. Note that this can
involve matching later arguments multiple times given earlier arguments, for example
`⟨a | b, ⟨c, d⟩⟩` performs the `⟨c, d⟩` match twice, once on the `a` branch and once on `b`.
-/
meta mutual def rcases_core, rcases.continue
with rcases_core : rcases_patt → expr → tactic (list uncleared_goal)
| (rcases_patt.one `rfl) e := do
(t, e) ← get_local_and_type e,
subst' e,
list.map (prod.mk []) <$> get_goals
-- If the pattern is any other name, we already bound the name in the
-- top-level `cases` tactic, so there is no more work to do for it.
| (rcases_patt.one _) _ := list.map (prod.mk []) <$> get_goals
| rcases_patt.clear e := do
m ← try_core (get_local_and_type e),
list.map (prod.mk $ m.elim [] (λ ⟨_, e⟩, [e])) <$> get_goals
| (rcases_patt.typed p ty) e := do
(t, e) ← get_local_and_type e,
ty ← i_to_expr_no_subgoals ``(%%ty : Sort*),
unify t ty,
t ← instantiate_mvars t,
ty ← instantiate_mvars ty,
e ← if t =ₐ ty then pure e else
change_core ty (some e) >> get_local e.local_pp_name,
rcases_core p e
| (rcases_patt.alts [p]) e := rcases_core p e
| pat e := do
(t, e) ← get_local_and_type e,
t ← whnf t,
env ← get_env,
let I := t.get_app_fn.const_name,
let pat := pat.as_alts,
(ids, r, l) ← (if I ≠ `quot
then do
when (¬env.is_inductive I) $
fail format!"rcases tactic failed: {e} : {I} is not an inductive datatype",
let params := env.inductive_num_params I,
let c := env.constructors_of I,
(ids, r) ← rcases.process_constructors params c pat,
l ← cases_core e ids.to_list,
pure (ids, r, l)
else do
(ids, r) ← rcases.process_constructors 2 [`quot.mk] pat,
[(_, d)] ← induction e ids.to_list `quot.induction_on |
fail format!"quotient induction on {e} failed. Maybe goal is not in Prop?",
-- the result from `induction` is missing the information that the original constructor was
-- `quot.mk` so we fix this up:
pure (ids, r, [(`quot.mk, d)])),
gs ← get_goals,
-- `cases_core` may not generate a new goal for every constructor,
-- as some constructors may be impossible for type reasons. (See its
-- documentation.) Match up the new goals with our remaining work
-- by constructor name.
let ls := align (λ (a : name × _) (b : _ × name × _), a.1 = b.2.1) r (gs.zip l),
list.join <$> ls.mmap (λ⟨⟨_, ps⟩, g, _, hs, _⟩, set_goals [g] >> rcases.continue (ps.zip hs))
with rcases.continue : listΠ (rcases_patt × expr) → tactic (list uncleared_goal)
| [] := list.map (prod.mk []) <$> get_goals
| ((pat, e) :: pes) := do
gs ← rcases_core pat e,
list.join <$> gs.mmap (λ ⟨cs, g⟩, do
set_goals [g],
ugs ← rcases.continue pes,
pure $ ugs.map $ λ ⟨cs', gs⟩, (cs ++ cs', gs))
/-- Given a list of `uncleared_goal`s, each of which is a goal metavariable and
a list of variables to clear, actually perform the clear and set the goals with the result. -/
meta def clear_goals (ugs : list uncleared_goal) : tactic unit := do
gs ← ugs.mmap (λ ⟨cs, g⟩, do
set_goals [g],
cs ← cs.mfoldr (λ c cs,
(do (_, c) ← get_local_and_type c, pure (c :: cs)) <|> pure cs) [],
clear' tt cs,
[g] ← get_goals,
pure g),
set_goals gs
/-- `rcases h e pat` performs case distinction on `e` using `pat` to
name the arising new variables and assumptions. If `h` is `some` name,
a new assumption `h : e = pat` will relate the expression `e` with the
current pattern. See the module comment for the syntax of `pat`. -/
meta def rcases (h : option name) (p : pexpr) (pat : rcases_patt) : tactic unit := do
let p := match pat with
| rcases_patt.typed _ ty := ``(%%p : %%ty)
| _ := p
end,
e ← match h with
| some h := do
x ← get_unused_name $ pat.name.get_or_else `this,
interactive.generalize h () (p, x),
get_local x
| none := i_to_expr p
end,
if e.is_local_constant then
focus1 (rcases_core pat e >>= clear_goals)
else do
x ← pat.name.elim mk_fresh_name pure,
n ← revert_kdependencies e semireducible,
tactic.generalize e x <|> (do
t ← infer_type e,
tactic.assertv x t e,
get_local x >>= tactic.revert,
pure ()),
h ← tactic.intro1,
focus1 (rcases_core pat h >>= clear_goals)
/-- `rcases_many es pats` performs case distinction on the `es` using `pat` to
name the arising new variables and assumptions.
See the module comment for the syntax of `pat`. -/
meta def rcases_many (ps : listΠ pexpr) (pat : rcases_patt) : tactic unit := do
let (_, pats) := rcases.process_constructor ps.length pat.as_tuple,
pes ← (ps.zip pats).mmap (λ ⟨p, pat⟩, do
let p := match pat with
| rcases_patt.typed _ ty := ``(%%p : %%ty)
| _ := p
end,
e ← i_to_expr p,
if e.is_local_constant then
pure (pat, e)
else do
x ← pat.name.elim mk_fresh_name pure,
n ← revert_kdependencies e semireducible,
tactic.generalize e x <|> (do
t ← infer_type e,
tactic.assertv x t e,
get_local x >>= tactic.revert,
pure ()),
prod.mk pat <$> tactic.intro1),
focus1 (rcases.continue pes >>= clear_goals)
/-- `rintro pat₁ pat₂ ... patₙ` introduces `n` arguments, then pattern matches on the `patᵢ` using
the same syntax as `rcases`. -/
meta def rintro (ids : listΠ rcases_patt) : tactic unit :=
do l ← ids.mmap (λ id, do
e ← intro $ id.name.get_or_else `_,
pure (id, e)),
focus1 (rcases.continue l >>= clear_goals)
/-- Like `zip_with`, but if the lists don't match in length, the excess elements will be put at the
end of the result. -/
def merge_list {α} (m : α → α → α) : list α → list α → list α
| [] l₂ := l₂
| l₁ [] := l₁
| (a :: l₁) (b :: l₂) := m a b :: merge_list l₁ l₂
/-- Merge two `rcases` patterns. This is used to underapproximate a case tree by an `rcases`
pattern. The two patterns come from cases in two branches, that due to the syntax of `rcases`
patterns are forced to overlap. The rule here is that we take only the case splits that are in
common between both branches. For example if one branch does `⟨a, b⟩` and the other does `c`,
then we return `c` because we don't know that a case on `c` would be safe to do. -/
meta def rcases_patt.merge : rcases_patt → rcases_patt → rcases_patt
| (rcases_patt.alts p₁) p₂ := rcases_patt.alts (merge_list rcases_patt.merge p₁ p₂.as_alts)
| p₁ (rcases_patt.alts p₂) := rcases_patt.alts (merge_list rcases_patt.merge p₁.as_alts p₂)
| (rcases_patt.tuple p₁) p₂ := rcases_patt.tuple (merge_list rcases_patt.merge p₁ p₂.as_tuple)
| p₁ (rcases_patt.tuple p₂) := rcases_patt.tuple (merge_list rcases_patt.merge p₁.as_tuple p₂)
| (rcases_patt.typed p₁ e) p₂ := rcases_patt.typed (p₁.merge p₂) e
| p₁ (rcases_patt.typed p₂ e) := rcases_patt.typed (p₁.merge p₂) e
| (rcases_patt.one `rfl) (rcases_patt.one `rfl) := rcases_patt.one `rfl
| (rcases_patt.one `_) p := p
| p (rcases_patt.one `_) := p
| rcases_patt.clear p := p
| p rcases_patt.clear := p
| (rcases_patt.one n) _ := rcases_patt.one n
/--
* `rcases_hint_core depth e` does the same as `rcases p e`, except the pattern `p` is an output
instead of an input, controlled only by the case depth argument `depth`. We use `cases` to depth
`depth` and then reconstruct an `rcases` pattern `p` that would, if passed to `rcases`, perform
the same thing as the case tree we just constructed (or at least, the nearest expressible
approximation to this.)
* `rcases_hint.process_constructors depth cs l` takes a list of constructor names `cs` and a
matching list `l` of elements `(g, c', hs, _)` where `c'` is a constructor name (used for
alignment with `cs`), `g` is the subgoal, and `hs` is the list of local hypotheses created by
`cases` in that subgoal. It matches on all of them, and then produces a `ΣΠ`-list of `rcases`
patterns describing the result, and the list of generated subgoals.
* `rcases_hint.continue depth es` does the same as `rcases.continue (ps.zip es)`, except the
patterns `ps` are an output instead of an input, created by matching on everything to depth
`depth` and recording the successful cases. It returns `ps`, and the list of generated subgoals.
-/
meta mutual def rcases_hint_core, rcases_hint.process_constructors, rcases_hint.continue
with rcases_hint_core : ℕ → expr → tactic (rcases_patt × list expr)
| depth e := do
(t, e) ← get_local_and_type e,
t ← whnf t,
env ← get_env,
let I := t.get_app_fn.const_name,
(do guard (I = ``eq),
subst' e,
prod.mk (rcases_patt.one `rfl) <$> get_goals) <|>
(do
let c := env.constructors_of I,
some l ← try_core (guard (depth ≠ 0) >> cases_core e) |
let n := match e.local_pp_name with name.anonymous := `_ | n := n end in
prod.mk (rcases_patt.one n) <$> get_goals,
gs ← get_goals,
if gs.empty then
pure (rcases_patt.tuple [], [])
else do
(ps, gs') ← rcases_hint.process_constructors (depth - 1) c (gs.zip l),
pure (rcases_patt.alts₁ ps, gs'))
with rcases_hint.process_constructors : ℕ → listΣ name →
list (expr × name × listΠ expr × list (name × expr)) →
tactic (listΣ (listΠ rcases_patt) × list expr)
| depth [] _ := pure ([], [])
| depth cs [] := pure (cs.map (λ _, []), [])
| depth (c::cs) ls@((g, c', hs, _) :: l) :=
if c ≠ c' then do
(ps, gs) ← rcases_hint.process_constructors depth cs ls,
pure ([] :: ps, gs)
else do
(p, gs) ← set_goals [g] >> rcases_hint.continue depth hs,
(ps, gs') ← rcases_hint.process_constructors depth cs l,
pure (p :: ps, gs ++ gs')
with rcases_hint.continue : ℕ → listΠ expr → tactic (listΠ rcases_patt × list expr)
| depth [] := prod.mk [] <$> get_goals
| depth (e :: es) := do
(p, gs) ← rcases_hint_core depth e,
(ps, gs') ← gs.mfoldl (λ (r : listΠ rcases_patt × list expr) g,
do (ps, gs') ← set_goals [g] >> rcases_hint.continue depth es,
pure (merge_list rcases_patt.merge r.1 ps, r.2 ++ gs')) ([], []),
pure (p :: ps, gs')
/--
* `rcases? e` is like `rcases e with ...`, except it generates `...` by matching on everything it
can, and it outputs an `rcases` invocation that should have the same effect.
* `rcases? e : n` can be used to control the depth of case splits (especially important for
recursive types like `nat`, which can be cased as many times as you like). -/
meta def rcases_hint (p : pexpr) (depth : nat) : tactic rcases_patt :=
do e ← i_to_expr p,
if e.is_local_constant then
focus1 $ do (p, gs) ← rcases_hint_core depth e, set_goals gs, pure p
else do
x ← mk_fresh_name,
n ← revert_kdependencies e semireducible,
tactic.generalize e x <|> (do
t ← infer_type e,
tactic.assertv x t e,
get_local x >>= tactic.revert,
pure ()),
h ← tactic.intro1,
focus1 $ do (p, gs) ← rcases_hint_core depth h, set_goals gs, pure p
/--
* `rcases? ⟨e1, e2, e3⟩` is like `rcases ⟨e1, e2, e3⟩ with ...`, except it
generates `...` by matching on everything it can, and it outputs an `rcases`
invocation that should have the same effect.
* `rcases? ⟨e1, e2, e3⟩ : n` can be used to control the depth of case splits
(especially important for recursive types like `nat`, which can be cased as many
times as you like). -/
meta def rcases_hint_many (ps : list pexpr) (depth : nat) : tactic (listΠ rcases_patt) :=
do es ← ps.mmap (λ p, do
e ← i_to_expr p,
if e.is_local_constant then pure e
else do
x ← mk_fresh_name,
n ← revert_kdependencies e semireducible,
tactic.generalize e x <|> (do
t ← infer_type e,
tactic.assertv x t e,
get_local x >>= tactic.revert,
pure ()),
tactic.intro1),
focus1 $ do
(ps, gs) ← rcases_hint.continue depth es,
set_goals gs,
pure ps
/--
* `rintro?` is like `rintro ...`, except it generates `...` by introducing and matching on
everything it can, and it outputs an `rintro` invocation that should have the same effect.
* `rintro? : n` can be used to control the depth of case splits (especially important for
recursive types like `nat`, which can be cased as many times as you like). -/
meta def rintro_hint (depth : nat) : tactic (listΠ rcases_patt) :=
do l ← intros,
focus1 $ do
(p, gs) ← rcases_hint.continue depth l,
set_goals gs,
pure p
setup_tactic_parser
/--
* `rcases_patt_parse_hi` will parse a high precedence `rcases` pattern, `patt_hi`.
This means only tuples and identifiers are allowed; alternations and type ascriptions
require `(...)` instead, which switches to `patt`.
* `rcases_patt_parse` will parse a low precedence `rcases` pattern, `patt`. This consists of a
`patt_med` (which deals with alternations), optionally followed by a `: ty` type ascription. The
expression `ty` is at `texpr` precedence because it can appear at the end of a tactic, for
example in `rcases e with x : ty <|> skip`.
* `rcases_patt_parse_list` will parse an alternation list, `patt_med`, one or more `patt`
patterns separated by `|`. It does not parse a `:` at the end, so that `a | b : ty` parses as
`(a | b) : ty` where `a | b` is the `patt_med` part.
* `rcases_patt_parse_list_rest a` parses an alternation list after the initial pattern, `| b | c`.
```lean
patt ::= patt_med (":" expr)?
patt_med ::= (patt_hi "|")* patt_hi
patt_hi ::= id | "rfl" | "_" | "⟨" (patt ",")* patt "⟩" | "(" patt ")"
```
-/
meta mutual def
rcases_patt_parse_hi, rcases_patt_parse, rcases_patt_parse_list, rcases_patt_parse_list_rest
with rcases_patt_parse_hi : parser rcases_patt
| x := with_desc "patt_hi"
((brackets "(" ")" rcases_patt_parse) <|>
(rcases_patt.tuple <$> brackets "⟨" "⟩" (sep_by (tk ",") rcases_patt_parse)) <|>
(tk "-" $> rcases_patt.clear) <|>
(rcases_patt.one <$> ident_)) x
with rcases_patt_parse : parser rcases_patt
| x := with_desc "patt" (do
pat ← rcases_patt.alts' <$> rcases_patt_parse_list,
(tk ":" *> pat.typed <$> texpr) <|> pure pat) x
with rcases_patt_parse_list : parser (listΣ rcases_patt)
| x := (with_desc "patt_med" $ rcases_patt_parse_hi >>= rcases_patt_parse_list_rest) x
with rcases_patt_parse_list_rest : rcases_patt → parser (listΣ rcases_patt)
| pat :=
(tk "|" *> list.cons pat <$> rcases_patt_parse_list) <|>
-- hack to support `-|-` patterns, because `|-` is a token
(tk "|-" *> list.cons pat <$> rcases_patt_parse_list_rest rcases_patt.clear) <|>
pure [pat]
/-- Parse the optional depth argument `(: n)?` of `rcases?` and `rintro?`, with default depth 5. -/
meta def rcases_parse_depth : parser nat :=
do o ← (tk ":" *> small_nat)?, pure $ o.get_or_else 5
/-- The arguments to `rcases`, which in fact dispatch to several other tactics.
* `rcases? expr (: n)?` or `rcases? ⟨expr, ...⟩ (: n)?` calls `rcases_hint`
* `rcases? ⟨expr, ...⟩ (: n)?` calls `rcases_hint_many`
* `rcases (h :)? expr (with patt)?` calls `rcases`
* `rcases ⟨expr, ...⟩ (with patt)?` calls `rcases_many`
-/
@[derive has_reflect]
meta inductive rcases_args
| hint (tgt : pexpr ⊕ list pexpr) (depth : nat)
| rcases (name : option name) (tgt : pexpr) (pat : rcases_patt)
| rcases_many (tgt : listΠ pexpr) (pat : rcases_patt)
/-- Syntax for a `rcases` pattern:
* `rcases? expr (: n)?`
* `rcases (h :)? expr (with patt_list (: expr)?)?`. -/
meta def rcases_parse : parser rcases_args :=
with_desc "('?' expr (: n)?) | ((h :)? expr (with patt)?)" $ do
hint ← (tk "?")?,
p ← (sum.inr <$> brackets "⟨" "⟩" (sep_by (tk ",") (parser.pexpr 0))) <|>
(sum.inl <$> texpr),
match hint with
| none := do
p ← (do
sum.inl (expr.local_const h _ _ _) ← pure p,
tk ":" *> (@sum.inl _ (pexpr ⊕ list pexpr) ∘ prod.mk h) <$> texpr) <|>
pure (sum.inr p),
ids ← (tk "with" *> rcases_patt_parse)?,
let ids := ids.get_or_else (rcases_patt.tuple []),
pure $ match p with
| sum.inl (name, tgt) := rcases_args.rcases (some name) tgt ids
| sum.inr (sum.inl tgt) := rcases_args.rcases none tgt ids
| sum.inr (sum.inr tgts) := rcases_args.rcases_many tgts ids
end
| some _ := do
depth ← rcases_parse_depth,
pure $ rcases_args.hint p depth
end
/--
`rintro_patt_parse_hi` and `rintro_patt_parse` are like `rcases_patt_parse`, but is used for
parsing top level `rintro` patterns, which allow sequences like `(x y : t)` in addition to simple
`rcases` patterns.
* `rintro_patt_parse_hi` will parse a high precedence `rcases` pattern, `rintro_patt_hi` below.
This means only tuples and identifiers are allowed; alternations and type ascriptions
require `(...)` instead, which switches to `patt`.
* `rintro_patt_parse` will parse a low precedence `rcases` pattern, `rintro_patt` below.
This consists of either a sequence of patterns `p1 p2 p3` or an alternation list `p1 | p2 | p3`
treated as a single pattern, optionally followed by a `: ty` type ascription, which applies to
every pattern in the list.
* `rintro_patt_parse_low` parses `rintro_patt_low`, which is the same as `rintro_patt_parse tt` but
it does not permit an unparenthesized alternation list, it must have the form `p1 p2 p3 (: ty)?`.
```lean
rintro_patt ::= (rintro_patt_hi+ | patt_med) (":" expr)?
rintro_patt_low ::= rintro_patt_hi* (":" expr)?
rintro_patt_hi ::= patt_hi | "(" rintro_patt ")"
```
-/
meta mutual def rintro_patt_parse_hi, rintro_patt_parse'
with rintro_patt_parse_hi : parser (listΠ rcases_patt)
| x := (with_desc "rintro_patt_hi" $
brackets "(" ")" (rintro_patt_parse' tt) <|>
(do p ← rcases_patt_parse_hi, pure [p])) x
with rintro_patt_parse' : bool → parser (listΠ rcases_patt)
| med := do
ll ← rintro_patt_parse_hi*,
pats ← match med, ll.join with
| tt, [] := failure
| tt, [pat] := do l ← rcases_patt_parse_list_rest pat, pure [rcases_patt.alts' l]
| _, pats := pure pats
end,
(do tk ":", e ← texpr, pure (pats.map (λ p, rcases_patt.typed p e))) <|>
pure pats
/--
`rintro_patt_parse` will parse a low precedence `rcases` pattern, `rintro_patt` below.
This consists of either a sequence of patterns `p1 p2 p3` or an alternation list `p1 | p2 | p3`
treated as a single pattern, optionally followed by a `: ty` type ascription, which applies to
every pattern in the list.
-/
meta def rintro_patt_parse := with_desc "rintro_patt" $ rintro_patt_parse' tt
/--
`rintro_patt_parse_low` parses `rintro_patt_low`, which is the same as `rintro_patt_parse tt` but
it does not permit an unparenthesized alternation list, it must have the form `p1 p2 p3 (: ty)?`.
-/
meta def rintro_patt_parse_low := with_desc "rintro_patt_low" $ rintro_patt_parse' ff
/-- Syntax for a `rintro` pattern: `('?' (: n)?) | rintro_patt`. -/
meta def rintro_parse : parser (listΠ rcases_patt ⊕ nat) :=
with_desc "('?' (: n)?) | patt*" $
(tk "?" >> sum.inr <$> rcases_parse_depth) <|>
sum.inl <$> rintro_patt_parse_low
namespace interactive
open interactive interactive.types expr
/--
`rcases` is a tactic that will perform `cases` recursively, according to a pattern. It is used to
destructure hypotheses or expressions composed of inductive types like `h1 : a ∧ b ∧ c ∨ d` or
`h2 : ∃ x y, trans_rel R x y`. Usual usage might be `rcases h1 with ⟨ha, hb, hc⟩ | hd` or
`rcases h2 with ⟨x, y, _ | ⟨z, hxz, hzy⟩⟩` for these examples.
Each element of an `rcases` pattern is matched against a particular local hypothesis (most of which
are generated during the execution of `rcases` and represent individual elements destructured from
the input expression). An `rcases` pattern has the following grammar:
* A name like `x`, which names the active hypothesis as `x`.
* A blank `_`, which does nothing (letting the automatic naming system used by `cases` name the
hypothesis).
* A hyphen `-`, which clears the active hypothesis and any dependents.
* The keyword `rfl`, which expects the hypothesis to be `h : a = b`, and calls `subst` on the
hypothesis (which has the effect of replacing `b` with `a` everywhere or vice versa).
* A type ascription `p : ty`, which sets the type of the hypothesis to `ty` and then matches it
against `p`. (Of course, `ty` must unify with the actual type of `h` for this to work.)
* A tuple pattern `⟨p1, p2, p3⟩`, which matches a constructor with many arguments, or a series
of nested conjunctions or existentials. For example if the active hypothesis is `a ∧ b ∧ c`,
then the conjunction will be destructured, and `p1` will be matched against `a`, `p2` against `b`
and so on.
* An alteration pattern `p1 | p2 | p3`, which matches an inductive type with multiple constructors,
or a nested disjunction like `a ∨ b ∨ c`.
A pattern like `⟨a, b, c⟩ | ⟨d, e⟩` will do a split over the inductive datatype,
naming the first three parameters of the first constructor as `a,b,c` and the
first two of the second constructor `d,e`. If the list is not as long as the
number of arguments to the constructor or the number of constructors, the
remaining variables will be automatically named. If there are nested brackets
such as `⟨⟨a⟩, b | c⟩ | d` then these will cause more case splits as necessary.
If there are too many arguments, such as `⟨a, b, c⟩` for splitting on
`∃ x, ∃ y, p x`, then it will be treated as `⟨a, ⟨b, c⟩⟩`, splitting the last
parameter as necessary.
`rcases` also has special support for quotient types: quotient induction into Prop works like
matching on the constructor `quot.mk`.
`rcases h : e with PAT` will do the same as `rcases e with PAT` with the exception that an
assumption `h : e = PAT` will be added to the context.
`rcases? e` will perform case splits on `e` in the same way as `rcases e`,
but rather than accepting a pattern, it does a maximal cases and prints the
pattern that would produce this case splitting. The default maximum depth is 5,
but this can be modified with `rcases? e : n`.
-/
meta def rcases : parse rcases_parse → tactic unit
| (rcases_args.rcases h p ids) := tactic.rcases h p ids
| (rcases_args.rcases_many ps ids) := tactic.rcases_many ps ids
| (rcases_args.hint p depth) := do
(pe, patt) ← match p with
| sum.inl p := prod.mk <$> pp p <*> rcases_hint p depth
| sum.inr ps := do
patts ← rcases_hint_many ps depth,
pes ← ps.mmap pp,
pure (format.bracket "⟨" "⟩" (format.comma_separated pes), rcases_patt.tuple patts)
end,
ppat ← pp patt,
trace $ ↑"Try this: rcases " ++ pe ++ " with " ++ ppat
add_tactic_doc
{ name := "rcases",
category := doc_category.tactic,
decl_names := [`tactic.interactive.rcases],
tags := ["induction"] }
/--
The `rintro` tactic is a combination of the `intros` tactic with `rcases` to
allow for destructuring patterns while introducing variables. See `rcases` for
a description of supported patterns. For example, `rintro (a | ⟨b, c⟩) ⟨d, e⟩`
will introduce two variables, and then do case splits on both of them producing
two subgoals, one with variables `a d e` and the other with `b c d e`.
`rintro`, unlike `rcases`, also supports the form `(x y : ty)` for introducing
and type-ascripting multiple variables at once, similar to binders.
`rintro?` will introduce and case split on variables in the same way as
`rintro`, but will also print the `rintro` invocation that would have the same
result. Like `rcases?`, `rintro? : n` allows for modifying the
depth of splitting; the default is 5.
`rintros` is an alias for `rintro`.
-/
meta def rintro : parse rintro_parse → tactic unit
| (sum.inl []) := intros []
| (sum.inl l) := tactic.rintro l
| (sum.inr depth) := do
ps ← tactic.rintro_hint depth,
fs ← ps.mmap (λ p, do
f ← pp $ p.format tt,
pure $ format.space ++ format.group f),
trace $ ↑"Try this: rintro" ++ format.join fs
/-- Alias for `rintro`. -/
meta def rintros := rintro
add_tactic_doc
{ name := "rintro",
category := doc_category.tactic,
decl_names := [`tactic.interactive.rintro, `tactic.interactive.rintros],
tags := ["induction"],
inherit_description_from := `tactic.interactive.rintro }
setup_tactic_parser
/-- Parses `patt? (: expr)? (:= expr)?`, the arguments for `obtain`.
(This is almost the same as `rcases_patt_parse`,
but it allows the pattern part to be empty.) -/
meta def obtain_parse :
parser ((option rcases_patt × option pexpr) × option (pexpr ⊕ list pexpr)) :=
with_desc "patt? (: expr)? (:= expr)?" $ do
(pat, tp) ←
(do pat ← rcases_patt_parse,
pure $ match pat with
| rcases_patt.typed pat tp := (some pat, some tp)
| _ := (some pat, none)
end) <|>
prod.mk none <$> (tk ":" >> texpr)?,
prod.mk (pat, tp) <$> (do
tk ":=",
(guard tp.is_none >>
sum.inr <$> brackets "⟨" "⟩" (sep_by (tk ",") (parser.pexpr 0))) <|>
(sum.inl <$> texpr))?
/--
The `obtain` tactic is a combination of `have` and `rcases`. See `rcases` for
a description of supported patterns.
```lean
obtain ⟨patt⟩ : type,
{ ... }
```
is equivalent to
```lean
have h : type,
{ ... },
rcases h with ⟨patt⟩
```
The syntax `obtain ⟨patt⟩ : type := proof` is also supported.
If `⟨patt⟩` is omitted, `rcases` will try to infer the pattern.
If `type` is omitted, `:= proof` is required.
-/
meta def obtain : parse obtain_parse → tactic unit
| ((pat, _), some (sum.inr val)) :=
tactic.rcases_many val (pat.get_or_else (default _))
| ((pat, none), some (sum.inl val)) :=
tactic.rcases none val (pat.get_or_else (default _))
| ((pat, some tp), some (sum.inl val)) :=
tactic.rcases none val $ (pat.get_or_else (default _)).typed tp
| ((pat, some tp), none) := do
nm ← mk_fresh_name,
e ← to_expr tp >>= assert nm,
(g :: gs) ← get_goals,
set_goals gs,
tactic.rcases none ``(%%e) (pat.get_or_else (rcases_patt.one `this)),
gs ← get_goals,
set_goals (g::gs)
| ((pat, none), none) :=
fail $ "`obtain` requires either an expected type or a value.\n" ++
"usage: `obtain ⟨patt⟩? : type (:= val)?` or `obtain ⟨patt⟩? (: type)? := val`"
add_tactic_doc
{ name := "obtain",
category := doc_category.tactic,
decl_names := [`tactic.interactive.obtain],
tags := ["induction"] }
end interactive
end tactic
|
cba802bf15671beb48111beadcf012debb8396f3 | c777c32c8e484e195053731103c5e52af26a25d1 | /src/probability/process/adapted.lean | 3cb162bc3b916647e6051c916484613ec33b304d | [
"Apache-2.0"
] | permissive | kbuzzard/mathlib | 2ff9e85dfe2a46f4b291927f983afec17e946eb8 | 58537299e922f9c77df76cb613910914a479c1f7 | refs/heads/master | 1,685,313,702,744 | 1,683,974,212,000 | 1,683,974,212,000 | 128,185,277 | 1 | 0 | null | 1,522,920,600,000 | 1,522,920,600,000 | null | UTF-8 | Lean | false | false | 8,921 | lean | /-
Copyright (c) 2021 Kexing Ying. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kexing Ying, Rémy Degenne
-/
import probability.process.filtration
import topology.instances.discrete
/-!
# Adapted and progressively measurable processes
This file defines some standard definition from the theory of stochastic processes including
filtrations and stopping times. These definitions are used to model the amount of information
at a specific time and are the first step in formalizing stochastic processes.
## Main definitions
* `measure_theory.adapted`: a sequence of functions `u` is said to be adapted to a
filtration `f` if at each point in time `i`, `u i` is `f i`-strongly measurable
* `measure_theory.prog_measurable`: a sequence of functions `u` is said to be progressively
measurable with respect to a filtration `f` if at each point in time `i`, `u` restricted to
`set.Iic i × Ω` is strongly measurable with respect to the product `measurable_space` structure
where the σ-algebra used for `Ω` is `f i`.
## Main results
* `adapted.prog_measurable_of_continuous`: a continuous adapted process is progressively measurable.
## Tags
adapted, progressively measurable
-/
open filter order topological_space
open_locale classical measure_theory nnreal ennreal topology big_operators
namespace measure_theory
variables {Ω β ι : Type*} {m : measurable_space Ω} [topological_space β] [preorder ι]
{u v : ι → Ω → β} {f : filtration ι m}
/-- A sequence of functions `u` is adapted to a filtration `f` if for all `i`,
`u i` is `f i`-measurable. -/
def adapted (f : filtration ι m) (u : ι → Ω → β) : Prop :=
∀ i : ι, strongly_measurable[f i] (u i)
namespace adapted
@[protected, to_additive] lemma mul [has_mul β] [has_continuous_mul β]
(hu : adapted f u) (hv : adapted f v) :
adapted f (u * v) :=
λ i, (hu i).mul (hv i)
@[protected, to_additive] lemma div [has_div β] [has_continuous_div β]
(hu : adapted f u) (hv : adapted f v) :
adapted f (u / v) :=
λ i, (hu i).div (hv i)
@[protected, to_additive] lemma inv [group β] [topological_group β] (hu : adapted f u) :
adapted f u⁻¹ :=
λ i, (hu i).inv
@[protected] lemma smul [has_smul ℝ β] [has_continuous_smul ℝ β] (c : ℝ) (hu : adapted f u) :
adapted f (c • u) :=
λ i, (hu i).const_smul c
@[protected] lemma strongly_measurable {i : ι} (hf : adapted f u) :
strongly_measurable[m] (u i) :=
(hf i).mono (f.le i)
lemma strongly_measurable_le {i j : ι} (hf : adapted f u) (hij : i ≤ j) :
strongly_measurable[f j] (u i) :=
(hf i).mono (f.mono hij)
end adapted
lemma adapted_const (f : filtration ι m) (x : β) : adapted f (λ _ _, x) :=
λ i, strongly_measurable_const
variable (β)
lemma adapted_zero [has_zero β] (f : filtration ι m) : adapted f (0 : ι → Ω → β) :=
λ i, @strongly_measurable_zero Ω β (f i) _ _
variable {β}
lemma filtration.adapted_natural [metrizable_space β] [mβ : measurable_space β] [borel_space β]
{u : ι → Ω → β} (hum : ∀ i, strongly_measurable[m] (u i)) :
adapted (filtration.natural u hum) u :=
begin
assume i,
refine strongly_measurable.mono _ (le_supr₂_of_le i (le_refl i) le_rfl),
rw strongly_measurable_iff_measurable_separable,
exact ⟨measurable_iff_comap_le.2 le_rfl, (hum i).is_separable_range⟩
end
/-- Progressively measurable process. A sequence of functions `u` is said to be progressively
measurable with respect to a filtration `f` if at each point in time `i`, `u` restricted to
`set.Iic i × Ω` is measurable with respect to the product `measurable_space` structure where the
σ-algebra used for `Ω` is `f i`.
The usual definition uses the interval `[0,i]`, which we replace by `set.Iic i`. We recover the
usual definition for index types `ℝ≥0` or `ℕ`. -/
def prog_measurable [measurable_space ι] (f : filtration ι m) (u : ι → Ω → β) : Prop :=
∀ i, strongly_measurable[subtype.measurable_space.prod (f i)] (λ p : set.Iic i × Ω, u p.1 p.2)
lemma prog_measurable_const [measurable_space ι] (f : filtration ι m) (b : β) :
prog_measurable f ((λ _ _, b) : ι → Ω → β) :=
λ i, @strongly_measurable_const _ _ (subtype.measurable_space.prod (f i)) _ _
namespace prog_measurable
variables [measurable_space ι]
protected lemma adapted (h : prog_measurable f u) : adapted f u :=
begin
intro i,
have : u i = (λ p : set.Iic i × Ω, u p.1 p.2) ∘ (λ x, (⟨i, set.mem_Iic.mpr le_rfl⟩, x)) := rfl,
rw this,
exact (h i).comp_measurable measurable_prod_mk_left,
end
protected lemma comp {t : ι → Ω → ι} [topological_space ι] [borel_space ι] [metrizable_space ι]
(h : prog_measurable f u) (ht : prog_measurable f t)
(ht_le : ∀ i ω, t i ω ≤ i) :
prog_measurable f (λ i ω, u (t i ω) ω) :=
begin
intro i,
have : (λ p : ↥(set.Iic i) × Ω, u (t (p.fst : ι) p.snd) p.snd)
= (λ p : ↥(set.Iic i) × Ω, u (p.fst : ι) p.snd) ∘ (λ p : ↥(set.Iic i) × Ω,
(⟨t (p.fst : ι) p.snd, set.mem_Iic.mpr ((ht_le _ _).trans p.fst.prop)⟩, p.snd)) := rfl,
rw this,
exact (h i).comp_measurable ((ht i).measurable.subtype_mk.prod_mk measurable_snd),
end
section arithmetic
@[to_additive] protected lemma mul [has_mul β] [has_continuous_mul β]
(hu : prog_measurable f u) (hv : prog_measurable f v) :
prog_measurable f (λ i ω, u i ω * v i ω) :=
λ i, (hu i).mul (hv i)
@[to_additive] protected lemma finset_prod' {γ} [comm_monoid β] [has_continuous_mul β]
{U : γ → ι → Ω → β} {s : finset γ} (h : ∀ c ∈ s, prog_measurable f (U c)) :
prog_measurable f (∏ c in s, U c) :=
finset.prod_induction U (prog_measurable f) (λ _ _, prog_measurable.mul)
(prog_measurable_const _ 1) h
@[to_additive] protected lemma finset_prod {γ} [comm_monoid β] [has_continuous_mul β]
{U : γ → ι → Ω → β} {s : finset γ} (h : ∀ c ∈ s, prog_measurable f (U c)) :
prog_measurable f (λ i a, ∏ c in s, U c i a) :=
by { convert prog_measurable.finset_prod' h, ext i a, simp only [finset.prod_apply], }
@[to_additive] protected lemma inv [group β] [topological_group β] (hu : prog_measurable f u) :
prog_measurable f (λ i ω, (u i ω)⁻¹) :=
λ i, (hu i).inv
@[to_additive] protected lemma div [group β] [topological_group β]
(hu : prog_measurable f u) (hv : prog_measurable f v) :
prog_measurable f (λ i ω, u i ω / v i ω) :=
λ i, (hu i).div (hv i)
end arithmetic
end prog_measurable
lemma prog_measurable_of_tendsto' {γ} [measurable_space ι] [pseudo_metrizable_space β]
(fltr : filter γ) [fltr.ne_bot] [fltr.is_countably_generated] {U : γ → ι → Ω → β}
(h : ∀ l, prog_measurable f (U l)) (h_tendsto : tendsto U fltr (𝓝 u)) :
prog_measurable f u :=
begin
assume i,
apply @strongly_measurable_of_tendsto (set.Iic i × Ω) β γ (measurable_space.prod _ (f i))
_ _ fltr _ _ _ _ (λ l, h l i),
rw tendsto_pi_nhds at h_tendsto ⊢,
intro x,
specialize h_tendsto x.fst,
rw tendsto_nhds at h_tendsto ⊢,
exact λ s hs h_mem, h_tendsto {g | g x.snd ∈ s} (hs.preimage (continuous_apply x.snd)) h_mem,
end
lemma prog_measurable_of_tendsto [measurable_space ι] [pseudo_metrizable_space β]
{U : ℕ → ι → Ω → β}
(h : ∀ l, prog_measurable f (U l)) (h_tendsto : tendsto U at_top (𝓝 u)) :
prog_measurable f u :=
prog_measurable_of_tendsto' at_top h h_tendsto
/-- A continuous and adapted process is progressively measurable. -/
theorem adapted.prog_measurable_of_continuous
[topological_space ι] [metrizable_space ι] [second_countable_topology ι]
[measurable_space ι] [opens_measurable_space ι]
[pseudo_metrizable_space β]
(h : adapted f u) (hu_cont : ∀ ω, continuous (λ i, u i ω)) :
prog_measurable f u :=
λ i, @strongly_measurable_uncurry_of_continuous_of_strongly_measurable _ _ (set.Iic i) _ _ _ _ _ _ _
(f i) _ (λ ω, (hu_cont ω).comp continuous_induced_dom) (λ j, (h j).mono (f.mono j.prop))
/-- For filtrations indexed by a discrete order, `adapted` and `prog_measurable` are equivalent.
This lemma provides `adapted f u → prog_measurable f u`.
See `prog_measurable.adapted` for the reverse direction, which is true more generally. -/
lemma adapted.prog_measurable_of_discrete [topological_space ι] [discrete_topology ι]
[second_countable_topology ι] [measurable_space ι] [opens_measurable_space ι]
[pseudo_metrizable_space β]
(h : adapted f u) :
prog_measurable f u :=
h.prog_measurable_of_continuous (λ _, continuous_of_discrete_topology)
-- this dot notation will make more sense once we have a more general definition for predictable
lemma predictable.adapted {f : filtration ℕ m} {u : ℕ → Ω → β}
(hu : adapted f (λ n, u (n + 1))) (hu0 : strongly_measurable[f 0] (u 0)) :
adapted f u :=
λ n, match n with
| 0 := hu0
| n + 1 := (hu n).mono (f.mono n.le_succ)
end
end measure_theory
|
aebef5c95861e4107d9fa99dc0da8014fded30a1 | b29f946a2f0afd23ef86b9219116968babbb9f4f | /src/lean_format_tests/monotone_formatted.lean | 2ece232655870c68062eb18083cf4b124c6f5ed8 | [
"Apache-2.0"
] | permissive | ImperialCollegeLondon/M1P1-lean | 58be7394fded719d95e45e6b10e1ecf2ed3c7c4c | 3723468cc50f8bebd00a9811caf25224a578de17 | refs/heads/master | 1,587,063,867,779 | 1,572,727,164,000 | 1,572,727,164,000 | 165,845,802 | 14 | 4 | Apache-2.0 | 1,549,730,698,000 | 1,547,554,675,000 | Lean | UTF-8 | Lean | false | false | 3,301 | lean | -- begin header
import tactic.linarith
import data.real.basic
noncomputable theory
local attribute [instance, priority 0] classical.prop_decidable
local notation `|` x `|` := abs x
-- end header
/- Section
Sequences, subsequences, and monotonic subsequences.
-/
/- Sub-section
Definitions
-/
/-
We model a sequence $a₀, a₁, a₂, \dots$ of real numbers as a function
from $ℕ := \{0,1,2,\dots\}$ to $ℝ$, sending $n$ to $a_n$. We sometimes write such
a sequence as $(a_n)_{n≥0}$ or just $(a_n)$.
-/
/- Definition
A sequence $(a_n)$ is *increasing* if forall $n∈ℕ$ we have $a_n≤a_{n+1}$. Note that equality
is allowed here -- so for example the sequence $3, 3, 3, 3, …$ is increasing. A sequence is
*strictly increasing* if for all $n∈ℕ$ we have $a_n<a_{n+1}$; constant sequences
are not strictly increasing.
-/
def is_increasing {X : Type*} [has_le X] (a : ℕ → X) : Prop :=
∀ n : ℕ, a n ≤ a (n + 1)
def is_strictly_increasing {X : Type*} [has_lt X] (a : ℕ → X) : Prop :=
∀ n : ℕ, a n < a (n + 1)
/- Definition
Similarly a sequence $(a_n)$ is *decreasing* if $∀ n∈ℕ, a_{n+1}≤ a_n$ and is
*strictly decreasing* if $∀ n∈ℕ, a_{n+1}<a_n$.
-/
def is_decreasing {X : Type*} [has_le X] (a : ℕ → X) : Prop :=
∀ n : ℕ, a (n + 1) ≤ a n
def is_strictly_decreasing {X : Type*} [has_lt X] (a : ℕ → X) : Prop :=
∀ n : ℕ, a (n + 1) < a n
/-
A *subsequence* of a sequence $(a_n)$ is a sequence $(b_m)$ of the form
$a_{k_0}$, $a_{k_1}$, $a_{k_2}$, where the indices $0≤ k_0<k_1<k_2<…$ are strictly increasing.
-/
def is_subsequence {X : Type*} (a : ℕ → X) (b : ℕ → X) :=
∃ k : ℕ → ℕ, is_strictly_increasing k ∧ ∀ n, a (k n) = b n
/-
We are going to show that any sequence contains either a strictly increasing subsequence,
or a decreasing sequence [Does this have a name?]. The strategy of the proof is to
consider so-called peaks of the sequence. A *peak* of a sequence $(a_n)$ is an index
$i∈ℕ$ such that $a_i$ is at least as large as subsequent members of the sequence.
-/
/- Definition
A *peak* of a sequence $(a_n)$ is $i∈ℕ$ such that $∀ j≥ i, $a_j≤ a_i$.
-/
def is_peak (a : ℕ → ℝ) (i : ℕ) : Prop :=
∀ j, i ≤ j → a j ≤ a i
/-
For any sequence, consider the set of peaks. It is clearly either finite or infinite.
Let us formally state this in a more convenient form.
-/
/- Lemma
If $(a_n)$ is a sequence, then either there exists $N∈ℕ$ such that all peak $i$
of $(a_n)$ satisfy $i≤N$, or for all $N∈ℕ$ there exists a peak $i$ of $(a_n)$ with $i>N$.
-/
lemma finite_or_infinite_peaks (a : ℕ → ℝ) :
(∃ N, ∀ i, is_peak a i → i ≤ N) ∨ (∀ N, ∃ i, is_peak a i ∧ i > N) :=
by simpa [not_forall] using classical.em (∃ N, ∀ i, is_peak a i → i ≤ N)
/- Lemma
If there are infinitely many peaks for a sequence $(a_n)$, then the peaks
form a decreasing subsequence.
-/
lemma decreasing_subsequence_of_infinite_peaks (a : ℕ → ℝ)
(h : ∀ N, ∃ i, is_peak a i ∧ i > N) :
∃ b : ℕ → ℝ, is_subsequence a b ∧ is_strictly_decreasing b :=
begin
choose f hf using h,
let k : ℕ → ℕ := λ n, nat.rec_on n (f 0) (λ n kn, f kn),
use (λ n, a (k n)),
split,
{ use k,
split,swap,intro,refl,
sorry },
{
sorry },
end
|
faf2e392f1aa3ec287326aae8d994f8c22e757f9 | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/control/basic.lean | ebb5951a0403439966450e7dd5c7c30dbda12e36 | [
"Apache-2.0"
] | permissive | jjgarzella/mathlib | 96a345378c4e0bf26cf604aed84f90329e4896a2 | 395d8716c3ad03747059d482090e2bb97db612c8 | refs/heads/master | 1,686,480,124,379 | 1,625,163,323,000 | 1,625,163,323,000 | 281,190,421 | 2 | 0 | Apache-2.0 | 1,595,268,170,000 | 1,595,268,169,000 | null | UTF-8 | Lean | false | false | 6,745 | 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
Extends the theory on functors, applicatives and monads.
-/
universes u v w
variables {α β γ : Type u}
notation a ` $< `:1 f:1 := f a
section functor
variables {f : Type u → Type v} [functor f] [is_lawful_functor f]
run_cmd mk_simp_attr `functor_norm
run_cmd tactic.add_doc_string `simp_attr.functor_norm "Simp set for functor_norm"
@[functor_norm] theorem functor.map_map (m : α → β) (g : β → γ) (x : f α) :
g <$> (m <$> x) = (g ∘ m) <$> x :=
(comp_map _ _ _).symm
@[simp] theorem id_map' (x : f α) : (λa, a) <$> x = x := id_map _
end functor
section applicative
variables {F : Type u → Type v} [applicative F]
def mzip_with
{α₁ α₂ φ : Type u}
(f : α₁ → α₂ → F φ) :
Π (ma₁ : list α₁) (ma₂: list α₂), F (list φ)
| (x :: xs) (y :: ys) := (::) <$> f x y <*> mzip_with xs ys
| _ _ := pure []
def mzip_with' (f : α → β → F γ) : list α → list β → F punit
| (x :: xs) (y :: ys) := f x y *> mzip_with' xs ys
| [] _ := pure punit.star
| _ [] := pure punit.star
variables [is_lawful_applicative F]
attribute [functor_norm] seq_assoc pure_seq_eq_map
@[simp] theorem pure_id'_seq (x : F α) : pure (λx, x) <*> x = x :=
pure_id_seq x
attribute [functor_norm] seq_assoc pure_seq_eq_map
@[functor_norm] theorem seq_map_assoc (x : F (α → β)) (f : γ → α) (y : F γ) :
(x <*> (f <$> y)) = (λ(m:α→β), m ∘ f) <$> x <*> y :=
begin
simp [(pure_seq_eq_map _ _).symm],
simp [seq_assoc, (comp_map _ _ _).symm, (∘)],
simp [pure_seq_eq_map]
end
@[functor_norm] theorem map_seq (f : β → γ) (x : F (α → β)) (y : F α) :
(f <$> (x <*> y)) = ((∘) f) <$> x <*> y :=
by simp [(pure_seq_eq_map _ _).symm]; simp [seq_assoc]
end applicative
-- TODO: setup `functor_norm` for `monad` laws
attribute [functor_norm] pure_bind bind_assoc bind_pure
section monad
variables {m : Type u → Type v} [monad m] [is_lawful_monad m]
open list
def list.mpartition {f : Type → Type} [monad f] {α : Type} (p : α → f bool) :
list α → f (list α × list α)
| [] := pure ([],[])
| (x :: xs) :=
mcond (p x) (prod.map (cons x) id <$> list.mpartition xs)
(prod.map id (cons x) <$> list.mpartition xs)
lemma map_bind (x : m α) {g : α → m β} {f : β → γ} : f <$> (x >>= g) = (x >>= λa, f <$> g a) :=
by rw [← bind_pure_comp_eq_map,bind_assoc]; simp [bind_pure_comp_eq_map]
lemma seq_bind_eq (x : m α) {g : β → m γ} {f : α → β} : (f <$> x) >>= g = (x >>= g ∘ f) :=
show bind (f <$> x) g = bind x (g ∘ f),
by rw [← bind_pure_comp_eq_map, bind_assoc]; simp [pure_bind]
lemma seq_eq_bind_map {x : m α} {f : m (α → β)} : f <*> x = (f >>= (<$> x)) :=
(bind_map_eq_seq f x).symm
/-- This is the Kleisli composition -/
@[reducible] def fish {m} [monad m] {α β γ} (f : α → m β) (g : β → m γ) := λ x, f x >>= g
-- >=> is already defined in the core library but it is unusable
-- because of its precedence (it is defined with precedence 2) and
-- because it is defined as a lambda instead of having a named
-- function
infix ` >=> `:55 := fish
@[functor_norm]
lemma fish_pure {α β} (f : α → m β) : f >=> pure = f :=
by simp only [(>=>)] with functor_norm
@[functor_norm]
lemma fish_pipe {α β} (f : α → m β) : pure >=> f = f :=
by simp only [(>=>)] with functor_norm
@[functor_norm]
lemma fish_assoc {α β γ φ} (f : α → m β) (g : β → m γ) (h : γ → m φ) :
(f >=> g) >=> h = f >=> (g >=> h) :=
by simp only [(>=>)] with functor_norm
variables {β' γ' : Type v}
variables {m' : Type v → Type w} [monad m']
def list.mmap_accumr (f : α → β' → m' (β' × γ')) : β' → list α → m' (β' × list γ')
| a [] := pure (a,[])
| a (x :: xs) :=
do (a',ys) ← list.mmap_accumr a xs,
(a'',y) ← f x a',
pure (a'',y::ys)
def list.mmap_accuml (f : β' → α → m' (β' × γ')) : β' → list α → m' (β' × list γ')
| a [] := pure (a,[])
| a (x :: xs) :=
do (a',y) ← f a x,
(a'',ys) ← list.mmap_accuml a' xs,
pure (a'',y :: ys)
end monad
section
variables {m : Type u → Type u} [monad m] [is_lawful_monad m]
lemma mjoin_map_map {α β : Type u} (f : α → β) (a : m (m α)) :
mjoin (functor.map f <$> a) = f <$> (mjoin a) :=
by simp only [mjoin, (∘), id.def,
(bind_pure_comp_eq_map _ _).symm, bind_assoc, map_bind, pure_bind]
lemma mjoin_map_mjoin {α : Type u} (a : m (m (m α))) :
mjoin (mjoin <$> a) = mjoin (mjoin a) :=
by simp only [mjoin, (∘), id.def,
map_bind, (bind_pure_comp_eq_map _ _).symm, bind_assoc, pure_bind]
@[simp] lemma mjoin_map_pure {α : Type u} (a : m α) :
mjoin (pure <$> a) = a :=
by simp only [mjoin, (∘), id.def,
map_bind, (bind_pure_comp_eq_map _ _).symm, bind_assoc, pure_bind, bind_pure]
@[simp] lemma mjoin_pure {α : Type u} (a : m α) : mjoin (pure a) = a :=
is_lawful_monad.pure_bind a id
end
section alternative
variables {F : Type → Type v} [alternative F]
def succeeds {α} (x : F α) : F bool := (x $> tt) <|> pure ff
def mtry {α} (x : F α) : F unit := (x $> ()) <|> pure ()
@[simp] theorem guard_true {h : decidable true} :
@guard F _ true h = pure () := by simp [guard]
@[simp] theorem guard_false {h : decidable false} :
@guard F _ false h = failure := by simp [guard]
end alternative
namespace sum
variables {e : Type v}
protected def bind {α β} : e ⊕ α → (α → e ⊕ β) → e ⊕ β
| (inl x) _ := inl x
| (inr x) f := f x
instance : monad (sum.{v u} e) :=
{ pure := @sum.inr e,
bind := @sum.bind e }
instance : is_lawful_functor (sum.{v u} e) :=
by refine { .. }; intros; casesm _ ⊕ _; refl
instance : is_lawful_monad (sum.{v u} e) :=
{ bind_assoc := by { intros, casesm _ ⊕ _; refl },
pure_bind := by { intros, refl },
bind_pure_comp_eq_map := by { intros, casesm _ ⊕ _; refl },
bind_map_eq_seq := by { intros, cases f; refl } }
end sum
class is_comm_applicative (m : Type* → Type*) [applicative m] extends is_lawful_applicative m :
Prop :=
(commutative_prod : ∀{α β} (a : m α) (b : m β), prod.mk <$> a <*> b = (λb a, (a, b)) <$> b <*> a)
open functor
lemma is_comm_applicative.commutative_map
{m : Type* → Type*} [applicative m] [is_comm_applicative m]
{α β γ} (a : m α) (b : m β) {f : α → β → γ} :
f <$> a <*> b = flip f <$> b <*> a :=
calc f <$> a <*> b = (λp:α×β, f p.1 p.2) <$> (prod.mk <$> a <*> b) :
by simp [seq_map_assoc, map_seq, seq_assoc, seq_pure, map_map]
... = (λb a, f a b) <$> b <*> a :
by rw [is_comm_applicative.commutative_prod];
simp [seq_map_assoc, map_seq, seq_assoc, seq_pure, map_map]
|
076b571bd2549725b6b44a757d0ba336f5f24c71 | 4fa161becb8ce7378a709f5992a594764699e268 | /src/algebra/group/basic.lean | b39d507d474d71ccedcfb8653194a7ed7e1cd93a | [
"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 | 12,557 | lean | /-
Copyright (c) 2014 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad, Leonardo de Moura, Simon Hudon, Mario Carneiro
-/
import algebra.group.to_additive
import algebra.group.defs
import tactic.simpa
import logic.function.basic
import tactic.protected
universe u
section comm_semigroup
variables {G : Type u} [comm_semigroup G]
@[no_rsimp, to_additive]
lemma mul_left_comm : ∀ a b c : G, a * (b * c) = b * (a * c) :=
left_comm has_mul.mul mul_comm mul_assoc
attribute [no_rsimp] add_left_comm
@[to_additive]
lemma mul_right_comm : ∀ a b c : G, a * b * c = a * c * b :=
right_comm has_mul.mul mul_comm mul_assoc
@[to_additive]
theorem mul_mul_mul_comm (a b c d : G) : (a * b) * (c * d) = (a * c) * (b * d) :=
by simp only [mul_left_comm, mul_assoc]
end comm_semigroup
local attribute [simp] mul_assoc sub_eq_add_neg
section add_monoid
variables {M : Type u} [add_monoid M] {a b c : M}
@[simp] lemma bit0_zero : bit0 (0 : M) = 0 := add_zero _
@[simp] lemma bit1_zero [has_one M] : bit1 (0 : M) = 1 :=
by rw [bit1, bit0_zero, zero_add]
end add_monoid
section comm_monoid
variables {M : Type u} [comm_monoid M] {x y z : M}
@[to_additive] lemma inv_unique (hy : x * y = 1) (hz : x * z = 1) : y = z :=
left_inv_eq_right_inv (trans (mul_comm _ _) hy) hz
end comm_monoid
section group
variables {G : Type u} [group G] {a b c : G}
@[simp, to_additive]
lemma inv_mul_cancel_right (a b : G) : a * b⁻¹ * b = a :=
by simp [mul_assoc]
@[simp, to_additive neg_zero]
lemma one_inv : 1⁻¹ = (1 : G) :=
inv_eq_of_mul_eq_one (one_mul 1)
@[to_additive]
theorem left_inverse_inv (G) [group G] :
function.left_inverse (λ a : G, a⁻¹) (λ a, a⁻¹) :=
inv_inv
@[to_additive]
lemma inv_inj (h : a⁻¹ = b⁻¹) : a = b :=
have a = a⁻¹⁻¹, by simp,
begin rw this, simp [h] end
@[simp, to_additive]
theorem inv_inj' : a⁻¹ = b⁻¹ ↔ a = b :=
⟨λ h, by rw [← inv_inv a, h, inv_inv], congr_arg _⟩
@[to_additive]
theorem eq_of_inv_eq_inv : a⁻¹ = b⁻¹ → a = b :=
inv_inj'.1
@[simp, to_additive]
lemma mul_inv_cancel_left (a b : G) : a * (a⁻¹ * b) = b :=
by rw [← mul_assoc, mul_right_inv, one_mul]
@[to_additive]
theorem mul_left_surjective (a : G) : function.surjective ((*) a) :=
λ x, ⟨a⁻¹ * x, mul_inv_cancel_left a x⟩
@[to_additive]
theorem mul_right_surjective (a : G) : function.surjective (λ x, x * a) :=
λ x, ⟨x * a⁻¹, inv_mul_cancel_right x a⟩
@[simp, to_additive neg_add_rev]
lemma mul_inv_rev (a b : G) : (a * b)⁻¹ = b⁻¹ * a⁻¹ :=
inv_eq_of_mul_eq_one $ by simp
@[to_additive]
lemma eq_inv_of_eq_inv (h : a = b⁻¹) : b = a⁻¹ :=
by simp [h]
@[to_additive]
lemma eq_inv_of_mul_eq_one (h : a * b = 1) : a = b⁻¹ :=
have a⁻¹ = b, from inv_eq_of_mul_eq_one h,
by simp [this.symm]
@[to_additive]
lemma eq_mul_inv_of_mul_eq (h : a * c = b) : a = b * c⁻¹ :=
by simp [h.symm]
@[to_additive]
lemma eq_inv_mul_of_mul_eq (h : b * a = c) : a = b⁻¹ * c :=
by simp [h.symm]
@[to_additive]
lemma inv_mul_eq_of_eq_mul (h : b = a * c) : a⁻¹ * b = c :=
by simp [h]
@[to_additive]
lemma mul_inv_eq_of_eq_mul (h : a = c * b) : a * b⁻¹ = c :=
by simp [h]
@[to_additive]
lemma eq_mul_of_mul_inv_eq (h : a * c⁻¹ = b) : a = b * c :=
by simp [h.symm]
@[to_additive]
lemma eq_mul_of_inv_mul_eq (h : b⁻¹ * a = c) : a = b * c :=
by simp [h.symm, mul_inv_cancel_left]
@[to_additive]
lemma mul_eq_of_eq_inv_mul (h : b = a⁻¹ * c) : a * b = c :=
by rw [h, mul_inv_cancel_left]
@[to_additive]
lemma mul_eq_of_eq_mul_inv (h : a = c * b⁻¹) : a * b = c :=
by simp [h]
@[to_additive]
theorem mul_self_iff_eq_one : a * a = a ↔ a = 1 :=
by have := @mul_right_inj _ _ a a 1; rwa mul_one at this
@[simp, to_additive]
theorem inv_eq_one : a⁻¹ = 1 ↔ a = 1 :=
by rw [← @inv_inj' _ _ a 1, one_inv]
@[to_additive]
theorem inv_ne_one : a⁻¹ ≠ 1 ↔ a ≠ 1 :=
not_congr inv_eq_one
@[to_additive]
theorem eq_inv_iff_eq_inv : a = b⁻¹ ↔ b = a⁻¹ :=
⟨eq_inv_of_eq_inv, eq_inv_of_eq_inv⟩
@[to_additive]
theorem inv_eq_iff_inv_eq : a⁻¹ = b ↔ b⁻¹ = a :=
eq_comm.trans $ eq_inv_iff_eq_inv.trans eq_comm
@[to_additive]
theorem mul_eq_one_iff_eq_inv : a * b = 1 ↔ a = b⁻¹ :=
by simpa [mul_left_inv, -mul_left_inj] using @mul_left_inj _ _ b a (b⁻¹)
@[to_additive]
theorem mul_eq_one_iff_inv_eq : a * b = 1 ↔ a⁻¹ = b :=
by rw [mul_eq_one_iff_eq_inv, eq_inv_iff_eq_inv, eq_comm]
@[to_additive]
theorem eq_inv_iff_mul_eq_one : a = b⁻¹ ↔ a * b = 1 :=
mul_eq_one_iff_eq_inv.symm
@[to_additive]
theorem inv_eq_iff_mul_eq_one : a⁻¹ = b ↔ a * b = 1 :=
mul_eq_one_iff_inv_eq.symm
@[to_additive]
theorem eq_mul_inv_iff_mul_eq : a = b * c⁻¹ ↔ a * c = b :=
⟨λ h, by rw [h, inv_mul_cancel_right], λ h, by rw [← h, mul_inv_cancel_right]⟩
@[to_additive]
theorem eq_inv_mul_iff_mul_eq : a = b⁻¹ * c ↔ b * a = c :=
⟨λ h, by rw [h, mul_inv_cancel_left], λ h, by rw [← h, inv_mul_cancel_left]⟩
@[to_additive]
theorem inv_mul_eq_iff_eq_mul : a⁻¹ * b = c ↔ b = a * c :=
⟨λ h, by rw [← h, mul_inv_cancel_left], λ h, by rw [h, inv_mul_cancel_left]⟩
@[to_additive]
theorem mul_inv_eq_iff_eq_mul : a * b⁻¹ = c ↔ a = c * b :=
⟨λ h, by rw [← h, inv_mul_cancel_right], λ h, by rw [h, mul_inv_cancel_right]⟩
@[to_additive]
theorem mul_inv_eq_one : a * b⁻¹ = 1 ↔ a = b :=
by rw [mul_eq_one_iff_eq_inv, inv_inv]
@[to_additive]
theorem inv_mul_eq_one : a⁻¹ * b = 1 ↔ a = b :=
by rw [mul_eq_one_iff_eq_inv, inv_inj']
@[simp, to_additive]
lemma mul_left_eq_self : a * b = b ↔ a = 1 :=
⟨λ h, @mul_right_cancel _ _ a b 1 (by simp [h]), λ h, by simp [h]⟩
@[simp, to_additive]
lemma mul_right_eq_self : a * b = a ↔ b = 1 :=
⟨λ h, @mul_left_cancel _ _ a b 1 (by simp [h]), λ h, by simp [h]⟩
@[to_additive]
lemma inv_involutive : function.involutive (has_inv.inv : G → G) := inv_inv
end group
section add_group
variables {G : Type u} [add_group G] {a b c d : G}
@[simp] lemma sub_self (a : G) : a - a = 0 :=
add_right_neg a
@[simp] lemma sub_add_cancel (a b : G) : a - b + b = a :=
neg_add_cancel_right a b
@[simp] lemma add_sub_cancel (a b : G) : a + b - b = a :=
add_neg_cancel_right a b
lemma add_sub_assoc (a b c : G) : a + b - c = a + (b - c) :=
by rw [sub_eq_add_neg, add_assoc, ←sub_eq_add_neg]
lemma eq_of_sub_eq_zero (h : a - b = 0) : a = b :=
have 0 + b = b, by rw zero_add,
have (a - b) + b = b, by rwa h,
by rwa [sub_eq_add_neg, neg_add_cancel_right] at this
lemma sub_eq_zero_of_eq (h : a = b) : a - b = 0 :=
by rw [h, sub_self]
lemma sub_eq_zero_iff_eq : a - b = 0 ↔ a = b :=
⟨eq_of_sub_eq_zero, sub_eq_zero_of_eq⟩
@[simp] lemma zero_sub (a : G) : 0 - a = -a :=
zero_add (-a)
@[simp] lemma sub_zero (a : G) : a - 0 = a :=
by rw [sub_eq_add_neg, neg_zero, add_zero]
lemma sub_ne_zero_of_ne (h : a ≠ b) : a - b ≠ 0 :=
begin
intro hab,
apply h,
apply eq_of_sub_eq_zero hab
end
@[simp] lemma sub_neg_eq_add (a b : G) : a - (-b) = a + b :=
by rw [sub_eq_add_neg, neg_neg]
@[simp] lemma neg_sub (a b : G) : -(a - b) = b - a :=
neg_eq_of_add_eq_zero (by rw [sub_eq_add_neg, sub_eq_add_neg, add_assoc, neg_add_cancel_left,
add_right_neg])
local attribute [simp] add_assoc
lemma add_sub (a b c : G) : a + (b - c) = a + b - c :=
by simp
lemma sub_add_eq_sub_sub_swap (a b c : G) : a - (b + c) = a - c - b :=
by simp
@[simp] lemma add_sub_add_right_eq_sub (a b c : G) : (a + c) - (b + c) = a - b :=
by rw [sub_add_eq_sub_sub_swap]; simp
lemma eq_sub_of_add_eq (h : a + c = b) : a = b - c :=
by simp [h.symm]
lemma sub_eq_of_eq_add (h : a = c + b) : a - b = c :=
by simp [h]
lemma eq_add_of_sub_eq (h : a - c = b) : a = b + c :=
by simp [h.symm]
lemma add_eq_of_eq_sub (h : a = c - b) : a + b = c :=
by simp [h]
@[simp] lemma sub_right_inj : a - b = a - c ↔ b = c :=
(add_right_inj _).trans neg_inj'
@[simp] lemma sub_left_inj : b - a = c - a ↔ b = c :=
add_left_inj _
lemma sub_add_sub_cancel (a b c : G) : (a - b) + (b - c) = a - c :=
by rw [← add_sub_assoc, sub_add_cancel]
lemma sub_sub_sub_cancel_right (a b c : G) : (a - c) - (b - c) = a - b :=
by rw [← neg_sub c b, sub_neg_eq_add, sub_add_sub_cancel]
theorem sub_sub_assoc_swap : a - (b - c) = a + c - b :=
by simp
theorem sub_eq_zero : a - b = 0 ↔ a = b :=
⟨eq_of_sub_eq_zero, λ h, by rw [h, sub_self]⟩
theorem sub_ne_zero : a - b ≠ 0 ↔ a ≠ b :=
not_congr sub_eq_zero
theorem eq_sub_iff_add_eq : a = b - c ↔ a + c = b :=
eq_add_neg_iff_add_eq
theorem sub_eq_iff_eq_add : a - b = c ↔ a = c + b :=
add_neg_eq_iff_eq_add
theorem eq_iff_eq_of_sub_eq_sub (H : a - b = c - d) : a = b ↔ c = d :=
by rw [← sub_eq_zero, H, sub_eq_zero]
theorem left_inverse_sub_add_left (c : G) : function.left_inverse (λ x, x - c) (λ x, x + c) :=
assume x, add_sub_cancel x c
theorem left_inverse_add_left_sub (c : G) : function.left_inverse (λ x, x + c) (λ x, x - c) :=
assume x, sub_add_cancel x c
theorem left_inverse_add_right_neg_add (c : G) :
function.left_inverse (λ x, c + x) (λ x, - c + x) :=
assume x, add_neg_cancel_left c x
theorem left_inverse_neg_add_add_right (c : G) :
function.left_inverse (λ x, - c + x) (λ x, c + x) :=
assume x, neg_add_cancel_left c x
end add_group
section comm_group
variables {G : Type u} [comm_group G]
@[to_additive neg_add]
lemma mul_inv (a b : G) : (a * b)⁻¹ = a⁻¹ * b⁻¹ :=
by rw [mul_inv_rev, mul_comm]
end comm_group
section add_comm_group
variables {G : Type u} [add_comm_group G] {a b c d : G}
local attribute [simp] add_assoc add_comm add_left_comm sub_eq_add_neg
lemma sub_add_eq_sub_sub (a b c : G) : a - (b + c) = a - b - c :=
by simp
lemma neg_add_eq_sub (a b : G) : -a + b = b - a :=
by simp
lemma sub_add_eq_add_sub (a b c : G) : a - b + c = a + c - b :=
by simp
lemma sub_sub (a b c : G) : a - b - c = a - (b + c) :=
by simp
lemma sub_add (a b c : G) : a - b + c = a - (b - c) :=
by simp
@[simp] lemma add_sub_add_left_eq_sub (a b c : G) : (c + a) - (c + b) = a - b :=
by simp
lemma eq_sub_of_add_eq' (h : c + a = b) : a = b - c :=
by simp [h.symm]
lemma sub_eq_of_eq_add' (h : a = b + c) : a - b = c :=
begin simp [h], rw [add_left_comm], simp end
lemma eq_add_of_sub_eq' (h : a - b = c) : a = b + c :=
by simp [h.symm]
lemma add_eq_of_eq_sub' (h : b = c - a) : a + b = c :=
begin simp [h], rw [add_comm c, add_neg_cancel_left] end
lemma sub_sub_self (a b : G) : a - (a - b) = b :=
begin simp, rw [add_comm b, add_neg_cancel_left] end
lemma add_sub_comm (a b c d : G) : a + b - (c + d) = (a - c) + (b - d) :=
by simp
lemma sub_eq_sub_add_sub (a b c : G) : a - b = c - b + (a - c) :=
begin simp, rw [add_left_comm c], simp end
lemma neg_neg_sub_neg (a b : G) : - (-a - -b) = a - b :=
by simp
lemma sub_sub_cancel (a b : G) : a - (a - b) = b := sub_sub_self a b
lemma sub_eq_neg_add (a b : G) : a - b = -b + a :=
add_comm _ _
theorem neg_add' (a b : G) : -(a + b) = -a - b := neg_add a b
@[simp]
lemma neg_sub_neg (a b : G) : -a - -b = b - a :=
by simp [sub_eq_neg_add, add_comm]
lemma eq_sub_iff_add_eq' : a = b - c ↔ c + a = b :=
by rw [eq_sub_iff_add_eq, add_comm]
lemma sub_eq_iff_eq_add' : a - b = c ↔ a = b + c :=
by rw [sub_eq_iff_eq_add, add_comm]
@[simp]
lemma add_sub_cancel' (a b : G) : a + b - a = b :=
by rw [sub_eq_neg_add, neg_add_cancel_left]
@[simp]
lemma add_sub_cancel'_right (a b : G) : a + (b - a) = b :=
by rw [← add_sub_assoc, add_sub_cancel']
@[simp] lemma add_add_neg_cancel'_right (a b : G) : a + (b + -a) = b :=
add_sub_cancel'_right a b
lemma sub_right_comm (a b c : G) : a - b - c = a - c - b :=
add_right_comm _ _ _
lemma add_add_sub_cancel (a b c : G) : (a + c) + (b - c) = a + b :=
by rw [add_assoc, add_sub_cancel'_right]
lemma sub_add_add_cancel (a b c : G) : (a - c) + (b + c) = a + b :=
by rw [add_left_comm, sub_add_cancel, add_comm]
lemma sub_add_sub_cancel' (a b c : G) : (a - b) + (c - a) = c - b :=
by rw add_comm; apply sub_add_sub_cancel
lemma add_sub_sub_cancel (a b c : G) : (a + b) - (a - c) = b + c :=
by rw [← sub_add, add_sub_cancel']
lemma sub_sub_sub_cancel_left (a b c : G) : (c - a) - (c - b) = b - a :=
by rw [← neg_sub b c, sub_neg_eq_add, add_comm, sub_add_sub_cancel]
lemma sub_eq_sub_iff_add_eq_add : a - b = c - d ↔ a + d = c + b :=
begin
rw [sub_eq_iff_eq_add, sub_add_eq_add_sub, eq_comm, sub_eq_iff_eq_add'],
simp only [add_comm, eq_comm]
end
lemma sub_eq_sub_iff_sub_eq_sub : a - b = c - d ↔ a - c = b - d :=
by simp [-sub_eq_add_neg, sub_eq_sub_iff_add_eq_add, add_comm]
end add_comm_group
|
add8311dc48984102a6cafa498af3cba6f63581e | 3f48345ac9bbaa421714efc9872a0409379bb4ae | /src/set_category/diagram_lemmas.lean | e4ab594254ce55225b5b4f1fbe04df5501d54a61 | [] | no_license | QaisHamarneh/Coalgebra-in-Lean | b4318ee6d83780e5c734eb78fed98b1fe8016f7e | bd0452df98bc64b608e5dfd7babc42c301bb6a46 | refs/heads/master | 1,663,371,200,241 | 1,661,004,695,000 | 1,661,004,695,000 | 209,798,828 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 4,947 | lean | import category_theory.category
import category_theory.functor
import category_theory.types
import help_functions
import tactic.tidy
namespace diagram_lemmas
universe u
open classical function set category_theory help_functions
local notation f ` ⊚ ` :80 g:80 := category_struct.comp g f
variables {F : Type u ⥤ Type u}
{A B C : Type u}
lemma diagram_injective (f: B ⟶ A)
(g: C ⟶ A)
(inj : injective f)
: (∃! h : C ⟶ B , f ∘ h = g) ↔
(range g ⊆ range f)
:=
iff.intro
begin
tidy
end
begin
assume im,
let G : C → B → Prop := λ c b , g c = f b,
have G1 : ∀ c : C , ∃ b : B, G c b
:=
have G10 :
∀ a : A , a ∈ range g →
a ∈ range f := im,
have G11 :
∀ c : C , g c ∈ range f :=
λ c ,
have G110 : g c ∈ range g := by tidy,
G10 (g c) G110,
have G12 : ∀ c : C , ∃ b : B , g c = f b :=
λ c : C,
have G110 : g c ∈ range f := G11 c,
by tidy,
G12,
have G2 : ∀ c : C , ∃! b : B, G c b
:=
λ c,
have G20 : G c (some (G1 c)) := some_spec (G1 c),
have G21 : f (some (G1 c)) = g c :=
show f (some (G1 c)) = g c,
from
have G210 : _ := G c (some (G1 c)),
by tidy,
have G22 : ∀ b₁ : B, G c b₁ →
b₁ = (some (G1 c)) :=
λ b₁ : B, assume cbG : G c b₁,
show b₁ = (some (G1 c)), from
have G220 : f b₁ = g c := by tidy,
have G221 : f (some (G1 c)) = f b₁ := by rw [G21 , G220],
eq.symm (inj G221),
by tidy,
let h : C ⟶ B := graph_to_map G G2,
have G3 : ∀ c , (f ∘ h) c = g c:=
assume c,
have G31 : h c = some (G2 c) := by tidy,
have G32 : _ := some_spec (G2 c),
have G33 : G c (some (G2 c)) := and.left G32,
by tidy,
have G4 : f ∘ h = g := funext G3,
have G5 : ∀ h₁ : C ⟶ B , f ∘ h₁ = g → h₁ = h :=
assume h₁ fh,
have G51 : f ∘ h₁ = f ∘ h := by rw [fh , G4],
have G511 : f ⊚ h₁ = f ⊚ h := by tidy,
have G52 : mono f := iff.elim_right (mono_iff_injective f) inj,
have G53 : _ := G52.right_cancellation,
G53 h₁ h G511,
exact exists_unique.intro h G4 G5
end
lemma diagram_surjective
(f : A ⟶ B)
(g : A ⟶ C)
(sur: surjective f)
: (∃! h : B ⟶ C , h ∘ f = g) ↔
(sub_kern f g)
:=
iff.intro
begin
assume ex : ∃ h , (h ∘ f = g ∧
∀ h₁, h₁ ∘ f = g → h₁ = h),
show ∀ a₁ a₂ , kern f a₁ a₂ → kern g a₁ a₂,
cases ex with h spec,
exact spec.1 ▸ kern_comp f h
end
begin
assume k : ∀ a₁ a₂ , f a₁ = f a₂ → g a₁ = g a₂,
let h : B → C := λ b : B, g (surj_inv sur b),
have s2 : ∀ a , f (surj_inv sur (f a)) = f a :=
assume a , surj_inv_eq sur (f a),
have s3 : ∀ a , g (surj_inv sur (f a)) = g a :=
assume a , k (surj_inv sur (f a)) a (s2 a),
have s4 : ∀ a , h (f a) = g a :=
assume a,
show g (surj_inv sur (f a)) = g a,
from s3 a,
have s5: h ∘ f = g := funext s4,
have s6 : ∀ h₂ :B → C , h₂ ∘ f = g → h₂ = h :=
begin
assume h₂ h2,
have s61 : h₂ ∘ f = h ∘ f := by simp [s5 , h2],
haveI s62 : epi f := (epi_iff_surjective f).2 sur,
have left_cancel := s62.left_cancellation,
have s63 : h₂ = h := left_cancel h₂ h s61,
tidy
end,
exact exists_unique.intro h s5 s6,
end
end diagram_lemmas |
883b9b86dcb90fabfb2f1813f19fef5ada3ae88a | 4727251e0cd73359b15b664c3170e5d754078599 | /src/topology/category/Top/adjunctions.lean | 62a4711da14c769c7452a75f67e8e7a25c3d4f86 | [
"Apache-2.0"
] | permissive | Vierkantor/mathlib | 0ea59ac32a3a43c93c44d70f441c4ee810ccceca | 83bc3b9ce9b13910b57bda6b56222495ebd31c2f | refs/heads/master | 1,658,323,012,449 | 1,652,256,003,000 | 1,652,256,003,000 | 209,296,341 | 0 | 1 | Apache-2.0 | 1,568,807,655,000 | 1,568,807,655,000 | null | UTF-8 | Lean | false | false | 1,252 | lean | /-
Copyright (c) 2017 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Patrick Massot, Mario Carneiro
-/
import topology.category.Top.basic
import category_theory.adjunction.basic
/-!
# Adjunctions regarding the category of topological spaces
This file shows that the forgetful functor from topological spaces to types has a left and right
adjoint, given by `Top.discrete`, resp. `Top.trivial`, the functors which equip a type with the
discrete, resp. trivial, topology.
-/
universe u
open category_theory
open Top
namespace Top
/-- Equipping a type with the discrete topology is left adjoint to the forgetful functor
`Top ⥤ Type`. -/
@[simps unit counit]
def adj₁ : discrete ⊣ forget Top.{u} :=
adjunction.mk_of_unit_counit
{ unit := { app := λ X, id },
counit := { app := λ X, ⟨id, continuous_bot⟩ } }
/-- Equipping a type with the trivial topology is right adjoint to the forgetful functor
`Top ⥤ Type`. -/
@[simps unit counit]
def adj₂ : forget Top.{u} ⊣ trivial :=
adjunction.mk_of_unit_counit
{ unit := { app := λ X, ⟨id, continuous_top⟩ },
counit := { app := λ X, id } }
instance : is_right_adjoint (forget Top.{u}) := ⟨_, adj₁⟩
end Top
|
fe48e3c07f4ebceed94aaa13008c42acbef273af | ba4794a0deca1d2aaa68914cd285d77880907b5c | /src/tactic/modded.lean | a3f7a6abc8c765edeb9b10325ccf68ca8c04213a | [
"Apache-2.0"
] | permissive | ChrisHughes24/natural_number_game | c7c00aa1f6a95004286fd456ed13cf6e113159ce | 9d09925424da9f6275e6cfe427c8bcf12bb0944f | refs/heads/master | 1,600,715,773,528 | 1,573,910,462,000 | 1,573,910,462,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,445 | lean | -- modded induction', cases', rw'
-- *TODO* -- waiting for simon PR for symmetry'
import tactic.interactive
run_cmd mk_simp_attr `leakage
open lean
open lean.parser
local postfix `?`:9001 := optional
local postfix *:9001 := many
namespace tactic
namespace interactive
open interactive interactive.types expr
private meta def resolve_name' (n : name) : tactic expr :=
do {
p ← resolve_name n,
match p with
| expr.const n _ := mk_const n -- create metavars for universe levels
| _ := i_to_expr p
end
}
private meta def rw_goal (cfg : rewrite_cfg) (rs : list rw_rule) : tactic unit :=
rs.mmap' $ λ r, do
save_info r.pos,
eq_lemmas ← get_rule_eqn_lemmas r,
orelse'
(do e ← to_expr' r.rule, rewrite_target e {symm := r.symm, ..cfg})
(eq_lemmas.mfirst $ λ n, do e ← mk_const n, rewrite_target e {symm := r.symm, ..cfg})
(eq_lemmas.empty)
private meta def uses_hyp (e : expr) (h : expr) : bool :=
e.fold ff $ λ t _ r, r || to_bool (t = h)
private meta def rw_hyp (cfg : rewrite_cfg) : list rw_rule → expr → tactic unit
| [] hyp := skip
| (r::rs) hyp := do
save_info r.pos,
eq_lemmas ← get_rule_eqn_lemmas r,
orelse'
(do e ← to_expr' r.rule, when (not (uses_hyp e hyp)) $ rewrite_hyp e hyp {symm := r.symm, ..cfg} >>= rw_hyp rs)
(eq_lemmas.mfirst $ λ n, do e ← mk_const n, rewrite_hyp e hyp {symm := r.symm, ..cfg} >>= rw_hyp rs)
(eq_lemmas.empty)
private meta def rw_core (rs : parse rw_rules) (loca : parse location) (cfg : rewrite_cfg) : tactic unit :=
match loca with
| loc.wildcard := loca.try_apply (rw_hyp cfg rs.rules) (rw_goal cfg rs.rules)
| _ := loca.apply (rw_hyp cfg rs.rules) (rw_goal cfg rs.rules)
end >> (returnopt rs.end_pos >>= save_info <|> skip)
meta def rw' (q : parse rw_rules) (l : parse location) (cfg : rewrite_cfg := {}) : tactic unit :=
propagate_tags (rw_core q l cfg)
end interactive
end tactic
namespace tactic.interactive
open tactic.interactive interactive.types expr lean lean.parser tactic interactive
meta def induction' (hp : parse cases_arg_p) (ids : parse with_ident_list) : tactic unit :=
do tactic.interactive.induction hp none ids none,
all_goals `[try { dsimp only with leakage at * }]
--meta def induction' (hp : parse cases_arg_p) (ids : parse with_ident_list) : tactic unit :=
--do tactic.interactive.induction hp none ids none,
--all_goals `[
-- try {rw' (show mynat.zero = (0 : mynat), from rfl) at *},
-- try {change mynat.le with (≤) at *}]
-- try {change @mynat.succ with (λ n, n + 1) at *, dsimp only at *}]
meta def cases' (hp : parse cases_arg_p) (ids : parse with_ident_list) : tactic unit :=
do tactic.interactive.cases hp ids,
all_goals `[try { dsimp only with leakage at * }]
--meta def cases' : parse cases_arg_p → parse with_ident_list → tactic unit
--| (none, p) ids := do
-- e ← i_to_expr p,
-- cases_core e ids,
-- all_goals `[
-- try {rw' (show mynat.zero = (0 : mynat), from rfl) at *},
-- try {rw' (show mynat.le = (≤), from rfl) at *}
-- ]
--| (some h, p) ids := do
-- x ← get_unused_name,
-- generalize h () (p, x),
-- hx ← get_local x,
-- cases_core hx ids,
-- all_goals `[
-- try {rw' (show mynat.zero = (0 : mynat), from rfl) at *},
-- try {rw' (show mynat.le = (≤), from rfl) at *}]
-- next def is waiting on https://github.com/leanprover-community/mathlib/pull/1269
--meta def := symmetry' -- already there
end tactic.interactive |
ab28c51a54cdaec2e711f2e5ac31ba9dc747338c | d642a6b1261b2cbe691e53561ac777b924751b63 | /src/topology/uniform_space/pi.lean | caf122a244dc041182e327d771130f58328e3899 | [
"Apache-2.0"
] | permissive | cipher1024/mathlib | fee56b9954e969721715e45fea8bcb95f9dc03fe | d077887141000fefa5a264e30fa57520e9f03522 | refs/heads/master | 1,651,806,490,504 | 1,573,508,694,000 | 1,573,508,694,000 | 107,216,176 | 0 | 0 | Apache-2.0 | 1,647,363,136,000 | 1,508,213,014,000 | Lean | UTF-8 | Lean | false | false | 1,964 | lean | /-
Copyright (c) 2019 Patrick Massot. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Patrick Massot
Indexed product of uniform spaces
-/
import topology.uniform_space.cauchy
import topology.uniform_space.separation
noncomputable theory
open_locale uniformity
section
open filter lattice uniform_space
universe u
variables {ι : Type*} (α : ι → Type u) [U : Πi, uniform_space (α i)]
include U
instance Pi.uniform_space : uniform_space (Πi, α i) :=
uniform_space.of_core_eq
(⨅i, uniform_space.comap (λ a : Πi, α i, a i) (U i)).to_core
Pi.topological_space $ eq.symm to_topological_space_infi
lemma Pi.uniformity :
𝓤 (Π i, α i) = ⨅ i : ι, filter.comap (λ a, (a.1 i, a.2 i)) $ 𝓤 (α i) :=
infi_uniformity
lemma Pi.uniform_continuous_proj (i : ι) : uniform_continuous (λ (a : Π (i : ι), α i), a i) :=
begin
rw uniform_continuous_iff,
exact infi_le (λ j, uniform_space.comap (λ (a : Π (i : ι), α i), a j) (U j)) i
end
lemma Pi.uniform_space_topology :
(Pi.uniform_space α).to_topological_space = Pi.topological_space := rfl
instance Pi.complete [∀ i, complete_space (α i)] : complete_space (Π i, α i) :=
⟨begin
intros f hf,
have : ∀ i, ∃ x : α i, filter.map (λ a : Πi, α i, a i) f ≤ nhds x,
{ intro i,
have key : cauchy (map (λ (a : Π (i : ι), α i), a i) f),
from cauchy_map (Pi.uniform_continuous_proj α i) hf,
exact (cauchy_iff_exists_le_nhds $ map_ne_bot hf.1).1 key },
choose x hx using this,
use x,
rw [show nhds x = (⨅i, comap (λa, a i) (nhds (x i))),
by rw Pi.uniform_space_topology ; exact nhds_pi,
le_infi_iff],
exact λ i, map_le_iff_le_comap.mp (hx i),
end⟩
instance Pi.separated [∀ i, separated (α i)] : separated (Π i, α i) :=
separated_def.2 $ assume x y H,
begin
ext i,
apply eq_of_separated_of_uniform_continuous (Pi.uniform_continuous_proj α i),
apply H,
end
end
|
97f30675030fa1fc92692de42580834f8c931d95 | ca1ad81c8733787aba30f7a8d63f418508e12812 | /clfrags/src/hilbert/wr/proofs/or.lean | d1cf3a4431a3cbe3abc845180ad2b45515b0102c | [] | no_license | greati/hilbert-classical-fragments | 5cdbe07851e979c8a03c621a5efd4d24bbfa333a | 18a21ac6b2e890060eb4ae65752fc0245394d226 | refs/heads/master | 1,591,973,117,184 | 1,573,822,710,000 | 1,573,822,710,000 | 194,334,439 | 2 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 4,011 | lean | import hilbert.wr.or
namespace clfrags
namespace hilbert
namespace wr
namespace or
theorem d₁' {a b : Prop} (h₁ : b) : or a b :=
have h₂ : or b a, from or.d₁ h₁,
show or a b, from or.d₃ h₂
theorem d₄' {a b c : Prop} (h₁ : or (or a b) c) : or a (or b c) :=
have h₂ : or c (or a b), from or.d₃ h₁,
have h₃ : or (or c a) b, from or.d₄ h₂,
have h₄ : or b (or c a), from or.d₃ h₃,
have h₅ : or (or b c) a, from or.d₄ h₄,
show or a (or b c), from or.d₃ h₅
theorem d₁_or {a b c : Prop} (h₁ : or c a) : or c (or a b) :=
have h₂ : or (or c a) b, from or.d₁ h₁,
show or c (or a b), from or.d₄' h₂
theorem d₂_or {a b : Prop} (h₁ : or b (or a a)) : or b a :=
have h₂ : or (or b a) a, from or.d₄ h₁,
have h₃ : or a (or b a), from or.d₃ h₂,
have h₄ : or b (or a (or b a)), from or.d₁' h₃,
have h₅ : or (or b a) (or b a), from or.d₄ h₄,
show or b a, from or.d₂ h₅
theorem d₃_or {a b c : Prop} (h₁ : or c (or a b)) : or c (or b a) :=
have h₂ : or (or c a) b, from or.d₄ h₁,
have h₃ : or (or (or c a) b) a, from or.d₁ h₂,
have h₄ : or (or c a) (or b a), from or.d₄' h₃,
have h₅ : or c (or a (or b a)), from or.d₄' h₄,
have h₆ : or (or a (or b a)) c, from or.d₃ h₅,
have h₇ : or a (or (or b a) c), from or.d₄' h₆,
have h₈ : or b (or a (or (or b a) c)), from or.d₁' h₇,
have h₉ : or (or b a) (or (or b a) c), from or.d₄ h₈,
have h₁₀ : or (or (or b a) (or b a)) c, from or.d₄ h₉,
have h₁₁ : or c (or (or b a) (or b a)), from or.d₃ h₁₀,
show or c (or b a), from or.d₂_or h₁₁
theorem d₄_or {a b c d : Prop} (h₁ : or d (or a (or b c))) : or d (or (or a b) c) :=
have h₂ : or (or d a) (or b c), from or.d₄ h₁,
have h₃ : or (or d a) (or c b), from or.d₃_or h₂,
have h₄ : or (or (or d a) c) b, from or.d₄ h₃,
have h₅ : or (or (or (or d a) c) b) a, from or.d₁ h₄,
have h₆ : or (or (or d a) c) (or b a), from or.d₄' h₅,
have h₇ : or (or (or d a) c) (or a b), from or.d₃_or h₆,
have h₈ : or (or d a) (or c (or a b)), from or.d₄' h₇,
have h₉ : or (or d a) (or (or a b) c), from or.d₃_or h₈,
let e := or (or a b) c in
have h₁₀ : or (or d a) e, from h₉,
have h₁₁ : or d (or a e), from or.d₄' h₁₀,
have h₁₂ : or d (or e a), from or.d₃_or h₁₁,
have h₁₃ : or (or d e) a, from or.d₄ h₁₂,
have h₁₄ : or (or (or d e) a) b, from or.d₁ h₁₃,
have h₁₅ : or (or d e) (or a b), from or.d₄' h₁₄,
have h₁₆ : or (or (or d e) (or a b)) c, from or.d₁ h₁₅,
have h₁₇ : or (or d e) (or (or a b) c), from or.d₄' h₁₆,
have h₁₈ : or (or d e) e, from h₁₇,
have h₁₉ : or d (or e e), from or.d₄' h₁₈,
have h₂₀ : or d e, from or.d₂_or h₁₉,
show or d (or (or a b) c), from h₂₀
end or
end wr
end hilbert
end clfrags
|
b5dcbd47c8d397131552c140fef311557a581f58 | 3618c6e11aa822fd542440674dfb9a7b9921dba0 | /src/multirelation/approach2/free_group2.lean | 0093029c2828ce55125820b8246cdc0e91bd3c9f | [] | no_license | ChrisHughes24/single_relation | 99ceedcc02d236ce46d6c65d72caa669857533c5 | 057e157a59de6d0e43b50fcb537d66792ec20450 | refs/heads/master | 1,683,652,062,698 | 1,683,360,089,000 | 1,683,360,089,000 | 279,346,432 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 10,845 | lean | import algebra.group
import data.list.basic
namespace group_rel
namespace free_group
open native
@[reducible] def free_group := list (ℕ × bool)
def ap : free_group → free_group → free_group := (++)
def cons (i : ℕ) (b : bool) (a : free_group) : free_group :=
⟨i, b⟩ :: a
instance : has_one (free_group) := ⟨[]⟩
lemma one_def : (1 : free_group) = [] := rfl
meta def to_expr : free_group → expr
| [] := `((1 : free_group))
| (⟨i, b⟩::l) := `(cons %%(reflect i) %%(reflect b) %%(to_expr l))
-- inv_core a b is a⁻¹ * b
def inv_core : free_group → free_group → free_group
| [] l := l
| (⟨i, n⟩::l₁) l₂ := inv_core l₁ (⟨i, bnot n⟩::l₂)
instance : has_inv free_group := ⟨λ a, inv_core a []⟩
def inv (a : free_group) : free_group := a⁻¹
lemma inv_def (w : free_group) : w⁻¹ = inv_core w [] := rfl
def mul_aux : Π (l₁ l₂ : free_group), free_group
| [] l₂ := l₂
| (i::l₁) [] := (i :: l₁).reverse_core []
| a@(i::l₁) b@(j::l₂) :=
if i.1 = j.1 ∧ i.2 ≠ j.2
then mul_aux l₁ l₂
else a.reverse_core b
instance : has_mul free_group := ⟨λ l₁ l₂, mul_aux l₁.reverse l₂⟩
lemma mul_def (w₁ w₂ : free_group) : w₁ * w₂ = mul_aux (w₁.reverse_core []) w₂ := rfl
lemma zero_eq_succ_eq (n : ℕ) : 0 = n.succ = false :=
eq_false_intro $ λ h, nat.no_confusion h
lemma succ_eq_zero_eq (n : ℕ) : n.succ = 0 = false :=
eq_false_intro $ λ h, nat.no_confusion h
example : ([⟨1, tt⟩, ⟨0, tt⟩] : free_group)⁻¹ * [⟨1, tt⟩] * [⟨2, tt⟩] *
[⟨3, tt⟩] = [⟨0, ff⟩, ⟨2, tt⟩, ⟨3, tt⟩] :=
begin
simp only [inv_core, inv_def, bool.bnot_true, bool.bnot_false, eq_self_iff_true,
true_and, and_true, one_def, mul_def, list.reverse_core, mul_aux, ne.def,
not_false_iff, if_true, if_false, false_and, and_false,
nat.succ.inj_eq, zero_eq_succ_eq, succ_eq_zero_eq],
end
def length (w : free_group) : ℕ := list.length w
def cost (no_atoms : ℕ) (w : free_group) : ℕ :=
w.foldl (λ cost i, cost * no_atoms + i.1.succ) 0
def subst (x : ℕ) (r : free_group) : Π (w : free_group), free_group
| [] := []
| (⟨a, b⟩::w) :=
if a = x
then cond b
(r⁻¹ * subst w)
(r * subst w)
else [⟨a, b⟩] * subst w
section
variables {G : Type*} [group G]
def nth : list G → ℕ → G
| [] _ := 1
| (g::l) 0 := g
| (g::l) (i+1) := nth l i
def eval (l : list G) : free_group → G
| [] := 1
| (⟨i, tt⟩::w) := (nth l i)⁻¹ * eval w
| (⟨i, ff⟩::w) := (nth l i) * eval w
lemma eval_one (l : list G) : eval l 1 = 1 := rfl
lemma eval_append (l : list G) : Π (w₁ w₂ : free_group),
eval l (w₁ ++ w₂) = eval l w₁ * eval l w₂
| [] w₂ := by simp [eval]
| (⟨i, tt⟩::w) w₂ := by simp [*, eval, mul_assoc]
| (⟨i, ff⟩::w) w₂ := by simp [*, eval, mul_assoc]
lemma eval_cons (l : list G) (i : ℕ) (o : bool) (w : free_group) :
eval l (cons i o w) = nth l i ^ cond o (-1 : int) (1 : int) * eval l w :=
by cases o; simp [cons, eval]
lemma eval_ap (l : list G) (w₁ w₂ : free_group) :
eval l (ap w₁ w₂) = eval l w₁ * eval l w₂ :=
eval_append l w₁ w₂
lemma eval_mul_aux (l : list G) : ∀ w₁ w₂ : free_group,
eval l (mul_aux w₁ w₂) = eval l (w₁.reverse_core w₂)
| [] l₂ := by simp [mul_aux, eval, list.reverse_core_eq]
| (i::l₁) [] := by simp [mul_aux, eval, list.reverse_core_eq]
| (⟨i₁, b₁⟩::w₁) (⟨i₂, b₂⟩::w₂) :=
begin
rw [mul_aux, list.reverse_core, list.reverse_core_eq, eval_append],
split_ifs,
{ dsimp at h,
cases h with hi hb,
subst hi,
cases b₂,
{ simp [bool.eq_tt_of_ne_ff hb, eval, eval_mul_aux, list.reverse_core_eq,
eval_append] },
{ simp [bool.eq_ff_of_ne_tt hb, eval, eval_mul_aux, list.reverse_core_eq,
eval_append] } },
{ simp [eval_append, eval] }
end
lemma eval_mul (l : list G) (w₁ w₂ : free_group) :
eval l (w₁ * w₂) = eval l w₁ * eval l w₂ :=
by simp [mul_def, eval_mul_aux, list.reverse_core_eq, eval_append]
lemma eval_inv_core (l : list G) : ∀ (w₁ w₂ : free_group),
eval l (inv_core w₁ w₂) = (eval l w₁)⁻¹ * eval l w₂
| [] w := by simp [inv_core, eval]
| (⟨i, ff⟩::w₁) w₂ :=
by rw [inv_core, bool.bnot_false, eval_inv_core, eval,
eval, mul_inv_rev, mul_assoc]
| (⟨i, tt⟩::w₁) w₂ :=
by rw [inv_core, bool.bnot_true, eval_inv_core, eval,
eval, mul_inv_rev, mul_assoc, inv_inv]
lemma eval_inv (l : list G) (w : free_group) : eval l w⁻¹ = (eval l w)⁻¹ :=
by rw [inv_def, eval_inv_core, eval, mul_one]
lemma eval_subst (l : list G) (x : ℕ) (r w : free_group)
(h : eval l [⟨x, ff⟩] = eval l r) :
eval l (subst x r w) = eval l w :=
begin
induction w with i w ih,
{ refl },
{ cases i,
rw [subst],
split_ifs,
{ subst h_1,
cases i_snd;
simp [eval_mul, eval, *, eval_inv] at * },
{ cases i_snd;
simp [eval_mul, eval, ih] } }
end
lemma subst_eq_one_of_eq_one
(l : list G) (x : ℕ)
(r₁ r₂ r rhs word new_word : free_group)
(hnew_r : rhs = r₁⁻¹ * r₂⁻¹)
(hr : r = r₁ ++ [⟨x, ff⟩] ++ r₂)
(hr : eval l r = 1)
(hw : eval l word = 1)
(hs : new_word = subst x rhs word) :
eval l new_word = 1 :=
begin
substs rhs r new_word,
rw [eval_subst _ _ _ _ _, hw],
simpa [eval_mul, eval_append, eval, mul_assoc, eval_inv,
eq_inv_mul_iff_mul_eq, eq_inv_iff_mul_eq_one] using hr
end
lemma eq_one_of_subst_eq_one
(l : list G) (x : ℕ)
(r₁ r₂ r rhs word new_word : free_group)
(hnew_r : rhs = r₁⁻¹ * r₂⁻¹)
(hr : r = r₁ ++ [⟨x, ff⟩] ++ r₂)
(hr : eval l r = 1)
(hs : new_word = subst x rhs word)
(hw : eval l new_word = 1) :
eval l word = 1 :=
begin
substs rhs r new_word,
rw [eval_subst] at hw, rw hw,
{ simp only [eval_append, eval_mul, eval_inv] at *,
rwa [eq_inv_mul_iff_mul_eq, eq_inv_iff_mul_eq_one] }
end
meta def is_subst_aux : free_group → free_group →
rb_set ℕ →
rb_map ℕ (ℕ × bool × free_group × free_group) →
rb_set ℕ × rb_map ℕ (ℕ × bool × free_group × free_group)
| [] [] rbs rbm := (rbs, rbm)
| (a::l) [] rbs rbm := is_subst_aux l [a] (rbs.insert a.1) (rbm.insert a.1 (a.1, a.2, [], l))
| (a::l₁) w₂@(b::l₂) rbs rbm :=
match rbs.contains a.1 with
| ff := is_subst_aux l₁ (a::b::l₂) (rbs.insert a.1) (rbm.insert a.1 (a.1, a.2, w₂.reverse, l₁))
| tt := is_subst_aux l₁ (a::b::l₂) rbs (rbm.erase a.1)
end
| [] l₂ rbs rbm := (rbs, rbm)
end
/-- is_subst `w` returns `(i, b, w₁, w₂)` such that `w = w₁ ++ [⟨i, b⟩] ++ w₂`,
and neither `w₁` nor `w₂` contain `i` -/
meta def is_subst (w : free_group) : option (ℕ × bool × free_group × free_group) :=
(is_subst_aux w [] mk_rb_map mk_rb_map).2.min
meta def cyclically_reduce_aux : Π (w wr : free_group), free_group
| [] wr := []
| w [] := []
| w@(⟨i₁, b₁⟩::l₁) (⟨i₂, b₂⟩::l₂) :=
if i₁ = i₂ ∧ b₁ = bnot b₂
then cyclically_reduce_aux l₁.init l₂.init
else w
meta def cyclically_reduce (w : free_group) : free_group :=
cyclically_reduce_aux w w.reverse
meta def cyclically_reduce_conj_aux : Π (w wr : free_group), free_group
| [] wr := []
| w [] := []
| w@(⟨i₁, b₁⟩::l₁) (⟨i₂, b₂⟩::l₂) :=
if i₁ = i₂ ∧ b₁ = bnot b₂
then [⟨i₁, b₁⟩] ++ cyclically_reduce_conj_aux l₁.init l₂.init
else 1
meta def cyclically_reduce_conj (w : free_group) : free_group :=
cyclically_reduce_conj_aux w w.reverse
def of : ℕ → free_group := λ n, [(n, ff)]
def lt : Π (a b : free_group), bool
| _ [] := ff
| [] (a::l) := tt
| (a::l₁) (b::l₂) :=
if a < b
then tt
else lt l₁ l₂
instance : has_pow free_group ℕ := ⟨λ a n, nat.rec_on n 1 (λ _, (* a))⟩
instance : has_pow free_group ℤ := ⟨λ a n, int.cases_on n
((^) a)
(λ n, a⁻¹ ^ (n + 1))⟩
-- @[instance, priority 100000] def free_group.has_lt :
-- has_lt (free_group) := ⟨λ a b, lt a b⟩
-- instance : decidable_rel ((<) : free_group → free_group → Prop) :=
-- λ _ _, show decidable ((_ : bool) : Prop), by apply_instance
section eqv
inductive eqv : free_group → free_group → Prop
| refl : ∀ a, eqv a a
| symm : ∀ {a b}, eqv a b → eqv b a
| trans : ∀ {a b c}, eqv a b → eqv b c → eqv a c
| cons : ∀ {a b i o}, eqv a b → eqv (cons i o a) (cons i o b)
| ap : ∀ {a b c d}, eqv a b → eqv c d → eqv (ap a c) (ap b d)
| inv_core : ∀ {a b c d}, eqv a b → eqv c d → eqv (inv_core a c) (inv_core b d)
| mul_inv_cancel : ∀ i a, eqv (cons i ff (cons i tt a)) a
| inv_mul_cancel : ∀ i a, eqv (cons i tt (cons i ff a)) a
attribute [refl] eqv.refl
attribute [symm] eqv.symm
attribute [trans] eqv.trans
attribute [congr] eqv.cons eqv.ap
variables {G : Type*} [group G]
lemma inv_core_eq (a b : free_group) : inv_core a b = a⁻¹ ++ b :=
begin
induction a with i a ih generalizing b,
{ simp [inv_def, inv_core] },
{ rw [inv_def] at *,
rcases i with ⟨_,_|_⟩,
{ rw [inv_core, ih, inv_core, ih [_]],
simp },
{ rw [inv_core, ih, inv_core, ih [_]],
simp } }
end
lemma inv_core_cons_ff_eqv (i : ℕ) (a b : free_group) :
eqv (inv_core (cons i ff a) b) (inv_core a (cons i tt b)) :=
by refl
lemma inv_core_cons_tt_eqv (i : ℕ) (a b : free_group) :
eqv (inv_core (cons i tt a) b) (inv_core a (cons i ff b)) :=
by refl
lemma inv_core_one_eqv (b : free_group) :
eqv (inv_core 1 b) b := by refl
lemma ap_one_eqv (a : free_group) : eqv (ap a 1) a := by simp [ap, one_def]
lemma one_ap_eqv (a : free_group) : eqv (ap 1 a) a := by simp [ap, one_def]
lemma one_inv_eqv : eqv (inv 1) 1 := eqv.refl _
lemma ap_cons_eqv {i : ℕ} {o : bool} {a b : free_group} :
eqv (ap (cons i o a) b) (cons i o (ap a b)) :=
by simp [cons, ap]
lemma eval_eq_of_eqv (atoms : list G) {a b : free_group} (hab : eqv a b) :
eval atoms a = eval atoms b :=
by induction hab; simp [eval, eval_append, *, ap, eval_cons, eval_inv, inv_core_eq] at *
meta def mk_free_group_simp_lemmas : tactic simp_lemmas :=
do sl : simp_lemmas ←
([``eqv.mul_inv_cancel, ``eqv.inv_mul_cancel,
``ap_one_eqv, ``one_ap_eqv, ``ap_cons_eqv,
``inv_core_cons_ff_eqv, ``inv_core_cons_tt_eqv,
``inv_core_one_eqv] : list name).mfoldl
(λ (sl : simp_lemmas) n, sl.add_simp n)
simp_lemmas.mk,
[``eqv.cons, ``eqv.inv_core, ``eqv.ap].mfoldl
(λ (sl : simp_lemmas) n, sl.add_congr n)
sl
run_cmd mk_free_group_simp_lemmas
open tactic expr
meta def free_group_simp (lhs : expr) (sl : simp_lemmas) : tactic expr :=
do
(lhs', prl) ← (simplify sl [] lhs (default _) ``eqv <|> return (lhs, `(eqv.refl %%lhs))),
return prl
end eqv
end free_group
end group_rel
|
c77c0ae404ce1480d2544d880ce19edaf3a01c13 | 1dd482be3f611941db7801003235dc84147ec60a | /src/analysis/normed_space/basic.lean | d6432b6d650e82f9e11eb04e1d70a5650c32eacd | [
"Apache-2.0"
] | permissive | sanderdahmen/mathlib | 479039302bd66434bb5672c2a4cecf8d69981458 | 8f0eae75cd2d8b7a083cf935666fcce4565df076 | refs/heads/master | 1,587,491,322,775 | 1,549,672,060,000 | 1,549,672,060,000 | 169,748,224 | 0 | 0 | Apache-2.0 | 1,549,636,694,000 | 1,549,636,694,000 | null | UTF-8 | Lean | false | false | 17,060 | lean | /-
Copyright (c) 2018 Patrick Massot. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Normed spaces.
Authors: Patrick Massot, Johannes Hölzl
-/
import algebra.pi_instances
import linear_algebra.basic
import topology.instances.nnreal
variables {α : Type*} {β : Type*} {γ : Type*} {ι : Type*}
noncomputable theory
open filter metric
local notation f `→_{`:50 a `}`:0 b := tendsto f (nhds a) (nhds b)
class has_norm (α : Type*) := (norm : α → ℝ)
export has_norm (norm)
notation `∥`:1024 e:1 `∥`:1 := norm e
class normed_group (α : Type*) extends has_norm α, add_comm_group α, metric_space α :=
(dist_eq : ∀ x y, dist x y = norm (x - y))
/-- Construct a normed group from a translation invariant distance -/
def normed_group.of_add_dist [has_norm α] [add_comm_group α] [metric_space α]
(H1 : ∀ x:α, ∥x∥ = dist x 0)
(H2 : ∀ x y z : α, dist x y ≤ dist (x + z) (y + z)) : normed_group α :=
{ dist_eq := λ x y, begin
rw H1, apply le_antisymm,
{ rw [sub_eq_add_neg, ← add_right_neg y], apply H2 },
{ have := H2 (x-y) 0 y, rwa [sub_add_cancel, zero_add] at this }
end }
/-- Construct a normed group from a translation invariant distance -/
def normed_group.of_add_dist' [has_norm α] [add_comm_group α] [metric_space α]
(H1 : ∀ x:α, ∥x∥ = dist x 0)
(H2 : ∀ x y z : α, dist (x + z) (y + z) ≤ dist x y) : normed_group α :=
{ dist_eq := λ x y, begin
rw H1, apply le_antisymm,
{ have := H2 (x-y) 0 y, rwa [sub_add_cancel, zero_add] at this },
{ rw [sub_eq_add_neg, ← add_right_neg y], apply H2 }
end }
section normed_group
variables [normed_group α] [normed_group β]
lemma dist_eq_norm (g h : α) : dist g h = ∥g - h∥ :=
normed_group.dist_eq _ _
@[simp] lemma dist_zero_right (g : α) : dist g 0 = ∥g∥ :=
by { rw[dist_eq_norm], simp }
lemma norm_triangle (g h : α) : ∥g + h∥ ≤ ∥g∥ + ∥h∥ :=
calc ∥g + h∥ = ∥g - (-h)∥ : by simp
... = dist g (-h) : by simp[dist_eq_norm]
... ≤ dist g 0 + dist 0 (-h) : by apply dist_triangle
... = ∥g∥ + ∥h∥ : by simp[dist_eq_norm]
@[simp] lemma norm_nonneg (g : α) : 0 ≤ ∥g∥ :=
by { rw[←dist_zero_right], exact dist_nonneg }
lemma norm_eq_zero (g : α) : ∥g∥ = 0 ↔ g = 0 :=
by { rw[←dist_zero_right], exact dist_eq_zero }
@[simp] lemma norm_zero : ∥(0:α)∥ = 0 := (norm_eq_zero _).2 (by simp)
lemma norm_pos_iff (g : α) : ∥ g ∥ > 0 ↔ g ≠ 0 :=
begin
split ; intro h ; rw[←dist_zero_right] at *,
{ exact dist_pos.1 h },
{ exact dist_pos.2 h }
end
lemma norm_le_zero_iff (g : α) : ∥g∥ ≤ 0 ↔ g = 0 :=
by { rw[←dist_zero_right], exact dist_le_zero }
@[simp] lemma norm_neg (g : α) : ∥-g∥ = ∥g∥ :=
calc ∥-g∥ = ∥0 - g∥ : by simp
... = dist 0 g : (dist_eq_norm 0 g).symm
... = dist g 0 : dist_comm _ _
... = ∥g - 0∥ : (dist_eq_norm g 0)
... = ∥g∥ : by simp
lemma abs_norm_sub_norm_le (g h : α) : abs(∥g∥ - ∥h∥) ≤ ∥g - h∥ :=
abs_le.2 $ and.intro
(suffices -∥g - h∥ ≤ -(∥h∥ - ∥g∥), by simpa,
neg_le_neg $ sub_right_le_of_le_add $
calc ∥h∥ = ∥h - g + g∥ : by simp
... ≤ ∥h - g∥ + ∥g∥ : norm_triangle _ _
... = ∥-(g - h)∥ + ∥g∥ : by simp
... = ∥g - h∥ + ∥g∥ : by { rw [norm_neg (g-h)] })
(sub_right_le_of_le_add $ calc ∥g∥ = ∥g - h + h∥ : by simp ... ≤ ∥g-h∥ + ∥h∥ : norm_triangle _ _)
lemma dist_norm_norm_le (g h : α) : dist ∥g∥ ∥h∥ ≤ ∥g - h∥ :=
abs_norm_sub_norm_le g h
lemma norm_sub_rev (g h : α) : ∥g - h∥ = ∥h - g∥ :=
by rw ←norm_neg; simp
section nnnorm
def nnnorm (a : α) : nnreal := ⟨norm a, norm_nonneg a⟩
@[simp] lemma coe_nnnorm (a : α) : (nnnorm a : ℝ) = norm a := rfl
lemma nndist_eq_nnnorm (a b : α) : nndist a b = nnnorm (a - b) := nnreal.eq $ dist_eq_norm _ _
lemma nnnorm_eq_zero (a : α) : nnnorm a = 0 ↔ a = 0 :=
by simp only [nnreal.eq_iff.symm, nnreal.coe_zero, coe_nnnorm, norm_eq_zero]
@[simp] lemma nnnorm_zero : nnnorm (0 : α) = 0 :=
nnreal.eq norm_zero
lemma nnnorm_triangle (g h : α) : nnnorm (g + h) ≤ nnnorm g + nnnorm h :=
by simpa [nnreal.coe_le] using norm_triangle g h
@[simp] lemma nnnorm_neg (g : α) : nnnorm (-g) = nnnorm g :=
nnreal.eq $ norm_neg g
lemma nndist_nnnorm_nnnorm_le (g h : α) : nndist (nnnorm g) (nnnorm h) ≤ nnnorm (g - h) :=
nnreal.coe_le.2 $ dist_norm_norm_le g h
end nnnorm
instance prod.normed_group [normed_group β] : normed_group (α × β) :=
{ norm := λx, max ∥x.1∥ ∥x.2∥,
dist_eq := assume (x y : α × β),
show max (dist x.1 y.1) (dist x.2 y.2) = (max ∥(x - y).1∥ ∥(x - y).2∥), by simp [dist_eq_norm] }
lemma norm_fst_le (x : α × β) : ∥x.1∥ ≤ ∥x∥ :=
begin have : ∥x∥ = max (∥x.fst∥) (∥x.snd∥) := rfl, rw this, simp[le_max_left] end
lemma norm_snd_le (x : α × β) : ∥x.2∥ ≤ ∥x∥ :=
begin have : ∥x∥ = max (∥x.fst∥) (∥x.snd∥) := rfl, rw this, simp[le_max_right] end
instance fintype.normed_group {π : α → Type*} [fintype α] [∀i, normed_group (π i)] :
normed_group (Πb, π b) :=
{ norm := λf, ((finset.sup finset.univ (λ b, nnnorm (f b)) : nnreal) : ℝ),
dist_eq := assume x y,
congr_arg (coe : nnreal → ℝ) $ congr_arg (finset.sup finset.univ) $ funext $ assume a,
show nndist (x a) (y a) = nnnorm (x a - y a), from nndist_eq_nnnorm _ _ }
lemma tendsto_iff_norm_tendsto_zero {f : ι → β} {a : filter ι} {b : β} :
tendsto f a (nhds b) ↔ tendsto (λ e, ∥ f e - b ∥) a (nhds 0) :=
by rw tendsto_iff_dist_tendsto_zero ; simp only [(dist_eq_norm _ _).symm]
lemma lim_norm (x : α) : ((λ g, ∥g - x∥) : α → ℝ) →_{x} 0 :=
tendsto_iff_norm_tendsto_zero.1 (continuous_iff_continuous_at.1 continuous_id x)
lemma lim_norm_zero : ((λ g, ∥g∥) : α → ℝ) →_{0} 0 :=
by simpa using lim_norm (0:α)
lemma continuous_norm : continuous ((λ g, ∥g∥) : α → ℝ) :=
begin
rw continuous_iff_continuous_at,
intro x,
rw [continuous_at, tendsto_iff_dist_tendsto_zero],
exact squeeze_zero (λ t, abs_nonneg _) (λ t, abs_norm_sub_norm_le _ _) (lim_norm x)
end
lemma continuous_nnnorm : continuous (nnnorm : α → nnreal) :=
continuous_subtype_mk _ continuous_norm
instance normed_top_monoid : topological_add_monoid α :=
⟨continuous_iff_continuous_at.2 $ λ ⟨x₁, x₂⟩,
tendsto_iff_norm_tendsto_zero.2
begin
refine squeeze_zero (by simp) _
(by simpa using tendsto_add (lim_norm (x₁, x₂)) (lim_norm (x₁, x₂))),
exact λ ⟨e₁, e₂⟩, calc
∥(e₁ + e₂) - (x₁ + x₂)∥ = ∥(e₁ - x₁) + (e₂ - x₂)∥ : by simp
... ≤ ∥e₁ - x₁∥ + ∥e₂ - x₂∥ : norm_triangle _ _
... ≤ max (∥e₁ - x₁∥) (∥e₂ - x₂∥) + max (∥e₁ - x₁∥) (∥e₂ - x₂∥) :
add_le_add (le_max_left _ _) (le_max_right _ _)
end⟩
instance normed_top_group : topological_add_group α :=
⟨continuous_iff_continuous_at.2 $ λ x,
tendsto_iff_norm_tendsto_zero.2 begin
have : ∀ (e : α), ∥-e - -x∥ = ∥e - x∥,
{ intro, simpa using norm_neg (e - x) },
rw funext this, exact lim_norm x,
end⟩
end normed_group
section normed_ring
class normed_ring (α : Type*) extends has_norm α, ring α, metric_space α :=
(dist_eq : ∀ x y, dist x y = norm (x - y))
(norm_mul : ∀ a b, norm (a * b) ≤ norm a * norm b)
instance normed_ring.to_normed_group [β : normed_ring α] : normed_group α := { ..β }
lemma norm_mul {α : Type*} [normed_ring α] (a b : α) : (∥a*b∥) ≤ (∥a∥) * (∥b∥) :=
normed_ring.norm_mul _ _
lemma norm_pow {α : Type*} [normed_ring α] (a : α) : ∀ {n : ℕ}, n > 0 → ∥a^n∥ ≤ ∥a∥^n
| 1 h := by simp
| (n+2) h :=
le_trans (norm_mul a (a^(n+1)))
(mul_le_mul (le_refl _)
(norm_pow (nat.succ_pos _)) (norm_nonneg _) (norm_nonneg _))
instance prod.normed_ring [normed_ring α] [normed_ring β] : normed_ring (α × β) :=
{ norm_mul := assume x y,
calc
∥x * y∥ = ∥(x.1*y.1, x.2*y.2)∥ : rfl
... = (max ∥x.1*y.1∥ ∥x.2*y.2∥) : rfl
... ≤ (max (∥x.1∥*∥y.1∥) (∥x.2∥*∥y.2∥)) : max_le_max (norm_mul (x.1) (y.1)) (norm_mul (x.2) (y.2))
... = (max (∥x.1∥*∥y.1∥) (∥y.2∥*∥x.2∥)) : by simp[mul_comm]
... ≤ (max (∥x.1∥) (∥x.2∥)) * (max (∥y.2∥) (∥y.1∥)) : by { apply max_mul_mul_le_max_mul_max; simp [norm_nonneg] }
... = (max (∥x.1∥) (∥x.2∥)) * (max (∥y.1∥) (∥y.2∥)) : by simp[max_comm]
... = (∥x∥*∥y∥) : rfl,
..prod.normed_group }
end normed_ring
instance normed_ring_top_monoid [normed_ring α] : topological_monoid α :=
⟨ continuous_iff_continuous_at.2 $ λ x, tendsto_iff_norm_tendsto_zero.2 $
have ∀ e : α × α, e.fst * e.snd - x.fst * x.snd =
e.fst * e.snd - e.fst * x.snd + (e.fst * x.snd - x.fst * x.snd), by intro; rw sub_add_sub_cancel,
begin
apply squeeze_zero,
{ intro, apply norm_nonneg },
{ simp only [this], intro, apply norm_triangle },
{ rw ←zero_add (0 : ℝ), apply tendsto_add,
{ apply squeeze_zero,
{ intro, apply norm_nonneg },
{ intro t, show ∥t.fst * t.snd - t.fst * x.snd∥ ≤ ∥t.fst∥ * ∥t.snd - x.snd∥,
rw ←mul_sub, apply norm_mul },
{ rw ←mul_zero (∥x.fst∥), apply tendsto_mul,
{ apply continuous_iff_continuous_at.1,
apply continuous.comp,
{ apply continuous_fst },
{ apply continuous_norm }},
{ apply tendsto_iff_norm_tendsto_zero.1,
apply continuous_iff_continuous_at.1,
apply continuous_snd }}},
{ apply squeeze_zero,
{ intro, apply norm_nonneg },
{ intro t, show ∥t.fst * x.snd - x.fst * x.snd∥ ≤ ∥t.fst - x.fst∥ * ∥x.snd∥,
rw ←sub_mul, apply norm_mul },
{ rw ←zero_mul (∥x.snd∥), apply tendsto_mul,
{ apply tendsto_iff_norm_tendsto_zero.1,
apply continuous_iff_continuous_at.1,
apply continuous_fst },
{ apply tendsto_const_nhds }}}}
end ⟩
instance normed_top_ring [normed_ring α] : topological_ring α :=
⟨ continuous_iff_continuous_at.2 $ λ x, tendsto_iff_norm_tendsto_zero.2 $
have ∀ e : α, -e - -x = -(e - x), by intro; simp,
by simp only [this, norm_neg]; apply lim_norm ⟩
section normed_field
class normed_field (α : Type*) extends has_norm α, discrete_field α, metric_space α :=
(dist_eq : ∀ x y, dist x y = norm (x - y))
(norm_mul : ∀ a b, norm (a * b) = norm a * norm b)
instance normed_field.to_normed_ring [i : normed_field α] : normed_ring α :=
{ norm_mul := by finish [i.norm_mul], ..i }
@[simp] lemma norm_one {α : Type*} [normed_field α] : ∥(1 : α)∥ = 1 :=
have ∥(1 : α)∥ * ∥(1 : α)∥ = ∥(1 : α)∥ * 1, by calc
∥(1 : α)∥ * ∥(1 : α)∥ = ∥(1 : α) * (1 : α)∥ : by rw normed_field.norm_mul
... = ∥(1 : α)∥ * 1 : by simp,
eq_of_mul_eq_mul_left (ne_of_gt ((norm_pos_iff _).2 (by simp))) this
@[simp] lemma norm_div {α : Type*} [normed_field α] (a b : α) : ∥a/b∥ = ∥a∥/∥b∥ :=
if hb : b = 0 then by simp [hb] else
begin
apply eq_div_of_mul_eq,
{ apply ne_of_gt, apply (norm_pos_iff _).mpr hb },
{ rw [←normed_field.norm_mul, div_mul_cancel _ hb] }
end
@[simp] lemma norm_inv {α : Type*} [normed_field α] (a : α) : ∥a⁻¹∥ = ∥a∥⁻¹ :=
by simp only [inv_eq_one_div, norm_div, norm_one]
@[simp] lemma normed_field.norm_pow {α : Type*} [normed_field α] (a : α) :
∀ n : ℕ, ∥a^n∥ = ∥a∥^n
| 0 := by simp
| (k+1) := calc
∥a ^ (k + 1)∥ = ∥a*(a^k)∥ : rfl
... = ∥a∥*∥a^k∥ : by rw normed_field.norm_mul
... = ∥a∥ ^ (k + 1) : by rw normed_field.norm_pow; simp [pow, monoid.pow]
instance : normed_field ℝ :=
{ norm := λ x, abs x,
dist_eq := assume x y, rfl,
norm_mul := abs_mul }
lemma real.norm_eq_abs (r : ℝ): norm r = abs r := rfl
end normed_field
section normed_space
class normed_space (α : out_param $ Type*) (β : Type*) [out_param $ normed_field α]
extends normed_group β, vector_space α β :=
(norm_smul : ∀ (a:α) b, norm (a • b) = has_norm.norm a * norm b)
variables [normed_field α]
instance normed_field.to_normed_space : normed_space α α :=
{ dist_eq := normed_field.dist_eq,
norm_smul := normed_field.norm_mul }
lemma norm_smul [normed_space α β] (s : α) (x : β) : ∥s • x∥ = ∥s∥ * ∥x∥ :=
normed_space.norm_smul s x
lemma nnnorm_smul [normed_space α β] (s : α) (x : β) : nnnorm (s • x) = nnnorm s * nnnorm x :=
nnreal.eq $ norm_smul s x
variables {E : Type*} {F : Type*} [normed_space α E] [normed_space α F]
lemma tendsto_smul {f : γ → α} { g : γ → F} {e : filter γ} {s : α} {b : F} :
(tendsto f e (nhds s)) → (tendsto g e (nhds b)) → tendsto (λ x, (f x) • (g x)) e (nhds (s • b)) :=
begin
intros limf limg,
rw tendsto_iff_norm_tendsto_zero,
have ineq := λ x : γ, calc
∥f x • g x - s • b∥ = ∥(f x • g x - s • g x) + (s • g x - s • b)∥ : by simp[add_assoc]
... ≤ ∥f x • g x - s • g x∥ + ∥s • g x - s • b∥ : norm_triangle (f x • g x - s • g x) (s • g x - s • b)
... ≤ ∥f x - s∥*∥g x∥ + ∥s∥*∥g x - b∥ : by { rw [←smul_sub, ←sub_smul, norm_smul, norm_smul] },
apply squeeze_zero,
{ intro t, exact norm_nonneg _ },
{ exact ineq },
{ clear ineq,
have limf': tendsto (λ x, ∥f x - s∥) e (nhds 0) := tendsto_iff_norm_tendsto_zero.1 limf,
have limg' : tendsto (λ x, ∥g x∥) e (nhds ∥b∥) := filter.tendsto.comp limg (continuous_iff_continuous_at.1 continuous_norm _),
have lim1 := tendsto_mul limf' limg',
simp only [zero_mul, sub_eq_add_neg] at lim1,
have limg3 := tendsto_iff_norm_tendsto_zero.1 limg,
have lim2 := tendsto_mul (tendsto_const_nhds : tendsto _ _ (nhds ∥ s ∥)) limg3,
simp only [sub_eq_add_neg, mul_zero] at lim2,
rw [show (0:ℝ) = 0 + 0, by simp],
exact tendsto_add lim1 lim2 }
end
lemma continuous_smul [topological_space γ] {f : γ → α} {g : γ → E}
(hf : continuous f) (hg : continuous g) : continuous (λc, f c • g c) :=
continuous_iff_continuous_at.2 $ assume c,
tendsto_smul (continuous_iff_continuous_at.1 hf _) (continuous_iff_continuous_at.1 hg _)
instance : normed_space α (E × F) :=
{ norm_smul :=
begin
intros s x,
cases x with x₁ x₂,
change max (∥s • x₁∥) (∥s • x₂∥) = ∥s∥ * max (∥x₁∥) (∥x₂∥),
rw [norm_smul, norm_smul, ← mul_max_of_nonneg _ _ (norm_nonneg _)]
end,
add_smul := λ r x y, prod.ext (add_smul _ _ _) (add_smul _ _ _),
smul_add := λ r x y, prod.ext (smul_add _ _ _) (smul_add _ _ _),
..prod.normed_group,
..prod.vector_space }
instance fintype.normed_space {ι : Type*} {E : ι → Type*} [fintype ι] [∀i, normed_space α (E i)] :
normed_space α (Πi, E i) :=
{ norm := λf, ((finset.univ.sup (λb, nnnorm (f b)) : nnreal) : ℝ),
dist_eq :=
assume f g, congr_arg coe $ congr_arg (finset.sup finset.univ) $
by funext i; exact nndist_eq_nnnorm _ _,
norm_smul := λ a f,
show (↑(finset.sup finset.univ (λ (b : ι), nnnorm (a • f b))) : ℝ) =
nnnorm a * ↑(finset.sup finset.univ (λ (b : ι), nnnorm (f b))),
by simp only [(nnreal.coe_mul _ _).symm, nnreal.mul_finset_sup, nnnorm_smul],
..metric_space_pi,
..pi.vector_space α }
/-- A normed space can be build from a norm that satisfies algebraic properties. This is formalised in this structure. -/
structure normed_space.core (α : Type*) (β : Type*)
[out_param $ discrete_field α] [normed_field α] [add_comm_group β] [has_scalar α β] [has_norm β]:=
(norm_eq_zero_iff : ∀ x : β, ∥x∥ = 0 ↔ x = 0)
(norm_smul : ∀ c : α, ∀ x : β, ∥c • x∥ = ∥c∥ * ∥x∥)
(triangle : ∀ x y : β, ∥x + y∥ ≤ ∥x∥ + ∥y∥)
noncomputable def normed_space.of_core (α : Type*) (β : Type*)
[normed_field α] [add_comm_group β] [vector_space α β] [has_norm β] (C : normed_space.core α β) : normed_space α β :=
{ dist := λ x y, ∥x - y∥,
dist_eq := assume x y, by refl,
dist_self := assume x, (C.norm_eq_zero_iff (x - x)).mpr (show x - x = 0, by simp),
eq_of_dist_eq_zero := assume x y h, show (x = y), from sub_eq_zero.mp $ (C.norm_eq_zero_iff (x - y)).mp h,
dist_triangle := assume x y z,
calc ∥x - z∥ = ∥x - y + (y - z)∥ : by simp
... ≤ ∥x - y∥ + ∥y - z∥ : C.triangle _ _,
dist_comm := assume x y,
calc ∥x - y∥ = ∥ -(1 : α) • (y - x)∥ : by simp
... = ∥y - x∥ : begin rw[C.norm_smul], simp end,
norm_smul := C.norm_smul
}
end normed_space
|
c3e1057200d5a60b1cb8c501d450531e03a26b1b | 9be442d9ec2fcf442516ed6e9e1660aa9071b7bd | /src/Lean/Meta/Eqns.lean | 238f3074a1cfc3fb3601d2da52ca4fd14f991b65 | [
"Apache-2.0",
"LLVM-exception",
"NCSA",
"LGPL-3.0-only",
"LicenseRef-scancode-inner-net-2.0",
"BSD-3-Clause",
"LGPL-2.0-or-later",
"Spencer-94",
"LGPL-2.1-or-later",
"HPND",
"LicenseRef-scancode-pcre",
"ISC",
"LGPL-2.1-only",
"LicenseRef-scancode-other-permissive",
"SunPro",
"CMU-Mach"... | permissive | EdAyers/lean4 | 57ac632d6b0789cb91fab2170e8c9e40441221bd | 37ba0df5841bde51dbc2329da81ac23d4f6a4de4 | refs/heads/master | 1,676,463,245,298 | 1,660,619,433,000 | 1,660,619,433,000 | 183,433,437 | 1 | 0 | Apache-2.0 | 1,657,612,672,000 | 1,556,196,574,000 | Lean | UTF-8 | Lean | false | false | 5,248 | lean | /-
Copyright (c) 2021 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Meta.Basic
import Lean.Meta.AppBuilder
namespace Lean.Meta
def GetEqnsFn := Name → MetaM (Option (Array Name))
private builtin_initialize getEqnsFnsRef : IO.Ref (List GetEqnsFn) ← IO.mkRef []
/--
Register a new function for retrieving equation theorems.
We generate equations theorems on demand, and they are generated by more than one module.
For example, the structural and well-founded recursion modules generate them.
Most recent getters are tried first.
A getter returns an `Option (Array Name)`. The result is `none` if the getter failed.
Otherwise, it is a sequence of theorem names where each one of them corresponds to
an alternative. Example: the definition
```
def f (xs : List Nat) : List Nat :=
match xs with
| [] => []
| x::xs => (x+1)::f xs
```
should have two equational theorems associated with it
```
f [] = []
```
and
```
(x : Nat) → (xs : List Nat) → f (x :: xs) = (x+1) :: f xs
```
-/
def registerGetEqnsFn (f : GetEqnsFn) : IO Unit := do
unless (← initializing) do
throw (IO.userError "failed to register equation getter, this kind of extension can only be registered during initialization")
getEqnsFnsRef.modify (f :: ·)
/-- Return true iff `declName` is a definition and its type is not a proposition. -/
private def shouldGenerateEqnThms (declName : Name) : MetaM Bool := do
if let some (.defnInfo info) := (← getEnv).find? declName then
return !(← isProp info.type)
else
return false
structure EqnsExtState where
map : Std.PHashMap Name (Array Name) := {}
deriving Inhabited
/- We generate the equations on demand, and do not save them on .olean files. -/
builtin_initialize eqnsExt : EnvExtension EqnsExtState ←
registerEnvExtension (pure {})
/--
Simple equation theorem for nonrecursive definitions.
-/
private def mkSimpleEqThm (declName : Name) : MetaM (Option Name) := do
if let some (.defnInfo info) := (← getEnv).find? declName then
lambdaTelescope info.value fun xs body => do
let lhs := mkAppN (mkConst info.name <| info.levelParams.map mkLevelParam) xs
let type ← mkForallFVars xs (← mkEq lhs body)
let value ← mkLambdaFVars xs (← mkEqRefl lhs)
let name := mkPrivateName (← getEnv) declName ++ `_eq_1
addDecl <| Declaration.thmDecl {
name, type, value
levelParams := info.levelParams
}
return some name
else
return none
/--
Return equation theorems for the given declaration.
By default, we not create equation theorems for nonrecursive definitions.
You can use `nonRec := true` to override this behavior, a dummy `rfl` proof is created on the fly.
-/
def getEqnsFor? (declName : Name) (nonRec := false) : MetaM (Option (Array Name)) := withLCtx {} {} do
if let some eqs := eqnsExt.getState (← getEnv) |>.map.find? declName then
return some eqs
else if (← shouldGenerateEqnThms declName) then
for f in (← getEqnsFnsRef.get) do
if let some r ← f declName then
modifyEnv fun env => eqnsExt.modifyState env fun s => { s with map := s.map.insert declName r }
return some r
if nonRec then
let some eqThm ← mkSimpleEqThm declName | return none
let r := #[eqThm]
modifyEnv fun env => eqnsExt.modifyState env fun s => { s with map := s.map.insert declName r }
return some r
return none
def GetUnfoldEqnFn := Name → MetaM (Option Name)
private builtin_initialize getUnfoldEqnFnsRef : IO.Ref (List GetUnfoldEqnFn) ← IO.mkRef []
/--
Register a new function for retrieving a "unfold" equation theorem.
We generate this kind of equation theorem on demand, and it is generated by more than one module.
For example, the structural and well-founded recursion modules generate it.
Most recent getters are tried first.
A getter returns an `Option Name`. The result is `none` if the getter failed.
Otherwise, it is a theorem name. Example: the definition
```
def f (xs : List Nat) : List Nat :=
match xs with
| [] => []
| x::xs => (x+1)::f xs
```
should have the theorem
```
(xs : Nat) →
f xs =
match xs with
| [] => []
| x::xs => (x+1)::f xs
```
-/
def registerGetUnfoldEqnFn (f : GetUnfoldEqnFn) : IO Unit := do
unless (← initializing) do
throw (IO.userError "failed to register equation getter, this kind of extension can only be registered during initialization")
getUnfoldEqnFnsRef.modify (f :: ·)
/--
Return a "unfold" theorem for the given declaration.
By default, we not create unfold theorems for nonrecursive definitions.
You can use `nonRec := true` to override this behavior.
-/
def getUnfoldEqnFor? (declName : Name) (nonRec := false) : MetaM (Option Name) := withLCtx {} {} do
if (← shouldGenerateEqnThms declName) then
for f in (← getUnfoldEqnFnsRef.get) do
if let some r ← f declName then
return some r
if nonRec then
let some #[eqThm] ← getEqnsFor? declName (nonRec := true) | return none
return some eqThm
return none
end Lean.Meta
|
476defb6122f4c48aeabb8a39752d578ad94352f | 2c096fdfecf64e46ea7bc6ce5521f142b5926864 | /tests/lean/interactive/hover.lean | d7e10d5929b7d512d20e6c54a66d7cfee5e895ca | [
"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 | Kha/lean4 | 1005785d2c8797ae266a303968848e5f6ce2fe87 | b99e11346948023cd6c29d248cd8f3e3fb3474cf | refs/heads/master | 1,693,355,498,027 | 1,669,080,461,000 | 1,669,113,138,000 | 184,748,176 | 0 | 0 | Apache-2.0 | 1,665,995,520,000 | 1,556,884,930,000 | Lean | UTF-8 | Lean | false | false | 5,770 | lean | import Lean
example : True := by
apply True.intro
--^ textDocument/hover
example : True := by
simp [True.intro]
--^ textDocument/hover
example (n : Nat) : True := by
match n with
| Nat.zero => _
--^ textDocument/hover
| n + 1 => _
/-- My tactic -/
macro "mytac" o:"only"? e:term : tactic => `(tactic| exact $e)
example : True := by
mytac only True.intro
--^ textDocument/hover
--^ textDocument/hover
--^ textDocument/hover
/-- My way better tactic -/
macro_rules
| `(tactic| mytac $[only]? $e:term) =>
--^ textDocument/hover
--^ textDocument/hover
`(tactic| apply $e:term)
--^ textDocument/hover
--^ textDocument/hover
example : True := by
mytac only True.intro
--^ textDocument/hover
/-- My ultimate tactic -/
elab_rules : tactic
| `(tactic| mytac $[only]? $e) => do Lean.Elab.Tactic.evalTactic (← `(tactic| refine $e))
example : True := by
mytac only True.intro
--^ textDocument/hover
/-- My notation -/
macro (name := myNota) "mynota" e:term : term => pure e
--^ textDocument/hover
#check mynota 1
--^ textDocument/hover
/-- My way better notation -/
macro_rules
| `(mynota $e) => `(2 * $e)
#check mynota 1
--^ textDocument/hover
-- macro_rules take precedence over elab_rules for term/command, so use new syntax
syntax "mynota'" term : term
/-- My ultimate notation -/
elab_rules : term
| `(mynota' $e) => `($e * $e) >>= (Lean.Elab.Term.elabTerm · none)
#check mynota' 1
--^ textDocument/hover
@[inherit_doc]
infix:65 (name := myInfix) " >+< " => Nat.add
--^ textDocument/hover
--^ textDocument/hover
#check 1 >+< 2
--^ textDocument/hover
@[inherit_doc] notation "ℕ" => Nat
#check ℕ
--^ textDocument/hover
/-- My command -/
macro "mycmd" e:term : command => do
let seq ← `(Lean.Parser.Term.doSeq| $e:term)
--^ textDocument/hover
`(def hi := Id.run do $seq:doSeq)
--^ textDocument/hover
mycmd 1
--^ textDocument/hover
/-- My way better command -/
macro_rules
| `(mycmd $e) => `(@[inline] def hi := $e)
mycmd 1
--^ textDocument/hover
syntax "mycmd'" ppSpace sepBy1(term, " + ") : command
--^ textDocument/hover
--^ textDocument/hover
--^ textDocument/hover
/-- My ultimate command -/
elab_rules : command
| `(mycmd' $e) => do Lean.Elab.Command.elabCommand (← `(/-- hi -/ @[inline] def hi := $e))
mycmd' 1
--^ textDocument/hover
#check ({ a := }) -- should not show `sorry`
--^ textDocument/hover
example : True := by
simp [id True.intro]
--^ textDocument/hover
--^ textDocument/hover
example : Id Nat := do
let mut n := 1
n := 2
--^ textDocument/hover
n
opaque foo : Nat
#check _root_.foo
--^ textDocument/hover
namespace Bar
opaque foo : Nat
--^ textDocument/hover
#check _root_.foo
--^ textDocument/hover
def bar := 1
--^ textDocument/hover
structure Foo := mk ::
--^ textDocument/hover
--^ textDocument/hover
hi : Nat
--^ textDocument/hover
inductive Bar
--^ textDocument/hover
| mk : Bar
--^ textDocument/hover
instance : ToString Nat := ⟨toString⟩
--^ textDocument/hover
instance f : ToString Nat := ⟨toString⟩
--^ textDocument/hover
example : Type 0 := Nat
--^ textDocument/hover
def foo.bar : Nat := 1
--^ textDocument/hover
--^ textDocument/hover
example : Nat → Nat → Nat :=
fun x y =>
--^ textDocument/hover
--v textDocument/definition
x
--^ textDocument/hover
-- textDocument/definition -- removed because the result is platform-dependent
set_option linter.unusedVariables false in
--^ textDocument/hover
example : Nat → Nat → Nat := by
intro x y
--^ textDocument/hover
--v textDocument/definition
exact x
--^ textDocument/hover
def g (n : Nat) : Nat := g 0
termination_by g n => n
decreasing_by have n' := n; admit
--^ textDocument/hover
@[inline]
--^ textDocument/hover
def one := 1
example : True ∧ False := by
constructor
· constructor
--^ textDocument/hover
example : Nat := Id.run do (← 1)
--^ textDocument/hover
#check (· + ·)
--^ textDocument/hover
--^ textDocument/hover
macro "my_intro" x:(ident <|> "_") : tactic =>
match x with
| `($x:ident) => `(tactic| intro $x:ident)
| _ => `(tactic| intro _%$x)
example : α → α := by intro x; assumption
--^ textDocument/hover
example : α → α := by intro _; assumption
--^ textDocument/hover
example : α → α := by my_intro x; assumption
--^ textDocument/hover
example : α → α := by my_intro _; assumption
--^ textDocument/hover
example : Nat → True := by
intro x
--^ textDocument/hover
cases x with
| zero => trivial
--^ textDocument/hover
--v textDocument/hover
| succ x => trivial
--^ textDocument/hover
example : Nat → True := by
intro x
--^ textDocument/hover
induction x with
--^ textDocument/hover
| zero => trivial
--^ textDocument/hover
--v textDocument/hover
| succ _ ih => exact ih
--^ textDocument/hover
example : Nat → Nat
--v textDocument/hover
| .zero => .zero
--^ textDocument/hover
--v textDocument/hover
| .succ x => .succ x
--^ textDocument/hover
example : Inhabited Nat := ⟨Nat.zero⟩
--^ textDocument/hover
--^ textDocument/hover
|
7ac55ec6fe03a3ef9801a13cfccd9ed1c768b417 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/playground/simp_binders.lean | bf77039b2e22093808889047223d2ed7c1b9f054 | [
"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 | 767 | lean | axiom let_in {A B} (x : A) (f : A -> B) : B
axiom add (x y : Nat) : Nat
@[simp] axiom addZero (x : Nat) : add x 0 = x
syntax "bigAdd0Seq! " num term:max : term
syntax "bigAddSeq! " num term:max : term
macro_rules
| `(bigAdd0Seq! $n $acc) =>
let n := n.toNat
if n == 0 then
`(add $acc (add $acc 0))
else
`(let_in (add $acc (add $acc 0)) (fun acc' => bigAdd0Seq! $(Lean.quote (n - 1)) acc'))
macro_rules
| `(bigAddSeq! $n $acc) =>
let n := n.toNat
if n == 0 then
`(add $acc $acc)
else
`(let_in (add $acc $acc) (fun acc' => bigAddSeq! $(Lean.quote (n - 1)) acc'))
theorem ex1 (x : Nat) : bigAdd0Seq! 150 x = bigAddSeq! 150 x := by
simp
-- theorem ex2 (x : Nat) : bigAddSeq! 5 x = bigAddSeq! 5 x := by
-- simp
|
91c0d583c6be56352d307d4495d5677ff9476cc8 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /src/Lean/Parser/Level.lean | 3f05122c4647bcce54771dd04ea17faa9d656bc3 | [
"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,094 | lean | /-
Copyright (c) 2019 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura, Sebastian Ullrich
-/
import Lean.Parser.Extra
namespace Lean
namespace Parser
builtin_initialize
registerBuiltinParserAttribute `builtin_level_parser ``Category.level
@[inline] def levelParser (rbp : Nat := 0) : Parser :=
categoryParser `level rbp
namespace Level
@[builtin_level_parser] def paren := leading_parser
"(" >> withoutPosition levelParser >> ")"
@[builtin_level_parser] def max := leading_parser
nonReservedSymbol "max" true >> many1 (ppSpace >> levelParser maxPrec)
@[builtin_level_parser] def imax := leading_parser
nonReservedSymbol "imax" true >> many1 (ppSpace >> levelParser maxPrec)
@[builtin_level_parser] def hole := leading_parser
"_"
@[builtin_level_parser] def num :=
checkPrec maxPrec >> numLit
@[builtin_level_parser] def ident :=
checkPrec maxPrec >> Parser.ident
@[builtin_level_parser] def addLit := trailing_parser:65
" + " >> numLit
end Level
end Parser
end Lean
|
affbf38750b122c1368fd5370e6329a185127df2 | 75db7e3219bba2fbf41bf5b905f34fcb3c6ca3f2 | /hott/types/equiv.hlean | 60d48e3ae7a71dd4bb919b05e8130c8e4de52902 | [
"Apache-2.0"
] | permissive | jroesch/lean | 30ef0860fa905d35b9ad6f76de1a4f65c9af6871 | 3de4ec1a6ce9a960feb2a48eeea8b53246fa34f2 | refs/heads/master | 1,586,090,835,348 | 1,455,142,203,000 | 1,455,142,277,000 | 51,536,958 | 1 | 0 | null | 1,455,215,811,000 | 1,455,215,811,000 | null | UTF-8 | Lean | false | false | 8,387 | hlean | /-
Copyright (c) 2014 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Floris van Doorn
Ported from Coq HoTT
Theorems about the types equiv and is_equiv
-/
import .fiber .arrow arity ..hprop_trunc
open eq is_trunc sigma sigma.ops pi fiber function equiv equiv.ops
namespace is_equiv
variables {A B : Type} (f : A → B) [H : is_equiv f]
include H
/- is_equiv f is a mere proposition -/
definition is_contr_fiber_of_is_equiv [instance] (b : B) : is_contr (fiber f b) :=
is_contr.mk
(fiber.mk (f⁻¹ b) (right_inv f b))
(λz, fiber.rec_on z (λa p,
fiber_eq ((ap f⁻¹ p)⁻¹ ⬝ left_inv f a) (calc
right_inv f b = (ap (f ∘ f⁻¹) p)⁻¹ ⬝ ((ap (f ∘ f⁻¹) p) ⬝ right_inv f b)
: by rewrite inv_con_cancel_left
... = (ap (f ∘ f⁻¹) p)⁻¹ ⬝ (right_inv f (f a) ⬝ p) : by rewrite ap_con_eq_con
... = (ap (f ∘ f⁻¹) p)⁻¹ ⬝ (ap f (left_inv f a) ⬝ p) : by rewrite [adj f]
... = (ap (f ∘ f⁻¹) p)⁻¹ ⬝ ap f (left_inv f a) ⬝ p : by rewrite con.assoc
... = (ap f (ap f⁻¹ p))⁻¹ ⬝ ap f (left_inv f a) ⬝ p : by rewrite ap_compose
... = ap f (ap f⁻¹ p)⁻¹ ⬝ ap f (left_inv f a) ⬝ p : by rewrite ap_inv
... = ap f ((ap f⁻¹ p)⁻¹ ⬝ left_inv f a) ⬝ p : by rewrite ap_con)))
definition is_contr_right_inverse : is_contr (Σ(g : B → A), f ∘ g ~ id) :=
begin
fapply is_trunc_equiv_closed,
{apply sigma_equiv_sigma_id, intro g, apply eq_equiv_homotopy},
fapply is_trunc_equiv_closed,
{apply fiber.sigma_char},
fapply is_contr_fiber_of_is_equiv,
apply (to_is_equiv (arrow_equiv_arrow_right B (equiv.mk f H))),
end
definition is_contr_right_coherence (u : Σ(g : B → A), f ∘ g ~ id)
: is_contr (Σ(η : u.1 ∘ f ~ id), Π(a : A), u.2 (f a) = ap f (η a)) :=
begin
fapply is_trunc_equiv_closed,
{apply equiv.symm, apply sigma_pi_equiv_pi_sigma},
fapply is_trunc_equiv_closed,
{apply pi_equiv_pi_id, intro a,
apply (fiber_eq_equiv (fiber.mk (u.1 (f a)) (u.2 (f a))) (fiber.mk a idp))},
end
omit H
protected definition sigma_char : (is_equiv f) ≃
(Σ(g : B → A) (ε : f ∘ g ~ id) (η : g ∘ f ~ id), Π(a : A), ε (f a) = ap f (η a)) :=
equiv.MK (λH, ⟨inv f, right_inv f, left_inv f, adj f⟩)
(λp, is_equiv.mk f p.1 p.2.1 p.2.2.1 p.2.2.2)
(λp, begin
induction p with p1 p2,
induction p2 with p21 p22,
induction p22 with p221 p222,
reflexivity
end)
(λH, by induction H; reflexivity)
protected definition sigma_char' : (is_equiv f) ≃
(Σ(u : Σ(g : B → A), f ∘ g ~ id), Σ(η : u.1 ∘ f ~ id), Π(a : A), u.2 (f a) = ap f (η a)) :=
calc
(is_equiv f) ≃
(Σ(g : B → A) (ε : f ∘ g ~ id) (η : g ∘ f ~ id), Π(a : A), ε (f a) = ap f (η a))
: is_equiv.sigma_char
... ≃ (Σ(u : Σ(g : B → A), f ∘ g ~ id), Σ(η : u.1 ∘ f ~ id), Π(a : A), u.2 (f a) = ap f (η a))
: {sigma_assoc_equiv (λu, Σ(η : u.1 ∘ f ~ id), Π(a : A), u.2 (f a) = ap f (η a))}
local attribute is_contr_right_inverse [instance] [priority 1600]
local attribute is_contr_right_coherence [instance] [priority 1600]
theorem is_hprop_is_equiv [instance] : is_hprop (is_equiv f) :=
is_hprop_of_imp_is_contr
(λ(H : is_equiv f), is_trunc_equiv_closed -2 (equiv.symm !is_equiv.sigma_char'))
definition inv_eq_inv {A B : Type} {f f' : A → B} {Hf : is_equiv f} {Hf' : is_equiv f'}
(p : f = f') : f⁻¹ = f'⁻¹ :=
apd011 inv p !is_hprop.elim
/- contractible fibers -/
definition is_contr_fun_of_is_equiv [H : is_equiv f] : is_contr_fun f :=
is_contr_fiber_of_is_equiv f
definition is_hprop_is_contr_fun (f : A → B) : is_hprop (is_contr_fun f) := _
definition is_equiv_of_is_contr_fun [H : is_contr_fun f] : is_equiv f :=
adjointify _ (λb, point (center (fiber f b)))
(λb, point_eq (center (fiber f b)))
(λa, ap point (center_eq (fiber.mk a idp)))
definition is_equiv_of_imp_is_equiv (H : B → is_equiv f) : is_equiv f :=
@is_equiv_of_is_contr_fun _ _ f (λb, @is_contr_fiber_of_is_equiv _ _ _ (H b) _)
definition is_equiv_equiv_is_contr_fun : is_equiv f ≃ is_contr_fun f :=
equiv_of_is_hprop _ (λH, !is_equiv_of_is_contr_fun)
end is_equiv
namespace is_equiv
/- Theorem 4.7.7 -/
variables {A : Type} {P Q : A → Type}
variable (f : Πa, P a → Q a)
definition is_fiberwise_equiv [reducible] := Πa, is_equiv (f a)
definition is_equiv_total_of_is_fiberwise_equiv [H : is_fiberwise_equiv f] : is_equiv (total f) :=
is_equiv_sigma_functor id f
definition is_fiberwise_equiv_of_is_equiv_total [H : is_equiv (total f)]
: is_fiberwise_equiv f :=
begin
intro a,
apply is_equiv_of_is_contr_fun, intro q,
apply @is_contr_equiv_closed _ _ (fiber_total_equiv f q)
end
end is_equiv
namespace equiv
open is_equiv
variables {A B C : Type}
definition equiv_mk_eq {f f' : A → B} [H : is_equiv f] [H' : is_equiv f'] (p : f = f')
: equiv.mk f H = equiv.mk f' H' :=
apd011 equiv.mk p !is_hprop.elim
definition equiv_eq {f f' : A ≃ B} (p : to_fun f = to_fun f') : f = f' :=
by (cases f; cases f'; apply (equiv_mk_eq p))
definition equiv_eq' {f f' : A ≃ B} (p : to_fun f ~ to_fun f') : f = f' :=
by apply equiv_eq;apply eq_of_homotopy p
definition trans_symm (f : A ≃ B) (g : B ≃ C) : (f ⬝e g)⁻¹ᵉ = g⁻¹ᵉ ⬝e f⁻¹ᵉ :> (C ≃ A) :=
equiv_eq idp
definition symm_symm (f : A ≃ B) : f⁻¹ᵉ⁻¹ᵉ = f :> (A ≃ B) :=
equiv_eq idp
protected definition equiv.sigma_char [constructor]
(A B : Type) : (A ≃ B) ≃ Σ(f : A → B), is_equiv f :=
begin
fapply equiv.MK,
{intro F, exact ⟨to_fun F, to_is_equiv F⟩},
{intro p, cases p with f H, exact (equiv.mk f H)},
{intro p, cases p, exact idp},
{intro F, cases F, exact idp},
end
definition equiv_eq_char (f f' : A ≃ B) : (f = f') ≃ (to_fun f = to_fun f') :=
calc
(f = f') ≃ (to_fun !equiv.sigma_char f = to_fun !equiv.sigma_char f')
: eq_equiv_fn_eq (to_fun !equiv.sigma_char)
... ≃ ((to_fun !equiv.sigma_char f).1 = (to_fun !equiv.sigma_char f').1 ) : equiv_subtype
... ≃ (to_fun f = to_fun f') : equiv.refl
definition is_equiv_ap_to_fun (f f' : A ≃ B)
: is_equiv (ap to_fun : f = f' → to_fun f = to_fun f') :=
begin
fapply adjointify,
{intro p, cases f with f H, cases f' with f' H', cases p, apply ap (mk f'), apply is_hprop.elim},
{intro p, cases f with f H, cases f' with f' H', cases p,
apply @concat _ _ (ap to_fun (ap (equiv.mk f') (is_hprop.elim H H'))), {apply idp},
generalize is_hprop.elim H H', intro q, cases q, apply idp},
{intro p, cases p, cases f with f H, apply ap (ap (equiv.mk f)), apply is_hset.elim}
end
definition equiv_pathover {A : Type} {a a' : A} (p : a = a')
{B : A → Type} {C : A → Type} (f : B a ≃ C a) (g : B a' ≃ C a')
(r : Π(b : B a) (b' : B a') (q : b =[p] b'), f b =[p] g b') : f =[p] g :=
begin
fapply pathover_of_fn_pathover_fn,
{ intro a, apply equiv.sigma_char},
{ fapply sigma_pathover,
esimp, apply arrow_pathover, exact r,
apply is_hprop.elimo}
end
definition is_contr_equiv (A B : Type) [HA : is_contr A] [HB : is_contr B] : is_contr (A ≃ B) :=
begin
apply @is_contr_of_inhabited_hprop, apply is_hprop.mk,
intro x y, cases x with fx Hx, cases y with fy Hy, generalize Hy,
apply (eq_of_homotopy (λ a, !eq_of_is_contr)) ▸ (λ Hy, !is_hprop.elim ▸ rfl),
apply equiv_of_is_contr_of_is_contr
end
definition is_trunc_succ_equiv (n : trunc_index) (A B : Type)
[HA : is_trunc n.+1 A] [HB : is_trunc n.+1 B] : is_trunc n.+1 (A ≃ B) :=
@is_trunc_equiv_closed _ _ n.+1 (equiv.symm !equiv.sigma_char)
(@is_trunc_sigma _ _ _ _ (λ f, !is_trunc_succ_of_is_hprop))
definition is_trunc_equiv (n : trunc_index) (A B : Type)
[HA : is_trunc n A] [HB : is_trunc n B] : is_trunc n (A ≃ B) :=
by cases n; apply !is_contr_equiv; apply !is_trunc_succ_equiv
end equiv
|
e1be557f6d97c78b2c75a6c211af25defad9c74a | 827a8a5c2041b1d7f55e128581f583dfbd65ecf6 | /moebius.hlean | f0c771eb59e7a4837b5318933b3a0d6db3152417 | [
"Apache-2.0"
] | permissive | fpvandoorn/leansnippets | 6af0499f6f3fd2c07e4b580734d77b67574e7c27 | 601bafbe07e9534af76f60994d6bdf741996ef93 | refs/heads/master | 1,590,063,910,882 | 1,545,093,878,000 | 1,545,093,878,000 | 36,044,957 | 2 | 2 | null | 1,442,619,708,000 | 1,432,256,875,000 | Lean | UTF-8 | Lean | false | false | 4,346 | hlean | import homotopy.circle
open eq bool equiv circle sigma pi
namespace bool
-- definition elim2_type_seg1_pathover (Pb1 Pb2 : Type) (Ps1 Ps2 : Pb1 ≃ Pb2) (x : Pb1) :
-- pathover (elim2_type Pb1 Pb2 Ps1 Ps2) x seg1 (Ps1 x) :=
-- pathover_of_tr_eq (apd10 (elim2_type_seg1 Pb1 Pb2 Ps1 Ps2) x)
-- definition elim2_type_seg2_pathover (Pb1 Pb2 : Type) (Ps1 Ps2 : Pb1 ≃ Pb2) (x : Pb1) :
-- pathover (elim2_type Pb1 Pb2 Ps1 Ps2) x seg2 (Ps2 x) :=
-- pathover_of_tr_eq (apd10 (elim2_type_seg2 Pb1 Pb2 Ps1 Ps2) x)
definition pathover_tr_pathover_idp_of_eq2 {A : Type} {B : A → Type} {a a' : A} {b : B a} {b' : B a'}
{p : a = a'} (q : p ▸ b = b') :
pathover_tr p b ⬝o pathover_idp_of_eq q = pathover_of_tr_eq q :=
begin induction q; induction p; reflexivity end
definition tr_eq_of_pathover_pathover_tr {A : Type} {B : A → Type} {a a₂ : A} (p : a = a₂)
(b : B a) : tr_eq_of_pathover (pathover_tr p b) = idp :=
by induction p; reflexivity
definition M [unfold 1] : S¹ → Type₀ :=
circle.elim_type bool equiv_bnot
definition circle_of_M [unfold 1] (x : Σx, M x) : S¹ :=
begin
induction x with x m,
induction x,
{ exact cond m base2 base1 /- tt ↦ base2, ff ↦ base1 -/ },
{ apply arrow_pathover_constant_right, intro b,
refine _ ⬝ (ap (λx, cond x _ _) (elim_type_loop _ _ b))⁻¹,
induction b, exact seg2, exact seg1⁻¹
}
end
definition M_of_circle [unfold 1] (x : S¹) : Σx, M x :=
begin
induction x using circle.elim2,
{ exact ⟨base, ff⟩ },
{ exact ⟨base, tt⟩ },
{ exact (dpair_eq_dpair loop (elim_type_loop_pathover _ _ tt))⁻¹ },
{ exact dpair_eq_dpair loop (elim_type_loop_pathover _ _ ff) },
end
definition M_of_circle_of_M_base [unfold 1] (b : M base) : M_of_circle (circle_of_M ⟨base, b⟩) = ⟨base, b⟩ :=
by induction b; reflexivity; reflexivity
definition circle_equiv_M : S¹ ≃ Σx, M x :=
equiv.MK M_of_circle circle_of_M
begin
intro x, induction x with x m,
induction x,
{ exact M_of_circle_of_M_base m },
{ apply pi_pathover_left, intro b,
apply eq_pathover_id_right,
refine ap_compose M_of_circle _ _ ⬝
ap02 _ (!ap_dpair_eq_dpair ⬝ !apd011_eq_apo11_apd ⬝
ap (λx, apo11_constant_right x _) !rec_loop ⬝ !apo11_arrow_pathover_constant_right) ⬝ph _,
esimp,
refine !ap_con ⬝ (!ap_con ⬝ idp ◾ (!ap_inv ⬝ !ap_compose'⁻²))
◾ (!ap_compose' ⬝ ap02 _ !tr_eq_of_pathover_pathover_tr) ⬝ph _,
note sq := (natural_square M_of_circle_of_M_base (elim_type_loop _ _ b))⁻¹ᵛ,
esimp at sq, esimp,
refine (_ ⬝v sq) ⬝hp !con_inv_cancel_right, clear sq,
induction b,
{ apply hdeg_square, refine !elim2_seg2 ⬝ _⁻¹,
refine idp ◾ !ap_dpair ⬝ !dpair_eq_dpair_con⁻¹ ⬝ ap (dpair_eq_dpair loop) _,
rexact pathover_tr_pathover_idp_of_eq2 (elim_type_loop bool equiv_bnot ff) },
{ apply hdeg_square, refine !ap_inv ⬝ !elim2_seg1⁻² ⬝ !inv_inv ⬝ _⁻¹,
refine idp ◾ !ap_dpair ⬝ !dpair_eq_dpair_con⁻¹ ⬝ ap (dpair_eq_dpair loop) _,
rexact pathover_tr_pathover_idp_of_eq2 (elim_type_loop bool equiv_bnot tt) }}
end
begin
intro x, induction x using circle.rec2,
{ reflexivity },
{ reflexivity },
{ apply eq_pathover_id_right, apply hdeg_square, refine ap_compose circle_of_M _ _ ⬝ _,
refine ap02 _ !elim2_seg1 ⬝ !ap_inv ⬝ _⁻² ⬝ !inv_inv,
refine !ap_dpair_eq_dpair ⬝ !apd011_eq_apo11_apd ⬝ ap (λx, apo11_constant_right x _) !rec_loop ⬝
!apo11_arrow_pathover_constant_right ⬝ _,
esimp,
refine idp ◾ idp ◾ ap02 _ _ ⬝ !inv_con_cancel_right,
exact to_right_inv (pathover_equiv_tr_eq _ _ _) _ },
{ apply eq_pathover_id_right, apply hdeg_square, refine ap_compose circle_of_M _ _ ⬝ _,
refine ap02 _ !elim2_seg2 ⬝ _,
refine !ap_dpair_eq_dpair ⬝ !apd011_eq_apo11_apd ⬝
ap (λx, apo11_constant_right x _) !rec_loop ⬝ !apo11_arrow_pathover_constant_right ⬝ _,
esimp,
refine idp ◾ idp ◾ ap02 _ _ ⬝ !inv_con_cancel_right, exact to_right_inv (pathover_equiv_tr_eq _ _ _) _ }
end
end bool
|
e1c0d825147858dcb8df8808f84eda154864aaef | d1a52c3f208fa42c41df8278c3d280f075eb020c | /stage0/src/Lean/Elab/Tactic/Conv/Congr.lean | 67268ba377c616864d7da06f9884db74c2466d22 | [
"Apache-2.0",
"LLVM-exception",
"NCSA",
"LGPL-3.0-only",
"LicenseRef-scancode-inner-net-2.0",
"BSD-3-Clause",
"LGPL-2.0-or-later",
"Spencer-94",
"LGPL-2.1-or-later",
"HPND",
"LicenseRef-scancode-pcre",
"ISC",
"LGPL-2.1-only",
"LicenseRef-scancode-other-permissive",
"SunPro",
"CMU-Mach"... | permissive | cipher1024/lean4 | 6e1f98bb58e7a92b28f5364eb38a14c8d0aae393 | 69114d3b50806264ef35b57394391c3e738a9822 | refs/heads/master | 1,642,227,983,603 | 1,642,011,696,000 | 1,642,011,696,000 | 228,607,691 | 0 | 0 | Apache-2.0 | 1,576,584,269,000 | 1,576,584,268,000 | null | UTF-8 | Lean | false | false | 4,859 | lean | /-
Copyright (c) 2021 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Meta.Tactic.Simp.Main
import Lean.Elab.Tactic.Conv.Basic
namespace Lean.Elab.Tactic.Conv
open Meta
private def congrApp (mvarId : MVarId) (lhs rhs : Expr) : MetaM (List MVarId) :=
-- TODO: add support for `[congr]` lemmas
lhs.withApp fun f args => do
let infos := (← getFunInfoNArgs f args.size).paramInfo
let mut r := { expr := f : Simp.Result }
let mut newGoals := #[]
let mut i := 0
for arg in args do
let addGoal ←
if i < infos.size && !infos[i].hasFwdDeps then
pure infos[i].binderInfo.isExplicit
else
pure (← whnfD (← inferType r.expr)).isArrow
if addGoal then
let (rhs, newGoal) ← mkConvGoalFor arg
newGoals := newGoals.push newGoal.mvarId!
r ← Simp.mkCongr r { expr := rhs, proof? := newGoal }
else
r ← Simp.mkCongrFun r arg
i := i + 1
let proof ← r.getProof
unless (← isDefEqGuarded rhs r.expr) do
throwError "invalid 'congr' conv tactic, failed to resolve{indentExpr rhs}\n=?={indentExpr r.expr}"
assignExprMVar mvarId proof
return newGoals.toList
private def congrImplies (mvarId : MVarId) : MetaM (List MVarId) := do
let [mvarId₁, mvarId₂, _, _] ← apply mvarId (← mkConstWithFreshMVarLevels ``implies_congr) | throwError "'apply implies_congr' unexpected result"
let mvarId₁ ← markAsConvGoal mvarId₁
let mvarId₂ ← markAsConvGoal mvarId₂
return [mvarId₁, mvarId₂]
-- TODO: move?
def isImplies (e : Expr) : MetaM Bool :=
if e.isArrow then
isProp e.bindingDomain! <&&> isProp e.bindingBody!
else
return false
def congr (mvarId : MVarId) : MetaM (List MVarId) :=
withMVarContext mvarId do
let (lhs, rhs) ← getLhsRhsCore mvarId
let lhs ← instantiateMVars lhs
trace[Meta.debug] "{lhs} = {rhs}"
if (← isImplies lhs) then
congrImplies mvarId
else if lhs.isApp then
congrApp mvarId lhs rhs
else
throwError "invalid 'congr' conv tactic, application or implication expected{indentExpr lhs}"
@[builtinTactic Lean.Parser.Tactic.Conv.congr] def evalCongr : Tactic := fun stx => do
replaceMainGoal (← congr (← getMainGoal))
private def selectIdx (tacticName : String) (mvarIds : List MVarId) (i : Int) : TacticM Unit := do
if i >= 0 then
let i := i.toNat
if h : i < mvarIds.length then
for mvarId in mvarIds, j in [:mvarIds.length] do
if i != j then
applyRefl mvarId
replaceMainGoal [mvarIds.get i h]
return ()
throwError "invalid '{tacticName}' conv tactic, application has only {mvarIds.length} (nondependent) argument(s)"
@[builtinTactic Lean.Parser.Tactic.Conv.lhs] def evalLhs : Tactic := fun stx => do
let mvarIds ← congr (← getMainGoal)
selectIdx "lhs" mvarIds ((mvarIds.length : Int) - 2)
@[builtinTactic Lean.Parser.Tactic.Conv.rhs] def evalRhs : Tactic := fun stx => do
let mvarIds ← congr (← getMainGoal)
selectIdx "rhs" mvarIds ((mvarIds.length : Int) - 1)
@[builtinTactic Lean.Parser.Tactic.Conv.arg] def evalArg : Tactic := fun stx => do
match stx with
| `(conv| arg $i) =>
let i := i.isNatLit?.getD 0
if i == 0 then
throwError "invalid 'arg' conv tactic, index must be greater than 0"
let i := i - 1
let mvarIds ← congr (← getMainGoal)
selectIdx "arg" mvarIds i
| _ => throwUnsupportedSyntax
private def extCore (mvarId : MVarId) (userName? : Option Name) : MetaM MVarId :=
withMVarContext mvarId do
let userNames := if let some userName := userName? then [userName] else []
let (lhs, rhs) ← getLhsRhsCore mvarId
let lhs ← instantiateMVars lhs
if lhs.isForall then
let [mvarId, _] ← apply mvarId (← mkConstWithFreshMVarLevels ``forall_congr) | throwError "'apply forall_congr' unexpected result"
let (_, mvarId) ← introN mvarId 1 userNames
markAsConvGoal mvarId
else
let lhsType ← whnfD (← inferType lhs)
unless lhsType.isForall do
throwError "invalid 'ext' conv tactic, function or arrow expected{indentD m!"{lhs} : {lhsType}"}"
let [mvarId] ← apply mvarId (← mkConstWithFreshMVarLevels ``funext) | throwError "'apply funext' unexpected result"
let (_, mvarId) ← introN mvarId 1 userNames
markAsConvGoal mvarId
private def ext (userName? : Option Name) : TacticM Unit := do
replaceMainGoal [← extCore (← getMainGoal) userName?]
@[builtinTactic Lean.Parser.Tactic.Conv.ext] def evalExt : Tactic := fun stx => do
let ids := stx[1].getArgs
if ids.isEmpty then
ext none
else
for id in ids do
withRef id <| ext id.getId
end Lean.Elab.Tactic.Conv
|
1450c8aef0445a7e43edd63dfbc282b6283f504a | d6124c8dbe5661dcc5b8c9da0a56fbf1f0480ad6 | /Papyrus/IR/ArgumentRef.lean | acaa91bc2af6247562d5b000d11dc396685d9d87 | [
"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 | 566 | lean | import Papyrus.Context
import Papyrus.IR.ValueRef
namespace Papyrus
/--
A reference to an external LLVM
[Argument](https://llvm.org/doxygen/classllvm_1_1Argument.html).
-/
structure ArgumentRef extends ValueRef where
is_argument : toValueRef.valueKind = ValueKind.argument
instance : Coe ArgumentRef ValueRef := ⟨(·.toValueRef)⟩
namespace ArgumentRef
/-- Cast a general `ValueRef` to a `ArgumentRef` given proof it is one. -/
def cast (val : ValueRef) (h : val.valueKind = ValueKind.argument) : ArgumentRef :=
{toValueRef := val, is_argument := h}
|
b1db347845e3eb5979b793c32b7de3f8fdbcf6c7 | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/data/list/alist.lean | ba275f2017909b01723e6ca6c4ad65fb143ab2c2 | [
"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 | 11,223 | lean | /-
Copyright (c) 2018 Sean Leather. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sean Leather, Mario Carneiro
-/
import data.list.sigma
/-!
# Association lists
-/
universes u v w
open list
variables {α : Type u} {β : α → Type v}
/-- `alist β` is a key-value map stored as a `list` (i.e. a linked list).
It is a wrapper around certain `list` functions with the added constraint
that the list have unique keys. -/
structure alist (β : α → Type v) : Type (max u v) :=
(entries : list (sigma β))
(nodupkeys : entries.nodupkeys)
/-- Given `l : list (sigma β)`, create a term of type `alist β` by removing
entries with duplicate keys. -/
def list.to_alist [decidable_eq α] {β : α → Type v} (l : list (sigma β)) : alist β :=
{ entries := _,
nodupkeys := nodupkeys_erase_dupkeys l }
namespace alist
@[ext] theorem ext : ∀ {s t : alist β}, s.entries = t.entries → s = t
| ⟨l₁, h₁⟩ ⟨l₂, h₂⟩ H := by congr'
lemma ext_iff {s t : alist β} : s = t ↔ s.entries = t.entries :=
⟨congr_arg _, ext⟩
instance [decidable_eq α] [∀ a, decidable_eq (β a)] : decidable_eq (alist β) :=
λ xs ys, by rw ext_iff; apply_instance
/-! ### keys -/
/-- The list of keys of an association list. -/
def keys (s : alist β) : list α := s.entries.keys
theorem keys_nodup (s : alist β) : s.keys.nodup := s.nodupkeys
/-! ### mem -/
/-- The predicate `a ∈ s` means that `s` has a value associated to the key `a`. -/
instance : has_mem α (alist β) := ⟨λ a s, a ∈ s.keys⟩
theorem mem_keys {a : α} {s : alist β} : a ∈ s ↔ a ∈ s.keys := iff.rfl
theorem mem_of_perm {a : α} {s₁ s₂ : alist β} (p : s₁.entries ~ s₂.entries) : a ∈ s₁ ↔ a ∈ s₂ :=
(p.map sigma.fst).mem_iff
/-! ### empty -/
/-- The empty association list. -/
instance : has_emptyc (alist β) := ⟨⟨[], nodupkeys_nil⟩⟩
instance : inhabited (alist β) := ⟨∅⟩
theorem not_mem_empty (a : α) : a ∉ (∅ : alist β) :=
not_mem_nil a
@[simp] theorem empty_entries : (∅ : alist β).entries = [] := rfl
@[simp] theorem keys_empty : (∅ : alist β).keys = [] := rfl
/-! ### singleton -/
/-- The singleton association list. -/
def singleton (a : α) (b : β a) : alist β :=
⟨[⟨a, b⟩], nodupkeys_singleton _⟩
@[simp] theorem singleton_entries (a : α) (b : β a) :
(singleton a b).entries = [sigma.mk a b] := rfl
@[simp] theorem keys_singleton (a : α) (b : β a) : (singleton a b).keys = [a] := rfl
/-! ### lookup -/
section
variables [decidable_eq α]
/-- Look up the value associated to a key in an association list. -/
def lookup (a : α) (s : alist β) : option (β a) :=
s.entries.lookup a
@[simp] theorem lookup_empty (a) : lookup a (∅ : alist β) = none :=
rfl
theorem lookup_is_some {a : α} {s : alist β} :
(s.lookup a).is_some ↔ a ∈ s := lookup_is_some
theorem lookup_eq_none {a : α} {s : alist β} :
lookup a s = none ↔ a ∉ s :=
lookup_eq_none
theorem perm_lookup {a : α} {s₁ s₂ : alist β} (p : s₁.entries ~ s₂.entries) :
s₁.lookup a = s₂.lookup a :=
perm_lookup _ s₁.nodupkeys s₂.nodupkeys p
instance (a : α) (s : alist β) : decidable (a ∈ s) :=
decidable_of_iff _ lookup_is_some
/-! ### replace -/
/-- Replace a key with a given value in an association list.
If the key is not present it does nothing. -/
def replace (a : α) (b : β a) (s : alist β) : alist β :=
⟨kreplace a b s.entries, (kreplace_nodupkeys a b).2 s.nodupkeys⟩
@[simp] theorem keys_replace (a : α) (b : β a) (s : alist β) :
(replace a b s).keys = s.keys :=
keys_kreplace _ _ _
@[simp] theorem mem_replace {a a' : α} {b : β a} {s : alist β} :
a' ∈ replace a b s ↔ a' ∈ s :=
by rw [mem_keys, keys_replace, ←mem_keys]
theorem perm_replace {a : α} {b : β a} {s₁ s₂ : alist β} :
s₁.entries ~ s₂.entries → (replace a b s₁).entries ~ (replace a b s₂).entries :=
perm.kreplace s₁.nodupkeys
end
/-- Fold a function over the key-value pairs in the map. -/
def foldl {δ : Type w} (f : δ → Π a, β a → δ) (d : δ) (m : alist β) : δ :=
m.entries.foldl (λ r a, f r a.1 a.2) d
/-! ### erase -/
section
variables [decidable_eq α]
/-- Erase a key from the map. If the key is not present, do nothing. -/
def erase (a : α) (s : alist β) : alist β :=
⟨kerase a s.entries, kerase_nodupkeys _ s.nodupkeys⟩
@[simp] theorem keys_erase (a : α) (s : alist β) :
(erase a s).keys = s.keys.erase a :=
by simp only [erase, keys, keys_kerase]
@[simp] theorem mem_erase {a a' : α} {s : alist β} : a' ∈ erase a s ↔ a' ≠ a ∧ a' ∈ s :=
by rw [mem_keys, keys_erase, mem_erase_iff_of_nodup s.keys_nodup, ←mem_keys]
theorem perm_erase {a : α} {s₁ s₂ : alist β} :
s₁.entries ~ s₂.entries → (erase a s₁).entries ~ (erase a s₂).entries :=
perm.kerase s₁.nodupkeys
@[simp] theorem lookup_erase (a) (s : alist β) : lookup a (erase a s) = none :=
lookup_kerase a s.nodupkeys
@[simp] theorem lookup_erase_ne {a a'} {s : alist β} (h : a ≠ a') :
lookup a (erase a' s) = lookup a s :=
lookup_kerase_ne h
theorem erase_erase (a a' : α) (s : alist β) :
(s.erase a).erase a' = (s.erase a').erase a :=
ext $ kerase_kerase
/-! ### insert -/
/-- Insert a key-value pair into an association list and erase any existing pair
with the same key. -/
def insert (a : α) (b : β a) (s : alist β) : alist β :=
⟨kinsert a b s.entries, kinsert_nodupkeys a b s.nodupkeys⟩
@[simp] theorem insert_entries {a} {b : β a} {s : alist β} :
(insert a b s).entries = sigma.mk a b :: kerase a s.entries :=
rfl
theorem insert_entries_of_neg {a} {b : β a} {s : alist β} (h : a ∉ s) :
(insert a b s).entries = ⟨a, b⟩ :: s.entries :=
by rw [insert_entries, kerase_of_not_mem_keys h]
@[simp] theorem mem_insert {a a'} {b' : β a'} (s : alist β) :
a ∈ insert a' b' s ↔ a = a' ∨ a ∈ s :=
mem_keys_kinsert
@[simp] theorem keys_insert {a} {b : β a} (s : alist β) :
(insert a b s).keys = a :: s.keys.erase a :=
by simp [insert, keys, keys_kerase]
theorem perm_insert {a} {b : β a} {s₁ s₂ : alist β} (p : s₁.entries ~ s₂.entries) :
(insert a b s₁).entries ~ (insert a b s₂).entries :=
by simp only [insert_entries]; exact p.kinsert s₁.nodupkeys
@[simp] theorem lookup_insert {a} {b : β a} (s : alist β) : lookup a (insert a b s) = some b :=
by simp only [lookup, insert, lookup_kinsert]
@[simp] theorem lookup_insert_ne {a a'} {b' : β a'} {s : alist β} (h : a ≠ a') :
lookup a (insert a' b' s) = lookup a s :=
lookup_kinsert_ne h
@[simp] theorem lookup_to_alist {a} (s : list (sigma β)) : lookup a s.to_alist = s.lookup a :=
by rw [list.to_alist,lookup,lookup_erase_dupkeys]
@[simp] theorem insert_insert {a} {b b' : β a} (s : alist β) :
(s.insert a b).insert a b' = s.insert a b' :=
by ext : 1; simp only [alist.insert_entries, list.kerase_cons_eq];
constructor_matching* [_ ∧ _]; refl
theorem insert_insert_of_ne {a a'} {b : β a} {b' : β a'} (s : alist β) (h : a ≠ a') :
((s.insert a b).insert a' b').entries ~ ((s.insert a' b').insert a b).entries :=
by simp only [insert_entries]; rw [kerase_cons_ne,kerase_cons_ne,kerase_comm];
[apply perm.swap, exact h, exact h.symm]
@[simp] lemma insert_singleton_eq {a : α} {b b' : β a} :
insert a b (singleton a b') = singleton a b :=
ext $ by simp only [alist.insert_entries, list.kerase_cons_eq, and_self, alist.singleton_entries,
heq_iff_eq, eq_self_iff_true]
@[simp] theorem entries_to_alist (xs : list (sigma β)) :
(list.to_alist xs).entries = erase_dupkeys xs := rfl
theorem to_alist_cons (a : α) (b : β a) (xs : list (sigma β)) :
list.to_alist (⟨a,b⟩ :: xs) = insert a b xs.to_alist := rfl
/-! ### extract -/
/-- Erase a key from the map, and return the corresponding value, if found. -/
def extract (a : α) (s : alist β) : option (β a) × alist β :=
have (kextract a s.entries).2.nodupkeys,
by rw [kextract_eq_lookup_kerase]; exact kerase_nodupkeys _ s.nodupkeys,
match kextract a s.entries, this with
| (b, l), h := (b, ⟨l, h⟩)
end
@[simp] theorem extract_eq_lookup_erase (a : α) (s : alist β) :
extract a s = (lookup a s, erase a s) :=
by simp [extract]; split; refl
/-! ### union -/
/-- `s₁ ∪ s₂` is the key-based union of two association lists. It is
left-biased: if there exists an `a ∈ s₁`, `lookup a (s₁ ∪ s₂) = lookup a s₁`.
-/
def union (s₁ s₂ : alist β) : alist β :=
⟨kunion s₁.entries s₂.entries, kunion_nodupkeys s₁.nodupkeys s₂.nodupkeys⟩
instance : has_union (alist β) := ⟨union⟩
@[simp] theorem union_entries {s₁ s₂ : alist β} :
(s₁ ∪ s₂).entries = kunion s₁.entries s₂.entries :=
rfl
@[simp] theorem empty_union {s : alist β} : (∅ : alist β) ∪ s = s :=
ext rfl
@[simp] theorem union_empty {s : alist β} : s ∪ (∅ : alist β) = s :=
ext $ by simp
@[simp] theorem mem_union {a} {s₁ s₂ : alist β} :
a ∈ s₁ ∪ s₂ ↔ a ∈ s₁ ∨ a ∈ s₂ :=
mem_keys_kunion
theorem perm_union {s₁ s₂ s₃ s₄ : alist β}
(p₁₂ : s₁.entries ~ s₂.entries) (p₃₄ : s₃.entries ~ s₄.entries) :
(s₁ ∪ s₃).entries ~ (s₂ ∪ s₄).entries :=
by simp [p₁₂.kunion s₃.nodupkeys p₃₄]
theorem union_erase (a : α) (s₁ s₂ : alist β) : erase a (s₁ ∪ s₂) = erase a s₁ ∪ erase a s₂ :=
ext kunion_kerase.symm
@[simp] theorem lookup_union_left {a} {s₁ s₂ : alist β} :
a ∈ s₁ → lookup a (s₁ ∪ s₂) = lookup a s₁ :=
lookup_kunion_left
@[simp] theorem lookup_union_right {a} {s₁ s₂ : alist β} :
a ∉ s₁ → lookup a (s₁ ∪ s₂) = lookup a s₂ :=
lookup_kunion_right
@[simp] theorem mem_lookup_union {a} {b : β a} {s₁ s₂ : alist β} :
b ∈ lookup a (s₁ ∪ s₂) ↔ b ∈ lookup a s₁ ∨ a ∉ s₁ ∧ b ∈ lookup a s₂ :=
mem_lookup_kunion
theorem mem_lookup_union_middle {a} {b : β a} {s₁ s₂ s₃ : alist β} :
b ∈ lookup a (s₁ ∪ s₃) → a ∉ s₂ → b ∈ lookup a (s₁ ∪ s₂ ∪ s₃) :=
mem_lookup_kunion_middle
theorem insert_union {a} {b : β a} {s₁ s₂ : alist β} :
insert a b (s₁ ∪ s₂) = insert a b s₁ ∪ s₂ :=
by ext; simp
theorem union_assoc {s₁ s₂ s₃ : alist β} : ((s₁ ∪ s₂) ∪ s₃).entries ~ (s₁ ∪ (s₂ ∪ s₃)).entries :=
lookup_ext (alist.nodupkeys _) (alist.nodupkeys _)
(by simp [decidable.not_or_iff_and_not,or_assoc,and_or_distrib_left,and_assoc])
end
/-! ### disjoint -/
/-- Two associative lists are disjoint if they have no common keys. -/
def disjoint (s₁ s₂ : alist β) : Prop :=
∀ k ∈ s₁.keys, ¬ k ∈ s₂.keys
variables [decidable_eq α]
theorem union_comm_of_disjoint {s₁ s₂ : alist β} (h : disjoint s₁ s₂) :
(s₁ ∪ s₂).entries ~ (s₂ ∪ s₁).entries :=
lookup_ext (alist.nodupkeys _) (alist.nodupkeys _)
(begin
intros, simp,
split; intro h',
cases h',
{ right, refine ⟨_,h'⟩,
apply h, rw [keys,← list.lookup_is_some,h'], exact rfl },
{ left, rw h'.2 },
cases h',
{ right, refine ⟨_,h'⟩, intro h'',
apply h _ h'', rw [keys,← list.lookup_is_some,h'], exact rfl },
{ left, rw h'.2 },
end)
end alist
|
e0dea87192986c26328daacf39460484e4652775 | 94637389e03c919023691dcd05bd4411b1034aa5 | /src/inClassNotes/equallity/equality.lean | 60ed31ae7a7e5841d69615c949c479aefda58fe8 | [] | no_license | kevinsullivan/complogic-s21 | 7c4eef2105abad899e46502270d9829d913e8afc | 99039501b770248c8ceb39890be5dfe129dc1082 | refs/heads/master | 1,682,985,669,944 | 1,621,126,241,000 | 1,621,126,241,000 | 335,706,272 | 0 | 38 | null | 1,618,325,669,000 | 1,612,374,118,000 | Lean | UTF-8 | Lean | false | false | 2,994 | lean | /-
Properties and Relations: Type Families
-/
#check @eq
-- Π {α : Sort u_1}, α → α → Prop
#check eq 1 0 -- 1 = 0
#check eq 1 1 -- 1 = 1
#check 1 = 1
/-
Introduction: construct a term
-/
#check @eq.refl
-- ∀ {α : Sort u_1} (a : α), a = a
example : eq 1 0 := _
example : eq 1 1 := eq.refl 1
/-
Elimination: use a term
-/
#check @eq.subst
-- ∀ {α : Sort u_1} {P : α → Prop} {a b : α}, a = b → P a → P b
axioms -- some assumptions we can work with
(Person : Type)
(Kevin Robert : Person)
(livesInCville : Person → Prop)
(klic : livesInCville Kevin)
(keqr : Kevin = Robert)
-- write proof term
example : livesInCville Robert :=
eq.subst keqr klic
-- use rw tactic
example : livesInCville Robert :=
begin
apply eq.subst,
exact keqr,
exact klic,
end
universe u
theorem eq_is_symm : ∀ {α : Sort u} (a b : α), a = b → b = a :=
begin
assume α a b,
assume h,
rw h,
end
theorem eq_is_symm' : ∀ {α : Sort u} (a b : α), a = b → b = a :=
λ α a b h, eq.subst h (eq.refl a)
#check @eq.symm
-- tactic script
theorem eq_is_trans :
∀ {α : Sort u} (a b c : α), a = b → b = c → a = c :=
begin
intros α a b c ab bc,
rw ab,
assumption,
end
-- HW: prove eq_is_trans by writing a proof term explicitly
#check @equivalence
-- Π {β : Sort u_1}, (β → β → Prop) → Prop
/-
section relation
variables {α : Sort u} {β : Sort v} (r : β → β → Prop)
local infix `≺`:50 := r
def reflexive := ∀ x, x ≺ x
def symmetric := ∀ ⦃x y⦄, x ≺ y → y ≺ x
def transitive := ∀ ⦃x y z⦄, x ≺ y → y ≺ z → x ≺ z
def equivalence := reflexive r ∧ symmetric r ∧ transitive r
def total := ∀ x y, x ≺ y ∨ y ≺ x -- not the "total" we've used for functions
def mk_equivalence (rfl : reflexive r) (symm : symmetric r) (trans : transitive r) : equivalence r :=
⟨rfl, symm, trans⟩
def irreflexive := ∀ x, ¬ x ≺ x
def anti_symmetric := ∀ ⦃x y⦄, x ≺ y → y ≺ x → x = y
def empty_relation := λ a₁ a₂ : α, false
def subrelation (q r : β → β → Prop) := ∀ ⦃x y⦄, q x y → r x y
def inv_image (f : α → β) : α → α → Prop :=
λ a₁ a₂, f a₁ ≺ f a₂
lemma inv_image.trans (f : α → β) (h : transitive r) : transitive (inv_image r f) :=
λ (a₁ a₂ a₃ : α) (h₁ : inv_image r f a₁ a₂) (h₂ : inv_image r f a₂ a₃), h h₁ h₂
lemma inv_image.irreflexive (f : α → β) (h : irreflexive r) : irreflexive (inv_image r f) :=
λ (a : α) (h₁ : inv_image r f a a), h (f a) h₁
inductive tc {α : Sort u} (r : α → α → Prop) : α → α → Prop
| base : ∀ a b, r a b → tc a b
| trans : ∀ a b c, tc a b → tc b c → tc a c
end relation
-/
theorem eq_is_equiv : ∀ (α : Sort u), equivalence (@eq α) :=
begin
assume α,
unfold equivalence,
apply and.intro _ _,
exact eq.refl,
apply and.intro _ _,
unfold symmetric,
intros x y xy,
rw xy,
unfold transitive,
_
end |
cead756a1a95b156549e37fcb31ee80319487645 | 274748215b6d042f0d9c9a505f9551fa8e0c5f38 | /src/for_mathlib/algebraically_closed.lean | ebb7d0b7723552694a831340e70c761ca4afb09e | [
"Apache-2.0"
] | permissive | ImperialCollegeLondon/M4P33 | 878ecb515c77d20cc799ff1ebd78f1bf4fd65c12 | 1a179372db71ad6802d11eacbc1f02f327d55f8f | refs/heads/master | 1,607,519,867,193 | 1,583,344,297,000 | 1,583,344,297,000 | 233,316,107 | 59 | 4 | Apache-2.0 | 1,579,285,778,000 | 1,578,788,367,000 | Lean | UTF-8 | Lean | false | false | 872 | lean | import data.polynomial
import analysis.complex.polynomial -- just a sanity check, probably shouldn't be imported
open polynomial
class algebraically_closed_field (k : Type*) extends discrete_field k :=
(exists_root' : ∀ (f : polynomial k) (hf : 0 < degree f),
∃ z : k, is_root f z)
namespace algebraically_closed_field
variables {k : Type*} [algebraically_closed_field k]
def exists_root {f : polynomial k} (hf : 0 < degree f) := ∃ z : k, is_root f z
-- sanity check
noncomputable example : algebraically_closed_field ℂ :=
{exists_root' := λ f hf, complex.exists_root hf,
..complex.discrete_field}
end algebraically_closed_field
def is_algebraically_closed (k : Type*) [discrete_field k] : Prop :=
∀ {f : polynomial k} (hf : 0 < degree f), ∃ z : k, is_root f z
-- sanity check
example : is_algebraically_closed ℂ := λ hf, complex.exists_root
|
272504b25e5b282a78777b877b2750e1bbbc05a9 | dc253be9829b840f15d96d986e0c13520b085033 | /algebra/spectral_sequence.hlean | d8cf03537e234898068f08a14528fe01dbb7874e | [
"Apache-2.0"
] | permissive | cmu-phil/Spectral | 4ce68e5c1ef2a812ffda5260e9f09f41b85ae0ea | 3b078f5f1de251637decf04bd3fc8aa01930a6b3 | refs/heads/master | 1,685,119,195,535 | 1,684,169,772,000 | 1,684,169,772,000 | 46,450,197 | 42 | 13 | null | 1,505,516,767,000 | 1,447,883,921,000 | Lean | UTF-8 | Lean | false | false | 8,677 | hlean | /- Spectral sequences
- basic properties of spectral sequences
- currently, we only construct an spectral sequence from an exact couple
-/
-- Author: Floris van Doorn
import .exact_couple
open algebra is_trunc left_module is_equiv equiv eq function nat sigma set_quotient group
left_module group int prod prod.ops
open exact_couple (Z2)
structure convergent_spectral_sequence.{u v w} {R : Ring} (E' : ℤ → ℤ → LeftModule.{u v} R)
(Dinf : ℤ → LeftModule.{u w} R) : Type.{max u (v+1) (w+1)} :=
(E : ℕ → graded_module.{u 0 v} R Z2)
(d : Π(r : ℕ), E r →gm E r)
(deg_d : ℕ → Z2)
(deg_d_eq0 : Π(r : ℕ), deg (d r) 0 = deg_d r)
(α : Π(r : ℕ) (x : Z2), E (r+1) x ≃lm graded_homology (d r) (d r) x)
(e : Π(x : Z2), E 0 x ≃lm E' x.1 x.2)
(s₀ : Z2 → ℕ)
(f : Π{r : ℕ} {x : Z2} (h : s₀ x ≤ r), E (s₀ x) x ≃lm E r x)
(lb : ℤ → ℤ)
(HDinf : Π(n : ℤ), is_built_from (Dinf n)
(λ(k : ℕ), (λx, E (s₀ x) x) (n - (k + lb n), k + lb n)))
/- todo: the current definition doesn't say that E (s₀ x) x is contractible for x.1 + x.2 = n and x.2 < lb n -/
definition convergent_spectral_sequence_g [reducible] (E' : ℤ → ℤ → AbGroup)
(Dinf : ℤ → AbGroup) : Type :=
convergent_spectral_sequence (λn s, LeftModule_int_of_AbGroup (E' n s))
(λn, LeftModule_int_of_AbGroup (Dinf n))
section exact_couple
open exact_couple exact_couple.exact_couple exact_couple.convergent_exact_couple
exact_couple.convergence_theorem exact_couple.derived_couple
definition convergent_spectral_sequence_of_exact_couple {R : Ring} {E' : ℤ → ℤ → LeftModule R}
{Dinf : ℤ → LeftModule R} (c : convergent_exact_couple E' Dinf)
(st_eq : Πn, (st c n).1 + (st c n).2 = n) (deg_i_eq : deg (i (X c)) 0 = (- 1, 1)) :
convergent_spectral_sequence E' Dinf :=
convergent_spectral_sequence.mk (λr, E (page (X c) r)) (λr, d (page (X c) r))
(deg_d c) (deg_d_eq0 c)
(λr ns, by reflexivity) (e c) (B3 (HH c)) (λr ns Hr, Einfstable (HH c) Hr idp)
(λn, (st c n).2)
begin
intro n,
refine is_built_from_isomorphism (f c n) _ (is_built_from_infpage (HH c) (st c n) (HB c n)),
intro p, apply isomorphism_of_eq, apply ap (λx, E (page (X c) (B3 (HH c) x)) x),
induction p with p IH,
{ exact !prod.eta⁻¹ ⬝ prod_eq (eq_sub_of_add_eq (ap (add _) !zero_add ⬝ st_eq n))
(zero_add (st c n).2)⁻¹ },
{ assert H1 : Π(a : ℤ), n - (p + a) - 1 = n - (succ p + a),
exact λa, !sub_add_eq_sub_sub⁻¹ ⬝ ap (sub n) (add_comm_middle p a 1 ⬝ proof idp qed),
assert H2 : Π(a : ℤ), p + a + 1 = succ p + a,
exact λa, add_comm_middle p a 1,
refine ap (deg (i (X c))) IH ⬝ !deg_eq ⬝ ap (add _) deg_i_eq ⬝ prod_eq !H1 !H2 }
end
end exact_couple
namespace spectral_sequence
open convergent_spectral_sequence
variables {R : Ring} {E' : ℤ → ℤ → LeftModule R} {Dinf : ℤ → LeftModule R}
(c : convergent_spectral_sequence E' Dinf)
-- (E : ℕ → graded_module.{u 0 v} R Z2)
-- (d : Π(r : ℕ), E r →gm E r)
-- (deg_d : ℕ → Z2)
-- (deg_d_eq0 : Π(r : ℕ), deg (d r) 0 = deg_d r)
-- (α : Π(r : ℕ) (x : Z2), E (r+1) x ≃lm graded_homology (d r) (d r) x)
-- (e : Π(x : Z2), E 0 x ≃lm E' x.1 x.2)
-- (s₀ : Z2 → ℕ)
-- (f : Π{r : ℕ} {x : Z2} (h : s₀ x ≤ r), E (s₀ x) x ≃lm E r x)
-- (lb : ℤ → ℤ)
-- (HDinf : Π(n : ℤ), is_built_from (Dinf n)
-- (λ(k : ℕ), (λx, E (s₀ x) x) (n - (k + lb n), k + lb n)))
definition Einf (x : Z2) : LeftModule R := E c (s₀ c x) x
definition is_contr_E_succ (r : ℕ) (x : Z2) (h : is_contr (E c r x)) : is_contr (E c (r+1) x) :=
is_contr_equiv_closed_rev (equiv_of_isomorphism (α c r x)) (is_contr_homology _ _ _)
definition deg_d_eq (r : ℕ) (x : Z2) : deg (d c r) x = x + deg_d c r :=
!deg_eq ⬝ ap (add _) !deg_d_eq0
definition deg_d_inv_eq (r : ℕ) (x : Z2) : (deg (d c r))⁻¹ᵉ x = x - deg_d c r :=
inv_eq_of_eq (!deg_d_eq ⬝ !neg_add_cancel_right)⁻¹
definition is_contr_E_of_le {r₁ r₂ : ℕ} (H : r₁ ≤ r₂) (x : Z2) (h : is_contr (E c r₁ x)) :
is_contr (E c r₂ x) :=
begin
induction H with r₂ H IH,
{ exact h },
{ apply is_contr_E_succ, exact IH }
end
definition is_contr_E (r : ℕ) (x : Z2) (h : is_contr (E' x.1 x.2)) : is_contr (E c r x) :=
is_contr_E_of_le c !zero_le x (is_contr_equiv_closed_rev (equiv_of_isomorphism (e c x)) h)
definition is_contr_Einf (x : Z2) (h : is_contr (E' x.1 x.2)) : is_contr (Einf c x) :=
is_contr_E c (s₀ c x) x h
definition E_isomorphism {r₁ r₂ : ℕ} {ns : Z2} (H : r₁ ≤ r₂)
(H1 : Π⦃r⦄, r₁ ≤ r → r < r₂ → is_contr (E c r (ns - deg_d c r)))
(H2 : Π⦃r⦄, r₁ ≤ r → r < r₂ → is_contr (E c r (ns + deg_d c r))) :
E c r₂ ns ≃lm E c r₁ ns :=
begin
assert H3 : Π⦃r⦄, r₁ ≤ r → r ≤ r₂ → E c r ns ≃lm E c r₁ ns,
{ intro r Hr₁ Hr₂,
induction Hr₁ with r Hr₁ IH, reflexivity,
let Hr₂' := le_of_succ_le Hr₂,
refine α c r ns ⬝lm homology_isomorphism _ _ _ _ ⬝lm IH Hr₂',
exact is_contr_equiv_closed (equiv_ap (E c r) !deg_d_inv_eq⁻¹) (H1 Hr₁ Hr₂),
exact is_contr_equiv_closed (equiv_ap (E c r) !deg_d_eq⁻¹) (H2 Hr₁ Hr₂) },
exact H3 H (le.refl _)
end
definition E_isomorphism0 {r : ℕ} {n s : ℤ}
(H1 : Πr', r' < r → is_contr (E' (n - (deg_d c r').1) (s - (deg_d c r').2)))
(H2 : Πr', r' < r → is_contr (E' (n + (deg_d c r').1) (s + (deg_d c r').2))) :
E c r (n, s) ≃lm E' n s :=
E_isomorphism c !zero_le (λr' Hr₁ Hr₂, is_contr_E c r' _ (H1 r' Hr₂))
(λr' Hr₁ Hr₂, is_contr_E c r' _ (H2 r' Hr₂)) ⬝lm
e c (n, s)
definition Einf_isomorphism (r₁ : ℕ) {ns : Z2}
(H1 : Π⦃r⦄, r₁ ≤ r → is_contr (E c r (ns - deg_d c r)))
(H2 : Π⦃r⦄, r₁ ≤ r → is_contr (E c r (ns + deg_d c r))) :
Einf c ns ≃lm E c r₁ ns :=
begin
cases le.total r₁ (s₀ c ns) with Hr Hr,
exact E_isomorphism c Hr (λr Hr₁ Hr₂, H1 Hr₁) (λr Hr₁ Hr₂, H2 Hr₁),
exact f c Hr
end
definition Einf_isomorphism0 {n s : ℤ}
(H1 : Πr, is_contr (E' (n - (deg_d c r).1) (s - (deg_d c r).2)))
(H2 : Πr, is_contr (E' (n + (deg_d c r).1) (s + (deg_d c r).2))) :
Einf c (n, s) ≃lm E' n s :=
E_isomorphism0 c (λr Hr, H1 r) (λr Hr, H2 r)
definition convergence_0 (n : ℤ) (H : Πm, lb c m = 0) :
is_built_from (Dinf n) (λ(k : ℕ), Einf c (n - k, k)) :=
is_built_from_isomorphism isomorphism.rfl
(λk, left_module.isomorphism_of_eq (ap (Einf c)
(prod_eq (ap (sub n) (ap (add _) !H ⬝ add_zero _)) (ap (add _) !H ⬝ add_zero _))))
(HDinf c n)
/- we call a spectral sequence normal if it is a first-quadrant spectral sequence and
the degree of d on page r (for r ≥ 2) is (r, -(r-1)).
The indexing is different, because we start counting pages at 2. -/
include c
structure is_normal : Type :=
(normal1 : Π{n} s, n < 0 → is_contr (E' n s))
(normal2 : Πn {s}, s < 0 → is_contr (E' n s))
(normal3 : Π(r : ℕ), deg_d c r = (r+2, -(r+1)))
open is_normal
variable {c}
variable (nc : is_normal c)
include nc
definition deg_d_normal_pr1 (r : ℕ) : (deg_d c r).1 = r+2 := ap pr1 (normal3 nc r)
definition deg_d_normal_pr2 (r : ℕ) : (deg_d c r).2 = -(r+1) := ap pr2 (normal3 nc r)
definition stable_range {n s : ℤ} {r : ℕ} (H1 : n < r + 2) (H2 : s < r + 1) :
Einf c (n, s) ≃lm E c r (n, s) :=
begin
fapply Einf_isomorphism,
{ intro r' Hr', apply is_contr_E, apply normal1 nc,
refine lt_of_le_of_lt (le_of_eq (ap (λx, n - x.1) (normal3 nc r'))) _,
apply sub_lt_left_of_lt_add,
refine lt_of_lt_of_le H1 (le.trans _ (le_of_eq !add_zero⁻¹)),
exact add_le_add_right (of_nat_le_of_nat_of_le Hr') 2 },
{ intro r' Hr', apply is_contr_E, apply normal2 nc,
refine lt_of_le_of_lt (le_of_eq (ap (λx, s + x.2) (normal3 nc r'))) _,
change s - (r' + 1) < 0,
apply sub_lt_left_of_lt_add,
refine lt_of_lt_of_le H2 (le.trans _ (le_of_eq !add_zero⁻¹)),
exact add_le_add_right (of_nat_le_of_nat_of_le Hr') 1 },
end
definition deg_d_normal_eq (r : ℕ) (x : Z2) : deg (d c r) x = x + (r+2, -(r+1)) :=
deg_d_eq c r x ⬝ ap (add x) (is_normal.normal3 nc r)
omit nc
end spectral_sequence
|
f677de7bd7075e9f1e56015fce9f7a75bdcc5a4f | 57aec6ee746bc7e3a3dd5e767e53bd95beb82f6d | /stage0/src/Lean/Elab.lean | 5e8a751bec2be2332f6c095d9b3389f898566cab | [
"Apache-2.0"
] | permissive | collares/lean4 | 861a9269c4592bce49b71059e232ff0bfe4594cc | 52a4f535d853a2c7c7eea5fee8a4fa04c682c1ee | refs/heads/master | 1,691,419,031,324 | 1,618,678,138,000 | 1,618,678,138,000 | 358,989,750 | 0 | 0 | Apache-2.0 | 1,618,696,333,000 | 1,618,696,333,000 | null | UTF-8 | Lean | false | false | 895 | 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.Elab.Import
import Lean.Elab.Exception
import Lean.Elab.Command
import Lean.Elab.Term
import Lean.Elab.App
import Lean.Elab.Binders
import Lean.Elab.LetRec
import Lean.Elab.Frontend
import Lean.Elab.BuiltinNotation
import Lean.Elab.Declaration
import Lean.Elab.Tactic
import Lean.Elab.Match
-- HACK: must come after `Match` because builtin elaborators (for `match` in this case) do not take priorities
import Lean.Elab.Quotation
import Lean.Elab.Syntax
import Lean.Elab.Do
import Lean.Elab.StructInst
import Lean.Elab.Inductive
import Lean.Elab.Structure
import Lean.Elab.Print
import Lean.Elab.MutualDef
import Lean.Elab.PreDefinition
import Lean.Elab.Deriving
import Lean.Elab.DeclarationRange
import Lean.Elab.Extra
|
93578d8e4d82619fc8b826b0e89c6a6417ec7cf0 | c09f5945267fd905e23a77be83d9a78580e04a4a | /src/topology/subset_properties.lean | c6cc5a3dab05a667fef8a20ee8ff4cb924694de2 | [
"Apache-2.0"
] | permissive | OHIHIYA20/mathlib | 023a6df35355b5b6eb931c404f7dd7535dccfa89 | 1ec0a1f49db97d45e8666a3bf33217ff79ca1d87 | refs/heads/master | 1,587,964,529,965 | 1,551,819,319,000 | 1,551,819,319,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 24,165 | 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
Properties of subsets of topological spaces: compact, clopen, irreducible,
connected, totally disconnected, totally separated.
-/
import topology.basic
open set filter lattice classical
local attribute [instance] prop_decidable
universes u v
variables {α : Type u} {β : Type v} [topological_space α]
/- compact sets -/
section compact
/-- A set `s` is compact if for every filter `f` that contains `s`,
every set of `f` also meets every neighborhood of some `a ∈ s`. -/
def compact (s : set α) := ∀f, f ≠ ⊥ → f ≤ principal s → ∃a∈s, f ⊓ nhds a ≠ ⊥
lemma compact_inter {s t : set α} (hs : compact s) (ht : is_closed t) : compact (s ∩ t) :=
assume f hnf hstf,
let ⟨a, hsa, (ha : f ⊓ nhds a ≠ ⊥)⟩ := hs f hnf (le_trans hstf (le_principal_iff.2 (inter_subset_left _ _))) in
have ∀a, principal t ⊓ nhds a ≠ ⊥ → a ∈ t,
by intro a; rw [inf_comm]; rw [is_closed_iff_nhds] at ht; exact ht a,
have a ∈ t,
from this a $ neq_bot_of_le_neq_bot ha $ inf_le_inf (le_trans hstf (le_principal_iff.2 (inter_subset_right _ _))) (le_refl _),
⟨a, ⟨hsa, this⟩, ha⟩
lemma compact_diff {s t : set α} (hs : compact s) (ht : is_open t) : compact (s \ t) :=
compact_inter hs (is_closed_compl_iff.mpr ht)
lemma compact_of_is_closed_subset {s t : set α}
(hs : compact s) (ht : is_closed t) (h : t ⊆ s) : compact t :=
by convert ← compact_inter hs ht; exact inter_eq_self_of_subset_right h
lemma compact_adherence_nhdset {s t : set α} {f : filter α}
(hs : compact s) (hf₂ : f ≤ principal s) (ht₁ : is_open t) (ht₂ : ∀a∈s, nhds a ⊓ f ≠ ⊥ → a ∈ t) :
t ∈ f.sets :=
classical.by_cases mem_sets_of_neq_bot $
assume : f ⊓ principal (- t) ≠ ⊥,
let ⟨a, ha, (hfa : f ⊓ principal (-t) ⊓ nhds a ≠ ⊥)⟩ := hs _ this $ inf_le_left_of_le hf₂ in
have a ∈ t,
from ht₂ a ha $ neq_bot_of_le_neq_bot hfa $ le_inf inf_le_right $ inf_le_left_of_le inf_le_left,
have nhds a ⊓ principal (-t) ≠ ⊥,
from neq_bot_of_le_neq_bot hfa $ le_inf inf_le_right $ inf_le_left_of_le inf_le_right,
have ∀s∈(nhds a ⊓ principal (-t)).sets, s ≠ ∅,
from forall_sets_neq_empty_iff_neq_bot.mpr this,
have false,
from this _ ⟨t, mem_nhds_sets ht₁ ‹a ∈ t›, -t, subset.refl _, subset.refl _⟩ (inter_compl_self _),
by contradiction
lemma compact_iff_ultrafilter_le_nhds {s : set α} :
compact s ↔ (∀f, is_ultrafilter f → f ≤ principal s → ∃a∈s, f ≤ nhds a) :=
⟨assume hs : compact s, assume f hf hfs,
let ⟨a, ha, h⟩ := hs _ hf.left hfs in
⟨a, ha, le_of_ultrafilter hf h⟩,
assume hs : (∀f, is_ultrafilter f → f ≤ principal s → ∃a∈s, f ≤ nhds a),
assume f hf hfs,
let ⟨a, ha, (h : ultrafilter_of f ≤ nhds a)⟩ :=
hs (ultrafilter_of f) (ultrafilter_ultrafilter_of hf) (le_trans ultrafilter_of_le hfs) in
have ultrafilter_of f ⊓ nhds a ≠ ⊥,
by simp only [inf_of_le_left, h]; exact (ultrafilter_ultrafilter_of hf).left,
⟨a, ha, neq_bot_of_le_neq_bot this (inf_le_inf ultrafilter_of_le (le_refl _))⟩⟩
lemma compact_elim_finite_subcover {s : set α} {c : set (set α)}
(hs : compact s) (hc₁ : ∀t∈c, is_open t) (hc₂ : s ⊆ ⋃₀ c) : ∃c'⊆c, finite c' ∧ s ⊆ ⋃₀ c' :=
classical.by_contradiction $ assume h,
have h : ∀{c'}, c' ⊆ c → finite c' → ¬ s ⊆ ⋃₀ c',
from assume c' h₁ h₂ h₃, h ⟨c', h₁, h₂, h₃⟩,
let
f : filter α := (⨅c':{c' : set (set α) // c' ⊆ c ∧ finite c'}, principal (s - ⋃₀ c')),
⟨a, ha⟩ := @exists_mem_of_ne_empty α s
(assume h', h (empty_subset _) finite_empty $ h'.symm ▸ empty_subset _)
in
have f ≠ ⊥, from infi_neq_bot_of_directed ⟨a⟩
(assume ⟨c₁, hc₁, hc'₁⟩ ⟨c₂, hc₂, hc'₂⟩, ⟨⟨c₁ ∪ c₂, union_subset hc₁ hc₂, finite_union hc'₁ hc'₂⟩,
principal_mono.mpr $ diff_subset_diff_right $ sUnion_mono $ subset_union_left _ _,
principal_mono.mpr $ diff_subset_diff_right $ sUnion_mono $ subset_union_right _ _⟩)
(assume ⟨c', hc'₁, hc'₂⟩, show principal (s \ _) ≠ ⊥, by simp only [ne.def, principal_eq_bot_iff, diff_eq_empty]; exact h hc'₁ hc'₂),
have f ≤ principal s, from infi_le_of_le ⟨∅, empty_subset _, finite_empty⟩ $
show principal (s \ ⋃₀∅) ≤ principal s, from le_principal_iff.2 (diff_subset _ _),
let
⟨a, ha, (h : f ⊓ nhds a ≠ ⊥)⟩ := hs f ‹f ≠ ⊥› this,
⟨t, ht₁, (ht₂ : a ∈ t)⟩ := hc₂ ha
in
have f ≤ principal (-t),
from infi_le_of_le ⟨{t}, by rwa singleton_subset_iff, finite_insert _ finite_empty⟩ $
principal_mono.mpr $
show s - ⋃₀{t} ⊆ - t, begin rw sUnion_singleton; exact assume x ⟨_, hnt⟩, hnt end,
have is_closed (- t), from is_open_compl_iff.mp $ by rw lattice.neg_neg; exact hc₁ t ht₁,
have a ∈ - t, from is_closed_iff_nhds.mp this _ $ neq_bot_of_le_neq_bot h $
le_inf inf_le_right (inf_le_left_of_le ‹f ≤ principal (- t)›),
this ‹a ∈ t›
lemma compact_elim_finite_subcover_image {s : set α} {b : set β} {c : β → set α}
(hs : compact s) (hc₁ : ∀i∈b, is_open (c i)) (hc₂ : s ⊆ ⋃i∈b, c i) :
∃b'⊆b, finite b' ∧ s ⊆ ⋃i∈b', c i :=
if h : b = ∅ then ⟨∅, empty_subset _, finite_empty, h ▸ hc₂⟩ else
let ⟨i, hi⟩ := exists_mem_of_ne_empty h in
have hc'₁ : ∀i∈c '' b, is_open i, from assume i ⟨j, hj, h⟩, h ▸ hc₁ _ hj,
have hc'₂ : s ⊆ ⋃₀ (c '' b), by rwa set.sUnion_image,
let ⟨d, hd₁, hd₂, hd₃⟩ := compact_elim_finite_subcover hs hc'₁ hc'₂ in
have ∀x : d, ∃i, i ∈ b ∧ c i = x, from assume ⟨x, hx⟩, hd₁ hx,
let ⟨f', hf⟩ := axiom_of_choice this,
f := λx:set α, (if h : x ∈ d then f' ⟨x, h⟩ else i : β) in
have ∀(x : α) (i : set α), i ∈ d → x ∈ i → (∃ (i : β), i ∈ f '' d ∧ x ∈ c i),
from assume x i hid hxi, ⟨f i, mem_image_of_mem f hid,
by simpa only [f, dif_pos hid, (hf ⟨_, hid⟩).2] using hxi⟩,
⟨f '' d,
assume i ⟨j, hj, h⟩,
h ▸ by simpa only [f, dif_pos hj] using (hf ⟨_, hj⟩).1,
finite_image f hd₂,
subset.trans hd₃ $ by simpa only [subset_def, mem_sUnion, exists_imp_distrib, mem_Union, exists_prop, and_imp]⟩
lemma compact_of_finite_subcover {s : set α}
(h : ∀c, (∀t∈c, is_open t) → s ⊆ ⋃₀ c → ∃c'⊆c, finite c' ∧ s ⊆ ⋃₀ c') : compact s :=
assume f hfn hfs, classical.by_contradiction $ assume : ¬ (∃x∈s, f ⊓ nhds x ≠ ⊥),
have hf : ∀x∈s, nhds x ⊓ f = ⊥,
by simpa only [not_exists, not_not, inf_comm],
have ¬ ∃x∈s, ∀t∈f.sets, x ∈ closure t,
from assume ⟨x, hxs, hx⟩,
have ∅ ∈ (nhds x ⊓ f).sets, by rw [empty_in_sets_eq_bot, hf x hxs],
let ⟨t₁, ht₁, t₂, ht₂, ht⟩ := by rw [mem_inf_sets] at this; exact this in
have ∅ ∈ (nhds x ⊓ principal t₂).sets,
from (nhds x ⊓ principal t₂).sets_of_superset (inter_mem_inf_sets ht₁ (subset.refl t₂)) ht,
have nhds x ⊓ principal t₂ = ⊥,
by rwa [empty_in_sets_eq_bot] at this,
by simp only [closure_eq_nhds] at hx; exact hx t₂ ht₂ this,
have ∀x∈s, ∃t∈f.sets, x ∉ closure t, by simpa only [not_exists, not_forall],
let c := (λt, - closure t) '' f.sets, ⟨c', hcc', hcf, hsc'⟩ := h c
(assume t ⟨s, hs, h⟩, h ▸ is_closed_closure) (by simpa only [subset_def, sUnion_image, mem_Union]) in
let ⟨b, hb⟩ := axiom_of_choice $
show ∀s:c', ∃t, t ∈ f.sets ∧ - closure t = s,
from assume ⟨x, hx⟩, hcc' hx in
have (⋂s∈c', if h : s ∈ c' then b ⟨s, h⟩ else univ) ∈ f.sets,
from Inter_mem_sets hcf $ assume t ht, by rw [dif_pos ht]; exact (hb ⟨t, ht⟩).left,
have s ∩ (⋂s∈c', if h : s ∈ c' then b ⟨s, h⟩ else univ) ∈ f.sets,
from inter_mem_sets (le_principal_iff.1 hfs) this,
have ∅ ∈ f.sets,
from mem_sets_of_superset this $ assume x ⟨hxs, hxi⟩,
let ⟨t, htc', hxt⟩ := (show ∃t ∈ c', x ∈ t, from hsc' hxs) in
have -closure (b ⟨t, htc'⟩) = t, from (hb _).right,
have x ∈ - t,
from this ▸ (calc x ∈ b ⟨t, htc'⟩ : by rw mem_bInter_iff at hxi; have h := hxi t htc'; rwa [dif_pos htc'] at h
... ⊆ closure (b ⟨t, htc'⟩) : subset_closure
... ⊆ - - closure (b ⟨t, htc'⟩) : by rw lattice.neg_neg; exact subset.refl _),
show false, from this hxt,
hfn $ by rwa [empty_in_sets_eq_bot] at this
lemma compact_iff_finite_subcover {s : set α} :
compact s ↔ (∀c, (∀t∈c, is_open t) → s ⊆ ⋃₀ c → ∃c'⊆c, finite c' ∧ s ⊆ ⋃₀ c') :=
⟨assume hc c, compact_elim_finite_subcover hc, compact_of_finite_subcover⟩
lemma compact_empty : compact (∅ : set α) :=
assume f hnf hsf, not.elim hnf $
empty_in_sets_eq_bot.1 $ le_principal_iff.1 hsf
lemma compact_singleton {a : α} : compact ({a} : set α) :=
compact_of_finite_subcover $ assume c hc₁ hc₂,
let ⟨i, hic, hai⟩ := (show ∃i ∈ c, a ∈ i, from mem_sUnion.1 $ singleton_subset_iff.1 hc₂) in
⟨{i}, singleton_subset_iff.2 hic, finite_singleton _, by rwa [sUnion_singleton, singleton_subset_iff]⟩
lemma compact_bUnion_of_compact {s : set β} {f : β → set α} (hs : finite s) :
(∀i ∈ s, compact (f i)) → compact (⋃i ∈ s, f i) :=
assume hf, compact_of_finite_subcover $ assume c c_open c_cover,
have ∀i : subtype s, ∃c' ⊆ c, finite c' ∧ f i ⊆ ⋃₀ c', from
assume ⟨i, hi⟩, compact_elim_finite_subcover (hf i hi) c_open
(calc f i ⊆ ⋃i ∈ s, f i : subset_bUnion_of_mem hi
... ⊆ ⋃₀ c : c_cover),
let ⟨finite_subcovers, h⟩ := axiom_of_choice this in
let c' := ⋃i, finite_subcovers i in
have c' ⊆ c, from Union_subset (λi, (h i).fst),
have finite c', from @finite_Union _ _ hs.fintype _ (λi, (h i).snd.1),
have (⋃i ∈ s, f i) ⊆ ⋃₀ c', from bUnion_subset $ λi hi, calc
f i ⊆ ⋃₀ finite_subcovers ⟨i,hi⟩ : (h ⟨i,hi⟩).snd.2
... ⊆ ⋃₀ c' : sUnion_mono (subset_Union _ _),
⟨c', ‹c' ⊆ c›, ‹finite c'›, this⟩
lemma compact_Union_of_compact {f : β → set α} [fintype β]
(h : ∀i, compact (f i)) : compact (⋃i, f i) :=
by rw ← bUnion_univ; exact compact_bUnion_of_compact finite_univ (λ i _, h i)
lemma compact_of_finite {s : set α} (hs : finite s) : compact s :=
let s' : set α := ⋃i ∈ s, {i} in
have e : s' = s, from ext $ λi, by simp only [mem_bUnion_iff, mem_singleton_iff, exists_eq_right'],
have compact s', from compact_bUnion_of_compact hs (λ_ _, compact_singleton),
e ▸ this
lemma compact_union_of_compact {s t : set α} (hs : compact s) (ht : compact t) : compact (s ∪ t) :=
by rw union_eq_Union; exact compact_Union_of_compact (λ b, by cases b; assumption)
/-- Type class for compact spaces. Separation is sometimes included in the definition, especially
in the French literature, but we do not include it here. -/
class compact_space (α : Type*) [topological_space α] : Prop :=
(compact_univ : compact (univ : set α))
lemma compact_univ [topological_space α] [h : compact_space α] : compact (univ : set α) := h.compact_univ
lemma compact_of_closed [topological_space α] [compact_space α] {s : set α} (h : is_closed s) :
compact s :=
compact_of_is_closed_subset compact_univ h (subset_univ _)
lemma compact_image [topological_space β] {s : set α} {f : α → β} (hs : compact s) (hf : continuous f) : compact (f '' s) :=
compact_of_finite_subcover $ assume c hco hcs,
have hdo : ∀t∈c, is_open (f ⁻¹' t), from assume t' ht, hf _ $ hco _ ht,
have hds : s ⊆ ⋃i∈c, f ⁻¹' i,
by simpa [subset_def, -mem_image] using hcs,
let ⟨d', hcd', hfd', hd'⟩ := compact_elim_finite_subcover_image hs hdo hds in
⟨d', hcd', hfd', by simpa [subset_def, -mem_image, image_subset_iff] using hd'⟩
lemma compact_range [compact_space α] [topological_space β] {f : α → β} (hf : continuous f) : compact (range f) :=
by rw ← image_univ; exact compact_image compact_univ hf
/-- There are various definitions of "locally compact space" in the literature, which agree for
Hausdorff spaces but not in general. This one is the precise condition on X needed for the
evaluation `map C(X, Y) × X → Y` to be continuous for all `Y` when `C(X, Y)` is given the
compact-open topology. -/
class locally_compact_space (α : Type*) [topological_space α] : Prop :=
(local_compact_nhds : ∀ (x : α) (n ∈ (nhds x).sets), ∃ s ∈ (nhds x).sets, s ⊆ n ∧ compact s)
end compact
section clopen
def is_clopen (s : set α) : Prop :=
is_open s ∧ is_closed s
theorem is_clopen_union {s t : set α} (hs : is_clopen s) (ht : is_clopen t) : is_clopen (s ∪ t) :=
⟨is_open_union hs.1 ht.1, is_closed_union hs.2 ht.2⟩
theorem is_clopen_inter {s t : set α} (hs : is_clopen s) (ht : is_clopen t) : is_clopen (s ∩ t) :=
⟨is_open_inter hs.1 ht.1, is_closed_inter hs.2 ht.2⟩
@[simp] theorem is_clopen_empty : is_clopen (∅ : set α) :=
⟨is_open_empty, is_closed_empty⟩
@[simp] theorem is_clopen_univ : is_clopen (univ : set α) :=
⟨is_open_univ, is_closed_univ⟩
theorem is_clopen_compl {s : set α} (hs : is_clopen s) : is_clopen (-s) :=
⟨hs.2, is_closed_compl_iff.2 hs.1⟩
@[simp] theorem is_clopen_compl_iff {s : set α} : is_clopen (-s) ↔ is_clopen s :=
⟨λ h, compl_compl s ▸ is_clopen_compl h, is_clopen_compl⟩
theorem is_clopen_diff {s t : set α} (hs : is_clopen s) (ht : is_clopen t) : is_clopen (s-t) :=
is_clopen_inter hs (is_clopen_compl ht)
end clopen
section irreducible
/-- A irreducible set is one where there is no non-trivial pair of disjoint opens. -/
def is_irreducible (s : set α) : Prop :=
∀ (u v : set α), is_open u → is_open v →
(∃ x, x ∈ s ∩ u) → (∃ x, x ∈ s ∩ v) → ∃ x, x ∈ s ∩ (u ∩ v)
theorem is_irreducible_empty : is_irreducible (∅ : set α) :=
λ _ _ _ _ _ ⟨x, h1, h2⟩, h1.elim
theorem is_irreducible_singleton {x} : is_irreducible ({x} : set α) :=
λ u v _ _ ⟨y, h1, h2⟩ ⟨z, h3, h4⟩, by rw mem_singleton_iff at h1 h3;
substs y z; exact ⟨x, or.inl rfl, h2, h4⟩
theorem is_irreducible_closure {s : set α} (H : is_irreducible s) :
is_irreducible (closure s) :=
λ u v hu hv ⟨y, hycs, hyu⟩ ⟨z, hzcs, hzv⟩,
let ⟨p, hpu, hps⟩ := exists_mem_of_ne_empty (mem_closure_iff.1 hycs u hu hyu) in
let ⟨q, hqv, hqs⟩ := exists_mem_of_ne_empty (mem_closure_iff.1 hzcs v hv hzv) in
let ⟨r, hrs, hruv⟩ := H u v hu hv ⟨p, hps, hpu⟩ ⟨q, hqs, hqv⟩ in
⟨r, subset_closure hrs, hruv⟩
theorem exists_irreducible (s : set α) (H : is_irreducible s) :
∃ t : set α, is_irreducible t ∧ s ⊆ t ∧ ∀ u, is_irreducible u → t ⊆ u → u = t :=
let ⟨m, hm, hsm, hmm⟩ := zorn.zorn_subset₀ { t : set α | is_irreducible t }
(λ c hc hcc hcn, let ⟨t, htc⟩ := exists_mem_of_ne_empty hcn in
⟨⋃₀ c, λ u v hu hv ⟨y, hy, hyu⟩ ⟨z, hz, hzv⟩,
let ⟨p, hpc, hyp⟩ := mem_sUnion.1 hy,
⟨q, hqc, hzq⟩ := mem_sUnion.1 hz in
or.cases_on (zorn.chain.total hcc hpc hqc)
(assume hpq : p ⊆ q, let ⟨x, hxp, hxuv⟩ := hc hqc u v hu hv
⟨y, hpq hyp, hyu⟩ ⟨z, hzq, hzv⟩ in
⟨x, mem_sUnion_of_mem hxp hqc, hxuv⟩)
(assume hqp : q ⊆ p, let ⟨x, hxp, hxuv⟩ := hc hpc u v hu hv
⟨y, hyp, hyu⟩ ⟨z, hqp hzq, hzv⟩ in
⟨x, mem_sUnion_of_mem hxp hpc, hxuv⟩),
λ x hxc, set.subset_sUnion_of_mem hxc⟩) s H in
⟨m, hm, hsm, λ u hu hmu, hmm _ hu hmu⟩
def irreducible_component (x : α) : set α :=
classical.some (exists_irreducible {x} is_irreducible_singleton)
theorem is_irreducible_irreducible_component {x : α} : is_irreducible (irreducible_component x) :=
(classical.some_spec (exists_irreducible {x} is_irreducible_singleton)).1
theorem mem_irreducible_component {x : α} : x ∈ irreducible_component x :=
singleton_subset_iff.1
(classical.some_spec (exists_irreducible {x} is_irreducible_singleton)).2.1
theorem eq_irreducible_component {x : α} :
∀ {s : set α}, is_irreducible s → irreducible_component x ⊆ s → s = irreducible_component x :=
(classical.some_spec (exists_irreducible {x} is_irreducible_singleton)).2.2
theorem is_closed_irreducible_component {x : α} :
is_closed (irreducible_component x) :=
closure_eq_iff_is_closed.1 $ eq_irreducible_component
(is_irreducible_closure is_irreducible_irreducible_component)
subset_closure
/-- A irreducible space is one where there is no non-trivial pair of disjoint opens. -/
class irreducible_space (α : Type u) [topological_space α] : Prop :=
(is_irreducible_univ : is_irreducible (univ : set α))
theorem irreducible_exists_mem_inter [irreducible_space α] {s t : set α} :
is_open s → is_open t → (∃ x, x ∈ s) → (∃ x, x ∈ t) → ∃ x, x ∈ s ∩ t :=
by simpa only [univ_inter, univ_subset_iff] using
@irreducible_space.is_irreducible_univ α _ _ s t
end irreducible
section connected
/-- A connected set is one where there is no non-trivial open partition. -/
def is_connected (s : set α) : Prop :=
∀ (u v : set α), is_open u → is_open v → s ⊆ u ∪ v →
(∃ x, x ∈ s ∩ u) → (∃ x, x ∈ s ∩ v) → ∃ x, x ∈ s ∩ (u ∩ v)
theorem is_connected_of_is_irreducible {s : set α} (H : is_irreducible s) : is_connected s :=
λ _ _ hu hv _, H _ _ hu hv
theorem is_connected_empty : is_connected (∅ : set α) :=
is_connected_of_is_irreducible is_irreducible_empty
theorem is_connected_singleton {x} : is_connected ({x} : set α) :=
is_connected_of_is_irreducible is_irreducible_singleton
theorem is_connected_sUnion (x : α) (c : set (set α)) (H1 : ∀ s ∈ c, x ∈ s)
(H2 : ∀ s ∈ c, is_connected s) : is_connected (⋃₀ c) :=
begin
rintro u v hu hv hUcuv ⟨y, hyUc, hyu⟩ ⟨z, hzUc, hzv⟩,
cases classical.em (c = ∅) with hc hc,
{ rw [hc, sUnion_empty] at hyUc, exact hyUc.elim },
cases ne_empty_iff_exists_mem.1 hc with s hs,
cases hUcuv (mem_sUnion_of_mem (H1 s hs) hs) with hxu hxv,
{ rcases hzUc with ⟨t, htc, hzt⟩,
specialize H2 t htc u v hu hv (subset.trans (subset_sUnion_of_mem htc) hUcuv),
cases H2 ⟨x, H1 t htc, hxu⟩ ⟨z, hzt, hzv⟩ with r hr,
exact ⟨r, mem_sUnion_of_mem hr.1 htc, hr.2⟩ },
{ rcases hyUc with ⟨t, htc, hyt⟩,
specialize H2 t htc u v hu hv (subset.trans (subset_sUnion_of_mem htc) hUcuv),
cases H2 ⟨y, hyt, hyu⟩ ⟨x, H1 t htc, hxv⟩ with r hr,
exact ⟨r, mem_sUnion_of_mem hr.1 htc, hr.2⟩ }
end
theorem is_connected_union (x : α) {s t : set α} (H1 : x ∈ s) (H2 : x ∈ t)
(H3 : is_connected s) (H4 : is_connected t) : is_connected (s ∪ t) :=
have _ := is_connected_sUnion x {t,s}
(by rintro r (rfl | rfl | h); [exact H1, exact H2, exact h.elim])
(by rintro r (rfl | rfl | h); [exact H3, exact H4, exact h.elim]),
have h2 : ⋃₀ {t, s} = s ∪ t, from (sUnion_insert _ _).trans (by rw sUnion_singleton),
by rwa h2 at this
theorem is_connected_closure {s : set α} (H : is_connected s) :
is_connected (closure s) :=
λ u v hu hv hcsuv ⟨y, hycs, hyu⟩ ⟨z, hzcs, hzv⟩,
let ⟨p, hpu, hps⟩ := exists_mem_of_ne_empty (mem_closure_iff.1 hycs u hu hyu) in
let ⟨q, hqv, hqs⟩ := exists_mem_of_ne_empty (mem_closure_iff.1 hzcs v hv hzv) in
let ⟨r, hrs, hruv⟩ := H u v hu hv (subset.trans subset_closure hcsuv) ⟨p, hps, hpu⟩ ⟨q, hqs, hqv⟩ in
⟨r, subset_closure hrs, hruv⟩
def connected_component (x : α) : set α :=
⋃₀ { s : set α | is_connected s ∧ x ∈ s }
theorem is_connected_connected_component {x : α} : is_connected (connected_component x) :=
is_connected_sUnion x _ (λ _, and.right) (λ _, and.left)
theorem mem_connected_component {x : α} : x ∈ connected_component x :=
mem_sUnion_of_mem (mem_singleton x) ⟨is_connected_singleton, mem_singleton x⟩
theorem subset_connected_component {x : α} {s : set α} (H1 : is_connected s) (H2 : x ∈ s) :
s ⊆ connected_component x :=
λ z hz, mem_sUnion_of_mem hz ⟨H1, H2⟩
theorem is_closed_connected_component {x : α} :
is_closed (connected_component x) :=
closure_eq_iff_is_closed.1 $ subset.antisymm
(subset_connected_component
(is_connected_closure is_connected_connected_component)
(subset_closure mem_connected_component))
subset_closure
theorem irreducible_component_subset_connected_component {x : α} :
irreducible_component x ⊆ connected_component x :=
subset_connected_component
(is_connected_of_is_irreducible is_irreducible_irreducible_component)
mem_irreducible_component
/-- A connected space is one where there is no non-trivial open partition. -/
class connected_space (α : Type u) [topological_space α] : Prop :=
(is_connected_univ : is_connected (univ : set α))
instance irreducible_space.connected_space (α : Type u) [topological_space α]
[irreducible_space α] : connected_space α :=
⟨is_connected_of_is_irreducible $ irreducible_space.is_irreducible_univ α⟩
theorem exists_mem_inter [connected_space α] {s t : set α} :
is_open s → is_open t → s ∪ t = univ →
(∃ x, x ∈ s) → (∃ x, x ∈ t) → ∃ x, x ∈ s ∩ t :=
by simpa only [univ_inter, univ_subset_iff] using
@connected_space.is_connected_univ α _ _ s t
theorem is_clopen_iff [connected_space α] {s : set α} : is_clopen s ↔ s = ∅ ∨ s = univ :=
⟨λ hs, classical.by_contradiction $ λ h,
have h1 : s ≠ ∅ ∧ -s ≠ ∅, from ⟨mt or.inl h,
mt (λ h2, or.inr $ (by rw [← compl_compl s, h2, compl_empty] : s = univ)) h⟩,
let ⟨_, h2, h3⟩ := exists_mem_inter hs.1 hs.2 (union_compl_self s)
(ne_empty_iff_exists_mem.1 h1.1) (ne_empty_iff_exists_mem.1 h1.2) in
h3 h2,
by rintro (rfl | rfl); [exact is_clopen_empty, exact is_clopen_univ]⟩
end connected
section totally_disconnected
def is_totally_disconnected (s : set α) : Prop :=
∀ t, t ⊆ s → is_connected t → subsingleton t
theorem is_totally_disconnected_empty : is_totally_disconnected (∅ : set α) :=
λ t ht _, ⟨λ ⟨_, h⟩, (ht h).elim⟩
theorem is_totally_disconnected_singleton {x} : is_totally_disconnected ({x} : set α) :=
λ t ht _, ⟨λ ⟨p, hp⟩ ⟨q, hq⟩, subtype.eq $ show p = q,
from (eq_of_mem_singleton (ht hp)).symm ▸ (eq_of_mem_singleton (ht hq)).symm⟩
class totally_disconnected_space (α : Type u) [topological_space α] : Prop :=
(is_totally_disconnected_univ : is_totally_disconnected (univ : set α))
end totally_disconnected
section totally_separated
def is_totally_separated (s : set α) : Prop :=
∀ x ∈ s, ∀ y ∈ s, x ≠ y → ∃ u v : set α, is_open u ∧ is_open v ∧
x ∈ u ∧ y ∈ v ∧ s ⊆ u ∪ v ∧ u ∩ v = ∅
theorem is_totally_separated_empty : is_totally_separated (∅ : set α) :=
λ x, false.elim
theorem is_totally_separated_singleton {x} : is_totally_separated ({x} : set α) :=
λ p hp q hq hpq, (hpq $ (eq_of_mem_singleton hp).symm ▸ (eq_of_mem_singleton hq).symm).elim
theorem is_totally_disconnected_of_is_totally_separated {s : set α}
(H : is_totally_separated s) : is_totally_disconnected s :=
λ t hts ht, ⟨λ ⟨x, hxt⟩ ⟨y, hyt⟩, subtype.eq $ classical.by_contradiction $
assume hxy : x ≠ y, let ⟨u, v, hu, hv, hxu, hyv, hsuv, huv⟩ := H x (hts hxt) y (hts hyt) hxy in
let ⟨r, hrt, hruv⟩ := ht u v hu hv (subset.trans hts hsuv) ⟨x, hxt, hxu⟩ ⟨y, hyt, hyv⟩ in
((ext_iff _ _).1 huv r).1 hruv⟩
class totally_separated_space (α : Type u) [topological_space α] : Prop :=
(is_totally_separated_univ : is_totally_separated (univ : set α))
instance totally_separated_space.totally_disconnected_space (α : Type u) [topological_space α]
[totally_separated_space α] : totally_disconnected_space α :=
⟨is_totally_disconnected_of_is_totally_separated $ totally_separated_space.is_totally_separated_univ α⟩
end totally_separated
|
72027aa79d0b180ed72af45090a0fcac5dfcef2a | 2eab05920d6eeb06665e1a6df77b3157354316ad | /src/data/quot.lean | fc2cf383adedfa4988f94a0142db5e97b4b4b1ff | [
"Apache-2.0"
] | permissive | ayush1801/mathlib | 78949b9f789f488148142221606bf15c02b960d2 | ce164e28f262acbb3de6281b3b03660a9f744e3c | refs/heads/master | 1,692,886,907,941 | 1,635,270,866,000 | 1,635,270,866,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 22,325 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl
-/
import logic.relator
/-!
# Quotient types
This module extends the core library's treatment of quotient types (`init.data.quot`).
## Tags
quotient
-/
variables {α : Sort*} {β : Sort*}
namespace setoid
lemma ext {α : Sort*} :
∀{s t : setoid α}, (∀a b, @setoid.r α s a b ↔ @setoid.r α t a b) → s = t
| ⟨r, _⟩ ⟨p, _⟩ eq :=
have r = p, from funext $ assume a, funext $ assume b, propext $ eq a b,
by subst this
end setoid
namespace quot
variables {ra : α → α → Prop} {rb : β → β → Prop} {φ : quot ra → quot rb → Sort*}
local notation `⟦`:max a `⟧` := quot.mk _ a
instance [inhabited α] : inhabited (quot ra) := ⟨⟦default _⟧⟩
/-- Recursion on two `quotient` arguments `a` and `b`, result type depends on `⟦a⟧` and `⟦b⟧`. -/
protected def hrec_on₂ (qa : quot ra) (qb : quot rb) (f : Π a b, φ ⟦a⟧ ⟦b⟧)
(ca : ∀ {b a₁ a₂}, ra a₁ a₂ → f a₁ b == f a₂ b)
(cb : ∀ {a b₁ b₂}, rb b₁ b₂ → f a b₁ == f a b₂) : φ qa qb :=
quot.hrec_on qa (λ a, quot.hrec_on qb (f a) (λ b₁ b₂ pb, cb pb)) $ λ a₁ a₂ pa,
quot.induction_on qb $ λ b,
calc @quot.hrec_on _ _ (φ _) ⟦b⟧ (f a₁) (@cb _)
== f a₁ b : by simp [heq_self_iff_true]
... == f a₂ b : ca pa
... == @quot.hrec_on _ _ (φ _) ⟦b⟧ (f a₂) (@cb _) : by simp [heq_self_iff_true]
/-- Map a function `f : α → β` such that `ra x y` implies `rb (f x) (f y)`
to a map `quot ra → quot rb`. -/
protected def map (f : α → β) (h : (ra ⇒ rb) f f) : quot ra → quot rb :=
quot.lift (λ x, ⟦f x⟧) $ assume x y (h₁ : ra x y), quot.sound $ h h₁
/-- If `ra` is a subrelation of `ra'`, then we have a natural map `quot ra → quot ra'`. -/
protected def map_right {ra' : α → α → Prop} (h : ∀a₁ a₂, ra a₁ a₂ → ra' a₁ a₂) :
quot ra → quot ra' :=
quot.map id h
/-- weaken the relation of a quotient -/
def factor {α : Type*} (r s : α → α → Prop) (h : ∀ x y, r x y → s x y) :
quot r → quot s :=
quot.lift (quot.mk s) (λ x y rxy, quot.sound (h x y rxy))
lemma factor_mk_eq {α : Type*} (r s : α → α → Prop) (h : ∀ x y, r x y → s x y) :
factor r s h ∘ quot.mk _ = quot.mk _ := rfl
variables {γ : Sort*} {r : α → α → Prop} {s : β → β → Prop}
/-- **Alias** of `quot.lift_beta`. -/
lemma lift_mk (f : α → γ) (h : ∀ a₁ a₂, r a₁ a₂ → f a₁ = f a₂) (a : α) :
quot.lift f h (quot.mk r a) = f a := quot.lift_beta f h a
@[simp]
lemma lift_on_mk (a : α) (f : α → γ) (h : ∀ a₁ a₂, r a₁ a₂ → f a₁ = f a₂) :
quot.lift_on (quot.mk r a) f h = f a := rfl
/-- Descends a function `f : α → β → γ` to quotients of `α` and `β`. -/
attribute [reducible, elab_as_eliminator]
protected def lift₂
(f : α → β → γ)
(hr : ∀ a b₁ b₂, s b₁ b₂ → f a b₁ = f a b₂)
(hs : ∀ a₁ a₂ b, r a₁ a₂ → f a₁ b = f a₂ b)
(q₁ : quot r) (q₂ : quot s) : γ :=
quot.lift (λ a, quot.lift (f a) (hr a))
(λ a₁ a₂ ha, funext (λ q, quot.induction_on q (λ b, hs a₁ a₂ b ha)))
q₁ q₂
@[simp]
lemma lift₂_mk (f : α → β → γ)
(hr : ∀ a b₁ b₂, s b₁ b₂ → f a b₁ = f a b₂)
(hs : ∀ a₁ a₂ b, r a₁ a₂ → f a₁ b = f a₂ b) (a : α) (b : β) :
quot.lift₂ f hr hs (quot.mk r a) (quot.mk s b) = f a b := rfl
/-- Descends a function `f : α → β → γ` to quotients of `α` and `β` and applies it. -/
attribute [reducible, elab_as_eliminator]
protected def lift_on₂ (p : quot r) (q : quot s) (f : α → β → γ)
(hr : ∀ a b₁ b₂, s b₁ b₂ → f a b₁ = f a b₂)
(hs : ∀ a₁ a₂ b, r a₁ a₂ → f a₁ b = f a₂ b) : γ := quot.lift₂ f hr hs p q
@[simp]
lemma lift_on₂_mk (a : α) (b : β) (f : α → β → γ)
(hr : ∀ a b₁ b₂, s b₁ b₂ → f a b₁ = f a b₂)
(hs : ∀ a₁ a₂ b, r a₁ a₂ → f a₁ b = f a₂ b) :
quot.lift_on₂ (quot.mk r a) (quot.mk s b) f hr hs = f a b := rfl
variables {t : γ → γ → Prop}
/-- Descends a function `f : α → β → γ` to quotients of `α` and `β` wih values in a quotient of
`γ`. -/
protected def map₂ (f : α → β → γ)
(hr : ∀ a b₁ b₂, s b₁ b₂ → t (f a b₁) (f a b₂))
(hs : ∀ a₁ a₂ b, r a₁ a₂ → t (f a₁ b) (f a₂ b))
(q₁ : quot r) (q₂ : quot s) : quot t :=
quot.lift₂ (λ a b, quot.mk t $ f a b) (λ a b₁ b₂ hb, quot.sound (hr a b₁ b₂ hb))
(λ a₁ a₂ b ha, quot.sound (hs a₁ a₂ b ha)) q₁ q₂
@[simp]
lemma map₂_mk (f : α → β → γ)
(hr : ∀ a b₁ b₂, s b₁ b₂ → t (f a b₁) (f a b₂))
(hs : ∀ a₁ a₂ b, r a₁ a₂ → t (f a₁ b) (f a₂ b))
(a : α) (b : β) : quot.map₂ f hr hs (quot.mk r a) (quot.mk s b) = quot.mk t (f a b) := rfl
attribute [elab_as_eliminator]
protected lemma induction_on₂
{δ : quot r → quot s → Prop} (q₁ : quot r) (q₂ : quot s)
(h : ∀ a b, δ (quot.mk r a) (quot.mk s b)) : δ q₁ q₂ :=
quot.ind (λ a₁, quot.ind (λ a₂, h a₁ a₂) q₂) q₁
attribute [elab_as_eliminator]
protected lemma induction_on₃
{δ : quot r → quot s → quot t → Prop} (q₁ : quot r) (q₂ : quot s) (q₃ : quot t)
(h : ∀ a b c, δ (quot.mk r a) (quot.mk s b) (quot.mk t c)) : δ q₁ q₂ q₃ :=
quot.ind (λ a₁, quot.ind (λ a₂, quot.ind (λ a₃, h a₁ a₂ a₃) q₃) q₂) q₁
end quot
namespace quotient
variables [sa : setoid α] [sb : setoid β]
variables {φ : quotient sa → quotient sb → Sort*}
instance [inhabited α] : inhabited (quotient sa) := ⟨⟦default _⟧⟩
/-- Induction on two `quotient` arguments `a` and `b`, result type depends on `⟦a⟧` and `⟦b⟧`. -/
protected def hrec_on₂ (qa : quotient sa) (qb : quotient sb) (f : Π a b, φ ⟦a⟧ ⟦b⟧)
(c : ∀ a₁ b₁ a₂ b₂, a₁ ≈ a₂ → b₁ ≈ b₂ → f a₁ b₁ == f a₂ b₂) : φ qa qb :=
quot.hrec_on₂ qa qb f
(λ _ _ _ p, c _ _ _ _ p (setoid.refl _))
(λ _ _ _ p, c _ _ _ _ (setoid.refl _) p)
/-- Map a function `f : α → β` that sends equivalent elements to equivalent elements
to a function `quotient sa → quotient sb`. Useful to define unary operations on quotients. -/
protected def map (f : α → β) (h : ((≈) ⇒ (≈)) f f) : quotient sa → quotient sb :=
quot.map f h
@[simp] lemma map_mk (f : α → β) (h : ((≈) ⇒ (≈)) f f) (x : α) :
quotient.map f h (⟦x⟧ : quotient sa) = (⟦f x⟧ : quotient sb) :=
rfl
variables {γ : Sort*} [sc : setoid γ]
/-- Map a function `f : α → β → γ` that sends equivalent elements to equivalent elements
to a function `f : quotient sa → quotient sb → quotient sc`.
Useful to define binary operations on quotients. -/
protected def map₂ (f : α → β → γ) (h : ((≈) ⇒ (≈) ⇒ (≈)) f f) :
quotient sa → quotient sb → quotient sc :=
quotient.lift₂ (λ x y, ⟦f x y⟧) (λ x₁ y₁ x₂ y₂ h₁ h₂, quot.sound $ h h₁ h₂)
end quotient
lemma quot.eq {α : Type*} {r : α → α → Prop} {x y : α} :
quot.mk r x = quot.mk r y ↔ eqv_gen r x y :=
⟨quot.exact r, quot.eqv_gen_sound⟩
@[simp] theorem quotient.eq [r : setoid α] {x y : α} : ⟦x⟧ = ⟦y⟧ ↔ x ≈ y :=
⟨quotient.exact, quotient.sound⟩
theorem forall_quotient_iff {α : Type*} [r : setoid α] {p : quotient r → Prop} :
(∀a:quotient r, p a) ↔ (∀a:α, p ⟦a⟧) :=
⟨assume h x, h _, assume h a, a.induction_on h⟩
@[simp] lemma quotient.lift_mk [s : setoid α] (f : α → β) (h : ∀ (a b : α), a ≈ b → f a = f b)
(x : α) :
quotient.lift f h (quotient.mk x) = f x := rfl
@[simp] lemma quotient.lift_on_mk [s : setoid α] (f : α → β) (h : ∀ (a b : α), a ≈ b → f a = f b)
(x : α) :
quotient.lift_on (quotient.mk x) f h = f x := rfl
@[simp] theorem quotient.lift_on₂_mk {α : Sort*} {β : Sort*} [setoid α] (f : α → α → β)
(h : ∀ (a₁ a₂ b₁ b₂ : α), a₁ ≈ b₁ → a₂ ≈ b₂ → f a₁ a₂ = f b₁ b₂) (x y : α) :
quotient.lift_on₂ (quotient.mk x) (quotient.mk y) f h = f x y := rfl
/-- `quot.mk r` is a surjective function. -/
lemma surjective_quot_mk (r : α → α → Prop) : function.surjective (quot.mk r) :=
quot.exists_rep
/-- `quotient.mk` is a surjective function. -/
lemma surjective_quotient_mk (α : Sort*) [s : setoid α] :
function.surjective (quotient.mk : α → quotient s) :=
quot.exists_rep
/-- Choose an element of the equivalence class using the axiom of choice.
Sound but noncomputable. -/
noncomputable def quot.out {r : α → α → Prop} (q : quot r) : α :=
classical.some (quot.exists_rep q)
/-- Unwrap the VM representation of a quotient to obtain an element of the equivalence class.
Computable but unsound. -/
meta def quot.unquot {r : α → α → Prop} : quot r → α := unchecked_cast
@[simp] theorem quot.out_eq {r : α → α → Prop} (q : quot r) : quot.mk r q.out = q :=
classical.some_spec (quot.exists_rep q)
/-- Choose an element of the equivalence class using the axiom of choice.
Sound but noncomputable. -/
noncomputable def quotient.out [s : setoid α] : quotient s → α := quot.out
@[simp] theorem quotient.out_eq [s : setoid α] (q : quotient s) : ⟦q.out⟧ = q := q.out_eq
theorem quotient.mk_out [s : setoid α] (a : α) : ⟦a⟧.out ≈ a :=
quotient.exact (quotient.out_eq _)
lemma quotient.mk_eq_iff_out [s : setoid α] {x : α} {y : quotient s} :
⟦x⟧ = y ↔ x ≈ quotient.out y :=
begin
refine iff.trans _ quotient.eq,
rw quotient.out_eq y,
end
lemma quotient.eq_mk_iff_out [s : setoid α] {x : quotient s} {y : α} :
x = ⟦y⟧ ↔ quotient.out x ≈ y :=
begin
refine iff.trans _ quotient.eq,
rw quotient.out_eq x,
end
@[simp] lemma quotient.out_equiv_out [s : setoid α] {x y : quotient s} :
x.out ≈ y.out ↔ x = y :=
by rw [← quotient.eq_mk_iff_out, quotient.out_eq]
@[simp] lemma quotient.out_inj [s : setoid α] {x y : quotient s} :
x.out = y.out ↔ x = y :=
⟨λ h, quotient.out_equiv_out.1 $ h ▸ setoid.refl _, λ h, h ▸ rfl⟩
instance pi_setoid {ι : Sort*} {α : ι → Sort*} [∀ i, setoid (α i)] : setoid (Π i, α i) :=
{ r := λ a b, ∀ i, a i ≈ b i,
iseqv := ⟨
λ a i, setoid.refl _,
λ a b h i, setoid.symm (h _),
λ a b c h₁ h₂ i, setoid.trans (h₁ _) (h₂ _)⟩ }
/-- Given a function `f : Π i, quotient (S i)`, returns the class of functions `Π i, α i` sending
each `i` to an element of the class `f i`. -/
noncomputable def quotient.choice {ι : Type*} {α : ι → Type*} [S : Π i, setoid (α i)]
(f : Π i, quotient (S i)) : @quotient (Π i, α i) (by apply_instance) :=
⟦λ i, (f i).out⟧
theorem quotient.choice_eq {ι : Type*} {α : ι → Type*} [Π i, setoid (α i)]
(f : Π i, α i) : quotient.choice (λ i, ⟦f i⟧) = ⟦f⟧ :=
quotient.sound $ λ i, quotient.mk_out _
lemma nonempty_quotient_iff (s : setoid α) : nonempty (quotient s) ↔ nonempty α :=
⟨assume ⟨a⟩, quotient.induction_on a nonempty.intro, assume ⟨a⟩, ⟨⟦a⟧⟩⟩
/-- `trunc α` is the quotient of `α` by the always-true relation. This
is related to the propositional truncation in HoTT, and is similar
in effect to `nonempty α`, but unlike `nonempty α`, `trunc α` is data,
so the VM representation is the same as `α`, and so this can be used to
maintain computability. -/
def {u} trunc (α : Sort u) : Sort u := @quot α (λ _ _, true)
theorem true_equivalence : @equivalence α (λ _ _, true) :=
⟨λ _, trivial, λ _ _ _, trivial, λ _ _ _ _ _, trivial⟩
namespace trunc
/-- Constructor for `trunc α` -/
def mk (a : α) : trunc α := quot.mk _ a
instance [inhabited α] : inhabited (trunc α) := ⟨mk (default _)⟩
/-- Any constant function lifts to a function out of the truncation -/
def lift (f : α → β) (c : ∀ a b : α, f a = f b) : trunc α → β :=
quot.lift f (λ a b _, c a b)
theorem ind {β : trunc α → Prop} : (∀ a : α, β (mk a)) → ∀ q : trunc α, β q := quot.ind
protected theorem lift_mk (f : α → β) (c) (a : α) : lift f c (mk a) = f a := rfl
/-- Lift a constant function on `q : trunc α`. -/
@[reducible, elab_as_eliminator]
protected def lift_on (q : trunc α) (f : α → β)
(c : ∀ a b : α, f a = f b) : β := lift f c q
@[elab_as_eliminator]
protected theorem induction_on {β : trunc α → Prop} (q : trunc α)
(h : ∀ a, β (mk a)) : β q := ind h q
theorem exists_rep (q : trunc α) : ∃ a : α, mk a = q := quot.exists_rep q
attribute [elab_as_eliminator]
protected theorem induction_on₂ {C : trunc α → trunc β → Prop} (q₁ : trunc α) (q₂ : trunc β)
(h : ∀ a b, C (mk a) (mk b)) : C q₁ q₂ :=
trunc.induction_on q₁ $ λ a₁, trunc.induction_on q₂ (h a₁)
protected theorem eq (a b : trunc α) : a = b :=
trunc.induction_on₂ a b (λ x y, quot.sound trivial)
instance : subsingleton (trunc α) := ⟨trunc.eq⟩
/-- The `bind` operator for the `trunc` monad. -/
def bind (q : trunc α) (f : α → trunc β) : trunc β :=
trunc.lift_on q f (λ a b, trunc.eq _ _)
/-- A function `f : α → β` defines a function `map f : trunc α → trunc β`. -/
def map (f : α → β) (q : trunc α) : trunc β := bind q (trunc.mk ∘ f)
instance : monad trunc :=
{ pure := @trunc.mk,
bind := @trunc.bind }
instance : is_lawful_monad trunc :=
{ id_map := λ α q, trunc.eq _ _,
pure_bind := λ α β q f, rfl,
bind_assoc := λ α β γ x f g, trunc.eq _ _ }
variable {C : trunc α → Sort*}
/-- Recursion/induction principle for `trunc`. -/
@[reducible, elab_as_eliminator]
protected def rec
(f : Π a, C (mk a)) (h : ∀ (a b : α), (eq.rec (f a) (trunc.eq (mk a) (mk b)) : C (mk b)) = f b)
(q : trunc α) : C q :=
quot.rec f (λ a b _, h a b) q
/-- A version of `trunc.rec` taking `q : trunc α` as the first argument. -/
@[reducible, elab_as_eliminator]
protected def rec_on (q : trunc α) (f : Π a, C (mk a))
(h : ∀ (a b : α), (eq.rec (f a) (trunc.eq (mk a) (mk b)) : C (mk b)) = f b) : C q :=
trunc.rec f h q
/-- A version of `trunc.rec_on` assuming the codomain is a `subsingleton`. -/
@[reducible, elab_as_eliminator]
protected def rec_on_subsingleton
[∀ a, subsingleton (C (mk a))] (q : trunc α) (f : Π a, C (mk a)) : C q :=
trunc.rec f (λ a b, subsingleton.elim _ (f b)) q
/-- Noncomputably extract a representative of `trunc α` (using the axiom of choice). -/
noncomputable def out : trunc α → α := quot.out
@[simp] theorem out_eq (q : trunc α) : mk q.out = q := trunc.eq _ _
protected theorem nonempty (q : trunc α) : nonempty α :=
nonempty_of_exists q.exists_rep
end trunc
namespace quotient
variables {γ : Sort*} {φ : Sort*}
{s₁ : setoid α} {s₂ : setoid β} {s₃ : setoid γ}
/-! Versions of quotient definitions and lemmas ending in `'` use unification instead
of typeclass inference for inferring the `setoid` argument. This is useful when there are
several different quotient relations on a type, for example quotient groups, rings and modules. -/
/-- A version of `quotient.mk` taking `{s : setoid α}` as an implicit argument instead of an
instance argument. -/
protected def mk' (a : α) : quotient s₁ := quot.mk s₁.1 a
/-- `quotient.mk'` is a surjective function. -/
lemma surjective_quotient_mk' : function.surjective (quotient.mk' : α → quotient s₁) :=
quot.exists_rep
/-- A version of `quotient.lift_on` taking `{s : setoid α}` as an implicit argument instead of an
instance argument. -/
@[elab_as_eliminator, reducible]
protected def lift_on' (q : quotient s₁) (f : α → φ)
(h : ∀ a b, @setoid.r α s₁ a b → f a = f b) : φ := quotient.lift_on q f h
@[simp]
protected lemma lift_on'_mk' (f : α → φ) (h) (x : α) :
quotient.lift_on' (@quotient.mk' _ s₁ x) f h = f x := rfl
/-- A version of `quotient.lift_on₂` taking `{s₁ : setoid α} {s₂ : setoid β}` as implicit arguments
instead of instance arguments. -/
@[elab_as_eliminator, reducible]
protected def lift_on₂' (q₁ : quotient s₁) (q₂ : quotient s₂) (f : α → β → γ)
(h : ∀ a₁ a₂ b₁ b₂, @setoid.r α s₁ a₁ b₁ → @setoid.r β s₂ a₂ b₂ → f a₁ a₂ = f b₁ b₂) : γ :=
quotient.lift_on₂ q₁ q₂ f h
@[simp]
protected lemma lift_on₂'_mk' (f : α → β → γ) (h) (a : α) (b : β) :
quotient.lift_on₂' (@quotient.mk' _ s₁ a) (@quotient.mk' _ s₂ b) f h = f a b := rfl
/-- A version of `quotient.ind` taking `{s : setoid α}` as an implicit argument instead of an
instance argument. -/
@[elab_as_eliminator]
protected lemma ind' {p : quotient s₁ → Prop}
(h : ∀ a, p (quotient.mk' a)) (q : quotient s₁) : p q :=
quotient.ind h q
/-- A version of `quotient.ind₂` taking `{s₁ : setoid α} {s₂ : setoid β}` as implicit arguments
instead of instance arguments. -/
@[elab_as_eliminator]
protected lemma ind₂' {p : quotient s₁ → quotient s₂ → Prop}
(h : ∀ a₁ a₂, p (quotient.mk' a₁) (quotient.mk' a₂))
(q₁ : quotient s₁) (q₂ : quotient s₂) : p q₁ q₂ :=
quotient.ind₂ h q₁ q₂
/-- A version of `quotient.induction_on` taking `{s : setoid α}` as an implicit argument instead
of an instance argument. -/
@[elab_as_eliminator]
protected lemma induction_on' {p : quotient s₁ → Prop} (q : quotient s₁)
(h : ∀ a, p (quotient.mk' a)) : p q := quotient.induction_on q h
/-- A version of `quotient.induction_on₂` taking `{s₁ : setoid α} {s₂ : setoid β}` as implicit
arguments instead of instance arguments. -/
@[elab_as_eliminator]
protected lemma induction_on₂' {p : quotient s₁ → quotient s₂ → Prop} (q₁ : quotient s₁)
(q₂ : quotient s₂) (h : ∀ a₁ a₂, p (quotient.mk' a₁) (quotient.mk' a₂)) : p q₁ q₂ :=
quotient.induction_on₂ q₁ q₂ h
/-- A version of `quotient.induction_on₃` taking `{s₁ : setoid α} {s₂ : setoid β} {s₃ : setoid γ}`
as implicit arguments instead of instance arguments. -/
@[elab_as_eliminator]
protected lemma induction_on₃' {p : quotient s₁ → quotient s₂ → quotient s₃ → Prop}
(q₁ : quotient s₁) (q₂ : quotient s₂) (q₃ : quotient s₃)
(h : ∀ a₁ a₂ a₃, p (quotient.mk' a₁) (quotient.mk' a₂) (quotient.mk' a₃)) : p q₁ q₂ q₃ :=
quotient.induction_on₃ q₁ q₂ q₃ h
/-- A version of `quotient.rec_on_subsingleton` taking `{s₁ : setoid α}` as an implicit argument
instead of an instance argument. -/
@[elab_as_eliminator]
protected def rec_on_subsingleton' {φ : quotient s₁ → Sort*}
[h : ∀ a, subsingleton (φ ⟦a⟧)] (q : quotient s₁) (f : Π a, φ (quotient.mk' a)) : φ q :=
quotient.rec_on_subsingleton q f
/-- A version of `quotient.rec_on_subsingleton₂` taking `{s₁ : setoid α} {s₂ : setoid α}`
as implicit arguments instead of instance arguments. -/
attribute [reducible, elab_as_eliminator]
protected def rec_on_subsingleton₂'
{φ : quotient s₁ → quotient s₂ → Sort*} [h : ∀ a b, subsingleton (φ ⟦a⟧ ⟦b⟧)]
(q₁ : quotient s₁) (q₂ : quotient s₂) (f : Π a₁ a₂, φ (quotient.mk' a₁) (quotient.mk' a₂)) :
φ q₁ q₂ :=
quotient.rec_on_subsingleton₂ q₁ q₂ f
/-- Recursion on a `quotient` argument `a`, result type depends on `⟦a⟧`. -/
protected def hrec_on' {φ : quotient s₁ → Sort*} (qa : quotient s₁) (f : Π a, φ (quotient.mk' a))
(c : ∀ a₁ a₂, a₁ ≈ a₂ → f a₁ == f a₂) : φ qa :=
quot.hrec_on qa f c
@[simp] lemma hrec_on'_mk' {φ : quotient s₁ → Sort*} (f : Π a, φ (quotient.mk' a))
(c : ∀ a₁ a₂, a₁ ≈ a₂ → f a₁ == f a₂) (x : α) :
(quotient.mk' x).hrec_on' f c = f x :=
rfl
/-- Recursion on two `quotient` arguments `a` and `b`, result type depends on `⟦a⟧` and `⟦b⟧`. -/
protected def hrec_on₂' {φ : quotient s₁ → quotient s₂ → Sort*} (qa : quotient s₁)
(qb : quotient s₂) (f : ∀ a b, φ (quotient.mk' a) (quotient.mk' b))
(c : ∀ a₁ b₁ a₂ b₂, a₁ ≈ a₂ → b₁ ≈ b₂ → f a₁ b₁ == f a₂ b₂) : φ qa qb :=
quotient.hrec_on₂ qa qb f c
@[simp] lemma hrec_on₂'_mk' {φ : quotient s₁ → quotient s₂ → Sort*}
(f : ∀ a b, φ (quotient.mk' a) (quotient.mk' b))
(c : ∀ a₁ b₁ a₂ b₂, a₁ ≈ a₂ → b₁ ≈ b₂ → f a₁ b₁ == f a₂ b₂) (x : α) (qb : quotient s₂) :
(quotient.mk' x).hrec_on₂' qb f c = qb.hrec_on' (f x) (λ b₁ b₂, c _ _ _ _ (setoid.refl _)) :=
rfl
/-- Map a function `f : α → β` that sends equivalent elements to equivalent elements
to a function `quotient sa → quotient sb`. Useful to define unary operations on quotients. -/
protected def map' (f : α → β) (h : ((≈) ⇒ (≈)) f f) :
quotient s₁ → quotient s₂ :=
quot.map f h
@[simp] lemma map'_mk' (f : α → β) (h) (x : α) :
(quotient.mk' x : quotient s₁).map' f h = (quotient.mk' (f x) : quotient s₂) :=
rfl
/-- A version of `quotient.map₂` using curly braces and unification. -/
protected def map₂' (f : α → β → γ) (h : ((≈) ⇒ (≈) ⇒ (≈)) f f) :
quotient s₁ → quotient s₂ → quotient s₃ :=
quotient.map₂ f h
@[simp] lemma map₂'_mk' (f : α → β → γ) (h) (x : α) :
(quotient.mk' x : quotient s₁).map₂' f h =
(quotient.map' (f x) (h (setoid.refl x)) : quotient s₂ → quotient s₃) :=
rfl
lemma exact' {a b : α} :
(quotient.mk' a : quotient s₁) = quotient.mk' b → @setoid.r _ s₁ a b :=
quotient.exact
lemma sound' {a b : α} : @setoid.r _ s₁ a b → @quotient.mk' α s₁ a = quotient.mk' b :=
quotient.sound
@[simp]
protected lemma eq' {a b : α} : @quotient.mk' α s₁ a = quotient.mk' b ↔ @setoid.r _ s₁ a b :=
quotient.eq
/-- A version of `quotient.out` taking `{s₁ : setoid α}` as an implicit argument instead of an
instance argument. -/
noncomputable def out' (a : quotient s₁) : α := quotient.out a
@[simp] theorem out_eq' (q : quotient s₁) : quotient.mk' q.out' = q := q.out_eq
theorem mk_out' (a : α) : @setoid.r α s₁ (quotient.mk' a : quotient s₁).out' a :=
quotient.exact (quotient.out_eq _)
end quotient
|
df672dd0d2d2a7edd53b8e4f0300ec7d62bc45ff | 88fb7558b0636ec6b181f2a548ac11ad3919f8a5 | /library/init/data/fin/basic.lean | 0809bd1a8cf5ae45f5a34584939401520e07421f | [
"Apache-2.0"
] | permissive | moritayasuaki/lean | 9f666c323cb6fa1f31ac597d777914aed41e3b7a | ae96ebf6ee953088c235ff7ae0e8c95066ba8001 | refs/heads/master | 1,611,135,440,814 | 1,493,852,869,000 | 1,493,852,869,000 | 90,269,903 | 0 | 0 | null | 1,493,906,291,000 | 1,493,906,291,000 | null | UTF-8 | Lean | false | false | 1,053 | 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
-/
prelude
import init.data.nat.basic
open nat
structure fin (n : nat) := (val : nat) (is_lt : val < n)
namespace fin
def {u} elim0 {α : Type u} : fin 0 → α
| ⟨_, h⟩ := absurd h (not_lt_zero _)
variable {n : nat}
lemma eq_of_veq : ∀ {i j : fin n}, (val i) = (val j) → i = j
| ⟨iv, ilt₁⟩ ⟨.(iv), ilt₂⟩ rfl := rfl
lemma veq_of_eq : ∀ {i j : fin n}, i = j → (val i) = (val j)
| ⟨iv, ilt⟩ .(_) rfl := rfl
lemma ne_of_vne {i j : fin n} (h : val i ≠ val j) : i ≠ j :=
λ h', absurd (veq_of_eq h') h
lemma vne_of_ne {i j : fin n} (h : i ≠ j) : val i ≠ val j :=
λ h', absurd (eq_of_veq h') h
end fin
open fin
instance (n : nat) : decidable_eq (fin n)
| ⟨ival, ilt⟩ ⟨jval, jlt⟩ :=
match nat.decidable_eq ival jval with
| is_true h₁ := is_true (eq_of_veq h₁)
| is_false h₁ := is_false (λ h₂, absurd (veq_of_eq h₂) h₁)
end
|
7ea4166f872e340458847bb5daf6e92d26daf54f | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/logic/unique.lean | 4f10a67a3ed6f4473306f691b857eaccc8ba7bf5 | [
"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 | 8,185 | lean | /-
Copyright (c) 2019 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin
-/
import tactic.basic
import logic.is_empty
/-!
# Types with a unique term
In this file we define a typeclass `unique`,
which expresses that a type has a unique term.
In other words, a type that is `inhabited` and a `subsingleton`.
## Main declaration
* `unique`: a typeclass that expresses that a type has a unique term.
## Main statements
* `unique.mk'`: an inhabited subsingleton type is `unique`. This can not be an instance because it
would lead to loops in typeclass inference.
* `function.surjective.unique`: if the domain of a surjective function is `unique`, then its
codomain is `unique` as well.
* `function.injective.subsingleton`: if the codomain of an injective function is `subsingleton`,
then its domain is `subsingleton` as well.
* `function.injective.unique`: if the codomain of an injective function is `subsingleton` and its
domain is `inhabited`, then its domain is `unique`.
## Implementation details
The typeclass `unique α` is implemented as a type,
rather than a `Prop`-valued predicate,
for good definitional properties of the default term.
-/
universes u v w
variables {α : Sort u} {β : Sort v} {γ : Sort w}
/-- `unique α` expresses that `α` is a type with a unique term `default`.
This is implemented as a type, rather than a `Prop`-valued predicate,
for good definitional properties of the default term. -/
@[ext]
structure unique (α : Sort u) extends inhabited α :=
(uniq : ∀ a : α, a = default)
attribute [class] unique
lemma unique_iff_exists_unique (α : Sort u) : nonempty (unique α) ↔ ∃! a : α, true :=
⟨λ ⟨u⟩, ⟨u.default, trivial, λ a _, u.uniq a⟩, λ ⟨a,_,h⟩, ⟨⟨⟨a⟩, λ _, h _ trivial⟩⟩⟩
lemma unique_subtype_iff_exists_unique {α} (p : α → Prop) :
nonempty (unique (subtype p)) ↔ ∃! a, p a :=
⟨λ ⟨u⟩, ⟨u.default.1, u.default.2, λ a h, congr_arg subtype.val (u.uniq ⟨a,h⟩)⟩,
λ ⟨a,ha,he⟩, ⟨⟨⟨⟨a,ha⟩⟩, λ ⟨b,hb⟩, by { congr, exact he b hb }⟩⟩⟩
/-- Given an explicit `a : α` with `[subsingleton α]`, we can construct
a `[unique α]` instance. This is a def because the typeclass search cannot
arbitrarily invent the `a : α` term. Nevertheless, these instances are all
equivalent by `unique.subsingleton.unique`.
See note [reducible non-instances]. -/
@[reducible] def unique_of_subsingleton {α : Sort*} [subsingleton α] (a : α) : unique α :=
{ default := a,
uniq := λ _, subsingleton.elim _ _ }
instance punit.unique : unique punit.{u} :=
{ default := punit.star,
uniq := λ x, punit_eq x _ }
@[simp] lemma punit.default_eq_star : (default : punit) = punit.star := rfl
/-- Every provable proposition is unique, as all proofs are equal. -/
def unique_prop {p : Prop} (h : p) : unique p :=
{ default := h, uniq := λ x, rfl }
instance : unique true := unique_prop trivial
lemma fin.eq_zero : ∀ n : fin 1, n = 0
| ⟨n, hn⟩ := fin.eq_of_veq (nat.eq_zero_of_le_zero (nat.le_of_lt_succ hn))
instance {n : ℕ} : inhabited (fin n.succ) := ⟨0⟩
instance inhabited_fin_one_add (n : ℕ) : inhabited (fin (1 + n)) := ⟨⟨0, nat.zero_lt_one_add n⟩⟩
@[simp] lemma fin.default_eq_zero (n : ℕ) : (default : fin n.succ) = 0 := rfl
instance fin.unique : unique (fin 1) :=
{ uniq := fin.eq_zero, .. fin.inhabited }
namespace unique
open function
section
variables [unique α]
@[priority 100] -- see Note [lower instance priority]
instance : inhabited α := to_inhabited ‹unique α›
lemma eq_default (a : α) : a = default := uniq _ a
lemma default_eq (a : α) : default = a := (uniq _ a).symm
@[priority 100] -- see Note [lower instance priority]
instance : subsingleton α := subsingleton_of_forall_eq _ eq_default
lemma forall_iff {p : α → Prop} : (∀ a, p a) ↔ p default :=
⟨λ h, h _, λ h x, by rwa [unique.eq_default x]⟩
lemma exists_iff {p : α → Prop} : Exists p ↔ p default :=
⟨λ ⟨a, ha⟩, eq_default a ▸ ha, exists.intro default⟩
end
@[ext] protected lemma subsingleton_unique' : ∀ (h₁ h₂ : unique α), h₁ = h₂
| ⟨⟨x⟩, h⟩ ⟨⟨y⟩, _⟩ := by congr; rw [h x, h y]
instance subsingleton_unique : subsingleton (unique α) :=
⟨unique.subsingleton_unique'⟩
/-- Construct `unique` from `inhabited` and `subsingleton`. Making this an instance would create
a loop in the class inheritance graph. -/
@[reducible] def mk' (α : Sort u) [h₁ : inhabited α] [subsingleton α] : unique α :=
{ uniq := λ x, subsingleton.elim _ _, .. h₁ }
end unique
lemma unique_iff_subsingleton_and_nonempty (α : Sort u) :
nonempty (unique α) ↔ subsingleton α ∧ nonempty α :=
⟨λ ⟨u⟩, by split; exactI infer_instance,
λ ⟨hs, hn⟩, ⟨by { resetI, inhabit α, exact unique.mk' α }⟩⟩
@[simp] lemma pi.default_def {β : α → Sort v} [Π a, inhabited (β a)] :
@default (Π a, β a) _ = λ a : α, @default (β a) _ := rfl
lemma pi.default_apply {β : α → Sort v} [Π a, inhabited (β a)] (a : α) :
@default (Π a, β a) _ a = default := rfl
instance pi.unique {β : α → Sort v} [Π a, unique (β a)] : unique (Π a, β a) :=
{ uniq := λ f, funext $ λ x, unique.eq_default _,
.. pi.inhabited α }
/-- There is a unique function on an empty domain. -/
instance pi.unique_of_is_empty [is_empty α] (β : α → Sort v) :
unique (Π a, β a) :=
{ default := is_empty_elim,
uniq := λ f, funext is_empty_elim }
lemma eq_const_of_unique [unique α] (f : α → β) : f = function.const α (f default) :=
by { ext x, rw subsingleton.elim x default }
lemma heq_const_of_unique [unique α] {β : α → Sort v}
(f : Π a, β a) : f == function.const α (f default) :=
function.hfunext rfl $ λ i _ _, by rw subsingleton.elim i default
namespace function
variable {f : α → β}
/-- If the codomain of an injective function is a subsingleton, then the domain
is a subsingleton as well. -/
protected lemma injective.subsingleton (hf : injective f) [subsingleton β] :
subsingleton α :=
⟨λ x y, hf $ subsingleton.elim _ _⟩
/-- If the domain of a surjective function is a subsingleton, then the codomain is a subsingleton as
well. -/
protected lemma surjective.subsingleton [subsingleton α] (hf : surjective f) :
subsingleton β :=
⟨hf.forall₂.2 $ λ x y, congr_arg f $ subsingleton.elim x y⟩
/-- If the domain of a surjective function is a singleton,
then the codomain is a singleton as well. -/
protected def surjective.unique (hf : surjective f) [unique α] : unique β :=
@unique.mk' _ ⟨f default⟩ hf.subsingleton
/-- If `α` is inhabited and admits an injective map to a subsingleton type, then `α` is `unique`. -/
protected def injective.unique [inhabited α] [subsingleton β] (hf : injective f) : unique α :=
@unique.mk' _ _ hf.subsingleton
/-- If a constant function is surjective, then the codomain is a singleton. -/
def surjective.unique_of_surjective_const (α : Type*) {β : Type*} (b : β)
(h : function.surjective (function.const α b)) : unique β :=
@unique_of_subsingleton _ (subsingleton_of_forall_eq b $ h.forall.mpr (λ _, rfl)) b
end function
lemma unique.bijective {A B} [unique A] [unique B] {f : A → B} : function.bijective f :=
begin
rw function.bijective_iff_has_inverse,
refine ⟨default, _, _⟩; intro x; simp
end
namespace option
/-- `option α` is a `subsingleton` if and only if `α` is empty. -/
lemma subsingleton_iff_is_empty {α} : subsingleton (option α) ↔ is_empty α :=
⟨λ h, ⟨λ x, option.no_confusion $ @subsingleton.elim _ h x none⟩,
λ h, ⟨λ x y, option.cases_on x (option.cases_on y rfl (λ x, h.elim x)) (λ x, h.elim x)⟩⟩
instance {α} [is_empty α] : unique (option α) := @unique.mk' _ _ (subsingleton_iff_is_empty.2 ‹_›)
end option
section subtype
instance unique.subtype_eq (y : α) : unique {x // x = y} :=
{ default := ⟨y, rfl⟩,
uniq := λ ⟨x, hx⟩, by simpa using hx }
instance unique.subtype_eq' (y : α) : unique {x // y = x} :=
{ default := ⟨y, rfl⟩,
uniq := λ ⟨x, hx⟩, by simpa using hx.symm }
end subtype
|
a275caf6cacdf9b07c0d4416ceab637075f43662 | c3f2fcd060adfa2ca29f924839d2d925e8f2c685 | /tests/lean/run/def_tac.lean | 2f4f7ec824aec83911012abf891b99bc0f1954b0 | [
"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 | 887 | lean | context
open tactic
definition cases_refl (e : expr) : tactic :=
cases e expr_list.nil; apply rfl
definition cases_lst_refl : expr_list → tactic
| cases_lst_refl expr_list.nil := apply rfl
| cases_lst_refl (expr_list.cons a l) := cases a expr_list.nil; cases_lst_refl l
-- Similar to cases_refl, but make sure the argument is not an arbitrary expression.
definition eq_rec {A : Type} {a b : A} (e : a = b) : tactic :=
cases e expr_list.nil; apply rfl
end
notation `cases_lst` l:(foldr `,` (h t, tactic.expr_list.cons h t) tactic.expr_list.nil) := cases_lst_refl l
open prod
theorem tst₁ (a : nat × nat) : (pr1 a, pr2 a) = a :=
by cases_refl a
theorem tst₂ (a b : nat × nat) (h₁ : pr₁ a = pr₁ b) (h₂ : pr₂ a = pr₂ b) : a = b :=
by cases_lst a, b, h₁, h₂
open nat
theorem tst₃ (a b : nat) (h : a = b) : a + b = b + a :=
by eq_rec h
|
c7051085e769e39091d5b973f3c3cdddb26d546d | 4d2583807a5ac6caaffd3d7a5f646d61ca85d532 | /src/logic/function/iterate.lean | 2466aac5716621855e699dffc6ad3764013bab11 | [
"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,968 | lean | /-
Copyright (c) 2020 Yury Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury Kudryashov
-/
import logic.function.conjugate
/-!
# Iterations of a function
In this file we prove simple properties of `nat.iterate f n` a.k.a. `f^[n]`:
* `iterate_zero`, `iterate_succ`, `iterate_succ'`, `iterate_add`, `iterate_mul`:
formulas for `f^[0]`, `f^[n+1]` (two versions), `f^[n+m]`, and `f^[n*m]`;
* `iterate_id` : `id^[n]=id`;
* `injective.iterate`, `surjective.iterate`, `bijective.iterate` :
iterates of an injective/surjective/bijective function belong to the same class;
* `left_inverse.iterate`, `right_inverse.iterate`, `commute.iterate_left`, `comute.iterate_right`,
`commute.iterate_iterate`:
some properties of pairs of functions survive under iterations
* `iterate_fixed`, `semiconj.iterate_*`, `semiconj₂.iterate`:
if `f` fixes a point (resp., semiconjugates unary/binary operarations), then so does `f^[n]`.
-/
universes u v
variables {α : Type u} {β : Type v}
namespace function
variable (f : α → α)
@[simp] theorem iterate_zero : f^[0] = id := rfl
theorem iterate_zero_apply (x : α) : f^[0] x = x := rfl
@[simp] theorem iterate_succ (n : ℕ) : f^[n.succ] = (f^[n]) ∘ f := rfl
theorem iterate_succ_apply (n : ℕ) (x : α) : f^[n.succ] x = (f^[n]) (f x) := rfl
@[simp] theorem iterate_id (n : ℕ) : (id : α → α)^[n] = id :=
nat.rec_on n rfl $ λ n ihn, by rw [iterate_succ, ihn, comp.left_id]
theorem iterate_add : ∀ (m n : ℕ), f^[m + n] = (f^[m]) ∘ (f^[n])
| m 0 := rfl
| m (nat.succ n) := by rw [nat.add_succ, iterate_succ, iterate_succ, iterate_add]
theorem iterate_add_apply (m n : ℕ) (x : α) : f^[m + n] x = (f^[m] (f^[n] x)) :=
by rw iterate_add
@[simp] theorem iterate_one : f^[1] = f := funext $ λ a, rfl
lemma iterate_mul (m : ℕ) : ∀ n, f^[m * n] = (f^[m]^[n])
| 0 := by simp only [nat.mul_zero, iterate_zero]
| (n + 1) := by simp only [nat.mul_succ, nat.mul_one, iterate_one, iterate_add, iterate_mul n]
variable {f}
theorem iterate_fixed {x} (h : f x = x) (n : ℕ) : f^[n] x = x :=
nat.rec_on n rfl $ λ n ihn, by rw [iterate_succ_apply, h, ihn]
theorem injective.iterate (Hinj : injective f) (n : ℕ) : injective (f^[n]) :=
nat.rec_on n injective_id $ λ n ihn, ihn.comp Hinj
theorem surjective.iterate (Hsurj : surjective f) (n : ℕ) : surjective (f^[n]) :=
nat.rec_on n surjective_id $ λ n ihn, ihn.comp Hsurj
theorem bijective.iterate (Hbij : bijective f) (n : ℕ) : bijective (f^[n]) :=
⟨Hbij.1.iterate n, Hbij.2.iterate n⟩
namespace semiconj
lemma iterate_right {f : α → β} {ga : α → α} {gb : β → β} (h : semiconj f ga gb) (n : ℕ) :
semiconj f (ga^[n]) (gb^[n]) :=
nat.rec_on n id_right $ λ n ihn, ihn.comp_right h
lemma iterate_left {g : ℕ → α → α} (H : ∀ n, semiconj f (g n) (g $ n + 1)) (n k : ℕ) :
semiconj (f^[n]) (g k) (g $ n + k) :=
begin
induction n with n ihn generalizing k,
{ rw [nat.zero_add], exact id_left },
{ rw [nat.succ_eq_add_one, nat.add_right_comm, nat.add_assoc],
exact (H k).comp_left (ihn (k + 1)) }
end
end semiconj
namespace commute
variable {g : α → α}
lemma iterate_right (h : commute f g) (n : ℕ) : commute f (g^[n]) := h.iterate_right n
lemma iterate_left (h : commute f g) (n : ℕ) : commute (f^[n]) g := (h.symm.iterate_right n).symm
lemma iterate_iterate (h : commute f g) (m n : ℕ) : commute (f^[m]) (g^[n]) :=
(h.iterate_left m).iterate_right n
lemma iterate_eq_of_map_eq (h : commute f g) (n : ℕ) {x} (hx : f x = g x) : f^[n] x = (g^[n]) x :=
nat.rec_on n rfl $ λ n ihn,
by simp only [iterate_succ_apply, hx, (h.iterate_left n).eq, ihn, ((refl g).iterate_right n).eq]
lemma comp_iterate (h : commute f g) (n : ℕ) : (f ∘ g)^[n] = (f^[n]) ∘ (g^[n]) :=
begin
induction n with n ihn, { refl },
funext x,
simp only [ihn, (h.iterate_right n).eq, iterate_succ, comp_app]
end
variable (f)
lemma iterate_self (n : ℕ) : commute (f^[n]) f := (refl f).iterate_left n
lemma self_iterate (n : ℕ) : commute f (f^[n]) := (refl f).iterate_right n
lemma iterate_iterate_self (m n : ℕ) : commute (f^[m]) (f^[n]) := (refl f).iterate_iterate m n
end commute
lemma semiconj₂.iterate {f : α → α} {op : α → α → α} (hf : semiconj₂ f op op) (n : ℕ) :
semiconj₂ (f^[n]) op op :=
nat.rec_on n (semiconj₂.id_left op) (λ n ihn, ihn.comp hf)
variable (f)
theorem iterate_succ' (n : ℕ) : f^[n.succ] = f ∘ (f^[n]) :=
by rw [iterate_succ, (commute.self_iterate f n).comp_eq]
theorem iterate_succ_apply' (n : ℕ) (x : α) : f^[n.succ] x = f (f^[n] x) :=
by rw [iterate_succ']
theorem iterate_pred_comp_of_pos {n : ℕ} (hn : 0 < n) : f^[n.pred] ∘ f = (f^[n]) :=
by rw [← iterate_succ, nat.succ_pred_eq_of_pos hn]
theorem comp_iterate_pred_of_pos {n : ℕ} (hn : 0 < n) : f ∘ (f^[n.pred]) = (f^[n]) :=
by rw [← iterate_succ', nat.succ_pred_eq_of_pos hn]
/-- A recursor for the iterate of a function. -/
def iterate.rec (p : α → Sort*) {f : α → α} (h : ∀ a, p a → p (f a)) {a : α} (ha : p a) (n : ℕ) :
p (f^[n] a) :=
nat.rec ha (λ m, by { rw iterate_succ', exact h _ }) n
lemma iterate.rec_zero (p : α → Sort*) {f : α → α} (h : ∀ a, p a → p (f a)) {a : α} (ha : p a) :
iterate.rec p h ha 0 = ha :=
rfl
variable {f}
theorem left_inverse.iterate {g : α → α} (hg : left_inverse g f) (n : ℕ) :
left_inverse (g^[n]) (f^[n]) :=
nat.rec_on n (λ _, rfl) $ λ n ihn, by { rw [iterate_succ', iterate_succ], exact ihn.comp hg }
theorem right_inverse.iterate {g : α → α} (hg : right_inverse g f) (n : ℕ) :
right_inverse (g^[n]) (f^[n]) :=
hg.iterate n
lemma iterate_comm (f : α → α) (m n : ℕ) : f^[n]^[m] = (f^[m]^[n]) :=
(iterate_mul _ _ _).symm.trans (eq.trans (by rw nat.mul_comm) (iterate_mul _ _ _))
lemma iterate_commute (m n : ℕ) : commute (λ f : α → α, f^[m]) (λ f, f^[n]) :=
λ f, iterate_comm f m n
end function
|
6f3086f1ccd6fed57407a20bcde9362c05e731d9 | a45212b1526d532e6e83c44ddca6a05795113ddc | /src/number_theory/sum_two_squares.lean | 3db8a51133c2ce8ae0325ef116ae41536fd51a4d | [
"Apache-2.0"
] | permissive | fpvandoorn/mathlib | b21ab4068db079cbb8590b58fda9cc4bc1f35df4 | b3433a51ea8bc07c4159c1073838fc0ee9b8f227 | refs/heads/master | 1,624,791,089,608 | 1,556,715,231,000 | 1,556,715,231,000 | 165,722,980 | 5 | 0 | Apache-2.0 | 1,552,657,455,000 | 1,547,494,646,000 | Lean | UTF-8 | Lean | false | false | 3,154 | lean | /-
Copyright (c) 2019 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Chris Hughes
Proof of Fermat's theorem on the sum of two squares. Every prime congruent to 1 mod 4 is the sum
of two squares
-/
import data.zsqrtd.gaussian_int data.zmod.quadratic_reciprocity ring_theory.principal_ideal_domain
open gaussian_int principal_ideal_domain zsqrtd
local notation `ℤ[i]` := gaussian_int
namespace nat
namespace prime
lemma sum_two_squares {p : ℕ} (hp : p.prime) (hp1 : p % 4 = 1) :
∃ a b : ℕ, a ^ 2 + b ^ 2 = p :=
let ⟨k, hk⟩ := (zmodp.exists_pow_two_eq_neg_one_iff_mod_four_ne_three hp).2 $
by rw hp1; exact dec_trivial in
have hpk : p ∣ k.val ^ 2 + 1,
by rw [← zmodp.eq_zero_iff_dvd_nat hp]; simp *,
have hkmul : (k.val ^ 2 + 1 : ℤ[i]) = ⟨k.val, 1⟩ * ⟨k.val, -1⟩ :=
by simp [_root_.pow_two, zsqrtd.ext],
have hpne1 : p ≠ 1, from (ne_of_lt (hp.gt_one)).symm,
have hkltp : 1 + k.val * k.val < p * p,
from calc 1 + k.val * k.val ≤ k.val + k.val * k.val :
add_le_add_right (nat.pos_of_ne_zero
(λ hk0, by clear_aux_decl; simp [*, nat.pow_succ] at *)) _
... = k.val * (k.val + 1) : by simp [mul_add]
... < p * p : mul_lt_mul k.2 k.2 (nat.succ_pos _) (nat.zero_le _),
have hpk₁ : ¬ (p : ℤ[i]) ∣ ⟨k.val, -1⟩ :=
λ ⟨x, hx⟩, lt_irrefl (p * x : ℤ[i]).norm.nat_abs $
calc (norm (p * x : ℤ[i])).nat_abs = (norm ⟨k.val, -1⟩).nat_abs : by rw hx
... < (norm (p : ℤ[i])).nat_abs : by simpa [norm] using hkltp
... ≤ (norm (p * x : ℤ[i])).nat_abs : norm_le_norm_mul_left _
(λ hx0, (show (-1 : ℤ) ≠ 0, from dec_trivial) $
by simpa [hx0] using congr_arg zsqrtd.im hx),
have hpk₂ : ¬ (p : ℤ[i]) ∣ ⟨k.val, 1⟩ :=
λ ⟨x, hx⟩, lt_irrefl (p * x : ℤ[i]).norm.nat_abs $
calc (norm (p * x : ℤ[i])).nat_abs = (norm ⟨k.val, 1⟩).nat_abs : by rw hx
... < (norm (p : ℤ[i])).nat_abs : by simpa [norm] using hkltp
... ≤ (norm (p * x : ℤ[i])).nat_abs : norm_le_norm_mul_left _
(λ hx0, (show (1 : ℤ) ≠ 0, from dec_trivial) $
by simpa [hx0] using congr_arg zsqrtd.im hx),
have hpu : ¬ is_unit (p : ℤ[i]), from mt norm_eq_one_iff.2 $
by rw [norm_nat_cast, int.nat_abs_mul, nat.mul_eq_one_iff];
exact λ h, (ne_of_lt hp.gt_one).symm h.1,
let ⟨y, hy⟩ := hpk in
have hpi : ¬ irreducible (p : ℤ[i]),
from mt irreducible_iff_prime.1
(λ hp, by have := hp.2.2 ⟨k.val, 1⟩ ⟨k.val, -1⟩
⟨y, by rw [← hkmul, ← nat.cast_mul p, ← hy]; simp⟩;
clear_aux_decl; tauto),
have hab : ∃ a b, (p : ℤ[i]) = a * b ∧ ¬ is_unit a ∧ ¬ is_unit b,
by simpa [irreducible, hpu, classical.not_forall, not_or_distrib] using hpi,
let ⟨a, b, hpab, hau, hbu⟩ := hab in
have hnap : (norm a).nat_abs = p, from ((hp.mul_eq_prime_pow_two_iff
(mt norm_eq_one_iff.1 hau) (mt norm_eq_one_iff.1 hbu)).1 $
by rw [← int.coe_nat_inj', int.coe_nat_pow, _root_.pow_two,
← @norm_nat_cast (-1), hpab];
simp).1,
⟨a.re.nat_abs, a.im.nat_abs, by simpa [nat_abs_norm_eq, pow_two] using hnap⟩
end prime
end nat
|
7500f6ec98429695efe36c6e6f42e2fe4e0d8057 | 36c7a18fd72e5b57229bd8ba36493daf536a19ce | /library/data/real/complete.lean | ccf9fefbfe3c89881c6a18ec19d8da00fb7e4c74 | [
"Apache-2.0"
] | permissive | YHVHvx/lean | 732bf0fb7a298cd7fe0f15d82f8e248c11db49e9 | 038369533e0136dd395dc252084d3c1853accbf2 | refs/heads/master | 1,610,701,080,210 | 1,449,128,595,000 | 1,449,128,595,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 30,710 | lean | /-
Copyright (c) 2015 Robert Y. Lewis. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Robert Y. Lewis
The real numbers, constructed as equivalence classes of Cauchy sequences of rationals.
This construction follows Bishop and Bridges (1985).
At this point, we no longer proceed constructively: this file makes heavy use of decidability,
excluded middle, and Hilbert choice. Two sets of definitions of Cauchy sequences, convergence,
etc are available in the libray, one with rates and one without. The definitions here, with rates,
are amenable to be used constructively if and when that development takes place. The second set of
definitions available in /library/theories/analysis/metric_space.lean are the usual classical ones.
Here, we show that ℝ is complete. The proofs of Cauchy completeness and the supremum property
are independent of each other.
-/
import data.real.basic data.real.order data.real.division data.rat data.nat data.pnat
open rat algebra
local postfix ⁻¹ := pnat.inv
open eq.ops pnat classical
namespace rat_seq
theorem rat_approx {s : seq} (H : regular s) :
∀ n : ℕ+, ∃ q : ℚ, ∃ N : ℕ+, ∀ m : ℕ+, m ≥ N → abs (s m - q) ≤ n⁻¹ :=
begin
intro n,
existsi (s (2 * n)),
existsi 2 * n,
intro m Hm,
apply le.trans,
apply H,
rewrite -(add_halves n),
apply algebra.add_le_add_right,
apply inv_ge_of_le Hm
end
theorem rat_approx_seq {s : seq} (H : regular s) :
∀ n : ℕ+, ∃ q : ℚ, s_le (s_abs (sadd s (sneg (const q)))) (const n⁻¹) :=
begin
intro m,
rewrite ↑s_le,
cases rat_approx H m with [q, Hq],
cases Hq with [N, HN],
existsi q,
apply nonneg_of_bdd_within,
repeat (apply reg_add_reg | apply reg_neg_reg | apply abs_reg_of_reg | apply const_reg
| assumption),
intro n,
existsi N,
intro p Hp,
rewrite ↑[sadd, sneg, s_abs, const],
apply le.trans,
rotate 1,
rewrite -sub_eq_add_neg,
apply algebra.sub_le_sub_left,
apply HN,
apply pnat.le_trans,
apply Hp,
rewrite -*pnat.mul_assoc,
apply pnat.mul_le_mul_left,
rewrite [algebra.sub_self, -neg_zero],
apply neg_le_neg,
apply rat.le_of_lt,
apply pnat.inv_pos
end
theorem r_rat_approx (s : reg_seq) :
∀ n : ℕ+, ∃ q : ℚ, r_le (r_abs (radd s (rneg (r_const q)))) (r_const n⁻¹) :=
rat_approx_seq (reg_seq.is_reg s)
theorem const_bound {s : seq} (Hs : regular s) (n : ℕ+) :
s_le (s_abs (sadd s (sneg (const (s n))))) (const n⁻¹) :=
begin
rewrite ↑[s_le, nonneg, s_abs, sadd, sneg, const],
intro m,
rewrite -sub_eq_add_neg,
apply iff.mp !le_add_iff_neg_le_sub_left,
apply le.trans,
apply Hs,
apply algebra.add_le_add_right,
rewrite -*pnat.mul_assoc,
apply inv_ge_of_le,
apply pnat.mul_le_mul_left
end
theorem abs_const (a : ℚ) : const (abs a) ≡ s_abs (const a) :=
by apply equiv.refl
theorem r_abs_const (a : ℚ) : requiv (r_const (abs a) ) (r_abs (r_const a)) := abs_const a
theorem equiv_abs_of_ge_zero {s : seq} (Hs : regular s) (Hz : s_le zero s) : s_abs s ≡ s :=
begin
apply eq_of_bdd,
apply abs_reg_of_reg Hs,
apply Hs,
intro j,
rewrite ↑s_abs,
let Hz' := s_nonneg_of_ge_zero Hs Hz,
existsi 2 * j,
intro n Hn,
cases em (s n ≥ 0) with [Hpos, Hneg],
rewrite [abs_of_nonneg Hpos, algebra.sub_self, abs_zero],
apply rat.le_of_lt,
apply pnat.inv_pos,
let Hneg' := lt_of_not_ge Hneg,
have Hsn : -s n - s n > 0, from add_pos (neg_pos_of_neg Hneg') (neg_pos_of_neg Hneg'),
rewrite [abs_of_neg Hneg', abs_of_pos Hsn],
apply le.trans,
apply add_le_add,
repeat (apply neg_le_neg; apply Hz'),
rewrite neg_neg,
apply le.trans,
apply add_le_add,
repeat (apply inv_ge_of_le; apply Hn),
krewrite pnat.add_halves,
apply le.refl
end
theorem equiv_neg_abs_of_le_zero {s : seq} (Hs : regular s) (Hz : s_le s zero) : s_abs s ≡ sneg s :=
begin
apply eq_of_bdd,
apply abs_reg_of_reg Hs,
apply reg_neg_reg Hs,
intro j,
rewrite [↑s_abs, ↑s_le at Hz],
have Hz' : nonneg (sneg s), begin
apply nonneg_of_nonneg_equiv,
rotate 3,
apply Hz,
rotate 2,
apply s_zero_add,
repeat (apply Hs | apply zero_is_reg | apply reg_neg_reg | apply reg_add_reg)
end,
existsi 2 * j,
intro n Hn,
cases em (s n ≥ 0) with [Hpos, Hneg],
have Hsn : s n + s n ≥ 0, from add_nonneg Hpos Hpos,
rewrite [abs_of_nonneg Hpos, ↑sneg, sub_neg_eq_add, abs_of_nonneg Hsn],
rewrite [↑nonneg at Hz', ↑sneg at Hz'],
apply le.trans,
apply add_le_add,
repeat apply (le_of_neg_le_neg !Hz'),
apply le.trans,
apply add_le_add,
repeat (apply inv_ge_of_le; apply Hn),
krewrite pnat.add_halves,
apply le.refl,
let Hneg' := lt_of_not_ge Hneg,
rewrite [abs_of_neg Hneg', ↑sneg, sub_neg_eq_add, neg_add_eq_sub, algebra.sub_self,
abs_zero],
apply rat.le_of_lt,
apply pnat.inv_pos
end
theorem r_equiv_abs_of_ge_zero {s : reg_seq} (Hz : r_le r_zero s) : requiv (r_abs s) s :=
equiv_abs_of_ge_zero (reg_seq.is_reg s) Hz
theorem r_equiv_neg_abs_of_le_zero {s : reg_seq} (Hz : r_le s r_zero) : requiv (r_abs s) (-s) :=
equiv_neg_abs_of_le_zero (reg_seq.is_reg s) Hz
end rat_seq
namespace real
open [classes] rat_seq
private theorem rewrite_helper9 (a b c : ℝ) : b - c = (b - a) - (c - a) :=
by rewrite [-sub_add_eq_sub_sub_swap, algebra.sub_add_cancel]
private theorem rewrite_helper10 (a b c d : ℝ) : c - d = (c - a) + (a - b) + (b - d) :=
by rewrite [*add_sub, *algebra.sub_add_cancel]
noncomputable definition rep (x : ℝ) : rat_seq.reg_seq := some (quot.exists_rep x)
definition re_abs (x : ℝ) : ℝ :=
quot.lift_on x (λ a, quot.mk (rat_seq.r_abs a))
(take a b Hab, quot.sound (rat_seq.r_abs_well_defined Hab))
theorem r_abs_nonneg {x : ℝ} : zero ≤ x → re_abs x = x :=
quot.induction_on x (λ a Ha, quot.sound (rat_seq.r_equiv_abs_of_ge_zero Ha))
theorem r_abs_nonpos {x : ℝ} : x ≤ zero → re_abs x = -x :=
quot.induction_on x (λ a Ha, quot.sound (rat_seq.r_equiv_neg_abs_of_le_zero Ha))
private theorem abs_const' (a : ℚ) : of_rat (abs a) = re_abs (of_rat a) :=
quot.sound (rat_seq.r_abs_const a)
private theorem re_abs_is_abs : re_abs = abs := funext
(begin
intro x,
apply eq.symm,
cases em (zero ≤ x) with [Hor1, Hor2],
rewrite [abs_of_nonneg Hor1, r_abs_nonneg Hor1],
have Hor2' : x ≤ zero, from algebra.le_of_lt (lt_of_not_ge Hor2),
rewrite [abs_of_neg (lt_of_not_ge Hor2), r_abs_nonpos Hor2']
end)
theorem abs_const (a : ℚ) : of_rat (abs a) = abs (of_rat a) :=
by rewrite -re_abs_is_abs
private theorem rat_approx' (x : ℝ) : ∀ n : ℕ+, ∃ q : ℚ, re_abs (x - of_rat q) ≤ of_rat n⁻¹ :=
quot.induction_on x (λ s n, rat_seq.r_rat_approx s n)
theorem rat_approx (x : ℝ) : ∀ n : ℕ+, ∃ q : ℚ, abs (x - of_rat q) ≤ of_rat n⁻¹ :=
by rewrite -re_abs_is_abs; apply rat_approx'
noncomputable definition approx (x : ℝ) (n : ℕ+) := some (rat_approx x n)
theorem approx_spec (x : ℝ) (n : ℕ+) : abs (x - (of_rat (approx x n))) ≤ of_rat n⁻¹ :=
some_spec (rat_approx x n)
theorem approx_spec' (x : ℝ) (n : ℕ+) : abs ((of_rat (approx x n)) - x) ≤ of_rat n⁻¹ :=
by rewrite abs_sub; apply approx_spec
notation `r_seq` := ℕ+ → ℝ
noncomputable definition converges_to_with_rate (X : r_seq) (a : ℝ) (N : ℕ+ → ℕ+) :=
∀ k : ℕ+, ∀ n : ℕ+, n ≥ N k → abs (X n - a) ≤ of_rat k⁻¹
noncomputable definition cauchy_with_rate (X : r_seq) (M : ℕ+ → ℕ+) :=
∀ k : ℕ+, ∀ m n : ℕ+, m ≥ M k → n ≥ M k → abs (X m - X n) ≤ of_rat k⁻¹
theorem cauchy_with_rate_of_converges_to_with_rate {X : r_seq} {a : ℝ} {N : ℕ+ → ℕ+}
(Hc : converges_to_with_rate X a N) :
cauchy_with_rate X (λ k, N (2 * k)) :=
begin
intro k m n Hm Hn,
rewrite (rewrite_helper9 a),
apply algebra.le.trans,
apply abs_add_le_abs_add_abs,
apply algebra.le.trans,
apply add_le_add,
apply Hc,
apply Hm,
krewrite abs_neg,
apply Hc,
apply Hn,
xrewrite -of_rat_add,
apply of_rat_le_of_rat_of_le,
krewrite pnat.add_halves,
apply rat.le_refl
end
private definition Nb (M : ℕ+ → ℕ+) := λ k, pnat.max (3 * k) (M (2 * k))
private theorem Nb_spec_right (M : ℕ+ → ℕ+) (k : ℕ+) : M (2 * k) ≤ Nb M k := !pnat.max_right
private theorem Nb_spec_left (M : ℕ+ → ℕ+) (k : ℕ+) : 3 * k ≤ Nb M k := !pnat.max_left
section lim_seq
parameter {X : r_seq}
parameter {M : ℕ+ → ℕ+}
hypothesis Hc : cauchy_with_rate X M
include Hc
noncomputable definition lim_seq : ℕ+ → ℚ :=
λ k, approx (X (Nb M k)) (2 * k)
private theorem lim_seq_reg_helper {m n : ℕ+} (Hmn : M (2 * n) ≤M (2 * m)) :
abs (of_rat (lim_seq m) - X (Nb M m)) + abs (X (Nb M m) - X (Nb M n)) + abs
(X (Nb M n) - of_rat (lim_seq n)) ≤ of_rat (m⁻¹ + n⁻¹) :=
begin
apply algebra.le.trans,
apply add_le_add_three,
apply approx_spec',
rotate 1,
apply approx_spec,
rotate 1,
apply Hc,
rotate 1,
apply Nb_spec_right,
rotate 1,
apply pnat.le_trans,
apply Hmn,
apply Nb_spec_right,
krewrite [-+of_rat_add],
change of_rat ((2 * m)⁻¹ + (2 * n)⁻¹ + (2 * n)⁻¹) ≤ of_rat (m⁻¹ + n⁻¹),
rewrite [algebra.add.assoc],
krewrite pnat.add_halves,
apply of_rat_le_of_rat_of_le,
apply add_le_add_right,
apply inv_ge_of_le,
apply pnat.mul_le_mul_left
end
theorem lim_seq_reg : rat_seq.regular lim_seq :=
begin
rewrite ↑rat_seq.regular,
intro m n,
apply le_of_of_rat_le_of_rat,
rewrite [abs_const, of_rat_sub, (rewrite_helper10 (X (Nb M m)) (X (Nb M n)))],
apply algebra.le.trans,
apply abs_add_three,
cases em (M (2 * m) ≥ M (2 * n)) with [Hor1, Hor2],
apply lim_seq_reg_helper Hor1,
let Hor2' := pnat.le_of_lt (pnat.lt_of_not_le Hor2),
krewrite [abs_sub (X (Nb M n)), abs_sub (X (Nb M m)), abs_sub,
rat.add_comm, add_comm_three],
apply lim_seq_reg_helper Hor2'
end
theorem lim_seq_spec (k : ℕ+) :
rat_seq.s_le (rat_seq.s_abs (rat_seq.sadd lim_seq
(rat_seq.sneg (rat_seq.const (lim_seq k))))) (rat_seq.const k⁻¹) :=
by apply rat_seq.const_bound; apply lim_seq_reg
private noncomputable definition r_lim_seq : rat_seq.reg_seq :=
rat_seq.reg_seq.mk lim_seq lim_seq_reg
private theorem r_lim_seq_spec (k : ℕ+) : rat_seq.r_le
(rat_seq.r_abs ((rat_seq.radd r_lim_seq (rat_seq.rneg
(rat_seq.r_const ((rat_seq.reg_seq.sq r_lim_seq) k))))))
(rat_seq.r_const k⁻¹) :=
lim_seq_spec k
noncomputable definition lim : ℝ :=
quot.mk r_lim_seq
theorem re_lim_spec (k : ℕ+) : re_abs (lim - (of_rat (lim_seq k))) ≤ of_rat k⁻¹ :=
r_lim_seq_spec k
theorem lim_spec' (k : ℕ+) : abs (lim - (of_rat (lim_seq k))) ≤ of_rat k⁻¹ :=
by rewrite -re_abs_is_abs; apply re_lim_spec
theorem lim_spec (k : ℕ+) :
abs ((of_rat (lim_seq k)) - lim) ≤ of_rat k⁻¹ :=
by rewrite abs_sub; apply lim_spec'
theorem converges_to_with_rate_of_cauchy_with_rate : converges_to_with_rate X lim (Nb M) :=
begin
intro k n Hn,
rewrite (rewrite_helper10 (X (Nb M n)) (of_rat (lim_seq n))),
apply algebra.le.trans,
apply abs_add_three,
apply algebra.le.trans,
apply add_le_add_three,
apply Hc,
apply pnat.le_trans,
rotate 1,
apply Hn,
rotate_right 1,
apply Nb_spec_right,
have HMk : M (2 * k) ≤ Nb M n, begin
apply pnat.le_trans,
apply Nb_spec_right,
apply pnat.le_trans,
apply Hn,
apply pnat.le_trans,
apply pnat.mul_le_mul_left 3,
apply Nb_spec_left
end,
apply HMk,
rewrite ↑lim_seq,
apply approx_spec,
apply lim_spec,
krewrite [-+of_rat_add],
change of_rat ((2 * k)⁻¹ + (2 * n)⁻¹ + n⁻¹) ≤ of_rat k⁻¹,
apply of_rat_le_of_rat_of_le,
apply algebra.le.trans,
apply add_le_add_three,
apply rat.le_refl,
apply inv_ge_of_le,
apply pnat_mul_le_mul_left',
apply pnat.le_trans,
rotate 1,
apply Hn,
rotate_right 1,
apply Nb_spec_left,
apply inv_ge_of_le,
apply pnat.le_trans,
rotate 1,
apply Hn,
rotate_right 1,
apply Nb_spec_left,
rewrite -*pnat.mul_assoc,
krewrite pnat.p_add_fractions,
apply rat.le_refl
end
end lim_seq
-------------------------------------------
-- int embedding theorems
-- archimedean properties, integer floor and ceiling
section ints
open int
theorem archimedean_upper (x : ℝ) : ∃ z : ℤ, x ≤ of_int z :=
begin
apply quot.induction_on x,
intro s,
cases rat_seq.bdd_of_regular (rat_seq.reg_seq.is_reg s) with [b, Hb],
existsi ubound b,
have H : rat_seq.s_le (rat_seq.reg_seq.sq s) (rat_seq.const (rat.of_nat (ubound b))), begin
apply rat_seq.s_le_of_le_pointwise (rat_seq.reg_seq.is_reg s),
apply rat_seq.const_reg,
intro n,
apply rat.le_trans,
apply Hb,
apply ubound_ge
end,
apply H
end
theorem archimedean_upper_strict (x : ℝ) : ∃ z : ℤ, x < of_int z :=
begin
cases archimedean_upper x with [z, Hz],
existsi z + 1,
apply algebra.lt_of_le_of_lt,
apply Hz,
apply of_int_lt_of_int_of_lt,
apply lt_add_of_pos_right,
apply dec_trivial
end
theorem archimedean_lower (x : ℝ) : ∃ z : ℤ, x ≥ of_int z :=
begin
cases archimedean_upper (-x) with [z, Hz],
existsi -z,
rewrite [of_int_neg],
apply iff.mp !neg_le_iff_neg_le Hz
end
theorem archimedean_lower_strict (x : ℝ) : ∃ z : ℤ, x > of_int z :=
begin
cases archimedean_upper_strict (-x) with [z, Hz],
existsi -z,
rewrite [of_int_neg],
apply iff.mp !neg_lt_iff_neg_lt Hz
end
private definition ex_floor (x : ℝ) :=
(@exists_greatest_of_bdd (λ z, x ≥ of_int z) _
(begin
existsi some (archimedean_upper_strict x),
let Har := some_spec (archimedean_upper_strict x),
intros z Hz,
apply algebra.not_le_of_gt,
apply algebra.lt_of_lt_of_le,
apply Har,
have H : of_int (some (archimedean_upper_strict x)) ≤ of_int z, begin
apply of_int_le_of_int_of_le,
apply Hz
end,
exact H
end)
(by existsi some (archimedean_lower x); apply some_spec (archimedean_lower x)))
noncomputable definition floor (x : ℝ) : ℤ :=
some (ex_floor x)
noncomputable definition ceil (x : ℝ) : ℤ := - floor (-x)
theorem floor_le (x : ℝ) : floor x ≤ x :=
and.left (some_spec (ex_floor x))
theorem lt_of_floor_lt {x : ℝ} {z : ℤ} (Hz : floor x < z) : x < z :=
begin
apply lt_of_not_ge,
cases some_spec (ex_floor x),
apply a_1 _ Hz
end
theorem le_ceil (x : ℝ) : x ≤ ceil x :=
begin
rewrite [↑ceil, of_int_neg],
apply iff.mp !le_neg_iff_le_neg,
apply floor_le
end
theorem lt_of_lt_ceil {x : ℝ} {z : ℤ} (Hz : z < ceil x) : z < x :=
begin
rewrite ↑ceil at Hz,
let Hz' := lt_of_floor_lt (iff.mp !lt_neg_iff_lt_neg Hz),
rewrite [of_int_neg at Hz'],
apply lt_of_neg_lt_neg Hz'
end
theorem floor_succ (x : ℝ) : floor (x + 1) = floor x + 1 :=
begin
apply by_contradiction,
intro H,
cases lt_or_gt_of_ne H with [Hgt, Hlt],
let Hl := lt_of_floor_lt Hgt,
rewrite [of_int_add at Hl],
apply not_le_of_gt (lt_of_add_lt_add_right Hl) !floor_le,
let Hl := lt_of_floor_lt (iff.mp !add_lt_iff_lt_sub_right Hlt),
rewrite [of_int_sub at Hl],
apply not_le_of_gt (iff.mpr !add_lt_iff_lt_sub_right Hl) !floor_le
end
theorem floor_sub_one_lt_floor (x : ℝ) : floor (x - 1) < floor x :=
begin
apply @algebra.lt_of_add_lt_add_right ℤ _ _ 1,
rewrite [-floor_succ (x - 1), sub_add_cancel],
apply lt_add_of_pos_right dec_trivial
end
theorem ceil_lt_ceil_succ (x : ℝ) : ceil x < ceil (x + 1) :=
begin
rewrite [↑ceil, neg_add],
apply neg_lt_neg,
apply floor_sub_one_lt_floor
end
open nat
theorem archimedean_small {ε : ℝ} (H : ε > 0) : ∃ (n : ℕ), 1 / succ n < ε :=
let n := int.nat_abs (ceil (2 / ε)) in
assert int.of_nat n ≥ ceil (2 / ε),
by rewrite of_nat_nat_abs; apply le_abs_self,
have int.of_nat (succ n) ≥ ceil (2 / ε),
begin apply algebra.le.trans, exact this, apply int.of_nat_le_of_nat_of_le, apply le_succ end,
have H₁ : succ n ≥ ceil (2 / ε), from of_int_le_of_int_of_le this,
have H₂ : succ n ≥ 2 / ε, from !algebra.le.trans !le_ceil H₁,
have H₃ : 2 / ε > 0, from div_pos_of_pos_of_pos two_pos H,
have 1 / succ n < ε, from calc
1 / succ n ≤ 1 / (2 / ε) : one_div_le_one_div_of_le H₃ H₂
... = ε / 2 : one_div_div
... < ε : div_two_lt_of_pos H,
exists.intro n this
end ints
--------------------------------------------------
-- supremum property
-- this development roughly follows the proof of completeness done in Isabelle.
-- It does not depend on the previous proof of Cauchy completeness. Much of the same
-- machinery can be used to show that Cauchy completeness implies the supremum property.
section supremum
open prod nat
local postfix `~` := nat_of_pnat
-- The top part of this section could be refactored. What is the appropriate place to define
-- bounds, supremum, etc? In algebra/ordered_field? They potentially apply to more than just ℝ.
parameter X : ℝ → Prop
definition ub (x : ℝ) := ∀ y : ℝ, X y → y ≤ x
definition is_sup (x : ℝ) := ub x ∧ ∀ y : ℝ, ub y → x ≤ y
definition lb (x : ℝ) := ∀ y : ℝ, X y → x ≤ y
definition is_inf (x : ℝ) := lb x ∧ ∀ y : ℝ, lb y → y ≤ x
parameter elt : ℝ
hypothesis inh : X elt
parameter bound : ℝ
hypothesis bdd : ub bound
include inh bdd
private definition avg (a b : ℚ) := a / 2 + b / 2
private noncomputable definition bisect (ab : ℚ × ℚ) :=
if ub (avg (pr1 ab) (pr2 ab)) then
(pr1 ab, (avg (pr1 ab) (pr2 ab)))
else
(avg (pr1 ab) (pr2 ab), pr2 ab)
private noncomputable definition under : ℚ := rat.of_int (floor (elt - 1))
private theorem under_spec1 : of_rat under < elt :=
have H : of_rat under < of_int (floor elt), begin
apply of_int_lt_of_int_of_lt,
apply floor_sub_one_lt_floor
end,
algebra.lt_of_lt_of_le H !floor_le
private theorem under_spec : ¬ ub under :=
begin
rewrite ↑ub,
apply not_forall_of_exists_not,
existsi elt,
apply iff.mpr not_implies_iff_and_not,
apply and.intro,
apply inh,
apply not_le_of_gt under_spec1
end
private noncomputable definition over : ℚ := rat.of_int (ceil (bound + 1)) -- b
private theorem over_spec1 : bound < of_rat over :=
have H : of_int (ceil bound) < of_rat over, begin
apply of_int_lt_of_int_of_lt,
apply ceil_lt_ceil_succ
end,
algebra.lt_of_le_of_lt !le_ceil H
private theorem over_spec : ub over :=
begin
rewrite ↑ub,
intro y Hy,
apply algebra.le_of_lt,
apply algebra.lt_of_le_of_lt,
apply bdd,
apply Hy,
apply over_spec1
end
private noncomputable definition under_seq := λ n : ℕ, pr1 (iterate bisect n (under, over)) -- A
private noncomputable definition over_seq := λ n : ℕ, pr2 (iterate bisect n (under, over)) -- B
private noncomputable definition avg_seq := λ n : ℕ, avg (over_seq n) (under_seq n) -- C
private theorem avg_symm (n : ℕ) : avg_seq n = avg (under_seq n) (over_seq n) :=
by rewrite [↑avg_seq, ↑avg, add.comm]
private theorem over_0 : over_seq 0 = over := rfl
private theorem under_0 : under_seq 0 = under := rfl
private theorem succ_helper (n : ℕ) :
avg (pr1 (iterate bisect n (under, over))) (pr2 (iterate bisect n (under, over))) = avg_seq n :=
by rewrite avg_symm
private theorem under_succ (n : ℕ) : under_seq (succ n) =
(if ub (avg_seq n) then under_seq n else avg_seq n) :=
begin
cases em (ub (avg_seq n)) with [Hub, Hub],
rewrite [if_pos Hub],
have H : pr1 (bisect (iterate bisect n (under, over))) = under_seq n, by
rewrite [↑under_seq, ↑bisect at {2}, -succ_helper at Hub, if_pos Hub],
apply H,
rewrite [if_neg Hub],
have H : pr1 (bisect (iterate bisect n (under, over))) = avg_seq n, by
rewrite [↑bisect at {2}, -succ_helper at Hub, if_neg Hub, avg_symm],
apply H
end
private theorem over_succ (n : ℕ) : over_seq (succ n) =
(if ub (avg_seq n) then avg_seq n else over_seq n) :=
begin
cases em (ub (avg_seq n)) with [Hub, Hub],
rewrite [if_pos Hub],
have H : pr2 (bisect (iterate bisect n (under, over))) = avg_seq n, by
rewrite [↑bisect at {2}, -succ_helper at Hub, if_pos Hub, avg_symm],
apply H,
rewrite [if_neg Hub],
have H : pr2 (bisect (iterate bisect n (under, over))) = over_seq n, by
rewrite [↑over_seq, ↑bisect at {2}, -succ_helper at Hub, if_neg Hub],
apply H
end
private theorem nat.zero_eq_0 : (zero : ℕ) = 0 := rfl
private theorem width (n : ℕ) : over_seq n - under_seq n = (over - under) / ((2^n) : ℚ) :=
nat.induction_on n
(by xrewrite [nat.zero_eq_0, over_0, under_0, pow_zero, div_one])
(begin
intro a Ha,
rewrite [over_succ, under_succ],
let Hou := calc
(over_seq a) / 2 - (under_seq a) / 2 = ((over - under) / 2^a) / 2 :
by rewrite [div_sub_div_same, Ha]
... = (over - under) / ((2^a) * 2) : by rewrite div_div_eq_div_mul
... = (over - under) / 2^(a + 1) : by rewrite pow_add,
cases em (ub (avg_seq a)),
rewrite [*if_pos a_1, -add_one, -Hou, ↑avg_seq, ↑avg, sub_eq_add_neg, algebra.add.assoc, -sub_eq_add_neg, div_two_sub_self],
rewrite [*if_neg a_1, -add_one, -Hou, ↑avg_seq, ↑avg, sub_add_eq_sub_sub,
algebra.sub_self_div_two]
end)
private theorem width_narrows : ∃ n : ℕ, over_seq n - under_seq n ≤ 1 :=
begin
cases binary_bound (over - under) with [a, Ha],
existsi a,
rewrite (width a),
apply div_le_of_le_mul,
apply pow_pos dec_trivial,
rewrite rat.mul_one,
apply Ha
end
private noncomputable definition over' := over_seq (some width_narrows)
private noncomputable definition under' := under_seq (some width_narrows)
private noncomputable definition over_seq' := λ n, over_seq (n + some width_narrows)
private noncomputable definition under_seq' := λ n, under_seq (n + some width_narrows)
private theorem over_seq'0 : over_seq' 0 = over' :=
by rewrite [↑over_seq', nat.zero_add]
private theorem under_seq'0 : under_seq' 0 = under' :=
by rewrite [↑under_seq', nat.zero_add]
private theorem under_over' : over' - under' ≤ 1 := some_spec width_narrows
private theorem width' (n : ℕ) : over_seq' n - under_seq' n ≤ 1 / 2^n :=
nat.induction_on n
(begin
xrewrite [nat.zero_eq_0, over_seq'0, under_seq'0, pow_zero, div_one],
apply under_over'
end)
(begin
intros a Ha,
rewrite [↑over_seq' at *, ↑under_seq' at *, *succ_add at *, width at *,
-add_one, -(add_one a), pow_add, pow_add _ a 1, *pow_one],
apply div_mul_le_div_mul_of_div_le_div_pos' Ha dec_trivial
end)
private theorem PA (n : ℕ) : ¬ ub (under_seq n) :=
nat.induction_on n
(by rewrite under_0; apply under_spec)
(begin
intro a Ha,
rewrite under_succ,
cases em (ub (avg_seq a)),
rewrite (if_pos a_1),
assumption,
rewrite (if_neg a_1),
assumption
end)
private theorem PB (n : ℕ) : ub (over_seq n) :=
nat.induction_on n
(by rewrite over_0; apply over_spec)
(begin
intro a Ha,
rewrite over_succ,
cases em (ub (avg_seq a)),
rewrite (if_pos a_1),
assumption,
rewrite (if_neg a_1),
assumption
end)
private theorem under_lt_over : under < over :=
begin
cases exists_not_of_not_forall under_spec with [x, Hx],
cases iff.mp not_implies_iff_and_not Hx with [HXx, Hxu],
apply lt_of_of_rat_lt_of_rat,
apply algebra.lt_of_lt_of_le,
apply lt_of_not_ge Hxu,
apply over_spec _ HXx
end
private theorem under_seq_lt_over_seq : ∀ m n : ℕ, under_seq m < over_seq n :=
begin
intros,
cases exists_not_of_not_forall (PA m) with [x, Hx],
cases iff.mp not_implies_iff_and_not Hx with [HXx, Hxu],
apply lt_of_of_rat_lt_of_rat,
apply algebra.lt_of_lt_of_le,
apply lt_of_not_ge Hxu,
apply PB,
apply HXx
end
private theorem under_seq_lt_over_seq_single : ∀ n : ℕ, under_seq n < over_seq n :=
by intros; apply under_seq_lt_over_seq
private theorem under_seq'_lt_over_seq' : ∀ m n : ℕ, under_seq' m < over_seq' n :=
by intros; apply under_seq_lt_over_seq
private theorem under_seq'_lt_over_seq'_single : ∀ n : ℕ, under_seq' n < over_seq' n :=
by intros; apply under_seq_lt_over_seq
private theorem under_seq_mono_helper (i k : ℕ) : under_seq i ≤ under_seq (i + k) :=
(nat.induction_on k
(by rewrite nat.add_zero; apply rat.le_refl)
(begin
intros a Ha,
rewrite [add_succ, under_succ],
cases em (ub (avg_seq (i + a))) with [Havg, Havg],
rewrite (if_pos Havg),
apply Ha,
rewrite [if_neg Havg, ↑avg_seq, ↑avg],
apply rat.le_trans,
apply Ha,
rewrite -add_halves at {1},
apply add_le_add_right,
apply div_le_div_of_le_of_pos,
apply rat.le_of_lt,
apply under_seq_lt_over_seq,
apply dec_trivial
end))
private theorem under_seq_mono (i j : ℕ) (H : i ≤ j) : under_seq i ≤ under_seq j :=
begin
cases le.elim H with [k, Hk'],
rewrite -Hk',
apply under_seq_mono_helper
end
private theorem over_seq_mono_helper (i k : ℕ) : over_seq (i + k) ≤ over_seq i :=
nat.induction_on k
(by rewrite nat.add_zero; apply rat.le_refl)
(begin
intros a Ha,
rewrite [add_succ, over_succ],
cases em (ub (avg_seq (i + a))) with [Havg, Havg],
rewrite [if_pos Havg, ↑avg_seq, ↑avg],
apply rat.le_trans,
rotate 1,
apply Ha,
rotate 1,
apply add_le_of_le_sub_left,
rewrite sub_self_div_two,
apply div_le_div_of_le_of_pos,
apply rat.le_of_lt,
apply under_seq_lt_over_seq,
apply dec_trivial,
rewrite [if_neg Havg],
apply Ha
end)
private theorem over_seq_mono (i j : ℕ) (H : i ≤ j) : over_seq j ≤ over_seq i :=
begin
cases le.elim H with [k, Hk'],
rewrite -Hk',
apply over_seq_mono_helper
end
private theorem rat_power_two_inv_ge (k : ℕ+) : 1 / 2^k~ ≤ k⁻¹ :=
one_div_le_one_div_of_le !rat_of_pnat_is_pos !rat_power_two_le
open rat_seq
private theorem regular_lemma_helper {s : seq} {m n : ℕ+} (Hm : m ≤ n)
(H : ∀ n i : ℕ+, i ≥ n → under_seq' n~ ≤ s i ∧ s i ≤ over_seq' n~) :
abs (s m - s n) ≤ m⁻¹ + n⁻¹ :=
begin
cases H m n Hm with [T1under, T1over],
cases H m m (!pnat.le_refl) with [T2under, T2over],
apply rat.le_trans,
apply dist_bdd_within_interval,
apply under_seq'_lt_over_seq'_single,
rotate 1,
repeat assumption,
apply rat.le_trans,
apply width',
apply rat.le_trans,
apply rat_power_two_inv_ge,
apply le_add_of_nonneg_right,
apply rat.le_of_lt (!pnat.inv_pos)
end
private theorem regular_lemma (s : seq) (H : ∀ n i : ℕ+, i ≥ n → under_seq' n~ ≤ s i ∧ s i ≤ over_seq' n~) :
regular s :=
begin
rewrite ↑regular,
intros,
cases em (m ≤ n) with [Hm, Hn],
apply regular_lemma_helper Hm H,
let T := regular_lemma_helper (pnat.le_of_lt (pnat.lt_of_not_le Hn)) H,
rewrite [abs_sub at T, {n⁻¹ + _}add.comm at T],
exact T
end
private noncomputable definition p_under_seq : seq := λ n : ℕ+, under_seq' n~
private noncomputable definition p_over_seq : seq := λ n : ℕ+, over_seq' n~
private theorem under_seq_regular : regular p_under_seq :=
begin
apply regular_lemma,
intros n i Hni,
apply and.intro,
apply under_seq_mono,
apply add_le_add_right,
apply Hni,
apply rat.le_of_lt,
apply under_seq_lt_over_seq
end
private theorem over_seq_regular : regular p_over_seq :=
begin
apply regular_lemma,
intros n i Hni,
apply and.intro,
apply rat.le_of_lt,
apply under_seq_lt_over_seq,
apply over_seq_mono,
apply add_le_add_right,
apply Hni
end
private noncomputable definition sup_over : ℝ := quot.mk (reg_seq.mk p_over_seq over_seq_regular)
private noncomputable definition sup_under : ℝ := quot.mk (reg_seq.mk p_under_seq under_seq_regular)
private theorem over_bound : ub sup_over :=
begin
rewrite ↑ub,
intros y Hy,
apply le_of_le_reprs,
intro n,
apply PB,
apply Hy
end
private theorem under_lowest_bound : ∀ y : ℝ, ub y → sup_under ≤ y :=
begin
intros y Hy,
apply le_of_reprs_le,
intro n,
cases exists_not_of_not_forall (PA _) with [x, Hx],
cases iff.mp not_implies_iff_and_not Hx with [HXx, Hxn],
apply algebra.le.trans,
apply algebra.le_of_lt,
apply lt_of_not_ge Hxn,
apply Hy,
apply HXx
end
private theorem under_over_equiv : p_under_seq ≡ p_over_seq :=
begin
intros,
apply rat.le_trans,
have H : p_under_seq n < p_over_seq n, from !under_seq_lt_over_seq,
rewrite [abs_of_neg (iff.mpr !sub_neg_iff_lt H), neg_sub],
apply width',
apply rat.le_trans,
apply rat_power_two_inv_ge,
apply le_add_of_nonneg_left,
apply rat.le_of_lt !pnat.inv_pos
end
private theorem under_over_eq : sup_under = sup_over := quot.sound under_over_equiv
theorem exists_is_sup_of_inh_of_bdd : ∃ x : ℝ, is_sup x :=
exists.intro sup_over (and.intro over_bound (under_over_eq ▸ under_lowest_bound))
end supremum
definition bounding_set (X : ℝ → Prop) (x : ℝ) : Prop := ∀ y : ℝ, X y → x ≤ y
theorem exists_is_inf_of_inh_of_bdd (X : ℝ → Prop) (elt : ℝ) (inh : X elt) (bound : ℝ)
(bdd : lb X bound) : ∃ x : ℝ, is_inf X x :=
begin
have Hinh : bounding_set X bound, begin
intros y Hy,
apply bdd,
apply Hy
end,
have Hub : ub (bounding_set X) elt, begin
intros y Hy,
apply Hy,
apply inh
end,
cases exists_is_sup_of_inh_of_bdd _ _ Hinh _ Hub with [supr, Hsupr],
existsi supr,
cases Hsupr with [Hubs1, Hubs2],
apply and.intro,
intros,
apply Hubs2,
intros z Hz,
apply Hz,
apply a,
intros y Hlby,
apply Hubs1,
intros z Hz,
apply Hlby,
apply Hz
end
end real
|
4437abc9bbfbaa03146a64e04109671ba6cd75e0 | ba4794a0deca1d2aaa68914cd285d77880907b5c | /src/solutions/world1_addition.lean | 250180a1e4e3f93690f49cb21659210d2b874969 | [
"Apache-2.0"
] | permissive | ChrisHughes24/natural_number_game | c7c00aa1f6a95004286fd456ed13cf6e113159ce | 9d09925424da9f6275e6cfe427c8bcf12bb0944f | refs/heads/master | 1,600,715,773,528 | 1,573,910,462,000 | 1,573,910,462,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 6,403 | lean | import mynat.definition -- Imports the natural numbers.
/- Here's what you get from the import:
1) The following data:
* a type called `mynat`
* a term `0 : mynat`, interpreted as the number zero.
* a function `succ : mynat → mynat`, with `succ n` interpreted as "the number after n".
* Usual numerical notation 0,1,2,3,4,5 etc.
2) The following axioms:
* `zero_ne_succ : ∀ (a : mynat), zero ≠ succ(a)`, the statement that zero isn't a successor.
-- this ensures that there is more than one natural number.
* `succ_inj : ∀ {a b : mynat}, succ(a) = succ(b) → a = b`, the statement that
if succ(a) = succ(b) then a = b.
-- this ensures that there are infinitely many natural numbers.
3) The principle of mathematical induction.
* In practice this means that if you have `n : mynat` then you can use the tactic `induction n`.
4) A few useful extra things:
* The theorem `one_eq_succ_zero : 1 = succ 0`
* The theorem `ne_iff_implies_false : a ≠ b ↔ (a = b) → false`
-/
import mynat.add -- definition of addition
/- Here's what you get from the import:
1) The following data:
* a function called mynat.add, and notation a + b for this function
2) The following axioms:
* `add_zero : ∀ a : mynat, a + 0 = a`
* `add_succ : ∀ a b : mynat, a + succ(b) = succ(a + b)`
These axiom between them tell you how to work out a + x for every x; use induction on x to
reduce to the case either `x = 0` or `x = succ b`, and then use `add_zero` or `add_succ` appropriately.
-/
namespace mynat
-- Summary:
-- Naturals:
-- 1) 0 and succ are constants
-- 2) succ_inj and zero_ne_succ are axioms
-- 3) Induction works.
-- Addition:
-- 1) add_zero and add_succ are the axioms
-- 2) notation is a + b
/-
Collectibles in this level:
add_comm_monoid -- collectible_02
add_monoid [zero_add] -- collectible_01
(has_zero)
add_semigroup [add_assoc]
(has_add)
add_comm_semigroup [add_comm]
add_semigroup (see above)
-/
/-
Instructions: First carefully explain definition of nat and add. Then
guide them through the first level.
"We're going to prove this by induction on n, which is a natural
thing to do because we defined addition by recursion on n (you
prove things by induction and define them by recursion).
For the base case, we are going to use the axiom that a + 0 = 0.
refl closes a goal of the form x = x. how to use add_succ here?
etc.
Full solution to zero_add:
induction n with d hd,
rw add_zero,
refl,
rw add_succ,
rw hd,
refl,
"
-/
lemma zero_add (n : mynat) : 0 + n = n :=
begin [less_leaky]
induction n with d hd,
{
rw add_zero,
refl,
},
{ rw add_succ,
rw hd,
refl
}
end
lemma add_assoc (a b c : mynat) : (a + b) + c = a + (b + c) :=
begin [less_leaky]
induction c with d hd,
{ -- ⊢ a + b + 0 = a + (b + 0)
rw add_zero,
rw add_zero,
refl
},
{ -- ⊢ (a + b) + succ d = a + (b + succ d)
rw add_succ,
rw add_succ,
rw add_succ,
rw hd,
refl,
}
end
-- first point: needs add_assoc, zero_add, add_zero
def collectible_01 : add_monoid mynat := by structure_helper
--#print axioms collectible_01 -- prove you got this by uncommenting
-- proving add_comm immediately is still tricky; trying it
-- reveals a natural intermediate lemma which we prove first.
lemma succ_add (a b : mynat) : succ a + b = succ (a + b) :=
begin [less_leaky]
induction b with d hd,
{
refl
},
{ rw add_succ,
rw hd,
rw add_succ,
refl
}
end
lemma add_comm (a b : mynat) : a + b = b + a :=
begin [less_leaky]
induction b with d hd,
{ -- ⊢ a + 0 = 0 + a,
rw zero_add,
rw add_zero,
refl
},
{
rw add_succ,
rw hd,
rw succ_add,
refl
}
end
-- level up
def collectible_02 : add_comm_monoid mynat := by structure_helper
--#print axioms collectible_02
-- no more collectibles beyond this point in this file, however
-- stuff below is used in other collectibles in other files.
theorem succ_ne_zero : ∀ {{a : mynat}}, succ a ≠ 0 :=
begin [less_leaky]
intro a,
symmetry,
exact zero_ne_succ a,
end
theorem eq_iff_succ_eq_succ (a b : mynat) : succ a = succ b ↔ a = b :=
begin [less_leaky]
split,
{ exact succ_inj},
{ intro H,
rw H,
refl,
}
end
theorem succ_eq_add_one (n : mynat) : succ n = n + 1 :=
begin [less_leaky]
rw one_eq_succ_zero,
rw add_succ,
rw add_zero,
refl,
end
lemma add_right_comm (a b c : mynat) : a + b + c = a + c + b :=
begin [less_leaky]
rw add_assoc,
rw add_comm b c,
rw ←add_assoc,
refl,
end
theorem add_left_cancel ⦃ a b c : mynat⦄ : a + b = a + c → b = c :=
begin [less_leaky]
intro h,
rw add_comm at h,
rw add_comm a at h,
revert b c,
induction a with d hd,
{ intros b c,
intro h,
rw add_zero at h,
rw add_zero at h,
assumption
},
{ intros b c,
intro h,
rw add_succ at h,
rw add_succ at h,
rw ←succ_add at h,
rw ←succ_add at h,
apply succ_inj,
exact hd h
}
end
theorem add_right_cancel ⦃a b c : mynat⦄ : a + b = c + b → a = c :=
begin [less_leaky]
intro h,
rw add_comm at h,
rw add_comm c at h,
exact add_left_cancel h
end
theorem add_right_cancel_iff (t a b : mynat) : a + t = b + t ↔ a = b :=
begin [less_leaky]
split,
{ apply add_right_cancel}, -- done that way already,
{ intro H, -- H : a = b,
rw H,
refl,
}
end
-- this is used for antisymmetry of ≤
lemma eq_zero_of_add_right_eq_self {{a b : mynat}} : a + b = a → b = 0 :=
begin [less_leaky]
intro h,
induction a with a ha,
{
rw zero_add at h,
assumption
},
{ apply ha,
apply succ_inj,
rw succ_add at h,
assumption,
}
end
-- now used for antisymmetry of ≤
lemma add_left_eq_zero {{a b : mynat}} : a + b = 0 → b = 0 :=
begin [less_leaky]
intro H,
cases b with c,
{ refl},
{ rw add_succ at H,
exfalso,
apply zero_ne_succ (a + c),
rw H,
refl,
},
end
lemma add_right_eq_zero {{a b : mynat}} : a + b = 0 → a = 0 :=
begin [less_leaky]
intro H,
rw add_comm at H,
exact add_left_eq_zero H,
end
theorem add_one_eq_succ (d : mynat) : d + 1 = succ d :=
begin [less_leaky]
rw succ_eq_add_one,
refl,
end
def ne_succ_self (n : mynat) : n ≠ succ n :=
begin [less_leaky]
induction n with d hd,
apply zero_ne_succ,
intro hs,
apply hd,
apply succ_inj,
assumption
end
end mynat
-- |
5b9981ff7d1bb7299e7fca84e1872dfa47fdbda0 | 55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5 | /src/tactic/linarith/datatypes.lean | e6be83348097804ce6db4f4dec852598978650bd | [
"Apache-2.0"
] | permissive | dupuisf/mathlib | 62de4ec6544bf3b79086afd27b6529acfaf2c1bb | 8582b06b0a5d06c33ee07d0bdf7c646cae22cf36 | refs/heads/master | 1,669,494,854,016 | 1,595,692,409,000 | 1,595,692,409,000 | 272,046,630 | 0 | 0 | Apache-2.0 | 1,592,066,143,000 | 1,592,066,142,000 | null | UTF-8 | Lean | false | false | 11,114 | lean | /-
Copyright (c) 2020 Robert Y. Lewis. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Robert Y. Lewis
-/
import meta.rb_map
import tactic.ring
import tactic.linarith.lemmas
/-!
# Datatypes for `linarith`
Some of the data structures here are used in multiple parts of the tactic.
We split them into their own file.
This file also contains a few convenient auxiliary functions.
-/
declare_trace linarith
open native
namespace linarith
/-- A shorthand for tracing when the `trace.linarith` option is set to true. -/
meta def linarith_trace {α} [has_to_tactic_format α] (s : α) : tactic unit :=
tactic.when_tracing `linarith (tactic.trace s)
/--
A shorthand for tracing the types of a list of proof terms
when the `trace.linarith` option is set to true.
-/
meta def linarith_trace_proofs (s : string := "") (l : list expr) : tactic unit :=
tactic.when_tracing `linarith $ do
tactic.trace s, l.mmap tactic.infer_type >>= tactic.trace
/-! ### Linear expressions -/
/--
A linear expression is a list of pairs of variable indices and coefficients,
representing the sum of the products of each coefficient with its corresponding variable.
Some functions on `linexp` assume that `n : ℕ` occurs at most once as the first element of a pair,
and that the list is sorted in decreasing order of the first argument.
This is not enforced by the type but the operations here preserve it.
-/
@[reducible]
def linexp : Type := list (ℕ × ℤ)
namespace linexp
/--
Add two `linexp`s together componentwise.
Preserves sorting and uniqueness of the first argument.
-/
meta def add : linexp → linexp → linexp
| [] a := a
| a [] := a
| (a@(n1,z1)::t1) (b@(n2,z2)::t2) :=
if n1 < n2 then b::add (a::t1) t2
else if n2 < n1 then a::add t1 (b::t2)
else let sum := z1 + z2 in if sum = 0 then add t1 t2 else (n1, sum)::add t1 t2
/-- `l.scale c` scales the values in `l` by `c` without modifying the order or keys. -/
def scale (c : ℤ) (l : linexp) : linexp :=
if c = 0 then []
else if c = 1 then l
else l.map $ λ ⟨n, z⟩, (n, z*c)
/--
`l.get n` returns the value in `l` associated with key `n`, if it exists, and `none` otherwise.
This function assumes that `l` is sorted in decreasing order of the first argument,
that is, it will return `none` as soon as it finds a key smaller than `n`.
-/
def get (n : ℕ) : linexp → option ℤ
| [] := none
| ((a, b)::t) :=
if a < n then none
else if a = n then some b
else get t
/--
`l.contains n` is true iff `n` is the first element of a pair in `l`.
-/
def contains (n : ℕ) : linexp → bool := option.is_some ∘ get n
/--
`l.zfind n` returns the value associated with key `n` if there is one, and 0 otherwise.
-/
def zfind (n : ℕ) (l : linexp) : ℤ :=
match l.get n with
| none := 0
| some v := v
end
/-- `l.vars` returns the list of variables that occur in `l`. -/
def vars (l : linexp) : list ℕ :=
l.map prod.fst
/--
Defines a lex ordering on `linexp`. This function is performance critical.
-/
def cmp : linexp → linexp → ordering
| [] [] := ordering.eq
| [] _ := ordering.lt
| _ [] := ordering.gt
| ((n1,z1)::t1) ((n2,z2)::t2) :=
if n1 < n2 then ordering.lt
else if n2 < n1 then ordering.gt
else if z1 < z2 then ordering.lt
else if z2 < z1 then ordering.gt
else cmp t1 t2
end linexp
/-! ### Inequalities -/
/-- The three-element type `ineq` is used to represent the strength of a comparison between terms. -/
@[derive decidable_eq, derive inhabited]
inductive ineq : Type
| eq | le | lt
namespace ineq
/--
`max R1 R2` computes the strength of the sum of two inequalities. If `t1 R1 0` and `t2 R2 0`,
then `t1 + t2 (max R1 R2) 0`.
-/
def max : ineq → ineq → ineq
| lt a := lt
| a lt := lt
| le a := le
| a le := le
| eq eq := eq
/-- `ineq` is ordered `eq < le < lt`. -/
def cmp : ineq → ineq → ordering
| eq eq := ordering.eq
| eq _ := ordering.lt
| le le := ordering.eq
| le lt := ordering.lt
| lt lt := ordering.eq
| _ _ := ordering.gt
/-- Prints an `ineq` as the corresponding infix symbol. -/
def to_string : ineq → string
| eq := "="
| le := "≤"
| lt := "<"
/-- Finds the name of a multiplicative lemma corresponding to an inequality strength. -/
meta def to_const_mul_nm : ineq → name
| lt := ``mul_neg
| le := ``mul_nonpos
| eq := ``mul_eq
instance : has_to_string ineq := ⟨ineq.to_string⟩
meta instance : has_to_format ineq := ⟨λ i, ineq.to_string i⟩
end ineq
/-! ### Comparisons with 0 -/
/--
The main datatype for FM elimination.
Variables are represented by natural numbers, each of which has an integer coefficient.
Index 0 is reserved for constants, i.e. `coeffs.find 0` is the coefficient of 1.
The represented term is `coeffs.sum (λ ⟨k, v⟩, v * Var[k])`.
str determines the strength of the comparison -- is it < 0, ≤ 0, or = 0?
-/
@[derive inhabited]
structure comp : Type :=
(str : ineq)
(coeffs : linexp)
/-- `c.vars` returns the list of variables that appear in the linear expression contained in `c`. -/
def comp.vars : comp → list ℕ :=
linexp.vars ∘ comp.coeffs
/-- `comp.coeff_of c a` projects the coefficient of variable `a` out of `c`. -/
def comp.coeff_of (c : comp) (a : ℕ) : ℤ :=
c.coeffs.zfind a
/-- `comp.scale c n` scales the coefficients of `c` by `n`. -/
def comp.scale (c : comp) (n : ℕ) : comp :=
{ c with coeffs := c.coeffs.scale n }
/--
`comp.add c1 c2` adds the expressions represented by `c1` and `c2`.
The coefficient of variable `a` in `c1.add c2`
is the sum of the coefficients of `a` in `c1` and `c2`.
-/
meta def comp.add (c1 c2 : comp) : comp :=
⟨c1.str.max c2.str, c1.coeffs.add c2.coeffs⟩
/-- `comp` has a lex order. First the `ineq`s are compared, then the `coeff`s. -/
meta def comp.cmp : comp → comp → ordering
| ⟨str1, coeffs1⟩ ⟨str2, coeffs2⟩ :=
match str1.cmp str2 with
| ordering.lt := ordering.lt
| ordering.gt := ordering.gt
| ordering.eq := coeffs1.cmp coeffs2
end
/--
A `comp` represents a contradiction if its expression has no coefficients and its strength is <,
that is, it represents the fact `0 < 0`.
-/
meta def comp.is_contr (c : comp) : bool := c.coeffs.empty ∧ c.str = ineq.lt
meta instance comp.to_format : has_to_format comp :=
⟨λ p, to_fmt p.coeffs ++ to_string p.str ++ "0"⟩
/-! ### Parsing into linear form -/
/-! ### Control -/
/--
A preprocessor transforms a proof of a proposition into a proof of a different propositon.
The return type is `list expr`, since some preprocessing steps may create multiple new hypotheses,
and some may remove a hypothesis from the list.
A "no-op" preprocessor should return its input as a singleton list.
-/
meta structure preprocessor : Type :=
(name : string)
(transform : expr → tactic (list expr))
/--
Some preprocessors need to examine the full list of hypotheses instead of working item by item.
As with `preprocessor`, the input to a `global_preprocessor` is replaced by, not added to, its output.
-/
meta structure global_preprocessor : Type :=
(name : string)
(transform : list expr → tactic (list expr))
/--
A `preprocessor` lifts to a `global_preprocessor` by folding it over the input list.
-/
meta def preprocessor.globalize (pp : preprocessor) : global_preprocessor :=
{ name := pp.name,
transform := list.mfoldl (λ ret e, do l' ← pp.transform e, return (l' ++ ret)) [] }
/--
`process pp l` runs `pp.transform` on `l` and returns the result,
tracing the result if `trace.linarith` is on.
-/
meta def global_preprocessor.process (pp : global_preprocessor) (l : list expr) :
tactic (list expr) :=
do l ← pp.transform l,
linarith_trace_proofs (to_string format!"Preprocessing: {pp.name}") l,
return l
meta instance : has_coe preprocessor global_preprocessor :=
⟨preprocessor.globalize⟩
/--
A `certificate_oracle` is a function `produce_certificate : list comp → ℕ → tactic (rb_map ℕ ℕ)`.
`produce_certificate hyps max_var` tries to derive a contradiction from the comparisons in `hyps`
by eliminating all variables ≤ `max_var`.
If successful, it returns a map `coeff : ℕ → ℕ` as a certificate.
This map represents that we can find a contradiction by taking the sum `∑ (coeff i) * hyps[i]`.
The default `certificate_oracle` used by `linarith` is `linarith.fourier_motzkin.produce_certificate`
-/
meta def certificate_oracle : Type :=
list comp → ℕ → tactic (rb_map ℕ ℕ)
/-- A configuration object for `linarith`. -/
meta structure linarith_config : Type :=
(discharger : tactic unit := `[ring])
(restrict_type : option Type := none)
(restrict_type_reflect : reflected restrict_type . tactic.apply_instance)
(exfalso : bool := tt)
(transparency : tactic.transparency := reducible)
(split_hypotheses : bool := tt)
(preprocessors : option (list global_preprocessor) := none)
(oracle : option certificate_oracle := none)
/--
`cfg.update_reducibility reduce_semi` will change the transparency setting of `cfg` to
`semireducible` if `reduce_semi` is true. In this case, it also sets the discharger to `ring!`,
since this is typically needed when using stronger unification.
-/
meta def linarith_config.update_reducibility (cfg : linarith_config) (reduce_semi : bool) :
linarith_config :=
if reduce_semi then { cfg with transparency := semireducible, discharger := `[ring!] }
else cfg
/-!
### Auxiliary functions
These functions are used by multiple modules, so we put them here for accessibility.
-/
open tactic
/--
`get_rel_sides e` returns the left and right hand sides of `e` if `e` is a comparison,
and fails otherwise.
This function is more naturally in the `option` monad, but it is convenient to put in `tactic`
for compositionality.
-/
meta def get_rel_sides : expr → tactic (expr × expr)
| `(%%a < %%b) := return (a, b)
| `(%%a ≤ %%b) := return (a, b)
| `(%%a = %%b) := return (a, b)
| `(%%a ≥ %%b) := return (a, b)
| `(%%a > %%b) := return (a, b)
| _ := tactic.failed
/--
`parse_into_comp_and_expr e` checks if `e` is of the form `t < 0`, `t ≤ 0`, or `t = 0`.
If it is, it returns the comparison along with `t`.
-/
meta def parse_into_comp_and_expr : expr → option (ineq × expr)
| `(%%e < 0) := (ineq.lt, e)
| `(%%e ≤ 0) := (ineq.le, e)
| `(%%e = 0) := (ineq.eq, e)
| _ := none
/--
`mk_single_comp_zero_pf c h` assumes that `h` is a proof of `t R 0`.
It produces a pair `(R', h')`, where `h'` is a proof of `c*t R' 0`.
Typically `R` and `R'` will be the same, except when `c = 0`, in which case `R'` is `=`.
If `c = 1`, `h'` is the same as `h` -- specifically, it does *not* change the type to `1*t R 0`.
-/
meta def mk_single_comp_zero_pf (c : ℕ) (h : expr) : tactic (ineq × expr) :=
do tp ← infer_type h,
some (iq, e) ← return $ parse_into_comp_and_expr tp,
if c = 0 then
do e' ← mk_app ``zero_mul [e], return (ineq.eq, e')
else if c = 1 then return (iq, h)
else
do tp ← (prod.snd <$> (infer_type h >>= get_rel_sides)) >>= infer_type,
c ← tp.of_nat c,
cpos ← to_expr ``(%%c > 0),
(_, ex) ← solve_aux cpos `[norm_num, done],
e' ← mk_app iq.to_const_mul_nm [h, ex],
return (iq, e')
end linarith
|
3db053b7b5307eecfa557701eac12ddcfaf2f54e | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/data/rat/order.lean | 46004118b583a26c2f9e30c3cb3378e9b9619fbb | [
"Apache-2.0"
] | permissive | alreadydone/mathlib | dc0be621c6c8208c581f5170a8216c5ba6721927 | c982179ec21091d3e102d8a5d9f5fe06c8fafb73 | refs/heads/master | 1,685,523,275,196 | 1,670,184,141,000 | 1,670,184,141,000 | 287,574,545 | 0 | 0 | Apache-2.0 | 1,670,290,714,000 | 1,597,421,623,000 | Lean | UTF-8 | Lean | false | false | 8,736 | lean | /-
Copyright (c) 2019 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Mario Carneiro
-/
import algebra.order.field.defs
import data.rat.basic
import data.int.cast.lemmas
import tactic.assert_exists
/-!
# Order for Rational Numbers
## Summary
We define the order on `ℚ`, prove that `ℚ` is a discrete, linearly ordered field, and define
functions such as `abs` and `sqrt` that depend on this order.
## Notations
- `/.` is infix notation for `rat.mk`.
## Tags
rat, rationals, field, ℚ, numerator, denominator, num, denom, order, ordering, sqrt, abs
-/
namespace rat
variables (a b c : ℚ)
open_locale rat
/-- A rational number is called nonnegative if its numerator is nonnegative. -/
protected def nonneg (r : ℚ) : Prop := 0 ≤ r.num
@[simp] theorem mk_nonneg (a : ℤ) {b : ℤ} (h : 0 < b) : (a /. b).nonneg ↔ 0 ≤ a :=
begin
generalize ha : a /. b = x, cases x with n₁ d₁ h₁ c₁, rw num_denom' at ha,
simp [rat.nonneg],
have d0 := int.coe_nat_lt.2 h₁,
have := (mk_eq (ne_of_gt h) (ne_of_gt d0)).1 ha,
constructor; intro h₂,
{ apply nonneg_of_mul_nonneg_left _ d0,
rw this, exact mul_nonneg h₂ (le_of_lt h) },
{ apply nonneg_of_mul_nonneg_left _ h,
rw ← this, exact mul_nonneg h₂ (int.coe_zero_le _) },
end
protected lemma nonneg_add {a b} : rat.nonneg a → rat.nonneg b → rat.nonneg (a + b) :=
num_denom_cases_on' a $ λ n₁ d₁ h₁,
num_denom_cases_on' b $ λ n₂ d₂ h₂,
begin
have d₁0 : 0 < (d₁:ℤ) := int.coe_nat_pos.2 (nat.pos_of_ne_zero h₁),
have d₂0 : 0 < (d₂:ℤ) := int.coe_nat_pos.2 (nat.pos_of_ne_zero h₂),
simp [d₁0, d₂0, h₁, h₂, mul_pos d₁0 d₂0],
intros n₁0 n₂0,
apply add_nonneg; apply mul_nonneg; {assumption <|> apply int.coe_zero_le},
end
protected lemma nonneg_mul {a b} : rat.nonneg a → rat.nonneg b → rat.nonneg (a * b) :=
num_denom_cases_on' a $ λ n₁ d₁ h₁,
num_denom_cases_on' b $ λ n₂ d₂ h₂,
begin
have d₁0 : 0 < (d₁:ℤ) := int.coe_nat_pos.2 (nat.pos_of_ne_zero h₁),
have d₂0 : 0 < (d₂:ℤ) := int.coe_nat_pos.2 (nat.pos_of_ne_zero h₂),
simp [d₁0, d₂0, h₁, h₂, mul_pos d₁0 d₂0, mul_nonneg] { contextual := tt }
end
protected lemma nonneg_antisymm {a} : rat.nonneg a → rat.nonneg (-a) → a = 0 :=
num_denom_cases_on' a $ λ n d h,
begin
have d0 : 0 < (d:ℤ) := int.coe_nat_pos.2 (nat.pos_of_ne_zero h),
simp [d0, h],
exact λ h₁ h₂, le_antisymm h₂ h₁
end
protected lemma nonneg_total : rat.nonneg a ∨ rat.nonneg (-a) :=
by cases a with n; exact
or.imp_right neg_nonneg_of_nonpos (le_total 0 n)
instance decidable_nonneg : decidable (rat.nonneg a) :=
by cases a; unfold rat.nonneg; apply_instance
/-- Relation `a ≤ b` on `ℚ` defined as `a ≤ b ↔ rat.nonneg (b - a)`. Use `a ≤ b` instead of
`rat.le a b`. -/
protected def le (a b : ℚ) := rat.nonneg (b - a)
instance : has_le ℚ := ⟨rat.le⟩
instance decidable_le : decidable_rel ((≤) : ℚ → ℚ → Prop)
| a b := show decidable (rat.nonneg (b - a)), by apply_instance
protected theorem le_def {a b c d : ℤ} (b0 : 0 < b) (d0 : 0 < d) :
a /. b ≤ c /. d ↔ a * d ≤ c * b :=
begin
show rat.nonneg _ ↔ _,
rw ← sub_nonneg,
simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0]
end
protected theorem le_refl : a ≤ a :=
show rat.nonneg (a - a), by rw sub_self; exact le_refl (0 : ℤ)
protected theorem le_total : a ≤ b ∨ b ≤ a :=
by have := rat.nonneg_total (b - a); rwa neg_sub at this
protected theorem le_antisymm {a b : ℚ} (hab : a ≤ b) (hba : b ≤ a) : a = b :=
by { have := eq_neg_of_add_eq_zero_left (rat.nonneg_antisymm hba $
by rwa [← sub_eq_add_neg, neg_sub]), rwa neg_neg at this }
protected theorem le_trans {a b c : ℚ} (hab : a ≤ b) (hbc : b ≤ c) : a ≤ c :=
have rat.nonneg (b - a + (c - b)), from rat.nonneg_add hab hbc,
by simpa [sub_eq_add_neg, add_comm, add_left_comm]
instance : linear_order ℚ :=
{ le := rat.le,
le_refl := rat.le_refl,
le_trans := @rat.le_trans,
le_antisymm := @rat.le_antisymm,
le_total := rat.le_total,
decidable_eq := by apply_instance,
decidable_le := assume a b, rat.decidable_nonneg (b - a) }
/- Extra instances to short-circuit type class resolution -/
instance : has_lt ℚ := by apply_instance
instance : distrib_lattice ℚ := by apply_instance
instance : lattice ℚ := by apply_instance
instance : semilattice_inf ℚ := by apply_instance
instance : semilattice_sup ℚ := by apply_instance
instance : has_inf ℚ := by apply_instance
instance : has_sup ℚ := by apply_instance
instance : partial_order ℚ := by apply_instance
instance : preorder ℚ := by apply_instance
protected lemma le_def' {p q : ℚ} : p ≤ q ↔ p.num * q.denom ≤ q.num * p.denom :=
begin
rw [←(@num_denom q), ←(@num_denom p)],
conv_rhs { simp only [num_denom] },
exact rat.le_def (by exact_mod_cast p.pos) (by exact_mod_cast q.pos)
end
protected lemma lt_def {p q : ℚ} : p < q ↔ p.num * q.denom < q.num * p.denom :=
begin
rw [lt_iff_le_and_ne, rat.le_def'],
suffices : p ≠ q ↔ p.num * q.denom ≠ q.num * p.denom, by
{ split; intro h,
{ exact lt_iff_le_and_ne.elim_right ⟨h.left, (this.elim_left h.right)⟩ },
{ have tmp := lt_iff_le_and_ne.elim_left h, exact ⟨tmp.left, this.elim_right tmp.right⟩ }},
exact (not_iff_not.elim_right eq_iff_mul_eq_mul)
end
theorem nonneg_iff_zero_le {a} : rat.nonneg a ↔ 0 ≤ a :=
show rat.nonneg a ↔ rat.nonneg (a - 0), by simp
theorem num_nonneg_iff_zero_le : ∀ {a : ℚ}, 0 ≤ a.num ↔ 0 ≤ a
| ⟨n, d, h, c⟩ := @nonneg_iff_zero_le ⟨n, d, h, c⟩
protected theorem add_le_add_left {a b c : ℚ} : c + a ≤ c + b ↔ a ≤ b :=
by unfold has_le.le rat.le; rw add_sub_add_left_eq_sub
protected theorem mul_nonneg {a b : ℚ} (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ a * b :=
by rw ← nonneg_iff_zero_le at ha hb ⊢; exact rat.nonneg_mul ha hb
instance : linear_ordered_field ℚ :=
{ zero_le_one := dec_trivial,
add_le_add_left := assume a b ab c, rat.add_le_add_left.2 ab,
mul_pos := assume a b ha hb, lt_of_le_of_ne
(rat.mul_nonneg (le_of_lt ha) (le_of_lt hb))
(mul_ne_zero (ne_of_lt ha).symm (ne_of_lt hb).symm).symm,
..rat.field,
..rat.linear_order,
..rat.semiring }
/- Extra instances to short-circuit type class resolution -/
instance : linear_ordered_comm_ring ℚ := by apply_instance
instance : linear_ordered_ring ℚ := by apply_instance
instance : ordered_ring ℚ := by apply_instance
instance : linear_ordered_semiring ℚ := by apply_instance
instance : ordered_semiring ℚ := by apply_instance
instance : linear_ordered_add_comm_group ℚ := by apply_instance
instance : ordered_add_comm_group ℚ := by apply_instance
instance : ordered_cancel_add_comm_monoid ℚ := by apply_instance
instance : ordered_add_comm_monoid ℚ := by apply_instance
attribute [irreducible] rat.le
theorem num_pos_iff_pos {a : ℚ} : 0 < a.num ↔ 0 < a :=
lt_iff_lt_of_le_iff_le $
by simpa [(by cases a; refl : (-a).num = -a.num)]
using @num_nonneg_iff_zero_le (-a)
lemma div_lt_div_iff_mul_lt_mul {a b c d : ℤ} (b_pos : 0 < b) (d_pos : 0 < d) :
(a : ℚ) / b < c / d ↔ a * d < c * b :=
begin
simp only [lt_iff_le_not_le],
apply and_congr,
{ simp [div_num_denom, (rat.le_def b_pos d_pos)] },
{ apply not_iff_not_of_iff, simp [div_num_denom, (rat.le_def d_pos b_pos)] }
end
lemma lt_one_iff_num_lt_denom {q : ℚ} : q < 1 ↔ q.num < q.denom :=
by simp [rat.lt_def]
theorem abs_def (q : ℚ) : |q| = q.num.nat_abs /. q.denom :=
begin
cases le_total q 0 with hq hq,
{ rw [abs_of_nonpos hq],
rw [←(@num_denom q), ← mk_zero_one, rat.le_def (int.coe_nat_pos.2 q.pos) zero_lt_one,
mul_one, zero_mul] at hq,
rw [int.of_nat_nat_abs_of_nonpos hq, ← neg_def, num_denom] },
{ rw [abs_of_nonneg hq],
rw [←(@num_denom q), ← mk_zero_one, rat.le_def zero_lt_one (int.coe_nat_pos.2 q.pos),
mul_one, zero_mul] at hq,
rw [int.nat_abs_of_nonneg hq, num_denom] }
end
end rat
-- We make some assertions here about declarations that do not need to be in the import dependencies
-- for this file, but have been in the past.
assert_not_exists fintype
assert_not_exists set.Icc
assert_not_exists galois_connection
-- These are less significant, but should not be relaxed until at least after port to Lean 4.
assert_not_exists linear_ordered_comm_group_with_zero
-- This one doesn't exist anywhere!
-- assert_not_exists positive.add_comm_semigroup
|
0b6a499bd29cba0a1d217409af1512d62a51285c | 206422fb9edabf63def0ed2aa3f489150fb09ccb | /src/data/nat/digits.lean | 6f3a88fd8cb7cc92fa9d6c9e7a896868aa473824 | [
"Apache-2.0"
] | permissive | hamdysalah1/mathlib | b915f86b2503feeae268de369f1b16932321f097 | 95454452f6b3569bf967d35aab8d852b1ddf8017 | refs/heads/master | 1,677,154,116,545 | 1,611,797,994,000 | 1,611,797,994,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 20,662 | lean | /-
Copyright (c) 2020 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Shing Tak Lam, Mario Carneiro
-/
import data.int.modeq
import tactic.interval_cases
import tactic.linarith
/-!
# Digits of a natural number
This provides a basic API for extracting the digits of a natural number in a given base,
and reconstructing numbers from their digits.
We also prove some divisibility tests based on digits, in particular completing
Theorem #85 from https://www.cs.ru.nl/~freek/100/.
A basic `norm_digits` tactic is also provided for proving goals of the form
`nat.digits a b = l` where `a` and `b` are numerals.
-/
namespace nat
/-- (Impl.) An auxiliary definition for `digits`, to help get the desired definitional unfolding. -/
def digits_aux_0 : ℕ → list ℕ
| 0 := []
| (n+1) := [n+1]
/-- (Impl.) An auxiliary definition for `digits`, to help get the desired definitional unfolding. -/
def digits_aux_1 (n : ℕ) : list ℕ := list.repeat 1 n
/-- (Impl.) An auxiliary definition for `digits`, to help get the desired definitional unfolding. -/
def digits_aux (b : ℕ) (h : 2 ≤ b) : ℕ → list ℕ
| 0 := []
| (n+1) :=
have (n+1)/b < n+1 := nat.div_lt_self (nat.succ_pos _) h,
(n+1) % b :: digits_aux ((n+1)/b)
@[simp] lemma digits_aux_zero (b : ℕ) (h : 2 ≤ b) : digits_aux b h 0 = [] := rfl
lemma digits_aux_def (b : ℕ) (h : 2 ≤ b) (n : ℕ) (w : 0 < n) :
digits_aux b h n = n % b :: digits_aux b h (n/b) :=
begin
cases n,
{ cases w, },
{ rw [digits_aux], }
end
/--
`digits b n` gives the digits, in little-endian order,
of a natural number `n` in a specified base `b`.
In any base, we have `of_digits b L = L.foldr (λ x y, x + b * y) 0`.
* For any `2 ≤ b`, we have `l < b` for any `l ∈ digits b n`,
and the last digit is not zero.
This uniquely specifies the behaviour of `digits b`.
* For `b = 1`, we define `digits 1 n = list.repeat 1 n`.
* For `b = 0`, we define `digits 0 n = [n]`, except `digits 0 0 = []`.
Note this differs from the existing `nat.to_digits` in core, which is used for printing numerals.
In particular, `nat.to_digits b 0 = [0]`, while `digits b 0 = []`.
-/
def digits : ℕ → ℕ → list ℕ
| 0 := digits_aux_0
| 1 := digits_aux_1
| (b+2) := digits_aux (b+2) (by norm_num)
@[simp] lemma digits_zero (b : ℕ) : digits b 0 = [] :=
begin
cases b,
{ refl, },
{ cases b; refl, },
end
@[simp] lemma digits_zero_zero : digits 0 0 = [] := rfl
@[simp] lemma digits_zero_succ (n : ℕ) : digits 0 (n.succ) = [n+1] := rfl
theorem digits_zero_succ' : ∀ {n : ℕ} (w : 0 < n), digits 0 n = [n]
| 0 h := absurd h dec_trivial
| (n+1) _ := rfl
@[simp] lemma digits_one (n : ℕ) : digits 1 n = list.repeat 1 n := rfl
@[simp] lemma digits_one_succ (n : ℕ) : digits 1 (n + 1) = 1 :: digits 1 n := rfl
@[simp] lemma digits_add_two_add_one (b n : ℕ) :
digits (b+2) (n+1) = (((n+1) % (b+2)) :: digits (b+2) ((n+1) / (b+2))) := rfl
theorem digits_def' : ∀ {b : ℕ} (h : 2 ≤ b) {n : ℕ} (w : 0 < n),
digits b n = n % b :: digits b (n/b)
| 0 h := absurd h dec_trivial
| 1 h := absurd h dec_trivial
| (b+2) h := digits_aux_def _ _
@[simp]
lemma digits_of_lt (b x : ℕ) (w₁ : 0 < x) (w₂ : x < b) : digits b x = [x] :=
begin
cases b,
{ cases w₂ },
{ cases b,
{ interval_cases x, },
{ cases x,
{ cases w₁, },
{ dsimp [digits],
rw digits_aux,
rw nat.div_eq_of_lt w₂,
dsimp only [digits_aux_zero],
rw nat.mod_eq_of_lt w₂, } } }
end
lemma digits_add (b : ℕ) (h : 2 ≤ b) (x y : ℕ) (w : x < b) (w' : 0 < x ∨ 0 < y) :
digits b (x + b * y) = x :: digits b y :=
begin
cases b,
{ cases h, },
{ cases b,
{ norm_num at h, },
{ cases y,
{ norm_num at w',
simp [w, w'], },
dsimp [digits],
rw digits_aux_def,
{ congr,
{ simp [nat.add_mod, nat.mod_eq_of_lt w], },
{ simp [mul_comm (b+2), nat.add_mul_div_right, nat.div_eq_of_lt w], }
},
{ apply nat.succ_pos, }, }, },
end
/--
`of_digits b L` takes a list `L` of natural numbers, and interprets them
as a number in semiring, as the little-endian digits in base `b`.
-/
-- If we had a function converting a list into a polynomial,
-- and appropriate lemmas about that function,
-- we could rewrite this in terms of that.
def of_digits {α : Type*} [semiring α] (b : α) : list ℕ → α
| [] := 0
| (h :: t) := h + b * of_digits t
lemma of_digits_eq_foldr {α : Type*} [semiring α] (b : α) (L : list ℕ) :
of_digits b L = L.foldr (λ x y, x + b * y) 0 :=
begin
induction L with d L ih,
{ refl, },
{ dsimp [of_digits], rw ih, },
end
@[simp] lemma of_digits_singleton {b n : ℕ} : of_digits b [n] = n := by simp [of_digits]
@[simp] lemma of_digits_one_cons {α : Type*} [semiring α] (h : ℕ) (L : list ℕ) :
of_digits (1 : α) (h :: L) = h + of_digits 1 L :=
by simp [of_digits]
lemma of_digits_append {b : ℕ} {l1 l2 : list ℕ} :
of_digits b (l1 ++ l2) = of_digits b l1 + b^(l1.length) * of_digits b l2 :=
begin
induction l1 with hd tl IH,
{ simp [of_digits] },
{ rw [of_digits, list.cons_append, of_digits, IH, list.length_cons, pow_succ'],
ring }
end
@[norm_cast] lemma coe_of_digits (α : Type*) [semiring α] (b : ℕ) (L : list ℕ) :
((of_digits b L : ℕ) : α) = of_digits (b : α) L :=
begin
induction L with d L ih,
{ refl, },
{ dsimp [of_digits], push_cast, rw ih, }
end
@[norm_cast] lemma coe_int_of_digits (b : ℕ) (L : list ℕ) :
((of_digits b L : ℕ) : ℤ) = of_digits (b : ℤ) L :=
begin
induction L with d L ih,
{ refl, },
{ dsimp [of_digits], push_cast, rw ih, }
end
lemma digits_zero_of_eq_zero {b : ℕ} (h : 1 ≤ b) {L : list ℕ} (w : of_digits b L = 0) :
∀ l ∈ L, l = 0 :=
begin
induction L with d L ih,
{ intros l m,
cases m, },
{ intros l m,
dsimp [of_digits] at w,
rcases m with ⟨rfl⟩,
{ convert nat.eq_zero_of_add_eq_zero_right w, simp, },
{ exact ih ((nat.mul_right_inj h).mp (nat.eq_zero_of_add_eq_zero_left w)) _ m, }, }
end
lemma digits_of_digits
(b : ℕ) (h : 2 ≤ b) (L : list ℕ)
(w₁ : ∀ l ∈ L, l < b) (w₂ : ∀ (h : L ≠ []), L.last h ≠ 0) :
digits b (of_digits b L) = L :=
begin
induction L with d L ih,
{ dsimp [of_digits], simp },
{ dsimp [of_digits],
replace w₂ := w₂ (by simp),
rw digits_add b h,
{ rw ih,
{ simp, },
{ intros l m, apply w₁, exact list.mem_cons_of_mem _ m, },
{ intro h,
{ rw [list.last_cons _ h] at w₂,
convert w₂, }}},
{ convert w₁ d (list.mem_cons_self _ _), simp, },
{ by_cases h' : L = [],
{ rcases h' with rfl,
simp at w₂,
left,
apply nat.pos_of_ne_zero,
convert w₂, simp, },
{ right,
apply nat.pos_of_ne_zero,
contrapose! w₂,
apply digits_zero_of_eq_zero _ w₂,
{ rw list.last_cons _ h',
exact list.last_mem h', },
{ exact le_of_lt h, }, }, }, },
end
lemma of_digits_digits (b n : ℕ) : of_digits b (digits b n) = n :=
begin
cases b with b,
{ cases n with n,
{ refl, },
{ change of_digits 0 [n+1] = n+1,
dsimp [of_digits],
simp, } },
{ cases b with b,
{ induction n with n ih,
{ refl, },
{ simp only [ih, add_comm 1, of_digits_one_cons, nat.cast_id, digits_one_succ], } },
{ apply nat.strong_induction_on n _, clear n,
intros n h,
cases n,
{ refl, },
{ simp only [nat.succ_eq_add_one, digits_add_two_add_one],
dsimp [of_digits],
rw h _ (nat.div_lt_self' n b),
rw [nat.cast_id, nat.mod_add_div], }, }, },
end
lemma of_digits_one (L : list ℕ) : of_digits 1 L = L.sum :=
begin
induction L with d L ih,
{ refl, },
{ simp [of_digits, list.sum_cons, ih], }
end
/-!
### Properties
This section contains various lemmas of properties relating to `digits` and `of_digits`.
-/
lemma digits_eq_nil_iff_eq_zero {b n : ℕ} : digits b n = [] ↔ n = 0 :=
begin
split,
{ intro h,
have : of_digits b (digits b n) = of_digits b [], by rw h,
convert this,
rw of_digits_digits },
{ rintro rfl,
simp }
end
lemma digits_ne_nil_iff_ne_zero {b n : ℕ} : digits b n ≠ [] ↔ n ≠ 0 :=
not_congr digits_eq_nil_iff_eq_zero
private lemma digits_last_aux {b n : ℕ} (h : 2 ≤ b) (w : 0 < n) :
digits b n = ((n % b) :: digits b (n / b)) :=
begin
rcases b with _|_|b,
{ finish },
{ norm_num at h },
rcases n with _|n,
{ norm_num at w },
simp,
end
lemma digits_last {b m : ℕ} (h : 2 ≤ b) (hm : 0 < m) (p q) :
(digits b m).last p = (digits b (m/b)).last q :=
by { simp only [digits_last_aux h hm], rw list.last_cons }
lemma last_digit_ne_zero (b : ℕ) {m : ℕ} (hm : m ≠ 0) :
(digits b m).last (digits_ne_nil_iff_ne_zero.mpr hm) ≠ 0 :=
begin
rcases b with _|_|b,
{ cases m; finish },
{ cases m, { finish },
simp_rw [digits_one, list.last_repeat_succ 1 m],
norm_num },
revert hm,
apply nat.strong_induction_on m,
intros n IH hn,
have hnpos : 0 < n := nat.pos_of_ne_zero hn,
by_cases hnb : n < b + 2,
{ simp_rw [digits_of_lt b.succ.succ n hnpos hnb],
exact pos_iff_ne_zero.mp hnpos },
{ rw digits_last (show 2 ≤ b + 2, from dec_trivial) hnpos,
refine IH _ (nat.div_lt_self hnpos dec_trivial) _,
{ rw ←pos_iff_ne_zero,
exact nat.div_pos (le_of_not_lt hnb) dec_trivial } },
end
/-- The digits in the base b+2 expansion of n are all less than b+2 -/
lemma digits_lt_base' {b m : ℕ} : ∀ {d}, d ∈ digits (b+2) m → d < b+2 :=
begin
apply nat.strong_induction_on m,
intros n IH d hd,
unfold digits at hd IH,
cases n with n,
{ cases hd }, -- base b+2 expansion of 0 has no digits
rw digits_aux_def (b+2) (by linarith) n.succ (nat.zero_lt_succ n) at hd,
cases hd,
{ rw hd, exact n.succ.mod_lt (by linarith) },
{ exact IH _ (nat.div_lt_self (nat.succ_pos _) (by linarith)) hd }
end
/-- The digits in the base b expansion of n are all less than b, if b ≥ 2 -/
lemma digits_lt_base {b m d : ℕ} (hb : 2 ≤ b) (hd : d ∈ digits b m) : d < b :=
begin
rcases b with _ | _ | b; try {linarith},
exact digits_lt_base' hd,
end
/-- an n-digit number in base b + 2 is less than (b + 2)^n -/
lemma of_digits_lt_base_pow_length' {b : ℕ} {l : list ℕ} (hl : ∀ x ∈ l, x < b+2) :
of_digits (b+2) l < (b+2)^(l.length) :=
begin
induction l with hd tl IH,
{ simp [of_digits], },
{ rw [of_digits, list.length_cons, pow_succ],
have : (of_digits (b + 2) tl + 1) * (b+2) ≤ (b + 2) ^ tl.length * (b+2) :=
mul_le_mul (IH (λ x hx, hl _ (list.mem_cons_of_mem _ hx)))
(by refl) dec_trivial (nat.zero_le _),
suffices : ↑hd < b + 2,
{ linarith },
norm_cast,
exact hl hd (list.mem_cons_self _ _) }
end
/-- an n-digit number in base b is less than b^n if b ≥ 2 -/
lemma of_digits_lt_base_pow_length {b : ℕ} {l : list ℕ} (hb : 2 ≤ b) (hl : ∀ x ∈ l, x < b) :
of_digits b l < b^l.length :=
begin
rcases b with _ | _ | b; try { linarith },
exact of_digits_lt_base_pow_length' hl,
end
/-- Any number m is less than (b+2)^(number of digits in the base b + 2 representation of m) -/
lemma lt_base_pow_length_digits' {b m : ℕ} : m < (b + 2) ^ (digits (b + 2) m).length :=
begin
convert of_digits_lt_base_pow_length' (λ _, digits_lt_base'),
rw of_digits_digits (b+2) m,
end
/-- Any number m is less than b^(number of digits in the base b representation of m) -/
lemma lt_base_pow_length_digits {b m : ℕ} (hb : 2 ≤ b) : m < b^(digits b m).length :=
begin
rcases b with _ | _ | b; try { linarith },
exact lt_base_pow_length_digits',
end
lemma of_digits_digits_append_digits {b m n : ℕ} :
of_digits b (digits b n ++ digits b m) = n + b ^ (digits b n).length * m:=
by rw [of_digits_append, of_digits_digits, of_digits_digits]
lemma digits_len_le_digits_len_succ (b n : ℕ) : (digits b n).length ≤ (digits b (n + 1)).length :=
begin
cases b,
{ -- base 0
cases n; simp },
{ cases b,
{ -- base 1
simp },
{ -- base >= 2
apply nat.strong_induction_on n,
clear n,
intros n IH,
cases n,
{ simp },
{ rw [digits_add_two_add_one, digits_add_two_add_one],
by_cases hdvd : (b.succ.succ) ∣ (n.succ+1),
{ rw [nat.succ_div_of_dvd hdvd, list.length_cons, list.length_cons, nat.succ_le_succ_iff],
apply IH,
exact nat.div_lt_self (by linarith) (by linarith) },
{ rw nat.succ_div_of_not_dvd hdvd,
refl } } } }
end
lemma le_digits_len_le (b n m : ℕ) (h : n ≤ m) : (digits b n).length ≤ (digits b m).length :=
monotone_of_monotone_nat (digits_len_le_digits_len_succ b) h
lemma pow_length_le_mul_of_digits {b : ℕ} {l : list ℕ} (hl : l ≠ []) (hl2 : l.last hl ≠ 0):
(b + 2) ^ l.length ≤ (b + 2) * of_digits (b+2) l :=
begin
rw [←list.init_append_last hl],
simp only [list.length_append, list.length, zero_add, list.length_init, of_digits_append,
list.length_init, of_digits_singleton, add_comm (l.length - 1), pow_add, pow_one],
apply nat.mul_le_mul_left,
refine le_trans _ (nat.le_add_left _ _),
have : 0 < l.last hl, { rwa [pos_iff_ne_zero] },
convert nat.mul_le_mul_left _ this, rw [mul_one]
end
/--
Any non-zero natural number `m` is greater than
(b+2)^((number of digits in the base (b+2) representation of m) - 1)
-/
lemma base_pow_length_digits_le' (b m : ℕ) (hm : m ≠ 0) :
(b + 2) ^ ((digits (b + 2) m).length) ≤ (b + 2) * m :=
begin
have : digits (b + 2) m ≠ [], from digits_ne_nil_iff_ne_zero.mpr hm,
convert pow_length_le_mul_of_digits this (last_digit_ne_zero _ hm),
rwa of_digits_digits,
end
/--
Any non-zero natural number `m` is greater than
b^((number of digits in the base b representation of m) - 1)
-/
lemma base_pow_length_digits_le (b m : ℕ) (hb : 2 ≤ b): m ≠ 0 → b ^ ((digits b m).length) ≤ b * m :=
begin
rcases b with _ | _ | b; try { linarith },
exact base_pow_length_digits_le' b m,
end
/-! ### Modular Arithmetic -/
-- This is really a theorem about polynomials.
lemma dvd_of_digits_sub_of_digits {α : Type*} [comm_ring α]
{a b k : α} (h : k ∣ a - b) (L : list ℕ) :
k ∣ of_digits a L - of_digits b L :=
begin
induction L with d L ih,
{ change k ∣ 0 - 0, simp, },
{ simp only [of_digits, add_sub_add_left_eq_sub],
exact dvd_mul_sub_mul h ih, }
end
lemma of_digits_modeq' (b b' : ℕ) (k : ℕ) (h : b ≡ b' [MOD k]) (L : list ℕ) :
of_digits b L ≡ of_digits b' L [MOD k] :=
begin
induction L with d L ih,
{ refl, },
{ dsimp [of_digits],
dsimp [nat.modeq] at *,
conv_lhs { rw [nat.add_mod, nat.mul_mod, h, ih], },
conv_rhs { rw [nat.add_mod, nat.mul_mod], }, }
end
lemma of_digits_modeq (b k : ℕ) (L : list ℕ) : of_digits b L ≡ of_digits (b % k) L [MOD k] :=
of_digits_modeq' b (b % k) k (nat.modeq.symm (nat.modeq.mod_modeq b k)) L
lemma of_digits_mod (b k : ℕ) (L : list ℕ) : of_digits b L % k = of_digits (b % k) L % k :=
of_digits_modeq b k L
lemma of_digits_zmodeq' (b b' : ℤ) (k : ℕ) (h : b ≡ b' [ZMOD k]) (L : list ℕ) :
of_digits b L ≡ of_digits b' L [ZMOD k] :=
begin
induction L with d L ih,
{ refl, },
{ dsimp [of_digits],
dsimp [int.modeq] at *,
conv_lhs { rw [int.add_mod, int.mul_mod, h, ih], },
conv_rhs { rw [int.add_mod, int.mul_mod], }, }
end
lemma of_digits_zmodeq (b : ℤ) (k : ℕ) (L : list ℕ) :
of_digits b L ≡ of_digits (b % k) L [ZMOD k] :=
of_digits_zmodeq' b (b % k) k (int.modeq.symm (int.modeq.mod_modeq b ↑k)) L
lemma of_digits_zmod (b : ℤ) (k : ℕ) (L : list ℕ) :
of_digits b L % k = of_digits (b % k) L % k :=
of_digits_zmodeq b k L
lemma modeq_digits_sum (b b' : ℕ) (h : b' % b = 1) (n : ℕ) :
n ≡ (digits b' n).sum [MOD b] :=
begin
rw ←of_digits_one,
conv { congr, skip, rw ←(of_digits_digits b' n) },
convert of_digits_modeq _ _ _,
exact h.symm,
end
lemma modeq_three_digits_sum (n : ℕ) : n ≡ (digits 10 n).sum [MOD 3] :=
modeq_digits_sum 3 10 (by norm_num) n
lemma modeq_nine_digits_sum (n : ℕ) : n ≡ (digits 10 n).sum [MOD 9] :=
modeq_digits_sum 9 10 (by norm_num) n
lemma zmodeq_of_digits_digits (b b' : ℕ) (c : ℤ) (h : b' ≡ c [ZMOD b]) (n : ℕ) :
n ≡ of_digits c (digits b' n) [ZMOD b] :=
begin
conv { congr, skip, rw ←(of_digits_digits b' n) },
rw coe_int_of_digits,
apply of_digits_zmodeq' _ _ _ h,
end
lemma of_digits_neg_one : Π (L : list ℕ),
of_digits (-1 : ℤ) L = (L.map (λ n : ℕ, (n : ℤ))).alternating_sum
| [] := rfl
| [n] := by simp [of_digits, list.alternating_sum]
| (a :: b :: t) :=
begin
simp only [of_digits, list.alternating_sum, list.map_cons, of_digits_neg_one t],
push_cast,
ring,
end
lemma modeq_eleven_digits_sum (n : ℕ) :
n ≡ ((digits 10 n).map (λ n : ℕ, (n : ℤ))).alternating_sum [ZMOD 11] :=
begin
have t := zmodeq_of_digits_digits 11 10 (-1 : ℤ) dec_trivial n,
rw of_digits_neg_one at t,
exact t,
end
/-! ## Divisibility -/
lemma dvd_iff_dvd_digits_sum (b b' : ℕ) (h : b' % b = 1) (n : ℕ) :
b ∣ n ↔ b ∣ (digits b' n).sum :=
begin
rw ←of_digits_one,
conv_lhs { rw ←(of_digits_digits b' n) },
rw [nat.dvd_iff_mod_eq_zero, nat.dvd_iff_mod_eq_zero, of_digits_mod, h],
end
lemma three_dvd_iff (n : ℕ) : 3 ∣ n ↔ 3 ∣ (digits 10 n).sum :=
dvd_iff_dvd_digits_sum 3 10 (by norm_num) n
lemma nine_dvd_iff (n : ℕ) : 9 ∣ n ↔ 9 ∣ (digits 10 n).sum :=
dvd_iff_dvd_digits_sum 9 10 (by norm_num) n
lemma dvd_iff_dvd_of_digits (b b' : ℕ) (c : ℤ) (h : (b : ℤ) ∣ (b' : ℤ) - c) (n : ℕ) :
b ∣ n ↔ (b : ℤ) ∣ of_digits c (digits b' n) :=
begin
rw ←int.coe_nat_dvd,
exact dvd_iff_dvd_of_dvd_sub
(int.modeq.modeq_iff_dvd.1
(zmodeq_of_digits_digits b b' c (int.modeq.modeq_iff_dvd.2 h).symm _).symm),
end
lemma eleven_dvd_iff (n : ℕ) :
11 ∣ n ↔ (11 : ℤ) ∣ ((digits 10 n).map (λ n : ℕ, (n : ℤ))).alternating_sum :=
begin
have t := dvd_iff_dvd_of_digits 11 10 (-1 : ℤ) (by norm_num) n,
rw of_digits_neg_one at t,
exact t,
end
/-! ### `norm_digits` tactic -/
namespace norm_digits
theorem digits_succ
(b n m r l)
(e : r + b * m = n)
(hr : r < b)
(h : nat.digits b m = l ∧ 2 ≤ b ∧ 0 < m) :
nat.digits b n = r :: l ∧ 2 ≤ b ∧ 0 < n :=
begin
rcases h with ⟨h, b2, m0⟩,
have b0 : 0 < b := by linarith,
have n0 : 0 < n := by linarith [mul_pos b0 m0],
refine ⟨_, b2, n0⟩,
obtain ⟨rfl, rfl⟩ := (nat.div_mod_unique b0).2 ⟨e, hr⟩,
subst h, exact nat.digits_def' b2 n0,
end
theorem digits_one
(b n) (n0 : 0 < n) (nb : n < b) :
nat.digits b n = [n] ∧ 2 ≤ b ∧ 0 < n :=
begin
have b2 : 2 ≤ b := by linarith,
refine ⟨_, b2, n0⟩,
rw [nat.digits_def' b2 n0, nat.mod_eq_of_lt nb,
(nat.div_eq_zero_iff (by linarith : 0 < b)).2 nb, nat.digits_zero],
end
open tactic
/-- Helper function for the `norm_digits` tactic. -/
meta def eval_aux (eb : expr) (b : ℕ) :
expr → ℕ → instance_cache → tactic (instance_cache × expr × expr)
| en n ic := do
let m := n / b,
let r := n % b,
(ic, er) ← ic.of_nat r,
(ic, pr) ← norm_num.prove_lt_nat ic er eb,
if m = 0 then do
(_, pn0) ← norm_num.prove_pos ic en,
return (ic, `([%%en] : list nat), `(digits_one %%eb %%en %%pn0 %%pr))
else do
em ← expr.of_nat `(ℕ) m,
(_, pe) ← norm_num.derive `(%%er + %%eb * %%em : ℕ),
(ic, el, p) ← eval_aux em m ic,
return (ic, `(@list.cons ℕ %%er %%el),
`(digits_succ %%eb %%en %%em %%er %%el %%pe %%pr %%p))
/--
A tactic for normalizing expressions of the form `nat.digits a b = l` where
`a` and `b` are numerals.
```
example : nat.digits 10 123 = [3,2,1] := by norm_num
```
-/
@[norm_num] meta def eval : expr → tactic (expr × expr)
| `(nat.digits %%eb %%en) := do
b ← expr.to_nat eb,
n ← expr.to_nat en,
if n = 0 then return (`([] : list ℕ), `(nat.digits_zero %%eb))
else if b = 0 then do
ic ← mk_instance_cache `(ℕ),
(_, pn0) ← norm_num.prove_pos ic en,
return (`([%%en] : list ℕ), `(@nat.digits_zero_succ' %%en %%pn0))
else if b = 1 then do
ic ← mk_instance_cache `(ℕ),
(_, pn0) ← norm_num.prove_pos ic en,
s ← simp_lemmas.add_simp simp_lemmas.mk `list.repeat,
(rhs, p2, _) ← simplify s [] `(list.repeat 1 %%en),
p ← mk_eq_trans `(nat.digits_one %%en) p2,
return (rhs, p)
else do
ic ← mk_instance_cache `(ℕ),
(_, l, p) ← eval_aux eb b en n ic,
p ← mk_app ``and.left [p],
return (l, p)
| _ := failed
end norm_digits
end nat
|
e02427e98af7fcc2c3b771f055f9679c0d9c9b70 | f22a91842f18b5e855075b9becd55d2233756c6a | /src/ch02.lean | 4dd588928b00374205f869264e9f5440afee97d5 | [] | no_license | kckennylau/tao-analysis-one | 48cef1ee896d6703fcaf47ce5548e44f88901a0e | 2bd2d14c3c722bb36526c5a4e6b87618b2c081b1 | refs/heads/master | 1,621,002,517,963 | 1,514,151,057,000 | 1,514,151,057,000 | 115,971,702 | 0 | 0 | null | 1,514,864,495,000 | 1,514,864,495,000 | null | UTF-8 | Lean | false | false | 9,177 | lean | --------------------------------------------------------------------------------
-- Chapter 2 - Starting at the beginning: the natural numbers
-- Analysis 1, Third Edition, Tao
--------------------------------------------------------------------------------
open nat
-- Exercise 2.2.1. Prove Proposition 2.2.5.
example : ∀ a b c : ℕ, (a + b) + c = a + (b + c) :=
λ a b c,
nat.rec_on c
(show a + b + 0 = a + (b + 0), from rfl)
(λ c ih,
show a + b + succ c = a + (b + succ c), from calc
a + b + succ c = succ (a + b + c) : by rw add_succ
... = succ (a + (b + c)) : by rw ih
... = a + (b + succ c) : by rw add_succ)
-- Exercise 2.2.2. Prove Lemma 2.2.10.
def positive (n : ℕ) := n ≠ 0
example (a : ℕ) (h₁ : positive a) : ∃! b, succ b = a := begin
cases a with a,
{unfold positive at h₁; contradiction},
{existsi a,
split,
{simp},
{intros b h₂; exact nat.no_confusion h₂ id}}
end
-- Exercise 2.2.3. Prove Proposition 2.2.12.
section
variables {a b c : ℕ}
example : a ≤ a := le.intro (add_zero a)
example (h₁ : a ≤ b) (h₂ : b ≤ c) : a ≤ c :=
let ⟨m, h₃⟩ := le.dest h₁, ⟨n, h₄⟩ := le.dest h₂ in
have h₅ : a + (m + n) = c, by rw [←add_assoc, h₃, h₄],
le.intro h₅
example (h₁ : a ≤ b) (h₂ : b ≤ a) : a = b :=
let ⟨m, h₃⟩ := le.dest h₁, ⟨n, h₄⟩ := le.dest h₂ in
have h₅ : a + (m + n) = a + 0, by rw [←add_assoc, h₃, h₄, add_zero],
have h₆ : m + n = 0, from add_left_cancel h₅,
let ⟨h₇, h₈⟩ := eq_zero_of_add_eq_zero h₆ in
by rw [←h₃, h₇, add_zero]
example : a ≤ b ↔ a + c ≤ b + c :=
iff.intro
(λ h₁,
let ⟨d, h₂⟩ := le.dest h₁ in
suffices ∃ k, a + c + k = b + c, from
exists.elim this (λ k h₃, le.intro h₃),
exists.intro d (by rw [←h₂, add_assoc a d c, add_comm d c, add_assoc]))
(λ h₁,
let ⟨d, h₂⟩ := le.dest h₁ in
have h₃ : a + d = b, from
have h₃ : c + (a + d) = c + b, by
rw [add_comm c b, add_comm c (a + d)];
rw [add_assoc, add_comm d c, ←add_assoc, h₂],
add_left_cancel h₃,
le.intro h₃)
-- Tao's definition 2.2.11 for less-than as a theorem
theorem lt_iff_le_and_ne : a < b ↔ a ≤ b ∧ a ≠ b :=
iff.intro
(λ h₁,
and.intro
(less_than_or_equal.rec_on h₁
(less_than_or_equal.step (less_than_or_equal.refl a))
(λ b' (ih₁ : succ a ≤ b') ih₂,
less_than_or_equal.step ih₂))
(λ h₂,
let ⟨k, h₃⟩ := le.dest h₁ in
have h₄ : k = 0, begin
rw [succ_add, ←add_succ] at h₃,
clear _let_match,
subst h₂,
exact absurd h₁ (lt_irrefl a)
end,
begin
subst h₄,
rw [add_zero, ←h₂] at h₃,
have h₄ : (succ a) ≠ a,
exact succ_ne_self a,
contradiction
end))
(λ ⟨h₁, h₂⟩,
let h₃ := lt_or_eq_of_le h₁ in
or.elim h₃ id (λ h₄, absurd h₄ h₂))
example : a < b ↔ succ a ≤ b :=
iff.intro
(λ h₁,
let ⟨h₂, h₃⟩ := (iff.mp lt_iff_le_and_ne) h₁ in
let h₄ := le.dest h₂ in
exists.elim h₄ (λ k,
nat.cases_on k
(λ h₅ : a = b, absurd h₅ h₃)
(λ k' h₅,
have h₆ : succ a + k' = b, by rw [succ_add, ←add_succ, h₅],
le.intro h₆)))
(λ h₁,
iff.mpr lt_iff_le_and_ne (and.intro
(let ⟨k, h₂⟩ := le.dest h₁ in
have h₃ : a + succ k = b, by rw [add_succ, ←succ_add, h₂],
le.intro h₃)
(let ⟨k, h₂⟩ := le.dest h₁ in
λ h₃,
have h₄ : succ a ≤ a, by rw ←h₃ at h₁; exact h₁,
let h₅ := not_succ_le_self a in absurd h₄ h₅)))
example : a < b ↔ ∃ d, (positive d ∧ b = a + d) :=
iff.intro
(λ h₁,
let ⟨h₂, h₃⟩ := lt_iff_le_and_ne.mp h₁ in
let h₄ := le.dest h₂ in
exists.elim h₄ (λ k, nat.cases_on k
(λ h₅ : a = b, absurd h₅ h₃)
(λ c h₅, exists.intro (succ c) $ and.intro
(λ h₆, nat.no_confusion h₆)
(eq.symm h₅))))
(λ h₁,
let ⟨c, ⟨h₂, h₃⟩⟩ := h₁ in begin
cases c with c',
{unfold positive at h₂,
contradiction},
have h₄ : b = succ a + c',
rw [succ_add, ←add_succ, h₃],
fapply le.intro,
exact c',
exact (eq.symm h₄)
end)
end
-- Exercise 2.2.4. Justify the three statements marked (why?) in the proof of
-- Proposition 2.2.13.
example (b : ℕ) : 0 ≤ b :=
nat.rec_on b
(less_than_or_equal.refl 0)
(λ a h₁, less_than_or_equal.step h₁)
example (a b : ℕ) (h₁ : a > b) : succ a > b :=
less_than_or_equal.step h₁
example (a b : ℕ) (h₁ : a = b) : succ a > b := h₁ ▸ (lt_succ_self _)
-- Exercise 2.2.5. Prove Proposition 2.2.14.
section
parameters (m₀ : ℕ) (P : ℕ → Prop)
example :
(∀ m, m₀ ≤ m) →
(∀ m' m, ((m₀ ≤ m' → m' < m) → P m') → P m) →
(∀ m, m₀ ≤ m → P m) :=
λ h₁ h₂ m,
nat.rec_on m
(λ h₃, h₂ 0 0 (λ h₄, false.elim (not_lt_zero 0 (h₄ h₃))))
(λ n ih h₃,
h₂ (succ n) (succ n) (λ h₄, false.elim (not_succ_le_self (succ n) (h₄ h₃))))
end
-- Exercise 2.2.6. Let n be a natural number, and let P(m) be a property per-
-- taining to the natural numbers such that whenever P(m++) is true, then P(m)
-- is true. Suppose that P(n) is also true. Prove that P(m) is true for all
-- natural numbers m ≤ n; this is known as the principle of backwards induction.
example
(m n : ℕ) (P : ℕ → Prop) (h1 : ∀ {m}, P (succ m) → P m) :
P n → (m ≤ n → P m) :=
nat.rec_on n
(λ h2 h3,
have h4 : m = 0, from eq_zero_of_le_zero h3,
h4.symm ▸ h2)
(λ n' h2 h3 h4,
have h5 : P n', from h1 h3,
have h6 : m < succ n' ∨ m = succ n', from lt_or_eq_of_le h4,
or.elim h6
(assume h7,
have h8 : m ≤ n', from le_of_lt_succ h7,
h2 h5 h8)
(assume h7 : m = succ n', h7.symm ▸ h3))
-- Exercise 2.3.1. Prove Lemma 2.3.2.
example : ∀ n : ℕ, 0 * n = 0 := begin
intro n,
induction n with n ih,
{trivial},
{rw [mul_succ, zero_mul]}
end
example (n m : ℕ) : n * m = m * n :=
nat.rec_on n
(show 0 * m = m * 0, by rw [zero_mul, mul_zero])
(λ n ih, calc
succ n * m = n * m + m : by rw [succ_mul, ih]
... = m * succ n : by rw [mul_succ, ih])
-- Exercise 2.3.2. Prove Lemma 2.3.3.
example (n m : ℕ) : n * m = 0 ↔ n = 0 ∨ m = 0 := begin
apply iff.intro,
{intro h₁,
cases m,
right; trivial,
rw mul_succ at h₁,
have h₂ := eq_zero_of_add_eq_zero_left h₁,
left; assumption},
{intro h₁,
cases h₁ with h₂ h₂,
rw [h₂, zero_mul],
rw [h₂, mul_zero]}
end
-- Exercise 2.3.3. Prove Proposition 2.3.5.
example (a b c : ℕ) : (a * b) * c = a * (b * c) := begin
induction a with a ih,
{repeat {rw zero_mul}},
{rw [succ_mul, succ_mul],
rw [add_mul, ih]}
end
-- Exercise 2.3.4. Prove the identity (a + b)^2 = a^2 + 2ab + b^2
lemma twice_of_add_self {n : ℕ} : n + n = 2 * n := by rw [succ_mul, one_mul]
lemma sq_of_mul_self {n : ℕ} : n * n = n^2 := by unfold pow; rw one_mul
example (a b : ℕ) : (a + b)^2 = a^2 + 2*a*b + b^2 := calc
(a + b)^2 = (a + b) * (a + b) : by unfold pow; rw [one_mul]
... = (a + b) * a + (a + b) * b : by rw [mul_add]
... = (a * a + b * a) + (a * b + b * b) : by repeat {rw add_mul}
... = (a^2 + b * a) + (a * b + b^2) : by rw [sq_of_mul_self, sq_of_mul_self]
... = a^2 + (b * a + b * a) + b^2 :
by rw [mul_comm b a, ←add_assoc, add_assoc (a^2) _ _]
... = a^2 + 2*a*b + b^2 : by rw [twice_of_add_self, mul_comm b a, mul_assoc]
-- Exercise 2.3.5. Prove Proposition 2.3.9.
section
variables (n q : ℕ) (h₁ : q > 0)
include h₁
example : ∃ (m r : ℕ), 0 ≤ r ∧ r < q ∧ n = m * q + r :=
nat.rec_on n
(exists.intro 0 (exists.intro 0
(and.intro
(show 0 ≤ 0, from le_refl 0)
(and.intro
(show q > 0, from h₁)
(show 0 = 0 * q + 0, by rw zero_mul)))))
(λ n ih,
let ⟨m, r, h₁, h₂, h₃⟩ := ih in
have h₄ : succ r ≤ q , from h₂,
let h₅ := lt_or_eq_of_le h₄ in
or.elim h₅
(λ h₆, begin
existsi [m, succ r],
split,
exact zero_le _,
split,
exact h₆,
rw [add_succ, h₃],
end)
(λ h₆, begin
existsi [succ m, 0],
split,
exact zero_le _,
split,
assumption,
rw add_zero,
rw [h₃, ←add_succ, succ_mul, h₆]
end))
end
|
0283133d7f9e0c7d713ca64f3628308207467764 | 856e2e1615a12f95b551ed48fa5b03b245abba44 | /src/topology/compact_open.lean | a265c0a976b0a2a8640ba9da0ef962ede6aaa196 | [
"Apache-2.0"
] | permissive | pimsp/mathlib | 8b77e1ccfab21703ba8fbe65988c7de7765aa0e5 | 913318ca9d6979686996e8d9b5ebf7e74aae1c63 | refs/heads/master | 1,669,812,465,182 | 1,597,133,610,000 | 1,597,133,610,000 | 281,890,685 | 1 | 0 | null | 1,595,491,577,000 | 1,595,491,576,000 | null | UTF-8 | Lean | false | false | 3,809 | lean | /-
Copyright (c) 2018 Reid Barton. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Reid Barton
Type of continuous maps and the compact-open topology on them.
-/
import topology.subset_properties
import topology.continuous_map
import tactic.tidy
open set
open_locale topological_space
namespace continuous_map
section compact_open
variables {α : Type} {β : Type*} {γ : Type*}
variables [topological_space α] [topological_space β] [topological_space γ]
def compact_open.gen (s : set α) (u : set β) : set C(α,β) := {f | f '' s ⊆ u}
-- The compact-open topology on the space of continuous maps α → β.
instance compact_open : topological_space C(α, β) :=
topological_space.generate_from
{m | ∃ (s : set α) (hs : is_compact s) (u : set β) (hu : is_open u), m = compact_open.gen s u}
private lemma is_open_gen {s : set α} (hs : is_compact s) {u : set β} (hu : is_open u) :
is_open (compact_open.gen s u) :=
topological_space.generate_open.basic _ (by dsimp [mem_set_of_eq]; tauto)
section functorial
variables {g : β → γ} (hg : continuous g)
def induced (f : C(α, β)) : C(α, γ) := ⟨g ∘ f, hg.comp f.continuous⟩
private lemma preimage_gen {s : set α} (hs : is_compact s) {u : set γ} (hu : is_open u) :
continuous_map.induced hg ⁻¹' (compact_open.gen s u) = compact_open.gen s (g ⁻¹' u) :=
begin
ext ⟨f, _⟩,
change g ∘ f '' s ⊆ u ↔ f '' s ⊆ g ⁻¹' u,
rw [image_comp, image_subset_iff]
end
/-- C(α, -) is a functor. -/
lemma continuous_induced : continuous (continuous_map.induced hg : C(α, β) → C(α, γ)) :=
continuous_generated_from $ assume m ⟨s, hs, u, hu, hm⟩,
by rw [hm, preimage_gen hg hs hu]; exact is_open_gen hs (hg _ hu)
end functorial
section ev
variables (α β)
def ev (p : C(α, β) × α) : β := p.1 p.2
variables {α β}
-- The evaluation map C(α, β) × α → β is continuous if α is locally compact.
lemma continuous_ev [locally_compact_space α] : continuous (ev α β) :=
continuous_iff_continuous_at.mpr $ assume ⟨f, x⟩ n hn,
let ⟨v, vn, vo, fxv⟩ := mem_nhds_sets_iff.mp hn in
have v ∈ 𝓝 (f x), from mem_nhds_sets vo fxv,
let ⟨s, hs, sv, sc⟩ :=
locally_compact_space.local_compact_nhds x (f ⁻¹' v)
(f.continuous.tendsto x this) in
let ⟨u, us, uo, xu⟩ := mem_nhds_sets_iff.mp hs in
show (ev α β) ⁻¹' n ∈ 𝓝 (f, x), from
let w := set.prod (compact_open.gen s v) u in
have w ⊆ ev α β ⁻¹' n, from assume ⟨f', x'⟩ ⟨hf', hx'⟩, calc
f' x' ∈ f' '' s : mem_image_of_mem f' (us hx')
... ⊆ v : hf'
... ⊆ n : vn,
have is_open w, from is_open_prod (is_open_gen sc vo) uo,
have (f, x) ∈ w, from ⟨image_subset_iff.mpr sv, xu⟩,
mem_nhds_sets_iff.mpr ⟨w, by assumption, by assumption, by assumption⟩
end ev
section coev
variables (α β)
def coev (b : β) : C(α, β × α) := ⟨λ a, (b, a), continuous.prod_mk continuous_const continuous_id⟩
variables {α β}
lemma image_coev {y : β} (s : set α) : (coev α β y) '' s = set.prod {y} s := by tidy
-- The coevaluation map β → C(α, β × α) is continuous (always).
lemma continuous_coev : continuous (coev α β) :=
continuous_generated_from $ begin
rintros _ ⟨s, sc, u, uo, rfl⟩,
rw is_open_iff_forall_mem_open,
intros y hy,
change (coev α β y) '' s ⊆ u at hy,
rw image_coev s at hy,
rcases generalized_tube_lemma compact_singleton sc uo hy
with ⟨v, w, vo, wo, yv, sw, vwu⟩,
refine ⟨v, _, vo, singleton_subset_iff.mp yv⟩,
intros y' hy',
change (coev α β y') '' s ⊆ u,
rw image_coev s,
exact subset.trans (prod_mono (singleton_subset_iff.mpr hy') sw) vwu
end
end coev
end compact_open
end continuous_map
|
257bc40f23b53c960d067f2d38d5543c4e4cb398 | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/analysis/special_functions/trigonometric/basic.lean | 5f261359e7d787af98d78af9f47e6aad316fc745 | [
"Apache-2.0"
] | permissive | robertylewis/mathlib | 3d16e3e6daf5ddde182473e03a1b601d2810952c | 1d13f5b932f5e40a8308e3840f96fc882fae01f0 | refs/heads/master | 1,651,379,945,369 | 1,644,276,960,000 | 1,644,276,960,000 | 98,875,504 | 0 | 0 | Apache-2.0 | 1,644,253,514,000 | 1,501,495,700,000 | Lean | UTF-8 | Lean | false | false | 41,831 | lean | /-
Copyright (c) 2018 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes, Abhimanyu Pallavi Sudhir, Jean Lo, Calle Sönne, Benjamin Davidson
-/
import analysis.special_functions.exp
import data.set.intervals.infinite
/-!
# Trigonometric functions
## Main definitions
This file contains the definition of `π`.
See also `analysis.special_functions.trigonometric.inverse` and
`analysis.special_functions.trigonometric.arctan` for the inverse trigonometric functions.
See also `analysis.special_functions.complex.arg` and
`analysis.special_functions.complex.log` for the complex argument function
and the complex logarithm.
## Main statements
Many basic inequalities on the real trigonometric functions are established.
The continuity of the usual trigonometric functions is proved.
Several facts about the real trigonometric functions have the proofs deferred to
`analysis.special_functions.trigonometric.complex`,
as they are most easily proved by appealing to the corresponding fact for
complex trigonometric functions.
See also `analysis.special_functions.trigonometric.chebyshev` for the multiple angle formulas
in terms of Chebyshev polynomials.
## Tags
sin, cos, tan, angle
-/
noncomputable theory
open_locale classical topological_space filter
open set filter
namespace complex
@[continuity] lemma continuous_sin : continuous sin :=
by { change continuous (λ z, ((exp (-z * I) - exp (z * I)) * I) / 2), continuity, }
lemma continuous_on_sin {s : set ℂ} : continuous_on sin s := continuous_sin.continuous_on
@[continuity] lemma continuous_cos : continuous cos :=
by { change continuous (λ z, (exp (z * I) + exp (-z * I)) / 2), continuity, }
lemma continuous_on_cos {s : set ℂ} : continuous_on cos s := continuous_cos.continuous_on
@[continuity] lemma continuous_sinh : continuous sinh :=
by { change continuous (λ z, (exp z - exp (-z)) / 2), continuity, }
@[continuity] lemma continuous_cosh : continuous cosh :=
by { change continuous (λ z, (exp z + exp (-z)) / 2), continuity, }
end complex
namespace real
variables {x y z : ℝ}
@[continuity] lemma continuous_sin : continuous sin :=
complex.continuous_re.comp (complex.continuous_sin.comp complex.continuous_of_real)
lemma continuous_on_sin {s} : continuous_on sin s :=
continuous_sin.continuous_on
@[continuity] lemma continuous_cos : continuous cos :=
complex.continuous_re.comp (complex.continuous_cos.comp complex.continuous_of_real)
lemma continuous_on_cos {s} : continuous_on cos s := continuous_cos.continuous_on
@[continuity] lemma continuous_sinh : continuous sinh :=
complex.continuous_re.comp (complex.continuous_sinh.comp complex.continuous_of_real)
@[continuity] lemma continuous_cosh : continuous cosh :=
complex.continuous_re.comp (complex.continuous_cosh.comp complex.continuous_of_real)
end real
namespace real
lemma exists_cos_eq_zero : 0 ∈ cos '' Icc (1:ℝ) 2 :=
intermediate_value_Icc' (by norm_num) continuous_on_cos
⟨le_of_lt cos_two_neg, le_of_lt cos_one_pos⟩
/-- The number π = 3.14159265... Defined here using choice as twice a zero of cos in [1,2], from
which one can derive all its properties. For explicit bounds on π, see `data.real.pi.bounds`. -/
protected noncomputable def pi : ℝ := 2 * classical.some exists_cos_eq_zero
localized "notation `π` := real.pi" in real
@[simp] lemma cos_pi_div_two : cos (π / 2) = 0 :=
by rw [real.pi, mul_div_cancel_left _ (@two_ne_zero' ℝ _ _ _)];
exact (classical.some_spec exists_cos_eq_zero).2
lemma one_le_pi_div_two : (1 : ℝ) ≤ π / 2 :=
by rw [real.pi, mul_div_cancel_left _ (@two_ne_zero' ℝ _ _ _)];
exact (classical.some_spec exists_cos_eq_zero).1.1
lemma pi_div_two_le_two : π / 2 ≤ 2 :=
by rw [real.pi, mul_div_cancel_left _ (@two_ne_zero' ℝ _ _ _)];
exact (classical.some_spec exists_cos_eq_zero).1.2
lemma two_le_pi : (2 : ℝ) ≤ π :=
(div_le_div_right (show (0 : ℝ) < 2, by norm_num)).1
(by rw div_self (@two_ne_zero' ℝ _ _ _); exact one_le_pi_div_two)
lemma pi_le_four : π ≤ 4 :=
(div_le_div_right (show (0 : ℝ) < 2, by norm_num)).1
(calc π / 2 ≤ 2 : pi_div_two_le_two
... = 4 / 2 : by norm_num)
lemma pi_pos : 0 < π :=
lt_of_lt_of_le (by norm_num) two_le_pi
lemma pi_ne_zero : π ≠ 0 :=
ne_of_gt pi_pos
lemma pi_div_two_pos : 0 < π / 2 :=
half_pos pi_pos
lemma two_pi_pos : 0 < 2 * π :=
by linarith [pi_pos]
end real
namespace nnreal
open real
open_locale real nnreal
/-- `π` considered as a nonnegative real. -/
noncomputable def pi : ℝ≥0 := ⟨π, real.pi_pos.le⟩
@[simp] lemma coe_real_pi : (pi : ℝ) = π := rfl
lemma pi_pos : 0 < pi := by exact_mod_cast real.pi_pos
lemma pi_ne_zero : pi ≠ 0 := pi_pos.ne'
end nnreal
namespace real
open_locale real
@[simp] lemma sin_pi : sin π = 0 :=
by rw [← mul_div_cancel_left π (@two_ne_zero ℝ _ _), two_mul, add_div,
sin_add, cos_pi_div_two]; simp
@[simp] lemma cos_pi : cos π = -1 :=
by rw [← mul_div_cancel_left π (@two_ne_zero ℝ _ _), mul_div_assoc,
cos_two_mul, cos_pi_div_two];
simp [bit0, pow_add]
@[simp] lemma sin_two_pi : sin (2 * π) = 0 :=
by simp [two_mul, sin_add]
@[simp] lemma cos_two_pi : cos (2 * π) = 1 :=
by simp [two_mul, cos_add]
lemma sin_antiperiodic : function.antiperiodic sin π :=
by simp [sin_add]
lemma sin_periodic : function.periodic sin (2 * π) :=
sin_antiperiodic.periodic
@[simp] lemma sin_add_pi (x : ℝ) : sin (x + π) = -sin x :=
sin_antiperiodic x
@[simp] lemma sin_add_two_pi (x : ℝ) : sin (x + 2 * π) = sin x :=
sin_periodic x
@[simp] lemma sin_sub_pi (x : ℝ) : sin (x - π) = -sin x :=
sin_antiperiodic.sub_eq x
@[simp] lemma sin_sub_two_pi (x : ℝ) : sin (x - 2 * π) = sin x :=
sin_periodic.sub_eq x
@[simp] lemma sin_pi_sub (x : ℝ) : sin (π - x) = sin x :=
neg_neg (sin x) ▸ sin_neg x ▸ sin_antiperiodic.sub_eq'
@[simp] lemma sin_two_pi_sub (x : ℝ) : sin (2 * π - x) = -sin x :=
sin_neg x ▸ sin_periodic.sub_eq'
@[simp] lemma sin_nat_mul_pi (n : ℕ) : sin (n * π) = 0 :=
sin_antiperiodic.nat_mul_eq_of_eq_zero sin_zero n
@[simp] lemma sin_int_mul_pi (n : ℤ) : sin (n * π) = 0 :=
sin_antiperiodic.int_mul_eq_of_eq_zero sin_zero n
@[simp] lemma sin_add_nat_mul_two_pi (x : ℝ) (n : ℕ) : sin (x + n * (2 * π)) = sin x :=
sin_periodic.nat_mul n x
@[simp] lemma sin_add_int_mul_two_pi (x : ℝ) (n : ℤ) : sin (x + n * (2 * π)) = sin x :=
sin_periodic.int_mul n x
@[simp] lemma sin_sub_nat_mul_two_pi (x : ℝ) (n : ℕ) : sin (x - n * (2 * π)) = sin x :=
sin_periodic.sub_nat_mul_eq n
@[simp] lemma sin_sub_int_mul_two_pi (x : ℝ) (n : ℤ) : sin (x - n * (2 * π)) = sin x :=
sin_periodic.sub_int_mul_eq n
@[simp] lemma sin_nat_mul_two_pi_sub (x : ℝ) (n : ℕ) : sin (n * (2 * π) - x) = -sin x :=
sin_neg x ▸ sin_periodic.nat_mul_sub_eq n
@[simp] lemma sin_int_mul_two_pi_sub (x : ℝ) (n : ℤ) : sin (n * (2 * π) - x) = -sin x :=
sin_neg x ▸ sin_periodic.int_mul_sub_eq n
lemma cos_antiperiodic : function.antiperiodic cos π :=
by simp [cos_add]
lemma cos_periodic : function.periodic cos (2 * π) :=
cos_antiperiodic.periodic
@[simp] lemma cos_add_pi (x : ℝ) : cos (x + π) = -cos x :=
cos_antiperiodic x
@[simp] lemma cos_add_two_pi (x : ℝ) : cos (x + 2 * π) = cos x :=
cos_periodic x
@[simp] lemma cos_sub_pi (x : ℝ) : cos (x - π) = -cos x :=
cos_antiperiodic.sub_eq x
@[simp] lemma cos_sub_two_pi (x : ℝ) : cos (x - 2 * π) = cos x :=
cos_periodic.sub_eq x
@[simp] lemma cos_pi_sub (x : ℝ) : cos (π - x) = -cos x :=
cos_neg x ▸ cos_antiperiodic.sub_eq'
@[simp] lemma cos_two_pi_sub (x : ℝ) : cos (2 * π - x) = cos x :=
cos_neg x ▸ cos_periodic.sub_eq'
@[simp] lemma cos_nat_mul_two_pi (n : ℕ) : cos (n * (2 * π)) = 1 :=
(cos_periodic.nat_mul_eq n).trans cos_zero
@[simp] lemma cos_int_mul_two_pi (n : ℤ) : cos (n * (2 * π)) = 1 :=
(cos_periodic.int_mul_eq n).trans cos_zero
@[simp] lemma cos_add_nat_mul_two_pi (x : ℝ) (n : ℕ) : cos (x + n * (2 * π)) = cos x :=
cos_periodic.nat_mul n x
@[simp] lemma cos_add_int_mul_two_pi (x : ℝ) (n : ℤ) : cos (x + n * (2 * π)) = cos x :=
cos_periodic.int_mul n x
@[simp] lemma cos_sub_nat_mul_two_pi (x : ℝ) (n : ℕ) : cos (x - n * (2 * π)) = cos x :=
cos_periodic.sub_nat_mul_eq n
@[simp] lemma cos_sub_int_mul_two_pi (x : ℝ) (n : ℤ) : cos (x - n * (2 * π)) = cos x :=
cos_periodic.sub_int_mul_eq n
@[simp] lemma cos_nat_mul_two_pi_sub (x : ℝ) (n : ℕ) : cos (n * (2 * π) - x) = cos x :=
cos_neg x ▸ cos_periodic.nat_mul_sub_eq n
@[simp] lemma cos_int_mul_two_pi_sub (x : ℝ) (n : ℤ) : cos (n * (2 * π) - x) = cos x :=
cos_neg x ▸ cos_periodic.int_mul_sub_eq n
@[simp] lemma cos_nat_mul_two_pi_add_pi (n : ℕ) : cos (n * (2 * π) + π) = -1 :=
by simpa only [cos_zero] using (cos_periodic.nat_mul n).add_antiperiod_eq cos_antiperiodic
@[simp] lemma cos_int_mul_two_pi_add_pi (n : ℤ) : cos (n * (2 * π) + π) = -1 :=
by simpa only [cos_zero] using (cos_periodic.int_mul n).add_antiperiod_eq cos_antiperiodic
@[simp] lemma cos_nat_mul_two_pi_sub_pi (n : ℕ) : cos (n * (2 * π) - π) = -1 :=
by simpa only [cos_zero] using (cos_periodic.nat_mul n).sub_antiperiod_eq cos_antiperiodic
@[simp] lemma cos_int_mul_two_pi_sub_pi (n : ℤ) : cos (n * (2 * π) - π) = -1 :=
by simpa only [cos_zero] using (cos_periodic.int_mul n).sub_antiperiod_eq cos_antiperiodic
lemma sin_pos_of_pos_of_lt_pi {x : ℝ} (h0x : 0 < x) (hxp : x < π) : 0 < sin x :=
if hx2 : x ≤ 2 then sin_pos_of_pos_of_le_two h0x hx2
else
have (2 : ℝ) + 2 = 4, from rfl,
have π - x ≤ 2, from sub_le_iff_le_add.2
(le_trans pi_le_four (this ▸ add_le_add_left (le_of_not_ge hx2) _)),
sin_pi_sub x ▸ sin_pos_of_pos_of_le_two (sub_pos.2 hxp) this
lemma sin_pos_of_mem_Ioo {x : ℝ} (hx : x ∈ Ioo 0 π) : 0 < sin x :=
sin_pos_of_pos_of_lt_pi hx.1 hx.2
lemma sin_nonneg_of_mem_Icc {x : ℝ} (hx : x ∈ Icc 0 π) : 0 ≤ sin x :=
begin
rw ← closure_Ioo pi_ne_zero.symm at hx,
exact closure_lt_subset_le continuous_const continuous_sin
(closure_mono (λ y, sin_pos_of_mem_Ioo) hx)
end
lemma sin_nonneg_of_nonneg_of_le_pi {x : ℝ} (h0x : 0 ≤ x) (hxp : x ≤ π) : 0 ≤ sin x :=
sin_nonneg_of_mem_Icc ⟨h0x, hxp⟩
lemma sin_neg_of_neg_of_neg_pi_lt {x : ℝ} (hx0 : x < 0) (hpx : -π < x) : sin x < 0 :=
neg_pos.1 $ sin_neg x ▸ sin_pos_of_pos_of_lt_pi (neg_pos.2 hx0) (neg_lt.1 hpx)
lemma sin_nonpos_of_nonnpos_of_neg_pi_le {x : ℝ} (hx0 : x ≤ 0) (hpx : -π ≤ x) : sin x ≤ 0 :=
neg_nonneg.1 $ sin_neg x ▸ sin_nonneg_of_nonneg_of_le_pi (neg_nonneg.2 hx0) (neg_le.1 hpx)
@[simp] lemma sin_pi_div_two : sin (π / 2) = 1 :=
have sin (π / 2) = 1 ∨ sin (π / 2) = -1 :=
by simpa [sq, mul_self_eq_one_iff] using sin_sq_add_cos_sq (π / 2),
this.resolve_right
(λ h, (show ¬(0 : ℝ) < -1, by norm_num) $
h ▸ sin_pos_of_pos_of_lt_pi pi_div_two_pos (half_lt_self pi_pos))
lemma sin_add_pi_div_two (x : ℝ) : sin (x + π / 2) = cos x :=
by simp [sin_add]
lemma sin_sub_pi_div_two (x : ℝ) : sin (x - π / 2) = -cos x :=
by simp [sub_eq_add_neg, sin_add]
lemma sin_pi_div_two_sub (x : ℝ) : sin (π / 2 - x) = cos x :=
by simp [sub_eq_add_neg, sin_add]
lemma cos_add_pi_div_two (x : ℝ) : cos (x + π / 2) = -sin x :=
by simp [cos_add]
lemma cos_sub_pi_div_two (x : ℝ) : cos (x - π / 2) = sin x :=
by simp [sub_eq_add_neg, cos_add]
lemma cos_pi_div_two_sub (x : ℝ) : cos (π / 2 - x) = sin x :=
by rw [← cos_neg, neg_sub, cos_sub_pi_div_two]
lemma cos_pos_of_mem_Ioo {x : ℝ} (hx : x ∈ Ioo (-(π / 2)) (π / 2)) : 0 < cos x :=
sin_add_pi_div_two x ▸ sin_pos_of_mem_Ioo ⟨by linarith [hx.1], by linarith [hx.2]⟩
lemma cos_nonneg_of_mem_Icc {x : ℝ} (hx : x ∈ Icc (-(π / 2)) (π / 2)) : 0 ≤ cos x :=
sin_add_pi_div_two x ▸ sin_nonneg_of_mem_Icc ⟨by linarith [hx.1], by linarith [hx.2]⟩
lemma cos_nonneg_of_neg_pi_div_two_le_of_le {x : ℝ} (hl : -(π / 2) ≤ x) (hu : x ≤ π / 2) :
0 ≤ cos x :=
cos_nonneg_of_mem_Icc ⟨hl, hu⟩
lemma cos_neg_of_pi_div_two_lt_of_lt {x : ℝ} (hx₁ : π / 2 < x) (hx₂ : x < π + π / 2) : cos x < 0 :=
neg_pos.1 $ cos_pi_sub x ▸ cos_pos_of_mem_Ioo ⟨by linarith, by linarith⟩
lemma cos_nonpos_of_pi_div_two_le_of_le {x : ℝ} (hx₁ : π / 2 ≤ x) (hx₂ : x ≤ π + π / 2) :
cos x ≤ 0 :=
neg_nonneg.1 $ cos_pi_sub x ▸ cos_nonneg_of_mem_Icc ⟨by linarith, by linarith⟩
lemma sin_eq_sqrt_one_sub_cos_sq {x : ℝ} (hl : 0 ≤ x) (hu : x ≤ π) :
sin x = sqrt (1 - cos x ^ 2) :=
by rw [← abs_sin_eq_sqrt_one_sub_cos_sq, abs_of_nonneg (sin_nonneg_of_nonneg_of_le_pi hl hu)]
lemma cos_eq_sqrt_one_sub_sin_sq {x : ℝ} (hl : -(π / 2) ≤ x) (hu : x ≤ π / 2) :
cos x = sqrt (1 - sin x ^ 2) :=
by rw [← abs_cos_eq_sqrt_one_sub_sin_sq, abs_of_nonneg (cos_nonneg_of_mem_Icc ⟨hl, hu⟩)]
lemma sin_eq_zero_iff_of_lt_of_lt {x : ℝ} (hx₁ : -π < x) (hx₂ : x < π) :
sin x = 0 ↔ x = 0 :=
⟨λ h, le_antisymm
(le_of_not_gt (λ h0, lt_irrefl (0 : ℝ) $
calc 0 < sin x : sin_pos_of_pos_of_lt_pi h0 hx₂
... = 0 : h))
(le_of_not_gt (λ h0, lt_irrefl (0 : ℝ) $
calc 0 = sin x : h.symm
... < 0 : sin_neg_of_neg_of_neg_pi_lt h0 hx₁)),
λ h, by simp [h]⟩
lemma sin_eq_zero_iff {x : ℝ} : sin x = 0 ↔ ∃ n : ℤ, (n : ℝ) * π = x :=
⟨λ h, ⟨⌊x / π⌋, le_antisymm (sub_nonneg.1 (sub_floor_div_mul_nonneg _ pi_pos))
(sub_nonpos.1 $ le_of_not_gt $ λ h₃,
(sin_pos_of_pos_of_lt_pi h₃ (sub_floor_div_mul_lt _ pi_pos)).ne
(by simp [sub_eq_add_neg, sin_add, h, sin_int_mul_pi]))⟩,
λ ⟨n, hn⟩, hn ▸ sin_int_mul_pi _⟩
lemma sin_ne_zero_iff {x : ℝ} : sin x ≠ 0 ↔ ∀ n : ℤ, (n : ℝ) * π ≠ x :=
by rw [← not_exists, not_iff_not, sin_eq_zero_iff]
lemma sin_eq_zero_iff_cos_eq {x : ℝ} : sin x = 0 ↔ cos x = 1 ∨ cos x = -1 :=
by rw [← mul_self_eq_one_iff, ← sin_sq_add_cos_sq x,
sq, sq, ← sub_eq_iff_eq_add, sub_self];
exact ⟨λ h, by rw [h, mul_zero], eq_zero_of_mul_self_eq_zero ∘ eq.symm⟩
lemma cos_eq_one_iff (x : ℝ) : cos x = 1 ↔ ∃ n : ℤ, (n : ℝ) * (2 * π) = x :=
⟨λ h, let ⟨n, hn⟩ := sin_eq_zero_iff.1 (sin_eq_zero_iff_cos_eq.2 (or.inl h)) in
⟨n / 2, (int.mod_two_eq_zero_or_one n).elim
(λ hn0, by rwa [← mul_assoc, ← @int.cast_two ℝ, ← int.cast_mul, int.div_mul_cancel
((int.dvd_iff_mod_eq_zero _ _).2 hn0)])
(λ hn1, by rw [← int.mod_add_div n 2, hn1, int.cast_add, int.cast_one, add_mul,
one_mul, add_comm, mul_comm (2 : ℤ), int.cast_mul, mul_assoc, int.cast_two] at hn;
rw [← hn, cos_int_mul_two_pi_add_pi] at h;
exact absurd h (by norm_num))⟩,
λ ⟨n, hn⟩, hn ▸ cos_int_mul_two_pi _⟩
lemma cos_eq_one_iff_of_lt_of_lt {x : ℝ} (hx₁ : -(2 * π) < x) (hx₂ : x < 2 * π) :
cos x = 1 ↔ x = 0 :=
⟨λ h,
begin
rcases (cos_eq_one_iff _).1 h with ⟨n, rfl⟩,
rw [mul_lt_iff_lt_one_left two_pi_pos] at hx₂,
rw [neg_lt, neg_mul_eq_neg_mul, mul_lt_iff_lt_one_left two_pi_pos] at hx₁,
norm_cast at hx₁ hx₂,
obtain rfl : n = 0 := le_antisymm (by linarith) (by linarith),
simp
end,
λ h, by simp [h]⟩
lemma cos_lt_cos_of_nonneg_of_le_pi_div_two {x y : ℝ} (hx₁ : 0 ≤ x) (hy₂ : y ≤ π / 2)
(hxy : x < y) :
cos y < cos x :=
begin
rw [← sub_lt_zero, cos_sub_cos],
have : 0 < sin ((y + x) / 2),
{ refine sin_pos_of_pos_of_lt_pi _ _; linarith },
have : 0 < sin ((y - x) / 2),
{ refine sin_pos_of_pos_of_lt_pi _ _; linarith },
nlinarith,
end
lemma cos_lt_cos_of_nonneg_of_le_pi {x y : ℝ} (hx₁ : 0 ≤ x) (hy₂ : y ≤ π) (hxy : x < y) :
cos y < cos x :=
match (le_total x (π / 2) : x ≤ π / 2 ∨ π / 2 ≤ x), le_total y (π / 2) with
| or.inl hx, or.inl hy := cos_lt_cos_of_nonneg_of_le_pi_div_two hx₁ hy hxy
| or.inl hx, or.inr hy := (lt_or_eq_of_le hx).elim
(λ hx, calc cos y ≤ 0 : cos_nonpos_of_pi_div_two_le_of_le hy (by linarith [pi_pos])
... < cos x : cos_pos_of_mem_Ioo ⟨by linarith, hx⟩)
(λ hx, calc cos y < 0 : cos_neg_of_pi_div_two_lt_of_lt (by linarith) (by linarith [pi_pos])
... = cos x : by rw [hx, cos_pi_div_two])
| or.inr hx, or.inl hy := by linarith
| or.inr hx, or.inr hy := neg_lt_neg_iff.1 (by rw [← cos_pi_sub, ← cos_pi_sub];
apply cos_lt_cos_of_nonneg_of_le_pi_div_two; linarith)
end
lemma strict_anti_on_cos : strict_anti_on cos (Icc 0 π) :=
λ x hx y hy hxy, cos_lt_cos_of_nonneg_of_le_pi hx.1 hy.2 hxy
lemma cos_le_cos_of_nonneg_of_le_pi {x y : ℝ} (hx₁ : 0 ≤ x) (hy₂ : y ≤ π) (hxy : x ≤ y) :
cos y ≤ cos x :=
(strict_anti_on_cos.le_iff_le ⟨hx₁.trans hxy, hy₂⟩ ⟨hx₁, hxy.trans hy₂⟩).2 hxy
lemma sin_lt_sin_of_lt_of_le_pi_div_two {x y : ℝ} (hx₁ : -(π / 2) ≤ x)
(hy₂ : y ≤ π / 2) (hxy : x < y) : sin x < sin y :=
by rw [← cos_sub_pi_div_two, ← cos_sub_pi_div_two, ← cos_neg (x - _), ← cos_neg (y - _)];
apply cos_lt_cos_of_nonneg_of_le_pi; linarith
lemma strict_mono_on_sin : strict_mono_on sin (Icc (-(π / 2)) (π / 2)) :=
λ x hx y hy hxy, sin_lt_sin_of_lt_of_le_pi_div_two hx.1 hy.2 hxy
lemma sin_le_sin_of_le_of_le_pi_div_two {x y : ℝ} (hx₁ : -(π / 2) ≤ x)
(hy₂ : y ≤ π / 2) (hxy : x ≤ y) : sin x ≤ sin y :=
(strict_mono_on_sin.le_iff_le ⟨hx₁, hxy.trans hy₂⟩ ⟨hx₁.trans hxy, hy₂⟩).2 hxy
lemma inj_on_sin : inj_on sin (Icc (-(π / 2)) (π / 2)) :=
strict_mono_on_sin.inj_on
lemma inj_on_cos : inj_on cos (Icc 0 π) := strict_anti_on_cos.inj_on
lemma surj_on_sin : surj_on sin (Icc (-(π / 2)) (π / 2)) (Icc (-1) 1) :=
by simpa only [sin_neg, sin_pi_div_two]
using intermediate_value_Icc (neg_le_self pi_div_two_pos.le) continuous_sin.continuous_on
lemma surj_on_cos : surj_on cos (Icc 0 π) (Icc (-1) 1) :=
by simpa only [cos_zero, cos_pi]
using intermediate_value_Icc' pi_pos.le continuous_cos.continuous_on
lemma sin_mem_Icc (x : ℝ) : sin x ∈ Icc (-1 : ℝ) 1 := ⟨neg_one_le_sin x, sin_le_one x⟩
lemma cos_mem_Icc (x : ℝ) : cos x ∈ Icc (-1 : ℝ) 1 := ⟨neg_one_le_cos x, cos_le_one x⟩
lemma maps_to_sin (s : set ℝ) : maps_to sin s (Icc (-1 : ℝ) 1) := λ x _, sin_mem_Icc x
lemma maps_to_cos (s : set ℝ) : maps_to cos s (Icc (-1 : ℝ) 1) := λ x _, cos_mem_Icc x
lemma bij_on_sin : bij_on sin (Icc (-(π / 2)) (π / 2)) (Icc (-1) 1) :=
⟨maps_to_sin _, inj_on_sin, surj_on_sin⟩
lemma bij_on_cos : bij_on cos (Icc 0 π) (Icc (-1) 1) :=
⟨maps_to_cos _, inj_on_cos, surj_on_cos⟩
@[simp] lemma range_cos : range cos = (Icc (-1) 1 : set ℝ) :=
subset.antisymm (range_subset_iff.2 cos_mem_Icc) surj_on_cos.subset_range
@[simp] lemma range_sin : range sin = (Icc (-1) 1 : set ℝ) :=
subset.antisymm (range_subset_iff.2 sin_mem_Icc) surj_on_sin.subset_range
lemma range_cos_infinite : (range real.cos).infinite :=
by { rw real.range_cos, exact Icc.infinite (by norm_num) }
lemma range_sin_infinite : (range real.sin).infinite :=
by { rw real.range_sin, exact Icc.infinite (by norm_num) }
lemma sin_lt {x : ℝ} (h : 0 < x) : sin x < x :=
begin
cases le_or_gt x 1 with h' h',
{ have hx : |x| = x := abs_of_nonneg (le_of_lt h),
have : |x| ≤ 1, rwa [hx],
have := sin_bound this, rw [abs_le] at this,
have := this.2, rw [sub_le_iff_le_add', hx] at this,
apply lt_of_le_of_lt this, rw [sub_add], apply lt_of_lt_of_le _ (le_of_eq (sub_zero x)),
apply sub_lt_sub_left, rw [sub_pos, div_eq_mul_inv (x ^ 3)], apply mul_lt_mul',
{ rw [pow_succ x 3], refine le_trans _ (le_of_eq (one_mul _)),
rw mul_le_mul_right, exact h', apply pow_pos h },
norm_num, norm_num, apply pow_pos h },
exact lt_of_le_of_lt (sin_le_one x) h'
end
/- note 1: this inequality is not tight, the tighter inequality is sin x > x - x ^ 3 / 6.
note 2: this is also true for x > 1, but it's nontrivial for x just above 1. -/
lemma sin_gt_sub_cube {x : ℝ} (h : 0 < x) (h' : x ≤ 1) : x - x ^ 3 / 4 < sin x :=
begin
have hx : |x| = x := abs_of_nonneg (le_of_lt h),
have : |x| ≤ 1, rwa [hx],
have := sin_bound this, rw [abs_le] at this,
have := this.1, rw [le_sub_iff_add_le, hx] at this,
refine lt_of_lt_of_le _ this,
rw [add_comm, sub_add, sub_neg_eq_add], apply sub_lt_sub_left,
apply add_lt_of_lt_sub_left,
rw (show x ^ 3 / 4 - x ^ 3 / 6 = x ^ 3 * 12⁻¹,
by simp [div_eq_mul_inv, ← mul_sub]; norm_num),
apply mul_lt_mul',
{ rw [pow_succ x 3], refine le_trans _ (le_of_eq (one_mul _)),
rw mul_le_mul_right, exact h', apply pow_pos h },
norm_num, norm_num, apply pow_pos h
end
section cos_div_sq
variable (x : ℝ)
/-- the series `sqrt_two_add_series x n` is `sqrt(2 + sqrt(2 + ... ))` with `n` square roots,
starting with `x`. We define it here because `cos (pi / 2 ^ (n+1)) = sqrt_two_add_series 0 n / 2`
-/
@[simp, pp_nodot] noncomputable def sqrt_two_add_series (x : ℝ) : ℕ → ℝ
| 0 := x
| (n+1) := sqrt (2 + sqrt_two_add_series n)
lemma sqrt_two_add_series_zero : sqrt_two_add_series x 0 = x := by simp
lemma sqrt_two_add_series_one : sqrt_two_add_series 0 1 = sqrt 2 := by simp
lemma sqrt_two_add_series_two : sqrt_two_add_series 0 2 = sqrt (2 + sqrt 2) := by simp
lemma sqrt_two_add_series_zero_nonneg : ∀(n : ℕ), 0 ≤ sqrt_two_add_series 0 n
| 0 := le_refl 0
| (n+1) := sqrt_nonneg _
lemma sqrt_two_add_series_nonneg {x : ℝ} (h : 0 ≤ x) : ∀(n : ℕ), 0 ≤ sqrt_two_add_series x n
| 0 := h
| (n+1) := sqrt_nonneg _
lemma sqrt_two_add_series_lt_two : ∀(n : ℕ), sqrt_two_add_series 0 n < 2
| 0 := by norm_num
| (n+1) :=
begin
refine lt_of_lt_of_le _ (sqrt_sq zero_lt_two.le).le,
rw [sqrt_two_add_series, sqrt_lt_sqrt_iff, ← lt_sub_iff_add_lt'],
{ refine (sqrt_two_add_series_lt_two n).trans_le _, norm_num },
{ exact add_nonneg zero_le_two (sqrt_two_add_series_zero_nonneg n) }
end
lemma sqrt_two_add_series_succ (x : ℝ) :
∀(n : ℕ), sqrt_two_add_series x (n+1) = sqrt_two_add_series (sqrt (2 + x)) n
| 0 := rfl
| (n+1) := by rw [sqrt_two_add_series, sqrt_two_add_series_succ, sqrt_two_add_series]
lemma sqrt_two_add_series_monotone_left {x y : ℝ} (h : x ≤ y) :
∀(n : ℕ), sqrt_two_add_series x n ≤ sqrt_two_add_series y n
| 0 := h
| (n+1) :=
begin
rw [sqrt_two_add_series, sqrt_two_add_series],
exact sqrt_le_sqrt (add_le_add_left (sqrt_two_add_series_monotone_left _) _)
end
@[simp] lemma cos_pi_over_two_pow : ∀(n : ℕ), cos (π / 2 ^ (n+1)) = sqrt_two_add_series 0 n / 2
| 0 := by simp
| (n+1) :=
begin
have : (2 : ℝ) ≠ 0 := two_ne_zero,
symmetry, rw [div_eq_iff_mul_eq this], symmetry,
rw [sqrt_two_add_series, sqrt_eq_iff_sq_eq, mul_pow, cos_sq, ←mul_div_assoc,
nat.add_succ, pow_succ, mul_div_mul_left _ _ this, cos_pi_over_two_pow, add_mul],
congr, { norm_num },
rw [mul_comm, sq, mul_assoc, ←mul_div_assoc, mul_div_cancel_left, ←mul_div_assoc,
mul_div_cancel_left]; try { exact this },
apply add_nonneg, norm_num, apply sqrt_two_add_series_zero_nonneg, norm_num,
apply le_of_lt, apply cos_pos_of_mem_Ioo ⟨_, _⟩,
{ transitivity (0 : ℝ), rw neg_lt_zero, apply pi_div_two_pos,
apply div_pos pi_pos, apply pow_pos, norm_num },
apply div_lt_div' (le_refl π) _ pi_pos _,
refine lt_of_le_of_lt (le_of_eq (pow_one _).symm) _,
apply pow_lt_pow, norm_num, apply nat.succ_lt_succ, apply nat.succ_pos, all_goals {norm_num}
end
lemma sin_sq_pi_over_two_pow (n : ℕ) :
sin (π / 2 ^ (n+1)) ^ 2 = 1 - (sqrt_two_add_series 0 n / 2) ^ 2 :=
by rw [sin_sq, cos_pi_over_two_pow]
lemma sin_sq_pi_over_two_pow_succ (n : ℕ) :
sin (π / 2 ^ (n+2)) ^ 2 = 1 / 2 - sqrt_two_add_series 0 n / 4 :=
begin
rw [sin_sq_pi_over_two_pow, sqrt_two_add_series, div_pow, sq_sqrt, add_div, ←sub_sub],
congr, norm_num, norm_num, apply add_nonneg, norm_num, apply sqrt_two_add_series_zero_nonneg,
end
@[simp] lemma sin_pi_over_two_pow_succ (n : ℕ) :
sin (π / 2 ^ (n+2)) = sqrt (2 - sqrt_two_add_series 0 n) / 2 :=
begin
symmetry, rw [div_eq_iff_mul_eq], symmetry,
rw [sqrt_eq_iff_sq_eq, mul_pow, sin_sq_pi_over_two_pow_succ, sub_mul],
{ congr, norm_num, rw [mul_comm], convert mul_div_cancel' _ _, norm_num, norm_num },
{ rw [sub_nonneg], apply le_of_lt, apply sqrt_two_add_series_lt_two },
apply le_of_lt, apply mul_pos, apply sin_pos_of_pos_of_lt_pi,
{ apply div_pos pi_pos, apply pow_pos, norm_num },
refine lt_of_lt_of_le _ (le_of_eq (div_one _)), rw [div_lt_div_left],
refine lt_of_le_of_lt (le_of_eq (pow_zero 2).symm) _,
apply pow_lt_pow, norm_num, apply nat.succ_pos, apply pi_pos,
apply pow_pos, all_goals {norm_num}
end
@[simp] lemma cos_pi_div_four : cos (π / 4) = sqrt 2 / 2 :=
by { transitivity cos (π / 2 ^ 2), congr, norm_num, simp }
@[simp] lemma sin_pi_div_four : sin (π / 4) = sqrt 2 / 2 :=
by { transitivity sin (π / 2 ^ 2), congr, norm_num, simp }
@[simp] lemma cos_pi_div_eight : cos (π / 8) = sqrt (2 + sqrt 2) / 2 :=
by { transitivity cos (π / 2 ^ 3), congr, norm_num, simp }
@[simp] lemma sin_pi_div_eight : sin (π / 8) = sqrt (2 - sqrt 2) / 2 :=
by { transitivity sin (π / 2 ^ 3), congr, norm_num, simp }
@[simp] lemma cos_pi_div_sixteen : cos (π / 16) = sqrt (2 + sqrt (2 + sqrt 2)) / 2 :=
by { transitivity cos (π / 2 ^ 4), congr, norm_num, simp }
@[simp] lemma sin_pi_div_sixteen : sin (π / 16) = sqrt (2 - sqrt (2 + sqrt 2)) / 2 :=
by { transitivity sin (π / 2 ^ 4), congr, norm_num, simp }
@[simp] lemma cos_pi_div_thirty_two : cos (π / 32) = sqrt (2 + sqrt (2 + sqrt (2 + sqrt 2))) / 2 :=
by { transitivity cos (π / 2 ^ 5), congr, norm_num, simp }
@[simp] lemma sin_pi_div_thirty_two : sin (π / 32) = sqrt (2 - sqrt (2 + sqrt (2 + sqrt 2))) / 2 :=
by { transitivity sin (π / 2 ^ 5), congr, norm_num, simp }
-- This section is also a convenient location for other explicit values of `sin` and `cos`.
/-- The cosine of `π / 3` is `1 / 2`. -/
@[simp] lemma cos_pi_div_three : cos (π / 3) = 1 / 2 :=
begin
have h₁ : (2 * cos (π / 3) - 1) ^ 2 * (2 * cos (π / 3) + 2) = 0,
{ have : cos (3 * (π / 3)) = cos π := by { congr' 1, ring },
linarith [cos_pi, cos_three_mul (π / 3)] },
cases mul_eq_zero.mp h₁ with h h,
{ linarith [pow_eq_zero h] },
{ have : cos π < cos (π / 3),
{ refine cos_lt_cos_of_nonneg_of_le_pi _ rfl.ge _;
linarith [pi_pos] },
linarith [cos_pi] }
end
/-- The square of the cosine of `π / 6` is `3 / 4` (this is sometimes more convenient than the
result for cosine itself). -/
lemma sq_cos_pi_div_six : cos (π / 6) ^ 2 = 3 / 4 :=
begin
have h1 : cos (π / 6) ^ 2 = 1 / 2 + 1 / 2 / 2,
{ convert cos_sq (π / 6),
have h2 : 2 * (π / 6) = π / 3 := by cancel_denoms,
rw [h2, cos_pi_div_three] },
rw ← sub_eq_zero at h1 ⊢,
convert h1 using 1,
ring
end
/-- The cosine of `π / 6` is `√3 / 2`. -/
@[simp] lemma cos_pi_div_six : cos (π / 6) = (sqrt 3) / 2 :=
begin
suffices : sqrt 3 = cos (π / 6) * 2,
{ field_simp [(by norm_num : 0 ≠ 2)], exact this.symm },
rw sqrt_eq_iff_sq_eq,
{ have h1 := (mul_right_inj' (by norm_num : (4:ℝ) ≠ 0)).mpr sq_cos_pi_div_six,
rw ← sub_eq_zero at h1 ⊢,
convert h1 using 1,
ring },
{ norm_num },
{ have : 0 < cos (π / 6) := by { apply cos_pos_of_mem_Ioo; split; linarith [pi_pos] },
linarith },
end
/-- The sine of `π / 6` is `1 / 2`. -/
@[simp] lemma sin_pi_div_six : sin (π / 6) = 1 / 2 :=
begin
rw [← cos_pi_div_two_sub, ← cos_pi_div_three],
congr,
ring
end
/-- The square of the sine of `π / 3` is `3 / 4` (this is sometimes more convenient than the
result for cosine itself). -/
lemma sq_sin_pi_div_three : sin (π / 3) ^ 2 = 3 / 4 :=
begin
rw [← cos_pi_div_two_sub, ← sq_cos_pi_div_six],
congr,
ring
end
/-- The sine of `π / 3` is `√3 / 2`. -/
@[simp] lemma sin_pi_div_three : sin (π / 3) = (sqrt 3) / 2 :=
begin
rw [← cos_pi_div_two_sub, ← cos_pi_div_six],
congr,
ring
end
end cos_div_sq
/-- `real.sin` as an `order_iso` between `[-(π / 2), π / 2]` and `[-1, 1]`. -/
def sin_order_iso : Icc (-(π / 2)) (π / 2) ≃o Icc (-1:ℝ) 1 :=
(strict_mono_on_sin.order_iso _ _).trans $ order_iso.set_congr _ _ bij_on_sin.image_eq
@[simp] lemma coe_sin_order_iso_apply (x : Icc (-(π / 2)) (π / 2)) :
(sin_order_iso x : ℝ) = sin x := rfl
lemma sin_order_iso_apply (x : Icc (-(π / 2)) (π / 2)) :
sin_order_iso x = ⟨sin x, sin_mem_Icc x⟩ := rfl
@[simp] lemma tan_pi_div_four : tan (π / 4) = 1 :=
begin
rw [tan_eq_sin_div_cos, cos_pi_div_four, sin_pi_div_four],
have h : (sqrt 2) / 2 > 0 := by cancel_denoms,
exact div_self (ne_of_gt h),
end
@[simp] lemma tan_pi_div_two : tan (π / 2) = 0 := by simp [tan_eq_sin_div_cos]
lemma tan_pos_of_pos_of_lt_pi_div_two {x : ℝ} (h0x : 0 < x) (hxp : x < π / 2) : 0 < tan x :=
by rw tan_eq_sin_div_cos; exact div_pos (sin_pos_of_pos_of_lt_pi h0x (by linarith))
(cos_pos_of_mem_Ioo ⟨by linarith, hxp⟩)
lemma tan_nonneg_of_nonneg_of_le_pi_div_two {x : ℝ} (h0x : 0 ≤ x) (hxp : x ≤ π / 2) : 0 ≤ tan x :=
match lt_or_eq_of_le h0x, lt_or_eq_of_le hxp with
| or.inl hx0, or.inl hxp := le_of_lt (tan_pos_of_pos_of_lt_pi_div_two hx0 hxp)
| or.inl hx0, or.inr hxp := by simp [hxp, tan_eq_sin_div_cos]
| or.inr hx0, _ := by simp [hx0.symm]
end
lemma tan_neg_of_neg_of_pi_div_two_lt {x : ℝ} (hx0 : x < 0) (hpx : -(π / 2) < x) : tan x < 0 :=
neg_pos.1 (tan_neg x ▸ tan_pos_of_pos_of_lt_pi_div_two (by linarith) (by linarith [pi_pos]))
lemma tan_nonpos_of_nonpos_of_neg_pi_div_two_le {x : ℝ} (hx0 : x ≤ 0) (hpx : -(π / 2) ≤ x) :
tan x ≤ 0 :=
neg_nonneg.1 (tan_neg x ▸ tan_nonneg_of_nonneg_of_le_pi_div_two (by linarith) (by linarith))
lemma tan_lt_tan_of_nonneg_of_lt_pi_div_two {x y : ℝ}
(hx₁ : 0 ≤ x) (hy₂ : y < π / 2) (hxy : x < y) :
tan x < tan y :=
begin
rw [tan_eq_sin_div_cos, tan_eq_sin_div_cos],
exact div_lt_div
(sin_lt_sin_of_lt_of_le_pi_div_two (by linarith) (le_of_lt hy₂) hxy)
(cos_le_cos_of_nonneg_of_le_pi hx₁ (by linarith) (le_of_lt hxy))
(sin_nonneg_of_nonneg_of_le_pi (by linarith) (by linarith))
(cos_pos_of_mem_Ioo ⟨by linarith, hy₂⟩)
end
lemma tan_lt_tan_of_lt_of_lt_pi_div_two {x y : ℝ} (hx₁ : -(π / 2) < x)
(hy₂ : y < π / 2) (hxy : x < y) : tan x < tan y :=
match le_total x 0, le_total y 0 with
| or.inl hx0, or.inl hy0 := neg_lt_neg_iff.1 $ by rw [← tan_neg, ← tan_neg]; exact
tan_lt_tan_of_nonneg_of_lt_pi_div_two (neg_nonneg.2 hy0)
(neg_lt.2 hx₁) (neg_lt_neg hxy)
| or.inl hx0, or.inr hy0 := (lt_or_eq_of_le hy0).elim
(λ hy0, calc tan x ≤ 0 : tan_nonpos_of_nonpos_of_neg_pi_div_two_le hx0 (le_of_lt hx₁)
... < tan y : tan_pos_of_pos_of_lt_pi_div_two hy0 hy₂)
(λ hy0, by rw [← hy0, tan_zero]; exact
tan_neg_of_neg_of_pi_div_two_lt (hy0.symm ▸ hxy) hx₁)
| or.inr hx0, or.inl hy0 := by linarith
| or.inr hx0, or.inr hy0 := tan_lt_tan_of_nonneg_of_lt_pi_div_two hx0 hy₂ hxy
end
lemma strict_mono_on_tan : strict_mono_on tan (Ioo (-(π / 2)) (π / 2)) :=
λ x hx y hy, tan_lt_tan_of_lt_of_lt_pi_div_two hx.1 hy.2
lemma inj_on_tan : inj_on tan (Ioo (-(π / 2)) (π / 2)) :=
strict_mono_on_tan.inj_on
lemma tan_inj_of_lt_of_lt_pi_div_two {x y : ℝ} (hx₁ : -(π / 2) < x) (hx₂ : x < π / 2)
(hy₁ : -(π / 2) < y) (hy₂ : y < π / 2) (hxy : tan x = tan y) : x = y :=
inj_on_tan ⟨hx₁, hx₂⟩ ⟨hy₁, hy₂⟩ hxy
lemma tan_periodic : function.periodic tan π :=
by simpa only [function.periodic, tan_eq_sin_div_cos] using sin_antiperiodic.div cos_antiperiodic
lemma tan_add_pi (x : ℝ) : tan (x + π) = tan x :=
tan_periodic x
lemma tan_sub_pi (x : ℝ) : tan (x - π) = tan x :=
tan_periodic.sub_eq x
lemma tan_pi_sub (x : ℝ) : tan (π - x) = -tan x :=
tan_neg x ▸ tan_periodic.sub_eq'
lemma tan_nat_mul_pi (n : ℕ) : tan (n * π) = 0 :=
tan_zero ▸ tan_periodic.nat_mul_eq n
lemma tan_int_mul_pi (n : ℤ) : tan (n * π) = 0 :=
tan_zero ▸ tan_periodic.int_mul_eq n
lemma tan_add_nat_mul_pi (x : ℝ) (n : ℕ) : tan (x + n * π) = tan x :=
tan_periodic.nat_mul n x
lemma tan_add_int_mul_pi (x : ℝ) (n : ℤ) : tan (x + n * π) = tan x :=
tan_periodic.int_mul n x
lemma tan_sub_nat_mul_pi (x : ℝ) (n : ℕ) : tan (x - n * π) = tan x :=
tan_periodic.sub_nat_mul_eq n
lemma tan_sub_int_mul_pi (x : ℝ) (n : ℤ) : tan (x - n * π) = tan x :=
tan_periodic.sub_int_mul_eq n
lemma tan_nat_mul_pi_sub (x : ℝ) (n : ℕ) : tan (n * π - x) = -tan x :=
tan_neg x ▸ tan_periodic.nat_mul_sub_eq n
lemma tan_int_mul_pi_sub (x : ℝ) (n : ℤ) : tan (n * π - x) = -tan x :=
tan_neg x ▸ tan_periodic.int_mul_sub_eq n
lemma tendsto_sin_pi_div_two : tendsto sin (𝓝[<] (π/2)) (𝓝 1) :=
by { convert continuous_sin.continuous_within_at, simp }
lemma tendsto_cos_pi_div_two : tendsto cos (𝓝[<] (π/2)) (𝓝[>] 0) :=
begin
apply tendsto_nhds_within_of_tendsto_nhds_of_eventually_within,
{ convert continuous_cos.continuous_within_at, simp },
{ filter_upwards [Ioo_mem_nhds_within_Iio (right_mem_Ioc.mpr (neg_lt_self pi_div_two_pos))]
with x hx using cos_pos_of_mem_Ioo hx },
end
lemma tendsto_tan_pi_div_two : tendsto tan (𝓝[<] (π/2)) at_top :=
begin
convert tendsto_cos_pi_div_two.inv_tendsto_zero.at_top_mul zero_lt_one
tendsto_sin_pi_div_two,
simp only [pi.inv_apply, ← div_eq_inv_mul, ← tan_eq_sin_div_cos]
end
lemma tendsto_sin_neg_pi_div_two : tendsto sin (𝓝[>] (-(π/2))) (𝓝 (-1)) :=
by { convert continuous_sin.continuous_within_at, simp }
lemma tendsto_cos_neg_pi_div_two : tendsto cos (𝓝[>] (-(π/2))) (𝓝[>] 0) :=
begin
apply tendsto_nhds_within_of_tendsto_nhds_of_eventually_within,
{ convert continuous_cos.continuous_within_at, simp },
{ filter_upwards [Ioo_mem_nhds_within_Ioi (left_mem_Ico.mpr (neg_lt_self pi_div_two_pos))]
with x hx using cos_pos_of_mem_Ioo hx },
end
lemma tendsto_tan_neg_pi_div_two : tendsto tan (𝓝[>] (-(π/2))) at_bot :=
begin
convert tendsto_cos_neg_pi_div_two.inv_tendsto_zero.at_top_mul_neg (by norm_num)
tendsto_sin_neg_pi_div_two,
simp only [pi.inv_apply, ← div_eq_inv_mul, ← tan_eq_sin_div_cos]
end
end real
namespace complex
open_locale real
lemma sin_eq_zero_iff_cos_eq {z : ℂ} : sin z = 0 ↔ cos z = 1 ∨ cos z = -1 :=
by rw [← mul_self_eq_one_iff, ← sin_sq_add_cos_sq, sq, sq, ← sub_eq_iff_eq_add, sub_self];
exact ⟨λ h, by rw [h, mul_zero], eq_zero_of_mul_self_eq_zero ∘ eq.symm⟩
@[simp] lemma cos_pi_div_two : cos (π / 2) = 0 :=
calc cos (π / 2) = real.cos (π / 2) : by rw [of_real_cos]; simp
... = 0 : by simp
@[simp] lemma sin_pi_div_two : sin (π / 2) = 1 :=
calc sin (π / 2) = real.sin (π / 2) : by rw [of_real_sin]; simp
... = 1 : by simp
@[simp] lemma sin_pi : sin π = 0 :=
by rw [← of_real_sin, real.sin_pi]; simp
@[simp] lemma cos_pi : cos π = -1 :=
by rw [← of_real_cos, real.cos_pi]; simp
@[simp] lemma sin_two_pi : sin (2 * π) = 0 :=
by simp [two_mul, sin_add]
@[simp] lemma cos_two_pi : cos (2 * π) = 1 :=
by simp [two_mul, cos_add]
lemma sin_antiperiodic : function.antiperiodic sin π :=
by simp [sin_add]
lemma sin_periodic : function.periodic sin (2 * π) :=
sin_antiperiodic.periodic
lemma sin_add_pi (x : ℂ) : sin (x + π) = -sin x :=
sin_antiperiodic x
lemma sin_add_two_pi (x : ℂ) : sin (x + 2 * π) = sin x :=
sin_periodic x
lemma sin_sub_pi (x : ℂ) : sin (x - π) = -sin x :=
sin_antiperiodic.sub_eq x
lemma sin_sub_two_pi (x : ℂ) : sin (x - 2 * π) = sin x :=
sin_periodic.sub_eq x
lemma sin_pi_sub (x : ℂ) : sin (π - x) = sin x :=
neg_neg (sin x) ▸ sin_neg x ▸ sin_antiperiodic.sub_eq'
lemma sin_two_pi_sub (x : ℂ) : sin (2 * π - x) = -sin x :=
sin_neg x ▸ sin_periodic.sub_eq'
lemma sin_nat_mul_pi (n : ℕ) : sin (n * π) = 0 :=
sin_antiperiodic.nat_mul_eq_of_eq_zero sin_zero n
lemma sin_int_mul_pi (n : ℤ) : sin (n * π) = 0 :=
sin_antiperiodic.int_mul_eq_of_eq_zero sin_zero n
lemma sin_add_nat_mul_two_pi (x : ℂ) (n : ℕ) : sin (x + n * (2 * π)) = sin x :=
sin_periodic.nat_mul n x
lemma sin_add_int_mul_two_pi (x : ℂ) (n : ℤ) : sin (x + n * (2 * π)) = sin x :=
sin_periodic.int_mul n x
lemma sin_sub_nat_mul_two_pi (x : ℂ) (n : ℕ) : sin (x - n * (2 * π)) = sin x :=
sin_periodic.sub_nat_mul_eq n
lemma sin_sub_int_mul_two_pi (x : ℂ) (n : ℤ) : sin (x - n * (2 * π)) = sin x :=
sin_periodic.sub_int_mul_eq n
lemma sin_nat_mul_two_pi_sub (x : ℂ) (n : ℕ) : sin (n * (2 * π) - x) = -sin x :=
sin_neg x ▸ sin_periodic.nat_mul_sub_eq n
lemma sin_int_mul_two_pi_sub (x : ℂ) (n : ℤ) : sin (n * (2 * π) - x) = -sin x :=
sin_neg x ▸ sin_periodic.int_mul_sub_eq n
lemma cos_antiperiodic : function.antiperiodic cos π :=
by simp [cos_add]
lemma cos_periodic : function.periodic cos (2 * π) :=
cos_antiperiodic.periodic
lemma cos_add_pi (x : ℂ) : cos (x + π) = -cos x :=
cos_antiperiodic x
lemma cos_add_two_pi (x : ℂ) : cos (x + 2 * π) = cos x :=
cos_periodic x
lemma cos_sub_pi (x : ℂ) : cos (x - π) = -cos x :=
cos_antiperiodic.sub_eq x
lemma cos_sub_two_pi (x : ℂ) : cos (x - 2 * π) = cos x :=
cos_periodic.sub_eq x
lemma cos_pi_sub (x : ℂ) : cos (π - x) = -cos x :=
cos_neg x ▸ cos_antiperiodic.sub_eq'
lemma cos_two_pi_sub (x : ℂ) : cos (2 * π - x) = cos x :=
cos_neg x ▸ cos_periodic.sub_eq'
lemma cos_nat_mul_two_pi (n : ℕ) : cos (n * (2 * π)) = 1 :=
(cos_periodic.nat_mul_eq n).trans cos_zero
lemma cos_int_mul_two_pi (n : ℤ) : cos (n * (2 * π)) = 1 :=
(cos_periodic.int_mul_eq n).trans cos_zero
lemma cos_add_nat_mul_two_pi (x : ℂ) (n : ℕ) : cos (x + n * (2 * π)) = cos x :=
cos_periodic.nat_mul n x
lemma cos_add_int_mul_two_pi (x : ℂ) (n : ℤ) : cos (x + n * (2 * π)) = cos x :=
cos_periodic.int_mul n x
lemma cos_sub_nat_mul_two_pi (x : ℂ) (n : ℕ) : cos (x - n * (2 * π)) = cos x :=
cos_periodic.sub_nat_mul_eq n
lemma cos_sub_int_mul_two_pi (x : ℂ) (n : ℤ) : cos (x - n * (2 * π)) = cos x :=
cos_periodic.sub_int_mul_eq n
lemma cos_nat_mul_two_pi_sub (x : ℂ) (n : ℕ) : cos (n * (2 * π) - x) = cos x :=
cos_neg x ▸ cos_periodic.nat_mul_sub_eq n
lemma cos_int_mul_two_pi_sub (x : ℂ) (n : ℤ) : cos (n * (2 * π) - x) = cos x :=
cos_neg x ▸ cos_periodic.int_mul_sub_eq n
lemma cos_nat_mul_two_pi_add_pi (n : ℕ) : cos (n * (2 * π) + π) = -1 :=
by simpa only [cos_zero] using (cos_periodic.nat_mul n).add_antiperiod_eq cos_antiperiodic
lemma cos_int_mul_two_pi_add_pi (n : ℤ) : cos (n * (2 * π) + π) = -1 :=
by simpa only [cos_zero] using (cos_periodic.int_mul n).add_antiperiod_eq cos_antiperiodic
lemma cos_nat_mul_two_pi_sub_pi (n : ℕ) : cos (n * (2 * π) - π) = -1 :=
by simpa only [cos_zero] using (cos_periodic.nat_mul n).sub_antiperiod_eq cos_antiperiodic
lemma cos_int_mul_two_pi_sub_pi (n : ℤ) : cos (n * (2 * π) - π) = -1 :=
by simpa only [cos_zero] using (cos_periodic.int_mul n).sub_antiperiod_eq cos_antiperiodic
lemma sin_add_pi_div_two (x : ℂ) : sin (x + π / 2) = cos x :=
by simp [sin_add]
lemma sin_sub_pi_div_two (x : ℂ) : sin (x - π / 2) = -cos x :=
by simp [sub_eq_add_neg, sin_add]
lemma sin_pi_div_two_sub (x : ℂ) : sin (π / 2 - x) = cos x :=
by simp [sub_eq_add_neg, sin_add]
lemma cos_add_pi_div_two (x : ℂ) : cos (x + π / 2) = -sin x :=
by simp [cos_add]
lemma cos_sub_pi_div_two (x : ℂ) : cos (x - π / 2) = sin x :=
by simp [sub_eq_add_neg, cos_add]
lemma cos_pi_div_two_sub (x : ℂ) : cos (π / 2 - x) = sin x :=
by rw [← cos_neg, neg_sub, cos_sub_pi_div_two]
lemma tan_periodic : function.periodic tan π :=
by simpa only [tan_eq_sin_div_cos] using sin_antiperiodic.div cos_antiperiodic
lemma tan_add_pi (x : ℂ) : tan (x + π) = tan x :=
tan_periodic x
lemma tan_sub_pi (x : ℂ) : tan (x - π) = tan x :=
tan_periodic.sub_eq x
lemma tan_pi_sub (x : ℂ) : tan (π - x) = -tan x :=
tan_neg x ▸ tan_periodic.sub_eq'
lemma tan_nat_mul_pi (n : ℕ) : tan (n * π) = 0 :=
tan_zero ▸ tan_periodic.nat_mul_eq n
lemma tan_int_mul_pi (n : ℤ) : tan (n * π) = 0 :=
tan_zero ▸ tan_periodic.int_mul_eq n
lemma tan_add_nat_mul_pi (x : ℂ) (n : ℕ) : tan (x + n * π) = tan x :=
tan_periodic.nat_mul n x
lemma tan_add_int_mul_pi (x : ℂ) (n : ℤ) : tan (x + n * π) = tan x :=
tan_periodic.int_mul n x
lemma tan_sub_nat_mul_pi (x : ℂ) (n : ℕ) : tan (x - n * π) = tan x :=
tan_periodic.sub_nat_mul_eq n
lemma tan_sub_int_mul_pi (x : ℂ) (n : ℤ) : tan (x - n * π) = tan x :=
tan_periodic.sub_int_mul_eq n
lemma tan_nat_mul_pi_sub (x : ℂ) (n : ℕ) : tan (n * π - x) = -tan x :=
tan_neg x ▸ tan_periodic.nat_mul_sub_eq n
lemma tan_int_mul_pi_sub (x : ℂ) (n : ℤ) : tan (n * π - x) = -tan x :=
tan_neg x ▸ tan_periodic.int_mul_sub_eq n
lemma exp_antiperiodic : function.antiperiodic exp (π * I) :=
by simp [exp_add, exp_mul_I]
lemma exp_periodic : function.periodic exp (2 * π * I) :=
(mul_assoc (2:ℂ) π I).symm ▸ exp_antiperiodic.periodic
lemma exp_mul_I_antiperiodic : function.antiperiodic (λ x, exp (x * I)) π :=
by simpa only [mul_inv_cancel_right₀ I_ne_zero] using exp_antiperiodic.mul_const I_ne_zero
lemma exp_mul_I_periodic : function.periodic (λ x, exp (x * I)) (2 * π) :=
exp_mul_I_antiperiodic.periodic
@[simp] lemma exp_pi_mul_I : exp (π * I) = -1 :=
exp_zero ▸ exp_antiperiodic.eq
@[simp] lemma exp_two_pi_mul_I : exp (2 * π * I) = 1 :=
exp_periodic.eq.trans exp_zero
@[simp] lemma exp_nat_mul_two_pi_mul_I (n : ℕ) : exp (n * (2 * π * I)) = 1 :=
(exp_periodic.nat_mul_eq n).trans exp_zero
@[simp] lemma exp_int_mul_two_pi_mul_I (n : ℤ) : exp (n * (2 * π * I)) = 1 :=
(exp_periodic.int_mul_eq n).trans exp_zero
@[simp] lemma exp_add_pi_mul_I (z : ℂ) : exp (z + π * I) = -exp z :=
exp_antiperiodic z
@[simp] lemma exp_sub_pi_mul_I (z : ℂ) : exp (z - π * I) = -exp z :=
exp_antiperiodic.sub_eq z
end complex
|
52f4fac0ff40ff229d16f16044d0a0fb8bcd1cd0 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/sizeof.lean | c82a953cd7c828022c57c8424640f2f7013c71fd | [
"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 | 464 | lean | -- Recall that we do not generate code for `sizeOf` instances since they are only used for proving termination
#reduce sizeOf 10
#reduce sizeOf [1, 2]
#reduce sizeOf #[1, 2]
#reduce sizeOf (10 : UInt8)
#reduce sizeOf 'a'
#reduce sizeOf ['h', 'e', 'l', 'l', 'o']
#reduce sizeOf "abc"
#reduce sizeOf `abc
inductive InfTree (α : Type u)
| leaf (a : α)
| node (children : Nat → InfTree α)
#reduce sizeOf <| InfTree.leaf 10
#check InfTree.node.sizeOf_spec
|
2c5f3ba3e3e8c6d59bd5bfd20c36a48126aef0e2 | 0c1546a496eccfb56620165cad015f88d56190c5 | /library/init/meta/smt/default.lean | 8fd097dad7410ee3f1f61ab90392d64770ee358a | [
"Apache-2.0"
] | permissive | Solertis/lean | 491e0939957486f664498fbfb02546e042699958 | 84188c5aa1673fdf37a082b2de8562dddf53df3f | refs/heads/master | 1,610,174,257,606 | 1,486,263,620,000 | 1,486,263,620,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 313 | lean | /-
Copyright (c) 2017 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
prelude
import init.meta.smt.congruence_closure init.cc_lemmas
import init.meta.smt.ematch
import init.meta.smt.smt_tactic init.meta.smt.interactive
|
93f386530f9e6b292f2e226744716e6bdba84d78 | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/dynamics/fixed_points/basic.lean | 2bc976b3213f10382df793781cf018b69f1e11c0 | [
"Apache-2.0"
] | permissive | robertylewis/mathlib | 3d16e3e6daf5ddde182473e03a1b601d2810952c | 1d13f5b932f5e40a8308e3840f96fc882fae01f0 | refs/heads/master | 1,651,379,945,369 | 1,644,276,960,000 | 1,644,276,960,000 | 98,875,504 | 0 | 0 | Apache-2.0 | 1,644,253,514,000 | 1,501,495,700,000 | Lean | UTF-8 | Lean | false | false | 5,565 | lean | /-
Copyright (c) 2020 Yury Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury Kudryashov
-/
import data.set.function
import logic.function.iterate
/-!
# Fixed points of a self-map
In this file we define
* the predicate `is_fixed_pt f x := f x = x`;
* the set `fixed_points f` of fixed points of a self-map `f`.
We also prove some simple lemmas about `is_fixed_pt` and `∘`, `iterate`, and `semiconj`.
## Tags
fixed point
-/
universes u v
variables {α : Type u} {β : Type v} {f fa g : α → α} {x y : α} {fb : β → β} {m n k : ℕ}
namespace function
/-- A point `x` is a fixed point of `f : α → α` if `f x = x`. -/
def is_fixed_pt (f : α → α) (x : α) := f x = x
/-- Every point is a fixed point of `id`. -/
lemma is_fixed_pt_id (x : α) : is_fixed_pt id x := (rfl : _)
namespace is_fixed_pt
instance [h : decidable_eq α] {f : α → α} {x : α} : decidable (is_fixed_pt f x) :=
h (f x) x
/-- If `x` is a fixed point of `f`, then `f x = x`. This is useful, e.g., for `rw` or `simp`.-/
protected lemma eq (hf : is_fixed_pt f x) : f x = x := hf
/-- If `x` is a fixed point of `f` and `g`, then it is a fixed point of `f ∘ g`. -/
protected lemma comp (hf : is_fixed_pt f x) (hg : is_fixed_pt g x) : is_fixed_pt (f ∘ g) x :=
calc f (g x) = f x : congr_arg f hg
... = x : hf
/-- If `x` is a fixed point of `f`, then it is a fixed point of `f^[n]`. -/
protected lemma iterate (hf : is_fixed_pt f x) (n : ℕ) : is_fixed_pt (f^[n]) x :=
iterate_fixed hf n
/-- If `x` is a fixed point of `f ∘ g` and `g`, then it is a fixed point of `f`. -/
lemma left_of_comp (hfg : is_fixed_pt (f ∘ g) x) (hg : is_fixed_pt g x) : is_fixed_pt f x :=
calc f x = f (g x) : congr_arg f hg.symm
... = x : hfg
/-- If `x` is a fixed point of `f` and `g` is a left inverse of `f`, then `x` is a fixed
point of `g`. -/
lemma to_left_inverse (hf : is_fixed_pt f x) (h : left_inverse g f) : is_fixed_pt g x :=
calc g x = g (f x) : congr_arg g hf.symm
... = x : h x
/-- If `g` (semi)conjugates `fa` to `fb`, then it sends fixed points of `fa` to fixed points
of `fb`. -/
protected lemma map {x : α} (hx : is_fixed_pt fa x) {g : α → β} (h : semiconj g fa fb) :
is_fixed_pt fb (g x) :=
calc fb (g x) = g (fa x) : (h.eq x).symm
... = g x : congr_arg g hx
protected lemma apply {x : α} (hx : is_fixed_pt f x) : is_fixed_pt f (f x) :=
by convert hx
end is_fixed_pt
@[simp] lemma injective.is_fixed_pt_apply_iff (hf : injective f) {x : α} :
is_fixed_pt f (f x) ↔ is_fixed_pt f x :=
⟨λ h, hf h.eq, is_fixed_pt.apply⟩
/-- The set of fixed points of a map `f : α → α`. -/
def fixed_points (f : α → α) : set α := {x : α | is_fixed_pt f x}
instance fixed_points.decidable [decidable_eq α] (f : α → α) (x : α) :
decidable (x ∈ fixed_points f) :=
is_fixed_pt.decidable
@[simp] lemma mem_fixed_points : x ∈ fixed_points f ↔ is_fixed_pt f x := iff.rfl
lemma mem_fixed_points_iff {α : Type*} {f : α → α} {x : α} :
x ∈ fixed_points f ↔ f x = x :=
by refl
@[simp] lemma fixed_points_id : fixed_points (@id α) = set.univ :=
set.ext $ λ _, by simpa using is_fixed_pt_id _
/-- If `g` semiconjugates `fa` to `fb`, then it sends fixed points of `fa` to fixed points
of `fb`. -/
lemma semiconj.maps_to_fixed_pts {g : α → β} (h : semiconj g fa fb) :
set.maps_to g (fixed_points fa) (fixed_points fb) :=
λ x hx, hx.map h
/-- Any two maps `f : α → β` and `g : β → α` are inverse of each other on the sets of fixed points
of `f ∘ g` and `g ∘ f`, respectively. -/
lemma inv_on_fixed_pts_comp (f : α → β) (g : β → α) :
set.inv_on f g (fixed_points $ f ∘ g) (fixed_points $ g ∘ f) :=
⟨λ x, id, λ x, id⟩
/-- Any map `f` sends fixed points of `g ∘ f` to fixed points of `f ∘ g`. -/
lemma maps_to_fixed_pts_comp (f : α → β) (g : β → α) :
set.maps_to f (fixed_points $ g ∘ f) (fixed_points $ f ∘ g) :=
λ x hx, hx.map $ λ x, rfl
/-- Given two maps `f : α → β` and `g : β → α`, `g` is a bijective map between the fixed points
of `f ∘ g` and the fixed points of `g ∘ f`. The inverse map is `f`, see `inv_on_fixed_pts_comp`. -/
lemma bij_on_fixed_pts_comp (f : α → β) (g : β → α) :
set.bij_on g (fixed_points $ f ∘ g) (fixed_points $ g ∘ f) :=
(inv_on_fixed_pts_comp f g).bij_on (maps_to_fixed_pts_comp g f) (maps_to_fixed_pts_comp f g)
/-- If self-maps `f` and `g` commute, then they are inverse of each other on the set of fixed points
of `f ∘ g`. This is a particular case of `function.inv_on_fixed_pts_comp`. -/
lemma commute.inv_on_fixed_pts_comp (h : commute f g) :
set.inv_on f g (fixed_points $ f ∘ g) (fixed_points $ f ∘ g) :=
by simpa only [h.comp_eq] using inv_on_fixed_pts_comp f g
/-- If self-maps `f` and `g` commute, then `f` is bijective on the set of fixed points of `f ∘ g`.
This is a particular case of `function.bij_on_fixed_pts_comp`. -/
lemma commute.left_bij_on_fixed_pts_comp (h : commute f g) :
set.bij_on f (fixed_points $ f ∘ g) (fixed_points $ f ∘ g) :=
by simpa only [h.comp_eq] using bij_on_fixed_pts_comp g f
/-- If self-maps `f` and `g` commute, then `g` is bijective on the set of fixed points of `f ∘ g`.
This is a particular case of `function.bij_on_fixed_pts_comp`. -/
lemma commute.right_bij_on_fixed_pts_comp (h : commute f g) :
set.bij_on g (fixed_points $ f ∘ g) (fixed_points $ f ∘ g) :=
by simpa only [h.comp_eq] using bij_on_fixed_pts_comp f g
end function
|
9c238b94879884b8a160ab4b1dc19a2c3e82e4aa | f3a5af2927397cf346ec0e24312bfff077f00425 | /src/game/world7/level8.lean | 5c23fd6051c467ff8c08a5f31f28d3da6bc0cf21 | [
"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 | 1,294 | lean | import tactic.finish game.world7.level7 -- hide
namespace mynat -- hide
/-
# Advanced proposition world.
## Level 8: `and_or_distrib_left`
We know that `x(y+z)=xy+xz` for numbers, and this
is called distributivity of multiplication over addition.
The same is true for `∧` and `∨` -- in fact `∧` distributes
over `∨` and `∨` distributes over `∧`. Let's prove one of these.
-/
/- Lemma
If $P$. $Q$ and $R$ are true/false statements, then
$$P\land(Q\lor R)\iff(P\land Q)\lor (P\land R).$$
-/
lemma and_or_distrib_left (P Q R : Prop) : P ∧ (Q ∨ R) ↔ (P ∧ Q) ∨ (P ∧ R) :=
begin
split,
intro h,
cases h with hp hqr,
cases hqr with q r,
left, split, assumption, assumption,
right, split, assumption, assumption,
intro h,
cases h with hpq hpr,
cases hpq with p q,
split, assumption,
left, assumption,
cases hpr with hp hr,
split, assumption,
right, assumption,
end
/-
## Pro tip
Did you spot the import? What do you think it does?
If you follow the instructions at
<a href="https://github.com/leanprover-community/mathlib#installation" target="blank">the mathlib github page</a>
you will be able to install Lean and mathlib on your own system, and then you can create a new project
and experiment with such imports yourself.
-/
end mynat |
3b109f1e0417ac3cb940947221ca2982026e4462 | e953c38599905267210b87fb5d82dcc3e52a4214 | /hott/init/path.hlean | 385f376823665383156b0bd3eeecf840592e0654 | [
"Apache-2.0"
] | permissive | c-cube/lean | 563c1020bff98441c4f8ba60111fef6f6b46e31b | 0fb52a9a139f720be418dafac35104468e293b66 | refs/heads/master | 1,610,753,294,113 | 1,440,451,356,000 | 1,440,499,588,000 | 41,748,334 | 0 | 0 | null | 1,441,122,656,000 | 1,441,122,656,000 | null | UTF-8 | Lean | false | false | 25,378 | hlean | /-
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad, Jakob von Raumer, Floris van Doorn
Ported from Coq HoTT
-/
prelude
import .function .tactic
open function eq
/- Path equality -/
namespace eq
variables {A B C : Type} {P : A → Type} {x y z t : A}
--notation a = b := eq a b
notation x = y `:>`:50 A:49 := @eq A x y
definition idp [reducible] [constructor] {a : A} := refl a
definition idpath [reducible] [constructor] (a : A) := refl a
-- unbased path induction
definition rec' [reducible] [unfold 6] {P : Π (a b : A), (a = b) → Type}
(H : Π (a : A), P a a idp) {a b : A} (p : a = b) : P a b p :=
eq.rec (H a) p
definition rec_on' [reducible] [unfold 5] {P : Π (a b : A), (a = b) → Type}
{a b : A} (p : a = b) (H : Π (a : A), P a a idp) : P a b p :=
eq.rec (H a) p
/- Concatenation and inverse -/
definition concat [trans] [unfold 6] (p : x = y) (q : y = z) : x = z :=
eq.rec (λp', p') q p
definition inverse [symm] [unfold 4] (p : x = y) : y = x :=
eq.rec (refl x) p
infix ⬝ := concat
postfix ⁻¹ := inverse
--a second notation for the inverse, which is not overloaded
postfix [parsing-only] `⁻¹ᵖ`:std.prec.max_plus := inverse
/- The 1-dimensional groupoid structure -/
-- The identity path is a right unit.
definition con_idp [unfold-full] (p : x = y) : p ⬝ idp = p :=
idp
-- The identity path is a right unit.
definition idp_con [unfold 4] (p : x = y) : idp ⬝ p = p :=
eq.rec_on p idp
-- Concatenation is associative.
definition con.assoc' (p : x = y) (q : y = z) (r : z = t) :
p ⬝ (q ⬝ r) = (p ⬝ q) ⬝ r :=
eq.rec_on r (eq.rec_on q idp)
definition con.assoc (p : x = y) (q : y = z) (r : z = t) :
(p ⬝ q) ⬝ r = p ⬝ (q ⬝ r) :=
eq.rec_on r (eq.rec_on q idp)
-- The left inverse law.
definition con.right_inv [unfold 4] (p : x = y) : p ⬝ p⁻¹ = idp :=
eq.rec_on p idp
-- The right inverse law.
definition con.left_inv [unfold 4] (p : x = y) : p⁻¹ ⬝ p = idp :=
eq.rec_on p idp
/- Several auxiliary theorems about canceling inverses across associativity. These are somewhat
redundant, following from earlier theorems. -/
definition inv_con_cancel_left (p : x = y) (q : y = z) : p⁻¹ ⬝ (p ⬝ q) = q :=
eq.rec_on q (eq.rec_on p idp)
definition con_inv_cancel_left (p : x = y) (q : x = z) : p ⬝ (p⁻¹ ⬝ q) = q :=
eq.rec_on q (eq.rec_on p idp)
definition con_inv_cancel_right (p : x = y) (q : y = z) : (p ⬝ q) ⬝ q⁻¹ = p :=
eq.rec_on q (eq.rec_on p idp)
definition inv_con_cancel_right (p : x = z) (q : y = z) : (p ⬝ q⁻¹) ⬝ q = p :=
eq.rec_on q (take p, eq.rec_on p idp) p
-- Inverse distributes over concatenation
definition con_inv (p : x = y) (q : y = z) : (p ⬝ q)⁻¹ = q⁻¹ ⬝ p⁻¹ :=
eq.rec_on q (eq.rec_on p idp)
definition inv_con_inv_left (p : y = x) (q : y = z) : (p⁻¹ ⬝ q)⁻¹ = q⁻¹ ⬝ p :=
eq.rec_on q (eq.rec_on p idp)
-- universe metavariables
definition inv_con_inv_right (p : x = y) (q : z = y) : (p ⬝ q⁻¹)⁻¹ = q ⬝ p⁻¹ :=
eq.rec_on p (take q, eq.rec_on q idp) q
definition inv_con_inv_inv (p : y = x) (q : z = y) : (p⁻¹ ⬝ q⁻¹)⁻¹ = q ⬝ p :=
eq.rec_on p (eq.rec_on q idp)
-- Inverse is an involution.
definition inv_inv (p : x = y) : p⁻¹⁻¹ = p :=
eq.rec_on p idp
-- auxiliary definition used by 'cases' tactic
definition elim_inv_inv {A : Type} {a b : A} {C : a = b → Type} (H₁ : a = b) (H₂ : C (H₁⁻¹⁻¹)) : C H₁ :=
eq.rec_on (inv_inv H₁) H₂
/- Theorems for moving things around in equations -/
definition con_eq_of_eq_inv_con {p : x = z} {q : y = z} {r : y = x} :
p = r⁻¹ ⬝ q → r ⬝ p = q :=
eq.rec_on r (take p h, !idp_con ⬝ h ⬝ !idp_con) p
definition con_eq_of_eq_con_inv [unfold 5] {p : x = z} {q : y = z} {r : y = x} :
r = q ⬝ p⁻¹ → r ⬝ p = q :=
eq.rec_on p (take q h, h) q
definition inv_con_eq_of_eq_con {p : x = z} {q : y = z} {r : x = y} :
p = r ⬝ q → r⁻¹ ⬝ p = q :=
eq.rec_on r (take q h, !idp_con ⬝ h ⬝ !idp_con) q
definition con_inv_eq_of_eq_con [unfold 5] {p : z = x} {q : y = z} {r : y = x} :
r = q ⬝ p → r ⬝ p⁻¹ = q :=
eq.rec_on p (take r h, h) r
definition eq_con_of_inv_con_eq {p : x = z} {q : y = z} {r : y = x} :
r⁻¹ ⬝ q = p → q = r ⬝ p :=
eq.rec_on r (take p h, !idp_con⁻¹ ⬝ h ⬝ !idp_con⁻¹) p
definition eq_con_of_con_inv_eq [unfold 5] {p : x = z} {q : y = z} {r : y = x} :
q ⬝ p⁻¹ = r → q = r ⬝ p :=
eq.rec_on p (take q h, h) q
definition eq_inv_con_of_con_eq {p : x = z} {q : y = z} {r : x = y} :
r ⬝ q = p → q = r⁻¹ ⬝ p :=
eq.rec_on r (take q h, !idp_con⁻¹ ⬝ h ⬝ !idp_con⁻¹) q
definition eq_con_inv_of_con_eq [unfold 5] {p : z = x} {q : y = z} {r : y = x} :
q ⬝ p = r → q = r ⬝ p⁻¹ :=
eq.rec_on p (take r h, h) r
definition eq_of_con_inv_eq_idp [unfold 5] {p q : x = y} : p ⬝ q⁻¹ = idp → p = q :=
eq.rec_on q (take p h, h) p
definition eq_of_inv_con_eq_idp {p q : x = y} : q⁻¹ ⬝ p = idp → p = q :=
eq.rec_on q (take p h, !idp_con⁻¹ ⬝ h) p
definition eq_inv_of_con_eq_idp' [unfold 5] {p : x = y} {q : y = x} : p ⬝ q = idp → p = q⁻¹ :=
eq.rec_on q (take p h, h) p
definition eq_inv_of_con_eq_idp {p : x = y} {q : y = x} : q ⬝ p = idp → p = q⁻¹ :=
eq.rec_on q (take p h, !idp_con⁻¹ ⬝ h) p
definition eq_of_idp_eq_inv_con {p q : x = y} : idp = p⁻¹ ⬝ q → p = q :=
eq.rec_on p (take q h, h ⬝ !idp_con) q
definition eq_of_idp_eq_con_inv [unfold 4] {p q : x = y} : idp = q ⬝ p⁻¹ → p = q :=
eq.rec_on p (take q h, h) q
definition inv_eq_of_idp_eq_con [unfold 4] {p : x = y} {q : y = x} : idp = q ⬝ p → p⁻¹ = q :=
eq.rec_on p (take q h, h) q
definition inv_eq_of_idp_eq_con' {p : x = y} {q : y = x} : idp = p ⬝ q → p⁻¹ = q :=
eq.rec_on p (take q h, h ⬝ !idp_con) q
definition con_inv_eq_idp [unfold 6] {p q : x = y} (r : p = q) : p ⬝ q⁻¹ = idp :=
by cases r;apply con.right_inv
definition inv_con_eq_idp [unfold 6] {p q : x = y} (r : p = q) : q⁻¹ ⬝ p = idp :=
by cases r;apply con.left_inv
definition con_eq_idp {p : x = y} {q : y = x} (r : p = q⁻¹) : p ⬝ q = idp :=
by cases q;exact r
definition idp_eq_inv_con {p q : x = y} (r : p = q) : idp = p⁻¹ ⬝ q :=
by cases r;exact !con.left_inv⁻¹
definition idp_eq_con_inv {p q : x = y} (r : p = q) : idp = q ⬝ p⁻¹ :=
by cases r;exact !con.right_inv⁻¹
definition idp_eq_con {p : x = y} {q : y = x} (r : p⁻¹ = q) : idp = q ⬝ p :=
by cases p;exact r
/- Transport -/
definition transport [subst] [reducible] [unfold 5] (P : A → Type) {x y : A} (p : x = y)
(u : P x) : P y :=
eq.rec_on p u
-- This idiom makes the operation right associative.
infixr `▸` := transport _
definition cast [reducible] [unfold 3] {A B : Type} (p : A = B) (a : A) : B :=
p ▸ a
definition tr_rev [reducible] [unfold 6] (P : A → Type) {x y : A} (p : x = y) (u : P y) : P x :=
p⁻¹ ▸ u
definition ap [unfold 6] ⦃A B : Type⦄ (f : A → B) {x y:A} (p : x = y) : f x = f y :=
eq.rec_on p idp
abbreviation ap01 [parsing-only] := ap
definition homotopy [reducible] (f g : Πx, P x) : Type :=
Πx : A, f x = g x
infix ~ := homotopy
protected definition homotopy.refl [refl] [reducible] (f : Πx, P x) : f ~ f :=
λ x, idp
protected definition homotopy.symm [symm] [reducible] {f g : Πx, P x} (H : f ~ g) : g ~ f :=
λ x, (H x)⁻¹
protected definition homotopy.trans [trans] [reducible] {f g h : Πx, P x}
(H1 : f ~ g) (H2 : g ~ h) : f ~ h :=
λ x, H1 x ⬝ H2 x
definition apd10 [unfold 5] {f g : Πx, P x} (H : f = g) : f ~ g :=
λx, eq.rec_on H idp
--the next theorem is useful if you want to write "apply (apd10' a)"
definition apd10' [unfold 6] {f g : Πx, P x} (a : A) (H : f = g) : f a = g a :=
eq.rec_on H idp
definition ap10 [reducible] [unfold 5] {f g : A → B} (H : f = g) : f ~ g := apd10 H
definition ap11 {f g : A → B} (H : f = g) {x y : A} (p : x = y) : f x = g y :=
eq.rec_on H (eq.rec_on p idp)
definition apd [unfold 6] (f : Πa, P a) {x y : A} (p : x = y) : p ▸ f x = f y :=
eq.rec_on p idp
/- More theorems for moving things around in equations -/
definition tr_eq_of_eq_inv_tr {P : A → Type} {x y : A} {p : x = y} {u : P x} {v : P y} :
u = p⁻¹ ▸ v → p ▸ u = v :=
eq.rec_on p (take v, id) v
definition inv_tr_eq_of_eq_tr {P : A → Type} {x y : A} {p : y = x} {u : P x} {v : P y} :
u = p ▸ v → p⁻¹ ▸ u = v :=
eq.rec_on p (take u, id) u
definition eq_inv_tr_of_tr_eq {P : A → Type} {x y : A} {p : x = y} {u : P x} {v : P y} :
p ▸ u = v → u = p⁻¹ ▸ v :=
eq.rec_on p (take v, id) v
definition eq_tr_of_inv_tr_eq {P : A → Type} {x y : A} {p : y = x} {u : P x} {v : P y} :
p⁻¹ ▸ u = v → u = p ▸ v :=
eq.rec_on p (take u, id) u
/- Functoriality of functions -/
-- Here we prove that functions behave like functors between groupoids, and that [ap] itself is
-- functorial.
-- Functions take identity paths to identity paths
definition ap_idp (x : A) (f : A → B) : ap f idp = idp :> (f x = f x) := idp
-- Functions commute with concatenation.
definition ap_con (f : A → B) {x y z : A} (p : x = y) (q : y = z) :
ap f (p ⬝ q) = ap f p ⬝ ap f q :=
eq.rec_on q (eq.rec_on p idp)
definition con_ap_con_eq_con_ap_con_ap (f : A → B) {w x y z : A} (r : f w = f x)
(p : x = y) (q : y = z) : r ⬝ ap f (p ⬝ q) = (r ⬝ ap f p) ⬝ ap f q :=
eq.rec_on q (take p, eq.rec_on p idp) p
definition ap_con_con_eq_ap_con_ap_con (f : A → B) {w x y z : A} (p : x = y) (q : y = z)
(r : f z = f w) : ap f (p ⬝ q) ⬝ r = ap f p ⬝ (ap f q ⬝ r) :=
eq.rec_on q (eq.rec_on p (take r, con.assoc _ _ _)) r
-- Functions commute with path inverses.
definition ap_inv' (f : A → B) {x y : A} (p : x = y) : (ap f p)⁻¹ = ap f p⁻¹ :=
eq.rec_on p idp
definition ap_inv {A B : Type} (f : A → B) {x y : A} (p : x = y) : ap f p⁻¹ = (ap f p)⁻¹ :=
eq.rec_on p idp
-- [ap] itself is functorial in the first argument.
definition ap_id (p : x = y) : ap id p = p :=
eq.rec_on p idp
definition ap_compose (g : B → C) (f : A → B) {x y : A} (p : x = y) :
ap (g ∘ f) p = ap g (ap f p) :=
eq.rec_on p idp
-- Sometimes we don't have the actual function [compose].
definition ap_compose' (g : B → C) (f : A → B) {x y : A} (p : x = y) :
ap (λa, g (f a)) p = ap g (ap f p) :=
eq.rec_on p idp
-- The action of constant maps.
definition ap_constant [unfold 5] (p : x = y) (z : B) : ap (λu, z) p = idp :=
eq.rec_on p idp
-- Naturality of [ap].
definition ap_con_eq_con_ap {f g : A → B} (p : f ~ g) {x y : A} (q : x = y) :
ap f q ⬝ p y = p x ⬝ ap g q :=
eq.rec_on q !idp_con
-- Naturality of [ap] at identity.
definition ap_con_eq_con {f : A → A} (p : Πx, f x = x) {x y : A} (q : x = y) :
ap f q ⬝ p y = p x ⬝ q :=
eq.rec_on q !idp_con
definition con_ap_eq_con {f : A → A} (p : Πx, x = f x) {x y : A} (q : x = y) :
p x ⬝ ap f q = q ⬝ p y :=
eq.rec_on q !idp_con⁻¹
-- Naturality of [ap] with constant function
definition ap_con_eq {f : A → B} {b : B} (p : Πx, f x = b) {x y : A} (q : x = y) :
ap f q ⬝ p y = p x :=
eq.rec_on q !idp_con
-- Naturality with other paths hanging around.
definition con_ap_con_con_eq_con_con_ap_con {f g : A → B} (p : f ~ g) {x y : A} (q : x = y)
{w z : B} (r : w = f x) (s : g y = z) :
(r ⬝ ap f q) ⬝ (p y ⬝ s) = (r ⬝ p x) ⬝ (ap g q ⬝ s) :=
eq.rec_on s (eq.rec_on q idp)
definition con_ap_con_eq_con_con_ap {f g : A → B} (p : f ~ g) {x y : A} (q : x = y)
{w : B} (r : w = f x) :
(r ⬝ ap f q) ⬝ p y = (r ⬝ p x) ⬝ ap g q :=
eq.rec_on q idp
-- TODO: try this using the simplifier, and compare proofs
definition ap_con_con_eq_con_ap_con {f g : A → B} (p : f ~ g) {x y : A} (q : x = y)
{z : B} (s : g y = z) :
ap f q ⬝ (p y ⬝ s) = p x ⬝ (ap g q ⬝ s) :=
eq.rec_on s (eq.rec_on q
(calc
(ap f idp) ⬝ (p x ⬝ idp) = idp ⬝ p x : idp
... = p x : !idp_con
... = (p x) ⬝ (ap g idp ⬝ idp) : idp))
-- This also works:
-- eq.rec_on s (eq.rec_on q (!idp_con ▸ idp))
definition con_ap_con_con_eq_con_con_con {f : A → A} (p : f ~ id) {x y : A} (q : x = y)
{w z : A} (r : w = f x) (s : y = z) :
(r ⬝ ap f q) ⬝ (p y ⬝ s) = (r ⬝ p x) ⬝ (q ⬝ s) :=
eq.rec_on s (eq.rec_on q idp)
definition con_con_ap_con_eq_con_con_con {g : A → A} (p : id ~ g) {x y : A} (q : x = y)
{w z : A} (r : w = x) (s : g y = z) :
(r ⬝ p x) ⬝ (ap g q ⬝ s) = (r ⬝ q) ⬝ (p y ⬝ s) :=
eq.rec_on s (eq.rec_on q idp)
definition con_ap_con_eq_con_con {f : A → A} (p : f ~ id) {x y : A} (q : x = y)
{w : A} (r : w = f x) :
(r ⬝ ap f q) ⬝ p y = (r ⬝ p x) ⬝ q :=
eq.rec_on q idp
definition ap_con_con_eq_con_con {f : A → A} (p : f ~ id) {x y : A} (q : x = y)
{z : A} (s : y = z) :
ap f q ⬝ (p y ⬝ s) = p x ⬝ (q ⬝ s) :=
eq.rec_on s (eq.rec_on q (!idp_con ▸ idp))
definition con_con_ap_eq_con_con {g : A → A} (p : id ~ g) {x y : A} (q : x = y)
{w : A} (r : w = x) :
(r ⬝ p x) ⬝ ap g q = (r ⬝ q) ⬝ p y :=
begin cases q, exact idp end
definition con_ap_con_eq_con_con' {g : A → A} (p : id ~ g) {x y : A} (q : x = y)
{z : A} (s : g y = z) :
p x ⬝ (ap g q ⬝ s) = q ⬝ (p y ⬝ s) :=
begin
apply (eq.rec_on s),
apply (eq.rec_on q),
apply (idp_con (p x) ▸ idp)
end
/- Action of [apd10] and [ap10] on paths -/
-- Application of paths between functions preserves the groupoid structure
definition apd10_idp (f : Πx, P x) (x : A) : apd10 (refl f) x = idp := idp
definition apd10_con {f f' f'' : Πx, P x} (h : f = f') (h' : f' = f'') (x : A) :
apd10 (h ⬝ h') x = apd10 h x ⬝ apd10 h' x :=
eq.rec_on h (take h', eq.rec_on h' idp) h'
definition apd10_inv {f g : Πx : A, P x} (h : f = g) (x : A) :
apd10 h⁻¹ x = (apd10 h x)⁻¹ :=
eq.rec_on h idp
definition ap10_idp {f : A → B} (x : A) : ap10 (refl f) x = idp := idp
definition ap10_con {f f' f'' : A → B} (h : f = f') (h' : f' = f'') (x : A) :
ap10 (h ⬝ h') x = ap10 h x ⬝ ap10 h' x := apd10_con h h' x
definition ap10_inv {f g : A → B} (h : f = g) (x : A) : ap10 h⁻¹ x = (ap10 h x)⁻¹ :=
apd10_inv h x
-- [ap10] also behaves nicely on paths produced by [ap]
definition ap_ap10 (f g : A → B) (h : B → C) (p : f = g) (a : A) :
ap h (ap10 p a) = ap10 (ap (λ f', h ∘ f') p) a:=
eq.rec_on p idp
/- Transport and the groupoid structure of paths -/
definition idp_tr {P : A → Type} {x : A} (u : P x) : idp ▸ u = u := idp
definition con_tr [unfold 7] {P : A → Type} {x y z : A} (p : x = y) (q : y = z) (u : P x) :
p ⬝ q ▸ u = q ▸ p ▸ u :=
eq.rec_on q idp
definition tr_inv_tr {P : A → Type} {x y : A} (p : x = y) (z : P y) :
p ▸ p⁻¹ ▸ z = z :=
(con_tr p⁻¹ p z)⁻¹ ⬝ ap (λr, transport P r z) (con.left_inv p)
definition inv_tr_tr {P : A → Type} {x y : A} (p : x = y) (z : P x) :
p⁻¹ ▸ p ▸ z = z :=
(con_tr p p⁻¹ z)⁻¹ ⬝ ap (λr, transport P r z) (con.right_inv p)
definition con_tr_lemma {P : A → Type}
{x y z w : A} (p : x = y) (q : y = z) (r : z = w) (u : P x) :
ap (λe, e ▸ u) (con.assoc' p q r) ⬝ (con_tr (p ⬝ q) r u) ⬝
ap (transport P r) (con_tr p q u)
= (con_tr p (q ⬝ r) u) ⬝ (con_tr q r (p ▸ u))
:> ((p ⬝ (q ⬝ r)) ▸ u = r ▸ q ▸ p ▸ u) :=
eq.rec_on r (eq.rec_on q (eq.rec_on p idp))
-- Here is another coherence lemma for transport.
definition tr_inv_tr_lemma {P : A → Type} {x y : A} (p : x = y) (z : P x) :
tr_inv_tr p (transport P p z) = ap (transport P p) (inv_tr_tr p z) :=
eq.rec_on p idp
/- some properties for apd -/
definition apd_idp (x : A) (f : Πx, P x) : apd f idp = idp :> (f x = f x) := idp
definition apd_con (f : Πx, P x) {x y z : A} (p : x = y) (q : y = z)
: apd f (p ⬝ q) = con_tr p q (f x) ⬝ ap (transport P q) (apd f p) ⬝ apd f q :=
by cases p;cases q;apply idp
definition apd_inv (f : Πx, P x) {x y : A} (p : x = y)
: apd f p⁻¹ = (eq_inv_tr_of_tr_eq (apd f p))⁻¹ :=
by cases p;apply idp
-- Dependent transport in a doubly dependent type.
definition transportD [unfold 6] {P : A → Type} (Q : Πa, P a → Type)
{a a' : A} (p : a = a') (b : P a) (z : Q a b) : Q a' (p ▸ b) :=
eq.rec_on p z
-- In Coq the variables P, Q and b are explicit, but in Lean we can probably have them implicit
-- using the following notation
notation p `▸D`:65 x:64 := transportD _ p _ x
-- Transporting along higher-dimensional paths
definition transport2 [unfold 7] (P : A → Type) {x y : A} {p q : x = y} (r : p = q) (z : P x) :
p ▸ z = q ▸ z :=
ap (λp', p' ▸ z) r
notation p `▸2`:65 x:64 := transport2 _ p _ x
-- An alternative definition.
definition tr2_eq_ap10 (Q : A → Type) {x y : A} {p q : x = y} (r : p = q)
(z : Q x) :
transport2 Q r z = ap10 (ap (transport Q) r) z :=
eq.rec_on r idp
definition tr2_con {P : A → Type} {x y : A} {p1 p2 p3 : x = y}
(r1 : p1 = p2) (r2 : p2 = p3) (z : P x) :
transport2 P (r1 ⬝ r2) z = transport2 P r1 z ⬝ transport2 P r2 z :=
eq.rec_on r1 (eq.rec_on r2 idp)
definition tr2_inv (Q : A → Type) {x y : A} {p q : x = y} (r : p = q) (z : Q x) :
transport2 Q r⁻¹ z = (transport2 Q r z)⁻¹ :=
eq.rec_on r idp
definition transportD2 [unfold 7] (B C : A → Type) (D : Π(a:A), B a → C a → Type)
{x1 x2 : A} (p : x1 = x2) (y : B x1) (z : C x1) (w : D x1 y z) : D x2 (p ▸ y) (p ▸ z) :=
eq.rec_on p w
notation p `▸D2`:65 x:64 := transportD2 _ _ _ p _ _ x
definition ap_tr_con_tr2 (P : A → Type) {x y : A} {p q : x = y} {z w : P x} (r : p = q)
(s : z = w) :
ap (transport P p) s ⬝ transport2 P r w = transport2 P r z ⬝ ap (transport P q) s :=
eq.rec_on r !idp_con⁻¹
definition fn_tr_eq_tr_fn {P Q : A → Type} {x y : A} (p : x = y) (f : Πx, P x → Q x) (z : P x) :
f y (p ▸ z) = (p ▸ (f x z)) :=
eq.rec_on p idp
/- Transporting in particular fibrations -/
/-
From the Coq HoTT library:
One frequently needs lemmas showing that transport in a certain dependent type is equal to some
more explicitly defined operation, defined according to the structure of that dependent type.
For most dependent types, we prove these lemmas in the appropriate file in the types/
subdirectory. Here we consider only the most basic cases.
-/
-- Transporting in a constant fibration.
definition tr_constant (p : x = y) (z : B) : transport (λx, B) p z = z :=
eq.rec_on p idp
definition tr2_constant {p q : x = y} (r : p = q) (z : B) :
tr_constant p z = transport2 (λu, B) r z ⬝ tr_constant q z :=
eq.rec_on r !idp_con⁻¹
-- Transporting in a pulled back fibration.
definition tr_compose (P : B → Type) (f : A → B) (p : x = y) (z : P (f x)) :
transport (P ∘ f) p z = transport P (ap f p) z :=
eq.rec_on p idp
definition ap_precompose (f : A → B) (g g' : B → C) (p : g = g') :
ap (λh, h ∘ f) p = transport (λh : B → C, g ∘ f = h ∘ f) p idp :=
eq.rec_on p idp
definition apd10_ap_precompose (f : A → B) (g g' : B → C) (p : g = g') (a : A) :
apd10 (ap (λh : B → C, h ∘ f) p) a = apd10 p (f a) :=
eq.rec_on p idp
definition apd10_ap_postcompose (f : B → C) (g g' : A → B) (p : g = g') (a : A) :
apd10 (ap (λh : A → B, f ∘ h) p) a = ap f (apd10 p a) :=
eq.rec_on p idp
-- A special case of [tr_compose] which seems to come up a lot.
definition tr_eq_cast_ap {P : A → Type} {x y} (p : x = y) (u : P x) : p ▸ u = cast (ap P p) u :=
eq.rec_on p idp
definition tr_eq_cast_ap_fn {P : A → Type} {x y} (p : x = y) : transport P p = cast (ap P p) :=
eq.rec_on p idp
/- The behavior of [ap] and [apd] -/
-- In a constant fibration, [apd] reduces to [ap], modulo [transport_const].
definition apd_eq_tr_constant_con_ap (f : A → B) (p : x = y) :
apd f p = tr_constant p (f x) ⬝ ap f p :=
eq.rec_on p idp
/- The 2-dimensional groupoid structure -/
-- Horizontal composition of 2-dimensional paths.
definition concat2 [unfold 9 10] {p p' : x = y} {q q' : y = z} (h : p = p') (h' : q = q')
: p ⬝ q = p' ⬝ q' :=
eq.rec_on h (eq.rec_on h' idp)
-- 2-dimensional path inversion
definition inverse2 [unfold 6] {p q : x = y} (h : p = q) : p⁻¹ = q⁻¹ :=
eq.rec_on h idp
infixl `◾`:75 := concat2
postfix [parsing-only] `⁻²`:(max+10) := inverse2 --this notation is abusive, should we use it?
/- Whiskering -/
definition whisker_left [unfold 8] (p : x = y) {q r : y = z} (h : q = r) : p ⬝ q = p ⬝ r :=
idp ◾ h
definition whisker_right [unfold 7] {p q : x = y} (h : p = q) (r : y = z) : p ⬝ r = q ⬝ r :=
h ◾ idp
-- Unwhiskering, a.k.a. cancelling
definition cancel_left {x y z : A} {p : x = y} {q r : y = z} : (p ⬝ q = p ⬝ r) → (q = r) :=
λs, !inv_con_cancel_left⁻¹ ⬝ whisker_left p⁻¹ s ⬝ !inv_con_cancel_left
definition cancel_right {x y z : A} {p q : x = y} {r : y = z} : (p ⬝ r = q ⬝ r) → (p = q) :=
λs, !con_inv_cancel_right⁻¹ ⬝ whisker_right s r⁻¹ ⬝ !con_inv_cancel_right
-- Whiskering and identity paths.
definition whisker_right_idp {p q : x = y} (h : p = q) :
whisker_right h idp = h :=
eq.rec_on h (eq.rec_on p idp)
definition whisker_right_idp_left (p : x = y) (q : y = z) :
whisker_right idp q = idp :> (p ⬝ q = p ⬝ q) :=
idp
definition whisker_left_idp_right (p : x = y) (q : y = z) :
whisker_left p idp = idp :> (p ⬝ q = p ⬝ q) :=
idp
definition whisker_left_idp {p q : x = y} (h : p = q) :
(idp_con p) ⁻¹ ⬝ whisker_left idp h ⬝ idp_con q = h :=
eq.rec_on h (eq.rec_on p idp)
definition con2_idp {p q : x = y} (h : p = q) :
h ◾ idp = whisker_right h idp :> (p ⬝ idp = q ⬝ idp) :=
idp
definition idp_con2 {p q : x = y} (h : p = q) :
idp ◾ h = whisker_left idp h :> (idp ⬝ p = idp ⬝ q) :=
idp
-- The interchange law for concatenation.
definition con2_con_con2 {p p' p'' : x = y} {q q' q'' : y = z}
(a : p = p') (b : p' = p'') (c : q = q') (d : q' = q'') :
(a ◾ c) ⬝ (b ◾ d) = (a ⬝ b) ◾ (c ⬝ d) :=
eq.rec_on d (eq.rec_on c (eq.rec_on b (eq.rec_on a idp)))
definition whisker_right_con_whisker_left {x y z : A} {p p' : x = y} {q q' : y = z}
(a : p = p') (b : q = q') :
(whisker_right a q) ⬝ (whisker_left p' b) = (whisker_left p b) ⬝ (whisker_right a q') :=
eq.rec_on b (eq.rec_on a !idp_con⁻¹)
-- Structure corresponding to the coherence equations of a bicategory.
-- The "pentagonator": the 3-cell witnessing the associativity pentagon.
definition pentagon {v w x y z : A} (p : v = w) (q : w = x) (r : x = y) (s : y = z) :
whisker_left p (con.assoc' q r s)
⬝ con.assoc' p (q ⬝ r) s
⬝ whisker_right (con.assoc' p q r) s
= con.assoc' p q (r ⬝ s) ⬝ con.assoc' (p ⬝ q) r s :=
by induction s;induction r;induction q;induction p;reflexivity
-- The 3-cell witnessing the left unit triangle.
definition triangulator (p : x = y) (q : y = z) :
con.assoc' p idp q ⬝ whisker_right (con_idp p) q = whisker_left p (idp_con q) :=
eq.rec_on q (eq.rec_on p idp)
definition eckmann_hilton {x:A} (p q : idp = idp :> x = x) : p ⬝ q = q ⬝ p :=
(!whisker_right_idp ◾ !whisker_left_idp)⁻¹
⬝ whisker_left _ !idp_con
⬝ !whisker_right_con_whisker_left
⬝ whisker_right !idp_con⁻¹ _
⬝ (!whisker_left_idp ◾ !whisker_right_idp)
-- The action of functions on 2-dimensional paths
definition ap02 [unfold 8] [reducible] (f : A → B) {x y : A} {p q : x = y} (r : p = q)
: ap f p = ap f q :=
ap (ap f) r
definition ap02_con (f : A → B) {x y : A} {p p' p'' : x = y} (r : p = p') (r' : p' = p'') :
ap02 f (r ⬝ r') = ap02 f r ⬝ ap02 f r' :=
eq.rec_on r (eq.rec_on r' idp)
definition ap02_con2 (f : A → B) {x y z : A} {p p' : x = y} {q q' :y = z} (r : p = p')
(s : q = q') :
ap02 f (r ◾ s) = ap_con f p q
⬝ (ap02 f r ◾ ap02 f s)
⬝ (ap_con f p' q')⁻¹ :=
eq.rec_on r (eq.rec_on s (eq.rec_on q (eq.rec_on p idp)))
definition apd02 [unfold 8] {p q : x = y} (f : Π x, P x) (r : p = q) :
apd f p = transport2 P r (f x) ⬝ apd f q :=
eq.rec_on r !idp_con⁻¹
-- And now for a lemma whose statement is much longer than its proof.
definition apd02_con {P : A → Type} (f : Π x:A, P x) {x y : A}
{p1 p2 p3 : x = y} (r1 : p1 = p2) (r2 : p2 = p3) :
apd02 f (r1 ⬝ r2) = apd02 f r1
⬝ whisker_left (transport2 P r1 (f x)) (apd02 f r2)
⬝ con.assoc' _ _ _
⬝ (whisker_right (tr2_con r1 r2 (f x))⁻¹ (apd f p3)) :=
eq.rec_on r2 (eq.rec_on r1 (eq.rec_on p1 idp))
end eq
|
5c578fa80613a090085c42c6a3ba2bf70f3ac43c | 1d265c7dd8cb3d0e1d645a19fd6157a2084c3921 | /src/other/int_vector3d_cross.lean | 50d7544c1bebd976f3839a3d9996489aac049da5 | [
"MIT"
] | permissive | hanzhi713/lean-proofs | de432372f220d302be09b5ca4227f8986567e4fd | 4d8356a878645b9ba7cb036f87737f3f1e68ede5 | refs/heads/master | 1,585,580,245,658 | 1,553,646,623,000 | 1,553,646,623,000 | 151,342,188 | 0 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 1,950 | lean | -- this is a proof that ∀ vectors a, b ∈ ℤ³,
-- (a × b) ⬝ a = 0 ∧ (a × b) ⬝ b = 0
def dot: ℤ × ℤ × ℤ → ℤ × ℤ × ℤ → ℤ := λ ⟨a, b, c⟩ ⟨d, e, f⟩, a*d + b*e + c*f
def cross: ℤ × ℤ × ℤ → ℤ × ℤ × ℤ → ℤ × ℤ × ℤ := λ ⟨a, b, c⟩ ⟨d, e, f⟩, ⟨b*f - c*e, c*d - a*f, a*e - b*d⟩
variables m n p j k l : ℤ
#eval cross (124, 1294, 123) (156, 949, 29238499)
#reduce dot (cross (8, 1, 5) (2, 8, 9)) (2, 8, 9)
#reduce dot (cross (m, n, p) (j, k, l)) (j, k, l)
theorem cross' (a b c d e f: ℤ) : cross (a, b, c) (d, e, f) = (b*f - c*e, c*d - a*f, a*e - b*d) := rfl
theorem dot' (a b c d e f: ℤ) : dot (a, b, c) (d, e, f) = a*d + b*e + c*f := rfl
local attribute [simp] mul_comm mul_assoc mul_right_comm
theorem proof1 : ∀ {a b c d e f: ℤ}, dot (cross (a, b, c) (d, e, f)) (a, b, c) = 0 :=
assume a b c d e f,
calc
dot (cross (a, b, c) (d, e, f)) (a, b, c) = dot (b*f - c*e, c*d - a*f, a*e - b*d) (a, b, c) : by rw cross'
... = (b*f - c*e)*a + (c*d - a*f)*b + (a*e - b*d)*c : by rw dot'
... = b*f*a - c*e*a + (c*d*b - a*f*b) + (a*e*c - b*d*c) : by repeat {rw sub_mul}
... = 0 : by simp
#check proof1
theorem proof2 : ∀ {a b c d e f: ℤ}, dot (cross (a, b, c) (d, e, f)) (d, e, f) = 0 :=
assume a b c d e f,
calc
dot (cross (a, b, c) (d, e, f)) (d, e, f) = dot (b*f - c*e, c*d - a*f, a*e - b*d) (d, e, f) : by rw cross'
... = (b*f - c*e)*d + (c*d - a*f)*e + (a*e - b*d)*f : by rw dot'
... = b*f*d - c*e*d + (c*d*e - a*f*e) + (a*e*f - b*d*f) : by repeat {rw sub_mul}
... = a * (e * f) + (c * (d * e) + (-(a * (e * f)) + -(c * (d * e)))) : by simp
... = a * (e * f) + (c * (d * e) + -(a * (e * f)) + -(c * (d * e))) : by rw add_assoc
... = 0 : by simp
#check proof2
example (a b c d e f: ℤ) :
dot (cross (a, b, c) (d, e, f)) (a, b, c) = 0 ∧ dot (cross (a, b, c) (d, e, f)) (d, e, f) = 0 :=
and.intro proof1 proof2 |
c16293a1bae8e0be0ed9e05ba9cf42ebfdde63db | 4950bf76e5ae40ba9f8491647d0b6f228ddce173 | /src/category_theory/monad/coequalizer.lean | 1cf2dfc4637ccbb0fc52c58d16b252ae3bd484d1 | [
"Apache-2.0"
] | permissive | ntzwq/mathlib | ca50b21079b0a7c6781c34b62199a396dd00cee2 | 36eec1a98f22df82eaccd354a758ef8576af2a7f | refs/heads/master | 1,675,193,391,478 | 1,607,822,996,000 | 1,607,822,996,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,905 | lean | /-
Copyright (c) 2020 Bhavik Mehta. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Bhavik Mehta
-/
import category_theory.limits.shapes.reflexive
import category_theory.limits.preserves.shapes.equalizers
import category_theory.limits.shapes.split_coequalizer
import category_theory.limits.preserves.limits
import category_theory.monad.adjunction
/-!
# Special coequalizers associated to a monad
Associated to a monad `T : C ⥤ C` we have important coequalizer constructions:
Any algebra is a coequalizer (in the category of algebras) of free algebras. Furthermore, this
coequalizer is reflexive.
In `C`, this cofork diagram is a split coequalizer (in particular, it is still a coequalizer).
This split coequalizer is known as the Beck coequalizer (as it features heavily in Beck's
monadicity theorem).
-/
universes v₁ u₁
namespace category_theory
namespace monad
open limits
variables {C : Type u₁}
variables [category.{v₁} C]
variables {T : C ⥤ C} [monad T] (X : monad.algebra T)
/-!
Show that any algebra is a coequalizer of free algebras.
-/
/-- The top map in the coequalizer diagram we will construct. -/
@[simps {rhs_md := semireducible}]
def coequalizer.top_map : (monad.free T).obj (T.obj X.A) ⟶ (monad.free T).obj X.A :=
(monad.free T).map X.a
/-- The bottom map in the coequalizer diagram we will construct. -/
@[simps]
def coequalizer.bottom_map : (monad.free T).obj (T.obj X.A) ⟶ (monad.free T).obj X.A :=
{ f := (μ_ T).app X.A,
h' := monad.assoc X.A }
/-- The cofork map in the coequalizer diagram we will construct. -/
@[simps]
def coequalizer.π : (monad.free T).obj X.A ⟶ X :=
{ f := X.a,
h' := X.assoc.symm }
lemma coequalizer.condition :
coequalizer.top_map X ≫ coequalizer.π X = coequalizer.bottom_map X ≫ coequalizer.π X :=
algebra.hom.ext _ _ X.assoc.symm
instance : is_reflexive_pair (coequalizer.top_map X) (coequalizer.bottom_map X) :=
begin
apply is_reflexive_pair.mk' _ _ _,
apply (free T).map ((η_ T).app X.A),
{ ext,
dsimp,
rw [← T.map_comp, X.unit, T.map_id] },
{ ext,
apply monad.right_unit }
end
/--
Construct the Beck cofork in the category of algebras. This cofork is reflexive as well as a
coequalizer.
-/
@[simps {rhs_md := semireducible}]
def beck_algebra_cofork : cofork (coequalizer.top_map X) (coequalizer.bottom_map X) :=
cofork.of_π _ (coequalizer.condition X)
/--
The cofork constructed is a colimit. This shows that any algebra is a (reflexive) coequalizer of
free algebras.
-/
def beck_algebra_coequalizer : is_colimit (beck_algebra_cofork X) :=
cofork.is_colimit.mk' _ $ λ s,
begin
have h₁ : T.map X.a ≫ s.π.f = (μ_ T).app X.A ≫ s.π.f := congr_arg monad.algebra.hom.f s.condition,
have h₂ : T.map s.π.f ≫ s.X.a = (μ_ T).app X.A ≫ s.π.f := s.π.h,
refine ⟨⟨(η_ T).app _ ≫ s.π.f, _⟩, _, _⟩,
{ dsimp,
rw [T.map_comp, category.assoc, h₂, monad.right_unit_assoc,
(show X.a ≫ _ ≫ _ = _, from (η_ T).naturality_assoc _ _), h₁, monad.left_unit_assoc] },
{ ext,
simpa [← (η_ T).naturality_assoc, monad.left_unit_assoc] using ((η_ T).app (T.obj X.A)) ≫= h₁ },
{ intros m hm,
ext,
dsimp only,
rw ← hm,
apply (X.unit_assoc _).symm }
end
/-- The Beck cofork is a split coequalizer. -/
def beck_split_coequalizer : is_split_coequalizer (T.map X.a) ((μ_ T).app _) X.a :=
⟨(η_ T).app _, (η_ T).app _, X.assoc.symm, X.unit, monad.left_unit _, ((η_ T).naturality _).symm⟩
/-- This is the Beck cofork. It is a split coequalizer, in particular a coequalizer. -/
@[simps {rhs_md := semireducible}]
def beck_cofork : cofork (T.map X.a) ((μ_ T).app _) :=
(beck_split_coequalizer X).as_cofork
/-- The Beck cofork is a coequalizer. -/
def beck_coequalizer : is_colimit (beck_cofork X) :=
(beck_split_coequalizer X).is_coequalizer
end monad
end category_theory
|
64964118152a8106d55ce1989f741bac86b58114 | 46125763b4dbf50619e8846a1371029346f4c3db | /src/topology/opens.lean | 95d61c66b4d79dd47ba928f60053e970e8f54fd7 | [
"Apache-2.0"
] | permissive | thjread/mathlib | a9d97612cedc2c3101060737233df15abcdb9eb1 | 7cffe2520a5518bba19227a107078d83fa725ddc | refs/heads/master | 1,615,637,696,376 | 1,583,953,063,000 | 1,583,953,063,000 | 246,680,271 | 0 | 0 | Apache-2.0 | 1,583,960,875,000 | 1,583,960,875,000 | null | UTF-8 | Lean | false | false | 6,350 | 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
Subtype of open subsets in a topological space.
-/
import topology.bases topology.separation
import order.copy
open filter lattice
variables {α : Type*} {β : Type*} [topological_space α] [topological_space β]
namespace topological_space
variable (α)
/-- The type of open subsets of a topological space. -/
def opens := {s : set α // _root_.is_open s}
/-- The type of closed subsets of a topological space. -/
def closeds := {s : set α // is_closed s}
/-- The type of non-empty compact subsets of a topological space. The
non-emptiness will be useful in metric spaces, as we will be able to put
a distance (and not merely an edistance) on this space. -/
def nonempty_compacts := {s : set α // s.nonempty ∧ compact s}
section nonempty_compacts
open topological_space set
variable {α}
instance nonempty_compacts.to_compact_space {p : nonempty_compacts α} : compact_space p.val :=
⟨compact_iff_compact_univ.1 p.property.2⟩
instance nonempty_compacts.to_nonempty {p : nonempty_compacts α} : nonempty p.val :=
p.property.1.to_subtype
/-- Associate to a nonempty compact subset the corresponding closed subset -/
def nonempty_compacts.to_closeds [t2_space α] : nonempty_compacts α → closeds α :=
set.inclusion $ λ s hs, closed_of_compact _ hs.2
end nonempty_compacts
variable {α}
namespace opens
instance : has_coe (opens α) (set α) := { coe := subtype.val }
instance : has_subset (opens α) :=
{ subset := λ U V, U.val ⊆ V.val }
instance : has_mem α (opens α) :=
{ mem := λ a U, a ∈ U.val }
@[ext] lemma ext {U V : opens α} (h : U.val = V.val) : U = V := subtype.ext.mpr h
instance : partial_order (opens α) := subtype.partial_order _
def interior (s : set α) : opens α := ⟨interior s, is_open_interior⟩
lemma gc : galois_connection (subtype.val : opens α → set α) interior :=
λ U s, ⟨λ h, interior_maximal h U.property, λ h, le_trans h interior_subset⟩
def gi : @galois_insertion (order_dual (set α)) (order_dual (opens α)) _ _ interior (subtype.val) :=
{ choice := λ s hs, ⟨s, interior_eq_iff_open.mp $ le_antisymm interior_subset hs⟩,
gc := gc.dual,
le_l_u := λ _, interior_subset,
choice_eq := λ s hs, le_antisymm interior_subset hs }
@[simp] lemma gi_choice_val {s : order_dual (set α)} {hs} : (gi.choice s hs).val = s := rfl
instance : complete_lattice (opens α) :=
complete_lattice.copy
(@order_dual.lattice.complete_lattice _
(@galois_insertion.lift_complete_lattice
(order_dual (set α)) (order_dual (opens α)) interior (subtype.val : opens α → set α) _ _ gi))
/- le -/ (λ U V, U.1 ⊆ V.1) rfl
/- top -/ ⟨set.univ, _root_.is_open_univ⟩ (subtype.ext.mpr interior_univ.symm)
/- bot -/ ⟨∅, is_open_empty⟩ rfl
/- sup -/ (λ U V, ⟨U.1 ∪ V.1, _root_.is_open_union U.2 V.2⟩) rfl
/- inf -/ (λ U V, ⟨U.1 ∩ V.1, _root_.is_open_inter U.2 V.2⟩)
begin
funext,
apply subtype.ext.mpr,
symmetry,
apply interior_eq_of_open,
exact (_root_.is_open_inter U.2 V.2),
end
/- Sup -/ (λ Us, ⟨⋃₀ (subtype.val '' Us), _root_.is_open_sUnion $ λ U hU,
by { rcases hU with ⟨⟨V, hV⟩, h, h'⟩, dsimp at h', subst h', exact hV}⟩)
begin
funext,
apply subtype.ext.mpr,
simp [Sup_range],
refl,
end
/- Inf -/ _ rfl
instance : has_inter (opens α) := ⟨λ U V, U ⊓ V⟩
instance : has_union (opens α) := ⟨λ U V, U ⊔ V⟩
instance : has_emptyc (opens α) := ⟨⊥⟩
instance : inhabited (opens α) := ⟨∅⟩
@[simp] lemma inter_eq (U V : opens α) : U ∩ V = U ⊓ V := rfl
@[simp] lemma union_eq (U V : opens α) : U ∪ V = U ⊔ V := rfl
@[simp] lemma empty_eq : (∅ : opens α) = ⊥ := rfl
@[simp] lemma Sup_s {Us : set (opens α)} : (Sup Us).val = ⋃₀ (subtype.val '' Us) :=
begin
rw [@galois_connection.l_Sup (opens α) (set α) _ _ (subtype.val : opens α → set α) interior gc Us, set.sUnion_image],
congr
end
def is_basis (B : set (opens α)) : Prop := is_topological_basis (subtype.val '' B)
lemma is_basis_iff_nbhd {B : set (opens α)} :
is_basis B ↔ ∀ {U : opens α} {x}, x ∈ U → ∃ U' ∈ B, x ∈ U' ∧ U' ⊆ U :=
begin
split; intro h,
{ rintros ⟨sU, hU⟩ x hx,
rcases (mem_nhds_of_is_topological_basis h).mp (mem_nhds_sets hU hx) with ⟨sV, ⟨⟨V, H₁, H₂⟩, hsV⟩⟩,
refine ⟨V, H₁, _⟩,
cases V, dsimp at H₂, subst H₂, exact hsV },
{ refine is_topological_basis_of_open_of_nhds _ _,
{ rintros sU ⟨U, ⟨H₁, H₂⟩⟩, subst H₂, exact U.property },
{ intros x sU hx hsU,
rcases @h (⟨sU, hsU⟩ : opens α) x hx with ⟨V, hV, H⟩,
exact ⟨V, ⟨V, hV, rfl⟩, H⟩ } }
end
lemma is_basis_iff_cover {B : set (opens α)} :
is_basis B ↔ ∀ U : opens α, ∃ Us ⊆ B, U = Sup Us :=
begin
split,
{ intros hB U,
rcases sUnion_basis_of_is_open hB U.property with ⟨sUs, H, hU⟩,
existsi {U : opens α | U ∈ B ∧ U.val ∈ sUs},
split,
{ intros U hU, exact hU.left },
{ apply ext,
rw [Sup_s, hU],
congr,
ext s; split; intro hs,
{ rcases H hs with ⟨V, hV⟩,
rw ← hV.right at hs,
refine ⟨V, ⟨⟨hV.left, hs⟩, hV.right⟩⟩ },
{ rcases hs with ⟨V, ⟨⟨H₁, H₂⟩, H₃⟩⟩,
subst H₃, exact H₂ } } },
{ intro h,
rw is_basis_iff_nbhd,
intros U x hx,
rcases h U with ⟨Us, hUs, H⟩,
replace H := congr_arg subtype.val H,
rw Sup_s at H,
change x ∈ U.val at hx,
rw H at hx,
rcases set.mem_sUnion.mp hx with ⟨sV, ⟨⟨V, H₁, H₂⟩, hsV⟩⟩,
refine ⟨V,hUs H₁,_⟩,
cases V with V hV,
dsimp at H₂, subst H₂,
refine ⟨hsV,_⟩,
change V ⊆ U.val, rw H,
exact set.subset_sUnion_of_mem ⟨⟨V, _⟩, ⟨H₁, rfl⟩⟩ }
end
end opens
end topological_space
namespace continuous
open topological_space
def comap {f : α → β} (hf : continuous f) (V : opens β) : opens α :=
⟨f ⁻¹' V.1, hf V.1 V.2⟩
@[simp] lemma comap_id (U : opens α) : (continuous_id).comap U = U := by { ext, refl }
lemma comap_mono {f : α → β} (hf : continuous f) {V W : opens β} (hVW : V ⊆ W) :
hf.comap V ⊆ hf.comap W :=
λ _ h, hVW h
end continuous
|
aec68d18ce4709aa0a46e8f1e178248a6d283ee1 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/group_theory/is_free_group.lean | 528ee7e69965da1c538f0da68fb585e4d0f812cb | [
"Apache-2.0"
] | permissive | Vierkantor/mathlib | 0ea59ac32a3a43c93c44d70f441c4ee810ccceca | 83bc3b9ce9b13910b57bda6b56222495ebd31c2f | refs/heads/master | 1,658,323,012,449 | 1,652,256,003,000 | 1,652,256,003,000 | 209,296,341 | 0 | 1 | Apache-2.0 | 1,568,807,655,000 | 1,568,807,655,000 | null | UTF-8 | Lean | false | false | 5,404 | lean | /-
Copyright (c) 2021 David Wärn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: David Wärn, Eric Wieser, Joachim Breitner
-/
import group_theory.free_group
/-!
# Free groups structures on arbitrary types
This file defines a type class for type that are free groups, together with the usual operations.
The type class can be instantiated by providing an isomorphim to the canonical free group, or by
proving that the universal property holds.
For the explicit construction of free groups, see `group_theory/free_group`.
## Main definitions
* `is_free_group G` - a typeclass to indicate that `G` is free over some generators
* `is_free_group.of` - the canonical injection of `G`'s generators into `G`
* `is_free_group.lift` - the universal property of the free group
## Main results
* `is_free_group.to_free_group` - any free group with generators `A` is equivalent to
`free_group A`.
* `is_free_group.unique_lift` - the universal property of a free group
* `is_free_group.of_unique_lift` - constructing `is_free_group` from the universal property
-/
universes u
/-- `is_free_group G` means that `G` isomorphic to a free group. -/
class is_free_group (G : Type u) [group G] :=
(generators : Type u)
(mul_equiv [] : free_group generators ≃* G)
instance (X : Type*) : is_free_group (free_group X) :=
{ generators := X,
mul_equiv := mul_equiv.refl _ }
namespace is_free_group
variables (G : Type*) [group G] [is_free_group G]
/-- Any free group is isomorphic to "the" free group. -/
@[simps] def to_free_group : G ≃* free_group (generators G) := (mul_equiv G).symm
variable {G}
/-- The canonical injection of G's generators into G -/
def of : generators G → G := (mul_equiv G).to_fun ∘ free_group.of
@[simp] lemma of_eq_free_group_of {A : Type u} : (@of (free_group A) _ _ ) = free_group.of := rfl
variables {H : Type*} [group H]
/-- The equivalence between functions on the generators and group homomorphisms from a free group
given by those generators. -/
def lift : (generators G → H) ≃ (G →* H) :=
free_group.lift.trans
{ to_fun := λ f, f.comp (mul_equiv G).symm.to_monoid_hom,
inv_fun := λ f, f.comp (mul_equiv G).to_monoid_hom,
left_inv := λ f, by { ext, simp, },
right_inv := λ f, by { ext, simp, }, }
@[simp] lemma lift'_eq_free_group_lift {A : Type u} :
(@lift (free_group A) _ _ H _) = free_group.lift := rfl
@[simp] lemma lift_of (f : generators G → H) (a : generators G) : lift f (of a) = f a :=
congr_fun (lift.symm_apply_apply f) a
@[simp] lemma lift_symm_apply (f : G →* H) (a : generators G) : (lift.symm f) a = f (of a) :=
rfl
@[ext] lemma ext_hom ⦃f g : G →* H⦄ (h : ∀ (a : generators G), f (of a) = g (of a)) : f = g :=
lift.symm.injective (funext h)
/-- The universal property of a free group: A functions from the generators of `G` to another
group extends in a unique way to a homomorphism from `G`.
Note that since `is_free_group.lift` is expressed as a bijection, it already
expresses the universal property. -/
lemma unique_lift (f : generators G → H) : ∃! F : G →* H, ∀ a, F (of a) = f a :=
by simpa only [function.funext_iff] using lift.symm.bijective.exists_unique f
/-- If a group satisfies the universal property of a free group, then it is a free group, where
the universal property is expressed as in `is_free_group.lift` and its properties. -/
def of_lift {G : Type u} [group G] (X : Type u)
(of : X → G)
(lift : ∀ {H : Type u} [group H], by exactI (X → H) ≃ (G →* H))
(lift_of : ∀ {H : Type u} [group H], by exactI ∀ (f : X → H) a, lift f (of a) = f a) :
is_free_group G :=
{ generators := X,
mul_equiv := monoid_hom.to_mul_equiv
(free_group.lift of)
(lift free_group.of)
begin
apply free_group.ext_hom, intro x,
simp only [monoid_hom.coe_comp, function.comp_app, monoid_hom.id_apply, free_group.lift.of,
lift_of],
end
begin
let lift_symm_of : ∀ {H : Type u} [group H], by exactI ∀ (f : G →* H) a,
lift.symm f a = f (of a) := by introsI H _ f a; simp [← lift_of (lift.symm f)],
apply lift.symm.injective, ext x,
simp only [monoid_hom.coe_comp, function.comp_app, monoid_hom.id_apply,
free_group.lift.of, lift_of, lift_symm_of],
end }
/-- If a group satisfies the universal property of a free group, then it is a free group, where
the universal property is expressed as in `is_free_group.unique_lift`. -/
noncomputable
def of_unique_lift {G : Type u} [group G] (X : Type u)
(of : X → G)
(h : ∀ {H : Type u} [group H] (f : X → H), by exactI ∃! F : G →* H, ∀ a, F (of a) = f a) :
is_free_group G :=
let lift {H : Type u} [group H] : by exactI (X → H) ≃ (G →* H) := by exactI
{ to_fun := λ f, classical.some (h f),
inv_fun := λ F, F ∘ of,
left_inv := λ f, funext (classical.some_spec (h f)).left,
right_inv := λ F, ((classical.some_spec (h (F ∘ of))).right F (λ _, rfl)).symm } in
let lift_of {H : Type u} [group H] (f : X → H) (a : X) : by exactI lift f (of a) = f a :=
by exactI congr_fun (lift.symm_apply_apply f) a in
of_lift X of @lift @lift_of
/-- Being a free group transports across group isomorphisms. -/
def of_mul_equiv {H : Type*} [group H] (h : G ≃* H) : is_free_group H :=
{ generators := generators G, mul_equiv := (mul_equiv G).trans h }
end is_free_group
|
d833aaceb6ee0ed8f97f6d00880a64345d7c5bb2 | e0f9ba56b7fedc16ef8697f6caeef5898b435143 | /src/ring_theory/free_comm_ring.lean | 181f524e8983e3293bef4fdb98f3fc827f040d8a | [
"Apache-2.0"
] | permissive | anrddh/mathlib | 6a374da53c7e3a35cb0298b0cd67824efef362b4 | a4266a01d2dcb10de19369307c986d038c7bb6a6 | refs/heads/master | 1,656,710,827,909 | 1,589,560,456,000 | 1,589,560,456,000 | 264,271,800 | 0 | 0 | Apache-2.0 | 1,589,568,062,000 | 1,589,568,061,000 | null | UTF-8 | Lean | false | false | 16,022 | lean | /-
Copyright (c) 2019 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Johan Commelin
-/
import data.equiv.functor
import data.mv_polynomial
import ring_theory.ideal_operations
import ring_theory.free_ring
noncomputable theory
local attribute [instance, priority 100] classical.prop_decidable
universes u v
variables (α : Type u)
def free_comm_ring (α : Type u) : Type u :=
free_abelian_group $ multiplicative $ multiset α
namespace free_comm_ring
instance : comm_ring (free_comm_ring α) := free_abelian_group.comm_ring _
instance : inhabited (free_comm_ring α) := ⟨0⟩
variables {α}
def of (x : α) : free_comm_ring α :=
free_abelian_group.of ([x] : multiset α)
@[elab_as_eliminator] protected lemma induction_on
{C : free_comm_ring α → Prop} (z : free_comm_ring α)
(hn1 : C (-1)) (hb : ∀ b, C (of b))
(ha : ∀ x y, C x → C y → C (x + y))
(hm : ∀ x y, C x → C y → C (x * y)) : C z :=
have hn : ∀ x, C x → C (-x), from λ x ih, neg_one_mul x ▸ hm _ _ hn1 ih,
have h1 : C 1, from neg_neg (1 : free_comm_ring α) ▸ hn _ hn1,
free_abelian_group.induction_on z
(add_left_neg (1 : free_comm_ring α) ▸ ha _ _ hn1 h1)
(λ m, multiset.induction_on m h1 $ λ a m ih, hm _ _ (hb a) ih)
(λ m ih, hn _ ih)
ha
section lift
variables {β : Type v} [comm_ring β] (f : α → β)
/-- Lift a map `α → R` to a ring homomorphism `free_comm_ring α → R`.
For a version producing a bundled homomorphism, see `lift_hom`. -/
def lift : free_comm_ring α → β :=
free_abelian_group.lift $ λ s, (s.map f).prod
@[simp] lemma lift_zero : lift f 0 = 0 := rfl
@[simp] lemma lift_one : lift f 1 = 1 :=
free_abelian_group.lift.of _ _
@[simp] lemma lift_of (x : α) : lift f (of x) = f x :=
(free_abelian_group.lift.of _ _).trans $ mul_one _
@[simp] lemma lift_add (x y) : lift f (x + y) = lift f x + lift f y :=
free_abelian_group.lift.add _ _ _
@[simp] lemma lift_neg (x) : lift f (-x) = -lift f x :=
free_abelian_group.lift.neg _ _
@[simp] lemma lift_sub (x y) : lift f (x - y) = lift f x - lift f y :=
free_abelian_group.lift.sub _ _ _
@[simp] lemma lift_mul (x y) : lift f (x * y) = lift f x * lift f y :=
begin
refine free_abelian_group.induction_on y (mul_zero _).symm _ _ _,
{ intros s2, conv { to_lhs, dsimp only [(*), mul_zero_class.mul, semiring.mul, ring.mul, semigroup.mul, comm_ring.mul] },
rw [free_abelian_group.lift.of, lift, free_abelian_group.lift.of],
refine free_abelian_group.induction_on x (zero_mul _).symm _ _ _,
{ intros s1, iterate 3 { rw free_abelian_group.lift.of },
calc _ = multiset.prod ((multiset.map f s1) + (multiset.map f s2)) :
by {congr' 1, exact multiset.map_add _ _ _}
... = _ : multiset.prod_add _ _ },
{ intros s1 ih, iterate 3 { rw free_abelian_group.lift.neg }, rw [ih, neg_mul_eq_neg_mul] },
{ intros x1 x2 ih1 ih2, iterate 3 { rw free_abelian_group.lift.add }, rw [ih1, ih2, add_mul] } },
{ intros s2 ih, rw [mul_neg_eq_neg_mul_symm, lift_neg, lift_neg, mul_neg_eq_neg_mul_symm, ih] },
{ intros y1 y2 ih1 ih2, rw [mul_add, lift_add, lift_add, mul_add, ih1, ih2] },
end
/-- Lift of a map `f : α → β` to `free_comm_ring α` as a ring homomorphism.
We don't use it as the canonical form because Lean fails to coerce it to a function. -/
def lift_hom : free_comm_ring α →+* β := ⟨lift f, lift_one f, lift_mul f, lift_zero f, lift_add f⟩
instance : is_ring_hom (lift f) := (lift_hom f).is_ring_hom
@[simp] lemma coe_lift_hom : ⇑(lift_hom f : free_comm_ring α →+* β) = lift f := rfl
@[simp] lemma lift_pow (x) (n : ℕ) : lift f (x ^ n) = lift f x ^ n :=
(lift_hom f).map_pow _ _
@[simp] lemma lift_comp_of (f : free_comm_ring α → β) [is_ring_hom f] : lift (f ∘ of) = f :=
funext $ λ x, free_comm_ring.induction_on x
(by rw [lift_neg, lift_one, is_ring_hom.map_neg f, is_ring_hom.map_one f])
(lift_of _)
(λ x y ihx ihy, by rw [lift_add, is_ring_hom.map_add f, ihx, ihy])
(λ x y ihx ihy, by rw [lift_mul, is_ring_hom.map_mul f, ihx, ihy])
end lift
variables {β : Type v} (f : α → β)
/-- A map `f : α → β` produces a ring homomorphism `free_comm_ring α → free_comm_ring β`. -/
def map : free_comm_ring α →+* free_comm_ring β :=
lift_hom $ of ∘ f
lemma map_zero : map f 0 = 0 := rfl
lemma map_one : map f 1 = 1 := rfl
lemma map_of (x : α) : map f (of x) = of (f x) := lift_of _ _
lemma map_add (x y) : map f (x + y) = map f x + map f y := lift_add _ _ _
lemma map_neg (x) : map f (-x) = -map f x := lift_neg _ _
lemma map_sub (x y) : map f (x - y) = map f x - map f y := lift_sub _ _ _
lemma map_mul (x y) : map f (x * y) = map f x * map f y := lift_mul _ _ _
lemma map_pow (x) (n : ℕ) : map f (x ^ n) = (map f x) ^ n := lift_pow _ _ _
def is_supported (x : free_comm_ring α) (s : set α) : Prop :=
x ∈ ring.closure (of '' s)
section is_supported
variables {x y : free_comm_ring α} {s t : set α}
theorem is_supported_upwards (hs : is_supported x s) (hst : s ⊆ t) :
is_supported x t :=
ring.closure_mono (set.monotone_image hst) hs
theorem is_supported_add (hxs : is_supported x s) (hys : is_supported y s) :
is_supported (x + y) s :=
is_add_submonoid.add_mem hxs hys
theorem is_supported_neg (hxs : is_supported x s) :
is_supported (-x) s :=
is_add_subgroup.neg_mem hxs
theorem is_supported_sub (hxs : is_supported x s) (hys : is_supported y s) :
is_supported (x - y) s :=
is_add_subgroup.sub_mem _ _ _ hxs hys
theorem is_supported_mul (hxs : is_supported x s) (hys : is_supported y s) :
is_supported (x * y) s :=
is_submonoid.mul_mem hxs hys
theorem is_supported_zero : is_supported 0 s :=
is_add_submonoid.zero_mem
theorem is_supported_one : is_supported 1 s :=
is_submonoid.one_mem
theorem is_supported_int {i : ℤ} {s : set α} : is_supported ↑i s :=
int.induction_on i is_supported_zero
(λ i hi, by rw [int.cast_add, int.cast_one]; exact is_supported_add hi is_supported_one)
(λ i hi, by rw [int.cast_sub, int.cast_one]; exact is_supported_sub hi is_supported_one)
end is_supported
def restriction (s : set α) [decidable_pred s] (x : free_comm_ring α) : free_comm_ring s :=
lift (λ p, if H : p ∈ s then of ⟨p, H⟩ else 0) x
section restriction
variables (s : set α) [decidable_pred s] (x y : free_comm_ring α)
@[simp] lemma restriction_of (p) : restriction s (of p) = if H : p ∈ s then of ⟨p, H⟩ else 0 := lift_of _ _
@[simp] lemma restriction_zero : restriction s 0 = 0 := lift_zero _
@[simp] lemma restriction_one : restriction s 1 = 1 := lift_one _
@[simp] lemma restriction_add : restriction s (x + y) = restriction s x + restriction s y := lift_add _ _ _
@[simp] lemma restriction_neg : restriction s (-x) = -restriction s x := lift_neg _ _
@[simp] lemma restriction_sub : restriction s (x - y) = restriction s x - restriction s y := lift_sub _ _ _
@[simp] lemma restriction_mul : restriction s (x * y) = restriction s x * restriction s y := lift_mul _ _ _
end restriction
theorem is_supported_of {p} {s : set α} : is_supported (of p) s ↔ p ∈ s :=
suffices is_supported (of p) s → p ∈ s, from ⟨this, λ hps, ring.subset_closure ⟨p, hps, rfl⟩⟩,
assume hps : is_supported (of p) s, begin
classical,
have : ∀ x, is_supported x s →
∃ (n : ℤ), lift (λ a, if a ∈ s then (0 : polynomial ℤ) else polynomial.X) x = n,
{ intros x hx, refine ring.in_closure.rec_on hx _ _ _ _,
{ use 1, rw [lift_one], norm_cast },
{ use -1, rw [lift_neg, lift_one], norm_cast },
{ rintros _ ⟨z, hzs, rfl⟩ _ _, use 0, rw [lift_mul, lift_of, if_pos hzs, zero_mul], norm_cast },
{ rintros x y ⟨q, hq⟩ ⟨r, hr⟩, refine ⟨q+r, _⟩, rw [lift_add, hq, hr], norm_cast } },
specialize this (of p) hps, rw [lift_of] at this, split_ifs at this, { exact h },
exfalso, apply ne.symm int.zero_ne_one,
rcases this with ⟨w, H⟩, rw polynomial.int_cast_eq_C at H,
have : polynomial.X.coeff 1 = (polynomial.C ↑w).coeff 1, by rw H,
rwa [polynomial.coeff_C, if_neg one_ne_zero, polynomial.coeff_X, if_pos rfl] at this,
apply_instance
end
theorem map_subtype_val_restriction {x} (s : set α) [decidable_pred s] (hxs : is_supported x s) :
map (subtype.val : s → α) (restriction s x) = x :=
begin
refine ring.in_closure.rec_on hxs _ _ _ _,
{ rw restriction_one, refl },
{ rw [restriction_neg, map_neg, restriction_one], refl },
{ rintros _ ⟨p, hps, rfl⟩ n ih, rw [restriction_mul, restriction_of, dif_pos hps, map_mul, map_of, ih] },
{ intros x y ihx ihy, rw [restriction_add, map_add, ihx, ihy] }
end
theorem exists_finite_support (x : free_comm_ring α) : ∃ s : set α, set.finite s ∧ is_supported x s :=
free_comm_ring.induction_on x
⟨∅, set.finite_empty, is_supported_neg is_supported_one⟩
(λ p, ⟨{p}, set.finite_singleton p, is_supported_of.2 $ finset.mem_singleton_self _⟩)
(λ x y ⟨s, hfs, hxs⟩ ⟨t, hft, hxt⟩, ⟨s ∪ t, set.finite_union hfs hft, is_supported_add
(is_supported_upwards hxs $ set.subset_union_left s t)
(is_supported_upwards hxt $ set.subset_union_right s t)⟩)
(λ x y ⟨s, hfs, hxs⟩ ⟨t, hft, hxt⟩, ⟨s ∪ t, set.finite_union hfs hft, is_supported_mul
(is_supported_upwards hxs $ set.subset_union_left s t)
(is_supported_upwards hxt $ set.subset_union_right s t)⟩)
theorem exists_finset_support (x : free_comm_ring α) : ∃ s : finset α, is_supported x ↑s :=
let ⟨s, hfs, hxs⟩ := exists_finite_support x in ⟨hfs.to_finset, by rwa set.finite.coe_to_finset⟩
end free_comm_ring
namespace free_ring
open function
variable (α)
def to_free_comm_ring {α} : free_ring α → free_comm_ring α :=
free_ring.lift free_comm_ring.of
instance to_free_comm_ring.is_ring_hom : is_ring_hom (@to_free_comm_ring α) :=
free_ring.is_ring_hom free_comm_ring.of
instance : has_coe (free_ring α) (free_comm_ring α) := ⟨to_free_comm_ring⟩
instance coe.is_ring_hom : is_ring_hom (coe : free_ring α → free_comm_ring α) :=
free_ring.to_free_comm_ring.is_ring_hom _
@[simp, norm_cast] protected lemma coe_zero : ↑(0 : free_ring α) = (0 : free_comm_ring α) := rfl
@[simp, norm_cast] protected lemma coe_one : ↑(1 : free_ring α) = (1 : free_comm_ring α) := rfl
variable {α}
@[simp] protected lemma coe_of (a : α) : ↑(free_ring.of a) = free_comm_ring.of a :=
free_ring.lift_of _ _
@[simp, norm_cast] protected lemma coe_neg (x : free_ring α) : ↑(-x) = -(x : free_comm_ring α) :=
free_ring.lift_neg _ _
@[simp, norm_cast] protected lemma coe_add (x y : free_ring α) : ↑(x + y) = (x : free_comm_ring α) + y :=
free_ring.lift_add _ _ _
@[simp, norm_cast] protected lemma coe_sub (x y : free_ring α) : ↑(x - y) = (x : free_comm_ring α) - y :=
free_ring.lift_sub _ _ _
@[simp, norm_cast] protected lemma coe_mul (x y : free_ring α) : ↑(x * y) = (x : free_comm_ring α) * y :=
free_ring.lift_mul _ _ _
variable (α)
protected lemma coe_surjective : surjective (coe : free_ring α → free_comm_ring α) :=
λ x,
begin
apply free_comm_ring.induction_on x,
{ use -1, refl },
{ intro x, use free_ring.of x, refl },
{ rintros _ _ ⟨x, rfl⟩ ⟨y, rfl⟩, use x + y, exact free_ring.lift_add _ _ _ },
{ rintros _ _ ⟨x, rfl⟩ ⟨y, rfl⟩, use x * y, exact free_ring.lift_mul _ _ _ }
end
lemma coe_eq :
(coe : free_ring α → free_comm_ring α) =
@functor.map free_abelian_group _ _ _ (λ (l : list α), (l : multiset α)) :=
begin
funext,
apply @free_abelian_group.lift.ext _ _ _
(coe : free_ring α → free_comm_ring α) _ _ (free_abelian_group.lift.is_add_group_hom _),
intros x,
change free_ring.lift free_comm_ring.of (free_abelian_group.of x) = _,
change _ = free_abelian_group.of (↑x),
induction x with hd tl ih, {refl},
simp only [*, free_ring.lift, free_comm_ring.of, free_abelian_group.of, free_abelian_group.lift,
free_group.of, free_group.to_group, free_group.to_group.aux,
mul_one, free_group.quot_lift_mk, abelianization.lift.of, bool.cond_tt, list.prod_cons,
cond, list.prod_nil, list.map] at *,
refl
end
def subsingleton_equiv_free_comm_ring [subsingleton α] :
free_ring α ≃+* free_comm_ring α :=
@ring_equiv.of' (free_ring α) (free_comm_ring α) _ _
(functor.map_equiv free_abelian_group (multiset.subsingleton_equiv α)) $
begin
delta functor.map_equiv,
rw congr_arg is_ring_hom _,
work_on_goal 2 { symmetry, exact coe_eq α },
apply_instance
end
instance [subsingleton α] : comm_ring (free_ring α) :=
{ mul_comm := λ x y,
by rw [← (subsingleton_equiv_free_comm_ring α).left_inv (y * x),
is_ring_hom.map_mul ((subsingleton_equiv_free_comm_ring α)).to_fun,
mul_comm,
← is_ring_hom.map_mul ((subsingleton_equiv_free_comm_ring α)).to_fun,
(subsingleton_equiv_free_comm_ring α).left_inv],
.. free_ring.ring α }
end free_ring
def free_comm_ring_equiv_mv_polynomial_int :
free_comm_ring α ≃+* mv_polynomial α ℤ :=
{ to_fun := free_comm_ring.lift $ λ a, mv_polynomial.X a,
inv_fun := mv_polynomial.eval₂ coe free_comm_ring.of,
left_inv :=
begin
intro x,
haveI : is_semiring_hom (coe : int → free_comm_ring α) :=
(int.cast_ring_hom _).is_semiring_hom,
refine free_abelian_group.induction_on x rfl _ _ _,
{ intro s,
refine multiset.induction_on s _ _,
{ unfold free_comm_ring.lift,
rw [free_abelian_group.lift.of],
exact mv_polynomial.eval₂_one _ _ },
{ intros hd tl ih,
show mv_polynomial.eval₂ coe free_comm_ring.of
(free_comm_ring.lift (λ a, mv_polynomial.X a)
(free_comm_ring.of hd * free_abelian_group.of tl)) =
free_comm_ring.of hd * free_abelian_group.of tl,
rw [free_comm_ring.lift_mul, free_comm_ring.lift_of,
mv_polynomial.eval₂_mul, mv_polynomial.eval₂_X, ih] } },
{ intros s ih,
rw [free_comm_ring.lift_neg, ← neg_one_mul, mv_polynomial.eval₂_mul,
← mv_polynomial.C_1, ← mv_polynomial.C_neg, mv_polynomial.eval₂_C,
int.cast_neg, int.cast_one, neg_one_mul, ih] },
{ intros x₁ x₂ ih₁ ih₂, rw [free_comm_ring.lift_add, mv_polynomial.eval₂_add, ih₁, ih₂] }
end,
right_inv :=
begin
intro x,
haveI : is_semiring_hom (coe : int → free_comm_ring α) :=
(int.cast_ring_hom _).is_semiring_hom,
have : ∀ i : ℤ, free_comm_ring.lift (λ (a : α), mv_polynomial.X a) ↑i = mv_polynomial.C i,
{ exact λ i, int.induction_on i
(by rw [int.cast_zero, free_comm_ring.lift_zero, mv_polynomial.C_0])
(λ i ih, by rw [int.cast_add, int.cast_one, free_comm_ring.lift_add,
free_comm_ring.lift_one, ih, mv_polynomial.C_add, mv_polynomial.C_1])
(λ i ih, by rw [int.cast_sub, int.cast_one, free_comm_ring.lift_sub,
free_comm_ring.lift_one, ih, mv_polynomial.C_sub, mv_polynomial.C_1]) },
apply mv_polynomial.induction_on x,
{ intro i, rw [mv_polynomial.eval₂_C, this] },
{ intros p q ihp ihq, rw [mv_polynomial.eval₂_add, free_comm_ring.lift_add, ihp, ihq] },
{ intros p a ih,
rw [mv_polynomial.eval₂_mul, mv_polynomial.eval₂_X,
free_comm_ring.lift_mul, free_comm_ring.lift_of, ih] }
end,
.. free_comm_ring.lift_hom $ λ a, mv_polynomial.X a }
def free_comm_ring_pempty_equiv_int : free_comm_ring pempty.{u+1} ≃+* ℤ :=
ring_equiv.trans (free_comm_ring_equiv_mv_polynomial_int _) (mv_polynomial.pempty_ring_equiv _)
def free_comm_ring_punit_equiv_polynomial_int : free_comm_ring punit.{u+1} ≃+* polynomial ℤ :=
ring_equiv.trans (free_comm_ring_equiv_mv_polynomial_int _) (mv_polynomial.punit_ring_equiv _)
open free_ring
def free_ring_pempty_equiv_int : free_ring pempty.{u+1} ≃+* ℤ :=
ring_equiv.trans (subsingleton_equiv_free_comm_ring _) free_comm_ring_pempty_equiv_int
def free_ring_punit_equiv_polynomial_int : free_ring punit.{u+1} ≃+* polynomial ℤ :=
ring_equiv.trans (subsingleton_equiv_free_comm_ring _) free_comm_ring_punit_equiv_polynomial_int
|
cc07b66b1620463fa38666a9c22ae7082dbd688a | 624f6f2ae8b3b1adc5f8f67a365c51d5126be45a | /tmp/new-frontend/parser/notation.lean | bce3240f6ddd53bd1851b93ae784744e45c46ce4 | [
"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 | 5,694 | lean | /-
Copyright (c) 2018 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Sebastian Ullrich
Notation parsers
-/
prelude
import init.lean.parser.token
namespace Lean
namespace Parser
open Combinators MonadParsec
open Parser.HasTokens Parser.HasView
local postfix `?`:10000 := optional
local postfix *:10000 := Combinators.many
local postfix +:10000 := Combinators.many1
@[derive Parser.HasTokens Parser.HasView]
def Term.Parser (rbp := 0) : termParser :=
recurse rbp <?> "Term"
set_option class.instance_max_depth 100
namespace «command»
namespace NotationSpec
@[derive Parser.HasTokens Parser.HasView]
def precedenceLit.Parser : termParser :=
nodeChoice! precedenceLit {
num: number.Parser,
max: symbolOrIdent "max",
-- TODO(Sebastian): `precOf`?
}
def precedenceLit.View.toNat : precedenceLit.View → Nat
| (precedenceLit.View.num n) := n.toNat
| (precedenceLit.View.max _) := maxPrec
@[derive Parser.HasTokens Parser.HasView]
def precedenceTerm.Parser : termParser :=
nodeChoice! precedenceTerm {
lit: precedenceLit.Parser,
offset: node! precedenceOffset ["(", lit: precedenceLit.Parser,
op: nodeChoice! precedenceOffsetOp {" + ", " - "},
offset: number.Parser,
")",
]
}
def precedenceTerm.View.toNat : precedenceTerm.View → Nat
| (precedenceTerm.View.lit l) := l.toNat
| (precedenceTerm.View.offset o) := match o.op with
| (precedenceOffsetOp.View.«+» _) := o.lit.toNat.add o.offset.toNat
| (precedenceOffsetOp.View.«-» _) := o.lit.toNat - o.offset.toNat
@[derive Parser.HasTokens Parser.HasView]
def precedence.Parser : termParser :=
node! «precedence» [":", Term: precedenceTerm.Parser]
@[derive Parser.HasTokens Parser.HasView]
def quotedSymbol.Parser : termParser :=
raw $ takeUntil (= '`')
@[derive Parser.HasTokens Parser.HasView]
def symbolQuote.Parser : termParser :=
node! symbolQuote [
leftQuote: rawStr "`",
symbol: quotedSymbol.Parser,
rightQuote: rawStr "`" true, -- consume trailing ws
prec: precedence.Parser?]
def unquotedSymbol.Parser : termParser :=
try $ do {
it ← leftOver,
stx@(Syntax.atom _) ← monadLift token | error "" (DList.singleton "symbol") it,
pure stx
} <?> "symbol"
instance unquotedSymbol.tokens : Parser.HasTokens unquotedSymbol.Parser := ⟨[]⟩
instance unquotedSymbol.View : Parser.HasView (Option SyntaxAtom) unquotedSymbol.Parser :=
{ view := λ stx, match stx with
| Syntax.atom atom := some atom
| _ := none,
review := λ a, (Syntax.atom <$> a).getOrElse Syntax.missing }
--TODO(Sebastian): cannot be called `symbol` because of hygiene problems
@[derive Parser.HasTokens Parser.HasView]
def notationSymbol.Parser : termParser :=
nodeChoice! notationSymbol {
quoted: symbolQuote.Parser,
--TODO(Sebastian): decide if we want this in notations
--unquoted: unquotedSymbol.Parser
}
@[derive Parser.HasTokens Parser.HasView]
def mixfixSymbol.Parser : termParser :=
nodeChoice! mixfixSymbol {
quoted: symbolQuote.Parser,
unquoted: unquotedSymbol.Parser
}
@[derive Parser.HasTokens Parser.HasView]
def foldAction.Parser : termParser :=
node! foldAction [
"(",
op: anyOf [symbolOrIdent "foldl", symbolOrIdent "foldr"],
sep: notationSymbol.Parser,
folder: node! foldActionFolder [
"(",
arg1: ident.Parser,
arg2: ident.Parser,
",",
rhs: Term.Parser,
")"
],
init: Term.Parser,
endTk: notationSymbol.Parser,
")"
]
@[derive Parser.HasTokens Parser.HasView]
def action.Parser : termParser :=
node! action [":", kind: nodeChoice! actionKind {
prec: try precedenceTerm.Parser,
prev: symbolOrIdent "prev",
scoped: node! scopedAction [
try ["(", scoped: symbolOrIdent "scoped"],
prec: precedence.Parser?,
id: ident.Parser,
", ",
Term: Term.Parser,
")",
],
fold: foldAction.Parser
}]
@[derive Parser.HasTokens Parser.HasView]
def transition.Parser : termParser :=
nodeChoice! transition {
binder: node! binder [binder: symbolOrIdent "binder", prec: precedence.Parser?],
binders: node! binders [binders: symbolOrIdent "binders", prec: precedence.Parser?],
Arg: node! argument [id: ident.Parser, action: action.Parser?]
}
@[derive Parser.HasTokens Parser.HasView]
def rule.Parser : termParser :=
node! rule [symbol: notationSymbol.Parser, transition: transition.Parser?]
end NotationSpec
@[derive Parser.HasTokens Parser.HasView]
def NotationSpec.Parser : termParser :=
node! NotationSpec [prefixArg: ident.Parser?, rules: NotationSpec.rule.Parser*]
@[derive Parser.HasTokens Parser.HasView]
def notation.Parser : termParser :=
node! «notation» [
try [«local»: (symbol "local ")?, "notation"],
spec: NotationSpec.Parser, ":=", Term: Term.Parser]
@[derive Parser.HasTokens Parser.HasView]
def reserveNotation.Parser : termParser :=
node! «reserveNotation» [try ["reserve", "notation"], spec: NotationSpec.Parser]
@[derive Parser.HasTokens Parser.HasView]
def mixfix.kind.Parser : termParser :=
nodeChoice! mixfix.kind {"prefix", "infix", "infixl", "infixr", "postfix"}
@[derive Parser.HasTokens Parser.HasView]
def mixfix.Parser : termParser :=
node! «mixfix» [
try [«local»: (symbol "local ")?, kind: mixfix.kind.Parser],
symbol: NotationSpec.mixfixSymbol.Parser, ":=", Term: Term.Parser]
@[derive Parser.HasTokens Parser.HasView]
def notationLike.Parser : termParser :=
nodeChoice! notationLike {«notation»: notation.Parser, mixfix: mixfix.Parser}
@[derive Parser.HasTokens Parser.HasView]
def reserveMixfix.Parser : termParser :=
node! «reserveMixfix» [
try ["reserve", kind: mixfix.kind.Parser],
symbol: NotationSpec.notationSymbol.Parser]
end «command»
end Parser
end Lean
|
38612783a7bb07433aaa58de50839ee01dcec066 | f7315930643edc12e76c229a742d5446dad77097 | /tests/lean/run/univ2.lean | fbaf29e83f8711bcf8e057485a0343552eaa867c | [
"Apache-2.0"
] | permissive | bmalehorn/lean | 8f77b762a76c59afff7b7403f9eb5fc2c3ce70c1 | 53653c352643751c4b62ff63ec5e555f11dae8eb | refs/heads/master | 1,610,945,684,489 | 1,429,681,220,000 | 1,429,681,449,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 228 | lean | import logic
axiom I : Type
definition F (X : Type) : Type := (X → Prop) → Prop
axiom unfold : I → F I
axiom foldd : F I → I
axiom iso1 : ∀x, foldd (unfold x) = x
theorem iso2 : ∀x, foldd (unfold x) = x
:= sorry
|
58a00814d36b8338f403266199c69b070f0e39c6 | a45212b1526d532e6e83c44ddca6a05795113ddc | /src/topology/algebra/group_completion.lean | 465a7cd7823aa8fe06ba643cc748539ba770c234 | [
"Apache-2.0"
] | permissive | fpvandoorn/mathlib | b21ab4068db079cbb8590b58fda9cc4bc1f35df4 | b3433a51ea8bc07c4159c1073838fc0ee9b8f227 | refs/heads/master | 1,624,791,089,608 | 1,556,715,231,000 | 1,556,715,231,000 | 165,722,980 | 5 | 0 | Apache-2.0 | 1,552,657,455,000 | 1,547,494,646,000 | Lean | UTF-8 | Lean | false | false | 5,554 | lean | /-
Copyright (c) 2018 Patrick Massot. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Patrick Massot, Johannes Hölzl
Completion of topological groups:
-/
import topology.uniform_space.completion topology.algebra.uniform_group
noncomputable theory
section group
open uniform_space Cauchy filter set
variables {α : Type*} [uniform_space α]
instance [has_zero α] : has_zero (completion α) := ⟨(0 : α)⟩
instance [has_neg α] : has_neg (completion α) := ⟨completion.map (λa, -a : α → α)⟩
instance [has_add α] : has_add (completion α) := ⟨completion.map₂ (+)⟩
lemma coe_zero [has_zero α] : ((0 : α) : completion α) = 0 := rfl
end group
namespace uniform_space.completion
section uniform_add_group
open uniform_space uniform_space.completion
variables {α : Type*} [uniform_space α] [add_group α] [uniform_add_group α]
lemma coe_neg (a : α) : ((- a : α) : completion α) = - a :=
(map_coe uniform_continuous_neg' a).symm
lemma coe_add (a b : α) : ((a + b : α) : completion α) = a + b :=
(map₂_coe_coe a b (+) uniform_continuous_add').symm
instance : add_group (completion α) :=
{ zero_add := assume a, completion.induction_on a
(is_closed_eq (continuous_map₂ continuous_const continuous_id) continuous_id)
(assume a, show 0 + (a : completion α) = a, by rw [← coe_zero, ← coe_add, zero_add]),
add_zero := assume a, completion.induction_on a
(is_closed_eq (continuous_map₂ continuous_id continuous_const) continuous_id)
(assume a, show (a : completion α) + 0 = a, by rw [← coe_zero, ← coe_add, add_zero]),
add_left_neg := assume a, completion.induction_on a
(is_closed_eq (continuous_map₂ completion.continuous_map continuous_id) continuous_const)
(assume a, show - (a : completion α) + a = 0, by rw [← coe_neg, ← coe_add, add_left_neg, coe_zero]),
add_assoc := assume a b c, completion.induction_on₃ a b c
(is_closed_eq
(continuous_map₂
(continuous_map₂ continuous_fst (continuous_snd.comp continuous_fst)) (continuous_snd.comp continuous_snd))
(continuous_map₂ continuous_fst
(continuous_map₂ (continuous_snd.comp continuous_fst) (continuous_snd.comp continuous_snd))))
(assume a b c, show (a : completion α) + b + c = a + (b + c),
by repeat { rw [← coe_add] }; rw [add_assoc]),
.. completion.has_zero, .. completion.has_neg, ..completion.has_add }
instance : uniform_add_group (completion α) :=
⟨ (uniform_continuous.prod_mk uniform_continuous_fst
(uniform_continuous_snd.comp uniform_continuous_map)).comp (uniform_continuous_map₂' (+)) ⟩
instance is_add_group_hom_coe : is_add_group_hom (coe : α → completion α) :=
⟨ coe_add ⟩
variables {β : Type*} [uniform_space β] [add_group β] [uniform_add_group β]
lemma is_add_group_hom_extension [complete_space β] [separated β]
{f : α → β} [is_add_group_hom f] (hf : continuous f) : is_add_group_hom (completion.extension f) :=
have hf : uniform_continuous f, from uniform_continuous_of_continuous hf,
⟨assume a b, completion.induction_on₂ a b
(is_closed_eq
(continuous_add'.comp continuous_extension)
(continuous_add (continuous_fst.comp continuous_extension) (continuous_snd.comp continuous_extension)))
(assume a b, by rw [← coe_add, extension_coe hf, extension_coe hf, extension_coe hf, is_add_group_hom.map_add f])⟩
lemma is_add_group_hom_map [add_group β] [uniform_add_group β]
{f : α → β} [is_add_group_hom f] (hf : continuous f) : is_add_group_hom (completion.map f) :=
is_add_group_hom_extension (hf.comp (continuous_coe _))
section instance_max_depth
-- TODO: continuous_add requires some long proofs through
-- uniform_add_group / topological_add_group w.r.t prod / completion etc
set_option class.instance_max_depth 52
lemma is_add_group_hom_prod [add_group β] [uniform_add_group β] :
is_add_group_hom (@completion.prod α β _ _) :=
⟨assume ⟨a₁, a₂⟩ ⟨b₁, b₂⟩,
begin
refine completion.induction_on₄ a₁ a₂ b₁ b₂ (is_closed_eq _ _) _,
{ refine continuous.comp _ uniform_continuous_prod.continuous,
refine continuous_add _ _,
exact continuous.prod_mk (continuous_fst.comp continuous_fst) (continuous_fst.comp continuous_snd),
exact continuous.prod_mk (continuous_snd.comp continuous_fst) (continuous_snd.comp continuous_snd) },
{ refine continuous_add _ _,
refine continuous.comp _ uniform_continuous_prod.continuous,
exact continuous.prod_mk (continuous_fst.comp continuous_fst) (continuous_fst.comp continuous_snd),
refine continuous.comp _ uniform_continuous_prod.continuous,
exact continuous.prod_mk (continuous_snd.comp continuous_fst) (continuous_snd.comp continuous_snd) },
{ assume a b c d,
show completion.prod (↑a + ↑c, ↑b + ↑d) = completion.prod (↑a, ↑b) + completion.prod (↑c, ↑d),
rw [← coe_add, ← coe_add, prod_coe_coe, prod_coe_coe, prod_coe_coe, ← coe_add],
refl }
end⟩
end instance_max_depth
instance {α : Type*} [uniform_space α] [add_comm_group α] [uniform_add_group α] : add_comm_group (completion α) :=
{ add_comm := assume a b, completion.induction_on₂ a b
(is_closed_eq (continuous_map₂ continuous_fst continuous_snd) (continuous_map₂ continuous_snd continuous_fst))
(assume x y, by { change ↑x + ↑y = ↑y + ↑x, rw [← coe_add, ← coe_add, add_comm]}),
.. completion.add_group }
end uniform_add_group
end uniform_space.completion
|
7da122c441c50843473da23ea6c8f55085e16266 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/category_theory/preadditive/left_exact.lean | d98a6fa81a43f1ffbbf94ad570a5bbb88729ce2f | [
"Apache-2.0"
] | permissive | alreadydone/mathlib | dc0be621c6c8208c581f5170a8216c5ba6721927 | c982179ec21091d3e102d8a5d9f5fe06c8fafb73 | refs/heads/master | 1,685,523,275,196 | 1,670,184,141,000 | 1,670,184,141,000 | 287,574,545 | 0 | 0 | Apache-2.0 | 1,670,290,714,000 | 1,597,421,623,000 | Lean | UTF-8 | Lean | false | false | 10,378 | lean | /-
Copyright (c) 2022 Jakob von Raumer. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Markus Himmel, Jakob von Raumer
-/
import category_theory.limits.constructions.finite_products_of_binary_products
import category_theory.limits.preserves.shapes.kernels
import category_theory.limits.constructions.limits_of_products_and_equalizers
import category_theory.preadditive.additive_functor
/-!
# Left exactness of functors between preadditive categories
We show that a functor is left exact in the sense that it preserves finite limits, if it
preserves kernels. The dual result holds for right exact functors and cokernels.
## Main results
* We first derive preservation of binary product in the lemma
`preserves_binary_products_of_preserves_kernels`,
* then show the preservation of equalizers in `preserves_equalizer_of_preserves_kernels`,
* and then derive the preservation of all finite limits with the usual construction.
-/
universes v₁ v₂ u₁ u₂
noncomputable theory
open category_theory
open category_theory.limits
open category_theory.preadditive
namespace category_theory
namespace functor
variables {C : Type u₁} [category.{v₁} C] [preadditive C]
{D : Type u₂} [category.{v₂} D] [preadditive D]
(F : C ⥤ D) [preserves_zero_morphisms F]
section finite_limits
/--
A functor between preadditive categories which preserves kernels preserves that an
arbitrary binary fan is a limit.
-/
def is_limit_map_cone_binary_fan_of_preserves_kernels {X Y Z : C} (π₁ : Z ⟶ X) (π₂ : Z ⟶ Y)
[preserves_limit (parallel_pair π₂ 0) F] (i : is_limit (binary_fan.mk π₁ π₂)) :
is_limit (F.map_cone (binary_fan.mk π₁ π₂)) :=
begin
let bc := binary_bicone.of_limit_cone i,
letI presf : preserves_limit (parallel_pair bc.snd 0) F, { simpa },
let hf : is_limit bc.snd_kernel_fork := binary_bicone.is_limit_snd_kernel_fork i,
exact (is_limit_map_cone_binary_fan_equiv F π₁ π₂).inv_fun
(binary_bicone.is_bilimit_of_kernel_inl (F.map_binary_bicone bc)
(is_limit_map_cone_fork_equiv' F _ (is_limit_of_preserves F hf))).is_limit
end
/-- A kernel preserving functor between preadditive categories preserves any pair being a limit. -/
def preserves_binary_product_of_preserves_kernels
[∀ {X Y} (f : X ⟶ Y), preserves_limit (parallel_pair f 0) F] {X Y : C} :
preserves_limit (pair X Y) F :=
{ preserves := λ c hc, is_limit.of_iso_limit
(is_limit_map_cone_binary_fan_of_preserves_kernels F _ _
(is_limit.of_iso_limit hc (iso_binary_fan_mk c)))
((cones.functoriality _ F).map_iso (iso_binary_fan_mk c).symm) }
local attribute [instance] preserves_binary_product_of_preserves_kernels
/-- A kernel preserving functor between preadditive categories preserves binary products. -/
def preserves_binary_products_of_preserves_kernels
[∀ {X Y} (f : X ⟶ Y), preserves_limit (parallel_pair f 0) F] :
preserves_limits_of_shape (discrete walking_pair) F :=
{ preserves_limit := λ p, preserves_limit_of_iso_diagram F (diagram_iso_pair p).symm }
local attribute [instance] preserves_binary_products_of_preserves_kernels
variables [has_binary_biproducts C]
/--
A functor between preadditive categories preserves the equalizer of two
morphisms if it preserves all kernels. -/
def preserves_equalizer_of_preserves_kernels
[∀ {X Y} (f : X ⟶ Y), preserves_limit (parallel_pair f 0) F] {X Y : C}
(f g : X ⟶ Y) : preserves_limit (parallel_pair f g) F :=
begin
letI := preserves_binary_biproducts_of_preserves_binary_products F,
haveI := additive_of_preserves_binary_biproducts F,
constructor, intros c i,
let c' := is_limit_kernel_fork_of_fork (i.of_iso_limit (fork.iso_fork_of_ι c)),
dsimp only [kernel_fork_of_fork_of_ι] at c',
let iFc := is_limit_fork_map_of_is_limit' F _ c',
apply is_limit.of_iso_limit _ ((cones.functoriality _ F).map_iso (fork.iso_fork_of_ι c).symm),
apply (is_limit_map_cone_fork_equiv F (fork.condition c)).inv_fun,
let p : parallel_pair (F.map (f - g)) 0 ≅ parallel_pair (F.map f - F.map g) 0 :=
parallel_pair.eq_of_hom_eq F.map_sub rfl,
refine is_limit.of_iso_limit (is_limit_fork_of_kernel_fork
((is_limit.postcompose_hom_equiv p _).symm iFc)) _,
convert fork.iso_fork_of_ι _,
rw [fork_of_kernel_fork_ι, fork.ι_postcompose, kernel_fork.ι_of_ι,
parallel_pair.eq_of_hom_eq_hom_app],
erw category.comp_id
end
/--
A functor between preadditive categories preserves all equalizers if it preserves all kernels.
-/
def preserves_equalizers_of_preserves_kernels
[∀ {X Y} (f : X ⟶ Y), preserves_limit (parallel_pair f 0) F] :
preserves_limits_of_shape walking_parallel_pair F :=
{ preserves_limit := λ K,
begin
letI := preserves_equalizer_of_preserves_kernels F
(K.map walking_parallel_pair_hom.left) (K.map walking_parallel_pair_hom.right),
apply preserves_limit_of_iso_diagram F (diagram_iso_parallel_pair K).symm,
end }
/--
A functor between preadditive categories which preserves kernels preserves all finite limits.
-/
def preserves_finite_limits_of_preserves_kernels
[has_finite_products C] [has_equalizers C] [has_zero_object C] [has_zero_object D]
[∀ {X Y} (f : X ⟶ Y), preserves_limit (parallel_pair f 0) F] : preserves_finite_limits F :=
begin
letI := preserves_equalizers_of_preserves_kernels F,
letI := preserves_terminal_object_of_preserves_zero_morphisms F,
letI := preserves_limits_of_shape_pempty_of_preserves_terminal F,
letI p_prod := preserves_finite_products_of_preserves_binary_and_terminal F,
apply @preserves_finite_limits_of_preserves_equalizers_and_finite_products _ _
_ _ _ _ _ _ @p_prod,
end
end finite_limits
section finite_colimits
/--
A functor between preadditive categories which preserves cokernels preserves finite coproducts.
-/
def is_colimit_map_cocone_binary_cofan_of_preserves_cokernels {X Y Z : C} (ι₁ : X ⟶ Z) (ι₂ : Y ⟶ Z)
[preserves_colimit (parallel_pair ι₂ 0) F] (i : is_colimit (binary_cofan.mk ι₁ ι₂)) :
is_colimit (F.map_cocone (binary_cofan.mk ι₁ ι₂)) :=
begin
let bc := binary_bicone.of_colimit_cocone i,
letI presf : preserves_colimit (parallel_pair bc.inr 0) F, { simpa },
let hf : is_colimit bc.inr_cokernel_cofork := binary_bicone.is_colimit_inr_cokernel_cofork i,
exact (is_colimit_map_cocone_binary_cofan_equiv F ι₁ ι₂).inv_fun
(binary_bicone.is_bilimit_of_cokernel_fst (F.map_binary_bicone bc)
(is_colimit_map_cocone_cofork_equiv' F _ (is_colimit_of_preserves F hf))).is_colimit
end
/-- A cokernel preserving functor between preadditive categories preserves any pair being
a colimit. -/
def preserves_coproduct_of_preserves_cokernels
[∀ {X Y} (f : X ⟶ Y), preserves_colimit (parallel_pair f 0) F] {X Y : C} :
preserves_colimit (pair X Y) F :=
{ preserves := λ c hc, is_colimit.of_iso_colimit
(is_colimit_map_cocone_binary_cofan_of_preserves_cokernels F _ _
(is_colimit.of_iso_colimit hc (iso_binary_cofan_mk c)))
((cocones.functoriality _ F).map_iso (iso_binary_cofan_mk c).symm) }
local attribute [instance] preserves_coproduct_of_preserves_cokernels
/-- A cokernel preserving functor between preadditive categories preserves binary coproducts. -/
def preserves_binary_coproducts_of_preserves_cokernels
[∀ {X Y} (f : X ⟶ Y), preserves_colimit (parallel_pair f 0) F] :
preserves_colimits_of_shape (discrete walking_pair) F :=
{ preserves_colimit := λ p, preserves_colimit_of_iso_diagram F (diagram_iso_pair p).symm }
local attribute [instance] preserves_binary_coproducts_of_preserves_cokernels
variables [has_binary_biproducts C]
/--
A functor between preadditive categoris preserves the coequalizer of two
morphisms if it preserves all cokernels. -/
def preserves_coequalizer_of_preserves_cokernels
[∀ {X Y} (f : X ⟶ Y), preserves_colimit (parallel_pair f 0) F] {X Y : C}
(f g : X ⟶ Y) : preserves_colimit (parallel_pair f g) F :=
begin
letI := preserves_binary_biproducts_of_preserves_binary_coproducts F,
haveI := additive_of_preserves_binary_biproducts F,
constructor, intros c i,
let c' := is_colimit_cokernel_cofork_of_cofork (i.of_iso_colimit (cofork.iso_cofork_of_π c)),
dsimp only [cokernel_cofork_of_cofork_of_π] at c',
let iFc := is_colimit_cofork_map_of_is_colimit' F _ c',
apply is_colimit.of_iso_colimit _
((cocones.functoriality _ F).map_iso (cofork.iso_cofork_of_π c).symm),
apply (is_colimit_map_cocone_cofork_equiv F (cofork.condition c)).inv_fun,
let p : parallel_pair (F.map (f - g)) 0 ≅ parallel_pair (F.map f - F.map g) 0,
{ exact parallel_pair.ext (iso.refl _) (iso.refl _) (by simp) (by simp) },
refine is_colimit.of_iso_colimit (is_colimit_cofork_of_cokernel_cofork
((is_colimit.precompose_hom_equiv p.symm _).symm iFc)) _,
convert cofork.iso_cofork_of_π _,
rw [cofork_of_cokernel_cofork_π, cofork.π_precompose, cokernel_cofork.π_of_π,
iso.symm_hom, parallel_pair.ext_inv_app, iso.refl_inv],
erw category.id_comp
end
/--
A functor between preadditive categories preserves all coequalizers if it preserves all kernels.
-/
def preserves_coequalizers_of_preserves_cokernels
[∀ {X Y} (f : X ⟶ Y), preserves_colimit (parallel_pair f 0) F] :
preserves_colimits_of_shape walking_parallel_pair F :=
{ preserves_colimit := λ K,
begin
letI := preserves_coequalizer_of_preserves_cokernels F
(K.map limits.walking_parallel_pair_hom.left)
(K.map limits.walking_parallel_pair_hom.right),
apply preserves_colimit_of_iso_diagram F (diagram_iso_parallel_pair K).symm,
end }
/--
A functor between preadditive categories which preserves kernels preserves all finite limits.
-/
def preserves_finite_colimits_of_preserves_cokernels
[has_finite_coproducts C] [has_coequalizers C] [has_zero_object C] [has_zero_object D]
[∀ {X Y} (f : X ⟶ Y), preserves_colimit (parallel_pair f 0) F] : preserves_finite_colimits F :=
begin
letI := preserves_coequalizers_of_preserves_cokernels F,
letI := preserves_initial_object_of_preserves_zero_morphisms F,
letI := preserves_colimits_of_shape_pempty_of_preserves_initial F,
letI p_prod := preserves_finite_coproducts_of_preserves_binary_and_initial F,
apply @preserves_finite_colimits_of_preserves_coequalizers_and_finite_coproducts C _
_ _ _ _ _ _ @p_prod,
end
end finite_colimits
end functor
end category_theory
|
d948a0ae071d299975479a20afe2b771602bf015 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/run/rflProofsCongrCastsIssue.lean | 8499da213d5f0130ace9ba83e29a4ed944a303a2 | [
"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 | 117 | lean | example (h₁: m < n) (h₂: m.succ.pred < n) :
(Fin.mk m h₁).succ = (Fin.mk m.succ.pred h₂).succ := by
simp
|
ffa00523c8f663cc554e007e61231c0ab430dd82 | cf39355caa609c0f33405126beee2739aa3cb77e | /tests/lean/run/struct_inst_exprs2.lean | 15d123f48f9560884548c584ba2fdc704d716e03 | [
"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 | 573 | lean | #exit
-- import data.nat.basic
open nat
structure s1 (A : Type) :=
(x : A) (y : A) (h : x = y)
structure s2 (A : Type) :=
(mul : A → A → A) (one : A)
structure s3 (A : Type) extends s1 A, s2 A :=
(mul_one : ∀ a : A, mul a one = a)
definition v1 : s1 nat := {| s1, x := 10, y := 10, h := rfl |}
definition v2 : s2 nat := {| s2, mul := nat.add, one := zero |}
definition v3 : s3 nat := {| s3, mul_one := nat.add_zero, v1, v2 |}
example : s3.x v3 = 10 := rfl
example : s3.y v3 = 10 := rfl
example : s3.mul v3 = nat.add := rfl
example : s3.one v3 = nat.zero := rfl
|
2f679004950e473ca50686f0cd96b7f4e9c87883 | 5ae26df177f810c5006841e9c73dc56e01b978d7 | /src/topology/uniform_space/pi.lean | 130fbe9a4bf829ea32e2bee4c9d3343b7f098148 | [
"Apache-2.0"
] | permissive | ChrisHughes24/mathlib | 98322577c460bc6b1fe5c21f42ce33ad1c3e5558 | a2a867e827c2a6702beb9efc2b9282bd801d5f9a | refs/heads/master | 1,583,848,251,477 | 1,565,164,247,000 | 1,565,164,247,000 | 129,409,993 | 0 | 1 | Apache-2.0 | 1,565,164,817,000 | 1,523,628,059,000 | Lean | UTF-8 | Lean | false | false | 1,977 | lean | /-
Copyright (c) 2019 Patrick Massot. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Patrick Massot
Indexed product of uniform spaces
-/
import topology.uniform_space.cauchy
import topology.uniform_space.separation
noncomputable theory
local notation `𝓤` := uniformity
section
open filter lattice uniform_space
universe u
variables {ι : Type*} (α : ι → Type u) [U : Πi, uniform_space (α i)]
include U
instance Pi.uniform_space : uniform_space (Πi, α i) :=
uniform_space.of_core_eq
(⨅i, uniform_space.comap (λ a : Πi, α i, a i) (U i)).to_core
Pi.topological_space $ eq.symm to_topological_space_infi
lemma Pi.uniformity :
𝓤 (Π i, α i) = ⨅ i : ι, filter.comap (λ a, (a.1 i, a.2 i)) $ 𝓤 (α i) :=
infi_uniformity
lemma Pi.uniform_continuous_proj (i : ι) : uniform_continuous (λ (a : Π (i : ι), α i), a i) :=
begin
rw uniform_continuous_iff,
exact infi_le (λ j, uniform_space.comap (λ (a : Π (i : ι), α i), a j) (U j)) i
end
lemma Pi.uniform_space_topology :
(Pi.uniform_space α).to_topological_space = Pi.topological_space := rfl
instance Pi.complete [∀ i, complete_space (α i)] : complete_space (Π i, α i) :=
⟨begin
intros f hf,
have : ∀ i, ∃ x : α i, filter.map (λ a : Πi, α i, a i) f ≤ nhds x,
{ intro i,
have key : cauchy (map (λ (a : Π (i : ι), α i), a i) f),
from cauchy_map (Pi.uniform_continuous_proj α i) hf,
exact (cauchy_iff_exists_le_nhds $ map_ne_bot hf.1).1 key },
choose x hx using this,
use x,
rw [show nhds x = (⨅i, comap (λa, a i) (nhds (x i))),
by rw Pi.uniform_space_topology ; exact nhds_pi,
le_infi_iff],
exact λ i, map_le_iff_le_comap.mp (hx i),
end⟩
instance Pi.separated [∀ i, separated (α i)] : separated (Π i, α i) :=
separated_def.2 $ assume x y H,
begin
ext i,
apply eq_of_separated_of_uniform_continuous (Pi.uniform_continuous_proj α i),
apply H,
end
end
|
b38fa7e0fb4d81251248115b04bc6fc934aac68d | 367134ba5a65885e863bdc4507601606690974c1 | /src/category_theory/monad/kleisli.lean | 2561841e945f3886055a63f8ce9c747dece8c6cf | [
"Apache-2.0"
] | permissive | kodyvajjha/mathlib | 9bead00e90f68269a313f45f5561766cfd8d5cad | b98af5dd79e13a38d84438b850a2e8858ec21284 | refs/heads/master | 1,624,350,366,310 | 1,615,563,062,000 | 1,615,563,062,000 | 162,666,963 | 0 | 0 | Apache-2.0 | 1,545,367,651,000 | 1,545,367,651,000 | null | UTF-8 | Lean | false | false | 3,093 | lean | /-
Copyright (c) 2020 Wojciech Nawrocki. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Wojciech Nawrocki, Bhavik Mehta
-/
import category_theory.adjunction
import category_theory.monad.adjunction
import category_theory.monad.basic
/-! # Kleisli category on a monad
This file defines the Kleisli category on a monad `(T, η_ T, μ_ T)`. It also defines the Kleisli
adjunction which gives rise to the monad `(T, η_ T, μ_ T)`.
## References
* [Riehl, *Category theory in context*, Definition 5.2.9][riehl2017]
-/
namespace category_theory
universes v u -- declare the `v`'s first; see `category_theory.category` for an explanation
variables {C : Type u} [category.{v} C]
/--
The objects for the Kleisli category of the functor (usually monad) `T : C ⥤ C`, which are the same
thing as objects of the base category `C`.
-/
@[nolint unused_arguments]
def kleisli (T : monad C) := C
namespace kleisli
variables (T : monad C)
instance [inhabited C] (T : monad C) : inhabited (kleisli T) := ⟨(default _ : C)⟩
/-- The Kleisli category on a monad `T`.
cf Definition 5.2.9 in [Riehl][riehl2017]. -/
instance kleisli.category : category (kleisli T) :=
{ hom := λ (X Y : C), X ⟶ (T : C ⥤ C).obj Y,
id := λ X, T.η.app X,
comp := λ X Y Z f g, f ≫ (T : C ⥤ C).map g ≫ T.μ.app Z,
id_comp' := λ X Y f,
begin
rw [← T.η.naturality_assoc f, T.left_unit],
apply category.comp_id,
end,
assoc' := λ W X Y Z f g h,
begin
simp only [functor.map_comp, category.assoc, monad.assoc],
erw T.μ.naturality_assoc,
end }
namespace adjunction
/-- The left adjoint of the adjunction which induces the monad `(T, η_ T, μ_ T)`. -/
@[simps] def to_kleisli : C ⥤ kleisli T :=
{ obj := λ X, (X : kleisli T),
map := λ X Y f, (f ≫ T.η.app Y : _),
map_comp' := λ X Y Z f g, by { unfold_projs, simp [← T.η.naturality g] } }
/-- The right adjoint of the adjunction which induces the monad `(T, η_ T, μ_ T)`. -/
@[simps] def from_kleisli : kleisli T ⥤ C :=
{ obj := λ X, T.obj X,
map := λ X Y f, T.map f ≫ T.μ.app Y,
map_id' := λ X, T.right_unit _,
map_comp' := λ X Y Z f g,
begin
unfold_projs,
simp only [functor.map_comp, category.assoc],
erw [← T.μ.naturality_assoc g, T.assoc],
refl,
end }
/-- The Kleisli adjunction which gives rise to the monad `(T, η_ T, μ_ T)`.
cf Lemma 5.2.11 of [Riehl][riehl2017]. -/
def adj : to_kleisli T ⊣ from_kleisli T :=
adjunction.mk_of_hom_equiv
{ hom_equiv := λ X Y, equiv.refl (X ⟶ T.obj Y),
hom_equiv_naturality_left_symm' := λ X Y Z f g,
begin
unfold_projs,
dsimp,
rw [category.assoc, ← T.η.naturality_assoc g, functor.id_map],
dsimp,
simp [monad.left_unit],
end }
/-- The composition of the adjunction gives the original functor. -/
def to_kleisli_comp_from_kleisli_iso_self : to_kleisli T ⋙ from_kleisli T ≅ T :=
nat_iso.of_components (λ X, iso.refl _) (λ X Y f, by { dsimp, simp })
end adjunction
end kleisli
end category_theory
|
d96d17dbd46d66b0da0ad7da8207b347047c57fe | df561f413cfe0a88b1056655515399c546ff32a5 | /3-multiplication-world/l2.lean | 534d2d7630ae007d29beda2ec06e9fbd583ab11b | [] | no_license | nicholaspun/natural-number-game-solutions | 31d5158415c6f582694680044c5c6469032c2a06 | 1e2aed86d2e76a3f4a275c6d99e795ad30cf6df0 | refs/heads/main | 1,675,123,625,012 | 1,607,633,548,000 | 1,607,633,548,000 | 318,933,860 | 3 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 116 | lean | lemma mul_one (m : mynat) : m * 1 = m :=
begin
rw one_eq_succ_zero,
rw mul_succ,
rw mul_zero,
rw zero_add,
refl,
end |
8a19dc373d7f90063012ef42fe39eeffa99437af | d4df7c538bbc0b06771d7b7a6f46fbadebb8c5eb | /src/parser.lean | ac66bc56bb9b0e011162fae53dd7c7bd6ae78b0b | [] | no_license | breakerzirconia/LeanTransformers | 11bb924b59ac589d0c6b39586d27cccc8f9e939c | 3c52441b8990bab7d8cf1817df6e04f657a14e1c | refs/heads/main | 1,682,610,526,027 | 1,620,477,129,000 | 1,620,477,129,000 | 365,507,838 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,859 | lean | ----------------------------
-- Definition + instances --
----------------------------
structure Parser (s a : Type) : Type := (run : list s → option (a × list s))
open Parser
instance Parser_functor {s : Type} : functor (Parser s) := {
map := λ α β f p,
mk (λ l,
match p.run l with
| none := none
| some ⟨a, lst⟩ := some ⟨f a, lst⟩
end)
}
instance Parser_has_pure {s : Type} : has_pure (Parser s) := {
pure := λ α a, mk (λ lst, some ⟨a, lst⟩)
}
instance Parser_has_seq {s : Type} : has_seq (Parser s) := {
seq := λ α β pab pa,
mk (λ l,
match pab.run l with
| none := none
| some ⟨ab, l'⟩ := match pa.run l' with
| none := none
| some ⟨a, l''⟩ := some ⟨ab a, l''⟩
end
end)
}
instance Parser_applicative {s : Type} : applicative (Parser s) := {}
instance Parser_has_bind {s : Type} : has_bind (Parser s) := {
bind := λ α β p f, mk (λ l, match p.run l with
| none := none
| some ⟨a, l'⟩ := (f a).run l'
end)
}
instance Parser_monad {s : Type} : monad (Parser s) := {}
instance Parser_has_orelse {s : Type} : has_orelse (Parser s) := {
orelse := λ α p q, mk (λ l, match p.run l with
| none := q.run l
| some ⟨a, l'⟩ := some ⟨a, l'⟩
end)
}
instance Parser_alternative {s : Type} : alternative (Parser s) := {
failure := λ α, mk (λ l, none)
}
---------------------
-- Extra functions --
---------------------
def evalParser {s a : Type} : Parser s a → list s → option a :=
λ p l, prod.fst <$> p.run l
def execParser {s a : Type} : Parser s a → list s → option (list s) :=
λ p l, prod.snd <$> p.run l
-----------------------
-- Basic combinators --
-----------------------
def ok {s : Type} : Parser s unit := pure ()
def eof {s : Type} : Parser s unit :=
mk $ λ l, match l with
| [] := some ⟨(), []⟩
| _ := none
end
def satisfy {s : Type} (predicate : s → bool) : Parser s s :=
mk $ λ l, match l with
| [] := none
| (x :: xs) := cond (predicate x) (some ⟨x, xs⟩) none
-- if ... then ... else ... works too
end
#eval (satisfy (λ (n : nat), n = 0) >> satisfy (λ (n : nat), n = 0)).run ([0, 0])
def element {s : Type} [decidable_eq s] (c : s) : Parser s s := satisfy (λ d, d = c)
def stream {s : Type} [decidable_eq s] : list s → Parser s (list s)
| [] := pure []
| (x :: xs) := do x' ← element x , xs' ← stream xs, pure (x' :: xs')
#eval (stream "Enter".data).run "Enter Shikari".data
|
8416ea325e63d6978979674334e0d2786adce193 | aa3f8992ef7806974bc1ffd468baa0c79f4d6643 | /tests/lean/run/trans.lean | 8c6a02cd7ae0c0e271065e28283cd29b66a3fc92 | [
"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,561 | lean | -- Copyright (c) 2014 Microsoft Corporation. All rights reserved.
-- Released under Apache 2.0 license as described in the file LICENSE.
-- Author: Leonardo de Moura
import logic
open eq
definition refl := @eq.refl
definition transport {A : Type} {a b : A} {P : A → Type} (p : a = b) (H : P a) : P b
:= eq.rec H p
theorem transport_refl {A : Type} {a : A} {P : A → Type} (H : P a) : transport (refl a) H = H
:= refl H
reducible [off] transport
theorem transport_proof_irrel {A : Type} {a b : A} {P : A → Type} (p1 p2 : a = b) (H : P a) : transport p1 H = transport p2 H
:= refl (transport p1 H)
theorem transport_eq {A : Type} {a : A} {P : A → Type} (p : a = a) (H : P a) : transport p H = H
:= calc transport p H = transport (refl a) H : transport_proof_irrel p (refl a) H
... = H : transport_refl H
theorem dcongr {A : Type} {B : A → Type} {a b : A} (f : Π x, B x) (p : a = b) : transport p (f a) = f b
:= have H1 : ∀ p1 : a = a, transport p1 (f a) = f a, from
assume p1 : a = a, transport_eq p1 (f a),
eq.rec H1 p p
theorem transport_trans {A : Type} {a b c : A} {P : A → Type} (p1 : a = b) (p2 : b = c) (H : P a) :
transport p1 (transport p2 H) = transport (trans p1 p2) H
:= have H1 : ∀ p, transport p1 (transport p H) = transport (trans p1 p) H, from
take p, calc transport p1 (transport p H) = transport p1 H : {transport_eq p H}
... = transport (trans p1 p) H : refl (transport p1 H),
eq.rec H1 p2 p2
|
8fbbeb1b1a050ef68b1129515c5be62371cadc3d | 57fdc8de88f5ea3bfde4325e6ecd13f93a274ab5 | /data/option.lean | daedb3345beebe3299966cdacb7852da35a2e66b | [
"Apache-2.0"
] | permissive | louisanu/mathlib | 11f56f2d40dc792bc05ee2f78ea37d73e98ecbfe | 2bd5e2159d20a8f20d04fc4d382e65eea775ed39 | refs/heads/master | 1,617,706,993,439 | 1,523,163,654,000 | 1,523,163,654,000 | 124,519,997 | 0 | 0 | Apache-2.0 | 1,520,588,283,000 | 1,520,588,283,000 | null | UTF-8 | Lean | false | false | 2,298 | lean | /-
Copyright (c) 2017 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import logic.basic data.bool init.data.option.instances
namespace option
universe u
variables {α β : Type u}
instance has_mem : has_mem α (option α) := ⟨λ a b, b = some a⟩
@[simp] lemma mem_def {a : α} {b : option α} : a ∈ b ↔ b = some a :=
iff.rfl
lemma some_inj {a b : α} : some a = some b ↔ a = b := by simp
@[simp] lemma none_bind (f : α → option β) : none >>= f = none := rfl
@[simp] lemma some_bind (a : α) (f : α → option β) : some a >>= f = f a := rfl
@[simp] lemma bind_some : ∀ x : option α, x >>= some = x :=
@bind_pure α option _ _
@[simp] lemma bind_eq_some {x : option α} {f : α → option β} {b : β} : x >>= f = some b ↔ ∃ a, x = some a ∧ f a = some b :=
by cases x; simp
@[simp] lemma map_none {f : α → β} : f <$> none = none := rfl
@[simp] lemma map_some {a : α} {f : α → β} : f <$> some a = some (f a) := rfl
@[simp] lemma map_eq_some {x : option α} {f : α → β} {b : β} : f <$> x = some b ↔ ∃ a, x = some a ∧ f a = b :=
by cases x; simp
@[simp] lemma map_id' : option.map (@id α) = id := map_id
@[simp] lemma seq_some {a : α} {f : α → β} : some f <*> some a = some (f a) := rfl
lemma is_some_iff_exists {x : option α} : is_some x ↔ ∃ a, x = some a :=
by cases x; simp [is_some]; exact ⟨_, rfl⟩
/-- inhabited `get` function. Returns `a` if the input is `some a`,
otherwise returns `default`. -/
@[reducible] def iget [inhabited α] : option α → α
| (some x) := x
| none := default α
/-- `guard p a` returns `some a` if `p a` holds, otherwise `none`. -/
def guard (p : α → Prop) [decidable_pred p] (a : α) : option α :=
if p a then some a else none
/-- `filter p o` returns `some a` if `o` is `some a`
and `p a` holds, otherwise `none`. -/
def filter (p : α → Prop) [decidable_pred p] (o : option α) : option α :=
o.bind (guard p)
@[simp] lemma guard_eq_some {p : α → Prop} [decidable_pred p] {a b : α} :
option.guard p a = some b ↔ a = b ∧ p a :=
by by_cases p a; simp [option.guard, h]; intro; contradiction
def to_list {α} : option α → list α
| none := []
| (some a) := [a]
end option
|
42f2e02b299911cf6362e17e3a0d3b03f75287e0 | 94e33a31faa76775069b071adea97e86e218a8ee | /src/group_theory/index.lean | c83b097b10af77afe1bd4fddef84ca3038a98741 | [
"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 | 11,593 | lean | /-
Copyright (c) 2021 Thomas Browning. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Thomas Browning
-/
import group_theory.quotient_group
import set_theory.cardinal.finite
/-!
# Index of a Subgroup
In this file we define the index of a subgroup, and prove several divisibility properties.
Several theorems proved in this file are known as Lagrange's theorem.
## Main definitions
- `H.index` : the index of `H : subgroup G` as a natural number,
and returns 0 if the index is infinite.
- `H.relindex K` : the relative index of `H : subgroup G` in `K : subgroup G` as a natural number,
and returns 0 if the relative index is infinite.
# Main results
- `card_mul_index` : `nat.card H * H.index = nat.card G`
- `index_mul_card` : `H.index * fintype.card H = fintype.card G`
- `index_dvd_card` : `H.index ∣ fintype.card G`
- `index_eq_mul_of_le` : If `H ≤ K`, then `H.index = K.index * (H.subgroup_of K).index`
- `index_dvd_of_le` : If `H ≤ K`, then `K.index ∣ H.index`
- `relindex_mul_relindex` : `relindex` is multiplicative in towers
-/
namespace subgroup
open_locale cardinal
variables {G : Type*} [group G] (H K L : subgroup G)
/-- The index of a subgroup as a natural number, and returns 0 if the index is infinite. -/
@[to_additive "The index of a subgroup as a natural number,
and returns 0 if the index is infinite."]
noncomputable def index : ℕ :=
nat.card (G ⧸ H)
/-- The relative index of a subgroup as a natural number,
and returns 0 if the relative index is infinite. -/
@[to_additive "The relative index of a subgroup as a natural number,
and returns 0 if the relative index is infinite."]
noncomputable def relindex : ℕ :=
(H.subgroup_of K).index
@[to_additive] lemma index_comap_of_surjective {G' : Type*} [group G'] {f : G' →* G}
(hf : function.surjective f) : (H.comap f).index = H.index :=
begin
letI := quotient_group.left_rel H,
letI := quotient_group.left_rel (H.comap f),
have key : ∀ x y : G', setoid.r x y ↔ setoid.r (f x) (f y),
{ simp only [quotient_group.left_rel_apply],
exact λ x y, iff_of_eq (congr_arg (∈ H) (by rw [f.map_mul, f.map_inv])) },
refine cardinal.to_nat_congr (equiv.of_bijective (quotient.map' f (λ x y, (key x y).mp)) ⟨_, _⟩),
{ simp_rw [←quotient.eq'] at key,
refine quotient.ind' (λ x, _),
refine quotient.ind' (λ y, _),
exact (key x y).mpr },
{ refine quotient.ind' (λ x, _),
obtain ⟨y, hy⟩ := hf x,
exact ⟨y, (quotient.map'_mk' f _ y).trans (congr_arg quotient.mk' hy)⟩ },
end
@[to_additive] lemma index_comap {G' : Type*} [group G'] (f : G' →* G) :
(H.comap f).index = H.relindex f.range :=
eq.trans (congr_arg index (by refl))
((H.subgroup_of f.range).index_comap_of_surjective f.range_restrict_surjective)
variables {H K L}
@[to_additive relindex_mul_index] lemma relindex_mul_index (h : H ≤ K) :
H.relindex K * K.index = H.index :=
((mul_comm _ _).trans (cardinal.to_nat_mul _ _).symm).trans
(congr_arg cardinal.to_nat (equiv.cardinal_eq (quotient_equiv_prod_of_le h))).symm
@[to_additive] lemma index_dvd_of_le (h : H ≤ K) : K.index ∣ H.index :=
dvd_of_mul_left_eq (H.relindex K) (relindex_mul_index h)
@[to_additive] lemma relindex_dvd_index_of_le (h : H ≤ K) : H.relindex K ∣ H.index :=
dvd_of_mul_right_eq K.index (relindex_mul_index h)
@[to_additive] lemma relindex_subgroup_of (hKL : K ≤ L) :
(H.subgroup_of L).relindex (K.subgroup_of L) = H.relindex K :=
((index_comap (H.subgroup_of L) (inclusion hKL)).trans (congr_arg _ (inclusion_range hKL))).symm
variables (H K L)
@[to_additive relindex_mul_relindex] lemma relindex_mul_relindex (hHK : H ≤ K) (hKL : K ≤ L) :
H.relindex K * K.relindex L = H.relindex L :=
begin
rw [←relindex_subgroup_of hKL],
exact relindex_mul_index (λ x hx, hHK hx),
end
@[to_additive] lemma inf_relindex_right : (H ⊓ K).relindex K = H.relindex K :=
begin
rw [←subgroup_of_map_subtype, relindex, relindex, subgroup_of, comap_map_eq_self_of_injective],
exact subtype.coe_injective,
end
@[to_additive] lemma inf_relindex_left : (H ⊓ K).relindex H = K.relindex H :=
by rw [inf_comm, inf_relindex_right]
@[to_additive relindex_inf_mul_relindex]
lemma relindex_inf_mul_relindex : H.relindex (K ⊓ L) * K.relindex L = (H ⊓ K).relindex L :=
by rw [←inf_relindex_right H (K ⊓ L), ←inf_relindex_right K L, ←inf_relindex_right (H ⊓ K) L,
inf_assoc, relindex_mul_relindex (H ⊓ (K ⊓ L)) (K ⊓ L) L inf_le_right inf_le_right]
@[to_additive]
lemma inf_relindex_eq_relindex_sup [K.normal] : (H ⊓ K).relindex H = K.relindex (H ⊔ K) :=
cardinal.to_nat_congr (quotient_group.quotient_inf_equiv_prod_normal_quotient H K).to_equiv
@[to_additive] lemma relindex_eq_relindex_sup [K.normal] : K.relindex H = K.relindex (H ⊔ K) :=
by rw [←inf_relindex_left, inf_relindex_eq_relindex_sup]
@[to_additive] lemma relindex_dvd_index_of_normal [H.normal] : H.relindex K ∣ H.index :=
(relindex_eq_relindex_sup K H).symm ▸ relindex_dvd_index_of_le le_sup_right
variables {H K}
@[to_additive] lemma relindex_dvd_of_le_left (hHK : H ≤ K) : K.relindex L ∣ H.relindex L :=
begin
apply dvd_of_mul_left_eq ((H ⊓ L).relindex (K ⊓ L)),
rw [←inf_relindex_right H L, ←inf_relindex_right K L],
exact relindex_mul_relindex (H ⊓ L) (K ⊓ L) L (inf_le_inf_right L hHK) inf_le_right,
end
variables (H K)
@[simp, to_additive] lemma index_top : (⊤ : subgroup G).index = 1 :=
cardinal.to_nat_eq_one_iff_unique.mpr ⟨quotient_group.subsingleton_quotient_top, ⟨1⟩⟩
@[simp, to_additive] lemma index_bot : (⊥ : subgroup G).index = nat.card G :=
cardinal.to_nat_congr (quotient_group.quotient_bot.to_equiv)
@[to_additive] lemma index_bot_eq_card [fintype G] : (⊥ : subgroup G).index = fintype.card G :=
index_bot.trans nat.card_eq_fintype_card
@[simp, to_additive] lemma relindex_top_left : (⊤ : subgroup G).relindex H = 1 :=
index_top
@[simp, to_additive] lemma relindex_top_right : H.relindex ⊤ = H.index :=
by rw [←relindex_mul_index (show H ≤ ⊤, from le_top), index_top, mul_one]
@[simp, to_additive] lemma relindex_bot_left : (⊥ : subgroup G).relindex H = nat.card H :=
by rw [relindex, bot_subgroup_of, index_bot]
@[to_additive] lemma relindex_bot_left_eq_card [fintype H] :
(⊥ : subgroup G).relindex H = fintype.card H :=
H.relindex_bot_left.trans nat.card_eq_fintype_card
@[simp, to_additive] lemma relindex_bot_right : H.relindex ⊥ = 1 :=
by rw [relindex, subgroup_of_bot_eq_top, index_top]
@[simp, to_additive] lemma relindex_self : H.relindex H = 1 :=
by rw [relindex, subgroup_of_self, index_top]
@[simp, to_additive card_mul_index]
lemma card_mul_index : nat.card H * H.index = nat.card G :=
by { rw [←relindex_bot_left, ←index_bot], exact relindex_mul_index bot_le }
@[to_additive] lemma index_map {G' : Type*} [group G'] (f : G →* G') :
(H.map f).index = (H ⊔ f.ker).index * f.range.index :=
by rw [←comap_map_eq, index_comap, relindex_mul_index (H.map_le_range f)]
@[to_additive] lemma index_map_dvd {G' : Type*} [group G'] {f : G →* G'}
(hf : function.surjective f) : (H.map f).index ∣ H.index :=
begin
rw [index_map, f.range_top_of_surjective hf, index_top, mul_one],
exact index_dvd_of_le le_sup_left,
end
@[to_additive] lemma dvd_index_map {G' : Type*} [group G'] {f : G →* G'}
(hf : f.ker ≤ H) : H.index ∣ (H.map f).index :=
begin
rw [index_map, sup_of_le_left hf],
apply dvd_mul_right,
end
@[to_additive] lemma index_map_eq {G' : Type*} [group G'] {f : G →* G'}
(hf1 : function.surjective f) (hf2 : f.ker ≤ H) : (H.map f).index = H.index :=
nat.dvd_antisymm (H.index_map_dvd hf1) (H.dvd_index_map hf2)
@[to_additive] lemma index_eq_card [fintype (G ⧸ H)] :
H.index = fintype.card (G ⧸ H) :=
nat.card_eq_fintype_card
@[to_additive index_mul_card] lemma index_mul_card [fintype G] [hH : fintype H] :
H.index * fintype.card H = fintype.card G :=
by rw [←relindex_bot_left_eq_card, ←index_bot_eq_card, mul_comm]; exact relindex_mul_index bot_le
@[to_additive] lemma index_dvd_card [fintype G] : H.index ∣ fintype.card G :=
begin
classical,
exact ⟨fintype.card H, H.index_mul_card.symm⟩,
end
variables {H K L}
@[to_additive]
lemma relindex_eq_zero_of_le_left (hHK : H ≤ K) (hKL : K.relindex L = 0) : H.relindex L = 0 :=
eq_zero_of_zero_dvd (hKL ▸ (relindex_dvd_of_le_left L hHK))
@[to_additive]
lemma relindex_eq_zero_of_le_right (hKL : K ≤ L) (hHK : H.relindex K = 0) : H.relindex L = 0 :=
cardinal.to_nat_apply_of_aleph_0_le (le_trans (le_of_not_lt (λ h, cardinal.mk_ne_zero _
((cardinal.cast_to_nat_of_lt_aleph_0 h).symm.trans (cardinal.nat_cast_inj.mpr hHK))))
(quotient_subgroup_of_embedding_of_le H hKL).cardinal_le)
@[to_additive] lemma relindex_le_of_le_left (hHK : H ≤ K) (hHL : H.relindex L ≠ 0) :
K.relindex L ≤ H.relindex L :=
nat.le_of_dvd (nat.pos_of_ne_zero hHL) (relindex_dvd_of_le_left L hHK)
@[to_additive] lemma relindex_le_of_le_right (hKL : K ≤ L) (hHL : H.relindex L ≠ 0) :
H.relindex K ≤ H.relindex L :=
cardinal.to_nat_le_of_le_of_lt_aleph_0 (lt_of_not_ge (mt cardinal.to_nat_apply_of_aleph_0_le hHL))
(cardinal.mk_le_of_injective (quotient_subgroup_of_embedding_of_le H hKL).2)
@[to_additive] lemma relindex_ne_zero_trans (hHK : H.relindex K ≠ 0) (hKL : K.relindex L ≠ 0) :
H.relindex L ≠ 0 :=
λ h, mul_ne_zero (mt (relindex_eq_zero_of_le_right (show K ⊓ L ≤ K, from inf_le_left)) hHK) hKL
((relindex_inf_mul_relindex H K L).trans (relindex_eq_zero_of_le_left inf_le_left h))
@[to_additive] lemma relindex_inf_ne_zero (hH : H.relindex L ≠ 0) (hK : K.relindex L ≠ 0) :
(H ⊓ K).relindex L ≠ 0 :=
begin
replace hH : H.relindex (K ⊓ L) ≠ 0 := mt (relindex_eq_zero_of_le_right inf_le_right) hH,
rw ← inf_relindex_right at hH hK ⊢,
rw inf_assoc,
exact relindex_ne_zero_trans hH hK,
end
@[to_additive] lemma index_inf_ne_zero (hH : H.index ≠ 0) (hK : K.index ≠ 0) : (H ⊓ K).index ≠ 0 :=
begin
rw ← relindex_top_right at hH hK ⊢,
exact relindex_inf_ne_zero hH hK,
end
@[to_additive] lemma relindex_inf_le : (H ⊓ K).relindex L ≤ H.relindex L * K.relindex L :=
begin
by_cases h : H.relindex L = 0,
{ exact (le_of_eq (relindex_eq_zero_of_le_left (by exact inf_le_left) h)).trans (zero_le _) },
rw [←inf_relindex_right, inf_assoc, ←relindex_mul_relindex _ _ L inf_le_right inf_le_right,
inf_relindex_right, inf_relindex_right],
exact mul_le_mul_right' (relindex_le_of_le_right inf_le_right h) (K.relindex L),
end
@[to_additive] lemma index_inf_le : (H ⊓ K).index ≤ H.index * K.index :=
by simp_rw [←relindex_top_right, relindex_inf_le]
@[simp, to_additive index_eq_one] lemma index_eq_one : H.index = 1 ↔ H = ⊤ :=
⟨λ h, quotient_group.subgroup_eq_top_of_subsingleton H (cardinal.to_nat_eq_one_iff_unique.mp h).1,
λ h, (congr_arg index h).trans index_top⟩
@[to_additive] lemma index_ne_zero_of_fintype [hH : fintype (G ⧸ H)] : H.index ≠ 0 :=
by { rw index_eq_card, exact fintype.card_ne_zero }
/-- Finite index implies finite quotient. -/
@[to_additive "Finite index implies finite quotient."]
noncomputable def fintype_of_index_ne_zero (hH : H.index ≠ 0) : fintype (G ⧸ H) :=
(cardinal.lt_aleph_0_iff_fintype.mp (lt_of_not_ge (mt cardinal.to_nat_apply_of_aleph_0_le hH))).some
@[to_additive one_lt_index_of_ne_top]
lemma one_lt_index_of_ne_top [fintype (G ⧸ H)] (hH : H ≠ ⊤) : 1 < H.index :=
nat.one_lt_iff_ne_zero_and_ne_one.mpr ⟨index_ne_zero_of_fintype, mt index_eq_one.mp hH⟩
end subgroup
|
cd7ca43d393d1a35eb03f92a903167b6a4d9ca05 | cf39355caa609c0f33405126beee2739aa3cb77e | /library/init/control/default.lean | cde32ca680bad2a26825ef406ccba0ca76169f68 | [
"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 | 423 | 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.control.applicative init.control.functor init.control.alternative
import init.control.monad init.control.lift init.control.lawful
import init.control.state init.control.id init.control.except init.control.reader
import init.control.option
|
e85738b8dd714c2eaf0e90cecf5e1b4293c27ffb | c5b07d17b3c9fb19e4b302465d237fd1d988c14f | /src/category/adjoint.lean | 6d7ec4e08024e0f395f8d1dec25dd1831d5ac0c3 | [
"MIT"
] | permissive | skaslev/papers | acaec61602b28c33d6115e53913b2002136aa29b | f15b379f3c43bbd0a37ac7bb75f4278f7e901389 | refs/heads/master | 1,665,505,770,318 | 1,660,378,602,000 | 1,660,378,602,000 | 14,101,547 | 0 | 1 | MIT | 1,595,414,522,000 | 1,383,542,702,000 | Lean | UTF-8 | Lean | false | false | 507 | lean | import data.iso
-- Adjoint functors
-- bᶠ⁽ᵃ⁾ = g(b)ᵃ
def adj (f g : Type → Type) := Π A B, (f A → B) ≃ (A → g B)
notation f ` ⊣ ` g := adj f g
-- "Every monad arises from some adjunction — in fact, typically from many adjunctions"
-- The slogan is "Adjoint functors arise everywhere".
-- — Saunders Mac Lane, Categories for the Working Mathematician
namespace adj
-- xa ⊣ xᵃ
def curry (A : Type) : (λ x, x × A) ⊣ (λ x, A → x) :=
λ B C, iso.curry⁻¹
end adj
|
78a561e2d2fd233cc52824ab52e0d746cd7ed638 | 75db7e3219bba2fbf41bf5b905f34fcb3c6ca3f2 | /hott/algebra/category/functor/attributes.hlean | 1534c5e9c659c1b6d821e4e9a4e9c4df8497c7f0 | [
"Apache-2.0"
] | permissive | jroesch/lean | 30ef0860fa905d35b9ad6f76de1a4f65c9af6871 | 3de4ec1a6ce9a960feb2a48eeea8b53246fa34f2 | refs/heads/master | 1,586,090,835,348 | 1,455,142,203,000 | 1,455,142,277,000 | 51,536,958 | 1 | 0 | null | 1,455,215,811,000 | 1,455,215,811,000 | null | UTF-8 | Lean | false | false | 6,532 | hlean | /-
Copyright (c) 2015 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris van Doorn
Attributes of functors (full, faithful, split essentially surjective, ...)
Adjoint functors, isomorphisms and equivalences have their own file
-/
import ..constructions.functor function arity
open eq functor trunc prod is_equiv iso equiv function is_trunc
namespace category
variables {C D E : Precategory} {F : C ⇒ D} {G : D ⇒ C}
definition faithful [class] (F : C ⇒ D) := Π⦃c c' : C⦄ ⦃f f' : c ⟶ c'⦄, F f = F f' → f = f'
definition full [class] (F : C ⇒ D) := Π⦃c c' : C⦄, is_surjective (@(to_fun_hom F) c c')
definition fully_faithful [class] (F : C ⇒ D) := Π(c c' : C), is_equiv (@(to_fun_hom F) c c')
definition split_essentially_surjective [class] (F : C ⇒ D) := Π(d : D), Σ(c : C), F c ≅ d
definition essentially_surjective [class] (F : C ⇒ D) := Π(d : D), ∃(c : C), F c ≅ d
definition is_weak_equivalence [class] (F : C ⇒ D) :=
fully_faithful F × essentially_surjective F
definition is_equiv_of_fully_faithful [instance] [reducible] (F : C ⇒ D)
[H : fully_faithful F] (c c' : C) : is_equiv (@(to_fun_hom F) c c') :=
!H
definition hom_inv [reducible] (F : C ⇒ D) [H : fully_faithful F] (c c' : C) (f : F c ⟶ F c')
: c ⟶ c' :=
(to_fun_hom F)⁻¹ᶠ f
definition reflect_is_iso [constructor] (F : C ⇒ D) [H : fully_faithful F] {c c' : C}
(f : c ⟶ c') [H : is_iso (F f)] : is_iso f :=
begin
fconstructor,
{ exact (to_fun_hom F)⁻¹ᶠ (F f)⁻¹},
{ apply eq_of_fn_eq_fn' (to_fun_hom F),
rewrite [respect_comp,right_inv (to_fun_hom F),respect_id,left_inverse]},
{ apply eq_of_fn_eq_fn' (to_fun_hom F),
rewrite [respect_comp,right_inv (to_fun_hom F),respect_id,right_inverse]},
end
definition reflect_iso [constructor] (F : C ⇒ D) [H : fully_faithful F] {c c' : C}
(f : F c ≅ F c') : c ≅ c' :=
begin
fconstructor,
{ exact (to_fun_hom F)⁻¹ᶠ f},
{ assert H : is_iso (F ((to_fun_hom F)⁻¹ᶠ f)),
{ have H' : is_iso (to_hom f), from _, exact (right_inv (to_fun_hom F) (to_hom f))⁻¹ ▸ H'},
exact reflect_is_iso F _},
end
theorem reflect_inverse (F : C ⇒ D) [H : fully_faithful F] {c c' : C} (f : c ⟶ c')
[H : is_iso f] : (to_fun_hom F)⁻¹ᶠ (F f)⁻¹ = f⁻¹ :=
inverse_eq_inverse (idp : to_hom (@(iso.mk f) (reflect_is_iso F f)) = f)
definition hom_equiv_F_hom_F [constructor] (F : C ⇒ D)
[H : fully_faithful F] (c c' : C) : (c ⟶ c') ≃ (F c ⟶ F c') :=
equiv.mk _ !H
definition iso_of_F_iso_F (F : C ⇒ D)
[H : fully_faithful F] (c c' : C) (g : F c ≅ F c') : c ≅ c' :=
begin
induction g with g G, induction G with h p q, fapply iso.MK,
{ rexact (to_fun_hom F)⁻¹ᶠ g},
{ rexact (to_fun_hom F)⁻¹ᶠ h},
{ exact abstract begin
apply eq_of_fn_eq_fn' (to_fun_hom F),
rewrite [respect_comp, respect_id,
right_inv (to_fun_hom F), right_inv (to_fun_hom F), p],
end end},
{ exact abstract begin
apply eq_of_fn_eq_fn' (to_fun_hom F),
rewrite [respect_comp, respect_id,
right_inv (to_fun_hom F), right_inv (@(to_fun_hom F) c' c), q],
end end}
end
definition iso_equiv_F_iso_F [constructor] (F : C ⇒ D)
[H : fully_faithful F] (c c' : C) : (c ≅ c') ≃ (F c ≅ F c') :=
begin
fapply equiv.MK,
{ exact to_fun_iso F},
{ apply iso_of_F_iso_F},
{ exact abstract begin
intro f, induction f with f F', induction F' with g p q, apply iso_eq,
esimp [iso_of_F_iso_F], apply right_inv end end},
{ exact abstract begin
intro f, induction f with f F', induction F' with g p q, apply iso_eq,
esimp [iso_of_F_iso_F], apply right_inv end end},
end
definition full_of_fully_faithful [instance] (F : C ⇒ D) [H : fully_faithful F] : full F :=
λc c' g, tr (fiber.mk ((@(to_fun_hom F) c c')⁻¹ᶠ g) !right_inv)
definition faithful_of_fully_faithful [instance] (F : C ⇒ D) [H : fully_faithful F]
: faithful F :=
λc c' f f' p, is_injective_of_is_embedding p
definition is_embedding_of_faithful [instance] (F : C ⇒ D) [H : faithful F] (c c' : C)
: is_embedding (to_fun_hom F : c ⟶ c' → F c ⟶ F c') :=
begin
apply is_embedding_of_is_injective,
apply H
end
definition is_surjective_of_full [instance] (F : C ⇒ D) [H : full F] (c c' : C)
: is_surjective (to_fun_hom F : c ⟶ c' → F c ⟶ F c') :=
@H c c'
definition fully_faithful_of_full_of_faithful (H : faithful F) (K : full F)
: fully_faithful F :=
begin
intro c c',
apply is_equiv_of_is_surjective_of_is_embedding,
end
theorem is_hprop_fully_faithful [instance] (F : C ⇒ D) : is_hprop (fully_faithful F) :=
by unfold fully_faithful; exact _
theorem is_hprop_full [instance] (F : C ⇒ D) : is_hprop (full F) :=
by unfold full; exact _
theorem is_hprop_faithful [instance] (F : C ⇒ D) : is_hprop (faithful F) :=
by unfold faithful; exact _
theorem is_hprop_essentially_surjective [instance] (F : C ⇒ D)
: is_hprop (essentially_surjective F) :=
by unfold essentially_surjective; exact _
theorem is_hprop_is_weak_equivalence [instance] (F : C ⇒ D) : is_hprop (is_weak_equivalence F) :=
by unfold is_weak_equivalence; exact _
definition fully_faithful_equiv (F : C ⇒ D) : fully_faithful F ≃ (faithful F × full F) :=
equiv_of_is_hprop (λH, (faithful_of_fully_faithful F, full_of_fully_faithful F))
(λH, fully_faithful_of_full_of_faithful (pr1 H) (pr2 H))
/- alternative proof using direct calculation with equivalences
definition fully_faithful_equiv (F : C ⇒ D) : fully_faithful F ≃ (faithful F × full F) :=
calc
fully_faithful F
≃ (Π(c c' : C), is_embedding (to_fun_hom F) × is_surjective (to_fun_hom F))
: pi_equiv_pi_id (λc, pi_equiv_pi_id
(λc', !is_equiv_equiv_is_embedding_times_is_surjective))
... ≃ (Π(c : C), (Π(c' : C), is_embedding (to_fun_hom F)) ×
(Π(c' : C), is_surjective (to_fun_hom F)))
: pi_equiv_pi_id (λc, !equiv_prod_corec)
... ≃ (Π(c c' : C), is_embedding (to_fun_hom F)) × full F
: equiv_prod_corec
... ≃ faithful F × full F
: prod_equiv_prod_right (pi_equiv_pi_id (λc, pi_equiv_pi_id
(λc', !is_embedding_equiv_is_injective)))
-/
end category
|
a59c428845c35ed199785b52c1d111b3e57795e3 | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/logic/nonempty.lean | 1e2df5304b30b00d641476bd4667ef26ee5686f0 | [
"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 | 5,691 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl
-/
import logic.basic
/-!
# Nonempty types
This file proves a few extra facts about `nonempty`, which is defined in core Lean.
## Main declarations
* `nonempty.some`: Extracts a witness of nonemptiness using choice. Takes `nonempty α` explicitly.
* `classical.arbitrary`: Extracts a witness of nonemptiness using choice. Takes `nonempty α` as an
instance.
-/
variables {α β : Type*} {γ : α → Type*}
attribute [simp] nonempty_of_inhabited
@[priority 20]
instance has_zero.nonempty [has_zero α] : nonempty α := ⟨0⟩
@[priority 20]
instance has_one.nonempty [has_one α] : nonempty α := ⟨1⟩
lemma exists_true_iff_nonempty {α : Sort*} : (∃a:α, true) ↔ nonempty α :=
iff.intro (λ⟨a, _⟩, ⟨a⟩) (λ⟨a⟩, ⟨a, trivial⟩)
@[simp] lemma nonempty_Prop {p : Prop} : nonempty p ↔ p :=
iff.intro (assume ⟨h⟩, h) (assume h, ⟨h⟩)
lemma not_nonempty_iff_imp_false {α : Sort*} : ¬ nonempty α ↔ α → false :=
⟨λ h a, h ⟨a⟩, λ h ⟨a⟩, h a⟩
@[simp] lemma nonempty_sigma : nonempty (Σa:α, γ a) ↔ (∃a:α, nonempty (γ a)) :=
iff.intro (assume ⟨⟨a, c⟩⟩, ⟨a, ⟨c⟩⟩) (assume ⟨a, ⟨c⟩⟩, ⟨⟨a, c⟩⟩)
@[simp] lemma nonempty_psigma {α} {β : α → Sort*} : nonempty (psigma β) ↔ (∃a:α, nonempty (β a)) :=
iff.intro (assume ⟨⟨a, c⟩⟩, ⟨a, ⟨c⟩⟩) (assume ⟨a, ⟨c⟩⟩, ⟨⟨a, c⟩⟩)
@[simp] lemma nonempty_subtype {α} {p : α → Prop} : nonempty (subtype p) ↔ (∃a:α, p a) :=
iff.intro (assume ⟨⟨a, h⟩⟩, ⟨a, h⟩) (assume ⟨a, h⟩, ⟨⟨a, h⟩⟩)
@[simp] lemma nonempty_prod : nonempty (α × β) ↔ (nonempty α ∧ nonempty β) :=
iff.intro (assume ⟨⟨a, b⟩⟩, ⟨⟨a⟩, ⟨b⟩⟩) (assume ⟨⟨a⟩, ⟨b⟩⟩, ⟨⟨a, b⟩⟩)
@[simp] lemma nonempty_pprod {α β} : nonempty (pprod α β) ↔ (nonempty α ∧ nonempty β) :=
iff.intro (assume ⟨⟨a, b⟩⟩, ⟨⟨a⟩, ⟨b⟩⟩) (assume ⟨⟨a⟩, ⟨b⟩⟩, ⟨⟨a, b⟩⟩)
@[simp] lemma nonempty_sum : nonempty (α ⊕ β) ↔ (nonempty α ∨ nonempty β) :=
iff.intro
(assume ⟨h⟩, match h with sum.inl a := or.inl ⟨a⟩ | sum.inr b := or.inr ⟨b⟩ end)
(assume h, match h with or.inl ⟨a⟩ := ⟨sum.inl a⟩ | or.inr ⟨b⟩ := ⟨sum.inr b⟩ end)
@[simp] lemma nonempty_psum {α β} : nonempty (psum α β) ↔ (nonempty α ∨ nonempty β) :=
iff.intro
(assume ⟨h⟩, match h with psum.inl a := or.inl ⟨a⟩ | psum.inr b := or.inr ⟨b⟩ end)
(assume h, match h with or.inl ⟨a⟩ := ⟨psum.inl a⟩ | or.inr ⟨b⟩ := ⟨psum.inr b⟩ end)
@[simp] lemma nonempty_empty : ¬ nonempty empty :=
assume ⟨h⟩, h.elim
@[simp] lemma nonempty_ulift : nonempty (ulift α) ↔ nonempty α :=
iff.intro (assume ⟨⟨a⟩⟩, ⟨a⟩) (assume ⟨a⟩, ⟨⟨a⟩⟩)
@[simp] lemma nonempty_plift {α} : nonempty (plift α) ↔ nonempty α :=
iff.intro (assume ⟨⟨a⟩⟩, ⟨a⟩) (assume ⟨a⟩, ⟨⟨a⟩⟩)
@[simp] lemma nonempty.forall {α} {p : nonempty α → Prop} : (∀h:nonempty α, p h) ↔ (∀a, p ⟨a⟩) :=
iff.intro (assume h a, h _) (assume h ⟨a⟩, h _)
@[simp] lemma nonempty.exists {α} {p : nonempty α → Prop} : (∃h:nonempty α, p h) ↔ (∃a, p ⟨a⟩) :=
iff.intro (assume ⟨⟨a⟩, h⟩, ⟨a, h⟩) (assume ⟨a, h⟩, ⟨⟨a⟩, h⟩)
/-- Using `classical.choice`, lifts a (`Prop`-valued) `nonempty` instance to a (`Type`-valued)
`inhabited` instance. `classical.inhabited_of_nonempty` already exists, in
`core/init/classical.lean`, but the assumption is not a type class argument,
which makes it unsuitable for some applications. -/
noncomputable def classical.inhabited_of_nonempty' {α} [h : nonempty α] : inhabited α :=
⟨classical.choice h⟩
/-- Using `classical.choice`, extracts a term from a `nonempty` type. -/
@[reducible] protected noncomputable def nonempty.some {α} (h : nonempty α) : α :=
classical.choice h
/-- Using `classical.choice`, extracts a term from a `nonempty` type. -/
@[reducible] protected noncomputable def classical.arbitrary (α) [h : nonempty α] : α :=
classical.choice h
/-- Given `f : α → β`, if `α` is nonempty then `β` is also nonempty.
`nonempty` cannot be a `functor`, because `functor` is restricted to `Type`. -/
lemma nonempty.map {α β} (f : α → β) : nonempty α → nonempty β
| ⟨h⟩ := ⟨f h⟩
protected lemma nonempty.map2 {α β γ : Sort*} (f : α → β → γ) : nonempty α → nonempty β → nonempty γ
| ⟨x⟩ ⟨y⟩ := ⟨f x y⟩
protected lemma nonempty.congr {α β} (f : α → β) (g : β → α) :
nonempty α ↔ nonempty β :=
⟨nonempty.map f, nonempty.map g⟩
lemma nonempty.elim_to_inhabited {α : Sort*} [h : nonempty α] {p : Prop}
(f : inhabited α → p) : p :=
h.elim $ f ∘ inhabited.mk
instance {α β} [h : nonempty α] [h2 : nonempty β] : nonempty (α × β) :=
h.elim $ λ g, h2.elim $ λ g2, ⟨⟨g, g2⟩⟩
instance {ι : Sort*} {α : ι → Sort*} [Π i, nonempty (α i)] : nonempty (Π i, α i) :=
⟨λ _, classical.arbitrary _⟩
lemma classical.nonempty_pi {ι} {α : ι → Sort*} : nonempty (Π i, α i) ↔ ∀ i, nonempty (α i) :=
⟨λ ⟨f⟩ a, ⟨f a⟩, @pi.nonempty _ _⟩
lemma subsingleton_of_not_nonempty {α : Sort*} (h : ¬ nonempty α) : subsingleton α :=
⟨λ x, false.elim $ not_nonempty_iff_imp_false.mp h x⟩
lemma function.surjective.nonempty [h : nonempty β] {f : α → β} (hf : function.surjective f) :
nonempty α :=
let ⟨y⟩ := h, ⟨x, hx⟩ := hf y in ⟨x⟩
|
c4c8557742e5caaabe01875c61013f2b4394e9a1 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/category_theory/preadditive/eilenberg_moore.lean | fdb3f3332c17099b2fafbf0f10dc0a5461ecfb61 | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 4,734 | lean | /-
Copyright (c) 2022 Julian Kuelshammer. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Julian Kuelshammer
-/
import category_theory.preadditive.basic
import category_theory.monad.algebra
import category_theory.preadditive.additive_functor
/-!
# Preadditive structure on algebras over a monad
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
If `C` is a preadditive categories and `T` is an additive monad on `C` then `algebra T` is also
preadditive. Dually, if `U` is an additive comonad on `C` then `coalgebra U` is preadditive as well.
-/
universes v₁ u₁ -- morphism levels before object levels. See note [category_theory universes].
namespace category_theory
variables (C : Type u₁) [category.{v₁} C] [preadditive C] (T : monad C)
[functor.additive (T : C ⥤ C)]
open category_theory.limits preadditive
/-- The category of algebras over an additive monad on a preadditive category is preadditive. -/
@[simps]
instance monad.algebra_preadditive : preadditive (monad.algebra T) :=
{ hom_group := λ F G,
{ add := λ α β,
{ f := α.f + β.f,
h' := by simp only [functor.map_add, add_comp, monad.algebra.hom.h, comp_add] },
zero :=
{ f := 0,
h' := by simp only [functor.map_zero, zero_comp, comp_zero] },
nsmul := λ n α,
{ f := n • α.f,
h' := by rw [functor.map_nsmul, nsmul_comp, monad.algebra.hom.h, comp_nsmul] },
neg := λ α,
{ f := -α.f,
h' := by simp only [functor.map_neg, neg_comp, monad.algebra.hom.h, comp_neg] },
sub := λ α β,
{ f := α.f - β.f,
h' := by simp only [functor.map_sub, sub_comp, monad.algebra.hom.h, comp_sub] },
zsmul := λ r α,
{ f := r • α.f,
h' := by rw [functor.map_zsmul, zsmul_comp, monad.algebra.hom.h, comp_zsmul] },
add_assoc := by { intros, ext, apply add_assoc },
zero_add := by { intros, ext, apply zero_add },
add_zero := by { intros, ext, apply add_zero },
nsmul_zero' := by { intros, ext, apply zero_smul },
nsmul_succ' := by { intros, ext, apply succ_nsmul },
sub_eq_add_neg := by { intros, ext, apply sub_eq_add_neg },
zsmul_zero' := by { intros, ext, apply zero_smul },
zsmul_succ' := by { intros, ext, dsimp, simp only [coe_nat_zsmul, succ_nsmul], refl, },
zsmul_neg' := by { intros, ext, simp only [zsmul_neg_succ_of_nat, neg_inj,
nsmul_eq_smul_cast ℤ] },
add_left_neg := by { intros, ext, apply add_left_neg },
add_comm := by { intros, ext, apply add_comm } },
add_comp' := by { intros, ext, apply add_comp },
comp_add' := by { intros, ext, apply comp_add } }
instance monad.forget_additive : (monad.forget T).additive := {}
variables (U : comonad C) [functor.additive (U : C ⥤ C)]
/-- The category of coalgebras over an additive comonad on a preadditive category is preadditive. -/
@[simps]
instance comonad.coalgebra_preadditive : preadditive (comonad.coalgebra U) :=
{ hom_group := λ F G,
{ add := λ α β,
{ f := α.f + β.f,
h' := by simp only [functor.map_add, comp_add, comonad.coalgebra.hom.h, add_comp] },
zero :=
{ f := 0,
h' := by simp only [functor.map_zero, comp_zero, zero_comp] },
nsmul := λ n α,
{ f := n • α.f,
h' := by rw [functor.map_nsmul, comp_nsmul, comonad.coalgebra.hom.h, nsmul_comp] },
neg := λ α,
{ f := -α.f,
h' := by simp only [functor.map_neg, comp_neg, comonad.coalgebra.hom.h, neg_comp] },
sub := λ α β,
{ f := α.f - β.f,
h' := by simp only [functor.map_sub, comp_sub, comonad.coalgebra.hom.h, sub_comp] },
zsmul := λ r α,
{ f := r • α.f,
h' := by rw [functor.map_zsmul, comp_zsmul, comonad.coalgebra.hom.h, zsmul_comp] },
add_assoc := by { intros, ext, apply add_assoc },
zero_add := by { intros, ext, apply zero_add },
add_zero := by { intros, ext, apply add_zero },
nsmul_zero' := by { intros, ext, apply zero_smul },
nsmul_succ' := by { intros, ext, apply succ_nsmul },
sub_eq_add_neg := by { intros, ext, apply sub_eq_add_neg },
zsmul_zero' := by { intros, ext, apply zero_smul },
zsmul_succ' := by { intros, ext, dsimp, simp only [coe_nat_zsmul, succ_nsmul], refl, },
zsmul_neg' := by { intros, ext, simp only [zsmul_neg_succ_of_nat, neg_inj,
nsmul_eq_smul_cast ℤ] },
add_left_neg := by { intros, ext, apply add_left_neg },
add_comm := by { intros, ext, apply add_comm } },
add_comp' := by { intros, ext, apply add_comp },
comp_add' := by { intros, ext, apply comp_add } }
instance comonad.forget_additive : (comonad.forget U).additive := {}
end category_theory
|
ef6665a1555ff146f88de6ca69b35c254b0b08e2 | bbecf0f1968d1fba4124103e4f6b55251d08e9c4 | /src/order/lexicographic.lean | ea40cd5e0b166a5ffe11ba3ae3161eedcc8969ca | [
"Apache-2.0"
] | permissive | waynemunro/mathlib | e3fd4ff49f4cb43d4a8ded59d17be407bc5ee552 | 065a70810b5480d584033f7bbf8e0409480c2118 | refs/heads/master | 1,693,417,182,397 | 1,634,644,781,000 | 1,634,644,781,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 8,623 | lean | /-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Minchao Wu
-/
import tactic.basic
/-!
# Lexicographic order
This file defines the lexicographic relation for pairs and dependent pairs of orders, partial orders
and linear orders.
## Main declarations
* `lex α β`: Synonym of `α × β` to equip it with lexicographic order without creating conflicting
instances.
* `lex_<pre/partial_/linear_>order`: Instances lifting the orders on `α` and `β` to `lex α β`
* `dlex_<pre/partial_/linear_>order`: Instances lifting the orders on every `Z a` to the dependent
pair `Z`.
## See also
The lexicographic ordering on lists is provided in `data.list.basic`.
-/
universes u v
/-- The cartesian product, equipped with the lexicographic order. -/
def lex (α : Type u) (β : Type v) := α × β
variables {α : Type u} {β : Type v}
instance [decidable_eq α] [decidable_eq β] : decidable_eq (lex α β) :=
prod.decidable_eq
instance [inhabited α] [inhabited β] : inhabited (lex α β) :=
prod.inhabited
/-- Dictionary / lexicographic ordering on pairs. -/
instance lex_has_le [preorder α] [preorder β] : has_le (lex α β) :=
{ le := prod.lex (<) (≤) }
instance lex_has_lt [preorder α] [preorder β] : has_lt (lex α β) :=
{ lt := prod.lex (<) (<) }
/-- Dictionary / lexicographic preorder for pairs. -/
instance lex_preorder [preorder α] [preorder β] : preorder (lex α β) :=
{ le_refl := λ ⟨l, r⟩, by { right, apply le_refl },
le_trans :=
begin
rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩ ⟨a₃, b₃⟩ ⟨h₁l, h₁r⟩ ⟨h₂l, h₂r⟩,
{ left, apply lt_trans, repeat { assumption } },
{ left, assumption },
{ left, assumption },
{ right, apply le_trans, repeat { assumption } }
end,
lt_iff_le_not_le :=
begin
rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩,
split,
{ rintros (⟨_, _, _, _, hlt⟩ | ⟨_, _, _, hlt⟩),
{ split,
{ left, assumption },
{ rintro ⟨l,r⟩,
{ apply lt_asymm hlt, assumption },
{ apply lt_irrefl _ hlt } } },
{ split,
{ right, rw lt_iff_le_not_le at hlt, exact hlt.1 },
{ rintro ⟨l,r⟩,
{ apply lt_irrefl a₁, assumption },
{ rw lt_iff_le_not_le at hlt, apply hlt.2, assumption } } } },
{ rintros ⟨⟨h₁ll, h₁lr⟩, h₂r⟩,
{ left, assumption },
{ right, rw lt_iff_le_not_le, split,
{ assumption },
{ intro h, apply h₂r, right, exact h } } }
end,
.. lex_has_le,
.. lex_has_lt }
/-- Dictionary / lexicographic partial_order for pairs. -/
instance lex_partial_order [partial_order α] [partial_order β] : partial_order (lex α β) :=
{ le_antisymm :=
begin
rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩
(⟨_, _, _, _, hlt₁⟩ | ⟨_, _, _, hlt₁⟩) (⟨_, _, _, _, hlt₂⟩ | ⟨_, _, _, hlt₂⟩),
{ exfalso, exact lt_irrefl a₁ (lt_trans hlt₁ hlt₂) },
{ exfalso, exact lt_irrefl a₁ hlt₁ },
{ exfalso, exact lt_irrefl a₁ hlt₂ },
{ have := le_antisymm hlt₁ hlt₂, simp [this] }
end
.. lex_preorder }
/-- Dictionary / lexicographic linear_order for pairs. -/
instance lex_linear_order [linear_order α] [linear_order β] : linear_order (lex α β) :=
{ le_total :=
begin
rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩,
obtain ha | ha := le_total a₁ a₂;
cases lt_or_eq_of_le ha with a_lt a_eq,
-- Deal with the two goals with a₁ ≠ a₂
{ left, left, exact a_lt },
swap,
{ right, left, exact a_lt },
-- Now deal with the two goals with a₁ = a₂
all_goals { subst a_eq, obtain hb | hb := le_total b₁ b₂ },
{ left, right, exact hb },
{ right, right, exact hb },
{ left, right, exact hb },
{ right, right, exact hb },
end,
decidable_le :=
begin
rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩,
obtain a_lt | a_le := linear_order.decidable_le a₁ a₂,
{ -- a₂ < a₁
left, rw not_le at a_lt, rintro ⟨l, r⟩,
{ apply lt_irrefl a₂, apply lt_trans, repeat { assumption } },
{ apply lt_irrefl a₁, assumption } },
{ -- a₁ ≤ a₂
by_cases h : a₁ = a₂,
{ rw h,
obtain b_lt | b_le := linear_order.decidable_le b₁ b₂,
{ -- b₂ < b₁
left, rw not_le at b_lt, rintro ⟨l, r⟩,
{ apply lt_irrefl a₂, assumption },
{ apply lt_irrefl b₂, apply lt_of_lt_of_le, repeat { assumption } } },
-- b₁ ≤ b₂
{ right, right, assumption } },
-- a₁ < a₂
{ right, left, apply lt_of_le_of_ne, repeat { assumption } } }
end,
.. lex_partial_order }.
variables {Z : α → Type v}
/--
Dictionary / lexicographic ordering on dependent pairs.
The 'pointwise' partial order `prod.has_le` doesn't make
sense for dependent pairs, so it's safe to mark these as
instances here.
-/
instance dlex_has_le [preorder α] [∀ a, preorder (Z a)] : has_le (Σ' a, Z a) :=
{ le := psigma.lex (<) (λ a, (≤)) }
instance dlex_has_lt [preorder α] [∀ a, preorder (Z a)] : has_lt (Σ' a, Z a) :=
{ lt := psigma.lex (<) (λ a, (<)) }
/-- Dictionary / lexicographic preorder on dependent pairs. -/
instance dlex_preorder [preorder α] [∀ a, preorder (Z a)] : preorder (Σ' a, Z a) :=
{ le_refl := λ ⟨l, r⟩, by { right, apply le_refl },
le_trans :=
begin
rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩ ⟨a₃, b₃⟩ ⟨h₁l, h₁r⟩ ⟨h₂l, h₂r⟩,
{ left, apply lt_trans, repeat { assumption } },
{ left, assumption },
{ left, assumption },
{ right, apply le_trans, repeat { assumption } }
end,
lt_iff_le_not_le :=
begin
rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩,
split,
{ rintros (⟨_, _, _, _, hlt⟩ | ⟨_, _, _, hlt⟩),
{ split,
{ left, assumption },
{ rintro ⟨l,r⟩,
{ apply lt_asymm hlt, assumption },
{ apply lt_irrefl _ hlt } } },
{ split,
{ right, rw lt_iff_le_not_le at hlt, exact hlt.1 },
{ rintro ⟨l,r⟩,
{ apply lt_irrefl a₁, assumption },
{ rw lt_iff_le_not_le at hlt, apply hlt.2, assumption } } } },
{ rintros ⟨⟨h₁ll, h₁lr⟩, h₂r⟩,
{ left, assumption },
{ right, rw lt_iff_le_not_le, split,
{ assumption },
{ intro h, apply h₂r, right, exact h } } }
end,
.. dlex_has_le,
.. dlex_has_lt }
/-- Dictionary / lexicographic partial_order for dependent pairs. -/
instance dlex_partial_order [partial_order α] [∀ a, partial_order (Z a)] :
partial_order (Σ' a, Z a) :=
{ le_antisymm :=
begin
rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩
(⟨_, _, _, _, hlt₁⟩ | ⟨_, _, _, hlt₁⟩) (⟨_, _, _, _, hlt₂⟩ | ⟨_, _, _, hlt₂⟩),
{ exfalso, exact lt_irrefl a₁ (lt_trans hlt₁ hlt₂) },
{ exfalso, exact lt_irrefl a₁ hlt₁ },
{ exfalso, exact lt_irrefl a₁ hlt₂ },
{ have := le_antisymm hlt₁ hlt₂, simp [this] }
end
.. dlex_preorder }
/-- Dictionary / lexicographic linear_order for pairs. -/
instance dlex_linear_order [linear_order α] [∀ a, linear_order (Z a)] : linear_order (Σ' a, Z a) :=
{ le_total :=
begin
rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩,
obtain ha | ha := le_total a₁ a₂;
cases lt_or_eq_of_le ha with a_lt a_eq,
-- Deal with the two goals with a₁ ≠ a₂
{ left, left, exact a_lt },
swap,
{ right, left, exact a_lt },
-- Now deal with the two goals with a₁ = a₂
all_goals { subst a_eq, obtain hb | hb := le_total b₁ b₂ },
{ left, right, exact hb },
{ right, right, exact hb },
{ left, right, exact hb },
{ right, right, exact hb },
end,
decidable_le :=
begin
rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩,
obtain a_lt | a_le := linear_order.decidable_le a₁ a₂,
{ -- a₂ < a₁
left, rw not_le at a_lt, rintro ⟨l, r⟩,
{ apply lt_irrefl a₂, apply lt_trans, repeat { assumption } },
{ apply lt_irrefl a₁, assumption } },
{ -- a₁ ≤ a₂
by_cases h : a₁ = a₂,
{ subst h,
obtain b_lt | b_le := linear_order.decidable_le b₁ b₂,
{ -- b₂ < b₁
left, rw not_le at b_lt, rintro ⟨l, r⟩,
{ apply lt_irrefl a₁, assumption },
{ apply lt_irrefl b₂, apply lt_of_lt_of_le, repeat { assumption } } },
-- b₁ ≤ b₂
{ right, right, assumption } },
-- a₁ < a₂
{ right, left, apply lt_of_le_of_ne, repeat { assumption } } }
end,
.. dlex_partial_order }.
|
4b8baae87952fc985d30e6decb29a86643e43ee0 | d5ecf6c46a2f605470a4a7724909dc4b9e7350e0 | /set_theory/cardinal.lean | d219f9a5248a0ecd7521dd1d4b5533db4260a8c5 | [
"Apache-2.0"
] | permissive | MonoidMusician/mathlib | 41f79df478987a636b735c338396813d2e8e44c4 | 72234ef1a050eea3a2197c23aeb345fc13c08ff3 | refs/heads/master | 1,583,672,205,771 | 1,522,892,143,000 | 1,522,892,143,000 | 128,144,032 | 0 | 0 | Apache-2.0 | 1,522,892,144,000 | 1,522,890,892,000 | Lean | UTF-8 | Lean | false | false | 27,369 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Johannes Hölzl, Mario Carneiro
Cardinal arithmetic.
Cardinals are represented as quotient over equinumerous types.
-/
import data.set.finite data.quot logic.schroeder_bernstein logic.function
noncomputable theory
open function lattice set classical
local attribute [instance] prop_decidable
universes u v w x
instance cardinal.is_equivalent : setoid (Type u) :=
{ r := λα β, nonempty (α ≃ β),
iseqv := ⟨λα,
⟨equiv.refl α⟩,
λα β ⟨e⟩, ⟨e.symm⟩,
λα β γ ⟨e₁⟩ ⟨e₂⟩, ⟨e₁.trans e₂⟩⟩ }
/-- `cardinal.{u}` is the type of cardinal numbers in `Type u`,
defined as the quotient of `Type u` by existence of an equivalence
(a bijection with explicit inverse). -/
def cardinal : Type (u + 1) := quotient cardinal.is_equivalent
namespace cardinal
/-- The cardinal of a type -/
def mk : Type u → cardinal := quotient.mk
@[simp] theorem mk_def (α : Type u) : @eq cardinal ⟦α⟧ (mk α) := rfl
@[simp] theorem mk_out (c : cardinal) : mk (c.out) = c := quotient.out_eq _
instance : has_le cardinal.{u} :=
⟨λq₁ q₂, quotient.lift_on₂ q₁ q₂ (λα β, nonempty $ α ↪ β) $
assume α β γ δ ⟨e₁⟩ ⟨e₂⟩,
propext ⟨assume ⟨e⟩, ⟨e.congr e₁ e₂⟩, assume ⟨e⟩, ⟨e.congr e₁.symm e₂.symm⟩⟩⟩
theorem le_mk_iff_exists_set {c : cardinal} {α : Type u} :
c ≤ mk α ↔ ∃ p : set α, mk p = c :=
⟨quotient.induction_on c $ λ β ⟨⟨f, hf⟩⟩,
⟨set.range f, eq.symm $ quot.sound ⟨equiv.set.range f hf⟩⟩,
λ ⟨p, e⟩, e ▸ ⟨⟨subtype.val, λ a b, subtype.eq⟩⟩⟩
instance : linear_order cardinal.{u} :=
{ le := (≤),
le_refl := assume a, quot.induction_on a $ λ α, ⟨embedding.refl _⟩,
le_trans := assume a b c, quotient.induction_on₃ a b c $ assume α β γ ⟨e₁⟩ ⟨e₂⟩, ⟨e₁.trans e₂⟩,
le_antisymm := assume a b, quotient.induction_on₂ a b $ assume α β ⟨e₁⟩ ⟨e₂⟩,
quotient.sound (e₁.antisymm e₂),
le_total := assume a b, quotient.induction_on₂ a b $ assume α β, embedding.total }
instance : decidable_linear_order cardinal.{u} :=
{ decidable_le := by apply_instance, ..cardinal.linear_order }
instance : distrib_lattice cardinal.{u} := by apply_instance
instance : has_zero cardinal.{u} := ⟨⟦ulift empty⟧⟩
instance : inhabited cardinal.{u} := ⟨0⟩
theorem ne_zero_iff_nonempty {α : Type u} : mk α ≠ 0 ↔ nonempty α :=
not_iff_comm.1
⟨λ h, quotient.sound ⟨(equiv.empty_of_not_nonempty h).trans equiv.ulift.symm⟩,
λ e, let ⟨h⟩ := quotient.exact e in λ ⟨a⟩, (h a).down.elim⟩
instance : has_one cardinal.{u} := ⟨⟦ulift unit⟧⟩
instance : zero_ne_one_class cardinal.{u} :=
{ zero := 0, one := 1, zero_ne_one :=
ne.symm $ ne_zero_iff_nonempty.2 ⟨⟨()⟩⟩ }
theorem le_one_iff_subsingleton {α : Type u} : mk α ≤ 1 ↔ subsingleton α :=
⟨λ ⟨f⟩, ⟨λ a b, f.inj (subsingleton.elim _ _)⟩,
λ ⟨h⟩, ⟨⟨λ a, ⟨()⟩, λ a b _, h _ _⟩⟩⟩
instance : has_add cardinal.{u} :=
⟨λq₁ q₂, quotient.lift_on₂ q₁ q₂ (λα β, mk (α ⊕ β)) $ assume α β γ δ ⟨e₁⟩ ⟨e₂⟩,
quotient.sound ⟨equiv.sum_congr e₁ e₂⟩⟩
@[simp] theorem add_def (α β) : mk α + mk β = mk (α ⊕ β) := rfl
instance : has_mul cardinal.{u} :=
⟨λq₁ q₂, quotient.lift_on₂ q₁ q₂ (λα β, mk (α × β)) $ assume α β γ δ ⟨e₁⟩ ⟨e₂⟩,
quotient.sound ⟨equiv.prod_congr e₁ e₂⟩⟩
@[simp] theorem mul_def (α β) : mk α * mk β = mk (α × β) := rfl
private theorem add_comm (a b : cardinal.{u}) : a + b = b + a :=
quotient.induction_on₂ a b $ assume α β, quotient.sound ⟨equiv.sum_comm α β⟩
private theorem mul_comm (a b : cardinal.{u}) : a * b = b * a :=
quotient.induction_on₂ a b $ assume α β, quotient.sound ⟨equiv.prod_comm α β⟩
private theorem zero_add (a : cardinal.{u}) : 0 + a = a :=
quotient.induction_on a $ assume α, quotient.sound
⟨equiv.trans (equiv.sum_congr equiv.ulift (equiv.refl α)) (equiv.empty_sum α)⟩
private theorem zero_mul (a : cardinal.{u}) : 0 * a = 0 :=
quotient.induction_on a $ assume α, quotient.sound
⟨equiv.trans (equiv.prod_congr equiv.ulift (equiv.refl α)) $
equiv.trans (equiv.empty_prod α) equiv.ulift.symm⟩
private theorem one_mul (a : cardinal.{u}) : 1 * a = a :=
quotient.induction_on a $ assume α, quotient.sound
⟨equiv.trans (equiv.prod_congr equiv.ulift (equiv.refl α)) (equiv.unit_prod α)⟩
private theorem left_distrib (a b c : cardinal.{u}) : a * (b + c) = a * b + a * c :=
quotient.induction_on₃ a b c $ assume α β γ, quotient.sound ⟨equiv.prod_sum_distrib α β γ⟩
instance : comm_semiring cardinal.{u} :=
{ zero := 0,
one := 1,
add := (+),
mul := (*),
zero_add := zero_add,
add_zero := assume a, by rw [add_comm a 0, zero_add a],
add_assoc := λa b c, quotient.induction_on₃ a b c $ assume α β γ,
quotient.sound ⟨equiv.sum_assoc α β γ⟩,
add_comm := add_comm,
zero_mul := zero_mul,
mul_zero := assume a, by rw [mul_comm a 0, zero_mul a],
one_mul := one_mul,
mul_one := assume a, by rw [mul_comm a 1, one_mul a],
mul_assoc := λa b c, quotient.induction_on₃ a b c $ assume α β γ,
quotient.sound ⟨equiv.prod_assoc α β γ⟩,
mul_comm := mul_comm,
left_distrib := left_distrib,
right_distrib := assume a b c,
by rw [mul_comm (a + b) c, left_distrib c a b, mul_comm c a, mul_comm c b] }
/-- The cardinal exponential. `mk α ^ mk β` is the cardinal of `β → α`. -/
protected def power (a b : cardinal.{u}) : cardinal.{u} :=
quotient.lift_on₂ a b (λα β, mk (β → α)) $ assume α₁ α₂ β₁ β₂ ⟨e₁⟩ ⟨e₂⟩,
quotient.sound ⟨equiv.arrow_congr e₂ e₁⟩
local notation a ^ b := cardinal.power a b
@[simp] theorem power_def (α β) : mk α ^ mk β = mk (β → α) := rfl
@[simp] theorem power_zero {a : cardinal} : a ^ 0 = 1 :=
quotient.induction_on a $ assume α, quotient.sound ⟨
equiv.trans (equiv.arrow_congr equiv.ulift (equiv.refl α)) $
equiv.trans equiv.arrow_empty_unit $
equiv.ulift.symm⟩
@[simp] theorem power_one {a : cardinal} : a ^ 1 = a :=
quotient.induction_on a $ assume α, quotient.sound ⟨
equiv.trans (equiv.arrow_congr equiv.ulift (equiv.refl α)) $
equiv.unit_arrow_equiv α⟩
@[simp] theorem one_power {a : cardinal} : 1 ^ a = 1 :=
quotient.induction_on a $ assume α, quotient.sound ⟨
equiv.trans (equiv.arrow_congr (equiv.refl α) equiv.ulift) $
equiv.trans (equiv.arrow_unit_equiv_unit α) $
equiv.ulift.symm⟩
@[simp] theorem prop_eq_two : mk (ulift Prop) = 2 :=
quot.sound ⟨equiv.ulift.trans $ equiv.Prop_equiv_bool.trans $
equiv.bool_equiv_unit_sum_unit.trans
(equiv.sum_congr equiv.ulift equiv.ulift).symm⟩
@[simp] theorem zero_power {a : cardinal} : a ≠ 0 → 0 ^ a = 0 :=
quotient.induction_on a $ assume α heq,
have nonempty α, from ne_zero_iff_nonempty.1 heq,
let a := choice this in
have (α → empty) ≃ empty,
from ⟨λf, f a, λe a, e, assume f, (f a).rec_on (λ_, (λa', f a) = f), assume e, rfl⟩,
quotient.sound
⟨equiv.trans (equiv.arrow_congr (equiv.refl α) equiv.ulift) $ equiv.trans this equiv.ulift.symm⟩
theorem power_ne_zero {a : cardinal} (b) : a ≠ 0 → a ^ b ≠ 0 :=
quotient.induction_on₂ a b $ λ α β h,
let ⟨a⟩ := ne_zero_iff_nonempty.1 h in
ne_zero_iff_nonempty.2 ⟨λ _, a⟩
theorem mul_power {a b c : cardinal} : (a * b) ^ c = a ^ c * b ^ c :=
quotient.induction_on₃ a b c $ assume α β γ,
quotient.sound ⟨equiv.arrow_prod_equiv_prod_arrow α β γ⟩
theorem power_add {a b c : cardinal} : a ^ (b + c) = a ^ b * a ^ c :=
quotient.induction_on₃ a b c $ assume α β γ,
quotient.sound ⟨equiv.sum_arrow_equiv_prod_arrow β γ α⟩
theorem power_mul {a b c : cardinal} : (a ^ b) ^ c = a ^ (b * c) :=
by rw [_root_.mul_comm b c];
from (quotient.induction_on₃ a b c $ assume α β γ,
quotient.sound ⟨equiv.arrow_arrow_equiv_prod_arrow γ β α⟩)
section order_properties
open sum
theorem zero_le (a : cardinal) : 0 ≤ a :=
quot.induction_on a $ λ α, ⟨embedding.of_not_nonempty $ λ ⟨⟨a⟩⟩, a.elim⟩
theorem le_zero (a : cardinal) : a ≤ 0 ↔ a = 0 :=
by simp [le_antisymm_iff, zero_le]
theorem pos_iff_ne_zero {o : cardinal} : 0 < o ↔ o ≠ 0 :=
by simp [lt_iff_le_and_ne, eq_comm, zero_le]
theorem zero_lt_one : (0 : cardinal) < 1 :=
lt_of_le_of_ne (zero_le _) zero_ne_one
theorem add_le_add {a b c d : cardinal} : a ≤ b → c ≤ d → a + c ≤ b + d :=
quotient.induction_on₂ a b $ assume α β, quotient.induction_on₂ c d $ assume γ δ ⟨e₁⟩ ⟨e₂⟩,
⟨embedding.sum_congr e₁ e₂⟩
theorem add_le_add_left (a) {b c : cardinal} : b ≤ c → a + b ≤ a + c :=
add_le_add (le_refl _)
theorem add_le_add_right {a b : cardinal} (c) (h : a ≤ b) : a + c ≤ b + c :=
add_le_add h (le_refl _)
theorem le_add_right (a b : cardinal) : a ≤ a + b :=
by simpa using add_le_add_left a (zero_le b)
theorem le_add_left (a b : cardinal) : a ≤ b + a :=
by simpa using add_le_add_right a (zero_le b)
theorem mul_le_mul {a b c d : cardinal} : a ≤ b → c ≤ d → a * c ≤ b * d :=
quotient.induction_on₂ a b $ assume α β, quotient.induction_on₂ c d $ assume γ δ ⟨e₁⟩ ⟨e₂⟩,
⟨embedding.prod_congr e₁ e₂⟩
theorem mul_le_mul_left (a) {b c : cardinal} : b ≤ c → a * b ≤ a * c :=
mul_le_mul (le_refl _)
theorem mul_le_mul_right {a b : cardinal} (c) (h : a ≤ b) : a * c ≤ b * c :=
mul_le_mul h (le_refl _)
theorem power_le_power_left {a b c : cardinal} : a ≠ 0 → b ≤ c → a ^ b ≤ a ^ c :=
quotient.induction_on₃ a b c $ assume α β γ hα ⟨e⟩,
have nonempty α, from classical.by_contradiction $ assume hnα,
hα $ quotient.sound ⟨equiv.trans (equiv.empty_of_not_nonempty hnα) equiv.ulift.symm⟩,
let ⟨a⟩ := this in
⟨@embedding.arrow_congr_right _ _ _ ⟨a⟩ e⟩
theorem power_le_power_right {a b c : cardinal} : a ≤ b → a ^ c ≤ b ^ c :=
quotient.induction_on₃ a b c $ assume α β γ ⟨e⟩, ⟨embedding.arrow_congr_left e⟩
theorem le_iff_exists_add {a b : cardinal} : a ≤ b ↔ ∃ c, b = a + c :=
⟨quotient.induction_on₂ a b $ λ α β ⟨⟨f, hf⟩⟩,
have (α ⊕ ↥-range f) ≃ β, from
(equiv.sum_congr (equiv.set.range f hf) (equiv.refl _)).trans $
(equiv.set.sum_compl (range f)),
⟨⟦(-range f : set β)⟧, quotient.sound ⟨this.symm⟩⟩,
λ ⟨c, e⟩, add_zero a ▸ e.symm ▸ add_le_add_left _ (zero_le _)⟩
end order_properties
instance : canonically_ordered_monoid cardinal.{u} :=
{ add_le_add_left := λ a b h c, add_le_add_left _ h,
lt_of_add_lt_add_left := λ a b c, le_imp_le_iff_lt_imp_lt.1 (add_le_add_left _),
le_iff_exists_add := @le_iff_exists_add,
..cardinal.comm_semiring, ..cardinal.linear_order }
instance : order_bot cardinal.{u} :=
{ bot := 0, bot_le := zero_le, ..cardinal.linear_order }
theorem cantor (a : cardinal.{u}) : a < 2 ^ a :=
by rw ← prop_eq_two; exact
quot.induction_on a (λ α, ⟨⟨⟨λ a b, ⟨a = b⟩,
λ a b h, cast (ulift.up.inj (@congr_fun _ _ _ _ h b)).symm rfl⟩⟩,
λ ⟨⟨f, hf⟩⟩, cantor_injective (λ s, f (λ a, ⟨s a⟩)) $
λ s t h, by funext a; injection congr_fun (hf h) a⟩)
instance : no_top_order cardinal.{u} :=
{ no_top := λ a, ⟨_, cantor a⟩, ..cardinal.linear_order }
/-- The minimum cardinal in a family of cardinals (the existence
of which is provided by `injective_min`). -/
def min {ι} (I : nonempty ι) (f : ι → cardinal) : cardinal :=
f $ classical.some $
@embedding.injective_min _ (λ i, (f i).out) I
theorem min_eq {ι} (I) (f : ι → cardinal) : ∃ i, min I f = f i :=
⟨_, rfl⟩
theorem min_le {ι I} (f : ι → cardinal) (i) : min I f ≤ f i :=
by rw [← mk_out (min I f), ← mk_out (f i)]; exact
let ⟨g⟩ := classical.some_spec
(@embedding.injective_min _ (λ i, (f i).out) I) in
⟨g i⟩
theorem le_min {ι I} {f : ι → cardinal} {a} : a ≤ min I f ↔ ∀ i, a ≤ f i :=
⟨λ h i, le_trans h (min_le _ _),
λ h, let ⟨i, e⟩ := min_eq I f in e.symm ▸ h i⟩
protected theorem wf : @well_founded cardinal.{u} (<) :=
⟨λ a, classical.by_contradiction $ λ h,
let ι := {c :cardinal // ¬ acc (<) c},
f : ι → cardinal := subtype.val,
⟨⟨c, hc⟩, hi⟩ := @min_eq ι ⟨⟨_, h⟩⟩ f in
hc (acc.intro _ (λ j ⟨_, h'⟩,
classical.by_contradiction $ λ hj, h' $
by have := min_le f ⟨j, hj⟩; rwa hi at this))⟩
instance has_wf : @has_well_founded cardinal.{u} := ⟨(<), cardinal.wf⟩
instance wo : @is_well_order cardinal.{u} (<) := ⟨cardinal.wf⟩
/-- The successor cardinal - the smallest cardinal greater than
`c`. This is not the same as `c + 1` except in the case of finite `c`. -/
def succ (c : cardinal) : cardinal :=
@min {c' // c < c'} ⟨⟨_, cantor _⟩⟩ subtype.val
theorem lt_succ_self (c : cardinal) : c < succ c :=
by cases min_eq _ _ with s e; rw [succ, e]; exact s.2
theorem succ_le {a b : cardinal} : succ a ≤ b ↔ a < b :=
⟨lt_of_lt_of_le (lt_succ_self _), λ h,
by exact min_le _ (subtype.mk b h)⟩
theorem lt_succ {a b : cardinal} : a < succ b ↔ a ≤ b :=
by rw [← not_le, succ_le, not_lt]
theorem add_one_le_succ (c : cardinal) : c + 1 ≤ succ c :=
begin
refine quot.induction_on c (λ α, _) (lt_succ_self c),
refine quot.induction_on (succ (quot.mk setoid.r α)) (λ β h, _),
cases h.left with f,
have : ¬ surjective f := λ hn,
ne_of_lt h (quotient.sound ⟨equiv.of_bijective ⟨f.inj, hn⟩⟩),
cases classical.not_forall.1 this with b nex,
refine ⟨⟨sum.rec (by exact f) _, _⟩⟩,
{ exact λ _, b },
{ intros a b h, rcases a with a|⟨⟨⟨⟩⟩⟩; rcases b with b|⟨⟨⟨⟩⟩⟩,
{ rw f.inj h },
{ exact nex.elim ⟨_, h⟩ },
{ exact nex.elim ⟨_, h.symm⟩ },
{ refl } }
end
/-- The indexed sum of cardinals is the cardinality of the
indexed disjoint union, i.e. sigma type. -/
def sum {ι} (f : ι → cardinal) : cardinal := mk Σ i, (f i).out
theorem le_sum {ι} (f : ι → cardinal) (i) : f i ≤ sum f :=
by rw ← quotient.out_eq (f i); exact
⟨⟨λ a, ⟨i, a⟩, λ a b h, eq_of_heq $ by injection h⟩⟩
@[simp] theorem sum_mk {ι} (f : ι → Type*) : sum (λ i, mk (f i)) = mk (Σ i, f i) :=
quot.sound ⟨equiv.sigma_congr_right $ λ i,
classical.choice $ quotient.exact $ quot.out_eq $ mk (f i)⟩
theorem sum_const (ι : Type u) (a : cardinal.{u}) : sum (λ _:ι, a) = mk ι * a :=
quotient.induction_on a $ λ α, by simp; exact
quotient.sound ⟨equiv.sigma_equiv_prod _ _⟩
theorem sum_le_sum {ι} (f g : ι → cardinal) (H : ∀ i, f i ≤ g i) : sum f ≤ sum g :=
⟨embedding.sigma_congr_right $ λ i, classical.choice $
by have := H i; rwa [← quot.out_eq (f i), ← quot.out_eq (g i)] at this⟩
/-- The indexed supremum of cardinals is the smallest cardinal above
everything in the family. -/
def sup {ι} (f : ι → cardinal) : cardinal :=
@min {c // ∀ i, f i ≤ c} ⟨⟨sum f, le_sum f⟩⟩ (λ a, a.1)
theorem le_sup {ι} (f : ι → cardinal) (i) : f i ≤ sup f :=
by dsimp [sup]; cases min_eq _ _ with c hc; rw hc; exact c.2 i
theorem sup_le {ι} (f : ι → cardinal) (a) : sup f ≤ a ↔ ∀ i, f i ≤ a :=
⟨λ h i, le_trans (le_sup _ _) h,
λ h, by dsimp [sup]; change a with (⟨a, h⟩:subtype _).1; apply min_le⟩
theorem sup_le_sup {ι} (f g : ι → cardinal) (H : ∀ i, f i ≤ g i) : sup f ≤ sup g :=
(sup_le _ _).2 $ λ i, le_trans (H i) (le_sup _ _)
theorem sup_le_sum {ι} (f : ι → cardinal) : sup f ≤ sum f :=
(sup_le _ _).2 $ le_sum _
theorem sum_le_sup {ι : Type u} (f : ι → cardinal.{u}) : sum f ≤ mk ι * sup.{u u} f :=
by rw ← sum_const; exact sum_le_sum _ _ (le_sup _)
/-- The indexed product of cardinals is the cardinality of the Pi type
(dependent product). -/
def prod {ι : Type u} (f : ι → cardinal) : cardinal := mk (Π i, (f i).out)
@[simp] theorem prod_mk {ι} (f : ι → Type*) : prod (λ i, mk (f i)) = mk (Π i, f i) :=
quot.sound ⟨equiv.Pi_congr_right $ λ i,
classical.choice $ quotient.exact $ mk_out $ mk (f i)⟩
theorem prod_const (ι : Type u) (a : cardinal.{u}) : prod (λ _:ι, a) = a ^ mk ι :=
quotient.induction_on a $ by simp
theorem prod_le_prod {ι} (f g : ι → cardinal) (H : ∀ i, f i ≤ g i) : prod f ≤ prod g :=
⟨embedding.Pi_congr_right $ λ i, classical.choice $
by have := H i; rwa [← mk_out (f i), ← mk_out (g i)] at this⟩
theorem prod_ne_zero {ι} (f : ι → cardinal) : prod f ≠ 0 ↔ ∀ i, f i ≠ 0 :=
begin
conv in (f _) {rw ← mk_out (f i)},
simp [prod, ne_zero_iff_nonempty, -mk_out, -ne.def],
exact ⟨λ ⟨F⟩ i, ⟨F i⟩, λ h, ⟨λ i, classical.choice (h i)⟩⟩,
end
theorem prod_eq_zero {ι} (f : ι → cardinal) : prod f = 0 ↔ ∃ i, f i = 0 :=
not_iff_not.1 $ by simpa using prod_ne_zero f
/-- The universe lift operation on cardinals -/
def lift (c : cardinal.{u}) : cardinal.{max u v} :=
quotient.lift_on c (λ α, ⟦ulift α⟧) $ λ α β ⟨e⟩,
quotient.sound ⟨equiv.ulift.trans $ e.trans equiv.ulift.symm⟩
theorem lift_mk (α) : lift.{u v} (mk α) = mk (ulift.{v u} α) := rfl
theorem lift_umax : lift.{u (max u v)} = lift.{u v} :=
funext $ λ a, quot.induction_on a $ λ α,
quotient.sound ⟨equiv.ulift.trans equiv.ulift.symm⟩
theorem lift_id' (a : cardinal) : lift a = a :=
quot.induction_on a $ λ α, quot.sound ⟨equiv.ulift⟩
@[simp] theorem lift_id : ∀ a, lift.{u u} a = a := lift_id'.{u u}
@[simp] theorem lift_lift (a : cardinal) : lift.{(max u v) w} (lift.{u v} a) = lift.{u (max v w)} a :=
quot.induction_on a $ λ α,
quotient.sound ⟨equiv.ulift.trans $ equiv.ulift.trans equiv.ulift.symm⟩
theorem lift_mk_le {α : Type u} {β : Type v} :
lift.{u (max v w)} (mk α) ≤ lift.{v (max u w)} (mk β) ↔ nonempty (α ↪ β) :=
⟨λ ⟨f⟩, ⟨embedding.congr equiv.ulift equiv.ulift f⟩,
λ ⟨f⟩, ⟨embedding.congr equiv.ulift.symm equiv.ulift.symm f⟩⟩
theorem lift_mk_eq {α : Type u} {β : Type v} :
lift.{u (max v w)} (mk α) = lift.{v (max u w)} (mk β) ↔ nonempty (α ≃ β) :=
quotient.eq.trans
⟨λ ⟨f⟩, ⟨equiv.ulift.symm.trans $ f.trans equiv.ulift⟩,
λ ⟨f⟩, ⟨equiv.ulift.trans $ f.trans equiv.ulift.symm⟩⟩
@[simp] theorem lift_le {a b : cardinal} : lift a ≤ lift b ↔ a ≤ b :=
quotient.induction_on₂ a b $ λ α β,
by rw ← lift_umax; exact lift_mk_le
@[simp] theorem lift_inj {a b : cardinal} : lift a = lift b ↔ a = b :=
by simp [le_antisymm_iff]
@[simp] theorem lift_lt {a b : cardinal} : lift a < lift b ↔ a < b :=
by simp [lt_iff_le_not_le, -not_le]
@[simp] theorem lift_zero : lift 0 = 0 :=
quotient.sound ⟨equiv.ulift.trans $ equiv.ulift.trans equiv.ulift.symm⟩
@[simp] theorem lift_one : lift 1 = 1 :=
quotient.sound ⟨equiv.ulift.trans $ equiv.ulift.trans equiv.ulift.symm⟩
@[simp] theorem lift_add (a b) : lift (a + b) = lift a + lift b :=
quotient.induction_on₂ a b $ λ α β,
quotient.sound ⟨equiv.ulift.trans (equiv.sum_congr equiv.ulift equiv.ulift).symm⟩
@[simp] theorem lift_mul (a b) : lift (a * b) = lift a * lift b :=
quotient.induction_on₂ a b $ λ α β,
quotient.sound ⟨equiv.ulift.trans (equiv.prod_congr equiv.ulift equiv.ulift).symm⟩
@[simp] theorem lift_power (a b) : lift (a ^ b) = lift a ^ lift b :=
quotient.induction_on₂ a b $ λ α β,
quotient.sound ⟨equiv.ulift.trans (equiv.arrow_congr equiv.ulift equiv.ulift).symm⟩
@[simp] theorem lift_two_power (a) : lift (2 ^ a) = 2 ^ lift a :=
by simp [bit0]
@[simp] theorem lift_min {ι I} (f : ι → cardinal) : lift (min I f) = min I (lift ∘ f) :=
le_antisymm (le_min.2 $ λ a, lift_le.2 $ min_le _ a) $
let ⟨i, e⟩ := min_eq I (lift ∘ f) in
by rw e; exact lift_le.2 (le_min.2 $ λ j, lift_le.1 $
by have := min_le (lift ∘ f) j; rwa e at this)
theorem lift_down {a : cardinal.{u}} {b : cardinal.{max u v}} :
b ≤ lift a → ∃ a', lift a' = b :=
quotient.induction_on₂ a b $ λ α β,
by dsimp; rw [← lift_id (mk β), ← lift_umax, ← lift_umax.{u v}, lift_mk_le]; exact
λ ⟨f⟩, ⟨mk (set.range f), eq.symm $ lift_mk_eq.2
⟨embedding.equiv_of_surjective
(embedding.cod_restrict _ f set.mem_range_self)
$ λ ⟨a, ⟨b, e⟩⟩, ⟨b, subtype.eq e⟩⟩⟩
theorem le_lift_iff {a : cardinal.{u}} {b : cardinal.{max u v}} :
b ≤ lift a ↔ ∃ a', lift a' = b ∧ a' ≤ a :=
⟨λ h, let ⟨a', e⟩ := lift_down h in ⟨a', e, lift_le.1 $ e.symm ▸ h⟩,
λ ⟨a', e, h⟩, e ▸ lift_le.2 h⟩
theorem lt_lift_iff {a : cardinal.{u}} {b : cardinal.{max u v}} :
b < lift a ↔ ∃ a', lift a' = b ∧ a' < a :=
⟨λ h, let ⟨a', e⟩ := lift_down (le_of_lt h) in
⟨a', e, lift_lt.1 $ e.symm ▸ h⟩,
λ ⟨a', e, h⟩, e ▸ lift_lt.2 h⟩
@[simp] theorem lift_succ (a) : lift (succ a) = succ (lift a) :=
le_antisymm
(le_of_not_gt $ λ h, begin
rcases lt_lift_iff.1 h with ⟨b, e, h⟩,
rw [lt_succ, ← lift_le, e] at h,
exact not_lt_of_le h (lt_succ_self _)
end)
(succ_le.2 $ lift_lt.2 $ lt_succ_self _)
/-- `ω` is the smallest infinite cardinal, also known as ℵ₀. -/
def omega : cardinal.{u} := lift (mk ℕ)
theorem omega_ne_zero : omega ≠ 0 :=
ne_zero_iff_nonempty.2 ⟨⟨0⟩⟩
theorem omega_pos : 0 < omega :=
pos_iff_ne_zero.2 omega_ne_zero
@[simp] theorem lift_omega : lift omega = omega := lift_lift _
@[simp] theorem mk_fin : ∀ (n : ℕ), mk (fin n) = n
| 0 := quotient.sound ⟨(equiv.empty_of_not_nonempty $
by exact λ ⟨h⟩, h.elim0).trans equiv.ulift.symm⟩
| (n+1) := by rw [nat.cast_succ, ← mk_fin]; exact
quotient.sound (fintype.card_eq.1 $ by simp)
@[simp] theorem lift_nat_cast (n : ℕ) : lift n = n :=
by induction n; simp *
theorem lift_mk_fin (n : ℕ) : lift (mk (fin n)) = n := by simp
theorem fintype_card (α : Type u) [fintype α] : mk α = fintype.card α :=
by rw [← lift_mk_fin.{u}, ← lift_id (mk α), lift_mk_eq.{u 0 u}];
exact fintype.card_eq.1 (by simp)
@[simp] theorem nat_cast_pow {m n : ℕ} : (@has_pow.pow _ _ _ m n : cardinal) = m ^ n :=
by induction n; simp [nat.pow_succ, -_root_.add_comm, power_add, *]
@[simp] theorem nat_cast_le {m n : ℕ} : (m : cardinal) ≤ n ↔ m ≤ n :=
by rw [← lift_mk_fin, ← lift_mk_fin, lift_le]; exact
⟨λ ⟨⟨f, hf⟩⟩, begin
have : _ = fintype.card _ := finset.card_image_of_injective finset.univ hf,
simp at this,
rw [← fintype.card_fin n, ← this],
exact finset.card_le_of_subset (finset.subset_univ _)
end,
λ h, ⟨⟨λ i, ⟨i.1, lt_of_lt_of_le i.2 h⟩, λ a b h,
have _, from fin.veq_of_eq h, fin.eq_of_veq this⟩⟩⟩
@[simp] theorem nat_cast_lt {m n : ℕ} : (m : cardinal) < n ↔ m < n :=
by simp [lt_iff_le_not_le, -not_le]
@[simp] theorem nat_cast_inj {m n : ℕ} : (m : cardinal) = n ↔ m = n :=
by simp [le_antisymm_iff]
@[simp] theorem nat_succ (n : ℕ) : succ n = n.succ :=
le_antisymm (succ_le.2 $ nat_cast_lt.2 $ nat.lt_succ_self _) (add_one_le_succ _)
@[simp] theorem succ_zero : succ 0 = 1 :=
by simpa using nat_succ 0
theorem cantor' (a) {b : cardinal} (hb : 1 < b) : a < b ^ a :=
by rw [← succ_le, (by simpa using nat_succ 1 : succ 1 = 2)] at hb;
exact lt_of_lt_of_le (cantor _) (power_le_power_right hb)
theorem one_le_iff_pos {c : cardinal} : 1 ≤ c ↔ 0 < c :=
by rw [← succ_zero, succ_le]
theorem one_le_iff_ne_zero {c : cardinal} : 1 ≤ c ↔ c ≠ 0 :=
by rw [one_le_iff_pos, pos_iff_ne_zero]
theorem nat_lt_omega (n : ℕ) : (n : cardinal.{u}) < omega :=
succ_le.1 $ by rw [nat_succ, ← lift_mk_fin, omega, lift_mk_le.{0 0 u}]; exact
⟨⟨fin.val, λ a b, fin.eq_of_veq⟩⟩
theorem one_lt_omega : 1 < omega :=
by simpa using nat_lt_omega 1
theorem lt_omega {c : cardinal.{u}} : c < omega ↔ ∃ n : ℕ, c = n :=
⟨λ h, begin
rcases lt_lift_iff.1 h with ⟨c, rfl, h'⟩,
rcases le_mk_iff_exists_set.1 h'.1 with ⟨S, rfl⟩,
suffices : finite S,
{ cases this, resetI,
existsi fintype.card S,
rw [← lift_nat_cast.{0 u}, lift_inj, fintype_card S] },
by_contra nf,
have P : ∀ (n : ℕ) (IH : ∀ i<n, S), ∃ a : S, ¬ ∃ y h, IH y h = a :=
λ n IH,
let g : {i | i < n} → S := λ ⟨i, h⟩, IH i h in
classical.not_forall.1 (λ h, nf
⟨fintype.of_surjective g (λ a, subtype.exists.2 (h a))⟩),
let F : ℕ → S := nat.lt_wf.fix (λ n IH, some (P n IH)),
refine not_le_of_lt h' ⟨⟨F, _⟩⟩,
suffices : ∀ (n : ℕ) (m < n), F m ≠ F n,
{ refine λ m n, not_imp_not.1 (λ ne, _),
rcases lt_trichotomy m n with h|h|h,
{ exact this n m h },
{ contradiction },
{ exact (this m n h).symm } },
intros n m h,
have := some_spec (P n (λ y _, F y)),
rw [← show F n = some (P n (λ y _, F y)),
from nat.lt_wf.fix_eq (λ n IH, some (P n IH)) n] at this,
exact λ e, this ⟨m, h, e⟩,
end, λ ⟨n, e⟩, e.symm ▸ nat_lt_omega _⟩
theorem omega_le {c : cardinal.{u}} : omega ≤ c ↔ ∀ n : ℕ, (n:cardinal) ≤ c :=
⟨λ h n, le_trans (le_of_lt (nat_lt_omega _)) h,
λ h, le_of_not_lt $ λ hn, begin
rcases lt_omega.1 hn with ⟨n, rfl⟩,
exact not_le_of_lt (nat.lt_succ_self _) (nat_cast_le.1 (h (n+1)))
end⟩
theorem lt_omega_iff_fintype {α : Type u} : mk α < omega ↔ nonempty (fintype α) :=
lt_omega.trans ⟨λ ⟨n, e⟩, begin
rw [← lift_mk_fin n] at e,
cases quotient.exact e with f,
exact ⟨fintype.of_equiv _ f.symm⟩
end, λ ⟨_⟩, by exactI ⟨_, fintype_card _⟩⟩
theorem lt_omega_iff_finite {α} {S : set α} : mk S < omega ↔ finite S :=
lt_omega_iff_fintype
theorem add_lt_omega {a b : cardinal} (ha : a < omega) (hb : b < omega) : a + b < omega :=
match a, b, lt_omega.1 ha, lt_omega.1 hb with
| _, _, ⟨m, rfl⟩, ⟨n, rfl⟩ := by rw [← nat.cast_add]; apply nat_lt_omega
end
theorem mul_lt_omega {a b : cardinal} (ha : a < omega) (hb : b < omega) : a * b < omega :=
match a, b, lt_omega.1 ha, lt_omega.1 hb with
| _, _, ⟨m, rfl⟩, ⟨n, rfl⟩ := by rw [← nat.cast_mul]; apply nat_lt_omega
end
theorem power_lt_omega {a b : cardinal} (ha : a < omega) (hb : b < omega) : a ^ b < omega :=
match a, b, lt_omega.1 ha, lt_omega.1 hb with
| _, _, ⟨m, rfl⟩, ⟨n, rfl⟩ := by rw [← nat_cast_pow]; apply nat_lt_omega
end
/-- König's theorem -/
theorem sum_lt_prod {ι} (f g : ι → cardinal) (H : ∀ i, f i < g i) : sum f < prod g :=
lt_of_not_ge $ λ ⟨F⟩, begin
have : inhabited (Π (i : ι), (g i).out),
{ refine ⟨λ i, classical.choice $ ne_zero_iff_nonempty.1 _⟩,
rw mk_out,
exact ne_of_gt (lt_of_le_of_lt (zero_le _) (H i)) }, resetI,
let G := inv_fun F,
have sG : surjective G := inv_fun_surjective F.2,
have : ∀ i, ¬ ∀ b, ∃ a, G ⟨i, a⟩ i = b,
{ refine λ i h, not_le_of_lt (H i) _,
rw [← mk_out (f i), ← mk_out (g i)],
exact ⟨embedding.of_surjective h⟩ },
simp [classical.not_forall] at this,
exact let ⟨C, hc⟩ := axiom_of_choice this, ⟨⟨i, a⟩, h⟩ := sG C in
hc i a (congr_fun h _),
end
end cardinal
|
49d5c6e2705d645641463a5390c1acf58cd1d07b | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /test/lint_coe_t.lean | 9177ae4222fcd1fb5aa832c985463ac9b6035c83 | [
"Apache-2.0"
] | permissive | jjgarzella/mathlib | 96a345378c4e0bf26cf604aed84f90329e4896a2 | 395d8716c3ad03747059d482090e2bb97db612c8 | refs/heads/master | 1,686,480,124,379 | 1,625,163,323,000 | 1,625,163,323,000 | 281,190,421 | 2 | 0 | Apache-2.0 | 1,595,268,170,000 | 1,595,268,169,000 | null | UTF-8 | Lean | false | false | 1,088 | lean | import tactic.lint
open tactic
-- bad, because every iteration of tc search will loop, generating nested quotients
section
local attribute [instance]
def a_to_quot {α} (R : setoid α) : has_coe α (quotient R) := ⟨quotient.mk⟩
run_cmd do
d ← get_decl ``a_to_quot,
some _ ← linter.has_coe_variable.test d,
d ← get_decl ``has_coe_to_fun,
some s ← fails_quickly 3000 d,
guard $ "maximum class-instance resolution depth has been reached".is_prefix_of s
end
-- good, because the term gets smaller in every iteration
noncomputable instance quot_to_a {α} (R : setoid α) : has_coe (quotient R) α :=
⟨λ q, quot.rec_on q (λ a, classical.choice ⟨a⟩) (by cc)⟩
run_cmd do
decl ← get_decl ``quot_to_a,
-- linter does not complain
none ← linter.has_coe_variable.test decl,
skip
-- bad, because it introduces a metavariable
section
local attribute [instance]
def int_to_a {α} [inhabited α] : has_coe ℤ α := ⟨λ _, default _⟩
run_cmd do
decl ← get_decl ``int_to_a,
-- linter does not complain
some _ ← linter.has_coe_variable.test decl,
skip
end
|
8e0a9d2c057bd425c1f556774539e0d44cb9029c | 4d2583807a5ac6caaffd3d7a5f646d61ca85d532 | /src/ring_theory/fractional_ideal.lean | eff5b0d9314e9155ead0920ccedd898ff8d18062 | [
"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 | 50,282 | lean | /-
Copyright (c) 2020 Anne Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Anne Baanen, Filippo A. E. Nuccio
-/
import ring_theory.localization
import ring_theory.noetherian
import ring_theory.principal_ideal_domain
import tactic.field_simp
/-!
# Fractional ideals
This file defines fractional ideals of an integral domain and proves basic facts about them.
## Main definitions
Let `S` be a submonoid of an integral domain `R`, `P` the localization of `R` at `S`, and `f` the
natural ring hom from `R` to `P`.
* `is_fractional` defines which `R`-submodules of `P` are fractional ideals
* `fractional_ideal S P` is the type of fractional ideals in `P`
* `has_coe_t (ideal R) (fractional_ideal S P)` instance
* `comm_semiring (fractional_ideal S P)` instance:
the typical ideal operations generalized to fractional ideals
* `lattice (fractional_ideal S P)` instance
* `map` is the pushforward of a fractional ideal along an algebra morphism
Let `K` be the localization of `R` at `R⁰ = R \ {0}` (i.e. the field of fractions).
* `fractional_ideal R⁰ K` is the type of fractional ideals in the field of fractions
* `has_div (fractional_ideal R⁰ K)` instance:
the ideal quotient `I / J` (typically written $I : J$, but a `:` operator cannot be defined)
## Main statements
* `mul_left_mono` and `mul_right_mono` state that ideal multiplication is monotone
* `prod_one_self_div_eq` states that `1 / I` is the inverse of `I` if one exists
* `is_noetherian` states that very fractional ideal of a noetherian integral domain is noetherian
## Implementation notes
Fractional ideals are considered equal when they contain the same elements,
independent of the denominator `a : R` such that `a I ⊆ R`.
Thus, we define `fractional_ideal` to be the subtype of the predicate `is_fractional`,
instead of having `fractional_ideal` be a structure of which `a` is a field.
Most definitions in this file specialize operations from submodules to fractional ideals,
proving that the result of this operation is fractional if the input is fractional.
Exceptions to this rule are defining `(+) := (⊔)` and `⊥ := 0`,
in order to re-use their respective proof terms.
We can still use `simp` to show `↑I + ↑J = ↑(I + J)` and `↑⊥ = ↑0`.
Many results in fact do not need that `P` is a localization, only that `P` is an
`R`-algebra. We omit the `is_localization` parameter whenever this is practical.
Similarly, we don't assume that the localization is a field until we need it to
define ideal quotients. When this assumption is needed, we replace `S` with `R⁰`,
making the localization a field.
## References
* https://en.wikipedia.org/wiki/Fractional_ideal
## Tags
fractional ideal, fractional ideals, invertible ideal
-/
open is_localization
open_locale pointwise
open_locale non_zero_divisors
section defs
variables {R : Type*} [comm_ring R] {S : submonoid R} {P : Type*} [comm_ring P]
variables [algebra R P]
variables (S)
/-- A submodule `I` is a fractional ideal if `a I ⊆ R` for some `a ≠ 0`. -/
def is_fractional (I : submodule R P) :=
∃ a ∈ S, ∀ b ∈ I, is_integer R (a • b)
variables (S P)
/-- The fractional ideals of a domain `R` are ideals of `R` divided by some `a ∈ R`.
More precisely, let `P` be a localization of `R` at some submonoid `S`,
then a fractional ideal `I ⊆ P` is an `R`-submodule of `P`,
such that there is a nonzero `a : R` with `a I ⊆ R`.
-/
def fractional_ideal :=
{I : submodule R P // is_fractional S I}
end defs
namespace fractional_ideal
open set
open submodule
variables {R : Type*} [comm_ring R] {S : submonoid R} {P : Type*} [comm_ring P]
variables [algebra R P] [loc : is_localization S P]
/-- Map a fractional ideal `I` to a submodule by forgetting that `∃ a, a I ⊆ R`.
This coercion is typically called `coe_to_submodule` in lemma names
(or `coe` when the coercion is clear from the context),
not to be confused with `is_localization.coe_submodule : ideal R → submodule R P`
(which we use to define `coe : ideal R → fractional_ideal S P`,
referred to as `coe_ideal` in theorem names).
-/
instance : has_coe (fractional_ideal S P) (submodule R P) := ⟨λ I, I.val⟩
protected lemma is_fractional (I : fractional_ideal S P) :
is_fractional S (I : submodule R P) :=
I.prop
section set_like
instance : set_like (fractional_ideal S P) P :=
{ coe := λ I, ↑(I : submodule R P),
coe_injective' := set_like.coe_injective.comp subtype.coe_injective }
@[simp] lemma mem_coe {I : fractional_ideal S P} {x : P} :
x ∈ (I : submodule R P) ↔ x ∈ I :=
iff.rfl
@[ext] lemma ext {I J : fractional_ideal S P} : (∀ x, x ∈ I ↔ x ∈ J) → I = J := set_like.ext
/-- Copy of a `fractional_ideal` with a new underlying set equal to the old one.
Useful to fix definitional equalities. -/
protected def copy (p : fractional_ideal S P) (s : set P) (hs : s = ↑p) : fractional_ideal S P :=
⟨submodule.copy p s hs, by { convert p.is_fractional, ext, simp only [hs], refl }⟩
end set_like
@[simp] lemma val_eq_coe (I : fractional_ideal S P) : I.val = I := rfl
@[simp, norm_cast] lemma coe_mk (I : submodule R P) (hI : is_fractional S I) :
(subtype.mk I hI : submodule R P) = I := rfl
lemma coe_to_submodule_injective :
function.injective (coe : fractional_ideal S P → submodule R P) :=
subtype.coe_injective
lemma is_fractional_of_le_one (I : submodule R P) (h : I ≤ 1) :
is_fractional S I :=
begin
use [1, S.one_mem],
intros b hb,
rw one_smul,
obtain ⟨b', b'_mem, rfl⟩ := h hb,
exact set.mem_range_self b',
end
lemma is_fractional_of_le {I : submodule R P} {J : fractional_ideal S P}
(hIJ : I ≤ J) : is_fractional S I :=
begin
obtain ⟨a, a_mem, ha⟩ := J.is_fractional,
use [a, a_mem],
intros b b_mem,
exact ha b (hIJ b_mem)
end
/-- Map an ideal `I` to a fractional ideal by forgetting `I` is integral.
This is a bundled version of `is_localization.coe_submodule : ideal R → submodule R P`,
which is not to be confused with the `coe : fractional_ideal S P → submodule R P`,
also called `coe_to_submodule` in theorem names.
This map is available as a ring hom, called `fractional_ideal.coe_ideal_hom`.
-/
-- Is a `coe_t` rather than `coe` to speed up failing inference, see library note [use has_coe_t]
instance coe_to_fractional_ideal : has_coe_t (ideal R) (fractional_ideal S P) :=
⟨λ I, ⟨coe_submodule P I, is_fractional_of_le_one _
(by simpa using coe_submodule_mono P (le_top : I ≤ ⊤))⟩⟩
@[simp, norm_cast] lemma coe_coe_ideal (I : ideal R) :
((I : fractional_ideal S P) : submodule R P) = coe_submodule P I := rfl
variables (S)
@[simp] lemma mem_coe_ideal {x : P} {I : ideal R} :
x ∈ (I : fractional_ideal S P) ↔ ∃ x', x' ∈ I ∧ algebra_map R P x' = x :=
mem_coe_submodule _ _
lemma mem_coe_ideal_of_mem {x : R} {I : ideal R} (hx : x ∈ I) :
algebra_map R P x ∈ (I : fractional_ideal S P) :=
(mem_coe_ideal S).mpr ⟨x, hx, rfl⟩
lemma coe_ideal_le_coe_ideal' [is_localization S P] (h : S ≤ non_zero_divisors R)
{I J : ideal R} : (I : fractional_ideal S P) ≤ J ↔ I ≤ J :=
coe_submodule_le_coe_submodule h
@[simp] lemma coe_ideal_le_coe_ideal (K : Type*) [comm_ring K] [algebra R K] [is_fraction_ring R K]
{I J : ideal R} : (I : fractional_ideal R⁰ K) ≤ J ↔ I ≤ J :=
is_fraction_ring.coe_submodule_le_coe_submodule
instance : has_zero (fractional_ideal S P) := ⟨(0 : ideal R)⟩
@[simp] lemma mem_zero_iff {x : P} : x ∈ (0 : fractional_ideal S P) ↔ x = 0 :=
⟨(λ ⟨x', x'_mem_zero, x'_eq_x⟩,
have x'_eq_zero : x' = 0 := x'_mem_zero,
by simp [x'_eq_x.symm, x'_eq_zero]),
(λ hx, ⟨0, rfl, by simp [hx]⟩)⟩
variables {S}
@[simp, norm_cast] lemma coe_zero : ↑(0 : fractional_ideal S P) = (⊥ : submodule R P) :=
submodule.ext $ λ _, mem_zero_iff S
@[simp, norm_cast] lemma coe_to_fractional_ideal_bot : ((⊥ : ideal R) : fractional_ideal S P) = 0 :=
rfl
variables (P)
include loc
@[simp] lemma exists_mem_to_map_eq {x : R} {I : ideal R} (h : S ≤ non_zero_divisors R) :
(∃ x', x' ∈ I ∧ algebra_map R P x' = algebra_map R P x) ↔ x ∈ I :=
⟨λ ⟨x', hx', eq⟩, is_localization.injective _ h eq ▸ hx', λ h, ⟨x, h, rfl⟩⟩
variables {P}
lemma coe_to_fractional_ideal_injective (h : S ≤ non_zero_divisors R) :
function.injective (coe : ideal R → fractional_ideal S P) :=
λ I J heq, have
∀ (x : R), algebra_map R P x ∈ (I : fractional_ideal S P) ↔
algebra_map R P x ∈ (J : fractional_ideal S P) :=
λ x, heq ▸ iff.rfl,
ideal.ext (by simpa only [mem_coe_ideal, exists_prop, exists_mem_to_map_eq P h] using this)
lemma coe_to_fractional_ideal_eq_zero {I : ideal R} (hS : S ≤ non_zero_divisors R) :
(I : fractional_ideal S P) = 0 ↔ I = (⊥ : ideal R) :=
⟨λ h, coe_to_fractional_ideal_injective hS h,
λ h, by rw [h, coe_to_fractional_ideal_bot]⟩
lemma coe_to_fractional_ideal_ne_zero {I : ideal R} (hS : S ≤ non_zero_divisors R) :
(I : fractional_ideal S P) ≠ 0 ↔ I ≠ (⊥ : ideal R) :=
not_iff_not.mpr (coe_to_fractional_ideal_eq_zero hS)
omit loc
lemma coe_to_submodule_eq_bot {I : fractional_ideal S P} :
(I : submodule R P) = ⊥ ↔ I = 0 :=
⟨λ h, coe_to_submodule_injective (by simp [h]),
λ h, by simp [h]⟩
lemma coe_to_submodule_ne_bot {I : fractional_ideal S P} :
↑I ≠ (⊥ : submodule R P) ↔ I ≠ 0 :=
not_iff_not.mpr coe_to_submodule_eq_bot
instance : inhabited (fractional_ideal S P) := ⟨0⟩
instance : has_one (fractional_ideal S P) :=
⟨(⊤ : ideal R)⟩
variables (S)
@[simp, norm_cast] lemma coe_ideal_top : ((⊤ : ideal R) : fractional_ideal S P) = 1 :=
rfl
lemma mem_one_iff {x : P} : x ∈ (1 : fractional_ideal S P) ↔ ∃ x' : R, algebra_map R P x' = x :=
iff.intro (λ ⟨x', _, h⟩, ⟨x', h⟩) (λ ⟨x', h⟩, ⟨x', ⟨⟩, h⟩)
lemma coe_mem_one (x : R) : algebra_map R P x ∈ (1 : fractional_ideal S P) :=
(mem_one_iff S).mpr ⟨x, rfl⟩
lemma one_mem_one : (1 : P) ∈ (1 : fractional_ideal S P) :=
(mem_one_iff S).mpr ⟨1, ring_hom.map_one _⟩
variables {S}
/-- `(1 : fractional_ideal S P)` is defined as the R-submodule `f(R) ≤ P`.
However, this is not definitionally equal to `1 : submodule R P`,
which is proved in the actual `simp` lemma `coe_one`. -/
lemma coe_one_eq_coe_submodule_top :
↑(1 : fractional_ideal S P) = coe_submodule P (⊤ : ideal R) :=
rfl
@[simp, norm_cast] lemma coe_one :
(↑(1 : fractional_ideal S P) : submodule R P) = 1 :=
by rw [coe_one_eq_coe_submodule_top, coe_submodule_top]
section lattice
/-!
### `lattice` section
Defines the order on fractional ideals as inclusion of their underlying sets,
and ports the lattice structure on submodules to fractional ideals.
-/
@[simp] lemma coe_le_coe {I J : fractional_ideal S P} :
(I : submodule R P) ≤ (J : submodule R P) ↔ I ≤ J :=
iff.rfl
lemma zero_le (I : fractional_ideal S P) : 0 ≤ I :=
begin
intros x hx,
convert submodule.zero_mem _,
simpa using hx
end
instance order_bot : order_bot (fractional_ideal S P) :=
{ bot := 0,
bot_le := zero_le }
@[simp] lemma bot_eq_zero : (⊥ : fractional_ideal S P) = 0 :=
rfl
@[simp] lemma le_zero_iff {I : fractional_ideal S P} : I ≤ 0 ↔ I = 0 :=
le_bot_iff
lemma eq_zero_iff {I : fractional_ideal S P} : I = 0 ↔ (∀ x ∈ I, x = (0 : P)) :=
⟨ (λ h x hx, by simpa [h, mem_zero_iff] using hx),
(λ h, le_bot_iff.mp (λ x hx, (mem_zero_iff S).mpr (h x hx))) ⟩
lemma fractional_sup (I J : fractional_ideal S P) : is_fractional S (I ⊔ J : submodule R P) :=
begin
rcases I.is_fractional with ⟨aI, haI, hI⟩,
rcases J.is_fractional with ⟨aJ, haJ, hJ⟩,
use aI * aJ,
use S.mul_mem haI haJ,
intros b hb,
rcases mem_sup.mp hb with ⟨bI, hbI, bJ, hbJ, rfl⟩,
rw smul_add,
apply is_integer_add,
{ rw [mul_smul, smul_comm],
exact is_integer_smul (hI bI hbI), },
{ rw mul_smul,
exact is_integer_smul (hJ bJ hbJ) }
end
lemma fractional_inf (I J : fractional_ideal S P) : is_fractional S (I ⊓ J : submodule R P) :=
begin
rcases I.is_fractional with ⟨aI, haI, hI⟩,
use aI,
use haI,
intros b hb,
rcases mem_inf.mp hb with ⟨hbI, hbJ⟩,
exact hI b hbI
end
instance lattice : lattice (fractional_ideal S P) :=
{ inf := λ I J, ⟨I ⊓ J, fractional_inf I J⟩,
sup := λ I J, ⟨I ⊔ J, fractional_sup I J⟩,
inf_le_left := λ I J, show (I ⊓ J : submodule R P) ≤ I, from inf_le_left,
inf_le_right := λ I J, show (I ⊓ J : submodule R P) ≤ J, from inf_le_right,
le_inf := λ I J K hIJ hIK, show (I : submodule R P) ≤ J ⊓ K, from le_inf hIJ hIK,
le_sup_left := λ I J, show (I : submodule R P) ≤ I ⊔ J, from le_sup_left,
le_sup_right := λ I J, show (J : submodule R P) ≤ I ⊔ J, from le_sup_right,
sup_le := λ I J K hIK hJK, show (I ⊔ J : submodule R P) ≤ K, from sup_le hIK hJK,
..set_like.partial_order }
instance : semilattice_sup_bot (fractional_ideal S P) :=
{ ..fractional_ideal.order_bot, ..fractional_ideal.lattice }
end lattice
section semiring
instance : has_add (fractional_ideal S P) := ⟨(⊔)⟩
@[simp]
lemma sup_eq_add (I J : fractional_ideal S P) : I ⊔ J = I + J := rfl
@[simp, norm_cast]
lemma coe_add (I J : fractional_ideal S P) : (↑(I + J) : submodule R P) = I + J := rfl
@[simp, norm_cast]
lemma coe_ideal_sup (I J : ideal R) : ↑(I ⊔ J) = (I + J : fractional_ideal S P) :=
coe_to_submodule_injective $ coe_submodule_sup _ _ _
lemma fractional_mul (I J : fractional_ideal S P) : is_fractional S (I * J : submodule R P) :=
begin
rcases I with ⟨I, aI, haI, hI⟩,
rcases J with ⟨J, aJ, haJ, hJ⟩,
use aI * aJ,
use S.mul_mem haI haJ,
intros b hb,
apply submodule.mul_induction_on hb,
{ intros m hm n hn,
obtain ⟨n', hn'⟩ := hJ n hn,
rw [mul_smul, mul_comm m, ← smul_mul_assoc, ← hn', ← algebra.smul_def],
apply hI,
exact submodule.smul_mem _ _ hm },
{ rw smul_zero,
exact ⟨0, ring_hom.map_zero _⟩ },
{ intros x y hx hy,
rw smul_add,
apply is_integer_add hx hy },
{ intros r x hx,
rw smul_comm,
exact is_integer_smul hx },
end
/-- `fractional_ideal.mul` is the product of two fractional ideals,
used to define the `has_mul` instance.
This is only an auxiliary definition: the preferred way of writing `I.mul J` is `I * J`.
Elaborated terms involving `fractional_ideal` tend to grow quite large,
so by making definitions irreducible, we hope to avoid deep unfolds.
-/
@[irreducible]
def mul (I J : fractional_ideal S P) : fractional_ideal S P :=
⟨I * J, fractional_mul I J⟩
local attribute [semireducible] mul
instance : has_mul (fractional_ideal S P) := ⟨λ I J, mul I J⟩
@[simp] lemma mul_eq_mul (I J : fractional_ideal S P) : mul I J = I * J := rfl
@[simp, norm_cast]
lemma coe_mul (I J : fractional_ideal S P) : (↑(I * J) : submodule R P) = I * J := rfl
@[simp, norm_cast]
lemma coe_ideal_mul (I J : ideal R) : (↑(I * J) : fractional_ideal S P) = I * J :=
coe_to_submodule_injective $ coe_submodule_mul _ _ _
lemma mul_left_mono (I : fractional_ideal S P) : monotone ((*) I) :=
λ J J' h, mul_le.mpr (λ x hx y hy, mul_mem_mul hx (h hy))
lemma mul_right_mono (I : fractional_ideal S P) : monotone (λ J, J * I) :=
λ J J' h, mul_le.mpr (λ x hx y hy, mul_mem_mul (h hx) hy)
lemma mul_mem_mul {I J : fractional_ideal S P} {i j : P} (hi : i ∈ I) (hj : j ∈ J) :
i * j ∈ I * J := submodule.mul_mem_mul hi hj
lemma mul_le {I J K : fractional_ideal S P} :
I * J ≤ K ↔ (∀ (i ∈ I) (j ∈ J), i * j ∈ K) :=
submodule.mul_le
@[elab_as_eliminator] protected theorem mul_induction_on
{I J : fractional_ideal S P}
{C : P → Prop} {r : P} (hr : r ∈ I * J)
(hm : ∀ (i ∈ I) (j ∈ J), C (i * j))
(h0 : C 0) (ha : ∀ x y, C x → C y → C (x + y))
(hs : ∀ (r : R) x, C x → C (r • x)) : C r :=
submodule.mul_induction_on hr hm h0 ha hs
instance comm_semiring : comm_semiring (fractional_ideal S P) :=
{ add_assoc := λ I J K, sup_assoc,
add_comm := λ I J, sup_comm,
add_zero := λ I, sup_bot_eq,
zero_add := λ I, bot_sup_eq,
mul_assoc := λ I J K, coe_to_submodule_injective (submodule.mul_assoc _ _ _),
mul_comm := λ I J, coe_to_submodule_injective (submodule.mul_comm _ _),
mul_one := λ I, begin
ext,
split; intro h,
{ apply mul_le.mpr _ h,
rintros x hx y ⟨y', y'_mem_R, rfl⟩,
convert submodule.smul_mem _ y' hx,
rw [mul_comm, eq_comm],
exact algebra.smul_def y' x },
{ have : x * 1 ∈ (I * 1) := mul_mem_mul h (one_mem_one _),
rwa [mul_one] at this }
end,
one_mul := λ I, begin
ext,
split; intro h,
{ apply mul_le.mpr _ h,
rintros x ⟨x', x'_mem_R, rfl⟩ y hy,
convert submodule.smul_mem _ x' hy,
rw eq_comm,
exact algebra.smul_def x' y },
{ have : 1 * x ∈ (1 * I) := mul_mem_mul (one_mem_one _) h,
rwa one_mul at this }
end,
mul_zero := λ I, eq_zero_iff.mpr (λ x hx, submodule.mul_induction_on hx
(λ x hx y hy, by simp [(mem_zero_iff S).mp hy])
rfl
(λ x y hx hy, by simp [hx, hy])
(λ r x hx, by simp [hx])),
zero_mul := λ I, eq_zero_iff.mpr (λ x hx, submodule.mul_induction_on hx
(λ x hx y hy, by simp [(mem_zero_iff S).mp hx])
rfl
(λ x y hx hy, by simp [hx, hy])
(λ r x hx, by simp [hx])),
left_distrib := λ I J K, coe_to_submodule_injective (mul_add _ _ _),
right_distrib := λ I J K, coe_to_submodule_injective (add_mul _ _ _),
..fractional_ideal.has_zero S,
..fractional_ideal.has_add,
..fractional_ideal.has_one,
..fractional_ideal.has_mul }
section order
lemma add_le_add_left {I J : fractional_ideal S P} (hIJ : I ≤ J) (J' : fractional_ideal S P) :
J' + I ≤ J' + J :=
sup_le_sup_left hIJ J'
lemma mul_le_mul_left {I J : fractional_ideal S P} (hIJ : I ≤ J) (J' : fractional_ideal S P) :
J' * I ≤ J' * J :=
mul_le.mpr (λ k hk j hj, mul_mem_mul hk (hIJ hj))
lemma le_self_mul_self {I : fractional_ideal S P} (hI: 1 ≤ I) : I ≤ I * I :=
begin
convert mul_left_mono I hI,
exact (mul_one I).symm
end
lemma mul_self_le_self {I : fractional_ideal S P} (hI: I ≤ 1) : I * I ≤ I :=
begin
convert mul_left_mono I hI,
exact (mul_one I).symm
end
lemma coe_ideal_le_one {I : ideal R} : (I : fractional_ideal S P) ≤ 1 :=
λ x hx, let ⟨y, _, hy⟩ := (fractional_ideal.mem_coe_ideal S).mp hx
in (fractional_ideal.mem_one_iff S).mpr ⟨y, hy⟩
lemma le_one_iff_exists_coe_ideal {J : fractional_ideal S P} :
J ≤ (1 : fractional_ideal S P) ↔ ∃ (I : ideal R), ↑I = J :=
begin
split,
{ intro hJ,
refine ⟨⟨{x : R | algebra_map R P x ∈ J}, _, _, _⟩, _⟩,
{ rw [mem_set_of_eq, ring_hom.map_zero],
exact J.val.zero_mem },
{ intros a b ha hb,
rw [mem_set_of_eq, ring_hom.map_add],
exact J.val.add_mem ha hb },
{ intros c x hx,
rw [smul_eq_mul, mem_set_of_eq, ring_hom.map_mul, ← algebra.smul_def],
exact J.val.smul_mem c hx },
{ ext x,
split,
{ rintros ⟨y, hy, eq_y⟩,
rwa ← eq_y },
{ intro hx,
obtain ⟨y, eq_x⟩ := (fractional_ideal.mem_one_iff S).mp (hJ hx),
rw ← eq_x at *,
exact ⟨y, hx, rfl⟩ } } },
{ rintro ⟨I, hI⟩,
rw ← hI,
apply coe_ideal_le_one },
end
variables (S P)
/-- `coe_ideal_hom (S : submonoid R) P` is `coe : ideal R → fractional_ideal S P` as a ring hom -/
@[simps]
def coe_ideal_hom : ideal R →+* fractional_ideal S P :=
{ to_fun := coe,
map_add' := coe_ideal_sup,
map_mul' := coe_ideal_mul,
map_one' := by rw [ideal.one_eq_top, coe_ideal_top],
map_zero' := coe_to_fractional_ideal_bot }
end order
variables {P' : Type*} [comm_ring P'] [algebra R P'] [loc' : is_localization S P']
variables {P'' : Type*} [comm_ring P''] [algebra R P''] [loc'' : is_localization S P'']
lemma fractional_map (g : P →ₐ[R] P') (I : fractional_ideal S P) :
is_fractional S (submodule.map g.to_linear_map I) :=
begin
rcases I with ⟨I, a, a_nonzero, hI⟩,
use [a, a_nonzero],
intros b hb,
obtain ⟨b', b'_mem, hb'⟩ := submodule.mem_map.mp hb,
obtain ⟨x, hx⟩ := hI b' b'_mem,
use x,
erw [←g.commutes, hx, g.map_smul, hb']
end
/-- `I.map g` is the pushforward of the fractional ideal `I` along the algebra morphism `g` -/
def map (g : P →ₐ[R] P') :
fractional_ideal S P → fractional_ideal S P' :=
λ I, ⟨submodule.map g.to_linear_map I, fractional_map g I⟩
@[simp, norm_cast] lemma coe_map (g : P →ₐ[R] P') (I : fractional_ideal S P) :
↑(map g I) = submodule.map g.to_linear_map I := rfl
@[simp] lemma mem_map {I : fractional_ideal S P} {g : P →ₐ[R] P'}
{y : P'} : y ∈ I.map g ↔ ∃ x, x ∈ I ∧ g x = y :=
submodule.mem_map
variables (I J : fractional_ideal S P) (g : P →ₐ[R] P')
@[simp] lemma map_id : I.map (alg_hom.id _ _) = I :=
coe_to_submodule_injective (submodule.map_id I)
@[simp] lemma map_comp (g' : P' →ₐ[R] P'') :
I.map (g'.comp g) = (I.map g).map g' :=
coe_to_submodule_injective (submodule.map_comp g.to_linear_map g'.to_linear_map I)
@[simp, norm_cast] lemma map_coe_ideal (I : ideal R) :
(I : fractional_ideal S P).map g = I :=
begin
ext x,
simp only [mem_coe_ideal],
split,
{ rintro ⟨_, ⟨y, hy, rfl⟩, rfl⟩,
exact ⟨y, hy, (g.commutes y).symm⟩ },
{ rintro ⟨y, hy, rfl⟩,
exact ⟨_, ⟨y, hy, rfl⟩, g.commutes y⟩ },
end
@[simp] lemma map_one :
(1 : fractional_ideal S P).map g = 1 :=
map_coe_ideal g ⊤
@[simp] lemma map_zero :
(0 : fractional_ideal S P).map g = 0 :=
map_coe_ideal g 0
@[simp] lemma map_add : (I + J).map g = I.map g + J.map g :=
coe_to_submodule_injective (submodule.map_sup _ _ _)
@[simp] lemma map_mul : (I * J).map g = I.map g * J.map g :=
coe_to_submodule_injective (submodule.map_mul _ _ _)
@[simp] lemma map_map_symm (g : P ≃ₐ[R] P') :
(I.map (g : P →ₐ[R] P')).map (g.symm : P' →ₐ[R] P) = I :=
by rw [←map_comp, g.symm_comp, map_id]
@[simp] lemma map_symm_map (I : fractional_ideal S P') (g : P ≃ₐ[R] P') :
(I.map (g.symm : P' →ₐ[R] P)).map (g : P →ₐ[R] P') = I :=
by rw [←map_comp, g.comp_symm, map_id]
lemma map_mem_map {f : P →ₐ[R] P'} (h : function.injective f) {x : P} {I : fractional_ideal S P} :
f x ∈ map f I ↔ x ∈ I :=
mem_map.trans ⟨λ ⟨x', hx', x'_eq⟩, h x'_eq ▸ hx', λ h, ⟨x, h, rfl⟩⟩
lemma map_injective (f : P →ₐ[R] P') (h : function.injective f) :
function.injective (map f : fractional_ideal S P → fractional_ideal S P') :=
λ I J hIJ, fractional_ideal.ext (λ x, (fractional_ideal.map_mem_map h).symm.trans
(hIJ.symm ▸ fractional_ideal.map_mem_map h))
/-- If `g` is an equivalence, `map g` is an isomorphism -/
def map_equiv (g : P ≃ₐ[R] P') :
fractional_ideal S P ≃+* fractional_ideal S P' :=
{ to_fun := map g,
inv_fun := map g.symm,
map_add' := λ I J, map_add I J _,
map_mul' := λ I J, map_mul I J _,
left_inv := λ I, by { rw [←map_comp, alg_equiv.symm_comp, map_id] },
right_inv := λ I, by { rw [←map_comp, alg_equiv.comp_symm, map_id] } }
@[simp] lemma coe_fun_map_equiv (g : P ≃ₐ[R] P') :
(map_equiv g : fractional_ideal S P → fractional_ideal S P') = map g :=
rfl
@[simp] lemma map_equiv_apply (g : P ≃ₐ[R] P') (I : fractional_ideal S P) :
map_equiv g I = map ↑g I := rfl
@[simp] lemma map_equiv_symm (g : P ≃ₐ[R] P') :
((map_equiv g).symm : fractional_ideal S P' ≃+* _) = map_equiv g.symm := rfl
@[simp] lemma map_equiv_refl :
map_equiv alg_equiv.refl = ring_equiv.refl (fractional_ideal S P) :=
ring_equiv.ext (λ x, by simp)
lemma is_fractional_span_iff {s : set P} :
is_fractional S (span R s) ↔ ∃ a ∈ S, ∀ (b : P), b ∈ s → is_integer R (a • b) :=
⟨λ ⟨a, a_mem, h⟩, ⟨a, a_mem, λ b hb, h b (subset_span hb)⟩,
λ ⟨a, a_mem, h⟩, ⟨a, a_mem, λ b hb, span_induction hb
h
(by { rw smul_zero, exact is_integer_zero })
(λ x y hx hy, by { rw smul_add, exact is_integer_add hx hy })
(λ s x hx, by { rw smul_comm, exact is_integer_smul hx })⟩⟩
include loc
lemma is_fractional_of_fg {I : submodule R P} (hI : I.fg) :
is_fractional S I :=
begin
rcases hI with ⟨I, rfl⟩,
rcases exist_integer_multiples_of_finset S I with ⟨⟨s, hs1⟩, hs⟩,
rw is_fractional_span_iff,
exact ⟨s, hs1, hs⟩,
end
omit loc
lemma mem_span_mul_finite_of_mem_mul {I J : fractional_ideal S P} {x : P} (hx : x ∈ I * J) :
∃ (T T' : finset P), (T : set P) ⊆ I ∧ (T' : set P) ⊆ J ∧ x ∈ span R (T * T' : set P) :=
submodule.mem_span_mul_finite_of_mem_mul (by simpa using mem_coe.mpr hx)
variables (S)
lemma coe_ideal_fg (inj : function.injective (algebra_map R P)) (I : ideal R) :
fg ((I : fractional_ideal S P) : submodule R P) ↔ fg I :=
coe_submodule_fg _ inj _
variables {S}
lemma fg_unit (I : units (fractional_ideal S P)) :
fg (I : submodule R P) :=
begin
have : (1 : P) ∈ (I * ↑I⁻¹ : fractional_ideal S P),
{ rw units.mul_inv, exact one_mem_one _ },
obtain ⟨T, T', hT, hT', one_mem⟩ := mem_span_mul_finite_of_mem_mul this,
refine ⟨T, submodule.span_eq_of_le _ hT _⟩,
rw [← one_mul ↑I, ← mul_one (span R ↑T)],
conv_rhs { rw [← fractional_ideal.coe_one, ← units.mul_inv I, fractional_ideal.coe_mul,
mul_comm ↑↑I, ← mul_assoc] },
refine submodule.mul_le_mul_left
(le_trans _ (submodule.mul_le_mul_right (submodule.span_le.mpr hT'))),
rwa [submodule.one_le, submodule.span_mul_span]
end
lemma fg_of_is_unit (I : fractional_ideal S P) (h : is_unit I) :
fg (I : submodule R P) :=
by { rcases h with ⟨I, rfl⟩, exact fg_unit I }
lemma _root_.ideal.fg_of_is_unit (inj : function.injective (algebra_map R P))
(I : ideal R) (h : is_unit (I : fractional_ideal S P)) :
I.fg :=
by { rw ← coe_ideal_fg S inj I, exact fg_of_is_unit I h }
variables (S P P')
include loc loc'
/-- `canonical_equiv f f'` is the canonical equivalence between the fractional
ideals in `P` and in `P'` -/
@[irreducible]
noncomputable def canonical_equiv :
fractional_ideal S P ≃+* fractional_ideal S P' :=
map_equiv
{ commutes' := λ r, ring_equiv_of_ring_equiv_eq _ _,
..ring_equiv_of_ring_equiv P P' (ring_equiv.refl R)
(show S.map _ = S, by rw [ring_equiv.to_monoid_hom_refl, submonoid.map_id]) }
@[simp] lemma mem_canonical_equiv_apply {I : fractional_ideal S P} {x : P'} :
x ∈ canonical_equiv S P P' I ↔
∃ y ∈ I, is_localization.map P' (ring_hom.id R)
(λ y (hy : y ∈ S), show ring_hom.id R y ∈ S, from hy) (y : P) = x :=
begin
rw [canonical_equiv, map_equiv_apply, mem_map],
exact ⟨λ ⟨y, mem, eq⟩, ⟨y, mem, eq⟩, λ ⟨y, mem, eq⟩, ⟨y, mem, eq⟩⟩
end
@[simp] lemma canonical_equiv_symm :
(canonical_equiv S P P').symm = canonical_equiv S P' P :=
ring_equiv.ext $ λ I, set_like.ext_iff.mpr $ λ x,
by { rw [mem_canonical_equiv_apply, canonical_equiv, map_equiv_symm, map_equiv,
ring_equiv.coe_mk, mem_map],
exact ⟨λ ⟨y, mem, eq⟩, ⟨y, mem, eq⟩, λ ⟨y, mem, eq⟩, ⟨y, mem, eq⟩⟩ }
@[simp] lemma canonical_equiv_flip (I) :
canonical_equiv S P P' (canonical_equiv S P' P I) = I :=
by rw [←canonical_equiv_symm, ring_equiv.symm_apply_apply]
end semiring
section is_fraction_ring
/-!
### `is_fraction_ring` section
This section concerns fractional ideals in the field of fractions,
i.e. the type `fractional_ideal R⁰ K` where `is_fraction_ring R K`.
-/
variables {K K' : Type*} [field K] [field K']
variables [algebra R K] [is_fraction_ring R K] [algebra R K'] [is_fraction_ring R K']
variables {I J : fractional_ideal R⁰ K} (h : K →ₐ[R] K')
/-- Nonzero fractional ideals contain a nonzero integer. -/
lemma exists_ne_zero_mem_is_integer [nontrivial R] (hI : I ≠ 0) :
∃ x ≠ (0 : R), algebra_map R K x ∈ I :=
begin
obtain ⟨y, y_mem, y_not_mem⟩ := set_like.exists_of_lt
(by simpa only using bot_lt_iff_ne_bot.mpr hI),
have y_ne_zero : y ≠ 0 := by simpa using y_not_mem,
obtain ⟨z, ⟨x, hx⟩⟩ := exists_integer_multiple R⁰ y,
refine ⟨x, _, _⟩,
{ rw [ne.def, ← @is_fraction_ring.to_map_eq_zero_iff R _ K, hx, algebra.smul_def],
exact mul_ne_zero (is_fraction_ring.to_map_ne_zero_of_mem_non_zero_divisors z.2) y_ne_zero },
{ rw hx,
exact smul_mem _ _ y_mem }
end
lemma map_ne_zero [nontrivial R] (hI : I ≠ 0) : I.map h ≠ 0 :=
begin
obtain ⟨x, x_ne_zero, hx⟩ := exists_ne_zero_mem_is_integer hI,
contrapose! x_ne_zero with map_eq_zero,
refine is_fraction_ring.to_map_eq_zero_iff.mp (eq_zero_iff.mp map_eq_zero _ (mem_map.mpr _)),
exact ⟨algebra_map R K x, hx, h.commutes x⟩,
end
@[simp] lemma map_eq_zero_iff [nontrivial R] : I.map h = 0 ↔ I = 0 :=
⟨imp_of_not_imp_not _ _ (map_ne_zero _),
λ hI, hI.symm ▸ map_zero h⟩
lemma coe_ideal_injective :
function.injective (coe : ideal R → fractional_ideal R⁰ K) :=
injective_of_le_imp_le _ (λ _ _, (coe_ideal_le_coe_ideal _).mp)
@[simp]
lemma coe_ideal_eq_zero_iff
{I : ideal R} : (I : fractional_ideal R⁰ K) = 0 ↔ I = ⊥ :=
by { rw ← coe_to_fractional_ideal_bot, exact coe_ideal_injective.eq_iff }
lemma coe_ideal_ne_zero_iff
{I : ideal R} : (I : fractional_ideal R⁰ K) ≠ 0 ↔ I ≠ ⊥ :=
not_iff_not.mpr coe_ideal_eq_zero_iff
lemma coe_ideal_ne_zero
{I : ideal R} (hI : I ≠ ⊥) : (I : fractional_ideal R⁰ K) ≠ 0 :=
coe_ideal_ne_zero_iff.mpr hI
end is_fraction_ring
section quotient
/-!
### `quotient` section
This section defines the ideal quotient of fractional ideals.
In this section we need that each non-zero `y : R` has an inverse in
the localization, i.e. that the localization is a field. We satisfy this
assumption by taking `S = non_zero_divisors R`, `R`'s localization at which
is a field because `R` is a domain.
-/
open_locale classical
variables {R₁ : Type*} [comm_ring R₁] {K : Type*} [field K]
variables [algebra R₁ K] [frac : is_fraction_ring R₁ K]
instance : nontrivial (fractional_ideal R₁⁰ K) :=
⟨⟨0, 1, λ h,
have this : (1 : K) ∈ (0 : fractional_ideal R₁⁰ K) :=
by { rw ← (algebra_map R₁ K).map_one, simpa only [h] using coe_mem_one R₁⁰ 1 },
one_ne_zero ((mem_zero_iff _).mp this)⟩⟩
lemma ne_zero_of_mul_eq_one (I J : fractional_ideal R₁⁰ K) (h : I * J = 1) : I ≠ 0 :=
λ hI, @zero_ne_one (fractional_ideal R₁⁰ K) _ _ (by { convert h, simp [hI], })
variables [is_domain R₁]
include frac
lemma fractional_div_of_nonzero {I J : fractional_ideal R₁⁰ K} (h : J ≠ 0) :
is_fractional R₁⁰ (I / J : submodule R₁ K) :=
begin
rcases I with ⟨I, aI, haI, hI⟩,
rcases J with ⟨J, aJ, haJ, hJ⟩,
obtain ⟨y, mem_J, not_mem_zero⟩ := set_like.exists_of_lt
(by simpa only using bot_lt_iff_ne_bot.mpr h),
obtain ⟨y', hy'⟩ := hJ y mem_J,
use (aI * y'),
split,
{ apply (non_zero_divisors R₁).mul_mem haI (mem_non_zero_divisors_iff_ne_zero.mpr _),
intro y'_eq_zero,
have : algebra_map R₁ K aJ * y = 0,
{ rw [← algebra.smul_def, ←hy', y'_eq_zero, ring_hom.map_zero] },
have y_zero := (mul_eq_zero.mp this).resolve_left
(mt ((algebra_map R₁ K).injective_iff.1 (is_fraction_ring.injective _ _) _)
(mem_non_zero_divisors_iff_ne_zero.mp haJ)),
exact not_mem_zero ((mem_zero_iff R₁⁰).mpr y_zero) },
intros b hb,
convert hI _ (hb _ (submodule.smul_mem _ aJ mem_J)) using 1,
rw [← hy', mul_comm b, ← algebra.smul_def, mul_smul]
end
noncomputable instance fractional_ideal_has_div :
has_div (fractional_ideal R₁⁰ K) :=
⟨ λ I J, if h : J = 0 then 0 else ⟨I / J, fractional_div_of_nonzero h⟩ ⟩
variables {I J : fractional_ideal R₁⁰ K} [ J ≠ 0 ]
@[simp] lemma div_zero {I : fractional_ideal R₁⁰ K} :
I / 0 = 0 :=
dif_pos rfl
lemma div_nonzero {I J : fractional_ideal R₁⁰ K} (h : J ≠ 0) :
(I / J) = ⟨I / J, fractional_div_of_nonzero h⟩ :=
dif_neg h
@[simp] lemma coe_div {I J : fractional_ideal R₁⁰ K} (hJ : J ≠ 0) :
(↑(I / J) : submodule R₁ K) = ↑I / (↑J : submodule R₁ K) :=
begin
unfold has_div.div,
simp only [dif_neg hJ, coe_mk, val_eq_coe],
end
lemma mem_div_iff_of_nonzero {I J : fractional_ideal R₁⁰ K} (h : J ≠ 0) {x} :
x ∈ I / J ↔ ∀ y ∈ J, x * y ∈ I :=
by { rw div_nonzero h, exact submodule.mem_div_iff_forall_mul_mem }
lemma mul_one_div_le_one {I : fractional_ideal R₁⁰ K} : I * (1 / I) ≤ 1 :=
begin
by_cases hI : I = 0,
{ rw [hI, div_zero, mul_zero],
exact zero_le 1 },
{ rw [← coe_le_coe, coe_mul, coe_div hI, coe_one],
apply submodule.mul_one_div_le_one },
end
lemma le_self_mul_one_div {I : fractional_ideal R₁⁰ K} (hI : I ≤ (1 : fractional_ideal R₁⁰ K)) :
I ≤ I * (1 / I) :=
begin
by_cases hI_nz : I = 0,
{ rw [hI_nz, div_zero, mul_zero], exact zero_le 0 },
{ rw [← coe_le_coe, coe_mul, coe_div hI_nz, coe_one],
rw [← coe_le_coe, coe_one] at hI,
exact submodule.le_self_mul_one_div hI },
end
lemma le_div_iff_of_nonzero {I J J' : fractional_ideal R₁⁰ K} (hJ' : J' ≠ 0) :
I ≤ J / J' ↔ ∀ (x ∈ I) (y ∈ J'), x * y ∈ J :=
⟨ λ h x hx, (mem_div_iff_of_nonzero hJ').mp (h hx),
λ h x hx, (mem_div_iff_of_nonzero hJ').mpr (h x hx) ⟩
lemma le_div_iff_mul_le {I J J' : fractional_ideal R₁⁰ K} (hJ' : J' ≠ 0) :
I ≤ J / J' ↔ I * J' ≤ J :=
begin
rw div_nonzero hJ',
convert submodule.le_div_iff_mul_le using 1,
rw [← coe_mul, coe_le_coe]
end
@[simp] lemma div_one {I : fractional_ideal R₁⁰ K} : I / 1 = I :=
begin
rw [div_nonzero (@one_ne_zero (fractional_ideal R₁⁰ K) _ _)],
ext,
split; intro h,
{ simpa using mem_div_iff_forall_mul_mem.mp h 1
((algebra_map R₁ K).map_one ▸ coe_mem_one R₁⁰ 1) },
{ apply mem_div_iff_forall_mul_mem.mpr,
rintros y ⟨y', _, rfl⟩,
rw mul_comm,
convert submodule.smul_mem _ y' h,
exact (algebra.smul_def _ _).symm }
end
theorem eq_one_div_of_mul_eq_one (I J : fractional_ideal R₁⁰ K) (h : I * J = 1) :
J = 1 / 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_div_self_cancel_iff {I : fractional_ideal R₁⁰ K} :
I * (1 / I) = 1 ↔ ∃ J, I * J = 1 :=
⟨λ h, ⟨(1 / I), h⟩, λ ⟨J, hJ⟩, by rwa [← eq_one_div_of_mul_eq_one I J hJ]⟩
variables {K' : Type*} [field K'] [algebra R₁ K'] [is_fraction_ring R₁ K']
@[simp] lemma map_div (I J : fractional_ideal R₁⁰ K) (h : K ≃ₐ[R₁] K') :
(I / J).map (h : K →ₐ[R₁] K') = I.map h / J.map h :=
begin
by_cases H : J = 0,
{ rw [H, div_zero, map_zero, div_zero] },
{ apply coe_to_submodule_injective,
simp [div_nonzero H, div_nonzero (map_ne_zero _ H), submodule.map_div] }
end
@[simp] lemma map_one_div (I : fractional_ideal R₁⁰ K) (h : K ≃ₐ[R₁] K') :
(1 / I).map (h : K →ₐ[R₁] K') = 1 / I.map h :=
by rw [map_div, map_one]
end quotient
section field
variables {R₁ K L : Type*} [comm_ring R₁] [is_domain R₁] [field K] [field L]
variables [algebra R₁ K] [is_fraction_ring R₁ K] [algebra K L] [is_fraction_ring K L]
lemma eq_zero_or_one (I : fractional_ideal K⁰ L) : I = 0 ∨ I = 1 :=
begin
rw or_iff_not_imp_left,
intro hI,
simp_rw [@set_like.ext_iff _ _ _ I 1, fractional_ideal.mem_one_iff],
intro x,
split,
{ intro x_mem,
obtain ⟨n, d, rfl⟩ := is_localization.mk'_surjective K⁰ x,
refine ⟨n / d, _⟩,
rw [ring_hom.map_div, is_fraction_ring.mk'_eq_div] },
{ rintro ⟨x, rfl⟩,
obtain ⟨y, y_ne, y_mem⟩ := fractional_ideal.exists_ne_zero_mem_is_integer hI,
rw [← div_mul_cancel x y_ne, ring_hom.map_mul, ← algebra.smul_def],
exact submodule.smul_mem I _ y_mem }
end
lemma eq_zero_or_one_of_is_field (hF : is_field R₁) (I : fractional_ideal R₁⁰ K) : I = 0 ∨ I = 1 :=
begin
letI : field R₁ := hF.to_field R₁,
-- TODO can this be less ugly?
exact @eq_zero_or_one R₁ K _ _ _ (by { unfreezingI {cases _inst_4}, convert _inst_9 }) I
end
end field
section principal_ideal_ring
variables {R₁ : Type*} [comm_ring R₁] {K : Type*} [field K]
variables [algebra R₁ K] [is_fraction_ring R₁ K]
open_locale classical
open submodule submodule.is_principal
include loc
lemma is_fractional_span_singleton (x : P) : is_fractional S (span R {x} : submodule R P) :=
let ⟨a, ha⟩ := exists_integer_multiple S x in
is_fractional_span_iff.mpr ⟨a, a.2, λ x' hx', (set.mem_singleton_iff.mp hx').symm ▸ ha⟩
variables (S)
/-- `span_singleton x` is the fractional ideal generated by `x` if `0 ∉ S` -/
@[irreducible]
def span_singleton (x : P) : fractional_ideal S P :=
⟨span R {x}, is_fractional_span_singleton x⟩
local attribute [semireducible] span_singleton
@[simp] lemma coe_span_singleton (x : P) :
(span_singleton S x : submodule R P) = span R {x} := rfl
@[simp] lemma mem_span_singleton {x y : P} :
x ∈ span_singleton S y ↔ ∃ (z : R), z • y = x :=
submodule.mem_span_singleton
lemma mem_span_singleton_self (x : P) :
x ∈ span_singleton S x :=
(mem_span_singleton S).mpr ⟨1, one_smul _ _⟩
variables {S}
lemma eq_span_singleton_of_principal (I : fractional_ideal S P)
[is_principal (I : submodule R P)] :
I = span_singleton S (generator (I : submodule R P)) :=
coe_to_submodule_injective (span_singleton_generator ↑I).symm
lemma is_principal_iff (I : fractional_ideal S P) :
is_principal (I : submodule R P) ↔ ∃ x, I = span_singleton S x :=
⟨λ h, ⟨@generator _ _ _ _ _ ↑I h, @eq_span_singleton_of_principal _ _ _ _ _ _ _ I h⟩,
λ ⟨x, hx⟩, { principal := ⟨x, trans (congr_arg _ hx) (coe_span_singleton _ x)⟩ } ⟩
@[simp] lemma span_singleton_zero : span_singleton S (0 : P) = 0 :=
by { ext, simp [submodule.mem_span_singleton, eq_comm] }
lemma span_singleton_eq_zero_iff {y : P} : span_singleton S y = 0 ↔ y = 0 :=
⟨λ h, span_eq_bot.mp (by simpa using congr_arg subtype.val h : span R {y} = ⊥) y (mem_singleton y),
λ h, by simp [h] ⟩
lemma span_singleton_ne_zero_iff {y : P} : span_singleton S y ≠ 0 ↔ y ≠ 0 :=
not_congr span_singleton_eq_zero_iff
@[simp] lemma span_singleton_one : span_singleton S (1 : P) = 1 :=
begin
ext,
refine (mem_span_singleton S).trans ((exists_congr _).trans (mem_one_iff S).symm),
intro x',
rw [algebra.smul_def, mul_one]
end
@[simp]
lemma span_singleton_mul_span_singleton (x y : P) :
span_singleton S x * span_singleton S y = span_singleton S (x * y) :=
begin
apply coe_to_submodule_injective,
simp only [coe_mul, coe_span_singleton, span_mul_span, singleton_mul_singleton],
end
@[simp]
lemma coe_ideal_span_singleton (x : R) :
(↑(ideal.span {x} : ideal R) : fractional_ideal S P) = span_singleton S (algebra_map R P x) :=
begin
ext y,
refine (mem_coe_ideal S).trans (iff.trans _ (mem_span_singleton S).symm),
split,
{ rintros ⟨y', hy', rfl⟩,
obtain ⟨x', rfl⟩ := submodule.mem_span_singleton.mp hy',
use x',
rw [smul_eq_mul, ring_hom.map_mul, algebra.smul_def] },
{ rintros ⟨y', rfl⟩,
refine ⟨y' * x, submodule.mem_span_singleton.mpr ⟨y', rfl⟩, _⟩,
rw [ring_hom.map_mul, algebra.smul_def] }
end
@[simp]
lemma canonical_equiv_span_singleton {P'} [comm_ring P'] [algebra R P'] [is_localization S P']
(x : P) :
canonical_equiv S P P' (span_singleton S x) =
span_singleton S (is_localization.map P' (ring_hom.id R)
(λ y (hy : y ∈ S), show ring_hom.id R y ∈ S, from hy) x) :=
begin
apply set_like.ext_iff.mpr,
intro y,
split; intro h,
{ rw mem_span_singleton,
obtain ⟨x', hx', rfl⟩ := (mem_canonical_equiv_apply _ _ _).mp h,
obtain ⟨z, rfl⟩ := (mem_span_singleton _).mp hx',
use z,
rw is_localization.map_smul,
refl },
{ rw mem_canonical_equiv_apply,
obtain ⟨z, rfl⟩ := (mem_span_singleton _).mp h,
use z • x,
use (mem_span_singleton _).mpr ⟨z, rfl⟩,
simp [is_localization.map_smul] }
end
lemma mem_singleton_mul {x y : P} {I : fractional_ideal S P} :
y ∈ span_singleton S x * I ↔ ∃ y' ∈ I, y = x * y' :=
begin
split,
{ intro h,
apply fractional_ideal.mul_induction_on h,
{ intros x' hx' y' hy',
obtain ⟨a, ha⟩ := (mem_span_singleton S).mp hx',
use [a • y', submodule.smul_mem I a hy'],
rw [←ha, algebra.mul_smul_comm, algebra.smul_mul_assoc] },
{ exact ⟨0, submodule.zero_mem I, (mul_zero x).symm⟩ },
{ rintros _ _ ⟨y, hy, rfl⟩ ⟨y', hy', rfl⟩,
exact ⟨y + y', submodule.add_mem I hy hy', (mul_add _ _ _).symm⟩ },
{ rintros r _ ⟨y', hy', rfl⟩,
exact ⟨r • y', submodule.smul_mem I r hy', (algebra.mul_smul_comm _ _ _).symm ⟩ } },
{ rintros ⟨y', hy', rfl⟩,
exact mul_mem_mul ((mem_span_singleton S).mpr ⟨1, one_smul _ _⟩) hy' }
end
omit loc
variables (K)
lemma mk'_mul_coe_ideal_eq_coe_ideal {I J : ideal R₁} {x y : R₁} (hy : y ∈ R₁⁰) :
span_singleton R₁⁰ (is_localization.mk' K x ⟨y, hy⟩) * I = (J : fractional_ideal R₁⁰ K) ↔
ideal.span {x} * I = ideal.span {y} * J :=
begin
have inj : function.injective (coe : ideal R₁ → fractional_ideal R₁⁰ K) :=
fractional_ideal.coe_ideal_injective,
have : span_singleton R₁⁰ (is_localization.mk' _ (1 : R₁) ⟨y, hy⟩) *
span_singleton R₁⁰ (algebra_map R₁ K y) = 1,
{ rw [span_singleton_mul_span_singleton, mul_comm, ← is_localization.mk'_eq_mul_mk'_one,
is_localization.mk'_self, span_singleton_one] },
let y' : units (fractional_ideal R₁⁰ K) := units.mk_of_mul_eq_one _ _ this,
have coe_y' : ↑y' = span_singleton R₁⁰ (is_localization.mk' K (1 : R₁) ⟨y, hy⟩) := rfl,
refine iff.trans _ (y'.mul_right_inj.trans inj.eq_iff),
rw [coe_y', coe_ideal_mul, coe_ideal_span_singleton, coe_ideal_mul, coe_ideal_span_singleton,
←mul_assoc, span_singleton_mul_span_singleton, ←mul_assoc, span_singleton_mul_span_singleton,
mul_comm (mk' _ _ _), ← is_localization.mk'_eq_mul_mk'_one,
mul_comm (mk' _ _ _), ← is_localization.mk'_eq_mul_mk'_one,
is_localization.mk'_self, span_singleton_one, one_mul],
end
variables {K}
lemma span_singleton_mul_coe_ideal_eq_coe_ideal {I J : ideal R₁} {z : K} :
span_singleton R₁⁰ z * (I : fractional_ideal R₁⁰ K) = J ↔
ideal.span {((is_localization.sec R₁⁰ z).1 : R₁)} * I =
ideal.span {(is_localization.sec R₁⁰ z).2} * J :=
-- `erw` to deal with the distinction between `y` and `⟨y.1, y.2⟩`
by erw [← mk'_mul_coe_ideal_eq_coe_ideal K (is_localization.sec R₁⁰ z).2.prop,
is_localization.mk'_sec K z]
variables [is_domain R₁]
lemma one_div_span_singleton (x : K) :
1 / span_singleton R₁⁰ x = span_singleton R₁⁰ (x⁻¹) :=
if h : x = 0 then by simp [h] else (eq_one_div_of_mul_eq_one _ _ (by simp [h])).symm
@[simp] lemma div_span_singleton (J : fractional_ideal R₁⁰ K) (d : K) :
J / span_singleton R₁⁰ d = span_singleton R₁⁰ (d⁻¹) * J :=
begin
rw ← one_div_span_singleton,
by_cases hd : d = 0,
{ simp only [hd, span_singleton_zero, div_zero, zero_mul] },
have h_spand : span_singleton R₁⁰ d ≠ 0 := mt span_singleton_eq_zero_iff.mp hd,
apply le_antisymm,
{ intros x hx,
rw [← mem_coe, coe_div h_spand, submodule.mem_div_iff_forall_mul_mem] at hx,
specialize hx d (mem_span_singleton_self R₁⁰ d),
have h_xd : x = d⁻¹ * (x * d), { field_simp },
rw [← mem_coe, coe_mul, one_div_span_singleton, h_xd],
exact submodule.mul_mem_mul (mem_span_singleton_self R₁⁰ _) hx },
{ rw [le_div_iff_mul_le h_spand, mul_assoc, mul_left_comm, one_div_span_singleton,
span_singleton_mul_span_singleton, inv_mul_cancel hd, span_singleton_one, mul_one],
exact le_refl J },
end
lemma exists_eq_span_singleton_mul (I : fractional_ideal R₁⁰ K) :
∃ (a : R₁) (aI : ideal R₁), a ≠ 0 ∧ I = span_singleton R₁⁰ (algebra_map R₁ K a)⁻¹ * aI :=
begin
obtain ⟨a_inv, nonzero, ha⟩ := I.is_fractional,
have nonzero := mem_non_zero_divisors_iff_ne_zero.mp nonzero,
have map_a_nonzero : algebra_map R₁ K a_inv ≠ 0 :=
mt is_fraction_ring.to_map_eq_zero_iff.mp nonzero,
refine ⟨a_inv,
submodule.comap (algebra.linear_map R₁ K)
↑(span_singleton R₁⁰ (algebra_map R₁ K a_inv) * I),
nonzero,
ext (λ x, iff.trans ⟨_, _⟩ mem_singleton_mul.symm)⟩,
{ intro hx,
obtain ⟨x', hx'⟩ := ha x hx,
rw algebra.smul_def at hx',
refine ⟨algebra_map R₁ K x', (mem_coe_ideal _).mpr ⟨x', mem_singleton_mul.mpr _, rfl⟩, _⟩,
{ exact ⟨x, hx, hx'⟩ },
{ rw [hx', ← mul_assoc, inv_mul_cancel map_a_nonzero, one_mul] } },
{ rintros ⟨y, hy, rfl⟩,
obtain ⟨x', hx', rfl⟩ := (mem_coe_ideal _).mp hy,
obtain ⟨y', hy', hx'⟩ := mem_singleton_mul.mp hx',
rw algebra.linear_map_apply at hx',
rwa [hx', ←mul_assoc, inv_mul_cancel map_a_nonzero, one_mul] }
end
instance is_principal {R} [comm_ring R] [is_domain R] [is_principal_ideal_ring R]
[algebra R K] [is_fraction_ring R K]
(I : fractional_ideal R⁰ K) : (I : submodule R K).is_principal :=
begin
obtain ⟨a, aI, -, ha⟩ := exists_eq_span_singleton_mul I,
use (algebra_map R K a)⁻¹ * algebra_map R K (generator aI),
suffices : I = span_singleton R⁰ ((algebra_map R K a)⁻¹ * algebra_map R K (generator aI)),
{ exact congr_arg subtype.val this },
conv_lhs { rw [ha, ←span_singleton_generator aI] },
rw [ideal.submodule_span_eq, coe_ideal_span_singleton (generator aI),
span_singleton_mul_span_singleton]
end
include loc
lemma le_span_singleton_mul_iff {x : P} {I J : fractional_ideal S P} :
I ≤ span_singleton S x * J ↔ ∀ zI ∈ I, ∃ zJ ∈ J, x * zJ = zI :=
show (∀ {zI} (hzI : zI ∈ I), zI ∈ span_singleton _ x * J) ↔ ∀ zI ∈ I, ∃ zJ ∈ J, x * zJ = zI,
by simp only [fractional_ideal.mem_singleton_mul, eq_comm]
lemma span_singleton_mul_le_iff {x : P} {I J : fractional_ideal S P} :
span_singleton _ x * I ≤ J ↔ ∀ z ∈ I, x * z ∈ J :=
begin
simp only [fractional_ideal.mul_le, fractional_ideal.mem_singleton_mul,
fractional_ideal.mem_span_singleton],
split,
{ intros h zI hzI,
exact h x ⟨1, one_smul _ _⟩ zI hzI },
{ rintros h _ ⟨z, rfl⟩ zI hzI,
rw [algebra.smul_mul_assoc],
exact submodule.smul_mem J.1 _ (h zI hzI) },
end
lemma eq_span_singleton_mul {x : P} {I J : fractional_ideal S P} :
I = span_singleton _ x * J ↔ (∀ zI ∈ I, ∃ zJ ∈ J, x * zJ = zI) ∧ ∀ z ∈ J, x * z ∈ I :=
by simp only [le_antisymm_iff, fractional_ideal.le_span_singleton_mul_iff,
fractional_ideal.span_singleton_mul_le_iff]
end principal_ideal_ring
variables {R₁ : Type*} [comm_ring R₁]
variables {K : Type*} [field K] [algebra R₁ K] [frac : is_fraction_ring R₁ K]
local attribute [instance] classical.prop_decidable
lemma is_noetherian_zero : is_noetherian R₁ (0 : fractional_ideal R₁⁰ K) :=
is_noetherian_submodule.mpr (λ I (hI : I ≤ (0 : fractional_ideal R₁⁰ K)),
by { rw coe_zero at hI, rw le_bot_iff.mp hI, exact fg_bot })
lemma is_noetherian_iff {I : fractional_ideal R₁⁰ K} :
is_noetherian R₁ I ↔ ∀ J ≤ I, (J : submodule R₁ K).fg :=
is_noetherian_submodule.trans ⟨λ h J hJ, h _ hJ, λ h J hJ, h ⟨J, is_fractional_of_le hJ⟩ hJ⟩
lemma is_noetherian_coe_to_fractional_ideal [_root_.is_noetherian_ring R₁] (I : ideal R₁) :
is_noetherian R₁ (I : fractional_ideal R₁⁰ K) :=
begin
rw is_noetherian_iff,
intros J hJ,
obtain ⟨J, rfl⟩ := le_one_iff_exists_coe_ideal.mp (le_trans hJ coe_ideal_le_one),
exact fg_map (is_noetherian.noetherian J),
end
include frac
variables [is_domain R₁]
lemma is_noetherian_span_singleton_inv_to_map_mul (x : R₁) {I : fractional_ideal R₁⁰ K}
(hI : is_noetherian R₁ I) :
is_noetherian R₁ (span_singleton R₁⁰ (algebra_map R₁ K x)⁻¹ * I : fractional_ideal R₁⁰ K) :=
begin
by_cases hx : x = 0,
{ rw [hx, ring_hom.map_zero, _root_.inv_zero, span_singleton_zero, zero_mul],
exact is_noetherian_zero },
have h_gx : algebra_map R₁ K x ≠ 0,
from mt ((algebra_map R₁ K).injective_iff.mp (is_fraction_ring.injective _ _) x) hx,
have h_spanx : span_singleton R₁⁰ (algebra_map R₁ K x) ≠ 0,
from span_singleton_ne_zero_iff.mpr h_gx,
rw is_noetherian_iff at ⊢ hI,
intros J hJ,
rw [← div_span_singleton, le_div_iff_mul_le h_spanx] at hJ,
obtain ⟨s, hs⟩ := hI _ hJ,
use s * {(algebra_map R₁ K x)⁻¹},
rw [finset.coe_mul, finset.coe_singleton, ← span_mul_span, hs, ← coe_span_singleton R₁⁰,
← coe_mul, mul_assoc, span_singleton_mul_span_singleton, mul_inv_cancel h_gx,
span_singleton_one, mul_one],
end
/-- Every fractional ideal of a noetherian integral domain is noetherian. -/
theorem is_noetherian [_root_.is_noetherian_ring R₁] (I : fractional_ideal R₁⁰ K) :
is_noetherian R₁ I :=
begin
obtain ⟨d, J, h_nzd, rfl⟩ := exists_eq_span_singleton_mul I,
apply is_noetherian_span_singleton_inv_to_map_mul,
apply is_noetherian_coe_to_fractional_ideal,
end
section adjoin
include loc
omit frac
variables {R P} (S) (x : P) (hx : is_integral R x)
/-- `A[x]` is a fractional ideal for every integral `x`. -/
lemma is_fractional_adjoin_integral :
is_fractional S (algebra.adjoin R ({x} : set P)).to_submodule :=
is_fractional_of_fg (fg_adjoin_singleton_of_integral x hx)
/-- `fractional_ideal.adjoin_integral (S : submonoid R) x hx` is `R[x]` as a fractional ideal,
where `hx` is a proof that `x : P` is integral over `R`. -/
@[simps]
def adjoin_integral : fractional_ideal S P :=
⟨_, is_fractional_adjoin_integral S x hx⟩
lemma mem_adjoin_integral_self :
x ∈ adjoin_integral S x hx :=
algebra.subset_adjoin (set.mem_singleton x)
end adjoin
end fractional_ideal
|
c2f60949ee8beb2c63af4e57ddd0718ffcd1c57b | 1d265c7dd8cb3d0e1d645a19fd6157a2084c3921 | /src/lessons/lesson5.lean | 4e8c4ed431c581d1624a564ab915597cb2584bae | [
"MIT"
] | permissive | hanzhi713/lean-proofs | de432372f220d302be09b5ca4227f8986567e4fd | 4d8356a878645b9ba7cb036f87737f3f1e68ede5 | refs/heads/master | 1,585,580,245,658 | 1,553,646,623,000 | 1,553,646,623,000 | 151,342,188 | 0 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 492 | lean | variable g: false
theorem ttt : true → false := false.elim g
theorem zneqo : 0 ≠ 1.
theorem what : "Hello, Lean!" ≠ "Hello Lean!" :=
begin
assume h : "Hello, Lean!" = "Hello Lean!",
show false,
from string.no_confusion,
end
def f : ℕ → ℕ := λ x, x - 1
def v : Prop := 1 = 1
theorem gg : 1 = 1 := rfl
theorem ex2 : 2 ≠ 1.
theorem modus_tollens:
∀ { P Q: Prop }, (P → Q) → ¬ Q → ¬ P :=
λ P Q pfPQ pfnQ pfP,
pfnQ (pfPQ pfP)
|
4a19aa12e886b7f60df0afdb1a76b47945ca659c | e38d5e91d30731bef617cc9b6de7f79c34cdce9a | /src/path/induction.lean | 82bb27ad297f926b9179e834077c991a21b72115 | [
"Apache-2.0"
] | permissive | bbentzen/cubicalean | 55e979c303fbf55a81ac46b1000c944b2498be7a | 3b94cd2aefdfc2163c263bd3fc6f2086fef814b5 | refs/heads/master | 1,588,314,875,258 | 1,554,412,699,000 | 1,554,412,699,000 | 177,333,390 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 4,046 | lean | /-
Copyright (c) 2019 Bruno Bentzen. All rights reserved.
Released under the Apache License 2.0 (see "License");
Author: Bruno Bentzen
-/
import ..core.kan.coe ..core.connection.meet .groupoid.ops
open interval path
-- auxiliary definitions and lemmas
section
variable {A : Type}
private def f {a x y : A} (h : x = y) :
path A a x → path A a y :=
λ p, eq.rec p h
private def f.eq {a x y : A} (h : x = y) (p : I → A) (h0 : p i0 = a)
(hx : p i1 = x) (hy : p i1 = y) : f h (abs p h0 hx ) = abs p h0 hy :=
by unfold f; induction h; simp
variable (kan : has_hcom2 A)
private lemma aux {a b x : A} (p : path A a b) (h : b = x)
(ha : p @@ i0 = a) (hx : p @@ i1 = x) : f h p = path.abs (app p) ha hx :=
by unfold f; induction h; simp; symmetry; apply pathdp.eta
private lemma aux' {a b : A} (p : path A a b) (ha : meet kan (app p) i1 i0 = a) (hb : meet kan (app p) i1 i1 = p @@ i1) :
f ((app1 p).symm) p = abs (meet kan (path.app p) i1) ha hb :=
by revert ha hb; refine eq.rec _ (meet.face1j kan (app p)).symm; intros; apply aux
lemma family.eq {a x y: A} (p : path A a x) (q : path A a y) (h : x = y) (h' : f h p = q)
{C : Π x : A, path A a x → Type} : C x p = C y q :=
by revert h'; unfold f; induction h; simp; intro; induction h'; refl
end
-- transport (coercion for paths)
section
variables {A : Type} (kan' : Π U, coe.has_coe U)
theorem path.transport {a x : A} (p : path A a x) (C : Π x : A, Type)
(u : C a) : C x :=
have C0 : C (p @@ i0) = C a := by rw app0 p,
have C1 : C (p @@ i1) = C x := by rw app1 p,
eq.mp C1 ((kan' (λ i, C (p @@ i) )).coe i0 (eq.mp C0.symm u) i1)
end
-- based path induction
variables {A : Type} (kan : has_hcom2 A) (kan' : Π U, coe.has_coe U)
theorem path.induction {a x : A} {C : Π x : A, path A a x → Type}
(p : path A a x) (u : C a (refl a)) : C x p :=
let X := path.abs (meet kan (app p) i0)
(eq.trans (meet.face0j0i kan (app p)) (app0 p))
(eq.trans (meet.face0j1i kan (app p)) rfl) in
have h : f ((app0 p).symm) (path.refl a) = X, from
begin
rw refl.eq (eq.trans (meet.face0j kan (app p)) (funext (λ i, app0 p))),
apply f.eq, transitivity, apply meet.face0j1i, apply app0
end,
let Y := abs (meet kan (app p) i1)
(eq.trans (meet.face1j0i kan (app p)) (app0 p))
(eq.trans (meet.face1j1i kan (app p)) rfl) in
have h' : f ((app1 p).symm) p = Y, from
begin
symmetry, apply abs_irrel, symmetry, apply aux',
apply (eq.trans (meet.face1j0i kan (app p)) (app0 p)),
apply (eq.trans (meet.face1j1i kan (app p)) rfl)
end,
have C0 : C a (refl a) = C (p @@ i0) _ := family.eq _ _ (app0 p).symm h,
have C1 : C x p = C (p @@ i1) _ := family.eq p _ (app1 p).symm h',
eq.mp C1.symm ((kan' (λ i, C (p @@ i) (path.abs (meet kan (app p) i)
(eq.trans (meet.face0i' kan (app p) i) (app0 p) ) (meet.face1i' kan (app p) i) ))).coe
i0 (eq.mp C0 u) i1)
/-def C0 {a x : A} {C : Π x : A, path A a x → Type}
(p : path A a x) : C a (path.refl a) = C (p @@ i0) _ :=
let X := path.abs (meet kan (app p) i0)
(eq.trans (meet.face0j0i kan (app p)) (app0 p))
(eq.trans (meet.face0j1i kan (app p)) rfl) in
have h : f ((app0 p).symm) (path.refl a) = X, from
begin
rw refl.eq (eq.trans (meet.face0j kan (app p)) (funext (λ i, app0 p))),
apply f.eq, transitivity, apply meet.face0j1i, apply app0
end,
family.eq _ _ (app0 p).symm h
variables {a x : A} {C : Π x : A, path A a x → Type}
(p : path A a x) (u : C a (path.refl a))
#check @path.induction
#check @C0
#check (eq.mp (C0 kan (refl a)) u)
#check (kan' (λ i, C (p @@ i) (path.abs (meet kan (app p) i) (eq.trans (meet.face0i' kan (app p) i) (app0 p) ) (meet.face1i' kan (app p) i) ))).coeq
i0 (eq.mp (C0 kan p) u)
theorem path.induction.comp {a : A} {C : Π x : A, path A a x → Type} (u : C a (refl a)) :
path (C a (refl a)) (path.induction kan kan' (path.refl a) u) u :=
begin
fapply path.abs,
--exact λ i, (kan' _).coe i0 i
end
-- eq.mp C1.symm (eq.mp C0 u) i1-/ |
dcf5b29b724a1c5bce1e97420a78220a2d0ab63f | 4767244035cdd124e1ce3d0c81128f8929df6163 | /meta/rb_map.lean | 5bc9ce0afece16775a112250a98b6e640ba6a810 | [
"Apache-2.0"
] | permissive | 5HT/mathlib | b941fecacd31a9c5dd0abad58770084b8a1e56b1 | 40fa9ade2f5649569639608db5e621e5fad0cc02 | refs/heads/master | 1,586,978,681,358 | 1,546,681,764,000 | 1,546,681,764,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,110 | lean | /-
Copyright (c) 2018 Robert Y. Lewis. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Robert Y. Lewis
Additional operations on native rb_maps and rb_sets.
-/
import data.option.defs
namespace native
namespace rb_set
meta def filter {key} (s : rb_set key) (P : key → bool) : rb_set key :=
s.fold s (λ a m, if P a then m else m.erase a)
meta def union {key} (s t : rb_set key) : rb_set key :=
s.fold t (λ a t, t.insert a)
end rb_set
namespace rb_map
meta def find_def {α β} [has_lt α] [decidable_rel ((<) : α → α → Prop)]
(x : β) (m : rb_map α β) (k : α) :=
(m.find k).get_or_else x
meta def insert_cons {α β} [has_lt α] [decidable_rel ((<) : α → α → Prop)]
(k : α) (x : β) (m : rb_map α (list β)) : rb_map α (list β) :=
m.insert k (x :: m.find_def [] k)
meta def ifind {α β} [inhabited β] (m : rb_map α β) (a : α) : β :=
(m.find a).iget
meta def zfind {α β} [has_zero β] (m : rb_map α β) (a : α) : β :=
(m.find a).get_or_else 0
meta def add {α β} [has_add β] [has_zero β] [decidable_eq β] (m1 m2 : rb_map α β) : rb_map α β :=
m1.fold m2
(λ n v m,
let nv := v + m2.zfind n in
if nv = 0 then m.erase n else m.insert n nv)
meta def scale {α β} [has_lt α] [decidable_rel ((<) : α → α → Prop)] [has_mul β] (b : β) (m : rb_map α β) : rb_map α β :=
m.map ((*) b)
section
open format prod
variables {key : Type} {data : Type} [has_to_tactic_format key] [has_to_tactic_format data]
private meta def pp_key_data (k : key) (d : data) (first : bool) : tactic format :=
do fk ← tactic.pp k, fd ← tactic.pp d, return $
(if first then to_fmt "" else to_fmt "," ++ line) ++ fk ++ space ++ to_fmt "←" ++ space ++ fd
meta instance : has_to_tactic_format (rb_map key data) :=
⟨λ m, do
(fmt, _) ← fold m (return (to_fmt "", tt))
(λ k d p, do p ← p, pkd ← pp_key_data k d (snd p), return (fst p ++ pkd, ff)),
return $ group $ to_fmt "⟨" ++ nest 1 fmt ++ to_fmt "⟩"⟩
end
end rb_map
end native
|
e4efc144c5905dc5fa2441514a945218296127a4 | 3d2a7f1582fe5bae4d0bdc2fe86e997521239a65 | /spatial-reasoning/spatial-reasoning-problem-18.lean | 2be461cd8312bbaecef6ec90701ead0f68299a73 | [] | no_license | own-pt/common-sense-lean | e4fa643ae010459de3d1bf673be7cbc7062563c9 | f672210aecb4172f5bae265e43e6867397e13b1c | refs/heads/master | 1,622,065,660,261 | 1,589,487,533,000 | 1,589,487,533,000 | 254,167,782 | 3 | 2 | null | 1,589,487,535,000 | 1,586,370,214,000 | Lean | UTF-8 | Lean | false | false | 227 | lean | /- Spatial Reasoning Problem 18 -/
/- It can be found at: SpatialQs.txt -/
/- (18) John holds one end of a string.
The other end is tied to a cart.
If the string is taut and John pulls on his end, does the cart move? -/
|
89551b748abcd5f637c66ab79b0133237187ee49 | 6065973b1fa7bbacba932011c9e2f32bf7bdd6c1 | /src/geometry/euclidean/basic.lean | abad398a88c5e5fa87fb22d408104d21438ba04a | [
"Apache-2.0"
] | permissive | khmacdonald/mathlib | 90a0fa2222369fa69ed2fbfb841b74d2bdfd66cb | 3669cb35c578441812ad30fd967d21a94b6f387e | refs/heads/master | 1,675,863,801,090 | 1,609,761,876,000 | 1,609,761,876,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 46,601 | lean | /-
Copyright (c) 2020 Joseph Myers. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Joseph Myers.
-/
import analysis.normed_space.inner_product
import algebra.quadratic_discriminant
import analysis.normed_space.add_torsor
import data.matrix.notation
import linear_algebra.affine_space.finite_dimensional
import tactic.fin_cases
noncomputable theory
open_locale big_operators
open_locale classical
open_locale real
open_locale real_inner_product_space
/-!
# Euclidean spaces
This file makes some definitions and proves very basic geometrical
results about real inner product spaces and Euclidean affine spaces.
Results about real inner product spaces that involve the norm and
inner product but not angles generally go in
`analysis.normed_space.inner_product`. Results with longer
proofs or more geometrical content generally go in separate files.
## Main definitions
* `inner_product_geometry.angle` is the undirected angle between two
vectors.
* `euclidean_geometry.angle`, with notation `∠`, is the undirected
angle determined by three points.
* `euclidean_geometry.orthogonal_projection` is the orthogonal
projection of a point onto an affine subspace.
* `euclidean_geometry.reflection` is the reflection of a point in an
affine subspace.
## Implementation notes
To declare `P` as the type of points in a Euclidean affine space with
`V` as the type of vectors, use `[inner_product_space ℝ V] [metric_space P]
[normed_add_torsor V P]`. This works better with `out_param` to make
`V` implicit in most cases than having a separate type alias for
Euclidean affine spaces.
Rather than requiring Euclidean affine spaces to be finite-dimensional
(as in the definition on Wikipedia), this is specified only for those
theorems that need it.
## References
* https://en.wikipedia.org/wiki/Euclidean_space
-/
namespace inner_product_geometry
/-!
### Geometrical results on real inner product spaces
This section develops some geometrical definitions and results on real
inner product spaces, where those definitions and results can most
conveniently be developed in terms of vectors and then used to deduce
corresponding results for Euclidean affine spaces.
-/
variables {V : Type*} [inner_product_space ℝ V]
/-- The undirected angle between two vectors. If either vector is 0,
this is π/2. -/
def angle (x y : V) : ℝ := real.arccos (inner x y / (∥x∥ * ∥y∥))
/-- The cosine of the angle between two vectors. -/
lemma cos_angle (x y : V) : real.cos (angle x y) = inner x y / (∥x∥ * ∥y∥) :=
real.cos_arccos (abs_le.mp (abs_real_inner_div_norm_mul_norm_le_one x y)).1
(abs_le.mp (abs_real_inner_div_norm_mul_norm_le_one x y)).2
/-- The angle between two vectors does not depend on their order. -/
lemma angle_comm (x y : V) : angle x y = angle y x :=
begin
unfold angle,
rw [real_inner_comm, mul_comm]
end
/-- The angle between the negation of two vectors. -/
@[simp] lemma angle_neg_neg (x y : V) : angle (-x) (-y) = angle x y :=
begin
unfold angle,
rw [inner_neg_neg, norm_neg, norm_neg]
end
/-- The angle between two vectors is nonnegative. -/
lemma angle_nonneg (x y : V) : 0 ≤ angle x y :=
real.arccos_nonneg _
/-- The angle between two vectors is at most π. -/
lemma angle_le_pi (x y : V) : angle x y ≤ π :=
real.arccos_le_pi _
/-- The angle between a vector and the negation of another vector. -/
lemma angle_neg_right (x y : V) : angle x (-y) = π - angle x y :=
begin
unfold angle,
rw [←real.arccos_neg, norm_neg, inner_neg_right, neg_div]
end
/-- The angle between the negation of a vector and another vector. -/
lemma angle_neg_left (x y : V) : angle (-x) y = π - angle x y :=
by rw [←angle_neg_neg, neg_neg, angle_neg_right]
/-- The angle between the zero vector and a vector. -/
@[simp] lemma angle_zero_left (x : V) : angle 0 x = π / 2 :=
begin
unfold angle,
rw [inner_zero_left, zero_div, real.arccos_zero]
end
/-- The angle between a vector and the zero vector. -/
@[simp] lemma angle_zero_right (x : V) : angle x 0 = π / 2 :=
begin
unfold angle,
rw [inner_zero_right, zero_div, real.arccos_zero]
end
/-- The angle between a nonzero vector and itself. -/
@[simp] lemma angle_self {x : V} (hx : x ≠ 0) : angle x x = 0 :=
begin
unfold angle,
rw [←real_inner_self_eq_norm_square, div_self (λ h, hx (inner_self_eq_zero.1 h)),
real.arccos_one]
end
/-- The angle between a nonzero vector and its negation. -/
@[simp] lemma angle_self_neg_of_nonzero {x : V} (hx : x ≠ 0) : angle x (-x) = π :=
by rw [angle_neg_right, angle_self hx, sub_zero]
/-- The angle between the negation of a nonzero vector and that
vector. -/
@[simp] lemma angle_neg_self_of_nonzero {x : V} (hx : x ≠ 0) : angle (-x) x = π :=
by rw [angle_comm, angle_self_neg_of_nonzero hx]
/-- The angle between a vector and a positive multiple of a vector. -/
@[simp] lemma angle_smul_right_of_pos (x y : V) {r : ℝ} (hr : 0 < r) :
angle x (r • y) = angle x y :=
begin
unfold angle,
rw [inner_smul_right, norm_smul, real.norm_eq_abs, abs_of_nonneg (le_of_lt hr), ←mul_assoc,
mul_comm _ r, mul_assoc, mul_div_mul_left _ _ (ne_of_gt hr)]
end
/-- The angle between a positive multiple of a vector and a vector. -/
@[simp] lemma angle_smul_left_of_pos (x y : V) {r : ℝ} (hr : 0 < r) :
angle (r • x) y = angle x y :=
by rw [angle_comm, angle_smul_right_of_pos y x hr, angle_comm]
/-- The angle between a vector and a negative multiple of a vector. -/
@[simp] lemma angle_smul_right_of_neg (x y : V) {r : ℝ} (hr : r < 0) :
angle x (r • y) = angle x (-y) :=
by rw [←neg_neg r, neg_smul, angle_neg_right, angle_smul_right_of_pos x y (neg_pos_of_neg hr),
angle_neg_right]
/-- The angle between a negative multiple of a vector and a vector. -/
@[simp] lemma angle_smul_left_of_neg (x y : V) {r : ℝ} (hr : r < 0) :
angle (r • x) y = angle (-x) y :=
by rw [angle_comm, angle_smul_right_of_neg y x hr, angle_comm]
/-- The cosine of the angle between two vectors, multiplied by the
product of their norms. -/
lemma cos_angle_mul_norm_mul_norm (x y : V) : real.cos (angle x y) * (∥x∥ * ∥y∥) = inner x y :=
begin
rw cos_angle,
by_cases h : (∥x∥ * ∥y∥) = 0,
{ rw [h, mul_zero],
cases eq_zero_or_eq_zero_of_mul_eq_zero h with hx hy,
{ rw norm_eq_zero at hx,
rw [hx, inner_zero_left] },
{ rw norm_eq_zero at hy,
rw [hy, inner_zero_right] } },
{ exact div_mul_cancel _ h }
end
/-- The sine of the angle between two vectors, multiplied by the
product of their norms. -/
lemma sin_angle_mul_norm_mul_norm (x y : V) : real.sin (angle x y) * (∥x∥ * ∥y∥) =
real.sqrt (inner x x * inner y y - inner x y * inner x y) :=
begin
unfold angle,
rw [real.sin_arccos (abs_le.mp (abs_real_inner_div_norm_mul_norm_le_one x y)).1
(abs_le.mp (abs_real_inner_div_norm_mul_norm_le_one x y)).2,
←real.sqrt_mul_self (mul_nonneg (norm_nonneg x) (norm_nonneg y)),
←real.sqrt_mul' _ (mul_self_nonneg _), pow_two,
real.sqrt_mul_self (mul_nonneg (norm_nonneg x) (norm_nonneg y)), real_inner_self_eq_norm_square,
real_inner_self_eq_norm_square],
by_cases h : (∥x∥ * ∥y∥) = 0,
{ rw [(show ∥x∥ * ∥x∥ * (∥y∥ * ∥y∥) = (∥x∥ * ∥y∥) * (∥x∥ * ∥y∥), by ring), h, mul_zero, mul_zero,
zero_sub],
cases eq_zero_or_eq_zero_of_mul_eq_zero h with hx hy,
{ rw norm_eq_zero at hx,
rw [hx, inner_zero_left, zero_mul, neg_zero] },
{ rw norm_eq_zero at hy,
rw [hy, inner_zero_right, zero_mul, neg_zero] } },
{ field_simp [h],
ring }
end
/-- The angle between two vectors is zero if and only if they are
nonzero and one is a positive multiple of the other. -/
lemma angle_eq_zero_iff (x y : V) : angle x y = 0 ↔ (x ≠ 0 ∧ ∃ (r : ℝ), 0 < r ∧ y = r • x) :=
begin
unfold angle,
rw [←real_inner_div_norm_mul_norm_eq_one_iff, ←real.arccos_one],
split,
{ intro h,
exact real.arccos_inj (abs_le.mp (abs_real_inner_div_norm_mul_norm_le_one x y)).1
(abs_le.mp (abs_real_inner_div_norm_mul_norm_le_one x y)).2
(by norm_num)
(by norm_num)
h },
{ intro h,
rw h }
end
/-- The angle between two vectors is π if and only if they are nonzero
and one is a negative multiple of the other. -/
lemma angle_eq_pi_iff (x y : V) : angle x y = π ↔ (x ≠ 0 ∧ ∃ (r : ℝ), r < 0 ∧ y = r • x) :=
begin
unfold angle,
rw [←real_inner_div_norm_mul_norm_eq_neg_one_iff, ←real.arccos_neg_one],
split,
{ intro h,
exact real.arccos_inj (abs_le.mp (abs_real_inner_div_norm_mul_norm_le_one x y)).1
(abs_le.mp (abs_real_inner_div_norm_mul_norm_le_one x y)).2
(by norm_num)
(by norm_num)
h },
{ intro h,
rw h }
end
/-- If the angle between two vectors is π, the angles between those
vectors and a third vector add to π. -/
lemma angle_add_angle_eq_pi_of_angle_eq_pi {x y : V} (z : V) (h : angle x y = π) :
angle x z + angle y z = π :=
begin
rw angle_eq_pi_iff at h,
rcases h with ⟨hx, ⟨r, ⟨hr, hxy⟩⟩⟩,
rw [hxy, angle_smul_left_of_neg x z hr, angle_neg_left,
add_sub_cancel'_right]
end
/-- Two vectors have inner product 0 if and only if the angle between
them is π/2. -/
lemma inner_eq_zero_iff_angle_eq_pi_div_two (x y : V) : ⟪x, y⟫ = 0 ↔ angle x y = π / 2 :=
begin
split,
{ intro h,
unfold angle,
rw [h, zero_div, real.arccos_zero] },
{ intro h,
unfold angle at h,
rw ←real.arccos_zero at h,
have h2 : inner x y / (∥x∥ * ∥y∥) = 0 :=
real.arccos_inj (abs_le.mp (abs_real_inner_div_norm_mul_norm_le_one x y)).1
(abs_le.mp (abs_real_inner_div_norm_mul_norm_le_one x y)).2
(by norm_num)
(by norm_num)
h,
by_cases h : (∥x∥ * ∥y∥) = 0,
{ cases eq_zero_or_eq_zero_of_mul_eq_zero h with hx hy,
{ rw norm_eq_zero at hx,
rw [hx, inner_zero_left] },
{ rw norm_eq_zero at hy,
rw [hy, inner_zero_right] } },
{ simpa [h, div_eq_zero_iff] using h2 } },
end
end inner_product_geometry
namespace euclidean_geometry
/-!
### Geometrical results on Euclidean affine spaces
This section develops some geometrical definitions and results on
Euclidean affine spaces.
-/
open inner_product_geometry
variables {V : Type*} {P : Type*} [inner_product_space ℝ V] [metric_space P]
[normed_add_torsor V P]
local notation `⟪`x`, `y`⟫` := @inner ℝ V _ x y
include V
/-- The undirected angle at `p2` between the line segments to `p1` and
`p3`. If either of those points equals `p2`, this is π/2. Use
`open_locale euclidean_geometry` to access the `∠ p1 p2 p3`
notation. -/
def angle (p1 p2 p3 : P) : ℝ := angle (p1 -ᵥ p2 : V) (p3 -ᵥ p2)
localized "notation `∠` := euclidean_geometry.angle" in euclidean_geometry
/-- The angle at a point does not depend on the order of the other two
points. -/
lemma angle_comm (p1 p2 p3 : P) : ∠ p1 p2 p3 = ∠ p3 p2 p1 :=
angle_comm _ _
/-- The angle at a point is nonnegative. -/
lemma angle_nonneg (p1 p2 p3 : P) : 0 ≤ ∠ p1 p2 p3 :=
angle_nonneg _ _
/-- The angle at a point is at most π. -/
lemma angle_le_pi (p1 p2 p3 : P) : ∠ p1 p2 p3 ≤ π :=
angle_le_pi _ _
/-- The angle ∠AAB at a point. -/
lemma angle_eq_left (p1 p2 : P) : ∠ p1 p1 p2 = π / 2 :=
begin
unfold angle,
rw vsub_self,
exact angle_zero_left _
end
/-- The angle ∠ABB at a point. -/
lemma angle_eq_right (p1 p2 : P) : ∠ p1 p2 p2 = π / 2 :=
by rw [angle_comm, angle_eq_left]
/-- The angle ∠ABA at a point. -/
lemma angle_eq_of_ne {p1 p2 : P} (h : p1 ≠ p2) : ∠ p1 p2 p1 = 0 :=
angle_self (λ he, h (vsub_eq_zero_iff_eq.1 he))
/-- If the angle ∠ABC at a point is π, the angle ∠BAC is 0. -/
lemma angle_eq_zero_of_angle_eq_pi_left {p1 p2 p3 : P} (h : ∠ p1 p2 p3 = π) :
∠ p2 p1 p3 = 0 :=
begin
unfold angle at h,
rw angle_eq_pi_iff at h,
rcases h with ⟨hp1p2, ⟨r, ⟨hr, hpr⟩⟩⟩,
unfold angle,
rw angle_eq_zero_iff,
rw [←neg_vsub_eq_vsub_rev, neg_ne_zero] at hp1p2,
use [hp1p2, -r + 1, add_pos (neg_pos_of_neg hr) zero_lt_one],
rw [add_smul, ←neg_vsub_eq_vsub_rev p1 p2, smul_neg],
simp [←hpr]
end
/-- If the angle ∠ABC at a point is π, the angle ∠BCA is 0. -/
lemma angle_eq_zero_of_angle_eq_pi_right {p1 p2 p3 : P} (h : ∠ p1 p2 p3 = π) :
∠ p2 p3 p1 = 0 :=
begin
rw angle_comm at h,
exact angle_eq_zero_of_angle_eq_pi_left h
end
/-- If ∠BCD = π, then ∠ABC = ∠ABD. -/
lemma angle_eq_angle_of_angle_eq_pi (p1 : P) {p2 p3 p4 : P} (h : ∠ p2 p3 p4 = π) :
∠ p1 p2 p3 = ∠ p1 p2 p4 :=
begin
unfold angle at h,
rw angle_eq_pi_iff at h,
rcases h with ⟨hp2p3, ⟨r, ⟨hr, hpr⟩⟩⟩,
unfold angle,
symmetry,
convert angle_smul_right_of_pos _ _ (add_pos (neg_pos_of_neg hr) zero_lt_one),
rw [add_smul, ←neg_vsub_eq_vsub_rev p2 p3, smul_neg],
simp [←hpr]
end
/-- If ∠BCD = π, then ∠ACB + ∠ACD = π. -/
lemma angle_add_angle_eq_pi_of_angle_eq_pi (p1 : P) {p2 p3 p4 : P} (h : ∠ p2 p3 p4 = π) :
∠ p1 p3 p2 + ∠ p1 p3 p4 = π :=
begin
unfold angle at h,
rw [angle_comm p1 p3 p2, angle_comm p1 p3 p4],
unfold angle,
exact angle_add_angle_eq_pi_of_angle_eq_pi _ h
end
/-- The inner product of two vectors given with `weighted_vsub`, in
terms of the pairwise distances. -/
lemma inner_weighted_vsub {ι₁ : Type*} {s₁ : finset ι₁} {w₁ : ι₁ → ℝ} (p₁ : ι₁ → P)
(h₁ : ∑ i in s₁, w₁ i = 0) {ι₂ : Type*} {s₂ : finset ι₂} {w₂ : ι₂ → ℝ} (p₂ : ι₂ → P)
(h₂ : ∑ i in s₂, w₂ i = 0) :
inner (s₁.weighted_vsub p₁ w₁) (s₂.weighted_vsub p₂ w₂) =
(-∑ i₁ in s₁, ∑ i₂ in s₂,
w₁ i₁ * w₂ i₂ * (dist (p₁ i₁) (p₂ i₂) * dist (p₁ i₁) (p₂ i₂))) / 2 :=
begin
rw [finset.weighted_vsub_apply, finset.weighted_vsub_apply,
inner_sum_smul_sum_smul_of_sum_eq_zero _ h₁ _ h₂],
simp_rw [vsub_sub_vsub_cancel_right],
rcongr i₁ i₂; rw dist_eq_norm_vsub V (p₁ i₁) (p₂ i₂)
end
/-- The distance between two points given with `affine_combination`,
in terms of the pairwise distances between the points in that
combination. -/
lemma dist_affine_combination {ι : Type*} {s : finset ι} {w₁ w₂ : ι → ℝ} (p : ι → P)
(h₁ : ∑ i in s, w₁ i = 1) (h₂ : ∑ i in s, w₂ i = 1) :
dist (s.affine_combination p w₁) (s.affine_combination p w₂) *
dist (s.affine_combination p w₁) (s.affine_combination p w₂) =
(-∑ i₁ in s, ∑ i₂ in s,
(w₁ - w₂) i₁ * (w₁ - w₂) i₂ * (dist (p i₁) (p i₂) * dist (p i₁) (p i₂))) / 2 :=
begin
rw [dist_eq_norm_vsub V (s.affine_combination p w₁) (s.affine_combination p w₂),
←inner_self_eq_norm_square, finset.affine_combination_vsub],
have h : ∑ i in s, (w₁ - w₂) i = 0,
{ simp_rw [pi.sub_apply, finset.sum_sub_distrib, h₁, h₂, sub_self] },
exact inner_weighted_vsub p h p h
end
/-- Suppose that `c₁` is equidistant from `p₁` and `p₂`, and the same
applies to `c₂`. Then the vector between `c₁` and `c₂` is orthogonal
to that between `p₁` and `p₂`. (In two dimensions, this says that the
diagonals of a kite are orthogonal.) -/
lemma inner_vsub_vsub_of_dist_eq_of_dist_eq {c₁ c₂ p₁ p₂ : P} (hc₁ : dist p₁ c₁ = dist p₂ c₁)
(hc₂ : dist p₁ c₂ = dist p₂ c₂) : ⟪c₂ -ᵥ c₁, p₂ -ᵥ p₁⟫ = 0 :=
begin
have h : ⟪(c₂ -ᵥ c₁) + (c₂ -ᵥ c₁), p₂ -ᵥ p₁⟫ = 0,
{ conv_lhs { congr, congr, rw ←vsub_sub_vsub_cancel_right c₂ c₁ p₁,
skip, rw ←vsub_sub_vsub_cancel_right c₂ c₁ p₂ },
rw [←add_sub_comm, inner_sub_left],
conv_lhs { congr, rw ←vsub_sub_vsub_cancel_right p₂ p₁ c₂,
skip, rw ←vsub_sub_vsub_cancel_right p₂ p₁ c₁ },
rw [dist_comm p₁, dist_comm p₂, dist_eq_norm_vsub V _ p₁,
dist_eq_norm_vsub V _ p₂, ←real_inner_add_sub_eq_zero_iff] at hc₁ hc₂,
simp_rw [←neg_vsub_eq_vsub_rev c₁, ←neg_vsub_eq_vsub_rev c₂, sub_neg_eq_add,
neg_add_eq_sub, hc₁, hc₂, sub_zero] },
simpa [inner_add_left, ←mul_two, (by norm_num : (2 : ℝ) ≠ 0)] using h
end
/-- The squared distance between points on a line (expressed as a
multiple of a fixed vector added to a point) and another point,
expressed as a quadratic. -/
lemma dist_smul_vadd_square (r : ℝ) (v : V) (p₁ p₂ : P) :
dist (r • v +ᵥ p₁) p₂ * dist (r • v +ᵥ p₁) p₂ =
⟪v, v⟫ * r * r + 2 * ⟪v, p₁ -ᵥ p₂⟫ * r + ⟪p₁ -ᵥ p₂, p₁ -ᵥ p₂⟫ :=
begin
rw [dist_eq_norm_vsub V _ p₂, ←real_inner_self_eq_norm_square, vadd_vsub_assoc, real_inner_add_add_self,
real_inner_smul_left, real_inner_smul_left, real_inner_smul_right],
ring
end
/-- The condition for two points on a line to be equidistant from
another point. -/
lemma dist_smul_vadd_eq_dist {v : V} (p₁ p₂ : P) (hv : v ≠ 0) (r : ℝ) :
dist (r • v +ᵥ p₁) p₂ = dist p₁ p₂ ↔ (r = 0 ∨ r = -2 * ⟪v, p₁ -ᵥ p₂⟫ / ⟪v, v⟫) :=
begin
conv_lhs { rw [←mul_self_inj_of_nonneg dist_nonneg dist_nonneg, dist_smul_vadd_square,
←sub_eq_zero_iff_eq, add_sub_assoc, dist_eq_norm_vsub V p₁ p₂,
←real_inner_self_eq_norm_square, sub_self] },
have hvi : ⟪v, v⟫ ≠ 0, by simpa using hv,
have hd : discrim ⟪v, v⟫ (2 * ⟪v, p₁ -ᵥ p₂⟫) 0 =
(2 * inner v (p₁ -ᵥ p₂)) * (2 * inner v (p₁ -ᵥ p₂)),
{ rw discrim, ring },
rw [quadratic_eq_zero_iff hvi hd, add_left_neg, zero_div, neg_mul_eq_neg_mul,
←mul_sub_right_distrib, sub_eq_add_neg, ←mul_two, mul_assoc, mul_div_assoc,
mul_div_mul_left, mul_div_assoc],
norm_num
end
open affine_subspace finite_dimensional
/-- Distances `r₁` `r₂` of `p` from two different points `c₁` `c₂` determine at
most two points `p₁` `p₂` in a two-dimensional subspace containing those points
(two circles intersect in at most two points). -/
lemma eq_of_dist_eq_of_dist_eq_of_mem_of_findim_eq_two {s : affine_subspace ℝ P}
[finite_dimensional ℝ s.direction] (hd : findim ℝ s.direction = 2) {c₁ c₂ p₁ p₂ p : P}
(hc₁s : c₁ ∈ s) (hc₂s : c₂ ∈ s) (hp₁s : p₁ ∈ s) (hp₂s : p₂ ∈ s) (hps : p ∈ s) {r₁ r₂ : ℝ}
(hc : c₁ ≠ c₂) (hp : p₁ ≠ p₂) (hp₁c₁ : dist p₁ c₁ = r₁) (hp₂c₁ : dist p₂ c₁ = r₁)
(hpc₁ : dist p c₁ = r₁) (hp₁c₂ : dist p₁ c₂ = r₂) (hp₂c₂ : dist p₂ c₂ = r₂)
(hpc₂ : dist p c₂ = r₂) : p = p₁ ∨ p = p₂ :=
begin
have ho : ⟪c₂ -ᵥ c₁, p₂ -ᵥ p₁⟫ = 0 :=
inner_vsub_vsub_of_dist_eq_of_dist_eq (by cc) (by cc),
have hop : ⟪c₂ -ᵥ c₁, p -ᵥ p₁⟫ = 0 :=
inner_vsub_vsub_of_dist_eq_of_dist_eq (by cc) (by cc),
let b : fin 2 → V := ![c₂ -ᵥ c₁, p₂ -ᵥ p₁],
have hb : linear_independent ℝ b,
{ refine linear_independent_of_ne_zero_of_inner_eq_zero _ _,
{ intro i,
fin_cases i; simp [b, hc.symm, hp.symm] },
{ intros i j hij,
fin_cases i; fin_cases j; try { exact false.elim (hij rfl) },
{ exact ho },
{ rw real_inner_comm, exact ho } } },
have hbs : submodule.span ℝ (set.range b) = s.direction,
{ refine eq_of_le_of_findim_eq _ _,
{ rw [submodule.span_le, set.range_subset_iff],
intro i,
fin_cases i,
{ exact vsub_mem_direction hc₂s hc₁s },
{ exact vsub_mem_direction hp₂s hp₁s } },
{ rw [findim_span_eq_card hb, fintype.card_fin, hd] } },
have hv : ∀ v ∈ s.direction, ∃ t₁ t₂ : ℝ, v = t₁ • (c₂ -ᵥ c₁) + t₂ • (p₂ -ᵥ p₁),
{ intros v hv,
have hr : set.range b = {c₂ -ᵥ c₁, p₂ -ᵥ p₁},
{ have hu : (finset.univ : finset (fin 2)) = {0, 1}, by dec_trivial,
rw [←fintype.coe_image_univ, hu],
simp,
refl },
rw [←hbs, hr, submodule.mem_span_insert] at hv,
rcases hv with ⟨t₁, v', hv', hv⟩,
rw submodule.mem_span_singleton at hv',
rcases hv' with ⟨t₂, rfl⟩,
exact ⟨t₁, t₂, hv⟩ },
rcases hv (p -ᵥ p₁) (vsub_mem_direction hps hp₁s) with ⟨t₁, t₂, hpt⟩,
simp only [hpt, inner_add_right, inner_smul_right, ho, mul_zero, add_zero, mul_eq_zero,
inner_self_eq_zero, vsub_eq_zero_iff_eq, hc.symm, or_false] at hop,
rw [hop, zero_smul, zero_add, ←eq_vadd_iff_vsub_eq] at hpt,
subst hpt,
have hp' : (p₂ -ᵥ p₁ : V) ≠ 0, { simp [hp.symm] },
have hp₂ : dist ((1 : ℝ) • (p₂ -ᵥ p₁) +ᵥ p₁) c₁ = r₁, { simp [hp₂c₁] },
rw [←hp₁c₁, dist_smul_vadd_eq_dist _ _ hp'] at hpc₁ hp₂,
simp only [one_ne_zero, false_or] at hp₂,
rw hp₂.symm at hpc₁,
cases hpc₁; simp [hpc₁]
end
/-- Distances `r₁` `r₂` of `p` from two different points `c₁` `c₂` determine at
most two points `p₁` `p₂` in two-dimensional space (two circles intersect in at
most two points). -/
lemma eq_of_dist_eq_of_dist_eq_of_findim_eq_two [finite_dimensional ℝ V] (hd : findim ℝ V = 2)
{c₁ c₂ p₁ p₂ p : P} {r₁ r₂ : ℝ} (hc : c₁ ≠ c₂) (hp : p₁ ≠ p₂) (hp₁c₁ : dist p₁ c₁ = r₁)
(hp₂c₁ : dist p₂ c₁ = r₁) (hpc₁ : dist p c₁ = r₁) (hp₁c₂ : dist p₁ c₂ = r₂)
(hp₂c₂ : dist p₂ c₂ = r₂) (hpc₂ : dist p c₂ = r₂) : p = p₁ ∨ p = p₂ :=
begin
have hd' : findim ℝ (⊤ : affine_subspace ℝ P).direction = 2,
{ rw [direction_top, findim_top],
exact hd },
exact eq_of_dist_eq_of_dist_eq_of_mem_of_findim_eq_two hd'
(mem_top ℝ V _) (mem_top ℝ V _) (mem_top ℝ V _) (mem_top ℝ V _) (mem_top ℝ V _)
hc hp hp₁c₁ hp₂c₁ hpc₁ hp₁c₂ hp₂c₂ hpc₂
end
variables {V}
/-- The orthogonal projection of a point onto a nonempty affine
subspace, whose direction is complete, as an unbundled function. This
definition is only intended for use in setting up the bundled version
`orthogonal_projection` and should not be used once that is
defined. -/
def orthogonal_projection_fn (s : affine_subspace ℝ P) [nonempty s] [complete_space s.direction]
(p : P) : P :=
classical.some $ inter_eq_singleton_of_nonempty_of_is_compl
(nonempty_subtype.mp ‹_›)
(mk'_nonempty p s.directionᗮ)
begin
convert submodule.is_compl_orthogonal_of_is_complete (complete_space_coe_iff_is_complete.mp ‹_›),
exact direction_mk' p s.directionᗮ
end
/-- The intersection of the subspace and the orthogonal subspace
through the given point is the `orthogonal_projection_fn` of that
point onto the subspace. This lemma is only intended for use in
setting up the bundled version and should not be used once that is
defined. -/
lemma inter_eq_singleton_orthogonal_projection_fn {s : affine_subspace ℝ P} [nonempty s]
[complete_space s.direction] (p : P) :
(s : set P) ∩ (mk' p s.directionᗮ) = {orthogonal_projection_fn s p} :=
classical.some_spec $ inter_eq_singleton_of_nonempty_of_is_compl
(nonempty_subtype.mp ‹_›)
(mk'_nonempty p s.directionᗮ)
begin
convert submodule.is_compl_orthogonal_of_is_complete (complete_space_coe_iff_is_complete.mp ‹_›),
exact direction_mk' p s.directionᗮ
end
/-- The `orthogonal_projection_fn` lies in the given subspace. This
lemma is only intended for use in setting up the bundled version and
should not be used once that is defined. -/
lemma orthogonal_projection_fn_mem {s : affine_subspace ℝ P} [nonempty s]
[complete_space s.direction] (p : P) : orthogonal_projection_fn s p ∈ s :=
begin
rw [←mem_coe, ←set.singleton_subset_iff, ←inter_eq_singleton_orthogonal_projection_fn],
exact set.inter_subset_left _ _
end
/-- The `orthogonal_projection_fn` lies in the orthogonal
subspace. This lemma is only intended for use in setting up the
bundled version and should not be used once that is defined. -/
lemma orthogonal_projection_fn_mem_orthogonal {s : affine_subspace ℝ P} [nonempty s]
[complete_space s.direction] (p : P) :
orthogonal_projection_fn s p ∈ mk' p s.directionᗮ :=
begin
rw [←mem_coe, ←set.singleton_subset_iff, ←inter_eq_singleton_orthogonal_projection_fn],
exact set.inter_subset_right _ _
end
/-- Subtracting `p` from its `orthogonal_projection_fn` produces a
result in the orthogonal direction. This lemma is only intended for
use in setting up the bundled version and should not be used once that
is defined. -/
lemma orthogonal_projection_fn_vsub_mem_direction_orthogonal {s : affine_subspace ℝ P} [nonempty s]
[complete_space s.direction] (p : P) :
orthogonal_projection_fn s p -ᵥ p ∈ s.directionᗮ :=
direction_mk' p s.directionᗮ ▸
vsub_mem_direction (orthogonal_projection_fn_mem_orthogonal p) (self_mem_mk' _ _)
/-- The orthogonal projection of a point onto a nonempty affine
subspace, whose direction is complete. The corresponding linear map
(mapping a vector to the difference between the projections of two
points whose difference is that vector) is the `orthogonal_projection`
for real inner product spaces, onto the direction of the affine
subspace being projected onto. -/
def orthogonal_projection (s : affine_subspace ℝ P) [nonempty s] [complete_space s.direction] :
P →ᵃ[ℝ] s :=
{ to_fun := λ p, ⟨orthogonal_projection_fn s p, orthogonal_projection_fn_mem p⟩,
linear := orthogonal_projection s.direction,
map_vadd' := λ p v, begin
have hs : ((orthogonal_projection s.direction) v : V) +ᵥ orthogonal_projection_fn s p ∈ s :=
vadd_mem_of_mem_direction (orthogonal_projection s.direction v).2
(orthogonal_projection_fn_mem p),
have ho : ((orthogonal_projection s.direction) v : V) +ᵥ orthogonal_projection_fn s p ∈
mk' (v +ᵥ p) s.directionᗮ,
{ rw [←vsub_right_mem_direction_iff_mem (self_mem_mk' _ _) _, direction_mk',
vsub_vadd_eq_vsub_sub, vadd_vsub_assoc, add_comm, add_sub_assoc],
refine submodule.add_mem _ (orthogonal_projection_fn_vsub_mem_direction_orthogonal p) _,
rw submodule.mem_orthogonal',
intros w hw,
rw [←neg_sub, inner_neg_left, orthogonal_projection_inner_eq_zero _ w hw, neg_zero], },
have hm : ((orthogonal_projection s.direction) v : V) +ᵥ orthogonal_projection_fn s p ∈
({orthogonal_projection_fn s (v +ᵥ p)} : set P),
{ rw ←inter_eq_singleton_orthogonal_projection_fn (v +ᵥ p),
exact set.mem_inter hs ho },
rw set.mem_singleton_iff at hm,
ext,
exact hm.symm
end }
@[simp] lemma orthogonal_projection_fn_eq {s : affine_subspace ℝ P} [nonempty s]
[complete_space s.direction] (p : P) :
orthogonal_projection_fn s p = orthogonal_projection s p :=
rfl
/-- The linear map corresponding to `orthogonal_projection`. -/
@[simp] lemma orthogonal_projection_linear {s : affine_subspace ℝ P} [nonempty s]
[complete_space s.direction] :
(orthogonal_projection s).linear = _root_.orthogonal_projection s.direction :=
rfl
/-- The intersection of the subspace and the orthogonal subspace
through the given point is the `orthogonal_projection` of that point
onto the subspace. -/
lemma inter_eq_singleton_orthogonal_projection {s : affine_subspace ℝ P} [nonempty s]
[complete_space s.direction] (p : P) :
(s : set P) ∩ (mk' p s.directionᗮ) = {orthogonal_projection s p} :=
begin
rw ←orthogonal_projection_fn_eq,
exact inter_eq_singleton_orthogonal_projection_fn p
end
/-- The `orthogonal_projection` lies in the given subspace. -/
lemma orthogonal_projection_mem {s : affine_subspace ℝ P} [nonempty s] [complete_space s.direction]
(p : P) : ↑(orthogonal_projection s p) ∈ s :=
(orthogonal_projection s p).2
/-- The `orthogonal_projection` lies in the orthogonal subspace. -/
lemma orthogonal_projection_mem_orthogonal (s : affine_subspace ℝ P) [nonempty s]
[complete_space s.direction] (p : P) :
↑(orthogonal_projection s p) ∈ mk' p s.directionᗮ :=
orthogonal_projection_fn_mem_orthogonal p
/-- Subtracting a point in the given subspace from the
`orthogonal_projection` produces a result in the direction of the
given subspace. -/
lemma orthogonal_projection_vsub_mem_direction {s : affine_subspace ℝ P} [nonempty s]
[complete_space s.direction] {p1 : P} (p2 : P) (hp1 : p1 ∈ s) :
↑(orthogonal_projection s p2 -ᵥ ⟨p1, hp1⟩ : s.direction) ∈ s.direction :=
(orthogonal_projection s p2 -ᵥ ⟨p1, hp1⟩ : s.direction).2
/-- Subtracting the `orthogonal_projection` from a point in the given
subspace produces a result in the direction of the given subspace. -/
lemma vsub_orthogonal_projection_mem_direction {s : affine_subspace ℝ P} [nonempty s]
[complete_space s.direction] {p1 : P} (p2 : P) (hp1 : p1 ∈ s) :
↑((⟨p1, hp1⟩ : s) -ᵥ orthogonal_projection s p2 : s.direction) ∈ s.direction :=
((⟨p1, hp1⟩ : s) -ᵥ orthogonal_projection s p2 : s.direction).2
/-- A point equals its orthogonal projection if and only if it lies in
the subspace. -/
lemma orthogonal_projection_eq_self_iff {s : affine_subspace ℝ P} [nonempty s]
[complete_space s.direction] {p : P} :
↑(orthogonal_projection s p) = p ↔ p ∈ s :=
begin
split,
{ exact λ h, h ▸ orthogonal_projection_mem p },
{ intro h,
have hp : p ∈ ((s : set P) ∩ mk' p s.directionᗮ) := ⟨h, self_mem_mk' p _⟩,
rw [inter_eq_singleton_orthogonal_projection p] at hp,
symmetry,
exact hp }
end
/-- Orthogonal projection is idempotent. -/
@[simp] lemma orthogonal_projection_orthogonal_projection (s : affine_subspace ℝ P) [nonempty s]
[complete_space s.direction] (p : P) :
orthogonal_projection s (orthogonal_projection s p) = orthogonal_projection s p :=
begin
ext,
rw orthogonal_projection_eq_self_iff,
exact orthogonal_projection_mem p,
end
lemma eq_orthogonal_projection_of_eq_subspace {s s' : affine_subspace ℝ P} [nonempty s]
[nonempty s'] [complete_space s.direction] [complete_space s'.direction] (h : s = s') (p : P) :
(orthogonal_projection s p : P) = (orthogonal_projection s' p : P) :=
begin
change orthogonal_projection_fn s p = orthogonal_projection_fn s' p,
congr,
exact h
end
/-- The distance to a point's orthogonal projection is 0 iff it lies in the subspace. -/
lemma dist_orthogonal_projection_eq_zero_iff {s : affine_subspace ℝ P} [nonempty s]
[complete_space s.direction] {p : P} :
dist p (orthogonal_projection s p) = 0 ↔ p ∈ s :=
by rw [dist_comm, dist_eq_zero, orthogonal_projection_eq_self_iff]
/-- The distance between a point and its orthogonal projection is
nonzero if it does not lie in the subspace. -/
lemma dist_orthogonal_projection_ne_zero_of_not_mem {s : affine_subspace ℝ P} [nonempty s]
[complete_space s.direction] {p : P} (hp : p ∉ s) :
dist p (orthogonal_projection s p) ≠ 0 :=
mt dist_orthogonal_projection_eq_zero_iff.mp hp
/-- Subtracting `p` from its `orthogonal_projection` produces a result
in the orthogonal direction. -/
lemma orthogonal_projection_vsub_mem_direction_orthogonal (s : affine_subspace ℝ P) [nonempty s]
[complete_space s.direction] (p : P) :
(orthogonal_projection s p : P) -ᵥ p ∈ s.directionᗮ :=
orthogonal_projection_fn_vsub_mem_direction_orthogonal p
/-- Subtracting the `orthogonal_projection` from `p` produces a result
in the orthogonal direction. -/
lemma vsub_orthogonal_projection_mem_direction_orthogonal (s : affine_subspace ℝ P) [nonempty s]
[complete_space s.direction] (p : P) :
p -ᵥ orthogonal_projection s p ∈ s.directionᗮ :=
direction_mk' p s.directionᗮ ▸
vsub_mem_direction (self_mem_mk' _ _) (orthogonal_projection_mem_orthogonal s p)
/-- Adding a vector to a point in the given subspace, then taking the
orthogonal projection, produces the original point if the vector was
in the orthogonal direction. -/
lemma orthogonal_projection_vadd_eq_self {s : affine_subspace ℝ P} [nonempty s]
[complete_space s.direction] {p : P} (hp : p ∈ s) {v : V}
(hv : v ∈ s.directionᗮ) :
orthogonal_projection s (v +ᵥ p) = ⟨p, hp⟩ :=
begin
have h := vsub_orthogonal_projection_mem_direction_orthogonal s (v +ᵥ p),
rw [vadd_vsub_assoc, submodule.add_mem_iff_right _ hv] at h,
refine (eq_of_vsub_eq_zero _).symm,
ext,
refine submodule.disjoint_def.1 s.direction.orthogonal_disjoint _ _ h,
exact (_ : s.direction).2
end
/-- Adding a vector to a point in the given subspace, then taking the
orthogonal projection, produces the original point if the vector is a
multiple of the result of subtracting a point's orthogonal projection
from that point. -/
lemma orthogonal_projection_vadd_smul_vsub_orthogonal_projection {s : affine_subspace ℝ P}
[nonempty s] [complete_space s.direction] {p1 : P} (p2 : P) (r : ℝ) (hp : p1 ∈ s) :
orthogonal_projection s (r • (p2 -ᵥ orthogonal_projection s p2 : V) +ᵥ p1) = ⟨p1, hp⟩ :=
orthogonal_projection_vadd_eq_self hp
(submodule.smul_mem _ _ (vsub_orthogonal_projection_mem_direction_orthogonal s _))
/-- The square of the distance from a point in `s` to `p2` equals the
sum of the squares of the distances of the two points to the
`orthogonal_projection`. -/
lemma dist_square_eq_dist_orthogonal_projection_square_add_dist_orthogonal_projection_square
{s : affine_subspace ℝ P} [nonempty s] [complete_space s.direction] {p1 : P}
(p2 : P) (hp1 : p1 ∈ s) :
dist p1 p2 * dist p1 p2 =
dist p1 (orthogonal_projection s p2) * dist p1 (orthogonal_projection s p2) +
dist p2 (orthogonal_projection s p2) * dist p2 (orthogonal_projection s p2) :=
begin
rw [metric_space.dist_comm p2 _, dist_eq_norm_vsub V p1 _, dist_eq_norm_vsub V p1 _,
dist_eq_norm_vsub V _ p2, ← vsub_add_vsub_cancel p1 (orthogonal_projection s p2) p2,
norm_add_square_eq_norm_square_add_norm_square_iff_real_inner_eq_zero],
exact submodule.inner_right_of_mem_orthogonal
(vsub_orthogonal_projection_mem_direction p2 hp1)
(orthogonal_projection_vsub_mem_direction_orthogonal s p2),
end
/-- The square of the distance between two points constructed by
adding multiples of the same orthogonal vector to points in the same
subspace. -/
lemma dist_square_smul_orthogonal_vadd_smul_orthogonal_vadd {s : affine_subspace ℝ P}
{p1 p2 : P} (hp1 : p1 ∈ s) (hp2 : p2 ∈ s) (r1 r2 : ℝ) {v : V}
(hv : v ∈ s.directionᗮ) :
dist (r1 • v +ᵥ p1) (r2 • v +ᵥ p2) * dist (r1 • v +ᵥ p1) (r2 • v +ᵥ p2) =
dist p1 p2 * dist p1 p2 + (r1 - r2) * (r1 - r2) * (∥v∥ * ∥v∥) :=
calc dist (r1 • v +ᵥ p1) (r2 • v +ᵥ p2) * dist (r1 • v +ᵥ p1) (r2 • v +ᵥ p2)
= ∥(p1 -ᵥ p2) + (r1 - r2) • v∥ * ∥(p1 -ᵥ p2) + (r1 - r2) • v∥
: by { rw [dist_eq_norm_vsub V (r1 • v +ᵥ p1), vsub_vadd_eq_vsub_sub, vadd_vsub_assoc, sub_smul],
abel }
... = ∥p1 -ᵥ p2∥ * ∥p1 -ᵥ p2∥ + ∥(r1 - r2) • v∥ * ∥(r1 - r2) • v∥
: norm_add_square_eq_norm_square_add_norm_square_real
(submodule.inner_right_of_mem_orthogonal (vsub_mem_direction hp1 hp2)
(submodule.smul_mem _ _ hv))
... = ∥(p1 -ᵥ p2 : V)∥ * ∥(p1 -ᵥ p2 : V)∥ + abs (r1 - r2) * abs (r1 - r2) * ∥v∥ * ∥v∥
: by { rw [norm_smul, real.norm_eq_abs], ring }
... = dist p1 p2 * dist p1 p2 + (r1 - r2) * (r1 - r2) * (∥v∥ * ∥v∥)
: by { rw [dist_eq_norm_vsub V p1, abs_mul_abs_self, mul_assoc] }
/-- Reflection in an affine subspace, which is expected to be nonempty
and complete. The word "reflection" is sometimes understood to mean
specifically reflection in a codimension-one subspace, and sometimes
more generally to cover operations such as reflection in a point. The
definition here, of reflection in an affine subspace, is a more
general sense of the word that includes both those common cases. If
the subspace is empty or not complete, `orthogonal_projection` is
defined as the identity map, which results in `reflection` being the
identity map in that case as well. -/
def reflection (s : affine_subspace ℝ P) [nonempty s] [complete_space s.direction] :
P ≃ᵢ P :=
{ to_fun := λ p, (↑(orthogonal_projection s p) -ᵥ p) +ᵥ orthogonal_projection s p,
inv_fun := λ p, (↑(orthogonal_projection s p) -ᵥ p) +ᵥ orthogonal_projection s p,
left_inv := λ p, by simp [vsub_vadd_eq_vsub_sub, -orthogonal_projection_linear],
right_inv := λ p, by simp [vsub_vadd_eq_vsub_sub, -orthogonal_projection_linear],
isometry_to_fun := begin
dsimp only,
rw isometry_emetric_iff_metric,
intros p₁ p₂,
rw [←mul_self_inj_of_nonneg dist_nonneg dist_nonneg, dist_eq_norm_vsub V
((↑(orthogonal_projection s p₁) -ᵥ p₁) +ᵥ ↑(orthogonal_projection s p₁)),
dist_eq_norm_vsub V p₁, ←inner_self_eq_norm_square, ←inner_self_eq_norm_square],
calc
⟪((orthogonal_projection s p₁ : P) -ᵥ p₁ +ᵥ (orthogonal_projection s p₁ : P) -ᵥ
((orthogonal_projection s p₂ : P) -ᵥ p₂ +ᵥ orthogonal_projection s p₂)),
((orthogonal_projection s p₁ : P) -ᵥ p₁ +ᵥ (orthogonal_projection s p₁ : P) -ᵥ
((orthogonal_projection s p₂ : P) -ᵥ p₂ +ᵥ orthogonal_projection s p₂))⟫
= ⟪(_root_.orthogonal_projection s.direction (p₁ -ᵥ p₂)) +
_root_.orthogonal_projection s.direction (p₁ -ᵥ p₂) - (p₁ -ᵥ p₂),
_root_.orthogonal_projection s.direction (p₁ -ᵥ p₂) +
_root_.orthogonal_projection s.direction (p₁ -ᵥ p₂) - (p₁ -ᵥ p₂)⟫
: by { rw [vsub_vadd_eq_vsub_sub, vadd_vsub_assoc, add_comm, add_sub_assoc,
←vsub_vadd_eq_vsub_sub, vsub_vadd_comm, vsub_vadd_eq_vsub_sub, ←add_sub_assoc, ←coe_vsub,
←affine_map.linear_map_vsub], simp }
... = -4 * inner (p₁ -ᵥ p₂ - (_root_.orthogonal_projection s.direction (p₁ -ᵥ p₂) : V))
(_root_.orthogonal_projection s.direction (p₁ -ᵥ p₂)) +
⟪p₁ -ᵥ p₂, p₁ -ᵥ p₂⟫
: by { simp [inner_sub_left, inner_sub_right, inner_add_left, inner_add_right,
real_inner_comm (p₁ -ᵥ p₂)], ring }
... = ⟪p₁ -ᵥ p₂, p₁ -ᵥ p₂⟫ : by simp,
end }
/-- The result of reflecting. -/
lemma reflection_apply (s : affine_subspace ℝ P) [nonempty s] [complete_space s.direction] (p : P) :
reflection s p = (↑(orthogonal_projection s p) -ᵥ p) +ᵥ orthogonal_projection s p :=
rfl
lemma eq_reflection_of_eq_subspace {s s' : affine_subspace ℝ P} [nonempty s]
[nonempty s'] [complete_space s.direction] [complete_space s'.direction] (h : s = s') (p : P) :
(reflection s p : P) = (reflection s' p : P) :=
by simp [reflection_apply, eq_orthogonal_projection_of_eq_subspace h]
/-- Reflection is its own inverse. -/
@[simp] lemma reflection_symm (s : affine_subspace ℝ P) [nonempty s] [complete_space s.direction] :
(reflection s).symm = reflection s :=
rfl
/-- Reflecting twice in the same subspace. -/
@[simp] lemma reflection_reflection (s : affine_subspace ℝ P) [nonempty s]
[complete_space s.direction] (p : P) :
reflection s (reflection s p) = p :=
(reflection s).left_inv p
/-- Reflection is involutive. -/
lemma reflection_involutive (s : affine_subspace ℝ P) [nonempty s] [complete_space s.direction] :
function.involutive (reflection s) :=
reflection_reflection s
/-- A point is its own reflection if and only if it is in the
subspace. -/
lemma reflection_eq_self_iff {s : affine_subspace ℝ P} [nonempty s] [complete_space s.direction]
(p : P) : reflection s p = p ↔ p ∈ s :=
begin
rw [←orthogonal_projection_eq_self_iff, reflection_apply],
split,
{ intro h,
rw [←@vsub_eq_zero_iff_eq V, vadd_vsub_assoc,
←two_smul ℝ (↑(orthogonal_projection s p) -ᵥ p), smul_eq_zero] at h,
norm_num at h,
exact h },
{ intro h,
simp [h] }
end
/-- Reflecting a point in two subspaces produces the same result if
and only if the point has the same orthogonal projection in each of
those subspaces. -/
lemma reflection_eq_iff_orthogonal_projection_eq (s₁ s₂ : affine_subspace ℝ P)
[nonempty s₁] [nonempty s₂] [complete_space s₁.direction] [complete_space s₂.direction] (p : P) :
reflection s₁ p = reflection s₂ p ↔
(orthogonal_projection s₁ p : P) = orthogonal_projection s₂ p :=
begin
rw [reflection_apply, reflection_apply],
split,
{ intro h,
rw [←@vsub_eq_zero_iff_eq V, vsub_vadd_eq_vsub_sub, vadd_vsub_assoc, add_comm,
add_sub_assoc, vsub_sub_vsub_cancel_right,
←two_smul ℝ ((orthogonal_projection s₁ p : P) -ᵥ orthogonal_projection s₂ p),
smul_eq_zero] at h,
norm_num at h,
exact h },
{ intro h,
rw h }
end
/-- The distance between `p₁` and the reflection of `p₂` equals that
between the reflection of `p₁` and `p₂`. -/
lemma dist_reflection (s : affine_subspace ℝ P) [nonempty s] [complete_space s.direction] (p₁ p₂ : P) :
dist p₁ (reflection s p₂) = dist (reflection s p₁) p₂ :=
begin
conv_lhs { rw ←reflection_reflection s p₁ },
exact (reflection s).dist_eq _ _
end
/-- A point in the subspace is equidistant from another point and its
reflection. -/
lemma dist_reflection_eq_of_mem (s : affine_subspace ℝ P) [nonempty s] [complete_space s.direction]
{p₁ : P} (hp₁ : p₁ ∈ s) (p₂ : P) :
dist p₁ (reflection s p₂) = dist p₁ p₂ :=
begin
rw ←reflection_eq_self_iff p₁ at hp₁,
convert (reflection s).dist_eq p₁ p₂,
rw hp₁
end
/-- The reflection of a point in a subspace is contained in any larger
subspace containing both the point and the subspace reflected in. -/
lemma reflection_mem_of_le_of_mem {s₁ s₂ : affine_subspace ℝ P} [nonempty s₁]
[complete_space s₁.direction] (hle : s₁ ≤ s₂) {p : P}
(hp : p ∈ s₂) : reflection s₁ p ∈ s₂ :=
begin
rw [reflection_apply],
have ho : ↑(orthogonal_projection s₁ p) ∈ s₂ := hle (orthogonal_projection_mem p),
exact vadd_mem_of_mem_direction (vsub_mem_direction ho hp) ho
end
/-- Reflecting an orthogonal vector plus a point in the subspace
produces the negation of that vector plus the point. -/
lemma reflection_orthogonal_vadd {s : affine_subspace ℝ P} [nonempty s]
[complete_space s.direction] {p : P} (hp : p ∈ s) {v : V}
(hv : v ∈ s.directionᗮ) : reflection s (v +ᵥ p) = -v +ᵥ p :=
begin
rw [reflection_apply, orthogonal_projection_vadd_eq_self hp hv, vsub_vadd_eq_vsub_sub],
simp
end
/-- Reflecting a vector plus a point in the subspace produces the
negation of that vector plus the point if the vector is a multiple of
the result of subtracting a point's orthogonal projection from that
point. -/
lemma reflection_vadd_smul_vsub_orthogonal_projection {s : affine_subspace ℝ P} [nonempty s]
[complete_space s.direction] {p₁ : P} (p₂ : P) (r : ℝ) (hp₁ : p₁ ∈ s) :
reflection s (r • (p₂ -ᵥ orthogonal_projection s p₂) +ᵥ p₁) =
-(r • (p₂ -ᵥ orthogonal_projection s p₂)) +ᵥ p₁ :=
reflection_orthogonal_vadd hp₁
(submodule.smul_mem _ _ (vsub_orthogonal_projection_mem_direction_orthogonal s _))
omit V
/-- A set of points is cospherical if they are equidistant from some
point. In two dimensions, this is the same thing as being
concyclic. -/
def cospherical (ps : set P) : Prop :=
∃ (center : P) (radius : ℝ), ∀ p ∈ ps, dist p center = radius
/-- The definition of `cospherical`. -/
lemma cospherical_def (ps : set P) :
cospherical ps ↔ ∃ (center : P) (radius : ℝ), ∀ p ∈ ps, dist p center = radius :=
iff.rfl
/-- A subset of a cospherical set is cospherical. -/
lemma cospherical_subset {ps₁ ps₂ : set P} (hs : ps₁ ⊆ ps₂) (hc : cospherical ps₂) :
cospherical ps₁ :=
begin
rcases hc with ⟨c, r, hcr⟩,
exact ⟨c, r, λ p hp, hcr p (hs hp)⟩
end
include V
/-- The empty set is cospherical. -/
lemma cospherical_empty : cospherical (∅ : set P) :=
begin
use add_torsor.nonempty.some,
simp,
end
omit V
/-- A single point is cospherical. -/
lemma cospherical_singleton (p : P) : cospherical ({p} : set P) :=
begin
use p,
simp
end
include V
/-- Two points are cospherical. -/
lemma cospherical_insert_singleton (p₁ p₂ : P) : cospherical ({p₁, p₂} : set P) :=
begin
use [(2⁻¹ : ℝ) • (p₂ -ᵥ p₁) +ᵥ p₁, (2⁻¹ : ℝ) * (dist p₂ p₁)],
intro p,
rw [set.mem_insert_iff, set.mem_singleton_iff],
rintro ⟨_|_⟩,
{ rw [dist_eq_norm_vsub V p₁, vsub_vadd_eq_vsub_sub, vsub_self, zero_sub, norm_neg, norm_smul,
dist_eq_norm_vsub V p₂],
simp },
{ rw [H, dist_eq_norm_vsub V p₂, vsub_vadd_eq_vsub_sub, dist_eq_norm_vsub V p₂],
conv_lhs { congr, congr, rw ←one_smul ℝ (p₂ -ᵥ p₁ : V) },
rw [←sub_smul, norm_smul],
norm_num }
end
/-- Any three points in a cospherical set are affinely independent. -/
lemma cospherical.affine_independent {s : set P} (hs : cospherical s) {p : fin 3 → P}
(hps : set.range p ⊆ s) (hpi : function.injective p) :
affine_independent ℝ p :=
begin
rw affine_independent_iff_not_collinear,
intro hc,
rw collinear_iff_of_mem ℝ (set.mem_range_self (0 : fin 3)) at hc,
rcases hc with ⟨v, hv⟩,
rw set.forall_range_iff at hv,
have hv0 : v ≠ 0,
{ intro h,
have he : p 1 = p 0, by simpa [h] using hv 1,
exact (dec_trivial : (1 : fin 3) ≠ 0) (hpi he) },
rcases hs with ⟨c, r, hs⟩,
have hs' := λ i, hs (p i) (set.mem_of_mem_of_subset (set.mem_range_self _) hps),
choose f hf using hv,
have hsd : ∀ i, dist ((f i • v) +ᵥ p 0) c = r,
{ intro i,
rw ←hf,
exact hs' i },
have hf0 : f 0 = 0,
{ have hf0' := hf 0,
rw [eq_comm, ←@vsub_eq_zero_iff_eq V, vadd_vsub, smul_eq_zero] at hf0',
simpa [hv0] using hf0' },
have hfi : function.injective f,
{ intros i j h,
have hi := hf i,
rw [h, ←hf j] at hi,
exact hpi hi },
simp_rw [←hsd 0, hf0, zero_smul, zero_vadd, dist_smul_vadd_eq_dist (p 0) c hv0] at hsd,
have hfn0 : ∀ i, i ≠ 0 → f i ≠ 0 := λ i, (hfi.ne_iff' hf0).2,
have hfn0' : ∀ i, i ≠ 0 → f i = (-2) * ⟪v, (p 0 -ᵥ c)⟫ / ⟪v, v⟫,
{ intros i hi,
have hsdi := hsd i,
simpa [hfn0, hi] using hsdi },
have hf12 : f 1 = f 2, { rw [hfn0' 1 dec_trivial, hfn0' 2 dec_trivial] },
exact (dec_trivial : (1 : fin 3) ≠ 2) (hfi hf12)
end
end euclidean_geometry
|
179b1c1ba428bd34b990b26aea9abae5ba1b6248 | 7565ffb53cc64430691ce89265da0f944ee43051 | /hott/types/unit.hlean | 38d8a9ed5ffa15a39fba8c8c98ef5c9b79f12bd8 | [
"Apache-2.0"
] | permissive | EgbertRijke/lean2 | cacddba3d150f8b38688e044960a208bf851f90e | 519dcee739fbca5a4ab77d66db7652097b4604cd | refs/heads/master | 1,606,936,954,854 | 1,498,836,083,000 | 1,498,910,882,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,389 | hlean | /-
Copyright (c) 2015 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris van Doorn
Theorems about the unit type
-/
open is_equiv equiv option eq pointed is_trunc function
namespace unit
protected definition eta : Π(u : unit), ⋆ = u
| eta ⋆ := idp
definition unit_equiv_option_empty [constructor] : unit ≃ option empty :=
begin
fapply equiv.MK,
{ intro u, exact none},
{ intro e, exact star},
{ intro e, cases e, reflexivity, contradiction},
{ intro u, cases u, reflexivity},
end
-- equivalences involving unit and other type constructors are in the file
-- of the other constructor
/- pointed and truncated unit -/
definition punit [constructor] : Set* :=
pSet.mk unit _ ⋆
notation `unit*` := punit
definition unit_arrow_eq {X : Type} (f : unit → X) : (λx, f ⋆) = f :=
by apply eq_of_homotopy; intro u; induction u; reflexivity
open funext
definition unit_arrow_eq_compose {X Y : Type} (g : X → Y) (f : unit → X) :
unit_arrow_eq (g ∘ f) = ap (λf, g ∘ f) (unit_arrow_eq f) :=
begin
apply eq_of_fn_eq_fn' apd10,
refine right_inv apd10 _ ⬝ _,
refine _ ⬝ ap apd10 (!compose_eq_of_homotopy)⁻¹,
refine _ ⬝ (right_inv apd10 _)⁻¹,
apply eq_of_homotopy, intro u, induction u, reflexivity
end
end unit
|
fbfbc34ed86bc7bf3f6197ce88a31e91b39faae9 | 4fa118f6209450d4e8d058790e2967337811b2b5 | /src/sheaves/covering.lean | 3dec6eb697daaa99c750adf4912cf2e452c89427 | [
"Apache-2.0"
] | permissive | leanprover-community/lean-perfectoid-spaces | 16ab697a220ed3669bf76311daa8c466382207f7 | 95a6520ce578b30a80b4c36e36ab2d559a842690 | refs/heads/master | 1,639,557,829,139 | 1,638,797,866,000 | 1,638,797,866,000 | 135,769,296 | 96 | 10 | Apache-2.0 | 1,638,797,866,000 | 1,527,892,754,000 | Lean | UTF-8 | Lean | false | false | 2,316 | lean | /-
Package the definition of an open cover of an open set.
Author: Ramon Fernandez Mir
-/
import topology.basic
import topology.opens
import sheaves.opens
universes u
open topological_space lattice
section covering
variables {α : Type u} [topological_space α]
-- Open cover.
structure covering (U : opens α) :=
{γ : Type u}
(Uis : γ → opens α)
(Hcov : ⋃ Uis = U)
variable (α)
def covering.univ := covering (@opens.univ α _)
variable {α}
-- If ⋃ Ui = U then for all i, Ui ⊆ U.
lemma subset_covering {U : opens α} {OC : covering U} :
∀ i, OC.Uis i ⊆ U :=
λ i x Hx, OC.Hcov ▸ opens_supr_mem OC.Uis i x Hx
-- Make covering from standard definition. Used for instance in compactness.
def opens.from_sets {A : Type*} [topological_space A]
: set (set A) → set (opens A) := λ C, { x | x.1 ∈ C }
lemma opens.from_sets.eq {A : Type*} [topological_space A]
(S : set (set A)) (HS : ∀ (t : set A), t ∈ S → is_open t)
: subtype.val '' (opens.from_sets S) = S :=
set.ext $ λ x, ⟨
λ ⟨x', Hx', Hval⟩, Hval ▸ Hx',
λ Hx, by simp [HS x Hx]; by exact Hx⟩
@[reducible] def covering.from_cover {A : Type*} [topological_space A]
(U : opens A)
(C : set (set A))
(HC : ∀ (t : set A), t ∈ C → is_open t)
(Hcov : U.1 = ⋃₀ C)
: covering U :=
{ γ := opens.from_sets C,
Uis := λ x, x,
Hcov :=
begin
apply subtype.ext.2,
rw Hcov,
apply set.ext,
intros x,
split,
{ intros Hx,
rcases Hx with ⟨U, HU, HxU⟩,
existsi U,
simp at HU,
rcases HU with ⟨OU, HU⟩,
rw ←opens.from_sets.eq C HC,
split,
{ simp [HU],
use OU, },
{ exact HxU, } },
{ intros Hx,
rcases Hx with ⟨U, HU, HxU⟩,
use U,
simp,
use (HC U HU),
{ simp [opens.from_sets],
exact HU, },
{ exact HxU, } }
end, }
lemma covering.from_cover.Uis {A : Type*} [topological_space A]
(U : opens A)
(C : set (set A))
(HC : ∀ (t : set A), t ∈ C → is_open t)
(Hcov : U.1 = ⋃₀ C)
: ∀ i, ((covering.from_cover U C HC Hcov).Uis i).1 ∈ C :=
begin
intros i,
simp [covering.from_cover] at *,
cases i with i Hi,
simp,
simp [opens.from_sets] at *,
exact Hi,
end
end covering
|
fbbd63e0cfe80c6880ba45bca2ef9b1ec631fa04 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/playground/ir.lean | 8514077524471b6a7e0eedc0134349e72ca0997a | [
"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 | 220 | lean | import init.lean.compiler.ir
open Lean.IR
def tst1 : IO Unit :=
let fbody : FnBody :=
FnBody.vdecl `x IRType.uint32 (Expr.lit (LitVal.num 0)) $
FnBody.ret `x;
IO.println $ format fbody
def main : IO Unit :=
tst1
|
db7f8a0f925cfe022193d63074857d4260054e11 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/Lean3Lib/init/meta/backward.lean | f43fe749fe679e378f233a3de7e30f12e8d4bdb7 | [] | 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 | 326 | 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
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.meta.tactic
import Mathlib.Lean3Lib.init.meta.set_get_option_tactics
namespace Mathlib
namespace tactic
|
a520b925d5b62d5a927775356ac8ebd8c9208cda | c5b07d17b3c9fb19e4b302465d237fd1d988c14f | /src/isos/nat.lean | ca1286a09acd14254215432991498185d8f673a8 | [
"MIT"
] | permissive | skaslev/papers | acaec61602b28c33d6115e53913b2002136aa29b | f15b379f3c43bbd0a37ac7bb75f4278f7e901389 | refs/heads/master | 1,665,505,770,318 | 1,660,378,602,000 | 1,660,378,602,000 | 14,101,547 | 0 | 1 | MIT | 1,595,414,522,000 | 1,383,542,702,000 | Lean | UTF-8 | Lean | false | false | 1,015 | lean | import isos.id
namespace nat
-- ℕ = 1 + ℕ
def def_iso : ℕ ≃ 1 ⊕ ℕ :=
⟨λ x, nat.rec (sum.inl ()) (λ y ih, sum.inr y) x,
λ x, sum.rec (λ y, 0) (λ y, y+1) x,
λ x, begin induction x with x ih, repeat { refl } end,
λ x, begin induction x, { induction x, refl }, refl end⟩
-- ℕ = Σ n:ℕ, 1
def ogf_iso : ℕ ≃ ogf (const 1) 1 :=
iso.sigma_one⁻¹ ⋆ iso.sigma_subst (λ n, iso.mul_one_left ⋆ (iso.mul fin.one_iso fseq.one_iso₂)⁻¹)
instance : has_ogf₁ ℕ :=
⟨const 1, ogf_iso⟩
end nat
namespace nats
-- ω^ω = ω ω^ω
def def_iso : ℕ → ℕ ≃ ℕ × (ℕ → ℕ) :=
iso.func_left nat.def_iso ⋆ iso.mul_func₁⁻¹ ⋆ iso.mul_left id.one_iso⁻¹
-- ω^ω = ωⁿ ω^ω
def fseq_iso {n} : ℕ → ℕ ≃ fseq n ℕ × (ℕ → ℕ) :=
begin
induction n with n ih,
{ exact (iso.mul_one_left ⋆ iso.mul_left fseq.one_iso.inv) },
apply (_ ⋆ iso.mul_assoc ⋆ iso.mul_left fseq.cons_iso),
apply (_ ⋆ iso.mul_right ih),
apply def_iso
end
end nats
|
d8fb451647e33811332d6129b436628bd8504ac9 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/playground/lowtech_expander.lean | 9fe4551d5c19a1338bf9c65bee3712882aedd61c | [
"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 | 10,554 | lean | import init.lean.name
open Lean (Name NameMap)
def MacroScope := Nat
abbrev MacroScopes := List MacroScope
structure SourceInfo :=
(leading : Substring)
(pos : Nat)
(trailing : Substring)
def mkUniqIdRef : IO (IO.Ref Nat) :=
IO.mkRef 0
@[init mkUniqIdRef]
constant nextUniqId : IO.Ref Nat := default _
structure SyntaxNodeKind :=
(name : Name) (id : Nat)
instance : Inhabited SyntaxNodeKind :=
⟨{name := default _, id := default _}⟩
instance : BEq SyntaxNodeKind :=
⟨λ k₁ k₂, k₁.id == k₂.id⟩
def mkNameToKindTable : IO (IO.Ref (NameMap Nat)) :=
IO.mkRef {}
@[init mkNameToKindTable]
constant nameToKindTable : IO.Ref (NameMap Nat) := default _
def nextKind (k : Name) : IO SyntaxNodeKind :=
do m ← nameToKindTable.get,
when (m.contains k) (throw $ IO.userError ("Error kind '" ++ toString k ++ "' already exists")),
id ← nextUniqId.get,
nameToKindTable.set (m.insert k id),
nextUniqId.set (id+1),
pure { name := k, id := id }
def mkNullKind : IO SyntaxNodeKind := nextKind `null
@[init mkNullKind] constant nullKind : SyntaxNodeKind := default _
inductive Syntax
| missing
| node (kind : SyntaxNodeKind) (args : Array Syntax) (scopes : MacroScopes)
| atom (info : Option SourceInfo) (val : String)
| ident (info : Option SourceInfo) (rawVal : Substring) (val : Name) (preresolved : List Name) (scopes : MacroScopes)
instance : Inhabited Syntax :=
⟨Syntax.missing⟩
def SyntaxNodeKind.fix : SyntaxNodeKind → IO SyntaxNodeKind
| {name := n, ..} := do
m ← nameToKindTable.get,
match m.find n with
| some id := pure {name := n, id := id}
| none := throw $ IO.userError ("Error unknown Syntax kind '" ++ toString n ++ "'")
partial def Syntax.fixKinds : Syntax → IO Syntax
| (Syntax.node k args scopes) := do
k ← k.fix,
args ← args.mmap Syntax.fixKinds,
pure (Syntax.node k args scopes)
| other := pure other
inductive IsNode : Syntax → Prop
| mk (kind : SyntaxNodeKind) (args : Array Syntax) (scopes : MacroScopes) : IsNode (Syntax.node kind args scopes)
def SyntaxNode : Type := {s : Syntax // IsNode s }
def notIsNodeMissing (h : IsNode Syntax.missing) : False := match h with end
def notIsNodeAtom {info val} (h : IsNode (Syntax.atom info val)) : False := match h with end
def notIsNodeIdent {info rawVal val preresolved scopes} (h : IsNode (Syntax.ident info rawVal val preresolved scopes)) : False := match h with end
def unreachIsNodeMissing {α : Type} (h : IsNode Syntax.missing) : α := False.elim (notIsNodeMissing h)
def unreachIsNodeAtom {α : Type} {info val} (h : IsNode (Syntax.atom info val)) : α := False.elim (notIsNodeAtom h)
def unreachIsNodeIdent {α : Type} {info rawVal val preresolved scopes} (h : IsNode (Syntax.ident info rawVal val preresolved scopes)) : α := False.elim (match h with end)
@[inline] def toSyntaxNode {α : Type} (s : Syntax) (base : α) (fn : SyntaxNode → α) : α :=
match s with
| Syntax.node kind args scopes := fn ⟨Syntax.node kind args scopes, IsNode.mk kind args scopes⟩
| other := base
@[inline] def toSyntaxNodeOf {α : Type} (kind : SyntaxNodeKind) (s : Syntax) (base : α) (fn : SyntaxNode → α) : α :=
match s with
| Syntax.node k args scopes :=
if k == kind then fn ⟨Syntax.node kind args scopes, IsNode.mk kind args scopes⟩
else base
| other := base
@[inline] def mkAtom (val : String) : Syntax :=
Syntax.atom none val
def mkOptionSomeKind : IO SyntaxNodeKind := nextKind `some
@[init mkOptionSomeKind] constant optionSomeKind : SyntaxNodeKind := default _
def mkOptionNoneKind : IO SyntaxNodeKind := nextKind `none
@[init mkOptionSomeKind] constant optionNoneKind : SyntaxNodeKind := default _
def mkManyKind : IO SyntaxNodeKind := nextKind `many
@[init mkManyKind] constant manyKind : SyntaxNodeKind := default _
def mkHoleKind : IO SyntaxNodeKind := nextKind `hole
@[init mkHoleKind] constant holeKind : SyntaxNodeKind := default _
def mkNotKind : IO SyntaxNodeKind := nextKind `not
@[init mkNotKind] constant notKind : SyntaxNodeKind := default _
def mkIfKind : IO SyntaxNodeKind := nextKind `if
@[init mkIfKind] constant ifKind : SyntaxNodeKind := default _
def mkLetKind : IO SyntaxNodeKind := nextKind `let
@[init mkLetKind] constant letKind : SyntaxNodeKind := default _
def mkLetLhsIdKind : IO SyntaxNodeKind := nextKind `letLhsId
@[init mkLetLhsIdKind] constant letLhsIdKind : SyntaxNodeKind := default _
def mkLetLhsPatternKind : IO SyntaxNodeKind := nextKind `letLhsPattern
@[init mkLetLhsPatternKind] constant letLhsPatternKind : SyntaxNodeKind := default _
@[inline] def withArgs {α : Type} (n : SyntaxNode) (fn : Array Syntax → α) : α :=
match n with
| ⟨Syntax.node _ args _, _⟩ := fn args
| ⟨Syntax.missing, h⟩ := unreachIsNodeMissing h
| ⟨Syntax.atom _ _, h⟩ := unreachIsNodeAtom h
| ⟨Syntax.ident _ _ _ _ _, h⟩ := unreachIsNodeIdent h
@[inline] def updateArgs (n : SyntaxNode) (fn : Array Syntax → Array Syntax) : Syntax :=
match n with
| ⟨Syntax.node kind args scopes, _⟩ := Syntax.node kind (fn args) scopes
| ⟨Syntax.missing, h⟩ := unreachIsNodeMissing h
| ⟨Syntax.atom _ _, h⟩ := unreachIsNodeAtom h
| ⟨Syntax.ident _ _ _ _ _, h⟩ := unreachIsNodeIdent h
@[inline] def mkNotAux (tk : Syntax) (c : Syntax) : Syntax :=
Syntax.node notKind [tk, c].toArray []
@[inline] def mkNot (c : Syntax) : Syntax :=
mkNotAux (mkAtom "not") c
@[inline] def withNot {α : Type} (n : SyntaxNode) (fn : Syntax → α) : α :=
withArgs n $ λ args, fn (args.get 1)
@[inline] def updateNot (src : SyntaxNode) (c : Syntax) : Syntax :=
updateArgs src $ λ args, args.set 1 c
@[inline] def mkIfAux (ifTk : Syntax) (condNode : Syntax) (thenTk : Syntax) (thenNode : Syntax) (elseTk : Syntax) (elseNode: Syntax) : Syntax :=
Syntax.node ifKind [ifTk, condNode, thenTk, thenNode, elseTk, elseNode].toArray []
@[inline] def mkIf (c t e : Syntax) : Syntax :=
mkIfAux (mkAtom "if") c (mkAtom "then") t (mkAtom "else") e
@[inline] def withIf {α : Type} (n : SyntaxNode) (fn : Syntax → Syntax → Syntax → α) : α :=
withArgs n $ λ args, fn (args.get 1) (args.get 3) (args.get 5)
@[inline] def updateIf (src : SyntaxNode) (c t e : Syntax) : Syntax :=
updateArgs src $ λ args,
let args := args.set 1 c in
let args := args.set 3 t in
let args := args.set 5 e in
args
@[inline] def mkLetAux (letTk : Syntax) (lhs : Syntax) (assignTk : Syntax) (val : Syntax) (inTk : Syntax) (body : Syntax) : Syntax :=
Syntax.node letKind [letTk, lhs, assignTk, val, inTk, body].toArray []
@[inline] def mkLet (lhs : Syntax) (val : Syntax) (body : Syntax) : Syntax :=
mkLetAux (mkAtom "let") lhs (mkAtom ":=") val (mkAtom "in") body
@[inline] def withLet {α : Type} (n : SyntaxNode) (fn : Syntax → Syntax → Syntax → α) : α :=
withArgs n $ λ args, fn (args.get 1) (args.get 3) (args.get 5)
@[inline] def updateLet (src : SyntaxNode) (lhs val body : Syntax) : Syntax :=
updateArgs src $ λ args,
let args := args.set 1 lhs in
let args := args.set 3 val in
let args := args.set 5 body in
args
@[inline] def mkLetLhsId (id : Syntax) (binders : Syntax) (type : Syntax) : Syntax :=
Syntax.node letLhsIdKind [id, binders, type].toArray []
@[inline] def withLetLhsId {α : Type} (n : SyntaxNode) (fn : Syntax → Syntax → Syntax → α) : α :=
withArgs n $ λ args, fn (args.get 0) (args.get 1) (args.get 2)
@[inline] def updateLhsId (src : SyntaxNode) (id binders type : Syntax) : Syntax :=
updateArgs src $ λ args,
let args := args.set 0 id in
let args := args.set 1 binders in
let args := args.set 2 type in
args
@[inline] def mkLetLhsPattern (pattern : Syntax) : Syntax :=
Syntax.node letLhsPatternKind [pattern].toArray []
@[inline] def withLetLhsPattern {α : Type} (n : SyntaxNode) (fn : Syntax → α) : α :=
withArgs n $ λ args, fn (args.get 0)
@[inline] def withOptionSome {α : Type} (n : SyntaxNode) (fn : Syntax → α) : α :=
withArgs n $ λ args, fn (args.get 0)
def Syntax.getNumChildren (n : Syntax) : Nat :=
match n with
| Syntax.node _ args _ := args.size
| _ := 0
def hole : Syntax := Syntax.node holeKind ∅ []
def mkOptionSome (s : Syntax) := Syntax.node optionSomeKind [s].toArray []
abbrev FrontendConfig := Bool -- placeholder
abbrev Message := String -- placeholder
abbrev TransformM := ReaderT FrontendConfig $ ExceptT Message Id
abbrev Transformer := SyntaxNode → TransformM (Option Syntax)
def noExpansion : TransformM (Option Syntax) := pure none
@[inline] def Syntax.case {α : Type} (n : Syntax) (k : SyntaxNodeKind) (fn : SyntaxNode → TransformM (Option α)) : TransformM (Option α) :=
match n with
| Syntax.node k' args s := if k == k' then fn ⟨Syntax.node k' args s, IsNode.mk _ _ _⟩ else pure none
| _ := pure none
@[inline] def TransformM.orCase {α : Type} (x y : TransformM (Option α)) : TransformM (Option α) :=
λ cfg, match x cfg with
| Except.ok none := y cfg
| other := other
infix `<?>`:2 := TransformM.orCase
set_option pp.implicit true
set_option trace.compiler.boxed true
def flipIf : Transformer :=
λ n, withIf n $ λ c t e,
c.case notKind $ λ c, withNot c $ λ c',
pure $ updateIf n c' e t
def letTransformer : Transformer :=
λ n, withLet n $ λ lhs val body,
(lhs.case letLhsIdKind $ λ lhs, withLetLhsId lhs $ λ id binders type,
if binders.getNumChildren == 0 then
type.case optionNoneKind $ λ _,
let newLhs := updateLhsId lhs id binders (mkOptionSome hole) in
pure (some (updateLet n newLhs val body))
else
-- TODO
noExpansion)
<?>
(lhs.case letLhsPatternKind $ λ lhs,
-- TODO
noExpansion)
@[inline] def Syntax.isNode {α : Type} (n : Syntax) (fn : SyntaxNodeKind → SyntaxNode → TransformM (Option α)) : TransformM (Option α) :=
match n with
| Syntax.node k args s := fn k ⟨Syntax.node k args s, IsNode.mk _ _ _⟩
| other := pure none
/- Version without using the combinator <?>. -/
def letTransformer' : Transformer :=
λ n, withLet n $ λ lhs val body,
lhs.isNode $ λ k lhs, -- lhs is now a SyntaxNode
if k == letLhsIdKind then withLetLhsId lhs $ λ id binders type,
if binders.getNumChildren == 0 then
type.case optionNoneKind $ λ _,
let newLhs := updateLhsId lhs id binders (mkOptionSome hole) in
pure (some (updateLet n newLhs val body))
else
-- TODO
noExpansion
else withLetLhsPattern lhs $ λ pattern,
-- TODO
noExpansion
|
ca65c604f5793b7d99ae251f31399b355f22309c | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/logic/is_empty.lean | a866aa51e2c1e10f097a82469f90b44a480d49c6 | [
"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,850 | lean | /-
Copyright (c) 2021 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris van Doorn
-/
import logic.basic
import tactic.protected
/-!
# Types that are empty
In this file we define a typeclass `is_empty`, which expresses that a type has no elements.
## Main declaration
* `is_empty`: a typeclass that expresses that a type is empty.
-/
variables {α β γ : Sort*}
/-- `is_empty α` expresses that `α` is empty. -/
@[protect_proj]
class is_empty (α : Sort*) : Prop :=
(false : α → false)
instance : is_empty empty := ⟨empty.elim⟩
instance : is_empty pempty := ⟨pempty.elim⟩
instance : is_empty false := ⟨id⟩
instance : is_empty (fin 0) := ⟨λ n, nat.not_lt_zero n.1 n.2⟩
protected lemma function.is_empty [is_empty β] (f : α → β) : is_empty α :=
⟨λ x, is_empty.false (f x)⟩
instance {p : α → Sort*} [h : nonempty α] [∀ x, is_empty (p x)] : is_empty (Π x, p x) :=
h.elim $ λ x, function.is_empty $ function.eval x
instance pprod.is_empty_left [is_empty α] : is_empty (pprod α β) :=
function.is_empty pprod.fst
instance pprod.is_empty_right [is_empty β] : is_empty (pprod α β) :=
function.is_empty pprod.snd
instance prod.is_empty_left {α β} [is_empty α] : is_empty (α × β) :=
function.is_empty prod.fst
instance prod.is_empty_right {α β} [is_empty β] : is_empty (α × β) :=
function.is_empty prod.snd
instance [is_empty α] [is_empty β] : is_empty (psum α β) :=
⟨λ x, psum.rec is_empty.false is_empty.false x⟩
instance {α β} [is_empty α] [is_empty β] : is_empty (α ⊕ β) :=
⟨λ x, sum.rec is_empty.false is_empty.false x⟩
/-- subtypes of an empty type are empty -/
instance [is_empty α] (p : α → Prop) : is_empty (subtype p) :=
⟨λ x, is_empty.false x.1⟩
/-- subtypes by an all-false predicate are false. -/
lemma subtype.is_empty_of_false {p : α → Prop} (hp : ∀ a, ¬(p a)) : is_empty (subtype p) :=
⟨λ x, hp _ x.2⟩
/-- subtypes by false are false. -/
instance subtype.is_empty_false : is_empty {a : α // false} :=
subtype.is_empty_of_false (λ a, id)
/- Test that `pi.is_empty` finds this instance. -/
example [h : nonempty α] [is_empty β] : is_empty (α → β) := by apply_instance
/-- Eliminate out of a type that `is_empty` (without using projection notation). -/
@[elab_as_eliminator]
def is_empty_elim [is_empty α] {p : α → Sort*} (a : α) : p a :=
(is_empty.false a).elim
lemma is_empty_iff : is_empty α ↔ α → false :=
⟨@is_empty.false α, is_empty.mk⟩
namespace is_empty
open function
/-- Eliminate out of a type that `is_empty` (using projection notation). -/
protected def elim (h : is_empty α) {p : α → Sort*} (a : α) : p a :=
is_empty_elim a
/-- Non-dependent version of `is_empty.elim`. Helpful if the elaborator cannot elaborate `h.elim a`
correctly. -/
protected def elim' {β : Sort*} (h : is_empty α) (a : α) : β :=
h.elim a
protected lemma prop_iff {p : Prop} : is_empty p ↔ ¬ p :=
is_empty_iff
variables [is_empty α]
lemma forall_iff {p : α → Prop} : (∀ a, p a) ↔ true :=
iff_true_intro is_empty_elim
lemma exists_iff {p : α → Prop} : (∃ a, p a) ↔ false :=
iff_false_intro $ λ ⟨x, hx⟩, is_empty.false x
@[priority 100] -- see Note [lower instance priority]
instance : subsingleton α := ⟨is_empty_elim⟩
end is_empty
@[simp] lemma not_nonempty_iff : ¬ nonempty α ↔ is_empty α :=
⟨λ h, ⟨λ x, h ⟨x⟩⟩, λ h1 h2, h2.elim h1.elim⟩
@[simp] lemma not_is_empty_iff : ¬ is_empty α ↔ nonempty α :=
not_iff_comm.mp not_nonempty_iff
variables (α)
lemma is_empty_or_nonempty : is_empty α ∨ nonempty α :=
(em $ is_empty α).elim or.inl $ or.inr ∘ not_is_empty_iff.mp
@[simp] lemma not_is_empty_of_nonempty [h : nonempty α] : ¬ is_empty α :=
not_is_empty_iff.mpr h
|
21e95505d5959ef50736da0fc82d25612d240bf6 | 2eab05920d6eeb06665e1a6df77b3157354316ad | /src/group_theory/perm/cycles.lean | d08dc2b260e3d158ba36495c232d40e9672b5caa | [
"Apache-2.0"
] | permissive | ayush1801/mathlib | 78949b9f789f488148142221606bf15c02b960d2 | ce164e28f262acbb3de6281b3b03660a9f744e3c | refs/heads/master | 1,692,886,907,941 | 1,635,270,866,000 | 1,635,270,866,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 58,407 | lean | /-
Copyright (c) 2019 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes
-/
import data.nat.parity
import data.equiv.fintype
import group_theory.perm.sign
import data.finset.noncomm_prod
/-!
# Cyclic permutations
## Main definitions
In the following, `f : equiv.perm β`.
* `equiv.perm.is_cycle`: `f.is_cycle` when two nonfixed points of `β`
are related by repeated application of `f`.
* `equiv.perm.same_cycle`: `f.same_cycle x y` when `x` and `y` are in the same cycle of `f`.
The following two definitions require that `β` is a `fintype`:
* `equiv.perm.cycle_of`: `f.cycle_of x` is the cycle of `f` that `x` belongs to.
* `equiv.perm.cycle_factors`: `f.cycle_factors` is a list of disjoint cyclic permutations that
multiply to `f`.
## Main results
* This file contains several closure results:
- `closure_is_cycle` : The symmetric group is generated by cycles
- `closure_cycle_adjacent_swap` : The symmetric group is generated by
a cycle and an adjacent transposition
- `closure_cycle_coprime_swap` : The symmetric group is generated by
a cycle and a coprime transposition
- `closure_prime_cycle_swap` : The symmetric group is generated by
a prime cycle and a transposition
-/
namespace equiv.perm
open equiv function finset
variables {α : Type*} {β : Type*} [decidable_eq α]
section sign_cycle
/-!
### `is_cycle`
-/
variables [fintype α]
/-- A permutation is a cycle when any two nonfixed points of the permutation are related by repeated
application of the permutation. -/
def is_cycle (f : perm β) : Prop := ∃ x, f x ≠ x ∧ ∀ y, f y ≠ y → ∃ i : ℤ, (f ^ i) x = y
lemma is_cycle.ne_one {f : perm β} (h : is_cycle f) : f ≠ 1 :=
λ hf, by simpa [hf, is_cycle] using h
@[simp] lemma not_is_cycle_one : ¬ (1 : perm β).is_cycle :=
λ H, H.ne_one rfl
lemma is_cycle.two_le_card_support {f : perm α} (h : is_cycle f) :
2 ≤ f.support.card :=
two_le_card_support_of_ne_one h.ne_one
lemma is_cycle_swap {α : Type*} [decidable_eq α] {x y : α} (hxy : x ≠ y) : is_cycle (swap x y) :=
⟨y, by rwa swap_apply_right,
λ a (ha : ite (a = x) y (ite (a = y) x a) ≠ a),
if hya : y = a then ⟨0, hya⟩
else ⟨1, by { rw [gpow_one, swap_apply_def], split_ifs at *; cc }⟩⟩
lemma is_swap.is_cycle {α : Type*} [decidable_eq α] {f : perm α} (hf : is_swap f) : is_cycle f :=
begin
obtain ⟨x, y, hxy, rfl⟩ := hf,
exact is_cycle_swap hxy,
end
lemma is_cycle.inv {f : perm β} (hf : is_cycle f) : is_cycle (f⁻¹) :=
let ⟨x, hx⟩ := hf in
⟨x, by { simp only [inv_eq_iff_eq, *, forall_prop_of_true, ne.def] at *, cc },
λ y hy, let ⟨i, hi⟩ := hx.2 y (by { simp only [inv_eq_iff_eq, *, forall_prop_of_true,
ne.def] at *, cc }) in
⟨-i, by rwa [gpow_neg, inv_gpow, inv_inv]⟩⟩
lemma is_cycle.is_cycle_conj {f g : perm β} (hf : is_cycle f) : is_cycle (g * f * g⁻¹) :=
begin
obtain ⟨a, ha1, ha2⟩ := hf,
refine ⟨g a, by simp [ha1], λ b hb, _⟩,
obtain ⟨i, hi⟩ := ha2 (g⁻¹ b) _,
{ refine ⟨i, _⟩,
rw conj_gpow,
simp [hi] },
{ contrapose! hb,
rw [perm.mul_apply, perm.mul_apply, hb, apply_inv_self] }
end
lemma is_cycle.exists_gpow_eq {f : perm β} (hf : is_cycle f) {x y : β}
(hx : f x ≠ x) (hy : f y ≠ y) : ∃ i : ℤ, (f ^ i) x = y :=
let ⟨g, hg⟩ := hf in
let ⟨a, ha⟩ := hg.2 x hx in
let ⟨b, hb⟩ := hg.2 y hy in
⟨b - a, by rw [← ha, ← mul_apply, ← gpow_add, sub_add_cancel, hb]⟩
lemma is_cycle.exists_pow_eq [fintype β] {f : perm β} (hf : is_cycle f) {x y : β}
(hx : f x ≠ x) (hy : f y ≠ y) : ∃ i : ℕ, (f ^ i) x = y :=
let ⟨n, hn⟩ := hf.exists_gpow_eq hx hy in
by classical; exact ⟨(n % order_of f).to_nat, by {
have := n.mod_nonneg (int.coe_nat_ne_zero.mpr (ne_of_gt (order_of_pos f))),
rwa [← gpow_coe_nat, int.to_nat_of_nonneg this, ← gpow_eq_mod_order_of] }⟩
lemma is_cycle.exists_pow_eq_one [fintype β] {f : perm β} (hf : is_cycle f) :
∃ (k : ℕ) (hk : 1 < k), f ^ k = 1 :=
begin
classical,
have : is_of_fin_order f := exists_pow_eq_one f,
rw is_of_fin_order_iff_pow_eq_one at this,
obtain ⟨x, hx, hx'⟩ := hf,
obtain ⟨_ | _ | k, hk, hk'⟩ := this,
{ exact absurd hk (lt_asymm hk) },
{ rw pow_one at hk',
simpa [hk'] using hx },
{ exact ⟨k + 2, by simp, hk'⟩ }
end
/-- The subgroup generated by a cycle is in bijection with its support -/
noncomputable def is_cycle.gpowers_equiv_support {σ : perm α} (hσ : is_cycle σ) :
(↑(subgroup.gpowers σ) : set (perm α)) ≃ (↑(σ.support) : set α) :=
equiv.of_bijective (λ τ, ⟨τ (classical.some hσ),
begin
obtain ⟨τ, n, rfl⟩ := τ,
rw [finset.mem_coe, coe_fn_coe_base', subtype.coe_mk, gpow_apply_mem_support, mem_support],
exact (classical.some_spec hσ).1,
end⟩)
begin
split,
{ rintros ⟨a, m, rfl⟩ ⟨b, n, rfl⟩ h,
ext y,
by_cases hy : σ y = y,
{ simp_rw [subtype.coe_mk, gpow_apply_eq_self_of_apply_eq_self hy] },
{ obtain ⟨i, rfl⟩ := (classical.some_spec hσ).2 y hy,
rw [subtype.coe_mk, subtype.coe_mk, gpow_apply_comm σ m i, gpow_apply_comm σ n i],
exact congr_arg _ (subtype.ext_iff.mp h) } }, by
{ rintros ⟨y, hy⟩,
rw [finset.mem_coe, mem_support] at hy,
obtain ⟨n, rfl⟩ := (classical.some_spec hσ).2 y hy,
exact ⟨⟨σ ^ n, n, rfl⟩, rfl⟩ },
end
@[simp] lemma is_cycle.gpowers_equiv_support_apply {σ : perm α} (hσ : is_cycle σ) {n : ℕ} :
hσ.gpowers_equiv_support ⟨σ ^ n, n, rfl⟩ = ⟨(σ ^ n) (classical.some hσ),
pow_apply_mem_support.2 (mem_support.2 (classical.some_spec hσ).1)⟩ :=
rfl
@[simp] lemma is_cycle.gpowers_equiv_support_symm_apply {σ : perm α} (hσ : is_cycle σ) (n : ℕ) :
hσ.gpowers_equiv_support.symm ⟨(σ ^ n) (classical.some hσ),
pow_apply_mem_support.2 (mem_support.2 (classical.some_spec hσ).1)⟩ =
⟨σ ^ n, n, rfl⟩ :=
(equiv.symm_apply_eq _).2 hσ.gpowers_equiv_support_apply
lemma order_of_is_cycle {σ : perm α} (hσ : is_cycle σ) : order_of σ = σ.support.card :=
begin
rw [order_eq_card_gpowers, ←fintype.card_coe],
convert fintype.card_congr (is_cycle.gpowers_equiv_support hσ),
end
lemma is_cycle_swap_mul_aux₁ {α : Type*} [decidable_eq α] : ∀ (n : ℕ) {b x : α} {f : perm α}
(hb : (swap x (f x) * f) b ≠ b) (h : (f ^ n) (f x) = b),
∃ i : ℤ, ((swap x (f x) * f) ^ i) (f x) = b
| 0 := λ b x f hb h, ⟨0, h⟩
| (n+1 : ℕ) := λ b x f hb h,
if hfbx : f x = b then ⟨0, hfbx⟩
else
have f b ≠ b ∧ b ≠ x, from ne_and_ne_of_swap_mul_apply_ne_self hb,
have hb' : (swap x (f x) * f) (f⁻¹ b) ≠ f⁻¹ b,
by { rw [mul_apply, apply_inv_self, swap_apply_of_ne_of_ne this.2 (ne.symm hfbx),
ne.def, ← f.injective.eq_iff, apply_inv_self],
exact this.1 },
let ⟨i, hi⟩ := is_cycle_swap_mul_aux₁ n hb'
(f.injective $ by { rw [apply_inv_self], rwa [pow_succ, mul_apply] at h }) in
⟨i + 1, by rw [add_comm, gpow_add, mul_apply, hi, gpow_one, mul_apply, apply_inv_self,
swap_apply_of_ne_of_ne (ne_and_ne_of_swap_mul_apply_ne_self hb).2 (ne.symm hfbx)]⟩
lemma is_cycle_swap_mul_aux₂ {α : Type*} [decidable_eq α] :
∀ (n : ℤ) {b x : α} {f : perm α} (hb : (swap x (f x) * f) b ≠ b) (h : (f ^ n) (f x) = b),
∃ i : ℤ, ((swap x (f x) * f) ^ i) (f x) = b
| (n : ℕ) := λ b x f, is_cycle_swap_mul_aux₁ n
| -[1+ n] := λ b x f hb h,
if hfbx : f⁻¹ x = b then
⟨-1, by rwa [gpow_neg, gpow_one, mul_inv_rev, mul_apply, swap_inv, swap_apply_right]⟩
else if hfbx' : f x = b then ⟨0, hfbx'⟩
else
have f b ≠ b ∧ b ≠ x := ne_and_ne_of_swap_mul_apply_ne_self hb,
have hb : (swap x (f⁻¹ x) * f⁻¹) (f⁻¹ b) ≠ f⁻¹ b,
by { rw [mul_apply, swap_apply_def],
split_ifs;
simp only [inv_eq_iff_eq, perm.mul_apply, gpow_neg_succ_of_nat, ne.def,
perm.apply_inv_self] at *;
cc },
let ⟨i, hi⟩ := is_cycle_swap_mul_aux₁ n hb
(show (f⁻¹ ^ n) (f⁻¹ x) = f⁻¹ b, by
rw [← gpow_coe_nat, ← h, ← mul_apply, ← mul_apply, ← mul_apply, gpow_neg_succ_of_nat,
← inv_pow, pow_succ', mul_assoc, mul_assoc, inv_mul_self, mul_one, gpow_coe_nat,
← pow_succ', ← pow_succ]) in
have h : (swap x (f⁻¹ x) * f⁻¹) (f x) = f⁻¹ x, by rw [mul_apply, inv_apply_self, swap_apply_left],
⟨-i, by rw [← add_sub_cancel i 1, neg_sub, sub_eq_add_neg, gpow_add, gpow_one, gpow_neg,
← inv_gpow, mul_inv_rev, swap_inv, mul_swap_eq_swap_mul, inv_apply_self, swap_comm _ x,
gpow_add, gpow_one, mul_apply, mul_apply (_ ^ i), h, hi, mul_apply, apply_inv_self,
swap_apply_of_ne_of_ne this.2 (ne.symm hfbx')]⟩
lemma is_cycle.eq_swap_of_apply_apply_eq_self {α : Type*} [decidable_eq α]
{f : perm α} (hf : is_cycle f) {x : α}
(hfx : f x ≠ x) (hffx : f (f x) = x) : f = swap x (f x) :=
equiv.ext $ λ y,
let ⟨z, hz⟩ := hf in
let ⟨i, hi⟩ := hz.2 x hfx in
if hyx : y = x then by simp [hyx]
else if hfyx : y = f x then by simp [hfyx, hffx]
else begin
rw [swap_apply_of_ne_of_ne hyx hfyx],
refine by_contradiction (λ hy, _),
cases hz.2 y hy with j hj,
rw [← sub_add_cancel j i, gpow_add, mul_apply, hi] at hj,
cases gpow_apply_eq_of_apply_apply_eq_self hffx (j - i) with hji hji,
{ rw [← hj, hji] at hyx, cc },
{ rw [← hj, hji] at hfyx, cc }
end
lemma is_cycle.swap_mul {α : Type*} [decidable_eq α] {f : perm α} (hf : is_cycle f) {x : α}
(hx : f x ≠ x) (hffx : f (f x) ≠ x) : is_cycle (swap x (f x) * f) :=
⟨f x, by { simp only [swap_apply_def, mul_apply],
split_ifs; simp [f.injective.eq_iff] at *; cc },
λ y hy,
let ⟨i, hi⟩ := hf.exists_gpow_eq hx (ne_and_ne_of_swap_mul_apply_ne_self hy).1 in
have hi : (f ^ (i - 1)) (f x) = y, from
calc (f ^ (i - 1)) (f x) = (f ^ (i - 1) * f ^ (1 : ℤ)) x : by rw [gpow_one, mul_apply]
... = y : by rwa [← gpow_add, sub_add_cancel],
is_cycle_swap_mul_aux₂ (i - 1) hy hi⟩
lemma is_cycle.sign : ∀ {f : perm α} (hf : is_cycle f),
sign f = -(-1) ^ f.support.card
| f := λ hf,
let ⟨x, hx⟩ := hf in
calc sign f = sign (swap x (f x) * (swap x (f x) * f)) :
by rw [← mul_assoc, mul_def, mul_def, swap_swap, trans_refl]
... = -(-1) ^ f.support.card :
if h1 : f (f x) = x
then
have h : swap x (f x) * f = 1,
begin
rw hf.eq_swap_of_apply_apply_eq_self hx.1 h1,
simp only [perm.mul_def, perm.one_def, swap_apply_left, swap_swap]
end,
by { rw [sign_mul, sign_swap hx.1.symm, h, sign_one, hf.eq_swap_of_apply_apply_eq_self hx.1 h1,
card_support_swap hx.1.symm], refl }
else
have h : card (support (swap x (f x) * f)) + 1 = card (support f),
by rw [← insert_erase (mem_support.2 hx.1), support_swap_mul_eq _ _ h1,
card_insert_of_not_mem (not_mem_erase _ _), sdiff_singleton_eq_erase],
have wf : card (support (swap x (f x) * f)) < card (support f),
from card_support_swap_mul hx.1,
by { rw [sign_mul, sign_swap hx.1.symm, (hf.swap_mul hx.1 h1).sign, ← h],
simp only [pow_add, mul_one, units.neg_neg, one_mul, units.mul_neg, eq_self_iff_true,
pow_one, units.neg_mul_neg] }
using_well_founded {rel_tac := λ _ _, `[exact ⟨_, measure_wf (λ f, f.support.card)⟩]}
lemma is_cycle_of_is_cycle_pow {σ : perm α} {n : ℕ}
(h1 : is_cycle (σ ^ n)) (h2 : σ.support ≤ (σ ^ n).support) : is_cycle σ :=
begin
have key : ∀ x : α, (σ ^ n) x ≠ x ↔ σ x ≠ x,
{ simp_rw [←mem_support],
exact finset.ext_iff.mp (le_antisymm (support_pow_le σ n) h2) },
obtain ⟨x, hx1, hx2⟩ := h1,
refine ⟨x, (key x).mp hx1, λ y hy, _⟩,
cases (hx2 y ((key y).mpr hy)) with i _,
exact ⟨n * i, by rwa gpow_mul⟩
end
-- The lemma `support_gpow_le` is relevant. It means that `h2` is equivalent to
-- `σ.support = (σ ^ n).support`, as well as to `σ.support.card ≤ (σ ^ n).support.card`.
lemma is_cycle_of_is_cycle_gpow {σ : perm α} {n : ℤ}
(h1 : is_cycle (σ ^ n)) (h2 : σ.support ≤ (σ ^ n).support) : is_cycle σ :=
begin
cases n,
{ exact is_cycle_of_is_cycle_pow h1 h2 },
{ simp only [le_eq_subset, gpow_neg_succ_of_nat, perm.support_inv] at h1 h2,
simpa using is_cycle_of_is_cycle_pow h1.inv h2 }
end
lemma is_cycle.extend_domain {α : Type*} {p : β → Prop} [decidable_pred p]
(f : α ≃ subtype p) {g : perm α} (h : is_cycle g) :
is_cycle (g.extend_domain f) :=
begin
obtain ⟨a, ha, ha'⟩ := h,
refine ⟨f a, _, λ b hb, _⟩,
{ rw extend_domain_apply_image,
exact λ con, ha (f.injective (subtype.coe_injective con)) },
by_cases pb : p b,
{ obtain ⟨i, hi⟩ := ha' (f.symm ⟨b, pb⟩) (λ con, hb _),
{ refine ⟨i, _⟩,
have hnat : ∀ (k : ℕ) (a : α), (g.extend_domain f ^ k) ↑(f a) = f ((g ^ k) a),
{ intros k a,
induction k with k ih, { refl },
rw [pow_succ, perm.mul_apply, ih, extend_domain_apply_image, pow_succ, perm.mul_apply] },
have hint : ∀ (k : ℤ) (a : α), (g.extend_domain f ^ k) ↑(f a) = f ((g ^ k) a),
{ intros k a,
induction k with k k,
{ rw [gpow_of_nat, gpow_of_nat, hnat] },
rw [gpow_neg_succ_of_nat, gpow_neg_succ_of_nat, inv_eq_iff_eq, hnat, apply_inv_self] },
rw [hint, hi, apply_symm_apply, subtype.coe_mk] },
{ rw [extend_domain_apply_subtype _ _ pb, con, apply_symm_apply, subtype.coe_mk] } },
{ exact (hb (extend_domain_apply_not_subtype _ _ pb)).elim }
end
lemma nodup_of_pairwise_disjoint_cycles {l : list (perm β)} (h1 : ∀ f ∈ l, is_cycle f)
(h2 : l.pairwise disjoint) : l.nodup :=
nodup_of_pairwise_disjoint (λ h, (h1 1 h).ne_one rfl) h2
end sign_cycle
/-!
### `same_cycle`
-/
/-- The equivalence relation indicating that two points are in the same cycle of a permutation. -/
def same_cycle (f : perm β) (x y : β) : Prop := ∃ i : ℤ, (f ^ i) x = y
@[refl] lemma same_cycle.refl (f : perm β) (x : β) : same_cycle f x x := ⟨0, rfl⟩
@[symm] lemma same_cycle.symm {f : perm β} {x y : β} : same_cycle f x y → same_cycle f y x :=
λ ⟨i, hi⟩, ⟨-i, by rw [gpow_neg, ← hi, inv_apply_self]⟩
@[trans] lemma same_cycle.trans {f : perm β} {x y z : β} :
same_cycle f x y → same_cycle f y z → same_cycle f x z :=
λ ⟨i, hi⟩ ⟨j, hj⟩, ⟨j + i, by rw [gpow_add, mul_apply, hi, hj]⟩
lemma same_cycle.apply_eq_self_iff {f : perm β} {x y : β} :
same_cycle f x y → (f x = x ↔ f y = y) :=
λ ⟨i, hi⟩, by rw [← hi, ← mul_apply, ← gpow_one_add, add_comm, gpow_add_one, mul_apply,
(f ^ i).injective.eq_iff]
lemma is_cycle.same_cycle {f : perm β} (hf : is_cycle f) {x y : β}
(hx : f x ≠ x) (hy : f y ≠ y) : same_cycle f x y :=
hf.exists_gpow_eq hx hy
lemma same_cycle.nat' [fintype β] {f : perm β} {x y : β} (h : same_cycle f x y) :
∃ (i : ℕ) (h : i < order_of f), (f ^ i) x = y :=
begin
classical,
obtain ⟨k, rfl⟩ := id h,
by_cases hk : (k % order_of f) = 0,
{ use 0,
rw ←int.dvd_iff_mod_eq_zero at hk,
obtain ⟨m, rfl⟩ := hk,
simp [pow_order_of_eq_one, order_of_pos, gpow_mul] },
{ use ((k % order_of f).nat_abs),
rw [←gpow_coe_nat, int.nat_abs_of_nonneg, ←gpow_eq_mod_order_of],
{ refine ⟨_, rfl⟩,
rw [←int.coe_nat_lt, int.nat_abs_of_nonneg],
{ refine (int.mod_lt_of_pos _ _),
simpa using order_of_pos _ },
{ refine int.mod_nonneg _ _,
simpa using ne_of_gt (order_of_pos _) } },
{ refine int.mod_nonneg _ _,
simpa using (order_of_pos _).ne' } }
end
lemma same_cycle.nat'' [fintype β] {f : perm β} {x y : β} (h : same_cycle f x y) :
∃ (i : ℕ) (hpos : 0 < i) (h : i ≤ order_of f), (f ^ i) x = y :=
begin
classical,
obtain ⟨_|i, hi, rfl⟩ := h.nat',
{ refine ⟨order_of f, order_of_pos f, le_rfl, _⟩,
rw [pow_order_of_eq_one, pow_zero] },
{ exact ⟨i.succ, i.zero_lt_succ, hi.le, rfl⟩ }
end
instance [fintype α] (f : perm α) : decidable_rel (same_cycle f) :=
λ x y, decidable_of_iff (∃ n ∈ list.range (fintype.card (perm α)), (f ^ n) x = y)
⟨λ ⟨n, _, hn⟩, ⟨n, hn⟩, λ ⟨i, hi⟩, ⟨(i % order_of f).nat_abs, list.mem_range.2
(int.coe_nat_lt.1 $
by { rw int.nat_abs_of_nonneg (int.mod_nonneg _
(int.coe_nat_ne_zero_iff_pos.2 (order_of_pos _))),
{ apply lt_of_lt_of_le (int.mod_lt _ (int.coe_nat_ne_zero_iff_pos.2 (order_of_pos _))),
{ simp [order_of_le_card_univ] },
exact fintype_perm },
exact fintype_perm, }),
by { rw [← gpow_coe_nat, int.nat_abs_of_nonneg (int.mod_nonneg _
(int.coe_nat_ne_zero_iff_pos.2 (order_of_pos _))), ← gpow_eq_mod_order_of, hi],
exact fintype_perm }⟩⟩
lemma same_cycle_apply {f : perm β} {x y : β} : same_cycle f x (f y) ↔ same_cycle f x y :=
⟨λ ⟨i, hi⟩, ⟨-1 + i, by rw [gpow_add, mul_apply, hi, gpow_neg_one, inv_apply_self]⟩,
λ ⟨i, hi⟩, ⟨1 + i, by rw [gpow_add, mul_apply, hi, gpow_one]⟩⟩
lemma same_cycle_cycle {f : perm β} {x : β} (hx : f x ≠ x) : is_cycle f ↔
(∀ {y}, same_cycle f x y ↔ f y ≠ y) :=
⟨λ hf y, ⟨λ ⟨i, hi⟩ hy, hx $
by { rw [← gpow_apply_eq_self_of_apply_eq_self hy i, (f ^ i).injective.eq_iff] at hi,
rw [hi, hy] },
hf.exists_gpow_eq hx⟩,
λ h, ⟨x, hx, λ y hy, h.2 hy⟩⟩
lemma same_cycle_inv (f : perm β) {x y : β} : same_cycle f⁻¹ x y ↔ same_cycle f x y :=
⟨λ ⟨i, hi⟩, ⟨-i, by rw [gpow_neg, ← inv_gpow, hi]⟩,
λ ⟨i, hi⟩, ⟨-i, by rw [gpow_neg, ← inv_gpow, inv_inv, hi]⟩ ⟩
lemma same_cycle_inv_apply {f : perm β} {x y : β} : same_cycle f x (f⁻¹ y) ↔ same_cycle f x y :=
by rw [← same_cycle_inv, same_cycle_apply, same_cycle_inv]
@[simp] lemma same_cycle_pow_left_iff {f : perm β} {x y : β} {n : ℕ} :
same_cycle f ((f ^ n) x) y ↔ same_cycle f x y :=
begin
split,
{ rintro ⟨k, rfl⟩,
use (k + n),
simp [gpow_add] },
{ rintro ⟨k, rfl⟩,
use (k - n),
rw [←gpow_coe_nat, ←mul_apply, ←gpow_add, int.sub_add_cancel] }
end
@[simp] lemma same_cycle_gpow_left_iff {f : perm β} {x y : β} {n : ℤ} :
same_cycle f ((f ^ n) x) y ↔ same_cycle f x y :=
begin
cases n,
{ exact same_cycle_pow_left_iff },
{ rw [gpow_neg_succ_of_nat, ←inv_pow, ←same_cycle_inv, same_cycle_pow_left_iff, same_cycle_inv] }
end
/-- Unlike `support_congr`, which assumes that `∀ (x ∈ g.support), f x = g x)`, here
we have the weaker assumption that `∀ (x ∈ f.support), f x = g x`. -/
lemma is_cycle.support_congr [fintype α] {f g : perm α} (hf : is_cycle f) (hg : is_cycle g)
(h : f.support ⊆ g.support) (h' : ∀ (x ∈ f.support), f x = g x) : f = g :=
begin
have : f.support = g.support,
{ refine le_antisymm h _,
intros z hz,
obtain ⟨x, hx, hf'⟩ := id hf,
have hx' : g x ≠ x,
{ rwa [←h' x (mem_support.mpr hx)] },
obtain ⟨m, hm⟩ := hg.exists_pow_eq hx' (mem_support.mp hz),
have h'' : ∀ (x ∈ f.support ∩ g.support), f x = g x,
{ intros x hx,
exact h' x (mem_of_mem_inter_left hx) },
rwa [←hm, ←pow_eq_on_of_mem_support h'' _ x (mem_inter_of_mem (mem_support.mpr hx)
(mem_support.mpr hx')), pow_apply_mem_support, mem_support] },
refine support_congr h _,
simpa [←this] using h'
end
/-- If two cyclic permutations agree on all terms in their intersection,
and that intersection is not empty, then the two cyclic permutations must be equal. -/
lemma is_cycle.eq_on_support_inter_nonempty_congr [fintype α] {f g : perm α}
(hf : is_cycle f) (hg : is_cycle g) (h : ∀ (x ∈ f.support ∩ g.support), f x = g x) {x : α}
(hx : f x = g x) (hx' : x ∈ f.support) : f = g :=
begin
have hx'' : x ∈ g.support,
{ rwa [mem_support, ←hx, ←mem_support] },
have : f.support ⊆ g.support,
{ intros y hy,
obtain ⟨k, rfl⟩ := hf.exists_pow_eq (mem_support.mp hx') (mem_support.mp hy),
rwa [pow_eq_on_of_mem_support h _ _ (mem_inter_of_mem hx' hx''), pow_apply_mem_support] },
rw (inter_eq_left_iff_subset _ _).mpr this at h,
exact hf.support_congr hg this h
end
lemma is_cycle.support_pow_eq_iff [fintype α] {f : perm α} (hf : is_cycle f) {n : ℕ} :
support (f ^ n) = support f ↔ ¬ order_of f ∣ n :=
begin
rw order_of_dvd_iff_pow_eq_one,
split,
{ intros h H,
refine hf.ne_one _,
rw [←support_eq_empty_iff, ←h, H, support_one] },
{ intro H,
apply le_antisymm (support_pow_le _ n) _,
intros x hx,
contrapose! H,
ext z,
by_cases hz : f z = z,
{ rw [pow_apply_eq_self_of_apply_eq_self hz, one_apply] },
{ obtain ⟨k, rfl⟩ := hf.exists_pow_eq hz (mem_support.mp hx),
apply (f ^ k).injective,
rw [←mul_apply, (commute.pow_pow_self _ _ _).eq, mul_apply],
simpa using H } }
end
lemma is_cycle.pow_iff [fintype β] {f : perm β} (hf : is_cycle f) {n : ℕ} :
is_cycle (f ^ n) ↔ n.coprime (order_of f) :=
begin
classical,
split,
{ intro h,
have hr : support (f ^ n) = support f,
{ rw hf.support_pow_eq_iff,
rintro ⟨k, rfl⟩,
refine h.ne_one _,
simp [pow_mul, pow_order_of_eq_one] },
have : order_of (f ^ n) = order_of f,
{ rw [order_of_is_cycle h, hr, order_of_is_cycle hf] },
rw [order_of_pow, nat.div_eq_self] at this,
cases this,
{ exact absurd this (order_of_pos _).ne' },
{ rwa [nat.coprime_iff_gcd_eq_one, nat.gcd_comm] } },
{ intro h,
obtain ⟨m, hm⟩ := exists_pow_eq_self_of_coprime h,
have hf' : is_cycle ((f ^ n) ^ m) := by rwa hm,
refine is_cycle_of_is_cycle_pow hf' _,
intros x hx,
rw [hm],
exact support_pow_le _ n hx }
end
lemma is_cycle.pow_eq_one_iff [fintype α] {f : perm α} (hf : is_cycle f) {n : ℕ} :
f ^ n = 1 ↔ ∃ (x ∈ f.support), (f ^ n) x = x :=
begin
split,
{ intro h,
obtain ⟨x, hx, -⟩ := id hf,
exact ⟨x, mem_support.mpr hx, by simp [h]⟩ },
{ rintro ⟨x, hx, hx'⟩,
by_cases h : support (f ^ n) = support f,
{ rw [←h, mem_support] at hx,
contradiction },
{ rw [hf.support_pow_eq_iff, not_not] at h,
obtain ⟨k, rfl⟩ := h,
rw [pow_mul, pow_order_of_eq_one, one_pow] } }
end
lemma is_cycle.mem_support_pos_pow_iff_of_lt_order_of [fintype α] {f : perm α} (hf : is_cycle f)
{n : ℕ} (npos : 0 < n) (hn : n < order_of f) {x : α} :
x ∈ (f ^ n).support ↔ x ∈ f.support :=
begin
have : ¬ order_of f ∣ n := nat.not_dvd_of_pos_of_lt npos hn,
rw ←hf.support_pow_eq_iff at this,
rw this
end
lemma is_cycle.is_cycle_pow_pos_of_lt_prime_order [fintype β] {f : perm β} (hf : is_cycle f)
(hf' : (order_of f).prime) (n : ℕ) (hn : 0 < n) (hn' : n < order_of f) : is_cycle (f ^ n) :=
begin
classical,
have : n.coprime (order_of f),
{ refine nat.coprime.symm _,
rw nat.prime.coprime_iff_not_dvd hf',
exact nat.not_dvd_of_pos_of_lt hn hn' },
obtain ⟨m, hm⟩ := exists_pow_eq_self_of_coprime this,
have hf'' := hf,
rw ←hm at hf'',
refine is_cycle_of_is_cycle_pow hf'' _,
rw [hm],
exact support_pow_le f n
end
/-!
### `cycle_of`
-/
/-- `f.cycle_of x` is the cycle of the permutation `f` to which `x` belongs. -/
def cycle_of [fintype α] (f : perm α) (x : α) : perm α :=
of_subtype (@subtype_perm _ f (same_cycle f x) (λ _, same_cycle_apply.symm))
lemma cycle_of_apply [fintype α] (f : perm α) (x y : α) :
cycle_of f x y = if same_cycle f x y then f y else y := rfl
lemma cycle_of_inv [fintype α] (f : perm α) (x : α) :
(cycle_of f x)⁻¹ = cycle_of f⁻¹ x :=
equiv.ext $ λ y, begin
rw [inv_eq_iff_eq, cycle_of_apply, cycle_of_apply],
split_ifs; simp [*, same_cycle_inv, same_cycle_inv_apply] at *
end
@[simp] lemma cycle_of_pow_apply_self [fintype α] (f : perm α) (x : α) :
∀ n : ℕ, (cycle_of f x ^ n) x = (f ^ n) x
| 0 := rfl
| (n+1) := by { rw [pow_succ, mul_apply, cycle_of_apply,
cycle_of_pow_apply_self, if_pos, pow_succ, mul_apply],
exact ⟨n, rfl⟩ }
@[simp] lemma cycle_of_gpow_apply_self [fintype α] (f : perm α) (x : α) :
∀ n : ℤ, (cycle_of f x ^ n) x = (f ^ n) x
| (n : ℕ) := cycle_of_pow_apply_self f x n
| -[1+ n] := by rw [gpow_neg_succ_of_nat, ← inv_pow, cycle_of_inv,
gpow_neg_succ_of_nat, ← inv_pow, cycle_of_pow_apply_self]
lemma same_cycle.cycle_of_apply [fintype α] {f : perm α} {x y : α} (h : same_cycle f x y) :
cycle_of f x y = f y := dif_pos h
lemma cycle_of_apply_of_not_same_cycle [fintype α] {f : perm α} {x y : α} (h : ¬same_cycle f x y) :
cycle_of f x y = y := dif_neg h
lemma same_cycle.cycle_of_eq [fintype α] {f : perm α} {x y : α} (h : same_cycle f x y) :
cycle_of f x = cycle_of f y :=
begin
ext z,
rw cycle_of_apply,
split_ifs with hz hz,
{ exact (h.symm.trans hz).cycle_of_apply.symm },
{ exact (cycle_of_apply_of_not_same_cycle (mt h.trans hz)).symm }
end
@[simp] lemma cycle_of_apply_apply_gpow_self [fintype α] (f : perm α) (x : α) (k : ℤ) :
cycle_of f x ((f ^ k) x) = (f ^ (k + 1)) x :=
begin
rw same_cycle.cycle_of_apply,
{ rw [add_comm, gpow_add, gpow_one, mul_apply] },
{ exact ⟨k, rfl⟩ }
end
@[simp] lemma cycle_of_apply_apply_pow_self [fintype α] (f : perm α) (x : α) (k : ℕ) :
cycle_of f x ((f ^ k) x) = (f ^ (k + 1)) x :=
by convert cycle_of_apply_apply_gpow_self f x k using 1
@[simp] lemma cycle_of_apply_apply_self [fintype α] (f : perm α) (x : α) :
cycle_of f x (f x) = f (f x) :=
by convert cycle_of_apply_apply_pow_self f x 1 using 1
@[simp] lemma cycle_of_apply_self [fintype α] (f : perm α) (x : α) :
cycle_of f x x = f x := (same_cycle.refl _ _).cycle_of_apply
lemma is_cycle.cycle_of_eq [fintype α] {f : perm α} (hf : is_cycle f) {x : α} (hx : f x ≠ x) :
cycle_of f x = f :=
equiv.ext $ λ y,
if h : same_cycle f x y then by rw [h.cycle_of_apply]
else by rw [cycle_of_apply_of_not_same_cycle h, not_not.1 (mt ((same_cycle_cycle hx).1 hf).2 h)]
@[simp] lemma cycle_of_eq_one_iff [fintype α] (f : perm α) {x : α} : cycle_of f x = 1 ↔ f x = x :=
begin
simp_rw [ext_iff, cycle_of_apply, one_apply],
refine ⟨λ h, (if_pos (same_cycle.refl f x)).symm.trans (h x), λ h y, _⟩,
by_cases hy : f y = y,
{ rw [hy, if_t_t] },
{ exact if_neg (mt same_cycle.apply_eq_self_iff (by tauto)) },
end
@[simp] lemma cycle_of_self_apply [fintype α] (f : perm α) (x : α) :
cycle_of f (f x) = cycle_of f x :=
(same_cycle_apply.mpr (same_cycle.refl _ _)).symm.cycle_of_eq
@[simp] lemma cycle_of_self_apply_pow [fintype α] (f : perm α) (n : ℕ) (x : α) :
cycle_of f ((f ^ n) x) = cycle_of f x :=
(same_cycle_pow_left_iff.mpr (same_cycle.refl _ _)).cycle_of_eq
@[simp] lemma cycle_of_self_apply_gpow [fintype α] (f : perm α) (n : ℤ) (x : α) :
cycle_of f ((f ^ n) x) = cycle_of f x :=
(same_cycle_gpow_left_iff.mpr (same_cycle.refl _ _)).cycle_of_eq
lemma is_cycle.cycle_of [fintype α] {f : perm α} (hf : is_cycle f) {x : α} :
cycle_of f x = if f x = x then 1 else f :=
begin
by_cases hx : f x = x,
{ rwa [if_pos hx, cycle_of_eq_one_iff] },
{ rwa [if_neg hx, hf.cycle_of_eq] },
end
lemma cycle_of_one [fintype α] (x : α) : cycle_of 1 x = 1 :=
(cycle_of_eq_one_iff 1).mpr rfl
lemma is_cycle_cycle_of [fintype α] (f : perm α) {x : α} (hx : f x ≠ x) : is_cycle (cycle_of f x) :=
have cycle_of f x x ≠ x, by rwa [(same_cycle.refl _ _).cycle_of_apply],
(same_cycle_cycle this).2 $ λ y,
⟨λ h, mt h.apply_eq_self_iff.2 this,
λ h, if hxy : same_cycle f x y then
let ⟨i, hi⟩ := hxy in
⟨i, by rw [cycle_of_gpow_apply_self, hi]⟩
else by { rw [cycle_of_apply_of_not_same_cycle hxy] at h, exact (h rfl).elim }⟩
@[simp] lemma two_le_card_support_cycle_of_iff [fintype α] {f : perm α} {x : α} :
2 ≤ card (cycle_of f x).support ↔ f x ≠ x :=
begin
refine ⟨λ h, _, λ h, by simpa using (is_cycle_cycle_of _ h).two_le_card_support⟩,
contrapose! h,
rw ←cycle_of_eq_one_iff at h,
simp [h]
end
@[simp] lemma card_support_cycle_of_pos_iff [fintype α] {f : perm α} {x : α} :
0 < card (cycle_of f x).support ↔ f x ≠ x :=
begin
rw [←two_le_card_support_cycle_of_iff, ←nat.succ_le_iff],
exact ⟨λ h, or.resolve_left h.eq_or_lt (card_support_ne_one _).symm, zero_lt_two.trans_le⟩
end
lemma pow_apply_eq_pow_mod_order_of_cycle_of_apply [fintype α] (f : perm α) (n : ℕ) (x : α) :
(f ^ n) x = (f ^ (n % order_of (cycle_of f x))) x :=
by rw [←cycle_of_pow_apply_self f, ←cycle_of_pow_apply_self f, pow_eq_mod_order_of]
lemma cycle_of_mul_of_apply_right_eq_self [fintype α] {f g : perm α}
(h : _root_.commute f g) (x : α) (hx : g x = x) : (f * g).cycle_of x = f.cycle_of x :=
begin
ext y,
by_cases hxy : (f * g).same_cycle x y,
{ obtain ⟨z, rfl⟩ := hxy,
rw cycle_of_apply_apply_gpow_self,
simp [h.mul_gpow, gpow_apply_eq_self_of_apply_eq_self hx] },
{ rw [cycle_of_apply_of_not_same_cycle hxy, cycle_of_apply_of_not_same_cycle],
contrapose! hxy,
obtain ⟨z, rfl⟩ := hxy,
refine ⟨z, _⟩,
simp [h.mul_gpow, gpow_apply_eq_self_of_apply_eq_self hx] }
end
lemma disjoint.cycle_of_mul_distrib [fintype α] {f g : perm α} (h : f.disjoint g) (x : α) :
(f * g).cycle_of x = (f.cycle_of x * g.cycle_of x) :=
begin
cases (disjoint_iff_eq_or_eq.mp h) x with hfx hgx,
{ simp [h.commute.eq, cycle_of_mul_of_apply_right_eq_self h.symm.commute, hfx] },
{ simp [cycle_of_mul_of_apply_right_eq_self h.commute, hgx] }
end
lemma support_cycle_of_eq_nil_iff [fintype α] {f : perm α} {x : α} :
(f.cycle_of x).support = ∅ ↔ x ∉ f.support :=
by simp
lemma support_cycle_of_le [fintype α] (f : perm α) (x : α) :
support (f.cycle_of x) ≤ support f :=
begin
intros y hy,
rw [mem_support, cycle_of_apply] at hy,
split_ifs at hy,
{ exact mem_support.mpr hy },
{ exact absurd rfl hy }
end
lemma mem_support_cycle_of_iff [fintype α] {f : perm α} {x y : α} :
y ∈ support (f.cycle_of x) ↔ same_cycle f x y ∧ x ∈ support f :=
begin
by_cases hx : f x = x,
{ rw (cycle_of_eq_one_iff _).mpr hx,
simp [hx] },
{ rw [mem_support, cycle_of_apply],
split_ifs with hy,
{ simp only [hx, hy, iff_true, ne.def, not_false_iff, and_self, mem_support],
rcases hy with ⟨k, rfl⟩,
rw ←not_mem_support,
simpa using hx },
{ simpa [hx] using hy } }
end
lemma same_cycle.mem_support_iff [fintype α] {f : perm α} {x y : α} (h : same_cycle f x y) :
x ∈ support f ↔ y ∈ support f :=
⟨λ hx, support_cycle_of_le f x (mem_support_cycle_of_iff.mpr ⟨h, hx⟩),
λ hy, support_cycle_of_le f y (mem_support_cycle_of_iff.mpr ⟨h.symm, hy⟩)⟩
lemma pow_mod_card_support_cycle_of_self_apply [fintype α] (f : perm α) (n : ℕ) (x : α) :
(f ^ (n % (f.cycle_of x).support.card)) x = (f ^ n) x :=
begin
by_cases hx : f x = x,
{ rw [pow_apply_eq_self_of_apply_eq_self hx, pow_apply_eq_self_of_apply_eq_self hx] },
{ rw [←cycle_of_pow_apply_self, ←cycle_of_pow_apply_self f,
←order_of_is_cycle (is_cycle_cycle_of f hx), ←pow_eq_mod_order_of] }
end
/-!
### `cycle_factors`
-/
/-- Given a list `l : list α` and a permutation `f : perm α` whose nonfixed points are all in `l`,
recursively factors `f` into cycles. -/
def cycle_factors_aux [fintype α] : Π (l : list α) (f : perm α),
(∀ {x}, f x ≠ x → x ∈ l) →
{l : list (perm α) // l.prod = f ∧ (∀ g ∈ l, is_cycle g) ∧ l.pairwise disjoint}
| [] f h := ⟨[], by { simp only [imp_false, list.pairwise.nil, list.not_mem_nil, forall_const,
and_true, forall_prop_of_false, not_not, not_false_iff, list.prod_nil] at *,
ext, simp * }⟩
| (x::l) f h :=
if hx : f x = x then
cycle_factors_aux l f (λ y hy, list.mem_of_ne_of_mem (λ h, hy (by rwa h)) (h hy))
else let ⟨m, hm₁, hm₂, hm₃⟩ := cycle_factors_aux l ((cycle_of f x)⁻¹ * f)
(λ y hy, list.mem_of_ne_of_mem
(λ h : y = x,
by { rw [h, mul_apply, ne.def, inv_eq_iff_eq, cycle_of_apply_self] at hy, exact hy rfl })
(h (λ h : f y = y, by { rw [mul_apply, h, ne.def, inv_eq_iff_eq, cycle_of_apply] at hy,
split_ifs at hy; cc }))) in
⟨(cycle_of f x) :: m, by { rw [list.prod_cons, hm₁], simp },
λ g hg, ((list.mem_cons_iff _ _ _).1 hg).elim (λ hg, hg.symm ▸ is_cycle_cycle_of _ hx)
(hm₂ g),
list.pairwise_cons.2 ⟨λ g hg y,
or_iff_not_imp_left.2 (λ hfy,
have hxy : same_cycle f x y := not_not.1 (mt cycle_of_apply_of_not_same_cycle hfy),
have hgm : g :: m.erase g ~ m := list.cons_perm_iff_perm_erase.2 ⟨hg, list.perm.refl _⟩,
have ∀ h ∈ m.erase g, disjoint g h, from
(list.pairwise_cons.1 ((hgm.pairwise_iff (λ a b (h : disjoint a b), h.symm)).2 hm₃)).1,
classical.by_cases id $ λ hgy : g y ≠ y,
(disjoint_prod_right _ this y).resolve_right $
have hsc : same_cycle f⁻¹ x (f y), by rwa [same_cycle_inv, same_cycle_apply],
by { rw [disjoint_prod_perm hm₃ hgm.symm, list.prod_cons,
← eq_inv_mul_iff_mul_eq] at hm₁,
rwa [hm₁, mul_apply, mul_apply, cycle_of_inv, hsc.cycle_of_apply,
inv_apply_self, inv_eq_iff_eq, eq_comm] }),
hm₃⟩⟩
lemma mem_list_cycles_iff {α : Type*} [fintype α] {l : list (perm α)}
(h1 : ∀ σ : perm α, σ ∈ l → σ.is_cycle)
(h2 : l.pairwise disjoint) {σ : perm α} :
σ ∈ l ↔ σ.is_cycle ∧ ∀ (a : α) (h4 : σ a ≠ a), σ a = l.prod a :=
begin
suffices : σ.is_cycle → (σ ∈ l ↔ ∀ (a : α) (h4 : σ a ≠ a), σ a = l.prod a),
{ exact ⟨λ hσ, ⟨h1 σ hσ, (this (h1 σ hσ)).mp hσ⟩, λ hσ, (this hσ.1).mpr hσ.2⟩ },
intro h3,
classical,
split,
{ intros h a ha,
exact eq_on_support_mem_disjoint h h2 _ (mem_support.mpr ha) },
{ intros h,
have hσl : σ.support ⊆ l.prod.support,
{ intros x hx,
rw mem_support at hx,
rwa [mem_support, ←h _ hx] },
obtain ⟨a, ha, -⟩ := id h3,
rw ←mem_support at ha,
obtain ⟨τ, hτ, hτa⟩ := exists_mem_support_of_mem_support_prod (hσl ha),
have hτl : ∀ (x ∈ τ.support), τ x = l.prod x := eq_on_support_mem_disjoint hτ h2,
have key : ∀ (x ∈ σ.support ∩ τ.support), σ x = τ x,
{ intros x hx,
rw [h x (mem_support.mp (mem_of_mem_inter_left hx)), hτl x (mem_of_mem_inter_right hx)] },
convert hτ,
refine h3.eq_on_support_inter_nonempty_congr (h1 _ hτ) key _ ha,
exact key a (mem_inter_of_mem ha hτa) }
end
lemma list_cycles_perm_list_cycles {α : Type*} [fintype α] {l₁ l₂ : list (perm α)}
(h₀ : l₁.prod = l₂.prod)
(h₁l₁ : ∀ σ : perm α, σ ∈ l₁ → σ.is_cycle) (h₁l₂ : ∀ σ : perm α, σ ∈ l₂ → σ.is_cycle)
(h₂l₁ : l₁.pairwise disjoint) (h₂l₂ : l₂.pairwise disjoint) :
l₁ ~ l₂ :=
begin
classical,
refine (list.perm_ext (nodup_of_pairwise_disjoint_cycles h₁l₁ h₂l₁)
(nodup_of_pairwise_disjoint_cycles h₁l₂ h₂l₂)).mpr (λ σ, _),
by_cases hσ : σ.is_cycle,
{ obtain ⟨a, ha⟩ := not_forall.mp (mt ext hσ.ne_one),
rw [mem_list_cycles_iff h₁l₁ h₂l₁, mem_list_cycles_iff h₁l₂ h₂l₂, h₀] },
{ exact iff_of_false (mt (h₁l₁ σ) hσ) (mt (h₁l₂ σ) hσ) }
end
/-- Factors a permutation `f` into a list of disjoint cyclic permutations that multiply to `f`. -/
def cycle_factors [fintype α] [linear_order α] (f : perm α) :
{l : list (perm α) // l.prod = f ∧ (∀ g ∈ l, is_cycle g) ∧ l.pairwise disjoint} :=
cycle_factors_aux (univ.sort (≤)) f (λ _ _, (mem_sort _).2 (mem_univ _))
/-- Factors a permutation `f` into a list of disjoint cyclic permutations that multiply to `f`,
without a linear order. -/
def trunc_cycle_factors [fintype α] (f : perm α) :
trunc {l : list (perm α) // l.prod = f ∧ (∀ g ∈ l, is_cycle g) ∧ l.pairwise disjoint} :=
quotient.rec_on_subsingleton (@univ α _).1
(λ l h, trunc.mk (cycle_factors_aux l f h))
(show ∀ x, f x ≠ x → x ∈ (@univ α _).1, from λ _ _, mem_univ _)
section cycle_factors_finset
variables [fintype α] (f : perm α)
/-- Factors a permutation `f` into a `finset` of disjoint cyclic permutations that multiply to `f`.
-/
def cycle_factors_finset : finset (perm α) :=
(trunc_cycle_factors f).lift
(λ (l : {l : list (perm α) // l.prod = f ∧ (∀ g ∈ l, is_cycle g) ∧ l.pairwise disjoint}),
l.val.to_finset) (λ ⟨l, hl⟩ ⟨l', hl'⟩, list.to_finset_eq_of_perm _ _
(list_cycles_perm_list_cycles (hl'.left.symm ▸ hl.left) hl.right.left (hl'.right.left)
hl.right.right hl'.right.right))
lemma cycle_factors_finset_eq_list_to_finset {σ : perm α} {l : list (perm α)} (hn : l.nodup) :
σ.cycle_factors_finset = l.to_finset ↔ (∀ f : perm α, f ∈ l → f.is_cycle) ∧
l.pairwise disjoint ∧ l.prod = σ :=
begin
obtain ⟨⟨l', hp', hc', hd'⟩, hl⟩ := trunc.exists_rep σ.trunc_cycle_factors,
have ht : cycle_factors_finset σ = l'.to_finset,
{ rw [cycle_factors_finset, ←hl, trunc.lift_mk] },
rw ht,
split,
{ intro h,
have hn' : l'.nodup := nodup_of_pairwise_disjoint_cycles hc' hd',
have hperm : l ~ l' := list.perm_of_nodup_nodup_to_finset_eq hn hn' h.symm,
refine ⟨_, _, _⟩,
{ exact λ _ h, hc' _ (hperm.subset h)},
{ rwa list.perm.pairwise_iff disjoint.symmetric hperm },
{ rw [←hp', hperm.symm.prod_eq'],
refine hd'.imp _,
exact λ _ _, disjoint.commute } },
{ rintro ⟨hc, hd, hp⟩,
refine list.to_finset_eq_of_perm _ _ _,
refine list_cycles_perm_list_cycles _ hc' hc hd' hd,
rw [hp, hp'] }
end
lemma cycle_factors_finset_eq_finset {σ : perm α} {s : finset (perm α)} :
σ.cycle_factors_finset = s ↔ (∀ f : perm α, f ∈ s → f.is_cycle) ∧
(∃ h : (∀ (a ∈ s) (b ∈ s), a ≠ b → disjoint a b), s.noncomm_prod id
(λ a ha b hb, (em (a = b)).by_cases (λ h, h ▸ commute.refl a)
(set.pairwise_on.mono' (λ _ _, disjoint.commute) h a ha b hb)) = σ) :=
begin
obtain ⟨l, hl, rfl⟩ := s.exists_list_nodup_eq,
rw cycle_factors_finset_eq_list_to_finset hl,
simp only [noncomm_prod_to_finset, hl, exists_prop, list.mem_to_finset, and.congr_left_iff,
and.congr_right_iff, list.map_id, ne.def],
intros,
exact ⟨list.forall_of_pairwise disjoint.symmetric, hl.pairwise_of_forall_ne⟩
end
lemma cycle_factors_finset_pairwise_disjoint (p : perm α) (hp : p ∈ cycle_factors_finset f)
(q : perm α) (hq : q ∈ cycle_factors_finset f) (h : p ≠ q) :
disjoint p q :=
begin
have : f.cycle_factors_finset = f.cycle_factors_finset := rfl,
obtain ⟨-, hd, -⟩ := cycle_factors_finset_eq_finset.mp this,
exact hd p hp q hq h
end
lemma cycle_factors_finset_mem_commute (p : perm α) (hp : p ∈ cycle_factors_finset f)
(q : perm α) (hq : q ∈ cycle_factors_finset f) :
_root_.commute p q :=
begin
by_cases h : p = q,
{ exact h ▸ commute.refl _ },
{ exact (cycle_factors_finset_pairwise_disjoint _ _ hp _ hq h).commute }
end
/-- The product of cycle factors is equal to the original `f : perm α`. -/
lemma cycle_factors_finset_noncomm_prod
(comm : ∀ (g ∈ f.cycle_factors_finset) (h ∈ f.cycle_factors_finset),
commute (id g) (id h) := cycle_factors_finset_mem_commute f) :
f.cycle_factors_finset.noncomm_prod id (comm) = f :=
begin
have : f.cycle_factors_finset = f.cycle_factors_finset := rfl,
obtain ⟨-, hd, hp⟩ := cycle_factors_finset_eq_finset.mp this,
exact hp
end
lemma mem_cycle_factors_finset_iff {f p : perm α} :
p ∈ cycle_factors_finset f ↔ p.is_cycle ∧ ∀ (a ∈ p.support), p a = f a :=
begin
obtain ⟨l, hl, hl'⟩ := f.cycle_factors_finset.exists_list_nodup_eq,
rw ←hl',
rw [eq_comm, cycle_factors_finset_eq_list_to_finset hl] at hl',
simpa [list.mem_to_finset, ne.def, ←hl'.right.right]
using mem_list_cycles_iff hl'.left hl'.right.left
end
lemma cycle_of_mem_cycle_factors_finset_iff {f : perm α} {x : α} :
cycle_of f x ∈ cycle_factors_finset f ↔ x ∈ f.support :=
begin
rw mem_cycle_factors_finset_iff,
split,
{ rintro ⟨hc, h⟩,
contrapose! hc,
rw [not_mem_support, ←cycle_of_eq_one_iff] at hc,
simp [hc] },
{ intros hx,
refine ⟨is_cycle_cycle_of _ (mem_support.mp hx), _⟩,
intros y hy,
rw mem_support at hy,
rw cycle_of_apply,
split_ifs with H,
{ refl },
{ rw cycle_of_apply_of_not_same_cycle H at hy,
contradiction } }
end
lemma mem_cycle_factors_finset_support_le {p f : perm α} (h : p ∈ cycle_factors_finset f) :
p.support ≤ f.support :=
begin
rw mem_cycle_factors_finset_iff at h,
intros x hx,
rwa [mem_support, ←h.right x hx, ←mem_support]
end
lemma cycle_factors_finset_eq_empty_iff {f : perm α} :
cycle_factors_finset f = ∅ ↔ f = 1 :=
by simpa [cycle_factors_finset_eq_finset] using eq_comm
@[simp] lemma cycle_factors_finset_one :
cycle_factors_finset (1 : perm α) = ∅ :=
by simp [cycle_factors_finset_eq_empty_iff]
@[simp] lemma cycle_factors_finset_eq_singleton_self_iff {f : perm α} :
f.cycle_factors_finset = {f} ↔ f.is_cycle :=
by simp [cycle_factors_finset_eq_finset]
lemma is_cycle.cycle_factors_finset_eq_singleton {f : perm α} (hf : is_cycle f) :
f.cycle_factors_finset = {f} :=
cycle_factors_finset_eq_singleton_self_iff.mpr hf
lemma cycle_factors_finset_eq_singleton_iff {f g : perm α} :
f.cycle_factors_finset = {g} ↔ f.is_cycle ∧ f = g :=
begin
suffices : f = g → (g.is_cycle ↔ f.is_cycle),
{ simpa [cycle_factors_finset_eq_finset, eq_comm] },
rintro rfl,
exact iff.rfl
end
/-- Two permutations `f g : perm α` have the same cycle factors iff they are the same. -/
lemma cycle_factors_finset_injective : function.injective (@cycle_factors_finset α _ _) :=
begin
intros f g h,
rw ←cycle_factors_finset_noncomm_prod f,
simpa [h] using cycle_factors_finset_noncomm_prod g
end
lemma disjoint.disjoint_cycle_factors_finset {f g : perm α} (h : disjoint f g) :
_root_.disjoint (cycle_factors_finset f) (cycle_factors_finset g) :=
begin
rw disjoint_iff_disjoint_support at h,
intros x hx,
simp only [mem_cycle_factors_finset_iff, inf_eq_inter, mem_inter, mem_support] at hx,
obtain ⟨⟨⟨a, ha, -⟩, hf⟩, -, hg⟩ := hx,
refine h (_ : a ∈ f.support ∩ g.support),
simp [ha, ←hf a ha, ←hg a ha]
end
lemma disjoint.cycle_factors_finset_mul_eq_union {f g : perm α} (h : disjoint f g) :
cycle_factors_finset (f * g) = cycle_factors_finset f ∪ cycle_factors_finset g :=
begin
rw cycle_factors_finset_eq_finset,
split,
{ simp only [mem_cycle_factors_finset_iff, mem_union],
rintro _ (⟨h, -⟩ | ⟨h, -⟩);
exact h },
{ refine ⟨_, _⟩,
{ simp_rw mem_union,
rintros x (hx | hx) y (hy | hy) hxy,
{ exact cycle_factors_finset_pairwise_disjoint _ _ hx _ hy hxy },
{ exact h.mono (mem_cycle_factors_finset_support_le hx)
(mem_cycle_factors_finset_support_le hy) },
{ exact h.symm.mono (mem_cycle_factors_finset_support_le hx)
(mem_cycle_factors_finset_support_le hy) },
{ exact cycle_factors_finset_pairwise_disjoint _ _ hx _ hy hxy } },
{ rw noncomm_prod_union_of_disjoint h.disjoint_cycle_factors_finset,
rw [cycle_factors_finset_noncomm_prod, cycle_factors_finset_noncomm_prod] } }
end
lemma disjoint_mul_inv_of_mem_cycle_factors_finset {f g : perm α} (h : f ∈ cycle_factors_finset g) :
disjoint (g * f⁻¹) f :=
begin
rw mem_cycle_factors_finset_iff at h,
intro x,
by_cases hx : f x = x,
{ exact or.inr hx },
{ refine or.inl _,
rw [mul_apply, ←h.right, apply_inv_self],
rwa [←support_inv, apply_mem_support, support_inv, mem_support] }
end
end cycle_factors_finset
@[elab_as_eliminator] lemma cycle_induction_on [fintype β] (P : perm β → Prop) (σ : perm β)
(base_one : P 1) (base_cycles : ∀ σ : perm β, σ.is_cycle → P σ)
(induction_disjoint : ∀ σ τ : perm β, disjoint σ τ → is_cycle σ → P σ → P τ → P (σ * τ)) :
P σ :=
begin
suffices :
∀ l : list (perm β), (∀ τ : perm β, τ ∈ l → τ.is_cycle) → l.pairwise disjoint → P l.prod,
{ classical,
let x := σ.trunc_cycle_factors.out,
exact (congr_arg P x.2.1).mp (this x.1 x.2.2.1 x.2.2.2) },
intro l,
induction l with σ l ih,
{ exact λ _ _, base_one },
{ intros h1 h2,
rw list.prod_cons,
exact induction_disjoint σ l.prod
(disjoint_prod_right _ (list.pairwise_cons.mp h2).1)
(h1 _ (list.mem_cons_self _ _))
(base_cycles σ (h1 σ (l.mem_cons_self σ)))
(ih (λ τ hτ, h1 τ (list.mem_cons_of_mem σ hτ)) (list.pairwise_of_pairwise_cons h2)) },
end
lemma cycle_factors_finset_mul_inv_mem_eq_sdiff [fintype α] {f g : perm α}
(h : f ∈ cycle_factors_finset g) :
cycle_factors_finset (g * f⁻¹) = (cycle_factors_finset g) \ {f} :=
begin
revert f,
apply cycle_induction_on _ g,
{ simp },
{ intros σ hσ f hf,
simp only [cycle_factors_finset_eq_singleton_self_iff.mpr hσ, mem_singleton] at hf ⊢,
simp [hf] },
{ intros σ τ hd hc hσ hτ f,
simp_rw [hd.cycle_factors_finset_mul_eq_union, mem_union],
-- if only `wlog` could work here...
rintro (hf | hf),
{ rw [hd.commute.eq, union_comm, union_sdiff_distrib, sdiff_singleton_eq_erase,
erase_eq_of_not_mem, mul_assoc, disjoint.cycle_factors_finset_mul_eq_union, hσ hf],
{ rw mem_cycle_factors_finset_iff at hf,
intro x,
cases hd.symm x with hx hx,
{ exact or.inl hx },
{ refine or.inr _,
by_cases hfx : f x = x,
{ rw ←hfx,
simpa [hx] using hfx.symm },
{ rw mul_apply,
rw ←hf.right _ (mem_support.mpr hfx) at hx,
contradiction } } },
{ exact λ H, hd.disjoint_cycle_factors_finset (mem_inter_of_mem hf H) } },
{ rw [union_sdiff_distrib, sdiff_singleton_eq_erase,
erase_eq_of_not_mem, mul_assoc, disjoint.cycle_factors_finset_mul_eq_union, hτ hf],
{ rw mem_cycle_factors_finset_iff at hf,
intro x,
cases hd x with hx hx,
{ exact or.inl hx },
{ refine or.inr _,
by_cases hfx : f x = x,
{ rw ←hfx,
simpa [hx] using hfx.symm },
{ rw mul_apply,
rw ←hf.right _ (mem_support.mpr hfx) at hx,
contradiction } } },
{ exact λ H, hd.disjoint_cycle_factors_finset (mem_inter_of_mem H hf) } } }
end
lemma same_cycle.nat_of_mem_support [fintype α] (f : perm α) {x y : α} (h : same_cycle f x y)
(hx : x ∈ f.support) :
∃ (i : ℕ) (hi' : i < (f.cycle_of x).support.card), (f ^ i) x = y :=
begin
revert f,
intro f,
apply cycle_induction_on _ f,
{ simp },
{ intros g hg H hx,
rw mem_support at hx,
rw [hg.cycle_of_eq hx, ←order_of_is_cycle hg],
exact H.nat' },
{ rintros g h hd hg IH IH' ⟨m, rfl⟩ hx,
cases (disjoint_iff_eq_or_eq.mp hd) x with hgx hhx,
{ have hpow : ∀ (k : ℤ), ((g * h) ^ k) x = (h ^ k) x,
{ intro k,
suffices : (g ^ k) x = x,
{ simpa [hd.commute.eq, hd.commute.symm.mul_gpow] },
rw gpow_apply_eq_self_of_apply_eq_self,
simpa using hgx },
obtain ⟨k, hk, hk'⟩ := IH' _ _,
{ refine ⟨k, _, _⟩,
{ rw [←cycle_of_eq_one_iff] at hgx,
rwa [hd.cycle_of_mul_distrib, hgx, one_mul] },
{ simpa [←gpow_coe_nat, hpow] using hk' } },
{ use m,
simp [hpow] },
{ rw [mem_support, hd.commute.eq] at hx,
simpa [hgx] using hx } },
{ have hpow : ∀ (k : ℤ), ((g * h) ^ k) x = (g ^ k) x,
{ intro k,
suffices : (h ^ k) x = x,
{ simpa [hd.commute.mul_gpow] },
rw gpow_apply_eq_self_of_apply_eq_self,
simpa using hhx },
obtain ⟨k, hk, hk'⟩ := IH _ _,
{ refine ⟨k, _, _⟩,
{ rw [←cycle_of_eq_one_iff] at hhx,
rwa [hd.cycle_of_mul_distrib, hhx, mul_one] },
{ simpa [←gpow_coe_nat, hpow] using hk' } },
{ use m,
simp [hpow] },
{ simpa [hhx] using hx } } }
end
lemma same_cycle.nat [fintype α] (f : perm α) {x y : α} (h : same_cycle f x y) :
∃ (i : ℕ) (hi : 0 < i) (hi' : i ≤ (f.cycle_of x).support.card + 1), (f ^ i) x = y :=
begin
by_cases hx : x ∈ f.support,
{ obtain ⟨k, hk, hk'⟩ := same_cycle.nat_of_mem_support f h hx,
cases k,
{ refine ⟨(f.cycle_of x).support.card, _, self_le_add_right _ _, _⟩,
{ refine zero_lt_one.trans (one_lt_card_support_of_ne_one _),
simpa using hx },
{ simp only [perm.coe_one, id.def, pow_zero] at hk',
subst hk',
rw [←order_of_is_cycle (is_cycle_cycle_of _ (mem_support.mp hx)),
←cycle_of_pow_apply_self, pow_order_of_eq_one, one_apply] } },
{ exact ⟨k + 1, by simp, nat.le_succ_of_le hk.le, hk'⟩ } },
{ refine ⟨1, zero_lt_one, by simp, _⟩,
obtain ⟨k, rfl⟩ := h,
rw [not_mem_support] at hx,
rw [pow_apply_eq_self_of_apply_eq_self hx,
gpow_apply_eq_self_of_apply_eq_self hx] }
end
section generation
variables [fintype α] [fintype β]
open subgroup
lemma closure_is_cycle : closure {σ : perm β | is_cycle σ} = ⊤ :=
begin
classical,
exact top_le_iff.mp (le_trans (ge_of_eq closure_is_swap) (closure_mono (λ _, is_swap.is_cycle))),
end
lemma closure_cycle_adjacent_swap {σ : perm α} (h1 : is_cycle σ) (h2 : σ.support = ⊤) (x : α) :
closure ({σ, swap x (σ x)} : set (perm α)) = ⊤ :=
begin
let H := closure ({σ, swap x (σ x)} : set (perm α)),
have h3 : σ ∈ H := subset_closure (set.mem_insert σ _),
have h4 : swap x (σ x) ∈ H := subset_closure (set.mem_insert_of_mem _ (set.mem_singleton _)),
have step1 : ∀ (n : ℕ), swap ((σ ^ n) x) ((σ^(n+1)) x) ∈ H,
{ intro n,
induction n with n ih,
{ exact subset_closure (set.mem_insert_of_mem _ (set.mem_singleton _)) },
{ convert H.mul_mem (H.mul_mem h3 ih) (H.inv_mem h3),
rw [mul_swap_eq_swap_mul, mul_inv_cancel_right], refl } },
have step2 : ∀ (n : ℕ), swap x ((σ ^ n) x) ∈ H,
{ intro n,
induction n with n ih,
{ convert H.one_mem,
exact swap_self x },
{ by_cases h5 : x = (σ ^ n) x,
{ rw [pow_succ, mul_apply, ←h5], exact h4 },
by_cases h6 : x = (σ^(n+1)) x,
{ rw [←h6, swap_self], exact H.one_mem },
rw [swap_comm, ←swap_mul_swap_mul_swap h5 h6],
exact H.mul_mem (H.mul_mem (step1 n) ih) (step1 n) } },
have step3 : ∀ (y : α), swap x y ∈ H,
{ intro y,
have hx : x ∈ (⊤ : finset α) := finset.mem_univ x,
rw [←h2, mem_support] at hx,
have hy : y ∈ (⊤ : finset α) := finset.mem_univ y,
rw [←h2, mem_support] at hy,
cases is_cycle.exists_pow_eq h1 hx hy with n hn,
rw ← hn,
exact step2 n },
have step4 : ∀ (y z : α), swap y z ∈ H,
{ intros y z,
by_cases h5 : z = x,
{ rw [h5, swap_comm], exact step3 y },
by_cases h6 : z = y,
{ rw [h6, swap_self], exact H.one_mem },
rw [←swap_mul_swap_mul_swap h5 h6, swap_comm z x],
exact H.mul_mem (H.mul_mem (step3 y) (step3 z)) (step3 y) },
rw [eq_top_iff, ←closure_is_swap, closure_le],
rintros τ ⟨y, z, h5, h6⟩,
rw h6,
exact step4 y z,
end
lemma closure_cycle_coprime_swap {n : ℕ} {σ : perm α} (h0 : nat.coprime n (fintype.card α))
(h1 : is_cycle σ) (h2 : σ.support = finset.univ) (x : α) :
closure ({σ, swap x ((σ ^ n) x)} : set (perm α)) = ⊤ :=
begin
rw [←finset.card_univ, ←h2, ←order_of_is_cycle h1] at h0,
cases exists_pow_eq_self_of_coprime h0 with m hm,
have h2' : (σ ^ n).support = ⊤ := eq.trans (support_pow_coprime h0) h2,
have h1' : is_cycle ((σ ^ n) ^ (m : ℤ)) := by rwa ← hm at h1,
replace h1' : is_cycle (σ ^ n) := is_cycle_of_is_cycle_pow h1'
(le_trans (support_pow_le σ n) (ge_of_eq (congr_arg support hm))),
rw [eq_top_iff, ←closure_cycle_adjacent_swap h1' h2' x, closure_le, set.insert_subset],
exact ⟨subgroup.pow_mem (closure _) (subset_closure (set.mem_insert σ _)) n,
set.singleton_subset_iff.mpr (subset_closure (set.mem_insert_of_mem _ (set.mem_singleton _)))⟩,
end
lemma closure_prime_cycle_swap {σ τ : perm α} (h0 : (fintype.card α).prime) (h1 : is_cycle σ)
(h2 : σ.support = finset.univ) (h3 : is_swap τ) : closure ({σ, τ} : set (perm α)) = ⊤ :=
begin
obtain ⟨x, y, h4, h5⟩ := h3,
obtain ⟨i, hi⟩ := h1.exists_pow_eq (mem_support.mp
((finset.ext_iff.mp h2 x).mpr (finset.mem_univ x)))
(mem_support.mp ((finset.ext_iff.mp h2 y).mpr (finset.mem_univ y))),
rw [h5, ←hi],
refine closure_cycle_coprime_swap (nat.coprime.symm
(h0.coprime_iff_not_dvd.mpr (λ h, h4 _))) h1 h2 x,
cases h with m hm,
rwa [hm, pow_mul, ←finset.card_univ, ←h2, ←order_of_is_cycle h1,
pow_order_of_eq_one, one_pow, one_apply] at hi,
end
end generation
section
variables [fintype α] {σ τ : perm α}
noncomputable theory
lemma is_conj_of_support_equiv (f : {x // x ∈ (σ.support : set α)} ≃ {x // x ∈ (τ.support : set α)})
(hf : ∀ (x : α) (hx : x ∈ (σ.support : set α)), (f ⟨σ x, apply_mem_support.2 hx⟩ : α) =
τ ↑(f ⟨x,hx⟩)) :
is_conj σ τ :=
begin
refine is_conj_iff.2 ⟨equiv.extend_subtype f, _⟩,
rw mul_inv_eq_iff_eq_mul,
ext,
simp only [perm.mul_apply],
by_cases hx : x ∈ σ.support,
{ rw [equiv.extend_subtype_apply_of_mem, equiv.extend_subtype_apply_of_mem],
{ exact hf x (finset.mem_coe.2 hx) } },
{ rwa [not_not.1 ((not_congr mem_support).1 (equiv.extend_subtype_not_mem f _ _)),
not_not.1 ((not_congr mem_support).mp hx)] }
end
theorem is_cycle.is_conj (hσ : is_cycle σ) (hτ : is_cycle τ) (h : σ.support.card = τ.support.card) :
is_conj σ τ :=
begin
refine is_conj_of_support_equiv (hσ.gpowers_equiv_support.symm.trans
((gpowers_equiv_gpowers begin
rw [order_of_is_cycle hσ, h, order_of_is_cycle hτ],
end).trans hτ.gpowers_equiv_support)) _,
intros x hx,
simp only [perm.mul_apply, equiv.trans_apply, equiv.sum_congr_apply],
obtain ⟨n, rfl⟩ := hσ.exists_pow_eq (classical.some_spec hσ).1 (mem_support.1 hx),
apply eq.trans _ (congr rfl (congr rfl (congr rfl
(congr rfl (hσ.gpowers_equiv_support_symm_apply n).symm)))),
apply (congr rfl (congr rfl (congr rfl (hσ.gpowers_equiv_support_symm_apply (n + 1))))).trans _,
simp only [ne.def, is_cycle.gpowers_equiv_support_apply,
subtype.coe_mk, gpowers_equiv_gpowers_apply],
rw [pow_succ, perm.mul_apply],
end
theorem is_cycle.is_conj_iff (hσ : is_cycle σ) (hτ : is_cycle τ) :
is_conj σ τ ↔ σ.support.card = τ.support.card :=
⟨begin
intro h,
obtain ⟨π, rfl⟩ := is_conj_iff.1 h,
apply finset.card_congr (λ a ha, π a) (λ _ ha, _) (λ _ _ _ _ ab, π.injective ab) (λ b hb, _),
{ simp [mem_support.1 ha] },
{ refine ⟨π⁻¹ b, ⟨_, π.apply_inv_self b⟩⟩,
contrapose! hb,
rw [mem_support, not_not] at hb,
rw [mem_support, not_not, perm.mul_apply, perm.mul_apply, hb, perm.apply_inv_self] }
end, hσ.is_conj hτ⟩
@[simp]
lemma support_conj : (σ * τ * σ⁻¹).support = τ.support.map σ.to_embedding :=
begin
ext,
simp only [mem_map_equiv, perm.coe_mul, comp_app, ne.def, perm.mem_support, equiv.eq_symm_apply],
refl,
end
lemma card_support_conj : (σ * τ * σ⁻¹).support.card = τ.support.card :=
by simp
end
theorem disjoint.is_conj_mul {α : Type*} [fintype α] {σ τ π ρ : perm α}
(hc1 : is_conj σ π) (hc2 : is_conj τ ρ)
(hd1 : disjoint σ τ) (hd2 : disjoint π ρ) :
is_conj (σ * τ) (π * ρ) :=
begin
classical,
obtain ⟨f, rfl⟩ := is_conj_iff.1 hc1,
obtain ⟨g, rfl⟩ := is_conj_iff.1 hc2,
have hd1' := coe_inj.2 hd1.support_mul,
have hd2' := coe_inj.2 hd2.support_mul,
rw [coe_union] at *,
have hd1'' := disjoint_iff_disjoint_coe.1 (disjoint_iff_disjoint_support.1 hd1),
have hd2'' := disjoint_iff_disjoint_coe.1 (disjoint_iff_disjoint_support.1 hd2),
refine is_conj_of_support_equiv _ _,
{ refine ((equiv.set.of_eq hd1').trans (equiv.set.union hd1'')).trans
((equiv.sum_congr (subtype_equiv f (λ a, _)) (subtype_equiv g (λ a, _))).trans
((equiv.set.of_eq hd2').trans (equiv.set.union hd2'')).symm);
{ simp only [set.mem_image, to_embedding_apply, exists_eq_right,
support_conj, coe_map, apply_eq_iff_eq] } },
{ intros x hx,
simp only [trans_apply, symm_trans_apply, set.of_eq_apply,
set.of_eq_symm_apply, equiv.sum_congr_apply],
rw [hd1', set.mem_union] at hx,
cases hx with hxσ hxτ,
{ rw [mem_coe, mem_support] at hxσ,
rw [set.union_apply_left hd1'' _, set.union_apply_left hd1'' _],
simp only [subtype_equiv_apply, perm.coe_mul, sum.map_inl, comp_app,
set.union_symm_apply_left, subtype.coe_mk, apply_eq_iff_eq],
{ have h := (hd2 (f x)).resolve_left _,
{ rw [mul_apply, mul_apply] at h,
rw [h, inv_apply_self, (hd1 x).resolve_left hxσ] },
{ rwa [mul_apply, mul_apply, inv_apply_self, apply_eq_iff_eq] } },
{ rwa [subtype.coe_mk, subtype.coe_mk, mem_coe, mem_support] },
{ rwa [subtype.coe_mk, subtype.coe_mk, perm.mul_apply,
(hd1 x).resolve_left hxσ, mem_coe, apply_mem_support, mem_support] } },
{ rw [mem_coe, ← apply_mem_support, mem_support] at hxτ,
rw [set.union_apply_right hd1'' _, set.union_apply_right hd1'' _],
simp only [subtype_equiv_apply, perm.coe_mul, sum.map_inr, comp_app,
set.union_symm_apply_right, subtype.coe_mk, apply_eq_iff_eq],
{ have h := (hd2 (g (τ x))).resolve_right _,
{ rw [mul_apply, mul_apply] at h,
rw [inv_apply_self, h, (hd1 (τ x)).resolve_right hxτ] },
{ rwa [mul_apply, mul_apply, inv_apply_self, apply_eq_iff_eq] } },
{ rwa [subtype.coe_mk, subtype.coe_mk, mem_coe, ← apply_mem_support, mem_support] },
{ rwa [subtype.coe_mk, subtype.coe_mk, perm.mul_apply,
(hd1 (τ x)).resolve_right hxτ, mem_coe, mem_support] } } }
end
section fixed_points
/-!
### Fixed points
-/
lemma fixed_point_card_lt_of_ne_one [fintype α] {σ : perm α} (h : σ ≠ 1) :
(filter (λ x, σ x = x) univ).card < fintype.card α - 1 :=
begin
rw [lt_tsub_iff_left, ← lt_tsub_iff_right, ← finset.card_compl,
finset.compl_filter],
exact one_lt_card_support_of_ne_one h
end
end fixed_points
end equiv.perm
|
afca860bc97b7ba96621877fc8e25ac2c0125e22 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/data/nat/choose/central.lean | 9cd4bf6c27000ba06927a9df627d5babbd936d8f | [
"Apache-2.0"
] | permissive | Vierkantor/mathlib | 0ea59ac32a3a43c93c44d70f441c4ee810ccceca | 83bc3b9ce9b13910b57bda6b56222495ebd31c2f | refs/heads/master | 1,658,323,012,449 | 1,652,256,003,000 | 1,652,256,003,000 | 209,296,341 | 0 | 1 | Apache-2.0 | 1,568,807,655,000 | 1,568,807,655,000 | null | UTF-8 | Lean | false | false | 8,649 | lean | /-
Copyright (c) 2021 Patrick Stevens. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Patrick Stevens, Thomas Browning
-/
import data.nat.prime
import data.nat.choose.basic
import data.nat.choose.sum
import data.nat.multiplicity
import number_theory.padics.padic_norm
import tactic.norm_num
import tactic.linarith
/-!
# Central binomial coefficients
This file proves properties of the central binomial coefficients (that is, `nat.choose (2 * n) n`).
## Main definition and results
* `nat.central_binom`: the central binomial coefficient, `(2 * n).choose n`.
* `nat.succ_mul_central_binom_succ`: the inductive relationship between successive central binomial
coefficients.
* `nat.four_pow_lt_mul_central_binom`: an exponential lower bound on the central binomial
coefficient.
* `nat.multiplicity_central_binom_le`: a logarithmic upper bound on the multiplicity of a prime in
the central binomial coefficient.
* `nat.multiplicity_central_binom_of_large_le_one`: sufficiently large primes appear at most once
in the factorisation of the central binomial coefficient.
* `nat.multiplicity_central_binom_of_large_eq_zero`: sufficiently large primes less than n do not
appear in the factorisation of the central binomial coefficient.
-/
namespace nat
/--
The central binomial coefficient, `nat.choose (2 * n) n`.
-/
def central_binom (n : ℕ) := (2 * n).choose n
lemma central_binom_eq_two_mul_choose (n : ℕ) : central_binom n = (2 * n).choose n := rfl
lemma central_binom_pos (n : ℕ) : 0 < central_binom n :=
choose_pos (nat.le_mul_of_pos_left zero_lt_two)
lemma central_binom_ne_zero (n : ℕ) : central_binom n ≠ 0 :=
(central_binom_pos n).ne'
@[simp] lemma central_binom_zero : central_binom 0 = 1 :=
choose_zero_right _
/--
The central binomial coefficient is the largest binomial coefficient.
-/
lemma choose_le_central_binom (r n : ℕ) : choose (2 * n) r ≤ central_binom n :=
calc (2 * n).choose r ≤ (2 * n).choose (2 * n / 2) : choose_le_middle r (2 * n)
... = (2 * n).choose n : by rw nat.mul_div_cancel_left n zero_lt_two
lemma two_le_central_binom (n : ℕ) (n_pos : 0 < n) : 2 ≤ central_binom n :=
calc 2 ≤ 2 * n : le_mul_of_pos_right n_pos
... = (2 * n).choose 1 : (choose_one_right (2 * n)).symm
... ≤ central_binom n : choose_le_central_binom 1 n
/--
An inductive property of the central binomial coefficient.
-/
lemma succ_mul_central_binom_succ (n : ℕ) :
(n + 1) * central_binom (n + 1) = 2 * (2 * n + 1) * central_binom n :=
calc (n + 1) * (2 * (n + 1)).choose (n + 1) = (2 * n + 2).choose (n + 1) * (n + 1) : mul_comm _ _
... = (2 * n + 1).choose n * (2 * n + 2) : by rw [choose_succ_right_eq, choose_mul_succ_eq]
... = 2 * ((2 * n + 1).choose n * (n + 1)) : by ring
... = 2 * ((2 * n + 1).choose n * ((2 * n + 1) - n)) :
by rw [two_mul n, add_assoc, nat.add_sub_cancel_left]
... = 2 * ((2 * n).choose n * (2 * n + 1)) : by rw choose_mul_succ_eq
... = (2 * (2 * n + 1)) * (2 * n).choose n : by rw [mul_assoc, mul_comm (2 * n + 1)]
/--
An exponential lower bound on the central binomial coefficient.
This bound is of interest because it appears in
[Tochiori's refinement of Erdős's proof of Bertrand's postulate](https://en.wikipedia.org/w/index.php?title=Proof_of_Bertrand%27s_postulate&oldid=859165151#Proof_by_Shigenori_Tochiori).
-/
lemma four_pow_lt_mul_central_binom (n : ℕ) (n_big : 4 ≤ n) : 4 ^ n < n * central_binom n :=
begin
induction n using nat.strong_induction_on with n IH,
rcases lt_trichotomy n 4 with (hn|rfl|hn),
{ clear IH, dec_trivial! },
{ norm_num [central_binom, choose] },
obtain ⟨n, rfl⟩ : ∃ m, n = m + 1 := nat.exists_eq_succ_of_ne_zero (zero_lt_four.trans hn).ne',
calc 4 ^ (n + 1) < 4 * (n * central_binom n) :
(mul_lt_mul_left zero_lt_four).mpr (IH n n.lt_succ_self (nat.le_of_lt_succ hn))
... ≤ 2 * (2 * n + 1) * central_binom n : by { rw ← mul_assoc, linarith }
... = (n + 1) * central_binom (n + 1) : (succ_mul_central_binom_succ n).symm,
end
/--
An exponential lower bound on the central binomial coefficient.
This bound is weaker than `four_pow_n_lt_n_mul_central_binom`, but it is of historical interest
because it appears in Erdős's proof of Bertrand's postulate.
-/
lemma four_pow_le_two_mul_self_mul_central_binom : ∀ (n : ℕ) (n_pos : 0 < n),
4 ^ n ≤ (2 * n) * central_binom n
| 0 pr := (nat.not_lt_zero _ pr).elim
| 1 pr := by norm_num [central_binom, choose]
| 2 pr := by norm_num [central_binom, choose]
| 3 pr := by norm_num [central_binom, choose]
| n@(m + 4) _ :=
calc 4 ^ n ≤ n * central_binom n : (four_pow_lt_mul_central_binom _ le_add_self).le
... ≤ 2 * n * central_binom n : by { rw [mul_assoc], refine le_mul_of_pos_left zero_lt_two }
variables {p n : ℕ}
/--
A logarithmic upper bound on the multiplicity of a prime in the central binomial coefficient.
-/
lemma padic_val_nat_central_binom_le (hp : p.prime) :
padic_val_nat p (central_binom n) ≤ log p (2 * n) :=
begin
rw @padic_val_nat_def _ ⟨hp⟩ _ (central_binom_pos n),
unfold central_binom,
have two_n_sub : 2 * n - n = n, by rw [two_mul n, nat.add_sub_cancel n n],
simp only [nat.prime.multiplicity_choose hp (le_mul_of_pos_left zero_lt_two) (lt_add_one _),
two_n_sub, ←two_mul, enat.get_coe', finset.filter_congr_decidable],
calc _ ≤ (finset.Ico 1 (log p (2 * n) + 1)).card : finset.card_filter_le _ _
... = (log p (2 * n) + 1) - 1 : nat.card_Ico _ _,
end
/--
Sufficiently large primes appear only to multiplicity 0 or 1 in the central binomial coefficient.
-/
lemma padic_val_nat_central_binom_of_large_le_one (hp : p.prime) (p_large : 2 * n < p ^ 2) :
(padic_val_nat p (central_binom n)) ≤ 1 :=
begin
have log_weak_bound : log p (2 * n) ≤ 2,
{ calc log p (2 * n) ≤ log p (p ^ 2) : log_mono_right (le_of_lt p_large)
... = 2 : log_pow hp.one_lt 2, },
have log_bound : log p (2 * n) ≤ 1,
{ cases le_or_lt (log p (2 * n)) 1 with log_le lt_log,
{ exact log_le, },
{ have v : log p (2 * n) = 2 := by linarith,
cases le_or_lt p (2 * n) with h h,
{ exfalso,
rw [log_of_one_lt_of_le hp.one_lt h, succ_inj', log_eq_one_iff] at v,
have bad : p ^ 2 ≤ 2 * n,
{ rw pow_two,
exact (nat.le_div_iff_mul_le _ _ (prime.pos hp)).1 v.2.2, },
exact lt_irrefl _ (lt_of_le_of_lt bad p_large), },
{ rw log_of_lt h,
exact zero_le 1, }, }, },
exact le_trans (padic_val_nat_central_binom_le hp) log_bound,
end
/--
Sufficiently large primes less than `n` do not appear in the factorisation of `central_binom n`.
-/
lemma padic_val_nat_central_binom_of_large_eq_zero
(hp : p.prime) (n_big : 2 < n) (p_le_n : p ≤ n) (big : 2 * n < 3 * p) :
padic_val_nat p (central_binom n) = 0 :=
begin
rw @padic_val_nat_def _ ⟨hp⟩ _ (central_binom_pos n),
unfold central_binom,
have two_n_sub : 2 * n - n = n, by rw [two_mul n, nat.add_sub_cancel n n],
simp only [nat.prime.multiplicity_choose hp (le_mul_of_pos_left zero_lt_two) (lt_add_one _),
two_n_sub, ←two_mul, finset.card_eq_zero, enat.get_coe', finset.filter_congr_decidable],
clear two_n_sub,
have three_lt_p : 3 ≤ p := by linarith,
have p_pos : 0 < p := nat.prime.pos hp,
apply finset.filter_false_of_mem,
intros i i_in_interval,
rw finset.mem_Ico at i_in_interval,
refine not_le.mpr _,
rcases lt_trichotomy 1 i with H|rfl|H,
{ have two_le_i : 2 ≤ i := nat.succ_le_of_lt H,
have two_n_lt_pow_p_i : 2 * n < p ^ i,
{ calc 2 * n < 3 * p : big
... ≤ p * p : (mul_le_mul_right p_pos).2 three_lt_p
... = p ^ 2 : (sq _).symm
... ≤ p ^ i : nat.pow_le_pow_of_le_right p_pos two_le_i, },
have n_mod : n % p ^ i = n,
{ apply nat.mod_eq_of_lt,
calc n ≤ n + n : nat.le.intro rfl
... = 2 * n : (two_mul n).symm
... < p ^ i : two_n_lt_pow_p_i, },
rw n_mod,
exact two_n_lt_pow_p_i, },
{ rw [pow_one],
suffices h23 : 2 * (p * (n / p)) + 2 * (n % p) < 2 * (p * (n / p)) + p,
{ exact (add_lt_add_iff_left (2 * (p * (n / p)))).mp h23, },
have n_big : 1 ≤ (n / p) := (nat.le_div_iff_mul_le' p_pos).2 (trans (one_mul _).le p_le_n),
rw [←mul_add, nat.div_add_mod],
calc 2 * n < 3 * p : big
... = 2 * p + p : nat.succ_mul _ _
... ≤ 2 * (p * (n / p)) + p : add_le_add_right ((mul_le_mul_left zero_lt_two).mpr
$ ((le_mul_iff_one_le_right p_pos).mpr n_big)) _ },
{ have i_zero: i = 0 := nat.le_zero_iff.mp (nat.le_of_lt_succ H),
rw [i_zero, pow_zero, nat.mod_one, mul_zero],
exact zero_lt_one, },
end
end nat
|
1b8e2425756dd13401eeea21398a272123924746 | b70031c8e2c5337b91d7e70f1e0c5f528f7b0e77 | /src/analysis/mean_inequalities.lean | 91332c4685da17e7f245e310cf84cdf19825e17f | [
"Apache-2.0"
] | permissive | molodiuc/mathlib | cae2ba3ef1601c1f42ca0b625c79b061b63fef5b | 98ebe5a6739fbe254f9ee9d401882d4388f91035 | refs/heads/master | 1,674,237,127,059 | 1,606,353,533,000 | 1,606,353,533,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 25,316 | lean | /-
Copyright (c) 2019 Yury Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury Kudryashov, Sébastien Gouëzel
-/
import analysis.convex.specific_functions
import analysis.special_functions.pow
import data.real.conjugate_exponents
import tactic.nth_rewrite
/-!
# Mean value inequalities
In this file we prove several inequalities, including AM-GM inequality, Young's inequality,
Hölder inequality, and Minkowski inequality.
## Main theorems
### AM-GM inequality:
The inequality says that the geometric mean of a tuple of non-negative numbers is less than or equal
to their arithmetic mean. We prove the weighted version of this inequality: if $w$ and $z$
are two non-negative vectors and $\sum_{i\in s} w_i=1$, then
$$
\prod_{i\in s} z_i^{w_i} ≤ \sum_{i\in s} w_iz_i.
$$
The classical version is a special case of this inequality for $w_i=\frac{1}{n}$.
We prove a few versions of this inequality. Each of the following lemmas comes in two versions:
a version for real-valued non-negative functions is in the `real` namespace, and a version for
`nnreal`-valued functions is in the `nnreal` namespace.
- `geom_mean_le_arith_mean_weighted` : weighted version for functions on `finset`s;
- `geom_mean_le_arith_mean2_weighted` : weighted version for two numbers;
- `geom_mean_le_arith_mean3_weighted` : weighted version for three numbers;
- `geom_mean_le_arith_mean4_weighted` : weighted version for four numbers.
### Generalized mean inequality
The inequality says that for two non-negative vectors $w$ and $z$ with $\sum_{i\in s} w_i=1$
and $p ≤ q$ we have
$$
\sqrt[p]{\sum_{i\in s} w_i z_i^p} ≤ \sqrt[q]{\sum_{i\in s} w_i z_i^q}.
$$
Currently we only prove this inequality for $p=1$. As in the rest of `mathlib`, we provide
different theorems for natural exponents (`pow_arith_mean_le_arith_mean_pow`), integer exponents
(`fpow_arith_mean_le_arith_mean_fpow`), and real exponents (`rpow_arith_mean_le_arith_mean_rpow` and
`arith_mean_le_rpow_mean`). In the first two cases we prove
$$
\left(\sum_{i\in s} w_i z_i\right)^n ≤ \sum_{i\in s} w_i z_i^n
$$
in order to avoid using real exponents. For real exponents we prove both this and standard versions.
### Young's inequality
Young's inequality says that for non-negative numbers `a`, `b`, `p`, `q` such that
$\frac{1}{p}+\frac{1}{q}=1$ we have
$$
ab ≤ \frac{a^p}{p} + \frac{b^q}{q}.
$$
This inequality is a special case of the AM-GM inequality. It can be used to prove Hölder's
inequality (see below) but we use a different proof.
### Hölder's inequality
The inequality says that for two conjugate exponents `p` and `q` (i.e., for two positive numbers
such that $\frac{1}{p}+\frac{1}{q}=1$) and any two non-negative vectors their inner product is
less than or equal to the product of the $L_p$ norm of the first vector and the $L_q$ norm of the
second vector:
$$
\sum_{i\in s} a_ib_i ≤ \sqrt[p]{\sum_{i\in s} a_i^p}\sqrt[q]{\sum_{i\in s} b_i^q}.
$$
We give versions of this result in `real`, `nnreal` and `ennreal`.
There are at least two short proofs of this inequality. In one proof we prenormalize both vectors,
then apply Young's inequality to each $a_ib_i$. We use a different proof deducing this inequality
from the generalized mean inequality for well-chosen vectors and weights.
### Minkowski's inequality
The inequality says that for `p ≥ 1` the function
$$
\|a\|_p=\sqrt[p]{\sum_{i\in s} a_i^p}
$$
satisfies the triangle inequality $\|a+b\|_p\le \|a\|_p+\|b\|_p$.
We give versions of this result in `real`, `nnreal` and `ennreal`.
We deduce this inequality from Hölder's inequality. Namely, Hölder inequality implies that $\|a\|_p$
is the maximum of the inner product $\sum_{i\in s}a_ib_i$ over `b` such that $\|b\|_q\le 1$. Now
Minkowski's inequality follows from the fact that the maximum value of the sum of two functions is
less than or equal to the sum of the maximum values of the summands.
## TODO
- each inequality `A ≤ B` should come with a theorem `A = B ↔ _`; one of the ways to prove them
is to define `strict_convex_on` functions.
- generalized mean inequality with any `p ≤ q`, including negative numbers;
- prove that the power mean tends to the geometric mean as the exponent tends to zero.
- prove integral versions of these inequalities.
-/
universes u v
open finset
open_locale classical nnreal big_operators
noncomputable theory
variables {ι : Type u} (s : finset ι)
namespace real
/-- AM-GM inequality: the geometric mean is less than or equal to the arithmetic mean, weighted version
for real-valued nonnegative functions. -/
theorem geom_mean_le_arith_mean_weighted (w z : ι → ℝ) (hw : ∀ i ∈ s, 0 ≤ w i)
(hw' : ∑ i in s, w i = 1) (hz : ∀ i ∈ s, 0 ≤ z i) :
(∏ i in s, (z i) ^ (w i)) ≤ ∑ i in s, w i * z i :=
begin
-- If some number `z i` equals zero and has non-zero weight, then LHS is 0 and RHS is nonnegative.
by_cases A : ∃ i ∈ s, z i = 0 ∧ w i ≠ 0,
{ rcases A with ⟨i, his, hzi, hwi⟩,
rw [prod_eq_zero his],
{ exact sum_nonneg (λ j hj, mul_nonneg (hw j hj) (hz j hj)) },
{ rw hzi, exact zero_rpow hwi } },
-- If all numbers `z i` with non-zero weight are positive, then we apply Jensen's inequality
-- for `exp` and numbers `log (z i)` with weights `w i`.
{ simp only [not_exists, not_and, ne.def, not_not] at A,
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; [apply prod_congr rfl, apply sum_congr rfl]; intros i hi,
{ cases eq_or_lt_of_le (hz i hi) with hz hz,
{ simp [A i hi hz.symm] },
{ exact rpow_def_of_pos hz _ } },
{ cases eq_or_lt_of_le (hz i hi) with hz hz,
{ simp [A i hi hz.symm] },
{ rw [exp_log hz] } } }
end
theorem pow_arith_mean_le_arith_mean_pow (w z : ι → ℝ) (hw : ∀ i ∈ s, 0 ≤ w i)
(hw' : ∑ i in s, w i = 1) (hz : ∀ i ∈ s, 0 ≤ z i) (n : ℕ) :
(∑ i in s, w i * z i) ^ n ≤ ∑ i in s, (w i * z i ^ n) :=
(convex_on_pow n).map_sum_le hw hw' hz
theorem pow_arith_mean_le_arith_mean_pow_of_even (w z : ι → ℝ) (hw : ∀ i ∈ s, 0 ≤ w i)
(hw' : ∑ i in s, w i = 1) {n : ℕ} (hn : even n) :
(∑ i in s, w i * z i) ^ n ≤ ∑ i in s, (w i * z i ^ n) :=
(convex_on_pow_of_even hn).map_sum_le hw hw' (λ _ _, trivial)
theorem fpow_arith_mean_le_arith_mean_fpow (w z : ι → ℝ) (hw : ∀ i ∈ s, 0 ≤ w i)
(hw' : ∑ i in s, w i = 1) (hz : ∀ i ∈ s, 0 < z i) (m : ℤ) :
(∑ i in s, w i * z i) ^ m ≤ ∑ i in s, (w i * z i ^ m) :=
(convex_on_fpow m).map_sum_le hw hw' hz
theorem rpow_arith_mean_le_arith_mean_rpow (w z : ι → ℝ) (hw : ∀ i ∈ s, 0 ≤ w i)
(hw' : ∑ i in s, w i = 1) (hz : ∀ i ∈ s, 0 ≤ z i) {p : ℝ} (hp : 1 ≤ p) :
(∑ i in s, w i * z i) ^ p ≤ ∑ i in s, (w i * z i ^ p) :=
(convex_on_rpow hp).map_sum_le hw hw' hz
theorem arith_mean_le_rpow_mean (w z : ι → ℝ) (hw : ∀ i ∈ s, 0 ≤ w i)
(hw' : ∑ i in s, w i = 1) (hz : ∀ i ∈ s, 0 ≤ z i) {p : ℝ} (hp : 1 ≤ p) :
∑ i in s, w i * z i ≤ (∑ i in s, (w i * z i ^ p)) ^ (1 / p) :=
begin
have : 0 < p := lt_of_lt_of_le zero_lt_one hp,
rw [← rpow_le_rpow_iff _ _ this, ← rpow_mul, one_div_mul_cancel (ne_of_gt this), rpow_one],
exact rpow_arith_mean_le_arith_mean_rpow s w z hw hw' hz hp,
all_goals { apply_rules [sum_nonneg, rpow_nonneg_of_nonneg],
intros i hi,
apply_rules [mul_nonneg, rpow_nonneg_of_nonneg, hw i hi, hz i hi] },
end
end real
namespace nnreal
/-- The geometric mean is less than or equal to the arithmetic mean, weighted version
for `nnreal`-valued functions. -/
theorem geom_mean_le_arith_mean_weighted (w z : ι → ℝ≥0) (hw' : ∑ i in s, w i = 1) :
(∏ i in s, (z i) ^ (w i:ℝ)) ≤ ∑ i in s, w i * z i :=
by exact_mod_cast real.geom_mean_le_arith_mean_weighted _ _ _ (λ i _, (w i).coe_nonneg)
(by assumption_mod_cast) (λ i _, (z i).coe_nonneg)
/-- The geometric mean is less than or equal to the arithmetic mean, weighted version
for two `nnreal` numbers. -/
theorem geom_mean_le_arith_mean2_weighted (w₁ w₂ p₁ p₂ : ℝ≥0) :
w₁ + w₂ = 1 → p₁ ^ (w₁:ℝ) * p₂ ^ (w₂:ℝ) ≤ w₁ * p₁ + w₂ * p₂ :=
by simpa 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]
using geom_mean_le_arith_mean_weighted (univ : finset (fin 2))
(fin.cons w₁ $ fin.cons w₂ fin_zero_elim) (fin.cons p₁ $ fin.cons p₂ $ fin_zero_elim)
theorem geom_mean_le_arith_mean3_weighted (w₁ w₂ w₃ p₁ p₂ p₃ : ℝ≥0) :
w₁ + w₂ + w₃ = 1 → p₁ ^ (w₁:ℝ) * p₂ ^ (w₂:ℝ) * p₃ ^ (w₃:ℝ) ≤ w₁ * p₁ + w₂ * p₂ + w₃ * p₃ :=
by simpa 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, mul_assoc]
using geom_mean_le_arith_mean_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)
theorem geom_mean_le_arith_mean4_weighted (w₁ w₂ w₃ w₄ p₁ p₂ p₃ p₄ : ℝ≥0) :
w₁ + w₂ + w₃ + w₄ = 1 → p₁ ^ (w₁:ℝ) * p₂ ^ (w₂:ℝ) * p₃ ^ (w₃:ℝ)* p₄ ^ (w₄:ℝ) ≤
w₁ * p₁ + w₂ * p₂ + w₃ * p₃ + w₄ * p₄ :=
by simpa 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, mul_assoc]
using geom_mean_le_arith_mean_weighted (univ : finset (fin 4))
(fin.cons w₁ $ fin.cons w₂ $ fin.cons w₃ $ fin.cons w₄ fin_zero_elim)
(fin.cons p₁ $ fin.cons p₂ $ fin.cons p₃ $ fin.cons p₄ fin_zero_elim)
/-- Weighted generalized mean inequality, version sums over finite sets, with `ℝ≥0`-valued
functions and natural exponent. -/
theorem pow_arith_mean_le_arith_mean_pow (w z : ι → ℝ≥0) (hw' : ∑ i in s, w i = 1) (n : ℕ) :
(∑ i in s, w i * z i) ^ n ≤ ∑ i in s, (w i * z i ^ n) :=
by exact_mod_cast real.pow_arith_mean_le_arith_mean_pow s _ _ (λ i _, (w i).coe_nonneg)
(by exact_mod_cast hw') (λ i _, (z i).coe_nonneg) n
/-- Weighted generalized mean inequality, version for sums over finite sets, with `ℝ≥0`-valued
functions and real exponents. -/
theorem rpow_arith_mean_le_arith_mean_rpow (w z : ι → ℝ≥0) (hw' : ∑ i in s, w i = 1) {p : ℝ}
(hp : 1 ≤ p) :
(∑ i in s, w i * z i) ^ p ≤ ∑ i in s, (w i * z i ^ p) :=
by exact_mod_cast real.rpow_arith_mean_le_arith_mean_rpow s _ _ (λ i _, (w i).coe_nonneg)
(by exact_mod_cast hw') (λ i _, (z i).coe_nonneg) hp
/-- Weighted generalized mean inequality, version for two elements of `ℝ≥0` and real exponents. -/
theorem rpow_arith_mean_le_arith_mean2_rpow (w₁ w₂ z₁ z₂ : ℝ≥0) (hw' : w₁ + w₂ = 1) {p : ℝ}
(hp : 1 ≤ p) :
(w₁ * z₁ + w₂ * z₂) ^ p ≤ w₁ * z₁ ^ p + w₂ * z₂ ^ p :=
begin
have h := rpow_arith_mean_le_arith_mean_rpow (univ : finset (fin 2))
(fin.cons w₁ $ fin.cons w₂ fin_zero_elim) (fin.cons z₁ $ fin.cons z₂ $ fin_zero_elim) _ hp,
{ simpa [fin.sum_univ_succ, fin.sum_univ_zero, fin.cons_succ, fin.cons_zero] using h, },
{ simp [hw', fin.sum_univ_succ, fin.sum_univ_zero, fin.cons_succ, fin.cons_zero], },
end
/-- Weighted generalized mean inequality, version for sums over finite sets, with `ℝ≥0`-valued
functions and real exponents. -/
theorem arith_mean_le_rpow_mean (w z : ι → ℝ≥0) (hw' : ∑ i in s, w i = 1) {p : ℝ}
(hp : 1 ≤ p) :
∑ i in s, w i * z i ≤ (∑ i in s, (w i * z i ^ p)) ^ (1 / p) :=
by exact_mod_cast real.arith_mean_le_rpow_mean s _ _ (λ i _, (w i).coe_nonneg)
(by exact_mod_cast hw') (λ i _, (z i).coe_nonneg) hp
end nnreal
namespace real
theorem geom_mean_le_arith_mean2_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.geom_mean_le_arith_mean2_weighted ⟨w₁, hw₁⟩ ⟨w₂, hw₂⟩ ⟨p₁, hp₁⟩ ⟨p₂, hp₂⟩ $
nnreal.coe_eq.1 $ by assumption
theorem geom_mean_le_arith_mean3_weighted {w₁ w₂ w₃ p₁ p₂ p₃ : ℝ} (hw₁ : 0 ≤ w₁) (hw₂ : 0 ≤ w₂)
(hw₃ : 0 ≤ w₃) (hp₁ : 0 ≤ p₁) (hp₂ : 0 ≤ p₂) (hp₃ : 0 ≤ p₃) (hw : w₁ + w₂ + w₃ = 1) :
p₁ ^ w₁ * p₂ ^ w₂ * p₃ ^ w₃ ≤ w₁ * p₁ + w₂ * p₂ + w₃ * p₃ :=
nnreal.geom_mean_le_arith_mean3_weighted ⟨w₁, hw₁⟩ ⟨w₂, hw₂⟩ ⟨w₃, hw₃⟩ ⟨p₁, hp₁⟩ ⟨p₂, hp₂⟩ ⟨p₃, hp₃⟩ $
nnreal.coe_eq.1 $ by assumption
theorem geom_mean_le_arith_mean4_weighted {w₁ w₂ w₃ w₄ p₁ p₂ p₃ p₄ : ℝ} (hw₁ : 0 ≤ w₁)
(hw₂ : 0 ≤ w₂) (hw₃ : 0 ≤ w₃) (hw₄ : 0 ≤ w₄) (hp₁ : 0 ≤ p₁) (hp₂ : 0 ≤ p₂) (hp₃ : 0 ≤ p₃)
(hp₄ : 0 ≤ p₄) (hw : w₁ + w₂ + w₃ + w₄ = 1) :
p₁ ^ w₁ * p₂ ^ w₂ * p₃ ^ w₃ * p₄ ^ w₄ ≤ w₁ * p₁ + w₂ * p₂ + w₃ * p₃ + w₄ * p₄ :=
nnreal.geom_mean_le_arith_mean4_weighted ⟨w₁, hw₁⟩ ⟨w₂, hw₂⟩ ⟨w₃, hw₃⟩ ⟨w₄, hw₄⟩
⟨p₁, hp₁⟩ ⟨p₂, hp₂⟩ ⟨p₃, hp₃⟩ ⟨p₄, hp₄⟩ $ nnreal.coe_eq.1 $ by assumption
/-- Young's inequality, a version for nonnegative real numbers. -/
theorem young_inequality_of_nonneg {a b p q : ℝ} (ha : 0 ≤ a) (hb : 0 ≤ b)
(hpq : p.is_conjugate_exponent q) :
a * b ≤ a^p / p + b^q / q :=
by simpa [← rpow_mul, ha, hb, hpq.ne_zero, hpq.symm.ne_zero, div_eq_inv_mul]
using geom_mean_le_arith_mean2_weighted hpq.one_div_nonneg hpq.symm.one_div_nonneg
(rpow_nonneg_of_nonneg ha p) (rpow_nonneg_of_nonneg hb q) hpq.inv_add_inv_conj
/-- Young's inequality, a version for arbitrary real numbers. -/
theorem young_inequality (a b : ℝ) {p q : ℝ} (hpq : p.is_conjugate_exponent q) :
a * b ≤ (abs a)^p / p + (abs b)^q / q :=
calc a * b ≤ abs (a * b) : le_abs_self (a * b)
... = abs a * abs b : abs_mul a b
... ≤ (abs a)^p / p + (abs b)^q / q :
real.young_inequality_of_nonneg (abs_nonneg a) (abs_nonneg b) hpq
end real
namespace nnreal
/-- Young's inequality, `ℝ≥0` version. We use `{p q : ℝ≥0}` in order to avoid constructing
witnesses of `0 ≤ p` and `0 ≤ q` for the denominators. -/
theorem young_inequality (a b : ℝ≥0) {p q : ℝ≥0} (hp : 1 < p) (hpq : 1 / p + 1 / q = 1) :
a * b ≤ a^(p:ℝ) / p + b^(q:ℝ) / q :=
real.young_inequality_of_nonneg a.coe_nonneg b.coe_nonneg ⟨hp, nnreal.coe_eq.2 hpq⟩
/-- Hölder inequality: the scalar product of two functions is bounded by the product of their
`L^p` and `L^q` norms when `p` and `q` are conjugate exponents. Version for sums over finite sets,
with `ℝ≥0`-valued functions. -/
theorem inner_le_Lp_mul_Lq (f g : ι → ℝ≥0) {p q : ℝ}
(hpq : p.is_conjugate_exponent q) :
∑ i in s, f i * g i ≤ (∑ i in s, (f i) ^ p) ^ (1 / p) * (∑ i in s, (g i) ^ q) ^ (1 / q) :=
begin
-- Let `G=∥g∥_q` be the `L_q`-norm of `g`.
set G := (∑ i in s, (g i) ^ q) ^ (1 / q),
have hGq : G ^ q = ∑ i in s, (g i) ^ q,
{ rw [← rpow_mul, one_div_mul_cancel hpq.symm.ne_zero, rpow_one], },
-- First consider the trivial case `∥g∥_q=0`
by_cases hG : G = 0,
{ rw [hG, sum_eq_zero, mul_zero],
intros i hi,
simp only [rpow_eq_zero_iff, sum_eq_zero_iff] at hG,
simp [(hG.1 i hi).1] },
{ -- Move power from right to left
rw [← div_le_iff hG, sum_div],
-- Now the inequality follows from the weighted generalized mean inequality
-- with weights `w_i` and numbers `z_i` given by the following formulas.
set w : ι → ℝ≥0 := λ i, (g i) ^ q / G ^ q,
set z : ι → ℝ≥0 := λ i, f i * (G / g i) ^ (q / p),
-- Show that the sum of weights equals one
have A : ∑ i in s, w i = 1,
{ rw [← sum_div, hGq, div_self],
simpa [rpow_eq_zero_iff, hpq.symm.ne_zero] using hG },
-- LHS of the goal equals LHS of the weighted generalized mean inequality
calc (∑ i in s, f i * g i / G) = (∑ i in s, w i * z i) :
begin
refine sum_congr rfl (λ i hi, _),
have : q - q / p = 1, by field_simp [hpq.ne_zero, hpq.symm.mul_eq_add],
dsimp only [w, z],
rw [← div_rpow, mul_left_comm, mul_div_assoc, ← @inv_div _ _ _ G, inv_rpow,
← div_eq_mul_inv, ← rpow_sub']; simp [this]
end
-- Apply the generalized mean inequality
... ≤ (∑ i in s, w i * (z i) ^ p) ^ (1 / p) :
nnreal.arith_mean_le_rpow_mean s w z A (le_of_lt hpq.one_lt)
-- Simplify the right hand side. Terms with `g i ≠ 0` are equal to `(f i) ^ p`,
-- the others are zeros.
... ≤ (∑ i in s, (f i) ^ p) ^ (1 / p) :
begin
refine rpow_le_rpow (sum_le_sum (λ i hi, _)) hpq.one_div_nonneg,
dsimp only [w, z],
rw [mul_rpow, mul_left_comm, ← rpow_mul _ _ p, div_mul_cancel _ hpq.ne_zero, div_rpow,
div_mul_div, mul_comm (G ^ q), mul_div_mul_right],
{ nth_rewrite 1 [← mul_one ((f i) ^ p)],
exact canonically_ordered_semiring.mul_le_mul (le_refl _) (div_self_le _) },
{ simpa [hpq.symm.ne_zero] using hG }
end }
end
/-- The `L_p` seminorm of a vector `f` is the greatest value of the inner product
`∑ i in s, f i * g i` over functions `g` of `L_q` seminorm less than or equal to one. -/
theorem is_greatest_Lp (f : ι → ℝ≥0) {p q : ℝ} (hpq : p.is_conjugate_exponent q) :
is_greatest ((λ g : ι → ℝ≥0, ∑ i in s, f i * g i) ''
{g | ∑ i in s, (g i)^q ≤ 1}) ((∑ i in s, (f i)^p) ^ (1 / p)) :=
begin
split,
{ use λ i, ((f i) ^ p / f i / (∑ i in s, (f i) ^ p) ^ (1 / q)),
by_cases hf : ∑ i in s, (f i)^p = 0,
{ simp [hf, hpq.ne_zero, hpq.symm.ne_zero] },
{ have A : p + q - q ≠ 0, by simp [hpq.ne_zero],
have B : ∀ y : ℝ≥0, y * y^p / y = y^p,
{ refine λ y, mul_div_cancel_left_of_imp (λ h, _),
simpa [h, hpq.ne_zero] },
simp only [set.mem_set_of_eq, div_rpow, ← sum_div, ← rpow_mul,
div_mul_cancel _ hpq.symm.ne_zero, rpow_one, div_le_iff hf, one_mul, hpq.mul_eq_add,
← rpow_sub' _ A, _root_.add_sub_cancel, le_refl, true_and, ← mul_div_assoc, B],
rw [div_eq_iff, ← rpow_add hf, hpq.inv_add_inv_conj, rpow_one],
simpa [hpq.symm.ne_zero] using hf } },
{ rintros _ ⟨g, hg, rfl⟩,
apply le_trans (inner_le_Lp_mul_Lq s f g hpq),
simpa only [mul_one] using canonically_ordered_semiring.mul_le_mul (le_refl _)
(nnreal.rpow_le_one hg (le_of_lt hpq.symm.one_div_pos)) }
end
/-- Minkowski inequality: the `L_p` seminorm of the sum of two vectors is less than or equal
to the sum of the `L_p`-seminorms of the summands. A version for `nnreal`-valued functions. -/
theorem Lp_add_le (f g : ι → ℝ≥0) {p : ℝ} (hp : 1 ≤ p) :
(∑ i in s, (f i + g i) ^ p) ^ (1 / p) ≤
(∑ i in s, (f i) ^ p) ^ (1 / p) + (∑ i in s, (g i) ^ p) ^ (1 / p) :=
begin
-- The result is trivial when `p = 1`, so we can assume `1 < p`.
rcases eq_or_lt_of_le hp with rfl|hp, { simp [finset.sum_add_distrib] },
have hpq := real.is_conjugate_exponent_conjugate_exponent hp,
have := is_greatest_Lp s (f + g) hpq,
simp only [pi.add_apply, add_mul, sum_add_distrib] at this,
rcases this.1 with ⟨φ, hφ, H⟩,
rw ← H,
exact add_le_add ((is_greatest_Lp s f hpq).2 ⟨φ, hφ, rfl⟩)
((is_greatest_Lp s g hpq).2 ⟨φ, hφ, rfl⟩)
end
end nnreal
namespace real
variables (f g : ι → ℝ) {p q : ℝ}
/-- Hölder inequality: the scalar product of two functions is bounded by the product of their
`L^p` and `L^q` norms when `p` and `q` are conjugate exponents. Version for sums over finite sets,
with real-valued functions. -/
theorem inner_le_Lp_mul_Lq (hpq : is_conjugate_exponent p q) :
∑ i in s, f i * g i ≤ (∑ i in s, (abs $ f i)^p) ^ (1 / p) * (∑ i in s, (abs $ g i)^q) ^ (1 / q) :=
begin
have := nnreal.coe_le_coe.2 (nnreal.inner_le_Lp_mul_Lq s (λ i, ⟨_, abs_nonneg (f i)⟩)
(λ i, ⟨_, abs_nonneg (g i)⟩) hpq),
push_cast at this,
refine le_trans (sum_le_sum $ λ i hi, _) this,
simp only [← abs_mul, le_abs_self]
end
/-- Minkowski inequality: the `L_p` seminorm of the sum of two vectors is less than or equal
to the sum of the `L_p`-seminorms of the summands. A version for `real`-valued functions. -/
theorem Lp_add_le (hp : 1 ≤ p) :
(∑ i in s, (abs $ f i + g i) ^ p) ^ (1 / p) ≤
(∑ i in s, (abs $ f i) ^ p) ^ (1 / p) + (∑ i in s, (abs $ g i) ^ p) ^ (1 / p) :=
begin
have := nnreal.coe_le_coe.2 (nnreal.Lp_add_le s (λ i, ⟨_, abs_nonneg (f i)⟩)
(λ i, ⟨_, abs_nonneg (g i)⟩) hp),
push_cast at this,
refine le_trans (rpow_le_rpow _ (sum_le_sum $ λ i hi, _) _) this;
simp [sum_nonneg, rpow_nonneg_of_nonneg, abs_nonneg, le_trans zero_le_one hp, abs_add,
rpow_le_rpow]
end
variables {f g}
/-- Hölder inequality: the scalar product of two functions is bounded by the product of their
`L^p` and `L^q` norms when `p` and `q` are conjugate exponents. Version for sums over finite sets,
with real-valued nonnegative functions. -/
theorem inner_le_Lp_mul_Lq_of_nonneg (hpq : is_conjugate_exponent p q)
(hf : ∀ i ∈ s, 0 ≤ f i) (hg : ∀ i ∈ s, 0 ≤ g i) :
∑ i in s, f i * g i ≤ (∑ i in s, (f i)^p) ^ (1 / p) * (∑ i in s, (g i)^q) ^ (1 / q) :=
by convert inner_le_Lp_mul_Lq s f g hpq using 3; apply sum_congr rfl; intros i hi;
simp only [abs_of_nonneg, hf i hi, hg i hi]
/-- Minkowski inequality: the `L_p` seminorm of the sum of two vectors is less than or equal
to the sum of the `L_p`-seminorms of the summands. A version for `real`-valued nonnegative
functions. -/
theorem Lp_add_le_of_nonneg (hp : 1 ≤ p) (hf : ∀ i ∈ s, 0 ≤ f i) (hg : ∀ i ∈ s, 0 ≤ g i) :
(∑ i in s, (f i + g i) ^ p) ^ (1 / p) ≤
(∑ i in s, (f i) ^ p) ^ (1 / p) + (∑ i in s, (g i) ^ p) ^ (1 / p) :=
by convert Lp_add_le s f g hp using 2 ; [skip, congr' 1, congr' 1];
apply sum_congr rfl; intros i hi; simp only [abs_of_nonneg, hf i hi, hg i hi, add_nonneg]
end real
namespace ennreal
variables (f g : ι → ennreal) {p q : ℝ}
/-- Hölder inequality: the scalar product of two functions is bounded by the product of their
`L^p` and `L^q` norms when `p` and `q` are conjugate exponents. Version for sums over finite sets,
with `ennreal`-valued functions. -/
theorem inner_le_Lp_mul_Lq (hpq : p.is_conjugate_exponent q) :
(∑ i in s, f i * g i) ≤ (∑ i in s, (f i)^p) ^ (1/p) * (∑ i in s, (g i)^q) ^ (1/q) :=
begin
by_cases H : (∑ i in s, (f i)^p) ^ (1/p) = 0 ∨ (∑ i in s, (g i)^q) ^ (1/q) = 0,
{ replace H : (∀ i ∈ s, f i = 0) ∨ (∀ i ∈ s, g i = 0),
by simpa [ennreal.rpow_eq_zero_iff, hpq.pos, hpq.symm.pos, asymm hpq.pos, asymm hpq.symm.pos,
sum_eq_zero_iff_of_nonneg] using H,
have : ∀ i ∈ s, f i * g i = 0 := λ i hi, by cases H; simp [H i hi],
have : (∑ i in s, f i * g i) = (∑ i in s, 0) := sum_congr rfl this,
simp [this] },
push_neg at H,
by_cases H' : (∑ i in s, (f i)^p) ^ (1/p) = ⊤ ∨ (∑ i in s, (g i)^q) ^ (1/q) = ⊤,
{ cases H'; simp [H', -one_div, H] },
replace H' : (∀ i ∈ s, f i ≠ ⊤) ∧ (∀ i ∈ s, g i ≠ ⊤),
by simpa [ennreal.rpow_eq_top_iff, asymm hpq.pos, asymm hpq.symm.pos, hpq.pos, hpq.symm.pos,
ennreal.sum_eq_top_iff, not_or_distrib] using H',
have := ennreal.coe_le_coe.2 (@nnreal.inner_le_Lp_mul_Lq _ s (λ i, ennreal.to_nnreal (f i))
(λ i, ennreal.to_nnreal (g i)) _ _ hpq),
simp [← ennreal.coe_rpow_of_nonneg, le_of_lt (hpq.pos), le_of_lt (hpq.one_div_pos),
le_of_lt (hpq.symm.pos), le_of_lt (hpq.symm.one_div_pos)] at this,
convert this using 1;
[skip, congr' 2];
[skip, skip, simp, skip, simp];
{ apply finset.sum_congr rfl (λ i hi, _), simp [H'.1 i hi, H'.2 i hi, -with_zero.coe_mul,
with_top.coe_mul.symm] },
end
/-- Minkowski inequality: the `L_p` seminorm of the sum of two vectors is less than or equal
to the sum of the `L_p`-seminorms of the summands. A version for `ennreal` valued nonnegative
functions. -/
theorem Lp_add_le (hp : 1 ≤ p) :
(∑ i in s, (f i + g i) ^ p)^(1/p) ≤ (∑ i in s, (f i)^p) ^ (1/p) + (∑ i in s, (g i)^p) ^ (1/p) :=
begin
by_cases H' : (∑ i in s, (f i)^p) ^ (1/p) = ⊤ ∨ (∑ i in s, (g i)^p) ^ (1/p) = ⊤,
{ cases H'; simp [H', -one_div] },
have pos : 0 < p := lt_of_lt_of_le zero_lt_one hp,
replace H' : (∀ i ∈ s, f i ≠ ⊤) ∧ (∀ i ∈ s, g i ≠ ⊤),
by simpa [ennreal.rpow_eq_top_iff, asymm pos, pos, ennreal.sum_eq_top_iff,
not_or_distrib] using H',
have := ennreal.coe_le_coe.2 (@nnreal.Lp_add_le _ s (λ i, ennreal.to_nnreal (f i))
(λ i, ennreal.to_nnreal (g i)) _ hp),
push_cast [← ennreal.coe_rpow_of_nonneg, le_of_lt (pos), le_of_lt (one_div_pos.2 pos)] at this,
convert this using 2;
[skip, congr' 1, congr' 1];
{ apply finset.sum_congr rfl (λ i hi, _), simp [H'.1 i hi, H'.2 i hi] }
end
end ennreal
|
387d45d6f967826b397ed177af8176ccc8cb8ae6 | 4c421481689d399e1e27a9266c47904d9200d42e | /fib.lean | 201e046492d6a0eee3e06b678a114cbfaaccf5c2 | [] | no_license | ivanmoreau/fib | f1fffe4fef5ba9beae88be3a740c8bcfc95e1e01 | 5d3440b45bb5822c80ebe73b4cbdb05e9506cee2 | refs/heads/master | 1,690,506,253,577 | 1,631,324,182,000 | 1,631,324,182,000 | 405,255,296 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 979 | lean | import tactic.basic
import tactic.congr
import init.data.nat
/- Nat minus, where 0 - n = 0 -/
def ominus : ℕ → ℕ → ℕ
| 0 m := 0
| n 0 := n
| (n + 1) (m + 1) := ominus n m
infix ` ⊖ `:60 := ominus
def fib_tail : ℕ → ℕ → ℕ → ℕ × ℕ
| 0 i j := (i , j)
| (s + 1) i j := fib_tail s j (i + j)
--def fib : ℕ → ℕ
-- | n := let (a , b) := fib_tail n 1 0 in b
def fib : ℕ → ℕ × ℕ
| 0 := (0, 0)
| k := fib_tail k 1 0
#eval fib 8
#eval fib (0 ⊖ 1)
#eval fib 0
/- Demuestra por inducción matemática que los valores de $i$ y $j$ al final
de la $k$-ésima iteración de Fibiter son $f_{k−1}$ y $f_k$, respectivamente,
en donde $f_n$ denota el $n$-ésimo numero de Fibonacci. -/
theorem fib_iter_l : ∀ (n : ℕ),
(fib n).1 = (fib (n ⊖ 1)).2 := by
begin
intro,
induction n,
refl,
suggest,
end
/-
theorem fib_iter_k : ∀ (n : ℕ),
fib n = ((fib (n ⊖ 1)).2 , (fib (n)).2)
| 0 := rfl
| (n + 1) := -/
|
e1341be4fe4f2287bbf97802c11987c8aa49294b | 6e36ebd5594a0d512dea8bc6ffe78c71b5b5032d | /src/mywork/Practice/review_9_13_21.lean | 3e4eda94bc025a0a95c4cc7e855f722949d2181c | [] | no_license | wrw2ztk/cs2120f21 | cdc4b1b4043c8ae8f3c8c3c0e91cdacb2cfddb16 | f55df4c723d3ce989908679f5653e4be669334ae | refs/heads/main | 1,691,764,473,342 | 1,633,707,809,000 | 1,633,707,809,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 811 | lean | namespace implies
axioms (P Q : Prop)
/-P and Q are arbitrary propositions, they could have more complex names -/
def if_P_is_true_then_so_is_Q : Prop := P → Q
axiom p : P
--assume P is true
--assume we have a proof of P (p)
axiom pq : P → Q
--assume that we have a proof, pq, of P → Q
-- intro for implies: assume premise, show conclusion
-- elimination rile for implies:
#check pq
/-
Suppose P and Q are propositions and you know that P → Q and that
P are both true. Show that Q is true.
Proof: Apply the truth of P → Q to the truth of P
to derive the truth of Q.
Proof: By the elimination rule for → with pq applied to p.
Proof: By "modus ponens". QED.
-/
end implies
/-
FOR ALL
-/
namespace all
axioms
(T : Type)
(P : T → Prop)
(t : T)
(a : ∀ (x : T), P x)
end all |
f0817d07e73990b5cc31f48de0c0d2895893f4db | cfa891621a41d154f396f5f38cbe2eb7e34c3f62 | /06-formalizacija-dokazov/lambda.lean | f2f77d13104d8516ae00a1575f92208d6762bde2 | [] | no_license | anastern881/teorija-programskih-jezikov | 40b247a26b12a3cbb9cc9aa4efaf30a17bb085c7 | dc5cc9c4a0265bd2ea32bb7c44bb62eb1b6802ab | refs/heads/master | 1,599,914,547,365 | 1,574,072,468,000 | 1,574,072,468,000 | 222,429,267 | 0 | 0 | null | 1,574,075,186,000 | 1,574,075,185,000 | null | UTF-8 | Lean | false | false | 6,130 | lean | inductive ty : Type
| unit : ty
| bool : ty
| arrow : ty -> ty -> ty
def nm := string
inductive tm : Type
| var : nm -> tm
| unit : tm
| true : tm
| false : tm
| app : tm -> tm -> tm
| lam : nm -> tm -> tm
| if_then_else : tm -> tm -> tm -> tm
inductive value : tm -> Prop
| unit : value tm.unit
| true : value tm.true
| false : value tm.false
| lam {x : nm} {N : tm} : value (tm.lam x N)
def subst (x : nm) (M : tm) : tm -> tm
| (tm.var y) := if x = y then M else tm.var y
| tm.unit := tm.unit
| tm.true := tm.true
| tm.false := tm.false
| (tm.app N1 N2) := tm.app (subst N1) (subst N2)
| (tm.lam y N) := if x = y then tm.lam y N else tm.lam y (subst N)
| (tm.if_then_else N N1 N2) := tm.if_then_else (subst N) (subst N1) (subst N2)
inductive step : tm -> tm -> Prop
| step1 {M M' N : tm} : step M M' -> step (tm.app M N) (tm.app M' N)
| step2 {V N N' : tm} : value V -> step N N' -> step (tm.app V N) (tm.app V N')
| beta {x} {M V : tm} : value V -> step (tm.app (tm.lam x M) V) (subst x V M)
| test {M M' N1 N2 : tm} : step M M' -> step (tm.if_then_else M N1 N2) (tm.if_then_else M' N1 N2)
| true {N1 N2 : tm} : step (tm.if_then_else tm.true N1 N2) N1
| false {N1 N2 : tm} : step (tm.if_then_else tm.false N1 N2) N2
inductive ctx : Type
| nil : ctx
| cons : nm -> ty -> ctx -> ctx
inductive lookup : nm -> ctx -> ty -> Prop
| here {x A Γ} : lookup x (ctx.cons x A Γ) A
| there {x y : nm} {A B : ty} {Γ : ctx} : x ≠ y -> lookup x Γ A -> lookup x (ctx.cons y B Γ) A
inductive of : ctx -> tm -> ty -> Prop
| var {x Γ A}: lookup x Γ A -> of Γ (tm.var x) A
| unit {Γ}: of Γ tm.unit ty.unit
| true {Γ}: of Γ tm.true ty.bool
| false {Γ}: of Γ tm.false ty.bool
| app {Γ M N A B} : of Γ M (ty.arrow A B) -> of Γ N A -> of Γ (tm.app M N) B
| lam {Γ x M A B} : of (ctx.cons x A Γ) M B -> of Γ (tm.lam x M) (ty.arrow A B)
| if_then_else {Γ M N1 N2 A} : of Γ M ty.bool -> of Γ N1 A -> of Γ N2 A -> of Γ (tm.if_then_else M N1 N2) A
theorem substitution (Γ x A M M' A') : of Γ M A -> of (ctx.cons x A Γ) M' A' -> of Γ (subst x M M') A' :=
begin
intros H,
generalize ctx_cons : (ctx.cons x A Γ) = Γ',
intros H',
induction H',
case of.var {
rewrite <- ctx_cons at H'_a,
unfold subst,
cases H'_a,
case lookup.here {
simp,
assumption
},
case lookup.there {
by_cases (x = H'_x),
have H := (ne.symm H'_a_a), contradiction,
simp [h], apply of.var, assumption
}
},
case of.unit {
unfold subst,
apply of.unit
},
case of.true {
unfold subst,
apply of.true
},
case of.false {
unfold subst,
apply of.false
},
case of.app {
unfold subst,
apply of.app,
apply H'_ih_a ctx_cons,
apply H'_ih_a_1 ctx_cons,
},
case of.if_then_else {
unfold subst,
apply of.if_then_else,
apply H'_ih_a ctx_cons,
apply H'_ih_a_1 ctx_cons,
apply H'_ih_a_2 ctx_cons,
},
case of.lam {
unfold subst,
by_cases (x = H'_x),
simp [h], apply of.lam, sorry,
simp [h], apply of.lam, sorry,
},
end
theorem preservation (Γ M M') : step M M' -> forall A, of Γ M A -> of Γ M' A :=
begin
intros Hstep,
induction Hstep,
repeat {intros A Hof},
case step.beta
{cases Hof, cases Hof_a, apply substitution _ _ _ _ _ _ Hof_a_1 Hof_a_a },
case step.step1
{cases Hof, apply of.app,
apply Hstep_ih _ Hof_a, apply Hof_a_1},
case step.step2
{cases Hof, apply of.app, apply Hof_a, apply Hstep_ih _ Hof_a_1},
case step.test {
cases Hof,
apply of.if_then_else,
apply Hstep_ih _ Hof_a,
apply Hof_a_1,
apply Hof_a_2
},
case step.true {
cases Hof,
apply Hof_a_1
},
case step.false {
cases Hof,
apply Hof_a_2
}
end
theorem progress (M A) : of ctx.nil M A -> (value M) ∨ (exists M', step M M') :=
begin
generalize empty : ctx.nil = Γ,
intros H,
induction H,
case of.var
{rewrite ←empty at H_a, cases H_a},
case of.unit
{left, exact value.unit},
case of.app
{
cases H_ih_a empty,
case or.inl {
cases H_a,
case of.var
{rw ←empty at H_a_a, cases H_a_a},
case of.app
{cases h},
case of.lam {
cases H_ih_a_1 empty,
right,
existsi (subst H_a_x H_N H_a_M),
apply step.beta,
assumption,
right,
cases h_1,
existsi (tm.app (tm.lam H_a_x H_a_M) h_1_w),
eapply step.step2,
exact value.lam,
assumption
},
case of.if_then_else {
cases h
}
},
case or.inr {
cases h with M H_step,
right,
existsi (tm.app M H_N),
apply step.step1,
assumption
}
},
case of.lam
{left, exact value.lam},
case of.true
{left, exact value.true},
case of.false
{left, exact value.false},
case of.if_then_else {
cases H_ih_a empty,
case or.inl {
cases H_a,
case of.var
{rw ←empty at H_a_a, cases H_a_a},
case of.true {
right, existsi H_N1, exact step.true
},
case of.false {
right, existsi H_N2, exact step.false
},
cases h,
cases h
},
case or.inr {
cases h,
right,
existsi (tm.if_then_else h_w H_N1 H_N2),
exact (step.test h_h),
}
}
end
|
2adf9c0319ca5cfa8ff88e9d23706973cded3a6f | 0d5fb6eeedc300fb6a7e26c860869d68d1aa7761 | /final/proofs.lean | 7bf781f9b712f081a80827948a3d442ae46ee1d4 | [] | no_license | FlyNeopolitan/CS242ProgrammingLanguage | 95e0cf82b9248d7376aa9a838d9cc4b93f223bf2 | 1ab4a24396afbc2207b421f1a601e5056c39fa20 | refs/heads/master | 1,685,591,558,205 | 1,624,171,234,000 | 1,624,171,234,000 | 371,285,585 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,240 | lean | -- Required: chapters 1, 2, 3
section part1
variables p q r s : Prop
theorem and_commutativity : p ∧ q ↔ q ∧ p :=
sorry
theorem demorgans_law : ¬(p ∨ q) ↔ ¬p ∧ ¬q :=
sorry
theorem contraposition : (p → q) → (¬q → ¬p) :=
sorry
end part1
-- Required: chapters 1, 2, 3, 4.1
section part2
variables (α : Type) (p q : α → Prop)
variable r : Prop
theorem and_forall_dist : (∀ x, p x ∧ q x) ↔ (∀ x, p x) ∧ (∀ x, q x) :=
sorry
theorem unbound_prop : (∀ x, p x ∨ r) ↔ (∀ x, p x) ∨ r :=
sorry
variables (men : Type) (barber : men)
variable (shaves : men → men → Prop)
theorem barber_paradox : ¬ (∀ x : men, shaves barber x ↔ ¬ shaves x x) :=
sorry
end part2
-- Required: chapters 1, 2, 3, 4.1, 4.4
section part4
open classical
variables (α : Type) (p q : α → Prop)
variable a : α
variable r : Prop
theorem forall_inverse : (∀ x, p x) ↔ ¬ (∃ x, ¬ p x) :=
sorry
end part4
-- Required: chapters 1, 2, 3, 4
section part5
variables (real : Type) [ordered_ring real]
variables (log exp : real → real)
variable log_exp_eq : ∀ x, log (exp x) = x
variable exp_log_eq : ∀ {x}, x > 0 → exp (log x) = x
variable exp_pos : ∀ x, exp x > 0
variable exp_add : ∀ x y, exp (x + y) = exp x * exp y
-- this ensures the assumptions are available in tactic proofs
include log_exp_eq exp_log_eq exp_pos exp_add
example (x y z : real) :
exp (x + y + z) = exp x * exp y * exp z :=
by rw [exp_add, exp_add]
example (y : real) (h : y > 0) : exp (log y) = y :=
exp_log_eq h
theorem log_mul {x y : real} (hx : x > 0) (hy : y > 0) :
log (x * y) = log x + log y :=
sorry
end part5
-- Required: chapters 1, 2, 3, 4, 7
section part6
inductive Typ
| Nat
| Fun : Typ → Typ → Typ
inductive Expr
| Nat : ℕ → Expr
| Add : Expr → Expr → Expr
| Lam : Typ → Expr → Expr
| App : Expr -> Expr -> Expr
| Var : ℕ → Expr
def ProofContext := list Typ
variable has_type : ProofContext → Expr → Typ → Prop
variable t_nat : ∀ (gamma : ProofContext), ∀ (n : ℕ),
has_type gamma (Expr.Nat n) Typ.Nat
variable t_add : ∀ (gamma : ProofContext), ∀ (e1 : Expr), ∀ (e2 : Expr),
(has_type gamma e1 Typ.Nat) ∧ (has_type gamma e2 Typ.Nat) →
has_type gamma (Expr.Add e1 e2) Typ.Nat
variable t_lam : ∀ (gamma : ProofContext), ∀ (e : Expr), ∀ (t_arg : Typ), ∀ (t_ret : Typ),
has_type (t_arg :: gamma) e t_ret →
has_type gamma (Expr.Lam t_arg e) (Typ.Fun t_arg t_ret)
variable t_app : ∀ (gamma : ProofContext), ∀ (e1 : Expr), ∀ (e2 : Expr), ∀ (t_arg : Typ), ∀ (t_ret : Typ),
has_type gamma e1 (Typ.Fun t_arg t_ret) ∧ has_type gamma e2 t_arg →
has_type gamma (Expr.App e1 e2) t_ret
variable t_var : ∀ (gamma : ProofContext), ∀ (t : Typ), ∀ (n : ℕ),
gamma.nth n = some t → has_type gamma (Expr.Var n) t
def e1 : Expr := (Expr.Add (Expr.Nat 3) (Expr.Nat 2))
theorem e1_type : has_type [] e1 Typ.Nat :=
sorry
def e2 : Expr := Expr.Lam Typ.Nat (Expr.Var 0)
theorem e2_type : has_type [] e2 (Typ.Fun Typ.Nat Typ.Nat) :=
sorry
end part6
|
5e0f951d0cab0007da352bb2aeff51715eb70d65 | 6ae186a0c6ab366b39397ec9250541c9d5aeb023 | /src/category_theory/rel.lean | 7db7c9da29275f7a4dedcdc5569d4ab515a5a1e2 | [] | no_license | ThanhPhamPhuong/lean-category-theory | 0d5c4fe1137866b4fe29ec2753d99aa0d0667881 | 968a29fe7c0b20e10d8a27e120aca8ddc184e1ea | refs/heads/master | 1,587,206,682,489 | 1,544,045,056,000 | 1,544,045,056,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 249 | lean | import category_theory.category
namespace category_theory
universe u
def Rel : large_category (Type u) :=
{ hom := λ X Y, X → Y → Prop,
id := λ X, λ x y, x = y,
comp := λ X Y Z f g x z, ∃ y, f x y ∧ g y z }
end category_theory |
accbf17bea42d615478527e11f31aacb2df3b016 | 4d2583807a5ac6caaffd3d7a5f646d61ca85d532 | /src/data/nat/prime.lean | b716196530076295754bc7d4466ae6a1aad3785b | [
"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 | 38,039 | lean | /-
Copyright (c) 2015 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura, Jeremy Avigad, Mario Carneiro
-/
import algebra.group_power
import data.list.sort
import data.nat.gcd
import data.nat.sqrt
import tactic.norm_num
import tactic.wlog
/-!
# Prime numbers
This file deals with prime numbers: natural numbers `p ≥ 2` whose only divisors are `p` and `1`.
## Important declarations
All the following declarations exist in the namespace `nat`.
- `prime`: the predicate that expresses that a natural number `p` is prime
- `primes`: the subtype of natural numbers that are prime
- `min_fac n`: the minimal prime factor of a natural number `n ≠ 1`
- `exists_infinite_primes`: Euclid's theorem that there exist infinitely many prime numbers
- `factors n`: the prime factorization of `n`
- `factors_unique`: uniqueness of the prime factorisation
-/
open bool subtype
open_locale nat
namespace nat
/-- `prime p` means that `p` is a prime number, that is, a natural number
at least 2 whose only divisors are `p` and `1`. -/
@[pp_nodot]
def prime (p : ℕ) := 2 ≤ p ∧ ∀ m ∣ p, m = 1 ∨ m = p
theorem prime.two_le {p : ℕ} : prime p → 2 ≤ p := and.left
theorem prime.one_lt {p : ℕ} : prime p → 1 < p := prime.two_le
instance prime.one_lt' (p : ℕ) [hp : _root_.fact p.prime] : _root_.fact (1 < p) := ⟨hp.1.one_lt⟩
lemma prime.ne_one {p : ℕ} (hp : p.prime) : p ≠ 1 :=
ne.symm $ ne_of_lt hp.one_lt
theorem prime_def_lt {p : ℕ} : prime p ↔ 2 ≤ p ∧ ∀ m < p, m ∣ p → m = 1 :=
and_congr_right $ λ p2, forall_congr $ λ m,
⟨λ h l d, (h d).resolve_right (ne_of_lt l),
λ h d, (le_of_dvd (le_of_succ_le p2) d).lt_or_eq_dec.imp_left (λ l, h l d)⟩
theorem prime_def_lt' {p : ℕ} : prime p ↔ 2 ≤ p ∧ ∀ m, 2 ≤ m → m < p → ¬ m ∣ p :=
prime_def_lt.trans $ and_congr_right $ λ p2, forall_congr $ λ m,
⟨λ h m2 l d, not_lt_of_ge m2 ((h l d).symm ▸ dec_trivial),
λ h l d, begin
rcases m with _|_|m,
{ rw eq_zero_of_zero_dvd d at p2, revert p2, exact dec_trivial },
{ refl },
{ exact (h dec_trivial l).elim d }
end⟩
theorem prime_def_le_sqrt {p : ℕ} : prime p ↔ 2 ≤ p ∧
∀ m, 2 ≤ m → m ≤ sqrt p → ¬ m ∣ p :=
prime_def_lt'.trans $ and_congr_right $ λ p2,
⟨λ a m m2 l, a m m2 $ lt_of_le_of_lt l $ sqrt_lt_self p2,
λ a, have ∀ {m k}, m ≤ k → 1 < m → p ≠ m * k, from
λ m k mk m1 e, a m m1
(le_sqrt.2 (e.symm ▸ nat.mul_le_mul_left m mk)) ⟨k, e⟩,
λ m m2 l ⟨k, e⟩, begin
cases (le_total m k) with mk km,
{ exact this mk m2 e },
{ rw [mul_comm] at e,
refine this km (lt_of_mul_lt_mul_right _ (zero_le m)) e,
rwa [one_mul, ← e] }
end⟩
theorem prime_of_coprime (n : ℕ) (h1 : 1 < n) (h : ∀ m < n, m ≠ 0 → n.coprime m) : prime n :=
begin
refine prime_def_lt.mpr ⟨h1, λ m mlt mdvd, _⟩,
have hm : m ≠ 0,
{ rintro rfl,
rw zero_dvd_iff at mdvd,
exact mlt.ne' mdvd },
exact (h m mlt hm).symm.eq_one_of_dvd mdvd,
end
section
/--
This instance is slower than the instance `decidable_prime` defined below,
but has the advantage that it works in the kernel for small values.
If you need to prove that a particular number is prime, in any case
you should not use `dec_trivial`, but rather `by norm_num`, which is
much faster.
-/
local attribute [instance]
def decidable_prime_1 (p : ℕ) : decidable (prime p) :=
decidable_of_iff' _ prime_def_lt'
lemma prime.ne_zero {n : ℕ} (h : prime n) : n ≠ 0 :=
by { rintro rfl, revert h, dec_trivial }
theorem prime.pos {p : ℕ} (pp : prime p) : 0 < p :=
lt_of_succ_lt pp.one_lt
theorem not_prime_zero : ¬ prime 0 := by simp [prime]
theorem not_prime_one : ¬ prime 1 := by simp [prime]
theorem prime_two : prime 2 := dec_trivial
end
theorem prime.pred_pos {p : ℕ} (pp : prime p) : 0 < pred p :=
lt_pred_iff.2 pp.one_lt
theorem succ_pred_prime {p : ℕ} (pp : prime p) : succ (pred p) = p :=
succ_pred_eq_of_pos pp.pos
theorem dvd_prime {p m : ℕ} (pp : prime p) : m ∣ p ↔ m = 1 ∨ m = p :=
⟨λ d, pp.2 m d, λ h, h.elim (λ e, e.symm ▸ one_dvd _) (λ e, e.symm ▸ dvd_rfl)⟩
theorem dvd_prime_two_le {p m : ℕ} (pp : prime p) (H : 2 ≤ m) : m ∣ p ↔ m = p :=
(dvd_prime pp).trans $ or_iff_right_of_imp $ not.elim $ ne_of_gt H
theorem prime_dvd_prime_iff_eq {p q : ℕ} (pp : p.prime) (qp : q.prime) : p ∣ q ↔ p = q :=
dvd_prime_two_le qp (prime.two_le pp)
theorem prime.not_dvd_one {p : ℕ} (pp : prime p) : ¬ p ∣ 1
| d := (not_le_of_gt pp.one_lt) $ le_of_dvd dec_trivial d
theorem not_prime_mul {a b : ℕ} (a1 : 1 < a) (b1 : 1 < b) : ¬ prime (a * b) :=
λ h, ne_of_lt (nat.mul_lt_mul_of_pos_left b1 (lt_of_succ_lt a1)) $
by simpa using (dvd_prime_two_le h a1).1 (dvd_mul_right _ _)
lemma not_prime_mul' {a b n : ℕ} (h : a * b = n) (h₁ : 1 < a) (h₂ : 1 < b) : ¬ prime n :=
by { rw ← h, exact not_prime_mul h₁ h₂ }
section min_fac
private lemma min_fac_lemma (n k : ℕ) (h : ¬ n < k * k) :
sqrt n - k < sqrt n + 2 - k :=
(tsub_lt_tsub_iff_right $ le_sqrt.2 $ le_of_not_gt h).2 $
nat.lt_add_of_pos_right dec_trivial
/-- If `n < k * k`, then `min_fac_aux n k = n`, if `k | n`, then `min_fac_aux n k = k`.
Otherwise, `min_fac_aux n k = min_fac_aux n (k+2)` using well-founded recursion.
If `n` is odd and `1 < n`, then then `min_fac_aux n 3` is the smallest prime factor of `n`. -/
def min_fac_aux (n : ℕ) : ℕ → ℕ | k :=
if h : n < k * k then n else
if k ∣ n then k else
have _, from min_fac_lemma n k h,
min_fac_aux (k + 2)
using_well_founded {rel_tac :=
λ _ _, `[exact ⟨_, measure_wf (λ k, sqrt n + 2 - k)⟩]}
/-- Returns the smallest prime factor of `n ≠ 1`. -/
def min_fac : ℕ → ℕ
| 0 := 2
| 1 := 1
| (n+2) := if 2 ∣ n then 2 else min_fac_aux (n + 2) 3
@[simp] theorem min_fac_zero : min_fac 0 = 2 := rfl
@[simp] theorem min_fac_one : min_fac 1 = 1 := rfl
theorem min_fac_eq : ∀ n, min_fac n = if 2 ∣ n then 2 else min_fac_aux n 3
| 0 := by simp
| 1 := by simp [show 2≠1, from dec_trivial]; rw min_fac_aux; refl
| (n+2) :=
have 2 ∣ n + 2 ↔ 2 ∣ n, from
(nat.dvd_add_iff_left (by refl)).symm,
by simp [min_fac, this]; congr
private def min_fac_prop (n k : ℕ) :=
2 ≤ k ∧ k ∣ n ∧ ∀ m, 2 ≤ m → m ∣ n → k ≤ m
theorem min_fac_aux_has_prop {n : ℕ} (n2 : 2 ≤ n) (nd2 : ¬ 2 ∣ n) :
∀ k i, k = 2*i+3 → (∀ m, 2 ≤ m → m ∣ n → k ≤ m) → min_fac_prop n (min_fac_aux n k)
| k := λ i e a, begin
rw min_fac_aux,
by_cases h : n < k*k; simp [h],
{ have pp : prime n :=
prime_def_le_sqrt.2 ⟨n2, λ m m2 l d,
not_lt_of_ge l $ lt_of_lt_of_le (sqrt_lt.2 h) (a m m2 d)⟩,
from ⟨n2, dvd_rfl, λ m m2 d, le_of_eq
((dvd_prime_two_le pp m2).1 d).symm⟩ },
have k2 : 2 ≤ k, { subst e, exact dec_trivial },
by_cases dk : k ∣ n; simp [dk],
{ exact ⟨k2, dk, a⟩ },
{ refine have _, from min_fac_lemma n k h,
min_fac_aux_has_prop (k+2) (i+1)
(by simp [e, left_distrib]) (λ m m2 d, _),
cases nat.eq_or_lt_of_le (a m m2 d) with me ml,
{ subst me, contradiction },
apply (nat.eq_or_lt_of_le ml).resolve_left, intro me,
rw [← me, e] at d, change 2 * (i + 2) ∣ n at d,
have := dvd_of_mul_right_dvd d, contradiction }
end
using_well_founded {rel_tac :=
λ _ _, `[exact ⟨_, measure_wf (λ k, sqrt n + 2 - k)⟩]}
theorem min_fac_has_prop {n : ℕ} (n1 : n ≠ 1) :
min_fac_prop n (min_fac n) :=
begin
by_cases n0 : n = 0, {simp [n0, min_fac_prop, ge]},
have n2 : 2 ≤ n, { revert n0 n1, rcases n with _|_|_; exact dec_trivial },
simp [min_fac_eq],
by_cases d2 : 2 ∣ n; simp [d2],
{ exact ⟨le_refl _, d2, λ k k2 d, k2⟩ },
{ refine min_fac_aux_has_prop n2 d2 3 0 rfl
(λ m m2 d, (nat.eq_or_lt_of_le m2).resolve_left (mt _ d2)),
exact λ e, e.symm ▸ d }
end
theorem min_fac_dvd (n : ℕ) : min_fac n ∣ n :=
if n1 : n = 1 then by simp [n1] else (min_fac_has_prop n1).2.1
theorem min_fac_prime {n : ℕ} (n1 : n ≠ 1) : prime (min_fac n) :=
let ⟨f2, fd, a⟩ := min_fac_has_prop n1 in
prime_def_lt'.2 ⟨f2, λ m m2 l d, not_le_of_gt l (a m m2 (d.trans fd))⟩
theorem min_fac_le_of_dvd {n : ℕ} : ∀ {m : ℕ}, 2 ≤ m → m ∣ n → min_fac n ≤ m :=
by by_cases n1 : n = 1;
[exact λ m m2 d, n1.symm ▸ le_trans dec_trivial m2,
exact (min_fac_has_prop n1).2.2]
theorem min_fac_pos (n : ℕ) : 0 < min_fac n :=
by by_cases n1 : n = 1;
[exact n1.symm ▸ dec_trivial, exact (min_fac_prime n1).pos]
theorem min_fac_le {n : ℕ} (H : 0 < n) : min_fac n ≤ n :=
le_of_dvd H (min_fac_dvd n)
theorem le_min_fac {m n : ℕ} : n = 1 ∨ m ≤ min_fac n ↔ ∀ p, prime p → p ∣ n → m ≤ p :=
⟨λ h p pp d, h.elim
(by rintro rfl; cases pp.not_dvd_one d)
(λ h, le_trans h $ min_fac_le_of_dvd pp.two_le d),
λ H, or_iff_not_imp_left.2 $ λ n1, H _ (min_fac_prime n1) (min_fac_dvd _)⟩
theorem le_min_fac' {m n : ℕ} : n = 1 ∨ m ≤ min_fac n ↔ ∀ p, 2 ≤ p → p ∣ n → m ≤ p :=
⟨λ h p (pp:1<p) d, h.elim
(by rintro rfl; cases not_le_of_lt pp (le_of_dvd dec_trivial d))
(λ h, le_trans h $ min_fac_le_of_dvd pp d),
λ H, le_min_fac.2 (λ p pp d, H p pp.two_le d)⟩
theorem prime_def_min_fac {p : ℕ} : prime p ↔ 2 ≤ p ∧ min_fac p = p :=
⟨λ pp, ⟨pp.two_le,
let ⟨f2, fd, a⟩ := min_fac_has_prop $ ne_of_gt pp.one_lt in
((dvd_prime pp).1 fd).resolve_left (ne_of_gt f2)⟩,
λ ⟨p2, e⟩, e ▸ min_fac_prime (ne_of_gt p2)⟩
/--
This instance is faster in the virtual machine than `decidable_prime_1`,
but slower in the kernel.
If you need to prove that a particular number is prime, in any case
you should not use `dec_trivial`, but rather `by norm_num`, which is
much faster.
-/
instance decidable_prime (p : ℕ) : decidable (prime p) :=
decidable_of_iff' _ prime_def_min_fac
theorem not_prime_iff_min_fac_lt {n : ℕ} (n2 : 2 ≤ n) : ¬ prime n ↔ min_fac n < n :=
(not_congr $ prime_def_min_fac.trans $ and_iff_right n2).trans $
(lt_iff_le_and_ne.trans $ and_iff_right $ min_fac_le $ le_of_succ_le n2).symm
lemma min_fac_le_div {n : ℕ} (pos : 0 < n) (np : ¬ prime n) : min_fac n ≤ n / min_fac n :=
match min_fac_dvd n with
| ⟨0, h0⟩ := absurd pos $ by rw [h0, mul_zero]; exact dec_trivial
| ⟨1, h1⟩ :=
begin
rw mul_one at h1,
rw [prime_def_min_fac, not_and_distrib, ← h1, eq_self_iff_true, not_true, or_false,
not_le] at np,
rw [le_antisymm (le_of_lt_succ np) (succ_le_of_lt pos), min_fac_one, nat.div_one]
end
| ⟨(x+2), hx⟩ :=
begin
conv_rhs { congr, rw hx },
rw [nat.mul_div_cancel_left _ (min_fac_pos _)],
exact min_fac_le_of_dvd dec_trivial ⟨min_fac n, by rwa mul_comm⟩
end
end
/--
The square of the smallest prime factor of a composite number `n` is at most `n`.
-/
lemma min_fac_sq_le_self {n : ℕ} (w : 0 < n) (h : ¬ prime n) : (min_fac n)^2 ≤ n :=
have t : (min_fac n) ≤ (n/min_fac n) := min_fac_le_div w h,
calc
(min_fac n)^2 = (min_fac n) * (min_fac n) : sq (min_fac n)
... ≤ (n/min_fac n) * (min_fac n) : nat.mul_le_mul_right (min_fac n) t
... ≤ n : div_mul_le_self n (min_fac n)
@[simp]
lemma min_fac_eq_one_iff {n : ℕ} : min_fac n = 1 ↔ n = 1 :=
begin
split,
{ intro h,
by_contradiction hn,
have := min_fac_prime hn,
rw h at this,
exact not_prime_one this, },
{ rintro rfl, refl, }
end
@[simp]
lemma min_fac_eq_two_iff (n : ℕ) : min_fac n = 2 ↔ 2 ∣ n :=
begin
split,
{ intro h,
convert min_fac_dvd _,
rw h, },
{ intro h,
have ub := min_fac_le_of_dvd (le_refl 2) h,
have lb := min_fac_pos n,
-- If `interval_cases` and `norm_num` were already available here,
-- this would be easy and pleasant.
-- But they aren't, so it isn't.
cases h : n.min_fac with m,
{ rw h at lb, cases lb, },
{ cases m with m,
{ simp at h, subst h, cases h with n h, cases n; cases h, },
{ cases m with m,
{ refl, },
{ rw h at ub,
cases ub with _ ub, cases ub with _ ub, cases ub, } } } }
end
end min_fac
theorem exists_dvd_of_not_prime {n : ℕ} (n2 : 2 ≤ n) (np : ¬ prime n) :
∃ m, m ∣ n ∧ m ≠ 1 ∧ m ≠ n :=
⟨min_fac n, min_fac_dvd _, ne_of_gt (min_fac_prime (ne_of_gt n2)).one_lt,
ne_of_lt $ (not_prime_iff_min_fac_lt n2).1 np⟩
theorem exists_dvd_of_not_prime2 {n : ℕ} (n2 : 2 ≤ n) (np : ¬ prime n) :
∃ m, m ∣ n ∧ 2 ≤ m ∧ m < n :=
⟨min_fac n, min_fac_dvd _, (min_fac_prime (ne_of_gt n2)).two_le,
(not_prime_iff_min_fac_lt n2).1 np⟩
theorem exists_prime_and_dvd {n : ℕ} (n2 : 2 ≤ n) : ∃ p, prime p ∧ p ∣ n :=
⟨min_fac n, min_fac_prime (ne_of_gt n2), min_fac_dvd _⟩
/-- Euclid's theorem on the **infinitude of primes**.
Here given in the form: for every `n`, there exists a prime number `p ≥ n`. -/
theorem exists_infinite_primes (n : ℕ) : ∃ p, n ≤ p ∧ prime p :=
let p := min_fac (n! + 1) in
have f1 : n! + 1 ≠ 1, from ne_of_gt $ succ_lt_succ $ factorial_pos _,
have pp : prime p, from min_fac_prime f1,
have np : n ≤ p, from le_of_not_ge $ λ h,
have h₁ : p ∣ n!, from dvd_factorial (min_fac_pos _) h,
have h₂ : p ∣ 1, from (nat.dvd_add_iff_right h₁).2 (min_fac_dvd _),
pp.not_dvd_one h₂,
⟨p, np, pp⟩
lemma prime.eq_two_or_odd {p : ℕ} (hp : prime p) : p = 2 ∨ p % 2 = 1 :=
(nat.mod_two_eq_zero_or_one p).elim
(λ h, or.inl ((hp.2 2 (dvd_of_mod_eq_zero h)).resolve_left dec_trivial).symm)
or.inr
theorem coprime_of_dvd {m n : ℕ} (H : ∀ k, prime k → k ∣ m → ¬ k ∣ n) : coprime m n :=
begin
cases nat.eq_zero_or_pos (gcd m n) with g0 g1,
{ rw [eq_zero_of_gcd_eq_zero_left g0, eq_zero_of_gcd_eq_zero_right g0] at H,
exfalso,
exact H 2 prime_two (dvd_zero _) (dvd_zero _) },
apply eq.symm,
change 1 ≤ _ at g1,
apply (lt_or_eq_of_le g1).resolve_left,
intro g2,
obtain ⟨p, hp, hpdvd⟩ := exists_prime_and_dvd g2,
apply H p hp; apply dvd_trans hpdvd,
{ exact gcd_dvd_left _ _ },
{ exact gcd_dvd_right _ _ }
end
theorem coprime_of_dvd' {m n : ℕ} (H : ∀ k, prime k → k ∣ m → k ∣ n → k ∣ 1) : coprime m n :=
coprime_of_dvd $ λk kp km kn, not_le_of_gt kp.one_lt $ le_of_dvd zero_lt_one $ H k kp km kn
theorem factors_lemma {k} : (k+2) / min_fac (k+2) < k+2 :=
div_lt_self dec_trivial (min_fac_prime dec_trivial).one_lt
/-- `factors n` is the prime factorization of `n`, listed in increasing order. -/
def factors : ℕ → list ℕ
| 0 := []
| 1 := []
| n@(k+2) :=
let m := min_fac n in have n / m < n := factors_lemma,
m :: factors (n / m)
@[simp] lemma factors_zero : factors 0 = [] := by rw factors
@[simp] lemma factors_one : factors 1 = [] := by rw factors
lemma prime_of_mem_factors : ∀ {n p}, p ∈ factors n → prime p
| 0 := by simp
| 1 := by simp
| n@(k+2) := λ p h,
let m := min_fac n in have n / m < n := factors_lemma,
have h₁ : p = m ∨ p ∈ (factors (n / m)) :=
(list.mem_cons_iff _ _ _).1 (by rwa [factors] at h),
or.cases_on h₁ (λ h₂, h₂.symm ▸ min_fac_prime dec_trivial)
prime_of_mem_factors
lemma prod_factors : ∀ {n}, 0 < n → list.prod (factors n) = n
| 0 := by simp
| 1 := by simp
| n@(k+2) := λ h,
let m := min_fac n in have n / m < n := factors_lemma,
show (factors n).prod = n, from
have h₁ : 0 < n / m :=
nat.pos_of_ne_zero $ λ h,
have n = 0 * m := (nat.div_eq_iff_eq_mul_left (min_fac_pos _) (min_fac_dvd _)).1 h,
by rw zero_mul at this; exact (show k + 2 ≠ 0, from dec_trivial) this,
by rw [factors, list.prod_cons, prod_factors h₁, nat.mul_div_cancel' (min_fac_dvd _)]
lemma factors_prime {p : ℕ} (hp : nat.prime p) : p.factors = [p] :=
begin
have : p = (p - 2) + 2 := (tsub_eq_iff_eq_add_of_le hp.1).mp rfl,
rw [this, nat.factors],
simp only [eq.symm this],
have : nat.min_fac p = p := (nat.prime_def_min_fac.mp hp).2,
split,
{ exact this, },
{ simp only [this, nat.factors, nat.div_self (nat.prime.pos hp)], },
end
lemma factors_chain : ∀ {n a}, (∀ p, prime p → p ∣ n → a ≤ p) → list.chain (≤) a (factors n)
| 0 := λ a h, by simp
| 1 := λ a h, by simp
| n@(k+2) := λ a h,
let m := min_fac n in have n / m < n := factors_lemma,
begin
rw factors,
refine list.chain.cons ((le_min_fac.2 h).resolve_left dec_trivial) (factors_chain _),
exact λ p pp d, min_fac_le_of_dvd pp.two_le (d.trans $ div_dvd_of_dvd $ min_fac_dvd _),
end
lemma factors_chain_2 (n) : list.chain (≤) 2 (factors n) := factors_chain $ λ p pp _, pp.two_le
lemma factors_chain' (n) : list.chain' (≤) (factors n) :=
@list.chain'.tail _ _ (_::_) (factors_chain_2 _)
lemma factors_sorted (n : ℕ) : list.sorted (≤) (factors n) :=
(list.chain'_iff_pairwise (@le_trans _ _)).1 (factors_chain' _)
/-- `factors` can be constructed inductively by extracting `min_fac`, for sufficiently large `n`. -/
lemma factors_add_two (n : ℕ) :
factors (n+2) = min_fac (n+2) :: factors ((n+2) / min_fac (n+2)) :=
by rw factors
@[simp]
lemma factors_eq_nil (n : ℕ) : n.factors = [] ↔ n = 0 ∨ n = 1 :=
begin
split; intro h,
{ rcases n with (_ | _ | n),
{ exact or.inl rfl },
{ exact or.inr rfl },
{ rw factors at h, injection h }, },
{ rcases h with (rfl | rfl),
{ exact factors_zero },
{ exact factors_one }, }
end
theorem prime.coprime_iff_not_dvd {p n : ℕ} (pp : prime p) : coprime p n ↔ ¬ p ∣ n :=
⟨λ co d, pp.not_dvd_one $ co.dvd_of_dvd_mul_left (by simp [d]),
λ nd, coprime_of_dvd $ λ m m2 mp, ((prime_dvd_prime_iff_eq m2 pp).1 mp).symm ▸ nd⟩
theorem prime.dvd_iff_not_coprime {p n : ℕ} (pp : prime p) : p ∣ n ↔ ¬ coprime p n :=
iff_not_comm.2 pp.coprime_iff_not_dvd
theorem prime.not_coprime_iff_dvd {m n : ℕ} :
¬ coprime m n ↔ ∃p, prime p ∧ p ∣ m ∧ p ∣ n :=
begin
apply iff.intro,
{ intro h,
exact ⟨min_fac (gcd m n), min_fac_prime h,
((min_fac_dvd (gcd m n)).trans (gcd_dvd_left m n)),
((min_fac_dvd (gcd m n)).trans (gcd_dvd_right m n))⟩ },
{ intro h,
cases h with p hp,
apply nat.not_coprime_of_dvd_of_dvd (prime.one_lt hp.1) hp.2.1 hp.2.2 }
end
theorem prime.dvd_mul {p m n : ℕ} (pp : prime p) : p ∣ m * n ↔ p ∣ m ∨ p ∣ n :=
⟨λ H, or_iff_not_imp_left.2 $ λ h,
(pp.coprime_iff_not_dvd.2 h).dvd_of_dvd_mul_left H,
or.rec (λ h : p ∣ m, h.mul_right _) (λ h : p ∣ n, h.mul_left _)⟩
theorem prime.not_dvd_mul {p m n : ℕ} (pp : prime p)
(Hm : ¬ p ∣ m) (Hn : ¬ p ∣ n) : ¬ p ∣ m * n :=
mt pp.dvd_mul.1 $ by simp [Hm, Hn]
theorem prime.dvd_of_dvd_pow {p m n : ℕ} (pp : prime p) (h : p ∣ m^n) : p ∣ m :=
by induction n with n IH;
[exact pp.not_dvd_one.elim h,
by { rw pow_succ at h, exact (pp.dvd_mul.1 h).elim id IH } ]
lemma prime.pow_dvd_of_dvd_mul_right {p n a b : ℕ} (hp : p.prime) (h : p ^ n ∣ a * b)
(hpb : ¬ p ∣ b) : p ^ n ∣ a :=
begin
induction n with n ih,
{ simp },
{ rw [pow_succ'] at *,
rcases ih ((dvd_mul_right _ _).trans h) with ⟨c, rfl⟩,
rw [mul_assoc] at h,
rcases hp.dvd_mul.1 (nat.dvd_of_mul_dvd_mul_left (pow_pos hp.pos _) h)
with ⟨d, rfl⟩|⟨d, rfl⟩,
{ rw [← mul_assoc],
exact dvd_mul_right _ _ },
{ exact (hpb (dvd_mul_right _ _)).elim } }
end
lemma prime.pow_dvd_of_dvd_mul_left {p n a b : ℕ} (hp : p.prime) (h : p ^ n ∣ a * b)
(hpb : ¬ p ∣ a) : p ^ n ∣ b :=
by rw [mul_comm] at h; exact hp.pow_dvd_of_dvd_mul_right h hpb
lemma prime.pow_not_prime {x n : ℕ} (hn : 2 ≤ n) : ¬ (x ^ n).prime :=
λ hp, (hp.2 x $ dvd_trans ⟨x, sq _⟩ (pow_dvd_pow _ hn)).elim
(λ hx1, hp.ne_one $ hx1.symm ▸ one_pow _)
(λ hxn, lt_irrefl x $ calc x = x ^ 1 : (pow_one _).symm
... < x ^ n : nat.pow_right_strict_mono (hxn.symm ▸ hp.two_le) hn
... = x : hxn.symm)
lemma prime.pow_not_prime' {x : ℕ} : ∀ {n : ℕ}, n ≠ 1 → ¬ (x ^ n).prime
| 0 := λ _, not_prime_one
| 1 := λ h, (h rfl).elim
| (n+2) := λ _, prime.pow_not_prime le_add_self
lemma prime.eq_one_of_pow {x n : ℕ} (h : (x ^ n).prime) : n = 1 :=
not_imp_not.mp prime.pow_not_prime' h
lemma prime.pow_eq_iff {p a k : ℕ} (hp : p.prime) : a ^ k = p ↔ a = p ∧ k = 1 :=
begin
refine ⟨_, λ h, by rw [h.1, h.2, pow_one]⟩,
rintro rfl,
rw [hp.eq_one_of_pow, eq_self_iff_true, and_true, pow_one],
end
lemma prime.mul_eq_prime_sq_iff {x y p : ℕ} (hp : p.prime) (hx : x ≠ 1) (hy : y ≠ 1) :
x * y = p ^ 2 ↔ x = p ∧ y = p :=
⟨λ h, have pdvdxy : p ∣ x * y, by rw h; simp [sq],
begin
wlog := hp.dvd_mul.1 pdvdxy using x y,
cases case with a ha,
have hap : a ∣ p, from ⟨y, by rwa [ha, sq,
mul_assoc, nat.mul_right_inj hp.pos, eq_comm] at h⟩,
exact ((nat.dvd_prime hp).1 hap).elim
(λ _, by clear_aux_decl; simp [*, sq, nat.mul_right_inj hp.pos] at *
{contextual := tt})
(λ _, by clear_aux_decl; simp [*, sq, mul_comm, mul_assoc,
nat.mul_right_inj hp.pos, nat.mul_right_eq_self_iff hp.pos] at *
{contextual := tt})
end,
λ ⟨h₁, h₂⟩, h₁.symm ▸ h₂.symm ▸ (sq _).symm⟩
lemma prime.dvd_factorial : ∀ {n p : ℕ} (hp : prime p), p ∣ n! ↔ p ≤ n
| 0 p hp := iff_of_false hp.not_dvd_one (not_le_of_lt hp.pos)
| (n+1) p hp := begin
rw [factorial_succ, hp.dvd_mul, prime.dvd_factorial hp],
exact ⟨λ h, h.elim (le_of_dvd (succ_pos _)) le_succ_of_le,
λ h, (_root_.lt_or_eq_of_le h).elim (or.inr ∘ le_of_lt_succ)
(λ h, or.inl $ by rw h)⟩
end
theorem prime.coprime_pow_of_not_dvd {p m a : ℕ} (pp : prime p) (h : ¬ p ∣ a) : coprime a (p^m) :=
(pp.coprime_iff_not_dvd.2 h).symm.pow_right _
theorem coprime_primes {p q : ℕ} (pp : prime p) (pq : prime q) : coprime p q ↔ p ≠ q :=
pp.coprime_iff_not_dvd.trans $ not_congr $ dvd_prime_two_le pq pp.two_le
theorem coprime_pow_primes {p q : ℕ} (n m : ℕ) (pp : prime p) (pq : prime q) (h : p ≠ q) :
coprime (p^n) (q^m) :=
((coprime_primes pp pq).2 h).pow _ _
theorem coprime_or_dvd_of_prime {p} (pp : prime p) (i : ℕ) : coprime p i ∨ p ∣ i :=
by rw [pp.dvd_iff_not_coprime]; apply em
theorem dvd_prime_pow {p : ℕ} (pp : prime p) {m i : ℕ} : i ∣ (p^m) ↔ ∃ k ≤ m, i = p^k :=
begin
induction m with m IH generalizing i, {simp [pow_succ, le_zero_iff] at *},
by_cases p ∣ i,
{ cases h with a e, subst e,
rw [pow_succ, nat.mul_dvd_mul_iff_left pp.pos, IH],
split; intro h; rcases h with ⟨k, h, e⟩,
{ exact ⟨succ k, succ_le_succ h, by rw [e, pow_succ]; refl⟩ },
cases k with k,
{ apply pp.not_dvd_one.elim,
simp at e, rw ← e, apply dvd_mul_right },
{ refine ⟨k, le_of_succ_le_succ h, _⟩,
rwa [mul_comm, pow_succ', nat.mul_left_inj pp.pos] at e } },
{ split; intro d,
{ rw (pp.coprime_pow_of_not_dvd h).eq_one_of_dvd d,
exact ⟨0, zero_le _, rfl⟩ },
{ rcases d with ⟨k, l, e⟩,
rw e, exact pow_dvd_pow _ l } }
end
/--
If `p` is prime,
and `a` doesn't divide `p^k`, but `a` does divide `p^(k+1)`
then `a = p^(k+1)`.
-/
lemma eq_prime_pow_of_dvd_least_prime_pow
{a p k : ℕ} (pp : prime p) (h₁ : ¬(a ∣ p^k)) (h₂ : a ∣ p^(k+1)) :
a = p^(k+1) :=
begin
obtain ⟨l, ⟨h, rfl⟩⟩ := (dvd_prime_pow pp).1 h₂,
congr,
exact le_antisymm h (not_le.1 ((not_congr (pow_dvd_pow_iff_le_right (prime.one_lt pp))).1 h₁)),
end
section
open list
lemma mem_list_primes_of_dvd_prod {p : ℕ} (hp : prime p) :
∀ {l : list ℕ}, (∀ p ∈ l, prime p) → p ∣ prod l → p ∈ l
| [] := λ h₁ h₂, absurd h₂ (prime.not_dvd_one hp)
| (q :: l) := λ h₁ h₂,
have h₃ : p ∣ q * prod l := @prod_cons _ _ l q ▸ h₂,
have hq : prime q := h₁ q (mem_cons_self _ _),
or.cases_on ((prime.dvd_mul hp).1 h₃)
(λ h, by rw [prime.dvd_iff_not_coprime hp, coprime_primes hp hq, ne.def, not_not] at h;
exact h ▸ mem_cons_self _ _)
(λ h, have hl : ∀ p ∈ l, prime p := λ p hlp, h₁ p ((mem_cons_iff _ _ _).2 (or.inr hlp)),
(mem_cons_iff _ _ _).2 (or.inr (mem_list_primes_of_dvd_prod hl h)))
lemma mem_factors_iff_dvd {n p : ℕ} (hn : 0 < n) (hp : prime p) : p ∈ factors n ↔ p ∣ n :=
⟨λ h, prod_factors hn ▸ list.dvd_prod h,
λ h, mem_list_primes_of_dvd_prod hp (@prime_of_mem_factors n) ((prod_factors hn).symm ▸ h)⟩
lemma mem_factors {n p} (hn : 0 < n) : p ∈ factors n ↔ prime p ∧ p ∣ n :=
⟨λ h, ⟨prime_of_mem_factors h, (mem_factors_iff_dvd hn $ prime_of_mem_factors h).mp h⟩,
λ ⟨hprime, hdvd⟩, (mem_factors_iff_dvd hn hprime).mpr hdvd⟩
lemma factors_subset_right {n k : ℕ} (h : k ≠ 0) : n.factors ⊆ (n * k).factors :=
begin
cases n,
{ rw zero_mul, refl },
cases n,
{ rw factors_one, apply list.nil_subset },
intros p hp,
rw mem_factors succ_pos' at hp,
rw mem_factors (nat.mul_pos succ_pos' (nat.pos_of_ne_zero h)),
exact ⟨hp.1, hp.2.mul_right k⟩,
end
lemma factors_subset_of_dvd {n k : ℕ} (h : n ∣ k) (h' : k ≠ 0) : n.factors ⊆ k.factors :=
begin
obtain ⟨a, rfl⟩ := h,
exact factors_subset_right (right_ne_zero_of_mul h'),
end
lemma perm_of_prod_eq_prod : ∀ {l₁ l₂ : list ℕ}, prod l₁ = prod l₂ →
(∀ p ∈ l₁, prime p) → (∀ p ∈ l₂, prime p) → l₁ ~ l₂
| [] [] _ _ _ := perm.nil
| [] (a :: l) h₁ h₂ h₃ :=
have ha : a ∣ 1 := @prod_nil ℕ _ ▸ h₁.symm ▸ (@prod_cons _ _ l a).symm ▸ dvd_mul_right _ _,
absurd ha (prime.not_dvd_one (h₃ a (mem_cons_self _ _)))
| (a :: l) [] h₁ h₂ h₃ :=
have ha : a ∣ 1 := @prod_nil ℕ _ ▸ h₁ ▸ (@prod_cons _ _ l a).symm ▸ dvd_mul_right _ _,
absurd ha (prime.not_dvd_one (h₂ a (mem_cons_self _ _)))
| (a :: l₁) (b :: l₂) h hl₁ hl₂ :=
have hl₁' : ∀ p ∈ l₁, prime p := λ p hp, hl₁ p (mem_cons_of_mem _ hp),
have hl₂' : ∀ p ∈ (b :: l₂).erase a, prime p := λ p hp, hl₂ p (mem_of_mem_erase hp),
have ha : a ∈ (b :: l₂) := mem_list_primes_of_dvd_prod (hl₁ a (mem_cons_self _ _)) hl₂
(h ▸ by rw prod_cons; exact dvd_mul_right _ _),
have hb : b :: l₂ ~ a :: (b :: l₂).erase a := perm_cons_erase ha,
have hl : prod l₁ = prod ((b :: l₂).erase a) :=
(nat.mul_right_inj (prime.pos (hl₁ a (mem_cons_self _ _)))).1 $
by rwa [← prod_cons, ← prod_cons, ← hb.prod_eq],
perm.trans ((perm_of_prod_eq_prod hl hl₁' hl₂').cons _) hb.symm
/-- **Fundamental theorem of arithmetic**-/
lemma factors_unique {n : ℕ} {l : list ℕ} (h₁ : prod l = n) (h₂ : ∀ p ∈ l, prime p) :
l ~ factors n :=
have hn : 0 < n := nat.pos_of_ne_zero $ λ h, begin
rw h at *, clear h,
induction l with a l hi,
{ exact absurd h₁ dec_trivial },
{ rw prod_cons at h₁,
exact nat.mul_ne_zero (ne_of_lt (prime.pos (h₂ a (mem_cons_self _ _)))).symm
(hi (λ p hp, h₂ p (mem_cons_of_mem _ hp))) h₁ }
end,
perm_of_prod_eq_prod (by rwa prod_factors hn) h₂ (@prime_of_mem_factors _)
lemma prime.factors_pow {p : ℕ} (hp : p.prime) (n : ℕ) :
(p ^ n).factors = list.repeat p n :=
begin
symmetry,
rw ← list.repeat_perm,
apply nat.factors_unique (list.prod_repeat p n),
{ intros q hq,
rwa eq_of_mem_repeat hq },
end
end
lemma succ_dvd_or_succ_dvd_of_succ_sum_dvd_mul {p : ℕ} (p_prime : prime p) {m n k l : ℕ}
(hpm : p ^ k ∣ m) (hpn : p ^ l ∣ n) (hpmn : p ^ (k+l+1) ∣ m*n) :
p ^ (k+1) ∣ m ∨ p ^ (l+1) ∣ n :=
have hpd : p^(k+l)*p ∣ m*n, by rwa pow_succ' at hpmn,
have hpd2 : p ∣ (m*n) / p ^ (k+l), from dvd_div_of_mul_dvd hpd,
have hpd3 : p ∣ (m*n) / (p^k * p^l), by simpa [pow_add] using hpd2,
have hpd4 : p ∣ (m / p^k) * (n / p^l), by simpa [nat.div_mul_div hpm hpn] using hpd3,
have hpd5 : p ∣ (m / p^k) ∨ p ∣ (n / p^l), from (prime.dvd_mul p_prime).1 hpd4,
suffices p^k*p ∣ m ∨ p^l*p ∣ n, by rwa [pow_succ', pow_succ'],
hpd5.elim
(assume : p ∣ m / p ^ k, or.inl $ mul_dvd_of_dvd_div hpm this)
(assume : p ∣ n / p ^ l, or.inr $ mul_dvd_of_dvd_div hpn this)
/-- The type of prime numbers -/
def primes := {p : ℕ // p.prime}
namespace primes
instance : has_repr nat.primes := ⟨λ p, repr p.val⟩
instance inhabited_primes : inhabited primes := ⟨⟨2, prime_two⟩⟩
instance coe_nat : has_coe nat.primes ℕ := ⟨subtype.val⟩
theorem coe_nat_inj (p q : nat.primes) : (p : ℕ) = (q : ℕ) → p = q :=
λ h, subtype.eq h
end primes
instance monoid.prime_pow {α : Type*} [monoid α] : has_pow α primes := ⟨λ x p, x^p.val⟩
end nat
/-! ### Primality prover -/
open norm_num
namespace tactic
namespace norm_num
lemma is_prime_helper (n : ℕ)
(h₁ : 1 < n) (h₂ : nat.min_fac n = n) : nat.prime n :=
nat.prime_def_min_fac.2 ⟨h₁, h₂⟩
lemma min_fac_bit0 (n : ℕ) : nat.min_fac (bit0 n) = 2 :=
by simp [nat.min_fac_eq, show 2 ∣ bit0 n, by simp [bit0_eq_two_mul n]]
/-- A predicate representing partial progress in a proof of `min_fac`. -/
def min_fac_helper (n k : ℕ) : Prop :=
0 < k ∧ bit1 k ≤ nat.min_fac (bit1 n)
theorem min_fac_helper.n_pos {n k : ℕ} (h : min_fac_helper n k) : 0 < n :=
pos_iff_ne_zero.2 $ λ e,
by rw e at h; exact not_le_of_lt (nat.bit1_lt h.1) h.2
lemma min_fac_ne_bit0 {n k : ℕ} : nat.min_fac (bit1 n) ≠ bit0 k :=
begin
rw bit0_eq_two_mul,
refine (λ e, absurd ((nat.dvd_add_iff_right _).2
(dvd_trans ⟨_, e⟩ (nat.min_fac_dvd _))) _); simp
end
lemma min_fac_helper_0 (n : ℕ) (h : 0 < n) : min_fac_helper n 1 :=
begin
refine ⟨zero_lt_one, lt_of_le_of_ne _ min_fac_ne_bit0.symm⟩,
refine @lt_of_le_of_ne ℕ _ _ _ (nat.min_fac_pos _) _,
intro e,
have := nat.min_fac_prime _,
{ rw ← e at this, exact nat.not_prime_one this },
{ exact ne_of_gt (nat.bit1_lt h) }
end
lemma min_fac_helper_1 {n k k' : ℕ} (e : k + 1 = k')
(np : nat.min_fac (bit1 n) ≠ bit1 k)
(h : min_fac_helper n k) : min_fac_helper n k' :=
begin
rw ← e,
refine ⟨nat.succ_pos _,
(lt_of_le_of_ne (lt_of_le_of_ne _ _ : k+1+k < _)
min_fac_ne_bit0.symm : bit0 (k+1) < _)⟩,
{ rw add_right_comm, exact h.2 },
{ rw add_right_comm, exact np.symm }
end
lemma min_fac_helper_2 (n k k' : ℕ) (e : k + 1 = k')
(np : ¬ nat.prime (bit1 k)) (h : min_fac_helper n k) : min_fac_helper n k' :=
begin
refine min_fac_helper_1 e _ h,
intro e₁, rw ← e₁ at np,
exact np (nat.min_fac_prime $ ne_of_gt $ nat.bit1_lt h.n_pos)
end
lemma min_fac_helper_3 (n k k' c : ℕ) (e : k + 1 = k')
(nc : bit1 n % bit1 k = c) (c0 : 0 < c)
(h : min_fac_helper n k) : min_fac_helper n k' :=
begin
refine min_fac_helper_1 e _ h,
refine mt _ (ne_of_gt c0), intro e₁,
rw [← nc, ← nat.dvd_iff_mod_eq_zero, ← e₁],
apply nat.min_fac_dvd
end
lemma min_fac_helper_4 (n k : ℕ) (hd : bit1 n % bit1 k = 0)
(h : min_fac_helper n k) : nat.min_fac (bit1 n) = bit1 k :=
by rw ← nat.dvd_iff_mod_eq_zero at hd; exact
le_antisymm (nat.min_fac_le_of_dvd (nat.bit1_lt h.1) hd) h.2
lemma min_fac_helper_5 (n k k' : ℕ) (e : bit1 k * bit1 k = k')
(hd : bit1 n < k') (h : min_fac_helper n k) : nat.min_fac (bit1 n) = bit1 n :=
begin
refine (nat.prime_def_min_fac.1 (nat.prime_def_le_sqrt.2
⟨nat.bit1_lt h.n_pos, _⟩)).2,
rw ← e at hd,
intros m m2 hm md,
have := le_trans h.2 (le_trans (nat.min_fac_le_of_dvd m2 md) hm),
rw nat.le_sqrt at this,
exact not_le_of_lt hd this
end
/-- Given `e` a natural numeral and `d : nat` a factor of it, return `⊢ ¬ prime e`. -/
meta def prove_non_prime (e : expr) (n d₁ : ℕ) : tactic expr :=
do let e₁ := reflect d₁,
c ← mk_instance_cache `(nat),
(c, p₁) ← prove_lt_nat c `(1) e₁,
let d₂ := n / d₁, let e₂ := reflect d₂,
(c, e', p) ← prove_mul_nat c e₁ e₂,
guard (e' =ₐ e),
(c, p₂) ← prove_lt_nat c `(1) e₂,
return $ `(@nat.not_prime_mul').mk_app [e₁, e₂, e, p, p₁, p₂]
/-- Given `a`,`a1 := bit1 a`, `n1` the value of `a1`, `b` and `p : min_fac_helper a b`,
returns `(c, ⊢ min_fac a1 = c)`. -/
meta def prove_min_fac_aux (a a1 : expr) (n1 : ℕ) :
instance_cache → expr → expr → tactic (instance_cache × expr × expr)
| ic b p := do
k ← b.to_nat,
let k1 := bit1 k,
let b1 := `(bit1:ℕ→ℕ).mk_app [b],
if n1 < k1*k1 then do
(ic, e', p₁) ← prove_mul_nat ic b1 b1,
(ic, p₂) ← prove_lt_nat ic a1 e',
return (ic, a1, `(min_fac_helper_5).mk_app [a, b, e', p₁, p₂, p])
else let d := k1.min_fac in
if to_bool (d < k1) then do
let k' := k+1, let e' := reflect k',
(ic, p₁) ← prove_succ ic b e',
p₂ ← prove_non_prime b1 k1 d,
prove_min_fac_aux ic e' $ `(min_fac_helper_2).mk_app [a, b, e', p₁, p₂, p]
else do
let nc := n1 % k1,
(ic, c, pc) ← prove_div_mod ic a1 b1 tt,
if nc = 0 then
return (ic, b1, `(min_fac_helper_4).mk_app [a, b, pc, p])
else do
(ic, p₀) ← prove_pos ic c,
let k' := k+1, let e' := reflect k',
(ic, p₁) ← prove_succ ic b e',
prove_min_fac_aux ic e' $ `(min_fac_helper_3).mk_app [a, b, e', c, p₁, pc, p₀, p]
/-- Given `a` a natural numeral, returns `(b, ⊢ min_fac a = b)`. -/
meta def prove_min_fac (ic : instance_cache) (e : expr) : tactic (instance_cache × expr × expr) :=
match match_numeral e with
| match_numeral_result.zero := return (ic, `(2:ℕ), `(nat.min_fac_zero))
| match_numeral_result.one := return (ic, `(1:ℕ), `(nat.min_fac_one))
| match_numeral_result.bit0 e := return (ic, `(2), `(min_fac_bit0).mk_app [e])
| match_numeral_result.bit1 e := do
n ← e.to_nat,
c ← mk_instance_cache `(nat),
(c, p) ← prove_pos c e,
let a1 := `(bit1:ℕ→ℕ).mk_app [e],
prove_min_fac_aux e a1 (bit1 n) c `(1) (`(min_fac_helper_0).mk_app [e, p])
| _ := failed
end
/-- A partial proof of `factors`. Asserts that `l` is a sorted list of primes, lower bounded by a
prime `p`, which multiplies to `n`. -/
def factors_helper (n p : ℕ) (l : list ℕ) : Prop :=
p.prime → list.chain (≤) p l ∧ (∀ a ∈ l, nat.prime a) ∧ list.prod l = n
lemma factors_helper_nil (a : ℕ) : factors_helper 1 a [] :=
λ pa, ⟨list.chain.nil, by rintro _ ⟨⟩, list.prod_nil⟩
lemma factors_helper_cons' (n m a b : ℕ) (l : list ℕ)
(h₁ : b * m = n) (h₂ : a ≤ b) (h₃ : nat.min_fac b = b)
(H : factors_helper m b l) : factors_helper n a (b :: l) :=
λ pa,
have pb : b.prime, from nat.prime_def_min_fac.2 ⟨le_trans pa.two_le h₂, h₃⟩,
let ⟨f₁, f₂, f₃⟩ := H pb in
⟨list.chain.cons h₂ f₁, λ c h, h.elim (λ e, e.symm ▸ pb) (f₂ _),
by rw [list.prod_cons, f₃, h₁]⟩
lemma factors_helper_cons (n m a b : ℕ) (l : list ℕ)
(h₁ : b * m = n) (h₂ : a < b) (h₃ : nat.min_fac b = b)
(H : factors_helper m b l) : factors_helper n a (b :: l) :=
factors_helper_cons' _ _ _ _ _ h₁ h₂.le h₃ H
lemma factors_helper_sn (n a : ℕ) (h₁ : a < n) (h₂ : nat.min_fac n = n) : factors_helper n a [n] :=
factors_helper_cons _ _ _ _ _ (mul_one _) h₁ h₂ (factors_helper_nil _)
lemma factors_helper_same (n m a : ℕ) (l : list ℕ) (h : a * m = n)
(H : factors_helper m a l) : factors_helper n a (a :: l) :=
λ pa, factors_helper_cons' _ _ _ _ _ h (le_refl _) (nat.prime_def_min_fac.1 pa).2 H pa
lemma factors_helper_same_sn (a : ℕ) : factors_helper a a [a] :=
factors_helper_same _ _ _ _ (mul_one _) (factors_helper_nil _)
lemma factors_helper_end (n : ℕ) (l : list ℕ) (H : factors_helper n 2 l) : nat.factors n = l :=
let ⟨h₁, h₂, h₃⟩ := H nat.prime_two in
have _, from (list.chain'_iff_pairwise (@le_trans _ _)).1 (@list.chain'.tail _ _ (_::_) h₁),
(list.eq_of_perm_of_sorted (nat.factors_unique h₃ h₂) this (nat.factors_sorted _)).symm
/-- Given `n` and `a` natural numerals, returns `(l, ⊢ factors_helper n a l)`. -/
meta def prove_factors_aux :
instance_cache → expr → expr → ℕ → ℕ → tactic (instance_cache × expr × expr)
| c en ea n a :=
let b := n.min_fac in
if b < n then do
let m := n / b,
(c, em) ← c.of_nat m,
if b = a then do
(c, _, p₁) ← prove_mul_nat c ea em,
(c, l, p₂) ← prove_factors_aux c em ea m a,
pure (c, `(%%ea::%%l:list ℕ), `(factors_helper_same).mk_app [en, em, ea, l, p₁, p₂])
else do
(c, eb) ← c.of_nat b,
(c, _, p₁) ← prove_mul_nat c eb em,
(c, p₂) ← prove_lt_nat c ea eb,
(c, _, p₃) ← prove_min_fac c eb,
(c, l, p₄) ← prove_factors_aux c em eb m b,
pure (c, `(%%eb::%%l : list ℕ),
`(factors_helper_cons).mk_app [en, em, ea, eb, l, p₁, p₂, p₃, p₄])
else if b = a then
pure (c, `([%%ea] : list ℕ), `(factors_helper_same_sn).mk_app [ea])
else do
(c, p₁) ← prove_lt_nat c ea en,
(c, _, p₂) ← prove_min_fac c en,
pure (c, `([%%en] : list ℕ), `(factors_helper_sn).mk_app [en, ea, p₁, p₂])
/-- Evaluates the `prime` and `min_fac` functions. -/
@[norm_num] meta def eval_prime : expr → tactic (expr × expr)
| `(nat.prime %%e) := do
n ← e.to_nat,
match n with
| 0 := false_intro `(nat.not_prime_zero)
| 1 := false_intro `(nat.not_prime_one)
| _ := let d₁ := n.min_fac in
if d₁ < n then prove_non_prime e n d₁ >>= false_intro
else do
let e₁ := reflect d₁,
c ← mk_instance_cache `(ℕ),
(c, p₁) ← prove_lt_nat c `(1) e₁,
(c, e₁, p) ← prove_min_fac c e,
true_intro $ `(is_prime_helper).mk_app [e, p₁, p]
end
| `(nat.min_fac %%e) := do
ic ← mk_instance_cache `(ℕ),
prod.snd <$> prove_min_fac ic e
| `(nat.factors %%e) := do
n ← e.to_nat,
match n with
| 0 := pure (`(@list.nil ℕ), `(nat.factors_zero))
| 1 := pure (`(@list.nil ℕ), `(nat.factors_one))
| _ := do
c ← mk_instance_cache `(ℕ),
(c, l, p) ← prove_factors_aux c e `(2) n 2,
pure (l, `(factors_helper_end).mk_app [e, l, p])
end
| _ := failed
end norm_num
end tactic
namespace nat
theorem prime_three : prime 3 := by norm_num
end nat
|
5ebe6046ca2a8b70ef78482ad154aa03a5515a63 | 05f637fa14ac28031cb1ea92086a0f4eb23ff2b1 | /tests/lean/exists3.lean | 438f94f972ff7b48a0dc4bff5a616c49033b14a7 | [
"Apache-2.0"
] | permissive | codyroux/lean0.1 | 1ce92751d664aacff0529e139083304a7bbc8a71 | 0dc6fb974aa85ed6f305a2f4b10a53a44ee5f0ef | refs/heads/master | 1,610,830,535,062 | 1,402,150,480,000 | 1,402,150,480,000 | 19,588,851 | 2 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,081 | lean | import Int.
variable P : Int -> Int -> Bool
set_opaque exists false.
theorem T1 (R1 : not (exists x y, P x y)) : forall x y, not (P x y) :=
fun a b,
not_not_elim (not_not_elim R1 a) b
axiom Ax : forall x, exists y, P x y
theorem T2 : exists x y, P x y :=
by_contradiction (fun R : not (exists x y, P x y),
let L1 : forall x y, not (P x y) := fun a b, (not_not_elim ((not_not_elim R) a)) b,
L2 : exists y, P 0 y := Ax 0
in exists_elim L2 (fun (w : Int) (H : P 0 w),
absurd H (L1 0 w))).
theorem T3 (A : (Type U)) (P : A -> A -> Bool) (a : A) (H1 : forall x, exists y, P x y) : exists x y, P x y :=
by_contradiction (fun R : not (exists x y, P x y),
let L1 : forall x y, not (P x y) := fun a b,
(not_not_elim ((not_not_elim R) a)) b,
L2 : exists y, P a y := H1 a
in exists_elim L2 (fun (w : A) (H : P a w),
absurd H (L1 a w))).
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.