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
1193c9d46b939697fdbe8c9278652a3c121c42c0
cf39355caa609c0f33405126beee2739aa3cb77e
/library/init/control/applicative.lean
f8d0e44e09af44b65fc7a7bb4b0e6ab96cc1a07d
[ "Apache-2.0" ]
permissive
leanprover-community/lean
12b87f69d92e614daea8bcc9d4de9a9ace089d0e
cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0
refs/heads/master
1,687,508,156,644
1,684,951,104,000
1,684,951,104,000
169,960,991
457
107
Apache-2.0
1,686,744,372,000
1,549,790,268,000
C++
UTF-8
Lean
false
false
1,113
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.control.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)
93d94c6164996399686799b9d33ad6c29a5eb062
57aec6ee746bc7e3a3dd5e767e53bd95beb82f6d
/tests/lean/run/choiceMacroRules.lean
197c2061680cdf035e4c29c07fbf23c75a6aa1db
[ "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
441
lean
syntax:65 (name := myAdd1) term "+++" term:65 : term syntax:65 (name := myAdd2) term "+++" term:65 : term macro_rules [myAdd1] | `($a +++ $b) => `(Nat.add $a $b) macro_rules [myAdd2] | `($a +++ $b) => `(Append.append $a $b) #check (1:Nat) +++ 3 theorem tst1 : ((1:Nat) +++ 3) = 1 + 3 := rfl #check fun (x : Nat) => if x +++ 3 = x then x else x + 1 #check [1, 2] +++ [3, 4] theorem tst2 : ([1, 2] +++ [3, 4]) = [1, 2] ++ [3, 4] := rfl
ea8c3f1d80e50163b13d79aa93fa5f54695887cb
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/topology/algebra/with_zero_topology.lean
7714790c9053be019c14464ceffe8896307f530a
[ "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,423
lean
/- Copyright (c) 2021 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot -/ import algebra.order.with_zero import topology.algebra.order.field /-! # The topology on linearly ordered commutative groups with zero Let `Γ₀` be a linearly ordered commutative group to which we have adjoined a zero element. Then `Γ₀` may naturally be endowed with a topology that turns `Γ₀` into a topological monoid. Neighborhoods of zero are sets containing `{γ | γ < γ₀}` for some invertible element `γ₀` and every invertible element is open. In particular the topology is the following: "a subset `U ⊆ Γ₀` is open if `0 ∉ U` or if there is an invertible `γ₀ ∈ Γ₀` such that `{γ | γ < γ₀} ⊆ U`", see `linear_ordered_comm_group_with_zero.is_open_iff`. We prove this topology is ordered and T₃ (in addition to be compatible with the monoid structure). All this is useful to extend a valuation to a completion. This is an abstract version of how the absolute value (resp. `p`-adic absolute value) on `ℚ` is extended to `ℝ` (resp. `ℚₚ`). ## Implementation notes This topology is not defined as an instance since it may not be the desired topology on a linearly ordered commutative group with zero. You can locally activate this topology using `local attribute [instance] linear_ordered_comm_group_with_zero.topological_space` All other instances will (`ordered_topology`, `t3_space`, `has_continuous_mul`) then follow. -/ open_locale topological_space filter open topological_space filter set function namespace linear_ordered_comm_group_with_zero variables {α Γ₀ : Type*} [linear_ordered_comm_group_with_zero Γ₀] {γ γ₁ γ₂ : Γ₀} {l : filter α} {f : α → Γ₀} /-- The topology on a linearly ordered commutative group with a zero element adjoined. A subset U is open if 0 ∉ U or if there is an invertible element γ₀ such that {γ | γ < γ₀} ⊆ U. -/ protected def topological_space : topological_space Γ₀ := topological_space.mk_of_nhds $ update pure 0 $ ⨅ γ ≠ 0, 𝓟 (Iio γ) local attribute [instance] linear_ordered_comm_group_with_zero.topological_space lemma nhds_eq_update : (𝓝 : Γ₀ → filter Γ₀) = update pure 0 (⨅ γ ≠ 0, 𝓟 (Iio γ)) := funext $ nhds_mk_of_nhds_single $ le_infi₂ $ λ γ h₀, le_principal_iff.2 $ zero_lt_iff.2 h₀ /-! ### Neighbourhoods of zero -/ lemma nhds_zero : 𝓝 (0 : Γ₀) = ⨅ γ ≠ 0, 𝓟 (Iio γ) := by rw [nhds_eq_update, update_same] /-- In a linearly ordered group with zero element adjoined, `U` is a neighbourhood of `0` if and only if there exists a nonzero element `γ₀` such that `Iio γ₀ ⊆ U`. -/ lemma has_basis_nhds_zero : (𝓝 (0 : Γ₀)).has_basis (λ γ : Γ₀, γ ≠ 0) Iio := begin rw [nhds_zero], refine has_basis_binfi_principal _ ⟨1, one_ne_zero⟩, exact directed_on_iff_directed.2 (directed_of_inf $ λ a b hab, Iio_subset_Iio hab) end lemma Iio_mem_nhds_zero (hγ : γ ≠ 0) : Iio γ ∈ 𝓝 (0 : Γ₀) := has_basis_nhds_zero.mem_of_mem hγ /-- If `γ` is an invertible element of a linearly ordered group with zero element adjoined, then `Iio (γ : Γ₀)` is a neighbourhood of `0`. -/ lemma nhds_zero_of_units (γ : Γ₀ˣ) : Iio ↑γ ∈ 𝓝 (0 : Γ₀) := Iio_mem_nhds_zero γ.ne_zero lemma tendsto_zero : tendsto f l (𝓝 (0 : Γ₀)) ↔ ∀ γ₀ ≠ 0, ∀ᶠ x in l, f x < γ₀ := by simp [nhds_zero] /-! ### Neighbourhoods of non-zero elements -/ /-- The neighbourhood filter of a nonzero element consists of all sets containing that element. -/ @[simp] lemma nhds_of_ne_zero {γ : Γ₀} (h₀ : γ ≠ 0) : 𝓝 γ = pure γ := by rw [nhds_eq_update, update_noteq h₀] /-- The neighbourhood filter of an invertible element consists of all sets containing that element. -/ lemma nhds_coe_units (γ : Γ₀ˣ) : 𝓝 (γ : Γ₀) = pure (γ : Γ₀) := nhds_of_ne_zero γ.ne_zero /-- If `γ` is an invertible element of a linearly ordered group with zero element adjoined, then `{γ}` is a neighbourhood of `γ`. -/ lemma singleton_mem_nhds_of_units (γ : Γ₀ˣ) : ({γ} : set Γ₀) ∈ 𝓝 (γ : Γ₀) := by simp /-- If `γ` is a nonzero element of a linearly ordered group with zero element adjoined, then `{γ}` is a neighbourhood of `γ`. -/ lemma singleton_mem_nhds_of_ne_zero (h : γ ≠ 0) : ({γ} : set Γ₀) ∈ 𝓝 (γ : Γ₀) := by simp [h] lemma has_basis_nhds_of_ne_zero {x : Γ₀} (h : x ≠ 0) : has_basis (𝓝 x) (λ i : unit, true) (λ i, {x}) := by { rw [nhds_of_ne_zero h], exact has_basis_pure _ } lemma has_basis_nhds_units (γ : Γ₀ˣ) : has_basis (𝓝 (γ : Γ₀)) (λ i : unit, true) (λ i, {γ}) := has_basis_nhds_of_ne_zero γ.ne_zero lemma tendsto_of_ne_zero {γ : Γ₀} (h : γ ≠ 0) : tendsto f l (𝓝 γ) ↔ ∀ᶠ x in l, f x = γ := by rw [nhds_of_ne_zero h, tendsto_pure] lemma tendsto_units {γ₀ : Γ₀ˣ} : tendsto f l (𝓝 (γ₀ : Γ₀)) ↔ ∀ᶠ x in l, f x = γ₀ := tendsto_of_ne_zero γ₀.ne_zero lemma Iio_mem_nhds (h : γ₁ < γ₂) : Iio γ₂ ∈ 𝓝 γ₁ := by rcases eq_or_ne γ₁ 0 with rfl|h₀; simp [*, h.ne', Iio_mem_nhds_zero] /-! ### Open/closed sets -/ lemma is_open_iff {s : set Γ₀} : is_open s ↔ (0 : Γ₀) ∉ s ∨ ∃ γ ≠ 0, Iio γ ⊆ s := begin rw [is_open_iff_mem_nhds, ← and_forall_ne (0 : Γ₀)], simp [nhds_of_ne_zero, imp_iff_not_or, has_basis_nhds_zero.mem_iff] { contextual := tt } end lemma is_closed_iff {s : set Γ₀} : is_closed s ↔ (0 : Γ₀) ∈ s ∨ ∃ γ ≠ 0, s ⊆ Ici γ := by simp only [← is_open_compl_iff, is_open_iff, mem_compl_iff, not_not, ← compl_Ici, compl_subset_compl] lemma is_open_Iio {a : Γ₀} : is_open (Iio a) := is_open_iff.mpr $ imp_iff_not_or.mp $ λ ha, ⟨a, ne_of_gt ha, subset.rfl⟩ /-! ### Instances -/ /-- The topology on a linearly ordered group with zero element adjoined is compatible with the order structure: the set `{p : Γ₀ × Γ₀ | p.1 ≤ p.2}` is closed. -/ @[priority 100] instance order_closed_topology : order_closed_topology Γ₀ := { is_closed_le' := begin simp only [← is_open_compl_iff, compl_set_of, not_le, is_open_iff_mem_nhds], rintros ⟨a, b⟩ (hab : b < a), rw [nhds_prod_eq, nhds_of_ne_zero (zero_le'.trans_lt hab).ne', pure_prod], exact Iio_mem_nhds hab end } /-- The topology on a linearly ordered group with zero element adjoined is T₃. -/ @[priority 100] instance t3_space : t3_space Γ₀ := { to_regular_space := regular_space.of_lift'_closure $ λ γ, begin rcases ne_or_eq γ 0 with h₀|rfl, { rw [nhds_of_ne_zero h₀, lift'_pure (monotone_closure Γ₀), closure_singleton, principal_singleton] }, { exact has_basis_nhds_zero.lift'_closure_eq_self (λ x hx, is_closed_iff.2 $ or.inl $ zero_lt_iff.2 hx) }, end } /-- The topology on a linearly ordered group with zero element adjoined makes it a topological monoid. -/ @[priority 100] instance : has_continuous_mul Γ₀ := ⟨begin rw continuous_iff_continuous_at, rintros ⟨x, y⟩, wlog hle : x ≤ y := le_total x y using [x y, y x] tactic.skip, swap, { simpa only [mul_comm, (∘), prod.swap] using tendsto.comp this (continuous_swap.tendsto (x, y)) }, rcases eq_or_ne x 0 with rfl|hx; [rcases eq_or_ne y 0 with rfl|hy, skip], { rw [continuous_at, zero_mul], refine ((has_basis_nhds_zero.prod_nhds has_basis_nhds_zero).tendsto_iff has_basis_nhds_zero).2 (λ γ hγ, ⟨(γ, 1), ⟨hγ, one_ne_zero⟩, _⟩), rintro ⟨x, y⟩ ⟨hx : x < γ, hy : y < 1⟩, exact (mul_lt_mul₀ hx hy).trans_eq (mul_one γ) }, { rw [continuous_at, zero_mul, nhds_prod_eq, nhds_of_ne_zero hy, prod_pure, tendsto_map'_iff], refine (has_basis_nhds_zero.tendsto_iff has_basis_nhds_zero).2 (λ γ hγ, _), refine ⟨γ / y, div_ne_zero hγ hy, λ x hx, _⟩, calc x * y < γ / y * y : mul_lt_right₀ _ hx hy ... = γ : div_mul_cancel _ hy }, { have hy : y ≠ 0, from ((zero_lt_iff.mpr hx).trans_le hle).ne', rw [continuous_at, nhds_prod_eq, nhds_of_ne_zero hx, nhds_of_ne_zero hy, prod_pure_pure], exact pure_le_nhds (x * y) } end⟩ @[priority 100] instance : has_continuous_inv₀ Γ₀ := ⟨λ γ h, by { rw [continuous_at, nhds_of_ne_zero h], exact pure_le_nhds γ⁻¹ }⟩ end linear_ordered_comm_group_with_zero
dc90ec8f62e261b290cbcc3e58adb79979298b5e
7cef822f3b952965621309e88eadf618da0c8ae9
/src/tactic/push_neg.lean
788392f23350d0d19100ed867b1f3b1ff6c37213
[ "Apache-2.0" ]
permissive
rmitta/mathlib
8d90aee30b4db2b013e01f62c33f297d7e64a43d
883d974b608845bad30ae19e27e33c285200bf84
refs/heads/master
1,585,776,832,544
1,576,874,096,000
1,576,874,096,000
153,663,165
0
2
Apache-2.0
1,544,806,490,000
1,539,884,365,000
Lean
UTF-8
Lean
false
false
6,884
lean
/- Copyright (c) 2019 Patrick Massot All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Patrick Massot, Simon Hudon A tactic pushing negations into an expression -/ import tactic.interactive import algebra.order open tactic expr namespace push_neg section universe u variable {α : Sort u} variables (p q : Prop) variable (s : α → Prop) local attribute [instance, priority 10] classical.prop_decidable theorem not_not_eq : (¬ ¬ p) = p := propext not_not theorem not_and_eq : (¬ (p ∧ q)) = (¬ p ∨ ¬ q) := propext not_and_distrib theorem not_or_eq : (¬ (p ∨ q)) = (¬ p ∧ ¬ q) := propext not_or_distrib theorem not_forall_eq : (¬ ∀ x, s x) = (∃ x, ¬ s x) := propext not_forall theorem not_exists_eq : (¬ ∃ x, s x) = (∀ x, ¬ s x) := propext not_exists theorem not_implies_eq : (¬ (p → q)) = (p ∧ ¬ q) := propext not_imp theorem classical.implies_iff_not_or : (p → q) ↔ (¬ p ∨ q) := imp_iff_not_or theorem not_eq (a b : α) : (¬ a = b) ↔ (a ≠ b) := iff.rfl variable {β : Type u} variable [linear_order β] theorem not_le_eq (a b : β) : (¬ (a ≤ b)) = (b < a) := propext not_le theorem not_lt_eq (a b : β) : (¬ (a < b)) = (b ≤ a) := propext not_lt end meta def whnf_reducible (e : expr) : tactic expr := whnf e reducible private meta def transform_negation_step (e : expr) : tactic (option (expr × expr)) := do e ← whnf_reducible e, match e with | `(¬ %%ne) := (do ne ← whnf_reducible ne, match ne with | `(¬ %%a) := do pr ← mk_app ``not_not_eq [a], return (some (a, pr)) | `(%%a ∧ %%b) := do pr ← mk_app ``not_and_eq [a, b], return (some (`(¬ %%a ∨ ¬ %%b), pr)) | `(%%a ∨ %%b) := do pr ← mk_app ``not_or_eq [a, b], return (some (`(¬ %%a ∧ ¬ %%b), pr)) | `(%%a ≤ %%b) := do e ← to_expr ``(%%b < %%a), pr ← mk_app ``not_le_eq [a, b], return (some (e, pr)) | `(%%a < %%b) := do e ← to_expr ``(%%b ≤ %%a), pr ← mk_app ``not_lt_eq [a, b], return (some (e, pr)) | `(Exists %%p) := do pr ← mk_app ``not_exists_eq [p], e ← match p with | (lam n bi typ bo) := do body ← mk_app ``not [bo], return (pi n bi typ body) | _ := tactic.fail "Unexpected failure negating ∃" end, return (some (e, pr)) | (pi n bi d p) := if p.has_var then do pr ← mk_app ``not_forall_eq [lam n bi d p], body ← mk_app ``not [p], e ← mk_app ``Exists [lam n bi d body], return (some (e, pr)) else do pr ← mk_app ``not_implies_eq [d, p], `(%%_ = %%e') ← infer_type pr, return (some (e', pr)) | _ := return none end) | _ := return none end private meta def transform_negation : expr → tactic (option (expr × expr)) | e := do (some (e', pr)) ← transform_negation_step e | return none, (some (e'', pr')) ← transform_negation e' | return (some (e', pr)), pr'' ← mk_eq_trans pr pr', return (some (e'', pr'')) meta def normalize_negations (t : expr) : tactic (expr × expr) := do (_, e, pr) ← simplify_top_down () (λ _, λ e, do oepr ← transform_negation e, match oepr with | (some (e', pr)) := return ((), e', pr) | none := do pr ← mk_eq_refl e, return ((), e, pr) end) t { eta := ff }, return (e, pr) meta def push_neg_at_hyp (h : name) : tactic unit := do H ← get_local h, t ← infer_type H, (e, pr) ← normalize_negations t, replace_hyp H e pr, skip meta def push_neg_at_goal : tactic unit := do H ← target, (e, pr) ← normalize_negations H, replace_target e pr end push_neg open interactive (parse loc.ns loc.wildcard) open interactive.types (location texpr) open lean.parser (tk ident many) interactive.loc local postfix `?`:9001 := optional local postfix *:9001 := many open push_neg /-- Push negations in the goal of some assumption. For instance, given `h : ¬ ∀ x, ∃ y, x ≤ y`, will be transformed by `push_neg at h` into `h : ∃ x, ∀ y, y < x`. Variables names are conserved. -/ meta def tactic.interactive.push_neg : parse location → tactic unit | (loc.ns loc_l) := loc_l.mmap' (λ l, match l with | some h := do push_neg_at_hyp h, try `[simp only [push_neg.not_eq] at h { eta := ff }] | none := do push_neg_at_goal, try `[simp only [push_neg.not_eq] { eta := ff }] end) | loc.wildcard := do push_neg_at_goal, local_context >>= mmap' (λ h, push_neg_at_hyp (local_pp_name h)) , try `[simp only [push_neg.not_eq] at * { eta := ff }] lemma imp_of_not_imp_not (P Q : Prop) [decidable Q] : (¬ Q → ¬ P) → (P → Q) := λ h hP, by_contradiction (λ h', h h' hP) /-- Matches either an identifier "h" or a pair of identifiers "h with k" -/ meta def name_with_opt : lean.parser (name × option name) := prod.mk <$> ident <*> (some <$> (tk "with" >> ident) <|> return none) /-- Transforms the goal into its contrapositive. `contrapose` turns a goal `P → Q` into `¬ Q → ¬ P` `contrapose!` turns a goal `P → Q` into `¬ Q → ¬ P` and pushes negations inside `P` and `Q` using `push_neg` `contrapose h` first reverts the local assumption `h`, and then uses `contrapose` and `intro h` `contrapose! h` first reverts the local assumption `h`, and then uses `contrapose!` and `intro h` `contrapose h with new_h` uses the name `new_h` for the introduced hypothesis. -/ meta def tactic.interactive.contrapose (push : parse (tk "!" )?) : parse name_with_opt? → tactic unit | (some (h, h')) := get_local h >>= revert >> tactic.interactive.contrapose none >> intro (h'.get_or_else h) >> skip | none := do `(%%P → %%Q) ← target | fail "The goal is not an implication, and you didn't specify an assumption", cp ← mk_mapp `imp_of_not_imp_not [P, Q, none] <|> fail "contrapose only applies to nondependent arrows between decidable props", apply cp, when push.is_some $ try (tactic.interactive.push_neg (loc.ns [none]))
13f7baa012a9170b5bfe057679a7f3bf629f3d2a
4efff1f47634ff19e2f786deadd394270a59ecd2
/src/category_theory/over.lean
084656472ba77cc7f8116365af34873d7e0f074a
[ "Apache-2.0" ]
permissive
agjftucker/mathlib
d634cd0d5256b6325e3c55bb7fb2403548371707
87fe50de17b00af533f72a102d0adefe4a2285e8
refs/heads/master
1,625,378,131,941
1,599,166,526,000
1,599,166,526,000
160,748,509
0
0
Apache-2.0
1,544,141,789,000
1,544,141,789,000
null
UTF-8
Lean
false
false
9,800
lean
/- Copyright (c) 2019 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Bhavik Mehta -/ import category_theory.comma import category_theory.punit import category_theory.reflects_isomorphisms /-! # Over and under categories Over (and under) categories are special cases of comma categories. * If `L` is the identity functor and `R` is a constant functor, then `comma L R` is the "slice" or "over" category over the object `R` maps to. * Conversely, if `L` is a constant functor and `R` is the identity functor, then `comma L R` is the "coslice" or "under" category under the object `L` maps to. ## Tags comma, slice, coslice, over, under -/ namespace category_theory universes v₁ v₂ u₁ u₂ -- declare the `v`'s first; see `category_theory.category` for an explanation variables {T : Type u₁} [category.{v₁} T] /-- The over category has as objects arrows in `T` with codomain `X` and as morphisms commutative triangles. -/ @[derive category] def over (X : T) := comma.{v₁ 0 v₁} (𝟭 T) (functor.from_punit X) -- Satisfying the inhabited linter instance over.inhabited [inhabited T] : inhabited (over (default T)) := { default := { left := default T, hom := 𝟙 _ } } namespace over variables {X : T} @[ext] lemma over_morphism.ext {X : T} {U V : over X} {f g : U ⟶ V} (h : f.left = g.left) : f = g := by tidy @[simp] lemma over_right (U : over X) : U.right = punit.star := by tidy @[simp] lemma id_left (U : over X) : comma_morphism.left (𝟙 U) = 𝟙 U.left := rfl @[simp] lemma comp_left (a b c : over X) (f : a ⟶ b) (g : b ⟶ c) : (f ≫ g).left = f.left ≫ g.left := rfl @[simp, reassoc] lemma w {A B : over X} (f : A ⟶ B) : f.left ≫ B.hom = A.hom := by have := f.w; tidy /-- To give an object in the over category, it suffices to give a morphism with codomain `X`. -/ @[simps] def mk {X Y : T} (f : Y ⟶ X) : over X := { left := Y, hom := f } /-- We can set up a coercion from arrows with codomain `X` to `over X`. This most likely should not be a global instance, but it is sometimes useful. -/ def coe_from_hom {X Y : T} : has_coe (Y ⟶ X) (over X) := { coe := mk } section local attribute [instance] coe_from_hom @[simp] lemma coe_hom {X Y : T} (f : Y ⟶ X) : (f : over X).hom = f := rfl end /-- To give a morphism in the over category, it suffices to give an arrow fitting in a commutative triangle. -/ @[simps] def hom_mk {U V : over X} (f : U.left ⟶ V.left) (w : f ≫ V.hom = U.hom . obviously) : U ⟶ V := { left := f } /-- Construct an isomorphism in the over category given isomorphisms of the objects whose forward direction gives a commutative triangle. -/ def iso_mk {f g : over X} (hl : f.left ≅ g.left) (hw : hl.hom ≫ g.hom = f.hom) : f ≅ g := comma.iso_mk hl (eq_to_iso (subsingleton.elim _ _)) (by simp [hw]) @[simp] lemma iso_mk_hom_left {f g : over X} (hl : f.left ≅ g.left) (hw : hl.hom ≫ g.hom = f.hom) : (iso_mk hl hw).hom.left = hl.hom := rfl @[simp] lemma iso_mk_inv_left {f g : over X} (hl : f.left ≅ g.left) (hw : hl.hom ≫ g.hom = f.hom) : (iso_mk hl hw).inv.left = hl.inv := rfl /-- The forgetful functor mapping an arrow to its domain. -/ def forget : over X ⥤ T := comma.fst _ _ @[simp] lemma forget_obj {U : over X} : forget.obj U = U.left := rfl @[simp] lemma forget_map {U V : over X} {f : U ⟶ V} : forget.map f = f.left := rfl /-- A morphism `f : X ⟶ Y` induces a functor `over X ⥤ over Y` in the obvious way. -/ def map {Y : T} (f : X ⟶ Y) : over X ⥤ over Y := comma.map_right _ $ discrete.nat_trans (λ _, f) section variables {Y : T} {f : X ⟶ Y} {U V : over X} {g : U ⟶ V} @[simp] lemma map_obj_left : ((map f).obj U).left = U.left := rfl @[simp] lemma map_obj_hom : ((map f).obj U).hom = U.hom ≫ f := rfl @[simp] lemma map_map_left : ((map f).map g).left = g.left := rfl /-- Mapping by the identity morphism is just the identity functor. -/ def map_id : map (𝟙 Y) ≅ 𝟭 _ := nat_iso.of_components (λ X, iso_mk (iso.refl _) (by tidy)) (by tidy) /-- Mapping by the composite morphism `f ≫ g` is the same as mapping by `f` then by `g`. -/ def map_comp {Y Z : T} (f : X ⟶ Y) (g : Y ⟶ Z) : map (f ≫ g) ≅ map f ⋙ map g := nat_iso.of_components (λ X, iso_mk (iso.refl _) (by tidy)) (by tidy) end instance forget_reflects_iso : reflects_isomorphisms (forget : over X ⥤ T) := { reflects := λ X Y f t, by exactI { inv := over.hom_mk t.inv ((as_iso (forget.map f)).inv_comp_eq.2 (over.w f).symm) } } section iterated_slice variables (f : over X) /-- Given f : Y ⟶ X, this is the obvious functor from (T/X)/f to T/Y -/ @[simps] def iterated_slice_forward : over f ⥤ over f.left := { obj := λ α, over.mk α.hom.left, map := λ α β κ, over.hom_mk κ.left.left (by { rw auto_param_eq, rw ← over.w κ, refl }) } /-- Given f : Y ⟶ X, this is the obvious functor from T/Y to (T/X)/f -/ @[simps] def iterated_slice_backward : over f.left ⥤ over f := { obj := λ g, mk (hom_mk g.hom : mk (g.hom ≫ f.hom) ⟶ f), map := λ g h α, hom_mk (hom_mk α.left (w_assoc α f.hom)) (over_morphism.ext (w α)) } /-- Given f : Y ⟶ X, we have an equivalence between (T/X)/f and T/Y -/ @[simps] def iterated_slice_equiv : over f ≌ over f.left := { functor := iterated_slice_forward f, inverse := iterated_slice_backward f, unit_iso := nat_iso.of_components (λ g, over.iso_mk (over.iso_mk (iso.refl _) (by tidy)) (by tidy)) (λ X Y g, by { ext, dsimp, simp }), counit_iso := nat_iso.of_components (λ g, over.iso_mk (iso.refl _) (by tidy)) (λ X Y g, by { ext, dsimp, simp }) } lemma iterated_slice_forward_forget : iterated_slice_forward f ⋙ forget = forget ⋙ forget := rfl lemma iterated_slice_backward_forget_forget : iterated_slice_backward f ⋙ forget ⋙ forget = forget := rfl end iterated_slice section variables {D : Type u₂} [category.{v₂} D] /-- A functor `F : T ⥤ D` induces a functor `over X ⥤ over (F.obj X)` in the obvious way. -/ @[simps] def post (F : T ⥤ D) : over X ⥤ over (F.obj X) := { obj := λ Y, mk $ F.map Y.hom, map := λ Y₁ Y₂ f, { left := F.map f.left, w' := by tidy; erw [← F.map_comp, w] } } end end over /-- The under category has as objects arrows with domain `X` and as morphisms commutative triangles. -/ @[derive category] def under (X : T) := comma.{0 v₁ v₁} (functor.from_punit X) (𝟭 T) -- Satisfying the inhabited linter instance under.inhabited [inhabited T] : inhabited (under (default T)) := { default := { right := default T, hom := 𝟙 _ } } namespace under variables {X : T} @[ext] lemma under_morphism.ext {X : T} {U V : under X} {f g : U ⟶ V} (h : f.right = g.right) : f = g := by tidy @[simp] lemma under_left (U : under X) : U.left = punit.star := by tidy @[simp] lemma id_right (U : under X) : comma_morphism.right (𝟙 U) = 𝟙 U.right := rfl @[simp] lemma comp_right (a b c : under X) (f : a ⟶ b) (g : b ⟶ c) : (f ≫ g).right = f.right ≫ g.right := rfl @[simp] lemma w {A B : under X} (f : A ⟶ B) : A.hom ≫ f.right = B.hom := by have := f.w; tidy /-- To give an object in the under category, it suffices to give an arrow with domain `X`. -/ @[simps] def mk {X Y : T} (f : X ⟶ Y) : under X := { right := Y, hom := f } /-- To give a morphism in the under category, it suffices to give a morphism fitting in a commutative triangle. -/ @[simps] def hom_mk {U V : under X} (f : U.right ⟶ V.right) (w : U.hom ≫ f = V.hom . obviously) : U ⟶ V := { right := f } /-- Construct an isomorphism in the over category given isomorphisms of the objects whose forward direction gives a commutative triangle. -/ def iso_mk {f g : under X} (hr : f.right ≅ g.right) (hw : f.hom ≫ hr.hom = g.hom) : f ≅ g := comma.iso_mk (eq_to_iso (subsingleton.elim _ _)) hr (by simp [hw]) @[simp] lemma iso_mk_hom_right {f g : under X} (hr : f.right ≅ g.right) (hw : f.hom ≫ hr.hom = g.hom) : (iso_mk hr hw).hom.right = hr.hom := rfl @[simp] lemma iso_mk_inv_right {f g : under X} (hr : f.right ≅ g.right) (hw : f.hom ≫ hr.hom = g.hom) : (iso_mk hr hw).inv.right = hr.inv := rfl /-- The forgetful functor mapping an arrow to its domain. -/ def forget : under X ⥤ T := comma.snd _ _ @[simp] lemma forget_obj {U : under X} : forget.obj U = U.right := rfl @[simp] lemma forget_map {U V : under X} {f : U ⟶ V} : forget.map f = f.right := rfl /-- A morphism `X ⟶ Y` induces a functor `under Y ⥤ under X` in the obvious way. -/ def map {Y : T} (f : X ⟶ Y) : under Y ⥤ under X := comma.map_left _ $ discrete.nat_trans (λ _, f) section variables {Y : T} {f : X ⟶ Y} {U V : under Y} {g : U ⟶ V} @[simp] lemma map_obj_right : ((map f).obj U).right = U.right := rfl @[simp] lemma map_obj_hom : ((map f).obj U).hom = f ≫ U.hom := rfl @[simp] lemma map_map_right : ((map f).map g).right = g.right := rfl /-- Mapping by the identity morphism is just the identity functor. -/ def map_id : map (𝟙 Y) ≅ 𝟭 _ := nat_iso.of_components (λ X, iso_mk (iso.refl _) (by tidy)) (by tidy) /-- Mapping by the composite morphism `f ≫ g` is the same as mapping by `f` then by `g`. -/ def map_comp {Y Z : T} (f : X ⟶ Y) (g : Y ⟶ Z) : map (f ≫ g) ≅ map g ⋙ map f := nat_iso.of_components (λ X, iso_mk (iso.refl _) (by tidy)) (by tidy) end section variables {D : Type u₂} [category.{v₂} D] /-- A functor `F : T ⥤ D` induces a functor `under X ⥤ under (F.obj X)` in the obvious way. -/ @[simps] def post {X : T} (F : T ⥤ D) : under X ⥤ under (F.obj X) := { obj := λ Y, mk $ F.map Y.hom, map := λ Y₁ Y₂ f, { right := F.map f.right, w' := by tidy; erw [← F.map_comp, w] } } end end under end category_theory
f7bded56f9af37897f53018ec18c2fa6424484be
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/category_theory/monoidal/functorial_auto.lean
dbfdb06140bb44642647000c6a2c16b45bd67ef7
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
5,705
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.category_theory.monoidal.functor import Mathlib.category_theory.functorial import Mathlib.PostPort universes v₁ v₂ u₁ u₂ l namespace Mathlib /-! # Unbundled lax monoidal functors ## Design considerations The essential problem I've encountered that requires unbundled functors is having an existing (non-monoidal) functor `F : C ⥤ D` between monoidal categories, and wanting to assert that it has an extension to a lax monoidal functor. The two options seem to be 1. Construct a separate `F' : lax_monoidal_functor C D`, and assert `F'.to_functor ≅ F`. 2. Introduce unbundled functors and unbundled lax monoidal functors, and construct `lax_monoidal F.obj`, then construct `F' := lax_monoidal_functor.of F.obj`. Both have costs, but as for option 2. the cost is in library design, while in option 1. the cost is users having to carry around additional isomorphisms forever, I wanted to introduce unbundled functors. TODO: later, we may want to do this for strong monoidal functors as well, but the immediate application, for enriched categories, only requires this notion. -/ namespace category_theory /-- An unbundled description of lax monoidal functors. -/ -- Perhaps in the future we'll redefine `lax_monoidal_functor` in terms of this, -- but that isn't the immediate plan. -- unit morphism class lax_monoidal {C : Type u₁} [category C] [monoidal_category C] {D : Type u₂} [category D] [monoidal_category D] (F : C → D) [functorial F] where ε : 𝟙_ ⟶ F 𝟙_ μ : (X Y : C) → F X ⊗ F Y ⟶ F (X ⊗ Y) μ_natural' : autoParam (∀ {X Y X' Y' : C} (f : X ⟶ Y) (g : X' ⟶ Y'), (map F f ⊗ map F g) ≫ μ Y Y' = μ X X' ≫ map F (f ⊗ g)) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.obviously") (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "obviously") []) associativity' : autoParam (∀ (X Y Z : C), (μ X Y ⊗ 𝟙) ≫ μ (X ⊗ Y) Z ≫ map F (iso.hom α_) = iso.hom α_ ≫ (𝟙 ⊗ μ Y Z) ≫ μ X (Y ⊗ Z)) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.obviously") (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "obviously") []) left_unitality' : autoParam (∀ (X : C), iso.hom λ_ = (ε ⊗ 𝟙) ≫ μ 𝟙_ X ≫ map F (iso.hom λ_)) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.obviously") (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "obviously") []) right_unitality' : autoParam (∀ (X : C), iso.hom ρ_ = (𝟙 ⊗ ε) ≫ μ X 𝟙_ ≫ map F (iso.hom ρ_)) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.obviously") (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "obviously") []) -- tensorator -- associativity of the tensorator -- unitality @[simp] theorem lax_monoidal.μ_natural {C : Type u₁} [category C] [monoidal_category C] {D : Type u₂} [category D] [monoidal_category D] {F : C → D} [functorial F] [c : lax_monoidal F] {X : C} {Y : C} {X' : C} {Y' : C} (f : X ⟶ Y) (g : X' ⟶ Y') : (map F f ⊗ map F g) ≫ lax_monoidal.μ F Y Y' = lax_monoidal.μ F X X' ≫ map F (f ⊗ g) := sorry theorem lax_monoidal.left_unitality {C : Type u₁} [category C] [monoidal_category C] {D : Type u₂} [category D] [monoidal_category D] {F : C → D} [functorial F] [c : lax_monoidal F] (X : C) : iso.hom λ_ = (lax_monoidal.ε F ⊗ 𝟙) ≫ lax_monoidal.μ F 𝟙_ X ≫ map F (iso.hom λ_) := sorry -- The unitality axioms cannot be used as simp lemmas because they require theorem lax_monoidal.right_unitality {C : Type u₁} [category C] [monoidal_category C] {D : Type u₂} [category D] [monoidal_category D] {F : C → D} [functorial F] [c : lax_monoidal F] (X : C) : iso.hom ρ_ = (𝟙 ⊗ lax_monoidal.ε F) ≫ lax_monoidal.μ F X 𝟙_ ≫ map F (iso.hom ρ_) := sorry -- higher-order matching to figure out the `F` and `X` from `F X`. @[simp] theorem lax_monoidal.associativity {C : Type u₁} [category C] [monoidal_category C] {D : Type u₂} [category D] [monoidal_category D] {F : C → D} [functorial F] [c : lax_monoidal F] (X : C) (Y : C) (Z : C) : (lax_monoidal.μ F X Y ⊗ 𝟙) ≫ lax_monoidal.μ F (X ⊗ Y) Z ≫ map F (iso.hom α_) = iso.hom α_ ≫ (𝟙 ⊗ lax_monoidal.μ F Y Z) ≫ lax_monoidal.μ F X (Y ⊗ Z) := sorry namespace lax_monoidal_functor /-- Construct a bundled `lax_monoidal_functor` from the object level function and `functorial` and `lax_monoidal` typeclasses. -/ @[simp] theorem of_μ {C : Type u₁} [category C] [monoidal_category C] {D : Type u₂} [category D] [monoidal_category D] (F : C → D) [I₁ : functorial F] [I₂ : lax_monoidal F] (X : C) (Y : C) : μ (of F) X Y = lax_monoidal.μ F X Y := Eq.refl (μ (of F) X Y) end lax_monoidal_functor protected instance lax_monoidal_functor.obj.lax_monoidal {C : Type u₁} [category C] [monoidal_category C] {D : Type u₂} [category D] [monoidal_category D] (F : lax_monoidal_functor C D) : lax_monoidal (functor.obj (lax_monoidal_functor.to_functor F)) := lax_monoidal.mk (lax_monoidal_functor.ε F) (lax_monoidal_functor.μ F) protected instance lax_monoidal_id {C : Type u₁} [category C] [monoidal_category C] : lax_monoidal id := lax_monoidal.mk 𝟙 fun (X Y : C) => 𝟙 end Mathlib
3728ad78068db78bdaef0246530d2ec7a8d3be8e
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/order/filter/pointwise.lean
a77cd4e374f60de8f2f61d8be5afa74d35a3038a
[ "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
6,682
lean
/- Copyright (c) 2019 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou -/ import algebra.pointwise import order.filter.basic /-! # Pointwise operations on filters. The pointwise operations on filters have nice properties, such as • `map m (f₁ * f₂) = map m f₁ * map m f₂` • `𝓝 x * 𝓝 y = 𝓝 (x * y)` -/ open classical set universes u v w variables {α : Type u} {β : Type v} {γ : Type w} open_locale classical pointwise namespace filter open set @[to_additive] instance [has_one α] : has_one (filter α) := ⟨principal 1⟩ @[simp, to_additive] lemma mem_one [has_one α] (s : set α) : s ∈ (1 : filter α) ↔ (1:α) ∈ s := calc s ∈ (1:filter α) ↔ 1 ⊆ s : iff.rfl ... ↔ (1 : α) ∈ s : by simp @[to_additive] instance [monoid α] : has_mul (filter α) := ⟨λf g, { sets := { s | ∃t₁ t₂, t₁ ∈ f ∧ t₂ ∈ g ∧ t₁ * t₂ ⊆ s }, univ_sets := begin have h₁ : (∃x, x ∈ f) := ⟨univ, univ_sets f⟩, have h₂ : (∃x, x ∈ g) := ⟨univ, univ_sets g⟩, simpa using and.intro h₁ h₂ end, sets_of_superset := λx y hx hxy, begin rcases hx with ⟨t₁, ht₁, t₂, ht₂, t₁t₂⟩, exact ⟨t₁, ht₁, t₂, ht₂, subset.trans t₁t₂ hxy⟩ end, inter_sets := λx y, begin simp only [exists_prop, mem_set_of_eq, subset_inter_iff], rintros ⟨s₁, s₂, hs₁, hs₂, s₁s₂⟩ ⟨t₁, t₂, ht₁, ht₂, t₁t₂⟩, exact ⟨s₁ ∩ t₁, s₂ ∩ t₂, inter_sets f hs₁ ht₁, inter_sets g hs₂ ht₂, subset.trans (mul_subset_mul (inter_subset_left _ _) (inter_subset_left _ _)) s₁s₂, subset.trans (mul_subset_mul (inter_subset_right _ _) (inter_subset_right _ _)) t₁t₂⟩, end }⟩ @[to_additive] lemma mem_mul [monoid α] {f g : filter α} {s : set α} : s ∈ f * g ↔ ∃t₁ t₂, t₁ ∈ f ∧ t₂ ∈ g ∧ t₁ * t₂ ⊆ s := iff.rfl @[to_additive] lemma mul_mem_mul [monoid α] {f g : filter α} {s t : set α} (hs : s ∈ f) (ht : t ∈ g) : s * t ∈ f * g := ⟨_, _, hs, ht, subset.refl _⟩ @[to_additive] protected lemma mul_le_mul [monoid α] {f₁ f₂ g₁ g₂ : filter α} (hf : f₁ ≤ f₂) (hg : g₁ ≤ g₂) : f₁ * g₁ ≤ f₂ * g₂ := assume _ ⟨s, t, hs, ht, hst⟩, ⟨s, t, hf hs, hg ht, hst⟩ @[to_additive] lemma ne_bot.mul [monoid α] {f g : filter α} : ne_bot f → ne_bot g → ne_bot (f * g) := begin simp only [forall_mem_nonempty_iff_ne_bot.symm], rintros hf hg s ⟨a, b, ha, hb, ab⟩, exact ((hf a ha).mul (hg b hb)).mono ab end @[to_additive] protected lemma mul_assoc [monoid α] (f g h : filter α) : f * g * h = f * (g * h) := begin ext s, split, { rintros ⟨a, b, ⟨a₁, a₂, ha₁, ha₂, a₁a₂⟩, hb, ab⟩, refine ⟨a₁, a₂ * b, ha₁, mul_mem_mul ha₂ hb, _⟩, rw [← mul_assoc], calc a₁ * a₂ * b ⊆ a * b : mul_subset_mul a₁a₂ (subset.refl _) ... ⊆ s : ab }, { rintros ⟨a, b, ha, ⟨b₁, b₂, hb₁, hb₂, b₁b₂⟩, ab⟩, refine ⟨a * b₁, b₂, mul_mem_mul ha hb₁, hb₂, _⟩, rw [mul_assoc], calc a * (b₁ * b₂) ⊆ a * b : mul_subset_mul (subset.refl _) b₁b₂ ... ⊆ s : ab } end @[to_additive] protected lemma one_mul [monoid α] (f : filter α) : 1 * f = f := begin ext s, split, { rintros ⟨t₁, t₂, ht₁, ht₂, t₁t₂⟩, refine mem_of_superset (mem_of_superset ht₂ _) t₁t₂, assume x hx, exact ⟨1, x, by rwa [← mem_one], hx, one_mul _⟩ }, { assume hs, refine ⟨(1:set α), s, mem_principal_self _, hs, by simp only [one_mul]⟩ } end @[to_additive] protected lemma mul_one [monoid α] (f : filter α) : f * 1 = f := begin ext s, split, { rintros ⟨t₁, t₂, ht₁, ht₂, t₁t₂⟩, refine mem_of_superset (mem_of_superset ht₁ _) t₁t₂, assume x hx, exact ⟨x, 1, hx, by rwa [← mem_one], mul_one _⟩ }, { assume hs, refine ⟨s, (1:set α), hs, mem_principal_self _, by simp only [mul_one]⟩ } end @[to_additive filter.add_monoid] instance [monoid α] : monoid (filter α) := { mul_assoc := filter.mul_assoc, one_mul := filter.one_mul, mul_one := filter.mul_one, .. filter.has_mul, .. filter.has_one } section map variables [monoid α] [monoid β] {f : filter α} (m : mul_hom α β) (φ : α →* β) @[to_additive] protected lemma map_mul {f₁ f₂ : filter α} : map m (f₁ * f₂) = map m f₁ * map m f₂ := begin ext s, simp only [mem_mul], split, { rintro ⟨t₁, t₂, ht₁, ht₂, t₁t₂⟩, have : m '' (t₁ * t₂) ⊆ s := subset.trans (image_subset m t₁t₂) (image_preimage_subset _ _), refine ⟨m '' t₁, m '' t₂, image_mem_map ht₁, image_mem_map ht₂, _⟩, rwa ← image_mul m }, { rintro ⟨t₁, t₂, ht₁, ht₂, t₁t₂⟩, refine ⟨m ⁻¹' t₁, m ⁻¹' t₂, ht₁, ht₂, image_subset_iff.1 _⟩, rw image_mul m, exact subset.trans (mul_subset_mul (image_preimage_subset _ _) (image_preimage_subset _ _)) t₁t₂ }, end @[to_additive] protected lemma map_one : map φ (1:filter α) = 1 := le_antisymm (le_principal_iff.2 $ mem_map_iff_exists_image.2 ⟨(1:set α), by simp, by { assume x, simp [φ.map_one] }⟩) (le_map $ assume s hs, begin simp only [mem_one], exact ⟨(1:α), (mem_one s).1 hs, φ.map_one⟩ end) /-- If `φ : α →* β` then `map_monoid_hom φ` is the monoid homomorphism `filter α →* filter β` induced by `map φ`. -/ @[to_additive "If `φ : α →+ β` then `map_add_monoid_hom φ` is the monoid homomorphism `filter α →+ filter β` induced by `map φ`."] def map_monoid_hom : filter α →* filter β := { to_fun := map φ, map_one' := filter.map_one φ, map_mul' := λ _ _, filter.map_mul φ.to_mul_hom } -- The other direction does not hold in general. @[to_additive] lemma comap_mul_comap_le {f₁ f₂ : filter β} : comap m f₁ * comap m f₂ ≤ comap m (f₁ * f₂) := begin rintros s ⟨t, ⟨t₁, t₂, ht₁, ht₂, t₁t₂⟩, mt⟩, refine ⟨m ⁻¹' t₁, m ⁻¹' t₂, ⟨t₁, ht₁, subset.refl _⟩, ⟨t₂, ht₂, subset.refl _⟩, _⟩, have := subset.trans (preimage_mono t₁t₂) mt, exact subset.trans (preimage_mul_preimage_subset _) this end @[to_additive] lemma tendsto.mul_mul {f₁ g₁ : filter α} {f₂ g₂ : filter β} : tendsto m f₁ f₂ → tendsto m g₁ g₂ → tendsto m (f₁ * g₁) (f₂ * g₂) := assume hf hg, by { rw [tendsto, filter.map_mul m], exact filter.mul_le_mul hf hg } end map end filter
48738fc1562d2d259bdae9dd5d5b70fe13ce8d51
1717bcbca047a0d25d687e7e9cd482fba00d058f
/src/measure_theory/interval_integral.lean
b958aaebfb59043bd52cbcbd185546f12a452bc9
[ "Apache-2.0" ]
permissive
swapnilkapoor22/mathlib
51ad5804e6a0635ed5c7611cee73e089ab271060
3e7efd4ecd5d379932a89212eebd362beb01309e
refs/heads/master
1,676,467,741,465
1,610,301,556,000
1,610,301,556,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
73,047
lean
/- Copyright (c) 2020 Yury G. Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Yury G. Kudryashov -/ import measure_theory.set_integral import measure_theory.lebesgue_measure import analysis.calculus.fderiv_measurable import analysis.calculus.mean_value /-! # Integral over an interval In this file we define `∫ x in a..b, f x ∂μ` to be `∫ x in Ioc a b, f x ∂μ` if `a ≤ b` and `-∫ x in Ioc b a, f x ∂μ` if `b ≤ a`. We prove a few simple properties and many versions of the first part of the [fundamental theorem of calculus](https://en.wikipedia.org/wiki/Fundamental_theorem_of_calculus). Recall that it states that the function `(u, v) ↦ ∫ x in u..v, f x` has derivative `(δu, δv) ↦ δv • f b - δu • f a` at `(a, b)` provided that `f` is continuous at `a` and `b`. ## Main statements ### FTC-1 for Lebesgue measure We prove several versions of FTC-1, all in the `interval_integral` namespace. Many of them follow the naming scheme `integral_has(_strict?)_(f?)deriv(_within?)_at(_of_tendsto_ae?)(_right|_left?)`. They formulate FTC in terms of `has(_strict?)_(f?)deriv(_within?)_at`. Let us explain the meaning of each part of the name: * `_strict` means that the theorem is about strict differentiability; * `f` means that the theorem is about differentiability in both endpoints; incompatible with `_right|_left`; * `_within` means that the theorem is about one-sided derivatives, see below for details; * `_of_tendsto_ae` means that instead of continuity the theorem assumes that `f` has a finite limit almost surely as `x` tends to `a` and/or `b`; * `_right` or `_left` mean that the theorem is about differentiability in the right (resp., left) endpoint. We also reformulate these theorems in terms of `(f?)deriv(_within?)`. These theorems are named `(f?)deriv(_within?)_integral(_of_tendsto_ae?)(_right|_left?)` with the same meaning of parts of the name. ### One-sided derivatives Theorem `integral_has_fderiv_within_at_of_tendsto_ae` states that `(u, v) ↦ ∫ x in u..v, f x` has a derivative `(δu, δv) ↦ δv • cb - δu • ca` within the set `s × t` at `(a, b)` provided that `f` tends to `ca` (resp., `cb`) almost surely at `la` (resp., `lb`), where possible values of `s`, `t`, and corresponding filters `la`, `lb` are given in the following table. | `s` | `la` | `t` | `lb` | | ------- | ---- | --- | ---- | | `Iic a` | `𝓝[Iic a] a` | `Iic b` | `𝓝[Iic b] b` | | `Ici a` | `𝓝[Ioi a] a` | `Ici b` | `𝓝[Ioi b] b` | | `{a}` | `⊥` | `{b}` | `⊥` | | `univ` | `𝓝 a` | `univ` | `𝓝 b` | We use a typeclass `FTC_filter` to make Lean automatically find `la`/`lb` based on `s`/`t`. This way we can formulate one theorem instead of `16` (or `8` if we leave only non-trivial ones not covered by `integral_has_deriv_within_at_of_tendsto_ae_(left|right)` and `integral_has_fderiv_at_of_tendsto_ae`). Similarly, `integral_has_deriv_within_at_of_tendsto_ae_right` works for both one-sided derivatives using the same typeclass to find an appropriate filter. ### FTC for a locally finite measure Before proving FTC for the Lebesgue measure, we prove a few statements that can be seen as FTC for any measure. The most general of them, `measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae`, states the following. Let `(la, la')` be an `FTC_filter` pair of filters around `a` (i.e., `FTC_filter a la la'`) and let `(lb, lb')` be an `FTC_filter` pair of filters around `b`. If `f` has finite limits `ca` and `cb` almost surely at `la'` and `lb'`, respectively, then `∫ x in va..vb, f x ∂μ - ∫ x in ua..ub, f x ∂μ = ∫ x in ub..vb, cb ∂μ - ∫ x in ua..va, ca ∂μ + o(∥∫ x in ua..va, (1:ℝ) ∂μ∥ + ∥∫ x in ub..vb, (1:ℝ) ∂μ∥)` as `ua` and `va` tend to `la` while `ub` and `vb` tend to `lb`. ## Implementation notes ### Avoiding `if`, `min`, and `max` In order to avoid `if`s in the definition, we define `interval_integrable f μ a b` as `integrable_on f (Ioc a b) μ ∧ integrable_on f (Ioc b a) μ`. For any `a`, `b` one of these intervals is empty and the other coincides with `Ioc (min a b) (max a b)`. Similarly, we define `∫ x in a..b, f x ∂μ` to be `∫ x in Ioc a b, f x ∂μ - ∫ x in Ioc b a, f x ∂μ`. Again, for any `a`, `b` one of these integrals is zero, and the other gives the expected result. This way some properties can be translated from integrals over sets without dealing with the cases `a ≤ b` and `b ≤ a` separately. ### Choice of the interval We use integral over `Ioc (min a b) (max a b)` instead of one of the other three possible intervals with the same endpoints for two reasons: * this way `∫ x in a..b, f x ∂μ + ∫ x in b..c, f x ∂μ = ∫ x in a..c, f x ∂μ` holds whenever `f` is integrable on each interval; in particular, it works even if the measure `μ` has an atom at `b`; this rules out `Ioo` and `Icc` intervals; * with this definition for a probability measure `μ`, the integral `∫ x in a..b, 1 ∂μ` equals the difference $F_μ(b)-F_μ(a)$, where $F_μ(a)=μ(-∞, a]$ is the [cumulative distribution function](https://en.wikipedia.org/wiki/Cumulative_distribution_function) of `μ`. ### `FTC_filter` class As explained above, many theorems in this file rely on the typeclass `FTC_filter (a : α) (l l' : filter α)` to avoid code duplication. This typeclass combines four assumptions: - `pure a ≤ l`; - `l' ≤ 𝓝 a`; - `l'` has a basis of measurable sets; - if `u n` and `v n` tend to `l`, then for any `s ∈ l'`, `Ioc (u n) (v n)` is eventually included in `s`. This typeclass has exactly four “real” instances: `(a, pure a, ⊥)`, `(a, 𝓝[Ici a] a, 𝓝[Ioi a] a)`, `(a, 𝓝[Iic a] a, 𝓝[Iic a] a)`, `(a, 𝓝 a, 𝓝 a)`, and two instances that are equal to the first and last “real” instances: `(a, 𝓝[{a}] a, ⊥)` and `(a, 𝓝[univ] a, 𝓝[univ] a)`. While the difference between `Ici a` and `Ioi a` doesn't matter for theorems about Lebesgue measure, it becomes important in the versions of FTC about any locally finite measure if this measure has an atom at one of the endpoints. ## Tags integral, fundamental theorem of calculus -/ noncomputable theory open topological_space (second_countable_topology) open measure_theory set classical filter open_locale classical topological_space filter variables {α β 𝕜 E F : Type*} [linear_order α] [measurable_space α] [measurable_space E] [normed_group E] /-! ### Integrability at an interval -/ /-- A function `f` is called *interval integrable* with respect to a measure `μ` on an unordered interval `a..b` if it is integrable on both intervals `(a, b]` and `(b, a]`. One of these intervals is always empty, so this property is equivalent to `f` being integrable on `(min a b, max a b]`. -/ def interval_integrable (f : α → E) (μ : measure α) (a b : α) := integrable_on f (Ioc a b) μ ∧ integrable_on f (Ioc b a) μ lemma measure_theory.integrable.interval_integrable {f : α → E} {μ : measure α} (hf : integrable f μ) {a b : α} : interval_integrable f μ a b := ⟨hf.integrable_on, hf.integrable_on⟩ namespace interval_integrable section variables {f : α → E} {a b c : α} {μ : measure α} @[symm] lemma symm (h : interval_integrable f μ a b) : interval_integrable f μ b a := h.symm @[refl] lemma refl : interval_integrable f μ a a := by split; simp @[trans] lemma trans (hab : interval_integrable f μ a b) (hbc : interval_integrable f μ b c) : interval_integrable f μ a c := ⟨(hab.1.union hbc.1).mono_set Ioc_subset_Ioc_union_Ioc, (hbc.2.union hab.2).mono_set Ioc_subset_Ioc_union_Ioc⟩ lemma neg [borel_space E] (h : interval_integrable f μ a b) : interval_integrable (-f) μ a b := ⟨h.1.neg, h.2.neg⟩ protected lemma ae_measurable (h : interval_integrable f μ a b) : ae_measurable f (μ.restrict (Ioc a b)):= h.1.ae_measurable protected lemma ae_measurable' (h : interval_integrable f μ a b) : ae_measurable f (μ.restrict (Ioc b a)):= h.2.ae_measurable end variables [borel_space E] {f g : α → E} {a b : α} {μ : measure α} lemma smul [normed_field 𝕜] [normed_space 𝕜 E] {f : α → E} {a b : α} {μ : measure α} (h : interval_integrable f μ a b) (r : 𝕜) : interval_integrable (r • f) μ a b := ⟨h.1.smul r, h.2.smul r⟩ lemma add [second_countable_topology E] (hf : interval_integrable f μ a b) (hg : interval_integrable g μ a b) : interval_integrable (f + g) μ a b := ⟨hf.1.add hg.1, hf.2.add hg.2⟩ lemma sub [second_countable_topology E] (hf : interval_integrable f μ a b) (hg : interval_integrable g μ a b) : interval_integrable (f - g) μ a b := ⟨hf.1.sub hg.1, hf.2.sub hg.2⟩ end interval_integrable section variables {μ : measure ℝ} [locally_finite_measure μ] lemma continuous_on.interval_integrable [borel_space E] {u : ℝ → E} {a b : ℝ} (hu : continuous_on u (interval a b)) : interval_integrable u μ a b := begin split, all_goals { refine measure_theory.integrable_on.mono_set _ Ioc_subset_Icc_self, refine continuous_on.integrable_on_compact compact_Icc (hu.mono _) }, exacts [Icc_subset_interval, Icc_subset_interval'] end lemma continuous_on.interval_integrable_of_Icc [borel_space E] {u : ℝ → E} {a b : ℝ} (h : a ≤ b) (hu : continuous_on u (Icc a b)) : interval_integrable u μ a b := continuous_on.interval_integrable ((interval_of_le h).symm ▸ hu) /-- A continuous function on `ℝ` is `interval_integrable` with respect to any locally finite measure `ν` on ℝ. -/ lemma continuous.interval_integrable [borel_space E] {u : ℝ → E} (hu : continuous u) (a b : ℝ) : interval_integrable u μ a b := hu.continuous_on.interval_integrable end /-- Let `l'` be a measurably generated filter; let `l` be a of filter such that each `s ∈ l'` eventually includes `Ioc u v` as both `u` and `v` tend to `l`. Let `μ` be a measure finite at `l'`. Suppose that `f : α → E` has a finite limit at `l' ⊓ μ.ae`. Then `f` is interval integrable on `u..v` provided that both `u` and `v` tend to `l`. Typeclass instances allow Lean to find `l'` based on `l` but not vice versa, so `apply tendsto.eventually_interval_integrable_ae` will generate goals `filter α` and `tendsto_Ixx_class Ioc ?m_1 l'`. -/ lemma filter.tendsto.eventually_interval_integrable_ae {f : α → E} {μ : measure α} (hfm : ae_measurable f μ) {l l' : filter α} [tendsto_Ixx_class Ioc l l'] [is_measurably_generated l'] (hμ : μ.finite_at_filter l') {c : E} (hf : tendsto f (l' ⊓ μ.ae) (𝓝 c)) {u v : β → α} {lt : filter β} (hu : tendsto u lt l) (hv : tendsto v lt l) : ∀ᶠ t in lt, interval_integrable f μ (u t) (v t) := have _ := (hf.integrable_at_filter_ae hfm hμ).eventually, ((hu.Ioc hv).eventually this).and $ (hv.Ioc hu).eventually this /-- Let `l'` be a measurably generated filter; let `l` be a of filter such that each `s ∈ l'` eventually includes `Ioc u v` as both `u` and `v` tend to `l`. Let `μ` be a measure finite at `l'`. Suppose that `f : α → E` has a finite limit at `l`. Then `f` is interval integrable on `u..v` provided that both `u` and `v` tend to `l`. Typeclass instances allow Lean to find `l'` based on `l` but not vice versa, so `apply tendsto.eventually_interval_integrable_ae` will generate goals `filter α` and `tendsto_Ixx_class Ioc ?m_1 l'`. -/ lemma filter.tendsto.eventually_interval_integrable {f : α → E} {μ : measure α} (hfm : ae_measurable f μ) {l l' : filter α} [tendsto_Ixx_class Ioc l l'] [is_measurably_generated l'] (hμ : μ.finite_at_filter l') {c : E} (hf : tendsto f l' (𝓝 c)) {u v : β → α} {lt : filter β} (hu : tendsto u lt l) (hv : tendsto v lt l) : ∀ᶠ t in lt, interval_integrable f μ (u t) (v t) := (hf.mono_left inf_le_left).eventually_interval_integrable_ae hfm hμ hu hv /-! ### Interval integral: definition and basic properties In this section we define `∫ x in a..b, f x ∂μ` as `∫ x in Ioc a b, f x ∂μ - ∫ x in Ioc b a, f x ∂μ` and prove some basic properties. -/ variables [second_countable_topology E] [complete_space E] [normed_space ℝ E] [borel_space E] /-- The interval integral `∫ x in a..b, f x ∂μ` is defined as `∫ x in Ioc a b, f x ∂μ - ∫ x in Ioc b a, f x ∂μ`. If `a ≤ b`, then it equals `∫ x in Ioc a b, f x ∂μ`, otherwise it equals `-∫ x in Ioc b a, f x ∂μ`. -/ def interval_integral (f : α → E) (a b : α) (μ : measure α) := ∫ x in Ioc a b, f x ∂μ - ∫ x in Ioc b a, f x ∂μ notation `∫` binders ` in ` a `..` b `, ` r:(scoped:60 f, f) ` ∂` μ:70 := interval_integral r a b μ notation `∫` binders ` in ` a `..` b `, ` r:(scoped:60 f, interval_integral f a b volume) := r namespace interval_integral section variables {a b c d : α} {f g : α → E} {μ : measure α} @[simp] lemma integral_zero : ∫ x in a..b, (0 : E) ∂μ = 0 := by simp [interval_integral] lemma integral_of_le (h : a ≤ b) : ∫ x in a..b, f x ∂μ = ∫ x in Ioc a b, f x ∂μ := by simp [interval_integral, h] @[simp] lemma integral_same : ∫ x in a..a, f x ∂μ = 0 := sub_self _ lemma integral_symm (a b) : ∫ x in b..a, f x ∂μ = -∫ x in a..b, f x ∂μ := by simp only [interval_integral, neg_sub] lemma integral_of_ge (h : b ≤ a) : ∫ x in a..b, f x ∂μ = -∫ x in Ioc b a, f x ∂μ := by simp only [integral_symm b, integral_of_le h] lemma integral_cases (f : α → E) (a b) : ∫ x in a..b, f x ∂μ ∈ ({∫ x in Ioc (min a b) (max a b), f x ∂μ, -∫ x in Ioc (min a b) (max a b), f x ∂μ} : set E) := (le_total a b).imp (λ h, by simp [h, integral_of_le]) (λ h, by simp [h, integral_of_ge]) lemma integral_non_ae_measurable {f : α → E} {a b} (h : a < b) (hf : ¬ ae_measurable f (μ.restrict (Ioc a b))) : ∫ x in a..b, f x ∂μ = 0 := by rw [integral_of_le h.le, integral_non_ae_measurable hf] lemma norm_integral_eq_norm_integral_Ioc : ∥∫ x in a..b, f x ∂μ∥ = ∥∫ x in Ioc (min a b) (max a b), f x ∂μ∥ := (integral_cases f a b).elim (congr_arg _) (λ h, (congr_arg _ h).trans (norm_neg _)) lemma norm_integral_le_integral_norm_Ioc : ∥∫ x in a..b, f x ∂μ∥ ≤ ∫ x in Ioc (min a b) (max a b), ∥f x∥ ∂μ := calc ∥∫ x in a..b, f x ∂μ∥ = ∥∫ x in Ioc (min a b) (max a b), f x ∂μ∥ : norm_integral_eq_norm_integral_Ioc ... ≤ ∫ x in Ioc (min a b) (max a b), ∥f x∥ ∂μ : norm_integral_le_integral_norm f lemma norm_integral_le_abs_integral_norm : ∥∫ x in a..b, f x ∂μ∥ ≤ abs (∫ x in a..b, ∥f x∥ ∂μ) := begin simp only [← real.norm_eq_abs, norm_integral_eq_norm_integral_Ioc], exact le_trans (norm_integral_le_integral_norm _) (le_abs_self _) end lemma norm_integral_le_of_norm_le_const_ae {a b C : ℝ} {f : ℝ → E} (h : ∀ᵐ x, x ∈ Ioc (min a b) (max a b) → ∥f x∥ ≤ C) : ∥∫ x in a..b, f x∥ ≤ C * abs (b - a) := begin rw [norm_integral_eq_norm_integral_Ioc], convert norm_set_integral_le_of_norm_le_const_ae'' _ is_measurable_Ioc h, { rw [real.volume_Ioc, max_sub_min_eq_abs, ennreal.to_real_of_real (abs_nonneg _)] }, { simp only [real.volume_Ioc, ennreal.of_real_lt_top] }, end lemma norm_integral_le_of_norm_le_const {a b C : ℝ} {f : ℝ → E} (h : ∀ x ∈ Ioc (min a b) (max a b), ∥f x∥ ≤ C) : ∥∫ x in a..b, f x∥ ≤ C * abs (b - a) := norm_integral_le_of_norm_le_const_ae $ eventually_of_forall h lemma integral_add (hf : interval_integrable f μ a b) (hg : interval_integrable g μ a b) : ∫ x in a..b, f x + g x ∂μ = ∫ x in a..b, f x ∂μ + ∫ x in a..b, g x ∂μ := by { simp only [interval_integral, integral_add hf.1 hg.1, integral_add hf.2 hg.2], abel } @[simp] lemma integral_neg : ∫ x in a..b, -f x ∂μ = -∫ x in a..b, f x ∂μ := by { simp only [interval_integral, integral_neg], abel } lemma integral_sub (hf : interval_integrable f μ a b) (hg : interval_integrable g μ a b) : ∫ x in a..b, f x - g x ∂μ = ∫ x in a..b, f x ∂μ - ∫ x in a..b, g x ∂μ := by simpa only [sub_eq_add_neg] using (integral_add hf hg.neg).trans (congr_arg _ integral_neg) lemma integral_smul (r : ℝ) : ∫ x in a..b, r • f x ∂μ = r • ∫ x in a..b, f x ∂μ := by simp only [interval_integral, integral_smul, smul_sub] lemma integral_const' (c : E) : ∫ x in a..b, c ∂μ = ((μ $ Ioc a b).to_real - (μ $ Ioc b a).to_real) • c := by simp only [interval_integral, set_integral_const, sub_smul] lemma integral_const {a b : ℝ} (c : E) : (∫ (x : ℝ) in a..b, c) = (b - a) • c := by simp only [integral_const', real.volume_Ioc, ennreal.to_real_of_real', ← neg_sub b, max_zero_sub_eq_self] lemma integral_smul_measure (c : ennreal) : ∫ x in a..b, f x ∂(c • μ) = c.to_real • ∫ x in a..b, f x ∂μ := by simp only [interval_integral, measure.restrict_smul, integral_smul_measure, smul_sub] lemma integral_comp_add_right (a b c : ℝ) (f : ℝ → E) (hfm : ae_measurable f) : ∫ x in a..b, f (x + c) = ∫ x in a+c..b+c, f x := have A : ae_measurable f (measure.map (λ x, x + c) volume), by rwa [real.map_volume_add_right], calc ∫ x in a..b, f (x + c) = ∫ x in a+c..b+c, f x ∂(measure.map (λ x, x + c) volume) : by simp only [interval_integral, set_integral_map is_measurable_Ioc A (measurable_add_right _), preimage_add_const_Ioc, add_sub_cancel] ... = ∫ x in a+c..b+c, f x : by rw [real.map_volume_add_right] lemma integral_comp_mul_right {c : ℝ} (hc : 0 < c) (a b : ℝ) (f : ℝ → E) (hfm : ae_measurable f) : ∫ x in a..b, f (x * c) = c⁻¹ • ∫ x in a*c..b*c, f x := begin have A : ae_measurable f (measure.map (λ (x : ℝ), x*c) volume), by { rw real.map_volume_mul_right (ne_of_gt hc), exact hfm.smul_measure _ }, conv_rhs { rw [← real.smul_map_volume_mul_right (ne_of_gt hc)] }, rw [integral_smul_measure], simp only [interval_integral, set_integral_map is_measurable_Ioc A (measurable_mul_right _), hc, preimage_mul_const_Ioc, mul_div_cancel _ (ne_of_gt hc), abs_of_pos, ennreal.to_real_of_real (le_of_lt hc), inv_smul_smul' (ne_of_gt hc)], end lemma integral_comp_neg (a b : ℝ) (f : ℝ → E) (hfm : ae_measurable f) : ∫ x in a..b, f (-x) = ∫ x in -b..-a, f x := begin have A : ae_measurable f (measure.map (λ (x : ℝ), -x) volume), by rwa real.map_volume_neg, conv_rhs { rw ← real.map_volume_neg }, simp only [interval_integral, set_integral_map is_measurable_Ioc A measurable_neg, neg_preimage, preimage_neg_Ioc, neg_neg, restrict_congr_set Ico_ae_eq_Ioc] end /-! ### Integral is an additive function of the interval In this section we prove that `∫ x in a..b, f x ∂μ + ∫ x in b..c, f x ∂μ = ∫ x in a..c, f x ∂μ` as well as a few other identities trivially equivalent to this one. We also prove that `∫ x in a..b, f x ∂μ = ∫ x, f x ∂μ` provided that `support f ⊆ Ioc a b`. -/ variables [topological_space α] [opens_measurable_space α] section order_closed_topology variables [order_closed_topology α] lemma integral_add_adjacent_intervals_cancel (hab : interval_integrable f μ a b) (hbc : interval_integrable f μ b c) : ∫ x in a..b, f x ∂μ + ∫ x in b..c, f x ∂μ + ∫ x in c..a, f x ∂μ = 0 := begin have hac := hab.trans hbc, simp only [interval_integral, ← add_sub_comm, sub_eq_zero], iterate 4 { rw ← integral_union }, { suffices : Ioc a b ∪ Ioc b c ∪ Ioc c a = Ioc b a ∪ Ioc c b ∪ Ioc a c, by rw this, rw [Ioc_union_Ioc_union_Ioc_cycle, union_right_comm, Ioc_union_Ioc_union_Ioc_cycle, min_left_comm, max_left_comm] }, all_goals { simp [*, is_measurable.union, is_measurable_Ioc, Ioc_disjoint_Ioc_same, Ioc_disjoint_Ioc_same.symm, hab.1, hab.2, hbc.1, hbc.2, hac.1, hac.2] } end lemma integral_add_adjacent_intervals (hab : interval_integrable f μ a b) (hbc : interval_integrable f μ b c) : ∫ x in a..b, f x ∂μ + ∫ x in b..c, f x ∂μ = ∫ x in a..c, f x ∂μ := by rw [← add_neg_eq_zero, ← integral_symm, integral_add_adjacent_intervals_cancel hab hbc] lemma integral_interval_sub_left (hab : interval_integrable f μ a b) (hac : interval_integrable f μ a c) : ∫ x in a..b, f x ∂μ - ∫ x in a..c, f x ∂μ = ∫ x in c..b, f x ∂μ := sub_eq_of_eq_add' $ eq.symm $ integral_add_adjacent_intervals hac (hac.symm.trans hab) lemma integral_interval_add_interval_comm (hab : interval_integrable f μ a b) (hcd : interval_integrable f μ c d) (hac : interval_integrable f μ a c) : ∫ x in a..b, f x ∂μ + ∫ x in c..d, f x ∂μ = ∫ x in a..d, f x ∂μ + ∫ x in c..b, f x ∂μ := by rw [← integral_add_adjacent_intervals hac hcd, add_assoc, add_left_comm, integral_add_adjacent_intervals hac (hac.symm.trans hab), add_comm] lemma integral_interval_sub_interval_comm (hab : interval_integrable f μ a b) (hcd : interval_integrable f μ c d) (hac : interval_integrable f μ a c) : ∫ x in a..b, f x ∂μ - ∫ x in c..d, f x ∂μ = ∫ x in a..c, f x ∂μ - ∫ x in b..d, f x ∂μ := by simp only [sub_eq_add_neg, ← integral_symm, integral_interval_add_interval_comm hab hcd.symm (hac.trans hcd)] lemma integral_interval_sub_interval_comm' (hab : interval_integrable f μ a b) (hcd : interval_integrable f μ c d) (hac : interval_integrable f μ a c) : ∫ x in a..b, f x ∂μ - ∫ x in c..d, f x ∂μ = ∫ x in d..b, f x ∂μ - ∫ x in c..a, f x ∂μ := by { rw [integral_interval_sub_interval_comm hab hcd hac, integral_symm b d, integral_symm a c, sub_neg_eq_add, sub_eq_neg_add], } lemma integral_Iic_sub_Iic (ha : integrable_on f (Iic a) μ) (hb : integrable_on f (Iic b) μ) : ∫ x in Iic b, f x ∂μ - ∫ x in Iic a, f x ∂μ = ∫ x in a..b, f x ∂μ := begin wlog hab : a ≤ b using [a b] tactic.skip, { rw [sub_eq_iff_eq_add', integral_of_le hab, ← integral_union (Iic_disjoint_Ioc (le_refl _)), Iic_union_Ioc_eq_Iic hab], exacts [is_measurable_Iic, is_measurable_Ioc, ha, hb.mono_set (λ _, and.right)] }, { intros ha hb, rw [integral_symm, ← this hb ha, neg_sub] } end /-- If `μ` is a finite measure then `∫ x in a..b, c ∂μ = (μ (Iic b) - μ (Iic a)) • c`. -/ lemma integral_const_of_cdf [finite_measure μ] (c : E) : ∫ x in a..b, c ∂μ = ((μ (Iic b)).to_real - (μ (Iic a)).to_real) • c := begin simp only [sub_smul, ← set_integral_const], refine (integral_Iic_sub_Iic _ _).symm; simp only [integrable_on_const, measure_lt_top, or_true] end lemma integral_eq_integral_of_support_subset {f : α → E} {a b} (h : function.support f ⊆ Ioc a b) : ∫ x in a..b, f x ∂μ = ∫ x, f x ∂μ := begin cases le_total a b with hab hab, { rw [integral_of_le hab, ← integral_indicator is_measurable_Ioc, indicator_eq_self.2 h]; apply_instance }, { rw [Ioc_eq_empty hab, subset_empty_iff, function.support_eq_empty_iff] at h, simp [h] } end end order_closed_topology end lemma integral_eq_zero_iff_of_le_of_nonneg_ae {f : ℝ → ℝ} {a b : ℝ} (hab : a ≤ b) (hf : 0 ≤ᵐ[volume.restrict (Ioc a b)] f) (hfi : interval_integrable f volume a b) : ∫ x in a..b, f x = 0 ↔ f =ᵐ[volume.restrict (Ioc a b)] 0 := by rw [integral_of_le hab, integral_eq_zero_iff_of_nonneg_ae hf hfi.1] lemma integral_eq_zero_iff_of_nonneg_ae {f : ℝ → ℝ} {a b : ℝ} (hf : 0 ≤ᵐ[volume.restrict (Ioc a b ∪ Ioc b a)] f) (hfi : interval_integrable f volume a b) : ∫ x in a..b, f x = 0 ↔ f =ᵐ[volume.restrict (Ioc a b ∪ Ioc b a)] 0 := begin cases le_total a b with hab hab; simp only [Ioc_eq_empty hab, empty_union, union_empty] at *, { exact integral_eq_zero_iff_of_le_of_nonneg_ae hab hf hfi }, { rw [integral_symm, neg_eq_zero], exact integral_eq_zero_iff_of_le_of_nonneg_ae hab hf hfi.symm } end lemma integral_pos_iff_support_of_nonneg_ae' {f : ℝ → ℝ} {a b : ℝ} (hf : 0 ≤ᵐ[volume.restrict (Ioc a b ∪ Ioc b a)] f) (hfi : interval_integrable f volume a b) : 0 < ∫ x in a..b, f x ↔ a < b ∧ 0 < volume (function.support f ∩ Ioc a b) := begin cases le_total a b with hab hab, { simp only [integral_of_le hab, Ioc_eq_empty hab, union_empty] at hf ⊢, symmetry, rw [set_integral_pos_iff_support_of_nonneg_ae hf hfi.1, and_iff_right_iff_imp], contrapose!, intro h, simp [Ioc_eq_empty h] }, { rw [Ioc_eq_empty hab, empty_union] at hf, simp [integral_of_ge hab, Ioc_eq_empty hab, integral_nonneg_of_ae hf] } end lemma integral_pos_iff_support_of_nonneg_ae {f : ℝ → ℝ} {a b : ℝ} (hf : 0 ≤ᵐ[volume] f) (hfi : interval_integrable f volume a b) : 0 < ∫ x in a..b, f x ↔ a < b ∧ 0 < volume (function.support f ∩ Ioc a b) := integral_pos_iff_support_of_nonneg_ae' (ae_mono measure.restrict_le_self hf) hfi /-! ### Fundamental theorem of calculus, part 1, for any measure In this section we prove a few lemmas that can be seen as versions of FTC-1 for interval integrals w.r.t. any measure. Many theorems are formulated for one or two pairs of filters related by `FTC_filter a l l'`. This typeclass has exactly four “real” instances: `(a, pure a, ⊥)`, `(a, 𝓝[Ici a] a, 𝓝[Ioi a] a)`, `(a, 𝓝[Iic a] a, 𝓝[Iic a] a)`, `(a, 𝓝 a, 𝓝 a)`, and two instances that are equal to the first and last “real” instances: `(a, 𝓝[{a}] a, ⊥)` and `(a, 𝓝[univ] a, 𝓝[univ] a)`. We use this approach to avoid repeating arguments in many very similar cases. Lean can automatically find both `a` and `l'` based on `l`. The most general theorem `measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae` can be seen as a generalization of lemma `integral_has_strict_fderiv_at` below which states strict differentiability of `∫ x in u..v, f x` in `(u, v)` at `(a, b)` for a measurable function `f` that is integrable on `a..b` and is continuous at `a` and `b`. The lemma is generalized in three directions: first, `measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae` deals with any locally finite measure `μ`; second, it works for one-sided limits/derivatives; third, it assumes only that `f` has finite limits almost surely at `a` and `b`. Namely, let `f` be a measurable function integrable on `a..b`. Let `(la, la')` be a pair of `FTC_filter`s around `a`; let `(lb, lb')` be a pair of `FTC_filter`s around `b`. Suppose that `f` has finite limits `ca` and `cb` at `la' ⊓ μ.ae` and `lb' ⊓ μ.ae`, respectively. Then `∫ x in va..vb, f x ∂μ - ∫ x in ua..ub, f x ∂μ = ∫ x in ub..vb, cb ∂μ - ∫ x in ua..va, ca ∂μ + o(∥∫ x in ua..va, (1:ℝ) ∂μ∥ + ∥∫ x in ub..vb, (1:ℝ) ∂μ∥)` as `ua` and `va` tend to `la` while `ub` and `vb` tend to `lb`. This theorem is formulated with integral of constants instead of measures in the right hand sides for two reasons: first, this way we avoid `min`/`max` in the statements; second, often it is possible to write better `simp` lemmas for these integrals, see `integral_const` and `integral_const_of_cdf`. In the next subsection we apply this theorem to prove various theorems about differentiability of the integral w.r.t. Lebesgue measure. -/ /-- An auxiliary typeclass for the Fundamental theorem of calculus, part 1. It is used to formulate theorems that work simultaneously for left and right one-sided derivatives of `∫ x in u..v, f x`. There are four instances: `(a, pure a, ⊥)`, `(a, 𝓝[Ici a], 𝓝[Ioi a])`, `(a, 𝓝[Iic a], 𝓝[Iic a])`, and `(a, 𝓝 a, 𝓝 a)`. -/ class FTC_filter {β : Type*} [linear_order β] [measurable_space β] [topological_space β] (a : out_param β) (outer : filter β) (inner : out_param $ filter β) extends tendsto_Ixx_class Ioc outer inner : Prop := (pure_le : pure a ≤ outer) (le_nhds : inner ≤ 𝓝 a) [meas_gen : is_measurably_generated inner] /- The `dangerous_instance` linter doesn't take `out_param`s into account, so it thinks that `FTC_filter.to_tendsto_Ixx_class` is dangerous. Disable this linter using `nolint`. -/ attribute [nolint dangerous_instance] FTC_filter.to_tendsto_Ixx_class namespace FTC_filter variables [linear_order β] [measurable_space β] [topological_space β] instance pure (a : β) : FTC_filter a (pure a) ⊥ := { pure_le := le_refl _, le_nhds := bot_le } instance nhds_within_singleton (a : β) : FTC_filter a (𝓝[{a}] a) ⊥ := by { rw [nhds_within, principal_singleton, inf_eq_right.2 (pure_le_nhds a)], apply_instance } lemma finite_at_inner {a : β} (l : filter β) {l'} [h : FTC_filter a l l'] {μ : measure β} [locally_finite_measure μ] : μ.finite_at_filter l' := (μ.finite_at_nhds a).filter_mono h.le_nhds variables [opens_measurable_space β] [order_topology β] instance nhds (a : β) : FTC_filter a (𝓝 a) (𝓝 a) := { pure_le := pure_le_nhds a, le_nhds := le_refl _ } instance nhds_univ (a : β) : FTC_filter a (𝓝[univ] a) (𝓝 a) := by { rw nhds_within_univ, apply_instance } instance nhds_left (a : β) : FTC_filter a (𝓝[Iic a] a) (𝓝[Iic a] a) := { pure_le := pure_le_nhds_within right_mem_Iic, le_nhds := inf_le_left } instance nhds_right (a : β) : FTC_filter a (𝓝[Ici a] a) (𝓝[Ioi a] a) := { pure_le := pure_le_nhds_within left_mem_Ici, le_nhds := inf_le_left } end FTC_filter open asymptotics section variables {f : α → E} {a b : α} {c ca cb : E} {l l' la la' lb lb' : filter α} {lt : filter β} {μ : measure α} {u v ua va ub vb : β → α} /-- Fundamental theorem of calculus-1, local version for any measure. Let filters `l` and `l'` be related by `tendsto_Ixx_class Ioc`. If `f` has a finite limit `c` at `l' ⊓ μ.ae`, where `μ` is a measure finite at `l'`, then `∫ x in u..v, f x ∂μ = ∫ x in u..v, c ∂μ + o(∫ x in u..v, 1 ∂μ)` as both `u` and `v` tend to `l`. See also `measure_integral_sub_linear_is_o_of_tendsto_ae` for a version assuming `[FTC_filter a l l']` and `[locally_finite_measure μ]`. If `l` is one of `𝓝[Ici a] a`, `𝓝[Iic a] a`, `𝓝 a`, then it's easier to apply the non-primed version. The primed version also works, e.g., for `l = l' = at_top`. We use integrals of constants instead of measures because this way it is easier to formulate a statement that works in both cases `u ≤ v` and `v ≤ u`. -/ lemma measure_integral_sub_linear_is_o_of_tendsto_ae' [is_measurably_generated l'] [tendsto_Ixx_class Ioc l l'] (hfm : ae_measurable f μ) (hf : tendsto f (l' ⊓ μ.ae) (𝓝 c)) (hl : μ.finite_at_filter l') (hu : tendsto u lt l) (hv : tendsto v lt l) : is_o (λ t, ∫ x in u t..v t, f x ∂μ - ∫ x in u t..v t, c ∂μ) (λ t, ∫ x in u t..v t, (1:ℝ) ∂μ) lt := begin have A := (hf.integral_sub_linear_is_o_ae hfm hl).comp_tendsto (hu.Ioc hv), have B := (hf.integral_sub_linear_is_o_ae hfm hl).comp_tendsto (hv.Ioc hu), simp only [integral_const'], convert (A.trans_le _).sub (B.trans_le _), { ext t, simp_rw [(∘), interval_integral, sub_smul], abel }, all_goals { intro t, cases le_total (u t) (v t) with huv huv; simp [huv] } end /-- Fundamental theorem of calculus-1, local version for any measure. Let filters `l` and `l'` be related by `tendsto_Ixx_class Ioc`. If `f` has a finite limit `c` at `l ⊓ μ.ae`, where `μ` is a measure finite at `l`, then `∫ x in u..v, f x ∂μ = μ (Ioc u v) • c + o(μ(Ioc u v))` as both `u` and `v` tend to `l` so that `u ≤ v`. See also `measure_integral_sub_linear_is_o_of_tendsto_ae_of_le` for a version assuming `[FTC_filter a l l']` and `[locally_finite_measure μ]`. If `l` is one of `𝓝[Ici a] a`, `𝓝[Iic a] a`, `𝓝 a`, then it's easier to apply the non-primed version. The primed version also works, e.g., for `l = l' = at_top`. -/ lemma measure_integral_sub_linear_is_o_of_tendsto_ae_of_le' [is_measurably_generated l'] [tendsto_Ixx_class Ioc l l'] (hfm : ae_measurable f μ) (hf : tendsto f (l' ⊓ μ.ae) (𝓝 c)) (hl : μ.finite_at_filter l') (hu : tendsto u lt l) (hv : tendsto v lt l) (huv : u ≤ᶠ[lt] v) : is_o (λ t, ∫ x in u t..v t, f x ∂μ - (μ (Ioc (u t) (v t))).to_real • c) (λ t, (μ $ Ioc (u t) (v t)).to_real) lt := (measure_integral_sub_linear_is_o_of_tendsto_ae' hfm hf hl hu hv).congr' (huv.mono $ λ x hx, by simp [integral_const', hx]) (huv.mono $ λ x hx, by simp [integral_const', hx]) /-- Fundamental theorem of calculus-1, local version for any measure. Let filters `l` and `l'` be related by `tendsto_Ixx_class Ioc`. If `f` has a finite limit `c` at `l ⊓ μ.ae`, where `μ` is a measure finite at `l`, then `∫ x in u..v, f x ∂μ = -μ (Ioc v u) • c + o(μ(Ioc v u))` as both `u` and `v` tend to `l` so that `v ≤ u`. See also `measure_integral_sub_linear_is_o_of_tendsto_ae_of_ge` for a version assuming `[FTC_filter a l l']` and `[locally_finite_measure μ]`. If `l` is one of `𝓝[Ici a] a`, `𝓝[Iic a] a`, `𝓝 a`, then it's easier to apply the non-primed version. The primed version also works, e.g., for `l = l' = at_top`. -/ lemma measure_integral_sub_linear_is_o_of_tendsto_ae_of_ge' [is_measurably_generated l'] [tendsto_Ixx_class Ioc l l'] (hfm : ae_measurable f μ) (hf : tendsto f (l' ⊓ μ.ae) (𝓝 c)) (hl : μ.finite_at_filter l') (hu : tendsto u lt l) (hv : tendsto v lt l) (huv : v ≤ᶠ[lt] u) : is_o (λ t, ∫ x in u t..v t, f x ∂μ + (μ (Ioc (v t) (u t))).to_real • c) (λ t, (μ $ Ioc (v t) (u t)).to_real) lt := (measure_integral_sub_linear_is_o_of_tendsto_ae_of_le' hfm hf hl hv hu huv).neg_left.congr_left $ λ t, by simp [integral_symm (u t), add_comm] variables [topological_space α] section variables [locally_finite_measure μ] [FTC_filter a l l'] include a local attribute [instance] FTC_filter.meas_gen /-- Fundamental theorem of calculus-1, local version for any measure. Let filters `l` and `l'` be related by `[FTC_filter a l l']`; let `μ` be a locally finite measure. If `f` has a finite limit `c` at `l' ⊓ μ.ae`, then `∫ x in u..v, f x ∂μ = ∫ x in u..v, c ∂μ + o(∫ x in u..v, 1 ∂μ)` as both `u` and `v` tend to `l`. See also `measure_integral_sub_linear_is_o_of_tendsto_ae'` for a version that also works, e.g., for `l = l' = at_top`. We use integrals of constants instead of measures because this way it is easier to formulate a statement that works in both cases `u ≤ v` and `v ≤ u`. -/ lemma measure_integral_sub_linear_is_o_of_tendsto_ae (hfm : ae_measurable f μ) (hf : tendsto f (l' ⊓ μ.ae) (𝓝 c)) (hu : tendsto u lt l) (hv : tendsto v lt l) : is_o (λ t, ∫ x in u t..v t, f x ∂μ - ∫ x in u t..v t, c ∂μ) (λ t, ∫ x in u t..v t, (1:ℝ) ∂μ) lt := measure_integral_sub_linear_is_o_of_tendsto_ae' hfm hf (FTC_filter.finite_at_inner l) hu hv /-- Fundamental theorem of calculus-1, local version for any measure. Let filters `l` and `l'` be related by `[FTC_filter a l l']`; let `μ` be a locally finite measure. If `f` has a finite limit `c` at `l' ⊓ μ.ae`, then `∫ x in u..v, f x ∂μ = μ (Ioc u v) • c + o(μ(Ioc u v))` as both `u` and `v` tend to `l`. See also `measure_integral_sub_linear_is_o_of_tendsto_ae_of_le'` for a version that also works, e.g., for `l = l' = at_top`. -/ lemma measure_integral_sub_linear_is_o_of_tendsto_ae_of_le (hfm : ae_measurable f μ) (hf : tendsto f (l' ⊓ μ.ae) (𝓝 c)) (hu : tendsto u lt l) (hv : tendsto v lt l) (huv : u ≤ᶠ[lt] v) : is_o (λ t, ∫ x in u t..v t, f x ∂μ - (μ (Ioc (u t) (v t))).to_real • c) (λ t, (μ $ Ioc (u t) (v t)).to_real) lt := measure_integral_sub_linear_is_o_of_tendsto_ae_of_le' hfm hf (FTC_filter.finite_at_inner l) hu hv huv /-- Fundamental theorem of calculus-1, local version for any measure. Let filters `l` and `l'` be related by `[FTC_filter a l l']`; let `μ` be a locally finite measure. If `f` has a finite limit `c` at `l' ⊓ μ.ae`, then `∫ x in u..v, f x ∂μ = -μ (Ioc v u) • c + o(μ(Ioc v u))` as both `u` and `v` tend to `l`. See also `measure_integral_sub_linear_is_o_of_tendsto_ae_of_ge'` for a version that also works, e.g., for `l = l' = at_top`. -/ lemma measure_integral_sub_linear_is_o_of_tendsto_ae_of_ge (hfm : ae_measurable f μ) (hf : tendsto f (l' ⊓ μ.ae) (𝓝 c)) (hu : tendsto u lt l) (hv : tendsto v lt l) (huv : v ≤ᶠ[lt] u) : is_o (λ t, ∫ x in u t..v t, f x ∂μ + (μ (Ioc (v t) (u t))).to_real • c) (λ t, (μ $ Ioc (v t) (u t)).to_real) lt := measure_integral_sub_linear_is_o_of_tendsto_ae_of_ge' hfm hf (FTC_filter.finite_at_inner l) hu hv huv end variables [order_topology α] [borel_space α] local attribute [instance] FTC_filter.meas_gen variables [FTC_filter a la la'] [FTC_filter b lb lb'] [locally_finite_measure μ] /-- Fundamental theorem of calculus-1, strict derivative in both limits for a locally finite measure. Let `f` be a measurable function integrable on `a..b`. Let `(la, la')` be a pair of `FTC_filter`s around `a`; let `(lb, lb')` be a pair of `FTC_filter`s around `b`. Suppose that `f` has finite limits `ca` and `cb` at `la' ⊓ μ.ae` and `lb' ⊓ μ.ae`, respectively. Then `∫ x in va..vb, f x ∂μ - ∫ x in ua..ub, f x ∂μ = ∫ x in ub..vb, cb ∂μ - ∫ x in ua..va, ca ∂μ + o(∥∫ x in ua..va, (1:ℝ) ∂μ∥ + ∥∫ x in ub..vb, (1:ℝ) ∂μ∥)` as `ua` and `va` tend to `la` while `ub` and `vb` tend to `lb`. -/ lemma measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae (hab : interval_integrable f μ a b) (hmeas : ae_measurable f μ) (ha_lim : tendsto f (la' ⊓ μ.ae) (𝓝 ca)) (hb_lim : tendsto f (lb' ⊓ μ.ae) (𝓝 cb)) (hua : tendsto ua lt la) (hva : tendsto va lt la) (hub : tendsto ub lt lb) (hvb : tendsto vb lt lb) : is_o (λ t, (∫ x in va t..vb t, f x ∂μ) - (∫ x in ua t..ub t, f x ∂μ) - (∫ x in ub t..vb t, cb ∂μ - ∫ x in ua t..va t, ca ∂μ)) (λ t, ∥∫ x in ua t..va t, (1:ℝ) ∂μ∥ + ∥∫ x in ub t..vb t, (1:ℝ) ∂μ∥) lt := begin refine ((measure_integral_sub_linear_is_o_of_tendsto_ae hmeas ha_lim hua hva).neg_left.add_add (measure_integral_sub_linear_is_o_of_tendsto_ae hmeas hb_lim hub hvb)).congr' _ (eventually_eq.refl _ _), have A : ∀ᶠ t in lt, interval_integrable f μ (ua t) (va t) := ha_lim.eventually_interval_integrable_ae hmeas (FTC_filter.finite_at_inner la) hua hva, have A' : ∀ᶠ t in lt, interval_integrable f μ a (ua t) := ha_lim.eventually_interval_integrable_ae hmeas (FTC_filter.finite_at_inner la) (tendsto_const_pure.mono_right FTC_filter.pure_le) hua, have B : ∀ᶠ t in lt, interval_integrable f μ (ub t) (vb t) := hb_lim.eventually_interval_integrable_ae hmeas (FTC_filter.finite_at_inner lb) hub hvb, have B' : ∀ᶠ t in lt, interval_integrable f μ b (ub t) := hb_lim.eventually_interval_integrable_ae hmeas (FTC_filter.finite_at_inner lb) (tendsto_const_pure.mono_right FTC_filter.pure_le) hub, filter_upwards [A, A', B, B'], intros t ua_va a_ua ub_vb b_ub, rw [← integral_interval_sub_interval_comm'], { dsimp only [], abel }, exacts [ub_vb, ua_va, b_ub.symm.trans $ hab.symm.trans a_ua] end /-- Fundamental theorem of calculus-1, strict derivative in right endpoint for a locally finite measure. Let `f` be a measurable function integrable on `a..b`. Let `(lb, lb')` be a pair of `FTC_filter`s around `b`. Suppose that `f` has a finite limit `c` at `lb' ⊓ μ.ae`. Then `∫ x in a..v, f x ∂μ - ∫ x in a..u, f x ∂μ = ∫ x in u..v, c ∂μ + o(∫ x in u..v, (1:ℝ) ∂μ)` as `u` and `v` tend to `lb`. -/ lemma measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae_right (hab : interval_integrable f μ a b) (hmeas : ae_measurable f μ) (hf : tendsto f (lb' ⊓ μ.ae) (𝓝 c)) (hu : tendsto u lt lb) (hv : tendsto v lt lb) : is_o (λ t, ∫ x in a..v t, f x ∂μ - ∫ x in a..u t, f x ∂μ - ∫ x in u t..v t, c ∂μ) (λ t, ∫ x in u t..v t, (1:ℝ) ∂μ) lt := by simpa using measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae hab hmeas ((tendsto_bot : tendsto _ ⊥ (𝓝 0)).mono_left inf_le_left) hf (tendsto_const_pure : tendsto _ _ (pure a)) tendsto_const_pure hu hv /-- Fundamental theorem of calculus-1, strict derivative in left endpoint for a locally finite measure. Let `f` be a measurable function integrable on `a..b`. Let `(la, la')` be a pair of `FTC_filter`s around `a`. Suppose that `f` has a finite limit `c` at `la' ⊓ μ.ae`. Then `∫ x in v..b, f x ∂μ - ∫ x in u..b, f x ∂μ = -∫ x in u..v, c ∂μ + o(∫ x in u..v, (1:ℝ) ∂μ)` as `u` and `v` tend to `la`. -/ lemma measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae_left (hab : interval_integrable f μ a b) (hmeas : ae_measurable f μ) (hf : tendsto f (la' ⊓ μ.ae) (𝓝 c)) (hu : tendsto u lt la) (hv : tendsto v lt la) : is_o (λ t, ∫ x in v t..b, f x ∂μ - ∫ x in u t..b, f x ∂μ + ∫ x in u t..v t, c ∂μ) (λ t, ∫ x in u t..v t, (1:ℝ) ∂μ) lt := by simpa using measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae hab hmeas hf ((tendsto_bot : tendsto _ ⊥ (𝓝 0)).mono_left inf_le_left) hu hv (tendsto_const_pure : tendsto _ _ (pure b)) tendsto_const_pure end /-! ### Fundamental theorem of calculus-1 for Lebesgue measure In this section we restate theorems from the previous section for Lebesgue measure. In particular, we prove that `∫ x in u..v, f x` is strictly differentiable in `(u, v)` at `(a, b)` provided that `f` is integrable on `a..b` and is continuous at `a` and `b`. -/ variables {f : ℝ → E} {c ca cb : E} {l l' la la' lb lb' : filter ℝ} {lt : filter β} {a b z : ℝ} {u v ua ub va vb : β → ℝ} [FTC_filter a la la'] [FTC_filter b lb lb'] /-! #### Auxiliary `is_o` statements In this section we prove several lemmas that can be interpreted as strict differentiability of `(u, v) ↦ ∫ x in u..v, f x ∂μ` in `u` and/or `v` at a filter. The statements use `is_o` because we have no definition of `has_strict_(f)deriv_at_filter` in the library. -/ /-- Fundamental theorem of calculus-1, local version. If `f` has a finite limit `c` almost surely at `l'`, where `(l, l')` is an `FTC_filter` pair around `a`, then `∫ x in u..v, f x ∂μ = (v - u) • c + o (v - u)` as both `u` and `v` tend to `l`. -/ lemma integral_sub_linear_is_o_of_tendsto_ae [FTC_filter a l l'] (hfm : ae_measurable f) (hf : tendsto f (l' ⊓ volume.ae) (𝓝 c)) {u v : β → ℝ} (hu : tendsto u lt l) (hv : tendsto v lt l) : is_o (λ t, (∫ x in u t..v t, f x) - (v t - u t) • c) (v - u) lt := by simpa [integral_const] using measure_integral_sub_linear_is_o_of_tendsto_ae hfm hf hu hv /-- Fundamental theorem of calculus-1, strict differentiability at filter in both endpoints. If `f` is a measurable function integrable on `a..b`, `(la, la')` is an `FTC_filter` pair around `a`, and `(lb, lb')` is an `FTC_filter` pair around `b`, and `f` has finite limits `ca` and `cb` almost surely at `la'` and `lb'`, respectively, then `(∫ x in va..vb, f x) - ∫ x in ua..ub, f x = (vb - ub) • cb - (va - ua) • ca + o(∥va - ua∥ + ∥vb - ub∥)` as `ua` and `va` tend to `la` while `ub` and `vb` tend to `lb`. This lemma could've been formulated using `has_strict_fderiv_at_filter` if we had this definition. -/ lemma integral_sub_integral_sub_linear_is_o_of_tendsto_ae (hab : interval_integrable f volume a b) (hmeas : ae_measurable f) (ha_lim : tendsto f (la' ⊓ volume.ae) (𝓝 ca)) (hb_lim : tendsto f (lb' ⊓ volume.ae) (𝓝 cb)) (hua : tendsto ua lt la) (hva : tendsto va lt la) (hub : tendsto ub lt lb) (hvb : tendsto vb lt lb) : is_o (λ t, (∫ x in va t..vb t, f x) - (∫ x in ua t..ub t, f x) - ((vb t - ub t) • cb - (va t - ua t) • ca)) (λ t, ∥va t - ua t∥ + ∥vb t - ub t∥) lt := by simpa [integral_const] using measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae hab hmeas ha_lim hb_lim hua hva hub hvb /-- Fundamental theorem of calculus-1, strict differentiability at filter in both endpoints. If `f` is a measurable function integrable on `a..b`, `(lb, lb')` is an `FTC_filter` pair around `b`, and `f` has a finite limit `c` almost surely at `lb'`, then `(∫ x in a..v, f x) - ∫ x in a..u, f x = (v - u) • c + o(∥v - u∥)` as `u` and `v` tend to `lb`. This lemma could've been formulated using `has_strict_deriv_at_filter` if we had this definition. -/ lemma integral_sub_integral_sub_linear_is_o_of_tendsto_ae_right (hab : interval_integrable f volume a b) (hmeas : ae_measurable f) (hf : tendsto f (lb' ⊓ volume.ae) (𝓝 c)) (hu : tendsto u lt lb) (hv : tendsto v lt lb) : is_o (λ t, (∫ x in a..v t, f x) - (∫ x in a..u t, f x) - (v t - u t) • c) (v - u) lt := by simpa only [integral_const, smul_eq_mul, mul_one] using measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae_right hab hmeas hf hu hv /-- Fundamental theorem of calculus-1, strict differentiability at filter in both endpoints. If `f` is a measurable function integrable on `a..b`, `(la, la')` is an `FTC_filter` pair around `a`, and `f` has a finite limit `c` almost surely at `la'`, then `(∫ x in v..b, f x) - ∫ x in u..b, f x = -(v - u) • c + o(∥v - u∥)` as `u` and `v` tend to `la`. This lemma could've been formulated using `has_strict_deriv_at_filter` if we had this definition. -/ lemma integral_sub_integral_sub_linear_is_o_of_tendsto_ae_left (hab : interval_integrable f volume a b) (hmeas : ae_measurable f) (hf : tendsto f (la' ⊓ volume.ae) (𝓝 c)) (hu : tendsto u lt la) (hv : tendsto v lt la) : is_o (λ t, (∫ x in v t..b, f x) - (∫ x in u t..b, f x) + (v t - u t) • c) (v - u) lt := by simpa only [integral_const, smul_eq_mul, mul_one] using measure_integral_sub_integral_sub_linear_is_o_of_tendsto_ae_left hab hmeas hf hu hv open continuous_linear_map (fst snd smul_right sub_apply smul_right_apply coe_fst' coe_snd' map_sub) /-! #### Strict differentiability In this section we prove that for a measurable function `f` integrable on `a..b`, * `integral_has_strict_fderiv_at_of_tendsto_ae`: the function `(u, v) ↦ ∫ x in u..v, f x` has derivative `(u, v) ↦ v • cb - u • ca` at `(a, b)` in the sense of strict differentiability provided that `f` tends to `ca` and `cb` almost surely as `x` tendsto to `a` and `b`, respectively; * `integral_has_strict_fderiv_at`: the function `(u, v) ↦ ∫ x in u..v, f x` has derivative `(u, v) ↦ v • f b - u • f a` at `(a, b)` in the sense of strict differentiability provided that `f` is continuous at `a` and `b`; * `integral_has_strict_deriv_at_of_tendsto_ae_right`: the function `u ↦ ∫ x in a..u, f x` has derivative `c` at `b` in the sense of strict differentiability provided that `f` tends to `c` almost surely as `x` tends to `b`; * `integral_has_strict_deriv_at_right`: the function `u ↦ ∫ x in a..u, f x` has derivative `f b` at `b` in the sense of strict differentiability provided that `f` is continuous at `b`; * `integral_has_strict_deriv_at_of_tendsto_ae_left`: the function `u ↦ ∫ x in u..b, f x` has derivative `-c` at `a` in the sense of strict differentiability provided that `f` tends to `c` almost surely as `x` tends to `a`; * `integral_has_strict_deriv_at_left`: the function `u ↦ ∫ x in u..b, f x` has derivative `-f a` at `a` in the sense of strict differentiability provided that `f` is continuous at `a`. -/ /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f x` has finite limits `ca` and `cb` almost surely as `x` tends to `a` and `b`, respectively, then `(u, v) ↦ ∫ x in u..v, f x` has derivative `(u, v) ↦ v • cb - u • ca` at `(a, b)` in the sense of strict differentiability. -/ lemma integral_has_strict_fderiv_at_of_tendsto_ae (hf : interval_integrable f volume a b) (hmeas : ae_measurable f) (ha : tendsto f (𝓝 a ⊓ volume.ae) (𝓝 ca)) (hb : tendsto f (𝓝 b ⊓ volume.ae) (𝓝 cb)) : has_strict_fderiv_at (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x) ((snd ℝ ℝ ℝ).smul_right cb - (fst ℝ ℝ ℝ).smul_right ca) (a, b) := begin have := integral_sub_integral_sub_linear_is_o_of_tendsto_ae hf hmeas ha hb ((continuous_fst.comp continuous_snd).tendsto ((a, b), (a, b))) ((continuous_fst.comp continuous_fst).tendsto ((a, b), (a, b))) ((continuous_snd.comp continuous_snd).tendsto ((a, b), (a, b))) ((continuous_snd.comp continuous_fst).tendsto ((a, b), (a, b))), refine (this.congr_left _).trans_is_O _, { intro x, simp [sub_smul] }, { exact is_O_fst_prod.norm_left.add is_O_snd_prod.norm_left } end /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous at `a` and `b`, then `(u, v) ↦ ∫ x in u..v, f x` has derivative `(u, v) ↦ v • cb - u • ca` at `(a, b)` in the sense of strict differentiability. -/ lemma integral_has_strict_fderiv_at (hf : interval_integrable f volume a b) (hmeas : ae_measurable f) (ha : continuous_at f a) (hb : continuous_at f b) : has_strict_fderiv_at (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x) ((snd ℝ ℝ ℝ).smul_right (f b) - (fst ℝ ℝ ℝ).smul_right (f a)) (a, b) := integral_has_strict_fderiv_at_of_tendsto_ae hf hmeas (ha.mono_left inf_le_left) (hb.mono_left inf_le_left) /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f x` has a finite limit `c` almost surely at `b`, then `u ↦ ∫ x in a..u, f x` has derivative `c` at `b` in the sense of strict differentiability. -/ lemma integral_has_strict_deriv_at_of_tendsto_ae_right (hf : interval_integrable f volume a b) (hmeas : ae_measurable f) (hb : tendsto f (𝓝 b ⊓ volume.ae) (𝓝 c)) : has_strict_deriv_at (λ u, ∫ x in a..u, f x) c b := integral_sub_integral_sub_linear_is_o_of_tendsto_ae_right hf hmeas hb continuous_at_snd continuous_at_fst /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous at `b`, then `u ↦ ∫ x in a..u, f x` has derivative `f b` at `b` in the sense of strict differentiability. -/ lemma integral_has_strict_deriv_at_right (hf : interval_integrable f volume a b) (hmeas : ae_measurable f) (hb : continuous_at f b) : has_strict_deriv_at (λ u, ∫ x in a..u, f x) (f b) b := integral_has_strict_deriv_at_of_tendsto_ae_right hf hmeas (hb.mono_left inf_le_left) /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f x` has a finite limit `c` almost surely at `a`, then `u ↦ ∫ x in u..b, f x` has derivative `-c` at `a` in the sense of strict differentiability. -/ lemma integral_has_strict_deriv_at_of_tendsto_ae_left (hf : interval_integrable f volume a b) (hmeas : ae_measurable f) (ha : tendsto f (𝓝 a ⊓ volume.ae) (𝓝 c)) : has_strict_deriv_at (λ u, ∫ x in u..b, f x) (-c) a := by simpa only [← integral_symm] using (integral_has_strict_deriv_at_of_tendsto_ae_right hf.symm hmeas ha).neg /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous at `a`, then `u ↦ ∫ x in u..b, f x` has derivative `-f a` at `a` in the sense of strict differentiability. -/ lemma integral_has_strict_deriv_at_left (hf : interval_integrable f volume a b) (hmeas : ae_measurable f) (ha : continuous_at f a) : has_strict_deriv_at (λ u, ∫ x in u..b, f x) (-f a) a := by simpa only [← integral_symm] using (integral_has_strict_deriv_at_right hf.symm hmeas ha).neg /-! #### Fréchet differentiability In this subsection we restate results from the previous subsection in terms of `has_fderiv_at`, `has_deriv_at`, `fderiv`, and `deriv`. -/ /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f x` has finite limits `ca` and `cb` almost surely as `x` tends to `a` and `b`, respectively, then `(u, v) ↦ ∫ x in u..v, f x` has derivative `(u, v) ↦ v • cb - u • ca` at `(a, b)`. -/ lemma integral_has_fderiv_at_of_tendsto_ae (hf : interval_integrable f volume a b) (hmeas : ae_measurable f) (ha : tendsto f (𝓝 a ⊓ volume.ae) (𝓝 ca)) (hb : tendsto f (𝓝 b ⊓ volume.ae) (𝓝 cb)) : has_fderiv_at (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x) ((snd ℝ ℝ ℝ).smul_right cb - (fst ℝ ℝ ℝ).smul_right ca) (a, b) := (integral_has_strict_fderiv_at_of_tendsto_ae hf hmeas ha hb).has_fderiv_at /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous at `a` and `b`, then `(u, v) ↦ ∫ x in u..v, f x` has derivative `(u, v) ↦ v • cb - u • ca` at `(a, b)`. -/ lemma integral_has_fderiv_at (hf : interval_integrable f volume a b) (hmeas : ae_measurable f) (ha : continuous_at f a) (hb : continuous_at f b) : has_fderiv_at (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x) ((snd ℝ ℝ ℝ).smul_right (f b) - (fst ℝ ℝ ℝ).smul_right (f a)) (a, b) := (integral_has_strict_fderiv_at hf hmeas ha hb).has_fderiv_at /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f x` has finite limits `ca` and `cb` almost surely as `x` tends to `a` and `b`, respectively, then `fderiv` derivative of `(u, v) ↦ ∫ x in u..v, f x` at `(a, b)` equals `(u, v) ↦ v • cb - u • ca`. -/ lemma fderiv_integral_of_tendsto_ae (hf : interval_integrable f volume a b) (hmeas : ae_measurable f) (ha : tendsto f (𝓝 a ⊓ volume.ae) (𝓝 ca)) (hb : tendsto f (𝓝 b ⊓ volume.ae) (𝓝 cb)) : fderiv ℝ (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x) (a, b) = (snd ℝ ℝ ℝ).smul_right cb - (fst ℝ ℝ ℝ).smul_right ca := (integral_has_fderiv_at_of_tendsto_ae hf hmeas ha hb).fderiv /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous at `a` and `b`, then `fderiv` derivative of `(u, v) ↦ ∫ x in u..v, f x` at `(a, b)` equals `(u, v) ↦ v • cb - u • ca`. -/ lemma fderiv_integral (hf : interval_integrable f volume a b) (hmeas : ae_measurable f) (ha : continuous_at f a) (hb : continuous_at f b) : fderiv ℝ (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x) (a, b) = (snd ℝ ℝ ℝ).smul_right (f b) - (fst ℝ ℝ ℝ).smul_right (f a) := (integral_has_fderiv_at hf hmeas ha hb).fderiv /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f x` has a finite limit `c` almost surely at `b`, then `u ↦ ∫ x in a..u, f x` has derivative `c` at `b`. -/ lemma integral_has_deriv_at_of_tendsto_ae_right (hf : interval_integrable f volume a b) (hmeas : ae_measurable f) (hb : tendsto f (𝓝 b ⊓ volume.ae) (𝓝 c)) : has_deriv_at (λ u, ∫ x in a..u, f x) c b := (integral_has_strict_deriv_at_of_tendsto_ae_right hf hmeas hb).has_deriv_at /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous at `b`, then `u ↦ ∫ x in a..u, f x` has derivative `f b` at `b`. -/ lemma integral_has_deriv_at_right (hf : interval_integrable f volume a b) (hmeas : ae_measurable f) (hb : continuous_at f b) : has_deriv_at (λ u, ∫ x in a..u, f x) (f b) b := (integral_has_strict_deriv_at_right hf hmeas hb).has_deriv_at /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f` has a finite limit `c` almost surely at `b`, then the derivative of `u ↦ ∫ x in a..u, f x` at `b` equals `c`. -/ lemma deriv_integral_of_tendsto_ae_right (hf : interval_integrable f volume a b) (hmeas : ae_measurable f) (hb : tendsto f (𝓝 b ⊓ volume.ae) (𝓝 c)) : deriv (λ u, ∫ x in a..u, f x) b = c := (integral_has_deriv_at_of_tendsto_ae_right hf hmeas hb).deriv /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous at `b`, then the derivative of `u ↦ ∫ x in a..u, f x` at `b` equals `f b`. -/ lemma deriv_integral_right (hf : interval_integrable f volume a b) (hmeas : ae_measurable f ) (hb : continuous_at f b) : deriv (λ u, ∫ x in a..u, f x) b = f b := (integral_has_deriv_at_right hf hmeas hb).deriv /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f x` has a finite limit `c` almost surely at `a`, then `u ↦ ∫ x in u..b, f x` has derivative `-c` at `a`. -/ lemma integral_has_deriv_at_of_tendsto_ae_left (hf : interval_integrable f volume a b) (hmeas : ae_measurable f) (ha : tendsto f (𝓝 a ⊓ volume.ae) (𝓝 c)) : has_deriv_at (λ u, ∫ x in u..b, f x) (-c) a := (integral_has_strict_deriv_at_of_tendsto_ae_left hf hmeas ha).has_deriv_at /-- Fundamental theorem of calculus-1: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous at `a`, then `u ↦ ∫ x in u..b, f x` has derivative `-f a` at `a`. -/ lemma integral_has_deriv_at_left (hf : interval_integrable f volume a b) (hmeas : ae_measurable f) (ha : continuous_at f a) : has_deriv_at (λ u, ∫ x in u..b, f x) (-f a) a := (integral_has_strict_deriv_at_left hf hmeas ha).has_deriv_at /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f` has a finite limit `c` almost surely at `a`, then the derivative of `u ↦ ∫ x in u..b, f x` at `a` equals `-c`. -/ lemma deriv_integral_of_tendsto_ae_left (hf : interval_integrable f volume a b) (hmeas : ae_measurable f) (hb : tendsto f (𝓝 a ⊓ volume.ae) (𝓝 c)) : deriv (λ u, ∫ x in u..b, f x) a = -c := (integral_has_deriv_at_of_tendsto_ae_left hf hmeas hb).deriv /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f` is continuous at `a`, then the derivative of `u ↦ ∫ x in u..b, f x` at `a` equals `-f a`. -/ lemma deriv_integral_left (hf : interval_integrable f volume a b) (hmeas : ae_measurable f ) (hb : continuous_at f a) : deriv (λ u, ∫ x in u..b, f x) a = -f a := (integral_has_deriv_at_left hf hmeas hb).deriv /-! #### One-sided derivatives -/ /-- Let `f` be a measurable function integrable on `a..b`. The function `(u, v) ↦ ∫ x in u..v, f x` has derivative `(u, v) ↦ v • cb - u • ca` within `s × t` at `(a, b)`, where `s ∈ {Iic a, {a}, Ici a, univ}` and `t ∈ {Iic b, {b}, Ici b, univ}` provided that `f` tends to `ca` and `cb` almost surely at the filters `la` and `lb` from the following table. | `s` | `la` | `t` | `lb` | | ------- | ---- | --- | ---- | | `Iic a` | `𝓝[Iic a] a` | `Iic b` | `𝓝[Iic b] b` | | `Ici a` | `𝓝[Ioi a] a` | `Ici b` | `𝓝[Ioi b] b` | | `{a}` | `⊥` | `{b}` | `⊥` | | `univ` | `𝓝 a` | `univ` | `𝓝 b` | -/ lemma integral_has_fderiv_within_at_of_tendsto_ae (hf : interval_integrable f volume a b) (hmeas : ae_measurable f) {s t : set ℝ} [FTC_filter a (𝓝[s] a) la] [FTC_filter b (𝓝[t] b) lb] (ha : tendsto f (la ⊓ volume.ae) (𝓝 ca)) (hb : tendsto f (lb ⊓ volume.ae) (𝓝 cb)) : has_fderiv_within_at (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x) ((snd ℝ ℝ ℝ).smul_right cb - (fst ℝ ℝ ℝ).smul_right ca) (s.prod t) (a, b) := begin rw [has_fderiv_within_at, nhds_within_prod_eq], have := integral_sub_integral_sub_linear_is_o_of_tendsto_ae hf hmeas ha hb (tendsto_const_pure.mono_right FTC_filter.pure_le : tendsto _ _ (𝓝[s] a)) tendsto_fst (tendsto_const_pure.mono_right FTC_filter.pure_le : tendsto _ _ (𝓝[t] b)) tendsto_snd, refine (this.congr_left _).trans_is_O _, { intro x, simp [sub_smul] }, { exact is_O_fst_prod.norm_left.add is_O_snd_prod.norm_left } end /-- Let `f` be a measurable function integrable on `a..b`. The function `(u, v) ↦ ∫ x in u..v, f x` has derivative `(u, v) ↦ v • f b - u • f a` within `s × t` at `(a, b)`, where `s ∈ {Iic a, {a}, Ici a, univ}` and `t ∈ {Iic b, {b}, Ici b, univ}` provided that `f` tends to `f a` and `f b` at the filters `la` and `lb` from the following table. In most cases this assumption is definitionally equal `continuous_at f _` or `continuous_within_at f _ _`. | `s` | `la` | `t` | `lb` | | ------- | ---- | --- | ---- | | `Iic a` | `𝓝[Iic a] a` | `Iic b` | `𝓝[Iic b] b` | | `Ici a` | `𝓝[Ioi a] a` | `Ici b` | `𝓝[Ioi b] b` | | `{a}` | `⊥` | `{b}` | `⊥` | | `univ` | `𝓝 a` | `univ` | `𝓝 b` | -/ lemma integral_has_fderiv_within_at (hf : interval_integrable f volume a b) (hmeas : ae_measurable f) {s t : set ℝ} [FTC_filter a (𝓝[s] a) la] [FTC_filter b (𝓝[t] b) lb] (ha : tendsto f la (𝓝 $ f a)) (hb : tendsto f lb (𝓝 $ f b)) : has_fderiv_within_at (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x) ((snd ℝ ℝ ℝ).smul_right (f b) - (fst ℝ ℝ ℝ).smul_right (f a)) (s.prod t) (a, b) := integral_has_fderiv_within_at_of_tendsto_ae hf hmeas (ha.mono_left inf_le_left) (hb.mono_left inf_le_left) /-- An auxiliary tactic closing goals `unique_diff_within_at ℝ s a` where `s ∈ {Iic a, Ici a, univ}`. -/ meta def unique_diff_within_at_Ici_Iic_univ : tactic unit := `[apply_rules [unique_diff_on.unique_diff_within_at, unique_diff_on_Ici, unique_diff_on_Iic, left_mem_Ici, right_mem_Iic, unique_diff_within_at_univ]] /-- Let `f` be a measurable function integrable on `a..b`. Choose `s ∈ {Iic a, Ici a, univ}` and `t ∈ {Iic b, Ici b, univ}`. Suppose that `f` tends to `ca` and `cb` almost surely at the filters `la` and `lb` from the table below. Then `fderiv_within ℝ (λ p, ∫ x in p.1..p.2, f x) (s.prod t)` is equal to `(u, v) ↦ u • cb - v • ca`. | `s` | `la` | `t` | `lb` | | ------- | ---- | --- | ---- | | `Iic a` | `𝓝[Iic a] a` | `Iic b` | `𝓝[Iic b] b` | | `Ici a` | `𝓝[Ioi a] a` | `Ici b` | `𝓝[Ioi b] b` | | `univ` | `𝓝 a` | `univ` | `𝓝 b` | -/ lemma fderiv_within_integral_of_tendsto_ae (hf : interval_integrable f volume a b) (hmeas : ae_measurable f) {s t : set ℝ} [FTC_filter a (𝓝[s] a) la] [FTC_filter b (𝓝[t] b) lb] (ha : tendsto f (la ⊓ volume.ae) (𝓝 ca)) (hb : tendsto f (lb ⊓ volume.ae) (𝓝 cb)) (hs : unique_diff_within_at ℝ s a . unique_diff_within_at_Ici_Iic_univ) (ht : unique_diff_within_at ℝ t b . unique_diff_within_at_Ici_Iic_univ) : fderiv_within ℝ (λ p : ℝ × ℝ, ∫ x in p.1..p.2, f x) (s.prod t) (a, b) = ((snd ℝ ℝ ℝ).smul_right cb - (fst ℝ ℝ ℝ).smul_right ca) := (integral_has_fderiv_within_at_of_tendsto_ae hf hmeas ha hb).fderiv_within $ hs.prod ht /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f x` has a finite limit `c` almost surely as `x` tends to `b` from the right or from the left, then `u ↦ ∫ x in a..u, f x` has right (resp., left) derivative `c` at `b`. -/ lemma integral_has_deriv_within_at_of_tendsto_ae_right (hf : interval_integrable f volume a b) (hmeas : ae_measurable f) {s t : set ℝ} [FTC_filter b (𝓝[s] b) (𝓝[t] b)] (hb : tendsto f (𝓝[t] b ⊓ volume.ae) (𝓝 c)) : has_deriv_within_at (λ u, ∫ x in a..u, f x) c s b := integral_sub_integral_sub_linear_is_o_of_tendsto_ae_right hf hmeas hb (tendsto_const_pure.mono_right FTC_filter.pure_le) tendsto_id /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f x` is continuous from the left or from the right at `b`, then `u ↦ ∫ x in a..u, f x` has left (resp., right) derivative `f b` at `b`. -/ lemma integral_has_deriv_within_at_right (hf : interval_integrable f volume a b) (hmeas : ae_measurable f) {s t : set ℝ} [FTC_filter b (𝓝[s] b) (𝓝[t] b)] (hb : continuous_within_at f t b) : has_deriv_within_at (λ u, ∫ x in a..u, f x) (f b) s b := integral_has_deriv_within_at_of_tendsto_ae_right hf hmeas (hb.mono_left inf_le_left) /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f x` has a finite limit `c` almost surely as `x` tends to `b` from the right or from the left, then the right (resp., left) derivative of `u ↦ ∫ x in a..u, f x` at `b` equals `c`. -/ lemma deriv_within_integral_of_tendsto_ae_right (hf : interval_integrable f volume a b) (hmeas : ae_measurable f) {s t : set ℝ} [FTC_filter b (𝓝[s] b) (𝓝[t] b)] (hb : tendsto f (𝓝[t] b ⊓ volume.ae) (𝓝 c)) (hs : unique_diff_within_at ℝ s b . unique_diff_within_at_Ici_Iic_univ) : deriv_within (λ u, ∫ x in a..u, f x) s b = c := (integral_has_deriv_within_at_of_tendsto_ae_right hf hmeas hb).deriv_within hs /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f x` is continuous on the right or on the left at `b`, then the right (resp., left) derivative of `u ↦ ∫ x in a..u, f x` at `b` equals `f b`. -/ lemma deriv_within_integral_right (hf : interval_integrable f volume a b) (hmeas : ae_measurable f) {s t : set ℝ} [FTC_filter b (𝓝[s] b) (𝓝[t] b)] (hb : continuous_within_at f t b) (hs : unique_diff_within_at ℝ s b . unique_diff_within_at_Ici_Iic_univ) : deriv_within (λ u, ∫ x in a..u, f x) s b = f b := (integral_has_deriv_within_at_right hf hmeas hb).deriv_within hs /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f x` has a finite limit `c` almost surely as `x` tends to `a` from the right or from the left, then `u ↦ ∫ x in u..b, f x` has right (resp., left) derivative `-c` at `a`. -/ lemma integral_has_deriv_within_at_of_tendsto_ae_left (hf : interval_integrable f volume a b) (hmeas : ae_measurable f) {s t : set ℝ} [FTC_filter a (𝓝[s] a) (𝓝[t] a)] (ha : tendsto f (𝓝[t] a ⊓ volume.ae) (𝓝 c)) : has_deriv_within_at (λ u, ∫ x in u..b, f x) (-c) s a := by { simp only [integral_symm b], exact (integral_has_deriv_within_at_of_tendsto_ae_right hf.symm hmeas ha).neg } /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f x` is continuous from the left or from the right at `a`, then `u ↦ ∫ x in u..b, f x` has left (resp., right) derivative `-f a` at `a`. -/ lemma integral_has_deriv_within_at_left (hf : interval_integrable f volume a b) (hmeas : ae_measurable f) {s t : set ℝ} [FTC_filter a (𝓝[s] a) (𝓝[t] a)] (ha : continuous_within_at f t a) : has_deriv_within_at (λ u, ∫ x in u..b, f x) (-f a) s a := integral_has_deriv_within_at_of_tendsto_ae_left hf hmeas (ha.mono_left inf_le_left) /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f x` has a finite limit `c` almost surely as `x` tends to `a` from the right or from the left, then the right (resp., left) derivative of `u ↦ ∫ x in u..b, f x` at `a` equals `-c`. -/ lemma deriv_within_integral_of_tendsto_ae_left (hf : interval_integrable f volume a b) (hmeas : ae_measurable f) {s t : set ℝ} [FTC_filter a (𝓝[s] a) (𝓝[t] a)] (ha : tendsto f (𝓝[t] a ⊓ volume.ae) (𝓝 c)) (hs : unique_diff_within_at ℝ s a . unique_diff_within_at_Ici_Iic_univ) : deriv_within (λ u, ∫ x in u..b, f x) s a = -c := (integral_has_deriv_within_at_of_tendsto_ae_left hf hmeas ha).deriv_within hs /-- Fundamental theorem of calculus: if `f : ℝ → E` is integrable on `a..b` and `f x` is continuous on the right or on the left at `a`, then the right (resp., left) derivative of `u ↦ ∫ x in u..b, f x` at `a` equals `-f a`. -/ lemma deriv_within_integral_left (hf : interval_integrable f volume a b) (hmeas : ae_measurable f) {s t : set ℝ} [FTC_filter a (𝓝[s] a) (𝓝[t] a)] (ha : continuous_within_at f t a) (hs : unique_diff_within_at ℝ s a . unique_diff_within_at_Ici_Iic_univ) : deriv_within (λ u, ∫ x in u..b, f x) s a = -f a := (integral_has_deriv_within_at_left hf hmeas ha).deriv_within hs /-! ### Fundamental theorem of calculus, part 2 This section contains theorems pertaining to FTC-2 for interval integrals. -/ variables {f' : ℝ → E} /-- The integral of a continuous function is differentiable on a real set `s`. -/ theorem differentiable_on_integral_of_continuous {s : set ℝ} (hintg : ∀ x ∈ s, interval_integrable f volume a x) (hcont : continuous f) : differentiable_on ℝ (λ u, ∫ x in a..u, f x) s := λ y hy, (integral_has_deriv_at_right (hintg y hy) hcont.measurable.ae_measurable hcont.continuous_at).differentiable_at.differentiable_within_at /-- The integral of a continuous function is continuous on a real set `s`. This is true even without the assumption of continuity, but a proof of that fact does not yet exist in mathlib. -/ theorem continuous_on_integral_of_continuous {s : set ℝ} (hintg : ∀ x ∈ s, interval_integrable f volume a x) (hcont : continuous f) : continuous_on (λ u, ∫ x in a..u, f x) s := (differentiable_on_integral_of_continuous hintg hcont).continuous_on /-- Fundamental theorem of calculus-2: If `f : ℝ → E` is continuous on `[a, b]` and has a right derivative at `f' x` for all `x` in `[a, b)`, and `f'` is continuous on `[a, b]` and measurable, then `∫ y in a..b, f' y` equals `f b - f a`. -/ theorem integral_eq_sub_of_has_deriv_right_of_le (hab : a ≤ b) (hcont : continuous_on f (Icc a b)) (hderiv : ∀ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ici x) x) (hcont' : continuous_on f' (Icc a b)) (hmeas' : ae_measurable f') : ∫ y in a..b, f' y = f b - f a := begin refine eq_sub_of_add_eq (eq_of_has_deriv_right_eq (λ y hy, _) hderiv (λ y hy, _) hcont (by simp) _ (right_mem_Icc.2 hab)), { refine (integral_has_deriv_within_at_right _ hmeas' _).add_const _, { refine (hcont'.mono _).interval_integrable, simp only [hy.left, Icc_subset_Icc_right hy.right.le, interval_of_le] }, { exact (hcont' _ (mem_Icc_of_Ico hy)).mono_of_mem (Icc_mem_nhds_within_Ioi hy) } }, { -- TODO: prove that integral of any integrable function is continuous, and use here letI : tendsto_Ixx_class Ioc (𝓟 (Icc a b)) (𝓟 (Ioc a b)) := tendsto_Ixx_class_principal.2 (λ x hx y hy, Ioc_subset_Ioc hx.1 hy.2), haveI : is_measurably_generated (𝓝[Ioc a b] y) := is_measurable_Ioc.nhds_within_is_measurably_generated y, letI : FTC_filter y (𝓝[Icc a b] y) (𝓝[Ioc a b] y) := ⟨pure_le_nhds_within hy, inf_le_left⟩, refine (integral_has_deriv_within_at_right _ hmeas' _).continuous_within_at.add continuous_within_at_const, { exact (hcont'.mono $ Icc_subset_Icc_right hy.2).interval_integrable_of_Icc hy.1 }, { exact (hcont' y hy).mono Ioc_subset_Icc_self } } end /-- Fundamental theorem of calculus-2: If `f : ℝ → E` is continuous on `[a, b]` (where `a ≤ b`) and has a right derivative at `f' x` for all `x` in `[a, b)`, and `f'` is continuous on `[a, b]` and measurable, then `∫ y in a..b, f' y` equals `f b - f a`. -/ theorem integral_eq_sub_of_has_deriv_right (hcont : continuous_on f (interval a b)) (hderiv : ∀ x ∈ Ico (min a b) (max a b), has_deriv_within_at f (f' x) (Ici x) x) (hcont' : continuous_on f' (interval a b)) (hmeas' : ae_measurable f') : ∫ y in a..b, f' y = f b - f a := begin cases le_total a b with hab hab, { simp only [interval_of_le, min_eq_left, max_eq_right, hab] at hcont hcont' hderiv, exact integral_eq_sub_of_has_deriv_right_of_le hab hcont hderiv hcont' hmeas' }, { simp only [interval_of_ge, min_eq_right, max_eq_left, hab] at hcont hcont' hderiv, rw [integral_symm, integral_eq_sub_of_has_deriv_right_of_le hab hcont hderiv hcont' hmeas', neg_sub] } end /-- Fundamental theorem of calculus-2: If `f : ℝ → E` is continuous on `[a, b]` and has a derivative at `f' x` for all `x` in `[a, b)`, and `f'` is continuous on `[a, b]` and measurable, then `∫ y in a..b, f' y` equals `f b - f a`. -/ theorem integral_eq_sub_of_has_deriv_at' (hcont : continuous_on f (interval a b)) (hderiv : ∀ x ∈ Ico (min a b) (max a b), has_deriv_at f (f' x) x) (hcont' : continuous_on f' (interval a b)) (hmeas' : ae_measurable f') : ∫ y in a..b, f' y = f b - f a := integral_eq_sub_of_has_deriv_right hcont (λ x hx, (hderiv x hx).has_deriv_within_at) hcont' hmeas' /-- Fundamental theorem of calculus-2: If `f : ℝ → E` has a derivative at `f' x` for all `x` in `[a, b)` and `f'` is continuous on `[a, b]` and measurable, then `∫ y in a..b, f' y` equals `f b - f a`. -/ theorem integral_eq_sub_of_has_deriv_at (hderiv : ∀ x ∈ interval a b, has_deriv_at f (f' x) x) (hcont' : continuous_on f' (interval a b)) (hmeas' : ae_measurable f') : ∫ y in a..b, f' y = f b - f a := integral_eq_sub_of_has_deriv_at' (λ x hx, (hderiv x hx).continuous_at.continuous_within_at) (λ x hx, hderiv _ (mem_Icc_of_Ico hx)) hcont' hmeas' /-- Fundamental theorem of calculus-2: If `f : ℝ → E` is differentiable at every `x` in `[a, b]` and its derivative is continuous on `[a, b]`, then `∫ y in a..b, deriv f y` equals `f b - f a`. -/ theorem integral_deriv_eq_sub (hderiv : ∀ x ∈ interval a b, differentiable_at ℝ f x) (hcont' : continuous_on (deriv f) (interval a b)) : ∫ y in a..b, deriv f y = f b - f a := integral_eq_sub_of_has_deriv_at (λ x hx, (hderiv x hx).has_deriv_at) hcont' (measurable_deriv f).ae_measurable end interval_integral
49a88e150b14a56ff311450906ddee667c4c0488
8eeb99d0fdf8125f5d39a0ce8631653f588ee817
/src/order/pilex.lean
57310b701b9413a019ff9a953b29dec3e708ec27
[ "Apache-2.0" ]
permissive
jesse-michael-han/mathlib
a15c58378846011b003669354cbab7062b893cfe
fa6312e4dc971985e6b7708d99a5bc3062485c89
refs/heads/master
1,625,200,760,912
1,602,081,753,000
1,602,081,753,000
181,787,230
0
0
null
1,555,460,682,000
1,555,460,682,000
null
UTF-8
Lean
false
false
4,317
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 algebra.group.pi import order.rel_classes import algebra.order_functions variables {ι : Type*} {β : ι → Type*} (r : ι → ι → Prop) (s : Π {i}, β i → β i → Prop) /-- The lexicographic relation on `Π i : ι, β i`, where `ι` is ordered by `r`, and each `β i` is ordered by `s`. -/ def pi.lex (x y : Π i, β i) : Prop := ∃ i, (∀ j, r j i → x j = y j) ∧ s (x i) (y i) /-- The cartesian product of an indexed family, equipped with the lexicographic order. -/ def pilex (α : Type*) (β : α → Type*) : Type* := Π a, β a instance [has_lt ι] [∀ a, has_lt (β a)] : has_lt (pilex ι β) := { lt := pi.lex (<) (λ _, (<)) } instance [∀ a, inhabited (β a)] : inhabited (pilex ι β) := by unfold pilex; apply_instance set_option eqn_compiler.zeta true instance [linear_order ι] [∀ a, partial_order (β a)] : partial_order (pilex ι β) := let I := classical.DLO ι in have lt_not_symm : ∀ {x y : pilex ι β}, ¬ (x < y ∧ y < x), from λ x y ⟨⟨i, hi⟩, ⟨j, hj⟩⟩, begin rcases lt_trichotomy i j with hij | hij | hji, { exact lt_irrefl (x i) (by simpa [hj.1 _ hij] using hi.2) }, { exact not_le_of_gt hj.2 (hij ▸ le_of_lt hi.2) }, { exact lt_irrefl (x j) (by simpa [hi.1 _ hji] using hj.2) }, end, { le := λ x y, x < y ∨ x = y, le_refl := λ _, or.inr rfl, le_antisymm := λ x y hxy hyx, hxy.elim (λ hxy, hyx.elim (λ hyx, false.elim (lt_not_symm ⟨hxy, hyx⟩)) eq.symm) id, le_trans := λ x y z hxy hyz, hxy.elim (λ ⟨i, hi⟩, hyz.elim (λ ⟨j, hj⟩, or.inl ⟨by exactI min i j, by resetI; exact λ k hk, by rw [hi.1 _ (lt_min_iff.1 hk).1, hj.1 _ (lt_min_iff.1 hk).2], by resetI; exact (le_total i j).elim (λ hij, by rw [min_eq_left hij]; exact lt_of_lt_of_le hi.2 ((lt_or_eq_of_le hij).elim (λ h, le_of_eq (hj.1 _ h)) (λ h, h.symm ▸ le_of_lt hj.2))) (λ hji, by rw [min_eq_right hji]; exact lt_of_le_of_lt ((lt_or_eq_of_le hji).elim (λ h, le_of_eq (hi.1 _ h)) (λ h, h.symm ▸ le_of_lt hi.2)) hj.2)⟩) (λ hyz, hyz ▸ hxy)) (λ hxy, hxy.symm ▸ hyz), lt_iff_le_not_le := λ x y, show x < y ↔ (x < y ∨ x = y) ∧ ¬ (y < x ∨ y = x), from ⟨λ ⟨i, hi⟩, ⟨or.inl ⟨i, hi⟩, λ h, h.elim (λ ⟨j, hj⟩, begin rcases lt_trichotomy i j with hij | hij | hji, { exact lt_irrefl (x i) (by simpa [hj.1 _ hij] using hi.2) }, { exact not_le_of_gt hj.2 (hij ▸ le_of_lt hi.2) }, { exact lt_irrefl (x j) (by simpa [hi.1 _ hji] using hj.2) }, end) (λ hyx, lt_irrefl (x i) (by simpa [hyx] using hi.2))⟩, by tauto⟩, ..pilex.has_lt } /-- `pilex` is a linear order if the original order is well-founded. This cannot be an instance, since it depends on the well-foundedness of `<`. -/ protected def pilex.linear_order [linear_order ι] (wf : well_founded ((<) : ι → ι → Prop)) [∀ a, linear_order (β a)] : linear_order (pilex ι β) := { le_total := λ x y, by classical; exact or_iff_not_imp_left.2 (λ hxy, begin have := not_or_distrib.1 hxy, let i : ι := well_founded.min wf _ (not_forall.1 (this.2 ∘ funext)), have hjiyx : ∀ j < i, y j = x j, { assume j, rw [eq_comm, ← not_imp_not], exact λ h, well_founded.not_lt_min wf _ _ h }, refine or.inl ⟨i, hjiyx, _⟩, { refine lt_of_not_ge (λ hyx, _), exact this.1 ⟨i, (λ j hj, (hjiyx j hj).symm), lt_of_le_of_ne hyx (well_founded.min_mem _ {i | x i ≠ y i} _)⟩ } end), ..pilex.partial_order } instance [linear_order ι] [∀ a, ordered_add_comm_group (β a)] : ordered_add_comm_group (pilex ι β) := { add_le_add_left := λ x y hxy z, hxy.elim (λ ⟨i, hi⟩, or.inl ⟨i, λ j hji, show z j + x j = z j + y j, by rw [hi.1 j hji], add_lt_add_left hi.2 _⟩) (λ hxy, hxy ▸ le_refl _), ..pilex.partial_order, ..pi.add_comm_group }
c5d3d76ab026a310e1428b731c3e6183198b3345
5c7fe6c4a9d4079b5457ffa5f061797d42a1cd65
/src/exercises/src_21_natural_numbers.lean
6650a1477f33c917c391c9002107b28b83704a6e
[]
no_license
gihanmarasingha/mth1001_tutorial
8e0817feeb96e7c1bb3bac49b63e3c9a3a329061
bb277eebd5013766e1418365b91416b406275130
refs/heads/master
1,675,008,746,310
1,607,993,443,000
1,607,993,443,000
321,511,270
3
0
null
null
null
null
UTF-8
Lean
false
false
2,302
lean
import data.num.basic set_option pp.structure_projections false namespace mth1001 section setup @[derive has_reflect, derive decidable_eq] inductive mynat : Type | zero : mynat | S : mynat → mynat open mynat def add : mynat → mynat → mynat | a zero := a -- a + zero = a | a (S b) := S (add a b) -- a + (S b) = S (a + b) notation a `+` b := add a b end setup section addition open mynat lemma add_zero (a : mynat) : a + zero = a := rfl lemma add_S (a b : mynat) : a + S b = S (a + b) := rfl -- Exercise 117: lemma zero_add (a : mynat) : zero + a = a:= begin induction a with k hk, { sorry, }, { sorry, }, end -- Exercise 118: /- Note that multiple applications of `rw h` (for some lemma or equation `h`) can be replaced with `repeat {rw h}`. -/ lemma add_assoc (a b c : mynat) : (a + b) + c = a + (b + c) := begin induction c with k hk, { sorry, }, { sorry, }, end -- Exercise 119: lemma S_add (a b : mynat) : (S a) + b = S (a + b) := begin sorry end -- Exercise 120: lemma S_eq_add_one (a : mynat) : S a = a + S zero := begin sorry end -- Exercise 121: lemma add_comm (a b : mynat) : a + b = b + a := begin sorry end end addition section multiplication open mynat -- Next result adapted from data/nat/basic.lean inductive less_than_or_equal (a : mynat) : mynat → Prop | refl : less_than_or_equal a | step : Π {b}, less_than_or_equal b → less_than_or_equal (S b) def mul : mynat → mynat → mynat | a zero := zero | a (S b) := (mul a b) + a notation a `*` b := mul a b open mynat lemma mul_zero (a : mynat) : a * zero = zero := rfl lemma mul_S (a b : mynat) : a * (S b) = (a * b) + a := rfl -- Exercise 122: lemma zero_mul (a : mynat) : zero * a = zero := begin sorry end -- Exercise 123: lemma S_mul (a b : mynat) : (S a) * b = a * b + b := begin sorry end -- Exercise 124: lemma mul_comm (a b : mynat) : a * b = b * a := begin sorry end /- In the remainder of this section, we prove associativity of multiplication. -/ -- Exercise 125: lemma mul_add (a b c : mynat) : a * (b + c) = a * b + a * c := begin sorry end -- Exercise 126: lemma mul_assoc (a b c : mynat) : (a * b) * c = a * (b * c) := begin sorry end end multiplication end mth1001
afe43a4efbee75bf5bc6827f87c35b34cfe4aaff
2ee768fce4d0783536570385ee340d97e2411436
/src/continuity.lean
04d9058444cd225a09850fc3f021ae7db97c6e7b
[]
no_license
agjftucker/exists-unique
835cf0c0ec622d49dbceb84be37db62e6ab8a6d0
1fba6035a08347f424c765d8a1c56b1a15c8abbe
refs/heads/master
1,683,445,592,336
1,622,649,822,000
1,622,649,822,000
309,485,697
2
0
null
null
null
null
UTF-8
Lean
false
false
2,348
lean
import u_prop local prefix `𝒫`:100 := fun {α : Type} (s : finset α), {t // t ≤ s} variables {𝒩 : Type} {T : with_top ℝ} lemma join_continuous {t : Tt T} (f g : debt_fn 𝒩 T) : continuous (f t) → continuous (g t) → continuous ((f ⊔ g) t) := begin intros hf hg, rw continuous_pi_iff, intro i, rw continuous_pi_iff, intro τ, apply continuous.max, all_goals { revert τ, rw ←continuous_pi_iff, revert i, rw ←continuous_pi_iff, assumption, }, end variables [decidable_eq 𝒩] {ℋ : well_behaved_soln 𝒩 T} {ℰ : equity_function 𝒩 T} section v variable {ψ : ∀ (B : finset 𝒩), Tt T → X 𝒩 → 𝒫 B} lemma v_continuous_of_continuous_on_compl {A : finset 𝒩} : (∀ t, continuous_on (v ℋ ψ A t) (V ψ A t)ᶜ) → continuous_wrt_assets (v ℋ ψ A) := begin intros h t, rw show v ℋ ψ A t = fun y i, ite _ _ _, { funext y i, rw v_eq_ite, }, rw continuous_pi_iff, intro i, split_ifs, { revert t, apply ℋ.continuity_preserving, intro t, specialize h t, rw [continuous_on_iff_continuous_restrict, continuous_pi_iff] at h, exact h i, }, { exact continuous_const, }, end end v section u open finset lemma u_continuous_of_continuous_on_compl {A : finset 𝒩} : (∀ t, continuous_on (u ℋ ℰ A t) (U ℋ ℰ A t)ᶜ) → continuous_wrt_assets (u ℋ ℰ A) := v_continuous_of_continuous_on_compl lemma u_eq_on_compl {A : finset 𝒩} (hA : A.nonempty) {t : Tt T} : (U ℋ ℰ A t)ᶜ.eq_on (u ℋ ℰ A t) (A.ssubsets.sup' ⟨∅, empty_mem_ssubsets hA⟩ (u ℋ ℰ) t) := fun y, u_eq_sup' lemma uA_continuous_of_uB_continuous (A : finset 𝒩) : (∀ B < A, continuous_wrt_assets (u ℋ ℰ B)) → continuous_wrt_assets (u ℋ ℰ A) := begin intro ih, apply u_continuous_of_continuous_on_compl, intro t, cases A.eq_empty_or_nonempty with he hne, { rw [he, U_empty_eq_univ, set.compl_univ], apply continuous_on_empty, }, { rw continuous_on_congr (u_eq_on_compl hne), apply continuous.continuous_on, apply sup'_induction ⟨∅, empty_mem_ssubsets hne⟩ (u ℋ ℰ) join_continuous, intros B hB, apply ih, rwa mem_ssubsets at hB, }, end lemma u_continuous_wrt_assets : ∀ A, continuous_wrt_assets (u ℋ ℰ A) := finset.strong_induction uA_continuous_of_uB_continuous end u
5cf5e00bcc60e2e88ea22df2485ae2f95047948d
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/list/default.lean
3aedec00af09c45a5f53e91f2a2c95d164062715
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
1,005
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Jeremy Avigad -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.list.alist import Mathlib.data.list.bag_inter import Mathlib.data.list.basic import Mathlib.data.list.chain import Mathlib.data.list.defs import Mathlib.data.list.erase_dup import Mathlib.data.list.forall2 import Mathlib.data.list.func import Mathlib.data.list.intervals import Mathlib.data.list.min_max import Mathlib.data.list.indexes import Mathlib.data.list.nat_antidiagonal import Mathlib.data.list.nodup import Mathlib.data.list.of_fn import Mathlib.data.list.pairwise import Mathlib.data.list.perm import Mathlib.data.list.range import Mathlib.data.list.rotate import Mathlib.data.list.sections import Mathlib.data.list.sigma import Mathlib.data.list.sort import Mathlib.data.list.tfae import Mathlib.data.list.zip import Mathlib.PostPort namespace Mathlib
9bb62e7c0308649aabb00cb9485ae48e9cb648ee
f5f7e6fae601a5fe3cac7cc3ed353ed781d62419
/src/order/lexicographic.lean
898a59572e2963ff756ce0d54da974f781d75b2c
[ "Apache-2.0" ]
permissive
EdAyers/mathlib
9ecfb2f14bd6caad748b64c9c131befbff0fb4e0
ca5d4c1f16f9c451cf7170b10105d0051db79e1b
refs/heads/master
1,626,189,395,845
1,555,284,396,000
1,555,284,396,000
144,004,030
0
0
Apache-2.0
1,533,727,664,000
1,533,727,663,000
null
UTF-8
Lean
false
false
8,504
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Scott Morrison, Minchao Wu Lexicographic preorder / partial_order / linear_order / decidable_linear_order, for pairs and dependent pairs. -/ import order.basic import tactic.interactive universes u v def lex (α : Type u) (β : Type v) := α × β variables {α : Type u} {β : Type v} /-- 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₂⟩, rcases le_total a₁ a₂ with ha | ha; 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, rcases le_total b₁ b₂ with hb | hb }, { left, right, exact hb }, { right, right, exact hb }, { left, right, exact hb }, { right, right, exact hb }, end .. lex_partial_order }. /-- Dictionary / lexicographic decidable_linear_order for pairs. -/ instance lex_decidable_linear_order [decidable_linear_order α] [decidable_linear_order β] : decidable_linear_order (lex α β) := { decidable_le := begin rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩, rcases decidable_linear_order.decidable_le α a₁ a₂ with a_lt | a_le, { -- 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, rcases decidable_linear_order.decidable_le _ b₁ b₂ with b_lt | b_le, { -- 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_linear_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₂⟩, rcases le_total a₁ a₂ with ha | ha; 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, rcases le_total b₁ b₂ with hb | hb }, { left, right, exact hb }, { right, right, exact hb }, { left, right, exact hb }, { right, right, exact hb }, end .. dlex_partial_order }. /-- Dictionary / lexicographic decidable_linear_order for dependent pairs. -/ instance dlex_decidable_linear_order [decidable_linear_order α] [∀ a, decidable_linear_order (Z a)] : decidable_linear_order (Σ' a, Z a) := { decidable_le := begin rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩, rcases decidable_linear_order.decidable_le α a₁ a₂ with a_lt | a_le, { -- 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, rcases decidable_linear_order.decidable_le _ b₁ b₂ with b_lt | b_le, { -- 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_linear_order }
b96876de9d84fedfb4b6ced2c8bac989064b7f5c
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/number_theory/liouville/basic.lean
0d1d2eb417854f72b393818436aa355100eac58a
[ "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
12,278
lean
/- Copyright (c) 2020 Jujian Zhang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Damiano Testa, Jujian Zhang -/ import analysis.calculus.mean_value import data.polynomial.denoms_clearable import data.real.irrational /-! # Liouville's theorem This file contains a proof of Liouville's theorem stating that all Liouville numbers are transcendental. To obtain this result, there is first a proof that Liouville numbers are irrational and two technical lemmas. These lemmas exploit the fact that a polynomial with integer coefficients takes integer values at integers. When evaluating at a rational number, we can clear denominators and obtain precise inequalities that ultimately allow us to prove transcendence of Liouville numbers. -/ /-- A Liouville number is a real number `x` such that for every natural number `n`, there exist `a, b ∈ ℤ` with `1 < b` such that `0 < |x - a/b| < 1/bⁿ`. In the implementation, the condition `x ≠ a/b` replaces the traditional equivalent `0 < |x - a/b|`. -/ def liouville (x : ℝ) := ∀ n : ℕ, ∃ a b : ℤ, 1 < b ∧ x ≠ a / b ∧ |x - a / b| < 1 / b ^ n namespace liouville @[protected] lemma irrational {x : ℝ} (h : liouville x) : irrational x := begin -- By contradiction, `x = a / b`, with `a ∈ ℤ`, `0 < b ∈ ℕ` is a Liouville number, rintros ⟨⟨a, b, bN0, cop⟩, rfl⟩, -- clear up the mess of constructions of rationals change (liouville (a / b)) at h, -- Since `a / b` is a Liouville number, there are `p, q ∈ ℤ`, with `q1 : 1 < q`, -- `a0 : a / b ≠ p / q` and `a1 : |a / b - p / q| < 1 / q ^ (b + 1)` rcases h (b + 1) with ⟨p, q, q1, a0, a1⟩, -- A few useful inequalities have qR0 : (0 : ℝ) < q := int.cast_pos.mpr (zero_lt_one.trans q1), have b0 : (b : ℝ) ≠ 0 := ne_of_gt (nat.cast_pos.mpr bN0), have bq0 : (0 : ℝ) < b * q := mul_pos (nat.cast_pos.mpr bN0) qR0, -- At a1, clear denominators... replace a1 : |a * q - b * p| * q ^ (b + 1) < b * q, by rwa [div_sub_div _ _ b0 (ne_of_gt qR0), abs_div, div_lt_div_iff (abs_pos.mpr (ne_of_gt bq0)) (pow_pos qR0 _), abs_of_pos bq0, one_mul, -- ... and revert to integers ← int.cast_pow, ← int.cast_mul, ← int.cast_coe_nat, ← int.cast_mul, ← int.cast_mul, ← int.cast_sub, ← int.cast_abs, ← int.cast_mul, int.cast_lt] at a1, -- At a0, clear denominators... replace a0 : ¬a * q - ↑b * p = 0, by rwa [ne.def, div_eq_div_iff b0 (ne_of_gt qR0), mul_comm ↑p, ← sub_eq_zero, -- ... and revert to integers ← int.cast_coe_nat, ← int.cast_mul, ← int.cast_mul, ← int.cast_sub, int.cast_eq_zero] at a0, -- Actually, `q` is a natural number lift q to ℕ using (zero_lt_one.trans q1).le, -- Looks innocuous, but we now have an integer with non-zero absolute value: this is at -- least one away from zero. The gain here is what gets the proof going. have ap : 0 < |a * ↑q - ↑b * p| := abs_pos.mpr a0, -- Actually, the absolute value of an integer is a natural number lift (|a * ↑q - ↑b * p|) to ℕ using (abs_nonneg (a * ↑q - ↑b * p)), -- At a1, revert to natural numbers rw [← int.coe_nat_mul, ← int.coe_nat_pow, ← int.coe_nat_mul, int.coe_nat_lt] at a1, -- Recall this is by contradiction: we obtained the inequality `b * q ≤ x * q ^ (b + 1)`, so -- we are done. exact not_le.mpr a1 (nat.mul_lt_mul_pow_succ (int.coe_nat_pos.mp ap) (int.coe_nat_lt.mp q1)).le, end open polynomial metric set real ring_hom /-- Let `Z, N` be types, let `R` be a metric space, let `α : R` be a point and let `j : Z → N → R` be a function. We aim to estimate how close we can get to `α`, while staying in the image of `j`. The points `j z a` of `R` in the image of `j` come with a "cost" equal to `d a`. As we get closer to `α` while staying in the image of `j`, we are interested in bounding the quantity `d a * dist α (j z a)` from below by a strictly positive amount `1 / A`: the intuition is that approximating well `α` with the points in the image of `j` should come at a high cost. The hypotheses on the function `f : R → R` provide us with sufficient conditions to ensure our goal. The first hypothesis is that `f` is Lipschitz at `α`: this yields a bound on the distance. The second hypothesis is specific to the Liouville argument and provides the missing bound involving the cost function `d`. This lemma collects the properties used in the proof of `exists_pos_real_of_irrational_root`. It is stated in more general form than needed: in the intended application, `Z = ℤ`, `N = ℕ`, `R = ℝ`, `d a = (a + 1) ^ f.nat_degree`, `j z a = z / (a + 1)`, `f ∈ ℤ[x]`, `α` is an irrational root of `f`, `ε` is small, `M` is a bound on the Lipschitz constant of `f` near `α`, `n` is the degree of the polynomial `f`. -/ lemma exists_one_le_pow_mul_dist {Z N R : Type*} [metric_space R] {d : N → ℝ} {j : Z → N → R} {f : R → R} {α : R} {ε M : ℝ} -- denominators are positive (d0 : ∀ (a : N), 1 ≤ d a) (e0 : 0 < ε) -- function is Lipschitz at α (B : ∀ ⦃y : R⦄, y ∈ closed_ball α ε → dist (f α) (f y) ≤ (dist α y) * M) -- clear denominators (L : ∀ ⦃z : Z⦄, ∀ ⦃a : N⦄, j z a ∈ closed_ball α ε → 1 ≤ (d a) * dist (f α) (f (j z a))) : ∃ A : ℝ, 0 < A ∧ ∀ (z : Z), ∀ (a : N), 1 ≤ (d a) * (dist α (j z a) * A) := begin -- A useful inequality to keep at hand have me0 : 0 < max (1 / ε) M := lt_max_iff.mpr (or.inl (one_div_pos.mpr e0)), -- The maximum between `1 / ε` and `M` works refine ⟨max (1 / ε) M, me0, λ z a, _⟩, -- First, let's deal with the easy case in which we are far away from `α` by_cases dm1 : 1 ≤ (dist α (j z a) * max (1 / ε) M), { exact one_le_mul_of_one_le_of_one_le (d0 a) dm1 }, { -- `j z a = z / (a + 1)`: we prove that this ratio is close to `α` have : j z a ∈ closed_ball α ε, { refine mem_closed_ball'.mp (le_trans _ ((one_div_le me0 e0).mpr (le_max_left _ _))), exact ((le_div_iff me0).mpr (not_le.mp dm1).le) }, -- use the "separation from `1`" (assumption `L`) for numerators, refine (L this).trans _, -- remove a common factor and use the Lipschitz assumption `B` refine mul_le_mul_of_nonneg_left ((B this).trans _) (zero_le_one.trans (d0 a)), exact mul_le_mul_of_nonneg_left (le_max_right _ M) dist_nonneg } end lemma exists_pos_real_of_irrational_root {α : ℝ} (ha : irrational α) {f : polynomial ℤ} (f0 : f ≠ 0) (fa : eval α (map (algebra_map ℤ ℝ) f) = 0): ∃ A : ℝ, 0 < A ∧ ∀ (a : ℤ), ∀ (b : ℕ), (1 : ℝ) ≤ (b + 1) ^ f.nat_degree * (|α - (a / (b + 1))| * A) := begin -- `fR` is `f` viewed as a polynomial with `ℝ` coefficients. set fR : polynomial ℝ := map (algebra_map ℤ ℝ) f, -- `fR` is non-zero, since `f` is non-zero. obtain fR0 : fR ≠ 0 := λ fR0, (map_injective (algebra_map ℤ ℝ) (λ _ _ A, int.cast_inj.mp A)).ne f0 (fR0.trans (polynomial.map_zero _).symm), -- reformulating assumption `fa`: `α` is a root of `fR`. have ar : α ∈ (fR.roots.to_finset : set ℝ) := finset.mem_coe.mpr (multiset.mem_to_finset.mpr ((mem_roots fR0).mpr (is_root.def.mpr fa))), -- Since the polynomial `fR` has finitely many roots, there is a closed interval centered at `α` -- such that `α` is the only root of `fR` in the interval. obtain ⟨ζ, z0, U⟩ : ∃ ζ > 0, closed_ball α ζ ∩ (fR.roots.to_finset) = {α} := @exists_closed_ball_inter_eq_singleton_of_discrete _ _ _ discrete_of_t1_of_finite _ ar, -- Since `fR` is continuous, it is bounded on the interval above. obtain ⟨xm, -, hM⟩ : ∃ (xm : ℝ) (H : xm ∈ Icc (α - ζ) (α + ζ)), ∀ (y : ℝ), y ∈ Icc (α - ζ) (α + ζ) → |fR.derivative.eval y| ≤ |fR.derivative.eval xm| := is_compact.exists_forall_ge is_compact_Icc ⟨α, (sub_lt_self α z0).le, (lt_add_of_pos_right α z0).le⟩ (continuous_abs.comp fR.derivative.continuous_aeval).continuous_on, -- Use the key lemma `exists_one_le_pow_mul_dist`: we are left to show that ... refine @exists_one_le_pow_mul_dist ℤ ℕ ℝ _ _ _ (λ y, fR.eval y) α ζ (|fR.derivative.eval xm|) _ z0 (λ y hy, _) (λ z a hq, _), -- 1: the denominators are positive -- essentially by definition; { exact λ a, one_le_pow_of_one_le ((le_add_iff_nonneg_left 1).mpr a.cast_nonneg) _ }, -- 2: the polynomial `fR` is Lipschitz at `α` -- as its derivative continuous; { rw mul_comm, rw real.closed_ball_eq at hy, -- apply the Mean Value Theorem: the bound on the derivative comes from differentiability. refine convex.norm_image_sub_le_of_norm_deriv_le (λ _ _, fR.differentiable_at) (λ y h, by { rw fR.deriv, exact hM _ h }) (convex_Icc _ _) hy (mem_Icc_iff_abs_le.mp _), exact @mem_closed_ball_self ℝ _ α ζ (le_of_lt z0) }, -- 3: the weird inequality of Liouville type with powers of the denominators. { show 1 ≤ (a + 1 : ℝ) ^ f.nat_degree * |eval α fR - eval (z / (a + 1)) fR|, rw [fa, zero_sub, abs_neg], -- key observation: the right-hand side of the inequality is an *integer*. Therefore, -- if its absolute value is not at least one, then it vanishes. Proceed by contradiction refine one_le_pow_mul_abs_eval_div (int.coe_nat_succ_pos a) (λ hy, _), -- As the evaluation of the polynomial vanishes, we found a root of `fR` that is rational. -- We know that `α` is the only root of `fR` in our interval, and `α` is irrational: -- follow your nose. refine (irrational_iff_ne_rational α).mp ha z (a + 1) ((mem_singleton_iff.mp _).symm), refine U.subset _, refine ⟨hq, finset.mem_coe.mp (multiset.mem_to_finset.mpr _)⟩, exact (mem_roots fR0).mpr (is_root.def.mpr hy) } end /-- **Liouville's Theorem** -/ theorem transcendental {x : ℝ} (lx : liouville x) : transcendental ℤ x := begin -- Proceed by contradiction: if `x` is algebraic, then `x` is the root (`ef0`) of a -- non-zero (`f0`) polynomial `f` rintros ⟨f : polynomial ℤ, f0, ef0⟩, -- Change `aeval x f = 0` to `eval (map _ f) = 0`, who knew. replace ef0 : (f.map (algebra_map ℤ ℝ)).eval x = 0, { rwa [aeval_def, ← eval_map] at ef0 }, -- There is a "large" real number `A` such that `(b + 1) ^ (deg f) * |f (x - a / (b + 1))| * A` -- is at least one. This is obtained from lemma `exists_pos_real_of_irrational_root`. obtain ⟨A, hA, h⟩ : ∃ (A : ℝ), 0 < A ∧ ∀ (a : ℤ) (b : ℕ), (1 : ℝ) ≤ (b.succ) ^ f.nat_degree * (|x - a / (b.succ)| * A) := exists_pos_real_of_irrational_root lx.irrational f0 ef0, -- Since the real numbers are Archimedean, a power of `2` exceeds `A`: `hn : A < 2 ^ r`. rcases pow_unbounded_of_one_lt A (lt_add_one 1) with ⟨r, hn⟩, -- Use the Liouville property, with exponent `r + deg f`. obtain ⟨a, b, b1, -, a1⟩ : ∃ (a b : ℤ), 1 < b ∧ x ≠ a / b ∧ |x - a / b| < 1 / b ^ (r + f.nat_degree) := lx (r + f.nat_degree), have b0 : (0 : ℝ) < b := zero_lt_one.trans (by { rw ← int.cast_one, exact int.cast_lt.mpr b1 }), -- Prove that `b ^ f.nat_degree * abs (x - a / b)` is strictly smaller than itself -- recall, this is a proof by contradiction! refine lt_irrefl ((b : ℝ) ^ f.nat_degree * |x - ↑a / ↑b|) _, -- clear denominators at `a1` rw [lt_div_iff' (pow_pos b0 _), pow_add, mul_assoc] at a1, -- split the inequality via `1 / A`. refine ((_ : (b : ℝ) ^ f.nat_degree * |x - a / b| < 1 / A).trans_le _), -- This branch of the proof uses the Liouville condition and the Archimedean property { refine (lt_div_iff' hA).mpr _, refine lt_of_le_of_lt _ a1, refine mul_le_mul_of_nonneg_right _ (mul_nonneg (pow_nonneg b0.le _) (abs_nonneg _)), refine hn.le.trans _, refine pow_le_pow_of_le_left zero_le_two _ _, exact int.cast_two.symm.le.trans (int.cast_le.mpr (int.add_one_le_iff.mpr b1)) }, -- this branch of the proof exploits the "integrality" of evaluations of polynomials -- at ratios of integers. { lift b to ℕ using zero_le_one.trans b1.le, specialize h a b.pred, rwa [nat.succ_pred_eq_of_pos (zero_lt_one.trans _), ← mul_assoc, ← (div_le_iff hA)] at h, exact int.coe_nat_lt.mp b1 } end end liouville
d1b3e60973c0c7b35dfa562a7a4afaf594a3244b
07c76fbd96ea1786cc6392fa834be62643cea420
/hott/init/pathover.hlean
fe10d96a7428dc22474a7260cde4f6c62f68e7fe
[ "Apache-2.0" ]
permissive
fpvandoorn/lean2
5a430a153b570bf70dc8526d06f18fc000a60ad9
0889cf65b7b3cebfb8831b8731d89c2453dd1e9f
refs/heads/master
1,592,036,508,364
1,545,093,958,000
1,545,093,958,000
75,436,854
0
0
null
1,480,718,780,000
1,480,718,780,000
null
UTF-8
Lean
false
false
20,718
hlean
/- Copyright (c) 2015 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Floris van Doorn Basic theorems about pathovers -/ prelude import .path .equiv open equiv is_equiv function variables {A A' : Type} {B B' : A → Type} {B'' : A' → Type} {C : Π⦃a⦄, B a → Type} {a a₂ a₃ a₄ : A} {p p' : a = a₂} {p₂ : a₂ = a₃} {p₃ : a₃ = a₄} {p₁₃ : a = a₃} {a' a₂' a₃' : A'} {b b' : B a} {b₂ b₂' : B a₂} {b₃ : B a₃} {b₄ : B a₄} {c : C b} {c₂ : C b₂} namespace eq inductive pathover.{l} (B : A → Type.{l}) (b : B a) : Π{a₂ : A}, a = a₂ → B a₂ → Type.{l} := idpatho : pathover B b (refl a) b notation b ` =[`:50 p:0 `] `:0 b₂:50 := pathover _ b p b₂ notation b ` =[`:50 p:0 `; `:0 B `] `:0 b₂:50 := pathover B b p b₂ definition idpo [reducible] [constructor] : b =[refl a] b := pathover.idpatho b definition idpatho [reducible] [constructor] (b : B a) : b =[refl a] b := pathover.idpatho b /- equivalences with equality using transport -/ definition pathover_of_tr_eq [unfold 5 8] (r : p ▸ b = b₂) : b =[p] b₂ := by cases p; cases r; constructor definition pathover_of_eq_tr [unfold 5 8] (r : b = p⁻¹ ▸ b₂) : b =[p] b₂ := by cases p; cases r; constructor definition tr_eq_of_pathover [unfold 8] (r : b =[p] b₂) : p ▸ b = b₂ := by cases r; reflexivity definition eq_tr_of_pathover [unfold 8] (r : b =[p] b₂) : b = p⁻¹ ▸ b₂ := by cases r; reflexivity definition pathover_equiv_tr_eq [constructor] (p : a = a₂) (b : B a) (b₂ : B a₂) : (b =[p] b₂) ≃ (p ▸ b = b₂) := begin fapply equiv.MK, { exact tr_eq_of_pathover}, { exact pathover_of_tr_eq}, { intro r, cases p, cases r, apply idp}, { intro r, cases r, apply idp}, end definition pathover_equiv_eq_tr [constructor] (p : a = a₂) (b : B a) (b₂ : B a₂) : (b =[p] b₂) ≃ (b = p⁻¹ ▸ b₂) := begin fapply equiv.MK, { exact eq_tr_of_pathover}, { exact pathover_of_eq_tr}, { intro r, cases p, cases r, apply idp}, { intro r, cases r, apply idp}, end definition pathover_tr [unfold 5] (p : a = a₂) (b : B a) : b =[p] p ▸ b := by cases p; constructor definition tr_pathover [unfold 5] (p : a = a₂) (b : B a₂) : p⁻¹ ▸ b =[p] b := by cases p; constructor definition concato [unfold 12] (r : b =[p] b₂) (r₂ : b₂ =[p₂] b₃) : b =[p ⬝ p₂] b₃ := by induction r₂; exact r definition inverseo [unfold 8] (r : b =[p] b₂) : b₂ =[p⁻¹] b := by induction r; constructor definition concato_eq [unfold 10] (r : b =[p] b₂) (q : b₂ = b₂') : b =[p] b₂' := by induction q; exact r definition eq_concato [unfold 9] (q : b = b') (r : b' =[p] b₂) : b =[p] b₂ := by induction q; exact r definition change_path [unfold 9] (q : p = p') (r : b =[p] b₂) : b =[p'] b₂ := q ▸ r definition change_path_idp [unfold_full] (r : b =[p] b₂) : change_path idp r = r := by reflexivity -- infix ` ⬝ ` := concato infix ` ⬝o `:72 := concato infix ` ⬝op `:73 := concato_eq infix ` ⬝po `:74 := eq_concato -- postfix `⁻¹` := inverseo postfix `⁻¹ᵒ`:(max+10) := inverseo definition pathover_cancel_right (q : b =[p ⬝ p₂] b₃) (r : b₃ =[p₂⁻¹] b₂) : b =[p] b₂ := change_path !con_inv_cancel_right (q ⬝o r) definition pathover_cancel_right' (q : b =[p₁₃ ⬝ p₂⁻¹] b₂) (r : b₂ =[p₂] b₃) : b =[p₁₃] b₃ := change_path !inv_con_cancel_right (q ⬝o r) definition pathover_cancel_left (q : b₂ =[p⁻¹] b) (r : b =[p ⬝ p₂] b₃) : b₂ =[p₂] b₃ := change_path !inv_con_cancel_left (q ⬝o r) definition pathover_cancel_left' (q : b =[p] b₂) (r : b₂ =[p⁻¹ ⬝ p₁₃] b₃) : b =[p₁₃] b₃ := change_path !con_inv_cancel_left (q ⬝o r) /- Some of the theorems analogous to theorems for = in init.path -/ definition cono_idpo (r : b =[p] b₂) : r ⬝o idpo = r := by reflexivity definition idpo_cono (r : b =[p] b₂) : idpo ⬝o r =[idp_con p] r := by induction r; constructor definition cono.assoc' (r : b =[p] b₂) (r₂ : b₂ =[p₂] b₃) (r₃ : b₃ =[p₃] b₄) : r ⬝o (r₂ ⬝o r₃) =[!con.assoc'] (r ⬝o r₂) ⬝o r₃ := by induction r₃; constructor definition cono.assoc (r : b =[p] b₂) (r₂ : b₂ =[p₂] b₃) (r₃ : b₃ =[p₃] b₄) : (r ⬝o r₂) ⬝o r₃ =[!con.assoc] r ⬝o (r₂ ⬝o r₃) := by induction r₃; constructor definition cono.right_inv (r : b =[p] b₂) : r ⬝o r⁻¹ᵒ =[!con.right_inv] idpo := by induction r; constructor definition cono.left_inv (r : b =[p] b₂) : r⁻¹ᵒ ⬝o r =[!con.left_inv] idpo := by induction r; constructor definition eq_of_pathover (q : a' =[p] a₂') : a' = a₂' := by cases q;reflexivity definition pathover_of_eq [unfold 5 8] (p : a = a₂) (q : a' = a₂') : a' =[p] a₂' := by cases p;cases q;constructor definition pathover_constant [constructor] (p : a = a₂) (a' a₂' : A') : a' =[p] a₂' ≃ a' = a₂' := begin fapply equiv.MK, { exact eq_of_pathover}, { exact pathover_of_eq p}, { intro r, cases p, cases r, reflexivity}, { intro r, cases r, reflexivity}, end definition pathover_of_eq_tr_constant_inv (p : a = a₂) (a' : A') : pathover_of_eq p (tr_constant p a')⁻¹ = pathover_tr p a' := by cases p; constructor definition eq_of_pathover_idp [unfold 6] {b' : B a} (q : b =[idpath a] b') : b = b' := tr_eq_of_pathover q --should B be explicit in the next two definitions? definition pathover_idp_of_eq [unfold 6] {b' : B a} (q : b = b') : b =[idpath a] b' := pathover_of_tr_eq q definition pathover_idp [constructor] (b : B a) (b' : B a) : b =[idpath a] b' ≃ b = b' := equiv.MK eq_of_pathover_idp (pathover_idp_of_eq) (to_right_inv !pathover_equiv_tr_eq) (to_left_inv !pathover_equiv_tr_eq) definition eq_of_pathover_idp_pathover_of_eq (a' : A') (p : a = a₂) : eq_of_pathover_idp (pathover_of_eq (idpath a') p) = p := by induction p; reflexivity variable (B) definition idpo_concato_eq (r : b = b') : eq_of_pathover_idp (@idpo A B a b ⬝op r) = r := by induction r; reflexivity variable {B} -- definition pathover_idp (b : B a) (b' : B a) : b =[idpath a] b' ≃ b = b' := -- pathover_equiv_tr_eq idp b b' -- definition eq_of_pathover_idp [reducible] {b' : B a} (q : b =[idpath a] b') : b = b' := -- to_fun !pathover_idp q -- definition pathover_idp_of_eq [reducible] {b' : B a} (q : b = b') : b =[idpath a] b' := -- to_inv !pathover_idp q definition idp_rec_on [recursor] [unfold 7] {P : Π⦃b₂ : B a⦄, b =[idpath a] b₂ → Type} {b₂ : B a} (r : b =[idpath a] b₂) (H : P idpo) : P r := have H2 : P (pathover_idp_of_eq (eq_of_pathover_idp r)), from eq.rec_on (eq_of_pathover_idp r) H, proof left_inv !pathover_idp r ▸ H2 qed definition rec_on_right [recursor] {P : Π⦃b₂ : B a₂⦄, b =[p] b₂ → Type} {b₂ : B a₂} (r : b =[p] b₂) (H : P !pathover_tr) : P r := by cases r; exact H definition rec_on_left [recursor] {P : Π⦃b : B a⦄, b =[p] b₂ → Type} {b : B a} (r : b =[p] b₂) (H : P !tr_pathover) : P r := by cases r; exact H --pathover with fibration B' ∘ f definition pathover_ap [unfold 10] (B' : A' → Type) (f : A → A') {p : a = a₂} {b : B' (f a)} {b₂ : B' (f a₂)} (q : b =[p] b₂) : b =[ap f p] b₂ := by cases q; constructor definition pathover_of_pathover_ap (B' : A' → Type) (f : A → A') {p : a = a₂} {b : B' (f a)} {b₂ : B' (f a₂)} (q : b =[ap f p] b₂) : b =[p] b₂ := by cases p; apply (idp_rec_on q); apply idpo definition pathover_compose [constructor] (B' : A' → Type) (f : A → A') (p : a = a₂) (b : B' (f a)) (b₂ : B' (f a₂)) : b =[p] b₂ ≃ b =[ap f p] b₂ := begin fapply equiv.MK, { exact pathover_ap B' f}, { exact pathover_of_pathover_ap B' f}, { intro q, cases p, esimp, apply (idp_rec_on q), apply idp}, { intro q, cases q, reflexivity}, end definition pathover_of_pathover_tr (q : b =[p ⬝ p₂] p₂ ▸ b₂) : b =[p] b₂ := pathover_cancel_right q !pathover_tr⁻¹ᵒ definition pathover_tr_of_pathover (q : b =[p₁₃ ⬝ p₂⁻¹] b₂) : b =[p₁₃] p₂ ▸ b₂ := pathover_cancel_right' q !pathover_tr definition pathover_of_tr_pathover (q : p ▸ b =[p⁻¹ ⬝ p₁₃] b₃) : b =[p₁₃] b₃ := pathover_cancel_left' !pathover_tr q definition tr_pathover_of_pathover (q : b =[p ⬝ p₂] b₃) : p ▸ b =[p₂] b₃ := pathover_cancel_left !pathover_tr⁻¹ᵒ q definition pathover_tr_of_eq (q : b = b') : b =[p] p ▸ b' := by cases q;apply pathover_tr definition tr_pathover_of_eq (q : b₂ = b₂') : p⁻¹ ▸ b₂ =[p] b₂' := by cases q;apply tr_pathover definition eq_of_parallel_po_right (q : b =[p] b₂) (q' : b =[p] b₂') : b₂ = b₂' := begin apply @eq_of_pathover_idp A B, apply change_path (con.left_inv p), exact q⁻¹ᵒ ⬝o q' end definition eq_of_parallel_po_left (q : b =[p] b₂) (q' : b' =[p] b₂) : b = b' := begin apply @eq_of_pathover_idp A B, apply change_path (con.right_inv p), exact q ⬝o q'⁻¹ᵒ end variable (C) definition transporto [unfold 9] (r : b =[p] b₂) (c : C b) : C b₂ := by induction r;exact c infix ` ▸o `:75 := transporto _ definition fn_tro_eq_tro_fn {C' : Π ⦃a : A⦄, B a → Type} (q : b =[p] b₂) (f : Π⦃a : A⦄ (b : B a), C b → C' b) (c : C b) : f b₂ (q ▸o c) = q ▸o (f b c) := by induction q; reflexivity variable {C} /- various variants of ap for pathovers -/ definition apd [unfold 6] (f : Πa, B a) (p : a = a₂) : f a =[p] f a₂ := by induction p; constructor definition apd_idp [unfold_full] (f : Πa, B a) : apd f idp = @idpo A B a (f a) := by reflexivity definition apo [unfold 12] {f : A → A'} (g : Πa, B a → B'' (f a)) (q : b =[p] b₂) : g a b =[p] g a₂ b₂ := by induction q; constructor definition apd011 [unfold 10] (f : Πa, B a → A') (Ha : a = a₂) (Hb : b =[Ha] b₂) : f a b = f a₂ b₂ := by cases Hb; reflexivity definition apd0111 [unfold 13 14] (f : Πa b, C b → A') (Ha : a = a₂) (Hb : b =[Ha] b₂) (Hc : c =[apd011 C Ha Hb] c₂) : f a b c = f a₂ b₂ c₂ := by cases Hb; apply (idp_rec_on Hc); apply idp definition apod11 {f : Πb, C b} {g : Πb₂, C b₂} (r : f =[p] g) {b : B a} {b₂ : B a₂} (q : b =[p] b₂) : f b =[apd011 C p q] g b₂ := by cases r; apply (idp_rec_on q); constructor definition apdo10 {f : Πb, C b} {g : Πb₂, C b₂} (r : f =[p] g) (b : B a) : f b =[apd011 C p !pathover_tr] g (p ▸ b) := by cases r; constructor definition apo10 [unfold 9] {f : B a → B' a} {g : B a₂ → B' a₂} (r : f =[p] g) (b : B a) : f b =[p] g (p ▸ b) := by cases r; constructor definition apo10_constant_right [unfold 9] {f : B a → A'} {g : B a₂ → A'} (r : f =[p] g) (b : B a) : f b = g (p ▸ b) := by cases r; constructor definition apo10_constant_left [unfold 9] {f : A' → B a} {g : A' → B a₂} (r : f =[p] g) (a' : A') : f a' =[p] g a' := by cases r; constructor definition apo11 {f : B a → B' a} {g : B a₂ → B' a₂} (r : f =[p] g) (q : b =[p] b₂) : f b =[p] g b₂ := by induction q; exact apo10 r b definition apo011 {A : Type} {B C D : A → Type} {a a' : A} {p : a = a'} {b : B a} {b' : B a'} {c : C a} {c' : C a'} (f : Π⦃a⦄, B a → C a → D a) (q : b =[p] b') (r : c =[p] c') : f b c =[p] f b' c' := begin induction q, induction r using idp_rec_on, exact idpo end definition apdo011 {A : Type} {B : A → Type} {C : Π⦃a⦄, B a → Type} (f : Π⦃a⦄ (b : B a), C b) {a a' : A} (p : a = a') {b : B a} {b' : B a'} (q : b =[p] b') : f b =[apd011 C p q] f b' := by cases q; constructor definition apdo0111 {A : Type} {B : A → Type} {C C' : Π⦃a⦄, B a → Type} (f : Π⦃a⦄ {b : B a}, C b → C' b) {a a' : A} (p : a = a') {b : B a} {b' : B a'} (q : b =[p] b') {c : C b} {c' : C b'} (r : c =[apd011 C p q] c') : f c =[apd011 C' p q] f c' := begin induction q, esimp at r, induction r using idp_rec_on, constructor end definition apo11_constant_right [unfold 12] {f : B a → A'} {g : B a₂ → A'} (q : f =[p] g) (r : b =[p] b₂) : f b = g b₂ := eq_of_pathover (apo11 q r) definition apd02 [unfold 8] (f : Πa, B a) {a a' : A} {p q : a = a'} (r : p = q) : change_path r (apd f p) = apd f q := by induction r; reflexivity /- properties about these "ap"s, transporto and pathover_ap -/ definition apd_con (f : Πa, B a) (p : a = a₂) (q : a₂ = a₃) : apd f (p ⬝ q) = apd f p ⬝o apd f q := by cases p; cases q; reflexivity definition apd_inv (f : Πa, B a) (p : a = a₂) : apd f p⁻¹ = (apd f p)⁻¹ᵒ := by cases p; reflexivity /- probably more useful: apd_eq_ap -/ definition apd_eq_pathover_of_eq_ap (f : A → A') (p : a = a₂) : apd f p = pathover_of_eq p (ap f p) := eq.rec_on p idp definition apo_invo (f : Πa, B a → B' a) {Ha : a = a₂} (Hb : b =[Ha] b₂) : (apo f Hb)⁻¹ᵒ = apo f Hb⁻¹ᵒ := by induction Hb; reflexivity definition apd011_inv (f : Πa, B a → A') (Ha : a = a₂) (Hb : b =[Ha] b₂) : (apd011 f Ha Hb)⁻¹ = (apd011 f Ha⁻¹ Hb⁻¹ᵒ) := by induction Hb; reflexivity definition cast_apd011 (q : b =[p] b₂) (c : C b) : cast (apd011 C p q) c = q ▸o c := by induction q; reflexivity definition apd_compose1 (g : Πa, B a → B' a) (f : Πa, B a) (p : a = a₂) : apd (g ∘' f) p = apo g (apd f p) := by induction p; reflexivity definition apd_compose2 (g : Πa', B'' a') (f : A → A') (p : a = a₂) : apd (λa, g (f a)) p = pathover_of_pathover_ap B'' f (apd g (ap f p)) := by induction p; reflexivity definition apo_tro (C : Π⦃a⦄, B' a → Type) (f : Π⦃a⦄, B a → B' a) (q : b =[p] b₂) (c : C (f b)) : apo f q ▸o c = q ▸o c := by induction q; reflexivity definition pathover_ap_tro {B' : A' → Type} (C : Π⦃a'⦄, B' a' → Type) (f : A → A') {b : B' (f a)} {b₂ : B' (f a₂)} (q : b =[p] b₂) (c : C b) : pathover_ap B' f q ▸o c = q ▸o c := by induction q; reflexivity definition pathover_ap_invo_tro {B' : A' → Type} (C : Π⦃a'⦄, B' a' → Type) (f : A → A') {b : B' (f a)} {b₂ : B' (f a₂)} (q : b =[p] b₂) (c : C b₂) : (pathover_ap B' f q)⁻¹ᵒ ▸o c = q⁻¹ᵒ ▸o c := by induction q; reflexivity definition pathover_tro (q : b =[p] b₂) (c : C b) : c =[apd011 C p q] q ▸o c := by induction q; constructor definition pathover_ap_invo {B' : A' → Type} (f : A → A') {p : a = a₂} {b : B' (f a)} {b₂ : B' (f a₂)} (q : b =[p] b₂) : pathover_ap B' f q⁻¹ᵒ =[ap_inv f p] (pathover_ap B' f q)⁻¹ᵒ := by induction q; exact idpo definition tro_invo_tro {A : Type} {B : A → Type} (C : Π⦃a⦄, B a → Type) {a a' : A} {p : a = a'} {b : B a} {b' : B a'} (q : b =[p] b') (c : C b') : q ▸o (q⁻¹ᵒ ▸o c) = c := by induction q; reflexivity definition invo_tro_tro {A : Type} {B : A → Type} (C : Π⦃a⦄, B a → Type) {a a' : A} {p : a = a'} {b : B a} {b' : B a'} (q : b =[p] b') (c : C b) : q⁻¹ᵒ ▸o (q ▸o c) = c := by induction q; reflexivity definition cono_tro {A : Type} {B : A → Type} (C : Π⦃a⦄, B a → Type) {a₁ a₂ a₃ : A} {p₁ : a₁ = a₂} {p₂ : a₂ = a₃} {b₁ : B a₁} {b₂ : B a₂} {b₃ : B a₃} (q₁ : b₁ =[p₁] b₂) (q₂ : b₂ =[p₂] b₃) (c : C b₁) : transporto C (q₁ ⬝o q₂) c = transporto C q₂ (transporto C q₁ c) := by induction q₂; reflexivity definition is_equiv_transporto [constructor] {A : Type} {B : A → Type} (C : Π⦃a⦄, B a → Type) {a a' : A} {p : a = a'} {b : B a} {b' : B a'} (q : b =[p] b') : is_equiv (transporto C q) := begin fapply adjointify, { exact transporto C q⁻¹ᵒ}, { exact tro_invo_tro C q}, { exact invo_tro_tro C q} end definition equiv_apd011 [constructor] {A : Type} {B : A → Type} (C : Π⦃a⦄, B a → Type) {a a' : A} {p : a = a'} {b : B a} {b' : B a'} (q : b =[p] b') : C b ≃ C b' := equiv.mk (transporto C q) !is_equiv_transporto /- some cancellation laws for concato_eq an variants -/ definition cono.right_inv_eq (q : b = b') : pathover_idp_of_eq q ⬝op q⁻¹ = (idpo : b =[refl a] b) := by induction q; constructor definition cono.right_inv_eq' (q : b = b') : q ⬝po (pathover_idp_of_eq q⁻¹) = (idpo : b =[refl a] b) := by induction q; constructor definition cono.left_inv_eq (q : b = b') : pathover_idp_of_eq q⁻¹ ⬝op q = (idpo : b' =[refl a] b') := by induction q; constructor definition cono.left_inv_eq' (q : b = b') : q⁻¹ ⬝po pathover_idp_of_eq q = (idpo : b' =[refl a] b') := by induction q; constructor definition pathover_of_fn_pathover_fn (f : Π{a}, B a ≃ B' a) (r : f b =[p] f b₂) : b =[p] b₂ := (left_inv f b)⁻¹ ⬝po apo (λa, f⁻¹ᵉ) r ⬝op left_inv f b₂ /- a pathover in a pathover type where the only thing which varies is the path is the same as an equality with a change_path -/ definition change_path_of_pathover (s : p = p') (r : b =[p] b₂) (r' : b =[p'] b₂) (q : r =[s] r') : change_path s r = r' := by induction s; eapply idp_rec_on q; reflexivity definition pathover_of_change_path (s : p = p') (r : b =[p] b₂) (r' : b =[p'] b₂) (q : change_path s r = r') : r =[s] r' := by induction s; induction q; constructor definition pathover_pathover_path [constructor] (s : p = p') (r : b =[p] b₂) (r' : b =[p'] b₂) : (r =[s] r') ≃ change_path s r = r' := begin fapply equiv.MK, { apply change_path_of_pathover}, { apply pathover_of_change_path}, { intro q, induction s, induction q, reflexivity}, { intro q, induction s, eapply idp_rec_on q, reflexivity}, end /- variants of inverse2 and concat2 -/ definition inverseo2 [unfold 10] {r r' : b =[p] b₂} (s : r = r') : r⁻¹ᵒ = r'⁻¹ᵒ := by induction s; reflexivity definition concato2 [unfold 15 16] {r r' : b =[p] b₂} {r₂ r₂' : b₂ =[p₂] b₃} (s : r = r') (s₂ : r₂ = r₂') : r ⬝o r₂ = r' ⬝o r₂' := by induction s; induction s₂; reflexivity infixl ` ◾o `:79 := concato2 postfix [parsing_only] `⁻²ᵒ`:(max+10) := inverseo2 --this notation is abusive, should we use it? -- find a better name for this definition fn_tro_eq_tro_fn2 (q : b =[p] b₂) {k : A → A} {l : Π⦃a⦄, B a → B (k a)} (m : Π⦃a⦄ {b : B a}, C b → C (l b)) (c : C b) : m (q ▸o c) = (pathover_ap B k (apo l q)) ▸o (m c) := by induction q; reflexivity definition apd0111_precompose (f : Π⦃a⦄ {b : B a}, C b → A') {k : A → A} {l : Π⦃a⦄, B a → B (k a)} (m : Π⦃a⦄ {b : B a}, C b → C (l b)) {q : b =[p] b₂} (c : C b) : apd0111 (λa b c, f (m c)) p q (pathover_tro q c) ⬝ ap (@f _ _) (fn_tro_eq_tro_fn2 q m c) = apd0111 f (ap k p) (pathover_ap B k (apo l q)) (pathover_tro _ (m c)) := by induction q; reflexivity /- some properties about eq_of_pathover -/ definition apd_eq_ap (f : A → A') (p : a = a₂) : eq_of_pathover (apd f p) = ap f p := begin induction p, reflexivity end definition eq_of_pathover_idp_constant (p : a' =[idpath a] a₂') : eq_of_pathover_idp p = eq_of_pathover p := begin induction p using idp_rec_on, reflexivity end definition eq_of_pathover_change_path (r : p = p') (q : a' =[p] a₂') : eq_of_pathover (change_path r q) = eq_of_pathover q := begin induction r, reflexivity end definition eq_of_pathover_cono (q : a' =[p] a₂') (q₂ : a₂' =[p₂] a₃') : eq_of_pathover (q ⬝o q₂) = eq_of_pathover q ⬝ eq_of_pathover q₂ := begin induction q₂, reflexivity end definition eq_of_pathover_invo (q : a' =[p] a₂') : eq_of_pathover q⁻¹ᵒ = (eq_of_pathover q)⁻¹ := begin induction q, reflexivity end definition eq_of_pathover_concato_eq (q : a' =[p] a₂') (q₂ : a₂' = a₃') : eq_of_pathover (q ⬝op q₂) = eq_of_pathover q ⬝ q₂ := begin induction q₂, reflexivity end definition eq_of_pathover_eq_concato (q : a' = a₂') (q₂ : a₂' =[p₂] a₃') : eq_of_pathover (q ⬝po q₂) = q ⬝ eq_of_pathover q₂ := begin induction q, induction q₂, reflexivity end end eq
b93b26e3fa947ff56bb0bf24acf8518a99c51193
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/simpMatch.lean
68637ee6ee1618ac5fb660aa5319eb41ac8dd959
[ "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
195
lean
def f (x y : Nat) : Nat := match x, y with | 0, 0 => 1 | _, _ => 2 example (h : f x y = 1) : f x y ≠ 2 := by simp [f] at * split next => decide next x' y' hnp => simp [hnp] at h
524f7bcad1a4c13f4d0cc342fe130a729a091985
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/noncomp_theory.lean
efdac7d96c36e2a1b6f4f182207ae02d1683c44a
[ "Apache-2.0" ]
permissive
soonhokong/lean
cb8aa01055ffe2af0fb99a16b4cda8463b882cd1
38607e3eb57f57f77c0ac114ad169e9e4262e24f
refs/heads/master
1,611,187,284,081
1,450,766,737,000
1,476,122,547,000
11,513,992
2
0
null
1,401,763,102,000
1,374,182,235,000
C++
UTF-8
Lean
false
false
417
lean
import data.real open real definition f (a b : real) := (a + a) / b -- ERROR f is noncomputable noncomputable definition f (a b : real) := (a + a) / b -- ERROR f is noncomputable noncomputable theory definition g (a b : real) := (a + a) / b definition h (a b : real) := (a - a) / b definition f₂ (a b : real) := (a * a) / b definition r (a : nat) := a print g -- g is still marked as noncomputable print r
741885d4356b3151c108f90582e597d3d5557bdd
aa5a655c05e5359a70646b7154e7cac59f0b4132
/stage0/src/Lean/ScopedEnvExtension.lean
3ad55cc6d83ab861e1c095ffc626d2bd4752d107
[ "Apache-2.0" ]
permissive
lambdaxymox/lean4
ae943c960a42247e06eff25c35338268d07454cb
278d47c77270664ef29715faab467feac8a0f446
refs/heads/master
1,677,891,867,340
1,612,500,005,000
1,612,500,005,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
8,060
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Environment import Lean.Data.NameTrie import Lean.Attributes namespace Lean namespace ScopedEnvExtension inductive Entry (α : Type) where | global : α → Entry α | «scoped» : Name → α → Entry α structure State (σ : Type) where state : σ activeScopes : NameSet := {} structure ScopedEntries (β : Type) where map : SMap Name (Std.PArray β) := {} deriving Inhabited structure StateStack (α : Type) (β : Type) (σ : Type) where stateStack : List (State σ) := {} scopedEntries : ScopedEntries β := {} newEntries : List (Entry α) := [] deriving Inhabited structure Descr (α : Type) (β : Type) (σ : Type) where name : Name mkInitial : IO σ ofOLeanEntry : σ → α → ImportM β toOLeanEntry : β → α addEntry : σ → β → σ instance [Inhabited α] : Inhabited (Descr α β σ) where default := { name := arbitrary mkInitial := arbitrary ofOLeanEntry := arbitrary toOLeanEntry := arbitrary addEntry := fun s _ => s } def mkInitial (descr : Descr α β σ) : IO (StateStack α β σ) := return { stateStack := [ { state := (← descr.mkInitial ) } ] } def ScopedEntries.insert (scopedEntries : ScopedEntries β) (ns : Name) (b : β) : ScopedEntries β := match scopedEntries.map.find? ns with | none => { map := scopedEntries.map.insert ns <| ({} : Std.PArray β).push b } | some bs => { map := scopedEntries.map.insert ns <| bs.push b } def addImportedFn (descr : Descr α β σ) (as : Array (Array (Entry α))) : ImportM (StateStack α β σ) := do let mut s ← descr.mkInitial let mut scopedEntries : ScopedEntries β := {} for a in as do for e in a do match e with | Entry.global a => let b ← descr.ofOLeanEntry s a s := descr.addEntry s b | Entry.scoped ns a => let b ← descr.ofOLeanEntry s a scopedEntries := scopedEntries.insert ns b return { stateStack := [ { state := s } ], scopedEntries := scopedEntries } def addEntryFn (descr : Descr α β σ) (s : StateStack α β σ) (e : Entry β) : StateStack α β σ := match s with | { stateStack := stateStack, scopedEntries := scopedEntries, newEntries := newEntries } => match e with | Entry.global b => { scopedEntries := scopedEntries newEntries := (Entry.global (descr.toOLeanEntry b)) :: newEntries stateStack := stateStack.map fun s => { s with state := descr.addEntry s.state b } } | Entry.«scoped» ns b => { scopedEntries := scopedEntries.insert ns b newEntries := (Entry.«scoped» ns (descr.toOLeanEntry b)) :: newEntries stateStack := stateStack.map fun s => if s.activeScopes.contains ns then { s with state := descr.addEntry s.state b } else s } def exportEntriesFn (s : StateStack α β σ) : Array (Entry α) := s.newEntries.toArray.reverse end ScopedEnvExtension open ScopedEnvExtension structure ScopedEnvExtension (α : Type) (β : Type) (σ : Type) where descr : Descr α β σ ext : PersistentEnvExtension (Entry α) (Entry β) (StateStack α β σ) deriving Inhabited builtin_initialize scopedEnvExtensionsRef : IO.Ref (Array (ScopedEnvExtension EnvExtensionEntry EnvExtensionEntry EnvExtensionState)) ← IO.mkRef #[] unsafe def registerScopedEnvExtensionUnsafe (descr : Descr α β σ) : IO (ScopedEnvExtension α β σ) := do let ext ← registerPersistentEnvExtension { name := descr.name mkInitial := mkInitial descr addImportedFn := addImportedFn descr addEntryFn := addEntryFn descr exportEntriesFn := exportEntriesFn statsFn := fun s => format "number of local entries: " ++ format s.newEntries.length } let ext := { descr := descr, ext := ext : ScopedEnvExtension α β σ } scopedEnvExtensionsRef.modify fun exts => exts.push (unsafeCast ext) return ext @[implementedBy registerScopedEnvExtensionUnsafe] constant registerScopedEnvExtension (descr : Descr α β σ) : IO (ScopedEnvExtension α β σ) def ScopedEnvExtension.pushScope (ext : ScopedEnvExtension α β σ) (env : Environment) : Environment := let s := ext.ext.getState env match s.stateStack with | [] => env | state :: stack => ext.ext.setState env { s with stateStack := state :: state :: stack } def ScopedEnvExtension.popScope (ext : ScopedEnvExtension α β σ) (env : Environment) : Environment := let s := ext.ext.getState env match s.stateStack with | state₁ :: state₂ :: stack => ext.ext.setState env { s with stateStack := state₂ :: stack } | _ => env def ScopedEnvExtension.addEntry (ext : ScopedEnvExtension α β σ) (env : Environment) (b : β) : Environment := ext.ext.addEntry env (Entry.global b) def ScopedEnvExtension.addScopedEntry (ext : ScopedEnvExtension α β σ) (env : Environment) (namespaceName : Name) (b : β) : Environment := ext.ext.addEntry env (Entry.«scoped» namespaceName b) def ScopedEnvExtension.addLocalEntry (ext : ScopedEnvExtension α β σ) (env : Environment) (b : β) : Environment := let s := ext.ext.getState env match s.stateStack with | [] => env | top :: states => let top := { top with state := ext.descr.addEntry top.state b } ext.ext.setState env { s with stateStack := top :: states } def ScopedEnvExtension.add [Monad m] [MonadResolveName m] [MonadEnv m] (ext : ScopedEnvExtension α β σ) (b : β) (kind := AttributeKind.global) : m Unit := do match kind with | AttributeKind.global => modifyEnv (ext.addEntry · b) | AttributeKind.local => modifyEnv (ext.addLocalEntry · b) | AttributeKind.scoped => modifyEnv (ext.addScopedEntry · (← getCurrNamespace) b) def ScopedEnvExtension.getState [Inhabited σ] (ext : ScopedEnvExtension α β σ) (env : Environment) : σ := match ext.ext.getState env |>.stateStack with | top :: _ => top.state | _ => unreachable! def ScopedEnvExtension.activateScoped (ext : ScopedEnvExtension α β σ) (env : Environment) (namespaceName : Name) : Environment := let s := ext.ext.getState env match s.stateStack with | top :: stack => if top.activeScopes.contains namespaceName then env else let activeScopes := top.activeScopes.insert namespaceName let top := match s.scopedEntries.map.find? namespaceName with | none => { top with activeScopes := activeScopes } | some bs => do let mut state := top.state for b in bs do state := ext.descr.addEntry state b { state := state, activeScopes := activeScopes } ext.ext.setState env { s with stateStack := top :: stack } | _ => env def pushScope [Monad m] [MonadEnv m] [MonadLiftT (ST IO.RealWorld) m] : m Unit := do for ext in (← scopedEnvExtensionsRef.get) do modifyEnv ext.pushScope def popScope [Monad m] [MonadEnv m] [MonadLiftT (ST IO.RealWorld) m] : m Unit := do for ext in (← scopedEnvExtensionsRef.get) do modifyEnv ext.popScope def activateScoped [Monad m] [MonadEnv m] [MonadLiftT (ST IO.RealWorld) m] (namespaceName : Name) : m Unit := do for ext in (← scopedEnvExtensionsRef.get) do modifyEnv (ext.activateScoped · namespaceName) abbrev SimpleScopedEnvExtension (α : Type) (σ : Type) := ScopedEnvExtension α α σ structure SimpleScopedEnvExtension.Descr (α : Type) (σ : Type) where name : Name addEntry : σ → α → σ initial : σ def registerSimpleScopedEnvExtension (descr : SimpleScopedEnvExtension.Descr α σ) : IO (SimpleScopedEnvExtension α σ) := do registerScopedEnvExtension { name := descr.name mkInitial := return descr.initial addEntry := descr.addEntry toOLeanEntry := id ofOLeanEntry := fun s a => return a } end Lean
c49c03db170810daf8a059a790a08ce63694a6a6
4d7079ae603c07560e99d1ce35876f769cbb3e24
/src/condorcet.lean
33adbd921394ce816941d688e39ad81aad1dbc66
[]
no_license
chasenorman/Formalized-Voting
72493a08aa09d98d0fb589731b842e2d08c991d0
de04e630b83525b042db166670ba97f9952b5691
refs/heads/main
1,687,282,160,284
1,627,155,031,000
1,627,155,031,000
370,472,025
13
0
null
null
null
null
UTF-8
Lean
false
false
5,397
lean
import main import split_cycle open_locale classical variables {V X : Type} def condorcet_winner [fintype V] (P : Prof V X) (x : X) : Prop := ∀ y, x ≠ y → margin_pos P x y --def condorcet_winner [fintype V] (P : Prof V X) (x : X) : Prop := ∀ y ≠ x, margin_pos P x y def condorcet_loser [fintype V] (P : Prof V X) (x : X) : Prop := (∀ y, x ≠ y → margin_pos P y x) ∧ ∃ y, x ≠ y def condorcet_criterion [fintype V] (F : VSCC) (P : Prof V X) : Prop := ∀ x, condorcet_winner P x → F V X P = {x} def condorcet_loser_criterion [fintype V] (F : VSCC) (P : Prof V X) : Prop := ∀ x, condorcet_loser P x → x ∉ F V X P lemma ineq {n : ℕ} (e : n > 1) : (n - 2).succ < n := by omega lemma ineq2 {n : ℕ} (e : n > 1) : (n - 2).succ = (n - 1) := by omega lemma gt_zero_of_ne_zero (i : ℕ) : i ≠ 0 → i > 0 := by omega def condorcet_SCC [fintype V] : SCC V X := λ P, if c : (∃x, condorcet_winner P x) then {c.some} else set.univ def condorcet_VSCC : VSCC := λ V X P, if f : (∃ [fintype V], true) then @condorcet_SCC V X f.some P else ∅ def split_cycle_condorcet_criterion [fintype V] (P : Prof V X) : condorcet_criterion split_cycle P := begin intro w, intro x_winner, unfold split_cycle, unfold split_cycle_VCCR, unfold split_cycle_CCR, unfold max_el_VSCC, simp, have no_cycles : ¬ ∃ l (e : cycle (margin_pos P) l), w ∈ l, by_contradiction a, cases a with l a, cases a with e a, have defeated := dominate_of_cycle l (margin_pos P) e w a, cases defeated with bad defeated, cases defeated with _ defeated, specialize x_winner bad, have ne : w ≠ bad, simp, by_contradiction a_1, rw a_1 at defeated, unfold margin_pos at defeated, rw self_margin_zero bad P at defeated, linarith, specialize x_winner ne, unfold margin_pos at *, have as := margin_antisymmetric P, unfold antisymmetric at as, specialize as bad w, rw as at defeated, linarith, ext1, split, intro a, specialize a w, apply of_not_not, by_contradiction a_1, specialize x_winner x, have x_winner := x_winner (ne.symm a_1), cases a with f a, unfold margin_pos at a, rw margin_eq_margin f _inst_1 P w x at a, have a := a x_winner, cases a with c a, cases a with w_mem a, cases a with x_mem cy, have contr : ∃ (l : list X) (e : cycle (margin_pos P) l), w ∈ l, use c, refine and.intro _ w_mem, have imp : ∀ e1 e2, (λ (a b : X), margin P w x ≤ margin P a b) e1 e2 → (margin_pos P) e1 e2, {intros e1 e2, simp, intro z, have o : ∀ (a b : ℤ), a > 0 → a ≤ b → b > 0 := by omega, exact o (margin P w x) (margin P e1 e2) x_winner z,}, simp_rw margin_eq_margin f _inst_1 at cy, exact cycle_of_cycle_imp imp cy, exact no_cycles contr, intro eq, intro x_1, by_contradiction a, push_neg at a, specialize a _inst_1, cases a with a b, simp at eq, rw eq at a, specialize x_winner x_1, by_cases eq : x_1 = w, rw eq at a, unfold margin_pos at a, rw self_margin_zero w P at a, linarith, specialize x_winner (ne.symm eq), unfold margin_pos at *, have as := margin_antisymmetric P, unfold antisymmetric at as, specialize as x_1 w, rw as at a, linarith, end lemma margin_pos_irrefl [fintype V] (P : Prof V X) : ∀ x, ¬ margin_pos P x x := begin intro x, -- for any candidate x unfold margin_pos, unfold margin, linarith, end -- we should probably get the axioms out of the way for this. Is it a strict total order? lemma margin_pos_asymm [fintype V] (P : Prof V X) : ∀ x y, margin_pos P x y → ¬ margin_pos P y x := begin intro x, intro y, unfold margin_pos, unfold margin, intro dominates, push_neg, linarith, end lemma condorcet_loser_ineq (a : ℤ) : (0 < a) → (-a ≤ 0) := by omega def split_cycle_condorcet_loser_criterion [fintype V] (P : Prof V X) : condorcet_loser_criterion split_cycle P := begin intro x, intro x_loser, unfold split_cycle, unfold max_el_VSCC, simp, unfold condorcet_loser at x_loser, cases x_loser with a b, cases b with y b, -- consider the element y ≠ x guaranteed by condorcet_loser. use y, -- we will prove this element defeats x. unfold split_cycle_VCCR, introI _inst_2, split, unfold margin_pos, rw margin_eq_margin _inst_2 _inst_1, exact a y b, by_contradiction, -- we show there is no clemency cycle by contradiction cases h with l h, cases h with y_mem h, cases h with x_mem c, have dominates := dominates_of_cycle l (λ (a b : X), margin P y x ≤ margin P a b) c x x_mem, cases dominates with z dominates, -- let z be the element dominated by x cases dominates with z_mem dominates, have contr : margin P x z ≤ 0, specialize a z, by_cases x = z, rw h, rw self_margin_zero, specialize a h, unfold margin_pos at a, rw margin_antisymmetric, rw margin_eq_margin _inst_2 _inst_1, exact condorcet_loser_ineq (@margin V X _inst_1 P z x) a, have f := int.le_trans dominates contr, specialize a y, specialize a b, unfold margin_pos at a, rw margin_eq_margin _inst_2 _inst_1 at f, exact (not_lt.mpr f) a, end
d3b460dbbd22f08ce4115414b032578491f68303
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/ematch1.lean
8a5a823ea9c563de9b367213ebcfed3249a94a0d
[ "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
766
lean
constant f : nat → nat constant g : nat → nat axiom Ax : ∀ x, (: f (g x) :) = x open tactic meta def add_insts : list (expr × expr) → tactic unit | [] := skip | ((inst, pr)::r) := do assertv `_einst inst pr, add_insts r meta def ematch_test (h : name) (e : expr) : tactic unit := do cc ← cc_state.mk_using_hs, ems ← return $ ematch_state.mk {}, hlemma ← hinst_lemma.mk_from_decl h, (r, cc, ems) ← ematch cc ems hlemma e, add_insts r example (a b c : nat) : f a = b → a = g c → f a ≠ c → false := by do intros, e ← to_expr ```(f a), ematch_test `Ax e, trace_state, cc example (a b c : nat) : f a = b → a = g c → f a = c := by do intros, e ← to_expr ```(f a), ematch_test `Ax e, cc
931cc8799442df7f60b040d74b27cf51d25efeeb
302c785c90d40ad3d6be43d33bc6a558354cc2cf
/src/measure_theory/integration.lean
f17509438ee479b2c878b0596274992bf514e6e2
[ "Apache-2.0" ]
permissive
ilitzroth/mathlib
ea647e67f1fdfd19a0f7bdc5504e8acec6180011
5254ef14e3465f6504306132fe3ba9cec9ffff16
refs/heads/master
1,680,086,661,182
1,617,715,647,000
1,617,715,647,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
82,967
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Johannes Hölzl -/ import measure_theory.measure_space import measure_theory.borel_space import data.indicator_function import data.support /-! # Lebesgue integral for `ℝ≥0∞`-valued functions We define simple functions and show that each Borel measurable function on `ℝ≥0∞` can be approximated by a sequence of simple functions. To prove something for an arbitrary measurable function into `ℝ≥0∞`, the theorem `measurable.ennreal_induction` shows that is it sufficient to show that the property holds for (multiples of) characteristic functions and is closed under addition and supremum of increasing sequences of functions. ## Notation We introduce the following notation for the lower Lebesgue integral of a function `f : α → ℝ≥0∞`. * `∫⁻ x, f x ∂μ`: integral of a function `f : α → ℝ≥0∞` with respect to a measure `μ`; * `∫⁻ x, f x`: integral of a function `f : α → ℝ≥0∞` with respect to the canonical measure `volume` on `α`; * `∫⁻ x in s, f x ∂μ`: integral of a function `f : α → ℝ≥0∞` over a set `s` with respect to a measure `μ`, defined as `∫⁻ x, f x ∂(μ.restrict s)`; * `∫⁻ x in s, f x`: integral of a function `f : α → ℝ≥0∞` over a set `s` with respect to the canonical measure `volume`, defined as `∫⁻ x, f x ∂(volume.restrict s)`. -/ noncomputable theory open set (hiding restrict restrict_apply) filter ennreal function (support) open_locale classical topological_space big_operators nnreal ennreal namespace measure_theory variables {α β γ δ : Type*} /-- A function `f` from a measurable space to any type is called *simple*, if every preimage `f ⁻¹' {x}` is measurable, and the range is finite. This structure bundles a function with these properties. -/ structure {u v} simple_func (α : Type u) [measurable_space α] (β : Type v) := (to_fun : α → β) (measurable_set_fiber' : ∀ x, measurable_set (to_fun ⁻¹' {x})) (finite_range' : (set.range to_fun).finite) local infixr ` →ₛ `:25 := simple_func namespace simple_func section measurable variables [measurable_space α] instance has_coe_to_fun : has_coe_to_fun (α →ₛ β) := ⟨_, to_fun⟩ lemma coe_injective ⦃f g : α →ₛ β⦄ (H : ⇑f = g) : f = g := by cases f; cases g; congr; exact H @[ext] theorem ext {f g : α →ₛ β} (H : ∀ a, f a = g a) : f = g := coe_injective $ funext H lemma finite_range (f : α →ₛ β) : (set.range f).finite := f.finite_range' lemma measurable_set_fiber (f : α →ₛ β) (x : β) : measurable_set (f ⁻¹' {x}) := f.measurable_set_fiber' x /-- Range of a simple function `α →ₛ β` as a `finset β`. -/ protected def range (f : α →ₛ β) : finset β := f.finite_range.to_finset @[simp] theorem mem_range {f : α →ₛ β} {b} : b ∈ f.range ↔ b ∈ range f := finite.mem_to_finset _ theorem mem_range_self (f : α →ₛ β) (x : α) : f x ∈ f.range := mem_range.2 ⟨x, rfl⟩ @[simp] lemma coe_range (f : α →ₛ β) : (↑f.range : set β) = set.range f := f.finite_range.coe_to_finset theorem mem_range_of_measure_ne_zero {f : α →ₛ β} {x : β} {μ : measure α} (H : μ (f ⁻¹' {x}) ≠ 0) : x ∈ f.range := let ⟨a, ha⟩ := nonempty_of_measure_ne_zero H in mem_range.2 ⟨a, ha⟩ lemma forall_range_iff {f : α →ₛ β} {p : β → Prop} : (∀ y ∈ f.range, p y) ↔ ∀ x, p (f x) := by simp only [mem_range, set.forall_range_iff] lemma exists_range_iff {f : α →ₛ β} {p : β → Prop} : (∃ y ∈ f.range, p y) ↔ ∃ x, p (f x) := by simpa only [mem_range, exists_prop] using set.exists_range_iff lemma preimage_eq_empty_iff (f : α →ₛ β) (b : β) : f ⁻¹' {b} = ∅ ↔ b ∉ f.range := preimage_singleton_eq_empty.trans $ not_congr mem_range.symm lemma exists_forall_le [nonempty β] [directed_order β] (f : α →ₛ β) : ∃ C, ∀ x, f x ≤ C := f.range.exists_le.imp $ λ C, forall_range_iff.1 /-- Constant function as a `simple_func`. -/ def const (α) {β} [measurable_space α] (b : β) : α →ₛ β := ⟨λ a, b, λ x, measurable_set.const _, finite_range_const⟩ instance [inhabited β] : inhabited (α →ₛ β) := ⟨const _ (default _)⟩ theorem const_apply (a : α) (b : β) : (const α b) a = b := rfl @[simp] theorem coe_const (b : β) : ⇑(const α b) = function.const α b := rfl @[simp] lemma range_const (α) [measurable_space α] [nonempty α] (b : β) : (const α b).range = {b} := finset.coe_injective $ by simp lemma measurable_set_cut (r : α → β → Prop) (f : α →ₛ β) (h : ∀b, measurable_set {a | r a b}) : measurable_set {a | r a (f a)} := begin have : {a | r a (f a)} = ⋃ b ∈ range f, {a | r a b} ∩ f ⁻¹' {b}, { ext a, suffices : r a (f a) ↔ ∃ i, r a (f i) ∧ f a = f i, by simpa, exact ⟨λ h, ⟨a, ⟨h, rfl⟩⟩, λ ⟨a', ⟨h', e⟩⟩, e.symm ▸ h'⟩ }, rw this, exact measurable_set.bUnion f.finite_range.countable (λ b _, measurable_set.inter (h b) (f.measurable_set_fiber _)) end theorem measurable_set_preimage (f : α →ₛ β) (s) : measurable_set (f ⁻¹' s) := measurable_set_cut (λ _ b, b ∈ s) f (λ b, measurable_set.const (b ∈ s)) /-- A simple function is measurable -/ protected theorem measurable [measurable_space β] (f : α →ₛ β) : measurable f := λ s _, measurable_set_preimage f s protected theorem ae_measurable [measurable_space β] {μ : measure α} (f : α →ₛ β) : ae_measurable f μ := f.measurable.ae_measurable protected lemma sum_measure_preimage_singleton (f : α →ₛ β) {μ : measure α} (s : finset β) : ∑ y in s, μ (f ⁻¹' {y}) = μ (f ⁻¹' ↑s) := sum_measure_preimage_singleton _ (λ _ _, f.measurable_set_fiber _) lemma sum_range_measure_preimage_singleton (f : α →ₛ β) (μ : measure α) : ∑ y in f.range, μ (f ⁻¹' {y}) = μ univ := by rw [f.sum_measure_preimage_singleton, coe_range, preimage_range] /-- If-then-else as a `simple_func`. -/ def piecewise (s : set α) (hs : measurable_set s) (f g : α →ₛ β) : α →ₛ β := ⟨s.piecewise f g, λ x, by letI : measurable_space β := ⊤; exact f.measurable.piecewise hs g.measurable trivial, (f.finite_range.union g.finite_range).subset range_ite_subset⟩ @[simp] theorem coe_piecewise {s : set α} (hs : measurable_set s) (f g : α →ₛ β) : ⇑(piecewise s hs f g) = s.piecewise f g := rfl theorem piecewise_apply {s : set α} (hs : measurable_set s) (f g : α →ₛ β) (a) : piecewise s hs f g a = if a ∈ s then f a else g a := rfl @[simp] lemma piecewise_compl {s : set α} (hs : measurable_set sᶜ) (f g : α →ₛ β) : piecewise sᶜ hs f g = piecewise s hs.of_compl g f := coe_injective $ by simp [hs] @[simp] lemma piecewise_univ (f g : α →ₛ β) : piecewise univ measurable_set.univ f g = f := coe_injective $ by simp @[simp] lemma piecewise_empty (f g : α →ₛ β) : piecewise ∅ measurable_set.empty f g = g := coe_injective $ by simp lemma measurable_bind [measurable_space γ] (f : α →ₛ β) (g : β → α → γ) (hg : ∀ b, measurable (g b)) : measurable (λ a, g (f a) a) := λ s hs, f.measurable_set_cut (λ a b, g b a ∈ s) $ λ b, hg b hs /-- If `f : α →ₛ β` is a simple function and `g : β → α →ₛ γ` is a family of simple functions, then `f.bind g` binds the first argument of `g` to `f`. In other words, `f.bind g a = g (f a) a`. -/ def bind (f : α →ₛ β) (g : β → α →ₛ γ) : α →ₛ γ := ⟨λa, g (f a) a, λ c, f.measurable_set_cut (λ a b, g b a = c) $ λ b, (g b).measurable_set_preimage {c}, (f.finite_range.bUnion (λ b _, (g b).finite_range)).subset $ by rintro _ ⟨a, rfl⟩; simp; exact ⟨a, a, rfl⟩⟩ @[simp] theorem bind_apply (f : α →ₛ β) (g : β → α →ₛ γ) (a) : f.bind g a = g (f a) a := rfl /-- Given a function `g : β → γ` and a simple function `f : α →ₛ β`, `f.map g` return the simple function `g ∘ f : α →ₛ γ` -/ def map (g : β → γ) (f : α →ₛ β) : α →ₛ γ := bind f (const α ∘ g) theorem map_apply (g : β → γ) (f : α →ₛ β) (a) : f.map g a = g (f a) := rfl theorem map_map (g : β → γ) (h: γ → δ) (f : α →ₛ β) : (f.map g).map h = f.map (h ∘ g) := rfl @[simp] theorem coe_map (g : β → γ) (f : α →ₛ β) : (f.map g : α → γ) = g ∘ f := rfl @[simp] theorem range_map [decidable_eq γ] (g : β → γ) (f : α →ₛ β) : (f.map g).range = f.range.image g := finset.coe_injective $ by simp [range_comp] @[simp] theorem map_const (g : β → γ) (b : β) : (const α b).map g = const α (g b) := rfl lemma map_preimage (f : α →ₛ β) (g : β → γ) (s : set γ) : (f.map g) ⁻¹' s = f ⁻¹' ↑(f.range.filter (λb, g b ∈ s)) := by { simp only [coe_range, sep_mem_eq, set.mem_range, function.comp_app, coe_map, finset.coe_filter, ← mem_preimage, inter_comm, preimage_inter_range], apply preimage_comp } lemma map_preimage_singleton (f : α →ₛ β) (g : β → γ) (c : γ) : (f.map g) ⁻¹' {c} = f ⁻¹' ↑(f.range.filter (λ b, g b = c)) := map_preimage _ _ _ /-- Composition of a `simple_fun` and a measurable function is a `simple_func`. -/ def comp [measurable_space β] (f : β →ₛ γ) (g : α → β) (hgm : measurable g) : α →ₛ γ := { to_fun := f ∘ g, finite_range' := f.finite_range.subset $ set.range_comp_subset_range _ _, measurable_set_fiber' := λ z, hgm (f.measurable_set_fiber z) } @[simp] lemma coe_comp [measurable_space β] (f : β →ₛ γ) {g : α → β} (hgm : measurable g) : ⇑(f.comp g hgm) = f ∘ g := rfl lemma range_comp_subset_range [measurable_space β] (f : β →ₛ γ) {g : α → β} (hgm : measurable g) : (f.comp g hgm).range ⊆ f.range := finset.coe_subset.1 $ by simp only [coe_range, coe_comp, set.range_comp_subset_range] /-- If `f` is a simple function taking values in `β → γ` and `g` is another simple function with the same domain and codomain `β`, then `f.seq g = f a (g a)`. -/ def seq (f : α →ₛ (β → γ)) (g : α →ₛ β) : α →ₛ γ := f.bind (λf, g.map f) @[simp] lemma seq_apply (f : α →ₛ (β → γ)) (g : α →ₛ β) (a : α) : f.seq g a = f a (g a) := rfl /-- Combine two simple functions `f : α →ₛ β` and `g : α →ₛ β` into `λ a, (f a, g a)`. -/ def pair (f : α →ₛ β) (g : α →ₛ γ) : α →ₛ (β × γ) := (f.map prod.mk).seq g @[simp] lemma pair_apply (f : α →ₛ β) (g : α →ₛ γ) (a) : pair f g a = (f a, g a) := rfl lemma pair_preimage (f : α →ₛ β) (g : α →ₛ γ) (s : set β) (t : set γ) : (pair f g) ⁻¹' (set.prod s t) = (f ⁻¹' s) ∩ (g ⁻¹' t) := rfl /- A special form of `pair_preimage` -/ lemma pair_preimage_singleton (f : α →ₛ β) (g : α →ₛ γ) (b : β) (c : γ) : (pair f g) ⁻¹' {(b, c)} = (f ⁻¹' {b}) ∩ (g ⁻¹' {c}) := by { rw ← singleton_prod_singleton, exact pair_preimage _ _ _ _ } theorem bind_const (f : α →ₛ β) : f.bind (const α) = f := by ext; simp instance [has_zero β] : has_zero (α →ₛ β) := ⟨const α 0⟩ instance [has_add β] : has_add (α →ₛ β) := ⟨λf g, (f.map (+)).seq g⟩ instance [has_mul β] : has_mul (α →ₛ β) := ⟨λf g, (f.map (*)).seq g⟩ instance [has_sup β] : has_sup (α →ₛ β) := ⟨λf g, (f.map (⊔)).seq g⟩ instance [has_inf β] : has_inf (α →ₛ β) := ⟨λf g, (f.map (⊓)).seq g⟩ instance [has_le β] : has_le (α →ₛ β) := ⟨λf g, ∀a, f a ≤ g a⟩ @[simp, norm_cast] lemma coe_zero [has_zero β] : ⇑(0 : α →ₛ β) = 0 := rfl @[simp] lemma const_zero [has_zero β] : const α (0:β) = 0 := rfl @[simp, norm_cast] lemma coe_add [has_add β] (f g : α →ₛ β) : ⇑(f + g) = f + g := rfl @[simp, norm_cast] lemma coe_mul [has_mul β] (f g : α →ₛ β) : ⇑(f * g) = f * g := rfl @[simp, norm_cast] lemma coe_le [preorder β] {f g : α →ₛ β} : (f : α → β) ≤ g ↔ f ≤ g := iff.rfl @[simp] lemma range_zero [nonempty α] [has_zero β] : (0 : α →ₛ β).range = {0} := finset.ext $ λ x, by simp [eq_comm] lemma eq_zero_of_mem_range_zero [has_zero β] : ∀ {y : β}, y ∈ (0 : α →ₛ β).range → y = 0 := forall_range_iff.2 $ λ x, rfl lemma sup_apply [has_sup β] (f g : α →ₛ β) (a : α) : (f ⊔ g) a = f a ⊔ g a := rfl lemma mul_apply [has_mul β] (f g : α →ₛ β) (a : α) : (f * g) a = f a * g a := rfl lemma add_apply [has_add β] (f g : α →ₛ β) (a : α) : (f + g) a = f a + g a := rfl lemma add_eq_map₂ [has_add β] (f g : α →ₛ β) : f + g = (pair f g).map (λp:β×β, p.1 + p.2) := rfl lemma mul_eq_map₂ [has_mul β] (f g : α →ₛ β) : f * g = (pair f g).map (λp:β×β, p.1 * p.2) := rfl lemma sup_eq_map₂ [has_sup β] (f g : α →ₛ β) : f ⊔ g = (pair f g).map (λp:β×β, p.1 ⊔ p.2) := rfl lemma const_mul_eq_map [has_mul β] (f : α →ₛ β) (b : β) : const α b * f = f.map (λa, b * a) := rfl theorem map_add [has_add β] [has_add γ] {g : β → γ} (hg : ∀ x y, g (x + y) = g x + g y) (f₁ f₂ : α →ₛ β) : (f₁ + f₂).map g = f₁.map g + f₂.map g := ext $ λ x, hg _ _ instance [add_monoid β] : add_monoid (α →ₛ β) := function.injective.add_monoid (λ f, show α → β, from f) coe_injective coe_zero coe_add instance add_comm_monoid [add_comm_monoid β] : add_comm_monoid (α →ₛ β) := function.injective.add_comm_monoid (λ f, show α → β, from f) coe_injective coe_zero coe_add instance [has_neg β] : has_neg (α →ₛ β) := ⟨λf, f.map (has_neg.neg)⟩ @[simp, norm_cast] lemma coe_neg [has_neg β] (f : α →ₛ β) : ⇑(-f) = -f := rfl instance [has_sub β] : has_sub (α →ₛ β) := ⟨λf g, (f.map (has_sub.sub)).seq g⟩ @[simp, norm_cast] lemma coe_sub [has_sub β] (f g : α →ₛ β) : ⇑(f - g) = f - g := rfl lemma sub_apply [has_sub β] (f g : α →ₛ β) (x : α) : (f - g) x = f x - g x := rfl instance [add_group β] : add_group (α →ₛ β) := function.injective.add_group (λ f, show α → β, from f) coe_injective coe_zero coe_add coe_neg coe_sub instance [add_comm_group β] : add_comm_group (α →ₛ β) := function.injective.add_comm_group (λ f, show α → β, from f) coe_injective coe_zero coe_add coe_neg coe_sub variables {K : Type*} instance [has_scalar K β] : has_scalar K (α →ₛ β) := ⟨λk f, f.map ((•) k)⟩ @[simp] lemma coe_smul [has_scalar K β] (c : K) (f : α →ₛ β) : ⇑(c • f) = c • f := rfl lemma smul_apply [has_scalar K β] (k : K) (f : α →ₛ β) (a : α) : (k • f) a = k • f a := rfl instance [semiring K] [add_comm_monoid β] [semimodule K β] : semimodule K (α →ₛ β) := function.injective.semimodule K ⟨λ f, show α → β, from f, coe_zero, coe_add⟩ coe_injective coe_smul lemma smul_eq_map [has_scalar K β] (k : K) (f : α →ₛ β) : k • f = f.map ((•) k) := rfl instance [preorder β] : preorder (α →ₛ β) := { le_refl := λf a, le_refl _, le_trans := λf g h hfg hgh a, le_trans (hfg _) (hgh a), .. simple_func.has_le } instance [partial_order β] : partial_order (α →ₛ β) := { le_antisymm := assume f g hfg hgf, ext $ assume a, le_antisymm (hfg a) (hgf a), .. simple_func.preorder } instance [order_bot β] : order_bot (α →ₛ β) := { bot := const α ⊥, bot_le := λf a, bot_le, .. simple_func.partial_order } instance [order_top β] : order_top (α →ₛ β) := { top := const α ⊤, le_top := λf a, le_top, .. simple_func.partial_order } instance [semilattice_inf β] : semilattice_inf (α →ₛ β) := { inf := (⊓), inf_le_left := assume f g a, inf_le_left, inf_le_right := assume f g a, inf_le_right, le_inf := assume f g h hfh hgh a, le_inf (hfh a) (hgh a), .. simple_func.partial_order } instance [semilattice_sup β] : semilattice_sup (α →ₛ β) := { sup := (⊔), le_sup_left := assume f g a, le_sup_left, le_sup_right := assume f g a, le_sup_right, sup_le := assume f g h hfh hgh a, sup_le (hfh a) (hgh a), .. simple_func.partial_order } instance [semilattice_sup_bot β] : semilattice_sup_bot (α →ₛ β) := { .. simple_func.semilattice_sup,.. simple_func.order_bot } instance [lattice β] : lattice (α →ₛ β) := { .. simple_func.semilattice_sup,.. simple_func.semilattice_inf } instance [bounded_lattice β] : bounded_lattice (α →ₛ β) := { .. simple_func.lattice, .. simple_func.order_bot, .. simple_func.order_top } lemma finset_sup_apply [semilattice_sup_bot β] {f : γ → α →ₛ β} (s : finset γ) (a : α) : s.sup f a = s.sup (λc, f c a) := begin refine finset.induction_on s rfl _, assume a s hs ih, rw [finset.sup_insert, finset.sup_insert, sup_apply, ih] end section restrict variables [has_zero β] /-- Restrict a simple function `f : α →ₛ β` to a set `s`. If `s` is measurable, then `f.restrict s a = if a ∈ s then f a else 0`, otherwise `f.restrict s = const α 0`. -/ def restrict (f : α →ₛ β) (s : set α) : α →ₛ β := if hs : measurable_set s then piecewise s hs f 0 else 0 theorem restrict_of_not_measurable {f : α →ₛ β} {s : set α} (hs : ¬measurable_set s) : restrict f s = 0 := dif_neg hs @[simp] theorem coe_restrict (f : α →ₛ β) {s : set α} (hs : measurable_set s) : ⇑(restrict f s) = indicator s f := by { rw [restrict, dif_pos hs], refl } @[simp] theorem restrict_univ (f : α →ₛ β) : restrict f univ = f := by simp [restrict] @[simp] theorem restrict_empty (f : α →ₛ β) : restrict f ∅ = 0 := by simp [restrict] theorem map_restrict_of_zero [has_zero γ] {g : β → γ} (hg : g 0 = 0) (f : α →ₛ β) (s : set α) : (f.restrict s).map g = (f.map g).restrict s := ext $ λ x, if hs : measurable_set s then by simp [hs, set.indicator_comp_of_zero hg] else by simp [restrict_of_not_measurable hs, hg] theorem map_coe_ennreal_restrict (f : α →ₛ ℝ≥0) (s : set α) : (f.restrict s).map (coe : ℝ≥0 → ℝ≥0∞) = (f.map coe).restrict s := map_restrict_of_zero ennreal.coe_zero _ _ theorem map_coe_nnreal_restrict (f : α →ₛ ℝ≥0) (s : set α) : (f.restrict s).map (coe : ℝ≥0 → ℝ) = (f.map coe).restrict s := map_restrict_of_zero nnreal.coe_zero _ _ theorem restrict_apply (f : α →ₛ β) {s : set α} (hs : measurable_set s) (a) : restrict f s a = indicator s f a := by simp only [f.coe_restrict hs] theorem restrict_preimage (f : α →ₛ β) {s : set α} (hs : measurable_set s) {t : set β} (ht : (0:β) ∉ t) : restrict f s ⁻¹' t = s ∩ f ⁻¹' t := by simp [hs, indicator_preimage_of_not_mem _ _ ht, inter_comm] theorem restrict_preimage_singleton (f : α →ₛ β) {s : set α} (hs : measurable_set s) {r : β} (hr : r ≠ 0) : restrict f s ⁻¹' {r} = s ∩ f ⁻¹' {r} := f.restrict_preimage hs hr.symm lemma mem_restrict_range {r : β} {s : set α} {f : α →ₛ β} (hs : measurable_set s) : r ∈ (restrict f s).range ↔ (r = 0 ∧ s ≠ univ) ∨ (r ∈ f '' s) := by rw [← finset.mem_coe, coe_range, coe_restrict _ hs, mem_range_indicator] lemma mem_image_of_mem_range_restrict {r : β} {s : set α} {f : α →ₛ β} (hr : r ∈ (restrict f s).range) (h0 : r ≠ 0) : r ∈ f '' s := if hs : measurable_set s then by simpa [mem_restrict_range hs, h0] using hr else by { rw [restrict_of_not_measurable hs] at hr, exact (h0 $ eq_zero_of_mem_range_zero hr).elim } @[mono] lemma restrict_mono [preorder β] (s : set α) {f g : α →ₛ β} (H : f ≤ g) : f.restrict s ≤ g.restrict s := if hs : measurable_set s then λ x, by simp only [coe_restrict _ hs, indicator_le_indicator (H x)] else by simp only [restrict_of_not_measurable hs, le_refl] end restrict section approx section variables [semilattice_sup_bot β] [has_zero β] /-- Fix a sequence `i : ℕ → β`. Given a function `α → β`, its `n`-th approximation by simple functions is defined so that in case `β = ℝ≥0∞` it sends each `a` to the supremum of the set `{i k | k ≤ n ∧ i k ≤ f a}`, see `approx_apply` and `supr_approx_apply` for details. -/ def approx (i : ℕ → β) (f : α → β) (n : ℕ) : α →ₛ β := (finset.range n).sup (λk, restrict (const α (i k)) {a:α | i k ≤ f a}) lemma approx_apply [topological_space β] [order_closed_topology β] [measurable_space β] [opens_measurable_space β] {i : ℕ → β} {f : α → β} {n : ℕ} (a : α) (hf : measurable f) : (approx i f n : α →ₛ β) a = (finset.range n).sup (λk, if i k ≤ f a then i k else 0) := begin dsimp only [approx], rw [finset_sup_apply], congr, funext k, rw [restrict_apply], refl, exact (hf measurable_set_Ici) end lemma monotone_approx (i : ℕ → β) (f : α → β) : monotone (approx i f) := assume n m h, finset.sup_mono $ finset.range_subset.2 h lemma approx_comp [topological_space β] [order_closed_topology β] [measurable_space β] [opens_measurable_space β] [measurable_space γ] {i : ℕ → β} {f : γ → β} {g : α → γ} {n : ℕ} (a : α) (hf : measurable f) (hg : measurable g) : (approx i (f ∘ g) n : α →ₛ β) a = (approx i f n : γ →ₛ β) (g a) := by rw [approx_apply _ hf, approx_apply _ (hf.comp hg)] end lemma supr_approx_apply [topological_space β] [complete_lattice β] [order_closed_topology β] [has_zero β] [measurable_space β] [opens_measurable_space β] (i : ℕ → β) (f : α → β) (a : α) (hf : measurable f) (h_zero : (0 : β) = ⊥) : (⨆n, (approx i f n : α →ₛ β) a) = (⨆k (h : i k ≤ f a), i k) := begin refine le_antisymm (supr_le $ assume n, _) (supr_le $ assume k, supr_le $ assume hk, _), { rw [approx_apply a hf, h_zero], refine finset.sup_le (assume k hk, _), split_ifs, exact le_supr_of_le k (le_supr _ h), exact bot_le }, { refine le_supr_of_le (k+1) _, rw [approx_apply a hf], have : k ∈ finset.range (k+1) := finset.mem_range.2 (nat.lt_succ_self _), refine le_trans (le_of_eq _) (finset.le_sup this), rw [if_pos hk] } end end approx section eapprox /-- A sequence of `ℝ≥0∞`s such that its range is the set of non-negative rational numbers. -/ def ennreal_rat_embed (n : ℕ) : ℝ≥0∞ := ennreal.of_real ((encodable.decode ℚ n).get_or_else (0 : ℚ)) lemma ennreal_rat_embed_encode (q : ℚ) : ennreal_rat_embed (encodable.encode q) = nnreal.of_real q := by rw [ennreal_rat_embed, encodable.encodek]; refl /-- Approximate a function `α → ℝ≥0∞` by a sequence of simple functions. -/ def eapprox : (α → ℝ≥0∞) → ℕ → α →ₛ ℝ≥0∞ := approx ennreal_rat_embed @[mono] lemma monotone_eapprox (f : α → ℝ≥0∞) : monotone (eapprox f) := monotone_approx _ f lemma supr_eapprox_apply (f : α → ℝ≥0∞) (hf : measurable f) (a : α) : (⨆n, (eapprox f n : α →ₛ ℝ≥0∞) a) = f a := begin rw [eapprox, supr_approx_apply ennreal_rat_embed f a hf rfl], refine le_antisymm (supr_le $ assume i, supr_le $ assume hi, hi) (le_of_not_gt _), assume h, rcases ennreal.lt_iff_exists_rat_btwn.1 h with ⟨q, hq, lt_q, q_lt⟩, have : (nnreal.of_real q : ℝ≥0∞) ≤ (⨆ (k : ℕ) (h : ennreal_rat_embed k ≤ f a), ennreal_rat_embed k), { refine le_supr_of_le (encodable.encode q) _, rw [ennreal_rat_embed_encode q], refine le_supr_of_le (le_of_lt q_lt) _, exact le_refl _ }, exact lt_irrefl _ (lt_of_le_of_lt this lt_q) end lemma eapprox_comp [measurable_space γ] {f : γ → ℝ≥0∞} {g : α → γ} {n : ℕ} (hf : measurable f) (hg : measurable g) : (eapprox (f ∘ g) n : α → ℝ≥0∞) = (eapprox f n : γ →ₛ ℝ≥0∞) ∘ g := funext $ assume a, approx_comp a hf hg end eapprox end measurable section measure variables [measurable_space α] {μ : measure α} /-- Integral of a simple function whose codomain is `ℝ≥0∞`. -/ def lintegral (f : α →ₛ ℝ≥0∞) (μ : measure α) : ℝ≥0∞ := ∑ x in f.range, x * μ (f ⁻¹' {x}) lemma lintegral_eq_of_subset (f : α →ₛ ℝ≥0∞) {s : finset ℝ≥0∞} (hs : ∀ x, f x ≠ 0 → μ (f ⁻¹' {f x}) ≠ 0 → f x ∈ s) : f.lintegral μ = ∑ x in s, x * μ (f ⁻¹' {x}) := begin refine finset.sum_bij_ne_zero (λr _ _, r) _ _ _ _, { simpa only [forall_range_iff, mul_ne_zero_iff, and_imp] }, { intros, assumption }, { intros b _ hb, refine ⟨b, _, hb, rfl⟩, rw [mem_range, ← preimage_singleton_nonempty], exact nonempty_of_measure_ne_zero (mul_ne_zero_iff.1 hb).2 }, { intros, refl } end /-- Calculate the integral of `(g ∘ f)`, where `g : β → ℝ≥0∞` and `f : α →ₛ β`. -/ lemma map_lintegral (g : β → ℝ≥0∞) (f : α →ₛ β) : (f.map g).lintegral μ = ∑ x in f.range, g x * μ (f ⁻¹' {x}) := begin simp only [lintegral, range_map], refine finset.sum_image' _ (assume b hb, _), rcases mem_range.1 hb with ⟨a, rfl⟩, rw [map_preimage_singleton, ← f.sum_measure_preimage_singleton, finset.mul_sum], refine finset.sum_congr _ _, { congr }, { assume x, simp only [finset.mem_filter], rintro ⟨_, h⟩, rw h }, end lemma add_lintegral (f g : α →ₛ ℝ≥0∞) : (f + g).lintegral μ = f.lintegral μ + g.lintegral μ := calc (f + g).lintegral μ = ∑ x in (pair f g).range, (x.1 * μ (pair f g ⁻¹' {x}) + x.2 * μ (pair f g ⁻¹' {x})) : by rw [add_eq_map₂, map_lintegral]; exact finset.sum_congr rfl (assume a ha, add_mul _ _ _) ... = ∑ x in (pair f g).range, x.1 * μ (pair f g ⁻¹' {x}) + ∑ x in (pair f g).range, x.2 * μ (pair f g ⁻¹' {x}) : by rw [finset.sum_add_distrib] ... = ((pair f g).map prod.fst).lintegral μ + ((pair f g).map prod.snd).lintegral μ : by rw [map_lintegral, map_lintegral] ... = lintegral f μ + lintegral g μ : rfl lemma const_mul_lintegral (f : α →ₛ ℝ≥0∞) (x : ℝ≥0∞) : (const α x * f).lintegral μ = x * f.lintegral μ := calc (f.map (λa, x * a)).lintegral μ = ∑ r in f.range, x * r * μ (f ⁻¹' {r}) : map_lintegral _ _ ... = ∑ r in f.range, x * (r * μ (f ⁻¹' {r})) : finset.sum_congr rfl (assume a ha, mul_assoc _ _ _) ... = x * f.lintegral μ : finset.mul_sum.symm /-- Integral of a simple function `α →ₛ ℝ≥0∞` as a bilinear map. -/ def lintegralₗ : (α →ₛ ℝ≥0∞) →ₗ[ℝ≥0∞] measure α →ₗ[ℝ≥0∞] ℝ≥0∞ := { to_fun := λ f, { to_fun := lintegral f, map_add' := by simp [lintegral, mul_add, finset.sum_add_distrib], map_smul' := λ c μ, by simp [lintegral, mul_left_comm _ c, finset.mul_sum] }, map_add' := λ f g, linear_map.ext (λ μ, add_lintegral f g), map_smul' := λ c f, linear_map.ext (λ μ, const_mul_lintegral f c) } @[simp] lemma zero_lintegral : (0 : α →ₛ ℝ≥0∞).lintegral μ = 0 := linear_map.ext_iff.1 lintegralₗ.map_zero μ lemma lintegral_add {ν} (f : α →ₛ ℝ≥0∞) : f.lintegral (μ + ν) = f.lintegral μ + f.lintegral ν := (lintegralₗ f).map_add μ ν lemma lintegral_smul (f : α →ₛ ℝ≥0∞) (c : ℝ≥0∞) : f.lintegral (c • μ) = c • f.lintegral μ := (lintegralₗ f).map_smul c μ @[simp] lemma lintegral_zero (f : α →ₛ ℝ≥0∞) : f.lintegral 0 = 0 := (lintegralₗ f).map_zero lemma lintegral_sum {ι} (f : α →ₛ ℝ≥0∞) (μ : ι → measure α) : f.lintegral (measure.sum μ) = ∑' i, f.lintegral (μ i) := begin simp only [lintegral, measure.sum_apply, f.measurable_set_preimage, ← finset.tsum_subtype, ← ennreal.tsum_mul_left], apply ennreal.tsum_comm end lemma restrict_lintegral (f : α →ₛ ℝ≥0∞) {s : set α} (hs : measurable_set s) : (restrict f s).lintegral μ = ∑ r in f.range, r * μ (f ⁻¹' {r} ∩ s) := calc (restrict f s).lintegral μ = ∑ r in f.range, r * μ (restrict f s ⁻¹' {r}) : lintegral_eq_of_subset _ $ λ x hx, if hxs : x ∈ s then λ _, by simp only [f.restrict_apply hs, indicator_of_mem hxs, mem_range_self] else false.elim $ hx $ by simp [*] ... = ∑ r in f.range, r * μ (f ⁻¹' {r} ∩ s) : finset.sum_congr rfl $ forall_range_iff.2 $ λ b, if hb : f b = 0 then by simp only [hb, zero_mul] else by rw [restrict_preimage_singleton _ hs hb, inter_comm] lemma lintegral_restrict (f : α →ₛ ℝ≥0∞) (s : set α) (μ : measure α) : f.lintegral (μ.restrict s) = ∑ y in f.range, y * μ (f ⁻¹' {y} ∩ s) := by simp only [lintegral, measure.restrict_apply, f.measurable_set_preimage] lemma restrict_lintegral_eq_lintegral_restrict (f : α →ₛ ℝ≥0∞) {s : set α} (hs : measurable_set s) : (restrict f s).lintegral μ = f.lintegral (μ.restrict s) := by rw [f.restrict_lintegral hs, lintegral_restrict] lemma const_lintegral (c : ℝ≥0∞) : (const α c).lintegral μ = c * μ univ := begin rw [lintegral], by_cases ha : nonempty α, { resetI, simp [preimage_const_of_mem] }, { simp [μ.eq_zero_of_not_nonempty ha] } end lemma const_lintegral_restrict (c : ℝ≥0∞) (s : set α) : (const α c).lintegral (μ.restrict s) = c * μ s := by rw [const_lintegral, measure.restrict_apply measurable_set.univ, univ_inter] lemma restrict_const_lintegral (c : ℝ≥0∞) {s : set α} (hs : measurable_set s) : ((const α c).restrict s).lintegral μ = c * μ s := by rw [restrict_lintegral_eq_lintegral_restrict _ hs, const_lintegral_restrict] lemma le_sup_lintegral (f g : α →ₛ ℝ≥0∞) : f.lintegral μ ⊔ g.lintegral μ ≤ (f ⊔ g).lintegral μ := calc f.lintegral μ ⊔ g.lintegral μ = ((pair f g).map prod.fst).lintegral μ ⊔ ((pair f g).map prod.snd).lintegral μ : rfl ... ≤ ∑ x in (pair f g).range, (x.1 ⊔ x.2) * μ (pair f g ⁻¹' {x}) : begin rw [map_lintegral, map_lintegral], refine sup_le _ _; refine finset.sum_le_sum (λ a _, canonically_ordered_semiring.mul_le_mul _ (le_refl _)), exact le_sup_left, exact le_sup_right end ... = (f ⊔ g).lintegral μ : by rw [sup_eq_map₂, map_lintegral] /-- `simple_func.lintegral` is monotone both in function and in measure. -/ @[mono] lemma lintegral_mono {f g : α →ₛ ℝ≥0∞} (hfg : f ≤ g) {μ ν : measure α} (hμν : μ ≤ ν) : f.lintegral μ ≤ g.lintegral ν := calc f.lintegral μ ≤ f.lintegral μ ⊔ g.lintegral μ : le_sup_left ... ≤ (f ⊔ g).lintegral μ : le_sup_lintegral _ _ ... = g.lintegral μ : by rw [sup_of_le_right hfg] ... ≤ g.lintegral ν : finset.sum_le_sum $ λ y hy, ennreal.mul_left_mono $ hμν _ (g.measurable_set_preimage _) /-- `simple_func.lintegral` depends only on the measures of `f ⁻¹' {y}`. -/ lemma lintegral_eq_of_measure_preimage [measurable_space β] {f : α →ₛ ℝ≥0∞} {g : β →ₛ ℝ≥0∞} {ν : measure β} (H : ∀ y, μ (f ⁻¹' {y}) = ν (g ⁻¹' {y})) : f.lintegral μ = g.lintegral ν := begin simp only [lintegral, ← H], apply lintegral_eq_of_subset, simp only [H], intros, exact mem_range_of_measure_ne_zero ‹_› end /-- If two simple functions are equal a.e., then their `lintegral`s are equal. -/ lemma lintegral_congr {f g : α →ₛ ℝ≥0∞} (h : f =ᵐ[μ] g) : f.lintegral μ = g.lintegral μ := lintegral_eq_of_measure_preimage $ λ y, measure_congr $ eventually.set_eq $ h.mono $ λ x hx, by simp [hx] lemma lintegral_map {β} [measurable_space β] {μ' : measure β} (f : α →ₛ ℝ≥0∞) (g : β →ₛ ℝ≥0∞) (m : α → β) (eq : ∀a:α, f a = g (m a)) (h : ∀s:set β, measurable_set s → μ' s = μ (m ⁻¹' s)) : f.lintegral μ = g.lintegral μ' := lintegral_eq_of_measure_preimage $ λ y, by { simp only [preimage, eq], exact (h (g ⁻¹' {y}) (g.measurable_set_preimage _)).symm } end measure section fin_meas_supp variables [measurable_space α] [has_zero β] [has_zero γ] {μ : measure α} open finset function lemma support_eq (f : α →ₛ β) : support f = ⋃ y ∈ f.range.filter (λ y, y ≠ 0), f ⁻¹' {y} := set.ext $ λ x, by simp only [finset.set_bUnion_preimage_singleton, mem_support, set.mem_preimage, finset.mem_coe, mem_filter, mem_range_self, true_and] /-- A `simple_func` has finite measure support if it is equal to `0` outside of a set of finite measure. -/ protected def fin_meas_supp (f : α →ₛ β) (μ : measure α) : Prop := f =ᶠ[μ.cofinite] 0 lemma fin_meas_supp_iff_support {f : α →ₛ β} {μ : measure α} : f.fin_meas_supp μ ↔ μ (support f) < ∞ := iff.rfl lemma fin_meas_supp_iff {f : α →ₛ β} {μ : measure α} : f.fin_meas_supp μ ↔ ∀ y ≠ 0, μ (f ⁻¹' {y}) < ∞ := begin split, { refine λ h y hy, lt_of_le_of_lt (measure_mono _) h, exact λ x hx (H : f x = 0), hy $ H ▸ eq.symm hx }, { intro H, rw [fin_meas_supp_iff_support, support_eq], refine lt_of_le_of_lt (measure_bUnion_finset_le _ _) (sum_lt_top _), exact λ y hy, H y (finset.mem_filter.1 hy).2 } end namespace fin_meas_supp lemma meas_preimage_singleton_ne_zero {f : α →ₛ β} (h : f.fin_meas_supp μ) {y : β} (hy : y ≠ 0) : μ (f ⁻¹' {y}) < ∞ := fin_meas_supp_iff.1 h y hy protected lemma map {f : α →ₛ β} {g : β → γ} (hf : f.fin_meas_supp μ) (hg : g 0 = 0) : (f.map g).fin_meas_supp μ := flip lt_of_le_of_lt hf (measure_mono $ support_comp_subset hg f) lemma of_map {f : α →ₛ β} {g : β → γ} (h : (f.map g).fin_meas_supp μ) (hg : ∀b, g b = 0 → b = 0) : f.fin_meas_supp μ := flip lt_of_le_of_lt h $ measure_mono $ support_subset_comp hg _ lemma map_iff {f : α →ₛ β} {g : β → γ} (hg : ∀ {b}, g b = 0 ↔ b = 0) : (f.map g).fin_meas_supp μ ↔ f.fin_meas_supp μ := ⟨λ h, h.of_map $ λ b, hg.1, λ h, h.map $ hg.2 rfl⟩ protected lemma pair {f : α →ₛ β} {g : α →ₛ γ} (hf : f.fin_meas_supp μ) (hg : g.fin_meas_supp μ) : (pair f g).fin_meas_supp μ := calc μ (support $ pair f g) = μ (support f ∪ support g) : congr_arg μ $ support_prod_mk f g ... ≤ μ (support f) + μ (support g) : measure_union_le _ _ ... < _ : add_lt_top.2 ⟨hf, hg⟩ protected lemma map₂ [has_zero δ] {μ : measure α} {f : α →ₛ β} (hf : f.fin_meas_supp μ) {g : α →ₛ γ} (hg : g.fin_meas_supp μ) {op : β → γ → δ} (H : op 0 0 = 0) : ((pair f g).map (function.uncurry op)).fin_meas_supp μ := (hf.pair hg).map H protected lemma add {β} [add_monoid β] {f g : α →ₛ β} (hf : f.fin_meas_supp μ) (hg : g.fin_meas_supp μ) : (f + g).fin_meas_supp μ := by { rw [add_eq_map₂], exact hf.map₂ hg (zero_add 0) } protected lemma mul {β} [monoid_with_zero β] {f g : α →ₛ β} (hf : f.fin_meas_supp μ) (hg : g.fin_meas_supp μ) : (f * g).fin_meas_supp μ := by { rw [mul_eq_map₂], exact hf.map₂ hg (zero_mul 0) } lemma lintegral_lt_top {f : α →ₛ ℝ≥0∞} (hm : f.fin_meas_supp μ) (hf : ∀ᵐ a ∂μ, f a < ∞) : f.lintegral μ < ∞ := begin refine sum_lt_top (λ a ha, _), rcases eq_or_lt_of_le (le_top : a ≤ ∞) with rfl|ha, { simp only [ae_iff, lt_top_iff_ne_top, ne.def, not_not] at hf, simp [set.preimage, hf] }, { by_cases ha0 : a = 0, { subst a, rwa [zero_mul] }, { exact mul_lt_top ha (fin_meas_supp_iff.1 hm _ ha0) } } end lemma of_lintegral_lt_top {f : α →ₛ ℝ≥0∞} (h : f.lintegral μ < ∞) : f.fin_meas_supp μ := begin refine fin_meas_supp_iff.2 (λ b hb, _), rw [lintegral, sum_lt_top_iff] at h, by_cases b_mem : b ∈ f.range, { rw ennreal.lt_top_iff_ne_top, have h : ¬ _ = ∞ := ennreal.lt_top_iff_ne_top.1 (h b b_mem), simp only [mul_eq_top, not_or_distrib, not_and_distrib] at h, rcases h with ⟨h, h'⟩, refine or.elim h (λh, by contradiction) (λh, h) }, { rw ← preimage_eq_empty_iff at b_mem, rw [b_mem, measure_empty], exact with_top.zero_lt_top } end lemma iff_lintegral_lt_top {f : α →ₛ ℝ≥0∞} (hf : ∀ᵐ a ∂μ, f a < ∞) : f.fin_meas_supp μ ↔ f.lintegral μ < ∞ := ⟨λ h, h.lintegral_lt_top hf, λ h, of_lintegral_lt_top h⟩ end fin_meas_supp end fin_meas_supp /-- To prove something for an arbitrary simple function, it suffices to show that the property holds for (multiples of) characteristic functions and is closed under addition (of functions with disjoint support). It is possible to make the hypotheses in `h_add` a bit stronger, and such conditions can be added once we need them (for example it is only necessary to consider the case where `g` is a multiple of a characteristic function, and that this multiple doesn't appear in the image of `f`) -/ @[elab_as_eliminator] protected lemma induction {α γ} [measurable_space α] [add_monoid γ] {P : simple_func α γ → Prop} (h_ind : ∀ c {s} (hs : measurable_set s), P (simple_func.piecewise s hs (simple_func.const _ c) (simple_func.const _ 0))) (h_add : ∀ ⦃f g : simple_func α γ⦄, disjoint (support f) (support g) → P f → P g → P (f + g)) (f : simple_func α γ) : P f := begin generalize' h : f.range \ {0} = s, rw [← finset.coe_inj, finset.coe_sdiff, finset.coe_singleton, simple_func.coe_range] at h, revert s f h, refine finset.induction _ _, { intros f hf, rw [finset.coe_empty, diff_eq_empty, range_subset_singleton] at hf, convert h_ind 0 measurable_set.univ, ext x, simp [hf] }, { intros x s hxs ih f hf, have mx := f.measurable_set_preimage {x}, let g := simple_func.piecewise (f ⁻¹' {x}) mx 0 f, have Pg : P g, { apply ih, simp only [g, simple_func.coe_piecewise, range_piecewise], rw [image_compl_preimage, union_diff_distrib, diff_diff_comm, hf, finset.coe_insert, insert_diff_self_of_not_mem, diff_eq_empty.mpr, set.empty_union], { rw [set.image_subset_iff], convert set.subset_univ _, exact preimage_const_of_mem (mem_singleton _) }, { rwa [finset.mem_coe] }}, convert h_add _ Pg (h_ind x mx), { ext1 y, by_cases hy : y ∈ f ⁻¹' {x}; [simpa [hy], simp [hy]] }, rintro y, by_cases hy : y ∈ f ⁻¹' {x}; simp [hy] } end end simple_func section lintegral open simple_func variables [measurable_space α] {μ : measure α} /-- The lower Lebesgue integral of a function `f` with respect to a measure `μ`. -/ def lintegral (μ : measure α) (f : α → ℝ≥0∞) : ℝ≥0∞ := ⨆ (g : α →ₛ ℝ≥0∞) (hf : ⇑g ≤ f), g.lintegral μ /-! In the notation for integrals, an expression like `∫⁻ x, g ∥x∥ ∂μ` will not be parsed correctly, and needs parentheses. We do not set the binding power of `r` to `0`, because then `∫⁻ x, f x = 0` will be parsed incorrectly. -/ notation `∫⁻` binders `, ` r:(scoped:60 f, f) ` ∂` μ:70 := lintegral μ r notation `∫⁻` binders `, ` r:(scoped:60 f, lintegral volume f) := r notation `∫⁻` binders ` in ` s `, ` r:(scoped:60 f, f) ` ∂` μ:70 := lintegral (measure.restrict μ s) r notation `∫⁻` binders ` in ` s `, ` r:(scoped:60 f, lintegral (measure.restrict volume s) f) := r theorem simple_func.lintegral_eq_lintegral (f : α →ₛ ℝ≥0∞) (μ : measure α) : ∫⁻ a, f a ∂ μ = f.lintegral μ := le_antisymm (bsupr_le $ λ g hg, lintegral_mono hg $ le_refl _) (le_supr_of_le f $ le_supr_of_le (le_refl _) (le_refl _)) @[mono] lemma lintegral_mono' ⦃μ ν : measure α⦄ (hμν : μ ≤ ν) ⦃f g : α → ℝ≥0∞⦄ (hfg : f ≤ g) : ∫⁻ a, f a ∂μ ≤ ∫⁻ a, g a ∂ν := supr_le_supr $ λ φ, supr_le_supr2 $ λ hφ, ⟨le_trans hφ hfg, lintegral_mono (le_refl φ) hμν⟩ lemma lintegral_mono ⦃f g : α → ℝ≥0∞⦄ (hfg : f ≤ g) : ∫⁻ a, f a ∂μ ≤ ∫⁻ a, g a ∂μ := lintegral_mono' (le_refl μ) hfg lemma lintegral_mono_nnreal {f g : α → ℝ≥0} (h : f ≤ g) : ∫⁻ a, f a ∂μ ≤ ∫⁻ a, g a ∂μ := begin refine lintegral_mono _, intro a, rw ennreal.coe_le_coe, exact h a, end lemma monotone_lintegral (μ : measure α) : monotone (lintegral μ) := lintegral_mono @[simp] lemma lintegral_const (c : ℝ≥0∞) : ∫⁻ a, c ∂μ = c * μ univ := by rw [← simple_func.const_lintegral, ← simple_func.lintegral_eq_lintegral, simple_func.coe_const] @[simp] lemma lintegral_one : ∫⁻ a, (1 : ℝ≥0∞) ∂μ = μ univ := by rw [lintegral_const, one_mul] lemma set_lintegral_const (s : set α) (c : ℝ≥0∞) : ∫⁻ a in s, c ∂μ = c * μ s := by rw [lintegral_const, measure.restrict_apply_univ] lemma set_lintegral_one (s) : ∫⁻ a in s, 1 ∂μ = μ s := by rw [set_lintegral_const, one_mul] /-- `∫⁻ a in s, f a ∂μ` is defined as the supremum of integrals of simple functions `φ : α →ₛ ℝ≥0∞` such that `φ ≤ f`. This lemma says that it suffices to take functions `φ : α →ₛ ℝ≥0`. -/ lemma lintegral_eq_nnreal (f : α → ℝ≥0∞) (μ : measure α) : (∫⁻ a, f a ∂μ) = (⨆ (φ : α →ₛ ℝ≥0) (hf : ∀ x, ↑(φ x) ≤ f x), (φ.map (coe : ℝ≥0 → ℝ≥0∞)).lintegral μ) := begin refine le_antisymm (bsupr_le $ assume φ hφ, _) (supr_le_supr2 $ λ φ, ⟨φ.map (coe : ℝ≥0 → ℝ≥0∞), le_refl _⟩), by_cases h : ∀ᵐ a ∂μ, φ a ≠ ∞, { let ψ := φ.map ennreal.to_nnreal, replace h : ψ.map (coe : ℝ≥0 → ℝ≥0∞) =ᵐ[μ] φ := h.mono (λ a, ennreal.coe_to_nnreal), have : ∀ x, ↑(ψ x) ≤ f x := λ x, le_trans ennreal.coe_to_nnreal_le_self (hφ x), exact le_supr_of_le (φ.map ennreal.to_nnreal) (le_supr_of_le this (ge_of_eq $ lintegral_congr h)) }, { have h_meas : μ (φ ⁻¹' {∞}) ≠ 0, from mt measure_zero_iff_ae_nmem.1 h, refine le_trans le_top (ge_of_eq $ (supr_eq_top _).2 $ λ b hb, _), obtain ⟨n, hn⟩ : ∃ n : ℕ, b < n * μ (φ ⁻¹' {∞}), from exists_nat_mul_gt h_meas (ne_of_lt hb), use (const α (n : ℝ≥0)).restrict (φ ⁻¹' {∞}), simp only [lt_supr_iff, exists_prop, coe_restrict, φ.measurable_set_preimage, coe_const, ennreal.coe_indicator, map_coe_ennreal_restrict, map_const, ennreal.coe_nat, restrict_const_lintegral], refine ⟨indicator_le (λ x hx, le_trans _ (hφ _)), hn⟩, simp only [mem_preimage, mem_singleton_iff] at hx, simp only [hx, le_top] } end lemma exists_simple_func_forall_lintegral_sub_lt_of_pos {f : α → ℝ≥0∞} (h : ∫⁻ x, f x ∂μ < ∞) {ε : ℝ≥0∞} (hε : 0 < ε) : ∃ φ : α →ₛ ℝ≥0, (∀ x, ↑(φ x) ≤ f x) ∧ ∀ ψ : α →ₛ ℝ≥0, (∀ x, ↑(ψ x) ≤ f x) → (map coe (ψ - φ)).lintegral μ < ε := begin rw lintegral_eq_nnreal at h, have := ennreal.lt_add_right h hε, erw ennreal.bsupr_add at this; [skip, exact ⟨0, λ x, by simp⟩], simp_rw [lt_supr_iff, supr_lt_iff, supr_le_iff] at this, rcases this with ⟨φ, hle : ∀ x, ↑(φ x) ≤ f x, b, hbφ, hb⟩, refine ⟨φ, hle, λ ψ hψ, _⟩, have : (map coe φ).lintegral μ < ∞, from (le_bsupr φ hle).trans_lt h, rw [← add_lt_add_iff_left this, ← add_lintegral, ← map_add @ennreal.coe_add], refine (hb _ (λ x, le_trans _ (max_le (hle x) (hψ x)))).trans_lt hbφ, norm_cast, simp only [add_apply, sub_apply, nnreal.add_sub_eq_max] end theorem supr_lintegral_le {ι : Sort*} (f : ι → α → ℝ≥0∞) : (⨆i, ∫⁻ a, f i a ∂μ) ≤ (∫⁻ a, ⨆i, f i a ∂μ) := begin simp only [← supr_apply], exact (monotone_lintegral μ).le_map_supr end theorem supr2_lintegral_le {ι : Sort*} {ι' : ι → Sort*} (f : Π i, ι' i → α → ℝ≥0∞) : (⨆i (h : ι' i), ∫⁻ a, f i h a ∂μ) ≤ (∫⁻ a, ⨆i (h : ι' i), f i h a ∂μ) := by { convert (monotone_lintegral μ).le_map_supr2 f, ext1 a, simp only [supr_apply] } theorem le_infi_lintegral {ι : Sort*} (f : ι → α → ℝ≥0∞) : (∫⁻ a, ⨅i, f i a ∂μ) ≤ (⨅i, ∫⁻ a, f i a ∂μ) := by { simp only [← infi_apply], exact (monotone_lintegral μ).map_infi_le } theorem le_infi2_lintegral {ι : Sort*} {ι' : ι → Sort*} (f : Π i, ι' i → α → ℝ≥0∞) : (∫⁻ a, ⨅ i (h : ι' i), f i h a ∂μ) ≤ (⨅ i (h : ι' i), ∫⁻ a, f i h a ∂μ) := by { convert (monotone_lintegral μ).map_infi2_le f, ext1 a, simp only [infi_apply] } lemma lintegral_mono_ae {f g : α → ℝ≥0∞} (h : ∀ᵐ a ∂μ, f a ≤ g a) : (∫⁻ a, f a ∂μ) ≤ (∫⁻ a, g a ∂μ) := begin rcases exists_measurable_superset_of_null h with ⟨t, hts, ht, ht0⟩, have : ∀ᵐ x ∂μ, x ∉ t := measure_zero_iff_ae_nmem.1 ht0, refine (supr_le $ assume s, supr_le $ assume hfs, le_supr_of_le (s.restrict tᶜ) $ le_supr_of_le _ _), { assume a, by_cases a ∈ t; simp [h, restrict_apply, ht.compl], exact le_trans (hfs a) (by_contradiction $ assume hnfg, h (hts hnfg)) }, { refine le_of_eq (simple_func.lintegral_congr $ this.mono $ λ a hnt, _), by_cases hat : a ∈ t; simp [hat, ht.compl], exact (hnt hat).elim } end lemma lintegral_congr_ae {f g : α → ℝ≥0∞} (h : f =ᵐ[μ] g) : (∫⁻ a, f a ∂μ) = (∫⁻ a, g a ∂μ) := le_antisymm (lintegral_mono_ae $ h.le) (lintegral_mono_ae $ h.symm.le) lemma lintegral_congr {f g : α → ℝ≥0∞} (h : ∀ a, f a = g a) : (∫⁻ a, f a ∂μ) = (∫⁻ a, g a ∂μ) := by simp only [h] lemma set_lintegral_congr {f : α → ℝ≥0∞} {s t : set α} (h : s =ᵐ[μ] t) : ∫⁻ x in s, f x ∂μ = ∫⁻ x in t, f x ∂μ := by rw [restrict_congr_set h] /-- Monotone convergence theorem -- sometimes called Beppo-Levi convergence. See `lintegral_supr_directed` for a more general form. -/ theorem lintegral_supr {f : ℕ → α → ℝ≥0∞} (hf : ∀n, measurable (f n)) (h_mono : monotone f) : (∫⁻ a, ⨆n, f n a ∂μ) = (⨆n, ∫⁻ a, f n a ∂μ) := begin set c : ℝ≥0 → ℝ≥0∞ := coe, set F := λ a:α, ⨆n, f n a, have hF : measurable F := measurable_supr hf, refine le_antisymm _ (supr_lintegral_le _), rw [lintegral_eq_nnreal], refine supr_le (assume s, supr_le (assume hsf, _)), refine ennreal.le_of_forall_lt_one_mul_le (assume a ha, _), rcases ennreal.lt_iff_exists_coe.1 ha with ⟨r, rfl, ha⟩, have ha : r < 1 := ennreal.coe_lt_coe.1 ha, let rs := s.map (λa, r * a), have eq_rs : (const α r : α →ₛ ℝ≥0∞) * map c s = rs.map c, { ext1 a, exact ennreal.coe_mul.symm }, have eq : ∀p, (rs.map c) ⁻¹' {p} = (⋃n, (rs.map c) ⁻¹' {p} ∩ {a | p ≤ f n a}), { assume p, rw [← inter_Union, ← inter_univ ((map c rs) ⁻¹' {p})] {occs := occurrences.pos [1]}, refine set.ext (assume x, and_congr_right $ assume hx, (true_iff _).2 _), by_cases p_eq : p = 0, { simp [p_eq] }, simp at hx, subst hx, have : r * s x ≠ 0, { rwa [(≠), ← ennreal.coe_eq_zero] }, have : s x ≠ 0, { refine mt _ this, assume h, rw [h, mul_zero] }, have : (rs.map c) x < ⨆ (n : ℕ), f n x, { refine lt_of_lt_of_le (ennreal.coe_lt_coe.2 (_)) (hsf x), suffices : r * s x < 1 * s x, simpa [rs], exact mul_lt_mul_of_pos_right ha (pos_iff_ne_zero.2 this) }, rcases lt_supr_iff.1 this with ⟨i, hi⟩, exact mem_Union.2 ⟨i, le_of_lt hi⟩ }, have mono : ∀r:ℝ≥0∞, monotone (λn, (rs.map c) ⁻¹' {r} ∩ {a | r ≤ f n a}), { assume r i j h, refine inter_subset_inter (subset.refl _) _, assume x hx, exact le_trans hx (h_mono h x) }, have h_meas : ∀n, measurable_set {a : α | ⇑(map c rs) a ≤ f n a} := assume n, measurable_set_le (simple_func.measurable _) (hf n), calc (r:ℝ≥0∞) * (s.map c).lintegral μ = ∑ r in (rs.map c).range, r * μ ((rs.map c) ⁻¹' {r}) : by rw [← const_mul_lintegral, eq_rs, simple_func.lintegral] ... ≤ ∑ r in (rs.map c).range, r * μ (⋃n, (rs.map c) ⁻¹' {r} ∩ {a | r ≤ f n a}) : le_of_eq (finset.sum_congr rfl $ assume x hx, by rw ← eq) ... ≤ ∑ r in (rs.map c).range, (⨆n, r * μ ((rs.map c) ⁻¹' {r} ∩ {a | r ≤ f n a})) : le_of_eq (finset.sum_congr rfl $ assume x hx, begin rw [measure_Union_eq_supr _ (directed_of_sup $ mono x), ennreal.mul_supr], { assume i, refine ((rs.map c).measurable_set_preimage _).inter _, exact hf i measurable_set_Ici } end) ... ≤ ⨆n, ∑ r in (rs.map c).range, r * μ ((rs.map c) ⁻¹' {r} ∩ {a | r ≤ f n a}) : begin refine le_of_eq _, rw [ennreal.finset_sum_supr_nat], assume p i j h, exact canonically_ordered_semiring.mul_le_mul (le_refl _) (measure_mono $ mono p h) end ... ≤ (⨆n:ℕ, ((rs.map c).restrict {a | (rs.map c) a ≤ f n a}).lintegral μ) : begin refine supr_le_supr (assume n, _), rw [restrict_lintegral _ (h_meas n)], { refine le_of_eq (finset.sum_congr rfl $ assume r hr, _), congr' 2 with a, refine and_congr_right _, simp {contextual := tt} } end ... ≤ (⨆n, ∫⁻ a, f n a ∂μ) : begin refine supr_le_supr (assume n, _), rw [← simple_func.lintegral_eq_lintegral], refine lintegral_mono (assume a, _), simp only [map_apply] at h_meas, simp only [coe_map, restrict_apply _ (h_meas _), (∘)], exact indicator_apply_le id, end end /-- Monotone convergence theorem -- sometimes called Beppo-Levi convergence. Version with ae_measurable functions. -/ theorem lintegral_supr' {f : ℕ → α → ℝ≥0∞} (hf : ∀n, ae_measurable (f n) μ) (h_mono : ∀ᵐ x ∂μ, monotone (λ n, f n x)) : (∫⁻ a, ⨆n, f n a ∂μ) = (⨆n, ∫⁻ a, f n a ∂μ) := begin simp_rw ←supr_apply, let p : α → (ℕ → ℝ≥0∞) → Prop := λ x f', monotone f', have hp : ∀ᵐ x ∂μ, p x (λ i, f i x), from h_mono, have h_ae_seq_mono : monotone (ae_seq hf p), { intros n m hnm x, by_cases hx : x ∈ ae_seq_set hf p, { exact ae_seq.prop_of_mem_ae_seq_set hf hx hnm, }, { simp only [ae_seq, hx, if_false], exact le_refl _, }, }, rw lintegral_congr_ae (ae_seq.supr hf hp).symm, simp_rw supr_apply, rw @lintegral_supr _ _ μ _ (ae_seq.measurable hf p) h_ae_seq_mono, congr, exact funext (λ n, lintegral_congr_ae (ae_seq.ae_seq_n_eq_fun_n_ae hf hp n)), end lemma lintegral_eq_supr_eapprox_lintegral {f : α → ℝ≥0∞} (hf : measurable f) : (∫⁻ a, f a ∂μ) = (⨆n, (eapprox f n).lintegral μ) := calc (∫⁻ a, f a ∂μ) = (∫⁻ a, ⨆n, (eapprox f n : α → ℝ≥0∞) a ∂μ) : by congr; ext a; rw [supr_eapprox_apply f hf] ... = (⨆n, ∫⁻ a, (eapprox f n : α → ℝ≥0∞) a ∂μ) : begin rw [lintegral_supr], { assume n, exact (eapprox f n).measurable }, { assume i j h, exact (monotone_eapprox f h) } end ... = (⨆n, (eapprox f n).lintegral μ) : by congr; ext n; rw [(eapprox f n).lintegral_eq_lintegral] /-- If `f` has finite integral, then `∫⁻ x in s, f x ∂μ` is absolutely continuous in `s`: it tends to zero as `μ s` tends to zero. This lemma states states this fact in terms of `ε` and `δ`. -/ lemma exists_pos_set_lintegral_lt_of_measure_lt {f : α → ℝ≥0∞} (h : ∫⁻ x, f x ∂μ < ∞) {ε : ℝ≥0∞} (hε : 0 < ε) : ∃ δ > 0, ∀ s, μ s < δ → ∫⁻ x in s, f x ∂μ < ε := begin rcases exists_between hε with ⟨ε₂, hε₂0, hε₂ε⟩, rcases exists_between hε₂0 with ⟨ε₁, hε₁0, hε₁₂⟩, rcases exists_simple_func_forall_lintegral_sub_lt_of_pos h hε₁0 with ⟨φ, hle, hφ⟩, rcases φ.exists_forall_le with ⟨C, hC⟩, use [(ε₂ - ε₁) / C, ennreal.div_pos_iff.2 ⟨(zero_lt_sub_iff_lt.2 hε₁₂).ne', ennreal.coe_ne_top⟩], intros s hs, simp only [lintegral_eq_nnreal, supr_lt_iff, supr_le_iff], refine ⟨ε₂, hε₂ε, λ ψ hψ, _⟩, calc (map coe ψ).lintegral (μ.restrict s) ≤ (map coe φ).lintegral (μ.restrict s) + (map coe (ψ - φ)).lintegral (μ.restrict s) : begin rw [← simple_func.add_lintegral, ← simple_func.map_add @ennreal.coe_add], refine simple_func.lintegral_mono (λ x, _) le_rfl, simp [-ennreal.coe_add, nnreal.add_sub_eq_max, le_max_right] end ... ≤ (map coe φ).lintegral (μ.restrict s) + ε₁ : begin refine add_le_add le_rfl (le_trans _ (hφ _ hψ).le), exact simple_func.lintegral_mono le_rfl measure.restrict_le_self end ... ≤ (simple_func.const α (C : ℝ≥0∞)).lintegral (μ.restrict s) + ε₁ : by { mono*, exacts [λ x, coe_le_coe.2 (hC x), le_rfl, le_rfl] } ... = C * μ s + ε₁ : by simp [← simple_func.lintegral_eq_lintegral] ... ≤ C * ((ε₂ - ε₁) / C) + ε₁ : by { mono*, exacts [le_rfl, hs.le, le_rfl] } ... ≤ (ε₂ - ε₁) + ε₁ : add_le_add mul_div_le le_rfl ... = ε₂ : sub_add_cancel_of_le hε₁₂.le, end /-- If `f` has finite integral, then `∫⁻ x in s, f x ∂μ` is absolutely continuous in `s`: it tends to zero as `μ s` tends to zero. -/ lemma tendsto_set_lintegral_zero {ι} {f : α → ℝ≥0∞} (h : ∫⁻ x, f x ∂μ < ∞) {l : filter ι} {s : ι → set α} (hl : tendsto (μ ∘ s) l (𝓝 0)) : tendsto (λ i, ∫⁻ x in s i, f x ∂μ) l (𝓝 0) := begin simp only [ennreal.nhds_zero, tendsto_infi, tendsto_principal, mem_Iio, ← pos_iff_ne_zero] at hl ⊢, intros ε ε0, rcases exists_pos_set_lintegral_lt_of_measure_lt h ε0 with ⟨δ, δ0, hδ⟩, exact (hl δ δ0).mono (λ i, hδ _) end @[simp] lemma lintegral_add {f g : α → ℝ≥0∞} (hf : measurable f) (hg : measurable g) : (∫⁻ a, f a + g a ∂μ) = (∫⁻ a, f a ∂μ) + (∫⁻ a, g a ∂μ) := calc (∫⁻ a, f a + g a ∂μ) = (∫⁻ a, (⨆n, (eapprox f n : α → ℝ≥0∞) a) + (⨆n, (eapprox g n : α → ℝ≥0∞) a) ∂μ) : by simp only [supr_eapprox_apply, hf, hg] ... = (∫⁻ a, (⨆n, (eapprox f n + eapprox g n : α → ℝ≥0∞) a) ∂μ) : begin congr, funext a, rw [ennreal.supr_add_supr_of_monotone], { refl }, { assume i j h, exact monotone_eapprox _ h a }, { assume i j h, exact monotone_eapprox _ h a }, end ... = (⨆n, (eapprox f n).lintegral μ + (eapprox g n).lintegral μ) : begin rw [lintegral_supr], { congr, funext n, rw [← simple_func.add_lintegral, ← simple_func.lintegral_eq_lintegral], refl }, { assume n, exact measurable.add (eapprox f n).measurable (eapprox g n).measurable }, { assume i j h a, exact add_le_add (monotone_eapprox _ h _) (monotone_eapprox _ h _) } end ... = (⨆n, (eapprox f n).lintegral μ) + (⨆n, (eapprox g n).lintegral μ) : by refine (ennreal.supr_add_supr_of_monotone _ _).symm; { assume i j h, exact simple_func.lintegral_mono (monotone_eapprox _ h) (le_refl μ) } ... = (∫⁻ a, f a ∂μ) + (∫⁻ a, g a ∂μ) : by rw [lintegral_eq_supr_eapprox_lintegral hf, lintegral_eq_supr_eapprox_lintegral hg] lemma lintegral_add' {f g : α → ℝ≥0∞} (hf : ae_measurable f μ) (hg : ae_measurable g μ) : (∫⁻ a, f a + g a ∂μ) = (∫⁻ a, f a ∂μ) + (∫⁻ a, g a ∂μ) := calc (∫⁻ a, f a + g a ∂μ) = (∫⁻ a, hf.mk f a + hg.mk g a ∂μ) : lintegral_congr_ae (eventually_eq.add hf.ae_eq_mk hg.ae_eq_mk) ... = (∫⁻ a, hf.mk f a ∂μ) + (∫⁻ a, hg.mk g a ∂μ) : lintegral_add hf.measurable_mk hg.measurable_mk ... = (∫⁻ a, f a ∂μ) + (∫⁻ a, g a ∂μ) : begin congr' 1, { exact lintegral_congr_ae hf.ae_eq_mk.symm }, { exact lintegral_congr_ae hg.ae_eq_mk.symm }, end lemma lintegral_zero : (∫⁻ a:α, 0 ∂μ) = 0 := by simp lemma lintegral_zero_fun : (∫⁻ a:α, (0 : α → ℝ≥0∞) a ∂μ) = 0 := by simp @[simp] lemma lintegral_smul_measure (c : ℝ≥0∞) (f : α → ℝ≥0∞) : ∫⁻ a, f a ∂ (c • μ) = c * ∫⁻ a, f a ∂μ := by simp only [lintegral, supr_subtype', simple_func.lintegral_smul, ennreal.mul_supr, smul_eq_mul] @[simp] lemma lintegral_sum_measure {ι} (f : α → ℝ≥0∞) (μ : ι → measure α) : ∫⁻ a, f a ∂(measure.sum μ) = ∑' i, ∫⁻ a, f a ∂(μ i) := begin simp only [lintegral, supr_subtype', simple_func.lintegral_sum, ennreal.tsum_eq_supr_sum], rw [supr_comm], congr, funext s, induction s using finset.induction_on with i s hi hs, { apply bot_unique, simp }, simp only [finset.sum_insert hi, ← hs], refine (ennreal.supr_add_supr _).symm, intros φ ψ, exact ⟨⟨φ ⊔ ψ, λ x, sup_le (φ.2 x) (ψ.2 x)⟩, add_le_add (simple_func.lintegral_mono le_sup_left (le_refl _)) (finset.sum_le_sum $ λ j hj, simple_func.lintegral_mono le_sup_right (le_refl _))⟩ end @[simp] lemma lintegral_add_measure (f : α → ℝ≥0∞) (μ ν : measure α) : ∫⁻ a, f a ∂ (μ + ν) = ∫⁻ a, f a ∂μ + ∫⁻ a, f a ∂ν := by simpa [tsum_fintype] using lintegral_sum_measure f (λ b, cond b μ ν) @[simp] lemma lintegral_zero_measure (f : α → ℝ≥0∞) : ∫⁻ a, f a ∂0 = 0 := bot_unique $ by simp [lintegral] lemma lintegral_finset_sum (s : finset β) {f : β → α → ℝ≥0∞} (hf : ∀ b ∈ s, measurable (f b)) : (∫⁻ a, ∑ b in s, f b a ∂μ) = ∑ b in s, ∫⁻ a, f b a ∂μ := begin induction s using finset.induction_on with a s has ih, { simp }, { simp only [finset.sum_insert has], rw [finset.forall_mem_insert] at hf, rw [lintegral_add hf.1 (s.measurable_sum hf.2), ih hf.2] } end @[simp] lemma lintegral_const_mul (r : ℝ≥0∞) {f : α → ℝ≥0∞} (hf : measurable f) : (∫⁻ a, r * f a ∂μ) = r * (∫⁻ a, f a ∂μ) := calc (∫⁻ a, r * f a ∂μ) = (∫⁻ a, (⨆n, (const α r * eapprox f n) a) ∂μ) : by { congr, funext a, rw [← supr_eapprox_apply f hf, ennreal.mul_supr], refl } ... = (⨆n, r * (eapprox f n).lintegral μ) : begin rw [lintegral_supr], { congr, funext n, rw [← simple_func.const_mul_lintegral, ← simple_func.lintegral_eq_lintegral] }, { assume n, exact simple_func.measurable _ }, { assume i j h a, exact canonically_ordered_semiring.mul_le_mul (le_refl _) (monotone_eapprox _ h _) } end ... = r * (∫⁻ a, f a ∂μ) : by rw [← ennreal.mul_supr, lintegral_eq_supr_eapprox_lintegral hf] lemma lintegral_const_mul'' (r : ℝ≥0∞) {f : α → ℝ≥0∞} (hf : ae_measurable f μ) : (∫⁻ a, r * f a ∂μ) = r * (∫⁻ a, f a ∂μ) := begin have A : ∫⁻ a, f a ∂μ = ∫⁻ a, hf.mk f a ∂μ := lintegral_congr_ae hf.ae_eq_mk, have B : ∫⁻ a, r * f a ∂μ = ∫⁻ a, r * hf.mk f a ∂μ := lintegral_congr_ae (eventually_eq.fun_comp hf.ae_eq_mk _), rw [A, B, lintegral_const_mul _ hf.measurable_mk], end lemma lintegral_const_mul_le (r : ℝ≥0∞) (f : α → ℝ≥0∞) : r * (∫⁻ a, f a ∂μ) ≤ (∫⁻ a, r * f a ∂μ) := begin rw [lintegral, ennreal.mul_supr], refine supr_le (λs, _), rw [ennreal.mul_supr], simp only [supr_le_iff, ge_iff_le], assume hs, rw ← simple_func.const_mul_lintegral, refine le_supr_of_le (const α r * s) (le_supr_of_le (λx, _) (le_refl _)), exact canonically_ordered_semiring.mul_le_mul (le_refl _) (hs x) end lemma lintegral_const_mul' (r : ℝ≥0∞) (f : α → ℝ≥0∞) (hr : r ≠ ∞) : (∫⁻ a, r * f a ∂μ) = r * (∫⁻ a, f a ∂μ) := begin by_cases h : r = 0, { simp [h] }, apply le_antisymm _ (lintegral_const_mul_le r f), have rinv : r * r⁻¹ = 1 := ennreal.mul_inv_cancel h hr, have rinv' : r ⁻¹ * r = 1, by { rw mul_comm, exact rinv }, have := lintegral_const_mul_le (r⁻¹) (λx, r * f x), simp [(mul_assoc _ _ _).symm, rinv'] at this, simpa [(mul_assoc _ _ _).symm, rinv] using canonically_ordered_semiring.mul_le_mul (le_refl r) this end lemma lintegral_mul_const (r : ℝ≥0∞) {f : α → ℝ≥0∞} (hf : measurable f) : ∫⁻ a, f a * r ∂μ = ∫⁻ a, f a ∂μ * r := by simp_rw [mul_comm, lintegral_const_mul r hf] lemma lintegral_mul_const'' (r : ℝ≥0∞) {f : α → ℝ≥0∞} (hf : ae_measurable f μ) : ∫⁻ a, f a * r ∂μ = ∫⁻ a, f a ∂μ * r := by simp_rw [mul_comm, lintegral_const_mul'' r hf] lemma lintegral_mul_const_le (r : ℝ≥0∞) (f : α → ℝ≥0∞) : ∫⁻ a, f a ∂μ * r ≤ ∫⁻ a, f a * r ∂μ := by simp_rw [mul_comm, lintegral_const_mul_le r f] lemma lintegral_mul_const' (r : ℝ≥0∞) (f : α → ℝ≥0∞) (hr : r ≠ ∞): ∫⁻ a, f a * r ∂μ = ∫⁻ a, f a ∂μ * r := by simp_rw [mul_comm, lintegral_const_mul' r f hr] /- A double integral of a product where each factor contains only one variable is a product of integrals -/ lemma lintegral_lintegral_mul {β} [measurable_space β] {ν : measure β} {f : α → ℝ≥0∞} {g : β → ℝ≥0∞} (hf : ae_measurable f μ) (hg : ae_measurable g ν) : ∫⁻ x, ∫⁻ y, f x * g y ∂ν ∂μ = ∫⁻ x, f x ∂μ * ∫⁻ y, g y ∂ν := by simp [lintegral_const_mul'' _ hg, lintegral_mul_const'' _ hf] -- TODO: Need a better way of rewriting inside of a integral lemma lintegral_rw₁ {f f' : α → β} (h : f =ᵐ[μ] f') (g : β → ℝ≥0∞) : (∫⁻ a, g (f a) ∂μ) = (∫⁻ a, g (f' a) ∂μ) := lintegral_congr_ae $ h.mono $ λ a h, by rw h -- TODO: Need a better way of rewriting inside of a integral lemma lintegral_rw₂ {f₁ f₁' : α → β} {f₂ f₂' : α → γ} (h₁ : f₁ =ᵐ[μ] f₁') (h₂ : f₂ =ᵐ[μ] f₂') (g : β → γ → ℝ≥0∞) : (∫⁻ a, g (f₁ a) (f₂ a) ∂μ) = (∫⁻ a, g (f₁' a) (f₂' a) ∂μ) := lintegral_congr_ae $ h₁.mp $ h₂.mono $ λ _ h₂ h₁, by rw [h₁, h₂] @[simp] lemma lintegral_indicator (f : α → ℝ≥0∞) {s : set α} (hs : measurable_set s) : ∫⁻ a, s.indicator f a ∂μ = ∫⁻ a in s, f a ∂μ := begin simp only [lintegral, ← restrict_lintegral_eq_lintegral_restrict _ hs, supr_subtype'], apply le_antisymm; refine supr_le_supr2 (subtype.forall.2 $ λ φ hφ, _), { refine ⟨⟨φ, le_trans hφ (indicator_le_self _ _)⟩, _⟩, refine simple_func.lintegral_mono (λ x, _) (le_refl _), by_cases hx : x ∈ s, { simp [hx, hs, le_refl] }, { apply le_trans (hφ x), simp [hx, hs, le_refl] } }, { refine ⟨⟨φ.restrict s, λ x, _⟩, le_refl _⟩, simp [hφ x, hs, indicator_le_indicator] } end /-- Chebyshev's inequality -/ lemma mul_meas_ge_le_lintegral {f : α → ℝ≥0∞} (hf : measurable f) (ε : ℝ≥0∞) : ε * μ {x | ε ≤ f x} ≤ ∫⁻ a, f a ∂μ := begin have : measurable_set {a : α | ε ≤ f a }, from hf measurable_set_Ici, rw [← simple_func.restrict_const_lintegral _ this, ← simple_func.lintegral_eq_lintegral], refine lintegral_mono (λ a, _), simp only [restrict_apply _ this], exact indicator_apply_le id end lemma meas_ge_le_lintegral_div {f : α → ℝ≥0∞} (hf : measurable f) {ε : ℝ≥0∞} (hε : ε ≠ 0) (hε' : ε ≠ ∞) : μ {x | ε ≤ f x} ≤ (∫⁻ a, f a ∂μ) / ε := (ennreal.le_div_iff_mul_le (or.inl hε) (or.inl hε')).2 $ by { rw [mul_comm], exact mul_meas_ge_le_lintegral hf ε } @[simp] lemma lintegral_eq_zero_iff {f : α → ℝ≥0∞} (hf : measurable f) : ∫⁻ a, f a ∂μ = 0 ↔ (f =ᵐ[μ] 0) := begin refine iff.intro (assume h, _) (assume h, _), { have : ∀n:ℕ, ∀ᵐ a ∂μ, f a < n⁻¹, { assume n, rw [ae_iff, ← nonpos_iff_eq_zero, ← @ennreal.zero_div n⁻¹, ennreal.le_div_iff_mul_le, mul_comm], simp only [not_lt], -- TODO: why `rw ← h` fails with "not an equality or an iff"? exacts [h ▸ mul_meas_ge_le_lintegral hf n⁻¹, or.inl (ennreal.inv_ne_zero.2 ennreal.coe_nat_ne_top), or.inr ennreal.zero_ne_top] }, refine (ae_all_iff.2 this).mono (λ a ha, _), by_contradiction h, rcases ennreal.exists_inv_nat_lt h with ⟨n, hn⟩, exact (lt_irrefl _ $ lt_trans hn $ ha n).elim }, { calc ∫⁻ a, f a ∂μ = ∫⁻ a, 0 ∂μ : lintegral_congr_ae h ... = 0 : lintegral_zero } end @[simp] lemma lintegral_eq_zero_iff' {f : α → ℝ≥0∞} (hf : ae_measurable f μ) : ∫⁻ a, f a ∂μ = 0 ↔ (f =ᵐ[μ] 0) := begin have : ∫⁻ a, f a ∂μ = ∫⁻ a, hf.mk f a ∂μ := lintegral_congr_ae hf.ae_eq_mk, rw [this, lintegral_eq_zero_iff hf.measurable_mk], exact ⟨λ H, hf.ae_eq_mk.trans H, λ H, hf.ae_eq_mk.symm.trans H⟩ end lemma lintegral_pos_iff_support {f : α → ℝ≥0∞} (hf : measurable f) : 0 < ∫⁻ a, f a ∂μ ↔ 0 < μ (function.support f) := by simp [pos_iff_ne_zero, hf, filter.eventually_eq, ae_iff, function.support] /-- Weaker version of the monotone convergence theorem-/ lemma lintegral_supr_ae {f : ℕ → α → ℝ≥0∞} (hf : ∀n, measurable (f n)) (h_mono : ∀n, ∀ᵐ a ∂μ, f n a ≤ f n.succ a) : (∫⁻ a, ⨆n, f n a ∂μ) = (⨆n, ∫⁻ a, f n a ∂μ) := let ⟨s, hs⟩ := exists_measurable_superset_of_null (ae_iff.1 (ae_all_iff.2 h_mono)) in let g := λ n a, if a ∈ s then 0 else f n a in have g_eq_f : ∀ᵐ a ∂μ, ∀n, g n a = f n a, from (measure_zero_iff_ae_nmem.1 hs.2.2).mono (assume a ha n, if_neg ha), calc ∫⁻ a, ⨆n, f n a ∂μ = ∫⁻ a, ⨆n, g n a ∂μ : lintegral_congr_ae $ g_eq_f.mono $ λ a ha, by simp only [ha] ... = ⨆n, (∫⁻ a, g n a ∂μ) : lintegral_supr (assume n, measurable_const.piecewise hs.2.1 (hf n)) (monotone_of_monotone_nat $ assume n a, classical.by_cases (assume h : a ∈ s, by simp [g, if_pos h]) (assume h : a ∉ s, begin simp only [g, if_neg h], have := hs.1, rw subset_def at this, have := mt (this a) h, simp only [not_not, mem_set_of_eq] at this, exact this n end)) ... = ⨆n, (∫⁻ a, f n a ∂μ) : by simp only [lintegral_congr_ae (g_eq_f.mono $ λ a ha, ha _)] lemma lintegral_sub {f g : α → ℝ≥0∞} (hf : measurable f) (hg : measurable g) (hg_fin : ∫⁻ a, g a ∂μ < ∞) (h_le : g ≤ᵐ[μ] f) : ∫⁻ a, f a - g a ∂μ = ∫⁻ a, f a ∂μ - ∫⁻ a, g a ∂μ := begin rw [← ennreal.add_left_inj hg_fin, ennreal.sub_add_cancel_of_le (lintegral_mono_ae h_le), ← lintegral_add (hf.sub hg) hg], refine lintegral_congr_ae (h_le.mono $ λ x hx, _), exact ennreal.sub_add_cancel_of_le hx end /-- Monotone convergence theorem for nonincreasing sequences of functions -/ lemma lintegral_infi_ae {f : ℕ → α → ℝ≥0∞} (h_meas : ∀n, measurable (f n)) (h_mono : ∀n:ℕ, f n.succ ≤ᵐ[μ] f n) (h_fin : ∫⁻ a, f 0 a ∂μ < ∞) : ∫⁻ a, ⨅n, f n a ∂μ = ⨅n, ∫⁻ a, f n a ∂μ := have fn_le_f0 : ∫⁻ a, ⨅n, f n a ∂μ ≤ ∫⁻ a, f 0 a ∂μ, from lintegral_mono (assume a, infi_le_of_le 0 (le_refl _)), have fn_le_f0' : (⨅n, ∫⁻ a, f n a ∂μ) ≤ ∫⁻ a, f 0 a ∂μ, from infi_le_of_le 0 (le_refl _), (ennreal.sub_right_inj h_fin fn_le_f0 fn_le_f0').1 $ show ∫⁻ a, f 0 a ∂μ - ∫⁻ a, ⨅n, f n a ∂μ = ∫⁻ a, f 0 a ∂μ - (⨅n, ∫⁻ a, f n a ∂μ), from calc ∫⁻ a, f 0 a ∂μ - (∫⁻ a, ⨅n, f n a ∂μ) = ∫⁻ a, f 0 a - ⨅n, f n a ∂μ: (lintegral_sub (h_meas 0) (measurable_infi h_meas) (calc (∫⁻ a, ⨅n, f n a ∂μ) ≤ ∫⁻ a, f 0 a ∂μ : lintegral_mono (assume a, infi_le _ _) ... < ∞ : h_fin ) (ae_of_all _ $ assume a, infi_le _ _)).symm ... = ∫⁻ a, ⨆n, f 0 a - f n a ∂μ : congr rfl (funext (assume a, ennreal.sub_infi)) ... = ⨆n, ∫⁻ a, f 0 a - f n a ∂μ : lintegral_supr_ae (assume n, (h_meas 0).sub (h_meas n)) (assume n, (h_mono n).mono $ assume a ha, ennreal.sub_le_sub (le_refl _) ha) ... = ⨆n, ∫⁻ a, f 0 a ∂μ - ∫⁻ a, f n a ∂μ : have h_mono : ∀ᵐ a ∂μ, ∀n:ℕ, f n.succ a ≤ f n a := ae_all_iff.2 h_mono, have h_mono : ∀n, ∀ᵐ a ∂μ, f n a ≤ f 0 a := assume n, h_mono.mono $ assume a h, begin induction n with n ih, {exact le_refl _}, {exact le_trans (h n) ih} end, congr rfl (funext $ assume n, lintegral_sub (h_meas _) (h_meas _) (calc ∫⁻ a, f n a ∂μ ≤ ∫⁻ a, f 0 a ∂μ : lintegral_mono_ae $ h_mono n ... < ∞ : h_fin) (h_mono n)) ... = ∫⁻ a, f 0 a ∂μ - ⨅n, ∫⁻ a, f n a ∂μ : ennreal.sub_infi.symm /-- Monotone convergence theorem for nonincreasing sequences of functions -/ lemma lintegral_infi {f : ℕ → α → ℝ≥0∞} (h_meas : ∀n, measurable (f n)) (h_mono : ∀ ⦃m n⦄, m ≤ n → f n ≤ f m) (h_fin : ∫⁻ a, f 0 a ∂μ < ∞) : ∫⁻ a, ⨅n, f n a ∂μ = ⨅n, ∫⁻ a, f n a ∂μ := lintegral_infi_ae h_meas (λ n, ae_of_all _ $ h_mono $ le_of_lt n.lt_succ_self) h_fin /-- Known as Fatou's lemma, version with `ae_measurable` functions -/ lemma lintegral_liminf_le' {f : ℕ → α → ℝ≥0∞} (h_meas : ∀n, ae_measurable (f n) μ) : ∫⁻ a, liminf at_top (λ n, f n a) ∂μ ≤ liminf at_top (λ n, ∫⁻ a, f n a ∂μ) := calc ∫⁻ a, liminf at_top (λ n, f n a) ∂μ = ∫⁻ a, ⨆n:ℕ, ⨅i≥n, f i a ∂μ : by simp only [liminf_eq_supr_infi_of_nat] ... = ⨆n:ℕ, ∫⁻ a, ⨅i≥n, f i a ∂μ : lintegral_supr' (assume n, ae_measurable_binfi _ (countable_encodable _) h_meas) (ae_of_all μ (assume a n m hnm, infi_le_infi_of_subset $ λ i hi, le_trans hnm hi)) ... ≤ ⨆n:ℕ, ⨅i≥n, ∫⁻ a, f i a ∂μ : supr_le_supr $ λ n, le_infi2_lintegral _ ... = at_top.liminf (λ n, ∫⁻ a, f n a ∂μ) : filter.liminf_eq_supr_infi_of_nat.symm /-- Known as Fatou's lemma -/ lemma lintegral_liminf_le {f : ℕ → α → ℝ≥0∞} (h_meas : ∀n, measurable (f n)) : ∫⁻ a, liminf at_top (λ n, f n a) ∂μ ≤ liminf at_top (λ n, ∫⁻ a, f n a ∂μ) := lintegral_liminf_le' (λ n, (h_meas n).ae_measurable) lemma limsup_lintegral_le {f : ℕ → α → ℝ≥0∞} {g : α → ℝ≥0∞} (hf_meas : ∀ n, measurable (f n)) (h_bound : ∀n, f n ≤ᵐ[μ] g) (h_fin : ∫⁻ a, g a ∂μ < ∞) : limsup at_top (λn, ∫⁻ a, f n a ∂μ) ≤ ∫⁻ a, limsup at_top (λn, f n a) ∂μ := calc limsup at_top (λn, ∫⁻ a, f n a ∂μ) = ⨅n:ℕ, ⨆i≥n, ∫⁻ a, f i a ∂μ : limsup_eq_infi_supr_of_nat ... ≤ ⨅n:ℕ, ∫⁻ a, ⨆i≥n, f i a ∂μ : infi_le_infi $ assume n, supr2_lintegral_le _ ... = ∫⁻ a, ⨅n:ℕ, ⨆i≥n, f i a ∂μ : begin refine (lintegral_infi _ _ _).symm, { assume n, exact measurable_bsupr _ (countable_encodable _) hf_meas }, { assume n m hnm a, exact (supr_le_supr_of_subset $ λ i hi, le_trans hnm hi) }, { refine lt_of_le_of_lt (lintegral_mono_ae _) h_fin, refine (ae_all_iff.2 h_bound).mono (λ n hn, _), exact supr_le (λ i, supr_le $ λ hi, hn i) } end ... = ∫⁻ a, limsup at_top (λn, f n a) ∂μ : by simp only [limsup_eq_infi_supr_of_nat] /-- Dominated convergence theorem for nonnegative functions -/ lemma tendsto_lintegral_of_dominated_convergence {F : ℕ → α → ℝ≥0∞} {f : α → ℝ≥0∞} (bound : α → ℝ≥0∞) (hF_meas : ∀n, measurable (F n)) (h_bound : ∀n, F n ≤ᵐ[μ] bound) (h_fin : ∫⁻ a, bound a ∂μ < ∞) (h_lim : ∀ᵐ a ∂μ, tendsto (λ n, F n a) at_top (𝓝 (f a))) : tendsto (λn, ∫⁻ a, F n a ∂μ) at_top (𝓝 (∫⁻ a, f a ∂μ)) := tendsto_of_le_liminf_of_limsup_le (calc ∫⁻ a, f a ∂μ = ∫⁻ a, liminf at_top (λ (n : ℕ), F n a) ∂μ : lintegral_congr_ae $ h_lim.mono $ assume a h, h.liminf_eq.symm ... ≤ liminf at_top (λ n, ∫⁻ a, F n a ∂μ) : lintegral_liminf_le hF_meas) (calc limsup at_top (λ (n : ℕ), ∫⁻ a, F n a ∂μ) ≤ ∫⁻ a, limsup at_top (λn, F n a) ∂μ : limsup_lintegral_le hF_meas h_bound h_fin ... = ∫⁻ a, f a ∂μ : lintegral_congr_ae $ h_lim.mono $ λ a h, h.limsup_eq) /-- Dominated convergence theorem for nonnegative functions which are just almost everywhere measurable. -/ lemma tendsto_lintegral_of_dominated_convergence' {F : ℕ → α → ℝ≥0∞} {f : α → ℝ≥0∞} (bound : α → ℝ≥0∞) (hF_meas : ∀n, ae_measurable (F n) μ) (h_bound : ∀n, F n ≤ᵐ[μ] bound) (h_fin : ∫⁻ a, bound a ∂μ < ∞) (h_lim : ∀ᵐ a ∂μ, tendsto (λ n, F n a) at_top (𝓝 (f a))) : tendsto (λn, ∫⁻ a, F n a ∂μ) at_top (𝓝 (∫⁻ a, f a ∂μ)) := begin have : ∀ n, ∫⁻ a, F n a ∂μ = ∫⁻ a, (hF_meas n).mk (F n) a ∂μ := λ n, lintegral_congr_ae (hF_meas n).ae_eq_mk, simp_rw this, apply tendsto_lintegral_of_dominated_convergence bound (λ n, (hF_meas n).measurable_mk) _ h_fin, { have : ∀ n, ∀ᵐ a ∂μ, (hF_meas n).mk (F n) a = F n a := λ n, (hF_meas n).ae_eq_mk.symm, have : ∀ᵐ a ∂μ, ∀ n, (hF_meas n).mk (F n) a = F n a := ae_all_iff.mpr this, filter_upwards [this, h_lim], assume a H H', simp_rw H, exact H' }, { assume n, filter_upwards [h_bound n, (hF_meas n).ae_eq_mk], assume a H H', rwa H' at H } end /-- Dominated convergence theorem for filters with a countable basis -/ lemma tendsto_lintegral_filter_of_dominated_convergence {ι} {l : filter ι} {F : ι → α → ℝ≥0∞} {f : α → ℝ≥0∞} (bound : α → ℝ≥0∞) (hl_cb : l.is_countably_generated) (hF_meas : ∀ᶠ n in l, measurable (F n)) (h_bound : ∀ᶠ n in l, ∀ᵐ a ∂μ, F n a ≤ bound a) (h_fin : ∫⁻ a, bound a ∂μ < ∞) (h_lim : ∀ᵐ a ∂μ, tendsto (λ n, F n a) l (𝓝 (f a))) : tendsto (λn, ∫⁻ a, F n a ∂μ) l (𝓝 $ ∫⁻ a, f a ∂μ) := begin rw hl_cb.tendsto_iff_seq_tendsto, { intros x xl, have hxl, { rw tendsto_at_top' at xl, exact xl }, have h := inter_mem_sets hF_meas h_bound, replace h := hxl _ h, rcases h with ⟨k, h⟩, rw ← tendsto_add_at_top_iff_nat k, refine tendsto_lintegral_of_dominated_convergence _ _ _ _ _, { exact bound }, { intro, refine (h _ _).1, exact nat.le_add_left _ _ }, { intro, refine (h _ _).2, exact nat.le_add_left _ _ }, { assumption }, { refine h_lim.mono (λ a h_lim, _), apply @tendsto.comp _ _ _ (λn, x (n + k)) (λn, F n a), { assumption }, rw tendsto_add_at_top_iff_nat, assumption } }, end section open encodable /-- Monotone convergence for a suprema over a directed family and indexed by an encodable type -/ theorem lintegral_supr_directed [encodable β] {f : β → α → ℝ≥0∞} (hf : ∀b, measurable (f b)) (h_directed : directed (≤) f) : ∫⁻ a, ⨆b, f b a ∂μ = ⨆b, ∫⁻ a, f b a ∂μ := begin by_cases hβ : nonempty β, swap, { simp [supr_of_empty hβ] }, resetI, inhabit β, have : ∀a, (⨆ b, f b a) = (⨆ n, f (h_directed.sequence f n) a), { assume a, refine le_antisymm (supr_le $ assume b, _) (supr_le $ assume n, le_supr (λn, f n a) _), exact le_supr_of_le (encode b + 1) (h_directed.le_sequence b a) }, calc ∫⁻ a, ⨆ b, f b a ∂μ = ∫⁻ a, ⨆ n, f (h_directed.sequence f n) a ∂μ : by simp only [this] ... = ⨆ n, ∫⁻ a, f (h_directed.sequence f n) a ∂μ : lintegral_supr (assume n, hf _) h_directed.sequence_mono ... = ⨆ b, ∫⁻ a, f b a ∂μ : begin refine le_antisymm (supr_le $ assume n, _) (supr_le $ assume b, _), { exact le_supr (λb, ∫⁻ a, f b a ∂μ) _ }, { exact le_supr_of_le (encode b + 1) (lintegral_mono $ h_directed.le_sequence b) } end end end lemma lintegral_tsum [encodable β] {f : β → α → ℝ≥0∞} (hf : ∀i, measurable (f i)) : ∫⁻ a, ∑' i, f i a ∂μ = ∑' i, ∫⁻ a, f i a ∂μ := begin simp only [ennreal.tsum_eq_supr_sum], rw [lintegral_supr_directed], { simp [lintegral_finset_sum _ (λ i _, hf i)] }, { assume b, exact finset.measurable_sum _ (λ i _, hf i) }, { assume s t, use [s ∪ t], split, exact assume a, finset.sum_le_sum_of_subset (finset.subset_union_left _ _), exact assume a, finset.sum_le_sum_of_subset (finset.subset_union_right _ _) } end open measure lemma lintegral_Union [encodable β] {s : β → set α} (hm : ∀ i, measurable_set (s i)) (hd : pairwise (disjoint on s)) (f : α → ℝ≥0∞) : ∫⁻ a in ⋃ i, s i, f a ∂μ = ∑' i, ∫⁻ a in s i, f a ∂μ := by simp only [measure.restrict_Union hd hm, lintegral_sum_measure] lemma lintegral_Union_le [encodable β] (s : β → set α) (f : α → ℝ≥0∞) : ∫⁻ a in ⋃ i, s i, f a ∂μ ≤ ∑' i, ∫⁻ a in s i, f a ∂μ := begin rw [← lintegral_sum_measure], exact lintegral_mono' restrict_Union_le (le_refl _) end lemma lintegral_map [measurable_space β] {f : β → ℝ≥0∞} {g : α → β} (hf : measurable f) (hg : measurable g) : ∫⁻ a, f a ∂(map g μ) = ∫⁻ a, f (g a) ∂μ := begin simp only [lintegral_eq_supr_eapprox_lintegral, hf, hf.comp hg], { congr, funext n, symmetry, apply simple_func.lintegral_map, { assume a, exact congr_fun (simple_func.eapprox_comp hf hg) a }, { assume s hs, exact map_apply hg hs } }, end lemma lintegral_map' [measurable_space β] {f : β → ℝ≥0∞} {g : α → β} (hf : ae_measurable f (measure.map g μ)) (hg : measurable g) : ∫⁻ a, f a ∂(measure.map g μ) = ∫⁻ a, f (g a) ∂μ := calc ∫⁻ a, f a ∂(measure.map g μ) = ∫⁻ a, hf.mk f a ∂(measure.map g μ) : lintegral_congr_ae hf.ae_eq_mk ... = ∫⁻ a, hf.mk f (g a) ∂μ : lintegral_map hf.measurable_mk hg ... = ∫⁻ a, f (g a) ∂μ : lintegral_congr_ae (ae_eq_comp hg hf.ae_eq_mk.symm) lemma lintegral_comp [measurable_space β] {f : β → ℝ≥0∞} {g : α → β} (hf : measurable f) (hg : measurable g) : lintegral μ (f ∘ g) = ∫⁻ a, f a ∂(map g μ) := (lintegral_map hf hg).symm lemma set_lintegral_map [measurable_space β] {f : β → ℝ≥0∞} {g : α → β} {s : set β} (hs : measurable_set s) (hf : measurable f) (hg : measurable g) : ∫⁻ y in s, f y ∂(map g μ) = ∫⁻ x in g ⁻¹' s, f (g x) ∂μ := by rw [restrict_map hg hs, lintegral_map hf hg] lemma lintegral_dirac' (a : α) {f : α → ℝ≥0∞} (hf : measurable f) : ∫⁻ a, f a ∂(dirac a) = f a := by simp [lintegral_congr_ae (ae_eq_dirac' hf)] lemma lintegral_dirac [measurable_singleton_class α] (a : α) (f : α → ℝ≥0∞) : ∫⁻ a, f a ∂(dirac a) = f a := by simp [lintegral_congr_ae (ae_eq_dirac f)] lemma lintegral_count' {f : α → ℝ≥0∞} (hf : measurable f) : ∫⁻ a, f a ∂count = ∑' a, f a := begin rw [count, lintegral_sum_measure], congr, exact funext (λ a, lintegral_dirac' a hf), end lemma lintegral_count [measurable_singleton_class α] (f : α → ℝ≥0∞) : ∫⁻ a, f a ∂count = ∑' a, f a := begin rw [count, lintegral_sum_measure], congr, exact funext (λ a, lintegral_dirac a f), end lemma ae_lt_top {f : α → ℝ≥0∞} (hf : measurable f) (h2f : ∫⁻ x, f x ∂μ < ∞) : ∀ᵐ x ∂μ, f x < ∞ := begin simp_rw [ae_iff, ennreal.not_lt_top], by_contra h, rw [← not_le] at h2f, apply h2f, have : (f ⁻¹' {∞}).indicator ⊤ ≤ f, { intro x, by_cases hx : x ∈ f ⁻¹' {∞}; [simpa [hx], simp [hx]] }, convert lintegral_mono this, rw [lintegral_indicator _ (hf (measurable_set_singleton ∞))], simp [ennreal.top_mul, preimage, h] end lemma ae_lt_top' {f : α → ℝ≥0∞} (hf : ae_measurable f μ) (h2f : ∫⁻ x, f x ∂μ < ∞) : ∀ᵐ x ∂μ, f x < ∞ := begin have h2f_meas : ∫⁻ x, hf.mk f x ∂μ < ∞, by rwa ←lintegral_congr_ae hf.ae_eq_mk, exact (ae_lt_top hf.measurable_mk h2f_meas).mp (hf.ae_eq_mk.mono (λ x hx h, by rwa hx)), end /-- Given a measure `μ : measure α` and a function `f : α → ℝ≥0∞`, `μ.with_density f` is the measure such that for a measurable set `s` we have `μ.with_density f s = ∫⁻ a in s, f a ∂μ`. -/ def measure.with_density (μ : measure α) (f : α → ℝ≥0∞) : measure α := measure.of_measurable (λs hs, ∫⁻ a in s, f a ∂μ) (by simp) (λ s hs hd, lintegral_Union hs hd _) @[simp] lemma with_density_apply (f : α → ℝ≥0∞) {s : set α} (hs : measurable_set s) : μ.with_density f s = ∫⁻ a in s, f a ∂μ := measure.of_measurable_apply s hs end lintegral end measure_theory open measure_theory measure_theory.simple_func /-- To prove something for an arbitrary measurable function into `ℝ≥0∞`, it suffices to show that the property holds for (multiples of) characteristic functions and is closed under addition and supremum of increasing sequences of functions. It is possible to make the hypotheses in the induction steps a bit stronger, and such conditions can be added once we need them (for example in `h_add` it is only necessary to consider the sum of a simple function with a multiple of a characteristic function and that the intersection of their images is a subset of `{0}`. -/ @[elab_as_eliminator] theorem measurable.ennreal_induction {α} [measurable_space α] {P : (α → ℝ≥0∞) → Prop} (h_ind : ∀ (c : ℝ≥0∞) ⦃s⦄, measurable_set s → P (indicator s (λ _, c))) (h_add : ∀ ⦃f g : α → ℝ≥0∞⦄, disjoint (support f) (support g) → measurable f → measurable g → P f → P g → P (f + g)) (h_supr : ∀ ⦃f : ℕ → α → ℝ≥0∞⦄ (hf : ∀n, measurable (f n)) (h_mono : monotone f) (hP : ∀ n, P (f n)), P (λ x, ⨆ n, f n x)) ⦃f : α → ℝ≥0∞⦄ (hf : measurable f) : P f := begin convert h_supr (λ n, (eapprox f n).measurable) (monotone_eapprox f) _, { ext1 x, rw [supr_eapprox_apply f hf] }, { exact λ n, simple_func.induction (λ c s hs, h_ind c hs) (λ f g hfg hf hg, h_add hfg f.measurable g.measurable hf hg) (eapprox f n) } end namespace measure_theory /-- This is Exercise 1.2.1 from [tao2010]. It allows you to express integration of a measurable function with respect to `(μ.with_density f)` as an integral with respect to `μ`, called the base measure. `μ` is often the Lebesgue measure, and in this circumstance `f` is the probability density function, and `(μ.with_density f)` represents any continuous random variable as a probability measure, such as the uniform distribution between 0 and 1, the Gaussian distribution, the exponential distribution, the Beta distribution, or the Cauchy distribution (see Section 2.4 of [wasserman2004]). Thus, this method shows how to one can calculate expectations, variances, and other moments as a function of the probability density function. -/ lemma lintegral_with_density_eq_lintegral_mul {α} [measurable_space α] (μ : measure α) {f : α → ℝ≥0∞} (h_mf : measurable f) : ∀ {g : α → ℝ≥0∞}, measurable g → ∫⁻ a, g a ∂(μ.with_density f) = ∫⁻ a, (f * g) a ∂μ := begin apply measurable.ennreal_induction, { intros c s h_ms, simp [*, mul_comm _ c, ← indicator_mul_right], }, { intros g h h_univ h_mea_g h_mea_h h_ind_g h_ind_h, simp [mul_add, *, measurable.mul] }, { intros g h_mea_g h_mono_g h_ind, have : monotone (λ n a, f a * g n a) := λ m n hmn x, ennreal.mul_le_mul le_rfl (h_mono_g hmn x), simp [lintegral_supr, ennreal.mul_supr, h_mf.mul (h_mea_g _), *] } end end measure_theory
01b9b2b5e73594dbc80a9e7762a100dd17422b64
d1a52c3f208fa42c41df8278c3d280f075eb020c
/tests/lean/run/structuralEqns2.lean
a2d3d73e87f6fd6651fb438078d9e17685e33c55
[ "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
441
lean
import Lean open Lean open Lean.Meta def tst (declName : Name) : MetaM Unit := do IO.println (← getEqnsFor? declName) def g (i j : Nat) : Nat := if i < 5 then 0 else match j with | Nat.zero => 1 | Nat.succ j => g i j #eval tst ``g #check g.eq_1 #check g.eq_2 #check g.eq_3 def h (i j : Nat) : Nat := let z := match j with | Nat.zero => 1 | Nat.succ j => h i j z + z #eval tst ``h #check h.eq_1 #check h.eq_2
bae0fb319ca88c37e229afc2bbcdeabe2ec7fc33
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/simp_emeta_order.lean
7e5048f44486f74b18428fa93e02061d9386f873
[ "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
293
lean
class A (α : Type) class B (α : Type) [A α] def P (α : Type) : Prop := true @[simp] lemma foo (α : Type) [A α] [B α] : P α ↔ true := iff.rfl -- should work example (α : Type) [A α] [B α] : P α ↔ id true := begin simp only [foo], guard_target true ↔ id true, refl end
670933756a1dff53a7d1ff9677abaa843beb4d2a
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/nat/multiplicity.lean
07017e41ef9ce19c1bedbdc91ad1a3c7a0ec3566
[]
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
4,806
lean
/- Copyright (c) 2019 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.nat.bitwise import Mathlib.data.nat.parity import Mathlib.ring_theory.int.basic import Mathlib.algebra.big_operators.intervals import Mathlib.PostPort namespace Mathlib /-! # Natural number multiplicity This file contains lemmas about the multiplicity function (the maximum prime power divding a number). # Main results There are natural number versions of some basic lemmas about multiplicity. There are also lemmas about the multiplicity of primes in factorials and in binomial coefficients. -/ namespace nat /-- The multiplicity of a divisor `m` of `n`, is the cardinality of the set of positive natural numbers `i` such that `p ^ i` divides `n`. The set is expressed by filtering `Ico 1 b` where `b` is any bound at least `n` -/ theorem multiplicity_eq_card_pow_dvd {m : ℕ} {n : ℕ} {b : ℕ} (hm1 : m ≠ 1) (hn0 : 0 < n) (hb : n ≤ b) : multiplicity m n = ↑(finset.card (finset.filter (fun (i : ℕ) => m ^ i ∣ n) (finset.Ico 1 b))) := sorry namespace prime theorem multiplicity_one {p : ℕ} (hp : prime p) : multiplicity p 1 = 0 := multiplicity.one_right (prime.not_unit (iff.mp prime_iff_prime hp)) theorem multiplicity_mul {p : ℕ} {m : ℕ} {n : ℕ} (hp : prime p) : multiplicity p (m * n) = multiplicity p m + multiplicity p n := multiplicity.mul (iff.mp prime_iff_prime hp) theorem multiplicity_pow {p : ℕ} {m : ℕ} {n : ℕ} (hp : prime p) : multiplicity p (m ^ n) = n •ℕ multiplicity p m := multiplicity.pow (iff.mp prime_iff_prime hp) theorem multiplicity_self {p : ℕ} (hp : prime p) : multiplicity p p = 1 := multiplicity.multiplicity_self (prime.not_unit (iff.mp prime_iff_prime hp)) (ne_zero hp) theorem multiplicity_pow_self {p : ℕ} {n : ℕ} (hp : prime p) : multiplicity p (p ^ n) = ↑n := multiplicity.multiplicity_pow_self (ne_zero hp) (prime.not_unit (iff.mp prime_iff_prime hp)) n /-- The multiplicity of a prime in `n!` is the sum of the quotients `n / p ^ i`. This sum is expressed over the set `Ico 1 b` where `b` is any bound at least `n` -/ theorem multiplicity_factorial {p : ℕ} (hp : prime p) {n : ℕ} {b : ℕ} : n ≤ b → multiplicity p (factorial n) = ↑(finset.sum (finset.Ico 1 b) fun (i : ℕ) => n / p ^ i) := sorry /-- The multiplicity of `p` in `(p(n+1))!` is one more than the sum of the multiplicities of `p` in `(p * n)!` and `n + 1`. -/ theorem multiplicity_factorial_mul_succ {n : ℕ} {p : ℕ} (hp : prime p) : multiplicity p (factorial (p * (n + 1))) = multiplicity p (factorial (p * n)) + multiplicity p (n + 1) + 1 := sorry /-- The multiplicity of `p` in `(pn)!` is `n` more than that of `n!`. -/ theorem multiplicity_factorial_mul {n : ℕ} {p : ℕ} (hp : prime p) : multiplicity p (factorial (p * n)) = multiplicity p (factorial n) + ↑n := sorry /-- A prime power divides `n!` iff it is at most the sum of the quotients `n / p ^ i`. This sum is expressed over the set `Ico 1 b` where `b` is any bound at least `n` -/ theorem pow_dvd_factorial_iff {p : ℕ} {n : ℕ} {r : ℕ} {b : ℕ} (hp : prime p) (hbn : n ≤ b) : p ^ r ∣ factorial n ↔ r ≤ finset.sum (finset.Ico 1 b) fun (i : ℕ) => n / p ^ i := sorry theorem multiplicity_choose_aux {p : ℕ} {n : ℕ} {b : ℕ} {k : ℕ} (hp : prime p) (hkn : k ≤ n) : (finset.sum (finset.Ico 1 b) fun (i : ℕ) => n / p ^ i) = ((finset.sum (finset.Ico 1 b) fun (i : ℕ) => k / p ^ i) + finset.sum (finset.Ico 1 b) fun (i : ℕ) => (n - k) / p ^ i) + finset.card (finset.filter (fun (i : ℕ) => p ^ i ≤ k % p ^ i + (n - k) % p ^ i) (finset.Ico 1 b)) := sorry /-- The multiplity of `p` in `choose n k` is the number of carries when `k` and `n - k` are added in base `p`. The set is expressed by filtering `Ico 1 b` where `b` is any bound at least `n`. -/ theorem multiplicity_choose {p : ℕ} {n : ℕ} {k : ℕ} {b : ℕ} (hp : prime p) (hkn : k ≤ n) (hnb : n ≤ b) : multiplicity p (choose n k) = ↑(finset.card (finset.filter (fun (i : ℕ) => p ^ i ≤ k % p ^ i + (n - k) % p ^ i) (finset.Ico 1 b))) := sorry /-- A lower bound on the multiplicity of `p` in `choose n k`. -/ theorem multiplicity_le_multiplicity_choose_add {p : ℕ} (hp : prime p) (n : ℕ) (k : ℕ) : multiplicity p n ≤ multiplicity p (choose n k) + multiplicity p k := sorry theorem multiplicity_choose_prime_pow {p : ℕ} {n : ℕ} {k : ℕ} (hp : prime p) (hkn : k ≤ p ^ n) (hk0 : 0 < k) : multiplicity p (choose (p ^ n) k) + multiplicity p k = ↑n := sorry end prime theorem multiplicity_two_factorial_lt {n : ℕ} (h : n ≠ 0) : multiplicity (bit0 1) (factorial n) < ↑n := sorry
8f47f2b1b16d40b12938a9a66fe5bb00cc802e59
59e5eb5b0b465111c363c967363a954891f7de2e
/Leancode/fptp/src/list_lemma.lean
04dbb9f4c24f71a216e11f98d9d8720511a1d2f3
[ "Apache-2.0" ]
permissive
mukeshtiwari/formal-fptp
1987fb35e5594ea047b1b2f7e36a804dece6272f
883a6a245cfdce7af0fc3cc2c3769281fe60afa0
refs/heads/master
1,650,002,177,929
1,585,742,596,000
1,585,742,596,000
249,091,342
0
0
null
null
null
null
UTF-8
Lean
false
false
663
lean
import tactic.find tactic.omega lemma zip_with_len_l {α β γ : Type*} {l₁ : list α} {l₂ : list β} {f : α → β → γ} (h : list.length l₁ = list.length l₂) : list.length (list.zip_with f l₁ l₂) = list.length l₁ := begin induction l₁ with x xs ih generalizing l₂, {simp [list.zip_with]}, { cases l₂ with y ys, {injection h}, {simp only [list.zip_with, list.length], finish} } end lemma map_with_len_l {α β : Type*} {l₁ : list α} {f : α → β} : list.length (list.map f l₁) = list.length l₁ := begin induction l₁ with x xs ih, {simp [list.map]}, {simp [list.map]}, end
4d1974fa343b3c0178f36a88cb894acb9615b4d8
29cc89d6158dd3b90acbdbcab4d2c7eb9a7dbf0f
/4.1/41_exercise_solution.lean
c4030442648c303ddc23b125ff75c1704e6d6c6d
[]
no_license
KjellZijlemaker/Logical_Verification_VU
ced0ba95316a30e3c94ba8eebd58ea004fa6f53b
4578b93bf1615466996157bb333c84122b201d99
refs/heads/master
1,585,966,086,108
1,549,187,704,000
1,549,187,704,000
155,690,284
0
0
null
null
null
null
UTF-8
Lean
false
false
3,710
lean
/- Exercise 4.1: Mathematics — Foundation -/ /- Question 1: Hilbert choice -/ /- The following command enables noncomputable decidability on every `Prop`. The `priority 0` attribute ensures this is used only when necessary; otherwise, it would make some computable definitions noncomputable for Lean. -/ local attribute [instance, priority 0] classical.prop_decidable /- 1.1. Prove the following lemma. -/ lemma exists_minimal_arg.aux (f : ℕ → ℕ) : ∀x n, f n = x → ∃n, ∀i, f n ≤ f i | x n eq := begin -- this works thanks to `classical.prop_decidable` by_cases (∃n', f n' < x), { cases h with n' h, exact exists_minimal_arg.aux _ n' rfl }, { have h' : ∀n', x ≤ f n', { intro n', apply le_of_not_gt _, intro h', exact h ⟨n', h'⟩ }, apply exists.intro n, rw eq, exact h' } end /- Now this interesting lemma falls off: -/ lemma exists_minimal_arg (f : ℕ → ℕ) : ∃n : ℕ, ∀i : ℕ, f n ≤ f i := exists_minimal_arg.aux f _ 0 rfl /- 1.2. Use what you learned in the lecture notes to define the following function, which returns the (or an) index of the minimal element in `f`'s image. -/ noncomputable def minimal_arg (f : ℕ → ℕ) : ℕ := classical.some (exists_minimal_arg f) /- 1.3. Prove the following characteristic lemma about your definition. -/ lemma minimal_arg_spec (f : ℕ → ℕ) : ∀i : ℕ, f (minimal_arg f) ≤ f i := classical.some_spec (exists_minimal_arg f) /- Question 2: Integers as quotients -/ /- First, we repeat some material from the lecture. As usual, ignore the `sorry`. -/ namespace exercise lemma reflexive1 {α: Type} (x: (ℕ × ℕ)) : x.1 + x.2 = x.1 + x.2:= by refl lemma symmetry1 {α: Type} (x y: (ℕ × ℕ)) (h1: x.1 + y.2 = y.1 + x.2) : y.1 + x.2 = x.1 + y.2 := by rw[h1] lemma transitive1 {α: Type} (x y z: (ℕ × ℕ)) (h1: y.1 + x.2 = x.1 + y.2) (h2: z.1 + y.2 = y.1 + z.2) : (x.1 + z.2) + y.2 = (x.1 + y.2) + z.2 := begin calc (x.fst + z.snd) + y.snd = (x.fst + y.snd) + z.snd : by ac_refl end lemma reflexivelist {α: Type} (xs: list α) : ∀x, x∈xs ↔ x∈xs := begin intro x, apply iff.intro, intro h, assumption, intro h, assumption end lemma symmetriclist {α: Type} (xs ys: list α) : ∀x, x∈xs ↔ x∈ys → x∈ys ↔ x∈xs := begin intro x, apply iff.intro, intro h, cases h, apply h_mpr, intro s, assumption, intro h, apply iff.intro, intros a b, assumption, intro a, assumption end lemma transitivelist {α: Type} (xs ys zs: list α) : ∀x, x∈xs ↔ x∈ys → x∈ys ↔ x∈zs → x ∈xs ↔ x∈zs := begin intro x, apply iff.intro, intro h, cases h, simp[symmetriclist] at h_mp, simp[symmetriclist] at h_mpr, sorry instance fin_set.rel (α: Type) : setoid (list α) :={ r :=λxs ys,∀x, x∈xs↔x∈ys, iseqv := ⟨ (assume xs, by simp*), ⟩ } instance int.rel : setoid (ℕ × ℕ) := { r := λa b, a.1 + b.2 = b.1 + a.2, iseqv := ⟨ (assume a, reflexive1 ), (assume a b eq, symmetry1), (assume a b c d e , transitive1) ⟩ } @[simp] lemma rel_iff (a b : ℕ × ℕ) : a ≈ b ↔ a.1 + b.2 = b.1 + a.2 := iff.rfl def int : Type := quotient int.rel /- 2.1. Define negation using `quotient.lift_on`. -/ def neg (a : int) : int := quotient.lift_on a (λpn, ⟦(pn.2, pn.1)⟧) begin intros a b h, cases a, cases b, apply quotient.sound, simp at h ⊢, rw [h] end /- 2.2. Prove the following lemmas. -/ lemma neg_mk (p n : ℕ) : neg ⟦(p, n)⟧ = ⟦(n, p)⟧ := by refl lemma neg_neg (a : int) : neg (neg a) = a := begin refine quotient.induction_on a _, intro a, cases a, simp [neg_mk] end end exercise
7bdc768df5c7cbe65c3e1a87afc65fb3ae0334a1
618003631150032a5676f229d13a079ac875ff77
/src/tactic/converter/binders.lean
4b2f1524e3c2082b72bf04a1bac2e13903f7750a
[ "Apache-2.0" ]
permissive
awainverse/mathlib
939b68c8486df66cfda64d327ad3d9165248c777
ea76bd8f3ca0a8bf0a166a06a475b10663dec44a
refs/heads/master
1,659,592,962,036
1,590,987,592,000
1,590,987,592,000
268,436,019
1
0
Apache-2.0
1,590,990,500,000
1,590,990,500,000
null
UTF-8
Lean
false
false
7,438
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 Binder elimination -/ import order namespace old_conv open tactic monad meta instance : monad_fail old_conv := { fail := λ α s, (λr e, tactic.fail (to_fmt s) : old_conv α), ..old_conv.monad } meta instance : has_monad_lift tactic old_conv := ⟨λα, lift_tactic⟩ meta instance (α : Type) : has_coe (tactic α) (old_conv α) := ⟨monad_lift⟩ meta def current_relation : old_conv name := λr lhs, return ⟨r, lhs, none⟩ meta def head_beta : old_conv unit := λ r e, do n ← tactic.head_beta e, return ⟨(), n, none⟩ /- congr should forward data! -/ meta def congr_arg : old_conv unit → old_conv unit := congr_core (return ()) meta def congr_fun : old_conv unit → old_conv unit := λc, congr_core c (return ()) meta def congr_rule (congr : expr) (cs : list (list expr → old_conv unit)) : old_conv unit := λr lhs, do meta_rhs ← infer_type lhs >>= mk_meta_var, -- is maybe overly restricted for `heq` t ← mk_app r [lhs, meta_rhs], ((), meta_pr) ← solve_aux t (do apply congr, focus $ cs.map $ λc, (do xs ← intros, conversion (head_beta >> c xs)), done), rhs ← instantiate_mvars meta_rhs, pr ← instantiate_mvars meta_pr, return ⟨(), rhs, some pr⟩ meta def congr_binder (congr : name) (cs : expr → old_conv unit) : old_conv unit := do e ← mk_const congr, congr_rule e [λbs, do [b] ← return bs, cs b] meta def funext' : (expr → old_conv unit) → old_conv unit := congr_binder ``_root_.funext meta def propext' {α : Type} (c : old_conv α) : old_conv α := λr lhs, (do guard (r = `iff), c r lhs) <|> (do guard (r = `eq), ⟨res, rhs, pr⟩ ← c `iff lhs, match pr with | some pr := return ⟨res, rhs, (expr.const `propext [] : expr) lhs rhs pr⟩ | none := return ⟨res, rhs, none⟩ end) meta def apply (pr : expr) : old_conv unit := λ r e, do sl ← simp_lemmas.mk.add pr, apply_lemmas sl r e meta def applyc (n : name) : old_conv unit := λ r e, do sl ← simp_lemmas.mk.add_simp n, apply_lemmas sl r e meta def apply' (n : name) : old_conv unit := do e ← mk_const n, congr_rule e [] end old_conv open expr tactic old_conv /- Binder elimination: We assume a binder `B : p → Π (α : Sort u), (α → t) → t`, where `t` is a type depending on `p`. Examples: ∃: there is no `p` and `t` is `Prop`. ⨅, ⨆: here p is `β` and `[complete_lattice β]`, `p` is `β` Problem: ∀x, _ should be a binder, but is not a constant! Provide a mechanism to rewrite: B (x : α) ..x.. (h : x = t), p x = B ..x/t.., p t Here ..x.. are binders, maybe also some constants which provide commutativity rules with `B`. -/ meta structure binder_eq_elim := (match_binder : expr → tactic (expr × expr)) -- returns the bound type and body (adapt_rel : old_conv unit → old_conv unit) -- optionally adapt `eq` to `iff` (apply_comm : old_conv unit) -- apply commutativity rule (apply_congr : (expr → old_conv unit) → old_conv unit) -- apply congruence rule (apply_elim_eq : old_conv unit) -- (B (x : β) (h : x = t), s x) = s t meta def binder_eq_elim.check_eq (b : binder_eq_elim) (x : expr) : expr → tactic unit | `(@eq %%β %%l %%r) := guard ((l = x ∧ ¬ x.occurs r) ∨ (r = x ∧ ¬ x.occurs l)) | _ := fail "no match" meta def binder_eq_elim.pull (b : binder_eq_elim) (x : expr) : old_conv unit := do (β, f) ← lhs >>= (lift_tactic ∘ b.match_binder), guard (¬ x.occurs β) <|> b.check_eq x β <|> (do b.apply_congr $ λx, binder_eq_elim.pull, b.apply_comm) meta def binder_eq_elim.push (b : binder_eq_elim) : old_conv unit := b.apply_elim_eq <|> (do b.apply_comm, b.apply_congr $ λx, binder_eq_elim.push) <|> (do b.apply_congr $ b.pull, binder_eq_elim.push) meta def binder_eq_elim.check (b : binder_eq_elim) (x : expr) : expr → tactic unit | e := do (β, f) ← b.match_binder e, b.check_eq x β <|> (do (lam n bi d bd) ← return f, x ← mk_local' n bi d, binder_eq_elim.check $ bd.instantiate_var x) meta def binder_eq_elim.old_conv (b : binder_eq_elim) : old_conv unit := do (β, f) ← lhs >>= (lift_tactic ∘ b.match_binder), (lam n bi d bd) ← return f, x ← mk_local' n bi d, b.check x (bd.instantiate_var x), b.adapt_rel b.push theorem {u v} exists_elim_eq_left {α : Sort u} (a : α) (p : Π(a':α), a' = a → Prop) : (∃(a':α)(h : a' = a), p a' h) ↔ p a rfl := ⟨λ⟨a', ⟨h, p_h⟩⟩, match a', h, p_h with ._, rfl, h := h end, λh, ⟨a, rfl, h⟩⟩ theorem {u v} exists_elim_eq_right {α : Sort u} (a : α) (p : Π(a':α), a = a' → Prop) : (∃(a':α)(h : a = a'), p a' h) ↔ p a rfl := ⟨λ⟨a', ⟨h, p_h⟩⟩, match a', h, p_h with ._, rfl, h := h end, λh, ⟨a, rfl, h⟩⟩ meta def exists_eq_elim : binder_eq_elim := { match_binder := λe, (do `(@Exists %%β %%f) ← return e, return (β, f)), adapt_rel := propext', apply_comm := applyc ``exists_comm, apply_congr := congr_binder ``exists_congr, apply_elim_eq := apply' ``exists_elim_eq_left <|> apply' ``exists_elim_eq_right } theorem {u v} forall_comm {α : Sort u} {β : Sort v} (p : α → β → Prop) : (∀a b, p a b) ↔ (∀b a, p a b) := ⟨assume h b a, h a b, assume h b a, h a b⟩ theorem {u v} forall_elim_eq_left {α : Sort u} (a : α) (p : Π(a':α), a' = a → Prop) : (∀(a':α)(h : a' = a), p a' h) ↔ p a rfl := ⟨λh, h a rfl, λh a' h_eq, match a', h_eq with ._, rfl := h end⟩ theorem {u v} forall_elim_eq_right {α : Sort u} (a : α) (p : Π(a':α), a = a' → Prop) : (∀(a':α)(h : a = a'), p a' h) ↔ p a rfl := ⟨λh, h a rfl, λh a' h_eq, match a', h_eq with ._, rfl := h end⟩ meta def forall_eq_elim : binder_eq_elim := { match_binder := λe, (do (expr.pi n bi d bd) ← return e, return (d, expr.lam n bi d bd)), adapt_rel := propext', apply_comm := applyc ``forall_comm, apply_congr := congr_binder ``forall_congr, apply_elim_eq := apply' ``forall_elim_eq_left <|> apply' ``forall_elim_eq_right } meta def supr_eq_elim : binder_eq_elim := { match_binder := λe, (do `(@supr %%α %%β %%cl %%f) ← return e, return (β, f)), adapt_rel := λc, (do r ← current_relation, guard (r = `eq), c), apply_comm := applyc ``supr_comm, apply_congr := congr_arg ∘ funext', apply_elim_eq := applyc ``supr_supr_eq_left <|> applyc ``supr_supr_eq_right } meta def infi_eq_elim : binder_eq_elim := { match_binder := λe, (do `(@infi %%α %%β %%cl %%f) ← return e, return (β, f)), adapt_rel := λc, (do r ← current_relation, guard (r = `eq), c), apply_comm := applyc ``infi_comm, apply_congr := congr_arg ∘ funext', apply_elim_eq := applyc ``infi_infi_eq_left <|> applyc ``infi_infi_eq_right } universes u v w w₂ variables {α : Type u} {β : Type v} {ι : Sort w} {ι₂ : Sort w₂} {s t : set α} {a : α} section variables [complete_lattice α] example {s : set β} {f : β → α} : Inf (set.image f s) = (⨅ a ∈ s, f a) := begin simp [Inf_eq_infi, infi_and], conversion infi_eq_elim.old_conv, end example {s : set β} {f : β → α} : Sup (set.image f s) = (⨆ a ∈ s, f a) := begin simp [Sup_eq_supr, supr_and], conversion supr_eq_elim.old_conv, end end
8dfb25d4fc9cf3faf57c8ff24709b53a150ba9d9
ec62863c729b7eedee77b86d974f2c529fa79d25
/24/b.lean
02033ca3861260d6870726f5a782667339503d44
[]
no_license
rwbarton/advent-of-lean-4
2ac9b17ba708f66051e3d8cd694b0249bc433b65
417c7e2718253ba7148c0279fcb251b6fc291477
refs/heads/main
1,675,917,092,057
1,609,864,581,000
1,609,864,581,000
317,700,289
24
0
null
null
null
null
UTF-8
Lean
false
false
1,453
lean
import Std.Data.HashSet import Std.Data.HashMap open Std instance : Hashable Int where hash n := hash n.toNat @[reducible] def Hex := Int × Int def Hex.x : Hex → Int := Prod.fst def Hex.y : Hex → Int := Prod.snd instance : Add Hex where add h₁ h₂ := ⟨h₁.x + h₂.x, h₁.y + h₂.y⟩ def parseLine : List Char → Hex | [] => ⟨0, 0⟩ | 'e' :: rest => ⟨2, 0⟩ + parseLine rest | 'n' :: 'e' :: rest => ⟨1, 1⟩ + parseLine rest | 's' :: 'e' :: rest => ⟨1, -1⟩ + parseLine rest | 'w' :: rest => ⟨-2, 0⟩ + parseLine rest | 'n' :: 'w' :: rest => ⟨-1, 1⟩ + parseLine rest | 's' :: 'w' :: rest => ⟨-1, -1⟩ + parseLine rest | _ => panic! "invalid parse" def adj : List Hex := [⟨2, 0⟩, ⟨1, 1⟩, ⟨1, -1⟩, ⟨-2, 0⟩, ⟨-1, 1⟩, ⟨-1, -1⟩] def evolve (st : HashSet Hex) : HashSet Hex := do let mut ns : HashMap Hex Nat := HashMap.empty for h in st.toArray do for a in adj do ns := ns.insert (h+a) (ns.findD (h+a) 0 + 1) let mut st' : HashSet Hex := HashSet.empty for ⟨h, c⟩ in ns.toArray do let cond := if st.contains h then c = 1 ∨ c = 2 else c = 2 if cond then st' := st'.insert h st' def main : IO Unit := do let input ← IO.FS.lines "a.in" let mut black := HashSet.empty for h in input.map (parseLine ∘ String.toList) do if black.contains h then black := black.erase h else black := black.insert h let final := Nat.repeat evolve 100 black IO.print s!"{final.size}\n"
3b969d3d7bdcd7bf97d5be4dbc41dd3c6ae08cf7
56762daf61566a2baf390b5d77988c29c75187e3
/src/addition.lean
4d4b47c87b90d189d63f2d6c01de90663e8ffbb3
[]
no_license
Nicknamen/lie_group
de173ce5f1ffccb945ba05dca23ff27daef0e3b4
e0d5c4f859654e3dea092702f1320c3c72a49983
refs/heads/master
1,674,937,428,196
1,607,213,423,000
1,607,213,423,000
275,196,635
0
0
null
null
null
null
UTF-8
Lean
false
false
178
lean
import tactic.default theorem addition_of_natural_numbers_is_commutative (m n : ℕ) : m + n = n + m := begin induction n with d hd, { ring, }, { ring, } end
5eba72c32d9b3674ded6f07e93aa69cffd5a6558
26bff4ed296b8373c92b6b025f5d60cdf02104b9
/tests/lean/run/beginend3.lean
184857aae284493604f7d829d35604fc15f381bc
[ "Apache-2.0" ]
permissive
guiquanz/lean
b8a878ea24f237b84b0e6f6be2f300e8bf028229
242f8ba0486860e53e257c443e965a82ee342db3
refs/heads/master
1,526,680,092,098
1,427,492,833,000
1,427,493,281,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
272
lean
import logic open tactic theorem foo (A : Type) (a b c : A) : a = b → b = c → a = c ∧ c = a := begin intros (Hab, Hbc), apply and.intro, apply eq.trans, rotate 2, apply eq.trans, apply (eq.symm Hbc), apply (eq.symm Hab), apply Hab, apply Hbc, end
e9ca24db8082b08677257bbbaca577adbc8b8be9
957a80ea22c5abb4f4670b250d55534d9db99108
/tests/lean/run/eq_cases_on.lean
de9b535a30bef19d184de3c33609c1fe313f6d32
[ "Apache-2.0" ]
permissive
GaloisInc/lean
aa1e64d604051e602fcf4610061314b9a37ab8cd
f1ec117a24459b59c6ff9e56a1d09d9e9e60a6c0
refs/heads/master
1,592,202,909,807
1,504,624,387,000
1,504,624,387,000
75,319,626
2
1
Apache-2.0
1,539,290,164,000
1,480,616,104,000
C++
UTF-8
Lean
false
false
240
lean
def g {n m : nat} (v : array nat (n + m)) : array nat (m + n) := eq.rec_on (add_comm n m) v -- Worked before def f {n m : nat} (v : array nat (n + m)) : array nat (m + n) := eq.cases_on (add_comm n m) v -- eq.cases_on was not being erased
ed54316de753e0f57cb7be2f6536d1175d96e75b
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/isabelle.lean
ebdb0d5987e2fcee38e20a22a5461c79c90230d1
[ "Apache-2.0" ]
permissive
leanprover-community/lean
12b87f69d92e614daea8bcc9d4de9a9ace089d0e
cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0
refs/heads/master
1,687,508,156,644
1,684,951,104,000
1,684,951,104,000
169,960,991
457
107
Apache-2.0
1,686,744,372,000
1,549,790,268,000
C++
UTF-8
Lean
false
false
2,996
lean
/- Isabelle style tactics. This test is based on a file created by Gabriel Ebner. -/ universe variables u inductive lazy_list (α : Type u) : Type u | nil {} : lazy_list | cons : α → thunk (lazy_list) → lazy_list namespace lazy_list variables {α β : Type u} def singleton : α → lazy_list α | a := cons a nil def append : lazy_list α → thunk (lazy_list α) → lazy_list α | nil l := l () | (cons h t) l := cons h (@append (t ()) l) def join : lazy_list (lazy_list α) → lazy_list α | nil := nil | (cons h t) := append h (join (t ())) def map (f : α → β) : lazy_list α → lazy_list β | nil := nil | (cons h t) := cons (f h) (map (t ())) def for (l : lazy_list α) (f : α → β) : lazy_list β := map f l def of_list : list α → lazy_list α | [] := nil | (h::t) := cons h (of_list t) end lazy_list meta def lazy_tactic (α : Type u) := tactic_state → lazy_list (α × tactic_state) namespace lazy_tactic open lazy_list meta def of_tactic {α : Type u} (t : tactic α) : lazy_tactic α := λ s, match t s with | result.success a new_s := lazy_list.singleton (a, new_s) | result.exception f e s := lazy_list.nil end meta instance {α : Type} : has_coe (tactic α) (lazy_tactic α) := ⟨of_tactic⟩ protected meta def return {α} (a : α) : lazy_tactic α := λ s, lazy_list.singleton (a, s) protected meta def bind {α β} : lazy_tactic α → (α → lazy_tactic β) → lazy_tactic β := λ t₁ t₂ s, join (for (t₁ s) (λ ⟨a, new_s⟩, t₂ a new_s)) protected meta def orelse {α} (t₁ t₂ : lazy_tactic α) : lazy_tactic α := λ s, append (t₁ s) (t₂ s) protected meta def failure {α} : lazy_tactic α := λ s, nil meta instance : monad lazy_tactic := monad_from_pure_bind @lazy_tactic.return @lazy_tactic.bind meta instance : alternative lazy_tactic := { lazy_tactic.monad with failure := @lazy_tactic.failure, orelse := @lazy_tactic.orelse } meta def choose {α} (xs : list α) : lazy_tactic α := λ s, of_list $ xs^.map (λ a, (a, s)) protected meta def run {α} (t : lazy_tactic α) : tactic α := λ s, match t s with | nil := tactic.failed s | cons (a, new_s) ss := result.success a new_s end open tactic private meta def try_constructors : list name → lazy_tactic unit | [] := failure | (c::cs) := (mk_const c >>= apply >> skip : tactic unit) <|> try_constructors cs /- Backtracking version of constructor -/ meta def constructor : lazy_tactic unit := do t ← target, cs ← get_constructors_for t, try_constructors cs end lazy_tactic open lazy_tactic example (p q : Prop) : q → p ∨ q := by lazy_tactic.run $ do tactic.intros, constructor, tactic.trace_state, tactic.assumption meta def naive_instantiation : lazy_tactic unit := let vals := [``(1),``(2),``(3)] in do x ← choose vals, y ← choose vals, e ← tactic.to_expr ``(nat.add_comm %%x %%y), tactic.trace e, tactic.exact e lemma ex : 1 + 3 = 3 + 1 := by naive_instantiation^.run
48bfbf9ad0134aff1511295162b47fcb3a944924
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/lake/examples/targets/src/Foo.lean
7bf9513b39691a4f52f0384c533695667844977f
[ "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
32
lean
import Foo.Bar def foo := "foo"
8f38c7d094a208f56e240f126a113492d662ad9a
74924b1fe80b8f61262cefcfc0cd4d96135c8731
/src/data/real/basic.lean
b729bd98eb0bc24d3fd1539f1b1144a09c864761
[ "Apache-2.0" ]
permissive
101damnations/mathlib
0938b3806a09032d8716d3642cbab65db7688c23
900c53ae6d5e3f8cc47953363479593e8debc4d8
refs/heads/master
1,593,832,305,164
1,565,631,735,000
1,565,631,735,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
27,584
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Floris van Doorn The (classical) real numbers ℝ. This is a direct construction from Cauchy sequences. -/ import order.conditionally_complete_lattice data.real.cau_seq_completion algebra.archimedean order.bounds def real := @cau_seq.completion.Cauchy ℚ _ _ _ abs _ notation `ℝ` := real namespace real open cau_seq cau_seq.completion variables {x y : ℝ} def of_rat (x : ℚ) : ℝ := of_rat x def mk (x : cau_seq ℚ abs) : ℝ := cau_seq.completion.mk x def comm_ring_aux : comm_ring ℝ := cau_seq.completion.comm_ring instance : comm_ring ℝ := { ..comm_ring_aux } /- Extra instances to short-circuit type class resolution -/ instance : ring ℝ := by apply_instance instance : comm_semiring ℝ := by apply_instance instance : semiring ℝ := by apply_instance instance : add_comm_group ℝ := by apply_instance instance : add_group ℝ := by apply_instance instance : add_comm_monoid ℝ := by apply_instance instance : add_monoid ℝ := by apply_instance instance : add_left_cancel_semigroup ℝ := by apply_instance instance : add_right_cancel_semigroup ℝ := by apply_instance instance : add_comm_semigroup ℝ := by apply_instance instance : add_semigroup ℝ := by apply_instance instance : comm_monoid ℝ := by apply_instance instance : monoid ℝ := by apply_instance instance : comm_semigroup ℝ := by apply_instance instance : semigroup ℝ := by apply_instance instance : inhabited ℝ := ⟨0⟩ theorem of_rat_sub (x y : ℚ) : of_rat (x - y) = of_rat x - of_rat y := congr_arg mk (const_sub _ _) instance : has_lt ℝ := ⟨λ x y, quotient.lift_on₂ x y (<) $ λ f₁ g₁ f₂ g₂ hf hg, propext $ ⟨λ h, lt_of_eq_of_lt (setoid.symm hf) (lt_of_lt_of_eq h hg), λ h, lt_of_eq_of_lt hf (lt_of_lt_of_eq h (setoid.symm hg))⟩⟩ @[simp] theorem mk_lt {f g : cau_seq ℚ abs} : mk f < mk g ↔ f < g := iff.rfl theorem mk_eq {f g : cau_seq ℚ abs} : mk f = mk g ↔ f ≈ g := mk_eq theorem quotient_mk_eq_mk (f : cau_seq ℚ abs) : ⟦f⟧ = mk f := rfl theorem mk_eq_mk {f : cau_seq ℚ abs} : cau_seq.completion.mk f = mk f := rfl @[simp] theorem mk_pos {f : cau_seq ℚ abs} : 0 < mk f ↔ pos f := iff_of_eq (congr_arg pos (sub_zero f)) protected def le (x y : ℝ) : Prop := x < y ∨ x = y instance : has_le ℝ := ⟨real.le⟩ @[simp] theorem mk_le {f g : cau_seq ℚ abs} : mk f ≤ mk g ↔ f ≤ g := or_congr iff.rfl quotient.eq theorem add_lt_add_iff_left {a b : ℝ} (c : ℝ) : c + a < c + b ↔ a < b := quotient.induction_on₃ a b c (λ f g h, iff_of_eq (congr_arg pos $ by rw add_sub_add_left_eq_sub)) instance : linear_order ℝ := { le := (≤), lt := (<), le_refl := λ a, or.inr rfl, le_trans := λ a b c, quotient.induction_on₃ a b c $ λ f g h, by simpa [quotient_mk_eq_mk] using le_trans, lt_iff_le_not_le := λ a b, quotient.induction_on₂ a b $ λ f g, by simpa [quotient_mk_eq_mk] using lt_iff_le_not_le, le_antisymm := λ a b, quotient.induction_on₂ a b $ λ f g, by simpa [mk_eq, quotient_mk_eq_mk] using @cau_seq.le_antisymm _ _ f g, le_total := λ a b, quotient.induction_on₂ a b $ λ f g, by simpa [quotient_mk_eq_mk] using le_total f g } instance : partial_order ℝ := by apply_instance instance : preorder ℝ := by apply_instance theorem of_rat_lt {x y : ℚ} : of_rat x < of_rat y ↔ x < y := const_lt protected theorem zero_lt_one : (0 : ℝ) < 1 := of_rat_lt.2 zero_lt_one protected theorem mul_pos {a b : ℝ} : 0 < a → 0 < b → 0 < a * b := quotient.induction_on₂ a b $ λ f g, show pos (f - 0) → pos (g - 0) → pos (f * g - 0), by simpa using cau_seq.mul_pos instance : linear_ordered_comm_ring ℝ := { add_le_add_left := λ a b h c, (le_iff_le_iff_lt_iff_lt.2 $ real.add_lt_add_iff_left c).2 h, zero_ne_one := ne_of_lt real.zero_lt_one, mul_nonneg := λ a b a0 b0, match a0, b0 with | or.inl a0, or.inl b0 := le_of_lt (real.mul_pos a0 b0) | or.inr a0, _ := by simp [a0.symm] | _, or.inr b0 := by simp [b0.symm] end, mul_pos := @real.mul_pos, zero_lt_one := real.zero_lt_one, add_lt_add_left := λ a b h c, (real.add_lt_add_iff_left c).2 h, ..real.comm_ring, ..real.linear_order } /- Extra instances to short-circuit type class resolution -/ 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 : ordered_comm_group ℝ := by apply_instance instance : ordered_cancel_comm_monoid ℝ := by apply_instance instance : ordered_comm_monoid ℝ := by apply_instance instance : domain ℝ := by apply_instance local attribute [instance] classical.prop_decidable noncomputable instance : discrete_linear_ordered_field ℝ := { decidable_le := by apply_instance, ..real.linear_ordered_comm_ring, ..real.domain, ..cau_seq.completion.discrete_field } /- Extra instances to short-circuit type class resolution -/ noncomputable instance : linear_ordered_field ℝ := by apply_instance noncomputable instance : decidable_linear_ordered_comm_ring ℝ := by apply_instance noncomputable instance : decidable_linear_ordered_semiring ℝ := by apply_instance noncomputable instance : decidable_linear_ordered_comm_group ℝ := by apply_instance noncomputable instance discrete_field : discrete_field ℝ := by apply_instance noncomputable instance : field ℝ := by apply_instance noncomputable instance : division_ring ℝ := by apply_instance noncomputable instance : integral_domain ℝ := by apply_instance noncomputable instance : nonzero_comm_ring ℝ := by apply_instance noncomputable instance : decidable_linear_order ℝ := by apply_instance noncomputable instance : lattice.distrib_lattice ℝ := by apply_instance noncomputable instance : lattice.lattice ℝ := by apply_instance noncomputable instance : lattice.semilattice_inf ℝ := by apply_instance noncomputable instance : lattice.semilattice_sup ℝ := by apply_instance noncomputable instance : lattice.has_inf ℝ := by apply_instance noncomputable instance : lattice.has_sup ℝ := by apply_instance lemma le_of_forall_epsilon_le {a b : real} (h : ∀ε, ε > 0 → a ≤ b + ε) : a ≤ b := le_of_forall_le_of_dense $ assume x hxb, calc a ≤ b + (x - b) : h (x-b) $ sub_pos.2 hxb ... = x : by rw [add_comm]; simp open rat @[simp] theorem of_rat_eq_cast : ∀ x : ℚ, of_rat x = x := eq_cast of_rat rfl of_rat_add of_rat_mul theorem le_mk_of_forall_le {f : cau_seq ℚ abs} : (∃ i, ∀ j ≥ i, x ≤ f j) → x ≤ mk f := quotient.induction_on x $ λ g h, le_of_not_lt $ λ ⟨K, K0, hK⟩, let ⟨i, H⟩ := exists_forall_ge_and h $ exists_forall_ge_and hK (f.cauchy₃ $ half_pos K0) in begin apply not_lt_of_le (H _ (le_refl _)).1, rw ← of_rat_eq_cast, refine ⟨_, half_pos K0, i, λ j ij, _⟩, have := add_le_add (H _ ij).2.1 (le_of_lt (abs_lt.1 $ (H _ (le_refl _)).2.2 _ ij).1), rwa [← sub_eq_add_neg, sub_self_div_two, sub_apply, sub_add_sub_cancel] at this end theorem mk_le_of_forall_le {f : cau_seq ℚ abs} {x : ℝ} : (∃ i, ∀ j ≥ i, (f j : ℝ) ≤ x) → mk f ≤ x | ⟨i, H⟩ := by rw [← neg_le_neg_iff, ← mk_eq_mk, mk_neg]; exact le_mk_of_forall_le ⟨i, λ j ij, by simp [H _ ij]⟩ theorem mk_near_of_forall_near {f : cau_seq ℚ abs} {x : ℝ} {ε : ℝ} (H : ∃ i, ∀ j ≥ i, abs ((f j : ℝ) - x) ≤ ε) : abs (mk f - x) ≤ ε := abs_sub_le_iff.2 ⟨sub_le_iff_le_add'.2 $ mk_le_of_forall_le $ H.imp $ λ i h j ij, sub_le_iff_le_add'.1 (abs_sub_le_iff.1 $ h j ij).1, sub_le.1 $ le_mk_of_forall_le $ H.imp $ λ i h j ij, sub_le.1 (abs_sub_le_iff.1 $ h j ij).2⟩ instance : archimedean ℝ := archimedean_iff_rat_le.2 $ λ x, quotient.induction_on x $ λ f, let ⟨M, M0, H⟩ := f.bounded' 0 in ⟨M, mk_le_of_forall_le ⟨0, λ i _, rat.cast_le.2 $ le_of_lt (abs_lt.1 (H i)).2⟩⟩ /- mark `real` irreducible in order to prevent `auto_cases` unfolding reals, since users rarely want to consider real numbers as Cauchy sequences. Marking `comm_ring_aux` `irreducible` is done to ensure that there are no problems with non definitionally equal instances, caused by making `real` irreducible-/ attribute [irreducible] real comm_ring_aux noncomputable instance : floor_ring ℝ := archimedean.floor_ring _ theorem is_cau_seq_iff_lift {f : ℕ → ℚ} : is_cau_seq abs f ↔ is_cau_seq abs (λ i, (f i : ℝ)) := ⟨λ H ε ε0, let ⟨δ, δ0, δε⟩ := exists_pos_rat_lt ε0 in (H _ δ0).imp $ λ i hi j ij, lt_trans (by simpa using (@rat.cast_lt ℝ _ _ _).2 (hi _ ij)) δε, λ H ε ε0, (H _ (rat.cast_pos.2 ε0)).imp $ λ i hi j ij, (@rat.cast_lt ℝ _ _ _).1 $ by simpa using hi _ ij⟩ theorem of_near (f : ℕ → ℚ) (x : ℝ) (h : ∀ ε > 0, ∃ i, ∀ j ≥ i, abs ((f j : ℝ) - x) < ε) : ∃ h', real.mk ⟨f, h'⟩ = x := ⟨is_cau_seq_iff_lift.2 (of_near _ (const abs x) h), sub_eq_zero.1 $ abs_eq_zero.1 $ eq_of_le_of_forall_le_of_dense (abs_nonneg _) $ λ ε ε0, mk_near_of_forall_near $ (h _ ε0).imp (λ i h j ij, le_of_lt (h j ij))⟩ theorem exists_floor (x : ℝ) : ∃ (ub : ℤ), (ub:ℝ) ≤ x ∧ ∀ (z : ℤ), (z:ℝ) ≤ x → z ≤ ub := int.exists_greatest_of_bdd (let ⟨n, hn⟩ := exists_int_gt x in ⟨n, λ z h', int.cast_le.1 $ le_trans h' $ le_of_lt hn⟩) (let ⟨n, hn⟩ := exists_int_lt x in ⟨n, le_of_lt hn⟩) theorem exists_sup (S : set ℝ) : (∃ x, x ∈ S) → (∃ x, ∀ y ∈ S, y ≤ x) → ∃ x, ∀ y, x ≤ y ↔ ∀ z ∈ S, z ≤ y | ⟨L, hL⟩ ⟨U, hU⟩ := begin choose f hf using begin refine λ d : ℕ, @int.exists_greatest_of_bdd (λ n, ∃ y ∈ S, (n:ℝ) ≤ y * d) _ _ _, { cases exists_int_gt U with k hk, refine ⟨k * d, λ z h, _⟩, rcases h with ⟨y, yS, hy⟩, refine int.cast_le.1 (le_trans hy _), simp, exact mul_le_mul_of_nonneg_right (le_trans (hU _ yS) (le_of_lt hk)) (nat.cast_nonneg _) }, { exact ⟨⌊L * d⌋, L, hL, floor_le _⟩ } end, have hf₁ : ∀ n > 0, ∃ y ∈ S, ((f n / n:ℚ):ℝ) ≤ y := λ n n0, let ⟨y, yS, hy⟩ := (hf n).1 in ⟨y, yS, by simpa using (div_le_iff ((nat.cast_pos.2 n0):((_:ℝ) < _))).2 hy⟩, have hf₂ : ∀ (n > 0) (y ∈ S), (y - (n:ℕ)⁻¹ : ℝ) < (f n / n:ℚ), { intros n n0 y yS, have := lt_of_lt_of_le (sub_one_lt_floor _) (int.cast_le.2 $ (hf n).2 _ ⟨y, yS, floor_le _⟩), simp [-sub_eq_add_neg], rwa [lt_div_iff ((nat.cast_pos.2 n0):((_:ℝ) < _)), sub_mul, _root_.inv_mul_cancel], exact ne_of_gt (nat.cast_pos.2 n0) }, suffices hg, let g : cau_seq ℚ abs := ⟨λ n, f n / n, hg⟩, refine ⟨mk g, λ y, ⟨λ h x xS, le_trans _ h, λ h, _⟩⟩, { refine le_of_forall_ge_of_dense (λ z xz, _), cases exists_nat_gt (x - z)⁻¹ with K hK, refine le_mk_of_forall_le ⟨K, λ n nK, _⟩, replace xz := sub_pos.2 xz, replace hK := le_trans (le_of_lt hK) (nat.cast_le.2 nK), have n0 : 0 < n := nat.cast_pos.1 (lt_of_lt_of_le (inv_pos xz) hK), refine le_trans _ (le_of_lt $ hf₂ _ n0 _ xS), rwa [le_sub, inv_le ((nat.cast_pos.2 n0):((_:ℝ) < _)) xz] }, { exact mk_le_of_forall_le ⟨1, λ n n1, let ⟨x, xS, hx⟩ := hf₁ _ n1 in le_trans hx (h _ xS)⟩ }, intros ε ε0, suffices : ∀ j k ≥ nat_ceil ε⁻¹, (f j / j - f k / k : ℚ) < ε, { refine ⟨_, λ j ij, abs_lt.2 ⟨_, this _ _ ij (le_refl _)⟩⟩, rw [neg_lt, neg_sub], exact this _ _ (le_refl _) ij }, intros j k ij ik, replace ij := le_trans (le_nat_ceil _) (nat.cast_le.2 ij), replace ik := le_trans (le_nat_ceil _) (nat.cast_le.2 ik), have j0 := nat.cast_pos.1 (lt_of_lt_of_le (inv_pos ε0) ij), have k0 := nat.cast_pos.1 (lt_of_lt_of_le (inv_pos ε0) ik), rcases hf₁ _ j0 with ⟨y, yS, hy⟩, refine lt_of_lt_of_le ((@rat.cast_lt ℝ _ _ _).1 _) ((inv_le ε0 (nat.cast_pos.2 k0)).1 ik), simpa using sub_lt_iff_lt_add'.2 (lt_of_le_of_lt hy $ sub_lt_iff_lt_add.1 $ hf₂ _ k0 _ yS) end noncomputable def Sup (S : set ℝ) : ℝ := if h : (∃ x, x ∈ S) ∧ (∃ x, ∀ y ∈ S, y ≤ x) then classical.some (exists_sup S h.1 h.2) else 0 theorem Sup_le (S : set ℝ) (h₁ : ∃ x, x ∈ S) (h₂ : ∃ x, ∀ y ∈ S, y ≤ x) {y} : Sup S ≤ y ↔ ∀ z ∈ S, z ≤ y := by simp [Sup, h₁, h₂]; exact classical.some_spec (exists_sup S h₁ h₂) y theorem lt_Sup (S : set ℝ) (h₁ : ∃ x, x ∈ S) (h₂ : ∃ x, ∀ y ∈ S, y ≤ x) {y} : y < Sup S ↔ ∃ z ∈ S, y < z := by simpa [not_forall] using not_congr (@Sup_le S h₁ h₂ y) theorem le_Sup (S : set ℝ) (h₂ : ∃ x, ∀ y ∈ S, y ≤ x) {x} (xS : x ∈ S) : x ≤ Sup S := (Sup_le S ⟨_, xS⟩ h₂).1 (le_refl _) _ xS theorem Sup_le_ub (S : set ℝ) (h₁ : ∃ x, x ∈ S) {ub} (h₂ : ∀ y ∈ S, y ≤ ub) : Sup S ≤ ub := (Sup_le S h₁ ⟨_, h₂⟩).2 h₂ protected lemma is_lub_Sup {s : set ℝ} {a b : ℝ} (ha : a ∈ s) (hb : b ∈ upper_bounds s) : is_lub s (Sup s) := ⟨λ x xs, real.le_Sup s ⟨_, hb⟩ xs, λ u h, real.Sup_le_ub _ ⟨_, ha⟩ h⟩ noncomputable def Inf (S : set ℝ) : ℝ := -Sup {x | -x ∈ S} theorem le_Inf (S : set ℝ) (h₁ : ∃ x, x ∈ S) (h₂ : ∃ x, ∀ y ∈ S, x ≤ y) {y} : y ≤ Inf S ↔ ∀ z ∈ S, y ≤ z := begin refine le_neg.trans ((Sup_le _ _ _).trans _), { cases h₁ with x xS, exact ⟨-x, by simp [xS]⟩ }, { cases h₂ with ub h, exact ⟨-ub, λ y hy, le_neg.1 $ h _ hy⟩ }, split; intros H z hz, { exact neg_le_neg_iff.1 (H _ $ by simp [hz]) }, { exact le_neg.2 (H _ hz) } end theorem Inf_lt (S : set ℝ) (h₁ : ∃ x, x ∈ S) (h₂ : ∃ x, ∀ y ∈ S, x ≤ y) {y} : Inf S < y ↔ ∃ z ∈ S, z < y := by simpa [not_forall] using not_congr (@le_Inf S h₁ h₂ y) theorem Inf_le (S : set ℝ) (h₂ : ∃ x, ∀ y ∈ S, x ≤ y) {x} (xS : x ∈ S) : Inf S ≤ x := (le_Inf S ⟨_, xS⟩ h₂).1 (le_refl _) _ xS theorem lb_le_Inf (S : set ℝ) (h₁ : ∃ x, x ∈ S) {lb} (h₂ : ∀ y ∈ S, lb ≤ y) : lb ≤ Inf S := (le_Inf S h₁ ⟨_, h₂⟩).2 h₂ open lattice noncomputable instance lattice : lattice ℝ := by apply_instance noncomputable instance : conditionally_complete_linear_order ℝ := { Sup := real.Sup, Inf := real.Inf, le_cSup := assume (s : set ℝ) (a : ℝ) (_ : bdd_above s) (_ : a ∈ s), show a ≤ Sup s, from le_Sup s ‹bdd_above s› ‹a ∈ s›, cSup_le := assume (s : set ℝ) (a : ℝ) (_ : s ≠ ∅) (H : ∀b∈s, b ≤ a), show Sup s ≤ a, from Sup_le_ub s (set.exists_mem_of_ne_empty ‹s ≠ ∅›) H, cInf_le := assume (s : set ℝ) (a : ℝ) (_ : bdd_below s) (_ : a ∈ s), show Inf s ≤ a, from Inf_le s ‹bdd_below s› ‹a ∈ s›, le_cInf := assume (s : set ℝ) (a : ℝ) (_ : s ≠ ∅) (H : ∀b∈s, a ≤ b), show a ≤ Inf s, from lb_le_Inf s (set.exists_mem_of_ne_empty ‹s ≠ ∅›) H, decidable_le := classical.dec_rel _, ..real.linear_order, ..real.lattice} theorem Sup_empty : lattice.Sup (∅ : set ℝ) = 0 := dif_neg $ by simp theorem Sup_of_not_bdd_above {s : set ℝ} (hs : ¬ bdd_above s) : lattice.Sup s = 0 := dif_neg $ assume h, hs h.2 theorem Sup_univ : real.Sup set.univ = 0 := real.Sup_of_not_bdd_above $ λ h, Exists.dcases_on h $ λ x h', not_le_of_lt (lt_add_one _) $ h' (x + 1) $ set.mem_univ _ theorem Inf_empty : lattice.Inf (∅ : set ℝ) = 0 := show Inf ∅ = 0, by simp [Inf]; exact Sup_empty theorem Inf_of_not_bdd_below {s : set ℝ} (hs : ¬ bdd_below s) : lattice.Inf s = 0 := have bdd_above {x | -x ∈ s} → bdd_below s, from assume ⟨b, hb⟩, ⟨-b, assume x hxs, neg_le.2 $ hb _ $ by simp [hxs]⟩, have ¬ bdd_above {x | -x ∈ s}, from mt this hs, neg_eq_zero.2 $ Sup_of_not_bdd_above $ this theorem cau_seq_converges (f : cau_seq ℝ abs) : ∃ x, f ≈ const abs x := begin let S := {x : ℝ | const abs x < f}, have lb : ∃ x, x ∈ S := exists_lt f, have ub' : ∀ x, f < const abs x → ∀ y ∈ S, y ≤ x := λ x h y yS, le_of_lt $ const_lt.1 $ cau_seq.lt_trans yS h, have ub : ∃ x, ∀ y ∈ S, y ≤ x := (exists_gt f).imp ub', refine ⟨Sup S, ((lt_total _ _).resolve_left (λ h, _)).resolve_right (λ h, _)⟩, { rcases h with ⟨ε, ε0, i, ih⟩, refine not_lt_of_le (Sup_le_ub S lb (ub' _ _)) ((sub_lt_self_iff _).2 (half_pos ε0)), refine ⟨_, half_pos ε0, i, λ j ij, _⟩, rw [sub_apply, const_apply, sub_right_comm, le_sub_iff_add_le, add_halves], exact ih _ ij }, { rcases h with ⟨ε, ε0, i, ih⟩, refine not_lt_of_le (le_Sup S ub _) ((lt_add_iff_pos_left _).2 (half_pos ε0)), refine ⟨_, half_pos ε0, i, λ j ij, _⟩, rw [sub_apply, const_apply, add_comm, ← sub_sub, le_sub_iff_add_le, add_halves], exact ih _ ij } end noncomputable instance : cau_seq.is_complete ℝ abs := ⟨cau_seq_converges⟩ theorem sqrt_exists : ∀ {x : ℝ}, 0 ≤ x → ∃ y, 0 ≤ y ∧ y * y = x := suffices H : ∀ {x : ℝ}, 0 < x → x ≤ 1 → ∃ y, 0 < y ∧ y * y = x, begin intros x x0, cases x0, cases le_total x 1 with x1 x1, { rcases H x0 x1 with ⟨y, y0, hy⟩, exact ⟨y, le_of_lt y0, hy⟩ }, { have := (inv_le_inv x0 zero_lt_one).2 x1, rw inv_one at this, rcases H (inv_pos x0) this with ⟨y, y0, hy⟩, refine ⟨y⁻¹, le_of_lt (inv_pos y0), _⟩, rw [← mul_inv', hy, inv_inv'] }, { exact ⟨0, by simp [x0.symm]⟩ } end, λ x x0 x1, begin let S := {y | 0 < y ∧ y * y ≤ x}, have lb : x ∈ S := ⟨x0, by simpa using (mul_le_mul_right x0).2 x1⟩, have ub : ∀ y ∈ S, (y:ℝ) ≤ 1, { intros y yS, cases yS with y0 yx, refine (mul_self_le_mul_self_iff (le_of_lt y0) zero_le_one).2 _, simpa using le_trans yx x1 }, have S0 : 0 < Sup S := lt_of_lt_of_le x0 (le_Sup _ ⟨_, ub⟩ lb), refine ⟨Sup S, S0, le_antisymm (not_lt.1 $ λ h, _) (not_lt.1 $ λ h, _)⟩, { rw [← div_lt_iff S0, lt_Sup S ⟨_, lb⟩ ⟨_, ub⟩] at h, rcases h with ⟨y, ⟨y0, yx⟩, hy⟩, rw [div_lt_iff S0, ← div_lt_iff' y0, lt_Sup S ⟨_, lb⟩ ⟨_, ub⟩] at hy, rcases hy with ⟨z, ⟨z0, zx⟩, hz⟩, rw [div_lt_iff y0] at hz, exact not_lt_of_lt ((mul_lt_mul_right y0).1 (lt_of_le_of_lt yx hz)) ((mul_lt_mul_left z0).1 (lt_of_le_of_lt zx hz)) }, { let s := Sup S, let y := s + (x - s * s) / 3, replace h : 0 < x - s * s := sub_pos.2 h, have _30 := bit1_pos zero_le_one, have : s < y := (lt_add_iff_pos_right _).2 (div_pos h _30), refine not_le_of_lt this (le_Sup S ⟨_, ub⟩ ⟨lt_trans S0 this, _⟩), rw [add_mul_self_eq, add_assoc, ← le_sub_iff_add_le', ← add_mul, ← le_div_iff (div_pos h _30), div_div_cancel (ne_of_gt h)], apply add_le_add, { simpa using (mul_le_mul_left (@two_pos ℝ _)).2 (Sup_le_ub _ ⟨_, lb⟩ ub) }, { rw [div_le_one_iff_le _30], refine le_trans (sub_le_self _ (mul_self_nonneg _)) (le_trans x1 _), exact (le_add_iff_nonneg_left _).2 (le_of_lt two_pos) } } end def sqrt_aux (f : cau_seq ℚ abs) : ℕ → ℚ | 0 := rat.mk_nat (f 0).num.to_nat.sqrt (f 0).denom.sqrt | (n + 1) := let s := sqrt_aux n in max 0 $ (s + f (n+1) / s) / 2 theorem sqrt_aux_nonneg (f : cau_seq ℚ abs) : ∀ i : ℕ, 0 ≤ sqrt_aux f i | 0 := by rw [sqrt_aux, mk_nat_eq, mk_eq_div]; apply div_nonneg'; exact int.cast_nonneg.2 (int.of_nat_nonneg _) | (n + 1) := le_max_left _ _ /- TODO(Mario): finish the proof theorem sqrt_aux_converges (f : cau_seq ℚ abs) : ∃ h x, 0 ≤ x ∧ x * x = max 0 (mk f) ∧ mk ⟨sqrt_aux f, h⟩ = x := begin rcases sqrt_exists (le_max_left 0 (mk f)) with ⟨x, x0, hx⟩, suffices : ∃ h, mk ⟨sqrt_aux f, h⟩ = x, { exact this.imp (λ h e, ⟨x, x0, hx, e⟩) }, apply of_near, suffices : ∃ δ > 0, ∀ i, abs (↑(sqrt_aux f i) - x) < δ / 2 ^ i, { rcases this with ⟨δ, δ0, hδ⟩, intros, } end -/ noncomputable def sqrt (x : ℝ) : ℝ := classical.some (sqrt_exists (le_max_left 0 x)) /-quotient.lift_on x (λ f, mk ⟨sqrt_aux f, (sqrt_aux_converges f).fst⟩) (λ f g e, begin rcases sqrt_aux_converges f with ⟨hf, x, x0, xf, xs⟩, rcases sqrt_aux_converges g with ⟨hg, y, y0, yg, ys⟩, refine xs.trans (eq.trans _ ys.symm), rw [← @mul_self_inj_of_nonneg ℝ _ x y x0 y0, xf, yg], congr' 1, exact quotient.sound e end)-/ theorem sqrt_prop (x : ℝ) : 0 ≤ sqrt x ∧ sqrt x * sqrt x = max 0 x := classical.some_spec (sqrt_exists (le_max_left 0 x)) /-quotient.induction_on x $ λ f, by rcases sqrt_aux_converges f with ⟨hf, _, x0, xf, rfl⟩; exact ⟨x0, xf⟩-/ theorem sqrt_eq_zero_of_nonpos (h : x ≤ 0) : sqrt x = 0 := eq_zero_of_mul_self_eq_zero $ (sqrt_prop x).2.trans $ max_eq_left h theorem sqrt_nonneg (x : ℝ) : 0 ≤ sqrt x := (sqrt_prop x).1 @[simp] theorem mul_self_sqrt (h : 0 ≤ x) : sqrt x * sqrt x = x := (sqrt_prop x).2.trans (max_eq_right h) @[simp] theorem sqrt_mul_self (h : 0 ≤ x) : sqrt (x * x) = x := (mul_self_inj_of_nonneg (sqrt_nonneg _) h).1 (mul_self_sqrt (mul_self_nonneg _)) theorem sqrt_eq_iff_mul_self_eq (hx : 0 ≤ x) (hy : 0 ≤ y) : sqrt x = y ↔ y * y = x := ⟨λ h, by rw [← h, mul_self_sqrt hx], λ h, by rw [← h, sqrt_mul_self hy]⟩ @[simp] theorem sqr_sqrt (h : 0 ≤ x) : sqrt x ^ 2 = x := by rw [pow_two, mul_self_sqrt h] @[simp] theorem sqrt_sqr (h : 0 ≤ x) : sqrt (x ^ 2) = x := by rw [pow_two, sqrt_mul_self h] theorem sqrt_eq_iff_sqr_eq (hx : 0 ≤ x) (hy : 0 ≤ y) : sqrt x = y ↔ y ^ 2 = x := by rw [pow_two, sqrt_eq_iff_mul_self_eq hx hy] theorem sqrt_mul_self_eq_abs (x : ℝ) : sqrt (x * x) = abs x := (le_total 0 x).elim (λ h, (sqrt_mul_self h).trans (abs_of_nonneg h).symm) (λ h, by rw [← neg_mul_neg, sqrt_mul_self (neg_nonneg.2 h), abs_of_nonpos h]) theorem sqrt_sqr_eq_abs (x : ℝ) : sqrt (x ^ 2) = abs x := by rw [pow_two, sqrt_mul_self_eq_abs] @[simp] theorem sqrt_zero : sqrt 0 = 0 := by simpa using sqrt_mul_self (le_refl _) @[simp] theorem sqrt_one : sqrt 1 = 1 := by simpa using sqrt_mul_self zero_le_one @[simp] theorem sqrt_le (hx : 0 ≤ x) (hy : 0 ≤ y) : sqrt x ≤ sqrt y ↔ x ≤ y := by rw [mul_self_le_mul_self_iff (sqrt_nonneg _) (sqrt_nonneg _), mul_self_sqrt hx, mul_self_sqrt hy] @[simp] theorem sqrt_lt (hx : 0 ≤ x) (hy : 0 ≤ y) : sqrt x < sqrt y ↔ x < y := lt_iff_lt_of_le_iff_le (sqrt_le hy hx) lemma sqrt_le_sqrt (h : x ≤ y) : sqrt x ≤ sqrt y := begin rw [mul_self_le_mul_self_iff (sqrt_nonneg _) (sqrt_nonneg _), (sqrt_prop _).2, (sqrt_prop _).2], exact max_le_max (le_refl _) h end lemma sqrt_le_left (hy : 0 ≤ y) : sqrt x ≤ y ↔ x ≤ y ^ 2 := begin rw [mul_self_le_mul_self_iff (sqrt_nonneg _) hy, pow_two], cases le_total 0 x with hx hx, { rw [mul_self_sqrt hx] }, { have h1 : 0 ≤ y * y := mul_nonneg hy hy, have h2 : x ≤ y * y := le_trans hx h1, simp [sqrt_eq_zero_of_nonpos, hx, h1, h2] } end /- note: if you want to conclude `x ≤ sqrt y`, then use `le_sqrt_of_sqr_le`. if you have `x > 0`, consider using `le_sqrt'` -/ lemma le_sqrt (hx : 0 ≤ x) (hy : 0 ≤ y) : x ≤ sqrt y ↔ x ^ 2 ≤ y := by rw [mul_self_le_mul_self_iff hx (sqrt_nonneg _), pow_two, mul_self_sqrt hy] lemma le_sqrt' (hx : 0 < x) : x ≤ sqrt y ↔ x ^ 2 ≤ y := begin rw [mul_self_le_mul_self_iff (le_of_lt hx) (sqrt_nonneg _), pow_two], cases le_total 0 y with hy hy, { rw [mul_self_sqrt hy] }, { have h1 : 0 < x * x := mul_pos hx hx, have h2 : ¬x * x ≤ y := not_le_of_lt (lt_of_le_of_lt hy h1), simp [sqrt_eq_zero_of_nonpos, hy, h1, h2] } end lemma le_sqrt_of_sqr_le (h : x ^ 2 ≤ y) : x ≤ sqrt y := begin cases lt_or_ge 0 x with hx hx, { rwa [le_sqrt' hx] }, { exact le_trans hx (sqrt_nonneg y) } end @[simp] theorem sqrt_inj (hx : 0 ≤ x) (hy : 0 ≤ y) : sqrt x = sqrt y ↔ x = y := by simp [le_antisymm_iff, hx, hy] @[simp] theorem sqrt_eq_zero (h : 0 ≤ x) : sqrt x = 0 ↔ x = 0 := by simpa using sqrt_inj h (le_refl _) theorem sqrt_eq_zero' : sqrt x = 0 ↔ x ≤ 0 := (le_total x 0).elim (λ h, by simp [h, sqrt_eq_zero_of_nonpos]) (λ h, by simp [h]; simp [le_antisymm_iff, h]) @[simp] theorem sqrt_pos : 0 < sqrt x ↔ 0 < x := lt_iff_lt_of_le_iff_le (iff.trans (by simp [le_antisymm_iff, sqrt_nonneg]) sqrt_eq_zero') @[simp] theorem sqrt_mul' (x) {y : ℝ} (hy : 0 ≤ y) : sqrt (x * y) = sqrt x * sqrt y := begin cases le_total 0 x with hx hx, { refine (mul_self_inj_of_nonneg _ (mul_nonneg _ _)).1 _; try {apply sqrt_nonneg}, rw [mul_self_sqrt (mul_nonneg hx hy), mul_assoc, mul_left_comm (sqrt y), mul_self_sqrt hy, ← mul_assoc, mul_self_sqrt hx] }, { rw [sqrt_eq_zero'.2 (mul_nonpos_of_nonpos_of_nonneg hx hy), sqrt_eq_zero'.2 hx, zero_mul] } end @[simp] theorem sqrt_mul (hx : 0 ≤ x) (y : ℝ) : sqrt (x * y) = sqrt x * sqrt y := by rw [mul_comm, sqrt_mul' _ hx, mul_comm] @[simp] theorem sqrt_inv (x : ℝ) : sqrt x⁻¹ = (sqrt x)⁻¹ := (le_or_lt x 0).elim (λ h, by simp [sqrt_eq_zero'.2, inv_nonpos, h]) (λ h, by rw [ ← mul_self_inj_of_nonneg (sqrt_nonneg _) (le_of_lt $ inv_pos $ sqrt_pos.2 h), mul_self_sqrt (le_of_lt $ inv_pos h), ← mul_inv', mul_self_sqrt (le_of_lt h)]) @[simp] theorem sqrt_div (hx : 0 ≤ x) (y : ℝ) : sqrt (x / y) = sqrt x / sqrt y := by rw [division_def, sqrt_mul hx, sqrt_inv]; refl /-- This is used in proving the lemma that `sqrt` is a continuous function. The lemma, called `real.continuous_sqrt`, is located at the file `analysis.specific_limits` -/ lemma abs_sqrt_sub_sqrt_le_sqrt_abs (x y : ℝ) : abs (sqrt x - sqrt y) ≤ sqrt (abs (x - y)) := nonneg_le_nonneg_of_squares_le (sqrt_nonneg _) $ begin rw abs_mul_abs_self, wlog h : y ≤ x using [x y], -- The main goal, i.e., prove the goal with `y ≤ x` { have eq₁ : sqrt (abs (x - y)) * sqrt (abs (x - y)) = x - y, rw mul_self_sqrt (abs_nonneg _), apply abs_of_nonneg, linarith, rw eq₁, have eq₂ : (sqrt x - sqrt y) * (sqrt x - sqrt y) = sqrt x * sqrt x - (2 * sqrt x * sqrt y - sqrt y * sqrt y), ring, rw eq₂, cases le_total 0 x with hx hx, -- if `0 ≤ x` rw mul_self_sqrt hx, apply sub_le_sub_left, cases le_total 0 y with hy hy, { rw mul_self_sqrt hy, exact calc y ≤ 2 * y - y : by ring ... = 2 * (sqrt y * sqrt y) - y : by rw mul_self_sqrt hy ... ≤ 2 * (sqrt x * sqrt y) - y : begin apply sub_le_sub_right, apply mul_le_mul_of_nonneg_left, apply mul_le_mul_of_nonneg_right, exact sqrt_le_sqrt h, exact sqrt_nonneg _, norm_num end ... ≤ 2 * sqrt x * sqrt y - y : by ring }, { rw sqrt_eq_zero_of_nonpos hy, simpa }, -- if `x ≤ 0` { rw sqrt_eq_zero_of_nonpos hx, cases le_total 0 y with hy hy, rw mul_self_sqrt hy, linarith, rw sqrt_eq_zero_of_nonpos hy, linarith } }, -- The invariant goal { rw abs_sub, have : ∀ a b : ℝ, (a - b) * (a - b) = (b - a) * (b - a), intros, ring, rwa this } end attribute [irreducible] real.le end real
0cb9d5cbdf0c547358dddbdf4e9c07c13dcecc90
c777c32c8e484e195053731103c5e52af26a25d1
/src/ring_theory/integrally_closed.lean
a2483b48c5dc0b6210fc3e7332c37c59319c5d39
[ "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,113
lean
/- Copyright (c) 2021 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen -/ import field_theory.splitting_field import ring_theory.integral_closure import ring_theory.localization.integral /-! # Integrally closed rings An integrally closed domain `R` contains all the elements of `Frac(R)` that are integral over `R`. A special case of integrally closed domains are the Dedekind domains. ## Main definitions * `is_integrally_closed R` states `R` contains all integral elements of `Frac(R)` ## Main results * `is_integrally_closed_iff K`, where `K` is a fraction field of `R`, states `R` is integrally closed iff it is the integral closure of `R` in `K` * `eq_map_mul_C_of_dvd`: if `K = Frac(R)` and `g : K[X]` divides a monic polynomial with coefficients in `R`, then `g * (C g.leading_coeff⁻¹)` has coefficients in `R` -/ open_locale non_zero_divisors polynomial open polynomial /-- `R` is integrally closed if all integral elements of `Frac(R)` are also elements of `R`. This definition uses `fraction_ring R` to denote `Frac(R)`. See `is_integrally_closed_iff` if you want to choose another field of fractions for `R`. -/ class is_integrally_closed (R : Type*) [comm_ring R] [is_domain R] : Prop := (algebra_map_eq_of_integral : ∀ {x : fraction_ring R}, is_integral R x → ∃ y, algebra_map R (fraction_ring R) y = x) section iff variables {R : Type*} [comm_ring R] [is_domain R] variables (K : Type*) [field K] [algebra R K] [is_fraction_ring R K] /-- `R` is integrally closed iff all integral elements of its fraction field `K` are also elements of `R`. -/ theorem is_integrally_closed_iff : is_integrally_closed R ↔ ∀ {x : K}, is_integral R x → ∃ y, algebra_map R K y = x := begin let e : K ≃ₐ[R] fraction_ring R := is_localization.alg_equiv R⁰_ _, split, { rintros ⟨cl⟩, refine λ x hx, _, obtain ⟨y, hy⟩ := cl ((is_integral_alg_equiv e).mpr hx), exact ⟨y, e.algebra_map_eq_apply.mp hy⟩ }, { rintros cl, refine ⟨λ x hx, _⟩, obtain ⟨y, hy⟩ := cl ((is_integral_alg_equiv e.symm).mpr hx), exact ⟨y, e.symm.algebra_map_eq_apply.mp hy⟩ }, end /-- `R` is integrally closed iff it is the integral closure of itself in its field of fractions. -/ theorem is_integrally_closed_iff_is_integral_closure : is_integrally_closed R ↔ is_integral_closure R R K := (is_integrally_closed_iff K).trans $ begin let e : K ≃ₐ[R] fraction_ring R := is_localization.alg_equiv R⁰_ _, split, { intros cl, refine ⟨is_fraction_ring.injective _ _, λ x, ⟨cl, _⟩⟩, rintros ⟨y, y_eq⟩, rw ← y_eq, exact is_integral_algebra_map }, { rintros ⟨-, cl⟩ x hx, exact cl.mp hx } end end iff namespace is_integrally_closed variables {R : Type*} [comm_ring R] [id : is_domain R] [iic : is_integrally_closed R] variables {K : Type*} [field K] [algebra R K] [ifr : is_fraction_ring R K] include iic ifr instance : is_integral_closure R R K := (is_integrally_closed_iff_is_integral_closure K).mp iic lemma is_integral_iff {x : K} : is_integral R x ↔ ∃ y : R, algebra_map R K y = x := is_integral_closure.is_integral_iff lemma exists_algebra_map_eq_of_is_integral_pow {x : K} {n : ℕ} (hn : 0 < n) (hx : is_integral R $ x ^ n) : ∃ y : R, algebra_map R K y = x := is_integral_iff.mp $ is_integral_of_pow hn hx omit iic ifr lemma exists_algebra_map_eq_of_pow_mem_subalgebra {K : Type*} [field K] [algebra R K] {S : subalgebra R K} [is_integrally_closed S] [is_fraction_ring S K] {x : K} {n : ℕ} (hn : 0 < n) (hx : x ^ n ∈ S) : ∃ y : S, algebra_map S K y = x := exists_algebra_map_eq_of_is_integral_pow hn $ is_integral_iff.mpr ⟨⟨x ^ n, hx⟩, rfl⟩ include id ifr variables {R} (K) lemma integral_closure_eq_bot_iff : integral_closure R K = ⊥ ↔ is_integrally_closed R := begin refine eq_bot_iff.trans _, split, { rw is_integrally_closed_iff K, intros h x hx, exact set.mem_range.mp (algebra.mem_bot.mp (h hx)), assumption }, { intros h x hx, rw [algebra.mem_bot, set.mem_range], exactI is_integral_iff.mp hx }, end include iic variables (R K) @[simp] lemma integral_closure_eq_bot : integral_closure R K = ⊥ := (integral_closure_eq_bot_iff K).mpr ‹_› end is_integrally_closed namespace integral_closure open is_integrally_closed variables {R : Type*} [comm_ring R] variables (K : Type*) [field K] [algebra R K] theorem mem_lifts_of_monic_of_dvd_map {f : R[X]} (hf : f.monic) {g : K[X]} (hg : g.monic) (hd : g ∣ f.map (algebra_map R K)) : g ∈ lifts (algebra_map (integral_closure R K) K) := begin haveI : is_scalar_tower R K g.splitting_field := splitting_field_aux.is_scalar_tower _ _ _, have := mem_lift_of_splits_of_roots_mem_range (integral_closure R g.splitting_field) ((splits_id_iff_splits _).2 $ splitting_field.splits g) (hg.map _) (λ a ha, (set_like.ext_iff.mp (integral_closure R g.splitting_field).range_algebra_map _).mpr $ roots_mem_integral_closure hf _), { rw [lifts_iff_coeff_lifts, ←ring_hom.coe_range, subalgebra.range_algebra_map] at this, refine (lifts_iff_coeff_lifts _).2 (λ n, _), rw [← ring_hom.coe_range, subalgebra.range_algebra_map], obtain ⟨p, hp, he⟩ := (set_like.mem_coe.mp (this n)), use [p, hp], rw [is_scalar_tower.algebra_map_eq R K, coeff_map, ← eval₂_map, eval₂_at_apply] at he, rw eval₂_eq_eval_map, apply (injective_iff_map_eq_zero _).1 _ _ he, { apply ring_hom.injective } }, rw [is_scalar_tower.algebra_map_eq R K _, ← map_map], refine multiset.mem_of_le (roots.le_of_dvd ((hf.map _).map _).ne_zero _) ha, { apply_instance }, { exact map_dvd (algebra_map K g.splitting_field) hd }, { apply splitting_field_aux.is_scalar_tower }, end variables [is_domain R] [is_fraction_ring R K] variables {L : Type*} [field L] [algebra K L] [algebra R L] [is_scalar_tower R K L] -- Can't be an instance because you need to supply `K`. lemma is_integrally_closed_of_finite_extension [finite_dimensional K L] : is_integrally_closed (integral_closure R L) := begin letI : is_fraction_ring (integral_closure R L) L := is_fraction_ring_of_finite_extension K L, exact (integral_closure_eq_bot_iff L).mp integral_closure_idem end end integral_closure namespace is_integrally_closed open integral_closure variables {R : Type*} [comm_ring R] [is_domain R] variables (K : Type*) [field K] [algebra R K] [is_fraction_ring R K] /-- If `K = Frac(R)` and `g : K[X]` divides a monic polynomial with coefficients in `R`, then `g * (C g.leading_coeff⁻¹)` has coefficients in `R` -/ lemma eq_map_mul_C_of_dvd [is_integrally_closed R] {f : R[X]} (hf : f.monic) {g : K[X]} (hg : g ∣ f.map (algebra_map R K)) : ∃ g' : R[X], (g'.map (algebra_map R K)) * (C $ leading_coeff g) = g := begin have g_ne_0 : g ≠ 0 := ne_zero_of_dvd_ne_zero (monic.ne_zero $ hf.map (algebra_map R K)) hg, suffices lem : ∃ g' : R[X], g'.map (algebra_map R K) = g * (C g.leading_coeff⁻¹), { obtain ⟨g', hg'⟩ := lem, use g', rw [hg', mul_assoc, ← C_mul, inv_mul_cancel (leading_coeff_ne_zero.mpr g_ne_0), C_1, mul_one] }, have g_mul_dvd : g * (C g.leading_coeff⁻¹) ∣ f.map (algebra_map R K), { rwa associated.dvd_iff_dvd_left (show associated (g * (C (g.leading_coeff⁻¹))) g, from _), rw associated_mul_is_unit_left_iff, exact is_unit_C.mpr (inv_ne_zero $ leading_coeff_ne_zero.mpr g_ne_0).is_unit }, let algeq := (subalgebra.equiv_of_eq _ _ $ integral_closure_eq_bot R _).trans (algebra.bot_equiv_of_injective $ is_fraction_ring.injective R $ K), have : (algebra_map R _).comp algeq.to_alg_hom.to_ring_hom = (integral_closure R _).to_subring.subtype, { ext, conv_rhs { rw ← algeq.symm_apply_apply x }, refl }, have H := ((mem_lifts _ ).1 (mem_lifts_of_monic_of_dvd_map K hf (monic_mul_leading_coeff_inv g_ne_0) g_mul_dvd)), refine ⟨map algeq.to_alg_hom.to_ring_hom _, _⟩, use classical.some H, rw [map_map, this], exact classical.some_spec H end end is_integrally_closed
9bf8f34121a1a9665d9046dd1eb7c3aba3a25999
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/hott/homotopy/red_susp.hlean
a71996e89d2743fc3a5982218f883aced09ab5b9
[ "Apache-2.0" ]
permissive
soonhokong/lean-osx
4a954262c780e404c1369d6c06516161d07fcb40
3670278342d2f4faa49d95b46d86642d7875b47c
refs/heads/master
1,611,410,334,552
1,474,425,686,000
1,474,425,686,000
12,043,103
5
1
null
null
null
null
UTF-8
Lean
false
false
2,772
hlean
/- Copyright (c) 2015 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn Declaration of the reduced suspension -/ import hit.two_quotient types.pointed algebra.e_closure open simple_two_quotient eq unit pointed e_closure namespace red_susp section parameter {A : pType} inductive red_susp_R : unit → unit → Type := | Rmk : Π(a : A), red_susp_R star star open red_susp_R inductive red_susp_Q : Π⦃x : unit⦄, e_closure red_susp_R x x → Type := | Qmk : red_susp_Q [Rmk pt] open red_susp_Q local abbreviation R := red_susp_R local abbreviation Q := red_susp_Q definition red_susp : Type := simple_two_quotient R Q -- TODO: define this in root namespace definition base : red_susp := incl0 R Q star definition merid (a : A) : base = base := incl1 R Q (Rmk a) definition merid_pt : merid pt = idp := incl2 R Q Qmk -- protected definition rec {P : red_susp → Type} (Pb : P base) (Pm : Π(a : A), Pb =[merid a] Pb) -- (Pe : Pm pt =[merid_pt] idpo) (x : red_susp) : P x := -- begin -- induction x, -- end -- protected definition rec_on [reducible] {P : red_susp → Type} (x : red_susp) (Pb : P base) -- (Pm : Π(a : A), Pb =[merid a] Pb) (Pe : Pm pt =[merid_pt] idpo) : P x := -- rec Pb Pm Pe x -- definition rec_merid {P : red_susp → Type} (Pb : P base) (Pm : Π(a : A), Pb =[merid a] Pb) -- (Pe : Pm pt =[merid_pt] idpo) (a : A) -- : apdo (rec Pb Pm Pe) (merid a) = Pm a := -- !rec_incl1 -- theorem elim_merid_pt {P : red_susp → Type} (Pb : P base) (Pm : Π(a : A), Pb =[merid a] Pb) -- (Pe : Pm pt =[merid_pt] idpo) -- : square (ap02 (rec Pb Pm Pe) merid_pt) Pe (rec_merid Pe pt) idp := -- !rec_incl2 protected definition elim {P : Type} (Pb : P) (Pm : Π(a : A), Pb = Pb) (Pe : Pm pt = idp) (x : red_susp) : P := begin induction x, exact Pb, induction s, exact Pm a, induction q, exact Pe end protected definition elim_on [reducible] {P : Type} (x : red_susp) (Pb : P) (Pm : Π(a : A), Pb = Pb) (Pe : Pm pt = idp) : P := elim Pb Pm Pe x definition elim_merid {P : Type} {Pb : P} {Pm : Π(a : A), Pb = Pb} (Pe : Pm pt = idp) (a : A) : ap (elim Pb Pm Pe) (merid a) = Pm a := !elim_incl1 theorem elim_merid_pt {P : Type} (Pb : P) (Pm : Π(a : A), Pb = Pb) (Pe : Pm pt = idp) : square (ap02 (elim Pb Pm Pe) merid_pt) Pe (elim_merid Pe pt) idp := !elim_incl2 end end red_susp attribute red_susp.base [constructor] attribute /-red_susp.rec-/ red_susp.elim [unfold 6] [recursor 6] --attribute red_susp.elim_type [unfold 9] attribute /-red_susp.rec_on-/ red_susp.elim_on [unfold 3] --attribute red_susp.elim_type_on [unfold 6]
a08fd551da21e9f37c9b24d03afc7a963a527a32
82e44445c70db0f03e30d7be725775f122d72f3e
/src/algebra/ordered_monoid.lean
94d1567cd0a1fd565014e6a9da2937873d92344a
[ "Apache-2.0" ]
permissive
stjordanis/mathlib
51e286d19140e3788ef2c470bc7b953e4991f0c9
2568d41bca08f5d6bf39d915434c8447e21f42ee
refs/heads/master
1,631,748,053,501
1,627,938,886,000
1,627,938,886,000
228,728,358
0
0
Apache-2.0
1,576,630,588,000
1,576,630,587,000
null
UTF-8
Lean
false
false
37,267
lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Mario Carneiro, Johannes Hölzl -/ import algebra.group.with_one import algebra.group.type_tags import algebra.group.prod import algebra.order_functions import order.bounded_lattice import algebra.ordered_monoid_lemmas /-! # Ordered monoids This file develops the basics of ordered monoids. ## Implementation details Unfortunately, the number of `'` appended to lemmas in this file may differ between the multiplicative and the additive version of a lemma. The reason is that we did not want to change existing names in the library. -/ set_option old_structure_cmd true universe u variable {α : Type u} /-- An ordered commutative monoid is a commutative monoid with a partial order such that * `a ≤ b → c * a ≤ c * b` (multiplication is monotone) * `a * b < a * c → b < c`. -/ @[protect_proj, ancestor comm_monoid partial_order] class ordered_comm_monoid (α : Type*) extends comm_monoid α, partial_order α := (mul_le_mul_left : ∀ a b : α, a ≤ b → ∀ c : α, c * a ≤ c * b) (lt_of_mul_lt_mul_left : ∀ a b c : α, a * b < a * c → b < c) /-- An ordered (additive) commutative monoid is a commutative monoid with a partial order such that * `a ≤ b → c + a ≤ c + b` (addition is monotone) * `a + b < a + c → b < c`. -/ @[protect_proj, ancestor add_comm_monoid partial_order] class ordered_add_comm_monoid (α : Type*) extends add_comm_monoid α, partial_order α := (add_le_add_left : ∀ a b : α, a ≤ b → ∀ c : α, c + a ≤ c + b) (lt_of_add_lt_add_left : ∀ a b c : α, a + b < a + c → b < c) attribute [to_additive] ordered_comm_monoid section ordered_instances @[to_additive] instance ordered_comm_monoid.to_covariant_class_left (M : Type*) [ordered_comm_monoid M] : covariant_class M M (*) (≤) := { elim := λ a b c bc, ordered_comm_monoid.mul_le_mul_left _ _ bc a } @[to_additive] instance ordered_comm_monoid.to_contravariant_class_left (M : Type*) [ordered_comm_monoid M] : contravariant_class M M (*) (<) := { elim := λ a b c, ordered_comm_monoid.lt_of_mul_lt_mul_left _ _ _ } /- This instance can be proven with `by apply_instance`. However, `with_bot ℕ` does not pick up a `covariant_class M M (function.swap (*)) (≤)` instance without it (see PR #7940). -/ @[to_additive] instance ordered_comm_monoid.to_covariant_class_right (M : Type*) [ordered_comm_monoid M] : covariant_class M M (function.swap (*)) (≤) := covariant_swap_mul_le_of_covariant_mul_le M /- This instance can be proven with `by apply_instance`. However, by analogy with the instance `ordered_comm_monoid.to_covariant_class_right` above, I imagine that without this instance, some Type would not have a `contravariant_class M M (function.swap (*)) (≤)` instance. -/ @[to_additive] instance ordered_comm_monoid.to_contravariant_class_right (M : Type*) [ordered_comm_monoid M] : contravariant_class M M (function.swap (*)) (<) := contravariant_swap_mul_lt_of_contravariant_mul_lt M end ordered_instances /-- An `ordered_comm_monoid` with one-sided 'division' in the sense that if `a ≤ b`, there is some `c` for which `a * c = b`. This is a weaker version of the condition on canonical orderings defined by `canonically_ordered_monoid`. -/ class has_exists_mul_of_le (α : Type u) [ordered_comm_monoid α] : Prop := (exists_mul_of_le : ∀ {a b : α}, a ≤ b → ∃ (c : α), b = a * c) /-- An `ordered_add_comm_monoid` with one-sided 'subtraction' in the sense that if `a ≤ b`, then there is some `c` for which `a + c = b`. This is a weaker version of the condition on canonical orderings defined by `canonically_ordered_add_monoid`. -/ class has_exists_add_of_le (α : Type u) [ordered_add_comm_monoid α] : Prop := (exists_add_of_le : ∀ {a b : α}, a ≤ b → ∃ (c : α), b = a + c) attribute [to_additive] has_exists_mul_of_le export has_exists_mul_of_le (exists_mul_of_le) export has_exists_add_of_le (exists_add_of_le) /-- A linearly ordered additive commutative monoid. -/ @[protect_proj, ancestor linear_order ordered_add_comm_monoid] class linear_ordered_add_comm_monoid (α : Type*) extends linear_order α, ordered_add_comm_monoid α := (lt_of_add_lt_add_left := λ x y z, by { -- type-class inference uses `a : linear_order α` which it can't unfold, unless we provide this! -- `lt_iff_le_not_le` gets filled incorrectly with `autoparam` if we don't provide that field. letI : linear_order α := by refine { le := le, lt := lt, lt_iff_le_not_le := _, .. }; assumption, apply lt_imp_lt_of_le_imp_le, exact λ h, add_le_add_left _ _ h _ }) /-- A linearly ordered commutative monoid. -/ @[protect_proj, ancestor linear_order ordered_comm_monoid, to_additive] class linear_ordered_comm_monoid (α : Type*) extends linear_order α, ordered_comm_monoid α := (lt_of_mul_lt_mul_left := λ x y z, by { -- type-class inference uses `a : linear_order α` which it can't unfold, unless we provide this! -- `lt_iff_le_not_le` gets filled incorrectly with `autoparam` if we don't provide that field. letI : linear_order α := by refine { le := le, lt := lt, lt_iff_le_not_le := _, .. }; assumption, apply lt_imp_lt_of_le_imp_le, exact λ h, mul_le_mul_left _ _ h _ }) /-- A linearly ordered commutative monoid with a zero element. -/ class linear_ordered_comm_monoid_with_zero (α : Type*) extends linear_ordered_comm_monoid α, comm_monoid_with_zero α := (zero_le_one : (0 : α) ≤ 1) /-- A linearly ordered commutative monoid with an additively absorbing `⊤` element. Instances should include number systems with an infinite element adjoined.` -/ @[protect_proj, ancestor linear_ordered_add_comm_monoid order_top] class linear_ordered_add_comm_monoid_with_top (α : Type*) extends linear_ordered_add_comm_monoid α, order_top α := (top_add' : ∀ x : α, ⊤ + x = ⊤) section linear_ordered_add_comm_monoid_with_top variables [linear_ordered_add_comm_monoid_with_top α] {a b : α} @[simp] lemma top_add (a : α) : ⊤ + a = ⊤ := linear_ordered_add_comm_monoid_with_top.top_add' a @[simp] lemma add_top (a : α) : a + ⊤ = ⊤ := trans (add_comm _ _) (top_add _) end linear_ordered_add_comm_monoid_with_top /-- Pullback an `ordered_comm_monoid` under an injective map. See note [reducible non-instances]. -/ @[reducible, to_additive function.injective.ordered_add_comm_monoid "Pullback an `ordered_add_comm_monoid` under an injective map."] def function.injective.ordered_comm_monoid [ordered_comm_monoid α] {β : Type*} [has_one β] [has_mul β] (f : β → α) (hf : function.injective f) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) : ordered_comm_monoid β := { mul_le_mul_left := λ a b ab c, show f (c * a) ≤ f (c * b), by { rw [mul, mul], apply mul_le_mul_left', exact ab }, lt_of_mul_lt_mul_left := λ a b c bc, show f b < f c, from lt_of_mul_lt_mul_left' (by rwa [← mul, ← mul] : (f a) * _ < _), ..partial_order.lift f hf, ..hf.comm_monoid f one mul } /-- Pullback a `linear_ordered_comm_monoid` under an injective map. See note [reducible non-instances]. -/ @[reducible, to_additive function.injective.linear_ordered_add_comm_monoid "Pullback an `ordered_add_comm_monoid` under an injective map."] def function.injective.linear_ordered_comm_monoid [linear_ordered_comm_monoid α] {β : Type*} [has_one β] [has_mul β] (f : β → α) (hf : function.injective f) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) : linear_ordered_comm_monoid β := { .. hf.ordered_comm_monoid f one mul, .. linear_order.lift f hf } lemma bit0_pos [ordered_add_comm_monoid α] {a : α} (h : 0 < a) : 0 < bit0 a := add_pos h h namespace units @[to_additive] instance [monoid α] [preorder α] : preorder (units α) := preorder.lift (coe : units α → α) @[simp, norm_cast, to_additive] theorem coe_le_coe [monoid α] [preorder α] {a b : units α} : (a : α) ≤ b ↔ a ≤ b := iff.rfl -- should `to_additive` do this? attribute [norm_cast] add_units.coe_le_coe @[simp, norm_cast, to_additive] theorem coe_lt_coe [monoid α] [preorder α] {a b : units α} : (a : α) < b ↔ a < b := iff.rfl attribute [norm_cast] add_units.coe_lt_coe @[to_additive] instance [monoid α] [partial_order α] : partial_order (units α) := partial_order.lift coe units.ext @[to_additive] instance [monoid α] [linear_order α] : linear_order (units α) := linear_order.lift coe units.ext @[simp, norm_cast, to_additive] theorem max_coe [monoid α] [linear_order α] {a b : units α} : (↑(max a b) : α) = max a b := by by_cases b ≤ a; simp [max, h] attribute [norm_cast] add_units.max_coe @[simp, norm_cast, to_additive] theorem min_coe [monoid α] [linear_order α] {a b : units α} : (↑(min a b) : α) = min a b := by by_cases a ≤ b; simp [min, h] attribute [norm_cast] add_units.min_coe end units namespace with_zero local attribute [semireducible] with_zero instance [preorder α] : preorder (with_zero α) := with_bot.preorder instance [partial_order α] : partial_order (with_zero α) := with_bot.partial_order instance [partial_order α] : order_bot (with_zero α) := with_bot.order_bot lemma zero_le [partial_order α] (a : with_zero α) : 0 ≤ a := order_bot.bot_le a lemma zero_lt_coe [preorder α] (a : α) : (0 : with_zero α) < a := with_bot.bot_lt_coe a @[simp, norm_cast] lemma coe_lt_coe [partial_order α] {a b : α} : (a : with_zero α) < b ↔ a < b := with_bot.coe_lt_coe @[simp, norm_cast] lemma coe_le_coe [partial_order α] {a b : α} : (a : with_zero α) ≤ b ↔ a ≤ b := with_bot.coe_le_coe instance [lattice α] : lattice (with_zero α) := with_bot.lattice instance [linear_order α] : linear_order (with_zero α) := with_bot.linear_order lemma mul_le_mul_left {α : Type u} [has_mul α] [preorder α] [covariant_class α α (*) (≤)] : ∀ (a b : with_zero α), a ≤ b → ∀ (c : with_zero α), c * a ≤ c * b := begin rintro (_ | a) (_ | b) h (_ | c); try { exact λ f hf, option.no_confusion hf }, { exact false.elim (not_lt_of_le h (with_zero.zero_lt_coe a))}, { simp_rw [some_eq_coe] at h ⊢, norm_cast at h ⊢, exact covariant_class.elim _ h } end lemma lt_of_mul_lt_mul_left {α : Type u} [has_mul α] [partial_order α] [contravariant_class α α (*) (<)] : ∀ (a b c : with_zero α), a * b < a * c → b < c := begin rintro (_ | a) (_ | b) (_ | c) h; try { exact false.elim (lt_irrefl none h) }, { exact with_zero.zero_lt_coe c }, { exact false.elim (not_le_of_lt h (with_zero.zero_le _)) }, { simp_rw [some_eq_coe] at h ⊢, norm_cast at h ⊢, apply lt_of_mul_lt_mul_left' h } end instance [ordered_comm_monoid α] : ordered_comm_monoid (with_zero α) := { mul_le_mul_left := with_zero.mul_le_mul_left, lt_of_mul_lt_mul_left := with_zero.lt_of_mul_lt_mul_left, ..with_zero.comm_monoid_with_zero, ..with_zero.partial_order } /- Note 1 : the below is not an instance because it requires `zero_le`. It seems like a rather pathological definition because α already has a zero. Note 2 : there is no multiplicative analogue because it does not seem necessary. Mathematicians might be more likely to use the order-dual version, where all elements are ≤ 1 and then 1 is the top element. -/ /-- If `0` is the least element in `α`, then `with_zero α` is an `ordered_add_comm_monoid`. -/ def ordered_add_comm_monoid [ordered_add_comm_monoid α] (zero_le : ∀ a : α, 0 ≤ a) : ordered_add_comm_monoid (with_zero α) := begin suffices, refine { add_le_add_left := this, ..with_zero.partial_order, ..with_zero.add_comm_monoid, .. }, { intros a b c h, have h' := lt_iff_le_not_le.1 h, rw lt_iff_le_not_le at ⊢, refine ⟨λ b h₂, _, λ h₂, h'.2 $ this _ _ h₂ _⟩, cases h₂, cases c with c, { cases h'.2 (this _ _ bot_le a) }, { refine ⟨_, rfl, _⟩, cases a with a, { exact with_bot.some_le_some.1 h'.1 }, { exact le_of_lt (lt_of_add_lt_add_left $ with_bot.some_lt_some.1 h), } } }, { intros a b h c ca h₂, cases b with b, { rw le_antisymm h bot_le at h₂, exact ⟨_, h₂, le_refl _⟩ }, cases a with a, { change c + 0 = some ca at h₂, simp at h₂, simp [h₂], exact ⟨_, rfl, by simpa using add_le_add_left (zero_le b) _⟩ }, { simp at h, cases c with c; change some _ = _ at h₂; simp [-add_comm] at h₂; subst ca; refine ⟨_, rfl, _⟩, { exact h }, { exact add_le_add_left h _ } } } end end with_zero namespace with_top section has_one variables [has_one α] @[to_additive] instance : has_one (with_top α) := ⟨(1 : α)⟩ @[simp, to_additive] lemma coe_one : ((1 : α) : with_top α) = 1 := rfl @[simp, to_additive] lemma coe_eq_one {a : α} : (a : with_top α) = 1 ↔ a = 1 := coe_eq_coe @[simp, to_additive] theorem one_eq_coe {a : α} : 1 = (a : with_top α) ↔ a = 1 := trans eq_comm coe_eq_one attribute [norm_cast] coe_one coe_eq_one coe_zero coe_eq_zero one_eq_coe zero_eq_coe @[simp, to_additive] theorem top_ne_one : ⊤ ≠ (1 : with_top α) . @[simp, to_additive] theorem one_ne_top : (1 : with_top α) ≠ ⊤ . end has_one instance [has_add α] : has_add (with_top α) := ⟨λ o₁ o₂, o₁.bind (λ a, o₂.map (λ b, a + b))⟩ local attribute [semireducible] with_zero instance [add_semigroup α] : add_semigroup (with_top α) := { add := (+), ..(infer_instance : add_semigroup (additive (with_zero (multiplicative α)))) } @[norm_cast] lemma coe_add [has_add α] {a b : α} : ((a + b : α) : with_top α) = a + b := rfl @[norm_cast] lemma coe_bit0 [has_add α] {a : α} : ((bit0 a : α) : with_top α) = bit0 a := rfl @[norm_cast] lemma coe_bit1 [has_add α] [has_one α] {a : α} : ((bit1 a : α) : with_top α) = bit1 a := rfl @[simp] lemma add_top [has_add α] : ∀{a : with_top α}, a + ⊤ = ⊤ | none := rfl | (some a) := rfl @[simp] lemma top_add [has_add α] {a : with_top α} : ⊤ + a = ⊤ := rfl lemma add_eq_top [has_add α] {a b : with_top α} : a + b = ⊤ ↔ a = ⊤ ∨ b = ⊤ := by cases a; cases b; simp [none_eq_top, some_eq_coe, ←with_top.coe_add, ←with_zero.coe_add] lemma add_lt_top [has_add α] [partial_order α] {a b : with_top α} : a + b < ⊤ ↔ a < ⊤ ∧ b < ⊤ := by simp [lt_top_iff_ne_top, add_eq_top, not_or_distrib] lemma add_eq_coe [has_add α] : ∀ {a b : with_top α} {c : α}, a + b = c ↔ ∃ (a' b' : α), ↑a' = a ∧ ↑b' = b ∧ a' + b' = c | none b c := by simp [none_eq_top] | (some a) none c := by simp [none_eq_top] | (some a) (some b) c := by simp only [some_eq_coe, ← coe_add, coe_eq_coe, exists_and_distrib_left, exists_eq_left] instance [add_comm_semigroup α] : add_comm_semigroup (with_top α) := { ..@additive.add_comm_semigroup _ $ @with_zero.comm_semigroup (multiplicative α) _ } instance [add_monoid α] : add_monoid (with_top α) := { zero := some 0, add := (+), ..@additive.add_monoid _ $ @monoid_with_zero.to_monoid _ $ @with_zero.monoid_with_zero (multiplicative α) _ } instance [add_comm_monoid α] : add_comm_monoid (with_top α) := { zero := 0, add := (+), ..@additive.add_comm_monoid _ $ @comm_monoid_with_zero.to_comm_monoid _ $ @with_zero.comm_monoid_with_zero (multiplicative α) _ } instance [ordered_add_comm_monoid α] : ordered_add_comm_monoid (with_top α) := { add_le_add_left := begin rintros a b h (_|c), { simp [none_eq_top] }, rcases b with (_|b), { simp [none_eq_top] }, rcases le_coe_iff.1 h with ⟨a, rfl, h⟩, simp only [some_eq_coe, ← coe_add, coe_le_coe] at h ⊢, exact add_le_add_left h c end, lt_of_add_lt_add_left := begin intros a b c h, rcases lt_iff_exists_coe.1 h with ⟨ab, hab, hlt⟩, rcases add_eq_coe.1 hab with ⟨a, b, rfl, rfl, rfl⟩, rw coe_lt_iff, rintro c rfl, exact lt_of_add_lt_add_left (coe_lt_coe.1 hlt) end, ..with_top.partial_order, ..with_top.add_comm_monoid } instance [linear_ordered_add_comm_monoid α] : linear_ordered_add_comm_monoid_with_top (with_top α) := { top_add' := λ x, with_top.top_add, ..with_top.order_top, ..with_top.linear_order, ..with_top.ordered_add_comm_monoid, ..option.nontrivial } /-- Coercion from `α` to `with_top α` as an `add_monoid_hom`. -/ def coe_add_hom [add_monoid α] : α →+ with_top α := ⟨coe, rfl, λ _ _, rfl⟩ @[simp] lemma coe_coe_add_hom [add_monoid α] : ⇑(coe_add_hom : α →+ with_top α) = coe := rfl @[simp] lemma zero_lt_top [ordered_add_comm_monoid α] : (0 : with_top α) < ⊤ := coe_lt_top 0 @[simp, norm_cast] lemma zero_lt_coe [ordered_add_comm_monoid α] (a : α) : (0 : with_top α) < a ↔ 0 < a := coe_lt_coe end with_top namespace with_bot instance [has_zero α] : has_zero (with_bot α) := with_top.has_zero instance [has_one α] : has_one (with_bot α) := with_top.has_one instance [add_semigroup α] : add_semigroup (with_bot α) := with_top.add_semigroup instance [add_comm_semigroup α] : add_comm_semigroup (with_bot α) := with_top.add_comm_semigroup instance [add_monoid α] : add_monoid (with_bot α) := with_top.add_monoid instance [add_comm_monoid α] : add_comm_monoid (with_bot α) := with_top.add_comm_monoid instance [ordered_add_comm_monoid α] : ordered_add_comm_monoid (with_bot α) := begin suffices, refine { add_le_add_left := this, ..with_bot.partial_order, ..with_bot.add_comm_monoid, ..}, { intros a b c h, have h' := h, rw lt_iff_le_not_le at h' ⊢, refine ⟨λ b h₂, _, λ h₂, h'.2 $ this _ _ h₂ _⟩, cases h₂, cases a with a, { exact (not_le_of_lt h).elim bot_le }, cases c with c, { exact (not_le_of_lt h).elim bot_le }, { exact ⟨_, rfl, le_of_lt (lt_of_add_lt_add_left $ with_bot.some_lt_some.1 h)⟩ } }, { intros a b h c ca h₂, cases c with c, {cases h₂}, cases a with a; cases h₂, cases b with b, {cases le_antisymm h bot_le}, simp at h, exact ⟨_, rfl, add_le_add_left h _⟩, } end instance [linear_ordered_add_comm_monoid α] : linear_ordered_add_comm_monoid (with_bot α) := { ..with_bot.linear_order, ..with_bot.ordered_add_comm_monoid } -- `by norm_cast` proves this lemma, so I did not tag it with `norm_cast` lemma coe_zero [has_zero α] : ((0 : α) : with_bot α) = 0 := rfl -- `by norm_cast` proves this lemma, so I did not tag it with `norm_cast` lemma coe_one [has_one α] : ((1 : α) : with_bot α) = 1 := rfl -- `by norm_cast` proves this lemma, so I did not tag it with `norm_cast` lemma coe_eq_zero {α : Type*} [add_monoid α] {a : α} : (a : with_bot α) = 0 ↔ a = 0 := by norm_cast -- `by norm_cast` proves this lemma, so I did not tag it with `norm_cast` lemma coe_add [add_semigroup α] (a b : α) : ((a + b : α) : with_bot α) = a + b := by norm_cast -- `by norm_cast` proves this lemma, so I did not tag it with `norm_cast` lemma coe_bit0 [add_semigroup α] {a : α} : ((bit0 a : α) : with_bot α) = bit0 a := by norm_cast -- `by norm_cast` proves this lemma, so I did not tag it with `norm_cast` lemma coe_bit1 [add_semigroup α] [has_one α] {a : α} : ((bit1 a : α) : with_bot α) = bit1 a := by norm_cast @[simp] lemma bot_add [add_semigroup α] (a : with_bot α) : ⊥ + a = ⊥ := rfl @[simp] lemma add_bot [add_semigroup α] (a : with_bot α) : a + ⊥ = ⊥ := by cases a; refl @[simp] lemma add_eq_bot [add_semigroup α] {m n : with_bot α} : m + n = ⊥ ↔ m = ⊥ ∨ n = ⊥ := with_top.add_eq_top end with_bot /-- A canonically ordered additive monoid is an ordered commutative additive monoid in which the ordering coincides with the subtractibility relation, which is to say, `a ≤ b` iff there exists `c` with `b = a + c`. This is satisfied by the natural numbers, for example, but not the integers or other nontrivial `ordered_add_comm_group`s. -/ @[protect_proj, ancestor ordered_add_comm_monoid order_bot] class canonically_ordered_add_monoid (α : Type*) extends ordered_add_comm_monoid α, order_bot α := (le_iff_exists_add : ∀ a b : α, a ≤ b ↔ ∃ c, b = a + c) /-- A canonically ordered monoid is an ordered commutative monoid in which the ordering coincides with the divisibility relation, which is to say, `a ≤ b` iff there exists `c` with `b = a * c`. Examples seem rare; it seems more likely that the `order_dual` of a naturally-occurring lattice satisfies this than the lattice itself (for example, dual of the lattice of ideals of a PID or Dedekind domain satisfy this; collections of all things ≤ 1 seem to be more natural that collections of all things ≥ 1). -/ @[protect_proj, ancestor ordered_comm_monoid order_bot, to_additive] class canonically_ordered_monoid (α : Type*) extends ordered_comm_monoid α, order_bot α := (le_iff_exists_mul : ∀ a b : α, a ≤ b ↔ ∃ c, b = a * c) section canonically_ordered_monoid variables [canonically_ordered_monoid α] {a b c d : α} @[to_additive] lemma le_iff_exists_mul : a ≤ b ↔ ∃c, b = a * c := canonically_ordered_monoid.le_iff_exists_mul a b @[to_additive] lemma self_le_mul_right (a b : α) : a ≤ a * b := le_iff_exists_mul.mpr ⟨b, rfl⟩ @[to_additive] lemma self_le_mul_left (a b : α) : a ≤ b * a := by { rw [mul_comm], exact self_le_mul_right a b } @[simp, to_additive zero_le] lemma one_le (a : α) : 1 ≤ a := le_iff_exists_mul.mpr ⟨a, (one_mul _).symm⟩ @[simp, to_additive] lemma bot_eq_one : (⊥ : α) = 1 := le_antisymm bot_le (one_le ⊥) @[simp, to_additive] lemma mul_eq_one_iff : a * b = 1 ↔ a = 1 ∧ b = 1 := mul_eq_one_iff' (one_le _) (one_le _) @[simp, to_additive] lemma le_one_iff_eq_one : a ≤ 1 ↔ a = 1 := iff.intro (assume h, le_antisymm h (one_le a)) (assume h, h ▸ le_refl a) @[to_additive] lemma one_lt_iff_ne_one : 1 < a ↔ a ≠ 1 := iff.intro ne_of_gt $ assume hne, lt_of_le_of_ne (one_le _) hne.symm @[to_additive] lemma exists_pos_mul_of_lt (h : a < b) : ∃ c > 1, a * c = b := begin obtain ⟨c, hc⟩ := le_iff_exists_mul.1 h.le, refine ⟨c, one_lt_iff_ne_one.2 _, hc.symm⟩, rintro rfl, simpa [hc, lt_irrefl] using h end @[to_additive] lemma le_mul_left (h : a ≤ c) : a ≤ b * c := calc a = 1 * a : by simp ... ≤ b * c : mul_le_mul' (one_le _) h @[to_additive] lemma le_mul_self : a ≤ b * a := le_mul_left (le_refl a) @[to_additive] lemma le_mul_right (h : a ≤ b) : a ≤ b * c := calc a = a * 1 : by simp ... ≤ b * c : mul_le_mul' h (one_le _) @[to_additive] lemma le_self_mul : a ≤ a * c := le_mul_right (le_refl a) local attribute [semireducible] with_zero -- This instance looks absurd: a monoid already has a zero /-- Adding a new zero to a canonically ordered additive monoid produces another one. -/ instance with_zero.canonically_ordered_add_monoid {α : Type u} [canonically_ordered_add_monoid α] : canonically_ordered_add_monoid (with_zero α) := { le_iff_exists_add := λ a b, begin cases a with a, { exact iff_of_true bot_le ⟨b, (zero_add b).symm⟩ }, cases b with b, { exact iff_of_false (mt (le_antisymm bot_le) (by simp)) (λ ⟨c, h⟩, by cases c; cases h) }, { simp [le_iff_exists_add, -add_comm], split; intro h; rcases h with ⟨c, h⟩, { exact ⟨some c, congr_arg some h⟩ }, { cases c; cases h, { exact ⟨_, (add_zero _).symm⟩ }, { exact ⟨_, rfl⟩ } } } end, bot := 0, bot_le := assume a a' h, option.no_confusion h, .. with_zero.ordered_add_comm_monoid zero_le } instance with_top.canonically_ordered_add_monoid {α : Type u} [canonically_ordered_add_monoid α] : canonically_ordered_add_monoid (with_top α) := { le_iff_exists_add := assume a b, match a, b with | a, none := show a ≤ ⊤ ↔ ∃c, ⊤ = a + c, by simp; refine ⟨⊤, _⟩; cases a; refl | (some a), (some b) := show (a:with_top α) ≤ ↑b ↔ ∃c:with_top α, ↑b = ↑a + c, begin simp [canonically_ordered_add_monoid.le_iff_exists_add, -add_comm], split, { rintro ⟨c, rfl⟩, refine ⟨c, _⟩, norm_cast }, { exact assume h, match b, h with _, ⟨some c, rfl⟩ := ⟨_, rfl⟩ end } end | none, some b := show (⊤ : with_top α) ≤ b ↔ ∃c:with_top α, ↑b = ⊤ + c, by simp end, .. with_top.order_bot, .. with_top.ordered_add_comm_monoid } @[priority 100, to_additive] instance canonically_ordered_monoid.has_exists_mul_of_le (α : Type u) [canonically_ordered_monoid α] : has_exists_mul_of_le α := { exists_mul_of_le := λ a b hab, le_iff_exists_mul.mp hab } end canonically_ordered_monoid lemma pos_of_gt {M : Type*} [canonically_ordered_add_monoid M] {n m : M} (h : n < m) : 0 < m := lt_of_le_of_lt (zero_le _) h /-- A canonically linear-ordered additive monoid is a canonically ordered additive monoid whose ordering is a linear order. -/ @[protect_proj, ancestor canonically_ordered_add_monoid linear_order] class canonically_linear_ordered_add_monoid (α : Type*) extends canonically_ordered_add_monoid α, linear_order α /-- A canonically linear-ordered monoid is a canonically ordered monoid whose ordering is a linear order. -/ @[protect_proj, ancestor canonically_ordered_monoid linear_order, to_additive] class canonically_linear_ordered_monoid (α : Type*) extends canonically_ordered_monoid α, linear_order α section canonically_linear_ordered_monoid variables @[priority 100, to_additive] -- see Note [lower instance priority] instance canonically_linear_ordered_monoid.semilattice_sup_bot [canonically_linear_ordered_monoid α] : semilattice_sup_bot α := { ..lattice_of_linear_order, ..canonically_ordered_monoid.to_order_bot α } instance with_top.canonically_linear_ordered_add_monoid (α : Type*) [canonically_linear_ordered_add_monoid α] : canonically_linear_ordered_add_monoid (with_top α) := { .. (infer_instance : canonically_ordered_add_monoid (with_top α)), .. (infer_instance : linear_order (with_top α)) } @[to_additive] lemma min_mul_distrib [canonically_linear_ordered_monoid α] (a b c : α) : min a (b * c) = min a (min a b * min a c) := begin cases le_total a b with hb hb, { simp [hb, le_mul_right] }, { cases le_total a c with hc hc, { simp [hc, le_mul_left] }, { simp [hb, hc] } } end @[to_additive] lemma min_mul_distrib' [canonically_linear_ordered_monoid α] (a b c : α) : min (a * b) c = min (min a c * min b c) c := by simpa [min_comm _ c] using min_mul_distrib c a b end canonically_linear_ordered_monoid /-- An ordered cancellative additive commutative monoid is an additive commutative monoid with a partial order, in which addition is cancellative and monotone. -/ @[protect_proj, ancestor add_cancel_comm_monoid partial_order] class ordered_cancel_add_comm_monoid (α : Type u) extends add_cancel_comm_monoid α, partial_order α := (add_le_add_left : ∀ a b : α, a ≤ b → ∀ c : α, c + a ≤ c + b) (le_of_add_le_add_left : ∀ a b c : α, a + b ≤ a + c → b ≤ c) /-- An ordered cancellative commutative monoid is a commutative monoid with a partial order, in which multiplication is cancellative and monotone. -/ @[protect_proj, ancestor cancel_comm_monoid partial_order, to_additive] class ordered_cancel_comm_monoid (α : Type u) extends cancel_comm_monoid α, partial_order α := (mul_le_mul_left : ∀ a b : α, a ≤ b → ∀ c : α, c * a ≤ c * b) (le_of_mul_le_mul_left : ∀ a b c : α, a * b ≤ a * c → b ≤ c) section ordered_cancel_comm_monoid variables [ordered_cancel_comm_monoid α] {a b c d : α} @[priority 100, to_additive] -- see Note [lower instance priority] instance ordered_cancel_comm_monoid.to_ordered_comm_monoid : ordered_comm_monoid α := { lt_of_mul_lt_mul_left := λ a b c h, lt_of_le_not_le (ordered_cancel_comm_monoid.le_of_mul_le_mul_left a b c h.le) $ mt (λ h, ordered_cancel_comm_monoid.mul_le_mul_left _ _ h _) (not_le_of_gt h), ..‹ordered_cancel_comm_monoid α› } /-- Pullback an `ordered_cancel_comm_monoid` under an injective map. See note [reducible non-instances]. -/ @[reducible, to_additive function.injective.ordered_cancel_add_comm_monoid "Pullback an `ordered_cancel_add_comm_monoid` under an injective map."] def function.injective.ordered_cancel_comm_monoid {β : Type*} [has_one β] [has_mul β] (f : β → α) (hf : function.injective f) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) : ordered_cancel_comm_monoid β := { le_of_mul_le_mul_left := λ a b c (bc : f (a * b) ≤ f (a * c)), (mul_le_mul_iff_left (f a)).mp (by rwa [← mul, ← mul]), ..hf.left_cancel_semigroup f mul, ..hf.ordered_comm_monoid f one mul } end ordered_cancel_comm_monoid section ordered_cancel_add_comm_monoid variable [ordered_cancel_add_comm_monoid α] lemma with_top.add_lt_add_iff_left : ∀{a b c : with_top α}, a < ⊤ → (a + c < a + b ↔ c < b) | none := assume b c h, (lt_irrefl ⊤ h).elim | (some a) := begin assume b c h, cases b; cases c; simp [with_top.none_eq_top, with_top.some_eq_coe, with_top.coe_lt_top, with_top.coe_lt_coe], { norm_cast, exact with_top.coe_lt_top _ }, { norm_cast, exact add_lt_add_iff_left _ } end lemma with_bot.add_lt_add_iff_left : ∀{a b c : with_bot α}, ⊥ < a → (a + c < a + b ↔ c < b) | none := assume b c h, (lt_irrefl ⊥ h).elim | (some a) := begin assume b c h, cases b; cases c; simp [with_bot.none_eq_bot, with_bot.some_eq_coe, with_bot.bot_lt_coe, with_bot.coe_lt_coe], { norm_cast, exact with_bot.bot_lt_coe _ }, { norm_cast, exact add_lt_add_iff_left _ } end local attribute [semireducible] with_zero lemma with_top.add_lt_add_iff_right {a b c : with_top α} : a < ⊤ → (c + a < b + a ↔ c < b) := by simpa [add_comm] using @with_top.add_lt_add_iff_left _ _ a b c lemma with_bot.add_lt_add_iff_right {a b c : with_bot α} : ⊥ < a → (c + a < b + a ↔ c < b) := by simpa [add_comm] using @with_bot.add_lt_add_iff_left _ _ a b c end ordered_cancel_add_comm_monoid /-! Some lemmas about types that have an ordering and a binary operation, with no rules relating them. -/ @[to_additive] lemma fn_min_mul_fn_max {β} [linear_order α] [comm_semigroup β] (f : α → β) (n m : α) : f (min n m) * f (max n m) = f n * f m := by { cases le_total n m with h h; simp [h, mul_comm] } @[to_additive] lemma min_mul_max [linear_order α] [comm_semigroup α] (n m : α) : min n m * max n m = n * m := fn_min_mul_fn_max id n m /-- A linearly ordered cancellative additive commutative monoid is an additive commutative monoid with a decidable linear order in which addition is cancellative and monotone. -/ @[protect_proj, ancestor ordered_cancel_add_comm_monoid linear_ordered_add_comm_monoid] class linear_ordered_cancel_add_comm_monoid (α : Type u) extends ordered_cancel_add_comm_monoid α, linear_ordered_add_comm_monoid α /-- A linearly ordered cancellative commutative monoid is a commutative monoid with a linear order in which multiplication is cancellative and monotone. -/ @[protect_proj, ancestor ordered_cancel_comm_monoid linear_ordered_comm_monoid, to_additive] class linear_ordered_cancel_comm_monoid (α : Type u) extends ordered_cancel_comm_monoid α, linear_ordered_comm_monoid α section covariant_class_mul_le variables [cancel_comm_monoid α] [linear_order α] [covariant_class α α (*) (≤)] @[to_additive] lemma min_mul_mul_left (a b c : α) : min (a * b) (a * c) = a * min b c := (monotone_id.const_mul' a).map_min.symm @[to_additive] lemma min_mul_mul_right (a b c : α) : min (a * c) (b * c) = min a b * c := (monotone_id.mul_const' c).map_min.symm @[to_additive] lemma max_mul_mul_left (a b c : α) : max (a * b) (a * c) = a * max b c := (monotone_id.const_mul' a).map_max.symm @[to_additive] lemma max_mul_mul_right (a b c : α) : max (a * c) (b * c) = max a b * c := (monotone_id.mul_const' c).map_max.symm @[to_additive] lemma min_le_mul_of_one_le_right {a b : α} (hb : 1 ≤ b) : min a b ≤ a * b := min_le_iff.2 $ or.inl $ le_mul_of_one_le_right' hb @[to_additive] lemma min_le_mul_of_one_le_left {a b : α} (ha : 1 ≤ a) : min a b ≤ a * b := min_le_iff.2 $ or.inr $ le_mul_of_one_le_left' ha @[to_additive] lemma max_le_mul_of_one_le {a b : α} (ha : 1 ≤ a) (hb : 1 ≤ b) : max a b ≤ a * b := max_le_iff.2 ⟨le_mul_of_one_le_right' hb, le_mul_of_one_le_left' ha⟩ end covariant_class_mul_le section linear_ordered_cancel_comm_monoid variables [linear_ordered_cancel_comm_monoid α] /-- Pullback a `linear_ordered_cancel_comm_monoid` under an injective map. See note [reducible non-instances]. -/ @[reducible, to_additive function.injective.linear_ordered_cancel_add_comm_monoid "Pullback a `linear_ordered_cancel_add_comm_monoid` under an injective map."] def function.injective.linear_ordered_cancel_comm_monoid {β : Type*} [has_one β] [has_mul β] (f : β → α) (hf : function.injective f) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) : linear_ordered_cancel_comm_monoid β := { ..hf.linear_ordered_comm_monoid f one mul, ..hf.ordered_cancel_comm_monoid f one mul } end linear_ordered_cancel_comm_monoid namespace order_dual @[to_additive] instance [h : has_mul α] : has_mul (order_dual α) := h @[to_additive] instance [ordered_comm_monoid α] : ordered_comm_monoid (order_dual α) := { mul_le_mul_left := λ a b h c, show (id c : α) * b ≤ c * a, from mul_le_mul_left' h _, lt_of_mul_lt_mul_left := λ a b c h, by apply lt_of_mul_lt_mul_left' (by convert h : (id a : α) * c < a * b), ..order_dual.partial_order α, ..show comm_monoid α, by apply_instance } @[to_additive ordered_cancel_add_comm_monoid.to_contravariant_class] instance ordered_cancel_comm_monoid.to_contravariant_class [ordered_cancel_comm_monoid α] : contravariant_class (order_dual α) (order_dual α) has_mul.mul has_le.le := { elim := λ a b c bc, (ordered_cancel_comm_monoid.le_of_mul_le_mul_left a c b (dual_le.mp bc)) } @[to_additive] instance [ordered_cancel_comm_monoid α] : ordered_cancel_comm_monoid (order_dual α) := { le_of_mul_le_mul_left := λ a b c : α, le_of_mul_le_mul_left', mul_left_cancel := @mul_left_cancel α _, ..order_dual.ordered_comm_monoid } @[to_additive] instance [linear_ordered_cancel_comm_monoid α] : linear_ordered_cancel_comm_monoid (order_dual α) := { .. order_dual.linear_order α, .. order_dual.ordered_cancel_comm_monoid } @[to_additive] instance [linear_ordered_comm_monoid α] : linear_ordered_comm_monoid (order_dual α) := { .. order_dual.linear_order α, .. order_dual.ordered_comm_monoid } end order_dual namespace prod variables {M N : Type*} @[to_additive] instance [ordered_cancel_comm_monoid M] [ordered_cancel_comm_monoid N] : ordered_cancel_comm_monoid (M × N) := { mul_le_mul_left := λ a b h c, ⟨mul_le_mul_left' h.1 _, mul_le_mul_left' h.2 _⟩, le_of_mul_le_mul_left := λ a b c h, ⟨le_of_mul_le_mul_left' h.1, le_of_mul_le_mul_left' h.2⟩, .. prod.cancel_comm_monoid, .. prod.partial_order M N } end prod section type_tags instance : Π [preorder α], preorder (multiplicative α) := id instance : Π [preorder α], preorder (additive α) := id instance : Π [partial_order α], partial_order (multiplicative α) := id instance : Π [partial_order α], partial_order (additive α) := id instance : Π [linear_order α], linear_order (multiplicative α) := id instance : Π [linear_order α], linear_order (additive α) := id instance [ordered_add_comm_monoid α] : ordered_comm_monoid (multiplicative α) := { mul_le_mul_left := @ordered_add_comm_monoid.add_le_add_left α _, lt_of_mul_lt_mul_left := @ordered_add_comm_monoid.lt_of_add_lt_add_left α _, ..multiplicative.partial_order, ..multiplicative.comm_monoid } instance [ordered_comm_monoid α] : ordered_add_comm_monoid (additive α) := { add_le_add_left := @ordered_comm_monoid.mul_le_mul_left α _, lt_of_add_lt_add_left := @ordered_comm_monoid.lt_of_mul_lt_mul_left α _, ..additive.partial_order, ..additive.add_comm_monoid } instance [ordered_cancel_add_comm_monoid α] : ordered_cancel_comm_monoid (multiplicative α) := { le_of_mul_le_mul_left := @ordered_cancel_add_comm_monoid.le_of_add_le_add_left α _, ..multiplicative.left_cancel_semigroup, ..multiplicative.ordered_comm_monoid } instance [ordered_cancel_comm_monoid α] : ordered_cancel_add_comm_monoid (additive α) := { le_of_add_le_add_left := @ordered_cancel_comm_monoid.le_of_mul_le_mul_left α _, ..additive.add_left_cancel_semigroup, ..additive.ordered_add_comm_monoid } instance [linear_ordered_add_comm_monoid α] : linear_ordered_comm_monoid (multiplicative α) := { ..multiplicative.linear_order, ..multiplicative.ordered_comm_monoid } instance [linear_ordered_comm_monoid α] : linear_ordered_add_comm_monoid (additive α) := { ..additive.linear_order, ..additive.ordered_add_comm_monoid } instance [sub_neg_monoid α] : sub_neg_monoid (order_dual α) := { ..show sub_neg_monoid α, from infer_instance } instance [div_inv_monoid α] : div_inv_monoid (order_dual α) := { ..show div_inv_monoid α, from infer_instance } end type_tags
1e7463cb19b871a35c61dcacc79eedca0597f0d0
fe25de614feb5587799621c41487aaee0d083b08
/src/Lean/Elab/Tactic/ElabTerm.lean
8799f08b450ffff770c82fafc696a2275a034805
[ "Apache-2.0" ]
permissive
pollend/lean4
e8469c2f5fb8779b773618c3267883cf21fb9fac
c913886938c4b3b83238a3f99673c6c5a9cec270
refs/heads/master
1,687,973,251,481
1,628,039,739,000
1,628,039,739,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
10,803
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Meta.CollectMVars import Lean.Meta.Tactic.Apply import Lean.Meta.Tactic.Constructor import Lean.Meta.Tactic.Assert import Lean.Elab.Tactic.Basic import Lean.Elab.SyntheticMVars namespace Lean.Elab.Tactic open Meta /- `elabTerm` for Tactics and basic tactics that use it. -/ def elabTerm (stx : Syntax) (expectedType? : Option Expr) (mayPostpone := false) : TacticM Expr := do /- We have disabled `Term.withoutErrToSorry` to improve error recovery. When we were using it, any tactic using `elabTerm` would be interrupted at elaboration errors. Tactics that do not want to proceed should check whether the result contains sythetic sorrys or disable `errToSorry` before invoking `elabTerm` -/ withRef stx do -- <| Term.withoutErrToSorry do let e ← Term.elabTerm stx expectedType? Term.synthesizeSyntheticMVars mayPostpone instantiateMVars e def elabTermEnsuringType (stx : Syntax) (expectedType? : Option Expr) (mayPostpone := false) : TacticM Expr := do let e ← elabTerm stx expectedType? mayPostpone -- We do use `Term.ensureExpectedType` because we don't want coercions being inserted here. match expectedType? with | none => return e | some expectedType => let eType ← inferType e -- We allow synthetic opaque metavars to be assigned in the following step since the `isDefEq` is not really -- part of the elaboration, but part of the tactic. See issue #492 unless (← withAssignableSyntheticOpaque do isDefEq eType expectedType) do Term.throwTypeMismatchError none expectedType eType e return e /- Try to close main goal using `x target`, where `target` is the type of the main goal. -/ def closeMainGoalUsing (x : Expr → TacticM Expr) (checkUnassigned := true) : TacticM Unit := withMainContext do closeMainGoal (checkUnassigned := checkUnassigned) (← x (← getMainTarget)) private def logUnassignedAndAbort (mvarIds : Array MVarId) : TacticM Unit := do if (← Term.logUnassignedUsingErrorInfos mvarIds) then throwAbortTactic private def filterOldMVars (mvarIds : Array MVarId) (mvarCounterSaved : Nat) : MetaM (Array MVarId) := do let mctx ← getMCtx return mvarIds.filter fun mvarId => (mctx.getDecl mvarId |>.index) >= mvarCounterSaved @[builtinTactic «exact»] def evalExact : Tactic := fun stx => match stx with | `(tactic| exact $e) => closeMainGoalUsing (checkUnassigned := false) fun type => do let mvarCounterSaved := (← getMCtx).mvarCounter let r ← elabTermEnsuringType e type logUnassignedAndAbort (← filterOldMVars (← getMVars r) mvarCounterSaved) return r | _ => throwUnsupportedSyntax def elabTermWithHoles (stx : Syntax) (expectedType? : Option Expr) (tagSuffix : Name) (allowNaturalHoles := false) : TacticM (Expr × List MVarId) := do let mvarCounterSaved := (← getMCtx).mvarCounter let val ← elabTermEnsuringType stx expectedType? let newMVarIds ← getMVarsNoDelayed val /- ignore let-rec auxiliary variables, they are synthesized automatically later -/ let newMVarIds ← newMVarIds.filterM fun mvarId => return !(← Term.isLetRecAuxMVar mvarId) let newMVarIds ← if allowNaturalHoles then pure newMVarIds.toList else let naturalMVarIds ← newMVarIds.filterM fun mvarId => return (← getMVarDecl mvarId).kind.isNatural let syntheticMVarIds ← newMVarIds.filterM fun mvarId => return !(← getMVarDecl mvarId).kind.isNatural let naturalMVarIds ← filterOldMVars naturalMVarIds mvarCounterSaved logUnassignedAndAbort naturalMVarIds pure syntheticMVarIds.toList tagUntaggedGoals (← getMainTag) tagSuffix newMVarIds pure (val, newMVarIds) /- If `allowNaturalHoles == true`, then we allow the resultant expression to contain unassigned "natural" metavariables. Recall that "natutal" metavariables are created for explicit holes `_` and implicit arguments. They are meant to be filled by typing constraints. "Synthetic" metavariables are meant to be filled by tactics and are usually created using the synthetic hole notation `?<hole-name>`. -/ def refineCore (stx : Syntax) (tagSuffix : Name) (allowNaturalHoles : Bool) : TacticM Unit := do withMainContext do let (val, mvarIds') ← elabTermWithHoles stx (← getMainTarget) tagSuffix allowNaturalHoles assignExprMVar (← getMainGoal) val replaceMainGoal mvarIds' @[builtinTactic «refine»] def evalRefine : Tactic := fun stx => match stx with | `(tactic| refine $e) => refineCore e `refine (allowNaturalHoles := false) | _ => throwUnsupportedSyntax @[builtinTactic «refine'»] def evalRefine' : Tactic := fun stx => match stx with | `(tactic| refine' $e) => refineCore e `refine' (allowNaturalHoles := true) | _ => throwUnsupportedSyntax /-- Given a tactic ``` apply f ``` we want the `apply` tactic to create all metavariables. The following definition will return `@f` for `f`. That is, it will **not** create metavariables for implicit arguments. A similar method is also used in Lean 3. This method is useful when applying lemmas such as: ``` theorem infLeRight {s t : Set α} : s ⊓ t ≤ t ``` where `s ≤ t` here is defined as ``` ∀ {x : α}, x ∈ s → x ∈ t ``` -/ def elabTermForApply (stx : Syntax) : TacticM Expr := do if stx.isIdent then match (← Term.resolveId? stx (withInfo := true)) with | some e => return e | _ => pure () elabTerm stx none (mayPostpone := true) def evalApplyLikeTactic (tac : MVarId → Expr → MetaM (List MVarId)) (e : Syntax) : TacticM Unit := do withMainContext do let val ← elabTermForApply e let mvarIds' ← tac (← getMainGoal) val Term.synthesizeSyntheticMVarsNoPostponing replaceMainGoal mvarIds' @[builtinTactic Lean.Parser.Tactic.apply] def evalApply : Tactic := fun stx => match stx with | `(tactic| apply $e) => evalApplyLikeTactic Meta.apply e | _ => throwUnsupportedSyntax @[builtinTactic Lean.Parser.Tactic.constructor] def evalConstructor : Tactic := fun stx => withMainContext do let mvarIds' ← Meta.constructor (← getMainGoal) Term.synthesizeSyntheticMVarsNoPostponing replaceMainGoal mvarIds' @[builtinTactic Lean.Parser.Tactic.existsIntro] def evalExistsIntro : Tactic := fun stx => match stx with | `(tactic| exists $e) => evalApplyLikeTactic (fun mvarId e => return [(← Meta.existsIntro mvarId e)]) e | _ => throwUnsupportedSyntax @[builtinTactic Lean.Parser.Tactic.withReducible] def evalWithReducible : Tactic := fun stx => withReducible <| evalTactic stx[1] @[builtinTactic Lean.Parser.Tactic.withReducibleAndInstances] def evalWithReducibleAndInstances : Tactic := fun stx => withReducibleAndInstances <| evalTactic stx[1] /-- Elaborate `stx`. If it a free variable, return it. Otherwise, assert it, and return the free variable. Note that, the main goal is updated when `Meta.assert` is used in the second case. -/ def elabAsFVar (stx : Syntax) (userName? : Option Name := none) : TacticM FVarId := withMainContext do let e ← elabTerm stx none match e with | Expr.fvar fvarId _ => pure fvarId | _ => let type ← inferType e let intro (userName : Name) (preserveBinderNames : Bool) : TacticM FVarId := do let mvarId ← getMainGoal let (fvarId, mvarId) ← liftMetaM do let mvarId ← Meta.assert mvarId userName type e Meta.intro1Core mvarId preserveBinderNames replaceMainGoal [mvarId] return fvarId match userName? with | none => intro `h false | some userName => intro userName true @[builtinTactic Lean.Parser.Tactic.rename] def evalRename : Tactic := fun stx => match stx with | `(tactic| rename $typeStx:term => $h:ident) => do withMainContext do let fvarId ← withoutModifyingState <| withNewMCtxDepth do let type ← elabTerm typeStx none (mayPostpone := true) let fvarId? ← (← getLCtx).findDeclRevM? fun localDecl => do if (← isDefEq type localDecl.type) then return localDecl.fvarId else return none match fvarId? with | none => throwError "failed to find a hypothesis with type{indentExpr type}" | some fvarId => return fvarId let lctxNew := (← getLCtx).setUserName fvarId h.getId let mvarNew ← mkFreshExprMVarAt lctxNew (← getLocalInstances) (← getMainTarget) MetavarKind.syntheticOpaque (← getMainTag) assignExprMVar (← getMainGoal) mvarNew replaceMainGoal [mvarNew.mvarId!] | _ => throwUnsupportedSyntax /-- Make sure `expectedType` does not contain free and metavariables. It applies zeta-reduction to eliminate let-free-vars. -/ private def preprocessPropToDecide (expectedType : Expr) : TermElabM Expr := do let mut expectedType ← instantiateMVars expectedType if expectedType.hasFVar then expectedType ← zetaReduce expectedType if expectedType.hasFVar || expectedType.hasMVar then throwError "expected type must not contain free or meta variables{indentExpr expectedType}" return expectedType @[builtinTactic Lean.Parser.Tactic.decide] def evalDecide : Tactic := fun stx => closeMainGoalUsing fun expectedType => do let expectedType ← preprocessPropToDecide expectedType let d ← mkDecide expectedType let d ← instantiateMVars d let r ← withDefault <| whnf d unless r.isConstOf ``true do throwError "failed to reduce to 'true'{indentExpr r}" let s := d.appArg! -- get instance from `d` let rflPrf ← mkEqRefl (toExpr true) return mkApp3 (Lean.mkConst `ofDecideEqTrue) expectedType s rflPrf private def mkNativeAuxDecl (baseName : Name) (type val : Expr) : TermElabM Name := do let auxName ← Term.mkAuxName baseName let decl := Declaration.defnDecl { name := auxName, levelParams := [], type := type, value := val, hints := ReducibilityHints.abbrev, safety := DefinitionSafety.safe } addDecl decl compileDecl decl pure auxName @[builtinTactic Lean.Parser.Tactic.nativeDecide] def evalNativeDecide : Tactic := fun stx => closeMainGoalUsing fun expectedType => do let expectedType ← preprocessPropToDecide expectedType let d ← mkDecide expectedType let auxDeclName ← mkNativeAuxDecl `_nativeDecide (Lean.mkConst `Bool) d let rflPrf ← mkEqRefl (toExpr true) let s := d.appArg! -- get instance from `d` return mkApp3 (Lean.mkConst `ofDecideEqTrue) expectedType s <| mkApp3 (Lean.mkConst `Lean.ofReduceBool) (Lean.mkConst auxDeclName) (toExpr true) rflPrf end Lean.Elab.Tactic
84fd914a99117381c92cc026bf9c465a33f17170
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/polynomial/algebra_map_auto.lean
5958fc7783c9a9ff53cfba8bb7a2b0edc529f2dd
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
10,647
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.polynomial.eval import Mathlib.algebra.algebra.tower import Mathlib.PostPort universes u z u_1 u_2 u_3 v namespace Mathlib /-! # Theory of univariate polynomials We show that `polynomial A` is an R-algebra when `A` is an R-algebra. We promote `eval₂` to an algebra hom in `aeval`. -/ namespace polynomial /-- Note that this instance also provides `algebra R (polynomial R)`. -/ protected instance algebra_of_algebra {R : Type u} {A : Type z} [comm_semiring R] [semiring A] [algebra R A] : algebra R (polynomial A) := add_monoid_algebra.algebra theorem algebra_map_apply {R : Type u} {A : Type z} [comm_semiring R] [semiring A] [algebra R A] (r : R) : coe_fn (algebra_map R (polynomial A)) r = coe_fn C (coe_fn (algebra_map R A) r) := rfl /-- When we have `[comm_ring R]`, the function `C` is the same as `algebra_map R (polynomial R)`. (But note that `C` is defined when `R` is not necessarily commutative, in which case `algebra_map` is not available.) -/ theorem C_eq_algebra_map {R : Type u_1} [comm_ring R] (r : R) : coe_fn C r = coe_fn (algebra_map R (polynomial R)) r := rfl @[simp] theorem alg_hom_eval₂_algebra_map {R : Type u_1} {A : Type u_2} {B : Type u_3} [comm_ring R] [ring A] [ring B] [algebra R A] [algebra R B] (p : polynomial R) (f : alg_hom R A B) (a : A) : coe_fn f (eval₂ (algebra_map R A) a p) = eval₂ (algebra_map R B) (coe_fn f a) p := sorry @[simp] theorem eval₂_algebra_map_X {R : Type u_1} {A : Type u_2} [comm_ring R] [ring A] [algebra R A] (p : polynomial R) (f : alg_hom R (polynomial R) A) : eval₂ (algebra_map R A) (coe_fn f X) p = coe_fn f p := sorry @[simp] theorem ring_hom_eval₂_algebra_map_int {R : Type u_1} {S : Type u_2} [ring R] [ring S] (p : polynomial ℤ) (f : R →+* S) (r : R) : coe_fn f (eval₂ (algebra_map ℤ R) r p) = eval₂ (algebra_map ℤ S) (coe_fn f r) p := alg_hom_eval₂_algebra_map p (ring_hom.to_int_alg_hom f) r @[simp] theorem eval₂_algebra_map_int_X {R : Type u_1} [ring R] (p : polynomial ℤ) (f : polynomial ℤ →+* R) : eval₂ (algebra_map ℤ R) (coe_fn f X) p = coe_fn f p := sorry -- Unfortunately `f.to_int_alg_hom` doesn't work here, as typeclasses don't match up correctly. theorem eval₂_comp {R : Type u} {S : Type v} [comm_semiring R] {p : polynomial R} {q : polynomial R} [comm_semiring S] (f : R →+* S) {x : S} : eval₂ f x (comp p q) = eval₂ f (eval₂ f x q) p := sorry theorem eval_comp {R : Type u} {a : R} [comm_semiring R] {p : polynomial R} {q : polynomial R} : eval a (comp p q) = eval (eval a q) p := eval₂_comp (ring_hom.id R) protected instance comp.is_semiring_hom {R : Type u} [comm_semiring R] {p : polynomial R} : is_semiring_hom fun (q : polynomial R) => comp q p := eq.mpr (id ((fun (f f_1 : polynomial R → polynomial R) (e_3 : f = f_1) => congr_arg is_semiring_hom e_3) (fun (q : polynomial R) => comp q p) (fun (q : polynomial R) => eval₂ C p q) (funext fun (q : polynomial R) => comp.equations._eqn_1 q p))) (eval₂.is_semiring_hom C p) /-- Given a valuation `x` of the variable in an `R`-algebra `A`, `aeval R A x` is the unique `R`-algebra homomorphism from `R[X]` to `A` sending `X` to `x`. -/ def aeval {R : Type u} {A : Type z} [comm_semiring R] [semiring A] [algebra R A] (x : A) : alg_hom R (polynomial R) A := alg_hom.mk (ring_hom.to_fun (eval₂_ring_hom' (algebra_map R A) x sorry)) sorry sorry sorry sorry sorry theorem alg_hom_ext {R : Type u} {A : Type z} [comm_semiring R] [semiring A] [algebra R A] {f : alg_hom R (polynomial R) A} {g : alg_hom R (polynomial R) A} (h : coe_fn f X = coe_fn g X) : f = g := add_monoid_algebra.alg_hom_ext' (monoid_hom.ext_mnat h) theorem aeval_def {R : Type u} {A : Type z} [comm_semiring R] [semiring A] [algebra R A] (x : A) (p : polynomial R) : coe_fn (aeval x) p = eval₂ (algebra_map R A) x p := rfl @[simp] theorem aeval_zero {R : Type u} {A : Type z} [comm_semiring R] [semiring A] [algebra R A] (x : A) : coe_fn (aeval x) 0 = 0 := alg_hom.map_zero (aeval x) @[simp] theorem aeval_X {R : Type u} {A : Type z} [comm_semiring R] [semiring A] [algebra R A] (x : A) : coe_fn (aeval x) X = x := eval₂_X (algebra_map R A) x @[simp] theorem aeval_C {R : Type u} {A : Type z} [comm_semiring R] [semiring A] [algebra R A] (x : A) (r : R) : coe_fn (aeval x) (coe_fn C r) = coe_fn (algebra_map R A) r := eval₂_C (algebra_map R A) x theorem aeval_monomial {R : Type u} {A : Type z} [comm_semiring R] [semiring A] [algebra R A] (x : A) {n : ℕ} {r : R} : coe_fn (aeval x) (coe_fn (monomial n) r) = coe_fn (algebra_map R A) r * x ^ n := eval₂_monomial (algebra_map R A) x @[simp] theorem aeval_X_pow {R : Type u} {A : Type z} [comm_semiring R] [semiring A] [algebra R A] (x : A) {n : ℕ} : coe_fn (aeval x) (X ^ n) = x ^ n := eval₂_X_pow (algebra_map R A) x @[simp] theorem aeval_add {R : Type u} {A : Type z} [comm_semiring R] {p : polynomial R} {q : polynomial R} [semiring A] [algebra R A] (x : A) : coe_fn (aeval x) (p + q) = coe_fn (aeval x) p + coe_fn (aeval x) q := alg_hom.map_add (aeval x) p q @[simp] theorem aeval_one {R : Type u} {A : Type z} [comm_semiring R] [semiring A] [algebra R A] (x : A) : coe_fn (aeval x) 1 = 1 := alg_hom.map_one (aeval x) @[simp] theorem aeval_bit0 {R : Type u} {A : Type z} [comm_semiring R] {p : polynomial R} [semiring A] [algebra R A] (x : A) : coe_fn (aeval x) (bit0 p) = bit0 (coe_fn (aeval x) p) := alg_hom.map_bit0 (aeval x) p @[simp] theorem aeval_bit1 {R : Type u} {A : Type z} [comm_semiring R] {p : polynomial R} [semiring A] [algebra R A] (x : A) : coe_fn (aeval x) (bit1 p) = bit1 (coe_fn (aeval x) p) := alg_hom.map_bit1 (aeval x) p @[simp] theorem aeval_nat_cast {R : Type u} {A : Type z} [comm_semiring R] [semiring A] [algebra R A] (x : A) (n : ℕ) : coe_fn (aeval x) ↑n = ↑n := alg_hom.map_nat_cast (aeval x) n theorem aeval_mul {R : Type u} {A : Type z} [comm_semiring R] {p : polynomial R} {q : polynomial R} [semiring A] [algebra R A] (x : A) : coe_fn (aeval x) (p * q) = coe_fn (aeval x) p * coe_fn (aeval x) q := alg_hom.map_mul (aeval x) p q theorem aeval_comp {R : Type u} [comm_semiring R] {p : polynomial R} {q : polynomial R} {A : Type u_1} [comm_semiring A] [algebra R A] (x : A) : coe_fn (aeval x) (comp p q) = coe_fn (aeval (coe_fn (aeval x) q)) p := eval₂_comp (algebra_map R A) @[simp] theorem aeval_map {R : Type u} [comm_semiring R] {B : Type u_1} [semiring B] [algebra R B] {A : Type u_2} [comm_semiring A] [algebra R A] [algebra A B] [is_scalar_tower R A B] (b : B) (p : polynomial R) : coe_fn (aeval b) (map (algebra_map R A) p) = coe_fn (aeval b) p := sorry theorem eval_unique {R : Type u} {A : Type z} [comm_semiring R] [semiring A] [algebra R A] (φ : alg_hom R (polynomial R) A) (p : polynomial R) : coe_fn φ p = eval₂ (algebra_map R A) (coe_fn φ X) p := sorry theorem aeval_alg_hom {R : Type u} {A : Type z} [comm_semiring R] [semiring A] [algebra R A] {B : Type u_1} [semiring B] [algebra R B] (f : alg_hom R A B) (x : A) : aeval (coe_fn f x) = alg_hom.comp f (aeval x) := sorry theorem aeval_alg_hom_apply {R : Type u} {A : Type z} [comm_semiring R] [semiring A] [algebra R A] {B : Type u_1} [semiring B] [algebra R B] (f : alg_hom R A B) (x : A) (p : polynomial R) : coe_fn (aeval (coe_fn f x)) p = coe_fn f (coe_fn (aeval x) p) := iff.mp alg_hom.ext_iff (aeval_alg_hom f x) p @[simp] theorem coe_aeval_eq_eval {R : Type u} [comm_semiring R] (r : R) : ⇑(aeval r) = eval r := rfl theorem coeff_zero_eq_aeval_zero {R : Type u} [comm_semiring R] (p : polynomial R) : coeff p 0 = coe_fn (aeval 0) p := sorry theorem pow_comp {R : Type u} [comm_semiring R] (p : polynomial R) (q : polynomial R) (k : ℕ) : comp (p ^ k) q = comp p q ^ k := sorry theorem is_root_of_eval₂_map_eq_zero {R : Type u} {S : Type v} [comm_semiring R] {p : polynomial R} [comm_ring S] {f : R →+* S} (hf : function.injective ⇑f) {r : R} : eval₂ f (coe_fn f r) p = 0 → is_root p r := sorry theorem is_root_of_aeval_algebra_map_eq_zero {R : Type u} {S : Type v} [comm_semiring R] [comm_ring S] [algebra R S] {p : polynomial R} (inj : function.injective ⇑(algebra_map R S)) {r : R} (hr : coe_fn (aeval (coe_fn (algebra_map R S) r)) p = 0) : is_root p r := is_root_of_eval₂_map_eq_zero inj hr theorem dvd_term_of_dvd_eval_of_dvd_terms {S : Type v} [comm_ring S] {z : S} {p : S} {f : polynomial S} (i : ℕ) (dvd_eval : p ∣ eval z f) (dvd_terms : ∀ (j : ℕ), j ≠ i → p ∣ coeff f j * z ^ j) : p ∣ coeff f i * z ^ i := sorry theorem dvd_term_of_is_root_of_dvd_terms {S : Type v} [comm_ring S] {r : S} {p : S} {f : polynomial S} (i : ℕ) (hr : is_root f r) (h : ∀ (j : ℕ), j ≠ i → p ∣ coeff f j * r ^ j) : p ∣ coeff f i * r ^ i := dvd_term_of_dvd_eval_of_dvd_terms i (Eq.symm hr ▸ dvd_zero p) h theorem aeval_eq_sum_range {R : Type u} {S : Type v} [comm_semiring R] [comm_ring S] [algebra R S] {p : polynomial R} (x : S) : coe_fn (aeval x) p = finset.sum (finset.range (nat_degree p + 1)) fun (i : ℕ) => coeff p i • x ^ i := sorry theorem aeval_eq_sum_range' {R : Type u} {S : Type v} [comm_semiring R] [comm_ring S] [algebra R S] {p : polynomial R} {n : ℕ} (hn : nat_degree p < n) (x : S) : coe_fn (aeval x) p = finset.sum (finset.range n) fun (i : ℕ) => coeff p i • x ^ i := sorry /-- The evaluation map is not generally multiplicative when the coefficient ring is noncommutative, but nevertheless any polynomial of the form `p * (X - monomial 0 r)` is sent to zero when evaluated at `r`. This is the key step in our proof of the Cayley-Hamilton theorem. -/ theorem eval_mul_X_sub_C {R : Type u} [ring R] {p : polynomial R} (r : R) : eval r (p * (X - coe_fn C r)) = 0 := sorry theorem not_is_unit_X_sub_C {R : Type u} [ring R] [nontrivial R] {r : R} : ¬is_unit (X - coe_fn C r) := sorry theorem aeval_endomorphism {R : Type u} {M : Type u_1} [comm_ring R] [add_comm_group M] [module R M] (f : linear_map R M M) (v : M) (p : polynomial R) : coe_fn (coe_fn (aeval f) p) v = finsupp.sum p fun (n : ℕ) (b : R) => b • coe_fn (f ^ n) v := sorry end Mathlib
6981605bc529d16f78664856f7fb51b1d2b1eff3
367134ba5a65885e863bdc4507601606690974c1
/test/free_algebra.lean
1ef6dcc94f390761db800ae528c54e5c579365e7
[ "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
1,183
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser -/ import linear_algebra.exterior_algebra import linear_algebra.clifford_algebra /-! Tests that the ring instances for `free_algebra` and derived quotient types actually work. There is some discussion about this in https://leanprover.zulipchat.com/#narrow/stream/113488-general/topic/algebra.2Esemiring_to_ring.20breaks.20semimodule.20typeclass.20lookup/near/212580241 In essence, the use of `attribute [irreducible] the_type` was breaking instance resolution on that type. -/ variables {S : Type*} {M : Type*} section free variables [comm_ring S] example : (1 : free_algebra S M) - (1 : free_algebra S M) = 0 := by rw sub_self end free section exterior variables [comm_ring S] [add_comm_monoid M] [semimodule S M] example : (1 : exterior_algebra S M) - (1 : exterior_algebra S M) = 0 := by rw sub_self end exterior section clifford variables [comm_ring S] [add_comm_group M] [semimodule S M] (Q : quadratic_form S M) example : (1 : clifford_algebra Q) - (1 : clifford_algebra Q) = 0 := by rw sub_self end clifford
e28d1d0a83e2bbafab9fbd3294624af5bbec351a
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/Lean3Lib/init/meta/widget/tactic_component_auto.lean
873952a2a79472d3122e0425eaa01a430a6e2792
[]
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
416
lean
/- Copyright (c) E.W.Ayers. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: E.W.Ayers -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.meta.widget.basic namespace Mathlib namespace widget /-- A component that implicitly depends on tactic_state. For efficiency we always assume that the tactic_state is unchanged between component renderings. -/ end Mathlib
cd0be5e7ff326cd9198eae11ae33a345223c1158
22e97a5d648fc451e25a06c668dc03ac7ed7bc25
/src/data/real/basic.lean
65a147bb8b0d9ffc5b2b2329f9dae858e40a275b
[ "Apache-2.0" ]
permissive
keeferrowan/mathlib
f2818da875dbc7780830d09bd4c526b0764a4e50
aad2dfc40e8e6a7e258287a7c1580318e865817e
refs/heads/master
1,661,736,426,952
1,590,438,032,000
1,590,438,032,000
266,892,663
0
0
Apache-2.0
1,590,445,835,000
1,590,445,835,000
null
UTF-8
Lean
false
false
26,280
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Floris van Doorn The (classical) real numbers ℝ. This is a direct construction from Cauchy sequences. -/ import order.conditionally_complete_lattice import data.real.cau_seq_completion import algebra.archimedean def real := @cau_seq.completion.Cauchy ℚ _ _ _ abs _ notation `ℝ` := real namespace real open cau_seq cau_seq.completion variables {x y : ℝ} def comm_ring_aux : comm_ring ℝ := cau_seq.completion.comm_ring instance : comm_ring ℝ := { ..comm_ring_aux } /- Extra instances to short-circuit type class resolution -/ instance : ring ℝ := by apply_instance instance : comm_semiring ℝ := by apply_instance instance : semiring ℝ := by apply_instance instance : add_comm_group ℝ := by apply_instance instance : add_group ℝ := by apply_instance instance : add_comm_monoid ℝ := by apply_instance instance : add_monoid ℝ := by apply_instance instance : add_left_cancel_semigroup ℝ := by apply_instance instance : add_right_cancel_semigroup ℝ := by apply_instance instance : add_comm_semigroup ℝ := by apply_instance instance : add_semigroup ℝ := by apply_instance instance : comm_monoid ℝ := by apply_instance instance : monoid ℝ := by apply_instance instance : comm_semigroup ℝ := by apply_instance instance : semigroup ℝ := by apply_instance instance : inhabited ℝ := ⟨0⟩ /-- Coercion `ℚ` → `ℝ` as a `ring_hom`. Note that this is `cau_seq.completion.of_rat`, not `rat.cast`. -/ def of_rat : ℚ →+* ℝ := ⟨of_rat, rfl, of_rat_mul, rfl, of_rat_add⟩ /-- Make a real number from a Cauchy sequence of rationals (by taking the equivalence class). -/ def mk (x : cau_seq ℚ abs) : ℝ := cau_seq.completion.mk x theorem of_rat_sub (x y : ℚ) : of_rat (x - y) = of_rat x - of_rat y := congr_arg mk (const_sub _ _) instance : has_lt ℝ := ⟨λ x y, quotient.lift_on₂ x y (<) $ λ f₁ g₁ f₂ g₂ hf hg, propext $ ⟨λ h, lt_of_eq_of_lt (setoid.symm hf) (lt_of_lt_of_eq h hg), λ h, lt_of_eq_of_lt hf (lt_of_lt_of_eq h (setoid.symm hg))⟩⟩ @[simp] theorem mk_lt {f g : cau_seq ℚ abs} : mk f < mk g ↔ f < g := iff.rfl theorem mk_eq {f g : cau_seq ℚ abs} : mk f = mk g ↔ f ≈ g := mk_eq theorem quotient_mk_eq_mk (f : cau_seq ℚ abs) : ⟦f⟧ = mk f := rfl theorem mk_eq_mk {f : cau_seq ℚ abs} : cau_seq.completion.mk f = mk f := rfl @[simp] theorem mk_pos {f : cau_seq ℚ abs} : 0 < mk f ↔ pos f := iff_of_eq (congr_arg pos (sub_zero f)) protected def le (x y : ℝ) : Prop := x < y ∨ x = y instance : has_le ℝ := ⟨real.le⟩ @[simp] theorem mk_le {f g : cau_seq ℚ abs} : mk f ≤ mk g ↔ f ≤ g := or_congr iff.rfl quotient.eq theorem add_lt_add_iff_left {a b : ℝ} (c : ℝ) : c + a < c + b ↔ a < b := quotient.induction_on₃ a b c (λ f g h, iff_of_eq (congr_arg pos $ by rw add_sub_add_left_eq_sub)) instance : linear_order ℝ := { le := (≤), lt := (<), le_refl := λ a, or.inr rfl, le_trans := λ a b c, quotient.induction_on₃ a b c $ λ f g h, by simpa [quotient_mk_eq_mk] using le_trans, lt_iff_le_not_le := λ a b, quotient.induction_on₂ a b $ λ f g, by simpa [quotient_mk_eq_mk] using lt_iff_le_not_le, le_antisymm := λ a b, quotient.induction_on₂ a b $ λ f g, by simpa [mk_eq, quotient_mk_eq_mk] using @cau_seq.le_antisymm _ _ f g, le_total := λ a b, quotient.induction_on₂ a b $ λ f g, by simpa [quotient_mk_eq_mk] using le_total f g } instance : partial_order ℝ := by apply_instance instance : preorder ℝ := by apply_instance theorem of_rat_lt {x y : ℚ} : of_rat x < of_rat y ↔ x < y := const_lt protected theorem zero_lt_one : (0 : ℝ) < 1 := of_rat_lt.2 zero_lt_one protected theorem mul_pos {a b : ℝ} : 0 < a → 0 < b → 0 < a * b := quotient.induction_on₂ a b $ λ f g, show pos (f - 0) → pos (g - 0) → pos (f * g - 0), by simpa using cau_seq.mul_pos instance : linear_ordered_comm_ring ℝ := { add_le_add_left := λ a b h c, (le_iff_le_iff_lt_iff_lt.2 $ real.add_lt_add_iff_left c).2 h, zero_ne_one := ne_of_lt real.zero_lt_one, mul_pos := @real.mul_pos, zero_lt_one := real.zero_lt_one, ..real.comm_ring, ..real.linear_order, ..real.semiring } /- Extra instances to short-circuit type class resolution -/ 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 : ordered_add_comm_group ℝ := by apply_instance instance : ordered_cancel_add_comm_monoid ℝ := by apply_instance instance : ordered_add_comm_monoid ℝ := by apply_instance instance : domain ℝ := by apply_instance instance : has_one ℝ := by apply_instance instance : has_zero ℝ := by apply_instance instance : has_mul ℝ := by apply_instance instance : has_add ℝ := by apply_instance instance : has_sub ℝ := by apply_instance open_locale classical noncomputable instance : discrete_linear_ordered_field ℝ := { decidable_le := by apply_instance, ..real.linear_ordered_comm_ring, ..real.domain, ..cau_seq.completion.field } /- Extra instances to short-circuit type class resolution -/ noncomputable instance : linear_ordered_field ℝ := by apply_instance noncomputable instance : decidable_linear_ordered_comm_ring ℝ := by apply_instance noncomputable instance : decidable_linear_ordered_semiring ℝ := by apply_instance noncomputable instance : decidable_linear_ordered_add_comm_group ℝ := by apply_instance noncomputable instance field : field ℝ := by apply_instance noncomputable instance : division_ring ℝ := by apply_instance noncomputable instance : integral_domain ℝ := by apply_instance noncomputable instance : nonzero_comm_ring ℝ := by apply_instance noncomputable instance : decidable_linear_order ℝ := by apply_instance noncomputable instance : distrib_lattice ℝ := by apply_instance noncomputable instance : lattice ℝ := by apply_instance noncomputable instance : semilattice_inf ℝ := by apply_instance noncomputable instance : semilattice_sup ℝ := by apply_instance noncomputable instance : has_inf ℝ := by apply_instance noncomputable instance : has_sup ℝ := by apply_instance lemma le_of_forall_epsilon_le {a b : real} (h : ∀ε, ε > 0 → a ≤ b + ε) : a ≤ b := le_of_forall_le_of_dense $ assume x hxb, calc a ≤ b + (x - b) : h (x-b) $ sub_pos.2 hxb ... = x : by rw [add_comm]; simp open rat @[simp] theorem of_rat_eq_cast : ∀ x : ℚ, of_rat x = x := of_rat.eq_rat_cast theorem le_mk_of_forall_le {f : cau_seq ℚ abs} : (∃ i, ∀ j ≥ i, x ≤ f j) → x ≤ mk f := quotient.induction_on x $ λ g h, le_of_not_lt $ λ ⟨K, K0, hK⟩, let ⟨i, H⟩ := exists_forall_ge_and h $ exists_forall_ge_and hK (f.cauchy₃ $ half_pos K0) in begin apply not_lt_of_le (H _ (le_refl _)).1, rw ← of_rat_eq_cast, refine ⟨_, half_pos K0, i, λ j ij, _⟩, have := add_le_add (H _ ij).2.1 (le_of_lt (abs_lt.1 $ (H _ (le_refl _)).2.2 _ ij).1), rwa [← sub_eq_add_neg, sub_self_div_two, sub_apply, sub_add_sub_cancel] at this end theorem mk_le_of_forall_le {f : cau_seq ℚ abs} {x : ℝ} : (∃ i, ∀ j ≥ i, (f j : ℝ) ≤ x) → mk f ≤ x | ⟨i, H⟩ := by rw [← neg_le_neg_iff, ← mk_eq_mk, mk_neg]; exact le_mk_of_forall_le ⟨i, λ j ij, by simp [H _ ij]⟩ theorem mk_near_of_forall_near {f : cau_seq ℚ abs} {x : ℝ} {ε : ℝ} (H : ∃ i, ∀ j ≥ i, abs ((f j : ℝ) - x) ≤ ε) : abs (mk f - x) ≤ ε := abs_sub_le_iff.2 ⟨sub_le_iff_le_add'.2 $ mk_le_of_forall_le $ H.imp $ λ i h j ij, sub_le_iff_le_add'.1 (abs_sub_le_iff.1 $ h j ij).1, sub_le.1 $ le_mk_of_forall_le $ H.imp $ λ i h j ij, sub_le.1 (abs_sub_le_iff.1 $ h j ij).2⟩ instance : archimedean ℝ := archimedean_iff_rat_le.2 $ λ x, quotient.induction_on x $ λ f, let ⟨M, M0, H⟩ := f.bounded' 0 in ⟨M, mk_le_of_forall_le ⟨0, λ i _, rat.cast_le.2 $ le_of_lt (abs_lt.1 (H i)).2⟩⟩ /- mark `real` irreducible in order to prevent `auto_cases` unfolding reals, since users rarely want to consider real numbers as Cauchy sequences. Marking `comm_ring_aux` `irreducible` is done to ensure that there are no problems with non definitionally equal instances, caused by making `real` irreducible-/ attribute [irreducible] real comm_ring_aux noncomputable instance : floor_ring ℝ := archimedean.floor_ring _ theorem is_cau_seq_iff_lift {f : ℕ → ℚ} : is_cau_seq abs f ↔ is_cau_seq abs (λ i, (f i : ℝ)) := ⟨λ H ε ε0, let ⟨δ, δ0, δε⟩ := exists_pos_rat_lt ε0 in (H _ δ0).imp $ λ i hi j ij, lt_trans (by simpa using (@rat.cast_lt ℝ _ _ _).2 (hi _ ij)) δε, λ H ε ε0, (H _ (rat.cast_pos.2 ε0)).imp $ λ i hi j ij, (@rat.cast_lt ℝ _ _ _).1 $ by simpa using hi _ ij⟩ theorem of_near (f : ℕ → ℚ) (x : ℝ) (h : ∀ ε > 0, ∃ i, ∀ j ≥ i, abs ((f j : ℝ) - x) < ε) : ∃ h', real.mk ⟨f, h'⟩ = x := ⟨is_cau_seq_iff_lift.2 (of_near _ (const abs x) h), sub_eq_zero.1 $ abs_eq_zero.1 $ eq_of_le_of_forall_le_of_dense (abs_nonneg _) $ λ ε ε0, mk_near_of_forall_near $ (h _ ε0).imp (λ i h j ij, le_of_lt (h j ij))⟩ theorem exists_floor (x : ℝ) : ∃ (ub : ℤ), (ub:ℝ) ≤ x ∧ ∀ (z : ℤ), (z:ℝ) ≤ x → z ≤ ub := int.exists_greatest_of_bdd (let ⟨n, hn⟩ := exists_int_gt x in ⟨n, λ z h', int.cast_le.1 $ le_trans h' $ le_of_lt hn⟩) (let ⟨n, hn⟩ := exists_int_lt x in ⟨n, le_of_lt hn⟩) theorem exists_sup (S : set ℝ) : (∃ x, x ∈ S) → (∃ x, ∀ y ∈ S, y ≤ x) → ∃ x, ∀ y, x ≤ y ↔ ∀ z ∈ S, z ≤ y | ⟨L, hL⟩ ⟨U, hU⟩ := begin choose f hf using begin refine λ d : ℕ, @int.exists_greatest_of_bdd (λ n, ∃ y ∈ S, (n:ℝ) ≤ y * d) _ _, { cases exists_int_gt U with k hk, refine ⟨k * d, λ z h, _⟩, rcases h with ⟨y, yS, hy⟩, refine int.cast_le.1 (le_trans hy _), simp, exact mul_le_mul_of_nonneg_right (le_trans (hU _ yS) (le_of_lt hk)) (nat.cast_nonneg _) }, { exact ⟨⌊L * d⌋, L, hL, floor_le _⟩ } end, have hf₁ : ∀ n > 0, ∃ y ∈ S, ((f n / n:ℚ):ℝ) ≤ y := λ n n0, let ⟨y, yS, hy⟩ := (hf n).1 in ⟨y, yS, by simpa using (div_le_iff ((nat.cast_pos.2 n0):((_:ℝ) < _))).2 hy⟩, have hf₂ : ∀ (n > 0) (y ∈ S), (y - (n:ℕ)⁻¹ : ℝ) < (f n / n:ℚ), { intros n n0 y yS, have := lt_of_lt_of_le (sub_one_lt_floor _) (int.cast_le.2 $ (hf n).2 _ ⟨y, yS, floor_le _⟩), simp [-sub_eq_add_neg], rwa [lt_div_iff ((nat.cast_pos.2 n0):((_:ℝ) < _)), sub_mul, _root_.inv_mul_cancel], exact ne_of_gt (nat.cast_pos.2 n0) }, suffices hg, let g : cau_seq ℚ abs := ⟨λ n, f n / n, hg⟩, refine ⟨mk g, λ y, ⟨λ h x xS, le_trans _ h, λ h, _⟩⟩, { refine le_of_forall_ge_of_dense (λ z xz, _), cases exists_nat_gt (x - z)⁻¹ with K hK, refine le_mk_of_forall_le ⟨K, λ n nK, _⟩, replace xz := sub_pos.2 xz, replace hK := le_trans (le_of_lt hK) (nat.cast_le.2 nK), have n0 : 0 < n := nat.cast_pos.1 (lt_of_lt_of_le (inv_pos.2 xz) hK), refine le_trans _ (le_of_lt $ hf₂ _ n0 _ xS), rwa [le_sub, inv_le ((nat.cast_pos.2 n0):((_:ℝ) < _)) xz] }, { exact mk_le_of_forall_le ⟨1, λ n n1, let ⟨x, xS, hx⟩ := hf₁ _ n1 in le_trans hx (h _ xS)⟩ }, intros ε ε0, suffices : ∀ j k ≥ nat_ceil ε⁻¹, (f j / j - f k / k : ℚ) < ε, { refine ⟨_, λ j ij, abs_lt.2 ⟨_, this _ _ ij (le_refl _)⟩⟩, rw [neg_lt, neg_sub], exact this _ _ (le_refl _) ij }, intros j k ij ik, replace ij := le_trans (le_nat_ceil _) (nat.cast_le.2 ij), replace ik := le_trans (le_nat_ceil _) (nat.cast_le.2 ik), have j0 := nat.cast_pos.1 (lt_of_lt_of_le (inv_pos.2 ε0) ij), have k0 := nat.cast_pos.1 (lt_of_lt_of_le (inv_pos.2 ε0) ik), rcases hf₁ _ j0 with ⟨y, yS, hy⟩, refine lt_of_lt_of_le ((@rat.cast_lt ℝ _ _ _).1 _) ((inv_le ε0 (nat.cast_pos.2 k0)).1 ik), simpa using sub_lt_iff_lt_add'.2 (lt_of_le_of_lt hy $ sub_lt_iff_lt_add.1 $ hf₂ _ k0 _ yS) end noncomputable instance : has_Sup ℝ := ⟨λ S, if h : (∃ x, x ∈ S) ∧ (∃ x, ∀ y ∈ S, y ≤ x) then classical.some (exists_sup S h.1 h.2) else 0⟩ lemma Sup_def (S : set ℝ) : Sup S = if h : (∃ x, x ∈ S) ∧ (∃ x, ∀ y ∈ S, y ≤ x) then classical.some (exists_sup S h.1 h.2) else 0 := rfl theorem Sup_le (S : set ℝ) (h₁ : ∃ x, x ∈ S) (h₂ : ∃ x, ∀ y ∈ S, y ≤ x) {y} : Sup S ≤ y ↔ ∀ z ∈ S, z ≤ y := by simp [Sup_def, h₁, h₂]; exact classical.some_spec (exists_sup S h₁ h₂) y section -- this proof times out without this local attribute [instance, priority 1000] classical.prop_decidable theorem lt_Sup (S : set ℝ) (h₁ : ∃ x, x ∈ S) (h₂ : ∃ x, ∀ y ∈ S, y ≤ x) {y} : y < Sup S ↔ ∃ z ∈ S, y < z := by simpa [not_forall] using not_congr (@Sup_le S h₁ h₂ y) end theorem le_Sup (S : set ℝ) (h₂ : ∃ x, ∀ y ∈ S, y ≤ x) {x} (xS : x ∈ S) : x ≤ Sup S := (Sup_le S ⟨_, xS⟩ h₂).1 (le_refl _) _ xS theorem Sup_le_ub (S : set ℝ) (h₁ : ∃ x, x ∈ S) {ub} (h₂ : ∀ y ∈ S, y ≤ ub) : Sup S ≤ ub := (Sup_le S h₁ ⟨_, h₂⟩).2 h₂ protected lemma is_lub_Sup {s : set ℝ} {a b : ℝ} (ha : a ∈ s) (hb : b ∈ upper_bounds s) : is_lub s (Sup s) := ⟨λ x xs, real.le_Sup s ⟨_, hb⟩ xs, λ u h, real.Sup_le_ub _ ⟨_, ha⟩ h⟩ noncomputable instance : has_Inf ℝ := ⟨λ S, -Sup {x | -x ∈ S}⟩ lemma Inf_def (S : set ℝ) : Inf S = -Sup {x | -x ∈ S} := rfl theorem le_Inf (S : set ℝ) (h₁ : ∃ x, x ∈ S) (h₂ : ∃ x, ∀ y ∈ S, x ≤ y) {y} : y ≤ Inf S ↔ ∀ z ∈ S, y ≤ z := begin refine le_neg.trans ((Sup_le _ _ _).trans _), { cases h₁ with x xS, exact ⟨-x, by simp [xS]⟩ }, { cases h₂ with ub h, exact ⟨-ub, λ y hy, le_neg.1 $ h _ hy⟩ }, split; intros H z hz, { exact neg_le_neg_iff.1 (H _ $ by simp [hz]) }, { exact le_neg.2 (H _ hz) } end section -- this proof times out without this local attribute [instance, priority 1000] classical.prop_decidable theorem Inf_lt (S : set ℝ) (h₁ : ∃ x, x ∈ S) (h₂ : ∃ x, ∀ y ∈ S, x ≤ y) {y} : Inf S < y ↔ ∃ z ∈ S, z < y := by simpa [not_forall] using not_congr (@le_Inf S h₁ h₂ y) end theorem Inf_le (S : set ℝ) (h₂ : ∃ x, ∀ y ∈ S, x ≤ y) {x} (xS : x ∈ S) : Inf S ≤ x := (le_Inf S ⟨_, xS⟩ h₂).1 (le_refl _) _ xS theorem lb_le_Inf (S : set ℝ) (h₁ : ∃ x, x ∈ S) {lb} (h₂ : ∀ y ∈ S, lb ≤ y) : lb ≤ Inf S := (le_Inf S h₁ ⟨_, h₂⟩).2 h₂ noncomputable instance : conditionally_complete_linear_order ℝ := { Sup := has_Sup.Sup, Inf := has_Inf.Inf, le_cSup := assume (s : set ℝ) (a : ℝ) (_ : bdd_above s) (_ : a ∈ s), show a ≤ Sup s, from le_Sup s ‹bdd_above s› ‹a ∈ s›, cSup_le := assume (s : set ℝ) (a : ℝ) (_ : s.nonempty) (H : ∀b∈s, b ≤ a), show Sup s ≤ a, from Sup_le_ub s ‹s.nonempty› H, cInf_le := assume (s : set ℝ) (a : ℝ) (_ : bdd_below s) (_ : a ∈ s), show Inf s ≤ a, from Inf_le s ‹bdd_below s› ‹a ∈ s›, le_cInf := assume (s : set ℝ) (a : ℝ) (_ : s.nonempty) (H : ∀b∈s, a ≤ b), show a ≤ Inf s, from lb_le_Inf s ‹s.nonempty› H, decidable_le := classical.dec_rel _, ..real.linear_order, ..real.lattice} theorem Sup_empty : Sup (∅ : set ℝ) = 0 := dif_neg $ by simp theorem Sup_of_not_bdd_above {s : set ℝ} (hs : ¬ bdd_above s) : Sup s = 0 := dif_neg $ assume h, hs h.2 theorem Sup_univ : Sup (@set.univ ℝ) = 0 := real.Sup_of_not_bdd_above $ λ ⟨x, h⟩, not_le_of_lt (lt_add_one _) $ h (set.mem_univ _) theorem Inf_empty : Inf (∅ : set ℝ) = 0 := by simp [Inf_def, Sup_empty] theorem Inf_of_not_bdd_below {s : set ℝ} (hs : ¬ bdd_below s) : Inf s = 0 := have bdd_above {x | -x ∈ s} → bdd_below s, from assume ⟨b, hb⟩, ⟨-b, assume x hxs, neg_le.2 $ hb $ by simp [hxs]⟩, have ¬ bdd_above {x | -x ∈ s}, from mt this hs, neg_eq_zero.2 $ Sup_of_not_bdd_above $ this theorem cau_seq_converges (f : cau_seq ℝ abs) : ∃ x, f ≈ const abs x := begin let S := {x : ℝ | const abs x < f}, have lb : ∃ x, x ∈ S := exists_lt f, have ub' : ∀ x, f < const abs x → ∀ y ∈ S, y ≤ x := λ x h y yS, le_of_lt $ const_lt.1 $ cau_seq.lt_trans yS h, have ub : ∃ x, ∀ y ∈ S, y ≤ x := (exists_gt f).imp ub', refine ⟨Sup S, ((lt_total _ _).resolve_left (λ h, _)).resolve_right (λ h, _)⟩, { rcases h with ⟨ε, ε0, i, ih⟩, refine not_lt_of_le (Sup_le_ub S lb (ub' _ _)) ((sub_lt_self_iff _).2 (half_pos ε0)), refine ⟨_, half_pos ε0, i, λ j ij, _⟩, rw [sub_apply, const_apply, sub_right_comm, le_sub_iff_add_le, add_halves], exact ih _ ij }, { rcases h with ⟨ε, ε0, i, ih⟩, refine not_lt_of_le (le_Sup S ub _) ((lt_add_iff_pos_left _).2 (half_pos ε0)), refine ⟨_, half_pos ε0, i, λ j ij, _⟩, rw [sub_apply, const_apply, add_comm, ← sub_sub, le_sub_iff_add_le, add_halves], exact ih _ ij } end noncomputable instance : cau_seq.is_complete ℝ abs := ⟨cau_seq_converges⟩ theorem sqrt_exists : ∀ {x : ℝ}, 0 ≤ x → ∃ y, 0 ≤ y ∧ y * y = x := suffices H : ∀ {x : ℝ}, 0 < x → x ≤ 1 → ∃ y, 0 < y ∧ y * y = x, begin intros x x0, cases x0, cases le_total x 1 with x1 x1, { rcases H x0 x1 with ⟨y, y0, hy⟩, exact ⟨y, le_of_lt y0, hy⟩ }, { have := (inv_le_inv x0 zero_lt_one).2 x1, rw inv_one at this, rcases H (inv_pos.2 x0) this with ⟨y, y0, hy⟩, refine ⟨y⁻¹, le_of_lt (inv_pos.2 y0), _⟩, rw [← mul_inv', hy, inv_inv'] }, { exact ⟨0, by simp [x0.symm]⟩ } end, λ x x0 x1, begin let S := {y | 0 < y ∧ y * y ≤ x}, have lb : x ∈ S := ⟨x0, by simpa using (mul_le_mul_right x0).2 x1⟩, have ub : ∀ y ∈ S, (y:ℝ) ≤ 1, { intros y yS, cases yS with y0 yx, refine (mul_self_le_mul_self_iff (le_of_lt y0) zero_le_one).2 _, simpa using le_trans yx x1 }, have S0 : 0 < Sup S := lt_of_lt_of_le x0 (le_Sup _ ⟨_, ub⟩ lb), refine ⟨Sup S, S0, le_antisymm (not_lt.1 $ λ h, _) (not_lt.1 $ λ h, _)⟩, { rw [← div_lt_iff S0, lt_Sup S ⟨_, lb⟩ ⟨_, ub⟩] at h, rcases h with ⟨y, ⟨y0, yx⟩, hy⟩, rw [div_lt_iff S0, ← div_lt_iff' y0, lt_Sup S ⟨_, lb⟩ ⟨_, ub⟩] at hy, rcases hy with ⟨z, ⟨z0, zx⟩, hz⟩, rw [div_lt_iff y0] at hz, exact not_lt_of_lt ((mul_lt_mul_right y0).1 (lt_of_le_of_lt yx hz)) ((mul_lt_mul_left z0).1 (lt_of_le_of_lt zx hz)) }, { let s := Sup S, let y := s + (x - s * s) / 3, replace h : 0 < x - s * s := sub_pos.2 h, have _30 := bit1_pos zero_le_one, have : s < y := (lt_add_iff_pos_right _).2 (div_pos h _30), refine not_le_of_lt this (le_Sup S ⟨_, ub⟩ ⟨lt_trans S0 this, _⟩), rw [add_mul_self_eq, add_assoc, ← le_sub_iff_add_le', ← add_mul, ← le_div_iff (div_pos h _30), div_div_cancel' (ne_of_gt h)], apply add_le_add, { simpa using (mul_le_mul_left (@two_pos ℝ _)).2 (Sup_le_ub _ ⟨_, lb⟩ ub) }, { rw [div_le_one_iff_le _30], refine le_trans (sub_le_self _ (mul_self_nonneg _)) (le_trans x1 _), exact (le_add_iff_nonneg_left _).2 (le_of_lt two_pos) } } end def sqrt_aux (f : cau_seq ℚ abs) : ℕ → ℚ | 0 := rat.mk_nat (f 0).num.to_nat.sqrt (f 0).denom.sqrt | (n + 1) := let s := sqrt_aux n in max 0 $ (s + f (n+1) / s) / 2 theorem sqrt_aux_nonneg (f : cau_seq ℚ abs) : ∀ i : ℕ, 0 ≤ sqrt_aux f i | 0 := by rw [sqrt_aux, mk_nat_eq, mk_eq_div]; apply div_nonneg'; exact int.cast_nonneg.2 (int.of_nat_nonneg _) | (n + 1) := le_max_left _ _ /- TODO(Mario): finish the proof theorem sqrt_aux_converges (f : cau_seq ℚ abs) : ∃ h x, 0 ≤ x ∧ x * x = max 0 (mk f) ∧ mk ⟨sqrt_aux f, h⟩ = x := begin rcases sqrt_exists (le_max_left 0 (mk f)) with ⟨x, x0, hx⟩, suffices : ∃ h, mk ⟨sqrt_aux f, h⟩ = x, { exact this.imp (λ h e, ⟨x, x0, hx, e⟩) }, apply of_near, suffices : ∃ δ > 0, ∀ i, abs (↑(sqrt_aux f i) - x) < δ / 2 ^ i, { rcases this with ⟨δ, δ0, hδ⟩, intros, } end -/ noncomputable def sqrt (x : ℝ) : ℝ := classical.some (sqrt_exists (le_max_left 0 x)) /-quotient.lift_on x (λ f, mk ⟨sqrt_aux f, (sqrt_aux_converges f).fst⟩) (λ f g e, begin rcases sqrt_aux_converges f with ⟨hf, x, x0, xf, xs⟩, rcases sqrt_aux_converges g with ⟨hg, y, y0, yg, ys⟩, refine xs.trans (eq.trans _ ys.symm), rw [← @mul_self_inj_of_nonneg ℝ _ x y x0 y0, xf, yg], congr' 1, exact quotient.sound e end)-/ theorem sqrt_prop (x : ℝ) : 0 ≤ sqrt x ∧ sqrt x * sqrt x = max 0 x := classical.some_spec (sqrt_exists (le_max_left 0 x)) /-quotient.induction_on x $ λ f, by rcases sqrt_aux_converges f with ⟨hf, _, x0, xf, rfl⟩; exact ⟨x0, xf⟩-/ theorem sqrt_eq_zero_of_nonpos (h : x ≤ 0) : sqrt x = 0 := eq_zero_of_mul_self_eq_zero $ (sqrt_prop x).2.trans $ max_eq_left h theorem sqrt_nonneg (x : ℝ) : 0 ≤ sqrt x := (sqrt_prop x).1 @[simp] theorem mul_self_sqrt (h : 0 ≤ x) : sqrt x * sqrt x = x := (sqrt_prop x).2.trans (max_eq_right h) @[simp] theorem sqrt_mul_self (h : 0 ≤ x) : sqrt (x * x) = x := (mul_self_inj_of_nonneg (sqrt_nonneg _) h).1 (mul_self_sqrt (mul_self_nonneg _)) theorem sqrt_eq_iff_mul_self_eq (hx : 0 ≤ x) (hy : 0 ≤ y) : sqrt x = y ↔ y * y = x := ⟨λ h, by rw [← h, mul_self_sqrt hx], λ h, by rw [← h, sqrt_mul_self hy]⟩ @[simp] theorem sqr_sqrt (h : 0 ≤ x) : sqrt x ^ 2 = x := by rw [pow_two, mul_self_sqrt h] @[simp] theorem sqrt_sqr (h : 0 ≤ x) : sqrt (x ^ 2) = x := by rw [pow_two, sqrt_mul_self h] theorem sqrt_eq_iff_sqr_eq (hx : 0 ≤ x) (hy : 0 ≤ y) : sqrt x = y ↔ y ^ 2 = x := by rw [pow_two, sqrt_eq_iff_mul_self_eq hx hy] theorem sqrt_mul_self_eq_abs (x : ℝ) : sqrt (x * x) = abs x := (le_total 0 x).elim (λ h, (sqrt_mul_self h).trans (abs_of_nonneg h).symm) (λ h, by rw [← neg_mul_neg, sqrt_mul_self (neg_nonneg.2 h), abs_of_nonpos h]) theorem sqrt_sqr_eq_abs (x : ℝ) : sqrt (x ^ 2) = abs x := by rw [pow_two, sqrt_mul_self_eq_abs] @[simp] theorem sqrt_zero : sqrt 0 = 0 := by simpa using sqrt_mul_self (le_refl _) @[simp] theorem sqrt_one : sqrt 1 = 1 := by simpa using sqrt_mul_self zero_le_one @[simp] theorem sqrt_le (hx : 0 ≤ x) (hy : 0 ≤ y) : sqrt x ≤ sqrt y ↔ x ≤ y := by rw [mul_self_le_mul_self_iff (sqrt_nonneg _) (sqrt_nonneg _), mul_self_sqrt hx, mul_self_sqrt hy] @[simp] theorem sqrt_lt (hx : 0 ≤ x) (hy : 0 ≤ y) : sqrt x < sqrt y ↔ x < y := lt_iff_lt_of_le_iff_le (sqrt_le hy hx) lemma sqrt_le_sqrt (h : x ≤ y) : sqrt x ≤ sqrt y := begin rw [mul_self_le_mul_self_iff (sqrt_nonneg _) (sqrt_nonneg _), (sqrt_prop _).2, (sqrt_prop _).2], exact max_le_max (le_refl _) h end lemma sqrt_le_left (hy : 0 ≤ y) : sqrt x ≤ y ↔ x ≤ y ^ 2 := begin rw [mul_self_le_mul_self_iff (sqrt_nonneg _) hy, pow_two], cases le_total 0 x with hx hx, { rw [mul_self_sqrt hx] }, { have h1 : 0 ≤ y * y := mul_nonneg hy hy, have h2 : x ≤ y * y := le_trans hx h1, simp [sqrt_eq_zero_of_nonpos, hx, h1, h2] } end /- note: if you want to conclude `x ≤ sqrt y`, then use `le_sqrt_of_sqr_le`. if you have `x > 0`, consider using `le_sqrt'` -/ lemma le_sqrt (hx : 0 ≤ x) (hy : 0 ≤ y) : x ≤ sqrt y ↔ x ^ 2 ≤ y := by rw [mul_self_le_mul_self_iff hx (sqrt_nonneg _), pow_two, mul_self_sqrt hy] lemma le_sqrt' (hx : 0 < x) : x ≤ sqrt y ↔ x ^ 2 ≤ y := begin rw [mul_self_le_mul_self_iff (le_of_lt hx) (sqrt_nonneg _), pow_two], cases le_total 0 y with hy hy, { rw [mul_self_sqrt hy] }, { have h1 : 0 < x * x := mul_pos hx hx, have h2 : ¬x * x ≤ y := not_le_of_lt (lt_of_le_of_lt hy h1), simp [sqrt_eq_zero_of_nonpos, hy, h1, h2] } end lemma le_sqrt_of_sqr_le (h : x ^ 2 ≤ y) : x ≤ sqrt y := begin cases lt_or_ge 0 x with hx hx, { rwa [le_sqrt' hx] }, { exact le_trans hx (sqrt_nonneg y) } end @[simp] theorem sqrt_inj (hx : 0 ≤ x) (hy : 0 ≤ y) : sqrt x = sqrt y ↔ x = y := by simp [le_antisymm_iff, hx, hy] @[simp] theorem sqrt_eq_zero (h : 0 ≤ x) : sqrt x = 0 ↔ x = 0 := by simpa using sqrt_inj h (le_refl _) theorem sqrt_eq_zero' : sqrt x = 0 ↔ x ≤ 0 := (le_total x 0).elim (λ h, by simp [h, sqrt_eq_zero_of_nonpos]) (λ h, by simp [h]; simp [le_antisymm_iff, h]) @[simp] theorem sqrt_pos : 0 < sqrt x ↔ 0 < x := lt_iff_lt_of_le_iff_le (iff.trans (by simp [le_antisymm_iff, sqrt_nonneg]) sqrt_eq_zero') @[simp] theorem sqrt_mul' (x) {y : ℝ} (hy : 0 ≤ y) : sqrt (x * y) = sqrt x * sqrt y := begin cases le_total 0 x with hx hx, { refine iff.mp (mul_self_inj_of_nonneg _ (mul_nonneg _ _)) _; try {apply sqrt_nonneg}, rw [mul_self_sqrt (mul_nonneg hx hy), mul_assoc, mul_left_comm (sqrt y), mul_self_sqrt hy, ← mul_assoc, mul_self_sqrt hx] }, { rw [sqrt_eq_zero'.2 (mul_nonpos_of_nonpos_of_nonneg hx hy), sqrt_eq_zero'.2 hx, zero_mul] } end @[simp] theorem sqrt_mul (hx : 0 ≤ x) (y : ℝ) : sqrt (x * y) = sqrt x * sqrt y := by rw [mul_comm, sqrt_mul' _ hx, mul_comm] @[simp] theorem sqrt_inv (x : ℝ) : sqrt x⁻¹ = (sqrt x)⁻¹ := (le_or_lt x 0).elim (λ h, by simp [sqrt_eq_zero'.2, inv_nonpos, h]) (λ h, by rw [ ← mul_self_inj_of_nonneg (sqrt_nonneg _) (le_of_lt $ inv_pos.2 $ sqrt_pos.2 h), mul_self_sqrt (le_of_lt $ inv_pos.2 h), ← mul_inv', mul_self_sqrt (le_of_lt h)]) @[simp] theorem sqrt_div (hx : 0 ≤ x) (y : ℝ) : sqrt (x / y) = sqrt x / sqrt y := by rw [division_def, sqrt_mul hx, sqrt_inv]; refl attribute [irreducible] real.le end real
710494dc7bbfdda1c1e2eea42fe6cdae99a633b8
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/set_theory/surreal_auto.lean
16e4e8bf67d83e75acd94717f457a42e4a40b8de
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
10,736
lean
/- Copyright (c) 2019 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Scott Morrison -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.set_theory.pgame import Mathlib.PostPort universes u_1 u_2 u l u_3 namespace Mathlib /-! # Surreal numbers The basic theory of surreal numbers, built on top of the theory of combinatorial (pre-)games. A pregame is `numeric` if all the Left options are strictly smaller than all the Right options, and all those options are themselves numeric. In terms of combinatorial games, the numeric games have "frozen"; you can only make your position worse by playing, and Left is some definite "number" of moves ahead (or behind) Right. A surreal number is an equivalence class of numeric pregames. In fact, the surreals form a complete ordered field, containing a copy of the reals (and much else besides!) but we do not yet have a complete development. ## Order properties Surreal numbers inherit the relations `≤` and `<` from games, and these relations satisfy the axioms of a partial order (recall that `x < y ↔ x ≤ y ∧ ¬ y ≤ x` did not hold for games). ## Algebraic operations At this point, we have defined addition and negation (from pregames), and shown that surreals form an additive semigroup. It would be very little work to finish showing that the surreals form an ordered commutative group. We define the operations of multiplication and inverse on surreals, but do not yet establish any of the necessary properties to show the surreals form an ordered field. ## Embeddings It would be nice projects to define the group homomorphism `surreal → game`, and also `ℤ → surreal`, and then the homomorphic inclusion of the dyadic rationals into surreals, and finally via dyadic Dedekind cuts the homomorphic inclusion of the reals into the surreals. One can also map all the ordinals into the surreals! ## References * [Conway, *On numbers and games*][conway2001] -/ namespace pgame /-! Multiplicative operations can be defined at the level of pre-games, but as they are only useful on surreal numbers, we define them here. -/ /-- The product of `x = {xL | xR}` and `y = {yL | yR}` is `{xL*y + x*yL - xL*yL, xR*y + x*yR - xR*yR | xL*y + x*yR - xL*yR, x*yL + xR*y - xR*yL }`. -/ def mul (x : pgame) (y : pgame) : pgame := sorry protected instance has_mul : Mul pgame := { mul := mul } /-- Because the two halves of the definition of `inv` produce more elements of each side, we have to define the two families inductively. This is the indexing set for the function, and `inv_val` is the function part. -/ inductive inv_ty (l : Type u) (r : Type u) : Bool → Type u where | zero : inv_ty l r false | left₁ : r → inv_ty l r false → inv_ty l r false | left₂ : l → inv_ty l r tt → inv_ty l r false | right₁ : l → inv_ty l r false → inv_ty l r tt | right₂ : r → inv_ty l r tt → inv_ty l r tt /-- Because the two halves of the definition of `inv` produce more elements of each side, we have to define the two families inductively. This is the function part, defined by recursion on `inv_ty`. -/ def inv_val {l : Type u_1} {r : Type u_1} (L : l → pgame) (R : r → pgame) (IHl : l → pgame) (IHr : r → pgame) {b : Bool} : inv_ty l r b → pgame := sorry /-- The inverse of a positive surreal number `x = {L | R}` is given by `x⁻¹ = {0, (1 + (R - x) * x⁻¹L) * R, (1 + (L - x) * x⁻¹R) * L | (1 + (L - x) * x⁻¹L) * L, (1 + (R - x) * x⁻¹R) * R}`. Because the two halves `x⁻¹L, x⁻¹R` of `x⁻¹` are used in their own definition, the sets and elements are inductively generated. -/ def inv' : pgame → pgame := sorry /-- The inverse of a surreal number in terms of the inverse on positive surreals. -/ def inv (x : pgame) : pgame := ite (x = 0) 0 (ite (0 < x) (inv' x) (inv' (-x))) protected instance has_inv : has_inv pgame := has_inv.mk inv protected instance has_div : Div pgame := { div := fun (x y : pgame) => x * (y⁻¹) } /-- A pre-game is numeric if everything in the L set is less than everything in the R set, and all the elements of L and R are also numeric. -/ def numeric : pgame → Prop := sorry theorem numeric.move_left {x : pgame} (o : numeric x) (i : left_moves x) : numeric (move_left x i) := sorry theorem numeric.move_right {x : pgame} (o : numeric x) (j : right_moves x) : numeric (move_right x j) := sorry theorem numeric_rec {C : pgame → Prop} (H : ∀ (l r : Type u_1) (L : l → pgame) (R : r → pgame), (∀ (i : l) (j : r), L i < R j) → (∀ (i : l), numeric (L i)) → (∀ (i : r), numeric (R i)) → (∀ (i : l), C (L i)) → (∀ (i : r), C (R i)) → C (mk l r L R)) (x : pgame) : numeric x → C x := sorry theorem lt_asymm {x : pgame} {y : pgame} (ox : numeric x) (oy : numeric y) : x < y → ¬y < x := sorry theorem le_of_lt {x : pgame} {y : pgame} (ox : numeric x) (oy : numeric y) (h : x < y) : x ≤ y := iff.mp not_lt (lt_asymm ox oy h) /-- On numeric pre-games, `<` and `≤` satisfy the axioms of a partial order (even though they don't on all pre-games). -/ theorem lt_iff_le_not_le {x : pgame} {y : pgame} (ox : numeric x) (oy : numeric y) : x < y ↔ x ≤ y ∧ ¬y ≤ x := { mp := fun (h : x < y) => { left := le_of_lt ox oy h, right := iff.mpr not_le h }, mpr := fun (h : x ≤ y ∧ ¬y ≤ x) => iff.mp not_le (and.right h) } theorem numeric_zero : numeric 0 := sorry theorem numeric_one : numeric 1 := sorry theorem numeric_neg {x : pgame} (o : numeric x) : numeric (-x) := sorry theorem numeric.move_left_lt {x : pgame} (o : numeric x) (i : left_moves x) : move_left x i < x := eq.mpr (id (Eq._oldrec (Eq.refl (move_left x i < x)) (propext lt_def_le))) (Or.inl (Exists.intro i (id (le_refl (move_left x i))))) theorem numeric.move_left_le {x : pgame} (o : numeric x) (i : left_moves x) : move_left x i ≤ x := le_of_lt (numeric.move_left o i) o (numeric.move_left_lt o i) theorem numeric.lt_move_right {x : pgame} (o : numeric x) (j : right_moves x) : x < move_right x j := eq.mpr (id (Eq._oldrec (Eq.refl (x < move_right x j)) (propext lt_def_le))) (Or.inr (Exists.intro j (id (le_refl (move_right x j))))) theorem numeric.le_move_right {x : pgame} (o : numeric x) (j : right_moves x) : x ≤ move_right x j := le_of_lt o (numeric.move_right o j) (numeric.lt_move_right o j) theorem add_lt_add {w : pgame} {x : pgame} {y : pgame} {z : pgame} (ow : numeric w) (ox : numeric x) (oy : numeric y) (oz : numeric z) (hwx : w < x) (hyz : y < z) : w + y < x + z := sorry theorem numeric_add {x : pgame} {y : pgame} (ox : numeric x) (oy : numeric y) : numeric (x + y) := sorry -- TODO prove -- theorem numeric_nat (n : ℕ) : numeric n := sorry -- theorem numeric_omega : numeric omega := sorry end pgame /-- The equivalence on numeric pre-games. -/ def surreal.equiv (x : Subtype fun (x : pgame) => pgame.numeric x) (y : Subtype fun (x : pgame) => pgame.numeric x) := pgame.equiv (subtype.val x) (subtype.val y) protected instance surreal.setoid : setoid (Subtype fun (x : pgame) => pgame.numeric x) := setoid.mk (fun (x y : Subtype fun (x : pgame) => pgame.numeric x) => pgame.equiv (subtype.val x) (subtype.val y)) sorry /-- The type of surreal numbers. These are the numeric pre-games quotiented by the equivalence relation `x ≈ y ↔ x ≤ y ∧ y ≤ x`. In the quotient, the order becomes a total order. -/ def surreal := quotient sorry namespace surreal /-- Construct a surreal number from a numeric pre-game. -/ def mk (x : pgame) (h : pgame.numeric x) : surreal := quotient.mk { val := x, property := h } protected instance has_zero : HasZero surreal := { zero := quotient.mk { val := 0, property := pgame.numeric_zero } } protected instance has_one : HasOne surreal := { one := quotient.mk { val := 1, property := pgame.numeric_one } } protected instance inhabited : Inhabited surreal := { default := 0 } /-- Lift an equivalence-respecting function on pre-games to surreals. -/ def lift {α : Sort u_1} (f : (x : pgame) → pgame.numeric x → α) (H : ∀ {x y : pgame} (hx : pgame.numeric x) (hy : pgame.numeric y), pgame.equiv x y → f x hx = f y hy) : surreal → α := quotient.lift (fun (x : Subtype fun (x : pgame) => pgame.numeric x) => f (subtype.val x) sorry) sorry /-- Lift a binary equivalence-respecting function on pre-games to surreals. -/ def lift₂ {α : Sort u_1} (f : (x : pgame) → (y : pgame) → pgame.numeric x → pgame.numeric y → α) (H : ∀ {x₁ : pgame} {y₁ : pgame} {x₂ : pgame} {y₂ : pgame} (ox₁ : pgame.numeric x₁) (oy₁ : pgame.numeric y₁) (ox₂ : pgame.numeric x₂) (oy₂ : pgame.numeric y₂), pgame.equiv x₁ x₂ → pgame.equiv y₁ y₂ → f x₁ y₁ ox₁ oy₁ = f x₂ y₂ ox₂ oy₂) : surreal → surreal → α := lift (fun (x : pgame) (ox : pgame.numeric x) => lift (fun (y : pgame) (oy : pgame.numeric y) => f x y ox oy) sorry) sorry /-- The relation `x ≤ y` on surreals. -/ def le : surreal → surreal → Prop := lift₂ (fun (x y : pgame) (_x : pgame.numeric x) (_x : pgame.numeric y) => x ≤ y) sorry /-- The relation `x < y` on surreals. -/ def lt : surreal → surreal → Prop := lift₂ (fun (x y : pgame) (_x : pgame.numeric x) (_x : pgame.numeric y) => x < y) sorry theorem not_le {x : surreal} {y : surreal} : ¬le x y ↔ lt y x := sorry protected instance preorder : preorder surreal := preorder.mk le lt sorry sorry protected instance partial_order : partial_order surreal := partial_order.mk preorder.le preorder.lt preorder.le_refl preorder.le_trans sorry protected instance linear_order : linear_order surreal := linear_order.mk partial_order.le partial_order.lt partial_order.le_refl partial_order.le_trans partial_order.le_antisymm sorry (classical.dec_rel LessEq) Mathlib.decidable_eq_of_decidable_le Mathlib.decidable_lt_of_decidable_le /-- Addition on surreals is inherited from pre-game addition: the sum of `x = {xL | xR}` and `y = {yL | yR}` is `{xL + y, x + yL | xR + y, x + yR}`. -/ def add : surreal → surreal → surreal := lift₂ (fun (x y : pgame) (ox : pgame.numeric x) (oy : pgame.numeric y) => quotient.mk { val := x + y, property := pgame.numeric_add ox oy }) sorry protected instance has_add : Add surreal := { add := add } theorem add_assoc (x : surreal) (y : surreal) (z : surreal) : x + y + z = x + (y + z) := sorry protected instance add_semigroup : add_semigroup surreal := add_semigroup.mk Add.add add_assoc end Mathlib
ec68443e97fdf914d0663a0181aa70000f8d10b1
9dc8cecdf3c4634764a18254e94d43da07142918
/src/topology/sheaves/presheaf.lean
a46874dd0f145e722ce7a51ebb6f9dd174d37e43
[ "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
11,960
lean
/- Copyright (c) 2018 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Mario Carneiro, Reid Barton, Andrew Yang -/ import category_theory.limits.kan_extension import category_theory.adjunction import topology.category.Top.opens /-! # Presheaves on a topological space We define `presheaf C X` simply as `(opens X)ᵒᵖ ⥤ C`, and inherit the category structure with natural transformations as morphisms. We define * `pushforward_obj {X Y : Top.{w}} (f : X ⟶ Y) (ℱ : X.presheaf C) : Y.presheaf C` with notation `f _* ℱ` and for `ℱ : X.presheaf C` provide the natural isomorphisms * `pushforward.id : (𝟙 X) _* ℱ ≅ ℱ` * `pushforward.comp : (f ≫ g) _* ℱ ≅ g _* (f _* ℱ)` along with their `@[simp]` lemmas. We also define the functors `pushforward` and `pullback` between the categories `X.presheaf C` and `Y.presheaf C`, and provide their adjunction at `pushforward_pullback_adjunction`. -/ universes w v u open category_theory open topological_space open opposite variables (C : Type u) [category.{v} C] namespace Top /-- The category of `C`-valued presheaves on a (bundled) topological space `X`. -/ @[derive category, nolint has_nonempty_instance] def presheaf (X : Top.{w}) : Type (max u v w) := (opens X)ᵒᵖ ⥤ C variables {C} namespace presheaf /-- Pushforward a presheaf on `X` along a continuous map `f : X ⟶ Y`, obtaining a presheaf on `Y`. -/ def pushforward_obj {X Y : Top.{w}} (f : X ⟶ Y) (ℱ : X.presheaf C) : Y.presheaf C := (opens.map f).op ⋙ ℱ infix ` _* `: 80 := pushforward_obj @[simp] lemma pushforward_obj_obj {X Y : Top.{w}} (f : X ⟶ Y) (ℱ : X.presheaf C) (U : (opens Y)ᵒᵖ) : (f _* ℱ).obj U = ℱ.obj ((opens.map f).op.obj U) := rfl @[simp] lemma pushforward_obj_map {X Y : Top.{w}} (f : X ⟶ Y) (ℱ : X.presheaf C) {U V : (opens Y)ᵒᵖ} (i : U ⟶ V) : (f _* ℱ).map i = ℱ.map ((opens.map f).op.map i) := rfl /-- An equality of continuous maps induces a natural isomorphism between the pushforwards of a presheaf along those maps. -/ def pushforward_eq {X Y : Top.{w}} {f g : X ⟶ Y} (h : f = g) (ℱ : X.presheaf C) : f _* ℱ ≅ g _* ℱ := iso_whisker_right (nat_iso.op (opens.map_iso f g h).symm) ℱ lemma pushforward_eq' {X Y : Top.{w}} {f g : X ⟶ Y} (h : f = g) (ℱ : X.presheaf C) : f _* ℱ = g _* ℱ := by rw h @[simp] lemma pushforward_eq_hom_app {X Y : Top.{w}} {f g : X ⟶ Y} (h : f = g) (ℱ : X.presheaf C) (U) : (pushforward_eq h ℱ).hom.app U = ℱ.map (begin dsimp [functor.op], apply quiver.hom.op, apply eq_to_hom, rw h, end) := by simp [pushforward_eq] lemma pushforward_eq'_hom_app {X Y : Top.{w}} {f g : X ⟶ Y} (h : f = g) (ℱ : X.presheaf C) (U) : nat_trans.app (eq_to_hom (pushforward_eq' h ℱ)) U = ℱ.map (eq_to_hom (by rw h)) := by simpa [eq_to_hom_map] @[simp] lemma pushforward_eq_rfl {X Y : Top.{w}} (f : X ⟶ Y) (ℱ : X.presheaf C) (U) : (pushforward_eq (rfl : f = f) ℱ).hom.app (op U) = 𝟙 _ := begin dsimp [pushforward_eq], simp, end lemma pushforward_eq_eq {X Y : Top.{w}} {f g : X ⟶ Y} (h₁ h₂ : f = g) (ℱ : X.presheaf C) : ℱ.pushforward_eq h₁ = ℱ.pushforward_eq h₂ := rfl namespace pushforward variables {X : Top.{w}} (ℱ : X.presheaf C) /-- The natural isomorphism between the pushforward of a presheaf along the identity continuous map and the original presheaf. -/ def id : (𝟙 X) _* ℱ ≅ ℱ := (iso_whisker_right (nat_iso.op (opens.map_id X).symm) ℱ) ≪≫ functor.left_unitor _ lemma id_eq : (𝟙 X) _* ℱ = ℱ := by { unfold pushforward_obj, rw opens.map_id_eq, erw functor.id_comp } @[simp] lemma id_hom_app' (U) (p) : (id ℱ).hom.app (op ⟨U, p⟩) = ℱ.map (𝟙 (op ⟨U, p⟩)) := by { dsimp [id], simp, } local attribute [tidy] tactic.op_induction' @[simp, priority 990] lemma id_hom_app (U) : (id ℱ).hom.app U = ℱ.map (eq_to_hom (opens.op_map_id_obj U)) := by tidy @[simp] lemma id_inv_app' (U) (p) : (id ℱ).inv.app (op ⟨U, p⟩) = ℱ.map (𝟙 (op ⟨U, p⟩)) := by { dsimp [id], simp, } /-- The natural isomorphism between the pushforward of a presheaf along the composition of two continuous maps and the corresponding pushforward of a pushforward. -/ def comp {Y Z : Top.{w}} (f : X ⟶ Y) (g : Y ⟶ Z) : (f ≫ g) _* ℱ ≅ g _* (f _* ℱ) := iso_whisker_right (nat_iso.op (opens.map_comp f g).symm) ℱ lemma comp_eq {Y Z : Top.{w}} (f : X ⟶ Y) (g : Y ⟶ Z) : (f ≫ g) _* ℱ = g _* (f _* ℱ) := rfl @[simp] lemma comp_hom_app {Y Z : Top.{w}} (f : X ⟶ Y) (g : Y ⟶ Z) (U) : (comp ℱ f g).hom.app U = 𝟙 _ := by { dsimp [comp], tidy, } @[simp] lemma comp_inv_app {Y Z : Top.{w}} (f : X ⟶ Y) (g : Y ⟶ Z) (U) : (comp ℱ f g).inv.app U = 𝟙 _ := by { dsimp [comp], tidy, } end pushforward /-- A morphism of presheaves gives rise to a morphisms of the pushforwards of those presheaves. -/ @[simps] def pushforward_map {X Y : Top.{w}} (f : X ⟶ Y) {ℱ 𝒢 : X.presheaf C} (α : ℱ ⟶ 𝒢) : f _* ℱ ⟶ f _* 𝒢 := { app := λ U, α.app _, naturality' := λ U V i, by { erw α.naturality, refl, } } open category_theory.limits section pullback variable [has_colimits C] noncomputable theory /-- Pullback a presheaf on `Y` along a continuous map `f : X ⟶ Y`, obtaining a presheaf on `X`. This is defined in terms of left Kan extensions, which is just a fancy way of saying "take the colimits over the open sets whose preimage contains U". -/ @[simps] def pullback_obj {X Y : Top.{v}} (f : X ⟶ Y) (ℱ : Y.presheaf C) : X.presheaf C := (Lan (opens.map f).op).obj ℱ /-- Pulling back along continuous maps is functorial. -/ def pullback_map {X Y : Top.{v}} (f : X ⟶ Y) {ℱ 𝒢 : Y.presheaf C} (α : ℱ ⟶ 𝒢) : pullback_obj f ℱ ⟶ pullback_obj f 𝒢 := (Lan (opens.map f).op).map α /-- If `f '' U` is open, then `f⁻¹ℱ U ≅ ℱ (f '' U)`. -/ @[simps] def pullback_obj_obj_of_image_open {X Y : Top.{v}} (f : X ⟶ Y) (ℱ : Y.presheaf C) (U : opens X) (H : is_open (f '' U)) : (pullback_obj f ℱ).obj (op U) ≅ ℱ.obj (op ⟨_, H⟩) := begin let x : costructured_arrow (opens.map f).op (op U) := { left := op ⟨f '' U, H⟩, hom := ((@hom_of_le _ _ _ ((opens.map f).obj ⟨_, H⟩) (set.image_preimage.le_u_l _)).op : op ((opens.map f).obj (⟨⇑f '' ↑U, H⟩)) ⟶ op U) }, have hx : is_terminal x := { lift := λ s, begin fapply costructured_arrow.hom_mk, change op (unop _) ⟶ op (⟨_, H⟩ : opens _), refine (hom_of_le _).op, exact (set.image_subset f s.X.hom.unop.le).trans (set.image_preimage.l_u_le ↑(unop s.X.left)), simp end }, exact is_colimit.cocone_point_unique_up_to_iso (colimit.is_colimit _) (colimit_of_diagram_terminal hx _), end namespace pullback variables {X Y : Top.{v}} (ℱ : Y.presheaf C) /-- The pullback along the identity is isomorphic to the original presheaf. -/ def id : pullback_obj (𝟙 _) ℱ ≅ ℱ := nat_iso.of_components (λ U, pullback_obj_obj_of_image_open (𝟙 _) ℱ (unop U) (by simpa using U.unop.2) ≪≫ ℱ.map_iso (eq_to_iso (by simp))) (λ U V i, begin ext, simp, erw colimit.pre_desc_assoc, erw colimit.ι_desc_assoc, erw colimit.ι_desc_assoc, dsimp, simp only [←ℱ.map_comp], congr end) lemma id_inv_app (U : opens Y) : (id ℱ).inv.app (op U) = colimit.ι (Lan.diagram (opens.map (𝟙 Y)).op ℱ (op U)) (@costructured_arrow.mk _ _ _ _ _ (op U) _ (eq_to_hom (by simp))) := begin rw [← category.id_comp ((id ℱ).inv.app (op U)), ← nat_iso.app_inv, iso.comp_inv_eq], dsimp [id], rw colimit.ι_desc_assoc, dsimp, rw [← ℱ.map_comp, ← ℱ.map_id], refl, end end pullback end pullback variable (C) /-- The pushforward functor. -/ def pushforward {X Y : Top.{w}} (f : X ⟶ Y) : X.presheaf C ⥤ Y.presheaf C := { obj := pushforward_obj f, map := @pushforward_map _ _ X Y f } @[simp] lemma pushforward_map_app' {X Y : Top.{w}} (f : X ⟶ Y) {ℱ 𝒢 : X.presheaf C} (α : ℱ ⟶ 𝒢) {U : (opens Y)ᵒᵖ} : ((pushforward C f).map α).app U = α.app (op $ (opens.map f).obj U.unop) := rfl lemma id_pushforward {X : Top.{w}} : pushforward C (𝟙 X) = 𝟭 (X.presheaf C) := begin apply category_theory.functor.ext, { intros, ext U, have h := f.congr, erw h (opens.op_map_id_obj U), simpa [eq_to_hom_map], }, { intros, apply pushforward.id_eq }, end section iso /-- A homeomorphism of spaces gives an equivalence of categories of presheaves. -/ @[simps] def presheaf_equiv_of_iso {X Y : Top} (H : X ≅ Y) : X.presheaf C ≌ Y.presheaf C := equivalence.congr_left (opens.map_map_iso H).symm.op variable {C} /-- If `H : X ≅ Y` is a homeomorphism, then given an `H _* ℱ ⟶ 𝒢`, we may obtain an `ℱ ⟶ H ⁻¹ _* 𝒢`. -/ def to_pushforward_of_iso {X Y : Top} (H : X ≅ Y) {ℱ : X.presheaf C} {𝒢 : Y.presheaf C} (α : H.hom _* ℱ ⟶ 𝒢) : ℱ ⟶ H.inv _* 𝒢 := (presheaf_equiv_of_iso _ H).to_adjunction.hom_equiv ℱ 𝒢 α @[simp] lemma to_pushforward_of_iso_app {X Y : Top} (H₁ : X ≅ Y) {ℱ : X.presheaf C} {𝒢 : Y.presheaf C} (H₂ : H₁.hom _* ℱ ⟶ 𝒢) (U : (opens X)ᵒᵖ) : (to_pushforward_of_iso H₁ H₂).app U = ℱ.map (eq_to_hom (by simp [opens.map, set.preimage_preimage])) ≫ H₂.app (op ((opens.map H₁.inv).obj (unop U))) := begin delta to_pushforward_of_iso, simp only [equiv.to_fun_as_coe, nat_trans.comp_app, equivalence.equivalence_mk'_unit, eq_to_hom_map, eq_to_hom_op, eq_to_hom_trans, presheaf_equiv_of_iso_unit_iso_hom_app_app, equivalence.to_adjunction, equivalence.equivalence_mk'_counit, presheaf_equiv_of_iso_inverse_map_app, adjunction.mk_of_unit_counit_hom_equiv_apply], congr, end /-- If `H : X ≅ Y` is a homeomorphism, then given an `H _* ℱ ⟶ 𝒢`, we may obtain an `ℱ ⟶ H ⁻¹ _* 𝒢`. -/ def pushforward_to_of_iso {X Y : Top} (H₁ : X ≅ Y) {ℱ : Y.presheaf C} {𝒢 : X.presheaf C} (H₂ : ℱ ⟶ H₁.hom _* 𝒢) : H₁.inv _* ℱ ⟶ 𝒢 := ((presheaf_equiv_of_iso _ H₁.symm).to_adjunction.hom_equiv ℱ 𝒢).symm H₂ @[simp] lemma pushforward_to_of_iso_app {X Y : Top} (H₁ : X ≅ Y) {ℱ : Y.presheaf C} {𝒢 : X.presheaf C} (H₂ : ℱ ⟶ H₁.hom _* 𝒢) (U : (opens X)ᵒᵖ) : (pushforward_to_of_iso H₁ H₂).app U = H₂.app (op ((opens.map H₁.inv).obj (unop U))) ≫ 𝒢.map (eq_to_hom (by simp [opens.map, set.preimage_preimage])) := by simpa [pushforward_to_of_iso, equivalence.to_adjunction] end iso variables (C) [has_colimits C] /-- Pullback a presheaf on `Y` along a continuous map `f : X ⟶ Y`, obtaining a presheaf on `X`. -/ @[simps map_app] def pullback {X Y : Top.{v}} (f : X ⟶ Y) : Y.presheaf C ⥤ X.presheaf C := Lan (opens.map f).op @[simp] lemma pullback_obj_eq_pullback_obj {C} [category C] [has_colimits C] {X Y : Top.{w}} (f : X ⟶ Y) (ℱ : Y.presheaf C) : (pullback C f).obj ℱ = pullback_obj f ℱ := rfl /-- The pullback and pushforward along a continuous map are adjoint to each other. -/ @[simps unit_app_app counit_app_app] def pushforward_pullback_adjunction {X Y : Top.{v}} (f : X ⟶ Y) : pullback C f ⊣ pushforward C f := Lan.adjunction _ _ /-- Pulling back along a homeomorphism is the same as pushing forward along its inverse. -/ def pullback_hom_iso_pushforward_inv {X Y : Top.{v}} (H : X ≅ Y) : pullback C H.hom ≅ pushforward C H.inv := adjunction.left_adjoint_uniq (pushforward_pullback_adjunction C H.hom) (presheaf_equiv_of_iso C H.symm).to_adjunction /-- Pulling back along the inverse of a homeomorphism is the same as pushing forward along it. -/ def pullback_inv_iso_pushforward_hom {X Y : Top.{v}} (H : X ≅ Y) : pullback C H.inv ≅ pushforward C H.hom := adjunction.left_adjoint_uniq (pushforward_pullback_adjunction C H.inv) (presheaf_equiv_of_iso C H).to_adjunction end presheaf end Top
ed34c8df0af084051a367d088174bfcaa42c8264
22e97a5d648fc451e25a06c668dc03ac7ed7bc25
/src/topology/category/UniformSpace.lean
5d074fe31cffde303124583c569723094d3f2898
[ "Apache-2.0" ]
permissive
keeferrowan/mathlib
f2818da875dbc7780830d09bd4c526b0764a4e50
aad2dfc40e8e6a7e258287a7c1580318e865817e
refs/heads/master
1,661,736,426,952
1,590,438,032,000
1,590,438,032,000
266,892,663
0
0
Apache-2.0
1,590,445,835,000
1,590,445,835,000
null
UTF-8
Lean
false
false
6,392
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Reid Barton, Patrick Massot, Scott Morrison -/ import category_theory.monad.limits import topology.uniform_space.completion import topology.category.Top.basic /-! # The category of uniform spaces We construct the category of uniform spaces, show that the complete separated uniform spaces form a reflective subcategory, and hence possess all limits that uniform spaces do. TODO: show that uniform spaces actually have all limits! -/ universes u open category_theory /-- A (bundled) uniform space. -/ @[reducible] def UniformSpace : Type (u+1) := bundled uniform_space namespace UniformSpace instance (x : UniformSpace) : uniform_space x := x.str /-- Construct a bundled `UniformSpace` from the underlying type and the typeclass. -/ def of (α : Type u) [uniform_space α] : UniformSpace := ⟨α⟩ /-- The information required to build morphisms for `UniformSpace`. -/ instance concrete_category_uniform_continuous : unbundled_hom @uniform_continuous := ⟨@uniform_continuous_id, @uniform_continuous.comp⟩ instance (X Y : UniformSpace) : has_coe_to_fun (X ⟶ Y) := { F := λ _, X → Y, coe := category_theory.functor.map (forget UniformSpace) } @[simp] lemma coe_comp {X Y Z : UniformSpace} (f : X ⟶ Y) (g : Y ⟶ Z) : (f ≫ g : X → Z) = g ∘ f := rfl @[simp] lemma coe_id (X : UniformSpace) : (𝟙 X : X → X) = id := rfl @[simp] lemma coe_mk {X Y : UniformSpace} (f : X → Y) (hf : uniform_continuous f) : ((⟨f, hf⟩ : X ⟶ Y) : X → Y) = f := rfl lemma hom_ext {X Y : UniformSpace} {f g : X ⟶ Y} : (f : X → Y) = g → f = g := subtype.eq /-- The forgetful functor from uniform spaces to topological spaces. -/ instance has_forget_to_Top : has_forget₂ UniformSpace.{u} Top.{u} := unbundled_hom.mk_has_forget₂ @uniform_space.to_topological_space @uniform_continuous.continuous end UniformSpace /-- A (bundled) complete separated uniform space. -/ structure CpltSepUniformSpace := (α : Type u) [is_uniform_space : uniform_space α] [is_complete_space : complete_space α] [is_separated : separated α] namespace CpltSepUniformSpace instance : has_coe_to_sort CpltSepUniformSpace := { S := Type u, coe := CpltSepUniformSpace.α } attribute [instance] is_uniform_space is_complete_space is_separated def to_UniformSpace (X : CpltSepUniformSpace) : UniformSpace := UniformSpace.of X instance (X : CpltSepUniformSpace) : complete_space ((to_UniformSpace X).α) := CpltSepUniformSpace.is_complete_space X instance (X : CpltSepUniformSpace) : separated ((to_UniformSpace X).α) := CpltSepUniformSpace.is_separated X /-- Construct a bundled `UniformSpace` from the underlying type and the appropriate typeclasses. -/ def of (X : Type u) [uniform_space X] [complete_space X] [separated X] : CpltSepUniformSpace := ⟨X⟩ /-- The category instance on `CpltSepUniformSpace`. -/ instance category : category CpltSepUniformSpace := induced_category.category to_UniformSpace /-- The concrete category instance on `CpltSepUniformSpace`. -/ instance concrete_category : concrete_category CpltSepUniformSpace := induced_category.concrete_category to_UniformSpace instance has_forget_to_UniformSpace : has_forget₂ CpltSepUniformSpace UniformSpace := induced_category.has_forget₂ to_UniformSpace end CpltSepUniformSpace namespace UniformSpace open uniform_space open CpltSepUniformSpace /-- The functor turning uniform spaces into complete separated uniform spaces. -/ noncomputable def completion_functor : UniformSpace ⥤ CpltSepUniformSpace := { obj := λ X, CpltSepUniformSpace.of (completion X), map := λ X Y f, ⟨completion.map f.1, completion.uniform_continuous_map⟩, map_id' := λ X, subtype.eq completion.map_id, map_comp' := λ X Y Z f g, subtype.eq (completion.map_comp g.property f.property).symm, }. /-- The inclusion of any uniform spaces into its completion. -/ def completion_hom (X : UniformSpace) : X ⟶ (forget₂ CpltSepUniformSpace UniformSpace).obj (completion_functor.obj X) := { val := (coe : X → completion X), property := completion.uniform_continuous_coe X } @[simp] lemma completion_hom_val (X : UniformSpace) (x) : (completion_hom X) x = (x : completion X) := rfl /-- The mate of a morphism from a `UniformSpace` to a `CpltSepUniformSpace`. -/ noncomputable def extension_hom {X : UniformSpace} {Y : CpltSepUniformSpace} (f : X ⟶ (forget₂ CpltSepUniformSpace UniformSpace).obj Y) : completion_functor.obj X ⟶ Y := { val := completion.extension f, property := completion.uniform_continuous_extension } @[simp] lemma extension_hom_val {X : UniformSpace} {Y : CpltSepUniformSpace} (f : X ⟶ (forget₂ _ _).obj Y) (x) : (extension_hom f) x = completion.extension f x := rfl. @[simp] lemma extension_comp_coe {X : UniformSpace} {Y : CpltSepUniformSpace} (f : to_UniformSpace (CpltSepUniformSpace.of (completion X)) ⟶ to_UniformSpace Y) : extension_hom (completion_hom X ≫ f) = f := by { apply subtype.eq, funext x, exact congr_fun (completion.extension_comp_coe f.property) x } /-- The completion functor is left adjoint to the forgetful functor. -/ noncomputable def adj : completion_functor ⊣ forget₂ CpltSepUniformSpace UniformSpace := adjunction.mk_of_hom_equiv { hom_equiv := λ X Y, { to_fun := λ f, completion_hom X ≫ f, inv_fun := λ f, extension_hom f, left_inv := λ f, by { dsimp, erw extension_comp_coe }, right_inv := λ f, begin apply subtype.eq, funext x, cases f, change completion.extension f_val _ = f_val x, erw completion.extension_coe, assumption end }, hom_equiv_naturality_left_symm' := λ X X' Y f g, begin apply hom_ext, funext x, dsimp, erw [coe_comp, ←completion.extension_map], refl, exact g.property, exact f.property, end } noncomputable instance : is_right_adjoint (forget₂ CpltSepUniformSpace UniformSpace) := ⟨completion_functor, adj⟩ noncomputable instance : reflective (forget₂ CpltSepUniformSpace UniformSpace) := {} open category_theory.limits -- TODO Once someone defines `has_limits UniformSpace`, turn this into an instance. noncomputable example [has_limits.{u} UniformSpace.{u}] : has_limits.{u} CpltSepUniformSpace.{u} := has_limits_of_reflective $ forget₂ CpltSepUniformSpace UniformSpace end UniformSpace
e0885dfa2352944dce503d34ca4f5ff95b7469fd
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/stage0/src/Lean/Server/Rpc/Basic.lean
e8e1bd95adeab97280fb2ea5f0a2e7c7cd3c7612
[ "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
4,782
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Wojciech Nawrocki -/ import Lean.Data.Json import Std.Dynamic /-! Allows LSP clients to make Remote Procedure Calls to the server. The single use case for these is to allow the infoview UI to refer to and manipulate heavy-weight objects residing on the server. It would be inefficient to serialize these into JSON and send over. For example, the client can format an `Expr` without transporting the whole `Environment`. All RPC requests are relative to an open file and an RPC session for that file. The client must first connect to the session using `$/lean/rpc/connect`. -/ namespace Lean.Lsp /-- An object which RPC clients can refer to without marshalling. -/ structure RpcRef where /- NOTE(WN): It is important for this to be a single-field structure in order to deserialize as an `Object` on the JS side. -/ p : USize deriving BEq, Hashable, FromJson, ToJson instance : ToString RpcRef where toString r := toString r.p end Lean.Lsp namespace Lean.Server open Std structure RpcObjectStore : Type where /-- Objects that are being kept alive for the RPC client, together with their type names, mapped to by their RPC reference. Note that we may currently have multiple references to the same object. It is only disposed of once all of those are gone. This simplifies the client a bit as it can drop every reference received separately. -/ aliveRefs : Std.PersistentHashMap Lsp.RpcRef Dynamic := {} /-- Value to use for the next `RpcRef`. It is monotonically increasing to avoid any possible bugs resulting from its reuse. -/ nextRef : USize := 0 def rpcStoreRef (any : Dynamic) : StateM RpcObjectStore Lsp.RpcRef := do let st ← get set { st with aliveRefs := st.aliveRefs.insert ⟨st.nextRef⟩ any nextRef := st.nextRef + 1 } return ⟨st.nextRef⟩ def rpcGetRef (r : Lsp.RpcRef) : ReaderT RpcObjectStore Id (Option Dynamic) := return (← read).aliveRefs.find? r def rpcReleaseRef (r : Lsp.RpcRef) : StateM RpcObjectStore Bool := do let st ← get if st.aliveRefs.contains r then set { st with aliveRefs := st.aliveRefs.erase r } return true else return false /-- `RpcEncodable α` means that `α` can be serialized in the RPC system of the Lean server. This is required when `α` contains fields which should be serialized as an RPC reference instead of being sent in full. The type wrapper `WithRpcRef` is used for these fields which should be sent as a reference. - Any type with `FromJson` and `ToJson` instance is automatically `RpcEncodable`. - If a type has an `Std.Dynamic` instance, then `WithRpcRef` can be used for its references. - `deriving RpcEncodable` acts like `FromJson`/`ToJson` but marshalls any `WithRpcRef` fields as `Lsp.RpcRef`s. -/ -- TODO(WN): for Lean.js, compile `WithRpcRef` to "opaque reference" on the client class RpcEncodable (α : Type) where rpcEncode : α → StateM RpcObjectStore Json rpcDecode : Json → ExceptT String (ReaderT RpcObjectStore Id) α export RpcEncodable (rpcEncode rpcDecode) instance : Nonempty (RpcEncodable α) := ⟨{ rpcEncode := default, rpcDecode := default }⟩ instance [FromJson α] [ToJson α] : RpcEncodable α where rpcEncode a := return toJson a rpcDecode j := ofExcept (fromJson? j) instance [RpcEncodable α] : RpcEncodable (Option α) where rpcEncode v := toJson <$> v.mapM rpcEncode rpcDecode j := do Option.mapM rpcDecode (← fromJson? j) -- TODO(WN): instance [RpcEncodable α β] [Traversable t] : RpcEncodable (t α) (t β) instance [RpcEncodable α] : RpcEncodable (Array α) where rpcEncode a := toJson <$> a.mapM rpcEncode rpcDecode b := do Array.mapM rpcDecode (← fromJson? b) instance [RpcEncodable α] [RpcEncodable β] : RpcEncodable (α × β) where rpcEncode := fun (a, b) => return toJson (← rpcEncode a, ← rpcEncode b) rpcDecode j := do let (a, b) ← fromJson? j return (← rpcDecode a, ← rpcDecode b) /-- Marks fields to encode as opaque references in LSP packets. -/ structure WithRpcRef (α : Type u) where val : α deriving Inhabited instance [TypeName α] : RpcEncodable (WithRpcRef α) := { rpcEncode, rpcDecode } where -- separate definitions to prevent inlining rpcEncode r := toJson <$> rpcStoreRef (.mk r.val) rpcDecode j := do let r ← fromJson? j match (← rpcGetRef r) with | none => throw s!"RPC reference '{r}' is not valid" | some any => if let some obj := any.get? α then return ⟨obj⟩ else throw s!"RPC call type mismatch in reference '{r}'\nexpected '{TypeName.typeName α}', got '{any.typeName}'" end Lean.Server
390315f690fcf129d2f2e6ddfb1dfbc5afb6f640
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/category_theory/types.lean
1f16fcbe2ad274fb7761c9ee9a51618a32b453a9
[ "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
11,148
lean
/- Copyright (c) 2017 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Stephen Morgan, Scott Morrison, Johannes Hölzl -/ import category_theory.epi_mono import category_theory.functor.fully_faithful import logic.equiv.basic /-! # The category `Type`. In this section we set up the theory so that Lean's types and functions between them can be viewed as a `large_category` in our framework. Lean can not transparently view a function as a morphism in this category, and needs a hint in order to be able to type check. We provide the abbreviation `as_hom f` to guide type checking, as well as a corresponding notation `↾ f`. (Entered as `\upr `.) The notation is enabled using `open_locale category_theory.Type`. We provide various simplification lemmas for functors and natural transformations valued in `Type`. We define `ulift_functor`, from `Type u` to `Type (max u v)`, and show that it is fully faithful (but not, of course, essentially surjective). We prove some basic facts about the category `Type`: * epimorphisms are surjections and monomorphisms are injections, * `iso` is both `iso` and `equiv` to `equiv` (at least within a fixed universe), * every type level `is_lawful_functor` gives a categorical functor `Type ⥤ Type` (the corresponding fact about monads is in `src/category_theory/monad/types.lean`). -/ namespace category_theory -- morphism levels before object levels. See note [category_theory universes]. universes v v' w u u' /- The `@[to_additive]` attribute is just a hint that expressions involving this instance can still be additivized. -/ @[to_additive category_theory.types] instance types : large_category (Type u) := { hom := λ a b, (a → b), id := λ a, id, comp := λ _ _ _ f g, g ∘ f } lemma types_hom {α β : Type u} : (α ⟶ β) = (α → β) := rfl lemma types_id (X : Type u) : 𝟙 X = id := rfl lemma types_comp {X Y Z : Type u} (f : X ⟶ Y) (g : Y ⟶ Z) : f ≫ g = g ∘ f := rfl @[simp] lemma types_id_apply (X : Type u) (x : X) : ((𝟙 X) : X → X) x = x := rfl @[simp] lemma types_comp_apply {X Y Z : Type u} (f : X ⟶ Y) (g : Y ⟶ Z) (x : X) : (f ≫ g) x = g (f x) := rfl @[simp] lemma hom_inv_id_apply {X Y : Type u} (f : X ≅ Y) (x : X) : f.inv (f.hom x) = x := congr_fun f.hom_inv_id x @[simp] lemma inv_hom_id_apply {X Y : Type u} (f : X ≅ Y) (y : Y) : f.hom (f.inv y) = y := congr_fun f.inv_hom_id y /-- `as_hom f` helps Lean type check a function as a morphism in the category `Type`. -/ -- Unfortunately without this wrapper we can't use `category_theory` idioms, such as `is_iso f`. abbreviation as_hom {α β : Type u} (f : α → β) : α ⟶ β := f -- If you don't mind some notation you can use fewer keystrokes: localized "notation (name := category_theory.as_hom) `↾` f : 200 := category_theory.as_hom f" in category_theory.Type -- type as \upr in VScode section -- We verify the expected type checking behaviour of `as_hom`. variables (α β γ : Type u) (f : α → β) (g : β → γ) example : α → γ := ↾f ≫ ↾g example [is_iso ↾f] : mono ↾f := by apply_instance example [is_iso ↾f] : ↾f ≫ inv ↾f = 𝟙 α := by simp end namespace functor variables {J : Type u} [category.{v} J] /-- The sections of a functor `J ⥤ Type` are the choices of a point `u j : F.obj j` for each `j`, such that `F.map f (u j) = u j` for every morphism `f : j ⟶ j'`. We later use these to define limits in `Type` and in many concrete categories. -/ def sections (F : J ⥤ Type w) : set (Π j, F.obj j) := { u | ∀ {j j'} (f : j ⟶ j'), F.map f (u j) = u j'} end functor namespace functor_to_types variables {C : Type u} [category.{v} C] (F G H : C ⥤ Type w) {X Y Z : C} variables (σ : F ⟶ G) (τ : G ⟶ H) @[simp] lemma map_comp_apply (f : X ⟶ Y) (g : Y ⟶ Z) (a : F.obj X) : (F.map (f ≫ g)) a = (F.map g) ((F.map f) a) := by simp [types_comp] @[simp] lemma map_id_apply (a : F.obj X) : (F.map (𝟙 X)) a = a := by simp [types_id] lemma naturality (f : X ⟶ Y) (x : F.obj X) : σ.app Y ((F.map f) x) = (G.map f) (σ.app X x) := congr_fun (σ.naturality f) x @[simp] lemma comp (x : F.obj X) : (σ ≫ τ).app X x = τ.app X (σ.app X x) := rfl variables {D : Type u'} [𝒟 : category.{u'} D] (I J : D ⥤ C) (ρ : I ⟶ J) {W : D} @[simp] lemma hcomp (x : (I ⋙ F).obj W) : (ρ ◫ σ).app W x = (G.map (ρ.app W)) (σ.app (I.obj W) x) := rfl @[simp] lemma map_inv_map_hom_apply (f : X ≅ Y) (x : F.obj X) : F.map f.inv (F.map f.hom x) = x := congr_fun (F.map_iso f).hom_inv_id x @[simp] lemma map_hom_map_inv_apply (f : X ≅ Y) (y : F.obj Y) : F.map f.hom (F.map f.inv y) = y := congr_fun (F.map_iso f).inv_hom_id y @[simp] lemma hom_inv_id_app_apply (α : F ≅ G) (X) (x) : α.inv.app X (α.hom.app X x) = x := congr_fun (α.hom_inv_id_app X) x @[simp] lemma inv_hom_id_app_apply (α : F ≅ G) (X) (x) : α.hom.app X (α.inv.app X x) = x := congr_fun (α.inv_hom_id_app X) x end functor_to_types /-- The isomorphism between a `Type` which has been `ulift`ed to the same universe, and the original type. -/ def ulift_trivial (V : Type u) : ulift.{u} V ≅ V := by tidy /-- The functor embedding `Type u` into `Type (max u v)`. Write this as `ulift_functor.{5 2}` to get `Type 2 ⥤ Type 5`. -/ def ulift_functor : Type u ⥤ Type (max u v) := { obj := λ X, ulift.{v} X, map := λ X Y f, λ x : ulift.{v} X, ulift.up (f x.down) } @[simp] lemma ulift_functor_map {X Y : Type u} (f : X ⟶ Y) (x : ulift.{v} X) : ulift_functor.map f x = ulift.up (f x.down) := rfl instance ulift_functor_full : full.{u} ulift_functor := { preimage := λ X Y f x, (f (ulift.up x)).down } instance ulift_functor_faithful : faithful ulift_functor := { map_injective' := λ X Y f g p, funext $ λ x, congr_arg ulift.down ((congr_fun p (ulift.up x)) : ((ulift.up (f x)) = (ulift.up (g x)))) } /-- The functor embedding `Type u` into `Type u` via `ulift` is isomorphic to the identity functor. -/ def ulift_functor_trivial : ulift_functor.{u u} ≅ 𝟭 _ := nat_iso.of_components ulift_trivial (by tidy) /-- Any term `x` of a type `X` corresponds to a morphism `punit ⟶ X`. -/ -- TODO We should connect this to a general story about concrete categories -- whose forgetful functor is representable. def hom_of_element {X : Type u} (x : X) : punit ⟶ X := λ _, x lemma hom_of_element_eq_iff {X : Type u} (x y : X) : hom_of_element x = hom_of_element y ↔ x = y := ⟨λ H, congr_fun H punit.star, by cc⟩ /-- A morphism in `Type` is a monomorphism if and only if it is injective. See <https://stacks.math.columbia.edu/tag/003C>. -/ lemma mono_iff_injective {X Y : Type u} (f : X ⟶ Y) : mono f ↔ function.injective f := begin split, { intros H x x' h, resetI, rw ←hom_of_element_eq_iff at ⊢ h, exact (cancel_mono f).mp h }, { exact λ H, ⟨λ Z, H.comp_left⟩ } end lemma injective_of_mono {X Y : Type u} (f : X ⟶ Y) [hf : mono f] : function.injective f := (mono_iff_injective f).1 hf /-- A morphism in `Type` is an epimorphism if and only if it is surjective. See <https://stacks.math.columbia.edu/tag/003C>. -/ lemma epi_iff_surjective {X Y : Type u} (f : X ⟶ Y) : epi f ↔ function.surjective f := begin split, { rintros ⟨H⟩, refine function.surjective_of_right_cancellable_Prop (λ g₁ g₂ hg, _), rw [← equiv.ulift.symm.injective.comp_left.eq_iff], apply H, change ulift.up ∘ (g₁ ∘ f) = ulift.up ∘ (g₂ ∘ f), rw hg }, { exact λ H, ⟨λ Z, H.injective_comp_right⟩ } end lemma surjective_of_epi {X Y : Type u} (f : X ⟶ Y) [hf : epi f] : function.surjective f := (epi_iff_surjective f).1 hf section /-- `of_type_functor m` converts from Lean's `Type`-based `category` to `category_theory`. This allows us to use these functors in category theory. -/ def of_type_functor (m : Type u → Type v) [_root_.functor m] [is_lawful_functor m] : Type u ⥤ Type v := { obj := m, map := λα β, _root_.functor.map, map_id' := assume α, _root_.functor.map_id, map_comp' := assume α β γ f g, funext $ assume a, is_lawful_functor.comp_map f g _ } variables (m : Type u → Type v) [_root_.functor m] [is_lawful_functor m] @[simp] lemma of_type_functor_obj : (of_type_functor m).obj = m := rfl @[simp] lemma of_type_functor_map {α β} (f : α → β) : (of_type_functor m).map f = (_root_.functor.map f : m α → m β) := rfl end end category_theory -- Isomorphisms in Type and equivalences. namespace equiv universe u variables {X Y : Type u} /-- Any equivalence between types in the same universe gives a categorical isomorphism between those types. -/ def to_iso (e : X ≃ Y) : X ≅ Y := { hom := e.to_fun, inv := e.inv_fun, hom_inv_id' := funext e.left_inv, inv_hom_id' := funext e.right_inv } @[simp] lemma to_iso_hom {e : X ≃ Y} : e.to_iso.hom = e := rfl @[simp] lemma to_iso_inv {e : X ≃ Y} : e.to_iso.inv = e.symm := rfl end equiv universe u namespace category_theory.iso open category_theory variables {X Y : Type u} /-- Any isomorphism between types gives an equivalence. -/ def to_equiv (i : X ≅ Y) : X ≃ Y := { to_fun := i.hom, inv_fun := i.inv, left_inv := λ x, congr_fun i.hom_inv_id x, right_inv := λ y, congr_fun i.inv_hom_id y } @[simp] lemma to_equiv_fun (i : X ≅ Y) : (i.to_equiv : X → Y) = i.hom := rfl @[simp] lemma to_equiv_symm_fun (i : X ≅ Y) : (i.to_equiv.symm : Y → X) = i.inv := rfl @[simp] lemma to_equiv_id (X : Type u) : (iso.refl X).to_equiv = equiv.refl X := rfl @[simp] lemma to_equiv_comp {X Y Z : Type u} (f : X ≅ Y) (g : Y ≅ Z) : (f ≪≫ g).to_equiv = f.to_equiv.trans (g.to_equiv) := rfl end category_theory.iso namespace category_theory /-- A morphism in `Type u` is an isomorphism if and only if it is bijective. -/ lemma is_iso_iff_bijective {X Y : Type u} (f : X ⟶ Y) : is_iso f ↔ function.bijective f := iff.intro (λ i, (by exactI as_iso f : X ≅ Y).to_equiv.bijective) (λ b, is_iso.of_iso (equiv.of_bijective f b).to_iso) instance : split_epi_category (Type u) := { is_split_epi_of_epi := λ X Y f hf, is_split_epi.mk' { section_ := function.surj_inv $ (epi_iff_surjective f).1 hf, id' := funext $ function.right_inverse_surj_inv $ (epi_iff_surjective f).1 hf } } end category_theory -- We prove `equiv_iso_iso` and then use that to sneakily construct `equiv_equiv_iso`. -- (In this order the proofs are handled by `obviously`.) /-- Equivalences (between types in the same universe) are the same as (isomorphic to) isomorphisms of types. -/ @[simps] def equiv_iso_iso {X Y : Type u} : (X ≃ Y) ≅ (X ≅ Y) := { hom := λ e, e.to_iso, inv := λ i, i.to_equiv, } /-- Equivalences (between types in the same universe) are the same as (equivalent to) isomorphisms of types. -/ def equiv_equiv_iso {X Y : Type u} : (X ≃ Y) ≃ (X ≅ Y) := (equiv_iso_iso).to_equiv @[simp] lemma equiv_equiv_iso_hom {X Y : Type u} (e : X ≃ Y) : equiv_equiv_iso e = e.to_iso := rfl @[simp] lemma equiv_equiv_iso_inv {X Y : Type u} (e : X ≅ Y) : equiv_equiv_iso.symm e = e.to_equiv := rfl
b493812416d3c7240c9e04a65fc62697c144630c
dd0f5513e11c52db157d2fcc8456d9401a6cd9da
/06_Inductive_Types.org.3.lean
06cb4ef7ca5e806504a3faf58d033b1eee752fd9
[]
no_license
cjmazey/lean-tutorial
ba559a49f82aa6c5848b9bf17b7389bf7f4ba645
381f61c9fcac56d01d959ae0fa6e376f2c4e3b34
refs/heads/master
1,610,286,098,832
1,447,124,923,000
1,447,124,923,000
43,082,433
0
0
null
null
null
null
UTF-8
Lean
false
false
426
lean
/- page 78 -/ import standard import data.nat open nat inductive weekday : Type := | sunday : weekday | monday : weekday | tuesday : weekday | wednesday : weekday | thursday : weekday | friday : weekday | saturday : weekday -- BEGIN definition number_of_day (d : weekday) : nat := weekday.rec_on d 1 2 3 4 5 6 7 eval number_of_day weekday.sunday eval number_of_day weekday.monday eval number_of_day weekday.tuesday -- END
b5b3163b4df936e907c87450e7a5909fbeaba8e6
3f7026ea8bef0825ca0339a275c03b911baef64d
/src/topology/uniform_space/cauchy.lean
da594192962978769458392cf3b8a31aa681c848
[ "Apache-2.0" ]
permissive
rspencer01/mathlib
b1e3afa5c121362ef0881012cc116513ab09f18c
c7d36292c6b9234dc40143c16288932ae38fdc12
refs/heads/master
1,595,010,346,708
1,567,511,503,000
1,567,511,503,000
206,071,681
0
0
Apache-2.0
1,567,513,643,000
1,567,513,643,000
null
UTF-8
Lean
false
false
16,394
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 Theory of Cauchy filters in uniform spaces. Complete uniform spaces. Totally bounded subsets. -/ import topology.uniform_space.basic open filter topological_space lattice set classical local attribute [instance, priority 0] prop_decidable variables {α : Type*} {β : Type*} [uniform_space α] universe u local notation `𝓤` := uniformity /-- A filter `f` is Cauchy if for every entourage `r`, there exists an `s ∈ f` such that `s × s ⊆ r`. This is a generalization of Cauchy sequences, because if `a : ℕ → α` then the filter of sets containing cofinitely many of the `a n` is Cauchy iff `a` is a Cauchy sequence. -/ def cauchy (f : filter α) := f ≠ ⊥ ∧ filter.prod f f ≤ (𝓤 α) def is_complete (s : set α) := ∀f, cauchy f → f ≤ principal s → ∃x∈s, f ≤ nhds x lemma cauchy_iff {f : filter α} : cauchy f ↔ (f ≠ ⊥ ∧ (∀ s ∈ 𝓤 α, ∃t∈f.sets, set.prod t t ⊆ s)) := and_congr (iff.refl _) $ forall_congr $ assume s, forall_congr $ assume hs, mem_prod_same_iff lemma cauchy_map_iff {l : filter β} {f : β → α} : cauchy (l.map f) ↔ (l ≠ ⊥ ∧ tendsto (λp:β×β, (f p.1, f p.2)) (l.prod l) (𝓤 α)) := by rw [cauchy, (≠), map_eq_bot_iff, prod_map_map_eq]; refl lemma cauchy_downwards {f g : filter α} (h_c : cauchy f) (hg : g ≠ ⊥) (h_le : g ≤ f) : cauchy g := ⟨hg, le_trans (filter.prod_mono h_le h_le) h_c.right⟩ lemma cauchy_nhds {a : α} : cauchy (nhds a) := ⟨nhds_neq_bot, calc filter.prod (nhds a) (nhds a) = (𝓤 α).lift (λs:set (α×α), (𝓤 α).lift' (λt:set(α×α), set.prod {y : α | (y, a) ∈ s} {y : α | (a, y) ∈ t})) : nhds_nhds_eq_uniformity_uniformity_prod ... ≤ (𝓤 α).lift' (λs:set (α×α), comp_rel s s) : le_infi $ assume s, le_infi $ assume hs, infi_le_of_le s $ infi_le_of_le hs $ infi_le_of_le s $ infi_le_of_le hs $ principal_mono.mpr $ assume ⟨x, y⟩ ⟨(hx : (x, a) ∈ s), (hy : (a, y) ∈ s)⟩, ⟨a, hx, hy⟩ ... ≤ 𝓤 α : comp_le_uniformity⟩ lemma cauchy_pure {a : α} : cauchy (pure a) := cauchy_downwards cauchy_nhds (show principal {a} ≠ ⊥, by simp) (pure_le_nhds a) lemma le_nhds_of_cauchy_adhp {f : filter α} {x : α} (hf : cauchy f) (adhs : f ⊓ nhds x ≠ ⊥) : f ≤ nhds x := have ∀s∈f.sets, x ∈ closure s, begin intros s hs, simp [closure_eq_nhds, inf_comm], exact assume h', adhs $ bot_unique $ h' ▸ inf_le_inf (by simp; exact hs) (le_refl _) end, calc f ≤ f.lift' (λs:set α, {y | x ∈ closure s ∧ y ∈ closure s}) : le_infi $ assume s, le_infi $ assume hs, begin rw [←forall_sets_neq_empty_iff_neq_bot] at adhs, simp [this s hs], exact mem_sets_of_superset hs subset_closure end ... ≤ f.lift' (λs:set α, {y | (x, y) ∈ closure (set.prod s s)}) : by simp [closure_prod_eq]; exact le_refl _ ... = (filter.prod f f).lift' (λs:set (α×α), {y | (x, y) ∈ closure s}) : begin rw [prod_same_eq], rw [lift'_lift'_assoc], exact monotone_prod monotone_id monotone_id, exact monotone_comp (assume s t h x h', closure_mono h h') monotone_preimage end ... ≤ (𝓤 α).lift' (λs:set (α×α), {y | (x, y) ∈ closure s}) : lift'_mono hf.right (le_refl _) ... = ((𝓤 α).lift' closure).lift' (λs:set (α×α), {y | (x, y) ∈ s}) : begin rw [lift'_lift'_assoc], exact assume s t h, closure_mono h, exact monotone_preimage end ... = (𝓤 α).lift' (λs:set (α×α), {y | (x, y) ∈ s}) : by rw [←uniformity_eq_uniformity_closure] ... = nhds x : by rw [nhds_eq_uniformity] lemma le_nhds_iff_adhp_of_cauchy {f : filter α} {x : α} (hf : cauchy f) : f ≤ nhds x ↔ f ⊓ nhds x ≠ ⊥ := ⟨assume h, (inf_of_le_left h).symm ▸ hf.left, le_nhds_of_cauchy_adhp hf⟩ lemma cauchy_map [uniform_space β] {f : filter α} {m : α → β} (hm : uniform_continuous m) (hf : cauchy f) : cauchy (map m f) := ⟨have f ≠ ⊥, from hf.left, by simp; assumption, calc filter.prod (map m f) (map m f) = map (λp:α×α, (m p.1, m p.2)) (filter.prod f f) : filter.prod_map_map_eq ... ≤ map (λp:α×α, (m p.1, m p.2)) (𝓤 α) : map_mono hf.right ... ≤ 𝓤 β : hm⟩ lemma cauchy_comap [uniform_space β] {f : filter β} {m : α → β} (hm : comap (λp:α×α, (m p.1, m p.2)) (𝓤 β) ≤ 𝓤 α) (hf : cauchy f) (hb : comap m f ≠ ⊥) : cauchy (comap m f) := ⟨hb, calc filter.prod (comap m f) (comap m f) = comap (λp:α×α, (m p.1, m p.2)) (filter.prod f f) : filter.prod_comap_comap_eq ... ≤ comap (λp:α×α, (m p.1, m p.2)) (𝓤 β) : comap_mono hf.right ... ≤ 𝓤 α : hm⟩ /-- Cauchy sequences. Usually defined on ℕ, but often it is also useful to say that a function defined on ℝ is Cauchy at +∞ to deduce convergence. Therefore, we define it in a type class that is general enough to cover both ℕ and ℝ, which are the main motivating examples. -/ def cauchy_seq [semilattice_sup β] (u : β → α) := cauchy (at_top.map u) lemma cauchy_seq_iff_prod_map [inhabited β] [semilattice_sup β] {u : β → α} : cauchy_seq u ↔ map (prod.map u u) at_top ≤ 𝓤 α := iff.trans (and_iff_right (map_ne_bot at_top_ne_bot)) (prod_map_at_top_eq u u ▸ iff.rfl) /-- A complete space is defined here using uniformities. A uniform space is complete if every Cauchy filter converges. -/ class complete_space (α : Type u) [uniform_space α] : Prop := (complete : ∀{f:filter α}, cauchy f → ∃x, f ≤ nhds x) lemma complete_univ {α : Type u} [uniform_space α] [complete_space α] : is_complete (univ : set α) := begin assume f hf _, rcases complete_space.complete hf with ⟨x, hx⟩, exact ⟨x, by simp, hx⟩ end lemma cauchy_prod [uniform_space β] {f : filter α} {g : filter β} : cauchy f → cauchy g → cauchy (filter.prod f g) | ⟨f_proper, hf⟩ ⟨g_proper, hg⟩ := ⟨filter.prod_neq_bot.2 ⟨f_proper, g_proper⟩, let p_α := λp:(α×β)×(α×β), (p.1.1, p.2.1), p_β := λp:(α×β)×(α×β), (p.1.2, p.2.2) in suffices (f.prod f).comap p_α ⊓ (g.prod g).comap p_β ≤ (𝓤 α).comap p_α ⊓ (𝓤 β).comap p_β, by simpa [uniformity_prod, filter.prod, filter.comap_inf, filter.comap_comap_comp, (∘), lattice.inf_assoc, lattice.inf_comm, lattice.inf_left_comm], lattice.inf_le_inf (filter.comap_mono hf) (filter.comap_mono hg)⟩ instance complete_space.prod [uniform_space β] [complete_space α] [complete_space β] : complete_space (α × β) := { complete := λ f hf, let ⟨x1, hx1⟩ := complete_space.complete $ cauchy_map uniform_continuous_fst hf in let ⟨x2, hx2⟩ := complete_space.complete $ cauchy_map uniform_continuous_snd hf in ⟨(x1, x2), by rw [nhds_prod_eq, filter.prod_def]; from filter.le_lift (λ s hs, filter.le_lift' $ λ t ht, have H1 : prod.fst ⁻¹' s ∈ f.sets := hx1 hs, have H2 : prod.snd ⁻¹' t ∈ f.sets := hx2 ht, filter.inter_mem_sets H1 H2)⟩ } /--If `univ` is complete, the space is a complete space -/ lemma complete_space_of_is_complete_univ (h : is_complete (univ : set α)) : complete_space α := ⟨λ f hf, let ⟨x, _, hx⟩ := h f hf ((@principal_univ α).symm ▸ le_top) in ⟨x, hx⟩⟩ lemma cauchy_iff_exists_le_nhds [complete_space α] {l : filter α} (hl : l ≠ ⊥) : cauchy l ↔ (∃x, l ≤ nhds x) := ⟨complete_space.complete, assume ⟨x, hx⟩, cauchy_downwards cauchy_nhds hl hx⟩ lemma cauchy_map_iff_exists_tendsto [complete_space α] {l : filter β} {f : β → α} (hl : l ≠ ⊥) : cauchy (l.map f) ↔ (∃x, tendsto f l (nhds x)) := cauchy_iff_exists_le_nhds (map_ne_bot hl) /-- A Cauchy sequence in a complete space converges -/ theorem cauchy_seq_tendsto_of_complete [semilattice_sup β] [complete_space α] {u : β → α} (H : cauchy_seq u) : ∃x, tendsto u at_top (nhds x) := complete_space.complete H /-- If `K` is a complete subset, then any cauchy sequence in `K` converges to a point in `K` -/ lemma cauchy_seq_tendsto_of_is_complete [semilattice_sup β] {K : set α} (h₁ : is_complete K) {u : β → α} (h₂ : ∀ n, u n ∈ K) (h₃ : cauchy_seq u) : ∃ v ∈ K, tendsto u at_top (nhds v) := h₁ _ h₃ $ le_principal_iff.2 $ mem_map_sets_iff.2 ⟨univ, univ_mem_sets, by { simp only [image_univ], rintros _ ⟨n, rfl⟩, exact h₂ n }⟩ theorem le_nhds_lim_of_cauchy {α} [uniform_space α] [complete_space α] [inhabited α] {f : filter α} (hf : cauchy f) : f ≤ nhds (lim f) := lim_spec (complete_space.complete hf) lemma is_complete_of_is_closed [complete_space α] {s : set α} (h : is_closed s) : is_complete s := λ f cf fs, let ⟨x, hx⟩ := complete_space.complete cf in ⟨x, is_closed_iff_nhds.mp h x (neq_bot_of_le_neq_bot cf.left (le_inf hx fs)), hx⟩ /-- A set `s` is totally bounded if for every entourage `d` there is a finite set of points `t` such that every element of `s` is `d`-near to some element of `t`. -/ def totally_bounded (s : set α) : Prop := ∀d ∈ 𝓤 α, ∃t : set α, finite t ∧ s ⊆ (⋃y∈t, {x | (x,y) ∈ d}) theorem totally_bounded_iff_subset {s : set α} : totally_bounded s ↔ ∀d ∈ 𝓤 α, ∃t ⊆ s, finite t ∧ s ⊆ (⋃y∈t, {x | (x,y) ∈ d}) := ⟨λ H d hd, begin rcases comp_symm_of_uniformity hd with ⟨r, hr, rs, rd⟩, rcases H r hr with ⟨k, fk, ks⟩, let u := {y ∈ k | ∃ x, x ∈ s ∧ (x, y) ∈ r}, let f : u → α := λ x, classical.some x.2.2, have : ∀ x : u, f x ∈ s ∧ (f x, x.1) ∈ r := λ x, classical.some_spec x.2.2, refine ⟨range f, _, _, _⟩, { exact range_subset_iff.2 (λ x, (this x).1) }, { have : finite u := finite_subset fk (λ x h, h.1), exact ⟨@set.fintype_range _ _ _ _ this.fintype⟩ }, { intros x xs, have := ks xs, simp at this, rcases this with ⟨y, hy, xy⟩, let z : coe_sort u := ⟨y, hy, x, xs, xy⟩, exact mem_bUnion_iff.2 ⟨_, ⟨z, rfl⟩, rd $ mem_comp_rel.2 ⟨_, xy, rs (this z).2⟩⟩ } end, λ H d hd, let ⟨t, _, ht⟩ := H d hd in ⟨t, ht⟩⟩ lemma totally_bounded_subset {s₁ s₂ : set α} (hs : s₁ ⊆ s₂) (h : totally_bounded s₂) : totally_bounded s₁ := assume d hd, let ⟨t, ht₁, ht₂⟩ := h d hd in ⟨t, ht₁, subset.trans hs ht₂⟩ lemma totally_bounded_empty : totally_bounded (∅ : set α) := λ d hd, ⟨∅, finite_empty, empty_subset _⟩ lemma totally_bounded_closure {s : set α} (h : totally_bounded s) : totally_bounded (closure s) := assume t ht, let ⟨t', ht', hct', htt'⟩ := mem_uniformity_is_closed ht, ⟨c, hcf, hc⟩ := h t' ht' in ⟨c, hcf, calc closure s ⊆ closure (⋃ (y : α) (H : y ∈ c), {x : α | (x, y) ∈ t'}) : closure_mono hc ... = _ : closure_eq_of_is_closed $ is_closed_bUnion hcf $ assume i hi, continuous_iff_is_closed.mp (continuous_id.prod_mk continuous_const) _ hct' ... ⊆ _ : bUnion_subset $ assume i hi, subset.trans (assume x, @htt' (x, i)) (subset_bUnion_of_mem hi)⟩ lemma totally_bounded_image [uniform_space β] {f : α → β} {s : set α} (hf : uniform_continuous f) (hs : totally_bounded s) : totally_bounded (f '' s) := assume t ht, have {p:α×α | (f p.1, f p.2) ∈ t} ∈ 𝓤 α, from hf ht, let ⟨c, hfc, hct⟩ := hs _ this in ⟨f '' c, finite_image f hfc, begin simp [image_subset_iff], simp [subset_def] at hct, intros x hx, simp [-mem_image], exact let ⟨i, hi, ht⟩ := hct x hx in ⟨f i, mem_image_of_mem f hi, ht⟩ end⟩ lemma cauchy_of_totally_bounded_of_ultrafilter {s : set α} {f : filter α} (hs : totally_bounded s) (hf : is_ultrafilter f) (h : f ≤ principal s) : cauchy f := ⟨hf.left, assume t ht, let ⟨t', ht'₁, ht'_symm, ht'_t⟩ := comp_symm_of_uniformity ht in let ⟨i, hi, hs_union⟩ := hs t' ht'₁ in have (⋃y∈i, {x | (x,y) ∈ t'}) ∈ f.sets, from mem_sets_of_superset (le_principal_iff.mp h) hs_union, have ∃y∈i, {x | (x,y) ∈ t'} ∈ f.sets, from mem_of_finite_Union_ultrafilter hf hi this, let ⟨y, hy, hif⟩ := this in have set.prod {x | (x,y) ∈ t'} {x | (x,y) ∈ t'} ⊆ comp_rel t' t', from assume ⟨x₁, x₂⟩ ⟨(h₁ : (x₁, y) ∈ t'), (h₂ : (x₂, y) ∈ t')⟩, ⟨y, h₁, ht'_symm h₂⟩, (filter.prod f f).sets_of_superset (prod_mem_prod hif hif) (subset.trans this ht'_t)⟩ lemma totally_bounded_iff_filter {s : set α} : totally_bounded s ↔ (∀f, f ≠ ⊥ → f ≤ principal s → ∃c ≤ f, cauchy c) := ⟨assume : totally_bounded s, assume f hf hs, ⟨ultrafilter_of f, ultrafilter_of_le, cauchy_of_totally_bounded_of_ultrafilter this (ultrafilter_ultrafilter_of hf) (le_trans ultrafilter_of_le hs)⟩, assume h : ∀f, f ≠ ⊥ → f ≤ principal s → ∃c ≤ f, cauchy c, assume d hd, classical.by_contradiction $ assume hs, have hd_cover : ∀{t:set α}, finite t → ¬ s ⊆ (⋃y∈t, {x | (x,y) ∈ d}), by simpa using hs, let f := ⨅t:{t : set α // finite t}, principal (s \ (⋃y∈t.val, {x | (x,y) ∈ d})), ⟨a, ha⟩ := @exists_mem_of_ne_empty α s (assume h, hd_cover finite_empty $ h.symm ▸ empty_subset _) in have f ≠ ⊥, from infi_neq_bot_of_directed ⟨a⟩ (assume ⟨t₁, ht₁⟩ ⟨t₂, ht₂⟩, ⟨⟨t₁ ∪ t₂, finite_union ht₁ ht₂⟩, principal_mono.mpr $ diff_subset_diff_right $ Union_subset_Union $ assume t, Union_subset_Union_const or.inl, principal_mono.mpr $ diff_subset_diff_right $ Union_subset_Union $ assume t, Union_subset_Union_const or.inr⟩) (assume ⟨t, ht⟩, by simp [diff_eq_empty]; exact hd_cover ht), have f ≤ principal s, from infi_le_of_le ⟨∅, finite_empty⟩ $ by simp; exact subset.refl s, let ⟨c, (hc₁ : c ≤ f), (hc₂ : cauchy c)⟩ := h f ‹f ≠ ⊥› this, ⟨m, hm, (hmd : set.prod m m ⊆ d)⟩ := (@mem_prod_same_iff α c d).mp $ hc₂.right hd in have c ≤ principal s, from le_trans ‹c ≤ f› this, have m ∩ s ∈ c.sets, from inter_mem_sets hm $ le_principal_iff.mp this, let ⟨y, hym, hys⟩ := inhabited_of_mem_sets hc₂.left this in let ys := (⋃y'∈({y}:set α), {x | (x, y') ∈ d}) in have m ⊆ ys, from assume y' hy', show y' ∈ (⋃y'∈({y}:set α), {x | (x, y') ∈ d}), by simp; exact @hmd (y', y) ⟨hy', hym⟩, have c ≤ principal (s - ys), from le_trans hc₁ $ infi_le_of_le ⟨{y}, finite_singleton _⟩ $ le_refl _, have (s - ys) ∩ (m ∩ s) ∈ c.sets, from inter_mem_sets (le_principal_iff.mp this) ‹m ∩ s ∈ c.sets›, have ∅ ∈ c.sets, from c.sets_of_superset this $ assume x ⟨⟨hxs, hxys⟩, hxm, _⟩, hxys $ ‹m ⊆ ys› hxm, hc₂.left $ empty_in_sets_eq_bot.mp this⟩ lemma totally_bounded_iff_ultrafilter {s : set α} : totally_bounded s ↔ (∀f, is_ultrafilter f → f ≤ principal s → cauchy f) := ⟨assume hs f, cauchy_of_totally_bounded_of_ultrafilter hs, assume h, totally_bounded_iff_filter.mpr $ assume f hf hfs, have cauchy (ultrafilter_of f), from h (ultrafilter_of f) (ultrafilter_ultrafilter_of hf) (le_trans ultrafilter_of_le hfs), ⟨ultrafilter_of f, ultrafilter_of_le, this⟩⟩ lemma compact_iff_totally_bounded_complete {s : set α} : compact s ↔ totally_bounded s ∧ is_complete s := ⟨λ hs, ⟨totally_bounded_iff_ultrafilter.2 (λ f hf1 hf2, let ⟨x, xs, fx⟩ := compact_iff_ultrafilter_le_nhds.1 hs f hf1 hf2 in cauchy_downwards (cauchy_nhds) (hf1.1) fx), λ f fc fs, let ⟨a, as, fa⟩ := hs f fc.1 fs in ⟨a, as, le_nhds_of_cauchy_adhp fc fa⟩⟩, λ ⟨ht, hc⟩, compact_iff_ultrafilter_le_nhds.2 (λf hf hfs, hc _ (totally_bounded_iff_ultrafilter.1 ht _ hf hfs) hfs)⟩ instance complete_of_compact {α : Type u} [uniform_space α] [compact_space α] : complete_space α := ⟨λf hf, by simpa [principal_univ] using (compact_iff_totally_bounded_complete.1 compact_univ).2 f hf⟩ lemma compact_of_totally_bounded_is_closed [complete_space α] {s : set α} (ht : totally_bounded s) (hc : is_closed s) : compact s := (@compact_iff_totally_bounded_complete α _ s).2 ⟨ht, is_complete_of_is_closed hc⟩
8c6d034e01e87b4bb91649093492ff93fadd3b26
80746c6dba6a866de5431094bf9f8f841b043d77
/src/data/list/basic.lean
52dceb3b4a1b25ea79860f442eed557bc991b9f8
[ "Apache-2.0" ]
permissive
leanprover-fork/mathlib-backup
8b5c95c535b148fca858f7e8db75a76252e32987
0eb9db6a1a8a605f0cf9e33873d0450f9f0ae9b0
refs/heads/master
1,585,156,056,139
1,548,864,430,000
1,548,864,438,000
143,964,213
0
0
Apache-2.0
1,550,795,966,000
1,533,705,322,000
Lean
UTF-8
Lean
false
false
188,172
lean
/- Copyright (c) 2014 Parikshit Khanna. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Parikshit Khanna, Jeremy Avigad, Leonardo de Moura, Floris van Doorn, Mario Carneiro Basic properties of lists. -/ import tactic.interactive tactic.mk_iff_of_inductive_prop tactic.split_ifs logic.basic logic.function logic.relation algebra.group order.basic data.list.defs data.nat.basic data.option.basic data.bool data.prod data.sigma data.fin open function nat namespace list universes u v w x variables {α : Type u} {β : Type v} {γ : Type w} {δ : Type x} instance : is_left_id (list α) has_append.append [] := ⟨ nil_append ⟩ instance : is_right_id (list α) has_append.append [] := ⟨ append_nil ⟩ instance : is_associative (list α) has_append.append := ⟨ append_assoc ⟩ @[simp] theorem cons_ne_nil (a : α) (l : list α) : a::l ≠ []. theorem head_eq_of_cons_eq {h₁ h₂ : α} {t₁ t₂ : list α} : (h₁::t₁) = (h₂::t₂) → h₁ = h₂ := assume Peq, list.no_confusion Peq (assume Pheq Pteq, Pheq) theorem tail_eq_of_cons_eq {h₁ h₂ : α} {t₁ t₂ : list α} : (h₁::t₁) = (h₂::t₂) → t₁ = t₂ := assume Peq, list.no_confusion Peq (assume Pheq Pteq, Pteq) theorem cons_inj {a : α} : injective (cons a) := assume l₁ l₂, assume Pe, tail_eq_of_cons_eq Pe @[simp] theorem cons_inj' (a : α) {l l' : list α} : a::l = a::l' ↔ l = l' := ⟨λ e, cons_inj e, congr_arg _⟩ /- mem -/ theorem eq_nil_of_forall_not_mem : ∀ {l : list α}, (∀ a, a ∉ l) → l = nil | [] := assume h, rfl | (b :: l') := assume h, absurd (mem_cons_self b l') (h b) theorem mem_singleton_self (a : α) : a ∈ [a] := mem_cons_self _ _ theorem eq_of_mem_singleton {a b : α} : a ∈ [b] → a = b := assume : a ∈ [b], or.elim (eq_or_mem_of_mem_cons this) (assume : a = b, this) (assume : a ∈ [], absurd this (not_mem_nil a)) @[simp] theorem mem_singleton {a b : α} : a ∈ [b] ↔ a = b := ⟨eq_of_mem_singleton, or.inl⟩ theorem mem_of_mem_cons_of_mem {a b : α} {l : list α} : a ∈ b::l → b ∈ l → a ∈ l := assume ainbl binl, or.elim (eq_or_mem_of_mem_cons ainbl) (assume : a = b, begin subst a, exact binl end) (assume : a ∈ l, this) theorem eq_or_ne_mem_of_mem {a b : α} {l : list α} (h : a ∈ b :: l) : a = b ∨ (a ≠ b ∧ a ∈ l) := classical.by_cases or.inl $ assume : a ≠ b, h.elim or.inl $ assume h, or.inr ⟨this, h⟩ theorem not_mem_append {a : α} {s t : list α} (h₁ : a ∉ s) (h₂ : a ∉ t) : a ∉ s ++ t := mt mem_append.1 $ not_or_distrib.2 ⟨h₁, h₂⟩ theorem ne_nil_of_mem {a : α} {l : list α} (h : a ∈ l) : l ≠ [] := by intro e; rw e at h; cases h theorem length_eq_zero {l : list α} : length l = 0 ↔ l = [] := ⟨eq_nil_of_length_eq_zero, λ h, h.symm ▸ rfl⟩ theorem length_pos_of_mem {a : α} : ∀ {l : list α}, a ∈ l → 0 < length l | (b::l) _ := zero_lt_succ _ theorem exists_mem_of_length_pos : ∀ {l : list α}, 0 < length l → ∃ a, a ∈ l | (b::l) _ := ⟨b, mem_cons_self _ _⟩ theorem length_pos_iff_exists_mem {l : list α} : 0 < length l ↔ ∃ a, a ∈ l := ⟨exists_mem_of_length_pos, λ ⟨a, h⟩, length_pos_of_mem h⟩ theorem length_eq_one {l : list α} : length l = 1 ↔ ∃ a, l = [a] := ⟨match l with [a], _ := ⟨a, rfl⟩ end, λ ⟨a, e⟩, e.symm ▸ rfl⟩ theorem mem_split {a : α} {l : list α} (h : a ∈ l) : ∃ s t : list α, l = s ++ a :: t := begin induction l with b l ih, {cases h}, rcases h with rfl | h, { exact ⟨[], l, rfl⟩ }, { rcases ih h with ⟨s, t, rfl⟩, exact ⟨b::s, t, rfl⟩ } end theorem mem_of_ne_of_mem {a y : α} {l : list α} (h₁ : a ≠ y) (h₂ : a ∈ y :: l) : a ∈ l := or.elim (eq_or_mem_of_mem_cons h₂) (λe, absurd e h₁) (λr, r) theorem ne_of_not_mem_cons {a b : α} {l : list α} : a ∉ b::l → a ≠ b := assume nin aeqb, absurd (or.inl aeqb) nin theorem not_mem_of_not_mem_cons {a b : α} {l : list α} : a ∉ b::l → a ∉ l := assume nin nainl, absurd (or.inr nainl) nin theorem not_mem_cons_of_ne_of_not_mem {a y : α} {l : list α} : a ≠ y → a ∉ l → a ∉ y::l := assume p1 p2, not.intro (assume Pain, absurd (eq_or_mem_of_mem_cons Pain) (not_or p1 p2)) theorem ne_and_not_mem_of_not_mem_cons {a y : α} {l : list α} : a ∉ y::l → a ≠ y ∧ a ∉ l := assume p, and.intro (ne_of_not_mem_cons p) (not_mem_of_not_mem_cons p) theorem mem_map_of_mem (f : α → β) {a : α} {l : list α} (h : a ∈ l) : f a ∈ map f l := begin induction l with b l' ih, {cases h}, {rcases h with rfl | h, {exact or.inl rfl}, {exact or.inr (ih h)}} end theorem exists_of_mem_map {f : α → β} {b : β} {l : list α} (h : b ∈ map f l) : ∃ a, a ∈ l ∧ f a = b := begin induction l with c l' ih, {cases h}, {cases (eq_or_mem_of_mem_cons h) with h h, {exact ⟨c, mem_cons_self _ _, h.symm⟩}, {rcases ih h with ⟨a, ha₁, ha₂⟩, exact ⟨a, mem_cons_of_mem _ ha₁, ha₂⟩ }} end @[simp] theorem mem_map {f : α → β} {b : β} {l : list α} : b ∈ map f l ↔ ∃ a, a ∈ l ∧ f a = b := ⟨exists_of_mem_map, λ ⟨a, la, h⟩, by rw [← h]; exact mem_map_of_mem f la⟩ @[simp] theorem mem_map_of_inj {f : α → β} (H : injective f) {a : α} {l : list α} : f a ∈ map f l ↔ a ∈ l := ⟨λ m, let ⟨a', m', e⟩ := exists_of_mem_map m in H e ▸ m', mem_map_of_mem _⟩ @[simp] theorem mem_join {a : α} : ∀ {L : list (list α)}, a ∈ join L ↔ ∃ l, l ∈ L ∧ a ∈ l | [] := ⟨false.elim, λ⟨_, h, _⟩, false.elim h⟩ | (c :: L) := by simp only [join, mem_append, @mem_join L, mem_cons_iff, or_and_distrib_right, exists_or_distrib, exists_eq_left] theorem exists_of_mem_join {a : α} {L : list (list α)} : a ∈ join L → ∃ l, l ∈ L ∧ a ∈ l := mem_join.1 theorem mem_join_of_mem {a : α} {L : list (list α)} {l} (lL : l ∈ L) (al : a ∈ l) : a ∈ join L := mem_join.2 ⟨l, lL, al⟩ @[simp] theorem mem_bind {b : β} {l : list α} {f : α → list β} : b ∈ list.bind l f ↔ ∃ a ∈ l, b ∈ f a := iff.trans mem_join ⟨λ ⟨l', h1, h2⟩, let ⟨a, al, fa⟩ := exists_of_mem_map h1 in ⟨a, al, fa.symm ▸ h2⟩, λ ⟨a, al, bfa⟩, ⟨f a, mem_map_of_mem _ al, bfa⟩⟩ theorem exists_of_mem_bind {b : β} {l : list α} {f : α → list β} : b ∈ list.bind l f → ∃ a ∈ l, b ∈ f a := mem_bind.1 theorem mem_bind_of_mem {b : β} {l : list α} {f : α → list β} {a} (al : a ∈ l) (h : b ∈ f a) : b ∈ list.bind l f := mem_bind.2 ⟨a, al, h⟩ lemma bind_map {g : α → list β} {f : β → γ} : ∀(l : list α), list.map f (l.bind g) = l.bind (λa, (g a).map f) | [] := rfl | (a::l) := by simp only [cons_bind, map_append, bind_map l] /- bounded quantifiers over lists -/ theorem forall_mem_nil (p : α → Prop) : ∀ x ∈ @nil α, p x. @[simp] theorem forall_mem_cons' {p : α → Prop} {a : α} {l : list α} : (∀ (x : α), x = a ∨ x ∈ l → p x) ↔ p a ∧ ∀ x ∈ l, p x := by simp only [or_imp_distrib, forall_and_distrib, forall_eq] theorem forall_mem_cons {p : α → Prop} {a : α} {l : list α} : (∀ x ∈ a :: l, p x) ↔ p a ∧ ∀ x ∈ l, p x := by simp only [mem_cons_iff, forall_mem_cons'] theorem forall_mem_of_forall_mem_cons {p : α → Prop} {a : α} {l : list α} (h : ∀ x ∈ a :: l, p x) : ∀ x ∈ l, p x := (forall_mem_cons.1 h).2 theorem forall_mem_singleton {p : α → Prop} {a : α} : (∀ x ∈ [a], p x) ↔ p a := by simp only [mem_singleton, forall_eq] theorem forall_mem_append {p : α → Prop} {l₁ l₂ : list α} : (∀ x ∈ l₁ ++ l₂, p x) ↔ (∀ x ∈ l₁, p x) ∧ (∀ x ∈ l₂, p x) := by simp only [mem_append, or_imp_distrib, forall_and_distrib] theorem not_exists_mem_nil (p : α → Prop) : ¬ ∃ x ∈ @nil α, p x. theorem exists_mem_cons_of {p : α → Prop} {a : α} (l : list α) (h : p a) : ∃ x ∈ a :: l, p x := bex.intro a (mem_cons_self _ _) h theorem exists_mem_cons_of_exists {p : α → Prop} {a : α} {l : list α} (h : ∃ x ∈ l, p x) : ∃ x ∈ a :: l, p x := bex.elim h (λ x xl px, bex.intro x (mem_cons_of_mem _ xl) px) theorem or_exists_of_exists_mem_cons {p : α → Prop} {a : α} {l : list α} (h : ∃ x ∈ a :: l, p x) : p a ∨ ∃ x ∈ l, p x := bex.elim h (λ x xal px, or.elim (eq_or_mem_of_mem_cons xal) (assume : x = a, begin rw ←this, left, exact px end) (assume : x ∈ l, or.inr (bex.intro x this px))) @[simp] theorem exists_mem_cons_iff (p : α → Prop) (a : α) (l : list α) : (∃ x ∈ a :: l, p x) ↔ p a ∨ ∃ x ∈ l, p x := iff.intro or_exists_of_exists_mem_cons (assume h, or.elim h (exists_mem_cons_of l) exists_mem_cons_of_exists) /- list subset -/ theorem subset_def {l₁ l₂ : list α} : l₁ ⊆ l₂ ↔ ∀ ⦃a : α⦄, a ∈ l₁ → a ∈ l₂ := iff.rfl theorem subset_app_of_subset_left (l l₁ l₂ : list α) : l ⊆ l₁ → l ⊆ l₁++l₂ := λ s, subset.trans s $ subset_append_left _ _ theorem subset_app_of_subset_right (l l₁ l₂ : list α) : l ⊆ l₂ → l ⊆ l₁++l₂ := λ s, subset.trans s $ subset_append_right _ _ @[simp] theorem cons_subset {a : α} {l m : list α} : a::l ⊆ m ↔ a ∈ m ∧ l ⊆ m := by simp only [subset_def, mem_cons_iff, or_imp_distrib, forall_and_distrib, forall_eq] theorem cons_subset_of_subset_of_mem {a : α} {l m : list α} (ainm : a ∈ m) (lsubm : l ⊆ m) : a::l ⊆ m := cons_subset.2 ⟨ainm, lsubm⟩ theorem app_subset_of_subset_of_subset {l₁ l₂ l : list α} (l₁subl : l₁ ⊆ l) (l₂subl : l₂ ⊆ l) : l₁ ++ l₂ ⊆ l := λ a h, (mem_append.1 h).elim (@l₁subl _) (@l₂subl _) theorem eq_nil_of_subset_nil : ∀ {l : list α}, l ⊆ [] → l = [] | [] s := rfl | (a::l) s := false.elim $ s $ mem_cons_self a l theorem eq_nil_iff_forall_not_mem {l : list α} : l = [] ↔ ∀ a, a ∉ l := show l = [] ↔ l ⊆ [], from ⟨λ e, e ▸ subset.refl _, eq_nil_of_subset_nil⟩ theorem map_subset {l₁ l₂ : list α} (f : α → β) (H : l₁ ⊆ l₂) : map f l₁ ⊆ map f l₂ := λ x, by simp only [mem_map, not_and, exists_imp_distrib, and_imp]; exact λ a h e, ⟨a, H h, e⟩ /- append -/ lemma append_eq_has_append {L₁ L₂ : list α} : list.append L₁ L₂ = L₁ ++ L₂ := rfl theorem append_ne_nil_of_ne_nil_left (s t : list α) : s ≠ [] → s ++ t ≠ [] := by induction s; intros; contradiction theorem append_ne_nil_of_ne_nil_right (s t : list α) : t ≠ [] → s ++ t ≠ [] := by induction s; intros; contradiction theorem append_foldl (f : α → β → α) (a : α) (s t : list β) : foldl f a (s ++ t) = foldl f (foldl f a s) t := by {induction s with b s H generalizing a, refl, simp only [foldl, cons_append], rw H _} theorem append_foldr (f : α → β → β) (a : β) (s t : list α) : foldr f a (s ++ t) = foldr f (foldr f a t) s := by {induction s with b s H generalizing a, refl, simp only [foldr, cons_append], rw H _} @[simp] lemma append_eq_nil {p q : list α} : (p ++ q) = [] ↔ p = [] ∧ q = [] := by cases p; simp only [nil_append, cons_append, eq_self_iff_true, true_and, false_and] @[simp] lemma nil_eq_append_iff {a b : list α} : [] = a ++ b ↔ a = [] ∧ b = [] := by rw [eq_comm, append_eq_nil] lemma append_eq_cons_iff {a b c : list α} {x : α} : a ++ b = x :: c ↔ (a = [] ∧ b = x :: c) ∨ (∃a', a = x :: a' ∧ c = a' ++ b) := by cases a; simp only [and_assoc, @eq_comm _ c, nil_append, cons_append, eq_self_iff_true, true_and, false_and, exists_false, false_or, or_false, exists_and_distrib_left, exists_eq_left'] lemma cons_eq_append_iff {a b c : list α} {x : α} : (x :: c : list α) = a ++ b ↔ (a = [] ∧ b = x :: c) ∨ (∃a', a = x :: a' ∧ c = a' ++ b) := by rw [eq_comm, append_eq_cons_iff] lemma append_eq_append_iff {a b c d : list α} : a ++ b = c ++ d ↔ (∃a', c = a ++ a' ∧ b = a' ++ d) ∨ (∃c', a = c ++ c' ∧ d = c' ++ b) := begin induction a generalizing c, case nil { rw nil_append, split, { rintro rfl, left, exact ⟨_, rfl, rfl⟩ }, { rintro (⟨a', rfl, rfl⟩ | ⟨a', H, rfl⟩), {refl}, {rw [← append_assoc, ← H], refl} } }, case cons : a as ih { cases c, { simp only [cons_append, nil_append, false_and, exists_false, false_or, exists_eq_left'], exact eq_comm }, { simp only [cons_append, @eq_comm _ a, ih, and_assoc, and_or_distrib_left, exists_and_distrib_left] } } end @[simp] theorem split_at_eq_take_drop : ∀ (n : ℕ) (l : list α), split_at n l = (take n l, drop n l) | 0 a := rfl | (succ n) [] := rfl | (succ n) (x :: xs) := by simp only [split_at, split_at_eq_take_drop n xs, take, drop] @[simp] theorem take_append_drop : ∀ (n : ℕ) (l : list α), take n l ++ drop n l = l | 0 a := rfl | (succ n) [] := rfl | (succ n) (x :: xs) := congr_arg (cons x) $ take_append_drop n xs -- TODO(Leo): cleanup proof after arith dec proc theorem append_inj : ∀ {s₁ s₂ t₁ t₂ : list α}, s₁ ++ t₁ = s₂ ++ t₂ → length s₁ = length s₂ → s₁ = s₂ ∧ t₁ = t₂ | [] [] t₁ t₂ h hl := ⟨rfl, h⟩ | (a::s₁) [] t₁ t₂ h hl := list.no_confusion $ eq_nil_of_length_eq_zero hl | [] (b::s₂) t₁ t₂ h hl := list.no_confusion $ eq_nil_of_length_eq_zero hl.symm | (a::s₁) (b::s₂) t₁ t₂ h hl := list.no_confusion h $ λab hap, let ⟨e1, e2⟩ := @append_inj s₁ s₂ t₁ t₂ hap (succ.inj hl) in by rw [ab, e1, e2]; exact ⟨rfl, rfl⟩ theorem append_inj_left {s₁ s₂ t₁ t₂ : list α} (h : s₁ ++ t₁ = s₂ ++ t₂) (hl : length s₁ = length s₂) : t₁ = t₂ := (append_inj h hl).right theorem append_inj_right {s₁ s₂ t₁ t₂ : list α} (h : s₁ ++ t₁ = s₂ ++ t₂) (hl : length s₁ = length s₂) : s₁ = s₂ := (append_inj h hl).left theorem append_inj' {s₁ s₂ t₁ t₂ : list α} (h : s₁ ++ t₁ = s₂ ++ t₂) (hl : length t₁ = length t₂) : s₁ = s₂ ∧ t₁ = t₂ := append_inj h $ @nat.add_right_cancel _ (length t₁) _ $ let hap := congr_arg length h in by simp only [length_append] at hap; rwa [← hl] at hap theorem append_inj_left' {s₁ s₂ t₁ t₂ : list α} (h : s₁ ++ t₁ = s₂ ++ t₂) (hl : length t₁ = length t₂) : t₁ = t₂ := (append_inj' h hl).right theorem append_inj_right' {s₁ s₂ t₁ t₂ : list α} (h : s₁ ++ t₁ = s₂ ++ t₂) (hl : length t₁ = length t₂) : s₁ = s₂ := (append_inj' h hl).left theorem append_left_cancel {s t₁ t₂ : list α} (h : s ++ t₁ = s ++ t₂) : t₁ = t₂ := append_inj_left h rfl theorem append_right_cancel {s₁ s₂ t : list α} (h : s₁ ++ t = s₂ ++ t) : s₁ = s₂ := append_inj_right' h rfl theorem append_left_inj {t₁ t₂ : list α} (s) : s ++ t₁ = s ++ t₂ ↔ t₁ = t₂ := ⟨append_left_cancel, congr_arg _⟩ theorem append_right_inj {s₁ s₂ : list α} (t) : s₁ ++ t = s₂ ++ t ↔ s₁ = s₂ := ⟨append_right_cancel, congr_arg _⟩ theorem map_eq_append_split {f : α → β} {l : list α} {s₁ s₂ : list β} (h : map f l = s₁ ++ s₂) : ∃ l₁ l₂, l = l₁ ++ l₂ ∧ map f l₁ = s₁ ∧ map f l₂ = s₂ := begin have := h, rw [← take_append_drop (length s₁) l] at this ⊢, rw map_append at this, refine ⟨_, _, rfl, append_inj this _⟩, rw [length_map, length_take, min_eq_left], rw [← length_map f l, h, length_append], apply nat.le_add_right end /- join -/ attribute [simp] join theorem join_eq_nil : ∀ {L : list (list α)}, join L = [] ↔ ∀ l ∈ L, l = [] | [] := iff_of_true rfl (forall_mem_nil _) | (l::L) := by simp only [join, append_eq_nil, join_eq_nil, forall_mem_cons] @[simp] theorem join_append (L₁ L₂ : list (list α)) : join (L₁ ++ L₂) = join L₁ ++ join L₂ := by induction L₁; [refl, simp only [*, join, cons_append, append_assoc]] /- repeat -/ @[simp] theorem repeat_succ (a : α) (n) : repeat a (n + 1) = a :: repeat a n := rfl theorem eq_of_mem_repeat {a b : α} : ∀ {n}, b ∈ repeat a n → b = a | (n+1) h := or.elim h id $ @eq_of_mem_repeat _ theorem eq_repeat_of_mem {a : α} : ∀ {l : list α}, (∀ b ∈ l, b = a) → l = repeat a l.length | [] H := rfl | (b::l) H := by cases forall_mem_cons.1 H with H₁ H₂; unfold length repeat; congr; [exact H₁, exact eq_repeat_of_mem H₂] theorem eq_repeat' {a : α} {l : list α} : l = repeat a l.length ↔ ∀ b ∈ l, b = a := ⟨λ h, h.symm ▸ λ b, eq_of_mem_repeat, eq_repeat_of_mem⟩ theorem eq_repeat {a : α} {n} {l : list α} : l = repeat a n ↔ length l = n ∧ ∀ b ∈ l, b = a := ⟨λ h, h.symm ▸ ⟨length_repeat _ _, λ b, eq_of_mem_repeat⟩, λ ⟨e, al⟩, e ▸ eq_repeat_of_mem al⟩ theorem repeat_add (a : α) (m n) : repeat a (m + n) = repeat a m ++ repeat a n := by induction m; simp only [*, zero_add, succ_add, repeat]; split; refl theorem repeat_subset_singleton (a : α) (n) : repeat a n ⊆ [a] := λ b h, mem_singleton.2 (eq_of_mem_repeat h) @[simp] theorem map_const (l : list α) (b : β) : map (function.const α b) l = repeat b l.length := by induction l; [refl, simp only [*, map]]; split; refl theorem eq_of_mem_map_const {b₁ b₂ : β} {l : list α} (h : b₁ ∈ map (function.const α b₂) l) : b₁ = b₂ := by rw map_const at h; exact eq_of_mem_repeat h @[simp] theorem map_repeat (f : α → β) (a : α) (n) : map f (repeat a n) = repeat (f a) n := by induction n; [refl, simp only [*, repeat, map]]; split; refl @[simp] theorem tail_repeat (a : α) (n) : tail (repeat a n) = repeat a n.pred := by cases n; refl @[simp] theorem join_repeat_nil (n : ℕ) : join (repeat [] n) = @nil α := by induction n; [refl, simp only [*, repeat, join, append_nil]] /- bind -/ @[simp] theorem bind_eq_bind {α β} (f : α → list β) (l : list α) : l >>= f = l.bind f := rfl @[simp] theorem bind_append {α β} (f : α → list β) (l₁ l₂ : list α) : (l₁ ++ l₂).bind f = l₁.bind f ++ l₂.bind f := append_bind _ _ _ /- concat -/ @[simp] theorem concat_nil (a : α) : concat [] a = [a] := rfl @[simp] theorem concat_cons (a b : α) (l : list α) : concat (a :: l) b = a :: concat l b := rfl @[simp] theorem concat_ne_nil (a : α) (l : list α) : concat l a ≠ [] := by induction l; intro h; contradiction @[simp] theorem concat_append (a : α) (l₁ l₂ : list α) : concat l₁ a ++ l₂ = l₁ ++ a :: l₂ := by induction l₁; simp only [*, cons_append, concat]; split; refl @[simp] theorem concat_eq_append (a : α) (l : list α) : concat l a = l ++ [a] := by induction l; simp only [*, concat]; split; refl @[simp] theorem length_concat (a : α) (l : list α) : length (concat l a) = succ (length l) := by simp only [concat_eq_append, length_append, length] theorem append_concat (a : α) (l₁ l₂ : list α) : l₁ ++ concat l₂ a = concat (l₁ ++ l₂) a := by induction l₂ with b l₂ ih; simp only [concat_eq_append, nil_append, cons_append, append_assoc] /- reverse -/ @[simp] theorem reverse_nil : reverse (@nil α) = [] := rfl local attribute [simp] reverse_core @[simp] theorem reverse_cons (a : α) (l : list α) : reverse (a::l) = reverse l ++ [a] := have aux : ∀ l₁ l₂, reverse_core l₁ l₂ ++ [a] = reverse_core l₁ (l₂ ++ [a]), by intro l₁; induction l₁; intros; [refl, simp only [*, reverse_core, cons_append]], (aux l nil).symm theorem reverse_core_eq (l₁ l₂ : list α) : reverse_core l₁ l₂ = reverse l₁ ++ l₂ := by induction l₁ generalizing l₂; [refl, simp only [*, reverse_core, reverse_cons, append_assoc]]; refl theorem reverse_cons' (a : α) (l : list α) : reverse (a::l) = concat (reverse l) a := by simp only [reverse_cons, concat_eq_append] @[simp] theorem reverse_singleton (a : α) : reverse [a] = [a] := rfl @[simp] theorem reverse_append (s t : list α) : reverse (s ++ t) = (reverse t) ++ (reverse s) := by induction s; [rw [nil_append, reverse_nil, append_nil], simp only [*, cons_append, reverse_cons, append_assoc]] @[simp] theorem reverse_reverse (l : list α) : reverse (reverse l) = l := by induction l; [refl, simp only [*, reverse_cons, reverse_append]]; refl theorem reverse_injective : injective (@reverse α) := injective_of_left_inverse reverse_reverse @[simp] theorem reverse_inj {l₁ l₂ : list α} : reverse l₁ = reverse l₂ ↔ l₁ = l₂ := reverse_injective.eq_iff @[simp] theorem reverse_eq_nil {l : list α} : reverse l = [] ↔ l = [] := @reverse_inj _ l [] theorem concat_eq_reverse_cons (a : α) (l : list α) : concat l a = reverse (a :: reverse l) := by simp only [concat_eq_append, reverse_cons, reverse_reverse] @[simp] theorem length_reverse (l : list α) : length (reverse l) = length l := by induction l; [refl, simp only [*, reverse_cons, length_append, length]] @[simp] theorem map_reverse (f : α → β) (l : list α) : map f (reverse l) = reverse (map f l) := by induction l; [refl, simp only [*, map, reverse_cons, map_append]] theorem map_reverse_core (f : α → β) (l₁ l₂ : list α) : map f (reverse_core l₁ l₂) = reverse_core (map f l₁) (map f l₂) := by simp only [reverse_core_eq, map_append, map_reverse] @[simp] theorem mem_reverse {a : α} {l : list α} : a ∈ reverse l ↔ a ∈ l := by induction l; [refl, simp only [*, reverse_cons, mem_append, mem_singleton, mem_cons_iff, not_mem_nil, false_or, or_false, or_comm]] @[simp] theorem reverse_repeat (a : α) (n) : reverse (repeat a n) = repeat a n := eq_repeat.2 ⟨by simp only [length_reverse, length_repeat], λ b h, eq_of_mem_repeat (mem_reverse.1 h)⟩ @[elab_as_eliminator] def reverse_rec_on {C : list α → Sort*} (l : list α) (H0 : C []) (H1 : ∀ (l : list α) (a : α), C l → C (l ++ [a])) : C l := begin rw ← reverse_reverse l, induction reverse l, { exact H0 }, { rw reverse_cons, exact H1 _ _ ih } end /- last -/ @[simp] theorem last_cons {a : α} {l : list α} : ∀ (h₁ : a :: l ≠ nil) (h₂ : l ≠ nil), last (a :: l) h₁ = last l h₂ := by {induction l; intros, contradiction, reflexivity} @[simp] theorem last_append {a : α} (l : list α) (h : l ++ [a] ≠ []) : last (l ++ [a]) h = a := by induction l; [refl, simp only [cons_append, last_cons _ (λ H, cons_ne_nil _ _ (append_eq_nil.1 H).2), *]] theorem last_concat {a : α} (l : list α) (h : concat l a ≠ []) : last (concat l a) h = a := by simp only [concat_eq_append, last_append] @[simp] theorem last_singleton (a : α) (h : [a] ≠ []) : last [a] h = a := rfl @[simp] theorem last_cons_cons (a₁ a₂ : α) (l : list α) (h : a₁::a₂::l ≠ []) : last (a₁::a₂::l) h = last (a₂::l) (cons_ne_nil a₂ l) := rfl theorem last_congr {l₁ l₂ : list α} (h₁ : l₁ ≠ []) (h₂ : l₂ ≠ []) (h₃ : l₁ = l₂) : last l₁ h₁ = last l₂ h₂ := by subst l₁ /- head(') and tail -/ theorem head_eq_head' [inhabited α] (l : list α) : head l = (head' l).iget := by cases l; refl @[simp] theorem head_cons [inhabited α] (a : α) (l : list α) : head (a::l) = a := rfl @[simp] theorem tail_nil : tail (@nil α) = [] := rfl @[simp] theorem tail_cons (a : α) (l : list α) : tail (a::l) = l := rfl @[simp] theorem head_append [inhabited α] (t : list α) {s : list α} (h : s ≠ []) : head (s ++ t) = head s := by {induction s, contradiction, refl} theorem cons_head_tail [inhabited α] {l : list α} (h : l ≠ []) : (head l)::(tail l) = l := by {induction l, contradiction, refl} /- map -/ lemma map_congr {f g : α → β} : ∀ {l : list α}, (∀ x ∈ l, f x = g x) → map f l = map g l | [] _ := rfl | (a::l) h := let ⟨h₁, h₂⟩ := forall_mem_cons.1 h in by rw [map, map, h₁, map_congr h₂] theorem map_concat (f : α → β) (a : α) (l : list α) : map f (concat l a) = concat (map f l) (f a) := by induction l; [refl, simp only [*, concat_eq_append, cons_append, map, map_append]]; split; refl theorem map_id' {f : α → α} (h : ∀ x, f x = x) (l : list α) : map f l = l := by induction l; [refl, simp only [*, map]]; split; refl @[simp] theorem foldl_map (g : β → γ) (f : α → γ → α) (a : α) (l : list β) : foldl f a (map g l) = foldl (λx y, f x (g y)) a l := by revert a; induction l; intros; [refl, simp only [*, map, foldl]] @[simp] theorem foldr_map (g : β → γ) (f : γ → α → α) (a : α) (l : list β) : foldr f a (map g l) = foldr (f ∘ g) a l := by revert a; induction l; intros; [refl, simp only [*, map, foldr]] theorem foldl_hom (f : α → β) (g : α → γ → α) (g' : β → γ → β) (a : α) (h : ∀a x, f (g a x) = g' (f a) x) (l : list γ) : f (foldl g a l) = foldl g' (f a) l := by revert a; induction l; intros; [refl, simp only [*, foldl]] theorem foldr_hom (f : α → β) (g : γ → α → α) (g' : γ → β → β) (a : α) (h : ∀x a, f (g x a) = g' x (f a)) (l : list γ) : f (foldr g a l) = foldr g' (f a) l := by revert a; induction l; intros; [refl, simp only [*, foldr]] theorem eq_nil_of_map_eq_nil {f : α → β} {l : list α} (h : map f l = nil) : l = nil := eq_nil_of_length_eq_zero $ by rw [← length_map f l, h]; refl @[simp] theorem map_join (f : α → β) (L : list (list α)) : map f (join L) = join (map (map f) L) := by induction L; [refl, simp only [*, join, map, map_append]] theorem bind_ret_eq_map {α β} (f : α → β) (l : list α) : l.bind (list.ret ∘ f) = map f l := by unfold list.bind; induction l; simp only [map, join, list.ret, cons_append, nil_append, *]; split; refl @[simp] theorem map_eq_map {α β} (f : α → β) (l : list α) : f <$> l = map f l := rfl @[simp] theorem map_tail (f : α → β) (l) : map f (tail l) = tail (map f l) := by cases l; refl /- map₂ -/ theorem nil_map₂ (f : α → β → γ) (l : list β) : map₂ f [] l = [] := by cases l; refl theorem map₂_nil (f : α → β → γ) (l : list α) : map₂ f l [] = [] := by cases l; refl /- sublists -/ @[simp] theorem nil_sublist : Π (l : list α), [] <+ l | [] := sublist.slnil | (a :: l) := sublist.cons _ _ a (nil_sublist l) @[refl, simp] theorem sublist.refl : Π (l : list α), l <+ l | [] := sublist.slnil | (a :: l) := sublist.cons2 _ _ a (sublist.refl l) @[trans] theorem sublist.trans {l₁ l₂ l₃ : list α} (h₁ : l₁ <+ l₂) (h₂ : l₂ <+ l₃) : l₁ <+ l₃ := sublist.rec_on h₂ (λ_ s, s) (λl₂ l₃ a h₂ IH l₁ h₁, sublist.cons _ _ _ (IH l₁ h₁)) (λl₂ l₃ a h₂ IH l₁ h₁, @sublist.cases_on _ (λl₁ l₂', l₂' = a :: l₂ → l₁ <+ a :: l₃) _ _ h₁ (λ_, nil_sublist _) (λl₁ l₂' a' h₁' e, match a', l₂', e, h₁' with ._, ._, rfl, h₁ := sublist.cons _ _ _ (IH _ h₁) end) (λl₁ l₂' a' h₁' e, match a', l₂', e, h₁' with ._, ._, rfl, h₁ := sublist.cons2 _ _ _ (IH _ h₁) end) rfl) l₁ h₁ @[simp] theorem sublist_cons (a : α) (l : list α) : l <+ a::l := sublist.cons _ _ _ (sublist.refl l) theorem sublist_of_cons_sublist {a : α} {l₁ l₂ : list α} : a::l₁ <+ l₂ → l₁ <+ l₂ := sublist.trans (sublist_cons a l₁) theorem cons_sublist_cons {l₁ l₂ : list α} (a : α) (s : l₁ <+ l₂) : a::l₁ <+ a::l₂ := sublist.cons2 _ _ _ s @[simp] theorem sublist_append_left : Π (l₁ l₂ : list α), l₁ <+ l₁++l₂ | [] l₂ := nil_sublist _ | (a::l₁) l₂ := cons_sublist_cons _ (sublist_append_left l₁ l₂) @[simp] theorem sublist_append_right : Π (l₁ l₂ : list α), l₂ <+ l₁++l₂ | [] l₂ := sublist.refl _ | (a::l₁) l₂ := sublist.cons _ _ _ (sublist_append_right l₁ l₂) theorem sublist_cons_of_sublist (a : α) {l₁ l₂ : list α} : l₁ <+ l₂ → l₁ <+ a::l₂ := sublist.cons _ _ _ theorem sublist_app_of_sublist_left {l l₁ l₂ : list α} (s : l <+ l₁) : l <+ l₁++l₂ := s.trans $ sublist_append_left _ _ theorem sublist_app_of_sublist_right {l l₁ l₂ : list α} (s : l <+ l₂) : l <+ l₁++l₂ := s.trans $ sublist_append_right _ _ theorem sublist_of_cons_sublist_cons {l₁ l₂ : list α} : ∀ {a : α}, a::l₁ <+ a::l₂ → l₁ <+ l₂ | ._ (sublist.cons ._ ._ a s) := sublist_of_cons_sublist s | ._ (sublist.cons2 ._ ._ a s) := s theorem cons_sublist_cons_iff {l₁ l₂ : list α} {a : α} : a::l₁ <+ a::l₂ ↔ l₁ <+ l₂ := ⟨sublist_of_cons_sublist_cons, cons_sublist_cons _⟩ @[simp] theorem append_sublist_append_left {l₁ l₂ : list α} : ∀ l, l++l₁ <+ l++l₂ ↔ l₁ <+ l₂ | [] := iff.rfl | (a::l) := cons_sublist_cons_iff.trans (append_sublist_append_left l) theorem append_sublist_append_of_sublist_right {l₁ l₂ : list α} (h : l₁ <+ l₂) (l) : l₁++l <+ l₂++l := begin induction h with _ _ a _ ih _ _ a _ ih, { refl }, { apply sublist_cons_of_sublist a ih }, { apply cons_sublist_cons a ih } end theorem sublist_or_mem_of_sublist {l l₁ l₂ : list α} {a : α} (h : l <+ l₁ ++ a::l₂) : l <+ l₁ ++ l₂ ∨ a ∈ l := begin induction l₁ with b l₁ IH generalizing l, { cases h, { left, exact ‹l <+ l₂› }, { right, apply mem_cons_self } }, { cases h with _ _ _ h _ _ _ h, { exact or.imp_left (sublist_cons_of_sublist _) (IH h) }, { exact (IH h).imp (cons_sublist_cons _) (mem_cons_of_mem _) } } end theorem reverse_sublist {l₁ l₂ : list α} (h : l₁ <+ l₂) : l₁.reverse <+ l₂.reverse := begin induction h with _ _ _ _ ih _ _ a _ ih, {refl}, { rw reverse_cons, exact sublist_app_of_sublist_left ih }, { rw [reverse_cons, reverse_cons], exact append_sublist_append_of_sublist_right ih [a] } end @[simp] theorem reverse_sublist_iff {l₁ l₂ : list α} : l₁.reverse <+ l₂.reverse ↔ l₁ <+ l₂ := ⟨λ h, by have := reverse_sublist h; simp only [reverse_reverse] at this; assumption, reverse_sublist⟩ @[simp] theorem append_sublist_append_right {l₁ l₂ : list α} (l) : l₁++l <+ l₂++l ↔ l₁ <+ l₂ := ⟨λ h, by have := reverse_sublist h; simp only [reverse_append, append_sublist_append_left, reverse_sublist_iff] at this; assumption, λ h, append_sublist_append_of_sublist_right h l⟩ theorem subset_of_sublist : Π {l₁ l₂ : list α}, l₁ <+ l₂ → l₁ ⊆ l₂ | ._ ._ sublist.slnil b h := h | ._ ._ (sublist.cons l₁ l₂ a s) b h := mem_cons_of_mem _ (subset_of_sublist s h) | ._ ._ (sublist.cons2 l₁ l₂ a s) b h := match eq_or_mem_of_mem_cons h with | or.inl h := h ▸ mem_cons_self _ _ | or.inr h := mem_cons_of_mem _ (subset_of_sublist s h) end theorem singleton_sublist {a : α} {l} : [a] <+ l ↔ a ∈ l := ⟨λ h, subset_of_sublist h (mem_singleton_self _), λ h, let ⟨s, t, e⟩ := mem_split h in e.symm ▸ (cons_sublist_cons _ (nil_sublist _)).trans (sublist_append_right _ _)⟩ theorem eq_nil_of_sublist_nil {l : list α} (s : l <+ []) : l = [] := eq_nil_of_subset_nil $ subset_of_sublist s theorem repeat_sublist_repeat (a : α) {m n} : repeat a m <+ repeat a n ↔ m ≤ n := ⟨λ h, by simpa only [length_repeat] using length_le_of_sublist h, λ h, by induction h; [refl, simp only [*, repeat_succ, sublist.cons]] ⟩ theorem eq_of_sublist_of_length_eq : ∀ {l₁ l₂ : list α}, l₁ <+ l₂ → length l₁ = length l₂ → l₁ = l₂ | ._ ._ sublist.slnil h := rfl | ._ ._ (sublist.cons l₁ l₂ a s) h := absurd (length_le_of_sublist s) $ not_le_of_gt $ by rw h; apply lt_succ_self | ._ ._ (sublist.cons2 l₁ l₂ a s) h := by rw [length, length] at h; injection h with h; rw eq_of_sublist_of_length_eq s h theorem eq_of_sublist_of_length_le {l₁ l₂ : list α} (s : l₁ <+ l₂) (h : length l₂ ≤ length l₁) : l₁ = l₂ := eq_of_sublist_of_length_eq s (le_antisymm (length_le_of_sublist s) h) theorem sublist_antisymm {l₁ l₂ : list α} (s₁ : l₁ <+ l₂) (s₂ : l₂ <+ l₁) : l₁ = l₂ := eq_of_sublist_of_length_le s₁ (length_le_of_sublist s₂) instance decidable_sublist [decidable_eq α] : ∀ (l₁ l₂ : list α), decidable (l₁ <+ l₂) | [] l₂ := is_true $ nil_sublist _ | (a::l₁) [] := is_false $ λh, list.no_confusion $ eq_nil_of_sublist_nil h | (a::l₁) (b::l₂) := if h : a = b then decidable_of_decidable_of_iff (decidable_sublist l₁ l₂) $ by rw [← h]; exact ⟨cons_sublist_cons _, sublist_of_cons_sublist_cons⟩ else decidable_of_decidable_of_iff (decidable_sublist (a::l₁) l₂) ⟨sublist_cons_of_sublist _, λs, match a, l₁, s, h with | a, l₁, sublist.cons ._ ._ ._ s', h := s' | ._, ._, sublist.cons2 t ._ ._ s', h := absurd rfl h end⟩ /- index_of -/ section index_of variable [decidable_eq α] @[simp] theorem index_of_nil (a : α) : index_of a [] = 0 := rfl theorem index_of_cons (a b : α) (l : list α) : index_of a (b::l) = if a = b then 0 else succ (index_of a l) := rfl theorem index_of_cons_eq {a b : α} (l : list α) : a = b → index_of a (b::l) = 0 := assume e, if_pos e @[simp] theorem index_of_cons_self (a : α) (l : list α) : index_of a (a::l) = 0 := index_of_cons_eq _ rfl @[simp] theorem index_of_cons_ne {a b : α} (l : list α) : a ≠ b → index_of a (b::l) = succ (index_of a l) := assume n, if_neg n theorem index_of_eq_length {a : α} {l : list α} : index_of a l = length l ↔ a ∉ l := begin induction l with b l ih, { exact iff_of_true rfl (not_mem_nil _) }, simp only [length, mem_cons_iff, index_of_cons], split_ifs, { exact iff_of_false (by rintro ⟨⟩) (λ H, H $ or.inl h) }, { simp only [h, false_or], rw ← ih, exact succ_inj' } end @[simp] theorem index_of_of_not_mem {l : list α} {a : α} : a ∉ l → index_of a l = length l := index_of_eq_length.2 theorem index_of_le_length {a : α} {l : list α} : index_of a l ≤ length l := begin induction l with b l ih, {refl}, simp only [length, index_of_cons], by_cases h : a = b, {rw if_pos h, exact nat.zero_le _}, rw if_neg h, exact succ_le_succ ih end theorem index_of_lt_length {a} {l : list α} : index_of a l < length l ↔ a ∈ l := ⟨λh, by_contradiction $ λ al, ne_of_lt h $ index_of_eq_length.2 al, λal, lt_of_le_of_ne index_of_le_length $ λ h, index_of_eq_length.1 h al⟩ end index_of /- nth element -/ theorem nth_le_of_mem : ∀ {a} {l : list α}, a ∈ l → ∃ n h, nth_le l n h = a | a (_ :: l) (or.inl rfl) := ⟨0, succ_pos _, rfl⟩ | a (b :: l) (or.inr m) := let ⟨n, h, e⟩ := nth_le_of_mem m in ⟨n+1, succ_lt_succ h, e⟩ theorem nth_le_nth : ∀ {l : list α} {n} h, nth l n = some (nth_le l n h) | (a :: l) 0 h := rfl | (a :: l) (n+1) h := @nth_le_nth l n _ theorem nth_ge_len : ∀ {l : list α} {n}, n ≥ length l → nth l n = none | [] n h := rfl | (a :: l) (n+1) h := nth_ge_len (le_of_succ_le_succ h) theorem nth_eq_some {l : list α} {n a} : nth l n = some a ↔ ∃ h, nth_le l n h = a := ⟨λ e, have h : n < length l, from lt_of_not_ge $ λ hn, by rw nth_ge_len hn at e; contradiction, ⟨h, by rw nth_le_nth h at e; injection e with e; apply nth_le_mem⟩, λ ⟨h, e⟩, e ▸ nth_le_nth _⟩ theorem nth_of_mem {a} {l : list α} (h : a ∈ l) : ∃ n, nth l n = some a := let ⟨n, h, e⟩ := nth_le_of_mem h in ⟨n, by rw [nth_le_nth, e]⟩ theorem nth_le_mem : ∀ (l : list α) n h, nth_le l n h ∈ l | (a :: l) 0 h := mem_cons_self _ _ | (a :: l) (n+1) h := mem_cons_of_mem _ (nth_le_mem l _ _) theorem nth_mem {l : list α} {n a} (e : nth l n = some a) : a ∈ l := let ⟨h, e⟩ := nth_eq_some.1 e in e ▸ nth_le_mem _ _ _ theorem mem_iff_nth_le {a} {l : list α} : a ∈ l ↔ ∃ n h, nth_le l n h = a := ⟨nth_le_of_mem, λ ⟨n, h, e⟩, e ▸ nth_le_mem _ _ _⟩ theorem mem_iff_nth {a} {l : list α} : a ∈ l ↔ ∃ n, nth l n = some a := mem_iff_nth_le.trans $ exists_congr $ λ n, nth_eq_some.symm @[simp] theorem nth_map (f : α → β) : ∀ l n, nth (map f l) n = (nth l n).map f | [] n := rfl | (a :: l) 0 := rfl | (a :: l) (n+1) := nth_map l n theorem nth_le_map (f : α → β) {l n} (H1 H2) : nth_le (map f l) n H1 = f (nth_le l n H2) := option.some.inj $ by rw [← nth_le_nth, nth_map, nth_le_nth]; refl @[simp] theorem nth_le_map' (f : α → β) {l n} (H) : nth_le (map f l) n H = f (nth_le l n (length_map f l ▸ H)) := nth_le_map f _ _ @[simp] lemma nth_le_singleton (a : α) {n : ℕ} (hn : n < 1) : nth_le [a] n hn = a := have hn0 : n = 0 := le_zero_iff.1 (le_of_lt_succ hn), by subst hn0; refl lemma nth_le_append : ∀ {l₁ l₂ : list α} {n : ℕ} (hn₁) (hn₂), (l₁ ++ l₂).nth_le n hn₁ = l₁.nth_le n hn₂ | [] _ n hn₁ hn₂ := (not_lt_zero _ hn₂).elim | (a::l) _ 0 hn₁ hn₂ := rfl | (a::l) _ (n+1) hn₁ hn₂ := by simp only [nth_le, cons_append]; exact nth_le_append _ _ @[simp] lemma nth_le_repeat (a : α) {n m : ℕ} (h : m < n) : (list.repeat a n).nth_le m (by rwa list.length_repeat) = a := eq_of_mem_repeat (nth_le_mem _ _ _) lemma nth_append {l₁ l₂ : list α} {n : ℕ} (hn : n < l₁.length) : (l₁ ++ l₂).nth n = l₁.nth n := have hn' : n < (l₁ ++ l₂).length := lt_of_lt_of_le hn (by rw length_append; exact le_add_right _ _), by rw [nth_le_nth hn, nth_le_nth hn', nth_le_append] @[simp] lemma nth_concat_length: ∀ (l : list α) (a : α), (l ++ [a]).nth l.length = a | [] a := rfl | (b::l) a := by rw [cons_append, length_cons, nth, nth_concat_length] @[extensionality] theorem ext : ∀ {l₁ l₂ : list α}, (∀n, nth l₁ n = nth l₂ n) → l₁ = l₂ | [] [] h := rfl | (a::l₁) [] h := by have h0 := h 0; contradiction | [] (a'::l₂) h := by have h0 := h 0; contradiction | (a::l₁) (a'::l₂) h := by have h0 : some a = some a' := h 0; injection h0 with aa; simp only [aa, ext (λn, h (n+1))]; split; refl theorem ext_le {l₁ l₂ : list α} (hl : length l₁ = length l₂) (h : ∀n h₁ h₂, nth_le l₁ n h₁ = nth_le l₂ n h₂) : l₁ = l₂ := ext $ λn, if h₁ : n < length l₁ then by rw [nth_le_nth, nth_le_nth, h n h₁ (by rwa [← hl])] else let h₁ := le_of_not_gt h₁ in by rw [nth_ge_len h₁, nth_ge_len (by rwa [← hl])] @[simp] theorem index_of_nth_le [decidable_eq α] {a : α} : ∀ {l : list α} h, nth_le l (index_of a l) h = a | (b::l) h := by by_cases h' : a = b; simp only [h', if_pos, if_false, index_of_cons, nth_le, @index_of_nth_le l] @[simp] theorem index_of_nth [decidable_eq α] {a : α} {l : list α} (h : a ∈ l) : nth l (index_of a l) = some a := by rw [nth_le_nth, index_of_nth_le (index_of_lt_length.2 h)] theorem nth_le_reverse_aux1 : ∀ (l r : list α) (i h1 h2), nth_le (reverse_core l r) (i + length l) h1 = nth_le r i h2 | [] r i := λh1 h2, rfl | (a :: l) r i := by rw (show i + length (a :: l) = i + 1 + length l, from add_right_comm i (length l) 1); exact λh1 h2, nth_le_reverse_aux1 l (a :: r) (i+1) h1 (succ_lt_succ h2) theorem nth_le_reverse_aux2 : ∀ (l r : list α) (i : nat) (h1) (h2), nth_le (reverse_core l r) (length l - 1 - i) h1 = nth_le l i h2 | [] r i h1 h2 := absurd h2 (not_lt_zero _) | (a :: l) r 0 h1 h2 := begin have aux := nth_le_reverse_aux1 l (a :: r) 0, rw zero_add at aux, exact aux _ (zero_lt_succ _) end | (a :: l) r (i+1) h1 h2 := begin have aux := nth_le_reverse_aux2 l (a :: r) i, have heq := calc length (a :: l) - 1 - (i + 1) = length l - (1 + i) : by rw add_comm; refl ... = length l - 1 - i : by rw nat.sub_sub, rw [← heq] at aux, apply aux end @[simp] theorem nth_le_reverse (l : list α) (i : nat) (h1 h2) : nth_le (reverse l) (length l - 1 - i) h1 = nth_le l i h2 := nth_le_reverse_aux2 _ _ _ _ _ lemma modify_nth_tail_modify_nth_tail {f g : list α → list α} (m : ℕ) : ∀n (l:list α), (l.modify_nth_tail f n).modify_nth_tail g (m + n) = l.modify_nth_tail (λl, (f l).modify_nth_tail g m) n | 0 l := rfl | (n+1) [] := rfl | (n+1) (a::l) := congr_arg (list.cons a) (modify_nth_tail_modify_nth_tail n l) lemma modify_nth_tail_modify_nth_tail_le {f g : list α → list α} (m n : ℕ) (l : list α) (h : n ≤ m) : (l.modify_nth_tail f n).modify_nth_tail g m = l.modify_nth_tail (λl, (f l).modify_nth_tail g (m - n)) n := begin rcases le_iff_exists_add.1 h with ⟨m, rfl⟩, rw [nat.add_sub_cancel_left, add_comm, modify_nth_tail_modify_nth_tail] end lemma modify_nth_tail_modify_nth_tail_same {f g : list α → list α} (n : ℕ) (l:list α) : (l.modify_nth_tail f n).modify_nth_tail g n = l.modify_nth_tail (g ∘ f) n := by rw [modify_nth_tail_modify_nth_tail_le n n l (le_refl n), nat.sub_self]; refl lemma modify_nth_tail_id : ∀n (l:list α), l.modify_nth_tail id n = l | 0 l := rfl | (n+1) [] := rfl | (n+1) (a::l) := congr_arg (list.cons a) (modify_nth_tail_id n l) theorem remove_nth_eq_nth_tail : ∀ n (l : list α), remove_nth l n = modify_nth_tail tail n l | 0 l := by cases l; refl | (n+1) [] := rfl | (n+1) (a::l) := congr_arg (cons _) (remove_nth_eq_nth_tail _ _) theorem update_nth_eq_modify_nth (a : α) : ∀ n (l : list α), update_nth l n a = modify_nth (λ _, a) n l | 0 l := by cases l; refl | (n+1) [] := rfl | (n+1) (b::l) := congr_arg (cons _) (update_nth_eq_modify_nth _ _) theorem modify_nth_eq_update_nth (f : α → α) : ∀ n (l : list α), modify_nth f n l = ((λ a, update_nth l n (f a)) <$> nth l n).get_or_else l | 0 l := by cases l; refl | (n+1) [] := rfl | (n+1) (b::l) := (congr_arg (cons b) (modify_nth_eq_update_nth n l)).trans $ by cases nth l n; refl theorem nth_modify_nth (f : α → α) : ∀ n (l : list α) m, nth (modify_nth f n l) m = (λ a, if n = m then f a else a) <$> nth l m | n l 0 := by cases l; cases n; refl | n [] (m+1) := by cases n; refl | 0 (a::l) (m+1) := by cases nth l m; refl | (n+1) (a::l) (m+1) := (nth_modify_nth n l m).trans $ by cases nth l m with b; by_cases n = m; simp only [h, if_pos, if_true, if_false, option.map_none, option.map_some, mt succ_inj, not_false_iff] theorem modify_nth_tail_length (f : list α → list α) (H : ∀ l, length (f l) = length l) : ∀ n l, length (modify_nth_tail f n l) = length l | 0 l := H _ | (n+1) [] := rfl | (n+1) (a::l) := @congr_arg _ _ _ _ (+1) (modify_nth_tail_length _ _) @[simp] theorem modify_nth_length (f : α → α) : ∀ n l, length (modify_nth f n l) = length l := modify_nth_tail_length _ (λ l, by cases l; refl) @[simp] theorem update_nth_length (l : list α) (n) (a : α) : length (update_nth l n a) = length l := by simp only [update_nth_eq_modify_nth, modify_nth_length] @[simp] theorem nth_modify_nth_eq (f : α → α) (n) (l : list α) : nth (modify_nth f n l) n = f <$> nth l n := by simp only [nth_modify_nth, if_pos] @[simp] theorem nth_modify_nth_ne (f : α → α) {m n} (l : list α) (h : m ≠ n) : nth (modify_nth f m l) n = nth l n := by simp only [nth_modify_nth, if_neg h, id_map'] theorem nth_update_nth_eq (a : α) (n) (l : list α) : nth (update_nth l n a) n = (λ _, a) <$> nth l n := by simp only [update_nth_eq_modify_nth, nth_modify_nth_eq] theorem nth_update_nth_of_lt (a : α) {n} {l : list α} (h : n < length l) : nth (update_nth l n a) n = some a := by rw [nth_update_nth_eq, nth_le_nth h]; refl theorem nth_update_nth_ne (a : α) {m n} (l : list α) (h : m ≠ n) : nth (update_nth l m a) n = nth l n := by simp only [update_nth_eq_modify_nth, nth_modify_nth_ne _ _ h] section insert_nth variable {a : α} @[simp] lemma insert_nth_nil (a : α) : insert_nth 0 a [] = [a] := rfl lemma length_insert_nth : ∀n as, n ≤ length as → length (insert_nth n a as) = length as + 1 | 0 as h := rfl | (n+1) [] h := (nat.not_succ_le_zero _ h).elim | (n+1) (a'::as) h := congr_arg nat.succ $ length_insert_nth n as (nat.le_of_succ_le_succ h) lemma remove_nth_insert_nth (n:ℕ) (l : list α) : (l.insert_nth n a).remove_nth n = l := by rw [remove_nth_eq_nth_tail, insert_nth, modify_nth_tail_modify_nth_tail_same]; from modify_nth_tail_id _ _ lemma insert_nth_remove_nth_of_ge : ∀n m as, n < length as → m ≥ n → insert_nth m a (as.remove_nth n) = (as.insert_nth (m + 1) a).remove_nth n | 0 0 [] has _ := (lt_irrefl _ has).elim | 0 0 (a::as) has hmn := by simp [remove_nth, insert_nth] | 0 (m+1) (a::as) has hmn := rfl | (n+1) (m+1) (a::as) has hmn := congr_arg (cons a) $ insert_nth_remove_nth_of_ge n m as (nat.lt_of_succ_lt_succ has) (nat.le_of_succ_le_succ hmn) lemma insert_nth_remove_nth_of_le : ∀n m as, n < length as → m ≤ n → insert_nth m a (as.remove_nth n) = (as.insert_nth m a).remove_nth (n + 1) | n 0 (a :: as) has hmn := rfl | (n + 1) (m + 1) (a :: as) has hmn := congr_arg (cons a) $ insert_nth_remove_nth_of_le n m as (nat.lt_of_succ_lt_succ has) (nat.le_of_succ_le_succ hmn) lemma insert_nth_comm (a b : α) : ∀(i j : ℕ) (l : list α) (h : i ≤ j) (hj : j ≤ length l), (l.insert_nth i a).insert_nth (j + 1) b = (l.insert_nth j b).insert_nth i a | 0 j l := by simp [insert_nth] | (i + 1) 0 l := assume h, (nat.not_lt_zero _ h).elim | (i + 1) (j+1) [] := by simp | (i + 1) (j+1) (c::l) := assume h₀ h₁, by simp [insert_nth]; exact insert_nth_comm i j l (nat.le_of_succ_le_succ h₀) (nat.le_of_succ_le_succ h₁) end insert_nth /- take, drop -/ @[simp] theorem take_zero (l : list α) : take 0 l = [] := rfl @[simp] theorem take_nil : ∀ n, take n [] = ([] : list α) | 0 := rfl | (n+1) := rfl theorem take_cons (n) (a : α) (l : list α) : take (succ n) (a::l) = a :: take n l := rfl @[simp] theorem take_all : ∀ (l : list α), take (length l) l = l | [] := rfl | (a::l) := begin change a :: (take (length l) l) = a :: l, rw take_all end theorem take_all_of_ge : ∀ {n} {l : list α}, n ≥ length l → take n l = l | 0 [] h := rfl | 0 (a::l) h := absurd h (not_le_of_gt (zero_lt_succ _)) | (n+1) [] h := rfl | (n+1) (a::l) h := begin change a :: take n l = a :: l, rw [take_all_of_ge (le_of_succ_le_succ h)] end @[simp] theorem take_left : ∀ l₁ l₂ : list α, take (length l₁) (l₁ ++ l₂) = l₁ | [] l₂ := rfl | (a::l₁) l₂ := congr_arg (cons a) (take_left l₁ l₂) theorem take_left' {l₁ l₂ : list α} {n} (h : length l₁ = n) : take n (l₁ ++ l₂) = l₁ := by rw ← h; apply take_left theorem take_take : ∀ (n m) (l : list α), take n (take m l) = take (min n m) l | n 0 l := by rw [min_zero, take_zero, take_nil] | 0 m l := by rw [zero_min, take_zero, take_zero] | (succ n) (succ m) nil := by simp only [take_nil] | (succ n) (succ m) (a::l) := by simp only [take, min_succ_succ, take_take n m l]; split; refl @[simp] theorem drop_nil : ∀ n, drop n [] = ([] : list α) | 0 := rfl | (n+1) := rfl @[simp] theorem drop_one : ∀ l : list α, drop 1 l = tail l | [] := rfl | (a :: l) := rfl theorem drop_add : ∀ m n (l : list α), drop (m + n) l = drop m (drop n l) | m 0 l := rfl | m (n+1) [] := (drop_nil _).symm | m (n+1) (a::l) := drop_add m n _ @[simp] theorem drop_left : ∀ l₁ l₂ : list α, drop (length l₁) (l₁ ++ l₂) = l₂ | [] l₂ := rfl | (a::l₁) l₂ := drop_left l₁ l₂ theorem drop_left' {l₁ l₂ : list α} {n} (h : length l₁ = n) : drop n (l₁ ++ l₂) = l₂ := by rw ← h; apply drop_left theorem drop_eq_nth_le_cons : ∀ {n} {l : list α} h, drop n l = nth_le l n h :: drop (n+1) l | 0 (a::l) h := rfl | (n+1) (a::l) h := @drop_eq_nth_le_cons n _ _ @[simp] lemma drop_all (l : list α) : l.drop l.length = [] := calc l.drop l.length = (l ++ []).drop l.length : by simp ... = [] : drop_left _ _ lemma drop_append_of_le_length : ∀ {l₁ l₂ : list α} {n : ℕ}, n ≤ l₁.length → (l₁ ++ l₂).drop n = l₁.drop n ++ l₂ | l₁ l₂ 0 hn := by simp | [] l₂ (n+1) hn := absurd hn dec_trivial | (a::l₁) l₂ (n+1) hn := by rw [drop, cons_append, drop, drop_append_of_le_length (le_of_succ_le_succ hn)] lemma take_append_of_le_length : ∀ {l₁ l₂ : list α} {n : ℕ}, n ≤ l₁.length → (l₁ ++ l₂).take n = l₁.take n | l₁ l₂ 0 hn := by simp | [] l₂ (n+1) hn := absurd hn dec_trivial | (a::l₁) l₂ (n+1) hn := by rw [list.take, list.cons_append, list.take, take_append_of_le_length (le_of_succ_le_succ hn)] @[simp] theorem drop_drop (n : ℕ) : ∀ (m) (l : list α), drop n (drop m l) = drop (n + m) l | m [] := by simp | 0 l := by simp | (m+1) (a::l) := calc drop n (drop (m + 1) (a :: l)) = drop n (drop m l) : rfl ... = drop (n + m) l : drop_drop m l ... = drop (n + (m + 1)) (a :: l) : rfl theorem drop_take : ∀ (m : ℕ) (n : ℕ) (l : list α), drop m (take (m + n) l) = take n (drop m l) | 0 n _ := by simp | (m+1) n nil := by simp | (m+1) n (_::l) := have h: m + 1 + n = (m+n) + 1, by simp, by simpa [take_cons, h] using drop_take m n l theorem modify_nth_tail_eq_take_drop (f : list α → list α) (H : f [] = []) : ∀ n l, modify_nth_tail f n l = take n l ++ f (drop n l) | 0 l := rfl | (n+1) [] := H.symm | (n+1) (b::l) := congr_arg (cons b) (modify_nth_tail_eq_take_drop n l) theorem modify_nth_eq_take_drop (f : α → α) : ∀ n l, modify_nth f n l = take n l ++ modify_head f (drop n l) := modify_nth_tail_eq_take_drop _ rfl theorem modify_nth_eq_take_cons_drop (f : α → α) {n l} (h) : modify_nth f n l = take n l ++ f (nth_le l n h) :: drop (n+1) l := by rw [modify_nth_eq_take_drop, drop_eq_nth_le_cons h]; refl theorem update_nth_eq_take_cons_drop (a : α) {n l} (h : n < length l) : update_nth l n a = take n l ++ a :: drop (n+1) l := by rw [update_nth_eq_modify_nth, modify_nth_eq_take_cons_drop _ h] @[simp] lemma update_nth_eq_nil (l : list α) (n : ℕ) (a : α) : l.update_nth n a = [] ↔ l = [] := by cases l; cases n; simp only [update_nth] section take' variable [inhabited α] @[simp] theorem take'_length : ∀ n l, length (@take' α _ n l) = n | 0 l := rfl | (n+1) l := congr_arg succ (take'_length _ _) @[simp] theorem take'_nil : ∀ n, take' n (@nil α) = repeat (default _) n | 0 := rfl | (n+1) := congr_arg (cons _) (take'_nil _) theorem take'_eq_take : ∀ {n} {l : list α}, n ≤ length l → take' n l = take n l | 0 l h := rfl | (n+1) (a::l) h := congr_arg (cons _) $ take'_eq_take $ le_of_succ_le_succ h @[simp] theorem take'_left (l₁ l₂ : list α) : take' (length l₁) (l₁ ++ l₂) = l₁ := (take'_eq_take (by simp only [length_append, nat.le_add_right])).trans (take_left _ _) theorem take'_left' {l₁ l₂ : list α} {n} (h : length l₁ = n) : take' n (l₁ ++ l₂) = l₁ := by rw ← h; apply take'_left end take' /- foldl, foldr -/ lemma foldl_ext (f g : α → β → α) (a : α) {l : list β} (H : ∀ a : α, ∀ b ∈ l, f a b = g a b) : foldl f a l = foldl g a l := begin induction l with hd tl ih generalizing a, {refl}, unfold foldl, rw [ih (λ a b bin, H a b $ mem_cons_of_mem _ bin), H a hd (mem_cons_self _ _)] end lemma foldr_ext (f g : α → β → β) (b : β) {l : list α} (H : ∀ a ∈ l, ∀ b : β, f a b = g a b) : foldr f b l = foldr g b l := begin induction l with hd tl ih, {refl}, simp only [mem_cons_iff, or_imp_distrib, forall_and_distrib, forall_eq] at H, simp only [foldr, ih H.2, H.1] end @[simp] theorem foldl_nil (f : α → β → α) (a : α) : foldl f a [] = a := rfl @[simp] theorem foldl_cons (f : α → β → α) (a : α) (b : β) (l : list β) : foldl f a (b::l) = foldl f (f a b) l := rfl @[simp] theorem foldr_nil (f : α → β → β) (b : β) : foldr f b [] = b := rfl @[simp] theorem foldr_cons (f : α → β → β) (b : β) (a : α) (l : list α) : foldr f b (a::l) = f a (foldr f b l) := rfl @[simp] theorem foldl_append (f : α → β → α) : ∀ (a : α) (l₁ l₂ : list β), foldl f a (l₁++l₂) = foldl f (foldl f a l₁) l₂ | a [] l₂ := rfl | a (b::l₁) l₂ := by simp only [cons_append, foldl_cons, foldl_append (f a b) l₁ l₂] @[simp] theorem foldr_append (f : α → β → β) : ∀ (b : β) (l₁ l₂ : list α), foldr f b (l₁++l₂) = foldr f (foldr f b l₂) l₁ | b [] l₂ := rfl | b (a::l₁) l₂ := by simp only [cons_append, foldr_cons, foldr_append b l₁ l₂] @[simp] theorem foldl_join (f : α → β → α) : ∀ (a : α) (L : list (list β)), foldl f a (join L) = foldl (foldl f) a L | a [] := rfl | a (l::L) := by simp only [join, foldl_append, foldl_cons, foldl_join (foldl f a l) L] @[simp] theorem foldr_join (f : α → β → β) : ∀ (b : β) (L : list (list α)), foldr f b (join L) = foldr (λ l b, foldr f b l) b L | a [] := rfl | a (l::L) := by simp only [join, foldr_append, foldr_join a L, foldr_cons] theorem foldl_reverse (f : α → β → α) (a : α) (l : list β) : foldl f a (reverse l) = foldr (λx y, f y x) a l := by induction l; [refl, simp only [*, reverse_cons, foldl_append, foldl_cons, foldl_nil, foldr]] theorem foldr_reverse (f : α → β → β) (a : β) (l : list α) : foldr f a (reverse l) = foldl (λx y, f y x) a l := let t := foldl_reverse (λx y, f y x) a (reverse l) in by rw reverse_reverse l at t; rwa t @[simp] theorem foldr_eta : ∀ (l : list α), foldr cons [] l = l | [] := rfl | (x::l) := by simp only [foldr_cons, foldr_eta l]; split; refl @[simp] theorem reverse_foldl {l : list α} : reverse (foldl (λ t h, h :: t) [] l) = l := by rw ←foldr_reverse; simp /- scanr -/ @[simp] theorem scanr_nil (f : α → β → β) (b : β) : scanr f b [] = [b] := rfl @[simp] theorem scanr_aux_cons (f : α → β → β) (b : β) : ∀ (a : α) (l : list α), scanr_aux f b (a::l) = (foldr f b (a::l), scanr f b l) | a [] := rfl | a (x::l) := let t := scanr_aux_cons x l in by simp only [scanr, scanr_aux, t, foldr_cons] @[simp] theorem scanr_cons (f : α → β → β) (b : β) (a : α) (l : list α) : scanr f b (a::l) = foldr f b (a::l) :: scanr f b l := by simp only [scanr, scanr_aux_cons, foldr_cons]; split; refl section foldl_eq_foldr -- foldl and foldr coincide when f is commutative and associative variables {f : α → α → α} (hcomm : commutative f) (hassoc : associative f) include hassoc theorem foldl1_eq_foldr1 : ∀ a b l, foldl f a (l++[b]) = foldr f b (a::l) | a b nil := rfl | a b (c :: l) := by simp only [cons_append, foldl_cons, foldr_cons, foldl1_eq_foldr1 _ _ l]; rw hassoc include hcomm theorem foldl_eq_of_comm_of_assoc : ∀ a b l, foldl f a (b::l) = f b (foldl f a l) | a b nil := hcomm a b | a b (c::l) := by simp only [foldl_cons]; rw [← foldl_eq_of_comm_of_assoc, right_comm _ hcomm hassoc]; refl theorem foldl_eq_foldr : ∀ a l, foldl f a l = foldr f a l | a nil := rfl | a (b :: l) := by simp only [foldr_cons, foldl_eq_of_comm_of_assoc hcomm hassoc]; rw (foldl_eq_foldr a l) end foldl_eq_foldr section variables {op : α → α → α} [ha : is_associative α op] [hc : is_commutative α op] local notation a * b := op a b local notation l <*> a := foldl op a l include ha lemma foldl_assoc : ∀ {l : list α} {a₁ a₂}, l <*> (a₁ * a₂) = a₁ * (l <*> a₂) | [] a₁ a₂ := rfl | (a :: l) a₁ a₂ := calc a::l <*> (a₁ * a₂) = l <*> (a₁ * (a₂ * a)) : by simp only [foldl_cons, ha.assoc] ... = a₁ * (a::l <*> a₂) : by rw [foldl_assoc, foldl_cons] lemma foldl_op_eq_op_foldr_assoc : ∀{l : list α} {a₁ a₂}, (l <*> a₁) * a₂ = a₁ * l.foldr (*) a₂ | [] a₁ a₂ := rfl | (a :: l) a₁ a₂ := by simp only [foldl_cons, foldr_cons, foldl_assoc, ha.assoc]; rw [foldl_op_eq_op_foldr_assoc] include hc lemma foldl_assoc_comm_cons {l : list α} {a₁ a₂} : (a₁ :: l) <*> a₂ = a₁ * (l <*> a₂) := by rw [foldl_cons, hc.comm, foldl_assoc] end /- mfoldl, mfoldr -/ section mfoldl_mfoldr variables {m : Type v → Type w} [monad m] @[simp] theorem mfoldl_nil (f : β → α → m β) {b} : mfoldl f b [] = pure b := rfl @[simp] theorem mfoldr_nil (f : α → β → m β) {b} : mfoldr f b [] = pure b := rfl @[simp] theorem mfoldl_cons {f : β → α → m β} {b a l} : mfoldl f b (a :: l) = f b a >>= λ b', mfoldl f b' l := rfl @[simp] theorem mfoldr_cons {f : α → β → m β} {b a l} : mfoldr f b (a :: l) = mfoldr f b l >>= f a := rfl variables [is_lawful_monad m] @[simp] theorem mfoldl_append {f : β → α → m β} : ∀ {b l₁ l₂}, mfoldl f b (l₁ ++ l₂) = mfoldl f b l₁ >>= λ x, mfoldl f x l₂ | _ [] _ := by simp only [nil_append, mfoldl_nil, pure_bind] | _ (_::_) _ := by simp only [cons_append, mfoldl_cons, mfoldl_append, bind_assoc] @[simp] theorem mfoldr_append {f : α → β → m β} : ∀ {b l₁ l₂}, mfoldr f b (l₁ ++ l₂) = mfoldr f b l₂ >>= λ x, mfoldr f x l₁ | _ [] _ := by simp only [nil_append, mfoldr_nil, bind_pure] | _ (_::_) _ := by simp only [mfoldr_cons, cons_append, mfoldr_append, bind_assoc] end mfoldl_mfoldr /- sum -/ attribute [to_additive list.sum] list.prod attribute [to_additive list.sum.equations._eqn_1] list.prod.equations._eqn_1 section monoid variables [monoid α] {l l₁ l₂ : list α} {a : α} @[simp, to_additive list.sum_nil] theorem prod_nil : ([] : list α).prod = 1 := rfl @[simp, to_additive list.sum_cons] theorem prod_cons : (a::l).prod = a * l.prod := calc (a::l).prod = foldl (*) (a * 1) l : by simp only [list.prod, foldl_cons, one_mul, mul_one] ... = _ : foldl_assoc @[simp, to_additive list.sum_append] theorem prod_append : (l₁ ++ l₂).prod = l₁.prod * l₂.prod := calc (l₁ ++ l₂).prod = foldl (*) (foldl (*) 1 l₁ * 1) l₂ : by simp [list.prod] ... = l₁.prod * l₂.prod : foldl_assoc @[simp, to_additive list.sum_join] theorem prod_join {l : list (list α)} : l.join.prod = (l.map list.prod).prod := by induction l; [refl, simp only [*, list.join, map, prod_append, prod_cons]] end monoid @[simp, to_additive list.sum_erase] theorem prod_erase [decidable_eq α] [comm_monoid α] {a} : Π {l : list α}, a ∈ l → a * (l.erase a).prod = l.prod | (b::l) h := begin rcases eq_or_ne_mem_of_mem h with rfl | ⟨ne, h⟩, { simp only [list.erase, if_pos, prod_cons] }, { simp only [list.erase, if_neg (mt eq.symm ne), prod_cons, prod_erase h, mul_left_comm a b] } end lemma dvd_prod [comm_semiring α] {a} {l : list α} (ha : a ∈ l) : a ∣ l.prod := let ⟨s, t, h⟩ := mem_split ha in by rw [h, prod_append, prod_cons, mul_left_comm]; exact dvd_mul_right _ _ @[simp] theorem sum_const_nat (m n : ℕ) : sum (list.repeat m n) = m * n := by induction n; [refl, simp only [*, repeat_succ, sum_cons, nat.mul_succ, add_comm]] @[simp] theorem length_join (L : list (list α)) : length (join L) = sum (map length L) := by induction L; [refl, simp only [*, join, map, sum_cons, length_append]] @[simp] theorem length_bind (l : list α) (f : α → list β) : length (list.bind l f) = sum (map (length ∘ f) l) := by rw [list.bind, length_join, map_map] /- lexicographic ordering -/ inductive lex (r : α → α → Prop) : list α → list α → Prop | nil {} {a l} : lex [] (a :: l) | cons {a l₁ l₂} (h : lex l₁ l₂) : lex (a :: l₁) (a :: l₂) | rel {a₁ l₁ a₂ l₂} (h : r a₁ a₂) : lex (a₁ :: l₁) (a₂ :: l₂) namespace lex theorem cons_iff {r : α → α → Prop} [is_irrefl α r] {a l₁ l₂} : lex r (a :: l₁) (a :: l₂) ↔ lex r l₁ l₂ := ⟨λ h, by cases h with _ _ _ _ _ h _ _ _ _ h; [exact h, exact (irrefl_of r a h).elim], lex.cons⟩ instance is_order_connected (r : α → α → Prop) [is_order_connected α r] [is_trichotomous α r] : is_order_connected (list α) (lex r) := ⟨λ l₁, match l₁ with | _, [], c::l₃, nil := or.inr nil | _, [], c::l₃, rel _ := or.inr nil | _, [], c::l₃, cons _ := or.inr nil | _, b::l₂, c::l₃, nil := or.inl nil | a::l₁, b::l₂, c::l₃, rel h := (is_order_connected.conn _ b _ h).imp rel rel | a::l₁, b::l₂, _::l₃, cons h := begin rcases trichotomous_of r a b with ab | rfl | ab, { exact or.inl (rel ab) }, { exact (_match _ l₂ _ h).imp cons cons }, { exact or.inr (rel ab) } end end⟩ instance is_trichotomous (r : α → α → Prop) [is_trichotomous α r] : is_trichotomous (list α) (lex r) := ⟨λ l₁, match l₁ with | [], [] := or.inr (or.inl rfl) | [], b::l₂ := or.inl nil | a::l₁, [] := or.inr (or.inr nil) | a::l₁, b::l₂ := begin rcases trichotomous_of r a b with ab | rfl | ab, { exact or.inl (rel ab) }, { exact (_match l₁ l₂).imp cons (or.imp (congr_arg _) cons) }, { exact or.inr (or.inr (rel ab)) } end end⟩ instance is_asymm (r : α → α → Prop) [is_asymm α r] : is_asymm (list α) (lex r) := ⟨λ l₁, match l₁ with | a::l₁, b::l₂, lex.rel h₁, lex.rel h₂ := asymm h₁ h₂ | a::l₁, b::l₂, lex.rel h₁, lex.cons h₂ := asymm h₁ h₁ | a::l₁, b::l₂, lex.cons h₁, lex.rel h₂ := asymm h₂ h₂ | a::l₁, b::l₂, lex.cons h₁, lex.cons h₂ := by exact _match _ _ h₁ h₂ end⟩ instance is_strict_total_order (r : α → α → Prop) [is_strict_total_order' α r] : is_strict_total_order' (list α) (lex r) := {..is_strict_weak_order_of_is_order_connected} instance decidable_rel [decidable_eq α] (r : α → α → Prop) [decidable_rel r] : decidable_rel (lex r) | l₁ [] := is_false $ λ h, by cases h | [] (b::l₂) := is_true lex.nil | (a::l₁) (b::l₂) := begin haveI := decidable_rel l₁ l₂, refine decidable_of_iff (r a b ∨ a = b ∧ lex r l₁ l₂) ⟨λ h, _, λ h, _⟩, { rcases h with h | ⟨rfl, h⟩, { exact lex.rel h }, { exact lex.cons h } }, { rcases h with _|⟨_,_,_,h⟩|⟨_,_,_,_,h⟩, { exact or.inr ⟨rfl, h⟩ }, { exact or.inl h } } end theorem append_right (r : α → α → Prop) : ∀ {s₁ s₂} t, lex r s₁ s₂ → lex r s₁ (s₂ ++ t) | _ _ t nil := nil | _ _ t (cons h) := cons (append_right _ h) | _ _ t (rel r) := rel r theorem append_left (R : α → α → Prop) {t₁ t₂} (h : lex R t₁ t₂) : ∀ s, lex R (s ++ t₁) (s ++ t₂) | [] := h | (a::l) := cons (append_left l) theorem imp {r s : α → α → Prop} (H : ∀ a b, r a b → s a b) : ∀ l₁ l₂, lex r l₁ l₂ → lex s l₁ l₂ | _ _ nil := nil | _ _ (cons h) := cons (imp _ _ h) | _ _ (rel r) := rel (H _ _ r) theorem to_ne : ∀ {l₁ l₂ : list α}, lex (≠) l₁ l₂ → l₁ ≠ l₂ | _ _ (cons h) e := to_ne h (list.cons.inj e).2 | _ _ (rel r) e := r (list.cons.inj e).1 theorem ne_iff {l₁ l₂ : list α} (H : length l₁ ≤ length l₂) : lex (≠) l₁ l₂ ↔ l₁ ≠ l₂ := ⟨to_ne, λ h, begin induction l₁ with a l₁ IH generalizing l₂; cases l₂ with b l₂, { contradiction }, { apply nil }, { exact (not_lt_of_ge H).elim (succ_pos _) }, { cases classical.em (a = b) with ab ab, { subst b, apply cons, exact IH (le_of_succ_le_succ H) (mt (congr_arg _) h) }, { exact rel ab } } end⟩ end lex --Note: this overrides an instance in core lean instance has_lt' [has_lt α] : has_lt (list α) := ⟨lex (<)⟩ theorem nil_lt_cons [has_lt α] (a : α) (l : list α) : [] < a :: l := lex.nil instance [linear_order α] : linear_order (list α) := linear_order_of_STO' (lex (<)) --Note: this overrides an instance in core lean instance has_le' [linear_order α] : has_le (list α) := preorder.to_has_le _ instance [decidable_linear_order α] : decidable_linear_order (list α) := decidable_linear_order_of_STO' (lex (<)) /- all & any -/ @[simp] theorem all_nil (p : α → bool) : all [] p = tt := rfl @[simp] theorem all_cons (p : α → bool) (a : α) (l : list α) : all (a::l) p = (p a && all l p) := rfl theorem all_iff_forall {p : α → bool} {l : list α} : all l p ↔ ∀ a ∈ l, p a := begin induction l with a l ih, { exact iff_of_true rfl (forall_mem_nil _) }, simp only [all_cons, band_coe_iff, ih, forall_mem_cons] end theorem all_iff_forall_prop {p : α → Prop} [decidable_pred p] {l : list α} : all l (λ a, p a) ↔ ∀ a ∈ l, p a := by simp only [all_iff_forall, bool.of_to_bool_iff] @[simp] theorem any_nil (p : α → bool) : any [] p = ff := rfl @[simp] theorem any_cons (p : α → bool) (a : α) (l : list α) : any (a::l) p = (p a || any l p) := rfl theorem any_iff_exists {p : α → bool} {l : list α} : any l p ↔ ∃ a ∈ l, p a := begin induction l with a l ih, { exact iff_of_false bool.not_ff (not_exists_mem_nil _) }, simp only [any_cons, bor_coe_iff, ih, exists_mem_cons_iff] end theorem any_iff_exists_prop {p : α → Prop} [decidable_pred p] {l : list α} : any l (λ a, p a) ↔ ∃ a ∈ l, p a := by simp [any_iff_exists] theorem any_of_mem {p : α → bool} {a : α} {l : list α} (h₁ : a ∈ l) (h₂ : p a) : any l p := any_iff_exists.2 ⟨_, h₁, h₂⟩ @[priority 500] instance decidable_forall_mem {p : α → Prop} [decidable_pred p] (l : list α) : decidable (∀ x ∈ l, p x) := decidable_of_iff _ all_iff_forall_prop instance decidable_exists_mem {p : α → Prop} [decidable_pred p] (l : list α) : decidable (∃ x ∈ l, p x) := decidable_of_iff _ any_iff_exists_prop /- map for partial functions -/ /-- Partial map. If `f : Π a, p a → β` is a partial function defined on `a : α` satisfying `p`, then `pmap f l h` is essentially the same as `map f l` but is defined only when all members of `l` satisfy `p`, using the proof to apply `f`. -/ @[simp] def pmap {p : α → Prop} (f : Π a, p a → β) : Π l : list α, (∀ a ∈ l, p a) → list β | [] H := [] | (a::l) H := f a (forall_mem_cons.1 H).1 :: pmap l (forall_mem_cons.1 H).2 /-- "Attach" the proof that the elements of `l` are in `l` to produce a new list with the same elements but in the type `{x // x ∈ l}`. -/ def attach (l : list α) : list {x // x ∈ l} := pmap subtype.mk l (λ a, id) theorem pmap_eq_map (p : α → Prop) (f : α → β) (l : list α) (H) : @pmap _ _ p (λ a _, f a) l H = map f l := by induction l; [refl, simp only [*, pmap, map]]; split; refl theorem pmap_congr {p q : α → Prop} {f : Π a, p a → β} {g : Π a, q a → β} (l : list α) {H₁ H₂} (h : ∀ a h₁ h₂, f a h₁ = g a h₂) : pmap f l H₁ = pmap g l H₂ := by induction l with _ _ ih; [refl, rw [pmap, pmap, h, ih]] theorem map_pmap {p : α → Prop} (g : β → γ) (f : Π a, p a → β) (l H) : map g (pmap f l H) = pmap (λ a h, g (f a h)) l H := by induction l; [refl, simp only [*, pmap, map]]; split; refl theorem pmap_eq_map_attach {p : α → Prop} (f : Π a, p a → β) (l H) : pmap f l H = l.attach.map (λ x, f x.1 (H _ x.2)) := by rw [attach, map_pmap]; exact pmap_congr l (λ a h₁ h₂, rfl) theorem attach_map_val (l : list α) : l.attach.map subtype.val = l := by rw [attach, map_pmap]; exact (pmap_eq_map _ _ _ _).trans (map_id l) @[simp] theorem mem_attach (l : list α) : ∀ x, x ∈ l.attach | ⟨a, h⟩ := by have := mem_map.1 (by rw [attach_map_val]; exact h); { rcases this with ⟨⟨_, _⟩, m, rfl⟩, exact m } @[simp] theorem mem_pmap {p : α → Prop} {f : Π a, p a → β} {l H b} : b ∈ pmap f l H ↔ ∃ a (h : a ∈ l), f a (H a h) = b := by simp only [pmap_eq_map_attach, mem_map, mem_attach, true_and, subtype.exists] @[simp] theorem length_pmap {p : α → Prop} {f : Π a, p a → β} {l H} : length (pmap f l H) = length l := by induction l; [refl, simp only [*, pmap, length]] @[simp] lemma length_attach {α} (L : list α) : L.attach.length = L.length := length_pmap /- find -/ section find variables {p : α → Prop} [decidable_pred p] {l : list α} {a : α} @[simp] theorem find_nil (p : α → Prop) [decidable_pred p] : find p [] = none := rfl @[simp] theorem find_cons_of_pos (l) (h : p a) : find p (a::l) = some a := if_pos h @[simp] theorem find_cons_of_neg (l) (h : ¬ p a) : find p (a::l) = find p l := if_neg h @[simp] theorem find_eq_none : find p l = none ↔ ∀ x ∈ l, ¬ p x := begin induction l with a l IH, { exact iff_of_true rfl (forall_mem_nil _) }, rw forall_mem_cons, by_cases h : p a, { simp only [find_cons_of_pos _ h, h, not_true, false_and] }, { rwa [find_cons_of_neg _ h, iff_true_intro h, true_and] } end @[simp] theorem find_some (H : find p l = some a) : p a := begin induction l with b l IH, {contradiction}, by_cases h : p b, { rw find_cons_of_pos _ h at H, cases H, exact h }, { rw find_cons_of_neg _ h at H, exact IH H } end @[simp] theorem find_mem (H : find p l = some a) : a ∈ l := begin induction l with b l IH, {contradiction}, by_cases h : p b, { rw find_cons_of_pos _ h at H, cases H, apply mem_cons_self }, { rw find_cons_of_neg _ h at H, exact mem_cons_of_mem _ (IH H) } end end find /- lookmap -/ section lookmap variables (f : α → option α) @[simp] theorem lookmap_nil : [].lookmap f = [] := rfl @[simp] theorem lookmap_cons_none {a : α} (l : list α) (h : f a = none) : (a :: l).lookmap f = a :: l.lookmap f := by simp [lookmap, h] @[simp] theorem lookmap_cons_some {a b : α} (l : list α) (h : f a = some b) : (a :: l).lookmap f = b :: l := by simp [lookmap, h] theorem lookmap_some : ∀ l : list α, l.lookmap some = l | [] := rfl | (a::l) := rfl theorem lookmap_none : ∀ l : list α, l.lookmap (λ _, none) = l | [] := rfl | (a::l) := congr_arg (cons a) (lookmap_none l) theorem lookmap_congr {f g : α → option α} : ∀ {l : list α}, (∀ a ∈ l, f a = g a) → l.lookmap f = l.lookmap g | [] H := rfl | (a::l) H := begin cases forall_mem_cons.1 H with H₁ H₂, cases h : g a with b, { simp [h, H₁.trans h, lookmap_congr H₂] }, { simp [lookmap_cons_some _ _ h, lookmap_cons_some _ _ (H₁.trans h)] } end theorem lookmap_of_forall_not {l : list α} (H : ∀ a ∈ l, f a = none) : l.lookmap f = l := (lookmap_congr H).trans (lookmap_none l) theorem lookmap_map_eq (g : α → β) (h : ∀ a (b ∈ f a), g a = g b) : ∀ l : list α, map g (l.lookmap f) = map g l | [] := rfl | (a::l) := begin cases h' : f a with b, { simp [h', lookmap_map_eq] }, { simp [lookmap_cons_some _ _ h', h _ _ h'] } end theorem lookmap_id' (h : ∀ a (b ∈ f a), a = b) (l : list α) : l.lookmap f = l := by rw [← map_id (l.lookmap f), lookmap_map_eq, map_id]; exact h theorem length_lookmap (l : list α) : length (l.lookmap f) = length l := by rw [← length_map, lookmap_map_eq _ (λ _, ()), length_map]; simp end lookmap /- filter_map -/ @[simp] theorem filter_map_nil (f : α → option β) : filter_map f [] = [] := rfl @[simp] theorem filter_map_cons_none {f : α → option β} (a : α) (l : list α) (h : f a = none) : filter_map f (a :: l) = filter_map f l := by simp only [filter_map, h] @[simp] theorem filter_map_cons_some (f : α → option β) (a : α) (l : list α) {b : β} (h : f a = some b) : filter_map f (a :: l) = b :: filter_map f l := by simp only [filter_map, h]; split; refl theorem filter_map_eq_map (f : α → β) : filter_map (some ∘ f) = map f := begin funext l, induction l with a l IH, {refl}, simp only [filter_map_cons_some (some ∘ f) _ _ rfl, IH, map_cons], split; refl end theorem filter_map_eq_filter (p : α → Prop) [decidable_pred p] : filter_map (option.guard p) = filter p := begin funext l, induction l with a l IH, {refl}, by_cases pa : p a, { simp only [filter_map, option.guard, IH, if_pos pa, filter_cons_of_pos _ pa], split; refl }, { simp only [filter_map, option.guard, IH, if_neg pa, filter_cons_of_neg _ pa] } end theorem filter_map_filter_map (f : α → option β) (g : β → option γ) (l : list α) : filter_map g (filter_map f l) = filter_map (λ x, (f x).bind g) l := begin induction l with a l IH, {refl}, cases h : f a with b, { rw [filter_map_cons_none _ _ h, filter_map_cons_none, IH], simp only [h, option.none_bind'] }, rw filter_map_cons_some _ _ _ h, cases h' : g b with c; [ rw [filter_map_cons_none _ _ h', filter_map_cons_none, IH], rw [filter_map_cons_some _ _ _ h', filter_map_cons_some, IH] ]; simp only [h, h', option.some_bind'] end theorem map_filter_map (f : α → option β) (g : β → γ) (l : list α) : map g (filter_map f l) = filter_map (λ x, (f x).map g) l := by rw [← filter_map_eq_map, filter_map_filter_map]; refl theorem filter_map_map (f : α → β) (g : β → option γ) (l : list α) : filter_map g (map f l) = filter_map (g ∘ f) l := by rw [← filter_map_eq_map, filter_map_filter_map]; refl theorem filter_filter_map (f : α → option β) (p : β → Prop) [decidable_pred p] (l : list α) : filter p (filter_map f l) = filter_map (λ x, (f x).filter p) l := by rw [← filter_map_eq_filter, filter_map_filter_map]; refl theorem filter_map_filter (p : α → Prop) [decidable_pred p] (f : α → option β) (l : list α) : filter_map f (filter p l) = filter_map (λ x, if p x then f x else none) l := begin rw [← filter_map_eq_filter, filter_map_filter_map], congr, funext x, show (option.guard p x).bind f = ite (p x) (f x) none, by_cases h : p x, { simp only [option.guard, if_pos h, option.some_bind'] }, { simp only [option.guard, if_neg h, option.none_bind'] } end @[simp] theorem filter_map_some (l : list α) : filter_map some l = l := by rw filter_map_eq_map; apply map_id @[simp] theorem mem_filter_map (f : α → option β) (l : list α) {b : β} : b ∈ filter_map f l ↔ ∃ a, a ∈ l ∧ f a = some b := begin induction l with a l IH, { split, { intro H, cases H }, { rintro ⟨_, H, _⟩, cases H } }, cases h : f a with b', { have : f a ≠ some b, {rw h, intro, contradiction}, simp only [filter_map_cons_none _ _ h, IH, mem_cons_iff, or_and_distrib_right, exists_or_distrib, exists_eq_left, this, false_or] }, { have : f a = some b ↔ b = b', { split; intro t, {rw t at h; injection h}, {exact t.symm ▸ h} }, simp only [filter_map_cons_some _ _ _ h, IH, mem_cons_iff, or_and_distrib_right, exists_or_distrib, this, exists_eq_left] } end theorem map_filter_map_of_inv (f : α → option β) (g : β → α) (H : ∀ x : α, (f x).map g = some x) (l : list α) : map g (filter_map f l) = l := by simp only [map_filter_map, H, filter_map_some] theorem filter_map_sublist_filter_map (f : α → option β) {l₁ l₂ : list α} (s : l₁ <+ l₂) : filter_map f l₁ <+ filter_map f l₂ := by induction s with l₁ l₂ a s IH l₁ l₂ a s IH; simp only [filter_map]; cases f a with b; simp only [filter_map, IH, sublist.cons, sublist.cons2] theorem map_sublist_map (f : α → β) {l₁ l₂ : list α} (s : l₁ <+ l₂) : map f l₁ <+ map f l₂ := by rw ← filter_map_eq_map; exact filter_map_sublist_filter_map _ s /- filter -/ section filter variables {p : α → Prop} [decidable_pred p] lemma filter_congr {p q : α → Prop} [decidable_pred p] [decidable_pred q] : ∀ {l : list α}, (∀ x ∈ l, p x ↔ q x) → filter p l = filter q l | [] _ := rfl | (a::l) h := by rw forall_mem_cons at h; by_cases pa : p a; [simp only [filter_cons_of_pos _ pa, filter_cons_of_pos _ (h.1.1 pa), filter_congr h.2], simp only [filter_cons_of_neg _ pa, filter_cons_of_neg _ (mt h.1.2 pa), filter_congr h.2]]; split; refl @[simp] theorem filter_subset (l : list α) : filter p l ⊆ l := subset_of_sublist $ filter_sublist l theorem of_mem_filter {a : α} : ∀ {l}, a ∈ filter p l → p a | (b::l) ain := if pb : p b then have a ∈ b :: filter p l, by simpa only [filter_cons_of_pos _ pb] using ain, or.elim (eq_or_mem_of_mem_cons this) (assume : a = b, begin rw [← this] at pb, exact pb end) (assume : a ∈ filter p l, of_mem_filter this) else begin simp only [filter_cons_of_neg _ pb] at ain, exact (of_mem_filter ain) end theorem mem_of_mem_filter {a : α} {l} (h : a ∈ filter p l) : a ∈ l := filter_subset l h theorem mem_filter_of_mem {a : α} : ∀ {l}, a ∈ l → p a → a ∈ filter p l | (_::l) (or.inl rfl) pa := by rw filter_cons_of_pos _ pa; apply mem_cons_self | (b::l) (or.inr ain) pa := if pb : p b then by rw [filter_cons_of_pos _ pb]; apply mem_cons_of_mem; apply mem_filter_of_mem ain pa else by rw [filter_cons_of_neg _ pb]; apply mem_filter_of_mem ain pa @[simp] theorem mem_filter {a : α} {l} : a ∈ filter p l ↔ a ∈ l ∧ p a := ⟨λ h, ⟨mem_of_mem_filter h, of_mem_filter h⟩, λ ⟨h₁, h₂⟩, mem_filter_of_mem h₁ h₂⟩ theorem filter_eq_self {l} : filter p l = l ↔ ∀ a ∈ l, p a := begin induction l with a l ih, { exact iff_of_true rfl (forall_mem_nil _) }, rw forall_mem_cons, by_cases p a, { rw [filter_cons_of_pos _ h, cons_inj', ih, and_iff_right h] }, { rw [filter_cons_of_neg _ h], refine iff_of_false _ (mt and.left h), intro e, have := filter_sublist l, rw e at this, exact not_lt_of_ge (length_le_of_sublist this) (lt_succ_self _) } end theorem filter_eq_nil {l} : filter p l = [] ↔ ∀ a ∈ l, ¬p a := by simp only [eq_nil_iff_forall_not_mem, mem_filter, not_and] theorem filter_sublist_filter {l₁ l₂} (s : l₁ <+ l₂) : filter p l₁ <+ filter p l₂ := by rw ← filter_map_eq_filter; exact filter_map_sublist_filter_map _ s theorem filter_of_map (f : β → α) (l) : filter p (map f l) = map f (filter (p ∘ f) l) := by rw [← filter_map_eq_map, filter_filter_map, filter_map_filter]; refl @[simp] theorem filter_filter {q} [decidable_pred q] : ∀ l, filter p (filter q l) = filter (λ a, p a ∧ q a) l | [] := rfl | (a :: l) := by by_cases hp : p a; by_cases hq : q a; simp only [hp, hq, filter, if_true, if_false, true_and, false_and, filter_filter l, eq_self_iff_true] @[simp] theorem span_eq_take_drop (p : α → Prop) [decidable_pred p] : ∀ (l : list α), span p l = (take_while p l, drop_while p l) | [] := rfl | (a::l) := if pa : p a then by simp only [span, if_pos pa, span_eq_take_drop l, take_while, drop_while] else by simp only [span, take_while, drop_while, if_neg pa] @[simp] theorem take_while_append_drop (p : α → Prop) [decidable_pred p] : ∀ (l : list α), take_while p l ++ drop_while p l = l | [] := rfl | (a::l) := if pa : p a then by rw [take_while, drop_while, if_pos pa, if_pos pa, cons_append, take_while_append_drop l] else by rw [take_while, drop_while, if_neg pa, if_neg pa, nil_append] @[simp] theorem countp_nil (p : α → Prop) [decidable_pred p] : countp p [] = 0 := rfl @[simp] theorem countp_cons_of_pos {a : α} (l) (pa : p a) : countp p (a::l) = countp p l + 1 := if_pos pa @[simp] theorem countp_cons_of_neg {a : α} (l) (pa : ¬ p a) : countp p (a::l) = countp p l := if_neg pa theorem countp_eq_length_filter (l) : countp p l = length (filter p l) := by induction l with x l ih; [refl, by_cases (p x)]; [simp only [filter_cons_of_pos _ h, countp, ih, if_pos h], simp only [countp_cons_of_neg _ h, ih, filter_cons_of_neg _ h]]; refl local attribute [simp] countp_eq_length_filter @[simp] theorem countp_append (l₁ l₂) : countp p (l₁ ++ l₂) = countp p l₁ + countp p l₂ := by simp only [countp_eq_length_filter, filter_append, length_append] theorem countp_pos {l} : 0 < countp p l ↔ ∃ a ∈ l, p a := by simp only [countp_eq_length_filter, length_pos_iff_exists_mem, mem_filter, exists_prop] theorem countp_le_of_sublist {l₁ l₂} (s : l₁ <+ l₂) : countp p l₁ ≤ countp p l₂ := by simpa only [countp_eq_length_filter] using length_le_of_sublist (filter_sublist_filter s) @[simp] theorem countp_filter {q} [decidable_pred q] (l : list α) : countp p (filter q l) = countp (λ a, p a ∧ q a) l := by simp only [countp_eq_length_filter, filter_filter] end filter /- count -/ section count variable [decidable_eq α] @[simp] theorem count_nil (a : α) : count a [] = 0 := rfl theorem count_cons (a b : α) (l : list α) : count a (b :: l) = if a = b then succ (count a l) else count a l := rfl theorem count_cons' (a b : α) (l : list α) : count a (b :: l) = count a l + (if a = b then 1 else 0) := begin rw count_cons, split_ifs; refl end @[simp] theorem count_cons_self (a : α) (l : list α) : count a (a::l) = succ (count a l) := if_pos rfl @[simp] theorem count_cons_of_ne {a b : α} (h : a ≠ b) (l : list α) : count a (b::l) = count a l := if_neg h theorem count_le_of_sublist (a : α) {l₁ l₂} : l₁ <+ l₂ → count a l₁ ≤ count a l₂ := countp_le_of_sublist theorem count_le_count_cons (a b : α) (l : list α) : count a l ≤ count a (b :: l) := count_le_of_sublist _ (sublist_cons _ _) theorem count_singleton (a : α) : count a [a] = 1 := if_pos rfl @[simp] theorem count_append (a : α) : ∀ l₁ l₂, count a (l₁ ++ l₂) = count a l₁ + count a l₂ := countp_append @[simp] theorem count_concat (a : α) (l : list α) : count a (concat l a) = succ (count a l) := by rw [concat_eq_append, count_append, count_singleton] theorem count_pos {a : α} {l : list α} : 0 < count a l ↔ a ∈ l := by simp only [count, countp_pos, exists_prop, exists_eq_right'] @[simp] theorem count_eq_zero_of_not_mem {a : α} {l : list α} (h : a ∉ l) : count a l = 0 := by_contradiction $ λ h', h $ count_pos.1 (nat.pos_of_ne_zero h') theorem not_mem_of_count_eq_zero {a : α} {l : list α} (h : count a l = 0) : a ∉ l := λ h', ne_of_gt (count_pos.2 h') h @[simp] theorem count_repeat (a : α) (n : ℕ) : count a (repeat a n) = n := by rw [count, countp_eq_length_filter, filter_eq_self.2, length_repeat]; exact λ b m, (eq_of_mem_repeat m).symm theorem le_count_iff_repeat_sublist {a : α} {l : list α} {n : ℕ} : n ≤ count a l ↔ repeat a n <+ l := ⟨λ h, ((repeat_sublist_repeat a).2 h).trans $ have filter (eq a) l = repeat a (count a l), from eq_repeat.2 ⟨by simp only [count, countp_eq_length_filter], λ b m, (of_mem_filter m).symm⟩, by rw ← this; apply filter_sublist, λ h, by simpa only [count_repeat] using count_le_of_sublist a h⟩ @[simp] theorem count_filter {p} [decidable_pred p] {a} {l : list α} (h : p a) : count a (filter p l) = count a l := by simp only [count, countp_filter]; congr; exact set.ext (λ b, and_iff_left_of_imp (λ e, e ▸ h)) end count /- prefix, suffix, infix -/ @[simp] theorem prefix_append (l₁ l₂ : list α) : l₁ <+: l₁ ++ l₂ := ⟨l₂, rfl⟩ @[simp] theorem suffix_append (l₁ l₂ : list α) : l₂ <:+ l₁ ++ l₂ := ⟨l₁, rfl⟩ @[simp] theorem infix_append (l₁ l₂ l₃ : list α) : l₂ <:+: l₁ ++ l₂ ++ l₃ := ⟨l₁, l₃, rfl⟩ theorem nil_prefix (l : list α) : [] <+: l := ⟨l, rfl⟩ theorem nil_suffix (l : list α) : [] <:+ l := ⟨l, append_nil _⟩ @[refl] theorem prefix_refl (l : list α) : l <+: l := ⟨[], append_nil _⟩ @[refl] theorem suffix_refl (l : list α) : l <:+ l := ⟨[], rfl⟩ @[simp] theorem suffix_cons (a : α) : ∀ l, l <:+ a :: l := suffix_append [a] @[simp] theorem prefix_concat (a : α) (l) : l <+: concat l a := by simp only [concat_eq_append, prefix_append] theorem infix_of_prefix {l₁ l₂ : list α} : l₁ <+: l₂ → l₁ <:+: l₂ := λ⟨t, h⟩, ⟨[], t, h⟩ theorem infix_of_suffix {l₁ l₂ : list α} : l₁ <:+ l₂ → l₁ <:+: l₂ := λ⟨t, h⟩, ⟨t, [], by simp only [h, append_nil]⟩ @[refl] theorem infix_refl (l : list α) : l <:+: l := infix_of_prefix $ prefix_refl l theorem nil_infix (l : list α) : [] <:+: l := infix_of_prefix $ nil_prefix l theorem infix_cons {L₁ L₂ : list α} {x : α} : L₁ <:+: L₂ → L₁ <:+: x :: L₂ := λ⟨LP, LS, H⟩, ⟨x :: LP, LS, H ▸ rfl⟩ @[trans] theorem is_prefix.trans : ∀ {l₁ l₂ l₃ : list α}, l₁ <+: l₂ → l₂ <+: l₃ → l₁ <+: l₃ | l ._ ._ ⟨r₁, rfl⟩ ⟨r₂, rfl⟩ := ⟨r₁ ++ r₂, (append_assoc _ _ _).symm⟩ @[trans] theorem is_suffix.trans : ∀ {l₁ l₂ l₃ : list α}, l₁ <:+ l₂ → l₂ <:+ l₃ → l₁ <:+ l₃ | l ._ ._ ⟨l₁, rfl⟩ ⟨l₂, rfl⟩ := ⟨l₂ ++ l₁, append_assoc _ _ _⟩ @[trans] theorem is_infix.trans : ∀ {l₁ l₂ l₃ : list α}, l₁ <:+: l₂ → l₂ <:+: l₃ → l₁ <:+: l₃ | l ._ ._ ⟨l₁, r₁, rfl⟩ ⟨l₂, r₂, rfl⟩ := ⟨l₂ ++ l₁, r₁ ++ r₂, by simp only [append_assoc]⟩ theorem sublist_of_infix {l₁ l₂ : list α} : l₁ <:+: l₂ → l₁ <+ l₂ := λ⟨s, t, h⟩, by rw [← h]; exact (sublist_append_right _ _).trans (sublist_append_left _ _) theorem sublist_of_prefix {l₁ l₂ : list α} : l₁ <+: l₂ → l₁ <+ l₂ := sublist_of_infix ∘ infix_of_prefix theorem sublist_of_suffix {l₁ l₂ : list α} : l₁ <:+ l₂ → l₁ <+ l₂ := sublist_of_infix ∘ infix_of_suffix theorem reverse_suffix {l₁ l₂ : list α} : reverse l₁ <:+ reverse l₂ ↔ l₁ <+: l₂ := ⟨λ ⟨r, e⟩, ⟨reverse r, by rw [← reverse_reverse l₁, ← reverse_append, e, reverse_reverse]⟩, λ ⟨r, e⟩, ⟨reverse r, by rw [← reverse_append, e]⟩⟩ theorem reverse_prefix {l₁ l₂ : list α} : reverse l₁ <+: reverse l₂ ↔ l₁ <:+ l₂ := by rw ← reverse_suffix; simp only [reverse_reverse] theorem length_le_of_infix {l₁ l₂ : list α} (s : l₁ <:+: l₂) : length l₁ ≤ length l₂ := length_le_of_sublist $ sublist_of_infix s theorem eq_nil_of_infix_nil {l : list α} (s : l <:+: []) : l = [] := eq_nil_of_sublist_nil $ sublist_of_infix s theorem eq_nil_of_prefix_nil {l : list α} (s : l <+: []) : l = [] := eq_nil_of_infix_nil $ infix_of_prefix s theorem eq_nil_of_suffix_nil {l : list α} (s : l <:+ []) : l = [] := eq_nil_of_infix_nil $ infix_of_suffix s theorem infix_iff_prefix_suffix (l₁ l₂ : list α) : l₁ <:+: l₂ ↔ ∃ t, l₁ <+: t ∧ t <:+ l₂ := ⟨λ⟨s, t, e⟩, ⟨l₁ ++ t, ⟨_, rfl⟩, by rw [← e, append_assoc]; exact ⟨_, rfl⟩⟩, λ⟨._, ⟨t, rfl⟩, ⟨s, e⟩⟩, ⟨s, t, by rw append_assoc; exact e⟩⟩ theorem eq_of_infix_of_length_eq {l₁ l₂ : list α} (s : l₁ <:+: l₂) : length l₁ = length l₂ → l₁ = l₂ := eq_of_sublist_of_length_eq $ sublist_of_infix s theorem eq_of_prefix_of_length_eq {l₁ l₂ : list α} (s : l₁ <+: l₂) : length l₁ = length l₂ → l₁ = l₂ := eq_of_sublist_of_length_eq $ sublist_of_prefix s theorem eq_of_suffix_of_length_eq {l₁ l₂ : list α} (s : l₁ <:+ l₂) : length l₁ = length l₂ → l₁ = l₂ := eq_of_sublist_of_length_eq $ sublist_of_suffix s theorem prefix_of_prefix_length_le : ∀ {l₁ l₂ l₃ : list α}, l₁ <+: l₃ → l₂ <+: l₃ → length l₁ ≤ length l₂ → l₁ <+: l₂ | [] l₂ l₃ h₁ h₂ _ := nil_prefix _ | (a::l₁) (b::l₂) _ ⟨r₁, rfl⟩ ⟨r₂, e⟩ ll := begin injection e with _ e', subst b, rcases prefix_of_prefix_length_le ⟨_, rfl⟩ ⟨_, e'⟩ (le_of_succ_le_succ ll) with ⟨r₃, rfl⟩, exact ⟨r₃, rfl⟩ end theorem prefix_or_prefix_of_prefix {l₁ l₂ l₃ : list α} (h₁ : l₁ <+: l₃) (h₂ : l₂ <+: l₃) : l₁ <+: l₂ ∨ l₂ <+: l₁ := (le_total (length l₁) (length l₂)).imp (prefix_of_prefix_length_le h₁ h₂) (prefix_of_prefix_length_le h₂ h₁) theorem suffix_of_suffix_length_le {l₁ l₂ l₃ : list α} (h₁ : l₁ <:+ l₃) (h₂ : l₂ <:+ l₃) (ll : length l₁ ≤ length l₂) : l₁ <:+ l₂ := reverse_prefix.1 $ prefix_of_prefix_length_le (reverse_prefix.2 h₁) (reverse_prefix.2 h₂) (by simp [ll]) theorem suffix_or_suffix_of_suffix {l₁ l₂ l₃ : list α} (h₁ : l₁ <:+ l₃) (h₂ : l₂ <:+ l₃) : l₁ <:+ l₂ ∨ l₂ <:+ l₁ := (prefix_or_prefix_of_prefix (reverse_prefix.2 h₁) (reverse_prefix.2 h₂)).imp reverse_prefix.1 reverse_prefix.1 theorem infix_of_mem_join : ∀ {L : list (list α)} {l}, l ∈ L → l <:+: join L | (_ :: L) l (or.inl rfl) := infix_append [] _ _ | (l' :: L) l (or.inr h) := is_infix.trans (infix_of_mem_join h) $ infix_of_suffix $ suffix_append _ _ theorem prefix_append_left_inj {l₁ l₂ : list α} (l) : l ++ l₁ <+: l ++ l₂ ↔ l₁ <+: l₂ := exists_congr $ λ r, by rw [append_assoc, append_left_inj] theorem prefix_cons_inj {l₁ l₂ : list α} (a) : a :: l₁ <+: a :: l₂ ↔ l₁ <+: l₂ := prefix_append_left_inj [a] theorem take_prefix (n) (l : list α) : take n l <+: l := ⟨_, take_append_drop _ _⟩ theorem drop_suffix (n) (l : list α) : drop n l <:+ l := ⟨_, take_append_drop _ _⟩ theorem prefix_iff_eq_append {l₁ l₂ : list α} : l₁ <+: l₂ ↔ l₁ ++ drop (length l₁) l₂ = l₂ := ⟨by rintros ⟨r, rfl⟩; rw drop_left, λ e, ⟨_, e⟩⟩ theorem suffix_iff_eq_append {l₁ l₂ : list α} : l₁ <:+ l₂ ↔ take (length l₂ - length l₁) l₂ ++ l₁ = l₂ := ⟨by rintros ⟨r, rfl⟩; simp only [length_append, nat.add_sub_cancel, take_left], λ e, ⟨_, e⟩⟩ theorem prefix_iff_eq_take {l₁ l₂ : list α} : l₁ <+: l₂ ↔ l₁ = take (length l₁) l₂ := ⟨λ h, append_right_cancel $ (prefix_iff_eq_append.1 h).trans (take_append_drop _ _).symm, λ e, e.symm ▸ take_prefix _ _⟩ theorem suffix_iff_eq_drop {l₁ l₂ : list α} : l₁ <:+ l₂ ↔ l₁ = drop (length l₂ - length l₁) l₂ := ⟨λ h, append_left_cancel $ (suffix_iff_eq_append.1 h).trans (take_append_drop _ _).symm, λ e, e.symm ▸ drop_suffix _ _⟩ instance decidable_prefix [decidable_eq α] : ∀ (l₁ l₂ : list α), decidable (l₁ <+: l₂) | [] l₂ := is_true ⟨l₂, rfl⟩ | (a::l₁) [] := is_false $ λ ⟨t, te⟩, list.no_confusion te | (a::l₁) (b::l₂) := if h : a = b then @decidable_of_iff _ _ (by rw [← h, prefix_cons_inj]) (decidable_prefix l₁ l₂) else is_false $ λ ⟨t, te⟩, h $ by injection te -- Alternatively, use mem_tails instance decidable_suffix [decidable_eq α] : ∀ (l₁ l₂ : list α), decidable (l₁ <:+ l₂) | [] l₂ := is_true ⟨l₂, append_nil _⟩ | (a::l₁) [] := is_false $ mt (length_le_of_sublist ∘ sublist_of_suffix) dec_trivial | l₁ l₂ := let len1 := length l₁, len2 := length l₂ in if hl : len1 ≤ len2 then decidable_of_iff' (l₁ = drop (len2-len1) l₂) suffix_iff_eq_drop else is_false $ λ h, hl $ length_le_of_sublist $ sublist_of_suffix h @[simp] theorem mem_inits : ∀ (s t : list α), s ∈ inits t ↔ s <+: t | s [] := suffices s = nil ↔ s <+: nil, by simpa only [inits, mem_singleton], ⟨λh, h.symm ▸ prefix_refl [], eq_nil_of_prefix_nil⟩ | s (a::t) := suffices (s = nil ∨ ∃ l ∈ inits t, a :: l = s) ↔ s <+: a :: t, by simpa, ⟨λo, match s, o with | ._, or.inl rfl := ⟨_, rfl⟩ | s, or.inr ⟨r, hr, hs⟩ := let ⟨s, ht⟩ := (mem_inits _ _).1 hr in by rw [← hs, ← ht]; exact ⟨s, rfl⟩ end, λmi, match s, mi with | [], ⟨._, rfl⟩ := or.inl rfl | (b::s), ⟨r, hr⟩ := list.no_confusion hr $ λba (st : s++r = t), or.inr $ by rw ba; exact ⟨_, (mem_inits _ _).2 ⟨_, st⟩, rfl⟩ end⟩ @[simp] theorem mem_tails : ∀ (s t : list α), s ∈ tails t ↔ s <:+ t | s [] := by simp only [tails, mem_singleton]; exact ⟨λh, by rw h; exact suffix_refl [], eq_nil_of_suffix_nil⟩ | s (a::t) := by simp only [tails, mem_cons_iff, mem_tails s t]; exact show s = a :: t ∨ s <:+ t ↔ s <:+ a :: t, from ⟨λo, match s, t, o with | ._, t, or.inl rfl := suffix_refl _ | s, ._, or.inr ⟨l, rfl⟩ := ⟨a::l, rfl⟩ end, λe, match s, t, e with | ._, t, ⟨[], rfl⟩ := or.inl rfl | s, t, ⟨b::l, he⟩ := list.no_confusion he (λab lt, or.inr ⟨l, lt⟩) end⟩ instance decidable_infix [decidable_eq α] : ∀ (l₁ l₂ : list α), decidable (l₁ <:+: l₂) | [] l₂ := is_true ⟨[], l₂, rfl⟩ | (a::l₁) [] := is_false $ λ⟨s, t, te⟩, absurd te $ append_ne_nil_of_ne_nil_left _ _ $ append_ne_nil_of_ne_nil_right _ _ $ λh, list.no_confusion h | l₁ l₂ := decidable_of_decidable_of_iff (list.decidable_bex (λt, l₁ <+: t) (tails l₂)) $ by refine (exists_congr (λt, _)).trans (infix_iff_prefix_suffix _ _).symm; exact ⟨λ⟨h1, h2⟩, ⟨h2, (mem_tails _ _).1 h1⟩, λ⟨h2, h1⟩, ⟨(mem_tails _ _).2 h1, h2⟩⟩ /- sublists -/ @[simp] theorem sublists'_nil : sublists' (@nil α) = [[]] := rfl @[simp] theorem sublists'_singleton (a : α) : sublists' [a] = [[], [a]] := rfl theorem map_sublists'_aux (g : list β → list γ) (l : list α) (f r) : map g (sublists'_aux l f r) = sublists'_aux l (g ∘ f) (map g r) := by induction l generalizing f r; [refl, simp only [*, sublists'_aux]] theorem sublists'_aux_append (r' : list (list β)) (l : list α) (f r) : sublists'_aux l f (r ++ r') = sublists'_aux l f r ++ r' := by induction l generalizing f r; [refl, simp only [*, sublists'_aux]] theorem sublists'_aux_eq_sublists' (l f r) : @sublists'_aux α β l f r = map f (sublists' l) ++ r := by rw [sublists', map_sublists'_aux, ← sublists'_aux_append]; refl @[simp] theorem sublists'_cons (a : α) (l : list α) : sublists' (a :: l) = sublists' l ++ map (cons a) (sublists' l) := by rw [sublists', sublists'_aux]; simp only [sublists'_aux_eq_sublists', map_id, append_nil]; refl @[simp] theorem mem_sublists' {s t : list α} : s ∈ sublists' t ↔ s <+ t := begin induction t with a t IH generalizing s, { simp only [sublists'_nil, mem_singleton], exact ⟨λ h, by rw h, eq_nil_of_sublist_nil⟩ }, simp only [sublists'_cons, mem_append, IH, mem_map], split; intro h, rcases h with h | ⟨s, h, rfl⟩, { exact sublist_cons_of_sublist _ h }, { exact cons_sublist_cons _ h }, { cases h with _ _ _ h s _ _ h, { exact or.inl h }, { exact or.inr ⟨s, h, rfl⟩ } } end @[simp] theorem length_sublists' : ∀ l : list α, length (sublists' l) = 2 ^ length l | [] := rfl | (a::l) := by simp only [sublists'_cons, length_append, length_sublists' l, length_map, length, pow_succ, mul_succ, mul_zero, zero_add] @[simp] theorem sublists_nil : sublists (@nil α) = [[]] := rfl @[simp] theorem sublists_singleton (a : α) : sublists [a] = [[], [a]] := rfl theorem sublists_aux₁_eq_sublists_aux : ∀ l (f : list α → list β), sublists_aux₁ l f = sublists_aux l (λ ys r, f ys ++ r) | [] f := rfl | (a::l) f := by rw [sublists_aux₁, sublists_aux]; simp only [*, append_assoc] theorem sublists_aux_cons_eq_sublists_aux₁ (l : list α) : sublists_aux l cons = sublists_aux₁ l (λ x, [x]) := by rw [sublists_aux₁_eq_sublists_aux]; refl theorem sublists_aux_eq_foldr.aux {a : α} {l : list α} (IH₁ : ∀ (f : list α → list β → list β), sublists_aux l f = foldr f [] (sublists_aux l cons)) (IH₂ : ∀ (f : list α → list (list α) → list (list α)), sublists_aux l f = foldr f [] (sublists_aux l cons)) (f : list α → list β → list β) : sublists_aux (a::l) f = foldr f [] (sublists_aux (a::l) cons) := begin simp only [sublists_aux, foldr_cons], rw [IH₂, IH₁], congr' 1, induction sublists_aux l cons with _ _ ih, {refl}, simp only [ih, foldr_cons] end theorem sublists_aux_eq_foldr (l : list α) : ∀ (f : list α → list β → list β), sublists_aux l f = foldr f [] (sublists_aux l cons) := suffices _ ∧ ∀ f : list α → list (list α) → list (list α), sublists_aux l f = foldr f [] (sublists_aux l cons), from this.1, begin induction l with a l IH, {split; intro; refl}, exact ⟨sublists_aux_eq_foldr.aux IH.1 IH.2, sublists_aux_eq_foldr.aux IH.2 IH.2⟩ end theorem sublists_aux_cons_cons (l : list α) (a : α) : sublists_aux (a::l) cons = [a] :: foldr (λys r, ys :: (a :: ys) :: r) [] (sublists_aux l cons) := by rw [← sublists_aux_eq_foldr]; refl theorem sublists_aux₁_append : ∀ (l₁ l₂ : list α) (f : list α → list β), sublists_aux₁ (l₁ ++ l₂) f = sublists_aux₁ l₁ f ++ sublists_aux₁ l₂ (λ x, f x ++ sublists_aux₁ l₁ (f ∘ (++ x))) | [] l₂ f := by simp only [sublists_aux₁, nil_append, append_nil] | (a::l₁) l₂ f := by simp only [sublists_aux₁, cons_append, sublists_aux₁_append l₁, append_assoc]; refl theorem sublists_aux₁_concat (l : list α) (a : α) (f : list α → list β) : sublists_aux₁ (l ++ [a]) f = sublists_aux₁ l f ++ f [a] ++ sublists_aux₁ l (λ x, f (x ++ [a])) := by simp only [sublists_aux₁_append, sublists_aux₁, append_assoc, append_nil] theorem sublists_aux₁_bind : ∀ (l : list α) (f : list α → list β) (g : β → list γ), (sublists_aux₁ l f).bind g = sublists_aux₁ l (λ x, (f x).bind g) | [] f g := rfl | (a::l) f g := by simp only [sublists_aux₁, bind_append, sublists_aux₁_bind l] theorem sublists_aux_cons_append (l₁ l₂ : list α) : sublists_aux (l₁ ++ l₂) cons = sublists_aux l₁ cons ++ (do x ← sublists_aux l₂ cons, (++ x) <$> sublists l₁) := begin simp only [sublists, sublists_aux_cons_eq_sublists_aux₁, sublists_aux₁_append, bind_eq_bind, sublists_aux₁_bind], congr, funext x, apply congr_arg _, rw [← bind_ret_eq_map, sublists_aux₁_bind], exact (append_nil _).symm end theorem sublists_append (l₁ l₂ : list α) : sublists (l₁ ++ l₂) = (do x ← sublists l₂, (++ x) <$> sublists l₁) := by simp only [map, sublists, sublists_aux_cons_append, map_eq_map, bind_eq_bind, cons_bind, map_id', append_nil, cons_append, map_id' (λ _, rfl)]; split; refl @[simp] theorem sublists_concat (l : list α) (a : α) : sublists (l ++ [a]) = sublists l ++ map (λ x, x ++ [a]) (sublists l) := by rw [sublists_append, sublists_singleton, bind_eq_bind, cons_bind, cons_bind, nil_bind, map_eq_map, map_eq_map, map_id' (append_nil), append_nil] theorem sublists_reverse (l : list α) : sublists (reverse l) = map reverse (sublists' l) := by induction l with hd tl ih; [refl, simp only [reverse_cons, sublists_append, sublists'_cons, map_append, ih, sublists_singleton, map_eq_map, bind_eq_bind, map_map, cons_bind, append_nil, nil_bind, (∘)]] theorem sublists_eq_sublists' (l : list α) : sublists l = map reverse (sublists' (reverse l)) := by rw [← sublists_reverse, reverse_reverse] theorem sublists'_reverse (l : list α) : sublists' (reverse l) = map reverse (sublists l) := by simp only [sublists_eq_sublists', map_map, map_id' (reverse_reverse)] theorem sublists'_eq_sublists (l : list α) : sublists' l = map reverse (sublists (reverse l)) := by rw [← sublists'_reverse, reverse_reverse] theorem sublists_aux_ne_nil : ∀ (l : list α), [] ∉ sublists_aux l cons | [] := id | (a::l) := begin rw [sublists_aux_cons_cons], refine not_mem_cons_of_ne_of_not_mem (cons_ne_nil _ _).symm _, have := sublists_aux_ne_nil l, revert this, induction sublists_aux l cons; intro, {rwa foldr}, simp only [foldr, mem_cons_iff, false_or, not_or_distrib], exact ⟨ne_of_not_mem_cons this, ih (not_mem_of_not_mem_cons this)⟩ end @[simp] theorem mem_sublists {s t : list α} : s ∈ sublists t ↔ s <+ t := by rw [← reverse_sublist_iff, ← mem_sublists', sublists'_reverse, mem_map_of_inj reverse_injective] @[simp] theorem length_sublists (l : list α) : length (sublists l) = 2 ^ length l := by simp only [sublists_eq_sublists', length_map, length_sublists', length_reverse] theorem map_ret_sublist_sublists (l : list α) : map list.ret l <+ sublists l := reverse_rec_on l (nil_sublist _) $ λ l a IH, by simp only [map, map_append, sublists_concat]; exact ((append_sublist_append_left _).2 $ singleton_sublist.2 $ mem_map.2 ⟨[], mem_sublists.2 (nil_sublist _), by refl⟩).trans ((append_sublist_append_right _).2 IH) /- forall₂ -/ section forall₂ variables {r : α → β → Prop} {p : γ → δ → Prop} open relator relation run_cmd tactic.mk_iff_of_inductive_prop `list.forall₂ `list.forall₂_iff @[simp] theorem forall₂_cons {R : α → β → Prop} {a b l₁ l₂} : forall₂ R (a::l₁) (b::l₂) ↔ R a b ∧ forall₂ R l₁ l₂ := ⟨λ h, by cases h with h₁ h₂; split; assumption, λ ⟨h₁, h₂⟩, forall₂.cons h₁ h₂⟩ theorem forall₂.imp {R S : α → β → Prop} (H : ∀ a b, R a b → S a b) {l₁ l₂} (h : forall₂ R l₁ l₂) : forall₂ S l₁ l₂ := by induction h; constructor; solve_by_elim lemma forall₂.mp {r q s : α → β → Prop} (h : ∀a b, r a b → q a b → s a b) : ∀{l₁ l₂}, forall₂ r l₁ l₂ → forall₂ q l₁ l₂ → forall₂ s l₁ l₂ | [] [] forall₂.nil forall₂.nil := forall₂.nil | (a::l₁) (b::l₂) (forall₂.cons hr hrs) (forall₂.cons hq hqs) := forall₂.cons (h a b hr hq) (forall₂.mp hrs hqs) lemma forall₂.flip : ∀{a b}, forall₂ (flip r) b a → forall₂ r a b | _ _ forall₂.nil := forall₂.nil | (a :: as) (b :: bs) (forall₂.cons h₁ h₂) := forall₂.cons h₁ h₂.flip lemma forall₂_same {r : α → α → Prop} : ∀{l}, (∀x∈l, r x x) → forall₂ r l l | [] _ := forall₂.nil | (a::as) h := forall₂.cons (h _ (mem_cons_self _ _)) (forall₂_same $ assume a ha, h a $ mem_cons_of_mem _ ha) lemma forall₂_refl {r} [is_refl α r] (l : list α) : forall₂ r l l := forall₂_same $ assume a h, is_refl.refl _ _ lemma forall₂_eq_eq_eq : forall₂ ((=) : α → α → Prop) = (=) := begin funext a b, apply propext, split, { assume h, induction h, {refl}, simp only [*]; split; refl }, { assume h, subst h, exact forall₂_refl _ } end @[simp] lemma forall₂_nil_left_iff {l} : forall₂ r nil l ↔ l = nil := ⟨λ H, by cases H; refl, by rintro rfl; exact forall₂.nil⟩ @[simp] lemma forall₂_nil_right_iff {l} : forall₂ r l nil ↔ l = nil := ⟨λ H, by cases H; refl, by rintro rfl; exact forall₂.nil⟩ lemma forall₂_cons_left_iff {a l u} : forall₂ r (a::l) u ↔ (∃b u', r a b ∧ forall₂ r l u' ∧ u = b :: u') := iff.intro (assume h, match u, h with (b :: u'), forall₂.cons h₁ h₂ := ⟨b, u', h₁, h₂, rfl⟩ end) (assume h, match u, h with _, ⟨b, u', h₁, h₂, rfl⟩ := forall₂.cons h₁ h₂ end) lemma forall₂_cons_right_iff {b l u} : forall₂ r u (b::l) ↔ (∃a u', r a b ∧ forall₂ r u' l ∧ u = a :: u') := iff.intro (assume h, match u, h with (b :: u'), forall₂.cons h₁ h₂ := ⟨b, u', h₁, h₂, rfl⟩ end) (assume h, match u, h with _, ⟨b, u', h₁, h₂, rfl⟩ := forall₂.cons h₁ h₂ end) lemma forall₂_and_left {r : α → β → Prop} {p : α → Prop} : ∀l u, forall₂ (λa b, p a ∧ r a b) l u ↔ (∀a∈l, p a) ∧ forall₂ r l u | [] u := by simp only [forall₂_nil_left_iff, forall_prop_of_false (not_mem_nil _), imp_true_iff, true_and] | (a::l) u := by simp only [forall₂_and_left l, forall₂_cons_left_iff, forall_mem_cons, and_assoc, and_comm, and.left_comm, exists_and_distrib_left.symm] @[simp] lemma forall₂_map_left_iff {f : γ → α} : ∀{l u}, forall₂ r (map f l) u ↔ forall₂ (λc b, r (f c) b) l u | [] _ := by simp only [map, forall₂_nil_left_iff] | (a::l) _ := by simp only [map, forall₂_cons_left_iff, forall₂_map_left_iff] @[simp] lemma forall₂_map_right_iff {f : γ → β} : ∀{l u}, forall₂ r l (map f u) ↔ forall₂ (λa c, r a (f c)) l u | _ [] := by simp only [map, forall₂_nil_right_iff] | _ (b::u) := by simp only [map, forall₂_cons_right_iff, forall₂_map_right_iff] lemma left_unique_forall₂ (hr : left_unique r) : left_unique (forall₂ r) | a₀ nil a₁ forall₂.nil forall₂.nil := rfl | (a₀::l₀) (b::l) (a₁::l₁) (forall₂.cons ha₀ h₀) (forall₂.cons ha₁ h₁) := hr ha₀ ha₁ ▸ left_unique_forall₂ h₀ h₁ ▸ rfl lemma right_unique_forall₂ (hr : right_unique r) : right_unique (forall₂ r) | nil a₀ a₁ forall₂.nil forall₂.nil := rfl | (b::l) (a₀::l₀) (a₁::l₁) (forall₂.cons ha₀ h₀) (forall₂.cons ha₁ h₁) := hr ha₀ ha₁ ▸ right_unique_forall₂ h₀ h₁ ▸ rfl lemma bi_unique_forall₂ (hr : bi_unique r) : bi_unique (forall₂ r) := ⟨assume a b c, left_unique_forall₂ hr.1, assume a b c, right_unique_forall₂ hr.2⟩ theorem forall₂_length_eq {R : α → β → Prop} : ∀ {l₁ l₂}, forall₂ R l₁ l₂ → length l₁ = length l₂ | _ _ forall₂.nil := rfl | _ _ (forall₂.cons h₁ h₂) := congr_arg succ (forall₂_length_eq h₂) theorem forall₂_zip {R : α → β → Prop} : ∀ {l₁ l₂}, forall₂ R l₁ l₂ → ∀ {a b}, (a, b) ∈ zip l₁ l₂ → R a b | _ _ (forall₂.cons h₁ h₂) x y (or.inl rfl) := h₁ | _ _ (forall₂.cons h₁ h₂) x y (or.inr h₃) := forall₂_zip h₂ h₃ theorem forall₂_iff_zip {R : α → β → Prop} {l₁ l₂} : forall₂ R l₁ l₂ ↔ length l₁ = length l₂ ∧ ∀ {a b}, (a, b) ∈ zip l₁ l₂ → R a b := ⟨λ h, ⟨forall₂_length_eq h, @forall₂_zip _ _ _ _ _ h⟩, λ h, begin cases h with h₁ h₂, induction l₁ with a l₁ IH generalizing l₂, { cases length_eq_zero.1 h₁.symm, constructor }, { cases l₂ with b l₂; injection h₁ with h₁, exact forall₂.cons (h₂ $ or.inl rfl) (IH h₁ $ λ a b h, h₂ $ or.inr h) } end⟩ theorem forall₂_take {R : α → β → Prop} : ∀ n {l₁ l₂}, forall₂ R l₁ l₂ → forall₂ R (take n l₁) (take n l₂) | 0 _ _ _ := by simp only [forall₂.nil, take] | (n+1) _ _ (forall₂.nil) := by simp only [forall₂.nil, take] | (n+1) _ _ (forall₂.cons h₁ h₂) := by simp [and.intro h₁ h₂, forall₂_take n] theorem forall₂_drop {R : α → β → Prop} : ∀ n {l₁ l₂}, forall₂ R l₁ l₂ → forall₂ R (drop n l₁) (drop n l₂) | 0 _ _ h := by simp only [drop, h] | (n+1) _ _ (forall₂.nil) := by simp only [forall₂.nil, drop] | (n+1) _ _ (forall₂.cons h₁ h₂) := by simp [and.intro h₁ h₂, forall₂_drop n] theorem forall₂_take_append {R : α → β → Prop} (l : list α) (l₁ : list β) (l₂ : list β) (h : forall₂ R l (l₁ ++ l₂)) : forall₂ R (list.take (length l₁) l) l₁ := have h': forall₂ R (take (length l₁) l) (take (length l₁) (l₁ ++ l₂)), from forall₂_take (length l₁) h, by rwa [take_left] at h' theorem forall₂_drop_append {R : α → β → Prop} (l : list α) (l₁ : list β) (l₂ : list β) (h : forall₂ R l (l₁ ++ l₂)) : forall₂ R (list.drop (length l₁) l) l₂ := have h': forall₂ R (drop (length l₁) l) (drop (length l₁) (l₁ ++ l₂)), from forall₂_drop (length l₁) h, by rwa [drop_left] at h' lemma rel_mem (hr : bi_unique r) : (r ⇒ forall₂ r ⇒ iff) (∈) (∈) | a b h [] [] forall₂.nil := by simp only [not_mem_nil] | a b h (a'::as) (b'::bs) (forall₂.cons h₁ h₂) := rel_or (rel_eq hr h h₁) (rel_mem h h₂) lemma rel_map : ((r ⇒ p) ⇒ forall₂ r ⇒ forall₂ p) map map | f g h [] [] forall₂.nil := forall₂.nil | f g h (a::as) (b::bs) (forall₂.cons h₁ h₂) := forall₂.cons (h h₁) (rel_map @h h₂) lemma rel_append : (forall₂ r ⇒ forall₂ r ⇒ forall₂ r) append append | [] [] h l₁ l₂ hl := hl | (a::as) (b::bs) (forall₂.cons h₁ h₂) l₁ l₂ hl := forall₂.cons h₁ (rel_append h₂ hl) lemma rel_join : (forall₂ (forall₂ r) ⇒ forall₂ r) join join | [] [] forall₂.nil := forall₂.nil | (a::as) (b::bs) (forall₂.cons h₁ h₂) := rel_append h₁ (rel_join h₂) lemma rel_bind : (forall₂ r ⇒ (r ⇒ forall₂ p) ⇒ forall₂ p) list.bind list.bind := assume a b h₁ f g h₂, rel_join (rel_map @h₂ h₁) lemma rel_foldl : ((p ⇒ r ⇒ p) ⇒ p ⇒ forall₂ r ⇒ p) foldl foldl | f g hfg _ _ h _ _ forall₂.nil := h | f g hfg x y hxy _ _ (forall₂.cons hab hs) := rel_foldl @hfg (hfg hxy hab) hs lemma rel_foldr : ((r ⇒ p ⇒ p) ⇒ p ⇒ forall₂ r ⇒ p) foldr foldr | f g hfg _ _ h _ _ forall₂.nil := h | f g hfg x y hxy _ _ (forall₂.cons hab hs) := hfg hab (rel_foldr @hfg hxy hs) lemma rel_filter {p : α → Prop} {q : β → Prop} [decidable_pred p] [decidable_pred q] (hpq : (r ⇒ (↔)) p q) : (forall₂ r ⇒ forall₂ r) (filter p) (filter q) | _ _ forall₂.nil := forall₂.nil | (a::as) (b::bs) (forall₂.cons h₁ h₂) := begin by_cases p a, { have : q b, { rwa [← hpq h₁] }, simp only [filter_cons_of_pos _ h, filter_cons_of_pos _ this, forall₂_cons, h₁, rel_filter h₂, and_true], }, { have : ¬ q b, { rwa [← hpq h₁] }, simp only [filter_cons_of_neg _ h, filter_cons_of_neg _ this, rel_filter h₂], }, end theorem filter_map_cons (f : α → option β) (a : α) (l : list α) : filter_map f (a :: l) = option.cases_on (f a) (filter_map f l) (λb, b :: filter_map f l) := begin generalize eq : f a = b, cases b, { rw filter_map_cons_none _ _ eq }, { rw filter_map_cons_some _ _ _ eq }, end lemma rel_filter_map {f : α → option γ} {q : β → option δ} : ((r ⇒ option.rel p) ⇒ forall₂ r ⇒ forall₂ p) filter_map filter_map | f g hfg _ _ forall₂.nil := forall₂.nil | f g hfg (a::as) (b::bs) (forall₂.cons h₁ h₂) := by rw [filter_map_cons, filter_map_cons]; from match f a, g b, hfg h₁ with | _, _, option.rel.none := rel_filter_map @hfg h₂ | _, _, option.rel.some h := forall₂.cons h (rel_filter_map @hfg h₂) end @[to_additive list.rel_sum] lemma rel_prod [monoid α] [monoid β] (h : r 1 1) (hf : (r ⇒ r ⇒ r) (*) (*)) : (forall₂ r ⇒ r) prod prod := assume a b, rel_foldl (assume a b, hf) h end forall₂ /- sections -/ theorem mem_sections {L : list (list α)} {f} : f ∈ sections L ↔ forall₂ (∈) f L := begin refine ⟨λ h, _, λ h, _⟩, { induction L generalizing f, {cases mem_singleton.1 h, exact forall₂.nil}, simp only [sections, bind_eq_bind, mem_bind, mem_map] at h, rcases h with ⟨_, _, _, _, rfl⟩, simp only [*, forall₂_cons, true_and] }, { induction h with a l f L al fL fs, {exact or.inl rfl}, simp only [sections, bind_eq_bind, mem_bind, mem_map], exact ⟨_, fs, _, al, rfl, rfl⟩ } end theorem mem_sections_length {L : list (list α)} {f} (h : f ∈ sections L) : length f = length L := forall₂_length_eq (mem_sections.1 h) lemma rel_sections {r : α → β → Prop} : (forall₂ (forall₂ r) ⇒ forall₂ (forall₂ r)) sections sections | _ _ forall₂.nil := forall₂.cons forall₂.nil forall₂.nil | _ _ (forall₂.cons h₀ h₁) := rel_bind (rel_sections h₁) (assume _ _ hl, rel_map (assume _ _ ha, forall₂.cons ha hl) h₀) /- permutations -/ section permutations @[simp] theorem permutations_aux_nil (is : list α) : permutations_aux [] is = [] := by rw [permutations_aux, permutations_aux.rec] @[simp] theorem permutations_aux_cons (t : α) (ts is : list α) : permutations_aux (t :: ts) is = foldr (λy r, (permutations_aux2 t ts r y id).2) (permutations_aux ts (t::is)) (permutations is) := by rw [permutations_aux, permutations_aux.rec]; refl end permutations /- insert -/ section insert variable [decidable_eq α] @[simp] theorem insert_nil (a : α) : insert a nil = [a] := rfl theorem insert.def (a : α) (l : list α) : insert a l = if a ∈ l then l else a :: l := rfl @[simp] theorem insert_of_mem {a : α} {l : list α} (h : a ∈ l) : insert a l = l := by simp only [insert.def, if_pos h] @[simp] theorem insert_of_not_mem {a : α} {l : list α} (h : a ∉ l) : insert a l = a :: l := by simp only [insert.def, if_neg h]; split; refl @[simp] theorem mem_insert_iff {a b : α} {l : list α} : a ∈ insert b l ↔ a = b ∨ a ∈ l := begin by_cases h' : b ∈ l, { simp only [insert_of_mem h'], apply (or_iff_right_of_imp _).symm, exact λ e, e.symm ▸ h' }, simp only [insert_of_not_mem h', mem_cons_iff] end @[simp] theorem suffix_insert (a : α) (l : list α) : l <:+ insert a l := by by_cases a ∈ l; [simp only [insert_of_mem h], simp only [insert_of_not_mem h, suffix_cons]] @[simp] theorem mem_insert_self (a : α) (l : list α) : a ∈ insert a l := mem_insert_iff.2 (or.inl rfl) @[simp] theorem mem_insert_of_mem {a b : α} {l : list α} (h : a ∈ l) : a ∈ insert b l := mem_insert_iff.2 (or.inr h) theorem eq_or_mem_of_mem_insert {a b : α} {l : list α} (h : a ∈ insert b l) : a = b ∨ a ∈ l := mem_insert_iff.1 h @[simp] theorem length_insert_of_mem {a : α} [decidable_eq α] {l : list α} (h : a ∈ l) : length (insert a l) = length l := by rw insert_of_mem h @[simp] theorem length_insert_of_not_mem {a : α} [decidable_eq α] {l : list α} (h : a ∉ l) : length (insert a l) = length l + 1 := by rw insert_of_not_mem h; refl end insert /- erasep -/ section erasep variables {p : α → Prop} [decidable_pred p] @[simp] theorem erasep_nil : [].erasep p = [] := rfl theorem erasep_cons (a : α) (l : list α) : (a :: l).erasep p = if p a then l else a :: l.erasep p := rfl @[simp] theorem erasep_cons_of_pos {a : α} {l : list α} (h : p a) : (a :: l).erasep p = l := by simp [erasep_cons, h] @[simp] theorem erasep_cons_of_neg {a : α} {l : list α} (h : ¬ p a) : (a::l).erasep p = a :: l.erasep p := by simp [erasep_cons, h] theorem erasep_of_forall_not {l : list α} (h : ∀ a ∈ l, ¬ p a) : l.erasep p = l := by induction l with _ _ ih; [refl, simp [h _ (or.inl rfl), ih (forall_mem_of_forall_mem_cons h)]] theorem exists_of_erasep {l : list α} {a} (al : a ∈ l) (pa : p a) : ∃ a l₁ l₂, (∀ b ∈ l₁, ¬ p b) ∧ p a ∧ l = l₁ ++ a :: l₂ ∧ l.erasep p = l₁ ++ l₂ := begin induction l with b l IH, {cases al}, by_cases pb : p b, { exact ⟨b, [], l, forall_mem_nil _, pb, by simp [pb]⟩ }, { rcases al with rfl | al, {exact pb.elim pa}, rcases IH al with ⟨c, l₁, l₂, h₁, h₂, h₃, h₄⟩, exact ⟨c, b::l₁, l₂, forall_mem_cons.2 ⟨pb, h₁⟩, h₂, by rw h₃; refl, by simp [pb, h₄]⟩ } end theorem exists_or_eq_self_of_erasep (l : list α) : l.erasep p = l ∨ ∃ a l₁ l₂, (∀ b ∈ l₁, ¬ p b) ∧ p a ∧ l = l₁ ++ a :: l₂ ∧ l.erasep p = l₁ ++ l₂ := begin by_cases h : ∃ a ∈ l, p a, { rcases h with ⟨a, ha, pa⟩, exact or.inr (exists_of_erasep ha pa) }, { simp at h, exact or.inl (erasep_of_forall_not h) } end @[simp] theorem length_erasep_of_mem {l : list α} {a} (al : a ∈ l) (pa : p a) : length (l.erasep p) = pred (length l) := by rcases exists_of_erasep al pa with ⟨_, l₁, l₂, _, _, e₁, e₂⟩; rw e₂; simp [-add_comm, e₁]; refl theorem erasep_append_left {a : α} (pa : p a) : ∀ {l₁ : list α} (l₂), a ∈ l₁ → (l₁++l₂).erasep p = l₁.erasep p ++ l₂ | (x::xs) l₂ h := begin by_cases h' : p x; simp [h'], rw erasep_append_left l₂ (mem_of_ne_of_mem (mt _ h') h), rintro rfl, exact pa end theorem erasep_append_right : ∀ {l₁ : list α} (l₂), (∀ b ∈ l₁, ¬ p b) → (l₁++l₂).erasep p = l₁ ++ l₂.erasep p | [] l₂ h := rfl | (x::xs) l₂ h := by simp [(forall_mem_cons.1 h).1, erasep_append_right _ (forall_mem_cons.1 h).2] theorem erasep_sublist (l : list α) : l.erasep p <+ l := by rcases exists_or_eq_self_of_erasep l with h | ⟨c, l₁, l₂, h₁, h₂, h₃, h₄⟩; [rw h, {rw [h₄, h₃], simp}] theorem erasep_subset (l : list α) : l.erasep p ⊆ l := subset_of_sublist (erasep_sublist l) theorem erasep_sublist_erasep {l₁ l₂ : list α} (s : l₁ <+ l₂) : l₁.erasep p <+ l₂.erasep p := begin induction s, case list.sublist.slnil { refl }, case list.sublist.cons : l₁ l₂ a s IH { by_cases h : p a; simp [h], exacts [IH.trans (erasep_sublist _), IH.cons _ _ _] }, case list.sublist.cons2 : l₁ l₂ a s IH { by_cases h : p a; simp [h], exacts [s, IH.cons2 _ _ _] } end theorem mem_of_mem_erasep {a : α} {l : list α} : a ∈ l.erasep p → a ∈ l := @erasep_subset _ _ _ _ _ @[simp] theorem mem_erasep_of_neg {a : α} {l : list α} (pa : ¬ p a) : a ∈ l.erasep p ↔ a ∈ l := ⟨mem_of_mem_erasep, λ al, begin rcases exists_or_eq_self_of_erasep l with h | ⟨c, l₁, l₂, h₁, h₂, h₃, h₄⟩, { rwa h }, { rw h₄, rw h₃ at al, have : a ≠ c, {rintro rfl, exact pa.elim h₂}, simpa [this] using al } end⟩ theorem erasep_map (f : β → α) : ∀ (l : list β), (map f l).erasep p = map f (l.erasep (p ∘ f)) | [] := rfl | (b::l) := by by_cases p (f b); simp [h, erasep_map l] @[simp] theorem extractp_eq_find_erasep : ∀ l : list α, extractp p l = (find p l, erasep p l) | [] := rfl | (a::l) := by by_cases pa : p a; simp [extractp, pa, extractp_eq_find_erasep l] end erasep /- erase -/ section erase variable [decidable_eq α] @[simp] theorem erase_nil (a : α) : [].erase a = [] := rfl theorem erase_cons (a b : α) (l : list α) : (b :: l).erase a = if b = a then l else b :: l.erase a := rfl @[simp] theorem erase_cons_head (a : α) (l : list α) : (a :: l).erase a = l := by simp only [erase_cons, if_pos rfl] @[simp] theorem erase_cons_tail {a b : α} (l : list α) (h : b ≠ a) : (b::l).erase a = b :: l.erase a := by simp only [erase_cons, if_neg h]; split; refl theorem erase_eq_erasep (a : α) (l : list α) : l.erase a = l.erasep (eq a) := by { induction l with b l, {refl}, by_cases a = b; [simp [h], simp [h, ne.symm h, *]] } @[simp] theorem erase_of_not_mem {a : α} {l : list α} (h : a ∉ l) : l.erase a = l := by rw [erase_eq_erasep, erasep_of_forall_not]; rintro b h' rfl; exact h h' theorem exists_erase_eq {a : α} {l : list α} (h : a ∈ l) : ∃ l₁ l₂, a ∉ l₁ ∧ l = l₁ ++ a :: l₂ ∧ l.erase a = l₁ ++ l₂ := by rcases exists_of_erasep h rfl with ⟨_, l₁, l₂, h₁, rfl, h₂, h₃⟩; rw erase_eq_erasep; exact ⟨l₁, l₂, λ h, h₁ _ h rfl, h₂, h₃⟩ @[simp] theorem length_erase_of_mem {a : α} {l : list α} (h : a ∈ l) : length (l.erase a) = pred (length l) := by rw erase_eq_erasep; exact length_erasep_of_mem h rfl theorem erase_append_left {a : α} {l₁ : list α} (l₂) (h : a ∈ l₁) : (l₁++l₂).erase a = l₁.erase a ++ l₂ := by simp [erase_eq_erasep]; exact erasep_append_left (by refl) l₂ h theorem erase_append_right {a : α} {l₁ : list α} (l₂) (h : a ∉ l₁) : (l₁++l₂).erase a = l₁ ++ l₂.erase a := by rw [erase_eq_erasep, erase_eq_erasep, erasep_append_right]; rintro b h' rfl; exact h h' theorem erase_sublist (a : α) (l : list α) : l.erase a <+ l := by rw erase_eq_erasep; apply erasep_sublist theorem erase_subset (a : α) (l : list α) : l.erase a ⊆ l := subset_of_sublist (erase_sublist a l) theorem erase_sublist_erase (a : α) {l₁ l₂ : list α} (h : l₁ <+ l₂) : l₁.erase a <+ l₂.erase a := by simp [erase_eq_erasep]; exact erasep_sublist_erasep h theorem mem_of_mem_erase {a b : α} {l : list α} : a ∈ l.erase b → a ∈ l := @erase_subset _ _ _ _ _ @[simp] theorem mem_erase_of_ne {a b : α} {l : list α} (ab : a ≠ b) : a ∈ l.erase b ↔ a ∈ l := by rw erase_eq_erasep; exact mem_erasep_of_neg ab.symm theorem erase_comm (a b : α) (l : list α) : (l.erase a).erase b = (l.erase b).erase a := if ab : a = b then by rw ab else if ha : a ∈ l then if hb : b ∈ l then match l, l.erase a, exists_erase_eq ha, hb with | ._, ._, ⟨l₁, l₂, ha', rfl, rfl⟩, hb := if h₁ : b ∈ l₁ then by rw [erase_append_left _ h₁, erase_append_left _ h₁, erase_append_right _ (mt mem_of_mem_erase ha'), erase_cons_head] else by rw [erase_append_right _ h₁, erase_append_right _ h₁, erase_append_right _ ha', erase_cons_tail _ ab, erase_cons_head] end else by simp only [erase_of_not_mem hb, erase_of_not_mem (mt mem_of_mem_erase hb)] else by simp only [erase_of_not_mem ha, erase_of_not_mem (mt mem_of_mem_erase ha)] theorem map_erase [decidable_eq β] {f : α → β} (finj : injective f) {a : α} (l : list α) : map f (l.erase a) = (map f l).erase (f a) := by rw [erase_eq_erasep, erase_eq_erasep, erasep_map]; congr; ext b; simp [finj.eq_iff] theorem map_foldl_erase [decidable_eq β] {f : α → β} (finj : injective f) {l₁ l₂ : list α} : map f (foldl list.erase l₁ l₂) = foldl (λ l a, l.erase (f a)) (map f l₁) l₂ := by induction l₂ generalizing l₁; [refl, simp only [foldl_cons, map_erase finj, *]] end erase /- diff -/ section diff variable [decidable_eq α] @[simp] theorem diff_nil (l : list α) : l.diff [] = l := rfl @[simp] theorem diff_cons (l₁ l₂ : list α) (a : α) : l₁.diff (a::l₂) = (l₁.erase a).diff l₂ := if h : a ∈ l₁ then by simp only [list.diff, if_pos h] else by simp only [list.diff, if_neg h, erase_of_not_mem h] @[simp] theorem nil_diff (l : list α) : [].diff l = [] := by induction l; [refl, simp only [*, diff_cons, erase_of_not_mem (not_mem_nil _)]] theorem diff_eq_foldl : ∀ (l₁ l₂ : list α), l₁.diff l₂ = foldl list.erase l₁ l₂ | l₁ [] := rfl | l₁ (a::l₂) := (diff_cons l₁ l₂ a).trans (diff_eq_foldl _ _) @[simp] theorem diff_append (l₁ l₂ l₃ : list α) : l₁.diff (l₂ ++ l₃) = (l₁.diff l₂).diff l₃ := by simp only [diff_eq_foldl, foldl_append] @[simp] theorem map_diff [decidable_eq β] {f : α → β} (finj : injective f) {l₁ l₂ : list α} : map f (l₁.diff l₂) = (map f l₁).diff (map f l₂) := by simp only [diff_eq_foldl, foldl_map, map_foldl_erase finj] theorem diff_sublist : ∀ l₁ l₂ : list α, l₁.diff l₂ <+ l₁ | l₁ [] := sublist.refl _ | l₁ (a::l₂) := calc l₁.diff (a :: l₂) = (l₁.erase a).diff l₂ : diff_cons _ _ _ ... <+ l₁.erase a : diff_sublist _ _ ... <+ l₁ : list.erase_sublist _ _ theorem diff_subset (l₁ l₂ : list α) : l₁.diff l₂ ⊆ l₁ := subset_of_sublist $ diff_sublist _ _ theorem mem_diff_of_mem {a : α} : ∀ {l₁ l₂ : list α}, a ∈ l₁ → a ∉ l₂ → a ∈ l₁.diff l₂ | l₁ [] h₁ h₂ := h₁ | l₁ (b::l₂) h₁ h₂ := by rw diff_cons; exact mem_diff_of_mem ((mem_erase_of_ne (ne_of_not_mem_cons h₂)).2 h₁) (not_mem_of_not_mem_cons h₂) theorem diff_sublist_of_sublist : ∀ {l₁ l₂ l₃: list α}, l₁ <+ l₂ → l₁.diff l₃ <+ l₂.diff l₃ | l₁ l₂ [] h := h | l₁ l₂ (a::l₃) h := by simp only [diff_cons, diff_sublist_of_sublist (erase_sublist_erase _ h)] theorem erase_diff_erase_sublist_of_sublist {a : α} : ∀ {l₁ l₂ : list α}, l₁ <+ l₂ → (l₂.erase a).diff (l₁.erase a) <+ l₂.diff l₁ | [] l₂ h := erase_sublist _ _ | (b::l₁) l₂ h := if heq : b = a then by simp only [heq, erase_cons_head, diff_cons] else by simpa only [erase_cons_head, erase_cons_tail _ heq, diff_cons, erase_comm a b l₂] using erase_diff_erase_sublist_of_sublist (erase_sublist_erase b h) end diff /- zip & unzip -/ @[simp] theorem zip_cons_cons (a : α) (b : β) (l₁ : list α) (l₂ : list β) : zip (a :: l₁) (b :: l₂) = (a, b) :: zip l₁ l₂ := rfl @[simp] theorem zip_nil_left (l : list α) : zip ([] : list β) l = [] := rfl @[simp] theorem zip_nil_right (l : list α) : zip l ([] : list β) = [] := by cases l; refl @[simp] theorem zip_swap : ∀ (l₁ : list α) (l₂ : list β), (zip l₁ l₂).map prod.swap = zip l₂ l₁ | [] l₂ := (zip_nil_right _).symm | l₁ [] := by rw zip_nil_right; refl | (a::l₁) (b::l₂) := by simp only [zip_cons_cons, map_cons, zip_swap l₁ l₂, prod.swap_prod_mk]; split; refl @[simp] theorem length_zip : ∀ (l₁ : list α) (l₂ : list β), length (zip l₁ l₂) = min (length l₁) (length l₂) | [] l₂ := rfl | l₁ [] := by simp only [length, zip_nil_right, min_zero] | (a::l₁) (b::l₂) := by by simp only [length, zip_cons_cons, length_zip l₁ l₂, min_add_add_right] theorem zip_append : ∀ {l₁ l₂ r₁ r₂ : list α} (h : length l₁ = length l₂), zip (l₁ ++ r₁) (l₂ ++ r₂) = zip l₁ l₂ ++ zip r₁ r₂ | [] l₂ r₁ r₂ h := by simp only [eq_nil_of_length_eq_zero h.symm]; refl | l₁ [] r₁ r₂ h := by simp only [eq_nil_of_length_eq_zero h]; refl | (a::l₁) (b::l₂) r₁ r₂ h := by simp only [cons_append, zip_cons_cons, zip_append (succ_inj h)]; split; refl theorem zip_map (f : α → γ) (g : β → δ) : ∀ (l₁ : list α) (l₂ : list β), zip (l₁.map f) (l₂.map g) = (zip l₁ l₂).map (prod.map f g) | [] l₂ := rfl | l₁ [] := by simp only [map, zip_nil_right] | (a::l₁) (b::l₂) := by simp only [map, zip_cons_cons, zip_map l₁ l₂, prod.map]; split; refl theorem zip_map_left (f : α → γ) (l₁ : list α) (l₂ : list β) : zip (l₁.map f) l₂ = (zip l₁ l₂).map (prod.map f id) := by rw [← zip_map, map_id] theorem zip_map_right (f : β → γ) (l₁ : list α) (l₂ : list β) : zip l₁ (l₂.map f) = (zip l₁ l₂).map (prod.map id f) := by rw [← zip_map, map_id] theorem zip_map' (f : α → β) (g : α → γ) : ∀ (l : list α), zip (l.map f) (l.map g) = l.map (λ a, (f a, g a)) | [] := rfl | (a::l) := by simp only [map, zip_cons_cons, zip_map' l]; split; refl theorem mem_zip {a b} : ∀ {l₁ : list α} {l₂ : list β}, (a, b) ∈ zip l₁ l₂ → a ∈ l₁ ∧ b ∈ l₂ | (_::l₁) (_::l₂) (or.inl rfl) := ⟨or.inl rfl, or.inl rfl⟩ | (a'::l₁) (b'::l₂) (or.inr h) := by split; simp only [mem_cons_iff, or_true, mem_zip h] @[simp] theorem unzip_nil : unzip (@nil (α × β)) = ([], []) := rfl @[simp] theorem unzip_cons (a : α) (b : β) (l : list (α × β)) : unzip ((a, b) :: l) = (a :: (unzip l).1, b :: (unzip l).2) := by rw unzip; cases unzip l; refl theorem unzip_eq_map : ∀ (l : list (α × β)), unzip l = (l.map prod.fst, l.map prod.snd) | [] := rfl | ((a, b) :: l) := by simp only [unzip_cons, map_cons, unzip_eq_map l] theorem unzip_left (l : list (α × β)) : (unzip l).1 = l.map prod.fst := by simp only [unzip_eq_map] theorem unzip_right (l : list (α × β)) : (unzip l).2 = l.map prod.snd := by simp only [unzip_eq_map] theorem unzip_swap (l : list (α × β)) : unzip (l.map prod.swap) = (unzip l).swap := by simp only [unzip_eq_map, map_map]; split; refl theorem zip_unzip : ∀ (l : list (α × β)), zip (unzip l).1 (unzip l).2 = l | [] := rfl | ((a, b) :: l) := by simp only [unzip_cons, zip_cons_cons, zip_unzip l]; split; refl theorem unzip_zip_left : ∀ {l₁ : list α} {l₂ : list β}, length l₁ ≤ length l₂ → (unzip (zip l₁ l₂)).1 = l₁ | [] l₂ h := rfl | l₁ [] h := by rw eq_nil_of_length_eq_zero (eq_zero_of_le_zero h); refl | (a::l₁) (b::l₂) h := by simp only [zip_cons_cons, unzip_cons, unzip_zip_left (le_of_succ_le_succ h)]; split; refl theorem unzip_zip_right {l₁ : list α} {l₂ : list β} (h : length l₂ ≤ length l₁) : (unzip (zip l₁ l₂)).2 = l₂ := by rw [← zip_swap, unzip_swap]; exact unzip_zip_left h theorem unzip_zip {l₁ : list α} {l₂ : list β} (h : length l₁ = length l₂) : unzip (zip l₁ l₂) = (l₁, l₂) := by rw [← @prod.mk.eta _ _ (unzip (zip l₁ l₂)), unzip_zip_left (le_of_eq h), unzip_zip_right (ge_of_eq h)] @[simp] theorem length_revzip (l : list α) : length (revzip l) = length l := by simp only [revzip, length_zip, length_reverse, min_self] @[simp] theorem unzip_revzip (l : list α) : (revzip l).unzip = (l, l.reverse) := unzip_zip (length_reverse l).symm @[simp] theorem revzip_map_fst (l : list α) : (revzip l).map prod.fst = l := by rw [← unzip_left, unzip_revzip] @[simp] theorem revzip_map_snd (l : list α) : (revzip l).map prod.snd = l.reverse := by rw [← unzip_right, unzip_revzip] theorem reverse_revzip (l : list α) : reverse l.revzip = revzip l.reverse := by rw [← zip_unzip.{u u} (revzip l).reverse, unzip_eq_map]; simp; simp [revzip] theorem revzip_swap (l : list α) : (revzip l).map prod.swap = revzip l.reverse := by simp [revzip] /- enum -/ theorem length_enum_from : ∀ n (l : list α), length (enum_from n l) = length l | n [] := rfl | n (a::l) := congr_arg nat.succ (length_enum_from _ _) theorem length_enum : ∀ (l : list α), length (enum l) = length l := length_enum_from _ @[simp] theorem enum_from_nth : ∀ n (l : list α) m, nth (enum_from n l) m = (λ a, (n + m, a)) <$> nth l m | n [] m := rfl | n (a :: l) 0 := rfl | n (a :: l) (m+1) := (enum_from_nth (n+1) l m).trans $ by rw [add_right_comm]; refl @[simp] theorem enum_nth : ∀ (l : list α) n, nth (enum l) n = (λ a, (n, a)) <$> nth l n := by simp only [enum, enum_from_nth, zero_add]; intros; refl @[simp] theorem enum_from_map_snd : ∀ n (l : list α), map prod.snd (enum_from n l) = l | n [] := rfl | n (a :: l) := congr_arg (cons _) (enum_from_map_snd _ _) @[simp] theorem enum_map_snd : ∀ (l : list α), map prod.snd (enum l) = l := enum_from_map_snd _ /- product -/ @[simp] theorem nil_product (l : list β) : product (@nil α) l = [] := rfl @[simp] theorem product_cons (a : α) (l₁ : list α) (l₂ : list β) : product (a::l₁) l₂ = map (λ b, (a, b)) l₂ ++ product l₁ l₂ := rfl @[simp] theorem product_nil : ∀ (l : list α), product l (@nil β) = [] | [] := rfl | (a::l) := by rw [product_cons, product_nil]; refl @[simp] theorem mem_product {l₁ : list α} {l₂ : list β} {a : α} {b : β} : (a, b) ∈ product l₁ l₂ ↔ a ∈ l₁ ∧ b ∈ l₂ := by simp only [product, mem_bind, mem_map, prod.ext_iff, exists_prop, and.left_comm, exists_and_distrib_left, exists_eq_left, exists_eq_right] theorem length_product (l₁ : list α) (l₂ : list β) : length (product l₁ l₂) = length l₁ * length l₂ := by induction l₁ with x l₁ IH; [exact (zero_mul _).symm, simp only [length, product_cons, length_append, IH, right_distrib, one_mul, length_map, add_comm]] /- sigma -/ section variable {σ : α → Type*} @[simp] theorem nil_sigma (l : Π a, list (σ a)) : (@nil α).sigma l = [] := rfl @[simp] theorem sigma_cons (a : α) (l₁ : list α) (l₂ : Π a, list (σ a)) : (a::l₁).sigma l₂ = map (sigma.mk a) (l₂ a) ++ l₁.sigma l₂ := rfl @[simp] theorem sigma_nil : ∀ (l : list α), l.sigma (λ a, @nil (σ a)) = [] | [] := rfl | (a::l) := by rw [sigma_cons, sigma_nil]; refl @[simp] theorem mem_sigma {l₁ : list α} {l₂ : Π a, list (σ a)} {a : α} {b : σ a} : sigma.mk a b ∈ l₁.sigma l₂ ↔ a ∈ l₁ ∧ b ∈ l₂ a := by simp only [list.sigma, mem_bind, mem_map, exists_prop, exists_and_distrib_left, and.left_comm, exists_eq_left, heq_iff_eq, exists_eq_right] theorem length_sigma (l₁ : list α) (l₂ : Π a, list (σ a)) : length (l₁.sigma l₂) = (l₁.map (λ a, length (l₂ a))).sum := by induction l₁ with x l₁ IH; [refl, simp only [map, sigma_cons, length_append, length_map, IH, sum_cons]] end /- of_fn -/ theorem length_of_fn_aux {n} (f : fin n → α) : ∀ m h l, length (of_fn_aux f m h l) = length l + m | 0 h l := rfl | (succ m) h l := (length_of_fn_aux m _ _).trans (succ_add _ _) @[simp] theorem length_of_fn {n} (f : fin n → α) : length (of_fn f) = n := (length_of_fn_aux f _ _ _).trans (zero_add _) theorem nth_of_fn_aux {n} (f : fin n → α) (i) : ∀ m h l, (∀ i, nth l i = of_fn_nth_val f (i + m)) → nth (of_fn_aux f m h l) i = of_fn_nth_val f i | 0 h l H := H i | (succ m) h l H := nth_of_fn_aux m _ _ begin intro j, cases j with j, { simp only [nth, of_fn_nth_val, zero_add, dif_pos (show m < n, from h)] }, { simp only [nth, H, succ_add] } end @[simp] theorem nth_of_fn {n} (f : fin n → α) (i) : nth (of_fn f) i = of_fn_nth_val f i := nth_of_fn_aux f _ _ _ _ $ λ i, by simp only [of_fn_nth_val, dif_neg (not_lt.2 (le_add_left n i))]; refl @[simp] theorem nth_le_of_fn {n} (f : fin n → α) (i : fin n) : nth_le (of_fn f) i.1 ((length_of_fn f).symm ▸ i.2) = f i := option.some.inj $ by rw [← nth_le_nth]; simp only [list.nth_of_fn, of_fn_nth_val, fin.eta, dif_pos i.2] theorem array_eq_of_fn {n} (a : array n α) : a.to_list = of_fn a.read := suffices ∀ {m h l}, d_array.rev_iterate_aux a (λ i, cons) m h l = of_fn_aux (d_array.read a) m h l, from this, begin intros, induction m with m IH generalizing l, {refl}, simp only [d_array.rev_iterate_aux, of_fn_aux, IH] end theorem of_fn_zero (f : fin 0 → α) : of_fn f = [] := rfl theorem of_fn_succ {n} (f : fin (succ n) → α) : of_fn f = f 0 :: of_fn (λ i, f i.succ) := suffices ∀ {m h l}, of_fn_aux f (succ m) (succ_le_succ h) l = f 0 :: of_fn_aux (λ i, f i.succ) m h l, from this, begin intros, induction m with m IH generalizing l, {refl}, rw [of_fn_aux, IH], refl end theorem of_fn_nth_le : ∀ l : list α, of_fn (λ i, nth_le l i.1 i.2) = l | [] := rfl | (a::l) := by rw of_fn_succ; congr; simp only [fin.succ_val]; exact of_fn_nth_le l /- disjoint -/ section disjoint theorem disjoint.symm {l₁ l₂ : list α} (d : disjoint l₁ l₂) : disjoint l₂ l₁ | a i₂ i₁ := d i₁ i₂ @[simp] theorem disjoint_comm {l₁ l₂ : list α} : disjoint l₁ l₂ ↔ disjoint l₂ l₁ := ⟨disjoint.symm, disjoint.symm⟩ theorem disjoint_left {l₁ l₂ : list α} : disjoint l₁ l₂ ↔ ∀ {a}, a ∈ l₁ → a ∉ l₂ := iff.rfl theorem disjoint_right {l₁ l₂ : list α} : disjoint l₁ l₂ ↔ ∀ {a}, a ∈ l₂ → a ∉ l₁ := disjoint_comm theorem disjoint_iff_ne {l₁ l₂ : list α} : disjoint l₁ l₂ ↔ ∀ a ∈ l₁, ∀ b ∈ l₂, a ≠ b := by simp only [disjoint_left, imp_not_comm, forall_eq'] theorem disjoint_of_subset_left {l₁ l₂ l : list α} (ss : l₁ ⊆ l) (d : disjoint l l₂) : disjoint l₁ l₂ | x m₁ := d (ss m₁) theorem disjoint_of_subset_right {l₁ l₂ l : list α} (ss : l₂ ⊆ l) (d : disjoint l₁ l) : disjoint l₁ l₂ | x m m₁ := d m (ss m₁) theorem disjoint_of_disjoint_cons_left {a : α} {l₁ l₂} : disjoint (a::l₁) l₂ → disjoint l₁ l₂ := disjoint_of_subset_left (list.subset_cons _ _) theorem disjoint_of_disjoint_cons_right {a : α} {l₁ l₂} : disjoint l₁ (a::l₂) → disjoint l₁ l₂ := disjoint_of_subset_right (list.subset_cons _ _) @[simp] theorem disjoint_nil_left (l : list α) : disjoint [] l | a := (not_mem_nil a).elim @[simp] theorem singleton_disjoint {l : list α} {a : α} : disjoint [a] l ↔ a ∉ l := by simp only [disjoint, mem_singleton, forall_eq]; refl @[simp] theorem disjoint_singleton {l : list α} {a : α} : disjoint l [a] ↔ a ∉ l := by rw disjoint_comm; simp only [singleton_disjoint] @[simp] theorem disjoint_append_left {l₁ l₂ l : list α} : disjoint (l₁++l₂) l ↔ disjoint l₁ l ∧ disjoint l₂ l := by simp only [disjoint, mem_append, or_imp_distrib, forall_and_distrib] @[simp] theorem disjoint_append_right {l₁ l₂ l : list α} : disjoint l (l₁++l₂) ↔ disjoint l l₁ ∧ disjoint l l₂ := disjoint_comm.trans $ by simp only [disjoint_comm, disjoint_append_left] @[simp] theorem disjoint_cons_left {a : α} {l₁ l₂ : list α} : disjoint (a::l₁) l₂ ↔ a ∉ l₂ ∧ disjoint l₁ l₂ := (@disjoint_append_left _ [a] l₁ l₂).trans $ by simp only [singleton_disjoint] @[simp] theorem disjoint_cons_right {a : α} {l₁ l₂ : list α} : disjoint l₁ (a::l₂) ↔ a ∉ l₁ ∧ disjoint l₁ l₂ := disjoint_comm.trans $ by simp only [disjoint_comm, disjoint_cons_left] theorem disjoint_of_disjoint_append_left_left {l₁ l₂ l : list α} (d : disjoint (l₁++l₂) l) : disjoint l₁ l := (disjoint_append_left.1 d).1 theorem disjoint_of_disjoint_append_left_right {l₁ l₂ l : list α} (d : disjoint (l₁++l₂) l) : disjoint l₂ l := (disjoint_append_left.1 d).2 theorem disjoint_of_disjoint_append_right_left {l₁ l₂ l : list α} (d : disjoint l (l₁++l₂)) : disjoint l l₁ := (disjoint_append_right.1 d).1 theorem disjoint_of_disjoint_append_right_right {l₁ l₂ l : list α} (d : disjoint l (l₁++l₂)) : disjoint l l₂ := (disjoint_append_right.1 d).2 end disjoint /- union -/ section union variable [decidable_eq α] @[simp] theorem nil_union (l : list α) : [] ∪ l = l := rfl @[simp] theorem cons_union (l₁ l₂ : list α) (a : α) : a :: l₁ ∪ l₂ = insert a (l₁ ∪ l₂) := rfl @[simp] theorem mem_union {l₁ l₂ : list α} {a : α} : a ∈ l₁ ∪ l₂ ↔ a ∈ l₁ ∨ a ∈ l₂ := by induction l₁; simp only [nil_union, not_mem_nil, false_or, cons_union, mem_insert_iff, mem_cons_iff, or_assoc, *] theorem mem_union_left {a : α} {l₁ : list α} (h : a ∈ l₁) (l₂ : list α) : a ∈ l₁ ∪ l₂ := mem_union.2 (or.inl h) theorem mem_union_right {a : α} (l₁ : list α) {l₂ : list α} (h : a ∈ l₂) : a ∈ l₁ ∪ l₂ := mem_union.2 (or.inr h) theorem sublist_suffix_of_union : ∀ l₁ l₂ : list α, ∃ t, t <+ l₁ ∧ t ++ l₂ = l₁ ∪ l₂ | [] l₂ := ⟨[], by refl, rfl⟩ | (a::l₁) l₂ := let ⟨t, s, e⟩ := sublist_suffix_of_union l₁ l₂ in if h : a ∈ l₁ ∪ l₂ then ⟨t, sublist_cons_of_sublist _ s, by simp only [e, cons_union, insert_of_mem h]⟩ else ⟨a::t, cons_sublist_cons _ s, by simp only [cons_append, cons_union, e, insert_of_not_mem h]; split; refl⟩ theorem suffix_union_right (l₁ l₂ : list α) : l₂ <:+ l₁ ∪ l₂ := (sublist_suffix_of_union l₁ l₂).imp (λ a, and.right) theorem union_sublist_append (l₁ l₂ : list α) : l₁ ∪ l₂ <+ l₁ ++ l₂ := let ⟨t, s, e⟩ := sublist_suffix_of_union l₁ l₂ in e ▸ (append_sublist_append_right _).2 s theorem forall_mem_union {p : α → Prop} {l₁ l₂ : list α} : (∀ x ∈ l₁ ∪ l₂, p x) ↔ (∀ x ∈ l₁, p x) ∧ (∀ x ∈ l₂, p x) := by simp only [mem_union, or_imp_distrib, forall_and_distrib] theorem forall_mem_of_forall_mem_union_left {p : α → Prop} {l₁ l₂ : list α} (h : ∀ x ∈ l₁ ∪ l₂, p x) : ∀ x ∈ l₁, p x := (forall_mem_union.1 h).1 theorem forall_mem_of_forall_mem_union_right {p : α → Prop} {l₁ l₂ : list α} (h : ∀ x ∈ l₁ ∪ l₂, p x) : ∀ x ∈ l₂, p x := (forall_mem_union.1 h).2 end union /- inter -/ section inter variable [decidable_eq α] @[simp] theorem inter_nil (l : list α) : [] ∩ l = [] := rfl @[simp] theorem inter_cons_of_mem {a : α} (l₁ : list α) {l₂ : list α} (h : a ∈ l₂) : (a::l₁) ∩ l₂ = a :: (l₁ ∩ l₂) := if_pos h @[simp] theorem inter_cons_of_not_mem {a : α} (l₁ : list α) {l₂ : list α} (h : a ∉ l₂) : (a::l₁) ∩ l₂ = l₁ ∩ l₂ := if_neg h theorem mem_of_mem_inter_left {l₁ l₂ : list α} {a : α} : a ∈ l₁ ∩ l₂ → a ∈ l₁ := mem_of_mem_filter theorem mem_of_mem_inter_right {l₁ l₂ : list α} {a : α} : a ∈ l₁ ∩ l₂ → a ∈ l₂ := of_mem_filter theorem mem_inter_of_mem_of_mem {l₁ l₂ : list α} {a : α} : a ∈ l₁ → a ∈ l₂ → a ∈ l₁ ∩ l₂ := mem_filter_of_mem @[simp] theorem mem_inter {a : α} {l₁ l₂ : list α} : a ∈ l₁ ∩ l₂ ↔ a ∈ l₁ ∧ a ∈ l₂ := mem_filter theorem inter_subset_left (l₁ l₂ : list α) : l₁ ∩ l₂ ⊆ l₁ := filter_subset _ theorem inter_subset_right (l₁ l₂ : list α) : l₁ ∩ l₂ ⊆ l₂ := λ a, mem_of_mem_inter_right theorem subset_inter {l l₁ l₂ : list α} (h₁ : l ⊆ l₁) (h₂ : l ⊆ l₂) : l ⊆ l₁ ∩ l₂ := λ a h, mem_inter.2 ⟨h₁ h, h₂ h⟩ theorem inter_eq_nil_iff_disjoint {l₁ l₂ : list α} : l₁ ∩ l₂ = [] ↔ disjoint l₁ l₂ := by simp only [eq_nil_iff_forall_not_mem, mem_inter, not_and]; refl theorem forall_mem_inter_of_forall_left {p : α → Prop} {l₁ : list α} (h : ∀ x ∈ l₁, p x) (l₂ : list α) : ∀ x, x ∈ l₁ ∩ l₂ → p x := ball.imp_left (λ x, mem_of_mem_inter_left) h theorem forall_mem_inter_of_forall_right {p : α → Prop} (l₁ : list α) {l₂ : list α} (h : ∀ x ∈ l₂, p x) : ∀ x, x ∈ l₁ ∩ l₂ → p x := ball.imp_left (λ x, mem_of_mem_inter_right) h end inter /- bag_inter -/ section bag_inter variable [decidable_eq α] @[simp] theorem nil_bag_inter (l : list α) : [].bag_inter l = [] := by cases l; refl @[simp] theorem bag_inter_nil (l : list α) : l.bag_inter [] = [] := by cases l; refl @[simp] theorem cons_bag_inter_of_pos {a} (l₁ : list α) {l₂} (h : a ∈ l₂) : (a :: l₁).bag_inter l₂ = a :: l₁.bag_inter (l₂.erase a) := by cases l₂; exact if_pos h @[simp] theorem cons_bag_inter_of_neg {a} (l₁ : list α) {l₂} (h : a ∉ l₂) : (a :: l₁).bag_inter l₂ = l₁.bag_inter l₂ := begin cases l₂, {simp only [bag_inter_nil]}, simp only [erase_of_not_mem h, list.bag_inter, if_neg h] end theorem mem_bag_inter {a : α} : ∀ {l₁ l₂ : list α}, a ∈ l₁.bag_inter l₂ ↔ a ∈ l₁ ∧ a ∈ l₂ | [] l₂ := by simp only [nil_bag_inter, not_mem_nil, false_and] | (b::l₁) l₂ := begin by_cases b ∈ l₂, { rw [cons_bag_inter_of_pos _ h, mem_cons_iff, mem_cons_iff, mem_bag_inter], by_cases ba : a = b, { simp only [ba, h, eq_self_iff_true, true_or, true_and] }, { simp only [mem_erase_of_ne ba, ba, false_or] } }, { rw [cons_bag_inter_of_neg _ h, mem_bag_inter, mem_cons_iff, or_and_distrib_right], symmetry, apply or_iff_right_of_imp, rintro ⟨rfl, h'⟩, exact h.elim h' } end theorem bag_inter_sublist_left : ∀ l₁ l₂ : list α, l₁.bag_inter l₂ <+ l₁ | [] l₂ := by simp [nil_sublist] | (b::l₁) l₂ := begin by_cases b ∈ l₂; simp [h], { apply cons_sublist_cons, apply bag_inter_sublist_left }, { apply sublist_cons_of_sublist, apply bag_inter_sublist_left } end end bag_inter /- pairwise relation (generalized no duplicate) -/ section pairwise run_cmd tactic.mk_iff_of_inductive_prop `list.pairwise `list.pairwise_iff variable {R : α → α → Prop} theorem rel_of_pairwise_cons {a : α} {l : list α} (p : pairwise R (a::l)) : ∀ {a'}, a' ∈ l → R a a' := (pairwise_cons.1 p).1 theorem pairwise_of_pairwise_cons {a : α} {l : list α} (p : pairwise R (a::l)) : pairwise R l := (pairwise_cons.1 p).2 theorem pairwise.imp_of_mem {S : α → α → Prop} {l : list α} (H : ∀ {a b}, a ∈ l → b ∈ l → R a b → S a b) (p : pairwise R l) : pairwise S l := begin induction p with a l r p IH generalizing H; constructor, { exact ball.imp_right (λ x h, H (mem_cons_self _ _) (mem_cons_of_mem _ h)) r }, { exact IH (λ a b m m', H (mem_cons_of_mem _ m) (mem_cons_of_mem _ m')) } end theorem pairwise.imp {S : α → α → Prop} (H : ∀ a b, R a b → S a b) {l : list α} : pairwise R l → pairwise S l := pairwise.imp_of_mem (λ a b _ _, H a b) theorem pairwise.and {S : α → α → Prop} {l : list α} : pairwise (λ a b, R a b ∧ S a b) l ↔ pairwise R l ∧ pairwise S l := ⟨λ h, ⟨h.imp (λ a b h, h.1), h.imp (λ a b h, h.2)⟩, λ ⟨hR, hS⟩, begin clear_, induction hR with a l R1 R2 IH; simp only [pairwise.nil, pairwise_cons] at *, exact ⟨λ b bl, ⟨R1 b bl, hS.1 b bl⟩, IH hS.2⟩ end⟩ theorem pairwise.imp₂ {S : α → α → Prop} {T : α → α → Prop} (H : ∀ a b, R a b → S a b → T a b) {l : list α} (hR : pairwise R l) (hS : pairwise S l) : pairwise T l := (pairwise.and.2 ⟨hR, hS⟩).imp $ λ a b, and.rec (H a b) theorem pairwise.iff_of_mem {S : α → α → Prop} {l : list α} (H : ∀ {a b}, a ∈ l → b ∈ l → (R a b ↔ S a b)) : pairwise R l ↔ pairwise S l := ⟨pairwise.imp_of_mem (λ a b m m', (H m m').1), pairwise.imp_of_mem (λ a b m m', (H m m').2)⟩ theorem pairwise.iff {S : α → α → Prop} (H : ∀ a b, R a b ↔ S a b) {l : list α} : pairwise R l ↔ pairwise S l := pairwise.iff_of_mem (λ a b _ _, H a b) theorem pairwise_of_forall {l : list α} (H : ∀ x y, R x y) : pairwise R l := by induction l; [exact pairwise.nil, simp only [*, pairwise_cons, forall_2_true_iff, and_true]] theorem pairwise.and_mem {l : list α} : pairwise R l ↔ pairwise (λ x y, x ∈ l ∧ y ∈ l ∧ R x y) l := pairwise.iff_of_mem (by simp only [true_and, iff_self, forall_2_true_iff] {contextual := tt}) theorem pairwise.imp_mem {l : list α} : pairwise R l ↔ pairwise (λ x y, x ∈ l → y ∈ l → R x y) l := pairwise.iff_of_mem (by simp only [forall_prop_of_true, iff_self, forall_2_true_iff] {contextual := tt}) theorem pairwise_of_sublist : Π {l₁ l₂ : list α}, l₁ <+ l₂ → pairwise R l₂ → pairwise R l₁ | ._ ._ sublist.slnil h := h | ._ ._ (sublist.cons l₁ l₂ a s) (pairwise.cons i n) := pairwise_of_sublist s n | ._ ._ (sublist.cons2 l₁ l₂ a s) (pairwise.cons i n) := (pairwise_of_sublist s n).cons (ball.imp_left (subset_of_sublist s) i) theorem forall_of_forall_of_pairwise (H : symmetric R) {l : list α} (H₁ : ∀ x ∈ l, R x x) (H₂ : pairwise R l) : ∀ (x ∈ l) (y ∈ l), R x y := begin induction l with a l IH, { exact forall_mem_nil _ }, cases forall_mem_cons.1 H₁ with H₁₁ H₁₂, cases pairwise_cons.1 H₂ with H₂₁ H₂₂, rintro x (rfl | hx) y (rfl | hy), exacts [H₁₁, H₂₁ _ hy, H (H₂₁ _ hx), IH H₁₂ H₂₂ _ hx _ hy] end lemma forall_of_pairwise (H : symmetric R) {l : list α} (hl : pairwise R l) : (∀a∈l, ∀b∈l, a ≠ b → R a b) := forall_of_forall_of_pairwise (λ a b h hne, H (h hne.symm)) (λ _ _ h, (h rfl).elim) (pairwise.imp (λ _ _ h _, h) hl) theorem pairwise_singleton (R) (a : α) : pairwise R [a] := by simp only [pairwise_cons, mem_singleton, forall_prop_of_false (not_mem_nil _), forall_true_iff, pairwise.nil, and_true] theorem pairwise_pair {a b : α} : pairwise R [a, b] ↔ R a b := by simp only [pairwise_cons, mem_singleton, forall_eq, forall_prop_of_false (not_mem_nil _), forall_true_iff, pairwise.nil, and_true] theorem pairwise_append {l₁ l₂ : list α} : pairwise R (l₁++l₂) ↔ pairwise R l₁ ∧ pairwise R l₂ ∧ ∀ x ∈ l₁, ∀ y ∈ l₂, R x y := by induction l₁ with x l₁ IH; [simp only [list.pairwise.nil, forall_prop_of_false (not_mem_nil _), forall_true_iff, and_true, true_and, nil_append], simp only [cons_append, pairwise_cons, forall_mem_append, IH, forall_mem_cons, forall_and_distrib, and_assoc, and.left_comm]] theorem pairwise_app_comm (s : symmetric R) {l₁ l₂ : list α} : pairwise R (l₁++l₂) ↔ pairwise R (l₂++l₁) := have ∀ l₁ l₂ : list α, (∀ (x : α), x ∈ l₁ → ∀ (y : α), y ∈ l₂ → R x y) → (∀ (x : α), x ∈ l₂ → ∀ (y : α), y ∈ l₁ → R x y), from λ l₁ l₂ a x xm y ym, s (a y ym x xm), by simp only [pairwise_append, and.left_comm]; rw iff.intro (this l₁ l₂) (this l₂ l₁) theorem pairwise_middle (s : symmetric R) {a : α} {l₁ l₂ : list α} : pairwise R (l₁ ++ a::l₂) ↔ pairwise R (a::(l₁++l₂)) := show pairwise R (l₁ ++ ([a] ++ l₂)) ↔ pairwise R ([a] ++ l₁ ++ l₂), by rw [← append_assoc, pairwise_append, @pairwise_append _ _ ([a] ++ l₁), pairwise_app_comm s]; simp only [mem_append, or_comm] theorem pairwise_map (f : β → α) : ∀ {l : list β}, pairwise R (map f l) ↔ pairwise (λ a b : β, R (f a) (f b)) l | [] := by simp only [map, pairwise.nil] | (b::l) := have (∀ a b', b' ∈ l → f b' = a → R (f b) a) ↔ ∀ (b' : β), b' ∈ l → R (f b) (f b'), from forall_swap.trans $ forall_congr $ λ a, forall_swap.trans $ by simp only [forall_eq'], by simp only [map, pairwise_cons, mem_map, exists_imp_distrib, and_imp, this, pairwise_map] theorem pairwise_of_pairwise_map {S : β → β → Prop} (f : α → β) (H : ∀ a b : α, S (f a) (f b) → R a b) {l : list α} (p : pairwise S (map f l)) : pairwise R l := ((pairwise_map f).1 p).imp H theorem pairwise_map_of_pairwise {S : β → β → Prop} (f : α → β) (H : ∀ a b : α, R a b → S (f a) (f b)) {l : list α} (p : pairwise R l) : pairwise S (map f l) := (pairwise_map f).2 $ p.imp H theorem pairwise_filter_map (f : β → option α) {l : list β} : pairwise R (filter_map f l) ↔ pairwise (λ a a' : β, ∀ (b ∈ f a) (b' ∈ f a'), R b b') l := let S (a a' : β) := ∀ (b ∈ f a) (b' ∈ f a'), R b b' in begin simp only [option.mem_def], induction l with a l IH, { simp only [filter_map, pairwise.nil] }, cases e : f a with b, { rw [filter_map_cons_none _ _ e, IH, pairwise_cons], simp only [e, forall_prop_of_false not_false, forall_3_true_iff, true_and] }, rw [filter_map_cons_some _ _ _ e], simp only [pairwise_cons, mem_filter_map, exists_imp_distrib, and_imp, IH, e, forall_eq'], show (∀ (a' : α) (x : β), x ∈ l → f x = some a' → R b a') ∧ pairwise S l ↔ (∀ (a' : β), a' ∈ l → ∀ (b' : α), f a' = some b' → R b b') ∧ pairwise S l, from and_congr ⟨λ h b mb a ma, h a b mb ma, λ h a b mb ma, h b mb a ma⟩ iff.rfl end theorem pairwise_filter_map_of_pairwise {S : β → β → Prop} (f : α → option β) (H : ∀ (a a' : α), R a a' → ∀ (b ∈ f a) (b' ∈ f a'), S b b') {l : list α} (p : pairwise R l) : pairwise S (filter_map f l) := (pairwise_filter_map _).2 $ p.imp H theorem pairwise_filter (p : α → Prop) [decidable_pred p] {l : list α} : pairwise R (filter p l) ↔ pairwise (λ x y, p x → p y → R x y) l := begin rw [← filter_map_eq_filter, pairwise_filter_map], apply pairwise.iff, intros, simp only [option.mem_def, option.guard_eq_some, and_imp, forall_eq'], end theorem pairwise_filter_of_pairwise (p : α → Prop) [decidable_pred p] {l : list α} : pairwise R l → pairwise R (filter p l) := pairwise_of_sublist (filter_sublist _) theorem pairwise_join {L : list (list α)} : pairwise R (join L) ↔ (∀ l ∈ L, pairwise R l) ∧ pairwise (λ l₁ l₂, ∀ (x ∈ l₁) (y ∈ l₂), R x y) L := begin induction L with l L IH, {simp only [join, pairwise.nil, forall_prop_of_false (not_mem_nil _), forall_const, and_self]}, have : (∀ (x : α), x ∈ l → ∀ (y : α) (x_1 : list α), x_1 ∈ L → y ∈ x_1 → R x y) ↔ ∀ (a' : list α), a' ∈ L → ∀ (x : α), x ∈ l → ∀ (y : α), y ∈ a' → R x y := ⟨λ h a b c d e, h c d e a b, λ h c d e a b, h a b c d e⟩, simp only [join, pairwise_append, IH, mem_join, exists_imp_distrib, and_imp, this, forall_mem_cons, pairwise_cons], simp only [and_assoc, and_comm, and.left_comm], end @[simp] theorem pairwise_reverse : ∀ {R} {l : list α}, pairwise R (reverse l) ↔ pairwise (λ x y, R y x) l := suffices ∀ {R l}, @pairwise α R l → pairwise (λ x y, R y x) (reverse l), from λ R l, ⟨λ p, reverse_reverse l ▸ this p, this⟩, λ R l p, by induction p with a l h p IH; [apply pairwise.nil, simpa only [reverse_cons, pairwise_append, IH, pairwise_cons, forall_prop_of_false (not_mem_nil _), forall_true_iff, pairwise.nil, mem_reverse, mem_singleton, forall_eq, true_and] using h] theorem pairwise_iff_nth_le {R} : ∀ {l : list α}, pairwise R l ↔ ∀ i j (h₁ : j < length l) (h₂ : i < j), R (nth_le l i (lt_trans h₂ h₁)) (nth_le l j h₁) | [] := by simp only [pairwise.nil, true_iff]; exact λ i j h, (not_lt_zero j).elim h | (a::l) := begin rw [pairwise_cons, pairwise_iff_nth_le], refine ⟨λ H i j h₁ h₂, _, λ H, ⟨λ a' m, _, λ i j h₁ h₂, H _ _ (succ_lt_succ h₁) (succ_lt_succ h₂)⟩⟩, { cases j with j, {exact (not_lt_zero _).elim h₂}, cases i with i, { exact H.1 _ (nth_le_mem l _ _) }, { exact H.2 _ _ (lt_of_succ_lt_succ h₁) (lt_of_succ_lt_succ h₂) } }, { rcases nth_le_of_mem m with ⟨n, h, rfl⟩, exact H _ _ (succ_lt_succ h) (succ_pos _) } end theorem pairwise_sublists' {R} : ∀ {l : list α}, pairwise R l → pairwise (lex (swap R)) (sublists' l) | _ pairwise.nil := pairwise_singleton _ _ | _ (@pairwise.cons _ _ a l H₁ H₂) := begin simp only [sublists'_cons, pairwise_append, pairwise_map, mem_sublists', mem_map, exists_imp_distrib, and_imp], have IH := pairwise_sublists' H₂, refine ⟨IH, IH.imp (λ l₁ l₂, lex.cons), _⟩, intros l₁ sl₁ x l₂ sl₂ e, subst e, cases l₁ with b l₁, {constructor}, exact lex.rel (H₁ _ $ subset_of_sublist sl₁ $ mem_cons_self _ _) end theorem pairwise_sublists {R} {l : list α} (H : pairwise R l) : pairwise (λ l₁ l₂, lex R (reverse l₁) (reverse l₂)) (sublists l) := by have := pairwise_sublists' (pairwise_reverse.2 H); rwa [sublists'_reverse, pairwise_map] at this /- pairwise reduct -/ variable [decidable_rel R] @[simp] theorem pw_filter_nil : pw_filter R [] = [] := rfl @[simp] theorem pw_filter_cons_of_pos {a : α} {l : list α} (h : ∀ b ∈ pw_filter R l, R a b) : pw_filter R (a::l) = a :: pw_filter R l := if_pos h @[simp] theorem pw_filter_cons_of_neg {a : α} {l : list α} (h : ¬ ∀ b ∈ pw_filter R l, R a b) : pw_filter R (a::l) = pw_filter R l := if_neg h theorem pw_filter_sublist : ∀ (l : list α), pw_filter R l <+ l | [] := nil_sublist _ | (x::l) := begin by_cases (∀ y ∈ pw_filter R l, R x y), { rw [pw_filter_cons_of_pos h], exact cons_sublist_cons _ (pw_filter_sublist l) }, { rw [pw_filter_cons_of_neg h], exact sublist_cons_of_sublist _ (pw_filter_sublist l) }, end theorem pw_filter_subset (l : list α) : pw_filter R l ⊆ l := subset_of_sublist (pw_filter_sublist _) theorem pairwise_pw_filter : ∀ (l : list α), pairwise R (pw_filter R l) | [] := pairwise.nil | (x::l) := begin by_cases (∀ y ∈ pw_filter R l, R x y), { rw [pw_filter_cons_of_pos h], exact pairwise_cons.2 ⟨h, pairwise_pw_filter l⟩ }, { rw [pw_filter_cons_of_neg h], exact pairwise_pw_filter l }, end theorem pw_filter_eq_self {l : list α} : pw_filter R l = l ↔ pairwise R l := ⟨λ e, e ▸ pairwise_pw_filter l, λ p, begin induction l with x l IH, {refl}, cases pairwise_cons.1 p with al p, rw [pw_filter_cons_of_pos (ball.imp_left (pw_filter_subset l) al), IH p], end⟩ @[simp] theorem pw_filter_idempotent {l : list α} : pw_filter R (pw_filter R l) = pw_filter R l := pw_filter_eq_self.mpr (pairwise_pw_filter l) theorem forall_mem_pw_filter (neg_trans : ∀ {x y z}, R x z → R x y ∨ R y z) (a : α) (l : list α) : (∀ b ∈ pw_filter R l, R a b) ↔ (∀ b ∈ l, R a b) := ⟨begin induction l with x l IH, { exact λ _ _, false.elim }, simp only [forall_mem_cons], by_cases (∀ y ∈ pw_filter R l, R x y); dsimp at h, { simp only [pw_filter_cons_of_pos h, forall_mem_cons, and_imp], exact λ r H, ⟨r, IH H⟩ }, { rw [pw_filter_cons_of_neg h], refine λ H, ⟨_, IH H⟩, cases e : find (λ y, ¬ R x y) (pw_filter R l) with k, { refine h.elim (ball.imp_right _ (find_eq_none.1 e)), exact λ y _, not_not.1 }, { have := find_some e, exact (neg_trans (H k (find_mem e))).resolve_right this } } end, ball.imp_left (pw_filter_subset l)⟩ end pairwise /- chain relation (conjunction of R a b ∧ R b c ∧ R c d ...) -/ section chain run_cmd tactic.mk_iff_of_inductive_prop `list.chain `list.chain_iff variable {R : α → α → Prop} theorem rel_of_chain_cons {a b : α} {l : list α} (p : chain R a (b::l)) : R a b := (chain_cons.1 p).1 theorem chain_of_chain_cons {a b : α} {l : list α} (p : chain R a (b::l)) : chain R b l := (chain_cons.1 p).2 theorem chain.imp {S : α → α → Prop} (H : ∀ a b, R a b → S a b) {a : α} {l : list α} (p : chain R a l) : chain S a l := by induction p with _ a b l r p IH; constructor; [exact H _ _ r, exact IH] theorem chain.iff {S : α → α → Prop} (H : ∀ a b, R a b ↔ S a b) {a : α} {l : list α} : chain R a l ↔ chain S a l := ⟨chain.imp (λ a b, (H a b).1), chain.imp (λ a b, (H a b).2)⟩ theorem chain.iff_mem {a : α} {l : list α} : chain R a l ↔ chain (λ x y, x ∈ a :: l ∧ y ∈ l ∧ R x y) a l := ⟨λ p, by induction p with _ a b l r p IH; constructor; [exact ⟨mem_cons_self _ _, mem_cons_self _ _, r⟩, exact IH.imp (λ a b ⟨am, bm, h⟩, ⟨mem_cons_of_mem _ am, mem_cons_of_mem _ bm, h⟩)], chain.imp (λ a b h, h.2.2)⟩ theorem chain_singleton {a b : α} : chain R a [b] ↔ R a b := by simp only [chain_cons, chain.nil, and_true] theorem chain_split {a b : α} {l₁ l₂ : list α} : chain R a (l₁++b::l₂) ↔ chain R a (l₁++[b]) ∧ chain R b l₂ := by induction l₁ with x l₁ IH generalizing a; simp only [*, nil_append, cons_append, chain.nil, chain_cons, and_true, and_assoc] theorem chain_map (f : β → α) {b : β} {l : list β} : chain R (f b) (map f l) ↔ chain (λ a b : β, R (f a) (f b)) b l := by induction l generalizing b; simp only [map, chain.nil, chain_cons, *] theorem chain_of_chain_map {S : β → β → Prop} (f : α → β) (H : ∀ a b : α, S (f a) (f b) → R a b) {a : α} {l : list α} (p : chain S (f a) (map f l)) : chain R a l := ((chain_map f).1 p).imp H theorem chain_map_of_chain {S : β → β → Prop} (f : α → β) (H : ∀ a b : α, R a b → S (f a) (f b)) {a : α} {l : list α} (p : chain R a l) : chain S (f a) (map f l) := (chain_map f).2 $ p.imp H theorem chain_of_pairwise {a : α} {l : list α} (p : pairwise R (a::l)) : chain R a l := begin cases pairwise_cons.1 p with r p', clear p, induction p' with b l r' p IH generalizing a, {exact chain.nil}, simp only [chain_cons, forall_mem_cons] at r, exact chain_cons.2 ⟨r.1, IH r'⟩ end theorem chain_iff_pairwise (tr : transitive R) {a : α} {l : list α} : chain R a l ↔ pairwise R (a::l) := ⟨λ c, begin induction c with b b c l r p IH, {exact pairwise_singleton _ _}, apply IH.cons _, simp only [mem_cons_iff, forall_mem_cons', r, true_and], show ∀ x ∈ l, R b x, from λ x m, (tr r (rel_of_pairwise_cons IH m)), end, chain_of_pairwise⟩ theorem chain'.imp {S : α → α → Prop} (H : ∀ a b, R a b → S a b) {l : list α} (p : chain' R l) : chain' S l := by cases l; [trivial, exact p.imp H] theorem chain'.iff {S : α → α → Prop} (H : ∀ a b, R a b ↔ S a b) {l : list α} : chain' R l ↔ chain' S l := ⟨chain'.imp (λ a b, (H a b).1), chain'.imp (λ a b, (H a b).2)⟩ theorem chain'.iff_mem {S : α → α → Prop} : ∀ {l : list α}, chain' R l ↔ chain' (λ x y, x ∈ l ∧ y ∈ l ∧ R x y) l | [] := iff.rfl | (x::l) := ⟨λ h, (chain.iff_mem.1 h).imp $ λ a b ⟨h₁, h₂, h₃⟩, ⟨h₁, or.inr h₂, h₃⟩, chain'.imp $ λ a b h, h.2.2⟩ theorem chain'_singleton (a : α) : chain' R [a] := chain.nil theorem chain'_split {a : α} : ∀ {l₁ l₂ : list α}, chain' R (l₁++a::l₂) ↔ chain' R (l₁++[a]) ∧ chain' R (a::l₂) | [] l₂ := (and_iff_right (chain'_singleton a)).symm | (b::l₁) l₂ := chain_split theorem chain'_map (f : β → α) {l : list β} : chain' R (map f l) ↔ chain' (λ a b : β, R (f a) (f b)) l := by cases l; [refl, exact chain_map _] theorem chain'_of_chain'_map {S : β → β → Prop} (f : α → β) (H : ∀ a b : α, S (f a) (f b) → R a b) {l : list α} (p : chain' S (map f l)) : chain' R l := ((chain'_map f).1 p).imp H theorem chain'_map_of_chain' {S : β → β → Prop} (f : α → β) (H : ∀ a b : α, R a b → S (f a) (f b)) {l : list α} (p : chain' R l) : chain' S (map f l) := (chain'_map f).2 $ p.imp H theorem chain'_of_pairwise : ∀ {l : list α}, pairwise R l → chain' R l | [] _ := trivial | (a::l) h := chain_of_pairwise h theorem chain'_iff_pairwise (tr : transitive R) : ∀ {l : list α}, chain' R l ↔ pairwise R l | [] := (iff_true_intro pairwise.nil).symm | (a::l) := chain_iff_pairwise tr end chain /- no duplicates predicate -/ section nodup @[simp] theorem forall_mem_ne {a : α} {l : list α} : (∀ (a' : α), a' ∈ l → ¬a = a') ↔ a ∉ l := ⟨λ h m, h _ m rfl, λ h a' m e, h (e.symm ▸ m)⟩ @[simp] theorem nodup_nil : @nodup α [] := pairwise.nil @[simp] theorem nodup_cons {a : α} {l : list α} : nodup (a::l) ↔ a ∉ l ∧ nodup l := by simp only [nodup, pairwise_cons, forall_mem_ne] lemma rel_nodup {r : α → β → Prop} (hr : relator.bi_unique r) : (forall₂ r ⇒ (↔)) nodup nodup | _ _ forall₂.nil := by simp only [nodup_nil] | _ _ (forall₂.cons hab h) := by simpa only [nodup_cons] using relator.rel_and (relator.rel_not (rel_mem hr hab h)) (rel_nodup h) theorem nodup_cons_of_nodup {a : α} {l : list α} (m : a ∉ l) (n : nodup l) : nodup (a::l) := nodup_cons.2 ⟨m, n⟩ theorem nodup_singleton (a : α) : nodup [a] := nodup_cons_of_nodup (not_mem_nil a) nodup_nil theorem nodup_of_nodup_cons {a : α} {l : list α} (h : nodup (a::l)) : nodup l := (nodup_cons.1 h).2 theorem not_mem_of_nodup_cons {a : α} {l : list α} (h : nodup (a::l)) : a ∉ l := (nodup_cons.1 h).1 theorem not_nodup_cons_of_mem {a : α} {l : list α} : a ∈ l → ¬ nodup (a :: l) := imp_not_comm.1 not_mem_of_nodup_cons theorem nodup_of_sublist {l₁ l₂ : list α} : l₁ <+ l₂ → nodup l₂ → nodup l₁ := pairwise_of_sublist theorem not_nodup_pair (a : α) : ¬ nodup [a, a] := not_nodup_cons_of_mem $ mem_singleton_self _ theorem nodup_iff_sublist {l : list α} : nodup l ↔ ∀ a, ¬ [a, a] <+ l := ⟨λ d a h, not_nodup_pair a (nodup_of_sublist h d), begin induction l with a l IH; intro h, {exact nodup_nil}, exact nodup_cons_of_nodup (λ al, h a $ cons_sublist_cons _ $ singleton_sublist.2 al) (IH $ λ a s, h a $ sublist_cons_of_sublist _ s) end⟩ theorem nodup_iff_nth_le_inj {l : list α} : nodup l ↔ ∀ i j h₁ h₂, nth_le l i h₁ = nth_le l j h₂ → i = j := pairwise_iff_nth_le.trans ⟨λ H i j h₁ h₂ h, ((lt_trichotomy _ _) .resolve_left (λ h', H _ _ h₂ h' h)) .resolve_right (λ h', H _ _ h₁ h' h.symm), λ H i j h₁ h₂ h, ne_of_lt h₂ (H _ _ _ _ h)⟩ @[simp] theorem nth_le_index_of [decidable_eq α] {l : list α} (H : nodup l) (n h) : index_of (nth_le l n h) l = n := nodup_iff_nth_le_inj.1 H _ _ _ h $ index_of_nth_le $ index_of_lt_length.2 $ nth_le_mem _ _ _ theorem nodup_iff_count_le_one [decidable_eq α] {l : list α} : nodup l ↔ ∀ a, count a l ≤ 1 := nodup_iff_sublist.trans $ forall_congr $ λ a, have [a, a] <+ l ↔ 1 < count a l, from (@le_count_iff_repeat_sublist _ _ a l 2).symm, (not_congr this).trans not_lt theorem nodup_repeat (a : α) : ∀ {n : ℕ}, nodup (repeat a n) ↔ n ≤ 1 | 0 := by simp [nat.zero_le] | 1 := by simp | (n+2) := iff_of_false (λ H, nodup_iff_sublist.1 H a ((repeat_sublist_repeat _).2 (le_add_left 2 n))) (not_le_of_lt $ le_add_left 2 n) @[simp] theorem count_eq_one_of_mem [decidable_eq α] {a : α} {l : list α} (d : nodup l) (h : a ∈ l) : count a l = 1 := le_antisymm (nodup_iff_count_le_one.1 d a) (count_pos.2 h) theorem nodup_of_nodup_append_left {l₁ l₂ : list α} : nodup (l₁++l₂) → nodup l₁ := nodup_of_sublist (sublist_append_left l₁ l₂) theorem nodup_of_nodup_append_right {l₁ l₂ : list α} : nodup (l₁++l₂) → nodup l₂ := nodup_of_sublist (sublist_append_right l₁ l₂) theorem nodup_append {l₁ l₂ : list α} : nodup (l₁++l₂) ↔ nodup l₁ ∧ nodup l₂ ∧ disjoint l₁ l₂ := by simp only [nodup, pairwise_append, disjoint_iff_ne] theorem disjoint_of_nodup_append {l₁ l₂ : list α} (d : nodup (l₁++l₂)) : disjoint l₁ l₂ := (nodup_append.1 d).2.2 theorem nodup_append_of_nodup {l₁ l₂ : list α} (d₁ : nodup l₁) (d₂ : nodup l₂) (dj : disjoint l₁ l₂) : nodup (l₁++l₂) := nodup_append.2 ⟨d₁, d₂, dj⟩ theorem nodup_app_comm {l₁ l₂ : list α} : nodup (l₁++l₂) ↔ nodup (l₂++l₁) := by simp only [nodup_append, and.left_comm, disjoint_comm] theorem nodup_middle {a : α} {l₁ l₂ : list α} : nodup (l₁ ++ a::l₂) ↔ nodup (a::(l₁++l₂)) := by simp only [nodup_append, not_or_distrib, and.left_comm, and_assoc, nodup_cons, mem_append, disjoint_cons_right] theorem nodup_of_nodup_map (f : α → β) {l : list α} : nodup (map f l) → nodup l := pairwise_of_pairwise_map f $ λ a b, mt $ congr_arg f theorem nodup_map_on {f : α → β} {l : list α} (H : ∀x∈l, ∀y∈l, f x = f y → x = y) (d : nodup l) : nodup (map f l) := pairwise_map_of_pairwise _ (by exact λ a b ⟨ma, mb, n⟩ e, n (H a ma b mb e)) (pairwise.and_mem.1 d) theorem nodup_map {f : α → β} {l : list α} (hf : injective f) : nodup l → nodup (map f l) := nodup_map_on (assume x _ y _ h, hf h) theorem nodup_map_iff {f : α → β} {l : list α} (hf : injective f) : nodup (map f l) ↔ nodup l := ⟨nodup_of_nodup_map _, nodup_map hf⟩ @[simp] theorem nodup_attach {l : list α} : nodup (attach l) ↔ nodup l := ⟨λ h, attach_map_val l ▸ nodup_map (λ a b, subtype.eq) h, λ h, nodup_of_nodup_map subtype.val ((attach_map_val l).symm ▸ h)⟩ theorem nodup_pmap {p : α → Prop} {f : Π a, p a → β} {l : list α} {H} (hf : ∀ a ha b hb, f a ha = f b hb → a = b) (h : nodup l) : nodup (pmap f l H) := by rw [pmap_eq_map_attach]; exact nodup_map (λ ⟨a, ha⟩ ⟨b, hb⟩ h, by congr; exact hf a (H _ ha) b (H _ hb) h) (nodup_attach.2 h) theorem nodup_filter (p : α → Prop) [decidable_pred p] {l} : nodup l → nodup (filter p l) := pairwise_filter_of_pairwise p @[simp] theorem nodup_reverse {l : list α} : nodup (reverse l) ↔ nodup l := pairwise_reverse.trans $ by simp only [nodup, ne.def, eq_comm] theorem nodup_erase_eq_filter [decidable_eq α] (a : α) {l} (d : nodup l) : l.erase a = filter (≠ a) l := begin induction d with b l m d IH, {refl}, by_cases b = a, { subst h, rw [erase_cons_head, filter_cons_of_neg], symmetry, rw filter_eq_self, simpa only [ne.def, eq_comm] using m, exact not_not_intro rfl }, { rw [erase_cons_tail _ h, filter_cons_of_pos, IH], exact h } end theorem nodup_erase_of_nodup [decidable_eq α] (a : α) {l} : nodup l → nodup (l.erase a) := nodup_of_sublist (erase_sublist _ _) theorem mem_erase_iff_of_nodup [decidable_eq α] {a b : α} {l} (d : nodup l) : a ∈ l.erase b ↔ a ≠ b ∧ a ∈ l := by rw nodup_erase_eq_filter b d; simp only [mem_filter, and_comm] theorem mem_erase_of_nodup [decidable_eq α] {a : α} {l} (h : nodup l) : a ∉ l.erase a := λ H, ((mem_erase_iff_of_nodup h).1 H).1 rfl theorem nodup_join {L : list (list α)} : nodup (join L) ↔ (∀ l ∈ L, nodup l) ∧ pairwise disjoint L := by simp only [nodup, pairwise_join, disjoint_left.symm, forall_mem_ne] theorem nodup_bind {l₁ : list α} {f : α → list β} : nodup (l₁.bind f) ↔ (∀ x ∈ l₁, nodup (f x)) ∧ pairwise (λ (a b : α), disjoint (f a) (f b)) l₁ := by simp only [list.bind, nodup_join, pairwise_map, and_comm, and.left_comm, mem_map, exists_imp_distrib, and_imp]; rw [show (∀ (l : list β) (x : α), f x = l → x ∈ l₁ → nodup l) ↔ (∀ (x : α), x ∈ l₁ → nodup (f x)), from forall_swap.trans $ forall_congr $ λ_, forall_eq'] theorem nodup_product {l₁ : list α} {l₂ : list β} (d₁ : nodup l₁) (d₂ : nodup l₂) : nodup (product l₁ l₂) := nodup_bind.2 ⟨λ a ma, nodup_map (injective_of_left_inverse (λ b, (rfl : (a,b).2 = b))) d₂, d₁.imp $ λ a₁ a₂ n x h₁ h₂, begin rcases mem_map.1 h₁ with ⟨b₁, mb₁, rfl⟩, rcases mem_map.1 h₂ with ⟨b₂, mb₂, ⟨⟩⟩, exact n rfl end⟩ theorem nodup_sigma {σ : α → Type*} {l₁ : list α} {l₂ : Π a, list (σ a)} (d₁ : nodup l₁) (d₂ : ∀ a, nodup (l₂ a)) : nodup (l₁.sigma l₂) := nodup_bind.2 ⟨λ a ma, nodup_map (λ b b' h, by injection h with _ h; exact eq_of_heq h) (d₂ a), d₁.imp $ λ a₁ a₂ n x h₁ h₂, begin rcases mem_map.1 h₁ with ⟨b₁, mb₁, rfl⟩, rcases mem_map.1 h₂ with ⟨b₂, mb₂, ⟨⟩⟩, exact n rfl end⟩ theorem nodup_filter_map {f : α → option β} {l : list α} (H : ∀ (a a' : α) (b : β), b ∈ f a → b ∈ f a' → a = a') : nodup l → nodup (filter_map f l) := pairwise_filter_map_of_pairwise f $ λ a a' n b bm b' bm' e, n $ H a a' b' (e ▸ bm) bm' theorem nodup_concat {a : α} {l : list α} (h : a ∉ l) (h' : nodup l) : nodup (concat l a) := by rw concat_eq_append; exact nodup_append_of_nodup h' (nodup_singleton _) (disjoint_singleton.2 h) theorem nodup_insert [decidable_eq α] {a : α} {l : list α} (h : nodup l) : nodup (insert a l) := if h' : a ∈ l then by rw [insert_of_mem h']; exact h else by rw [insert_of_not_mem h', nodup_cons]; split; assumption theorem nodup_union [decidable_eq α] (l₁ : list α) {l₂ : list α} (h : nodup l₂) : nodup (l₁ ∪ l₂) := begin induction l₁ with a l₁ ih generalizing l₂, { exact h }, apply nodup_insert, exact ih h end theorem nodup_inter_of_nodup [decidable_eq α] {l₁ : list α} (l₂) : nodup l₁ → nodup (l₁ ∩ l₂) := nodup_filter _ @[simp] theorem nodup_sublists {l : list α} : nodup (sublists l) ↔ nodup l := ⟨λ h, nodup_of_nodup_map _ (nodup_of_sublist (map_ret_sublist_sublists _) h), λ h, (pairwise_sublists h).imp (λ _ _ h, mt reverse_inj.2 h.to_ne)⟩ @[simp] theorem nodup_sublists' {l : list α} : nodup (sublists' l) ↔ nodup l := by rw [sublists'_eq_sublists, nodup_map_iff reverse_injective, nodup_sublists, nodup_reverse] end nodup /- erase duplicates function -/ section erase_dup variable [decidable_eq α] @[simp] theorem erase_dup_nil : erase_dup [] = ([] : list α) := rfl theorem erase_dup_cons_of_mem' {a : α} {l : list α} (h : a ∈ erase_dup l) : erase_dup (a::l) = erase_dup l := pw_filter_cons_of_neg $ by simpa only [forall_mem_ne] using h theorem erase_dup_cons_of_not_mem' {a : α} {l : list α} (h : a ∉ erase_dup l) : erase_dup (a::l) = a :: erase_dup l := pw_filter_cons_of_pos $ by simpa only [forall_mem_ne] using h @[simp] theorem mem_erase_dup {a : α} {l : list α} : a ∈ erase_dup l ↔ a ∈ l := by simpa only [erase_dup, forall_mem_ne, not_not] using not_congr (@forall_mem_pw_filter α (≠) _ (λ x y z xz, not_and_distrib.1 $ mt (and.rec eq.trans) xz) a l) @[simp] theorem erase_dup_cons_of_mem {a : α} {l : list α} (h : a ∈ l) : erase_dup (a::l) = erase_dup l := erase_dup_cons_of_mem' $ mem_erase_dup.2 h @[simp] theorem erase_dup_cons_of_not_mem {a : α} {l : list α} (h : a ∉ l) : erase_dup (a::l) = a :: erase_dup l := erase_dup_cons_of_not_mem' $ mt mem_erase_dup.1 h theorem erase_dup_sublist : ∀ (l : list α), erase_dup l <+ l := pw_filter_sublist theorem erase_dup_subset : ∀ (l : list α), erase_dup l ⊆ l := pw_filter_subset theorem subset_erase_dup (l : list α) : l ⊆ erase_dup l := λ a, mem_erase_dup.2 theorem nodup_erase_dup : ∀ l : list α, nodup (erase_dup l) := pairwise_pw_filter theorem erase_dup_eq_self {l : list α} : erase_dup l = l ↔ nodup l := pw_filter_eq_self @[simp] theorem erase_dup_idempotent {l : list α} : erase_dup (erase_dup l) = erase_dup l := pw_filter_idempotent theorem erase_dup_append (l₁ l₂ : list α) : erase_dup (l₁ ++ l₂) = l₁ ∪ erase_dup l₂ := begin induction l₁ with a l₁ IH, {refl}, rw [cons_union, ← IH], show erase_dup (a :: (l₁ ++ l₂)) = insert a (erase_dup (l₁ ++ l₂)), by_cases a ∈ erase_dup (l₁ ++ l₂); [ rw [erase_dup_cons_of_mem' h, insert_of_mem h], rw [erase_dup_cons_of_not_mem' h, insert_of_not_mem h]] end end erase_dup /- iota and range(') -/ @[simp] theorem length_range' : ∀ (s n : ℕ), length (range' s n) = n | s 0 := rfl | s (n+1) := congr_arg succ (length_range' _ _) @[simp] theorem mem_range' {m : ℕ} : ∀ {s n : ℕ}, m ∈ range' s n ↔ s ≤ m ∧ m < s + n | s 0 := (false_iff _).2 $ λ ⟨H1, H2⟩, not_le_of_lt H2 H1 | s (succ n) := have m = s → m < s + n + 1, from λ e, e ▸ lt_succ_of_le (le_add_right _ _), have l : m = s ∨ s + 1 ≤ m ↔ s ≤ m, by simpa only [eq_comm] using (@le_iff_eq_or_lt _ _ s m).symm, (mem_cons_iff _ _ _).trans $ by simp only [mem_range', or_and_distrib_left, or_iff_right_of_imp this, l, add_right_comm]; refl theorem map_add_range' (a) : ∀ s n : ℕ, map ((+) a) (range' s n) = range' (a + s) n | s 0 := rfl | s (n+1) := congr_arg (cons _) (map_add_range' (s+1) n) theorem chain_succ_range' : ∀ s n : ℕ, chain (λ a b, b = succ a) s (range' (s+1) n) | s 0 := chain.nil | s (n+1) := (chain_succ_range' (s+1) n).cons rfl theorem chain_lt_range' (s n : ℕ) : chain (<) s (range' (s+1) n) := (chain_succ_range' s n).imp (λ a b e, e.symm ▸ lt_succ_self _) theorem pairwise_lt_range' : ∀ s n : ℕ, pairwise (<) (range' s n) | s 0 := pairwise.nil | s (n+1) := (chain_iff_pairwise (by exact λ a b c, lt_trans)).1 (chain_lt_range' s n) theorem nodup_range' (s n : ℕ) : nodup (range' s n) := (pairwise_lt_range' s n).imp (λ a b, ne_of_lt) @[simp] theorem range'_append : ∀ s m n : ℕ, range' s m ++ range' (s+m) n = range' s (n+m) | s 0 n := rfl | s (m+1) n := show s :: (range' (s+1) m ++ range' (s+m+1) n) = s :: range' (s+1) (n+m), by rw [add_right_comm, range'_append] theorem range'_sublist_right {s m n : ℕ} : range' s m <+ range' s n ↔ m ≤ n := ⟨λ h, by simpa only [length_range'] using length_le_of_sublist h, λ h, by rw [← nat.sub_add_cancel h, ← range'_append]; apply sublist_append_left⟩ theorem range'_subset_right {s m n : ℕ} : range' s m ⊆ range' s n ↔ m ≤ n := ⟨λ h, le_of_not_lt $ λ hn, lt_irrefl (s+n) $ (mem_range'.1 $ h $ mem_range'.2 ⟨le_add_right _ _, nat.add_lt_add_left hn s⟩).2, λ h, subset_of_sublist (range'_sublist_right.2 h)⟩ theorem nth_range' : ∀ s {m n : ℕ}, m < n → nth (range' s n) m = some (s + m) | s 0 (n+1) _ := rfl | s (m+1) (n+1) h := (nth_range' (s+1) (lt_of_add_lt_add_right h)).trans $ by rw add_right_comm; refl theorem range'_concat (s n : ℕ) : range' s (n + 1) = range' s n ++ [s+n] := by rw add_comm n 1; exact (range'_append s n 1).symm theorem range_core_range' : ∀ s n : ℕ, range_core s (range' s n) = range' 0 (n + s) | 0 n := rfl | (s+1) n := by rw [show n+(s+1) = n+1+s, from add_right_comm n s 1]; exact range_core_range' s (n+1) theorem range_eq_range' (n : ℕ) : range n = range' 0 n := (range_core_range' n 0).trans $ by rw zero_add theorem range_succ_eq_map (n : ℕ) : range (n + 1) = 0 :: map succ (range n) := by rw [range_eq_range', range_eq_range', range', add_comm, ← map_add_range']; congr; exact funext one_add theorem range'_eq_map_range (s n : ℕ) : range' s n = map ((+) s) (range n) := by rw [range_eq_range', map_add_range']; refl @[simp] theorem length_range (n : ℕ) : length (range n) = n := by simp only [range_eq_range', length_range'] theorem pairwise_lt_range (n : ℕ) : pairwise (<) (range n) := by simp only [range_eq_range', pairwise_lt_range'] theorem nodup_range (n : ℕ) : nodup (range n) := by simp only [range_eq_range', nodup_range'] theorem range_sublist {m n : ℕ} : range m <+ range n ↔ m ≤ n := by simp only [range_eq_range', range'_sublist_right] theorem range_subset {m n : ℕ} : range m ⊆ range n ↔ m ≤ n := by simp only [range_eq_range', range'_subset_right] @[simp] theorem mem_range {m n : ℕ} : m ∈ range n ↔ m < n := by simp only [range_eq_range', mem_range', nat.zero_le, true_and, zero_add] @[simp] theorem not_mem_range_self {n : ℕ} : n ∉ range n := mt mem_range.1 $ lt_irrefl _ theorem nth_range {m n : ℕ} (h : m < n) : nth (range n) m = some m := by simp only [range_eq_range', nth_range' _ h, zero_add] theorem range_concat (n : ℕ) : range (n + 1) = range n ++ [n] := by simp only [range_eq_range', range'_concat, zero_add] theorem iota_eq_reverse_range' : ∀ n : ℕ, iota n = reverse (range' 1 n) | 0 := rfl | (n+1) := by simp only [iota, range'_concat, iota_eq_reverse_range' n, reverse_append, add_comm]; refl @[simp] theorem length_iota (n : ℕ) : length (iota n) = n := by simp only [iota_eq_reverse_range', length_reverse, length_range'] theorem pairwise_gt_iota (n : ℕ) : pairwise (>) (iota n) := by simp only [iota_eq_reverse_range', pairwise_reverse, pairwise_lt_range'] theorem nodup_iota (n : ℕ) : nodup (iota n) := by simp only [iota_eq_reverse_range', nodup_reverse, nodup_range'] theorem mem_iota {m n : ℕ} : m ∈ iota n ↔ 1 ≤ m ∧ m ≤ n := by simp only [iota_eq_reverse_range', mem_reverse, mem_range', add_comm, lt_succ_iff] theorem reverse_range' : ∀ s n : ℕ, reverse (range' s n) = map (λ i, s + n - 1 - i) (range n) | s 0 := rfl | s (n+1) := by rw [range'_concat, reverse_append, range_succ_eq_map]; simpa only [show s + (n + 1) - 1 = s + n, from rfl, (∘), λ a i, show a - 1 - i = a - succ i, from pred_sub _ _, reverse_singleton, map_cons, nat.sub_zero, cons_append, nil_append, eq_self_iff_true, true_and, map_map] using reverse_range' s n def Ico (n m : ℕ) : list ℕ := range' n (m - n) namespace Ico theorem map_add (n m k : ℕ) : (Ico n m).map ((+) k) = Ico (n + k) (m + k) := by rw [Ico, Ico, map_add_range', nat.add_sub_add_right, add_comm n k] theorem zero_bot (n : ℕ) : Ico 0 n = range n := by rw [Ico, nat.sub_zero, range_eq_range'] @[simp] theorem length (n m : ℕ) : length (Ico n m) = m - n := by dsimp [Ico]; simp only [length_range'] theorem pairwise_lt (n m : ℕ) : pairwise (<) (Ico n m) := by dsimp [Ico]; simp only [pairwise_lt_range'] theorem nodup (n m : ℕ) : nodup (Ico n m) := by dsimp [Ico]; simp only [nodup_range'] @[simp] theorem mem {n m l : ℕ} : l ∈ Ico n m ↔ n ≤ l ∧ l < m := suffices n ≤ l ∧ l < n + (m - n) ↔ n ≤ l ∧ l < m, by simp [Ico, this], begin cases le_total n m with hnm hmn, { rw [nat.add_sub_of_le hnm] }, { rw [nat.sub_eq_zero_of_le hmn, add_zero], exact and_congr_right (assume hnl, iff.intro (assume hln, (not_le_of_gt hln hnl).elim) (assume hlm, lt_of_lt_of_le hlm hmn)) } end theorem eq_nil_of_le {n m : ℕ} (h : m ≤ n) : Ico n m = [] := by simp [Ico, nat.sub_eq_zero_of_le h] @[simp] theorem self_empty {n : ℕ} : Ico n n = [] := eq_nil_of_le (le_refl n) @[simp] theorem eq_empty_iff {n m : ℕ} : Ico n m = [] ↔ m ≤ n := iff.intro (assume h, nat.le_of_sub_eq_zero $ by rw [← length, h]; refl) eq_nil_of_le lemma append_consecutive {n m l : ℕ} (hnm : n ≤ m) (hml : m ≤ l) : Ico n m ++ Ico m l = Ico n l := begin dunfold Ico, convert range'_append _ _ _, { exact (nat.add_sub_of_le hnm).symm }, { rwa [← nat.add_sub_assoc hnm, nat.sub_add_cancel] } end @[simp] theorem succ_singleton {n : ℕ} : Ico n (n+1) = [n] := by dsimp [Ico]; simp [nat.add_sub_cancel_left] @[simp] theorem succ_top {n m : ℕ} (h : n ≤ m) : Ico n (m + 1) = Ico n m ++ [m] := by rwa [← succ_singleton, append_consecutive]; exact nat.le_succ _ theorem eq_cons {n m : ℕ} (h : n < m) : Ico n m = n :: Ico (n + 1) m := by rw [← append_consecutive (nat.le_succ n) h, succ_singleton]; refl theorem pred_singleton {m : ℕ} (h : m > 0) : Ico (m - 1) m = [m - 1] := by dsimp [Ico]; rw nat.sub_sub_self h; simp theorem chain'_succ (n m : ℕ) : chain' (λa b, b = succ a) (Ico n m) := begin by_cases n < m, { rw [eq_cons h], exact chain_succ_range' _ _ }, { rw [eq_nil_of_le (le_of_not_gt h)], trivial } end @[simp] theorem not_mem_top {n m : ℕ} : m ∉ Ico n m := by simp; intros; refl lemma filter_lt_of_top_le {n m l : ℕ} (hml : m ≤ l) : (Ico n m).filter (λ x, x < l) = Ico n m := filter_eq_self.2 $ assume k hk, lt_of_lt_of_le (mem.1 hk).2 hml lemma filter_lt_of_le_bot {n m l : ℕ} (hln : l ≤ n) : (Ico n m).filter (λ x, x < l) = [] := filter_eq_nil.2 $ assume k hk, not_lt_of_le $ le_trans hln $ (mem.1 hk).1 lemma filter_lt_of_ge {n m l : ℕ} (hlm : l ≤ m) : (Ico n m).filter (λ x, x < l) = Ico n l := begin cases le_total n l with hnl hln, { rw [← append_consecutive hnl hlm, filter_append, filter_lt_of_top_le (le_refl l), filter_lt_of_le_bot (le_refl l), append_nil] }, { rw [eq_nil_of_le hln, filter_lt_of_le_bot hln] } end @[simp] lemma filter_lt (n m l : ℕ) : (Ico n m).filter (λ x, x < l) = Ico n (min m l) := begin cases le_total m l with hml hlm, { rw [min_eq_left hml, filter_lt_of_top_le hml] }, { rw [min_eq_right hlm, filter_lt_of_ge hlm] } end lemma filter_ge_of_le_bot {n m l : ℕ} (hln : l ≤ n) : (Ico n m).filter (λ x, x ≥ l) = Ico n m := filter_eq_self.2 $ assume k hk, le_trans hln (mem.1 hk).1 lemma filter_ge_of_top_le {n m l : ℕ} (hml : m ≤ l) : (Ico n m).filter (λ x, x ≥ l) = [] := filter_eq_nil.2 $ assume k hk, not_le_of_gt (lt_of_lt_of_le (mem.1 hk).2 hml) lemma filter_ge_of_ge {n m l : ℕ} (hnl : n ≤ l) : (Ico n m).filter (λ x, x ≥ l) = Ico l m := begin cases le_total l m with hlm hml, { rw [← append_consecutive hnl hlm, filter_append, filter_ge_of_top_le (le_refl l), filter_ge_of_le_bot (le_refl l), nil_append] }, { rw [eq_nil_of_le hml, filter_ge_of_top_le hml] } end @[simp] lemma filter_ge (n m l : ℕ) : (Ico n m).filter (λ x, x ≥ l) = Ico (max n l) m := begin cases le_total n l with hnl hln, { rw [max_eq_right hnl, filter_ge_of_ge hnl] }, { rw [max_eq_left hln, filter_ge_of_le_bot hln] } end end Ico @[simp] theorem enum_from_map_fst : ∀ n (l : list α), map prod.fst (enum_from n l) = range' n l.length | n [] := rfl | n (a :: l) := congr_arg (cons _) (enum_from_map_fst _ _) @[simp] theorem enum_map_fst (l : list α) : map prod.fst (enum l) = range l.length := by simp only [enum, enum_from_map_fst, range_eq_range'] theorem last'_mem {α} : ∀ a l, @last' α a l ∈ a :: l | a [] := or.inl rfl | a (b::l) := or.inr (last'_mem b l) @[simp] lemma nth_le_attach {α} (L : list α) (i) (H : i < L.attach.length) : (L.attach.nth_le i H).1 = L.nth_le i (length_attach L ▸ H) := calc (L.attach.nth_le i H).1 = (L.attach.map subtype.val).nth_le i (by simpa using H) : by rw nth_le_map' ... = L.nth_le i _ : by congr; apply attach_map_val @[simp] lemma nth_le_range {n} (i) (H : i < (range n).length) : nth_le (range n) i H = i := option.some.inj $ by rw [← nth_le_nth _, nth_range (by simpa using H)] theorem of_fn_eq_pmap {α n} {f : fin n → α} : of_fn f = pmap (λ i hi, f ⟨i, hi⟩) (range n) (λ _, mem_range.1) := by rw [pmap_eq_map_attach]; from ext_le (by simp) (λ i hi1 hi2, by simp at hi1; simp [nth_le_of_fn f ⟨i, hi1⟩]) theorem nodup_of_fn {α n} {f : fin n → α} (hf : function.injective f) : nodup (of_fn f) := by rw of_fn_eq_pmap; from nodup_pmap (λ _ _ _ _ H, fin.veq_of_eq $ hf H) (nodup_range n) section tfae /- tfae: The Following (propositions) Are Equivalent -/ theorem tfae_nil : tfae [] := forall_mem_nil _ theorem tfae_singleton (p) : tfae [p] := by simp [tfae] theorem tfae_cons_of_mem {a b} {l : list Prop} (h : b ∈ l) : tfae (a::l) ↔ (a ↔ b) ∧ tfae l := ⟨λ H, ⟨H a (by simp) b (or.inr h), λ p hp q hq, H _ (or.inr hp) _ (or.inr hq)⟩, begin rintro ⟨ab, H⟩ p (rfl | hp) q (rfl | hq), { refl }, { exact ab.trans (H _ h _ hq) }, { exact (ab.trans (H _ h _ hp)).symm }, { exact H _ hp _ hq } end⟩ theorem tfae_cons_cons {a b} {l : list Prop} : tfae (a::b::l) ↔ (a ↔ b) ∧ tfae (b::l) := tfae_cons_of_mem (or.inl rfl) theorem tfae_of_forall (b : Prop) (l : list Prop) (h : ∀ a ∈ l, a ↔ b) : tfae l := λ a₁ h₁ a₂ h₂, (h _ h₁).trans (h _ h₂).symm theorem tfae_of_cycle {a b} {l : list Prop} : list.chain (→) a (b::l) → (last' b l → a) → tfae (a::b::l) := begin induction l with c l IH generalizing a b; simp [tfae_cons_cons, tfae_singleton] at *, { intros a _ b, exact iff.intro a b }, intros ab bc ch la, have := IH bc ch (ab ∘ la), exact ⟨⟨ab, la ∘ (this.2 c (or.inl rfl) _ (last'_mem _ _)).1 ∘ bc⟩, this⟩ end theorem tfae.out {l} (h : tfae l) (n₁ n₂) (h₁ : n₁ < list.length l . tactic.exact_dec_trivial) (h₂ : n₂ < list.length l . tactic.exact_dec_trivial) : list.nth_le l n₁ h₁ ↔ list.nth_le l n₂ h₂ := h _ (list.nth_le_mem _ _ _) _ (list.nth_le_mem _ _ _) end tfae lemma rotate_mod (l : list α) (n : ℕ) : l.rotate (n % l.length) = l.rotate n := by simp [rotate] @[simp] lemma rotate_nil (n : ℕ) : ([] : list α).rotate n = [] := by cases n; refl @[simp] lemma rotate_zero (l : list α) : l.rotate 0 = l := by simp [rotate] @[simp] lemma rotate'_nil (n : ℕ) : ([] : list α).rotate' n = [] := by cases n; refl @[simp] lemma rotate'_zero (l : list α) : l.rotate' 0 = l := by cases l; refl lemma rotate'_cons_succ (l : list α) (a : α) (n : ℕ) : (a :: l : list α).rotate' n.succ = (l ++ [a]).rotate' n := by simp [rotate'] @[simp] lemma length_rotate' : ∀ (l : list α) (n : ℕ), (l.rotate' n).length = l.length | [] n := rfl | (a::l) 0 := rfl | (a::l) (n+1) := by rw [list.rotate', length_rotate' (l ++ [a]) n]; simp lemma rotate'_eq_take_append_drop : ∀ {l : list α} {n : ℕ}, n ≤ l.length → l.rotate' n = l.drop n ++ l.take n | [] n h := by simp [drop_append_of_le_length h] | l 0 h := by simp [take_append_of_le_length h] | (a::l) (n+1) h := have hnl : n ≤ l.length, from le_of_succ_le_succ h, have hnl' : n ≤ (l ++ [a]).length, by rw [length_append, length_cons, list.length, zero_add]; exact (le_of_succ_le h), by rw [rotate'_cons_succ, rotate'_eq_take_append_drop hnl', drop, take, drop_append_of_le_length hnl, take_append_of_le_length hnl]; simp lemma rotate'_rotate' : ∀ (l : list α) (n m : ℕ), (l.rotate' n).rotate' m = l.rotate' (n + m) | (a::l) 0 m := by simp | [] n m := by simp | (a::l) (n+1) m := by rw [rotate'_cons_succ, rotate'_rotate', add_right_comm, rotate'_cons_succ] @[simp] lemma rotate'_length (l : list α) : rotate' l l.length = l := by rw rotate'_eq_take_append_drop (le_refl _); simp @[simp] lemma rotate'_length_mul (l : list α) : ∀ n : ℕ, l.rotate' (l.length * n) = l | 0 := by simp | (n+1) := calc l.rotate' (l.length * (n + 1)) = (l.rotate' (l.length * n)).rotate' (l.rotate' (l.length * n)).length : by simp [-rotate'_length, nat.mul_succ, rotate'_rotate'] ... = l : by rw [rotate'_length, rotate'_length_mul] lemma rotate'_mod (l : list α) (n : ℕ) : l.rotate' (n % l.length) = l.rotate' n := calc l.rotate' (n % l.length) = (l.rotate' (n % l.length)).rotate' ((l.rotate' (n % l.length)).length * (n / l.length)) : by rw rotate'_length_mul ... = l.rotate' n : by rw [rotate'_rotate', length_rotate', nat.mod_add_div] lemma rotate_eq_rotate' (l : list α) (n : ℕ) : l.rotate n = l.rotate' n := if h : l.length = 0 then by simp [length_eq_zero, *] at * else by rw [← rotate'_mod, rotate'_eq_take_append_drop (le_of_lt (nat.mod_lt _ (nat.pos_of_ne_zero h)))]; simp [rotate] lemma rotate_cons_succ (l : list α) (a : α) (n : ℕ) : (a :: l : list α).rotate n.succ = (l ++ [a]).rotate n := by rw [rotate_eq_rotate', rotate_eq_rotate', rotate'_cons_succ] @[simp] lemma length_rotate (l : list α) (n : ℕ) : (l.rotate n).length = l.length := by rw [rotate_eq_rotate', length_rotate'] lemma rotate_eq_take_append_drop {l : list α} {n : ℕ} : n ≤ l.length → l.rotate n = l.drop n ++ l.take n := by rw rotate_eq_rotate'; exact rotate'_eq_take_append_drop lemma rotate_rotate (l : list α) (n m : ℕ) : (l.rotate n).rotate m = l.rotate (n + m) := by rw [rotate_eq_rotate', rotate_eq_rotate', rotate_eq_rotate', rotate'_rotate'] @[simp] lemma rotate_length (l : list α) : rotate l l.length = l := by rw [rotate_eq_rotate', rotate'_length] @[simp] lemma rotate_length_mul (l : list α) (n : ℕ) : l.rotate (l.length * n) = l := by rw [rotate_eq_rotate', rotate'_length_mul] lemma prod_rotate_eq_one_of_prod_eq_one [group α] : ∀ {l : list α} (hl : l.prod = 1) (n : ℕ), (l.rotate n).prod = 1 | [] _ _ := by simp | (a::l) hl n := have n % list.length (a :: l) ≤ list.length (a :: l), from le_of_lt (nat.mod_lt _ dec_trivial), by rw ← list.take_append_drop (n % list.length (a :: l)) (a :: l) at hl; rw [← rotate_mod, rotate_eq_take_append_drop this, list.prod_append, mul_eq_one_iff_inv_eq, ← one_mul (list.prod _)⁻¹, ← hl, list.prod_append, mul_assoc, mul_inv_self, mul_one] section choose variables (p : α → Prop) [decidable_pred p] (l : list α) lemma choose_spec (hp : ∃ a, a ∈ l ∧ p a) : choose p l hp ∈ l ∧ p (choose p l hp) := (choose_x p l hp).property lemma choose_mem (hp : ∃ a, a ∈ l ∧ p a) : choose p l hp ∈ l := (choose_spec _ _ _).1 lemma choose_property (hp : ∃ a, a ∈ l ∧ p a) : p (choose p l hp) := (choose_spec _ _ _).2 end choose end list theorem option.to_list_nodup {α} : ∀ o : option α, o.to_list.nodup | none := list.nodup_nil | (some x) := list.nodup_singleton x
e6993405dcf1129dfe21046a72f9aa3bf929f81c
86f6f4f8d827a196a32bfc646234b73328aeb306
/examples/logic/unnamed_612.lean
4ec4f743c28c51e5961a00e111b8051a18e97643
[]
no_license
jamescheuk91/mathematics_in_lean
09f1f87d2b0dce53464ff0cbe592c568ff59cf5e
4452499264e2975bca2f42565c0925506ba5dda3
refs/heads/master
1,679,716,410,967
1,613,957,947,000
1,613,957,947,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
183
lean
import data.real.basic -- BEGIN example : ∃ x : ℝ, 2 < x ∧ x < 3 := begin have h : 2 < (5 : ℝ) / 2 ∧ (5 : ℝ) / 2 < 3, by norm_num, exact ⟨5 / 2, h⟩ end -- END
a8a7db06b75c52351c080b186cbfa4485c2a135f
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/linear_algebra/free_algebra.lean
846d10b1f393c4e7f1bed44c4be50b8f51ef84a1
[ "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
1,275
lean
/- Copyright (c) 2021 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser -/ import linear_algebra.basis import algebra.free_algebra import linear_algebra.dimension import linear_algebra.finsupp_vector_space /-! # Linear algebra properties of `free_algebra R X` > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file provides a `free_monoid X` basis on the `free_algebra R X`, and uses it to show the dimension of the algebra is the cardinality of `list X` -/ universes u v namespace free_algebra /-- The `free_monoid X` basis on the `free_algebra R X`, mapping `[x₁, x₂, ..., xₙ]` to the "monomial" `1 • x₁ * x₂ * ⋯ * xₙ` -/ @[simps] noncomputable def basis_free_monoid (R : Type u) (X : Type v) [comm_ring R] : basis (free_monoid X) R (free_algebra R X) := finsupp.basis_single_one.map (equiv_monoid_algebra_free_monoid.symm.to_linear_equiv : _ ≃ₗ[R] free_algebra R X) -- TODO: generalize to `X : Type v` lemma rank_eq {K : Type u} {X : Type (max u v)} [field K] : module.rank K (free_algebra K X) = cardinal.mk (list X) := (cardinal.lift_inj.mp (basis_free_monoid K X).mk_eq_rank).symm end free_algebra
f3893d4de81723f3988b34fa53d561a16cde95cf
618003631150032a5676f229d13a079ac875ff77
/src/algebra/group/basic.lean
4927cc79a4a0be405acfe8836a869632ff7657e9
[ "Apache-2.0" ]
permissive
awainverse/mathlib
939b68c8486df66cfda64d327ad3d9165248c777
ea76bd8f3ca0a8bf0a166a06a475b10663dec44a
refs/heads/master
1,659,592,962,036
1,590,987,592,000
1,590,987,592,000
268,436,019
1
0
Apache-2.0
1,590,990,500,000
1,590,990,500,000
null
UTF-8
Lean
false
false
12,844
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'] @[to_additive] theorem inv_comm_of_comm (H : a * b = b * a) : a⁻¹ * b = b * a⁻¹ := begin have : a⁻¹ * (b * a) * a⁻¹ = a⁻¹ * (a * b) * a⁻¹ := congr_arg (λ x:G, a⁻¹ * x * a⁻¹) H.symm, rwa [inv_mul_cancel_left, mul_assoc, mul_inv_cancel_right] at this end @[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
888ccae7afa899a3658ac327e499cd7f67d2ac2f
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/control/bifunctor.lean
fae259c06f95a64fef01e7cab2a1a4749e5624d8
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
5,168
lean
/- Copyright (c) 2018 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon -/ import control.functor import data.sum.basic /-! # Functors with two arguments > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines bifunctors. A bifunctor is a function `F : Type* → Type* → Type*` along with a bimap which turns `F α β` into `F α' β'` given two functions `α → α'` and `β → β'`. It further * respects the identity: `bimap id id = id` * composes in the obvious way: `(bimap f' g') ∘ (bimap f g) = bimap (f' ∘ f) (g' ∘ g)` ## Main declarations * `bifunctor`: A typeclass for the bare bimap of a bifunctor. * `is_lawful_bifunctor`: A typeclass asserting this bimap respects the bifunctor laws. -/ universes u₀ u₁ u₂ v₀ v₁ v₂ open function /-- Lawless bifunctor. This typeclass only holds the data for the bimap. -/ class bifunctor (F : Type u₀ → Type u₁ → Type u₂) := (bimap : Π {α α' β β'}, (α → α') → (β → β') → F α β → F α' β') export bifunctor ( bimap ) /-- Bifunctor. This typeclass asserts that a lawless `bifunctor` is lawful. -/ class is_lawful_bifunctor (F : Type u₀ → Type u₁ → Type u₂) [bifunctor F] := (id_bimap : Π {α β} (x : F α β), bimap id id x = x) (bimap_bimap : Π {α₀ α₁ α₂ β₀ β₁ β₂} (f : α₀ → α₁) (f' : α₁ → α₂) (g : β₀ → β₁) (g' : β₁ → β₂) (x : F α₀ β₀), bimap f' g' (bimap f g x) = bimap (f' ∘ f) (g' ∘ g) x) export is_lawful_bifunctor (id_bimap bimap_bimap) attribute [higher_order bimap_id_id] id_bimap attribute [higher_order bimap_comp_bimap] bimap_bimap export is_lawful_bifunctor (bimap_id_id bimap_comp_bimap) variables {F : Type u₀ → Type u₁ → Type u₂} [bifunctor F] namespace bifunctor /-- Left map of a bifunctor. -/ @[reducible] def fst {α α' β} (f : α → α') : F α β → F α' β := bimap f id /-- Right map of a bifunctor. -/ @[reducible] def snd {α β β'} (f : β → β') : F α β → F α β' := bimap id f variable [is_lawful_bifunctor F] @[higher_order fst_id] lemma id_fst : Π {α β} (x : F α β), fst id x = x := @id_bimap _ _ _ @[higher_order snd_id] lemma id_snd : Π {α β} (x : F α β), snd id x = x := @id_bimap _ _ _ @[higher_order fst_comp_fst] lemma comp_fst {α₀ α₁ α₂ β} (f : α₀ → α₁) (f' : α₁ → α₂) (x : F α₀ β) : fst f' (fst f x) = fst (f' ∘ f) x := by simp [fst,bimap_bimap] @[higher_order fst_comp_snd] lemma fst_snd {α₀ α₁ β₀ β₁} (f : α₀ → α₁) (f' : β₀ → β₁) (x : F α₀ β₀) : fst f (snd f' x) = bimap f f' x := by simp [fst,bimap_bimap] @[higher_order snd_comp_fst] lemma snd_fst {α₀ α₁ β₀ β₁} (f : α₀ → α₁) (f' : β₀ → β₁) (x : F α₀ β₀) : snd f' (fst f x) = bimap f f' x := by simp [snd,bimap_bimap] @[higher_order snd_comp_snd] lemma comp_snd {α β₀ β₁ β₂} (g : β₀ → β₁) (g' : β₁ → β₂) (x : F α β₀) : snd g' (snd g x) = snd (g' ∘ g) x := by simp [snd,bimap_bimap] attribute [functor_norm] bimap_bimap comp_snd comp_fst snd_comp_snd snd_comp_fst fst_comp_snd fst_comp_fst bimap_comp_bimap bimap_id_id fst_id snd_id end bifunctor open functor instance : bifunctor prod := { bimap := @prod.map } instance : is_lawful_bifunctor prod := by refine { .. }; intros; cases x; refl instance bifunctor.const : bifunctor const := { bimap := (λ α α' β β f _, f) } instance is_lawful_bifunctor.const : is_lawful_bifunctor const := by refine { .. }; intros; refl instance bifunctor.flip : bifunctor (flip F) := { bimap := (λ α α' β β' f f' x, (bimap f' f x : F β' α')) } instance is_lawful_bifunctor.flip [is_lawful_bifunctor F] : is_lawful_bifunctor (flip F) := by refine { .. }; intros; simp [bimap] with functor_norm instance : bifunctor sum := { bimap := @sum.map } instance : is_lawful_bifunctor sum := by refine { .. }; intros; cases x; refl open bifunctor functor @[priority 10] instance bifunctor.functor {α} : functor (F α) := { map := λ _ _, snd } @[priority 10] instance bifunctor.is_lawful_functor [is_lawful_bifunctor F] {α} : is_lawful_functor (F α) := by refine {..}; intros; simp [functor.map] with functor_norm section bicompl variables (G : Type* → Type u₀) (H : Type* → Type u₁) [functor G] [functor H] instance : bifunctor (bicompl F G H) := { bimap := λ α α' β β' f f' x, (bimap (map f) (map f') x : F (G α') (H β')) } instance [is_lawful_functor G] [is_lawful_functor H] [is_lawful_bifunctor F] : is_lawful_bifunctor (bicompl F G H) := by constructor; intros; simp [bimap,map_id,map_comp_map] with functor_norm end bicompl section bicompr variables (G : Type u₂ → Type*) [functor G] instance : bifunctor (bicompr G F) := { bimap := λ α α' β β' f f' x, (map (bimap f f') x : G (F α' β')) } instance [is_lawful_functor G] [is_lawful_bifunctor F] : is_lawful_bifunctor (bicompr G F) := by constructor; intros; simp [bimap] with functor_norm end bicompr
65ba3f540c5f3fca3e6c963dd259973677d5aea6
32025d5c2d6e33ad3b6dd8a3c91e1e838066a7f7
/stage0/src/Lean/Elab/PreDefinition/Structural.lean
7bdc2cdb2c3edb3688b68e0a66d43f0840350df2
[ "Apache-2.0" ]
permissive
walterhu1015/lean4
b2c71b688975177402758924eaa513475ed6ce72
2214d81e84646a905d0b20b032c89caf89c737ad
refs/heads/master
1,671,342,096,906
1,599,695,985,000
1,599,695,985,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
6,569
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Util.ForEachExpr import Lean.Meta.RecursorInfo import Lean.Elab.PreDefinition.Basic namespace Lean namespace Elab open Meta private def getFixedPrefix (declName : Name) (xs : Array Expr) (value : Expr) : Nat := let visitor {ω} : StateRefT Nat (ST ω) Unit := value.forEach' fun e => if e.isAppOf declName then do let args := e.getAppArgs; modify fun numFixed => if args.size < numFixed then args.size else numFixed; -- we continue searching if the e's arguments are not a prefix of `xs` pure !args.isPrefixOf xs else pure true; runST fun _ => do (_, numFixed) ← visitor.run xs.size; pure numFixed structure RecArgInfo := /- `fixedParams ++ ys` are the arguments of the function we are trying to justify termination using structural recursion. -/ (fixedParams : Array Expr) (ys : Array Expr) -- recursion arguments (pos : Nat) -- position in `ys` of the argument we are recursing on (indicesPos : Array Nat) -- position in `ys` of the inductive datatype indices we are recursing on (reflexive : Bool) -- true if we are recursing over a reflexive inductive datatype private def getIndexMinPos (xs : Array Expr) (indices : Array Expr) : Nat := indices.foldl (fun minPos index => match xs.indexOf index with | some pos => if pos.val < minPos then pos.val else minPos | _ => minPos) xs.size -- Indices can only depend on other indices private def hasBadIndexDep? (ys : Array Expr) (indices : Array Expr) : TermElabM (Option (Expr × Expr)) := indices.findSomeM? fun index => do indexType ← inferType index; ys.findSomeM? fun y => if indices.contains y then pure none else condM (dependsOn indexType y.fvarId!) (pure (some (index, y))) (pure none) -- Inductive datatype parameters cannot depend on ys private def hasBadParamDep? (ys : Array Expr) (indParams : Array Expr) : TermElabM (Option (Expr × Expr)) := indParams.findSomeM? fun p => do pType ← inferType p; ys.findSomeM? fun y => condM (dependsOn pType y.fvarId!) (pure (some (p, y))) (pure none) private partial def findRecArgAux? {α} (numFixed : Nat) (xs : Array Expr) (k? : RecArgInfo → TermElabM (Option α)) : Nat → TermElabM (Option α) | i => if h : i < xs.size then do let x := xs.get ⟨i, h⟩; localDecl ← getFVarLocalDecl x; if localDecl.isLet then pure none else do xType ← whnfD localDecl.type; matchConstInduct xType.getAppFn (fun _ => findRecArgAux? (i+1)) fun indInfo us => do condM (not <$> hasConst (mkBRecOnFor indInfo.name)) (findRecArgAux? (i+1)) do condM (do hasBInductionOn ← hasConst (mkBInductionOnFor indInfo.name); pure $ indInfo.isReflexive && !hasBInductionOn) (findRecArgAux? (i+1)) do let indArgs := xType.getAppArgs; let indParams := indArgs.extract 0 indInfo.nparams; let indIndices := indArgs.extract indInfo.nparams indArgs.size; if !indIndices.all Expr.isFVar then do trace `Elab.definition.structural fun _ => "argument #" ++ toString (i+1) ++ " was not used because its type is an inductive family and indices are not variables" ++ indentExpr xType; findRecArgAux? (i+1) else if !indIndices.allDiff then do trace `Elab.definition.structural fun _ => "argument #" ++ toString (i+1) ++ " was not used because its type is an inductive family and indices are not pairwise distinct" ++ indentExpr xType; findRecArgAux? (i+1) else do let indexMinPos := getIndexMinPos xs indIndices; let numFixed := if indexMinPos < numFixed then indexMinPos else numFixed; let fixedParams := xs.extract 0 numFixed; let ys := xs.extract numFixed xs.size; badDep? ← hasBadIndexDep? ys indIndices; match badDep? with | some (index, y) => do trace `Elab.definition.structural fun _ => "argument #" ++ toString (i+1) ++ " was not used because its type is an inductive family" ++ indentExpr xType ++ Format.line ++ "and index" ++ indentExpr index ++ Format.line ++ "depends on the non index" ++ indentExpr y; findRecArgAux? (i+1) | none => do badDep? ← hasBadParamDep? ys indParams; match badDep? with | some (indParam, y) => do trace `Elab.definition.structural fun _ => "argument #" ++ toString (i+1) ++ " was not used because its type is an inductive datatype" ++ indentExpr xType ++ Format.line ++ "and parameter" ++ indentExpr indParam ++ Format.line ++ "depends on" ++ indentExpr y; findRecArgAux? (i+1) | none => do let indicesPos := indIndices.map fun index => match ys.indexOf index with | some i => i.val | none => unreachable!; a? ← k? { fixedParams := fixedParams, ys := ys, pos := i - fixedParams.size, indicesPos := indicesPos, reflexive := indInfo.isReflexive }; match a? with | some a => pure a | none => findRecArgAux? (i+1) else pure none @[inline] private def findRecArg? {α} (numFixed : Nat) (xs : Array Expr) (k? : RecArgInfo → TermElabM (Option α)) : TermElabM (Option α) := findRecArgAux? numFixed xs k? numFixed private def elimRecursion? (preDef : PreDefinition) : TermElabM (Option PreDefinition) := lambdaLetTelescope preDef.value fun xs value => do trace `Elab.definition.structural fun _ => preDef.declName ++ " " ++ xs ++ " :=\n" ++ value; let numFixed := getFixedPrefix preDef.declName xs value; findRecArg? numFixed xs fun argInfo => do -- TODO trace `Elab.definition.structural fun _ => "try " ++ argInfo.fixedParams ++ " " ++ argInfo.ys ++ " " ++ toString argInfo.pos ++ ", " ++ toString argInfo.indicesPos; pure none def structuralRecursion (preDefs : Array PreDefinition) : TermElabM Bool := if preDefs.size != 1 then pure false else do preDefNonRec? ← elimRecursion? (preDefs.get! 0); match preDefNonRec? with | none => pure false | some preDefNonRec => do addNonRec preDefNonRec; addAndCompileUnsafeRec preDefs; pure true @[init] private def regTraceClasses : IO Unit := do registerTraceClass `Elab.definition.structural; pure () end Elab end Lean
6ed63fb3ef3a514073d66da67258b2cae8aaa504
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/unboxStruct.lean
df57b1d66d26746bb57db5241980e44ff6c82ec5
[ "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
239
lean
structure AddrSpace where index : UInt32 @[extern "foo"] opaque foo (addrSpace : AddrSpace) : IO PUnit set_option trace.compiler.ir.result true in -- should accept and pass an unboxed `uint32` def test2 : AddrSpace → IO PUnit := foo
2083db0a0a3f975cc5f10911e3ed4bc0098a8092
fbf512ee44de430e3d3c5869751ad95325c938d7
/logic.lean
9914f4a0a3f4227fc7df36a1cd836c2cd6cf4ead
[]
no_license
skbaek/clausify
4858c9005fb86a5e410bfcaa77524f82d955f655
d09b071bdcce7577c3fffacd0893b776285b1590
refs/heads/master
1,588,360,590,818
1,553,553,880,000
1,553,553,880,000
177,644,542
0
0
null
null
null
null
UTF-8
Lean
false
false
2,953
lean
import logic.basic variable {α : Type} lemma forall_iff_forall {P Q : α → Prop} : (∀ a, P a ↔ Q a) → ((∀ a, P a) ↔ (∀ a, Q a)) := λ h, iff.intro (λ hP a, (h a).elim_left (hP _)) (λ hQ a, (h a).elim_right (hQ _)) lemma exists_iff_exists {P Q : α → Prop} : (∀ a, P a ↔ Q a) → ((∃ a, P a) ↔ (∃ a, Q a)) := λ h, iff.intro (λ hP, begin cases hP with a ha, existsi a, apply (h a).elim_left ha end) (λ hQ, begin cases hQ with a ha, existsi a, apply (h a).elim_right ha end) lemma or_iff_or {p p' q q' : Prop} : (p ↔ p') → (q ↔ q') → ((p ∨ q) ↔ (p' ∨ q')) := begin intros hp hq, rewrite hp, rewrite hq end lemma and_iff_and {p p' q q' : Prop} : (p ↔ p') → (q ↔ q') → ((p ∧ q) ↔ (p' ∧ q')) := begin intros hp hq, rewrite hp, rewrite hq end lemma imp_of_imp (p) {q} : (p → q) → (p → q) := id #exit lemma not_true.elim : Π {C : Sort u}, ¬ true → C := λ _ h, false.elim (h trivial) lemma not_and.elim : ∀ {a b c : Prop}, ¬(a ∧ b) → (¬a → c) → (¬b → c) → c := begin intros a b c h1 h2 h3, cases (@classical.or_not a) with ha ha, apply (h3 $ λ hb, h1 ⟨ha,hb⟩), apply h2 ha end lemma not_or.elim : ∀ {a b c : Prop}, ¬(a ∨ b) → (¬a → ¬b → c) → c := begin intros a b c h1 h2, rw not_or_distrib at h1, cases h1, apply h2; assumption end lemma imp.elim : ∀ {a b c : Prop}, (a → b) → (¬a → c) → (b → c) → c := begin intros a b c h1 h2 h3, cases (@classical.or_not a) with ha ha, apply (h3 $ h1 ha), apply h2 ha end lemma not_imp.elim : ∀ {a b c : Prop}, ¬(a → b) → (a → ¬b → c) → c := begin intros a b c h1 h2, cases (@classical.or_not b) with hb hb, exfalso, apply (h1 $ λ _, hb), cases (@classical.or_not a) with ha ha, apply (h2 ha hb), exfalso, apply (h1 $ λ ha', false.elim $ ha ha'), end lemma not_forall.elim : ∀ {α : Sort u} {p : α → Prop} {b : Prop}, (¬ ∀ (x : α), p x) → (∀ (a : α), (¬ p a) → b) → b := begin intros α p b h1 h2, rw classical.not_forall at h1, cases h1 with a ha, apply h2 _ ha end variables {p q : Prop} lemma not_or_not_of_not_and : ¬ (p ∧ q) → (¬ p ∨ ¬ q) := λ h, @classical.by_cases p (¬ p ∨ ¬ q) (λ hp, or.inr (λ hq, h $ and.intro hp hq)) (λ hnp, or.inl hnp) lemma not_or_left : ¬ (p ∨ q) → ¬ p := λ h, (not_or_distrib.elim_left h).left lemma not_or_right : ¬ (p ∨ q) → ¬ q := λ h, (not_or_distrib.elim_left h).right lemma not_or_of_imp_cls : (p → q) → ¬p ∨ q := @not_or_of_imp p q (classical.dec p) lemma not_imp_left : ¬ (p → q) → p := λ h, ((@not_imp _ _ (classical.dec _)).elim_left h).left lemma not_imp_right : ¬ (p → q) → ¬ q := λ h, ((@not_imp _ _ (classical.dec _)).elim_left h).right lemma iff_of_left_of_right : p → q → (p ↔ q) := λ hp hq, iff.intro (λ _, hq) (λ _, hp) variables {α : Type}
e60cca55277f2679935c6ab63413750c51b5d01c
5c7fe6c4a9d4079b5457ffa5f061797d42a1cd65
/src/exercises/src_37_completeness_axiom.lean
e59dd778c5d6f3a849d2041a0801e11d1caa9b25
[]
no_license
gihanmarasingha/mth1001_tutorial
8e0817feeb96e7c1bb3bac49b63e3c9a3a329061
bb277eebd5013766e1418365b91416b406275130
refs/heads/master
1,675,008,746,310
1,607,993,443,000
1,607,993,443,000
321,511,270
3
0
null
null
null
null
UTF-8
Lean
false
false
2,980
lean
import ..library.src_real_field import ..library.src_ordered_field_lemmas import data.set.basic import tactic namespace mth1001 namespace myreal open myreal_field classical myordered_field open_locale classical variables {R : Type} [myreal_field R] def has_upper_bound (S : set R) := ∃ u : R, upper_bound u S lemma sup_is_sup {S : set R} (h₁ : has_upper_bound S) (h₂ : S ≠ ∅) : is_sup (sup S) S := begin have h₃ : ∃ x : R, is_sup x S, from completeness h₂ h₁, have h₄ : sup S = some h₃, from dif_pos h₃, rw h₄, exact some_spec h₃, end -- Exercise 214: theorem sup_monotone (S T : set R) (h₁ : has_upper_bound S) (h₂ : has_upper_bound T) (h₃ : S ≠ ∅) (h₄ : T ≠ ∅) : S ⊆ T → sup S ≤ sup T := begin have h₅ : ∀ (v : R), upper_bound v S → sup S ≤ v, from (sup_is_sup h₁ h₃).right, have h₆ : upper_bound (sup T) T, from (sup_is_sup h₂ h₄).left, intro k, sorry end -- Exercise 215: -- The exercise below is rather challenging. theorem archimedean : ∀ x : R, ∃ n : ℕ, x < n := begin by_contra h, push_neg at h, cases h with x hx, let S := {m : R | ∃ n : ℕ, (m = n) ∧ (x ≥ n)}, have h₁ : ↑1 ∈ S, from ⟨1, rfl, hx 1⟩, have h₂ : S ≠ ∅, { intro h, rw h at h₁, exact h₁, }, have h₃ : has_upper_bound S, { use x, rintros _ ⟨_, rfl, xgen⟩, exact xgen, }, have h₄ : is_sup (sup S) S, from sup_is_sup h₃ h₂, sorry end -- Exercise 216: -- Use `archimedean` and other results (such as `inv_pos` and `inv_lt_inv`) to prove: theorem inv_lt_of_pos (ε : R) (h : 0 < ε) : ∃ n : ℕ, n ≠ 0 ∧ (↑n)⁻¹ < ε := begin sorry end -- Exercise 217: -- If a non-negative number is smaller than every positive number, it must be zero. -- This *can* be proved without the completeness axiom, but the proof is more straightforward -- (in my opinion) using the completeness axiom. lemma zero_of_non_neg_of_lt_pos (a : R) (h : 0 ≤ a) (h₂ : ∀ ε > 0, a < ε) : a = 0 := begin sorry end -- Exercise 218: theorem bounded_iff_abs_lt (S : set R) : bounded S ↔ ∃ m : ℕ, ∀ s ∈ S, abs s < ↑m := begin split, { rintro ⟨⟨ub, hub⟩, lb, hlb⟩, rcases archimedean (max (abs ub) (abs lb)) with ⟨n, hn⟩, use n, intros s hs, unfold abs, cases max_choice s (-s) with hms hmns, { sorry, }, { rw hmns, have h₂ : -s ≤ -lb, from neg_le_neg_iff.mpr (hlb s hs), have h₃ : -lb ≤ abs lb, from neg_le_abs lb, have h₄ : abs lb ≤ max (abs ub) (abs lb), from le_max_right _ _, apply lt_of_le_of_lt, { exact le_trans _ _ _ h₂ (le_trans _ _ _ h₃ h₄) }, { exact hn, }, }, }, { rintro ⟨m, hm⟩, split, { sorry, }, { use (-m), intros s hs, rw [←neg_le_neg_iff, neg_neg], apply le_trans, { exact neg_le_abs s, }, { exact le_iff_lt_or_eq.mpr (or.inl (hm s hs)), }, }, }, end end myreal end mth1001
78c9e655f51219d3c44c711c48e04a46f4b5bca7
35677d2df3f081738fa6b08138e03ee36bc33cad
/test/coinductive.lean
36bf92a01b7c8d152da579ec089bd47bec412807
[ "Apache-2.0" ]
permissive
gebner/mathlib
eab0150cc4f79ec45d2016a8c21750244a2e7ff0
cc6a6edc397c55118df62831e23bfbd6e6c6b4ab
refs/heads/master
1,625,574,853,976
1,586,712,827,000
1,586,712,827,000
99,101,412
1
0
Apache-2.0
1,586,716,389,000
1,501,667,958,000
Lean
UTF-8
Lean
false
false
5,278
lean
/- test cases for coinductive predicates -/ import data.stream meta.coinductive_predicates universe u coinductive all_stream {α : Type u} (s : set α) : stream α → Prop | step : ∀{a : α} {ω : stream α}, a ∈ s → all_stream ω → all_stream (a :: ω) example : Π {α : Type u}, set α → stream α → Prop := @all_stream example : ∀ {α : Type u} {s : set α} {a : α} {ω : stream α}, a ∈ s → all_stream s ω → all_stream s (a :: ω) := @all_stream.step example : ∀ {α : Type u} (s : set α) {a : stream α}, all_stream s a → all_stream.functional s (all_stream s) a := @all_stream.destruct example : ∀ {α : Type u} (s : set α) (C : stream α → Prop) {a : stream α}, C a → (∀ (a : stream α), C a → (∃ {a_1 : α} {ω : stream α}, a_1 ∈ s ∧ C ω ∧ a_1 :: ω = a)) → all_stream s a := @all_stream.corec_on example : ∀ {α : Type u} (s : set α) (C : stream α → Prop), (∀ (a : stream α), C a → all_stream.functional s C a) → ∀ (a : stream α), C a → all_stream s a := @all_stream.corec_functional coinductive all_stream' {α : Type u} (s : set α) : stream α → Prop | step : ∀{ω : stream α}, stream.head ω ∈ s → all_stream' (stream.tail ω) → all_stream' ω coinductive alt_stream : stream bool → Prop | tt_step : ∀{ω : stream bool}, alt_stream (ff :: ω) → alt_stream (tt :: ff :: ω) | ff_step : ∀{ω : stream bool}, alt_stream (tt :: ω) → alt_stream (ff :: tt :: ω) example : stream bool → Prop := @alt_stream example : ∀ {ω : stream bool}, alt_stream (ff :: ω) → alt_stream (tt :: ff :: ω) := @alt_stream.tt_step example : ∀ {ω : stream bool}, alt_stream (tt :: ω) → alt_stream (ff :: tt :: ω) := @alt_stream.ff_step example : ∀ (C : stream bool → Prop), (∀ (a : stream bool), C a → alt_stream.functional C a) → ∀ (a : stream bool), C a → alt_stream a := @alt_stream.corec_functional mutual coinductive tt_stream, ff_stream with tt_stream : stream bool → Prop | step : ∀{ω : stream bool}, ff_stream ω → tt_stream (stream.cons tt ω) with ff_stream : stream bool → Prop | step : ∀{ω : stream bool}, tt_stream ω → ff_stream (stream.cons ff ω) example : stream bool → Prop := @tt_stream example : stream bool → Prop := @ff_stream example : ∀ (C_tt_stream C_ff_stream : stream bool → Prop), (∀ (a : stream bool), C_tt_stream a → tt_stream.functional C_tt_stream C_ff_stream a) → (∀ (a : stream bool), C_ff_stream a → ff_stream.functional C_tt_stream C_ff_stream a) → ∀ (a : stream bool), C_tt_stream a → tt_stream a := @tt_stream.corec_functional example : ∀ (C_tt_stream C_ff_stream : stream bool → Prop), (∀ (a : stream bool), C_tt_stream a → tt_stream.functional C_tt_stream C_ff_stream a) → (∀ (a : stream bool), C_ff_stream a → ff_stream.functional C_tt_stream C_ff_stream a) → ∀ (a : stream bool), C_ff_stream a → ff_stream a := @ff_stream.corec_functional mutual coinductive tt_ff_stream, ff_tt_stream with tt_ff_stream : stream bool → Prop | step : ∀{ω : stream bool}, tt_ff_stream ω ∨ ff_tt_stream ω → tt_ff_stream (stream.cons tt ω) with ff_tt_stream : stream bool → Prop | step : ∀{ω : stream bool}, ff_tt_stream ω ∨ tt_ff_stream ω → ff_tt_stream (stream.cons ff ω) inductive all_list {α : Type} (p : α → Prop) : list α → Prop | nil : all_list [] | cons : ∀a xs, p a → all_list xs → all_list (a :: xs) @[monotonicity] lemma monotonicity.all_list {α : Type} {p q : α → Prop} (h : ∀a, implies (p a) (q a)) : ∀xs, implies (all_list p xs) (all_list q xs) | _ (all_list.nil) := all_list.nil | _ (all_list.cons a xs ha hxs) := all_list.cons _ _ (h a ha) (monotonicity.all_list _ hxs) mutual coinductive walk_a, walk_b {α β : Type} (f : α → list β) (g : β → α) (p : α → Prop) (t : α → Prop) with walk_a : α → Prop | step : ∀a, all_list walk_b (f a) → p a → walk_a a | term : ∀a, t a → walk_a a with walk_b : β → Prop | step : ∀b, walk_a (g b) → walk_b b example : ∀ {α β : Type} (f : α → list β) (g : β → α) (p t C_walk_a : α → Prop) (C_walk_b : β → Prop) {a : α}, C_walk_a a → (∀ (a : α), C_walk_a a → all_list C_walk_b (f a) ∧ p a ∨ t a) → (∀ (a : β), C_walk_b a → C_walk_a (g a)) → walk_a f g p t a := @walk_a.corec_on coinductive walk_list {α : Type} (f : α → list α) (p : α → Prop) : ℕ → α → Prop | step : ∀n a, all_list (walk_list n) (f a) → p a → walk_list (n + 1) a example {f : ℕ → list ℕ} {a' : ℕ} {n : ℕ} {a : fin n} : true := begin suffices : walk_list f (λ a'', a'' = a') (n + 1) a', {trivial}, coinduction walk_list.corec_on generalizing a n, show ∃ (n : ℕ), all_list (λ (a : ℕ), ∃ {n_1 : ℕ} {a_1 : fin n_1}, n_1 + 1 = n ∧ a' = a) (f a') ∧ a' = a' ∧ n + 1 = w + 1, admit end coinductive coind_foo : list ℕ → Prop | mk : ∀ xs, (∀ k l m, coind_foo (k::l::m::xs)) → coind_foo xs meta example : true := begin success_if_fail { let := compact_relation }, trivial end import_private compact_relation from tactic.coinduction meta example : true := begin let := compact_relation, trivial end
bf7fd77cc7b771c43ecf93e3ef1d196bc7364254
31f556cdeb9239ffc2fad8f905e33987ff4feab9
/stage0/src/Lean/Compiler/LCNF/ReduceJpArity.lean
1d1f06a4fad6c9bdb9042443c516daaadf2e7810
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
tobiasgrosser/lean4
ce0fd9cca0feba1100656679bf41f0bffdbabb71
ebdbdc10436a4d9d6b66acf78aae7a23f5bd073f
refs/heads/master
1,673,103,412,948
1,664,930,501,000
1,664,930,501,000
186,870,185
0
0
Apache-2.0
1,665,129,237,000
1,557,939,901,000
Lean
UTF-8
Lean
false
false
2,408
lean
/- Copyright (c) 2022 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Compiler.LCNF.CompilerM import Lean.Compiler.LCNF.InferType import Lean.Compiler.LCNF.PassManager namespace Lean.Compiler.LCNF /-! Join point arity reduction. -/ namespace ReduceJpArity abbrev ReduceM := ReaderT (FVarIdMap (Array Bool)) CompilerM partial def reduce (code : Code) : ReduceM Code := do match code with | .let decl k => return code.updateLet! decl (← reduce k) | .fun decl k => let value ← reduce decl.value let decl ← decl.update' decl.type value return code.updateFun! decl (← reduce k) | .jp decl k => let value ← reduce decl.value let mut used := value.collectUsed let mut mask := #[] let mut paramsNew := #[] for param in decl.params.reverse do if used.contains param.fvarId then used := collectUsedAtExpr used param.type mask := mask.push true paramsNew := paramsNew.push param else eraseParam param mask := mask.push false mask := mask.reverse paramsNew := paramsNew.reverse if paramsNew.size != decl.params.size then let type ← mkForallParams paramsNew (← value.inferType) let decl ← decl.update type paramsNew value let k ← withReader (·.insert decl.fvarId mask) (reduce k) return .jp decl k else let decl ← decl.update' decl.type value return code.updateFun! decl (← reduce k) | .cases c => let alts ← c.alts.mapMonoM fun alt => return alt.updateCode (← reduce alt.getCode) return code.updateAlts! alts | .return .. | .unreach .. => return code | .jmp fvarId args => if let some mask := (← read).find? fvarId then let mut argsNew := #[] for keep in mask, arg in args do if keep then argsNew := argsNew.push arg return .jmp fvarId argsNew else return code end ReduceJpArity open ReduceJpArity /-- Try to reduce arity of join points -/ def Decl.reduceJpArity (decl : Decl) : CompilerM Decl := do let value ← reduce decl.value |>.run {} return { decl with value } def reduceJpArity : Pass := .mkPerDeclaration `reduceJpArity Decl.reduceJpArity .base builtin_initialize registerTraceClass `Compiler.reduceJpArity (inherited := true) end Lean.Compiler.LCNF
5a3da63bdd72f94f8fc1747a1de8dcc2d5f4b99e
4fa161becb8ce7378a709f5992a594764699e268
/src/linear_algebra/direct_sum_module.lean
1da16eda156ba15b1129f9b00b1eac5628b0cc52
[ "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
3,544
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau Direct sum of modules over commutative rings, indexed by a discrete type. -/ import algebra.direct_sum import linear_algebra.basic universes u v w u₁ variables (R : Type u) [semiring R] variables (ι : Type v) [decidable_eq ι] (M : ι → Type w) variables [Π i, add_comm_group (M i)] [Π i, semimodule R (M i)] include R namespace direct_sum variables {R ι M} instance : semimodule R (direct_sum ι M) := dfinsupp.to_semimodule variables R ι M def lmk : Π s : finset ι, (Π i : (↑s : set ι), M i.1) →ₗ[R] direct_sum ι M := dfinsupp.lmk M R def lof : Π i : ι, M i →ₗ[R] direct_sum ι M := dfinsupp.lsingle M R variables {ι M} lemma single_eq_lof (i : ι) (b : M i) : dfinsupp.single i b = lof R ι M i b := rfl theorem mk_smul (s : finset ι) (c : R) (x) : mk M s (c • x) = c • mk M s x := (lmk R ι M s).map_smul c x theorem of_smul (i : ι) (c : R) (x) : of M i (c • x) = c • of M i x := (lof R ι M i).map_smul c x variables {N : Type u₁} [add_comm_group N] [semimodule R N] variables (φ : Π i, M i →ₗ[R] N) variables (ι N φ) def to_module : direct_sum ι M →ₗ[R] N := { to_fun := to_group (λ i, φ i), map_add' := to_group_add _, map_smul' := λ c x, direct_sum.induction_on x (by rw [smul_zero, to_group_zero, smul_zero]) (λ i x, by rw [← of_smul, to_group_of, to_group_of, (φ i).map_smul c x]) (λ x y ihx ihy, by rw [smul_add, to_group_add, ihx, ihy, to_group_add, smul_add]) } variables {ι N φ} @[simp] lemma to_module_lof (i) (x : M i) : to_module R ι N φ (lof R ι M i x) = φ i x := to_group_of (λ i, φ i) i x variables (ψ : direct_sum ι M →ₗ[R] N) theorem to_module.unique (f : direct_sum ι M) : ψ f = to_module R ι N (λ i, ψ.comp $ lof R ι M i) f := to_group.unique ψ f variables {ψ} {ψ' : direct_sum ι M →ₗ[R] N} theorem to_module.ext (H : ∀ i, ψ.comp (lof R ι M i) = ψ'.comp (lof R ι M i)) (f : direct_sum ι M) : ψ f = ψ' f := by rw [to_module.unique R ψ, to_module.unique R ψ', funext H] def lset_to_set (S T : set ι) (H : S ⊆ T) : direct_sum S (M ∘ subtype.val) →ₗ direct_sum T (M ∘ subtype.val) := to_module R _ _ $ λ i, lof R T (M ∘ @subtype.val _ T) ⟨i.1, H i.2⟩ protected def lid (M : Type v) [add_comm_group M] [semimodule R M] : direct_sum punit (λ _, M) ≃ₗ M := { .. direct_sum.id M, .. to_module R punit M (λ i, linear_map.id) } variables (ι M) def component (i : ι) : direct_sum ι M →ₗ[R] M i := { to_fun := λ f, f i, map_add' := λ _ _, dfinsupp.add_apply, map_smul' := λ _ _, dfinsupp.smul_apply } variables {ι M} @[simp] lemma lof_apply (i : ι) (b : M i) : ((lof R ι M i) b) i = b := by rw [lof, dfinsupp.lsingle_apply, dfinsupp.single_apply, dif_pos rfl] lemma apply_eq_component (f : direct_sum ι M) (i : ι) : f i = component R ι M i f := rfl @[simp] lemma component.lof_self (i : ι) (b : M i) : component R ι M i ((lof R ι M i) b) = b := lof_apply R i b lemma component.of (i j : ι) (b : M j) : component R ι M i ((lof R ι M j) b) = if h : j = i then eq.rec_on h b else 0 := dfinsupp.single_apply @[ext] lemma ext {f g : direct_sum ι M} (h : ∀ i, component R ι M i f = component R ι M i g) : f = g := dfinsupp.ext h lemma ext_iff {f g : direct_sum ι M} : f = g ↔ ∀ i, component R ι M i f = component R ι M i g := ⟨λ h _, by rw h, ext R⟩ end direct_sum
d75b6304e33aa68d7165dfcf88f88adcc75a02b7
30b012bb72d640ec30c8fdd4c45fdfa67beb012c
/category_theory/embedding.lean
be8073956776888c48da3374b9c683c8c346f9df
[ "Apache-2.0" ]
permissive
kckennylau/mathlib
21fb810b701b10d6606d9002a4004f7672262e83
47b3477e20ffb5a06588dd3abb01fe0fe3205646
refs/heads/master
1,634,976,409,281
1,542,042,832,000
1,542,319,733,000
109,560,458
0
0
Apache-2.0
1,542,369,208,000
1,509,867,494,000
Lean
UTF-8
Lean
false
false
2,636
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.isomorphism universes u₁ v₁ u₂ v₂ u₃ v₃ namespace category_theory variables {C : Type u₁} [𝒞 : category.{u₁ v₁} C] {D : Type u₂} [𝒟 : category.{u₂ v₂} D] include 𝒞 𝒟 class full (F : C ⥤ D) := (preimage : ∀ {X Y : C} (f : (F.obj X) ⟶ (F.obj Y)), X ⟶ Y) (witness' : ∀ {X Y : C} (f : (F.obj X) ⟶ (F.obj Y)), F.map (preimage f) = f . obviously) restate_axiom full.witness' attribute [simp] full.witness class faithful (F : C ⥤ D) : Prop := (injectivity' : ∀ {X Y : C} {f g : X ⟶ Y} (p : F.map f = F.map g), f = g . obviously) restate_axiom faithful.injectivity' namespace functor def injectivity (F : C ⥤ D) [faithful F] {X Y : C} {f g : X ⟶ Y} (p : F.map f = F.map g) : f = g := faithful.injectivity F p def preimage (F : C ⥤ D) [full F] {X Y : C} (f : F.obj X ⟶ F.obj Y) : X ⟶ Y := full.preimage.{u₁ v₁ u₂ v₂} f @[simp] lemma image_preimage (F : C ⥤ D) [full F] {X Y : C} (f : F.obj X ⟶ F.obj Y) : F.map (preimage F f) = f := begin unfold preimage, obviously end end functor section variables {F : C ⥤ D} [full F] [faithful F] {X Y : C} def preimage_iso (f : (F.obj X) ≅ (F.obj Y)) : X ≅ Y := { hom := F.preimage f.hom, inv := F.preimage f.inv, hom_inv_id' := begin apply @faithful.injectivity _ _ _ _ F, obviously, end, inv_hom_id' := begin apply @faithful.injectivity _ _ _ _ F, obviously, end, } @[simp] lemma preimage_iso_hom (f : (F.obj X) ≅ (F.obj Y)) : (preimage_iso f).hom = F.preimage f.hom := rfl @[simp] lemma preimage_iso_inv (f : (F.obj X) ≅ (F.obj Y)) : (preimage_iso f).inv = F.preimage (f.inv) := rfl end class embedding (F : C ⥤ D) extends (full F), (faithful F). end category_theory namespace category_theory variables {C : Type u₁} [𝒞 : category.{u₁ v₁} C] include 𝒞 instance full.id : full (functor.id C) := { preimage := λ _ _ f, f } instance : faithful (functor.id C) := by obviously instance : embedding (functor.id C) := { ((by apply_instance) : full (functor.id C)) with } variables {D : Type u₂} [𝒟 : category.{u₂ v₂} D] {E : Type u₃} [ℰ : category.{u₃ v₃} E] include 𝒟 ℰ variables (F : C ⥤ D) (G : D ⥤ E) instance faithful.comp [faithful F] [faithful G] : faithful (F ⋙ G) := { injectivity' := λ _ _ _ _ p, F.injectivity (G.injectivity p) } instance full.comp [full F] [full G] : full (F ⋙ G) := { preimage := λ _ _ f, F.preimage (G.preimage f) } end category_theory
89f8dc07105fe5c197747f768eb42ec89baf57e5
9dc8cecdf3c4634764a18254e94d43da07142918
/src/data/int/modeq.lean
c50554a490059b16c4d1fca0ac38c89de186c90f
[ "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
7,774
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import data.nat.modeq import tactic.ring /-! # Congruences modulo an integer This file defines the equivalence relation `a ≡ b [ZMOD n]` on the integers, similarly to how `data.nat.modeq` defines them for the natural numbers. The notation is short for `n.modeq a b`, which is defined to be `a % n = b % n` for integers `a b n`. ## Tags modeq, congruence, mod, MOD, modulo, integers -/ namespace int /-- `a ≡ b [ZMOD n]` when `a % n = b % n`. -/ @[derive decidable] def modeq (n a b : ℤ) := a % n = b % n notation a ` ≡ `:50 b ` [ZMOD `:50 n `]`:0 := modeq n a b variables {m n a b c d : ℤ} namespace modeq @[refl] protected theorem refl (a : ℤ) : a ≡ a [ZMOD n] := @rfl _ _ protected theorem rfl : a ≡ a [ZMOD n] := modeq.refl _ instance : is_refl _ (modeq n) := ⟨modeq.refl⟩ @[symm] protected theorem symm : a ≡ b [ZMOD n] → b ≡ a [ZMOD n] := eq.symm @[trans] protected theorem trans : a ≡ b [ZMOD n] → b ≡ c [ZMOD n] → a ≡ c [ZMOD n] := eq.trans end modeq lemma coe_nat_modeq_iff {a b n : ℕ} : a ≡ b [ZMOD n] ↔ a ≡ b [MOD n] := by unfold modeq nat.modeq; rw ← int.coe_nat_eq_coe_nat_iff; simp [coe_nat_mod] theorem modeq_zero_iff_dvd : a ≡ 0 [ZMOD n] ↔ n ∣ a := by rw [modeq, zero_mod, dvd_iff_mod_eq_zero] lemma _root_.has_dvd.dvd.modeq_zero_int (h : n ∣ a) : a ≡ 0 [ZMOD n] := modeq_zero_iff_dvd.2 h lemma _root_.has_dvd.dvd.zero_modeq_int (h : n ∣ a) : 0 ≡ a [ZMOD n] := h.modeq_zero_int.symm theorem modeq_iff_dvd : a ≡ b [ZMOD n] ↔ n ∣ b - a := by rw [modeq, eq_comm]; simp [mod_eq_mod_iff_mod_sub_eq_zero, dvd_iff_mod_eq_zero, -euclidean_domain.mod_eq_zero] theorem modeq_iff_add_fac {a b n : ℤ} : a ≡ b [ZMOD n] ↔ ∃ t, b = a + n * t := begin rw modeq_iff_dvd, exact exists_congr (λ t, sub_eq_iff_eq_add'), end theorem modeq.dvd : a ≡ b [ZMOD n] → n ∣ b - a := modeq_iff_dvd.1 theorem modeq_of_dvd : n ∣ b - a → a ≡ b [ZMOD n] := modeq_iff_dvd.2 theorem mod_modeq (a n) : a % n ≡ a [ZMOD n] := mod_mod _ _ namespace modeq protected theorem modeq_of_dvd (d : m ∣ n) (h : a ≡ b [ZMOD n]) : a ≡ b [ZMOD m] := modeq_iff_dvd.2 $ d.trans h.dvd protected theorem mul_left' (hc : 0 ≤ c) (h : a ≡ b [ZMOD n]) : c * a ≡ c * b [ZMOD (c * n)] := or.cases_on hc.lt_or_eq (λ hc, by unfold modeq; simp [mul_mod_mul_of_pos hc, (show _ = _, from h)] ) (λ hc, by simp [hc.symm]) protected theorem mul_right' (hc : 0 ≤ c) (h : a ≡ b [ZMOD n]) : a * c ≡ b * c [ZMOD (n * c)] := by rw [mul_comm a, mul_comm b, mul_comm n]; exact h.mul_left' hc protected theorem add (h₁ : a ≡ b [ZMOD n]) (h₂ : c ≡ d [ZMOD n]) : a + c ≡ b + d [ZMOD n] := modeq_iff_dvd.2 $ by { convert dvd_add h₁.dvd h₂.dvd, ring } protected theorem add_left (c : ℤ) (h : a ≡ b [ZMOD n]) : c + a ≡ c + b [ZMOD n] := modeq.rfl.add h protected theorem add_right (c : ℤ) (h : a ≡ b [ZMOD n]) : a + c ≡ b + c [ZMOD n] := h.add modeq.rfl protected theorem add_left_cancel (h₁ : a ≡ b [ZMOD n]) (h₂ : a + c ≡ b + d [ZMOD n]) : c ≡ d [ZMOD n] := have d - c = b + d - (a + c) - (b - a) := by ring, modeq_iff_dvd.2 $ by { rw [this], exact dvd_sub h₂.dvd h₁.dvd } protected theorem add_left_cancel' (c : ℤ) (h : c + a ≡ c + b [ZMOD n]) : a ≡ b [ZMOD n] := modeq.rfl.add_left_cancel h protected theorem add_right_cancel (h₁ : c ≡ d [ZMOD n]) (h₂ : a + c ≡ b + d [ZMOD n]) : a ≡ b [ZMOD n] := by { rw [add_comm a, add_comm b] at h₂, exact h₁.add_left_cancel h₂ } protected theorem add_right_cancel' (c : ℤ) (h : a + c ≡ b + c [ZMOD n]) : a ≡ b [ZMOD n] := modeq.rfl.add_right_cancel h protected theorem neg (h : a ≡ b [ZMOD n]) : -a ≡ -b [ZMOD n] := h.add_left_cancel (by simp_rw [←sub_eq_add_neg, sub_self]) protected theorem sub (h₁ : a ≡ b [ZMOD n]) (h₂ : c ≡ d [ZMOD n]) : a - c ≡ b - d [ZMOD n] := by { rw [sub_eq_add_neg, sub_eq_add_neg], exact h₁.add h₂.neg } protected theorem sub_left (c : ℤ) (h : a ≡ b [ZMOD n]) : c - a ≡ c - b [ZMOD n] := modeq.rfl.sub h protected theorem sub_right (c : ℤ) (h : a ≡ b [ZMOD n]) : a - c ≡ b - c [ZMOD n] := h.sub modeq.rfl protected theorem mul_left (c : ℤ) (h : a ≡ b [ZMOD n]) : c * a ≡ c * b [ZMOD n] := or.cases_on (le_total 0 c) (λ hc, (h.mul_left' hc).modeq_of_dvd (dvd_mul_left _ _) ) (λ hc, by rw [← neg_neg c, neg_mul, neg_mul _ b]; exact ((h.mul_left' $ neg_nonneg.2 hc).modeq_of_dvd (dvd_mul_left _ _)).neg) protected theorem mul_right (c : ℤ) (h : a ≡ b [ZMOD n]) : a * c ≡ b * c [ZMOD n] := by { rw [mul_comm a, mul_comm b], exact h.mul_left c } protected theorem mul (h₁ : a ≡ b [ZMOD n]) (h₂ : c ≡ d [ZMOD n]) : a * c ≡ b * d [ZMOD n] := (h₂.mul_left _).trans (h₁.mul_right _) protected theorem pow (m : ℕ) (h : a ≡ b [ZMOD n]) : a ^ m ≡ b ^ m [ZMOD n] := begin induction m with d hd, {refl}, rw [pow_succ, pow_succ], exact h.mul hd, end theorem of_modeq_mul_left (m : ℤ) (h : a ≡ b [ZMOD m * n]) : a ≡ b [ZMOD n] := by rw [modeq_iff_dvd] at *; exact (dvd_mul_left n m).trans h theorem of_modeq_mul_right (m : ℤ) : a ≡ b [ZMOD n * m] → a ≡ b [ZMOD n] := mul_comm m n ▸ of_modeq_mul_left _ end modeq theorem modeq_one : a ≡ b [ZMOD 1] := modeq_of_dvd (one_dvd _) lemma modeq_sub (a b : ℤ) : a ≡ b [ZMOD a - b] := (modeq_of_dvd dvd_rfl).symm lemma modeq_and_modeq_iff_modeq_mul {a b m n : ℤ} (hmn : m.nat_abs.coprime n.nat_abs) : a ≡ b [ZMOD m] ∧ a ≡ b [ZMOD n] ↔ (a ≡ b [ZMOD m * n]) := ⟨λ h, begin rw [modeq_iff_dvd, modeq_iff_dvd] at h, rw [modeq_iff_dvd, ← nat_abs_dvd, ← dvd_nat_abs, coe_nat_dvd, nat_abs_mul], refine hmn.mul_dvd_of_dvd_of_dvd _ _; rw [← coe_nat_dvd, nat_abs_dvd, dvd_nat_abs]; tauto end, λ h, ⟨h.of_modeq_mul_right _, h.of_modeq_mul_left _⟩⟩ lemma gcd_a_modeq (a b : ℕ) : (a : ℤ) * nat.gcd_a a b ≡ nat.gcd a b [ZMOD b] := by { rw [← add_zero ((a : ℤ) * _), nat.gcd_eq_gcd_ab], exact (dvd_mul_right _ _).zero_modeq_int.add_left _ } theorem modeq_add_fac {a b n : ℤ} (c : ℤ) (ha : a ≡ b [ZMOD n]) : a + n*c ≡ b [ZMOD n] := calc a + n*c ≡ b + n*c [ZMOD n] : ha.add_right _ ... ≡ b + 0 [ZMOD n] : (dvd_mul_right _ _).modeq_zero_int.add_left _ ... ≡ b [ZMOD n] : by rw add_zero theorem modeq_add_fac_self {a t n : ℤ} : a + n * t ≡ a [ZMOD n] := modeq_add_fac _ modeq.rfl lemma mod_coprime {a b : ℕ} (hab : nat.coprime a b) : ∃ y : ℤ, a * y ≡ 1 [ZMOD b] := ⟨ nat.gcd_a a b, have hgcd : nat.gcd a b = 1, from nat.coprime.gcd_eq_one hab, calc ↑a * nat.gcd_a a b ≡ ↑a * nat.gcd_a a b + ↑b * nat.gcd_b a b [ZMOD ↑b] : modeq.symm $ modeq_add_fac _ $ modeq.refl _ ... ≡ 1 [ZMOD ↑b] : by rw [← nat.gcd_eq_gcd_ab, hgcd]; reflexivity ⟩ lemma exists_unique_equiv (a : ℤ) {b : ℤ} (hb : 0 < b) : ∃ z : ℤ, 0 ≤ z ∧ z < b ∧ z ≡ a [ZMOD b] := ⟨ a % b, mod_nonneg _ (ne_of_gt hb), have a % b < |b|, from mod_lt _ (ne_of_gt hb), by rwa abs_of_pos hb at this, by simp [modeq] ⟩ lemma exists_unique_equiv_nat (a : ℤ) {b : ℤ} (hb : 0 < b) : ∃ z : ℕ, ↑z < b ∧ ↑z ≡ a [ZMOD b] := let ⟨z, hz1, hz2, hz3⟩ := exists_unique_equiv a hb in ⟨z.nat_abs, by split; rw [←of_nat_eq_coe, of_nat_nat_abs_eq_of_nonneg hz1]; assumption⟩ @[simp] lemma mod_mul_right_mod (a b c : ℤ) : a % (b * c) % b = a % b := (mod_modeq _ _).of_modeq_mul_right _ @[simp] lemma mod_mul_left_mod (a b c : ℤ) : a % (b * c) % c = a % c := (mod_modeq _ _).of_modeq_mul_left _ end int
f69fd84861e34897d823cc32d2f09dd7f11a321d
2d34dfb0a1cc250584282618dc10ea03d3fa858e
/src/locally_constant/basic.lean
d241916847935a8db44c5bbee984a6c7b40dd64d
[]
no_license
zeta1999/lean-liquid
61e294ec5adae959d8ee1b65d015775484ff58c2
96bb0fa3afc3b451bcd1fb7d974348de2f290541
refs/heads/master
1,676,579,150,248
1,610,771,445,000
1,610,771,445,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
8,906
lean
import topology.subset_properties import topology.algebra.monoid variables {X Y Z α : Type*} [topological_space X] -- move this section for_mathlib def finite_of_is_compact_of_discrete [discrete_topology X] (s : set X) (hs : is_compact s) : s.finite := begin have := hs.elim_finite_subcover (λ x : X, ({x} : set X)) (λ x, is_open_discrete _), simp only [set.subset_univ, forall_prop_of_true, set.Union_of_singleton] at this, rcases this with ⟨t, ht⟩, suffices : (⋃ (i : X) (H : i ∈ t), {i} : set X) = (t : set X), { rw this at ht, exact t.finite_to_set.subset ht }, ext x, simp only [exists_prop, set.mem_Union, set.mem_singleton_iff, exists_eq_right', finset.mem_coe] end noncomputable def fintype_of_univ_finite (H : set.finite (set.univ : set Y)) : fintype Y := begin choose t ht using H.exists_finset, refine ⟨t, _⟩, simpa only [set.mem_univ, iff_true] using ht end noncomputable def fintype_of_compact_of_discrete [compact_space X] [discrete_topology X] : fintype X := fintype_of_univ_finite $ finite_of_is_compact_of_discrete _ compact_univ end for_mathlib open_locale topological_space def is_locally_constant (f : X → Y) : Prop := ∀ s, is_open (f ⁻¹' s) namespace is_locally_constant lemma exists_open {f : X → Y} (hf : is_locally_constant f) (x : X) : ∃ (U : set X) (hU : is_open U) (hx : x ∈ U), ∀ x' ∈ U, f x' = f x := ⟨f ⁻¹' {(f x)}, hf _, set.mem_singleton _, λ x' hx', set.mem_singleton_iff.mp hx'⟩ lemma exists_nhds {f : X → Y} (hf : is_locally_constant f) (x : X) : ∃ U ∈ 𝓝 x, ∀ x' ∈ U, f x' = f x := let ⟨U, hU, hx, H⟩ := hf.exists_open x in ⟨U, mem_nhds_sets hU hx, H⟩ lemma iff_exists_open (f : X → Y) : is_locally_constant f ↔ ∀ x, ∃ (U : set X) (hU : is_open U) (hx : x ∈ U), ∀ x' ∈ U, f x' = f x := begin refine ⟨exists_open, _⟩, assume h s, rw is_open_iff_forall_mem_open, assume x hx, obtain ⟨U, hU, hxU, H⟩ := h x, refine ⟨U, _, hU, hxU⟩, assume x' hx', simp only [*, set.mem_preimage] at *, end lemma iff_exists_nhds (f : X → Y) : is_locally_constant f ↔ ∀ x, ∃ U ∈ 𝓝 x, ∀ x' ∈ U, f x' = f x := begin refine ⟨exists_nhds, _⟩, assume h, rw iff_exists_open, assume x, obtain ⟨U, hU, H⟩ := h x, obtain ⟨V, hVU, hV, hxV⟩ : ∃ (V : set X) (H : V ⊆ U), is_open V ∧ x ∈ V, by rwa mem_nhds_sets_iff at hU, refine ⟨V, hV, hxV, _⟩, assume x' hx', solve_by_elim only [H, hxV, hx', hVU] end lemma of_constant (f : X → Y) (h : ∃ y, ∀ x, f x = y) : is_locally_constant f := begin obtain ⟨y, hy⟩ := h, rw iff_exists_nhds, intro x, refine ⟨set.univ, filter.univ_mem_sets, _⟩, rintro x -, rw [hy, hy] end lemma const (y : Y) : is_locally_constant (function.const X y) := of_constant _ ⟨y, λ _, rfl⟩ protected lemma continuous {_ : topological_space Y} {f : X → Y} (hf : is_locally_constant f) : continuous f := ⟨λ U hU, hf _⟩ lemma iff_continuous {_ : topological_space Y} [discrete_topology Y] (f : X → Y) : is_locally_constant f ↔ continuous f := ⟨is_locally_constant.continuous, λ h s, h.is_open_preimage s (is_open_discrete _)⟩ lemma apply_eq_of_is_preconnected {f : X → Y} (hf : is_locally_constant f) (s : set X) (hs : is_preconnected s) (x y : X) (hx : x ∈ s) (hy : y ∈ s) : f y = f x := begin letI : topological_space Y := ⊥, haveI : discrete_topology Y := ⟨rfl⟩, have aux := is_preconnected.image hs f hf.continuous.continuous_on {f x} (f '' s \ {f x}) (is_open_discrete _) (is_open_discrete _), simp only [set.union_diff_self, ← set.inter_diff_assoc, set.inter_self, set.inter_diff_self, set.inter_empty, ← @set.ne_empty_iff_nonempty _ ∅, eq_self_iff_true, not_true, ne.def] at aux, classical, by_contra hxy, exact aux (set.subset_union_right _ _) ⟨f x, set.mem_inter (set.mem_image_of_mem f hx) (set.mem_singleton _)⟩ ⟨f y, set.mem_diff_singleton.mpr ⟨set.mem_image_of_mem f hy, hxy⟩⟩ end lemma range_finite [compact_space X] {f : X → Y} (hf : is_locally_constant f) : (set.range f).finite := begin letI : topological_space Y := ⊥, haveI : discrete_topology Y := ⟨rfl⟩, rw @iff_continuous X Y ‹_› ‹_› at hf, exact finite_of_is_compact_of_discrete _ (compact_range hf) end @[to_additive] lemma one [has_one Y] : is_locally_constant (1 : X → Y) := const 1 @[to_additive] lemma inv [has_inv Y] ⦃f : X → Y⦄ (hf : is_locally_constant f) : is_locally_constant f⁻¹ := begin intro s, suffices : f⁻¹ ⁻¹' s = f ⁻¹' (has_inv.inv ⁻¹' s), by { rw this, exact hf _ }, ext, simp only [set.mem_preimage, pi.inv_apply], end @[to_additive] lemma mul [has_mul Y] ⦃f g : X → Y⦄ (hf : is_locally_constant f) (hg : is_locally_constant g) : is_locally_constant (f * g) := begin letI : topological_space Y := ⊥, haveI : discrete_topology Y := ⟨rfl⟩, rw @iff_continuous X Y ‹_› ‹_› at hf hg ⊢, exact hf.mul hg end @[to_additive] lemma div [has_div Y] ⦃f g : X → Y⦄ (hf : is_locally_constant f) (hg : is_locally_constant g) : is_locally_constant (f / g) := begin rw iff_exists_open at hf hg ⊢, intro x, obtain ⟨U, hU, hxU, HU⟩ := hf x, obtain ⟨V, hV, hxV, HV⟩ := hg x, use [U ∩ V, is_open_inter hU hV, ⟨hxU, hxV⟩], rintro x' ⟨hx'U, hx'V⟩, simp only [pi.div_apply, HU x' hx'U, HV x' hx'V] end end is_locally_constant structure locally_constant (X Y : Type*) [topological_space X] := (to_fun : X → Y) (is_locally_constant : is_locally_constant to_fun) namespace locally_constant instance : has_coe_to_fun (locally_constant X Y) := ⟨_, locally_constant.to_fun⟩ initialize_simps_projections locally_constant (to_fun → apply) @[simp] lemma to_fun_eq_coe (f : locally_constant X Y) : f.to_fun = f := rfl @[simp] lemma coe_mk (f : X → Y) (h) : ⇑(⟨f, h⟩ : locally_constant X Y) = f := rfl theorem congr_fun {f g : locally_constant X Y} (h : f = g) (x : X) : f x = g x := congr_arg (λ h : locally_constant X Y, h x) h theorem congr_arg (f : locally_constant X Y) {x y : X} (h : x = y) : f x = f y := congr_arg (λ x : X, f x) h theorem coe_inj ⦃f g : locally_constant X Y⦄ (h : (f : X → Y) = g) : f = g := by cases f; cases g; cases h; refl @[ext] theorem ext ⦃f g : locally_constant X Y⦄ (h : ∀ x, f x = g x) : f = g := coe_inj (funext h) theorem ext_iff {f g : locally_constant X Y} : f = g ↔ ∀ x, f x = g x := ⟨λ h x, h ▸ rfl, λ h, ext h⟩ protected lemma continuous [topological_space Y] (f : locally_constant X Y) : continuous f := f.is_locally_constant.continuous /-- The constant locally constant function on `X` with value `y : Y`. -/ def const (X : Type*) {Y : Type*} [topological_space X] (y : Y) : locally_constant X Y := ⟨function.const X y, is_locally_constant.const _⟩ lemma range_finite [compact_space X] (f : locally_constant X Y) : (set.range f).finite := f.is_locally_constant.range_finite def map (f : Y → Z) : locally_constant X Y → locally_constant X Z := λ g, ⟨f ∘ g, λ s, by { rw set.preimage_comp, apply g.is_locally_constant }⟩ @[simp] lemma map_apply (f : Y → Z) (g : locally_constant X Y) : ⇑(map f g) = f ∘ g := rfl @[simp] lemma map_id : @map X Y Y _ id = id := by { ext, refl } @[simp] lemma map_comp {Y₁ Y₂ Y₃ : Type*} (g : Y₂ → Y₃) (f : Y₁ → Y₂) : @map X _ _ _ g ∘ map f = map (g ∘ f) := by { ext, refl } section comap open_locale classical variables [topological_space Y] noncomputable def comap (f : X → Y) : locally_constant Y Z → locally_constant X Z := if hf : continuous f then λ g, ⟨g ∘ f, λ s, by { rw set.preimage_comp, apply hf.is_open_preimage, apply g.is_locally_constant }⟩ else begin by_cases H : nonempty X, { introsI g, exact const X (g $ f $ classical.arbitrary X) }, { intro g, refine ⟨λ x, (H ⟨x⟩).elim, _⟩, intro s, rw is_open_iff_nhds, intro x, exact (H ⟨x⟩).elim } end @[simp] lemma coe_comap (f : X → Y) (g : locally_constant Y Z) (hf : continuous f) : ⇑(comap f g) = g ∘ f := by { rw [comap, dif_pos hf], refl } @[simp] lemma comap_id : @comap X X Z _ _ id = id := by { ext, simp only [continuous_id, id.def, function.comp.right_id, coe_comap] } lemma comap_comp [topological_space Z] (f : X → Y) (g : Y → Z) (hf : continuous f) (hg : continuous g) : @comap _ _ α _ _ f ∘ comap g = comap (g ∘ f) := by { ext, simp only [hf, hg, hg.comp hf, coe_comap] } lemma comap_const (f : X → Y) (y : Y) (h : ∀ x, f x = y) : (comap f : locally_constant Y Z → locally_constant X Z) = λ g, ⟨λ x, g y, is_locally_constant.const _⟩ := begin ext, rw coe_comap, { simp only [h, coe_mk, function.comp_app] }, { rw show f = λ x, y, by ext; apply h, exact continuous_const } end end comap end locally_constant
9b15ce7e126abfe51fa6da0c4a4959b6164c16a2
5ee26964f602030578ef0159d46145dd2e357ba5
/src/for_mathlib/sheaves/covering.lean
1daa4368332e94af52f6c7d579e09782e687e6e2
[ "Apache-2.0" ]
permissive
fpvandoorn/lean-perfectoid-spaces
569b4006fdfe491ca8b58dd817bb56138ada761f
06cec51438b168837fc6e9268945735037fd1db6
refs/heads/master
1,590,154,571,918
1,557,685,392,000
1,557,685,392,000
186,363,547
0
0
Apache-2.0
1,557,730,933,000
1,557,730,933,000
null
UTF-8
Lean
false
false
2,328
lean
/- Package the definition of an open cover of an open set. Author: Ramon Fernandez Mir -/ import topology.basic import topology.opens import for_mathlib.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
d401d140862bd591e30d3d4aaba2542d3167e5ce
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/extra/slow1.lean
6f84d090a9e32b9e77b4579725e25f8846146e13
[ "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
395
lean
open nat definition f (a : nat) : nat := a definition g (a : nat) : nat := 0 example (a b : nat) : @eq nat (g (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f a)))))))))))))))))))))) (g (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f b)))))))))))))))))))))) := @eq.refl nat (g (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f (f a))))))))))))))))))))))
c48dae831c4d357aa6596b776abae531a6d5e895
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/list/sections.lean
6ee2bb5893c573e346a084ec2a37d627127b1127
[ "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
1,559
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import data.list.forall2 /-! # List sections > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file proves some stuff about `list.sections` (definition in `data.list.defs`). A section of a list of lists `[l₁, ..., lₙ]` is a list whose `i`-th element comes from the `i`-th list. -/ open nat function namespace list variables {α β : Type*} theorem mem_sections {L : list (list α)} {f} : f ∈ sections L ↔ forall₂ (∈) f L := begin refine ⟨λ h, _, λ h, _⟩, { induction L generalizing f, {cases mem_singleton.1 h, exact forall₂.nil}, simp only [sections, bind_eq_bind, mem_bind, mem_map] at h, rcases h with ⟨_, _, _, _, rfl⟩, simp only [*, forall₂_cons, true_and] }, { induction h with a l f L al fL fs, {exact or.inl rfl}, simp only [sections, bind_eq_bind, mem_bind, mem_map], exact ⟨_, fs, _, al, rfl, rfl⟩ } end theorem mem_sections_length {L : list (list α)} {f} (h : f ∈ sections L) : length f = length L := (mem_sections.1 h).length_eq lemma rel_sections {r : α → β → Prop} : (forall₂ (forall₂ r) ⇒ forall₂ (forall₂ r)) sections sections | _ _ forall₂.nil := forall₂.cons forall₂.nil forall₂.nil | _ _ (forall₂.cons h₀ h₁) := rel_bind (rel_sections h₁) (assume _ _ hl, rel_map (assume _ _ ha, forall₂.cons ha hl) h₀) end list
7aadf7099ba13a79a42b6f32ef6909fe5e5f249c
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/category_theory/limits/shapes/pullbacks.lean
d667c0fcfd7c4341b3ab99f22acbf3cb12b0bd23
[]
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
36,322
lean
/- Copyright (c) 2018 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Markus Himmel, Bhavik Mehta -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.category_theory.limits.shapes.wide_pullbacks import Mathlib.category_theory.limits.shapes.binary_products import Mathlib.PostPort universes v u_1 u u₂ namespace Mathlib /-! # Pullbacks We define a category `walking_cospan` (resp. `walking_span`), which is the index category for the given data for a pullback (resp. pushout) diagram. Convenience methods `cospan f g` and `span f g` construct functors from the walking (co)span, hitting the given morphisms. We define `pullback f g` and `pushout f g` as limits and colimits of such functors. ## References * [Stacks: Fibre products](https://stacks.math.columbia.edu/tag/001U) * [Stacks: Pushouts](https://stacks.math.columbia.edu/tag/0025) -/ namespace category_theory.limits /-- The type of objects for the diagram indexing a pullback, defined as a special case of `wide_pullback_shape`. -/ def walking_cospan := wide_pullback_shape walking_pair /-- The left point of the walking cospan. -/ /-- The right point of the walking cospan. -/ def walking_cospan.left : walking_cospan := some walking_pair.left /-- The central point of the walking cospan. -/ def walking_cospan.right : walking_cospan := some walking_pair.right def walking_cospan.one : walking_cospan := none /-- The type of objects for the diagram indexing a pushout, defined as a special case of `wide_pushout_shape`. -/ def walking_span := wide_pushout_shape walking_pair /-- The left point of the walking span. -/ /-- The right point of the walking span. -/ def walking_span.left : walking_span := some walking_pair.left /-- The central point of the walking span. -/ def walking_span.right : walking_span := some walking_pair.right def walking_span.zero : walking_span := none namespace walking_cospan /-- The type of arrows for the diagram indexing a pullback. -/ def hom : walking_cospan → walking_cospan → Type v := wide_pullback_shape.hom /-- The left arrow of the walking cospan. -/ /-- The right arrow of the walking cospan. -/ def hom.inl : left ⟶ one := wide_pullback_shape.hom.term walking_pair.left /-- The identity arrows of the walking cospan. -/ def hom.inr : right ⟶ one := wide_pullback_shape.hom.term walking_pair.right def hom.id (X : walking_cospan) : X ⟶ X := wide_pullback_shape.hom.id X protected instance category_theory.has_hom.hom.subsingleton (X : walking_cospan) (Y : walking_cospan) : subsingleton (X ⟶ Y) := subsingleton.intro fun (a b : X ⟶ Y) => eq.mpr (id (propext (eq_iff_true_of_subsingleton a b))) trivial end walking_cospan namespace walking_span /-- The type of arrows for the diagram indexing a pushout. -/ def hom : walking_span → walking_span → Type v := wide_pushout_shape.hom /-- The left arrow of the walking span. -/ /-- The right arrow of the walking span. -/ def hom.fst : zero ⟶ left := wide_pushout_shape.hom.init walking_pair.left /-- The identity arrows of the walking span. -/ def hom.snd : zero ⟶ right := wide_pushout_shape.hom.init walking_pair.right def hom.id (X : walking_span) : X ⟶ X := wide_pushout_shape.hom.id X protected instance category_theory.has_hom.hom.subsingleton (X : walking_span) (Y : walking_span) : subsingleton (X ⟶ Y) := subsingleton.intro fun (a b : X ⟶ Y) => eq.mpr (id (propext (eq_iff_true_of_subsingleton a b))) trivial end walking_span /-- `cospan f g` is the functor from the walking cospan hitting `f` and `g`. -/ def cospan {C : Type u} [category C] {X : C} {Y : C} {Z : C} (f : X ⟶ Z) (g : Y ⟶ Z) : walking_cospan ⥤ C := wide_pullback_shape.wide_cospan Z (fun (j : walking_pair) => walking_pair.cases_on j X Y) fun (j : walking_pair) => walking_pair.cases_on j f g /-- `span f g` is the functor from the walking span hitting `f` and `g`. -/ def span {C : Type u} [category C] {X : C} {Y : C} {Z : C} (f : X ⟶ Y) (g : X ⟶ Z) : walking_span ⥤ C := wide_pushout_shape.wide_span X (fun (j : walking_pair) => walking_pair.cases_on j Y Z) fun (j : walking_pair) => walking_pair.cases_on j f g @[simp] theorem cospan_left {C : Type u} [category C] {X : C} {Y : C} {Z : C} (f : X ⟶ Z) (g : Y ⟶ Z) : functor.obj (cospan f g) walking_cospan.left = X := rfl @[simp] theorem span_left {C : Type u} [category C] {X : C} {Y : C} {Z : C} (f : X ⟶ Y) (g : X ⟶ Z) : functor.obj (span f g) walking_span.left = Y := rfl @[simp] theorem cospan_right {C : Type u} [category C] {X : C} {Y : C} {Z : C} (f : X ⟶ Z) (g : Y ⟶ Z) : functor.obj (cospan f g) walking_cospan.right = Y := rfl @[simp] theorem span_right {C : Type u} [category C] {X : C} {Y : C} {Z : C} (f : X ⟶ Y) (g : X ⟶ Z) : functor.obj (span f g) walking_span.right = Z := rfl @[simp] theorem cospan_one {C : Type u} [category C] {X : C} {Y : C} {Z : C} (f : X ⟶ Z) (g : Y ⟶ Z) : functor.obj (cospan f g) walking_cospan.one = Z := rfl @[simp] theorem span_zero {C : Type u} [category C] {X : C} {Y : C} {Z : C} (f : X ⟶ Y) (g : X ⟶ Z) : functor.obj (span f g) walking_span.zero = X := rfl @[simp] theorem cospan_map_inl {C : Type u} [category C] {X : C} {Y : C} {Z : C} (f : X ⟶ Z) (g : Y ⟶ Z) : functor.map (cospan f g) walking_cospan.hom.inl = f := rfl @[simp] theorem span_map_fst {C : Type u} [category C] {X : C} {Y : C} {Z : C} (f : X ⟶ Y) (g : X ⟶ Z) : functor.map (span f g) walking_span.hom.fst = f := rfl @[simp] theorem cospan_map_inr {C : Type u} [category C] {X : C} {Y : C} {Z : C} (f : X ⟶ Z) (g : Y ⟶ Z) : functor.map (cospan f g) walking_cospan.hom.inr = g := rfl @[simp] theorem span_map_snd {C : Type u} [category C] {X : C} {Y : C} {Z : C} (f : X ⟶ Y) (g : X ⟶ Z) : functor.map (span f g) walking_span.hom.snd = g := rfl theorem cospan_map_id {C : Type u} [category C] {X : C} {Y : C} {Z : C} (f : X ⟶ Z) (g : Y ⟶ Z) (w : walking_cospan) : functor.map (cospan f g) (walking_cospan.hom.id w) = 𝟙 := rfl theorem span_map_id {C : Type u} [category C] {X : C} {Y : C} {Z : C} (f : X ⟶ Y) (g : X ⟶ Z) (w : walking_span) : functor.map (span f g) (walking_span.hom.id w) = 𝟙 := rfl /-- Every diagram indexing an pullback is naturally isomorphic (actually, equal) to a `cospan` -/ def diagram_iso_cospan {C : Type u} [category C] (F : walking_cospan ⥤ C) : F ≅ cospan (functor.map F walking_cospan.hom.inl) (functor.map F walking_cospan.hom.inr) := nat_iso.of_components (fun (j : walking_cospan) => eq_to_iso sorry) sorry /-- Every diagram indexing a pushout is naturally isomorphic (actually, equal) to a `span` -/ def diagram_iso_span {C : Type u} [category C] (F : walking_span ⥤ C) : F ≅ span (functor.map F walking_span.hom.fst) (functor.map F walking_span.hom.snd) := nat_iso.of_components (fun (j : walking_span) => eq_to_iso sorry) sorry /-- A pullback cone is just a cone on the cospan formed by two morphisms `f : X ⟶ Z` and `g : Y ⟶ Z`.-/ def pullback_cone {C : Type u} [category C] {X : C} {Y : C} {Z : C} (f : X ⟶ Z) (g : Y ⟶ Z) := cone (cospan f g) namespace pullback_cone /-- The first projection of a pullback cone. -/ def fst {C : Type u} [category C] {X : C} {Y : C} {Z : C} {f : X ⟶ Z} {g : Y ⟶ Z} (t : pullback_cone f g) : cone.X t ⟶ X := nat_trans.app (cone.π t) walking_cospan.left /-- The second projection of a pullback cone. -/ def snd {C : Type u} [category C] {X : C} {Y : C} {Z : C} {f : X ⟶ Z} {g : Y ⟶ Z} (t : pullback_cone f g) : cone.X t ⟶ Y := nat_trans.app (cone.π t) walking_cospan.right /-- This is a slightly more convenient method to verify that a pullback cone is a limit cone. It only asks for a proof of facts that carry any mathematical content -/ def is_limit_aux {C : Type u} [category C] {X : C} {Y : C} {Z : C} {f : X ⟶ Z} {g : Y ⟶ Z} (t : pullback_cone f g) (lift : (s : cone (cospan f g)) → cone.X s ⟶ cone.X t) (fac_left : ∀ (s : pullback_cone f g), lift s ≫ fst t = fst s) (fac_right : ∀ (s : pullback_cone f g), lift s ≫ snd t = snd s) (uniq : ∀ (s : pullback_cone f g) (m : cone.X s ⟶ cone.X t), (∀ (j : walking_cospan), m ≫ nat_trans.app (cone.π t) j = nat_trans.app (cone.π s) j) → m = lift s) : is_limit t := is_limit.mk lift /-- This is another convenient method to verify that a pullback cone is a limit cone. It only asks for a proof of facts that carry any mathematical content, and allows access to the same `s` for all parts. -/ def is_limit_aux' {C : Type u} [category C] {X : C} {Y : C} {Z : C} {f : X ⟶ Z} {g : Y ⟶ Z} (t : pullback_cone f g) (create : (s : pullback_cone f g) → Subtype fun (l : cone.X s ⟶ cone.X t) => l ≫ fst t = fst s ∧ l ≫ snd t = snd s ∧ ∀ {m : cone.X s ⟶ cone.X t}, m ≫ fst t = fst s → m ≫ snd t = snd s → m = l) : is_limit t := is_limit_aux t (fun (s : cone (cospan f g)) => subtype.val (create s)) sorry sorry sorry /-- A pullback cone on `f` and `g` is determined by morphisms `fst : W ⟶ X` and `snd : W ⟶ Y` such that `fst ≫ f = snd ≫ g`. -/ @[simp] theorem mk_π_app {C : Type u} [category C] {X : C} {Y : C} {Z : C} {f : X ⟶ Z} {g : Y ⟶ Z} {W : C} (fst : W ⟶ X) (snd : W ⟶ Y) (eq : fst ≫ f = snd ≫ g) (j : walking_cospan) : nat_trans.app (cone.π (mk fst snd eq)) j = option.cases_on j (fst ≫ f) fun (j' : walking_pair) => walking_pair.cases_on j' fst snd := Eq.refl (nat_trans.app (cone.π (mk fst snd eq)) j) @[simp] theorem mk_π_app_left {C : Type u} [category C] {X : C} {Y : C} {Z : C} {f : X ⟶ Z} {g : Y ⟶ Z} {W : C} (fst : W ⟶ X) (snd : W ⟶ Y) (eq : fst ≫ f = snd ≫ g) : nat_trans.app (cone.π (mk fst snd eq)) walking_cospan.left = fst := rfl @[simp] theorem mk_π_app_right {C : Type u} [category C] {X : C} {Y : C} {Z : C} {f : X ⟶ Z} {g : Y ⟶ Z} {W : C} (fst : W ⟶ X) (snd : W ⟶ Y) (eq : fst ≫ f = snd ≫ g) : nat_trans.app (cone.π (mk fst snd eq)) walking_cospan.right = snd := rfl @[simp] theorem mk_π_app_one {C : Type u} [category C] {X : C} {Y : C} {Z : C} {f : X ⟶ Z} {g : Y ⟶ Z} {W : C} (fst : W ⟶ X) (snd : W ⟶ Y) (eq : fst ≫ f = snd ≫ g) : nat_trans.app (cone.π (mk fst snd eq)) walking_cospan.one = fst ≫ f := rfl @[simp] theorem mk_fst {C : Type u} [category C] {X : C} {Y : C} {Z : C} {f : X ⟶ Z} {g : Y ⟶ Z} {W : C} (fst : W ⟶ X) (snd : W ⟶ Y) (eq : fst ≫ f = snd ≫ g) : fst (mk fst snd eq) = fst := rfl @[simp] theorem mk_snd {C : Type u} [category C] {X : C} {Y : C} {Z : C} {f : X ⟶ Z} {g : Y ⟶ Z} {W : C} (fst : W ⟶ X) (snd : W ⟶ Y) (eq : fst ≫ f = snd ≫ g) : snd (mk fst snd eq) = snd := rfl theorem condition_assoc {C : Type u} [category C] {X : C} {Y : C} {Z : C} {f : X ⟶ Z} {g : Y ⟶ Z} (t : pullback_cone f g) {X' : C} (f' : Z ⟶ X') : fst t ≫ f ≫ f' = snd t ≫ g ≫ f' := sorry /-- To check whether a morphism is equalized by the maps of a pullback cone, it suffices to check it for `fst t` and `snd t` -/ theorem equalizer_ext {C : Type u} [category C] {X : C} {Y : C} {Z : C} {f : X ⟶ Z} {g : Y ⟶ Z} (t : pullback_cone f g) {W : C} {k : W ⟶ cone.X t} {l : W ⟶ cone.X t} (h₀ : k ≫ fst t = l ≫ fst t) (h₁ : k ≫ snd t = l ≫ snd t) (j : walking_cospan) : k ≫ nat_trans.app (cone.π t) j = l ≫ nat_trans.app (cone.π t) j := sorry theorem is_limit.hom_ext {C : Type u} [category C] {X : C} {Y : C} {Z : C} {f : X ⟶ Z} {g : Y ⟶ Z} {t : pullback_cone f g} (ht : is_limit t) {W : C} {k : W ⟶ cone.X t} {l : W ⟶ cone.X t} (h₀ : k ≫ fst t = l ≫ fst t) (h₁ : k ≫ snd t = l ≫ snd t) : k = l := is_limit.hom_ext ht (equalizer_ext t h₀ h₁) /-- If `t` is a limit pullback cone over `f` and `g` and `h : W ⟶ X` and `k : W ⟶ Y` are such that `h ≫ f = k ≫ g`, then we have `l : W ⟶ t.X` satisfying `l ≫ fst t = h` and `l ≫ snd t = k`. -/ def is_limit.lift' {C : Type u} [category C] {X : C} {Y : C} {Z : C} {f : X ⟶ Z} {g : Y ⟶ Z} {t : pullback_cone f g} (ht : is_limit t) {W : C} (h : W ⟶ X) (k : W ⟶ Y) (w : h ≫ f = k ≫ g) : Subtype fun (l : W ⟶ cone.X t) => l ≫ fst t = h ∧ l ≫ snd t = k := { val := is_limit.lift ht (mk h k w), property := sorry } /-- This is a more convenient formulation to show that a `pullback_cone` constructed using `pullback_cone.mk` is a limit cone. -/ def is_limit.mk {C : Type u} [category C] {X : C} {Y : C} {Z : C} {f : X ⟶ Z} {g : Y ⟶ Z} {W : C} {fst : W ⟶ X} {snd : W ⟶ Y} (eq : fst ≫ f = snd ≫ g) (lift : (s : pullback_cone f g) → cone.X s ⟶ W) (fac_left : ∀ (s : pullback_cone f g), lift s ≫ fst = fst s) (fac_right : ∀ (s : pullback_cone f g), lift s ≫ snd = snd s) (uniq : ∀ (s : pullback_cone f g) (m : cone.X s ⟶ W), m ≫ fst = fst s → m ≫ snd = snd s → m = lift s) : is_limit (mk fst snd eq) := is_limit_aux (mk fst snd eq) lift fac_left fac_right sorry /-- The flip of a pullback square is a pullback square. -/ def flip_is_limit {C : Type u} [category C] {X : C} {Y : C} {Z : C} {f : X ⟶ Z} {g : Y ⟶ Z} {W : C} {h : W ⟶ X} {k : W ⟶ Y} {comm : h ≫ f = k ≫ g} (t : is_limit (mk k h flip_is_limit._proof_1)) : is_limit (mk h k comm) := is_limit_aux' (mk h k comm) fun (s : pullback_cone f g) => { val := subtype.val (is_limit.lift' t (snd s) (fst s) sorry), property := sorry } /-- The pullback cone `(𝟙 X, 𝟙 X)` for the pair `(f, f)` is a limit if `f` is a mono. The converse is shown in `mono_of_pullback_is_id`. -/ def is_limit_mk_id_id {C : Type u} [category C] {X : C} {Y : C} (f : X ⟶ Y) [mono f] : is_limit (mk 𝟙 𝟙 (is_limit_mk_id_id._proof_1 f)) := is_limit.mk sorry (fun (s : pullback_cone f f) => fst s) sorry sorry sorry /-- `f` is a mono if the pullback cone `(𝟙 X, 𝟙 X)` is a limit for the pair `(f, f)`. The converse is given in `pullback_cone.is_id_of_mono`. -/ theorem mono_of_is_limit_mk_id_id {C : Type u} [category C] {X : C} {Y : C} (f : X ⟶ Y) (t : is_limit (mk 𝟙 𝟙 rfl)) : mono f := sorry end pullback_cone /-- A pushout cocone is just a cocone on the span formed by two morphisms `f : X ⟶ Y` and `g : X ⟶ Z`.-/ def pushout_cocone {C : Type u} [category C] {X : C} {Y : C} {Z : C} (f : X ⟶ Y) (g : X ⟶ Z) := cocone (span f g) namespace pushout_cocone /-- The first inclusion of a pushout cocone. -/ def inl {C : Type u} [category C] {X : C} {Y : C} {Z : C} {f : X ⟶ Y} {g : X ⟶ Z} (t : pushout_cocone f g) : Y ⟶ cocone.X t := nat_trans.app (cocone.ι t) walking_span.left /-- The second inclusion of a pushout cocone. -/ def inr {C : Type u} [category C] {X : C} {Y : C} {Z : C} {f : X ⟶ Y} {g : X ⟶ Z} (t : pushout_cocone f g) : Z ⟶ cocone.X t := nat_trans.app (cocone.ι t) walking_span.right /-- This is a slightly more convenient method to verify that a pushout cocone is a colimit cocone. It only asks for a proof of facts that carry any mathematical content -/ def is_colimit_aux {C : Type u} [category C] {X : C} {Y : C} {Z : C} {f : X ⟶ Y} {g : X ⟶ Z} (t : pushout_cocone f g) (desc : (s : pushout_cocone f g) → cocone.X t ⟶ cocone.X s) (fac_left : ∀ (s : pushout_cocone f g), inl t ≫ desc s = inl s) (fac_right : ∀ (s : pushout_cocone f g), inr t ≫ desc s = inr s) (uniq : ∀ (s : pushout_cocone f g) (m : cocone.X t ⟶ cocone.X s), (∀ (j : walking_span), nat_trans.app (cocone.ι t) j ≫ m = nat_trans.app (cocone.ι s) j) → m = desc s) : is_colimit t := is_colimit.mk desc /-- This is another convenient method to verify that a pushout cocone is a colimit cocone. It only asks for a proof of facts that carry any mathematical content, and allows access to the same `s` for all parts. -/ def is_colimit_aux' {C : Type u} [category C] {X : C} {Y : C} {Z : C} {f : X ⟶ Y} {g : X ⟶ Z} (t : pushout_cocone f g) (create : (s : pushout_cocone f g) → Subtype fun (l : cocone.X t ⟶ cocone.X s) => inl t ≫ l = inl s ∧ inr t ≫ l = inr s ∧ ∀ {m : cocone.X t ⟶ cocone.X s}, inl t ≫ m = inl s → inr t ≫ m = inr s → m = l) : is_colimit t := is_colimit_aux t (fun (s : pushout_cocone f g) => subtype.val (create s)) sorry sorry sorry /-- A pushout cocone on `f` and `g` is determined by morphisms `inl : Y ⟶ W` and `inr : Z ⟶ W` such that `f ≫ inl = g ↠ inr`. -/ def mk {C : Type u} [category C] {X : C} {Y : C} {Z : C} {f : X ⟶ Y} {g : X ⟶ Z} {W : C} (inl : Y ⟶ W) (inr : Z ⟶ W) (eq : f ≫ inl = g ≫ inr) : pushout_cocone f g := cocone.mk W (nat_trans.mk fun (j : walking_span) => option.cases_on j (f ≫ inl) fun (j' : walking_pair) => walking_pair.cases_on j' inl inr) @[simp] theorem mk_ι_app_left {C : Type u} [category C] {X : C} {Y : C} {Z : C} {f : X ⟶ Y} {g : X ⟶ Z} {W : C} (inl : Y ⟶ W) (inr : Z ⟶ W) (eq : f ≫ inl = g ≫ inr) : nat_trans.app (cocone.ι (mk inl inr eq)) walking_span.left = inl := rfl @[simp] theorem mk_ι_app_right {C : Type u} [category C] {X : C} {Y : C} {Z : C} {f : X ⟶ Y} {g : X ⟶ Z} {W : C} (inl : Y ⟶ W) (inr : Z ⟶ W) (eq : f ≫ inl = g ≫ inr) : nat_trans.app (cocone.ι (mk inl inr eq)) walking_span.right = inr := rfl @[simp] theorem mk_ι_app_zero {C : Type u} [category C] {X : C} {Y : C} {Z : C} {f : X ⟶ Y} {g : X ⟶ Z} {W : C} (inl : Y ⟶ W) (inr : Z ⟶ W) (eq : f ≫ inl = g ≫ inr) : nat_trans.app (cocone.ι (mk inl inr eq)) walking_span.zero = f ≫ inl := rfl @[simp] theorem mk_inl {C : Type u} [category C] {X : C} {Y : C} {Z : C} {f : X ⟶ Y} {g : X ⟶ Z} {W : C} (inl : Y ⟶ W) (inr : Z ⟶ W) (eq : f ≫ inl = g ≫ inr) : inl (mk inl inr eq) = inl := rfl @[simp] theorem mk_inr {C : Type u} [category C] {X : C} {Y : C} {Z : C} {f : X ⟶ Y} {g : X ⟶ Z} {W : C} (inl : Y ⟶ W) (inr : Z ⟶ W) (eq : f ≫ inl = g ≫ inr) : inr (mk inl inr eq) = inr := rfl theorem condition_assoc {C : Type u} [category C] {X : C} {Y : C} {Z : C} {f : X ⟶ Y} {g : X ⟶ Z} (t : pushout_cocone f g) {X' : C} (f' : cocone.X t ⟶ X') : f ≫ inl t ≫ f' = g ≫ inr t ≫ f' := sorry /-- To check whether a morphism is coequalized by the maps of a pushout cocone, it suffices to check it for `inl t` and `inr t` -/ theorem coequalizer_ext {C : Type u} [category C] {X : C} {Y : C} {Z : C} {f : X ⟶ Y} {g : X ⟶ Z} (t : pushout_cocone f g) {W : C} {k : cocone.X t ⟶ W} {l : cocone.X t ⟶ W} (h₀ : inl t ≫ k = inl t ≫ l) (h₁ : inr t ≫ k = inr t ≫ l) (j : walking_span) : nat_trans.app (cocone.ι t) j ≫ k = nat_trans.app (cocone.ι t) j ≫ l := sorry theorem is_colimit.hom_ext {C : Type u} [category C] {X : C} {Y : C} {Z : C} {f : X ⟶ Y} {g : X ⟶ Z} {t : pushout_cocone f g} (ht : is_colimit t) {W : C} {k : cocone.X t ⟶ W} {l : cocone.X t ⟶ W} (h₀ : inl t ≫ k = inl t ≫ l) (h₁ : inr t ≫ k = inr t ≫ l) : k = l := is_colimit.hom_ext ht (coequalizer_ext t h₀ h₁) /-- If `t` is a colimit pushout cocone over `f` and `g` and `h : Y ⟶ W` and `k : Z ⟶ W` are morphisms satisfying `f ≫ h = g ≫ k`, then we have a factorization `l : t.X ⟶ W` such that `inl t ≫ l = h` and `inr t ≫ l = k`. -/ def is_colimit.desc' {C : Type u} [category C] {X : C} {Y : C} {Z : C} {f : X ⟶ Y} {g : X ⟶ Z} {t : pushout_cocone f g} (ht : is_colimit t) {W : C} (h : Y ⟶ W) (k : Z ⟶ W) (w : f ≫ h = g ≫ k) : Subtype fun (l : cocone.X t ⟶ W) => inl t ≫ l = h ∧ inr t ≫ l = k := { val := is_colimit.desc ht (mk h k w), property := sorry } /-- This is a more convenient formulation to show that a `pushout_cocone` constructed using `pushout_cocone.mk` is a colimit cocone. -/ def is_colimit.mk {C : Type u} [category C] {X : C} {Y : C} {Z : C} {f : X ⟶ Y} {g : X ⟶ Z} {W : C} {inl : Y ⟶ W} {inr : Z ⟶ W} (eq : f ≫ inl = g ≫ inr) (desc : (s : pushout_cocone f g) → W ⟶ cocone.X s) (fac_left : ∀ (s : pushout_cocone f g), inl ≫ desc s = inl s) (fac_right : ∀ (s : pushout_cocone f g), inr ≫ desc s = inr s) (uniq : ∀ (s : pushout_cocone f g) (m : W ⟶ cocone.X s), inl ≫ m = inl s → inr ≫ m = inr s → m = desc s) : is_colimit (mk inl inr eq) := is_colimit_aux (mk inl inr eq) desc fac_left fac_right sorry /-- The flip of a pushout square is a pushout square. -/ def flip_is_colimit {C : Type u} [category C] {X : C} {Y : C} {Z : C} {f : X ⟶ Y} {g : X ⟶ Z} {W : C} {h : Y ⟶ W} {k : Z ⟶ W} {comm : f ≫ h = g ≫ k} (t : is_colimit (mk k h flip_is_colimit._proof_1)) : is_colimit (mk h k comm) := is_colimit_aux' (mk h k comm) fun (s : pushout_cocone f g) => { val := subtype.val (is_colimit.desc' t (inr s) (inl s) sorry), property := sorry } end pushout_cocone /-- This is a helper construction that can be useful when verifying that a category has all pullbacks. Given `F : walking_cospan ⥤ C`, which is really the same as `cospan (F.map inl) (F.map inr)`, and a pullback cone on `F.map inl` and `F.map inr`, we get a cone on `F`. If you're thinking about using this, have a look at `has_pullbacks_of_has_limit_cospan`, which you may find to be an easier way of achieving your goal. -/ @[simp] theorem cone.of_pullback_cone_π {C : Type u} [category C] {F : walking_cospan ⥤ C} (t : pullback_cone (functor.map F walking_cospan.hom.inl) (functor.map F walking_cospan.hom.inr)) : cone.π (cone.of_pullback_cone t) = cone.π t ≫ iso.inv (diagram_iso_cospan F) := Eq.refl (cone.π (cone.of_pullback_cone t)) /-- This is a helper construction that can be useful when verifying that a category has all pushout. Given `F : walking_span ⥤ C`, which is really the same as `span (F.map fst) (F.mal snd)`, and a pushout cocone on `F.map fst` and `F.map snd`, we get a cocone on `F`. If you're thinking about using this, have a look at `has_pushouts_of_has_colimit_span`, which you may find to be an easiery way of achieving your goal. -/ @[simp] theorem cocone.of_pushout_cocone_X {C : Type u} [category C] {F : walking_span ⥤ C} (t : pushout_cocone (functor.map F walking_span.hom.fst) (functor.map F walking_span.hom.snd)) : cocone.X (cocone.of_pushout_cocone t) = cocone.X t := Eq.refl (cocone.X (cocone.of_pushout_cocone t)) /-- Given `F : walking_cospan ⥤ C`, which is really the same as `cospan (F.map inl) (F.map inr)`, and a cone on `F`, we get a pullback cone on `F.map inl` and `F.map inr`. -/ @[simp] theorem pullback_cone.of_cone_X {C : Type u} [category C] {F : walking_cospan ⥤ C} (t : cone F) : cone.X (pullback_cone.of_cone t) = cone.X t := Eq.refl (cone.X (pullback_cone.of_cone t)) /-- Given `F : walking_span ⥤ C`, which is really the same as `span (F.map fst) (F.map snd)`, and a cocone on `F`, we get a pushout cocone on `F.map fst` and `F.map snd`. -/ def pushout_cocone.of_cocone {C : Type u} [category C] {F : walking_span ⥤ C} (t : cocone F) : pushout_cocone (functor.map F walking_span.hom.fst) (functor.map F walking_span.hom.snd) := cocone.mk (cocone.X t) (iso.inv (diagram_iso_span F) ≫ cocone.ι t) /-- `has_pullback f g` represents a particular choice of limiting cone for the pair of morphisms `f : X ⟶ Z` and `g : Y ⟶ Z`. -/ /-- def has_pullback {C : Type u} [category C] {X : C} {Y : C} {Z : C} (f : X ⟶ Z) (g : Y ⟶ Z) := has_limit (cospan f g) `has_pushout f g` represents a particular choice of colimiting cocone for the pair of morphisms `f : X ⟶ Y` and `g : X ⟶ Z`. -/ def has_pushout {C : Type u} [category C] {X : C} {Y : C} {Z : C} (f : X ⟶ Y) (g : X ⟶ Z) := has_colimit (span f g) /-- `pullback f g` computes the pullback of a pair of morphisms with the same target. -/ def pullback {C : Type u} [category C] {X : C} {Y : C} {Z : C} (f : X ⟶ Z) (g : Y ⟶ Z) [has_pullback f g] : C := limit (cospan f g) /-- `pushout f g` computes the pushout of a pair of morphisms with the same source. -/ def pushout {C : Type u} [category C] {X : C} {Y : C} {Z : C} (f : X ⟶ Y) (g : X ⟶ Z) [has_pushout f g] : C := colimit (span f g) /-- The first projection of the pullback of `f` and `g`. -/ def pullback.fst {C : Type u} [category C] {X : C} {Y : C} {Z : C} {f : X ⟶ Z} {g : Y ⟶ Z} [has_pullback f g] : pullback f g ⟶ X := limit.π (cospan f g) walking_cospan.left /-- The second projection of the pullback of `f` and `g`. -/ def pullback.snd {C : Type u} [category C] {X : C} {Y : C} {Z : C} {f : X ⟶ Z} {g : Y ⟶ Z} [has_pullback f g] : pullback f g ⟶ Y := limit.π (cospan f g) walking_cospan.right /-- The first inclusion into the pushout of `f` and `g`. -/ def pushout.inl {C : Type u} [category C] {X : C} {Y : C} {Z : C} {f : X ⟶ Y} {g : X ⟶ Z} [has_pushout f g] : Y ⟶ pushout f g := colimit.ι (span f g) walking_span.left /-- The second inclusion into the pushout of `f` and `g`. -/ def pushout.inr {C : Type u} [category C] {X : C} {Y : C} {Z : C} {f : X ⟶ Y} {g : X ⟶ Z} [has_pushout f g] : Z ⟶ pushout f g := colimit.ι (span f g) walking_span.right /-- A pair of morphisms `h : W ⟶ X` and `k : W ⟶ Y` satisfying `h ≫ f = k ≫ g` induces a morphism `pullback.lift : W ⟶ pullback f g`. -/ def pullback.lift {C : Type u} [category C] {W : C} {X : C} {Y : C} {Z : C} {f : X ⟶ Z} {g : Y ⟶ Z} [has_pullback f g] (h : W ⟶ X) (k : W ⟶ Y) (w : h ≫ f = k ≫ g) : W ⟶ pullback f g := limit.lift (cospan f g) (pullback_cone.mk h k w) /-- A pair of morphisms `h : Y ⟶ W` and `k : Z ⟶ W` satisfying `f ≫ h = g ≫ k` induces a morphism `pushout.desc : pushout f g ⟶ W`. -/ def pushout.desc {C : Type u} [category C] {W : C} {X : C} {Y : C} {Z : C} {f : X ⟶ Y} {g : X ⟶ Z} [has_pushout f g] (h : Y ⟶ W) (k : Z ⟶ W) (w : f ≫ h = g ≫ k) : pushout f g ⟶ W := colimit.desc (span f g) (pushout_cocone.mk h k w) @[simp] theorem pullback.lift_fst_assoc {C : Type u} [category C] {W : C} {X : C} {Y : C} {Z : C} {f : X ⟶ Z} {g : Y ⟶ Z} [has_pullback f g] (h : W ⟶ X) (k : W ⟶ Y) (w : h ≫ f = k ≫ g) {X' : C} (f' : X ⟶ X') : pullback.lift h k w ≫ pullback.fst ≫ f' = h ≫ f' := sorry @[simp] theorem pullback.lift_snd_assoc {C : Type u} [category C] {W : C} {X : C} {Y : C} {Z : C} {f : X ⟶ Z} {g : Y ⟶ Z} [has_pullback f g] (h : W ⟶ X) (k : W ⟶ Y) (w : h ≫ f = k ≫ g) {X' : C} (f' : Y ⟶ X') : pullback.lift h k w ≫ pullback.snd ≫ f' = k ≫ f' := sorry @[simp] theorem pushout.inl_desc_assoc {C : Type u} [category C] {W : C} {X : C} {Y : C} {Z : C} {f : X ⟶ Y} {g : X ⟶ Z} [has_pushout f g] (h : Y ⟶ W) (k : Z ⟶ W) (w : f ≫ h = g ≫ k) {X' : C} (f' : W ⟶ X') : pushout.inl ≫ pushout.desc h k w ≫ f' = h ≫ f' := sorry @[simp] theorem pushout.inr_desc_assoc {C : Type u} [category C] {W : C} {X : C} {Y : C} {Z : C} {f : X ⟶ Y} {g : X ⟶ Z} [has_pushout f g] (h : Y ⟶ W) (k : Z ⟶ W) (w : f ≫ h = g ≫ k) {X' : C} (f' : W ⟶ X') : pushout.inr ≫ pushout.desc h k w ≫ f' = k ≫ f' := sorry /-- A pair of morphisms `h : W ⟶ X` and `k : W ⟶ Y` satisfying `h ≫ f = k ≫ g` induces a morphism `l : W ⟶ pullback f g` such that `l ≫ pullback.fst = h` and `l ≫ pullback.snd = k`. -/ def pullback.lift' {C : Type u} [category C] {W : C} {X : C} {Y : C} {Z : C} {f : X ⟶ Z} {g : Y ⟶ Z} [has_pullback f g] (h : W ⟶ X) (k : W ⟶ Y) (w : h ≫ f = k ≫ g) : Subtype fun (l : W ⟶ pullback f g) => l ≫ pullback.fst = h ∧ l ≫ pullback.snd = k := { val := pullback.lift h k w, property := sorry } /-- A pair of morphisms `h : Y ⟶ W` and `k : Z ⟶ W` satisfying `f ≫ h = g ≫ k` induces a morphism `l : pushout f g ⟶ W` such that `pushout.inl ≫ l = h` and `pushout.inr ≫ l = k`. -/ def pullback.desc' {C : Type u} [category C] {W : C} {X : C} {Y : C} {Z : C} {f : X ⟶ Y} {g : X ⟶ Z} [has_pushout f g] (h : Y ⟶ W) (k : Z ⟶ W) (w : f ≫ h = g ≫ k) : Subtype fun (l : pushout f g ⟶ W) => pushout.inl ≫ l = h ∧ pushout.inr ≫ l = k := { val := pushout.desc h k w, property := sorry } theorem pullback.condition {C : Type u} [category C] {X : C} {Y : C} {Z : C} {f : X ⟶ Z} {g : Y ⟶ Z} [has_pullback f g] : pullback.fst ≫ f = pullback.snd ≫ g := pullback_cone.condition (limit.cone (cospan f g)) theorem pushout.condition_assoc {C : Type u} [category C] {X : C} {Y : C} {Z : C} {f : X ⟶ Y} {g : X ⟶ Z} [has_pushout f g] {X' : C} (f' : pushout f g ⟶ X') : f ≫ pushout.inl ≫ f' = g ≫ pushout.inr ≫ f' := sorry /-- Two morphisms into a pullback are equal if their compositions with the pullback morphisms are equal -/ theorem pullback.hom_ext {C : Type u} [category C] {X : C} {Y : C} {Z : C} {f : X ⟶ Z} {g : Y ⟶ Z} [has_pullback f g] {W : C} {k : W ⟶ pullback f g} {l : W ⟶ pullback f g} (h₀ : k ≫ pullback.fst = l ≫ pullback.fst) (h₁ : k ≫ pullback.snd = l ≫ pullback.snd) : k = l := limit.hom_ext (pullback_cone.equalizer_ext (limit.cone (cospan f g)) h₀ h₁) /-- The pullback cone built from the pullback projections is a pullback. -/ def pullback_is_pullback {C : Type u} [category C] {X : C} {Y : C} {Z : C} (f : X ⟶ Z) (g : Y ⟶ Z) [has_pullback f g] : is_limit (pullback_cone.mk pullback.fst pullback.snd pullback.condition) := pullback_cone.is_limit.mk pullback.condition (fun (s : pullback_cone f g) => pullback.lift (pullback_cone.fst s) (pullback_cone.snd s) (pullback_cone.condition s)) sorry sorry sorry /-- The pullback of a monomorphism is a monomorphism -/ protected instance pullback.fst_of_mono {C : Type u} [category C] {X : C} {Y : C} {Z : C} {f : X ⟶ Z} {g : Y ⟶ Z} [has_pullback f g] [mono g] : mono pullback.fst := sorry /-- The pullback of a monomorphism is a monomorphism -/ protected instance pullback.snd_of_mono {C : Type u} [category C] {X : C} {Y : C} {Z : C} {f : X ⟶ Z} {g : Y ⟶ Z} [has_pullback f g] [mono f] : mono pullback.snd := sorry /-- Two morphisms out of a pushout are equal if their compositions with the pushout morphisms are equal -/ theorem pushout.hom_ext {C : Type u} [category C] {X : C} {Y : C} {Z : C} {f : X ⟶ Y} {g : X ⟶ Z} [has_pushout f g] {W : C} {k : pushout f g ⟶ W} {l : pushout f g ⟶ W} (h₀ : pushout.inl ≫ k = pushout.inl ≫ l) (h₁ : pushout.inr ≫ k = pushout.inr ≫ l) : k = l := colimit.hom_ext (pushout_cocone.coequalizer_ext (colimit.cocone (span f g)) h₀ h₁) /-- The pushout of an epimorphism is an epimorphism -/ protected instance pushout.inl_of_epi {C : Type u} [category C] {X : C} {Y : C} {Z : C} {f : X ⟶ Y} {g : X ⟶ Z} [has_pushout f g] [epi g] : epi pushout.inl := epi.mk fun (W : C) (u v : pushout f g ⟶ W) (h : pushout.inl ≫ u = pushout.inl ≫ v) => pushout.hom_ext h (iff.mp (cancel_epi g) (eq.mpr (id (Eq.trans ((fun (a a_1 : X ⟶ W) (e_1 : a = a_1) (ᾰ ᾰ_1 : X ⟶ W) (e_2 : ᾰ = ᾰ_1) => congr (congr_arg Eq e_1) e_2) (g ≫ pushout.inr ≫ u) (f ≫ pushout.inl ≫ v) (Eq.trans (Eq.symm (pushout.condition_assoc u)) ((fun (ᾰ ᾰ_1 : X ⟶ Y) (e_1 : ᾰ = ᾰ_1) (ᾰ_2 ᾰ_3 : Y ⟶ W) (e_2 : ᾰ_2 = ᾰ_3) => congr (congr_arg category_struct.comp e_1) e_2) f f (Eq.refl f) (pushout.inl ≫ u) (pushout.inl ≫ v) h)) (g ≫ pushout.inr ≫ v) (f ≫ pushout.inl ≫ v) (Eq.symm (pushout.condition_assoc v))) (propext (eq_self_iff_true (f ≫ pushout.inl ≫ v))))) trivial)) /-- The pushout of an epimorphism is an epimorphism -/ protected instance pushout.inr_of_epi {C : Type u} [category C] {X : C} {Y : C} {Z : C} {f : X ⟶ Y} {g : X ⟶ Z} [has_pushout f g] [epi f] : epi pushout.inr := epi.mk fun (W : C) (u v : pushout f g ⟶ W) (h : pushout.inr ≫ u = pushout.inr ≫ v) => pushout.hom_ext (iff.mp (cancel_epi f) (eq.mpr (id (Eq.trans ((fun (a a_1 : X ⟶ W) (e_1 : a = a_1) (ᾰ ᾰ_1 : X ⟶ W) (e_2 : ᾰ = ᾰ_1) => congr (congr_arg Eq e_1) e_2) (f ≫ pushout.inl ≫ u) (g ≫ pushout.inr ≫ v) (Eq.trans (pushout.condition_assoc u) ((fun (ᾰ ᾰ_1 : X ⟶ Z) (e_1 : ᾰ = ᾰ_1) (ᾰ_2 ᾰ_3 : Z ⟶ W) (e_2 : ᾰ_2 = ᾰ_3) => congr (congr_arg category_struct.comp e_1) e_2) g g (Eq.refl g) (pushout.inr ≫ u) (pushout.inr ≫ v) h)) (f ≫ pushout.inl ≫ v) (g ≫ pushout.inr ≫ v) (pushout.condition_assoc v)) (propext (eq_self_iff_true (g ≫ pushout.inr ≫ v))))) trivial)) h /-- The comparison morphism for the pullback of `f,g`. This is an isomorphism iff `G` preserves the pullback of `f,g`; see `category_theory/limits/preserves/shapes/pullbacks.lean` -/ def pullback_comparison {C : Type u} [category C] {X : C} {Y : C} {Z : C} {D : Type u₂} [category D] (G : C ⥤ D) (f : X ⟶ Z) (g : Y ⟶ Z) [has_pullback f g] [has_pullback (functor.map G f) (functor.map G g)] : functor.obj G (pullback f g) ⟶ pullback (functor.map G f) (functor.map G g) := pullback.lift (functor.map G pullback.fst) (functor.map G pullback.snd) sorry @[simp] theorem pullback_comparison_comp_fst_assoc {C : Type u} [category C] {X : C} {Y : C} {Z : C} {D : Type u₂} [category D] (G : C ⥤ D) (f : X ⟶ Z) (g : Y ⟶ Z) [has_pullback f g] [has_pullback (functor.map G f) (functor.map G g)] {X' : D} (f' : functor.obj G X ⟶ X') : pullback_comparison G f g ≫ pullback.fst ≫ f' = functor.map G pullback.fst ≫ f' := sorry @[simp] theorem pullback_comparison_comp_snd_assoc {C : Type u} [category C] {X : C} {Y : C} {Z : C} {D : Type u₂} [category D] (G : C ⥤ D) (f : X ⟶ Z) (g : Y ⟶ Z) [has_pullback f g] [has_pullback (functor.map G f) (functor.map G g)] {X' : D} (f' : functor.obj G Y ⟶ X') : pullback_comparison G f g ≫ pullback.snd ≫ f' = functor.map G pullback.snd ≫ f' := sorry @[simp] theorem map_lift_pullback_comparison_assoc {C : Type u} [category C] {X : C} {Y : C} {Z : C} {D : Type u₂} [category D] (G : C ⥤ D) (f : X ⟶ Z) (g : Y ⟶ Z) [has_pullback f g] [has_pullback (functor.map G f) (functor.map G g)] {W : C} {h : W ⟶ X} {k : W ⟶ Y} (w : h ≫ f = k ≫ g) {X' : D} (f' : pullback (functor.map G f) (functor.map G g) ⟶ X') : functor.map G (pullback.lift h k w) ≫ pullback_comparison G f g ≫ f' = pullback.lift (functor.map G h) (functor.map G k) (eq.mpr (id ((fun (a a_1 : functor.obj G W ⟶ functor.obj G Z) (e_1 : a = a_1) (ᾰ ᾰ_1 : functor.obj G W ⟶ functor.obj G Z) (e_2 : ᾰ = ᾰ_1) => congr (congr_arg Eq e_1) e_2) (functor.map G h ≫ functor.map G f) (functor.map G (k ≫ g)) (Eq.trans (Eq.symm (functor.map_comp G h f)) ((fun (c : C ⥤ D) {X Y : C} (ᾰ ᾰ_1 : X ⟶ Y) (e_4 : ᾰ = ᾰ_1) => congr_arg (functor.map c) e_4) G (h ≫ f) (k ≫ g) w)) (functor.map G k ≫ functor.map G g) (functor.map G (k ≫ g)) (Eq.symm (functor.map_comp G k g)))) (Eq.refl (functor.map G (k ≫ g)))) ≫ f' := sorry /-- `has_pullbacks` represents a choice of pullback for every pair of morphisms See https://stacks.math.columbia.edu/tag/001W. -/ def has_pullbacks (C : Type u) [category C] := has_limits_of_shape walking_cospan C /-- `has_pushouts` represents a choice of pushout for every pair of morphisms -/ def has_pushouts (C : Type u) [category C] := has_colimits_of_shape walking_span C /-- If `C` has all limits of diagrams `cospan f g`, then it has all pullbacks -/ theorem has_pullbacks_of_has_limit_cospan (C : Type u) [category C] [∀ {X Y Z : C} {f : X ⟶ Z} {g : Y ⟶ Z}, has_limit (cospan f g)] : has_pullbacks C := has_limits_of_shape.mk fun (F : walking_cospan ⥤ C) => has_limit_of_iso (iso.symm (diagram_iso_cospan F)) /-- If `C` has all colimits of diagrams `span f g`, then it has all pushouts -/ theorem has_pushouts_of_has_colimit_span (C : Type u) [category C] [∀ {X Y Z : C} {f : X ⟶ Y} {g : X ⟶ Z}, has_colimit (span f g)] : has_pushouts C := has_colimits_of_shape.mk fun (F : walking_span ⥤ C) => has_colimit_of_iso (diagram_iso_span F)
45fea05dc28b12026b5045b01cd03c765ae4fb2c
c777c32c8e484e195053731103c5e52af26a25d1
/src/number_theory/modular_forms/slash_actions.lean
9c980afa75e783b83ff3cefce2ce7bff9408845a
[ "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
9,593
lean
/- Copyright (c) 2022 Chris Birkbeck. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Birkbeck -/ import analysis.complex.upper_half_plane.basic import linear_algebra.matrix.general_linear_group import linear_algebra.matrix.special_linear_group /-! # Slash actions This file defines a class of slash actions, which are families of right actions of a given group parametrized by some Type. This is modeled on the slash action of `GL_pos (fin 2) ℝ` on the space of modular forms. ## Notation In the `modular_form` locale, this provides * `f ∣[k;γ] A`: the `k`th `γ`-compatible slash action by `A` on `f` * `f ∣[k] A`: the `k`th `ℂ`-compatible slash action by `A` on `f`; a shorthand for `f ∣[k;ℂ] A` -/ open complex upper_half_plane open_locale upper_half_plane local prefix `↑ₘ`:1024 := @coe _ (matrix (fin 2) (fin 2) _) _ -- like `↑ₘ`, but allows the user to specify the ring `R`. Useful to help Lean elaborate. local notation `↑ₘ[` R `]` := @coe _ (matrix (fin 2) (fin 2) R) _ local notation `GL(` n `, ` R `)`⁺ := matrix.GL_pos (fin n) R local notation `SL(` n `, ` R `)` := matrix.special_linear_group (fin n) R /--A general version of the slash action of the space of modular forms.-/ class slash_action (β G α γ : Type*) [group G] [add_monoid α] [has_smul γ α] := (map : β → G → α → α) (zero_slash : ∀ (k : β) (g : G), map k g 0 = 0) (slash_one : ∀ (k : β) (a : α) , map k 1 a = a) (slash_mul : ∀ (k : β) (g h : G) (a : α), map k (g * h) a =map k h (map k g a)) (smul_slash : ∀ (k : β) (g : G) (a : α) (z : γ), map k g (z • a) = z • (map k g a)) (add_slash : ∀ (k : β) (g : G) (a b : α), map k g (a + b) = map k g a + map k g b) localized "notation (name := modular_form.slash) f ` ∣[`:100 k `;` γ `] `:0 a :100 := slash_action.map γ k a f" in modular_form localized "notation (name := modular_form.slash_complex) f ` ∣[`:100 k `] `:0 a :100 := slash_action.map ℂ k a f" in modular_form @[simp] lemma slash_action.neg_slash {β G α γ : Type*} [group G] [add_group α] [has_smul γ α] [slash_action β G α γ] (k : β) (g : G) (a : α) : (-a) ∣[k;γ] g = - (a ∣[k;γ] g) := eq_neg_of_add_eq_zero_left $ by rw [←slash_action.add_slash, add_left_neg, slash_action.zero_slash] @[simp] lemma slash_action.smul_slash_of_tower {R β G α : Type*} (γ : Type*) [group G] [add_group α] [monoid γ] [mul_action γ α] [has_smul R γ] [has_smul R α] [is_scalar_tower R γ α] [slash_action β G α γ] (k : β) (g : G) (a : α) (r : R) : (r • a) ∣[k;γ] g = r • (a ∣[k;γ] g) := by rw [←smul_one_smul γ r a, slash_action.smul_slash, smul_one_smul] attribute [simp] slash_action.zero_slash slash_action.slash_one slash_action.smul_slash slash_action.add_slash /--Slash_action induced by a monoid homomorphism.-/ def monoid_hom_slash_action {β G H α γ : Type*} [group G] [add_monoid α] [has_smul γ α] [group H] [slash_action β G α γ] (h : H →* G) : slash_action β H α γ := { map := λ k g, slash_action.map γ k (h g), zero_slash := λ k g, slash_action.zero_slash k (h g), slash_one := λ k a, by simp only [map_one, slash_action.slash_one], slash_mul := λ k g gg a, by simp only [map_mul, slash_action.slash_mul], smul_slash := λ _ _, slash_action.smul_slash _ _, add_slash := λ _ g _ _, slash_action.add_slash _ (h g) _ _,} namespace modular_form noncomputable theory /--The weight `k` action of `GL(2, ℝ)⁺` on functions `f : ℍ → ℂ`. -/ def slash (k : ℤ) (γ : GL(2, ℝ)⁺) (f : ℍ → ℂ) (x : ℍ) : ℂ := f (γ • x) * (((↑ₘ γ).det) : ℝ)^(k-1) * (upper_half_plane.denom γ x)^(-k) variables {Γ : subgroup SL(2, ℤ)} {k: ℤ} (f : ℍ → ℂ) section -- temporary notation until the instance is built local notation f ` ∣[`:100 k `]`:0 γ :100 := modular_form.slash k γ f private lemma slash_mul (k : ℤ) (A B : GL(2, ℝ)⁺) (f : ℍ → ℂ) : f ∣[k] (A * B) = (f ∣[k] A) ∣[k] B := begin ext1, simp_rw [slash,(upper_half_plane.denom_cocycle A B x)], have e3 : (A * B) • x = A • B • x , by { convert (upper_half_plane.mul_smul' A B x), } , rw e3, simp only [upper_half_plane.num, upper_half_plane.denom, of_real_mul, subgroup.coe_mul, coe_coe, upper_half_plane.coe_smul, units.coe_mul, matrix.mul_eq_mul, matrix.det_mul, upper_half_plane.smul_aux, upper_half_plane.smul_aux', subtype.coe_mk] at *, field_simp, have : (((↑(↑A : GL (fin 2) ℝ) : (matrix (fin 2) (fin 2) ℝ)).det : ℂ) * ((↑(↑B : GL (fin 2) ℝ) : (matrix (fin 2) (fin 2) ℝ)).det : ℂ))^(k-1) = ((↑(↑A : GL (fin 2) ℝ) : (matrix (fin 2) (fin 2) ℝ)).det : ℂ)^(k-1) * ((↑(↑B : GL (fin 2) ℝ) : (matrix (fin 2) (fin 2) ℝ)).det : ℂ)^(k-1) , by {simp_rw [←mul_zpow]}, simp_rw [this, ← mul_assoc, ←mul_zpow], end private lemma add_slash (k : ℤ) (A : GL(2, ℝ)⁺) (f g : ℍ → ℂ) : (f + g) ∣[k] A = (f ∣[k] A) + (g ∣[k] A) := begin ext1, simp only [slash, pi.add_apply, denom, coe_coe, zpow_neg], ring, end private lemma slash_one (k : ℤ) (f : ℍ → ℂ) : (f ∣[k] 1) = f := funext $ by simp [slash] variables {α : Type*} [has_smul α ℂ] [is_scalar_tower α ℂ ℂ] private lemma smul_slash (k : ℤ) (A : GL(2, ℝ)⁺) (f : ℍ → ℂ) (c : α) : (c • f) ∣[k] A = c • (f ∣[k] A) := begin simp_rw [←smul_one_smul ℂ c f, ←smul_one_smul ℂ c (f ∣[k] A)], ext1, simp_rw slash, simp only [slash, algebra.id.smul_eq_mul, matrix.general_linear_group.coe_det_apply, pi.smul_apply, subtype.val_eq_coe, coe_coe], ring, end private lemma zero_slash (k : ℤ) (A : GL(2, ℝ)⁺) : (0 : ℍ → ℂ) ∣[k] A = 0 := funext $ λ _, by simp only [slash, pi.zero_apply, zero_mul] instance : slash_action ℤ GL(2, ℝ)⁺ (ℍ → ℂ) ℂ := { map := slash, zero_slash := zero_slash, slash_one := slash_one, slash_mul := slash_mul, smul_slash := smul_slash, add_slash := add_slash } end lemma slash_def (A : GL(2, ℝ)⁺) : f ∣[k] A = slash k A f := rfl instance subgroup_action (Γ : subgroup SL(2, ℤ)) : slash_action ℤ Γ (ℍ → ℂ) ℂ := monoid_hom_slash_action (monoid_hom.comp (matrix.special_linear_group.to_GL_pos) (monoid_hom.comp (matrix.special_linear_group.map (int.cast_ring_hom ℝ)) (subgroup.subtype Γ))) @[simp] lemma subgroup_slash (Γ : subgroup SL(2, ℤ)) (γ : Γ): (f ∣[k] γ) = f ∣[k] (γ : GL(2,ℝ)⁺) := rfl instance SL_action : slash_action ℤ SL(2, ℤ) (ℍ → ℂ) ℂ := monoid_hom_slash_action (monoid_hom.comp (matrix.special_linear_group.to_GL_pos) (matrix.special_linear_group.map (int.cast_ring_hom ℝ))) @[simp] lemma SL_slash (γ : SL(2, ℤ)): f ∣[k] γ = f ∣[k] (γ : GL(2,ℝ)⁺) := rfl /-- The constant function 1 is invariant under any element of `SL(2, ℤ)`. -/ @[simp] lemma is_invariant_one (A : SL(2, ℤ)) : (1 : ℍ → ℂ) ∣[(0 : ℤ)] A = (1 : ℍ → ℂ) := begin have : (((↑ₘ(A : GL(2,ℝ)⁺)).det) : ℝ) = 1, { simp only [coe_coe, matrix.special_linear_group.coe_GL_pos_coe_GL_coe_matrix, matrix.special_linear_group.det_coe], }, funext, rw [SL_slash, slash_def, slash, zero_sub, this], simp, end /-- A function `f : ℍ → ℂ` is `slash_invariant`, of weight `k ∈ ℤ` and level `Γ`, if for every matrix `γ ∈ Γ` we have `f(γ • z)= (c*z+d)^k f(z)` where `γ= ![![a, b], ![c, d]]`, and it acts on `ℍ` via Möbius transformations. -/ lemma slash_action_eq'_iff (k : ℤ) (Γ : subgroup SL(2, ℤ)) (f : ℍ → ℂ) (γ : Γ) (z : ℍ) : (f ∣[k] γ) z = f z ↔ f (γ • z) = ((↑ₘ[ℤ]γ 1 0 : ℂ) * z + (↑ₘ[ℤ]γ 1 1 : ℂ))^k * f z := begin simp only [subgroup_slash, slash_def, modular_form.slash], convert inv_mul_eq_iff_eq_mul₀ _ using 2, { rw mul_comm, simp only [denom, coe_coe, matrix.special_linear_group.coe_GL_pos_coe_GL_coe_matrix, zpow_neg, matrix.special_linear_group.det_coe, of_real_one, one_zpow, mul_one, subgroup_to_sl_moeb, sl_moeb], refl, }, { convert zpow_ne_zero k (denom_ne_zero γ z) }, end lemma mul_slash (k1 k2 : ℤ) (A : GL(2, ℝ)⁺) (f g : ℍ → ℂ) : (f * g) ∣[k1 + k2] A = (((↑ₘ A).det) : ℝ) • (f ∣[k1] A) * (g ∣[k2] A) := begin ext1, simp only [slash_def, slash, matrix.general_linear_group.coe_det_apply, subtype.val_eq_coe, pi.mul_apply, pi.smul_apply, algebra.smul_mul_assoc, real_smul], set d : ℂ := ↑((↑ₘ A).det : ℝ), have h1 : d ^ (k1 + k2 - 1) = d * d ^ (k1 - 1) * d ^ (k2 - 1), { have : d ≠ 0, { dsimp [d], norm_cast, exact matrix.GL_pos.det_ne_zero A }, rw [← zpow_one_add₀ this, ← zpow_add₀ this], ring_exp }, have h22 : denom A x ^ (- (k1 + k2)) = denom A x ^ (- k1) * denom A x ^ (- k2), { rw [int.neg_add, zpow_add₀], exact upper_half_plane.denom_ne_zero A x, }, rw [h1, h22], ring, end @[simp] lemma mul_slash_SL2 (k1 k2 : ℤ) (A : SL(2, ℤ)) (f g : ℍ → ℂ) : (f * g) ∣[k1 + k2] A = (f ∣[k1] A) * (g ∣[k2] A) := calc (f * g) ∣[k1 + k2] (A : GL(2, ℝ)⁺) = _ • (f ∣[k1] A) * (g ∣[k2] A) : mul_slash _ _ _ _ _ ... = (1:ℝ) • (f ∣[k1] A) * (g ∣[k2] A) : by simp [-matrix.special_linear_group.coe_matrix_coe] ... = (f ∣[k1] A) * (g ∣[k2] A) : by simp lemma mul_slash_subgroup (k1 k2 : ℤ) (Γ : subgroup SL(2, ℤ)) (A : Γ) (f g : ℍ → ℂ) : (f * g) ∣[k1 + k2] A = (f ∣[k1] A) * (g ∣[k2] A) := mul_slash_SL2 k1 k2 A f g end modular_form
1d8d901fdb848a0ebc23877c5be779223a0330eb
74d9d5f45c6ce5c4f2faf215c04a68eab55fe525
/src/continuous_linear_map.lean
083bbb8587e694c0ef9bab644fbbbda44a1f6613
[]
no_license
joshpoll/differential_geometry
290bb8a934ca3b3b6b707d810e6d4b941710b710
57e00a7e37b7c4c73c847429171ff63d3a48def5
refs/heads/master
1,584,551,626,391
1,527,747,643,000
1,527,747,643,000
135,014,993
1
0
null
null
null
null
UTF-8
Lean
false
false
6,588
lean
/- Continuous Linear Maps These are a well-behaved subset of all linear maps. In finite dimensional normed vector spaces, all linear maps are continuous. For a certain type of normed space (which?), continuous linear maps and bounded linear maps are the same. The derivative of f : E → F is f' : E → continuous_linear_map E F. -/ import differentiability.normed_space universes u v w x -- TODO: maybe continuous should be a structure and not a regular prop structure is_continuous_linear_map {k : Type u} {E : Type v} {F : Type w} [normed_field k] [normed_space k E] [normed_space k F] (L : E → F) extends is_linear_map L : Prop := (continuous : continuous L) namespace is_continuous_linear_map variables {k : Type u} {E : Type v} {F : Type w} {G : Type x} variables [normed_field k] [normed_space k E] [normed_space k F] [normed_space k G] variable {L : E → F} include k section variable (hL : is_continuous_linear_map L) include hL -- linear map simp lemmas -- TODO: should there be an smul lemma @[simp] lemma zero : L 0 = 0 := hL.to_is_linear_map.zero @[simp] lemma neg (v : E) : L (- v) = - L v := hL.to_is_linear_map.neg _ @[simp] lemma sub (v w : E) : L (v - w) = L v - L w := hL.to_is_linear_map.sub _ _ @[simp] lemma sum {ι : Type x} {t : finset ι} {f : ι → E} : L (t.sum f) = t.sum (λi, L (f i)) := hL.to_is_linear_map.sum end -- TODO: is_linear_map and continuous have different order conventions for this theorem. adopting is_linear_map's lemma comp {M : G → E} : is_continuous_linear_map L → is_continuous_linear_map M → is_continuous_linear_map (L ∘ M) | ⟨L_lin, L_cont⟩ ⟨M_lin, M_cont⟩ := ⟨is_linear_map.comp L_lin M_lin, continuous.comp M_cont L_cont⟩ lemma id : is_continuous_linear_map (id : E → E) := ⟨is_linear_map.id, continuous_id⟩ -- no inverse thm except for special circumstances lemma map_zero : is_continuous_linear_map (λv, 0 : E → F) := ⟨is_linear_map.map_zero, continuous_const⟩ -- TODO: could move hypothesis to the left if continuous was a structure b/c then I could use to_is_continuous lemma map_neg : is_continuous_linear_map L → is_continuous_linear_map (λv, - L v) | ⟨lin, cont⟩ := ⟨is_linear_map.map_neg lin, continuous_neg cont⟩ lemma map_add {M : E → F} : is_continuous_linear_map L → is_continuous_linear_map M → is_continuous_linear_map (λv, L v + M v) | ⟨L_lin, L_cont⟩ ⟨M_lin, M_cont⟩ := ⟨is_linear_map.map_add L_lin M_lin, continuous_add L_cont M_cont⟩ -- TODO: I don't understand this lemma so I don't want to translate it /- lemma map_sum [decidable_eq δ] {t : finset δ} {f : δ → β → γ} : (∀d∈t, is_linear_map (f d)) → is_linear_map (λb, t.sum $ λd, f d b) -/ lemma map_sub {M : E → F} : is_continuous_linear_map L → is_continuous_linear_map M → is_continuous_linear_map (λv, L v - M v) | ⟨L_lin, L_cont⟩ ⟨M_lin, M_cont⟩ := ⟨is_linear_map.map_sub L_lin M_lin, continuous_sub L_cont M_cont⟩ -- TODO: this requires topological vector spaces lemma map_smul_right {c : k} : is_continuous_linear_map L → is_continuous_linear_map (λv, c • L v) | ⟨lin, cont⟩ := ⟨is_linear_map.map_smul_right lin, sorry⟩ -- TODO: this requires topological vector spaces and normed_field.to_normed_space (see ring.to_module in module.lean) lemma map_smul_left {L : E → k} {v : F} : is_continuous_linear_map L → is_continuous_linear_map (λc, L c • v) | ⟨lin, cont⟩ := sorry end is_continuous_linear_map -- begins diverging from homeos approach -- draw from linear_map_module and poly -- TODO: convert poly-like theorems def continuous_linear_map {k : Type u} (E : Type v) (F : Type w) [normed_field k] [normed_space k E] [normed_space k F] := subtype (@is_continuous_linear_map k E F _ _ _) namespace continuous_linear_map variables {k : Type u} {E : Type v} {F : Type w} {G : Type x} variables [normed_field k] [normed_space k E] [normed_space k F] [normed_space k G] variables {c : k} {v w : E} {L M : continuous_linear_map E F} include k instance : has_coe_to_fun (continuous_linear_map E F) := ⟨_, subtype.val⟩ @[extensionality] theorem ext {M : continuous_linear_map E F} (h : ∀ v, L v = M v) : L = M := subtype.eq $ funext h lemma is_clm (L : continuous_linear_map E F) : is_continuous_linear_map L := L.property def subst (L : continuous_linear_map E F) (M : E → F) (e : ∀ v, L v = M v) : continuous_linear_map E F := ⟨M, by rw ← (funext e : coe_fn L = M); exact L.is_clm⟩ def comp (L :continuous_linear_map F G) (M : continuous_linear_map E F) : continuous_linear_map E G := ⟨λv, L (M v), is_continuous_linear_map.comp L.is_clm M.is_clm⟩ @[simp] lemma map_add : L (v + w) = L v + L w := L.is_clm.add v w @[simp] lemma map_zero : L 0 = 0 := L.is_clm.zero @[simp] lemma map_smul : L (c • v) = c • L v := L.is_clm.smul c v @[simp] lemma map_neg : L (-v) = -L v := L.is_clm.neg _ @[simp] lemma map_sub : L (v - w) = L v - L w := L.is_clm.sub _ _ section add_comm_group def add : continuous_linear_map E F → continuous_linear_map E F → continuous_linear_map E F := λ L M, ⟨L + M, is_continuous_linear_map.map_add L.is_clm M.is_clm⟩ def zero : continuous_linear_map E F := ⟨λv, 0, is_continuous_linear_map.map_zero⟩ def neg : continuous_linear_map E F → continuous_linear_map E F := λ L, ⟨λv, -(L v), is_continuous_linear_map.map_neg L.is_clm⟩ instance : has_add (continuous_linear_map E F) := ⟨add⟩ instance : has_zero (continuous_linear_map E F) := ⟨zero⟩ instance : has_neg (continuous_linear_map E F) := ⟨neg⟩ @[simp] lemma add_app : (L + M) v = L v + M v := rfl @[simp] lemma zero_app : (0 : continuous_linear_map E F) v = 0 := rfl @[simp] lemma neg_app : (-L) v = -L v := rfl instance : add_comm_group (continuous_linear_map E F) := by refine {add := (+), zero := 0, neg := has_neg.neg, ..}; { intros, apply ext, simp } end add_comm_group section module -- TODO: need to prove topological vector space stuff def smul : k → continuous_linear_map E F → continuous_linear_map E F := λ c L, ⟨λ v, c•(L v), is_continuous_linear_map.smul_right c L.is_clm⟩ instance : has_scalar k (continuous_linear_map E F) := ⟨smul⟩ @[simp] lemma smul_app : (c • L) v = c • (L v) := rfl instance : module k (continuous_linear_map E F) := by refine {smul := (•), ..continuous_linear_map.add_comm_group, ..}; { intros, apply ext, simp [smul_add, add_smul, mul_smul] } end module section metric_space -- TODO! end metric_space section normed_space -- TODO! end normed_space end continuous_linear_map
e919c9cba657b7516088bbe9df551ad5d6c19698
8153b09016144aaad2ec06668e8d34a353e80572
/src/quiz05.lean
a5ab7ddce44d8c7396f7285212cc89f6e74a87fc
[]
no_license
UVM-M52/quiz-5-danijumps
13e50b433e05b66bb92426ad3c1f7d6933a4bf0d
387989e688a9d321713d64735d27a0d9fbac3a30
refs/heads/master
1,614,294,343,679
1,583,514,357,000
1,583,514,357,000
245,475,409
0
0
null
null
null
null
UTF-8
Lean
false
false
663
lean
-- Math 52: Quiz 5 -- Open this file in a folder that contains 'utils'. import utils open classical definition divides (a b : ℤ) : Prop := ∃ (k : ℤ), b = a * k local infix ∣ := divides axiom not_3_divides : ∀ (m : ℤ), ¬ (3 ∣ m) ↔ 3 ∣ m - 1 ∨ 3 ∣ m + 1 lemma not_3_divides_of_3_divides_minus_1 : ∀ (m : ℤ), 3 ∣ m - 1 → ¬ (3 ∣ m) := begin intros m H, rw not_3_divides, left, assumption, end lemma not_3_divides_of_3_divides_plus_1 : ∀ (m : ℤ), 3 ∣ m + 1 → ¬ (3 ∣ m) := begin intros m H, rw not_3_divides, right, assumption, end theorem main : ∀ (n : ℤ), 3 ∣ n * n - 1 → ¬ (3 ∣ n) := begin sorry end
5fd073fa7356bdca4db84c36fe691ae9c1cf150a
6e44fda625e48340c6ffc7b1109a9e3b208e5384
/src/topology/exercies.lean
bbf862eb16cb49b99c50eccd56e80d8ab4edec48
[]
no_license
JasonKYi/learn_mspaces
9f998a265b907af6be6a54061637fcf1f6d1ee9d
54083e81da420d2d362a7024a8c86bea8529fe66
refs/heads/master
1,619,008,842,896
1,609,897,382,000
1,609,897,382,000
249,780,600
5
0
null
null
null
null
UTF-8
Lean
false
false
1,899
lean
import topology.theorems /- A topological space (X, 𝒯) consists of a non-empty set X together with a collection 𝒯 of subsets of X that satisfy - ∅ ∈ 𝒯, X ∈ 𝒯 - U, V ∈ 𝒯 → U ∩ V ∈ 𝒯 - Uᵢ ∈ 𝒯 → ⋃ᵢ U ∈ 𝒯 Elements of 𝒯 are called open sets in (X, 𝒯) and 𝒯 is called a topology on X. In Lean this is represented by: structure topological_space (α : Type u) := (is_open : set α → Prop) (is_open_univ : is_open univ) (is_open_inter : ∀s t, is_open s → is_open t → is_open (s ∩ t)) (is_open_sUnion : ∀s, (∀ t ∈ s, is_open t) → is_open (⋃₀ s)) -/ open definitions set variables {X : Type*} [topological_space X] variables {Y : Type*} [topological_space Y] variables {f : X → Y} (hcontin : is_continuous f) /- In this exercise, we prove that if the closure of U equals the closure of V and f is a continuous function, then the closure of f(U) equals the closure of f(V) -/ -- We first show that the image of the closure is smaller than the closure of the image -- This was proven in theorem.lean -- We then show that the closure of the image of the closure equals the closure of the image theorem closure_of_map_of_closure_eq_closure_of_map {U : set X} (hcontin : is_continuous f) : closure (f '' closure U) = closure (f '' U) := le_antisymm (λ x hx U' ⟨hU'₀, hU'₁⟩, hx U' ⟨hU'₀, subset.trans (mapping.map_closure_le_closure_map hcontin) (closed.closure_is_min hU'₁ hU'₀)⟩) (closed.closure_mono' $ image_subset f (closed.set_le_closure U)) -- With that, our question becomes trivial theorem eq_closure_map_closure_eq {U V : set X} (heq : closure U = closure V) (hcontin : is_continuous f): closure (f '' U) = closure (f '' V) := by rw [← closure_of_map_of_closure_eq_closure_of_map hcontin, heq, closure_of_map_of_closure_eq_closure_of_map hcontin]
b8e7e06b7b971dadf6d36516da76bfcd672a5950
e00ea76a720126cf9f6d732ad6216b5b824d20a7
/src/algebra/category/Group/basic.lean
4c57f89d318826911c5c23c7fbb4c2d448fb5f6f
[ "Apache-2.0" ]
permissive
vaibhavkarve/mathlib
a574aaf68c0a431a47fa82ce0637f0f769826bfe
17f8340912468f49bdc30acdb9a9fa02eeb0473a
refs/heads/master
1,621,263,802,637
1,585,399,588,000
1,585,399,588,000
250,833,447
0
0
Apache-2.0
1,585,410,341,000
1,585,410,341,000
null
UTF-8
Lean
false
false
8,467
lean
/- Copyright (c) 2018 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import algebra.punit_instances import algebra.category.Mon.basic import category_theory.endomorphism import category_theory.epi_mono /-! # Category instances for group, add_group, comm_group, and add_comm_group. We introduce the bundled categories: * `Group` * `AddGroup` * `CommGroup` * `AddCommGroup` along with the relevant forgetful functors between them, and to the bundled monoid categories. ## Implementation notes See the note [locally reducible category instances]. -/ universes u v open category_theory /-- The category of groups and group morphisms. -/ @[to_additive AddGroup] def Group : Type (u+1) := induced_category Mon (bundled.map group.to_monoid) namespace Group /-- Construct a bundled Group from the underlying type and typeclass. -/ @[to_additive] def of (X : Type u) [group X] : Group := bundled.of X local attribute [reducible] Group @[to_additive] instance : has_coe_to_sort Group := infer_instance -- short-circuit type class inference @[to_additive add_group] instance (G : Group) : group G := G.str @[to_additive] instance : has_one Group := ⟨Group.of punit⟩ @[to_additive] instance : inhabited Group := ⟨1⟩ @[to_additive] instance : unique (1 : Group.{u}) := { default := 1, uniq := λ a, begin cases a, refl, end } @[simp, to_additive] lemma one_apply (G H : Group) (g : G) : (1 : G ⟶ H) g = 1 := rfl @[to_additive] instance : concrete_category Group := infer_instance -- short-circuit type class inference @[to_additive,ext] lemma ext (G H : Group) (f₁ f₂ : G ⟶ H) (w : ∀ x, f₁ x = f₂ x) : f₁ = f₂ := by { ext1, apply w } attribute [ext] AddGroup.ext @[to_additive has_forget_to_AddMon] instance has_forget_to_Mon : has_forget₂ Group Mon := infer_instance -- short-circuit type class inference end Group /-- The category of commutative groups and group morphisms. -/ @[to_additive AddCommGroup] def CommGroup : Type (u+1) := induced_category Group (bundled.map comm_group.to_group) /-- `Ab` is an abbreviation for `AddCommGroup`, for the sake of mathematicians' sanity. -/ abbreviation Ab := AddCommGroup namespace CommGroup /-- Construct a bundled CommGroup from the underlying type and typeclass. -/ @[to_additive] def of (G : Type u) [comm_group G] : CommGroup := bundled.of G local attribute [reducible] CommGroup @[to_additive] instance : has_coe_to_sort CommGroup := infer_instance -- short-circuit type class inference @[to_additive add_comm_group_instance] instance comm_group_instance (G : CommGroup) : comm_group G := G.str @[to_additive] instance : has_one CommGroup := ⟨CommGroup.of punit⟩ @[to_additive] instance : inhabited CommGroup := ⟨1⟩ @[to_additive] instance : unique (1 : CommGroup.{u}) := { default := 1, uniq := λ a, begin cases a, refl, end } @[simp, to_additive] lemma one_apply (G H : CommGroup) (g : G) : (1 : G ⟶ H) g = 1 := rfl @[to_additive] instance : concrete_category CommGroup := infer_instance -- short-circuit type class inference @[to_additive,ext] lemma ext (G H : CommGroup) (f₁ f₂ : G ⟶ H) (w : ∀ x, f₁ x = f₂ x) : f₁ = f₂ := by { ext1, apply w } attribute [ext] AddCommGroup.ext @[to_additive has_forget_to_AddGroup] instance has_forget_to_Group : has_forget₂ CommGroup Group := infer_instance -- short-circuit type class inference @[to_additive has_forget_to_AddCommMon] instance has_forget_to_CommMon : has_forget₂ CommGroup CommMon := induced_category.has_forget₂ (λ G : CommGroup, CommMon.of G) end CommGroup namespace AddCommGroup /-- Any element of an abelian group gives a unique morphism from `ℤ` sending `1` to that element. -/ -- TODO allow other universe levels -- this will require writing a `ulift_instances.lean` file def as_hom {G : AddCommGroup.{0}} (g : G) : (AddCommGroup.of ℤ) ⟶ G := { to_fun := λ i : ℤ, i • g, map_zero' := rfl, map_add' := λ a b, gpow_add g a b } @[simp] lemma as_hom_apply {G : AddCommGroup.{0}} (g : G) (i : ℤ) : (as_hom g) i = i • g := rfl lemma as_hom_injective {G : AddCommGroup.{0}} : function.injective (@as_hom G) := λ h k w, by convert congr_arg (λ k : (AddCommGroup.of ℤ) ⟶ G, (k : ℤ → G) (1 : ℤ)) w; simp @[ext] lemma int_hom_ext {G : AddCommGroup.{0}} (f g : (AddCommGroup.of ℤ) ⟶ G) (w : f (1 : ℤ) = g (1 : ℤ)) : f = g := begin ext, change ℤ at x, rw ←gsmul_int_one x, rw [add_monoid_hom.map_gsmul, add_monoid_hom.map_gsmul, w], end -- TODO: this argument should be generalised to the situation where -- the forgetful functor is representable. lemma injective_of_mono {G H : AddCommGroup.{0}} (f : G ⟶ H) [mono f] : function.injective f := λ g₁ g₂ h, begin have t0 : as_hom g₁ ≫ f = as_hom g₂ ≫ f := begin ext, dsimp [as_hom], simpa using h, end, have t1 : as_hom g₁ = as_hom g₂ := (cancel_mono _).1 t0, apply as_hom_injective t1, end end AddCommGroup variables {X Y : Type u} /-- Build an isomorphism in the category `Group` from a `mul_equiv` between `group`s. -/ @[to_additive add_equiv.to_AddGroup_iso "Build an isomorphism in the category `AddGroup` from a `add_equiv` between `add_group`s."] def mul_equiv.to_Group_iso [group X] [group Y] (e : X ≃* Y) : Group.of X ≅ Group.of Y := { hom := e.to_monoid_hom, inv := e.symm.to_monoid_hom } attribute [simps] mul_equiv.to_Group_iso add_equiv.to_AddGroup_iso /-- Build an isomorphism in the category `CommGroup` from a `mul_equiv` between `comm_group`s. -/ @[to_additive add_equiv.to_AddCommGroup_iso "Build an isomorphism in the category `AddCommGroup` from a `add_equiv` between `add_comm_group`s."] def mul_equiv.to_CommGroup_iso [comm_group X] [comm_group Y] (e : X ≃* Y) : CommGroup.of X ≅ CommGroup.of Y := { hom := e.to_monoid_hom, inv := e.symm.to_monoid_hom } attribute [simps] mul_equiv.to_CommGroup_iso add_equiv.to_AddCommGroup_iso namespace category_theory.iso /-- Build a `mul_equiv` from an isomorphism in the category `Group`. -/ @[to_additive AddGroup_iso_to_add_equiv "Build an `add_equiv` from an isomorphism in the category `AddGroup`."] def Group_iso_to_mul_equiv {X Y : Group.{u}} (i : X ≅ Y) : X ≃* Y := { to_fun := i.hom, inv_fun := i.inv, left_inv := by tidy, right_inv := by tidy, map_mul' := by tidy }. attribute [simps] Group_iso_to_mul_equiv AddGroup_iso_to_add_equiv /-- Build a `mul_equiv` from an isomorphism in the category `CommGroup`. -/ @[to_additive AddCommGroup_iso_to_add_equiv "Build an `add_equiv` from an isomorphism in the category `AddCommGroup`."] def CommGroup_iso_to_mul_equiv {X Y : CommGroup.{u}} (i : X ≅ Y) : X ≃* Y := { to_fun := i.hom, inv_fun := i.inv, left_inv := by tidy, right_inv := by tidy, map_mul' := by tidy }. attribute [simps] CommGroup_iso_to_mul_equiv AddCommGroup_iso_to_add_equiv end category_theory.iso /-- multiplicative equivalences between `group`s are the same as (isomorphic to) isomorphisms in `Group` -/ @[to_additive add_equiv_iso_AddGroup_iso "additive equivalences between `add_group`s are the same as (isomorphic to) isomorphisms in `AddGroup`"] def mul_equiv_iso_Group_iso {X Y : Type u} [group X] [group Y] : (X ≃* Y) ≅ (Group.of X ≅ Group.of Y) := { hom := λ e, e.to_Group_iso, inv := λ i, i.Group_iso_to_mul_equiv, } /-- multiplicative equivalences between `comm_group`s are the same as (isomorphic to) isomorphisms in `CommGroup` -/ @[to_additive add_equiv_iso_AddCommGroup_iso "additive equivalences between `add_comm_group`s are the same as (isomorphic to) isomorphisms in `AddCommGroup`"] def mul_equiv_iso_CommGroup_iso {X Y : Type u} [comm_group X] [comm_group Y] : (X ≃* Y) ≅ (CommGroup.of X ≅ CommGroup.of Y) := { hom := λ e, e.to_CommGroup_iso, inv := λ i, i.CommGroup_iso_to_mul_equiv, } namespace category_theory.Aut /-- The (bundled) group of automorphisms of a type is isomorphic to the (bundled) group of permutations. -/ def iso_perm {α : Type u} : Group.of (Aut α) ≅ Group.of (equiv.perm α) := { hom := ⟨λ g, g.to_equiv, (by tidy), (by tidy)⟩, inv := ⟨λ g, g.to_iso, (by tidy), (by tidy)⟩ } /-- The (unbundled) group of automorphisms of a type is `mul_equiv` to the (unbundled) group of permutations. -/ def mul_equiv_perm {α : Type u} : Aut α ≃* equiv.perm α := iso_perm.Group_iso_to_mul_equiv end category_theory.Aut
94113e677b4bdb3e35f3fc88d2faa553879e2d19
367134ba5a65885e863bdc4507601606690974c1
/test/nontriviality.lean
63edda3fa3db05cda643b681eae92481e75c619f
[ "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
2,516
lean
import logic.nontrivial import algebra.ordered_ring import data.nat.basic /-! ### Test `nontriviality` with inequality hypotheses -/ example {R : Type} [ordered_ring R] {a : R} (h : 0 < a) : 0 < a := begin nontriviality, guard_hyp _inst : nontrivial R, assumption, end /-! ### Test `nontriviality` with equality or non-strict inequality goals -/ example {R : Type} [comm_ring R] {r s : R} : r * s = s * r := begin nontriviality, guard_hyp _inst : nontrivial R, apply mul_comm, end /-! ### Test deducing `nontriviality` by instance search -/ 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 /-! Test using `@[nontriviality]` lemmas in `nontriviality and custom `simp` lemmas -/ def empty_or_univ {α : Type*} (s : set α) : Prop := s = ∅ ∨ s = set.univ lemma subsingleton.set_empty_or_univ {α} [subsingleton α] (s : set α) : s = ∅ ∨ s = set.univ := subsingleton.set_cases (or.inl rfl) (or.inr rfl) s lemma subsingleton.set_empty_or_univ' {α} [subsingleton α] (s : set α) : empty_or_univ s := subsingleton.set_empty_or_univ s example {α : Type*} (s : set α) (hs : s = ∅ ∪ set.univ) : empty_or_univ s := begin success_if_fail { nontriviality α }, rw [set.empty_union] at hs, exact or.inr hs end section local attribute [nontriviality] subsingleton.set_empty_or_univ example {α : Type*} (s : set α) (hs : s = ∅ ∪ set.univ) : empty_or_univ s := begin success_if_fail { nontriviality α }, nontriviality α using [subsingleton.set_empty_or_univ'], rw [set.empty_union] at hs, exact or.inr hs end end local attribute [nontriviality] subsingleton.set_empty_or_univ' example {α : Type*} (s : set α) (hs : s = ∅ ∪ set.univ) : empty_or_univ s := begin nontriviality α, rw [set.empty_union] at hs, exact or.inr hs end /-! Test with nonatomic type argument -/ example (α : ℕ → Type) (a b : α 0) (h : a = b) : a = b := begin nontriviality α 0 using [nat.zero_lt_one], guard_hyp _inst : nontrivial (α 0), exact h end
cd7f796f64bb0ebb1b66f07a185d9c57dc408fc5
ccb7cdf8ebc2d015a000e8e7904952a36b910425
/src/heap/lemmas.lean
4891d1f63eef3365b7144bc7e81c85a4a894abce
[]
no_license
cipher1024/lean-pl
f7258bda55606b75e3e39deaf7ce8928ed177d66
829680605ac17e91038d793c0188e9614353ca25
refs/heads/master
1,592,558,951,987
1,565,043,356,000
1,565,043,531,000
196,661,367
1
0
null
null
null
null
UTF-8
Lean
false
false
7,905
lean
import category.basic import heap.basic import data.finmap import tactic.omega import tactic.linarith namespace memory variables value : Type variables {value} def maplet (x : ptr) (v : value) : heap value := finmap.singleton x v def heap.mk (l : list (ptr × value)) : heap value := (l.map $ λ x : ptr × value, (⟨x.1,x.2⟩ : sigma $ λ _, value)).to_finmap @[simp] lemma heap.mk_nil : heap.mk [] = (∅ : heap value) := rfl @[simp] lemma heap.mk_cons (p v) (l : list (ptr × value)) : heap.mk ((p,v) :: l) = maplet p v ∪ heap.mk l := rfl @[simp] lemma mem_heap.mk (p) (l : list (ptr × value)) : p ∈ heap.mk l ↔ ∃ v, (p,v) ∈ l := iff.trans (finmap.mem_list_to_finmap _ _) (exists_congr $ λ v, by simp [list.mem_map]; split; [ { rintro ⟨a,b,h,⟨⟩,⟨⟩⟩, exact h }, { intro h, refine ⟨_,_,h,rfl,rfl⟩ }]) def erase_all (p : ptr) (n : ℕ) (m : heap value) : heap value := (list.range n).foldl (λ m i, finmap.erase (p + i) m) m @[simp] lemma erase_all_zero (p : ptr) (m : heap value) : erase_all p 0 m = m := rfl lemma erase_all_succ (p : ptr) (n : ℕ) (m : heap value) : erase_all p n.succ m = finmap.erase (p+n) (erase_all p n m) := by simp [erase_all, list.foldl_eq_foldr' _ m (list.range _),list.range_concat] lemma erase_all_one_add (p : ptr) (n : ℕ) (m : heap value) : erase_all p (1+n) m = finmap.erase (p+n) (erase_all p n m) := by rw nat.add_comm; apply erase_all_succ p _ m lemma erase_all_succ' (p : ptr) (n : ℕ) (m : heap value) : erase_all p n.succ m = finmap.erase p (erase_all (p+1) n m) := by { simp only [erase_all,list.range_succ_eq_map], rw [list.foldl_eq_of_comm',list.foldl_map], congr, simp only [add_zero, nat.add_comm, add_left_comm], simp only [finmap.erase_erase, forall_3_true_iff, eq_self_iff_true] } lemma erase_all_one_add' (p : ptr) (n : ℕ) (m : heap value) : erase_all p (1+n) m = finmap.erase p (erase_all (p+1) n m) := by rw nat.add_comm; apply erase_all_succ' p _ m open list nat lemma mem_erase_all (p p' : ptr) (m : heap value) : Π {n}, p ∈ erase_all p' n m ↔ ¬ p ∈ range' p' n ∧ p ∈ m | 0 := by simp [erase_all_zero] | (succ n) := by { simp only [erase_all_succ, mem_cons_iff, add_comm, mem_range', finmap.mem_erase, ne.def, add_left_comm, mem_erase_all, not_and_distrib], repeat { rw ← and_assoc }, apply and_congr _ (iff.refl _), clear mem_erase_all, dsimp [ptr] at p p', omega nat } open finmap @[simp] lemma mem_maplet (p q : ptr) (v : value) : p ∈ maplet q v ↔ p = q := by simp only [maplet, not_mem_empty, finmap.mem_singleton, iff_self, or_false] lemma disjoint_maplet (p) (v : value) (frame : heap value) : disjoint (maplet p v) frame ↔ ¬ p ∈ frame := begin split, { simp [disjoint.symm_iff], intros h h', specialize h _ h', simp only [maplet,finmap.mem_singleton] at h, exact h rfl }, { intros h p' h', simp only [maplet,finmap.not_mem_empty,finmap.mem_singleton] at h', subst h', exact h } end lemma maplet_add (p : ptr) (v : value) (h : heap value) (hp : p ∉ h) : some (maplet p v) ⊗ some h = some (h.insert p v) := by rw ← union_eq_add_of_disjoint; [refl, { rw disjoint_maplet; exact hp }] @[simp] lemma heap.mem_mk (p : ptr) (vs : list (ptr × value)) : p ∈ heap.mk vs ↔ p ∈ vs.map prod.fst := by { simp only [heap.mk,mem_list_to_finmap,_root_.and_comm (_ ∈ vs),_root_.and_assoc, list.mem_map, iff_self, exists_eq_left, exists_and_distrib_left, heq_iff_eq, list.map, prod.exists] } instance : has_le (heap value) := { le := λ x y, ∀ a b, x.lookup a = some b → y.lookup a = some b } instance : preorder (heap value) := { le_refl := λ h a b H, H, le_trans := λ h₀ h₁ h₂ H H' p _ HH, H' _ _ (H _ _ HH), .. memory.has_le } lemma le_of_add_eq_some {h₀ h₁ : heap value} (h' : heap value) (H : some h₁ = some h₀ ⊗ some h') : h₀ ≤ h₁ := λ p v HH, have HH' : _, from eq_union_of_eq_add H, by rw [HH',lookup_union_left,HH]; apply mem_of_lookup_eq_some HH lemma erase_all_union_mk_self (p : ptr) (vs : list value) (h : heap value) (H : heap.mk (vs.enum_from p) ≤ h) : erase_all p (length vs) h ∪ heap.mk (vs.enum_from p) = h := begin induction vs generalizing p; simp [length,enum_from,erase_all_one_add',union_assoc], rw [← union_assoc,maplet,erase_union_singleton,vs_ih], { transitivity'; [skip, exact H], dsimp [enum_from], intros p' v Hp, rw [lookup_union_right,Hp], simp [maplet], intro Hp', subst p', replace Hp := mem_of_lookup_eq_some Hp, simp at Hp, change ℕ at p, clear_except Hp, replace Hp := Hp.1, omega nat, }, clear vs_ih, replace H : h.lookup p = some vs_hd, { apply H, simp [enum_from,maplet,lookup_singleton_eq] }, generalize hq : p + 1 = q, have : p < q, { rw ← hq, apply lt_add_one }, clear hq, induction vs_tl with v vs, { simp [H] }, { simp [length,erase_all_one_add], rw [lookup_erase_ne,vs_tl_ih], apply ne_of_lt, linarith } end lemma mem_of_mem_of_le {h₀ h₁ : heap value} (H : h₀ ≤ h₁) : ∀ x ∈ h₀, x ∈ h₁ := by intros p; rw [mem_iff,mem_iff]; apply exists_imp_exists; exact H p lemma insert_maplet (p) (v v' : value) : finmap.insert p v (maplet p v') = maplet p v := finmap.insert_singleton_eq lemma disjoint_mono {ha hb ha' hb' : heap value} (H₀ : ha' ≤ ha) (H₁ : hb' ≤ hb) : disjoint ha hb → disjoint ha' hb' := begin intros H₂ x H₃ H₄, replace H₃ := mem_of_mem_of_le H₀ _ H₃, replace H₄ := mem_of_mem_of_le H₁ _ H₄, exact H₂ _ H₃ H₄, end lemma add_inj {h₀ h₁ : option (heap value)} (hh : option (heap value)) (hh₀ hh₁ : heap value) (H₀ : some hh₀ = h₀ ⊗ hh) (H₁ : some hh₁ = h₁ ⊗ hh) (H₂ : h₀ ⊗ hh = h₁ ⊗ hh) : h₀ = h₁ := begin cases h₀; [skip, cases h₁], cases H₀, cases H₁, cases hh, cases H₀, rw [← union_eq_add_of_disjoint (disjoint_of_add _ H₀),← union_eq_add_of_disjoint (disjoint_of_add _ H₁),option.some_inj,finmap.union_cancel] at H₂, rw H₂, all_goals { apply disjoint_of_add, assumption }, end end memory namespace separation open memory structure tptr (val : Type) (α : Type*) := (get : ptr) variables {val : Type} {α : Type*} {β : Type*} include val local notation `tptr` := tptr val instance : decidable_linear_order (tptr α) := decidable_linear_order.lift tptr.get (λ ⟨_,_,x⟩ ⟨_,_,y⟩, congr_arg _) (by apply_instance) def tptr.recast (β) (p : tptr α) : tptr β := { get := p.get } def tptr.add (p : tptr α) (n : ℕ) : tptr α := { get := p.get + n } infixl ` +. `:65 := tptr.add @[simp, separation_logic] lemma offset_zero (p : tptr α) : p +. 0 = p := by cases p; refl @[simp, separation_logic] lemma offset_offset (p : tptr α) (n m : ℕ) : p +. n +. m = p +. (n + m) := congr_arg _ (nat.add_assoc _ _ _) lemma gt_offset_of_gt {α} (p : tptr α) {n : ℕ} (h : n > 0) : p +. n > p := by cases p; dsimp [(+.)]; apply lt_add_of_pos_right _ h lemma le_offset (p : tptr α) {n : ℕ} : p ≤ p +. n := by cases p; dsimp [(+.)]; apply nat.le_add_right lemma offset_ne (p : tptr α) {n : ℕ} (h : n > 0) : p +. n ≠ p := ne_of_gt (gt_offset_of_gt _ h) @[simp, separation_logic] lemma recast_offset (p : tptr α) (n : ℕ) : (p +. n).recast β = (p.recast β +. n) := rfl lemma recast_le_iff_le_recast (p : tptr α) (q : tptr β) : p.recast β ≤ q ↔ p ≤ q.recast α := by cases p; cases q; refl @[simp] lemma tptr.mk_offset (p : ptr) (n : ℕ) : tptr.mk val α p +. n = tptr.mk val α (p + n) := rfl open list lemma some_mk_enum_from_cons (p : ptr) (x : val) (xs : list val) : some (heap.mk (enum_from p (x :: xs))) = some (maplet p x) ⊗ some (heap.mk $ xs.enum_from $ p + 1) := begin rw ← union_eq_add_of_disjoint, { simp [enum_from] }, intro p', simp, intros h h', subst p', cases nat.not_succ_le_self _ h' end end separation
c71935c35b7b93875bf3a4781120b5a4d594dec5
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/group_theory/submonoid/membership.lean
61336ecb40075156edfbcd4d169a3e482c2f7513
[ "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
23,136
lean
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Kenny Lau, Johan Commelin, Mario Carneiro, Kevin Buzzard, Amelia Livingston, Yury Kudryashov -/ import group_theory.submonoid.operations import algebra.big_operators.basic import algebra.free_monoid.basic import data.finset.noncomm_prod /-! # Submonoids: membership criteria In this file we prove various facts about membership in a submonoid: * `list_prod_mem`, `multiset_prod_mem`, `prod_mem`: if each element of a collection belongs to a multiplicative submonoid, then so does their product; * `list_sum_mem`, `multiset_sum_mem`, `sum_mem`: if each element of a collection belongs to an additive submonoid, then so does their sum; * `pow_mem`, `nsmul_mem`: if `x ∈ S` where `S` is a multiplicative (resp., additive) submonoid and `n` is a natural number, then `x^n` (resp., `n • x`) belongs to `S`; * `mem_supr_of_directed`, `coe_supr_of_directed`, `mem_Sup_of_directed_on`, `coe_Sup_of_directed_on`: the supremum of a directed collection of submonoid is their union. * `sup_eq_range`, `mem_sup`: supremum of two submonoids `S`, `T` of a commutative monoid is the set of products; * `closure_singleton_eq`, `mem_closure_singleton`, `mem_closure_pair`: the multiplicative (resp., additive) closure of `{x}` consists of powers (resp., natural multiples) of `x`, and a similar result holds for the closure of `{x, y}`. ## Tags submonoid, submonoids -/ open_locale big_operators variables {M A B : Type*} section assoc variables [monoid M] [set_like B M] [submonoid_class B M] {S : B} namespace submonoid_class @[simp, norm_cast, to_additive] theorem coe_list_prod (l : list S) : (l.prod : M) = (l.map coe).prod := (submonoid_class.subtype S : _ →* M).map_list_prod l @[simp, norm_cast, to_additive] theorem coe_multiset_prod {M} [comm_monoid M] [set_like B M] [submonoid_class B M] (m : multiset S) : (m.prod : M) = (m.map coe).prod := (submonoid_class.subtype S : _ →* M).map_multiset_prod m @[simp, norm_cast, to_additive] theorem coe_finset_prod {ι M} [comm_monoid M] [set_like B M] [submonoid_class B M] (f : ι → S) (s : finset ι) : ↑(∏ i in s, f i) = (∏ i in s, f i : M) := (submonoid_class.subtype S : _ →* M).map_prod f s end submonoid_class open submonoid_class /-- Product of a list of elements in a submonoid is in the submonoid. -/ @[to_additive "Sum of a list of elements in an `add_submonoid` is in the `add_submonoid`."] lemma list_prod_mem {l : list M} (hl : ∀ x ∈ l, x ∈ S) : l.prod ∈ S := by { lift l to list S using hl, rw ← coe_list_prod, exact l.prod.coe_prop } /-- Product of a multiset of elements in a submonoid of a `comm_monoid` is in the submonoid. -/ @[to_additive "Sum of a multiset of elements in an `add_submonoid` of an `add_comm_monoid` is in the `add_submonoid`."] lemma multiset_prod_mem {M} [comm_monoid M] [set_like B M] [submonoid_class B M] (m : multiset M) (hm : ∀ a ∈ m, a ∈ S) : m.prod ∈ S := by { lift m to multiset S using hm, rw ← coe_multiset_prod, exact m.prod.coe_prop } /-- Product of elements of a submonoid of a `comm_monoid` indexed by a `finset` is in the submonoid. -/ @[to_additive "Sum of elements in an `add_submonoid` of an `add_comm_monoid` indexed by a `finset` is in the `add_submonoid`."] lemma prod_mem {M : Type*} [comm_monoid M] [set_like B M] [submonoid_class B M] {ι : Type*} {t : finset ι} {f : ι → M} (h : ∀c ∈ t, f c ∈ S) : ∏ c in t, f c ∈ S := multiset_prod_mem (t.1.map f) $ λ x hx, let ⟨i, hi, hix⟩ := multiset.mem_map.1 hx in hix ▸ h i hi namespace submonoid variables (s : submonoid M) @[simp, norm_cast, to_additive] theorem coe_list_prod (l : list s) : (l.prod : M) = (l.map coe).prod := s.subtype.map_list_prod l @[simp, norm_cast, to_additive] theorem coe_multiset_prod {M} [comm_monoid M] (S : submonoid M) (m : multiset S) : (m.prod : M) = (m.map coe).prod := S.subtype.map_multiset_prod m @[simp, norm_cast, to_additive] theorem coe_finset_prod {ι M} [comm_monoid M] (S : submonoid M) (f : ι → S) (s : finset ι) : ↑(∏ i in s, f i) = (∏ i in s, f i : M) := S.subtype.map_prod f s /-- Product of a list of elements in a submonoid is in the submonoid. -/ @[to_additive "Sum of a list of elements in an `add_submonoid` is in the `add_submonoid`."] lemma list_prod_mem {l : list M} (hl : ∀ x ∈ l, x ∈ s) : l.prod ∈ s := by { lift l to list s using hl, rw ← coe_list_prod, exact l.prod.coe_prop } /-- Product of a multiset of elements in a submonoid of a `comm_monoid` is in the submonoid. -/ @[to_additive "Sum of a multiset of elements in an `add_submonoid` of an `add_comm_monoid` is in the `add_submonoid`."] lemma multiset_prod_mem {M} [comm_monoid M] (S : submonoid M) (m : multiset M) (hm : ∀ a ∈ m, a ∈ S) : m.prod ∈ S := by { lift m to multiset S using hm, rw ← coe_multiset_prod, exact m.prod.coe_prop } @[to_additive] lemma multiset_noncomm_prod_mem (S : submonoid M) (m : multiset M) (comm) (h : ∀ (x ∈ m), x ∈ S) : m.noncomm_prod comm ∈ S := begin induction m using quotient.induction_on with l, simp only [multiset.quot_mk_to_coe, multiset.noncomm_prod_coe], exact submonoid.list_prod_mem _ h, end /-- Product of elements of a submonoid of a `comm_monoid` indexed by a `finset` is in the submonoid. -/ @[to_additive "Sum of elements in an `add_submonoid` of an `add_comm_monoid` indexed by a `finset` is in the `add_submonoid`."] lemma prod_mem {M : Type*} [comm_monoid M] (S : submonoid M) {ι : Type*} {t : finset ι} {f : ι → M} (h : ∀c ∈ t, f c ∈ S) : ∏ c in t, f c ∈ S := S.multiset_prod_mem (t.1.map f) $ λ x hx, let ⟨i, hi, hix⟩ := multiset.mem_map.1 hx in hix ▸ h i hi @[to_additive] lemma noncomm_prod_mem (S : submonoid M) {ι : Type*} (t : finset ι) (f : ι → M) (comm) (h : ∀ c ∈ t, f c ∈ S) : t.noncomm_prod f comm ∈ S := begin apply multiset_noncomm_prod_mem, intro y, rw multiset.mem_map, rintros ⟨x, ⟨hx, rfl⟩⟩, exact h x hx, end end submonoid end assoc section non_assoc variables [mul_one_class M] open set namespace submonoid -- TODO: this section can be generalized to `[submonoid_class B M] [complete_lattice B]` -- such that `complete_lattice.le` coincides with `set_like.le` @[to_additive] lemma mem_supr_of_directed {ι} [hι : nonempty ι] {S : ι → submonoid M} (hS : directed (≤) S) {x : M} : x ∈ (⨆ i, S i) ↔ ∃ i, x ∈ S i := begin refine ⟨_, λ ⟨i, hi⟩, (set_like.le_def.1 $ le_supr S i) hi⟩, suffices : x ∈ closure (⋃ i, (S i : set M)) → ∃ i, x ∈ S i, by simpa only [closure_Union, closure_eq (S _)] using this, refine (λ hx, closure_induction hx (λ _, mem_Union.1) _ _), { exact hι.elim (λ i, ⟨i, (S i).one_mem⟩) }, { rintros x y ⟨i, hi⟩ ⟨j, hj⟩, rcases hS i j with ⟨k, hki, hkj⟩, exact ⟨k, (S k).mul_mem (hki hi) (hkj hj)⟩ } end @[to_additive] lemma coe_supr_of_directed {ι} [nonempty ι] {S : ι → submonoid M} (hS : directed (≤) S) : ((⨆ i, S i : submonoid M) : set M) = ⋃ i, ↑(S i) := set.ext $ λ x, by simp [mem_supr_of_directed hS] @[to_additive] lemma mem_Sup_of_directed_on {S : set (submonoid M)} (Sne : S.nonempty) (hS : directed_on (≤) S) {x : M} : x ∈ Sup S ↔ ∃ s ∈ S, x ∈ s := begin haveI : nonempty S := Sne.to_subtype, simp only [Sup_eq_supr', mem_supr_of_directed hS.directed_coe, set_coe.exists, subtype.coe_mk] end @[to_additive] lemma coe_Sup_of_directed_on {S : set (submonoid M)} (Sne : S.nonempty) (hS : directed_on (≤) S) : (↑(Sup S) : set M) = ⋃ s ∈ S, ↑s := set.ext $ λ x, by simp [mem_Sup_of_directed_on Sne hS] @[to_additive] lemma mem_sup_left {S T : submonoid M} : ∀ {x : M}, x ∈ S → x ∈ S ⊔ T := show S ≤ S ⊔ T, from le_sup_left @[to_additive] lemma mem_sup_right {S T : submonoid M} : ∀ {x : M}, x ∈ T → x ∈ S ⊔ T := show T ≤ S ⊔ T, from le_sup_right @[to_additive] lemma mul_mem_sup {S T : submonoid M} {x y : M} (hx : x ∈ S) (hy : y ∈ T) : x * y ∈ S ⊔ T := (S ⊔ T).mul_mem (mem_sup_left hx) (mem_sup_right hy) @[to_additive] lemma mem_supr_of_mem {ι : Sort*} {S : ι → submonoid M} (i : ι) : ∀ {x : M}, x ∈ S i → x ∈ supr S := show S i ≤ supr S, from le_supr _ _ @[to_additive] lemma mem_Sup_of_mem {S : set (submonoid M)} {s : submonoid M} (hs : s ∈ S) : ∀ {x : M}, x ∈ s → x ∈ Sup S := show s ≤ Sup S, from le_Sup hs /-- An induction principle for elements of `⨆ i, S i`. If `C` holds for `1` and all elements of `S i` for all `i`, and is preserved under multiplication, then it holds for all elements of the supremum of `S`. -/ @[elab_as_eliminator, to_additive /-" An induction principle for elements of `⨆ i, S i`. If `C` holds for `0` and all elements of `S i` for all `i`, and is preserved under addition, then it holds for all elements of the supremum of `S`. "-/] lemma supr_induction {ι : Sort*} (S : ι → submonoid M) {C : M → Prop} {x : M} (hx : x ∈ ⨆ i, S i) (hp : ∀ i (x ∈ S i), C x) (h1 : C 1) (hmul : ∀ x y, C x → C y → C (x * y)) : C x := begin rw supr_eq_closure at hx, refine closure_induction hx (λ x hx, _) h1 hmul, obtain ⟨i, hi⟩ := set.mem_Union.mp hx, exact hp _ _ hi, end /-- A dependent version of `submonoid.supr_induction`. -/ @[elab_as_eliminator, to_additive /-"A dependent version of `add_submonoid.supr_induction`. "-/] lemma supr_induction' {ι : Sort*} (S : ι → submonoid M) {C : Π x, (x ∈ ⨆ i, S i) → Prop} (hp : ∀ i (x ∈ S i), C x (mem_supr_of_mem i ‹_›)) (h1 : C 1 (one_mem _)) (hmul : ∀ x y hx hy, C x hx → C y hy → C (x * y) (mul_mem ‹_› ‹_›)) {x : M} (hx : x ∈ ⨆ i, S i) : C x hx := begin refine exists.elim _ (λ (hx : x ∈ ⨆ i, S i) (hc : C x hx), hc), refine supr_induction S hx (λ i x hx, _) _ (λ x y, _), { exact ⟨_, hp _ _ hx⟩ }, { exact ⟨_, h1⟩ }, { rintro ⟨_, Cx⟩ ⟨_, Cy⟩, refine ⟨_, hmul _ _ _ _ Cx Cy⟩ }, end end submonoid end non_assoc namespace free_monoid variables {α : Type*} open submonoid @[to_additive] theorem closure_range_of : closure (set.range $ @of α) = ⊤ := eq_top_iff.2 $ λ x hx, free_monoid.rec_on x (one_mem _) $ λ x xs hxs, mul_mem (subset_closure $ set.mem_range_self _) hxs end free_monoid namespace submonoid variables [monoid M] open monoid_hom lemma closure_singleton_eq (x : M) : closure ({x} : set M) = (powers_hom M x).mrange := closure_eq_of_le (set.singleton_subset_iff.2 ⟨multiplicative.of_add 1, pow_one x⟩) $ λ x ⟨n, hn⟩, hn ▸ pow_mem (subset_closure $ set.mem_singleton _) _ /-- The submonoid generated by an element of a monoid equals the set of natural number powers of the element. -/ lemma mem_closure_singleton {x y : M} : y ∈ closure ({x} : set M) ↔ ∃ n:ℕ, x^n=y := by rw [closure_singleton_eq, mem_mrange]; refl lemma mem_closure_singleton_self {y : M} : y ∈ closure ({y} : set M) := mem_closure_singleton.2 ⟨1, pow_one y⟩ lemma closure_singleton_one : closure ({1} : set M) = ⊥ := by simp [eq_bot_iff_forall, mem_closure_singleton] @[to_additive] lemma _root_.free_monoid.mrange_lift {α} (f : α → M) : (free_monoid.lift f).mrange = closure (set.range f) := by rw [mrange_eq_map, ← free_monoid.closure_range_of, map_mclosure, ← set.range_comp, free_monoid.lift_comp_of] @[to_additive] lemma closure_eq_mrange (s : set M) : closure s = (free_monoid.lift (coe : s → M)).mrange := by rw [free_monoid.mrange_lift, subtype.range_coe] @[to_additive] lemma closure_eq_image_prod (s : set M) : (closure s : set M) = list.prod '' {l : list M | ∀ x ∈ l, x ∈ s} := begin rw [closure_eq_mrange, coe_mrange, ← list.range_map_coe, ← set.range_comp], refl end @[to_additive] lemma exists_list_of_mem_closure {s : set M} {x : M} (hx : x ∈ closure s) : ∃ (l : list M) (hl : ∀ y ∈ l, y ∈ s), l.prod = x := by rwa [← set_like.mem_coe, closure_eq_image_prod, set.mem_image_iff_bex] at hx @[to_additive] lemma exists_multiset_of_mem_closure {M : Type*} [comm_monoid M] {s : set M} {x : M} (hx : x ∈ closure s) : ∃ (l : multiset M) (hl : ∀ y ∈ l, y ∈ s), l.prod = x := begin obtain ⟨l, h1, h2⟩ := exists_list_of_mem_closure hx, exact ⟨l, h1, (multiset.coe_prod l).trans h2⟩, end @[to_additive] lemma closure_induction_left {s : set M} {p : M → Prop} {x : M} (h : x ∈ closure s) (H1 : p 1) (Hmul : ∀ (x ∈ s) y, p y → p (x * y)) : p x := begin rw closure_eq_mrange at h, obtain ⟨l, rfl⟩ := h, induction l using free_monoid.rec_on with x y ih, { exact H1 }, { simpa only [map_mul, free_monoid.lift_eval_of] using Hmul _ x.prop _ ih } end @[elab_as_eliminator, to_additive] lemma induction_of_closure_eq_top_left {s : set M} {p : M → Prop} (hs : closure s = ⊤) (x : M) (H1 : p 1) (Hmul : ∀ (x ∈ s) y, p y → p (x * y)) : p x := closure_induction_left (by { rw [hs], exact mem_top _ }) H1 Hmul @[to_additive] lemma closure_induction_right {s : set M} {p : M → Prop} {x : M} (h : x ∈ closure s) (H1 : p 1) (Hmul : ∀ x (y ∈ s), p x → p (x * y)) : p x := @closure_induction_left _ _ (mul_opposite.unop ⁻¹' s) (p ∘ mul_opposite.unop) (mul_opposite.op x) (closure_induction h (λ x hx, subset_closure hx) (one_mem _) (λ x y hx hy, mul_mem hy hx)) H1 (λ x hx y, Hmul _ _ hx) @[elab_as_eliminator, to_additive] lemma induction_of_closure_eq_top_right {s : set M} {p : M → Prop} (hs : closure s = ⊤) (x : M) (H1 : p 1) (Hmul : ∀ x (y ∈ s), p x → p (x * y)) : p x := closure_induction_right (by { rw [hs], exact mem_top _ }) H1 Hmul /-- The submonoid generated by an element. -/ def powers (n : M) : submonoid M := submonoid.copy (powers_hom M n).mrange (set.range ((^) n : ℕ → M)) $ set.ext (λ n, exists_congr $ λ i, by simp; refl) @[simp] lemma mem_powers (n : M) : n ∈ powers n := ⟨1, pow_one _⟩ lemma mem_powers_iff (x z : M) : x ∈ powers z ↔ ∃ n : ℕ, z ^ n = x := iff.rfl lemma powers_eq_closure (n : M) : powers n = closure {n} := by { ext, exact mem_closure_singleton.symm } lemma powers_subset {n : M} {P : submonoid M} (h : n ∈ P) : powers n ≤ P := λ x hx, match x, hx with _, ⟨i, rfl⟩ := pow_mem h i end @[simp] lemma powers_one : powers (1 : M) = ⊥ := bot_unique $ powers_subset (one_mem _) /-- Exponentiation map from natural numbers to powers. -/ @[simps] def pow (n : M) (m : ℕ) : powers n := (powers_hom M n).mrange_restrict (multiplicative.of_add m) lemma pow_apply (n : M) (m : ℕ) : submonoid.pow n m = ⟨n ^ m, m, rfl⟩ := rfl /-- Logarithms from powers to natural numbers. -/ def log [decidable_eq M] {n : M} (p : powers n) : ℕ := nat.find $ (mem_powers_iff p.val n).mp p.prop @[simp] theorem pow_log_eq_self [decidable_eq M] {n : M} (p : powers n) : pow n (log p) = p := subtype.ext $ nat.find_spec p.prop lemma pow_right_injective_iff_pow_injective {n : M} : function.injective (λ m : ℕ, n ^ m) ↔ function.injective (pow n) := subtype.coe_injective.of_comp_iff (pow n) @[simp] theorem log_pow_eq_self [decidable_eq M] {n : M} (h : function.injective (λ m : ℕ, n ^ m)) (m : ℕ) : log (pow n m) = m := pow_right_injective_iff_pow_injective.mp h $ pow_log_eq_self _ /-- The exponentiation map is an isomorphism from the additive monoid on natural numbers to powers when it is injective. The inverse is given by the logarithms. -/ @[simps] def pow_log_equiv [decidable_eq M] {n : M} (h : function.injective (λ m : ℕ, n ^ m)) : multiplicative ℕ ≃* powers n := { to_fun := λ m, pow n m.to_add, inv_fun := λ m, multiplicative.of_add (log m), left_inv := log_pow_eq_self h, right_inv := pow_log_eq_self, map_mul' := λ _ _, by { simp only [pow, map_mul, of_add_add, to_add_mul] } } lemma log_mul [decidable_eq M] {n : M} (h : function.injective (λ m : ℕ, n ^ m)) (x y : powers (n : M)) : log (x * y) = log x + log y := (pow_log_equiv h).symm.map_mul x y theorem log_pow_int_eq_self {x : ℤ} (h : 1 < x.nat_abs) (m : ℕ) : log (pow x m) = m := (pow_log_equiv (int.pow_right_injective h)).symm_apply_apply _ @[simp] lemma map_powers {N : Type*} {F : Type*} [monoid N] [monoid_hom_class F M N] (f : F) (m : M) : (powers m).map f = powers (f m) := by simp only [powers_eq_closure, map_mclosure f, set.image_singleton] /-- If all the elements of a set `s` commute, then `closure s` is a commutative monoid. -/ @[to_additive "If all the elements of a set `s` commute, then `closure s` forms an additive commutative monoid."] def closure_comm_monoid_of_comm {s : set M} (hcomm : ∀ a b ∈ s, a * b = b * a) : comm_monoid (closure s) := { mul_comm := λ x y, begin ext, simp only [submonoid.coe_mul], exact closure_induction₂ x.prop y.prop hcomm commute.one_left commute.one_right (λ x y z, commute.mul_left) (λ x y z, commute.mul_right), end, .. (closure s).to_monoid } end submonoid @[to_additive] lemma is_scalar_tower.of_mclosure_eq_top {N α} [monoid M] [mul_action M N] [has_smul N α] [mul_action M α] {s : set M} (htop : submonoid.closure s = ⊤) (hs : ∀ (x ∈ s) (y : N) (z : α), (x • y) • z = x • (y • z)) : is_scalar_tower M N α := begin refine ⟨λ x, submonoid.induction_of_closure_eq_top_left htop x _ _⟩, { intros y z, rw [one_smul, one_smul] }, { clear x, intros x hx x' hx' y z, rw [mul_smul, mul_smul, hs x hx, hx'] } end @[to_additive] lemma smul_comm_class.of_mclosure_eq_top {N α} [monoid M] [has_smul N α] [mul_action M α] {s : set M} (htop : submonoid.closure s = ⊤) (hs : ∀ (x ∈ s) (y : N) (z : α), x • y • z = y • x • z) : smul_comm_class M N α := begin refine ⟨λ x, submonoid.induction_of_closure_eq_top_left htop x _ _⟩, { intros y z, rw [one_smul, one_smul] }, { clear x, intros x hx x' hx' y z, rw [mul_smul, mul_smul, hx', hs x hx] } end namespace submonoid variables {N : Type*} [comm_monoid N] open monoid_hom @[to_additive] lemma sup_eq_range (s t : submonoid N) : s ⊔ t = (s.subtype.coprod t.subtype).mrange := by rw [mrange_eq_map, ← mrange_inl_sup_mrange_inr, map_sup, map_mrange, coprod_comp_inl, map_mrange, coprod_comp_inr, range_subtype, range_subtype] @[to_additive] lemma mem_sup {s t : submonoid N} {x : N} : x ∈ s ⊔ t ↔ ∃ (y ∈ s) (z ∈ t), y * z = x := by simp only [sup_eq_range, mem_mrange, coprod_apply, prod.exists, set_like.exists, coe_subtype, subtype.coe_mk] end submonoid namespace add_submonoid variables [add_monoid A] open set lemma closure_singleton_eq (x : A) : closure ({x} : set A) = (multiples_hom A x).mrange := closure_eq_of_le (set.singleton_subset_iff.2 ⟨1, one_nsmul x⟩) $ λ x ⟨n, hn⟩, hn ▸ nsmul_mem (subset_closure $ set.mem_singleton _) _ /-- The `add_submonoid` generated by an element of an `add_monoid` equals the set of natural number multiples of the element. -/ lemma mem_closure_singleton {x y : A} : y ∈ closure ({x} : set A) ↔ ∃ n:ℕ, n • x = y := by rw [closure_singleton_eq, add_monoid_hom.mem_mrange]; refl lemma closure_singleton_zero : closure ({0} : set A) = ⊥ := by simp [eq_bot_iff_forall, mem_closure_singleton, nsmul_zero] /-- The additive submonoid generated by an element. -/ def multiples (x : A) : add_submonoid A := add_submonoid.copy (multiples_hom A x).mrange (set.range (λ i, i • x : ℕ → A)) $ set.ext (λ n, exists_congr $ λ i, by simp; refl) attribute [to_additive multiples] submonoid.powers attribute [to_additive mem_multiples] submonoid.mem_powers attribute [to_additive mem_multiples_iff] submonoid.mem_powers_iff attribute [to_additive multiples_eq_closure] submonoid.powers_eq_closure attribute [to_additive multiples_subset] submonoid.powers_subset attribute [to_additive multiples_zero] submonoid.powers_one end add_submonoid /-! Lemmas about additive closures of `subsemigroup`. -/ namespace mul_mem_class variables {R : Type*} [non_unital_non_assoc_semiring R] [set_like M R] [mul_mem_class M R] {S : M} {a b : R} /-- The product of an element of the additive closure of a multiplicative subsemigroup `M` and an element of `M` is contained in the additive closure of `M`. -/ lemma mul_right_mem_add_closure (ha : a ∈ add_submonoid.closure (S : set R)) (hb : b ∈ S) : a * b ∈ add_submonoid.closure (S : set R) := begin revert b, refine add_submonoid.closure_induction ha _ _ _; clear ha a, { exact λ r hr b hb, add_submonoid.mem_closure.mpr (λ y hy, hy (mul_mem hr hb)) }, { exact λ b hb, by simp only [zero_mul, (add_submonoid.closure (S : set R)).zero_mem] }, { simp_rw add_mul, exact λ r s hr hs b hb, (add_submonoid.closure (S : set R)).add_mem (hr hb) (hs hb) } end /-- The product of two elements of the additive closure of a submonoid `M` is an element of the additive closure of `M`. -/ lemma mul_mem_add_closure (ha : a ∈ add_submonoid.closure (S : set R)) (hb : b ∈ add_submonoid.closure (S : set R)) : a * b ∈ add_submonoid.closure (S : set R) := begin revert a, refine add_submonoid.closure_induction hb _ _ _; clear hb b, { exact λ r hr b hb, mul_mem_class.mul_right_mem_add_closure hb hr }, { exact λ b hb, by simp only [mul_zero, (add_submonoid.closure (S : set R)).zero_mem] }, { simp_rw mul_add, exact λ r s hr hs b hb, (add_submonoid.closure (S : set R)).add_mem (hr hb) (hs hb) } end /-- The product of an element of `S` and an element of the additive closure of a multiplicative submonoid `S` is contained in the additive closure of `S`. -/ lemma mul_left_mem_add_closure (ha : a ∈ S) (hb : b ∈ add_submonoid.closure (S : set R)) : a * b ∈ add_submonoid.closure (S : set R) := mul_mem_add_closure (add_submonoid.mem_closure.mpr (λ sT hT, hT ha)) hb end mul_mem_class namespace submonoid /-- An element is in the closure of a two-element set if it is a linear combination of those two elements. -/ @[to_additive "An element is in the closure of a two-element set if it is a linear combination of those two elements."] lemma mem_closure_pair {A : Type*} [comm_monoid A] (a b c : A) : c ∈ submonoid.closure ({a, b} : set A) ↔ ∃ m n : ℕ, a ^ m * b ^ n = c := begin rw [←set.singleton_union, submonoid.closure_union, mem_sup], simp_rw [exists_prop, mem_closure_singleton, exists_exists_eq_and], end end submonoid section mul_add lemma of_mul_image_powers_eq_multiples_of_mul [monoid M] {x : M} : additive.of_mul '' ((submonoid.powers x) : set M) = add_submonoid.multiples (additive.of_mul x) := begin ext, split, { rintros ⟨y, ⟨n, hy1⟩, hy2⟩, use n, simpa [← of_mul_pow, hy1] }, { rintros ⟨n, hn⟩, refine ⟨x ^ n, ⟨n, rfl⟩, _⟩, rwa of_mul_pow } end lemma of_add_image_multiples_eq_powers_of_add [add_monoid A] {x : A} : multiplicative.of_add '' ((add_submonoid.multiples x) : set A) = submonoid.powers (multiplicative.of_add x) := begin symmetry, rw equiv.eq_image_iff_symm_image_eq, exact of_mul_image_powers_eq_multiples_of_mul, end end mul_add
25d6d3a30cc72f287687d60063b8b75ec1ab3479
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/data/matrix/block.lean
8a178f776a6f7a390791e98d84678709d9d7b776
[ "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
15,854
lean
/- Copyright (c) 2018 Ellen Arlt. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Ellen Arlt, Blair Shi, Sean Leather, Mario Carneiro, Johan Commelin -/ import data.matrix.basic /-! # Block Matrices ## Main definitions * `matrix.from_blocks`: build a block matrix out of 4 blocks * `matrix.to_blocks₁₁`, `matrix.to_blocks₁₂`, `matrix.to_blocks₂₁`, `matrix.to_blocks₂₂`: extract each of the four blocks from `matrix.from_blocks`. * `matrix.block_diagonal`: block diagonal of equally sized blocks * `matrix.block_diagonal'`: block diagonal of unequally sized blocks -/ variables {l m n o : Type*} {m' : o → Type*} {n' : o → Type*} variables {R : Type*} {S : Type*} {α : Type*} {β : Type*} open_locale matrix namespace matrix section block_matrices /-- We can form a single large matrix by flattening smaller 'block' matrices of compatible dimensions. -/ def from_blocks (A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) : matrix (n ⊕ o) (l ⊕ m) α := sum.elim (λ i, sum.elim (A i) (B i)) (λ i, sum.elim (C i) (D i)) @[simp] lemma from_blocks_apply₁₁ (A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) (i : n) (j : l) : from_blocks A B C D (sum.inl i) (sum.inl j) = A i j := rfl @[simp] lemma from_blocks_apply₁₂ (A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) (i : n) (j : m) : from_blocks A B C D (sum.inl i) (sum.inr j) = B i j := rfl @[simp] lemma from_blocks_apply₂₁ (A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) (i : o) (j : l) : from_blocks A B C D (sum.inr i) (sum.inl j) = C i j := rfl @[simp] lemma from_blocks_apply₂₂ (A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) (i : o) (j : m) : from_blocks A B C D (sum.inr i) (sum.inr j) = D i j := rfl /-- Given a matrix whose row and column indexes are sum types, we can extract the corresponding "top left" submatrix. -/ def to_blocks₁₁ (M : matrix (n ⊕ o) (l ⊕ m) α) : matrix n l α := λ i j, M (sum.inl i) (sum.inl j) /-- Given a matrix whose row and column indexes are sum types, we can extract the corresponding "top right" submatrix. -/ def to_blocks₁₂ (M : matrix (n ⊕ o) (l ⊕ m) α) : matrix n m α := λ i j, M (sum.inl i) (sum.inr j) /-- Given a matrix whose row and column indexes are sum types, we can extract the corresponding "bottom left" submatrix. -/ def to_blocks₂₁ (M : matrix (n ⊕ o) (l ⊕ m) α) : matrix o l α := λ i j, M (sum.inr i) (sum.inl j) /-- Given a matrix whose row and column indexes are sum types, we can extract the corresponding "bottom right" submatrix. -/ def to_blocks₂₂ (M : matrix (n ⊕ o) (l ⊕ m) α) : matrix o m α := λ i j, M (sum.inr i) (sum.inr j) lemma from_blocks_to_blocks (M : matrix (n ⊕ o) (l ⊕ m) α) : from_blocks M.to_blocks₁₁ M.to_blocks₁₂ M.to_blocks₂₁ M.to_blocks₂₂ = M := begin ext i j, rcases i; rcases j; refl, end @[simp] lemma to_blocks_from_blocks₁₁ (A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) : (from_blocks A B C D).to_blocks₁₁ = A := rfl @[simp] lemma to_blocks_from_blocks₁₂ (A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) : (from_blocks A B C D).to_blocks₁₂ = B := rfl @[simp] lemma to_blocks_from_blocks₂₁ (A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) : (from_blocks A B C D).to_blocks₂₁ = C := rfl @[simp] lemma to_blocks_from_blocks₂₂ (A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) : (from_blocks A B C D).to_blocks₂₂ = D := rfl lemma from_blocks_map (A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) (f : α → β) : (from_blocks A B C D).map f = from_blocks (A.map f) (B.map f) (C.map f) (D.map f) := begin ext i j, rcases i; rcases j; simp [from_blocks], end lemma from_blocks_transpose (A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) : (from_blocks A B C D)ᵀ = from_blocks Aᵀ Cᵀ Bᵀ Dᵀ := begin ext i j, rcases i; rcases j; simp [from_blocks], end lemma from_blocks_conj_transpose [has_star α] (A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) : (from_blocks A B C D)ᴴ = from_blocks Aᴴ Cᴴ Bᴴ Dᴴ := begin simp only [conj_transpose, from_blocks_transpose, from_blocks_map] end /-- A 2x2 block matrix is block diagonal if the blocks outside of the diagonal vanish -/ def is_two_block_diagonal [has_zero α] (A : matrix (n ⊕ o) (l ⊕ m) α) : Prop := to_blocks₁₂ A = 0 ∧ to_blocks₂₁ A = 0 /-- Let `p` pick out certain rows and `q` pick out certain columns of a matrix `M`. Then `to_block M p q` is the corresponding block matrix. -/ def to_block (M : matrix m n α) (p : m → Prop) (q : n → Prop) : matrix {a // p a} {a // q a} α := M.minor coe coe @[simp] lemma to_block_apply (M : matrix m n α) (p : m → Prop) (q : n → Prop) (i : {a // p a}) (j : {a // q a}) : to_block M p q i j = M ↑i ↑j := rfl /-- Let `b` map rows and columns of a square matrix `M` to blocks. Then `to_square_block M b k` is the block `k` matrix. -/ def to_square_block (M : matrix m m α) {n : nat} (b : m → fin n) (k : fin n) : matrix {a // b a = k} {a // b a = k} α := M.minor coe coe @[simp] lemma to_square_block_def (M : matrix m m α) {n : nat} (b : m → fin n) (k : fin n) : to_square_block M b k = λ i j, M ↑i ↑j := rfl /-- Alternate version with `b : m → nat`. Let `b` map rows and columns of a square matrix `M` to blocks. Then `to_square_block' M b k` is the block `k` matrix. -/ def to_square_block' (M : matrix m m α) (b : m → nat) (k : nat) : matrix {a // b a = k} {a // b a = k} α := M.minor coe coe @[simp] lemma to_square_block_def' (M : matrix m m α) (b : m → nat) (k : nat) : to_square_block' M b k = λ i j, M ↑i ↑j := rfl /-- Let `p` pick out certain rows and columns of a square matrix `M`. Then `to_square_block_prop M p` is the corresponding block matrix. -/ def to_square_block_prop (M : matrix m m α) (p : m → Prop) : matrix {a // p a} {a // p a} α := M.minor coe coe @[simp] lemma to_square_block_prop_def (M : matrix m m α) (p : m → Prop) : to_square_block_prop M p = λ i j, M ↑i ↑j := rfl variables [semiring α] lemma from_blocks_smul (x : α) (A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) : x • (from_blocks A B C D) = from_blocks (x • A) (x • B) (x • C) (x • D) := begin ext i j, rcases i; rcases j; simp [from_blocks], end lemma from_blocks_add (A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) (A' : matrix n l α) (B' : matrix n m α) (C' : matrix o l α) (D' : matrix o m α) : (from_blocks A B C D) + (from_blocks A' B' C' D') = from_blocks (A + A') (B + B') (C + C') (D + D') := begin ext i j, rcases i; rcases j; refl, end lemma from_blocks_multiply {p q : Type*} [fintype l] [fintype m] (A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) (A' : matrix l p α) (B' : matrix l q α) (C' : matrix m p α) (D' : matrix m q α) : (from_blocks A B C D) ⬝ (from_blocks A' B' C' D') = from_blocks (A ⬝ A' + B ⬝ C') (A ⬝ B' + B ⬝ D') (C ⬝ A' + D ⬝ C') (C ⬝ B' + D ⬝ D') := begin ext i j, rcases i; rcases j; simp only [from_blocks, mul_apply, fintype.sum_sum_type, sum.elim_inl, sum.elim_inr, pi.add_apply], end variables [decidable_eq l] [decidable_eq m] @[simp] lemma from_blocks_diagonal (d₁ : l → α) (d₂ : m → α) : from_blocks (diagonal d₁) 0 0 (diagonal d₂) = diagonal (sum.elim d₁ d₂) := begin ext i j, rcases i; rcases j; simp [diagonal], end @[simp] lemma from_blocks_one : from_blocks (1 : matrix l l α) 0 0 (1 : matrix m m α) = 1 := by { ext i j, rcases i; rcases j; simp [one_apply] } end block_matrices section block_diagonal variables (M N : o → matrix m n α) [decidable_eq o] section has_zero variables [has_zero α] [has_zero β] /-- `matrix.block_diagonal M` turns a homogenously-indexed collection of matrices `M : o → matrix m n α'` into a `m × o`-by-`n × o` block matrix which has the entries of `M` along the diagonal and zero elsewhere. See also `matrix.block_diagonal'` if the matrices may not have the same size everywhere. -/ def block_diagonal : matrix (m × o) (n × o) α | ⟨i, k⟩ ⟨j, k'⟩ := if k = k' then M k i j else 0 lemma block_diagonal_apply (ik jk) : block_diagonal M ik jk = if ik.2 = jk.2 then M ik.2 ik.1 jk.1 else 0 := by { cases ik, cases jk, refl } @[simp] lemma block_diagonal_apply_eq (i j k) : block_diagonal M (i, k) (j, k) = M k i j := if_pos rfl lemma block_diagonal_apply_ne (i j) {k k'} (h : k ≠ k') : block_diagonal M (i, k) (j, k') = 0 := if_neg h lemma block_diagonal_map (f : α → β) (hf : f 0 = 0) : (block_diagonal M).map f = block_diagonal (λ k, (M k).map f) := begin ext, simp only [map_apply, block_diagonal_apply, eq_comm], rw [apply_ite f, hf], end @[simp] lemma block_diagonal_transpose : (block_diagonal M)ᵀ = block_diagonal (λ k, (M k)ᵀ) := begin ext, simp only [transpose_apply, block_diagonal_apply, eq_comm], split_ifs with h, { rw h }, { refl } end @[simp] lemma block_diagonal_conj_transpose {α : Type*} [semiring α] [star_ring α] (M : o → matrix m n α) : (block_diagonal M)ᴴ = block_diagonal (λ k, (M k)ᴴ) := begin simp only [conj_transpose, block_diagonal_transpose], rw block_diagonal_map _ star (star_zero α), end @[simp] lemma block_diagonal_zero : block_diagonal (0 : o → matrix m n α) = 0 := by { ext, simp [block_diagonal_apply] } @[simp] lemma block_diagonal_diagonal [decidable_eq m] (d : o → m → α) : block_diagonal (λ k, diagonal (d k)) = diagonal (λ ik, d ik.2 ik.1) := begin ext ⟨i, k⟩ ⟨j, k'⟩, simp only [block_diagonal_apply, diagonal], split_ifs; finish end @[simp] lemma block_diagonal_one [decidable_eq m] [has_one α] : block_diagonal (1 : o → matrix m m α) = 1 := show block_diagonal (λ (_ : o), diagonal (λ (_ : m), (1 : α))) = diagonal (λ _, 1), by rw [block_diagonal_diagonal] end has_zero @[simp] lemma block_diagonal_add [add_monoid α] : block_diagonal (M + N) = block_diagonal M + block_diagonal N := begin ext, simp only [block_diagonal_apply, pi.add_apply], split_ifs; simp end @[simp] lemma block_diagonal_neg [add_group α] : block_diagonal (-M) = - block_diagonal M := begin ext, simp only [block_diagonal_apply, pi.neg_apply], split_ifs; simp end @[simp] lemma block_diagonal_sub [add_group α] : block_diagonal (M - N) = block_diagonal M - block_diagonal N := by simp [sub_eq_add_neg] @[simp] lemma block_diagonal_mul {p : Type*} [fintype n] [fintype o] [semiring α] (N : o → matrix n p α) : block_diagonal (λ k, M k ⬝ N k) = block_diagonal M ⬝ block_diagonal N := begin ext ⟨i, k⟩ ⟨j, k'⟩, simp only [block_diagonal_apply, mul_apply, ← finset.univ_product_univ, finset.sum_product], split_ifs with h; simp [h] end @[simp] lemma block_diagonal_smul {R : Type*} [semiring R] [add_comm_monoid α] [module R α] (x : R) : block_diagonal (x • M) = x • block_diagonal M := by { ext, simp only [block_diagonal_apply, pi.smul_apply], split_ifs; simp } end block_diagonal section block_diagonal' variables (M N : Π i, matrix (m' i) (n' i) α) [decidable_eq o] section has_zero variables [has_zero α] [has_zero β] /-- `matrix.block_diagonal' M` turns `M : Π i, matrix (m i) (n i) α` into a `Σ i, m i`-by-`Σ i, n i` block matrix which has the entries of `M` along the diagonal and zero elsewhere. This is the dependently-typed version of `matrix.block_diagonal`. -/ def block_diagonal' : matrix (Σ i, m' i) (Σ i, n' i) α | ⟨k, i⟩ ⟨k', j⟩ := if h : k = k' then M k i (cast (congr_arg n' h.symm) j) else 0 lemma block_diagonal'_eq_block_diagonal (M : o → matrix m n α) {k k'} (i j) : block_diagonal M (i, k) (j, k') = block_diagonal' M ⟨k, i⟩ ⟨k', j⟩ := rfl lemma block_diagonal'_minor_eq_block_diagonal (M : o → matrix m n α) : (block_diagonal' M).minor (prod.to_sigma ∘ prod.swap) (prod.to_sigma ∘ prod.swap) = block_diagonal M := matrix.ext $ λ ⟨k, i⟩ ⟨k', j⟩, rfl lemma block_diagonal'_apply (ik jk) : block_diagonal' M ik jk = if h : ik.1 = jk.1 then M ik.1 ik.2 (cast (congr_arg n' h.symm) jk.2) else 0 := by { cases ik, cases jk, refl } @[simp] lemma block_diagonal'_apply_eq (k i j) : block_diagonal' M ⟨k, i⟩ ⟨k, j⟩ = M k i j := dif_pos rfl lemma block_diagonal'_apply_ne {k k'} (i j) (h : k ≠ k') : block_diagonal' M ⟨k, i⟩ ⟨k', j⟩ = 0 := dif_neg h lemma block_diagonal'_map (f : α → β) (hf : f 0 = 0) : (block_diagonal' M).map f = block_diagonal' (λ k, (M k).map f) := begin ext, simp only [map_apply, block_diagonal'_apply, eq_comm], rw [apply_dite f, hf], end @[simp] lemma block_diagonal'_transpose : (block_diagonal' M)ᵀ = block_diagonal' (λ k, (M k)ᵀ) := begin ext ⟨ii, ix⟩ ⟨ji, jx⟩, simp only [transpose_apply, block_diagonal'_apply, eq_comm], dsimp only, split_ifs with h₁ h₂ h₂, { subst h₁, refl, }, { exact (h₂ h₁.symm).elim }, { exact (h₁ h₂.symm).elim }, { refl } end @[simp] lemma block_diagonal'_conj_transpose {α} [semiring α] [star_ring α] (M : Π i, matrix (m' i) (n' i) α) : (block_diagonal' M)ᴴ = block_diagonal' (λ k, (M k)ᴴ) := begin simp only [conj_transpose, block_diagonal'_transpose], exact block_diagonal'_map _ star (star_zero α), end @[simp] lemma block_diagonal'_zero : block_diagonal' (0 : Π i, matrix (m' i) (n' i) α) = 0 := by { ext, simp [block_diagonal'_apply] } @[simp] lemma block_diagonal'_diagonal [∀ i, decidable_eq (m' i)] (d : Π i, m' i → α) : block_diagonal' (λ k, diagonal (d k)) = diagonal (λ ik, d ik.1 ik.2) := begin ext ⟨i, k⟩ ⟨j, k'⟩, simp only [block_diagonal'_apply, diagonal], split_ifs; finish end @[simp] lemma block_diagonal'_one [∀ i, decidable_eq (m' i)] [has_one α] : block_diagonal' (1 : Π i, matrix (m' i) (m' i) α) = 1 := show block_diagonal' (λ (i : o), diagonal (λ (_ : m' i), (1 : α))) = diagonal (λ _, 1), by rw [block_diagonal'_diagonal] end has_zero @[simp] lemma block_diagonal'_add [add_monoid α] : block_diagonal' (M + N) = block_diagonal' M + block_diagonal' N := begin ext, simp only [block_diagonal'_apply, pi.add_apply], split_ifs; simp end @[simp] lemma block_diagonal'_neg [add_group α] : block_diagonal' (-M) = - block_diagonal' M := begin ext, simp only [block_diagonal'_apply, pi.neg_apply], split_ifs; simp end @[simp] lemma block_diagonal'_sub [add_group α] : block_diagonal' (M - N) = block_diagonal' M - block_diagonal' N := by simp [sub_eq_add_neg] @[simp] lemma block_diagonal'_mul {p : o → Type*} [semiring α] [Π i, fintype (n' i)] [fintype o] (N : Π i, matrix (n' i) (p i) α) : block_diagonal' (λ k, M k ⬝ N k) = block_diagonal' M ⬝ block_diagonal' N := begin ext ⟨k, i⟩ ⟨k', j⟩, simp only [block_diagonal'_apply, mul_apply, ← finset.univ_sigma_univ, finset.sum_sigma], rw fintype.sum_eq_single k, { split_ifs; simp }, { intros j' hj', exact finset.sum_eq_zero (λ _ _, by rw [dif_neg hj'.symm, zero_mul]) }, end @[simp] lemma block_diagonal'_smul {R : Type*} [semiring R] [add_comm_monoid α] [module R α] (x : R) : block_diagonal' (x • M) = x • block_diagonal' M := by { ext, simp only [block_diagonal'_apply, pi.smul_apply], split_ifs; simp } end block_diagonal' end matrix
7f1336062320f1f57c799f5a8f3b68bb7f793c8a
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/analysis/normed_space/continuous_affine_map.lean
c55cae476084dda377491f560821cd7697dc1af4
[ "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
2,441
lean
/- Copyright (c) 2021 Oliver Nash. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Oliver Nash -/ import topology.algebra.continuous_affine_map import analysis.normed_space.add_torsor import analysis.normed_space.affine_isometry /-! # Continuous affine maps between normed spaces. This file develops the theory of continuous affine maps between affine spaces modelled on normed spaces. -/ namespace continuous_affine_map variables {R V W P Q : Type*} variables [normed_field R] variables [normed_group V] [normed_space R V] [metric_space P] [normed_add_torsor V P] variables [normed_group W] [normed_space R W] [metric_space Q] [normed_add_torsor W Q] include V W /-- The linear map underlying a continuous affine map is continuous. -/ def cont_linear (f : P →A[R] Q) : V →L[R] W := { to_fun := f.linear, cont := by { rw affine_map.continuous_linear_iff, exact f.cont, }, .. f.linear, } @[simp] lemma coe_cont_linear_eq_linear (f : P →A[R] Q) : (f.cont_linear : V →ₗ[R] W) = (f : P →ᵃ[R] Q).linear := by { ext, refl, } @[simp] lemma coe_mk_const_linear_eq_linear (f : P →ᵃ[R] Q) (h) : ((⟨f, h⟩ : P →A[R] Q).cont_linear : V → W) = f.linear := rfl @[simp] lemma coe_linear_eq_coe_cont_linear (f : P →A[R] Q) : ((f : P →ᵃ[R] Q).linear : V → W) = (⇑f.cont_linear : V → W) := rfl @[simp] lemma map_vadd (f : P →A[R] Q) (p : P) (v : V) : f (v +ᵥ p) = f.cont_linear v +ᵥ f p := f.map_vadd' p v @[simp] lemma cont_linear_map_vsub (f : P →A[R] Q) (p₁ p₂ : P) : f.cont_linear (p₁ -ᵥ p₂) = f p₁ -ᵥ f p₂ := f.to_affine_map.linear_map_vsub p₁ p₂ @[simp] lemma const_cont_linear (q : Q) : (const R P q).cont_linear = 0 := rfl lemma cont_linear_eq_zero_iff_exists_const (f : P →A[R] Q) : f.cont_linear = 0 ↔ ∃ q, f = const R P q := begin have h₁ : f.cont_linear = 0 ↔ (f : P →ᵃ[R] Q).linear = 0, { refine ⟨λ h, _, λ h, _⟩; ext, { rw [← coe_cont_linear_eq_linear, h], refl, }, { rw [← coe_linear_eq_coe_cont_linear, h], refl, }, }, have h₂ : ∀ (q : Q), f = const R P q ↔ (f : P →ᵃ[R] Q) = affine_map.const R P q, { intros q, refine ⟨λ h, _, λ h, _⟩; ext, { rw h, refl, }, { rw [← coe_to_affine_map, h], refl, }, }, simp_rw [h₁, h₂], exact (f : P →ᵃ[R] Q).linear_eq_zero_iff_exists_const, end end continuous_affine_map
cd618e4069789059eb38a1ac977f5c3fecd93c4d
6c56e3fbe5d7711b66a3b46f1810b44cfa109768
/src/even.lean
5c7e845e485e456495a1843adb40958c577b9199
[]
no_license
kodyvajjha/lean-mmc2021
015e7ba9d2f43397582d42ad282e88c8ce876dc3
cc774208f9dc041cad4def289357301b4a0a8173
refs/heads/master
1,689,416,888,246
1,630,161,054,000
1,630,161,054,000
384,140,257
1
0
null
null
null
null
UTF-8
Lean
false
false
311
lean
import tactic namespace myeven def is_even (n : ℕ) := ∃ p : ℕ, n = 2*p theorem inf_many_evens : ∀ n:ℕ, ∃ k : ℕ, (k > n) ∧ (is_even k) := begin assume n, -- assume an arbitrary n use (2*(n+1)), split, { linarith, }, { unfold is_even, use (n+1), } end end myeven
80bdcbadf726553ceb5502263cb207768cec2231
302c785c90d40ad3d6be43d33bc6a558354cc2cf
/src/group_theory/dihedral_group.lean
d86d32996ef7fc1194babcc5788a5cc3baa0a8b5
[ "Apache-2.0" ]
permissive
ilitzroth/mathlib
ea647e67f1fdfd19a0f7bdc5504e8acec6180011
5254ef14e3465f6504306132fe3ba9cec9ffff16
refs/heads/master
1,680,086,661,182
1,617,715,647,000
1,617,715,647,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
5,476
lean
/- Copyright (c) 2020 Shing Tak Lam. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Shing Tak Lam -/ import data.zmod.basic import group_theory.order_of_element /-! # Dihedral Groups We define the dihedral groups `dihedral_group n`, with elements `r i` and `sr i` for `i : zmod n`. For `n ≠ 0`, `dihedral_group n` represents the symmetry group of the regular `n`-gon. `r i` represents the rotations of the `n`-gon by `2πi/n`, and `sr i` represents the reflections of the `n`-gon. `dihedral_group 0` corresponds to the infinite dihedral group. -/ /-- For `n ≠ 0`, `dihedral_group n` represents the symmetry group of the regular `n`-gon. `r i` represents the rotations of the `n`-gon by `2πi/n`, and `sr i` represents the reflections of the `n`-gon. `dihedral_group 0` corresponds to the infinite dihedral group. -/ @[derive decidable_eq] inductive dihedral_group (n : ℕ) : Type | r : zmod n → dihedral_group | sr : zmod n → dihedral_group namespace dihedral_group variables {n : ℕ} /-- Multiplication of the dihedral group. -/ private def mul : dihedral_group n → dihedral_group n → dihedral_group n | (r i) (r j) := r (i + j) | (r i) (sr j) := sr (j - i) | (sr i) (r j) := sr (i + j) | (sr i) (sr j) := r (j - i) /-- The identity `1` is the rotation by `0`. -/ private def one : dihedral_group n := r 0 instance : inhabited (dihedral_group n) := ⟨one⟩ /-- The inverse of a an element of the dihedral group. -/ private def inv : dihedral_group n → dihedral_group n | (r i) := r (-i) | (sr i) := sr i /-- The group structure on `dihedral_group n`. -/ instance : group (dihedral_group n) := { mul := mul, mul_assoc := begin rintros (a | a) (b | b) (c | c); simp only [mul]; ring, end, one := one, one_mul := begin rintros (a | a), exact congr_arg r (zero_add a), exact congr_arg sr (sub_zero a), end, mul_one := begin rintros (a | a), exact congr_arg r (add_zero a), exact congr_arg sr (add_zero a), end, inv := inv, mul_left_inv := begin rintros (a | a), exact congr_arg r (neg_add_self a), exact congr_arg r (sub_self a), end } @[simp] lemma r_mul_r (i j : zmod n) : r i * r j = r (i + j) := rfl @[simp] lemma r_mul_sr (i j : zmod n) : r i * sr j = sr (j - i) := rfl @[simp] lemma sr_mul_r (i j : zmod n) : sr i * r j = sr (i + j) := rfl @[simp] lemma sr_mul_sr (i j : zmod n) : sr i * sr j = r (j - i) := rfl lemma one_def : (1 : dihedral_group n) = r 0 := rfl private def fintype_helper : (zmod n ⊕ zmod n) ≃ dihedral_group n := { inv_fun := λ i, match i with | (r j) := sum.inl j | (sr j) := sum.inr j end, to_fun := λ i, match i with | (sum.inl j) := r j | (sum.inr j) := sr j end, left_inv := by rintro (x | x); refl, right_inv := by rintro (x | x); refl } /-- If `0 < n`, then `dihedral_group n` is a finite group. -/ instance [fact (0 < n)] : fintype (dihedral_group n) := fintype.of_equiv _ fintype_helper instance : nontrivial (dihedral_group n) := ⟨⟨r 0, sr 0, dec_trivial⟩⟩ /-- If `0 < n`, then `dihedral_group n` has `2n` elements. -/ lemma card [fact (0 < n)] : fintype.card (dihedral_group n) = 2 * n := by rw [← fintype.card_eq.mpr ⟨fintype_helper⟩, fintype.card_sum, zmod.card, two_mul] @[simp] lemma r_one_pow (k : ℕ) : (r 1 : dihedral_group n) ^ k = r k := begin induction k with k IH, { refl }, { rw [pow_succ, IH, r_mul_r], congr' 1, norm_cast, rw nat.one_add } end @[simp] lemma r_one_pow_n : (r (1 : zmod n))^n = 1 := begin cases n, { rw pow_zero }, { rw [r_one_pow, one_def], congr' 1, exact zmod.nat_cast_self _, } end @[simp] lemma sr_mul_self (i : zmod n) : sr i * sr i = 1 := by rw [sr_mul_sr, sub_self, one_def] /-- If `0 < n`, then `sr i` has order 2. -/ @[simp] lemma order_of_sr (i : zmod n) : order_of (sr i) = 2 := begin rw order_of_eq_prime _ _, { exact ⟨nat.prime_two⟩ }, rw [pow_two, sr_mul_self], dec_trivial, end /-- If `0 < n`, then `r 1` has order `n`. -/ @[simp] lemma order_of_r_one : order_of (r 1 : dihedral_group n) = n := begin by_cases hnpos : 0 < n, { haveI : fact (0 < n) := ⟨hnpos⟩, cases lt_or_eq_of_le (nat.le_of_dvd hnpos (order_of_dvd_of_pow_eq_one (@r_one_pow_n n))) with h h, { have h1 : (r 1 : dihedral_group n)^(order_of (r 1)) = 1, { exact pow_order_of_eq_one _ }, rw r_one_pow at h1, injection h1 with h2, rw [← zmod.val_eq_zero, zmod.val_nat_cast, nat.mod_eq_of_lt h] at h2, apply absurd h2.symm, apply ne_of_lt, exact absurd h2.symm (ne_of_lt (order_of_pos _)) }, { exact h } }, { simp only [not_lt, nonpos_iff_eq_zero] at hnpos, rw hnpos, apply order_of_eq_zero, intros m hm, rw [r_one_pow, one_def], by_contradiction, rw not_not at h, have h' : (m : zmod 0) = 0, { exact r.inj h, }, have h'' : m = 0, { simp only [int.coe_nat_eq_zero, int.nat_cast_eq_coe_nat] at h', exact h', }, rw h'' at hm, apply nat.lt_irrefl, exact hm }, end /-- If `0 < n`, then `i : zmod n` has order `n / gcd n i`. -/ lemma order_of_r [fact (0 < n)] (i : zmod n) : order_of (r i) = n / nat.gcd n i.val := begin conv_lhs { rw ←zmod.nat_cast_zmod_val i }, rw [←r_one_pow, order_of_pow, order_of_r_one] end end dihedral_group
37024149b999b37a2aa2f3cdf59a1714e4d37d62
626e312b5c1cb2d88fca108f5933076012633192
/src/ring_theory/noetherian.lean
445ef2e7c7814144be1a36063a3124600b8681f6
[ "Apache-2.0" ]
permissive
Bioye97/mathlib
9db2f9ee54418d29dd06996279ba9dc874fd6beb
782a20a27ee83b523f801ff34efb1a9557085019
refs/heads/master
1,690,305,956,488
1,631,067,774,000
1,631,067,774,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
35,871
lean
/- Copyright (c) 2018 Mario Carneiro, Kevin Buzzard. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Kevin Buzzard -/ import algebraic_geometry.prime_spectrum import data.multiset.finset_ops import linear_algebra.linear_independent import order.order_iso_nat import order.compactly_generated import ring_theory.ideal.operations import group_theory.finiteness /-! # Noetherian rings and modules The following are equivalent for a module M over a ring R: 1. Every increasing chain of submodules M₁ ⊆ M₂ ⊆ M₃ ⊆ ⋯ eventually stabilises. 2. Every submodule is finitely generated. A module satisfying these equivalent conditions is said to be a *Noetherian* R-module. A ring is a *Noetherian ring* if it is Noetherian as a module over itself. (Note that we do not assume yet that our rings are commutative, so perhaps this should be called "left Noetherian". To avoid cumbersome names once we specialize to the commutative case, we don't make this explicit in the declaration names.) ## Main definitions Let `R` be a ring and let `M` and `P` be `R`-modules. Let `N` be an `R`-submodule of `M`. * `fg N : Prop` is the assertion that `N` is finitely generated as an `R`-module. * `is_noetherian R M` is the proposition that `M` is a Noetherian `R`-module. It is a class, implemented as the predicate that all `R`-submodules of `M` are finitely generated. ## Main statements * `exists_sub_one_mem_and_smul_eq_zero_of_fg_of_le_smul` is Nakayama's lemma, in the following form: if N is a finitely generated submodule of an ambient R-module M and I is an ideal of R such that N ⊆ IN, then there exists r ∈ 1 + I such that rN = 0. * `is_noetherian_iff_well_founded` is the theorem that an R-module M is Noetherian iff `>` is well-founded on `submodule R M`. Note that the Hilbert basis theorem, that if a commutative ring R is Noetherian then so is R[X], is proved in `ring_theory.polynomial`. ## References * [M. F. Atiyah and I. G. Macdonald, *Introduction to commutative algebra*][atiyah-macdonald] * [samuel] ## Tags Noetherian, noetherian, Noetherian ring, Noetherian module, noetherian ring, noetherian module -/ open set open_locale big_operators pointwise namespace submodule variables {R : Type*} {M : Type*} [semiring R] [add_comm_monoid M] [module R M] /-- A submodule of `M` is finitely generated if it is the span of a finite subset of `M`. -/ def fg (N : submodule R M) : Prop := ∃ S : finset M, submodule.span R ↑S = N theorem fg_def {N : submodule R M} : N.fg ↔ ∃ S : set M, finite S ∧ span R S = N := ⟨λ ⟨t, h⟩, ⟨_, finset.finite_to_set t, h⟩, begin rintro ⟨t', h, rfl⟩, rcases finite.exists_finset_coe h with ⟨t, rfl⟩, exact ⟨t, rfl⟩ end⟩ lemma fg_iff_add_submonoid_fg (P : submodule ℕ M) : P.fg ↔ P.to_add_submonoid.fg := ⟨λ ⟨S, hS⟩, ⟨S, by simpa [← span_nat_eq_add_submonoid_closure] using hS⟩, λ ⟨S, hS⟩, ⟨S, by simpa [← span_nat_eq_add_submonoid_closure] using hS⟩⟩ lemma fg_iff_add_subgroup_fg {G : Type*} [add_comm_group G] (P : submodule ℤ G) : P.fg ↔ P.to_add_subgroup.fg := ⟨λ ⟨S, hS⟩, ⟨S, by simpa [← span_int_eq_add_subgroup_closure] using hS⟩, λ ⟨S, hS⟩, ⟨S, by simpa [← span_int_eq_add_subgroup_closure] using hS⟩⟩ lemma fg_iff_exists_fin_generating_family {N : submodule R M} : N.fg ↔ ∃ (n : ℕ) (s : fin n → M), span R (range s) = N := begin rw fg_def, split, { rintros ⟨S, Sfin, hS⟩, obtain ⟨n, f, rfl⟩ := Sfin.fin_embedding, exact ⟨n, f, hS⟩, }, { rintros ⟨n, s, hs⟩, refine ⟨range s, finite_range s, hs⟩ }, end /-- **Nakayama's Lemma**. Atiyah-Macdonald 2.5, Eisenbud 4.7, Matsumura 2.2, [Stacks 00DV](https://stacks.math.columbia.edu/tag/00VL) -/ theorem exists_sub_one_mem_and_smul_eq_zero_of_fg_of_le_smul {R : Type*} [comm_ring R] {M : Type*} [add_comm_group M] [module R M] (I : ideal R) (N : submodule R M) (hn : N.fg) (hin : N ≤ I • N) : ∃ r : R, r - 1 ∈ I ∧ ∀ n ∈ N, r • n = (0 : M) := begin rw fg_def at hn, rcases hn with ⟨s, hfs, hs⟩, have : ∃ r : R, r - 1 ∈ I ∧ N ≤ (I • span R s).comap (linear_map.lsmul R M r) ∧ s ⊆ N, { refine ⟨1, _, _, _⟩, { rw sub_self, exact I.zero_mem }, { rw [hs], intros n hn, rw [mem_comap], change (1:R) • n ∈ I • N, rw one_smul, exact hin hn }, { rw [← span_le, hs], exact le_refl N } }, clear hin hs, revert this, refine set.finite.dinduction_on hfs (λ H, _) (λ i s his hfs ih H, _), { rcases H with ⟨r, hr1, hrn, hs⟩, refine ⟨r, hr1, λ n hn, _⟩, specialize hrn hn, rwa [mem_comap, span_empty, smul_bot, mem_bot] at hrn }, apply ih, rcases H with ⟨r, hr1, hrn, hs⟩, rw [← set.singleton_union, span_union, smul_sup] at hrn, rw [set.insert_subset] at hs, have : ∃ c : R, c - 1 ∈ I ∧ c • i ∈ I • span R s, { specialize hrn hs.1, rw [mem_comap, mem_sup] at hrn, rcases hrn with ⟨y, hy, z, hz, hyz⟩, change y + z = r • i at hyz, rw mem_smul_span_singleton at hy, rcases hy with ⟨c, hci, rfl⟩, use r-c, split, { rw [sub_right_comm], exact I.sub_mem hr1 hci }, { rw [sub_smul, ← hyz, add_sub_cancel'], exact hz } }, rcases this with ⟨c, hc1, hci⟩, refine ⟨c * r, _, _, hs.2⟩, { rw [← ideal.quotient.eq, ring_hom.map_one] at hr1 hc1 ⊢, rw [ring_hom.map_mul, hc1, hr1, mul_one] }, { intros n hn, specialize hrn hn, rw [mem_comap, mem_sup] at hrn, rcases hrn with ⟨y, hy, z, hz, hyz⟩, change y + z = r • n at hyz, rw mem_smul_span_singleton at hy, rcases hy with ⟨d, hdi, rfl⟩, change _ • _ ∈ I • span R s, rw [mul_smul, ← hyz, smul_add, smul_smul, mul_comm, mul_smul], exact add_mem _ (smul_mem _ _ hci) (smul_mem _ _ hz) } end theorem fg_bot : (⊥ : submodule R M).fg := ⟨∅, by rw [finset.coe_empty, span_empty]⟩ theorem fg_span {s : set M} (hs : finite s) : fg (span R s) := ⟨hs.to_finset, by rw [hs.coe_to_finset]⟩ theorem fg_span_singleton (x : M) : fg (R ∙ x) := fg_span (finite_singleton x) theorem fg_sup {N₁ N₂ : submodule R M} (hN₁ : N₁.fg) (hN₂ : N₂.fg) : (N₁ ⊔ N₂).fg := let ⟨t₁, ht₁⟩ := fg_def.1 hN₁, ⟨t₂, ht₂⟩ := fg_def.1 hN₂ in fg_def.2 ⟨t₁ ∪ t₂, ht₁.1.union ht₂.1, by rw [span_union, ht₁.2, ht₂.2]⟩ variables {P : Type*} [add_comm_monoid P] [module R P] variables {f : M →ₗ[R] P} theorem fg_map {N : submodule R M} (hs : N.fg) : (N.map f).fg := let ⟨t, ht⟩ := fg_def.1 hs in fg_def.2 ⟨f '' t, ht.1.image _, by rw [span_image, ht.2]⟩ lemma fg_of_fg_map {R M P : Type*} [ring R] [add_comm_group M] [module R M] [add_comm_group P] [module R P] (f : M →ₗ[R] P) (hf : f.ker = ⊥) {N : submodule R M} (hfn : (N.map f).fg) : N.fg := let ⟨t, ht⟩ := hfn in ⟨t.preimage f $ λ x _ y _ h, linear_map.ker_eq_bot.1 hf h, linear_map.map_injective hf $ by { rw [f.map_span, finset.coe_preimage, set.image_preimage_eq_inter_range, set.inter_eq_self_of_subset_left, ht], rw [← linear_map.range_coe, ← span_le, ht, ← map_top], exact map_mono le_top }⟩ lemma fg_top {R M : Type*} [ring R] [add_comm_group M] [module R M] (N : submodule R M) : (⊤ : submodule R N).fg ↔ N.fg := ⟨λ h, N.range_subtype ▸ map_top N.subtype ▸ fg_map h, λ h, fg_of_fg_map N.subtype N.ker_subtype $ by rwa [map_top, range_subtype]⟩ lemma fg_of_linear_equiv (e : M ≃ₗ[R] P) (h : (⊤ : submodule R P).fg) : (⊤ : submodule R M).fg := e.symm.range ▸ map_top (e.symm : P →ₗ[R] M) ▸ fg_map h theorem fg_prod {sb : submodule R M} {sc : submodule R P} (hsb : sb.fg) (hsc : sc.fg) : (sb.prod sc).fg := let ⟨tb, htb⟩ := fg_def.1 hsb, ⟨tc, htc⟩ := fg_def.1 hsc in fg_def.2 ⟨linear_map.inl R M P '' tb ∪ linear_map.inr R M P '' tc, (htb.1.image _).union (htc.1.image _), by rw [linear_map.span_inl_union_inr, htb.2, htc.2]⟩ /-- If 0 → M' → M → M'' → 0 is exact and M' and M'' are finitely generated then so is M. -/ theorem fg_of_fg_map_of_fg_inf_ker {R M P : Type*} [ring R] [add_comm_group M] [module R M] [add_comm_group P] [module R P] (f : M →ₗ[R] P) {s : submodule R M} (hs1 : (s.map f).fg) (hs2 : (s ⊓ f.ker).fg) : s.fg := begin haveI := classical.dec_eq R, haveI := classical.dec_eq M, haveI := classical.dec_eq P, cases hs1 with t1 ht1, cases hs2 with t2 ht2, have : ∀ y ∈ t1, ∃ x ∈ s, f x = y, { intros y hy, have : y ∈ map f s, { rw ← ht1, exact subset_span hy }, rcases mem_map.1 this with ⟨x, hx1, hx2⟩, exact ⟨x, hx1, hx2⟩ }, have : ∃ g : P → M, ∀ y ∈ t1, g y ∈ s ∧ f (g y) = y, { choose g hg1 hg2, existsi λ y, if H : y ∈ t1 then g y H else 0, intros y H, split, { simp only [dif_pos H], apply hg1 }, { simp only [dif_pos H], apply hg2 } }, cases this with g hg, clear this, existsi t1.image g ∪ t2, rw [finset.coe_union, span_union, finset.coe_image], apply le_antisymm, { refine sup_le (span_le.2 $ image_subset_iff.2 _) (span_le.2 _), { intros y hy, exact (hg y hy).1 }, { intros x hx, have := subset_span hx, rw ht2 at this, exact this.1 } }, intros x hx, have : f x ∈ map f s, { rw mem_map, exact ⟨x, hx, rfl⟩ }, rw [← ht1,← set.image_id ↑t1, finsupp.mem_span_image_iff_total] at this, rcases this with ⟨l, hl1, hl2⟩, refine mem_sup.2 ⟨(finsupp.total M M R id).to_fun ((finsupp.lmap_domain R R g : (P →₀ R) → M →₀ R) l), _, x - finsupp.total M M R id ((finsupp.lmap_domain R R g : (P →₀ R) → M →₀ R) l), _, add_sub_cancel'_right _ _⟩, { rw [← set.image_id (g '' ↑t1), finsupp.mem_span_image_iff_total], refine ⟨_, _, rfl⟩, haveI : inhabited P := ⟨0⟩, rw [← finsupp.lmap_domain_supported _ _ g, mem_map], refine ⟨l, hl1, _⟩, refl, }, rw [ht2, mem_inf], split, { apply s.sub_mem hx, rw [finsupp.total_apply, finsupp.lmap_domain_apply, finsupp.sum_map_domain_index], refine s.sum_mem _, { intros y hy, exact s.smul_mem _ (hg y (hl1 hy)).1 }, { exact zero_smul _ }, { exact λ _ _ _, add_smul _ _ _ } }, { rw [linear_map.mem_ker, f.map_sub, ← hl2], rw [finsupp.total_apply, finsupp.total_apply, finsupp.lmap_domain_apply], rw [finsupp.sum_map_domain_index, finsupp.sum, finsupp.sum, f.map_sum], rw sub_eq_zero, refine finset.sum_congr rfl (λ y hy, _), unfold id, rw [f.map_smul, (hg y (hl1 hy)).2], { exact zero_smul _ }, { exact λ _ _ _, add_smul _ _ _ } } end /-- The image of a finitely generated ideal is finitely generated. -/ lemma map_fg_of_fg {R S : Type*} [comm_ring R] [comm_ring S] (I : ideal R) (h : I.fg) (f : R →+* S) : (I.map f).fg := begin obtain ⟨X, hXfin, hXgen⟩ := fg_def.1 h, apply fg_def.2, refine ⟨set.image f X, finite.image ⇑f hXfin, _⟩, rw [ideal.map, ideal.span, ← hXgen], refine le_antisymm (submodule.span_mono (image_subset _ ideal.subset_span)) _, rw [submodule.span_le, image_subset_iff], intros i hi, refine submodule.span_induction hi (λ x hx, _) _ (λ x y hx hy, _) (λ r x hx, _), { simp only [set_like.mem_coe, mem_preimage], suffices : f x ∈ f '' X, { exact ideal.subset_span this }, exact mem_image_of_mem ⇑f hx }, { simp only [set_like.mem_coe, ring_hom.map_zero, mem_preimage, zero_mem] }, { simp only [set_like.mem_coe, mem_preimage] at hx hy, simp only [ring_hom.map_add, set_like.mem_coe, mem_preimage], exact submodule.add_mem _ hx hy }, { simp only [set_like.mem_coe, mem_preimage] at hx, simp only [algebra.id.smul_eq_mul, set_like.mem_coe, mem_preimage, ring_hom.map_mul], exact submodule.smul_mem _ _ hx } end /-- The kernel of the composition of two linear maps is finitely generated if both kernels are and the first morphism is surjective. -/ lemma fg_ker_comp {R M N P : Type*} [ring R] [add_comm_group M] [module R M] [add_comm_group N] [module R N] [add_comm_group P] [module R P] (f : M →ₗ[R] N) (g : N →ₗ[R] P) (hf1 : f.ker.fg) (hf2 : g.ker.fg) (hsur : function.surjective f) : (g.comp f).ker.fg := begin rw linear_map.ker_comp, apply fg_of_fg_map_of_fg_inf_ker f, { rwa [linear_map.map_comap_eq, linear_map.range_eq_top.2 hsur, top_inf_eq] }, { rwa [inf_of_le_right (show f.ker ≤ (comap f g.ker), from comap_mono (@bot_le _ _ g.ker))] } end lemma fg_restrict_scalars {R S M : Type*} [comm_ring R] [comm_ring S] [algebra R S] [add_comm_group M] [module S M] [module R M] [is_scalar_tower R S M] (N : submodule S M) (hfin : N.fg) (h : function.surjective (algebra_map R S)) : (submodule.restrict_scalars R N).fg := begin obtain ⟨X, rfl⟩ := hfin, use X, exact submodule.span_eq_restrict_scalars R S M X h end lemma fg_ker_ring_hom_comp {R S A : Type*} [comm_ring R] [comm_ring S] [comm_ring A] (f : R →+* S) (g : S →+* A) (hf : f.ker.fg) (hg : g.ker.fg) (hsur : function.surjective f) : (g.comp f).ker.fg := begin letI : algebra R S := ring_hom.to_algebra f, letI : algebra R A := ring_hom.to_algebra (g.comp f), letI : algebra S A := ring_hom.to_algebra g, letI : is_scalar_tower R S A := is_scalar_tower.of_algebra_map_eq (λ _, rfl), let f₁ := algebra.linear_map R S, let g₁ := (is_scalar_tower.to_alg_hom R S A).to_linear_map, exact fg_ker_comp f₁ g₁ hf (fg_restrict_scalars g.ker hg hsur) hsur end /-- Finitely generated submodules are precisely compact elements in the submodule lattice. -/ theorem fg_iff_compact (s : submodule R M) : s.fg ↔ complete_lattice.is_compact_element s := begin classical, -- Introduce shorthand for span of an element let sp : M → submodule R M := λ a, span R {a}, -- Trivial rewrite lemma; a small hack since simp (only) & rw can't accomplish this smoothly. have supr_rw : ∀ t : finset M, (⨆ x ∈ t, sp x) = (⨆ x ∈ (↑t : set M), sp x), from λ t, by refl, split, { rintro ⟨t, rfl⟩, rw [span_eq_supr_of_singleton_spans, ←supr_rw, ←(finset.sup_eq_supr t sp)], apply complete_lattice.finset_sup_compact_of_compact, exact λ n _, singleton_span_is_compact_element n, }, { intro h, -- s is the Sup of the spans of its elements. have sSup : s = Sup (sp '' ↑s), by rw [Sup_eq_supr, supr_image, ←span_eq_supr_of_singleton_spans, eq_comm, span_eq], -- by h, s is then below (and equal to) the sup of the spans of finitely many elements. obtain ⟨u, ⟨huspan, husup⟩⟩ := h (sp '' ↑s) (le_of_eq sSup), have ssup : s = u.sup id, { suffices : u.sup id ≤ s, from le_antisymm husup this, rw [sSup, finset.sup_id_eq_Sup], exact Sup_le_Sup huspan, }, obtain ⟨t, ⟨hts, rfl⟩⟩ := finset.subset_image_iff.mp huspan, rw [finset.sup_finset_image, function.comp.left_id, finset.sup_eq_supr, supr_rw, ←span_eq_supr_of_singleton_spans, eq_comm] at ssup, exact ⟨t, ssup⟩, }, end end submodule /-- `is_noetherian R M` is the proposition that `M` is a Noetherian `R`-module, implemented as the predicate that all `R`-submodules of `M` are finitely generated. -/ class is_noetherian (R M) [semiring R] [add_comm_monoid M] [module R M] : Prop := (noetherian : ∀ (s : submodule R M), s.fg) section variables {R : Type*} {M : Type*} {P : Type*} variables [ring R] [add_comm_group M] [add_comm_group P] variables [module R M] [module R P] open is_noetherian include R /-- An R-module is Noetherian iff all its submodules are finitely-generated. -/ lemma is_noetherian_def : is_noetherian R M ↔ ∀ (s : submodule R M), s.fg := ⟨λ h, h.noetherian, is_noetherian.mk⟩ theorem is_noetherian_submodule {N : submodule R M} : is_noetherian R N ↔ ∀ s : submodule R M, s ≤ N → s.fg := ⟨λ ⟨hn⟩, λ s hs, have s ≤ N.subtype.range, from (N.range_subtype).symm ▸ hs, linear_map.map_comap_eq_self this ▸ submodule.fg_map (hn _), λ h, ⟨λ s, submodule.fg_of_fg_map_of_fg_inf_ker N.subtype (h _ $ submodule.map_subtype_le _ _) $ by rw [submodule.ker_subtype, inf_bot_eq]; exact submodule.fg_bot⟩⟩ theorem is_noetherian_submodule_left {N : submodule R M} : is_noetherian R N ↔ ∀ s : submodule R M, (N ⊓ s).fg := is_noetherian_submodule.trans ⟨λ H s, H _ inf_le_left, λ H s hs, (inf_of_le_right hs) ▸ H _⟩ theorem is_noetherian_submodule_right {N : submodule R M} : is_noetherian R N ↔ ∀ s : submodule R M, (s ⊓ N).fg := is_noetherian_submodule.trans ⟨λ H s, H _ inf_le_right, λ H s hs, (inf_of_le_left hs) ▸ H _⟩ instance is_noetherian_submodule' [is_noetherian R M] (N : submodule R M) : is_noetherian R N := is_noetherian_submodule.2 $ λ _ _, is_noetherian.noetherian _ lemma is_noetherian_of_le {s t : submodule R M} [ht : is_noetherian R t] (h : s ≤ t) : is_noetherian R s := is_noetherian_submodule.mpr (λ s' hs', is_noetherian_submodule.mp ht _ (le_trans hs' h)) variable (M) theorem is_noetherian_of_surjective (f : M →ₗ[R] P) (hf : f.range = ⊤) [is_noetherian R M] : is_noetherian R P := ⟨λ s, have (s.comap f).map f = s, from linear_map.map_comap_eq_self $ hf.symm ▸ le_top, this ▸ submodule.fg_map $ noetherian _⟩ variable {M} theorem is_noetherian_of_linear_equiv (f : M ≃ₗ[R] P) [is_noetherian R M] : is_noetherian R P := is_noetherian_of_surjective _ f.to_linear_map f.range lemma is_noetherian_of_injective [is_noetherian R P] (f : M →ₗ[R] P) (hf : f.ker = ⊥) : is_noetherian R M := is_noetherian_of_linear_equiv (linear_equiv.of_injective f hf).symm lemma fg_of_injective [is_noetherian R P] {N : submodule R M} (f : M →ₗ[R] P) (hf : f.ker = ⊥) : N.fg := @@is_noetherian.noetherian _ _ _ (is_noetherian_of_injective f hf) N instance is_noetherian_prod [is_noetherian R M] [is_noetherian R P] : is_noetherian R (M × P) := ⟨λ s, submodule.fg_of_fg_map_of_fg_inf_ker (linear_map.snd R M P) (noetherian _) $ have s ⊓ linear_map.ker (linear_map.snd R M P) ≤ linear_map.range (linear_map.inl R M P), from λ x ⟨hx1, hx2⟩, ⟨x.1, prod.ext rfl $ eq.symm $ linear_map.mem_ker.1 hx2⟩, linear_map.map_comap_eq_self this ▸ submodule.fg_map (noetherian _)⟩ instance is_noetherian_pi {R ι : Type*} {M : ι → Type*} [ring R] [Π i, add_comm_group (M i)] [Π i, module R (M i)] [fintype ι] [∀ i, is_noetherian R (M i)] : is_noetherian R (Π i, M i) := begin haveI := classical.dec_eq ι, suffices on_finset : ∀ s : finset ι, is_noetherian R (Π i : s, M i), { let coe_e := equiv.subtype_univ_equiv finset.mem_univ, letI : is_noetherian R (Π i : finset.univ, M (coe_e i)) := on_finset finset.univ, exact is_noetherian_of_linear_equiv (linear_equiv.Pi_congr_left R M coe_e), }, intro s, induction s using finset.induction with a s has ih, { split, intro s, convert submodule.fg_bot, apply eq_bot_iff.2, intros x hx, refine (submodule.mem_bot R).2 _, ext i, cases i.2 }, refine @is_noetherian_of_linear_equiv _ _ _ _ _ _ _ _ _ (@is_noetherian_prod _ (M a) _ _ _ _ _ _ _ ih), fconstructor, { exact λ f i, or.by_cases (finset.mem_insert.1 i.2) (λ h : i.1 = a, show M i.1, from (eq.rec_on h.symm f.1)) (λ h : i.1 ∈ s, show M i.1, from f.2 ⟨i.1, h⟩) }, { intros f g, ext i, unfold or.by_cases, cases i with i hi, rcases finset.mem_insert.1 hi with rfl | h, { change _ = _ + _, simp only [dif_pos], refl }, { change _ = _ + _, have : ¬i = a, { rintro rfl, exact has h }, simp only [dif_neg this, dif_pos h], refl } }, { intros c f, ext i, unfold or.by_cases, cases i with i hi, rcases finset.mem_insert.1 hi with rfl | h, { change _ = c • _, simp only [dif_pos], refl }, { change _ = c • _, have : ¬i = a, { rintro rfl, exact has h }, simp only [dif_neg this, dif_pos h], refl } }, { exact λ f, (f ⟨a, finset.mem_insert_self _ _⟩, λ i, f ⟨i.1, finset.mem_insert_of_mem i.2⟩) }, { intro f, apply prod.ext, { simp only [or.by_cases, dif_pos] }, { ext ⟨i, his⟩, have : ¬i = a, { rintro rfl, exact has his }, dsimp only [or.by_cases], change i ∈ s at his, rw [dif_neg this, dif_pos his] } }, { intro f, ext ⟨i, hi⟩, rcases finset.mem_insert.1 hi with rfl | h, { simp only [or.by_cases, dif_pos], }, { have : ¬i = a, { rintro rfl, exact has h }, simp only [or.by_cases, dif_neg this, dif_pos h], } } end /-- A version of `is_noetherian_pi` for non-dependent functions. We need this instance because sometimes Lean fails to apply the dependent version in non-dependent settings (e.g., it fails to prove that `ι → ℝ` is finite dimensional over `ℝ`). -/ instance is_noetherian_pi' {R ι M : Type*} [ring R] [add_comm_group M] [module R M] [fintype ι] [is_noetherian R M] : is_noetherian R (ι → M) := is_noetherian_pi end open is_noetherian submodule function section variables {R M : Type*} [ring R] [add_comm_group M] [module R M] theorem is_noetherian_iff_well_founded : is_noetherian R M ↔ well_founded ((>) : submodule R M → submodule R M → Prop) := begin rw (complete_lattice.well_founded_characterisations $ submodule R M).out 0 3, exact ⟨λ ⟨h⟩, λ k, (fg_iff_compact k).mp (h k), λ h, ⟨λ k, (fg_iff_compact k).mpr (h k)⟩⟩, end variables (R M) lemma well_founded_submodule_gt (R M) [ring R] [add_comm_group M] [module R M] : ∀ [is_noetherian R M], well_founded ((>) : submodule R M → submodule R M → Prop) := is_noetherian_iff_well_founded.mp variables {R M} lemma finite_of_linear_independent [nontrivial R] [is_noetherian R M] {s : set M} (hs : linear_independent R (coe : s → M)) : s.finite := begin refine classical.by_contradiction (λ hf, (rel_embedding.well_founded_iff_no_descending_seq.1 (well_founded_submodule_gt R M)).elim' _), have f : ℕ ↪ s, from @infinite.nat_embedding s ⟨λ f, hf ⟨f⟩⟩, have : ∀ n, (coe ∘ f) '' {m | m ≤ n} ⊆ s, { rintros n x ⟨y, hy₁, hy₂⟩, subst hy₂, exact (f y).2 }, have : ∀ a b : ℕ, a ≤ b ↔ span R ((coe ∘ f) '' {m | m ≤ a}) ≤ span R ((coe ∘ f) '' {m | m ≤ b}), { assume a b, rw [span_le_span_iff hs (this a) (this b), set.image_subset_image_iff (subtype.coe_injective.comp f.injective), set.subset_def], exact ⟨λ hab x (hxa : x ≤ a), le_trans hxa hab, λ hx, hx a (le_refl a)⟩ }, exact ⟨⟨λ n, span R ((coe ∘ f) '' {m | m ≤ n}), λ x y, by simp [le_antisymm_iff, (this _ _).symm] {contextual := tt}⟩, by dsimp [gt]; simp only [lt_iff_le_not_le, (this _ _).symm]; tauto⟩ end /-- A module is Noetherian iff every nonempty set of submodules has a maximal submodule among them. -/ theorem set_has_maximal_iff_noetherian : (∀ a : set $ submodule R M, a.nonempty → ∃ M' ∈ a, ∀ I ∈ a, M' ≤ I → I = M') ↔ is_noetherian R M := by rw [is_noetherian_iff_well_founded, well_founded.well_founded_iff_has_max'] /-- A module is Noetherian iff every increasing chain of submodules stabilizes. -/ theorem monotone_stabilizes_iff_noetherian : (∀ (f : ℕ →ₘ submodule R M), ∃ n, ∀ m, n ≤ m → f n = f m) ↔ is_noetherian R M := by rw [is_noetherian_iff_well_founded, well_founded.monotone_chain_condition] /-- If `∀ I > J, P I` implies `P J`, then `P` holds for all submodules. -/ lemma is_noetherian.induction [is_noetherian R M] {P : submodule R M → Prop} (hgt : ∀ I, (∀ J > I, P J) → P I) (I : submodule R M) : P I := well_founded.recursion (well_founded_submodule_gt R M) I hgt /-- For any endomorphism of a Noetherian module, there is some nontrivial iterate with disjoint kernel and range. -/ theorem is_noetherian.exists_endomorphism_iterate_ker_inf_range_eq_bot [I : is_noetherian R M] (f : M →ₗ[R] M) : ∃ n : ℕ, n ≠ 0 ∧ (f ^ n).ker ⊓ (f ^ n).range = ⊥ := begin obtain ⟨n, w⟩ := monotone_stabilizes_iff_noetherian.mpr I (f.iterate_ker.comp ⟨λ n, n+1, λ n m w, by linarith⟩), specialize w (2 * n + 1) (by linarith), dsimp at w, refine ⟨n+1, nat.succ_ne_zero _, _⟩, rw eq_bot_iff, rintros - ⟨h, ⟨y, rfl⟩⟩, rw [mem_bot, ←linear_map.mem_ker, w], erw linear_map.mem_ker at h ⊢, change ((f ^ (n + 1)) * (f ^ (n + 1))) y = 0 at h, rw ←pow_add at h, convert h using 3, linarith, end /-- Any surjective endomorphism of a Noetherian module is injective. -/ theorem is_noetherian.injective_of_surjective_endomorphism [is_noetherian R M] (f : M →ₗ[R] M) (s : surjective f) : injective f := begin obtain ⟨n, ne, w⟩ := is_noetherian.exists_endomorphism_iterate_ker_inf_range_eq_bot f, rw [linear_map.range_eq_top.mpr (linear_map.iterate_surjective s n), inf_top_eq, linear_map.ker_eq_bot] at w, exact linear_map.injective_of_iterate_injective ne w, end /-- Any surjective endomorphism of a Noetherian module is bijective. -/ theorem is_noetherian.bijective_of_surjective_endomorphism [is_noetherian R M] (f : M →ₗ[R] M) (s : surjective f) : bijective f := ⟨is_noetherian.injective_of_surjective_endomorphism f s, s⟩ /-- A sequence `f` of submodules of a noetherian module, with `f (n+1)` disjoint from the supremum of `f 0`, ..., `f n`, is eventually zero. -/ lemma is_noetherian.disjoint_partial_sups_eventually_bot [I : is_noetherian R M] (f : ℕ → submodule R M) (h : ∀ n, disjoint (partial_sups f n) (f (n+1))) : ∃ n : ℕ, ∀ m, n ≤ m → f m = ⊥ := begin -- A little off-by-one cleanup first: suffices t : ∃ n : ℕ, ∀ m, n ≤ m → f (m+1) = ⊥, { obtain ⟨n, w⟩ := t, use n+1, rintros (_|m) p, { cases p, }, { apply w, exact nat.succ_le_succ_iff.mp p }, }, obtain ⟨n, w⟩ := monotone_stabilizes_iff_noetherian.mpr I (partial_sups f), exact ⟨n, (λ m p, eq_bot_of_disjoint_absorbs (h m) ((eq.symm (w (m + 1) (le_add_right p))).trans (w m p)))⟩ end universe w variables {N : Type w} [add_comm_group N] [module R N] /-- If `M ⊕ N` embeds into `M`, for `M` noetherian over `R`, then `N` is trivial. -/ noncomputable def is_noetherian.equiv_punit_of_prod_injective [is_noetherian R M] (f : M × N →ₗ[R] M) (i : injective f) : N ≃ₗ[R] punit.{w+1} := begin apply nonempty.some, obtain ⟨n, w⟩ := is_noetherian.disjoint_partial_sups_eventually_bot (f.tailing i) (f.tailings_disjoint_tailing i), specialize w n (le_refl n), apply nonempty.intro, refine (f.tailing_linear_equiv i n).symm ≪≫ₗ _, rw w, exact submodule.bot_equiv_punit, end end /-- A ring is Noetherian if it is Noetherian as a module over itself, i.e. all its ideals are finitely generated. -/ class is_noetherian_ring (R) [ring R] extends is_noetherian R R : Prop theorem is_noetherian_ring_iff {R} [ring R] : is_noetherian_ring R ↔ is_noetherian R R := ⟨λ h, h.1, @is_noetherian_ring.mk _ _⟩ /-- A commutative ring is Noetherian if and only if all its ideals are finitely-generated. -/ lemma is_noetherian_ring_iff_ideal_fg (R : Type*) [comm_ring R] : is_noetherian_ring R ↔ ∀ I : ideal R, I.fg := is_noetherian_ring_iff.trans is_noetherian_def @[priority 80] -- see Note [lower instance priority] instance ring.is_noetherian_of_fintype (R M) [fintype M] [ring R] [add_comm_group M] [module R M] : is_noetherian R M := by letI := classical.dec; exact ⟨assume s, ⟨to_finset s, by rw [set.coe_to_finset, submodule.span_eq]⟩⟩ theorem ring.is_noetherian_of_zero_eq_one {R} [ring R] (h01 : (0 : R) = 1) : is_noetherian_ring R := by haveI := subsingleton_of_zero_eq_one h01; haveI := fintype.of_subsingleton (0:R); exact is_noetherian_ring_iff.2 (ring.is_noetherian_of_fintype R R) theorem is_noetherian_of_submodule_of_noetherian (R M) [ring R] [add_comm_group M] [module R M] (N : submodule R M) (h : is_noetherian R M) : is_noetherian R N := begin rw is_noetherian_iff_well_founded at h ⊢, exact order_embedding.well_founded (submodule.map_subtype.order_embedding N).dual h, end theorem is_noetherian_of_quotient_of_noetherian (R) [ring R] (M) [add_comm_group M] [module R M] (N : submodule R M) (h : is_noetherian R M) : is_noetherian R N.quotient := begin rw is_noetherian_iff_well_founded at h ⊢, exact order_embedding.well_founded (submodule.comap_mkq.order_embedding N).dual h, end /-- If `M / S / R` is a scalar tower, and `M / R` is Noetherian, then `M / S` is also noetherian. -/ theorem is_noetherian_of_tower (R) {S M} [comm_ring R] [ring S] [add_comm_group M] [algebra R S] [module S M] [module R M] [is_scalar_tower R S M] (h : is_noetherian R M) : is_noetherian S M := begin rw is_noetherian_iff_well_founded at h ⊢, refine (submodule.restrict_scalars_embedding R S M).dual.well_founded h end theorem is_noetherian_of_fg_of_noetherian {R M} [ring R] [add_comm_group M] [module R M] (N : submodule R M) [is_noetherian_ring R] (hN : N.fg) : is_noetherian R N := let ⟨s, hs⟩ := hN in begin haveI := classical.dec_eq M, haveI := classical.dec_eq R, letI : is_noetherian R R := by apply_instance, have : ∀ x ∈ s, x ∈ N, from λ x hx, hs ▸ submodule.subset_span hx, refine @@is_noetherian_of_surjective ((↑s : set M) → R) _ _ _ (pi.module _ _ _) _ _ _ is_noetherian_pi, { fapply linear_map.mk, { exact λ f, ⟨∑ i in s.attach, f i • i.1, N.sum_mem (λ c _, N.smul_mem _ $ this _ c.2)⟩ }, { intros f g, apply subtype.eq, change ∑ i in s.attach, (f i + g i) • _ = _, simp only [add_smul, finset.sum_add_distrib], refl }, { intros c f, apply subtype.eq, change ∑ i in s.attach, (c • f i) • _ = _, simp only [smul_eq_mul, mul_smul], exact finset.smul_sum.symm } }, rw linear_map.range_eq_top, rintro ⟨n, hn⟩, change n ∈ N at hn, rw [← hs, ← set.image_id ↑s, finsupp.mem_span_image_iff_total] at hn, rcases hn with ⟨l, hl1, hl2⟩, refine ⟨λ x, l x, subtype.ext _⟩, change ∑ i in s.attach, l i • (i : M) = n, rw [@finset.sum_attach M M s _ (λ i, l i • i), ← hl2, finsupp.total_apply, finsupp.sum, eq_comm], refine finset.sum_subset hl1 (λ x _ hx, _), rw [finsupp.not_mem_support_iff.1 hx, zero_smul] end lemma is_noetherian_of_fg_of_noetherian' {R M} [ring R] [add_comm_group M] [module R M] [is_noetherian_ring R] (h : (⊤ : submodule R M).fg) : is_noetherian R M := have is_noetherian R (⊤ : submodule R M), from is_noetherian_of_fg_of_noetherian _ h, by exactI is_noetherian_of_linear_equiv (linear_equiv.of_top (⊤ : submodule R M) rfl) /-- In a module over a noetherian ring, the submodule generated by finitely many vectors is noetherian. -/ theorem is_noetherian_span_of_finite (R) {M} [ring R] [add_comm_group M] [module R M] [is_noetherian_ring R] {A : set M} (hA : finite A) : is_noetherian R (submodule.span R A) := is_noetherian_of_fg_of_noetherian _ (submodule.fg_def.mpr ⟨A, hA, rfl⟩) theorem is_noetherian_ring_of_surjective (R) [comm_ring R] (S) [comm_ring S] (f : R →+* S) (hf : function.surjective f) [H : is_noetherian_ring R] : is_noetherian_ring S := begin rw [is_noetherian_ring_iff, is_noetherian_iff_well_founded] at H ⊢, exact order_embedding.well_founded (ideal.order_embedding_of_surjective f hf).dual H, end instance is_noetherian_ring_range {R} [comm_ring R] {S} [comm_ring S] (f : R →+* S) [is_noetherian_ring R] : is_noetherian_ring f.range := is_noetherian_ring_of_surjective R f.range f.range_restrict f.range_restrict_surjective theorem is_noetherian_ring_of_ring_equiv (R) [comm_ring R] {S} [comm_ring S] (f : R ≃+* S) [is_noetherian_ring R] : is_noetherian_ring S := is_noetherian_ring_of_surjective R S f.to_ring_hom f.to_equiv.surjective namespace submodule variables {R : Type*} {A : Type*} [comm_ring R] [ring A] [algebra R A] variables (M N : submodule R A) theorem fg_mul (hm : M.fg) (hn : N.fg) : (M * N).fg := let ⟨m, hfm, hm⟩ := fg_def.1 hm, ⟨n, hfn, hn⟩ := fg_def.1 hn in fg_def.2 ⟨m * n, hfm.mul hfn, span_mul_span R m n ▸ hm ▸ hn ▸ rfl⟩ lemma fg_pow (h : M.fg) (n : ℕ) : (M ^ n).fg := nat.rec_on n (⟨{1}, by simp [one_eq_span]⟩) (λ n ih, by simpa [pow_succ] using fg_mul _ _ h ih) end submodule section primes variables {R : Type*} [comm_ring R] [is_noetherian_ring R] /--In a noetherian ring, every ideal contains a product of prime ideals ([samuel, § 3.3, Lemma 3])-/ lemma exists_prime_spectrum_prod_le (I : ideal R) : ∃ (Z : multiset (prime_spectrum R)), multiset.prod (Z.map (coe : subtype _ → ideal R)) ≤ I := begin refine is_noetherian.induction (λ (M : ideal R) hgt, _) I, by_cases h_prM : M.is_prime, { use {⟨M, h_prM⟩}, rw [multiset.map_singleton, multiset.prod_singleton, subtype.coe_mk], exact le_rfl }, by_cases htop : M = ⊤, { rw htop, exact ⟨0, le_top⟩ }, have lt_add : ∀ z ∉ M, M < M + span R {z}, { intros z hz, refine lt_of_le_of_ne le_sup_left (λ m_eq, hz _), rw m_eq, exact mem_sup_right (mem_span_singleton_self z) }, obtain ⟨x, hx, y, hy, hxy⟩ := (ideal.not_is_prime_iff.mp h_prM).resolve_left htop, obtain ⟨Wx, h_Wx⟩ := hgt (M + span R {x}) (lt_add _ hx), obtain ⟨Wy, h_Wy⟩ := hgt (M + span R {y}) (lt_add _ hy), use Wx + Wy, rw [multiset.map_add, multiset.prod_add], apply le_trans (submodule.mul_le_mul h_Wx h_Wy), rw add_mul, apply sup_le (show M * (M + span R {y}) ≤ M, from ideal.mul_le_right), rw mul_add, apply sup_le (show span R {x} * M ≤ M, from ideal.mul_le_left), rwa [span_mul_span, singleton_mul_singleton, span_singleton_le_iff_mem], end variables {A : Type*} [integral_domain A] [is_noetherian_ring A] /--In a noetherian integral domain which is not a field, every non-zero ideal contains a non-zero product of prime ideals; in a field, the whole ring is a non-zero ideal containing only 0 as product or prime ideals ([samuel, § 3.3, Lemma 3]) -/ lemma exists_prime_spectrum_prod_le_and_ne_bot_of_domain (h_fA : ¬ is_field A) {I : ideal A} (h_nzI: I ≠ ⊥) : ∃ (Z : multiset (prime_spectrum A)), multiset.prod (Z.map (coe : subtype _ → ideal A)) ≤ I ∧ multiset.prod (Z.map (coe : subtype _ → ideal A)) ≠ ⊥ := begin revert h_nzI, refine is_noetherian.induction (λ (M : ideal A) hgt, _) I, intro h_nzM, have hA_nont : nontrivial A, apply is_integral_domain.to_nontrivial (integral_domain.to_is_integral_domain A), by_cases h_topM : M = ⊤, { rcases h_topM with rfl, obtain ⟨p_id, h_nzp, h_pp⟩ : ∃ (p : ideal A), p ≠ ⊥ ∧ p.is_prime, { apply ring.not_is_field_iff_exists_prime.mp h_fA }, use [({⟨p_id, h_pp⟩} : multiset (prime_spectrum A)), le_top], rwa [multiset.map_singleton, multiset.prod_singleton, subtype.coe_mk] }, by_cases h_prM : M.is_prime, { use ({⟨M, h_prM⟩} : multiset (prime_spectrum A)), rw [multiset.map_singleton, multiset.prod_singleton, subtype.coe_mk], exact ⟨le_rfl, h_nzM⟩ }, obtain ⟨x, hx, y, hy, h_xy⟩ := (ideal.not_is_prime_iff.mp h_prM).resolve_left h_topM, have lt_add : ∀ z ∉ M, M < M + span A {z}, { intros z hz, refine lt_of_le_of_ne le_sup_left (λ m_eq, hz _), rw m_eq, exact mem_sup_right (mem_span_singleton_self z) }, obtain ⟨Wx, h_Wx_le, h_Wx_ne⟩ := hgt (M + span A {x}) (lt_add _ hx) (ne_bot_of_gt (lt_add _ hx)), obtain ⟨Wy, h_Wy_le, h_Wx_ne⟩ := hgt (M + span A {y}) (lt_add _ hy) (ne_bot_of_gt (lt_add _ hy)), use Wx + Wy, rw [multiset.map_add, multiset.prod_add], refine ⟨le_trans (submodule.mul_le_mul h_Wx_le h_Wy_le) _, mt ideal.mul_eq_bot.mp _⟩, { rw add_mul, apply sup_le (show M * (M + span A {y}) ≤ M, from ideal.mul_le_right), rw mul_add, apply sup_le (show span A {x} * M ≤ M, from ideal.mul_le_left), rwa [span_mul_span, singleton_mul_singleton, span_singleton_le_iff_mem] }, { rintro (hx | hy); contradiction }, end end primes
4286a68bfbd108fc8d9928da2e27fffff319c9f9
626e312b5c1cb2d88fca108f5933076012633192
/src/analysis/convex/cone.lean
f4f3e0a8d8d26476d4f87290206f8bff9e6fa5f1
[ "Apache-2.0" ]
permissive
Bioye97/mathlib
9db2f9ee54418d29dd06996279ba9dc874fd6beb
782a20a27ee83b523f801ff34efb1a9557085019
refs/heads/master
1,690,305,956,488
1,631,067,774,000
1,631,067,774,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
23,976
lean
/- Copyright (c) 2020 Yury Kudryashov All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov, Frédéric Dupuis -/ import analysis.convex.basic import analysis.normed_space.inner_product /-! # Convex cones In a vector space `E` over `ℝ`, we define a convex cone as a subset `s` such that `a • x + b • y ∈ s` whenever `x, y ∈ s` and `a, b > 0`. We prove that convex cones form a `complete_lattice`, and define their images (`convex_cone.map`) and preimages (`convex_cone.comap`) under linear maps. We define pointed, blunt, flat and salient cones, and prove the correspondence between convex cones and ordered modules. We also define `convex.to_cone` to be the minimal cone that includes a given convex set. We define `set.inner_dual_cone` to be the cone consisting of all points `y` such that for all points `x` in a given set `0 ≤ ⟪ x, y ⟫`. ## Main statements We prove two extension theorems: * `riesz_extension`: [M. Riesz extension theorem](https://en.wikipedia.org/wiki/M._Riesz_extension_theorem) says that if `s` is a convex cone in a real vector space `E`, `p` is a submodule of `E` such that `p + s = E`, and `f` is a linear function `p → ℝ` which is nonnegative on `p ∩ s`, then there exists a globally defined linear function `g : E → ℝ` that agrees with `f` on `p`, and is nonnegative on `s`. * `exists_extension_of_le_sublinear`: Hahn-Banach theorem: if `N : E → ℝ` is a sublinear map, `f` is a linear map defined on a subspace of `E`, and `f x ≤ N x` for all `x` in the domain of `f`, then `f` can be extended to the whole space to a linear map `g` such that `g x ≤ N x` for all `x` ## Implementation notes While `convex` is a predicate on sets, `convex_cone` is a bundled convex cone. ## References * https://en.wikipedia.org/wiki/Convex_cone -/ universes u v open set linear_map open_locale classical pointwise variables (E : Type*) [add_comm_group E] [module ℝ E] {F : Type*} [add_comm_group F] [module ℝ F] {G : Type*} [add_comm_group G] [module ℝ G] /-! ### Definition of `convex_cone` and basic properties -/ /-- A convex cone is a subset `s` of a vector space over `ℝ` such that `a • x + b • y ∈ s` whenever `a, b > 0` and `x, y ∈ s`. -/ structure convex_cone := (carrier : set E) (smul_mem' : ∀ ⦃c : ℝ⦄, 0 < c → ∀ ⦃x : E⦄, x ∈ carrier → c • x ∈ carrier) (add_mem' : ∀ ⦃x⦄ (hx : x ∈ carrier) ⦃y⦄ (hy : y ∈ carrier), x + y ∈ carrier) variable {E} namespace convex_cone variables (S T : convex_cone E) instance : has_coe (convex_cone E) (set E) := ⟨convex_cone.carrier⟩ instance : has_mem E (convex_cone E) := ⟨λ m S, m ∈ S.carrier⟩ instance : has_le (convex_cone E) := ⟨λ S T, S.carrier ⊆ T.carrier⟩ instance : has_lt (convex_cone E) := ⟨λ S T, S.carrier ⊂ T.carrier⟩ @[simp, norm_cast] lemma mem_coe {x : E} : x ∈ (S : set E) ↔ x ∈ S := iff.rfl @[simp] lemma mem_mk {s : set E} {h₁ h₂ x} : x ∈ mk s h₁ h₂ ↔ x ∈ s := iff.rfl /-- Two `convex_cone`s are equal if the underlying subsets are equal. -/ theorem ext' {S T : convex_cone E} (h : (S : set E) = T) : S = T := by cases S; cases T; congr' /-- Two `convex_cone`s are equal if and only if the underlying subsets are equal. -/ protected theorem ext'_iff {S T : convex_cone E} : (S : set E) = T ↔ S = T := ⟨ext', λ h, h ▸ rfl⟩ /-- Two `convex_cone`s are equal if they have the same elements. -/ @[ext] theorem ext {S T : convex_cone E} (h : ∀ x, x ∈ S ↔ x ∈ T) : S = T := ext' $ set.ext h lemma smul_mem {c : ℝ} {x : E} (hc : 0 < c) (hx : x ∈ S) : c • x ∈ S := S.smul_mem' hc hx lemma add_mem ⦃x⦄ (hx : x ∈ S) ⦃y⦄ (hy : y ∈ S) : x + y ∈ S := S.add_mem' hx hy lemma smul_mem_iff {c : ℝ} (hc : 0 < c) {x : E} : c • x ∈ S ↔ x ∈ S := ⟨λ h, by simpa only [smul_smul, inv_mul_cancel (ne_of_gt hc), one_smul] using S.smul_mem (inv_pos.2 hc) h, λ h, S.smul_mem hc h⟩ lemma convex : convex (S : set E) := convex_iff_forall_pos.2 $ λ x y hx hy a b ha hb hab, S.add_mem (S.smul_mem ha hx) (S.smul_mem hb hy) instance : has_inf (convex_cone E) := ⟨λ S T, ⟨S ∩ T, λ c hc x hx, ⟨S.smul_mem hc hx.1, T.smul_mem hc hx.2⟩, λ x hx y hy, ⟨S.add_mem hx.1 hy.1, T.add_mem hx.2 hy.2⟩⟩⟩ lemma coe_inf : ((S ⊓ T : convex_cone E) : set E) = ↑S ∩ ↑T := rfl lemma mem_inf {x} : x ∈ S ⊓ T ↔ x ∈ S ∧ x ∈ T := iff.rfl instance : has_Inf (convex_cone E) := ⟨λ S, ⟨⋂ s ∈ S, ↑s, λ c hc x hx, mem_bInter $ λ s hs, s.smul_mem hc $ by apply mem_bInter_iff.1 hx s hs, λ x hx y hy, mem_bInter $ λ s hs, s.add_mem (by apply mem_bInter_iff.1 hx s hs) (by apply mem_bInter_iff.1 hy s hs)⟩⟩ lemma mem_Inf {x : E} {S : set (convex_cone E)} : x ∈ Inf S ↔ ∀ s ∈ S, x ∈ s := mem_bInter_iff instance : has_bot (convex_cone E) := ⟨⟨∅, λ c hc x, false.elim, λ x, false.elim⟩⟩ lemma mem_bot (x : E) : x ∈ (⊥ : convex_cone E) = false := rfl instance : has_top (convex_cone E) := ⟨⟨univ, λ c hc x hx, mem_univ _, λ x hx y hy, mem_univ _⟩⟩ lemma mem_top (x : E) : x ∈ (⊤ : convex_cone E) := mem_univ x instance : complete_lattice (convex_cone E) := { le := (≤), lt := (<), bot := (⊥), bot_le := λ S x, false.elim, top := (⊤), le_top := λ S x hx, mem_top x, inf := (⊓), Inf := has_Inf.Inf, sup := λ a b, Inf {x | a ≤ x ∧ b ≤ x}, Sup := λ s, Inf {T | ∀ S ∈ s, S ≤ T}, le_sup_left := λ a b, λ x hx, mem_Inf.2 $ λ s hs, hs.1 hx, le_sup_right := λ a b, λ x hx, mem_Inf.2 $ λ s hs, hs.2 hx, sup_le := λ a b c ha hb x hx, mem_Inf.1 hx c ⟨ha, hb⟩, le_inf := λ a b c ha hb x hx, ⟨ha hx, hb hx⟩, inf_le_left := λ a b x, and.left, inf_le_right := λ a b x, and.right, le_Sup := λ s p hs x hx, mem_Inf.2 $ λ t ht, ht p hs hx, Sup_le := λ s p hs x hx, mem_Inf.1 hx p hs, le_Inf := λ s a ha x hx, mem_Inf.2 $ λ t ht, ha t ht hx, Inf_le := λ s a ha x hx, mem_Inf.1 hx _ ha, .. partial_order.lift (coe : convex_cone E → set E) (λ a b, ext') } instance : inhabited (convex_cone E) := ⟨⊥⟩ /-- The image of a convex cone under an `ℝ`-linear map is a convex cone. -/ def map (f : E →ₗ[ℝ] F) (S : convex_cone E) : convex_cone F := { carrier := f '' S, smul_mem' := λ c hc y ⟨x, hx, hy⟩, hy ▸ f.map_smul c x ▸ mem_image_of_mem f (S.smul_mem hc hx), add_mem' := λ y₁ ⟨x₁, hx₁, hy₁⟩ y₂ ⟨x₂, hx₂, hy₂⟩, hy₁ ▸ hy₂ ▸ f.map_add x₁ x₂ ▸ mem_image_of_mem f (S.add_mem hx₁ hx₂) } lemma map_map (g : F →ₗ[ℝ] G) (f : E →ₗ[ℝ] F) (S : convex_cone E) : (S.map f).map g = S.map (g.comp f) := ext' $ image_image g f S @[simp] lemma map_id : S.map linear_map.id = S := ext' $ image_id _ /-- The preimage of a convex cone under an `ℝ`-linear map is a convex cone. -/ def comap (f : E →ₗ[ℝ] F) (S : convex_cone F) : convex_cone E := { carrier := f ⁻¹' S, smul_mem' := λ c hc x hx, by { rw [mem_preimage, f.map_smul c], exact S.smul_mem hc hx }, add_mem' := λ x hx y hy, by { rw [mem_preimage, f.map_add], exact S.add_mem hx hy } } @[simp] lemma comap_id : S.comap linear_map.id = S := ext' preimage_id lemma comap_comap (g : F →ₗ[ℝ] G) (f : E →ₗ[ℝ] F) (S : convex_cone G) : (S.comap g).comap f = S.comap (g.comp f) := ext' $ preimage_comp.symm @[simp] lemma mem_comap {f : E →ₗ[ℝ] F} {S : convex_cone F} {x : E} : x ∈ S.comap f ↔ f x ∈ S := iff.rfl /-- Constructs an ordered module given an `ordered_add_comm_group`, a cone, and a proof that the order relation is the one defined by the cone. -/ lemma to_ordered_smul {M : Type*} [ordered_add_comm_group M] [module ℝ M] (S : convex_cone M) (h : ∀ x y : M, x ≤ y ↔ y - x ∈ S) : ordered_smul ℝ M := ordered_smul.mk' begin intros x y z xy hz, rw [h (z • x) (z • y), ←smul_sub z y x], exact smul_mem S hz ((h x y).mp (le_of_lt xy)) end /-! ### Convex cones with extra properties -/ /-- A convex cone is pointed if it includes 0. -/ def pointed (S : convex_cone E) : Prop := (0 : E) ∈ S /-- A convex cone is blunt if it doesn't include 0. -/ def blunt (S : convex_cone E) : Prop := (0 : E) ∉ S /-- A convex cone is flat if it contains some nonzero vector `x` and its opposite `-x`. -/ def flat (S : convex_cone E) : Prop := ∃ x ∈ S, x ≠ (0 : E) ∧ -x ∈ S /-- A convex cone is salient if it doesn't include `x` and `-x` for any nonzero `x`. -/ def salient (S : convex_cone E) : Prop := ∀ x ∈ S, x ≠ (0 : E) → -x ∉ S lemma pointed_iff_not_blunt (S : convex_cone E) : pointed S ↔ ¬blunt S := ⟨λ h₁ h₂, h₂ h₁, λ h, not_not.mp h⟩ lemma salient_iff_not_flat (S : convex_cone E) : salient S ↔ ¬flat S := begin split, { rintros h₁ ⟨x, xs, H₁, H₂⟩, exact h₁ x xs H₁ H₂ }, { intro h, unfold flat at h, push_neg at h, exact h } end /-- A blunt cone (one not containing 0) is always salient. -/ lemma salient_of_blunt (S : convex_cone E) : blunt S → salient S := begin intro h₁, rw [salient_iff_not_flat], intro h₂, obtain ⟨x, xs, H₁, H₂⟩ := h₂, have hkey : (0 : E) ∈ S := by rw [(show 0 = x + (-x), by simp)]; exact add_mem S xs H₂, exact h₁ hkey, end /-- A pointed convex cone defines a preorder. -/ def to_preorder (S : convex_cone E) (h₁ : pointed S) : preorder E := { le := λ x y, y - x ∈ S, le_refl := λ x, by change x - x ∈ S; rw [sub_self x]; exact h₁, le_trans := λ x y z xy zy, by simp [(show z - x = z - y + (y - x), by abel), add_mem S zy xy] } /-- A pointed and salient cone defines a partial order. -/ def to_partial_order (S : convex_cone E) (h₁ : pointed S) (h₂ : salient S) : partial_order E := { le_antisymm := begin intros a b ab ba, by_contradiction h, have h' : b - a ≠ 0 := λ h'', h (eq_of_sub_eq_zero h'').symm, have H := h₂ (b-a) ab h', rw [neg_sub b a] at H, exact H ba, end, ..to_preorder S h₁ } /-- A pointed and salient cone defines an `ordered_add_comm_group`. -/ def to_ordered_add_comm_group (S : convex_cone E) (h₁ : pointed S) (h₂ : salient S) : ordered_add_comm_group E := { add_le_add_left := begin intros a b hab c, change c + b - (c + a) ∈ S, rw [add_sub_add_left_eq_sub], exact hab, end, ..to_partial_order S h₁ h₂, ..show add_comm_group E, by apply_instance } /-! ### Positive cone of an ordered module -/ section positive_cone variables (M : Type*) [ordered_add_comm_group M] [module ℝ M] [ordered_smul ℝ M] /-- The positive cone is the convex cone formed by the set of nonnegative elements in an ordered module. -/ def positive_cone : convex_cone M := { carrier := {x | 0 ≤ x}, smul_mem' := begin intros c hc x hx, have := smul_le_smul_of_nonneg (show 0 ≤ x, by exact hx) (le_of_lt hc), have h' : c • (0 : M) = 0, { simp only [smul_zero] }, rwa [h'] at this end, add_mem' := λ x hx y hy, add_nonneg (show 0 ≤ x, by exact hx) (show 0 ≤ y, by exact hy) } /-- The positive cone of an ordered module is always salient. -/ lemma salient_of_positive_cone : salient (positive_cone M) := begin intros x xs hx hx', have := calc 0 < x : lt_of_le_of_ne xs hx.symm ... ≤ x + (-x) : (le_add_iff_nonneg_right x).mpr hx' ... = 0 : by rw [tactic.ring.add_neg_eq_sub x x]; exact sub_self x, exact lt_irrefl 0 this, end /-- The positive cone of an ordered module is always pointed. -/ lemma pointed_of_positive_cone : pointed (positive_cone M) := le_refl 0 end positive_cone end convex_cone /-! ### Cone over a convex set -/ namespace convex /-- The set of vectors proportional to those in a convex set forms a convex cone. -/ def to_cone (s : set E) (hs : convex s) : convex_cone E := begin apply convex_cone.mk (⋃ c > 0, (c : ℝ) • s); simp only [mem_Union, mem_smul_set], { rintros c c_pos _ ⟨c', c'_pos, x, hx, rfl⟩, exact ⟨c * c', mul_pos c_pos c'_pos, x, hx, (smul_smul _ _ _).symm⟩ }, { rintros _ ⟨cx, cx_pos, x, hx, rfl⟩ _ ⟨cy, cy_pos, y, hy, rfl⟩, have : 0 < cx + cy, from add_pos cx_pos cy_pos, refine ⟨_, this, _, convex_iff_div.1 hs hx hy (le_of_lt cx_pos) (le_of_lt cy_pos) this, _⟩, simp only [smul_add, smul_smul, mul_div_assoc', mul_div_cancel_left _ (ne_of_gt this)] } end variables {s : set E} (hs : convex s) {x : E} lemma mem_to_cone : x ∈ hs.to_cone s ↔ ∃ (c > 0) (y ∈ s), (c : ℝ) • y = x := by simp only [to_cone, convex_cone.mem_mk, mem_Union, mem_smul_set, eq_comm, exists_prop] lemma mem_to_cone' : x ∈ hs.to_cone s ↔ ∃ c > 0, (c : ℝ) • x ∈ s := begin refine hs.mem_to_cone.trans ⟨_, _⟩, { rintros ⟨c, hc, y, hy, rfl⟩, exact ⟨c⁻¹, inv_pos.2 hc, by rwa [smul_smul, inv_mul_cancel (ne_of_gt hc), one_smul]⟩ }, { rintros ⟨c, hc, hcx⟩, exact ⟨c⁻¹, inv_pos.2 hc, _, hcx, by rw [smul_smul, inv_mul_cancel (ne_of_gt hc), one_smul]⟩ } end lemma subset_to_cone : s ⊆ hs.to_cone s := λ x hx, hs.mem_to_cone'.2 ⟨1, zero_lt_one, by rwa one_smul⟩ /-- `hs.to_cone s` is the least cone that includes `s`. -/ lemma to_cone_is_least : is_least { t : convex_cone E | s ⊆ t } (hs.to_cone s) := begin refine ⟨hs.subset_to_cone, λ t ht x hx, _⟩, rcases hs.mem_to_cone.1 hx with ⟨c, hc, y, hy, rfl⟩, exact t.smul_mem hc (ht hy) end lemma to_cone_eq_Inf : hs.to_cone s = Inf { t : convex_cone E | s ⊆ t } := hs.to_cone_is_least.is_glb.Inf_eq.symm end convex lemma convex_hull_to_cone_is_least (s : set E) : is_least {t : convex_cone E | s ⊆ t} ((convex_convex_hull s).to_cone _) := begin convert (convex_convex_hull s).to_cone_is_least, ext t, exact ⟨λ h, convex_hull_min h t.convex, λ h, subset.trans (subset_convex_hull s) h⟩ end lemma convex_hull_to_cone_eq_Inf (s : set E) : (convex_convex_hull s).to_cone _ = Inf {t : convex_cone E | s ⊆ t} := (convex_hull_to_cone_is_least s).is_glb.Inf_eq.symm /-! ### M. Riesz extension theorem Given a convex cone `s` in a vector space `E`, a submodule `p`, and a linear `f : p → ℝ`, assume that `f` is nonnegative on `p ∩ s` and `p + s = E`. Then there exists a globally defined linear function `g : E → ℝ` that agrees with `f` on `p`, and is nonnegative on `s`. We prove this theorem using Zorn's lemma. `riesz_extension.step` is the main part of the proof. It says that if the domain `p` of `f` is not the whole space, then `f` can be extended to a larger subspace `p ⊔ span ℝ {y}` without breaking the non-negativity condition. In `riesz_extension.exists_top` we use Zorn's lemma to prove that we can extend `f` to a linear map `g` on `⊤ : submodule E`. Mathematically this is the same as a linear map on `E` but in Lean `⊤ : submodule E` is isomorphic but is not equal to `E`. In `riesz_extension` we use this isomorphism to prove the theorem. -/ namespace riesz_extension open submodule variables (s : convex_cone E) (f : linear_pmap ℝ E ℝ) /-- Induction step in M. Riesz extension theorem. Given a convex cone `s` in a vector space `E`, a partially defined linear map `f : f.domain → ℝ`, assume that `f` is nonnegative on `f.domain ∩ p` and `p + s = E`. If `f` is not defined on the whole `E`, then we can extend it to a larger submodule without breaking the non-negativity condition. -/ lemma step (nonneg : ∀ x : f.domain, (x : E) ∈ s → 0 ≤ f x) (dense : ∀ y, ∃ x : f.domain, (x : E) + y ∈ s) (hdom : f.domain ≠ ⊤) : ∃ g, f < g ∧ ∀ x : g.domain, (x : E) ∈ s → 0 ≤ g x := begin rcases set_like.exists_of_lt (lt_top_iff_ne_top.2 hdom) with ⟨y, hy', hy⟩, clear hy', obtain ⟨c, le_c, c_le⟩ : ∃ c, (∀ x : f.domain, -(x:E) - y ∈ s → f x ≤ c) ∧ (∀ x : f.domain, (x:E) + y ∈ s → c ≤ f x), { set Sp := f '' {x : f.domain | (x:E) + y ∈ s}, set Sn := f '' {x : f.domain | -(x:E) - y ∈ s}, suffices : (upper_bounds Sn ∩ lower_bounds Sp).nonempty, by simpa only [set.nonempty, upper_bounds, lower_bounds, ball_image_iff] using this, refine exists_between_of_forall_le (nonempty.image f _) (nonempty.image f (dense y)) _, { rcases (dense (-y)) with ⟨x, hx⟩, rw [← neg_neg x, coe_neg, ← sub_eq_add_neg] at hx, exact ⟨_, hx⟩ }, rintros a ⟨xn, hxn, rfl⟩ b ⟨xp, hxp, rfl⟩, have := s.add_mem hxp hxn, rw [add_assoc, add_sub_cancel'_right, ← sub_eq_add_neg, ← coe_sub] at this, replace := nonneg _ this, rwa [f.map_sub, sub_nonneg] at this }, have hy' : y ≠ 0, from λ hy₀, hy (hy₀.symm ▸ zero_mem _), refine ⟨f.sup_span_singleton y (-c) hy, _, _⟩, { refine lt_iff_le_not_le.2 ⟨f.left_le_sup _ _, λ H, _⟩, replace H := linear_pmap.domain_mono.monotone H, rw [linear_pmap.domain_sup_span_singleton, sup_le_iff, span_le, singleton_subset_iff] at H, exact hy H.2 }, { rintros ⟨z, hz⟩ hzs, rcases mem_sup.1 hz with ⟨x, hx, y', hy', rfl⟩, rcases mem_span_singleton.1 hy' with ⟨r, rfl⟩, simp only [subtype.coe_mk] at hzs, erw [linear_pmap.sup_span_singleton_apply_mk _ _ _ _ _ hx, smul_neg, ← sub_eq_add_neg, sub_nonneg], rcases lt_trichotomy r 0 with hr|hr|hr, { have : -(r⁻¹ • x) - y ∈ s, by rwa [← s.smul_mem_iff (neg_pos.2 hr), smul_sub, smul_neg, neg_smul, neg_neg, smul_smul, mul_inv_cancel (ne_of_lt hr), one_smul, sub_eq_add_neg, neg_smul, neg_neg], replace := le_c (r⁻¹ • ⟨x, hx⟩) this, rwa [← mul_le_mul_left (neg_pos.2 hr), ← neg_mul_eq_neg_mul, ← neg_mul_eq_neg_mul, neg_le_neg_iff, f.map_smul, smul_eq_mul, ← mul_assoc, mul_inv_cancel (ne_of_lt hr), one_mul] at this }, { subst r, simp only [zero_smul, add_zero] at hzs ⊢, apply nonneg, exact hzs }, { have : r⁻¹ • x + y ∈ s, by rwa [← s.smul_mem_iff hr, smul_add, smul_smul, mul_inv_cancel (ne_of_gt hr), one_smul], replace := c_le (r⁻¹ • ⟨x, hx⟩) this, rwa [← mul_le_mul_left hr, f.map_smul, smul_eq_mul, ← mul_assoc, mul_inv_cancel (ne_of_gt hr), one_mul] at this } } end theorem exists_top (p : linear_pmap ℝ E ℝ) (hp_nonneg : ∀ x : p.domain, (x : E) ∈ s → 0 ≤ p x) (hp_dense : ∀ y, ∃ x : p.domain, (x : E) + y ∈ s) : ∃ q ≥ p, q.domain = ⊤ ∧ ∀ x : q.domain, (x : E) ∈ s → 0 ≤ q x := begin replace hp_nonneg : p ∈ { p | _ }, by { rw mem_set_of_eq, exact hp_nonneg }, obtain ⟨q, hqs, hpq, hq⟩ := zorn.zorn_nonempty_partial_order₀ _ _ _ hp_nonneg, { refine ⟨q, hpq, _, hqs⟩, contrapose! hq, rcases step s q hqs _ hq with ⟨r, hqr, hr⟩, { exact ⟨r, hr, le_of_lt hqr, ne_of_gt hqr⟩ }, { exact λ y, let ⟨x, hx⟩ := hp_dense y in ⟨of_le hpq.left x, hx⟩ } }, { intros c hcs c_chain y hy, clear hp_nonneg hp_dense p, have cne : c.nonempty := ⟨y, hy⟩, refine ⟨linear_pmap.Sup c c_chain.directed_on, _, λ _, linear_pmap.le_Sup c_chain.directed_on⟩, rintros ⟨x, hx⟩ hxs, have hdir : directed_on (≤) (linear_pmap.domain '' c), from directed_on_image.2 (c_chain.directed_on.mono linear_pmap.domain_mono.monotone), rcases (mem_Sup_of_directed (cne.image _) hdir).1 hx with ⟨_, ⟨f, hfc, rfl⟩, hfx⟩, have : f ≤ linear_pmap.Sup c c_chain.directed_on, from linear_pmap.le_Sup _ hfc, convert ← hcs hfc ⟨x, hfx⟩ hxs, apply this.2, refl } end end riesz_extension /-- M. **Riesz extension theorem**: given a convex cone `s` in a vector space `E`, a submodule `p`, and a linear `f : p → ℝ`, assume that `f` is nonnegative on `p ∩ s` and `p + s = E`. Then there exists a globally defined linear function `g : E → ℝ` that agrees with `f` on `p`, and is nonnegative on `s`. -/ theorem riesz_extension (s : convex_cone E) (f : linear_pmap ℝ E ℝ) (nonneg : ∀ x : f.domain, (x : E) ∈ s → 0 ≤ f x) (dense : ∀ y, ∃ x : f.domain, (x : E) + y ∈ s) : ∃ g : E →ₗ[ℝ] ℝ, (∀ x : f.domain, g x = f x) ∧ (∀ x ∈ s, 0 ≤ g x) := begin rcases riesz_extension.exists_top s f nonneg dense with ⟨⟨g_dom, g⟩, ⟨hpg, hfg⟩, htop, hgs⟩, clear hpg, refine ⟨g ∘ₗ ↑(linear_equiv.of_top _ htop).symm, _, _⟩; simp only [comp_apply, linear_equiv.coe_coe, linear_equiv.of_top_symm_apply], { exact λ x, (hfg (submodule.coe_mk _ _).symm).symm }, { exact λ x hx, hgs ⟨x, _⟩ hx } end /-- **Hahn-Banach theorem**: if `N : E → ℝ` is a sublinear map, `f` is a linear map defined on a subspace of `E`, and `f x ≤ N x` for all `x` in the domain of `f`, then `f` can be extended to the whole space to a linear map `g` such that `g x ≤ N x` for all `x`. -/ theorem exists_extension_of_le_sublinear (f : linear_pmap ℝ E ℝ) (N : E → ℝ) (N_hom : ∀ (c : ℝ), 0 < c → ∀ x, N (c • x) = c * N x) (N_add : ∀ x y, N (x + y) ≤ N x + N y) (hf : ∀ x : f.domain, f x ≤ N x) : ∃ g : E →ₗ[ℝ] ℝ, (∀ x : f.domain, g x = f x) ∧ (∀ x, g x ≤ N x) := begin let s : convex_cone (E × ℝ) := { carrier := {p : E × ℝ | N p.1 ≤ p.2 }, smul_mem' := λ c hc p hp, calc N (c • p.1) = c * N p.1 : N_hom c hc p.1 ... ≤ c * p.2 : mul_le_mul_of_nonneg_left hp (le_of_lt hc), add_mem' := λ x hx y hy, le_trans (N_add _ _) (add_le_add hx hy) }, obtain ⟨g, g_eq, g_nonneg⟩ := riesz_extension s ((-f).coprod (linear_map.id.to_pmap ⊤)) _ _; try { simp only [linear_pmap.coprod_apply, to_pmap_apply, id_apply, linear_pmap.neg_apply, ← sub_eq_neg_add, sub_nonneg, subtype.coe_mk] at * }, replace g_eq : ∀ (x : f.domain) (y : ℝ), g (x, y) = y - f x, { intros x y, simpa only [subtype.coe_mk, subtype.coe_eta] using g_eq ⟨(x, y), ⟨x.2, trivial⟩⟩ }, { refine ⟨-g.comp (inl ℝ E ℝ), _, _⟩; simp only [neg_apply, inl_apply, comp_apply], { intro x, simp [g_eq x 0] }, { intro x, have A : (x, N x) = (x, 0) + (0, N x), by simp, have B := g_nonneg ⟨x, N x⟩ (le_refl (N x)), rw [A, map_add, ← neg_le_iff_add_nonneg'] at B, have C := g_eq 0 (N x), simp only [submodule.coe_zero, f.map_zero, sub_zero] at C, rwa ← C } }, { exact λ x hx, le_trans (hf _) hx }, { rintros ⟨x, y⟩, refine ⟨⟨(0, N x - y), ⟨f.domain.zero_mem, trivial⟩⟩, _⟩, simp only [convex_cone.mem_mk, mem_set_of_eq, subtype.coe_mk, prod.fst_add, prod.snd_add, zero_add, sub_add_cancel] } end /-! ### The dual cone -/ section dual variables {H : Type*} [inner_product_space ℝ H] (s t : set H) open_locale real_inner_product_space /-- The dual cone is the cone consisting of all points `y` such that for all points `x` in a given set `0 ≤ ⟪ x, y ⟫`. -/ noncomputable def set.inner_dual_cone (s : set H) : convex_cone H := { carrier := { y | ∀ x ∈ s, 0 ≤ ⟪ x, y ⟫ }, smul_mem' := λ c hc y hy x hx, begin rw real_inner_smul_right, exact mul_nonneg (le_of_lt hc) (hy x hx) end, add_mem' := λ u hu v hv x hx, begin rw inner_add_right, exact add_nonneg (hu x hx) (hv x hx) end } lemma mem_inner_dual_cone (y : H) (s : set H) : y ∈ s.inner_dual_cone ↔ ∀ x ∈ s, 0 ≤ ⟪ x, y ⟫ := by refl @[simp] lemma inner_dual_cone_empty : (∅ : set H).inner_dual_cone = ⊤ := convex_cone.ext' (eq_univ_of_forall (λ x y hy, false.elim (set.not_mem_empty _ hy))) lemma inner_dual_cone_le_inner_dual_cone (h : t ⊆ s) : s.inner_dual_cone ≤ t.inner_dual_cone := λ y hy x hx, hy x (h hx) lemma pointed_inner_dual_cone : s.inner_dual_cone.pointed := λ x hx, by rw inner_zero_right end dual
a77c60b5c5b2eddfc0125626cde1bbac2d2fe34e
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/tactic/zify.lean
3751a0def70399620a575ee1454cb4f3b05230f4
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
5,376
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 tactic.norm_cast import data.int.cast /-! # A tactic to shift `ℕ` goals to `ℤ` It is often easier to work in `ℤ`, where subtraction is well behaved, than in `ℕ` where it isn't. `zify` is a tactic that casts goals and hypotheses about natural numbers to ones about integers. It makes use of `push_cast`, part of the `norm_cast` family, to simplify these goals. ## Implementation notes `zify` is extensible, using the attribute `@[zify]` to label lemmas used for moving propositions from `ℕ` to `ℤ`. `zify` lemmas should have the form `∀ a₁ ... aₙ : ℕ, Pz (a₁ : ℤ) ... (aₙ : ℤ) ↔ Pn a₁ ... aₙ`. For example, `int.coe_nat_le_coe_nat_iff : ∀ (m n : ℕ), ↑m ≤ ↑n ↔ m ≤ n` is a `zify` lemma. `zify` is very nearly just `simp only with zify push_cast`. There are a few minor differences: * `zify` lemmas are used in the opposite order of the standard simp form. E.g. we will rewrite with `int.coe_nat_le_coe_nat_iff` from right to left. * `zify` should fail if no `zify` lemma applies (i.e. it was unable to shift any proposition to ℤ). However, once this succeeds, it does not necessarily need to rewrite with any `push_cast` rules. -/ open tactic namespace zify /-- The `zify` attribute is used by the `zify` tactic. It applies to lemmas that shift propositions between `nat` and `int`. `zify` lemmas should have the form `∀ a₁ ... aₙ : ℕ, Pz (a₁ : ℤ) ... (aₙ : ℤ) ↔ Pn a₁ ... aₙ`. For example, `int.coe_nat_le_coe_nat_iff : ∀ (m n : ℕ), ↑m ≤ ↑n ↔ m ≤ n` is a `zify` lemma. -/ @[user_attribute] meta def zify_attr : user_attribute simp_lemmas unit := { name := `zify, descr := "Used to tag lemmas for use in the `zify` tactic", cache_cfg := { mk_cache := λ ns, mmap (λ n, do c ← mk_const n, return (c, tt)) ns >>= simp_lemmas.mk.append_with_symm, dependencies := [] } } /-- Given an expression `e`, `lift_to_z e` looks for subterms of `e` that are propositions "about" natural numbers and change them to propositions about integers. Returns an expression `e'` and a proof that `e = e'`. Includes `ge_iff_le` and `gt_iff_lt` in the simp set. These can't be tagged with `zify` as we want to use them in the "forward", not "backward", direction. -/ meta def lift_to_z (e : expr) : tactic (expr × expr) := do sl ← zify_attr.get_cache, sl ← sl.add_simp `ge_iff_le, sl ← sl.add_simp `gt_iff_lt, (e', prf, _) ← simplify sl [] e, return (e', prf) attribute [zify] int.coe_nat_le_coe_nat_iff int.coe_nat_lt_coe_nat_iff int.coe_nat_eq_coe_nat_iff end zify @[zify] lemma int.coe_nat_ne_coe_nat_iff (a b : ℕ) : (a : ℤ) ≠ b ↔ a ≠ b := by simp /-- `zify extra_lems e` is used to shift propositions in `e` from `ℕ` to `ℤ`. This is often useful since `ℤ` has well-behaved subtraction. The list of extra lemmas is used in the `push_cast` step. Returns an expression `e'` and a proof that `e = e'`.-/ meta def tactic.zify (extra_lems : list simp_arg_type) : expr → tactic (expr × expr) := λ z, do (z1, p1) ← zify.lift_to_z z <|> fail "failed to find an applicable zify lemma", (z2, p2) ← norm_cast.derive_push_cast extra_lems z1, prod.mk z2 <$> mk_eq_trans p1 p2 /-- A variant of `tactic.zify` that takes `h`, a proof of a proposition about natural numbers, and returns a proof of the zified version of that propositon. -/ meta def tactic.zify_proof (extra_lems : list simp_arg_type) (h : expr) : tactic expr := do (_, pf) ← infer_type h >>= tactic.zify extra_lems, mk_eq_mp pf h section setup_tactic_parser /-- The `zify` tactic is used to shift propositions from `ℕ` to `ℤ`. This is often useful since `ℤ` has well-behaved subtraction. ```lean example (a b c x y z : ℕ) (h : ¬ x*y*z < 0) : c < a + 3*b := begin zify, zify at h, /- h : ¬↑x * ↑y * ↑z < 0 ⊢ ↑c < ↑a + 3 * ↑b -/ end ``` `zify` can be given extra lemmas to use in simplification. This is especially useful in the presence of nat subtraction: passing `≤` arguments will allow `push_cast` to do more work. ``` example (a b c : ℕ) (h : a - b < c) (hab : b ≤ a) : false := begin zify [hab] at h, /- h : ↑a - ↑b < ↑c -/ end ``` `zify` makes use of the `@[zify]` attribute to move propositions, and the `push_cast` tactic to simplify the `ℤ`-valued expressions. `zify` is in some sense dual to the `lift` tactic. `lift (z : ℤ) to ℕ` will change the type of an integer `z` (in the supertype) to `ℕ` (the subtype), given a proof that `z ≥ 0`; propositions concerning `z` will still be over `ℤ`. `zify` changes propositions about `ℕ` (the subtype) to propositions about `ℤ` (the supertype), without changing the type of any variable. -/ meta def tactic.interactive.zify (sl : parse simp_arg_list) (l : parse location) : tactic unit := do locs ← l.get_locals, replace_at (tactic.zify sl) locs l.include_goal >>= guardb end add_tactic_doc { name := "zify", category := doc_category.attr, decl_names := [`zify.zify_attr], tags := ["coercions", "transport"] } add_tactic_doc { name := "zify", category := doc_category.tactic, decl_names := [`tactic.interactive.zify], tags := ["coercions", "transport"] }
f5cfa05252b7af59a9dab221874eaad4a8c6cab5
3fc1c63665292f73d04b29573f40247b9c66497c
/src/export_json.lean
e7fb98682fac4000c6426d45f4afd64403bfd5d2
[]
no_license
glangmead/doc-gen
9f925d16643c0b28133c489ce184b1a0e0ad97d9
77a63f85ccdeaa1bebc7d5ee814f1c2fce93753b
refs/heads/master
1,609,058,960,060
1,580,327,132,000
1,580,327,132,000
237,839,678
0
0
null
1,580,678,968,000
1,580,678,968,000
null
UTF-8
Lean
false
false
11,183
lean
/- Copyright (c) 2019 Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Robert Y. Lewis -/ import tactic.core system.io data.string.defs tactic.interactive data.list.sort import all /-! Used to generate a json file for html docs. The json file is a list of maps, where each map has the structure { name: string, args : list string, type: string, doc_string: string, filename: string, line: int, attributes: list string, equations: list string, kind: string, structure_fields: list (list string), constructors: list (list string) } The lists in structure_fields and constructors are assumed to contain two strings each. Include this file somewhere in mathlib, e.g. in the `scripts` directory. Make sure mathlib is precompiled, with `all.lean` generated by `mk_all.sh`. Usage: `lean --run export_json.lean` creates `json_export.txt` in the current directory. -/ open tactic io io.fs native set_option pp.generalized_field_notation true /-- The information collected from each declaration -/ structure decl_info := (name : name) (is_meta : bool) (args : list (bool × string)) -- tt means implicit (type : string) (doc_string : option string) (filename : string) (line : ℕ) (attributes : list string) -- not all attributes, we have a hardcoded list to check (equations : list string) (kind : string) -- def, thm, cnst, ax (structure_fields : list (string × string)) -- name and type of fields of a constructor (constructors : list (string × string)) -- name and type of constructors of an inductive type structure module_doc_info := (filename : string) (line : ℕ) (content : string) meta def escape_quotes (s : string) : string := s.fold "" (λ s x, s ++ if x = '"' then '\\'.to_string ++ '"'.to_string else x.to_string) meta def print_arg : bool × string → string | (b, s) := let bstr := if b then "true" else "false" in "{" ++ (to_string $ format!"\"arg\":{repr s}, \"implicit\":{bstr}") ++ "}" meta def decl_info.to_format : decl_info → format | ⟨name, is_meta, args, type, doc_string, filename, line, attributes, equations, kind, structure_fields, constructors⟩ := let doc_string := doc_string.get_or_else "", is_meta := if is_meta then "true" else "false", args := args.map print_arg, attributes := attributes.map repr, equations := equations.map repr, structure_fields := structure_fields.map (λ ⟨n, t⟩, format!"[\"{to_string n}\", {repr t}]"), constructors := constructors.map (λ ⟨n, t⟩, format!"[\"{to_string n}\", {repr t}]") in "{" ++ format!"\"name\":\"{to_string name}\", \"is_meta\":{is_meta}, \"args\":{args}, \"type\":{repr type}, \"doc_string\":{repr doc_string}, " ++ format!"\"filename\":\"{filename}\",\"line\":{line}, \"attributes\":{attributes}, \"equations\":{equations}, " ++ format!" \"kind\":{repr kind}, \"structure_fields\":{structure_fields}, \"constructors\":{constructors}" ++ "}" section open tactic.interactive -- tt means implicit private meta def format_binders : list name × binder_info × expr → tactic (bool × format) | (ns, binder_info.default, t) := prod.mk ff <$> pformat!"({format_names ns} : {t})" | (ns, binder_info.implicit, t) := prod.mk tt <$> pformat!"{{{format_names ns} : {t}}" | (ns, binder_info.strict_implicit, t) := prod.mk tt <$> pformat!"⦃{format_names ns} : {t}⦄" | ([n], binder_info.inst_implicit, t) := prod.mk tt <$> if "_".is_prefix_of n.to_string then pformat!"[{t}]" else pformat!"[{format_names [n]} : {t}]" | (ns, binder_info.inst_implicit, t) := prod.mk tt <$> pformat!"[{format_names ns} : {t}]" | (ns, binder_info.aux_decl, t) := prod.mk tt <$> pformat!"({format_names ns} : {t})" meta def binder_info.is_inst_implicit : binder_info → bool | binder_info.inst_implicit := tt | _ := ff meta def count_named_intros : expr → tactic ℕ | e@(expr.pi _ bi _ _) := do ([_], b) ← mk_local_pisn e 1, v ← count_named_intros b, return $ if v = 0 ∧ e.is_arrow ∧ ¬ bi.is_inst_implicit then v else v + 1 | _ := return 0 /- meta def count_named_intros : expr → ℕ | e@(expr.pi _ _ _ b) := let v := count_named_intros b in if v = 0 ∧ e.is_arrow then v else v + 1 | _ := 0 -/ -- tt means implicit meta def get_args_and_type (e : expr) : tactic (list (bool × string) × string) := prod.fst <$> solve_aux e ( do count_named_intros e >>= intron, cxt ← local_context >>= tactic.interactive.compact_decl, cxt' ← cxt.mmap (λ t, do ft ← format_binders t, return (ft.1, to_string ft.2)), tgt ← target >>= pp, return (cxt', to_string tgt)) end /-- The attributes we check for -/ meta def attribute_list := [`simp, `squash_cast, `move_cast, `elim_cast, `nolint, `ext, `instance, `class] meta def attributes_of (n : name) : tactic (list string) := list.map to_string <$> attribute_list.mfilter (λ attr, succeeds $ has_attribute attr n) meta def declaration.kind : declaration → string | (declaration.defn a a_1 a_2 a_3 a_4 a_5) := "def" | (declaration.thm a a_1 a_2 a_3) := "thm" | (declaration.cnst a a_1 a_2 a_3) := "cnst" | (declaration.ax a a_1 a_2) := "ax" -- does this not exist already? I'm confused. meta def expr.instantiate_pis : list expr → expr → expr | (e'::es) (expr.pi n bi t e) := expr.instantiate_pis es (e.instantiate_var e') | _ e := e meta def enable_links : tactic unit := do o ← get_options, set_options $ o.set_bool `pp.links tt -- assumes proj_name exists meta def get_proj_type (struct_name proj_name : name) : tactic string := do (locs, _) ← mk_const struct_name >>= infer_type >>= mk_local_pis, proj_tp ← mk_const proj_name >>= infer_type, (_, t) ← mk_local_pisn (proj_tp.instantiate_pis locs) 1, to_string <$> pp t meta def mk_structure_fields (decl : name) (e : environment) : tactic (list (string × string)) := match e.is_structure decl, e.structure_fields_full decl with | tt, some proj_names := proj_names.mmap $ λ n, do tp ← get_proj_type decl n, return (to_string n, to_string tp) | _, _ := return [] end -- this is used as a hack in get_constructor_type to avoid printing `Type ?`. meta def mk_const_with_params (d : declaration) : expr := let lvls := d.univ_params.map level.param in expr.const d.to_name lvls meta def get_constructor_type (type_name constructor_name : name) : tactic string := do d ← get_decl type_name, (locs, _) ← infer_type (mk_const_with_params d) >>= mk_local_pis, proj_tp ← mk_const constructor_name >>= infer_type, do t ← pis locs (proj_tp.instantiate_pis locs), --.abstract_locals (locs.map expr.local_uniq_name), to_string <$> pp t meta def mk_constructors (decl : name) (e : environment): tactic (list (string × string)) := if (¬ e.is_inductive decl) ∨ (e.is_structure decl) then return [] else do d ← get_decl decl, ns ← get_constructors_for (mk_const_with_params d), ns.mmap $ λ n, do tp ← get_constructor_type decl n, return (to_string n, to_string tp) meta def get_equations (decl : name) : tactic (list string) := do ns ← get_eqn_lemmas_for tt decl, ns.mmap $ λ n, do d ← get_decl n, (_, ty) ← mk_local_pis d.type, to_string <$> pp ty /-- extracts `decl_info` from `d`. Should return `none` instead of failing. -/ meta def process_decl (d : declaration) : tactic (option decl_info) := do ff ← d.in_current_file | return none, e ← get_env, let decl_name := d.to_name, if decl_name.is_internal ∨ d.is_auto_generated e then return none else do some filename ← return (e.decl_olean decl_name) | return none, some ⟨line, _⟩ ← return (e.decl_pos decl_name) | return none, doc_string ← (some <$> doc_string decl_name) <|> return none, (args, type) ← get_args_and_type d.type, -- type ← escape_quotes <$> to_string <$> pp d.type, attributes ← attributes_of decl_name, equations ← get_equations decl_name, structure_fields ← mk_structure_fields decl_name e, constructors ← mk_constructors decl_name e, return $ some ⟨decl_name, !d.is_trusted, args, type, doc_string, filename, line, attributes, equations, d.kind, structure_fields, constructors⟩ meta def run_on_dcl_list (e : environment) (ens : list name) (handle : handle) (is_first : bool) : io unit := ens.mfoldl (λ is_first d_name, do d ← run_tactic (e.get d_name), odi ← run_tactic (enable_links >> process_decl d), match odi with | some di := do when (bnot is_first) (put_str_ln handle ","), put_str_ln handle $ to_string di.to_format, return ff | none := return is_first end) is_first >> return () meta def itersplit {α} : list α → ℕ → list (list α) | l 0 := [l] | l 1 := let (l1, l2) := l.split in [l1, l2] | l (k+2) := let (l1, l2) := l.split in itersplit l1 (k+1) ++ itersplit l2 (k+1) meta def write_module_doc_pair : pos × string → string | (⟨line, _⟩, doc) := "{\"line\":" ++ to_string line ++ ", \"doc\" :" ++ repr doc ++ "}" meta def write_olean_docs : tactic (list string) := do docs ← olean_doc_strings, return (docs.foldl (λ rest p, match p with | (none, _) := rest | (_, []) := rest | (some filename, l) := let new := "\"" ++ filename ++ "\":" ++ to_string (l.map write_module_doc_pair) in new::rest end) []) meta def get_instances : tactic (rb_lmap string string) := attribute.get_instances `instance >>= list.mfoldl (λ map inst_nm, do (_, e) ← mk_const inst_nm >>= infer_type >>= mk_local_pis, (expr.const class_nm _) ← return e.get_app_fn, return $ map.insert class_nm.to_string inst_nm.to_string) mk_rb_map meta def format_instance_list : tactic string := do map ← get_instances, let lst := map.to_list.map (λ ⟨n, l⟩, to_string format!"\"{n}\" : {repr l}"), return $ "{" ++ (string.join (lst.intersperse ",")) ++ "}" meta def format_notes : tactic string := do l ← get_library_notes, let l := l.map $ λ ⟨l, r⟩, to_string $ format!"[{repr l}, {repr r}]", let l := string.join $ l.intersperse ", ", return $ to_string $ format!"[{l}]" /-- Using `environment.mfold` is much cleaner. Unfortunately this led to a segfault, I think because of a stack overflow. Converting the environment to a list of declarations and folding over that led to "deep recursion detected". Instead, we split that list into 8 smaller lists and process them one by one. More investigation is needed. -/ meta def export_json (filename : string) : io unit := do handle ← mk_file_handle filename mode.write, put_str_ln handle "{ \"decls\":[", e ← run_tactic get_env, let ens := environment.get_decl_names e, let enss := itersplit ens 3, enss.mfoldl (λ is_first l, do run_on_dcl_list e l handle is_first, return ff) tt, put_str_ln handle "],", ods ← run_tactic write_olean_docs, put_str_ln handle $ "\"mod_docs\": {" ++ string.join (ods.intersperse ",\n") ++ "},", notes ← run_tactic format_notes, put_str_ln handle $ "\"notes\": " ++ notes ++ ",", instl ← run_tactic format_instance_list, put_str_ln handle $ "\"instances\": " ++ instl ++ "}", close handle meta def main : io unit := export_json "json_export.txt"
df34b9a77c82403f33544f60880228a7fccb4a97
54deab7025df5d2df4573383df7e1e5497b7a2c2
/topology/topological_space.lean
786b07e651e99fc9124efb520a2db675c9b9a436
[ "Apache-2.0" ]
permissive
HGldJ1966/mathlib
f8daac93a5b4ae805cfb0ecebac21a9ce9469009
c5c5b504b918a6c5e91e372ee29ed754b0513e85
refs/heads/master
1,611,340,395,683
1,503,040,489,000
1,503,040,489,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
38,064
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl Theory of topological spaces. Parts of the formalization is based on the books: N. Bourbaki: General Topology I. M. James: Topologies and Uniformities A major difference is that this formalization is heavily based on the filter library. -/ import order.filter open set filter lattice classical local attribute [instance] decidable_inhabited prop_decidable universes u v w structure topological_space (α : Type u) := (is_open : set α → Prop) (is_open_univ : is_open univ) (is_open_inter : ∀s t, is_open s → is_open t → is_open (s ∩ t)) (is_open_sUnion : ∀s, (∀t∈s, is_open t) → is_open (⋃₀ s)) attribute [class] topological_space section topological_space variables {α : Type u} {β : Type v} {ι : Sort w} {a a₁ a₂ : α} {s s₁ s₂ : set α} lemma topological_space_eq {f g : topological_space α} (h' : f.is_open = g.is_open) : f = g := begin cases f with a, cases g with b, have h : a = b, assumption, clear h', subst h end section variables [t : topological_space α] include t /- open -/ def is_open (s : set α) : Prop := topological_space.is_open t s @[simp] lemma is_open_univ : is_open (univ : set α) := topological_space.is_open_univ t lemma is_open_inter (h₁ : is_open s₁) (h₂ : is_open s₂) : is_open (s₁ ∩ s₂) := topological_space.is_open_inter t s₁ s₂ h₁ h₂ lemma is_open_sUnion {s : set (set α)} (h : ∀t ∈ s, is_open t) : is_open (⋃₀ s) := topological_space.is_open_sUnion t s h end variables [topological_space α] lemma is_open_union (h₁ : is_open s₁) (h₂ : is_open s₂) : is_open (s₁ ∪ s₂) := have (⋃₀ {s₁, s₂}) = (s₁ ∪ s₂), by simp, this ▸ is_open_sUnion $ show ∀(t : set α), t ∈ ({s₁, s₂} : set (set α)) → is_open t, by finish lemma is_open_Union {f : ι → set α} (h : ∀i, is_open (f i)) : is_open (⋃i, f i) := is_open_sUnion $ assume t ⟨i, (heq : t = f i)⟩, heq.symm ▸ h i @[simp] lemma is_open_empty : is_open (∅ : set α) := have is_open (⋃₀ ∅ : set α), from is_open_sUnion (assume a, false.elim), by simp at this; assumption /- is_closed -/ def is_closed (s : set α) : Prop := is_open (-s) @[simp] lemma is_closed_empty : is_closed (∅ : set α) := by simp [is_closed] @[simp] lemma is_closed_univ : is_closed (univ : set α) := by simp [is_closed] lemma is_closed_union : is_closed s₁ → is_closed s₂ → is_closed (s₁ ∪ s₂) := by simp [is_closed]; exact is_open_inter lemma is_closed_sInter {s : set (set α)} : (∀t ∈ s, is_closed t) → is_closed (⋂₀ s) := by simp [is_closed, compl_sInter]; exact assume h, is_open_Union $ assume t, is_open_Union $ assume ht, h t ht lemma is_closed_Inter {f : ι → set α} (h : ∀i, is_closed (f i)) : is_closed (⋂i, f i ) := is_closed_sInter $ assume t ⟨i, (heq : t = f i)⟩, heq.symm ▸ h i @[simp] lemma is_open_compl_iff {s : set α} : is_open (-s) ↔ is_closed s := iff.rfl @[simp] lemma is_closed_compl_iff {s : set α} : is_closed (-s) ↔ is_open s := by rw [←is_open_compl_iff, compl_compl] lemma is_open_diff {s t : set α} (h₁ : is_open s) (h₂ : is_closed t) : is_open (s - t) := is_open_inter h₁ $ is_open_compl_iff.mpr h₂ lemma is_closed_inter (h₁ : is_closed s₁) (h₂ : is_closed s₂) : is_closed (s₁ ∩ s₂) := by rw [is_closed, compl_inter]; exact is_open_union h₁ h₂ lemma is_closed_Union {s : set β} {f : β → set α} (hs : finite s) : (∀i∈s, is_closed (f i)) → is_closed (⋃i∈s, f i) := begin induction hs, simp, simp, exact assume h, is_closed_union (h _ $ or.inl rfl) (by finish) end /- interior -/ def interior (s : set α) : set α := ⋃₀ {t | is_open t ∧ t ⊆ s} @[simp] lemma is_open_interior {s : set α} : is_open (interior s) := is_open_sUnion $ assume t ⟨h₁, h₂⟩, h₁ lemma interior_subset {s : set α} : interior s ⊆ s := sUnion_subset $ assume t ⟨h₁, h₂⟩, h₂ lemma interior_maximal {s t : set α} (h₁ : t ⊆ s) (h₂ : is_open t) : t ⊆ interior s := subset_sUnion_of_mem ⟨h₂, h₁⟩ lemma interior_eq_of_open {s : set α} (h : is_open s) : interior s = s := subset.antisymm interior_subset (interior_maximal (subset.refl s) h) lemma interior_eq_iff_open {s : set α} : interior s = s ↔ is_open s := ⟨assume h, h ▸ is_open_interior, interior_eq_of_open⟩ lemma subset_interior_iff_subset_of_open {s t : set α} (h₁ : is_open s) : s ⊆ interior t ↔ s ⊆ t := ⟨assume h, subset.trans h interior_subset, assume h₂, interior_maximal h₂ h₁⟩ lemma interior_mono {s t : set α} (h : s ⊆ t) : interior s ⊆ interior t := interior_maximal (subset.trans interior_subset h) is_open_interior @[simp] lemma interior_empty : interior (∅ : set α) = ∅ := interior_eq_of_open is_open_empty @[simp] lemma interior_univ : interior (univ : set α) = univ := interior_eq_of_open is_open_univ @[simp] lemma interior_interior {s : set α} : interior (interior s) = interior s := interior_eq_of_open is_open_interior @[simp] lemma interior_inter {s t : set α} : interior (s ∩ t) = interior s ∩ interior t := subset.antisymm (subset_inter (interior_mono $ inter_subset_left s t) (interior_mono $ inter_subset_right s t)) (interior_maximal (inter_subset_inter interior_subset interior_subset) $ by simp [is_open_inter]) lemma interior_union_is_closed_of_interior_empty {s t : set α} (h₁ : is_closed s) (h₂ : interior t = ∅) : interior (s ∪ t) = interior s := have interior (s ∪ t) ⊆ s, from assume x ⟨u, ⟨(hu₁ : is_open u), (hu₂ : u ⊆ s ∪ t)⟩, (hx₁ : x ∈ u)⟩, classical.by_contradiction $ assume hx₂ : x ∉ s, have u - s ⊆ t, from assume x ⟨h₁, h₂⟩, or.resolve_left (hu₂ h₁) h₂, have u - s ⊆ interior t, by simp [subset_interior_iff_subset_of_open, this, is_open_diff hu₁ h₁], have u - s ⊆ ∅, by rw [h₂] at this; assumption, this ⟨hx₁, hx₂⟩, subset.antisymm (interior_maximal this is_open_interior) (interior_mono $ subset_union_left _ _) /- closure -/ def closure (s : set α) : set α := ⋂₀ {t | is_closed t ∧ s ⊆ t} @[simp] lemma is_closed_closure {s : set α} : is_closed (closure s) := is_closed_sInter $ assume t ⟨h₁, h₂⟩, h₁ lemma subset_closure {s : set α} : s ⊆ closure s := subset_sInter $ assume t ⟨h₁, h₂⟩, h₂ lemma closure_minimal {s t : set α} (h₁ : s ⊆ t) (h₂ : is_closed t) : closure s ⊆ t := sInter_subset_of_mem ⟨h₂, h₁⟩ lemma closure_eq_of_is_closed {s : set α} (h : is_closed s) : closure s = s := subset.antisymm (closure_minimal (subset.refl s) h) subset_closure lemma closure_eq_iff_is_closed {s : set α} : closure s = s ↔ is_closed s := ⟨assume h, h ▸ is_closed_closure, closure_eq_of_is_closed⟩ lemma closure_subset_iff_subset_of_is_closed {s t : set α} (h₁ : is_closed t) : closure s ⊆ t ↔ s ⊆ t := ⟨subset.trans subset_closure, assume h, closure_minimal h h₁⟩ lemma closure_mono {s t : set α} (h : s ⊆ t) : closure s ⊆ closure t := closure_minimal (subset.trans h subset_closure) is_closed_closure @[simp] lemma closure_empty : closure (∅ : set α) = ∅ := closure_eq_of_is_closed is_closed_empty @[simp] lemma closure_univ : closure (univ : set α) = univ := closure_eq_of_is_closed is_closed_univ @[simp] lemma closure_closure {s : set α} : closure (closure s) = closure s := closure_eq_of_is_closed is_closed_closure @[simp] lemma closure_union {s t : set α} : closure (s ∪ t) = closure s ∪ closure t := subset.antisymm (closure_minimal (union_subset_union subset_closure subset_closure) $ by simp [is_closed_union]) (union_subset (closure_mono $ subset_union_left _ _) (closure_mono $ subset_union_right _ _)) lemma interior_subset_closure {s : set α} : interior s ⊆ closure s := subset.trans interior_subset subset_closure lemma closure_eq_compl_interior_compl {s : set α} : closure s = - interior (- s) := begin simp [interior, closure], rw [compl_sUnion, compl_image_set_of], simp [compl_subset_compl_iff_subset] end @[simp] lemma interior_compl_eq {s : set α} : interior (- s) = - closure s := by simp [closure_eq_compl_interior_compl] @[simp] lemma closure_compl_eq {s : set α} : closure (- s) = - interior s := by simp [closure_eq_compl_interior_compl] /- neighbourhood filter -/ def nhds (a : α) : filter α := (⨅ s ∈ {s : set α | a ∈ s ∧ is_open s}, principal s) lemma tendsto_nhds {m : β → α} {f : filter β} (h : ∀s, a ∈ s → is_open s → preimage m s ∈ f.sets) : tendsto m f (nhds a) := show map m f ≤ (⨅ s ∈ {s : set α | a ∈ s ∧ is_open s}, principal s), from le_infi $ assume s, le_infi $ assume ⟨ha, hs⟩, le_principal_iff.mpr $ h s ha hs lemma tendsto_const_nhds {a : α} {f : filter β} : tendsto (λb:β, a) f (nhds a) := tendsto_nhds $ assume s ha hs, univ_mem_sets' $ assume _, ha lemma nhds_sets {a : α} : (nhds a).sets = {s | ∃t⊆s, is_open t ∧ a ∈ t} := calc (nhds a).sets = (⋃s∈{s : set α| a ∈ s ∧ is_open s}, (principal s).sets) : infi_sets_eq' begin simp, exact assume x ⟨hx₁, hx₂⟩ y ⟨hy₁, hy₂⟩, ⟨_, ⟨is_open_inter hx₁ hy₁, ⟨hx₂, hy₂⟩⟩, ⟨inter_subset_left _ _, inter_subset_right _ _⟩⟩ end ⟨univ, by simp⟩ ... = {s | ∃t⊆s, is_open t ∧ a ∈ t} : le_antisymm (supr_le $ assume i, supr_le $ assume ⟨hi₁, hi₂⟩ t ht, ⟨i, ht, hi₂, hi₁⟩) (assume t ⟨i, hi₁, hi₂, hi₃⟩, begin simp; exact ⟨i, hi₂, hi₁, hi₃⟩ end) lemma map_nhds {a : α} {f : α → β} : map f (nhds a) = (⨅ s ∈ {s : set α | a ∈ s ∧ is_open s}, principal (image f s)) := calc map f (nhds a) = (⨅ s ∈ {s : set α | a ∈ s ∧ is_open s}, map f (principal s)) : map_binfi_eq begin simp, exact assume x ⟨hx₁, hx₂⟩ y ⟨hy₁, hy₂⟩, ⟨_, ⟨is_open_inter hx₁ hy₁, ⟨hx₂, hy₂⟩⟩, ⟨inter_subset_left _ _, inter_subset_right _ _⟩⟩ end ⟨univ, by simp⟩ ... = _ : by simp lemma mem_nhds_sets_iff {a : α} {s : set α} : s ∈ (nhds a).sets ↔ ∃t⊆s, is_open t ∧ a ∈ t := by simp [nhds_sets] lemma mem_of_nhds {a : α} {s : set α} : s ∈ (nhds a).sets → a ∈ s := begin simp [mem_nhds_sets_iff]; exact assume ⟨t, _, ht, hs⟩, ht hs end lemma mem_nhds_sets {a : α} {s : set α} (hs : is_open s) (ha : a ∈ s) : s ∈ (nhds a).sets := by simp [nhds_sets]; exact ⟨s, hs, subset.refl _, ha⟩ lemma return_le_nhds : return ≤ (nhds : α → filter α) := assume a, le_infi $ assume s, le_infi $ assume ⟨h₁, _⟩, principal_mono.mpr $ by simp [h₁] @[simp] lemma nhds_neq_bot {a : α} : nhds a ≠ ⊥ := assume : nhds a = ⊥, have return a = (⊥ : filter α), from lattice.bot_unique $ this ▸ return_le_nhds a, return_neq_bot this lemma interior_eq_nhds {s : set α} : interior s = {a | nhds a ≤ principal s} := set.ext $ by simp [interior, nhds_sets] lemma is_open_iff_nhds {s : set α} : is_open s ↔ (∀a∈s, nhds a ≤ principal s) := calc is_open s ↔ interior s = s : by rw [interior_eq_iff_open] ... ↔ s ⊆ interior s : ⟨assume h, by simp [*, subset.refl], subset.antisymm interior_subset⟩ ... ↔ (∀a∈s, nhds a ≤ principal s) : by rw [interior_eq_nhds]; refl lemma closure_eq_nhds {s : set α} : closure s = {a | nhds a ⊓ principal s ≠ ⊥} := calc closure s = - interior (- s) : closure_eq_compl_interior_compl ... = {a | ¬ nhds a ≤ principal (-s)} : by rw [interior_eq_nhds]; refl ... = {a | nhds a ⊓ principal s ≠ ⊥} : set.ext $ assume a, not_congr (inf_eq_bot_iff_le_compl (show principal s ⊔ principal (-s) = ⊤, by simp [principal_univ]) (by simp)).symm lemma is_closed_iff_nhds {s : set α} : is_closed s ↔ (∀a, nhds a ⊓ principal s ≠ ⊥ → a ∈ s) := calc is_closed s ↔ closure s = s : by rw [closure_eq_iff_is_closed] ... ↔ closure s ⊆ s : ⟨assume h, by simp [*, subset.refl], assume h, subset.antisymm h subset_closure⟩ ... ↔ (∀a, nhds a ⊓ principal s ≠ ⊥ → a ∈ s) : by rw [closure_eq_nhds]; refl lemma closure_inter_open {s t : set α} (h : is_open s) : s ∩ closure t ⊆ closure (s ∩ t) := assume a ⟨hs, ht⟩, have s ∈ (nhds a).sets, from mem_nhds_sets h hs, have nhds a ⊓ principal s = nhds a, from inf_of_le_left $ by simp [this], have nhds a ⊓ principal (s ∩ t) ≠ ⊥, from calc nhds a ⊓ principal (s ∩ t) = nhds a ⊓ (principal s ⊓ principal t) : by simp ... = nhds a ⊓ principal t : by rw [←inf_assoc, this] ... ≠ ⊥ : by rw [closure_eq_nhds] at ht; assumption, by rw [closure_eq_nhds]; assumption lemma closure_diff [topological_space α] {s t : set α} : closure s - closure t ⊆ closure (s - t) := calc closure s \ closure t = (- closure t) ∩ closure s : by simp [diff_eq] ... ⊆ closure (- closure t ∩ s) : closure_inter_open $ is_open_compl_iff.mpr $ is_closed_closure ... = closure (s \ closure t) : by simp [diff_eq] ... ⊆ closure (s \ t) : closure_mono $ diff_subset_diff (subset.refl s) subset_closure /- locally finite family [General Topology (Bourbaki, 1995)] -/ section locally_finite def locally_finite (f : β → set α) := ∀x:α, ∃t∈(nhds x).sets, finite {i | f i ∩ t ≠ ∅ } lemma locally_finite_of_finite {f : β → set α} (h : finite (univ : set β)) : locally_finite f := assume x, ⟨univ, univ_mem_sets, finite_subset h $ by simp⟩ lemma locally_finite_subset {f₁ f₂ : β → set α} (hf₂ : locally_finite f₂) (hf : ∀b, f₁ b ⊆ f₂ b) : locally_finite f₁ := assume a, let ⟨t, ht₁, ht₂⟩ := hf₂ a in ⟨t, ht₁, finite_subset ht₂ $ assume i hi, neq_bot_of_le_neq_bot hi $ inter_subset_inter (hf i) $ subset.refl _⟩ lemma is_closed_Union_of_locally_finite {f : β → set α} (h₁ : locally_finite f) (h₂ : ∀i, is_closed (f i)) : is_closed (⋃i, f i) := is_open_iff_nhds.mpr $ assume a, assume h : a ∉ (⋃i, f i), have ∀i, a ∈ -f i, from assume i hi, by simp at h; exact h ⟨i, hi⟩, have ∀i, - f i ∈ (nhds a).sets, by rw [nhds_sets]; exact assume i, ⟨- f i, subset.refl _, h₂ i, this i⟩, let ⟨t, h_sets, (h_fin : finite {i | f i ∩ t ≠ ∅ })⟩ := h₁ a in calc nhds a ≤ principal (t ∩ (⋂ i∈{i | f i ∩ t ≠ ∅ }, - f i)) : begin simp, apply @filter.inter_mem_sets _ (nhds a) _ _ h_sets, apply @filter.Inter_mem_sets _ _ (nhds a) _ _ h_fin, exact assume i h, this i end ... ≤ principal (- ⋃i, f i) : begin simp, intro x, simp [not_eq_empty_iff_exists], exact assume ⟨xt, ht⟩ i xfi, ht i ⟨x, xt, xfi⟩ xfi end end locally_finite section compact def compact (s : set α) := ∀f, f ≠ ⊥ → f ≤ principal s → ∃a∈s, f ⊓ nhds a ≠ ⊥ lemma compact_of_is_closed_subset {s t : set α} (hs : compact s) (ht : is_closed t) (h : t ⊆ s) : compact t := assume f hnf hsf, let ⟨a, hsa, (ha : f ⊓ nhds a ≠ ⊥)⟩ := hs f hnf (le_trans hsf $ by simp [h]) 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 hsf (le_refl _), ⟨a, this, ha⟩ 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 _⟩ (by simp), by contradiction lemma compact_iff_ultrafilter_le_nhds {s : set α} : compact s ↔ (∀f, 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, 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 [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_right_antimono $ sUnion_mono $ subset_union_left _ _, principal_mono.mpr $ diff_right_antimono $ sUnion_mono $ subset_union_right _ _⟩) (assume ⟨c', hc'₁, hc'₂⟩, show principal (s \ _) ≠ ⊥, by simp [diff_neq_empty]; exact h hc'₁ hc'₂), have f ≤ principal s, from infi_le_of_le ⟨∅, empty_subset _, finite.empty⟩ $ show principal (s \ ⋃₀∅) ≤ principal s, by simp; exact subset.refl s, 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 simp [ht₁], finite_insert finite.empty⟩ $ principal_mono.mpr $ show s - ⋃₀{t} ⊆ - t, begin simp; exact assume x ⟨_, hnt⟩, hnt end, have is_closed (- t), from is_open_compl_iff.mp $ by simp; 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 := classical.by_cases (assume : b = ∅, ⟨∅, by simp, by simp, this ▸ hc₂⟩) (assume : b ≠ ∅, let ⟨i, hi⟩ := exists_mem_of_ne_empty this in have hc'₁ : ∀i∈c '' b, is_open i, from assume i ⟨j, hj, h⟩, h ▸ hc₁ _ hj, have hc'₂ : s ⊆ ⋃₀ (c '' b), by simp; assumption, let ⟨d, hd₁, hd₂, hd₃⟩ := compact_elim_finite_subcover hs hc'₁ hc'₂ in have ∀x, x ∈ d → ∃i, i ∈ b ∧ c i = x, from assume x hx, hd₁ hx, have ∃f:(Πx:set α, x ∈ d → β), ∀x (hx : x ∈ d), c (f x hx) = x ∧ f x hx ∈ b, by simp [skolem] at this; exact this, let ⟨f', hf⟩ := this, f := λx:set α, (if h : x ∈ d then f' x h else i) in have f_eq : f = λx:set α, (if h : x ∈ d then f' x h else i), from rfl, have ∀(x : α) (i : set α), x ∈ i ∧ i ∈ d → (∃ (i : β), x ∈ c i ∧ i ∈ f '' d), from assume x i ⟨hxi, hid⟩, ⟨f i, by simp [f_eq, hid]; exact ((hf _ hid).left.symm ▸ hxi), mem_image_of_mem f hid⟩, ⟨f '' d, assume i ⟨j, hj, h⟩, h ▸ by simp [f_eq, hj]; exact (hf _ hj).right, finite_image hd₂, subset.trans hd₃ $ by simp [subset_def, exists_implies_distrib]; exact this⟩) 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 simp [not_exists_iff, not_and_iff_imp_not, not_not_iff] at this; simp [inf_comm]; exact this, 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₂).upwards_sets (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 [closure_eq_nhds] at hx; exact hx t₂ ht₂ this, have ∀x∈s, ∃t∈f.sets, x ∉ closure t, begin simp [not_exists_iff, not_and_iff_imp_not, classical.not_forall_iff, not_implies_iff] at this, simp, assumption end, let c := (λt, - closure t) '' f.sets in have ∃c'⊆c, finite c' ∧ s ⊆ ⋃₀ c', from h c (assume t ⟨s, hs, h⟩, h ▸ is_open_compl_iff.mpr is_closed_closure) $ assume x hx, let ⟨t, ht, hxt⟩ := this x hx in by simp; exact ⟨t, hxt, ht⟩, let ⟨c', hcc', hcf, hsc'⟩ := this in have ∀s:{s // s∈c'}, ∃t, t ∈ f.sets ∧ - closure t = s, from assume ⟨x, hx⟩, hcc' hx, let ⟨b, hb⟩ := skolem.mp this 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 (by simp at hfs; assumption) this, have ∅ ∈ f.sets, from f.upwards_sets this $ assume x ⟨hxs, hxi⟩, have ∃t, x ∈ t ∧ t ∈ c', by simp [subset_def] at hsc'; exact hsc' x hxs, let ⟨t, hxt, htc'⟩ := this in have -closure (b ⟨t, htc'⟩) = t, from (hb _).right, have x ∈ - t, from this ▸ (calc x ∈ b ⟨t, htc'⟩ : by simp at hxi; have h := hxi t htc'; rwa [dif_pos htc'] at h ... ⊆ closure (b ⟨t, htc'⟩) : subset_closure ... ⊆ - - closure (b ⟨t, htc'⟩) : by simp; 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, have f = ⊥, by simp [empty_in_sets_eq_bot] at hsf; assumption, false.elim $ hnf this lemma compact_singleton {a : α} : compact ({a} : set α) := compact_of_finite_subcover $ assume c hc₁ hc₂, have ∃i, a ∈ i ∧ i ∈ c, by simp at hc₂; assumption, let ⟨i, hai, hic⟩ := this in ⟨{i}, by simp [hic], finite_singleton, by simp [hai]⟩ end compact section separation class t1_space (α : Type u) [topological_space α] := (t1 : ∀x, is_closed ({x} : set α)) lemma is_closed_singleton [t1_space α] {x : α} : is_closed ({x} : set α) := t1_space.t1 _ x lemma compl_singleton_mem_nhds [t1_space α] {x y : α} (h : y ≠ x) : - {x} ∈ (nhds y).sets := mem_nhds_sets is_closed_singleton $ by simp; exact h @[simp] lemma closure_singleton [topological_space α] [t1_space α] {a : α} : closure ({a} : set α) = {a} := closure_eq_of_is_closed is_closed_singleton class t2_space (α : Type u) [topological_space α] := (t2 : ∀x y, x ≠ y → ∃u v : set α, is_open u ∧ is_open v ∧ x ∈ u ∧ y ∈ v ∧ u ∩ v = ∅) lemma t2_separation [t2_space α] {x y : α} (h : x ≠ y) : ∃u v : set α, is_open u ∧ is_open v ∧ x ∈ u ∧ y ∈ v ∧ u ∩ v = ∅ := t2_space.t2 _ _ _ h instance t2_space.t1_space [topological_space α] [t2_space α] : t1_space α := ⟨assume x, have ∀y, y ≠ x ↔ ∃ (i : set α), is_open i ∧ y ∈ i ∧ x ∉ i, from assume y, ⟨assume h', let ⟨u, v, hu, hv, hy, hx, h⟩ := t2_separation h' in have x ∉ u, from assume : x ∈ u, have x ∈ u ∩ v, from ⟨this, hx⟩, by rwa [h] at this, ⟨u, hu, hy, this⟩, assume ⟨s, hs, hy, hx⟩ h, hx $ h ▸ hy⟩, have (-{x} : set α) = (⋃s∈{s : set α | x ∉ s ∧ is_open s}, s), by apply set.ext; simp; exact this, show is_open (- {x}), by rw [this]; exact (is_open_Union $ assume s, is_open_Union $ assume ⟨_, hs⟩, hs)⟩ lemma eq_of_nhds_neq_bot [ht : t2_space α] {x y : α} (h : nhds x ⊓ nhds y ≠ ⊥) : x = y := classical.by_contradiction $ assume : x ≠ y, let ⟨u, v, hu, hv, hx, hy, huv⟩ := t2_space.t2 _ x y this in have u ∩ v ∈ (nhds x ⊓ nhds y).sets, from inter_mem_inf_sets (mem_nhds_sets hu hx) (mem_nhds_sets hv hy), h $ empty_in_sets_eq_bot.mp $ huv ▸ this @[simp] lemma nhds_eq_nhds_iff {a b : α} [t2_space α] : nhds a = nhds b ↔ a = b := ⟨assume h, eq_of_nhds_neq_bot $ by simp [h], assume h, h ▸ rfl⟩ @[simp] lemma nhds_le_nhds_iff {a b : α} [t2_space α] : nhds a ≤ nhds b ↔ a = b := ⟨assume h, eq_of_nhds_neq_bot $ by simp [inf_of_le_left h], assume h, h ▸ le_refl _⟩ lemma tendsto_nhds_unique [t2_space α] {f : β → α} {l : filter β} {a b : α} (hl : l ≠ ⊥) (ha : tendsto f l (nhds a)) (hb : tendsto f l (nhds b)) : a = b := eq_of_nhds_neq_bot $ neq_bot_of_le_neq_bot (map_ne_bot hl) $ le_inf ha hb end separation section regularity class regular_space (α : Type u) [topological_space α] extends t2_space α := (regular : ∀{s:set α} {a}, is_closed s → a ∉ s → ∃t, is_open t ∧ s ⊆ t ∧ nhds a ⊓ principal t = ⊥) lemma nhds_is_closed [regular_space α] {a : α} {s : set α} (h : s ∈ (nhds a).sets) : ∃t∈(nhds a).sets, t ⊆ s ∧ is_closed t := let ⟨s', h₁, h₂, h₃⟩ := mem_nhds_sets_iff.mp h in have ∃t, is_open t ∧ -s' ⊆ t ∧ nhds a ⊓ principal t = ⊥, from regular_space.regular (is_closed_compl_iff.mpr h₂) (not_not_intro h₃), let ⟨t, ht₁, ht₂, ht₃⟩ := this in ⟨-t, mem_sets_of_neq_bot $ by simp; exact ht₃, subset.trans (compl_subset_of_compl_subset ht₂) h₁, is_closed_compl_iff.mpr ht₁⟩ end regularity end topological_space namespace topological_space variables {α : Type u} inductive generate_open (g : set (set α)) : set α → Prop | basic : ∀s∈g, generate_open s | univ : generate_open univ | inter : ∀s t, generate_open s → generate_open t → generate_open (s ∩ t) | sUnion : ∀k, (∀s∈k, generate_open s) → generate_open (⋃₀ k) def generate_from (g : set (set α)) : topological_space α := { topological_space . is_open := generate_open g, is_open_univ := generate_open.univ g, is_open_inter := generate_open.inter, is_open_sUnion := generate_open.sUnion } lemma nhds_generate_from {g : set (set α)} {a : α} : @nhds α (generate_from g) a = (⨅s∈{s | a ∈ s ∧ s ∈ g}, principal s) := le_antisymm (infi_le_infi $ assume s, infi_le_infi_const $ assume ⟨as, sg⟩, ⟨as, generate_open.basic _ sg⟩) (le_infi $ assume s, le_infi $ assume ⟨as, hs⟩, have ∀s, generate_open g s → a ∈ s → (⨅s∈{s | a ∈ s ∧ s ∈ g}, principal s) ≤ principal s, begin intros s hs, induction hs, case generate_open.basic s hs { exact assume as, infi_le_of_le s $ infi_le _ ⟨as, hs⟩ }, case generate_open.univ { rw [principal_univ], exact assume _, le_top }, case generate_open.inter s t hs' ht' hs ht { exact assume ⟨has, hat⟩, calc _ ≤ principal s ⊓ principal t : le_inf (hs has) (ht hat) ... = _ : by simp }, case generate_open.sUnion k hk' hk { intro h, simp at h, revert h, exact assume ⟨t, hat, htk⟩, calc _ ≤ principal t : hk t htk hat ... ≤ _ : begin simp; exact subset_sUnion_of_mem htk end }, end, this s hs as) end topological_space section constructions variables {α : Type u} {β : Type v} instance : partial_order (topological_space α) := { le := λt s, t.is_open ≤ s.is_open, le_antisymm := assume t s h₁ h₂, topological_space_eq $ le_antisymm h₁ h₂, le_refl := assume t, le_refl t.is_open, le_trans := assume a b c h₁ h₂, @le_trans _ _ a.is_open b.is_open c.is_open h₁ h₂ } instance : has_Inf (topological_space α) := ⟨assume (tt : set (topological_space α)), { topological_space . is_open := λs, ∀t∈tt, topological_space.is_open t s, is_open_univ := assume t h, t.is_open_univ, is_open_inter := assume s₁ s₂ h₁ h₂ t ht, t.is_open_inter s₁ s₂ (h₁ t ht) (h₂ t ht), is_open_sUnion := assume s h t ht, t.is_open_sUnion _ $ assume s' hss', h _ hss' _ ht }⟩ private lemma Inf_le {tt : set (topological_space α)} {t : topological_space α} (h : t ∈ tt) : Inf tt ≤ t := assume s hs, hs t h private lemma le_Inf {tt : set (topological_space α)} {t : topological_space α} (h : ∀t'∈tt, t ≤ t') : t ≤ Inf tt := assume s hs t' ht', h t' ht' s hs def topological_space.induced {α : Type u} {β : Type v} (f : α → β) (t : topological_space β) : topological_space α := { topological_space . is_open := λs, ∃s', t.is_open s' ∧ s = preimage f s', is_open_univ := ⟨univ, by simp; exact t.is_open_univ⟩, is_open_inter := assume s₁ s₂ ⟨s'₁, hs₁, eq₁⟩ ⟨s'₂, hs₂, eq₂⟩, ⟨s'₁ ∩ s'₂, by simp [eq₁, eq₂]; exact t.is_open_inter _ _ hs₁ hs₂⟩, is_open_sUnion := assume s h, begin simp [classical.skolem] at h, cases h with f hf, apply exists.intro (⋃(x : set α) (h : x ∈ s), f x h), simp [sUnion_eq_Union, (λx h, (hf x h).right.symm)], exact (@is_open_Union β _ t _ $ assume i, show is_open (⋃h, f i h), from @is_open_Union β _ t _ $ assume h, (hf i h).left) end } lemma is_closed_induced_iff [t : topological_space β] {s : set α} {f : α → β} : @is_closed α (t.induced f) s ↔ (∃t, is_closed t ∧ s = preimage f t) := ⟨assume ⟨t, ht, heq⟩, ⟨-t, by simp; assumption, by simp [preimage_compl, heq.symm]⟩, assume ⟨t, ht, heq⟩, ⟨-t, ht, by simp [preimage_compl, heq.symm]⟩⟩ def topological_space.coinduced {α : Type u} {β : Type v} (f : α → β) (t : topological_space α) : topological_space β := { topological_space . is_open := λs, t.is_open (preimage f s), is_open_univ := by simp; exact t.is_open_univ, is_open_inter := assume s₁ s₂ h₁ h₂, by simp; exact t.is_open_inter _ _ h₁ h₂, is_open_sUnion := assume s h, by rw [preimage_sUnion]; exact (@is_open_Union _ _ t _ $ assume i, show is_open (⋃ (H : i ∈ s), preimage f i), from @is_open_Union _ _ t _ $ assume hi, h i hi) } instance : has_inf (topological_space α) := ⟨assume t₁ t₂ : topological_space α, { topological_space . is_open := λs, t₁.is_open s ∧ t₂.is_open s, is_open_univ := ⟨t₁.is_open_univ, t₂.is_open_univ⟩, is_open_inter := assume s₁ s₂ ⟨h₁₁, h₁₂⟩ ⟨h₂₁, h₂₂⟩, ⟨t₁.is_open_inter s₁ s₂ h₁₁ h₂₁, t₂.is_open_inter s₁ s₂ h₁₂ h₂₂⟩, is_open_sUnion := assume s h, ⟨t₁.is_open_sUnion _ $ assume t ht, (h t ht).left, t₂.is_open_sUnion _ $ assume t ht, (h t ht).right⟩ }⟩ instance : has_top (topological_space α) := ⟨{topological_space . is_open := λs, true, is_open_univ := trivial, is_open_inter := assume a b ha hb, trivial, is_open_sUnion := assume s h, trivial }⟩ instance {α : Type u} : complete_lattice (topological_space α) := { topological_space.partial_order with sup := λa b, Inf {x | a ≤ x ∧ b ≤ x}, le_sup_left := assume a b, le_Inf $ assume x, assume h : a ≤ x ∧ b ≤ x, h.left, le_sup_right := assume a b, le_Inf $ assume x, assume h : a ≤ x ∧ b ≤ x, h.right, sup_le := assume a b c h₁ h₂, Inf_le $ show c ∈ {x | a ≤ x ∧ b ≤ x}, from ⟨h₁, h₂⟩, inf := (⊓), le_inf := assume a b h h₁ h₂ s hs, ⟨h₁ s hs, h₂ s hs⟩, inf_le_left := assume a b s ⟨h₁, h₂⟩, h₁, inf_le_right := assume a b s ⟨h₁, h₂⟩, h₂, top := ⊤, le_top := assume a t ht, trivial, bot := Inf univ, bot_le := assume a, Inf_le $ mem_univ a, Sup := λtt, Inf {t | ∀t'∈tt, t' ≤ t}, le_Sup := assume s f h, le_Inf $ assume t ht, ht _ h, Sup_le := assume s f h, Inf_le $ assume t ht, h _ ht, Inf := Inf, le_Inf := assume s a, le_Inf, Inf_le := assume s a, Inf_le } instance inhabited_topological_space {α : Type u} : inhabited (topological_space α) := ⟨⊤⟩ lemma t2_space_top : @t2_space α ⊤ := ⟨assume x y hxy, ⟨{x}, {y}, trivial, trivial, mem_insert _ _, mem_insert _ _, eq_empty_of_forall_not_mem $ by intros z hz; simp at hz; cc⟩⟩ lemma le_of_nhds_le_nhds {t₁ t₂ : topological_space α} (h : ∀x, @nhds α t₂ x ≤ @nhds α t₁ x) : t₁ ≤ t₂ := assume s, show @is_open α t₁ s → @is_open α t₂ s, begin simp [is_open_iff_nhds]; exact assume hs a ha, h _ $ hs _ ha end lemma eq_of_nhds_eq_nhds {t₁ t₂ : topological_space α} (h : ∀x, @nhds α t₂ x = @nhds α t₁ x) : t₁ = t₂ := le_antisymm (le_of_nhds_le_nhds $ assume x, le_of_eq $ h x) (le_of_nhds_le_nhds $ assume x, le_of_eq $ (h x).symm) instance : topological_space empty := ⊤ instance : topological_space unit := ⊤ instance : topological_space bool := ⊤ instance : topological_space ℕ := ⊤ instance : topological_space ℤ := ⊤ instance sierpinski_space : topological_space Prop := topological_space.generate_from {{true}} instance {p : α → Prop} [t : topological_space α] : topological_space (subtype p) := topological_space.induced subtype.val t instance [t₁ : topological_space α] [t₂ : topological_space β] : topological_space (α × β) := topological_space.induced prod.fst t₁ ⊔ topological_space.induced prod.snd t₂ instance [t₁ : topological_space α] [t₂ : topological_space β] : topological_space (α ⊕ β) := topological_space.coinduced sum.inl t₁ ⊓ topological_space.coinduced sum.inr t₂ instance {β : α → Type v} [t₂ : Πa, topological_space (β a)] : topological_space (sigma β) := ⨅a, topological_space.coinduced (sigma.mk a) (t₂ a) instance topological_space_Pi {β : α → Type v} [t₂ : Πa, topological_space (β a)] : topological_space (Πa, β a) := ⨆a, topological_space.induced (λf, f a) (t₂ a) section open topological_space lemma generate_from_le {t : topological_space α} { g : set (set α) } (h : ∀s∈g, is_open s) : generate_from g ≤ t := assume s (hs : generate_open g s), generate_open.rec_on hs h is_open_univ (assume s t _ _ hs ht, is_open_inter hs ht) (assume k _ hk, is_open_sUnion hk) lemma supr_eq_generate_from {ι : Sort w} { g : ι → topological_space α } : supr g = generate_from (⋃i, {s | (g i).is_open s}) := le_antisymm (supr_le $ assume i s is_open_s, generate_open.basic _ $ by simp; exact ⟨i, is_open_s⟩) (generate_from_le $ assume s, begin simp, exact assume ⟨i, is_open_s⟩, have g i ≤ supr g, from le_supr _ _, this s is_open_s end) lemma sup_eq_generate_from { g₁ g₂ : topological_space α } : g₁ ⊔ g₂ = generate_from {s | g₁.is_open s ∨ g₂.is_open s} := le_antisymm (sup_le (assume s, generate_open.basic _ ∘ or.inl) (assume s, generate_open.basic _ ∘ or.inr)) (generate_from_le $ assume s hs, have h₁ : g₁ ≤ g₁ ⊔ g₂, from le_sup_left, have h₂ : g₂ ≤ g₁ ⊔ g₂, from le_sup_right, or.rec_on hs (h₁ s) (h₂ s)) lemma nhds_mono {t₁ t₂ : topological_space α} {a : α} (h : t₁ ≤ t₂) : @nhds α t₂ a ≤ @nhds α t₁ a := infi_le_infi $ assume s, infi_le_infi2 $ assume ⟨ha, hs⟩, ⟨⟨ha, h _ hs⟩, le_refl _⟩ lemma nhds_supr {ι : Sort w} {t : ι → topological_space α} {a : α} : @nhds α (supr t) a = (⨅i, @nhds α (t i) a) := le_antisymm (le_infi $ assume i, nhds_mono $ le_supr _ _) begin rw [supr_eq_generate_from, nhds_generate_from], exact (le_infi $ assume s, le_infi $ assume ⟨hs, hi⟩, begin simp at hi, cases hi with i hi, exact (infi_le_of_le i $ le_principal_iff.mpr $ @mem_nhds_sets α (t i) _ _ hi hs) end) end end end constructions section limit variables {α : Type u} [inhabited α] [topological_space α] open classical noncomputable def lim (f : filter α) : α := epsilon $ λa, f ≤ nhds a lemma lim_spec {f : filter α} (h : ∃a, f ≤ nhds a) : f ≤ nhds (lim f) := epsilon_spec h variables [t2_space α] {f : filter α} lemma lim_eq {a : α} (hf : f ≠ ⊥) (h : f ≤ nhds a) : lim f = a := eq_of_nhds_neq_bot $ neq_bot_of_le_neq_bot hf $ le_inf (lim_spec ⟨_, h⟩) h @[simp] lemma lim_nhds_eq {a : α} : lim (nhds a) = a := lim_eq nhds_neq_bot (le_refl _) @[simp] lemma lim_nhds_eq_of_closure {a : α} {s : set α} (h : a ∈ closure s) : lim (nhds a ⊓ principal s) = a := lim_eq begin rw [closure_eq_nhds] at h, exact h end inf_le_left end limit
a4934a2a85750ee1f7b02f91ea586c72d0471db6
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/exfalsoBug.lean
f8da44ecf73dace12b9efbaefe3a79627365b908
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
leanprover/lean4
4bdf9790294964627eb9be79f5e8f6157780b4cc
f1f9dc0f2f531af3312398999d8b8303fa5f096b
refs/heads/master
1,693,360,665,786
1,693,350,868,000
1,693,350,868,000
129,571,436
2,827
311
Apache-2.0
1,694,716,156,000
1,523,760,560,000
Lean
UTF-8
Lean
false
false
2,316
lean
namespace Ex1 def f (n : Nat) : Option { r : Nat // r ≤ n } := match n with | 0 => some ⟨0, Nat.le_refl _⟩ | n+1 => match f n with | some ⟨m, h₁⟩ => have : m < n+1 := Nat.lt_of_le_of_lt h₁ (Nat.lt_succ_self _) match f m with | some ⟨r, h₂⟩ => some ⟨r, Nat.le_trans h₂ (Nat.le_trans h₁ (Nat.le_succ _))⟩ | none => none | none => none end Ex1 namespace Ex2 def f (n : Nat) : Option { r : Nat // r ≤ n } := if h : n = 0 then some ⟨0, h ▸ Nat.le_refl _⟩ else match f (n-1) with | some ⟨m, h₁⟩ => have : m < n := Nat.lt_of_le_of_lt h₁ (Nat.pred_lt h) match f m with | some ⟨r, h₂⟩ => some ⟨r, Nat.le_trans h₂ (Nat.le_trans h₁ (Nat.pred_le _))⟩ | none => none | none => none end Ex2 namespace Ex3 def f' (n : Nat) : Option { r : Nat // r ≤ n } := match n with | 0 => some ⟨0, Nat.le_refl _⟩ | n+1 => match f' n with | some ⟨m, h₁⟩ => have : m < n+1 := Nat.lt_of_le_of_lt h₁ (Nat.lt_succ_self _) match f' m with | some ⟨r, h₂⟩ => some ⟨r, Nat.le_trans h₂ (Nat.le_trans h₁ (Nat.le_succ _))⟩ | none => none | none => none theorem f'_ne_none (n : Nat) : f' n ≠ none := by match n with | 0 => simp [f'] | n+1 => simp [f'] have ih₁ := f'_ne_none n split next m h₁ he => have : m < n+1 := Nat.lt_of_le_of_lt h₁ (Nat.lt_succ_self _) have ih₂ := f'_ne_none m split next => simp next h => contradiction next => contradiction def f (n : Nat) : Option Nat := match f' n with | some r => some r.1 | none => none theorem f_eq (n : Nat) : f n = match n with | 0 => some 0 | n => match f (n - 1) with | some m => f m | none => none := by unfold f split next r h => revert h split <;> try simp [f'] next => intro h; subst h; simp next hne => cases n <;> simp [f'] next => contradiction next n _ => have : Nat.succ n - 1 = n := rfl rw [this] split <;> try simp next r hrn h₁ => split <;> simp next => intro he; subst he; simp [*] next h_eq_none => have hne := f'_ne_none n contradiction end Ex3
e43542b6d4a8474386ad3d6027f3d47e09c52274
dd0f5513e11c52db157d2fcc8456d9401a6cd9da
/06_Inductive_Types.org.18.lean
532dbe1aa7a02db3b17cf113854d6f34180cc0df
[]
no_license
cjmazey/lean-tutorial
ba559a49f82aa6c5848b9bf17b7389bf7f4ba645
381f61c9fcac56d01d959ae0fa6e376f2c4e3b34
refs/heads/master
1,610,286,098,832
1,447,124,923,000
1,447,124,923,000
43,082,433
0
0
null
null
null
null
UTF-8
Lean
false
false
177
lean
/- page 83 -/ import standard structure Semigroup : Type := (carrier : Type) (mul : carrier → carrier → carrier) (mul_assoc : ∀ a b c, mul (mul a b) c = mul a (mul b c))
eb6390d578a97c77298983f290f9f48dd3896fe8
f618aea02cb4104ad34ecf3b9713065cc0d06103
/src/algebra/group_power.lean
dfcbf140ea5583a8d465ed543385b8b6f1b3a6c1
[ "Apache-2.0" ]
permissive
joehendrix/mathlib
84b6603f6be88a7e4d62f5b1b0cbb523bb82b9a5
c15eab34ad754f9ecd738525cb8b5a870e834ddc
refs/heads/master
1,589,606,591,630
1,555,946,393,000
1,555,946,393,000
182,813,854
0
0
null
1,555,946,309,000
1,555,946,308,000
null
UTF-8
Lean
false
false
27,323
lean
/- Copyright (c) 2015 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Robert Y. Lewis The power operation on monoids and groups. We separate this from group, because it depends on nat, which in turn depends on other parts of algebra. We have "pow a n" for natural number powers, and "gpow a i" for integer powers. The notation a^n is used for the first, but users can locally redefine it to gpow when needed. Note: power adopts the convention that 0^0=1. -/ import algebra.char_zero algebra.group algebra.ordered_field import data.int.basic data.list.basic local attribute [instance, priority 0] nat.cast_coe local attribute [instance, priority 0] int.cast_coe universes u v variable {α : Type u} @[simp] theorem inv_one [division_ring α] : (1⁻¹ : α) = 1 := by rw [inv_eq_one_div, one_div_one] @[simp] theorem inv_inv' [discrete_field α] {a:α} : a⁻¹⁻¹ = a := by rw [inv_eq_one_div, inv_eq_one_div, div_div_eq_mul_div, one_mul, div_one] /-- The power operation in a monoid. `a^n = a*a*...*a` n times. -/ def monoid.pow [monoid α] (a : α) : ℕ → α | 0 := 1 | (n+1) := a * monoid.pow n def add_monoid.smul [add_monoid α] (n : ℕ) (a : α) : α := @monoid.pow (multiplicative α) _ a n precedence `•`:70 local infix ` • ` := add_monoid.smul @[priority 5] instance monoid.has_pow [monoid α] : has_pow α ℕ := ⟨monoid.pow⟩ /- monoid -/ section monoid variables [monoid α] {β : Type u} [add_monoid β] @[simp] theorem pow_zero (a : α) : a^0 = 1 := rfl @[simp] theorem add_monoid.zero_smul (a : β) : 0 • a = 0 := rfl attribute [to_additive add_monoid.zero_smul] pow_zero theorem pow_succ (a : α) (n : ℕ) : a^(n+1) = a * a^n := rfl theorem succ_smul (a : β) (n : ℕ) : (n+1)•a = a + n•a := rfl attribute [to_additive succ_smul] pow_succ @[simp] theorem pow_one (a : α) : a^1 = a := mul_one _ @[simp] theorem add_monoid.one_smul (a : β) : 1•a = a := add_zero _ attribute [to_additive add_monoid.one_smul] pow_one theorem pow_mul_comm' (a : α) (n : ℕ) : a^n * a = a * a^n := by induction n with n ih; [rw [pow_zero, one_mul, mul_one], rw [pow_succ, mul_assoc, ih]] theorem smul_add_comm' : ∀ (a : β) (n : ℕ), n•a + a = a + n•a := @pow_mul_comm' (multiplicative β) _ theorem pow_succ' (a : α) (n : ℕ) : a^(n+1) = a^n * a := by rw [pow_succ, pow_mul_comm'] theorem succ_smul' (a : β) (n : ℕ) : (n+1)•a = n•a + a := by rw [succ_smul, smul_add_comm'] attribute [to_additive succ_smul'] pow_succ' theorem pow_two (a : α) : a^2 = a * a := show a*(a*1)=a*a, by rw mul_one theorem two_smul (a : β) : 2•a = a + a := show a+(a+0)=a+a, by rw add_zero attribute [to_additive two_smul] pow_two theorem pow_add (a : α) (m n : ℕ) : a^(m + n) = a^m * a^n := by induction n with n ih; [rw [add_zero, pow_zero, mul_one], rw [pow_succ, ← pow_mul_comm', ← mul_assoc, ← ih, ← pow_succ']]; refl theorem add_monoid.add_smul : ∀ (a : β) (m n : ℕ), (m + n)•a = m•a + n•a := @pow_add (multiplicative β) _ attribute [to_additive add_monoid.add_smul] pow_add @[simp] theorem one_pow (n : ℕ) : (1 : α)^n = (1:α) := by induction n with n ih; [refl, rw [pow_succ, ih, one_mul]] @[simp] theorem add_monoid.smul_zero (n : ℕ) : n•(0 : β) = (0:β) := by induction n with n ih; [refl, rw [succ_smul, ih, zero_add]] attribute [to_additive add_monoid.smul_zero] one_pow theorem pow_mul (a : α) (m n : ℕ) : a^(m * n) = (a^m)^n := by induction n with n ih; [rw mul_zero, rw [nat.mul_succ, pow_add, pow_succ', ih]]; refl theorem add_monoid.mul_smul' : ∀ (a : β) (m n : ℕ), m * n • a = n•(m•a) := @pow_mul (multiplicative β) _ attribute [to_additive add_monoid.mul_smul'] pow_mul theorem pow_mul' (a : α) (m n : ℕ) : a^(m * n) = (a^n)^m := by rw [mul_comm, pow_mul] theorem add_monoid.mul_smul (a : β) (m n : ℕ) : m * n • a = m•(n•a) := by rw [mul_comm, add_monoid.mul_smul'] attribute [to_additive add_monoid.mul_smul] pow_mul' @[simp] theorem add_monoid.smul_one [has_one β] : ∀ n : ℕ, n • (1 : β) = n := nat.eq_cast _ (add_monoid.zero_smul _) (add_monoid.one_smul _) (add_monoid.add_smul _) theorem pow_bit0 (a : α) (n : ℕ) : a ^ bit0 n = a^n * a^n := pow_add _ _ _ theorem bit0_smul (a : β) (n : ℕ) : bit0 n • a = n•a + n•a := add_monoid.add_smul _ _ _ attribute [to_additive bit0_smul] pow_bit0 theorem pow_bit1 (a : α) (n : ℕ) : a ^ bit1 n = a^n * a^n * a := by rw [bit1, pow_succ', pow_bit0] theorem bit1_smul : ∀ (a : β) (n : ℕ), bit1 n • a = n•a + n•a + a := @pow_bit1 (multiplicative β) _ attribute [to_additive bit1_smul] pow_bit1 theorem pow_mul_comm (a : α) (m n : ℕ) : a^m * a^n = a^n * a^m := by rw [←pow_add, ←pow_add, add_comm] theorem smul_add_comm : ∀ (a : β) (m n : ℕ), m•a + n•a = n•a + m•a := @pow_mul_comm (multiplicative β) _ attribute [to_additive smul_add_comm] pow_mul_comm @[simp] theorem list.prod_repeat (a : α) (n : ℕ) : (list.repeat a n).prod = a ^ n := by induction n with n ih; [refl, rw [list.repeat_succ, list.prod_cons, ih]]; refl @[simp] theorem list.sum_repeat : ∀ (a : β) (n : ℕ), (list.repeat a n).sum = n • a := @list.prod_repeat (multiplicative β) _ attribute [to_additive list.sum_repeat] list.prod_repeat @[simp] lemma units.coe_pow (u : units α) (n : ℕ) : ((u ^ n : units α) : α) = u ^ n := by induction n; simp [*, pow_succ] end monoid namespace is_monoid_hom variables {β : Type v} [monoid α] [monoid β] (f : α → β) [is_monoid_hom f] theorem map_pow (a : α) : ∀(n : ℕ), f (a ^ n) = (f a) ^ n | 0 := is_monoid_hom.map_one f | (nat.succ n) := by rw [pow_succ, is_monoid_hom.map_mul f, map_pow n]; refl end is_monoid_hom namespace is_add_monoid_hom variables {β : Type*} [add_monoid α] [add_monoid β] (f : α → β) [is_add_monoid_hom f] theorem map_smul (a : α) : ∀(n : ℕ), f (n • a) = n • (f a) | 0 := is_add_monoid_hom.map_zero f | (nat.succ n) := by rw [succ_smul, is_add_monoid_hom.map_add f, map_smul n]; refl end is_add_monoid_hom attribute [to_additive is_add_monoid_hom.map_smul] is_monoid_hom.map_pow @[simp] theorem nat.pow_eq_pow (p q : ℕ) : @has_pow.pow _ _ monoid.has_pow p q = p ^ q := by induction q with q ih; [refl, rw [nat.pow_succ, pow_succ, mul_comm, ih]] @[simp] theorem nat.smul_eq_mul (m n : ℕ) : m • n = m * n := by induction m with m ih; [rw [add_monoid.zero_smul, zero_mul], rw [succ_smul', ih, nat.succ_mul]] /- commutative monoid -/ section comm_monoid variables [comm_monoid α] {β : Type*} [add_comm_monoid β] theorem mul_pow (a b : α) (n : ℕ) : (a * b)^n = a^n * b^n := by induction n with n ih; [exact (mul_one _).symm, simp only [pow_succ, ih, mul_assoc, mul_left_comm]] theorem add_monoid.smul_add : ∀ (a b : β) (n : ℕ), n•(a + b) = n•a + n•b := @mul_pow (multiplicative β) _ attribute [to_additive add_monoid.add_smul] mul_pow instance pow.is_monoid_hom (n : ℕ) : is_monoid_hom ((^ n) : α → α) := { map_mul := λ _ _, mul_pow _ _ _, map_one := one_pow _ } instance add_monoid.smul.is_add_monoid_hom (n : ℕ) : is_add_monoid_hom (add_monoid.smul n : β → β) := { map_add := λ _ _, add_monoid.smul_add _ _ _, map_zero := add_monoid.smul_zero _ } attribute [to_additive add_monoid.smul.is_add_monoid_hom] pow.is_monoid_hom end comm_monoid section group variables [group α] {β : Type*} [add_group β] section nat @[simp] theorem inv_pow (a : α) (n : ℕ) : (a⁻¹)^n = (a^n)⁻¹ := by induction n with n ih; [exact one_inv.symm, rw [pow_succ', pow_succ, ih, mul_inv_rev]] @[simp] theorem add_monoid.neg_smul : ∀ (a : β) (n : ℕ), n•(-a) = -(n•a) := @inv_pow (multiplicative β) _ attribute [to_additive add_monoid.neg_smul] inv_pow theorem pow_sub (a : α) {m n : ℕ} (h : m ≥ n) : a^(m - n) = a^m * (a^n)⁻¹ := have h1 : m - n + n = m, from nat.sub_add_cancel h, have h2 : a^(m - n) * a^n = a^m, by rw [←pow_add, h1], eq_mul_inv_of_mul_eq h2 theorem add_monoid.smul_sub : ∀ (a : β) {m n : ℕ}, m ≥ n → (m - n)•a = m•a - n•a := @pow_sub (multiplicative β) _ attribute [to_additive add_monoid.smul_sub] inv_pow theorem pow_inv_comm (a : α) (m n : ℕ) : (a⁻¹)^m * a^n = a^n * (a⁻¹)^m := by rw inv_pow; exact inv_comm_of_comm (pow_mul_comm _ _ _) theorem add_monoid.smul_neg_comm : ∀ (a : β) (m n : ℕ), m•(-a) + n•a = n•a + m•(-a) := @pow_inv_comm (multiplicative β) _ attribute [to_additive add_monoid.smul_neg_comm] pow_inv_comm end nat open int /-- The power operation in a group. This extends `monoid.pow` to negative integers with the definition `a^(-n) = (a^n)⁻¹`. -/ def gpow (a : α) : ℤ → α | (of_nat n) := a^n | -[1+n] := (a^(nat.succ n))⁻¹ def gsmul (n : ℤ) (a : β) : β := @gpow (multiplicative β) _ a n @[priority 10] instance group.has_pow : has_pow α ℤ := ⟨gpow⟩ local infix ` • `:70 := gsmul local infix ` •ℕ `:70 := add_monoid.smul @[simp] theorem gpow_coe_nat (a : α) (n : ℕ) : a ^ (n:ℤ) = a ^ n := rfl @[simp] theorem gsmul_coe_nat (a : β) (n : ℕ) : (n:ℤ) • a = n •ℕ a := rfl attribute [to_additive gsmul_coe_nat] gpow_coe_nat @[simp] theorem gpow_of_nat (a : α) (n : ℕ) : a ^ of_nat n = a ^ n := rfl @[simp] theorem gsmul_of_nat (a : β) (n : ℕ) : of_nat n • a = n •ℕ a := rfl attribute [to_additive gsmul_of_nat] gpow_of_nat @[simp] theorem gpow_neg_succ (a : α) (n : ℕ) : a ^ -[1+n] = (a ^ n.succ)⁻¹ := rfl @[simp] theorem gsmul_neg_succ (a : β) (n : ℕ) : -[1+n] • a = - (n.succ •ℕ a) := rfl attribute [to_additive gsmul_neg_succ] gpow_neg_succ local attribute [ematch] le_of_lt open nat @[simp] theorem gpow_zero (a : α) : a ^ (0:ℤ) = 1 := rfl @[simp] theorem zero_gsmul (a : β) : (0:ℤ) • a = 0 := rfl attribute [to_additive zero_gsmul] gpow_zero @[simp] theorem gpow_one (a : α) : a ^ (1:ℤ) = a := mul_one _ @[simp] theorem one_gsmul (a : β) : (1:ℤ) • a = a := add_zero _ attribute [to_additive one_gsmul] gpow_one @[simp] theorem one_gpow : ∀ (n : ℤ), (1 : α) ^ n = 1 | (n : ℕ) := one_pow _ | -[1+ n] := show _⁻¹=(1:α), by rw [_root_.one_pow, one_inv] @[simp] theorem gsmul_zero : ∀ (n : ℤ), n • (0 : β) = 0 := @one_gpow (multiplicative β) _ attribute [to_additive gsmul_zero] one_gpow @[simp] theorem gpow_neg (a : α) : ∀ (n : ℤ), a ^ -n = (a ^ n)⁻¹ | (n+1:ℕ) := rfl | 0 := one_inv.symm | -[1+ n] := (inv_inv _).symm @[simp] theorem neg_gsmul : ∀ (a : β) (n : ℤ), -n • a = -(n • a) := @gpow_neg (multiplicative β) _ attribute [to_additive neg_gsmul] gpow_neg theorem gpow_neg_one (x : α) : x ^ (-1:ℤ) = x⁻¹ := congr_arg has_inv.inv $ pow_one x theorem neg_one_gsmul (x : β) : (-1:ℤ) • x = -x := congr_arg has_neg.neg $ add_monoid.one_smul x attribute [to_additive neg_one_gsmul] gpow_neg_one theorem inv_gpow (a : α) : ∀n:ℤ, a⁻¹ ^ n = (a ^ n)⁻¹ | (n : ℕ) := inv_pow a n | -[1+ n] := congr_arg has_inv.inv $ inv_pow a (n+1) private lemma gpow_add_aux (a : α) (m n : nat) : a ^ ((of_nat m) + -[1+n]) = a ^ of_nat m * a ^ -[1+n] := or.elim (nat.lt_or_ge m (nat.succ n)) (assume h1 : m < succ n, have h2 : m ≤ n, from le_of_lt_succ h1, suffices a ^ -[1+ n-m] = a ^ of_nat m * a ^ -[1+n], by rwa [of_nat_add_neg_succ_of_nat_of_lt h1], show (a ^ nat.succ (n - m))⁻¹ = a ^ of_nat m * a ^ -[1+n], by rw [← succ_sub h2, pow_sub _ (le_of_lt h1), mul_inv_rev, inv_inv]; refl) (assume : m ≥ succ n, suffices a ^ (of_nat (m - succ n)) = (a ^ (of_nat m)) * (a ^ -[1+ n]), by rw [of_nat_add_neg_succ_of_nat_of_ge]; assumption, suffices a ^ (m - succ n) = a ^ m * (a ^ n.succ)⁻¹, from this, by rw pow_sub; assumption) theorem gpow_add (a : α) : ∀ (i j : ℤ), a ^ (i + j) = a ^ i * a ^ j | (of_nat m) (of_nat n) := pow_add _ _ _ | (of_nat m) -[1+n] := gpow_add_aux _ _ _ | -[1+m] (of_nat n) := by rw [add_comm, gpow_add_aux, gpow_neg_succ, gpow_of_nat, ← inv_pow, ← pow_inv_comm] | -[1+m] -[1+n] := suffices (a ^ (m + succ (succ n)))⁻¹ = (a ^ m.succ)⁻¹ * (a ^ n.succ)⁻¹, from this, by rw [← succ_add_eq_succ_add, add_comm, _root_.pow_add, mul_inv_rev] theorem add_gsmul : ∀ (a : β) (i j : ℤ), (i + j) • a = i • a + j • a := @gpow_add (multiplicative β) _ theorem gpow_add_one (a : α) (i : ℤ) : a ^ (i + 1) = a ^ i * a := by rw [gpow_add, gpow_one] theorem add_one_gsmul : ∀ (a : β) (i : ℤ), (i + 1) • a = i • a + a := @gpow_add_one (multiplicative β) _ attribute [to_additive add_one_gsmul] gpow_add_one theorem gpow_one_add (a : α) (i : ℤ) : a ^ (1 + i) = a * a ^ i := by rw [gpow_add, gpow_one] theorem one_add_gsmul : ∀ (a : β) (i : ℤ), (1 + i) • a = a + i • a := @gpow_one_add (multiplicative β) _ attribute [to_additive one_add_gsmul] gpow_one_add theorem gpow_mul_comm (a : α) (i j : ℤ) : a ^ i * a ^ j = a ^ j * a ^ i := by rw [← gpow_add, ← gpow_add, add_comm] theorem gsmul_add_comm : ∀ (a : β) (i j), i • a + j • a = j • a + i • a := @gpow_mul_comm (multiplicative β) _ attribute [to_additive gsmul_add_comm] gpow_mul_comm theorem gpow_mul (a : α) : ∀ m n : ℤ, a ^ (m * n) = (a ^ m) ^ n | (m : ℕ) (n : ℕ) := pow_mul _ _ _ | (m : ℕ) -[1+ n] := (gpow_neg _ (m * succ n)).trans $ show (a ^ (m * succ n))⁻¹ = _, by rw pow_mul; refl | -[1+ m] (n : ℕ) := (gpow_neg _ (succ m * n)).trans $ show (a ^ (m.succ * n))⁻¹ = _, by rw [pow_mul, ← inv_pow]; refl | -[1+ m] -[1+ n] := (pow_mul a (succ m) (succ n)).trans $ show _ = (_⁻¹^_)⁻¹, by rw [inv_pow, inv_inv] theorem gsmul_mul' : ∀ (a : β) (m n : ℤ), m * n • a = n • (m • a) := @gpow_mul (multiplicative β) _ attribute [to_additive gsmul_mul'] gpow_mul theorem gpow_mul' (a : α) (m n : ℤ) : a ^ (m * n) = (a ^ n) ^ m := by rw [mul_comm, gpow_mul] theorem gsmul_mul (a : β) (m n : ℤ) : m * n • a = m • (n • a) := by rw [mul_comm, gsmul_mul'] attribute [to_additive gsmul_mul] gpow_mul' theorem gpow_bit0 (a : α) (n : ℤ) : a ^ bit0 n = a ^ n * a ^ n := gpow_add _ _ _ theorem bit0_gsmul (a : β) (n : ℤ) : bit0 n • a = n • a + n • a := gpow_add _ _ _ attribute [to_additive bit0_gsmul] gpow_bit0 theorem gpow_bit1 (a : α) (n : ℤ) : a ^ bit1 n = a ^ n * a ^ n * a := by rw [bit1, gpow_add]; simp [gpow_bit0] theorem bit1_gsmul : ∀ (a : β) (n : ℤ), bit1 n • a = n • a + n • a + a := @gpow_bit1 (multiplicative β) _ attribute [to_additive bit1_gsmul] gpow_bit1 theorem gsmul_neg (a : β) (n : ℤ) : gsmul n (- a) = - gsmul n a := begin induction n using int.induction_on with z ih z ih, { simp }, { rw [add_comm] {occs := occurrences.pos [1]}, simp [add_gsmul, ih, -add_comm] }, { rw [sub_eq_add_neg, add_comm] {occs := occurrences.pos [1]}, simp [ih, add_gsmul, neg_gsmul, -add_comm] } end attribute [to_additive gsmul_neg] gpow_neg end group namespace is_group_hom variables {β : Type v} [group α] [group β] (f : α → β) [is_group_hom f] theorem map_pow (a : α) (n : ℕ) : f (a ^ n) = f a ^ n := is_monoid_hom.map_pow f a n theorem map_gpow (a : α) (n : ℤ) : f (a ^ n) = f a ^ n := by cases n; [exact is_group_hom.map_pow f _ _, exact (is_group_hom.map_inv f _).trans (congr_arg _ $ is_group_hom.map_pow f _ _)] end is_group_hom namespace is_add_group_hom variables {β : Type v} [add_group α] [add_group β] (f : α → β) [is_add_group_hom f] theorem map_smul (a : α) (n : ℕ) : f (n • a) = n • f a := is_add_monoid_hom.map_smul f a n theorem map_gsmul (a : α) (n : ℤ) : f (gsmul n a) = gsmul n (f a) := @is_group_hom.map_gpow (multiplicative α) (multiplicative β) _ _ f _ a n end is_add_group_hom local infix ` •ℤ `:70 := gsmul section comm_monoid variables [comm_group α] {β : Type*} [add_comm_group β] theorem mul_gpow (a b : α) : ∀ n:ℤ, (a * b)^n = a^n * b^n | (n : ℕ) := mul_pow a b n | -[1+ n] := show _⁻¹=_⁻¹*_⁻¹, by rw [mul_pow, mul_inv_rev, mul_comm] theorem gsmul_add : ∀ (a b : β) (n : ℤ), n •ℤ (a + b) = n •ℤ a + n •ℤ b := @mul_gpow (multiplicative β) _ attribute [to_additive gsmul_add] mul_gpow theorem gsmul_sub : ∀ (a b : β) (n : ℤ), gsmul n (a - b) = gsmul n a - gsmul n b := by simp [gsmul_add, gsmul_neg] instance gpow.is_group_hom (n : ℤ) : is_group_hom ((^ n) : α → α) := ⟨λ _ _, mul_gpow _ _ n⟩ instance gsmul.is_add_group_hom (n : ℤ) : is_add_group_hom (gsmul n : β → β) := ⟨λ _ _, gsmul_add _ _ n⟩ attribute [to_additive gsmul.is_add_group_hom] gpow.is_group_hom end comm_monoid section group @[instance] theorem is_add_group_hom.gsmul {α β} [add_group α] [add_comm_group β] (f : α → β) [is_add_group_hom f] (z : ℤ) : is_add_group_hom (λa, gsmul z (f a)) := ⟨assume a b, by rw [is_add_group_hom.map_add f, gsmul_add]⟩ end group @[simp] lemma with_bot.coe_smul [add_monoid α] (a : α) (n : ℕ) : ((add_monoid.smul n a : α) : with_bot α) = add_monoid.smul n a := by induction n; simp [*, succ_smul]; refl theorem add_monoid.smul_eq_mul' [semiring α] (a : α) (n : ℕ) : n • a = a * n := by induction n with n ih; [rw [add_monoid.zero_smul, nat.cast_zero, mul_zero], rw [succ_smul', ih, nat.cast_succ, mul_add, mul_one]] theorem add_monoid.smul_eq_mul [semiring α] (n : ℕ) (a : α) : n • a = n * a := by rw [add_monoid.smul_eq_mul', nat.mul_cast_comm] theorem add_monoid.mul_smul_left [semiring α] (a b : α) (n : ℕ) : n • (a * b) = a * (n • b) := by rw [add_monoid.smul_eq_mul', add_monoid.smul_eq_mul', mul_assoc] theorem add_monoid.mul_smul_assoc [semiring α] (a b : α) (n : ℕ) : n • (a * b) = n • a * b := by rw [add_monoid.smul_eq_mul, add_monoid.smul_eq_mul, mul_assoc] lemma zero_pow [semiring α] : ∀ {n : ℕ}, 0 < n → (0 : α) ^ n = 0 | (n+1) _ := zero_mul _ @[simp] theorem nat.cast_pow [semiring α] (n m : ℕ) : (↑(n ^ m) : α) = ↑n ^ m := by induction m with m ih; [exact nat.cast_one, rw [nat.pow_succ, pow_succ', nat.cast_mul, ih]] @[simp] theorem int.coe_nat_pow (n m : ℕ) : ((n ^ m : ℕ) : ℤ) = n ^ m := by induction m with m ih; [exact int.coe_nat_one, rw [nat.pow_succ, pow_succ', int.coe_nat_mul, ih]] theorem int.nat_abs_pow (n : ℤ) (k : ℕ) : int.nat_abs (n ^ k) = (int.nat_abs n) ^ k := by induction k with k ih; [refl, rw [pow_succ', int.nat_abs_mul, nat.pow_succ, ih]] theorem is_semiring_hom.map_pow {β} [semiring α] [semiring β] (f : α → β) [is_semiring_hom f] (x : α) (n : ℕ) : f (x ^ n) = f x ^ n := by induction n with n ih; [exact is_semiring_hom.map_one f, rw [pow_succ, pow_succ, is_semiring_hom.map_mul f, ih]] theorem neg_one_pow_eq_or {R} [ring R] : ∀ n : ℕ, (-1 : R)^n = 1 ∨ (-1 : R)^n = -1 | 0 := or.inl rfl | (n+1) := (neg_one_pow_eq_or n).swap.imp (λ h, by rw [pow_succ, h, neg_one_mul, neg_neg]) (λ h, by rw [pow_succ, h, mul_one]) lemma pow_dvd_pow [comm_semiring α] (a : α) {m n : ℕ} (h : m ≤ n) : a ^ m ∣ a ^ n := ⟨a ^ (n - m), by rw [← pow_add, nat.add_sub_cancel' h]⟩ theorem gsmul_eq_mul [ring α] (a : α) : ∀ n, n •ℤ a = n * a | (n : ℕ) := add_monoid.smul_eq_mul _ _ | -[1+ n] := show -(_•_)=-_*_, by rw [neg_mul_eq_neg_mul_symm, add_monoid.smul_eq_mul, nat.cast_succ] theorem gsmul_eq_mul' [ring α] (a : α) (n : ℤ) : n •ℤ a = a * n := by rw [gsmul_eq_mul, int.mul_cast_comm] theorem mul_gsmul_left [ring α] (a b : α) (n : ℤ) : n •ℤ (a * b) = a * (n •ℤ b) := by rw [gsmul_eq_mul', gsmul_eq_mul', mul_assoc] theorem mul_gsmul_assoc [ring α] (a b : α) (n : ℤ) : n •ℤ (a * b) = n •ℤ a * b := by rw [gsmul_eq_mul, gsmul_eq_mul, mul_assoc] @[simp] theorem int.cast_pow [ring α] (n : ℤ) (m : ℕ) : (↑(n ^ m) : α) = ↑n ^ m := by induction m with m ih; [exact int.cast_one, rw [pow_succ, pow_succ, int.cast_mul, ih]] lemma neg_one_pow_eq_pow_mod_two [ring α] {n : ℕ} : (-1 : α) ^ n = -1 ^ (n % 2) := by rw [← nat.mod_add_div n 2, pow_add, pow_mul]; simp [pow_two] theorem pow_eq_zero [domain α] {x : α} {n : ℕ} (H : x^n = 0) : x = 0 := begin induction n with n ih, { rw pow_zero at H, rw [← mul_one x, H, mul_zero] }, exact or.cases_on (mul_eq_zero.1 H) id ih end theorem pow_ne_zero [domain α] {a : α} (n : ℕ) (h : a ≠ 0) : a ^ n ≠ 0 := mt pow_eq_zero h @[simp] theorem one_div_pow [division_ring α] {a : α} (ha : a ≠ 0) (n : ℕ) : (1 / a) ^ n = 1 / a ^ n := by induction n with n ih; [exact (div_one _).symm, rw [pow_succ', ih, division_ring.one_div_mul_one_div (pow_ne_zero _ ha) ha]]; refl @[simp] theorem division_ring.inv_pow [division_ring α] {a : α} (ha : a ≠ 0) (n : ℕ) : a⁻¹ ^ n = (a ^ n)⁻¹ := by simpa only [inv_eq_one_div] using one_div_pow ha n @[simp] theorem div_pow [field α] (a : α) {b : α} (hb : b ≠ 0) (n : ℕ) : (a / b) ^ n = a ^ n / b ^ n := by rw [div_eq_mul_one_div, mul_pow, one_div_pow hb, ← div_eq_mul_one_div] theorem add_monoid.smul_nonneg [ordered_comm_monoid α] {a : α} (H : 0 ≤ a) : ∀ n : ℕ, 0 ≤ n • a | 0 := le_refl _ | (n+1) := add_nonneg' H (add_monoid.smul_nonneg n) lemma pow_abs [decidable_linear_ordered_comm_ring α] (a : α) (n : ℕ) : (abs a)^n = abs (a^n) := by induction n with n ih; [exact (abs_one).symm, rw [pow_succ, pow_succ, ih, abs_mul]] lemma inv_pow' [discrete_field α] (a : α) (n : ℕ) : (a ^ n)⁻¹ = a⁻¹ ^ n := by induction n; simp [*, pow_succ, mul_inv', mul_comm] lemma pow_inv [division_ring α] (a : α) : ∀ n : ℕ, a ≠ 0 → (a^n)⁻¹ = (a⁻¹)^n | 0 ha := inv_one | (n+1) ha := by rw [pow_succ, pow_succ', mul_inv_eq (pow_ne_zero _ ha) ha, pow_inv _ ha] namespace add_monoid variable [ordered_comm_monoid α] theorem smul_le_smul {a : α} {n m : ℕ} (ha : 0 ≤ a) (h : n ≤ m) : n • a ≤ m • a := let ⟨k, hk⟩ := nat.le.dest h in calc n • a = n • a + 0 : (add_zero _).symm ... ≤ n • a + k • a : add_le_add_left' (smul_nonneg ha _) ... = m • a : by rw [← hk, add_smul] lemma smul_le_smul_of_le_right {a b : α} (hab : a ≤ b) : ∀ i : ℕ, i • a ≤ i • b | 0 := by simp | (k+1) := add_le_add' hab (smul_le_smul_of_le_right _) end add_monoid section linear_ordered_semiring variable [linear_ordered_semiring α] theorem pow_pos {a : α} (H : 0 < a) : ∀ (n : ℕ), 0 < a ^ n | 0 := zero_lt_one | (n+1) := mul_pos H (pow_pos _) theorem pow_nonneg {a : α} (H : 0 ≤ a) : ∀ (n : ℕ), 0 ≤ a ^ n | 0 := zero_le_one | (n+1) := mul_nonneg H (pow_nonneg _) theorem pow_lt_pow_of_lt_left {x y : α} {n : ℕ} (Hxy : x < y) (Hxpos : 0 ≤ x) (Hnpos : 0 < n) : x ^ n < y ^ n := begin cases lt_or_eq_of_le Hxpos, { rw ←nat.sub_add_cancel Hnpos, induction (n - 1), { simpa only [pow_one] }, rw [pow_add, pow_add, nat.succ_eq_add_one, pow_one, pow_one], apply mul_lt_mul ih (le_of_lt Hxy) h (le_of_lt (pow_pos (lt_trans h Hxy) _)) }, { rw [←h, zero_pow Hnpos], apply pow_pos (by rwa ←h at Hxy : 0 < y),} end theorem pow_right_inj {x y : α} {n : ℕ} (Hxpos : 0 ≤ x) (Hypos : 0 ≤ y) (Hnpos : 0 < n) (Hxyn : x ^ n = y ^ n) : x = y := begin rcases lt_trichotomy x y with hxy | rfl | hyx, { exact absurd Hxyn (ne_of_lt (pow_lt_pow_of_lt_left hxy Hxpos Hnpos)) }, { refl }, { exact absurd Hxyn (ne_of_gt (pow_lt_pow_of_lt_left hyx Hypos Hnpos)) }, end theorem one_le_pow_of_one_le {a : α} (H : 1 ≤ a) : ∀ (n : ℕ), 1 ≤ a ^ n | 0 := le_refl _ | (n+1) := by simpa only [mul_one] using mul_le_mul H (one_le_pow_of_one_le n) zero_le_one (le_trans zero_le_one H) theorem pow_ge_one_add_mul {a : α} (H : a ≥ 0) : ∀ (n : ℕ), 1 + n • a ≤ (1 + a) ^ n | 0 := le_of_eq $ add_zero _ | (n+1) := begin rw [pow_succ', succ_smul'], refine le_trans _ (mul_le_mul_of_nonneg_right (pow_ge_one_add_mul n) (add_nonneg zero_le_one H)), rw [mul_add, mul_one, ← add_assoc, add_le_add_iff_left], simpa only [one_mul] using mul_le_mul_of_nonneg_right ((le_add_iff_nonneg_right 1).2 (add_monoid.smul_nonneg H n)) H end theorem pow_le_pow {a : α} {n m : ℕ} (ha : 1 ≤ a) (h : n ≤ m) : a ^ n ≤ a ^ m := let ⟨k, hk⟩ := nat.le.dest h in calc a ^ n = a ^ n * 1 : (mul_one _).symm ... ≤ a ^ n * a ^ k : mul_le_mul_of_nonneg_left (one_le_pow_of_one_le ha _) (pow_nonneg (le_trans zero_le_one ha) _) ... = a ^ m : by rw [←hk, pow_add] lemma pow_lt_pow {a : α} {n m : ℕ} (h : 1 < a) (h2 : n < m) : a ^ n < a ^ m := begin have h' : 1 ≤ a := le_of_lt h, have h'' : 0 < a := lt_trans zero_lt_one h, cases m, cases h2, rw [pow_succ, ←one_mul (a ^ n)], exact mul_lt_mul h (pow_le_pow h' (nat.le_of_lt_succ h2)) (pow_pos h'' _) (le_of_lt h'') end lemma pow_le_pow_of_le_left {a b : α} (ha : 0 ≤ a) (hab : a ≤ b) : ∀ i : ℕ, a^i ≤ b^i | 0 := by simp | (k+1) := mul_le_mul hab (pow_le_pow_of_le_left _) (pow_nonneg ha _) (le_trans ha hab) lemma lt_of_pow_lt_pow {a b : α} (n : ℕ) (hb : 0 ≤ b) (h : a ^ n < b ^ n) : a < b := lt_of_not_ge $ λ hn, not_lt_of_ge (pow_le_pow_of_le_left hb hn _) h private lemma pow_lt_pow_of_lt_one_aux {a : α} (h : 0 < a) (ha : a < 1) (i : ℕ) : ∀ k : ℕ, a ^ (i + k + 1) < a ^ i | 0 := begin simp, rw ←one_mul (a^i), exact mul_lt_mul ha (le_refl _) (pow_pos h _) zero_le_one end | (k+1) := begin rw ←one_mul (a^i), apply mul_lt_mul ha _ _ zero_le_one, { apply le_of_lt, apply pow_lt_pow_of_lt_one_aux }, { show 0 < a ^ (i + (k + 1) + 0), apply pow_pos h } end private lemma pow_le_pow_of_le_one_aux {a : α} (h : 0 ≤ a) (ha : a ≤ 1) (i : ℕ) : ∀ k : ℕ, a ^ (i + k) ≤ a ^ i | 0 := by simp | (k+1) := by rw [←add_assoc, ←one_mul (a^i)]; exact mul_le_mul ha (pow_le_pow_of_le_one_aux _) (pow_nonneg h _) zero_le_one lemma pow_lt_pow_of_lt_one {a : α} (h : 0 < a) (ha : a < 1) {i j : ℕ} (hij : i < j) : a ^ j < a ^ i := let ⟨k, hk⟩ := nat.exists_eq_add_of_lt hij in by rw hk; exact pow_lt_pow_of_lt_one_aux h ha _ _ lemma pow_le_pow_of_le_one {a : α} (h : 0 ≤ a) (ha : a ≤ 1) {i j : ℕ} (hij : i ≤ j) : a ^ j ≤ a ^ i := let ⟨k, hk⟩ := nat.exists_eq_add_of_le hij in by rw hk; exact pow_le_pow_of_le_one_aux h ha _ _ lemma pow_le_one {x : α} : ∀ (n : ℕ) (h0 : 0 ≤ x) (h1 : x ≤ 1), x ^ n ≤ 1 | 0 h0 h1 := le_refl (1 : α) | (n+1) h0 h1 := mul_le_one h1 (pow_nonneg h0 _) (pow_le_one n h0 h1) end linear_ordered_semiring theorem pow_two_nonneg [linear_ordered_ring α] (a : α) : 0 ≤ a ^ 2 := by rw pow_two; exact mul_self_nonneg _ theorem pow_ge_one_add_sub_mul [linear_ordered_ring α] {a : α} (H : a ≥ 1) (n : ℕ) : 1 + n • (a - 1) ≤ a ^ n := by simpa only [add_sub_cancel'_right] using pow_ge_one_add_mul (sub_nonneg.2 H) n namespace int lemma units_pow_two (u : units ℤ) : u ^ 2 = 1 := (units_eq_one_or u).elim (λ h, h.symm ▸ rfl) (λ h, h.symm ▸ rfl) lemma units_pow_eq_pow_mod_two (u : units ℤ) (n : ℕ) : u ^ n = u ^ (n % 2) := by conv {to_lhs, rw ← nat.mod_add_div n 2}; rw [pow_add, pow_mul, units_pow_two, one_pow, mul_one] end int @[simp] lemma neg_square {α} [ring α] (z : α) : (-z)^2 = z^2 := by simp [pow, monoid.pow] lemma div_sq_cancel {α} [field α] {a : α} (ha : a ≠ 0) (b : α) : a^2 * b / a = a * b := by rw [pow_two, mul_assoc, mul_div_cancel_left _ ha]
5f5985ac698f5579a3e694c3364982ef6ef2ae15
b561a44b48979a98df50ade0789a21c79ee31288
/tests/lean/run/sarray.lean
ebfafc00ee70f488aca173c329a435a0b7499124
[ "Apache-2.0" ]
permissive
3401ijk/lean4
97659c475ebd33a034fed515cb83a85f75ccfb06
a5b1b8de4f4b038ff752b9e607b721f15a9a4351
refs/heads/master
1,693,933,007,651
1,636,424,845,000
1,636,424,845,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
910
lean
def mkByteArray (n : Nat) : ByteArray := do let mut r := {} for i in [:n] do r := r.push (UInt8.ofNat i) return r def tst1 (n : Nat) (expected : UInt32) : IO Unit := do let bs := mkByteArray n let sum := bs.foldl (init := 0) fun s b => s + b.toUInt32 assert! sum == expected IO.println sum #eval tst1 100 4950 def tst2 (n : Nat) (expected : UInt32) : IO Unit := do let bs := mkByteArray n let mut sum := 0 for b in bs do sum := sum + b.toUInt32 assert! sum == expected IO.println sum #eval tst2 100 4950 def tst3 (n : Nat) (expected : UInt32) : IO Unit := do let bs := mkByteArray n let mut sum := 0 for i in [:bs.size] do sum := sum + bs[i].toUInt32 assert! sum == expected IO.println sum #eval tst3 100 4950 set_option trace.compiler.ir.result true in def computeByteHash (bytes : ByteArray) := bytes.foldl (init := 1723) fun h b => mixHash h (hash b)
25e2d5b2a836632ea0ee07c2fa7c0fb528c4dc31
f20db13587f4dd28a4b1fbd31953afd491691fa0
/library/init/data/option_t.lean
84be9d3b56a59b091a3b76206b4ff01d7f2e0d21
[ "Apache-2.0" ]
permissive
AHartNtkn/lean
9a971edfc6857c63edcbf96bea6841b9a84cf916
0d83a74b26541421fc1aa33044c35b03759710ed
refs/heads/master
1,620,592,591,236
1,516,749,881,000
1,516,749,881,000
118,697,288
1
0
null
1,516,759,470,000
1,516,759,470,000
null
UTF-8
Lean
false
false
2,201
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.interactive init.category.transformers universes u v def option_t (m : Type u → Type v) [monad m] (α : Type u) : Type v := m (option α) @[inline] def option_t_bind {m : Type u → Type v} [monad m] {α β : Type u} (a : option_t m α) (b : α → option_t m β) : option_t m β := show m (option β), from do o ← a, match o with | none := return none | (some a) := b a end @[inline] def option_t_return {m : Type u → Type v} [monad m] {α : Type u} (a : α) : option_t m α := show m (option α), from return (some a) instance {m : Type u → Type v} [monad m] : monad (option_t m) := {pure := @option_t_return m _, bind := @option_t_bind m _, id_map := begin intros, simp [option_t_bind, function.comp], have h : option_t_bind._match_1 option_t_return = @pure m _ (option α), { funext s, cases s; refl }, { simp [h, monad.bind_pure] }, end, pure_bind := begin intros, simp [option_t_bind, option_t_return, monad.pure_bind] end, bind_assoc := begin intros, simp [option_t_bind, option_t_return, monad.bind_assoc], apply congr_arg, funext x, cases x, { simp [option_t_bind, monad.pure_bind] }, { refl } end} def option_t_orelse {m : Type u → Type v} [monad m] {α : Type u} (a : option_t m α) (b : option_t m α) : option_t m α := show m (option α), from do o ← a, match o with | none := b | (some v) := return (some v) end def option_t_fail {m : Type u → Type v} [monad m] {α : Type u} : option_t m α := show m (option α), from return none instance {m : Type u → Type v} [monad m] : alternative (option_t m) := { failure := @option_t_fail m _, orelse := @option_t_orelse m _, ..@option_t.monad m _ } def option_t.lift {m : Type u → Type v} [monad m] {α : Type u} (a : m α) : option_t m α := (some <$> a : m (option α)) instance option_t.monad_transformer : monad.monad_transformer option_t := { is_monad := @option_t.monad, monad_lift := @option_t.lift }
67db16e8a02cf8159da45caa47ec4e962f6f4c81
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/logic/equiv/set.lean
f99ab279507a721be2427a43c04d690c7af74970
[ "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
25,332
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Mario Carneiro -/ import data.set.function import logic.equiv.defs /-! # Equivalences and sets In this file we provide lemmas linking equivalences to sets. Some notable definitions are: * `equiv.of_injective`: an injective function is (noncomputably) equivalent to its range. * `equiv.set_congr`: two equal sets are equivalent as types. * `equiv.set.union`: a disjoint union of sets is equivalent to their `sum`. This file is separate from `equiv/basic` such that we do not require the full lattice structure on sets before defining what an equivalence is. -/ open function set universes u v w z variables {α : Sort u} {β : Sort v} {γ : Sort w} namespace equiv @[simp] lemma range_eq_univ {α : Type*} {β : Type*} (e : α ≃ β) : range e = univ := eq_univ_of_forall e.surjective protected lemma image_eq_preimage {α β} (e : α ≃ β) (s : set α) : e '' s = e.symm ⁻¹' s := set.ext $ λ x, mem_image_iff_of_inverse e.left_inv e.right_inv lemma _root_.set.mem_image_equiv {α β} {S : set α} {f : α ≃ β} {x : β} : x ∈ f '' S ↔ f.symm x ∈ S := set.ext_iff.mp (f.image_eq_preimage S) x /-- Alias for `equiv.image_eq_preimage` -/ lemma _root_.set.image_equiv_eq_preimage_symm {α β} (S : set α) (f : α ≃ β) : f '' S = f.symm ⁻¹' S := f.image_eq_preimage S /-- Alias for `equiv.image_eq_preimage` -/ lemma _root_.set.preimage_equiv_eq_image_symm {α β} (S : set α) (f : β ≃ α) : f ⁻¹' S = f.symm '' S := (f.symm.image_eq_preimage S).symm @[simp] protected lemma subset_image {α β} (e : α ≃ β) (s : set α) (t : set β) : e.symm '' t ⊆ s ↔ t ⊆ e '' s := by rw [image_subset_iff, e.image_eq_preimage] @[simp] protected lemma subset_image' {α β} (e : α ≃ β) (s : set α) (t : set β) : s ⊆ e.symm '' t ↔ e '' s ⊆ t := calc s ⊆ e.symm '' t ↔ e.symm.symm '' s ⊆ t : by rw e.symm.subset_image ... ↔ e '' s ⊆ t : by rw e.symm_symm @[simp] lemma symm_image_image {α β} (e : α ≃ β) (s : set α) : e.symm '' (e '' s) = s := e.left_inverse_symm.image_image s lemma eq_image_iff_symm_image_eq {α β} (e : α ≃ β) (s : set α) (t : set β) : t = e '' s ↔ e.symm '' t = s := (e.symm.injective.image_injective.eq_iff' (e.symm_image_image s)).symm @[simp] lemma image_symm_image {α β} (e : α ≃ β) (s : set β) : e '' (e.symm '' s) = s := e.symm.symm_image_image s @[simp] lemma image_preimage {α β} (e : α ≃ β) (s : set β) : e '' (e ⁻¹' s) = s := e.surjective.image_preimage s @[simp] lemma preimage_image {α β} (e : α ≃ β) (s : set α) : e ⁻¹' (e '' s) = s := e.injective.preimage_image s protected lemma image_compl {α β} (f : equiv α β) (s : set α) : f '' sᶜ = (f '' s)ᶜ := image_compl_eq f.bijective @[simp] lemma symm_preimage_preimage {α β} (e : α ≃ β) (s : set β) : e.symm ⁻¹' (e ⁻¹' s) = s := e.right_inverse_symm.preimage_preimage s @[simp] lemma preimage_symm_preimage {α β} (e : α ≃ β) (s : set α) : e ⁻¹' (e.symm ⁻¹' s) = s := e.left_inverse_symm.preimage_preimage s @[simp] lemma preimage_subset {α β} (e : α ≃ β) (s t : set β) : e ⁻¹' s ⊆ e ⁻¹' t ↔ s ⊆ t := e.surjective.preimage_subset_preimage_iff @[simp] lemma image_subset {α β} (e : α ≃ β) (s t : set α) : e '' s ⊆ e '' t ↔ s ⊆ t := image_subset_image_iff e.injective @[simp] lemma image_eq_iff_eq {α β} (e : α ≃ β) (s t : set α) : e '' s = e '' t ↔ s = t := image_eq_image e.injective lemma preimage_eq_iff_eq_image {α β} (e : α ≃ β) (s t) : e ⁻¹' s = t ↔ s = e '' t := preimage_eq_iff_eq_image e.bijective lemma eq_preimage_iff_image_eq {α β} (e : α ≃ β) (s t) : s = e ⁻¹' t ↔ e '' s = t := eq_preimage_iff_image_eq e.bijective @[simp] lemma prod_assoc_preimage {α β γ} {s : set α} {t : set β} {u : set γ} : equiv.prod_assoc α β γ ⁻¹' s ×ˢ (t ×ˢ u) = (s ×ˢ t) ×ˢ u := by { ext, simp [and_assoc] } @[simp] lemma prod_assoc_symm_preimage {α β γ} {s : set α} {t : set β} {u : set γ} : (equiv.prod_assoc α β γ).symm ⁻¹' (s ×ˢ t) ×ˢ u = s ×ˢ (t ×ˢ u) := by { ext, simp [and_assoc] } -- `@[simp]` doesn't like these lemmas, as it uses `set.image_congr'` to turn `equiv.prod_assoc` -- into a lambda expression and then unfold it. lemma prod_assoc_image {α β γ} {s : set α} {t : set β} {u : set γ} : equiv.prod_assoc α β γ '' (s ×ˢ t) ×ˢ u = s ×ˢ (t ×ˢ u) := by simpa only [equiv.image_eq_preimage] using prod_assoc_symm_preimage lemma prod_assoc_symm_image {α β γ} {s : set α} {t : set β} {u : set γ} : (equiv.prod_assoc α β γ).symm '' s ×ˢ (t ×ˢ u) = (s ×ˢ t) ×ˢ u := by simpa only [equiv.image_eq_preimage] using prod_assoc_preimage /-- A set `s` in `α × β` is equivalent to the sigma-type `Σ x, {y | (x, y) ∈ s}`. -/ def set_prod_equiv_sigma {α β : Type*} (s : set (α × β)) : s ≃ Σ x : α, {y | (x, y) ∈ s} := { to_fun := λ x, ⟨x.1.1, x.1.2, by simp⟩, inv_fun := λ x, ⟨(x.1, x.2.1), x.2.2⟩, left_inv := λ ⟨⟨x, y⟩, h⟩, rfl, right_inv := λ ⟨x, y, h⟩, rfl } /-- The subtypes corresponding to equal sets are equivalent. -/ @[simps apply] def set_congr {α : Type*} {s t : set α} (h : s = t) : s ≃ t := subtype_equiv_prop h /-- A set is equivalent to its image under an equivalence. -/ -- We could construct this using `equiv.set.image e s e.injective`, -- but this definition provides an explicit inverse. @[simps] def image {α β : Type*} (e : α ≃ β) (s : set α) : s ≃ e '' s := { to_fun := λ x, ⟨e x.1, by simp⟩, inv_fun := λ y, ⟨e.symm y.1, by { rcases y with ⟨-, ⟨a, ⟨m, rfl⟩⟩⟩, simpa using m, }⟩, left_inv := λ x, by simp, right_inv := λ y, by simp, }. namespace set /-- `univ α` is equivalent to `α`. -/ @[simps apply symm_apply] protected def univ (α) : @univ α ≃ α := ⟨coe, λ a, ⟨a, trivial⟩, λ ⟨a, _⟩, rfl, λ a, rfl⟩ /-- An empty set is equivalent to the `empty` type. -/ protected def empty (α) : (∅ : set α) ≃ empty := equiv_empty _ /-- An empty set is equivalent to a `pempty` type. -/ protected def pempty (α) : (∅ : set α) ≃ pempty := equiv_pempty _ /-- If sets `s` and `t` are separated by a decidable predicate, then `s ∪ t` is equivalent to `s ⊕ t`. -/ protected def union' {α} {s t : set α} (p : α → Prop) [decidable_pred p] (hs : ∀ x ∈ s, p x) (ht : ∀ x ∈ t, ¬ p x) : (s ∪ t : set α) ≃ s ⊕ t := { to_fun := λ x, if hp : p x then sum.inl ⟨_, x.2.resolve_right (λ xt, ht _ xt hp)⟩ else sum.inr ⟨_, x.2.resolve_left (λ xs, hp (hs _ xs))⟩, inv_fun := λ o, match o with | (sum.inl x) := ⟨x, or.inl x.2⟩ | (sum.inr x) := ⟨x, or.inr x.2⟩ end, left_inv := λ ⟨x, h'⟩, by by_cases p x; simp [union'._match_1, h]; congr, right_inv := λ o, begin rcases o with ⟨x, h⟩ | ⟨x, h⟩; dsimp [union'._match_1]; [simp [hs _ h], simp [ht _ h]] end } /-- If sets `s` and `t` are disjoint, then `s ∪ t` is equivalent to `s ⊕ t`. -/ protected def union {α} {s t : set α} [decidable_pred (λ x, x ∈ s)] (H : s ∩ t ⊆ ∅) : (s ∪ t : set α) ≃ s ⊕ t := set.union' (λ x, x ∈ s) (λ _, id) (λ x xt xs, H ⟨xs, xt⟩) lemma union_apply_left {α} {s t : set α} [decidable_pred (λ x, x ∈ s)] (H : s ∩ t ⊆ ∅) {a : (s ∪ t : set α)} (ha : ↑a ∈ s) : equiv.set.union H a = sum.inl ⟨a, ha⟩ := dif_pos ha lemma union_apply_right {α} {s t : set α} [decidable_pred (λ x, x ∈ s)] (H : s ∩ t ⊆ ∅) {a : (s ∪ t : set α)} (ha : ↑a ∈ t) : equiv.set.union H a = sum.inr ⟨a, ha⟩ := dif_neg $ λ h, H ⟨h, ha⟩ @[simp] lemma union_symm_apply_left {α} {s t : set α} [decidable_pred (λ x, x ∈ s)] (H : s ∩ t ⊆ ∅) (a : s) : (equiv.set.union H).symm (sum.inl a) = ⟨a, subset_union_left _ _ a.2⟩ := rfl @[simp] lemma union_symm_apply_right {α} {s t : set α} [decidable_pred (λ x, x ∈ s)] (H : s ∩ t ⊆ ∅) (a : t) : (equiv.set.union H).symm (sum.inr a) = ⟨a, subset_union_right _ _ a.2⟩ := rfl /-- A singleton set is equivalent to a `punit` type. -/ protected def singleton {α} (a : α) : ({a} : set α) ≃ punit.{u} := ⟨λ _, punit.star, λ _, ⟨a, mem_singleton _⟩, λ ⟨x, h⟩, by { simp at h, subst x }, λ ⟨⟩, rfl⟩ /-- Equal sets are equivalent. TODO: this is the same as `equiv.set_congr`! -/ @[simps apply symm_apply] protected def of_eq {α : Type u} {s t : set α} (h : s = t) : s ≃ t := equiv.set_congr h /-- If `a ∉ s`, then `insert a s` is equivalent to `s ⊕ punit`. -/ protected def insert {α} {s : set.{u} α} [decidable_pred (∈ s)] {a : α} (H : a ∉ s) : (insert a s : set α) ≃ s ⊕ punit.{u+1} := calc (insert a s : set α) ≃ ↥(s ∪ {a}) : equiv.set.of_eq (by simp) ... ≃ s ⊕ ({a} : set α) : equiv.set.union (λ x ⟨hx, hx'⟩, by simp [*] at *) ... ≃ s ⊕ punit.{u+1} : sum_congr (equiv.refl _) (equiv.set.singleton _) @[simp] lemma insert_symm_apply_inl {α} {s : set.{u} α} [decidable_pred (∈ s)] {a : α} (H : a ∉ s) (b : s) : (equiv.set.insert H).symm (sum.inl b) = ⟨b, or.inr b.2⟩ := rfl @[simp] lemma insert_symm_apply_inr {α} {s : set.{u} α} [decidable_pred (∈ s)] {a : α} (H : a ∉ s) (b : punit.{u+1}) : (equiv.set.insert H).symm (sum.inr b) = ⟨a, or.inl rfl⟩ := rfl @[simp] lemma insert_apply_left {α} {s : set.{u} α} [decidable_pred (∈ s)] {a : α} (H : a ∉ s) : equiv.set.insert H ⟨a, or.inl rfl⟩ = sum.inr punit.star := (equiv.set.insert H).apply_eq_iff_eq_symm_apply.2 rfl @[simp] lemma insert_apply_right {α} {s : set.{u} α} [decidable_pred (∈ s)] {a : α} (H : a ∉ s) (b : s) : equiv.set.insert H ⟨b, or.inr b.2⟩ = sum.inl b := (equiv.set.insert H).apply_eq_iff_eq_symm_apply.2 rfl /-- If `s : set α` is a set with decidable membership, then `s ⊕ sᶜ` is equivalent to `α`. -/ protected def sum_compl {α} (s : set α) [decidable_pred (∈ s)] : s ⊕ (sᶜ : set α) ≃ α := calc s ⊕ (sᶜ : set α) ≃ ↥(s ∪ sᶜ) : (equiv.set.union (by simp [set.ext_iff])).symm ... ≃ @univ α : equiv.set.of_eq (by simp) ... ≃ α : equiv.set.univ _ @[simp] lemma sum_compl_apply_inl {α : Type u} (s : set α) [decidable_pred (∈ s)] (x : s) : equiv.set.sum_compl s (sum.inl x) = x := rfl @[simp] lemma sum_compl_apply_inr {α : Type u} (s : set α) [decidable_pred (∈ s)] (x : sᶜ) : equiv.set.sum_compl s (sum.inr x) = x := rfl lemma sum_compl_symm_apply_of_mem {α : Type u} {s : set α} [decidable_pred (∈ s)] {x : α} (hx : x ∈ s) : (equiv.set.sum_compl s).symm x = sum.inl ⟨x, hx⟩ := have ↑(⟨x, or.inl hx⟩ : (s ∪ sᶜ : set α)) ∈ s, from hx, by { rw [equiv.set.sum_compl], simpa using set.union_apply_left _ this } lemma sum_compl_symm_apply_of_not_mem {α : Type u} {s : set α} [decidable_pred (∈ s)] {x : α} (hx : x ∉ s) : (equiv.set.sum_compl s).symm x = sum.inr ⟨x, hx⟩ := have ↑(⟨x, or.inr hx⟩ : (s ∪ sᶜ : set α)) ∈ sᶜ, from hx, by { rw [equiv.set.sum_compl], simpa using set.union_apply_right _ this } @[simp] lemma sum_compl_symm_apply {α : Type*} {s : set α} [decidable_pred (∈ s)] {x : s} : (equiv.set.sum_compl s).symm x = sum.inl x := by cases x with x hx; exact set.sum_compl_symm_apply_of_mem hx @[simp] lemma sum_compl_symm_apply_compl {α : Type*} {s : set α} [decidable_pred (∈ s)] {x : sᶜ} : (equiv.set.sum_compl s).symm x = sum.inr x := by cases x with x hx; exact set.sum_compl_symm_apply_of_not_mem hx /-- `sum_diff_subset s t` is the natural equivalence between `s ⊕ (t \ s)` and `t`, where `s` and `t` are two sets. -/ protected def sum_diff_subset {α} {s t : set α} (h : s ⊆ t) [decidable_pred (∈ s)] : s ⊕ (t \ s : set α) ≃ t := calc s ⊕ (t \ s : set α) ≃ (s ∪ (t \ s) : set α) : (equiv.set.union (by simp [inter_diff_self])).symm ... ≃ t : equiv.set.of_eq (by { simp [union_diff_self, union_eq_self_of_subset_left h] }) @[simp] lemma sum_diff_subset_apply_inl {α} {s t : set α} (h : s ⊆ t) [decidable_pred (∈ s)] (x : s) : equiv.set.sum_diff_subset h (sum.inl x) = inclusion h x := rfl @[simp] lemma sum_diff_subset_apply_inr {α} {s t : set α} (h : s ⊆ t) [decidable_pred (∈ s)] (x : t \ s) : equiv.set.sum_diff_subset h (sum.inr x) = inclusion (diff_subset t s) x := rfl lemma sum_diff_subset_symm_apply_of_mem {α} {s t : set α} (h : s ⊆ t) [decidable_pred (∈ s)] {x : t} (hx : x.1 ∈ s) : (equiv.set.sum_diff_subset h).symm x = sum.inl ⟨x, hx⟩ := begin apply (equiv.set.sum_diff_subset h).injective, simp only [apply_symm_apply, sum_diff_subset_apply_inl], exact subtype.eq rfl, end lemma sum_diff_subset_symm_apply_of_not_mem {α} {s t : set α} (h : s ⊆ t) [decidable_pred (∈ s)] {x : t} (hx : x.1 ∉ s) : (equiv.set.sum_diff_subset h).symm x = sum.inr ⟨x, ⟨x.2, hx⟩⟩ := begin apply (equiv.set.sum_diff_subset h).injective, simp only [apply_symm_apply, sum_diff_subset_apply_inr], exact subtype.eq rfl, end /-- If `s` is a set with decidable membership, then the sum of `s ∪ t` and `s ∩ t` is equivalent to `s ⊕ t`. -/ protected def union_sum_inter {α : Type u} (s t : set α) [decidable_pred (∈ s)] : (s ∪ t : set α) ⊕ (s ∩ t : set α) ≃ s ⊕ t := calc (s ∪ t : set α) ⊕ (s ∩ t : set α) ≃ (s ∪ t \ s : set α) ⊕ (s ∩ t : set α) : by rw [union_diff_self] ... ≃ (s ⊕ (t \ s : set α)) ⊕ (s ∩ t : set α) : sum_congr (set.union $ subset_empty_iff.2 (inter_diff_self _ _)) (equiv.refl _) ... ≃ s ⊕ (t \ s : set α) ⊕ (s ∩ t : set α) : sum_assoc _ _ _ ... ≃ s ⊕ (t \ s ∪ s ∩ t : set α) : sum_congr (equiv.refl _) begin refine (set.union' (∉ s) _ _).symm, exacts [λ x hx, hx.2, λ x hx, not_not_intro hx.1] end ... ≃ s ⊕ t : by { rw (_ : t \ s ∪ s ∩ t = t), rw [union_comm, inter_comm, inter_union_diff] } /-- Given an equivalence `e₀` between sets `s : set α` and `t : set β`, the set of equivalences `e : α ≃ β` such that `e ↑x = ↑(e₀ x)` for each `x : s` is equivalent to the set of equivalences between `sᶜ` and `tᶜ`. -/ protected def compl {α : Type u} {β : Type v} {s : set α} {t : set β} [decidable_pred (∈ s)] [decidable_pred (∈ t)] (e₀ : s ≃ t) : {e : α ≃ β // ∀ x : s, e x = e₀ x} ≃ ((sᶜ : set α) ≃ (tᶜ : set β)) := { to_fun := λ e, subtype_equiv e (λ a, not_congr $ iff.symm $ maps_to.mem_iff (maps_to_iff_exists_map_subtype.2 ⟨e₀, e.2⟩) (surj_on.maps_to_compl (surj_on_iff_exists_map_subtype.2 ⟨t, e₀, subset.refl t, e₀.surjective, e.2⟩) e.1.injective)), inv_fun := λ e₁, subtype.mk (calc α ≃ s ⊕ (sᶜ : set α) : (set.sum_compl s).symm ... ≃ t ⊕ (tᶜ : set β) : e₀.sum_congr e₁ ... ≃ β : set.sum_compl t) (λ x, by simp only [sum.map_inl, trans_apply, sum_congr_apply, set.sum_compl_apply_inl, set.sum_compl_symm_apply]), left_inv := λ e, begin ext x, by_cases hx : x ∈ s, { simp only [set.sum_compl_symm_apply_of_mem hx, ←e.prop ⟨x, hx⟩, sum.map_inl, sum_congr_apply, trans_apply, subtype.coe_mk, set.sum_compl_apply_inl] }, { simp only [set.sum_compl_symm_apply_of_not_mem hx, sum.map_inr, subtype_equiv_apply, set.sum_compl_apply_inr, trans_apply, sum_congr_apply, subtype.coe_mk] }, end, right_inv := λ e, equiv.ext $ λ x, by simp only [sum.map_inr, subtype_equiv_apply, set.sum_compl_apply_inr, function.comp_app, sum_congr_apply, equiv.coe_trans, subtype.coe_eta, subtype.coe_mk, set.sum_compl_symm_apply_compl] } /-- The set product of two sets is equivalent to the type product of their coercions to types. -/ protected def prod {α β} (s : set α) (t : set β) : ↥(s ×ˢ t) ≃ s × t := @subtype_prod_equiv_prod α β s t /-- The set `set.pi set.univ s` is equivalent to `Π a, s a`. -/ @[simps] protected def univ_pi {α : Type*} {β : α → Type*} (s : Π a, set (β a)) : pi univ s ≃ Π a, s a := { to_fun := λ f a, ⟨(f : Π a, β a) a, f.2 a (mem_univ a)⟩, inv_fun := λ f, ⟨λ a, f a, λ a ha, (f a).2⟩, left_inv := λ ⟨f, hf⟩, by { ext a, refl }, right_inv := λ f, by { ext a, refl } } /-- If a function `f` is injective on a set `s`, then `s` is equivalent to `f '' s`. -/ protected noncomputable def image_of_inj_on {α β} (f : α → β) (s : set α) (H : inj_on f s) : s ≃ (f '' s) := ⟨λ p, ⟨f p, mem_image_of_mem f p.2⟩, λ p, ⟨classical.some p.2, (classical.some_spec p.2).1⟩, λ ⟨x, h⟩, subtype.eq (H (classical.some_spec (mem_image_of_mem f h)).1 h (classical.some_spec (mem_image_of_mem f h)).2), λ ⟨y, h⟩, subtype.eq (classical.some_spec h).2⟩ /-- If `f` is an injective function, then `s` is equivalent to `f '' s`. -/ @[simps apply] protected noncomputable def image {α β} (f : α → β) (s : set α) (H : injective f) : s ≃ (f '' s) := equiv.set.image_of_inj_on f s (H.inj_on s) @[simp] protected lemma image_symm_apply {α β} (f : α → β) (s : set α) (H : injective f) (x : α) (h : x ∈ s) : (set.image f s H).symm ⟨f x, ⟨x, ⟨h, rfl⟩⟩⟩ = ⟨x, h⟩ := begin apply (set.image f s H).injective, simp [(set.image f s H).apply_symm_apply], end lemma image_symm_preimage {α β} {f : α → β} (hf : injective f) (u s : set α) : (λ x, (set.image f s hf).symm x : f '' s → α) ⁻¹' u = coe ⁻¹' (f '' u) := begin ext ⟨b, a, has, rfl⟩, have : ∀(h : ∃a', a' ∈ s ∧ a' = a), classical.some h = a := λ h, (classical.some_spec h).2, simp [equiv.set.image, equiv.set.image_of_inj_on, hf.eq_iff, this], end /-- If `α` is equivalent to `β`, then `set α` is equivalent to `set β`. -/ @[simps] protected def congr {α β : Type*} (e : α ≃ β) : set α ≃ set β := ⟨λ s, e '' s, λ t, e.symm '' t, symm_image_image e, symm_image_image e.symm⟩ /-- The set `{x ∈ s | t x}` is equivalent to the set of `x : s` such that `t x`. -/ protected def sep {α : Type u} (s : set α) (t : α → Prop) : ({ x ∈ s | t x } : set α) ≃ { x : s | t x } := (equiv.subtype_subtype_equiv_subtype_inter s t).symm /-- The set `𝒫 S := {x | x ⊆ S}` is equivalent to the type `set S`. -/ protected def powerset {α} (S : set α) : 𝒫 S ≃ set S := { to_fun := λ x : 𝒫 S, coe ⁻¹' (x : set α), inv_fun := λ x : set S, ⟨coe '' x, by rintro _ ⟨a : S, _, rfl⟩; exact a.2⟩, left_inv := λ x, by ext y; exact ⟨λ ⟨⟨_, _⟩, h, rfl⟩, h, λ h, ⟨⟨_, x.2 h⟩, h, rfl⟩⟩, right_inv := λ x, by ext; simp } /-- If `s` is a set in `range f`, then its image under `range_splitting f` is in bijection (via `f`) with `s`. -/ @[simps] noncomputable def range_splitting_image_equiv {α β : Type*} (f : α → β) (s : set (range f)) : range_splitting f '' s ≃ s := { to_fun := λ x, ⟨⟨f x, by simp⟩, (by { rcases x with ⟨x, ⟨y, ⟨m, rfl⟩⟩⟩, simpa [apply_range_splitting f] using m, })⟩, inv_fun := λ x, ⟨range_splitting f x, ⟨x, ⟨x.2, rfl⟩⟩⟩, left_inv := λ x, by { rcases x with ⟨x, ⟨y, ⟨m, rfl⟩⟩⟩, simp [apply_range_splitting f] }, right_inv := λ x, by simp [apply_range_splitting f], } end set /-- If `f : α → β` has a left-inverse when `α` is nonempty, then `α` is computably equivalent to the range of `f`. While awkward, the `nonempty α` hypothesis on `f_inv` and `hf` allows this to be used when `α` is empty too. This hypothesis is absent on analogous definitions on stronger `equiv`s like `linear_equiv.of_left_inverse` and `ring_equiv.of_left_inverse` as their typeclass assumptions are already sufficient to ensure non-emptiness. -/ @[simps] def of_left_inverse {α β : Sort*} (f : α → β) (f_inv : nonempty α → β → α) (hf : Π h : nonempty α, left_inverse (f_inv h) f) : α ≃ range f := { to_fun := λ a, ⟨f a, a, rfl⟩, inv_fun := λ b, f_inv (nonempty_of_exists b.2) b, left_inv := λ a, hf ⟨a⟩ a, right_inv := λ ⟨b, a, ha⟩, subtype.eq $ show f (f_inv ⟨a⟩ b) = b, from eq.trans (congr_arg f $ by exact ha ▸ (hf _ a)) ha } /-- If `f : α → β` has a left-inverse, then `α` is computably equivalent to the range of `f`. Note that if `α` is empty, no such `f_inv` exists and so this definition can't be used, unlike the stronger but less convenient `of_left_inverse`. -/ abbreviation of_left_inverse' {α β : Sort*} (f : α → β) (f_inv : β → α) (hf : left_inverse f_inv f) : α ≃ range f := of_left_inverse f (λ _, f_inv) (λ _, hf) /-- If `f : α → β` is an injective function, then domain `α` is equivalent to the range of `f`. -/ @[simps apply] noncomputable def of_injective {α β} (f : α → β) (hf : injective f) : α ≃ range f := equiv.of_left_inverse f (λ h, by exactI function.inv_fun f) (λ h, by exactI function.left_inverse_inv_fun hf) theorem apply_of_injective_symm {α β} {f : α → β} (hf : injective f) (b : range f) : f ((of_injective f hf).symm b) = b := subtype.ext_iff.1 $ (of_injective f hf).apply_symm_apply b @[simp] theorem of_injective_symm_apply {α β} {f : α → β} (hf : injective f) (a : α) : (of_injective f hf).symm ⟨f a, ⟨a, rfl⟩⟩ = a := begin apply (of_injective f hf).injective, simp [apply_of_injective_symm hf], end lemma coe_of_injective_symm {α β} {f : α → β} (hf : injective f) : ((of_injective f hf).symm : range f → α) = range_splitting f := by { ext ⟨y, x, rfl⟩, apply hf, simp [apply_range_splitting f] } @[simp] lemma self_comp_of_injective_symm {α β} {f : α → β} (hf : injective f) : f ∘ ((of_injective f hf).symm) = coe := funext (λ x, apply_of_injective_symm hf x) lemma of_left_inverse_eq_of_injective {α β : Type*} (f : α → β) (f_inv : nonempty α → β → α) (hf : Π h : nonempty α, left_inverse (f_inv h) f) : of_left_inverse f f_inv hf = of_injective f ((em (nonempty α)).elim (λ h, (hf h).injective) (λ h _ _ _, by { haveI : subsingleton α := subsingleton_of_not_nonempty h, simp })) := by { ext, simp } lemma of_left_inverse'_eq_of_injective {α β : Type*} (f : α → β) (f_inv : β → α) (hf : left_inverse f_inv f) : of_left_inverse' f f_inv hf = of_injective f hf.injective := by { ext, simp } protected lemma set_forall_iff {α β} (e : α ≃ β) {p : set α → Prop} : (∀ a, p a) ↔ (∀ a, p (e ⁻¹' a)) := e.injective.preimage_surjective.forall lemma preimage_pi_equiv_pi_subtype_prod_symm_pi {α : Type*} {β : α → Type*} (p : α → Prop) [decidable_pred p] (s : Π i, set (β i)) : (pi_equiv_pi_subtype_prod p β).symm ⁻¹' pi univ s = (pi univ (λ i : {i // p i}, s i)) ×ˢ pi univ (λ i : {i // ¬p i}, s i) := begin ext ⟨f, g⟩, simp only [mem_preimage, mem_univ_pi, prod_mk_mem_set_prod_eq, subtype.forall, ← forall_and_distrib], refine forall_congr (λ i, _), dsimp only [subtype.coe_mk], by_cases hi : p i; simp [hi] end /-- `sigma_fiber_equiv f` for `f : α → β` is the natural equivalence between the type of all preimages of points under `f` and the total space `α`. -/ -- See also `equiv.sigma_fiber_equiv`. @[simps] def sigma_preimage_equiv {α β} (f : α → β) : (Σ b, f ⁻¹' {b}) ≃ α := sigma_fiber_equiv f /-- A family of equivalences between preimages of points gives an equivalence between domains. -/ -- See also `equiv.of_fiber_equiv`. @[simps] def of_preimage_equiv {α β γ} {f : α → γ} {g : β → γ} (e : Π c, (f ⁻¹' {c}) ≃ (g ⁻¹' {c})) : α ≃ β := equiv.of_fiber_equiv e lemma of_preimage_equiv_map {α β γ} {f : α → γ} {g : β → γ} (e : Π c, (f ⁻¹' {c}) ≃ (g ⁻¹' {c})) (a : α) : g (of_preimage_equiv e a) = f a := equiv.of_fiber_equiv_map e a end equiv /-- If a function is a bijection between two sets `s` and `t`, then it induces an equivalence between the types `↥s` and `↥t`. -/ noncomputable def set.bij_on.equiv {α : Type*} {β : Type*} {s : set α} {t : set β} (f : α → β) (h : bij_on f s t) : s ≃ t := equiv.of_bijective _ h.bijective /-- The composition of an updated function with an equiv on a subset can be expressed as an updated function. -/ lemma dite_comp_equiv_update {α : Type*} {β : Sort*} {γ : Sort*} {s : set α} (e : β ≃ s) (v : β → γ) (w : α → γ) (j : β) (x : γ) [decidable_eq β] [decidable_eq α] [∀ j, decidable (j ∈ s)] : (λ (i : α), if h : i ∈ s then (function.update v j x) (e.symm ⟨i, h⟩) else w i) = function.update (λ (i : α), if h : i ∈ s then v (e.symm ⟨i, h⟩) else w i) (e j) x := begin ext i, by_cases h : i ∈ s, { rw [dif_pos h, function.update_apply_equiv_apply, equiv.symm_symm, function.comp, function.update_apply, function.update_apply, dif_pos h], have h_coe : (⟨i, h⟩ : s) = e j ↔ i = e j := subtype.ext_iff.trans (by rw subtype.coe_mk), simp_rw h_coe }, { have : i ≠ e j, by { contrapose! h, have : (e j : α) ∈ s := (e j).2, rwa ← h at this }, simp [h, this] } end
09be9e5c5132601081b2e4a70e7c991b98a031c9
df561f413cfe0a88b1056655515399c546ff32a5
/3-multiplication-world/l3.lean
35ea0e6b7c2a5ecaa32e28ab01807d39e33daeea
[]
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
147
lean
lemma one_mul (m : mynat) : 1 * m = m := begin induction m with k Pk, rw mul_zero, refl, rw mul_succ, rw Pk, symmetry, exact succ_eq_add_one _, end
e8adab1abf6866c360ae62eaf37d7df2eb478cac
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/analysis/special_functions/trigonometric/arctan.lean
ab95d5755caab27f55327bf0da5101dd4c5e7a02
[ "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
7,977
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.trigonometric.complex /-! # The `arctan` function. Inequalities, derivatives, and `real.tan` as a `local_homeomorph` between `(-(π / 2), π / 2)` and the whole line. -/ noncomputable theory namespace real open set filter open_locale topological_space real lemma tan_add {x y : ℝ} (h : ((∀ k : ℤ, x ≠ (2 * k + 1) * π / 2) ∧ ∀ l : ℤ, y ≠ (2 * l + 1) * π / 2) ∨ ((∃ k : ℤ, x = (2 * k + 1) * π / 2) ∧ ∃ l : ℤ, y = (2 * l + 1) * π / 2)) : tan (x + y) = (tan x + tan y) / (1 - tan x * tan y) := by simpa only [← complex.of_real_inj, complex.of_real_sub, complex.of_real_add, complex.of_real_div, complex.of_real_mul, complex.of_real_tan] using @complex.tan_add (x:ℂ) (y:ℂ) (by convert h; norm_cast) lemma tan_add' {x y : ℝ} (h : ((∀ k : ℤ, x ≠ (2 * k + 1) * π / 2) ∧ ∀ l : ℤ, y ≠ (2 * l + 1) * π / 2)) : tan (x + y) = (tan x + tan y) / (1 - tan x * tan y) := tan_add (or.inl h) lemma tan_two_mul {x:ℝ} : tan (2 * x) = 2 * tan x / (1 - tan x ^ 2) := by simpa only [← complex.of_real_inj, complex.of_real_sub, complex.of_real_div, complex.of_real_pow, complex.of_real_mul, complex.of_real_tan, complex.of_real_bit0, complex.of_real_one] using complex.tan_two_mul lemma tan_ne_zero_iff {θ : ℝ} : tan θ ≠ 0 ↔ ∀ k : ℤ, θ ≠ k * π / 2 := by rw [← complex.of_real_ne_zero, complex.of_real_tan, complex.tan_ne_zero_iff]; norm_cast lemma tan_eq_zero_iff {θ : ℝ} : tan θ = 0 ↔ ∃ k : ℤ, θ = k * π / 2 := by rw [← not_iff_not, not_exists, ← ne, tan_ne_zero_iff] lemma tan_int_mul_pi_div_two (n : ℤ) : tan (n * π/2) = 0 := tan_eq_zero_iff.mpr (by use n) lemma continuous_on_tan : continuous_on tan {x | cos x ≠ 0} := begin suffices : continuous_on (λ x, sin x / cos x) {x | cos x ≠ 0}, { have h_eq : (λ x, sin x / cos x) = tan, by {ext1 x, rw tan_eq_sin_div_cos, }, rwa h_eq at this, }, exact continuous_on_sin.div continuous_on_cos (λ x, id), end @[continuity] lemma continuous_tan : continuous (λ x : {x | cos x ≠ 0}, tan x) := continuous_on_iff_continuous_restrict.1 continuous_on_tan lemma continuous_on_tan_Ioo : continuous_on tan (Ioo (-(π/2)) (π/2)) := begin refine continuous_on.mono continuous_on_tan (λ x, _), simp only [and_imp, mem_Ioo, mem_set_of_eq, ne.def], rw cos_eq_zero_iff, rintros hx_gt hx_lt ⟨r, hxr_eq⟩, cases le_or_lt 0 r, { rw lt_iff_not_ge at hx_lt, refine hx_lt _, rw [hxr_eq, ← one_mul (π / 2), mul_div_assoc, ge_iff_le, mul_le_mul_right (half_pos pi_pos)], simp [h], }, { rw lt_iff_not_ge at hx_gt, refine hx_gt _, rw [hxr_eq, ← one_mul (π / 2), mul_div_assoc, ge_iff_le, neg_mul_eq_neg_mul, mul_le_mul_right (half_pos pi_pos)], have hr_le : r ≤ -1, by rwa [int.lt_iff_add_one_le, ← le_neg_iff_add_nonpos_right] at h, rw [← le_sub_iff_add_le, mul_comm, ← le_div_iff], { norm_num, rw [← int.cast_one, ← int.cast_neg], norm_cast, exact hr_le, }, { exact zero_lt_two, }, }, end lemma surj_on_tan : surj_on tan (Ioo (-(π / 2)) (π / 2)) univ := have _ := neg_lt_self pi_div_two_pos, continuous_on_tan_Ioo.surj_on_of_tendsto (nonempty_Ioo.2 this) (by simp [tendsto_tan_neg_pi_div_two, this]) (by simp [tendsto_tan_pi_div_two, this]) lemma tan_surjective : function.surjective tan := λ x, surj_on_tan.subset_range trivial lemma image_tan_Ioo : tan '' (Ioo (-(π / 2)) (π / 2)) = univ := univ_subset_iff.1 surj_on_tan /-- `real.tan` as an `order_iso` between `(-(π / 2), π / 2)` and `ℝ`. -/ def tan_order_iso : Ioo (-(π / 2)) (π / 2) ≃o ℝ := (strict_mono_on_tan.order_iso _ _).trans $ (order_iso.set_congr _ _ image_tan_Ioo).trans order_iso.set.univ /-- Inverse of the `tan` function, returns values in the range `-π / 2 < arctan x` and `arctan x < π / 2` -/ @[pp_nodot] noncomputable def arctan (x : ℝ) : ℝ := tan_order_iso.symm x @[simp] lemma tan_arctan (x : ℝ) : tan (arctan x) = x := tan_order_iso.apply_symm_apply x lemma arctan_mem_Ioo (x : ℝ) : arctan x ∈ Ioo (-(π / 2)) (π / 2) := subtype.coe_prop _ @[simp] lemma range_arctan : range arctan = Ioo (-(π / 2)) (π / 2) := ((equiv_like.surjective _).range_comp _).trans subtype.range_coe lemma arctan_tan {x : ℝ} (hx₁ : -(π / 2) < x) (hx₂ : x < π / 2) : arctan (tan x) = x := subtype.ext_iff.1 $ tan_order_iso.symm_apply_apply ⟨x, hx₁, hx₂⟩ lemma cos_arctan_pos (x : ℝ) : 0 < cos (arctan x) := cos_pos_of_mem_Ioo $ arctan_mem_Ioo x lemma cos_sq_arctan (x : ℝ) : cos (arctan x) ^ 2 = 1 / (1 + x ^ 2) := by rw [one_div, ← inv_one_add_tan_sq (cos_arctan_pos x).ne', tan_arctan] lemma sin_arctan (x : ℝ) : sin (arctan x) = x / sqrt (1 + x ^ 2) := by rw [← tan_div_sqrt_one_add_tan_sq (cos_arctan_pos x), tan_arctan] lemma cos_arctan (x : ℝ) : cos (arctan x) = 1 / sqrt (1 + x ^ 2) := by rw [one_div, ← inv_sqrt_one_add_tan_sq (cos_arctan_pos x), tan_arctan] lemma arctan_lt_pi_div_two (x : ℝ) : arctan x < π / 2 := (arctan_mem_Ioo x).2 lemma neg_pi_div_two_lt_arctan (x : ℝ) : -(π / 2) < arctan x := (arctan_mem_Ioo x).1 lemma arctan_eq_arcsin (x : ℝ) : arctan x = arcsin (x / sqrt (1 + x ^ 2)) := eq.symm $ arcsin_eq_of_sin_eq (sin_arctan x) (mem_Icc_of_Ioo $ arctan_mem_Ioo x) lemma arcsin_eq_arctan {x : ℝ} (h : x ∈ Ioo (-(1:ℝ)) 1) : arcsin x = arctan (x / sqrt (1 - x ^ 2)) := begin rw [arctan_eq_arcsin, div_pow, sq_sqrt, one_add_div, div_div, ← sqrt_mul, mul_div_cancel', sub_add_cancel, sqrt_one, div_one]; nlinarith [h.1, h.2], end @[simp] lemma arctan_zero : arctan 0 = 0 := by simp [arctan_eq_arcsin] lemma arctan_eq_of_tan_eq {x y : ℝ} (h : tan x = y) (hx : x ∈ Ioo (-(π / 2)) (π / 2)) : arctan y = x := inj_on_tan (arctan_mem_Ioo _) hx (by rw [tan_arctan, h]) @[simp] lemma arctan_one : arctan 1 = π / 4 := arctan_eq_of_tan_eq tan_pi_div_four $ by split; linarith [pi_pos] @[simp] lemma arctan_neg (x : ℝ) : arctan (-x) = - arctan x := by simp [arctan_eq_arcsin, neg_div] lemma arctan_eq_arccos {x : ℝ} (h : 0 ≤ x) : arctan x = arccos ((sqrt (1 + x ^ 2))⁻¹) := begin rw [arctan_eq_arcsin, arccos_eq_arcsin], swap, { exact inv_nonneg.2 (sqrt_nonneg _) }, congr' 1, rw [←sqrt_inv, sq_sqrt, ←one_div, one_sub_div, add_sub_cancel', sqrt_div, sqrt_sq h], all_goals { positivity } end -- The junk values for `arccos` and `sqrt` make this true even for `1 < x`. lemma arccos_eq_arctan {x : ℝ} (h : 0 < x) : arccos x = arctan (sqrt (1 - x ^ 2) / x) := begin rw [arccos, eq_comm], refine arctan_eq_of_tan_eq _ ⟨_, _⟩, { rw [tan_pi_div_two_sub, tan_arcsin, inv_div] }, { linarith only [arcsin_le_pi_div_two x, pi_pos] }, { linarith only [arcsin_pos.2 h] } end @[continuity] lemma continuous_arctan : continuous arctan := continuous_subtype_coe.comp tan_order_iso.to_homeomorph.continuous_inv_fun lemma continuous_at_arctan {x : ℝ} : continuous_at arctan x := continuous_arctan.continuous_at /-- `real.tan` as a `local_homeomorph` between `(-(π / 2), π / 2)` and the whole line. -/ def tan_local_homeomorph : local_homeomorph ℝ ℝ := { to_fun := tan, inv_fun := arctan, source := Ioo (-(π / 2)) (π / 2), target := univ, map_source' := maps_to_univ _ _, map_target' := λ y hy, arctan_mem_Ioo y, left_inv' := λ x hx, arctan_tan hx.1 hx.2, right_inv' := λ y hy, tan_arctan y, open_source := is_open_Ioo, open_target := is_open_univ, continuous_to_fun := continuous_on_tan_Ioo, continuous_inv_fun := continuous_arctan.continuous_on } @[simp] lemma coe_tan_local_homeomorph : ⇑tan_local_homeomorph = tan := rfl @[simp] lemma coe_tan_local_homeomorph_symm : ⇑tan_local_homeomorph.symm = arctan := rfl end real
4db84998c1d6a24c00f0896381af25fa044086d0
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/nat/log.lean
d9c490022e26f00041d139eedfda889dcede22dd
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
1,200
lean
/- Copyright (c) 2020 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.nat.basic import Mathlib.PostPort namespace Mathlib /-! # Natural number logarithm This file defines `log b n`, the logarithm of `n` with base `b`, to be the largest `k` such that `b ^ k ≤ n`. -/ namespace nat /-- `log b n`, is the logarithm of natural number `n` in base `b`. It returns the largest `k : ℕ` such that `b^k ≤ n`, so if `b^k = n`, it returns exactly `k`. -/ def log (b : ℕ) : ℕ → ℕ := sorry theorem pow_le_iff_le_log (x : ℕ) (y : ℕ) {b : ℕ} (hb : 1 < b) (hy : 1 ≤ y) : b ^ x ≤ y ↔ x ≤ log b y := sorry theorem log_pow (b : ℕ) (x : ℕ) (hb : 1 < b) : log b (b ^ x) = x := sorry theorem pow_succ_log_gt_self (b : ℕ) (x : ℕ) (hb : 1 < b) (hy : 1 ≤ x) : x < b ^ Nat.succ (log b x) := sorry theorem pow_log_le_self (b : ℕ) (x : ℕ) (hb : 1 < b) (hx : 1 ≤ x) : b ^ log b x ≤ x := eq.mpr (id (Eq._oldrec (Eq.refl (b ^ log b x ≤ x)) (propext (pow_le_iff_le_log (log b x) x hb hx)))) (le_refl (log b x))
2a2f82fac7e69a97eb9254686c264a5afaf7793f
cf39355caa609c0f33405126beee2739aa3cb77e
/library/init/control/combinators.lean
ce5e04079afe36b8f1db7d538c90985ac42aae61
[ "Apache-2.0" ]
permissive
leanprover-community/lean
12b87f69d92e614daea8bcc9d4de9a9ace089d0e
cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0
refs/heads/master
1,687,508,156,644
1,684,951,104,000
1,684,951,104,000
169,960,991
457
107
Apache-2.0
1,686,744,372,000
1,549,790,268,000
C++
UTF-8
Lean
false
false
2,686
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura Monad combinators, as in Haskell's Control.Monad. -/ prelude import init.control.monad init.control.alternative init.data.list.basic universes u v w def list.mmap {m : Type u → Type v} [monad m] {α : Type w} {β : Type u} (f : α → m β) : list α → m (list β) | [] := return [] | (h :: t) := do h' ← f h, t' ← list.mmap t, return (h' :: t') def list.mmap' {m : Type → Type v} [monad m] {α : Type u} {β : Type} (f : α → m β) : list α → m unit | [] := return () | (h :: t) := f h >> list.mmap' t def mjoin {m : Type u → Type u} [monad m] {α : Type u} (a : m (m α)) : m α := bind a id def list.mfilter {m : Type → Type v} [monad m] {α : Type} (f : α → m bool) : list α → m (list α) | [] := return [] | (h :: t) := do b ← f h, t' ← list.mfilter t, cond b (return (h :: t')) (return t') def list.mfoldl {m : Type u → Type v} [monad m] {s : Type u} {α : Type w} : (s → α → m s) → s → list α → m s | f s [] := return s | f s (h :: r) := do s' ← f s h, list.mfoldl f s' r def list.mfoldr {m : Type u → Type v} [monad m] {s : Type u} {α : Type w} : (α → s → m s) → s → list α → m s | f s [] := return s | f s (h :: r) := do s' ← list.mfoldr f s r, f h s' def list.mfirst {m : Type u → Type v} [monad m] [alternative m] {α : Type w} {β : Type u} (f : α → m β) : list α → m β | [] := failure | (a::as) := f a <|> list.mfirst as def when {m : Type → Type} [monad m] (c : Prop) [h : decidable c] (t : m unit) : m unit := ite c t (pure ()) def mcond {m : Type → Type} [monad m] {α : Type} (mbool : m bool) (tm fm : m α) : m α := do b ← mbool, cond b tm fm def mwhen {m : Type → Type} [monad m] (c : m bool) (t : m unit) : m unit := mcond c t (return ()) export list (mmap mmap' mfilter mfoldl) namespace monad def mapm := @mmap def mapm' := @mmap' def join := @mjoin def filter := @mfilter def foldl := @mfoldl def cond := @mcond def sequence {m : Type u → Type v} [monad m] {α : Type u} : list (m α) → m (list α) | [] := return [] | (h :: t) := do h' ← h, t' ← sequence t, return (h' :: t') def sequence' {m : Type → Type u} [monad m] {α : Type} : list (m α) → m unit | [] := return () | (h :: t) := h >> sequence' t def whenb {m : Type → Type} [monad m] (b : bool) (t : m unit) : m unit := _root_.cond b t (return ()) def unlessb {m : Type → Type} [monad m] (b : bool) (t : m unit) : m unit := _root_.cond b (return ()) t end monad
53c35669e8448f799996fe3d051c8e4886d54932
dd0f5513e11c52db157d2fcc8456d9401a6cd9da
/10_Structures_and_Records.org.17.lean
7496d99db40b9ef77b10b0cf916424ce8ab63dce
[]
no_license
cjmazey/lean-tutorial
ba559a49f82aa6c5848b9bf17b7389bf7f4ba645
381f61c9fcac56d01d959ae0fa6e376f2c4e3b34
refs/heads/master
1,610,286,098,832
1,447,124,923,000
1,447,124,923,000
43,082,433
0
0
null
null
null
null
UTF-8
Lean
false
false
546
lean
import standard import data.nat open nat structure point (A : Type) := (x : A) (y : A) (z : A) structure rgb_val := (red : nat) (green : nat) (blue : nat) structure red_green_point (A : Type) extends point A, rgb_val := (no_blue : blue = 0) definition p : point nat := {| point, x := 10, y := 10, z := 20 |} definition r : rgb_val := {| rgb_val, red := 200, green := 50, blue := 0 |} definition rgp : red_green_point nat := {| red_green_point, p, r, no_blue := rfl |} example : point.x rgp = 10 := rfl example : rgb_val.red rgp = 200 := rfl
ed298d97de29043934623453e482ee4741131569
2d787ac9a5ab28a7d164cd13cee8a3cdc1e4680a
/src/my_exercises/01_equality_rewriting.lean
bb15990d7e33ce21102a6f7b90f3b9abb91ce4dd
[ "Apache-2.0" ]
permissive
paulzfm/lean3-tutorials
8c113ec2c9494d401c8a877f094db93e5a631fd6
f41baf1c62c251976ec8dd9ccae9e85ec4c4d336
refs/heads/master
1,679,267,488,532
1,614,782,461,000
1,614,782,461,000
344,157,026
0
0
null
null
null
null
UTF-8
Lean
false
false
5,803
lean
import data.real.basic /- One of the earliest kind of proofs one encounters while learning mathematics is proving by a calculation. It may not sound like a proof, but this is actually using lemmas expressing properties of operations on numbers. It also uses the fundamental property of equality: if two mathematical objects A and B are equal then, in any statement involving A, one can replace A by B. This operation is called rewriting, and the Lean "tactic" for this is `rw`. In the following exercises, we will use the following two lemmas: mul_assoc a b c : a * b * c = a * (b * c) mul_comm a b : a*b = b*a Hence the command rw mul_assoc a b c, will replace a*b*c by a*(b*c) in the current goal. In order to replace backward, we use rw ← mul_assoc a b c, replacing a*(b*c) by a*b*c in the current goal. Of course we don't want to constantly invoke those lemmas, and we will eventually introduce more powerful solutions. -/ example (a b c : ℝ) : (a * b) * c = b * (a * c) := begin rw mul_comm a b, rw mul_assoc b a c, end -- 0001 example (a b c : ℝ) : (c * b) * a = b * (a * c) := begin rw mul_comm c b, rw mul_assoc b c a, rw mul_comm c a, end -- 0002 example (a b c : ℝ) : a * (b * c) = b * (a * c) := begin rw ← mul_assoc a b c, rw mul_comm a b, rw mul_assoc b a c, end /- Now let's return to the preceding example to experiment with what happens if we don't give arguments to mul_assoc or mul_comm. For instance, you can start the next proof with rw ← mul_assoc, Try to figure out what happens. -/ -- 0003 example (a b c : ℝ) : a * (b * c) = b * (a * c) := begin rw ← mul_assoc, rw mul_comm a b, rw mul_assoc, end /- We can also perform rewriting in an assumption of the local context, using for instance rw mul_comm a b at hyp, in order to replace a*b by b*a in assumption hyp. The next example will use a third lemma: two_mul a : 2*a = a + a Also we use the `exact` tactic, which allows to provide a direct proof term. -/ example (a b c d : ℝ) (hyp : c = d*a + b) (hyp' : b = a*d) : c = 2*a*d := begin rw hyp' at hyp, -- rewrite hyp' in hyp rw mul_comm d a at hyp, rw ← two_mul (a*d) at hyp, rw ← mul_assoc 2 a d at hyp, exact hyp, -- Our assumption hyp is now exactly what we have to prove end /- And the next one can use: sub_self x : x - x = 0 -/ -- 0004 example (a b c d : ℝ) (hyp : c = b*a - d) (hyp' : d = a*b) : c = 0 := begin rw hyp' at hyp, rw mul_comm at hyp, rw sub_self at hyp, exact hyp, end /- What is written in the two preceding example is very far away from what we would write on paper. Let's now see how to get a more natural layout. Inside each pair of curly braces below, the goal is to prove equality with the preceding line. -/ example (a b c d : ℝ) (hyp : c = d*a + b) (hyp' : b = a*d) : c = 2*a*d := begin calc c = d*a + b : by { rw hyp } ... = d*a + a*d : by { rw hyp' } ... = a*d + a*d : by { rw mul_comm d a } ... = 2*(a*d) : by { rw two_mul } ... = 2*a*d : by { rw mul_assoc }, end /- Let's note there is no comma at the end of each line of calculation. `calc` is really one command, and the comma comes only after it's fully done. From a practical point of view, when writing such a proof, it is convenient to: * pause the tactic state view update in VScode by clicking the Pause icon button in the top right corner of the Lean Goal buffer * write the full calculation, ending each line with ": by {}" * resume tactic state update by clicking the Play icon button and fill in proofs between curly braces. Let's return to the other example using this method. -/ -- 0005 example (a b c d : ℝ) (hyp : c = b*a - d) (hyp' : d = a*b) : c = 0 := begin calc c = b*a - d : by { from hyp } ... = b*a - a*b : by { rw hyp' } ... = a*b - a*b : by { rw mul_comm } ... = 0 : by { rw sub_self }, end /- The preceding proofs have exhausted our supply of "mul_comm" patience. Now it's time to get the computer to work harder. The `ring` tactic will prove any goal that follows by applying only the axioms of commutative (semi-)rings, in particular commutativity and associativity of addition and multiplication, as well as distributivity. We also note that curly braces are not necessary when we write a single tactic proof, so let's get rid of them. -/ example (a b c d : ℝ) (hyp : c = d*a + b) (hyp' : b = a*d) : c = 2*a*d := begin calc c = d*a + b : by rw hyp ... = d*a + a*d : by rw hyp' ... = 2*a*d : by ring, end /- Of course we can use `ring` outside of `calc`. Let's do the next one in one line. -/ -- 0006 example (a b c : ℝ) : a * (b * c) = b * (a * c) := begin ring, end /- This is too much fun. Let's do it again. -/ -- 0007 example (a b : ℝ) : (a + b) + a = 2*a + b := begin ring, end /- Maybe this is cheating. Let's try to do the next computation without ring. We could use: pow_two x : x^2 = x*x mul_sub a b c : a*(b-c) = a*b - a*c add_mul a b c : (a+b)*c = a*c + b*c add_sub a b c : a + (b - c) = (a + b) - c sub_sub a b c : a - b - c = a - (b + c) add_zero a : a + 0 = a -/ -- 0008 example (a b : ℝ) : (a + b)*(a - b) = a^2 - b^2 := begin calc (a + b) * (a - b) = (a + b) * a - (a + b) * b : by { rw mul_sub } ... = (a * a + b * a) - (a * b + b * b) : by { rw add_mul, rw add_mul } ... = (a * a + b * a) - a * b - b * b : by { rw ← sub_sub } ... = a * a + (b * a - a * b) - b * b : by { rw ← add_sub } ... = a * a + (a * b - a * b) - b * b : by { rw mul_comm b a } ... = a * a + 0 - b * b : by { rw sub_self } ... = a * a - b * b : by { rw add_zero} ... = a^2 - b^2 : by { rw pow_two, rw pow_two }, end /- Let's stick to ring in the end. -/
1b64d3a8fe0b4923c5800b697f657920b851867a
b7f22e51856f4989b970961f794f1c435f9b8f78
/library/algebra/ordered_field.lean
43c17a06272bc9bde8ae703fad99ef15a97741bc
[ "Apache-2.0" ]
permissive
soonhokong/lean
cb8aa01055ffe2af0fb99a16b4cda8463b882cd1
38607e3eb57f57f77c0ac114ad169e9e4262e24f
refs/heads/master
1,611,187,284,081
1,450,766,737,000
1,476,122,547,000
11,513,992
2
0
null
1,401,763,102,000
1,374,182,235,000
C++
UTF-8
Lean
false
false
23,559
lean
/- Copyright (c) 2014 Robert Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Lewis -/ import algebra.ordered_ring algebra.field open eq eq.ops structure linear_ordered_field [class] (A : Type) extends linear_ordered_ring A, field A section linear_ordered_field variable {A : Type} variables [s : linear_ordered_field A] {a b c d : A} include s -- helpers for following theorem mul_zero_lt_mul_inv_of_pos (H : 0 < a) : a * 0 < a * (1 / a) := calc a * 0 = 0 : mul_zero ... < 1 : zero_lt_one ... = a * a⁻¹ : mul_inv_cancel (ne.symm (ne_of_lt H)) ... = a * (1 / a) : inv_eq_one_div theorem mul_zero_lt_mul_inv_of_neg (H : a < 0) : a * 0 < a * (1 / a) := calc a * 0 = 0 : mul_zero ... < 1 : zero_lt_one ... = a * a⁻¹ : mul_inv_cancel (ne_of_lt H) ... = a * (1 / a) : inv_eq_one_div theorem one_div_pos_of_pos (H : 0 < a) : 0 < 1 / a := lt_of_mul_lt_mul_left (mul_zero_lt_mul_inv_of_pos H) (le_of_lt H) theorem one_div_neg_of_neg (H : a < 0) : 1 / a < 0 := gt_of_mul_lt_mul_neg_left (mul_zero_lt_mul_inv_of_neg H) (le_of_lt H) theorem le_mul_of_ge_one_right (Hb : b ≥ 0) (H : a ≥ 1) : b ≤ b * a := mul_one _ ▸ (mul_le_mul_of_nonneg_left H Hb) theorem lt_mul_of_gt_one_right (Hb : b > 0) (H : a > 1) : b < b * a := mul_one _ ▸ (mul_lt_mul_of_pos_left H Hb) theorem one_le_div_iff_le (a : A) {b : A} (Hb : b > 0) : 1 ≤ a / b ↔ b ≤ a := have Hb' : b ≠ 0, from ne.symm (ne_of_lt Hb), iff.intro (assume H : 1 ≤ a / b, calc b = b : refl ... ≤ b * (a / b) : le_mul_of_ge_one_right (le_of_lt Hb) H ... = a : mul_div_cancel' Hb') (assume H : b ≤ a, have Hbinv : 1 / b > 0, from one_div_pos_of_pos Hb, calc 1 = b * (1 / b) : mul_one_div_cancel Hb' ... ≤ a * (1 / b) : mul_le_mul_of_nonneg_right H (le_of_lt Hbinv) ... = a / b : div_eq_mul_one_div) theorem le_of_one_le_div (Hb : b > 0) (H : 1 ≤ a / b) : b ≤ a := (iff.mp (!one_le_div_iff_le Hb)) H theorem one_le_div_of_le (Hb : b > 0) (H : b ≤ a) : 1 ≤ a / b := (iff.mpr (!one_le_div_iff_le Hb)) H theorem one_lt_div_iff_lt (a : A) {b : A} (Hb : b > 0) : 1 < a / b ↔ b < a := have Hb' : b ≠ 0, from ne.symm (ne_of_lt Hb), iff.intro (assume H : 1 < a / b, calc b < b * (a / b) : lt_mul_of_gt_one_right Hb H ... = a : mul_div_cancel' Hb') (assume H : b < a, have Hbinv : 1 / b > 0, from one_div_pos_of_pos Hb, calc 1 = b * (1 / b) : mul_one_div_cancel Hb' ... < a * (1 / b) : mul_lt_mul_of_pos_right H Hbinv ... = a / b : div_eq_mul_one_div) theorem lt_of_one_lt_div (Hb : b > 0) (H : 1 < a / b) : b < a := (iff.mp (!one_lt_div_iff_lt Hb)) H theorem one_lt_div_of_lt (Hb : b > 0) (H : b < a) : 1 < a / b := (iff.mpr (!one_lt_div_iff_lt Hb)) H theorem exists_lt (a : A) : ∃ x, x < a := have H : a - 1 < a, from add_lt_of_le_of_neg (le.refl _) zero_gt_neg_one, exists.intro _ H theorem exists_gt (a : A) : ∃ x, x > a := have H : a + 1 > a, from lt_add_of_le_of_pos (le.refl _) zero_lt_one, exists.intro _ H -- the following theorems amount to four iffs, for <, ≤, ≥, >. theorem mul_le_of_le_div (Hc : 0 < c) (H : a ≤ b / c) : a * c ≤ b := !div_mul_cancel (ne.symm (ne_of_lt Hc)) ▸ mul_le_mul_of_nonneg_right H (le_of_lt Hc) theorem le_div_of_mul_le (Hc : 0 < c) (H : a * c ≤ b) : a ≤ b / c := calc a = a * c * (1 / c) : !mul_mul_div (ne.symm (ne_of_lt Hc)) ... ≤ b * (1 / c) : mul_le_mul_of_nonneg_right H (le_of_lt (one_div_pos_of_pos Hc)) ... = b / c : div_eq_mul_one_div theorem mul_lt_of_lt_div (Hc : 0 < c) (H : a < b / c) : a * c < b := !div_mul_cancel (ne.symm (ne_of_lt Hc)) ▸ mul_lt_mul_of_pos_right H Hc theorem lt_div_of_mul_lt (Hc : 0 < c) (H : a * c < b) : a < b / c := calc a = a * c * (1 / c) : !mul_mul_div (ne.symm (ne_of_lt Hc)) ... < b * (1 / c) : mul_lt_mul_of_pos_right H (one_div_pos_of_pos Hc) ... = b / c : div_eq_mul_one_div theorem mul_le_of_div_le_of_neg (Hc : c < 0) (H : b / c ≤ a) : a * c ≤ b := !div_mul_cancel (ne_of_lt Hc) ▸ mul_le_mul_of_nonpos_right H (le_of_lt Hc) theorem div_le_of_mul_le_of_neg (Hc : c < 0) (H : a * c ≤ b) : b / c ≤ a := calc a = a * c * (1 / c) : !mul_mul_div (ne_of_lt Hc) ... ≥ b * (1 / c) : mul_le_mul_of_nonpos_right H (le_of_lt (one_div_neg_of_neg Hc)) ... = b / c : div_eq_mul_one_div theorem mul_lt_of_gt_div_of_neg (Hc : c < 0) (H : a > b / c) : a * c < b := !div_mul_cancel (ne_of_lt Hc) ▸ mul_lt_mul_of_neg_right H Hc theorem div_lt_of_mul_lt_of_pos (Hc : c > 0) (H : b < a * c) : b / c < a := calc a = a * c * (1 / c) : !mul_mul_div (ne_of_gt Hc) ... > b * (1 / c) : mul_lt_mul_of_pos_right H (one_div_pos_of_pos Hc) ... = b / c : div_eq_mul_one_div theorem div_lt_of_mul_gt_of_neg (Hc : c < 0) (H : a * c < b) : b / c < a := calc a = a * c * (1 / c) : !mul_mul_div (ne_of_lt Hc) ... > b * (1 / c) : mul_lt_mul_of_neg_right H (one_div_neg_of_neg Hc) ... = b / c : div_eq_mul_one_div theorem div_le_of_le_mul (Hb : b > 0) (H : a ≤ b * c) : a / b ≤ c := calc a / b = a * (1 / b) : div_eq_mul_one_div ... ≤ (b * c) * (1 / b) : mul_le_mul_of_nonneg_right H (le_of_lt (one_div_pos_of_pos Hb)) ... = (b * c) / b : div_eq_mul_one_div ... = c : mul_div_cancel_left (ne.symm (ne_of_lt Hb)) theorem le_mul_of_div_le (Hc : c > 0) (H : a / c ≤ b) : a ≤ b * c := calc a = a / c * c : !div_mul_cancel (ne.symm (ne_of_lt Hc)) ... ≤ b * c : mul_le_mul_of_nonneg_right H (le_of_lt Hc) -- following these in the isabelle file, there are 8 biconditionals for the above with - signs -- skipping for now theorem mul_sub_mul_div_mul_neg (Hc : c ≠ 0) (Hd : d ≠ 0) (H : a / c < b / d) : (a * d - b * c) / (c * d) < 0 := have H1 : a / c - b / d < 0, from calc a / c - b / d < b / d - b / d : sub_lt_sub_right H ... = 0 : sub_self, calc 0 > a / c - b / d : H1 ... = (a * d - c * b) / (c * d) : !div_sub_div Hc Hd ... = (a * d - b * c) / (c * d) : mul.comm theorem mul_sub_mul_div_mul_nonpos (Hc : c ≠ 0) (Hd : d ≠ 0) (H : a / c ≤ b / d) : (a * d - b * c) / (c * d) ≤ 0 := have H1 : a / c - b / d ≤ 0, from calc a / c - b / d ≤ b / d - b / d : sub_le_sub_right H ... = 0 : sub_self, calc 0 ≥ a / c - b / d : H1 ... = (a * d - c * b) / (c * d) : !div_sub_div Hc Hd ... = (a * d - b * c) / (c * d) : mul.comm theorem div_lt_div_of_mul_sub_mul_div_neg (Hc : c ≠ 0) (Hd : d ≠ 0) (H : (a * d - b * c) / (c * d) < 0) : a / c < b / d := have H1 : (a * d - c * b) / (c * d) < 0, by rewrite [mul.comm c b]; exact H, have H2 : a / c - b / d < 0, by rewrite [!div_sub_div Hc Hd]; exact H1, have H3 : a / c - b / d + b / d < 0 + b / d, from add_lt_add_right H2 _, begin rewrite [zero_add at H3, sub_eq_add_neg at H3, neg_add_cancel_right at H3], exact H3 end theorem div_le_div_of_mul_sub_mul_div_nonpos (Hc : c ≠ 0) (Hd : d ≠ 0) (H : (a * d - b * c) / (c * d) ≤ 0) : a / c ≤ b / d := have H1 : (a * d - c * b) / (c * d) ≤ 0, by rewrite [mul.comm c b]; exact H, have H2 : a / c - b / d ≤ 0, by rewrite [!div_sub_div Hc Hd]; exact H1, have H3 : a / c - b / d + b / d ≤ 0 + b / d, from add_le_add_right H2 _, begin rewrite [zero_add at H3, sub_eq_add_neg at H3, neg_add_cancel_right at H3], exact H3 end theorem div_pos_of_pos_of_pos (Ha : 0 < a) (Hb : 0 < b) : 0 < a / b := begin rewrite div_eq_mul_one_div, apply mul_pos, exact Ha, apply one_div_pos_of_pos, exact Hb end theorem div_nonneg_of_nonneg_of_pos (Ha : 0 ≤ a) (Hb : 0 < b) : 0 ≤ a / b := begin rewrite div_eq_mul_one_div, apply mul_nonneg, exact Ha, apply le_of_lt, apply one_div_pos_of_pos, exact Hb end theorem div_neg_of_neg_of_pos (Ha : a < 0) (Hb : 0 < b) : a / b < 0:= begin rewrite div_eq_mul_one_div, apply mul_neg_of_neg_of_pos, exact Ha, apply one_div_pos_of_pos, exact Hb end theorem div_nonpos_of_nonpos_of_pos (Ha : a ≤ 0) (Hb : 0 < b) : a / b ≤ 0 := begin rewrite div_eq_mul_one_div, apply mul_nonpos_of_nonpos_of_nonneg, exact Ha, apply le_of_lt, apply one_div_pos_of_pos, exact Hb end theorem div_neg_of_pos_of_neg (Ha : 0 < a) (Hb : b < 0) : a / b < 0 := begin rewrite div_eq_mul_one_div, apply mul_neg_of_pos_of_neg, exact Ha, apply one_div_neg_of_neg, exact Hb end theorem div_nonpos_of_nonneg_of_neg (Ha : 0 ≤ a) (Hb : b < 0) : a / b ≤ 0 := begin rewrite div_eq_mul_one_div, apply mul_nonpos_of_nonneg_of_nonpos, exact Ha, apply le_of_lt, apply one_div_neg_of_neg, exact Hb end theorem div_pos_of_neg_of_neg (Ha : a < 0) (Hb : b < 0) : 0 < a / b := begin rewrite div_eq_mul_one_div, apply mul_pos_of_neg_of_neg, exact Ha, apply one_div_neg_of_neg, exact Hb end theorem div_nonneg_of_nonpos_of_neg (Ha : a ≤ 0) (Hb : b < 0) : 0 ≤ a / b := begin rewrite div_eq_mul_one_div, apply mul_nonneg_of_nonpos_of_nonpos, exact Ha, apply le_of_lt, apply one_div_neg_of_neg, exact Hb end theorem div_lt_div_of_lt_of_pos (H : a < b) (Hc : 0 < c) : a / c < b / c := begin rewrite [{a/c}div_eq_mul_one_div, {b/c}div_eq_mul_one_div], exact mul_lt_mul_of_pos_right H (one_div_pos_of_pos Hc) end theorem div_le_div_of_le_of_pos (H : a ≤ b) (Hc : 0 < c) : a / c ≤ b / c := begin rewrite [{a/c}div_eq_mul_one_div, {b/c}div_eq_mul_one_div], exact mul_le_mul_of_nonneg_right H (le_of_lt (one_div_pos_of_pos Hc)) end theorem div_lt_div_of_lt_of_neg (H : b < a) (Hc : c < 0) : a / c < b / c := begin rewrite [{a/c}div_eq_mul_one_div, {b/c}div_eq_mul_one_div], exact mul_lt_mul_of_neg_right H (one_div_neg_of_neg Hc) end theorem div_le_div_of_le_of_neg (H : b ≤ a) (Hc : c < 0) : a / c ≤ b / c := begin rewrite [{a/c}div_eq_mul_one_div, {b/c}div_eq_mul_one_div], exact mul_le_mul_of_nonpos_right H (le_of_lt (one_div_neg_of_neg Hc)) end theorem two_pos : (1 : A) + 1 > 0 := add_pos zero_lt_one zero_lt_one theorem one_add_one_ne_zero : 1 + 1 ≠ (0:A) := ne.symm (ne_of_lt two_pos) theorem two_ne_zero : 2 ≠ (0:A) := by unfold bit0; apply one_add_one_ne_zero theorem add_halves (a : A) : a / 2 + a / 2 = a := calc a / 2 + a / 2 = (a + a) / 2 : by rewrite div_add_div_same ... = (a * 1 + a * 1) / 2 : by rewrite mul_one ... = (a * (1 + 1)) / 2 : by rewrite left_distrib ... = (a * 2) / 2 : by rewrite one_add_one_eq_two ... = a : by rewrite [@mul_div_cancel A _ _ _ two_ne_zero] theorem sub_self_div_two (a : A) : a - a / 2 = a / 2 := by rewrite [-{a}add_halves at {1}, add_sub_cancel] theorem add_midpoint {a b : A} (H : a < b) : a + (b - a) / 2 < b := begin rewrite [-div_sub_div_same, sub_eq_add_neg, {b / 2 + _}add.comm, -add.assoc, -sub_eq_add_neg], apply add_lt_of_lt_sub_right, rewrite *sub_self_div_two, apply div_lt_div_of_lt_of_pos H two_pos end theorem div_two_sub_self (a : A) : a / 2 - a = - (a / 2) := by rewrite [-{a}add_halves at {2}, sub_add_eq_sub_sub, sub_self, zero_sub] theorem add_self_div_two (a : A) : (a + a) / 2 = a := symm (iff.mpr (!eq_div_iff_mul_eq (ne_of_gt (add_pos zero_lt_one zero_lt_one))) (by krewrite [left_distrib, *mul_one])) theorem two_gt_one : (2:A) > 1 := calc (2:A) = 1+1 : one_add_one_eq_two ... > 1+0 : add_lt_add_left zero_lt_one ... = 1 : add_zero theorem two_ge_one : (2:A) ≥ 1 := le_of_lt two_gt_one theorem four_pos : (4 : A) > 0 := add_pos two_pos two_pos theorem mul_le_mul_of_mul_div_le (H : a * (b / c) ≤ d) (Hc : c > 0) : b * a ≤ d * c := begin rewrite [-mul_div_assoc at H, mul.comm b], apply le_mul_of_div_le Hc H end theorem div_two_lt_of_pos (H : a > 0) : a / (1 + 1) < a := have Ha : a / (1 + 1) > 0, from div_pos_of_pos_of_pos H (add_pos zero_lt_one zero_lt_one), calc a / (1 + 1) < a / (1 + 1) + a / (1 + 1) : lt_add_of_pos_left Ha ... = a : add_halves theorem div_mul_le_div_mul_of_div_le_div_pos {e : A} (Hb : b ≠ 0) (Hd : d ≠ 0) (H : a / b ≤ c / d) (He : e > 0) : a / (b * e) ≤ c / (d * e) := begin rewrite [!field.div_mul_eq_div_mul_one_div Hb (ne_of_gt He), !field.div_mul_eq_div_mul_one_div Hd (ne_of_gt He)], apply mul_le_mul_of_nonneg_right H, apply le_of_lt, apply one_div_pos_of_pos He end theorem exists_add_lt_and_pos_of_lt (H : b < a) : ∃ c : A, b + c < a ∧ c > 0 := exists.intro ((a - b) / (1 + 1)) (and.intro (have H2 : a + a > (b + b) + (a - b), from calc a + a > b + a : add_lt_add_right H ... = b + a + b - b : add_sub_cancel ... = b + b + a - b : add.right_comm ... = (b + b) + (a - b) : add_sub, have H3 : (a + a) / 2 > ((b + b) + (a - b)) / 2, from div_lt_div_of_lt_of_pos H2 two_pos, by rewrite [one_add_one_eq_two, sub_eq_add_neg, add_self_div_two at H3, -div_add_div_same at H3, add_self_div_two at H3]; exact H3) (div_pos_of_pos_of_pos (iff.mpr !sub_pos_iff_lt H) two_pos)) theorem ge_of_forall_ge_sub {a b : A} (H : ∀ ε : A, ε > 0 → a ≥ b - ε) : a ≥ b := begin apply le_of_not_gt, intro Hb, cases exists_add_lt_and_pos_of_lt Hb with [c, Hc], let Hc' := H c (and.right Hc), apply (not_le_of_gt (and.left Hc)) (iff.mpr !le_add_iff_sub_right_le Hc') end theorem mul_div_self_add_lt {b c d : A} (Hb : b > 0) (Hc : c > 0) (Hd : d > 0) : c * (b / (c + d)) < b := begin rewrite -mul_div_assoc, apply div_lt_of_mul_lt_of_pos, apply add_pos, repeat assumption, rewrite mul.comm, apply mul_lt_mul_of_pos_left, apply lt_add_of_pos_right, repeat assumption end theorem mul_div_add_self_lt {b c d : A} (Hb : b > 0) (Hc : c > 0) (Hd : d > 0) : d * (b / (c + d)) < b := by rewrite add.comm; apply mul_div_self_add_lt; repeat assumption end linear_ordered_field structure discrete_linear_ordered_field [class] (A : Type) extends linear_ordered_field A, decidable_linear_ordered_comm_ring A := (inv_zero : inv zero = zero) section discrete_linear_ordered_field variable {A : Type} variables [s : discrete_linear_ordered_field A] {a b c : A} include s definition dec_eq_of_dec_lt : ∀ x y : A, decidable (x = y) := take x y, decidable.by_cases (assume H : x < y, decidable.inr (ne_of_lt H)) (assume H : ¬ x < y, decidable.by_cases (assume H' : y < x, decidable.inr (ne.symm (ne_of_lt H'))) (assume H' : ¬ y < x, decidable.inl (le.antisymm (le_of_not_gt H') (le_of_not_gt H)))) definition discrete_linear_ordered_field.to_discrete_field [trans_instance] : discrete_field A := ⦃ discrete_field, s, has_decidable_eq := dec_eq_of_dec_lt⦄ theorem pos_of_one_div_pos (H : 0 < 1 / a) : 0 < a := have H1 : 0 < 1 / (1 / a), from one_div_pos_of_pos H, have H2 : 1 / a ≠ 0, from (assume H3 : 1 / a = 0, have H4 : 1 / (1 / a) = 0, from H3⁻¹ ▸ !div_zero, absurd H4 (ne.symm (ne_of_lt H1))), (division_ring.one_div_one_div (ne_zero_of_one_div_ne_zero H2)) ▸ H1 theorem neg_of_one_div_neg (H : 1 / a < 0) : a < 0 := have H1 : 0 < - (1 / a), from neg_pos_of_neg H, have Ha : a ≠ 0, from ne_zero_of_one_div_ne_zero (ne_of_lt H), have H2 : 0 < 1 / (-a), from (division_ring.one_div_neg_eq_neg_one_div Ha)⁻¹ ▸ H1, have H3 : 0 < -a, from pos_of_one_div_pos H2, neg_of_neg_pos H3 theorem le_of_one_div_le_one_div (H : 0 < a) (Hl : 1 / a ≤ 1 / b) : b ≤ a := have Hb : 0 < b, from pos_of_one_div_pos (calc 0 < 1 / a : one_div_pos_of_pos H ... ≤ 1 / b : Hl), have H' : 1 ≤ a / b, from (calc 1 = a / a : div_self (ne.symm (ne_of_lt H)) ... = a * (1 / a) : div_eq_mul_one_div ... ≤ a * (1 / b) : mul_le_mul_of_nonneg_left Hl (le_of_lt H) ... = a / b : div_eq_mul_one_div ), le_of_one_le_div Hb H' theorem le_of_one_div_le_one_div_of_neg (H : b < 0) (Hl : 1 / a ≤ 1 / b) : b ≤ a := have Ha : a ≠ 0, from ne_of_lt (neg_of_one_div_neg (calc 1 / a ≤ 1 / b : Hl ... < 0 : one_div_neg_of_neg H)), have H' : -b > 0, from neg_pos_of_neg H, have Hl' : - (1 / b) ≤ - (1 / a), from neg_le_neg Hl, have Hl'' : 1 / - b ≤ 1 / - a, from calc 1 / -b = - (1 / b) : by rewrite [division_ring.one_div_neg_eq_neg_one_div (ne_of_lt H)] ... ≤ - (1 / a) : Hl' ... = 1 / -a : by rewrite [division_ring.one_div_neg_eq_neg_one_div Ha], le_of_neg_le_neg (le_of_one_div_le_one_div H' Hl'') theorem lt_of_one_div_lt_one_div (H : 0 < a) (Hl : 1 / a < 1 / b) : b < a := have Hb : 0 < b, from pos_of_one_div_pos (calc 0 < 1 / a : one_div_pos_of_pos H ... < 1 / b : Hl), have H : 1 < a / b, from (calc 1 = a / a : div_self (ne.symm (ne_of_lt H)) ... = a * (1 / a) : div_eq_mul_one_div ... < a * (1 / b) : mul_lt_mul_of_pos_left Hl H ... = a / b : div_eq_mul_one_div), lt_of_one_lt_div Hb H theorem lt_of_one_div_lt_one_div_of_neg (H : b < 0) (Hl : 1 / a < 1 / b) : b < a := have H1 : b ≤ a, from le_of_one_div_le_one_div_of_neg H (le_of_lt Hl), have Hn : b ≠ a, from (assume Hn' : b = a, have Hl' : 1 / a = 1 / b, from Hn' ▸ refl _, absurd Hl' (ne_of_lt Hl)), lt_of_le_of_ne H1 Hn theorem one_div_lt_one_div_of_lt (Ha : 0 < a) (H : a < b) : 1 / b < 1 / a := lt_of_not_ge (assume H', absurd H (not_lt_of_ge (le_of_one_div_le_one_div Ha H'))) theorem one_div_le_one_div_of_le (Ha : 0 < a) (H : a ≤ b) : 1 / b ≤ 1 / a := le_of_not_gt (assume H', absurd H (not_le_of_gt (lt_of_one_div_lt_one_div Ha H'))) theorem one_div_lt_one_div_of_lt_of_neg (Hb : b < 0) (H : a < b) : 1 / b < 1 / a := lt_of_not_ge (assume H', absurd H (not_lt_of_ge (le_of_one_div_le_one_div_of_neg Hb H'))) theorem one_div_le_one_div_of_le_of_neg (Hb : b < 0) (H : a ≤ b) : 1 / b ≤ 1 / a := le_of_not_gt (assume H', absurd H (not_le_of_gt (lt_of_one_div_lt_one_div_of_neg Hb H'))) theorem one_div_le_of_one_div_le_of_pos (Ha : a > 0) (H : 1 / a ≤ b) : 1 / b ≤ a := begin rewrite -(one_div_one_div a), apply one_div_le_one_div_of_le, apply one_div_pos_of_pos, repeat assumption end theorem one_div_le_of_one_div_le_of_neg (Ha : b < 0) (H : 1 / a ≤ b) : 1 / b ≤ a := begin rewrite -(one_div_one_div a), apply one_div_le_one_div_of_le_of_neg, repeat assumption end theorem one_lt_one_div (H1 : 0 < a) (H2 : a < 1) : 1 < 1 / a := one_div_one ▸ one_div_lt_one_div_of_lt H1 H2 theorem one_le_one_div (H1 : 0 < a) (H2 : a ≤ 1) : 1 ≤ 1 / a := one_div_one ▸ one_div_le_one_div_of_le H1 H2 theorem one_div_lt_neg_one (H1 : a < 0) (H2 : -1 < a) : 1 / a < -1 := one_div_neg_one_eq_neg_one ▸ one_div_lt_one_div_of_lt_of_neg H1 H2 theorem one_div_le_neg_one (H1 : a < 0) (H2 : -1 ≤ a) : 1 / a ≤ -1 := one_div_neg_one_eq_neg_one ▸ one_div_le_one_div_of_le_of_neg H1 H2 theorem div_lt_div_of_pos_of_lt_of_pos (Hb : 0 < b) (H : b < a) (Hc : 0 < c) : c / a < c / b := begin apply iff.mp !sub_neg_iff_lt, rewrite [div_eq_mul_one_div, {c / b}div_eq_mul_one_div, -mul_sub_left_distrib], apply mul_neg_of_pos_of_neg, exact Hc, apply iff.mpr !sub_neg_iff_lt, apply one_div_lt_one_div_of_lt, repeat assumption end theorem div_mul_le_div_mul_of_div_le_div_pos' {d e : A} (H : a / b ≤ c / d) (He : e > 0) : a / (b * e) ≤ c / (d * e) := begin rewrite [2 div_mul_eq_div_mul_one_div], apply mul_le_mul_of_nonneg_right H, apply le_of_lt, apply one_div_pos_of_pos He end theorem abs_div (a b : A) : abs (a / b) = abs a / abs b := decidable.by_cases (suppose b = 0, by rewrite [this, abs_zero, *div_zero, abs_zero]) (suppose b ≠ 0, have abs b ≠ 0, from assume H, this (eq_zero_of_abs_eq_zero H), eq_div_of_mul_eq _ _ this (show abs (a / b) * abs b = abs a, by rewrite [-abs_mul, div_mul_cancel _ `b ≠ 0`])) theorem abs_one_div (a : A) : abs (1 / a) = 1 / abs a := by rewrite [abs_div, abs_of_nonneg (zero_le_one : 1 ≥ (0 : A))] theorem sign_eq_div_abs (a : A) : sign a = a / (abs a) := decidable.by_cases (suppose a = 0, by subst a; rewrite [zero_div, sign_zero]) (suppose a ≠ 0, have abs a ≠ 0, from assume H, this (eq_zero_of_abs_eq_zero H), !eq_div_of_mul_eq this !eq_sign_mul_abs⁻¹) theorem add_quarters (a : A) : a / 4 + a / 4 = a / 2 := have H4 : (4 : A) = 2 * 2, by norm_num, calc a / 4 + a / 4 = (a + a) / (2 * 2) : by rewrite [-H4, div_add_div_same] ... = (a * 1 + a * 1) / (2 * 2) : by rewrite mul_one ... = (a * (1 + 1)) / (2 * 2) : by rewrite left_distrib ... = (a * 2) / (2 * 2) : rfl ... = ((a * 2) / 2) / 2 : by rewrite -div_div_eq_div_mul ... = a / 2 : by rewrite (mul_div_cancel a two_ne_zero) lemma div_two_add_div_four_lt {a : A} (H : a > 0) : a / 2 + a / 4 < a := begin replace (4 : A) with (2 : A) + 2, have Hne : (2 + 2 : A) ≠ 0, from ne_of_gt four_pos, krewrite (div_add_div _ _ two_ne_zero Hne), have Hnum : (2 + 2 + 2) / (2 * (2 + 2)) = (3 : A) / 4, by norm_num, rewrite [{2 * a}mul.comm, -left_distrib, mul_div_assoc, -mul_one a at {2}], krewrite Hnum, apply mul_lt_mul_of_pos_left, apply div_lt_of_mul_lt_of_pos, apply four_pos, rewrite one_mul, replace (3 : A) with (2 : A) + 1, replace (4 : A) with (2 : A) + 2, apply add_lt_add_left, apply two_gt_one, exact H end theorem div_nonneg_of_nonneg_of_nonneg {a b : A} (Ha : a ≥ 0) (Hb : b ≥ 0) : a / b ≥ 0 := if Hgt : b > 0 then div_nonneg_of_nonneg_of_pos Ha Hgt else have b = 0, from eq_of_le_of_ge (le_of_not_gt Hgt) Hb, by rewrite [this, div_zero]; apply le.refl theorem div_le_div_of_le_of_nonneg (H : a ≤ b) (Hc : 0 ≤ c) : a / c ≤ b / c := begin cases lt_or_eq_of_le Hc with Hlt Heq, apply div_le_div_of_le_of_pos H Hlt, rewrite [-Heq, 2 div_zero] end theorem div_le_div_of_le_of_nonpos (H : b ≤ a) (Hc : c ≤ 0) : a / c ≤ b / c := begin cases lt_or_eq_of_le Hc with Hlt Heq, apply div_le_div_of_le_of_neg H Hlt, rewrite [Heq, 2 div_zero] end end discrete_linear_ordered_field
cafeec0fd19ba97deb4321d5e50109dd2c012370
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/interactive/codeaction.lean
1ae5a6aa4e29c8362c04ecef879554d9bdba7f20
[ "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
835
lean
import Lean open Lean Server Lsp @[code_action_provider] def helloProvider : CodeActionProvider := fun params _snap => do let td := params.textDocument let edit : TextEdit := { range := params.range, newText := "hello!!!" } let ca : CodeAction := { title := "hello world", kind? := "quickfix", edit? := WorkspaceEdit.ofTextEdit td.uri edit } let longRunner : CodeAction := { title := "a long-running action", kind? := "refactor", } let lazyResult : IO CodeAction := do let v? ← IO.getEnv "PWD" let v := v?.getD "none" return { longRunner with edit? := WorkspaceEdit.ofTextEdit td.uri { range := params.range, newText := v} } return #[ca, {eager := longRunner, lazy? := lazyResult}] theorem asdf : (x : Nat) → x = x := by intro x --^ codeAction rfl
7aefff41d06660dfc360597eb228ef52520143df
853df553b1d6ca524e3f0a79aedd32dde5d27ec3
/src/tactic/ext.lean
a8a4a6c3f6d60f59f8d941433bb69f38cfd64f78
[ "Apache-2.0" ]
permissive
DanielFabian/mathlib
efc3a50b5dde303c59eeb6353ef4c35a345d7112
f520d07eba0c852e96fe26da71d85bf6d40fcc2a
refs/heads/master
1,668,739,922,971
1,595,201,756,000
1,595,201,756,000
279,469,476
0
0
null
1,594,696,604,000
1,594,696,604,000
null
UTF-8
Lean
false
false
14,677
lean
/- Copyright (c) 2018 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon -/ import tactic.rcases import data.sum logic.function.basic universes u₁ u₂ open interactive interactive.types open lean.parser nat tactic /-- `derive_struct_ext_lemma n` generates two extensionality lemmas based on the equality of all non-propositional projections. On the following: ```lean @[ext] structure foo (α : Type*) := (x y : ℕ) (z : {z // z < x}) (k : α) (h : x < y) ``` `derive_struct_lemma` generates: ```lean lemma foo.ext : ∀ {α : Type u_1} (x y : foo α), x.x = y.x → x.y = y.y → x.z == y.z → x.k = y.k → x = y lemma foo.ext_iff : ∀ {α : Type u_1} (x y : foo α), x = y ↔ x.x = y.x ∧ x.y = y.y ∧ x.z == y.z ∧ x.k = y.k ``` -/ meta def derive_struct_ext_lemma (n : name) : tactic name := do e ← get_env, fs ← e.structure_fields n, d ← get_decl n, n ← resolve_constant n, let r := @expr.const tt n $ d.univ_params.map level.param, (args,_) ← infer_type r >>= mk_local_pis, let args := args.map expr.to_implicit_local_const, let t := r.mk_app args, x ← mk_local_def `x t, y ← mk_local_def `y t, let args_x := args ++ [x], let args_y := args ++ [y], bs ← fs.mmap $ λ f, do { d ← get_decl (n ++ f), let a := @expr.const tt (n ++ f) $ d.univ_params.map level.param, t ← infer_type a, s ← infer_type t, if s ≠ `(Prop) then do let x := a.mk_app args_x, let y := a.mk_app args_y, t ← infer_type x, t' ← infer_type y, some <$> if t = t' then mk_app `eq [x,y] >>= mk_local_def `h else mk_mapp `heq [none,x,none,y] >>= mk_local_def `h else pure none }, let bs := bs.filter_map id, eq_t ← mk_app `eq [x,y], t ← pis (args ++ [x,y] ++ bs) eq_t, pr ← run_async $ do { (_,pr) ← solve_aux t (do { args ← intron args.length, x ← intro1, y ← intro1, cases x, cases y, bs.mmap' (λ _, do e ← intro1, cases e), reflexivity }), instantiate_mvars pr }, let decl_n := n <.> "ext", add_decl (declaration.thm decl_n d.univ_params t pr), bs ← bs.mmap infer_type, let rhs := expr.mk_and_lst bs, iff_t ← mk_app `iff [eq_t,rhs], t ← pis (args ++ [x,y]) iff_t, pr ← run_async $ do { (_,pr) ← solve_aux t $ do { args ← intron args.length, x ← intro1, y ← intro1, cases x, cases y, split, solve1 $ do { h ← intro1, hs ← injection h, subst_vars, repeat (refine ``( and.intro _ _ ) >> reflexivity ), done <|> reflexivity }, solve1 $ do { repeat (do refine ``(and_imp.mpr _), h ← intro1, cases h, skip ), h ← intro1, cases h, reflexivity } }, instantiate_mvars pr }, add_decl (declaration.thm (n <.> "ext_iff") d.univ_params t pr), pure decl_n meta def get_ext_subject : expr → tactic name | (expr.pi n bi d b) := do v ← mk_local' n bi d, b' ← whnf $ b.instantiate_var v, get_ext_subject b' | (expr.app _ e) := do t ← infer_type e >>= instantiate_mvars >>= head_beta, if t.get_app_fn.is_constant then pure $ t.get_app_fn.const_name else if t.is_pi then pure $ name.mk_numeral 0 name.anonymous else if t.is_sort then pure $ name.mk_numeral 1 name.anonymous else do t ← pp t, fail format!"only constants and Pi types are supported: {t}" | e := fail format!"Only expressions of the form `_ → _ → ... → R ... e are supported: {e}" open native @[reducible] def ext_param_type := option name ⊕ option name meta def opt_minus : lean.parser (option name → ext_param_type) := sum.inl <$ tk "-" <|> pure sum.inr meta def ext_param := opt_minus <*> ( name.mk_numeral 0 name.anonymous <$ brackets "(" ")" (tk "→" <|> tk "->") <|> none <$ tk "*" <|> some <$> ident ) meta def saturate_fun : name → tactic expr | (name.mk_numeral 0 name.anonymous) := do v₀ ← mk_mvar, v₁ ← mk_mvar, return $ v₀.imp v₁ | (name.mk_numeral 1 name.anonymous) := do u ← mk_meta_univ, pure $ expr.sort u | n := do e ← resolve_constant n >>= mk_const, a ← get_arity e, e.mk_app <$> (list.iota a).mmap (λ _, mk_mvar) meta def equiv_type_constr (n n' : name) : tactic unit := do e ← saturate_fun n, e' ← saturate_fun n', unify e e' <|> fail format!"{n} and {n'} are not definitionally equal types" section performance_hack /-- For performance reasons, it is inadvisable to use `user_attribute.get_param`. The parameter is stored as a reflected expression. When calling `get_param`, the stored parameter is evaluated using `eval_expr`, which first compiles the expression into VM bytecode. The unevaluated expression is available using `user_attribute.get_param_untyped`. In particular, `user_attribute.get_param` MUST NEVER BE USED in the implementation of an attribute cache. This is because calling `eval_expr` disables the attribute cache. There are several possible workarounds: 1. Set a different attribute depending on the parameter. 2. Use your own evaluation function instead of `eval_expr`, such as e.g. `expr.to_nat`. 3. Write your own `has_reflect Param` instance (using a more efficient serialization format). The `user_attribute` code unfortunately checks whether the expression has the correct type, but you can use `` `(id %%e : Param) `` to pretend that your expression `e` has type `Param`. -/ library_note "user attribute parameters" /-! For performance reasons, the parameters of the `@[ext]` attribute are stored in two auxiliary attributes: ```lean attribute [ext [thunk]] funext -- is turned into attribute [_ext_core (@id name @funext)] thunk attribute [_ext_lemma_core] funext ``` see Note [user attribute parameters] -/ local attribute [semireducible] reflected local attribute [instance, priority 9000] private meta def hacky_name_reflect : has_reflect name := λ n, `(id %%(expr.const n []) : name) @[user_attribute] private meta def ext_attr_core : user_attribute (name_map name) name := { name := `_ext_core, descr := "(internal attribute used by ext)", cache_cfg := { dependencies := [], mk_cache := λ ns, do attrs ← ns.mmap (λ n, do ext_l ← ext_attr_core.get_param_untyped n, pure (n, ext_l.app_arg.const_name)), pure $ rb_map.of_list attrs }, parser := failure } end performance_hack /-- Private attribute used to tag extensionality lemmas. -/ @[user_attribute] private meta def ext_lemma_attr_core : user_attribute := { name := `_ext_lemma_core, descr := "(internal attribute used by ext)", parser := failure } /-- Returns the extensionality lemmas in the environment, as a map from structure name to lemma name. -/ meta def get_ext_lemmas : tactic (name_map name) := ext_attr_core.get_cache /-- Returns the extensionality lemmas in the environment, as a list of lemma names. -/ meta def get_ext_lemma_names : tactic (list name) := attribute.get_instances ext_lemma_attr_core.name /-- Tag lemmas of the form: ```lean @[ext] lemma my_collection.ext (a b : my_collection) (h : ∀ x, a.lookup x = b.lookup y) : a = b := ... ``` The attribute indexes extensionality lemma using the type of the objects (i.e. `my_collection`) which it gets from the statement of the lemma. In some cases, the same lemma can be used to state the extensionality of multiple types that are definitionally equivalent. ```lean attribute [ext [(→),thunk,stream]] funext ``` Those parameters are cumulative. The following are equivalent: ```lean attribute [ext [(→),thunk]] funext attribute [ext [stream]] funext ``` and ```lean attribute [ext [(→),thunk,stream]] funext ``` One removes type names from the list for one lemma with: ```lean attribute [ext [-stream,-thunk]] funext ``` Also, the following: ```lean @[ext] lemma my_collection.ext (a b : my_collection) (h : ∀ x, a.lookup x = b.lookup y) : a = b := ... ``` is equivalent to ```lean @[ext *] lemma my_collection.ext (a b : my_collection) (h : ∀ x, a.lookup x = b.lookup y) : a = b := ... ``` This allows us specify type synonyms along with the type that is referred to in the lemma statement. ```lean @[ext [*,my_type_synonym]] lemma my_collection.ext (a b : my_collection) (h : ∀ x, a.lookup x = b.lookup y) : a = b := ... ``` The `ext` attribute can be applied to a structure to generate its extensionality lemmas: ```lean @[ext] structure foo (α : Type*) := (x y : ℕ) (z : {z // z < x}) (k : α) (h : x < y) ``` will generate: ```lean @[ext] lemma foo.ext : ∀ {α : Type u_1} (x y : foo α), x.x = y.x → x.y = y.y → x.z == y.z → x.k = y.k → x = y lemma foo.ext_iff : ∀ {α : Type u_1} (x y : foo α), x = y ↔ x.x = y.x ∧ x.y = y.y ∧ x.z == y.z ∧ x.k = y.k ``` -/ @[user_attribute] meta def extensional_attribute : user_attribute unit (list ext_param_type) := { name := `ext, descr := "lemmas usable by `ext` tactic", parser := pure <$> ext_param <|> list_of ext_param <|> pure [], after_set := some $ λ n prio b, do ls ← extensional_attribute.get_param n, e ← get_env, n ← if (e.structure_fields n).is_some then derive_struct_ext_lemma n else pure n, s ← mk_const n >>= infer_type >>= get_ext_subject, let (rs,ls'') := if ls.empty then ([],[s]) else ls.partition_map (sum.map (flip option.get_or_else s) (flip option.get_or_else s)), ls''.mmap' (equiv_type_constr s), ls' ← get_ext_lemmas, let l := ls'' ∪ (ls'.to_list.filter $ λ l, prod.snd l = n).map prod.fst \ rs, l.mmap' $ λ l, do ext_attr_core.set l n b prio, ext_lemma_attr_core.set n () b prio } add_tactic_doc { name := "ext", category := doc_category.attr, decl_names := [`extensional_attribute], tags := ["rewrite", "logic"] } -- We mark some existing extensionality lemmas. attribute [ext] array.ext propext function.hfunext attribute [ext [(→),thunk]] _root_.funext -- We create some extensionality lemmas for existing structures. attribute [ext] ulift namespace plift -- This is stronger than the one generated automatically. @[ext] lemma ext {P : Prop} (a b : plift P) : a = b := begin cases a, cases b, refl end end plift -- Conservatively, we'll only add extensionality lemmas for `has_*` structures -- as they become useful. attribute [ext] has_zero namespace tactic meta def try_intros : ext_patt → tactic ext_patt | [] := try intros $> [] | (x::xs) := do tgt ← target >>= whnf, if tgt.is_pi then rintro [x] >> try_intros xs else pure (x :: xs) meta def ext1 (xs : ext_patt) (cfg : apply_cfg := {}) : tactic ext_patt := do subject ← target >>= get_ext_subject, m ← get_ext_lemmas, do { rule ← m.find subject, applyc rule cfg } <|> do { ls ← get_ext_lemma_names, ls.any_of (λ n, applyc n cfg) } <|> fail format!"no applicable extensionality rule found for {subject}", try_intros xs meta def ext : ext_patt → option ℕ → tactic unit | _ (some 0) := skip | xs n := focus1 $ do ys ← ext1 xs, try (ext ys (nat.pred <$> n)) local postfix `?`:9001 := optional local postfix *:9001 := many /-- `ext1 id` selects and apply one extensionality lemma (with attribute `ext`), using `id`, if provided, to name a local constant introduced by the lemma. If `id` is omitted, the local constant is named automatically, as per `intro`. -/ meta def interactive.ext1 (xs : parse ext_parse) : tactic unit := ext1 xs $> () /-- - `ext` applies as many extensionality lemmas as possible; - `ext ids`, with `ids` a list of identifiers, finds extentionality and applies them until it runs out of identifiers in `ids` to name the local constants. - `ext` can also be given an `rcases` pattern in place of an identifier. This will destruct the introduced local constant. When trying to prove: ```lean α β : Type, f g : α → set β ⊢ f = g ``` applying `ext x y` yields: ```lean α β : Type, f g : α → set β, x : α, y : β ⊢ y ∈ f x ↔ y ∈ f x ``` by applying functional extensionality and set extensionality. When trying to prove: ```lean α β γ : Type f g : α × β → γ ⊢ f = g ``` applying `ext ⟨a, b⟩` yields: ```lean α β γ : Type, f g : α × β → γ, a : α, b : β ⊢ f (a, b) = g (a, b) ``` by applying functional extensionality and destructing the introduced pair. A maximum depth can be provided with `ext x y z : 3`. -/ meta def interactive.ext : parse ext_parse → parse (tk ":" *> small_nat)? → tactic unit | [] (some n) := iterate_range 1 n (ext1 [] $> ()) | [] none := repeat1 (ext1 [] $> ()) | xs n := tactic.ext xs n /-- * `ext1 id` selects and apply one extensionality lemma (with attribute `ext`), using `id`, if provided, to name a local constant introduced by the lemma. If `id` is omitted, the local constant is named automatically, as per `intro`. * `ext` applies as many extensionality lemmas as possible; * `ext ids`, with `ids` a list of identifiers, finds extensionality lemmas and applies them until it runs out of identifiers in `ids` to name the local constants. * `ext` can also be given an `rcases` pattern in place of an identifier. This will destruct the introduced local constant. When trying to prove: ```lean α β : Type, f g : α → set β ⊢ f = g ``` applying `ext x y` yields: ```lean α β : Type, f g : α → set β, x : α, y : β ⊢ y ∈ f x ↔ y ∈ g x ``` by applying functional extensionality and set extensionality. When trying to prove: ```lean α β γ : Type f g : α × β → γ ⊢ f = g ``` applying `ext ⟨a, b⟩` yields: ```lean α β γ : Type, f g : α × β → γ, a : α, b : β ⊢ f (a, b) = g (a, b) ``` by applying functional extensionality and destructing the introduced pair. A maximum depth can be provided with `ext x y z : 3`. -/ add_tactic_doc { name := "ext1 / ext", category := doc_category.tactic, decl_names := [`tactic.interactive.ext1, `tactic.interactive.ext], tags := ["rewriting", "logic"] } end tactic