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
2436f17442a45e10731dc5da500998e30ee50bda
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/analysis/calculus/lhopital.lean
00712c862df1c7b3f00f9c1346040497ec6d75a1
[]
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
16,640
lean
/- Copyright (c) 2020 Anatole Dedecker. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anatole Dedecker -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.analysis.calculus.mean_value import Mathlib.PostPort namespace Mathlib /-! # L'HΓ΄pital's rule for 0/0 indeterminate forms In this file, we prove several forms of "L'Hopital's rule" for computing 0/0 indeterminate forms. The proof of `has_deriv_at.lhopital_zero_right_on_Ioo` is based on the one given in the corresponding [Wikibooks](https://en.wikibooks.org/wiki/Calculus/L%27H%C3%B4pital%27s_Rule) chapter, and all other statements are derived from this one by composing by carefully chosen functions. Note that the filter `f'/g'` tends to isn't required to be one of `𝓝 a`, `at_top` or `at_bot`. In fact, we give a slightly stronger statement by allowing it to be any filter on `ℝ`. Each statement is available in a `has_deriv_at` form and a `deriv` form, which is denoted by each statement being in either the `has_deriv_at` or the `deriv` namespace. -/ /-! ## Interval-based versions We start by proving statements where all conditions (derivability, `g' β‰  0`) have to be satisfied on an explicitely-provided interval. -/ namespace has_deriv_at theorem lhopital_zero_right_on_Ioo {a : ℝ} {b : ℝ} (hab : a < b) {l : filter ℝ} {f : ℝ β†’ ℝ} {f' : ℝ β†’ ℝ} {g : ℝ β†’ ℝ} {g' : ℝ β†’ ℝ} (hff' : βˆ€ (x : ℝ), x ∈ set.Ioo a b β†’ has_deriv_at f (f' x) x) (hgg' : βˆ€ (x : ℝ), x ∈ set.Ioo a b β†’ has_deriv_at g (g' x) x) (hg' : βˆ€ (x : ℝ), x ∈ set.Ioo a b β†’ g' x β‰  0) (hfa : filter.tendsto f (nhds_within a (set.Ioi a)) (nhds 0)) (hga : filter.tendsto g (nhds_within a (set.Ioi a)) (nhds 0)) (hdiv : filter.tendsto (fun (x : ℝ) => f' x / g' x) (nhds_within a (set.Ioi a)) l) : filter.tendsto (fun (x : ℝ) => f x / g x) (nhds_within a (set.Ioi a)) l := sorry theorem lhopital_zero_right_on_Ico {a : ℝ} {b : ℝ} (hab : a < b) {l : filter ℝ} {f : ℝ β†’ ℝ} {f' : ℝ β†’ ℝ} {g : ℝ β†’ ℝ} {g' : ℝ β†’ ℝ} (hff' : βˆ€ (x : ℝ), x ∈ set.Ioo a b β†’ has_deriv_at f (f' x) x) (hgg' : βˆ€ (x : ℝ), x ∈ set.Ioo a b β†’ has_deriv_at g (g' x) x) (hcf : continuous_on f (set.Ico a b)) (hcg : continuous_on g (set.Ico a b)) (hg' : βˆ€ (x : ℝ), x ∈ set.Ioo a b β†’ g' x β‰  0) (hfa : f a = 0) (hga : g a = 0) (hdiv : filter.tendsto (fun (x : ℝ) => f' x / g' x) (nhds_within a (set.Ioi a)) l) : filter.tendsto (fun (x : ℝ) => f x / g x) (nhds_within a (set.Ioi a)) l := sorry theorem lhopital_zero_left_on_Ioo {a : ℝ} {b : ℝ} (hab : a < b) {l : filter ℝ} {f : ℝ β†’ ℝ} {f' : ℝ β†’ ℝ} {g : ℝ β†’ ℝ} {g' : ℝ β†’ ℝ} (hff' : βˆ€ (x : ℝ), x ∈ set.Ioo a b β†’ has_deriv_at f (f' x) x) (hgg' : βˆ€ (x : ℝ), x ∈ set.Ioo a b β†’ has_deriv_at g (g' x) x) (hg' : βˆ€ (x : ℝ), x ∈ set.Ioo a b β†’ g' x β‰  0) (hfb : filter.tendsto f (nhds_within b (set.Iio b)) (nhds 0)) (hgb : filter.tendsto g (nhds_within b (set.Iio b)) (nhds 0)) (hdiv : filter.tendsto (fun (x : ℝ) => f' x / g' x) (nhds_within b (set.Iio b)) l) : filter.tendsto (fun (x : ℝ) => f x / g x) (nhds_within b (set.Iio b)) l := sorry theorem lhopital_zero_left_on_Ioc {a : ℝ} {b : ℝ} (hab : a < b) {l : filter ℝ} {f : ℝ β†’ ℝ} {f' : ℝ β†’ ℝ} {g : ℝ β†’ ℝ} {g' : ℝ β†’ ℝ} (hff' : βˆ€ (x : ℝ), x ∈ set.Ioo a b β†’ has_deriv_at f (f' x) x) (hgg' : βˆ€ (x : ℝ), x ∈ set.Ioo a b β†’ has_deriv_at g (g' x) x) (hcf : continuous_on f (set.Ioc a b)) (hcg : continuous_on g (set.Ioc a b)) (hg' : βˆ€ (x : ℝ), x ∈ set.Ioo a b β†’ g' x β‰  0) (hfb : f b = 0) (hgb : g b = 0) (hdiv : filter.tendsto (fun (x : ℝ) => f' x / g' x) (nhds_within b (set.Iio b)) l) : filter.tendsto (fun (x : ℝ) => f x / g x) (nhds_within b (set.Iio b)) l := sorry theorem lhopital_zero_at_top_on_Ioi {a : ℝ} {l : filter ℝ} {f : ℝ β†’ ℝ} {f' : ℝ β†’ ℝ} {g : ℝ β†’ ℝ} {g' : ℝ β†’ ℝ} (hff' : βˆ€ (x : ℝ), x ∈ set.Ioi a β†’ has_deriv_at f (f' x) x) (hgg' : βˆ€ (x : ℝ), x ∈ set.Ioi a β†’ has_deriv_at g (g' x) x) (hg' : βˆ€ (x : ℝ), x ∈ set.Ioi a β†’ g' x β‰  0) (hftop : filter.tendsto f filter.at_top (nhds 0)) (hgtop : filter.tendsto g filter.at_top (nhds 0)) (hdiv : filter.tendsto (fun (x : ℝ) => f' x / g' x) filter.at_top l) : filter.tendsto (fun (x : ℝ) => f x / g x) filter.at_top l := sorry theorem lhopital_zero_at_bot_on_Iio {a : ℝ} {l : filter ℝ} {f : ℝ β†’ ℝ} {f' : ℝ β†’ ℝ} {g : ℝ β†’ ℝ} {g' : ℝ β†’ ℝ} (hff' : βˆ€ (x : ℝ), x ∈ set.Iio a β†’ has_deriv_at f (f' x) x) (hgg' : βˆ€ (x : ℝ), x ∈ set.Iio a β†’ has_deriv_at g (g' x) x) (hg' : βˆ€ (x : ℝ), x ∈ set.Iio a β†’ g' x β‰  0) (hfbot : filter.tendsto f filter.at_bot (nhds 0)) (hgbot : filter.tendsto g filter.at_bot (nhds 0)) (hdiv : filter.tendsto (fun (x : ℝ) => f' x / g' x) filter.at_bot l) : filter.tendsto (fun (x : ℝ) => f x / g x) filter.at_bot l := sorry end has_deriv_at namespace deriv theorem lhopital_zero_right_on_Ioo {a : ℝ} {b : ℝ} (hab : a < b) {l : filter ℝ} {f : ℝ β†’ ℝ} {g : ℝ β†’ ℝ} (hdf : differentiable_on ℝ f (set.Ioo a b)) (hg' : βˆ€ (x : ℝ), x ∈ set.Ioo a b β†’ deriv g x β‰  0) (hfa : filter.tendsto f (nhds_within a (set.Ioi a)) (nhds 0)) (hga : filter.tendsto g (nhds_within a (set.Ioi a)) (nhds 0)) (hdiv : filter.tendsto (fun (x : ℝ) => deriv f x / deriv g x) (nhds_within a (set.Ioi a)) l) : filter.tendsto (fun (x : ℝ) => f x / g x) (nhds_within a (set.Ioi a)) l := sorry theorem lhopital_zero_right_on_Ico {a : ℝ} {b : ℝ} (hab : a < b) {l : filter ℝ} {f : ℝ β†’ ℝ} {g : ℝ β†’ ℝ} (hdf : differentiable_on ℝ f (set.Ioo a b)) (hcf : continuous_on f (set.Ico a b)) (hcg : continuous_on g (set.Ico a b)) (hg' : βˆ€ (x : ℝ), x ∈ set.Ioo a b β†’ deriv g x β‰  0) (hfa : f a = 0) (hga : g a = 0) (hdiv : filter.tendsto (fun (x : ℝ) => deriv f x / deriv g x) (nhds_within a (set.Ioi a)) l) : filter.tendsto (fun (x : ℝ) => f x / g x) (nhds_within a (set.Ioi a)) l := sorry theorem lhopital_zero_left_on_Ioo {a : ℝ} {b : ℝ} (hab : a < b) {l : filter ℝ} {f : ℝ β†’ ℝ} {g : ℝ β†’ ℝ} (hdf : differentiable_on ℝ f (set.Ioo a b)) (hg' : βˆ€ (x : ℝ), x ∈ set.Ioo a b β†’ deriv g x β‰  0) (hfb : filter.tendsto f (nhds_within b (set.Iio b)) (nhds 0)) (hgb : filter.tendsto g (nhds_within b (set.Iio b)) (nhds 0)) (hdiv : filter.tendsto (fun (x : ℝ) => deriv f x / deriv g x) (nhds_within b (set.Iio b)) l) : filter.tendsto (fun (x : ℝ) => f x / g x) (nhds_within b (set.Iio b)) l := sorry theorem lhopital_zero_at_top_on_Ioi {a : ℝ} {l : filter ℝ} {f : ℝ β†’ ℝ} {g : ℝ β†’ ℝ} (hdf : differentiable_on ℝ f (set.Ioi a)) (hg' : βˆ€ (x : ℝ), x ∈ set.Ioi a β†’ deriv g x β‰  0) (hftop : filter.tendsto f filter.at_top (nhds 0)) (hgtop : filter.tendsto g filter.at_top (nhds 0)) (hdiv : filter.tendsto (fun (x : ℝ) => deriv f x / deriv g x) filter.at_top l) : filter.tendsto (fun (x : ℝ) => f x / g x) filter.at_top l := sorry theorem lhopital_zero_at_bot_on_Iio {a : ℝ} {l : filter ℝ} {f : ℝ β†’ ℝ} {g : ℝ β†’ ℝ} (hdf : differentiable_on ℝ f (set.Iio a)) (hg' : βˆ€ (x : ℝ), x ∈ set.Iio a β†’ deriv g x β‰  0) (hfbot : filter.tendsto f filter.at_bot (nhds 0)) (hgbot : filter.tendsto g filter.at_bot (nhds 0)) (hdiv : filter.tendsto (fun (x : ℝ) => deriv f x / deriv g x) filter.at_bot l) : filter.tendsto (fun (x : ℝ) => f x / g x) filter.at_bot l := sorry end deriv /-! ## Generic versions The following statements no longer any explicit interval, as they only require conditions holding eventually. -/ namespace has_deriv_at /-- L'HΓ΄pital's rule for approaching a real from the right, `has_deriv_at` version -/ theorem lhopital_zero_nhds_right {a : ℝ} {l : filter ℝ} {f : ℝ β†’ ℝ} {f' : ℝ β†’ ℝ} {g : ℝ β†’ ℝ} {g' : ℝ β†’ ℝ} (hff' : filter.eventually (fun (x : ℝ) => has_deriv_at f (f' x) x) (nhds_within a (set.Ioi a))) (hgg' : filter.eventually (fun (x : ℝ) => has_deriv_at g (g' x) x) (nhds_within a (set.Ioi a))) (hg' : filter.eventually (fun (x : ℝ) => g' x β‰  0) (nhds_within a (set.Ioi a))) (hfa : filter.tendsto f (nhds_within a (set.Ioi a)) (nhds 0)) (hga : filter.tendsto g (nhds_within a (set.Ioi a)) (nhds 0)) (hdiv : filter.tendsto (fun (x : ℝ) => f' x / g' x) (nhds_within a (set.Ioi a)) l) : filter.tendsto (fun (x : ℝ) => f x / g x) (nhds_within a (set.Ioi a)) l := sorry /-- L'HΓ΄pital's rule for approaching a real from the left, `has_deriv_at` version -/ theorem lhopital_zero_nhds_left {a : ℝ} {l : filter ℝ} {f : ℝ β†’ ℝ} {f' : ℝ β†’ ℝ} {g : ℝ β†’ ℝ} {g' : ℝ β†’ ℝ} (hff' : filter.eventually (fun (x : ℝ) => has_deriv_at f (f' x) x) (nhds_within a (set.Iio a))) (hgg' : filter.eventually (fun (x : ℝ) => has_deriv_at g (g' x) x) (nhds_within a (set.Iio a))) (hg' : filter.eventually (fun (x : ℝ) => g' x β‰  0) (nhds_within a (set.Iio a))) (hfa : filter.tendsto f (nhds_within a (set.Iio a)) (nhds 0)) (hga : filter.tendsto g (nhds_within a (set.Iio a)) (nhds 0)) (hdiv : filter.tendsto (fun (x : ℝ) => f' x / g' x) (nhds_within a (set.Iio a)) l) : filter.tendsto (fun (x : ℝ) => f x / g x) (nhds_within a (set.Iio a)) l := sorry /-- L'HΓ΄pital's rule for approaching a real, `has_deriv_at` version. This does not require anything about the situation at `a` -/ theorem lhopital_zero_nhds' {a : ℝ} {l : filter ℝ} {f : ℝ β†’ ℝ} {f' : ℝ β†’ ℝ} {g : ℝ β†’ ℝ} {g' : ℝ β†’ ℝ} (hff' : filter.eventually (fun (x : ℝ) => has_deriv_at f (f' x) x) (nhds_within a (set.univ \ singleton a))) (hgg' : filter.eventually (fun (x : ℝ) => has_deriv_at g (g' x) x) (nhds_within a (set.univ \ singleton a))) (hg' : filter.eventually (fun (x : ℝ) => g' x β‰  0) (nhds_within a (set.univ \ singleton a))) (hfa : filter.tendsto f (nhds_within a (set.univ \ singleton a)) (nhds 0)) (hga : filter.tendsto g (nhds_within a (set.univ \ singleton a)) (nhds 0)) (hdiv : filter.tendsto (fun (x : ℝ) => f' x / g' x) (nhds_within a (set.univ \ singleton a)) l) : filter.tendsto (fun (x : ℝ) => f x / g x) (nhds_within a (set.univ \ singleton a)) l := sorry /-- L'HΓ΄pital's rule for approaching a real, `has_deriv_at` version -/ theorem lhopital_zero_nhds {a : ℝ} {l : filter ℝ} {f : ℝ β†’ ℝ} {f' : ℝ β†’ ℝ} {g : ℝ β†’ ℝ} {g' : ℝ β†’ ℝ} (hff' : filter.eventually (fun (x : ℝ) => has_deriv_at f (f' x) x) (nhds a)) (hgg' : filter.eventually (fun (x : ℝ) => has_deriv_at g (g' x) x) (nhds a)) (hg' : filter.eventually (fun (x : ℝ) => g' x β‰  0) (nhds a)) (hfa : filter.tendsto f (nhds a) (nhds 0)) (hga : filter.tendsto g (nhds a) (nhds 0)) (hdiv : filter.tendsto (fun (x : ℝ) => f' x / g' x) (nhds a) l) : filter.tendsto (fun (x : ℝ) => f x / g x) (nhds_within a (set.univ \ singleton a)) l := lhopital_zero_nhds' (eventually_nhds_within_of_eventually_nhds hff') (eventually_nhds_within_of_eventually_nhds hgg') (eventually_nhds_within_of_eventually_nhds hg') (tendsto_nhds_within_of_tendsto_nhds hfa) (tendsto_nhds_within_of_tendsto_nhds hga) (tendsto_nhds_within_of_tendsto_nhds hdiv) /-- L'HΓ΄pital's rule for approaching +∞, `has_deriv_at` version -/ theorem lhopital_zero_at_top {l : filter ℝ} {f : ℝ β†’ ℝ} {f' : ℝ β†’ ℝ} {g : ℝ β†’ ℝ} {g' : ℝ β†’ ℝ} (hff' : filter.eventually (fun (x : ℝ) => has_deriv_at f (f' x) x) filter.at_top) (hgg' : filter.eventually (fun (x : ℝ) => has_deriv_at g (g' x) x) filter.at_top) (hg' : filter.eventually (fun (x : ℝ) => g' x β‰  0) filter.at_top) (hftop : filter.tendsto f filter.at_top (nhds 0)) (hgtop : filter.tendsto g filter.at_top (nhds 0)) (hdiv : filter.tendsto (fun (x : ℝ) => f' x / g' x) filter.at_top l) : filter.tendsto (fun (x : ℝ) => f x / g x) filter.at_top l := sorry /-- L'HΓ΄pital's rule for approaching -∞, `has_deriv_at` version -/ theorem lhopital_zero_at_bot {l : filter ℝ} {f : ℝ β†’ ℝ} {f' : ℝ β†’ ℝ} {g : ℝ β†’ ℝ} {g' : ℝ β†’ ℝ} (hff' : filter.eventually (fun (x : ℝ) => has_deriv_at f (f' x) x) filter.at_bot) (hgg' : filter.eventually (fun (x : ℝ) => has_deriv_at g (g' x) x) filter.at_bot) (hg' : filter.eventually (fun (x : ℝ) => g' x β‰  0) filter.at_bot) (hfbot : filter.tendsto f filter.at_bot (nhds 0)) (hgbot : filter.tendsto g filter.at_bot (nhds 0)) (hdiv : filter.tendsto (fun (x : ℝ) => f' x / g' x) filter.at_bot l) : filter.tendsto (fun (x : ℝ) => f x / g x) filter.at_bot l := sorry end has_deriv_at namespace deriv /-- L'HΓ΄pital's rule for approaching a real from the right, `deriv` version -/ theorem lhopital_zero_nhds_right {a : ℝ} {l : filter ℝ} {f : ℝ β†’ ℝ} {g : ℝ β†’ ℝ} (hdf : filter.eventually (fun (x : ℝ) => differentiable_at ℝ f x) (nhds_within a (set.Ioi a))) (hg' : filter.eventually (fun (x : ℝ) => deriv g x β‰  0) (nhds_within a (set.Ioi a))) (hfa : filter.tendsto f (nhds_within a (set.Ioi a)) (nhds 0)) (hga : filter.tendsto g (nhds_within a (set.Ioi a)) (nhds 0)) (hdiv : filter.tendsto (fun (x : ℝ) => deriv f x / deriv g x) (nhds_within a (set.Ioi a)) l) : filter.tendsto (fun (x : ℝ) => f x / g x) (nhds_within a (set.Ioi a)) l := sorry /-- L'HΓ΄pital's rule for approaching a real from the left, `deriv` version -/ theorem lhopital_zero_nhds_left {a : ℝ} {l : filter ℝ} {f : ℝ β†’ ℝ} {g : ℝ β†’ ℝ} (hdf : filter.eventually (fun (x : ℝ) => differentiable_at ℝ f x) (nhds_within a (set.Iio a))) (hg' : filter.eventually (fun (x : ℝ) => deriv g x β‰  0) (nhds_within a (set.Iio a))) (hfa : filter.tendsto f (nhds_within a (set.Iio a)) (nhds 0)) (hga : filter.tendsto g (nhds_within a (set.Iio a)) (nhds 0)) (hdiv : filter.tendsto (fun (x : ℝ) => deriv f x / deriv g x) (nhds_within a (set.Iio a)) l) : filter.tendsto (fun (x : ℝ) => f x / g x) (nhds_within a (set.Iio a)) l := sorry /-- L'HΓ΄pital's rule for approaching a real, `deriv` version. This does not require anything about the situation at `a` -/ theorem lhopital_zero_nhds' {a : ℝ} {l : filter ℝ} {f : ℝ β†’ ℝ} {g : ℝ β†’ ℝ} (hdf : filter.eventually (fun (x : ℝ) => differentiable_at ℝ f x) (nhds_within a (set.univ \ singleton a))) (hg' : filter.eventually (fun (x : ℝ) => deriv g x β‰  0) (nhds_within a (set.univ \ singleton a))) (hfa : filter.tendsto f (nhds_within a (set.univ \ singleton a)) (nhds 0)) (hga : filter.tendsto g (nhds_within a (set.univ \ singleton a)) (nhds 0)) (hdiv : filter.tendsto (fun (x : ℝ) => deriv f x / deriv g x) (nhds_within a (set.univ \ singleton a)) l) : filter.tendsto (fun (x : ℝ) => f x / g x) (nhds_within a (set.univ \ singleton a)) l := sorry /-- L'HΓ΄pital's rule for approaching a real, `deriv` version -/ theorem lhopital_zero_nhds {a : ℝ} {l : filter ℝ} {f : ℝ β†’ ℝ} {g : ℝ β†’ ℝ} (hdf : filter.eventually (fun (x : ℝ) => differentiable_at ℝ f x) (nhds a)) (hg' : filter.eventually (fun (x : ℝ) => deriv g x β‰  0) (nhds a)) (hfa : filter.tendsto f (nhds a) (nhds 0)) (hga : filter.tendsto g (nhds a) (nhds 0)) (hdiv : filter.tendsto (fun (x : ℝ) => deriv f x / deriv g x) (nhds a) l) : filter.tendsto (fun (x : ℝ) => f x / g x) (nhds_within a (set.univ \ singleton a)) l := lhopital_zero_nhds' (eventually_nhds_within_of_eventually_nhds hdf) (eventually_nhds_within_of_eventually_nhds hg') (tendsto_nhds_within_of_tendsto_nhds hfa) (tendsto_nhds_within_of_tendsto_nhds hga) (tendsto_nhds_within_of_tendsto_nhds hdiv) /-- L'HΓ΄pital's rule for approaching +∞, `deriv` version -/ theorem lhopital_zero_at_top {l : filter ℝ} {f : ℝ β†’ ℝ} {g : ℝ β†’ ℝ} (hdf : filter.eventually (fun (x : ℝ) => differentiable_at ℝ f x) filter.at_top) (hg' : filter.eventually (fun (x : ℝ) => deriv g x β‰  0) filter.at_top) (hftop : filter.tendsto f filter.at_top (nhds 0)) (hgtop : filter.tendsto g filter.at_top (nhds 0)) (hdiv : filter.tendsto (fun (x : ℝ) => deriv f x / deriv g x) filter.at_top l) : filter.tendsto (fun (x : ℝ) => f x / g x) filter.at_top l := sorry /-- L'HΓ΄pital's rule for approaching -∞, `deriv` version -/ theorem lhopital_zero_at_bot {l : filter ℝ} {f : ℝ β†’ ℝ} {g : ℝ β†’ ℝ} (hdf : filter.eventually (fun (x : ℝ) => differentiable_at ℝ f x) filter.at_bot) (hg' : filter.eventually (fun (x : ℝ) => deriv g x β‰  0) filter.at_bot) (hfbot : filter.tendsto f filter.at_bot (nhds 0)) (hgbot : filter.tendsto g filter.at_bot (nhds 0)) (hdiv : filter.tendsto (fun (x : ℝ) => deriv f x / deriv g x) filter.at_bot l) : filter.tendsto (fun (x : ℝ) => f x / g x) filter.at_bot l := sorry
c45cc06cfab770d2603091a3041d2b91676bdefd
8cb37a089cdb4af3af9d8bf1002b417e407a8e9e
/library/init/meta/converter/interactive.lean
11e244ca36615e6357a3a8d1ba3f7344cd0f44d3
[ "Apache-2.0" ]
permissive
kbuzzard/lean
ae3c3db4bb462d750dbf7419b28bafb3ec983ef7
ed1788fd674bb8991acffc8fca585ec746711928
refs/heads/master
1,620,983,366,617
1,618,937,600,000
1,618,937,600,000
359,886,396
1
0
Apache-2.0
1,618,936,987,000
1,618,936,987,000
null
UTF-8
Lean
false
false
6,879
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 Converter monad for building simplifiers. -/ prelude import init.meta.interactive import init.meta.converter.conv namespace conv meta def save_info (p : pos) : conv unit := do s ← tactic.read, tactic.save_info_thunk p (Ξ» _, s.to_format tt) meta def step {Ξ± : Type} (c : conv Ξ±) : conv unit := c >> return () meta def istep {Ξ± : Type} (line0 col0 line col : nat) (c : conv Ξ±) : conv unit := tactic.istep line0 col0 line col c meta def execute (c : conv unit) : tactic unit := c meta def solve1 (c : conv unit) : conv unit := tactic.solve1 $ c >> tactic.try (tactic.any_goals tactic.reflexivity) namespace interactive open lean open lean.parser open interactive open interactive.types open tactic_result meta def itactic : Type := conv unit meta def skip : conv unit := conv.skip meta def whnf : conv unit := conv.whnf meta def dsimp (no_dflt : parse only_flag) (es : parse tactic.simp_arg_list) (attr_names : parse with_ident_list) (cfg : tactic.dsimp_config := {}) : conv unit := do (s, u) ← tactic.mk_simp_set no_dflt attr_names es, conv.dsimp (some s) u cfg meta def trace_lhs : conv unit := lhs >>= tactic.trace meta def change (p : parse texpr) : conv unit := tactic.i_to_expr p >>= conv.change meta def congr : conv unit := conv.congr meta def funext : conv unit := conv.funext private meta def is_relation : conv unit := (lhs >>= tactic.relation_lhs_rhs >> return ()) <|> tactic.fail "current expression is not a relation" meta def to_lhs : conv unit := is_relation >> congr >> tactic.swap >> skip meta def to_rhs : conv unit := is_relation >> congr >> skip meta def done : conv unit := tactic.done meta def find (p : parse parser.pexpr) (c : itactic) : conv unit := do (r, lhs, _) ← tactic.target_lhs_rhs, pat ← tactic.pexpr_to_pattern p, s ← simp_lemmas.mk_default, -- to be able to use congruence lemmas @[congr] -- we have to thread the tactic errors through `ext_simplify_core` manually st ← tactic.read, (found_result, new_lhs, pr) ← tactic.ext_simplify_core (success ff st) -- loop counter {zeta := ff, beta := ff, single_pass := tt, eta := ff, proj := ff, fail_if_unchanged := ff, memoize := ff} s (Ξ» u, return u) (Ξ» found_result s r p e, do found ← tactic.unwrap found_result, guard (not found), matched ← (tactic.match_pattern pat e >> return tt) <|> return ff, guard matched, res ← tactic.capture (c.convert e r), -- If an error occurs in conversion, capture it; `ext_simplify_core` will not -- propagate it. match res with | (success r s') := return (success tt s', r.fst, some r.snd, ff) | (exception f p s') := return (exception f p s', e, none, ff) end) (Ξ» a s r p e, tactic.failed) r lhs, found ← tactic.unwrap found_result, when (not found) $ tactic.fail "find converter failed, pattern was not found", update_lhs new_lhs pr meta def for (p : parse parser.pexpr) (occs : parse (list_of small_nat)) (c : itactic) : conv unit := do (r, lhs, _) ← tactic.target_lhs_rhs, pat ← tactic.pexpr_to_pattern p, s ← simp_lemmas.mk_default, -- to be able to use congruence lemmas @[congr] -- we have to thread the tactic errors through `ext_simplify_core` manually st ← tactic.read, (found_result, new_lhs, pr) ← tactic.ext_simplify_core (success 1 st) -- loop counter, and whether the conversion tactic failed {zeta := ff, beta := ff, single_pass := tt, eta := ff, proj := ff, fail_if_unchanged := ff, memoize := ff} s (Ξ» u, return u) (Ξ» found_result s r p e, do i ← tactic.unwrap found_result, matched ← (tactic.match_pattern pat e >> return tt) <|> return ff, guard matched, if i ∈ occs then do res ← tactic.capture (c.convert e r), -- If an error occurs in conversion, capture it; `ext_simplify_core` will not -- propagate it. match res with | (success r s') := return (success (i+1) s', r.fst, some r.snd, tt) | (exception f p s') := return (exception f p s', e, none, tt) end else do st ← tactic.read, return (success (i+1) st, e, none, tt)) (Ξ» a s r p e, tactic.failed) r lhs, tactic.unwrap found_result, update_lhs new_lhs pr meta def simp (no_dflt : parse only_flag) (hs : parse tactic.simp_arg_list) (attr_names : parse with_ident_list) (cfg : tactic.simp_config_ext := {}) : conv unit := do (s, u) ← tactic.mk_simp_set no_dflt attr_names hs, (r, lhs, rhs) ← tactic.target_lhs_rhs, (new_lhs, pr, lms) ← tactic.simplify s u lhs cfg.to_simp_config r cfg.discharger, update_lhs new_lhs pr, return () meta def guard_lhs (p : parse texpr) : tactic unit := do t ← lhs, tactic.interactive.guard_expr_eq t p section rw open tactic.interactive (rw_rules rw_rule get_rule_eqn_lemmas to_expr') open tactic (rewrite_cfg) private meta def rw_lhs (h : expr) (cfg : rewrite_cfg) : conv unit := do l ← conv.lhs, (new_lhs, prf, _) ← tactic.rewrite h l cfg, update_lhs new_lhs prf private meta def rw_core (rs : list rw_rule) (cfg : rewrite_cfg) : conv unit := rs.mmap' $ Ξ» r, do save_info r.pos, eq_lemmas ← get_rule_eqn_lemmas r, orelse' (do h ← to_expr' r.rule, rw_lhs h {symm := r.symm, ..cfg}) (eq_lemmas.mfirst $ Ξ» n, do e ← tactic.mk_const n, rw_lhs e {symm := r.symm, ..cfg}) (eq_lemmas.empty) meta def rewrite (q : parse rw_rules) (cfg : rewrite_cfg := {}) : conv unit := rw_core q.rules cfg meta def rw (q : parse rw_rules) (cfg : rewrite_cfg := {}) : conv unit := rw_core q.rules cfg end rw end interactive end conv namespace tactic namespace interactive open lean open lean.parser open interactive open interactive.types open tactic local postfix `?`:9001 := optional private meta def conv_at (h_name : name) (c : conv unit) : tactic unit := do h ← get_local h_name, h_type ← infer_type h, (new_h_type, pr) ← c.convert h_type, replace_hyp h new_h_type pr, return () private meta def conv_target (c : conv unit) : tactic unit := do t ← target, (new_t, pr) ← c.convert t, replace_target new_t pr, try tactic.triv, try (tactic.reflexivity reducible) meta def conv (loc : parse (tk "at" *> ident)?) (p : parse (tk "in" *> parser.pexpr)?) (c : conv.interactive.itactic) : tactic unit := do let c := match p with | some p := _root_.conv.interactive.find p c | none := c end, match loc with | some h := conv_at h c | none := conv_target c end end interactive end tactic
3e32fa5e39aa8fdd284e3138982b92fc84a52a86
bb31430994044506fa42fd667e2d556327e18dfe
/src/algebra/continued_fractions/computation/translations.lean
a7eeeac76d51a26fbcd50a91b3b2389d571d49b1
[ "Apache-2.0" ]
permissive
sgouezel/mathlib
0cb4e5335a2ba189fa7af96d83a377f83270e503
00638177efd1b2534fc5269363ebf42a7871df9a
refs/heads/master
1,674,527,483,042
1,673,665,568,000
1,673,665,568,000
119,598,202
0
0
null
1,517,348,647,000
1,517,348,646,000
null
UTF-8
Lean
false
false
8,326
lean
/- Copyright (c) 2020 Kevin Kappelmann. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin Kappelmann -/ import algebra.continued_fractions.computation.basic import algebra.continued_fractions.translations /-! # Basic Translation Lemmas Between Structures Defined for Computing Continued Fractions ## Summary This is a collection of simple lemmas between the different structures used for the computation of continued fractions defined in `algebra.continued_fractions.computation.basic`. The file consists of three sections: 1. Recurrences and inversion lemmas for `int_fract_pair.stream`: these lemmas give us inversion rules and recurrences for the computation of the stream of integer and fractional parts of a value. 2. Translation lemmas for the head term: these lemmas show us that the head term of the computed continued fraction of a value `v` is `⌊vβŒ‹` and how this head term is moved along the structures used in the computation process. 3. Translation lemmas for the sequence: these lemmas show how the sequences of the involved structures (`int_fract_pair.stream`, `int_fract_pair.seq1`, and `generalized_continued_fraction.of`) are connected, i.e. how the values are moved along the structures and the termination of one sequence implies the termination of another sequence. ## Main Theorems - `succ_nth_stream_eq_some_iff` gives as a recurrence to compute the `n + 1`th value of the sequence of integer and fractional parts of a value in case of non-termination. - `succ_nth_stream_eq_none_iff` gives as a recurrence to compute the `n + 1`th value of the sequence of integer and fractional parts of a value in case of termination. - `nth_of_eq_some_of_succ_nth_int_fract_pair_stream` and `nth_of_eq_some_of_nth_int_fract_pair_stream_fr_ne_zero` show how the entries of the sequence of the computed continued fraction can be obtained from the stream of integer and fractional parts. -/ namespace generalized_continued_fraction open generalized_continued_fraction (of) /- Fix a discrete linear ordered floor field and a value `v`. -/ variables {K : Type*} [linear_ordered_field K] [floor_ring K] {v : K} namespace int_fract_pair /-! ### Recurrences and Inversion Lemmas for `int_fract_pair.stream` Here we state some lemmas that give us inversion rules and recurrences for the computation of the stream of integer and fractional parts of a value. -/ variable {n : β„•} lemma stream_eq_none_of_fr_eq_zero {ifp_n : int_fract_pair K} (stream_nth_eq : int_fract_pair.stream v n = some ifp_n) (nth_fr_eq_zero : ifp_n.fr = 0) : int_fract_pair.stream v (n + 1) = none := begin cases ifp_n with _ fr, change fr = 0 at nth_fr_eq_zero, simp [int_fract_pair.stream, stream_nth_eq, nth_fr_eq_zero] end /-- Gives a recurrence to compute the `n + 1`th value of the sequence of integer and fractional parts of a value in case of termination. -/ lemma succ_nth_stream_eq_none_iff : int_fract_pair.stream v (n + 1) = none ↔ (int_fract_pair.stream v n = none ∨ βˆƒ ifp, int_fract_pair.stream v n = some ifp ∧ ifp.fr = 0) := begin rw [int_fract_pair.stream], cases int_fract_pair.stream v n; simp [imp_false] end /-- Gives a recurrence to compute the `n + 1`th value of the sequence of integer and fractional parts of a value in case of non-termination. -/ lemma succ_nth_stream_eq_some_iff {ifp_succ_n : int_fract_pair K} : int_fract_pair.stream v (n + 1) = some ifp_succ_n ↔ βˆƒ (ifp_n : int_fract_pair K), int_fract_pair.stream v n = some ifp_n ∧ ifp_n.fr β‰  0 ∧ int_fract_pair.of ifp_n.fr⁻¹ = ifp_succ_n := by simp [int_fract_pair.stream, ite_eq_iff] lemma exists_succ_nth_stream_of_fr_zero {ifp_succ_n : int_fract_pair K} (stream_succ_nth_eq : int_fract_pair.stream v (n + 1) = some ifp_succ_n) (succ_nth_fr_eq_zero : ifp_succ_n.fr = 0) : βˆƒ ifp_n : int_fract_pair K, int_fract_pair.stream v n = some ifp_n ∧ ifp_n.fr⁻¹ = ⌊ifp_n.frβ»ΒΉβŒ‹ := begin -- get the witness from `succ_nth_stream_eq_some_iff` and prove that it has the additional -- properties rcases (succ_nth_stream_eq_some_iff.mp stream_succ_nth_eq) with ⟨ifp_n, seq_nth_eq, nth_fr_ne_zero, rfl⟩, refine ⟨ifp_n, seq_nth_eq, _⟩, simpa only [int_fract_pair.of, int.fract, sub_eq_zero] using succ_nth_fr_eq_zero end end int_fract_pair section head /-! ### Translation of the Head Term Here we state some lemmas that show us that the head term of the computed continued fraction of a value `v` is `⌊vβŒ‹` and how this head term is moved along the structures used in the computation process. -/ /-- The head term of the sequence with head of `v` is just the integer part of `v`. -/ @[simp] lemma int_fract_pair.seq1_fst_eq_of : (int_fract_pair.seq1 v).fst = int_fract_pair.of v := rfl lemma of_h_eq_int_fract_pair_seq1_fst_b : (of v).h = (int_fract_pair.seq1 v).fst.b := by { cases aux_seq_eq : (int_fract_pair.seq1 v), simp [of, aux_seq_eq] } /-- The head term of the gcf of `v` is `⌊vβŒ‹`. -/ @[simp] lemma of_h_eq_floor : (of v).h = ⌊vβŒ‹ := by simp [of_h_eq_int_fract_pair_seq1_fst_b, int_fract_pair.of] end head section sequence /-! ### Translation of the Sequences Here we state some lemmas that show how the sequences of the involved structures (`int_fract_pair.stream`, `int_fract_pair.seq1`, and `generalized_continued_fraction.of`) are connected, i.e. how the values are moved along the structures and how the termination of one sequence implies the termination of another sequence. -/ variable {n : β„•} lemma int_fract_pair.nth_seq1_eq_succ_nth_stream : (int_fract_pair.seq1 v).snd.nth n = (int_fract_pair.stream v) (n + 1) := rfl section termination /-! #### Translation of the Termination of the Sequences Let's first show how the termination of one sequence implies the termination of another sequence. -/ lemma of_terminated_at_iff_int_fract_pair_seq1_terminated_at : (of v).terminated_at n ↔ (int_fract_pair.seq1 v).snd.terminated_at n := option.map_eq_none lemma of_terminated_at_n_iff_succ_nth_int_fract_pair_stream_eq_none : (of v).terminated_at n ↔ int_fract_pair.stream v (n + 1) = none := by rw [of_terminated_at_iff_int_fract_pair_seq1_terminated_at, seq.terminated_at, int_fract_pair.nth_seq1_eq_succ_nth_stream] end termination section values /-! #### Translation of the Values of the Sequence Now let's show how the values of the sequences correspond to one another. -/ lemma int_fract_pair.exists_succ_nth_stream_of_gcf_of_nth_eq_some {gp_n : pair K} (s_nth_eq : (of v).s.nth n = some gp_n) : βˆƒ (ifp : int_fract_pair K), int_fract_pair.stream v (n + 1) = some ifp ∧ (ifp.b : K) = gp_n.b := begin obtain ⟨ifp, stream_succ_nth_eq, gp_n_eq⟩ : βˆƒ ifp, int_fract_pair.stream v (n + 1) = some ifp ∧ pair.mk 1 (ifp.b : K) = gp_n, by { unfold of int_fract_pair.seq1 at s_nth_eq, rwa [seq.map_tail, seq.nth_tail, seq.map_nth, option.map_eq_some'] at s_nth_eq }, cases gp_n_eq, injection gp_n_eq with _ ifp_b_eq_gp_n_b, existsi ifp, exact ⟨stream_succ_nth_eq, ifp_b_eq_gp_n_b⟩ end /-- Shows how the entries of the sequence of the computed continued fraction can be obtained by the integer parts of the stream of integer and fractional parts. -/ lemma nth_of_eq_some_of_succ_nth_int_fract_pair_stream {ifp_succ_n : int_fract_pair K} (stream_succ_nth_eq : int_fract_pair.stream v (n + 1) = some ifp_succ_n) : (of v).s.nth n = some ⟨1, ifp_succ_n.b⟩ := begin unfold of int_fract_pair.seq1, rw [seq.map_tail, seq.nth_tail, seq.map_nth], simp [seq.nth, stream_succ_nth_eq] end /-- Shows how the entries of the sequence of the computed continued fraction can be obtained by the fractional parts of the stream of integer and fractional parts. -/ lemma nth_of_eq_some_of_nth_int_fract_pair_stream_fr_ne_zero {ifp_n : int_fract_pair K} (stream_nth_eq : int_fract_pair.stream v n = some ifp_n) (nth_fr_ne_zero : ifp_n.fr β‰  0) : (of v).s.nth n = some ⟨1, (int_fract_pair.of ifp_n.fr⁻¹).b⟩ := have int_fract_pair.stream v (n + 1) = some (int_fract_pair.of ifp_n.fr⁻¹), by { cases ifp_n, simp [int_fract_pair.stream, stream_nth_eq, nth_fr_ne_zero] }, nth_of_eq_some_of_succ_nth_int_fract_pair_stream this end values end sequence end generalized_continued_fraction
ad2708bd373d9d0e9df4a80c16bfcb86c43f9cec
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/category_theory/functorial.lean
6a393cc6231e39b17a82ce2cebc6957e96c877d7
[]
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
3,293
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.functor import Mathlib.PostPort universes v₁ vβ‚‚ u₁ uβ‚‚ l u₃ v₃ namespace Mathlib /-! # Unbundled functors, as a typeclass decorating the object-level function. -/ namespace category_theory /-- A unbundled functor. -/ -- Perhaps in the future we could redefine `functor` in terms of this, but that isn't the -- immediate plan. class functorial {C : Type u₁} [category C] {D : Type uβ‚‚} [category D] (F : C β†’ D) where map : {X Y : C} β†’ (X ⟢ Y) β†’ (F X ⟢ F Y) map_id' : autoParam (C β†’ map πŸ™ = πŸ™) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.obviously") (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "obviously") []) map_comp' : autoParam (βˆ€ {X Y Z : C} (f : X ⟢ Y) (g : Y ⟢ Z), map (f ≫ g) = map f ≫ map g) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.obviously") (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "obviously") []) /-- If `F : C β†’ D` (just a function) has `[functorial F]`, we can write `map F f : F X ⟢ F Y` for the action of `F` on a morphism `f : X ⟢ Y`. -/ def map {C : Type u₁} [category C] {D : Type uβ‚‚} [category D] (F : C β†’ D) [functorial F] {X : C} {Y : C} (f : X ⟢ Y) : F X ⟢ F Y := functorial.map f @[simp] theorem map_as_map {C : Type u₁} [category C] {D : Type uβ‚‚} [category D] {F : C β†’ D} [functorial F] {X : C} {Y : C} {f : X ⟢ Y} : functorial.map f = map F f := rfl @[simp] theorem functorial.map_id {C : Type u₁} [category C] {D : Type uβ‚‚} [category D] {F : C β†’ D} [functorial F] {X : C} : map F πŸ™ = πŸ™ := functorial.map_id' X @[simp] theorem functorial.map_comp {C : Type u₁} [category C] {D : Type uβ‚‚} [category D] {F : C β†’ D} [functorial F] {X : C} {Y : C} {Z : C} {f : X ⟢ Y} {g : Y ⟢ Z} : map F (f ≫ g) = map F f ≫ map F g := functorial.map_comp' f g namespace functor /-- Bundle a functorial function as a functor. -/ def of {C : Type u₁} [category C] {D : Type uβ‚‚} [category D] (F : C β†’ D) [I : functorial F] : C β₯€ D := mk F functorial.map end functor protected instance functor.obj.functorial {C : Type u₁} [category C] {D : Type uβ‚‚} [category D] (F : C β₯€ D) : functorial (functor.obj F) := functorial.mk (functor.map F) @[simp] theorem map_functorial_obj {C : Type u₁} [category C] {D : Type uβ‚‚} [category D] (F : C β₯€ D) {X : C} {Y : C} (f : X ⟢ Y) : map (functor.obj F) f = functor.map F f := rfl protected instance functorial_id {C : Type u₁} [category C] : functorial id := functorial.mk fun (X Y : C) (f : X ⟢ Y) => f /-- `G ∘ F` is a functorial if both `F` and `G` are. -/ -- This is no longer viable as an instance in Lean 3.7, -- #lint reports an instance loop -- Will this be a problem? def functorial_comp {C : Type u₁} [category C] {D : Type uβ‚‚} [category D] {E : Type u₃} [category E] (F : C β†’ D) [functorial F] (G : D β†’ E) [functorial G] : functorial (G ∘ F) := functorial.mk (functor.map (functor.of F β‹™ functor.of G))
9f9760bad4208de1926c2d0184c56c77e5c06fcf
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/Lean/Data/Json/Stream.lean
e5edb713834d836bd8f2415d330649e22a2c4e70
[ "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
717
lean
/- Copyright (c) 2020 Marc Huisinga. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Marc Huisinga -/ import Init.System.IO import Lean.Data.Json.Parser import Lean.Data.Json.Printer import Lean.Data.Json.FromToJson namespace IO.FS.Stream open Lean open IO /-- Consumes `nBytes` bytes from the stream, interprets the bytes as a utf-8 string and the string as a valid JSON object. -/ def readJson (h : FS.Stream) (nBytes : Nat) : IO Json := do let bytes ← h.read (USize.ofNat nBytes) let s := String.fromUTF8Unchecked bytes ofExcept (Json.parse s) def writeJson (h : FS.Stream) (j : Json) : IO Unit := do h.putStr j.compress h.flush end IO.FS.Stream
0538c8d44f91f801cb035b9f81efee018e6680ec
d7189ea2ef694124821b033e533f18905b5e87ef
/galois/list/fin_nth.lean
eac9cc4cce3f2b59a3dfae37f2de8c96820a8f55
[ "Apache-2.0" ]
permissive
digama0/lean-protocol-support
eaa7e6f8b8e0d5bbfff1f7f52bfb79a3b11b0f59
cabfa3abedbdd6fdca6e2da6fbbf91a13ed48dda
refs/heads/master
1,625,421,450,627
1,506,035,462,000
1,506,035,462,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,324
lean
/- This defines an operation fin_nth which gets the nth item on a list when the index is guaranteed to reference a valid element. -/ import galois.nat.simplify_le import init.data.fin.ops universe variable u namespace list /-- Return the element in the list at the given index. This is a total version of nth. -/ def fin_nth {Ξ± : Type u} : Ξ  (l : list Ξ±), fin l.length β†’ Ξ± | [] ⟨x, pr⟩ := begin simp [nat.lt_is_succ_le, nat.add_succ, nat.not_succ_le_zero] at pr, contradiction end | (h::r) ⟨0, pr⟩ := h | (h::r) ⟨nat.succ i, p⟩ := let q : i < length r := begin simp [nat.succ_lt_succ_iff, nat.succ_add] at p, exact p, end in fin_nth r ⟨i,q⟩ structure found_member {Ξ± : Type u} (x : Ξ±) (xs : list Ξ±) : Type := (idx : fin xs.length) (lookup_eq : fin_nth _ idx = x) lemma no_member_nil {Ξ± : Type u} (H : fin (@nil Ξ±).length) : false := begin cases H, simp [length] at is_lt, simp [nat.lt_is_succ_le, nat.add_succ, nat.not_succ_le_zero] at is_lt, contradiction end namespace fin -- This is now in the standard library -- https://github.com/leanprover/lean/blob/master/library/init/data/fin/ops.lean#L13-L14 protected def succ {n : β„• } : fin n β†’ fin (nat.succ n) | ⟨a, h⟩ := ⟨nat.succ a, nat.succ_lt_succ h⟩ end fin end list
b0102d3de186b6b2a75c7ea1ee6e9f3838e1e1d4
624f6f2ae8b3b1adc5f8f67a365c51d5126be45a
/stage0/src/Init/Data/HashMap/Basic.lean
97459d3b7c21854531789e7543bb73683e45d171
[ "Apache-2.0" ]
permissive
mhuisi/lean4
28d35a4febc2e251c7f05492e13f3b05d6f9b7af
dda44bc47f3e5d024508060dac2bcb59fd12e4c0
refs/heads/master
1,621,225,489,283
1,585,142,689,000
1,585,142,689,000
250,590,438
0
2
Apache-2.0
1,602,443,220,000
1,585,327,814,000
C
UTF-8
Lean
false
false
7,565
lean
/- Copyright (c) 2018 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura -/ prelude import Init.Data.Array.Basic import Init.Data.AssocList import Init.Data.Option.Basic import Init.Data.Hashable universes u v w def HashMapBucket (Ξ± : Type u) (Ξ² : Type v) := { b : Array (AssocList Ξ± Ξ²) // b.size > 0 } def HashMapBucket.update {Ξ± : Type u} {Ξ² : Type v} (data : HashMapBucket Ξ± Ξ²) (i : USize) (d : AssocList Ξ± Ξ²) (h : i.toNat < data.val.size) : HashMapBucket Ξ± Ξ² := ⟨ data.val.uset i d h, transRelRight Greater (Array.szFSetEq (data.val) ⟨USize.toNat i, h⟩ d) data.property ⟩ structure HashMapImp (Ξ± : Type u) (Ξ² : Type v) := (size : Nat) (buckets : HashMapBucket Ξ± Ξ²) def mkHashMapImp {Ξ± : Type u} {Ξ² : Type v} (nbuckets := 8) : HashMapImp Ξ± Ξ² := let n := if nbuckets = 0 then 8 else nbuckets; { size := 0, buckets := ⟨ mkArray n AssocList.nil, have p₁ : (mkArray n (@AssocList.nil Ξ± Ξ²)).size = n from Array.szMkArrayEq _ _; have pβ‚‚ : n = (if nbuckets = 0 then 8 else nbuckets) from rfl; have p₃ : (if nbuckets = 0 then 8 else nbuckets) > 0 from match nbuckets with | 0 => Nat.zeroLtSucc _ | (Nat.succ x) => Nat.zeroLtSucc _; transRelRight Greater (Eq.trans p₁ pβ‚‚) p₃ ⟩ } namespace HashMapImp variables {Ξ± : Type u} {Ξ² : Type v} def mkIdx {n : Nat} (h : n > 0) (u : USize) : { u : USize // u.toNat < n } := ⟨u %β‚™ n, USize.modnLt _ h⟩ @[inline] def reinsertAux (hashFn : Ξ± β†’ USize) (data : HashMapBucket Ξ± Ξ²) (a : Ξ±) (b : Ξ²) : HashMapBucket Ξ± Ξ² := let ⟨i, h⟩ := mkIdx data.property (hashFn a); data.update i (AssocList.cons a b (data.val.uget i h)) h @[inline] def foldBucketsM {Ξ΄ : Type w} {m : Type w β†’ Type w} [Monad m] (data : HashMapBucket Ξ± Ξ²) (d : Ξ΄) (f : Ξ΄ β†’ Ξ± β†’ Ξ² β†’ m Ξ΄) : m Ξ΄ := data.val.foldlM (fun d b => b.foldlM f d) d @[inline] def foldBuckets {Ξ΄ : Type w} (data : HashMapBucket Ξ± Ξ²) (d : Ξ΄) (f : Ξ΄ β†’ Ξ± β†’ Ξ² β†’ Ξ΄) : Ξ΄ := Id.run $ foldBucketsM data d f @[inline] def foldM {Ξ΄ : Type w} {m : Type w β†’ Type w} [Monad m] (f : Ξ΄ β†’ Ξ± β†’ Ξ² β†’ m Ξ΄) (d : Ξ΄) (h : HashMapImp Ξ± Ξ²) : m Ξ΄ := foldBucketsM h.buckets d f @[inline] def fold {Ξ΄ : Type w} (f : Ξ΄ β†’ Ξ± β†’ Ξ² β†’ Ξ΄) (d : Ξ΄) (m : HashMapImp Ξ± Ξ²) : Ξ΄ := foldBuckets m.buckets d f def findEntry? [HasBeq Ξ±] [Hashable Ξ±] (m : HashMapImp Ξ± Ξ²) (a : Ξ±) : Option (Ξ± Γ— Ξ²) := match m with | ⟨_, buckets⟩ => let ⟨i, h⟩ := mkIdx buckets.property (hash a); (buckets.val.uget i h).findEntry? a def find? [HasBeq Ξ±] [Hashable Ξ±] (m : HashMapImp Ξ± Ξ²) (a : Ξ±) : Option Ξ² := match m with | ⟨_, buckets⟩ => let ⟨i, h⟩ := mkIdx buckets.property (hash a); (buckets.val.uget i h).find? a def contains [HasBeq Ξ±] [Hashable Ξ±] (m : HashMapImp Ξ± Ξ²) (a : Ξ±) : Bool := match m with | ⟨_, buckets⟩ => let ⟨i, h⟩ := mkIdx buckets.property (hash a); (buckets.val.uget i h).contains a -- TODO: remove `partial` by using well-founded recursion partial def moveEntries [Hashable Ξ±] : Nat β†’ Array (AssocList Ξ± Ξ²) β†’ HashMapBucket Ξ± Ξ² β†’ HashMapBucket Ξ± Ξ² | i, source, target => if h : i < source.size then let idx : Fin source.size := ⟨i, h⟩; let es : AssocList Ξ± Ξ² := source.get idx; -- We remove `es` from `source` to make sure we can reuse its memory cells when performing es.foldl let source := source.set idx AssocList.nil; let target := es.foldl (reinsertAux hash) target; moveEntries (i+1) source target else target def expand [Hashable Ξ±] (size : Nat) (buckets : HashMapBucket Ξ± Ξ²) : HashMapImp Ξ± Ξ² := let nbuckets := buckets.val.size * 2; have aux₁ : nbuckets > 0 from Nat.mulPos buckets.property (Nat.zeroLtBit0 Nat.oneNeZero); have auxβ‚‚ : (mkArray nbuckets (@AssocList.nil Ξ± Ξ²)).size = nbuckets from Array.szMkArrayEq _ _; let new_buckets : HashMapBucket Ξ± Ξ² := ⟨mkArray nbuckets AssocList.nil, auxβ‚‚.symm β–Έ auxβ‚βŸ©; { size := size, buckets := moveEntries 0 buckets.val new_buckets } def insert [HasBeq Ξ±] [Hashable Ξ±] (m : HashMapImp Ξ± Ξ²) (a : Ξ±) (b : Ξ²) : HashMapImp Ξ± Ξ² := match m with | ⟨size, buckets⟩ => let ⟨i, h⟩ := mkIdx buckets.property (hash a); let bkt := buckets.val.uget i h; if bkt.contains a then ⟨size, buckets.update i (bkt.replace a b) h⟩ else let size' := size + 1; let buckets' := buckets.update i (AssocList.cons a b bkt) h; if size' ≀ buckets.val.size then { size := size', buckets := buckets' } else expand size' buckets' def erase [HasBeq Ξ±] [Hashable Ξ±] (m : HashMapImp Ξ± Ξ²) (a : Ξ±) : HashMapImp Ξ± Ξ² := match m with | ⟨ size, buckets ⟩ => let ⟨i, h⟩ := mkIdx buckets.property (hash a); let bkt := buckets.val.uget i h; if bkt.contains a then ⟨size - 1, buckets.update i (bkt.erase a) h⟩ else m inductive WellFormed [HasBeq Ξ±] [Hashable Ξ±] : HashMapImp Ξ± Ξ² β†’ Prop | mkWff : βˆ€ n, WellFormed (mkHashMapImp n) | insertWff : βˆ€ m a b, WellFormed m β†’ WellFormed (insert m a b) | eraseWff : βˆ€ m a, WellFormed m β†’ WellFormed (erase m a) end HashMapImp def HashMap (Ξ± : Type u) (Ξ² : Type v) [HasBeq Ξ±] [Hashable Ξ±] := { m : HashMapImp Ξ± Ξ² // m.WellFormed } open HashMapImp def mkHashMap {Ξ± : Type u} {Ξ² : Type v} [HasBeq Ξ±] [Hashable Ξ±] (nbuckets := 8) : HashMap Ξ± Ξ² := ⟨ mkHashMapImp nbuckets, WellFormed.mkWff nbuckets ⟩ namespace HashMap variables {Ξ± : Type u} {Ξ² : Type v} [HasBeq Ξ±] [Hashable Ξ±] instance : Inhabited (HashMap Ξ± Ξ²) := ⟨mkHashMap⟩ instance : HasEmptyc (HashMap Ξ± Ξ²) := ⟨mkHashMap⟩ @[inline] def insert (m : HashMap Ξ± Ξ²) (a : Ξ±) (b : Ξ²) : HashMap Ξ± Ξ² := match m with | ⟨ m, hw ⟩ => ⟨ m.insert a b, WellFormed.insertWff m a b hw ⟩ @[inline] def erase (m : HashMap Ξ± Ξ²) (a : Ξ±) : HashMap Ξ± Ξ² := match m with | ⟨ m, hw ⟩ => ⟨ m.erase a, WellFormed.eraseWff m a hw ⟩ @[inline] def findEntry? (m : HashMap Ξ± Ξ²) (a : Ξ±) : Option (Ξ± Γ— Ξ²) := match m with | ⟨ m, _ ⟩ => m.findEntry? a @[inline] def find? (m : HashMap Ξ± Ξ²) (a : Ξ±) : Option Ξ² := match m with | ⟨ m, _ ⟩ => m.find? a @[inline] def findD (m : HashMap Ξ± Ξ²) (a : Ξ±) (bβ‚€ : Ξ²) : Ξ² := (m.find? a).getD bβ‚€ @[inline] def find! [Inhabited Ξ²] (m : HashMap Ξ± Ξ²) (a : Ξ±) : Ξ² := match m.find? a with | some b => b | none => panic! "key is not in the map" @[inline] def getOp (self : HashMap Ξ± Ξ²) (idx : Ξ±) : Option Ξ² := self.find? idx @[inline] def contains (m : HashMap Ξ± Ξ²) (a : Ξ±) : Bool := match m with | ⟨ m, _ ⟩ => m.contains a @[inline] def foldM {Ξ΄ : Type w} {m : Type w β†’ Type w} [Monad m] (f : Ξ΄ β†’ Ξ± β†’ Ξ² β†’ m Ξ΄) (d : Ξ΄) (h : HashMap Ξ± Ξ²) : m Ξ΄ := match h with | ⟨ h, _ ⟩ => h.foldM f d @[inline] def fold {Ξ΄ : Type w} (f : Ξ΄ β†’ Ξ± β†’ Ξ² β†’ Ξ΄) (d : Ξ΄) (m : HashMap Ξ± Ξ²) : Ξ΄ := match m with | ⟨ m, _ ⟩ => m.fold f d @[inline] def size (m : HashMap Ξ± Ξ²) : Nat := match m with | ⟨ {size := sz, ..}, _ ⟩ => sz @[inline] def isEmpty (m : HashMap Ξ± Ξ²) : Bool := m.size = 0 @[inline] def empty : HashMap Ξ± Ξ² := mkHashMap def toList (m : HashMap Ξ± Ξ²) : List (Ξ± Γ— Ξ²) := m.fold (fun r k v => (k, v)::r) [] def toArray (m : HashMap Ξ± Ξ²) : Array (Ξ± Γ— Ξ²) := m.fold (fun r k v => r.push (k, v)) #[] def numBuckets (m : HashMap Ξ± Ξ²) : Nat := m.val.buckets.val.size end HashMap
a1c64eb4cfd0a0b54e44ec9c32ad629b0c8f4fa1
206422fb9edabf63def0ed2aa3f489150fb09ccb
/src/data/real/nnreal.lean
74f26174aea3b0ecb6cfcf64e80995907fc5ac18
[ "Apache-2.0" ]
permissive
hamdysalah1/mathlib
b915f86b2503feeae268de369f1b16932321f097
95454452f6b3569bf967d35aab8d852b1ddf8017
refs/heads/master
1,677,154,116,545
1,611,797,994,000
1,611,797,994,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
27,601
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.linear_ordered_comm_group_with_zero import algebra.big_operators.ring import data.real.basic import data.indicator_function /-! # Nonnegative real numbers In this file we define `nnreal` (notation: `ℝβ‰₯0`) to be the type of non-negative real numbers, a.k.a. the interval `[0, ∞)`. We also define the following operations and structures on `ℝβ‰₯0`: * the order on `ℝβ‰₯0` is the restriction of the order on `ℝ`; these relations define a conditionally complete linear order with a bottom element, `conditionally_complete_linear_order_bot`; * `a + b` and `a * b` are the restrictions of addition and multiplication of real numbers to `ℝβ‰₯0`; these operations together with `0 = ⟨0, _⟩` and `1 = ⟨1, _⟩` turn `ℝβ‰₯0` into a linear ordered archimedean commutative semifield; we have no typeclass for this in `mathlib` yet, so we define the following instances instead: - `linear_ordered_semiring ℝβ‰₯0`; - `comm_semiring ℝβ‰₯0`; - `canonically_ordered_comm_semiring ℝβ‰₯0`; - `linear_ordered_comm_group_with_zero ℝβ‰₯0`; - `archimedean ℝβ‰₯0`. * `nnreal.of_real x` is defined as `⟨max x 0, _⟩`, i.e. `↑(nnreal.of_real x) = x` when `0 ≀ x` and `↑(nnreal.of_real x) = 0` otherwise. We also define an instance `can_lift ℝ ℝβ‰₯0`. This instance can be used by the `lift` tactic to replace `x : ℝ` and `hx : 0 ≀ x` in the proof context with `x : ℝβ‰₯0` while replacing all occurences of `x` with `↑x`. This tactic also works for a function `f : Ξ± β†’ ℝ` with a hypothesis `hf : βˆ€ x, 0 ≀ f x`. ## Notations This file defines `ℝβ‰₯0` as a localized notation for `nnreal`. -/ noncomputable theory open_locale classical big_operators /-- Nonnegative real numbers. -/ def nnreal := {r : ℝ // 0 ≀ r} localized "notation ` ℝβ‰₯0 ` := nnreal" in nnreal namespace nnreal instance : has_coe ℝβ‰₯0 ℝ := ⟨subtype.val⟩ /- Simp lemma to put back `n.val` into the normal form given by the coercion. -/ @[simp] lemma val_eq_coe (n : ℝβ‰₯0) : n.val = n := rfl instance : can_lift ℝ ℝβ‰₯0 := { coe := coe, cond := Ξ» r, 0 ≀ r, prf := Ξ» x hx, ⟨⟨x, hx⟩, rfl⟩ } protected lemma eq {n m : ℝβ‰₯0} : (n : ℝ) = (m : ℝ) β†’ n = m := subtype.eq protected lemma eq_iff {n m : ℝβ‰₯0} : (n : ℝ) = (m : ℝ) ↔ n = m := iff.intro nnreal.eq (congr_arg coe) lemma ne_iff {x y : ℝβ‰₯0} : (x : ℝ) β‰  (y : ℝ) ↔ x β‰  y := not_iff_not_of_iff $ nnreal.eq_iff /-- Reinterpret a real number `r` as a non-negative real number. Returns `0` if `r < 0`. -/ protected def of_real (r : ℝ) : ℝβ‰₯0 := ⟨max r 0, le_max_right _ _⟩ lemma coe_of_real (r : ℝ) (hr : 0 ≀ r) : (nnreal.of_real r : ℝ) = r := max_eq_left hr lemma le_coe_of_real (r : ℝ) : r ≀ nnreal.of_real r := le_max_left r 0 lemma coe_nonneg (r : ℝβ‰₯0) : (0 : ℝ) ≀ r := r.2 @[norm_cast] theorem coe_mk (a : ℝ) (ha) : ((⟨a, ha⟩ : ℝβ‰₯0) : ℝ) = a := rfl instance : has_zero ℝβ‰₯0 := ⟨⟨0, le_refl 0⟩⟩ instance : has_one ℝβ‰₯0 := ⟨⟨1, zero_le_one⟩⟩ instance : has_add ℝβ‰₯0 := ⟨λa b, ⟨a + b, add_nonneg a.2 b.2⟩⟩ instance : has_sub ℝβ‰₯0 := ⟨λa b, nnreal.of_real (a - b)⟩ instance : has_mul ℝβ‰₯0 := ⟨λa b, ⟨a * b, mul_nonneg a.2 b.2⟩⟩ instance : has_inv ℝβ‰₯0 := ⟨λa, ⟨(a.1)⁻¹, inv_nonneg.2 a.2⟩⟩ instance : has_le ℝβ‰₯0 := ⟨λ r s, (r:ℝ) ≀ s⟩ instance : has_bot ℝβ‰₯0 := ⟨0⟩ instance : inhabited ℝβ‰₯0 := ⟨0⟩ protected lemma injective_coe : function.injective (coe : ℝβ‰₯0 β†’ ℝ) := subtype.coe_injective @[simp, norm_cast] protected lemma coe_eq {r₁ rβ‚‚ : ℝβ‰₯0} : (r₁ : ℝ) = rβ‚‚ ↔ r₁ = rβ‚‚ := nnreal.injective_coe.eq_iff @[simp, norm_cast] protected lemma coe_zero : ((0 : ℝβ‰₯0) : ℝ) = 0 := rfl @[simp, norm_cast] protected lemma coe_one : ((1 : ℝβ‰₯0) : ℝ) = 1 := rfl @[simp, norm_cast] protected lemma coe_add (r₁ rβ‚‚ : ℝβ‰₯0) : ((r₁ + rβ‚‚ : ℝβ‰₯0) : ℝ) = r₁ + rβ‚‚ := rfl @[simp, norm_cast] protected lemma coe_mul (r₁ rβ‚‚ : ℝβ‰₯0) : ((r₁ * rβ‚‚ : ℝβ‰₯0) : ℝ) = r₁ * rβ‚‚ := rfl @[simp, norm_cast] protected lemma coe_inv (r : ℝβ‰₯0) : ((r⁻¹ : ℝβ‰₯0) : ℝ) = r⁻¹ := rfl @[simp, norm_cast] protected lemma coe_bit0 (r : ℝβ‰₯0) : ((bit0 r : ℝβ‰₯0) : ℝ) = bit0 r := rfl @[simp, norm_cast] protected lemma coe_bit1 (r : ℝβ‰₯0) : ((bit1 r : ℝβ‰₯0) : ℝ) = bit1 r := rfl @[simp, norm_cast] protected lemma coe_sub {r₁ rβ‚‚ : ℝβ‰₯0} (h : rβ‚‚ ≀ r₁) : ((r₁ - rβ‚‚ : ℝβ‰₯0) : ℝ) = r₁ - rβ‚‚ := max_eq_left $ le_sub.2 $ by simp [show (rβ‚‚ : ℝ) ≀ r₁, from h] -- TODO: setup semifield! @[simp] protected lemma coe_eq_zero (r : ℝβ‰₯0) : ↑r = (0 : ℝ) ↔ r = 0 := by norm_cast lemma coe_ne_zero {r : ℝβ‰₯0} : (r : ℝ) β‰  0 ↔ r β‰  0 := by norm_cast instance : comm_semiring ℝβ‰₯0 := begin refine { zero := 0, add := (+), one := 1, mul := (*), ..}; { intros; apply nnreal.eq; simp [mul_comm, mul_assoc, add_comm_monoid.add, left_distrib, right_distrib, add_comm_monoid.zero, add_comm, add_left_comm] } end /-- Coercion `ℝβ‰₯0 β†’ ℝ` as a `ring_hom`. -/ def to_real_hom : ℝβ‰₯0 β†’+* ℝ := ⟨coe, nnreal.coe_one, nnreal.coe_mul, nnreal.coe_zero, nnreal.coe_add⟩ @[simp] lemma coe_to_real_hom : ⇑to_real_hom = coe := rfl instance : comm_group_with_zero ℝβ‰₯0 := { exists_pair_ne := ⟨0, 1, assume h, zero_ne_one $ nnreal.eq_iff.2 h⟩, inv_zero := nnreal.eq $ show (0⁻¹ : ℝ) = 0, from inv_zero, mul_inv_cancel := assume x h, nnreal.eq $ mul_inv_cancel $ ne_iff.2 h, .. (by apply_instance : has_inv ℝβ‰₯0), .. (_ : comm_semiring ℝβ‰₯0), .. (_ : semiring ℝβ‰₯0) } @[simp, norm_cast] lemma coe_indicator {Ξ±} (s : set Ξ±) (f : Ξ± β†’ ℝβ‰₯0) (a : Ξ±) : ((s.indicator f a : ℝβ‰₯0) : ℝ) = s.indicator (Ξ» x, f x) a := (to_real_hom : ℝβ‰₯0 β†’+ ℝ).map_indicator _ _ _ @[simp, norm_cast] protected lemma coe_div (r₁ rβ‚‚ : ℝβ‰₯0) : ((r₁ / rβ‚‚ : ℝβ‰₯0) : ℝ) = r₁ / rβ‚‚ := rfl @[simp, norm_cast] lemma coe_pow (r : ℝβ‰₯0) (n : β„•) : ((r^n : ℝβ‰₯0) : ℝ) = r^n := to_real_hom.map_pow r n @[norm_cast] lemma coe_list_sum (l : list ℝβ‰₯0) : ((l.sum : ℝβ‰₯0) : ℝ) = (l.map coe).sum := to_real_hom.map_list_sum l @[norm_cast] lemma coe_list_prod (l : list ℝβ‰₯0) : ((l.prod : ℝβ‰₯0) : ℝ) = (l.map coe).prod := to_real_hom.map_list_prod l @[norm_cast] lemma coe_multiset_sum (s : multiset ℝβ‰₯0) : ((s.sum : ℝβ‰₯0) : ℝ) = (s.map coe).sum := to_real_hom.map_multiset_sum s @[norm_cast] lemma coe_multiset_prod (s : multiset ℝβ‰₯0) : ((s.prod : ℝβ‰₯0) : ℝ) = (s.map coe).prod := to_real_hom.map_multiset_prod s @[norm_cast] lemma coe_sum {Ξ±} {s : finset Ξ±} {f : Ξ± β†’ ℝβ‰₯0} : ↑(βˆ‘ a in s, f a) = βˆ‘ a in s, (f a : ℝ) := to_real_hom.map_sum _ _ lemma of_real_sum_of_nonneg {Ξ±} {s : finset Ξ±} {f : Ξ± β†’ ℝ} (hf : βˆ€ a, a ∈ s β†’ 0 ≀ f a) : nnreal.of_real (βˆ‘ a in s, f a) = βˆ‘ a in s, nnreal.of_real (f a) := begin rw [←nnreal.coe_eq, nnreal.coe_sum, nnreal.coe_of_real _ (finset.sum_nonneg hf)], exact finset.sum_congr rfl (Ξ» x hxs, by rw nnreal.coe_of_real _ (hf x hxs)), end @[norm_cast] lemma coe_prod {Ξ±} {s : finset Ξ±} {f : Ξ± β†’ ℝβ‰₯0} : ↑(∏ a in s, f a) = ∏ a in s, (f a : ℝ) := to_real_hom.map_prod _ _ lemma of_real_prod_of_nonneg {Ξ±} {s : finset Ξ±} {f : Ξ± β†’ ℝ} (hf : βˆ€ a, a ∈ s β†’ 0 ≀ f a) : nnreal.of_real (∏ a in s, f a) = ∏ a in s, nnreal.of_real (f a) := begin rw [←nnreal.coe_eq, nnreal.coe_prod, nnreal.coe_of_real _ (finset.prod_nonneg hf)], exact finset.prod_congr rfl (Ξ» x hxs, by rw nnreal.coe_of_real _ (hf x hxs)), end @[norm_cast] lemma nsmul_coe (r : ℝβ‰₯0) (n : β„•) : ↑(n β€’β„• r) = n β€’β„• (r:ℝ) := to_real_hom.to_add_monoid_hom.map_nsmul _ _ @[simp, norm_cast] protected lemma coe_nat_cast (n : β„•) : (↑(↑n : ℝβ‰₯0) : ℝ) = n := to_real_hom.map_nat_cast n instance : linear_order ℝβ‰₯0 := linear_order.lift (coe : ℝβ‰₯0 β†’ ℝ) nnreal.injective_coe @[simp, norm_cast] protected lemma coe_le_coe {r₁ rβ‚‚ : ℝβ‰₯0} : (r₁ : ℝ) ≀ rβ‚‚ ↔ r₁ ≀ rβ‚‚ := iff.rfl @[simp, norm_cast] protected lemma coe_lt_coe {r₁ rβ‚‚ : ℝβ‰₯0} : (r₁ : ℝ) < rβ‚‚ ↔ r₁ < rβ‚‚ := iff.rfl @[simp, norm_cast] protected lemma coe_pos {r : ℝβ‰₯0} : (0 : ℝ) < r ↔ 0 < r := iff.rfl protected lemma coe_mono : monotone (coe : ℝβ‰₯0 β†’ ℝ) := Ξ» _ _, nnreal.coe_le_coe.2 protected lemma of_real_mono : monotone nnreal.of_real := Ξ» x y h, max_le_max h (le_refl 0) @[simp] lemma of_real_coe {r : ℝβ‰₯0} : nnreal.of_real r = r := nnreal.eq $ max_eq_left r.2 @[simp] lemma mk_coe_nat (n : β„•) : @eq ℝβ‰₯0 (⟨(n : ℝ), n.cast_nonneg⟩ : ℝβ‰₯0) n := nnreal.eq (nnreal.coe_nat_cast n).symm @[simp] lemma of_real_coe_nat (n : β„•) : nnreal.of_real n = n := nnreal.eq $ by simp [coe_of_real] /-- `nnreal.of_real` and `coe : ℝβ‰₯0 β†’ ℝ` form a Galois insertion. -/ protected def gi : galois_insertion nnreal.of_real coe := galois_insertion.monotone_intro nnreal.coe_mono nnreal.of_real_mono le_coe_of_real (Ξ» _, of_real_coe) instance : order_bot ℝβ‰₯0 := { bot := βŠ₯, bot_le := assume ⟨a, h⟩, h, .. nnreal.linear_order } instance : canonically_ordered_add_monoid ℝβ‰₯0 := { add_le_add_left := assume a b h c, @add_le_add_left ℝ _ a b h c, lt_of_add_lt_add_left := assume a b c, @lt_of_add_lt_add_left ℝ _ a b c, le_iff_exists_add := assume ⟨a, ha⟩ ⟨b, hb⟩, iff.intro (assume h : a ≀ b, ⟨⟨b - a, le_sub_iff_add_le.2 $ by simp [h]⟩, nnreal.eq $ show b = a + (b - a), by rw [add_sub_cancel'_right]⟩) (assume ⟨⟨c, hc⟩, eq⟩, eq.symm β–Έ show a ≀ a + c, from (le_add_iff_nonneg_right a).2 hc), ..nnreal.comm_semiring, ..nnreal.order_bot, ..nnreal.linear_order } instance : distrib_lattice ℝβ‰₯0 := by apply_instance instance : semilattice_inf_bot ℝβ‰₯0 := { .. nnreal.order_bot, .. nnreal.distrib_lattice } instance : semilattice_sup_bot ℝβ‰₯0 := { .. nnreal.order_bot, .. nnreal.distrib_lattice } instance : linear_ordered_semiring ℝβ‰₯0 := { add_left_cancel := assume a b c h, nnreal.eq $ @add_left_cancel ℝ _ a b c (nnreal.eq_iff.2 h), add_right_cancel := assume a b c h, nnreal.eq $ @add_right_cancel ℝ _ a b c (nnreal.eq_iff.2 h), le_of_add_le_add_left := assume a b c, @le_of_add_le_add_left ℝ _ a b c, mul_lt_mul_of_pos_left := assume a b c, @mul_lt_mul_of_pos_left ℝ _ a b c, mul_lt_mul_of_pos_right := assume a b c, @mul_lt_mul_of_pos_right ℝ _ a b c, zero_le_one := @zero_le_one ℝ _, exists_pair_ne := ⟨0, 1, ne_of_lt (@zero_lt_one ℝ _ _)⟩, .. nnreal.linear_order, .. nnreal.canonically_ordered_add_monoid, .. nnreal.comm_semiring, } instance : linear_ordered_comm_group_with_zero ℝβ‰₯0 := { mul_le_mul_left := assume a b h c, mul_le_mul (le_refl c) h (zero_le a) (zero_le c), zero_le_one := zero_le 1, .. nnreal.linear_ordered_semiring, .. nnreal.comm_group_with_zero } instance : canonically_ordered_comm_semiring ℝβ‰₯0 := { .. nnreal.canonically_ordered_add_monoid, .. nnreal.comm_semiring, .. (show no_zero_divisors ℝβ‰₯0, by apply_instance), .. nnreal.comm_group_with_zero } instance : densely_ordered ℝβ‰₯0 := ⟨assume a b (h : (a : ℝ) < b), let ⟨c, hac, hcb⟩ := exists_between h in ⟨⟨c, le_trans a.property $ le_of_lt $ hac⟩, hac, hcb⟩⟩ instance : no_top_order ℝβ‰₯0 := ⟨assume a, let ⟨b, hb⟩ := no_top (a:ℝ) in ⟨⟨b, le_trans a.property $ le_of_lt $ hb⟩, hb⟩⟩ lemma bdd_above_coe {s : set ℝβ‰₯0} : bdd_above ((coe : ℝβ‰₯0 β†’ ℝ) '' s) ↔ bdd_above s := iff.intro (assume ⟨b, hb⟩, ⟨nnreal.of_real b, assume ⟨y, hy⟩ hys, show y ≀ max b 0, from le_max_left_of_le $ hb $ set.mem_image_of_mem _ hys⟩) (assume ⟨b, hb⟩, ⟨b, assume y ⟨x, hx, eq⟩, eq β–Έ hb hx⟩) lemma bdd_below_coe (s : set ℝβ‰₯0) : bdd_below ((coe : ℝβ‰₯0 β†’ ℝ) '' s) := ⟨0, assume r ⟨q, _, eq⟩, eq β–Έ q.2⟩ instance : has_Sup ℝβ‰₯0 := ⟨λs, ⟨Sup ((coe : ℝβ‰₯0 β†’ ℝ) '' s), begin cases s.eq_empty_or_nonempty with h h, { simp [h, set.image_empty, real.Sup_empty] }, rcases h with ⟨⟨b, hb⟩, hbs⟩, by_cases h' : bdd_above s, { exact le_cSup_of_le (bdd_above_coe.2 h') (set.mem_image_of_mem _ hbs) hb }, { rw [real.Sup_of_not_bdd_above], rwa [bdd_above_coe] } end⟩⟩ instance : has_Inf ℝβ‰₯0 := ⟨λs, ⟨Inf ((coe : ℝβ‰₯0 β†’ ℝ) '' s), begin cases s.eq_empty_or_nonempty with h h, { simp [h, set.image_empty, real.Inf_empty] }, exact le_cInf (h.image _) (assume r ⟨q, _, eq⟩, eq β–Έ q.2) end⟩⟩ lemma coe_Sup (s : set ℝβ‰₯0) : (↑(Sup s) : ℝ) = Sup ((coe : ℝβ‰₯0 β†’ ℝ) '' s) := rfl lemma coe_Inf (s : set ℝβ‰₯0) : (↑(Inf s) : ℝ) = Inf ((coe : ℝβ‰₯0 β†’ ℝ) '' s) := rfl instance : conditionally_complete_linear_order_bot ℝβ‰₯0 := { Sup := Sup, Inf := Inf, le_cSup := assume s a hs ha, le_cSup (bdd_above_coe.2 hs) (set.mem_image_of_mem _ ha), cSup_le := assume s a hs h,show Sup ((coe : ℝβ‰₯0 β†’ ℝ) '' s) ≀ a, from cSup_le (by simp [hs]) $ assume r ⟨b, hb, eq⟩, eq β–Έ h hb, cInf_le := assume s a _ has, cInf_le (bdd_below_coe s) (set.mem_image_of_mem _ has), le_cInf := assume s a hs h, show (↑a : ℝ) ≀ Inf ((coe : ℝβ‰₯0 β†’ ℝ) '' s), from le_cInf (by simp [hs]) $ assume r ⟨b, hb, eq⟩, eq β–Έ h hb, cSup_empty := nnreal.eq $ by simp [coe_Sup, real.Sup_empty]; refl, decidable_le := begin assume x y, apply classical.dec end, .. nnreal.linear_ordered_semiring, .. lattice_of_linear_order, .. nnreal.order_bot } instance : archimedean ℝβ‰₯0 := ⟨ assume x y pos_y, let ⟨n, hr⟩ := archimedean.arch (x:ℝ) (pos_y : (0 : ℝ) < y) in ⟨n, show (x:ℝ) ≀ (n β€’β„• y : ℝβ‰₯0), by simp [*, -nsmul_eq_mul, nsmul_coe]⟩ ⟩ lemma le_of_forall_pos_le_add {a b : ℝβ‰₯0} (h : βˆ€Ξ΅, 0 < Ξ΅ β†’ a ≀ b + Ξ΅) : a ≀ b := le_of_forall_le_of_dense $ assume x hxb, begin rcases le_iff_exists_add.1 (le_of_lt hxb) with ⟨Ρ, rfl⟩, exact h _ ((lt_add_iff_pos_right b).1 hxb) end lemma lt_iff_exists_rat_btwn (a b : ℝβ‰₯0) : a < b ↔ (βˆƒq:β„š, 0 ≀ q ∧ a < nnreal.of_real q ∧ nnreal.of_real q < b) := iff.intro (assume (h : (↑a:ℝ) < (↑b:ℝ)), let ⟨q, haq, hqb⟩ := exists_rat_btwn h in have 0 ≀ (q : ℝ), from le_trans a.2 $ le_of_lt haq, ⟨q, rat.cast_nonneg.1 this, by simp [coe_of_real _ this, nnreal.coe_lt_coe.symm, haq, hqb]⟩) (assume ⟨q, _, haq, hqb⟩, lt_trans haq hqb) lemma bot_eq_zero : (βŠ₯ : ℝβ‰₯0) = 0 := rfl lemma mul_sup (a b c : ℝβ‰₯0) : a * (b βŠ” c) = (a * b) βŠ” (a * c) := begin cases le_total b c with h h, { simp [sup_eq_max, max_eq_right h, max_eq_right (mul_le_mul_of_nonneg_left h (zero_le a))] }, { simp [sup_eq_max, max_eq_left h, max_eq_left (mul_le_mul_of_nonneg_left h (zero_le a))] }, end lemma mul_finset_sup {Ξ±} {f : Ξ± β†’ ℝβ‰₯0} {s : finset Ξ±} (r : ℝβ‰₯0) : r * s.sup f = s.sup (Ξ»a, r * f a) := begin refine s.induction_on _ _, { simp [bot_eq_zero] }, { assume a s has ih, simp [has, ih, mul_sup], } end @[simp, norm_cast] lemma coe_max (x y : ℝβ‰₯0) : ((max x y : ℝβ‰₯0) : ℝ) = max (x : ℝ) (y : ℝ) := by { delta max, split_ifs; refl } @[simp, norm_cast] lemma coe_min (x y : ℝβ‰₯0) : ((min x y : ℝβ‰₯0) : ℝ) = min (x : ℝ) (y : ℝ) := by { delta min, split_ifs; refl } section of_real @[simp] lemma zero_le_coe {q : ℝβ‰₯0} : 0 ≀ (q : ℝ) := q.2 @[simp] lemma of_real_zero : nnreal.of_real 0 = 0 := by simp [nnreal.of_real]; refl @[simp] lemma of_real_one : nnreal.of_real 1 = 1 := by simp [nnreal.of_real, max_eq_left (zero_le_one : (0 :ℝ) ≀ 1)]; refl @[simp] lemma of_real_pos {r : ℝ} : 0 < nnreal.of_real r ↔ 0 < r := by simp [nnreal.of_real, nnreal.coe_lt_coe.symm, lt_irrefl] @[simp] lemma of_real_eq_zero {r : ℝ} : nnreal.of_real r = 0 ↔ r ≀ 0 := by simpa [-of_real_pos] using (not_iff_not.2 (@of_real_pos r)) lemma of_real_of_nonpos {r : ℝ} : r ≀ 0 β†’ nnreal.of_real r = 0 := of_real_eq_zero.2 @[simp] lemma of_real_le_of_real_iff {r p : ℝ} (hp : 0 ≀ p) : nnreal.of_real r ≀ nnreal.of_real p ↔ r ≀ p := by simp [nnreal.coe_le_coe.symm, nnreal.of_real, hp] @[simp] lemma of_real_lt_of_real_iff' {r p : ℝ} : nnreal.of_real r < nnreal.of_real p ↔ r < p ∧ 0 < p := by simp [nnreal.coe_lt_coe.symm, nnreal.of_real, lt_irrefl] lemma of_real_lt_of_real_iff {r p : ℝ} (h : 0 < p) : nnreal.of_real r < nnreal.of_real p ↔ r < p := of_real_lt_of_real_iff'.trans (and_iff_left h) lemma of_real_lt_of_real_iff_of_nonneg {r p : ℝ} (hr : 0 ≀ r) : nnreal.of_real r < nnreal.of_real p ↔ r < p := of_real_lt_of_real_iff'.trans ⟨and.left, Ξ» h, ⟨h, lt_of_le_of_lt hr h⟩⟩ @[simp] lemma of_real_add {r p : ℝ} (hr : 0 ≀ r) (hp : 0 ≀ p) : nnreal.of_real (r + p) = nnreal.of_real r + nnreal.of_real p := nnreal.eq $ by simp [nnreal.of_real, hr, hp, add_nonneg] lemma of_real_add_of_real {r p : ℝ} (hr : 0 ≀ r) (hp : 0 ≀ p) : nnreal.of_real r + nnreal.of_real p = nnreal.of_real (r + p) := (of_real_add hr hp).symm lemma of_real_le_of_real {r p : ℝ} (h : r ≀ p) : nnreal.of_real r ≀ nnreal.of_real p := nnreal.of_real_mono h lemma of_real_add_le {r p : ℝ} : nnreal.of_real (r + p) ≀ nnreal.of_real r + nnreal.of_real p := nnreal.coe_le_coe.1 $ max_le (add_le_add (le_max_left _ _) (le_max_left _ _)) nnreal.zero_le_coe lemma of_real_le_iff_le_coe {r : ℝ} {p : ℝβ‰₯0} : nnreal.of_real r ≀ p ↔ r ≀ ↑p := nnreal.gi.gc r p lemma le_of_real_iff_coe_le {r : ℝβ‰₯0} {p : ℝ} (hp : 0 ≀ p) : r ≀ nnreal.of_real p ↔ ↑r ≀ p := by rw [← nnreal.coe_le_coe, nnreal.coe_of_real p hp] lemma le_of_real_iff_coe_le' {r : ℝβ‰₯0} {p : ℝ} (hr : 0 < r) : r ≀ nnreal.of_real p ↔ ↑r ≀ p := (le_or_lt 0 p).elim le_of_real_iff_coe_le $ Ξ» hp, by simp only [(hp.trans_le r.coe_nonneg).not_le, of_real_eq_zero.2 hp.le, hr.not_le] lemma of_real_lt_iff_lt_coe {r : ℝ} {p : ℝβ‰₯0} (ha : 0 ≀ r) : nnreal.of_real r < p ↔ r < ↑p := by rw [← nnreal.coe_lt_coe, nnreal.coe_of_real r ha] lemma lt_of_real_iff_coe_lt {r : ℝβ‰₯0} {p : ℝ} : r < nnreal.of_real p ↔ ↑r < p := begin cases le_total 0 p, { rw [← nnreal.coe_lt_coe, nnreal.coe_of_real p h] }, { rw [of_real_eq_zero.2 h], split, intro, have := not_lt_of_le (zero_le r), contradiction, intro rp, have : Β¬(p ≀ 0) := not_le_of_lt (lt_of_le_of_lt (coe_nonneg _) rp), contradiction } end end of_real section mul lemma mul_eq_mul_left {a b c : ℝβ‰₯0} (h : a β‰  0) : (a * b = a * c ↔ b = c) := begin rw [← nnreal.eq_iff, ← nnreal.eq_iff, nnreal.coe_mul, nnreal.coe_mul], split, { exact mul_left_cancel' (mt (@nnreal.eq_iff a 0).1 h) }, { assume h, rw [h] } end lemma of_real_mul {p q : ℝ} (hp : 0 ≀ p) : nnreal.of_real (p * q) = nnreal.of_real p * nnreal.of_real q := begin cases le_total 0 q with hq hq, { apply nnreal.eq, simp [nnreal.of_real, hp, hq, max_eq_left, mul_nonneg] }, { have hpq := mul_nonpos_of_nonneg_of_nonpos hp hq, rw [of_real_eq_zero.2 hq, of_real_eq_zero.2 hpq, mul_zero] } end end mul section sub lemma sub_def {r p : ℝβ‰₯0} : r - p = nnreal.of_real (r - p) := rfl lemma sub_eq_zero {r p : ℝβ‰₯0} (h : r ≀ p) : r - p = 0 := nnreal.eq $ max_eq_right $ sub_le_iff_le_add.2 $ by simpa [nnreal.coe_le_coe] using h @[simp] lemma sub_self {r : ℝβ‰₯0} : r - r = 0 := sub_eq_zero $ le_refl r @[simp] lemma sub_zero {r : ℝβ‰₯0} : r - 0 = r := by rw [sub_def, nnreal.coe_zero, sub_zero, nnreal.of_real_coe] lemma sub_pos {r p : ℝβ‰₯0} : 0 < r - p ↔ p < r := of_real_pos.trans $ sub_pos.trans $ nnreal.coe_lt_coe protected lemma sub_lt_self {r p : ℝβ‰₯0} : 0 < r β†’ 0 < p β†’ r - p < r := assume hr hp, begin cases le_total r p, { rwa [sub_eq_zero h] }, { rw [← nnreal.coe_lt_coe, nnreal.coe_sub h], exact sub_lt_self _ hp } end @[simp] lemma sub_le_iff_le_add {r p q : ℝβ‰₯0} : r - p ≀ q ↔ r ≀ q + p := match le_total p r with | or.inl h := by rw [← nnreal.coe_le_coe, ← nnreal.coe_le_coe, nnreal.coe_sub h, nnreal.coe_add, sub_le_iff_le_add] | or.inr h := have r ≀ p + q, from le_add_right h, by simpa [nnreal.coe_le_coe, nnreal.coe_le_coe, sub_eq_zero h, add_comm] end @[simp] lemma sub_le_self {r p : ℝβ‰₯0} : r - p ≀ r := sub_le_iff_le_add.2 $ le_add_right $ le_refl r lemma add_sub_cancel {r p : ℝβ‰₯0} : (p + r) - r = p := nnreal.eq $ by rw [nnreal.coe_sub, nnreal.coe_add, add_sub_cancel]; exact le_add_left (le_refl _) lemma add_sub_cancel' {r p : ℝβ‰₯0} : (r + p) - r = p := by rw [add_comm, add_sub_cancel] lemma sub_add_eq_max {r p : ℝβ‰₯0} : (r - p) + p = max r p := nnreal.eq $ by rw [sub_def, nnreal.coe_add, coe_max, nnreal.of_real, coe_mk, ← max_add_add_right, zero_add, sub_add_cancel] lemma add_sub_eq_max {r p : ℝβ‰₯0} : p + (r - p) = max p r := by rw [add_comm, sub_add_eq_max, max_comm] @[simp] lemma sub_add_cancel_of_le {a b : ℝβ‰₯0} (h : b ≀ a) : (a - b) + b = a := by rw [sub_add_eq_max, max_eq_left h] lemma sub_sub_cancel_of_le {r p : ℝβ‰₯0} (h : r ≀ p) : p - (p - r) = r := by rw [nnreal.sub_def, nnreal.sub_def, nnreal.coe_of_real _ $ sub_nonneg.2 h, sub_sub_cancel, nnreal.of_real_coe] lemma lt_sub_iff_add_lt {p q r : ℝβ‰₯0} : p < q - r ↔ p + r < q := begin split, { assume H, have : (((q - r) : ℝβ‰₯0) : ℝ) = (q : ℝ) - (r : ℝ) := nnreal.coe_sub (le_of_lt (sub_pos.1 (lt_of_le_of_lt (zero_le _) H))), rwa [← nnreal.coe_lt_coe, this, lt_sub_iff_add_lt, ← nnreal.coe_add] at H }, { assume H, have : r ≀ q := le_trans (le_add_left (le_refl _)) (le_of_lt H), rwa [← nnreal.coe_lt_coe, nnreal.coe_sub this, lt_sub_iff_add_lt, ← nnreal.coe_add] } end lemma sub_lt_iff_lt_add {a b c : ℝβ‰₯0} (h : b ≀ a) : a - b < c ↔ a < b + c := by simp only [←nnreal.coe_lt_coe, nnreal.coe_sub h, nnreal.coe_add, sub_lt_iff_lt_add'] lemma sub_eq_iff_eq_add {a b c : ℝβ‰₯0} (h : b ≀ a) : a - b = c ↔ a = c + b := by rw [←nnreal.eq_iff, nnreal.coe_sub h, ←nnreal.eq_iff, nnreal.coe_add, sub_eq_iff_eq_add] end sub section inv lemma sum_div {ΞΉ} (s : finset ΞΉ) (f : ΞΉ β†’ ℝβ‰₯0) (b : ℝβ‰₯0) : (βˆ‘ i in s, f i) / b = βˆ‘ i in s, (f i / b) := by simp only [div_eq_mul_inv, finset.sum_mul] @[simp] lemma inv_pos {r : ℝβ‰₯0} : 0 < r⁻¹ ↔ 0 < r := by simp [pos_iff_ne_zero] lemma div_pos {r p : ℝβ‰₯0} (hr : 0 < r) (hp : 0 < p) : 0 < r / p := by simpa only [div_eq_mul_inv] using mul_pos hr (inv_pos.2 hp) protected lemma mul_inv {r p : ℝβ‰₯0} : (r * p)⁻¹ = p⁻¹ * r⁻¹ := nnreal.eq $ mul_inv_rev' _ _ lemma div_self_le (r : ℝβ‰₯0) : r / r ≀ 1 := if h : r = 0 then by simp [h] else by rw [div_self h] @[simp] lemma inv_le {r p : ℝβ‰₯0} (h : r β‰  0) : r⁻¹ ≀ p ↔ 1 ≀ r * p := by rw [← mul_le_mul_left (pos_iff_ne_zero.2 h), mul_inv_cancel h] lemma inv_le_of_le_mul {r p : ℝβ‰₯0} (h : 1 ≀ r * p) : r⁻¹ ≀ p := by by_cases r = 0; simp [*, inv_le] @[simp] lemma le_inv_iff_mul_le {r p : ℝβ‰₯0} (h : p β‰  0) : (r ≀ p⁻¹ ↔ r * p ≀ 1) := by rw [← mul_le_mul_left (pos_iff_ne_zero.2 h), mul_inv_cancel h, mul_comm] @[simp] lemma lt_inv_iff_mul_lt {r p : ℝβ‰₯0} (h : p β‰  0) : (r < p⁻¹ ↔ r * p < 1) := by rw [← mul_lt_mul_left (pos_iff_ne_zero.2 h), mul_inv_cancel h, mul_comm] lemma mul_le_iff_le_inv {a b r : ℝβ‰₯0} (hr : r β‰  0) : r * a ≀ b ↔ a ≀ r⁻¹ * b := have 0 < r, from lt_of_le_of_ne (zero_le r) hr.symm, by rw [← @mul_le_mul_left _ _ a _ r this, ← mul_assoc, mul_inv_cancel hr, one_mul] lemma le_div_iff_mul_le {a b r : ℝβ‰₯0} (hr : r β‰  0) : a ≀ b / r ↔ a * r ≀ b := by rw [div_eq_inv_mul, ← mul_le_iff_le_inv hr, mul_comm] lemma div_le_iff {a b r : ℝβ‰₯0} (hr : r β‰  0) : a / r ≀ b ↔ a ≀ b * r := @div_le_iff ℝ _ a r b $ pos_iff_ne_zero.2 hr lemma lt_div_iff {a b r : ℝβ‰₯0} (hr : r β‰  0) : a < b / r ↔ a * r < b := lt_iff_lt_of_le_iff_le (div_le_iff hr) lemma mul_lt_of_lt_div {a b r : ℝβ‰₯0} (h : a < b / r) : a * r < b := begin refine (lt_div_iff $ Ξ» hr, false.elim _).1 h, subst r, simpa using h end lemma le_of_forall_lt_one_mul_le {x y : ℝβ‰₯0} (h : βˆ€a<1, a * x ≀ y) : x ≀ y := le_of_forall_ge_of_dense $ assume a ha, have hx : x β‰  0 := pos_iff_ne_zero.1 (lt_of_le_of_lt (zero_le _) ha), have hx' : x⁻¹ β‰  0, by rwa [(β‰ ), inv_eq_zero], have a * x⁻¹ < 1, by rwa [← lt_inv_iff_mul_lt hx', inv_inv'], have (a * x⁻¹) * x ≀ y, from h _ this, by rwa [mul_assoc, inv_mul_cancel hx, mul_one] at this lemma div_add_div_same (a b c : ℝβ‰₯0) : a / c + b / c = (a + b) / c := eq.symm $ right_distrib a b (c⁻¹) lemma half_pos {a : ℝβ‰₯0} (h : 0 < a) : 0 < a / 2 := div_pos h zero_lt_two lemma add_halves (a : ℝβ‰₯0) : a / 2 + a / 2 = a := nnreal.eq (add_halves a) lemma half_lt_self {a : ℝβ‰₯0} (h : a β‰  0) : a / 2 < a := by rw [← nnreal.coe_lt_coe, nnreal.coe_div]; exact half_lt_self (bot_lt_iff_ne_bot.2 h) lemma two_inv_lt_one : (2⁻¹:ℝβ‰₯0) < 1 := by simpa using half_lt_self zero_ne_one.symm lemma div_lt_iff {a b c : ℝβ‰₯0} (hc : c β‰  0) : b / c < a ↔ b < a * c := begin rw [← nnreal.coe_lt_coe, ← nnreal.coe_lt_coe, nnreal.coe_div, nnreal.coe_mul], exact div_lt_iff (pos_iff_ne_zero.mpr hc) end lemma div_lt_one_of_lt {a b : ℝβ‰₯0} (h : a < b) : a / b < 1 := begin rwa [div_lt_iff, one_mul], exact ne_of_gt (lt_of_le_of_lt (zero_le _) h) end @[field_simps] lemma div_add_div (a : ℝβ‰₯0) {b : ℝβ‰₯0} (c : ℝβ‰₯0) {d : ℝβ‰₯0} (hb : b β‰  0) (hd : d β‰  0) : a / b + c / d = (a * d + b * c) / (b * d) := begin rw ← nnreal.eq_iff, simp only [nnreal.coe_add, nnreal.coe_div, nnreal.coe_mul], exact div_add_div _ _ (coe_ne_zero.2 hb) (coe_ne_zero.2 hd) end @[field_simps] lemma add_div' (a b c : ℝβ‰₯0) (hc : c β‰  0) : b + a / c = (b * c + a) / c := by simpa using div_add_div b a one_ne_zero hc @[field_simps] lemma div_add' (a b c : ℝβ‰₯0) (hc : c β‰  0) : a / c + b = (a + b * c) / c := by rwa [add_comm, add_div', add_comm] lemma of_real_inv {x : ℝ} : nnreal.of_real x⁻¹ = (nnreal.of_real x)⁻¹ := begin by_cases hx : 0 ≀ x, { nth_rewrite 0 ← coe_of_real x hx, rw [←nnreal.coe_inv, of_real_coe], }, { have hx' := le_of_not_ge hx, rw [of_real_eq_zero.mpr hx', inv_zero, of_real_eq_zero.mpr (inv_nonpos.mpr hx')], }, end lemma of_real_div {x y : ℝ} (hx : 0 ≀ x) : nnreal.of_real (x / y) = nnreal.of_real x / nnreal.of_real y := by rw [div_eq_mul_inv, div_eq_mul_inv, ←of_real_inv, ←of_real_mul hx] lemma of_real_div' {x y : ℝ} (hy : 0 ≀ y) : nnreal.of_real (x / y) = nnreal.of_real x / nnreal.of_real y := by rw [div_eq_inv_mul, div_eq_inv_mul, of_real_mul (inv_nonneg.2 hy), of_real_inv] end inv @[simp] lemma abs_eq (x : ℝβ‰₯0) : abs (x : ℝ) = x := abs_of_nonneg x.property end nnreal /-- The absolute value on `ℝ` as a map to `ℝβ‰₯0`. -/ @[pp_nodot] def real.nnabs (x : ℝ) : ℝβ‰₯0 := ⟨abs x, abs_nonneg x⟩ @[norm_cast, simp] lemma nnreal.coe_nnabs (x : ℝ) : (real.nnabs x : ℝ) = abs x := by simp [real.nnabs]
2e805dd8ef23ccfee7cd6f67e98bf0f431494c6d
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/set_theory/ordinal_notation.lean
5bb363920e169b6a9e32781e24cb03d050e67721
[ "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
35,945
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 set_theory.ordinal_arithmetic /-! # Ordinal notation Constructive ordinal arithmetic for ordinals below `Ξ΅β‚€`. We define a type `onote`, with constructors `0 : onote` and `onote.oadd e n a` representing `Ο‰ ^ e * n + a`. We say that `o` is in Cantor normal form - `onote.NF o` - if either `o = 0` or `o = Ο‰ ^ e * n + a` with `a < Ο‰ ^ e` and `a` in Cantor normal form. The type `nonote` is the type of ordinals below `Ξ΅β‚€` in Cantor normal form. Various operations (addition, subtraction, multiplication, power function) are defined on `onote` and `nonote`. -/ open ordinal open_locale ordinal -- get notation for `Ο‰` /-- Recursive definition of an ordinal notation. `zero` denotes the ordinal 0, and `oadd e n a` is intended to refer to `Ο‰^e * n + a`. For this to be valid Cantor normal form, we must have the exponents decrease to the right, but we can't state this condition until we've defined `repr`, so it is a separate definition `NF`. -/ @[derive decidable_eq] inductive onote : Type | zero : onote | oadd : onote β†’ β„•+ β†’ onote β†’ onote namespace onote /-- Notation for 0 -/ instance : has_zero onote := ⟨zero⟩ @[simp] theorem zero_def : zero = 0 := rfl instance : inhabited onote := ⟨0⟩ /-- Notation for 1 -/ instance : has_one onote := ⟨oadd 0 1 0⟩ /-- Notation for Ο‰ -/ def omega : onote := oadd 1 1 0 /-- The ordinal denoted by a notation -/ @[simp] noncomputable def repr : onote β†’ ordinal.{0} | 0 := 0 | (oadd e n a) := Ο‰ ^ repr e * n + repr a /-- Auxiliary definition to print an ordinal notation -/ def to_string_aux1 (e : onote) (n : β„•) (s : string) : string := if e = 0 then _root_.to_string n else (if e = 1 then "Ο‰" else "Ο‰^(" ++ s ++ ")") ++ if n = 1 then "" else "*" ++ _root_.to_string n /-- Print an ordinal notation -/ def to_string : onote β†’ string | zero := "0" | (oadd e n 0) := to_string_aux1 e n (to_string e) | (oadd e n a) := to_string_aux1 e n (to_string e) ++ " + " ++ to_string a /-- Print an ordinal notation -/ def repr' : onote β†’ string | zero := "0" | (oadd e n a) := "(oadd " ++ repr' e ++ " " ++ _root_.to_string (n:β„•) ++ " " ++ repr' a ++ ")" instance : has_to_string onote := ⟨to_string⟩ instance : has_repr onote := ⟨repr'⟩ instance : preorder onote := { le := Ξ» x y, repr x ≀ repr y, lt := Ξ» x y, repr x < repr y, le_refl := Ξ» a, @le_refl ordinal _ _, le_trans := Ξ» a b c, @le_trans ordinal _ _ _ _, lt_iff_le_not_le := Ξ» a b, @lt_iff_le_not_le ordinal _ _ _ } theorem lt_def {x y : onote} : x < y ↔ repr x < repr y := iff.rfl theorem le_def {x y : onote} : x ≀ y ↔ repr x ≀ repr y := iff.rfl /-- Convert a `nat` into an ordinal -/ @[simp] def of_nat : β„• β†’ onote | 0 := 0 | (nat.succ n) := oadd 0 n.succ_pnat 0 @[simp] theorem of_nat_one : of_nat 1 = 1 := rfl @[simp] theorem repr_of_nat (n : β„•) : repr (of_nat n) = n := by cases n; simp @[simp] theorem repr_one : repr 1 = 1 := by simpa using repr_of_nat 1 theorem omega_le_oadd (e n a) : Ο‰ ^ repr e ≀ repr (oadd e n a) := begin unfold repr, refine le_trans _ (le_add_right _ _), simpa using (mul_le_mul_iff_left $ opow_pos (repr e) omega_pos).2 (nat_cast_le.2 n.2) end theorem oadd_pos (e n a) : 0 < oadd e n a := @lt_of_lt_of_le _ _ _ _ _ (opow_pos _ omega_pos) (omega_le_oadd _ _ _) /-- Compare ordinal notations -/ def cmp : onote β†’ onote β†’ ordering | 0 0 := ordering.eq | _ 0 := ordering.gt | 0 _ := ordering.lt | o₁@(oadd e₁ n₁ a₁) oβ‚‚@(oadd eβ‚‚ nβ‚‚ aβ‚‚) := (cmp e₁ eβ‚‚).or_else $ (_root_.cmp (n₁:β„•) nβ‚‚).or_else (cmp a₁ aβ‚‚) theorem eq_of_cmp_eq : βˆ€ {o₁ oβ‚‚}, cmp o₁ oβ‚‚ = ordering.eq β†’ o₁ = oβ‚‚ | 0 0 h := rfl | (oadd e n a) 0 h := by injection h | 0 (oadd e n a) h := by injection h | o₁@(oadd e₁ n₁ a₁) oβ‚‚@(oadd eβ‚‚ nβ‚‚ aβ‚‚) h := begin revert h, simp [cmp], cases h₁ : cmp e₁ eβ‚‚; intro h; try {cases h}, have := eq_of_cmp_eq h₁, subst eβ‚‚, revert h, cases hβ‚‚ : _root_.cmp (n₁:β„•) nβ‚‚; intro h; try {cases h}, have := eq_of_cmp_eq h, subst aβ‚‚, rw [_root_.cmp, cmp_using_eq_eq] at hβ‚‚, have := subtype.eq (eq_of_incomp hβ‚‚), subst nβ‚‚, simp end theorem zero_lt_one : (0 : onote) < 1 := by rw [lt_def, repr, repr_one]; exact zero_lt_one /-- `NF_below o b` says that `o` is a normal form ordinal notation satisfying `repr o < Ο‰ ^ b`. -/ inductive NF_below : onote β†’ ordinal.{0} β†’ Prop | zero {b} : NF_below 0 b | oadd' {e n a eb b} : NF_below e eb β†’ NF_below a (repr e) β†’ repr e < b β†’ NF_below (oadd e n a) b /-- A normal form ordinal notation has the form Ο‰ ^ a₁ * n₁ + Ο‰ ^ aβ‚‚ * nβ‚‚ + ... Ο‰ ^ aβ‚– * nβ‚– where `a₁ > aβ‚‚ > ... > aβ‚–` and all the `aα΅’` are also in normal form. We will essentially only be interested in normal form ordinal notations, but to avoid complicating the algorithms we define everything over general ordinal notations and only prove correctness with normal form as an invariant. -/ class NF (o : onote) : Prop := (out : Exists (NF_below o)) attribute [pp_nodot] NF instance NF.zero : NF 0 := ⟨⟨0, NF_below.zero⟩⟩ theorem NF_below.oadd {e n a b} : NF e β†’ NF_below a (repr e) β†’ repr e < b β†’ NF_below (oadd e n a) b | ⟨⟨eb, h⟩⟩ := NF_below.oadd' h theorem NF_below.fst {e n a b} (h : NF_below (oadd e n a) b) : NF e := by cases h with _ _ _ _ eb _ h₁ hβ‚‚ h₃; exact ⟨⟨_, hβ‚βŸ©βŸ© theorem NF.fst {e n a} : NF (oadd e n a) β†’ NF e | ⟨⟨b, h⟩⟩ := h.fst theorem NF_below.snd {e n a b} (h : NF_below (oadd e n a) b) : NF_below a (repr e) := by cases h with _ _ _ _ eb _ h₁ hβ‚‚ h₃; exact hβ‚‚ theorem NF.snd' {e n a} : NF (oadd e n a) β†’ NF_below a (repr e) | ⟨⟨b, h⟩⟩ := h.snd theorem NF.snd {e n a} (h : NF (oadd e n a)) : NF a := ⟨⟨_, h.snd'⟩⟩ theorem NF.oadd {e a} (h₁ : NF e) (n) (hβ‚‚ : NF_below a (repr e)) : NF (oadd e n a) := ⟨⟨_, NF_below.oadd h₁ hβ‚‚ (ordinal.lt_succ_self _)⟩⟩ instance NF.oadd_zero (e n) [h : NF e] : NF (oadd e n 0) := h.oadd _ NF_below.zero theorem NF_below.lt {e n a b} (h : NF_below (oadd e n a) b) : repr e < b := by cases h with _ _ _ _ eb _ h₁ hβ‚‚ h₃; exact h₃ theorem NF_below_zero : βˆ€ {o}, NF_below o 0 ↔ o = 0 | 0 := ⟨λ _, rfl, Ξ» _, NF_below.zero⟩ | (oadd e n a) := ⟨λ h, (not_le_of_lt h.lt).elim (ordinal.zero_le _), Ξ» e, e.symm β–Έ NF_below.zero⟩ theorem NF.zero_of_zero {e n a} (h : NF (oadd e n a)) (e0 : e = 0) : a = 0 := by simpa [e0, NF_below_zero] using h.snd' theorem NF_below.repr_lt {o b} (h : NF_below o b) : repr o < Ο‰ ^ b := begin induction h with _ e n a eb b h₁ hβ‚‚ h₃ _ IH, { exact opow_pos _ omega_pos }, { rw repr, apply ((add_lt_add_iff_left _).2 IH).trans_le, rw ← mul_succ, apply (mul_le_mul_left' (ordinal.succ_le.2 (nat_lt_omega _)) _).trans, rw ← opow_succ, exact opow_le_opow_right omega_pos (ordinal.succ_le.2 h₃) } end theorem NF_below.mono {o b₁ bβ‚‚} (bb : b₁ ≀ bβ‚‚) (h : NF_below o b₁) : NF_below o bβ‚‚ := begin induction h with _ e n a eb b h₁ hβ‚‚ h₃ _ IH; constructor, exacts [h₁, hβ‚‚, lt_of_lt_of_le h₃ bb] end theorem NF.below_of_lt {e n a b} (H : repr e < b) : NF (oadd e n a) β†’ NF_below (oadd e n a) b | ⟨⟨b', h⟩⟩ := by cases h with _ _ _ _ eb _ h₁ hβ‚‚ h₃; exact NF_below.oadd' h₁ hβ‚‚ H theorem NF.below_of_lt' : βˆ€ {o b}, repr o < Ο‰ ^ b β†’ NF o β†’ NF_below o b | 0 b H _ := NF_below.zero | (oadd e n a) b H h := h.below_of_lt $ (opow_lt_opow_iff_right one_lt_omega).1 $ (lt_of_le_of_lt (omega_le_oadd _ _ _) H) theorem NF_below_of_nat : βˆ€ n, NF_below (of_nat n) 1 | 0 := NF_below.zero | (nat.succ n) := NF_below.oadd NF.zero NF_below.zero ordinal.zero_lt_one instance NF_of_nat (n) : NF (of_nat n) := ⟨⟨_, NF_below_of_nat n⟩⟩ instance NF_one : NF 1 := by rw ← of_nat_one; apply_instance theorem oadd_lt_oadd_1 {e₁ n₁ o₁ eβ‚‚ nβ‚‚ oβ‚‚} (h₁ : NF (oadd e₁ n₁ o₁)) (h : e₁ < eβ‚‚) : oadd e₁ n₁ o₁ < oadd eβ‚‚ nβ‚‚ oβ‚‚ := @lt_of_lt_of_le _ _ _ _ _ ((h₁.below_of_lt h).repr_lt) (omega_le_oadd _ _ _) theorem oadd_lt_oadd_2 {e o₁ oβ‚‚ : onote} {n₁ nβ‚‚ : β„•+} (h₁ : NF (oadd e n₁ o₁)) (h : (n₁:β„•) < nβ‚‚) : oadd e n₁ o₁ < oadd e nβ‚‚ oβ‚‚ := begin simp [lt_def], refine lt_of_lt_of_le ((add_lt_add_iff_left _).2 h₁.snd'.repr_lt) (le_trans _ (le_add_right _ _)), rwa [← mul_succ, mul_le_mul_iff_left (opow_pos _ omega_pos), ordinal.succ_le, nat_cast_lt] end theorem oadd_lt_oadd_3 {e n a₁ aβ‚‚} (h : a₁ < aβ‚‚) : oadd e n a₁ < oadd e n aβ‚‚ := begin rw lt_def, unfold repr, exact add_lt_add_left h _ end theorem cmp_compares : βˆ€ (a b : onote) [NF a] [NF b], (cmp a b).compares a b | 0 0 h₁ hβ‚‚ := rfl | (oadd e n a) 0 h₁ hβ‚‚ := oadd_pos _ _ _ | 0 (oadd e n a) h₁ hβ‚‚ := oadd_pos _ _ _ | o₁@(oadd e₁ n₁ a₁) oβ‚‚@(oadd eβ‚‚ nβ‚‚ aβ‚‚) h₁ hβ‚‚ := begin rw cmp, have IHe := @cmp_compares _ _ h₁.fst hβ‚‚.fst, cases cmp e₁ eβ‚‚, case ordering.lt { exact oadd_lt_oadd_1 h₁ IHe }, case ordering.gt { exact oadd_lt_oadd_1 hβ‚‚ IHe }, change e₁ = eβ‚‚ at IHe, subst IHe, unfold _root_.cmp, cases nh : cmp_using (<) (n₁:β„•) nβ‚‚, case ordering.lt { rw cmp_using_eq_lt at nh, exact oadd_lt_oadd_2 h₁ nh }, case ordering.gt { rw cmp_using_eq_gt at nh, exact oadd_lt_oadd_2 hβ‚‚ nh }, rw cmp_using_eq_eq at nh, have := subtype.eq (eq_of_incomp nh), subst nβ‚‚, have IHa := @cmp_compares _ _ h₁.snd hβ‚‚.snd, cases cmp a₁ aβ‚‚, case ordering.lt { exact oadd_lt_oadd_3 IHa }, case ordering.gt { exact oadd_lt_oadd_3 IHa }, change a₁ = aβ‚‚ at IHa, subst IHa, exact rfl end theorem repr_inj {a b} [NF a] [NF b] : repr a = repr b ↔ a = b := ⟨match cmp a b, cmp_compares a b with | ordering.lt, (h : repr a < repr b), e := (ne_of_lt h e).elim | ordering.gt, (h : repr a > repr b), e := (ne_of_gt h e).elim | ordering.eq, h, e := h end, congr_arg _⟩ theorem NF.of_dvd_omega_opow {b e n a} (h : NF (oadd e n a)) (d : Ο‰ ^ b ∣ repr (oadd e n a)) : b ≀ repr e ∧ Ο‰ ^ b ∣ repr a := begin have := mt repr_inj.1 (Ξ» h, by injection h : oadd e n a β‰  0), have L := le_of_not_lt (Ξ» l, not_le_of_lt (h.below_of_lt l).repr_lt (le_of_dvd this d)), simp at d, exact ⟨L, (dvd_add_iff $ (opow_dvd_opow _ L).mul_right _).1 d⟩ end theorem NF.of_dvd_omega {e n a} (h : NF (oadd e n a)) : Ο‰ ∣ repr (oadd e n a) β†’ repr e β‰  0 ∧ Ο‰ ∣ repr a := by rw [← opow_one Ο‰, ← one_le_iff_ne_zero]; exact h.of_dvd_omega_opow /-- `top_below b o` asserts that the largest exponent in `o`, if it exists, is less than `b`. This is an auxiliary definition for decidability of `NF`. -/ def top_below (b) : onote β†’ Prop | 0 := true | (oadd e n a) := cmp e b = ordering.lt instance decidable_top_below : decidable_rel top_below := by intros b o; cases o; delta top_below; apply_instance theorem NF_below_iff_top_below {b} [NF b] : βˆ€ {o}, NF_below o (repr b) ↔ NF o ∧ top_below b o | 0 := ⟨λ h, ⟨⟨⟨_, h⟩⟩, trivial⟩, Ξ» _, NF_below.zero⟩ | (oadd e n a) := ⟨λ h, ⟨⟨⟨_, h⟩⟩, (@cmp_compares _ b h.fst _).eq_lt.2 h.lt⟩, Ξ» ⟨h₁, hβ‚‚βŸ©, h₁.below_of_lt $ (@cmp_compares _ b h₁.fst _).eq_lt.1 hβ‚‚βŸ© instance decidable_NF : decidable_pred NF | 0 := is_true NF.zero | (oadd e n a) := begin have := decidable_NF e, have := decidable_NF a, resetI, apply decidable_of_iff (NF e ∧ NF a ∧ top_below e a), abstract { rw ← and_congr_right (Ξ» h, @NF_below_iff_top_below _ h _), exact ⟨λ ⟨h₁, hβ‚‚βŸ©, NF.oadd h₁ n hβ‚‚, Ξ» h, ⟨h.fst, h.snd'⟩⟩ }, end /-- Addition of ordinal notations (correct only for normal input) -/ def add : onote β†’ onote β†’ onote | 0 o := o | (oadd e n a) o := match add a o with | 0 := oadd e n 0 | o'@(oadd e' n' a') := match cmp e e' with | ordering.lt := o' | ordering.eq := oadd e (n + n') a' | ordering.gt := oadd e n o' end end instance : has_add onote := ⟨add⟩ @[simp] theorem zero_add (o : onote) : 0 + o = o := rfl theorem oadd_add (e n a o) : oadd e n a + o = add._match_1 e n (a + o) := rfl /-- Subtraction of ordinal notations (correct only for normal input) -/ def sub : onote β†’ onote β†’ onote | 0 o := 0 | o 0 := o | o₁@(oadd e₁ n₁ a₁) (oadd eβ‚‚ nβ‚‚ aβ‚‚) := match cmp e₁ eβ‚‚ with | ordering.lt := 0 | ordering.gt := o₁ | ordering.eq := match (n₁:β„•) - nβ‚‚ with | 0 := if n₁ = nβ‚‚ then sub a₁ aβ‚‚ else 0 | (nat.succ k) := oadd e₁ k.succ_pnat a₁ end end instance : has_sub onote := ⟨sub⟩ theorem add_NF_below {b} : βˆ€ {o₁ oβ‚‚}, NF_below o₁ b β†’ NF_below oβ‚‚ b β†’ NF_below (o₁ + oβ‚‚) b | 0 o h₁ hβ‚‚ := hβ‚‚ | (oadd e n a) o h₁ hβ‚‚ := begin have h' := add_NF_below (h₁.snd.mono $ le_of_lt h₁.lt) hβ‚‚, simp [oadd_add], cases a + o with e' n' a', { exact NF_below.oadd h₁.fst NF_below.zero h₁.lt }, simp [add], have := @cmp_compares _ _ h₁.fst h'.fst, cases cmp e e'; simp [add], { exact h' }, { simp at this, subst e', exact NF_below.oadd h'.fst h'.snd h'.lt }, { exact NF_below.oadd h₁.fst (NF.below_of_lt this ⟨⟨_, h'⟩⟩) h₁.lt } end instance add_NF (o₁ oβ‚‚) : βˆ€ [NF o₁] [NF oβ‚‚], NF (o₁ + oβ‚‚) | ⟨⟨b₁, hβ‚βŸ©βŸ© ⟨⟨bβ‚‚, hβ‚‚βŸ©βŸ© := ⟨(b₁.le_total bβ‚‚).elim (Ξ» h, ⟨bβ‚‚, add_NF_below (h₁.mono h) hβ‚‚βŸ©) (Ξ» h, ⟨b₁, add_NF_below h₁ (hβ‚‚.mono h)⟩)⟩ @[simp] theorem repr_add : βˆ€ o₁ oβ‚‚ [NF o₁] [NF oβ‚‚], repr (o₁ + oβ‚‚) = repr o₁ + repr oβ‚‚ | 0 o h₁ hβ‚‚ := by simp | (oadd e n a) o h₁ hβ‚‚ := begin haveI := h₁.snd, have h' := repr_add a o, conv at h' in (_+o) {simp [(+)]}, have nf := onote.add_NF a o, conv at nf in (_+o) {simp [(+)]}, conv in (_+o) {simp [(+), add]}, cases add a o with e' n' a'; simp [add, h'.symm, add_assoc], have := h₁.fst, haveI := nf.fst, have ee := cmp_compares e e', cases cmp e e'; simp [add], { rw [← add_assoc, @add_absorp _ (repr e') (Ο‰ ^ repr e' * (n':β„•))], { have := (h₁.below_of_lt ee).repr_lt, unfold repr at this, exact lt_of_le_of_lt (le_add_right _ _) this }, { simpa using (mul_le_mul_iff_left $ opow_pos (repr e') omega_pos).2 (nat_cast_le.2 n'.pos) } }, { change e = e' at ee, substI e', rw [← add_assoc, ← ordinal.mul_add, ← nat.cast_add] } end theorem sub_NF_below : βˆ€ {o₁ oβ‚‚ b}, NF_below o₁ b β†’ NF oβ‚‚ β†’ NF_below (o₁ - oβ‚‚) b | 0 o b h₁ hβ‚‚ := by cases o; exact NF_below.zero | (oadd e n a) 0 b h₁ hβ‚‚ := h₁ | (oadd e₁ n₁ a₁) (oadd eβ‚‚ nβ‚‚ aβ‚‚) b h₁ hβ‚‚ := begin have h' := sub_NF_below h₁.snd hβ‚‚.snd, simp [has_sub.sub, sub] at h' ⊒, have := @cmp_compares _ _ h₁.fst hβ‚‚.fst, cases cmp e₁ eβ‚‚; simp [sub], { apply NF_below.zero }, { simp at this, subst eβ‚‚, cases mn : (n₁:β„•) - nβ‚‚; simp [sub], { by_cases en : n₁ = nβ‚‚; simp [en], { exact h'.mono (le_of_lt h₁.lt) }, { exact NF_below.zero } }, { exact NF_below.oadd h₁.fst h₁.snd h₁.lt } }, { exact h₁ } end instance sub_NF (o₁ oβ‚‚) : βˆ€ [NF o₁] [NF oβ‚‚], NF (o₁ - oβ‚‚) | ⟨⟨b₁, hβ‚βŸ©βŸ© hβ‚‚ := ⟨⟨b₁, sub_NF_below h₁ hβ‚‚βŸ©βŸ© @[simp] theorem repr_sub : βˆ€ o₁ oβ‚‚ [NF o₁] [NF oβ‚‚], repr (o₁ - oβ‚‚) = repr o₁ - repr oβ‚‚ | 0 o h₁ hβ‚‚ := by cases o; exact (ordinal.zero_sub _).symm | (oadd e n a) 0 h₁ hβ‚‚ := (ordinal.sub_zero _).symm | (oadd e₁ n₁ a₁) (oadd eβ‚‚ nβ‚‚ aβ‚‚) h₁ hβ‚‚ := begin haveI := h₁.snd, haveI := hβ‚‚.snd, have h' := repr_sub a₁ aβ‚‚, conv at h' in (a₁-aβ‚‚) {simp [has_sub.sub]}, have nf := onote.sub_NF a₁ aβ‚‚, conv at nf in (a₁-aβ‚‚) {simp [has_sub.sub]}, conv in (_-oadd _ _ _) {simp [has_sub.sub, sub]}, have ee := @cmp_compares _ _ h₁.fst hβ‚‚.fst, cases cmp e₁ eβ‚‚, { rw [ordinal.sub_eq_zero_iff_le.2], {refl}, exact le_of_lt (oadd_lt_oadd_1 h₁ ee) }, { change e₁ = eβ‚‚ at ee, substI eβ‚‚, unfold sub._match_1, cases mn : (n₁:β„•) - nβ‚‚; dsimp only [sub._match_2], { by_cases en : n₁ = nβ‚‚, { simp [en], rwa [add_sub_add_cancel] }, { simp [en, -repr], exact (ordinal.sub_eq_zero_iff_le.2 $ le_of_lt $ oadd_lt_oadd_2 h₁ $ lt_of_le_of_ne (tsub_eq_zero_iff_le.1 mn) (mt pnat.eq en)).symm } }, { simp [nat.succ_pnat, -nat.cast_succ], rw [(tsub_eq_iff_eq_add_of_le $ le_of_lt $ nat.lt_of_sub_eq_succ mn).1 mn, add_comm, nat.cast_add, ordinal.mul_add, add_assoc, add_sub_add_cancel], refine (ordinal.sub_eq_of_add_eq $ add_absorp hβ‚‚.snd'.repr_lt $ le_trans _ (le_add_right _ _)).symm, simpa using mul_le_mul_left' (nat_cast_le.2 $ nat.succ_pos _) _ } }, { exact (ordinal.sub_eq_of_add_eq $ add_absorp (hβ‚‚.below_of_lt ee).repr_lt $ omega_le_oadd _ _ _).symm } end /-- Multiplication of ordinal notations (correct only for normal input) -/ def mul : onote β†’ onote β†’ onote | 0 _ := 0 | _ 0 := 0 | o₁@(oadd e₁ n₁ a₁) (oadd eβ‚‚ nβ‚‚ aβ‚‚) := if eβ‚‚ = 0 then oadd e₁ (n₁ * nβ‚‚) a₁ else oadd (e₁ + eβ‚‚) nβ‚‚ (mul o₁ aβ‚‚) instance : has_mul onote := ⟨mul⟩ @[simp] theorem zero_mul (o : onote) : 0 * o = 0 := by cases o; refl @[simp] theorem mul_zero (o : onote) : o * 0 = 0 := by cases o; refl theorem oadd_mul (e₁ n₁ a₁ eβ‚‚ nβ‚‚ aβ‚‚) : oadd e₁ n₁ a₁ * oadd eβ‚‚ nβ‚‚ aβ‚‚ = if eβ‚‚ = 0 then oadd e₁ (n₁ * nβ‚‚) a₁ else oadd (e₁ + eβ‚‚) nβ‚‚ (oadd e₁ n₁ a₁ * aβ‚‚) := rfl theorem oadd_mul_NF_below {e₁ n₁ a₁ b₁} (h₁ : NF_below (oadd e₁ n₁ a₁) b₁) : βˆ€ {oβ‚‚ bβ‚‚}, NF_below oβ‚‚ bβ‚‚ β†’ NF_below (oadd e₁ n₁ a₁ * oβ‚‚) (repr e₁ + bβ‚‚) | 0 bβ‚‚ hβ‚‚ := NF_below.zero | (oadd eβ‚‚ nβ‚‚ aβ‚‚) bβ‚‚ hβ‚‚ := begin have IH := oadd_mul_NF_below hβ‚‚.snd, by_cases e0 : eβ‚‚ = 0; simp [e0, oadd_mul], { apply NF_below.oadd h₁.fst h₁.snd, simpa using (add_lt_add_iff_left (repr e₁)).2 (lt_of_le_of_lt (ordinal.zero_le _) hβ‚‚.lt) }, { haveI := h₁.fst, haveI := hβ‚‚.fst, apply NF_below.oadd, apply_instance, { rwa repr_add }, { rw [repr_add, add_lt_add_iff_left], exact hβ‚‚.lt } } end instance mul_NF : βˆ€ o₁ oβ‚‚ [NF o₁] [NF oβ‚‚], NF (o₁ * oβ‚‚) | 0 o h₁ hβ‚‚ := by cases o; exact NF.zero | (oadd e n a) o ⟨⟨b₁, hbβ‚βŸ©βŸ© ⟨⟨bβ‚‚, hbβ‚‚βŸ©βŸ© := ⟨⟨_, oadd_mul_NF_below hb₁ hbβ‚‚βŸ©βŸ© @[simp] theorem repr_mul : βˆ€ o₁ oβ‚‚ [NF o₁] [NF oβ‚‚], repr (o₁ * oβ‚‚) = repr o₁ * repr oβ‚‚ | 0 o h₁ hβ‚‚ := by cases o; exact (ordinal.zero_mul _).symm | (oadd e₁ n₁ a₁) 0 h₁ hβ‚‚ := (ordinal.mul_zero _).symm | (oadd e₁ n₁ a₁) (oadd eβ‚‚ nβ‚‚ aβ‚‚) h₁ hβ‚‚ := begin have IH : repr (mul _ _) = _ := @repr_mul _ _ h₁ hβ‚‚.snd, conv {to_lhs, simp [(*)]}, have ao : repr a₁ + Ο‰ ^ repr e₁ * (n₁:β„•) = Ο‰ ^ repr e₁ * (n₁:β„•), { apply add_absorp h₁.snd'.repr_lt, simpa using (mul_le_mul_iff_left $ opow_pos _ omega_pos).2 (nat_cast_le.2 n₁.2) }, by_cases e0 : eβ‚‚ = 0; simp [e0, mul], { cases nat.exists_eq_succ_of_ne_zero nβ‚‚.ne_zero with x xe, simp [hβ‚‚.zero_of_zero e0, xe, -nat.cast_succ], rw [← nat_cast_succ x, add_mul_succ _ ao, mul_assoc] }, { haveI := h₁.fst, haveI := hβ‚‚.fst, simp [IH, repr_add, opow_add, ordinal.mul_add], rw ← mul_assoc, congr' 2, have := mt repr_inj.1 e0, rw [add_mul_limit ao (opow_is_limit_left omega_is_limit this), mul_assoc, mul_omega_dvd (nat_cast_pos.2 n₁.pos) (nat_lt_omega _)], simpa using opow_dvd_opow Ο‰ (one_le_iff_ne_zero.2 this) }, end /-- Calculate division and remainder of `o` mod Ο‰. `split' o = (a, n)` means `o = Ο‰ * a + n`. -/ def split' : onote β†’ onote Γ— β„• | 0 := (0, 0) | (oadd e n a) := if e = 0 then (0, n) else let (a', m) := split' a in (oadd (e - 1) n a', m) /-- Calculate division and remainder of `o` mod Ο‰. `split o = (a, n)` means `o = a + n`, where `Ο‰ ∣ a`. -/ def split : onote β†’ onote Γ— β„• | 0 := (0, 0) | (oadd e n a) := if e = 0 then (0, n) else let (a', m) := split a in (oadd e n a', m) /-- `scale x o` is the ordinal notation for `Ο‰ ^ x * o`. -/ def scale (x : onote) : onote β†’ onote | 0 := 0 | (oadd e n a) := oadd (x + e) n (scale a) /-- `mul_nat o n` is the ordinal notation for `o * n`. -/ def mul_nat : onote β†’ β„• β†’ onote | 0 m := 0 | _ 0 := 0 | (oadd e n a) (m+1) := oadd e (n * m.succ_pnat) a /-- Auxiliary definition to compute the ordinal notation for the ordinal exponentiation in `opow` -/ def opow_aux (e a0 a : onote) : β„• β†’ β„• β†’ onote | _ 0 := 0 | 0 (m+1) := oadd e m.succ_pnat 0 | (k+1) m := scale (e + mul_nat a0 k) a + opow_aux k m /-- `opow o₁ oβ‚‚` calculates the ordinal notation for the ordinal exponential `o₁ ^ oβ‚‚`. -/ def opow (o₁ oβ‚‚ : onote) : onote := match split o₁ with | (0, 0) := if oβ‚‚ = 0 then 1 else 0 | (0, 1) := 1 | (0, m+1) := let (b', k) := split' oβ‚‚ in oadd b' (@has_pow.pow β„•+ _ _ m.succ_pnat k) 0 | (a@(oadd a0 _ _), m) := match split oβ‚‚ with | (b, 0) := oadd (a0 * b) 1 0 | (b, k+1) := let eb := a0*b in scale (eb + mul_nat a0 k) a + opow_aux eb a0 (mul_nat a m) k m end end instance : has_pow onote onote := ⟨opow⟩ theorem opow_def (o₁ oβ‚‚ : onote) : o₁ ^ oβ‚‚ = opow._match_1 oβ‚‚ (split o₁) := rfl theorem split_eq_scale_split' : βˆ€ {o o' m} [NF o], split' o = (o', m) β†’ split o = (scale 1 o', m) | 0 o' m h p := by injection p; substs o' m; refl | (oadd e n a) o' m h p := begin by_cases e0 : e = 0; simp [e0, split, split'] at p ⊒, { rcases p with ⟨rfl, rfl⟩, exact ⟨rfl, rfl⟩ }, { revert p, cases h' : split' a with a' m', haveI := h.fst, haveI := h.snd, simp [split_eq_scale_split' h', split, split'], have : 1 + (e - 1) = e, { refine repr_inj.1 _, simp, have := mt repr_inj.1 e0, exact ordinal.add_sub_cancel_of_le (one_le_iff_ne_zero.2 this) }, intros, substs o' m, simp [scale, this] } end theorem NF_repr_split' : βˆ€ {o o' m} [NF o], split' o = (o', m) β†’ NF o' ∧ repr o = Ο‰ * repr o' + m | 0 o' m h p := by injection p; substs o' m; simp [NF.zero] | (oadd e n a) o' m h p := begin by_cases e0 : e = 0; simp [e0, split, split'] at p ⊒, { rcases p with ⟨rfl, rfl⟩, simp [h.zero_of_zero e0, NF.zero] }, { revert p, cases h' : split' a with a' m', haveI := h.fst, haveI := h.snd, cases NF_repr_split' h' with IH₁ IHβ‚‚, simp [IHβ‚‚, split'], intros, substs o' m, have : Ο‰ ^ repr e = Ο‰ ^ (1 : ordinal.{0}) * Ο‰ ^ (repr e - 1), { have := mt repr_inj.1 e0, rw [← opow_add, ordinal.add_sub_cancel_of_le (one_le_iff_ne_zero.2 this)] }, refine ⟨NF.oadd (by apply_instance) _ _, _⟩, { simp at this ⊒, refine IH₁.below_of_lt' ((mul_lt_mul_iff_left omega_pos).1 $ lt_of_le_of_lt (le_add_right _ m') _), rw [← this, ← IHβ‚‚], exact h.snd'.repr_lt }, { rw this, simp [ordinal.mul_add, mul_assoc, add_assoc] } } end theorem scale_eq_mul (x) [NF x] : βˆ€ o [NF o], scale x o = oadd x 1 0 * o | 0 h := rfl | (oadd e n a) h := begin simp [(*)], simp [mul, scale], haveI := h.snd, by_cases e0 : e = 0, { rw scale_eq_mul, simp [e0, h.zero_of_zero, show x + 0 = x, from repr_inj.1 (by simp)] }, { simp [e0, scale_eq_mul, (*)] } end instance NF_scale (x) [NF x] (o) [NF o] : NF (scale x o) := by rw scale_eq_mul; apply_instance @[simp] theorem repr_scale (x) [NF x] (o) [NF o] : repr (scale x o) = Ο‰ ^ repr x * repr o := by simp [scale_eq_mul] theorem NF_repr_split {o o' m} [NF o] (h : split o = (o', m)) : NF o' ∧ repr o = repr o' + m := begin cases e : split' o with a n, cases NF_repr_split' e with s₁ sβ‚‚, resetI, rw split_eq_scale_split' e at h, injection h, substs o' n, simp [repr_scale, sβ‚‚.symm], apply_instance end theorem split_dvd {o o' m} [NF o] (h : split o = (o', m)) : Ο‰ ∣ repr o' := begin cases e : split' o with a n, rw split_eq_scale_split' e at h, injection h, subst o', cases NF_repr_split' e, resetI, simp end theorem split_add_lt {o e n a m} [NF o] (h : split o = (oadd e n a, m)) : repr a + m < Ο‰ ^ repr e := begin cases NF_repr_split h with h₁ hβ‚‚, cases h₁.of_dvd_omega (split_dvd h) with e0 d, have := h₁.fst, have := h₁.snd, refine add_lt_omega_opow h₁.snd'.repr_lt (lt_of_lt_of_le (nat_lt_omega _) _), simpa using opow_le_opow_right omega_pos (one_le_iff_ne_zero.2 e0), end @[simp] theorem mul_nat_eq_mul (n o) : mul_nat o n = o * of_nat n := by cases o; cases n; refl instance NF_mul_nat (o) [NF o] (n) : NF (mul_nat o n) := by simp; apply_instance instance NF_opow_aux (e a0 a) [NF e] [NF a0] [NF a] : βˆ€ k m, NF (opow_aux e a0 a k m) | k 0 := by cases k; exact NF.zero | 0 (m+1) := NF.oadd_zero _ _ | (k+1) (m+1) := by haveI := NF_opow_aux k; simp [opow_aux, nat.succ_ne_zero]; apply_instance instance NF_opow (o₁ oβ‚‚) [NF o₁] [NF oβ‚‚] : NF (o₁ ^ oβ‚‚) := begin cases e₁ : split o₁ with a m, have na := (NF_repr_split e₁).1, cases eβ‚‚ : split' oβ‚‚ with b' k, haveI := (NF_repr_split' eβ‚‚).1, casesI a with a0 n a', { cases m with m, { by_cases oβ‚‚ = 0; simp [pow, opow, *]; apply_instance }, { by_cases m = 0, { simp only [pow, opow, *, zero_def], apply_instance }, { simp [pow, opow, *, - npow_eq_pow], apply_instance } } }, { simp [pow, opow, e₁, eβ‚‚, split_eq_scale_split' eβ‚‚], have := na.fst, cases k with k; simp [succ_eq_add_one, opow]; resetI; apply_instance } end theorem scale_opow_aux (e a0 a : onote) [NF e] [NF a0] [NF a] : βˆ€ k m, repr (opow_aux e a0 a k m) = Ο‰ ^ repr e * repr (opow_aux 0 a0 a k m) | 0 m := by cases m; simp [opow_aux] | (k+1) m := by by_cases m = 0; simp [h, opow_aux, ordinal.mul_add, opow_add, mul_assoc, scale_opow_aux] theorem repr_opow_aux₁ {e a} [Ne : NF e] [Na : NF a] {a' : ordinal} (e0 : repr e β‰  0) (h : a' < Ο‰ ^ repr e) (aa : repr a = a') (n : β„•+) : (Ο‰ ^ repr e * (n:β„•) + a') ^ Ο‰ = (Ο‰ ^ repr e) ^ Ο‰ := begin subst aa, have No := Ne.oadd n (Na.below_of_lt' h), have := omega_le_oadd e n a, unfold repr at this, refine le_antisymm _ (opow_le_opow_left _ this), apply (opow_le_of_limit (ne_of_gt $ lt_of_lt_of_le (opow_pos _ omega_pos) this) omega_is_limit).2, intros b l, have := (No.below_of_lt (lt_succ_self _)).repr_lt, unfold repr at this, apply le_trans (opow_le_opow_left b $ le_of_lt this), rw [← opow_mul, ← opow_mul], apply opow_le_opow_right omega_pos, cases le_or_lt Ο‰ (repr e) with h h, { apply le_trans (mul_le_mul_left' (le_of_lt (lt_succ_self _)) _), rw [succ, add_mul_succ _ (one_add_of_omega_le h), ← succ, succ_le, mul_lt_mul_iff_left (ordinal.pos_iff_ne_zero.2 e0)], exact omega_is_limit.2 _ l }, { refine le_trans (le_of_lt $ mul_lt_omega (omega_is_limit.2 _ h) l) _, simpa using mul_le_mul_right' (one_le_iff_ne_zero.2 e0) Ο‰ } end section local infixr ^ := @pow ordinal.{0} ordinal ordinal.has_pow theorem repr_opow_auxβ‚‚ {a0 a'} [N0 : NF a0] [Na' : NF a'] (m : β„•) (d : Ο‰ ∣ repr a') (e0 : repr a0 β‰  0) (h : repr a' + m < Ο‰ ^ repr a0) (n : β„•+) (k : β„•) : let R := repr (opow_aux 0 a0 (oadd a0 n a' * of_nat m) k m) in (k β‰  0 β†’ R < (Ο‰ ^ repr a0) ^ succ k) ∧ (Ο‰ ^ repr a0) ^ k * (Ο‰ ^ repr a0 * (n:β„•) + repr a') + R = (Ο‰ ^ repr a0 * (n:β„•) + repr a' + m) ^ succ k := begin intro, haveI No : NF (oadd a0 n a') := N0.oadd n (Na'.below_of_lt' $ lt_of_le_of_lt (le_add_right _ _) h), induction k with k IH, {cases m; simp [opow_aux, R]}, rename R R', let R := repr (opow_aux 0 a0 (oadd a0 n a' * of_nat m) k m), let Ο‰0 := Ο‰ ^ repr a0, let Ξ±' := Ο‰0 * n + repr a', change (k β‰  0 β†’ R < Ο‰0 ^ succ k) ∧ Ο‰0 ^ k * Ξ±' + R = (Ξ±' + m) ^ succ k at IH, have RR : R' = Ο‰0 ^ k * (Ξ±' * m) + R, { by_cases m = 0; simp [h, R', opow_aux, R, opow_mul], { cases k; simp [opow_aux] }, { refl } }, have Ξ±0 : 0 < Ξ±', {simpa [Ξ±', lt_def, repr] using oadd_pos a0 n a'}, have Ο‰00 : 0 < Ο‰0 ^ k := opow_pos _ (opow_pos _ omega_pos), have Rl : R < Ο‰ ^ (repr a0 * succ ↑k), { by_cases k0 : k = 0, { simp [k0], refine lt_of_lt_of_le _ (opow_le_opow_right omega_pos (one_le_iff_ne_zero.2 e0)), cases m with m; simp [k0, R, opow_aux, omega_pos], rw [← nat.cast_succ], apply nat_lt_omega }, { rw opow_mul, exact IH.1 k0 } }, refine ⟨λ_, _, _⟩, { rw [RR, ← opow_mul _ _ (succ k.succ)], have e0 := ordinal.pos_iff_ne_zero.2 e0, have rr0 := lt_of_lt_of_le e0 (le_add_left _ _), apply add_lt_omega_opow, { simp [opow_mul, Ο‰0, opow_add, mul_assoc], rw [mul_lt_mul_iff_left Ο‰00, ← ordinal.opow_add], have := (No.below_of_lt _).repr_lt, unfold repr at this, refine mul_lt_omega_opow rr0 this (nat_lt_omega _), simpa using (add_lt_add_iff_left (repr a0)).2 e0 }, { refine lt_of_lt_of_le Rl (opow_le_opow_right omega_pos $ mul_le_mul_left' (succ_le_succ.2 (nat_cast_le.2 (le_of_lt k.lt_succ_self))) _) } }, calc Ο‰0 ^ k.succ * Ξ±' + R' = Ο‰0 ^ succ k * Ξ±' + (Ο‰0 ^ k * Ξ±' * m + R) : by rw [nat_cast_succ, RR, ← mul_assoc] ... = (Ο‰0 ^ k * Ξ±' + R) * Ξ±' + (Ο‰0 ^ k * Ξ±' + R) * m : _ ... = (Ξ±' + m) ^ succ k.succ : by rw [← ordinal.mul_add, ← nat_cast_succ, opow_succ, IH.2], congr' 1, { have Ξ±d : Ο‰ ∣ Ξ±' := dvd_add (dvd_mul_of_dvd_left (by simpa using opow_dvd_opow Ο‰ (one_le_iff_ne_zero.2 e0)) _) d, rw [ordinal.mul_add (Ο‰0 ^ k), add_assoc, ← mul_assoc, ← opow_succ, add_mul_limit _ (is_limit_iff_omega_dvd.2 ⟨ne_of_gt Ξ±0, Ξ±d⟩), mul_assoc, @mul_omega_dvd n (nat_cast_pos.2 n.pos) (nat_lt_omega _) _ Ξ±d], apply @add_absorp _ (repr a0 * succ k), { refine add_lt_omega_opow _ Rl, rw [opow_mul, opow_succ, mul_lt_mul_iff_left Ο‰00], exact No.snd'.repr_lt }, { have := mul_le_mul_left' (one_le_iff_pos.2 $ nat_cast_pos.2 n.pos) (Ο‰0 ^ succ k), rw opow_mul, simpa [-opow_succ] } }, { cases m, { have : R = 0, {cases k; simp [R, opow_aux]}, simp [this] }, { rw [← nat_cast_succ, add_mul_succ], apply add_absorp Rl, rw [opow_mul, opow_succ], apply mul_le_mul_left', simpa [Ξ±', repr] using omega_le_oadd a0 n a' } } end end theorem repr_opow (o₁ oβ‚‚) [NF o₁] [NF oβ‚‚] : repr (o₁ ^ oβ‚‚) = repr o₁ ^ repr oβ‚‚ := begin cases e₁ : split o₁ with a m, cases NF_repr_split e₁ with N₁ r₁, cases a with a0 n a', { cases m with m, { by_cases oβ‚‚ = 0; simp [opow_def, opow, e₁, h, r₁], have := mt repr_inj.1 h, rw zero_opow this }, { cases eβ‚‚ : split' oβ‚‚ with b' k, cases NF_repr_split' eβ‚‚ with _ rβ‚‚, by_cases m = 0; simp [opow_def, opow, e₁, h, r₁, eβ‚‚, rβ‚‚, -nat.cast_succ], rw [opow_add, opow_mul, opow_omega _ (nat_lt_omega _)], simpa using nat_cast_lt.2 (nat.succ_lt_succ $ pos_iff_ne_zero.2 h) } }, { haveI := N₁.fst, haveI := N₁.snd, cases N₁.of_dvd_omega (split_dvd e₁) with a00 ad, have al := split_add_lt e₁, have aa : repr (a' + of_nat m) = repr a' + m, {simp}, cases eβ‚‚ : split' oβ‚‚ with b' k, cases NF_repr_split' eβ‚‚ with _ rβ‚‚, simp [opow_def, opow, e₁, r₁, split_eq_scale_split' eβ‚‚], cases k with k; resetI, { simp [opow, rβ‚‚, opow_mul, repr_opow_aux₁ a00 al aa, add_assoc] }, { simp [succ_eq_add_one, opow, rβ‚‚, opow_add, opow_mul, mul_assoc, add_assoc], rw [repr_opow_aux₁ a00 al aa, scale_opow_aux], simp [opow_mul], rw [← ordinal.mul_add, ← add_assoc (Ο‰ ^ repr a0 * (n:β„•))], congr' 1, rw [← opow_succ], exact (repr_opow_auxβ‚‚ _ ad a00 al _ _).2 } } end end onote /-- The type of normal ordinal notations. (It would have been nicer to define this right in the inductive type, but `NF o` requires `repr` which requires `onote`, so all these things would have to be defined at once, which messes up the VM representation.) -/ def nonote := {o : onote // o.NF} instance : decidable_eq nonote := by unfold nonote; apply_instance namespace nonote open onote instance NF (o : nonote) : NF o.1 := o.2 /-- Construct a `nonote` from an ordinal notation (and infer normality) -/ def mk (o : onote) [h : NF o] : nonote := ⟨o, h⟩ /-- The ordinal represented by an ordinal notation. (This function is noncomputable because ordinal arithmetic is noncomputable. In computational applications `nonote` can be used exclusively without reference to `ordinal`, but this function allows for correctness results to be stated.) -/ noncomputable def repr (o : nonote) : ordinal := o.1.repr instance : has_to_string nonote := ⟨λ x, x.1.to_string⟩ instance : has_repr nonote := ⟨λ x, x.1.repr'⟩ instance : preorder nonote := { le := Ξ» x y, repr x ≀ repr y, lt := Ξ» x y, repr x < repr y, le_refl := Ξ» a, @le_refl ordinal _ _, le_trans := Ξ» a b c, @le_trans ordinal _ _ _ _, lt_iff_le_not_le := Ξ» a b, @lt_iff_le_not_le ordinal _ _ _ } instance : has_zero nonote := ⟨⟨0, NF.zero⟩⟩ instance : inhabited nonote := ⟨0⟩ theorem wf : @well_founded nonote (<) := inv_image.wf repr ordinal.wf instance : has_well_founded nonote := ⟨(<), wf⟩ /-- Convert a natural number to an ordinal notation -/ def of_nat (n : β„•) : nonote := ⟨of_nat n, ⟨⟨_, NF_below_of_nat _⟩⟩⟩ /-- Compare ordinal notations -/ def cmp (a b : nonote) : ordering := cmp a.1 b.1 theorem cmp_compares : βˆ€ a b : nonote, (cmp a b).compares a b | ⟨a, ha⟩ ⟨b, hb⟩ := begin resetI, dsimp [cmp], have := onote.cmp_compares a b, cases onote.cmp a b; try {exact this}, exact subtype.mk_eq_mk.2 this end instance : linear_order nonote := linear_order_of_compares cmp cmp_compares instance : is_well_order nonote (<) := ⟨wf⟩ /-- Asserts that `repr a < Ο‰ ^ repr b`. Used in `nonote.rec_on` -/ def below (a b : nonote) : Prop := NF_below a.1 (repr b) /-- The `oadd` pseudo-constructor for `nonote` -/ def oadd (e : nonote) (n : β„•+) (a : nonote) (h : below a e) : nonote := ⟨_, NF.oadd e.2 n h⟩ /-- This is a recursor-like theorem for `nonote` suggesting an inductive definition, which can't actually be defined this way due to conflicting dependencies. -/ @[elab_as_eliminator] def rec_on {C : nonote β†’ Sort*} (o : nonote) (H0 : C 0) (H1 : βˆ€ e n a h, C e β†’ C a β†’ C (oadd e n a h)) : C o := begin cases o with o h, induction o with e n a IHe IHa, { exact H0 }, { exact H1 ⟨e, h.fst⟩ n ⟨a, h.snd⟩ h.snd' (IHe _) (IHa _) } end /-- Addition of ordinal notations -/ instance : has_add nonote := ⟨λ x y, mk (x.1 + y.1)⟩ theorem repr_add (a b) : repr (a + b) = repr a + repr b := onote.repr_add a.1 b.1 /-- Subtraction of ordinal notations -/ instance : has_sub nonote := ⟨λ x y, mk (x.1 - y.1)⟩ theorem repr_sub (a b) : repr (a - b) = repr a - repr b := onote.repr_sub a.1 b.1 /-- Multiplication of ordinal notations -/ instance : has_mul nonote := ⟨λ x y, mk (x.1 * y.1)⟩ theorem repr_mul (a b) : repr (a * b) = repr a * repr b := onote.repr_mul a.1 b.1 /-- Exponentiation of ordinal notations -/ def opow (x y : nonote) := mk (x.1.opow y.1) theorem repr_opow (a b) : repr (opow a b) = (repr a).opow (repr b) := onote.repr_opow a.1 b.1 end nonote
ec5960772498f9695f760a3202f80752cfa52624
206422fb9edabf63def0ed2aa3f489150fb09ccb
/src/ring_theory/adjoin_root.lean
acf6b881796b4079671cb25c9131697946fc9b3b
[ "Apache-2.0" ]
permissive
hamdysalah1/mathlib
b915f86b2503feeae268de369f1b16932321f097
95454452f6b3569bf967d35aab8d852b1ddf8017
refs/heads/master
1,677,154,116,545
1,611,797,994,000
1,611,797,994,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
7,716
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Chris Hughes Adjoining roots of polynomials -/ import data.polynomial.field_division import ring_theory.adjoin import ring_theory.principal_ideal_domain import linear_algebra.finite_dimensional /-! # Adjoining roots of polynomials This file defines the commutative ring `adjoin_root f`, the ring R[X]/(f) obtained from a commutative ring `R` and a polynomial `f : R[X]`. If furthermore `R` is a field and `f` is irreducible, the field structure on `adjoin_root f` is constructed. ## Main definitions and results The main definitions are in the `adjoin_root` namespace. * `mk f : polynomial R β†’+* adjoin_root f`, the natural ring homomorphism. * `of f : R β†’+* adjoin_root f`, the natural ring homomorphism. * `root f : adjoin_root f`, the image of X in R[X]/(f). * `lift (i : R β†’+* S) (x : S) (h : f.evalβ‚‚ i x = 0) : (adjoin_root f) β†’+* S`, the ring homomorphism from R[X]/(f) to S extending `i : R β†’+* S` and sending `X` to `x`. * `lift_hom (x : S) (hfx : aeval x f = 0) : adjoin_root f →ₐ[R] S`, the algebra homomorphism from R[X]/(f) to S extending `algebra_map R S` and sending `X` to `x` * `equiv : (adjoin_root f →ₐ[F] E) ≃ {x // x ∈ (f.map (algebra_map F E)).roots}` a bijection between algebra homomorphisms from `adjoin_root` and roots of `f` in `S` -/ noncomputable theory open_locale classical open_locale big_operators universes u v w variables {R : Type u} {S : Type v} {K : Type w} open polynomial ideal /-- Adjoin a root of a polynomial `f` to a commutative ring `R`. We define the new ring as the quotient of `R` by the principal ideal of `f`. -/ def adjoin_root [comm_ring R] (f : polynomial R) : Type u := ideal.quotient (span {f} : ideal (polynomial R)) namespace adjoin_root section comm_ring variables [comm_ring R] (f : polynomial R) instance : comm_ring (adjoin_root f) := ideal.quotient.comm_ring _ instance : inhabited (adjoin_root f) := ⟨0⟩ instance : decidable_eq (adjoin_root f) := classical.dec_eq _ /-- Ring homomorphism from `R[x]` to `adjoin_root f` sending `X` to the `root`. -/ def mk : polynomial R β†’+* adjoin_root f := ideal.quotient.mk _ @[elab_as_eliminator] theorem induction_on {C : adjoin_root f β†’ Prop} (x : adjoin_root f) (ih : βˆ€ p : polynomial R, C (mk f p)) : C x := quotient.induction_on' x ih /-- Embedding of the original ring `R` into `adjoin_root f`. -/ def of : R β†’+* adjoin_root f := (mk f).comp (ring_hom.of C) instance : algebra R (adjoin_root f) := (of f).to_algebra @[simp] lemma algebra_map_eq : algebra_map R (adjoin_root f) = of f := rfl /-- The adjoined root. -/ def root : adjoin_root f := mk f X variables {f} instance adjoin_root.has_coe_t : has_coe_t R (adjoin_root f) := ⟨of f⟩ @[simp] lemma mk_self : mk f f = 0 := quotient.sound' (mem_span_singleton.2 $ by simp) @[simp] lemma mk_C (x : R) : mk f (C x) = x := rfl @[simp] lemma mk_X : mk f X = root f := rfl @[simp] lemma aeval_eq (p : polynomial R) : aeval (root f) p = mk f p := polynomial.induction_on p (Ξ» x, by { rw aeval_C, refl }) (Ξ» p q ihp ihq, by rw [alg_hom.map_add, ring_hom.map_add, ihp, ihq]) (Ξ» n x ih, by { rw [alg_hom.map_mul, aeval_C, alg_hom.map_pow, aeval_X, ring_hom.map_mul, mk_C, ring_hom.map_pow, mk_X], refl }) theorem adjoin_root_eq_top : algebra.adjoin R ({root f} : set (adjoin_root f)) = ⊀ := algebra.eq_top_iff.2 $ Ξ» x, induction_on f x $ Ξ» p, (algebra.adjoin_singleton_eq_range R (root f)).symm β–Έ ⟨p, set.mem_univ _, aeval_eq p⟩ @[simp] lemma evalβ‚‚_root (f : polynomial R) : f.evalβ‚‚ (of f) (root f) = 0 := by rw [← algebra_map_eq, ← aeval_def, aeval_eq, mk_self] lemma is_root_root (f : polynomial R) : is_root (f.map (of f)) (root f) := by rw [is_root, eval_map, evalβ‚‚_root] variables [comm_ring S] /-- Lift a ring homomorphism `i : R β†’+* S` to `adjoin_root f β†’+* S`. -/ def lift (i : R β†’+* S) (x : S) (h : f.evalβ‚‚ i x = 0) : (adjoin_root f) β†’+* S := begin apply ideal.quotient.lift _ (evalβ‚‚_ring_hom i x), intros g H, rcases mem_span_singleton.1 H with ⟨y, hy⟩, rw [hy, ring_hom.map_mul, coe_evalβ‚‚_ring_hom, h, zero_mul] end variables {i : R β†’+* S} {a : S} {h : f.evalβ‚‚ i a = 0} @[simp] lemma lift_mk {g : polynomial R} : lift i a h (mk f g) = g.evalβ‚‚ i a := ideal.quotient.lift_mk _ _ _ @[simp] lemma lift_root : lift i a h (root f) = a := by rw [root, lift_mk, evalβ‚‚_X] @[simp] lemma lift_of {x : R} : lift i a h x = i x := by rw [← mk_C x, lift_mk, evalβ‚‚_C] @[simp] lemma lift_comp_of : (lift i a h).comp (of f) = i := ring_hom.ext $ Ξ» _, @lift_of _ _ _ _ _ _ _ h _ variables (f) [algebra R S] /-- Produce an algebra homomorphism `adjoin_root f →ₐ[R] S` sending `root f` to a root of `f` in `S`. -/ def lift_hom (x : S) (hfx : aeval x f = 0) : adjoin_root f →ₐ[R] S := { commutes' := Ξ» r, show lift _ _ hfx r = _, from lift_of, .. lift (algebra_map R S) x hfx } @[simp] lemma coe_lift_hom (x : S) (hfx : aeval x f = 0) : (lift_hom f x hfx : adjoin_root f β†’+* S) = lift (algebra_map R S) x hfx := rfl @[simp] lemma aeval_alg_hom_eq_zero (Ο• : adjoin_root f →ₐ[R] S) : aeval (Ο• (root f)) f = 0 := begin have h : Ο•.to_ring_hom.comp (of f) = algebra_map R S := ring_hom.ext_iff.mpr (Ο•.commutes), rw [aeval_def, ←h, ←ring_hom.map_zero Ο•.to_ring_hom, ←evalβ‚‚_root f, hom_evalβ‚‚], refl, end @[simp] lemma lift_hom_eq_alg_hom (f : polynomial R) (Ο• : adjoin_root f →ₐ[R] S) : lift_hom f (Ο• (root f)) (aeval_alg_hom_eq_zero f Ο•) = Ο• := begin suffices : Ο•.equalizer (lift_hom f (Ο• (root f)) (aeval_alg_hom_eq_zero f Ο•)) = ⊀, { exact (alg_hom.ext (Ξ» x, (subalgebra.ext_iff.mp (this) x).mpr algebra.mem_top)).symm }, rw [eq_top_iff, ←adjoin_root_eq_top, algebra.adjoin_le_iff, set.singleton_subset_iff], exact (@lift_root _ _ _ _ _ _ _ (aeval_alg_hom_eq_zero f Ο•)).symm, end /-- If `E` is a field extension of `F` and `f` is a polynomial over `F` then the set of maps from `F[x]/(f)` into `E` is in bijection with the set of roots of `f` in `E`. -/ def equiv (F E : Type*) [field F] [field E] [algebra F E] (f : polynomial F) (hf : f β‰  0) : (adjoin_root f →ₐ[F] E) ≃ {x // x ∈ (f.map (algebra_map F E)).roots} := { to_fun := Ξ» Ο•, βŸ¨Ο• (root f), begin rw [mem_roots (map_ne_zero hf), is_root.def, ←evalβ‚‚_eq_eval_map], exact aeval_alg_hom_eq_zero f Ο•, exact field.to_nontrivial E, end⟩, inv_fun := Ξ» x, lift_hom f ↑x (begin rw [aeval_def, evalβ‚‚_eq_eval_map, ←is_root.def, ←mem_roots (map_ne_zero hf)], exact subtype.mem x, exact field.to_nontrivial E end), left_inv := Ξ» Ο•, lift_hom_eq_alg_hom f Ο•, right_inv := Ξ» x, begin ext, refine @lift_root F E _ f _ _ ↑x _, rw [evalβ‚‚_eq_eval_map, ←is_root.def, ←mem_roots (map_ne_zero hf), ←multiset.mem_to_finset], exact multiset.mem_to_finset.mpr (subtype.mem x), exact field.to_nontrivial E end } end comm_ring variables [field K] {f : polynomial K} [irreducible f] instance is_maximal_span : is_maximal (span {f} : ideal (polynomial K)) := principal_ideal_ring.is_maximal_of_irreducible β€Ήirreducible fβ€Ί noncomputable instance field : field (adjoin_root f) := { ..adjoin_root.comm_ring f, ..ideal.quotient.field (span {f} : ideal (polynomial K)) } lemma coe_injective : function.injective (coe : K β†’ adjoin_root f) := (of f).injective variable (f) lemma mul_div_root_cancel : ((X - C (root f)) * (f.map (of f) / (X - C (root f))) : polynomial (adjoin_root f)) = f.map (of f) := mul_div_eq_iff_is_root.2 $ is_root_root _ end adjoin_root
3973f2a7f54be2b2299ab3c8414af7158bf1747b
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/tests/lean/StxQuot.lean
1cdf9e5bee72ef9fc56457a196d776aa8a0fe3fb
[ "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,544
lean
import Lean open Lean open Lean.Elab def run {Ξ±} [ToString Ξ±] : Unhygienic Ξ± β†’ String := toString ∘ Unhygienic.run #eval run `() #eval run `(Nat.one) def miss : TSyntax `term := ⟨Syntax.missing⟩ #eval run `($miss) namespace Lean.Syntax #eval run `($miss) #eval run `($(miss)) #eval run `($(id miss) + 1) #eval run $ let id := miss; `($id + 1) end Lean.Syntax #eval run `(1 + 1) #eval run `([x,]) #eval run $ `(fun a => a) >>= pure #eval run $ `(def foo := 1) #eval run $ `(def foo := 1 def bar := 2) #eval run $ do let a ← `(Nat.one); `($a) #eval run $ do `($(← `(Nat.one))) #eval run $ do let a ← `(Nat.one); `(f $a $a) #eval run $ do let a ← `(Nat.one); `(f $ f $a 1) #eval run $ do let a ← `(Nat.one); `(f $(id a)) #eval run $ do let a ← `(Nat.one); `($(a).b) #eval run $ do let a ← `(1 + 2); match a with | `($a + $b) => `($b + $a) | _ => pure miss #eval run $ do let a ← `(1 + 2); match a with | stx@`($a + $b) => `($stx + $a) | _ => pure miss #eval run $ do let a ← `(def foo := 1); match a with | `($f:command) => pure f #eval run $ do let a ← `(def foo := 1 def bar := 2); match a with | `($f:command $g:command) => `($g:command $f:command) | _ => pure ⟨Syntax.missing⟩ #eval run $ do let a ← `(aa); match a with | `($_:ident) => pure 0 | `($_) => pure 1 #eval match mkIdent `aa with | `(aa) => 0 | _ => 1 #eval match mkIdent `aa with | `(ab) => 0 | _ => 1 #eval run $ do let a ← `(1 + 2); match a with | `($id:ident) => pure 0 | `($e) => pure 1 #eval run $ do let params ← #[`(a), `((b : Nat))].mapM id; `(fun $params:term* => 1) #eval run $ do let a ← `(fun (a : Nat) b => c); match a with | `(fun $aa* => $e) => pure aa | _ => pure #[] #eval run $ do let a ← `(βˆ€ a, c); match a with | `(βˆ€ $id:ident, $e) => pure id | _ => pure ⟨a⟩ #eval run $ do let a ← `(βˆ€ _, c); match a with | `(βˆ€ $id:ident, $e) => pure id | _ => pure ⟨a⟩ -- this one should NOT check the kind of the matched node #eval run $ do let a ← `(βˆ€ _, c); match a with | `(βˆ€ $a, $e) => pure a | _ => pure ⟨a⟩ #eval run $ do let a ← `(a); match a with | `($id:ident) => pure id | _ => pure ⟨a⟩ #eval run $ do let a ← `(a.{0}); match a with | `($id:ident) => pure id | _ => pure ⟨a⟩ #eval run $ do let a ← `(match a with | a => 1 | _ => 2); match a with | `(match $e:term with $eqns:matchAlt*) => pure eqns | _ => pure #[] def f (stx : Syntax) : Unhygienic Syntax := match stx with | `({ $f:ident := $e $[: $a]?}) => `({ $f:ident := $e $[: $(id a)]?}) | _ => unreachable! #eval run do f (← `({ a := a : a })) #eval run do f (← `({ a := a })) def f' (stx : Syntax) : Unhygienic Syntax := match stx with | `(section $(id?)?) => `(section $(id?)?) | _ => unreachable! #eval run do f' (← `(section)) #eval run do f' (← `(section foo)) #eval run do match ← `(match a with | a => b | a + 1 => b + 1) with | `(match $e:term with $[| $pats =>%$arr $rhss]*) => `(match $e:term with $[| $pats =>%$arr $rhss]*) | _ => unreachable! #eval run do match ← `(match a with | a => b | a + 1 => b + 1) with | `(match $e:term with $alts:matchAlt*) => `(match $e:term with $alts:matchAlt*) | _ => unreachable! open Parser.Term #eval run do match ← `(structInstField|a := b) with | `(Parser.Term.structInstField| $lhs:ident := $rhs) => pure #[lhs.raw, rhs] | _ => unreachable! #eval run do match ← `({ a := a : a }) with | `({ $f:ident := $e : 0 }) => pure "0" | `({ $f:ident := $e $[: $a?]?}) => pure "1" | stx => pure "2" #eval run `(sufficesDecl|x from x) #eval run do match ← `([1, 2, 3, 4]) with | `([$x, $ys,*, $z]) => pure #[x.raw, mkNullNode ys, z] | _ => unreachable! #eval run do match ← `([1, 2]) with | `([$x, $y, $zs,*]) => pure zs.getElems | `([$x, $ys,*]) => pure ys.getElems | _ => unreachable! #check (match Β· with | `([1, $ys,*, 2, $zs,*, 3]) => _) #eval run do match Syntax.setHeadInfo (← `(fun x =>%$(Syntax.atom (SourceInfo.synthetic ⟨2⟩ ⟨2⟩) "") x)) (SourceInfo.synthetic ⟨1⟩ ⟨1⟩) with | `(fun%$i1 $x =>%$i2 $y) => pure #[i1.getPos?, i2.getPos?] | _ => unreachable! #eval run ``(x) #eval run ``(id) #eval run ``(pure) syntax "foo" term : term #eval run ``(foo $(miss)) -- syntax with no quoted identifiers should be ignored #eval run ``(fun x => x) #eval run ``(fun x => y) #eval run ``(fun x y => x y) #eval run ``(fun ⟨x, y⟩ => x) #eval run do match mkIdent `b with | `(a) => pure "0" | `(b) => pure "1" | _ => pure "2"
911fd1fca68bdfba9b8ae2db28b5bd98407b791f
8cb37a089cdb4af3af9d8bf1002b417e407a8e9e
/library/init/meta/exceptional.lean
d28c6f66d3db45c5fe162b75d000d74ccd7bff0d
[ "Apache-2.0" ]
permissive
kbuzzard/lean
ae3c3db4bb462d750dbf7419b28bafb3ec983ef7
ed1788fd674bb8991acffc8fca585ec746711928
refs/heads/master
1,620,983,366,617
1,618,937,600,000
1,618,937,600,000
359,886,396
1
0
Apache-2.0
1,618,936,987,000
1,618,936,987,000
null
UTF-8
Lean
false
false
1,614
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.control.monad init.meta.format init.util /- Remark: we use a function that produces a format object as the exception information. Motivation: the formatting object may be big, and we may create it on demand. -/ /-- An exceptional is similar to `Result` in Haskell.-/ meta inductive exceptional (Ξ± : Type) | success : Ξ± β†’ exceptional | exception : (options β†’ format) β†’ exceptional section open exceptional variables {Ξ± : Type} variables [has_to_string Ξ±] protected meta def exceptional.to_string : exceptional Ξ± β†’ string | (success a) := to_string a | (exception e) := "Exception: " ++ to_string (e options.mk) meta instance : has_to_string (exceptional Ξ±) := has_to_string.mk exceptional.to_string end namespace exceptional variables {Ξ± Ξ² : Type} protected meta def to_bool : exceptional Ξ± β†’ bool | (success _) := tt | (exception _) := ff protected meta def to_option : exceptional Ξ± β†’ option Ξ± | (success a) := some a | (exception _) := none @[inline] protected meta def bind (e₁ : exceptional Ξ±) (eβ‚‚ : Ξ± β†’ exceptional Ξ²) : exceptional Ξ² := exceptional.cases_on e₁ (Ξ» a, eβ‚‚ a) (Ξ» f, exception f) @[inline] protected meta def return (a : Ξ±) : exceptional Ξ± := success a @[inline] meta def fail (f : format) : exceptional Ξ± := exception (Ξ» u, f) end exceptional meta instance : monad exceptional := {pure := @exceptional.return, bind := @exceptional.bind}
dd9078151710c945e05f2c84fddac8dcab3525ac
69d4931b605e11ca61881fc4f66db50a0a875e39
/src/topology/constructions.lean
0d746832a3e2013c5a9de8e4b16819b90506c013
[ "Apache-2.0" ]
permissive
abentkamp/mathlib
d9a75d291ec09f4637b0f30cc3880ffb07549ee5
5360e476391508e092b5a1e5210bd0ed22dc0755
refs/heads/master
1,682,382,954,948
1,622,106,077,000
1,622,106,077,000
149,285,665
0
0
null
null
null
null
UTF-8
Lean
false
false
41,190
lean
/- Copyright (c) 2017 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro, Patrick Massot -/ import topology.maps /-! # Constructions of new topological spaces from old ones This file constructs products, sums, subtypes and quotients of topological spaces and sets up their basic theory, such as criteria for maps into or out of these constructions to be continuous; descriptions of the open sets, neighborhood filters, and generators of these constructions; and their behavior with respect to embeddings and other specific classes of maps. ## Implementation note The constructed topologies are defined using induced and coinduced topologies along with the complete lattice structure on topologies. Their universal properties (for example, a map `X β†’ Y Γ— Z` is continuous if and only if both projections `X β†’ Y`, `X β†’ Z` are) follow easily using order-theoretic descriptions of continuity. With more work we can also extract descriptions of the open sets, neighborhood filters and so on. ## Tags product, sum, disjoint union, subspace, quotient space -/ noncomputable theory open topological_space set filter open_locale classical topological_space filter universes u v w x variables {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} {Ξ΄ : Type x} section constructions instance {p : Ξ± β†’ Prop} [t : topological_space Ξ±] : topological_space (subtype p) := induced coe t instance {r : Ξ± β†’ Ξ± β†’ Prop} [t : topological_space Ξ±] : topological_space (quot r) := coinduced (quot.mk r) t instance {s : setoid Ξ±} [t : topological_space Ξ±] : topological_space (quotient s) := coinduced quotient.mk t instance [t₁ : topological_space Ξ±] [tβ‚‚ : topological_space Ξ²] : topological_space (Ξ± Γ— Ξ²) := induced prod.fst t₁ βŠ“ induced prod.snd tβ‚‚ instance [t₁ : topological_space Ξ±] [tβ‚‚ : topological_space Ξ²] : topological_space (Ξ± βŠ• Ξ²) := coinduced sum.inl t₁ βŠ” coinduced sum.inr tβ‚‚ instance {Ξ² : Ξ± β†’ Type v} [tβ‚‚ : Ξ a, topological_space (Ξ² a)] : topological_space (sigma Ξ²) := ⨆a, coinduced (sigma.mk a) (tβ‚‚ a) instance Pi.topological_space {Ξ² : Ξ± β†’ Type v} [tβ‚‚ : Ξ a, topological_space (Ξ² a)] : topological_space (Ξ a, Ξ² a) := β¨…a, induced (Ξ»f, f a) (tβ‚‚ a) instance ulift.topological_space [t : topological_space Ξ±] : topological_space (ulift.{v u} Ξ±) := t.induced ulift.down lemma quotient.preimage_mem_nhds [topological_space Ξ±] [s : setoid Ξ±] {V : set $ quotient s} {a : Ξ±} (hs : V ∈ 𝓝 (quotient.mk a)) : quotient.mk ⁻¹' V ∈ 𝓝 a := preimage_nhds_coinduced hs /-- The image of a dense set under `quotient.mk` is a dense set. -/ lemma dense.quotient [setoid Ξ±] [topological_space Ξ±] {s : set Ξ±} (H : dense s) : dense (quotient.mk '' s) := (surjective_quotient_mk Ξ±).dense_range.dense_image continuous_coinduced_rng H /-- The composition of `quotient.mk` and a function with dense range has dense range. -/ lemma dense_range.quotient [setoid Ξ±] [topological_space Ξ±] {f : Ξ² β†’ Ξ±} (hf : dense_range f) : dense_range (quotient.mk ∘ f) := (surjective_quotient_mk Ξ±).dense_range.comp hf continuous_coinduced_rng instance {p : Ξ± β†’ Prop} [topological_space Ξ±] [discrete_topology Ξ±] : discrete_topology (subtype p) := ⟨bot_unique $ assume s hs, ⟨coe '' s, is_open_discrete _, (set.preimage_image_eq _ subtype.coe_injective)⟩⟩ instance sum.discrete_topology [topological_space Ξ±] [topological_space Ξ²] [hΞ± : discrete_topology Ξ±] [hΞ² : discrete_topology Ξ²] : discrete_topology (Ξ± βŠ• Ξ²) := ⟨by unfold sum.topological_space; simp [hΞ±.eq_bot, hΞ².eq_bot]⟩ instance sigma.discrete_topology {Ξ² : Ξ± β†’ Type v} [Ξ a, topological_space (Ξ² a)] [h : Ξ a, discrete_topology (Ξ² a)] : discrete_topology (sigma Ξ²) := ⟨by { unfold sigma.topological_space, simp [Ξ» a, (h a).eq_bot] }⟩ section topΞ± variable [topological_space Ξ±] /- The 𝓝 filter and the subspace topology. -/ theorem mem_nhds_subtype (s : set Ξ±) (a : {x // x ∈ s}) (t : set {x // x ∈ s}) : t ∈ 𝓝 a ↔ βˆƒ u ∈ 𝓝 (a : Ξ±), coe ⁻¹' u βŠ† t := mem_nhds_induced coe a t theorem nhds_subtype (s : set Ξ±) (a : {x // x ∈ s}) : 𝓝 a = comap coe (𝓝 (a : Ξ±)) := nhds_induced coe a end topΞ± end constructions section prod variables [topological_space Ξ±] [topological_space Ξ²] [topological_space Ξ³] [topological_space Ξ΄] @[continuity] lemma continuous_fst : continuous (@prod.fst Ξ± Ξ²) := continuous_inf_dom_left continuous_induced_dom lemma continuous_at_fst {p : Ξ± Γ— Ξ²} : continuous_at prod.fst p := continuous_fst.continuous_at @[continuity] lemma continuous_snd : continuous (@prod.snd Ξ± Ξ²) := continuous_inf_dom_right continuous_induced_dom lemma continuous_at_snd {p : Ξ± Γ— Ξ²} : continuous_at prod.snd p := continuous_snd.continuous_at @[continuity] lemma continuous.prod_mk {f : Ξ³ β†’ Ξ±} {g : Ξ³ β†’ Ξ²} (hf : continuous f) (hg : continuous g) : continuous (Ξ»x, (f x, g x)) := continuous_inf_rng (continuous_induced_rng hf) (continuous_induced_rng hg) lemma continuous.prod_map {f : Ξ³ β†’ Ξ±} {g : Ξ΄ β†’ Ξ²} (hf : continuous f) (hg : continuous g) : continuous (Ξ» x : Ξ³ Γ— Ξ΄, (f x.1, g x.2)) := (hf.comp continuous_fst).prod_mk (hg.comp continuous_snd) lemma filter.eventually.prod_inl_nhds {p : Ξ± β†’ Prop} {a : Ξ±} (h : βˆ€αΆ  x in 𝓝 a, p x) (b : Ξ²) : βˆ€αΆ  x in 𝓝 (a, b), p (x : Ξ± Γ— Ξ²).1 := continuous_at_fst h lemma filter.eventually.prod_inr_nhds {p : Ξ² β†’ Prop} {b : Ξ²} (h : βˆ€αΆ  x in 𝓝 b, p x) (a : Ξ±) : βˆ€αΆ  x in 𝓝 (a, b), p (x : Ξ± Γ— Ξ²).2 := continuous_at_snd h lemma filter.eventually.prod_mk_nhds {pa : Ξ± β†’ Prop} {a} (ha : βˆ€αΆ  x in 𝓝 a, pa x) {pb : Ξ² β†’ Prop} {b} (hb : βˆ€αΆ  y in 𝓝 b, pb y) : βˆ€αΆ  p in 𝓝 (a, b), pa (p : Ξ± Γ— Ξ²).1 ∧ pb p.2 := (ha.prod_inl_nhds b).and (hb.prod_inr_nhds a) lemma continuous_swap : continuous (prod.swap : Ξ± Γ— Ξ² β†’ Ξ² Γ— Ξ±) := continuous.prod_mk continuous_snd continuous_fst lemma continuous_uncurry_left {f : Ξ± β†’ Ξ² β†’ Ξ³} (a : Ξ±) (h : continuous (function.uncurry f)) : continuous (f a) := show continuous (function.uncurry f ∘ (Ξ» b, (a, b))), from h.comp (by continuity) lemma continuous_uncurry_right {f : Ξ± β†’ Ξ² β†’ Ξ³} (b : Ξ²) (h : continuous (function.uncurry f)) : continuous (Ξ» a, f a b) := show continuous (function.uncurry f ∘ (Ξ» a, (a, b))), from h.comp (by continuity) lemma continuous_curry {g : Ξ± Γ— Ξ² β†’ Ξ³} (a : Ξ±) (h : continuous g) : continuous (function.curry g a) := show continuous (g ∘ (Ξ» b, (a, b))), from h.comp (by continuity) lemma is_open.prod {s : set Ξ±} {t : set Ξ²} (hs : is_open s) (ht : is_open t) : is_open (set.prod s t) := is_open.inter (hs.preimage continuous_fst) (ht.preimage continuous_snd) lemma nhds_prod_eq {a : Ξ±} {b : Ξ²} : 𝓝 (a, b) = 𝓝 a Γ—αΆ  𝓝 b := by rw [filter.prod, prod.topological_space, nhds_inf, nhds_induced, nhds_induced] lemma mem_nhds_prod_iff {a : Ξ±} {b : Ξ²} {s : set (Ξ± Γ— Ξ²)} : s ∈ 𝓝 (a, b) ↔ βˆƒ (u ∈ 𝓝 a) (v ∈ 𝓝 b), set.prod u v βŠ† s := by rw [nhds_prod_eq, mem_prod_iff] lemma mem_nhds_prod_iff' {a : Ξ±} {b : Ξ²} {s : set (Ξ± Γ— Ξ²)} : s ∈ 𝓝 (a, b) ↔ βˆƒ u v, is_open u ∧ a ∈ u ∧ is_open v ∧ b ∈ v ∧ set.prod u v βŠ† s := begin rw mem_nhds_prod_iff, split, { rintros ⟨u, Hu, v, Hv, h⟩, rcases mem_nhds_iff.1 Hu with ⟨u', u'u, u'_open, Hu'⟩, rcases mem_nhds_iff.1 Hv with ⟨v', v'v, v'_open, Hv'⟩, exact ⟨u', v', u'_open, Hu', v'_open, Hv', (set.prod_mono u'u v'v).trans h⟩ }, { rintros ⟨u, v, u_open, au, v_open, bv, huv⟩, exact ⟨u, u_open.mem_nhds au, v, v_open.mem_nhds bv, huv⟩ } end lemma filter.has_basis.prod_nhds {ΞΉa ΞΉb : Type*} {pa : ΞΉa β†’ Prop} {pb : ΞΉb β†’ Prop} {sa : ΞΉa β†’ set Ξ±} {sb : ΞΉb β†’ set Ξ²} {a : Ξ±} {b : Ξ²} (ha : (𝓝 a).has_basis pa sa) (hb : (𝓝 b).has_basis pb sb) : (𝓝 (a, b)).has_basis (Ξ» i : ΞΉa Γ— ΞΉb, pa i.1 ∧ pb i.2) (Ξ» i, (sa i.1).prod (sb i.2)) := by { rw nhds_prod_eq, exact ha.prod hb } lemma filter.has_basis.prod_nhds' {ΞΉa ΞΉb : Type*} {pa : ΞΉa β†’ Prop} {pb : ΞΉb β†’ Prop} {sa : ΞΉa β†’ set Ξ±} {sb : ΞΉb β†’ set Ξ²} {ab : Ξ± Γ— Ξ²} (ha : (𝓝 ab.1).has_basis pa sa) (hb : (𝓝 ab.2).has_basis pb sb) : (𝓝 ab).has_basis (Ξ» i : ΞΉa Γ— ΞΉb, pa i.1 ∧ pb i.2) (Ξ» i, (sa i.1).prod (sb i.2)) := by { cases ab, exact ha.prod_nhds hb } instance [discrete_topology Ξ±] [discrete_topology Ξ²] : discrete_topology (Ξ± Γ— Ξ²) := ⟨eq_of_nhds_eq_nhds $ assume ⟨a, b⟩, by rw [nhds_prod_eq, nhds_discrete Ξ±, nhds_discrete Ξ², nhds_bot, filter.prod_pure_pure]⟩ lemma prod_mem_nhds_iff {s : set Ξ±} {t : set Ξ²} {a : Ξ±} {b : Ξ²} : s.prod t ∈ 𝓝 (a, b) ↔ s ∈ 𝓝 a ∧ t ∈ 𝓝 b := by rw [nhds_prod_eq, prod_mem_prod_iff] lemma prod_is_open.mem_nhds {s : set Ξ±} {t : set Ξ²} {a : Ξ±} {b : Ξ²} (ha : s ∈ 𝓝 a) (hb : t ∈ 𝓝 b) : set.prod s t ∈ 𝓝 (a, b) := prod_mem_nhds_iff.2 ⟨ha, hb⟩ lemma nhds_swap (a : Ξ±) (b : Ξ²) : 𝓝 (a, b) = (𝓝 (b, a)).map prod.swap := by rw [nhds_prod_eq, filter.prod_comm, nhds_prod_eq]; refl lemma filter.tendsto.prod_mk_nhds {Ξ³} {a : Ξ±} {b : Ξ²} {f : filter Ξ³} {ma : Ξ³ β†’ Ξ±} {mb : Ξ³ β†’ Ξ²} (ha : tendsto ma f (𝓝 a)) (hb : tendsto mb f (𝓝 b)) : tendsto (Ξ»c, (ma c, mb c)) f (𝓝 (a, b)) := by rw [nhds_prod_eq]; exact filter.tendsto.prod_mk ha hb lemma filter.eventually.curry_nhds {p : Ξ± Γ— Ξ² β†’ Prop} {x : Ξ±} {y : Ξ²} (h : βˆ€αΆ  x in 𝓝 (x, y), p x) : βˆ€αΆ  x' in 𝓝 x, βˆ€αΆ  y' in 𝓝 y, p (x', y') := by { rw [nhds_prod_eq] at h, exact h.curry } lemma continuous_at.prod {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {x : Ξ±} (hf : continuous_at f x) (hg : continuous_at g x) : continuous_at (Ξ»x, (f x, g x)) x := hf.prod_mk_nhds hg lemma continuous_at.prod_map {f : Ξ± β†’ Ξ³} {g : Ξ² β†’ Ξ΄} {p : Ξ± Γ— Ξ²} (hf : continuous_at f p.fst) (hg : continuous_at g p.snd) : continuous_at (Ξ» p : Ξ± Γ— Ξ², (f p.1, g p.2)) p := (hf.comp continuous_at_fst).prod (hg.comp continuous_at_snd) lemma continuous_at.prod_map' {f : Ξ± β†’ Ξ³} {g : Ξ² β†’ Ξ΄} {x : Ξ±} {y : Ξ²} (hf : continuous_at f x) (hg : continuous_at g y) : continuous_at (Ξ» p : Ξ± Γ— Ξ², (f p.1, g p.2)) (x, y) := have hf : continuous_at f (x, y).fst, from hf, have hg : continuous_at g (x, y).snd, from hg, hf.prod_map hg lemma prod_generate_from_generate_from_eq {Ξ± Ξ² : Type*} {s : set (set Ξ±)} {t : set (set Ξ²)} (hs : ⋃₀ s = univ) (ht : ⋃₀ t = univ) : @prod.topological_space Ξ± Ξ² (generate_from s) (generate_from t) = generate_from {g | βˆƒu∈s, βˆƒv∈t, g = set.prod u v} := let G := generate_from {g | βˆƒu∈s, βˆƒv∈t, g = set.prod u v} in le_antisymm (le_generate_from $ assume g ⟨u, hu, v, hv, g_eq⟩, g_eq.symm β–Έ @is_open.prod _ _ (generate_from s) (generate_from t) _ _ (generate_open.basic _ hu) (generate_open.basic _ hv)) (le_inf (coinduced_le_iff_le_induced.mp $ le_generate_from $ assume u hu, have (⋃v∈t, set.prod u v) = prod.fst ⁻¹' u, from calc (⋃v∈t, set.prod u v) = set.prod u univ : set.ext $ assume ⟨a, b⟩, by rw ← ht; simp [and.left_comm] {contextual:=tt} ... = prod.fst ⁻¹' u : by simp [set.prod, preimage], show G.is_open (prod.fst ⁻¹' u), from this β–Έ @is_open_Union _ _ G _ $ assume v, @is_open_Union _ _ G _ $ assume hv, generate_open.basic _ ⟨_, hu, _, hv, rfl⟩) (coinduced_le_iff_le_induced.mp $ le_generate_from $ assume v hv, have (⋃u∈s, set.prod u v) = prod.snd ⁻¹' v, from calc (⋃u∈s, set.prod u v) = set.prod univ v: set.ext $ assume ⟨a, b⟩, by rw [←hs]; by_cases b ∈ v; simp [h] {contextual:=tt} ... = prod.snd ⁻¹' v : by simp [set.prod, preimage], show G.is_open (prod.snd ⁻¹' v), from this β–Έ @is_open_Union _ _ G _ $ assume u, @is_open_Union _ _ G _ $ assume hu, generate_open.basic _ ⟨_, hu, _, hv, rfl⟩)) lemma prod_eq_generate_from : prod.topological_space = generate_from {g | βˆƒ(s:set Ξ±) (t:set Ξ²), is_open s ∧ is_open t ∧ g = set.prod s t} := le_antisymm (le_generate_from $ assume g ⟨s, t, hs, ht, g_eq⟩, g_eq.symm β–Έ hs.prod ht) (le_inf (ball_image_of_ball $ Ξ»t ht, generate_open.basic _ ⟨t, univ, by simpa [set.prod_eq] using ht⟩) (ball_image_of_ball $ Ξ»t ht, generate_open.basic _ ⟨univ, t, by simpa [set.prod_eq] using ht⟩)) lemma is_open_prod_iff {s : set (Ξ±Γ—Ξ²)} : is_open s ↔ (βˆ€a b, (a, b) ∈ s β†’ βˆƒu v, is_open u ∧ is_open v ∧ a ∈ u ∧ b ∈ v ∧ set.prod u v βŠ† s) := begin rw [is_open_iff_nhds], simp_rw [le_principal_iff, prod.forall, ((nhds_basis_opens _).prod_nhds (nhds_basis_opens _)).mem_iff, prod.exists, exists_prop], simp only [and_assoc, and.left_comm] end lemma continuous_uncurry_of_discrete_topology_left [discrete_topology Ξ±] {f : Ξ± β†’ Ξ² β†’ Ξ³} (h : βˆ€ a, continuous (f a)) : continuous (function.uncurry f) := continuous_iff_continuous_at.2 $ Ξ» ⟨a, b⟩, by simp only [continuous_at, nhds_prod_eq, nhds_discrete Ξ±, pure_prod, tendsto_map'_iff, (∘), function.uncurry, (h a).tendsto] /-- Given a neighborhood `s` of `(x, x)`, then `(x, x)` has a square open neighborhood that is a subset of `s`. -/ lemma exists_nhds_square {s : set (Ξ± Γ— Ξ±)} {x : Ξ±} (hx : s ∈ 𝓝 (x, x)) : βˆƒU, is_open U ∧ x ∈ U ∧ set.prod U U βŠ† s := by simpa [nhds_prod_eq, (nhds_basis_opens x).prod_self.mem_iff, and.assoc, and.left_comm] using hx /-- `prod.fst` maps neighborhood of `x : Ξ± Γ— Ξ²` within the section `prod.snd ⁻¹' {x.2}` to `𝓝 x.1`. -/ lemma map_fst_nhds_within (x : Ξ± Γ— Ξ²) : map prod.fst (𝓝[prod.snd ⁻¹' {x.2}] x) = 𝓝 x.1 := begin refine le_antisymm (continuous_at_fst.mono_left inf_le_left) (Ξ» s hs, _), rcases x with ⟨x, y⟩, rw [mem_map, nhds_within, mem_inf_principal, mem_nhds_prod_iff] at hs, rcases hs with ⟨u, hu, v, hv, H⟩, simp only [prod_subset_iff, mem_singleton_iff, mem_set_of_eq, mem_preimage] at H, exact mem_sets_of_superset hu (Ξ» z hz, H _ hz _ (mem_of_mem_nhds hv) rfl) end @[simp] lemma map_fst_nhds (x : Ξ± Γ— Ξ²) : map prod.fst (𝓝 x) = 𝓝 x.1 := le_antisymm continuous_at_fst $ (map_fst_nhds_within x).symm.trans_le (map_mono inf_le_left) /-- The first projection in a product of topological spaces sends open sets to open sets. -/ lemma is_open_map_fst : is_open_map (@prod.fst Ξ± Ξ²) := is_open_map_iff_nhds_le.2 $ Ξ» x, (map_fst_nhds x).ge /-- `prod.snd` maps neighborhood of `x : Ξ± Γ— Ξ²` within the section `prod.fst ⁻¹' {x.1}` to `𝓝 x.2`. -/ lemma map_snd_nhds_within (x : Ξ± Γ— Ξ²) : map prod.snd (𝓝[prod.fst ⁻¹' {x.1}] x) = 𝓝 x.2 := begin refine le_antisymm (continuous_at_snd.mono_left inf_le_left) (Ξ» s hs, _), rcases x with ⟨x, y⟩, rw [mem_map, nhds_within, mem_inf_principal, mem_nhds_prod_iff] at hs, rcases hs with ⟨u, hu, v, hv, H⟩, simp only [prod_subset_iff, mem_singleton_iff, mem_set_of_eq, mem_preimage] at H, exact mem_sets_of_superset hv (Ξ» z hz, H _ (mem_of_mem_nhds hu) _ hz rfl) end @[simp] lemma map_snd_nhds (x : Ξ± Γ— Ξ²) : map prod.snd (𝓝 x) = 𝓝 x.2 := le_antisymm continuous_at_snd $ (map_snd_nhds_within x).symm.trans_le (map_mono inf_le_left) /-- The second projection in a product of topological spaces sends open sets to open sets. -/ lemma is_open_map_snd : is_open_map (@prod.snd Ξ± Ξ²) := is_open_map_iff_nhds_le.2 $ Ξ» x, (map_snd_nhds x).ge /-- A product set is open in a product space if and only if each factor is open, or one of them is empty -/ lemma is_open_prod_iff' {s : set Ξ±} {t : set Ξ²} : is_open (set.prod s t) ↔ (is_open s ∧ is_open t) ∨ (s = βˆ…) ∨ (t = βˆ…) := begin cases (set.prod s t).eq_empty_or_nonempty with h h, { simp [h, prod_eq_empty_iff.1 h] }, { have st : s.nonempty ∧ t.nonempty, from prod_nonempty_iff.1 h, split, { assume H : is_open (set.prod s t), refine or.inl ⟨_, _⟩, show is_open s, { rw ← fst_image_prod s st.2, exact is_open_map_fst _ H }, show is_open t, { rw ← snd_image_prod st.1 t, exact is_open_map_snd _ H } }, { assume H, simp only [st.1.ne_empty, st.2.ne_empty, not_false_iff, or_false] at H, exact H.1.prod H.2 } } end lemma closure_prod_eq {s : set Ξ±} {t : set Ξ²} : closure (set.prod s t) = set.prod (closure s) (closure t) := set.ext $ assume ⟨a, b⟩, have (𝓝 a Γ—αΆ  𝓝 b) βŠ“ π“Ÿ (set.prod s t) = (𝓝 a βŠ“ π“Ÿ s) Γ—αΆ  (𝓝 b βŠ“ π“Ÿ t), by rw [←prod_inf_prod, prod_principal_principal], by simp [closure_eq_cluster_pts, cluster_pt, nhds_prod_eq, this]; exact prod_ne_bot lemma interior_prod_eq (s : set Ξ±) (t : set Ξ²) : interior (s.prod t) = (interior s).prod (interior t) := set.ext $ Ξ» ⟨a, b⟩, by simp only [mem_interior_iff_mem_nhds, mem_prod, prod_mem_nhds_iff] lemma frontier_prod_eq (s : set Ξ±) (t : set Ξ²) : frontier (s.prod t) = (closure s).prod (frontier t) βˆͺ (frontier s).prod (closure t) := by simp only [frontier, closure_prod_eq, interior_prod_eq, prod_diff_prod] @[simp] lemma frontier_prod_univ_eq (s : set Ξ±) : frontier (s.prod (univ : set Ξ²)) = (frontier s).prod univ := by simp [frontier_prod_eq] @[simp] lemma frontier_univ_prod_eq (s : set Ξ²) : frontier ((univ : set Ξ±).prod s) = (univ : set Ξ±).prod (frontier s) := by simp [frontier_prod_eq] lemma map_mem_closure2 {s : set Ξ±} {t : set Ξ²} {u : set Ξ³} {f : Ξ± β†’ Ξ² β†’ Ξ³} {a : Ξ±} {b : Ξ²} (hf : continuous (Ξ»p:Ξ±Γ—Ξ², f p.1 p.2)) (ha : a ∈ closure s) (hb : b ∈ closure t) (hu : βˆ€a b, a ∈ s β†’ b ∈ t β†’ f a b ∈ u) : f a b ∈ closure u := have (a, b) ∈ closure (set.prod s t), by rw [closure_prod_eq]; from ⟨ha, hb⟩, show (Ξ»p:Ξ±Γ—Ξ², f p.1 p.2) (a, b) ∈ closure u, from map_mem_closure hf this $ assume ⟨a, b⟩ ⟨ha, hb⟩, hu a b ha hb lemma is_closed.prod {s₁ : set Ξ±} {sβ‚‚ : set Ξ²} (h₁ : is_closed s₁) (hβ‚‚ : is_closed sβ‚‚) : is_closed (set.prod s₁ sβ‚‚) := closure_eq_iff_is_closed.mp $ by simp only [h₁.closure_eq, hβ‚‚.closure_eq, closure_prod_eq] /-- The product of two dense sets is a dense set. -/ lemma dense.prod {s : set Ξ±} {t : set Ξ²} (hs : dense s) (ht : dense t) : dense (s.prod t) := Ξ» x, by { rw closure_prod_eq, exact ⟨hs x.1, ht x.2⟩ } /-- If `f` and `g` are maps with dense range, then `prod.map f g` has dense range. -/ lemma dense_range.prod_map {ΞΉ : Type*} {ΞΊ : Type*} {f : ΞΉ β†’ Ξ²} {g : ΞΊ β†’ Ξ³} (hf : dense_range f) (hg : dense_range g) : dense_range (prod.map f g) := by simpa only [dense_range, prod_range_range_eq] using hf.prod hg lemma inducing.prod_mk {f : Ξ± β†’ Ξ²} {g : Ξ³ β†’ Ξ΄} (hf : inducing f) (hg : inducing g) : inducing (Ξ»x:Ξ±Γ—Ξ³, (f x.1, g x.2)) := ⟨by rw [prod.topological_space, prod.topological_space, hf.induced, hg.induced, induced_compose, induced_compose, induced_inf, induced_compose, induced_compose]⟩ lemma embedding.prod_mk {f : Ξ± β†’ Ξ²} {g : Ξ³ β†’ Ξ΄} (hf : embedding f) (hg : embedding g) : embedding (Ξ»x:Ξ±Γ—Ξ³, (f x.1, g x.2)) := { inj := assume ⟨x₁, xβ‚‚βŸ© ⟨y₁, yβ‚‚βŸ©, by simp; exact assume h₁ hβ‚‚, ⟨hf.inj h₁, hg.inj hβ‚‚βŸ©, ..hf.to_inducing.prod_mk hg.to_inducing } protected lemma is_open_map.prod {f : Ξ± β†’ Ξ²} {g : Ξ³ β†’ Ξ΄} (hf : is_open_map f) (hg : is_open_map g) : is_open_map (Ξ» p : Ξ± Γ— Ξ³, (f p.1, g p.2)) := begin rw [is_open_map_iff_nhds_le], rintros ⟨a, b⟩, rw [nhds_prod_eq, nhds_prod_eq, ← filter.prod_map_map_eq], exact filter.prod_mono (is_open_map_iff_nhds_le.1 hf a) (is_open_map_iff_nhds_le.1 hg b) end protected lemma open_embedding.prod {f : Ξ± β†’ Ξ²} {g : Ξ³ β†’ Ξ΄} (hf : open_embedding f) (hg : open_embedding g) : open_embedding (Ξ»x:Ξ±Γ—Ξ³, (f x.1, g x.2)) := open_embedding_of_embedding_open (hf.1.prod_mk hg.1) (hf.is_open_map.prod hg.is_open_map) lemma embedding_graph {f : Ξ± β†’ Ξ²} (hf : continuous f) : embedding (Ξ»x, (x, f x)) := embedding_of_embedding_compose (continuous_id.prod_mk hf) continuous_fst embedding_id end prod section sum open sum variables [topological_space Ξ±] [topological_space Ξ²] [topological_space Ξ³] @[continuity] lemma continuous_inl : continuous (@inl Ξ± Ξ²) := continuous_sup_rng_left continuous_coinduced_rng @[continuity] lemma continuous_inr : continuous (@inr Ξ± Ξ²) := continuous_sup_rng_right continuous_coinduced_rng @[continuity] lemma continuous_sum_rec {f : Ξ± β†’ Ξ³} {g : Ξ² β†’ Ξ³} (hf : continuous f) (hg : continuous g) : @continuous (Ξ± βŠ• Ξ²) Ξ³ _ _ (@sum.rec Ξ± Ξ² (Ξ»_, Ξ³) f g) := begin apply continuous_sup_dom; rw continuous_def at hf hg ⊒; assumption end lemma is_open_sum_iff {s : set (Ξ± βŠ• Ξ²)} : is_open s ↔ is_open (inl ⁻¹' s) ∧ is_open (inr ⁻¹' s) := iff.rfl lemma is_open_map_sum {f : Ξ± βŠ• Ξ² β†’ Ξ³} (h₁ : is_open_map (Ξ» a, f (inl a))) (hβ‚‚ : is_open_map (Ξ» b, f (inr b))) : is_open_map f := begin intros u hu, rw is_open_sum_iff at hu, cases hu with hu₁ huβ‚‚, have : u = inl '' (inl ⁻¹' u) βˆͺ inr '' (inr ⁻¹' u), { ext (_|_); simp }, rw [this, set.image_union, set.image_image, set.image_image], exact is_open.union (h₁ _ hu₁) (hβ‚‚ _ huβ‚‚) end lemma embedding_inl : embedding (@inl Ξ± Ξ²) := { induced := begin unfold sum.topological_space, apply le_antisymm, { rw ← coinduced_le_iff_le_induced, exact le_sup_left }, { intros u hu, existsi (inl '' u), change (is_open (inl ⁻¹' (@inl Ξ± Ξ² '' u)) ∧ is_open (inr ⁻¹' (@inl Ξ± Ξ² '' u))) ∧ inl ⁻¹' (inl '' u) = u, have : inl ⁻¹' (@inl Ξ± Ξ² '' u) = u := preimage_image_eq u (Ξ» _ _, inl.inj_iff.mp), rw this, have : inr ⁻¹' (@inl Ξ± Ξ² '' u) = βˆ… := eq_empty_iff_forall_not_mem.mpr (assume a ⟨b, _, h⟩, inl_ne_inr h), rw this, exact ⟨⟨hu, is_open_empty⟩, rfl⟩ } end, inj := Ξ» _ _, inl.inj_iff.mp } lemma embedding_inr : embedding (@inr Ξ± Ξ²) := { induced := begin unfold sum.topological_space, apply le_antisymm, { rw ← coinduced_le_iff_le_induced, exact le_sup_right }, { intros u hu, existsi (inr '' u), change (is_open (inl ⁻¹' (@inr Ξ± Ξ² '' u)) ∧ is_open (inr ⁻¹' (@inr Ξ± Ξ² '' u))) ∧ inr ⁻¹' (inr '' u) = u, have : inl ⁻¹' (@inr Ξ± Ξ² '' u) = βˆ… := eq_empty_iff_forall_not_mem.mpr (assume b ⟨a, _, h⟩, inr_ne_inl h), rw this, have : inr ⁻¹' (@inr Ξ± Ξ² '' u) = u := preimage_image_eq u (Ξ» _ _, inr.inj_iff.mp), rw this, exact ⟨⟨is_open_empty, hu⟩, rfl⟩ } end, inj := Ξ» _ _, inr.inj_iff.mp } lemma is_open_range_inl : is_open (range (inl : Ξ± β†’ Ξ± βŠ• Ξ²)) := is_open_sum_iff.2 $ by simp lemma is_open_range_inr : is_open (range (inr : Ξ² β†’ Ξ± βŠ• Ξ²)) := is_open_sum_iff.2 $ by simp lemma open_embedding_inl : open_embedding (inl : Ξ± β†’ Ξ± βŠ• Ξ²) := { open_range := is_open_range_inl, .. embedding_inl } lemma open_embedding_inr : open_embedding (inr : Ξ² β†’ Ξ± βŠ• Ξ²) := { open_range := is_open_range_inr, .. embedding_inr } end sum section subtype variables [topological_space Ξ±] [topological_space Ξ²] [topological_space Ξ³] {p : Ξ± β†’ Prop} lemma embedding_subtype_coe : embedding (coe : subtype p β†’ Ξ±) := ⟨⟨rfl⟩, subtype.coe_injective⟩ lemma closed_embedding_subtype_coe (h : is_closed {a | p a}) : closed_embedding (coe : subtype p β†’ Ξ±) := ⟨embedding_subtype_coe, by rwa [subtype.range_coe_subtype]⟩ @[continuity] lemma continuous_subtype_val : continuous (@subtype.val Ξ± p) := continuous_induced_dom lemma continuous_subtype_coe : continuous (coe : subtype p β†’ Ξ±) := continuous_subtype_val lemma is_open.open_embedding_subtype_coe {s : set Ξ±} (hs : is_open s) : open_embedding (coe : s β†’ Ξ±) := { induced := rfl, inj := subtype.coe_injective, open_range := (subtype.range_coe : range coe = s).symm β–Έ hs } lemma is_open.is_open_map_subtype_coe {s : set Ξ±} (hs : is_open s) : is_open_map (coe : s β†’ Ξ±) := hs.open_embedding_subtype_coe.is_open_map lemma is_open_map.restrict {f : Ξ± β†’ Ξ²} (hf : is_open_map f) {s : set Ξ±} (hs : is_open s) : is_open_map (s.restrict f) := hf.comp hs.is_open_map_subtype_coe lemma is_closed.closed_embedding_subtype_coe {s : set Ξ±} (hs : is_closed s) : closed_embedding (coe : {x // x ∈ s} β†’ Ξ±) := { induced := rfl, inj := subtype.coe_injective, closed_range := (subtype.range_coe : range coe = s).symm β–Έ hs } @[continuity] lemma continuous_subtype_mk {f : Ξ² β†’ Ξ±} (hp : βˆ€x, p (f x)) (h : continuous f) : continuous (Ξ»x, (⟨f x, hp x⟩ : subtype p)) := continuous_induced_rng h lemma continuous_inclusion {s t : set Ξ±} (h : s βŠ† t) : continuous (inclusion h) := continuous_subtype_mk _ continuous_subtype_coe lemma continuous_at_subtype_coe {p : Ξ± β†’ Prop} {a : subtype p} : continuous_at (coe : subtype p β†’ Ξ±) a := continuous_iff_continuous_at.mp continuous_subtype_coe _ lemma map_nhds_subtype_coe_eq {a : Ξ±} (ha : p a) (h : {a | p a} ∈ 𝓝 a) : map (coe : subtype p β†’ Ξ±) (𝓝 ⟨a, ha⟩) = 𝓝 a := map_nhds_induced_of_mem $ by simpa only [subtype.coe_mk, subtype.range_coe] using h lemma nhds_subtype_eq_comap {a : Ξ±} {h : p a} : 𝓝 (⟨a, h⟩ : subtype p) = comap coe (𝓝 a) := nhds_induced _ _ lemma tendsto_subtype_rng {Ξ² : Type*} {p : Ξ± β†’ Prop} {b : filter Ξ²} {f : Ξ² β†’ subtype p} : βˆ€{a:subtype p}, tendsto f b (𝓝 a) ↔ tendsto (Ξ»x, (f x : Ξ±)) b (𝓝 (a : Ξ±)) | ⟨a, ha⟩ := by rw [nhds_subtype_eq_comap, tendsto_comap_iff, subtype.coe_mk] lemma continuous_subtype_nhds_cover {ΞΉ : Sort*} {f : Ξ± β†’ Ξ²} {c : ΞΉ β†’ Ξ± β†’ Prop} (c_cover : βˆ€x:Ξ±, βˆƒi, {x | c i x} ∈ 𝓝 x) (f_cont : βˆ€i, continuous (Ξ»(x : subtype (c i)), f x)) : continuous f := continuous_iff_continuous_at.mpr $ assume x, let ⟨i, (c_sets : {x | c i x} ∈ 𝓝 x)⟩ := c_cover x in let x' : subtype (c i) := ⟨x, mem_of_mem_nhds c_sets⟩ in calc map f (𝓝 x) = map f (map coe (𝓝 x')) : congr_arg (map f) (map_nhds_subtype_coe_eq _ $ c_sets).symm ... = map (Ξ»x:subtype (c i), f x) (𝓝 x') : rfl ... ≀ 𝓝 (f x) : continuous_iff_continuous_at.mp (f_cont i) x' lemma continuous_subtype_is_closed_cover {ΞΉ : Sort*} {f : Ξ± β†’ Ξ²} (c : ΞΉ β†’ Ξ± β†’ Prop) (h_lf : locally_finite (Ξ»i, {x | c i x})) (h_is_closed : βˆ€i, is_closed {x | c i x}) (h_cover : βˆ€x, βˆƒi, c i x) (f_cont : βˆ€i, continuous (Ξ»(x : subtype (c i)), f x)) : continuous f := continuous_iff_is_closed.mpr $ assume s hs, have βˆ€i, is_closed ((coe : {x | c i x} β†’ Ξ±) '' (f ∘ coe ⁻¹' s)), from assume i, (closed_embedding_subtype_coe (h_is_closed _)).is_closed_map _ (hs.preimage (f_cont i)), have is_closed (⋃i, (coe : {x | c i x} β†’ Ξ±) '' (f ∘ coe ⁻¹' s)), from locally_finite.is_closed_Union (h_lf.subset $ assume i x ⟨⟨x', hx'⟩, _, heq⟩, heq β–Έ hx') this, have f ⁻¹' s = (⋃i, (coe : {x | c i x} β†’ Ξ±) '' (f ∘ coe ⁻¹' s)), begin apply set.ext, have : βˆ€ (x : Ξ±), f x ∈ s ↔ βˆƒ (i : ΞΉ), c i x ∧ f x ∈ s := Ξ» x, ⟨λ hx, let ⟨i, hi⟩ := h_cover x in ⟨i, hi, hx⟩, Ξ» ⟨i, hi, hx⟩, hx⟩, simpa [and.comm, @and.left_comm (c _ _), ← exists_and_distrib_right], end, by rwa [this] lemma closure_subtype {x : {a // p a}} {s : set {a // p a}}: x ∈ closure s ↔ (x : Ξ±) ∈ closure ((coe : _ β†’ Ξ±) '' s) := closure_induced end subtype section quotient variables [topological_space Ξ±] [topological_space Ξ²] [topological_space Ξ³] variables {r : Ξ± β†’ Ξ± β†’ Prop} {s : setoid Ξ±} lemma quotient_map_quot_mk : quotient_map (@quot.mk Ξ± r) := ⟨quot.exists_rep, rfl⟩ @[continuity] lemma continuous_quot_mk : continuous (@quot.mk Ξ± r) := continuous_coinduced_rng @[continuity] lemma continuous_quot_lift {f : Ξ± β†’ Ξ²} (hr : βˆ€ a b, r a b β†’ f a = f b) (h : continuous f) : continuous (quot.lift f hr : quot r β†’ Ξ²) := continuous_coinduced_dom h lemma quotient_map_quotient_mk : quotient_map (@quotient.mk Ξ± s) := quotient_map_quot_mk lemma continuous_quotient_mk : continuous (@quotient.mk Ξ± s) := continuous_coinduced_rng lemma continuous_quotient_lift {f : Ξ± β†’ Ξ²} (hs : βˆ€ a b, a β‰ˆ b β†’ f a = f b) (h : continuous f) : continuous (quotient.lift f hs : quotient s β†’ Ξ²) := continuous_coinduced_dom h end quotient section pi variables {ΞΉ : Type*} {Ο€ : ΞΉ β†’ Type*} @[continuity] lemma continuous_pi [topological_space Ξ±] [βˆ€i, topological_space (Ο€ i)] {f : Ξ± β†’ Ξ i:ΞΉ, Ο€ i} (h : βˆ€i, continuous (Ξ»a, f a i)) : continuous f := continuous_infi_rng $ assume i, continuous_induced_rng $ h i @[continuity] lemma continuous_apply [βˆ€i, topological_space (Ο€ i)] (i : ΞΉ) : continuous (Ξ»p:Ξ i, Ο€ i, p i) := continuous_infi_dom continuous_induced_dom lemma continuous_at_apply [βˆ€i, topological_space (Ο€ i)] (i : ΞΉ) (x : Ξ  i, Ο€ i) : continuous_at (Ξ» p : Ξ  i, Ο€ i, p i) x := (continuous_apply i).continuous_at lemma filter.tendsto.apply [βˆ€i, topological_space (Ο€ i)] {l : filter Ξ±} {f : Ξ± β†’ Ξ  i, Ο€ i} {x : Ξ  i, Ο€ i} (h : tendsto f l (𝓝 x)) (i : ΞΉ) : tendsto (Ξ» a, f a i) l (𝓝 $ x i) := (continuous_at_apply i _).tendsto.comp h lemma continuous_pi_iff [topological_space Ξ±] [βˆ€ i, topological_space (Ο€ i)] {f : Ξ± β†’ Ξ  i, Ο€ i} : continuous f ↔ βˆ€ i, continuous (Ξ» y, f y i) := iff.intro (Ξ» h i, (continuous_apply i).comp h) continuous_pi lemma nhds_pi [t : βˆ€i, topological_space (Ο€ i)] {a : Ξ i, Ο€ i} : 𝓝 a = (β¨…i, comap (Ξ»x, x i) (𝓝 (a i))) := calc 𝓝 a = (β¨…i, @nhds _ (@topological_space.induced _ _ (Ξ»x:Ξ i, Ο€ i, x i) (t i)) a) : nhds_infi ... = (β¨…i, comap (Ξ»x, x i) (𝓝 (a i))) : by simp [nhds_induced] lemma tendsto_pi [t : βˆ€i, topological_space (Ο€ i)] {f : Ξ± β†’ Ξ i, Ο€ i} {g : Ξ i, Ο€ i} {u : filter Ξ±} : tendsto f u (𝓝 g) ↔ βˆ€ x, tendsto (Ξ» i, f i x) u (𝓝 (g x)) := by simp [nhds_pi, filter.tendsto_comap_iff] lemma continuous_at_pi [βˆ€ i, topological_space (Ο€ i)] [topological_space Ξ±] {f : Ξ± β†’ Ξ  i, Ο€ i} {x : Ξ±} : continuous_at f x ↔ βˆ€ i, continuous_at (Ξ» y, f y i) x := tendsto_pi lemma filter.tendsto.update [βˆ€i, topological_space (Ο€ i)] [decidable_eq ΞΉ] {l : filter Ξ±} {f : Ξ± β†’ Ξ  i, Ο€ i} {x : Ξ  i, Ο€ i} (hf : tendsto f l (𝓝 x)) (i : ΞΉ) {g : Ξ± β†’ Ο€ i} {xi : Ο€ i} (hg : tendsto g l (𝓝 xi)) : tendsto (Ξ» a, function.update (f a) i (g a)) l (𝓝 $ function.update x i xi) := tendsto_pi.2 $ Ξ» j, by { rcases em (j = i) with rfl|hj; simp [*, hf.apply] } lemma continuous_at.update [βˆ€i, topological_space (Ο€ i)] [topological_space Ξ±] [decidable_eq ΞΉ] {f : Ξ± β†’ Ξ  i, Ο€ i} {a : Ξ±} (hf : continuous_at f a) (i : ΞΉ) {g : Ξ± β†’ Ο€ i} (hg : continuous_at g a) : continuous_at (Ξ» a, function.update (f a) i (g a)) a := hf.update i hg lemma continuous.update [βˆ€i, topological_space (Ο€ i)] [topological_space Ξ±] [decidable_eq ΞΉ] {f : Ξ± β†’ Ξ  i, Ο€ i} (hf : continuous f) (i : ΞΉ) {g : Ξ± β†’ Ο€ i} (hg : continuous g) : continuous (Ξ» a, function.update (f a) i (g a)) := continuous_iff_continuous_at.2 $ Ξ» x, hf.continuous_at.update i hg.continuous_at /-- `function.update f i x` is continuous in `(f, x)`. -/ @[continuity] lemma continuous_update [βˆ€i, topological_space (Ο€ i)] [decidable_eq ΞΉ] (i : ΞΉ) : continuous (Ξ» f : (Ξ  j, Ο€ j) Γ— Ο€ i, function.update f.1 i f.2) := continuous_fst.update i continuous_snd lemma is_open_set_pi [βˆ€a, topological_space (Ο€ a)] {i : set ΞΉ} {s : Ξ a, set (Ο€ a)} (hi : finite i) (hs : βˆ€a∈i, is_open (s a)) : is_open (pi i s) := by rw [pi_def]; exact (is_open_bInter hi $ assume a ha, (hs _ ha).preimage (continuous_apply _)) lemma is_closed_set_pi [βˆ€a, topological_space (Ο€ a)] {i : set ΞΉ} {s : Ξ a, set (Ο€ a)} (hs : βˆ€a∈i, is_closed (s a)) : is_closed (pi i s) := by rw [pi_def]; exact (is_closed_Inter $ Ξ» a, is_closed_Inter $ Ξ» ha, (hs _ ha).preimage (continuous_apply _)) lemma set_pi_mem_nhds [Ξ  a, topological_space (Ο€ a)] {i : set ΞΉ} {s : Ξ  a, set (Ο€ a)} {x : Ξ  a, Ο€ a} (hi : finite i) (hs : βˆ€ a ∈ i, s a ∈ 𝓝 (x a)) : pi i s ∈ 𝓝 x := by { rw [pi_def, bInter_mem_sets hi], exact Ξ» a ha, (continuous_apply a).continuous_at (hs a ha) } lemma pi_eq_generate_from [βˆ€a, topological_space (Ο€ a)] : Pi.topological_space = generate_from {g | βˆƒ(s:Ξ a, set (Ο€ a)) (i : finset ΞΉ), (βˆ€a∈i, is_open (s a)) ∧ g = pi ↑i s} := le_antisymm (le_generate_from $ assume g ⟨s, i, hi, eq⟩, eq.symm β–Έ is_open_set_pi (finset.finite_to_set _) hi) (le_infi $ assume a s ⟨t, ht, s_eq⟩, generate_open.basic _ $ ⟨function.update (Ξ»a, univ) a t, {a}, by simpa using ht, by ext f; simp [s_eq.symm, pi]⟩) lemma pi_generate_from_eq {g : Ξ a, set (set (Ο€ a))} : @Pi.topological_space ΞΉ Ο€ (Ξ»a, generate_from (g a)) = generate_from {t | βˆƒ(s:Ξ a, set (Ο€ a)) (i : finset ΞΉ), (βˆ€a∈i, s a ∈ g a) ∧ t = pi ↑i s} := let G := {t | βˆƒ(s:Ξ a, set (Ο€ a)) (i : finset ΞΉ), (βˆ€a∈i, s a ∈ g a) ∧ t = pi ↑i s} in begin rw [pi_eq_generate_from], refine le_antisymm (generate_from_mono _) (le_generate_from _), exact assume s ⟨t, i, ht, eq⟩, ⟨t, i, assume a ha, generate_open.basic _ (ht a ha), eq⟩, { rintros s ⟨t, i, hi, rfl⟩, rw [pi_def], apply is_open_bInter (finset.finite_to_set _), assume a ha, show ((generate_from G).coinduced (Ξ»f:Ξ a, Ο€ a, f a)).is_open (t a), refine le_generate_from _ _ (hi a ha), exact assume s hs, generate_open.basic _ ⟨function.update (Ξ»a, univ) a s, {a}, by simp [hs]⟩ } end lemma pi_generate_from_eq_fintype {g : Ξ a, set (set (Ο€ a))} [fintype ΞΉ] (hg : βˆ€a, ⋃₀ g a = univ) : @Pi.topological_space ΞΉ Ο€ (Ξ»a, generate_from (g a)) = generate_from {t | βˆƒ(s:Ξ a, set (Ο€ a)), (βˆ€a, s a ∈ g a) ∧ t = pi univ s} := begin rw [pi_generate_from_eq], refine le_antisymm (generate_from_mono _) (le_generate_from _), exact assume s ⟨t, ht, eq⟩, ⟨t, finset.univ, by simp [ht, eq]⟩, { rintros s ⟨t, i, ht, rfl⟩, apply is_open_iff_forall_mem_open.2 _, assume f hf, choose c hc using show βˆ€a, βˆƒs, s ∈ g a ∧ f a ∈ s, { assume a, have : f a ∈ ⋃₀ g a, { rw [hg], apply mem_univ }, simpa }, refine ⟨pi univ (Ξ»a, if a ∈ i then t a else (c : Ξ a, set (Ο€ a)) a), _, _, _⟩, { simp [pi_if] }, { refine generate_open.basic _ ⟨_, assume a, _, rfl⟩, by_cases a ∈ i; simp [*, pi] at * }, { have : f ∈ pi {a | a βˆ‰ i} c, { simp [*, pi] at * }, simpa [pi_if, hf] } } end variables [fintype ΞΉ] [βˆ€ i, topological_space (Ο€ i)] [βˆ€ i, discrete_topology (Ο€ i)] /-- A finite product of discrete spaces is discrete. -/ instance Pi.discrete_topology : discrete_topology (Ξ  i, Ο€ i) := singletons_open_iff_discrete.mp (Ξ» x, begin rw show {x} = β‹‚ i, {y : Ξ  i, Ο€ i | y i = x i}, { ext, simp only [function.funext_iff, set.mem_singleton_iff, set.mem_Inter, set.mem_set_of_eq] }, exact is_open_Inter (Ξ» i, (continuous_apply i).is_open_preimage {x i} (is_open_discrete {x i})) end) end pi section sigma variables {ΞΉ : Type*} {Οƒ : ΞΉ β†’ Type*} [Ξ  i, topological_space (Οƒ i)] @[continuity] lemma continuous_sigma_mk {i : ΞΉ} : continuous (@sigma.mk ΞΉ Οƒ i) := continuous_supr_rng continuous_coinduced_rng lemma is_open_sigma_iff {s : set (sigma Οƒ)} : is_open s ↔ βˆ€ i, is_open (sigma.mk i ⁻¹' s) := by simp only [is_open_supr_iff, is_open_coinduced] lemma is_closed_sigma_iff {s : set (sigma Οƒ)} : is_closed s ↔ βˆ€ i, is_closed (sigma.mk i ⁻¹' s) := by simp [← is_open_compl_iff, is_open_sigma_iff] lemma is_open_map_sigma_mk {i : ΞΉ} : is_open_map (@sigma.mk ΞΉ Οƒ i) := begin intros s hs, rw is_open_sigma_iff, intro j, classical, by_cases h : i = j, { subst j, convert hs, exact set.preimage_image_eq _ sigma_mk_injective }, { convert is_open_empty, apply set.eq_empty_of_subset_empty, rintro x ⟨y, _, hy⟩, have : i = j, by cc, contradiction } end lemma is_open_range_sigma_mk {i : ΞΉ} : is_open (set.range (@sigma.mk ΞΉ Οƒ i)) := by { rw ←set.image_univ, exact is_open_map_sigma_mk _ is_open_univ } lemma is_closed_map_sigma_mk {i : ΞΉ} : is_closed_map (@sigma.mk ΞΉ Οƒ i) := begin intros s hs, rw is_closed_sigma_iff, intro j, classical, by_cases h : i = j, { subst j, convert hs, exact set.preimage_image_eq _ sigma_mk_injective }, { convert is_closed_empty, apply set.eq_empty_of_subset_empty, rintro x ⟨y, _, hy⟩, have : i = j, by cc, contradiction } end lemma is_closed_sigma_mk {i : ΞΉ} : is_closed (set.range (@sigma.mk ΞΉ Οƒ i)) := by { rw ←set.image_univ, exact is_closed_map_sigma_mk _ is_closed_univ } lemma open_embedding_sigma_mk {i : ΞΉ} : open_embedding (@sigma.mk ΞΉ Οƒ i) := open_embedding_of_continuous_injective_open continuous_sigma_mk sigma_mk_injective is_open_map_sigma_mk lemma closed_embedding_sigma_mk {i : ΞΉ} : closed_embedding (@sigma.mk ΞΉ Οƒ i) := closed_embedding_of_continuous_injective_closed continuous_sigma_mk sigma_mk_injective is_closed_map_sigma_mk lemma embedding_sigma_mk {i : ΞΉ} : embedding (@sigma.mk ΞΉ Οƒ i) := closed_embedding_sigma_mk.1 /-- A map out of a sum type is continuous if its restriction to each summand is. -/ @[continuity] lemma continuous_sigma [topological_space Ξ²] {f : sigma Οƒ β†’ Ξ²} (h : βˆ€ i, continuous (Ξ» a, f ⟨i, a⟩)) : continuous f := continuous_supr_dom (Ξ» i, continuous_coinduced_dom (h i)) @[continuity] lemma continuous_sigma_map {ΞΊ : Type*} {Ο„ : ΞΊ β†’ Type*} [Ξ  k, topological_space (Ο„ k)] {f₁ : ΞΉ β†’ ΞΊ} {fβ‚‚ : Ξ  i, Οƒ i β†’ Ο„ (f₁ i)} (hf : βˆ€ i, continuous (fβ‚‚ i)) : continuous (sigma.map f₁ fβ‚‚) := continuous_sigma $ Ξ» i, show continuous (Ξ» a, sigma.mk (f₁ i) (fβ‚‚ i a)), from continuous_sigma_mk.comp (hf i) lemma is_open_map_sigma [topological_space Ξ²] {f : sigma Οƒ β†’ Ξ²} (h : βˆ€ i, is_open_map (Ξ» a, f ⟨i, a⟩)) : is_open_map f := begin intros s hs, rw is_open_sigma_iff at hs, have : s = ⋃ i, sigma.mk i '' (sigma.mk i ⁻¹' s), { rw Union_image_preimage_sigma_mk_eq_self }, rw this, rw [image_Union], apply is_open_Union, intro i, rw [image_image], exact h i _ (hs i) end /-- The sum of embeddings is an embedding. -/ lemma embedding_sigma_map {Ο„ : ΞΉ β†’ Type*} [Ξ  i, topological_space (Ο„ i)] {f : Ξ  i, Οƒ i β†’ Ο„ i} (hf : βˆ€ i, embedding (f i)) : embedding (sigma.map id f) := begin refine ⟨⟨_⟩, function.injective_id.sigma_map (Ξ» i, (hf i).inj)⟩, refine le_antisymm (continuous_iff_le_induced.mp (continuous_sigma_map (Ξ» i, (hf i).continuous))) _, intros s hs, replace hs := is_open_sigma_iff.mp hs, have : βˆ€ i, βˆƒ t, is_open t ∧ f i ⁻¹' t = sigma.mk i ⁻¹' s, { intro i, apply is_open_induced_iff.mp, convert hs i, exact (hf i).induced.symm }, choose t ht using this, apply is_open_induced_iff.mpr, refine βŸ¨β‹ƒ i, sigma.mk i '' t i, is_open_Union (Ξ» i, is_open_map_sigma_mk _ (ht i).1), _⟩, ext ⟨i, x⟩, change (sigma.mk i (f i x) ∈ ⋃ (i : ΞΉ), sigma.mk i '' t i) ↔ x ∈ sigma.mk i ⁻¹' s, rw [←(ht i).2, mem_Union], split, { rintro ⟨j, hj⟩, rw mem_image at hj, rcases hj with ⟨y, hy₁, hyβ‚‚βŸ©, rcases sigma.mk.inj_iff.mp hyβ‚‚ with ⟨rfl, hy⟩, replace hy := eq_of_heq hy, subst y, exact hy₁ }, { intro hx, use i, rw mem_image, exact ⟨f i x, hx, rfl⟩ } end end sigma section ulift @[continuity] lemma continuous_ulift_down [topological_space Ξ±] : continuous (ulift.down : ulift.{v u} Ξ± β†’ Ξ±) := continuous_induced_dom @[continuity] lemma continuous_ulift_up [topological_space Ξ±] : continuous (ulift.up : Ξ± β†’ ulift.{v u} Ξ±) := continuous_induced_rng continuous_id end ulift lemma mem_closure_of_continuous [topological_space Ξ±] [topological_space Ξ²] {f : Ξ± β†’ Ξ²} {a : Ξ±} {s : set Ξ±} {t : set Ξ²} (hf : continuous f) (ha : a ∈ closure s) (h : maps_to f s (closure t)) : f a ∈ closure t := calc f a ∈ f '' closure s : mem_image_of_mem _ ha ... βŠ† closure (f '' s) : image_closure_subset_closure_image hf ... βŠ† closure t : closure_minimal h.image_subset is_closed_closure lemma mem_closure_of_continuous2 [topological_space Ξ±] [topological_space Ξ²] [topological_space Ξ³] {f : Ξ± β†’ Ξ² β†’ Ξ³} {a : Ξ±} {b : Ξ²} {s : set Ξ±} {t : set Ξ²} {u : set Ξ³} (hf : continuous (Ξ»p:Ξ±Γ—Ξ², f p.1 p.2)) (ha : a ∈ closure s) (hb : b ∈ closure t) (h : βˆ€a∈s, βˆ€b∈t, f a b ∈ closure u) : f a b ∈ closure u := have (a,b) ∈ closure (set.prod s t), by simp [closure_prod_eq, ha, hb], show f (a, b).1 (a, b).2 ∈ closure u, from @mem_closure_of_continuous (Ξ±Γ—Ξ²) _ _ _ (Ξ»p:Ξ±Γ—Ξ², f p.1 p.2) (a,b) _ u hf this $ assume ⟨p₁, pβ‚‚βŸ© ⟨h₁, hβ‚‚βŸ©, h p₁ h₁ pβ‚‚ hβ‚‚
6391e8f3989b18195cb56ba275f641572c5cdb5d
dd0f5513e11c52db157d2fcc8456d9401a6cd9da
/06_Inductive_Types.org.37.lean
7e7ca0ee72b4360083b934915c64cfe10bec53e4
[]
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
130
lean
/- page 90 -/ import standard inductive binary_tree := | leaf : binary_tree | node : binary_tree β†’ binary_tree β†’ binary_tree
9b3402a0a0bc4bf9aaed2a782730c54515df163c
d0c6b2ba2af981e9ab0a98f6e169262caad4b9b9
/stage0/src/Lean/Elab/Level.lean
a2000ef2069e3bbaa11ca0dc261e6bebc6a61c04
[ "Apache-2.0" ]
permissive
fizruk/lean4
953b7dcd76e78c17a0743a2c1a918394ab64bbc0
545ed50f83c570f772ade4edbe7d38a078cbd761
refs/heads/master
1,677,655,987,815
1,612,393,885,000
1,612,393,885,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,583
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Meta.LevelDefEq import Lean.Elab.Exception import Lean.Elab.Log import Lean.Elab.AutoBound namespace Lean.Elab.Level structure Context where ref : Syntax autoBoundImplicit : Bool structure State where ngen : NameGenerator mctx : MetavarContext levelNames : List Name abbrev LevelElabM := ReaderT Context (EStateM Exception State) instance : MonadRef LevelElabM where getRef := return (← read).ref withRef ref x := withReader (fun ctx => { ctx with ref := ref }) x instance : AddMessageContext LevelElabM where addMessageContext msg := pure msg instance : MonadNameGenerator LevelElabM where getNGen := return (← get).ngen setNGen ngen := modify fun s => { s with ngen := ngen } def mkFreshLevelMVar : LevelElabM Level := do let mvarId ← mkFreshId modify fun s => { s with mctx := s.mctx.addLevelMVarDecl mvarId } return mkLevelMVar mvarId partial def elabLevel (stx : Syntax) : LevelElabM Level := withRef stx do let kind := stx.getKind if kind == `Lean.Parser.Level.paren then elabLevel (stx.getArg 1) else if kind == `Lean.Parser.Level.max then let args := stx.getArg 1 |>.getArgs args[:args.size - 1].foldrM (init := ← elabLevel args.back) fun stx lvl => return mkLevelMax' (← elabLevel stx) lvl else if kind == `Lean.Parser.Level.imax then let args := stx.getArg 1 |>.getArgs args[:args.size - 1].foldrM (init := ← elabLevel args.back) fun stx lvl => return mkLevelIMax' (← elabLevel stx) lvl else if kind == `Lean.Parser.Level.hole then mkFreshLevelMVar else if kind == numLitKind then match stx.isNatLit? with | some val => return Level.ofNat val | none => throwIllFormedSyntax else if kind == identKind then let paramName := stx.getId unless (← get).levelNames.contains paramName do if (← read).autoBoundImplicit && isValidAutoBoundLevelName paramName then modify fun s => { s with levelNames := paramName :: s.levelNames } else throwError! "unknown universe level '{paramName}'" return mkLevelParam paramName else if kind == `Lean.Parser.Level.addLit then let lvl ← elabLevel (stx.getArg 0) match stx.getArg 2 |>.isNatLit? with | some val => return lvl.addOffset val | none => throwIllFormedSyntax else throwError "unexpected universe level syntax kind" end Lean.Elab.Level
3c2e7c5f3109fb34c0264e69c8389f9670fc001f
2eab05920d6eeb06665e1a6df77b3157354316ad
/src/analysis/convex/specific_functions.lean
3bca1de1375c83a08d6aefcf64dabecd948762d3
[ "Apache-2.0" ]
permissive
ayush1801/mathlib
78949b9f789f488148142221606bf15c02b960d2
ce164e28f262acbb3de6281b3b03660a9f744e3c
refs/heads/master
1,692,886,907,941
1,635,270,866,000
1,635,270,866,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
6,314
lean
/- Copyright (c) 2020 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov, SΓ©bastien GouΓ«zel -/ import analysis.calculus.mean_value import analysis.special_functions.pow /-! # Collection of convex functions In this file we prove that the following functions are convex: * `convex_on_exp` : the exponential function is convex on $(-∞, +∞)$; * `convex_on_pow_of_even` : given an even natural number $n$, the function $f(x)=x^n$ is convex on $(-∞, +∞)$; * `convex_on_pow` : for a natural $n$, the function $f(x)=x^n$ is convex on $[0, +∞)$; * `convex_on_fpow` : for an integer $m$, the function $f(x)=x^m$ is convex on $(0, +∞)$. * `convex_on_rpow : βˆ€ p : ℝ, 1 ≀ p β†’ convex_on ℝ (Ici 0) (Ξ» x, x ^ p)` * `concave_on_log_Ioi` and `concave_on_log_Iio`: log is concave on `Ioi 0` and `Iio 0` respectively. -/ open real set open_locale big_operators /-- `exp` is convex on the whole real line -/ lemma convex_on_exp : convex_on ℝ univ exp := convex_on_univ_of_deriv2_nonneg differentiable_exp (by simp) (assume x, (iter_deriv_exp 2).symm β–Έ le_of_lt (exp_pos x)) /-- `x^n`, `n : β„•` is convex on the whole real line whenever `n` is even -/ lemma convex_on_pow_of_even {n : β„•} (hn : even n) : convex_on ℝ set.univ (Ξ» x : ℝ, x^n) := begin apply convex_on_univ_of_deriv2_nonneg differentiable_pow, { simp only [deriv_pow', differentiable.mul, differentiable_const, differentiable_pow] }, { intro x, rcases nat.even.sub_even hn (nat.even_bit0 1) with ⟨k, hk⟩, rw [iter_deriv_pow, finset.prod_range_cast_nat_sub, hk, pow_mul'], exact mul_nonneg (nat.cast_nonneg _) (pow_two_nonneg _) } end /-- `x^n`, `n : β„•` is convex on `[0, +∞)` for all `n` -/ lemma convex_on_pow (n : β„•) : convex_on ℝ (Ici 0) (Ξ» x : ℝ, x^n) := begin apply convex_on_of_deriv2_nonneg (convex_Ici _) (continuous_pow n).continuous_on differentiable_on_pow, { simp only [deriv_pow'], exact (@differentiable_on_pow ℝ _ _ _).const_mul (n : ℝ) }, { intros x hx, rw [iter_deriv_pow, finset.prod_range_cast_nat_sub], exact mul_nonneg (nat.cast_nonneg _) (pow_nonneg (interior_subset hx) _) } end lemma finset.prod_nonneg_of_card_nonpos_even {Ξ± Ξ² : Type*} [linear_ordered_comm_ring Ξ²] {f : Ξ± β†’ Ξ²} [decidable_pred (Ξ» x, f x ≀ 0)] {s : finset Ξ±} (h0 : even (s.filter (Ξ» x, f x ≀ 0)).card) : 0 ≀ ∏ x in s, f x := calc 0 ≀ (∏ x in s, ((if f x ≀ 0 then (-1:Ξ²) else 1) * f x)) : finset.prod_nonneg (Ξ» x _, by { split_ifs with hx hx, by simp [hx], simp at hx ⊒, exact le_of_lt hx }) ... = _ : by rw [finset.prod_mul_distrib, finset.prod_ite, finset.prod_const_one, mul_one, finset.prod_const, neg_one_pow_eq_pow_mod_two, nat.even_iff.1 h0, pow_zero, one_mul] lemma int_prod_range_nonneg (m : β„€) (n : β„•) (hn : even n) : 0 ≀ ∏ k in finset.range n, (m - k) := begin rcases hn with ⟨n, rfl⟩, induction n with n ihn, { simp }, rw [nat.succ_eq_add_one, mul_add, mul_one, bit0, ← add_assoc, finset.prod_range_succ, finset.prod_range_succ, mul_assoc], refine mul_nonneg ihn _, generalize : (1 + 1) * n = k, cases le_or_lt m k with hmk hmk, { have : m ≀ k + 1, from hmk.trans (lt_add_one ↑k).le, exact mul_nonneg_of_nonpos_of_nonpos (sub_nonpos.2 hmk) (sub_nonpos.2 this) }, { exact mul_nonneg (sub_nonneg.2 hmk.le) (sub_nonneg.2 hmk) } end /-- `x^m`, `m : β„€` is convex on `(0, +∞)` for all `m` -/ lemma convex_on_fpow (m : β„€) : convex_on ℝ (Ioi 0) (Ξ» x : ℝ, x^m) := begin have : βˆ€ n : β„€, differentiable_on ℝ (Ξ» x, x ^ n) (Ioi (0 : ℝ)), from Ξ» n, differentiable_on_fpow _ _ (or.inl $ lt_irrefl _), apply convex_on_of_deriv2_nonneg (convex_Ioi 0); try { simp only [interior_Ioi, deriv_fpow'] }, { exact (this _).continuous_on }, { exact this _ }, { exact (this _).const_mul _ }, { intros x hx, simp only [iter_deriv_fpow, ← int.cast_coe_nat, ← int.cast_sub, ← int.cast_prod], refine mul_nonneg (int.cast_nonneg.2 _) (fpow_nonneg (le_of_lt hx) _), exact int_prod_range_nonneg _ _ (nat.even_bit0 1) } end lemma convex_on_rpow {p : ℝ} (hp : 1 ≀ p) : convex_on ℝ (Ici 0) (Ξ» x : ℝ, x^p) := begin have A : deriv (Ξ» (x : ℝ), x ^ p) = Ξ» x, p * x^(p-1), by { ext x, simp [hp] }, apply convex_on_of_deriv2_nonneg (convex_Ici 0), { exact continuous_on_id.rpow_const (Ξ» x _, or.inr (zero_le_one.trans hp)) }, { exact (differentiable_rpow_const hp).differentiable_on }, { rw A, assume x hx, replace hx : x β‰  0, by { simp at hx, exact ne_of_gt hx }, simp [differentiable_at.differentiable_within_at, hx] }, { assume x hx, replace hx : 0 < x, by simpa using hx, suffices : 0 ≀ p * ((p - 1) * x ^ (p - 1 - 1)), by simpa [ne_of_gt hx, A], apply mul_nonneg (le_trans zero_le_one hp), exact mul_nonneg (sub_nonneg_of_le hp) (rpow_nonneg_of_nonneg (le_of_lt hx) _) } end lemma concave_on_log_Ioi : concave_on ℝ (Ioi 0) log := begin have h₁ : Ioi 0 βŠ† ({0} : set ℝ)ᢜ, { intros x hx hx', rw [mem_singleton_iff] at hx', rw [hx'] at hx, exact lt_irrefl 0 hx }, refine concave_on_open_of_deriv2_nonpos (convex_Ioi 0) is_open_Ioi _ _ _, { exact differentiable_on_log.mono h₁ }, { refine ((times_cont_diff_on_log.deriv_of_open _ le_top).differentiable_on le_top).mono h₁, exact is_open_compl_singleton }, { intros x hx, rw [function.iterate_succ, function.iterate_one], change (deriv (deriv log)) x ≀ 0, rw [deriv_log', deriv_inv], exact neg_nonpos.mpr (inv_nonneg.mpr (sq_nonneg x)) } end lemma concave_on_log_Iio : concave_on ℝ (Iio 0) log := begin have h₁ : Iio 0 βŠ† ({0} : set ℝ)ᢜ, { intros x hx hx', rw [mem_singleton_iff] at hx', rw [hx'] at hx, exact lt_irrefl 0 hx }, refine concave_on_open_of_deriv2_nonpos (convex_Iio 0) is_open_Iio _ _ _, { exact differentiable_on_log.mono h₁ }, { refine ((times_cont_diff_on_log.deriv_of_open _ le_top).differentiable_on le_top).mono h₁, exact is_open_compl_singleton }, { intros x hx, rw [function.iterate_succ, function.iterate_one], change (deriv (deriv log)) x ≀ 0, rw [deriv_log', deriv_inv], exact neg_nonpos.mpr (inv_nonneg.mpr (sq_nonneg x)) } end
3020fbe922df9e283e8f02adf5abc239b2762067
4727251e0cd73359b15b664c3170e5d754078599
/src/analysis/box_integral/box/basic.lean
c1158afc42622c16e801711a5ecdbeae14ec2057
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
17,904
lean
/- Copyright (c) 2021 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import data.set.intervals.monotone import topology.algebra.order.monotone_convergence import topology.metric_space.basic /-! # Rectangular boxes in `ℝⁿ` In this file we define rectangular boxes in `ℝⁿ`. As usual, we represent `ℝⁿ` as the type of functions `ΞΉ β†’ ℝ` (usually `ΞΉ = fin n` for some `n`). When we need to interpret a box `[l, u]` as a set, we use the product `{x | βˆ€ i, l i < x i ∧ x i ≀ u i}` of half-open intervals `(l i, u i]`. We exclude `l i` because this way boxes of a partition are disjoint as sets in `ℝⁿ`. Currently, the only use cases for these constructions are the definitions of Riemann-style integrals (Riemann, Henstock-Kurzweil, McShane). ## Main definitions We use the same structure `box_integral.box` both for ambient boxes and for elements of a partition. Each box is stored as two points `lower upper : ΞΉ β†’ ℝ` and a proof of `βˆ€ i, lower i < upper i`. We define instances `has_mem (ΞΉ β†’ ℝ) (box ΞΉ)` and `has_coe_t (box ΞΉ) (set $ ΞΉ β†’ ℝ)` so that each box is interpreted as the set `{x | βˆ€ i, x i ∈ set.Ioc (I.lower i) (I.upper i)}`. This way boxes of a partition are pairwise disjoint and their union is exactly the original box. We require boxes to be nonempty, because this way coercion to sets is injective. The empty box can be represented as `βŠ₯ : with_bot (box_integral.box ΞΉ)`. We define the following operations on boxes: * coercion to `set (ΞΉ β†’ ℝ)` and `has_mem (ΞΉ β†’ ℝ) (box_integral.box ΞΉ)` as described above; * `partial_order` and `semilattice_sup` instances such that `I ≀ J` is equivalent to `(I : set (ΞΉ β†’ ℝ)) βŠ† J`; * `lattice` instances on `with_bot (box_integral.box ΞΉ)`; * `box_integral.box.Icc`: the closed box `set.Icc I.lower I.upper`; defined as a bundled monotone map from `box ΞΉ` to `set (ΞΉ β†’ ℝ)`; * `box_integral.box.face I i : box (fin n)`: a hyperface of `I : box_integral.box (fin (n + 1))`; * `box_integral.box.distortion`: the maximal ratio of two lengths of edges of a box; defined as the supremum of `nndist I.lower I.upper / nndist (I.lower i) (I.upper i)`. We also provide a convenience constructor `box_integral.box.mk' (l u : ΞΉ β†’ ℝ) : with_bot (box ΞΉ)` that returns the box `⟨l, u, _⟩` if it is nonempty and `βŠ₯` otherwise. ## Tags rectangular box -/ open set function metric filter noncomputable theory open_locale nnreal classical topological_space namespace box_integral variables {ΞΉ : Type*} /-! ### Rectangular box: definition and partial order -/ /-- A nontrivial rectangular box in `ΞΉ β†’ ℝ` with corners `lower` and `upper`. Repesents the product of half-open intervals `(lower i, upper i]`. -/ structure box (ΞΉ : Type*) := (lower upper : ΞΉ β†’ ℝ) (lower_lt_upper : βˆ€ i, lower i < upper i) attribute [simp] box.lower_lt_upper namespace box variables (I J : box ΞΉ) {x y : ΞΉ β†’ ℝ} instance : inhabited (box ΞΉ) := ⟨⟨0, 1, Ξ» i, zero_lt_one⟩⟩ lemma lower_le_upper : I.lower ≀ I.upper := Ξ» i, (I.lower_lt_upper i).le lemma lower_ne_upper (i) : I.lower i β‰  I.upper i := (I.lower_lt_upper i).ne instance : has_mem (ΞΉ β†’ ℝ) (box ΞΉ) := ⟨λ x I, βˆ€ i, x i ∈ Ioc (I.lower i) (I.upper i)⟩ instance : has_coe_t (box ΞΉ) (set $ ΞΉ β†’ ℝ) := ⟨λ I, {x | x ∈ I}⟩ @[simp] lemma mem_mk {l u x : ΞΉ β†’ ℝ} {H} : x ∈ mk l u H ↔ βˆ€ i, x i ∈ Ioc (l i) (u i) := iff.rfl @[simp, norm_cast] lemma mem_coe : x ∈ (I : set (ΞΉ β†’ ℝ)) ↔ x ∈ I := iff.rfl lemma mem_def : x ∈ I ↔ βˆ€ i, x i ∈ Ioc (I.lower i) (I.upper i) := iff.rfl lemma mem_univ_Ioc {I : box ΞΉ} : x ∈ pi univ (Ξ» i, Ioc (I.lower i) (I.upper i)) ↔ x ∈ I := mem_univ_pi lemma coe_eq_pi : (I : set (ΞΉ β†’ ℝ)) = pi univ (Ξ» i, Ioc (I.lower i) (I.upper i)) := set.ext $ Ξ» x, mem_univ_Ioc.symm @[simp] lemma upper_mem : I.upper ∈ I := Ξ» i, right_mem_Ioc.2 $ I.lower_lt_upper i lemma exists_mem : βˆƒ x, x ∈ I := ⟨_, I.upper_mem⟩ lemma nonempty_coe : set.nonempty (I : set (ΞΉ β†’ ℝ)) := I.exists_mem @[simp] lemma coe_ne_empty : (I : set (ΞΉ β†’ ℝ)) β‰  βˆ… := I.nonempty_coe.ne_empty @[simp] lemma empty_ne_coe : βˆ… β‰  (I : set (ΞΉ β†’ ℝ)) := I.coe_ne_empty.symm instance : has_le (box ΞΉ) := ⟨λ I J, βˆ€ ⦃x⦄, x ∈ I β†’ x ∈ J⟩ lemma le_def : I ≀ J ↔ βˆ€ x ∈ I, x ∈ J := iff.rfl lemma le_tfae : tfae [I ≀ J, (I : set (ΞΉ β†’ ℝ)) βŠ† J, Icc I.lower I.upper βŠ† Icc J.lower J.upper, J.lower ≀ I.lower ∧ I.upper ≀ J.upper] := begin tfae_have : 1 ↔ 2, from iff.rfl, tfae_have : 2 β†’ 3, { intro h, simpa [coe_eq_pi, closure_pi_set, lower_ne_upper] using closure_mono h }, tfae_have : 3 ↔ 4, from Icc_subset_Icc_iff I.lower_le_upper, tfae_have : 4 β†’ 2, from Ξ» h x hx i, Ioc_subset_Ioc (h.1 i) (h.2 i) (hx i), tfae_finish end variables {I J} @[simp, norm_cast] lemma coe_subset_coe : (I : set (ΞΉ β†’ ℝ)) βŠ† J ↔ I ≀ J := iff.rfl lemma le_iff_bounds : I ≀ J ↔ J.lower ≀ I.lower ∧ I.upper ≀ J.upper := (le_tfae I J).out 0 3 lemma injective_coe : injective (coe : box ΞΉ β†’ set (ΞΉ β†’ ℝ)) := begin rintros ⟨l₁, u₁, hβ‚βŸ© ⟨lβ‚‚, uβ‚‚, hβ‚‚βŸ© h, simp only [subset.antisymm_iff, coe_subset_coe, le_iff_bounds] at h, congr, exacts [le_antisymm h.2.1 h.1.1, le_antisymm h.1.2 h.2.2] end @[simp, norm_cast] lemma coe_inj : (I : set (ΞΉ β†’ ℝ)) = J ↔ I = J := injective_coe.eq_iff @[ext] lemma ext (H : βˆ€ x, x ∈ I ↔ x ∈ J) : I = J := injective_coe $ set.ext H lemma ne_of_disjoint_coe (h : disjoint (I : set (ΞΉ β†’ ℝ)) J) : I β‰  J := mt coe_inj.2 $ h.ne I.coe_ne_empty instance : partial_order (box ΞΉ) := { le := (≀), .. partial_order.lift (coe : box ΞΉ β†’ set (ΞΉ β†’ ℝ)) injective_coe } /-- Closed box corresponding to `I : box_integral.box ΞΉ`. -/ protected def Icc : box ΞΉ β†ͺo set (ΞΉ β†’ ℝ) := order_embedding.of_map_le_iff (Ξ» I : box ΞΉ, Icc I.lower I.upper) (Ξ» I J, (le_tfae I J).out 2 0) lemma Icc_def : I.Icc = Icc I.lower I.upper := rfl @[simp] lemma upper_mem_Icc (I : box ΞΉ) : I.upper ∈ I.Icc := right_mem_Icc.2 I.lower_le_upper @[simp] lemma lower_mem_Icc (I : box ΞΉ) : I.lower ∈ I.Icc := left_mem_Icc.2 I.lower_le_upper protected lemma is_compact_Icc (I : box ΞΉ) : is_compact I.Icc := is_compact_Icc lemma Icc_eq_pi : I.Icc = pi univ (Ξ» i, Icc (I.lower i) (I.upper i)) := (pi_univ_Icc _ _).symm lemma le_iff_Icc : I ≀ J ↔ I.Icc βŠ† J.Icc := (le_tfae I J).out 0 2 lemma antitone_lower : antitone (Ξ» I : box ΞΉ, I.lower) := Ξ» I J H, (le_iff_bounds.1 H).1 lemma monotone_upper : monotone (Ξ» I : box ΞΉ, I.upper) := Ξ» I J H, (le_iff_bounds.1 H).2 lemma coe_subset_Icc : ↑I βŠ† I.Icc := Ξ» x hx, ⟨λ i, (hx i).1.le, Ξ» i, (hx i).2⟩ /-! ### Supremum of two boxes -/ /-- `I βŠ” J` is the least box that includes both `I` and `J`. Since `↑I βˆͺ ↑J` is usually not a box, `↑(I βŠ” J)` is larger than `↑I βˆͺ ↑J`. -/ instance : has_sup (box ΞΉ) := ⟨λ I J, ⟨I.lower βŠ“ J.lower, I.upper βŠ” J.upper, Ξ» i, (min_le_left _ _).trans_lt $ (I.lower_lt_upper i).trans_le (le_max_left _ _)⟩⟩ instance : semilattice_sup (box ΞΉ) := { le_sup_left := Ξ» I J, le_iff_bounds.2 ⟨inf_le_left, le_sup_left⟩, le_sup_right := Ξ» I J, le_iff_bounds.2 ⟨inf_le_right, le_sup_right⟩, sup_le := Ξ» I₁ Iβ‚‚ J h₁ hβ‚‚, le_iff_bounds.2 ⟨le_inf (antitone_lower h₁) (antitone_lower hβ‚‚), sup_le (monotone_upper h₁) (monotone_upper hβ‚‚)⟩, .. box.partial_order, .. box.has_sup } /-! ### `with_bot (box ΞΉ)` In this section we define coercion from `with_bot (box ΞΉ)` to `set (ΞΉ β†’ ℝ)` by sending `βŠ₯` to `βˆ…`. -/ instance with_bot_coe : has_coe_t (with_bot (box ΞΉ)) (set (ΞΉ β†’ ℝ)) := ⟨λ o, o.elim βˆ… coe⟩ @[simp, norm_cast] lemma coe_bot : ((βŠ₯ : with_bot (box ΞΉ)) : set (ΞΉ β†’ ℝ)) = βˆ… := rfl @[simp, norm_cast] lemma coe_coe : ((I : with_bot (box ΞΉ)) : set (ΞΉ β†’ ℝ)) = I := rfl lemma is_some_iff : βˆ€ {I : with_bot (box ΞΉ)}, I.is_some ↔ (I : set (ΞΉ β†’ ℝ)).nonempty | βŠ₯ := by { erw option.is_some, simp } | (I : box ΞΉ) := by { erw option.is_some, simp [I.nonempty_coe] } lemma bUnion_coe_eq_coe (I : with_bot (box ΞΉ)) : (⋃ (J : box ΞΉ) (hJ : ↑J = I), (J : set (ΞΉ β†’ ℝ))) = I := by induction I using with_bot.rec_bot_coe; simp [with_bot.coe_eq_coe] @[simp, norm_cast] lemma with_bot_coe_subset_iff {I J : with_bot (box ΞΉ)} : (I : set (ΞΉ β†’ ℝ)) βŠ† J ↔ I ≀ J := begin induction I using with_bot.rec_bot_coe, { simp }, induction J using with_bot.rec_bot_coe, { simp [subset_empty_iff] }, simp end @[simp, norm_cast] lemma with_bot_coe_inj {I J : with_bot (box ΞΉ)} : (I : set (ΞΉ β†’ ℝ)) = J ↔ I = J := by simp only [subset.antisymm_iff, ← le_antisymm_iff, with_bot_coe_subset_iff] /-- Make a `with_bot (box ΞΉ)` from a pair of corners `l u : ΞΉ β†’ ℝ`. If `l i < u i` for all `i`, then the result is `⟨l, u, _⟩ : box ΞΉ`, otherwise it is `βŠ₯`. In any case, the result interpreted as a set in `ΞΉ β†’ ℝ` is the set `{x : ΞΉ β†’ ℝ | βˆ€ i, x i ∈ Ioc (l i) (u i)}`. -/ def mk' (l u : ΞΉ β†’ ℝ) : with_bot (box ΞΉ) := if h : βˆ€ i, l i < u i then ↑(⟨l, u, h⟩ : box ΞΉ) else βŠ₯ @[simp] lemma mk'_eq_bot {l u : ΞΉ β†’ ℝ} : mk' l u = βŠ₯ ↔ βˆƒ i, u i ≀ l i := by { rw mk', split_ifs; simpa using h } @[simp] lemma mk'_eq_coe {l u : ΞΉ β†’ ℝ} : mk' l u = I ↔ l = I.lower ∧ u = I.upper := begin cases I with lI uI hI, rw mk', split_ifs, { simp [with_bot.coe_eq_coe] }, { suffices : l = lI β†’ u β‰  uI, by simpa, rintro rfl rfl, exact h hI } end @[simp] lemma coe_mk' (l u : ΞΉ β†’ ℝ) : (mk' l u : set (ΞΉ β†’ ℝ)) = pi univ (Ξ» i, Ioc (l i) (u i)) := begin rw mk', split_ifs, { exact coe_eq_pi _ }, { rcases not_forall.mp h with ⟨i, hi⟩, rw [coe_bot, univ_pi_eq_empty], exact Ioc_eq_empty hi } end instance : has_inf (with_bot (box ΞΉ)) := ⟨λ I, with_bot.rec_bot_coe (Ξ» J, βŠ₯) (Ξ» I J, with_bot.rec_bot_coe βŠ₯ (Ξ» J, mk' (I.lower βŠ” J.lower) (I.upper βŠ“ J.upper)) J) I⟩ @[simp] lemma coe_inf (I J : with_bot (box ΞΉ)) : (↑(I βŠ“ J) : set (ΞΉ β†’ ℝ)) = I ∩ J := begin induction I using with_bot.rec_bot_coe, { change βˆ… = _, simp }, induction J using with_bot.rec_bot_coe, { change βˆ… = _, simp }, change ↑(mk' _ _) = _, simp only [coe_eq_pi, ← pi_inter_distrib, Ioc_inter_Ioc, pi.sup_apply, pi.inf_apply, coe_mk', coe_coe] end instance : lattice (with_bot (box ΞΉ)) := { inf_le_left := Ξ» I J, begin rw [← with_bot_coe_subset_iff, coe_inf], exact inter_subset_left _ _ end, inf_le_right := Ξ» I J, begin rw [← with_bot_coe_subset_iff, coe_inf], exact inter_subset_right _ _ end, le_inf := Ξ» I J₁ Jβ‚‚ h₁ hβ‚‚, begin simp only [← with_bot_coe_subset_iff, coe_inf] at *, exact subset_inter h₁ hβ‚‚ end, .. with_bot.semilattice_sup, .. box.with_bot.has_inf } @[simp, norm_cast] lemma disjoint_with_bot_coe {I J : with_bot (box ΞΉ)} : disjoint (I : set (ΞΉ β†’ ℝ)) J ↔ disjoint I J := by { simp only [disjoint, ← with_bot_coe_subset_iff, coe_inf], refl } lemma disjoint_coe : disjoint (I : with_bot (box ΞΉ)) J ↔ disjoint (I : set (ΞΉ β†’ ℝ)) J := disjoint_with_bot_coe.symm lemma not_disjoint_coe_iff_nonempty_inter : Β¬disjoint (I : with_bot (box ΞΉ)) J ↔ (I ∩ J : set (ΞΉ β†’ ℝ)).nonempty := by rw [disjoint_coe, set.not_disjoint_iff_nonempty_inter] /-! ### Hyperface of a box in `ℝⁿ⁺¹ = fin (n + 1) β†’ ℝ` -/ /-- Face of a box in `ℝⁿ⁺¹ = fin (n + 1) β†’ ℝ`: the box in `ℝⁿ = fin n β†’ ℝ` with corners at `I.lower ∘ fin.succ_above i` and `I.upper ∘ fin.succ_above i`. -/ @[simps { simp_rhs := tt }] def face {n} (I : box (fin (n + 1))) (i : fin (n + 1)) : box (fin n) := ⟨I.lower ∘ fin.succ_above i, I.upper ∘ fin.succ_above i, Ξ» j, I.lower_lt_upper _⟩ @[simp] lemma face_mk {n} (l u : fin (n + 1) β†’ ℝ) (h : βˆ€ i, l i < u i) (i : fin (n + 1)) : face ⟨l, u, h⟩ i = ⟨l ∘ fin.succ_above i, u ∘ fin.succ_above i, Ξ» j, h _⟩ := rfl @[mono] lemma face_mono {n} {I J : box (fin (n + 1))} (h : I ≀ J) (i : fin (n + 1)) : face I i ≀ face J i := Ξ» x hx i, Ioc_subset_Ioc ((le_iff_bounds.1 h).1 _) ((le_iff_bounds.1 h).2 _) (hx _) lemma monotone_face {n} (i : fin (n + 1)) : monotone (Ξ» I, face I i) := Ξ» I J h, face_mono h i lemma maps_to_insert_nth_face_Icc {n} (I : box (fin (n + 1))) {i : fin (n + 1)} {x : ℝ} (hx : x ∈ Icc (I.lower i) (I.upper i)) : maps_to (i.insert_nth x) (I.face i).Icc I.Icc := Ξ» y hy, fin.insert_nth_mem_Icc.2 ⟨hx, hy⟩ lemma maps_to_insert_nth_face {n} (I : box (fin (n + 1))) {i : fin (n + 1)} {x : ℝ} (hx : x ∈ Ioc (I.lower i) (I.upper i)) : maps_to (i.insert_nth x) (I.face i) I := Ξ» y hy, by simpa only [mem_coe, mem_def, i.forall_iff_succ_above, hx, fin.insert_nth_apply_same, fin.insert_nth_apply_succ_above, true_and] lemma continuous_on_face_Icc {X} [topological_space X] {n} {f : (fin (n + 1) β†’ ℝ) β†’ X} {I : box (fin (n + 1))} (h : continuous_on f I.Icc) {i : fin (n + 1)} {x : ℝ} (hx : x ∈ Icc (I.lower i) (I.upper i)) : continuous_on (f ∘ i.insert_nth x) (I.face i).Icc := h.comp (continuous_on_const.fin_insert_nth i continuous_on_id) (I.maps_to_insert_nth_face_Icc hx) /-! ### Covering of the interior of a box by a monotone sequence of smaller boxes -/ /-- The interior of a box. -/ protected def Ioo : box ΞΉ β†’o set (ΞΉ β†’ ℝ) := { to_fun := Ξ» I, pi univ (Ξ» i, Ioo (I.lower i) (I.upper i)), monotone' := Ξ» I J h, pi_mono $ Ξ» i hi, Ioo_subset_Ioo ((le_iff_bounds.1 h).1 i) ((le_iff_bounds.1 h).2 i) } lemma Ioo_subset_coe (I : box ΞΉ) : I.Ioo βŠ† I := Ξ» x hx i, Ioo_subset_Ioc_self (hx i trivial) protected lemma Ioo_subset_Icc (I : box ΞΉ) : I.Ioo βŠ† I.Icc := I.Ioo_subset_coe.trans coe_subset_Icc lemma Union_Ioo_of_tendsto [fintype ΞΉ] {I : box ΞΉ} {J : β„• β†’ box ΞΉ} (hJ : monotone J) (hl : tendsto (lower ∘ J) at_top (𝓝 I.lower)) (hu : tendsto (upper ∘ J) at_top (𝓝 I.upper)) : (⋃ n, (J n).Ioo) = I.Ioo := have hl' : βˆ€ i, antitone (Ξ» n, (J n).lower i), from Ξ» i, (monotone_eval i).comp_antitone (antitone_lower.comp_monotone hJ), have hu' : βˆ€ i, monotone (Ξ» n, (J n).upper i), from Ξ» i, (monotone_eval i).comp (monotone_upper.comp hJ), calc (⋃ n, (J n).Ioo) = pi univ (Ξ» i, ⋃ n, Ioo ((J n).lower i) ((J n).upper i)) : Union_univ_pi_of_monotone (Ξ» i, (hl' i).Ioo (hu' i)) ... = I.Ioo : pi_congr rfl (Ξ» i hi, Union_Ioo_of_mono_of_is_glb_of_is_lub (hl' i) (hu' i) (is_glb_of_tendsto_at_top (hl' i) (tendsto_pi_nhds.1 hl _)) (is_lub_of_tendsto_at_top (hu' i) (tendsto_pi_nhds.1 hu _))) lemma exists_seq_mono_tendsto (I : box ΞΉ) : βˆƒ J : β„• β†’o box ΞΉ, (βˆ€ n, (J n).Icc βŠ† I.Ioo) ∧ tendsto (lower ∘ J) at_top (𝓝 I.lower) ∧ tendsto (upper ∘ J) at_top (𝓝 I.upper) := begin choose a b ha_anti hb_mono ha_mem hb_mem hab ha_tendsto hb_tendsto using Ξ» i, exists_seq_strict_anti_strict_mono_tendsto (I.lower_lt_upper i), exact ⟨⟨λ k, ⟨flip a k, flip b k, Ξ» i, hab _ _ _⟩, Ξ» k l hkl, le_iff_bounds.2 ⟨λ i, (ha_anti i).antitone hkl, Ξ» i, (hb_mono i).monotone hkl⟩⟩, Ξ» n x hx i hi, ⟨(ha_mem _ _).1.trans_le (hx.1 _), (hx.2 _).trans_lt (hb_mem _ _).2⟩, tendsto_pi_nhds.2 ha_tendsto, tendsto_pi_nhds.2 hb_tendsto⟩ end section distortion variable [fintype ΞΉ] /-- The distortion of a box `I` is the maximum of the ratios of the lengths of its edges. It is defined as the maximum of the ratios `nndist I.lower I.upper / nndist (I.lower i) (I.upper i)`. -/ def distortion (I : box ΞΉ) : ℝβ‰₯0 := finset.univ.sup $ Ξ» i : ΞΉ, nndist I.lower I.upper / nndist (I.lower i) (I.upper i) lemma distortion_eq_of_sub_eq_div {I J : box ΞΉ} {r : ℝ} (h : βˆ€ i, I.upper i - I.lower i = (J.upper i - J.lower i) / r) : distortion I = distortion J := begin simp only [distortion, nndist_pi_def, real.nndist_eq', h, real.nnabs.map_div], congr' 1 with i, have : 0 < r, { by_contra hr, have := div_nonpos_of_nonneg_of_nonpos (sub_nonneg.2 $ J.lower_le_upper i) (not_lt.1 hr), rw ← h at this, exact this.not_lt (sub_pos.2 $ I.lower_lt_upper i) }, simp only [nnreal.finset_sup_div, div_div_div_cancel_right _ (real.nnabs.map_ne_zero.2 this.ne')] end lemma nndist_le_distortion_mul (I : box ΞΉ) (i : ΞΉ) : nndist I.lower I.upper ≀ I.distortion * nndist (I.lower i) (I.upper i) := calc nndist I.lower I.upper = (nndist I.lower I.upper / nndist (I.lower i) (I.upper i)) * nndist (I.lower i) (I.upper i) : (div_mul_cancel _ $ mt nndist_eq_zero.1 (I.lower_lt_upper i).ne).symm ... ≀ I.distortion * nndist (I.lower i) (I.upper i) : mul_le_mul_right' (finset.le_sup $ finset.mem_univ i) _ lemma dist_le_distortion_mul (I : box ΞΉ) (i : ΞΉ) : dist I.lower I.upper ≀ I.distortion * (I.upper i - I.lower i) := have A : I.lower i - I.upper i < 0, from sub_neg.2 (I.lower_lt_upper i), by simpa only [← nnreal.coe_le_coe, ← dist_nndist, nnreal.coe_mul, real.dist_eq, abs_of_neg A, neg_sub] using I.nndist_le_distortion_mul i lemma diam_Icc_le_of_distortion_le (I : box ΞΉ) (i : ΞΉ) {c : ℝβ‰₯0} (h : I.distortion ≀ c) : diam I.Icc ≀ c * (I.upper i - I.lower i) := have (0 : ℝ) ≀ c * (I.upper i - I.lower i), from mul_nonneg c.coe_nonneg (sub_nonneg.2 $ I.lower_le_upper _), diam_le_of_forall_dist_le this $ Ξ» x hx y hy, calc dist x y ≀ dist I.lower I.upper : real.dist_le_of_mem_pi_Icc hx hy ... ≀ I.distortion * (I.upper i - I.lower i) : I.dist_le_distortion_mul i ... ≀ c * (I.upper i - I.lower i) : mul_le_mul_of_nonneg_right h (sub_nonneg.2 (I.lower_le_upper i)) end distortion end box end box_integral
0d6cd70798b5eb2e5937b64c712986309f414deb
4bcaca5dc83d49803f72b7b5920b75b6e7d9de2d
/tests/lean/termination_by.lean
0aa90a62a02c5e15816bc51a0bb5d0a3ec554633
[ "Apache-2.0" ]
permissive
subfish-zhou/leanprover-zh_CN.github.io
30b9fba9bd790720bd95764e61ae796697d2f603
8b2985d4a3d458ceda9361ac454c28168d920d3f
refs/heads/master
1,689,709,967,820
1,632,503,056,000
1,632,503,056,000
409,962,097
1
0
null
null
null
null
UTF-8
Lean
false
false
923
lean
mutual inductive Even : Nat β†’ Prop | base : Even 0 | step : Odd n β†’ Even (n+1) inductive Odd : Nat β†’ Prop | step : Even n β†’ Odd (n+1) end termination_by measure mutual def f (n : Nat) := if n == 0 then 0 else f (n / 2) + 1 termination_by measure end mutual def f (n : Nat) := match n with | 0 => 1 | n+1 => f n * 2 end termination_by measure def g (n : Nat) := match n with | 0 => 1 | n+1 => g n * 3 termination_by measure def g' (n : Nat) := match n with | 0 => 1 | n+1 => g' n * 3 termination_by h => measure namespace Test mutual def f : Nat β†’ Ξ± β†’ Ξ± β†’ Ξ± | 0, a, b => a | n+1, a, b => g n a b |>.1 def g : Nat β†’ Ξ± β†’ Ξ± β†’ (Ξ± Γ— Ξ±) | 0, a, b => (a, b) | n+1, a, b => (h n a b, a) def h : Nat β†’ Ξ± β†’ Ξ± β†’ Ξ± | 0, a, b => b | n+1, a, b => f n a b end termination_by f => measure g => measure end Test
a768ee59f813ebd7548334c9720f6cc728581584
e0f9ba56b7fedc16ef8697f6caeef5898b435143
/src/data/finsupp.lean
bfd7de7bb016e0441453484589d5e32c367224c1
[ "Apache-2.0" ]
permissive
anrddh/mathlib
6a374da53c7e3a35cb0298b0cd67824efef362b4
a4266a01d2dcb10de19369307c986d038c7bb6a6
refs/heads/master
1,656,710,827,909
1,589,560,456,000
1,589,560,456,000
264,271,800
0
0
Apache-2.0
1,589,568,062,000
1,589,568,061,000
null
UTF-8
Lean
false
false
68,484
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, Scott Morrison -/ import algebra.module /-! # Type of functions with finite support For any type `Ξ±` and a type `Ξ²` with zero, we define the type `finsupp Ξ± Ξ²` of finitely supported functions from `Ξ±` to `Ξ²`, i.e. the functions which are zero everywhere on `Ξ±` except on a finite set. We write this in infix notation as `Ξ± β†’β‚€ Ξ²`. Functions with finite support provide the basis for the following concrete instances: * `β„• β†’β‚€ Ξ±`: Polynomials (where `Ξ±` is a ring) * `(Οƒ β†’β‚€ β„•) β†’β‚€ Ξ±`: Multivariate Polynomials (again `Ξ±` is a ring, and `Οƒ` are variable names) * `Ξ± β†’β‚€ β„•`: Multisets * `Ξ± β†’β‚€ β„€`: Abelian groups freely generated by `Ξ±` * `Ξ² β†’β‚€ Ξ±`: Linear combinations over `Ξ²` where `Ξ±` is the scalar ring Most of the theory assumes that the range is a commutative monoid. This gives us the big sum operator as a powerful way to construct `finsupp` elements. A general piece of advice is to not use `Ξ± β†’β‚€ Ξ²` directly, as the type class setup might not be a good fit. Defining a copy and selecting the instances that are best suited for the application works better. ## Implementation notes This file is a `noncomputable theory` and uses classical logic throughout. ## Notation This file defines `Ξ± β†’β‚€ Ξ²` as notation for `finsupp Ξ± Ξ²`. -/ noncomputable theory open_locale classical big_operators open finset variables {Ξ± : Type*} {Ξ² : Type*} {Ξ³ : Type*} {Ξ΄ : Type*} {ΞΉ : Type*} {α₁ : Type*} {Ξ±β‚‚ : Type*} {β₁ : Type*} {Ξ²β‚‚ : Type*} /-- `finsupp Ξ± Ξ²`, denoted `Ξ± β†’β‚€ Ξ²`, is the type of functions `f : Ξ± β†’ Ξ²` such that `f x = 0` for all but finitely many `x`. -/ structure finsupp (Ξ± : Type*) (Ξ² : Type*) [has_zero Ξ²] := (support : finset Ξ±) (to_fun : Ξ± β†’ Ξ²) (mem_support_to_fun : βˆ€a, a ∈ support ↔ to_fun a β‰  0) infixr ` β†’β‚€ `:25 := finsupp namespace finsupp /-! ### Basic declarations about `finsupp` -/ section basic variable [has_zero Ξ²] instance : has_coe_to_fun (Ξ± β†’β‚€ Ξ²) := ⟨λ_, Ξ± β†’ Ξ², to_fun⟩ instance : has_zero (Ξ± β†’β‚€ Ξ²) := βŸ¨βŸ¨βˆ…, (Ξ»_, 0), Ξ» _, ⟨false.elim, Ξ» H, H rfl⟩⟩⟩ @[simp] lemma zero_apply {a : Ξ±} : (0 : Ξ± β†’β‚€ Ξ²) a = 0 := rfl @[simp] lemma support_zero : (0 : Ξ± β†’β‚€ Ξ²).support = βˆ… := rfl instance : inhabited (Ξ± β†’β‚€ Ξ²) := ⟨0⟩ @[simp] lemma mem_support_iff {f : Ξ± β†’β‚€ Ξ²} : βˆ€{a:Ξ±}, a ∈ f.support ↔ f a β‰  0 := f.mem_support_to_fun lemma not_mem_support_iff {f : Ξ± β†’β‚€ Ξ²} {a} : a βˆ‰ f.support ↔ f a = 0 := not_iff_comm.1 mem_support_iff.symm @[ext] lemma ext : βˆ€{f g : Ξ± β†’β‚€ Ξ²}, (βˆ€a, f a = g a) β†’ f = g | ⟨s, f, hf⟩ ⟨t, g, hg⟩ h := begin have : f = g, { funext a, exact h a }, subst this, have : s = t, { ext a, exact (hf a).trans (hg a).symm }, subst this end lemma ext_iff {f g : Ξ± β†’β‚€ Ξ²} : f = g ↔ (βˆ€a:Ξ±, f a = g a) := ⟨by rintros rfl a; refl, ext⟩ @[simp] lemma support_eq_empty {f : Ξ± β†’β‚€ Ξ²} : f.support = βˆ… ↔ f = 0 := ⟨assume h, ext $ assume a, by_contradiction $ Ξ» H, (finset.ext.1 h a).1 $ mem_support_iff.2 H, by rintro rfl; refl⟩ instance finsupp.decidable_eq [decidable_eq Ξ±] [decidable_eq Ξ²] : decidable_eq (Ξ± β†’β‚€ Ξ²) := assume f g, decidable_of_iff (f.support = g.support ∧ (βˆ€a∈f.support, f a = g a)) ⟨assume ⟨h₁, hβ‚‚βŸ©, ext $ assume a, if h : a ∈ f.support then hβ‚‚ a h else have hf : f a = 0, by rwa [mem_support_iff, not_not] at h, have hg : g a = 0, by rwa [h₁, mem_support_iff, not_not] at h, by rw [hf, hg], by rintro rfl; exact ⟨rfl, Ξ» _ _, rfl⟩⟩ lemma finite_supp (f : Ξ± β†’β‚€ Ξ²) : set.finite {a | f a β‰  0} := ⟨fintype.of_finset f.support (Ξ» _, mem_support_iff)⟩ lemma support_subset_iff {s : set Ξ±} {f : Ξ± β†’β‚€ Ξ²} : ↑f.support βŠ† s ↔ (βˆ€aβˆ‰s, f a = 0) := by simp only [set.subset_def, mem_coe, mem_support_iff]; exact forall_congr (assume a, @not_imp_comm _ _ (classical.dec _) (classical.dec _)) /-- Given `fintype Ξ±`, `equiv_fun_on_fintype` is the `equiv` between `Ξ± β†’β‚€ Ξ²` and `Ξ± β†’ Ξ²`. (All functions on a finite type are finitely supported.) -/ def equiv_fun_on_fintype [fintype Ξ±] : (Ξ± β†’β‚€ Ξ²) ≃ (Ξ± β†’ Ξ²) := ⟨λf a, f a, Ξ»f, mk (finset.univ.filter $ Ξ»a, f a β‰  0) f (by simp only [true_and, finset.mem_univ, iff_self, finset.mem_filter, finset.filter_congr_decidable, forall_true_iff]), begin intro f, ext a, refl end, begin intro f, ext a, refl end⟩ end basic /-! ### Declarations about `single` -/ section single variables [has_zero Ξ²] {a a' : Ξ±} {b : Ξ²} /-- `single a b` is the finitely supported function which has value `b` at `a` and zero otherwise. -/ def single (a : Ξ±) (b : Ξ²) : Ξ± β†’β‚€ Ξ² := ⟨if b = 0 then βˆ… else finset.singleton a, Ξ» a', if a = a' then b else 0, Ξ» a', begin by_cases hb : b = 0; by_cases a = a'; simp only [hb, h, if_pos, if_false, mem_singleton], { exact ⟨false.elim, Ξ» H, H rfl⟩ }, { exact ⟨false.elim, Ξ» H, H rfl⟩ }, { exact ⟨λ _, hb, Ξ» _, rfl⟩ }, { exact ⟨λ H _, h H.symm, Ξ» H, (H rfl).elim⟩ } end⟩ lemma single_apply : (single a b : Ξ± β†’β‚€ Ξ²) a' = if a = a' then b else 0 := rfl @[simp] lemma single_eq_same : (single a b : Ξ± β†’β‚€ Ξ²) a = b := if_pos rfl @[simp] lemma single_eq_of_ne (h : a β‰  a') : (single a b : Ξ± β†’β‚€ Ξ²) a' = 0 := if_neg h @[simp] lemma single_zero : (single a 0 : Ξ± β†’β‚€ Ξ²) = 0 := ext $ assume a', begin by_cases h : a = a', { rw [h, single_eq_same, zero_apply] }, { rw [single_eq_of_ne h, zero_apply] } end lemma support_single_ne_zero (hb : b β‰  0) : (single a b).support = {a} := if_neg hb lemma support_single_subset : (single a b).support βŠ† {a} := show ite _ _ _ βŠ† _, by split_ifs; [exact empty_subset _, exact subset.refl _] lemma injective_single (a : Ξ±) : function.injective (single a : Ξ² β†’ Ξ± β†’β‚€ Ξ²) := assume b₁ bβ‚‚ eq, have (single a b₁ : Ξ± β†’β‚€ Ξ²) a = (single a bβ‚‚ : Ξ± β†’β‚€ Ξ²) a, by rw eq, by rwa [single_eq_same, single_eq_same] at this lemma single_eq_single_iff (a₁ aβ‚‚ : Ξ±) (b₁ bβ‚‚ : Ξ²) : single a₁ b₁ = single aβ‚‚ bβ‚‚ ↔ ((a₁ = aβ‚‚ ∧ b₁ = bβ‚‚) ∨ (b₁ = 0 ∧ bβ‚‚ = 0)) := begin split, { assume eq, by_cases a₁ = aβ‚‚, { refine or.inl ⟨h, _⟩, rwa [h, (injective_single aβ‚‚).eq_iff] at eq }, { rw [ext_iff] at eq, have h₁ := eq a₁, have hβ‚‚ := eq aβ‚‚, simp only [single_eq_same, single_eq_of_ne h, single_eq_of_ne (ne.symm h)] at h₁ hβ‚‚, exact or.inr ⟨h₁, hβ‚‚.symm⟩ } }, { rintros (⟨rfl, rfl⟩ | ⟨rfl, rfl⟩), { refl }, { rw [single_zero, single_zero] } } end lemma single_left_inj (h : b β‰  0) : single a b = single a' b ↔ a = a' := ⟨λ H, by simpa only [h, single_eq_single_iff, and_false, or_false, eq_self_iff_true, and_true] using H, Ξ» H, by rw [H]⟩ lemma single_eq_zero : single a b = 0 ↔ b = 0 := ⟨λ h, by { rw ext_iff at h, simpa only [single_eq_same, zero_apply] using h a }, Ξ» h, by rw [h, single_zero]⟩ lemma single_swap {Ξ± Ξ² : Type*} [has_zero Ξ²] (a₁ aβ‚‚ : Ξ±) (b : Ξ²) : (single a₁ b : Ξ± β†’ Ξ²) aβ‚‚ = (single aβ‚‚ b : Ξ± β†’ Ξ²) a₁ := by simp only [single_apply]; ac_refl lemma unique_single [unique Ξ±] (x : Ξ± β†’β‚€ Ξ²) : x = single (default Ξ±) (x (default Ξ±)) := by ext i; simp only [unique.eq_default i, single_eq_same] @[simp] lemma unique_single_eq_iff [unique Ξ±] {b' : Ξ²} : single a b = single a' b' ↔ b = b' := begin rw [single_eq_single_iff], split, { rintros (⟨_, rfl⟩ | ⟨rfl, rfl⟩); refl }, { intro h, left, exact ⟨subsingleton.elim _ _, h⟩ } end end single /-! ### Declarations about `on_finset` -/ section on_finset variables [has_zero Ξ²] /-- `on_finset s f hf` is the finsupp function representing `f` restricted to the finset `s`. The function needs to be `0` outside of `s`. Use this when the set needs to be filtered anyways, otherwise a better set representation is often available. -/ def on_finset (s : finset Ξ±) (f : Ξ± β†’ Ξ²) (hf : βˆ€a, f a β‰  0 β†’ a ∈ s) : Ξ± β†’β‚€ Ξ² := ⟨s.filter (Ξ»a, f a β‰  0), f, assume a, classical.by_cases (assume h : f a = 0, by rw mem_filter; exact ⟨and.right, Ξ» H, (H h).elim⟩) (assume h : f a β‰  0, by rw mem_filter; simp only [iff_true_intro h, hf a h, true_and])⟩ @[simp] lemma on_finset_apply {s : finset Ξ±} {f : Ξ± β†’ Ξ²} {hf a} : (on_finset s f hf : Ξ± β†’β‚€ Ξ²) a = f a := rfl @[simp] lemma support_on_finset_subset {s : finset Ξ±} {f : Ξ± β†’ Ξ²} {hf} : (on_finset s f hf).support βŠ† s := filter_subset _ end on_finset /-! ### Declarations about `map_range` -/ section map_range variables [has_zero β₁] [has_zero Ξ²β‚‚] /-- The composition of `f : β₁ β†’ Ξ²β‚‚` and `g : Ξ± β†’β‚€ β₁` is `map_range f hf g : Ξ± β†’β‚€ Ξ²β‚‚`, well-defined when `f 0 = 0`. -/ def map_range (f : β₁ β†’ Ξ²β‚‚) (hf : f 0 = 0) (g : Ξ± β†’β‚€ β₁) : Ξ± β†’β‚€ Ξ²β‚‚ := on_finset g.support (f ∘ g) $ assume a, by rw [mem_support_iff, not_imp_not]; exact Ξ» H, (congr_arg f H).trans hf @[simp] lemma map_range_apply {f : β₁ β†’ Ξ²β‚‚} {hf : f 0 = 0} {g : Ξ± β†’β‚€ β₁} {a : Ξ±} : map_range f hf g a = f (g a) := rfl @[simp] lemma map_range_zero {f : β₁ β†’ Ξ²β‚‚} {hf : f 0 = 0} : map_range f hf (0 : Ξ± β†’β‚€ β₁) = 0 := ext $ Ξ» a, by simp only [hf, zero_apply, map_range_apply] lemma support_map_range {f : β₁ β†’ Ξ²β‚‚} {hf : f 0 = 0} {g : Ξ± β†’β‚€ β₁} : (map_range f hf g).support βŠ† g.support := support_on_finset_subset @[simp] lemma map_range_single {f : β₁ β†’ Ξ²β‚‚} {hf : f 0 = 0} {a : Ξ±} {b : β₁} : map_range f hf (single a b) = single a (f b) := ext $ Ξ» a', show f (ite _ _ _) = ite _ _ _, by split_ifs; [refl, exact hf] end map_range /-! ### Declarations about `emb_domain` -/ section emb_domain variables [has_zero Ξ²] /-- Given `f : α₁ β†ͺ Ξ±β‚‚` and `v : α₁ β†’β‚€ Ξ²`, `emb_domain f v : Ξ±β‚‚ β†’β‚€ Ξ²` is the finitely supported function whose value at `f a : Ξ±β‚‚` is `v a`. For a `b : Ξ±β‚‚` outside the range of `f`, it is zero. -/ def emb_domain (f : α₁ β†ͺ Ξ±β‚‚) (v : α₁ β†’β‚€ Ξ²) : Ξ±β‚‚ β†’β‚€ Ξ² := begin refine ⟨v.support.map f, Ξ»aβ‚‚, if h : aβ‚‚ ∈ v.support.map f then v (v.support.choose (Ξ»a₁, f a₁ = aβ‚‚) _) else 0, _⟩, { rcases finset.mem_map.1 h with ⟨a, ha, rfl⟩, exact exists_unique.intro a ⟨ha, rfl⟩ (assume b ⟨_, hb⟩, f.inj hb) }, { assume aβ‚‚, split_ifs, { simp only [h, true_iff, ne.def], rw [← not_mem_support_iff, not_not], apply finset.choose_mem }, { simp only [h, ne.def, ne_self_iff_false] } } end lemma support_emb_domain (f : α₁ β†ͺ Ξ±β‚‚) (v : α₁ β†’β‚€ Ξ²) : (emb_domain f v).support = v.support.map f := rfl lemma emb_domain_zero (f : α₁ β†ͺ Ξ±β‚‚) : (emb_domain f 0 : Ξ±β‚‚ β†’β‚€ Ξ²) = 0 := rfl lemma emb_domain_apply (f : α₁ β†ͺ Ξ±β‚‚) (v : α₁ β†’β‚€ Ξ²) (a : α₁) : emb_domain f v (f a) = v a := begin change dite _ _ _ = _, split_ifs; rw [finset.mem_map' f] at h, { refine congr_arg (v : α₁ β†’ Ξ²) (f.inj' _), exact finset.choose_property (Ξ»a₁, f a₁ = f a) _ _ }, { exact (not_mem_support_iff.1 h).symm } end lemma emb_domain_notin_range (f : α₁ β†ͺ Ξ±β‚‚) (v : α₁ β†’β‚€ Ξ²) (a : Ξ±β‚‚) (h : a βˆ‰ set.range f) : emb_domain f v a = 0 := begin refine dif_neg (mt (assume h, _) h), rcases finset.mem_map.1 h with ⟨a, h, rfl⟩, exact set.mem_range_self a end lemma emb_domain_inj {f : α₁ β†ͺ Ξ±β‚‚} {l₁ lβ‚‚ : α₁ β†’β‚€ Ξ²} : emb_domain f l₁ = emb_domain f lβ‚‚ ↔ l₁ = lβ‚‚ := ⟨λ h, ext $ Ξ» a, by simpa only [emb_domain_apply] using ext_iff.1 h (f a), Ξ» h, by rw h⟩ lemma emb_domain_map_range {β₁ Ξ²β‚‚ : Type*} [has_zero β₁] [has_zero Ξ²β‚‚] (f : α₁ β†ͺ Ξ±β‚‚) (g : β₁ β†’ Ξ²β‚‚) (p : α₁ β†’β‚€ β₁) (hg : g 0 = 0) : emb_domain f (map_range g hg p) = map_range g hg (emb_domain f p) := begin ext a, by_cases a ∈ set.range f, { rcases h with ⟨a', rfl⟩, rw [map_range_apply, emb_domain_apply, emb_domain_apply, map_range_apply] }, { rw [map_range_apply, emb_domain_notin_range, emb_domain_notin_range, ← hg]; assumption } end lemma single_of_emb_domain_single (l : α₁ β†’β‚€ Ξ²) (f : α₁ β†ͺ Ξ±β‚‚) (a : Ξ±β‚‚) (b : Ξ²) (hb : b β‰  0) (h : l.emb_domain f = single a b) : βˆƒ x, l = single x b ∧ f x = a := begin have h_map_support : finset.map f (l.support) = finset.singleton a, by rw [←support_emb_domain, h, support_single_ne_zero hb]; refl, have ha : a ∈ finset.map f (l.support), by simp only [h_map_support, finset.mem_singleton], rcases finset.mem_map.1 ha with ⟨c, hc₁, hcβ‚‚βŸ©, use c, split, { ext d, rw [← emb_domain_apply f l, h], by_cases h_cases : c = d, { simp only [eq.symm h_cases, hcβ‚‚, single_eq_same] }, { rw [single_apply, single_apply, if_neg, if_neg h_cases], by_contra hfd, exact h_cases (f.inj (hcβ‚‚.trans hfd)) } }, { exact hcβ‚‚ } end end emb_domain /-! ### Declarations about `zip_with` -/ section zip_with variables [has_zero Ξ²] [has_zero β₁] [has_zero Ξ²β‚‚] /-- `zip_with f hf g₁ gβ‚‚` is the finitely supported function satisfying `zip_with f hf g₁ gβ‚‚ a = f (g₁ a) (gβ‚‚ a)`, and it is well-defined when `f 0 0 = 0`. -/ def zip_with (f : β₁ β†’ Ξ²β‚‚ β†’ Ξ²) (hf : f 0 0 = 0) (g₁ : Ξ± β†’β‚€ β₁) (gβ‚‚ : Ξ± β†’β‚€ Ξ²β‚‚) : (Ξ± β†’β‚€ Ξ²) := on_finset (g₁.support βˆͺ gβ‚‚.support) (Ξ»a, f (g₁ a) (gβ‚‚ a)) $ Ξ» a H, begin simp only [mem_union, mem_support_iff, ne], rw [← not_and_distrib], rintro ⟨h₁, hβ‚‚βŸ©, rw [h₁, hβ‚‚] at H, exact H hf end @[simp] lemma zip_with_apply {f : β₁ β†’ Ξ²β‚‚ β†’ Ξ²} {hf : f 0 0 = 0} {g₁ : Ξ± β†’β‚€ β₁} {gβ‚‚ : Ξ± β†’β‚€ Ξ²β‚‚} {a : Ξ±} : zip_with f hf g₁ gβ‚‚ a = f (g₁ a) (gβ‚‚ a) := rfl lemma support_zip_with {f : β₁ β†’ Ξ²β‚‚ β†’ Ξ²} {hf : f 0 0 = 0} {g₁ : Ξ± β†’β‚€ β₁} {gβ‚‚ : Ξ± β†’β‚€ Ξ²β‚‚} : (zip_with f hf g₁ gβ‚‚).support βŠ† g₁.support βˆͺ gβ‚‚.support := support_on_finset_subset end zip_with /-! ### Declarations about `erase` -/ section erase /-- `erase a f` is the finitely supported function equal to `f` except at `a` where it is equal to `0`. -/ def erase [has_zero Ξ²] (a : Ξ±) (f : Ξ± β†’β‚€ Ξ²) : Ξ± β†’β‚€ Ξ² := ⟨f.support.erase a, (Ξ»a', if a' = a then 0 else f a'), assume a', by rw [mem_erase, mem_support_iff]; split_ifs; [exact ⟨λ H _, H.1 h, Ξ» H, (H rfl).elim⟩, exact and_iff_right h]⟩ @[simp] lemma support_erase [has_zero Ξ²] {a : Ξ±} {f : Ξ± β†’β‚€ Ξ²} : (f.erase a).support = f.support.erase a := rfl @[simp] lemma erase_same [has_zero Ξ²] {a : Ξ±} {f : Ξ± β†’β‚€ Ξ²} : (f.erase a) a = 0 := if_pos rfl @[simp] lemma erase_ne [has_zero Ξ²] {a a' : Ξ±} {f : Ξ± β†’β‚€ Ξ²} (h : a' β‰  a) : (f.erase a) a' = f a' := if_neg h @[simp] lemma erase_single [has_zero Ξ²] {a : Ξ±} {b : Ξ²} : (erase a (single a b)) = 0 := begin ext s, by_cases hs : s = a, { rw [hs, erase_same], refl }, { rw [erase_ne hs], exact single_eq_of_ne (ne.symm hs) } end lemma erase_single_ne [has_zero Ξ²] {a a' : Ξ±} {b : Ξ²} (h : a β‰  a') : (erase a (single a' b)) = single a' b := begin ext s, by_cases hs : s = a, { rw [hs, erase_same, single_eq_of_ne (h.symm)] }, { rw [erase_ne hs] } end end erase /-! ### Declarations about `sum` and `prod` In most of this section, the domain `Ξ²` is assumed to be an `add_monoid`. -/ -- [to_additive sum] for finsupp.prod doesn't work, the equation lemmas are not generated /-- `sum f g` is the sum of `g a (f a)` over the support of `f`. -/ def sum [has_zero Ξ²] [add_comm_monoid Ξ³] (f : Ξ± β†’β‚€ Ξ²) (g : Ξ± β†’ Ξ² β†’ Ξ³) : Ξ³ := f.support.sum (Ξ»a, g a (f a)) /-- `prod f g` is the product of `g a (f a)` over the support of `f`. -/ @[to_additive] def prod [has_zero Ξ²] [comm_monoid Ξ³] (f : Ξ± β†’β‚€ Ξ²) (g : Ξ± β†’ Ξ² β†’ Ξ³) : Ξ³ := f.support.prod (Ξ»a, g a (f a)) @[to_additive] lemma prod_map_range_index [has_zero β₁] [has_zero Ξ²β‚‚] [comm_monoid Ξ³] {f : β₁ β†’ Ξ²β‚‚} {hf : f 0 = 0} {g : Ξ± β†’β‚€ β₁} {h : Ξ± β†’ Ξ²β‚‚ β†’ Ξ³} (h0 : βˆ€a, h a 0 = 1) : (map_range f hf g).prod h = g.prod (Ξ»a b, h a (f b)) := finset.prod_subset support_map_range $ Ξ» _ _ H, by rw [not_mem_support_iff.1 H, h0] @[to_additive] lemma prod_zero_index [add_comm_monoid Ξ²] [comm_monoid Ξ³] {h : Ξ± β†’ Ξ² β†’ Ξ³} : (0 : Ξ± β†’β‚€ Ξ²).prod h = 1 := rfl @[to_additive] lemma prod_comm {Ξ±' : Type*} [has_zero Ξ²] {Ξ²' : Type*} [has_zero Ξ²'] (f : Ξ± β†’β‚€ Ξ²) (g : Ξ±' β†’β‚€ Ξ²') [comm_monoid Ξ³] (h : Ξ± β†’ Ξ² β†’ Ξ±' β†’ Ξ²' β†’ Ξ³) : f.prod (Ξ» x v, g.prod (Ξ» x' v', h x v x' v')) = g.prod (Ξ» x' v', f.prod (Ξ» x v, h x v x' v')) := begin dsimp [finsupp.prod], rw finset.prod_comm, end @[simp, to_additive] lemma prod_ite_eq [has_zero Ξ²] [comm_monoid Ξ³] (f : Ξ± β†’β‚€ Ξ²) (a : Ξ±) (b : Ξ± β†’ Ξ² β†’ Ξ³) : f.prod (Ξ» x v, ite (a = x) (b x v) 1) = ite (a ∈ f.support) (b a (f a)) 1 := by { dsimp [finsupp.prod], rw f.support.prod_ite_eq, } /-- A restatement of `prod_ite_eq` with the equality test reversed. -/ @[simp, to_additive "A restatement of `sum_ite_eq` with the equality test reversed."] lemma prod_ite_eq' [has_zero Ξ²] [comm_monoid Ξ³] (f : Ξ± β†’β‚€ Ξ²) (a : Ξ±) (b : Ξ± β†’ Ξ² β†’ Ξ³) : f.prod (Ξ» x v, ite (x = a) (b x v) 1) = ite (a ∈ f.support) (b a (f a)) 1 := by { dsimp [finsupp.prod], rw f.support.prod_ite_eq', } @[simp] lemma prod_pow [fintype Ξ±] [comm_monoid Ξ³] (f : Ξ± β†’β‚€ β„•) (g : Ξ± β†’ Ξ³) : f.prod (Ξ» a b, g a ^ b) = ∏ a, g a ^ (f a) := begin apply prod_subset (finset.subset_univ _), intros a _ ha, simp only [finsupp.not_mem_support_iff.mp ha, pow_zero] end section add_monoid variables [add_monoid Ξ²] @[to_additive] lemma prod_single_index [comm_monoid Ξ³] {a : Ξ±} {b : Ξ²} {h : Ξ± β†’ Ξ² β†’ Ξ³} (h_zero : h a 0 = 1) : (single a b).prod h = h a b := begin by_cases h : b = 0, { simp only [h, h_zero, single_zero]; refl }, { simp only [finsupp.prod, support_single_ne_zero h, insert_empty_eq_singleton, prod_singleton, single_eq_same] } end instance : has_add (Ξ± β†’β‚€ Ξ²) := ⟨zip_with (+) (add_zero 0)⟩ @[simp] lemma add_apply {g₁ gβ‚‚ : Ξ± β†’β‚€ Ξ²} {a : Ξ±} : (g₁ + gβ‚‚) a = g₁ a + gβ‚‚ a := rfl lemma support_add {g₁ gβ‚‚ : Ξ± β†’β‚€ Ξ²} : (g₁ + gβ‚‚).support βŠ† g₁.support βˆͺ gβ‚‚.support := support_zip_with lemma support_add_eq {g₁ gβ‚‚ : Ξ± β†’β‚€ Ξ²} (h : disjoint g₁.support gβ‚‚.support) : (g₁ + gβ‚‚).support = g₁.support βˆͺ gβ‚‚.support := le_antisymm support_zip_with $ assume a ha, (finset.mem_union.1 ha).elim (assume ha, have a βˆ‰ gβ‚‚.support, from disjoint_left.1 h ha, by simp only [mem_support_iff, not_not] at *; simpa only [add_apply, this, add_zero]) (assume ha, have a βˆ‰ g₁.support, from disjoint_right.1 h ha, by simp only [mem_support_iff, not_not] at *; simpa only [add_apply, this, zero_add]) @[simp] lemma single_add {a : Ξ±} {b₁ bβ‚‚ : Ξ²} : single a (b₁ + bβ‚‚) = single a b₁ + single a bβ‚‚ := ext $ assume a', begin by_cases h : a = a', { rw [h, add_apply, single_eq_same, single_eq_same, single_eq_same] }, { rw [add_apply, single_eq_of_ne h, single_eq_of_ne h, single_eq_of_ne h, zero_add] } end instance : add_monoid (Ξ± β†’β‚€ Ξ²) := { add_monoid . zero := 0, add := (+), add_assoc := assume ⟨s, f, hf⟩ ⟨t, g, hg⟩ ⟨u, h, hh⟩, ext $ assume a, add_assoc _ _ _, zero_add := assume ⟨s, f, hf⟩, ext $ assume a, zero_add _, add_zero := assume ⟨s, f, hf⟩, ext $ assume a, add_zero _ } instance (a : Ξ±) : is_add_monoid_hom (Ξ» g : Ξ± β†’β‚€ Ξ², g a) := { map_add := Ξ» _ _, add_apply, map_zero := zero_apply } lemma single_add_erase {a : Ξ±} {f : Ξ± β†’β‚€ Ξ²} : single a (f a) + f.erase a = f := ext $ Ξ» a', if h : a = a' then by subst h; simp only [add_apply, single_eq_same, erase_same, add_zero] else by simp only [add_apply, single_eq_of_ne h, zero_add, erase_ne (ne.symm h)] lemma erase_add_single {a : Ξ±} {f : Ξ± β†’β‚€ Ξ²} : f.erase a + single a (f a) = f := ext $ Ξ» a', if h : a = a' then by subst h; simp only [add_apply, single_eq_same, erase_same, zero_add] else by simp only [add_apply, single_eq_of_ne h, add_zero, erase_ne (ne.symm h)] @[simp] lemma erase_add (a : Ξ±) (f f' : Ξ± β†’β‚€ Ξ²) : erase a (f + f') = erase a f + erase a f' := begin ext s, by_cases hs : s = a, { rw [hs, add_apply, erase_same, erase_same, erase_same, add_zero] }, rw [add_apply, erase_ne hs, erase_ne hs, erase_ne hs, add_apply], end @[elab_as_eliminator] protected theorem induction {p : (Ξ± β†’β‚€ Ξ²) β†’ Prop} (f : Ξ± β†’β‚€ Ξ²) (h0 : p 0) (ha : βˆ€a b (f : Ξ± β†’β‚€ Ξ²), a βˆ‰ f.support β†’ b β‰  0 β†’ p f β†’ p (single a b + f)) : p f := suffices βˆ€s (f : Ξ± β†’β‚€ Ξ²), f.support = s β†’ p f, from this _ _ rfl, assume s, finset.induction_on s (Ξ» f hf, by rwa [support_eq_empty.1 hf]) $ assume a s has ih f hf, suffices p (single a (f a) + f.erase a), by rwa [single_add_erase] at this, begin apply ha, { rw [support_erase, mem_erase], exact Ξ» H, H.1 rfl }, { rw [← mem_support_iff, hf], exact mem_insert_self _ _ }, { apply ih _ _, rw [support_erase, hf, finset.erase_insert has] } end lemma inductionβ‚‚ {p : (Ξ± β†’β‚€ Ξ²) β†’ Prop} (f : Ξ± β†’β‚€ Ξ²) (h0 : p 0) (ha : βˆ€a b (f : Ξ± β†’β‚€ Ξ²), a βˆ‰ f.support β†’ b β‰  0 β†’ p f β†’ p (f + single a b)) : p f := suffices βˆ€s (f : Ξ± β†’β‚€ Ξ²), f.support = s β†’ p f, from this _ _ rfl, assume s, finset.induction_on s (Ξ» f hf, by rwa [support_eq_empty.1 hf]) $ assume a s has ih f hf, suffices p (f.erase a + single a (f a)), by rwa [erase_add_single] at this, begin apply ha, { rw [support_erase, mem_erase], exact Ξ» H, H.1 rfl }, { rw [← mem_support_iff, hf], exact mem_insert_self _ _ }, { apply ih _ _, rw [support_erase, hf, finset.erase_insert has] } end lemma map_range_add [add_monoid β₁] [add_monoid Ξ²β‚‚] {f : β₁ β†’ Ξ²β‚‚} {hf : f 0 = 0} (hf' : βˆ€ x y, f (x + y) = f x + f y) (v₁ vβ‚‚ : Ξ± β†’β‚€ β₁) : map_range f hf (v₁ + vβ‚‚) = map_range f hf v₁ + map_range f hf vβ‚‚ := ext $ Ξ» a, by simp only [hf', add_apply, map_range_apply] end add_monoid section nat_sub instance nat_sub : has_sub (Ξ± β†’β‚€ β„•) := ⟨zip_with (Ξ» m n, m - n) (nat.sub_zero 0)⟩ @[simp] lemma nat_sub_apply {g₁ gβ‚‚ : Ξ± β†’β‚€ β„•} {a : Ξ±} : (g₁ - gβ‚‚) a = g₁ a - gβ‚‚ a := rfl @[simp] lemma single_sub {a : Ξ±} {n₁ nβ‚‚ : β„•} : single a (n₁ - nβ‚‚) = single a n₁ - single a nβ‚‚ := begin ext f, by_cases h : (a = f), { rw [h, nat_sub_apply, single_eq_same, single_eq_same, single_eq_same] }, rw [nat_sub_apply, single_eq_of_ne h, single_eq_of_ne h, single_eq_of_ne h] end -- These next two lemmas are used in developing -- the partial derivative on `mv_polynomial`. lemma sub_single_one_add {a : Ξ±} {u u' : Ξ± β†’β‚€ β„•} (h : u a β‰  0) : u - single a 1 + u' = u + u' - single a 1 := begin ext b, rw [add_apply, nat_sub_apply, nat_sub_apply, add_apply], by_cases h : a = b, { rw [←h, single_eq_same], cases (u a), { contradiction }, { simp }, }, { simp [h], } end lemma add_sub_single_one {a : Ξ±} {u u' : Ξ± β†’β‚€ β„•} (h : u' a β‰  0) : u + (u' - single a 1) = u + u' - single a 1 := begin ext b, rw [add_apply, nat_sub_apply, nat_sub_apply, add_apply], by_cases h : a = b, { rw [←h, single_eq_same], cases (u' a), { contradiction }, { simp }, }, { simp [h], } end end nat_sub instance [add_comm_monoid Ξ²] : add_comm_monoid (Ξ± β†’β‚€ Ξ²) := { add_comm := assume ⟨s, f, _⟩ ⟨t, g, _⟩, ext $ assume a, add_comm _ _, .. finsupp.add_monoid } instance [add_group Ξ²] : add_group (Ξ± β†’β‚€ Ξ²) := { neg := map_range (has_neg.neg) neg_zero, add_left_neg := assume ⟨s, f, _⟩, ext $ assume x, add_left_neg _, .. finsupp.add_monoid } lemma single_multiset_sum [add_comm_monoid Ξ²] (s : multiset Ξ²) (a : Ξ±) : single a s.sum = (s.map (single a)).sum := multiset.induction_on s single_zero $ Ξ» a s ih, by rw [multiset.sum_cons, single_add, ih, multiset.map_cons, multiset.sum_cons] lemma single_finset_sum [add_comm_monoid Ξ²] (s : finset Ξ³) (f : Ξ³ β†’ Ξ²) (a : Ξ±) : single a (s.sum f) = s.sum (Ξ»b, single a (f b)) := begin transitivity, apply single_multiset_sum, rw [multiset.map_map], refl end lemma single_sum [has_zero Ξ³] [add_comm_monoid Ξ²] (s : Ξ΄ β†’β‚€ Ξ³) (f : Ξ΄ β†’ Ξ³ β†’ Ξ²) (a : Ξ±) : single a (s.sum f) = s.sum (Ξ»d c, single a (f d c)) := single_finset_sum _ _ _ @[to_additive] lemma prod_neg_index [add_group Ξ²] [comm_monoid Ξ³] {g : Ξ± β†’β‚€ Ξ²} {h : Ξ± β†’ Ξ² β†’ Ξ³} (h0 : βˆ€a, h a 0 = 1) : (-g).prod h = g.prod (Ξ»a b, h a (- b)) := prod_map_range_index h0 @[simp] lemma neg_apply [add_group Ξ²] {g : Ξ± β†’β‚€ Ξ²} {a : Ξ±} : (- g) a = - g a := rfl @[simp] lemma sub_apply [add_group Ξ²] {g₁ gβ‚‚ : Ξ± β†’β‚€ Ξ²} {a : Ξ±} : (g₁ - gβ‚‚) a = g₁ a - gβ‚‚ a := rfl @[simp] lemma support_neg [add_group Ξ²] {f : Ξ± β†’β‚€ Ξ²} : support (-f) = support f := finset.subset.antisymm support_map_range (calc support f = support (- (- f)) : congr_arg support (neg_neg _).symm ... βŠ† support (- f) : support_map_range) instance [add_comm_group Ξ²] : add_comm_group (Ξ± β†’β‚€ Ξ²) := { add_comm := add_comm, ..finsupp.add_group } @[simp] lemma sum_apply [has_zero β₁] [add_comm_monoid Ξ²] {f : α₁ β†’β‚€ β₁} {g : α₁ β†’ β₁ β†’ Ξ± β†’β‚€ Ξ²} {aβ‚‚ : Ξ±} : (f.sum g) aβ‚‚ = f.sum (Ξ»a₁ b, g a₁ b aβ‚‚) := (f.support.sum_hom (Ξ»f : Ξ± β†’β‚€ Ξ², f aβ‚‚)).symm lemma support_sum [has_zero β₁] [add_comm_monoid Ξ²] {f : α₁ β†’β‚€ β₁} {g : α₁ β†’ β₁ β†’ (Ξ± β†’β‚€ Ξ²)} : (f.sum g).support βŠ† f.support.bind (Ξ»a, (g a (f a)).support) := have βˆ€a₁ : Ξ±, f.sum (Ξ» (a : α₁) (b : β₁), (g a b) a₁) β‰  0 β†’ (βˆƒ (a : α₁), f a β‰  0 ∧ Β¬ (g a (f a)) a₁ = 0), from assume a₁ h, let ⟨a, ha, ne⟩ := finset.exists_ne_zero_of_sum_ne_zero h in ⟨a, mem_support_iff.mp ha, ne⟩, by simpa only [finset.subset_iff, mem_support_iff, finset.mem_bind, sum_apply, exists_prop] using this @[simp] lemma sum_zero [add_comm_monoid Ξ²] [add_comm_monoid Ξ³] {f : Ξ± β†’β‚€ Ξ²} : f.sum (Ξ»a b, (0 : Ξ³)) = 0 := finset.sum_const_zero @[simp] lemma sum_add [add_comm_monoid Ξ²] [add_comm_monoid Ξ³] {f : Ξ± β†’β‚€ Ξ²} {h₁ hβ‚‚ : Ξ± β†’ Ξ² β†’ Ξ³} : f.sum (Ξ»a b, h₁ a b + hβ‚‚ a b) = f.sum h₁ + f.sum hβ‚‚ := finset.sum_add_distrib @[simp] lemma sum_neg [add_comm_monoid Ξ²] [add_comm_group Ξ³] {f : Ξ± β†’β‚€ Ξ²} {h : Ξ± β†’ Ξ² β†’ Ξ³} : f.sum (Ξ»a b, - h a b) = - f.sum h := f.support.sum_hom (@has_neg.neg Ξ³ _) @[simp] lemma sum_sub [add_comm_monoid Ξ²] [add_comm_group Ξ³] {f : Ξ± β†’β‚€ Ξ²} {h₁ hβ‚‚ : Ξ± β†’ Ξ² β†’ Ξ³} : f.sum (Ξ»a b, h₁ a b - hβ‚‚ a b) = f.sum h₁ - f.sum hβ‚‚ := by rw [sub_eq_add_neg, ←sum_neg, ←sum_add]; refl @[simp] lemma sum_single [add_comm_monoid Ξ²] (f : Ξ± β†’β‚€ Ξ²) : f.sum single = f := have βˆ€a:Ξ±, f.sum (Ξ»a' b, ite (a' = a) b 0) = ({a} : finset Ξ±).sum (Ξ»a', ite (a' = a) (f a') 0), begin intro a, by_cases h : a ∈ f.support, { have : (finset.singleton a : finset Ξ±) βŠ† f.support, { simpa only [finset.subset_iff, mem_singleton, forall_eq] }, refine (finset.sum_subset this (Ξ» _ _ H, _)).symm, exact if_neg (mt mem_singleton.2 H) }, { transitivity (f.support.sum (Ξ»a, (0 : Ξ²))), { refine (finset.sum_congr rfl $ Ξ» a' ha', if_neg _), rintro rfl, exact h ha' }, { rw [sum_const_zero, insert_empty_eq_singleton, sum_singleton, if_pos rfl, not_mem_support_iff.1 h] } } end, ext $ assume a, by simp only [sum_apply, single_apply, this, insert_empty_eq_singleton, sum_singleton, if_pos] @[to_additive] lemma prod_add_index [add_comm_monoid Ξ²] [comm_monoid Ξ³] {f g : Ξ± β†’β‚€ Ξ²} {h : Ξ± β†’ Ξ² β†’ Ξ³} (h_zero : βˆ€a, h a 0 = 1) (h_add : βˆ€a b₁ bβ‚‚, h a (b₁ + bβ‚‚) = h a b₁ * h a bβ‚‚) : (f + g).prod h = f.prod h * g.prod h := have f_eq : (f.support βˆͺ g.support).prod (Ξ»a, h a (f a)) = f.prod h, from (finset.prod_subset (finset.subset_union_left _ _) $ by intros _ _ H; rw [not_mem_support_iff.1 H, h_zero]).symm, have g_eq : (f.support βˆͺ g.support).prod (Ξ»a, h a (g a)) = g.prod h, from (finset.prod_subset (finset.subset_union_right _ _) $ by intros _ _ H; rw [not_mem_support_iff.1 H, h_zero]).symm, calc (f + g).support.prod (Ξ»a, h a ((f + g) a)) = (f.support βˆͺ g.support).prod (Ξ»a, h a ((f + g) a)) : finset.prod_subset support_add $ by intros _ _ H; rw [not_mem_support_iff.1 H, h_zero] ... = (f.support βˆͺ g.support).prod (Ξ»a, h a (f a)) * (f.support βˆͺ g.support).prod (Ξ»a, h a (g a)) : by simp only [add_apply, h_add, finset.prod_mul_distrib] ... = _ : by rw [f_eq, g_eq] lemma sum_sub_index [add_comm_group Ξ²] [add_comm_group Ξ³] {f g : Ξ± β†’β‚€ Ξ²} {h : Ξ± β†’ Ξ² β†’ Ξ³} (h_sub : βˆ€a b₁ bβ‚‚, h a (b₁ - bβ‚‚) = h a b₁ - h a bβ‚‚) : (f - g).sum h = f.sum h - g.sum h := have h_zero : βˆ€a, h a 0 = 0, from assume a, have h a (0 - 0) = h a 0 - h a 0, from h_sub a 0 0, by simpa only [sub_self] using this, have h_neg : βˆ€a b, h a (- b) = - h a b, from assume a b, have h a (0 - b) = h a 0 - h a b, from h_sub a 0 b, by simpa only [h_zero, zero_sub] using this, have h_add : βˆ€a b₁ bβ‚‚, h a (b₁ + bβ‚‚) = h a b₁ + h a bβ‚‚, from assume a b₁ bβ‚‚, have h a (b₁ - (- bβ‚‚)) = h a b₁ - h a (- bβ‚‚), from h_sub a b₁ (-bβ‚‚), by simpa only [h_neg, sub_neg_eq_add] using this, calc (f - g).sum h = (f + - g).sum h : rfl ... = f.sum h + - g.sum h : by simp only [sum_add_index h_zero h_add, sum_neg_index h_zero, h_neg, sum_neg] ... = f.sum h - g.sum h : rfl @[to_additive] lemma prod_finset_sum_index [add_comm_monoid Ξ²] [comm_monoid Ξ³] {s : finset ΞΉ} {g : ΞΉ β†’ Ξ± β†’β‚€ Ξ²} {h : Ξ± β†’ Ξ² β†’ Ξ³} (h_zero : βˆ€a, h a 0 = 1) (h_add : βˆ€a b₁ bβ‚‚, h a (b₁ + bβ‚‚) = h a b₁ * h a bβ‚‚) : s.prod (Ξ»i, (g i).prod h) = (s.sum g).prod h := finset.induction_on s rfl $ Ξ» a s has ih, by rw [prod_insert has, ih, sum_insert has, prod_add_index h_zero h_add] @[to_additive] lemma prod_sum_index [add_comm_monoid β₁] [add_comm_monoid Ξ²] [comm_monoid Ξ³] {f : α₁ β†’β‚€ β₁} {g : α₁ β†’ β₁ β†’ Ξ± β†’β‚€ Ξ²} {h : Ξ± β†’ Ξ² β†’ Ξ³} (h_zero : βˆ€a, h a 0 = 1) (h_add : βˆ€a b₁ bβ‚‚, h a (b₁ + bβ‚‚) = h a b₁ * h a bβ‚‚) : (f.sum g).prod h = f.prod (Ξ»a b, (g a b).prod h) := (prod_finset_sum_index h_zero h_add).symm lemma multiset_sum_sum_index [add_comm_monoid Ξ²] [add_comm_monoid Ξ³] (f : multiset (Ξ± β†’β‚€ Ξ²)) (h : Ξ± β†’ Ξ² β†’ Ξ³) (hβ‚€ : βˆ€a, h a 0 = 0) (h₁ : βˆ€ (a : Ξ±) (b₁ bβ‚‚ : Ξ²), h a (b₁ + bβ‚‚) = h a b₁ + h a bβ‚‚) : (f.sum.sum h) = (f.map $ Ξ»g:Ξ± β†’β‚€ Ξ², g.sum h).sum := multiset.induction_on f rfl $ assume a s ih, by rw [multiset.sum_cons, multiset.map_cons, multiset.sum_cons, sum_add_index hβ‚€ h₁, ih] lemma multiset_map_sum [has_zero Ξ²] {f : Ξ± β†’β‚€ Ξ²} {m : Ξ³ β†’ Ξ΄} {h : Ξ± β†’ Ξ² β†’ multiset Ξ³} : multiset.map m (f.sum h) = f.sum (Ξ»a b, (h a b).map m) := (f.support.sum_hom _).symm lemma multiset_sum_sum [has_zero Ξ²] [add_comm_monoid Ξ³] {f : Ξ± β†’β‚€ Ξ²} {h : Ξ± β†’ Ξ² β†’ multiset Ξ³} : multiset.sum (f.sum h) = f.sum (Ξ»a b, multiset.sum (h a b)) := (f.support.sum_hom multiset.sum).symm section map_range variables [add_comm_monoid β₁] [add_comm_monoid Ξ²β‚‚] (f : β₁ β†’ Ξ²β‚‚) [hf : is_add_monoid_hom f] instance is_add_monoid_hom_map_range : is_add_monoid_hom (map_range f hf.map_zero : (Ξ± β†’β‚€ β₁) β†’ (Ξ± β†’β‚€ Ξ²β‚‚)) := { map_zero := map_range_zero, map_add := Ξ» a b, map_range_add hf.map_add _ _ } lemma map_range_multiset_sum (m : multiset (Ξ± β†’β‚€ β₁)) : map_range f hf.map_zero m.sum = (m.map $ Ξ»x, map_range f hf.map_zero x).sum := (m.sum_hom (map_range f hf.map_zero)).symm lemma map_range_finset_sum {ΞΉ : Type*} (s : finset ΞΉ) (g : ΞΉ β†’ (Ξ± β†’β‚€ β₁)) : map_range f hf.map_zero (s.sum g) = s.sum (Ξ»x, map_range f hf.map_zero (g x)) := by rw [finset.sum.equations._eqn_1, map_range_multiset_sum, multiset.map_map]; refl end map_range /-! ### Declarations about `map_domain` -/ section map_domain variables [add_comm_monoid Ξ²] {v v₁ vβ‚‚ : Ξ± β†’β‚€ Ξ²} /-- Given `f : α₁ β†’ Ξ±β‚‚` and `v : α₁ β†’β‚€ Ξ²`, `map_domain f v : Ξ±β‚‚ β†’β‚€ Ξ²` is the finitely supported function whose value at `a : Ξ±β‚‚` is the sum of `v x` over all `x` such that `f x = a`. -/ def map_domain (f : α₁ β†’ Ξ±β‚‚) (v : α₁ β†’β‚€ Ξ²) : Ξ±β‚‚ β†’β‚€ Ξ² := v.sum $ Ξ»a, single (f a) lemma map_domain_apply {f : α₁ β†’ Ξ±β‚‚} (hf : function.injective f) (x : α₁ β†’β‚€ Ξ²) (a : α₁) : map_domain f x (f a) = x a := begin rw [map_domain, sum_apply, sum, finset.sum_eq_single a, single_eq_same], { assume b _ hba, exact single_eq_of_ne (hf.ne hba) }, { simp only [(βˆ‰), (β‰ ), not_not, mem_support_iff], assume h, rw [h, single_zero], refl } end lemma map_domain_notin_range {f : α₁ β†’ Ξ±β‚‚} (x : α₁ β†’β‚€ Ξ²) (a : Ξ±β‚‚) (h : a βˆ‰ set.range f) : map_domain f x a = 0 := begin rw [map_domain, sum_apply, sum], exact finset.sum_eq_zero (assume a' h', single_eq_of_ne $ assume eq, h $ eq β–Έ set.mem_range_self _) end lemma map_domain_id : map_domain id v = v := sum_single _ lemma map_domain_comp {f : Ξ± β†’ α₁} {g : α₁ β†’ Ξ±β‚‚} : map_domain (g ∘ f) v = map_domain g (map_domain f v) := begin refine ((sum_sum_index _ _).trans _).symm, { intros, exact single_zero }, { intros, exact single_add }, refine sum_congr rfl (Ξ» _ _, sum_single_index _), { exact single_zero } end lemma map_domain_single {f : Ξ± β†’ α₁} {a : Ξ±} {b : Ξ²} : map_domain f (single a b) = single (f a) b := sum_single_index single_zero @[simp] lemma map_domain_zero {f : Ξ± β†’ Ξ±β‚‚} : map_domain f 0 = (0 : Ξ±β‚‚ β†’β‚€ Ξ²) := sum_zero_index lemma map_domain_congr {f g : Ξ± β†’ Ξ±β‚‚} (h : βˆ€x∈v.support, f x = g x) : v.map_domain f = v.map_domain g := finset.sum_congr rfl $ Ξ» _ H, by simp only [h _ H] lemma map_domain_add {f : Ξ± β†’ Ξ±β‚‚} : map_domain f (v₁ + vβ‚‚) = map_domain f v₁ + map_domain f vβ‚‚ := sum_add_index (Ξ» _, single_zero) (Ξ» _ _ _, single_add) lemma map_domain_finset_sum {f : Ξ± β†’ Ξ±β‚‚} {s : finset ΞΉ} {v : ΞΉ β†’ Ξ± β†’β‚€ Ξ²} : map_domain f (s.sum v) = s.sum (Ξ»i, map_domain f (v i)) := eq.symm $ sum_finset_sum_index (Ξ» _, single_zero) (Ξ» _ _ _, single_add) lemma map_domain_sum [has_zero β₁] {f : Ξ± β†’ Ξ±β‚‚} {s : Ξ± β†’β‚€ β₁} {v : Ξ± β†’ β₁ β†’ Ξ± β†’β‚€ Ξ²} : map_domain f (s.sum v) = s.sum (Ξ»a b, map_domain f (v a b)) := eq.symm $ sum_finset_sum_index (Ξ» _, single_zero) (Ξ» _ _ _, single_add) lemma map_domain_support {f : Ξ± β†’ Ξ±β‚‚} {s : Ξ± β†’β‚€ Ξ²} : (s.map_domain f).support βŠ† s.support.image f := finset.subset.trans support_sum $ finset.subset.trans (finset.bind_mono $ assume a ha, support_single_subset) $ by rw [finset.bind_singleton]; exact subset.refl _ @[to_additive] lemma prod_map_domain_index [comm_monoid Ξ³] {f : Ξ± β†’ Ξ±β‚‚} {s : Ξ± β†’β‚€ Ξ²} {h : Ξ±β‚‚ β†’ Ξ² β†’ Ξ³} (h_zero : βˆ€a, h a 0 = 1) (h_add : βˆ€a b₁ bβ‚‚, h a (b₁ + bβ‚‚) = h a b₁ * h a bβ‚‚) : (s.map_domain f).prod h = s.prod (Ξ»a b, h (f a) b) := (prod_sum_index h_zero h_add).trans $ prod_congr rfl $ Ξ» _ _, prod_single_index (h_zero _) lemma emb_domain_eq_map_domain (f : α₁ β†ͺ Ξ±β‚‚) (v : α₁ β†’β‚€ Ξ²) : emb_domain f v = map_domain f v := begin ext a, by_cases a ∈ set.range f, { rcases h with ⟨a, rfl⟩, rw [map_domain_apply f.inj, emb_domain_apply] }, { rw [map_domain_notin_range, emb_domain_notin_range]; assumption } end lemma injective_map_domain {f : α₁ β†’ Ξ±β‚‚} (hf : function.injective f) : function.injective (map_domain f : (α₁ β†’β‚€ Ξ²) β†’ (Ξ±β‚‚ β†’β‚€ Ξ²)) := begin assume v₁ vβ‚‚ eq, ext a, have : map_domain f v₁ (f a) = map_domain f vβ‚‚ (f a), { rw eq }, rwa [map_domain_apply hf, map_domain_apply hf] at this, end end map_domain /-! ### Declarations about `comap_domain` -/ section comap_domain /-- Given `f : α₁ β†’ Ξ±β‚‚`, `l : Ξ±β‚‚ β†’β‚€ Ξ³` and a proof `hf` that `f` is injective on the preimage of `l.support`, `comap_domain f l hf` is the finitely supported function from `α₁` to `Ξ³` given by composing `l` with `f`. -/ def comap_domain {α₁ Ξ±β‚‚ Ξ³ : Type*} [has_zero Ξ³] (f : α₁ β†’ Ξ±β‚‚) (l : Ξ±β‚‚ β†’β‚€ Ξ³) (hf : set.inj_on f (f ⁻¹' ↑l.support)) : α₁ β†’β‚€ Ξ³ := { support := l.support.preimage hf, to_fun := (Ξ» a, l (f a)), mem_support_to_fun := begin intros a, simp only [finset.mem_def.symm, finset.mem_preimage], exact l.mem_support_to_fun (f a), end } @[simp] lemma comap_domain_apply {α₁ Ξ±β‚‚ Ξ³ : Type*} [has_zero Ξ³] (f : α₁ β†’ Ξ±β‚‚) (l : Ξ±β‚‚ β†’β‚€ Ξ³) (hf : set.inj_on f (f ⁻¹' ↑l.support)) (a : α₁) : comap_domain f l hf a = l (f a) := rfl lemma sum_comap_domain {α₁ Ξ±β‚‚ Ξ² Ξ³ : Type*} [has_zero Ξ²] [add_comm_monoid Ξ³] (f : α₁ β†’ Ξ±β‚‚) (l : Ξ±β‚‚ β†’β‚€ Ξ²) (g : Ξ±β‚‚ β†’ Ξ² β†’ Ξ³) (hf : set.bij_on f (f ⁻¹' ↑l.support) ↑l.support) : (comap_domain f l hf.inj_on).sum (g ∘ f) = l.sum g := begin simp [sum], simp [comap_domain, finset.sum_preimage f _ _ (Ξ» (x : Ξ±β‚‚), g x (l x))] end lemma eq_zero_of_comap_domain_eq_zero {α₁ Ξ±β‚‚ Ξ³ : Type*} [add_comm_monoid Ξ³] (f : α₁ β†’ Ξ±β‚‚) (l : Ξ±β‚‚ β†’β‚€ Ξ³) (hf : set.bij_on f (f ⁻¹' ↑l.support) ↑l.support) : comap_domain f l hf.inj_on = 0 β†’ l = 0 := begin rw [← support_eq_empty, ← support_eq_empty, comap_domain], simp only [finset.ext, finset.not_mem_empty, iff_false, mem_preimage], assume h a ha, cases hf.2.2 ha with b hb, exact h b (hb.2.symm β–Έ ha) end lemma map_domain_comap_domain {α₁ Ξ±β‚‚ Ξ³ : Type*} [add_comm_monoid Ξ³] (f : α₁ β†’ Ξ±β‚‚) (l : Ξ±β‚‚ β†’β‚€ Ξ³) (hf : function.injective f) (hl : ↑l.support βŠ† set.range f): map_domain f (comap_domain f l (hf.inj_on _)) = l := begin ext a, by_cases h_cases: a ∈ set.range f, { rcases set.mem_range.1 h_cases with ⟨b, hb⟩, rw [hb.symm, map_domain_apply hf, comap_domain_apply] }, { rw map_domain_notin_range _ _ h_cases, by_contra h_contr, apply h_cases (hl $ finset.mem_coe.2 $ mem_support_iff.2 $ Ξ» h, h_contr h.symm) } end end comap_domain /-! ### Declarations about `filter` -/ section filter section has_zero variables [has_zero Ξ²] (p : Ξ± β†’ Prop) (f : Ξ± β†’β‚€ Ξ²) /-- `filter p f` is the function which is `f a` if `p a` is true and 0 otherwise. -/ def filter (p : Ξ± β†’ Prop) (f : Ξ± β†’β‚€ Ξ²) : Ξ± β†’β‚€ Ξ² := on_finset f.support (Ξ»a, if p a then f a else 0) $ Ξ» a H, mem_support_iff.2 $ Ξ» h, by rw [h, if_t_t] at H; exact H rfl @[simp] lemma filter_apply_pos {a : Ξ±} (h : p a) : f.filter p a = f a := if_pos h @[simp] lemma filter_apply_neg {a : Ξ±} (h : Β¬ p a) : f.filter p a = 0 := if_neg h @[simp] lemma support_filter : (f.filter p).support = f.support.filter p := finset.ext.mpr $ assume a, if H : p a then by simp only [mem_support_iff, filter_apply_pos _ _ H, mem_filter, H, and_true] else by simp only [mem_support_iff, filter_apply_neg _ _ H, mem_filter, H, and_false, ne.def, ne_self_iff_false] lemma filter_zero : (0 : Ξ± β†’β‚€ Ξ²).filter p = 0 := by rw [← support_eq_empty, support_filter, support_zero, finset.filter_empty] @[simp] lemma filter_single_of_pos {a : Ξ±} {b : Ξ²} (h : p a) : (single a b).filter p = single a b := ext $ Ξ» x, begin by_cases h' : p x, { simp only [h', filter_apply_pos] }, { simp only [h', filter_apply_neg, not_false_iff], rw single_eq_of_ne, rintro rfl, exact h' h } end @[simp] lemma filter_single_of_neg {a : Ξ±} {b : Ξ²} (h : Β¬ p a) : (single a b).filter p = 0 := ext $ Ξ» x, begin by_cases h' : p x, { simp only [h', filter_apply_pos, zero_apply], rw single_eq_of_ne, rintro rfl, exact h h' }, { simp only [h', finsupp.zero_apply, not_false_iff, filter_apply_neg] } end end has_zero lemma filter_pos_add_filter_neg [add_monoid Ξ²] (f : Ξ± β†’β‚€ Ξ²) (p : Ξ± β†’ Prop) : f.filter p + f.filter (Ξ»a, Β¬ p a) = f := ext $ assume a, if H : p a then by simp only [add_apply, filter_apply_pos, filter_apply_neg, H, not_not, add_zero] else by simp only [add_apply, filter_apply_pos, filter_apply_neg, H, not_false_iff, zero_add] end filter /-! ### Declarations about `frange` -/ section frange variables [has_zero Ξ²] /-- `frange f` is the image of `f` on the support of `f`. -/ def frange (f : Ξ± β†’β‚€ Ξ²) : finset Ξ² := finset.image f f.support theorem mem_frange {f : Ξ± β†’β‚€ Ξ²} {y : Ξ²} : y ∈ f.frange ↔ y β‰  0 ∧ βˆƒ x, f x = y := finset.mem_image.trans ⟨λ ⟨x, hx1, hx2⟩, ⟨hx2 β–Έ mem_support_iff.1 hx1, x, hx2⟩, Ξ» ⟨hy, x, hx⟩, ⟨x, mem_support_iff.2 (hx.symm β–Έ hy), hx⟩⟩ theorem zero_not_mem_frange {f : Ξ± β†’β‚€ Ξ²} : (0:Ξ²) βˆ‰ f.frange := Ξ» H, (mem_frange.1 H).1 rfl theorem frange_single {x : Ξ±} {y : Ξ²} : frange (single x y) βŠ† {y} := Ξ» r hr, let ⟨t, ht1, ht2⟩ := mem_frange.1 hr in ht2 β–Έ (by rw single_apply at ht2 ⊒; split_ifs at ht2 ⊒; [exact finset.mem_singleton_self _, cc]) end frange /-! ### Declarations about `subtype_domain` -/ section subtype_domain variables {Ξ±' : Type*} [has_zero Ξ΄] {p : Ξ± β†’ Prop} section zero variables [has_zero Ξ²] {v v' : Ξ±' β†’β‚€ Ξ²} /-- `subtype_domain p f` is the restriction of the finitely supported function `f` to the subtype `p`. -/ def subtype_domain (p : Ξ± β†’ Prop) (f : Ξ± β†’β‚€ Ξ²) : (subtype p β†’β‚€ Ξ²) := ⟨f.support.subtype p, f ∘ subtype.val, Ξ» a, by simp only [mem_subtype, mem_support_iff]⟩ @[simp] lemma support_subtype_domain {f : Ξ± β†’β‚€ Ξ²} : (subtype_domain p f).support = f.support.subtype p := rfl @[simp] lemma subtype_domain_apply {a : subtype p} {v : Ξ± β†’β‚€ Ξ²} : (subtype_domain p v) a = v (a.val) := rfl @[simp] lemma subtype_domain_zero : subtype_domain p (0 : Ξ± β†’β‚€ Ξ²) = 0 := rfl @[to_additive] lemma prod_subtype_domain_index [comm_monoid Ξ³] {v : Ξ± β†’β‚€ Ξ²} {h : Ξ± β†’ Ξ² β†’ Ξ³} (hp : βˆ€x∈v.support, p x) : (v.subtype_domain p).prod (Ξ»a b, h a.1 b) = v.prod h := prod_bij (Ξ»p _, p.val) (Ξ» _, mem_subtype.1) (Ξ» _ _, rfl) (Ξ» _ _ _ _, subtype.eq) (Ξ» b hb, ⟨⟨b, hp b hb⟩, mem_subtype.2 hb, rfl⟩) end zero section monoid variables [add_monoid Ξ²] {v v' : Ξ±' β†’β‚€ Ξ²} @[simp] lemma subtype_domain_add {v v' : Ξ± β†’β‚€ Ξ²} : (v + v').subtype_domain p = v.subtype_domain p + v'.subtype_domain p := ext $ Ξ» _, rfl instance subtype_domain.is_add_monoid_hom : is_add_monoid_hom (subtype_domain p : (Ξ± β†’β‚€ Ξ²) β†’ subtype p β†’β‚€ Ξ²) := { map_add := Ξ» _ _, subtype_domain_add, map_zero := subtype_domain_zero } @[simp] lemma filter_add {v v' : Ξ± β†’β‚€ Ξ²} : (v + v').filter p = v.filter p + v'.filter p := ext $ Ξ» a, begin by_cases p a, { simp only [h, filter_apply_pos, add_apply] }, { simp only [h, add_zero, add_apply, not_false_iff, filter_apply_neg] } end instance filter.is_add_monoid_hom (p : Ξ± β†’ Prop) : is_add_monoid_hom (filter p : (Ξ± β†’β‚€ Ξ²) β†’ (Ξ± β†’β‚€ Ξ²)) := { map_zero := filter_zero p, map_add := Ξ» x y, filter_add } end monoid section comm_monoid variables [add_comm_monoid Ξ²] lemma subtype_domain_sum {s : finset Ξ³} {h : Ξ³ β†’ Ξ± β†’β‚€ Ξ²} : (s.sum h).subtype_domain p = s.sum (Ξ»c, (h c).subtype_domain p) := eq.symm (s.sum_hom _) lemma subtype_domain_finsupp_sum {s : Ξ³ β†’β‚€ Ξ΄} {h : Ξ³ β†’ Ξ΄ β†’ Ξ± β†’β‚€ Ξ²} : (s.sum h).subtype_domain p = s.sum (Ξ»c d, (h c d).subtype_domain p) := subtype_domain_sum lemma filter_sum (s : finset Ξ³) (f : Ξ³ β†’ Ξ± β†’β‚€ Ξ²) : (s.sum f).filter p = s.sum (Ξ»a, filter p (f a)) := (s.sum_hom (filter p)).symm end comm_monoid section group variables [add_group Ξ²] {v v' : Ξ±' β†’β‚€ Ξ²} @[simp] lemma subtype_domain_neg {v : Ξ± β†’β‚€ Ξ²} : (- v).subtype_domain p = - v.subtype_domain p := ext $ Ξ» _, rfl @[simp] lemma subtype_domain_sub {v v' : Ξ± β†’β‚€ Ξ²} : (v - v').subtype_domain p = v.subtype_domain p - v'.subtype_domain p := ext $ Ξ» _, rfl end group end subtype_domain /-! ### Declarations relating `finsupp` to `multiset` -/ section multiset /-- Given `f : Ξ± β†’β‚€ β„•`, `f.to_multiset` is the multiset with multiplicities given by the values of `f` on the elements of `Ξ±`. -/ def to_multiset (f : Ξ± β†’β‚€ β„•) : multiset Ξ± := f.sum (Ξ»a n, add_monoid.smul n {a}) lemma to_multiset_zero : (0 : Ξ± β†’β‚€ β„•).to_multiset = 0 := rfl lemma to_multiset_add (m n : Ξ± β†’β‚€ β„•) : (m + n).to_multiset = m.to_multiset + n.to_multiset := sum_add_index (assume a, add_monoid.zero_smul _) (assume a b₁ bβ‚‚, add_monoid.add_smul _ _ _) lemma to_multiset_single (a : Ξ±) (n : β„•) : to_multiset (single a n) = add_monoid.smul n {a} := by rw [to_multiset, sum_single_index]; apply add_monoid.zero_smul instance is_add_monoid_hom.to_multiset : is_add_monoid_hom (to_multiset : _ β†’ multiset Ξ±) := { map_zero := to_multiset_zero, map_add := to_multiset_add } lemma card_to_multiset (f : Ξ± β†’β‚€ β„•) : f.to_multiset.card = f.sum (Ξ»a, id) := begin refine f.induction _ _, { rw [to_multiset_zero, multiset.card_zero, sum_zero_index] }, { assume a n f _ _ ih, rw [to_multiset_add, multiset.card_add, ih, sum_add_index, to_multiset_single, sum_single_index, multiset.card_smul, multiset.singleton_eq_singleton, multiset.card_singleton, mul_one]; intros; refl } end lemma to_multiset_map (f : Ξ± β†’β‚€ β„•) (g : Ξ± β†’ Ξ²) : f.to_multiset.map g = (f.map_domain g).to_multiset := begin refine f.induction _ _, { rw [to_multiset_zero, multiset.map_zero, map_domain_zero, to_multiset_zero] }, { assume a n f _ _ ih, rw [to_multiset_add, multiset.map_add, ih, map_domain_add, map_domain_single, to_multiset_single, to_multiset_add, to_multiset_single, is_add_monoid_hom.map_smul (multiset.map g)], refl } end lemma prod_to_multiset [comm_monoid Ξ±] (f : Ξ± β†’β‚€ β„•) : f.to_multiset.prod = f.prod (Ξ»a n, a ^ n) := begin refine f.induction _ _, { rw [to_multiset_zero, multiset.prod_zero, finsupp.prod_zero_index] }, { assume a n f _ _ ih, rw [to_multiset_add, multiset.prod_add, ih, to_multiset_single, finsupp.prod_add_index, finsupp.prod_single_index, multiset.prod_smul, multiset.singleton_eq_singleton, multiset.prod_singleton], { exact pow_zero a }, { exact pow_zero }, { exact pow_add } } end lemma to_finset_to_multiset (f : Ξ± β†’β‚€ β„•) : f.to_multiset.to_finset = f.support := begin refine f.induction _ _, { rw [to_multiset_zero, multiset.to_finset_zero, support_zero] }, { assume a n f ha hn ih, rw [to_multiset_add, multiset.to_finset_add, ih, to_multiset_single, support_add_eq, support_single_ne_zero hn, multiset.to_finset_smul _ _ hn, multiset.singleton_eq_singleton, multiset.to_finset_cons, multiset.to_finset_zero], refl, refine disjoint.mono_left support_single_subset _, rwa [finset.singleton_eq_singleton, finset.singleton_disjoint] } end @[simp] lemma count_to_multiset (f : Ξ± β†’β‚€ β„•) (a : Ξ±) : f.to_multiset.count a = f a := calc f.to_multiset.count a = f.sum (Ξ»x n, (add_monoid.smul n {x} : multiset Ξ±).count a) : (f.support.sum_hom $ multiset.count a).symm ... = f.sum (Ξ»x n, n * ({x} : multiset Ξ±).count a) : by simp only [multiset.count_smul] ... = f.sum (Ξ»x n, n * (x :: 0 : multiset Ξ±).count a) : rfl ... = f a * (a :: 0 : multiset Ξ±).count a : sum_eq_single _ (Ξ» a' _ H, by simp only [multiset.count_cons_of_ne (ne.symm H), multiset.count_zero, mul_zero]) (Ξ» H, by simp only [not_mem_support_iff.1 H, zero_mul]) ... = f a : by simp only [multiset.count_singleton, mul_one] /-- Given `m : multiset Ξ±`, `of_multiset m` is the finitely supported function from `Ξ±` to `β„•` given by the multiplicities of the elements of `Ξ±` in `m`. -/ def of_multiset (m : multiset Ξ±) : Ξ± β†’β‚€ β„• := on_finset m.to_finset (Ξ»a, m.count a) $ Ξ» a H, multiset.mem_to_finset.2 $ by_contradiction (mt multiset.count_eq_zero.2 H) @[simp] lemma of_multiset_apply (m : multiset Ξ±) (a : Ξ±) : of_multiset m a = m.count a := rfl /-- `equiv_multiset` defines an `equiv` between finitely supported functions from `Ξ±` to `β„•` and multisets on `Ξ±`. -/ def equiv_multiset : (Ξ± β†’β‚€ β„•) ≃ (multiset Ξ±) := ⟨ to_multiset, of_multiset, assume f, finsupp.ext $ Ξ» a, by rw [of_multiset_apply, count_to_multiset], assume m, multiset.ext.2 $ Ξ» a, by rw [count_to_multiset, of_multiset_apply] ⟩ lemma mem_support_multiset_sum [add_comm_monoid Ξ²] {s : multiset (Ξ± β†’β‚€ Ξ²)} (a : Ξ±) : a ∈ s.sum.support β†’ βˆƒf∈s, a ∈ (f : Ξ± β†’β‚€ Ξ²).support := multiset.induction_on s false.elim begin assume f s ih ha, by_cases a ∈ f.support, { exact ⟨f, multiset.mem_cons_self _ _, h⟩ }, { simp only [multiset.sum_cons, mem_support_iff, add_apply, not_mem_support_iff.1 h, zero_add] at ha, rcases ih (mem_support_iff.2 ha) with ⟨f', hβ‚€, hβ‚βŸ©, exact ⟨f', multiset.mem_cons_of_mem hβ‚€, hβ‚βŸ© } end lemma mem_support_finset_sum [add_comm_monoid Ξ²] {s : finset Ξ³} {h : Ξ³ β†’ Ξ± β†’β‚€ Ξ²} (a : Ξ±) (ha : a ∈ (s.sum h).support) : βˆƒc∈s, a ∈ (h c).support := let ⟨f, hf, hfa⟩ := mem_support_multiset_sum a ha in let ⟨c, hc, eq⟩ := multiset.mem_map.1 hf in ⟨c, hc, eq.symm β–Έ hfa⟩ lemma mem_support_single [has_zero Ξ²] (a a' : Ξ±) (b : Ξ²) : a ∈ (single a' b).support ↔ a = a' ∧ b β‰  0 := ⟨λ H : (a ∈ ite _ _ _), if h : b = 0 then by rw if_pos h at H; exact H.elim else ⟨by rw if_neg h at H; exact mem_singleton.1 H, h⟩, Ξ» ⟨h1, h2⟩, show a ∈ ite _ _ _, by rw [if_neg h2]; exact mem_singleton.2 h1⟩ end multiset /-! ### Declarations about `curry` and `uncurry` -/ section curry_uncurry /-- Given a finitely supported function `f` from a product type `Ξ± Γ— Ξ²` to `Ξ³`, `curry f` is the "curried" finitely supported function from `Ξ±` to the type of finitely supported functions from `Ξ²` to `Ξ³`. -/ protected def curry [add_comm_monoid Ξ³] (f : (Ξ± Γ— Ξ²) β†’β‚€ Ξ³) : Ξ± β†’β‚€ (Ξ² β†’β‚€ Ξ³) := f.sum $ Ξ»p c, single p.1 (single p.2 c) lemma sum_curry_index [add_comm_monoid Ξ³] [add_comm_monoid Ξ΄] (f : (Ξ± Γ— Ξ²) β†’β‚€ Ξ³) (g : Ξ± β†’ Ξ² β†’ Ξ³ β†’ Ξ΄) (hgβ‚€ : βˆ€ a b, g a b 0 = 0) (hg₁ : βˆ€a b cβ‚€ c₁, g a b (cβ‚€ + c₁) = g a b cβ‚€ + g a b c₁) : f.curry.sum (Ξ»a f, f.sum (g a)) = f.sum (Ξ»p c, g p.1 p.2 c) := begin rw [finsupp.curry], transitivity, { exact sum_sum_index (assume a, sum_zero_index) (assume a bβ‚€ b₁, sum_add_index (assume a, hgβ‚€ _ _) (assume c dβ‚€ d₁, hg₁ _ _ _ _)) }, congr, funext p c, transitivity, { exact sum_single_index sum_zero_index }, exact sum_single_index (hgβ‚€ _ _) end /-- Given a finitely supported function `f` from `Ξ±` to the type of finitely supported functions from `Ξ²` to `Ξ³`, `uncurry f` is the "uncurried" finitely supported function from `Ξ± Γ— Ξ²` to `Ξ³`. -/ protected def uncurry [add_comm_monoid Ξ³] (f : Ξ± β†’β‚€ (Ξ² β†’β‚€ Ξ³)) : (Ξ± Γ— Ξ²) β†’β‚€ Ξ³ := f.sum $ Ξ»a g, g.sum $ Ξ»b c, single (a, b) c /-- `finsupp_prod_equiv` defines the `equiv` between `((Ξ± Γ— Ξ²) β†’β‚€ Ξ³)` and `(Ξ± β†’β‚€ (Ξ² β†’β‚€ Ξ³))` given by currying and uncurrying. -/ def finsupp_prod_equiv [add_comm_monoid Ξ³] : ((Ξ± Γ— Ξ²) β†’β‚€ Ξ³) ≃ (Ξ± β†’β‚€ (Ξ² β†’β‚€ Ξ³)) := by refine ⟨finsupp.curry, finsupp.uncurry, Ξ» f, _, Ξ» f, _⟩; simp only [ finsupp.curry, finsupp.uncurry, sum_sum_index, sum_zero_index, sum_add_index, sum_single_index, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, prod.mk.eta, (single_sum _ _ _).symm, sum_single] lemma filter_curry [add_comm_monoid Ξ²] (f : α₁ Γ— Ξ±β‚‚ β†’β‚€ Ξ²) (p : α₁ β†’ Prop) : (f.filter (Ξ»a:α₁×α₂, p a.1)).curry = f.curry.filter p := begin rw [finsupp.curry, finsupp.curry, finsupp.sum, finsupp.sum, @filter_sum _ (Ξ±β‚‚ β†’β‚€ Ξ²) _ p _ f.support _], rw [support_filter, sum_filter], refine finset.sum_congr rfl _, rintros ⟨a₁, aβ‚‚βŸ© ha, dsimp only, split_ifs, { rw [filter_apply_pos, filter_single_of_pos]; exact h }, { rwa [filter_single_of_neg] } end lemma support_curry [add_comm_monoid Ξ²] (f : α₁ Γ— Ξ±β‚‚ β†’β‚€ Ξ²) : f.curry.support βŠ† f.support.image prod.fst := begin rw ← finset.bind_singleton, refine finset.subset.trans support_sum _, refine finset.bind_mono (assume a _, support_single_subset) end end curry_uncurry section variables [group Ξ³] [mul_action Ξ³ Ξ±] [add_comm_monoid Ξ²] /-- Scalar multiplication by a group element g, given by precomposition with the action of g⁻¹ on the domain. -/ def comap_has_scalar : has_scalar Ξ³ (Ξ± β†’β‚€ Ξ²) := { smul := Ξ» g f, f.comap_domain (Ξ» a, g⁻¹ β€’ a) (Ξ» a a' m m' h, by simpa [←mul_smul] using (congr_arg (Ξ» a, g β€’ a) h)) } local attribute [instance] comap_has_scalar /-- Scalar multiplication by a group element, given by precomposition with the action of g⁻¹ on the domain, is multiplicative in g. -/ def comap_mul_action : mul_action Ξ³ (Ξ± β†’β‚€ Ξ²) := { one_smul := Ξ» f, by { ext, dsimp [(β€’)], simp, }, mul_smul := Ξ» g g' f, by { ext, dsimp [(β€’)], simp [mul_smul], }, } local attribute [instance] comap_mul_action /-- Scalar multiplication by a group element, given by precomposition with the action of g⁻¹ on the domain, is additive in the second argument. -/ def comap_distrib_mul_action : distrib_mul_action Ξ³ (Ξ± β†’β‚€ Ξ²) := { smul_zero := Ξ» g, by { ext, dsimp [(β€’)], simp, }, smul_add := Ξ» g f f', by { ext, dsimp [(β€’)], simp, }, } /-- Scalar multiplication by a group element on finitely supported functions on a group, given by precomposition with the action of g⁻¹. -/ def comap_distrib_mul_action_self : distrib_mul_action Ξ³ (Ξ³ β†’β‚€ Ξ²) := @finsupp.comap_distrib_mul_action Ξ³ Ξ² Ξ³ _ (mul_action.regular Ξ³) _ @[simp] lemma comap_smul_single (g : Ξ³) (a : Ξ±) (b : Ξ²) : g β€’ single a b = single (g β€’ a) b := begin ext a', dsimp [(β€’)], by_cases h : g β€’ a = a', { subst h, simp [←mul_smul], }, { simp [single_eq_of_ne h], rw [single_eq_of_ne], rintro rfl, simpa [←mul_smul] using h, } end @[simp] lemma comap_smul_apply (g : Ξ³) (f : Ξ± β†’β‚€ Ξ²) (a : Ξ±) : (g β€’ f) a = f (g⁻¹ β€’ a) := rfl end section instance [semiring Ξ³] [add_comm_monoid Ξ²] [semimodule Ξ³ Ξ²] : has_scalar Ξ³ (Ξ± β†’β‚€ Ξ²) := ⟨λa v, v.map_range ((β€’) a) (smul_zero _)⟩ variables (Ξ± Ξ²) @[simp] lemma smul_apply' {R:semiring Ξ³} [add_comm_monoid Ξ²] [semimodule Ξ³ Ξ²] {a : Ξ±} {b : Ξ³} {v : Ξ± β†’β‚€ Ξ²} : (b β€’ v) a = b β€’ (v a) := rfl instance [semiring Ξ³] [add_comm_monoid Ξ²] [semimodule Ξ³ Ξ²] : semimodule Ξ³ (Ξ± β†’β‚€ Ξ²) := { smul := (β€’), smul_add := Ξ» a x y, ext $ Ξ» _, smul_add _ _ _, add_smul := Ξ» a x y, ext $ Ξ» _, add_smul _ _ _, one_smul := Ξ» x, ext $ Ξ» _, one_smul _ _, mul_smul := Ξ» r s x, ext $ Ξ» _, mul_smul _ _ _, zero_smul := Ξ» x, ext $ Ξ» _, zero_smul _ _, smul_zero := Ξ» x, ext $ Ξ» _, smul_zero _ } instance [ring Ξ³] [add_comm_group Ξ²] [module Ξ³ Ξ²] : module Ξ³ (Ξ± β†’β‚€ Ξ²) := { ..finsupp.semimodule Ξ± Ξ² } instance [field Ξ³] [add_comm_group Ξ²] [vector_space Ξ³ Ξ²] : vector_space Ξ³ (Ξ± β†’β‚€ Ξ²) := { ..finsupp.module Ξ± Ξ² } variables {Ξ± Ξ²} lemma support_smul {R:semiring Ξ³} [add_comm_monoid Ξ²] [semimodule Ξ³ Ξ²] {b : Ξ³} {g : Ξ± β†’β‚€ Ξ²} : (b β€’ g).support βŠ† g.support := Ξ» a, by simp only [smul_apply', mem_support_iff, ne.def]; exact mt (Ξ» h, h.symm β–Έ smul_zero _) section variables {Ξ±' : Type*} [has_zero Ξ΄] {p : Ξ± β†’ Prop} @[simp] lemma filter_smul {R : semiring Ξ³} [add_comm_monoid Ξ²] [semimodule Ξ³ Ξ²] {b : Ξ³} {v : Ξ± β†’β‚€ Ξ²} : (b β€’ v).filter p = b β€’ v.filter p := ext $ Ξ» a, begin by_cases p a, { simp only [h, smul_apply', filter_apply_pos] }, { simp only [h, smul_apply', not_false_iff, filter_apply_neg, smul_zero] } end end lemma map_domain_smul {Ξ±'} {R : semiring Ξ³} [add_comm_monoid Ξ²] [semimodule Ξ³ Ξ²] {f : Ξ± β†’ Ξ±'} (b : Ξ³) (v : Ξ± β†’β‚€ Ξ²) : map_domain f (b β€’ v) = b β€’ map_domain f v := begin change map_domain f (map_range _ _ _) = map_range _ _ _, apply finsupp.induction v, { simp only [map_domain_zero, map_range_zero] }, intros a b v' hv₁ hvβ‚‚ IH, rw [map_range_add, map_domain_add, IH, map_domain_add, map_range_add, map_range_single, map_domain_single, map_domain_single, map_range_single]; apply smul_add end @[simp] lemma smul_single {R : semiring Ξ³} [add_comm_monoid Ξ²] [semimodule Ξ³ Ξ²] (c : Ξ³) (a : Ξ±) (b : Ξ²) : c β€’ finsupp.single a b = finsupp.single a (c β€’ b) := ext $ Ξ» a', by by_cases a = a'; [{ subst h, simp only [smul_apply', single_eq_same] }, simp only [h, smul_apply', ne.def, not_false_iff, single_eq_of_ne, smul_zero]] end @[simp] lemma smul_apply [semiring Ξ²] {a : Ξ±} {b : Ξ²} {v : Ξ± β†’β‚€ Ξ²} : (b β€’ v) a = b β€’ (v a) := rfl lemma sum_smul_index [ring Ξ²] [add_comm_monoid Ξ³] {g : Ξ± β†’β‚€ Ξ²} {b : Ξ²} {h : Ξ± β†’ Ξ² β†’ Ξ³} (h0 : βˆ€i, h i 0 = 0) : (b β€’ g).sum h = g.sum (Ξ»i a, h i (b * a)) := finsupp.sum_map_range_index h0 section variables [semiring Ξ²] [semiring Ξ³] lemma sum_mul (b : Ξ³) (s : Ξ± β†’β‚€ Ξ²) {f : Ξ± β†’ Ξ² β†’ Ξ³} : (s.sum f) * b = s.sum (Ξ» a c, (f a (s a)) * b) := by simp only [finsupp.sum, finset.sum_mul] lemma mul_sum (b : Ξ³) (s : Ξ± β†’β‚€ Ξ²) {f : Ξ± β†’ Ξ² β†’ Ξ³} : b * (s.sum f) = s.sum (Ξ» a c, b * (f a (s a))) := by simp only [finsupp.sum, finset.mul_sum] protected lemma eq_zero_of_zero_eq_one (zero_eq_one : (0 : Ξ²) = 1) (l : Ξ± β†’β‚€ Ξ²) : l = 0 := by ext i; simp only [eq_zero_of_zero_eq_one Ξ² zero_eq_one (l i), finsupp.zero_apply] end /-- Given an `add_comm_monoid Ξ²` and `s : set Ξ±`, `restrict_support_equiv` is the `equiv` between the subtype of finitely supported functions with support contained in `s` and the type of finitely supported functions from `s`. -/ def restrict_support_equiv [add_comm_monoid Ξ²] (s : set Ξ±) : {f : Ξ± β†’β‚€ Ξ² // ↑f.support βŠ† s } ≃ (s β†’β‚€ Ξ²):= begin refine ⟨λf, subtype_domain (Ξ»x, x ∈ s) f.1, Ξ» f, ⟨f.map_domain subtype.val, _⟩, _, _⟩, { refine set.subset.trans (finset.coe_subset.2 map_domain_support) _, rw [finset.coe_image, set.image_subset_iff], exact assume x hx, x.2 }, { rintros ⟨f, hf⟩, apply subtype.eq, ext a, dsimp only, refine classical.by_cases (assume h : a ∈ set.range (subtype.val : s β†’ Ξ±), _) (assume h, _), { rcases h with ⟨x, rfl⟩, rw [map_domain_apply subtype.val_injective, subtype_domain_apply] }, { convert map_domain_notin_range _ _ h, rw [← not_mem_support_iff], refine mt _ h, exact assume ha, ⟨⟨a, hf ha⟩, rfl⟩ } }, { assume f, ext ⟨a, ha⟩, dsimp only, rw [subtype_domain_apply, map_domain_apply subtype.val_injective] } end /-- Given `add_comm_monoid Ξ²` and `e : α₁ ≃ Ξ±β‚‚`, `dom_congr e` is the corresponding `equiv` between `α₁ β†’β‚€ Ξ²` and `Ξ±β‚‚ β†’β‚€ Ξ²`. -/ protected def dom_congr [add_comm_monoid Ξ²] (e : α₁ ≃ Ξ±β‚‚) : (α₁ β†’β‚€ Ξ²) ≃ (Ξ±β‚‚ β†’β‚€ Ξ²) := ⟨map_domain e, map_domain e.symm, begin assume v, simp only [map_domain_comp.symm, (∘), equiv.symm_apply_apply], exact map_domain_id end, begin assume v, simp only [map_domain_comp.symm, (∘), equiv.apply_symm_apply], exact map_domain_id end⟩ /-! ### Declarations about sigma types -/ section sigma variables {Ξ±s : ΞΉ β†’ Type*} [has_zero Ξ²] (l : (Ξ£ i, Ξ±s i) β†’β‚€ Ξ²) /-- Given `l`, a finitely supported function from the sigma type `Ξ£ (i : ΞΉ), Ξ±s i` to `Ξ²` and an index element `i : ΞΉ`, `split l i` is the `i`th component of `l`, a finitely supported function from `as i` to `Ξ²`. -/ def split (i : ΞΉ) : Ξ±s i β†’β‚€ Ξ² := l.comap_domain (sigma.mk i) (Ξ» x1 x2 _ _ hx, heq_iff_eq.1 (sigma.mk.inj hx).2) lemma split_apply (i : ΞΉ) (x : Ξ±s i) : split l i x = l ⟨i, x⟩ := begin dunfold split, rw comap_domain_apply end /-- Given `l`, a finitely supported function from the sigma type `Ξ£ (i : ΞΉ), Ξ±s i` to `Ξ²`, `split_support l` is the finset of indices in `ΞΉ` that appear in the support of `l`. -/ def split_support : finset ΞΉ := l.support.image sigma.fst lemma mem_split_support_iff_nonzero (i : ΞΉ) : i ∈ split_support l ↔ split l i β‰  0 := begin rw [split_support, mem_image, ne.def, ← support_eq_empty, ← ne.def, ← finset.nonempty_iff_ne_empty, split, comap_domain, finset.nonempty], simp only [exists_prop, finset.mem_preimage, exists_and_distrib_right, exists_eq_right, mem_support_iff, sigma.exists, ne.def] end /-- Given `l`, a finitely supported function from the sigma type `Ξ£ i, Ξ±s i` to `Ξ²` and an `ΞΉ`-indexed family `g` of functions from `(Ξ±s i β†’β‚€ Ξ²)` to `Ξ³`, `split_comp` defines a finitely supported function from the index type `ΞΉ` to `Ξ³` given by composing `g i` with `split l i`. -/ def split_comp [has_zero Ξ³] (g : Ξ  i, (Ξ±s i β†’β‚€ Ξ²) β†’ Ξ³) (hg : βˆ€ i x, x = 0 ↔ g i x = 0) : ΞΉ β†’β‚€ Ξ³ := { support := split_support l, to_fun := Ξ» i, g i (split l i), mem_support_to_fun := begin intros i, rw [mem_split_support_iff_nonzero, not_iff_not, hg], end } lemma sigma_support : l.support = l.split_support.sigma (Ξ» i, (l.split i).support) := by simp only [finset.ext, split_support, split, comap_domain, mem_image, mem_preimage, sigma.forall, mem_sigma]; tauto lemma sigma_sum [add_comm_monoid Ξ³] (f : (Ξ£ (i : ΞΉ), Ξ±s i) β†’ Ξ² β†’ Ξ³) : l.sum f = (split_support l).sum (Ξ» (i : ΞΉ), (split l i).sum (Ξ» (a : Ξ±s i) b, f ⟨i, a⟩ b)) := by simp only [sum, sigma_support, sum_sigma, split_apply] end sigma end finsupp /-! ### Declarations relating `multiset` to `finsupp` -/ namespace multiset /-- Given a multiset `s`, `s.to_finsupp` returns the finitely supported function on `β„•` given by the multiplicities of the elements of `s`. -/ def to_finsupp (s : multiset Ξ±) : Ξ± β†’β‚€ β„• := { support := s.to_finset, to_fun := Ξ» a, s.count a, mem_support_to_fun := Ξ» a, begin rw mem_to_finset, convert not_iff_not_of_iff (count_eq_zero.symm), rw not_not end } @[simp] lemma to_finsupp_support (s : multiset Ξ±) : s.to_finsupp.support = s.to_finset := rfl @[simp] lemma to_finsupp_apply (s : multiset Ξ±) (a : Ξ±) : s.to_finsupp a = s.count a := rfl @[simp] lemma to_finsupp_zero : to_finsupp (0 : multiset Ξ±) = 0 := finsupp.ext $ Ξ» a, count_zero a @[simp] lemma to_finsupp_add (s t : multiset Ξ±) : to_finsupp (s + t) = to_finsupp s + to_finsupp t := finsupp.ext $ Ξ» a, count_add a s t lemma to_finsupp_singleton (a : Ξ±) : to_finsupp {a} = finsupp.single a 1 := finsupp.ext $ Ξ» b, if h : a = b then by rw [to_finsupp_apply, finsupp.single_apply, h, if_pos rfl, singleton_eq_singleton, count_singleton] else begin rw [to_finsupp_apply, finsupp.single_apply, if_neg h, count_eq_zero, singleton_eq_singleton, mem_singleton], rintro rfl, exact h rfl end namespace to_finsupp instance : is_add_monoid_hom (to_finsupp : multiset Ξ± β†’ Ξ± β†’β‚€ β„•) := { map_zero := to_finsupp_zero, map_add := to_finsupp_add } end to_finsupp @[simp] lemma to_finsupp_to_multiset (s : multiset Ξ±) : s.to_finsupp.to_multiset = s := ext.2 $ Ξ» a, by rw [finsupp.count_to_multiset, to_finsupp_apply] end multiset /-! ### Declarations about order(ed) instances on `finsupp` -/ namespace finsupp variables {Οƒ : Type*} instance [preorder Ξ±] [has_zero Ξ±] : preorder (Οƒ β†’β‚€ Ξ±) := { le := Ξ» f g, βˆ€ s, f s ≀ g s, le_refl := Ξ» f s, le_refl _, le_trans := Ξ» f g h Hfg Hgh s, le_trans (Hfg s) (Hgh s) } instance [partial_order Ξ±] [has_zero Ξ±] : partial_order (Οƒ β†’β‚€ Ξ±) := { le_antisymm := Ξ» f g hfg hgf, ext $ Ξ» s, le_antisymm (hfg s) (hgf s), .. finsupp.preorder } instance [ordered_cancel_add_comm_monoid Ξ±] : add_left_cancel_semigroup (Οƒ β†’β‚€ Ξ±) := { add_left_cancel := Ξ» a b c h, ext $ Ξ» s, by { rw ext_iff at h, exact add_left_cancel (h s) }, .. finsupp.add_monoid } instance [ordered_cancel_add_comm_monoid Ξ±] : add_right_cancel_semigroup (Οƒ β†’β‚€ Ξ±) := { add_right_cancel := Ξ» a b c h, ext $ Ξ» s, by { rw ext_iff at h, exact add_right_cancel (h s) }, .. finsupp.add_monoid } instance [ordered_cancel_add_comm_monoid Ξ±] : ordered_cancel_add_comm_monoid (Οƒ β†’β‚€ Ξ±) := { add_le_add_left := Ξ» a b h c s, add_le_add_left (h s) (c s), le_of_add_le_add_left := Ξ» a b c h s, le_of_add_le_add_left (h s), .. finsupp.add_comm_monoid, .. finsupp.partial_order, .. finsupp.add_left_cancel_semigroup, .. finsupp.add_right_cancel_semigroup } lemma le_iff [canonically_ordered_add_monoid Ξ±] (f g : Οƒ β†’β‚€ Ξ±) : f ≀ g ↔ βˆ€ s ∈ f.support, f s ≀ g s := ⟨λ h s hs, h s, Ξ» h s, if H : s ∈ f.support then h s H else (not_mem_support_iff.1 H).symm β–Έ zero_le (g s)⟩ @[simp] lemma add_eq_zero_iff [canonically_ordered_add_monoid Ξ±] (f g : Οƒ β†’β‚€ Ξ±) : f + g = 0 ↔ f = 0 ∧ g = 0 := begin split, { assume h, split, all_goals { ext s, suffices H : f s + g s = 0, { rw add_eq_zero_iff at H, cases H, assumption }, show (f + g) s = 0, rw h, refl } }, { rintro ⟨rfl, rfl⟩, rw add_zero } end attribute [simp] to_multiset_zero to_multiset_add @[simp] lemma to_multiset_to_finsupp (f : Οƒ β†’β‚€ β„•) : f.to_multiset.to_finsupp = f := ext $ Ξ» s, by rw [multiset.to_finsupp_apply, count_to_multiset] lemma to_multiset_strict_mono : strict_mono (@to_multiset Οƒ) := Ξ» m n h, begin rw lt_iff_le_and_ne at h ⊒, cases h with h₁ hβ‚‚, split, { rw multiset.le_iff_count, intro s, erw [count_to_multiset m s, count_to_multiset], exact h₁ s }, { intro H, apply hβ‚‚, replace H := congr_arg multiset.to_finsupp H, simpa only [to_multiset_to_finsupp] using H } end lemma sum_id_lt_of_lt (m n : Οƒ β†’β‚€ β„•) (h : m < n) : m.sum (Ξ» _, id) < n.sum (Ξ» _, id) := begin rw [← card_to_multiset, ← card_to_multiset], apply multiset.card_lt_of_lt, exact to_multiset_strict_mono h end variable (Οƒ) /-- The order on `Οƒ β†’β‚€ β„•` is well-founded.-/ lemma lt_wf : well_founded (@has_lt.lt (Οƒ β†’β‚€ β„•) _) := subrelation.wf (sum_id_lt_of_lt) $ inv_image.wf _ nat.lt_wf instance decidable_le : decidable_rel (@has_le.le (Οƒ β†’β‚€ β„•) _) := Ξ» m n, by rw le_iff; apply_instance variable {Οƒ} /-- The `finsupp` counterpart of `multiset.antidiagonal`: the antidiagonal of `s : Οƒ β†’β‚€ β„•` consists of all pairs `(t₁, tβ‚‚) : (Οƒ β†’β‚€ β„•) Γ— (Οƒ β†’β‚€ β„•)` such that `t₁ + tβ‚‚ = s`. The finitely supported function `antidiagonal s` is equal to the multiplicities of these pairs. -/ def antidiagonal (f : Οƒ β†’β‚€ β„•) : ((Οƒ β†’β‚€ β„•) Γ— (Οƒ β†’β‚€ β„•)) β†’β‚€ β„• := (f.to_multiset.antidiagonal.map (prod.map multiset.to_finsupp multiset.to_finsupp)).to_finsupp lemma mem_antidiagonal_support {f : Οƒ β†’β‚€ β„•} {p : (Οƒ β†’β‚€ β„•) Γ— (Οƒ β†’β‚€ β„•)} : p ∈ (antidiagonal f).support ↔ p.1 + p.2 = f := begin erw [multiset.mem_to_finset, multiset.mem_map], split, { rintros ⟨⟨a, b⟩, h, rfl⟩, rw multiset.mem_antidiagonal at h, simpa only [to_multiset_to_finsupp, multiset.to_finsupp_add] using congr_arg multiset.to_finsupp h}, { intro h, refine ⟨⟨p.1.to_multiset, p.2.to_multiset⟩, _, _⟩, { simpa only [multiset.mem_antidiagonal, to_multiset_add] using congr_arg to_multiset h}, { rw [prod.map, to_multiset_to_finsupp, to_multiset_to_finsupp, prod.mk.eta] } } end @[simp] lemma antidiagonal_zero : antidiagonal (0 : Οƒ β†’β‚€ β„•) = single (0,0) 1 := by rw [← multiset.to_finsupp_singleton]; refl lemma swap_mem_antidiagonal_support {n : Οƒ β†’β‚€ β„•} {f} (hf : f ∈ (antidiagonal n).support) : f.swap ∈ (antidiagonal n).support := by simpa only [mem_antidiagonal_support, add_comm, prod.swap] using hf end finsupp
87c5c9f73fddf84ceb7309c7df3eeaf0165bfb98
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/Lean/Compiler/IR/PushProj.lean
63f73393f0cf1a6d16e71bd37fce05ac862f1218
[ "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,025
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Compiler.IR.Basic import Lean.Compiler.IR.FreeVars import Lean.Compiler.IR.NormIds namespace Lean.IR partial def pushProjs (bs : Array FnBody) (alts : Array Alt) (altsF : Array IndexSet) (ctx : Array FnBody) (ctxF : IndexSet) : Array FnBody Γ— Array Alt := if bs.isEmpty then (ctx.reverse, alts) else let b := bs.back let bs := bs.pop let done (_ : Unit) := (bs.push b ++ ctx.reverse, alts) let skip (_ : Unit) := pushProjs bs alts altsF (ctx.push b) (b.collectFreeIndices ctxF) let push (x : VarId) := if !ctxF.contains x.idx then let alts := alts.mapIdx fun i alt => alt.modifyBody fun b' => if (altsF.get! i).contains x.idx then b.setBody b' else b' let altsF := altsF.map fun s => if s.contains x.idx then b.collectFreeIndices s else s pushProjs bs alts altsF ctx ctxF else skip () match b with | FnBody.vdecl x _ v _ => match v with | Expr.proj _ _ => push x | Expr.uproj _ _ => push x | Expr.sproj _ _ _ => push x | Expr.isShared _ => skip () | _ => done () | _ => done () partial def FnBody.pushProj (b : FnBody) : FnBody := let (bs, term) := b.flatten let bs := modifyJPs bs pushProj match term with | .case tid x xType alts => let altsF := alts.map fun alt => alt.body.freeIndices let (bs, alts) := pushProjs bs alts altsF #[] (mkIndexSet x.idx) let alts := alts.map fun alt => alt.modifyBody pushProj let term := FnBody.case tid x xType alts reshape bs term | _ => reshape bs term /-- Push projections inside `case` branches. -/ def Decl.pushProj (d : Decl) : Decl := match d with | .fdecl (body := b) .. => d.updateBody! b.pushProj |>.normalizeIds | other => other end Lean.IR
2c4676e63d64e1ac7162a09a8a28edd2041b9310
22e97a5d648fc451e25a06c668dc03ac7ed7bc25
/src/group_theory/group_action.lean
0994ae417bbb43acc68d6cdd41df50c09b4ca6a6
[ "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
9,776
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.set.finite import group_theory.coset universes u v w variables {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} /-- Typeclass for types with a scalar multiplication operation, denoted `β€’` (`\bu`) -/ class has_scalar (Ξ± : Type u) (Ξ³ : Type v) := (smul : Ξ± β†’ Ξ³ β†’ Ξ³) infixr ` β€’ `:73 := has_scalar.smul section prio set_option default_priority 100 -- see Note [default priority] /-- Typeclass for multiplicative actions by monoids. This generalizes group actions. -/ class mul_action (Ξ± : Type u) (Ξ² : Type v) [monoid Ξ±] extends has_scalar Ξ± Ξ² := (one_smul : βˆ€ b : Ξ², (1 : Ξ±) β€’ b = b) (mul_smul : βˆ€ (x y : Ξ±) (b : Ξ²), (x * y) β€’ b = x β€’ y β€’ b) end prio section variables [monoid Ξ±] [mul_action Ξ± Ξ²] theorem mul_smul (a₁ aβ‚‚ : Ξ±) (b : Ξ²) : (a₁ * aβ‚‚) β€’ b = a₁ β€’ aβ‚‚ β€’ b := mul_action.mul_smul _ _ _ lemma smul_smul (a₁ aβ‚‚ : Ξ±) (b : Ξ²) : a₁ β€’ aβ‚‚ β€’ b = (a₁ * aβ‚‚) β€’ b := (mul_smul _ _ _).symm lemma smul_comm {Ξ± : Type u} {Ξ² : Type v} [comm_monoid Ξ±] [mul_action Ξ± Ξ²] (a₁ aβ‚‚ : Ξ±) (b : Ξ²) : a₁ β€’ aβ‚‚ β€’ b = aβ‚‚ β€’ a₁ β€’ b := by rw [←mul_smul, ←mul_smul, mul_comm] variable (Ξ±) @[simp] theorem one_smul (b : Ξ²) : (1 : Ξ±) β€’ b = b := mul_action.one_smul _ variables {Ξ±} @[simp] lemma units.inv_smul_smul (u : units Ξ±) (x : Ξ²) : (↑u⁻¹:Ξ±) β€’ (u:Ξ±) β€’ x = x := by rw [smul_smul, u.inv_mul, one_smul] @[simp] lemma units.smul_inv_smul (u : units Ξ±) (x : Ξ²) : (u:Ξ±) β€’ (↑u⁻¹:Ξ±) β€’ x = x := by rw [smul_smul, u.mul_inv, one_smul] section gwz variables {G : Type*} [group_with_zero G] [mul_action G Ξ²] lemma inv_smul_smul' {c : G} (hc : c β‰  0) (x : Ξ²) : c⁻¹ β€’ c β€’ x = x := (units.mk0 c hc).inv_smul_smul x lemma smul_inv_smul' {c : G} (hc : c β‰  0) (x : Ξ²) : c β€’ c⁻¹ β€’ x = x := (units.mk0 c hc).smul_inv_smul x end gwz variables (p : Prop) [decidable p] lemma ite_smul (a₁ aβ‚‚ : Ξ±) (b : Ξ²) : (ite p a₁ aβ‚‚) β€’ b = ite p (a₁ β€’ b) (aβ‚‚ β€’ b) := by split_ifs; refl lemma smul_ite (a : Ξ±) (b₁ bβ‚‚ : Ξ²) : a β€’ (ite p b₁ bβ‚‚) = ite p (a β€’ b₁) (a β€’ bβ‚‚) := by split_ifs; refl end namespace mul_action variables (Ξ±) [monoid Ξ±] /-- The regular action of a monoid on itself by left multiplication. -/ def regular : mul_action Ξ± Ξ± := { smul := Ξ» a₁ aβ‚‚, a₁ * aβ‚‚, one_smul := Ξ» a, one_mul a, mul_smul := Ξ» a₁ aβ‚‚ a₃, mul_assoc _ _ _, } variables [mul_action Ξ± Ξ²] def orbit (b : Ξ²) := set.range (Ξ» x : Ξ±, x β€’ b) variable {Ξ±} @[simp] lemma mem_orbit_iff {b₁ bβ‚‚ : Ξ²} : bβ‚‚ ∈ orbit Ξ± b₁ ↔ βˆƒ x : Ξ±, x β€’ b₁ = bβ‚‚ := iff.rfl @[simp] lemma mem_orbit (b : Ξ²) (x : Ξ±) : x β€’ b ∈ orbit Ξ± b := ⟨x, rfl⟩ @[simp] lemma mem_orbit_self (b : Ξ²) : b ∈ orbit Ξ± b := ⟨1, by simp [mul_action.one_smul]⟩ instance orbit_fintype (b : Ξ²) [fintype Ξ±] [decidable_eq Ξ²] : fintype (orbit Ξ± b) := set.fintype_range _ variable (Ξ±) def stabilizer (b : Ξ²) : set Ξ± := {x : Ξ± | x β€’ b = b} variable {Ξ±} @[simp] lemma mem_stabilizer_iff {b : Ξ²} {x : Ξ±} : x ∈ stabilizer Ξ± b ↔ x β€’ b = b := iff.rfl variables (Ξ±) (Ξ²) def fixed_points : set Ξ² := {b : Ξ² | βˆ€ x, x ∈ stabilizer Ξ± b} variables {Ξ±} (Ξ²) @[simp] lemma mem_fixed_points {b : Ξ²} : b ∈ fixed_points Ξ± Ξ² ↔ βˆ€ x : Ξ±, x β€’ b = b := iff.rfl lemma mem_fixed_points' {b : Ξ²} : b ∈ fixed_points Ξ± Ξ² ↔ (βˆ€ b', b' ∈ orbit Ξ± b β†’ b' = b) := ⟨λ h b h₁, let ⟨x, hx⟩ := mem_orbit_iff.1 h₁ in hx β–Έ h x, Ξ» h b, mem_stabilizer_iff.2 (h _ (mem_orbit _ _))⟩ def comp_hom [monoid Ξ³] (g : Ξ³ β†’ Ξ±) [is_monoid_hom g] : mul_action Ξ³ Ξ² := { smul := Ξ» x b, (g x) β€’ b, one_smul := by simp [is_monoid_hom.map_one g, mul_action.one_smul], mul_smul := by simp [is_monoid_hom.map_mul g, mul_action.mul_smul] } instance (b : Ξ²) : is_submonoid (stabilizer Ξ± b) := { one_mem := one_smul b, mul_mem := Ξ» a a' (ha : a β€’ b = b) (hb : a' β€’ b = b), by rw [mem_stabilizer_iff, ←smul_smul, hb, ha] } end mul_action namespace mul_action variables [group Ξ±] [mul_action Ξ± Ξ²] section open mul_action quotient_group lemma inv_smul_smul (c : Ξ±) (x : Ξ²) : c⁻¹ β€’ c β€’ x = x := (to_units Ξ± c).inv_smul_smul x lemma smul_inv_smul (c : Ξ±) (x : Ξ²) : c β€’ c⁻¹ β€’ x = x := (to_units Ξ± c).smul_inv_smul x variables (Ξ±) (Ξ²) /-- Given an action of a group `Ξ±` on a set `Ξ²`, each `g : Ξ±` defines a permutation of `Ξ²`. -/ def to_perm (g : Ξ±) : equiv.perm Ξ² := { to_fun := (β€’) g, inv_fun := (β€’) g⁻¹, left_inv := inv_smul_smul g, right_inv := smul_inv_smul g } variables {Ξ±} {Ξ²} instance : is_group_hom (to_perm Ξ± Ξ²) := { map_mul := Ξ» x y, equiv.ext (Ξ» a, mul_action.mul_smul x y a) } lemma bijective (g : Ξ±) : function.bijective (Ξ» b : Ξ², g β€’ b) := (to_perm Ξ± Ξ² g).bijective lemma orbit_eq_iff {a b : Ξ²} : orbit Ξ± a = orbit Ξ± b ↔ a ∈ orbit Ξ± b:= ⟨λ h, h β–Έ mem_orbit_self _, Ξ» ⟨x, (hx : x β€’ b = a)⟩, set.ext (Ξ» c, ⟨λ ⟨y, (hy : y β€’ a = c)⟩, ⟨y * x, show (y * x) β€’ b = c, by rwa [mul_action.mul_smul, hx]⟩, Ξ» ⟨y, (hy : y β€’ b = c)⟩, ⟨y * x⁻¹, show (y * x⁻¹) β€’ a = c, by conv {to_rhs, rw [← hy, ← mul_one y, ← inv_mul_self x, ← mul_assoc, mul_action.mul_smul, hx]}⟩⟩)⟩ instance (b : Ξ²) : is_subgroup (stabilizer Ξ± b) := { one_mem := mul_action.one_smul _, mul_mem := Ξ» x y (hx : x β€’ b = b) (hy : y β€’ b = b), show (x * y) β€’ b = b, by rw mul_action.mul_smul; simp *, inv_mem := Ξ» x (hx : x β€’ b = b), show x⁻¹ β€’ b = b, by rw [← hx, ← mul_action.mul_smul, inv_mul_self, mul_action.one_smul, hx] } variables (Ξ±) (Ξ²) def orbit_rel : setoid Ξ² := { r := Ξ» a b, a ∈ orbit Ξ± b, iseqv := ⟨mem_orbit_self, Ξ» a b, by simp [orbit_eq_iff.symm, eq_comm], Ξ» a b, by simp [orbit_eq_iff.symm, eq_comm] {contextual := tt}⟩ } variables {Ξ²} open quotient_group noncomputable def orbit_equiv_quotient_stabilizer (b : Ξ²) : orbit Ξ± b ≃ quotient (stabilizer Ξ± b) := equiv.symm (@equiv.of_bijective _ _ (Ξ» x : quotient (stabilizer Ξ± b), quotient.lift_on' x (Ξ» x, (⟨x β€’ b, mem_orbit _ _⟩ : orbit Ξ± b)) (Ξ» g h (H : _ = _), subtype.eq $ (mul_action.bijective (g⁻¹)).1 $ show g⁻¹ β€’ (g β€’ b) = g⁻¹ β€’ (h β€’ b), by rw [← mul_action.mul_smul, ← mul_action.mul_smul, H, inv_mul_self, mul_action.one_smul])) ⟨λ g h, quotient.induction_onβ‚‚' g h (Ξ» g h H, quotient.sound' $ have H : g β€’ b = h β€’ b := subtype.mk.inj H, show (g⁻¹ * h) β€’ b = b, by rw [mul_action.mul_smul, ← H, ← mul_action.mul_smul, inv_mul_self, mul_action.one_smul]), Ξ» ⟨b, ⟨g, hgb⟩⟩, ⟨g, subtype.eq hgb⟩⟩) end open quotient_group mul_action is_subgroup def mul_left_cosets (H : set Ξ±) [is_subgroup H] (x : Ξ±) (y : quotient H) : quotient H := quotient.lift_on' y (Ξ» y, quotient_group.mk ((x : Ξ±) * y)) (Ξ» a b (hab : _ ∈ H), quotient_group.eq.2 (by rwa [mul_inv_rev, ← mul_assoc, mul_assoc (a⁻¹), inv_mul_self, mul_one])) instance (H : set Ξ±) [is_subgroup H] : mul_action Ξ± (quotient H) := { smul := mul_left_cosets H, one_smul := Ξ» a, quotient.induction_on' a (Ξ» a, quotient_group.eq.2 (by simp [is_submonoid.one_mem])), mul_smul := Ξ» x y a, quotient.induction_on' a (Ξ» a, quotient_group.eq.2 (by simp [mul_inv_rev, is_submonoid.one_mem, mul_assoc])) } instance mul_left_cosets_comp_subtype_val (H I : set Ξ±) [is_subgroup H] [is_subgroup I] : mul_action I (quotient H) := mul_action.comp_hom (quotient H) (subtype.val : I β†’ Ξ±) end mul_action section prio set_option default_priority 100 -- see Note [default priority] /-- Typeclass for multiplicative actions on additive structures. This generalizes group modules. -/ class distrib_mul_action (Ξ± : Type u) (Ξ² : Type v) [monoid Ξ±] [add_monoid Ξ²] extends mul_action Ξ± Ξ² := (smul_add : βˆ€(r : Ξ±) (x y : Ξ²), r β€’ (x + y) = r β€’ x + r β€’ y) (smul_zero : βˆ€(r : Ξ±), r β€’ (0 : Ξ²) = 0) end prio section variables [monoid Ξ±] [add_monoid Ξ²] [distrib_mul_action Ξ± Ξ²] theorem smul_add (a : Ξ±) (b₁ bβ‚‚ : Ξ²) : a β€’ (b₁ + bβ‚‚) = a β€’ b₁ + a β€’ bβ‚‚ := distrib_mul_action.smul_add _ _ _ @[simp] theorem smul_zero (a : Ξ±) : a β€’ (0 : Ξ²) = 0 := distrib_mul_action.smul_zero _ theorem units.smul_eq_zero (u : units Ξ±) {x : Ξ²} : (u : Ξ±) β€’ x = 0 ↔ x = 0 := ⟨λ h, by rw [← u.inv_smul_smul x, h, smul_zero], Ξ» h, h.symm β–Έ smul_zero _⟩ theorem units.smul_ne_zero (u : units Ξ±) {x : Ξ²} : (u : Ξ±) β€’ x β‰  0 ↔ x β‰  0 := not_congr u.smul_eq_zero @[simp] theorem is_unit.smul_eq_zero {u : Ξ±} (hu : is_unit u) {x : Ξ²} : u β€’ x = 0 ↔ x = 0 := exists.elim hu $ Ξ» u hu, hu.symm β–Έ u.smul_eq_zero variable (Ξ²) /-- Scalar multiplication by `r` as an `add_monoid_hom`. -/ def const_smul_hom (r : Ξ±) : Ξ² β†’+ Ξ² := { to_fun := (β€’) r, map_zero' := smul_zero r, map_add' := smul_add r } variable {Ξ²} @[simp] lemma const_smul_hom_apply (r : Ξ±) (x : Ξ²) : const_smul_hom Ξ² r x = r β€’ x := rfl lemma list.smul_sum {r : Ξ±} {l : list Ξ²} : r β€’ l.sum = (l.map ((β€’) r)).sum := (const_smul_hom Ξ² r).map_list_sum l end section variables [monoid Ξ±] [add_comm_monoid Ξ²] [distrib_mul_action Ξ± Ξ²] lemma multiset.smul_sum {r : Ξ±} {s : multiset Ξ²} : r β€’ s.sum = (s.map ((β€’) r)).sum := (const_smul_hom Ξ² r).map_multiset_sum s lemma finset.smul_sum {r : Ξ±} {f : Ξ³ β†’ Ξ²} {s : finset Ξ³} : r β€’ s.sum f = s.sum (Ξ» x, r β€’ f x) := (const_smul_hom Ξ² r).map_sum f s end
b827ee4be3ac1060cca976f02a3237ccf7d8c056
4727251e0cd73359b15b664c3170e5d754078599
/src/analysis/calculus/fderiv_analytic.lean
b18d50ad74cf499d91462a0f1681a726727bc0a3
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
7,181
lean
/- Copyright (c) 2021 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import analysis.calculus.deriv import analysis.analytic.basic import analysis.calculus.cont_diff /-! # Frechet derivatives of analytic functions. A function expressible as a power series at a point has a Frechet derivative there. Also the special case in terms of `deriv` when the domain is 1-dimensional. -/ open filter asymptotics open_locale ennreal variables {π•œ : Type*} [nondiscrete_normed_field π•œ] variables {E : Type*} [normed_group E] [normed_space π•œ E] variables {F : Type*} [normed_group F] [normed_space π•œ F] section fderiv variables {p : formal_multilinear_series π•œ E F} {r : ℝβ‰₯0∞} variables {f : E β†’ F} {x : E} {s : set E} lemma has_fpower_series_at.has_strict_fderiv_at (h : has_fpower_series_at f p x) : has_strict_fderiv_at f (continuous_multilinear_curry_fin1 π•œ E F (p 1)) x := begin refine h.is_O_image_sub_norm_mul_norm_sub.trans_is_o (is_o.of_norm_right _), refine is_o_iff_exists_eq_mul.2 ⟨λ y, βˆ₯y - (x, x)βˆ₯, _, eventually_eq.rfl⟩, refine (continuous_id.sub continuous_const).norm.tendsto' _ _ _, rw [_root_.id, sub_self, norm_zero] end lemma has_fpower_series_at.has_fderiv_at (h : has_fpower_series_at f p x) : has_fderiv_at f (continuous_multilinear_curry_fin1 π•œ E F (p 1)) x := h.has_strict_fderiv_at.has_fderiv_at lemma has_fpower_series_at.differentiable_at (h : has_fpower_series_at f p x) : differentiable_at π•œ f x := h.has_fderiv_at.differentiable_at lemma analytic_at.differentiable_at : analytic_at π•œ f x β†’ differentiable_at π•œ f x | ⟨p, hp⟩ := hp.differentiable_at lemma analytic_at.differentiable_within_at (h : analytic_at π•œ f x) : differentiable_within_at π•œ f s x := h.differentiable_at.differentiable_within_at lemma has_fpower_series_at.fderiv_eq (h : has_fpower_series_at f p x) : fderiv π•œ f x = continuous_multilinear_curry_fin1 π•œ E F (p 1) := h.has_fderiv_at.fderiv lemma has_fpower_series_on_ball.differentiable_on [complete_space F] (h : has_fpower_series_on_ball f p x r) : differentiable_on π•œ f (emetric.ball x r) := Ξ» y hy, (h.analytic_at_of_mem hy).differentiable_within_at lemma analytic_on.differentiable_on (h : analytic_on π•œ f s) : differentiable_on π•œ f s := Ξ» y hy, (h y hy).differentiable_within_at lemma has_fpower_series_on_ball.has_fderiv_at [complete_space F] (h : has_fpower_series_on_ball f p x r) {y : E} (hy : (βˆ₯yβˆ₯β‚Š : ℝβ‰₯0∞) < r) : has_fderiv_at f (continuous_multilinear_curry_fin1 π•œ E F (p.change_origin y 1)) (x + y) := (h.change_origin hy).has_fpower_series_at.has_fderiv_at lemma has_fpower_series_on_ball.fderiv_eq [complete_space F] (h : has_fpower_series_on_ball f p x r) {y : E} (hy : (βˆ₯yβˆ₯β‚Š : ℝβ‰₯0∞) < r) : fderiv π•œ f (x + y) = continuous_multilinear_curry_fin1 π•œ E F (p.change_origin y 1) := (h.has_fderiv_at hy).fderiv /-- If a function has a power series on a ball, then so does its derivative. -/ lemma has_fpower_series_on_ball.fderiv [complete_space F] (h : has_fpower_series_on_ball f p x r) : has_fpower_series_on_ball (fderiv π•œ f) ((continuous_multilinear_curry_fin1 π•œ E F : (E [Γ—1]β†’L[π•œ] F) β†’L[π•œ] (E β†’L[π•œ] F)) .comp_formal_multilinear_series (p.change_origin_series 1)) x r := begin suffices A : has_fpower_series_on_ball (Ξ» z, continuous_multilinear_curry_fin1 π•œ E F (p.change_origin (z - x) 1)) ((continuous_multilinear_curry_fin1 π•œ E F : (E [Γ—1]β†’L[π•œ] F) β†’L[π•œ] (E β†’L[π•œ] F)) .comp_formal_multilinear_series (p.change_origin_series 1)) x r, { apply A.congr, assume z hz, dsimp, rw [← h.fderiv_eq, add_sub_cancel'_right], simpa only [edist_eq_coe_nnnorm_sub, emetric.mem_ball] using hz}, suffices B : has_fpower_series_on_ball (Ξ» z, p.change_origin (z - x) 1) (p.change_origin_series 1) x r, from (continuous_multilinear_curry_fin1 π•œ E F).to_continuous_linear_equiv .to_continuous_linear_map.comp_has_fpower_series_on_ball B, simpa using ((p.has_fpower_series_on_ball_change_origin 1 (h.r_pos.trans_le h.r_le)).mono h.r_pos h.r_le).comp_sub x, end /-- If a function is analytic on a set `s`, so is its FrΓ©chet derivative. -/ lemma analytic_on.fderiv [complete_space F] (h : analytic_on π•œ f s) : analytic_on π•œ (fderiv π•œ f) s := begin assume y hy, rcases h y hy with ⟨p, r, hp⟩, exact hp.fderiv.analytic_at, end /-- If a function is analytic on a set `s`, so are its successive FrΓ©chet derivative. -/ lemma analytic_on.iterated_fderiv [complete_space F] (h : analytic_on π•œ f s) (n : β„•) : analytic_on π•œ (iterated_fderiv π•œ n f) s := begin induction n with n IH, { rw iterated_fderiv_zero_eq_comp, exact ((continuous_multilinear_curry_fin0 π•œ E F).symm : F β†’L[π•œ] (E [Γ—0]β†’L[π•œ] F)) .comp_analytic_on h }, { rw iterated_fderiv_succ_eq_comp_left, apply (continuous_multilinear_curry_left_equiv π•œ (Ξ» (i : fin (n + 1)), E) F) .to_continuous_linear_equiv.to_continuous_linear_map.comp_analytic_on, exact IH.fderiv } end /-- An analytic function is infinitely differentiable. -/ lemma analytic_on.cont_diff_on [complete_space F] (h : analytic_on π•œ f s) {n : with_top β„•} : cont_diff_on π•œ n f s := begin let t := {x | analytic_at π•œ f x}, suffices : cont_diff_on π•œ n f t, from this.mono h, have H : analytic_on π•œ f t := Ξ» x hx, hx, have t_open : is_open t := is_open_analytic_at π•œ f, apply cont_diff_on_of_continuous_on_differentiable_on, { assume m hm, apply (H.iterated_fderiv m).continuous_on.congr, assume x hx, exact iterated_fderiv_within_of_is_open _ t_open hx }, { assume m hm, apply (H.iterated_fderiv m).differentiable_on.congr, assume x hx, exact iterated_fderiv_within_of_is_open _ t_open hx } end end fderiv section deriv variables {p : formal_multilinear_series π•œ π•œ F} {r : ℝβ‰₯0∞} variables {f : π•œ β†’ F} {x : π•œ} {s : set π•œ} protected lemma has_fpower_series_at.has_strict_deriv_at (h : has_fpower_series_at f p x) : has_strict_deriv_at f (p 1 (Ξ» _, 1)) x := h.has_strict_fderiv_at.has_strict_deriv_at protected lemma has_fpower_series_at.has_deriv_at (h : has_fpower_series_at f p x) : has_deriv_at f (p 1 (Ξ» _, 1)) x := h.has_strict_deriv_at.has_deriv_at protected lemma has_fpower_series_at.deriv (h : has_fpower_series_at f p x) : deriv f x = p 1 (Ξ» _, 1) := h.has_deriv_at.deriv /-- If a function is analytic on a set `s`, so is its derivative. -/ lemma analytic_on.deriv [complete_space F] (h : analytic_on π•œ f s) : analytic_on π•œ (deriv f) s := (continuous_linear_map.apply π•œ F (1 : π•œ)).comp_analytic_on h.fderiv /-- If a function is analytic on a set `s`, so are its successive derivatives. -/ lemma analytic_on.iterated_deriv [complete_space F] (h : analytic_on π•œ f s) (n : β„•) : analytic_on π•œ (deriv^[n] f) s := begin induction n with n IH, { exact h }, { simpa only [function.iterate_succ', function.comp_app] using IH.deriv } end end deriv
6dd8136478c0935b04f5c99a74f111398efb13dc
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/ring_theory/adjoin/power_basis.lean
070b9fc1506c105481d10e09de7c3cc76e4ae407
[ "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
8,070
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 ring_theory.adjoin.basic import ring_theory.power_basis import linear_algebra.matrix.basis /-! # Power basis for `algebra.adjoin R {x}` > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines the canonical power basis on `algebra.adjoin R {x}`, where `x` is an integral element over `R`. -/ variables {K S : Type*} [field K] [comm_ring S] [algebra K S] namespace algebra open polynomial open power_basis open_locale big_operators /-- The elements `1, x, ..., x ^ (d - 1)` for a basis for the `K`-module `K[x]`, where `d` is the degree of the minimal polynomial of `x`. -/ noncomputable def adjoin.power_basis_aux {x : S} (hx : is_integral K x) : basis (fin (minpoly K x).nat_degree) K (adjoin K ({x} : set S)) := begin have hST : function.injective (algebra_map (adjoin K ({x} : set S)) S) := subtype.coe_injective, have hx' : is_integral K (show adjoin K ({x} : set S), from ⟨x, subset_adjoin (set.mem_singleton x)⟩), { apply (is_integral_algebra_map_iff hST).mp, convert hx, apply_instance }, have minpoly_eq := minpoly.eq_of_algebra_map_eq hST hx' rfl, apply @basis.mk (fin (minpoly K x).nat_degree) _ (adjoin K {x}) (Ξ» i, ⟨x, subset_adjoin (set.mem_singleton x)⟩ ^ (i : β„•)), { have := linear_independent_pow _, rwa minpoly_eq at this }, { rintros ⟨y, hy⟩ _, have := hx'.mem_span_pow, rw minpoly_eq at this, apply this, { rw [adjoin_singleton_eq_range_aeval] at hy, obtain ⟨f, rfl⟩ := (aeval x).mem_range.mp hy, use f, ext, exact aeval_algebra_map_apply S (⟨x, _⟩ : adjoin K {x}) _, } } end /-- The power basis `1, x, ..., x ^ (d - 1)` for `K[x]`, where `d` is the degree of the minimal polynomial of `x`. See `algebra.adjoin.power_basis'` for a version over a more general base ring. -/ @[simps gen dim] noncomputable def adjoin.power_basis {x : S} (hx : is_integral K x) : power_basis K (adjoin K ({x} : set S)) := { gen := ⟨x, subset_adjoin (set.mem_singleton x)⟩, dim := (minpoly K x).nat_degree, basis := adjoin.power_basis_aux hx, basis_eq_pow := basis.mk_apply _ _ } end algebra open algebra /-- The power basis given by `x` if `B.gen ∈ adjoin K {x}`. See `power_basis.of_gen_mem_adjoin'` for a version over a more general base ring. -/ @[simps] noncomputable def power_basis.of_gen_mem_adjoin {x : S} (B : power_basis K S) (hint : is_integral K x) (hx : B.gen ∈ adjoin K ({x} : set S)) : power_basis K S := (algebra.adjoin.power_basis hint).map $ (subalgebra.equiv_of_eq _ _ $ power_basis.adjoin_eq_top_of_gen_mem_adjoin hx).trans subalgebra.top_equiv section is_integral namespace power_basis open polynomial open_locale polynomial variables {R : Type*} [comm_ring R] [algebra R S] [algebra R K] [is_scalar_tower R K S] variables {A : Type*} [comm_ring A] [algebra R A] [algebra S A] variables [is_scalar_tower R S A] {B : power_basis S A} (hB : is_integral R B.gen) include hB /-- If `B : power_basis S A` is such that `is_integral R B.gen`, then `is_integral R (B.basis.repr (B.gen ^ n) i)` for all `i` if `minpoly S B.gen = (minpoly R B.gen).map (algebra_map R S)`. This is the case if `R` is a GCD domain and `S` is its fraction ring. -/ lemma repr_gen_pow_is_integral [is_domain S] (hmin : minpoly S B.gen = (minpoly R B.gen).map (algebra_map R S)) (n : β„•) : βˆ€ i, is_integral R (B.basis.repr (B.gen ^ n) i) := begin intro i, let Q := (X ^ n) %β‚˜ (minpoly R B.gen), have : B.gen ^ n = aeval B.gen Q, { rw [← @aeval_X_pow R _ _ _ _ B.gen, ← mod_by_monic_add_div (X ^ n) (minpoly.monic hB)], simp }, by_cases hQ : Q = 0, { simp [this, hQ, is_integral_zero] }, have hlt : Q.nat_degree < B.dim, { rw [← B.nat_degree_minpoly, hmin, (minpoly.monic hB).nat_degree_map, nat_degree_lt_nat_degree_iff hQ], letI : nontrivial R := nontrivial.of_polynomial_ne hQ, exact degree_mod_by_monic_lt _ (minpoly.monic hB), apply_instance }, rw [this, aeval_eq_sum_range' hlt], simp only [linear_equiv.map_sum, linear_equiv.map_smulβ‚›β‚—, ring_hom.id_apply, finset.sum_apply'], refine is_integral.sum _ (Ξ» j hj, _), replace hj := finset.mem_range.1 hj, rw [← fin.coe_mk hj, ← B.basis_eq_pow, algebra.smul_def, is_scalar_tower.algebra_map_apply R S A, ← algebra.smul_def, linear_equiv.map_smul], simp only [algebra_map_smul, finsupp.coe_smul, pi.smul_apply, B.basis.repr_self_apply], by_cases hij : (⟨j, hj⟩ : fin _) = i, { simp only [hij, eq_self_iff_true, if_true], rw [algebra.smul_def, mul_one], exact is_integral_algebra_map }, { simp [hij, is_integral_zero] } end variable {B} /-- Let `B : power_basis S A` be such that `is_integral R B.gen`, and let `x y : A` be elements with integral coordinates in the base `B.basis`. Then `is_integral R ((B.basis.repr (x * y) i)` for all `i` if `minpoly S B.gen = (minpoly R B.gen).map (algebra_map R S)`. This is the case if `R` is a GCD domain and `S` is its fraction ring. -/ lemma repr_mul_is_integral [is_domain S] {x y : A} (hx : βˆ€ i, is_integral R (B.basis.repr x i)) (hy : βˆ€ i, is_integral R (B.basis.repr y i)) (hmin : minpoly S B.gen = (minpoly R B.gen).map (algebra_map R S)) : βˆ€ i, is_integral R ((B.basis.repr (x * y) i)) := begin intro i, rw [← B.basis.sum_repr x, ← B.basis.sum_repr y, finset.sum_mul_sum, linear_equiv.map_sum, finset.sum_apply'], refine is_integral.sum _ (Ξ» I hI, _), simp only [algebra.smul_mul_assoc, algebra.mul_smul_comm, linear_equiv.map_smulβ‚›β‚—, ring_hom.id_apply, finsupp.coe_smul, pi.smul_apply, id.smul_eq_mul], refine is_integral_mul (hy _) (is_integral_mul (hx _) _), simp only [coe_basis, ← pow_add], refine repr_gen_pow_is_integral hB hmin _ _, end /-- Let `B : power_basis S A` be such that `is_integral R B.gen`, and let `x : A` be and element with integral coordinates in the base `B.basis`. Then `is_integral R ((B.basis.repr (x ^ n) i)` for all `i` and all `n` if `minpoly S B.gen = (minpoly R B.gen).map (algebra_map R S)`. This is the case if `R` is a GCD domain and `S` is its fraction ring. -/ lemma repr_pow_is_integral [is_domain S] {x : A} (hx : βˆ€ i, is_integral R (B.basis.repr x i)) (hmin : minpoly S B.gen = (minpoly R B.gen).map (algebra_map R S)) (n : β„•) : βˆ€ i, is_integral R ((B.basis.repr (x ^ n) i)) := begin nontriviality A using [subsingleton.elim (x ^ n) 0, is_integral_zero], revert hx, refine nat.case_strong_induction_on n _ (Ξ» n hn, _), { intros hx i, rw [pow_zero, ← pow_zero B.gen, ← fin.coe_mk B.dim_pos, ← B.basis_eq_pow, B.basis.repr_self_apply], split_ifs, { exact is_integral_one }, { exact is_integral_zero } }, { intros hx, rw [pow_succ], exact repr_mul_is_integral hB hx (Ξ» _, hn _ le_rfl (Ξ» _, hx _) _) hmin } end /-- Let `B B' : power_basis K S` be such that `is_integral R B.gen`, and let `P : R[X]` be such that `aeval B.gen P = B'.gen`. Then `is_integral R (B.basis.to_matrix B'.basis i j)` for all `i` and `j` if `minpoly K B.gen = (minpoly R B.gen).map (algebra_map R L)`. This is the case if `R` is a GCD domain and `K` is its fraction ring. -/ lemma to_matrix_is_integral {B B' : power_basis K S} {P : R[X]} (h : aeval B.gen P = B'.gen) (hB : is_integral R B.gen) (hmin : minpoly K B.gen = (minpoly R B.gen).map (algebra_map R K)) : βˆ€ i j, is_integral R (B.basis.to_matrix B'.basis i j) := begin intros i j, rw [B.basis.to_matrix_apply, B'.coe_basis], refine repr_pow_is_integral hB (Ξ» i, _) hmin _ _, rw [← h, aeval_eq_sum_range, linear_equiv.map_sum, finset.sum_apply'], refine is_integral.sum _ (Ξ» n hn, _), rw [algebra.smul_def, is_scalar_tower.algebra_map_apply R K S, ← algebra.smul_def, linear_equiv.map_smul, algebra_map_smul], exact is_integral_smul _ (repr_gen_pow_is_integral hB hmin _ _), end end power_basis end is_integral
df77284af16d9b3558f5bead14f8fbf24fdd34ba
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/ring_theory/polynomial/chebyshev.lean
dd62074f3edbfeb214c349ac841a2eb670eb564f
[ "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
10,927
lean
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Julian Kuelshammer, Heather Macbeth -/ import data.polynomial.derivative import tactic.linear_combination /-! # Chebyshev polynomials The Chebyshev polynomials are two families of polynomials indexed by `β„•`, with integral coefficients. ## Main definitions * `polynomial.chebyshev.T`: the Chebyshev polynomials of the first kind. * `polynomial.chebyshev.U`: the Chebyshev polynomials of the second kind. ## Main statements * The formal derivative of the Chebyshev polynomials of the first kind is a scalar multiple of the Chebyshev polynomials of the second kind. * `polynomial.chebyshev.mul_T`, the product of the `m`-th and `(m + k)`-th Chebyshev polynomials of the first kind is the sum of the `(2 * m + k)`-th and `k`-th Chebyshev polynomials of the first kind. * `polynomial.chebyshev.T_mul`, the `(m * n)`-th Chebyshev polynomial of the first kind is the composition of the `m`-th and `n`-th Chebyshev polynomials of the first kind. ## Implementation details Since Chebyshev polynomials have interesting behaviour over the complex numbers and modulo `p`, we define them to have coefficients in an arbitrary commutative ring, even though technically `β„€` would suffice. The benefit of allowing arbitrary coefficient rings, is that the statements afterwards are clean, and do not have `map (int.cast_ring_hom R)` interfering all the time. ## References [Lionel Ponton, _Roots of the Chebyshev polynomials: A purely algebraic approach_] [ponton2020chebyshev] ## TODO * Redefine and/or relate the definition of Chebyshev polynomials to `linear_recurrence`. * Add explicit formula involving square roots for Chebyshev polynomials * Compute zeroes and extrema of Chebyshev polynomials. * Prove that the roots of the Chebyshev polynomials (except 0) are irrational. * Prove minimax properties of Chebyshev polynomials. -/ noncomputable theory namespace polynomial.chebyshev open polynomial variables (R S : Type*) [comm_ring R] [comm_ring S] /-- `T n` is the `n`-th Chebyshev polynomial of the first kind -/ noncomputable def T : β„• β†’ polynomial R | 0 := 1 | 1 := X | (n + 2) := 2 * X * T (n + 1) - T n @[simp] lemma T_zero : T R 0 = 1 := rfl @[simp] lemma T_one : T R 1 = X := rfl lemma T_two : T R 2 = 2 * X ^ 2 - 1 := by simp only [T, sub_left_inj, sq, mul_assoc] @[simp] lemma T_add_two (n : β„•) : T R (n + 2) = 2 * X * T R (n + 1) - T R n := by rw T lemma T_of_two_le (n : β„•) (h : 2 ≀ n) : T R n = 2 * X * T R (n - 1) - T R (n - 2) := begin obtain ⟨n, rfl⟩ := nat.exists_eq_add_of_le h, rw add_comm, exact T_add_two R n end variables {R S} lemma map_T (f : R β†’+* S) : βˆ€ (n : β„•), map f (T R n) = T S n | 0 := by simp only [T_zero, polynomial.map_one] | 1 := by simp only [T_one, map_X] | (n + 2) := begin simp only [T_add_two, polynomial.map_mul, polynomial.map_sub, map_X, bit0, polynomial.map_add, polynomial.map_one], rw [map_T (n + 1), map_T n], end variables (R S) /-- `U n` is the `n`-th Chebyshev polynomial of the second kind -/ noncomputable def U : β„• β†’ polynomial R | 0 := 1 | 1 := 2 * X | (n + 2) := 2 * X * U (n + 1) - U n @[simp] lemma U_zero : U R 0 = 1 := rfl @[simp] lemma U_one : U R 1 = 2 * X := rfl lemma U_two : U R 2 = 4 * X ^ 2 - 1 := by { simp only [U], ring, } @[simp] lemma U_add_two (n : β„•) : U R (n + 2) = 2 * X * U R (n + 1) - U R n := by rw U lemma U_of_two_le (n : β„•) (h : 2 ≀ n) : U R n = 2 * X * U R (n - 1) - U R (n - 2) := begin obtain ⟨n, rfl⟩ := nat.exists_eq_add_of_le h, rw add_comm, exact U_add_two R n end lemma U_eq_X_mul_U_add_T : βˆ€ (n : β„•), U R (n+1) = X * U R n + T R (n+1) | 0 := by { simp only [U_zero, U_one, T_one], ring } | 1 := by { simp only [U_one, T_two, U_two], ring } | (n + 2) := calc U R (n + 2 + 1) = 2 * X * (X * U R (n + 1) + T R (n + 2)) - (X * U R n + T R (n + 1)) : by simp only [U_add_two, U_eq_X_mul_U_add_T n, U_eq_X_mul_U_add_T (n + 1)] ... = X * (2 * X * U R (n + 1) - U R n) + (2 * X * T R (n + 2) - T R (n + 1)) : by ring ... = X * U R (n + 2) + T R (n + 2 + 1) : by simp only [U_add_two, T_add_two] lemma T_eq_U_sub_X_mul_U (n : β„•) : T R (n+1) = U R (n+1) - X * U R n := by rw [U_eq_X_mul_U_add_T, add_comm (X * U R n), add_sub_cancel] lemma T_eq_X_mul_T_sub_pol_U : βˆ€ (n : β„•), T R (n+2) = X * T R (n+1) - (1 - X ^ 2) * U R n | 0 := by { simp only [T_one, T_two, U_zero], ring } | 1 := by { simp only [T_add_two, T_zero, T_add_two, U_one, T_one], ring } | (n + 2) := calc T R (n + 2 + 2) = 2 * X * T R (n + 2 + 1) - T R (n + 2) : T_add_two _ _ ... = 2 * X * (X * T R (n + 2) - (1 - X ^ 2) * U R (n + 1)) - (X * T R (n + 1) - (1 - X ^ 2) * U R n) : by simp only [T_eq_X_mul_T_sub_pol_U] ... = X * (2 * X * T R (n + 2) - T R (n + 1)) - (1 - X ^ 2) * (2 * X * U R (n + 1) - U R n) : by ring ... = X * T R (n + 2 + 1) - (1 - X ^ 2) * U R (n + 2) : by rw [T_add_two _ (n + 1), U_add_two] lemma one_sub_X_sq_mul_U_eq_pol_in_T (n : β„•) : (1 - X ^ 2) * U R n = X * T R (n + 1) - T R (n + 2) := by rw [T_eq_X_mul_T_sub_pol_U, ←sub_add, sub_self, zero_add] variables {R S} @[simp] lemma map_U (f : R β†’+* S) : βˆ€ (n : β„•), map f (U R n) = U S n | 0 := by simp only [U_zero, polynomial.map_one] | 1 := begin simp only [U_one, map_X, polynomial.map_mul, polynomial.map_add, polynomial.map_one], change map f (1+1) * X = 2 * X, simpa only [polynomial.map_add, polynomial.map_one] end | (n + 2) := begin simp only [U_add_two, polynomial.map_mul, polynomial.map_sub, map_X, bit0, polynomial.map_add, polynomial.map_one], rw [map_U (n + 1), map_U n], end lemma T_derivative_eq_U : βˆ€ (n : β„•), derivative (T R (n + 1)) = (n + 1) * U R n | 0 := by simp only [T_one, U_zero, derivative_X, nat.cast_zero, zero_add, mul_one] | 1 := by { simp only [T_two, U_one, derivative_sub, derivative_one, derivative_mul, derivative_X_pow, nat.cast_one, nat.cast_two], norm_num } | (n + 2) := calc derivative (T R (n + 2 + 1)) = 2 * T R (n + 2) + 2 * X * derivative (T R (n + 1 + 1)) - derivative (T R (n + 1)) : by simp only [T_add_two _ (n + 1), derivative_sub, derivative_mul, derivative_X, derivative_bit0, derivative_one, bit0_zero, zero_mul, zero_add, mul_one] ... = 2 * (U R (n + 1 + 1) - X * U R (n + 1)) + 2 * X * ((n + 1 + 1) * U R (n + 1)) - (n + 1) * U R n : by rw_mod_cast [T_derivative_eq_U, T_derivative_eq_U, T_eq_U_sub_X_mul_U] ... = (n + 1) * (2 * X * U R (n + 1) - U R n) + 2 * U R (n + 2) : by ring ... = (n + 1) * U R (n + 2) + 2 * U R (n + 2) : by rw U_add_two ... = (n + 2 + 1) * U R (n + 2) : by ring ... = (↑(n + 2) + 1) * U R (n + 2) : by norm_cast lemma one_sub_X_sq_mul_derivative_T_eq_poly_in_T (n : β„•) : (1 - X ^ 2) * (derivative (T R (n+1))) = (n + 1) * (T R n - X * T R (n+1)) := calc (1 - X ^ 2) * (derivative (T R (n+1))) = (1 - X ^ 2 ) * ((n + 1) * U R n) : by rw T_derivative_eq_U ... = (n + 1) * ((1 - X ^ 2) * U R n) : by ring ... = (n + 1) * (X * T R (n + 1) - (2 * X * T R (n + 1) - T R n)) : by rw [one_sub_X_sq_mul_U_eq_pol_in_T, T_add_two] ... = (n + 1) * (T R n - X * T R (n+1)) : by ring lemma add_one_mul_T_eq_poly_in_U (n : β„•) : ((n : polynomial R) + 1) * T R (n+1) = X * U R n - (1 - X ^ 2) * derivative ( U R n) := begin have h : derivative (T R (n + 2)) = (U R (n + 1) - X * U R n) + X * derivative (T R (n + 1)) + 2 * X * U R n - (1 - X ^ 2) * derivative (U R n), { conv_lhs { rw T_eq_X_mul_T_sub_pol_U }, simp only [derivative_sub, derivative_mul, derivative_X, derivative_one, derivative_X_pow, one_mul, T_derivative_eq_U], rw [T_eq_U_sub_X_mul_U, nat.cast_bit0, nat.cast_one], ring }, calc ((n : polynomial R) + 1) * T R (n + 1) = ((n : polynomial R) + 1 + 1) * (X * U R n + T R (n + 1)) - X * ((n + 1) * U R n) - (X * U R n + T R (n + 1)) : by ring ... = derivative (T R (n + 2)) - X * derivative (T R (n + 1)) - U R (n + 1) : by rw [←U_eq_X_mul_U_add_T, ←T_derivative_eq_U, ←nat.cast_one, ←nat.cast_add, nat.cast_one, ←T_derivative_eq_U (n + 1)] ... = (U R (n + 1) - X * U R n) + X * derivative (T R (n + 1)) + 2 * X * U R n - (1 - X ^ 2) * derivative (U R n) - X * derivative (T R (n + 1)) - U R (n + 1) : by rw h ... = X * U R n - (1 - X ^ 2) * derivative (U R n) : by ring, end variables (R) /-- The product of two Chebyshev polynomials is the sum of two other Chebyshev polynomials. -/ lemma mul_T : βˆ€ m : β„•, βˆ€ k, 2 * T R m * T R (m + k) = T R (2 * m + k) + T R k | 0 := by simp [two_mul, add_mul] | 1 := by simp [add_comm] | (m + 2) := begin intros k, -- clean up the `T` nat indices in the goal suffices : 2 * T R (m + 2) * T R (m + k + 2) = T R (2 * m + k + 4) + T R k, { have h_nat₁ : 2 * (m + 2) + k = 2 * m + k + 4 := by ring, have h_natβ‚‚ : m + 2 + k = m + k + 2 := by simp [add_comm, add_assoc], simpa [h_nat₁, h_natβ‚‚] using this }, -- clean up the `T` nat indices in the inductive hypothesis applied to `m + 1` and -- `k + 1` have H₁ : 2 * T R (m + 1) * T R (m + k + 2) = T R (2 * m + k + 3) + T R (k + 1), { have h_nat₁ : m + 1 + (k + 1) = m + k + 2 := by ring, have h_natβ‚‚ : 2 * (m + 1) + (k + 1) = 2 * m + k + 3 := by ring, simpa [h_nat₁, h_natβ‚‚] using mul_T (m + 1) (k + 1) }, -- clean up the `T` nat indices in the inductive hypothesis applied to `m` and `k + 2` have Hβ‚‚ : 2 * T R m * T R (m + k + 2) = T R (2 * m + k + 2) + T R (k + 2), { have h_nat₁ : 2 * m + (k + 2) = 2 * m + k + 2 := by simp [add_assoc], have h_natβ‚‚ : m + (k + 2) = m + k + 2 := by simp [add_assoc], simpa [h_nat₁, h_natβ‚‚] using mul_T m (k + 2) }, -- state the `T` recurrence relation for a few useful indices have h₁ := T_add_two R m, have hβ‚‚ := T_add_two R (2 * m + k + 2), have h₃ := T_add_two R k, -- the desired identity is an appropriate linear combination of H₁, Hβ‚‚, h₁, hβ‚‚, h₃ linear_combination (h₁, 2 * T R (m + k + 2)) (H₁, 2 * X) (Hβ‚‚, -1) (hβ‚‚, -1) (h₃, -1), end /-- The `(m * n)`-th Chebyshev polynomial is the composition of the `m`-th and `n`-th -/ lemma T_mul : βˆ€ m : β„•, βˆ€ n : β„•, T R (m * n) = (T R m).comp (T R n) | 0 := by simp | 1 := by simp | (m + 2) := begin intros n, have : 2 * T R n * T R ((m + 1) * n) = T R ((m + 2) * n) + T R (m * n), { convert mul_T R n (m * n); ring }, simp [this, T_mul m, ← T_mul (m + 1)] end end polynomial.chebyshev
0455d11ba3657edef482ece48a3c82ba50539753
d436468d80b739ba7e06843c4d0d2070e43448e5
/src/algebra/pointwise.lean
7f153d03f4b52bd6ba5f5fd9afee7af24762ba15
[ "Apache-2.0" ]
permissive
roro47/mathlib
761fdc002aef92f77818f3fef06bf6ec6fc1a28e
80aa7d52537571a2ca62a3fdf71c9533a09422cf
refs/heads/master
1,599,656,410,625
1,573,649,488,000
1,573,649,488,000
221,452,951
0
0
Apache-2.0
1,573,647,693,000
1,573,647,692,000
null
UTF-8
Lean
false
false
8,469
lean
/- Copyright (c) 2019 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin Pointwise addition and multiplication of sets -/ import data.set.finite data.set.lattice group_theory.group_action namespace set open function variables {Ξ± : Type*} {Ξ² : Type*} (f : Ξ± β†’ Ξ²) @[to_additive] def pointwise_one [has_one Ξ±] : has_one (set Ξ±) := ⟨{1}⟩ local attribute [instance] pointwise_one @[simp, to_additive] lemma mem_pointwise_one [has_one Ξ±] (a : Ξ±) : a ∈ (1 : set Ξ±) ↔ a = 1 := mem_singleton_iff @[to_additive] def pointwise_mul [has_mul Ξ±] : has_mul (set Ξ±) := ⟨λ s t, {a | βˆƒ x ∈ s, βˆƒ y ∈ t, a = x * y}⟩ local attribute [instance] pointwise_one pointwise_mul pointwise_add @[to_additive] lemma mem_pointwise_mul [has_mul Ξ±] {s t : set Ξ±} {a : Ξ±} : a ∈ s * t ↔ βˆƒ x ∈ s, βˆƒ y ∈ t, a = x * y := iff.rfl @[to_additive] lemma mul_mem_pointwise_mul [has_mul Ξ±] {s t : set Ξ±} {a b : Ξ±} (ha : a ∈ s) (hb : b ∈ t) : a * b ∈ s * t := ⟨_, ha, _, hb, rfl⟩ @[to_additive] lemma pointwise_mul_eq_image [has_mul Ξ±] {s t : set Ξ±} : s * t = (Ξ» x : Ξ± Γ— Ξ±, x.fst * x.snd) '' s.prod t := set.ext $ Ξ» a, ⟨ by { rintros ⟨_, _, _, _, rfl⟩, exact ⟨(_, _), mem_prod.mpr βŸ¨β€Ή_β€Ί, β€Ή_β€ΊβŸ©, rfl⟩ }, by { rintros ⟨_, _, rfl⟩, exact ⟨_, (mem_prod.mp β€Ή_β€Ί).1, _, (mem_prod.mp β€Ή_β€Ί).2, rfl⟩ }⟩ @[to_additive] lemma pointwise_mul_finite [has_mul Ξ±] {s t : set Ξ±} (hs : finite s) (ht : finite t) : finite (s * t) := by { rw pointwise_mul_eq_image, apply set.finite_image, exact set.finite_prod hs ht } @[to_additive pointwise_add_add_semigroup] def pointwise_mul_semigroup [semigroup Ξ±] : semigroup (set Ξ±) := { mul_assoc := Ξ» _ _ _, set.ext $ Ξ» _, begin split, { rintros ⟨_, ⟨_, _, _, _, rfl⟩, _, _, rfl⟩, exact ⟨_, β€Ή_β€Ί, _, ⟨_, β€Ή_β€Ί, _, β€Ή_β€Ί, rfl⟩, mul_assoc _ _ _⟩ }, { rintros ⟨_, _, _, ⟨_, _, _, _, rfl⟩, rfl⟩, exact ⟨_, ⟨_, β€Ή_β€Ί, _, β€Ή_β€Ί, rfl⟩, _, β€Ή_β€Ί, (mul_assoc _ _ _).symm⟩ } end, ..pointwise_mul } @[to_additive pointwise_add_add_monoid] def pointwise_mul_monoid [monoid Ξ±] : monoid (set Ξ±) := { one_mul := Ξ» s, set.ext $ Ξ» a, ⟨by {rintros ⟨_, _, _, _, rfl⟩, simp * at *}, Ξ» h, ⟨1, mem_singleton 1, a, h, (one_mul a).symm⟩⟩, mul_one := Ξ» s, set.ext $ Ξ» a, ⟨by {rintros ⟨_, _, _, _, rfl⟩, simp * at *}, Ξ» h, ⟨a, h, 1, mem_singleton 1, (mul_one a).symm⟩⟩, ..pointwise_mul_semigroup, ..pointwise_one } local attribute [instance] pointwise_mul_monoid @[to_additive] lemma singleton.is_mul_hom [has_mul Ξ±] : is_mul_hom (singleton : Ξ± β†’ set Ξ±) := { map_mul := Ξ» x y, set.ext $ Ξ» a, by simp [mem_singleton_iff, mem_pointwise_mul] } @[to_additive is_add_monoid_hom] lemma singleton.is_monoid_hom [monoid Ξ±] : is_monoid_hom (singleton : Ξ± β†’ set Ξ±) := { map_one := rfl, ..singleton.is_mul_hom } @[to_additive] def pointwise_inv [has_inv Ξ±] : has_inv (set Ξ±) := ⟨λ s, {a | a⁻¹ ∈ s}⟩ @[simp, to_additive] lemma pointwise_mul_empty [has_mul Ξ±] (s : set Ξ±) : s * βˆ… = βˆ… := set.ext $ Ξ» a, ⟨by {rintros ⟨_, _, _, _, rfl⟩, tauto}, false.elim⟩ @[simp, to_additive] lemma empty_pointwise_mul [has_mul Ξ±] (s : set Ξ±) : βˆ… * s = βˆ… := set.ext $ Ξ» a, ⟨by {rintros ⟨_, _, _, _, rfl⟩, tauto}, false.elim⟩ @[to_additive] lemma pointwise_mul_subset_mul [has_mul Ξ±] {s₁ sβ‚‚ t₁ tβ‚‚ : set Ξ±} (h₁ : s₁ βŠ† t₁) (hβ‚‚ : sβ‚‚ βŠ† tβ‚‚) : s₁ * sβ‚‚ βŠ† t₁ * tβ‚‚ := by {rintros _ ⟨_, _, _, _, rfl⟩, exact ⟨_, h₁ β€Ή_β€Ί, _, hβ‚‚ β€Ή_β€Ί, rfl⟩ } @[to_additive] lemma pointwise_mul_union [has_mul Ξ±] (s t u : set Ξ±) : s * (t βˆͺ u) = (s * t) βˆͺ (s * u) := begin ext a, split, { rintros ⟨_, _, _, H, rfl⟩, cases H; [left, right]; exact ⟨_, β€Ή_β€Ί, _, β€Ή_β€Ί, rfl⟩ }, { intro H, cases H with H H; { rcases H with ⟨_, _, _, _, rfl⟩, refine ⟨_, β€Ή_β€Ί, _, _, rfl⟩, erw mem_union, simp * } } end @[to_additive] lemma union_pointwise_mul [has_mul Ξ±] (s t u : set Ξ±) : (s βˆͺ t) * u = (s * u) βˆͺ (t * u) := begin ext a, split, { rintros ⟨_, H, _, _, rfl⟩, cases H; [left, right]; exact ⟨_, β€Ή_β€Ί, _, β€Ή_β€Ί, rfl⟩ }, { intro H, cases H with H H; { rcases H with ⟨_, _, _, _, rfl⟩; refine ⟨_, _, _, β€Ή_β€Ί, rfl⟩; erw mem_union; simp * } } end @[to_additive] lemma pointwise_mul_eq_Union_mul_left [has_mul Ξ±] {s t : set Ξ±} : s * t = ⋃a∈s, (Ξ»x, a * x) '' t := by { ext y; split; simp only [mem_Union]; rintros ⟨a, ha, x, hx, ax⟩; exact ⟨a, ha, x, hx, ax.symm⟩ } @[to_additive] lemma pointwise_mul_eq_Union_mul_right [has_mul Ξ±] {s t : set Ξ±} : s * t = ⋃a∈t, (Ξ»x, x * a) '' s := by { ext y; split; simp only [mem_Union]; rintros ⟨a, ha, x, hx, ax⟩; exact ⟨x, hx, a, ha, ax.symm⟩ } @[to_additive] lemma pointwise_mul_ne_empty [has_mul Ξ±] {s t : set Ξ±} : s β‰  βˆ… β†’ t β‰  βˆ… β†’ s * t β‰  βˆ… := begin simp only [ne_empty_iff_exists_mem], rintros ⟨x, hx⟩ ⟨y, hy⟩, exact ⟨x * y, mul_mem_pointwise_mul hx hy⟩ end @[simp, to_additive] lemma univ_pointwise_mul_univ [monoid Ξ±] : (univ : set Ξ±) * univ = univ := begin have : βˆ€x, βˆƒa b : Ξ±, x = a * b := Ξ»x, ⟨x, ⟨1, (mul_one x).symm⟩⟩, show {a | βˆƒ x ∈ univ, βˆƒ y ∈ univ, a = x * y} = univ, simpa [eq_univ_iff_forall] end def pointwise_mul_fintype [has_mul Ξ±] [decidable_eq Ξ±] (s t : set Ξ±) [hs : fintype s] [ht : fintype t] : fintype (s * t : set Ξ±) := by { rw pointwise_mul_eq_image, apply set.fintype_image } def pointwise_add_fintype [has_add Ξ±] [decidable_eq Ξ±] (s t : set Ξ±) [hs : fintype s] [ht : fintype t] : fintype (s + t : set Ξ±) := by { rw pointwise_add_eq_image, apply set.fintype_image } attribute [to_additive] set.pointwise_mul_fintype section monoid def pointwise_mul_semiring [monoid Ξ±] : semiring (set Ξ±) := { add := (βŠ”), zero := βˆ…, add_assoc := set.union_assoc, zero_add := set.empty_union, add_zero := set.union_empty, add_comm := set.union_comm, zero_mul := empty_pointwise_mul, mul_zero := pointwise_mul_empty, left_distrib := pointwise_mul_union, right_distrib := union_pointwise_mul, ..pointwise_mul_monoid } def pointwise_mul_comm_semiring [comm_monoid Ξ±] : comm_semiring (set Ξ±) := { mul_comm := Ξ» s t, set.ext $ Ξ» a, by split; { rintros ⟨_, _, _, _, rfl⟩, exact ⟨_, β€Ή_β€Ί, _, β€Ή_β€Ί, mul_comm _ _⟩ }, ..pointwise_mul_semiring } local attribute [instance] pointwise_mul_semiring section is_mul_hom open is_mul_hom variables [has_mul Ξ±] [has_mul Ξ²] (m : Ξ± β†’ Ξ²) [is_mul_hom m] @[to_additive] lemma image_pointwise_mul (s t : set Ξ±) : m '' (s * t) = m '' s * m '' t := set.ext $ assume y, begin split, { rintros ⟨_, ⟨_, _, _, _, rfl⟩, rfl⟩, refine ⟨_, mem_image_of_mem _ β€Ή_β€Ί, _, mem_image_of_mem _ β€Ή_β€Ί, map_mul _ _ _⟩ }, { rintros ⟨_, ⟨_, _, rfl⟩, _, ⟨_, _, rfl⟩, rfl⟩, refine ⟨_, ⟨_, β€Ή_β€Ί, _, β€Ή_β€Ί, rfl⟩, map_mul _ _ _⟩ } end @[to_additive] lemma preimage_pointwise_mul_preimage_subset (s t : set Ξ²) : m ⁻¹' s * m ⁻¹' t βŠ† m ⁻¹' (s * t) := begin rintros _ ⟨_, _, _, _, rfl⟩, exact ⟨_, β€Ή_β€Ί, _, β€Ή_β€Ί, map_mul _ _ _⟩, end end is_mul_hom variables [monoid Ξ±] [monoid Ξ²] [is_monoid_hom f] lemma pointwise_mul_image_is_semiring_hom : is_semiring_hom (image f) := { map_zero := image_empty _, map_one := by erw [image_singleton, is_monoid_hom.map_one f]; refl, map_add := image_union _, map_mul := image_pointwise_mul _ } local attribute [instance] singleton.is_monoid_hom def pointwise_mul_action : mul_action Ξ± (set Ξ±) := { smul := Ξ» a s, ({a} : set Ξ±) * s, one_smul := one_mul, mul_smul := Ξ» _ _ _, show {_} * _ = _, by { erw is_monoid_hom.map_mul (singleton : Ξ± β†’ set Ξ±), apply mul_assoc } } local attribute [instance] pointwise_mul_action lemma mem_smul_set {a : Ξ±} {s : set Ξ±} {x : Ξ±} : x ∈ a β€’ s ↔ βˆƒ y ∈ s, x = a * y := by { erw mem_pointwise_mul, simp } lemma smul_set_eq_image {a : Ξ±} {s : set Ξ±} : a β€’ s = (Ξ» b, a * b) '' s := set.ext $ Ξ» x, begin simp only [mem_smul_set, exists_prop, mem_image], apply exists_congr, intro y, apply and_congr iff.rfl, split; exact eq.symm end end monoid end set
4c5365ed7b648687eea05c0d92d0dd180d3d7934
a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940
/tests/lean/smartUnfolding.lean
1c62670e82377154ff275072d5cc8a73d110c5e5
[ "Apache-2.0" ]
permissive
WojciechKarpiel/lean4
7f89706b8e3c1f942b83a2c91a3a00b05da0e65b
f6e1314fa08293dea66a329e05b6c196a0189163
refs/heads/master
1,686,633,402,214
1,625,821,189,000
1,625,821,258,000
384,640,886
0
0
Apache-2.0
1,625,903,617,000
1,625,903,026,000
null
UTF-8
Lean
false
false
245
lean
theorem ex1 (x y : Nat) (h : x + 2 = y + 2) : x = y := by injection h with h traceState -- without smart unfolding the state would be a mess injection h with h traceState -- without smart unfolding the state would be a mess assumption
ddb39e66d3757e691b50b6761ce704b9a8c2c8bd
d1a52c3f208fa42c41df8278c3d280f075eb020c
/stage0/src/Lean/Meta/Tactic/Contradiction.lean
2ae00020133bc2b3d405686c9df245fd9827bd4a
[ "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
6,623
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Meta.MatchUtil import Lean.Meta.Tactic.Assumption import Lean.Meta.Tactic.Cases import Lean.Meta.Tactic.Apply namespace Lean.Meta structure Contradiction.Config where useDecide : Bool := true /- When checking for empty types, `searchFuel` specifies the number of goals visited. -/ searchFuel : Nat := 16 /- Support for hypotheses such as ``` h : (x y : Nat) (ys : List Nat) β†’ x = 0 β†’ y::ys = [a, b, c] β†’ False ``` This kind of hypotheses appear when proving conditional equation theorems for match expressions. -/ genDiseq : Bool := false -- We only consider inductives with no constructors and indexed families private def isElimEmptyInductiveCandidate (fvarId : FVarId) : MetaM Bool := do let localDecl ← getLocalDecl fvarId let type ← whnfD localDecl.type matchConstInduct type.getAppFn (fun _ => pure false) fun info _ => do return info.ctors.length == 0 || info.numIndices > 0 namespace ElimEmptyInductive abbrev M := StateRefT Nat MetaM instance : MonadBacktrack SavedState M where saveState := Meta.saveState restoreState s := s.restore partial def elim (mvarId : MVarId) (fvarId : FVarId) : M Bool := do if (← get) == 0 then trace[Meta.Tactic.contradiction] "elimEmptyInductive out-of-fuel" return false modify (. - 1) -- We only consider inductives with no constructors and indexed families commitWhen do let subgoals ← try cases mvarId fvarId catch ex => trace[Meta.Tactic.contradiction] "{ex.toMessageData}"; return false trace[Meta.Tactic.contradiction] "elimEmptyInductive, number subgoals: {subgoals.size}" for subgoal in subgoals do -- If one of the fields is uninhabited, then we are done let found ← withMVarContext subgoal.mvarId do for field in subgoal.fields do let field := subgoal.subst.apply field if field.isFVar then if (← isElimEmptyInductiveCandidate field.fvarId!) then if (← elim subgoal.mvarId field.fvarId!) then return true return false unless found do return false return true end ElimEmptyInductive private def elimEmptyInductive (mvarId : MVarId) (fvarId : FVarId) (fuel : Nat) : MetaM Bool := do withMVarContext mvarId do if (← isElimEmptyInductiveCandidate fvarId) then commitWhen do ElimEmptyInductive.elim (← exfalso mvarId) fvarId |>.run' fuel else return false /-- Return true if `e` is of the form `(x : Ξ±) β†’ ... β†’ s = t β†’ ... β†’ False` -/ private def isGenDiseq (e : Expr) : Bool := match e with | Expr.forallE _ d b _ => (d.isEq || b.hasLooseBVar 0) && isGenDiseq b | _ => e.isConstOf ``False /-- Close goal if `localDecl` is a "generalized disequality". Example: ``` h : (x y : Nat) (ys : List Nat) β†’ x = 0 β†’ y::ys = [a, b, c] β†’ False ``` This kind of hypotheses is created when we generate conditional equations for match expressions. -/ private def processGenDiseq (mvarId : MVarId) (localDecl : LocalDecl) : MetaM Bool := do assert! isGenDiseq localDecl.type let val? ← withNewMCtxDepth do let (args, _, _) ← forallMetaTelescope localDecl.type for arg in args do let argType ← inferType arg if let some (_, lhs, rhs) ← matchEq? argType then unless (← isDefEq lhs rhs) do return none unless (← isDefEq arg (← mkEqRefl lhs)) do return none let falseProof ← instantiateMVars (mkAppN localDecl.toExpr args) if (← hasAssignableMVar falseProof) then return none return some (← mkFalseElim (← getMVarType mvarId) falseProof) if let some val := val? then assignExprMVar mvarId val return true else return false def contradictionCore (mvarId : MVarId) (config : Contradiction.Config) : MetaM Bool := do withMVarContext mvarId do checkNotAssigned mvarId `contradiction for localDecl in (← getLCtx) do unless localDecl.isAuxDecl do -- (h : Β¬ p) (h' : p) if let some p ← matchNot? localDecl.type then if let some pFVarId ← findLocalDeclWithType? p then assignExprMVar mvarId (← mkAbsurd (← getMVarType mvarId) (mkFVar pFVarId) localDecl.toExpr) return true -- (h : x β‰  x) if let some (_, lhs, rhs) ← matchNe? localDecl.type then if (← isDefEq lhs rhs) then assignExprMVar mvarId (← mkAbsurd (← getMVarType mvarId) (← mkEqRefl lhs) localDecl.toExpr) return true let mut isEq := false -- (h : ctor₁ ... = ctorβ‚‚ ...) if let some (_, lhs, rhs) ← matchEq? localDecl.type then isEq := true if let some lhsCtor ← matchConstructorApp? lhs then if let some rhsCtor ← matchConstructorApp? rhs then if lhsCtor.name != rhsCtor.name then assignExprMVar mvarId (← mkNoConfusion (← getMVarType mvarId) localDecl.toExpr) return true -- (h : p) s.t. `decide p` evaluates to `false` if config.useDecide && !localDecl.type.hasFVar then let type ← instantiateMVars localDecl.type if !type.hasMVar && !type.hasFVar then try let d ← mkDecide localDecl.type let r ← withDefault <| whnf d if r.isConstOf ``false then let hn := mkAppN (mkConst ``of_decide_eq_false) <| d.getAppArgs.push (← mkEqRefl d) assignExprMVar mvarId (← mkAbsurd (← getMVarType mvarId) localDecl.toExpr hn) return true catch _ => pure () -- "generalized" diseq if config.genDiseq && isGenDiseq localDecl.type then if (← processGenDiseq mvarId localDecl) then return true -- (h : <empty-inductive-type>) unless isEq do -- We do not use `elimEmptyInductive` for equality, since `cases h` produces a huge proof -- when `(h : 10000 = 10001)`. TODO: `cases` add a threshold at `cases` if (← elimEmptyInductive mvarId localDecl.fvarId config.searchFuel) then return true return false def contradiction (mvarId : MVarId) (config : Contradiction.Config := {}) : MetaM Unit := unless (← contradictionCore mvarId config) do throwTacticEx `contradiction mvarId "" builtin_initialize registerTraceClass `Meta.Tactic.contradiction end Lean.Meta
63c8514bfd9d935318194af25b0d1d264e64cf2a
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/lake/Lake/Util/EStateT.lean
1af244868454a839d9ef7d226c9012715a8e2b4f
[ "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
684
lean
/- Copyright (c) 2022 Mac Malone. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mac Malone -/ namespace Lake /-- An exception plus state monad transformer (ΞΉ.e., `ExceptT` + `StateT`). -/ abbrev EStateT.{u,v} (Ξ΅ : Type u) (Οƒ : Type u) (m : Type u β†’ Type v) := ExceptT Ξ΅ <| StateT Οƒ m namespace EStateT variable {Ξ΅ : Type u} {Οƒ : Type u} {m : Type u β†’ Type v} @[inline] def run (init : Οƒ) (self : EStateT Ξ΅ Οƒ m Ξ±) : m (Except Ξ΅ Ξ± Γ— Οƒ) := ExceptT.run self |>.run init @[inline] def run' [Functor m] (init : Οƒ) (self : EStateT Ξ΅ Οƒ m Ξ±) : m (Except Ξ΅ Ξ±) := ExceptT.run self |>.run' init end EStateT
832c468c33bc44a4d393e07ad74dc51bdb351323
d450724ba99f5b50b57d244eb41fef9f6789db81
/src/homework/hw7.lean
540590c313093d6f0e6206037ebcf8b0524d57c3
[]
no_license
jakekauff/CS2120F21
4f009adeb4ce4a148442b562196d66cc6c04530c
e69529ec6f5d47a554291c4241a3d8ec4fe8f5ad
refs/heads/main
1,693,841,880,030
1,637,604,848,000
1,637,604,848,000
399,946,698
0
0
null
null
null
null
UTF-8
Lean
false
false
3,987
lean
import data.set namespace relation -- PRELIMINARY SETUP /- Preliminary set up. For the rest of this file, we specify an arbitrary binary relation, r, on an arbitrary type, Ξ², as a two-place predicate, with infix notation x β‰Ί y for (r x y). One can pronounce these expressions in English as "x is related to y". -/ variables {Ξ± Ξ² : Type} (r : Ξ² β†’ Ξ² β†’ Prop) local infix `β‰Ί` : 50 := r /- The default Lean libraries are missing definitions for the assympetric property of relations and for the notion of a powerset. We define these terms for use in the rest of this file. -/ def asymmetric := βˆ€ ⦃x y⦄, x β‰Ί y β†’ Β¬ y β‰Ί x def powerset (s : set Ξ²) := { s' | s' βŠ† s} -- PROBLEMS /- #1: Give both a formal and an English-language proof. Then answer the question, is the proposition true if you remove the first condition, that Ξ² is inhabited? Briefly explain your answer (in English). -/ example : (βˆƒ (b : Ξ²), true) β†’ asymmetric r β†’ Β¬reflexive r := begin end /- #2. Logic, like programming, is subtle. It's very easy for humans to miss subtle corner cases. As an example, today I ran across a problem in a textbook by Paul Traiger, a professor emeritus of philosophy and cognitive science at Occidental College. He asks students to prove that if a relation is both transitive and reflexive that it cannot be anti-symmetric. See the question at the very bottom of the page here: https://sites.oxy.edu/traiger/logic/exercises/chapter13/properties_of_relations_exercise.html Is the conjecture actually logically valid? If not, what condition needs to be added to make it so? Try prove this/his version of the conjecture, as articulated slightly differently below. If you get stuck, then you need to figure out an additional condition that needs to be added as a premise to make the proposition true. In that case, add the premise and then show that the updated conjecture is true. -/ example : transitive r β†’ reflexive r β†’ Β¬ asymmetric r := begin end /- #3: Prove that the subset relation on the powerset of any set, s, is antisymmetric. Formally state and prove, and then give an informal proof of, this proposition. -/ example : βˆ€ (s : set Ξ²) (s1 s2 ∈ powerset s), s1 βŠ† s2 β†’ s2 βŠ† s1 β†’ s1 = s2 := begin end /- Given two natural numbers, n and m, we will say that m divides n if there is a natural number, k, such that n = k*m. Here's a formal definition of this relation. -/ def divides (m n : β„•) := βˆƒ k, n = k * m /- #4: Formally and informally state and prove each of the following propositions. Remember that the ring tactic is useful for producing proofs of simple algebraic equalities involving + and *. You can use the phrase, "by basic algebra" when translating the use of this tactic into English. (Or if you wanted to be truly Hobbit-like you could say "by the ring axioms!") -/ -- A: For any n, 1 divides n. example : βˆ€ n, divides 1 n := begin end -- B. For any n, n divides n example : βˆ€ n, divides n n := begin end -- #C. prove that divides is reflexive example : reflexive divides := begin end -- #D. prove that divides is transitive example : transitive divides := begin end /- E. Is divides symmetric? if yes, give a proof, otherwise give a counterexample and a brief explanation to show that it's not. -/ -- Answer here /- #F. Prove that divides is antisymmetric. -/ example : anti_symmetric divides := begin end /- #4 Prove the following propositions. Remember that throughout this file, each definition implicitly includes Ξ² as a type and r as an arbitrary binary relation on Ξ². In addition to formal proofs, give an English language proof of the last of the three problems. -/ -- A example : asymmetric r β†’ irreflexive r := begin end -- B example : irreflexive r β†’ transitive r β†’ asymmetric r := begin end -- C example : transitive r β†’ Β¬ symmetric r β†’ Β¬ irreflexive r := begin end end relation
2e25c9c6c0fe012456e0f2e8910dd70157eee25e
618003631150032a5676f229d13a079ac875ff77
/src/geometry/manifold/basic_smooth_bundle.lean
173fb9c6f99903f2b4e7ee4e1280a5d418a3ff10
[ "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
31,950
lean
/- Copyright (c) 2019 SΓ©bastien GouΓ«zel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: SΓ©bastien GouΓ«zel -/ import topology.topological_fiber_bundle import geometry.manifold.smooth_manifold_with_corners /-! # Basic smooth bundles In general, a smooth bundle is a bundle over a smooth manifold, whose fiber is a manifold, and for which the coordinate changes are smooth. In this definition, there are charts involved at several places: in the manifold structure of the base, in the manifold structure of the fibers, and in the local trivializations. This makes it a complicated object in general. There is however a specific situation where things are much simpler: when the fiber is a vector space (no need for charts for the fibers), and when the local trivializations of the bundle and the charts of the base coincide. Then everything is expressed in terms of the charts of the base, making for a much simpler overall structure, which is easier to manipulate formally. Most vector bundles that naturally occur in differential geometry are of this form: the tangent bundle, the cotangent bundle, differential forms (used to define de Rham cohomology) and the bundle of Riemannian metrics. Therefore, it is worth defining a specific constructor for this kind of bundle, that we call basic smooth bundles. A basic smooth bundle is thus a smooth bundle over a smooth manifold whose fiber is a vector space, and which is trivial in the coordinate charts of the base. (We recall that in our notion of manifold there is a distinguished atlas, which does not need to be maximal: we require the triviality above this specific atlas). It can be constructed from a basic smooth bundled core, defined below, specifying the changes in the fiber when one goes from one coordinate chart to another one. We do not require that this changes in fiber are linear, but only diffeomorphisms. ## Main definitions * `basic_smooth_bundle_core I M F`: assuming that `M` is a smooth manifold over the model with corners `I` on `(π•œ, E, H)`, and `F` is a normed vector space over `π•œ`, this structure registers, for each pair of charts of `M`, a smooth change of coordinates on `F`. This is the core structure from which one will build a smooth bundle with fiber `F` over `M`. Let `Z` be a basic smooth bundle core over `M` with fiber `F`. We define `Z.to_topological_fiber_bundle_core`, the (topological) fiber bundle core associated to `Z`. From it, we get a space `Z.to_topological_fiber_bundle_core.total_space` (which as a Type is just `M Γ— F`), with the fiber bundle topology. It inherits a manifold structure (where the charts are in bijection with the charts of the basis). We show that this manifold is smooth. Then we use this machinery to construct the tangent bundle of a smooth manifold. * `tangent_bundle_core I M`: the basic smooth bundle core associated to a smooth manifold `M` over a model with corners `I`. * `tangent_bundle I M` : the total space of `tangent_bundle_core I M`. It is itself a smooth manifold over the model with corners `I.tangent`, the product of `I` and the trivial model with corners on `E`. * `tangent_space I x` : the tangent space to `M` at `x` * `tangent_bundle.proj I M`: the projection from the tangent bundle to the base manifold ## Implementation notes In the definition of a basic smooth bundle core, we do not require that the coordinate changes of the fibers are linear map, only that they are diffeomorphisms. Therefore, the fibers of the resulting fiber bundle do not inherit a vector space structure (as an algebraic object) in general. As the fiber, as a type, is just `F`, one can still always register the vector space structure, but it does not make sense to do so (i.e., it will not lead to any useful theorem) unless this structure is canonical, i.e., the coordinate changes are linear maps. For instance, we register the vector space structure on the fibers of the tangent bundle. However, we do not register the normed space structure coming from that of `F` (as it is not canonical, and we also want to keep the possibility to add a Riemannian structure on the manifold later on without having two competing normed space instances on the tangent spaces). We require `F` to be a normed space, and not just a topological vector space, as we want to talk about smooth functions on `F`. The notion of derivative requires a norm to be defined. ## TODO construct the cotangent bundle, and the bundles of differential forms. They should follow functorially from the description of the tangent bundle as a basic smooth bundle. ## Tags Smooth fiber bundle, vector bundle, tangent space, tangent bundle -/ noncomputable theory universe u open topological_space set /-- Core structure used to create a smooth bundle above `M` (a manifold over the model with corner `I`) with fiber the normed vector space `F` over `π•œ`, which is trivial in the chart domains of `M`. This structure registers the changes in the fibers when one changes coordinate charts in the base. We do not require the change of coordinates of the fibers to be linear, only smooth. Therefore, the fibers of the resulting bundle will not inherit a canonical vector space structure in general. -/ structure basic_smooth_bundle_core {π•œ : Type*} [nondiscrete_normed_field π•œ] {E : Type*} [normed_group E] [normed_space π•œ E] {H : Type*} [topological_space H] (I : model_with_corners π•œ E H) (M : Type*) [topological_space M] [manifold H M] [smooth_manifold_with_corners I M] (F : Type*) [normed_group F] [normed_space π•œ F] := (coord_change : atlas H M β†’ atlas H M β†’ H β†’ F β†’ F) (coord_change_self : βˆ€ i : atlas H M, βˆ€ x ∈ i.1.target, βˆ€ v, coord_change i i x v = v) (coord_change_comp : βˆ€ i j k : atlas H M, βˆ€ x ∈ ((i.1.symm.trans j.1).trans (j.1.symm.trans k.1)).source, βˆ€ v, (coord_change j k ((i.1.symm.trans j.1) x)) (coord_change i j x v) = coord_change i k x v) (coord_change_smooth : βˆ€ i j : atlas H M, times_cont_diff_on π•œ ⊀ (Ξ»p : E Γ— F, coord_change i j (I.symm p.1) p.2) ((I '' (i.1.symm.trans j.1).source).prod (univ : set F))) /-- The trivial basic smooth bundle core, in which all the changes of coordinates are the identity. -/ def trivial_basic_smooth_bundle_core {π•œ : Type*} [nondiscrete_normed_field π•œ] {E : Type*} [normed_group E] [normed_space π•œ E] {H : Type*} [topological_space H] (I : model_with_corners π•œ E H) (M : Type*) [topological_space M] [manifold H M] [smooth_manifold_with_corners I M] (F : Type*) [normed_group F] [normed_space π•œ F] : basic_smooth_bundle_core I M F := { coord_change := Ξ» i j x v, v, coord_change_self := Ξ» i x hx v, rfl, coord_change_comp := Ξ» i j k x hx v, rfl, coord_change_smooth := Ξ» i j, times_cont_diff_snd.times_cont_diff_on } namespace basic_smooth_bundle_core variables {π•œ : Type*} [nondiscrete_normed_field π•œ] {E : Type*} [normed_group E] [normed_space π•œ E] {H : Type*} [topological_space H] {I : model_with_corners π•œ E H} {M : Type*} [topological_space M] [manifold H M] [smooth_manifold_with_corners I M] {F : Type*} [normed_group F] [normed_space π•œ F] (Z : basic_smooth_bundle_core I M F) instance : inhabited (basic_smooth_bundle_core I M F) := ⟨trivial_basic_smooth_bundle_core I M F⟩ /-- Fiber bundle core associated to a basic smooth bundle core -/ def to_topological_fiber_bundle_core : topological_fiber_bundle_core (atlas H M) M F := { base_set := Ξ»i, i.1.source, is_open_base_set := Ξ»i, i.1.open_source, index_at := Ξ»x, ⟨chart_at H x, chart_mem_atlas H x⟩, mem_base_set_at := Ξ»x, mem_chart_source H x, coord_change := Ξ»i j x v, Z.coord_change i j (i.1 x) v, coord_change_self := Ξ»i x hx v, Z.coord_change_self i (i.1 x) (i.1.map_source hx) v, coord_change_comp := Ξ»i j k x ⟨⟨hx1, hx2⟩, hx3⟩ v, begin have := Z.coord_change_comp i j k (i.1 x) _ v, convert this using 2, { simp [hx1] }, { simp [local_equiv.trans_source, hx1, hx2, hx3, i.1.map_source, j.1.map_source] } end, coord_change_continuous := Ξ»i j, begin have A : continuous_on (Ξ»p : E Γ— F, Z.coord_change i j (I.symm p.1) p.2) ((I '' (i.1.symm.trans j.1).source).prod (univ : set F)) := (Z.coord_change_smooth i j).continuous_on, have B : continuous_on (Ξ»x : M, I (i.1 x)) i.1.source := I.continuous.comp_continuous_on i.1.continuous_on, have C : continuous_on (Ξ»p : M Γ— F, (⟨I (i.1 p.1), p.2⟩ : E Γ— F)) (i.1.source.prod univ), { apply continuous_on.prod _ continuous_snd.continuous_on, exact B.comp continuous_fst.continuous_on (prod_subset_preimage_fst _ _) }, have C' : continuous_on (Ξ»p : M Γ— F, (⟨I (i.1 p.1), p.2⟩ : E Γ— F)) ((i.1.source ∩ j.1.source).prod univ) := continuous_on.mono C (prod_mono (inter_subset_left _ _) (subset.refl _)), have D : (i.1.source ∩ j.1.source).prod univ βŠ† (Ξ» (p : M Γ— F), (I (i.1 p.1), p.2)) ⁻¹' ((I '' (i.1.symm.trans j.1).source).prod univ), { rintros ⟨x, v⟩ hx, simp at hx, simp [mem_image_of_mem, local_equiv.trans_source, hx] }, convert continuous_on.comp A C' D, ext p, simp end } @[simp] lemma base_set (i : atlas H M) : Z.to_topological_fiber_bundle_core.base_set i = i.1.source := rfl /-- Local chart for the total space of a basic smooth bundle -/ def chart {e : local_homeomorph M H} (he : e ∈ atlas H M) : local_homeomorph (Z.to_topological_fiber_bundle_core.total_space) (H Γ— F) := (Z.to_topological_fiber_bundle_core.local_triv ⟨e, he⟩).trans (local_homeomorph.prod e (local_homeomorph.refl F)) @[simp] lemma chart_source (e : local_homeomorph M H) (he : e ∈ atlas H M) : (Z.chart he).source = Z.to_topological_fiber_bundle_core.proj ⁻¹' e.source := by { ext p, simp [chart, local_equiv.trans_source] } @[simp] lemma chart_target (e : local_homeomorph M H) (he : e ∈ atlas H M) : (Z.chart he).target = e.target.prod univ := begin simp only [chart, local_equiv.trans_target, local_homeomorph.prod_to_local_equiv, id.def, local_homeomorph.trans_to_local_equiv, local_equiv.refl_target, local_homeomorph.refl_local_equiv, local_equiv.prod_target, local_equiv.refl_coe, local_homeomorph.coe_coe_symm, local_equiv.refl_symm, local_equiv.prod_coe_symm], ext p, split; simp [e.map_target] {contextual := tt} end /-- The total space of a basic smooth bundle is endowed with a manifold structure, where the charts are in bijection with the charts of the basis. -/ instance to_manifold : manifold (H Γ— F) Z.to_topological_fiber_bundle_core.total_space := { atlas := ⋃(e : local_homeomorph M H) (he : e ∈ atlas H M), {Z.chart he}, chart_at := Ξ»p, Z.chart (chart_mem_atlas H p.1), mem_chart_source := Ξ»p, by simp [mem_chart_source], chart_mem_atlas := Ξ»p, begin simp only [mem_Union, mem_singleton_iff, chart_mem_atlas], exact ⟨chart_at H p.1, chart_mem_atlas H p.1, rfl⟩ end } lemma mem_atlas_iff (f : local_homeomorph Z.to_topological_fiber_bundle_core.total_space (H Γ— F)) : f ∈ atlas (H Γ— F) Z.to_topological_fiber_bundle_core.total_space ↔ βˆƒ(e : local_homeomorph M H) (he : e ∈ atlas H M), f = Z.chart he := by simp [atlas, manifold.atlas] @[simp] lemma mem_chart_source_iff (p q : Z.to_topological_fiber_bundle_core.total_space) : p ∈ (chart_at (H Γ— F) q).source ↔ p.1 ∈ (chart_at H q.1).source := by simp [chart_at, manifold.chart_at] @[simp] lemma mem_chart_target_iff (p : H Γ— F) (q : Z.to_topological_fiber_bundle_core.total_space) : p ∈ (chart_at (H Γ— F) q).target ↔ p.1 ∈ (chart_at H q.1).target := by simp [chart_at, manifold.chart_at] @[simp] lemma coe_chart_at_fst (p q : Z.to_topological_fiber_bundle_core.total_space) : (((chart_at (H Γ— F) q) : _ β†’ H Γ— F) p).1 = (chart_at H q.1 : _ β†’ H) p.1 := rfl @[simp] lemma coe_chart_at_symm_fst (p : H Γ— F) (q : Z.to_topological_fiber_bundle_core.total_space) : (((chart_at (H Γ— F) q).symm : H Γ— F β†’ Z.to_topological_fiber_bundle_core.total_space) p).1 = ((chart_at H q.1).symm : H β†’ M) p.1 := rfl /-- Smooth manifold structure on the total space of a basic smooth bundle -/ instance to_smooth_manifold : smooth_manifold_with_corners (I.prod (model_with_corners_self π•œ F)) Z.to_topological_fiber_bundle_core.total_space := begin /- We have to check that the charts belong to the smooth groupoid, i.e., they are smooth on their source, and their inverses are smooth on the target. Since both objects are of the same kind, it suffices to prove the first statement in A below, and then glue back the pieces at the end. -/ let J := model_with_corners.to_local_equiv (I.prod (model_with_corners_self π•œ F)), have A : βˆ€ (e e' : local_homeomorph M H) (he : e ∈ atlas H M) (he' : e' ∈ atlas H M), times_cont_diff_on π•œ ⊀ (J ∘ ((Z.chart he).symm.trans (Z.chart he')) ∘ J.symm) (J.symm ⁻¹' ((Z.chart he).symm.trans (Z.chart he')).source ∩ range J), { assume e e' he he', have : J.symm ⁻¹' ((chart Z he).symm.trans (chart Z he')).source ∩ range J = (I.symm ⁻¹' (e.symm.trans e').source ∩ range I).prod univ, { have : range (Ξ» (p : H Γ— F), (I (p.fst), id p.snd)) = (range I).prod (range (id : F β†’ F)) := prod_range_range_eq.symm, simp at this, ext p, simp [-mem_range, J, local_equiv.trans_source, chart, model_with_corners.prod, local_equiv.trans_target, this], split, { tauto }, { exact λ⟨⟨hx1, hx2⟩, hx3⟩, ⟨⟨⟨hx1, e.map_target hx1⟩, hx2⟩, hx3⟩ } }, rw this, -- check separately that the two components of the coordinate change are smooth apply times_cont_diff_on.prod, show times_cont_diff_on π•œ ⊀ (Ξ» (p : E Γ— F), (I ∘ e' ∘ e.symm ∘ I.symm) p.1) ((I.symm ⁻¹' (e.symm.trans e').source ∩ range I).prod (univ : set F)), { -- the coordinate change on the base is just a coordinate change for `M`, smooth since -- `M` is smooth have A : times_cont_diff_on π•œ ⊀ (I ∘ (e.symm.trans e') ∘ I.symm) (I.symm ⁻¹' (e.symm.trans e').source ∩ range I) := (has_groupoid.compatible (times_cont_diff_groupoid ⊀ I) he he').1, have B : times_cont_diff_on π•œ ⊀ (Ξ»p : E Γ— F, p.1) ((I.symm ⁻¹' (e.symm.trans e').source ∩ range I).prod univ) := times_cont_diff_fst.times_cont_diff_on, exact times_cont_diff_on.comp A B (prod_subset_preimage_fst _ _) }, show times_cont_diff_on π•œ ⊀ (Ξ» (p : E Γ— F), Z.coord_change ⟨chart_at H (e.symm (I.symm p.1)), _⟩ ⟨e', he'⟩ ((chart_at H (e.symm (I.symm p.1)) : M β†’ H) (e.symm (I.symm p.1))) (Z.coord_change ⟨e, he⟩ ⟨chart_at H (e.symm (I.symm p.1)), _⟩ (e (e.symm (I.symm p.1))) p.2)) ((I.symm ⁻¹' (e.symm.trans e').source ∩ range I).prod (univ : set F)), { /- The coordinate change in the fiber is more complicated as its definition involves the reference chart chosen at each point. However, it appears with its inverse, so using the cocycle property one can get rid of it, and then conclude using the smoothness of the cocycle as given in the definition of basic smooth bundles. -/ have := Z.coord_change_smooth ⟨e, he⟩ ⟨e', he'⟩, rw model_with_corners.image at this, apply times_cont_diff_on.congr this, rintros ⟨x, v⟩ hx, simp [local_equiv.trans_source] at hx, let f := chart_at H (e.symm (I.symm x)), have A : I.symm x ∈ ((e.symm.trans f).trans (f.symm.trans e')).source, by simp [local_equiv.trans_source, hx.1.1, hx.1.2, mem_chart_source, f.map_source], rw e.right_inv hx.1.1, have := Z.coord_change_comp ⟨e, he⟩ ⟨f, chart_mem_atlas _ _⟩ ⟨e', he'⟩ (I.symm x) A v, simpa using this } }, haveI : has_groupoid Z.to_topological_fiber_bundle_core.total_space (times_cont_diff_groupoid ⊀ (I.prod (model_with_corners_self π•œ F))) := begin split, assume eβ‚€ eβ‚€' heβ‚€ heβ‚€', rcases (Z.mem_atlas_iff _).1 heβ‚€ with ⟨e, he, rfl⟩, rcases (Z.mem_atlas_iff _).1 heβ‚€' with ⟨e', he', rfl⟩, rw [times_cont_diff_groupoid, mem_groupoid_of_pregroupoid], exact ⟨A e e' he he', A e' e he' he⟩ end, constructor end end basic_smooth_bundle_core section tangent_bundle variables {π•œ : Type*} [nondiscrete_normed_field π•œ] {E : Type*} [normed_group E] [normed_space π•œ E] {H : Type*} [topological_space H] (I : model_with_corners π•œ E H) (M : Type*) [topological_space M] [manifold H M] [smooth_manifold_with_corners I M] /-- Basic smooth bundle core version of the tangent bundle of a smooth manifold `M` modelled over a model with corners `I` on `(E, H)`. The fibers are equal to `E`, and the coordinate change in the fiber corresponds to the derivative of the coordinate change in `M`. -/ def tangent_bundle_core : basic_smooth_bundle_core I M E := { coord_change := Ξ»i j x v, (fderiv_within π•œ (I ∘ j.1 ∘ i.1.symm ∘ I.symm) (range I) (I x) : E β†’ E) v, coord_change_smooth := Ξ»i j, begin /- To check that the coordinate change of the bundle is smooth, one should just use the smoothness of the charts, and thus the smoothness of their derivatives. -/ rw model_with_corners.image, have A : times_cont_diff_on π•œ ⊀ (I ∘ (i.1.symm.trans j.1) ∘ I.symm) (I.symm ⁻¹' (i.1.symm.trans j.1).source ∩ range I) := (has_groupoid.compatible (times_cont_diff_groupoid ⊀ I) i.2 j.2).1, have B : unique_diff_on π•œ (I.symm ⁻¹' (i.1.symm.trans j.1).source ∩ range I) := I.unique_diff_preimage_source, have C : times_cont_diff_on π•œ ⊀ (Ξ» (p : E Γ— E), (fderiv_within π•œ (I ∘ j.1 ∘ i.1.symm ∘ I.symm) (I.symm ⁻¹' (i.1.symm.trans j.1).source ∩ range I) p.1 : E β†’ E) p.2) ((I.symm ⁻¹' (i.1.symm.trans j.1).source ∩ range I).prod univ) := times_cont_diff_on_fderiv_within_apply A B le_top, have D : βˆ€ x ∈ (I.symm ⁻¹' (i.1.symm.trans j.1).source ∩ range I), fderiv_within π•œ (I ∘ j.1 ∘ i.1.symm ∘ I.symm) (range I) x = fderiv_within π•œ (I ∘ j.1 ∘ i.1.symm ∘ I.symm) (I.symm ⁻¹' (i.1.symm.trans j.1).source ∩ range I) x, { assume x hx, have N : I.symm ⁻¹' (i.1.symm.trans j.1).source ∈ nhds x := I.continuous_symm.continuous_at.preimage_mem_nhds (mem_nhds_sets (local_homeomorph.open_source _) hx.1), symmetry, rw inter_comm, exact fderiv_within_inter N (I.unique_diff _ hx.2) }, apply times_cont_diff_on.congr C, rintros ⟨x, v⟩ hx, have E : x ∈ I.symm ⁻¹' (i.1.symm.trans j.1).source ∩ range I, by simpa using hx, have : I (I.symm x) = x, by simp [E.2], dsimp, rw [this, D x E], refl end, coord_change_self := Ξ»i x hx v, begin /- Locally, a self-change of coordinate is just the identity, thus its derivative is the identity. One just needs to write this carefully, paying attention to the sets where the functions are defined. -/ have A : I.symm ⁻¹' (i.1.symm.trans i.1).source ∩ range I ∈ nhds_within (I x) (range I), { rw inter_comm, apply inter_mem_nhds_within, apply I.continuous_symm.continuous_at.preimage_mem_nhds (mem_nhds_sets (local_homeomorph.open_source _) _), simp [hx, local_equiv.trans_source, i.1.map_target] }, have B : βˆ€αΆ  y in nhds_within (I x) (range I), (I ∘ i.1 ∘ i.1.symm ∘ I.symm) y = (id : E β†’ E) y, { apply filter.mem_sets_of_superset A, assume y hy, rw ← model_with_corners.image at hy, rcases hy with ⟨z, hz⟩, simp [local_equiv.trans_source] at hz, simp [hz.2.symm, hz.1] }, have C : fderiv_within π•œ (I ∘ i.1 ∘ i.1.symm ∘ I.symm) (range I) (I x) = fderiv_within π•œ (id : E β†’ E) (range I) (I x) := fderiv_within_congr_of_mem_nhds_within I.unique_diff_at_image B (by simp [hx]), rw fderiv_within_id I.unique_diff_at_image at C, rw C, refl end, coord_change_comp := Ξ»i j u x hx, begin -- FIXME this causes a deterministic timeout with `-T50000` /- The cocycle property is just the fact that the derivative of a composition is the product of the derivatives. One needs however to check that all the functions one considers are smooth, and to pay attention to the domains where these functions are defined, making this proof a little bit cumbersome although there is nothing complicated here. -/ have M : I x ∈ (I.symm ⁻¹' ((i.1.symm.trans j.1).trans (j.1.symm.trans u.1)).source ∩ range I) := ⟨by simpa using hx, mem_range_self _⟩, have U : unique_diff_within_at π•œ (I.symm ⁻¹' ((i.1.symm.trans j.1).trans (j.1.symm.trans u.1)).source ∩ range I) (I x) := I.unique_diff_preimage_source _ M, have A : fderiv_within π•œ ((I ∘ u.1 ∘ j.1.symm ∘ I.symm) ∘ (I ∘ j.1 ∘ i.1.symm ∘ I.symm)) (I.symm ⁻¹' ((i.1.symm.trans j.1).trans (j.1.symm.trans u.1)).source ∩ range I) (I x) = (fderiv_within π•œ (I ∘ u.1 ∘ j.1.symm ∘ I.symm) (I.symm ⁻¹' (j.1.symm.trans u.1).source ∩ range I) ((I ∘ j.1 ∘ i.1.symm ∘ I.symm) (I x))).comp (fderiv_within π•œ (I ∘ j.1 ∘ i.1.symm ∘ I.symm) (I.symm ⁻¹' ((i.1.symm.trans j.1).trans (j.1.symm.trans u.1)).source ∩ range I) (I x)), { apply fderiv_within.comp _ _ _ _ U, show differentiable_within_at π•œ (I ∘ j.1 ∘ i.1.symm ∘ I.symm) (I.symm ⁻¹' ((i.1.symm.trans j.1).trans (j.1.symm.trans u.1)).source ∩ range I) (I x), { have A : times_cont_diff_on π•œ ⊀ (I ∘ (i.1.symm.trans j.1) ∘ I.symm) (I.symm ⁻¹' (i.1.symm.trans j.1).source ∩ range I) := (has_groupoid.compatible (times_cont_diff_groupoid ⊀ I) i.2 j.2).1, have B : differentiable_on π•œ (I ∘ j.1 ∘ i.1.symm ∘ I.symm) (I.symm ⁻¹' ((i.1.symm.trans j.1).trans (j.1.symm.trans u.1)).source ∩ range I), { apply (A.differentiable_on le_top).mono, have : ((i.1.symm.trans j.1).trans (j.1.symm.trans u.1)).source βŠ† (i.1.symm.trans j.1).source := inter_subset_left _ _, exact inter_subset_inter (preimage_mono this) (subset.refl (range I)) }, apply B, simpa [mem_inter_iff, -mem_image, -mem_range, mem_range_self] using hx }, show differentiable_within_at π•œ (I ∘ u.1 ∘ j.1.symm ∘ I.symm) (I.symm ⁻¹' (j.1.symm.trans u.1).source ∩ range I) ((I ∘ j.1 ∘ i.1.symm ∘ I.symm) (I x)), { have A : times_cont_diff_on π•œ ⊀ (I ∘ (j.1.symm.trans u.1) ∘ I.symm) (I.symm ⁻¹' (j.1.symm.trans u.1).source ∩ range I) := (has_groupoid.compatible (times_cont_diff_groupoid ⊀ I) j.2 u.2).1, apply A.differentiable_on le_top, rw [local_homeomorph.trans_source] at hx, simp [mem_inter_iff, -mem_image, -mem_range, mem_range_self], exact hx.2 }, show (I.symm ⁻¹' ((i.1.symm.trans j.1).trans (j.1.symm.trans u.1)).source ∩ range I) βŠ† (I ∘ j.1 ∘ i.1.symm ∘ I.symm) ⁻¹' (I.symm ⁻¹' (j.1.symm.trans u.1).source ∩ range I), { assume y hy, simp [-mem_range, local_equiv.trans_source] at hy, rw [local_homeomorph.left_inv] at hy, { simp [-mem_range, mem_range_self, hy, local_equiv.trans_source] }, { exact hy.1.1.2 } } }, have B : fderiv_within π•œ ((I ∘ u.1 ∘ j.1.symm ∘ I.symm) ∘ (I ∘ j.1 ∘ i.1.symm ∘ I.symm)) (I.symm ⁻¹' ((i.1.symm.trans j.1).trans (j.1.symm.trans u.1)).source ∩ range I) (I x) = fderiv_within π•œ (I ∘ u.1 ∘ i.1.symm ∘ I.symm) (I.symm ⁻¹' ((i.1.symm.trans j.1).trans (j.1.symm.trans u.1)).source ∩ range I) (I x), { have E : βˆ€ y ∈ (I.symm ⁻¹' ((i.1.symm.trans j.1).trans (j.1.symm.trans u.1)).source ∩ range I), ((I ∘ u.1 ∘ j.1.symm ∘ I.symm) ∘ (I ∘ j.1 ∘ i.1.symm ∘ I.symm)) y = (I ∘ u.1 ∘ i.1.symm ∘ I.symm) y, { assume y hy, simp only [function.comp_app, model_with_corners.left_inv], rw [j.1.left_inv], exact hy.1.1.2 }, exact fderiv_within_congr U E (E _ M) }, have C : fderiv_within π•œ (I ∘ u.1 ∘ i.1.symm ∘ I.symm) (I.symm ⁻¹' ((i.1.symm.trans j.1).trans (j.1.symm.trans u.1)).source ∩ range I) (I x) = fderiv_within π•œ (I ∘ u.1 ∘ i.1.symm ∘ I.symm) (range I) (I x), { rw inter_comm, apply fderiv_within_inter _ I.unique_diff_at_image, apply I.continuous_symm.continuous_at.preimage_mem_nhds (mem_nhds_sets (local_homeomorph.open_source _) _), simpa using hx }, have D : fderiv_within π•œ (I ∘ u.1 ∘ j.1.symm ∘ I.symm) (I.symm ⁻¹' (j.1.symm.trans u.1).source ∩ range I) ((I ∘ j.1 ∘ i.1.symm ∘ I.symm) (I x)) = fderiv_within π•œ (I ∘ u.1 ∘ j.1.symm ∘ I.symm) (range I) ((I ∘ j.1 ∘ i.1.symm ∘ I.symm) (I x)), { rw inter_comm, apply fderiv_within_inter _ I.unique_diff_at_image, apply I.continuous_symm.continuous_at.preimage_mem_nhds (mem_nhds_sets (local_homeomorph.open_source _) _), rw [local_homeomorph.trans_source] at hx, simp [mem_inter_iff, -mem_image, -mem_range, mem_range_self], exact hx.2 }, have E : fderiv_within π•œ (I ∘ j.1 ∘ i.1.symm ∘ I.symm) (I.symm ⁻¹' ((i.1.symm.trans j.1).trans (j.1.symm.trans u.1)).source ∩ range I) (I x) = (fderiv_within π•œ (I ∘ j.1 ∘ i.1.symm ∘ I.symm) (range I) (I x)), { rw inter_comm, apply fderiv_within_inter _ I.unique_diff_at_image, apply I.continuous_symm.continuous_at.preimage_mem_nhds (mem_nhds_sets (local_homeomorph.open_source _) _), simpa using hx }, rw [B, C, D, E] at A, simp [A] end } /-- The tangent bundle to a smooth manifold, as a plain type. -/ @[nolint has_inhabited_instance] -- is empty if the base manifold is empty def tangent_bundle := (tangent_bundle_core I M).to_topological_fiber_bundle_core.total_space /-- The projection from the tangent bundle of a smooth manifold to the manifold. As the tangent bundle is represented internally as a product type, the notation `p.1` also works for the projection of the point `p`. -/ def tangent_bundle.proj : tangent_bundle I M β†’ M := (tangent_bundle_core I M).to_topological_fiber_bundle_core.proj variable {M} /-- The tangent space at a point of the manifold `M`. It is just `E`. -/ def tangent_space (x : M) : Type* := (tangent_bundle_core I M).to_topological_fiber_bundle_core.fiber x section tangent_bundle_instances /- In general, the definition of tangent_bundle and tangent_space are not reducible, so that type class inference does not pick wrong instances. In this section, we record the right instances for them, noting in particular that the tangent bundle is a smooth manifold. -/ variable (M) local attribute [reducible] tangent_bundle instance : topological_space (tangent_bundle I M) := by apply_instance instance : manifold (H Γ— E) (tangent_bundle I M) := by apply_instance instance : smooth_manifold_with_corners I.tangent (tangent_bundle I M) := by apply_instance local attribute [reducible] tangent_space topological_fiber_bundle_core.fiber /- When `topological_fiber_bundle_core.fiber` is reducible, then `topological_fiber_bundle_core.topological_space_fiber` can be applied to prove that any space is a topological space, with several unknown metavariables. This is a bad instance, that we disable.-/ local attribute [instance, priority 0] topological_fiber_bundle_core.topological_space_fiber variables {M} (x : M) instance : topological_module π•œ (tangent_space I x) := by apply_instance instance : topological_space (tangent_space I x) := by apply_instance instance : add_comm_group (tangent_space I x) := by apply_instance instance : topological_add_group (tangent_space I x) := by apply_instance instance : vector_space π•œ (tangent_space I x) := by apply_instance instance : inhabited (tangent_space I x) := ⟨0⟩ end tangent_bundle_instances variable (M) /-- The tangent bundle projection on the basis is a continuous map. -/ lemma tangent_bundle_proj_continuous : continuous (tangent_bundle.proj I M) := topological_fiber_bundle_core.continuous_proj _ /-- The tangent bundle projection on the basis is an open map. -/ lemma tangent_bundle_proj_open : is_open_map (tangent_bundle.proj I M) := topological_fiber_bundle_core.is_open_map_proj _ /-- In the tangent bundle to the model space, the charts are just the identity-/ @[simp] lemma tangent_bundle_model_space_chart_at (p : tangent_bundle I H) : (chart_at (H Γ— E) p).to_local_equiv = local_equiv.refl (H Γ— E) := begin have A : βˆ€ x_fst, fderiv_within π•œ (I ∘ I.symm) (range I) (I x_fst) = continuous_linear_map.id π•œ E, { assume x_fst, have : fderiv_within π•œ (I ∘ I.symm) (range I) (I x_fst) = fderiv_within π•œ id (range I) (I x_fst), { refine fderiv_within_congr I.unique_diff_at_image (Ξ»y hy, _) (by simp), exact model_with_corners.right_inv _ hy }, rwa fderiv_within_id I.unique_diff_at_image at this }, ext x : 1, show (chart_at (H Γ— E) p : tangent_bundle I H β†’ H Γ— E) x = (local_equiv.refl (H Γ— E)) x, { cases x, simp [chart_at, manifold.chart_at, basic_smooth_bundle_core.chart, topological_fiber_bundle_core.local_triv, topological_fiber_bundle_core.local_triv', basic_smooth_bundle_core.to_topological_fiber_bundle_core, tangent_bundle_core, A] }, show βˆ€ x, ((chart_at (H Γ— E) p).to_local_equiv).symm x = (local_equiv.refl (H Γ— E)).symm x, { rintros ⟨x_fst, x_snd⟩, simp [chart_at, manifold.chart_at, basic_smooth_bundle_core.chart, topological_fiber_bundle_core.local_triv, topological_fiber_bundle_core.local_triv', basic_smooth_bundle_core.to_topological_fiber_bundle_core, tangent_bundle_core, A] }, show ((chart_at (H Γ— E) p).to_local_equiv).source = (local_equiv.refl (H Γ— E)).source, by simp [chart_at, manifold.chart_at, basic_smooth_bundle_core.chart, topological_fiber_bundle_core.local_triv, topological_fiber_bundle_core.local_triv', basic_smooth_bundle_core.to_topological_fiber_bundle_core, tangent_bundle_core, local_equiv.trans_source] end @[simp] lemma tangent_bundle_model_space_coe_chart_at (p : tangent_bundle I H) : (chart_at (H Γ— E) p : tangent_bundle I H β†’ H Γ— E) = id := by { unfold_coes, simp } @[simp] lemma tangent_bundle_model_space_coe_chart_at_symm (p : tangent_bundle I H) : ((chart_at (H Γ— E) p).symm : H Γ— E β†’ tangent_bundle I H) = id := by { unfold_coes, simp, refl } variable (H) /-- In the tangent bundle to the model space, the topology is the product topology, i.e., the bundle is trivial -/ lemma tangent_bundle_model_space_topology_eq_prod : tangent_bundle.topological_space I H = prod.topological_space := begin ext o, let x : tangent_bundle I H := (I.symm (0 : E), (0 : E)), let e := chart_at (H Γ— E) x, have e_source : e.source = univ, by { simp [e], refl }, have e_target : e.target = univ, by { simp [e], refl }, let e' := e.to_homeomorph_of_source_eq_univ_target_eq_univ e_source e_target, split, { assume ho, simpa [e'] using e'.symm.continuous o ho }, { assume ho, simpa [e'] using e'.continuous o ho } end end tangent_bundle
df6f79137b5cede29dcf11f4a55c1656b1fe5f7e
75c54c8946bb4203e0aaf196f918424a17b0de99
/old/language_term_n_de_bruijn.lean
084d72d7040a5a44359cc0573af4a0985d168bcf
[ "Apache-2.0" ]
permissive
urkud/flypitch
261e2a45f1038130178575406df8aea78255ba77
2250f5eda14b6ef9fc3e4e1f4a9ac4005634de5c
refs/heads/master
1,653,266,469,246
1,577,819,679,000
1,577,819,679,000
259,862,235
1
0
Apache-2.0
1,588,147,244,000
1,588,147,244,000
null
UTF-8
Lean
false
false
11,991
lean
/- 9/20/18 Preliminary definitions and some functions for working with languages and first-order logic. Much of the structure and methods are taken from Russell O'Connor's proof of Goedel's incompleteness theorem, at http://r6.ca/Goedel/goedel1.html Andrew Tindall-/ /- 2018-09-21T13:12:55 Added some proof rules more closely reflecting rules of deduction in Prop We should double-check that the additional constructors true, false, and and or have been taken care of appropriately in the implementation of de Bruijn indices. ---Jesse -/ structure Language := language :: (relations : Ξ  n : nat, Type) (functions : Ξ  n : nat, Type) (consts : Type) variable L : Language def term_param := unit βŠ• nat def atm :term_param := sum.inl () def vec : nat β†’ term_param := Ξ» n, sum.inr n inductive term : Ξ  n, Type | var : β„• β†’ term atm | const: L.consts β†’ term atm | apply : βˆ€ (n : nat) (f : L.functions n), term (vec n) β†’ term atm | nil : term (vec 0) | conj : βˆ€ n, term atm β†’ term (vec n) β†’ term (vec (n + 1)) open term local notation `⟦` l:(foldr `, ` (h t, ((prod.fst t) + 1, conj t.fst h (prod.snd t))) (0, nil L) `⟧`) := prod.snd l def term_sizeof :Ξ  n, term L n β†’ β„• | atm (var L n) := 0 | atm (const c) := 0 | atm (apply n f ts) := n + 2 + term_sizeof (vec n) ts | (sum.inr 0) (nil L) := 0 | (sum.inr (n+1)) (conj m t ts) := 1 + term_sizeof atm t + term_sizeof (vec m) ts instance term_has_sizeof { n: term_param} : has_sizeof (term L n) := ⟨ term_sizeof L n ⟩ inductive formula : Type | true : formula | false : formula | equal : term L atm β†’ term L atm β†’ formula | atomic : βˆ€ (n : nat) (r : L.relations n), term L (vec n) β†’ formula | and : formula β†’ formula β†’ formula | or : formula β†’ formula β†’ formula | imp : formula β†’ formula β†’ formula | not : formula β†’ formula | all : formula β†’ formula open formula def andf := Ξ» (f : formula L) g, not (imp f (not g)) reserve infix `⟾`:10 infix ⟾ := imp reserve infix `⇔`:15 local infix ⇔ := Ξ» (f : formula L) g, andf L (f ⟾ g) (g ⟾ f) reserve infix `≍`:20 infix ≍ := equal def free_vars_term : Ξ  n, β„• β†’ (term L n) β†’ list β„• | atm m (var L k) :=if k > m then [k - m] else [] | atm m (const c) := [] | atm m (apply n f ts) := free_vars_term (vec n) m ts | (sum.inr 0) _ _ := [] | (sum.inr (n+1)) m (conj _ t ts) := list.union (free_vars_term atm m t) (free_vars_term (vec n) m ts) def free_vars_formula : β„• β†’ formula L β†’ list β„• | m (equal t1 t2) := list.union (free_vars_term L atm m t1) (free_vars_term L atm m t2) | m (atomic n r ts) := free_vars_term L (vec n) m ts | m (and t1 t2) := list.union(free_vars_formula m t1) (free_vars_formula m t2) | m (or t1 t2) := list.union(free_vars_formula m t1) (free_vars_formula m t2) | m (imp f1 f2) := list.union (free_vars_formula m f1) (free_vars_formula m f2) | m (not f) := free_vars_formula m f | m (all f) := free_vars_formula (m+1) f | m (false L) := [] | m (true L) := [] def raise_depth_term : βˆ€ n, term L n β†’ β„• β†’ term L n | atm (var L n) m := var L (n+m) | atm (const c) _ := const c | atm (apply n f ts) m := apply n f (raise_depth_term (vec n) ts m) | (sum.inr 0) _ _ := nil L | (sum.inr (n+1)) (conj _ t ts) m := conj n (raise_depth_term atm t m) (raise_depth_term (vec n) ts m) def raise_depth_formula : formula L β†’ β„• β†’ formula L | (equal t1 t2) m := equal (raise_depth_term L atm t1 m) (raise_depth_term L atm t2 m) | (atomic n r ts) m := atomic n r (raise_depth_term L (vec n) ts m) | (and t1 t2) m := and (raise_depth_formula t1 m) (raise_depth_formula t2 m) | (or t1 t2) m := or (raise_depth_formula t1 m) (raise_depth_formula t2 m) | (imp f1 f2) m := imp (raise_depth_formula f1 m) (raise_depth_formula f2 m) | (not f) m := not (raise_depth_formula f m) | (all f) m := all (raise_depth_formula f m) | (false L) m := (false L) | (true L) m := (true L) def lower_depth_term : βˆ€ n, term L n β†’ β„• β†’ term L n | atm (var L n) m := var L (n-m) | atm (const c) _ := const c | atm (apply n f ts) m := apply n f (lower_depth_term (vec n) ts m) | (sum.inr 0) _ _ := nil L | (sum.inr (n+1)) (conj _ t ts) m := conj n (lower_depth_term atm t m) (lower_depth_term (vec n) ts m) def lower_depth_formula : formula L β†’ β„• β†’ formula L | (equal t1 t2) m := equal (lower_depth_term L atm t1 m) (lower_depth_term L atm t2 m) | (atomic n r ts) m := atomic n r (lower_depth_term L (vec n) ts m) | (and t1 t2) m := and (lower_depth_formula t1 m) (lower_depth_formula t2 m) | (or t1 t2) m := or (lower_depth_formula t1 m) (lower_depth_formula t2 m) | (imp f1 f2) m := imp (lower_depth_formula f1 m) (lower_depth_formula f2 m) | (not f) m := not (lower_depth_formula f m) | (all f) m := all (lower_depth_formula f m) | (false L) m := (false L) | (true L) m := (true L) lemma lt_add : βˆ€ (n m k : β„•), n < m β†’ n < k + m:= begin {intros n m k n_lt_m, induction k, rw zero_add,exact n_lt_m, rw nat.succ_add, apply nat.lt_succ_of_lt,exact k_ih} end lemma add_lt : βˆ€ (n m k : β„•), n < m β†’ n < m + k := begin {intros n m k n_lt_m, induction k, rw add_zero,exact n_lt_m, rw nat.add_comm, rw nat.succ_add, apply nat.lt_succ_of_lt, rw nat.add_comm, exact k_ih} end def substitute_term : Ξ  (n : term_param), term L n β†’ nat Γ— term L atm β†’ nat β†’ term L n | atm (var L n) (x, t') m := if (x+m) = n then raise_depth_term L atm t' m else var L n | atm (const c) _ _ := const c | atm (apply n f ts) (x,t') m := have term_sizeof L (sum.inr n) ts < term_sizeof L atm (apply n f ts), from begin simp [term_sizeof], apply lt_add, apply nat.lt_add_of_pos_right, apply nat.zero_lt_succ end, apply n f (substitute_term (sum.inr n) ts (x,t') m) | (sum.inr 0) _ _ _ := nil L | (sum.inr (n+1)) (conj k t ts) (x,t') m := have term_sizeof L atm t < term_sizeof L (sum.inr (k + 1)) (conj k t ts), from begin simp [term_sizeof, nat.succ_add], apply nat.lt_of_succ_le, apply nat.succ_le_succ, apply nat.le_add_right end, have term_sizeof L (vec k) ts < term_sizeof L (sum.inr (k + 1)) (conj k t ts), from begin simp [term_sizeof, nat.succ_add], apply nat.lt_of_succ_le, apply nat.succ_le_succ, apply nat.le_add_left end, conj n (substitute_term atm t (x,t') m) (substitute_term (vec n) ts (x,t') m) using_well_founded {rel_tac := Ξ» _ _, `[exact ⟨_, measure_wf psigma.sizeof⟩]} /- I think we don't need these but I didn't want to delete them yet def var_in_term : βˆ€ n, β„• β†’ β„• β†’ term L n β†’ Prop | atm x m (var L y) := (x+m) = y | atm _ _ (const c) := false | atm x m (apply n f ts) := var_in_term (vec n) x m ts | (sum.inr 0) _ _ _ := false | (sum.inr (n+1)) x m (conj _ t ts) := var_in_term atm x m t ∨ var_in_term (vec n) x m ts def var_in_formula : β„• β†’ β„• β†’ formula L β†’ Prop | x m (equal t t') := var_in_term L atm x m t ∨ var_in_term L atm x m t' | x m (atomic n r ts) := var_in_term L (vec n) x m ts | x m (imp f f') := var_in_formula x m f ∨ var_in_formula x m f' | x m (not f) := var_in_formula x m f | x m (all f) := var_in_formula x (m+1) f -/ def substitute_formula : formula L β†’ (nat Γ— term L atm) β†’ β„• β†’ formula L | (equal t t') pair m := equal (substitute_term L atm t pair m) (substitute_term L atm t' pair m) | (atomic n r t) pair m := atomic n r (substitute_term L (vec n) t pair m) | (and t1 t2) pair m := and (substitute_formula t1 pair m) (substitute_formula t2 pair m) | (or t1 t2) pair m := or (substitute_formula t1 pair m) (substitute_formula t2 pair m) | (imp f f') pair m := imp (substitute_formula f pair m) (substitute_formula f' pair m) | (not f) pair m := not (substitute_formula f pair m) | (all f) pair m := substitute_formula f pair (m+1) | (false L) pair m := (false L) | (true L) pair m := (true L) /-these are helper functions for constructing the family of extensionality axioms. even_vec n creates the term ⟦ var 0, var 2, ... var 2n ⟧ and odd_vec n creates ⟦ var 1, ... var 2n+1 ⟧ -/ def vec_length_n_diff_two : βˆ€ n, β„• β†’ term L (vec n) | 0 hd := nil L | (n+1) hd := conj n (var L hd) (vec_length_n_diff_two n (hd+2)) def even_vec := Ξ» n, vec_length_n_diff_two L n 0 def odd_vec := Ξ» n, vec_length_n_diff_two L n 1 def axm_eq' : βˆ€ m, formula L β†’ formula L | 0 f := (((var L 0) ≍ (var L 1)) ⟾ f) | (m+1) f := axm_eq' m (((var L (2 * (m + 1))) ≍ (var L (2 * (m + 1) + 1))) ⟾ f) def axm_eq_4 : βˆ€ n, L.functions n β†’ formula L | 0 f := ((apply 0 f (nil L)) ≍ (apply 0 f (nil L))) | (n+1) f := axm_eq' L n ((apply (n+1) f (even_vec L (n+1)) ≍ (apply (n+1) f (odd_vec L (n+1))))) def axm_eq_5 : βˆ€ n, L.relations n β†’ formula L | 0 r := ((atomic 0 r (nil L)) ⇔ (atomic 0 r (nil L))) | (n+1) r := axm_eq' L n ((atomic (n+1) r (even_vec L (n+1))) ⇔ (atomic (n+1) r (odd_vec L (n+1)))) -- L.true must be interpreted as true --- need to have reified true for soundness inductive prf : list (formula L) β†’ formula L β†’ Type | true_intro : βˆ€ as, prf as (true L) -- true is vacuously provable | false_elim : βˆ€ a, prf [false L] a -- principle of explosion | not_elim : βˆ€ a, prf [not a] (a ⟾ (false L)) | not_intro :βˆ€ a, prf [a ⟾ false L] (not a) | and_eliml : βˆ€ a b, prf [formula.and a b] a -- if we know A ∧ B, then we know A | and_elimr : βˆ€ a b, prf [formula.and a b] b -- if we know A ∧ B, then we know B | and_intro : βˆ€ as, prf as (as.foldr (formula.and) (true L)) -- if A1...An are known, then true ∧ A1 ∧ ... ∧ An is provable (and true ∧ B β†’ B) by modus ponens and and_elim | or_intro : βˆ€ a b, prf [a] (formula.or a b) -- if we know A is true, then for any B, A ∨ B is true. | axm : βˆ€ a, prf [a] (true L ⟾ a) -- equivalent to O'Connor's axm by MP | mp : βˆ€ axm axm' f g, prf axm (imp f g) β†’ prf axm' f β†’ prf (axm ++ axm') g -- | gen : βˆ€ axm f, prf axm f β†’ prf axm (all (raise_depth_formula L f 1)) --- this follows from all2 and using and_intro to move axm to the right | imp1 : βˆ€ f g, prf [] (f ⟾ (g ⟾ f)) | imp2 : βˆ€ f g h, prf [] (f ⟾ (g ⟾ h) ⟾ ((f ⟾ g) ⟾ (f ⟾ h))) -- | cp : βˆ€ f g, prf [] (((not f) ⟾ (not g)) ⟾ (g ⟾ f)) --- until we implement tautology reflection, we should just have double-negation elimination outright | dne : βˆ€ f, prf [] ((not (not f)) ⟾ f) | forall_elim : βˆ€ f t, prf [] ((all f) ⟾ (raise_depth_formula L (substitute_formula L f (0,t) 0)) 1) | all2 : βˆ€ f g, prf [] ((all (f ⟾ g) ⟾ ((all f) ⟾ (all g)))) -- TODO: this seems fishy --- why aren't we able to only quantify over _some_ of the free variables, if we're allowing open variables in the proof system in the first place? | eq1 : prf [] ((var L 0) ≍ (var L 0)) | eq2 : prf [] (( (var L 0) ≍ (var L 1)) ⟾ ((var L 1) ≍ (var L 0))) | eq3 : prf [] (((var L 0) ≍ (var L 1)) ⟾ (((var L 1) ≍ (var L 2)) ⟾ ((var L 0) ≍ (var L 2)))) /- the following two axioms are general equality axioms that are generated for n-ary relations and functions; x0 = x1 β‡’ … β‡’ x2n-2 = x2n-1 β‡’ (R(x0, x2, …, x2n-2) ⇔ R(x1, x3, …, x2n-1)) and x0 = x1 β‡’ … β‡’ x2n-2 = x2n-1 β‡’ f(x0, x2, …, x2n-2) = f(x1, x3, …, x2n-1) src http://r6.ca/Goedel/goedel1.html -/ | eq4 : βˆ€ n, βˆ€ r : L.functions n, prf [] (axm_eq_4 L n r) | eq5 : βˆ€ n, βˆ€ f : L.relations n, prf [] (axm_eq_5 L n f)
39f8fdaf38e3c0e3041fb1b9c93cd31cc17d5cd1
ce6917c5bacabee346655160b74a307b4a5ab620
/src/ch3/ex0102.lean
ec94ddf6877a467fd4e9e0f16b841a7a0cae4587
[]
no_license
Ailrun/Theorem_Proving_in_Lean
ae6a23f3c54d62d401314d6a771e8ff8b4132db2
2eb1b5caf93c6a5a555c79e9097cf2ba5a66cf68
refs/heads/master
1,609,838,270,467
1,586,846,743,000
1,586,846,743,000
240,967,761
1
0
null
null
null
null
UTF-8
Lean
false
false
157
lean
-- constant Proof : Prop β†’ Type -- constant and_comm : Ξ  p q : Prop, -- Proof (implies (and p q) (and q p)) variables p q : Prop #check and_comm p q
d1fc872211665a3612dfd15adc17e84027e12c8d
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/testing/slim_check/gen_auto.lean
1e511096ebdb4fce552d8642f443d5d311cb88cb
[]
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,050
lean
/- Copyright (c) 2020 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author(s): Simon Hudon -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.control.uliftable import Mathlib.Lean3Lib.system.random import Mathlib.system.random.basic import Mathlib.PostPort universes u u_1 v namespace Mathlib /-! # `gen` Monad This monad is used to formulate randomized computations with a parameter to specify the desired size of the result. This is a port of the Haskell QuickCheck library. ## Main definitions * `gen` monad ## Local notation * `i .. j` : `Icc i j`, the set of values between `i` and `j` inclusively; ## Tags random testing ## References * https://hackage.haskell.org/package/QuickCheck -/ namespace slim_check /-- Monad to generate random examples to test properties with. It has a `nat` parameter so that the caller can decide on the size of the examples. -/ def gen (Ξ± : Type u) := reader_t (ulift β„•) rand Ξ± /-- Execute a `gen` inside the `io` monad using `i` as the example size and with a fresh random number generator. -/ def io.run_gen {Ξ± : Type} (x : gen Ξ±) (i : β„•) : io Ξ± := io.run_rand (reader_t.run x (ulift.up i)) namespace gen /-- Lift `random.random` to the `gen` monad. -/ def choose_any (Ξ± : Type u) [random Ξ±] : gen Ξ± := reader_t.mk fun (_x : ulift β„•) => rand.random Ξ± /-- Lift `random.random_r` to the `gen` monad. -/ def choose {Ξ± : Type u} [preorder Ξ±] [bounded_random Ξ±] (x : Ξ±) (y : Ξ±) (p : x ≀ y) : gen β†₯(set.Icc x y) := reader_t.mk fun (_x : ulift β„•) => rand.random_r x y p /-- Generate a `nat` example between `x` and `y`. -/ def choose_nat (x : β„•) (y : β„•) (p : x ≀ y) : gen β†₯(set.Icc x y) := choose x y p /-- Generate a `nat` example between `x` and `y`. -/ def choose_nat' (x : β„•) (y : β„•) (p : x < y) : gen β†₯(set.Ico x y) := (fun (this : βˆ€ (i : β„•), x < i β†’ i ≀ y β†’ Nat.pred i < y) => subtype.map Nat.pred sorry <$> choose (x + 1) y p) sorry protected instance uliftable : uliftable gen gen := reader_t.uliftable' (equiv.trans equiv.ulift (equiv.symm equiv.ulift)) protected instance has_orelse : has_orelse gen := has_orelse.mk fun (Ξ± : Type u) (x y : gen Ξ±) => do let b ← uliftable.up (choose_any Bool) ite (β†₯(ulift.down b)) x y /-- Get access to the size parameter of the `gen` monad. For reasons of universe polymorphism, it is specified in continuation passing style. -/ def sized {Ξ± : Type u} (cmd : β„• β†’ gen Ξ±) : gen Ξ± := reader_t.mk fun (_x : ulift β„•) => sorry /-- Apply a function to the size parameter. -/ def resize {Ξ± : Type u} (f : β„• β†’ β„•) (cmd : gen Ξ±) : gen Ξ± := reader_t.mk fun (_x : ulift β„•) => sorry /-- Create `n` examples using `cmd`. -/ def vector_of {Ξ± : Type u} (n : β„•) (cmd : gen Ξ±) : gen (vector Ξ± n) := sorry /-- Create a list of examples using `cmd`. The size is controlled by the size parameter of `gen`. -/ def list_of {Ξ± : Type u} (cmd : gen Ξ±) : gen (List Ξ±) := sized fun (sz : β„•) => do uliftable.up (choose_nat 0 (sz + 1) sorry) sorry /-- Given a list of example generators, choose one to create an example. -/ def one_of {Ξ± : Type u} (xs : List (gen Ξ±)) (pos : 0 < list.length xs) : gen Ξ± := do uliftable.up (choose_nat' 0 (list.length xs) pos) sorry /-- Given a list of example generators, choose one to create an example. -/ def elements {Ξ± : Type u} (xs : List Ξ±) (pos : 0 < list.length xs) : gen Ξ± := do uliftable.up (choose_nat' 0 (list.length xs) pos) sorry /-- `freq_aux xs i _` takes a weighted list of generator and a number meant to select one of the generators. If we consider `freq_aux [(1, gena), (3, genb), (5, genc)] 4 _`, we choose a generator by splitting the interval 1-9 into 1-1, 2-4, 5-9 so that the width of each interval corresponds to one of the number in the list of generators. Then, we check which interval 4 falls into: it selects `genb`. -/ def freq_aux {Ξ± : Type u} (xs : List (β„•+ Γ— gen Ξ±)) (i : β„•) : i < list.sum (list.map (subtype.val ∘ prod.fst) xs) β†’ gen Ξ± := sorry /-- `freq [(1, gena), (3, genb), (5, genc)] _` will choose one of `gena`, `genb`, `genc` with probabiities proportional to the number accompanying them. In this example, the sum of those numbers is 9, `gena` will be chosen with probability ~1/9, `genb` with ~3/9 (i.e. 1/3) and `genc` with probability 5/9. -/ def freq {Ξ± : Type u} (xs : List (β„•+ Γ— gen Ξ±)) (pos : 0 < list.length xs) : gen Ξ± := let s : β„• := list.sum (list.map (subtype.val ∘ prod.fst) xs); (fun (ha : 1 ≀ s) => (fun (this : 0 ≀ s - 1) => uliftable.adapt_up gen gen (choose_nat 0 (s - 1) this) fun (i : β†₯(set.Icc 0 (s - 1))) => freq_aux xs (subtype.val i) sorry) sorry) sorry /-- Generate a random permutation of a given list. -/ def permutation_of {Ξ± : Type u} (xs : List Ξ±) : gen (Subtype (list.perm xs)) := sorry end Mathlib
c337964be1065755a97dd096f99bf873e60eff9b
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/category_theory/limits/types_auto.lean
d1f7daf36f75965b5281ca1a0d2c5291d1a512b4
[]
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
15,630
lean
/- Copyright (c) 2018 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Reid Barton -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.category_theory.limits.shapes.images import Mathlib.category_theory.filtered import Mathlib.tactic.equiv_rw import Mathlib.PostPort universes u namespace Mathlib namespace category_theory.limits.types /-- (internal implementation) the limit cone of a functor, implemented as flat sections of a pi type -/ def limit_cone {J : Type u} [small_category J] (F : J β₯€ Type u) : cone F := cone.mk (β†₯(functor.sections F)) (nat_trans.mk fun (j : J) (u : functor.obj (functor.obj (functor.const J) β†₯(functor.sections F)) j) => subtype.val u j) /-- (internal implementation) the fact that the proposed limit cone is the limit -/ def limit_cone_is_limit {J : Type u} [small_category J] (F : J β₯€ Type u) : is_limit (limit_cone F) := is_limit.mk fun (s : cone F) (v : cone.X s) => { val := fun (j : J) => nat_trans.app (cone.Ο€ s) j v, property := sorry } /-- The category of types has all limits. See https://stacks.math.columbia.edu/tag/002U. -/ protected instance sort.category_theory.limits.has_limits : has_limits (Type u) := has_limits.mk fun (J : Type u) (π’₯ : small_category J) => has_limits_of_shape.mk fun (F : J β₯€ Type u) => has_limit.mk (limit_cone.mk (limit_cone F) (limit_cone_is_limit F)) /-- The equivalence between a limiting cone of `F` in `Type u` and the "concrete" definition as the sections of `F`. -/ def is_limit_equiv_sections {J : Type u} [small_category J] {F : J β₯€ Type u} {c : cone F} (t : is_limit c) : cone.X c ≃ β†₯(functor.sections F) := iso.to_equiv (is_limit.cone_point_unique_up_to_iso t (limit_cone_is_limit F)) @[simp] theorem is_limit_equiv_sections_apply {J : Type u} [small_category J] {F : J β₯€ Type u} {c : cone F} (t : is_limit c) (j : J) (x : cone.X c) : coe (coe_fn (is_limit_equiv_sections t) x) j = nat_trans.app (cone.Ο€ c) j x := rfl @[simp] theorem is_limit_equiv_sections_symm_apply {J : Type u} [small_category J] {F : J β₯€ Type u} {c : cone F} (t : is_limit c) (x : β†₯(functor.sections F)) (j : J) : nat_trans.app (cone.Ο€ c) j (coe_fn (equiv.symm (is_limit_equiv_sections t)) x) = coe x j := sorry /-- The equivalence between the abstract limit of `F` in `Type u` and the "concrete" definition as the sections of `F`. -/ def limit_equiv_sections {J : Type u} [small_category J] (F : J β₯€ Type u) : limit F ≃ β†₯(functor.sections F) := is_limit_equiv_sections (limit.is_limit F) @[simp] theorem limit_equiv_sections_apply {J : Type u} [small_category J] (F : J β₯€ Type u) (x : limit F) (j : J) : coe (coe_fn (limit_equiv_sections F) x) j = limit.Ο€ F j x := rfl @[simp] theorem limit_equiv_sections_symm_apply {J : Type u} [small_category J] (F : J β₯€ Type u) (x : β†₯(functor.sections F)) (j : J) : limit.Ο€ F j (coe_fn (equiv.symm (limit_equiv_sections F)) x) = coe x j := is_limit_equiv_sections_symm_apply (limit.is_limit F) x j /-- Construct a term of `limit F : Type u` from a family of terms `x : Ξ  j, F.obj j` which are "coherent": `βˆ€ (j j') (f : j ⟢ j'), F.map f (x j) = x j'`. -/ def limit.mk {J : Type u} [small_category J] (F : J β₯€ Type u) (x : (j : J) β†’ functor.obj F j) (h : βˆ€ (j j' : J) (f : j ⟢ j'), functor.map F f (x j) = x j') : limit F := coe_fn (equiv.symm (limit_equiv_sections F)) { val := x, property := h } @[simp] theorem limit.Ο€_mk {J : Type u} [small_category J] (F : J β₯€ Type u) (x : (j : J) β†’ functor.obj F j) (h : βˆ€ (j j' : J) (f : j ⟢ j'), functor.map F f (x j) = x j') (j : J) : limit.Ο€ F j (limit.mk F x h) = x j := sorry -- PROJECT: prove this for concrete categories where the forgetful functor preserves limits theorem limit_ext {J : Type u} [small_category J] (F : J β₯€ Type u) (x : limit F) (y : limit F) (w : βˆ€ (j : J), limit.Ο€ F j x = limit.Ο€ F j y) : x = y := sorry theorem limit_ext_iff {J : Type u} [small_category J] (F : J β₯€ Type u) (x : limit F) (y : limit F) : x = y ↔ βˆ€ (j : J), limit.Ο€ F j x = limit.Ο€ F j y := { mp := fun (t : x = y) (_x : J) => t β–Έ rfl, mpr := limit_ext F x y } -- TODO: are there other limits lemmas that should have `_apply` versions? -- Can we generate these like with `@[reassoc]`? -- PROJECT: prove these for any concrete category where the forgetful functor preserves limits? @[simp] theorem limit.w_apply {J : Type u} [small_category J] {F : J β₯€ Type u} {j : J} {j' : J} {x : limit F} (f : j ⟢ j') : functor.map F f (limit.Ο€ F j x) = limit.Ο€ F j' x := congr_fun (limit.w F f) x @[simp] theorem limit.lift_Ο€_apply {J : Type u} [small_category J] (F : J β₯€ Type u) (s : cone F) (j : J) (x : cone.X s) : limit.Ο€ F j (limit.lift F s x) = nat_trans.app (cone.Ο€ s) j x := congr_fun (limit.lift_Ο€ s j) x @[simp] theorem limit.map_Ο€_apply {J : Type u} [small_category J] {F : J β₯€ Type u} {G : J β₯€ Type u} (Ξ± : F ⟢ G) (j : J) (x : limit F) : limit.Ο€ G j (lim_map Ξ± x) = nat_trans.app Ξ± j (limit.Ο€ F j x) := congr_fun (lim_map_Ο€ Ξ± j) x /-- The relation defining the quotient type which implements the colimit of a functor `F : J β₯€ Type u`. See `category_theory.limits.types.quot`. -/ def quot.rel {J : Type u} [small_category J] (F : J β₯€ Type u) : (sigma fun (j : J) => functor.obj F j) β†’ (sigma fun (j : J) => functor.obj F j) β†’ Prop := fun (p p' : sigma fun (j : J) => functor.obj F j) => βˆƒ (f : sigma.fst p ⟢ sigma.fst p'), sigma.snd p' = functor.map F f (sigma.snd p) /-- A quotient type implementing the colimit of a functor `F : J β₯€ Type u`, as pairs `⟨j, x⟩` where `x : F.obj j`, modulo the equivalence relation generated by `⟨j, x⟩ ~ ⟨j', x'⟩` whenever there is a morphism `f : j ⟢ j'` so `F.map f x = x'`. -/ def quot {J : Type u} [small_category J] (F : J β₯€ Type u) := Quot sorry /-- (internal implementation) the colimit cocone of a functor, implemented as a quotient of a sigma type -/ def colimit_cocone {J : Type u} [small_category J] (F : J β₯€ Type u) : cocone F := cocone.mk (quot F) (nat_trans.mk fun (j : J) (x : functor.obj F j) => Quot.mk (quot.rel F) (sigma.mk j x)) /-- (internal implementation) the fact that the proposed colimit cocone is the colimit -/ def colimit_cocone_is_colimit {J : Type u} [small_category J] (F : J β₯€ Type u) : is_colimit (colimit_cocone F) := is_colimit.mk fun (s : cocone F) => Quot.lift (fun (p : sigma fun (j : J) => functor.obj F j) => nat_trans.app (cocone.ΞΉ s) (sigma.fst p) (sigma.snd p)) sorry /-- The category of types has all colimits. See https://stacks.math.columbia.edu/tag/002U. -/ protected instance sort.category_theory.limits.has_colimits : has_colimits (Type u) := has_colimits.mk fun (J : Type u) (π’₯ : small_category J) => has_colimits_of_shape.mk fun (F : J β₯€ Type u) => has_colimit.mk (colimit_cocone.mk (colimit_cocone F) (colimit_cocone_is_colimit F)) /-- The equivalence between the abstract colimit of `F` in `Type u` and the "concrete" definition as a quotient. -/ def colimit_equiv_quot {J : Type u} [small_category J] (F : J β₯€ Type u) : colimit F ≃ quot F := iso.to_equiv (is_colimit.cocone_point_unique_up_to_iso (colimit.is_colimit F) (colimit_cocone_is_colimit F)) @[simp] theorem colimit_equiv_quot_symm_apply {J : Type u} [small_category J] (F : J β₯€ Type u) (j : J) (x : functor.obj F j) : coe_fn (equiv.symm (colimit_equiv_quot F)) (Quot.mk (quot.rel F) (sigma.mk j x)) = colimit.ΞΉ F j x := rfl @[simp] theorem colimit_equiv_quot_apply {J : Type u} [small_category J] (F : J β₯€ Type u) (j : J) (x : functor.obj F j) : coe_fn (colimit_equiv_quot F) (colimit.ΞΉ F j x) = Quot.mk (quot.rel F) (sigma.mk j x) := sorry @[simp] theorem colimit.w_apply {J : Type u} [small_category J] {F : J β₯€ Type u} {j : J} {j' : J} {x : functor.obj F j} (f : j ⟢ j') : colimit.ΞΉ F j' (functor.map F f x) = colimit.ΞΉ F j x := congr_fun (colimit.w F f) x @[simp] theorem colimit.ΞΉ_desc_apply {J : Type u} [small_category J] (F : J β₯€ Type u) (s : cocone F) (j : J) (x : functor.obj F j) : colimit.desc F s (colimit.ΞΉ F j x) = nat_trans.app (cocone.ΞΉ s) j x := congr_fun (colimit.ΞΉ_desc s j) x @[simp] theorem colimit.ΞΉ_map_apply {J : Type u} [small_category J] {F : J β₯€ Type u} {G : J β₯€ Type u} (Ξ± : F ⟢ G) (j : J) (x : functor.obj F j) : functor.map colim Ξ± (colimit.ΞΉ F j x) = colimit.ΞΉ G j (nat_trans.app Ξ± j x) := congr_fun (colimit.ΞΉ_map Ξ± j) x theorem colimit_sound {J : Type u} [small_category J] {F : J β₯€ Type u} {j : J} {j' : J} {x : functor.obj F j} {x' : functor.obj F j'} (f : j ⟢ j') (w : functor.map F f x = x') : colimit.ΞΉ F j x = colimit.ΞΉ F j' x' := sorry theorem colimit_sound' {J : Type u} [small_category J] {F : J β₯€ Type u} {j : J} {j' : J} {x : functor.obj F j} {x' : functor.obj F j'} {j'' : J} (f : j ⟢ j'') (f' : j' ⟢ j'') (w : functor.map F f x = functor.map F f' x') : colimit.ΞΉ F j x = colimit.ΞΉ F j' x' := sorry theorem colimit_eq {J : Type u} [small_category J] {F : J β₯€ Type u} {j : J} {j' : J} {x : functor.obj F j} {x' : functor.obj F j'} (w : colimit.ΞΉ F j x = colimit.ΞΉ F j' x') : eqv_gen (quot.rel F) (sigma.mk j x) (sigma.mk j' x') := sorry theorem jointly_surjective {J : Type u} [small_category J] (F : J β₯€ Type u) {t : cocone F} (h : is_colimit t) (x : cocone.X t) : βˆƒ (j : J), βˆƒ (y : functor.obj F j), nat_trans.app (cocone.ΞΉ t) j y = x := sorry /-- A variant of `jointly_surjective` for `x : colimit F`. -/ theorem jointly_surjective' {J : Type u} [small_category J] {F : J β₯€ Type u} (x : colimit F) : βˆƒ (j : J), βˆƒ (y : functor.obj F j), colimit.ΞΉ F j y = x := jointly_surjective F (colimit.is_colimit F) x namespace filtered_colimit /- For filtered colimits of types, we can give an explicit description of the equivalence relation generated by the relation used to form the colimit. -/ /-- An alternative relation on `Ξ£ j, F.obj j`, which generates the same equivalence relation as we use to define the colimit in `Type` above, but that is more convenient when working with filtered colimits. Elements in `F.obj j` and `F.obj j'` are equivalent if there is some `k : J` to the right where their images are equal. -/ protected def r {J : Type u} [small_category J] (F : J β₯€ Type u) (x : sigma fun (j : J) => functor.obj F j) (y : sigma fun (j : J) => functor.obj F j) := βˆƒ (k : J), βˆƒ (f : sigma.fst x ⟢ k), βˆƒ (g : sigma.fst y ⟢ k), functor.map F f (sigma.snd x) = functor.map F g (sigma.snd y) protected theorem r_ge {J : Type u} [small_category J] (F : J β₯€ Type u) (x : sigma fun (j : J) => functor.obj F j) (y : sigma fun (j : J) => functor.obj F j) : (βˆƒ (f : sigma.fst x ⟢ sigma.fst y), sigma.snd y = functor.map F f (sigma.snd x)) β†’ filtered_colimit.r F x y := sorry /-- Recognizing filtered colimits of types. -/ def is_colimit_of {J : Type u} [small_category J] (F : J β₯€ Type u) (t : cocone F) (hsurj : βˆ€ (x : cocone.X t), βˆƒ (i : J), βˆƒ (xi : functor.obj F i), x = nat_trans.app (cocone.ΞΉ t) i xi) (hinj : βˆ€ (i j : J) (xi : functor.obj F i) (xj : functor.obj F j), nat_trans.app (cocone.ΞΉ t) i xi = nat_trans.app (cocone.ΞΉ t) j xj β†’ βˆƒ (k : J), βˆƒ (f : i ⟢ k), βˆƒ (g : j ⟢ k), functor.map F f xi = functor.map F g xj) : is_colimit t := is_colimit.of_iso_colimit (colimit.is_colimit F) (cocones.ext (equiv.to_iso (equiv.of_bijective (colimit.desc F t) sorry)) sorry) -- Strategy: Prove that the map from "the" colimit of F (defined above) to t.X -- is a bijection. protected theorem r_equiv {J : Type u} [small_category J] (F : J β₯€ Type u) [is_filtered_or_empty J] : equivalence (filtered_colimit.r F) := sorry protected theorem r_eq {J : Type u} [small_category J] (F : J β₯€ Type u) [is_filtered_or_empty J] : filtered_colimit.r F = eqv_gen fun (x y : sigma fun (j : J) => functor.obj F j) => βˆƒ (f : sigma.fst x ⟢ sigma.fst y), sigma.snd y = functor.map F f (sigma.snd x) := sorry theorem colimit_eq_iff_aux {J : Type u} [small_category J] (F : J β₯€ Type u) [is_filtered_or_empty J] {i : J} {j : J} {xi : functor.obj F i} {xj : functor.obj F j} : nat_trans.app (cocone.ΞΉ (colimit_cocone F)) i xi = nat_trans.app (cocone.ΞΉ (colimit_cocone F)) j xj ↔ βˆƒ (k : J), βˆƒ (f : i ⟢ k), βˆƒ (g : j ⟢ k), functor.map F f xi = functor.map F g xj := sorry theorem is_colimit_eq_iff {J : Type u} [small_category J] (F : J β₯€ Type u) {t : cocone F} [is_filtered_or_empty J] (ht : is_colimit t) {i : J} {j : J} {xi : functor.obj F i} {xj : functor.obj F j} : nat_trans.app (cocone.ΞΉ t) i xi = nat_trans.app (cocone.ΞΉ t) j xj ↔ βˆƒ (k : J), βˆƒ (f : i ⟢ k), βˆƒ (g : j ⟢ k), functor.map F f xi = functor.map F g xj := sorry theorem colimit_eq_iff {J : Type u} [small_category J] (F : J β₯€ Type u) [is_filtered_or_empty J] {i : J} {j : J} {xi : functor.obj F i} {xj : functor.obj F j} : colimit.ΞΉ F i xi = colimit.ΞΉ F j xj ↔ βˆƒ (k : J), βˆƒ (f : i ⟢ k), βˆƒ (g : j ⟢ k), functor.map F f xi = functor.map F g xj := is_colimit_eq_iff F (colimit.is_colimit F) end filtered_colimit /-- the image of a morphism in Type is just `set.range f` -/ def image {Ξ± : Type u} {Ξ² : Type u} (f : Ξ± ⟢ Ξ²) := β†₯(set.range f) protected instance image.inhabited {Ξ± : Type u} {Ξ² : Type u} (f : Ξ± ⟢ Ξ²) [Inhabited Ξ±] : Inhabited (image f) := { default := { val := f Inhabited.default, property := sorry } } /-- the inclusion of `image f` into the target -/ def image.ΞΉ {Ξ± : Type u} {Ξ² : Type u} (f : Ξ± ⟢ Ξ²) : image f ⟢ Ξ² := subtype.val protected instance image.ΞΉ.category_theory.mono {Ξ± : Type u} {Ξ² : Type u} (f : Ξ± ⟢ Ξ²) : mono (image.ΞΉ f) := iff.mpr (mono_iff_injective (image.ΞΉ f)) subtype.val_injective /-- the universal property for the image factorisation -/ def image.lift {Ξ± : Type u} {Ξ² : Type u} {f : Ξ± ⟢ Ξ²} (F' : mono_factorisation f) : image f ⟢ mono_factorisation.I F' := fun (x : image f) => mono_factorisation.e F' (subtype.val (classical.indefinite_description (fun (x_1 : Ξ±) => f x_1 = subtype.val x) sorry)) theorem image.lift_fac {Ξ± : Type u} {Ξ² : Type u} {f : Ξ± ⟢ Ξ²} (F' : mono_factorisation f) : image.lift F' ≫ mono_factorisation.m F' = image.ΞΉ f := sorry /-- the factorisation of any morphism in Type through a mono. -/ def mono_factorisation {Ξ± : Type u} {Ξ² : Type u} (f : Ξ± ⟢ Ξ²) : mono_factorisation f := mono_factorisation.mk (image f) (image.ΞΉ f) (set.range_factorization f) /-- the facorisation through a mono has the universal property of the image. -/ def is_image {Ξ± : Type u} {Ξ² : Type u} (f : Ξ± ⟢ Ξ²) : is_image (mono_factorisation f) := is_image.mk image.lift protected instance category_theory.limits.has_image {Ξ± : Type u} {Ξ² : Type u} (f : Ξ± ⟢ Ξ²) : has_image f := has_image.mk (image_factorisation.mk (mono_factorisation f) (is_image f)) protected instance sort.category_theory.limits.has_images : has_images (Type u) := has_images.mk sorry protected instance sort.category_theory.limits.has_image_maps : has_image_maps (Type u) := has_image_maps.mk sorry end Mathlib
14ee659b56101064d48844781ca138666f9292db
626e312b5c1cb2d88fca108f5933076012633192
/src/group_theory/group_action/defs.lean
dfdc4b805179cf12204b8716746d6fe837685c77
[ "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
19,657
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Yury Kudryashov -/ import algebra.group.defs import algebra.group.hom import algebra.group.type_tags import logic.embedding /-! # Definitions of group actions This file defines a hierarchy of group action type-classes: * `has_scalar M Ξ±` and its additive version `has_vadd G P` are notation typeclasses for `β€’` and `+α΅₯`, respectively; * `mul_action M Ξ±` and its additive version `add_action G P` are typeclasses used for actions of multiplicative and additive monoids and groups; * `distrib_mul_action M A` is a typeclass for an action of a multiplicative monoid on an additive monoid such that `a β€’ (b + c) = a β€’ b + a β€’ c` and `a β€’ 0 = 0`. The hierarchy is extended further by `module`, defined elsewhere. Also provided are type-classes regarding the interaction of different group actions, * `smul_comm_class M N Ξ±` and its additive version `vadd_comm_class M N Ξ±`; * `is_scalar_tower M N Ξ±` (no additive version). ## Notation - `a β€’ b` is used as notation for `has_scalar.smul a b`. - `a +α΅₯ b` is used as notation for `has_vadd.vadd a b`. ## Implementation details This file should avoid depending on other parts of `group_theory`, to avoid import cycles. More sophisticated lemmas belong in `group_theory.group_action`. ## Tags group action -/ variables {M N G A B Ξ± Ξ² Ξ³ : Type*} open function /-- Type class for the `+α΅₯` notation. -/ class has_vadd (G : Type*) (P : Type*) := (vadd : G β†’ P β†’ P) /-- Typeclass for types with a scalar multiplication operation, denoted `β€’` (`\bu`) -/ @[to_additive has_vadd] class has_scalar (M : Type*) (Ξ± : Type*) := (smul : M β†’ Ξ± β†’ Ξ±) infix ` +α΅₯ `:65 := has_vadd.vadd infixr ` β€’ `:73 := has_scalar.smul /-- Typeclass for faithful actions. -/ class has_faithful_vadd (G : Type*) (P : Type*) [has_vadd G P] : Prop := (eq_of_vadd_eq_vadd : βˆ€ {g₁ gβ‚‚ : G}, (βˆ€ p : P, g₁ +α΅₯ p = gβ‚‚ +α΅₯ p) β†’ g₁ = gβ‚‚) /-- Typeclass for faithful actions. -/ @[to_additive has_faithful_vadd] class has_faithful_scalar (M : Type*) (Ξ± : Type*) [has_scalar M Ξ±] : Prop := (eq_of_smul_eq_smul : βˆ€ {m₁ mβ‚‚ : M}, (βˆ€ a : Ξ±, m₁ β€’ a = mβ‚‚ β€’ a) β†’ m₁ = mβ‚‚) export has_faithful_scalar (eq_of_smul_eq_smul) has_faithful_vadd (eq_of_vadd_eq_vadd) @[to_additive] lemma smul_left_injective' [has_scalar M Ξ±] [has_faithful_scalar M Ξ±] : function.injective ((β€’) : M β†’ Ξ± β†’ Ξ±) := Ξ» m₁ mβ‚‚ h, has_faithful_scalar.eq_of_smul_eq_smul (congr_fun h) /-- See also `monoid.to_mul_action` and `mul_zero_class.to_smul_with_zero`. -/ @[priority 910, to_additive] -- see Note [lower instance priority] instance has_mul.to_has_scalar (Ξ± : Type*) [has_mul Ξ±] : has_scalar Ξ± Ξ± := ⟨(*)⟩ @[simp, to_additive] lemma smul_eq_mul (Ξ± : Type*) [has_mul Ξ±] {a a' : Ξ±} : a β€’ a' = a * a' := rfl /-- Type class for additive monoid actions. -/ @[protect_proj] class add_action (G : Type*) (P : Type*) [add_monoid G] extends has_vadd G P := (zero_vadd : βˆ€ p : P, (0 : G) +α΅₯ p = p) (add_vadd : βˆ€ (g₁ gβ‚‚ : G) (p : P), (g₁ + gβ‚‚) +α΅₯ p = g₁ +α΅₯ (gβ‚‚ +α΅₯ p)) /-- Typeclass for multiplicative actions by monoids. This generalizes group actions. -/ @[protect_proj, to_additive] class mul_action (Ξ± : Type*) (Ξ² : Type*) [monoid Ξ±] extends has_scalar Ξ± Ξ² := (one_smul : βˆ€ b : Ξ², (1 : Ξ±) β€’ b = b) (mul_smul : βˆ€ (x y : Ξ±) (b : Ξ²), (x * y) β€’ b = x β€’ y β€’ b) /-- A typeclass mixin saying that two additive actions on the same space commute. -/ class vadd_comm_class (M N Ξ± : Type*) [has_vadd M Ξ±] [has_vadd N Ξ±] : Prop := (vadd_comm : βˆ€ (m : M) (n : N) (a : Ξ±), m +α΅₯ (n +α΅₯ a) = n +α΅₯ (m +α΅₯ a)) /-- A typeclass mixin saying that two multiplicative actions on the same space commute. -/ @[to_additive] class smul_comm_class (M N Ξ± : Type*) [has_scalar M Ξ±] [has_scalar N Ξ±] : Prop := (smul_comm : βˆ€ (m : M) (n : N) (a : Ξ±), m β€’ n β€’ a = n β€’ m β€’ a) export mul_action (mul_smul) add_action (add_vadd) smul_comm_class (smul_comm) vadd_comm_class (vadd_comm) attribute [to_additive_reorder 1] has_pow attribute [to_additive_reorder 1 4] has_pow.pow attribute [to_additive has_scalar] has_pow attribute [to_additive has_scalar.smul] has_pow.pow /-- Frequently, we find ourselves wanting to express a bilinear map `M β†’β‚—[R] N β†’β‚—[R] P` or an equivalence between maps `(M β†’β‚—[R] N) ≃ₗ[R] (M' β†’β‚—[R] N')` where the maps have an associated ring `R`. Unfortunately, using definitions like these requires that `R` satisfy `comm_semiring R`, and not just `semiring R`. Using `M β†’β‚—[R] N β†’+ P` and `(M β†’β‚—[R] N) ≃+ (M' β†’β‚—[R] N')` avoids this problem, but throws away structure that is useful for when we _do_ have a commutative (semi)ring. To avoid making this compromise, we instead state these definitions as `M β†’β‚—[R] N β†’β‚—[S] P` or `(M β†’β‚—[R] N) ≃ₗ[S] (M' β†’β‚—[R] N')` and require `smul_comm_class S R` on the appropriate modules. When the caller has `comm_semiring R`, they can set `S = R` and `smul_comm_class_self` will populate the instance. If the caller only has `semiring R` they can still set either `R = β„•` or `S = β„•`, and `add_comm_monoid.nat_smul_comm_class` or `add_comm_monoid.nat_smul_comm_class'` will populate the typeclass, which is still sufficient to recover a `≃+` or `β†’+` structure. An example of where this is used is `linear_map.prod_equiv`. -/ library_note "bundled maps over different rings" /-- Commutativity of actions is a symmetric relation. This lemma can't be an instance because this would cause a loop in the instance search graph. -/ @[to_additive] lemma smul_comm_class.symm (M N Ξ± : Type*) [has_scalar M Ξ±] [has_scalar N Ξ±] [smul_comm_class M N Ξ±] : smul_comm_class N M Ξ± := ⟨λ a' a b, (smul_comm a a' b).symm⟩ /-- Commutativity of additive actions is a symmetric relation. This lemma can't be an instance because this would cause a loop in the instance search graph. -/ add_decl_doc vadd_comm_class.symm @[to_additive] instance smul_comm_class_self (M Ξ± : Type*) [comm_monoid M] [mul_action M Ξ±] : smul_comm_class M M Ξ± := ⟨λ a a' b, by rw [← mul_smul, mul_comm, mul_smul]⟩ /-- An instance of `is_scalar_tower M N Ξ±` states that the multiplicative action of `M` on `Ξ±` is determined by the multiplicative actions of `M` on `N` and `N` on `Ξ±`. -/ class is_scalar_tower (M N Ξ± : Type*) [has_scalar M N] [has_scalar N Ξ±] [has_scalar M Ξ±] : Prop := (smul_assoc : βˆ€ (x : M) (y : N) (z : Ξ±), (x β€’ y) β€’ z = x β€’ (y β€’ z)) @[simp] lemma smul_assoc {M N} [has_scalar M N] [has_scalar N Ξ±] [has_scalar M Ξ±] [is_scalar_tower M N Ξ±] (x : M) (y : N) (z : Ξ±) : (x β€’ y) β€’ z = x β€’ y β€’ z := is_scalar_tower.smul_assoc x y z instance semigroup.is_scalar_tower [semigroup Ξ±] : is_scalar_tower Ξ± Ξ± Ξ± := ⟨mul_assoc⟩ namespace has_scalar variables [has_scalar M Ξ±] /-- Auxiliary definition for `has_scalar.comp`, `mul_action.comp_hom`, `distrib_mul_action.comp_hom`, `module.comp_hom`, etc. -/ @[simp, to_additive /-" Auxiliary definition for `has_vadd.comp`, `add_action.comp_hom`, etc. "-/] def comp.smul (g : N β†’ M) (n : N) (a : Ξ±) : Ξ± := g n β€’ a variables (Ξ±) /-- An action of `M` on `Ξ±` and a funcion `N β†’ M` induces an action of `N` on `Ξ±`. See note [reducible non-instances]. Since this is reducible, we make sure to go via `has_scalar.comp.smul` to prevent typeclass inference unfolding too far. -/ @[reducible, to_additive /-" An additive action of `M` on `Ξ±` and a funcion `N β†’ M` induces an additive action of `N` on `Ξ±` "-/] def comp (g : N β†’ M) : has_scalar N Ξ± := { smul := has_scalar.comp.smul g } variables {Ξ±} /-- If an action forms a scalar tower then so does the action formed by `has_scalar.comp`. -/ @[priority 100] instance comp.is_scalar_tower [has_scalar M Ξ²] [has_scalar Ξ± Ξ²] [is_scalar_tower M Ξ± Ξ²] (g : N β†’ M) : (by haveI := comp Ξ± g; haveI := comp Ξ² g; exact is_scalar_tower N Ξ± Ξ²) := by exact {smul_assoc := Ξ» n, @smul_assoc _ _ _ _ _ _ _ (g n) } @[priority 100] instance comp.smul_comm_class [has_scalar Ξ² Ξ±] [smul_comm_class M Ξ² Ξ±] (g : N β†’ M) : (by haveI := comp Ξ± g; exact smul_comm_class N Ξ² Ξ±) := by exact {smul_comm := Ξ» n, @smul_comm _ _ _ _ _ _ (g n) } @[priority 100] instance comp.smul_comm_class' [has_scalar Ξ² Ξ±] [smul_comm_class Ξ² M Ξ±] (g : N β†’ M) : (by haveI := comp Ξ± g; exact smul_comm_class Ξ² N Ξ±) := by exact {smul_comm := Ξ» _ n, @smul_comm _ _ _ _ _ _ _ (g n) } end has_scalar section variables [monoid M] [mul_action M Ξ±] @[to_additive] lemma smul_smul (a₁ aβ‚‚ : M) (b : Ξ±) : a₁ β€’ aβ‚‚ β€’ b = (a₁ * aβ‚‚) β€’ b := (mul_smul _ _ _).symm variable (M) @[simp, to_additive] theorem one_smul (b : Ξ±) : (1 : M) β€’ b = b := mul_action.one_smul _ variables {M} /-- Pullback a multiplicative action along an injective map respecting `β€’`. See note [reducible non-instances]. -/ @[reducible, to_additive "Pullback an additive action along an injective map respecting `+α΅₯`."] protected def function.injective.mul_action [has_scalar M Ξ²] (f : Ξ² β†’ Ξ±) (hf : injective f) (smul : βˆ€ (c : M) x, f (c β€’ x) = c β€’ f x) : mul_action M Ξ² := { smul := (β€’), one_smul := Ξ» x, hf $ (smul _ _).trans $ one_smul _ (f x), mul_smul := Ξ» c₁ cβ‚‚ x, hf $ by simp only [smul, mul_smul] } /-- Pushforward a multiplicative action along a surjective map respecting `β€’`. See note [reducible non-instances]. -/ @[reducible, to_additive "Pushforward an additive action along a surjective map respecting `+α΅₯`."] protected def function.surjective.mul_action [has_scalar M Ξ²] (f : Ξ± β†’ Ξ²) (hf : surjective f) (smul : βˆ€ (c : M) x, f (c β€’ x) = c β€’ f x) : mul_action M Ξ² := { smul := (β€’), one_smul := Ξ» y, by { rcases hf y with ⟨x, rfl⟩, rw [← smul, one_smul] }, mul_smul := Ξ» c₁ cβ‚‚ y, by { rcases hf y with ⟨x, rfl⟩, simp only [← smul, mul_smul] } } section ite variables (p : Prop) [decidable p] @[to_additive] lemma ite_smul (a₁ aβ‚‚ : M) (b : Ξ±) : (ite p a₁ aβ‚‚) β€’ b = ite p (a₁ β€’ b) (aβ‚‚ β€’ b) := by split_ifs; refl @[to_additive] lemma smul_ite (a : M) (b₁ bβ‚‚ : Ξ±) : a β€’ (ite p b₁ bβ‚‚) = ite p (a β€’ b₁) (a β€’ bβ‚‚) := by split_ifs; refl end ite section variables (M) /-- The regular action of a monoid on itself by left multiplication. This is promoted to a module by `semiring.to_module`. -/ @[priority 910, to_additive] -- see Note [lower instance priority] instance monoid.to_mul_action : mul_action M M := { smul := (*), one_smul := one_mul, mul_smul := mul_assoc } /-- The regular action of a monoid on itself by left addition. This is promoted to an `add_torsor` by `add_group_is_add_torsor`. -/ add_decl_doc add_monoid.to_add_action instance is_scalar_tower.left : is_scalar_tower M M Ξ± := ⟨λ x y z, mul_smul x y z⟩ variables {M} /-- Note that the `smul_comm_class Ξ± Ξ² Ξ²` typeclass argument is usually satisfied by `algebra Ξ± Ξ²`. -/ @[to_additive] lemma mul_smul_comm [has_mul Ξ²] [has_scalar Ξ± Ξ²] [smul_comm_class Ξ± Ξ² Ξ²] (s : Ξ±) (x y : Ξ²) : x * (s β€’ y) = s β€’ (x * y) := (smul_comm s x y).symm /-- Note that the `is_scalar_tower Ξ± Ξ² Ξ²` typeclass argument is usually satisfied by `algebra Ξ± Ξ²`. -/ lemma smul_mul_assoc [has_mul Ξ²] [has_scalar Ξ± Ξ²] [is_scalar_tower Ξ± Ξ² Ξ²] (r : Ξ±) (x y : Ξ²) : (r β€’ x) * y = r β€’ (x * y) := smul_assoc r x y /-- Note that the `is_scalar_tower M Ξ± Ξ±` and `smul_comm_class M Ξ± Ξ±` typeclass arguments are usually satisfied by `algebra M Ξ±`. -/ lemma smul_mul_smul [has_mul Ξ±] (r s : M) (x y : Ξ±) [is_scalar_tower M Ξ± Ξ±] [smul_comm_class M Ξ± Ξ±] : (r β€’ x) * (s β€’ y) = (r * s) β€’ (x * y) := by rw [smul_mul_assoc, mul_smul_comm, ← smul_assoc, smul_eq_mul] end namespace mul_action variables (M Ξ±) /-- Embedding of `Ξ±` into functions `M β†’ Ξ±` induced by a multiplicative action of `M` on `Ξ±`. -/ @[to_additive] def to_fun : Ξ± β†ͺ (M β†’ Ξ±) := ⟨λ y x, x β€’ y, Ξ» y₁ yβ‚‚ H, one_smul M y₁ β–Έ one_smul M yβ‚‚ β–Έ by convert congr_fun H 1⟩ /-- Embedding of `Ξ±` into functions `M β†’ Ξ±` induced by an additive action of `M` on `Ξ±`. -/ add_decl_doc add_action.to_fun variables {M Ξ±} @[simp, to_additive] lemma to_fun_apply (x : M) (y : Ξ±) : mul_action.to_fun M Ξ± y x = x β€’ y := rfl variable (Ξ±) /-- A multiplicative action of `M` on `Ξ±` and a monoid homomorphism `N β†’ M` induce a multiplicative action of `N` on `Ξ±`. See note [reducible non-instances]. -/ @[reducible, to_additive] def comp_hom [monoid N] (g : N β†’* M) : mul_action N Ξ± := { smul := has_scalar.comp.smul g, one_smul := by simp [g.map_one, mul_action.one_smul], mul_smul := by simp [g.map_mul, mul_action.mul_smul] } /-- An additive action of `M` on `Ξ±` and an additive monoid homomorphism `N β†’ M` induce an additive action of `N` on `Ξ±`. See note [reducible non-instances]. -/ add_decl_doc add_action.comp_hom end mul_action end section compatible_scalar @[simp] lemma smul_one_smul {M} (N) [monoid N] [has_scalar M N] [mul_action N Ξ±] [has_scalar M Ξ±] [is_scalar_tower M N Ξ±] (x : M) (y : Ξ±) : (x β€’ (1 : N)) β€’ y = x β€’ y := by rw [smul_assoc, one_smul] end compatible_scalar /-- Typeclass for multiplicative actions on additive structures. This generalizes group modules. -/ class distrib_mul_action (M : Type*) (A : Type*) [monoid M] [add_monoid A] extends mul_action M A := (smul_add : βˆ€(r : M) (x y : A), r β€’ (x + y) = r β€’ x + r β€’ y) (smul_zero : βˆ€(r : M), r β€’ (0 : A) = 0) section variables [monoid M] [add_monoid A] [distrib_mul_action M A] theorem smul_add (a : M) (b₁ bβ‚‚ : A) : a β€’ (b₁ + bβ‚‚) = a β€’ b₁ + a β€’ bβ‚‚ := distrib_mul_action.smul_add _ _ _ @[simp] theorem smul_zero (a : M) : a β€’ (0 : A) = 0 := distrib_mul_action.smul_zero _ /-- Pullback a distributive multiplicative action along an injective additive monoid homomorphism. See note [reducible non-instances]. -/ @[reducible] protected def function.injective.distrib_mul_action [add_monoid B] [has_scalar M B] (f : B β†’+ A) (hf : injective f) (smul : βˆ€ (c : M) x, f (c β€’ x) = c β€’ f x) : distrib_mul_action M B := { smul := (β€’), smul_add := Ξ» c x y, hf $ by simp only [smul, f.map_add, smul_add], smul_zero := Ξ» c, hf $ by simp only [smul, f.map_zero, smul_zero], .. hf.mul_action f smul } /-- Pushforward a distributive multiplicative action along a surjective additive monoid homomorphism. See note [reducible non-instances]. -/ @[reducible] protected def function.surjective.distrib_mul_action [add_monoid B] [has_scalar M B] (f : A β†’+ B) (hf : surjective f) (smul : βˆ€ (c : M) x, f (c β€’ x) = c β€’ f x) : distrib_mul_action M B := { smul := (β€’), smul_add := Ξ» c x y, by { rcases hf x with ⟨x, rfl⟩, rcases hf y with ⟨y, rfl⟩, simp only [smul_add, ← smul, ← f.map_add] }, smul_zero := Ξ» c, by simp only [← f.map_zero, ← smul, smul_zero], .. hf.mul_action f smul } variable (A) /-- Compose a `distrib_mul_action` with a `monoid_hom`, with action `f r' β€’ m`. See note [reducible non-instances]. -/ @[reducible] def distrib_mul_action.comp_hom [monoid N] (f : N β†’* M) : distrib_mul_action N A := { smul := has_scalar.comp.smul f, smul_zero := Ξ» x, smul_zero (f x), smul_add := Ξ» x, smul_add (f x), .. mul_action.comp_hom A f } /-- Each element of the monoid defines a additive monoid homomorphism. -/ @[simps] def distrib_mul_action.to_add_monoid_hom (x : M) : A β†’+ A := { to_fun := (β€’) x, map_zero' := smul_zero x, map_add' := smul_add x } variables (M) /-- Each element of the monoid defines an additive monoid homomorphism. -/ @[simps] def distrib_mul_action.to_add_monoid_End : M β†’* add_monoid.End A := { to_fun := distrib_mul_action.to_add_monoid_hom A, map_one' := add_monoid_hom.ext $ one_smul M, map_mul' := Ξ» x y, add_monoid_hom.ext $ mul_smul x y } end section variables [monoid M] [add_group A] [distrib_mul_action M A] @[simp] theorem smul_neg (r : M) (x : A) : r β€’ (-x) = -(r β€’ x) := eq_neg_of_add_eq_zero $ by rw [← smul_add, neg_add_self, smul_zero] theorem smul_sub (r : M) (x y : A) : r β€’ (x - y) = r β€’ x - r β€’ y := by rw [sub_eq_add_neg, sub_eq_add_neg, smul_add, smul_neg] end variable (Ξ±) /-- The monoid of endomorphisms. Note that this is generalized by `category_theory.End` to categories other than `Type u`. -/ protected def function.End := Ξ± β†’ Ξ± instance : monoid (function.End Ξ±) := { one := id, mul := (∘), mul_assoc := Ξ» f g h, rfl, mul_one := Ξ» f, rfl, one_mul := Ξ» f, rfl, } instance : inhabited (function.End Ξ±) := ⟨1⟩ variable {Ξ±} /-- The tautological action by `function.End Ξ±` on `Ξ±`. This is generalized to bundled endomorphisms by: * `equiv.perm.apply_mul_action` * `add_monoid.End.apply_distrib_mul_action` * `add_aut.apply_distrib_mul_action` * `ring_hom.apply_distrib_mul_action` * `linear_equiv.apply_distrib_mul_action` * `linear_map.apply_module` * `ring_hom.apply_mul_semiring_action` * `alg_equiv.apply_mul_semiring_action` -/ instance function.End.apply_mul_action : mul_action (function.End Ξ±) Ξ± := { smul := ($), one_smul := Ξ» _, rfl, mul_smul := Ξ» _ _ _, rfl } @[simp] lemma function.End.smul_def (f : function.End Ξ±) (a : Ξ±) : f β€’ a = f a := rfl /-- `function.End.apply_mul_action` is faithful. -/ instance function.End.apply_has_faithful_scalar : has_faithful_scalar (function.End Ξ±) Ξ± := ⟨λ x y, funext⟩ /-- The tautological action by `add_monoid.End Ξ±` on `Ξ±`. This generalizes `function.End.apply_mul_action`. -/ instance add_monoid.End.apply_distrib_mul_action [add_monoid Ξ±] : distrib_mul_action (add_monoid.End Ξ±) Ξ± := { smul := ($), smul_zero := add_monoid_hom.map_zero, smul_add := add_monoid_hom.map_add, one_smul := Ξ» _, rfl, mul_smul := Ξ» _ _ _, rfl } @[simp] lemma add_monoid.End.smul_def [add_monoid Ξ±] (f : add_monoid.End Ξ±) (a : Ξ±) : f β€’ a = f a := rfl /-- `add_monoid.End.apply_distrib_mul_action` is faithful. -/ instance add_monoid.End.apply_has_faithful_scalar [add_monoid Ξ±] : has_faithful_scalar (add_monoid.End Ξ±) Ξ± := ⟨add_monoid_hom.ext⟩ /-- The monoid hom representing a monoid action. When `M` is a group, see `mul_action.to_perm_hom`. -/ def mul_action.to_End_hom [monoid M] [mul_action M Ξ±] : M β†’* function.End Ξ± := { to_fun := (β€’), map_one' := funext (one_smul M), map_mul' := Ξ» x y, funext (mul_smul x y) } /-- The monoid action induced by a monoid hom to `function.End Ξ±` See note [reducible non-instances]. -/ @[reducible] def mul_action.of_End_hom [monoid M] (f : M β†’* function.End Ξ±) : mul_action M Ξ± := mul_action.comp_hom Ξ± f /-- The tautological additive action by `additive (function.End Ξ±)` on `Ξ±`. -/ instance add_action.function_End : add_action (additive (function.End Ξ±)) Ξ± := { vadd := ($), zero_vadd := Ξ» _, rfl, add_vadd := Ξ» _ _ _, rfl } /-- The additive monoid hom representing an additive monoid action. When `M` is a group, see `add_action.to_perm_hom`. -/ def add_action.to_End_hom [add_monoid M] [add_action M Ξ±] : M β†’+ additive (function.End Ξ±) := { to_fun := (+α΅₯), map_zero' := funext (zero_vadd M), map_add' := Ξ» x y, funext (add_vadd x y) } /-- The additive action induced by a hom to `additive (function.End Ξ±)` See note [reducible non-instances]. -/ @[reducible] def add_action.of_End_hom [add_monoid M] (f : M β†’+ additive (function.End Ξ±)) : add_action M Ξ± := add_action.comp_hom Ξ± f
5d7771b3acb34a6e2a8403d16aad421c4640a0fd
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/category_theory/adjunction/evaluation.lean
735917af13f81fa5628b1f8682c7a77e533e9c96
[ "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
4,848
lean
/- Copyright (c) 2021 Adam Topaz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Adam Topaz -/ import category_theory.limits.shapes.products import category_theory.functor.epi_mono /-! # Adjunctions involving evaluation We show that evaluation of functors have adjoints, given the existence of (co)products. -/ namespace category_theory open category_theory.limits universes v₁ vβ‚‚ u₁ uβ‚‚ variables {C : Type u₁} [category.{v₁} C] (D : Type uβ‚‚) [category.{vβ‚‚} D] noncomputable theory section variables [βˆ€ (a b : C), has_coproducts_of_shape (a ⟢ b) D] /-- The left adjoint of evaluation. -/ @[simps] def evaluation_left_adjoint (c : C) : D β₯€ C β₯€ D := { obj := Ξ» d, { obj := Ξ» t, ∐ (Ξ» i : c ⟢ t, d), map := Ξ» u v f, sigma.desc $ Ξ» g, sigma.ΞΉ (Ξ» _, d) $ g ≫ f, map_id' := begin intros, ext ⟨j⟩, simp only [cofan.mk_ΞΉ_app, colimit.ΞΉ_desc, category.comp_id], congr' 1, rw category.comp_id, end, map_comp' := begin intros, ext, simp only [cofan.mk_ΞΉ_app, colimit.ΞΉ_desc_assoc, colimit.ΞΉ_desc], congr' 1, rw category.assoc, end }, map := Ξ» d₁ dβ‚‚ f, { app := Ξ» e, sigma.desc $ Ξ» h, f ≫ sigma.ΞΉ (Ξ» _, dβ‚‚) h, naturality' := by { intros, ext, dsimp, simp } }, map_id' := by { intros, ext x ⟨j⟩, dsimp, simp }, map_comp' := by { intros, ext, dsimp, simp } } /-- The adjunction showing that evaluation is a right adjoint. -/ @[simps unit_app counit_app_app] def evaluation_adjunction_right (c : C) : evaluation_left_adjoint D c ⊣ (evaluation _ _).obj c := adjunction.mk_of_hom_equiv { hom_equiv := Ξ» d F, { to_fun := Ξ» f, sigma.ΞΉ (Ξ» _, d) (πŸ™ _) ≫ f.app c, inv_fun := Ξ» f, { app := Ξ» e, sigma.desc $ Ξ» h, f ≫ F.map h, naturality' := by { intros, ext, dsimp, simp } }, left_inv := begin intros f, ext x ⟨g⟩, dsimp, simp only [colimit.ΞΉ_desc, limits.cofan.mk_ΞΉ_app, category.assoc, ← f.naturality, evaluation_left_adjoint_obj_map, colimit.ΞΉ_desc_assoc, cofan.mk_ΞΉ_app], congr' 2, rw category.id_comp end, right_inv := Ξ» f, by { dsimp, simp } }, hom_equiv_naturality_left_symm' := by { intros, ext, dsimp, simp }, hom_equiv_naturality_right' := by { intros, dsimp, simp } } instance evaluation_is_right_adjoint (c : C) : is_right_adjoint ((evaluation _ D).obj c) := ⟨_, evaluation_adjunction_right _ _⟩ lemma nat_trans.mono_iff_mono_app {F G : C β₯€ D} (Ξ· : F ⟢ G) : mono Ξ· ↔ (βˆ€ c, mono (Ξ·.app c)) := begin split, { introsI h c, exact (infer_instance : mono (((evaluation _ _).obj c).map Ξ·)) }, { introsI _, apply nat_trans.mono_of_mono_app } end end section variables [βˆ€ (a b : C), has_products_of_shape (a ⟢ b) D] /-- The right adjoint of evaluation. -/ @[simps] def evaluation_right_adjoint (c : C) : D β₯€ C β₯€ D := { obj := Ξ» d, { obj := Ξ» t, ∏ (Ξ» i : t ⟢ c, d), map := Ξ» u v f, pi.lift $ Ξ» g, pi.Ο€ _ $ f ≫ g, map_id' := begin intros, ext ⟨j⟩, dsimp, simp only [limit.lift_Ο€, category.id_comp, fan.mk_Ο€_app], congr, simp, end, map_comp' := begin intros, ext ⟨j⟩, dsimp, simp only [limit.lift_Ο€, fan.mk_Ο€_app, category.assoc], congr' 1, simp, end }, map := Ξ» d₁ dβ‚‚ f, { app := Ξ» t, pi.lift $ Ξ» g, pi.Ο€ _ g ≫ f, naturality' := by { intros, ext, dsimp, simp } }, map_id' := by { intros, ext x ⟨j⟩, dsimp, simp }, map_comp' := by { intros, ext, dsimp, simp } } /-- The adjunction showing that evaluation is a left adjoint. -/ @[simps unit_app_app counit_app] def evaluation_adjunction_left (c : C) : (evaluation _ _).obj c ⊣ evaluation_right_adjoint D c := adjunction.mk_of_hom_equiv { hom_equiv := Ξ» F d, { to_fun := Ξ» f, { app := Ξ» t, pi.lift $ Ξ» g, F.map g ≫ f, naturality' := by { intros, ext, dsimp, simp } }, inv_fun := Ξ» f, f.app _ ≫ pi.Ο€ _ (πŸ™ _), left_inv := Ξ» f, by { dsimp, simp }, right_inv := begin intros f, ext x ⟨g⟩, dsimp, simp only [limit.lift_Ο€, evaluation_right_adjoint_obj_map, nat_trans.naturality_assoc, fan.mk_Ο€_app], congr, rw category.comp_id end }, hom_equiv_naturality_left_symm' := by { intros, dsimp, simp }, hom_equiv_naturality_right' := by { intros, ext, dsimp, simp } } instance evaluation_is_left_adjoint (c : C) : is_left_adjoint ((evaluation _ D).obj c) := ⟨_, evaluation_adjunction_left _ _⟩ lemma nat_trans.epi_iff_epi_app {F G : C β₯€ D} (Ξ· : F ⟢ G) : epi Ξ· ↔ (βˆ€ c, epi (Ξ·.app c)) := begin split, { introsI h c, exact (infer_instance : epi (((evaluation _ _).obj c).map Ξ·)) }, { introsI, apply nat_trans.epi_of_epi_app } end end end category_theory
c94eb7026fae8830906358867a8c0217b35d4154
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/tests/lean/unfold1.lean
a3cab75b52185c2bf17728fa1aade42f8d74c0ce
[ "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
515
lean
exit -- TODO(Leo): enable test again after we add rfl lemma support to type_context open tactic meta_definition rewriteH (Hname : name) : tactic unit := do get_local Hname >>= rewrite_core reducible tt occurrences.all ff, try reflexivity example (l : list nat) : list.append l [] = l := by do get_local `l >>= Ξ» H, induction_core semireducible H `list.rec_on [`h, `t, `iH], -- unfold [`list.append], trace_state, trace "------", reflexivity, unfold [`list.append], trace_state, rewriteH `iH
6e202547246748a35ac14a0fb8694be0ba77e16c
59a4b050600ed7b3d5826a8478db0a9bdc190252
/src/category_theory/universal/complete/functor_category.lean
d856db6c8e65ab9919a1b7681566044859b78a51
[]
no_license
rwbarton/lean-category-theory
f720268d800b62a25d69842ca7b5d27822f00652
00df814d463406b7a13a56f5dcda67758ba1b419
refs/heads/master
1,585,366,296,767
1,536,151,349,000
1,536,151,349,000
147,652,096
0
0
null
1,536,226,960,000
1,536,226,960,000
null
UTF-8
Lean
false
false
2,214
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.currying import category_theory.functor_categories.whiskering import category_theory.universal.complete import category_theory.universal.comparisons open category_theory open category_theory.prod namespace category_theory.limits universes u v private meta def dsimp' : tactic unit := `[dsimp at * {unfold_reducible := tt, md := semireducible}] variables {J : Type v} [small_category J] {C : Type v} [small_category C] {D : Type u} [π’Ÿ : category.{u v} D] include π’Ÿ -- def switch_curry : (J β₯€ (C β₯€ D)) β₯€ (C β₯€ (J β₯€ D)) := uncurry β‹™ (whisker_on_left_functor (prod.swap C J) D) β‹™ curry def switched (F : J β₯€ (C β₯€ D)) : C β₯€ (J β₯€ D) := { obj := Ξ» c, { obj := Ξ» j, (F j) c, map' := Ξ» j j' f, (F.map f) c }, map' := Ξ» c c' f, { app := Ξ» j, (F j).map f }}. -- section -- local attribute [back] category.id -- def switched_twice (F : J β₯€ (C β₯€ D)) : switched (switched F) β‰… F := by obviously -- end def introduce_switch (F : J β₯€ (C β₯€ D)) {j j' : J} (f : j ⟢ j') (X : C) : (F.map f) X = ((switched F) X).map f := sorry def limit_cone_in_functor_category [has_limits.{u v} D] (F : J β₯€ (C β₯€ D)) : cone F := { X := ((switched F) β‹™ lim), Ο€ := Ξ» j, { app := Ξ» X : C, (limit.cone (switched F X)).Ο€ j }, w := Ξ» j j' f, begin ext1, dsimp at *, rw introduce_switch, obviously, end }. instance [has_limits.{u v} D] : has_limits.{(max u v) v} (C β₯€ D) := { limit := Ξ» J π’₯ F, begin resetI, exact limit_cone_in_functor_category F end, is_limit := Ξ» J π’₯ F, begin resetI, exact { lift := Ξ» s, { app := Ξ» X, (limit.cone_morphism (switched F X) { X := s.X X, Ο€ := Ξ» j, (s.Ο€ j) X }).hom, naturality' := begin tidy, dsimp [limit_cone_in_functor_category], -- FIXME why does this rw fail? I wanted to apply this to both sides, then use naturality. -- rw limit.pullback_lift (switched F Y), sorry end, }, fac' := sorry, uniq' := sorry } end } end category_theory.limits
cfc0118593292b3de875e73e50c96d93bbb01197
b73bd2854495d87ad5ce4f247cfcd6faa7e71c7e
/src/game/world1/level6.lean
a04a06f02374a073ee95be8f8837a912e69b4f6e
[]
no_license
agusakov/category-theory-game
20db0b26270e0c95a3d5605498570273d72f731d
652dd7e90ae706643b2a597e2c938403653e167d
refs/heads/master
1,669,201,216,310
1,595,740,057,000
1,595,740,057,000
280,895,295
12
0
null
null
null
null
UTF-8
Lean
false
false
680
lean
import category_theory.category.default import game.world1.level4 universes v u -- The order in this declaration matters: v often needs to be explicitly specified while u often can be omitted namespace category_theory variables (C : Type u) [category.{v} C] /- # Category world ## Level 5: More tactic reviews -/ /-blah blah -/ /- Lemma If $$f : X ⟢ Y$$ and $$g : X ⟢ Y$$ are morphisms such that $$f = g$$, then $$f ≫ h = g ≫ h$$. -/ lemma id_of_comp_right_id' (X : C) (f : X ⟢ X) (w : βˆ€ {Y : C} (g : Y ⟢ X), g ≫ f = g) : f = πŸ™ X := begin apply eq_of_comp_right_eq'', intros Z h, rw category.comp_id h, apply w, end end category_theory
8a46fe00e4870c167897858fc34b7c9ef7a19fbb
86f6f4f8d827a196a32bfc646234b73328aeb306
/examples/basics/unnamed_115.lean
6a142b6a1d64016fbe186589ae54e65dc0f170f0
[]
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
182
lean
import data.real.basic -- BEGIN example (a b c : ℝ) : (c * b) * a = b * (a * c) := begin sorry end example (a b c : ℝ) : a * (b * c) = b * (a * c) := begin sorry end -- END
a4727ea91f64dc5ad5c2a07a7adf898b6088fadc
d1a52c3f208fa42c41df8278c3d280f075eb020c
/src/Lean/Meta/WHNF.lean
725472da784d48cd12f0b763eac0d1a75150855d
[ "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
30,230
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.ToExpr import Lean.AuxRecursor import Lean.ProjFns import Lean.Structure import Lean.Util.Recognizers import Lean.Meta.Basic import Lean.Meta.GetConst import Lean.Meta.Match.MatcherInfo namespace Lean.Meta /- =========================== Smart unfolding support =========================== -/ def smartUnfoldingSuffix := "_sunfold" @[inline] def mkSmartUnfoldingNameFor (declName : Name) : Name := Name.mkStr declName smartUnfoldingSuffix register_builtin_option smartUnfolding : Bool := { defValue := true descr := "when computing weak head normal form, use auxiliary definition created for functions defined by structural recursion" } /-- Add auxiliary annotation to indicate the `match`-expression `e` must be reduced when performing smart unfolding. -/ def markSmartUnfoldingMatch (e : Expr) : Expr := mkAnnotation `sunfoldMatch e def smartUnfoldingMatch? (e : Expr) : Option Expr := annotation? `sunfoldMatch e /-- Add auxiliary annotation to indicate expression `e` (a `match` alternative rhs) was successfully reduced by smart unfolding. -/ def markSmartUnfoldigMatchAlt (e : Expr) : Expr := mkAnnotation `sunfoldMatchAlt e def smartUnfoldingMatchAlt? (e : Expr) : Option Expr := annotation? `sunfoldMatchAlt e /- =========================== Helper methods =========================== -/ def isAuxDef (constName : Name) : MetaM Bool := do let env ← getEnv return isAuxRecursor env constName || isNoConfusion env constName @[inline] private def matchConstAux {Ξ±} (e : Expr) (failK : Unit β†’ MetaM Ξ±) (k : ConstantInfo β†’ List Level β†’ MetaM Ξ±) : MetaM Ξ± := match e with | Expr.const name lvls _ => do let (some cinfo) ← getConst? name | failK () k cinfo lvls | _ => failK () /- =========================== Helper functions for reducing recursors =========================== -/ private def getFirstCtor (d : Name) : MetaM (Option Name) := do let some (ConstantInfo.inductInfo { ctors := ctor::_, ..}) ← getConstNoEx? d | pure none return some ctor private def mkNullaryCtor (type : Expr) (nparams : Nat) : MetaM (Option Expr) := do match type.getAppFn with | Expr.const d lvls _ => let (some ctor) ← getFirstCtor d | pure none return mkAppN (mkConst ctor lvls) (type.getAppArgs.shrink nparams) | _ => return none def toCtorIfLit : Expr β†’ Expr | Expr.lit (Literal.natVal v) _ => if v == 0 then mkConst `Nat.zero else mkApp (mkConst `Nat.succ) (mkRawNatLit (v-1)) | Expr.lit (Literal.strVal v) _ => mkApp (mkConst `String.mk) (toExpr v.toList) | e => e private def getRecRuleFor (recVal : RecursorVal) (major : Expr) : Option RecursorRule := match major.getAppFn with | Expr.const fn _ _ => recVal.rules.find? fun r => r.ctor == fn | _ => none private def toCtorWhenK (recVal : RecursorVal) (major : Expr) : MetaM Expr := do let majorType ← inferType major let majorType ← instantiateMVars (← whnf majorType) let majorTypeI := majorType.getAppFn if !majorTypeI.isConstOf recVal.getInduct then return major else if majorType.hasExprMVar && majorType.getAppArgs[recVal.numParams:].any Expr.hasExprMVar then return major else do let (some newCtorApp) ← mkNullaryCtor majorType recVal.numParams | pure major let newType ← inferType newCtorApp if (← isDefEq majorType newType) then return newCtorApp else return major /-- If `major` is not a constructor application, and its type is a structure `C ...`, then return `C.mk major.1 ... major.n` \pre `inductName` is `C`. If `Meta.Config.etaStruct` is `false` or the condition above does not hold, this method just returns `major`. -/ private def toCtorWhenStructure (inductName : Name) (major : Expr) : MetaM Expr := do unless (← getConfig).etaStruct do return major let env ← getEnv if !isStructureLike env inductName then return major else if let some _ := major.isConstructorApp? env then return major else let majorType ← inferType major let majorType ← instantiateMVars (← whnf majorType) let majorTypeI := majorType.getAppFn if !majorTypeI.isConstOf inductName then return major match majorType.getAppFn with | Expr.const d us _ => let some ctorName ← getFirstCtor d | pure major let ctorInfo ← getConstInfoCtor ctorName let mut result := mkAppN (mkConst ctorName us) (majorType.getAppArgs.shrink ctorInfo.numParams) for i in [:ctorInfo.numFields] do result := mkApp result (mkProj inductName i major) return result | _ => return major /-- Auxiliary function for reducing recursor applications. -/ private def reduceRec (recVal : RecursorVal) (recLvls : List Level) (recArgs : Array Expr) (failK : Unit β†’ MetaM Ξ±) (successK : Expr β†’ MetaM Ξ±) : MetaM Ξ± := let majorIdx := recVal.getMajorIdx if h : majorIdx < recArgs.size then do let major := recArgs.get ⟨majorIdx, h⟩ let mut major ← whnf major if recVal.k then major ← toCtorWhenK recVal major major := toCtorIfLit major major ← toCtorWhenStructure recVal.getInduct major match getRecRuleFor recVal major with | some rule => let majorArgs := major.getAppArgs if recLvls.length != recVal.levelParams.length then failK () else let rhs := rule.rhs.instantiateLevelParams recVal.levelParams recLvls -- Apply parameters, motives and minor premises from recursor application. let rhs := mkAppRange rhs 0 (recVal.numParams+recVal.numMotives+recVal.numMinors) recArgs /- The number of parameters in the constructor is not necessarily equal to the number of parameters in the recursor when we have nested inductive types. -/ let nparams := majorArgs.size - rule.nfields let rhs := mkAppRange rhs nparams majorArgs.size majorArgs let rhs := mkAppRange rhs (majorIdx + 1) recArgs.size recArgs successK rhs | none => failK () else failK () /- =========================== Helper functions for reducing Quot.lift and Quot.ind =========================== -/ /-- Auxiliary function for reducing `Quot.lift` and `Quot.ind` applications. -/ private def reduceQuotRec (recVal : QuotVal) (recLvls : List Level) (recArgs : Array Expr) (failK : Unit β†’ MetaM Ξ±) (successK : Expr β†’ MetaM Ξ±) : MetaM Ξ± := let process (majorPos argPos : Nat) : MetaM Ξ± := if h : majorPos < recArgs.size then do let major := recArgs.get ⟨majorPos, h⟩ let major ← whnf major match major with | Expr.app (Expr.app (Expr.app (Expr.const majorFn _ _) _ _) _ _) majorArg _ => do let some (ConstantInfo.quotInfo { kind := QuotKind.ctor, .. }) ← getConstNoEx? majorFn | failK () let f := recArgs[argPos] let r := mkApp f majorArg let recArity := majorPos + 1 successK $ mkAppRange r recArity recArgs.size recArgs | _ => failK () else failK () match recVal.kind with | QuotKind.lift => process 5 3 | QuotKind.ind => process 4 3 | _ => failK () /- =========================== Helper function for extracting "stuck term" =========================== -/ mutual private partial def isRecStuck? (recVal : RecursorVal) (recLvls : List Level) (recArgs : Array Expr) : MetaM (Option MVarId) := if recVal.k then -- TODO: improve this case return none else do let majorIdx := recVal.getMajorIdx if h : majorIdx < recArgs.size then do let major := recArgs.get ⟨majorIdx, h⟩ let major ← whnf major getStuckMVar? major else return none private partial def isQuotRecStuck? (recVal : QuotVal) (recLvls : List Level) (recArgs : Array Expr) : MetaM (Option MVarId) := let process? (majorPos : Nat) : MetaM (Option MVarId) := if h : majorPos < recArgs.size then do let major := recArgs.get ⟨majorPos, h⟩ let major ← whnf major getStuckMVar? major else return none match recVal.kind with | QuotKind.lift => process? 5 | QuotKind.ind => process? 4 | _ => return none /-- Return `some (Expr.mvar mvarId)` if metavariable `mvarId` is blocking reduction. -/ partial def getStuckMVar? : Expr β†’ MetaM (Option MVarId) | Expr.mdata _ e _ => getStuckMVar? e | Expr.proj _ _ e _ => do getStuckMVar? (← whnf e) | e@(Expr.mvar ..) => do let e ← instantiateMVars e match e with | Expr.mvar mvarId _ => pure (some mvarId) | _ => getStuckMVar? e | e@(Expr.app f _ _) => let f := f.getAppFn match f with | Expr.mvar mvarId _ => return some mvarId | Expr.const fName fLvls _ => do let cinfo? ← getConstNoEx? fName match cinfo? with | some $ ConstantInfo.recInfo recVal => isRecStuck? recVal fLvls e.getAppArgs | some $ ConstantInfo.quotInfo recVal => isQuotRecStuck? recVal fLvls e.getAppArgs | _ => return none | _ => return none | _ => return none end /- =========================== Weak Head Normal Form auxiliary combinators =========================== -/ /-- Auxiliary combinator for handling easy WHNF cases. It takes a function for handling the "hard" cases as an argument -/ @[specialize] partial def whnfEasyCases (e : Expr) (k : Expr β†’ MetaM Expr) : MetaM Expr := do match e with | Expr.forallE .. => return e | Expr.lam .. => return e | Expr.sort .. => return e | Expr.lit .. => return e | Expr.bvar .. => unreachable! | Expr.letE .. => k e | Expr.const .. => k e | Expr.app .. => k e | Expr.proj .. => k e | Expr.mdata _ e _ => whnfEasyCases e k | Expr.fvar fvarId _ => let decl ← getLocalDecl fvarId match decl with | LocalDecl.cdecl .. => return e | LocalDecl.ldecl (value := v) (nonDep := nonDep) .. => let cfg ← getConfig if nonDep && !cfg.zetaNonDep then return e else if cfg.trackZeta then modify fun s => { s with zetaFVarIds := s.zetaFVarIds.insert fvarId } whnfEasyCases v k | Expr.mvar mvarId _ => match (← getExprMVarAssignment? mvarId) with | some v => whnfEasyCases v k | none => return e @[specialize] private def deltaDefinition (c : ConstantInfo) (lvls : List Level) (failK : Unit β†’ Ξ±) (successK : Expr β†’ Ξ±) : Ξ± := if c.levelParams.length != lvls.length then failK () else let val := c.instantiateValueLevelParams lvls successK val @[specialize] private def deltaBetaDefinition (c : ConstantInfo) (lvls : List Level) (revArgs : Array Expr) (failK : Unit β†’ Ξ±) (successK : Expr β†’ Ξ±) : Ξ± := if c.levelParams.length != lvls.length then failK () else let val := c.instantiateValueLevelParams lvls let val := val.betaRev revArgs successK val inductive ReduceMatcherResult where | reduced (val : Expr) | stuck (val : Expr) | notMatcher | partialApp /-- The "match" compiler uses `if-then-else` expressions and other auxiliary declarations to compile match-expressions such as ``` match v with | 'a' => 1 | 'b' => 2 | _ => 3 ``` because it is more efficient than using `casesOn` recursors. The method `reduceMatcher?` fails if these auxiliary definitions (e.g., `ite`) cannot be unfolded in the current transparency setting. This is problematic because tactics such as `simp` use `TransparencyMode.reducible`, and most users assume that expressions such as ``` match 0 with | 0 => 1 | 100 => 2 | _ => 3 ``` should reduce in any transparency mode. Thus, we define a custom `canUnfoldAtMatcher` predicate for `whnfMatcher`. This solution is not very modular because modications at the `match` compiler require changes here. We claim this is defensible because it is reducing the auxiliary declaration defined by the `match` compiler. Alternative solution: tactics that use `TransparencyMode.reducible` should rely on the equations we generated for match-expressions. This solution is also not perfect because the match-expression above will not reduce during type checking when we are not using `TransparencyMode.default` or `TransparencyMode.all`. -/ private def canUnfoldAtMatcher (cfg : Config) (info : ConstantInfo) : CoreM Bool := do match cfg.transparency with | TransparencyMode.all => return true | TransparencyMode.default => return true | m => if (← isReducible info.name) || isGlobalInstance (← getEnv) info.name then return true else return info.name == ``ite || info.name == ``dite || info.name == ``decEq || info.name == ``Nat.decEq || info.name == ``Char.ofNat || info.name == ``Char.ofNatAux || info.name == ``String.decEq || info.name == ``List.hasDecEq || info.name == ``Fin.ofNat || info.name == ``UInt8.ofNat || info.name == ``UInt8.decEq || info.name == ``UInt16.ofNat || info.name == ``UInt16.decEq || info.name == ``UInt32.ofNat || info.name == ``UInt32.decEq || info.name == ``UInt64.ofNat || info.name == ``UInt64.decEq /- Remark: we need to unfold the following two definitions because they are used for `Fin`, and lazy unfolding at `isDefEq` does not unfold projections. -/ || info.name == ``HMod.hMod || info.name == ``Mod.mod private def whnfMatcher (e : Expr) : MetaM Expr := do /- When reducing `match` expressions, if the reducibility setting is at `TransparencyMode.reducible`, we increase it to `TransparencyMode.instance`. We use the `TransparencyMode.reducible` in many places (e.g., `simp`), and this setting prevents us from reducing `match` expressions where the discriminants are terms such as `OfNat.ofNat Ξ± n inst`. For example, `simp [Int.div]` will not unfold the application `Int.div 2 1` occuring in the target. TODO: consider other solutions; investigate whether the solution above produces counterintuitive behavior. -/ let mut transparency ← getTransparency if transparency == TransparencyMode.reducible then transparency := TransparencyMode.instances withTransparency transparency <| withReader (fun ctx => { ctx with canUnfold? := canUnfoldAtMatcher }) do whnf e partial def reduceMatcher? (e : Expr) : MetaM ReduceMatcherResult := do match e.getAppFn with | Expr.const declName declLevels _ => let some info ← getMatcherInfo? declName | return ReduceMatcherResult.notMatcher let args := e.getAppArgs let prefixSz := info.numParams + 1 + info.numDiscrs if args.size < prefixSz + info.numAlts then return ReduceMatcherResult.partialApp else let constInfo ← getConstInfo declName let f := constInfo.instantiateValueLevelParams declLevels let auxApp := mkAppN f args[0:prefixSz] let auxAppType ← inferType auxApp forallBoundedTelescope auxAppType info.numAlts fun hs _ => do let auxApp ← whnfMatcher (mkAppN auxApp hs) let auxAppFn := auxApp.getAppFn let mut i := prefixSz for h in hs do if auxAppFn == h then let result := mkAppN args[i] auxApp.getAppArgs let result := mkAppN result args[prefixSz + info.numAlts:args.size] return ReduceMatcherResult.reduced result.headBeta i := i + 1 return ReduceMatcherResult.stuck auxApp | _ => pure ReduceMatcherResult.notMatcher def project? (e : Expr) (i : Nat) : MetaM (Option Expr) := do let e ← whnf e let e := toCtorIfLit e matchConstCtor e.getAppFn (fun _ => pure none) fun ctorVal _ => let numArgs := e.getAppNumArgs let idx := ctorVal.numParams + i if idx < numArgs then return some (e.getArg! idx) else return none /-- Reduce kernel projection `Expr.proj ..` expression. -/ def reduceProj? (e : Expr) : MetaM (Option Expr) := do match e with | Expr.proj _ i c _ => project? c i | _ => return none /- Auxiliary method for reducing terms of the form `?m t_1 ... t_n` where `?m` is delayed assigned. Recall that we can only expand a delayed assignment when all holes/metavariables in the assigned value have been "filled". -/ private def whnfDelayedAssigned? (f' : Expr) (e : Expr) : MetaM (Option Expr) := do if f'.isMVar then match (← getDelayedAssignment? f'.mvarId!) with | none => return none | some { fvars := fvars, val := val, .. } => let args := e.getAppArgs if fvars.size > args.size then -- Insufficient number of argument to expand delayed assignment return none else let newVal ← instantiateMVars val if newVal.hasExprMVar then -- Delayed assignment still contains metavariables return none else let newVal := newVal.abstract fvars let result := newVal.instantiateRevRange 0 fvars.size args return mkAppRange result fvars.size args.size args else return none /-- Apply beta-reduction, zeta-reduction (i.e., unfold let local-decls), iota-reduction, expand let-expressions, expand assigned meta-variables. -/ partial def whnfCore (e : Expr) : MetaM Expr := whnfEasyCases e fun e => do trace[Meta.whnf] e match e with | Expr.const .. => pure e | Expr.letE _ _ v b _ => whnfCore $ b.instantiate1 v | Expr.app f .. => let f := f.getAppFn let f' ← whnfCore f if f'.isLambda then let revArgs := e.getAppRevArgs whnfCore <| f'.betaRev revArgs else if let some eNew ← whnfDelayedAssigned? f' e then whnfCore eNew else let e := if f == f' then e else e.updateFn f' match (← reduceMatcher? e) with | ReduceMatcherResult.reduced eNew => whnfCore eNew | ReduceMatcherResult.partialApp => pure e | ReduceMatcherResult.stuck _ => pure e | ReduceMatcherResult.notMatcher => matchConstAux f' (fun _ => return e) fun cinfo lvls => match cinfo with | ConstantInfo.recInfo rec => reduceRec rec lvls e.getAppArgs (fun _ => return e) whnfCore | ConstantInfo.quotInfo rec => reduceQuotRec rec lvls e.getAppArgs (fun _ => return e) whnfCore | c@(ConstantInfo.defnInfo _) => do if (← isAuxDef c.name) then deltaBetaDefinition c lvls e.getAppRevArgs (fun _ => return e) whnfCore else return e | _ => return e | Expr.proj .. => match (← reduceProj? e) with | some e => whnfCore e | none => return e | _ => unreachable! /-- Recall that `_sunfold` auxiliary definitions contains the markers: `markSmartUnfoldigMatch` (*) and `markSmartUnfoldigMatchAlt` (**). For example, consider the following definition ``` def r (i j : Nat) : Nat := i + match j with | Nat.zero => 1 | Nat.succ j => i + match j with | Nat.zero => 2 | Nat.succ j => r i j ``` produces the following `_sunfold` auxiliary definition with the markers ``` def r._sunfold (i j : Nat) : Nat := i + (*) match j with | Nat.zero => (**) 1 | Nat.succ j => i + (*) match j with | Nat.zero => (**) 2 | Nat.succ j => (**) r i j ``` `match` expressions marked with `markSmartUnfoldigMatch` (*) must be reduced, otherwise the resulting term is not definitionally equal to the given expression. The recursion may be interrupted as soon as the annotation `markSmartUnfoldingAlt` (**) is reached. For example, the term `r i j.succ.succ` reduces to the definitionally equal term `i + i * r i j` -/ partial def smartUnfoldingReduce? (e : Expr) : MetaM (Option Expr) := go e |>.run where go (e : Expr) : OptionT MetaM Expr := do match e with | Expr.letE n t v b _ => withLetDecl n t (← go v) fun x => do mkLetFVars #[x] (← go (b.instantiate1 x)) | Expr.lam .. => lambdaTelescope e fun xs b => do mkLambdaFVars xs (← go b) | Expr.app f a .. => mkApp (← go f) (← go a) | Expr.proj _ _ s _ => e.updateProj! (← go s) | Expr.mdata _ b _ => if let some m := smartUnfoldingMatch? e then goMatch m else e.updateMData! (← go b) | _ => return e goMatch (e : Expr) : OptionT MetaM Expr := do match (← reduceMatcher? e) with | ReduceMatcherResult.reduced e => if let some alt := smartUnfoldingMatchAlt? e then return alt else go e | ReduceMatcherResult.stuck e' => let mvarId ← getStuckMVar? e' /- Try to "unstuck" by resolving pending TC problems -/ if (← Meta.synthPending mvarId) then goMatch e else failure | _ => failure mutual /-- Auxiliary method for unfolding a class projection. -/ partial def unfoldProjInst? (e : Expr) : MetaM (Option Expr) := do match e.getAppFn with | Expr.const declName .. => match (← getProjectionFnInfo? declName) with | some { fromClass := true, .. } => match (← withDefault <| unfoldDefinition? e) with | none => return none | some e => match (← withReducibleAndInstances <| reduceProj? e.getAppFn) with | none => return none | some r => return mkAppN r e.getAppArgs |>.headBeta | _ => return none | _ => return none /-- Auxiliary method for unfolding a class projection. when transparency is set to `TransparencyMode.instances`. Recall that class instance projections are not marked with `[reducible]` because we want them to be in "reducible canonical form". -/ private partial def unfoldProjInstWhenIntances? (e : Expr) : MetaM (Option Expr) := do if (← getTransparency) != TransparencyMode.instances then return none else unfoldProjInst? e /-- Unfold definition using "smart unfolding" if possible. -/ partial def unfoldDefinition? (e : Expr) : MetaM (Option Expr) := match e with | Expr.app f _ _ => matchConstAux f.getAppFn (fun _ => unfoldProjInstWhenIntances? e) fun fInfo fLvls => do if fInfo.levelParams.length != fLvls.length then return none else let unfoldDefault (_ : Unit) : MetaM (Option Expr) := if fInfo.hasValue then deltaBetaDefinition fInfo fLvls e.getAppRevArgs (fun _ => pure none) (fun e => pure (some e)) else return none if smartUnfolding.get (← getOptions) then match (← getConstNoEx? (mkSmartUnfoldingNameFor fInfo.name)) with | some fAuxInfo@(ConstantInfo.defnInfo _) => deltaBetaDefinition fAuxInfo fLvls e.getAppRevArgs (fun _ => pure none) fun e₁ => smartUnfoldingReduce? e₁ | _ => if (← getMatcherInfo? fInfo.name).isSome then -- Recall that `whnfCore` tries to reduce "matcher" applications. return none else unfoldDefault () else unfoldDefault () | Expr.const declName lvls _ => do if smartUnfolding.get (← getOptions) && (← getEnv).contains (mkSmartUnfoldingNameFor declName) then return none else let (some (cinfo@(ConstantInfo.defnInfo _))) ← getConstNoEx? declName | pure none deltaDefinition cinfo lvls (fun _ => pure none) (fun e => pure (some e)) | _ => return none end def unfoldDefinition (e : Expr) : MetaM Expr := do let some e ← unfoldDefinition? e | throwError "failed to unfold definition{indentExpr e}" return e @[specialize] partial def whnfHeadPred (e : Expr) (pred : Expr β†’ MetaM Bool) : MetaM Expr := whnfEasyCases e fun e => do let e ← whnfCore e if (← pred e) then match (← unfoldDefinition? e) with | some e => whnfHeadPred e pred | none => return e else return e def whnfUntil (e : Expr) (declName : Name) : MetaM (Option Expr) := do let e ← whnfHeadPred e (fun e => return !e.isAppOf declName) if e.isAppOf declName then return e else return none /-- Try to reduce matcher/recursor/quot applications. We say they are all "morally" recursor applications. -/ def reduceRecMatcher? (e : Expr) : MetaM (Option Expr) := do if !e.isApp then return none else match (← reduceMatcher? e) with | ReduceMatcherResult.reduced e => return e | _ => matchConstAux e.getAppFn (fun _ => pure none) fun cinfo lvls => do match cinfo with | ConstantInfo.recInfo Β«recΒ» => reduceRec Β«recΒ» lvls e.getAppArgs (fun _ => pure none) (fun e => pure (some e)) | ConstantInfo.quotInfo Β«recΒ» => reduceQuotRec Β«recΒ» lvls e.getAppArgs (fun _ => pure none) (fun e => pure (some e)) | c@(ConstantInfo.defnInfo _) => if (← isAuxDef c.name) then deltaBetaDefinition c lvls e.getAppRevArgs (fun _ => pure none) (fun e => pure (some e)) else return none | _ => return none unsafe def reduceBoolNativeUnsafe (constName : Name) : MetaM Bool := evalConstCheck Bool `Bool constName unsafe def reduceNatNativeUnsafe (constName : Name) : MetaM Nat := evalConstCheck Nat `Nat constName @[implementedBy reduceBoolNativeUnsafe] constant reduceBoolNative (constName : Name) : MetaM Bool @[implementedBy reduceNatNativeUnsafe] constant reduceNatNative (constName : Name) : MetaM Nat def reduceNative? (e : Expr) : MetaM (Option Expr) := match e with | Expr.app (Expr.const fName _ _) (Expr.const argName _ _) _ => if fName == ``Lean.reduceBool then do return toExpr (← reduceBoolNative argName) else if fName == ``Lean.reduceNat then do return toExpr (← reduceNatNative argName) else return none | _ => return none @[inline] def withNatValue {Ξ±} (a : Expr) (k : Nat β†’ MetaM (Option Ξ±)) : MetaM (Option Ξ±) := do let a ← whnf a match a with | Expr.const `Nat.zero _ _ => k 0 | Expr.lit (Literal.natVal v) _ => k v | _ => return none def reduceUnaryNatOp (f : Nat β†’ Nat) (a : Expr) : MetaM (Option Expr) := withNatValue a fun a => return mkRawNatLit <| f a def reduceBinNatOp (f : Nat β†’ Nat β†’ Nat) (a b : Expr) : MetaM (Option Expr) := withNatValue a fun a => withNatValue b fun b => do trace[Meta.isDefEq.whnf.reduceBinOp] "{a} op {b}" return mkRawNatLit <| f a b def reduceBinNatPred (f : Nat β†’ Nat β†’ Bool) (a b : Expr) : MetaM (Option Expr) := do withNatValue a fun a => withNatValue b fun b => return toExpr <| f a b def reduceNat? (e : Expr) : MetaM (Option Expr) := if e.hasFVar || e.hasMVar then return none else match e with | Expr.app (Expr.const fn _ _) a _ => if fn == ``Nat.succ then reduceUnaryNatOp Nat.succ a else return none | Expr.app (Expr.app (Expr.const fn _ _) a1 _) a2 _ => if fn == ``Nat.add then reduceBinNatOp Nat.add a1 a2 else if fn == ``Nat.sub then reduceBinNatOp Nat.sub a1 a2 else if fn == ``Nat.mul then reduceBinNatOp Nat.mul a1 a2 else if fn == ``Nat.div then reduceBinNatOp Nat.div a1 a2 else if fn == ``Nat.mod then reduceBinNatOp Nat.mod a1 a2 else if fn == ``Nat.beq then reduceBinNatPred Nat.beq a1 a2 else if fn == ``Nat.ble then reduceBinNatPred Nat.ble a1 a2 else return none | _ => return none @[inline] private def useWHNFCache (e : Expr) : MetaM Bool := do -- We cache only closed terms without expr metavars. -- Potential refinement: cache if `e` is not stuck at a metavariable if e.hasFVar || e.hasExprMVar || (← read).canUnfold?.isSome then return false else match (← getConfig).transparency with | TransparencyMode.default => true | TransparencyMode.all => true | _ => false @[inline] private def cached? (useCache : Bool) (e : Expr) : MetaM (Option Expr) := do if useCache then match (← getConfig).transparency with | TransparencyMode.default => return (← get).cache.whnfDefault.find? e | TransparencyMode.all => return (← get).cache.whnfAll.find? e | _ => unreachable! else return none private def cache (useCache : Bool) (e r : Expr) : MetaM Expr := do if useCache then match (← getConfig).transparency with | TransparencyMode.default => modify fun s => { s with cache.whnfDefault := s.cache.whnfDefault.insert e r } | TransparencyMode.all => modify fun s => { s with cache.whnfAll := s.cache.whnfAll.insert e r } | _ => unreachable! return r @[export lean_whnf] partial def whnfImp (e : Expr) : MetaM Expr := withIncRecDepth <| whnfEasyCases e fun e => do checkMaxHeartbeats "whnf" let useCache ← useWHNFCache e match (← cached? useCache e) with | some e' => pure e' | none => let e' ← whnfCore e match (← reduceNat? e') with | some v => cache useCache e v | none => match (← reduceNative? e') with | some v => cache useCache e v | none => match (← unfoldDefinition? e') with | some e => whnfImp e | none => cache useCache e e' /-- If `e` is a projection function that satisfies `p`, then reduce it -/ def reduceProjOf? (e : Expr) (p : Name β†’ Bool) : MetaM (Option Expr) := do if !e.isApp then pure none else match e.getAppFn with | Expr.const name .. => do let env ← getEnv match env.getProjectionStructureName? name with | some structName => if p structName then Meta.unfoldDefinition? e else pure none | none => pure none | _ => pure none builtin_initialize registerTraceClass `Meta.whnf registerTraceClass `Meta.isDefEq.whnf.reduceBinOp end Lean.Meta
c145d8ab17db2a984cf2427ac8ad471ba52ea595
fffbc47930dc6615e66ece42324ce57a21d5b64b
/src/category_theory/instances/Top/adjunctions.lean
7f46e7496c634f035b9310621550d4231f701303
[ "Apache-2.0" ]
permissive
skbaek/mathlib
3caae8ae413c66862293a95fd2fbada3647b1228
f25340175631cdc85ad768a262433f968d0d6450
refs/heads/master
1,588,130,123,636
1,558,287,609,000
1,558,287,609,000
160,935,713
0
0
Apache-2.0
1,544,271,146,000
1,544,271,146,000
null
UTF-8
Lean
false
false
929
lean
-- Copyright (c) 2017 Scott Morrison. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Patrick Massot, Mario Carneiro import category_theory.instances.Top.basic import category_theory.adjunction universe u open category_theory open category_theory.instances namespace category_theory.instances.Top def adj₁ : discrete ⊣ forget := { hom_equiv := Ξ» X Y, { to_fun := Ξ» f, f, inv_fun := Ξ» f, ⟨f, continuous_top⟩, left_inv := by tidy, right_inv := by tidy }, unit := { app := Ξ» X, id }, counit := { app := Ξ» X, ⟨id, continuous_top⟩ } } def adjβ‚‚ : forget ⊣ trivial := { hom_equiv := Ξ» X Y, { to_fun := Ξ» f, ⟨f, continuous_bot⟩, inv_fun := Ξ» f, f, left_inv := by tidy, right_inv := by tidy }, unit := { app := Ξ» X, ⟨id, continuous_bot⟩ }, counit := { app := Ξ» X, id } } end category_theory.instances.Top
5d3b439a9d93e8ceb27cc1247073bb79e8f447e1
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/set_like/basic.lean
b83d85eaf1033b41ed705d69619074d52bb0c0c5
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
6,406
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 data.set.basic import tactic.monotonicity.basic /-! # Typeclass for types with a set-like extensionality property > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. The `has_mem` typeclass is used to let terms of a type have elements. Many instances of `has_mem` have a set-like extensionality property: things are equal iff they have the same elements. The `set_like` typeclass provides a unified interface to define a `has_mem` that is extensional in this way. The main use of `set_like` is for algebraic subobjects (such as `submonoid` and `submodule`), whose non-proof data consists only of a carrier set. In such a situation, the projection to the carrier set is injective. In general, a type `A` is `set_like` with elements of type `B` if it has an injective map to `set B`. This module provides standard boilerplate for every `set_like`: a `coe_sort`, a `coe` to set, a `partial_order`, and various extensionality and simp lemmas. A typical subobject should be declared as: ``` structure my_subobject (X : Type*) [object_typeclass X] := (carrier : set X) (op_mem' : βˆ€ {x : X}, x ∈ carrier β†’ sorry ∈ carrier) namespace my_subobject variables {X : Type*} [object_typeclass X] {x : X} instance : set_like (my_subobject X) X := ⟨my_subobject.carrier, Ξ» p q h, by cases p; cases q; congr'⟩ @[simp] lemma mem_carrier {p : my_subobject X} : x ∈ p.carrier ↔ x ∈ (p : set X) := iff.rfl @[ext] theorem ext {p q : my_subobject X} (h : βˆ€ x, x ∈ p ↔ x ∈ q) : p = q := set_like.ext h /-- Copy of a `my_subobject` with a new `carrier` equal to the old one. Useful to fix definitional equalities. See Note [range copy pattern]. -/ protected def copy (p : my_subobject X) (s : set X) (hs : s = ↑p) : my_subobject X := { carrier := s, op_mem' := hs.symm β–Έ p.op_mem' } @[simp] lemma coe_copy (p : my_subobject X) (s : set X) (hs : s = ↑p) : (p.copy s hs : set X) = s := rfl lemma copy_eq (p : my_subobject X) (s : set X) (hs : s = ↑p) : p.copy s hs = p := set_like.coe_injective hs end my_subobject ``` An alternative to `set_like` could have been an extensional `has_mem` typeclass: ``` class has_ext_mem (Ξ± : out_param $ Type u) (Ξ² : Type v) extends has_mem Ξ± Ξ² := (ext_iff : βˆ€ {s t : Ξ²}, s = t ↔ βˆ€ (x : Ξ±), x ∈ s ↔ x ∈ t) ``` While this is equivalent, `set_like` conveniently uses a carrier set projection directly. ## Tags subobjects -/ /-- A class to indicate that there is a canonical injection between `A` and `set B`. This has the effect of giving terms of `A` elements of type `B` (through a `has_mem` instance) and a compatible coercion to `Type*` as a subtype. Note: if `set_like.coe` is a projection, implementers should create a simp lemma such as ``` @[simp] lemma mem_carrier {p : my_subobject X} : x ∈ p.carrier ↔ x ∈ (p : set X) := iff.rfl ``` to normalize terms. If you declare an unbundled subclass of `set_like`, for example: ``` class mul_mem_class (S : Type*) (M : Type*) [has_mul M] [set_like S M] where ... ``` Then you should *not* repeat the `out_param` declaration, `set_like` will supply the value instead. This ensures in Lean 4 your subclass will not have issues with synthesis of the `[has_mul M]` parameter starting before the value of `M` is known. -/ @[protect_proj] class set_like (A : Type*) (B : out_param $ Type*) := (coe : A β†’ set B) (coe_injective' : function.injective coe) namespace set_like variables {A : Type*} {B : Type*} [i : set_like A B] include i instance : has_coe_t A (set B) := ⟨set_like.coe⟩ @[priority 100] instance : has_mem B A := ⟨λ x p, x ∈ (p : set B)⟩ -- `dangerous_instance` does not know that `B` is used only as an `out_param` @[nolint dangerous_instance, priority 100] instance : has_coe_to_sort A Type* := ⟨λ p, {x : B // x ∈ p}⟩ variables (p q : A) @[simp, norm_cast] theorem coe_sort_coe : ((p : set B) : Type*) = p := rfl variables {p q} protected theorem Β«existsΒ» {q : p β†’ Prop} : (βˆƒ x, q x) ↔ (βˆƒ x ∈ p, q ⟨x, β€Ή_β€ΊβŸ©) := set_coe.exists protected theorem Β«forallΒ» {q : p β†’ Prop} : (βˆ€ x, q x) ↔ (βˆ€ x ∈ p, q ⟨x, β€Ή_β€ΊβŸ©) := set_coe.forall theorem coe_injective : function.injective (coe : A β†’ set B) := Ξ» x y h, set_like.coe_injective' h @[simp, norm_cast] theorem coe_set_eq : (p : set B) = q ↔ p = q := coe_injective.eq_iff theorem ext' (h : (p : set B) = q) : p = q := coe_injective h theorem ext'_iff : p = q ↔ (p : set B) = q := coe_set_eq.symm /-- Note: implementers of `set_like` must copy this lemma in order to tag it with `@[ext]`. -/ theorem ext (h : βˆ€ x, x ∈ p ↔ x ∈ q) : p = q := coe_injective $ set.ext h theorem ext_iff : p = q ↔ (βˆ€ x, x ∈ p ↔ x ∈ q) := coe_injective.eq_iff.symm.trans set.ext_iff @[simp] theorem mem_coe {x : B} : x ∈ (p : set B) ↔ x ∈ p := iff.rfl @[simp, norm_cast] lemma coe_eq_coe {x y : p} : (x : B) = y ↔ x = y := subtype.ext_iff_val.symm @[simp, norm_cast] lemma coe_mk (x : B) (hx : x ∈ p) : ((⟨x, hx⟩ : p) : B) = x := rfl @[simp] lemma coe_mem (x : p) : (x : B) ∈ p := x.2 @[simp] protected lemma eta (x : p) (hx : (x : B) ∈ p) : (⟨x, hx⟩ : p) = x := subtype.eta x hx -- `dangerous_instance` does not know that `B` is used only as an `out_param` @[nolint dangerous_instance, priority 100] instance : partial_order A := { le := Ξ» H K, βˆ€ ⦃x⦄, x ∈ H β†’ x ∈ K, .. partial_order.lift (coe : A β†’ set B) coe_injective } lemma le_def {S T : A} : S ≀ T ↔ βˆ€ ⦃x : B⦄, x ∈ S β†’ x ∈ T := iff.rfl @[simp, norm_cast] lemma coe_subset_coe {S T : A} : (S : set B) βŠ† T ↔ S ≀ T := iff.rfl @[mono] lemma coe_mono : monotone (coe : A β†’ set B) := Ξ» a b, coe_subset_coe.mpr @[simp, norm_cast] lemma coe_ssubset_coe {S T : A} : (S : set B) βŠ‚ T ↔ S < T := iff.rfl @[mono] lemma coe_strict_mono : strict_mono (coe : A β†’ set B) := Ξ» a b, coe_ssubset_coe.mpr lemma not_le_iff_exists : Β¬(p ≀ q) ↔ βˆƒ x ∈ p, x βˆ‰ q := set.not_subset lemma exists_of_lt : p < q β†’ βˆƒ x ∈ q, x βˆ‰ p := set.exists_of_ssubset lemma lt_iff_le_and_exists : p < q ↔ p ≀ q ∧ βˆƒ x ∈ q, x βˆ‰ p := by rw [lt_iff_le_not_le, not_le_iff_exists] end set_like
677ee0882fe5e684c53ad9c0d1bbb6dc2d54f7b7
64874bd1010548c7f5a6e3e8902efa63baaff785
/tests/lean/run/no_confusion_bug.lean
64b078a6b6786b5ae7ad0b2d4cf21ad2b16eb749
[ "Apache-2.0" ]
permissive
tjiaqi/lean
4634d729795c164664d10d093f3545287c76628f
d0ce4cf62f4246b0600c07e074d86e51f2195e30
refs/heads/master
1,622,323,796,480
1,422,643,069,000
1,422,643,069,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
352
lean
import data.nat.basic open nat inductive fin : nat β†’ Type := fz : Ξ  {n : nat}, fin (succ n), fs : Ξ  {n : nat}, fin n β†’ fin (succ n) namespace fin inductive le : βˆ€ {n : nat}, fin n β†’ fin n β†’ Prop := lez : βˆ€ {n : nat} (j : fin (succ n)), le fz j, les : βˆ€ {n : nat} {i j : fin n}, le i j β†’ le (fs i) (fs j) end fin
a7509c49dae6423ed0938bf59482ba3773faddf7
63abd62053d479eae5abf4951554e1064a4c45b4
/src/measure_theory/measurable_space.lean
51c6c7df85584e941bf1287fa23f5b81d67dd78b
[ "Apache-2.0" ]
permissive
Lix0120/mathlib
0020745240315ed0e517cbf32e738d8f9811dd80
e14c37827456fc6707f31b4d1d16f1f3a3205e91
refs/heads/master
1,673,102,855,024
1,604,151,044,000
1,604,151,044,000
308,930,245
0
0
Apache-2.0
1,604,164,710,000
1,604,163,547,000
null
UTF-8
Lean
false
false
54,509
lean
/- Copyright (c) 2017 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import data.set.disjointed import data.set.countable import data.indicator_function import data.equiv.encodable.lattice import order.filter.basic /-! # Measurable spaces and measurable functions This file defines measurable spaces and the functions and isomorphisms between them. A measurable space is a set equipped with a Οƒ-algebra, a collection of subsets closed under complementation and countable union. A function between measurable spaces is measurable if the preimage of each measurable subset is measurable. Οƒ-algebras on a fixed set `Ξ±` form a complete lattice. Here we order Οƒ-algebras by writing `m₁ ≀ mβ‚‚` if every set which is `m₁`-measurable is also `mβ‚‚`-measurable (that is, `m₁` is a subset of `mβ‚‚`). In particular, any collection of subsets of `Ξ±` generates a smallest Οƒ-algebra which contains all of them. A function `f : Ξ± β†’ Ξ²` induces a Galois connection between the lattices of Οƒ-algebras on `Ξ±` and `Ξ²`. A measurable equivalence between measurable spaces is an equivalence which respects the Οƒ-algebras, that is, for which both directions of the equivalence are measurable functions. We say that a filter `f` is measurably generated if every set `s ∈ f` includes a measurable set `t ∈ f`. This property is useful, e.g., to extract a measurable witness of `filter.eventually`. ## Main statements The main theorem of this file is Dynkin's Ο€-Ξ» theorem, which appears here as an induction principle `induction_on_inter`. Suppose `s` is a collection of subsets of `Ξ±` such that the intersection of two members of `s` belongs to `s` whenever it is nonempty. Let `m` be the Οƒ-algebra generated by `s`. In order to check that a predicate `C` holds on every member of `m`, it suffices to check that `C` holds on the members of `s` and that `C` is preserved by complementation and *disjoint* countable unions. ## Implementation notes Measurability of a function `f : Ξ± β†’ Ξ²` between measurable spaces is defined in terms of the Galois connection induced by f. ## References * <https://en.wikipedia.org/wiki/Measurable_space> * <https://en.wikipedia.org/wiki/Sigma-algebra> * <https://en.wikipedia.org/wiki/Dynkin_system> ## Tags measurable space, measurable function, dynkin system -/ open set encodable function open_locale classical filter variables {Ξ± Ξ² Ξ³ Ξ΄ Ξ΄' : Type*} {ΞΉ : Sort*} {s t u : set Ξ±} /-- A measurable space is a space equipped with a Οƒ-algebra. -/ structure measurable_space (Ξ± : Type*) := (is_measurable' : set Ξ± β†’ Prop) (is_measurable_empty : is_measurable' βˆ…) (is_measurable_compl : βˆ€ s, is_measurable' s β†’ is_measurable' sᢜ) (is_measurable_Union : βˆ€ f : β„• β†’ set Ξ±, (βˆ€ i, is_measurable' (f i)) β†’ is_measurable' (⋃ i, f i)) attribute [class] measurable_space section variable [measurable_space Ξ±] /-- `is_measurable s` means that `s` is measurable (in the ambient measure space on `Ξ±`) -/ def is_measurable : set Ξ± β†’ Prop := β€Ήmeasurable_space Ξ±β€Ί.is_measurable' @[simp] lemma is_measurable.empty : is_measurable (βˆ… : set Ξ±) := β€Ήmeasurable_space Ξ±β€Ί.is_measurable_empty lemma is_measurable.compl : is_measurable s β†’ is_measurable sᢜ := β€Ήmeasurable_space Ξ±β€Ί.is_measurable_compl s lemma is_measurable.of_compl (h : is_measurable sᢜ) : is_measurable s := s.compl_compl β–Έ h.compl @[simp] lemma is_measurable.compl_iff : is_measurable sᢜ ↔ is_measurable s := ⟨is_measurable.of_compl, is_measurable.compl⟩ @[simp] lemma is_measurable.univ : is_measurable (univ : set Ξ±) := by simpa using (@is_measurable.empty Ξ± _).compl lemma subsingleton.is_measurable [subsingleton Ξ±] {s : set Ξ±} : is_measurable s := subsingleton.set_cases is_measurable.empty is_measurable.univ s lemma is_measurable.congr {s t : set Ξ±} (hs : is_measurable s) (h : s = t) : is_measurable t := by rwa ← h lemma is_measurable.bUnion_decode2 [encodable Ξ²] ⦃f : Ξ² β†’ set α⦄ (h : βˆ€ b, is_measurable (f b)) (n : β„•) : is_measurable (⋃ b ∈ decode2 Ξ² n, f b) := encodable.Union_decode2_cases is_measurable.empty h lemma is_measurable.Union [encodable Ξ²] ⦃f : Ξ² β†’ set α⦄ (h : βˆ€ b, is_measurable (f b)) : is_measurable (⋃ b, f b) := begin rw ← encodable.Union_decode2, exact β€Ήmeasurable_space Ξ±β€Ί.is_measurable_Union _ (is_measurable.bUnion_decode2 h) end lemma is_measurable.bUnion {f : Ξ² β†’ set Ξ±} {s : set Ξ²} (hs : countable s) (h : βˆ€ b ∈ s, is_measurable (f b)) : is_measurable (⋃ b ∈ s, f b) := begin rw bUnion_eq_Union, haveI := hs.to_encodable, exact is_measurable.Union (by simpa using h) end lemma set.finite.is_measurable_bUnion {f : Ξ² β†’ set Ξ±} {s : set Ξ²} (hs : finite s) (h : βˆ€ b ∈ s, is_measurable (f b)) : is_measurable (⋃ b ∈ s, f b) := is_measurable.bUnion hs.countable h lemma finset.is_measurable_bUnion {f : Ξ² β†’ set Ξ±} (s : finset Ξ²) (h : βˆ€ b ∈ s, is_measurable (f b)) : is_measurable (⋃ b ∈ s, f b) := s.finite_to_set.is_measurable_bUnion h lemma is_measurable.sUnion {s : set (set Ξ±)} (hs : countable s) (h : βˆ€ t ∈ s, is_measurable t) : is_measurable (⋃₀ s) := by { rw sUnion_eq_bUnion, exact is_measurable.bUnion hs h } lemma set.finite.is_measurable_sUnion {s : set (set Ξ±)} (hs : finite s) (h : βˆ€ t ∈ s, is_measurable t) : is_measurable (⋃₀ s) := is_measurable.sUnion hs.countable h lemma is_measurable.Union_Prop {p : Prop} {f : p β†’ set Ξ±} (hf : βˆ€ b, is_measurable (f b)) : is_measurable (⋃ b, f b) := by { by_cases p; simp [h, hf, is_measurable.empty] } lemma is_measurable.Inter [encodable Ξ²] {f : Ξ² β†’ set Ξ±} (h : βˆ€ b, is_measurable (f b)) : is_measurable (β‹‚ b, f b) := is_measurable.compl_iff.1 $ by { rw compl_Inter, exact is_measurable.Union (Ξ» b, (h b).compl) } lemma is_measurable.bInter {f : Ξ² β†’ set Ξ±} {s : set Ξ²} (hs : countable s) (h : βˆ€ b ∈ s, is_measurable (f b)) : is_measurable (β‹‚ b ∈ s, f b) := is_measurable.compl_iff.1 $ by { rw compl_bInter, exact is_measurable.bUnion hs (Ξ» b hb, (h b hb).compl) } lemma set.finite.is_measurable_bInter {f : Ξ² β†’ set Ξ±} {s : set Ξ²} (hs : finite s) (h : βˆ€ b ∈ s, is_measurable (f b)) : is_measurable (β‹‚ b ∈ s, f b) := is_measurable.bInter hs.countable h lemma finset.is_measurable_bInter {f : Ξ² β†’ set Ξ±} (s : finset Ξ²) (h : βˆ€ b ∈ s, is_measurable (f b)) : is_measurable (β‹‚ b ∈ s, f b) := s.finite_to_set.is_measurable_bInter h lemma is_measurable.sInter {s : set (set Ξ±)} (hs : countable s) (h : βˆ€ t ∈ s, is_measurable t) : is_measurable (β‹‚β‚€ s) := by { rw sInter_eq_bInter, exact is_measurable.bInter hs h } lemma set.finite.is_measurable_sInter {s : set (set Ξ±)} (hs : finite s) (h : βˆ€ t ∈ s, is_measurable t) : is_measurable (β‹‚β‚€ s) := is_measurable.sInter hs.countable h lemma is_measurable.Inter_Prop {p : Prop} {f : p β†’ set Ξ±} (hf : βˆ€ b, is_measurable (f b)) : is_measurable (β‹‚ b, f b) := by { by_cases p; simp [h, hf, is_measurable.univ] } @[simp] lemma is_measurable.union {s₁ sβ‚‚ : set Ξ±} (h₁ : is_measurable s₁) (hβ‚‚ : is_measurable sβ‚‚) : is_measurable (s₁ βˆͺ sβ‚‚) := by { rw union_eq_Union, exact is_measurable.Union (bool.forall_bool.2 ⟨hβ‚‚, hβ‚βŸ©) } @[simp] lemma is_measurable.inter {s₁ sβ‚‚ : set Ξ±} (h₁ : is_measurable s₁) (hβ‚‚ : is_measurable sβ‚‚) : is_measurable (s₁ ∩ sβ‚‚) := by { rw inter_eq_compl_compl_union_compl, exact (h₁.compl.union hβ‚‚.compl).compl } @[simp] lemma is_measurable.diff {s₁ sβ‚‚ : set Ξ±} (h₁ : is_measurable s₁) (hβ‚‚ : is_measurable sβ‚‚) : is_measurable (s₁ \ sβ‚‚) := h₁.inter hβ‚‚.compl @[simp] lemma is_measurable.disjointed {f : β„• β†’ set Ξ±} (h : βˆ€ i, is_measurable (f i)) (n) : is_measurable (disjointed f n) := disjointed_induct (h n) (assume t i ht, is_measurable.diff ht $ h _) @[simp] lemma is_measurable.const (p : Prop) : is_measurable {a : Ξ± | p} := by { by_cases p; simp [h, is_measurable.empty]; apply is_measurable.univ } end @[ext] lemma measurable_space.ext : βˆ€ {m₁ mβ‚‚ : measurable_space Ξ±}, (βˆ€ s : set Ξ±, m₁.is_measurable' s ↔ mβ‚‚.is_measurable' s) β†’ m₁ = mβ‚‚ | ⟨s₁, _, _, _⟩ ⟨sβ‚‚, _, _, _⟩ h := have s₁ = sβ‚‚, from funext $ assume x, propext $ h x, by subst this @[ext] lemma measurable_space.ext_iff {m₁ mβ‚‚ : measurable_space Ξ±} : m₁ = mβ‚‚ ↔ (βˆ€ s : set Ξ±, m₁.is_measurable' s ↔ mβ‚‚.is_measurable' s) := ⟨by { unfreezingI {rintro rfl}, intro s, refl }, measurable_space.ext⟩ /-- A typeclass mixin for `measurable_space`s such that each singleton is measurable. -/ class measurable_singleton_class (Ξ± : Type*) [measurable_space Ξ±] : Prop := (is_measurable_singleton : βˆ€ x, is_measurable ({x} : set Ξ±)) export measurable_singleton_class (is_measurable_singleton) attribute [simp] is_measurable_singleton section measurable_singleton_class variables [measurable_space Ξ±] [measurable_singleton_class Ξ±] lemma is_measurable_eq {a : Ξ±} : is_measurable {x | x = a} := is_measurable_singleton a lemma is_measurable.insert {s : set Ξ±} (hs : is_measurable s) (a : Ξ±) : is_measurable (insert a s) := (is_measurable_singleton a).union hs @[simp] lemma is_measurable_insert {a : Ξ±} {s : set Ξ±} : is_measurable (insert a s) ↔ is_measurable s := ⟨λ h, if ha : a ∈ s then by rwa ← insert_eq_of_mem ha else insert_diff_self_of_not_mem ha β–Έ h.diff (is_measurable_singleton _), Ξ» h, h.insert a⟩ lemma set.finite.is_measurable {s : set Ξ±} (hs : finite s) : is_measurable s := finite.induction_on hs is_measurable.empty $ Ξ» a s ha hsf hsm, hsm.insert _ protected lemma finset.is_measurable (s : finset Ξ±) : is_measurable (↑s : set Ξ±) := s.finite_to_set.is_measurable end measurable_singleton_class namespace measurable_space section complete_lattice instance : partial_order (measurable_space Ξ±) := { le := Ξ» m₁ mβ‚‚, m₁.is_measurable' ≀ mβ‚‚.is_measurable', le_refl := assume a b, le_refl _, le_trans := assume a b c, le_trans, le_antisymm := assume a b h₁ hβ‚‚, measurable_space.ext $ assume s, ⟨h₁ s, hβ‚‚ s⟩ } /-- The smallest Οƒ-algebra containing a collection `s` of basic sets -/ inductive generate_measurable (s : set (set Ξ±)) : set Ξ± β†’ Prop | basic : βˆ€ u ∈ s, generate_measurable u | empty : generate_measurable βˆ… | compl : βˆ€ s, generate_measurable s β†’ generate_measurable sᢜ | union : βˆ€ f : β„• β†’ set Ξ±, (βˆ€ n, generate_measurable (f n)) β†’ generate_measurable (⋃ i, f i) /-- Construct the smallest measure space containing a collection of basic sets -/ def generate_from (s : set (set Ξ±)) : measurable_space Ξ± := { is_measurable' := generate_measurable s, is_measurable_empty := generate_measurable.empty, is_measurable_compl := generate_measurable.compl, is_measurable_Union := generate_measurable.union } lemma is_measurable_generate_from {s : set (set Ξ±)} {t : set Ξ±} (ht : t ∈ s) : (generate_from s).is_measurable' t := generate_measurable.basic t ht lemma generate_from_le {s : set (set Ξ±)} {m : measurable_space Ξ±} (h : βˆ€ t ∈ s, m.is_measurable' t) : generate_from s ≀ m := assume t (ht : generate_measurable s t), ht.rec_on h (is_measurable_empty m) (assume s _ hs, is_measurable_compl m s hs) (assume f _ hf, is_measurable_Union m f hf) lemma generate_from_le_iff {s : set (set Ξ±)} (m : measurable_space Ξ±) : generate_from s ≀ m ↔ s βŠ† {t | m.is_measurable' t} := iff.intro (assume h u hu, h _ $ is_measurable_generate_from hu) (assume h, generate_from_le h) @[simp] lemma generate_from_is_measurable [measurable_space Ξ±] : generate_from {s : set Ξ± | is_measurable s} = β€Ή_β€Ί := le_antisymm (generate_from_le $ Ξ» _, id) $ Ξ» s, is_measurable_generate_from /-- If `g` is a collection of subsets of `Ξ±` such that the `Οƒ`-algebra generated from `g` contains the same sets as `g`, then `g` was already a `Οƒ`-algebra. -/ protected def mk_of_closure (g : set (set Ξ±)) (hg : {t | (generate_from g).is_measurable' t} = g) : measurable_space Ξ± := { is_measurable' := Ξ» s, s ∈ g, is_measurable_empty := hg β–Έ is_measurable_empty _, is_measurable_compl := hg β–Έ is_measurable_compl _, is_measurable_Union := hg β–Έ is_measurable_Union _ } lemma mk_of_closure_sets {s : set (set Ξ±)} {hs : {t | (generate_from s).is_measurable' t} = s} : measurable_space.mk_of_closure s hs = generate_from s := measurable_space.ext $ assume t, show t ∈ s ↔ _, by { conv_lhs { rw [← hs] }, refl } /-- We get a Galois insertion between `Οƒ`-algebras on `Ξ±` and `set (set Ξ±)` by using `generate_from` on one side and the collection of measurable sets on the other side. -/ def gi_generate_from : galois_insertion (@generate_from Ξ±) (Ξ» m, {t | @is_measurable Ξ± m t}) := { gc := assume s, generate_from_le_iff, le_l_u := assume m s, is_measurable_generate_from, choice := Ξ» g hg, measurable_space.mk_of_closure g $ le_antisymm hg $ (generate_from_le_iff _).1 le_rfl, choice_eq := assume g hg, mk_of_closure_sets } instance : complete_lattice (measurable_space Ξ±) := gi_generate_from.lift_complete_lattice instance : inhabited (measurable_space Ξ±) := ⟨⊀⟩ lemma is_measurable_bot_iff {s : set Ξ±} : @is_measurable Ξ± βŠ₯ s ↔ (s = βˆ… ∨ s = univ) := let b : measurable_space Ξ± := { is_measurable' := Ξ» s, s = βˆ… ∨ s = univ, is_measurable_empty := or.inl rfl, is_measurable_compl := by simp [or_imp_distrib] {contextual := tt}, is_measurable_Union := assume f hf, classical.by_cases (assume h : βˆƒi, f i = univ, let ⟨i, hi⟩ := h in or.inr $ eq_univ_of_univ_subset $ hi β–Έ le_supr f i) (assume h : Β¬ βˆƒi, f i = univ, or.inl $ eq_empty_of_subset_empty $ Union_subset $ assume i, (hf i).elim (by simp {contextual := tt}) (assume hi, false.elim $ h ⟨i, hi⟩)) } in have b = βŠ₯, from bot_unique $ assume s hs, hs.elim (Ξ» s, s.symm β–Έ @is_measurable_empty _ βŠ₯) (Ξ» s, s.symm β–Έ @is_measurable.univ _ βŠ₯), this β–Έ iff.rfl @[simp] theorem is_measurable_top {s : set Ξ±} : @is_measurable _ ⊀ s := trivial @[simp] theorem is_measurable_inf {m₁ mβ‚‚ : measurable_space Ξ±} {s : set Ξ±} : @is_measurable _ (m₁ βŠ“ mβ‚‚) s ↔ @is_measurable _ m₁ s ∧ @is_measurable _ mβ‚‚ s := iff.rfl @[simp] theorem is_measurable_Inf {ms : set (measurable_space Ξ±)} {s : set Ξ±} : @is_measurable _ (Inf ms) s ↔ βˆ€ m ∈ ms, @is_measurable _ m s := show s ∈ (β‹‚ m ∈ ms, {t | @is_measurable _ m t }) ↔ _, by simp @[simp] theorem is_measurable_infi {ΞΉ} {m : ΞΉ β†’ measurable_space Ξ±} {s : set Ξ±} : @is_measurable _ (infi m) s ↔ βˆ€ i, @is_measurable _ (m i) s := show s ∈ (Ξ» m, {s | @is_measurable _ m s }) (infi m) ↔ _, by { rw (@gi_generate_from Ξ±).gc.u_infi, simp } theorem is_measurable_sup {m₁ mβ‚‚ : measurable_space Ξ±} {s : set Ξ±} : @is_measurable _ (m₁ βŠ” mβ‚‚) s ↔ generate_measurable (m₁.is_measurable' βˆͺ mβ‚‚.is_measurable') s := iff.refl _ theorem is_measurable_Sup {ms : set (measurable_space Ξ±)} {s : set Ξ±} : @is_measurable _ (Sup ms) s ↔ generate_measurable (⋃₀ (measurable_space.is_measurable' '' ms)) s := begin change @is_measurable' _ (generate_from _) _ ↔ _, dsimp [generate_from], rw (show (⨆ (b : measurable_space Ξ±) (H : b ∈ ms), set_of (@is_measurable _ b)) = (⋃₀ (is_measurable' '' ms)), { ext, simp only [exists_prop, mem_Union, sUnion_image, mem_set_of_eq], refl, }) end theorem is_measurable_supr {ΞΉ} {m : ΞΉ β†’ measurable_space Ξ±} {s : set Ξ±} : @is_measurable _ (supr m) s ↔ generate_measurable (⋃ i, (m i).is_measurable') s := begin convert @is_measurable_Sup _ (range m) s, simp, end end complete_lattice section functors variables {m m₁ mβ‚‚ : measurable_space Ξ±} {m' : measurable_space Ξ²} {f : Ξ± β†’ Ξ²} {g : Ξ² β†’ Ξ±} /-- The forward image of a measure space under a function. `map f m` contains the sets `s : set Ξ²` whose preimage under `f` is measurable. -/ protected def map (f : Ξ± β†’ Ξ²) (m : measurable_space Ξ±) : measurable_space Ξ² := { is_measurable' := Ξ» s, m.is_measurable' $ f ⁻¹' s, is_measurable_empty := m.is_measurable_empty, is_measurable_compl := assume s hs, m.is_measurable_compl _ hs, is_measurable_Union := assume f hf, by { rw [preimage_Union], exact m.is_measurable_Union _ hf }} @[simp] lemma map_id : m.map id = m := measurable_space.ext $ assume s, iff.rfl @[simp] lemma map_comp {f : Ξ± β†’ Ξ²} {g : Ξ² β†’ Ξ³} : (m.map f).map g = m.map (g ∘ f) := measurable_space.ext $ assume s, iff.rfl /-- The reverse image of a measure space under a function. `comap f m` contains the sets `s : set Ξ±` such that `s` is the `f`-preimage of a measurable set in `Ξ²`. -/ protected def comap (f : Ξ± β†’ Ξ²) (m : measurable_space Ξ²) : measurable_space Ξ± := { is_measurable' := Ξ» s, βˆƒs', m.is_measurable' s' ∧ f ⁻¹' s' = s, is_measurable_empty := βŸ¨βˆ…, m.is_measurable_empty, rfl⟩, is_measurable_compl := assume s ⟨s', h₁, hβ‚‚βŸ©, ⟨s'ᢜ, m.is_measurable_compl _ h₁, hβ‚‚ β–Έ rfl⟩, is_measurable_Union := assume s hs, let ⟨s', hs'⟩ := classical.axiom_of_choice hs in βŸ¨β‹ƒ i, s' i, m.is_measurable_Union _ (Ξ» i, (hs' i).left), by simp [hs'] ⟩ } @[simp] lemma comap_id : m.comap id = m := measurable_space.ext $ assume s, ⟨assume ⟨s', hs', h⟩, h β–Έ hs', assume h, ⟨s, h, rfl⟩⟩ @[simp] lemma comap_comp {f : Ξ² β†’ Ξ±} {g : Ξ³ β†’ Ξ²} : (m.comap f).comap g = m.comap (f ∘ g) := measurable_space.ext $ assume s, ⟨assume ⟨t, ⟨u, h, hu⟩, ht⟩, ⟨u, h, ht β–Έ hu β–Έ rfl⟩, assume ⟨t, h, ht⟩, ⟨f ⁻¹' t, ⟨_, h, rfl⟩, ht⟩⟩ lemma comap_le_iff_le_map {f : Ξ± β†’ Ξ²} : m'.comap f ≀ m ↔ m' ≀ m.map f := ⟨assume h s hs, h _ ⟨_, hs, rfl⟩, assume h s ⟨t, ht, heq⟩, heq β–Έ h _ ht⟩ lemma gc_comap_map (f : Ξ± β†’ Ξ²) : galois_connection (measurable_space.comap f) (measurable_space.map f) := assume f g, comap_le_iff_le_map lemma map_mono (h : m₁ ≀ mβ‚‚) : m₁.map f ≀ mβ‚‚.map f := (gc_comap_map f).monotone_u h lemma monotone_map : monotone (measurable_space.map f) := assume a b h, map_mono h lemma comap_mono (h : m₁ ≀ mβ‚‚) : m₁.comap g ≀ mβ‚‚.comap g := (gc_comap_map g).monotone_l h lemma monotone_comap : monotone (measurable_space.comap g) := assume a b h, comap_mono h @[simp] lemma comap_bot : (βŠ₯ : measurable_space Ξ±).comap g = βŠ₯ := (gc_comap_map g).l_bot @[simp] lemma comap_sup : (m₁ βŠ” mβ‚‚).comap g = m₁.comap g βŠ” mβ‚‚.comap g := (gc_comap_map g).l_sup @[simp] lemma comap_supr {m : ΞΉ β†’ measurable_space Ξ±} : (⨆i, m i).comap g = (⨆i, (m i).comap g) := (gc_comap_map g).l_supr @[simp] lemma map_top : (⊀ : measurable_space Ξ±).map f = ⊀ := (gc_comap_map f).u_top @[simp] lemma map_inf : (m₁ βŠ“ mβ‚‚).map f = m₁.map f βŠ“ mβ‚‚.map f := (gc_comap_map f).u_inf @[simp] lemma map_infi {m : ΞΉ β†’ measurable_space Ξ±} : (β¨…i, m i).map f = (β¨…i, (m i).map f) := (gc_comap_map f).u_infi lemma comap_map_le : (m.map f).comap f ≀ m := (gc_comap_map f).l_u_le _ lemma le_map_comap : m ≀ (m.comap g).map g := (gc_comap_map g).le_u_l _ end functors lemma generate_from_le_generate_from {s t : set (set Ξ±)} (h : s βŠ† t) : generate_from s ≀ generate_from t := gi_generate_from.gc.monotone_l h lemma generate_from_sup_generate_from {s t : set (set Ξ±)} : generate_from s βŠ” generate_from t = generate_from (s βˆͺ t) := (@gi_generate_from Ξ±).gc.l_sup.symm lemma comap_generate_from {f : Ξ± β†’ Ξ²} {s : set (set Ξ²)} : (generate_from s).comap f = generate_from (preimage f '' s) := le_antisymm (comap_le_iff_le_map.2 $ generate_from_le $ assume t hts, generate_measurable.basic _ $ mem_image_of_mem _ $ hts) (generate_from_le $ assume t ⟨u, hu, eq⟩, eq β–Έ ⟨u, generate_measurable.basic _ hu, rfl⟩) end measurable_space section measurable_functions open measurable_space /-- A function `f` between measurable spaces is measurable if the preimage of every measurable set is measurable. -/ def measurable [measurable_space Ξ±] [measurable_space Ξ²] (f : Ξ± β†’ Ξ²) : Prop := βˆ€ ⦃t : set β⦄, is_measurable t β†’ is_measurable (f ⁻¹' t) lemma measurable_iff_le_map {m₁ : measurable_space Ξ±} {mβ‚‚ : measurable_space Ξ²} {f : Ξ± β†’ Ξ²} : measurable f ↔ mβ‚‚ ≀ m₁.map f := iff.rfl alias measurable_iff_le_map ↔ measurable.le_map measurable.of_le_map lemma measurable_iff_comap_le {m₁ : measurable_space Ξ±} {mβ‚‚ : measurable_space Ξ²} {f : Ξ± β†’ Ξ²} : measurable f ↔ mβ‚‚.comap f ≀ m₁ := comap_le_iff_le_map.symm alias measurable_iff_comap_le ↔ measurable.comap_le measurable.of_comap_le lemma measurable.mono {ma ma' : measurable_space Ξ±} {mb mb' : measurable_space Ξ²} {f : Ξ± β†’ Ξ²} (hf : @measurable Ξ± Ξ² ma mb f) (ha : ma ≀ ma') (hb : mb' ≀ mb) : @measurable Ξ± Ξ² ma' mb' f := Ξ» t ht, ha _ $ hf $ hb _ ht lemma measurable_from_top [measurable_space Ξ²] {f : Ξ± β†’ Ξ²} : @measurable _ _ ⊀ _ f := Ξ» s hs, trivial lemma measurable_generate_from [measurable_space Ξ±] {s : set (set Ξ²)} {f : Ξ± β†’ Ξ²} (h : βˆ€ t ∈ s, is_measurable (f ⁻¹' t)) : @measurable _ _ _ (generate_from s) f := measurable.of_le_map $ generate_from_le h variables [measurable_space Ξ±] [measurable_space Ξ²] [measurable_space Ξ³] lemma measurable_id : measurable (@id Ξ±) := Ξ» t, id lemma measurable.comp {g : Ξ² β†’ Ξ³} {f : Ξ± β†’ Ξ²} (hg : measurable g) (hf : measurable f) : measurable (g ∘ f) := Ξ» t ht, hf (hg ht) lemma subsingleton.measurable [subsingleton Ξ±] {f : Ξ± β†’ Ξ²} : measurable f := Ξ» s hs, @subsingleton.is_measurable Ξ± _ _ _ lemma measurable.piecewise {s : set Ξ±} {_ : decidable_pred s} {f g : Ξ± β†’ Ξ²} (hs : is_measurable s) (hf : measurable f) (hg : measurable g) : measurable (piecewise s f g) := begin intros t ht, simp only [piecewise_preimage], exact (hs.inter $ hf ht).union (hs.compl.inter $ hg ht) end @[simp] lemma measurable_const {a : Ξ±} : measurable (Ξ» b : Ξ², a) := assume s hs, is_measurable.const (a ∈ s) lemma measurable.indicator [has_zero Ξ²] {s : set Ξ±} {f : Ξ± β†’ Ξ²} (hf : measurable f) (hs : is_measurable s) : measurable (s.indicator f) := hf.piecewise hs measurable_const @[to_additive] lemma measurable_one [has_one Ξ±] : measurable (1 : Ξ² β†’ Ξ±) := @measurable_const _ _ _ _ 1 end measurable_functions section constructions variables [measurable_space Ξ±] [measurable_space Ξ²] [measurable_space Ξ³] instance : measurable_space empty := ⊀ instance : measurable_space unit := ⊀ instance : measurable_space bool := ⊀ instance : measurable_space β„• := ⊀ instance : measurable_space β„€ := ⊀ instance : measurable_space β„š := ⊀ lemma measurable_to_encodable [encodable Ξ±] {f : Ξ² β†’ Ξ±} (h : βˆ€ y, is_measurable (f ⁻¹' {f y})) : measurable f := begin assume s hs, rw [← bUnion_preimage_singleton], refine is_measurable.Union (Ξ» y, is_measurable.Union_Prop $ Ξ» hy, _), by_cases hyf : y ∈ range f, { rcases hyf with ⟨y, rfl⟩, apply h }, { simp only [preimage_singleton_eq_empty.2 hyf, is_measurable.empty] } end lemma measurable_unit (f : unit β†’ Ξ±) : measurable f := measurable_from_top section nat lemma measurable_from_nat {f : β„• β†’ Ξ±} : measurable f := measurable_from_top lemma measurable_to_nat {f : Ξ± β†’ β„•} : (βˆ€ y, is_measurable (f ⁻¹' {f y})) β†’ measurable f := measurable_to_encodable lemma measurable_find_greatest' {p : Ξ± β†’ β„• β†’ Prop} {N} (hN : βˆ€ k ≀ N, is_measurable {x | nat.find_greatest (p x) N = k}) : measurable (Ξ» x, nat.find_greatest (p x) N) := measurable_to_nat $ Ξ» x, hN _ nat.find_greatest_le lemma measurable_find_greatest {p : Ξ± β†’ β„• β†’ Prop} {N} (hN : βˆ€ k ≀ N, is_measurable {x | p x k}) : measurable (Ξ» x, nat.find_greatest (p x) N) := begin refine measurable_find_greatest' (Ξ» k hk, _), simp only [nat.find_greatest_eq_iff, set_of_and, set_of_forall, ← compl_set_of], repeat { apply_rules [is_measurable.inter, is_measurable.const, is_measurable.Inter, is_measurable.Inter_Prop, is_measurable.compl, hN]; try { intros } } end lemma measurable_find {p : Ξ± β†’ β„• β†’ Prop} (hp : βˆ€ x, βˆƒ N, p x N) (hm : βˆ€ k, is_measurable {x | p x k}) : measurable (Ξ» x, nat.find (hp x)) := begin refine measurable_to_nat (Ξ» x, _), simp only [set.preimage, mem_singleton_iff, nat.find_eq_iff, set_of_and, set_of_forall, ← compl_set_of], repeat { apply_rules [is_measurable.inter, hm, is_measurable.Inter, is_measurable.Inter_Prop, is_measurable.compl]; try { intros } } end end nat section subtype instance {Ξ±} {p : Ξ± β†’ Prop} [m : measurable_space Ξ±] : measurable_space (subtype p) := m.comap (coe : _ β†’ Ξ±) lemma measurable_subtype_coe {p : Ξ± β†’ Prop} : measurable (coe : subtype p β†’ Ξ±) := measurable_space.le_map_comap lemma measurable.subtype_coe {p : Ξ² β†’ Prop} {f : Ξ± β†’ subtype p} (hf : measurable f) : measurable (Ξ» a : Ξ±, (f a : Ξ²)) := measurable_subtype_coe.comp hf lemma measurable.subtype_mk {p : Ξ² β†’ Prop} {f : Ξ± β†’ Ξ²} (hf : measurable f) {h : βˆ€ x, p (f x)} : measurable (Ξ» x, (⟨f x, h x⟩ : subtype p)) := Ξ» t ⟨s, hs⟩, hs.2 β–Έ by simp only [← preimage_comp, (∘), subtype.coe_mk, hf hs.1] lemma is_measurable.subtype_image {s : set Ξ±} {t : set s} (hs : is_measurable s) : is_measurable t β†’ is_measurable ((coe : s β†’ Ξ±) '' t) | ⟨u, (hu : is_measurable u), (eq : coe ⁻¹' u = t)⟩ := begin rw [← eq, subtype.image_preimage_coe], exact hu.inter hs end lemma measurable_of_measurable_union_cover {f : Ξ± β†’ Ξ²} (s t : set Ξ±) (hs : is_measurable s) (ht : is_measurable t) (h : univ βŠ† s βˆͺ t) (hc : measurable (Ξ» a : s, f a)) (hd : measurable (Ξ» a : t, f a)) : measurable f := begin intros u hu, convert (hs.subtype_image (hc hu)).union (ht.subtype_image (hd hu)), change f ⁻¹' u = coe '' (coe ⁻¹' (f ⁻¹' u) : set s) βˆͺ coe '' (coe ⁻¹' (f ⁻¹' u) : set t), rw [image_preimage_eq_inter_range, image_preimage_eq_inter_range, subtype.range_coe, subtype.range_coe, ← inter_distrib_left, univ_subset_iff.1 h, inter_univ], end lemma measurable_of_measurable_on_compl_singleton [measurable_singleton_class Ξ±] {f : Ξ± β†’ Ξ²} (a : Ξ±) (hf : measurable (set.restrict f {x | x β‰  a})) : measurable f := measurable_of_measurable_union_cover _ _ is_measurable_eq is_measurable_eq.compl (Ξ» x hx, classical.em _) (@subsingleton.measurable {x | x = a} _ _ _ ⟨λ x y, subtype.eq $ x.2.trans y.2.symm⟩ _) hf end subtype section prod instance {Ξ± Ξ²} [m₁ : measurable_space Ξ±] [mβ‚‚ : measurable_space Ξ²] : measurable_space (Ξ± Γ— Ξ²) := m₁.comap prod.fst βŠ” mβ‚‚.comap prod.snd lemma measurable_fst : measurable (prod.fst : Ξ± Γ— Ξ² β†’ Ξ±) := measurable.of_comap_le le_sup_left lemma measurable.fst {f : Ξ± β†’ Ξ² Γ— Ξ³} (hf : measurable f) : measurable (Ξ» a : Ξ±, (f a).1) := measurable_fst.comp hf lemma measurable_snd : measurable (prod.snd : Ξ± Γ— Ξ² β†’ Ξ²) := measurable.of_comap_le le_sup_right lemma measurable.snd {f : Ξ± β†’ Ξ² Γ— Ξ³} (hf : measurable f) : measurable (Ξ» a : Ξ±, (f a).2) := measurable_snd.comp hf lemma measurable.prod {f : Ξ± β†’ Ξ² Γ— Ξ³} (hf₁ : measurable (Ξ» a, (f a).1)) (hfβ‚‚ : measurable (Ξ» a, (f a).2)) : measurable f := measurable.of_le_map $ sup_le (by { rw [measurable_space.comap_le_iff_le_map, measurable_space.map_comp], exact hf₁ }) (by { rw [measurable_space.comap_le_iff_le_map, measurable_space.map_comp], exact hfβ‚‚ }) lemma measurable_prod {f : Ξ± β†’ Ξ² Γ— Ξ³} : measurable f ↔ measurable (Ξ» a, (f a).1) ∧ measurable (Ξ» a, (f a).2) := ⟨λ hf, ⟨measurable_fst.comp hf, measurable_snd.comp hf⟩, Ξ» h, measurable.prod h.1 h.2⟩ lemma measurable.prod_mk {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} (hf : measurable f) (hg : measurable g) : measurable (Ξ» a : Ξ±, (f a, g a)) := measurable.prod hf hg lemma measurable_prod_mk_left {x : Ξ±} : measurable (@prod.mk _ Ξ² x) := measurable_const.prod_mk measurable_id lemma measurable_prod_mk_right {y : Ξ²} : measurable (Ξ» x : Ξ±, (x, y)) := measurable_id.prod_mk measurable_const lemma measurable.of_uncurry_left {f : Ξ± β†’ Ξ² β†’ Ξ³} (hf : measurable (uncurry f)) {x : Ξ±} : measurable (f x) := hf.comp measurable_prod_mk_left lemma measurable.of_uncurry_right {f : Ξ± β†’ Ξ² β†’ Ξ³} (hf : measurable (uncurry f)) {y : Ξ²} : measurable (Ξ» x, f x y) := hf.comp measurable_prod_mk_right lemma measurable_swap : measurable (prod.swap : Ξ± Γ— Ξ² β†’ Ξ² Γ— Ξ±) := measurable.prod measurable_snd measurable_fst lemma measurable_swap_iff {f : Ξ± Γ— Ξ² β†’ Ξ³} : measurable (f ∘ prod.swap) ↔ measurable f := ⟨λ hf, by { convert hf.comp measurable_swap, ext ⟨x, y⟩, refl }, Ξ» hf, hf.comp measurable_swap⟩ lemma is_measurable.prod {s : set Ξ±} {t : set Ξ²} (hs : is_measurable s) (ht : is_measurable t) : is_measurable (s.prod t) := is_measurable.inter (measurable_fst hs) (measurable_snd ht) lemma is_measurable_prod_of_nonempty {s : set Ξ±} {t : set Ξ²} (h : (s.prod t).nonempty) : is_measurable (s.prod t) ↔ is_measurable s ∧ is_measurable t := begin rcases h with ⟨⟨x, y⟩, hx, hy⟩, refine ⟨λ hst, _, Ξ» h, h.1.prod h.2⟩, have : is_measurable ((Ξ» x, (x, y)) ⁻¹' s.prod t) := measurable_id.prod_mk measurable_const hst, have : is_measurable (prod.mk x ⁻¹' s.prod t) := measurable_const.prod_mk measurable_id hst, simp * at * end lemma is_measurable_prod {s : set Ξ±} {t : set Ξ²} : is_measurable (s.prod t) ↔ (is_measurable s ∧ is_measurable t) ∨ s = βˆ… ∨ t = βˆ… := begin cases (s.prod t).eq_empty_or_nonempty with h h, { simp [h, prod_eq_empty_iff.mp h] }, { simp [←not_nonempty_iff_eq_empty, prod_nonempty_iff.mp h, is_measurable_prod_of_nonempty h] } end lemma is_measurable_swap_iff {s : set (Ξ± Γ— Ξ²)} : is_measurable (prod.swap ⁻¹' s) ↔ is_measurable s := ⟨λ hs, by { convert measurable_swap hs, ext ⟨x, y⟩, refl }, Ξ» hs, measurable_swap hs⟩ end prod section pi variables {Ο€ : Ξ΄ β†’ Type*} instance measurable_space.pi [m : Ξ  a, measurable_space (Ο€ a)] : measurable_space (Ξ  a, Ο€ a) := ⨆ a, (m a).comap (Ξ» b, b a) variables [Ξ  a, measurable_space (Ο€ a)] [measurable_space Ξ³] lemma measurable_pi_apply (a : Ξ΄) : measurable (Ξ» f : Ξ  a, Ο€ a, f a) := measurable.of_comap_le $ le_supr _ a lemma measurable_pi_lambda (f : Ξ± β†’ Ξ  a, Ο€ a) (hf : βˆ€ a, measurable (Ξ» c, f c a)) : measurable f := measurable.of_le_map $ supr_le $ assume a, measurable_space.comap_le_iff_le_map.2 (hf a) end pi instance {Ξ± Ξ²} [m₁ : measurable_space Ξ±] [mβ‚‚ : measurable_space Ξ²] : measurable_space (Ξ± βŠ• Ξ²) := m₁.map sum.inl βŠ“ mβ‚‚.map sum.inr section sum lemma measurable_inl : measurable (@sum.inl Ξ± Ξ²) := measurable.of_le_map inf_le_left lemma measurable_inr : measurable (@sum.inr Ξ± Ξ²) := measurable.of_le_map inf_le_right lemma measurable_sum {f : Ξ± βŠ• Ξ² β†’ Ξ³} (hl : measurable (f ∘ sum.inl)) (hr : measurable (f ∘ sum.inr)) : measurable f := measurable.of_comap_le $ le_inf (measurable_space.comap_le_iff_le_map.2 $ hl) (measurable_space.comap_le_iff_le_map.2 $ hr) lemma measurable.sum_elim {f : Ξ± β†’ Ξ³} {g : Ξ² β†’ Ξ³} (hf : measurable f) (hg : measurable g) : measurable (sum.elim f g) := measurable_sum hf hg lemma is_measurable.inl_image {s : set Ξ±} (hs : is_measurable s) : is_measurable (sum.inl '' s : set (Ξ± βŠ• Ξ²)) := ⟨show is_measurable (sum.inl ⁻¹' _), by { rwa [preimage_image_eq], exact (Ξ» a b, sum.inl.inj) }, have sum.inr ⁻¹' (sum.inl '' s : set (Ξ± βŠ• Ξ²)) = βˆ… := eq_empty_of_subset_empty $ assume x ⟨y, hy, eq⟩, by contradiction, show is_measurable (sum.inr ⁻¹' _), by { rw [this], exact is_measurable.empty }⟩ lemma is_measurable_range_inl : is_measurable (range sum.inl : set (Ξ± βŠ• Ξ²)) := by { rw [← image_univ], exact is_measurable.univ.inl_image } lemma is_measurable_inr_image {s : set Ξ²} (hs : is_measurable s) : is_measurable (sum.inr '' s : set (Ξ± βŠ• Ξ²)) := ⟨ have sum.inl ⁻¹' (sum.inr '' s : set (Ξ± βŠ• Ξ²)) = βˆ… := eq_empty_of_subset_empty $ assume x ⟨y, hy, eq⟩, by contradiction, show is_measurable (sum.inl ⁻¹' _), by { rw [this], exact is_measurable.empty }, show is_measurable (sum.inr ⁻¹' _), by { rwa [preimage_image_eq], exact Ξ» a b, sum.inr.inj }⟩ lemma is_measurable_range_inr : is_measurable (range sum.inr : set (Ξ± βŠ• Ξ²)) := by { rw [← image_univ], exact is_measurable_inr_image is_measurable.univ } end sum instance {Ξ±} {Ξ² : Ξ± β†’ Type*} [m : Ξ a, measurable_space (Ξ² a)] : measurable_space (sigma Ξ²) := β¨…a, (m a).map (sigma.mk a) end constructions /-- Equivalences between measurable spaces. Main application is the simplification of measurability statements along measurable equivalences. -/ structure measurable_equiv (Ξ± Ξ² : Type*) [measurable_space Ξ±] [measurable_space Ξ²] extends Ξ± ≃ Ξ² := (measurable_to_fun : measurable to_fun) (measurable_inv_fun : measurable inv_fun) namespace measurable_equiv variables (Ξ± Ξ²) [measurable_space Ξ±] [measurable_space Ξ²] [measurable_space Ξ³] [measurable_space Ξ΄] instance : has_coe_to_fun (measurable_equiv Ξ± Ξ²) := ⟨λ _, Ξ± β†’ Ξ², Ξ» e, e.to_equiv⟩ variables {Ξ± Ξ²} lemma coe_eq (e : measurable_equiv Ξ± Ξ²) : (e : Ξ± β†’ Ξ²) = e.to_equiv := rfl protected lemma measurable (e : measurable_equiv Ξ± Ξ²) : measurable (e : Ξ± β†’ Ξ²) := e.measurable_to_fun /-- Any measurable space is equivalent to itself. -/ def refl (Ξ± : Type*) [measurable_space Ξ±] : measurable_equiv Ξ± Ξ± := { to_equiv := equiv.refl Ξ±, measurable_to_fun := measurable_id, measurable_inv_fun := measurable_id } instance : inhabited (measurable_equiv Ξ± Ξ±) := ⟨refl α⟩ /-- The composition of equivalences between measurable spaces. -/ @[simps] def trans (ab : measurable_equiv Ξ± Ξ²) (bc : measurable_equiv Ξ² Ξ³) : measurable_equiv Ξ± Ξ³ := { to_equiv := ab.to_equiv.trans bc.to_equiv, measurable_to_fun := bc.measurable_to_fun.comp ab.measurable_to_fun, measurable_inv_fun := ab.measurable_inv_fun.comp bc.measurable_inv_fun } /-- The inverse of an equivalence between measurable spaces. -/ @[simps] def symm (ab : measurable_equiv Ξ± Ξ²) : measurable_equiv Ξ² Ξ± := { to_equiv := ab.to_equiv.symm, measurable_to_fun := ab.measurable_inv_fun, measurable_inv_fun := ab.measurable_to_fun } /-- Equal measurable spaces are equivalent. -/ protected def cast {Ξ± Ξ²} [i₁ : measurable_space Ξ±] [iβ‚‚ : measurable_space Ξ²] (h : Ξ± = Ξ²) (hi : i₁ == iβ‚‚) : measurable_equiv Ξ± Ξ² := { to_equiv := equiv.cast h, measurable_to_fun := by { substI h, substI hi, exact measurable_id }, measurable_inv_fun := by { substI h, substI hi, exact measurable_id }} protected lemma measurable_coe_iff {f : Ξ² β†’ Ξ³} (e : measurable_equiv Ξ± Ξ²) : measurable (f ∘ e) ↔ measurable f := iff.intro (assume hfe, have measurable (f ∘ (e.symm.trans e).to_equiv) := hfe.comp e.symm.measurable, by rwa [trans_to_equiv, symm_to_equiv, equiv.symm_trans] at this) (Ξ» h, h.comp e.measurable) /-- Products of equivalent measurable spaces are equivalent. -/ def prod_congr (ab : measurable_equiv Ξ± Ξ²) (cd : measurable_equiv Ξ³ Ξ΄) : measurable_equiv (Ξ± Γ— Ξ³) (Ξ² Γ— Ξ΄) := { to_equiv := equiv.prod_congr ab.to_equiv cd.to_equiv, measurable_to_fun := (ab.measurable_to_fun.comp measurable_id.fst).prod_mk (cd.measurable_to_fun.comp measurable_id.snd), measurable_inv_fun := (ab.measurable_inv_fun.comp measurable_id.fst).prod_mk (cd.measurable_inv_fun.comp measurable_id.snd) } /-- Products of measurable spaces are symmetric. -/ def prod_comm : measurable_equiv (Ξ± Γ— Ξ²) (Ξ² Γ— Ξ±) := { to_equiv := equiv.prod_comm Ξ± Ξ², measurable_to_fun := measurable_id.snd.prod_mk measurable_id.fst, measurable_inv_fun := measurable_id.snd.prod_mk measurable_id.fst } /-- Products of measurable spaces are associative. -/ def prod_assoc : measurable_equiv ((Ξ± Γ— Ξ²) Γ— Ξ³) (Ξ± Γ— (Ξ² Γ— Ξ³)) := { to_equiv := equiv.prod_assoc Ξ± Ξ² Ξ³, measurable_to_fun := measurable_fst.fst.prod_mk $ measurable_fst.snd.prod_mk measurable_snd, measurable_inv_fun := (measurable_fst.prod_mk measurable_snd.fst).prod_mk measurable_snd.snd } /-- Sums of measurable spaces are symmetric. -/ def sum_congr (ab : measurable_equiv Ξ± Ξ²) (cd : measurable_equiv Ξ³ Ξ΄) : measurable_equiv (Ξ± βŠ• Ξ³) (Ξ² βŠ• Ξ΄) := { to_equiv := equiv.sum_congr ab.to_equiv cd.to_equiv, measurable_to_fun := begin cases ab with ab' abm, cases ab', cases cd with cd' cdm, cases cd', refine measurable_sum (measurable_inl.comp abm) (measurable_inr.comp cdm) end, measurable_inv_fun := begin cases ab with ab' _ abm, cases ab', cases cd with cd' _ cdm, cases cd', refine measurable_sum (measurable_inl.comp abm) (measurable_inr.comp cdm) end } /-- `set.prod s t ≃ (s Γ— t)` as measurable spaces. -/ def set.prod (s : set Ξ±) (t : set Ξ²) : measurable_equiv (s.prod t) (s Γ— t) := { to_equiv := equiv.set.prod s t, measurable_to_fun := measurable_id.subtype_coe.fst.subtype_mk.prod_mk measurable_id.subtype_coe.snd.subtype_mk, measurable_inv_fun := measurable.subtype_mk $ measurable_id.fst.subtype_coe.prod_mk measurable_id.snd.subtype_coe } /-- `univ Ξ± ≃ Ξ±` as measurable spaces. -/ def set.univ (Ξ± : Type*) [measurable_space Ξ±] : measurable_equiv (univ : set Ξ±) Ξ± := { to_equiv := equiv.set.univ Ξ±, measurable_to_fun := measurable_id.subtype_coe, measurable_inv_fun := measurable_id.subtype_mk } /-- `{a} ≃ unit` as measurable spaces. -/ def set.singleton (a : Ξ±) : measurable_equiv ({a} : set Ξ±) unit := { to_equiv := equiv.set.singleton a, measurable_to_fun := measurable_const, measurable_inv_fun := measurable_const } /-- A set is equivalent to its image under a function `f` as measurable spaces, if `f` is an injective measurable function that sends measurable sets to measurable sets. -/ noncomputable def set.image (f : Ξ± β†’ Ξ²) (s : set Ξ±) (hf : injective f) (hfm : measurable f) (hfi : βˆ€ s, is_measurable s β†’ is_measurable (f '' s)) : measurable_equiv s (f '' s) := { to_equiv := equiv.set.image f s hf, measurable_to_fun := (hfm.comp measurable_id.subtype_coe).subtype_mk, measurable_inv_fun := begin rintro t ⟨u, hu, rfl⟩, simp [preimage_preimage, equiv.set.image_symm_preimage hf], exact measurable_subtype_coe (hfi u hu) end } /-- The domain of `f` is equivalent to its range as measurable spaces, if `f` is an injective measurable function that sends measurable sets to measurable sets. -/ noncomputable def set.range (f : Ξ± β†’ Ξ²) (hf : injective f) (hfm : measurable f) (hfi : βˆ€ s, is_measurable s β†’ is_measurable (f '' s)) : measurable_equiv Ξ± (range f) := (measurable_equiv.set.univ _).symm.trans $ (measurable_equiv.set.image f univ hf hfm hfi).trans $ measurable_equiv.cast (by rw image_univ) (by rw image_univ) /-- `Ξ±` is equivalent to its image in `Ξ± βŠ• Ξ²` as measurable spaces. -/ def set.range_inl : measurable_equiv (range sum.inl : set (Ξ± βŠ• Ξ²)) Ξ± := { to_fun := Ξ» ab, match ab with | ⟨sum.inl a, _⟩ := a | ⟨sum.inr b, p⟩ := have false, by { cases p, contradiction }, this.elim end, inv_fun := Ξ» a, ⟨sum.inl a, a, rfl⟩, left_inv := by { rintro ⟨ab, a, rfl⟩, refl }, right_inv := assume a, rfl, measurable_to_fun := assume s (hs : is_measurable s), begin refine ⟨_, hs.inl_image, set.ext _⟩, rintros ⟨ab, a, rfl⟩, simp [set.range_inl._match_1] end, measurable_inv_fun := measurable.subtype_mk measurable_inl } /-- `Ξ²` is equivalent to its image in `Ξ± βŠ• Ξ²` as measurable spaces. -/ def set.range_inr : measurable_equiv (range sum.inr : set (Ξ± βŠ• Ξ²)) Ξ² := { to_fun := Ξ» ab, match ab with | ⟨sum.inr b, _⟩ := b | ⟨sum.inl a, p⟩ := have false, by { cases p, contradiction }, this.elim end, inv_fun := Ξ» b, ⟨sum.inr b, b, rfl⟩, left_inv := by { rintro ⟨ab, b, rfl⟩, refl }, right_inv := assume b, rfl, measurable_to_fun := assume s (hs : is_measurable s), begin refine ⟨_, is_measurable_inr_image hs, set.ext _⟩, rintros ⟨ab, b, rfl⟩, simp [set.range_inr._match_1] end, measurable_inv_fun := measurable.subtype_mk measurable_inr } /-- Products distribute over sums (on the right) as measurable spaces. -/ def sum_prod_distrib (Ξ± Ξ² Ξ³) [measurable_space Ξ±] [measurable_space Ξ²] [measurable_space Ξ³] : measurable_equiv ((Ξ± βŠ• Ξ²) Γ— Ξ³) ((Ξ± Γ— Ξ³) βŠ• (Ξ² Γ— Ξ³)) := { to_equiv := equiv.sum_prod_distrib Ξ± Ξ² Ξ³, measurable_to_fun := begin refine measurable_of_measurable_union_cover ((range sum.inl).prod univ) ((range sum.inr).prod univ) (is_measurable_range_inl.prod is_measurable.univ) (is_measurable_range_inr.prod is_measurable.univ) (by { rintro ⟨a|b, c⟩; simp [set.prod_eq] }) _ _, { refine (set.prod (range sum.inl) univ).symm.measurable_coe_iff.1 _, refine (prod_congr set.range_inl (set.univ _)).symm.measurable_coe_iff.1 _, dsimp [(∘)], convert measurable_inl, ext ⟨a, c⟩, refl }, { refine (set.prod (range sum.inr) univ).symm.measurable_coe_iff.1 _, refine (prod_congr set.range_inr (set.univ _)).symm.measurable_coe_iff.1 _, dsimp [(∘)], convert measurable_inr, ext ⟨b, c⟩, refl } end, measurable_inv_fun := measurable_sum ((measurable_inl.comp measurable_fst).prod_mk measurable_snd) ((measurable_inr.comp measurable_fst).prod_mk measurable_snd) } /-- Products distribute over sums (on the left) as measurable spaces. -/ def prod_sum_distrib (Ξ± Ξ² Ξ³) [measurable_space Ξ±] [measurable_space Ξ²] [measurable_space Ξ³] : measurable_equiv (Ξ± Γ— (Ξ² βŠ• Ξ³)) ((Ξ± Γ— Ξ²) βŠ• (Ξ± Γ— Ξ³)) := prod_comm.trans $ (sum_prod_distrib _ _ _).trans $ sum_congr prod_comm prod_comm /-- Products distribute over sums as measurable spaces. -/ def sum_prod_sum (Ξ± Ξ² Ξ³ Ξ΄) [measurable_space Ξ±] [measurable_space Ξ²] [measurable_space Ξ³] [measurable_space Ξ΄] : measurable_equiv ((Ξ± βŠ• Ξ²) Γ— (Ξ³ βŠ• Ξ΄)) (((Ξ± Γ— Ξ³) βŠ• (Ξ± Γ— Ξ΄)) βŠ• ((Ξ² Γ— Ξ³) βŠ• (Ξ² Γ— Ξ΄))) := (sum_prod_distrib _ _ _).trans $ sum_congr (prod_sum_distrib _ _ _) (prod_sum_distrib _ _ _) end measurable_equiv /-- A pi-system is a collection of subsets of `Ξ±` that is closed under intersections of sets that are not disjoint. Usually it is also required that the collection is nonempty, but we don't do that here. -/ def is_pi_system {Ξ±} (C : set (set Ξ±)) : Prop := βˆ€ s t ∈ C, (s ∩ t : set Ξ±).nonempty β†’ s ∩ t ∈ C namespace measurable_space lemma is_pi_system_is_measurable [measurable_space Ξ±] : is_pi_system {s : set Ξ± | is_measurable s} := Ξ» s t hs ht _, hs.inter ht /-- A Dynkin system is a collection of subsets of a type `Ξ±` that contains the empty set, is closed under complementation and under countable union of pairwise disjoint sets. The disjointness condition is the only difference with `Οƒ`-algebras. The main purpose of Dynkin systems is to provide a powerful induction rule for Οƒ-algebras generated by intersection stable set systems. A Dynkin system is also known as a "Ξ»-system" or a "d-system". -/ structure dynkin_system (Ξ± : Type*) := (has : set Ξ± β†’ Prop) (has_empty : has βˆ…) (has_compl : βˆ€ {a}, has a β†’ has aᢜ) (has_Union_nat : βˆ€ {f : β„• β†’ set Ξ±}, pairwise (disjoint on f) β†’ (βˆ€ i, has (f i)) β†’ has (⋃ i, f i)) namespace dynkin_system @[ext] lemma ext : βˆ€ {d₁ dβ‚‚ : dynkin_system Ξ±}, (βˆ€ s : set Ξ±, d₁.has s ↔ dβ‚‚.has s) β†’ d₁ = dβ‚‚ | ⟨s₁, _, _, _⟩ ⟨sβ‚‚, _, _, _⟩ h := have s₁ = sβ‚‚, from funext $ assume x, propext $ h x, by subst this variable (d : dynkin_system Ξ±) lemma has_compl_iff {a} : d.has aᢜ ↔ d.has a := ⟨λ h, by simpa using d.has_compl h, Ξ» h, d.has_compl h⟩ lemma has_univ : d.has univ := by simpa using d.has_compl d.has_empty theorem has_Union {Ξ²} [encodable Ξ²] {f : Ξ² β†’ set Ξ±} (hd : pairwise (disjoint on f)) (h : βˆ€ i, d.has (f i)) : d.has (⋃ i, f i) := by { rw ← encodable.Union_decode2, exact d.has_Union_nat (Union_decode2_disjoint_on hd) (Ξ» n, encodable.Union_decode2_cases d.has_empty h) } theorem has_union {s₁ sβ‚‚ : set Ξ±} (h₁ : d.has s₁) (hβ‚‚ : d.has sβ‚‚) (h : s₁ ∩ sβ‚‚ βŠ† βˆ…) : d.has (s₁ βˆͺ sβ‚‚) := by { rw union_eq_Union, exact d.has_Union (pairwise_disjoint_on_bool.2 h) (bool.forall_bool.2 ⟨hβ‚‚, hβ‚βŸ©) } lemma has_diff {s₁ sβ‚‚ : set Ξ±} (h₁ : d.has s₁) (hβ‚‚ : d.has sβ‚‚) (h : sβ‚‚ βŠ† s₁) : d.has (s₁ \ sβ‚‚) := begin apply d.has_compl_iff.1, simp [diff_eq, compl_inter], exact d.has_union (d.has_compl h₁) hβ‚‚ (Ξ» x ⟨h₁, hβ‚‚βŸ©, h₁ (h hβ‚‚)), end instance : partial_order (dynkin_system Ξ±) := { le := Ξ» m₁ mβ‚‚, m₁.has ≀ mβ‚‚.has, le_refl := assume a b, le_refl _, le_trans := assume a b c, le_trans, le_antisymm := assume a b h₁ hβ‚‚, ext $ assume s, ⟨h₁ s, hβ‚‚ s⟩ } /-- Every measurable space (Οƒ-algebra) forms a Dynkin system -/ def of_measurable_space (m : measurable_space Ξ±) : dynkin_system Ξ± := { has := m.is_measurable', has_empty := m.is_measurable_empty, has_compl := m.is_measurable_compl, has_Union_nat := assume f _ hf, m.is_measurable_Union f hf } lemma of_measurable_space_le_of_measurable_space_iff {m₁ mβ‚‚ : measurable_space Ξ±} : of_measurable_space m₁ ≀ of_measurable_space mβ‚‚ ↔ m₁ ≀ mβ‚‚ := iff.rfl /-- The least Dynkin system containing a collection of basic sets. This inductive type gives the underlying collection of sets. -/ inductive generate_has (s : set (set Ξ±)) : set Ξ± β†’ Prop | basic : βˆ€ t ∈ s, generate_has t | empty : generate_has βˆ… | compl : βˆ€ {a}, generate_has a β†’ generate_has aᢜ | Union : βˆ€ {f : β„• β†’ set Ξ±}, pairwise (disjoint on f) β†’ (βˆ€ i, generate_has (f i)) β†’ generate_has (⋃ i, f i) lemma generate_has_compl {C : set (set Ξ±)} {s : set Ξ±} : generate_has C sᢜ ↔ generate_has C s := by { refine ⟨_, generate_has.compl⟩, intro h, convert generate_has.compl h, simp } /-- The least Dynkin system containing a collection of basic sets. -/ def generate (s : set (set Ξ±)) : dynkin_system Ξ± := { has := generate_has s, has_empty := generate_has.empty, has_compl := assume a, generate_has.compl, has_Union_nat := assume f, generate_has.Union } lemma generate_has_def {C : set (set Ξ±)} : (generate C).has = generate_has C := rfl instance : inhabited (dynkin_system Ξ±) := ⟨generate univ⟩ /-- If a Dynkin system is closed under binary intersection, then it forms a `Οƒ`-algebra. -/ def to_measurable_space (h_inter : βˆ€ s₁ sβ‚‚, d.has s₁ β†’ d.has sβ‚‚ β†’ d.has (s₁ ∩ sβ‚‚)) := { measurable_space . is_measurable' := d.has, is_measurable_empty := d.has_empty, is_measurable_compl := assume s h, d.has_compl h, is_measurable_Union := assume f hf, have βˆ€ n, d.has (disjointed f n), from assume n, disjointed_induct (hf n) (assume t i h, h_inter _ _ h $ d.has_compl $ hf i), have d.has (⋃ n, disjointed f n), from d.has_Union disjoint_disjointed this, by rwa [Union_disjointed] at this } lemma of_measurable_space_to_measurable_space (h_inter : βˆ€ s₁ sβ‚‚, d.has s₁ β†’ d.has sβ‚‚ β†’ d.has (s₁ ∩ sβ‚‚)) : of_measurable_space (d.to_measurable_space h_inter) = d := ext $ assume s, iff.rfl /-- If `s` is in a Dynkin system `d`, we can form the new Dynkin system `{s ∩ t | t ∈ d}`. -/ def restrict_on {s : set Ξ±} (h : d.has s) : dynkin_system Ξ± := { has := Ξ» t, d.has (t ∩ s), has_empty := by simp [d.has_empty], has_compl := assume t hts, have tᢜ ∩ s = ((t ∩ s)ᢜ) \ sᢜ, from set.ext $ assume x, by { by_cases x ∈ s; simp [h] }, by { rw [this], exact d.has_diff (d.has_compl hts) (d.has_compl h) (compl_subset_compl.mpr $ inter_subset_right _ _) }, has_Union_nat := assume f hd hf, begin rw [inter_comm, inter_Union], apply d.has_Union_nat, { exact Ξ» i j h x ⟨⟨_, hβ‚βŸ©, _, hβ‚‚βŸ©, hd i j h ⟨h₁, hβ‚‚βŸ© }, { simpa [inter_comm] using hf }, end } lemma generate_le {s : set (set Ξ±)} (h : βˆ€ t ∈ s, d.has t) : generate s ≀ d := Ξ» t ht, ht.rec_on h d.has_empty (assume a _ h, d.has_compl h) (assume f hd _ hf, d.has_Union hd hf) lemma generate_has_subset_generate_measurable {C : set (set Ξ±)} {s : set Ξ±} (hs : (generate C).has s) : (generate_from C).is_measurable' s := generate_le (of_measurable_space (generate_from C)) (Ξ» t, is_measurable_generate_from) s hs lemma generate_inter {s : set (set Ξ±)} (hs : is_pi_system s) {t₁ tβ‚‚ : set Ξ±} (ht₁ : (generate s).has t₁) (htβ‚‚ : (generate s).has tβ‚‚) : (generate s).has (t₁ ∩ tβ‚‚) := have generate s ≀ (generate s).restrict_on htβ‚‚, from generate_le _ $ assume s₁ hs₁, have (generate s).has s₁, from generate_has.basic s₁ hs₁, have generate s ≀ (generate s).restrict_on this, from generate_le _ $ assume sβ‚‚ hsβ‚‚, show (generate s).has (sβ‚‚ ∩ s₁), from (sβ‚‚ ∩ s₁).eq_empty_or_nonempty.elim (Ξ» h, h.symm β–Έ generate_has.empty) (Ξ» h, generate_has.basic _ (hs _ _ hsβ‚‚ hs₁ h)), have (generate s).has (tβ‚‚ ∩ s₁), from this _ htβ‚‚, show (generate s).has (s₁ ∩ tβ‚‚), by rwa [inter_comm], this _ ht₁ /-- If we have a collection of sets closed under binary intersections, then the Dynkin system it generates is equal to the Οƒ-algebra it generates. This result is known as the Ο€-Ξ» theorem. A collection of sets closed under binary intersection is called a "Ο€-system" if it is non-empty. -/ lemma generate_from_eq {s : set (set Ξ±)} (hs : is_pi_system s) : generate_from s = (generate s).to_measurable_space (assume t₁ tβ‚‚, generate_inter hs) := le_antisymm (generate_from_le $ assume t ht, generate_has.basic t ht) (of_measurable_space_le_of_measurable_space_iff.mp $ by { rw [of_measurable_space_to_measurable_space], exact (generate_le _ $ assume t ht, is_measurable_generate_from ht) }) end dynkin_system lemma induction_on_inter {C : set Ξ± β†’ Prop} {s : set (set Ξ±)} [m : measurable_space Ξ±] (h_eq : m = generate_from s) (h_inter : is_pi_system s) (h_empty : C βˆ…) (h_basic : βˆ€ t ∈ s, C t) (h_compl : βˆ€ t, is_measurable t β†’ C t β†’ C tᢜ) (h_union : βˆ€ f : β„• β†’ set Ξ±, pairwise (disjoint on f) β†’ (βˆ€ i, is_measurable (f i)) β†’ (βˆ€ i, C (f i)) β†’ C (⋃ i, f i)) : βˆ€ ⦃t⦄, is_measurable t β†’ C t := have eq : is_measurable = dynkin_system.generate_has s, by { rw [h_eq, dynkin_system.generate_from_eq h_inter], refl }, assume t ht, have dynkin_system.generate_has s t, by rwa [eq] at ht, this.rec_on h_basic h_empty (assume t ht, h_compl t $ by { rw [eq], exact ht }) (assume f hf ht, h_union f hf $ assume i, by { rw [eq], exact ht _ }) end measurable_space namespace filter variables [measurable_space Ξ±] /-- A filter `f` is measurably generates if each `s ∈ f` includes a measurable `t ∈ f`. -/ class is_measurably_generated (f : filter Ξ±) : Prop := (exists_measurable_subset : βˆ€ ⦃s⦄, s ∈ f β†’ βˆƒ t ∈ f, is_measurable t ∧ t βŠ† s) instance is_measurably_generated_bot : is_measurably_generated (βŠ₯ : filter Ξ±) := ⟨λ _ _, βŸ¨βˆ…, mem_bot_sets, is_measurable.empty, empty_subset _⟩⟩ instance is_measurably_generated_top : is_measurably_generated (⊀ : filter Ξ±) := ⟨λ s hs, ⟨univ, univ_mem_sets, is_measurable.univ, Ξ» x _, hs x⟩⟩ lemma eventually.exists_measurable_mem {f : filter Ξ±} [is_measurably_generated f] {p : Ξ± β†’ Prop} (h : βˆ€αΆ  x in f, p x) : βˆƒ s ∈ f, is_measurable s ∧ βˆ€ x ∈ s, p x := is_measurably_generated.exists_measurable_subset h instance inf_is_measurably_generated (f g : filter Ξ±) [is_measurably_generated f] [is_measurably_generated g] : is_measurably_generated (f βŠ“ g) := begin refine ⟨_⟩, rintros t ⟨sf, hsf, sg, hsg, ht⟩, rcases is_measurably_generated.exists_measurable_subset hsf with ⟨s'f, hs'f, hmf, hs'sf⟩, rcases is_measurably_generated.exists_measurable_subset hsg with ⟨s'g, hs'g, hmg, hs'sg⟩, refine ⟨s'f ∩ s'g, inter_mem_inf_sets hs'f hs'g, hmf.inter hmg, _⟩, exact subset.trans (inter_subset_inter hs'sf hs'sg) ht end lemma principal_is_measurably_generated_iff {s : set Ξ±} : is_measurably_generated (π“Ÿ s) ↔ is_measurable s := begin refine ⟨_, Ξ» hs, ⟨λ t ht, ⟨s, mem_principal_self s, hs, ht⟩⟩⟩, rintros ⟨hs⟩, rcases hs (mem_principal_self s) with ⟨t, ht, htm, hts⟩, have : t = s := subset.antisymm hts ht, rwa ← this end alias principal_is_measurably_generated_iff ↔ _ is_measurable.principal_is_measurably_generated instance infi_is_measurably_generated {f : ΞΉ β†’ filter Ξ±} [βˆ€ i, is_measurably_generated (f i)] : is_measurably_generated (β¨… i, f i) := begin refine ⟨λ s hs, _⟩, rw [← equiv.plift.surjective.infi_comp, mem_infi_iff] at hs, rcases hs with ⟨t, ht, ⟨V, hVf, hVs⟩⟩, choose U hUf hU using Ξ» i, is_measurably_generated.exists_measurable_subset (hVf i), refine βŸ¨β‹‚ i : t, U i, _, _, _⟩, { rw [← equiv.plift.surjective.infi_comp, mem_infi_iff], refine ⟨t, ht, U, hUf, subset.refl _⟩ }, { haveI := ht.countable.to_encodable, refine is_measurable.Inter (Ξ» i, (hU i).1) }, { exact subset.trans (Inter_subset_Inter $ Ξ» i, (hU i).2) hVs } end end filter /-- We say that a collection of sets is countably spanning if a countable subset spans the whole type. This is a useful condition in various parts of measure theory. For example, it is a needed condition to show that the product of two collections generate the product sigma algebra, see `generate_from_prod_eq`. -/ def is_countably_spanning (C : set (set Ξ±)) : Prop := βˆƒ (s : β„• β†’ set Ξ±), (βˆ€ n, s n ∈ C) ∧ (⋃ n, s n) = univ lemma is_countably_spanning_is_measurable [measurable_space Ξ±] : is_countably_spanning {s : set Ξ± | is_measurable s} := ⟨λ _, univ, Ξ» _, is_measurable.univ, Union_const _⟩
ff48b38e7c21b781ee9839ab968c18bde3110abb
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/data/polynomial/reverse.lean
ce96e925bde9aa2b3680442486dabcb7ffbbd306
[ "Apache-2.0" ]
permissive
robertylewis/mathlib
3d16e3e6daf5ddde182473e03a1b601d2810952c
1d13f5b932f5e40a8308e3840f96fc882fae01f0
refs/heads/master
1,651,379,945,369
1,644,276,960,000
1,644,276,960,000
98,875,504
0
0
Apache-2.0
1,644,253,514,000
1,501,495,700,000
Lean
UTF-8
Lean
false
false
13,106
lean
/- Copyright (c) 2020 Damiano Testa. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Damiano Testa -/ import data.polynomial.degree.trailing_degree import data.polynomial.erase_lead import data.polynomial.eval /-! # Reverse of a univariate polynomial The main definition is `reverse`. Applying `reverse` to a polynomial `f : polynomial R` produces the polynomial with a reversed list of coefficients, equivalent to `X^f.nat_degree * f(1/X)`. The main result is that `reverse (f * g) = reverse f * reverse g`, provided the leading coefficients of `f` and `g` do not multiply to zero. -/ namespace polynomial open polynomial finsupp finset open_locale classical section semiring variables {R : Type*} [semiring R] {f : polynomial R} /-- If `i ≀ N`, then `rev_at_fun N i` returns `N - i`, otherwise it returns `i`. This is the map used by the embedding `rev_at`. -/ def rev_at_fun (N i : β„•) : β„• := ite (i ≀ N) (N-i) i lemma rev_at_fun_invol {N i : β„•} : rev_at_fun N (rev_at_fun N i) = i := begin unfold rev_at_fun, split_ifs with h j, { exact tsub_tsub_cancel_of_le h, }, { exfalso, apply j, exact nat.sub_le N i, }, { refl, }, end lemma rev_at_fun_inj {N : β„•} : function.injective (rev_at_fun N) := begin intros a b hab, rw [← @rev_at_fun_invol N a, hab, rev_at_fun_invol], end /-- If `i ≀ N`, then `rev_at N i` returns `N - i`, otherwise it returns `i`. Essentially, this embedding is only used for `i ≀ N`. The advantage of `rev_at N i` over `N - i` is that `rev_at` is an involution. -/ def rev_at (N : β„•) : function.embedding β„• β„• := { to_fun := Ξ» i , (ite (i ≀ N) (N-i) i), inj' := rev_at_fun_inj } /-- We prefer to use the bundled `rev_at` over unbundled `rev_at_fun`. -/ @[simp] lemma rev_at_fun_eq (N i : β„•) : rev_at_fun N i = rev_at N i := rfl @[simp] lemma rev_at_invol {N i : β„•} : (rev_at N) (rev_at N i) = i := rev_at_fun_invol @[simp] lemma rev_at_le {N i : β„•} (H : i ≀ N) : rev_at N i = N - i := if_pos H lemma rev_at_add {N O n o : β„•} (hn : n ≀ N) (ho : o ≀ O) : rev_at (N + O) (n + o) = rev_at N n + rev_at O o := begin rcases nat.le.dest hn with ⟨n', rfl⟩, rcases nat.le.dest ho with ⟨o', rfl⟩, repeat { rw rev_at_le (le_add_right rfl.le) }, rw [add_assoc, add_left_comm n' o, ← add_assoc, rev_at_le (le_add_right rfl.le)], repeat {rw add_tsub_cancel_left}, end @[simp] lemma rev_at_zero (N : β„•) : rev_at N 0 = N := by simp [rev_at] /-- `reflect N f` is the polynomial such that `(reflect N f).coeff i = f.coeff (rev_at N i)`. In other words, the terms with exponent `[0, ..., N]` now have exponent `[N, ..., 0]`. In practice, `reflect` is only used when `N` is at least as large as the degree of `f`. Eventually, it will be used with `N` exactly equal to the degree of `f`. -/ noncomputable def reflect (N : β„•) : polynomial R β†’ polynomial R | ⟨f⟩ := ⟨finsupp.emb_domain (rev_at N) f⟩ lemma reflect_support (N : β„•) (f : polynomial R) : (reflect N f).support = image (rev_at N) f.support := begin rcases f, ext1, rw [reflect, mem_image, support, support, support_emb_domain, mem_map], end @[simp] lemma coeff_reflect (N : β„•) (f : polynomial R) (i : β„•) : coeff (reflect N f) i = f.coeff (rev_at N i) := begin rcases f, simp only [reflect, coeff], calc finsupp.emb_domain (rev_at N) f i = finsupp.emb_domain (rev_at N) f (rev_at N (rev_at N i)) : by rw rev_at_invol ... = f (rev_at N i) : finsupp.emb_domain_apply _ _ _ end @[simp] lemma reflect_zero {N : β„•} : reflect N (0 : polynomial R) = 0 := rfl @[simp] lemma reflect_eq_zero_iff {N : β„•} {f : polynomial R} : reflect N (f : polynomial R) = 0 ↔ f = 0 := by { rcases f, simp [reflect, ← zero_to_finsupp] } @[simp] lemma reflect_add (f g : polynomial R) (N : β„•) : reflect N (f + g) = reflect N f + reflect N g := by { ext, simp only [coeff_add, coeff_reflect], } @[simp] lemma reflect_C_mul (f : polynomial R) (r : R) (N : β„•) : reflect N (C r * f) = C r * (reflect N f) := by { ext, simp only [coeff_reflect, coeff_C_mul], } @[simp] lemma reflect_C_mul_X_pow (N n : β„•) {c : R} : reflect N (C c * X ^ n) = C c * X ^ (rev_at N n) := begin ext, rw [reflect_C_mul, coeff_C_mul, coeff_C_mul, coeff_X_pow, coeff_reflect], split_ifs with h j, { rw [h, rev_at_invol, coeff_X_pow_self], }, { rw [not_mem_support_iff.mp], intro a, rw [← one_mul (X ^ n), ← C_1] at a, apply h, rw [← (mem_support_C_mul_X_pow a), rev_at_invol], }, end @[simp] lemma reflect_C (r : R) (N : β„•) : reflect N (C r) = C r * X ^ N := by conv_lhs { rw [← mul_one (C r), ← pow_zero X, reflect_C_mul_X_pow, rev_at_zero] } @[simp] lemma reflect_monomial (N n : β„•) : reflect N ((X : polynomial R) ^ n) = X ^ (rev_at N n) := by rw [← one_mul (X ^ n), ← one_mul (X ^ (rev_at N n)), ← C_1, reflect_C_mul_X_pow] lemma reflect_mul_induction (cf cg : β„•) : βˆ€ N O : β„•, βˆ€ f g : polynomial R, f.support.card ≀ cf.succ β†’ g.support.card ≀ cg.succ β†’ f.nat_degree ≀ N β†’ g.nat_degree ≀ O β†’ (reflect (N + O) (f * g)) = (reflect N f) * (reflect O g) := begin induction cf with cf hcf, --first induction (left): base case { induction cg with cg hcg, -- second induction (right): base case { intros N O f g Cf Cg Nf Og, rw [← C_mul_X_pow_eq_self Cf, ← C_mul_X_pow_eq_self Cg], simp only [mul_assoc, X_pow_mul, ← pow_add X, reflect_C_mul, reflect_monomial, add_comm, rev_at_add Nf Og] }, -- second induction (right): induction step { intros N O f g Cf Cg Nf Og, by_cases g0 : g = 0, { rw [g0, reflect_zero, mul_zero, mul_zero, reflect_zero], }, rw [← erase_lead_add_C_mul_X_pow g, mul_add, reflect_add, reflect_add, mul_add, hcg, hcg]; try { assumption }, { exact le_add_left card_support_C_mul_X_pow_le_one }, { exact (le_trans (nat_degree_C_mul_X_pow_le g.leading_coeff g.nat_degree) Og) }, { exact nat.lt_succ_iff.mp (gt_of_ge_of_gt Cg (erase_lead_support_card_lt g0)) }, { exact le_trans erase_lead_nat_degree_le Og } } }, --first induction (left): induction step { intros N O f g Cf Cg Nf Og, by_cases f0 : f = 0, { rw [f0, reflect_zero, zero_mul, zero_mul, reflect_zero], }, rw [← erase_lead_add_C_mul_X_pow f, add_mul, reflect_add, reflect_add, add_mul, hcf, hcf]; try { assumption }, { exact le_add_left card_support_C_mul_X_pow_le_one }, { exact (le_trans (nat_degree_C_mul_X_pow_le f.leading_coeff f.nat_degree) Nf) }, { exact nat.lt_succ_iff.mp (gt_of_ge_of_gt Cf (erase_lead_support_card_lt f0)) }, { exact (le_trans erase_lead_nat_degree_le Nf) } }, end @[simp] theorem reflect_mul (f g : polynomial R) {F G : β„•} (Ff : f.nat_degree ≀ F) (Gg : g.nat_degree ≀ G) : reflect (F + G) (f * g) = reflect F f * reflect G g := reflect_mul_induction _ _ F G f g f.support.card.le_succ g.support.card.le_succ Ff Gg section evalβ‚‚ variables {S : Type*} [comm_semiring S] lemma evalβ‚‚_reflect_mul_pow (i : R β†’+* S) (x : S) [invertible x] (N : β„•) (f : polynomial R) (hf : f.nat_degree ≀ N) : evalβ‚‚ i (β…Ÿx) (reflect N f) * x ^ N = evalβ‚‚ i x f := begin refine induction_with_nat_degree_le (Ξ» f, evalβ‚‚ i (β…Ÿx) (reflect N f) * x ^ N = evalβ‚‚ i x f) _ _ _ _ f hf, { simp }, { intros n r hr0 hnN, simp only [rev_at_le hnN, reflect_C_mul_X_pow, evalβ‚‚_X_pow, evalβ‚‚_C, evalβ‚‚_mul], conv in (x ^ N) { rw [← nat.sub_add_cancel hnN] }, rw [pow_add, ← mul_assoc, mul_assoc (i r), ← mul_pow, inv_of_mul_self, one_pow, mul_one] }, { intros, simp [*, add_mul] } end lemma evalβ‚‚_reflect_eq_zero_iff (i : R β†’+* S) (x : S) [invertible x] (N : β„•) (f : polynomial R) (hf : f.nat_degree ≀ N) : evalβ‚‚ i (β…Ÿx) (reflect N f) = 0 ↔ evalβ‚‚ i x f = 0 := begin conv_rhs { rw [← evalβ‚‚_reflect_mul_pow i x N f hf] }, split, { intro h, rw [h, zero_mul] }, { intro h, rw [← mul_one (evalβ‚‚ i (β…Ÿx) _), ← one_pow N, ← mul_inv_of_self x, mul_pow, ← mul_assoc, h, zero_mul] } end end evalβ‚‚ /-- The reverse of a polynomial f is the polynomial obtained by "reading f backwards". Even though this is not the actual definition, reverse f = f (1/X) * X ^ f.nat_degree. -/ noncomputable def reverse (f : polynomial R) : polynomial R := reflect f.nat_degree f lemma coeff_reverse (f : polynomial R) (n : β„•) : f.reverse.coeff n = f.coeff (rev_at f.nat_degree n) := by rw [reverse, coeff_reflect] @[simp] lemma coeff_zero_reverse (f : polynomial R) : coeff (reverse f) 0 = leading_coeff f := by rw [coeff_reverse, rev_at_le (zero_le f.nat_degree), tsub_zero, leading_coeff] @[simp] lemma reverse_zero : reverse (0 : polynomial R) = 0 := rfl @[simp] lemma reverse_eq_zero : f.reverse = 0 ↔ f = 0 := by simp [reverse] lemma reverse_nat_degree_le (f : polynomial R) : f.reverse.nat_degree ≀ f.nat_degree := begin rw [nat_degree_le_iff_degree_le, degree_le_iff_coeff_zero], intros n hn, rw with_bot.coe_lt_coe at hn, rw [coeff_reverse, rev_at, function.embedding.coe_fn_mk, if_neg (not_le_of_gt hn), coeff_eq_zero_of_nat_degree_lt hn], end lemma nat_degree_eq_reverse_nat_degree_add_nat_trailing_degree (f : polynomial R) : f.nat_degree = f.reverse.nat_degree + f.nat_trailing_degree := begin by_cases hf : f = 0, { rw [hf, reverse_zero, nat_degree_zero, nat_trailing_degree_zero] }, apply le_antisymm, { refine tsub_le_iff_right.mp _, apply le_nat_degree_of_ne_zero, rw [reverse, coeff_reflect, ←rev_at_le f.nat_trailing_degree_le_nat_degree, rev_at_invol], exact trailing_coeff_nonzero_iff_nonzero.mpr hf }, { rw ← le_tsub_iff_left f.reverse_nat_degree_le, apply nat_trailing_degree_le_of_ne_zero, have key := mt leading_coeff_eq_zero.mp (mt reverse_eq_zero.mp hf), rwa [leading_coeff, coeff_reverse, rev_at_le f.reverse_nat_degree_le] at key }, end lemma reverse_nat_degree (f : polynomial R) : f.reverse.nat_degree = f.nat_degree - f.nat_trailing_degree := by rw [f.nat_degree_eq_reverse_nat_degree_add_nat_trailing_degree, add_tsub_cancel_right] lemma reverse_leading_coeff (f : polynomial R) : f.reverse.leading_coeff = f.trailing_coeff := by rw [leading_coeff, reverse_nat_degree, ←rev_at_le f.nat_trailing_degree_le_nat_degree, coeff_reverse, rev_at_invol, trailing_coeff] lemma reverse_nat_trailing_degree (f : polynomial R) : f.reverse.nat_trailing_degree = 0 := begin by_cases hf : f = 0, { rw [hf, reverse_zero, nat_trailing_degree_zero] }, { rw ← nat.le_zero_iff, apply nat_trailing_degree_le_of_ne_zero, rw [coeff_zero_reverse], exact mt leading_coeff_eq_zero.mp hf }, end lemma reverse_trailing_coeff (f : polynomial R) : f.reverse.trailing_coeff = f.leading_coeff := by rw [trailing_coeff, reverse_nat_trailing_degree, coeff_zero_reverse] theorem reverse_mul {f g : polynomial R} (fg : f.leading_coeff * g.leading_coeff β‰  0) : reverse (f * g) = reverse f * reverse g := begin unfold reverse, rw [nat_degree_mul' fg, reflect_mul f g rfl.le rfl.le], end @[simp] lemma reverse_mul_of_domain {R : Type*} [ring R] [is_domain R] (f g : polynomial R) : reverse (f * g) = reverse f * reverse g := begin by_cases f0 : f=0, { simp only [f0, zero_mul, reverse_zero], }, by_cases g0 : g=0, { rw [g0, mul_zero, reverse_zero, mul_zero], }, simp [reverse_mul, *], end lemma trailing_coeff_mul {R : Type*} [ring R] [is_domain R] (p q : polynomial R) : (p * q).trailing_coeff = p.trailing_coeff * q.trailing_coeff := by rw [←reverse_leading_coeff, reverse_mul_of_domain, leading_coeff_mul, reverse_leading_coeff, reverse_leading_coeff] @[simp] lemma coeff_one_reverse (f : polynomial R) : coeff (reverse f) 1 = next_coeff f := begin rw [coeff_reverse, next_coeff], split_ifs with hf, { have : coeff f 1 = 0 := coeff_eq_zero_of_nat_degree_lt (by simp only [hf, zero_lt_one]), simp [*, rev_at] }, { rw rev_at_le, exact nat.succ_le_iff.2 (pos_iff_ne_zero.2 hf) } end section evalβ‚‚ variables {S : Type*} [comm_semiring S] lemma evalβ‚‚_reverse_mul_pow (i : R β†’+* S) (x : S) [invertible x] (f : polynomial R) : evalβ‚‚ i (β…Ÿx) (reverse f) * x ^ f.nat_degree = evalβ‚‚ i x f := evalβ‚‚_reflect_mul_pow i _ _ f le_rfl @[simp] lemma evalβ‚‚_reverse_eq_zero_iff (i : R β†’+* S) (x : S) [invertible x] (f : polynomial R) : evalβ‚‚ i (β…Ÿx) (reverse f) = 0 ↔ evalβ‚‚ i x f = 0 := evalβ‚‚_reflect_eq_zero_iff i x _ _ le_rfl end evalβ‚‚ end semiring section ring variables {R : Type*} [ring R] @[simp] lemma reflect_neg (f : polynomial R) (N : β„•) : reflect N (- f) = - reflect N f := by rw [neg_eq_neg_one_mul, ←C_1, ←C_neg, reflect_C_mul, C_neg, C_1, ←neg_eq_neg_one_mul] @[simp] lemma reflect_sub (f g : polynomial R) (N : β„•) : reflect N (f - g) = reflect N f - reflect N g := by rw [sub_eq_add_neg, sub_eq_add_neg, reflect_add, reflect_neg] @[simp] lemma reverse_neg (f : polynomial R) : reverse (- f) = - reverse f := by rw [reverse, reverse, reflect_neg, nat_degree_neg] end ring end polynomial
7e724dabd00be5842925936de5ad5decffcc1c24
d1a52c3f208fa42c41df8278c3d280f075eb020c
/src/Lean/Elab.lean
cfb880690f591b190d89495ac9454b71c98aa5ab
[ "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
1,241
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Elab.Import import Lean.Elab.Exception import Lean.Elab.Config import Lean.Elab.Command import Lean.Elab.Term import Lean.Elab.App import Lean.Elab.Binders import Lean.Elab.LetRec import Lean.Elab.Frontend import Lean.Elab.BuiltinNotation import Lean.Elab.Declaration import Lean.Elab.Tactic import Lean.Elab.Match -- HACK: must come after `Match` because builtin elaborators (for `match` in this case) do not take priorities import Lean.Elab.Quotation import Lean.Elab.Syntax import Lean.Elab.Do import Lean.Elab.StructInst import Lean.Elab.Inductive import Lean.Elab.Structure import Lean.Elab.Print import Lean.Elab.MutualDef import Lean.Elab.AuxDef import Lean.Elab.PreDefinition import Lean.Elab.Deriving import Lean.Elab.DeclarationRange import Lean.Elab.Extra import Lean.Elab.GenInjective import Lean.Elab.BuiltinTerm import Lean.Elab.Arg import Lean.Elab.PatternVar import Lean.Elab.ElabRules import Lean.Elab.Macro import Lean.Elab.Notation import Lean.Elab.Mixfix import Lean.Elab.MacroRules import Lean.Elab.BuiltinCommand import Lean.Elab.RecAppSyntax
7b2a57c2afefaafa014b29ade9c4093e2e911b6d
05f637fa14ac28031cb1ea92086a0f4eb23ff2b1
/tests/lua/threads/deadlock.lean
48b8fbba7438b283c4413be331ca90c6a4d547de
[ "Apache-2.0" ]
permissive
codyroux/lean0.1
1ce92751d664aacff0529e139083304a7bbc8a71
0dc6fb974aa85ed6f305a2f4b10a53a44ee5f0ef
refs/heads/master
1,610,830,535,062
1,402,150,480,000
1,402,150,480,000
19,588,851
2
0
null
null
null
null
UTF-8
Lean
false
false
911
lean
(* import("util.lua") local ps = proof_state() local env = environment() local Bool = Const("Bool") env:add_var("p", Bool) env:add_var("q", Bool) local p, q = Consts("p, q") local ctx = context() ctx = ctx:extend("H1", p) ctx = ctx:extend("H2", q) ps = to_proof_state(env, ctx, p) print(ps) S1 = State() S2 = State() x = 10 function tst_fn(env, ios, s) x = x + 1 print(x) return s end t = tactic(tst_fn) S1:dostring([[ x = 20; t, ps = ... ]], t, ps) S2:dostring([[ x = 20; t, ps = ... ]], t, ps) T1 = thread(S1, [[ local ios = io_state() local env = environment() for i = 1, 10 do for s in t(env, ios, ps) do print("s1") print(s) end end ]]) T2 = thread(S2, [[ local ios = io_state() local env = environment() for i = 1, 10 do for s in t(env, ios, ps) do print("s2") print(s) end end ]]) T1:wait() T2:wait() *)
5db63d6b54240a51747c71952f06eba593b9c5b5
624f6f2ae8b3b1adc5f8f67a365c51d5126be45a
/src/Init/Lean/Util/Profile.lean
7f0585584d99d601d2886998034a30fe6afd1837
[ "Apache-2.0" ]
permissive
mhuisi/lean4
28d35a4febc2e251c7f05492e13f3b05d6f9b7af
dda44bc47f3e5d024508060dac2bcb59fd12e4c0
refs/heads/master
1,621,225,489,283
1,585,142,689,000
1,585,142,689,000
250,590,438
0
2
Apache-2.0
1,602,443,220,000
1,585,327,814,000
C
UTF-8
Lean
false
false
608
lean
/- Copyright (c) 2019 Sebastian Ullrich. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Sebastian Ullrich -/ prelude import Init.System.IO import Init.Lean.Data.Position namespace Lean /-- Print and accumulate run time of `act` when Option `profiler` is set to `true`. -/ @[extern 5 "lean_lean_profileit"] constant profileit {Ξ± : Type} (category : @& String) (pos : @& Position) (act : IO Ξ±) : IO Ξ± := act def profileitPure {Ξ± : Type} (category : String) (pos : Position) (fn : Unit β†’ Ξ±) : IO Ξ± := profileit category pos $ IO.lazyPure fn end Lean
2aaa9920bba6fdf996cd8f31c8e4301a5f0aec6f
f1dc39e1c68f71465c8bf79910c4664d03824751
/library/init/algebra/ordered_group.lean
7018fa281537834b5824fb3cd63880413f1d5641
[ "Apache-2.0" ]
permissive
kckennylau/lean-2
6504f45da07bc98b098d726b74130103be25885c
c9a9368bc0fd600d832bd56c5cb2124b8a523ef9
refs/heads/master
1,659,140,308,864
1,589,361,166,000
1,589,361,166,000
263,748,786
0
0
null
1,589,405,915,000
1,589,405,915,000
null
UTF-8
Lean
false
false
20,605
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 -/ prelude import init.algebra.order init.algebra.group /- Make sure instances defined in this file have lower priority than the ones defined for concrete structures -/ set_option default_priority 100 set_option old_structure_cmd true universe u class ordered_cancel_add_comm_monoid (Ξ± : Type u) extends add_comm_monoid Ξ±, add_left_cancel_semigroup Ξ±, add_right_cancel_semigroup Ξ±, 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) section ordered_cancel_add_comm_monoid variable {Ξ± : Type u} variable [s : ordered_cancel_add_comm_monoid Ξ±] lemma add_le_add_left {a b : Ξ±} (h : a ≀ b) (c : Ξ±) : c + a ≀ c + b := @ordered_cancel_add_comm_monoid.add_le_add_left Ξ± s a b h c lemma le_of_add_le_add_left {a b c : Ξ±} (h : a + b ≀ a + c) : b ≀ c := @ordered_cancel_add_comm_monoid.le_of_add_le_add_left Ξ± s a b c h end ordered_cancel_add_comm_monoid section ordered_cancel_add_comm_monoid variable {Ξ± : Type u} variable [ordered_cancel_add_comm_monoid Ξ±] lemma add_lt_add_left {a b : Ξ±} (h : a < b) (c : Ξ±) : c + a < c + b := lt_of_le_not_le (add_le_add_left (le_of_lt h) _) $ mt le_of_add_le_add_left (not_le_of_gt h) lemma lt_of_add_lt_add_left {a b c : Ξ±} (h : a + b < a + c) : b < c := lt_of_le_not_le (le_of_add_le_add_left (le_of_lt h)) $ mt (Ξ» h, add_le_add_left h _) (not_le_of_gt h) lemma add_le_add_right {a b : Ξ±} (h : a ≀ b) (c : Ξ±) : a + c ≀ b + c := add_comm c a β–Έ add_comm c b β–Έ add_le_add_left h c theorem add_lt_add_right {a b : Ξ±} (h : a < b) (c : Ξ±) : a + c < b + c := begin rw [add_comm a c, add_comm b c], exact (add_lt_add_left h c) end lemma add_le_add {a b c d : Ξ±} (h₁ : a ≀ b) (hβ‚‚ : c ≀ d) : a + c ≀ b + d := le_trans (add_le_add_right h₁ c) (add_le_add_left hβ‚‚ b) lemma le_add_of_nonneg_right {a b : Ξ±} (h : b β‰₯ 0) : a ≀ a + b := have a + b β‰₯ a + 0, from add_le_add_left h a, by rwa add_zero at this lemma le_add_of_nonneg_left {a b : Ξ±} (h : b β‰₯ 0) : a ≀ b + a := have 0 + a ≀ b + a, from add_le_add_right h a, by rwa zero_add at this lemma add_lt_add {a b c d : Ξ±} (h₁ : a < b) (hβ‚‚ : c < d) : a + c < b + d := lt_trans (add_lt_add_right h₁ c) (add_lt_add_left hβ‚‚ b) lemma add_lt_add_of_le_of_lt {a b c d : Ξ±} (h₁ : a ≀ b) (hβ‚‚ : c < d) : a + c < b + d := lt_of_le_of_lt (add_le_add_right h₁ c) (add_lt_add_left hβ‚‚ b) lemma add_lt_add_of_lt_of_le {a b c d : Ξ±} (h₁ : a < b) (hβ‚‚ : c ≀ d) : a + c < b + d := lt_of_lt_of_le (add_lt_add_right h₁ c) (add_le_add_left hβ‚‚ b) lemma lt_add_of_pos_right (a : Ξ±) {b : Ξ±} (h : b > 0) : a < a + b := have a + 0 < a + b, from add_lt_add_left h a, by rwa [add_zero] at this lemma lt_add_of_pos_left (a : Ξ±) {b : Ξ±} (h : b > 0) : a < b + a := have 0 + a < b + a, from add_lt_add_right h a, by rwa [zero_add] at this lemma le_of_add_le_add_right {a b c : Ξ±} (h : a + b ≀ c + b) : a ≀ c := le_of_add_le_add_left (show b + a ≀ b + c, begin rw [add_comm b a, add_comm b c], assumption end) lemma lt_of_add_lt_add_right {a b c : Ξ±} (h : a + b < c + b) : a < c := lt_of_add_lt_add_left (show b + a < b + c, begin rw [add_comm b a, add_comm b c], assumption end) -- here we start using properties of zero. lemma add_nonneg {a b : Ξ±} (ha : 0 ≀ a) (hb : 0 ≀ b) : 0 ≀ a + b := zero_add (0:Ξ±) β–Έ (add_le_add ha hb) lemma add_pos {a b : Ξ±} (ha : 0 < a) (hb : 0 < b) : 0 < a + b := zero_add (0:Ξ±) β–Έ (add_lt_add ha hb) lemma add_pos_of_pos_of_nonneg {a b : Ξ±} (ha : 0 < a) (hb : 0 ≀ b) : 0 < a + b := zero_add (0:Ξ±) β–Έ (add_lt_add_of_lt_of_le ha hb) lemma add_pos_of_nonneg_of_pos {a b : Ξ±} (ha : 0 ≀ a) (hb : 0 < b) : 0 < a + b := zero_add (0:Ξ±) β–Έ (add_lt_add_of_le_of_lt ha hb) lemma add_nonpos {a b : Ξ±} (ha : a ≀ 0) (hb : b ≀ 0) : a + b ≀ 0 := zero_add (0:Ξ±) β–Έ (add_le_add ha hb) lemma add_neg {a b : Ξ±} (ha : a < 0) (hb : b < 0) : a + b < 0 := zero_add (0:Ξ±) β–Έ (add_lt_add ha hb) lemma add_neg_of_neg_of_nonpos {a b : Ξ±} (ha : a < 0) (hb : b ≀ 0) : a + b < 0 := zero_add (0:Ξ±) β–Έ (add_lt_add_of_lt_of_le ha hb) lemma add_neg_of_nonpos_of_neg {a b : Ξ±} (ha : a ≀ 0) (hb : b < 0) : a + b < 0 := zero_add (0:Ξ±) β–Έ (add_lt_add_of_le_of_lt ha hb) lemma add_eq_zero_iff_eq_zero_and_eq_zero_of_nonneg_of_nonneg {a b : Ξ±} (ha : 0 ≀ a) (hb : 0 ≀ b) : a + b = 0 ↔ a = 0 ∧ b = 0 := iff.intro (assume hab : a + b = 0, have ha' : a ≀ 0, from calc a = a + 0 : by rw add_zero ... ≀ a + b : add_le_add_left hb _ ... = 0 : hab, have haz : a = 0, from le_antisymm ha' ha, have hb' : b ≀ 0, from calc b = 0 + b : by rw zero_add ... ≀ a + b : by exact add_le_add_right ha _ ... = 0 : hab, have hbz : b = 0, from le_antisymm hb' hb, and.intro haz hbz) (assume ⟨ha', hb'⟩, by rw [ha', hb', add_zero]) lemma le_add_of_nonneg_of_le {a b c : Ξ±} (ha : 0 ≀ a) (hbc : b ≀ c) : b ≀ a + c := zero_add b β–Έ add_le_add ha hbc lemma le_add_of_le_of_nonneg {a b c : Ξ±} (hbc : b ≀ c) (ha : 0 ≀ a) : b ≀ c + a := add_zero b β–Έ add_le_add hbc ha lemma lt_add_of_pos_of_le {a b c : Ξ±} (ha : 0 < a) (hbc : b ≀ c) : b < a + c := zero_add b β–Έ add_lt_add_of_lt_of_le ha hbc lemma lt_add_of_le_of_pos {a b c : Ξ±} (hbc : b ≀ c) (ha : 0 < a) : b < c + a := add_zero b β–Έ add_lt_add_of_le_of_lt hbc ha lemma add_le_of_nonpos_of_le {a b c : Ξ±} (ha : a ≀ 0) (hbc : b ≀ c) : a + b ≀ c := zero_add c β–Έ add_le_add ha hbc lemma add_le_of_le_of_nonpos {a b c : Ξ±} (hbc : b ≀ c) (ha : a ≀ 0) : b + a ≀ c := add_zero c β–Έ add_le_add hbc ha lemma add_lt_of_neg_of_le {a b c : Ξ±} (ha : a < 0) (hbc : b ≀ c) : a + b < c := zero_add c β–Έ add_lt_add_of_lt_of_le ha hbc lemma add_lt_of_le_of_neg {a b c : Ξ±} (hbc : b ≀ c) (ha : a < 0) : b + a < c := add_zero c β–Έ add_lt_add_of_le_of_lt hbc ha lemma lt_add_of_nonneg_of_lt {a b c : Ξ±} (ha : 0 ≀ a) (hbc : b < c) : b < a + c := zero_add b β–Έ add_lt_add_of_le_of_lt ha hbc lemma lt_add_of_lt_of_nonneg {a b c : Ξ±} (hbc : b < c) (ha : 0 ≀ a) : b < c + a := add_zero b β–Έ add_lt_add_of_lt_of_le hbc ha lemma lt_add_of_pos_of_lt {a b c : Ξ±} (ha : 0 < a) (hbc : b < c) : b < a + c := zero_add b β–Έ add_lt_add ha hbc lemma lt_add_of_lt_of_pos {a b c : Ξ±} (hbc : b < c) (ha : 0 < a) : b < c + a := add_zero b β–Έ add_lt_add hbc ha lemma add_lt_of_nonpos_of_lt {a b c : Ξ±} (ha : a ≀ 0) (hbc : b < c) : a + b < c := zero_add c β–Έ add_lt_add_of_le_of_lt ha hbc lemma add_lt_of_lt_of_nonpos {a b c : Ξ±} (hbc : b < c) (ha : a ≀ 0) : b + a < c := add_zero c β–Έ add_lt_add_of_lt_of_le hbc ha lemma add_lt_of_neg_of_lt {a b c : Ξ±} (ha : a < 0) (hbc : b < c) : a + b < c := zero_add c β–Έ add_lt_add ha hbc lemma add_lt_of_lt_of_neg {a b c : Ξ±} (hbc : b < c) (ha : a < 0) : b + a < c := add_zero c β–Έ add_lt_add hbc ha end ordered_cancel_add_comm_monoid class ordered_add_comm_group (Ξ± : Type u) extends add_comm_group Ξ±, partial_order Ξ± := (add_le_add_left : βˆ€ a b : Ξ±, a ≀ b β†’ βˆ€ c : Ξ±, c + a ≀ c + b) section ordered_add_comm_group variable {Ξ± : Type u} variable [ordered_add_comm_group Ξ±] lemma ordered_add_comm_group.add_lt_add_left (a b : Ξ±) (h : a < b) (c : Ξ±) : c + a < c + b := begin rw lt_iff_le_not_le at h ⊒, split, { apply ordered_add_comm_group.add_le_add_left _ _ h.1 }, { intro w, have w : -c + (c + b) ≀ -c + (c + a) := ordered_add_comm_group.add_le_add_left _ _ w _, simp only [add_zero, add_comm, add_left_neg, add_left_comm] at w, exact h.2 w }, end lemma ordered_add_comm_group.le_of_add_le_add_left {a b c : Ξ±} (h : a + b ≀ a + c) : b ≀ c := have -a + (a + b) ≀ -a + (a + c), from ordered_add_comm_group.add_le_add_left _ _ h _, begin simp [neg_add_cancel_left] at this, assumption end lemma ordered_add_comm_group.lt_of_add_lt_add_left {a b c : Ξ±} (h : a + b < a + c) : b < c := have -a + (a + b) < -a + (a + c), from ordered_add_comm_group.add_lt_add_left _ _ h _, begin simp [neg_add_cancel_left] at this, assumption end end ordered_add_comm_group instance ordered_add_comm_group.to_ordered_cancel_add_comm_monoid (Ξ± : Type u) [s : ordered_add_comm_group Ξ±] : ordered_cancel_add_comm_monoid Ξ± := { add_left_cancel := @add_left_cancel Ξ± _, add_right_cancel := @add_right_cancel Ξ± _, le_of_add_le_add_left := @ordered_add_comm_group.le_of_add_le_add_left Ξ± _, ..s } section ordered_add_comm_group variables {Ξ± : Type u} [ordered_add_comm_group Ξ±] lemma neg_le_neg {a b : Ξ±} (h : a ≀ b) : -b ≀ -a := have 0 ≀ -a + b, from add_left_neg a β–Έ add_le_add_left h (-a), have 0 + -b ≀ -a + b + -b, from add_le_add_right this (-b), by rwa [add_neg_cancel_right, zero_add] at this lemma le_of_neg_le_neg {a b : Ξ±} (h : -b ≀ -a) : a ≀ b := suffices -(-a) ≀ -(-b), from begin simp [neg_neg] at this, assumption end, neg_le_neg h lemma nonneg_of_neg_nonpos {a : Ξ±} (h : -a ≀ 0) : 0 ≀ a := have -a ≀ -0, by rwa neg_zero, le_of_neg_le_neg this lemma neg_nonpos_of_nonneg {a : Ξ±} (h : 0 ≀ a) : -a ≀ 0 := have -a ≀ -0, from neg_le_neg h, by rwa neg_zero at this lemma nonpos_of_neg_nonneg {a : Ξ±} (h : 0 ≀ -a) : a ≀ 0 := have -0 ≀ -a, by rwa neg_zero, le_of_neg_le_neg this lemma neg_nonneg_of_nonpos {a : Ξ±} (h : a ≀ 0) : 0 ≀ -a := have -0 ≀ -a, from neg_le_neg h, by rwa neg_zero at this lemma neg_lt_neg {a b : Ξ±} (h : a < b) : -b < -a := have 0 < -a + b, from add_left_neg a β–Έ add_lt_add_left h (-a), have 0 + -b < -a + b + -b, from add_lt_add_right this (-b), by rwa [add_neg_cancel_right, zero_add] at this lemma lt_of_neg_lt_neg {a b : Ξ±} (h : -b < -a) : a < b := neg_neg a β–Έ neg_neg b β–Έ neg_lt_neg h lemma pos_of_neg_neg {a : Ξ±} (h : -a < 0) : 0 < a := have -a < -0, by rwa neg_zero, lt_of_neg_lt_neg this lemma neg_neg_of_pos {a : Ξ±} (h : 0 < a) : -a < 0 := have -a < -0, from neg_lt_neg h, by rwa neg_zero at this lemma neg_of_neg_pos {a : Ξ±} (h : 0 < -a) : a < 0 := have -0 < -a, by rwa neg_zero, lt_of_neg_lt_neg this lemma neg_pos_of_neg {a : Ξ±} (h : a < 0) : 0 < -a := have -0 < -a, from neg_lt_neg h, by rwa neg_zero at this lemma le_neg_of_le_neg {a b : Ξ±} (h : a ≀ -b) : b ≀ -a := begin have h := neg_le_neg h, rwa neg_neg at h end lemma neg_le_of_neg_le {a b : Ξ±} (h : -a ≀ b) : -b ≀ a := begin have h := neg_le_neg h, rwa neg_neg at h end lemma lt_neg_of_lt_neg {a b : Ξ±} (h : a < -b) : b < -a := begin have h := neg_lt_neg h, rwa neg_neg at h end lemma neg_lt_of_neg_lt {a b : Ξ±} (h : -a < b) : -b < a := begin have h := neg_lt_neg h, rwa neg_neg at h end lemma sub_nonneg_of_le {a b : Ξ±} (h : b ≀ a) : 0 ≀ a - b := begin have h := add_le_add_right h (-b), rwa add_right_neg at h end lemma le_of_sub_nonneg {a b : Ξ±} (h : 0 ≀ a - b) : b ≀ a := begin have h := add_le_add_right h b, rwa [sub_add_cancel, zero_add] at h end lemma sub_nonpos_of_le {a b : Ξ±} (h : a ≀ b) : a - b ≀ 0 := begin have h := add_le_add_right h (-b), rwa add_right_neg at h end lemma le_of_sub_nonpos {a b : Ξ±} (h : a - b ≀ 0) : a ≀ b := begin have h := add_le_add_right h b, rwa [sub_add_cancel, zero_add] at h end lemma sub_pos_of_lt {a b : Ξ±} (h : b < a) : 0 < a - b := begin have h := add_lt_add_right h (-b), rwa add_right_neg at h end lemma lt_of_sub_pos {a b : Ξ±} (h : 0 < a - b) : b < a := begin have h := add_lt_add_right h b, rwa [sub_add_cancel, zero_add] at h end lemma sub_neg_of_lt {a b : Ξ±} (h : a < b) : a - b < 0 := begin have h := add_lt_add_right h (-b), rwa add_right_neg at h end lemma lt_of_sub_neg {a b : Ξ±} (h : a - b < 0) : a < b := begin have h := add_lt_add_right h b, rwa [sub_add_cancel, zero_add] at h end lemma add_le_of_le_neg_add {a b c : Ξ±} (h : b ≀ -a + c) : a + b ≀ c := begin have h := add_le_add_left h a, rwa add_neg_cancel_left at h end lemma le_neg_add_of_add_le {a b c : Ξ±} (h : a + b ≀ c) : b ≀ -a + c := begin have h := add_le_add_left h (-a), rwa neg_add_cancel_left at h end lemma add_le_of_le_sub_left {a b c : Ξ±} (h : b ≀ c - a) : a + b ≀ c := begin have h := add_le_add_left h a, rwa [← add_sub_assoc, add_comm a c, add_sub_cancel] at h end lemma le_sub_left_of_add_le {a b c : Ξ±} (h : a + b ≀ c) : b ≀ c - a := begin have h := add_le_add_right h (-a), rwa [add_comm a b, add_neg_cancel_right] at h end lemma add_le_of_le_sub_right {a b c : Ξ±} (h : a ≀ c - b) : a + b ≀ c := begin have h := add_le_add_right h b, rwa sub_add_cancel at h end lemma le_sub_right_of_add_le {a b c : Ξ±} (h : a + b ≀ c) : a ≀ c - b := begin have h := add_le_add_right h (-b), rwa add_neg_cancel_right at h end lemma le_add_of_neg_add_le {a b c : Ξ±} (h : -b + a ≀ c) : a ≀ b + c := begin have h := add_le_add_left h b, rwa add_neg_cancel_left at h end lemma neg_add_le_of_le_add {a b c : Ξ±} (h : a ≀ b + c) : -b + a ≀ c := begin have h := add_le_add_left h (-b), rwa neg_add_cancel_left at h end lemma le_add_of_sub_left_le {a b c : Ξ±} (h : a - b ≀ c) : a ≀ b + c := begin have h := add_le_add_right h b, rwa [sub_add_cancel, add_comm] at h end lemma sub_left_le_of_le_add {a b c : Ξ±} (h : a ≀ b + c) : a - b ≀ c := begin have h := add_le_add_right h (-b), rwa [add_comm b c, add_neg_cancel_right] at h end lemma le_add_of_sub_right_le {a b c : Ξ±} (h : a - c ≀ b) : a ≀ b + c := begin have h := add_le_add_right h c, rwa sub_add_cancel at h end lemma sub_right_le_of_le_add {a b c : Ξ±} (h : a ≀ b + c) : a - c ≀ b := begin have h := add_le_add_right h (-c), rwa add_neg_cancel_right at h end lemma le_add_of_neg_add_le_left {a b c : Ξ±} (h : -b + a ≀ c) : a ≀ b + c := begin rw add_comm at h, exact le_add_of_sub_left_le h end lemma neg_add_le_left_of_le_add {a b c : Ξ±} (h : a ≀ b + c) : -b + a ≀ c := begin rw add_comm, exact sub_left_le_of_le_add h end lemma le_add_of_neg_add_le_right {a b c : Ξ±} (h : -c + a ≀ b) : a ≀ b + c := begin rw add_comm at h, exact le_add_of_sub_right_le h end lemma neg_add_le_right_of_le_add {a b c : Ξ±} (h : a ≀ b + c) : -c + a ≀ b := begin rw add_comm at h, apply neg_add_le_left_of_le_add h end lemma le_add_of_neg_le_sub_left {a b c : Ξ±} (h : -a ≀ b - c) : c ≀ a + b := le_add_of_neg_add_le_left (add_le_of_le_sub_right h) lemma neg_le_sub_left_of_le_add {a b c : Ξ±} (h : c ≀ a + b) : -a ≀ b - c := begin have h := le_neg_add_of_add_le (sub_left_le_of_le_add h), rwa add_comm at h end lemma le_add_of_neg_le_sub_right {a b c : Ξ±} (h : -b ≀ a - c) : c ≀ a + b := le_add_of_sub_right_le (add_le_of_le_sub_left h) lemma neg_le_sub_right_of_le_add {a b c : Ξ±} (h : c ≀ a + b) : -b ≀ a - c := le_sub_left_of_add_le (sub_right_le_of_le_add h) lemma sub_le_of_sub_le {a b c : Ξ±} (h : a - b ≀ c) : a - c ≀ b := sub_left_le_of_le_add (le_add_of_sub_right_le h) lemma sub_le_sub_left {a b : Ξ±} (h : a ≀ b) (c : Ξ±) : c - b ≀ c - a := add_le_add_left (neg_le_neg h) c lemma sub_le_sub_right {a b : Ξ±} (h : a ≀ b) (c : Ξ±) : a - c ≀ b - c := add_le_add_right h (-c) lemma sub_le_sub {a b c d : Ξ±} (hab : a ≀ b) (hcd : c ≀ d) : a - d ≀ b - c := add_le_add hab (neg_le_neg hcd) lemma add_lt_of_lt_neg_add {a b c : Ξ±} (h : b < -a + c) : a + b < c := begin have h := add_lt_add_left h a, rwa add_neg_cancel_left at h end lemma lt_neg_add_of_add_lt {a b c : Ξ±} (h : a + b < c) : b < -a + c := begin have h := add_lt_add_left h (-a), rwa neg_add_cancel_left at h end lemma add_lt_of_lt_sub_left {a b c : Ξ±} (h : b < c - a) : a + b < c := begin have h := add_lt_add_left h a, rwa [← add_sub_assoc, add_comm a c, add_sub_cancel] at h end lemma lt_sub_left_of_add_lt {a b c : Ξ±} (h : a + b < c) : b < c - a := begin have h := add_lt_add_right h (-a), rwa [add_comm a b, add_neg_cancel_right] at h end lemma add_lt_of_lt_sub_right {a b c : Ξ±} (h : a < c - b) : a + b < c := begin have h := add_lt_add_right h b, rwa sub_add_cancel at h end lemma lt_sub_right_of_add_lt {a b c : Ξ±} (h : a + b < c) : a < c - b := begin have h := add_lt_add_right h (-b), rwa add_neg_cancel_right at h end lemma lt_add_of_neg_add_lt {a b c : Ξ±} (h : -b + a < c) : a < b + c := begin have h := add_lt_add_left h b, rwa add_neg_cancel_left at h end lemma neg_add_lt_of_lt_add {a b c : Ξ±} (h : a < b + c) : -b + a < c := begin have h := add_lt_add_left h (-b), rwa neg_add_cancel_left at h end lemma lt_add_of_sub_left_lt {a b c : Ξ±} (h : a - b < c) : a < b + c := begin have h := add_lt_add_right h b, rwa [sub_add_cancel, add_comm] at h end lemma sub_left_lt_of_lt_add {a b c : Ξ±} (h : a < b + c) : a - b < c := begin have h := add_lt_add_right h (-b), rwa [add_comm b c, add_neg_cancel_right] at h end lemma lt_add_of_sub_right_lt {a b c : Ξ±} (h : a - c < b) : a < b + c := begin have h := add_lt_add_right h c, rwa sub_add_cancel at h end lemma sub_right_lt_of_lt_add {a b c : Ξ±} (h : a < b + c) : a - c < b := begin have h := add_lt_add_right h (-c), rwa add_neg_cancel_right at h end lemma lt_add_of_neg_add_lt_left {a b c : Ξ±} (h : -b + a < c) : a < b + c := begin rw add_comm at h, exact lt_add_of_sub_left_lt h end lemma neg_add_lt_left_of_lt_add {a b c : Ξ±} (h : a < b + c) : -b + a < c := begin rw add_comm, exact sub_left_lt_of_lt_add h end lemma lt_add_of_neg_add_lt_right {a b c : Ξ±} (h : -c + a < b) : a < b + c := begin rw add_comm at h, exact lt_add_of_sub_right_lt h end lemma neg_add_lt_right_of_lt_add {a b c : Ξ±} (h : a < b + c) : -c + a < b := begin rw add_comm at h, apply neg_add_lt_left_of_lt_add h end lemma lt_add_of_neg_lt_sub_left {a b c : Ξ±} (h : -a < b - c) : c < a + b := lt_add_of_neg_add_lt_left (add_lt_of_lt_sub_right h) lemma neg_lt_sub_left_of_lt_add {a b c : Ξ±} (h : c < a + b) : -a < b - c := begin have h := lt_neg_add_of_add_lt (sub_left_lt_of_lt_add h), rwa add_comm at h end lemma lt_add_of_neg_lt_sub_right {a b c : Ξ±} (h : -b < a - c) : c < a + b := lt_add_of_sub_right_lt (add_lt_of_lt_sub_left h) lemma neg_lt_sub_right_of_lt_add {a b c : Ξ±} (h : c < a + b) : -b < a - c := lt_sub_left_of_add_lt (sub_right_lt_of_lt_add h) lemma sub_lt_of_sub_lt {a b c : Ξ±} (h : a - b < c) : a - c < b := sub_left_lt_of_lt_add (lt_add_of_sub_right_lt h) lemma sub_lt_sub_left {a b : Ξ±} (h : a < b) (c : Ξ±) : c - b < c - a := add_lt_add_left (neg_lt_neg h) c lemma sub_lt_sub_right {a b : Ξ±} (h : a < b) (c : Ξ±) : a - c < b - c := add_lt_add_right h (-c) lemma sub_lt_sub {a b c d : Ξ±} (hab : a < b) (hcd : c < d) : a - d < b - c := add_lt_add hab (neg_lt_neg hcd) lemma sub_lt_sub_of_le_of_lt {a b c d : Ξ±} (hab : a ≀ b) (hcd : c < d) : a - d < b - c := add_lt_add_of_le_of_lt hab (neg_lt_neg hcd) lemma sub_lt_sub_of_lt_of_le {a b c d : Ξ±} (hab : a < b) (hcd : c ≀ d) : a - d < b - c := add_lt_add_of_lt_of_le hab (neg_le_neg hcd) lemma sub_le_self (a : Ξ±) {b : Ξ±} (h : b β‰₯ 0) : a - b ≀ a := calc a - b = a + -b : rfl ... ≀ a + 0 : add_le_add_left (neg_nonpos_of_nonneg h) _ ... = a : by rw add_zero lemma sub_lt_self (a : Ξ±) {b : Ξ±} (h : b > 0) : a - b < a := calc a - b = a + -b : rfl ... < a + 0 : add_lt_add_left (neg_neg_of_pos h) _ ... = a : by rw add_zero lemma add_le_add_three {a b c d e f : Ξ±} (h₁ : a ≀ d) (hβ‚‚ : b ≀ e) (h₃ : c ≀ f) : a + b + c ≀ d + e + f := begin apply le_trans, apply add_le_add, apply add_le_add, assumption', apply le_refl end end ordered_add_comm_group class decidable_linear_ordered_add_comm_group (Ξ± : Type u) extends add_comm_group Ξ±, decidable_linear_order Ξ± := (add_le_add_left : βˆ€ a b : Ξ±, a ≀ b β†’ βˆ€ c : Ξ±, c + a ≀ c + b) instance decidable_linear_ordered_comm_group.to_ordered_add_comm_group (Ξ± : Type u) [s : decidable_linear_ordered_add_comm_group Ξ±] : ordered_add_comm_group Ξ± := { add := s.add, ..s } class decidable_linear_ordered_cancel_add_comm_monoid (Ξ± : Type u) extends ordered_cancel_add_comm_monoid Ξ±, decidable_linear_order Ξ± lemma decidable_linear_ordered_add_comm_group.add_lt_add_left {Ξ±} [decidable_linear_ordered_add_comm_group Ξ±] (a b : Ξ±) (h : a < b) (c : Ξ±) : c + a < c + b := ordered_add_comm_group.add_lt_add_left a b h c
7c05aa15bfdc1122bc75653ce3387d9618a98b91
5382d69a781e8d7e4f53e2358896eb7649c9b298
/posets.lean
c3c92c3ed7c106e860ee5c355abd3c47b7afaf77
[]
no_license
evhub/lean-math-examples
c30249747a21fba3bc8793eba4928db47cf28768
dec44bf581a1e9d5bf0b5261803a43fe8fd350e1
refs/heads/master
1,624,170,837,738
1,623,889,725,000
1,623,889,725,000
148,759,369
3
0
null
null
null
null
UTF-8
Lean
false
false
15,986
lean
import .util namespace posets open function open classical (em prop_decidable) open classical (renaming some β†’ unexists) (renaming some_spec β†’ unexists_prop) local attribute [instance] prop_decidable open util -- comparability: inductive comp {T: Sort _} [hT: partial_order T] (x y: T): Prop | le (hle: x ≀ y): comp | ge (hge: y ≀ x): comp infix ` <=> `:50 := comp @[refl] theorem comp.refl {T: Sort _} [hT: partial_order T] (x: T): x <=> x := begin apply comp.le, refl, end @[symm] theorem comp.symm {T: Sort _} [hT: partial_order T] {x y: T}: x <=> y β†’ y <=> x := begin intro hcomp, induction hcomp, case comp.le { apply comp.ge, exact hcomp, }, case comp.ge { apply comp.le, exact hcomp, }, end theorem le_of_not_lt {T: Sort _} [hT: partial_order T] {x y: T} (hcomp: x <=> y): Β¬ x < y β†’ y ≀ x := begin intro hnlt, cases hcomp, case comp.le { rw [le_iff_lt_or_eq] at hcomp, cases hcomp, case or.inl { contradiction, }, case or.inr { rw [hcomp], }, }, case comp.ge { exact hcomp, }, end theorem lt_of_not_le {T: Sort _} [hT: partial_order T] {x y: T} (hcomp: x <=> y): Β¬ x ≀ y β†’ y < x := begin intro hnlt, cases hcomp, case comp.le { contradiction, }, case comp.ge { by_contra hnlt, have hyx: comp y x := comp.le hcomp, have hle := le_of_not_lt hyx hnlt, contradiction, }, end -- connectedness: inductive con {T: Sort _} [hT: partial_order T]: T β†’ T β†’ Prop | le {x y: T} (hle: x ≀ y): con x y | ge {x y: T} (hge: y ≀ x): con x y | trans {x y z: T} (hxy: con x y) (hyz: con y z): con x z attribute [trans] con.trans infix ` ~ ` := con @[refl] theorem con.refl {T: Sort _} [hT: partial_order T] (x: T): x ~ x := begin intros, apply con.le, refl, end @[symm] theorem con.symm {T: Sort _} [hT: partial_order T] {x y: T}: x ~ y β†’ y ~ x := begin intro hcon, induction hcon, case con.le { apply con.ge, assumption, }, case con.ge { apply con.le, assumption, }, case con.trans { exact con.trans hcon_ih_hyz hcon_ih_hxy, }, end theorem comp.con {T: Sort _} [hT: partial_order T] {x y: T} (hcomp: x <=> y): x ~ y := begin induction hcomp, case comp.le { apply con.le, exact hcomp, }, case comp.ge { apply con.ge, exact hcomp, }, end -- order with bottom: class bot_order (T: Sort _) extends partial_order T := (bot: T) (bot_le: βˆ€ x: T, bot ≀ x) @[reducible, inline] def bot {T: Sort _} [hT: bot_order T] := hT.bot @[reducible, inline] def bot_le {T: Sort _} [hT: bot_order T] := hT.bot_le instance bot_order.inhabited {T: Sort _} [hT: bot_order T]: inhabited T := (| bot |) theorem bot_uniq {T: Sort _} [hT: bot_order T]: βˆ€ bot': T, (βˆ€ x: T, bot' ≀ x) β†’ bot' = bot := begin intros bot' bot_le', apply le_antisymm, apply bot_le', apply bot_le, end theorem bot_ne_elim {T: Sort _} [hT: bot_order T] {x: T}: x β‰  bot β†’ βˆƒ y: T, Β¬ x ≀ y := begin intro hne, apply not_forall_not_elim, intro hnlt, apply hne, apply bot_uniq, intro y, have hy := hnlt y, simp at hy, exact hy, end theorem con.all_of_bot {T: Sort _} [hT: bot_order T] {x y: T}: x ~ y := begin have hx: x ~ bot, by { apply con.ge, apply bot_le, }, have hy: bot ~ y, by { apply con.le, apply bot_le, }, exact con.trans hx hy, end -- trivial order: @[reducible] def trivially_ordered (T: Sort _) [hT: partial_order T]: Prop := βˆ€ {x y: T}, x ≀ y β†’ x = y @[simp] theorem trivially_ordered.elim {T: Sort _} [hT: partial_order T] (htriv: trivially_ordered T): βˆ€ {x y: T}, x ≀ y ↔ x = y := begin intros, apply iff.intro, { intro h, exact htriv h, }, { intro h, rw [h], }, end @[simp] theorem trivially_ordered.of_bot {T: Sort _} [hT: bot_order T] (htriv: trivially_ordered T): βˆ€ {x: T}, x = bot := begin intros, symmetry, apply htriv, apply bot_le, end -- not marked as instance to avoid automatically trivially ordering everything def trivial_ordering (T: Sort _): partial_order T := { le := eq, le_refl := begin intros, refl, end, le_trans := begin intros x y z hxy hyz, rw [hxy, ←hyz], end, le_antisymm := begin intros, assumption, end, } theorem trivial_ordering.is_triv (T: Sort _): @trivially_ordered T (trivial_ordering T) := begin intros x y, intro hxy, cases hxy, refl, end -- min: noncomputable def or_else {T: Sort _} [hT: bot_order T] (x y: T): T := if x = bot then y else x infix ` ?? `:60 := or_else theorem or_else.le_refl {T: Sort _} [hT: bot_order T] {x y: T}: x ≀ x ?? y := begin rw [or_else], cases em (x = bot), case or.inl { rw [if_pos h, h], apply bot_le, }, case or.inr { rw [if_neg h], }, end theorem or_else.le_of_le {T: Sort _} [hT: bot_order T] {x y: T}: x ≀ y β†’ x ≀ y ?? x := begin intro hle, rw [or_else], cases em (y = bot), case or.inl { rw [if_pos h], }, case or.inr { rw [if_neg h], exact hle, }, end -- max: noncomputable def and_then {T: Sort _} [hT: bot_order T] (x y: T): T := if x = bot then x else y infix ` >> `:60 := and_then theorem and_then.le_refl {T: Sort _} [hT: bot_order T] {x y: T}: y >> x ≀ x := begin rw [and_then], cases em (y = bot), case or.inl { rw [if_pos h, h], apply bot_le, }, case or.inr { rw [if_neg h], }, end theorem and_then.le_of_le {T: Sort _} [hT: bot_order T] {x y: T}: x ≀ y β†’ y >> x ≀ y := begin intro hle, rw [and_then], cases em (y = bot), case or.inl { rw [if_pos h], }, case or.inr { rw [if_neg h], exact hle, }, end -- function classes: class increasing {T: Sort _} [hT: partial_order T] (f: T β†’ T): Prop := (elim: βˆ€ {x: T}, x ≀ f x) class decreasing {T: Sort _} [hT: partial_order T] (f: T β†’ T): Prop := (elim: βˆ€ {x: T}, f x ≀ x) class monotone {T T': Sort _} [hT: partial_order T] [hT': partial_order T'] (f: T β†’ T'): Prop := (elim: βˆ€ {x y: T}, x ≀ y β†’ f x ≀ f y) class antitone {T T': Sort _} [hT: partial_order T] [hT': partial_order T'] (f: T β†’ T'): Prop := (elim: βˆ€ {x y: T}, f x ≀ f y β†’ x ≀ y) -- examples: instance id.increasing {T: Sort _} [hT: partial_order T]: increasing (@id T) := begin split, intro x, simp, end instance id.decreasing {T: Sort _} [hT: partial_order T]: decreasing (@id T) := begin split, intro x, simp, end instance id.monotone {T: Sort _} [hT: partial_order T]: monotone (@id T) := begin split, intros x y hxy, simp, exact hxy, end instance id.antitone {T: Sort _} [hT: partial_order T]: antitone (@id T) := begin split, intros x y hid, simp at hid, exact hid, end instance nat.succ.monotone: monotone nat.succ := begin split, intros, apply nat.succ_le_succ, assumption, end -- monotonicity: theorem monotone.of_comparable {T T': Sort _} [hT: partial_order T] [hT': partial_order T'] {f: T β†’ T'} [hf: monotone f]: βˆ€ {x y: T}, x <=> y β†’ f x <=> f y := begin intros x y hcomp, induction hcomp, case comp.le { apply comp.le, apply hf.elim, exact hcomp, }, case comp.ge { apply comp.ge, apply hf.elim, exact hcomp, }, end theorem monotone.of_con {T T': Sort _} [hT: partial_order T] [hT': partial_order T'] {f: T β†’ T'} [hf: monotone f]: βˆ€ {x y: T}, x ~ y β†’ f x ~ f y := begin intros x y hcon, induction hcon, case con.le { apply con.le, apply hf.elim, assumption, }, case con.ge { apply con.ge, apply hf.elim, assumption, }, case con.trans { exact con.trans hcon_ih_hxy hcon_ih_hyz, }, end @[simp] theorem monotone.bot_to_bot_of_sur {T T': Sort _} [hT: bot_order T] [hT': bot_order T'] (f: T β†’ T') [hfm: monotone f] [hfs: surjective f]: f bot = bot := begin apply bot_uniq, intro x, have hfsx := hfs x, apply exists.elim hfsx, intros y hy, rw [←hy], apply hfm.elim, apply bot_le, end def monotone.cod_bot_of_sur {T T': Sort _} [hT: bot_order T] [hT': partial_order T'] (f: T β†’ T') [hfm: monotone f] [hfs: surjective f]: bot_order T' := begin split, show T', from f bot, intro x, have hfsx := hfs x, apply exists.elim hfsx, intros y hy, rw [←hy], apply hfm.elim, apply bot_le, end instance monotone.of_composition {T T' T'': Sort _} [hT: partial_order T] [hT': partial_order T'] [hT'': partial_order T''] (g: T β†’ T') [hg: monotone g] (f: T' β†’ T'') [hf: monotone f]: monotone (f ∘ g) := begin split, intros x y hxy, simp, apply hf.elim, apply hg.elim, exact hxy, end -- galois connections: structure galois_connection (A B: Sort _) [hA: partial_order A] [hB: partial_order B] := (F: A β†’ B) [hF: monotone F] (G: B β†’ A) [hG: monotone G] (elim: βˆ€ {a: A} {b: B}, F a ≀ b ↔ a ≀ G b) def galois_connection.closure {A B: Sort _} [hA: partial_order A] [hB: partial_order B] (gc: galois_connection A B): A β†’ A := gc.G ∘ gc.F instance galois_connection.closure.monotone {A B: Sort _} [hA: partial_order A] [hB: partial_order B] (gc: galois_connection A B): monotone gc.closure := by apply @monotone.of_composition A B A hA hB hA gc.F gc.hF gc.G gc.hG def galois_connection.kernel {A B: Sort _} [hA: partial_order A] [hB: partial_order B] (gc: galois_connection A B): B β†’ B := gc.F ∘ gc.G instance galois_connection.kernel.monotone {A B: Sort _} [hA: partial_order A] [hB: partial_order B] (gc: galois_connection A B): monotone gc.kernel := by apply @monotone.of_composition B A B hB hA hB gc.G gc.hG gc.F gc.hF -- trivial or well-founded: def has_bot (T: Sort _) [hT: partial_order T]: Prop := βˆƒ bot: T, βˆ€ x: T, bot ≀ x noncomputable def has_bot.bot_order {T: Sort _} [hT: partial_order T] (hbot: has_bot T): bot_order T := { bot := unexists hbot, bot_le := unexists_prop hbot, ..hT, } inductive triv_or_bot (T: Sort _) [hT: partial_order T]: Prop | triv (htriv: trivially_ordered T): triv_or_bot | bot (hbot: has_bot T): triv_or_bot -- lower bounds: @[reducible] def lower_bound {T: Sort _} [ht: partial_order T] (lb: T) (s: set T): Prop := βˆ€ x: T, x ∈ s β†’ lb ≀ x infix ≀ := lower_bound class bounded_below {T: Sort _} [hT: partial_order T] (s: set T) := (elim: βˆƒ lb: T, lb ≀ s) instance bounded_below.of_bot {T: Sort _} [hT: bot_order T] (s: set T): bounded_below s := begin split, apply exists.intro bot, intros x hx, apply bot_le, end noncomputable def bounded_below.bot_of_bounded_univ {T: Sort _} [hT: partial_order T] [hs: bounded_below {x: T | true}]: bot_order T := begin split, show T, from unexists hs.elim, intros, apply unexists_prop hs.elim, split, end class glb_prop (T: Sort _) extends partial_order T := (has_glb: βˆ€ s: set T, βˆ€ [hs: bounded_below s], βˆƒ glb: T, glb ≀ s ∧ βˆ€ x: T, x ≀ s β†’ x ≀ glb) noncomputable def inf {T: Sort _} [hT: glb_prop T] (s: set T) [hs: bounded_below s]: T := unexists (glb_prop.has_glb s) theorem inf_le {T: Sort _} [hT: glb_prop T] {s: set T} [hs: bounded_below s]: inf s ≀ s := (unexists_prop (glb_prop.has_glb s)).1 theorem inf_glb {T: Sort _} [hT: glb_prop T] {s: set T} [hs: bounded_below s]: βˆ€ x: T, x ≀ s β†’ x ≀ inf s := (unexists_prop (glb_prop.has_glb s)).2 -- order inversion: def invert_order (T: Sort _) [hT: partial_order T]: partial_order T := { le := Ξ» x y: T, y ≀ x, le_refl := begin intros, apply hT.le_refl, end, le_trans := begin intros, apply hT.le_trans; assumption, end, le_antisymm := begin intros, apply hT.le_antisymm; assumption, end, } class monotone.decreasing {T T': Sort _} [hT: partial_order T] [hT': partial_order T'] (f: T β†’ T'): Prop := (elim: βˆ€ {x y: T}, x ≀ y β†’ f y ≀ f x) end posets
7c3f086ace8b538b4fd4246510c5f95179018dc9
4efff1f47634ff19e2f786deadd394270a59ecd2
/src/topology/category/Top/opens.lean
6761ad22de4ded11ddb5036caeea881dd19c2212
[ "Apache-2.0" ]
permissive
agjftucker/mathlib
d634cd0d5256b6325e3c55bb7fb2403548371707
87fe50de17b00af533f72a102d0adefe4a2285e8
refs/heads/master
1,625,378,131,941
1,599,166,526,000
1,599,166,526,000
160,748,509
0
0
Apache-2.0
1,544,141,789,000
1,544,141,789,000
null
UTF-8
Lean
false
false
6,219
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 topology.category.Top.basic import category_theory.eq_to_hom /-! # The category of open sets in a topological space. We define `to_Top : opens X β₯€ Top` and `map (f : X ⟢ Y) : opens Y β₯€ opens X`, given by taking preimages of open sets. Unfortunately `opens` isn't (usefully) a functor `Top β₯€ Cat`. (One can in fact define such a functor, but using it results in unresolvable `eq.rec` terms in goals.) Really it's a 2-functor from (spaces, continuous functions, equalities) to (categories, functors, natural isomorphisms). We don't attempt to set up the full theory here, but do provide the natural isomorphisms `map_id : map (πŸ™ X) β‰… 𝟭 (opens X)` and `map_comp : map (f ≫ g) β‰… map g β‹™ map f`. Beyond that, there's a collection of simp lemmas for working with these constructions. -/ open category_theory open topological_space open opposite universe u namespace topological_space.opens variables {X Y Z : Top.{u}} /-! Since `opens X` has a partial order, it automatically receives a `category` instance. Unfortunately, because we do not allow morphisms in `Prop`, the morphisms `U ⟢ V` are not just proofs `U ≀ V`, but rather `ulift (plift (U ≀ V))`. -/ instance opens_hom_has_coe_to_fun {U V : opens X} : has_coe_to_fun (U ⟢ V) := { F := Ξ» f, U β†’ V, coe := Ξ» f x, ⟨x, (le_of_hom f) x.2⟩ } /-! We now construct as morphisms various inclusions of open sets. -/ -- This is tedious, but necessary because we decided not to allow Prop as morphisms in a category... /-- The inclusion `U βŠ“ V ⟢ U` as a morphism in the category of open sets. -/ def inf_le_left (U V : opens X) : U βŠ“ V ⟢ U := hom_of_le inf_le_left /-- The inclusion `U βŠ“ V ⟢ V` as a morphism in the category of open sets. -/ def inf_le_right (U V : opens X) : U βŠ“ V ⟢ V := hom_of_le inf_le_right /-- The inclusion `U i ⟢ supr U` as a morphism in the category of open sets. -/ def le_supr {ΞΉ : Type*} (U : ΞΉ β†’ opens X) (i : ΞΉ) : U i ⟢ supr U := hom_of_le (le_supr U i) /-- The functor from open sets in `X` to `Top`, realising each open set as a topological space itself. -/ def to_Top (X : Top.{u}) : opens X β₯€ Top := { obj := Ξ» U, ⟨U.val, infer_instance⟩, map := Ξ» U V i, ⟨λ x, ⟨x.1, (le_of_hom i) x.2⟩, (embedding.continuous_iff embedding_subtype_coe).2 continuous_induced_dom⟩ } @[simp] lemma to_Top_map (X : Top.{u}) {U V : opens X} {f : U ⟢ V} {x} {h} : ((to_Top X).map f) ⟨x, h⟩ = ⟨x, (le_of_hom f) h⟩ := rfl /-- The inclusion map from an open subset to the whole space, as a morphism in `Top`. -/ @[simps] def inclusion {X : Top.{u}} (U : opens X) : (to_Top X).obj U ⟢ X := { to_fun := _, continuous_to_fun := continuous_subtype_coe } lemma inclusion_open_embedding {X : Top.{u}} (U : opens X) : open_embedding (inclusion U) := is_open.open_embedding_subtype_coe U.2 /-- `opens.map f` gives the functor from open sets in Y to open set in X, given by taking preimages under f. -/ def map (f : X ⟢ Y) : opens Y β₯€ opens X := { obj := Ξ» U, ⟨ f ⁻¹' U.val, f.continuous _ U.property ⟩, map := Ξ» U V i, ⟨ ⟨ Ξ» a b, (le_of_hom i) b ⟩ ⟩ }. @[simp] lemma map_obj (f : X ⟢ Y) (U) (p) : (map f).obj ⟨U, p⟩ = ⟨f ⁻¹' U, f.continuous _ p⟩ := rfl @[simp] lemma map_id_obj (U : opens X) : (map (πŸ™ X)).obj U = U := by { ext, refl } -- not quite `rfl`, since we don't have eta for records @[simp] lemma map_id_obj' (U) (p) : (map (πŸ™ X)).obj ⟨U, p⟩ = ⟨U, p⟩ := rfl @[simp] lemma map_id_obj_unop (U : (opens X)α΅’α΅–) : (map (πŸ™ X)).obj (unop U) = unop U := by simp @[simp] lemma op_map_id_obj (U : (opens X)α΅’α΅–) : (map (πŸ™ X)).op.obj U = U := by simp section variable (X) /-- The functor `opens X β₯€ opens X` given by taking preimages under the identity function is naturally isomorphic to the identity functor. -/ @[simps] def map_id : map (πŸ™ X) β‰… 𝟭 (opens X) := { hom := { app := Ξ» U, eq_to_hom (map_id_obj U) }, inv := { app := Ξ» U, eq_to_hom (map_id_obj U).symm } } end @[simp] lemma map_comp_obj (f : X ⟢ Y) (g : Y ⟢ Z) (U) : (map (f ≫ g)).obj U = (map f).obj ((map g).obj U) := by { ext, refl } -- not quite `rfl`, since we don't have eta for records @[simp] lemma map_comp_obj' (f : X ⟢ Y) (g : Y ⟢ Z) (U) (p) : (map (f ≫ g)).obj ⟨U, p⟩ = (map f).obj ((map g).obj ⟨U, p⟩) := rfl @[simp] lemma map_comp_obj_unop (f : X ⟢ Y) (g : Y ⟢ Z) (U) : (map (f ≫ g)).obj (unop U) = (map f).obj ((map g).obj (unop U)) := by simp @[simp] lemma op_map_comp_obj (f : X ⟢ Y) (g : Y ⟢ Z) (U) : (map (f ≫ g)).op.obj U = (map f).op.obj ((map g).op.obj U) := by simp /-- The natural isomorphism between taking preimages under `f ≫ g`, and the composite of taking preimages under `g`, then preimages under `f`. -/ @[simps] def map_comp (f : X ⟢ Y) (g : Y ⟢ Z) : map (f ≫ g) β‰… map g β‹™ map f := { hom := { app := Ξ» U, eq_to_hom (map_comp_obj f g U) }, inv := { app := Ξ» U, eq_to_hom (map_comp_obj f g U).symm } } -- We could make `f g` implicit here, but it's nice to be able to see when -- they are the identity (often!) def map_iso (f g : X ⟢ Y) (h : f = g) : map f β‰… map g := nat_iso.of_components (Ξ» U, eq_to_iso (congr_fun (congr_arg functor.obj (congr_arg map h)) U) ) (by obviously) @[simp] lemma map_iso_refl (f : X ⟢ Y) (h) : map_iso f f h = iso.refl (map _) := rfl @[simp] lemma map_iso_hom_app (f g : X ⟢ Y) (h : f = g) (U : opens Y) : (map_iso f g h).hom.app U = eq_to_hom (congr_fun (congr_arg functor.obj (congr_arg map h)) U) := rfl @[simp] lemma map_iso_inv_app (f g : X ⟢ Y) (h : f = g) (U : opens Y) : (map_iso f g h).inv.app U = eq_to_hom (congr_fun (congr_arg functor.obj (congr_arg map h.symm)) U) := rfl end topological_space.opens /-- An open map `f : X ⟢ Y` induces a functor `opens X β₯€ opens Y`. -/ @[simps] def is_open_map.functor {X Y : Top} {f : X ⟢ Y} (hf : is_open_map f) : opens X β₯€ opens Y := { obj := Ξ» U, ⟨f '' U, hf U U.2⟩, map := Ξ» U V h, ⟨⟨set.image_subset _ h.down.down⟩⟩ }
f4c77407fea3ce503216627e9f2fd028110545eb
86f6f4f8d827a196a32bfc646234b73328aeb306
/examples/sets_functions_and_relations/unnamed_830.lean
6c13dc82ed6c74382ef99e99700b9f37a4cf6063
[]
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
373
lean
import data.set.function variables {Ξ± Ξ² : Type*} variable f : Ξ± β†’ Ξ² variables s t : set Ξ± -- BEGIN example : f '' (s βˆͺ t) = f '' s βˆͺ f '' t := begin ext y, split, { rintros ⟨x, xs | xt, rfl⟩, { left, use [x, xs] }, right, use [x, xt] }, rintros (⟨x, xs, rfl⟩ | ⟨x, xt, rfl⟩), { use [x, or.inl xs] }, use [x, or.inr xt] end -- END
b0cace693f954b2e465a0747ae98ae2fe4cbe56e
302c785c90d40ad3d6be43d33bc6a558354cc2cf
/src/data/equiv/local_equiv.lean
e844015f2afe7057584618ac0cf56cdbb37f1f35
[ "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
33,467
lean
/- Copyright (c) 2019 SΓ©bastien GouΓ«zel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: SΓ©bastien GouΓ«zel -/ import data.equiv.basic /-! # Local equivalences This files defines equivalences between subsets of given types. An element `e` of `local_equiv Ξ± Ξ²` is made of two maps `e.to_fun` and `e.inv_fun` respectively from Ξ± to Ξ² and from Ξ² to Ξ± (just like equivs), which are inverse to each other on the subsets `e.source` and `e.target` of respectively Ξ± and Ξ². They are designed in particular to define charts on manifolds. The main functionality is `e.trans f`, which composes the two local equivalences by restricting the source and target to the maximal set where the composition makes sense. As for equivs, we register a coercion to functions and use it in our simp normal form: we write `e x` and `e.symm y` instead of `e.to_fun x` and `e.inv_fun y`. ## Main definitions `equiv.to_local_equiv`: associating a local equiv to an equiv, with source = target = univ `local_equiv.symm` : the inverse of a local equiv `local_equiv.trans` : the composition of two local equivs `local_equiv.refl` : the identity local equiv `local_equiv.of_set` : the identity on a set `s` `eq_on_source` : equivalence relation describing the "right" notion of equality for local equivs (see below in implementation notes) ## Implementation notes There are at least three possible implementations of local equivalences: * equivs on subtypes * pairs of functions taking values in `option Ξ±` and `option Ξ²`, equal to none where the local equivalence is not defined * pairs of functions defined everywhere, keeping the source and target as additional data Each of these implementations has pros and cons. * When dealing with subtypes, one still need to define additional API for composition and restriction of domains. Checking that one always belongs to the right subtype makes things very tedious, and leads quickly to DTT hell (as the subtype `u ∩ v` is not the "same" as `v ∩ u`, for instance). * With option-valued functions, the composition is very neat (it is just the usual composition, and the domain is restricted automatically). These are implemented in `pequiv.lean`. For manifolds, where one wants to discuss thoroughly the smoothness of the maps, this creates however a lot of overhead as one would need to extend all classes of smoothness to option-valued maps. * The local_equiv version as explained above is easier to use for manifolds. The drawback is that there is extra useless data (the values of `to_fun` and `inv_fun` outside of `source` and `target`). In particular, the equality notion between local equivs is not "the right one", i.e., coinciding source and target and equality there. Moreover, there are no local equivs in this sense between an empty type and a nonempty type. Since empty types are not that useful, and since one almost never needs to talk about equal local equivs, this is not an issue in practice. Still, we introduce an equivalence relation `eq_on_source` that captures this right notion of equality, and show that many properties are invariant under this equivalence relation. ### Local coding conventions If a lemma deals with the intersection of a set with either source or target of a `local_equiv`, then it should use `e.source ∩ s` or `e.target ∩ t`, not `s ∩ e.source` or `t ∩ e.target`. -/ mk_simp_attribute mfld_simps "The simpset `mfld_simps` records several simp lemmas that are especially useful in manifolds. It is a subset of the whole set of simp lemmas, but it makes it possible to have quicker proofs (when used with `squeeze_simp` or `simp only`) while retaining readability. The typical use case is the following, in a file on manifolds: If `simp [foo, bar]` is slow, replace it with `squeeze_simp [foo, bar] with mfld_simps` and paste its output. The list of lemmas should be reasonable (contrary to the output of `squeeze_simp [foo, bar]` which might contain tens of lemmas), and the outcome should be quick enough. " -- register in the simpset `mfld_simps` several lemmas that are often useful when dealing -- with manifolds attribute [mfld_simps] id.def function.comp.left_id set.mem_set_of_eq set.image_eq_empty set.univ_inter set.preimage_univ set.prod_mk_mem_set_prod_eq and_true set.mem_univ set.mem_image_of_mem true_and set.mem_inter_eq set.mem_preimage function.comp_app set.inter_subset_left set.mem_prod set.range_id and_self set.mem_range_self eq_self_iff_true forall_const forall_true_iff set.inter_univ set.preimage_id function.comp.right_id not_false_iff and_imp set.prod_inter_prod set.univ_prod_univ true_or or_true prod.map_mk set.preimage_inter heq_iff_eq equiv.sigma_equiv_prod_apply equiv.sigma_equiv_prod_symm_apply subtype.coe_mk equiv.to_fun_as_coe equiv.inv_fun_as_coe namespace tactic.interactive /-- A very basic tactic to show that sets showing up in manifolds coincide or are included in one another. -/ meta def mfld_set_tac : tactic unit := do goal ← tactic.target, match goal with | `(%%e₁ = %%eβ‚‚) := `[ext my_y, split; { assume h_my_y, try { simp only [*, -h_my_y] with mfld_simps at h_my_y }, simp only [*] with mfld_simps }] | `(%%e₁ βŠ† %%eβ‚‚) := `[assume my_y h_my_y, try { simp only [*, -h_my_y] with mfld_simps at h_my_y }, simp only [*] with mfld_simps] | _ := tactic.fail "goal should be an equality or an inclusion" end end tactic.interactive open function set variables {Ξ± : Type*} {Ξ² : Type*} {Ξ³ : Type*} {Ξ΄ : Type*} /-- Local equivalence between subsets `source` and `target` of Ξ± and Ξ² respectively. The (global) maps `to_fun : Ξ± β†’ Ξ²` and `inv_fun : Ξ² β†’ Ξ±` map `source` to `target` and conversely, and are inverse to each other there. The values of `to_fun` outside of `source` and of `inv_fun` outside of `target` are irrelevant. -/ @[nolint has_inhabited_instance] structure local_equiv (Ξ± : Type*) (Ξ² : Type*) := (to_fun : Ξ± β†’ Ξ²) (inv_fun : Ξ² β†’ Ξ±) (source : set Ξ±) (target : set Ξ²) (map_source' : βˆ€{x}, x ∈ source β†’ to_fun x ∈ target) (map_target' : βˆ€{x}, x ∈ target β†’ inv_fun x ∈ source) (left_inv' : βˆ€{x}, x ∈ source β†’ inv_fun (to_fun x) = x) (right_inv' : βˆ€{x}, x ∈ target β†’ to_fun (inv_fun x) = x) /-- Associating a local_equiv to an equiv-/ def equiv.to_local_equiv (e : equiv Ξ± Ξ²) : local_equiv Ξ± Ξ² := { to_fun := e.to_fun, inv_fun := e.inv_fun, source := univ, target := univ, map_source' := Ξ»x hx, mem_univ _, map_target' := Ξ»y hy, mem_univ _, left_inv' := Ξ»x hx, e.left_inv x, right_inv' := Ξ»x hx, e.right_inv x } namespace local_equiv variables (e : local_equiv Ξ± Ξ²) (e' : local_equiv Ξ² Ξ³) /-- The inverse of a local equiv -/ protected def symm : local_equiv Ξ² Ξ± := { to_fun := e.inv_fun, inv_fun := e.to_fun, source := e.target, target := e.source, map_source' := e.map_target', map_target' := e.map_source', left_inv' := e.right_inv', right_inv' := e.left_inv' } instance : has_coe_to_fun (local_equiv Ξ± Ξ²) := ⟨_, local_equiv.to_fun⟩ /-- See Note [custom simps projection] -/ def simps.inv_fun (e : local_equiv Ξ± Ξ²) : Ξ² β†’ Ξ± := e.symm initialize_simps_projections local_equiv (to_fun β†’ apply, inv_fun β†’ symm_apply) @[simp, mfld_simps] theorem coe_mk (f : Ξ± β†’ Ξ²) (g s t ml mr il ir) : (local_equiv.mk f g s t ml mr il ir : Ξ± β†’ Ξ²) = f := rfl @[simp, mfld_simps] theorem coe_symm_mk (f : Ξ± β†’ Ξ²) (g s t ml mr il ir) : ((local_equiv.mk f g s t ml mr il ir).symm : Ξ² β†’ Ξ±) = g := rfl @[simp, mfld_simps] lemma to_fun_as_coe : e.to_fun = e := rfl @[simp, mfld_simps] lemma inv_fun_as_coe : e.inv_fun = e.symm := rfl @[simp, mfld_simps] lemma map_source {x : Ξ±} (h : x ∈ e.source) : e x ∈ e.target := e.map_source' h @[simp, mfld_simps] lemma map_target {x : Ξ²} (h : x ∈ e.target) : e.symm x ∈ e.source := e.map_target' h @[simp, mfld_simps] lemma left_inv {x : Ξ±} (h : x ∈ e.source) : e.symm (e x) = x := e.left_inv' h @[simp, mfld_simps] lemma right_inv {x : Ξ²} (h : x ∈ e.target) : e (e.symm x) = x := e.right_inv' h protected lemma maps_to : maps_to e e.source e.target := Ξ» x, e.map_source lemma symm_maps_to : maps_to e.symm e.target e.source := e.symm.maps_to protected lemma left_inv_on : left_inv_on e.symm e e.source := Ξ» x, e.left_inv protected lemma right_inv_on : right_inv_on e.symm e e.target := Ξ» x, e.right_inv protected lemma inv_on : inv_on e.symm e e.source e.target := ⟨e.left_inv_on, e.right_inv_on⟩ protected lemma inj_on : inj_on e e.source := e.left_inv_on.inj_on protected lemma bij_on : bij_on e e.source e.target := e.inv_on.bij_on e.maps_to e.symm_maps_to protected lemma surj_on : surj_on e e.source e.target := e.bij_on.surj_on /-- Create a copy of a `local_equiv` providing better definitional equalities. -/ @[simps] def copy (e : local_equiv Ξ± Ξ²) (f : Ξ± β†’ Ξ²) (hf : ⇑e = f) (g : Ξ² β†’ Ξ±) (hg : ⇑e.symm = g) (s : set Ξ±) (hs : e.source = s) (t : set Ξ²) (ht : e.target = t) : local_equiv Ξ± Ξ² := { to_fun := f, inv_fun := g, source := s, target := t, map_source' := Ξ» x, ht β–Έ hs β–Έ hf β–Έ e.map_source, map_target' := Ξ» y, hs β–Έ ht β–Έ hg β–Έ e.map_target, left_inv' := Ξ» x, hs β–Έ hf β–Έ hg β–Έ e.left_inv, right_inv' := Ξ» x, ht β–Έ hf β–Έ hg β–Έ e.right_inv } lemma copy_eq_self (e : local_equiv Ξ± Ξ²) (f : Ξ± β†’ Ξ²) (hf : ⇑e = f) (g : Ξ² β†’ Ξ±) (hg : ⇑e.symm = g) (s : set Ξ±) (hs : e.source = s) (t : set Ξ²) (ht : e.target = t) : e.copy f hf g hg s hs t ht = e := by { substs f g s t, cases e, refl } /-- Associating to a local_equiv an equiv between the source and the target -/ protected def to_equiv : equiv (e.source) (e.target) := { to_fun := Ξ» x, ⟨e x, e.map_source x.mem⟩, inv_fun := Ξ» y, ⟨e.symm y, e.map_target y.mem⟩, left_inv := λ⟨x, hx⟩, subtype.eq $ e.left_inv hx, right_inv := λ⟨y, hy⟩, subtype.eq $ e.right_inv hy } @[simp, mfld_simps] lemma symm_source : e.symm.source = e.target := rfl @[simp, mfld_simps] lemma symm_target : e.symm.target = e.source := rfl @[simp, mfld_simps] lemma symm_symm : e.symm.symm = e := by { cases e, refl } lemma image_source_eq_target : e '' e.source = e.target := e.bij_on.image_eq /-- We say that `t : set Ξ²` is an image of `s : set Ξ±` under a local equivalence if any of the following equivalent conditions hold: * `e '' (e.source ∩ s) = e.target ∩ t`; * `e.source ∩ e ⁻¹ t = e.source ∩ s`; * `βˆ€ x ∈ e.source, e x ∈ t ↔ x ∈ s` (this one is used in the definition). -/ def is_image (s : set Ξ±) (t : set Ξ²) : Prop := βˆ€ ⦃x⦄, x ∈ e.source β†’ (e x ∈ t ↔ x ∈ s) namespace is_image variables {e} {s : set Ξ±} {t : set Ξ²} {x : Ξ±} {y : Ξ²} lemma apply_mem_iff (h : e.is_image s t) (hx : x ∈ e.source) : e x ∈ t ↔ x ∈ s := h hx lemma symm_apply_mem_iff (h : e.is_image s t) : βˆ€ ⦃y⦄, y ∈ e.target β†’ (e.symm y ∈ s ↔ y ∈ t) := by { rw [← e.image_source_eq_target, ball_image_iff], intros x hx, rw [e.left_inv hx, h hx] } protected lemma symm (h : e.is_image s t) : e.symm.is_image t s := h.symm_apply_mem_iff @[simp] lemma symm_iff : e.symm.is_image t s ↔ e.is_image s t := ⟨λ h, h.symm, Ξ» h, h.symm⟩ protected lemma maps_to (h : e.is_image s t) : maps_to e (e.source ∩ s) (e.target ∩ t) := Ξ» x hx, ⟨e.maps_to hx.1, (h hx.1).2 hx.2⟩ lemma symm_maps_to (h : e.is_image s t) : maps_to e.symm (e.target ∩ t) (e.source ∩ s) := h.symm.maps_to /-- Restrict a `local_equiv` to a pair of corresponding sets. -/ @[simps] def restr (h : e.is_image s t) : local_equiv Ξ± Ξ² := { to_fun := e, inv_fun := e.symm, source := e.source ∩ s, target := e.target ∩ t, map_source' := h.maps_to, map_target' := h.symm_maps_to, left_inv' := e.left_inv_on.mono (inter_subset_left _ _), right_inv' := e.right_inv_on.mono (inter_subset_left _ _) } lemma image_eq (h : e.is_image s t) : e '' (e.source ∩ s) = e.target ∩ t := h.restr.image_source_eq_target lemma symm_image_eq (h : e.is_image s t) : e.symm '' (e.target ∩ t) = e.source ∩ s := h.symm.image_eq lemma iff_preimage_eq : e.is_image s t ↔ e.source ∩ e ⁻¹' t = e.source ∩ s := by simp only [is_image, set.ext_iff, mem_inter_eq, and.congr_right_iff, mem_preimage] alias iff_preimage_eq ↔ local_equiv.is_image.preimage_eq local_equiv.is_image.of_preimage_eq lemma iff_symm_preimage_eq : e.is_image s t ↔ e.target ∩ e.symm ⁻¹' s = e.target ∩ t := symm_iff.symm.trans iff_preimage_eq alias iff_symm_preimage_eq ↔ local_equiv.is_image.symm_preimage_eq local_equiv.is_image.of_symm_preimage_eq lemma of_image_eq (h : e '' (e.source ∩ s) = e.target ∩ t) : e.is_image s t := of_symm_preimage_eq $ eq.trans (of_symm_preimage_eq rfl).image_eq.symm h lemma of_symm_image_eq (h : e.symm '' (e.target ∩ t) = e.source ∩ s) : e.is_image s t := of_preimage_eq $ eq.trans (of_preimage_eq rfl).symm_image_eq.symm h protected lemma compl (h : e.is_image s t) : e.is_image sᢜ tᢜ := Ξ» x hx, not_congr (h hx) protected lemma inter {s' t'} (h : e.is_image s t) (h' : e.is_image s' t') : e.is_image (s ∩ s') (t ∩ t') := Ξ» x hx, and_congr (h hx) (h' hx) protected lemma union {s' t'} (h : e.is_image s t) (h' : e.is_image s' t') : e.is_image (s βˆͺ s') (t βˆͺ t') := Ξ» x hx, or_congr (h hx) (h' hx) protected lemma diff {s' t'} (h : e.is_image s t) (h' : e.is_image s' t') : e.is_image (s \ s') (t \ t') := h.inter h'.compl lemma left_inv_on_piecewise {e' : local_equiv Ξ± Ξ²} [βˆ€ i, decidable (i ∈ s)] [βˆ€ i, decidable (i ∈ t)] (h : e.is_image s t) (h' : e'.is_image s t) : left_inv_on (t.piecewise e.symm e'.symm) (s.piecewise e e') (s.ite e.source e'.source) := begin rintro x (⟨he, hs⟩|⟨he, hs : x βˆ‰ s⟩), { rw [piecewise_eq_of_mem _ _ _ hs, piecewise_eq_of_mem _ _ _ ((h he).2 hs), e.left_inv he], }, { rw [piecewise_eq_of_not_mem _ _ _ hs, piecewise_eq_of_not_mem _ _ _ ((h'.compl he).2 hs), e'.left_inv he] } end lemma inter_eq_of_inter_eq_of_eq_on {e' : local_equiv Ξ± Ξ²} (h : e.is_image s t) (h' : e'.is_image s t) (hs : e.source ∩ s = e'.source ∩ s) (Heq : eq_on e e' (e.source ∩ s)) : e.target ∩ t = e'.target ∩ t := by rw [← h.image_eq, ← h'.image_eq, ← hs, Heq.image_eq] lemma symm_eq_on_of_inter_eq_of_eq_on {e' : local_equiv Ξ± Ξ²} (h : e.is_image s t) (hs : e.source ∩ s = e'.source ∩ s) (Heq : eq_on e e' (e.source ∩ s)) : eq_on e.symm e'.symm (e.target ∩ t) := begin rw [← h.image_eq], rintros y ⟨x, hx, rfl⟩, have hx' := hx, rw hs at hx', rw [e.left_inv hx.1, Heq hx, e'.left_inv hx'.1] end end is_image lemma is_image_source_target : e.is_image e.source e.target := Ξ» x hx, by simp [hx] lemma is_image_source_target_of_disjoint (e' : local_equiv Ξ± Ξ²) (hs : disjoint e.source e'.source) (ht : disjoint e.target e'.target) : e.is_image e'.source e'.target := assume x hx, have x βˆ‰ e'.source, from Ξ» hx', hs ⟨hx, hx'⟩, have e x βˆ‰ e'.target, from Ξ» hx', ht ⟨e.maps_to hx, hx'⟩, by simp only * lemma image_source_inter_eq' (s : set Ξ±) : e '' (e.source ∩ s) = e.target ∩ e.symm ⁻¹' s := by rw [inter_comm, e.left_inv_on.image_inter', image_source_eq_target, inter_comm] lemma image_source_inter_eq (s : set Ξ±) : e '' (e.source ∩ s) = e.target ∩ e.symm ⁻¹' (e.source ∩ s) := by rw [inter_comm, e.left_inv_on.image_inter, image_source_eq_target, inter_comm] lemma image_eq_target_inter_inv_preimage {s : set Ξ±} (h : s βŠ† e.source) : e '' s = e.target ∩ e.symm ⁻¹' s := by rw [← e.image_source_inter_eq', inter_eq_self_of_subset_right h] lemma symm_image_eq_source_inter_preimage {s : set Ξ²} (h : s βŠ† e.target) : e.symm '' s = e.source ∩ e ⁻¹' s := e.symm.image_eq_target_inter_inv_preimage h lemma symm_image_target_inter_eq (s : set Ξ²) : e.symm '' (e.target ∩ s) = e.source ∩ e ⁻¹' (e.target ∩ s) := e.symm.image_source_inter_eq _ lemma symm_image_target_inter_eq' (s : set Ξ²) : e.symm '' (e.target ∩ s) = e.source ∩ e ⁻¹' s := e.symm.image_source_inter_eq' _ lemma source_inter_preimage_inv_preimage (s : set Ξ±) : e.source ∩ e ⁻¹' (e.symm ⁻¹' s) = e.source ∩ s := set.ext $ Ξ» x, and.congr_right_iff.2 $ Ξ» hx, by simp only [mem_preimage, e.left_inv hx] lemma target_inter_inv_preimage_preimage (s : set Ξ²) : e.target ∩ e.symm ⁻¹' (e ⁻¹' s) = e.target ∩ s := e.symm.source_inter_preimage_inv_preimage _ lemma source_subset_preimage_target : e.source βŠ† e ⁻¹' e.target := e.maps_to lemma symm_image_target_eq_source : e.symm '' e.target = e.source := e.symm.image_source_eq_target lemma target_subset_preimage_source : e.target βŠ† e.symm ⁻¹' e.source := e.symm_maps_to /-- Two local equivs that have the same `source`, same `to_fun` and same `inv_fun`, coincide. -/ @[ext] protected lemma ext {e e' : local_equiv Ξ± Ξ²} (h : βˆ€x, e x = e' x) (hsymm : βˆ€x, e.symm x = e'.symm x) (hs : e.source = e'.source) : e = e' := begin have A : (e : Ξ± β†’ Ξ²) = e', by { ext x, exact h x }, have B : (e.symm : Ξ² β†’ Ξ±) = e'.symm, by { ext x, exact hsymm x }, have I : e '' e.source = e.target := e.image_source_eq_target, have I' : e' '' e'.source = e'.target := e'.image_source_eq_target, rw [A, hs, I'] at I, cases e; cases e', simp * at * end /-- Restricting a local equivalence to e.source ∩ s -/ protected def restr (s : set Ξ±) : local_equiv Ξ± Ξ² := (@is_image.of_symm_preimage_eq Ξ± Ξ² e s (e.symm ⁻¹' s) rfl).restr @[simp, mfld_simps] lemma restr_coe (s : set Ξ±) : (e.restr s : Ξ± β†’ Ξ²) = e := rfl @[simp, mfld_simps] lemma restr_coe_symm (s : set Ξ±) : ((e.restr s).symm : Ξ² β†’ Ξ±) = e.symm := rfl @[simp, mfld_simps] lemma restr_source (s : set Ξ±) : (e.restr s).source = e.source ∩ s := rfl @[simp, mfld_simps] lemma restr_target (s : set Ξ±) : (e.restr s).target = e.target ∩ e.symm ⁻¹' s := rfl lemma restr_eq_of_source_subset {e : local_equiv Ξ± Ξ²} {s : set Ξ±} (h : e.source βŠ† s) : e.restr s = e := local_equiv.ext (Ξ»_, rfl) (Ξ»_, rfl) (by simp [inter_eq_self_of_subset_left h]) @[simp, mfld_simps] lemma restr_univ {e : local_equiv Ξ± Ξ²} : e.restr univ = e := restr_eq_of_source_subset (subset_univ _) /-- The identity local equiv -/ protected def refl (Ξ± : Type*) : local_equiv Ξ± Ξ± := (equiv.refl Ξ±).to_local_equiv @[simp, mfld_simps] lemma refl_source : (local_equiv.refl Ξ±).source = univ := rfl @[simp, mfld_simps] lemma refl_target : (local_equiv.refl Ξ±).target = univ := rfl @[simp, mfld_simps] lemma refl_coe : (local_equiv.refl Ξ± : Ξ± β†’ Ξ±) = id := rfl @[simp, mfld_simps] lemma refl_symm : (local_equiv.refl Ξ±).symm = local_equiv.refl Ξ± := rfl @[simp, mfld_simps] lemma refl_restr_source (s : set Ξ±) : ((local_equiv.refl Ξ±).restr s).source = s := by simp @[simp, mfld_simps] lemma refl_restr_target (s : set Ξ±) : ((local_equiv.refl Ξ±).restr s).target = s := by { change univ ∩ id⁻¹' s = s, simp } /-- The identity local equiv on a set `s` -/ def of_set (s : set Ξ±) : local_equiv Ξ± Ξ± := { to_fun := id, inv_fun := id, source := s, target := s, map_source' := Ξ»x hx, hx, map_target' := Ξ»x hx, hx, left_inv' := Ξ»x hx, rfl, right_inv' := Ξ»x hx, rfl } @[simp, mfld_simps] lemma of_set_source (s : set Ξ±) : (local_equiv.of_set s).source = s := rfl @[simp, mfld_simps] lemma of_set_target (s : set Ξ±) : (local_equiv.of_set s).target = s := rfl @[simp, mfld_simps] lemma of_set_coe (s : set Ξ±) : (local_equiv.of_set s : Ξ± β†’ Ξ±) = id := rfl @[simp, mfld_simps] lemma of_set_symm (s : set Ξ±) : (local_equiv.of_set s).symm = local_equiv.of_set s := rfl /-- Composing two local equivs if the target of the first coincides with the source of the second. -/ protected def trans' (e' : local_equiv Ξ² Ξ³) (h : e.target = e'.source) : local_equiv Ξ± Ξ³ := { to_fun := e' ∘ e, inv_fun := e.symm ∘ e'.symm, source := e.source, target := e'.target, map_source' := Ξ»x hx, by simp [h.symm, hx], map_target' := Ξ»y hy, by simp [h, hy], left_inv' := Ξ»x hx, by simp [hx, h.symm], right_inv' := Ξ»y hy, by simp [hy, h] } /-- Composing two local equivs, by restricting to the maximal domain where their composition is well defined. -/ protected def trans : local_equiv Ξ± Ξ³ := local_equiv.trans' (e.symm.restr (e'.source)).symm (e'.restr (e.target)) (inter_comm _ _) @[simp, mfld_simps] lemma coe_trans : (e.trans e' : Ξ± β†’ Ξ³) = e' ∘ e := rfl @[simp, mfld_simps] lemma coe_trans_symm : ((e.trans e').symm : Ξ³ β†’ Ξ±) = e.symm ∘ e'.symm := rfl lemma trans_symm_eq_symm_trans_symm : (e.trans e').symm = e'.symm.trans e.symm := by cases e; cases e'; refl @[simp, mfld_simps] lemma trans_source : (e.trans e').source = e.source ∩ e ⁻¹' e'.source := rfl lemma trans_source' : (e.trans e').source = e.source ∩ e ⁻¹' (e.target ∩ e'.source) := by mfld_set_tac lemma trans_source'' : (e.trans e').source = e.symm '' (e.target ∩ e'.source) := by rw [e.trans_source', e.symm_image_target_inter_eq] lemma image_trans_source : e '' (e.trans e').source = e.target ∩ e'.source := (e.symm.restr e'.source).symm.image_source_eq_target @[simp, mfld_simps] lemma trans_target : (e.trans e').target = e'.target ∩ e'.symm ⁻¹' e.target := rfl lemma trans_target' : (e.trans e').target = e'.target ∩ e'.symm ⁻¹' (e'.source ∩ e.target) := trans_source' e'.symm e.symm lemma trans_target'' : (e.trans e').target = e' '' (e'.source ∩ e.target) := trans_source'' e'.symm e.symm lemma inv_image_trans_target : e'.symm '' (e.trans e').target = e'.source ∩ e.target := image_trans_source e'.symm e.symm lemma trans_assoc (e'' : local_equiv Ξ³ Ξ΄) : (e.trans e').trans e'' = e.trans (e'.trans e'') := local_equiv.ext (Ξ»x, rfl) (Ξ»x, rfl) (by simp [trans_source, @preimage_comp Ξ± Ξ² Ξ³, inter_assoc]) @[simp, mfld_simps] lemma trans_refl : e.trans (local_equiv.refl Ξ²) = e := local_equiv.ext (Ξ»x, rfl) (Ξ»x, rfl) (by simp [trans_source]) @[simp, mfld_simps] lemma refl_trans : (local_equiv.refl Ξ±).trans e = e := local_equiv.ext (Ξ»x, rfl) (Ξ»x, rfl) (by simp [trans_source, preimage_id]) lemma trans_refl_restr (s : set Ξ²) : e.trans ((local_equiv.refl Ξ²).restr s) = e.restr (e ⁻¹' s) := local_equiv.ext (Ξ»x, rfl) (Ξ»x, rfl) (by simp [trans_source]) lemma trans_refl_restr' (s : set Ξ²) : e.trans ((local_equiv.refl Ξ²).restr s) = e.restr (e.source ∩ e ⁻¹' s) := local_equiv.ext (Ξ»x, rfl) (Ξ»x, rfl) $ by { simp [trans_source], rw [← inter_assoc, inter_self] } lemma restr_trans (s : set Ξ±) : (e.restr s).trans e' = (e.trans e').restr s := local_equiv.ext (Ξ»x, rfl) (Ξ»x, rfl) $ by { simp [trans_source, inter_comm], rwa inter_assoc } /-- `eq_on_source e e'` means that `e` and `e'` have the same source, and coincide there. Then `e` and `e'` should really be considered the same local equiv. -/ def eq_on_source (e e' : local_equiv Ξ± Ξ²) : Prop := e.source = e'.source ∧ (e.source.eq_on e e') /-- `eq_on_source` is an equivalence relation -/ instance eq_on_source_setoid : setoid (local_equiv Ξ± Ξ²) := { r := eq_on_source, iseqv := ⟨ Ξ»e, by simp [eq_on_source], Ξ»e e' h, by { simp [eq_on_source, h.1.symm], exact Ξ»x hx, (h.2 hx).symm }, Ξ»e e' e'' h h', ⟨by rwa [← h'.1, ← h.1], Ξ»x hx, by { rw [← h'.2, h.2 hx], rwa ← h.1 }⟩⟩ } lemma eq_on_source_refl : e β‰ˆ e := setoid.refl _ /-- Two equivalent local equivs have the same source -/ lemma eq_on_source.source_eq {e e' : local_equiv Ξ± Ξ²} (h : e β‰ˆ e') : e.source = e'.source := h.1 /-- Two equivalent local equivs coincide on the source -/ lemma eq_on_source.eq_on {e e' : local_equiv Ξ± Ξ²} (h : e β‰ˆ e') : e.source.eq_on e e' := h.2 /-- Two equivalent local equivs have the same target -/ lemma eq_on_source.target_eq {e e' : local_equiv Ξ± Ξ²} (h : e β‰ˆ e') : e.target = e'.target := by simp only [← image_source_eq_target, ← h.source_eq, h.2.image_eq] /-- If two local equivs are equivalent, so are their inverses. -/ lemma eq_on_source.symm' {e e' : local_equiv Ξ± Ξ²} (h : e β‰ˆ e') : e.symm β‰ˆ e'.symm := begin refine ⟨h.target_eq, eq_on_of_left_inv_on_of_right_inv_on e.left_inv_on _ _⟩; simp only [symm_source, h.target_eq, h.source_eq, e'.symm_maps_to], exact e'.right_inv_on.congr_right e'.symm_maps_to (h.source_eq β–Έ h.eq_on.symm), end /-- Two equivalent local equivs have coinciding inverses on the target -/ lemma eq_on_source.symm_eq_on {e e' : local_equiv Ξ± Ξ²} (h : e β‰ˆ e') : eq_on e.symm e'.symm e.target := h.symm'.eq_on /-- Composition of local equivs respects equivalence -/ lemma eq_on_source.trans' {e e' : local_equiv Ξ± Ξ²} {f f' : local_equiv Ξ² Ξ³} (he : e β‰ˆ e') (hf : f β‰ˆ f') : e.trans f β‰ˆ e'.trans f' := begin split, { rw [trans_source'', trans_source'', ← he.target_eq, ← hf.1], exact (he.symm'.eq_on.mono $ inter_subset_left _ _).image_eq }, { assume x hx, rw trans_source at hx, simp [(he.2 hx.1).symm, hf.2 hx.2] } end /-- Restriction of local equivs respects equivalence -/ lemma eq_on_source.restr {e e' : local_equiv Ξ± Ξ²} (he : e β‰ˆ e') (s : set Ξ±) : e.restr s β‰ˆ e'.restr s := begin split, { simp [he.1] }, { assume x hx, simp only [mem_inter_eq, restr_source] at hx, exact he.2 hx.1 } end /-- Preimages are respected by equivalence -/ lemma eq_on_source.source_inter_preimage_eq {e e' : local_equiv Ξ± Ξ²} (he : e β‰ˆ e') (s : set Ξ²) : e.source ∩ e ⁻¹' s = e'.source ∩ e' ⁻¹' s := by rw [he.eq_on.inter_preimage_eq, he.source_eq] /-- Composition of a local equiv and its inverse is equivalent to the restriction of the identity to the source -/ lemma trans_self_symm : e.trans e.symm β‰ˆ local_equiv.of_set e.source := begin have A : (e.trans e.symm).source = e.source, by mfld_set_tac, refine ⟨by simp [A], Ξ»x hx, _⟩, rw A at hx, simp only [hx] with mfld_simps end /-- Composition of the inverse of a local equiv and this local equiv is equivalent to the restriction of the identity to the target -/ lemma trans_symm_self : e.symm.trans e β‰ˆ local_equiv.of_set e.target := trans_self_symm (e.symm) /-- Two equivalent local equivs are equal when the source and target are univ -/ lemma eq_of_eq_on_source_univ (e e' : local_equiv Ξ± Ξ²) (h : e β‰ˆ e') (s : e.source = univ) (t : e.target = univ) : e = e' := begin apply local_equiv.ext (Ξ»x, _) (Ξ»x, _) h.1, { apply h.2, rw s, exact mem_univ _ }, { apply h.symm'.2, rw [symm_source, t], exact mem_univ _ } end section prod /-- The product of two local equivs, as a local equiv on the product. -/ def prod (e : local_equiv Ξ± Ξ²) (e' : local_equiv Ξ³ Ξ΄) : local_equiv (Ξ± Γ— Ξ³) (Ξ² Γ— Ξ΄) := { source := set.prod e.source e'.source, target := set.prod e.target e'.target, to_fun := Ξ»p, (e p.1, e' p.2), inv_fun := Ξ»p, (e.symm p.1, e'.symm p.2), map_source' := Ξ»p hp, by { simp at hp, simp [hp] }, map_target' := Ξ»p hp, by { simp at hp, simp [map_target, hp] }, left_inv' := Ξ»p hp, by { simp at hp, simp [hp] }, right_inv' := Ξ»p hp, by { simp at hp, simp [hp] } } @[simp, mfld_simps] lemma prod_source (e : local_equiv Ξ± Ξ²) (e' : local_equiv Ξ³ Ξ΄) : (e.prod e').source = set.prod e.source e'.source := rfl @[simp, mfld_simps] lemma prod_target (e : local_equiv Ξ± Ξ²) (e' : local_equiv Ξ³ Ξ΄) : (e.prod e').target = set.prod e.target e'.target := rfl @[simp, mfld_simps] lemma prod_coe (e : local_equiv Ξ± Ξ²) (e' : local_equiv Ξ³ Ξ΄) : ((e.prod e') : Ξ± Γ— Ξ³ β†’ Ξ² Γ— Ξ΄) = (Ξ»p, (e p.1, e' p.2)) := rfl lemma prod_coe_symm (e : local_equiv Ξ± Ξ²) (e' : local_equiv Ξ³ Ξ΄) : ((e.prod e').symm : Ξ² Γ— Ξ΄ β†’ Ξ± Γ— Ξ³) = (Ξ»p, (e.symm p.1, e'.symm p.2)) := rfl @[simp, mfld_simps] lemma prod_symm (e : local_equiv Ξ± Ξ²) (e' : local_equiv Ξ³ Ξ΄) : (e.prod e').symm = (e.symm.prod e'.symm) := by ext x; simp [prod_coe_symm] @[simp, mfld_simps] lemma prod_trans {Ξ· : Type*} {Ξ΅ : Type*} (e : local_equiv Ξ± Ξ²) (f : local_equiv Ξ² Ξ³) (e' : local_equiv Ξ΄ Ξ·) (f' : local_equiv Ξ· Ξ΅) : (e.prod e').trans (f.prod f') = (e.trans f).prod (e'.trans f') := by ext x; simp [ext_iff]; tauto end prod /-- Combine two `local_equiv`s using `set.piecewise`. The source of the new `local_equiv` is `s.ite e.source e'.source = e.source ∩ s βˆͺ e'.source \ s`, and similarly for target. The function sends `e.source ∩ s` to `e.target ∩ t` using `e` and `e'.source \ s` to `e'.target \ t` using `e'`, and similarly for the inverse function. The definition assumes `e.is_image s t` and `e'.is_image s t`. -/ @[simps] def piecewise (e e' : local_equiv Ξ± Ξ²) (s : set Ξ±) (t : set Ξ²) [βˆ€ x, decidable (x ∈ s)] [βˆ€ y, decidable (y ∈ t)] (H : e.is_image s t) (H' : e'.is_image s t) : local_equiv Ξ± Ξ² := { to_fun := s.piecewise e e', inv_fun := t.piecewise e.symm e'.symm, source := s.ite e.source e'.source, target := t.ite e.target e'.target, map_source' := H.maps_to.piecewise_ite H'.compl.maps_to, map_target' := H.symm.maps_to.piecewise_ite H'.symm.compl.maps_to, left_inv' := H.left_inv_on_piecewise H', right_inv' := H.symm.left_inv_on_piecewise H'.symm } lemma symm_piecewise (e e' : local_equiv Ξ± Ξ²) {s : set Ξ±} {t : set Ξ²} [βˆ€ x, decidable (x ∈ s)] [βˆ€ y, decidable (y ∈ t)] (H : e.is_image s t) (H' : e'.is_image s t) : (e.piecewise e' s t H H').symm = e.symm.piecewise e'.symm t s H.symm H'.symm := rfl /-- Combine two `local_equiv`s with disjoint sources and disjoint targets. We reuse `local_equiv.piecewise`, then override `source` and `target` to ensure better definitional equalities. -/ @[simps] def disjoint_union (e e' : local_equiv Ξ± Ξ²) (hs : disjoint e.source e'.source) (ht : disjoint e.target e'.target) [βˆ€ x, decidable (x ∈ e.source)] [βˆ€ y, decidable (y ∈ e.target)] : local_equiv Ξ± Ξ² := (e.piecewise e' e.source e.target e.is_image_source_target $ e'.is_image_source_target_of_disjoint _ hs.symm ht.symm).copy _ rfl _ rfl (e.source βˆͺ e'.source) (ite_left _ _) (e.target βˆͺ e'.target) (ite_left _ _) lemma disjoint_union_eq_piecewise (e e' : local_equiv Ξ± Ξ²) (hs : disjoint e.source e'.source) (ht : disjoint e.target e'.target) [βˆ€ x, decidable (x ∈ e.source)] [βˆ€ y, decidable (y ∈ e.target)] : e.disjoint_union e' hs ht = e.piecewise e' e.source e.target e.is_image_source_target (e'.is_image_source_target_of_disjoint _ hs.symm ht.symm) := copy_eq_self _ _ _ _ _ _ _ _ _ section pi variables {ΞΉ : Type*} {Ξ±i Ξ²i : ΞΉ β†’ Type*} (ei : Ξ  i, local_equiv (Ξ±i i) (Ξ²i i)) /-- The product of a family of local equivs, as a local equiv on the pi type. -/ @[simps source target] protected def pi : local_equiv (Ξ  i, Ξ±i i) (Ξ  i, Ξ²i i) := { to_fun := Ξ» f i, ei i (f i), inv_fun := Ξ» f i, (ei i).symm (f i), source := pi univ (Ξ» i, (ei i).source), target := pi univ (Ξ» i, (ei i).target), map_source' := Ξ» f hf i hi, (ei i).map_source (hf i hi), map_target' := Ξ» f hf i hi, (ei i).map_target (hf i hi), left_inv' := Ξ» f hf, funext $ Ξ» i, (ei i).left_inv (hf i trivial), right_inv' := Ξ» f hf, funext $ Ξ» i, (ei i).right_inv (hf i trivial) } attribute [mfld_simps] pi_source pi_target @[simp, mfld_simps] lemma pi_coe : ⇑(local_equiv.pi ei) = Ξ» (f : Ξ  i, Ξ±i i) i, ei i (f i) := rfl @[simp, mfld_simps] lemma pi_symm : (local_equiv.pi ei).symm = local_equiv.pi (Ξ» i, (ei i).symm) := rfl end pi end local_equiv namespace set -- All arguments are explicit to avoid missing information in the pretty printer output /-- A bijection between two sets `s : set Ξ±` and `t : set Ξ²` provides a local equivalence between `Ξ±` and `Ξ²`. -/ @[simps] noncomputable def bij_on.to_local_equiv [nonempty Ξ±] (f : Ξ± β†’ Ξ²) (s : set Ξ±) (t : set Ξ²) (hf : bij_on f s t) : local_equiv Ξ± Ξ² := { to_fun := f, inv_fun := inv_fun_on f s, source := s, target := t, map_source' := hf.maps_to, map_target' := hf.surj_on.maps_to_inv_fun_on, left_inv' := hf.inv_on_inv_fun_on.1, right_inv' := hf.inv_on_inv_fun_on.2 } /-- A map injective on a subset of its domain provides a local equivalence. -/ @[simp, mfld_simps] noncomputable def inj_on.to_local_equiv [nonempty Ξ±] (f : Ξ± β†’ Ξ²) (s : set Ξ±) (hf : inj_on f s) : local_equiv Ξ± Ξ² := hf.bij_on_image.to_local_equiv f s (f '' s) end set namespace equiv /- equivs give rise to local_equiv. We set up simp lemmas to reduce most properties of the local equiv to that of the equiv. -/ variables (e : equiv Ξ± Ξ²) (e' : equiv Ξ² Ξ³) @[simp, mfld_simps] lemma to_local_equiv_coe : (e.to_local_equiv : Ξ± β†’ Ξ²) = e := rfl @[simp, mfld_simps] lemma to_local_equiv_symm_coe : (e.to_local_equiv.symm : Ξ² β†’ Ξ±) = e.symm := rfl @[simp, mfld_simps] lemma to_local_equiv_source : e.to_local_equiv.source = univ := rfl @[simp, mfld_simps] lemma to_local_equiv_target : e.to_local_equiv.target = univ := rfl @[simp, mfld_simps] lemma refl_to_local_equiv : (equiv.refl Ξ±).to_local_equiv = local_equiv.refl Ξ± := rfl @[simp, mfld_simps] lemma symm_to_local_equiv : e.symm.to_local_equiv = e.to_local_equiv.symm := rfl @[simp, mfld_simps] lemma trans_to_local_equiv : (e.trans e').to_local_equiv = e.to_local_equiv.trans e'.to_local_equiv := local_equiv.ext (Ξ»x, rfl) (Ξ»x, rfl) (by simp [local_equiv.trans_source, equiv.to_local_equiv]) end equiv
f8822d7230f4664ec3d487d404edd80238216e0b
37a833c924892ee3ecb911484775a6d6ebb8984d
/src/category_theory/yoneda_comparisons.lean
ced08cd5b9997d70bb6882cd423ed1f089c9c604
[]
no_license
silky/lean-category-theory
28126e80564a1f99e9c322d86b3f7d750da0afa1
0f029a2364975f56ac727d31d867a18c95c22fd8
refs/heads/master
1,589,555,811,646
1,554,673,665,000
1,554,673,665,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,987
lean
import category_theory.yoneda import category_theory.follow_your_nose universes u₁ v₁ open category_theory -- Unimath, Coq -- https://github.com/UniMath/UniMath/blob/master/UniMath/CategoryTheory/yoneda.v -- Greg O'Keefe, Isabelle -- https://www.isa-afp.org/browser_info/current/AFP/Category/document.pdf -- Alexander Katovsky, Isabelle -- https://www.isa-afp.org/browser_info/current/AFP/Category2/document.pdf -- Gross, Chlipala, Spivak, Coq -- https://arxiv.org/src/1401.7694v2/anc/HoTT/theories/categories/Yoneda.v variables (C : Type u₁) [π’ž : category.{u₁ v₁} C] include π’ž def yoneda_0 : C β₯€ ((Cα΅’α΅–) β₯€ (Type v₁)) := { obj := Ξ» X, { obj := Ξ» Y : C, Y ⟢ X, map := Ξ» Y Y' f g, f ≫ g, map_comp' := begin intros X_1 Y Z f g, ext1, dsimp at *, erw [category.assoc] end, map_id' := begin intros X_1, ext1, dsimp at *, erw [category.id_comp] end }, map := Ξ» X X' f, { app := Ξ» Y g, g ≫ f, naturality' := begin intros X_1 Y f_1, ext1, dsimp at *, simp at * end }, map_comp' := begin intros X Y Z f g, ext1, ext1, dsimp at *, simp at * end, map_id' := begin intros X, ext1, ext1, dsimp at *, simp at * end } def yoneda_1 : C β₯€ ((Cα΅’α΅–) β₯€ (Type v₁)) := { obj := Ξ» X, { obj := Ξ» Y : C, Y ⟢ X, map := Ξ» Y Y' f g, f ≫ g, map_comp' := begin intros X_1 Y Z f g, ext1, dsimp at *, erw [category.assoc] end, map_id' := begin intros X_1, ext1, dsimp at *, erw [category.id_comp] end }, map := Ξ» X X' f, { app := Ξ» Y g, g ≫ f } } def yoneda_2 : C β₯€ ((Cα΅’α΅–) β₯€ (Type v₁)) := { obj := Ξ» X, { obj := Ξ» Y : C, Y ⟢ X, map := Ξ» Y Y' f g, f ≫ g }, map := Ξ» X X' f, { app := Ξ» Y g, g ≫ f } } def yoneda_3 : C β₯€ ((Cα΅’α΅–) β₯€ (Type v₁)) := Ζ› X, Ζ› Y : C, Y ⟢ X. def yoneda_lemma' : (yoneda_pairing C) β‰… (yoneda_evaluation C) := { hom := { app := Ξ» F x, ulift.up ((x.app F.1) (πŸ™ F.1)) }, inv := { app := Ξ» F x, { app := Ξ» X a, (F.2.map a) x.down } } }.
9a916ae816357e09ee902e7ad6c8add56de95dcc
bd12a817ba941113eb7fdb7ddf0979d9ed9386a0
/src/algebraic_geometry/presheafed_space.lean
04e407cb2fab94001bf2a90a29f389f416a5d363
[ "Apache-2.0" ]
permissive
flypitch/mathlib
563d9c3356c2885eb6cefaa704d8d86b89b74b15
70cd00bc20ad304f2ac0886b2291b44261787607
refs/heads/master
1,590,167,818,658
1,557,762,121,000
1,557,762,121,000
186,450,076
0
0
Apache-2.0
1,557,762,289,000
1,557,762,288,000
null
UTF-8
Lean
false
false
5,422
lean
-- Copyright (c) 2019 Scott Morrison. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Scott Morrison import category_theory.instances.Top.presheaf universes v u open category_theory open category_theory.instances open category_theory.instances.Top open topological_space variables (C : Type u) [π’ž : category.{v+1} C] include π’ž namespace algebraic_geometry structure PresheafedSpace := (to_Top : Top.{v}) (π’ͺ : to_Top.presheaf C) variables {C} namespace PresheafedSpace instance : has_coe_to_sort (PresheafedSpace.{v} C) := { S := Type v, coe := Ξ» F, F.to_Top.Ξ± } instance (X : PresheafedSpace.{v} C) : topological_space X := X.to_Top.str structure hom (X Y : PresheafedSpace.{v} C) := (f : X.to_Top ⟢ Y.to_Top) (c : Y.π’ͺ ⟢ f _* X.π’ͺ) @[extensionality] lemma ext {X Y : PresheafedSpace.{v} C} (Ξ± Ξ² : hom X Y) (w : Ξ±.f = Ξ².f) (h : Ξ±.c ≫ (whisker_right (nat_trans.op (opens.map_iso _ _ w).inv) X.π’ͺ) = Ξ².c) : Ξ± = Ξ² := begin cases Ξ±, cases Ξ², dsimp [presheaf.pushforward] at *, tidy, -- TODO including `injections` would make tidy work earlier. end . def id (X : PresheafedSpace.{v} C) : hom X X := { f := πŸ™ X.to_Top, c := ((functor.left_unitor _).inv) ≫ (whisker_right (nat_trans.op (opens.map_id _).hom) _) } def comp (X Y Z : PresheafedSpace.{v} C) (Ξ± : hom X Y) (Ξ² : hom Y Z) : hom X Z := { f := Ξ±.f ≫ Ξ².f, c := Ξ².c ≫ (whisker_left (opens.map Ξ².f).op Ξ±.c) } variables (C) section local attribute [simp] id comp presheaf.pushforward instance category_of_PresheafedSpaces : category (PresheafedSpace.{v} C) := { hom := hom, id := id, comp := comp, -- I'm still grumpy about these proofs. -- The obstacle here is the mysterious need to use `erw` for some `simp` lemmas. -- If we could avoid that, locally adding `op_induction` to `tidy` would discharge these. comp_id' := Ξ» X Y f, begin ext U, { op_induction U, cases U, dsimp, simp, }, { simp } end, id_comp' := Ξ» X Y f, begin ext U, { op_induction U, cases U, dsimp, simp only [category.assoc], -- This should be done by `simp`, but unfortunately isn't. erw [category_theory.functor.map_id], simp, }, { simp } end, assoc' := Ξ» W X Y Z f g h, begin ext U, { op_induction U, cases U, dsimp, simp only [category.assoc], -- This should be done by `simp`, but unfortunately isn't. erw [category_theory.functor.map_id], simp, }, { refl } end } end . variables {C} instance {X Y : PresheafedSpace.{v} C} : has_coe (X ⟢ Y) (X.to_Top ⟢ Y.to_Top) := { coe := Ξ» Ξ±, Ξ±.f } @[simp] lemma id_f (X : PresheafedSpace.{v} C) : ((πŸ™ X) : X ⟢ X).f = πŸ™ X.to_Top := rfl @[simp] lemma comp_f {X Y Z : PresheafedSpace.{v} C} (Ξ± : X ⟢ Y) (Ξ² : Y ⟢ Z) : (Ξ± ≫ Ξ²).f = Ξ±.f ≫ Ξ².f := rfl @[simp] lemma comp_coe {X Y Z : PresheafedSpace.{v} C} (Ξ± : X ⟢ Y) (Ξ² : Y ⟢ Z) : ((Ξ± ≫ Ξ² : X ⟢ Z) : X.to_Top ⟢ Z.to_Top) = (Ξ± : X.to_Top ⟢ Y.to_Top) ≫ (Ξ² : Y.to_Top ⟢ Z.to_Top) := rfl -- We don't mark these as simp lemmas, because the innards are pretty unsightly. lemma id_c (X : PresheafedSpace.{v} C) : ((πŸ™ X) : X ⟢ X).c = (((functor.left_unitor _).inv) ≫ (whisker_right (nat_trans.op (opens.map_id _).hom) _)) := rfl lemma comp_c {X Y Z : PresheafedSpace.{v} C} (Ξ± : X ⟢ Y) (Ξ² : Y ⟢ Z) : (Ξ± ≫ Ξ²).c = (Ξ².c ≫ (whisker_left (opens.map Ξ².f).op Ξ±.c)) := rfl def forget : PresheafedSpace.{v} C β₯€ Top := { obj := Ξ» X, X.to_Top, map := Ξ» X Y f, f } end PresheafedSpace end algebraic_geometry open algebraic_geometry variables {C} namespace category_theory variables {D : Type u} [π’Ÿ : category.{v+1} D] include π’Ÿ local attribute [simp] PresheafedSpace.id_c PresheafedSpace.comp_c presheaf.pushforward namespace functor def map_presheaf (F : C β₯€ D) : PresheafedSpace.{v} C β₯€ PresheafedSpace.{v} D := { obj := Ξ» X, { to_Top := X.to_Top, π’ͺ := X.π’ͺ β‹™ F }, map := Ξ» X Y f, { f := f.f, c := whisker_right f.c F } }. @[simp] lemma map_presheaf_obj_X (F : C β₯€ D) (X : PresheafedSpace.{v} C) : (F.map_presheaf.obj X).to_Top = X.to_Top := rfl @[simp] lemma map_presheaf_obj_π’ͺ (F : C β₯€ D) (X : PresheafedSpace.{v} C) : (F.map_presheaf.obj X).π’ͺ = X.π’ͺ β‹™ F := rfl @[simp] lemma map_presheaf_map_f (F : C β₯€ D) {X Y : PresheafedSpace.{v} C} (f : X ⟢ Y) : (F.map_presheaf.map f).f = f := rfl @[simp] lemma map_presheaf_map_c (F : C β₯€ D) {X Y : PresheafedSpace.{v} C} (f : X ⟢ Y) : (F.map_presheaf.map f).c = whisker_right f.c F := rfl end functor namespace nat_trans def on_presheaf {F G : C β₯€ D} (Ξ± : F ⟢ G) : G.map_presheaf ⟢ F.map_presheaf := { app := Ξ» X, { f := πŸ™ _, c := whisker_left X.π’ͺ Ξ± ≫ ((functor.left_unitor _).inv) ≫ (whisker_right (nat_trans.op (opens.map_id _).hom) _) }, naturality' := Ξ» X Y f, begin ext U, { op_induction U, cases U, dsimp, simp only [functor.map_id, category.id_comp, category.comp_id, category.assoc], -- This should be done by `simp`, but unfortunately isn't. erw category_theory.functor.map_id, erw category_theory.functor.map_id, simp only [category.comp_id], exact (Ξ±.naturality _).symm, }, { refl, } end }. end nat_trans end category_theory
3ee39523177f6c806bac6659592af0290d97ece4
42610cc2e5db9c90269470365e6056df0122eaa0
/hott/types/eq.hlean
af916be3b7feb86d805a0dc21417070f02b82c05
[ "Apache-2.0" ]
permissive
tomsib2001/lean
2ab59bfaebd24a62109f800dcf4a7139ebd73858
eb639a7d53fb40175bea5c8da86b51d14bb91f76
refs/heads/master
1,586,128,387,740
1,468,968,950,000
1,468,968,950,000
61,027,234
0
0
null
1,465,813,585,000
1,465,813,585,000
null
UTF-8
Lean
false
false
21,515
hlean
/- Copyright (c) 2014 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Floris van Doorn Partially ported from Coq HoTT Theorems about path types (identity types) -/ import types.sigma open eq sigma sigma.ops equiv is_equiv is_trunc -- TODO: Rename transport_eq_... and pathover_eq_... to eq_transport_... and eq_pathover_... namespace eq /- Path spaces -/ section variables {A B : Type} {a a₁ aβ‚‚ a₃ aβ‚„ a' : A} {b b1 b2 : B} {f g : A β†’ B} {h : B β†’ A} {p p' p'' : a₁ = aβ‚‚} /- The path spaces of a path space are not, of course, determined; they are just the higher-dimensional structure of the original space. -/ /- some lemmas about whiskering or other higher paths -/ theorem whisker_left_con_right (p : a₁ = aβ‚‚) {q q' q'' : aβ‚‚ = a₃} (r : q = q') (s : q' = q'') : whisker_left p (r ⬝ s) = whisker_left p r ⬝ whisker_left p s := begin induction p, induction r, induction s, reflexivity end theorem whisker_right_con_right (q : aβ‚‚ = a₃) (r : p = p') (s : p' = p'') : whisker_right (r ⬝ s) q = whisker_right r q ⬝ whisker_right s q := begin induction q, induction r, induction s, reflexivity end theorem whisker_left_con_left (p : a₁ = aβ‚‚) (p' : aβ‚‚ = a₃) {q q' : a₃ = aβ‚„} (r : q = q') : whisker_left (p ⬝ p') r = !con.assoc ⬝ whisker_left p (whisker_left p' r) ⬝ !con.assoc' := begin induction p', induction p, induction r, induction q, reflexivity end theorem whisker_right_con_left {p p' : a₁ = aβ‚‚} (q : aβ‚‚ = a₃) (q' : a₃ = aβ‚„) (r : p = p') : whisker_right r (q ⬝ q') = !con.assoc' ⬝ whisker_right (whisker_right r q) q' ⬝ !con.assoc := begin induction q', induction q, induction r, induction p, reflexivity end theorem whisker_left_inv_left (p : aβ‚‚ = a₁) {q q' : aβ‚‚ = a₃} (r : q = q') : !con_inv_cancel_left⁻¹ ⬝ whisker_left p (whisker_left p⁻¹ r) ⬝ !con_inv_cancel_left = r := begin induction p, induction r, induction q, reflexivity end theorem whisker_left_inv (p : a₁ = aβ‚‚) {q q' : aβ‚‚ = a₃} (r : q = q') : whisker_left p r⁻¹ = (whisker_left p r)⁻¹ := by induction r; reflexivity theorem whisker_right_inv {p p' : a₁ = aβ‚‚} (q : aβ‚‚ = a₃) (r : p = p') : whisker_right r⁻¹ q = (whisker_right r q)⁻¹ := by induction r; reflexivity theorem ap_eq_ap10 {f g : A β†’ B} (p : f = g) (a : A) : ap (Ξ»h, h a) p = ap10 p a := by induction p;reflexivity theorem inverse2_right_inv (r : p = p') : r β—Ύ inverse2 r ⬝ con.right_inv p' = con.right_inv p := by induction r;induction p;reflexivity theorem inverse2_left_inv (r : p = p') : inverse2 r β—Ύ r ⬝ con.left_inv p' = con.left_inv p := by induction r;induction p;reflexivity theorem ap_con_right_inv (f : A β†’ B) (p : a₁ = aβ‚‚) : ap_con f p p⁻¹ ⬝ whisker_left _ (ap_inv f p) ⬝ con.right_inv (ap f p) = ap (ap f) (con.right_inv p) := by induction p;reflexivity theorem ap_con_left_inv (f : A β†’ B) (p : a₁ = aβ‚‚) : ap_con f p⁻¹ p ⬝ whisker_right (ap_inv f p) _ ⬝ con.left_inv (ap f p) = ap (ap f) (con.left_inv p) := by induction p;reflexivity theorem idp_con_whisker_left {q q' : aβ‚‚ = a₃} (r : q = q') : !idp_con⁻¹ ⬝ whisker_left idp r = r ⬝ !idp_con⁻¹ := by induction r;induction q;reflexivity theorem whisker_left_idp_con {q q' : aβ‚‚ = a₃} (r : q = q') : whisker_left idp r ⬝ !idp_con = !idp_con ⬝ r := by induction r;induction q;reflexivity theorem idp_con_idp {p : a = a} (q : p = idp) : idp_con p ⬝ q = ap (Ξ»p, idp ⬝ p) q := by cases q;reflexivity definition ap_is_constant [unfold 8] {A B : Type} {f : A β†’ B} {b : B} (p : Ξ x, f x = b) {x y : A} (q : x = y) : ap f q = p x ⬝ (p y)⁻¹ := by induction q;exact !con.right_inv⁻¹ definition inv2_inv {p q : a = a'} (r : p = q) : inverse2 r⁻¹ = (inverse2 r)⁻¹ := by induction r;reflexivity definition inv2_con {p p' p'' : a = a'} (r : p = p') (r' : p' = p'') : inverse2 (r ⬝ r') = inverse2 r ⬝ inverse2 r' := by induction r';induction r;reflexivity definition con2_inv {p₁ q₁ : a₁ = aβ‚‚} {pβ‚‚ qβ‚‚ : aβ‚‚ = a₃} (r₁ : p₁ = q₁) (rβ‚‚ : pβ‚‚ = qβ‚‚) : (r₁ β—Ύ rβ‚‚)⁻¹ = r₁⁻¹ β—Ύ r₂⁻¹ := by induction r₁;induction rβ‚‚;reflexivity theorem eq_con_inv_of_con_eq_whisker_left {A : Type} {a aβ‚‚ a₃ : A} {p : a = aβ‚‚} {q q' : aβ‚‚ = a₃} {r : a = a₃} (s' : q = q') (s : p ⬝ q' = r) : eq_con_inv_of_con_eq (whisker_left p s' ⬝ s) = eq_con_inv_of_con_eq s ⬝ whisker_left r (inverse2 s')⁻¹ := by induction s';induction q;induction s;reflexivity theorem right_inv_eq_idp {A : Type} {a : A} {p : a = a} (r : p = idpath a) : con.right_inv p = r β—Ύ inverse2 r := by cases r;reflexivity /- Transporting in path spaces. There are potentially a lot of these lemmas, so we adopt a uniform naming scheme: - `l` means the left endpoint varies - `r` means the right endpoint varies - `F` means application of a function to that (varying) endpoint. -/ definition transport_eq_l (p : a₁ = aβ‚‚) (q : a₁ = a₃) : transport (Ξ»x, x = a₃) p q = p⁻¹ ⬝ q := by induction p; induction q; reflexivity definition transport_eq_r (p : aβ‚‚ = a₃) (q : a₁ = aβ‚‚) : transport (Ξ»x, a₁ = x) p q = q ⬝ p := by induction p; induction q; reflexivity definition transport_eq_lr (p : a₁ = aβ‚‚) (q : a₁ = a₁) : transport (Ξ»x, x = x) p q = p⁻¹ ⬝ q ⬝ p := by induction p; rewrite [β–Έ*,idp_con] definition transport_eq_Fl (p : a₁ = aβ‚‚) (q : f a₁ = b) : transport (Ξ»x, f x = b) p q = (ap f p)⁻¹ ⬝ q := by induction p; induction q; reflexivity definition transport_eq_Fr (p : a₁ = aβ‚‚) (q : b = f a₁) : transport (Ξ»x, b = f x) p q = q ⬝ (ap f p) := by induction p; reflexivity definition transport_eq_FlFr (p : a₁ = aβ‚‚) (q : f a₁ = g a₁) : transport (Ξ»x, f x = g x) p q = (ap f p)⁻¹ ⬝ q ⬝ (ap g p) := by induction p; rewrite [β–Έ*,idp_con] definition transport_eq_FlFr_D {B : A β†’ Type} {f g : Ξ a, B a} (p : a₁ = aβ‚‚) (q : f a₁ = g a₁) : transport (Ξ»x, f x = g x) p q = (apdt f p)⁻¹ ⬝ ap (transport B p) q ⬝ (apdt g p) := by induction p; rewrite [β–Έ*,idp_con,ap_id] definition transport_eq_FFlr (p : a₁ = aβ‚‚) (q : h (f a₁) = a₁) : transport (Ξ»x, h (f x) = x) p q = (ap h (ap f p))⁻¹ ⬝ q ⬝ p := by induction p; rewrite [β–Έ*,idp_con] definition transport_eq_lFFr (p : a₁ = aβ‚‚) (q : a₁ = h (f a₁)) : transport (Ξ»x, x = h (f x)) p q = p⁻¹ ⬝ q ⬝ (ap h (ap f p)) := by induction p; rewrite [β–Έ*,idp_con] /- Pathovers -/ -- In the comment we give the fibration of the pathover -- we should probably try to do everything just with pathover_eq (defined in cubical.square), -- the following definitions may be removed in future. definition pathover_eq_l (p : a₁ = aβ‚‚) (q : a₁ = a₃) : q =[p] p⁻¹ ⬝ q := /-(Ξ»x, x = a₃)-/ by induction p; induction q; exact idpo definition pathover_eq_r (p : aβ‚‚ = a₃) (q : a₁ = aβ‚‚) : q =[p] q ⬝ p := /-(Ξ»x, a₁ = x)-/ by induction p; induction q; exact idpo definition pathover_eq_lr (p : a₁ = aβ‚‚) (q : a₁ = a₁) : q =[p] p⁻¹ ⬝ q ⬝ p := /-(Ξ»x, x = x)-/ by induction p; rewrite [β–Έ*,idp_con]; exact idpo definition pathover_eq_Fl (p : a₁ = aβ‚‚) (q : f a₁ = b) : q =[p] (ap f p)⁻¹ ⬝ q := /-(Ξ»x, f x = b)-/ by induction p; induction q; exact idpo definition pathover_eq_Fr (p : a₁ = aβ‚‚) (q : b = f a₁) : q =[p] q ⬝ (ap f p) := /-(Ξ»x, b = f x)-/ by induction p; exact idpo definition pathover_eq_FlFr (p : a₁ = aβ‚‚) (q : f a₁ = g a₁) : q =[p] (ap f p)⁻¹ ⬝ q ⬝ (ap g p) := /-(Ξ»x, f x = g x)-/ by induction p; rewrite [β–Έ*,idp_con]; exact idpo definition pathover_eq_FlFr_D {B : A β†’ Type} {f g : Ξ a, B a} (p : a₁ = aβ‚‚) (q : f a₁ = g a₁) : q =[p] (apdt f p)⁻¹ ⬝ ap (transport B p) q ⬝ (apdt g p) := /-(Ξ»x, f x = g x)-/ by induction p; rewrite [β–Έ*,idp_con,ap_id];exact idpo definition pathover_eq_FFlr (p : a₁ = aβ‚‚) (q : h (f a₁) = a₁) : q =[p] (ap h (ap f p))⁻¹ ⬝ q ⬝ p := /-(Ξ»x, h (f x) = x)-/ by induction p; rewrite [β–Έ*,idp_con];exact idpo definition pathover_eq_lFFr (p : a₁ = aβ‚‚) (q : a₁ = h (f a₁)) : q =[p] p⁻¹ ⬝ q ⬝ (ap h (ap f p)) := /-(Ξ»x, x = h (f x))-/ by induction p; rewrite [β–Έ*,idp_con];exact idpo definition pathover_eq_r_idp (p : a₁ = aβ‚‚) : idp =[p] p := /-(Ξ»x, a₁ = x)-/ by induction p; exact idpo definition pathover_eq_l_idp (p : a₁ = aβ‚‚) : idp =[p] p⁻¹ := /-(Ξ»x, x = a₁)-/ by induction p; exact idpo definition pathover_eq_l_idp' (p : a₁ = aβ‚‚) : idp =[p⁻¹] p := /-(Ξ»x, x = aβ‚‚)-/ by induction p; exact idpo -- The Functorial action of paths is [ap]. /- Equivalences between path spaces -/ /- [ap_closed] is in init.equiv -/ definition equiv_ap (f : A β†’ B) [H : is_equiv f] (a₁ aβ‚‚ : A) : (a₁ = aβ‚‚) ≃ (f a₁ = f aβ‚‚) := equiv.mk (ap f) _ /- Path operations are equivalences -/ definition is_equiv_eq_inverse (a₁ aβ‚‚ : A) : is_equiv (inverse : a₁ = aβ‚‚ β†’ aβ‚‚ = a₁) := is_equiv.mk inverse inverse inv_inv inv_inv (Ξ»p, eq.rec_on p idp) local attribute is_equiv_eq_inverse [instance] definition eq_equiv_eq_symm (a₁ aβ‚‚ : A) : (a₁ = aβ‚‚) ≃ (aβ‚‚ = a₁) := equiv.mk inverse _ definition is_equiv_concat_left [constructor] [instance] (p : a₁ = aβ‚‚) (a₃ : A) : is_equiv (concat p : aβ‚‚ = a₃ β†’ a₁ = a₃) := is_equiv.mk (concat p) (concat p⁻¹) (con_inv_cancel_left p) (inv_con_cancel_left p) abstract (Ξ»q, by induction p;induction q;reflexivity) end local attribute is_equiv_concat_left [instance] definition equiv_eq_closed_left [constructor] (a₃ : A) (p : a₁ = aβ‚‚) : (a₁ = a₃) ≃ (aβ‚‚ = a₃) := equiv.mk (concat p⁻¹) _ definition is_equiv_concat_right [constructor] [instance] (p : aβ‚‚ = a₃) (a₁ : A) : is_equiv (Ξ»q : a₁ = aβ‚‚, q ⬝ p) := is_equiv.mk (Ξ»q, q ⬝ p) (Ξ»q, q ⬝ p⁻¹) (Ξ»q, inv_con_cancel_right q p) (Ξ»q, con_inv_cancel_right q p) (Ξ»q, by induction p;induction q;reflexivity) local attribute is_equiv_concat_right [instance] definition equiv_eq_closed_right [constructor] (a₁ : A) (p : aβ‚‚ = a₃) : (a₁ = aβ‚‚) ≃ (a₁ = a₃) := equiv.mk (Ξ»q, q ⬝ p) _ definition eq_equiv_eq_closed [constructor] (p : a₁ = aβ‚‚) (q : a₃ = aβ‚„) : (a₁ = a₃) ≃ (aβ‚‚ = aβ‚„) := equiv.trans (equiv_eq_closed_left a₃ p) (equiv_eq_closed_right aβ‚‚ q) definition is_equiv_whisker_left [constructor] (p : a₁ = aβ‚‚) (q r : aβ‚‚ = a₃) : is_equiv (whisker_left p : q = r β†’ p ⬝ q = p ⬝ r) := begin fapply adjointify, {intro s, apply (!cancel_left s)}, {intro s, apply concat, {apply whisker_left_con_right}, apply concat, rotate_left 1, apply (whisker_left_inv_left p s), apply concat2, {apply concat, {apply whisker_left_con_right}, apply concat2, {induction p, induction q, reflexivity}, {reflexivity}}, {induction p, induction r, reflexivity}}, {intro s, induction s, induction q, induction p, reflexivity} end definition eq_equiv_con_eq_con_left [constructor] (p : a₁ = aβ‚‚) (q r : aβ‚‚ = a₃) : (q = r) ≃ (p ⬝ q = p ⬝ r) := equiv.mk _ !is_equiv_whisker_left definition is_equiv_whisker_right [constructor] {p q : a₁ = aβ‚‚} (r : aβ‚‚ = a₃) : is_equiv (Ξ»s, whisker_right s r : p = q β†’ p ⬝ r = q ⬝ r) := begin fapply adjointify, {intro s, apply (!cancel_right s)}, {intro s, induction r, cases s, induction q, reflexivity}, {intro s, induction s, induction r, induction p, reflexivity} end definition eq_equiv_con_eq_con_right [constructor] (p q : a₁ = aβ‚‚) (r : aβ‚‚ = a₃) : (p = q) ≃ (p ⬝ r = q ⬝ r) := equiv.mk _ !is_equiv_whisker_right /- The following proofs can be simplified a bit by concatenating previous equivalences. However, these proofs have the advantage that the inverse is definitionally equal to what we would expect -/ definition is_equiv_con_eq_of_eq_inv_con [constructor] (p : a₁ = a₃) (q : aβ‚‚ = a₃) (r : aβ‚‚ = a₁) : is_equiv (con_eq_of_eq_inv_con : p = r⁻¹ ⬝ q β†’ r ⬝ p = q) := begin fapply adjointify, { apply eq_inv_con_of_con_eq}, { intro s, induction r, rewrite [↑[con_eq_of_eq_inv_con,eq_inv_con_of_con_eq], con.assoc,con.assoc,con.left_inv,β–Έ*,-con.assoc,con.right_inv,β–Έ* at *,idp_con s]}, { intro s, induction r, rewrite [↑[con_eq_of_eq_inv_con,eq_inv_con_of_con_eq], con.assoc,con.assoc,con.right_inv,β–Έ*,-con.assoc,con.left_inv,β–Έ* at *,idp_con s] }, end definition eq_inv_con_equiv_con_eq [constructor] (p : a₁ = a₃) (q : aβ‚‚ = a₃) (r : aβ‚‚ = a₁) : (p = r⁻¹ ⬝ q) ≃ (r ⬝ p = q) := equiv.mk _ !is_equiv_con_eq_of_eq_inv_con definition is_equiv_con_eq_of_eq_con_inv [constructor] (p : a₁ = a₃) (q : aβ‚‚ = a₃) (r : aβ‚‚ = a₁) : is_equiv (con_eq_of_eq_con_inv : r = q ⬝ p⁻¹ β†’ r ⬝ p = q) := begin fapply adjointify, { apply eq_con_inv_of_con_eq}, { intro s, induction p, rewrite [↑[con_eq_of_eq_con_inv,eq_con_inv_of_con_eq]]}, { intro s, induction p, rewrite [↑[con_eq_of_eq_con_inv,eq_con_inv_of_con_eq]] }, end definition eq_con_inv_equiv_con_eq [constructor] (p : a₁ = a₃) (q : aβ‚‚ = a₃) (r : aβ‚‚ = a₁) : (r = q ⬝ p⁻¹) ≃ (r ⬝ p = q) := equiv.mk _ !is_equiv_con_eq_of_eq_con_inv definition is_equiv_inv_con_eq_of_eq_con [constructor] (p : a₁ = a₃) (q : aβ‚‚ = a₃) (r : a₁ = aβ‚‚) : is_equiv (inv_con_eq_of_eq_con : p = r ⬝ q β†’ r⁻¹ ⬝ p = q) := begin fapply adjointify, { apply eq_con_of_inv_con_eq}, { intro s, induction r, rewrite [↑[inv_con_eq_of_eq_con,eq_con_of_inv_con_eq], con.assoc,con.assoc,con.left_inv,β–Έ*,-con.assoc,con.right_inv,β–Έ* at *,idp_con s]}, { intro s, induction r, rewrite [↑[inv_con_eq_of_eq_con,eq_con_of_inv_con_eq], con.assoc,con.assoc,con.right_inv,β–Έ*,-con.assoc,con.left_inv,β–Έ* at *,idp_con s] }, end definition eq_con_equiv_inv_con_eq [constructor] (p : a₁ = a₃) (q : aβ‚‚ = a₃) (r : a₁ = aβ‚‚) : (p = r ⬝ q) ≃ (r⁻¹ ⬝ p = q) := equiv.mk _ !is_equiv_inv_con_eq_of_eq_con definition is_equiv_con_inv_eq_of_eq_con [constructor] (p : a₃ = a₁) (q : aβ‚‚ = a₃) (r : aβ‚‚ = a₁) : is_equiv (con_inv_eq_of_eq_con : r = q ⬝ p β†’ r ⬝ p⁻¹ = q) := begin fapply adjointify, { apply eq_con_of_con_inv_eq}, { intro s, induction p, rewrite [↑[con_inv_eq_of_eq_con,eq_con_of_con_inv_eq]]}, { intro s, induction p, rewrite [↑[con_inv_eq_of_eq_con,eq_con_of_con_inv_eq]] }, end definition eq_con_equiv_con_inv_eq (p : a₃ = a₁) (q : aβ‚‚ = a₃) (r : aβ‚‚ = a₁) : (r = q ⬝ p) ≃ (r ⬝ p⁻¹ = q) := equiv.mk _ !is_equiv_con_inv_eq_of_eq_con local attribute is_equiv_inv_con_eq_of_eq_con is_equiv_con_inv_eq_of_eq_con is_equiv_con_eq_of_eq_con_inv is_equiv_con_eq_of_eq_inv_con [instance] definition is_equiv_eq_con_of_inv_con_eq (p : a₁ = a₃) (q : aβ‚‚ = a₃) (r : aβ‚‚ = a₁) : is_equiv (eq_con_of_inv_con_eq : r⁻¹ ⬝ q = p β†’ q = r ⬝ p) := is_equiv_inv inv_con_eq_of_eq_con definition is_equiv_eq_con_of_con_inv_eq (p : a₁ = a₃) (q : aβ‚‚ = a₃) (r : aβ‚‚ = a₁) : is_equiv (eq_con_of_con_inv_eq : q ⬝ p⁻¹ = r β†’ q = r ⬝ p) := is_equiv_inv con_inv_eq_of_eq_con definition is_equiv_eq_con_inv_of_con_eq (p : a₁ = a₃) (q : aβ‚‚ = a₃) (r : aβ‚‚ = a₁) : is_equiv (eq_con_inv_of_con_eq : r ⬝ p = q β†’ r = q ⬝ p⁻¹) := is_equiv_inv con_eq_of_eq_con_inv definition is_equiv_eq_inv_con_of_con_eq (p : a₁ = a₃) (q : aβ‚‚ = a₃) (r : aβ‚‚ = a₁) : is_equiv (eq_inv_con_of_con_eq : r ⬝ p = q β†’ p = r⁻¹ ⬝ q) := is_equiv_inv con_eq_of_eq_inv_con definition is_equiv_con_inv_eq_idp [constructor] (p q : a₁ = aβ‚‚) : is_equiv (con_inv_eq_idp : p = q β†’ p ⬝ q⁻¹ = idp) := begin fapply adjointify, { apply eq_of_con_inv_eq_idp}, { intro s, induction q, esimp at *, cases s, reflexivity}, { intro s, induction s, induction p, reflexivity}, end definition is_equiv_inv_con_eq_idp [constructor] (p q : a₁ = aβ‚‚) : is_equiv (inv_con_eq_idp : p = q β†’ q⁻¹ ⬝ p = idp) := begin fapply adjointify, { apply eq_of_inv_con_eq_idp}, { intro s, induction q, esimp [eq_of_inv_con_eq_idp] at *, eapply is_equiv_rect (eq_equiv_con_eq_con_left idp p idp), clear s, intro s, cases s, reflexivity}, { intro s, induction s, induction p, reflexivity}, end definition eq_equiv_con_inv_eq_idp [constructor] (p q : a₁ = aβ‚‚) : (p = q) ≃ (p ⬝ q⁻¹ = idp) := equiv.mk _ !is_equiv_con_inv_eq_idp definition eq_equiv_inv_con_eq_idp [constructor] (p q : a₁ = aβ‚‚) : (p = q) ≃ (q⁻¹ ⬝ p = idp) := equiv.mk _ !is_equiv_inv_con_eq_idp /- Pathover Equivalences -/ definition pathover_eq_equiv_l (p : a₁ = aβ‚‚) (q : a₁ = a₃) (r : aβ‚‚ = a₃) : q =[p] r ≃ q = p ⬝ r := /-(Ξ»x, x = a₃)-/ by induction p; exact !pathover_idp ⬝e !equiv_eq_closed_right !idp_con⁻¹ definition pathover_eq_equiv_r (p : aβ‚‚ = a₃) (q : a₁ = aβ‚‚) (r : a₁ = a₃) : q =[p] r ≃ q ⬝ p = r := /-(Ξ»x, a₁ = x)-/ by induction p; apply pathover_idp definition pathover_eq_equiv_lr (p : a₁ = aβ‚‚) (q : a₁ = a₁) (r : aβ‚‚ = aβ‚‚) : q =[p] r ≃ q ⬝ p = p ⬝ r := /-(Ξ»x, x = x)-/ by induction p; exact !pathover_idp ⬝e !equiv_eq_closed_right !idp_con⁻¹ definition pathover_eq_equiv_Fl (p : a₁ = aβ‚‚) (q : f a₁ = b) (r : f aβ‚‚ = b) : q =[p] r ≃ q = ap f p ⬝ r := /-(Ξ»x, f x = b)-/ by induction p; exact !pathover_idp ⬝e !equiv_eq_closed_right !idp_con⁻¹ definition pathover_eq_equiv_Fr (p : a₁ = aβ‚‚) (q : b = f a₁) (r : b = f aβ‚‚) : q =[p] r ≃ q ⬝ ap f p = r := /-(Ξ»x, b = f x)-/ by induction p; apply pathover_idp definition pathover_eq_equiv_FlFr (p : a₁ = aβ‚‚) (q : f a₁ = g a₁) (r : f aβ‚‚ = g aβ‚‚) : q =[p] r ≃ q ⬝ ap g p = ap f p ⬝ r := /-(Ξ»x, f x = g x)-/ by induction p; exact !pathover_idp ⬝e !equiv_eq_closed_right !idp_con⁻¹ definition pathover_eq_equiv_FFlr (p : a₁ = aβ‚‚) (q : h (f a₁) = a₁) (r : h (f aβ‚‚) = aβ‚‚) : q =[p] r ≃ q ⬝ p = ap h (ap f p) ⬝ r := /-(Ξ»x, h (f x) = x)-/ by induction p; exact !pathover_idp ⬝e !equiv_eq_closed_right !idp_con⁻¹ definition pathover_eq_equiv_lFFr (p : a₁ = aβ‚‚) (q : a₁ = h (f a₁)) (r : aβ‚‚ = h (f aβ‚‚)) : q =[p] r ≃ q ⬝ ap h (ap f p) = p ⬝ r := /-(Ξ»x, x = h (f x))-/ by induction p; exact !pathover_idp ⬝e !equiv_eq_closed_right !idp_con⁻¹ -- a lot of this library still needs to be ported from Coq HoTT -- the behavior of equality in other types is described in the corresponding type files -- encode decode method open is_trunc definition encode_decode_method' (aβ‚€ a : A) (code : A β†’ Type) (cβ‚€ : code aβ‚€) (decode : Ξ (a : A) (c : code a), aβ‚€ = a) (encode_decode : Ξ (a : A) (c : code a), cβ‚€ =[decode a c] c) (decode_encode : decode aβ‚€ cβ‚€ = idp) : (aβ‚€ = a) ≃ code a := begin fapply equiv.MK, { intro p, exact p β–Έ cβ‚€}, { apply decode}, { intro c, apply tr_eq_of_pathover, apply encode_decode}, { intro p, induction p, apply decode_encode}, end end section parameters {A : Type} (aβ‚€ : A) (code : A β†’ Type) (H : is_contr (Ξ£a, code a)) (p : (center (Ξ£a, code a)).1 = aβ‚€) include p protected definition encode {a : A} (q : aβ‚€ = a) : code a := (p ⬝ q) β–Έ (center (Ξ£a, code a)).2 protected definition decode' {a : A} (c : code a) : aβ‚€ = a := (is_prop.elim ⟨aβ‚€, encode idp⟩ ⟨a, c⟩)..1 protected definition decode {a : A} (c : code a) : aβ‚€ = a := (decode' (encode idp))⁻¹ ⬝ decode' c definition total_space_method (a : A) : (aβ‚€ = a) ≃ code a := begin fapply equiv.MK, { exact encode}, { exact decode}, { intro c, unfold [encode, decode, decode'], induction p, esimp, rewrite [is_prop_elim_self,β–Έ*,+idp_con], apply tr_eq_of_pathover, eapply @sigma.rec_on _ _ (Ξ»x, x.2 =[(is_prop.elim ⟨x.1, x.2⟩ ⟨a, c⟩)..1] c) (center (sigma code)), intro a c, apply eq_pr2}, { intro q, induction q, esimp, apply con.left_inv, }, end end definition encode_decode_method {A : Type} (aβ‚€ a : A) (code : A β†’ Type) (cβ‚€ : code aβ‚€) (decode : Ξ (a : A) (c : code a), aβ‚€ = a) (encode_decode : Ξ (a : A) (c : code a), cβ‚€ =[decode a c] c) : (aβ‚€ = a) ≃ code a := begin fapply total_space_method, { fapply @is_contr.mk, { exact ⟨aβ‚€, cβ‚€βŸ©}, { intro p, fapply sigma_eq, apply decode, exact p.2, apply encode_decode}}, { reflexivity} end end eq
c7216c2e2ae95a4ea21260826e7af6d688548ce7
f1dc39e1c68f71465c8bf79910c4664d03824751
/tests/lean/123-2.lean
d458594303fa96696d552abd09d785d0ad5759a2
[ "Apache-2.0" ]
permissive
kckennylau/lean-2
6504f45da07bc98b098d726b74130103be25885c
c9a9368bc0fd600d832bd56c5cb2124b8a523ef9
refs/heads/master
1,659,140,308,864
1,589,361,166,000
1,589,361,166,000
263,748,786
0
0
null
1,589,405,915,000
1,589,405,915,000
null
UTF-8
Lean
false
false
1,713
lean
open function class has_scalar' (R : Type*) (A : Type*) := (smul : R β†’ A β†’ A) infixr ` β€’ `:73 := has_scalar'.smul structure ring_hom' (M : Type*) (N : Type*) [semiring M] [semiring N] := (to_fun : M β†’ N) (map_one' : to_fun 1 = 1) (map_mul' : βˆ€ x y, to_fun (x * y) = to_fun x * to_fun y) (map_zero' : to_fun 0 = 0) (map_add' : βˆ€ x y, to_fun (x + y) = to_fun x + to_fun y) instance (Ξ± : Type*) (Ξ² : Type*) [semiring Ξ±] [semiring Ξ²] : has_coe_to_fun (ring_hom' Ξ± Ξ²) := ⟨_, Ξ» f, ring_hom'.to_fun (f)⟩ class algebra' (R : Type*) (A : Type*) [comm_semiring R] [semiring A] extends has_scalar' R A, ring_hom' R A := (commutes' : βˆ€ r x, to_fun r * x = x * to_fun r) (smul_def' : βˆ€ r x, r β€’ x = to_fun r * x) def algebra_map' (R : Type*) (A : Type*) [comm_semiring R] [semiring A] [algebra' R A] : ring_hom' R A := algebra'.to_ring_hom' structure alg_hom' (R : Type*) (A : Type*) (B : Type*) [comm_semiring R] [semiring A] [semiring B] [algebra' R A] [algebra' R B] extends ring_hom' A B := (commutes' : βˆ€ r : R, to_fun (algebra_map' R A r) = algebra_map' R B r) variables {R : Type*} {A : Type*} {B : Type*} variables [comm_semiring R] [semiring A] [semiring B] variables [algebra' R A] [algebra' R B] instance : has_coe_to_fun (alg_hom' R A B) := ⟨_, Ξ» f, f.to_fun⟩ def quot.lift {R : Type} [comm_ring R] {A : Type} [comm_ring A] [algebra' R A] {B : Type*} [comm_ring B] [algebra' R B] {C : Type} [comm_ring C] [algebra' R C] (f : alg_hom' R A B) (hf : surjective f) (g : alg_hom' R A C) (hfg : βˆ€ a : A, f a = 0 β†’ g a = 0) : alg_hom' R B C := { to_fun := Ξ» b, _, map_one' := _, map_mul' := _, map_zero' := _, map_add' := _, commutes' := _ }
e4bab7f48fa96dbbee047e932323fcc354e8f412
c3f2fcd060adfa2ca29f924839d2d925e8f2c685
/tests/lean/run/tactic24.lean
9326510649090d8e12f16981dc6894cdda690b22
[ "Apache-2.0" ]
permissive
respu/lean
6582d19a2f2838a28ecd2b3c6f81c32d07b5341d
8c76419c60b63d0d9f7bc04ebb0b99812d0ec654
refs/heads/master
1,610,882,451,231
1,427,747,084,000
1,427,747,429,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
548
lean
import logic open tactic definition my_tac1 := apply @eq.refl definition my_tac2 := repeat (apply @and.intro; assumption) tactic_hint my_tac1 tactic_hint my_tac2 theorem T1 {A : Type.{2}} (a : A) : a = a := _ theorem T2 {a b c : Prop} (Ha : a) (Hb : b) (Hc : c) : a ∧ b ∧ c := _ definition my_tac3 := fixpoint (λ f, [apply @or.intro_left; f | apply @or.intro_right; f | assumption]) tactic_hint my_tac3 theorem T3 {a b c : Prop} (Hb : b) : a ∨ b ∨ c := _
28223780996d2190f8913d083e6fadd13210cd61
43dcf09a5df9dc4729cdc024e8680eeaacd05af3
/binary_op.lean
23123ce76c4248ed5bd2b1980a91b48cbe2c679d
[]
no_license
khoek/lmath
f9ea911aabee1eb276a5319e70a5e62e6bfe8fb1
a4f35205e6b5a3f16926234bf8eb7f7b5f56e47f
refs/heads/master
1,611,452,778,710
1,532,531,556,000
1,532,569,670,000
122,820,532
0
0
null
null
null
null
UTF-8
Lean
false
false
4,288
lean
import data.quot import tidy.tidy lemma eq_is_refl {Ξ± : Type} {r : Ξ± β†’ Ξ± β†’ Prop} : equivalence r β†’ reflexive r := by intro h; exact and.elim_left h lemma eq_is_symm {Ξ± : Type} {r : Ξ± β†’ Ξ± β†’ Prop} : equivalence r β†’ symmetric r := by intro h; exact and.elim_left (and.elim_right h) lemma eq_is_trans {Ξ± : Type} {r : Ξ± β†’ Ξ± β†’ Prop} : equivalence r β†’ transitive r := by intro h; exact and.elim_right (and.elim_right h) def bop (Ξ± : Type) := Ξ± β†’ Ξ± β†’ Ξ± def prebop (Ξ± Ξ² : Type) := Ξ± β†’ Ξ± β†’ Ξ² def bop_rel_assoc {Ξ± : Type} (r : Ξ± β†’ Ξ± β†’ Prop) (Ξ³ : bop Ξ±) := βˆ€ a b c : Ξ±, r (Ξ³ (Ξ³ a b) c) (Ξ³ a (Ξ³ b c)) def bop_rel_comm {Ξ± : Type} (r : Ξ± β†’ Ξ± β†’ Prop) (Ξ³ : bop Ξ±) := βˆ€ a b : Ξ±, r (Ξ³ a b) (Ξ³ b a) def bop_assoc {Ξ± : Type} (Ξ³ : bop Ξ±) := bop_rel_assoc eq Ξ³ def bop_comm {Ξ± : Type} (Ξ³ : bop Ξ±) := bop_rel_comm eq Ξ³ def bop_rel_invar_first {Ξ± : Type} (r : Ξ± β†’ Ξ± β†’ Prop) (Ξ³ : bop Ξ±) := βˆ€ a b c : Ξ±, r a b β†’ r (Ξ³ a c) (Ξ³ b c) def bop_rel_invar {Ξ± : Type} (r : Ξ± β†’ Ξ± β†’ Prop) (Ξ³ : bop Ξ±) := βˆ€ a b c d : Ξ±, r a c β†’ r b d β†’ r (Ξ³ a b) (Ξ³ c d) structure liftable_bop := mk :: {Ξ± : Type} (s : setoid Ξ±) (bop : bop Ξ±) (class_invar : bop_rel_invar s.r bop) lemma bop_comm_easy_invar {Ξ± : Type} {s : setoid Ξ±} {Ξ³ : bop Ξ±} : bop_rel_comm s.r Ξ³ β†’ bop_rel_invar_first s.r Ξ³ β†’ bop_rel_invar s.r Ξ³ := begin have htrans : transitive s.r, by exact eq_is_trans s.iseqv, have hsymm : symmetric s.r, by exact eq_is_symm s.iseqv, intros hcomm hfirst, unfold bop_rel_comm at *, unfold bop_rel_invar_first at *, unfold bop_rel_invar at *, intros a b c d hac hbd, apply htrans, apply hfirst a c b hac, apply htrans, apply hcomm c b, apply hsymm, apply htrans, apply hcomm c d, exact hfirst d b c (hsymm hbd), end def induced_quobop {Ξ± : Type} (s : setoid Ξ±) (Ξ³ : bop Ξ±) : prebop Ξ± (quotient s) := Ξ» a b : Ξ±, quot.mk s.r (Ξ³ a b) lemma lift_well_defined {Ξ± : Type} (s : setoid Ξ±) (Ξ³ : bop Ξ±) (class_invar : bop_rel_invar s.r Ξ³) : βˆ€ (a b c d : Ξ±), a β‰ˆ c β†’ b β‰ˆ d β†’ induced_quobop s Ξ³ a b = induced_quobop s Ξ³ c d := begin intros a b c d hac hbd, tidy, unfold bop_rel_invar at class_invar, specialize class_invar a b c d hac hbd, unfold induced_quobop, apply quot.sound, assumption, end def induced_bop {Ξ± : Type} (s : setoid Ξ±) (Ξ³ : bop Ξ±) (class_invar : bop_rel_invar s.r Ξ³) : bop (quotient s) := quotient.liftβ‚‚ (induced_quobop s Ξ³) (lift_well_defined s Ξ³ class_invar) def lift_bop (lb : liftable_bop) : bop (quotient lb.s) := induced_bop lb.s lb.bop lb.class_invar theorem compute_lift (lb : liftable_bop): βˆ€ a b : lb.Ξ±, (lift_bop lb) (quot.mk lb.s.r a) (quot.mk lb.s.r b) = quot.mk lb.s.r (lb.bop a b) := by tidy --FIXME a single tactic should solve both of these below lemma lift_assoc (lb : liftable_bop) : bop_rel_assoc lb.s.r lb.bop β†’ bop_assoc (lift_bop lb) := begin intros hassoc a b c, -- unfold bop_assoc, -- unfold bop_rel_assoc at *, have haw : quot.mk lb.s.r (quot.out a) = a, by apply quotient.out_eq, have hbw : quot.mk lb.s.r (quot.out b) = b, by apply quotient.out_eq, have hcw : quot.mk lb.s.r (quot.out c) = c, by apply quotient.out_eq, rw eq.symm haw, rw eq.symm hbw, rw eq.symm hcw, rewrite (compute_lift lb (quot.out a) (quot.out b)), rewrite (compute_lift lb (quot.out b) (quot.out c)), rewrite (compute_lift lb (lb.bop (quot.out a) (quot.out b)) (quot.out c)), rewrite (compute_lift lb (quot.out a) (lb.bop (quot.out b) (quot.out c))), apply quot.sound, exact hassoc (quot.out a) (quot.out b) (quot.out c), end lemma lift_comm (lb : liftable_bop) : bop_rel_comm lb.s.r lb.bop β†’ bop_comm (lift_bop lb) := begin intros hcomm a b, -- unfold bop_comm, -- unfold bop_rel_comm at *, have haw : quot.mk lb.s.r (quot.out a) = a, by apply quotient.out_eq, have hbw : quot.mk lb.s.r (quot.out b) = b, by apply quotient.out_eq, rw eq.symm haw, rw eq.symm hbw, rewrite (compute_lift lb (quot.out a) (quot.out b)), rewrite (compute_lift lb (quot.out b) (quot.out a)), apply quot.sound, exact hcomm (quot.out a) (quot.out b), end
5676457658cc6cb24704164f1083b2d095bf2d3e
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/order/modular_lattice.lean
268d8a717d9a0d0a887d0936b310aa5da7072614
[]
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,468
lean
/- Copyright (c) 2020 Aaron Anderson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Aaron Anderson -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.order.rel_iso import Mathlib.order.lattice_intervals import Mathlib.order.order_dual import Mathlib.PostPort universes u_2 l u_1 namespace Mathlib /-! # Modular Lattices This file defines Modular Lattices, a kind of lattice useful in algebra. For examples, look to the subobject lattices of abelian groups, submodules, and ideals, or consider any distributive lattice. ## Main Definitions - `is_modular_lattice` defines a modular lattice to be one such that `x ≀ z β†’ (x βŠ” y) βŠ“ z ≀ x βŠ” (y βŠ“ z)` - `inf_Icc_order_iso_Icc_sup` gives an order isomorphism between the intervals `[a βŠ“ b, a]` and `[b, a βŠ” b]`. This corresponds to the diamond (or second) isomorphism theorems of algebra. ## Main Results - `is_modular_lattice_iff_sup_inf_sup_assoc`: Modularity is equivalent to the `sup_inf_sup_assoc`: `(x βŠ“ z) βŠ” (y βŠ“ z) = ((x βŠ“ z) βŠ” y) βŠ“ z` - `distrib_lattice.is_modular_lattice`: Distributive lattices are modular. ## To do - Relate atoms and coatoms in modular lattices -/ /-- A modular lattice is one with a limited associativity between `βŠ“` and `βŠ”`. -/ class is_modular_lattice (Ξ± : Type u_2) [lattice Ξ±] where sup_inf_le_assoc_of_le : βˆ€ {x : Ξ±} (y : Ξ±) {z : Ξ±}, x ≀ z β†’ (x βŠ” y) βŠ“ z ≀ x βŠ” y βŠ“ z theorem sup_inf_assoc_of_le {Ξ± : Type u_1} [lattice Ξ±] [is_modular_lattice Ξ±] {x : Ξ±} (y : Ξ±) {z : Ξ±} (h : x ≀ z) : (x βŠ” y) βŠ“ z = x βŠ” y βŠ“ z := le_antisymm (is_modular_lattice.sup_inf_le_assoc_of_le y h) (le_inf (sup_le_sup_left inf_le_left x) (sup_le h inf_le_right)) theorem is_modular_lattice.sup_inf_sup_assoc {Ξ± : Type u_1} [lattice Ξ±] [is_modular_lattice Ξ±] {x : Ξ±} {y : Ξ±} {z : Ξ±} : x βŠ“ z βŠ” y βŠ“ z = (x βŠ“ z βŠ” y) βŠ“ z := Eq.symm (sup_inf_assoc_of_le y inf_le_right) theorem inf_sup_assoc_of_le {Ξ± : Type u_1} [lattice Ξ±] [is_modular_lattice Ξ±] {x : Ξ±} (y : Ξ±) {z : Ξ±} (h : z ≀ x) : x βŠ“ y βŠ” z = x βŠ“ (y βŠ” z) := sorry protected instance order_dual.is_modular_lattice {Ξ± : Type u_1} [lattice Ξ±] [is_modular_lattice Ξ±] : is_modular_lattice (order_dual Ξ±) := is_modular_lattice.mk fun (x y z : order_dual Ξ±) (xz : x ≀ z) => le_of_eq (eq.mpr (id (Eq._oldrec (Eq.refl ((x βŠ” y) βŠ“ z = x βŠ” y βŠ“ z)) inf_comm)) (eq.mpr (id (Eq._oldrec (Eq.refl (z βŠ“ (x βŠ” y) = x βŠ” y βŠ“ z)) sup_comm)) (eq.mpr (id (Eq._oldrec (Eq.refl (z βŠ“ (y βŠ” x) = x βŠ” y βŠ“ z)) (propext eq_comm))) (eq.mpr (id (Eq._oldrec (Eq.refl (x βŠ” y βŠ“ z = z βŠ“ (y βŠ” x))) inf_comm)) (eq.mpr (id (Eq._oldrec (Eq.refl (x βŠ” z βŠ“ y = z βŠ“ (y βŠ” x))) sup_comm)) (eq.mpr ((fun (a a_1 : order_dual Ξ±) (e_1 : a = a_1) (αΎ° αΎ°_1 : order_dual Ξ±) (e_2 : αΎ° = αΎ°_1) => congr (congr_arg Eq e_1) e_2) (z βŠ“ y βŠ” x) ((z βŠ” coe_fn order_dual.of_dual y) βŠ“ x) (Eq.refl (z βŠ“ y βŠ” x)) (z βŠ“ (y βŠ” x)) (z βŠ” coe_fn order_dual.of_dual y βŠ“ x) (Eq.refl (z βŠ“ (y βŠ” x)))) (sup_inf_assoc_of_le (coe_fn order_dual.of_dual y) (iff.mpr order_dual.dual_le xz)))))))) /-- The diamond isomorphism between the intervals `[a βŠ“ b, a]` and `[b, a βŠ” b]` -/ def inf_Icc_order_iso_Icc_sup {Ξ± : Type u_1} [lattice Ξ±] [is_modular_lattice Ξ±] (a : Ξ±) (b : Ξ±) : β†₯(set.Icc (a βŠ“ b) a) ≃o β†₯(set.Icc b (a βŠ” b)) := rel_iso.mk (equiv.mk (fun (x : β†₯(set.Icc (a βŠ“ b) a)) => { val := ↑x βŠ” b, property := sorry }) (fun (x : β†₯(set.Icc b (a βŠ” b))) => { val := a βŠ“ ↑x, property := sorry }) sorry sorry) sorry namespace is_compl /-- The diamond isomorphism between the intervals `set.Iic a` and `set.Ici b`. -/ def Iic_order_iso_Ici {Ξ± : Type u_1} [bounded_lattice Ξ±] [is_modular_lattice Ξ±] {a : Ξ±} {b : Ξ±} (h : is_compl a b) : β†₯(set.Iic a) ≃o β†₯(set.Ici b) := order_iso.trans (order_iso.set_congr (set.Iic a) (set.Icc (a βŠ“ b) a) sorry) (order_iso.trans (inf_Icc_order_iso_Icc_sup a b) (order_iso.set_congr (set.Icc b (a βŠ” b)) (set.Ici b) sorry)) end is_compl theorem is_modular_lattice_iff_sup_inf_sup_assoc {Ξ± : Type u_1} [lattice Ξ±] : is_modular_lattice Ξ± ↔ βˆ€ (x y z : Ξ±), x βŠ“ z βŠ” y βŠ“ z = (x βŠ“ z βŠ” y) βŠ“ z := sorry namespace distrib_lattice protected instance is_modular_lattice {Ξ± : Type u_1} [distrib_lattice Ξ±] : is_modular_lattice Ξ± := is_modular_lattice.mk fun (x y z : Ξ±) (xz : x ≀ z) => eq.mpr (id (Eq._oldrec (Eq.refl ((x βŠ” y) βŠ“ z ≀ x βŠ” y βŠ“ z)) inf_sup_right)) (eq.mpr (id (Eq._oldrec (Eq.refl (x βŠ“ z βŠ” y βŠ“ z ≀ x βŠ” y βŠ“ z)) (iff.mpr inf_eq_left xz))) (le_refl (x βŠ” y βŠ“ z))) end distrib_lattice namespace is_modular_lattice protected instance is_modular_lattice_Iic {Ξ± : Type u_1} [bounded_lattice Ξ±] [is_modular_lattice Ξ±] {a : Ξ±} : is_modular_lattice β†₯(set.Iic a) := mk fun (x y z : β†₯(set.Iic a)) (xz : x ≀ z) => sup_inf_le_assoc_of_le (↑y) xz protected instance is_modular_lattice_Ici {Ξ± : Type u_1} [bounded_lattice Ξ±] [is_modular_lattice Ξ±] {a : Ξ±} : is_modular_lattice β†₯(set.Ici a) := mk fun (x y z : β†₯(set.Ici a)) (xz : x ≀ z) => sup_inf_le_assoc_of_le (↑y) xz
fc78719f17a031e77e72e3cd49c1e630c54de001
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/topology/metric_space/hausdorff_distance.lean
2f57d226d2b651ab65975b42231abfbda36d2d0e
[]
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
24,503
lean
/- Copyright (c) 2019 SΓ©bastien GouΓ«zel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: SΓ©bastien GouΓ«zel -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.topology.metric_space.isometry import Mathlib.topology.instances.ennreal import Mathlib.PostPort universes u v namespace Mathlib /-! # Hausdorff distance The Hausdorff distance on subsets of a metric (or emetric) space. Given two subsets `s` and `t` of a metric space, their Hausdorff distance is the smallest `d` such that any point `s` is within `d` of a point in `t`, and conversely. This quantity is often infinite (think of `s` bounded and `t` unbounded), and therefore better expressed in the setting of emetric spaces. ## Main definitions This files introduces: * `inf_edist x s`, the infimum edistance of a point `x` to a set `s` in an emetric space * `Hausdorff_edist s t`, the Hausdorff edistance of two sets in an emetric space * Versions of these notions on metric spaces, called respectively `inf_dist` and `Hausdorff_dist`. -/ namespace emetric /-! ### Distance of a point to a set as a function into `ennreal`. -/ /-- The minimal edistance of a point to a set -/ def inf_edist {Ξ± : Type u} [emetric_space Ξ±] (x : Ξ±) (s : set Ξ±) : ennreal := Inf (edist x '' s) @[simp] theorem inf_edist_empty {Ξ± : Type u} [emetric_space Ξ±] {x : Ξ±} : inf_edist x βˆ… = ⊀ := sorry /-- The edist to a union is the minimum of the edists -/ @[simp] theorem inf_edist_union {Ξ± : Type u} [emetric_space Ξ±] {x : Ξ±} {s : set Ξ±} {t : set Ξ±} : inf_edist x (s βˆͺ t) = inf_edist x s βŠ“ inf_edist x t := sorry /-- The edist to a singleton is the edistance to the single point of this singleton -/ @[simp] theorem inf_edist_singleton {Ξ± : Type u} [emetric_space Ξ±] {x : Ξ±} {y : Ξ±} : inf_edist x (singleton y) = edist x y := sorry /-- The edist to a set is bounded above by the edist to any of its points -/ theorem inf_edist_le_edist_of_mem {Ξ± : Type u} [emetric_space Ξ±] {x : Ξ±} {y : Ξ±} {s : set Ξ±} (h : y ∈ s) : inf_edist x s ≀ edist x y := Inf_le (iff.mpr (set.mem_image (edist x) s (edist x y)) (Exists.intro y { left := h, right := Eq.refl (edist x y) })) /-- If a point `x` belongs to `s`, then its edist to `s` vanishes -/ theorem inf_edist_zero_of_mem {Ξ± : Type u} [emetric_space Ξ±] {x : Ξ±} {s : set Ξ±} (h : x ∈ s) : inf_edist x s = 0 := iff.mp nonpos_iff_eq_zero (edist_self x β–Έ inf_edist_le_edist_of_mem h) /-- The edist is monotonous with respect to inclusion -/ theorem inf_edist_le_inf_edist_of_subset {Ξ± : Type u} [emetric_space Ξ±] {x : Ξ±} {s : set Ξ±} {t : set Ξ±} (h : s βŠ† t) : inf_edist x t ≀ inf_edist x s := Inf_le_Inf (set.image_subset (edist x) h) /-- If the edist to a set is `< r`, there exists a point in the set at edistance `< r` -/ theorem exists_edist_lt_of_inf_edist_lt {Ξ± : Type u} [emetric_space Ξ±] {x : Ξ±} {s : set Ξ±} {r : ennreal} (h : inf_edist x s < r) : βˆƒ (y : Ξ±), βˆƒ (H : y ∈ s), edist x y < r := sorry /-- The edist of `x` to `s` is bounded by the sum of the edist of `y` to `s` and the edist from `x` to `y` -/ theorem inf_edist_le_inf_edist_add_edist {Ξ± : Type u} [emetric_space Ξ±] {x : Ξ±} {y : Ξ±} {s : set Ξ±} : inf_edist x s ≀ inf_edist y s + edist x y := sorry /-- The edist to a set depends continuously on the point -/ theorem continuous_inf_edist {Ξ± : Type u} [emetric_space Ξ±] {s : set Ξ±} : continuous fun (x : Ξ±) => inf_edist x s := sorry /-- The edist to a set and to its closure coincide -/ theorem inf_edist_closure {Ξ± : Type u} [emetric_space Ξ±] {x : Ξ±} {s : set Ξ±} : inf_edist x (closure s) = inf_edist x s := sorry /-- A point belongs to the closure of `s` iff its infimum edistance to this set vanishes -/ theorem mem_closure_iff_inf_edist_zero {Ξ± : Type u} [emetric_space Ξ±] {x : Ξ±} {s : set Ξ±} : x ∈ closure s ↔ inf_edist x s = 0 := sorry /-- Given a closed set `s`, a point belongs to `s` iff its infimum edistance to this set vanishes -/ theorem mem_iff_ind_edist_zero_of_closed {Ξ± : Type u} [emetric_space Ξ±] {x : Ξ±} {s : set Ξ±} (h : is_closed s) : x ∈ s ↔ inf_edist x s = 0 := sorry /-- The infimum edistance is invariant under isometries -/ theorem inf_edist_image {Ξ± : Type u} {Ξ² : Type v} [emetric_space Ξ±] [emetric_space Ξ²] {x : Ξ±} {t : set Ξ±} {Ξ¦ : Ξ± β†’ Ξ²} (hΞ¦ : isometry Ξ¦) : inf_edist (Ξ¦ x) (Ξ¦ '' t) = inf_edist x t := sorry /-! ### The Hausdorff distance as a function into `ennreal`. -/ /-- The Hausdorff edistance between two sets is the smallest `r` such that each set is contained in the `r`-neighborhood of the other one -/ def Hausdorff_edist {Ξ± : Type u} [emetric_space Ξ±] (s : set Ξ±) (t : set Ξ±) : ennreal := Sup ((fun (x : Ξ±) => inf_edist x t) '' s) βŠ” Sup ((fun (x : Ξ±) => inf_edist x s) '' t) theorem Hausdorff_edist_def {Ξ± : Type u} [emetric_space Ξ±] (s : set Ξ±) (t : set Ξ±) : Hausdorff_edist s t = Sup ((fun (x : Ξ±) => inf_edist x t) '' s) βŠ” Sup ((fun (x : Ξ±) => inf_edist x s) '' t) := rfl /-- The Hausdorff edistance of a set to itself vanishes -/ @[simp] theorem Hausdorff_edist_self {Ξ± : Type u} [emetric_space Ξ±] {s : set Ξ±} : Hausdorff_edist s s = 0 := sorry /-- The Haudorff edistances of `s` to `t` and of `t` to `s` coincide -/ theorem Hausdorff_edist_comm {Ξ± : Type u} [emetric_space Ξ±] {s : set Ξ±} {t : set Ξ±} : Hausdorff_edist s t = Hausdorff_edist t s := sorry /-- Bounding the Hausdorff edistance by bounding the edistance of any point in each set to the other set -/ theorem Hausdorff_edist_le_of_inf_edist {Ξ± : Type u} [emetric_space Ξ±] {s : set Ξ±} {t : set Ξ±} {r : ennreal} (H1 : βˆ€ (x : Ξ±), x ∈ s β†’ inf_edist x t ≀ r) (H2 : βˆ€ (x : Ξ±), x ∈ t β†’ inf_edist x s ≀ r) : Hausdorff_edist s t ≀ r := sorry /-- Bounding the Hausdorff edistance by exhibiting, for any point in each set, another point in the other set at controlled distance -/ theorem Hausdorff_edist_le_of_mem_edist {Ξ± : Type u} [emetric_space Ξ±] {s : set Ξ±} {t : set Ξ±} {r : ennreal} (H1 : βˆ€ (x : Ξ±) (H : x ∈ s), βˆƒ (y : Ξ±), βˆƒ (H : y ∈ t), edist x y ≀ r) (H2 : βˆ€ (x : Ξ±) (H : x ∈ t), βˆƒ (y : Ξ±), βˆƒ (H : y ∈ s), edist x y ≀ r) : Hausdorff_edist s t ≀ r := sorry /-- The distance to a set is controlled by the Hausdorff distance -/ theorem inf_edist_le_Hausdorff_edist_of_mem {Ξ± : Type u} [emetric_space Ξ±] {x : Ξ±} {s : set Ξ±} {t : set Ξ±} (h : x ∈ s) : inf_edist x t ≀ Hausdorff_edist s t := eq.mpr (id (Eq._oldrec (Eq.refl (inf_edist x t ≀ Hausdorff_edist s t)) (Hausdorff_edist_def s t))) (le_trans (le_Sup (set.mem_image_of_mem (fun (x : Ξ±) => inf_edist x t) h)) le_sup_left) /-- If the Hausdorff distance is `<r`, then any point in one of the sets has a corresponding point at distance `<r` in the other set -/ theorem exists_edist_lt_of_Hausdorff_edist_lt {Ξ± : Type u} [emetric_space Ξ±] {x : Ξ±} {s : set Ξ±} {t : set Ξ±} {r : ennreal} (h : x ∈ s) (H : Hausdorff_edist s t < r) : βˆƒ (y : Ξ±), βˆƒ (H : y ∈ t), edist x y < r := exists_edist_lt_of_inf_edist_lt (lt_of_le_of_lt (le_trans (le_Sup (set.mem_image_of_mem (fun (x : Ξ±) => inf_edist x t) h)) le_sup_left) (eq.mp (Eq._oldrec (Eq.refl (Hausdorff_edist s t < r)) (Hausdorff_edist_def s t)) H)) /-- The distance from `x` to `s` or `t` is controlled in terms of the Hausdorff distance between `s` and `t` -/ theorem inf_edist_le_inf_edist_add_Hausdorff_edist {Ξ± : Type u} [emetric_space Ξ±] {x : Ξ±} {s : set Ξ±} {t : set Ξ±} : inf_edist x t ≀ inf_edist x s + Hausdorff_edist s t := sorry /-- The Hausdorff edistance is invariant under eisometries -/ theorem Hausdorff_edist_image {Ξ± : Type u} {Ξ² : Type v} [emetric_space Ξ±] [emetric_space Ξ²] {s : set Ξ±} {t : set Ξ±} {Ξ¦ : Ξ± β†’ Ξ²} (h : isometry Ξ¦) : Hausdorff_edist (Ξ¦ '' s) (Ξ¦ '' t) = Hausdorff_edist s t := sorry /-- The Hausdorff distance is controlled by the diameter of the union -/ theorem Hausdorff_edist_le_ediam {Ξ± : Type u} [emetric_space Ξ±] {s : set Ξ±} {t : set Ξ±} (hs : set.nonempty s) (ht : set.nonempty t) : Hausdorff_edist s t ≀ diam (s βˆͺ t) := sorry /-- The Hausdorff distance satisfies the triangular inequality -/ theorem Hausdorff_edist_triangle {Ξ± : Type u} [emetric_space Ξ±] {s : set Ξ±} {t : set Ξ±} {u : set Ξ±} : Hausdorff_edist s u ≀ Hausdorff_edist s t + Hausdorff_edist t u := sorry /-- The Hausdorff edistance between a set and its closure vanishes -/ @[simp] theorem Hausdorff_edist_self_closure {Ξ± : Type u} [emetric_space Ξ±] {s : set Ξ±} : Hausdorff_edist s (closure s) = 0 := sorry /-- Replacing a set by its closure does not change the Hausdorff edistance. -/ @[simp] theorem Hausdorff_edist_closure₁ {Ξ± : Type u} [emetric_space Ξ±] {s : set Ξ±} {t : set Ξ±} : Hausdorff_edist (closure s) t = Hausdorff_edist s t := sorry /-- Replacing a set by its closure does not change the Hausdorff edistance. -/ @[simp] theorem Hausdorff_edist_closureβ‚‚ {Ξ± : Type u} [emetric_space Ξ±] {s : set Ξ±} {t : set Ξ±} : Hausdorff_edist s (closure t) = Hausdorff_edist s t := sorry /-- The Hausdorff edistance between sets or their closures is the same -/ @[simp] theorem Hausdorff_edist_closure {Ξ± : Type u} [emetric_space Ξ±] {s : set Ξ±} {t : set Ξ±} : Hausdorff_edist (closure s) (closure t) = Hausdorff_edist s t := sorry /-- Two sets are at zero Hausdorff edistance if and only if they have the same closure -/ theorem Hausdorff_edist_zero_iff_closure_eq_closure {Ξ± : Type u} [emetric_space Ξ±] {s : set Ξ±} {t : set Ξ±} : Hausdorff_edist s t = 0 ↔ closure s = closure t := sorry /-- Two closed sets are at zero Hausdorff edistance if and only if they coincide -/ theorem Hausdorff_edist_zero_iff_eq_of_closed {Ξ± : Type u} [emetric_space Ξ±] {s : set Ξ±} {t : set Ξ±} (hs : is_closed s) (ht : is_closed t) : Hausdorff_edist s t = 0 ↔ s = t := sorry /-- The Haudorff edistance to the empty set is infinite -/ theorem Hausdorff_edist_empty {Ξ± : Type u} [emetric_space Ξ±] {s : set Ξ±} (ne : set.nonempty s) : Hausdorff_edist s βˆ… = ⊀ := sorry /-- If a set is at finite Hausdorff edistance of a nonempty set, it is nonempty -/ theorem nonempty_of_Hausdorff_edist_ne_top {Ξ± : Type u} [emetric_space Ξ±] {s : set Ξ±} {t : set Ξ±} (hs : set.nonempty s) (fin : Hausdorff_edist s t β‰  ⊀) : set.nonempty t := or.elim (set.eq_empty_or_nonempty t) (fun (ht : t = βˆ…) => false.elim (fin (Eq.symm ht β–Έ Hausdorff_edist_empty hs))) id theorem empty_or_nonempty_of_Hausdorff_edist_ne_top {Ξ± : Type u} [emetric_space Ξ±] {s : set Ξ±} {t : set Ξ±} (fin : Hausdorff_edist s t β‰  ⊀) : s = βˆ… ∧ t = βˆ… ∨ set.nonempty s ∧ set.nonempty t := sorry /-! Now, we turn to the same notions in metric spaces. To avoid the difficulties related to `Inf` and `Sup` on `ℝ` (which is only conditionally complete), we use the notions in `ennreal` formulated in terms of the edistance, and coerce them to `ℝ`. Then their properties follow readily from the corresponding properties in `ennreal`, modulo some tedious rewriting of inequalities from one to the other. -/ namespace metric /-! ### Distance of a point to a set as a function into `ℝ`. -/ /-- The minimal distance of a point to a set -/ def Mathlib.metric.inf_dist {Ξ± : Type u} [metric_space Ξ±] (x : Ξ±) (s : set Ξ±) : ℝ := ennreal.to_real (inf_edist x s) /-- the minimal distance is always nonnegative -/ theorem Mathlib.metric.inf_dist_nonneg {Ξ± : Type u} [metric_space Ξ±] {s : set Ξ±} {x : Ξ±} : 0 ≀ metric.inf_dist x s := sorry /-- the minimal distance to the empty set is 0 (if you want to have the more reasonable value ∞ instead, use `inf_edist`, which takes values in ennreal) -/ @[simp] theorem Mathlib.metric.inf_dist_empty {Ξ± : Type u} [metric_space Ξ±] {x : Ξ±} : metric.inf_dist x βˆ… = 0 := sorry /-- In a metric space, the minimal edistance to a nonempty set is finite -/ theorem Mathlib.metric.inf_edist_ne_top {Ξ± : Type u} [metric_space Ξ±] {s : set Ξ±} {x : Ξ±} (h : set.nonempty s) : inf_edist x s β‰  ⊀ := sorry /-- The minimal distance of a point to a set containing it vanishes -/ theorem Mathlib.metric.inf_dist_zero_of_mem {Ξ± : Type u} [metric_space Ξ±] {s : set Ξ±} {x : Ξ±} (h : x ∈ s) : metric.inf_dist x s = 0 := sorry /-- The minimal distance to a singleton is the distance to the unique point in this singleton -/ @[simp] theorem Mathlib.metric.inf_dist_singleton {Ξ± : Type u} [metric_space Ξ±] {x : Ξ±} {y : Ξ±} : metric.inf_dist x (singleton y) = dist x y := sorry /-- The minimal distance to a set is bounded by the distance to any point in this set -/ theorem Mathlib.metric.inf_dist_le_dist_of_mem {Ξ± : Type u} [metric_space Ξ±] {s : set Ξ±} {x : Ξ±} {y : Ξ±} (h : y ∈ s) : metric.inf_dist x s ≀ dist x y := sorry /-- The minimal distance is monotonous with respect to inclusion -/ theorem Mathlib.metric.inf_dist_le_inf_dist_of_subset {Ξ± : Type u} [metric_space Ξ±] {s : set Ξ±} {t : set Ξ±} {x : Ξ±} (h : s βŠ† t) (hs : set.nonempty s) : metric.inf_dist x t ≀ metric.inf_dist x s := sorry /-- If the minimal distance to a set is `<r`, there exists a point in this set at distance `<r` -/ theorem Mathlib.metric.exists_dist_lt_of_inf_dist_lt {Ξ± : Type u} [metric_space Ξ±] {s : set Ξ±} {x : Ξ±} {r : ℝ} (h : metric.inf_dist x s < r) (hs : set.nonempty s) : βˆƒ (y : Ξ±), βˆƒ (H : y ∈ s), dist x y < r := sorry /-- The minimal distance from `x` to `s` is bounded by the distance from `y` to `s`, modulo the distance between `x` and `y` -/ theorem Mathlib.metric.inf_dist_le_inf_dist_add_dist {Ξ± : Type u} [metric_space Ξ±] {s : set Ξ±} {x : Ξ±} {y : Ξ±} : metric.inf_dist x s ≀ metric.inf_dist y s + dist x y := sorry /-- The minimal distance to a set is Lipschitz in point with constant 1 -/ theorem Mathlib.metric.lipschitz_inf_dist_pt {Ξ± : Type u} [metric_space Ξ±] (s : set Ξ±) : lipschitz_with 1 fun (x : Ξ±) => metric.inf_dist x s := lipschitz_with.of_le_add fun (x y : Ξ±) => metric.inf_dist_le_inf_dist_add_dist /-- The minimal distance to a set is uniformly continuous in point -/ theorem Mathlib.metric.uniform_continuous_inf_dist_pt {Ξ± : Type u} [metric_space Ξ±] (s : set Ξ±) : uniform_continuous fun (x : Ξ±) => metric.inf_dist x s := lipschitz_with.uniform_continuous (metric.lipschitz_inf_dist_pt s) /-- The minimal distance to a set is continuous in point -/ theorem Mathlib.metric.continuous_inf_dist_pt {Ξ± : Type u} [metric_space Ξ±] (s : set Ξ±) : continuous fun (x : Ξ±) => metric.inf_dist x s := uniform_continuous.continuous (metric.uniform_continuous_inf_dist_pt s) /-- The minimal distance to a set and its closure coincide -/ theorem Mathlib.metric.inf_dist_eq_closure {Ξ± : Type u} [metric_space Ξ±] {s : set Ξ±} {x : Ξ±} : metric.inf_dist x (closure s) = metric.inf_dist x s := sorry /-- A point belongs to the closure of `s` iff its infimum distance to this set vanishes -/ theorem Mathlib.metric.mem_closure_iff_inf_dist_zero {Ξ± : Type u} [metric_space Ξ±] {s : set Ξ±} {x : Ξ±} (h : set.nonempty s) : x ∈ closure s ↔ metric.inf_dist x s = 0 := sorry /-- Given a closed set `s`, a point belongs to `s` iff its infimum distance to this set vanishes -/ theorem Mathlib.metric.mem_iff_inf_dist_zero_of_closed {Ξ± : Type u} [metric_space Ξ±] {s : set Ξ±} {x : Ξ±} (h : is_closed s) (hs : set.nonempty s) : x ∈ s ↔ metric.inf_dist x s = 0 := eq.mp (Eq._oldrec (Eq.refl (x ∈ closure s ↔ metric.inf_dist x s = 0)) (is_closed.closure_eq h)) (metric.mem_closure_iff_inf_dist_zero hs) /-- The infimum distance is invariant under isometries -/ theorem Mathlib.metric.inf_dist_image {Ξ± : Type u} {Ξ² : Type v} [metric_space Ξ±] [metric_space Ξ²] {t : set Ξ±} {x : Ξ±} {Ξ¦ : Ξ± β†’ Ξ²} (hΞ¦ : isometry Ξ¦) : metric.inf_dist (Ξ¦ x) (Ξ¦ '' t) = metric.inf_dist x t := sorry /-! ### Distance of a point to a set as a function into `ℝβ‰₯0`. -/ /-- The minimal distance of a point to a set as a `ℝβ‰₯0` -/ def Mathlib.metric.inf_nndist {Ξ± : Type u} [metric_space Ξ±] (x : Ξ±) (s : set Ξ±) : nnreal := ennreal.to_nnreal (inf_edist x s) @[simp] theorem Mathlib.metric.coe_inf_nndist {Ξ± : Type u} [metric_space Ξ±] {s : set Ξ±} {x : Ξ±} : ↑(metric.inf_nndist x s) = metric.inf_dist x s := rfl /-- The minimal distance to a set (as `ℝβ‰₯0`) is Lipschitz in point with constant 1 -/ theorem Mathlib.metric.lipschitz_inf_nndist_pt {Ξ± : Type u} [metric_space Ξ±] (s : set Ξ±) : lipschitz_with 1 fun (x : Ξ±) => metric.inf_nndist x s := lipschitz_with.of_le_add fun (x y : Ξ±) => metric.inf_dist_le_inf_dist_add_dist /-- The minimal distance to a set (as `ℝβ‰₯0`) is uniformly continuous in point -/ theorem Mathlib.metric.uniform_continuous_inf_nndist_pt {Ξ± : Type u} [metric_space Ξ±] (s : set Ξ±) : uniform_continuous fun (x : Ξ±) => metric.inf_nndist x s := lipschitz_with.uniform_continuous (metric.lipschitz_inf_nndist_pt s) /-- The minimal distance to a set (as `ℝβ‰₯0`) is continuous in point -/ theorem Mathlib.metric.continuous_inf_nndist_pt {Ξ± : Type u} [metric_space Ξ±] (s : set Ξ±) : continuous fun (x : Ξ±) => metric.inf_nndist x s := uniform_continuous.continuous (metric.uniform_continuous_inf_nndist_pt s) /-! ### The Hausdorff distance as a function into `ℝ`. -/ /-- The Hausdorff distance between two sets is the smallest nonnegative `r` such that each set is included in the `r`-neighborhood of the other. If there is no such `r`, it is defined to be `0`, arbitrarily -/ def Mathlib.metric.Hausdorff_dist {Ξ± : Type u} [metric_space Ξ±] (s : set Ξ±) (t : set Ξ±) : ℝ := ennreal.to_real (Hausdorff_edist s t) /-- The Hausdorff distance is nonnegative -/ theorem Mathlib.metric.Hausdorff_dist_nonneg {Ξ± : Type u} [metric_space Ξ±] {s : set Ξ±} {t : set Ξ±} : 0 ≀ metric.Hausdorff_dist s t := sorry /-- If two sets are nonempty and bounded in a metric space, they are at finite Hausdorff edistance. -/ theorem Mathlib.metric.Hausdorff_edist_ne_top_of_nonempty_of_bounded {Ξ± : Type u} [metric_space Ξ±] {s : set Ξ±} {t : set Ξ±} (hs : set.nonempty s) (ht : set.nonempty t) (bs : metric.bounded s) (bt : metric.bounded t) : Hausdorff_edist s t β‰  ⊀ := sorry /-- The Hausdorff distance between a set and itself is zero -/ @[simp] theorem Mathlib.metric.Hausdorff_dist_self_zero {Ξ± : Type u} [metric_space Ξ±] {s : set Ξ±} : metric.Hausdorff_dist s s = 0 := sorry /-- The Hausdorff distance from `s` to `t` and from `t` to `s` coincide -/ theorem Mathlib.metric.Hausdorff_dist_comm {Ξ± : Type u} [metric_space Ξ±] {s : set Ξ±} {t : set Ξ±} : metric.Hausdorff_dist s t = metric.Hausdorff_dist t s := sorry /-- The Hausdorff distance to the empty set vanishes (if you want to have the more reasonable value ∞ instead, use `Hausdorff_edist`, which takes values in ennreal) -/ @[simp] theorem Mathlib.metric.Hausdorff_dist_empty {Ξ± : Type u} [metric_space Ξ±] {s : set Ξ±} : metric.Hausdorff_dist s βˆ… = 0 := sorry /-- The Hausdorff distance to the empty set vanishes (if you want to have the more reasonable value ∞ instead, use `Hausdorff_edist`, which takes values in ennreal) -/ @[simp] theorem Mathlib.metric.Hausdorff_dist_empty' {Ξ± : Type u} [metric_space Ξ±] {s : set Ξ±} : metric.Hausdorff_dist βˆ… s = 0 := sorry /-- Bounding the Hausdorff distance by bounding the distance of any point in each set to the other set -/ theorem Mathlib.metric.Hausdorff_dist_le_of_inf_dist {Ξ± : Type u} [metric_space Ξ±] {s : set Ξ±} {t : set Ξ±} {r : ℝ} (hr : 0 ≀ r) (H1 : βˆ€ (x : Ξ±), x ∈ s β†’ metric.inf_dist x t ≀ r) (H2 : βˆ€ (x : Ξ±), x ∈ t β†’ metric.inf_dist x s ≀ r) : metric.Hausdorff_dist s t ≀ r := sorry /-- Bounding the Hausdorff distance by exhibiting, for any point in each set, another point in the other set at controlled distance -/ theorem Mathlib.metric.Hausdorff_dist_le_of_mem_dist {Ξ± : Type u} [metric_space Ξ±] {s : set Ξ±} {t : set Ξ±} {r : ℝ} (hr : 0 ≀ r) (H1 : βˆ€ (x : Ξ±) (H : x ∈ s), βˆƒ (y : Ξ±), βˆƒ (H : y ∈ t), dist x y ≀ r) (H2 : βˆ€ (x : Ξ±) (H : x ∈ t), βˆƒ (y : Ξ±), βˆƒ (H : y ∈ s), dist x y ≀ r) : metric.Hausdorff_dist s t ≀ r := sorry /-- The Hausdorff distance is controlled by the diameter of the union -/ theorem Mathlib.metric.Hausdorff_dist_le_diam {Ξ± : Type u} [metric_space Ξ±] {s : set Ξ±} {t : set Ξ±} (hs : set.nonempty s) (bs : metric.bounded s) (ht : set.nonempty t) (bt : metric.bounded t) : metric.Hausdorff_dist s t ≀ metric.diam (s βˆͺ t) := sorry /-- The distance to a set is controlled by the Hausdorff distance -/ theorem Mathlib.metric.inf_dist_le_Hausdorff_dist_of_mem {Ξ± : Type u} [metric_space Ξ±] {s : set Ξ±} {t : set Ξ±} {x : Ξ±} (hx : x ∈ s) (fin : Hausdorff_edist s t β‰  ⊀) : metric.inf_dist x t ≀ metric.Hausdorff_dist s t := sorry /-- If the Hausdorff distance is `<r`, then any point in one of the sets is at distance `<r` of a point in the other set -/ theorem Mathlib.metric.exists_dist_lt_of_Hausdorff_dist_lt {Ξ± : Type u} [metric_space Ξ±] {s : set Ξ±} {t : set Ξ±} {x : Ξ±} {r : ℝ} (h : x ∈ s) (H : metric.Hausdorff_dist s t < r) (fin : Hausdorff_edist s t β‰  ⊀) : βˆƒ (y : Ξ±), βˆƒ (H : y ∈ t), dist x y < r := sorry /-- If the Hausdorff distance is `<r`, then any point in one of the sets is at distance `<r` of a point in the other set -/ theorem Mathlib.metric.exists_dist_lt_of_Hausdorff_dist_lt' {Ξ± : Type u} [metric_space Ξ±] {s : set Ξ±} {t : set Ξ±} {y : Ξ±} {r : ℝ} (h : y ∈ t) (H : metric.Hausdorff_dist s t < r) (fin : Hausdorff_edist s t β‰  ⊀) : βˆƒ (x : Ξ±), βˆƒ (H : x ∈ s), dist x y < r := sorry /-- The infimum distance to `s` and `t` are the same, up to the Hausdorff distance between `s` and `t` -/ theorem Mathlib.metric.inf_dist_le_inf_dist_add_Hausdorff_dist {Ξ± : Type u} [metric_space Ξ±] {s : set Ξ±} {t : set Ξ±} {x : Ξ±} (fin : Hausdorff_edist s t β‰  ⊀) : metric.inf_dist x t ≀ metric.inf_dist x s + metric.Hausdorff_dist s t := sorry /-- The Hausdorff distance is invariant under isometries -/ theorem Mathlib.metric.Hausdorff_dist_image {Ξ± : Type u} {Ξ² : Type v} [metric_space Ξ±] [metric_space Ξ²] {s : set Ξ±} {t : set Ξ±} {Ξ¦ : Ξ± β†’ Ξ²} (h : isometry Ξ¦) : metric.Hausdorff_dist (Ξ¦ '' s) (Ξ¦ '' t) = metric.Hausdorff_dist s t := sorry /-- The Hausdorff distance satisfies the triangular inequality -/ theorem Mathlib.metric.Hausdorff_dist_triangle {Ξ± : Type u} [metric_space Ξ±] {s : set Ξ±} {t : set Ξ±} {u : set Ξ±} (fin : Hausdorff_edist s t β‰  ⊀) : metric.Hausdorff_dist s u ≀ metric.Hausdorff_dist s t + metric.Hausdorff_dist t u := sorry /-- The Hausdorff distance satisfies the triangular inequality -/ theorem Mathlib.metric.Hausdorff_dist_triangle' {Ξ± : Type u} [metric_space Ξ±] {s : set Ξ±} {t : set Ξ±} {u : set Ξ±} (fin : Hausdorff_edist t u β‰  ⊀) : metric.Hausdorff_dist s u ≀ metric.Hausdorff_dist s t + metric.Hausdorff_dist t u := sorry /-- The Hausdorff distance between a set and its closure vanish -/ @[simp] theorem Mathlib.metric.Hausdorff_dist_self_closure {Ξ± : Type u} [metric_space Ξ±] {s : set Ξ±} : metric.Hausdorff_dist s (closure s) = 0 := sorry /-- Replacing a set by its closure does not change the Hausdorff distance. -/ @[simp] theorem Mathlib.metric.Hausdorff_dist_closure₁ {Ξ± : Type u} [metric_space Ξ±] {s : set Ξ±} {t : set Ξ±} : metric.Hausdorff_dist (closure s) t = metric.Hausdorff_dist s t := sorry /-- Replacing a set by its closure does not change the Hausdorff distance. -/ @[simp] theorem Mathlib.metric.Hausdorff_dist_closureβ‚‚ {Ξ± : Type u} [metric_space Ξ±] {s : set Ξ±} {t : set Ξ±} : metric.Hausdorff_dist s (closure t) = metric.Hausdorff_dist s t := sorry /-- The Hausdorff distance between two sets and their closures coincide -/ @[simp] theorem Mathlib.metric.Hausdorff_dist_closure {Ξ± : Type u} [metric_space Ξ±] {s : set Ξ±} {t : set Ξ±} : metric.Hausdorff_dist (closure s) (closure t) = metric.Hausdorff_dist s t := sorry /-- Two sets are at zero Hausdorff distance if and only if they have the same closures -/ theorem Mathlib.metric.Hausdorff_dist_zero_iff_closure_eq_closure {Ξ± : Type u} [metric_space Ξ±] {s : set Ξ±} {t : set Ξ±} (fin : Hausdorff_edist s t β‰  ⊀) : metric.Hausdorff_dist s t = 0 ↔ closure s = closure t := sorry /-- Two closed sets are at zero Hausdorff distance if and only if they coincide -/ theorem Mathlib.metric.Hausdorff_dist_zero_iff_eq_of_closed {Ξ± : Type u} [metric_space Ξ±] {s : set Ξ±} {t : set Ξ±} (hs : is_closed s) (ht : is_closed t) (fin : Hausdorff_edist s t β‰  ⊀) : metric.Hausdorff_dist s t = 0 ↔ s = t := sorry
22096b84463def3e7d2e14500a6d34c6b0cf89ff
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/multiset/erase_dup_auto.lean
47c9359e73147c9736d4a9f68b2c561f9089a1e4
[]
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,393
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.multiset.nodup import Mathlib.PostPort universes u_1 u_2 namespace Mathlib /-! # Erasing duplicates in a multiset. -/ namespace multiset /-! ### erase_dup -/ /-- `erase_dup s` removes duplicates from `s`, yielding a `nodup` multiset. -/ def erase_dup {Ξ± : Type u_1} [DecidableEq Ξ±] (s : multiset Ξ±) : multiset Ξ± := quot.lift_on s (fun (l : List Ξ±) => ↑(list.erase_dup l)) sorry @[simp] theorem coe_erase_dup {Ξ± : Type u_1} [DecidableEq Ξ±] (l : List Ξ±) : erase_dup ↑l = ↑(list.erase_dup l) := rfl @[simp] theorem erase_dup_zero {Ξ± : Type u_1} [DecidableEq Ξ±] : erase_dup 0 = 0 := rfl @[simp] theorem mem_erase_dup {Ξ± : Type u_1} [DecidableEq Ξ±] {a : Ξ±} {s : multiset Ξ±} : a ∈ erase_dup s ↔ a ∈ s := quot.induction_on s fun (l : List Ξ±) => list.mem_erase_dup @[simp] theorem erase_dup_cons_of_mem {Ξ± : Type u_1} [DecidableEq Ξ±] {a : Ξ±} {s : multiset Ξ±} : a ∈ s β†’ erase_dup (a ::β‚˜ s) = erase_dup s := quot.induction_on s fun (l : List Ξ±) (m : a ∈ Quot.mk setoid.r l) => congr_arg coe (list.erase_dup_cons_of_mem m) @[simp] theorem erase_dup_cons_of_not_mem {Ξ± : Type u_1} [DecidableEq Ξ±] {a : Ξ±} {s : multiset Ξ±} : Β¬a ∈ s β†’ erase_dup (a ::β‚˜ s) = a ::β‚˜ erase_dup s := quot.induction_on s fun (l : List Ξ±) (m : Β¬a ∈ Quot.mk setoid.r l) => congr_arg coe (list.erase_dup_cons_of_not_mem m) theorem erase_dup_le {Ξ± : Type u_1} [DecidableEq Ξ±] (s : multiset Ξ±) : erase_dup s ≀ s := quot.induction_on s fun (l : List Ξ±) => list.sublist.subperm (list.erase_dup_sublist l) theorem erase_dup_subset {Ξ± : Type u_1} [DecidableEq Ξ±] (s : multiset Ξ±) : erase_dup s βŠ† s := subset_of_le (erase_dup_le s) theorem subset_erase_dup {Ξ± : Type u_1} [DecidableEq Ξ±] (s : multiset Ξ±) : s βŠ† erase_dup s := fun (a : Ξ±) => iff.mpr mem_erase_dup @[simp] theorem erase_dup_subset' {Ξ± : Type u_1} [DecidableEq Ξ±] {s : multiset Ξ±} {t : multiset Ξ±} : erase_dup s βŠ† t ↔ s βŠ† t := { mp := subset.trans (subset_erase_dup s), mpr := subset.trans (erase_dup_subset s) } @[simp] theorem subset_erase_dup' {Ξ± : Type u_1} [DecidableEq Ξ±] {s : multiset Ξ±} {t : multiset Ξ±} : s βŠ† erase_dup t ↔ s βŠ† t := { mp := fun (h : s βŠ† erase_dup t) => subset.trans h (erase_dup_subset t), mpr := fun (h : s βŠ† t) => subset.trans h (subset_erase_dup t) } @[simp] theorem nodup_erase_dup {Ξ± : Type u_1} [DecidableEq Ξ±] (s : multiset Ξ±) : nodup (erase_dup s) := quot.induction_on s list.nodup_erase_dup theorem erase_dup_eq_self {Ξ± : Type u_1} [DecidableEq Ξ±] {s : multiset Ξ±} : erase_dup s = s ↔ nodup s := sorry theorem erase_dup_eq_zero {Ξ± : Type u_1} [DecidableEq Ξ±] {s : multiset Ξ±} : erase_dup s = 0 ↔ s = 0 := { mp := fun (h : erase_dup s = 0) => eq_zero_of_subset_zero (h β–Έ subset_erase_dup s), mpr := fun (h : s = 0) => Eq.symm h β–Έ erase_dup_zero } @[simp] theorem erase_dup_singleton {Ξ± : Type u_1} [DecidableEq Ξ±] {a : Ξ±} : erase_dup (a ::β‚˜ 0) = a ::β‚˜ 0 := iff.mpr erase_dup_eq_self (nodup_singleton a) theorem le_erase_dup {Ξ± : Type u_1} [DecidableEq Ξ±] {s : multiset Ξ±} {t : multiset Ξ±} : s ≀ erase_dup t ↔ s ≀ t ∧ nodup s := sorry theorem erase_dup_ext {Ξ± : Type u_1} [DecidableEq Ξ±] {s : multiset Ξ±} {t : multiset Ξ±} : erase_dup s = erase_dup t ↔ βˆ€ (a : Ξ±), a ∈ s ↔ a ∈ t := sorry theorem erase_dup_map_erase_dup_eq {Ξ± : Type u_1} {Ξ² : Type u_2} [DecidableEq Ξ±] [DecidableEq Ξ²] (f : Ξ± β†’ Ξ²) (s : multiset Ξ±) : erase_dup (map f (erase_dup s)) = erase_dup (map f s) := sorry @[simp] theorem erase_dup_nsmul {Ξ± : Type u_1} [DecidableEq Ξ±] {s : multiset Ξ±} {n : β„•} (h0 : n β‰  0) : erase_dup (n β€’β„• s) = erase_dup s := sorry theorem nodup.le_erase_dup_iff_le {Ξ± : Type u_1} [DecidableEq Ξ±] {s : multiset Ξ±} {t : multiset Ξ±} (hno : nodup s) : s ≀ erase_dup t ↔ s ≀ t := sorry end multiset theorem multiset.nodup.le_nsmul_iff_le {Ξ± : Type u_1} {s : multiset Ξ±} {t : multiset Ξ±} {n : β„•} (h : multiset.nodup s) (hn : n β‰  0) : s ≀ n β€’β„• t ↔ s ≀ t := sorry end Mathlib
4e09c5f9055be5c84d4ba08bc66f27166d3d0f3e
02005f45e00c7ecf2c8ca5db60251bd1e9c860b5
/src/measure_theory/borel_space.lean
789a4571270f653a07f4f0e8dcb5f4719b76e794
[ "Apache-2.0" ]
permissive
anthony2698/mathlib
03cd69fe5c280b0916f6df2d07c614c8e1efe890
407615e05814e98b24b2ff322b14e8e3eb5e5d67
refs/heads/master
1,678,792,774,873
1,614,371,563,000
1,614,371,563,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
65,710
lean
/- Copyright (c) 2017 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Yury Kudryashov -/ import measure_theory.measure_space import measure_theory.ae_measurable_sequence import analysis.complex.basic import analysis.normed_space.finite_dimension import topology.G_delta /-! # Borel (measurable) space ## Main definitions * `borel Ξ±` : the least `Οƒ`-algebra that contains all open sets; * `class borel_space` : a space with `topological_space` and `measurable_space` structures such that `β€Ήmeasurable_space Ξ±β€Ί = borel Ξ±`; * `class opens_measurable_space` : a space with `topological_space` and `measurable_space` structures such that all open sets are measurable; equivalently, `borel Ξ± ≀ β€Ήmeasurable_space Ξ±β€Ί`. * `borel_space` instances on `empty`, `unit`, `bool`, `nat`, `int`, `rat`; * `measurable` and `borel_space` instances on `ℝ`, `ℝβ‰₯0`, `ℝβ‰₯0∞`. * A measure is `regular` if it is finite on compact sets, inner regular and outer regular. ## Main statements * `is_open.measurable_set`, `is_closed.measurable_set`: open and closed sets are measurable; * `continuous.measurable` : a continuous function is measurable; * `continuous.measurable2` : if `f : Ξ± β†’ Ξ²` and `g : Ξ± β†’ Ξ³` are measurable and `op : Ξ² Γ— Ξ³ β†’ Ξ΄` is continuous, then `Ξ» x, op (f x, g y)` is measurable; * `measurable.add` etc : dot notation for arithmetic operations on `measurable` predicates, and similarly for `dist` and `edist`; * `ae_measurable.add` : similar dot notation for almost everywhere measurable functions; * `measurable.ennreal*` : special cases for arithmetic operations on `ℝβ‰₯0∞`. -/ noncomputable theory open classical set filter measure_theory open_locale classical big_operators topological_space nnreal ennreal universes u v w x y variables {Ξ± Ξ² Ξ³ Ξ³β‚‚ Ξ΄ : Type*} {ΞΉ : Sort y} {s t u : set Ξ±} open measurable_space topological_space /-- `measurable_space` structure generated by `topological_space`. -/ def borel (Ξ± : Type u) [topological_space Ξ±] : measurable_space Ξ± := generate_from {s : set Ξ± | is_open s} lemma borel_eq_top_of_discrete [topological_space Ξ±] [discrete_topology Ξ±] : borel Ξ± = ⊀ := top_le_iff.1 $ Ξ» s hs, generate_measurable.basic s (is_open_discrete s) lemma borel_eq_top_of_encodable [topological_space Ξ±] [t1_space Ξ±] [encodable Ξ±] : borel Ξ± = ⊀ := begin refine (top_le_iff.1 $ Ξ» s hs, bUnion_of_singleton s β–Έ _), apply measurable_set.bUnion s.countable_encodable, intros x hx, apply measurable_set.of_compl, apply generate_measurable.basic, exact is_closed_singleton end lemma borel_eq_generate_from_of_subbasis {s : set (set Ξ±)} [t : topological_space Ξ±] [second_countable_topology Ξ±] (hs : t = generate_from s) : borel Ξ± = generate_from s := le_antisymm (generate_from_le $ assume u (hu : t.is_open u), begin rw [hs] at hu, induction hu, case generate_open.basic : u hu { exact generate_measurable.basic u hu }, case generate_open.univ { exact @measurable_set.univ Ξ± (generate_from s) }, case generate_open.inter : s₁ sβ‚‚ _ _ hs₁ hsβ‚‚ { exact @measurable_set.inter Ξ± (generate_from s) _ _ hs₁ hsβ‚‚ }, case generate_open.sUnion : f hf ih { rcases is_open_sUnion_countable f (by rwa hs) with ⟨v, hv, vf, vu⟩, rw ← vu, exact @measurable_set.sUnion Ξ± (generate_from s) _ hv (Ξ» x xv, ih _ (vf xv)) } end) (generate_from_le $ assume u hu, generate_measurable.basic _ $ show t.is_open u, by rw [hs]; exact generate_open.basic _ hu) lemma is_pi_system_is_open [topological_space Ξ±] : is_pi_system (is_open : set Ξ± β†’ Prop) := Ξ» s t hs ht hst, is_open_inter hs ht section order_topology variable (Ξ±) variables [topological_space Ξ±] [second_countable_topology Ξ±] [linear_order Ξ±] [order_topology Ξ±] lemma borel_eq_generate_Iio : borel Ξ± = generate_from (range Iio) := begin refine le_antisymm _ (generate_from_le _), { rw borel_eq_generate_from_of_subbasis (@order_topology.topology_eq_generate_intervals Ξ± _ _ _), letI : measurable_space Ξ± := measurable_space.generate_from (range Iio), have H : βˆ€ a : Ξ±, measurable_set (Iio a) := Ξ» a, generate_measurable.basic _ ⟨_, rfl⟩, refine generate_from_le _, rintro _ ⟨a, rfl | rfl⟩; [skip, apply H], by_cases h : βˆƒ a', βˆ€ b, a < b ↔ a' ≀ b, { rcases h with ⟨a', ha'⟩, rw (_ : Ioi a = (Iio a')ᢜ), { exact (H _).compl }, simp [set.ext_iff, ha'] }, { rcases is_open_Union_countable (Ξ» a' : {a' : Ξ± // a < a'}, {b | a'.1 < b}) (Ξ» a', is_open_lt' _) with ⟨v, ⟨hv⟩, vu⟩, simp [set.ext_iff] at vu, have : Ioi a = ⋃ x : v, (Iio x.1.1)ᢜ, { simp [set.ext_iff], refine Ξ» x, ⟨λ ax, _, Ξ» ⟨a', ⟨h, av⟩, ax⟩, lt_of_lt_of_le h ax⟩, rcases (vu x).2 _ with ⟨a', h₁, hβ‚‚βŸ©, { exact ⟨a', h₁, le_of_lt hβ‚‚βŸ© }, refine not_imp_comm.1 (Ξ» h, _) h, exact ⟨x, Ξ» b, ⟨λ ab, le_of_not_lt (Ξ» h', h ⟨b, ab, h'⟩), lt_of_lt_of_le ax⟩⟩ }, rw this, resetI, apply measurable_set.Union, exact Ξ» _, (H _).compl } }, { rw forall_range_iff, intro a, exact generate_measurable.basic _ is_open_Iio } end lemma borel_eq_generate_Ioi : borel Ξ± = generate_from (range Ioi) := @borel_eq_generate_Iio (order_dual Ξ±) _ (by apply_instance : second_countable_topology Ξ±) _ _ end order_topology lemma borel_comap {f : Ξ± β†’ Ξ²} {t : topological_space Ξ²} : @borel Ξ± (t.induced f) = (@borel Ξ² t).comap f := comap_generate_from.symm lemma continuous.borel_measurable [topological_space Ξ±] [topological_space Ξ²] {f : Ξ± β†’ Ξ²} (hf : continuous f) : @measurable Ξ± Ξ² (borel Ξ±) (borel Ξ²) f := measurable.of_le_map $ generate_from_le $ Ξ» s hs, generate_measurable.basic (f ⁻¹' s) (hs.preimage hf) /-- A space with `measurable_space` and `topological_space` structures such that all open sets are measurable. -/ class opens_measurable_space (Ξ± : Type*) [topological_space Ξ±] [h : measurable_space Ξ±] : Prop := (borel_le : borel Ξ± ≀ h) /-- A space with `measurable_space` and `topological_space` structures such that the `Οƒ`-algebra of measurable sets is exactly the `Οƒ`-algebra generated by open sets. -/ class borel_space (Ξ± : Type*) [topological_space Ξ±] [measurable_space Ξ±] : Prop := (measurable_eq : β€Ήmeasurable_space Ξ±β€Ί = borel Ξ±) /-- In a `borel_space` all open sets are measurable. -/ @[priority 100] instance borel_space.opens_measurable {Ξ± : Type*} [topological_space Ξ±] [measurable_space Ξ±] [borel_space Ξ±] : opens_measurable_space Ξ± := ⟨ge_of_eq $ borel_space.measurable_eq⟩ instance subtype.borel_space {Ξ± : Type*} [topological_space Ξ±] [measurable_space Ξ±] [hΞ± : borel_space Ξ±] (s : set Ξ±) : borel_space s := ⟨by { rw [hΞ±.1, subtype.measurable_space, ← borel_comap], refl }⟩ instance subtype.opens_measurable_space {Ξ± : Type*} [topological_space Ξ±] [measurable_space Ξ±] [h : opens_measurable_space Ξ±] (s : set Ξ±) : opens_measurable_space s := ⟨by { rw [borel_comap], exact comap_mono h.1 }⟩ section variables [topological_space Ξ±] [measurable_space Ξ±] [opens_measurable_space Ξ±] [topological_space Ξ²] [measurable_space Ξ²] [opens_measurable_space Ξ²] [topological_space Ξ³] [measurable_space Ξ³] [borel_space Ξ³] [topological_space Ξ³β‚‚] [measurable_space Ξ³β‚‚] [borel_space Ξ³β‚‚] [measurable_space Ξ΄] lemma is_open.measurable_set (h : is_open s) : measurable_set s := opens_measurable_space.borel_le _ $ generate_measurable.basic _ h lemma measurable_set_interior : measurable_set (interior s) := is_open_interior.measurable_set lemma is_GΞ΄.measurable_set (h : is_GΞ΄ s) : measurable_set s := begin rcases h with ⟨S, hSo, hSc, rfl⟩, exact measurable_set.sInter hSc (Ξ» t ht, (hSo t ht).measurable_set) end lemma measurable_set_of_continuous_at {Ξ²} [emetric_space Ξ²] (f : Ξ± β†’ Ξ²) : measurable_set {x | continuous_at f x} := (is_GΞ΄_set_of_continuous_at f).measurable_set lemma is_closed.measurable_set (h : is_closed s) : measurable_set s := h.measurable_set.of_compl lemma is_compact.measurable_set [t2_space Ξ±] (h : is_compact s) : measurable_set s := h.is_closed.measurable_set lemma measurable_set_closure : measurable_set (closure s) := is_closed_closure.measurable_set lemma measurable_of_is_open {f : Ξ΄ β†’ Ξ³} (hf : βˆ€ s, is_open s β†’ measurable_set (f ⁻¹' s)) : measurable f := by { rw [β€Ήborel_space Ξ³β€Ί.measurable_eq], exact measurable_generate_from hf } lemma measurable_of_is_closed {f : Ξ΄ β†’ Ξ³} (hf : βˆ€ s, is_closed s β†’ measurable_set (f ⁻¹' s)) : measurable f := begin apply measurable_of_is_open, intros s hs, rw [← measurable_set.compl_iff, ← preimage_compl], apply hf, rw [is_closed_compl_iff], exact hs end lemma measurable_of_is_closed' {f : Ξ΄ β†’ Ξ³} (hf : βˆ€ s, is_closed s β†’ s.nonempty β†’ s β‰  univ β†’ measurable_set (f ⁻¹' s)) : measurable f := begin apply measurable_of_is_closed, intros s hs, cases eq_empty_or_nonempty s with h1 h1, { simp [h1] }, by_cases h2 : s = univ, { simp [h2] }, exact hf s hs h1 h2 end instance nhds_is_measurably_generated (a : Ξ±) : (𝓝 a).is_measurably_generated := begin rw [nhds, infi_subtype'], refine @filter.infi_is_measurably_generated _ _ _ _ (Ξ» i, _), exact i.2.2.measurable_set.principal_is_measurably_generated end /-- If `s` is a measurable set, then `𝓝[s] a` is a measurably generated filter for each `a`. This cannot be an `instance` because it depends on a non-instance `hs : measurable_set s`. -/ lemma measurable_set.nhds_within_is_measurably_generated {s : set Ξ±} (hs : measurable_set s) (a : Ξ±) : (𝓝[s] a).is_measurably_generated := by haveI := hs.principal_is_measurably_generated; exact filter.inf_is_measurably_generated _ _ @[priority 100] -- see Note [lower instance priority] instance opens_measurable_space.to_measurable_singleton_class [t1_space Ξ±] : measurable_singleton_class Ξ± := ⟨λ x, is_closed_singleton.measurable_set⟩ instance pi.opens_measurable_space {ΞΉ : Type*} {Ο€ : ΞΉ β†’ Type*} [fintype ΞΉ] [t' : Ξ  i, topological_space (Ο€ i)] [Ξ  i, measurable_space (Ο€ i)] [βˆ€ i, second_countable_topology (Ο€ i)] [βˆ€ i, opens_measurable_space (Ο€ i)] : opens_measurable_space (Ξ  i, Ο€ i) := begin constructor, choose g hc he ho hu hinst using Ξ» i, is_open_generated_countable_inter (Ο€ i), have : Pi.topological_space = generate_from {t | βˆƒ(s:Ξ a, set (Ο€ a)) (i : finset ΞΉ), (βˆ€a∈i, s a ∈ g a) ∧ t = pi ↑i s}, { rw [funext hinst, pi_generate_from_eq] }, rw [borel_eq_generate_from_of_subbasis this], apply generate_from_le, rintros _ ⟨s, i, hi, rfl⟩, refine measurable_set.pi i.countable_to_set (Ξ» a ha, is_open.measurable_set _), rw [hinst], exact generate_open.basic _ (hi a ha) end instance prod.opens_measurable_space [second_countable_topology Ξ±] [second_countable_topology Ξ²] : opens_measurable_space (Ξ± Γ— Ξ²) := begin constructor, rcases is_open_generated_countable_inter Ξ± with ⟨a, ha₁, haβ‚‚, ha₃, haβ‚„, haβ‚…βŸ©, rcases is_open_generated_countable_inter Ξ² with ⟨b, hb₁, hbβ‚‚, hb₃, hbβ‚„, hbβ‚…βŸ©, have : prod.topological_space = generate_from {g | βˆƒu∈a, βˆƒv∈b, g = set.prod u v}, { rw [haβ‚…, hbβ‚…], exact prod_generate_from_generate_from_eq haβ‚„ hbβ‚„ }, rw [borel_eq_generate_from_of_subbasis this], apply generate_from_le, rintros _ ⟨u, hu, v, hv, rfl⟩, have hu : is_open u, by { rw [haβ‚…], exact generate_open.basic _ hu }, have hv : is_open v, by { rw [hbβ‚…], exact generate_open.basic _ hv }, exact hu.measurable_set.prod hv.measurable_set end section open measure_theory end section preorder variables [preorder Ξ±] [order_closed_topology Ξ±] {a b : Ξ±} @[simp] lemma measurable_set_Ici : measurable_set (Ici a) := is_closed_Ici.measurable_set @[simp] lemma measurable_set_Iic : measurable_set (Iic a) := is_closed_Iic.measurable_set @[simp] lemma measurable_set_Icc : measurable_set (Icc a b) := is_closed_Icc.measurable_set instance nhds_within_Ici_is_measurably_generated : (𝓝[Ici b] a).is_measurably_generated := measurable_set_Ici.nhds_within_is_measurably_generated _ instance nhds_within_Iic_is_measurably_generated : (𝓝[Iic b] a).is_measurably_generated := measurable_set_Iic.nhds_within_is_measurably_generated _ instance at_top_is_measurably_generated : (filter.at_top : filter Ξ±).is_measurably_generated := @filter.infi_is_measurably_generated _ _ _ _ $ Ξ» a, (measurable_set_Ici : measurable_set (Ici a)).principal_is_measurably_generated instance at_bot_is_measurably_generated : (filter.at_bot : filter Ξ±).is_measurably_generated := @filter.infi_is_measurably_generated _ _ _ _ $ Ξ» a, (measurable_set_Iic : measurable_set (Iic a)).principal_is_measurably_generated end preorder section partial_order variables [partial_order Ξ±] [order_closed_topology Ξ±] [second_countable_topology Ξ±] {a b : Ξ±} lemma measurable_set_le' : measurable_set {p : Ξ± Γ— Ξ± | p.1 ≀ p.2} := order_closed_topology.is_closed_le'.measurable_set lemma measurable_set_le {f g : Ξ΄ β†’ Ξ±} (hf : measurable f) (hg : measurable g) : measurable_set {a | f a ≀ g a} := hf.prod_mk hg measurable_set_le' end partial_order section linear_order variables [linear_order Ξ±] [order_closed_topology Ξ±] {a b : Ξ±} @[simp] lemma measurable_set_Iio : measurable_set (Iio a) := is_open_Iio.measurable_set @[simp] lemma measurable_set_Ioi : measurable_set (Ioi a) := is_open_Ioi.measurable_set @[simp] lemma measurable_set_Ioo : measurable_set (Ioo a b) := is_open_Ioo.measurable_set @[simp] lemma measurable_set_Ioc : measurable_set (Ioc a b) := measurable_set_Ioi.inter measurable_set_Iic @[simp] lemma measurable_set_Ico : measurable_set (Ico a b) := measurable_set_Ici.inter measurable_set_Iio instance nhds_within_Ioi_is_measurably_generated : (𝓝[Ioi b] a).is_measurably_generated := measurable_set_Ioi.nhds_within_is_measurably_generated _ instance nhds_within_Iio_is_measurably_generated : (𝓝[Iio b] a).is_measurably_generated := measurable_set_Iio.nhds_within_is_measurably_generated _ variables [second_countable_topology Ξ±] lemma measurable_set_lt' : measurable_set {p : Ξ± Γ— Ξ± | p.1 < p.2} := (is_open_lt continuous_fst continuous_snd).measurable_set lemma measurable_set_lt {f g : Ξ΄ β†’ Ξ±} (hf : measurable f) (hg : measurable g) : measurable_set {a | f a < g a} := hf.prod_mk hg measurable_set_lt' end linear_order section linear_order variables [linear_order Ξ±] [order_closed_topology Ξ±] lemma measurable_set_interval {a b : Ξ±} : measurable_set (interval a b) := measurable_set_Icc variables [second_countable_topology Ξ±] lemma measurable.max {f g : Ξ΄ β†’ Ξ±} (hf : measurable f) (hg : measurable g) : measurable (Ξ» a, max (f a) (g a)) := hf.piecewise (measurable_set_le hg hf) hg lemma ae_measurable.max {f g : Ξ΄ β†’ Ξ±} {ΞΌ : measure Ξ΄} (hf : ae_measurable f ΞΌ) (hg : ae_measurable g ΞΌ) : ae_measurable (Ξ» a, max (f a) (g a)) ΞΌ := ⟨λ a, max (hf.mk f a) (hg.mk g a), hf.measurable_mk.max hg.measurable_mk, eventually_eq.compβ‚‚ hf.ae_eq_mk _ hg.ae_eq_mk⟩ lemma measurable.min {f g : Ξ΄ β†’ Ξ±} (hf : measurable f) (hg : measurable g) : measurable (Ξ» a, min (f a) (g a)) := hf.piecewise (measurable_set_le hf hg) hg lemma ae_measurable.min {f g : Ξ΄ β†’ Ξ±} {ΞΌ : measure Ξ΄} (hf : ae_measurable f ΞΌ) (hg : ae_measurable g ΞΌ) : ae_measurable (Ξ» a, min (f a) (g a)) ΞΌ := ⟨λ a, min (hf.mk f a) (hg.mk g a), hf.measurable_mk.min hg.measurable_mk, eventually_eq.compβ‚‚ hf.ae_eq_mk _ hg.ae_eq_mk⟩ end linear_order /-- A continuous function from an `opens_measurable_space` to a `borel_space` is measurable. -/ lemma continuous.measurable {f : Ξ± β†’ Ξ³} (hf : continuous f) : measurable f := hf.borel_measurable.mono opens_measurable_space.borel_le (le_of_eq $ borel_space.measurable_eq) section homeomorph /-- A homeomorphism between two Borel spaces is a measurable equivalence.-/ def homeomorph.to_measurable_equiv (h : Ξ³ β‰ƒβ‚œ Ξ³β‚‚) : Ξ³ ≃ᡐ Ξ³β‚‚ := { measurable_to_fun := h.continuous_to_fun.measurable, measurable_inv_fun := h.continuous_inv_fun.measurable, .. h } @[simp] lemma homeomorph.to_measurable_equiv_coe (h : Ξ³ β‰ƒβ‚œ Ξ³β‚‚) : (h.to_measurable_equiv : Ξ³ β†’ Ξ³β‚‚) = h := rfl @[simp] lemma homeomorph.to_measurable_equiv_symm_coe (h : Ξ³ β‰ƒβ‚œ Ξ³β‚‚) : (h.to_measurable_equiv.symm : Ξ³β‚‚ β†’ Ξ³) = h.symm := rfl end homeomorph lemma measurable_of_continuous_on_compl_singleton [t1_space Ξ±] {f : Ξ± β†’ Ξ³} (a : Ξ±) (hf : continuous_on f {x | x β‰  a}) : measurable f := measurable_of_measurable_on_compl_singleton a (continuous_on_iff_continuous_restrict.1 hf).measurable lemma continuous.measurable2 [second_countable_topology Ξ±] [second_countable_topology Ξ²] {f : Ξ΄ β†’ Ξ±} {g : Ξ΄ β†’ Ξ²} {c : Ξ± β†’ Ξ² β†’ Ξ³} (h : continuous (Ξ» p : Ξ± Γ— Ξ², c p.1 p.2)) (hf : measurable f) (hg : measurable g) : measurable (Ξ» a, c (f a) (g a)) := h.measurable.comp (hf.prod_mk hg) lemma continuous.ae_measurable2 [second_countable_topology Ξ±] [second_countable_topology Ξ²] {f : Ξ΄ β†’ Ξ±} {g : Ξ΄ β†’ Ξ²} {c : Ξ± β†’ Ξ² β†’ Ξ³} {ΞΌ : measure Ξ΄} (h : continuous (Ξ» p : Ξ± Γ— Ξ², c p.1 p.2)) (hf : ae_measurable f ΞΌ) (hg : ae_measurable g ΞΌ) : ae_measurable (Ξ» a, c (f a) (g a)) ΞΌ := h.measurable.comp_ae_measurable (hf.prod_mk hg) lemma measurable.smul [semiring Ξ±] [second_countable_topology Ξ±] [add_comm_monoid Ξ³] [second_countable_topology Ξ³] [semimodule Ξ± Ξ³] [topological_semimodule Ξ± Ξ³] {f : Ξ΄ β†’ Ξ±} {g : Ξ΄ β†’ Ξ³} (hf : measurable f) (hg : measurable g) : measurable (Ξ» c, f c β€’ g c) := continuous_smul.measurable2 hf hg lemma ae_measurable.smul [semiring Ξ±] [second_countable_topology Ξ±] [add_comm_monoid Ξ³] [second_countable_topology Ξ³] [semimodule Ξ± Ξ³] [topological_semimodule Ξ± Ξ³] {f : Ξ΄ β†’ Ξ±} {g : Ξ΄ β†’ Ξ³} {ΞΌ : measure Ξ΄} (hf : ae_measurable f ΞΌ) (hg : ae_measurable g ΞΌ) : ae_measurable (Ξ» c, f c β€’ g c) ΞΌ := continuous_smul.ae_measurable2 hf hg lemma measurable.const_smul {R M : Type*} [topological_space R] [semiring R] [add_comm_monoid M] [semimodule R M] [topological_space M] [topological_semimodule R M] [measurable_space M] [borel_space M] {f : Ξ΄ β†’ M} (hf : measurable f) (c : R) : measurable (Ξ» x, c β€’ f x) := (continuous_const.smul continuous_id).measurable.comp hf lemma ae_measurable.const_smul {R M : Type*} [topological_space R] [semiring R] [add_comm_monoid M] [semimodule R M] [topological_space M] [topological_semimodule R M] [measurable_space M] [borel_space M] {f : Ξ΄ β†’ M} {ΞΌ : measure Ξ΄} (hf : ae_measurable f ΞΌ) (c : R) : ae_measurable (Ξ» x, c β€’ f x) ΞΌ := (continuous_const.smul continuous_id).measurable.comp_ae_measurable hf lemma measurable_const_smul_iff {Ξ± : Type*} [topological_space Ξ±] [division_ring Ξ±] [add_comm_monoid Ξ³] [semimodule Ξ± Ξ³] [topological_semimodule Ξ± Ξ³] {f : Ξ΄ β†’ Ξ³} {c : Ξ±} (hc : c β‰  0) : measurable (Ξ» x, c β€’ f x) ↔ measurable f := ⟨λ h, by simpa only [smul_smul, inv_mul_cancel hc, one_smul] using h.const_smul c⁻¹, Ξ» h, h.const_smul c⟩ lemma ae_measurable_const_smul_iff {Ξ± : Type*} [topological_space Ξ±] [division_ring Ξ±] [add_comm_monoid Ξ³] [semimodule Ξ± Ξ³] [topological_semimodule Ξ± Ξ³] {f : Ξ΄ β†’ Ξ³} {ΞΌ : measure Ξ΄} {c : Ξ±} (hc : c β‰  0) : ae_measurable (Ξ» x, c β€’ f x) ΞΌ ↔ ae_measurable f ΞΌ := ⟨λ h, by simpa only [smul_smul, inv_mul_cancel hc, one_smul] using h.const_smul c⁻¹, Ξ» h, h.const_smul c⟩ end section borel_space variables [topological_space Ξ±] [measurable_space Ξ±] [borel_space Ξ±] [topological_space Ξ²] [measurable_space Ξ²] [borel_space Ξ²] [topological_space Ξ³] [measurable_space Ξ³] [borel_space Ξ³] [measurable_space Ξ΄] lemma pi_le_borel_pi {ΞΉ : Type*} {Ο€ : ΞΉ β†’ Type*} [Ξ  i, topological_space (Ο€ i)] [Ξ  i, measurable_space (Ο€ i)] [βˆ€ i, borel_space (Ο€ i)] : measurable_space.pi ≀ borel (Ξ  i, Ο€ i) := begin have : β€ΉΞ  i, measurable_space (Ο€ i)β€Ί = Ξ» i, borel (Ο€ i) := funext (Ξ» i, borel_space.measurable_eq), rw [this], exact supr_le (Ξ» i, comap_le_iff_le_map.2 $ (continuous_apply i).borel_measurable) end lemma prod_le_borel_prod : prod.measurable_space ≀ borel (Ξ± Γ— Ξ²) := begin rw [β€Ήborel_space Ξ±β€Ί.measurable_eq, β€Ήborel_space Ξ²β€Ί.measurable_eq], refine sup_le _ _, { exact comap_le_iff_le_map.mpr continuous_fst.borel_measurable }, { exact comap_le_iff_le_map.mpr continuous_snd.borel_measurable } end instance pi.borel_space {ΞΉ : Type*} {Ο€ : ΞΉ β†’ Type*} [fintype ΞΉ] [t' : Ξ  i, topological_space (Ο€ i)] [Ξ  i, measurable_space (Ο€ i)] [βˆ€ i, second_countable_topology (Ο€ i)] [βˆ€ i, borel_space (Ο€ i)] : borel_space (Ξ  i, Ο€ i) := ⟨le_antisymm pi_le_borel_pi opens_measurable_space.borel_le⟩ instance prod.borel_space [second_countable_topology Ξ±] [second_countable_topology Ξ²] : borel_space (Ξ± Γ— Ξ²) := ⟨le_antisymm prod_le_borel_prod opens_measurable_space.borel_le⟩ @[to_additive] lemma measurable_mul [has_mul Ξ±] [has_continuous_mul Ξ±] [second_countable_topology Ξ±] : measurable (Ξ» p : Ξ± Γ— Ξ±, p.1 * p.2) := continuous_mul.measurable @[to_additive] lemma measurable.mul [has_mul Ξ±] [has_continuous_mul Ξ±] [second_countable_topology Ξ±] {f : Ξ΄ β†’ Ξ±} {g : Ξ΄ β†’ Ξ±} : measurable f β†’ measurable g β†’ measurable (Ξ» a, f a * g a) := (@continuous_mul Ξ± _ _ _).measurable2 @[to_additive] lemma ae_measurable.mul [has_mul Ξ±] [has_continuous_mul Ξ±] [second_countable_topology Ξ±] {f : Ξ΄ β†’ Ξ±} {g : Ξ΄ β†’ Ξ±} {ΞΌ : measure Ξ΄} (hf : ae_measurable f ΞΌ) (hg : ae_measurable g ΞΌ) : ae_measurable (Ξ» a, f a * g a) ΞΌ := (@continuous_mul Ξ± _ _ _).ae_measurable2 hf hg /-- A variant of `measurable.mul` that uses `*` on functions -/ @[to_additive] lemma measurable.mul' [has_mul Ξ±] [has_continuous_mul Ξ±] [second_countable_topology Ξ±] {f : Ξ΄ β†’ Ξ±} {g : Ξ΄ β†’ Ξ±} : measurable f β†’ measurable g β†’ measurable (f * g) := measurable.mul @[to_additive] lemma measurable_mul_left [has_mul Ξ±] [has_continuous_mul Ξ±] (x : Ξ±) : measurable (Ξ» y : Ξ±, x * y) := continuous.measurable $ continuous_const.mul continuous_id @[to_additive] lemma measurable_mul_right [has_mul Ξ±] [has_continuous_mul Ξ±] (x : Ξ±) : measurable (Ξ» y : Ξ±, y * x) := continuous.measurable $ continuous_id.mul continuous_const @[to_additive] lemma measurable.const_mul [has_mul Ξ±] [has_continuous_mul Ξ±] {f : Ξ΄ β†’ Ξ±} (hf : measurable f) (c : Ξ±) : measurable (Ξ» x, c * f x) := (measurable_mul_left _).comp hf @[to_additive] lemma measurable.mul_const [has_mul Ξ±] [has_continuous_mul Ξ±] {f : Ξ΄ β†’ Ξ±} (hf : measurable f) (c : Ξ±) : measurable (Ξ» x, f x * c) := (measurable_mul_right _).comp hf lemma measurable.sub_const [sub_neg_monoid Ξ±] [has_continuous_add Ξ±] {f : Ξ΄ β†’ Ξ±} (hf : measurable f) (c : Ξ±) : measurable (Ξ» x, f x - c) := by simpa only [sub_eq_add_neg] using hf.add_const (-c) @[to_additive] lemma finset.measurable_prod {ΞΉ : Type*} [comm_monoid Ξ±] [has_continuous_mul Ξ±] [second_countable_topology Ξ±] {f : ΞΉ β†’ Ξ΄ β†’ Ξ±} (s : finset ΞΉ) (hf : βˆ€i, measurable (f i)) : measurable (Ξ» a, ∏ i in s, f i a) := finset.induction_on s (by simp only [finset.prod_empty, measurable_const]) (assume i s his ih, by simpa [his] using (hf i).mul ih) @[to_additive] lemma finset.ae_measurable_prod {ΞΉ : Type*} [comm_monoid Ξ±] [has_continuous_mul Ξ±] [second_countable_topology Ξ±] {f : ΞΉ β†’ Ξ΄ β†’ Ξ±} {ΞΌ : measure Ξ΄} (s : finset ΞΉ) (hf : βˆ€i, ae_measurable (f i) ΞΌ) : ae_measurable (Ξ» a, ∏ i in s, f i a) ΞΌ := finset.induction_on s (by simp only [finset.prod_empty, ae_measurable_const]) (assume i s his ih, by simpa [his] using (hf i).mul ih) @[to_additive] lemma measurable_inv [group Ξ±] [topological_group Ξ±] : measurable (has_inv.inv : Ξ± β†’ Ξ±) := continuous_inv.measurable @[to_additive] lemma measurable.inv [group Ξ±] [topological_group Ξ±] {f : Ξ΄ β†’ Ξ±} (hf : measurable f) : measurable (Ξ» a, (f a)⁻¹) := measurable_inv.comp hf @[to_additive] lemma ae_measurable.inv [group Ξ±] [topological_group Ξ±] {f : Ξ΄ β†’ Ξ±} {ΞΌ : measure Ξ΄} (hf : ae_measurable f ΞΌ) : ae_measurable (Ξ» a, (f a)⁻¹) ΞΌ := measurable_inv.comp_ae_measurable hf lemma measurable_inv' {Ξ± : Type*} [normed_field Ξ±] [measurable_space Ξ±] [borel_space Ξ±] : measurable (has_inv.inv : Ξ± β†’ Ξ±) := measurable_of_continuous_on_compl_singleton 0 continuous_on_inv' lemma measurable.inv' {Ξ± : Type*} [normed_field Ξ±] [measurable_space Ξ±] [borel_space Ξ±] {f : Ξ΄ β†’ Ξ±} (hf : measurable f) : measurable (Ξ» a, (f a)⁻¹) := measurable_inv'.comp hf lemma measurable.div {Ξ± : Type*} [normed_field Ξ±] [measurable_space Ξ±] [borel_space Ξ±] [second_countable_topology Ξ±] {f g : Ξ΄ β†’ Ξ±} (hf : measurable f) (hg : measurable g) : measurable (Ξ» a, f a / g a) := hf.mul hg.inv' @[to_additive] lemma measurable.of_inv [group Ξ±] [topological_group Ξ±] {f : Ξ΄ β†’ Ξ±} (hf : measurable (Ξ» a, (f a)⁻¹)) : measurable f := by simpa only [inv_inv] using hf.inv @[simp, to_additive] lemma measurable_inv_iff [group Ξ±] [topological_group Ξ±] {f : Ξ΄ β†’ Ξ±} : measurable (Ξ» a, (f a)⁻¹) ↔ measurable f := ⟨measurable.of_inv, measurable.inv⟩ lemma measurable.sub [add_group Ξ±] [topological_add_group Ξ±] [second_countable_topology Ξ±] {f g : Ξ΄ β†’ Ξ±} (hf : measurable f) (hg : measurable g) : measurable (Ξ» x, f x - g x) := by simpa only [sub_eq_add_neg] using hf.add hg.neg lemma ae_measurable.sub [add_group Ξ±] [topological_add_group Ξ±] [second_countable_topology Ξ±] {f g : Ξ΄ β†’ Ξ±} {ΞΌ : measure Ξ΄} (hf : ae_measurable f ΞΌ) (hg : ae_measurable g ΞΌ) : ae_measurable (Ξ» x, f x - g x) ΞΌ := by simpa only [sub_eq_add_neg] using hf.add hg.neg lemma closed_embedding.measurable_inv_fun [n : nonempty Ξ²] {g : Ξ² β†’ Ξ³} (hg : closed_embedding g) : measurable (function.inv_fun g) := begin refine measurable_of_is_closed (Ξ» s hs, _), by_cases h : classical.choice n ∈ s, { rw preimage_inv_fun_of_mem hg.to_embedding.inj h, exact (hg.closed_iff_image_closed.mp hs).measurable_set.union hg.closed_range.measurable_set.compl }, { rw preimage_inv_fun_of_not_mem hg.to_embedding.inj h, exact (hg.closed_iff_image_closed.mp hs).measurable_set } end lemma measurable_comp_iff_of_closed_embedding {f : Ξ΄ β†’ Ξ²} (g : Ξ² β†’ Ξ³) (hg : closed_embedding g) : measurable (g ∘ f) ↔ measurable f := begin refine ⟨λ hf, _, Ξ» hf, hg.continuous.measurable.comp hf⟩, apply measurable_of_is_closed, intros s hs, convert hf (hg.is_closed_map s hs).measurable_set, rw [@preimage_comp _ _ _ f g, preimage_image_eq _ hg.to_embedding.inj] end lemma ae_measurable_comp_iff_of_closed_embedding {f : Ξ΄ β†’ Ξ²} {ΞΌ : measure Ξ΄} (g : Ξ² β†’ Ξ³) (hg : closed_embedding g) : ae_measurable (g ∘ f) ΞΌ ↔ ae_measurable f ΞΌ := begin by_cases h : nonempty Ξ², { resetI, refine ⟨λ hf, _, Ξ» hf, hg.continuous.measurable.comp_ae_measurable hf⟩, convert hg.measurable_inv_fun.comp_ae_measurable hf, ext x, exact (function.left_inverse_inv_fun hg.to_embedding.inj (f x)).symm }, { have H : Β¬ nonempty Ξ΄, by { contrapose! h, exact nonempty.map f h }, simp [(measurable_of_not_nonempty H (g ∘ f)).ae_measurable, (measurable_of_not_nonempty H f).ae_measurable] } end section linear_order variables [linear_order Ξ±] [order_topology Ξ±] [second_countable_topology Ξ±] lemma measurable_of_Iio {f : Ξ΄ β†’ Ξ±} (hf : βˆ€ x, measurable_set (f ⁻¹' Iio x)) : measurable f := begin convert measurable_generate_from _, exact borel_space.measurable_eq.trans (borel_eq_generate_Iio _), rintro _ ⟨x, rfl⟩, exact hf x end lemma measurable_of_Ioi {f : Ξ΄ β†’ Ξ±} (hf : βˆ€ x, measurable_set (f ⁻¹' Ioi x)) : measurable f := begin convert measurable_generate_from _, exact borel_space.measurable_eq.trans (borel_eq_generate_Ioi _), rintro _ ⟨x, rfl⟩, exact hf x end lemma measurable_of_Iic {f : Ξ΄ β†’ Ξ±} (hf : βˆ€ x, measurable_set (f ⁻¹' Iic x)) : measurable f := begin apply measurable_of_Ioi, simp_rw [← compl_Iic, preimage_compl, measurable_set.compl_iff], assumption end lemma measurable_of_Ici {f : Ξ΄ β†’ Ξ±} (hf : βˆ€ x, measurable_set (f ⁻¹' Ici x)) : measurable f := begin apply measurable_of_Iio, simp_rw [← compl_Ici, preimage_compl, measurable_set.compl_iff], assumption end lemma measurable.is_lub {ΞΉ} [encodable ΞΉ] {f : ΞΉ β†’ Ξ΄ β†’ Ξ±} {g : Ξ΄ β†’ Ξ±} (hf : βˆ€ i, measurable (f i)) (hg : βˆ€ b, is_lub {a | βˆƒ i, f i b = a} (g b)) : measurable g := begin change βˆ€ b, is_lub (range $ Ξ» i, f i b) (g b) at hg, rw [β€Ήborel_space Ξ±β€Ί.measurable_eq, borel_eq_generate_Ioi Ξ±], apply measurable_generate_from, rintro _ ⟨a, rfl⟩, simp_rw [set.preimage, mem_Ioi, lt_is_lub_iff (hg _), exists_range_iff, set_of_exists], exact measurable_set.Union (Ξ» i, hf i (is_open_lt' _).measurable_set) end private lemma ae_measurable.is_lub_of_nonempty {ΞΉ} (hΞΉ : nonempty ΞΉ) {ΞΌ : measure Ξ΄} [encodable ΞΉ] {f : ΞΉ β†’ Ξ΄ β†’ Ξ±} {g : Ξ΄ β†’ Ξ±} (hf : βˆ€ i, ae_measurable (f i) ΞΌ) (hg : βˆ€α΅ b βˆ‚ΞΌ, is_lub {a | βˆƒ i, f i b = a} (g b)) : ae_measurable g ΞΌ := begin let p : Ξ΄ β†’ (ΞΉ β†’ Ξ±) β†’ Prop := Ξ» x f', is_lub {a | βˆƒ i, f' i = a} (g x), let g_seq := Ξ» x, ite (x ∈ ae_seq_set hf p) (g x) (⟨g x⟩ : nonempty Ξ±).some, have hg_seq : βˆ€ b, is_lub {a | βˆƒ i, ae_seq hf p i b = a} (g_seq b), { intro b, haveI hΞ± : nonempty Ξ± := nonempty.map g ⟨b⟩, simp only [ae_seq, g_seq], split_ifs, { have h_set_eq : {a : Ξ± | βˆƒ (i : ΞΉ), (hf i).mk (f i) b = a} = {a : Ξ± | βˆƒ (i : ΞΉ), f i b = a}, { ext x, simp_rw [set.mem_set_of_eq, ae_seq.mk_eq_fun_of_mem_ae_seq_set hf h], }, rw h_set_eq, exact ae_seq.fun_prop_of_mem_ae_seq_set hf h, }, { have h_singleton : {a : Ξ± | βˆƒ (i : ΞΉ), hΞ±.some = a} = {hΞ±.some}, { ext1 x, exact ⟨λ hx, hx.some_spec.symm, Ξ» hx, ⟨hΞΉ.some, hx.symm⟩⟩, }, rw h_singleton, exact is_lub_singleton, }, }, refine ⟨g_seq, measurable.is_lub (ae_seq.measurable hf p) hg_seq, _⟩, exact (ite_ae_eq_of_measure_compl_zero g (Ξ» x, (⟨g x⟩ : nonempty Ξ±).some) (ae_seq_set hf p) (ae_seq.measure_compl_ae_seq_set_eq_zero hf hg)).symm, end lemma ae_measurable.is_lub {ΞΉ} {ΞΌ : measure Ξ΄} [encodable ΞΉ] {f : ΞΉ β†’ Ξ΄ β†’ Ξ±} {g : Ξ΄ β†’ Ξ±} (hf : βˆ€ i, ae_measurable (f i) ΞΌ) (hg : βˆ€α΅ b βˆ‚ΞΌ, is_lub {a | βˆƒ i, f i b = a} (g b)) : ae_measurable g ΞΌ := begin by_cases hΞΌ : ΞΌ = 0, { rw hΞΌ, exact ae_measurable_of_zero_measure }, haveI : ΞΌ.ae.ne_bot := by simpa [ne_bot_iff], by_cases hΞΉ : nonempty ΞΉ, { exact ae_measurable.is_lub_of_nonempty hΞΉ hf hg, }, suffices : βˆƒ x, g =ᡐ[ΞΌ] Ξ» y, g x, by { exact ⟨(Ξ» y, g this.some), measurable_const, this.some_spec⟩, }, have h_empty : βˆ€ x, {a : Ξ± | βˆƒ (i : ΞΉ), f i x = a} = βˆ…, { intro x, ext1 y, rw [set.mem_set_of_eq, set.mem_empty_eq, iff_false], exact Ξ» hi, hΞΉ (nonempty_of_exists hi), }, simp_rw h_empty at hg, exact ⟨hg.exists.some, hg.mono (Ξ» y hy, is_lub.unique hy hg.exists.some_spec)⟩, end lemma measurable.is_glb {ΞΉ} [encodable ΞΉ] {f : ΞΉ β†’ Ξ΄ β†’ Ξ±} {g : Ξ΄ β†’ Ξ±} (hf : βˆ€ i, measurable (f i)) (hg : βˆ€ b, is_glb {a | βˆƒ i, f i b = a} (g b)) : measurable g := begin change βˆ€ b, is_glb (range $ Ξ» i, f i b) (g b) at hg, rw [β€Ήborel_space Ξ±β€Ί.measurable_eq, borel_eq_generate_Iio Ξ±], apply measurable_generate_from, rintro _ ⟨a, rfl⟩, simp_rw [set.preimage, mem_Iio, is_glb_lt_iff (hg _), exists_range_iff, set_of_exists], exact measurable_set.Union (Ξ» i, hf i (is_open_gt' _).measurable_set) end private lemma ae_measurable.is_glb_of_nonempty {ΞΉ} (hΞΉ : nonempty ΞΉ) {ΞΌ : measure Ξ΄} [encodable ΞΉ] {f : ΞΉ β†’ Ξ΄ β†’ Ξ±} {g : Ξ΄ β†’ Ξ±} (hf : βˆ€ i, ae_measurable (f i) ΞΌ) (hg : βˆ€α΅ b βˆ‚ΞΌ, is_glb {a | βˆƒ i, f i b = a} (g b)) : ae_measurable g ΞΌ := begin let p : Ξ΄ β†’ (ΞΉ β†’ Ξ±) β†’ Prop := Ξ» x f', is_glb {a | βˆƒ i, f' i = a} (g x), let g_seq := Ξ» x, ite (x ∈ ae_seq_set hf p) (g x) (⟨g x⟩ : nonempty Ξ±).some, have hg_seq : βˆ€ b, is_glb {a | βˆƒ i, ae_seq hf p i b = a} (g_seq b), { intro b, haveI hΞ± : nonempty Ξ± := nonempty.map g ⟨b⟩, simp only [ae_seq, g_seq], split_ifs, { have h_set_eq : {a : Ξ± | βˆƒ (i : ΞΉ), (hf i).mk (f i) b = a} = {a : Ξ± | βˆƒ (i : ΞΉ), f i b = a}, { ext x, simp_rw [set.mem_set_of_eq, ae_seq.mk_eq_fun_of_mem_ae_seq_set hf h], }, rw h_set_eq, exact ae_seq.fun_prop_of_mem_ae_seq_set hf h, }, { have h_singleton : {a : Ξ± | βˆƒ (i : ΞΉ), hΞ±.some = a} = {hΞ±.some}, { ext1 x, exact ⟨λ hx, hx.some_spec.symm, Ξ» hx, ⟨hΞΉ.some, hx.symm⟩⟩, }, rw h_singleton, exact is_glb_singleton, }, }, refine ⟨g_seq, measurable.is_glb (ae_seq.measurable hf p) hg_seq, _⟩, exact (ite_ae_eq_of_measure_compl_zero g (Ξ» x, (⟨g x⟩ : nonempty Ξ±).some) (ae_seq_set hf p) (ae_seq.measure_compl_ae_seq_set_eq_zero hf hg)).symm, end lemma ae_measurable.is_glb {ΞΉ} {ΞΌ : measure Ξ΄} [encodable ΞΉ] {f : ΞΉ β†’ Ξ΄ β†’ Ξ±} {g : Ξ΄ β†’ Ξ±} (hf : βˆ€ i, ae_measurable (f i) ΞΌ) (hg : βˆ€α΅ b βˆ‚ΞΌ, is_glb {a | βˆƒ i, f i b = a} (g b)) : ae_measurable g ΞΌ := begin by_cases hΞΌ : ΞΌ = 0, { rw hΞΌ, exact ae_measurable_of_zero_measure }, haveI : ΞΌ.ae.ne_bot := by simpa [ne_bot_iff], by_cases hΞΉ : nonempty ΞΉ, { exact ae_measurable.is_glb_of_nonempty hΞΉ hf hg, }, suffices : βˆƒ x, g =ᡐ[ΞΌ] Ξ» y, g x, by { exact ⟨(Ξ» y, g this.some), measurable_const, this.some_spec⟩, }, have h_empty : βˆ€ x, {a : Ξ± | βˆƒ (i : ΞΉ), f i x = a} = βˆ…, { intro x, ext1 y, rw [set.mem_set_of_eq, set.mem_empty_eq, iff_false], exact Ξ» hi, hΞΉ (nonempty_of_exists hi), }, simp_rw h_empty at hg, exact ⟨hg.exists.some, hg.mono (Ξ» y hy, is_glb.unique hy hg.exists.some_spec)⟩, end end linear_order lemma measurable.supr_Prop {Ξ±} [measurable_space Ξ±] [complete_lattice Ξ±] (p : Prop) {f : Ξ΄ β†’ Ξ±} (hf : measurable f) : measurable (Ξ» b, ⨆ h : p, f b) := classical.by_cases (assume h : p, begin convert hf, funext, exact supr_pos h end) (assume h : Β¬p, begin convert measurable_const, funext, exact supr_neg h end) lemma measurable.infi_Prop {Ξ±} [measurable_space Ξ±] [complete_lattice Ξ±] (p : Prop) {f : Ξ΄ β†’ Ξ±} (hf : measurable f) : measurable (Ξ» b, β¨… h : p, f b) := classical.by_cases (assume h : p, begin convert hf, funext, exact infi_pos h end ) (assume h : Β¬p, begin convert measurable_const, funext, exact infi_neg h end) section complete_linear_order variables [complete_linear_order Ξ±] [order_topology Ξ±] [second_countable_topology Ξ±] lemma measurable_supr {ΞΉ} [encodable ΞΉ] {f : ΞΉ β†’ Ξ΄ β†’ Ξ±} (hf : βˆ€ i, measurable (f i)) : measurable (Ξ» b, ⨆ i, f i b) := measurable.is_lub hf $ Ξ» b, is_lub_supr lemma ae_measurable_supr {ΞΉ} {ΞΌ : measure Ξ΄} [encodable ΞΉ] {f : ΞΉ β†’ Ξ΄ β†’ Ξ±} (hf : βˆ€ i, ae_measurable (f i) ΞΌ) : ae_measurable (Ξ» b, ⨆ i, f i b) ΞΌ := ae_measurable.is_lub hf $ (ae_of_all ΞΌ (Ξ» b, is_lub_supr)) lemma measurable_infi {ΞΉ} [encodable ΞΉ] {f : ΞΉ β†’ Ξ΄ β†’ Ξ±} (hf : βˆ€ i, measurable (f i)) : measurable (Ξ» b, β¨… i, f i b) := measurable.is_glb hf $ Ξ» b, is_glb_infi lemma ae_measurable_infi {ΞΉ} {ΞΌ : measure Ξ΄} [encodable ΞΉ] {f : ΞΉ β†’ Ξ΄ β†’ Ξ±} (hf : βˆ€ i, ae_measurable (f i) ΞΌ) : ae_measurable (Ξ» b, β¨… i, f i b) ΞΌ := ae_measurable.is_glb hf $ (ae_of_all ΞΌ (Ξ» b, is_glb_infi)) lemma measurable_bsupr {ΞΉ} (s : set ΞΉ) {f : ΞΉ β†’ Ξ΄ β†’ Ξ±} (hs : countable s) (hf : βˆ€ i, measurable (f i)) : measurable (Ξ» b, ⨆ i ∈ s, f i b) := by { haveI : encodable s := hs.to_encodable, simp only [supr_subtype'], exact measurable_supr (Ξ» i, hf i) } lemma ae_measurable_bsupr {ΞΉ} {ΞΌ : measure Ξ΄} (s : set ΞΉ) {f : ΞΉ β†’ Ξ΄ β†’ Ξ±} (hs : countable s) (hf : βˆ€ i, ae_measurable (f i) ΞΌ) : ae_measurable (Ξ» b, ⨆ i ∈ s, f i b) ΞΌ := begin haveI : encodable s := hs.to_encodable, simp only [supr_subtype'], exact ae_measurable_supr (Ξ» i, hf i), end lemma measurable_binfi {ΞΉ} (s : set ΞΉ) {f : ΞΉ β†’ Ξ΄ β†’ Ξ±} (hs : countable s) (hf : βˆ€ i, measurable (f i)) : measurable (Ξ» b, β¨… i ∈ s, f i b) := by { haveI : encodable s := hs.to_encodable, simp only [infi_subtype'], exact measurable_infi (Ξ» i, hf i) } lemma ae_measurable_binfi {ΞΉ} {ΞΌ : measure Ξ΄} (s : set ΞΉ) {f : ΞΉ β†’ Ξ΄ β†’ Ξ±} (hs : countable s) (hf : βˆ€ i, ae_measurable (f i) ΞΌ) : ae_measurable (Ξ» b, β¨… i ∈ s, f i b) ΞΌ := begin haveI : encodable s := hs.to_encodable, simp only [infi_subtype'], exact ae_measurable_infi (Ξ» i, hf i), end /-- `liminf` over a general filter is measurable. See `measurable_liminf` for the version over `β„•`. -/ lemma measurable_liminf' {ΞΉ ΞΉ'} {f : ΞΉ β†’ Ξ΄ β†’ Ξ±} {u : filter ΞΉ} (hf : βˆ€ i, measurable (f i)) {p : ΞΉ' β†’ Prop} {s : ΞΉ' β†’ set ΞΉ} (hu : u.has_countable_basis p s) (hs : βˆ€ i, (s i).countable) : measurable (Ξ» x, liminf u (Ξ» i, f i x)) := begin simp_rw [hu.to_has_basis.liminf_eq_supr_infi], refine measurable_bsupr _ hu.countable _, exact Ξ» i, measurable_binfi _ (hs i) hf end /-- `limsup` over a general filter is measurable. See `measurable_limsup` for the version over `β„•`. -/ lemma measurable_limsup' {ΞΉ ΞΉ'} {f : ΞΉ β†’ Ξ΄ β†’ Ξ±} {u : filter ΞΉ} (hf : βˆ€ i, measurable (f i)) {p : ΞΉ' β†’ Prop} {s : ΞΉ' β†’ set ΞΉ} (hu : u.has_countable_basis p s) (hs : βˆ€ i, (s i).countable) : measurable (Ξ» x, limsup u (Ξ» i, f i x)) := begin simp_rw [hu.to_has_basis.limsup_eq_infi_supr], refine measurable_binfi _ hu.countable _, exact Ξ» i, measurable_bsupr _ (hs i) hf end /-- `liminf` over `β„•` is measurable. See `measurable_liminf'` for a version with a general filter. -/ lemma measurable_liminf {f : β„• β†’ Ξ΄ β†’ Ξ±} (hf : βˆ€ i, measurable (f i)) : measurable (Ξ» x, liminf at_top (Ξ» i, f i x)) := measurable_liminf' hf at_top_countable_basis (Ξ» i, countable_encodable _) /-- `limsup` over `β„•` is measurable. See `measurable_limsup'` for a version with a general filter. -/ lemma measurable_limsup {f : β„• β†’ Ξ΄ β†’ Ξ±} (hf : βˆ€ i, measurable (f i)) : measurable (Ξ» x, limsup at_top (Ξ» i, f i x)) := measurable_limsup' hf at_top_countable_basis (Ξ» i, countable_encodable _) end complete_linear_order section conditionally_complete_linear_order variables [conditionally_complete_linear_order Ξ±] [second_countable_topology Ξ±] [order_topology Ξ±] lemma measurable_cSup {ΞΉ} {f : ΞΉ β†’ Ξ΄ β†’ Ξ±} {s : set ΞΉ} (hs : s.countable) (hf : βˆ€ i, measurable (f i)) (bdd : βˆ€ x, bdd_above ((Ξ» i, f i x) '' s)) : measurable (Ξ» x, Sup ((Ξ» i, f i x) '' s)) := begin cases eq_empty_or_nonempty s with h2s h2s, { simp [h2s, measurable_const] }, { apply measurable_of_Iic, intro y, simp_rw [preimage, mem_Iic, cSup_le_iff (bdd _) (h2s.image _), ball_image_iff, set_of_forall], exact measurable_set.bInter hs (Ξ» i hi, measurable_set_le (hf i) measurable_const) } end end conditionally_complete_linear_order /-- Convert a `homeomorph` to a `measurable_equiv`. -/ def homemorph.to_measurable_equiv (h : Ξ± β‰ƒβ‚œ Ξ²) : Ξ± ≃ᡐ Ξ² := { to_equiv := h.to_equiv, measurable_to_fun := h.continuous_to_fun.measurable, measurable_inv_fun := h.continuous_inv_fun.measurable } end borel_space instance empty.borel_space : borel_space empty := ⟨borel_eq_top_of_discrete.symm⟩ instance unit.borel_space : borel_space unit := ⟨borel_eq_top_of_discrete.symm⟩ instance bool.borel_space : borel_space bool := ⟨borel_eq_top_of_discrete.symm⟩ instance nat.borel_space : borel_space β„• := ⟨borel_eq_top_of_discrete.symm⟩ instance int.borel_space : borel_space β„€ := ⟨borel_eq_top_of_discrete.symm⟩ instance rat.borel_space : borel_space β„š := ⟨borel_eq_top_of_encodable.symm⟩ instance real.measurable_space : measurable_space ℝ := borel ℝ instance real.borel_space : borel_space ℝ := ⟨rfl⟩ instance nnreal.measurable_space : measurable_space ℝβ‰₯0 := subtype.measurable_space instance nnreal.borel_space : borel_space ℝβ‰₯0 := subtype.borel_space _ instance ennreal.measurable_space : measurable_space ℝβ‰₯0∞ := borel ℝβ‰₯0∞ instance ennreal.borel_space : borel_space ℝβ‰₯0∞ := ⟨rfl⟩ instance complex.measurable_space : measurable_space β„‚ := borel β„‚ instance complex.borel_space : borel_space β„‚ := ⟨rfl⟩ section metric_space variables [metric_space Ξ±] [measurable_space Ξ±] [opens_measurable_space Ξ±] variables [measurable_space Ξ²] {x : Ξ±} {Ξ΅ : ℝ} open metric lemma measurable_set_ball : measurable_set (metric.ball x Ξ΅) := metric.is_open_ball.measurable_set lemma measurable_set_closed_ball : measurable_set (metric.closed_ball x Ξ΅) := metric.is_closed_ball.measurable_set lemma measurable_inf_dist {s : set Ξ±} : measurable (Ξ» x, inf_dist x s) := (continuous_inf_dist_pt s).measurable lemma measurable.inf_dist {f : Ξ² β†’ Ξ±} (hf : measurable f) {s : set Ξ±} : measurable (Ξ» x, inf_dist (f x) s) := measurable_inf_dist.comp hf lemma measurable_inf_nndist {s : set Ξ±} : measurable (Ξ» x, inf_nndist x s) := (continuous_inf_nndist_pt s).measurable lemma measurable.inf_nndist {f : Ξ² β†’ Ξ±} (hf : measurable f) {s : set Ξ±} : measurable (Ξ» x, inf_nndist (f x) s) := measurable_inf_nndist.comp hf variables [second_countable_topology Ξ±] lemma measurable_dist : measurable (Ξ» p : Ξ± Γ— Ξ±, dist p.1 p.2) := continuous_dist.measurable lemma measurable.dist {f g : Ξ² β†’ Ξ±} (hf : measurable f) (hg : measurable g) : measurable (Ξ» b, dist (f b) (g b)) := (@continuous_dist Ξ± _).measurable2 hf hg lemma measurable_nndist : measurable (Ξ» p : Ξ± Γ— Ξ±, nndist p.1 p.2) := continuous_nndist.measurable lemma measurable.nndist {f g : Ξ² β†’ Ξ±} (hf : measurable f) (hg : measurable g) : measurable (Ξ» b, nndist (f b) (g b)) := (@continuous_nndist Ξ± _).measurable2 hf hg end metric_space section emetric_space variables [emetric_space Ξ±] [measurable_space Ξ±] [opens_measurable_space Ξ±] variables [measurable_space Ξ²] {x : Ξ±} {Ξ΅ : ℝβ‰₯0∞} open emetric lemma measurable_set_eball : measurable_set (emetric.ball x Ξ΅) := emetric.is_open_ball.measurable_set lemma measurable_edist_right : measurable (edist x) := (continuous_const.edist continuous_id).measurable lemma measurable_edist_left : measurable (Ξ» y, edist y x) := (continuous_id.edist continuous_const).measurable lemma measurable_inf_edist {s : set Ξ±} : measurable (Ξ» x, inf_edist x s) := continuous_inf_edist.measurable lemma measurable.inf_edist {f : Ξ² β†’ Ξ±} (hf : measurable f) {s : set Ξ±} : measurable (Ξ» x, inf_edist (f x) s) := measurable_inf_edist.comp hf variables [second_countable_topology Ξ±] lemma measurable_edist : measurable (Ξ» p : Ξ± Γ— Ξ±, edist p.1 p.2) := continuous_edist.measurable lemma measurable.edist {f g : Ξ² β†’ Ξ±} (hf : measurable f) (hg : measurable g) : measurable (Ξ» b, edist (f b) (g b)) := (@continuous_edist Ξ± _).measurable2 hf hg lemma ae_measurable.edist {f g : Ξ² β†’ Ξ±} {ΞΌ : measure Ξ²} (hf : ae_measurable f ΞΌ) (hg : ae_measurable g ΞΌ) : ae_measurable (Ξ» a, edist (f a) (g a)) ΞΌ := (@continuous_edist Ξ± _).ae_measurable2 hf hg end emetric_space namespace real open measurable_space measure_theory lemma borel_eq_generate_from_Ioo_rat : borel ℝ = generate_from (⋃(a b : β„š) (h : a < b), {Ioo a b}) := borel_eq_generate_from_of_subbasis is_topological_basis_Ioo_rat.2.2 lemma measure_ext_Ioo_rat {ΞΌ Ξ½ : measure ℝ} [locally_finite_measure ΞΌ] (h : βˆ€ a b : β„š, ΞΌ (Ioo a b) = Ξ½ (Ioo a b)) : ΞΌ = Ξ½ := begin refine measure.ext_of_generate_from_of_cover_subset borel_eq_generate_from_Ioo_rat _ (subset.refl _) _ _ _ _, { simp only [is_pi_system, mem_Union, mem_singleton_iff], rintros _ _ ⟨a₁, b₁, h₁, rfl⟩ ⟨aβ‚‚, bβ‚‚, hβ‚‚, rfl⟩ ne, simp only [Ioo_inter_Ioo, sup_eq_max, inf_eq_min, ← rat.cast_max, ← rat.cast_min, nonempty_Ioo] at ne ⊒, refine ⟨_, _, _, rfl⟩, assumption_mod_cast }, { exact countable_Union (Ξ» a, (countable_encodable _).bUnion $ Ξ» _ _, countable_singleton _) }, { exact is_topological_basis_Ioo_rat.2.1 }, { simp only [mem_Union, mem_singleton_iff], rintros _ ⟨a, b, h, rfl⟩, refine (measure_mono subset_closure).trans_lt _, rw [closure_Ioo], exacts [compact_Icc.finite_measure, rat.cast_lt.2 h] }, { simp only [mem_Union, mem_singleton_iff], rintros _ ⟨a, b, hab, rfl⟩, exact h a b } end lemma borel_eq_generate_from_Iio_rat : borel ℝ = generate_from (⋃ a : β„š, {Iio a}) := begin let g, swap, apply le_antisymm (_ : _ ≀ g) (measurable_space.generate_from_le (Ξ» t, _)), { rw borel_eq_generate_from_Ioo_rat, refine generate_from_le (Ξ» t, _), simp only [mem_Union], rintro ⟨a, b, h, H⟩, rw [mem_singleton_iff.1 H], rw (set.ext (Ξ» x, _) : Ioo (a : ℝ) b = (⋃c>a, (Iio c)ᢜ) ∩ Iio b), { have hg : βˆ€ q : β„š, g.measurable_set' (Iio q) := Ξ» q, generate_measurable.basic (Iio q) (by { simp, exact ⟨_, rfl⟩ }), refine @measurable_set.inter _ g _ _ _ (hg _), refine @measurable_set.bUnion _ _ g _ _ (countable_encodable _) (Ξ» c h, _), exact @measurable_set.compl _ _ g (hg _) }, { suffices : x < ↑b β†’ (↑a < x ↔ βˆƒ (i : β„š), a < i ∧ ↑i ≀ x), by simpa, refine Ξ» _, ⟨λ h, _, Ξ» ⟨i, hai, hix⟩, (rat.cast_lt.2 hai).trans_le hix⟩, rcases exists_rat_btwn h with ⟨c, ac, cx⟩, exact ⟨c, rat.cast_lt.1 ac, cx.le⟩ } }, { simp, rintro r rfl, exact is_open_Iio.measurable_set } end end real variable [measurable_space Ξ±] lemma measurable.sub_nnreal {f g : Ξ± β†’ ℝβ‰₯0} : measurable f β†’ measurable g β†’ measurable (Ξ» a, f a - g a) := (@continuous_sub ℝβ‰₯0 _ _ _).measurable2 lemma measurable.nnreal_of_real {f : Ξ± β†’ ℝ} (hf : measurable f) : measurable (Ξ» x, nnreal.of_real (f x)) := nnreal.continuous_of_real.measurable.comp hf lemma nnreal.measurable_coe : measurable (coe : ℝβ‰₯0 β†’ ℝ) := nnreal.continuous_coe.measurable lemma measurable.nnreal_coe {f : Ξ± β†’ ℝβ‰₯0} (hf : measurable f) : measurable (Ξ» x, (f x : ℝ)) := nnreal.measurable_coe.comp hf lemma measurable.ennreal_coe {f : Ξ± β†’ ℝβ‰₯0} (hf : measurable f) : measurable (Ξ» x, (f x : ℝβ‰₯0∞)) := ennreal.continuous_coe.measurable.comp hf lemma ae_measurable.ennreal_coe {f : Ξ± β†’ ℝβ‰₯0} {ΞΌ : measure Ξ±} (hf : ae_measurable f ΞΌ) : ae_measurable (Ξ» x, (f x : ℝβ‰₯0∞)) ΞΌ := ennreal.continuous_coe.measurable.comp_ae_measurable hf lemma measurable.ennreal_of_real {f : Ξ± β†’ ℝ} (hf : measurable f) : measurable (Ξ» x, ennreal.of_real (f x)) := ennreal.continuous_of_real.measurable.comp hf /-- The set of finite `ℝβ‰₯0∞` numbers is `measurable_equiv` to `ℝβ‰₯0`. -/ def measurable_equiv.ennreal_equiv_nnreal : {r : ℝβ‰₯0∞ | r β‰  ∞} ≃ᡐ ℝβ‰₯0 := ennreal.ne_top_homeomorph_nnreal.to_measurable_equiv namespace ennreal lemma measurable_coe : measurable (coe : ℝβ‰₯0 β†’ ℝβ‰₯0∞) := measurable_id.ennreal_coe lemma measurable_of_measurable_nnreal {f : ℝβ‰₯0∞ β†’ Ξ±} (h : measurable (Ξ» p : ℝβ‰₯0, f p)) : measurable f := measurable_of_measurable_on_compl_singleton ∞ (measurable_equiv.ennreal_equiv_nnreal.symm.measurable_coe_iff.1 h) /-- `ℝβ‰₯0∞` is `measurable_equiv` to `ℝβ‰₯0 βŠ• unit`. -/ def ennreal_equiv_sum : ℝβ‰₯0∞ ≃ᡐ ℝβ‰₯0 βŠ• unit := { measurable_to_fun := measurable_of_measurable_nnreal measurable_inl, measurable_inv_fun := measurable_sum measurable_coe (@measurable_const ℝβ‰₯0∞ unit _ _ ∞), .. equiv.option_equiv_sum_punit ℝβ‰₯0 } open function (uncurry) lemma measurable_of_measurable_nnreal_prod [measurable_space Ξ²] [measurable_space Ξ³] {f : ℝβ‰₯0∞ Γ— Ξ² β†’ Ξ³} (H₁ : measurable (Ξ» p : ℝβ‰₯0 Γ— Ξ², f (p.1, p.2))) (Hβ‚‚ : measurable (Ξ» x, f (∞, x))) : measurable f := let e : ℝβ‰₯0∞ Γ— Ξ² ≃ᡐ ℝβ‰₯0 Γ— Ξ² βŠ• unit Γ— Ξ² := (ennreal_equiv_sum.prod_congr (measurable_equiv.refl Ξ²)).trans (measurable_equiv.sum_prod_distrib _ _ _) in e.symm.measurable_coe_iff.1 $ measurable_sum H₁ (Hβ‚‚.comp measurable_id.snd) lemma measurable_of_measurable_nnreal_nnreal [measurable_space Ξ²] {f : ℝβ‰₯0∞ Γ— ℝβ‰₯0∞ β†’ Ξ²} (h₁ : measurable (Ξ» p : ℝβ‰₯0 Γ— ℝβ‰₯0, f (p.1, p.2))) (hβ‚‚ : measurable (Ξ» r : ℝβ‰₯0, f (∞, r))) (h₃ : measurable (Ξ» r : ℝβ‰₯0, f (r, ∞))) : measurable f := measurable_of_measurable_nnreal_prod (measurable_swap_iff.1 $ measurable_of_measurable_nnreal_prod (h₁.comp measurable_swap) h₃) (measurable_of_measurable_nnreal hβ‚‚) lemma measurable_of_real : measurable ennreal.of_real := ennreal.continuous_of_real.measurable lemma measurable_to_real : measurable ennreal.to_real := ennreal.measurable_of_measurable_nnreal nnreal.measurable_coe lemma measurable_to_nnreal : measurable ennreal.to_nnreal := ennreal.measurable_of_measurable_nnreal measurable_id lemma measurable_mul : measurable (Ξ» p : ℝβ‰₯0∞ Γ— ℝβ‰₯0∞, p.1 * p.2) := begin apply measurable_of_measurable_nnreal_nnreal, { simp only [← ennreal.coe_mul, measurable_mul.ennreal_coe] }, { simp only [ennreal.top_mul, ennreal.coe_eq_zero], exact measurable_const.piecewise (measurable_set_singleton _) measurable_const }, { simp only [ennreal.mul_top, ennreal.coe_eq_zero], exact measurable_const.piecewise (measurable_set_singleton _) measurable_const } end lemma measurable_sub : measurable (Ξ» p : ℝβ‰₯0∞ Γ— ℝβ‰₯0∞, p.1 - p.2) := by apply measurable_of_measurable_nnreal_nnreal; simp [← ennreal.coe_sub, continuous_sub.measurable.ennreal_coe] lemma measurable_inv : measurable (has_inv.inv : ℝβ‰₯0∞ β†’ ℝβ‰₯0∞) := ennreal.continuous_inv.measurable lemma measurable_div : measurable (Ξ» p : ℝβ‰₯0∞ Γ— ℝβ‰₯0∞, p.1 / p.2) := ennreal.measurable_mul.comp $ measurable_fst.prod_mk $ ennreal.measurable_inv.comp measurable_snd end ennreal lemma measurable.to_nnreal {f : Ξ± β†’ ℝβ‰₯0∞} (hf : measurable f) : measurable (Ξ» x, (f x).to_nnreal) := ennreal.measurable_to_nnreal.comp hf lemma measurable_ennreal_coe_iff {f : Ξ± β†’ ℝβ‰₯0} : measurable (Ξ» x, (f x : ℝβ‰₯0∞)) ↔ measurable f := ⟨λ h, h.to_nnreal, Ξ» h, h.ennreal_coe⟩ lemma measurable.to_real {f : Ξ± β†’ ℝβ‰₯0∞} (hf : measurable f) : measurable (Ξ» x, ennreal.to_real (f x)) := ennreal.measurable_to_real.comp hf lemma ae_measurable.to_real {f : Ξ± β†’ ℝβ‰₯0∞} {ΞΌ : measure Ξ±} (hf : ae_measurable f ΞΌ) : ae_measurable (Ξ» x, ennreal.to_real (f x)) ΞΌ := ennreal.measurable_to_real.comp_ae_measurable hf lemma measurable.ennreal_mul {f g : Ξ± β†’ ℝβ‰₯0∞} (hf : measurable f) (hg : measurable g) : measurable (Ξ» a, f a * g a) := ennreal.measurable_mul.comp (hf.prod_mk hg) lemma ae_measurable.ennreal_mul {f g : Ξ± β†’ ℝβ‰₯0∞} {ΞΌ : measure Ξ±} (hf : ae_measurable f ΞΌ) (hg : ae_measurable g ΞΌ) : ae_measurable (Ξ» a, f a * g a) ΞΌ := ennreal.measurable_mul.comp_ae_measurable (hf.prod_mk hg) lemma measurable.ennreal_sub {f g : Ξ± β†’ ℝβ‰₯0∞} (hf : measurable f) (hg : measurable g) : measurable (Ξ» a, f a - g a) := ennreal.measurable_sub.comp (hf.prod_mk hg) /-- note: `ℝβ‰₯0∞` can probably be generalized in a future version of this lemma. -/ lemma measurable.ennreal_tsum {ΞΉ} [encodable ΞΉ] {f : ΞΉ β†’ Ξ± β†’ ℝβ‰₯0∞} (h : βˆ€ i, measurable (f i)) : measurable (Ξ» x, βˆ‘' i, f i x) := by { simp_rw [ennreal.tsum_eq_supr_sum], apply measurable_supr, exact Ξ» s, s.measurable_sum h } lemma ae_measurable.ennreal_tsum {ΞΉ} [encodable ΞΉ] {f : ΞΉ β†’ Ξ± β†’ ℝβ‰₯0∞} {ΞΌ : measure Ξ±} (h : βˆ€ i, ae_measurable (f i) ΞΌ) : ae_measurable (Ξ» x, βˆ‘' i, f i x) ΞΌ := by { simp_rw [ennreal.tsum_eq_supr_sum], apply ae_measurable_supr, exact Ξ» s, finset.ae_measurable_sum s h } lemma measurable.ennreal_inv {f : Ξ± β†’ ℝβ‰₯0∞} (hf : measurable f) : measurable (Ξ» a, (f a)⁻¹) := ennreal.measurable_inv.comp hf lemma measurable.ennreal_div {f g : Ξ± β†’ ℝβ‰₯0∞} (hf : measurable f) (hg : measurable g) : measurable (Ξ» a, f a / g a) := ennreal.measurable_div.comp $ hf.prod_mk hg section normed_group variables [normed_group Ξ±] [opens_measurable_space Ξ±] [measurable_space Ξ²] lemma measurable_norm : measurable (norm : Ξ± β†’ ℝ) := continuous_norm.measurable lemma measurable.norm {f : Ξ² β†’ Ξ±} (hf : measurable f) : measurable (Ξ» a, norm (f a)) := measurable_norm.comp hf lemma ae_measurable.norm {f : Ξ² β†’ Ξ±} {ΞΌ : measure Ξ²} (hf : ae_measurable f ΞΌ) : ae_measurable (Ξ» a, norm (f a)) ΞΌ := measurable_norm.comp_ae_measurable hf lemma measurable_nnnorm : measurable (nnnorm : Ξ± β†’ ℝβ‰₯0) := continuous_nnnorm.measurable lemma measurable.nnnorm {f : Ξ² β†’ Ξ±} (hf : measurable f) : measurable (Ξ» a, nnnorm (f a)) := measurable_nnnorm.comp hf lemma ae_measurable.nnnorm {f : Ξ² β†’ Ξ±} {ΞΌ : measure Ξ²} (hf : ae_measurable f ΞΌ) : ae_measurable (Ξ» a, nnnorm (f a)) ΞΌ := measurable_nnnorm.comp_ae_measurable hf lemma measurable_ennnorm : measurable (Ξ» x : Ξ±, (nnnorm x : ℝβ‰₯0∞)) := measurable_nnnorm.ennreal_coe lemma measurable.ennnorm {f : Ξ² β†’ Ξ±} (hf : measurable f) : measurable (Ξ» a, (nnnorm (f a) : ℝβ‰₯0∞)) := hf.nnnorm.ennreal_coe lemma ae_measurable.ennnorm {f : Ξ² β†’ Ξ±} {ΞΌ : measure Ξ²} (hf : ae_measurable f ΞΌ) : ae_measurable (Ξ» a, (nnnorm (f a) : ℝβ‰₯0∞)) ΞΌ := measurable_ennnorm.comp_ae_measurable hf end normed_group section limits variables [measurable_space Ξ²] [metric_space Ξ²] [borel_space Ξ²] open metric /-- A limit (over a general filter) of measurable `ℝβ‰₯0` valued functions is measurable. The assumption `hs` can be dropped using `filter.is_countably_generated.has_antimono_basis`, but we don't need that case yet. -/ lemma measurable_of_tendsto_nnreal' {ΞΉ ΞΉ'} {f : ΞΉ β†’ Ξ± β†’ ℝβ‰₯0} {g : Ξ± β†’ ℝβ‰₯0} (u : filter ΞΉ) [ne_bot u] (hf : βˆ€ i, measurable (f i)) (lim : tendsto f u (𝓝 g)) {p : ΞΉ' β†’ Prop} {s : ΞΉ' β†’ set ΞΉ} (hu : u.has_countable_basis p s) (hs : βˆ€ i, (s i).countable) : measurable g := begin rw [tendsto_pi] at lim, rw [← measurable_ennreal_coe_iff], have : βˆ€ x, liminf u (Ξ» n, (f n x : ℝβ‰₯0∞)) = (g x : ℝβ‰₯0∞) := Ξ» x, ((ennreal.continuous_coe.tendsto (g x)).comp (lim x)).liminf_eq, simp_rw [← this], show measurable (Ξ» x, liminf u (Ξ» n, (f n x : ℝβ‰₯0∞))), exact measurable_liminf' (Ξ» i, (hf i).ennreal_coe) hu hs, end /-- A sequential limit of measurable `ℝβ‰₯0` valued functions is measurable. -/ lemma measurable_of_tendsto_nnreal {f : β„• β†’ Ξ± β†’ ℝβ‰₯0} {g : Ξ± β†’ ℝβ‰₯0} (hf : βˆ€ i, measurable (f i)) (lim : tendsto f at_top (𝓝 g)) : measurable g := measurable_of_tendsto_nnreal' at_top hf lim at_top_countable_basis (Ξ» i, countable_encodable _) /-- A limit (over a general filter) of measurable functions valued in a metric space is measurable. The assumption `hs` can be dropped using `filter.is_countably_generated.has_antimono_basis`, but we don't need that case yet. -/ lemma measurable_of_tendsto_metric' {ΞΉ ΞΉ'} {f : ΞΉ β†’ Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ²} (u : filter ΞΉ) [ne_bot u] (hf : βˆ€ i, measurable (f i)) (lim : tendsto f u (𝓝 g)) {p : ΞΉ' β†’ Prop} {s : ΞΉ' β†’ set ΞΉ} (hu : u.has_countable_basis p s) (hs : βˆ€ i, (s i).countable) : measurable g := begin apply measurable_of_is_closed', intros s h1s h2s h3s, have : measurable (Ξ» x, inf_nndist (g x) s), { refine measurable_of_tendsto_nnreal' u (Ξ» i, (hf i).inf_nndist) _ hu hs, swap, rw [tendsto_pi], rw [tendsto_pi] at lim, intro x, exact ((continuous_inf_nndist_pt s).tendsto (g x)).comp (lim x) }, have h4s : g ⁻¹' s = (Ξ» x, inf_nndist (g x) s) ⁻¹' {0}, { ext x, simp [h1s, ← mem_iff_inf_dist_zero_of_closed h1s h2s, ← nnreal.coe_eq_zero] }, rw [h4s], exact this (measurable_set_singleton 0), end /-- A sequential limit of measurable functions valued in a metric space is measurable. -/ lemma measurable_of_tendsto_metric {f : β„• β†’ Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ²} (hf : βˆ€ i, measurable (f i)) (lim : tendsto f at_top (𝓝 g)) : measurable g := measurable_of_tendsto_metric' at_top hf lim at_top_countable_basis (Ξ» i, countable_encodable _) lemma ae_measurable_of_tendsto_metric_ae {ΞΌ : measure Ξ±} {f : β„• β†’ Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ²} (hf : βˆ€ n, ae_measurable (f n) ΞΌ) (h_ae_tendsto : βˆ€α΅ x βˆ‚ΞΌ, filter.at_top.tendsto (Ξ» n, f n x) (𝓝 (g x))) : ae_measurable g ΞΌ := begin let p : Ξ± β†’ (β„• β†’ Ξ²) β†’ Prop := Ξ» x f', filter.at_top.tendsto (Ξ» n, f' n) (𝓝 (g x)), let hp : βˆ€α΅ x βˆ‚ΞΌ, p x (Ξ» n, f n x), from h_ae_tendsto, let ae_seq_lim := Ξ» x, ite (x ∈ ae_seq_set hf p) (g x) (⟨f 0 x⟩ : nonempty Ξ²).some, refine ⟨ae_seq_lim, _, (ite_ae_eq_of_measure_compl_zero g (Ξ» x, (⟨f 0 x⟩ : nonempty Ξ²).some) (ae_seq_set hf p) (ae_seq.measure_compl_ae_seq_set_eq_zero hf hp)).symm⟩, refine measurable_of_tendsto_metric (@ae_seq.measurable Ξ± Ξ² _ _ _ f ΞΌ hf p) _, refine tendsto_pi.mpr (Ξ» x, _), simp_rw [ae_seq, ae_seq_lim], split_ifs with hx, { simp_rw ae_seq.mk_eq_fun_of_mem_ae_seq_set hf hx, exact @ae_seq.fun_prop_of_mem_ae_seq_set Ξ± Ξ² _ _ _ _ _ _ hf x hx, }, { exact tendsto_const_nhds, }, end lemma measurable_of_tendsto_metric_ae {ΞΌ : measure Ξ±} [ΞΌ.is_complete] {f : β„• β†’ Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ²} (hf : βˆ€ n, measurable (f n)) (h_ae_tendsto : βˆ€α΅ x βˆ‚ΞΌ, filter.at_top.tendsto (Ξ» n, f n x) (𝓝 (g x))) : measurable g := ae_measurable_iff_measurable.mp (ae_measurable_of_tendsto_metric_ae (Ξ» i, (hf i).ae_measurable) h_ae_tendsto) lemma measurable_limit_of_tendsto_metric_ae {ΞΌ : measure Ξ±} {f : β„• β†’ Ξ± β†’ Ξ²} (hf : βˆ€ n, ae_measurable (f n) ΞΌ) (h_ae_tendsto : βˆ€α΅ x βˆ‚ΞΌ, βˆƒ l : Ξ², filter.at_top.tendsto (Ξ» n, f n x) (𝓝 l)) : βˆƒ (f_lim : Ξ± β†’ Ξ²) (hf_lim_meas : measurable f_lim), βˆ€α΅ x βˆ‚ΞΌ, filter.at_top.tendsto (Ξ» n, f n x) (𝓝 (f_lim x)) := begin let p : Ξ± β†’ (β„• β†’ Ξ²) β†’ Prop := Ξ» x f', βˆƒ l : Ξ², filter.at_top.tendsto (Ξ» n, f' n) (𝓝 l), have hp_mem : βˆ€ x, x ∈ ae_seq_set hf p β†’ p x (Ξ» n, f n x), from Ξ» x hx, ae_seq.fun_prop_of_mem_ae_seq_set hf hx, have hΞΌ_compl : ΞΌ (ae_seq_set hf p)ᢜ = 0, from ae_seq.measure_compl_ae_seq_set_eq_zero hf h_ae_tendsto, let f_lim : Ξ± β†’ Ξ² := Ξ» x, dite (x ∈ ae_seq_set hf p) (Ξ» h, (hp_mem x h).some) (Ξ» h, (⟨f 0 x⟩ : nonempty Ξ²).some), have hf_lim_conv : βˆ€ x, x ∈ ae_seq_set hf p β†’ filter.at_top.tendsto (Ξ» n, f n x) (𝓝 (f_lim x)), { intros x hx_conv, simp only [f_lim, hx_conv, dif_pos], exact (hp_mem x hx_conv).some_spec, }, have hf_lim : βˆ€ x, filter.at_top.tendsto (Ξ» n, ae_seq hf p n x) (𝓝 (f_lim x)), { intros x, simp only [f_lim, ae_seq], split_ifs, { rw funext (Ξ» n, ae_seq.mk_eq_fun_of_mem_ae_seq_set hf h n), exact (hp_mem x h).some_spec, }, { exact tendsto_const_nhds, }, }, have h_ae_tendsto_f_lim : βˆ€α΅ x βˆ‚ΞΌ, filter.at_top.tendsto (Ξ» n, f n x) (𝓝 (f_lim x)), { refine le_antisymm (le_of_eq (measure_mono_null _ hΞΌ_compl)) (zero_le _), exact set.compl_subset_compl.mpr (Ξ» x hx, hf_lim_conv x hx), }, have h_f_lim_meas : measurable f_lim, from measurable_of_tendsto_metric (ae_seq.measurable hf p) (tendsto_pi.mpr (Ξ» x, hf_lim x)), exact ⟨f_lim, h_f_lim_meas, h_ae_tendsto_f_lim⟩, end end limits namespace continuous_linear_map variables {π•œ : Type*} [normed_field π•œ] variables {E : Type*} [normed_group E] [normed_space π•œ E] [measurable_space E] variables [opens_measurable_space E] variables {F : Type*} [normed_group F] [normed_space π•œ F] [measurable_space F] [borel_space F] protected lemma measurable (L : E β†’L[π•œ] F) : measurable L := L.continuous.measurable lemma measurable_comp (L : E β†’L[π•œ] F) {Ο† : Ξ± β†’ E} (Ο†_meas : measurable Ο†) : measurable (Ξ» (a : Ξ±), L (Ο† a)) := L.measurable.comp Ο†_meas end continuous_linear_map section normed_space variables {π•œ : Type*} [nondiscrete_normed_field π•œ] [complete_space π•œ] [measurable_space π•œ] variables [borel_space π•œ] variables {E : Type*} [normed_group E] [normed_space π•œ E] [measurable_space E] [borel_space E] lemma measurable_smul_const {f : Ξ± β†’ π•œ} {c : E} (hc : c β‰  0) : measurable (Ξ» x, f x β€’ c) ↔ measurable f := measurable_comp_iff_of_closed_embedding (Ξ» y : π•œ, y β€’ c) (closed_embedding_smul_left hc) lemma ae_measurable_smul_const {f : Ξ± β†’ π•œ} {ΞΌ : measure Ξ±} {c : E} (hc : c β‰  0) : ae_measurable (Ξ» x, f x β€’ c) ΞΌ ↔ ae_measurable f ΞΌ := ae_measurable_comp_iff_of_closed_embedding (Ξ» y : π•œ, y β€’ c) (closed_embedding_smul_left hc) end normed_space namespace measure_theory namespace measure variables [topological_space Ξ±] {ΞΌ : measure Ξ±} /-- A measure `ΞΌ` is regular if - it is finite on all compact sets; - it is outer regular: `ΞΌ(A) = inf { ΞΌ(U) | A βŠ† U open }` for `A` measurable; - it is inner regular: `ΞΌ(U) = sup { ΞΌ(K) | K βŠ† U compact }` for `U` open. -/ structure regular (ΞΌ : measure Ξ±) : Prop := (lt_top_of_is_compact : βˆ€ {{K : set Ξ±}}, is_compact K β†’ ΞΌ K < ∞) (outer_regular : βˆ€ {{A : set Ξ±}}, measurable_set A β†’ (β¨… (U : set Ξ±) (h : is_open U) (h2 : A βŠ† U), ΞΌ U) ≀ ΞΌ A) (inner_regular : βˆ€ {{U : set Ξ±}}, is_open U β†’ ΞΌ U ≀ ⨆ (K : set Ξ±) (h : is_compact K) (h2 : K βŠ† U), ΞΌ K) namespace regular lemma outer_regular_eq (hΞΌ : ΞΌ.regular) {{A : set Ξ±}} (hA : measurable_set A) : (β¨… (U : set Ξ±) (h : is_open U) (h2 : A βŠ† U), ΞΌ U) = ΞΌ A := le_antisymm (hΞΌ.outer_regular hA) $ le_infi $ Ξ» s, le_infi $ Ξ» hs, le_infi $ Ξ» h2s, ΞΌ.mono h2s lemma inner_regular_eq (hΞΌ : ΞΌ.regular) {{U : set Ξ±}} (hU : is_open U) : (⨆ (K : set Ξ±) (h : is_compact K) (h2 : K βŠ† U), ΞΌ K) = ΞΌ U := le_antisymm (supr_le $ Ξ» s, supr_le $ Ξ» hs, supr_le $ Ξ» h2s, ΞΌ.mono h2s) (hΞΌ.inner_regular hU) lemma exists_compact_not_null (hΞΌ : regular ΞΌ) : (βˆƒ K, is_compact K ∧ ΞΌ K β‰  0) ↔ ΞΌ β‰  0 := by simp_rw [ne.def, ← measure_univ_eq_zero, ← hΞΌ.inner_regular_eq is_open_univ, ennreal.supr_eq_zero, not_forall, exists_prop, subset_univ, true_and] protected lemma map [opens_measurable_space Ξ±] [measurable_space Ξ²] [topological_space Ξ²] [t2_space Ξ²] [borel_space Ξ²] (hΞΌ : ΞΌ.regular) (f : Ξ± β‰ƒβ‚œ Ξ²) : (measure.map f ΞΌ).regular := begin have hf := f.continuous.measurable, have h2f := f.to_equiv.injective.preimage_surjective, have h3f := f.to_equiv.surjective, split, { intros K hK, rw [map_apply hf hK.measurable_set], apply hΞΌ.lt_top_of_is_compact, rwa f.compact_preimage }, { intros A hA, rw [map_apply hf hA, ← hΞΌ.outer_regular_eq (hf hA)], refine le_of_eq _, apply infi_congr (preimage f) h2f, intro U, apply infi_congr_Prop f.is_open_preimage, intro hU, apply infi_congr_Prop h3f.preimage_subset_preimage_iff, intro h2U, rw [map_apply hf hU.measurable_set], }, { intros U hU, rw [map_apply hf hU.measurable_set, ← hΞΌ.inner_regular_eq (hU.preimage f.continuous)], refine ge_of_eq _, apply supr_congr (preimage f) h2f, intro K, apply supr_congr_Prop f.compact_preimage, intro hK, apply supr_congr_Prop h3f.preimage_subset_preimage_iff, intro h2U, rw [map_apply hf hK.measurable_set] } end protected lemma smul (hΞΌ : ΞΌ.regular) {x : ℝβ‰₯0∞} (hx : x < ∞) : (x β€’ ΞΌ).regular := begin split, { intros K hK, exact ennreal.mul_lt_top hx (hΞΌ.lt_top_of_is_compact hK) }, { intros A hA, rw [coe_smul], refine le_trans _ (ennreal.mul_left_mono $ hΞΌ.outer_regular hA), simp only [infi_and'], simp only [infi_subtype'], haveI : nonempty {s : set Ξ± // is_open s ∧ A βŠ† s} := ⟨⟨set.univ, is_open_univ, subset_univ _⟩⟩, rw [ennreal.mul_infi], refl', exact ne_of_lt hx }, { intros U hU, rw [coe_smul], refine le_trans (ennreal.mul_left_mono $ hΞΌ.inner_regular hU) _, simp only [supr_and'], simp only [supr_subtype'], rw [ennreal.mul_supr], refl' } end /-- A regular measure in a Οƒ-compact space is Οƒ-finite. -/ protected lemma sigma_finite [opens_measurable_space Ξ±] [t2_space Ξ±] [sigma_compact_space Ξ±] (hΞΌ : regular ΞΌ) : sigma_finite ΞΌ := ⟨⟨{ set := compact_covering Ξ±, set_mem := Ξ» n, (is_compact_compact_covering Ξ± n).measurable_set, finite := Ξ» n, hΞΌ.lt_top_of_is_compact $ is_compact_compact_covering Ξ± n, spanning := Union_compact_covering Ξ± }⟩⟩ end regular end measure end measure_theory lemma is_compact.measure_lt_top_of_nhds_within [topological_space Ξ±] {s : set Ξ±} {ΞΌ : measure Ξ±} (h : is_compact s) (hΞΌ : βˆ€ x ∈ s, ΞΌ.finite_at_filter (𝓝[s] x)) : ΞΌ s < ∞ := is_compact.induction_on h (by simp) (Ξ» s t hst ht, (measure_mono hst).trans_lt ht) (Ξ» s t hs ht, (measure_union_le s t).trans_lt (ennreal.add_lt_top.2 ⟨hs, ht⟩)) hΞΌ lemma is_compact.measure_lt_top [topological_space Ξ±] {s : set Ξ±} {ΞΌ : measure Ξ±} [locally_finite_measure ΞΌ] (h : is_compact s) : ΞΌ s < ∞ := h.measure_lt_top_of_nhds_within $ Ξ» x hx, ΞΌ.finite_at_nhds_within _ _
d0690737112ac35587aa06174dda7dbc316053aa
22e97a5d648fc451e25a06c668dc03ac7ed7bc25
/src/data/nat/basic.lean
5f8cdac840a51a362d983b311d9dc7057d029bbb
[ "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
64,490
lean
/- Copyright (c) 2014 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Leonardo de Moura, Jeremy Avigad, Mario Carneiro -/ import algebra.ordered_ring import algebra.order_functions import init_.data.nat.lemmas /-! # Basic operations on the natural numbers This files has some basic lemmas about natural numbers, definition of the `choice` function, and extra recursors: * `le_rec_on`, `le_induction`: recursion and induction principles starting at non-zero numbers. * `decreasing_induction` : recursion growing downwards. * `strong_rec'` : recursion based on strong inequalities. -/ universes u v instance : canonically_ordered_comm_semiring β„• := { le_iff_exists_add := assume a b, ⟨assume h, let ⟨c, hc⟩ := nat.le.dest h in ⟨c, hc.symm⟩, assume ⟨c, hc⟩, hc.symm β–Έ nat.le_add_right _ _⟩, zero_ne_one := ne_of_lt zero_lt_one, mul_eq_zero_iff := assume a b, iff.intro nat.eq_zero_of_mul_eq_zero (by simp [or_imp_distrib] {contextual := tt}), bot := 0, bot_le := nat.zero_le, .. (infer_instance : ordered_add_comm_monoid β„•), .. (infer_instance : linear_ordered_semiring β„•), .. (infer_instance : comm_semiring β„•) } namespace nat variables {m n k : β„•} -- Sometimes a bare `nat.add` or similar appears as a consequence of unfolding -- during pattern matching. These lemmas package them back up as typeclass -- mediated operations. @[simp] theorem add_def {a b : β„•} : nat.add a b = a + b := rfl @[simp] theorem mul_def {a b : β„•} : nat.mul a b = a * b := rfl attribute [simp] nat.add_sub_cancel nat.add_sub_cancel_left attribute [simp] nat.sub_self @[simp] lemma succ_pos' {n : β„•} : 0 < succ n := succ_pos n theorem succ_inj' {n m : β„•} : succ n = succ m ↔ n = m := ⟨succ_inj, congr_arg _⟩ theorem succ_le_succ_iff {m n : β„•} : succ m ≀ succ n ↔ m ≀ n := ⟨le_of_succ_le_succ, succ_le_succ⟩ lemma zero_max {m : nat} : max 0 m = m := max_eq_right (zero_le _) theorem max_succ_succ {m n : β„•} : max (succ m) (succ n) = succ (max m n) := begin by_cases h1 : m ≀ n, rw [max_eq_right h1, max_eq_right (succ_le_succ h1)], { rw not_le at h1, have h2 := le_of_lt h1, rw [max_eq_left h2, max_eq_left (succ_le_succ h2)] } end lemma not_succ_lt_self {n : β„•} : Β¬succ n < n := not_lt_of_ge (nat.le_succ _) theorem lt_succ_iff {m n : β„•} : m < succ n ↔ m ≀ n := succ_le_succ_iff lemma succ_le_iff {m n : β„•} : succ m ≀ n ↔ m < n := ⟨lt_of_succ_le, succ_le_of_lt⟩ lemma lt_iff_add_one_le {m n : β„•} : m < n ↔ m + 1 ≀ n := by rw succ_le_iff -- Just a restatement of `nat.lt_succ_iff` using `+1`. lemma lt_add_one_iff {a b : β„•} : a < b + 1 ↔ a ≀ b := lt_succ_iff -- A flipped version of `lt_add_one_iff`. lemma lt_one_add_iff {a b : β„•} : a < 1 + b ↔ a ≀ b := by simp only [add_comm, lt_succ_iff] -- This is true reflexively, by the definition of `≀` on β„•, -- but it's still useful to have, to convince Lean to change the syntactic type. lemma add_one_le_iff {a b : β„•} : a + 1 ≀ b ↔ a < b := iff.refl _ lemma one_add_le_iff {a b : β„•} : 1 + a ≀ b ↔ a < b := by simp only [add_comm, add_one_le_iff] theorem of_le_succ {n m : β„•} (H : n ≀ m.succ) : n ≀ m ∨ n = m.succ := (lt_or_eq_of_le H).imp le_of_lt_succ id /-- Recursion starting at a non-zero number: given a map `C k β†’ C (k+1)` for each `k`, there is a map from `C n` to each `C m`, `n ≀ m`. -/ @[elab_as_eliminator] def le_rec_on {C : β„• β†’ Sort u} {n : β„•} : Ξ  {m : β„•}, n ≀ m β†’ (Ξ  {k}, C k β†’ C (k+1)) β†’ C n β†’ C m | 0 H next x := eq.rec_on (eq_zero_of_le_zero H) x | (m+1) H next x := or.by_cases (of_le_succ H) (Ξ» h : n ≀ m, next $ le_rec_on h @next x) (Ξ» h : n = m + 1, eq.rec_on h x) theorem le_rec_on_self {C : β„• β†’ Sort u} {n} {h : n ≀ n} {next} (x : C n) : (le_rec_on h next x : C n) = x := by cases n; unfold le_rec_on or.by_cases; rw [dif_neg n.not_succ_le_self, dif_pos rfl] theorem le_rec_on_succ {C : β„• β†’ Sort u} {n m} (h1 : n ≀ m) {h2 : n ≀ m+1} {next} (x : C n) : (le_rec_on h2 @next x : C (m+1)) = next (le_rec_on h1 @next x : C m) := by conv { to_lhs, rw [le_rec_on, or.by_cases, dif_pos h1] } theorem le_rec_on_succ' {C : β„• β†’ Sort u} {n} {h : n ≀ n+1} {next} (x : C n) : (le_rec_on h next x : C (n+1)) = next x := by rw [le_rec_on_succ (le_refl n), le_rec_on_self] theorem le_rec_on_trans {C : β„• β†’ Sort u} {n m k} (hnm : n ≀ m) (hmk : m ≀ k) {next} (x : C n) : (le_rec_on (le_trans hnm hmk) @next x : C k) = le_rec_on hmk @next (le_rec_on hnm @next x) := begin induction hmk with k hmk ih, { rw le_rec_on_self }, rw [le_rec_on_succ (le_trans hnm hmk), ih, le_rec_on_succ] end theorem le_rec_on_succ_left {C : β„• β†’ Sort u} {n m} (h1 : n ≀ m) (h2 : n+1 ≀ m) {next : Ξ {{k}}, C k β†’ C (k+1)} (x : C n) : (le_rec_on h2 next (next x) : C m) = (le_rec_on h1 next x : C m) := begin rw [subsingleton.elim h1 (le_trans (le_succ n) h2), le_rec_on_trans (le_succ n) h2, le_rec_on_succ'] end theorem le_rec_on_injective {C : β„• β†’ Sort u} {n m} (hnm : n ≀ m) (next : Ξ  n, C n β†’ C (n+1)) (Hnext : βˆ€ n, function.injective (next n)) : function.injective (le_rec_on hnm next) := begin induction hnm with m hnm ih, { intros x y H, rwa [le_rec_on_self, le_rec_on_self] at H }, intros x y H, rw [le_rec_on_succ hnm, le_rec_on_succ hnm] at H, exact ih (Hnext _ H) end theorem le_rec_on_surjective {C : β„• β†’ Sort u} {n m} (hnm : n ≀ m) (next : Ξ  n, C n β†’ C (n+1)) (Hnext : βˆ€ n, function.surjective (next n)) : function.surjective (le_rec_on hnm next) := begin induction hnm with m hnm ih, { intros x, use x, rw le_rec_on_self }, intros x, rcases Hnext _ x with ⟨w, rfl⟩, rcases ih w with ⟨x, rfl⟩, use x, rw le_rec_on_succ end theorem pred_eq_of_eq_succ {m n : β„•} (H : m = n.succ) : m.pred = n := by simp [H] @[simp] lemma pred_eq_succ_iff {n m : β„•} : pred n = succ m ↔ n = m + 2 := by cases n; split; rintro ⟨⟩; refl theorem pred_sub (n m : β„•) : pred n - m = pred (n - m) := by rw [← sub_one, nat.sub_sub, one_add]; refl @[simp] lemma add_succ_sub_one (n m : β„•) : (n + succ m) - 1 = n + m := by rw [add_succ, succ_sub_one] @[simp] lemma succ_add_sub_one (n m : β„•) : (succ n + m) - 1 = n + m := by rw [succ_add, succ_sub_one] lemma pred_eq_sub_one (n : β„•) : pred n = n - 1 := rfl lemma one_le_of_lt {n m : β„•} (h : n < m) : 1 ≀ m := lt_of_le_of_lt (nat.zero_le _) h lemma le_pred_of_lt {n m : β„•} (h : m < n) : m ≀ n - 1 := nat.sub_le_sub_right h 1 lemma le_of_pred_lt {m n : β„•} : pred m < n β†’ m ≀ n := match m with | 0 := le_of_lt | m+1 := id end /-- This ensures that `simp` succeeds on `pred (n + 1) = n`. -/ @[simp] lemma pred_one_add (n : β„•) : pred (1 + n) = n := by rw [add_comm, add_one, pred_succ] theorem pos_iff_ne_zero : 0 < n ↔ n β‰  0 := ⟨ne_of_gt, nat.pos_of_ne_zero⟩ lemma one_lt_iff_ne_zero_and_ne_one : βˆ€ {n : β„•}, 1 < n ↔ n β‰  0 ∧ n β‰  1 | 0 := dec_trivial | 1 := dec_trivial | (n+2) := dec_trivial theorem eq_of_lt_succ_of_not_lt {a b : β„•} (h1 : a < b + 1) (h2 : Β¬ a < b) : a = b := have h3 : a ≀ b, from le_of_lt_succ h1, or.elim (eq_or_lt_of_not_lt h2) (Ξ» h, h) (Ξ» h, absurd h (not_lt_of_ge h3)) protected theorem le_sub_add (n m : β„•) : n ≀ n - m + m := or.elim (le_total n m) (assume : n ≀ m, begin rw [sub_eq_zero_of_le this, zero_add], exact this end) (assume : m ≀ n, begin rw (nat.sub_add_cancel this) end) theorem sub_add_eq_max (n m : β„•) : n - m + m = max n m := eq_max (nat.le_sub_add _ _) (le_add_left _ _) $ Ξ» k h₁ hβ‚‚, by rw ← nat.sub_add_cancel hβ‚‚; exact add_le_add_right (nat.sub_le_sub_right h₁ _) _ theorem add_sub_eq_max (n m : β„•) : n + (m - n) = max n m := by rw [add_comm, max_comm, sub_add_eq_max] theorem sub_add_min (n m : β„•) : n - m + min n m = n := (le_total n m).elim (Ξ» h, by rw [min_eq_left h, sub_eq_zero_of_le h, zero_add]) (Ξ» h, by rw [min_eq_right h, nat.sub_add_cancel h]) protected theorem add_sub_cancel' {n m : β„•} (h : m ≀ n) : m + (n - m) = n := by rw [add_comm, nat.sub_add_cancel h] protected theorem sub_eq_of_eq_add (h : k = m + n) : k - m = n := begin rw [h, nat.add_sub_cancel_left] end theorem sub_cancel {a b c : β„•} (h₁ : a ≀ b) (hβ‚‚ : a ≀ c) (w : b - a = c - a) : b = c := by rw [←nat.sub_add_cancel h₁, ←nat.sub_add_cancel hβ‚‚, w] lemma sub_sub_sub_cancel_right {a b c : β„•} (hβ‚‚ : c ≀ b) : (a - c) - (b - c) = a - b := by rw [nat.sub_sub, ←nat.add_sub_assoc hβ‚‚, nat.add_sub_cancel_left] lemma add_sub_cancel_right (n m k : β„•) : n + (m + k) - k = n + m := by { rw [nat.add_sub_assoc, nat.add_sub_cancel], apply k.le_add_left } protected lemma sub_add_eq_add_sub {a b c : β„•} (h : b ≀ a) : (a - b) + c = (a + c) - b := by rw [add_comm a, nat.add_sub_assoc h, add_comm] theorem sub_min (n m : β„•) : n - min n m = n - m := nat.sub_eq_of_eq_add $ by rw [add_comm, sub_add_min] theorem sub_sub_assoc {a b c : β„•} (h₁ : b ≀ a) (hβ‚‚ : c ≀ b) : a - (b - c) = a - b + c := (nat.sub_eq_iff_eq_add (le_trans (nat.sub_le _ _) h₁)).2 $ by rw [add_right_comm, add_assoc, nat.sub_add_cancel hβ‚‚, nat.sub_add_cancel h₁] protected theorem lt_of_sub_pos (h : 0 < n - m) : m < n := lt_of_not_ge (assume : n ≀ m, have n - m = 0, from sub_eq_zero_of_le this, begin rw this at h, exact lt_irrefl _ h end) protected theorem lt_of_sub_lt_sub_right : m - k < n - k β†’ m < n := lt_imp_lt_of_le_imp_le (Ξ» h, nat.sub_le_sub_right h _) protected theorem lt_of_sub_lt_sub_left : m - n < m - k β†’ k < n := lt_imp_lt_of_le_imp_le (nat.sub_le_sub_left _) protected theorem sub_lt_self (h₁ : 0 < m) (hβ‚‚ : 0 < n) : m - n < m := calc m - n = succ (pred m) - succ (pred n) : by rw [succ_pred_eq_of_pos h₁, succ_pred_eq_of_pos hβ‚‚] ... = pred m - pred n : by rw succ_sub_succ ... ≀ pred m : sub_le _ _ ... < succ (pred m) : lt_succ_self _ ... = m : succ_pred_eq_of_pos h₁ protected theorem le_sub_right_of_add_le (h : m + k ≀ n) : m ≀ n - k := by rw ← nat.add_sub_cancel m k; exact nat.sub_le_sub_right h k protected theorem le_sub_left_of_add_le (h : k + m ≀ n) : m ≀ n - k := nat.le_sub_right_of_add_le (by rwa add_comm at h) protected theorem lt_sub_right_of_add_lt (h : m + k < n) : m < n - k := lt_of_succ_le $ nat.le_sub_right_of_add_le $ by rw succ_add; exact succ_le_of_lt h protected theorem lt_sub_left_of_add_lt (h : k + m < n) : m < n - k := nat.lt_sub_right_of_add_lt (by rwa add_comm at h) protected theorem add_lt_of_lt_sub_right (h : m < n - k) : m + k < n := @nat.lt_of_sub_lt_sub_right _ _ k (by rwa nat.add_sub_cancel) protected theorem add_lt_of_lt_sub_left (h : m < n - k) : k + m < n := by rw add_comm; exact nat.add_lt_of_lt_sub_right h protected theorem le_add_of_sub_le_right : n - k ≀ m β†’ n ≀ m + k := le_imp_le_of_lt_imp_lt nat.lt_sub_right_of_add_lt protected theorem le_add_of_sub_le_left : n - k ≀ m β†’ n ≀ k + m := le_imp_le_of_lt_imp_lt nat.lt_sub_left_of_add_lt protected theorem lt_add_of_sub_lt_right : n - k < m β†’ n < m + k := lt_imp_lt_of_le_imp_le nat.le_sub_right_of_add_le protected theorem lt_add_of_sub_lt_left : n - k < m β†’ n < k + m := lt_imp_lt_of_le_imp_le nat.le_sub_left_of_add_le protected theorem sub_le_left_of_le_add : n ≀ k + m β†’ n - k ≀ m := le_imp_le_of_lt_imp_lt nat.add_lt_of_lt_sub_left protected theorem sub_le_right_of_le_add : n ≀ m + k β†’ n - k ≀ m := le_imp_le_of_lt_imp_lt nat.add_lt_of_lt_sub_right protected theorem sub_lt_left_iff_lt_add (H : n ≀ k) : k - n < m ↔ k < n + m := ⟨nat.lt_add_of_sub_lt_left, Ξ» h₁, have succ k ≀ n + m, from succ_le_of_lt h₁, have succ (k - n) ≀ m, from calc succ (k - n) = succ k - n : by rw (succ_sub H) ... ≀ n + m - n : nat.sub_le_sub_right this n ... = m : by rw nat.add_sub_cancel_left, lt_of_succ_le this⟩ protected theorem le_sub_left_iff_add_le (H : m ≀ k) : n ≀ k - m ↔ m + n ≀ k := le_iff_le_iff_lt_iff_lt.2 (nat.sub_lt_left_iff_lt_add H) protected theorem le_sub_right_iff_add_le (H : n ≀ k) : m ≀ k - n ↔ m + n ≀ k := by rw [nat.le_sub_left_iff_add_le H, add_comm] protected theorem lt_sub_left_iff_add_lt : n < k - m ↔ m + n < k := ⟨nat.add_lt_of_lt_sub_left, nat.lt_sub_left_of_add_lt⟩ protected theorem lt_sub_right_iff_add_lt : m < k - n ↔ m + n < k := by rw [nat.lt_sub_left_iff_add_lt, add_comm] theorem sub_le_left_iff_le_add : m - n ≀ k ↔ m ≀ n + k := le_iff_le_iff_lt_iff_lt.2 nat.lt_sub_left_iff_add_lt theorem sub_le_right_iff_le_add : m - k ≀ n ↔ m ≀ n + k := by rw [nat.sub_le_left_iff_le_add, add_comm] protected theorem sub_lt_right_iff_lt_add (H : k ≀ m) : m - k < n ↔ m < n + k := by rw [nat.sub_lt_left_iff_lt_add H, add_comm] protected theorem sub_le_sub_left_iff (H : k ≀ m) : m - n ≀ m - k ↔ k ≀ n := ⟨λ h, have k + (m - k) - n ≀ m - k, by rwa nat.add_sub_cancel' H, nat.le_of_add_le_add_right (nat.le_add_of_sub_le_left this), nat.sub_le_sub_left _⟩ protected theorem sub_lt_sub_right_iff (H : k ≀ m) : m - k < n - k ↔ m < n := lt_iff_lt_of_le_iff_le (nat.sub_le_sub_right_iff _ _ _ H) protected theorem sub_lt_sub_left_iff (H : n ≀ m) : m - n < m - k ↔ k < n := lt_iff_lt_of_le_iff_le (nat.sub_le_sub_left_iff H) protected theorem sub_le_iff : m - n ≀ k ↔ m - k ≀ n := nat.sub_le_left_iff_le_add.trans nat.sub_le_right_iff_le_add.symm protected lemma sub_le_self (n m : β„•) : n - m ≀ n := nat.sub_le_left_of_le_add (nat.le_add_left _ _) protected theorem sub_lt_iff (h₁ : n ≀ m) (hβ‚‚ : k ≀ m) : m - n < k ↔ m - k < n := (nat.sub_lt_left_iff_lt_add h₁).trans (nat.sub_lt_right_iff_lt_add hβ‚‚).symm lemma pred_le_iff {n m : β„•} : pred n ≀ m ↔ n ≀ succ m := @nat.sub_le_right_iff_le_add n m 1 lemma lt_pred_iff {n m : β„•} : n < pred m ↔ succ n < m := @nat.lt_sub_right_iff_add_lt n 1 m lemma lt_of_lt_pred {a b : β„•} (h : a < b - 1) : a < b := lt_of_succ_lt (lt_pred_iff.1 h) protected theorem mul_ne_zero {n m : β„•} (n0 : n β‰  0) (m0 : m β‰  0) : n * m β‰  0 | nm := (eq_zero_of_mul_eq_zero nm).elim n0 m0 @[simp] protected theorem mul_eq_zero {a b : β„•} : a * b = 0 ↔ a = 0 ∨ b = 0 := iff.intro eq_zero_of_mul_eq_zero (by simp [or_imp_distrib] {contextual := tt}) @[simp] protected theorem zero_eq_mul {a b : β„•} : 0 = a * b ↔ a = 0 ∨ b = 0 := by rw [eq_comm, nat.mul_eq_zero] lemma eq_zero_of_double_le {a : β„•} (h : 2 * a ≀ a) : a = 0 := nat.eq_zero_of_le_zero $ by rwa [two_mul, nat.add_le_to_le_sub, nat.sub_self] at h; refl lemma eq_zero_of_mul_le {a b : β„•} (hb : 2 ≀ b) (h : b * a ≀ a) : a = 0 := eq_zero_of_double_le $ le_trans (nat.mul_le_mul_right _ hb) h lemma le_mul_of_pos_left {m n : β„•} (h : 0 < n) : m ≀ n * m := begin conv {to_lhs, rw [← one_mul(m)]}, exact mul_le_mul_of_nonneg_right (nat.succ_le_of_lt h) dec_trivial, end lemma le_mul_of_pos_right {m n : β„•} (h : 0 < n) : m ≀ m * n := begin conv {to_lhs, rw [← mul_one(m)]}, exact mul_le_mul_of_nonneg_left (nat.succ_le_of_lt h) dec_trivial, end theorem two_mul_ne_two_mul_add_one {n m} : 2 * n β‰  2 * m + 1 := mt (congr_arg (%2)) (by rw [add_comm, add_mul_mod_self_left, mul_mod_right]; exact dec_trivial) /-- Recursion principle based on `<`. -/ @[elab_as_eliminator] protected def strong_rec' {p : β„• β†’ Sort u} (H : βˆ€ n, (βˆ€ m, m < n β†’ p m) β†’ p n) : βˆ€ (n : β„•), p n | n := H n (Ξ» m hm, strong_rec' m) attribute [simp] nat.div_self protected lemma div_le_of_le_mul' {m n : β„•} {k} (h : m ≀ k * n) : m / k ≀ n := (eq_zero_or_pos k).elim (Ξ» k0, by rw [k0, nat.div_zero]; apply zero_le) (Ξ» k0, (decidable.mul_le_mul_left k0).1 $ calc k * (m / k) ≀ m % k + k * (m / k) : le_add_left _ _ ... = m : mod_add_div _ _ ... ≀ k * n : h) protected lemma div_le_self' (m n : β„•) : m / n ≀ m := (eq_zero_or_pos n).elim (Ξ» n0, by rw [n0, nat.div_zero]; apply zero_le) (Ξ» n0, nat.div_le_of_le_mul' $ calc m = 1 * m : (one_mul _).symm ... ≀ n * m : mul_le_mul_right _ n0) theorem le_div_iff_mul_le' {x y : β„•} {k : β„•} (k0 : 0 < k) : x ≀ y / k ↔ x * k ≀ y := begin revert x, refine nat.strong_rec' _ y, clear y, intros y IH x, cases decidable.lt_or_le y k with h h, { rw [div_eq_of_lt h], cases x with x, { simp [zero_mul, zero_le] }, { rw succ_mul, exact iff_of_false (not_succ_le_zero _) (not_le_of_lt $ lt_of_lt_of_le h (le_add_left _ _)) } }, { rw [div_eq_sub_div k0 h], cases x with x, { simp [zero_mul, zero_le] }, { rw [← add_one, nat.add_le_add_iff_le_right, succ_mul, IH _ (sub_lt_of_pos_le _ _ k0 h), add_le_to_le_sub _ h] } } end theorem div_mul_le_self' (m n : β„•) : m / n * n ≀ m := (nat.eq_zero_or_pos n).elim (Ξ» n0, by simp [n0, zero_le]) $ Ξ» n0, (le_div_iff_mul_le' n0).1 (le_refl _) theorem div_lt_iff_lt_mul' {x y : β„•} {k : β„•} (k0 : 0 < k) : x / k < y ↔ x < y * k := lt_iff_lt_of_le_iff_le $ le_div_iff_mul_le' k0 protected theorem div_le_div_right {n m : β„•} (h : n ≀ m) {k : β„•} : n / k ≀ m / k := (nat.eq_zero_or_pos k).elim (Ξ» k0, by simp [k0]) $ Ξ» hk, (le_div_iff_mul_le' hk).2 $ le_trans (nat.div_mul_le_self' _ _) h lemma lt_of_div_lt_div {m n k : β„•} (h : m / k < n / k) : m < n := by_contradiction $ Ξ» h₁, absurd h (not_lt_of_ge (nat.div_le_div_right (not_lt.1 h₁))) protected theorem eq_mul_of_div_eq_right {a b c : β„•} (H1 : b ∣ a) (H2 : a / b = c) : a = b * c := by rw [← H2, nat.mul_div_cancel' H1] protected theorem div_eq_iff_eq_mul_right {a b c : β„•} (H : 0 < b) (H' : b ∣ a) : a / b = c ↔ a = b * c := ⟨nat.eq_mul_of_div_eq_right H', nat.div_eq_of_eq_mul_right H⟩ protected theorem div_eq_iff_eq_mul_left {a b c : β„•} (H : 0 < b) (H' : b ∣ a) : a / b = c ↔ a = c * b := by rw mul_comm; exact nat.div_eq_iff_eq_mul_right H H' protected theorem eq_mul_of_div_eq_left {a b c : β„•} (H1 : b ∣ a) (H2 : a / b = c) : a = c * b := by rw [mul_comm, nat.eq_mul_of_div_eq_right H1 H2] protected theorem mul_div_cancel_left' {a b : β„•} (Hd : a ∣ b) : a * (b / a) = b := by rw [mul_comm,nat.div_mul_cancel Hd] protected theorem div_mod_unique {n k m d : β„•} (h : 0 < k) : n / k = d ∧ n % k = m ↔ m + k * d = n ∧ m < k := ⟨λ ⟨e₁, eβ‚‚βŸ©, e₁ β–Έ eβ‚‚ β–Έ ⟨mod_add_div _ _, mod_lt _ h⟩, Ξ» ⟨h₁, hβ‚‚βŸ©, h₁ β–Έ by rw [add_mul_div_left _ _ h, add_mul_mod_self_left]; simp [div_eq_of_lt, mod_eq_of_lt, hβ‚‚]⟩ lemma two_mul_odd_div_two {n : β„•} (hn : n % 2 = 1) : 2 * (n / 2) = n - 1 := by conv {to_rhs, rw [← nat.mod_add_div n 2, hn, nat.add_sub_cancel_left]} lemma div_dvd_of_dvd {a b : β„•} (h : b ∣ a) : (a / b) ∣ a := ⟨b, (nat.div_mul_cancel h).symm⟩ protected lemma div_pos {a b : β„•} (hba : b ≀ a) (hb : 0 < b) : 0 < a / b := nat.pos_of_ne_zero (Ξ» h, lt_irrefl a (calc a = a % b : by simpa [h] using (mod_add_div a b).symm ... < b : nat.mod_lt a hb ... ≀ a : hba)) protected theorem mul_left_inj {a b c : β„•} (ha : 0 < a) : b * a = c * a ↔ b = c := ⟨nat.eq_of_mul_eq_mul_right ha, Ξ» e, e β–Έ rfl⟩ protected theorem mul_right_inj {a b c : β„•} (ha : 0 < a) : a * b = a * c ↔ b = c := ⟨nat.eq_of_mul_eq_mul_left ha, Ξ» e, e β–Έ rfl⟩ protected lemma div_div_self : βˆ€ {a b : β„•}, b ∣ a β†’ 0 < a β†’ a / (a / b) = b | a 0 h₁ hβ‚‚ := by rw eq_zero_of_zero_dvd h₁; refl | 0 b h₁ hβ‚‚ := absurd hβ‚‚ dec_trivial | (a+1) (b+1) h₁ hβ‚‚ := (nat.mul_left_inj (nat.div_pos (le_of_dvd (succ_pos a) h₁) (succ_pos b))).1 $ by rw [nat.div_mul_cancel (div_dvd_of_dvd h₁), nat.mul_div_cancel' h₁] protected lemma div_lt_of_lt_mul {m n k : β„•} (h : m < n * k) : m / n < k := lt_of_mul_lt_mul_left (calc n * (m / n) ≀ m % n + n * (m / n) : nat.le_add_left _ _ ... = m : mod_add_div _ _ ... < n * k : h) (nat.zero_le n) lemma lt_mul_of_div_lt {a b c : β„•} (h : a / c < b) (w : 0 < c) : a < b * c := lt_of_not_ge $ not_le_of_gt h ∘ (nat.le_div_iff_mul_le _ _ w).2 protected lemma div_eq_zero_iff {a b : β„•} (hb : 0 < b) : a / b = 0 ↔ a < b := ⟨λ h, by rw [← mod_add_div a b, h, mul_zero, add_zero]; exact mod_lt _ hb, Ξ» h, by rw [← nat.mul_right_inj hb, ← @add_left_cancel_iff _ _ (a % b), mod_add_div, mod_eq_of_lt h, mul_zero, add_zero]⟩ lemma eq_zero_of_le_div {a b : β„•} (hb : 2 ≀ b) (h : a ≀ a / b) : a = 0 := eq_zero_of_mul_le hb $ by rw mul_comm; exact (nat.le_div_iff_mul_le' (lt_of_lt_of_le dec_trivial hb)).1 h lemma mul_div_le_mul_div_assoc (a b c : β„•) : a * (b / c) ≀ (a * b) / c := if hc0 : c = 0 then by simp [hc0] else (nat.le_div_iff_mul_le _ _ (nat.pos_of_ne_zero hc0)).2 (by rw [mul_assoc]; exact mul_le_mul_left _ (nat.div_mul_le_self _ _)) lemma div_mul_div_le_div (a b c : β„•) : ((a / c) * b) / a ≀ b / c := if ha0 : a = 0 then by simp [ha0] else calc a / c * b / a ≀ b * a / c / a : nat.div_le_div_right (by rw [mul_comm]; exact mul_div_le_mul_div_assoc _ _ _) ... = b / c : by rw [nat.div_div_eq_div_mul, mul_comm b, mul_comm c, nat.mul_div_mul _ _ (nat.pos_of_ne_zero ha0)] lemma eq_zero_of_le_half {a : β„•} (h : a ≀ a / 2) : a = 0 := eq_zero_of_le_div (le_refl _) h lemma mod_mul_right_div_self (a b c : β„•) : a % (b * c) / b = (a / b) % c := if hb : b = 0 then by simp [hb] else if hc : c = 0 then by simp [hc] else by conv {to_rhs, rw ← mod_add_div a (b * c)}; rw [mul_assoc, nat.add_mul_div_left _ _ (nat.pos_of_ne_zero hb), add_mul_mod_self_left, mod_eq_of_lt (nat.div_lt_of_lt_mul (mod_lt _ (mul_pos (nat.pos_of_ne_zero hb) (nat.pos_of_ne_zero hc))))] lemma mod_mul_left_div_self (a b c : β„•) : a % (c * b) / b = (a / b) % c := by rw [mul_comm c, mod_mul_right_div_self] /- The `n+1`-st triangle number is `n` more than the `n`-th triangle number -/ lemma triangle_succ (n : β„•) : (n + 1) * ((n + 1) - 1) / 2 = n * (n - 1) / 2 + n := begin rw [← add_mul_div_left, mul_comm 2 n, ← mul_add, nat.add_sub_cancel, mul_comm], cases n; refl, apply zero_lt_succ end @[simp] protected theorem dvd_one {n : β„•} : n ∣ 1 ↔ n = 1 := ⟨eq_one_of_dvd_one, Ξ» e, e.symm β–Έ dvd_refl _⟩ protected theorem dvd_add_left {k m n : β„•} (h : k ∣ n) : k ∣ m + n ↔ k ∣ m := (nat.dvd_add_iff_left h).symm protected theorem dvd_add_right {k m n : β„•} (h : k ∣ m) : k ∣ m + n ↔ k ∣ n := (nat.dvd_add_iff_right h).symm /-- A natural number m divides the sum m + n if and only if m divides b.-/ @[simp] protected lemma dvd_add_self_left {m n : β„•} : m ∣ m + n ↔ m ∣ n := nat.dvd_add_right (dvd_refl m) /-- A natural number m divides the sum n + m if and only if m divides b.-/ @[simp] protected lemma dvd_add_self_right {m n : β„•} : m ∣ n + m ↔ m ∣ n := nat.dvd_add_left (dvd_refl m) protected theorem mul_dvd_mul_iff_left {a b c : β„•} (ha : 0 < a) : a * b ∣ a * c ↔ b ∣ c := exists_congr $ Ξ» d, by rw [mul_assoc, nat.mul_right_inj ha] protected theorem mul_dvd_mul_iff_right {a b c : β„•} (hc : 0 < c) : a * c ∣ b * c ↔ a ∣ b := exists_congr $ Ξ» d, by rw [mul_right_comm, nat.mul_left_inj hc] lemma succ_div : βˆ€ (a b : β„•), (a + 1) / b = a / b + if b ∣ a + 1 then 1 else 0 | a 0 := by simp | 0 1 := rfl | 0 (b+2) := have hb2 : b + 2 > 1, from dec_trivial, by simp [ne_of_gt hb2, div_eq_of_lt hb2] | (a+1) (b+1) := begin rw [nat.div_def], conv_rhs { rw nat.div_def }, by_cases hb_eq_a : b = a + 1, { simp [hb_eq_a, le_refl] }, by_cases hb_le_a1 : b ≀ a + 1, { have hb_le_a : b ≀ a, from le_of_lt_succ (lt_of_le_of_ne hb_le_a1 hb_eq_a), have h₁ : (0 < b + 1 ∧ b + 1 ≀ a + 1 + 1), from ⟨succ_pos _, (add_le_add_iff_right _).2 hb_le_a1⟩, have hβ‚‚ : (0 < b + 1 ∧ b + 1 ≀ a + 1), from ⟨succ_pos _, (add_le_add_iff_right _).2 hb_le_a⟩, have dvd_iff : b + 1 ∣ a - b + 1 ↔ b + 1 ∣ a + 1 + 1, { rw [nat.dvd_add_iff_left (dvd_refl (b + 1)), ← nat.add_sub_add_right a 1 b, add_comm (_ - _), add_assoc, nat.sub_add_cancel (succ_le_succ hb_le_a), add_comm 1] }, have wf : a - b < a + 1, from lt_succ_of_le (nat.sub_le_self _ _), rw [if_pos h₁, if_pos hβ‚‚, nat.add_sub_add_right, nat.sub_add_comm hb_le_a, by exact have _ := wf, succ_div (a - b), nat.add_sub_add_right], simp [dvd_iff, succ_eq_add_one, add_comm 1, add_assoc] }, { have hba : Β¬ b ≀ a, from not_le_of_gt (lt_trans (lt_succ_self a) (lt_of_not_ge hb_le_a1)), have hb_dvd_a : Β¬ b + 1 ∣ a + 2, from Ξ» h, hb_le_a1 (le_of_succ_le_succ (le_of_dvd (succ_pos _) h)), simp [hba, hb_le_a1, hb_dvd_a], } end lemma succ_div_of_dvd {a b : β„•} (hba : b ∣ a + 1) : (a + 1) / b = a / b + 1 := by rw [succ_div, if_pos hba] lemma succ_div_of_not_dvd {a b : β„•} (hba : Β¬ b ∣ a + 1) : (a + 1) / b = a / b := by rw [succ_div, if_neg hba, add_zero] @[simp] theorem mod_mod_of_dvd (n : nat) {m k : nat} (h : m ∣ k) : n % k % m = n % m := begin conv { to_rhs, rw ←mod_add_div n k }, rcases h with ⟨t, rfl⟩, rw [mul_assoc, add_mul_mod_self_left] end @[simp] theorem mod_mod (a n : β„•) : (a % n) % n = a % n := (eq_zero_or_pos n).elim (Ξ» n0, by simp [n0]) (Ξ» npos, mod_eq_of_lt (mod_lt _ npos)) /-- If `a` and `b` are equal mod `c`, `a - b` is zero mod `c`. -/ lemma sub_mod_eq_zero_of_mod_eq {a b c : β„•} (h : a % c = b % c) : (a - b) % c = 0 := by rw [←nat.mod_add_div a c, ←nat.mod_add_div b c, ←h, ←nat.sub_sub, nat.add_sub_cancel_left, ←nat.mul_sub_left_distrib, nat.mul_mod_right] lemma dvd_sub_mod (k : β„•) : n ∣ (k - (k % n)) := ⟨k / n, nat.sub_eq_of_eq_add (nat.mod_add_div k n).symm⟩ @[simp] theorem mod_add_mod (m n k : β„•) : (m % n + k) % n = (m + k) % n := by have := (add_mul_mod_self_left (m % n + k) n (m / n)).symm; rwa [add_right_comm, mod_add_div] at this @[simp] theorem add_mod_mod (m n k : β„•) : (m + n % k) % k = (m + n) % k := by rw [add_comm, mod_add_mod, add_comm] lemma add_mod (a b n : β„•) : (a + b) % n = ((a % n) + (b % n)) % n := by rw [add_mod_mod, mod_add_mod] theorem add_mod_eq_add_mod_right {m n k : β„•} (i : β„•) (H : m % n = k % n) : (m + i) % n = (k + i) % n := by rw [← mod_add_mod, ← mod_add_mod k, H] theorem add_mod_eq_add_mod_left {m n k : β„•} (i : β„•) (H : m % n = k % n) : (i + m) % n = (i + k) % n := by rw [add_comm, add_mod_eq_add_mod_right _ H, add_comm] lemma mul_mod (a b n : β„•) : (a * b) % n = ((a % n) * (b % n)) % n := begin conv_lhs { rw [←mod_add_div a n, ←mod_add_div b n, right_distrib, left_distrib, left_distrib, mul_assoc, mul_assoc, ←left_distrib n _ _, add_mul_mod_self_left, mul_comm _ (n * (b / n)), mul_assoc, add_mul_mod_self_left] } end theorem add_pos_left {m : β„•} (h : 0 < m) (n : β„•) : 0 < m + n := calc m + n > 0 + n : nat.add_lt_add_right h n ... = n : nat.zero_add n ... β‰₯ 0 : zero_le n theorem add_pos_right (m : β„•) {n : β„•} (h : 0 < n) : 0 < m + n := begin rw add_comm, exact add_pos_left h m end theorem add_pos_iff_pos_or_pos (m n : β„•) : 0 < m + n ↔ 0 < m ∨ 0 < n := iff.intro begin intro h, cases m with m, {simp [zero_add] at h, exact or.inr h}, exact or.inl (succ_pos _) end begin intro h, cases h with mpos npos, { apply add_pos_left mpos }, apply add_pos_right _ npos end lemma add_eq_one_iff : βˆ€ {a b : β„•}, a + b = 1 ↔ (a = 0 ∧ b = 1) ∨ (a = 1 ∧ b = 0) | 0 0 := dec_trivial | 0 1 := dec_trivial | 1 0 := dec_trivial | 1 1 := dec_trivial | (a+2) _ := by rw add_right_comm; exact dec_trivial | _ (b+2) := by rw [← add_assoc]; simp only [nat.succ_inj', nat.succ_ne_zero]; simp lemma mul_eq_one_iff : βˆ€ {a b : β„•}, a * b = 1 ↔ a = 1 ∧ b = 1 | 0 0 := dec_trivial | 0 1 := dec_trivial | 1 0 := dec_trivial | (a+2) 0 := by simp | 0 (b+2) := by simp | (a+1) (b+1) := ⟨λ h, by simp only [add_mul, mul_add, mul_add, one_mul, mul_one, (add_assoc _ _ _).symm, nat.succ_inj', add_eq_zero_iff] at h; simp [h.1.2, h.2], by clear_aux_decl; finish⟩ lemma mul_right_eq_self_iff {a b : β„•} (ha : 0 < a) : a * b = a ↔ b = 1 := suffices a * b = a * 1 ↔ b = 1, by rwa mul_one at this, nat.mul_right_inj ha lemma mul_left_eq_self_iff {a b : β„•} (hb : 0 < b) : a * b = b ↔ a = 1 := by rw [mul_comm, nat.mul_right_eq_self_iff hb] lemma lt_succ_iff_lt_or_eq {n i : β„•} : n < i.succ ↔ (n < i ∨ n = i) := lt_succ_iff.trans le_iff_lt_or_eq theorem le_zero_iff {i : β„•} : i ≀ 0 ↔ i = 0 := ⟨nat.eq_zero_of_le_zero, assume h, h β–Έ le_refl i⟩ theorem le_add_one_iff {i j : β„•} : i ≀ j + 1 ↔ (i ≀ j ∨ i = j + 1) := ⟨assume h, match nat.eq_or_lt_of_le h with | or.inl h := or.inr h | or.inr h := or.inl $ nat.le_of_succ_le_succ h end, or.rec (assume h, le_trans h $ nat.le_add_right _ _) le_of_eq⟩ theorem mul_self_inj {n m : β„•} : n * n = m * m ↔ n = m := le_antisymm_iff.trans (le_antisymm_iff.trans (and_congr mul_self_le_mul_self_iff mul_self_le_mul_self_iff)).symm section facts -- Inject some simple facts into the typeclass system. -- This `fact` should not be confused with the factorial function `nat.fact`! instance succ_pos'' (n : β„•) : _root_.fact (0 < n.succ) := n.succ_pos instance pos_of_one_lt (n : β„•) [h : fact (1 < n)] : fact (0 < n) := lt_trans zero_lt_one h end facts instance decidable_ball_lt (n : nat) (P : Ξ  k < n, Prop) : βˆ€ [H : βˆ€ n h, decidable (P n h)], decidable (βˆ€ n h, P n h) := begin induction n with n IH; intro; resetI, { exact is_true (Ξ» n, dec_trivial) }, cases IH (Ξ» k h, P k (lt_succ_of_lt h)) with h, { refine is_false (mt _ h), intros hn k h, apply hn }, by_cases p : P n (lt_succ_self n), { exact is_true (Ξ» k h', (lt_or_eq_of_le $ le_of_lt_succ h').elim (h _) (Ξ» e, match k, e, h' with _, rfl, h := p end)) }, { exact is_false (mt (Ξ» hn, hn _ _) p) } end instance decidable_forall_fin {n : β„•} (P : fin n β†’ Prop) [H : decidable_pred P] : decidable (βˆ€ i, P i) := decidable_of_iff (βˆ€ k h, P ⟨k, h⟩) ⟨λ a ⟨k, h⟩, a k h, Ξ» a k h, a ⟨k, h⟩⟩ instance decidable_ball_le (n : β„•) (P : Ξ  k ≀ n, Prop) [H : βˆ€ n h, decidable (P n h)] : decidable (βˆ€ n h, P n h) := decidable_of_iff (βˆ€ k (h : k < succ n), P k (le_of_lt_succ h)) ⟨λ a k h, a k (lt_succ_of_le h), Ξ» a k h, a k _⟩ instance decidable_lo_hi (lo hi : β„•) (P : β„• β†’ Prop) [H : decidable_pred P] : decidable (βˆ€x, lo ≀ x β†’ x < hi β†’ P x) := decidable_of_iff (βˆ€ x < hi - lo, P (lo + x)) ⟨λal x hl hh, by have := al (x - lo) (lt_of_not_ge $ (not_congr (nat.sub_le_sub_right_iff _ _ _ hl)).2 $ not_le_of_gt hh); rwa [nat.add_sub_of_le hl] at this, Ξ»al x h, al _ (nat.le_add_right _ _) (nat.add_lt_of_lt_sub_left h)⟩ instance decidable_lo_hi_le (lo hi : β„•) (P : β„• β†’ Prop) [H : decidable_pred P] : decidable (βˆ€x, lo ≀ x β†’ x ≀ hi β†’ P x) := decidable_of_iff (βˆ€x, lo ≀ x β†’ x < hi + 1 β†’ P x) $ ball_congr $ Ξ» x hl, imp_congr lt_succ_iff iff.rfl protected theorem bit0_le {n m : β„•} (h : n ≀ m) : bit0 n ≀ bit0 m := add_le_add h h protected theorem bit1_le {n m : β„•} (h : n ≀ m) : bit1 n ≀ bit1 m := succ_le_succ (add_le_add h h) theorem bit_le : βˆ€ (b : bool) {n m : β„•}, n ≀ m β†’ bit b n ≀ bit b m | tt n m h := nat.bit1_le h | ff n m h := nat.bit0_le h theorem bit_ne_zero (b) {n} (h : n β‰  0) : bit b n β‰  0 := by cases b; [exact nat.bit0_ne_zero h, exact nat.bit1_ne_zero _] theorem bit0_le_bit : βˆ€ (b) {m n : β„•}, m ≀ n β†’ bit0 m ≀ bit b n | tt m n h := le_of_lt $ nat.bit0_lt_bit1 h | ff m n h := nat.bit0_le h theorem bit_le_bit1 : βˆ€ (b) {m n : β„•}, m ≀ n β†’ bit b m ≀ bit1 n | ff m n h := le_of_lt $ nat.bit0_lt_bit1 h | tt m n h := nat.bit1_le h theorem bit_lt_bit0 : βˆ€ (b) {n m : β„•}, n < m β†’ bit b n < bit0 m | tt n m h := nat.bit1_lt_bit0 h | ff n m h := nat.bit0_lt h theorem bit_lt_bit (a b) {n m : β„•} (h : n < m) : bit a n < bit b m := lt_of_lt_of_le (bit_lt_bit0 _ h) (bit0_le_bit _ (le_refl _)) @[simp] lemma bit0_le_bit1_iff : bit0 k ≀ bit1 n ↔ k ≀ n := ⟨λ h, by rwa [← nat.lt_succ_iff, n.bit1_eq_succ_bit0, ← n.bit0_succ_eq, bit0_lt_bit0, nat.lt_succ_iff] at h, Ξ» h, le_of_lt (nat.bit0_lt_bit1 h)⟩ @[simp] lemma bit0_lt_bit1_iff : bit0 k < bit1 n ↔ k ≀ n := ⟨λ h, bit0_le_bit1_iff.1 (le_of_lt h), nat.bit0_lt_bit1⟩ @[simp] lemma bit1_le_bit0_iff : bit1 k ≀ bit0 n ↔ k < n := ⟨λ h, by rwa [k.bit1_eq_succ_bit0, succ_le_iff, bit0_lt_bit0] at h, Ξ» h, le_of_lt (nat.bit1_lt_bit0 h)⟩ @[simp] lemma bit1_lt_bit0_iff : bit1 k < bit0 n ↔ k < n := ⟨λ h, bit1_le_bit0_iff.1 (le_of_lt h), nat.bit1_lt_bit0⟩ @[simp] lemma one_le_bit0_iff : 1 ≀ bit0 n ↔ 0 < n := by { convert bit1_le_bit0_iff, refl, } @[simp] lemma one_lt_bit0_iff : 1 < bit0 n ↔ 1 ≀ n := by { convert bit1_lt_bit0_iff, refl, } @[simp] lemma bit_le_bit_iff : βˆ€ {b : bool}, bit b k ≀ bit b n ↔ k ≀ n | ff := bit0_le_bit0 | tt := bit1_le_bit1 @[simp] lemma bit_lt_bit_iff : βˆ€ {b : bool}, bit b k < bit b n ↔ k < n | ff := bit0_lt_bit0 | tt := bit1_lt_bit1 @[simp] lemma bit_le_bit1_iff : βˆ€ {b : bool}, bit b k ≀ bit1 n ↔ k ≀ n | ff := bit0_le_bit1_iff | tt := bit1_le_bit1 lemma pos_of_bit0_pos {n : β„•} (h : 0 < bit0 n) : 0 < n := by { cases n, cases h, apply succ_pos, } /-- Define a function on `β„•` depending on parity of the argument. -/ @[elab_as_eliminator] def bit_cases {C : β„• β†’ Sort u} (H : Ξ  b n, C (bit b n)) (n : β„•) : C n := eq.rec_on n.bit_decomp (H (bodd n) (div2 n)) /- partial subtraction -/ /-- Partial predecessor operation. Returns `ppred n = some m` if `n = m + 1`, otherwise `none`. -/ @[simp] def ppred : β„• β†’ option β„• | 0 := none | (n+1) := some n /-- Partial subtraction operation. Returns `psub m n = some k` if `m = n + k`, otherwise `none`. -/ @[simp] def psub (m : β„•) : β„• β†’ option β„• | 0 := some m | (n+1) := psub n >>= ppred theorem pred_eq_ppred (n : β„•) : pred n = (ppred n).get_or_else 0 := by cases n; refl theorem sub_eq_psub (m : β„•) : βˆ€ n, m - n = (psub m n).get_or_else 0 | 0 := rfl | (n+1) := (pred_eq_ppred (m-n)).trans $ by rw [sub_eq_psub, psub]; cases psub m n; refl @[simp] theorem ppred_eq_some {m : β„•} : βˆ€ {n}, ppred n = some m ↔ succ m = n | 0 := by split; intro h; contradiction | (n+1) := by dsimp; split; intro h; injection h; subst n @[simp] theorem ppred_eq_none : βˆ€ {n : β„•}, ppred n = none ↔ n = 0 | 0 := by simp | (n+1) := by dsimp; split; contradiction theorem psub_eq_some {m : β„•} : βˆ€ {n k}, psub m n = some k ↔ k + n = m | 0 k := by simp [eq_comm] | (n+1) k := begin dsimp, apply option.bind_eq_some.trans, simp [psub_eq_some, add_comm, add_left_comm, nat.succ_eq_add_one] end theorem psub_eq_none (m n : β„•) : psub m n = none ↔ m < n := begin cases s : psub m n; simp [eq_comm], { show m < n, refine lt_of_not_ge (Ξ» h, _), cases le.dest h with k e, injection s.symm.trans (psub_eq_some.2 $ (add_comm _ _).trans e) }, { show n ≀ m, rw ← psub_eq_some.1 s, apply le_add_left } end theorem ppred_eq_pred {n} (h : 0 < n) : ppred n = some (pred n) := ppred_eq_some.2 $ succ_pred_eq_of_pos h theorem psub_eq_sub {m n} (h : n ≀ m) : psub m n = some (m - n) := psub_eq_some.2 $ nat.sub_add_cancel h theorem psub_add (m n k) : psub m (n + k) = do x ← psub m n, psub x k := by induction k; simp [*, add_succ, bind_assoc] /- pow -/ attribute [simp] nat.pow_zero nat.pow_one @[simp] lemma one_pow : βˆ€ n : β„•, 1 ^ n = 1 | 0 := rfl | (k+1) := show 1^k * 1 = 1, by rw [mul_one, one_pow] theorem pow_add (a m n : β„•) : a^(m + n) = a^m * a^n := by induction n; simp [*, pow_succ, mul_assoc] theorem pow_two (a : β„•) : a ^ 2 = a * a := show (1 * a) * a = _, by rw one_mul theorem pow_dvd_pow (a : β„•) {m n : β„•} (h : m ≀ n) : a^m ∣ a^n := by rw [← nat.add_sub_cancel' h, pow_add]; apply dvd_mul_right theorem pow_dvd_pow_of_dvd {a b : β„•} (h : a ∣ b) : βˆ€ n:β„•, a^n ∣ b^n | 0 := dvd_refl _ | (n+1) := mul_dvd_mul (pow_dvd_pow_of_dvd n) h theorem mul_pow (a b n : β„•) : (a * b) ^ n = a ^ n * b ^ n := by induction n; simp [*, nat.pow_succ, mul_comm, mul_assoc, mul_left_comm] protected theorem pow_mul (a b n : β„•) : n ^ (a * b) = (n ^ a) ^ b := by induction b; simp [*, nat.succ_eq_add_one, nat.pow_add, mul_add, mul_comm] theorem pow_pos {p : β„•} (hp : 0 < p) : βˆ€ n : β„•, 0 < p ^ n | 0 := by simp | (k+1) := mul_pos (pow_pos _) hp lemma pow_eq_mul_pow_sub (p : β„•) {m n : β„•} (h : m ≀ n) : p ^ m * p ^ (n - m) = p ^ n := by rw [←nat.pow_add, nat.add_sub_cancel' h] lemma pow_lt_pow_succ {p : β„•} (h : 1 < p) (n : β„•) : p^n < p^(n+1) := suffices p^n*1 < p^n*p, by simpa, nat.mul_lt_mul_of_pos_left h (nat.pow_pos (lt_of_succ_lt h) n) lemma lt_pow_self {p : β„•} (h : 1 < p) : βˆ€ n : β„•, n < p ^ n | 0 := by simp [zero_lt_one] | (n+1) := calc n + 1 < p^n + 1 : nat.add_lt_add_right (lt_pow_self _) _ ... ≀ p ^ (n+1) : pow_lt_pow_succ h _ lemma pow_right_strict_mono {x : β„•} (k : 2 ≀ x) : strict_mono (nat.pow x) := Ξ» _ _, pow_lt_pow_of_lt_right k lemma pow_le_iff_le_right {x m n : β„•} (k : 2 ≀ x) : x^m ≀ x^n ↔ m ≀ n := strict_mono.le_iff_le (pow_right_strict_mono k) lemma pow_lt_iff_lt_right {x m n : β„•} (k : 2 ≀ x) : x^m < x^n ↔ m < n := strict_mono.lt_iff_lt (pow_right_strict_mono k) lemma pow_right_injective {x : β„•} (k : 2 ≀ x) : function.injective (nat.pow x) := strict_mono.injective (pow_right_strict_mono k) lemma pow_left_strict_mono {m : β„•} (k : 1 ≀ m) : strict_mono (Ξ» (x : β„•), x^m) := Ξ» _ _ h, pow_lt_pow_of_lt_left h k lemma pow_le_iff_le_left {m x y : β„•} (k : 1 ≀ m) : x^m ≀ y^m ↔ x ≀ y := strict_mono.le_iff_le (pow_left_strict_mono k) lemma pow_lt_iff_lt_left {m x y : β„•} (k : 1 ≀ m) : x^m < y^m ↔ x < y := strict_mono.lt_iff_lt (pow_left_strict_mono k) lemma pow_left_injective {m : β„•} (k : 1 ≀ m) : function.injective (Ξ» (x : β„•), x^m) := strict_mono.injective (pow_left_strict_mono k) lemma not_pos_pow_dvd : βˆ€ {p k : β„•} (hp : 1 < p) (hk : 1 < k), Β¬ p^k ∣ p | (succ p) (succ k) hp hk h := have (succ p)^k * succ p ∣ 1 * succ p, by simpa, have (succ p) ^ k ∣ 1, from dvd_of_mul_dvd_mul_right (succ_pos _) this, have he : (succ p) ^ k = 1, from eq_one_of_dvd_one this, have k < (succ p) ^ k, from lt_pow_self hp k, have k < 1, by rwa [he] at this, have k = 0, from eq_zero_of_le_zero $ le_of_lt_succ this, have 1 < 1, by rwa [this] at hk, absurd this dec_trivial @[simp] theorem bodd_div2_eq (n : β„•) : bodd_div2 n = (bodd n, div2 n) := by unfold bodd div2; cases bodd_div2 n; refl @[simp] lemma bodd_bit0 (n) : bodd (bit0 n) = ff := bodd_bit ff n @[simp] lemma bodd_bit1 (n) : bodd (bit1 n) = tt := bodd_bit tt n @[simp] lemma div2_bit0 (n) : div2 (bit0 n) = n := div2_bit ff n @[simp] lemma div2_bit1 (n) : div2 (bit1 n) = n := div2_bit tt n /- iterate -/ section variables {Ξ± : Sort*} (op : Ξ± β†’ Ξ±) @[simp] theorem iterate_zero : op^[0] = id := rfl theorem iterate_zero_apply (x : Ξ±) : op^[0] x = x := rfl @[simp] theorem iterate_succ (n : β„•) : op^[succ n] = (op^[n]) ∘ op := rfl theorem iterate_succ_apply (n : β„•) (x : Ξ±) : op^[succ n] x = (op^[n]) (op x) := rfl theorem iterate_add : βˆ€ (m n : β„•), op^[m + n] = (op^[m]) ∘ (op^[n]) | m 0 := rfl | m (succ n) := by rw [iterate_succ, iterate_succ, iterate_add] theorem iterate_add_apply (m n : β„•) (x : Ξ±) : op^[m + n] x = (op^[m] (op^[n] x)) := by rw iterate_add @[simp] theorem iterate_one : op^[1] = op := funext $ Ξ» a, rfl theorem iterate_succ' (n : β„•) : op^[succ n] = op ∘ (op^[n]) := by rw [← one_add, iterate_add, iterate_one] theorem iterate_succ_apply' (n : β„•) (x : Ξ±) : op^[succ n] x = op (op^[n] x) := by rw [iterate_succ'] lemma iterate_mul (m : β„•) : βˆ€ n, op^[m * n] = (op^[m]^[n]) | 0 := by { ext a, simp only [mul_zero, iterate_zero] } | (n + 1) := by { ext x, simp only [mul_add, mul_one, iterate_one, iterate_add, iterate_mul n] } @[elab_as_eliminator] theorem iterate_ind {Ξ± : Type u} (f : Ξ± β†’ Ξ±) {p : (Ξ± β†’ Ξ±) β†’ Prop} (hf : p f) (hid : p id) (hcomp : βˆ€ ⦃f g⦄, p f β†’ p g β†’ p (f ∘ g)) : βˆ€ n, p (f^[n]) | 0 := hid | (n+1) := hcomp (iterate_ind n) hf theorem iterateβ‚€ {Ξ± : Type u} {op : Ξ± β†’ Ξ±} {x : Ξ±} (H : op x = x) {n : β„•} : op^[n] x = x := by induction n; [simp only [iterate_zero_apply], simp only [iterate_succ_apply', H, *]] theorem iterate₁ {Ξ± : Type u} {Ξ² : Type v} {op : Ξ± β†’ Ξ±} {op' : Ξ² β†’ Ξ²} {op'' : Ξ± β†’ Ξ²} (H : βˆ€ x, op' (op'' x) = op'' (op x)) {n : β„•} {x : Ξ±} : op'^[n] (op'' x) = op'' (op^[n] x) := by induction n; [simp only [iterate_zero_apply], simp only [iterate_succ_apply', H, *]] theorem iterateβ‚‚ {Ξ± : Type u} {op : Ξ± β†’ Ξ±} {op' : Ξ± β†’ Ξ± β†’ Ξ±} (H : βˆ€ x y, op (op' x y) = op' (op x) (op y)) {n : β„•} {x y : Ξ±} : op^[n] (op' x y) = op' (op^[n] x) (op^[n] y) := by induction n; [simp only [iterate_zero_apply], simp only [iterate_succ_apply', H, *]] theorem iterate_cancel {Ξ± : Type u} {op op' : Ξ± β†’ Ξ±} (H : βˆ€ x, op (op' x) = x) {n : β„•} {x : Ξ±} : op^[n] (op'^[n] x) = x := by induction n; [refl, rwa [iterate_succ_apply, iterate_succ_apply', H]] end /- size and shift -/ theorem shiftl'_ne_zero_left (b) {m} (h : m β‰  0) (n) : shiftl' b m n β‰  0 := by induction n; simp [shiftl', bit_ne_zero, *] theorem shiftl'_tt_ne_zero (m) : βˆ€ {n} (h : n β‰  0), shiftl' tt m n β‰  0 | 0 h := absurd rfl h | (succ n) _ := nat.bit1_ne_zero _ @[simp] theorem size_zero : size 0 = 0 := rfl @[simp] theorem size_bit {b n} (h : bit b n β‰  0) : size (bit b n) = succ (size n) := begin rw size, conv { to_lhs, rw [binary_rec], simp [h] }, rw div2_bit, end @[simp] theorem size_bit0 {n} (h : n β‰  0) : size (bit0 n) = succ (size n) := @size_bit ff n (nat.bit0_ne_zero h) @[simp] theorem size_bit1 (n) : size (bit1 n) = succ (size n) := @size_bit tt n (nat.bit1_ne_zero n) @[simp] theorem size_one : size 1 = 1 := by apply size_bit1 0 @[simp] theorem size_shiftl' {b m n} (h : shiftl' b m n β‰  0) : size (shiftl' b m n) = size m + n := begin induction n with n IH; simp [shiftl'] at h ⊒, rw [size_bit h, nat.add_succ], by_cases s0 : shiftl' b m n = 0; [skip, rw [IH s0]], rw s0 at h ⊒, cases b, {exact absurd rfl h}, have : shiftl' tt m n + 1 = 1 := congr_arg (+1) s0, rw [shiftl'_tt_eq_mul_pow] at this, have m0 := succ_inj (eq_one_of_dvd_one ⟨_, this.symm⟩), subst m0, simp at this, have : n = 0 := eq_zero_of_le_zero (le_of_not_gt $ Ξ» hn, ne_of_gt (pow_lt_pow_of_lt_right dec_trivial hn) this), subst n, refl end @[simp] theorem size_shiftl {m} (h : m β‰  0) (n) : size (shiftl m n) = size m + n := size_shiftl' (shiftl'_ne_zero_left _ h _) theorem lt_size_self (n : β„•) : n < 2^size n := begin rw [← one_shiftl], have : βˆ€ {n}, n = 0 β†’ n < shiftl 1 (size n) := Ξ» n e, by subst e; exact dec_trivial, apply binary_rec _ _ n, {apply this rfl}, intros b n IH, by_cases bit b n = 0, {apply this h}, rw [size_bit h, shiftl_succ], exact bit_lt_bit0 _ IH end theorem size_le {m n : β„•} : size m ≀ n ↔ m < 2^n := ⟨λ h, lt_of_lt_of_le (lt_size_self _) (pow_le_pow_of_le_right dec_trivial h), begin rw [← one_shiftl], revert n, apply binary_rec _ _ m, { intros n h, apply zero_le }, { intros b m IH n h, by_cases e : bit b m = 0, { rw e, apply zero_le }, rw [size_bit e], cases n with n, { exact e.elim (eq_zero_of_le_zero (le_of_lt_succ h)) }, { apply succ_le_succ (IH _), apply lt_imp_lt_of_le_imp_le (Ξ» h', bit0_le_bit _ h') h } } end⟩ theorem lt_size {m n : β„•} : m < size n ↔ 2^m ≀ n := by rw [← not_lt, iff_not_comm, not_lt, size_le] theorem size_pos {n : β„•} : 0 < size n ↔ 0 < n := by rw lt_size; refl theorem size_eq_zero {n : β„•} : size n = 0 ↔ n = 0 := by have := @size_pos n; simp [pos_iff_ne_zero] at this; exact not_iff_not.1 this theorem size_pow {n : β„•} : size (2^n) = n+1 := le_antisymm (size_le.2 $ pow_lt_pow_of_lt_right dec_trivial (lt_succ_self _)) (lt_size.2 $ le_refl _) theorem size_le_size {m n : β„•} (h : m ≀ n) : size m ≀ size n := size_le.2 $ lt_of_le_of_lt h (lt_size_self _) /- factorial -/ /-- `fact n` is the factorial of `n`. -/ @[simp] def fact : nat β†’ nat | 0 := 1 | (succ n) := succ n * fact n @[simp] theorem fact_zero : fact 0 = 1 := rfl @[simp] theorem fact_succ (n) : fact (succ n) = succ n * fact n := rfl @[simp] theorem fact_one : fact 1 = 1 := rfl theorem fact_pos : βˆ€ n, 0 < fact n | 0 := zero_lt_one | (succ n) := mul_pos (succ_pos _) (fact_pos n) theorem fact_ne_zero (n : β„•) : fact n β‰  0 := ne_of_gt (fact_pos _) theorem fact_dvd_fact {m n} (h : m ≀ n) : fact m ∣ fact n := begin induction n with n IH; simp, { have := eq_zero_of_le_zero h, subst m, simp }, { cases eq_or_lt_of_le h with he hl, { subst m, simp }, { apply dvd_mul_of_dvd_right (IH (le_of_lt_succ hl)) } } end theorem dvd_fact : βˆ€ {m n}, 0 < m β†’ m ≀ n β†’ m ∣ fact n | (succ m) n _ h := dvd_of_mul_right_dvd (fact_dvd_fact h) theorem fact_le {m n} (h : m ≀ n) : fact m ≀ fact n := le_of_dvd (fact_pos _) (fact_dvd_fact h) lemma fact_mul_pow_le_fact : βˆ€ {m n : β„•}, m.fact * m.succ ^ n ≀ (m + n).fact | m 0 := by simp | m (n+1) := by rw [← add_assoc, nat.fact_succ, mul_comm (nat.succ _), nat.pow_succ, ← mul_assoc]; exact mul_le_mul fact_mul_pow_le_fact (nat.succ_le_succ (nat.le_add_right _ _)) (nat.zero_le _) (nat.zero_le _) lemma monotone_fact : monotone fact := Ξ» n m, fact_le lemma fact_lt (h0 : 0 < n) : n.fact < m.fact ↔ n < m := begin split; intro h, { rw [← not_le], intro hmn, apply not_le_of_lt h (fact_le hmn) }, { have : βˆ€(n : β„•), 0 < n β†’ n.fact < n.succ.fact, { intros k hk, rw [fact_succ, succ_mul, lt_add_iff_pos_left], apply mul_pos hk (fact_pos k) }, induction h generalizing h0, { exact this _ h0, }, { refine lt_trans (h_ih h0) (this _ _), exact lt_trans h0 (lt_of_succ_le h_a) }} end lemma one_lt_fact : 1 < n.fact ↔ 1 < n := by { convert fact_lt _, refl, exact one_pos } lemma fact_eq_one : n.fact = 1 ↔ n ≀ 1 := begin split; intro h, { rw [← not_lt, ← one_lt_fact, h], apply lt_irrefl }, { cases h with h h, refl, cases h, refl } end lemma fact_inj (h0 : 1 < n.fact) : n.fact = m.fact ↔ n = m := begin split; intro h, { rcases lt_trichotomy n m with hnm|hnm|hnm, { exfalso, rw [← fact_lt, h] at hnm, exact lt_irrefl _ hnm, rw [one_lt_fact] at h0, exact lt_trans one_pos h0 }, { exact hnm }, { exfalso, rw [← fact_lt, h] at hnm, exact lt_irrefl _ hnm, rw [h, one_lt_fact] at h0, exact lt_trans one_pos h0 }}, { rw h } end /- choose -/ /-- `choose n k` is the number of `k`-element subsets in an `n`-element set. Also known as binomial coefficients. -/ def choose : β„• β†’ β„• β†’ β„• | _ 0 := 1 | 0 (k + 1) := 0 | (n + 1) (k + 1) := choose n k + choose n (k + 1) @[simp] lemma choose_zero_right (n : β„•) : choose n 0 = 1 := by cases n; refl @[simp] lemma choose_zero_succ (k : β„•) : choose 0 (succ k) = 0 := rfl lemma choose_succ_succ (n k : β„•) : choose (succ n) (succ k) = choose n k + choose n (succ k) := rfl lemma choose_eq_zero_of_lt : βˆ€ {n k}, n < k β†’ choose n k = 0 | _ 0 hk := absurd hk dec_trivial | 0 (k + 1) hk := choose_zero_succ _ | (n + 1) (k + 1) hk := have hnk : n < k, from lt_of_succ_lt_succ hk, have hnk1 : n < k + 1, from lt_of_succ_lt hk, by rw [choose_succ_succ, choose_eq_zero_of_lt hnk, choose_eq_zero_of_lt hnk1] @[simp] lemma choose_self (n : β„•) : choose n n = 1 := by induction n; simp [*, choose, choose_eq_zero_of_lt (lt_succ_self _)] @[simp] lemma choose_succ_self (n : β„•) : choose n (succ n) = 0 := choose_eq_zero_of_lt (lt_succ_self _) @[simp] lemma choose_one_right (n : β„•) : choose n 1 = n := by induction n; simp [*, choose, add_comm] /-- `choose n 2` is the `n`-th triangle number. -/ lemma choose_two_right (n : β„•) : choose n 2 = n * (n - 1) / 2 := begin induction n with n ih, simp, {rw triangle_succ n, simp [choose, ih], rw add_comm}, end lemma choose_pos : βˆ€ {n k}, k ≀ n β†’ 0 < choose n k | 0 _ hk := by rw [eq_zero_of_le_zero hk]; exact dec_trivial | (n + 1) 0 hk := by simp; exact dec_trivial | (n + 1) (k + 1) hk := by rw choose_succ_succ; exact add_pos_of_pos_of_nonneg (choose_pos (le_of_succ_le_succ hk)) (nat.zero_le _) lemma succ_mul_choose_eq : βˆ€ n k, succ n * choose n k = choose (succ n) (succ k) * succ k | 0 0 := dec_trivial | 0 (k + 1) := by simp [choose] | (n + 1) 0 := by simp | (n + 1) (k + 1) := by rw [choose_succ_succ (succ n) (succ k), add_mul, ←succ_mul_choose_eq, mul_succ, ←succ_mul_choose_eq, add_right_comm, ←mul_add, ←choose_succ_succ, ←succ_mul] lemma choose_mul_fact_mul_fact : βˆ€ {n k}, k ≀ n β†’ choose n k * fact k * fact (n - k) = fact n | 0 _ hk := by simp [eq_zero_of_le_zero hk] | (n + 1) 0 hk := by simp | (n + 1) (succ k) hk := begin cases lt_or_eq_of_le hk with hk₁ hk₁, { have h : choose n k * fact (succ k) * fact (n - k) = succ k * fact n := by rw ← choose_mul_fact_mul_fact (le_of_succ_le_succ hk); simp [fact_succ, mul_comm, mul_left_comm], have h₁ : fact (n - k) = (n - k) * fact (n - succ k) := by rw [← succ_sub_succ, succ_sub (le_of_lt_succ hk₁), fact_succ], have hβ‚‚ : choose n (succ k) * fact (succ k) * ((n - k) * fact (n - succ k)) = (n - k) * fact n := by rw ← choose_mul_fact_mul_fact (le_of_lt_succ hk₁); simp [fact_succ, mul_comm, mul_left_comm, mul_assoc], have h₃ : k * fact n ≀ n * fact n := mul_le_mul_right _ (le_of_succ_le_succ hk), rw [choose_succ_succ, add_mul, add_mul, succ_sub_succ, h, h₁, hβ‚‚, ← add_one, add_mul, nat.mul_sub_right_distrib, fact_succ, ← nat.add_sub_assoc h₃, add_assoc, ← add_mul, nat.add_sub_cancel_left, add_comm] }, { simp [hk₁, mul_comm, choose, nat.sub_self] } end theorem choose_eq_fact_div_fact {n k : β„•} (hk : k ≀ n) : choose n k = fact n / (fact k * fact (n - k)) := begin have : fact n = choose n k * (fact k * fact (n - k)) := by rw ← mul_assoc; exact (choose_mul_fact_mul_fact hk).symm, exact (nat.div_eq_of_eq_mul_left (mul_pos (fact_pos _) (fact_pos _)) this).symm end theorem fact_mul_fact_dvd_fact {n k : β„•} (hk : k ≀ n) : fact k * fact (n - k) ∣ fact n := by rw [←choose_mul_fact_mul_fact hk, mul_assoc]; exact dvd_mul_left _ _ @[simp] lemma choose_symm {n k : β„•} (hk : k ≀ n) : choose n (n-k) = choose n k := by rw [choose_eq_fact_div_fact hk, choose_eq_fact_div_fact (sub_le _ _), nat.sub_sub_self hk, mul_comm] lemma choose_symm_of_eq_add {n a b : β„•} (h : n = a + b) : nat.choose n a = nat.choose n b := by { convert nat.choose_symm (nat.le_add_left _ _), rw nat.add_sub_cancel} lemma choose_symm_add {a b : β„•} : choose (a+b) a = choose (a+b) b := choose_symm_of_eq_add rfl lemma choose_succ_right_eq (n k : β„•) : choose n (k + 1) * (k + 1) = choose n k * (n - k) := begin have e : (n+1) * choose n k = choose n k * (k+1) + choose n (k+1) * (k+1), rw [← right_distrib, ← choose_succ_succ, succ_mul_choose_eq], rw [← nat.sub_eq_of_eq_add e, mul_comm, ← nat.mul_sub_left_distrib, nat.add_sub_add_right] end @[simp] lemma choose_succ_self_right : βˆ€ (n:β„•), (n+1).choose n = n+1 | 0 := rfl | (n+1) := by rw [choose_succ_succ, choose_succ_self_right, choose_self] lemma choose_mul_succ_eq (n k : β„•) : (n.choose k) * (n + 1) = ((n+1).choose k) * (n + 1 - k) := begin induction k with k ih, { simp }, by_cases hk : n < k + 1, { rw [choose_eq_zero_of_lt hk, sub_eq_zero_of_le hk, zero_mul, mul_zero] }, push_neg at hk, replace hk : k + 1 ≀ n + 1 := _root_.le_add_right hk, rw [choose_succ_succ], rw [add_mul, succ_sub_succ], rw [← choose_succ_right_eq], rw [← succ_sub_succ, nat.mul_sub_left_distrib], symmetry, apply nat.add_sub_cancel', exact mul_le_mul_left _ hk, end theorem units_eq_one (u : units β„•) : u = 1 := units.ext $ nat.eq_one_of_dvd_one ⟨u.inv, u.val_inv.symm⟩ theorem add_units_eq_zero (u : add_units β„•) : u = 0 := add_units.ext $ (nat.eq_zero_of_add_eq_zero u.val_neg).1 @[simp] protected theorem is_unit_iff {n : β„•} : is_unit n ↔ n = 1 := iff.intro (assume ⟨u, hu⟩, match n, u, hu, nat.units_eq_one u with _, _, rfl, rfl := rfl end) (assume h, h.symm β–Έ ⟨1, rfl⟩) section find_greatest /-- `find_greatest P b` is the largest `i ≀ bound` such that `P i` holds, or `0` if no such `i` exists -/ protected def find_greatest (P : β„• β†’ Prop) [decidable_pred P] : β„• β†’ β„• | 0 := 0 | (n + 1) := if P (n + 1) then n + 1 else find_greatest n variables {P : β„• β†’ Prop} [decidable_pred P] @[simp] lemma find_greatest_zero : nat.find_greatest P 0 = 0 := rfl @[simp] lemma find_greatest_eq : βˆ€{b}, P b β†’ nat.find_greatest P b = b | 0 h := rfl | (n + 1) h := by simp [nat.find_greatest, h] @[simp] lemma find_greatest_of_not {b} (h : Β¬ P (b + 1)) : nat.find_greatest P (b + 1) = nat.find_greatest P b := by simp [nat.find_greatest, h] lemma find_greatest_spec_and_le : βˆ€{b m}, m ≀ b β†’ P m β†’ P (nat.find_greatest P b) ∧ m ≀ nat.find_greatest P b | 0 m hm hP := have m = 0, from le_antisymm hm (nat.zero_le _), show P 0 ∧ m ≀ 0, from this β–Έ ⟨hP, le_refl _⟩ | (b + 1) m hm hP := begin by_cases h : P (b + 1), { simp [h, hm] }, { have : m β‰  b + 1 := assume this, h $ this β–Έ hP, have : m ≀ b := (le_of_not_gt $ assume h : b + 1 ≀ m, this $ le_antisymm hm h), have : P (nat.find_greatest P b) ∧ m ≀ nat.find_greatest P b := find_greatest_spec_and_le this hP, simp [h, this] } end lemma find_greatest_spec {b} : (βˆƒm, m ≀ b ∧ P m) β†’ P (nat.find_greatest P b) | ⟨m, hmb, hm⟩ := (find_greatest_spec_and_le hmb hm).1 lemma find_greatest_le : βˆ€ {b}, nat.find_greatest P b ≀ b | 0 := le_refl _ | (b + 1) := have nat.find_greatest P b ≀ b + 1, from le_trans find_greatest_le (nat.le_succ b), by by_cases P (b + 1); simp [h, this] lemma le_find_greatest {b m} (hmb : m ≀ b) (hm : P m) : m ≀ nat.find_greatest P b := (find_greatest_spec_and_le hmb hm).2 lemma find_greatest_is_greatest {P : β„• β†’ Prop} [decidable_pred P] {b} : (βˆƒ m, m ≀ b ∧ P m) β†’ βˆ€ k, nat.find_greatest P b < k ∧ k ≀ b β†’ Β¬ P k | ⟨m, hmb, hP⟩ k ⟨hk, hkb⟩ hPk := lt_irrefl k $ lt_of_le_of_lt (le_find_greatest hkb hPk) hk lemma find_greatest_eq_zero {P : β„• β†’ Prop} [decidable_pred P] : βˆ€ {b}, (βˆ€ n ≀ b, Β¬ P n) β†’ nat.find_greatest P b = 0 | 0 h := find_greatest_zero | (n + 1) h := begin have := nat.find_greatest_of_not (h (n + 1) (le_refl _)), rw this, exact find_greatest_eq_zero (assume k hk, h k (le_trans hk $ nat.le_succ _)) end lemma find_greatest_of_ne_zero {P : β„• β†’ Prop} [decidable_pred P] : βˆ€ {b m}, nat.find_greatest P b = m β†’ m β‰  0 β†’ P m | 0 m rfl h := by { have := @find_greatest_zero P _, contradiction } | (b + 1) m rfl h := decidable.by_cases (assume hb : P (b + 1), by { have := find_greatest_eq hb, rw this, exact hb }) (assume hb : Β¬ P (b + 1), find_greatest_of_ne_zero (find_greatest_of_not hb).symm h) end find_greatest section div lemma dvd_div_of_mul_dvd {a b c : β„•} (h : a * b ∣ c) : b ∣ c / a := if ha : a = 0 then by simp [ha] else have ha : 0 < a, from nat.pos_of_ne_zero ha, have h1 : βˆƒ d, c = a * b * d, from h, let ⟨d, hd⟩ := h1 in have hac : a ∣ c, from dvd_of_mul_right_dvd h, have h2 : c / a = b * d, from nat.div_eq_of_eq_mul_right ha (by simpa [mul_assoc] using hd), show βˆƒ d, c / a = b * d, from ⟨d, h2⟩ lemma mul_dvd_of_dvd_div {a b c : β„•} (hab : c ∣ b) (h : a ∣ b / c) : c * a ∣ b := have h1 : βˆƒ d, b / c = a * d, from h, have h2 : βˆƒ e, b = c * e, from hab, let ⟨d, hd⟩ := h1, ⟨e, he⟩ := h2 in have h3 : b = a * d * c, from nat.eq_mul_of_div_eq_left hab hd, show βˆƒ d, b = c * a * d, from ⟨d, by cc⟩ lemma div_mul_div {a b c d : β„•} (hab : b ∣ a) (hcd : d ∣ c) : (a / b) * (c / d) = (a * c) / (b * d) := have exi1 : βˆƒ x, a = b * x, from hab, have exi2 : βˆƒ y, c = d * y, from hcd, if hb : b = 0 then by simp [hb] else have 0 < b, from nat.pos_of_ne_zero hb, if hd : d = 0 then by simp [hd] else have 0 < d, from nat.pos_of_ne_zero hd, begin cases exi1 with x hx, cases exi2 with y hy, rw [hx, hy, nat.mul_div_cancel_left, nat.mul_div_cancel_left], symmetry, apply nat.div_eq_of_eq_mul_left, apply mul_pos, repeat {assumption}, cc end lemma pow_dvd_of_le_of_pow_dvd {p m n k : β„•} (hmn : m ≀ n) (hdiv : p ^ n ∣ k) : p ^ m ∣ k := have p ^ m ∣ p ^ n, from pow_dvd_pow _ hmn, dvd_trans this hdiv lemma dvd_of_pow_dvd {p k m : β„•} (hk : 1 ≀ k) (hpk : p^k ∣ m) : p ∣ m := by rw ←nat.pow_one p; exact pow_dvd_of_le_of_pow_dvd hk hpk lemma eq_of_dvd_of_div_eq_one {a b : β„•} (w : a ∣ b) (h : b / a = 1) : a = b := by rw [←nat.div_mul_cancel w, h, one_mul] lemma eq_zero_of_dvd_of_div_eq_zero {a b : β„•} (w : a ∣ b) (h : b / a = 0) : b = 0 := by rw [←nat.div_mul_cancel w, h, zero_mul] /-- If a small natural number is divisible by a larger natural number, the small number is zero. -/ lemma eq_zero_of_dvd_of_lt {a b : β„•} (w : a ∣ b) (h : b < a) : b = 0 := nat.eq_zero_of_dvd_of_div_eq_zero w ((nat.div_eq_zero_iff (lt_of_le_of_lt (zero_le b) h)).elim_right h) lemma div_le_div_left {a b c : β„•} (h₁ : c ≀ b) (hβ‚‚ : 0 < c) : a / b ≀ a / c := (nat.le_div_iff_mul_le _ _ hβ‚‚).2 $ le_trans (mul_le_mul_left _ h₁) (div_mul_le_self _ _) lemma div_eq_self {a b : β„•} : a / b = a ↔ a = 0 ∨ b = 1 := begin split, { intro, cases b, { simp * at * }, { cases b, { right, refl }, { left, have : a / (b + 2) ≀ a / 2 := div_le_div_left (by simp) dec_trivial, refine eq_zero_of_le_half _, simp * at * } } }, { rintros (rfl|rfl); simp } end end div lemma exists_eq_add_of_le : βˆ€ {m n : β„•}, m ≀ n β†’ βˆƒ k : β„•, n = m + k | 0 0 h := ⟨0, by simp⟩ | 0 (n+1) h := ⟨n+1, by simp⟩ | (m+1) (n+1) h := let ⟨k, hk⟩ := exists_eq_add_of_le (nat.le_of_succ_le_succ h) in ⟨k, by simp [hk, add_comm, add_left_comm]⟩ lemma exists_eq_add_of_lt : βˆ€ {m n : β„•}, m < n β†’ βˆƒ k : β„•, n = m + k + 1 | 0 0 h := false.elim $ lt_irrefl _ h | 0 (n+1) h := ⟨n, by simp⟩ | (m+1) (n+1) h := let ⟨k, hk⟩ := exists_eq_add_of_le (nat.le_of_succ_le_succ h) in ⟨k, by simp [hk]⟩ lemma with_bot.add_eq_zero_iff : βˆ€ {n m : with_bot β„•}, n + m = 0 ↔ n = 0 ∧ m = 0 | none m := iff_of_false dec_trivial (Ξ» h, absurd h.1 dec_trivial) | n none := iff_of_false (by cases n; exact dec_trivial) (Ξ» h, absurd h.2 dec_trivial) | (some n) (some m) := show (n + m : with_bot β„•) = (0 : β„•) ↔ (n : with_bot β„•) = (0 : β„•) ∧ (m : with_bot β„•) = (0 : β„•), by rw [← with_bot.coe_add, with_bot.coe_eq_coe, with_bot.coe_eq_coe, with_bot.coe_eq_coe, add_eq_zero_iff' (nat.zero_le _) (nat.zero_le _)] lemma with_bot.add_eq_one_iff : βˆ€ {n m : with_bot β„•}, n + m = 1 ↔ (n = 0 ∧ m = 1) ∨ (n = 1 ∧ m = 0) | none none := dec_trivial | none (some m) := dec_trivial | (some n) none := iff_of_false dec_trivial (Ξ» h, h.elim (Ξ» h, absurd h.2 dec_trivial) (Ξ» h, absurd h.2 dec_trivial)) | (some n) (some 0) := by erw [with_bot.coe_eq_coe, with_bot.coe_eq_coe, with_bot.coe_eq_coe, with_bot.coe_eq_coe]; simp | (some n) (some (m + 1)) := by erw [with_bot.coe_eq_coe, with_bot.coe_eq_coe, with_bot.coe_eq_coe, with_bot.coe_eq_coe, with_bot.coe_eq_coe]; simp [nat.add_succ, nat.succ_inj', nat.succ_ne_zero] -- induction /-- Induction principle starting at a non-zero number. For maps to a `Sort*` see `le_rec_on`. -/ @[elab_as_eliminator] lemma le_induction {P : nat β†’ Prop} {m} (h0 : P m) (h1 : βˆ€ n, m ≀ n β†’ P n β†’ P (n + 1)) : βˆ€ n, m ≀ n β†’ P n := by apply nat.less_than_or_equal.rec h0; exact h1 /-- Decreasing induction: if `P (k+1)` implies `P k`, then `P n` implies `P m` for all `m ≀ n`. Also works for functions to `Sort*`. -/ @[elab_as_eliminator] def decreasing_induction {P : β„• β†’ Sort*} (h : βˆ€n, P (n+1) β†’ P n) {m n : β„•} (mn : m ≀ n) (hP : P n) : P m := le_rec_on mn (Ξ» k ih hsk, ih $ h k hsk) (Ξ» h, h) hP @[simp] lemma decreasing_induction_self {P : β„• β†’ Sort*} (h : βˆ€n, P (n+1) β†’ P n) {n : β„•} (nn : n ≀ n) (hP : P n) : (decreasing_induction h nn hP : P n) = hP := by { dunfold decreasing_induction, rw [le_rec_on_self] } lemma decreasing_induction_succ {P : β„• β†’ Sort*} (h : βˆ€n, P (n+1) β†’ P n) {m n : β„•} (mn : m ≀ n) (msn : m ≀ n + 1) (hP : P (n+1)) : (decreasing_induction h msn hP : P m) = decreasing_induction h mn (h n hP) := by { dunfold decreasing_induction, rw [le_rec_on_succ] } @[simp] lemma decreasing_induction_succ' {P : β„• β†’ Sort*} (h : βˆ€n, P (n+1) β†’ P n) {m : β„•} (msm : m ≀ m + 1) (hP : P (m+1)) : (decreasing_induction h msm hP : P m) = h m hP := by { dunfold decreasing_induction, rw [le_rec_on_succ'] } lemma decreasing_induction_trans {P : β„• β†’ Sort*} (h : βˆ€n, P (n+1) β†’ P n) {m n k : β„•} (mn : m ≀ n) (nk : n ≀ k) (hP : P k) : (decreasing_induction h (le_trans mn nk) hP : P m) = decreasing_induction h mn (decreasing_induction h nk hP) := by { induction nk with k nk ih, rw [decreasing_induction_self], rw [decreasing_induction_succ h (le_trans mn nk), ih, decreasing_induction_succ] } lemma decreasing_induction_succ_left {P : β„• β†’ Sort*} (h : βˆ€n, P (n+1) β†’ P n) {m n : β„•} (smn : m + 1 ≀ n) (mn : m ≀ n) (hP : P n) : (decreasing_induction h mn hP : P m) = h m (decreasing_induction h smn hP) := by { rw [subsingleton.elim mn (le_trans (le_succ m) smn), decreasing_induction_trans, decreasing_induction_succ'] } end nat namespace function theorem injective.iterate {Ξ± : Type u} {op : Ξ± β†’ Ξ±} (Hinj : injective op) : βˆ€ n, injective (op^[n]) := nat.iterate_ind op Hinj injective_id $ Ξ» _ _, injective.comp theorem surjective.iterate {Ξ± : Type u} {op : Ξ± β†’ Ξ±} (Hinj : surjective op) : βˆ€ n, surjective (op^[n]) := nat.iterate_ind op Hinj surjective_id $ Ξ» _ _, surjective.comp theorem bijective.iterate {Ξ± : Type u} {op : Ξ± β†’ Ξ±} (Hinj : bijective op) : βˆ€ n, bijective (op^[n]) := nat.iterate_ind op Hinj bijective_id $ Ξ» _ _, bijective.comp end function namespace monoid_hom variables {M : Type*} {G : Type*} [monoid M] [group G] @[simp, to_additive] theorem iterate_map_one (f : M β†’* M) (n : β„•) : f^[n] 1 = 1 := nat.iterateβ‚€ f.map_one @[simp, to_additive] theorem iterate_map_mul (f : M β†’* M) (n : β„•) (x y) : f^[n] (x * y) = (f^[n] x) * (f^[n] y) := nat.iterateβ‚‚ f.map_mul @[simp, to_additive] theorem iterate_map_inv (f : G β†’* G) (n : β„•) (x) : f^[n] (x⁻¹) = (f^[n] x)⁻¹ := nat.iterate₁ f.map_inv @[simp] theorem iterate_map_sub {A : Type*} [add_group A] (f : A β†’+ A) (n : β„•) (x y) : f^[n] (x - y) = (f^[n] x) - (f^[n] y) := nat.iterateβ‚‚ f.map_sub end monoid_hom namespace ring_hom variables {R : Type*} [semiring R] {S : Type*} [ring S] @[simp] theorem iterate_map_one (f : R β†’+* R) (n : β„•) : f^[n] 1 = 1 := nat.iterateβ‚€ f.map_one @[simp] theorem iterate_map_zero (f : R β†’+* R) (n : β„•) : f^[n] 0 = 0 := nat.iterateβ‚€ f.map_zero @[simp] theorem iterate_map_mul (f : R β†’+* R) (n : β„•) (x y) : f^[n] (x * y) = (f^[n] x) * (f^[n] y) := nat.iterateβ‚‚ f.map_mul @[simp] theorem iterate_map_add (f : R β†’+* R) (n : β„•) (x y) : f^[n] (x + y) = (f^[n] x) + (f^[n] y) := nat.iterateβ‚‚ f.map_add @[simp] theorem iterate_map_neg (f : S β†’+* S) (n : β„•) (x) : f^[n] (-x) = -(f^[n] x) := nat.iterate₁ f.map_neg @[simp] theorem iterate_map_sub (f : S β†’+* S) (n : β„•) (x y) : f^[n] (x - y) = (f^[n] x) - (f^[n] y) := nat.iterateβ‚‚ f.map_sub end ring_hom
bd43567e635269c8383eea9cc61fd91adefdc8f6
7afc29faca4febb6e5005c20aa4aa5c3df5cf35c
/src/thm.lean
25f4def60462def4dee37522cbd8bac1fab72cd4
[ "MIT" ]
permissive
Piwry/Proof-of-Surreal
ad2883027e275050b43a578c5513ae3fe350515b
6b92baf2382ac23dd0d700f5c958aa910ad4b754
refs/heads/master
1,670,521,185,736
1,599,657,591,000
1,599,657,591,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,647
lean
import defs single finite branch grow data.list open classical lemma branch_to_dense : βˆ€ l, (βˆ€ b, height b = height_bound l β†’ ⟨b⟩ β†’ (l ↦ b)) β†’ (βˆ€ t, height_bound l <= height t β†’ (l ↦ t)):= begin intros l h1 t h2, cases (kernel_lemma _ _ (height_bound_ge1 _) h2), have ht1 : _ := h.left, have ht2 : _ := h.right.left, have ht3 : _ := h.right.right, have ht : (l ↦ w), apply h1, rewrite ht2, assumption, cases (grow_list_exists_one _ _ ht), apply exists_one_grow_list, existsi w_1, split, exact h_1.left, apply grow_trans, exact h_1.right, exact ht3 end lemma branch_to_complete : βˆ€ l, (βˆ€ b, height b = height_bound l β†’ ⟨b⟩ β†’ (l ↦ b)) β†’ almost_complete l := begin intros, apply almost_complete.cmp_rule, cases (bounded_height_list (height_bound l)), existsi w, intros t h', have ht : _ := (branch_to_dense l a t), have ht' : Β¬(height_bound l ≀ height t), tauto, apply h, omega end lemma complete_to_week_dense : βˆ€ l, almost_complete l β†’ (βˆƒ h, h β‰₯ 1 ∧ (βˆ€ t, height t = h β†’ (l ↦ t))) := begin intros l h, cases h, cases h_a, existsi (height_bound h_a_w)+1, split,omega, intros t h1, classical, by_contra, have ht : _ := h_a_h _ a, have ht' : _ := height_bound_correct h_a_w _ ht, rewrite h1 at ht', exact nat.not_succ_le_self (height_bound h_a_w) ht' end lemma complete_to_branch : βˆ€ l, almost_complete l β†’ (βˆ€ b, height b = height_bound l β†’ ⟨b⟩ β†’ (l ↦ b)) := begin intros l h1 b h2 h3, classical, by_contra, cases complete_to_week_dense l h1, cases le_or_gt w (height b), begin cases (kernel_lemma b w h.left h_1), have ht : _ := h.right w_1 (eq.symm (h_2.right.left)), apply a, cases grow_list_exists_one l w_1 ht, have ht' : _ := grow_trans _ _ _ h_3.right h_2.right.right, apply exists_one_grow_list, existsi w_2, split, repeat {tauto} end, begin have ht' : height b + (w - height b) = w, omega, cases (grow_high_bintree b h3 (w - height b)), cases h_2.right, have h_2' : height w_1 = w, omega, have h' : _ := h.right w_1 h_2', apply a, cases grow_list_exists_one l w_1 h', have h_height : _ := height_bound_correct l w_2 h_3.left, rewrite h2.symm at h_height, have h_almost : _ := (branch_prefix w_1 w_2 b h_2.left h_3.right left h_height), apply exists_one_grow_list, existsi w_2, tauto end end theorem complete_iff_branch : βˆ€ l, almost_complete l ↔ (βˆ€ b, height b = height_bound l β†’ ⟨b⟩ β†’ (l ↦ b)) := begin intros, split, exact complete_to_branch _, exact branch_to_complete _ end
3a6e6105970fade857386f52761de881dda55b54
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/lake/Lake/Config/Opaque.lean
93b708c09184230c0772e7465781ec571c8927f2
[ "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
556
lean
/- Copyright (c) 2021 Mac Malone. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mac Malone -/ import Lake.Util.Name import Lake.Util.Opaque namespace Lake /-- Opaque reference to a `Package` used for forward declaration. -/ declare_opaque_type OpaquePackage /-- Opaque reference to a `Workspace` used for forward declaration. -/ declare_opaque_type OpaqueWorkspace /-- Opaque reference to a `TargetConfig` used for forward declaration. -/ declare_opaque_type OpaqueTargetConfig (pkgName name : Name)
e9cbed21d950f9841c85c3c8e0299bfc007dc5fb
1446f520c1db37e157b631385707cc28a17a595e
/src/Init/Lean/Meta/Basic.lean
4b5664efcd86d89d01c69d6ace0efd6dc15f6ace
[ "Apache-2.0" ]
permissive
bdbabiak/lean4
cab06b8a2606d99a168dd279efdd404edb4e825a
3f4d0d78b2ce3ef541cb643bbe21496bd6b057ac
refs/heads/master
1,615,045,275,530
1,583,793,696,000
1,583,793,696,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
32,423
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import Init.Control.Reader import Init.Lean.Data.LOption import Init.Lean.Environment import Init.Lean.Class import Init.Lean.ReducibilityAttrs import Init.Lean.Util.Trace import Init.Lean.Util.RecDepth import Init.Lean.Meta.Exception import Init.Lean.Meta.DiscrTreeTypes import Init.Lean.Eval /- This module provides four (mutually dependent) goodies that are needed for building the elaborator and tactic frameworks. 1- Weak head normal form computation with support for metavariables and transparency modes. 2- Definitionally equality checking with support for metavariables (aka unification modulo definitional equality). 3- Type inference. 4- Type class resolution. They are packed into the MetaM monad. -/ namespace Lean namespace Meta inductive TransparencyMode | all | default | reducible namespace TransparencyMode instance : Inhabited TransparencyMode := ⟨TransparencyMode.default⟩ def beq : TransparencyMode β†’ TransparencyMode β†’ Bool | all, all => true | default, default => true | reducible, reducible => true | _, _ => false instance : HasBeq TransparencyMode := ⟨beq⟩ def hash : TransparencyMode β†’ USize | all => 7 | default => 11 | reducible => 13 instance : Hashable TransparencyMode := ⟨hash⟩ def lt : TransparencyMode β†’ TransparencyMode β†’ Bool | reducible, default => true | reducible, all => true | default, all => true | _, _ => false end TransparencyMode structure Config := (opts : Options := {}) (foApprox : Bool := false) (ctxApprox : Bool := false) (quasiPatternApprox : Bool := false) /- When `constApprox` is set to true, we solve `?m t =?= c` using `?m := fun _ => c` when `?m t` is not a higher-order pattern and `c` is not an application as -/ (constApprox : Bool := false) /- When the following flag is set, `isDefEq` throws the exeption `Exeption.isDefEqStuck` whenever it encounters a constraint `?m ... =?= t` where `?m` is read only. This feature is useful for type class resolution where we may want to notify the caller that the TC problem may be solveable later after it assigns `?m`. -/ (isDefEqStuckEx : Bool := false) (debug : Bool := false) (transparency : TransparencyMode := TransparencyMode.default) structure ParamInfo := (implicit : Bool := false) (instImplicit : Bool := false) (hasFwdDeps : Bool := false) (backDeps : Array Nat := #[]) instance ParamInfo.inhabited : Inhabited ParamInfo := ⟨{}⟩ structure FunInfo := (paramInfo : Array ParamInfo := #[]) (resultDeps : Array Nat := #[]) structure InfoCacheKey := (transparency : TransparencyMode) (expr : Expr) (nargs? : Option Nat) namespace InfoCacheKey instance : Inhabited InfoCacheKey := ⟨⟨arbitrary _, arbitrary _, arbitrary _⟩⟩ instance : Hashable InfoCacheKey := ⟨fun ⟨transparency, expr, nargs⟩ => mixHash (hash transparency) $ mixHash (hash expr) (hash nargs)⟩ instance : HasBeq InfoCacheKey := ⟨fun ⟨t₁, e₁, nβ‚βŸ© ⟨tβ‚‚, eβ‚‚, nβ‚‚βŸ© => t₁ == tβ‚‚ && n₁ == nβ‚‚ && e₁ == eβ‚‚βŸ© end InfoCacheKey structure Cache := (inferType : PersistentExprStructMap Expr := {}) (funInfo : PersistentHashMap InfoCacheKey FunInfo := {}) (synthInstance : PersistentHashMap Expr (Option Expr) := {}) structure Context := (config : Config := {}) (lctx : LocalContext := {}) (localInstances : LocalInstances := #[]) (currRecDepth : Nat) (maxRecDepth : Nat) structure PostponedEntry := (lhs : Level) (rhs : Level) structure State := (env : Environment) (mctx : MetavarContext := {}) (cache : Cache := {}) (ngen : NameGenerator := {}) (traceState : TraceState := {}) (postponed : PersistentArray PostponedEntry := {}) abbrev MetaM := ReaderT Context (EStateM Exception State) instance MetaM.inhabited {Ξ±} : Inhabited (MetaM Ξ±) := ⟨fun c s => EStateM.Result.error (arbitrary _) s⟩ @[inline] def withIncRecDepth {Ξ±} (x : MetaM Ξ±) : MetaM Ξ± := do ctx ← read; when (ctx.currRecDepth == ctx.maxRecDepth) $ throw $ Exception.other maxRecDepthErrorMessage; adaptReader (fun (ctx : Context) => { currRecDepth := ctx.currRecDepth + 1, .. ctx }) x @[inline] def getLCtx : MetaM LocalContext := do ctx ← read; pure ctx.lctx @[inline] def getLocalInstances : MetaM LocalInstances := do ctx ← read; pure ctx.localInstances @[inline] def getConfig : MetaM Config := do ctx ← read; pure ctx.config @[inline] def getMCtx : MetaM MetavarContext := do s ← get; pure s.mctx @[inline] def getEnv : MetaM Environment := do s ← get; pure s.env def mkWHNFRef : IO (IO.Ref (Expr β†’ MetaM Expr)) := IO.mkRef $ fun _ => throw $ Exception.other "whnf implementation was not set" @[init mkWHNFRef] def whnfRef : IO.Ref (Expr β†’ MetaM Expr) := arbitrary _ def mkInferTypeRef : IO (IO.Ref (Expr β†’ MetaM Expr)) := IO.mkRef $ fun _ => throw $ Exception.other "inferType implementation was not set" @[init mkInferTypeRef] def inferTypeRef : IO.Ref (Expr β†’ MetaM Expr) := arbitrary _ def mkIsExprDefEqAuxRef : IO (IO.Ref (Expr β†’ Expr β†’ MetaM Bool)) := IO.mkRef $ fun _ _ => throw $ Exception.other "isDefEq implementation was not set" @[init mkIsExprDefEqAuxRef] def isExprDefEqAuxRef : IO.Ref (Expr β†’ Expr β†’ MetaM Bool) := arbitrary _ def mkSynthPendingRef : IO (IO.Ref (Expr β†’ MetaM Bool)) := IO.mkRef $ fun _ => pure false @[init mkSynthPendingRef] def synthPendingRef : IO.Ref (Expr β†’ MetaM Bool) := arbitrary _ structure MetaExtState := (whnf : Expr β†’ MetaM Expr) (inferType : Expr β†’ MetaM Expr) (isDefEqAux : Expr β†’ Expr β†’ MetaM Bool) (synthPending : Expr β†’ MetaM Bool) instance MetaExtState.inhabited : Inhabited MetaExtState := ⟨{ whnf := arbitrary _, inferType := arbitrary _, isDefEqAux := arbitrary _, synthPending := arbitrary _ }⟩ def mkMetaExtension : IO (EnvExtension MetaExtState) := registerEnvExtension $ do whnf ← whnfRef.get; inferType ← inferTypeRef.get; isDefEqAux ← isExprDefEqAuxRef.get; synthPending ← synthPendingRef.get; pure { whnf := whnf, inferType := inferType, isDefEqAux := isDefEqAux, synthPending := synthPending } @[init mkMetaExtension] constant metaExt : EnvExtension MetaExtState := arbitrary _ def whnf (e : Expr) : MetaM Expr := withIncRecDepth $ do env ← getEnv; (metaExt.getState env).whnf e def whnfForall (e : Expr) : MetaM Expr := do e' ← whnf e; if e'.isForall then pure e' else pure e def inferType (e : Expr) : MetaM Expr := withIncRecDepth $ do env ← getEnv; (metaExt.getState env).inferType e def isExprDefEqAux (t s : Expr) : MetaM Bool := withIncRecDepth $ do env ← getEnv; (metaExt.getState env).isDefEqAux t s def synthPending (e : Expr) : MetaM Bool := withIncRecDepth $ do env ← getEnv; (metaExt.getState env).synthPending e def mkFreshId : MetaM Name := do s ← get; let id := s.ngen.curr; modify $ fun s => { ngen := s.ngen.next, .. s }; pure id def mkFreshExprMVarAt (lctx : LocalContext) (localInsts : LocalInstances) (type : Expr) (userName : Name := Name.anonymous) (kind : MetavarKind := MetavarKind.natural) : MetaM Expr := do mvarId ← mkFreshId; modify $ fun s => { mctx := s.mctx.addExprMVarDecl mvarId userName lctx localInsts type kind, .. s }; pure $ mkMVar mvarId def mkFreshExprMVar (type : Expr) (userName : Name := Name.anonymous) (kind : MetavarKind := MetavarKind.natural) : MetaM Expr := do lctx ← getLCtx; localInsts ← getLocalInstances; mkFreshExprMVarAt lctx localInsts type userName kind def mkFreshLevelMVar : MetaM Level := do mvarId ← mkFreshId; modify $ fun s => { mctx := s.mctx.addLevelMVarDecl mvarId, .. s}; pure $ mkLevelMVar mvarId @[inline] def throwEx {Ξ±} (f : ExceptionContext β†’ Exception) : MetaM Ξ± := do ctx ← read; s ← get; throw (f {env := s.env, mctx := s.mctx, lctx := ctx.lctx, opts := ctx.config.opts }) def throwBug {Ξ±} (b : Bug) : MetaM Ξ± := throwEx $ Exception.bug b /-- Execute `x` only in debugging mode. -/ @[inline] private def whenDebugging {Ξ±} (x : MetaM Ξ±) : MetaM Unit := do ctx ← read; when ctx.config.debug (do x; pure ()) @[inline] def shouldReduceAll : MetaM Bool := do ctx ← read; pure $ ctx.config.transparency == TransparencyMode.all @[inline] def shouldReduceReducibleOnly : MetaM Bool := do ctx ← read; pure $ ctx.config.transparency == TransparencyMode.reducible @[inline] def getTransparency : MetaM TransparencyMode := do ctx ← read; pure $ ctx.config.transparency @[inline] def getOptions : MetaM Options := do ctx ← read; pure ctx.config.opts -- Remark: wanted to use `private`, but in C++ parser, `private` declarations do not shadow outer public ones. -- TODO: fix this bug @[inline] def isReducible (constName : Name) : MetaM Bool := do env ← getEnv; pure $ isReducible env constName @[inline] def withConfig {Ξ±} (f : Config β†’ Config) (x : MetaM Ξ±) : MetaM Ξ± := adaptReader (fun (ctx : Context) => { config := f ctx.config, .. ctx }) x /-- While executing `x`, ensure the given transparency mode is used. -/ @[inline] def withTransparency {Ξ±} (mode : TransparencyMode) (x : MetaM Ξ±) : MetaM Ξ± := withConfig (fun config => { transparency := mode, .. config }) x @[inline] def withReducible {Ξ±} (x : MetaM Ξ±) : MetaM Ξ± := withTransparency TransparencyMode.reducible x @[inline] def withAtLeastTransparency {Ξ±} (mode : TransparencyMode) (x : MetaM Ξ±) : MetaM Ξ± := withConfig (fun config => let oldMode := config.transparency; let mode := if oldMode.lt mode then mode else oldMode; { transparency := mode, .. config }) x def getMVarDecl (mvarId : MVarId) : MetaM MetavarDecl := do mctx ← getMCtx; match mctx.findDecl? mvarId with | some d => pure d | none => throwEx $ Exception.unknownExprMVar mvarId def setMVarKind (mvarId : MVarId) (kind : MetavarKind) : MetaM Unit := modify $ fun s => { mctx := s.mctx.setMVarKind mvarId kind, .. s} def isReadOnlyExprMVar (mvarId : MVarId) : MetaM Bool := do mvarDecl ← getMVarDecl mvarId; mctx ← getMCtx; pure $ mvarDecl.depth != mctx.depth def isReadOnlyOrSyntheticOpaqueExprMVar (mvarId : MVarId) : MetaM Bool := do mvarDecl ← getMVarDecl mvarId; match mvarDecl.kind with | MetavarKind.syntheticOpaque => pure true | _ => do mctx ← getMCtx; pure $ mvarDecl.depth != mctx.depth def isReadOnlyLevelMVar (mvarId : MVarId) : MetaM Bool := do mctx ← getMCtx; match mctx.findLevelDepth? mvarId with | some depth => pure $ depth != mctx.depth | _ => throwEx $ Exception.unknownLevelMVar mvarId def renameMVar (mvarId : MVarId) (newUserName : Name) : MetaM Unit := modify $ fun s => { mctx := s.mctx.renameMVar mvarId newUserName, .. s } @[inline] def isExprMVarAssigned (mvarId : MVarId) : MetaM Bool := do mctx ← getMCtx; pure $ mctx.isExprAssigned mvarId @[inline] def getExprMVarAssignment? (mvarId : MVarId) : MetaM (Option Expr) := do mctx ← getMCtx; pure (mctx.getExprAssignment? mvarId) def assignExprMVar (mvarId : MVarId) (val : Expr) : MetaM Unit := do whenDebugging $ whenM (isExprMVarAssigned mvarId) $ throwBug $ Bug.overwritingExprMVar mvarId; modify $ fun s => { mctx := s.mctx.assignExpr mvarId val, .. s } def isDelayedAssigned (mvarId : MVarId) : MetaM Bool := do mctx ← getMCtx; pure $ mctx.isDelayedAssigned mvarId def hasAssignableMVar (e : Expr) : MetaM Bool := do mctx ← getMCtx; pure $ mctx.hasAssignableMVar e def dbgTrace {Ξ±} [HasToString Ξ±] (a : Ξ±) : MetaM Unit := _root_.dbgTrace (toString a) $ fun _ => pure () @[inline] private def getTraceState : MetaM TraceState := do s ← get; pure s.traceState def addContext (msg : MessageData) : MetaM MessageData := do ctx ← read; s ← get; pure $ MessageData.withContext { env := s.env, mctx := s.mctx, lctx := ctx.lctx, opts := ctx.config.opts } msg instance tracer : SimpleMonadTracerAdapter MetaM := { getOptions := getOptions, getTraceState := getTraceState, addContext := addContext, modifyTraceState := fun f => modify $ fun s => { traceState := f s.traceState, .. s } } def getConstAux (constName : Name) (exception? : Bool) : MetaM (Option ConstantInfo) := do env ← getEnv; match env.find? constName with | some (info@(ConstantInfo.thmInfo _)) => condM shouldReduceAll (pure (some info)) (pure none) | some (info@(ConstantInfo.defnInfo _)) => condM shouldReduceReducibleOnly (condM (isReducible constName) (pure (some info)) (pure none)) (pure (some info)) | some info => pure (some info) | none => if exception? then throwEx $ Exception.unknownConst constName else pure none @[inline] def getConst (constName : Name) : MetaM (Option ConstantInfo) := getConstAux constName true @[inline] def getConstNoEx (constName : Name) : MetaM (Option ConstantInfo) := getConstAux constName false def getConstInfo (constName : Name) : MetaM ConstantInfo := do env ← getEnv; match env.find? constName with | some info => pure info | none => throwEx $ Exception.unknownConst constName def getLocalDecl (fvarId : FVarId) : MetaM LocalDecl := do lctx ← getLCtx; match lctx.find? fvarId with | some d => pure d | none => throwEx $ Exception.unknownFVar fvarId def getFVarLocalDecl (fvar : Expr) : MetaM LocalDecl := getLocalDecl fvar.fvarId! def instantiateMVars (e : Expr) : MetaM Expr := if e.hasMVar then modifyGet $ fun s => let (e, mctx) := s.mctx.instantiateMVars e; (e, { mctx := mctx, .. s }) else pure e @[inline] private def liftMkBindingM {Ξ±} (x : MetavarContext.MkBindingM Ξ±) : MetaM Ξ± := fun ctx s => match x ctx.lctx { mctx := s.mctx, ngen := s.ngen } with | EStateM.Result.ok e newS => EStateM.Result.ok e { mctx := newS.mctx, ngen := newS.ngen, .. s} | EStateM.Result.error (MetavarContext.MkBinding.Exception.revertFailure mctx lctx toRevert decl) newS => EStateM.Result.error (Exception.revertFailure toRevert decl { lctx := lctx, mctx := mctx, env := s.env, opts := ctx.config.opts }) { mctx := newS.mctx, ngen := newS.ngen, .. s } def mkForall (xs : Array Expr) (e : Expr) : MetaM Expr := if xs.isEmpty then pure e else liftMkBindingM $ MetavarContext.mkForall xs e def mkLambda (xs : Array Expr) (e : Expr) : MetaM Expr := if xs.isEmpty then pure e else liftMkBindingM $ MetavarContext.mkLambda xs e def mkForallUsedOnly (xs : Array Expr) (e : Expr) : MetaM (Expr Γ— Nat) := if xs.isEmpty then pure (e, 0) else liftMkBindingM $ MetavarContext.mkForallUsedOnly xs e def elimMVarDeps (xs : Array Expr) (e : Expr) (preserveOrder : Bool := false) : MetaM Expr := if xs.isEmpty then pure e else liftMkBindingM $ MetavarContext.elimMVarDeps xs e preserveOrder /-- Save cache, execute `x`, restore cache -/ @[inline] def savingCache {Ξ±} (x : MetaM Ξ±) : MetaM Ξ± := do s ← get; let savedCache := s.cache; finally x (modify $ fun s => { cache := savedCache, .. s }) def isClassQuickConst (constName : Name) : MetaM (LOption Name) := do env ← getEnv; if isClass env constName then pure (LOption.some constName) else do cinfo? ← getConst constName; match cinfo? with | some _ => pure LOption.undef | none => pure LOption.none partial def isClassQuick : Expr β†’ MetaM (LOption Name) | Expr.bvar _ _ => pure LOption.none | Expr.lit _ _ => pure LOption.none | Expr.fvar _ _ => pure LOption.none | Expr.sort _ _ => pure LOption.none | Expr.lam _ _ _ _ => pure LOption.none | Expr.letE _ _ _ _ _ => pure LOption.undef | Expr.proj _ _ _ _ => pure LOption.undef | Expr.forallE _ _ b _ => isClassQuick b | Expr.mdata _ e _ => isClassQuick e | Expr.const n _ _ => isClassQuickConst n | Expr.mvar mvarId _ => do val? ← getExprMVarAssignment? mvarId; match val? with | some val => isClassQuick val | none => pure LOption.none | Expr.app f _ _ => match f.getAppFn with | Expr.const n _ _ => isClassQuickConst n | Expr.lam _ _ _ _ => pure LOption.undef | _ => pure LOption.none | Expr.localE _ _ _ _ => unreachable! /-- Reset `synthInstance` cache, execute `x`, and restore cache -/ @[inline] def resettingSynthInstanceCache {Ξ±} (x : MetaM Ξ±) : MetaM Ξ± := do s ← get; let savedSythInstance := s.cache.synthInstance; modify $ fun s => { cache := { synthInstance := {}, .. s.cache }, .. s }; finally x (modify $ fun s => { cache := { synthInstance := savedSythInstance, .. s.cache }, .. s }) /-- Add entry `{ className := className, fvar := fvar }` to localInstances, and then execute continuation `k`. It resets the type class cache using `resettingSynthInstanceCache`. -/ @[inline] def withNewLocalInstance {Ξ±} (className : Name) (fvar : Expr) (k : MetaM Ξ±) : MetaM Ξ± := resettingSynthInstanceCache $ adaptReader (fun (ctx : Context) => { localInstances := ctx.localInstances.push { className := className, fvar := fvar }, .. ctx }) k /-- `withNewLocalInstances isClassExpensive fvars j k` updates the vector or local instances using free variables `fvars[j] ... fvars.back`, and execute `k`. - `isClassExpensive` is defined later. - The type class chache is reset whenever a new local instance is found. - `isClassExpensive` uses `whnf` which depends (indirectly) on the set of local instances. Thus, each new local instance requires a new `resettingSynthInstanceCache`. -/ @[specialize] partial def withNewLocalInstances {Ξ±} (isClassExpensive : Expr β†’ MetaM (Option Name)) (fvars : Array Expr) : Nat β†’ MetaM Ξ± β†’ MetaM Ξ± | i, k => if h : i < fvars.size then do let fvar := fvars.get ⟨i, h⟩; decl ← getFVarLocalDecl fvar; c? ← isClassQuick decl.type; match c? with | LOption.none => withNewLocalInstances (i+1) k | LOption.undef => do c? ← isClassExpensive decl.type; match c? with | none => withNewLocalInstances (i+1) k | some c => withNewLocalInstance c fvar $ withNewLocalInstances (i+1) k | LOption.some c => withNewLocalInstance c fvar $ withNewLocalInstances (i+1) k else k /-- `forallTelescopeAux whnf k lctx fvars j type` Remarks: - `lctx` is the `MetaM` local context exteded with the declaration for `fvars`. - `type` is the type we are computing the telescope for. It contains only dangling bound variables in the range `[j, fvars.size)` - if `reducing? == true` and `type` is not `forallE`, we use `whnf`. - when `type` is not a `forallE` nor it can't be reduced to one, we excute the continuation `k`. Here is an example that demonstrates the `reducing?`. Suppose we have ``` abbrev StateM s a := s -> Prod a s ``` Now, assume we are trying to build the telescope for ``` forall (x : Nat), StateM Int Bool ``` if `reducing? == true`, the function executes `k #[(x : Nat) (s : Int)] Bool`. if `reducing? == false`, the function executes `k #[(x : Nat)] (StateM Int Bool)` if `maxFVars?` is `some max`, then we interrupt the telescope construction when `fvars.size == max` -/ @[specialize] private partial def forallTelescopeReducingAuxAux {Ξ±} (isClassExpensive : Expr β†’ MetaM (Option Name)) (reducing? : Bool) (maxFVars? : Option Nat) (k : Array Expr β†’ Expr β†’ MetaM Ξ±) : LocalContext β†’ Array Expr β†’ Nat β†’ Expr β†’ MetaM Ξ± | lctx, fvars, j, type@(Expr.forallE n d b c) => do let process : Unit β†’ MetaM Ξ± := fun _ => do { let d := d.instantiateRevRange j fvars.size fvars; fvarId ← mkFreshId; let lctx := lctx.mkLocalDecl fvarId n d c.binderInfo; let fvar := mkFVar fvarId; let fvars := fvars.push fvar; forallTelescopeReducingAuxAux lctx fvars j b }; match maxFVars? with | none => process () | some maxFVars => if fvars.size < maxFVars then process () else let type := type.instantiateRevRange j fvars.size fvars; adaptReader (fun (ctx : Context) => { lctx := lctx, .. ctx }) $ withNewLocalInstances isClassExpensive fvars j $ k fvars type | lctx, fvars, j, type => let type := type.instantiateRevRange j fvars.size fvars; adaptReader (fun (ctx : Context) => { lctx := lctx, .. ctx }) $ withNewLocalInstances isClassExpensive fvars j $ if reducing? then do newType ← whnf type; if newType.isForall then forallTelescopeReducingAuxAux lctx fvars fvars.size newType else k fvars type else k fvars type /- We need this auxiliary definition because it depends on `isClassExpensive`, and `isClassExpensive` depends on it. -/ @[specialize] private def forallTelescopeReducingAux {Ξ±} (isClassExpensive : Expr β†’ MetaM (Option Name)) (type : Expr) (maxFVars? : Option Nat) (k : Array Expr β†’ Expr β†’ MetaM Ξ±) : MetaM Ξ± := do newType ← whnf type; if newType.isForall then do lctx ← getLCtx; forallTelescopeReducingAuxAux isClassExpensive true maxFVars? k lctx #[] 0 newType else k #[] type partial def isClassExpensive : Expr β†’ MetaM (Option Name) | type => withReducible $ -- when testing whether a type is a type class, we only unfold reducible constants. forallTelescopeReducingAux isClassExpensive type none $ fun xs type => do match type.getAppFn with | Expr.const c _ _ => do env ← getEnv; pure $ if isClass env c then some c else none | _ => pure none /-- Given `type` of the form `forall xs, A`, execute `k xs A`. This combinator will declare local declarations, create free variables for them, execute `k` with updated local context, and make sure the cache is restored after executing `k`. -/ def forallTelescope {Ξ±} (type : Expr) (k : Array Expr β†’ Expr β†’ MetaM Ξ±) : MetaM Ξ± := do lctx ← getLCtx; forallTelescopeReducingAuxAux isClassExpensive false none k lctx #[] 0 type /-- Similar to `forallTelescope`, but given `type` of the form `forall xs, A`, it reduces `A` and continues bulding the telescope if it is a `forall`. -/ def forallTelescopeReducing {Ξ±} (type : Expr) (k : Array Expr β†’ Expr β†’ MetaM Ξ±) : MetaM Ξ± := forallTelescopeReducingAux isClassExpensive type none k /-- Similar to `forallTelescopeReducing`, stops constructing the telescope when it reaches size `maxFVars`. -/ def forallBoundedTelescope {Ξ±} (type : Expr) (maxFVars? : Option Nat) (k : Array Expr β†’ Expr β†’ MetaM Ξ±) : MetaM Ξ± := forallTelescopeReducingAux isClassExpensive type maxFVars? k /-- Return the parameter names for the givel global declaration. -/ def getParamNames (declName : Name) : MetaM (Array Name) := do cinfo ← getConstInfo declName; forallTelescopeReducing cinfo.type $ fun xs _ => do xs.mapM $ fun x => do localDecl ← getLocalDecl x.fvarId!; pure localDecl.userName def isClass (type : Expr) : MetaM (Option Name) := do c? ← isClassQuick type; match c? with | LOption.none => pure none | LOption.some c => pure (some c) | LOption.undef => isClassExpensive type /-- Similar to `forallTelescopeAuxAux` but for lambda and let expressions. -/ private partial def lambdaTelescopeAux {Ξ±} (k : Array Expr β†’ Expr β†’ MetaM Ξ±) : LocalContext β†’ Array Expr β†’ Nat β†’ Expr β†’ MetaM Ξ± | lctx, fvars, j, Expr.lam n d b c => do let d := d.instantiateRevRange j fvars.size fvars; fvarId ← mkFreshId; let lctx := lctx.mkLocalDecl fvarId n d c.binderInfo; let fvar := mkFVar fvarId; lambdaTelescopeAux lctx (fvars.push fvar) j b | lctx, fvars, j, Expr.letE n t v b _ => do let t := t.instantiateRevRange j fvars.size fvars; let v := v.instantiateRevRange j fvars.size fvars; fvarId ← mkFreshId; let lctx := lctx.mkLetDecl fvarId n t v; let fvar := mkFVar fvarId; lambdaTelescopeAux lctx (fvars.push fvar) j b | lctx, fvars, j, e => let e := e.instantiateRevRange j fvars.size fvars; adaptReader (fun (ctx : Context) => { lctx := lctx, .. ctx }) $ withNewLocalInstances isClassExpensive fvars j $ do k fvars e /-- Similar to `forallTelescope` but for lambda and let expressions. -/ def lambdaTelescope {Ξ±} (e : Expr) (k : Array Expr β†’ Expr β†’ MetaM Ξ±) : MetaM Ξ± := do lctx ← getLCtx; lambdaTelescopeAux k lctx #[] 0 e -- `kind` specifies the metavariable kind for metavariables not corresponding to instance implicit `[ ... ]` arguments. private partial def forallMetaTelescopeReducingAux (reducing? : Bool) (maxMVars? : Option Nat) (kind : MetavarKind) : Array Expr β†’ Array BinderInfo β†’ Nat β†’ Expr β†’ MetaM (Array Expr Γ— Array BinderInfo Γ— Expr) | mvars, bis, j, type@(Expr.forallE n d b c) => do let process : Unit β†’ MetaM (Array Expr Γ— Array BinderInfo Γ— Expr) := fun _ => do { let d := d.instantiateRevRange j mvars.size mvars; let k := if c.binderInfo.isInstImplicit then MetavarKind.synthetic else kind; mvar ← mkFreshExprMVar d n k; let mvars := mvars.push mvar; let bis := bis.push c.binderInfo; forallMetaTelescopeReducingAux mvars bis j b }; match maxMVars? with | none => process () | some maxMVars => if mvars.size < maxMVars then process () else let type := type.instantiateRevRange j mvars.size mvars; pure (mvars, bis, type) | mvars, bis, j, type => let type := type.instantiateRevRange j mvars.size mvars; if reducing? then do newType ← whnf type; if newType.isForall then forallMetaTelescopeReducingAux mvars bis mvars.size newType else pure (mvars, bis, type) else pure (mvars, bis, type) /-- Similar to `forallTelescope`, but creates metavariables instead of free variables. -/ def forallMetaTelescope (e : Expr) (kind := MetavarKind.natural) : MetaM (Array Expr Γ— Array BinderInfo Γ— Expr) := forallMetaTelescopeReducingAux false none kind #[] #[] 0 e /-- Similar to `forallTelescopeReducing`, but creates metavariables instead of free variables. -/ def forallMetaTelescopeReducing (e : Expr) (maxMVars? : Option Nat := none) (kind := MetavarKind.natural) : MetaM (Array Expr Γ— Array BinderInfo Γ— Expr) := forallMetaTelescopeReducingAux true maxMVars? kind #[] #[] 0 e /-- Similar to `forallMetaTelescopeReducingAux` but for lambda expressions. -/ private partial def lambdaMetaTelescopeAux (maxMVars? : Option Nat) : Array Expr β†’ Array BinderInfo β†’ Nat β†’ Expr β†’ MetaM (Array Expr Γ— Array BinderInfo Γ— Expr) | mvars, bis, j, type => do let finalize : Unit β†’ MetaM (Array Expr Γ— Array BinderInfo Γ— Expr) := fun _ => do { let type := type.instantiateRevRange j mvars.size mvars; pure (mvars, bis, type) }; let process : Unit β†’ MetaM (Array Expr Γ— Array BinderInfo Γ— Expr) := fun _ => do { match type with | Expr.lam n d b c => do let d := d.instantiateRevRange j mvars.size mvars; mvar ← mkFreshExprMVar d; let mvars := mvars.push mvar; let bis := bis.push c.binderInfo; lambdaMetaTelescopeAux mvars bis j b | _ => finalize () }; match maxMVars? with | none => process () | some maxMVars => if mvars.size < maxMVars then process () else finalize () /-- Similar to `forallMetaTelescope` but for lambda expressions. -/ def lambdaMetaTelescope (e : Expr) (maxMVars? : Option Nat := none) : MetaM (Array Expr Γ— Array BinderInfo Γ— Expr) := lambdaMetaTelescopeAux maxMVars? #[] #[] 0 e @[inline] def liftStateMCtx {Ξ±} (x : StateM MetavarContext Ξ±) : MetaM Ξ± := fun _ s => let (a, mctx) := x.run s.mctx; EStateM.Result.ok a { mctx := mctx, .. s } def instantiateLevelMVars (lvl : Level) : MetaM Level := liftStateMCtx $ MetavarContext.instantiateLevelMVars lvl def assignLevelMVar (mvarId : MVarId) (lvl : Level) : MetaM Unit := modify $ fun s => { mctx := MetavarContext.assignLevel s.mctx mvarId lvl, .. s } def mkFreshLevelMVarId : MetaM MVarId := do mvarId ← mkFreshId; modify $ fun s => { mctx := s.mctx.addLevelMVarDecl mvarId, .. s }; pure mvarId def whnfD : Expr β†’ MetaM Expr := fun e => withTransparency TransparencyMode.default $ whnf e /-- Execute `x` using approximate unification: `foApprox`, `ctxApprox` and `quasiPatternApprox`. -/ @[inline] def approxDefEq {Ξ±} (x : MetaM Ξ±) : MetaM Ξ± := adaptReader (fun (ctx : Context) => { config := { foApprox := true, ctxApprox := true, quasiPatternApprox := true, .. ctx.config }, .. ctx }) x /-- Similar to `approxDefEq`, but uses all available approximations. We don't use `constApprox` by default at `approxDefEq` because it often produces undesirable solution for monadic code. For example, suppose we have `pure (x > 0)` which has type `?m Prop`. We also have the goal `[HasPure ?m]`. Now, assume the expected type is `IO Bool`. Then, the unification constraint `?m Prop =?= IO Bool` could be solved as `?m := fun _ => IO Bool` using `constApprox`, but this spurious solution would generate a failure when we try to solve `[HasPure (fun _ => IO Bool)]` -/ @[inline] def fullApproxDefEq {Ξ±} (x : MetaM Ξ±) : MetaM Ξ± := adaptReader (fun (ctx : Context) => { config := { foApprox := true, ctxApprox := true, quasiPatternApprox := true, constApprox := true, .. ctx.config }, .. ctx }) x @[inline] private def withNewFVar {Ξ±} (fvar fvarType : Expr) (k : Expr β†’ MetaM Ξ±) : MetaM Ξ± := do c? ← isClass fvarType; match c? with | none => k fvar | some c => withNewLocalInstance c fvar $ k fvar def withLocalDecl {Ξ±} (n : Name) (type : Expr) (bi : BinderInfo) (k : Expr β†’ MetaM Ξ±) : MetaM Ξ± := do fvarId ← mkFreshId; ctx ← read; let lctx := ctx.lctx.mkLocalDecl fvarId n type bi; let fvar := mkFVar fvarId; adaptReader (fun (ctx : Context) => { lctx := lctx, .. ctx }) $ withNewFVar fvar type k def withLocalDeclD {Ξ±} (n : Name) (type : Expr) (k : Expr β†’ MetaM Ξ±) : MetaM Ξ± := withLocalDecl n type BinderInfo.default k def withLetDecl {Ξ±} (n : Name) (type : Expr) (val : Expr) (k : Expr β†’ MetaM Ξ±) : MetaM Ξ± := do fvarId ← mkFreshId; ctx ← read; let lctx := ctx.lctx.mkLetDecl fvarId n type val; let fvar := mkFVar fvarId; adaptReader (fun (ctx : Context) => { lctx := lctx, .. ctx }) $ withNewFVar fvar type k /-- Save cache and `MetavarContext`, bump the `MetavarContext` depth, execute `x`, and restore saved data. -/ @[inline] def withNewMCtxDepth {Ξ±} (x : MetaM Ξ±) : MetaM Ξ± := do s ← get; let savedMCtx := s.mctx; modify $ fun s => { mctx := s.mctx.incDepth, .. s }; finally x (modify $ fun s => { mctx := savedMCtx, .. s }) def withLocalContext {Ξ±} (lctx : LocalContext) (localInsts : LocalInstances) (x : MetaM Ξ±) : MetaM Ξ± := do localInstsCurr ← getLocalInstances; adaptReader (fun (ctx : Context) => { lctx := lctx, localInstances := localInsts, .. ctx }) $ if localInsts == localInstsCurr then x else resettingSynthInstanceCache x /-- Execute `x` using the given metavariable `LocalContext` and `LocalInstances`. The type class resolution cache is flushed when executing `x` if its `LocalInstances` are different from the current ones. -/ @[inline] def withMVarContext {Ξ±} (mvarId : MVarId) (x : MetaM Ξ±) : MetaM Ξ± := do mvarDecl ← getMVarDecl mvarId; withLocalContext mvarDecl.lctx mvarDecl.localInstances x @[inline] def withMCtx {Ξ±} (mctx : MetavarContext) (x : MetaM Ξ±) : MetaM Ξ± := do mctx' ← getMCtx; modify $ fun s => { mctx := mctx, .. s }; finally x (modify $ fun s => { mctx := mctx', .. s }) @[init] private def regTraceClasses : IO Unit := registerTraceClass `Meta def run {Ξ±} (env : Environment) (x : MetaM Ξ±) (maxRecDepth := 10000) : Except Exception Ξ± := match x { maxRecDepth := maxRecDepth, currRecDepth := 0 } { env := env } with | EStateM.Result.ok a _ => Except.ok a | EStateM.Result.error ex _ => Except.error ex end Meta export Meta (MetaM) end Lean open Lean open Lean.Meta /-- Helper function for running `MetaM` methods in attributes -/ @[inline] def IO.runMeta {Ξ±} (x : MetaM Ξ±) (env : Environment) (cfg : Config := {}) : IO (Ξ± Γ— Environment) := match (x { config := cfg, currRecDepth := 0, maxRecDepth := defaultMaxRecDepth }).run { env := env } with | EStateM.Result.ok a s => pure (a, s.env) | EStateM.Result.error ex _ => throw (IO.userError (toString ex))
4b38f562432930af44f59e97e8094c44eb1d3d9d
9dc8cecdf3c4634764a18254e94d43da07142918
/src/category_theory/graded_object.lean
ff2b216b6f51ab0138545c104803d8dfa5a27938
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
6,841
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import data.int.basic import algebra.group_power.lemmas import category_theory.pi.basic import category_theory.shift import category_theory.concrete_category.basic /-! # The category of graded objects For any type `Ξ²`, a `Ξ²`-graded object over some category `C` is just a function `Ξ² β†’ C` into the objects of `C`. We put the "pointwise" category structure on these, as the non-dependent specialization of `category_theory.pi`. We describe the `comap` functors obtained by precomposing with functions `Ξ² β†’ Ξ³`. As a consequence a fixed element (e.g. `1`) in an additive group `Ξ²` provides a shift functor on `Ξ²`-graded objects When `C` has coproducts we construct the `total` functor `graded_object Ξ² C β₯€ C`, show that it is faithful, and deduce that when `C` is concrete so is `graded_object Ξ² C`. -/ open category_theory.pi open category_theory.limits namespace category_theory universes w v u /-- A type synonym for `Ξ² β†’ C`, used for `Ξ²`-graded objects in a category `C`. -/ def graded_object (Ξ² : Type w) (C : Type u) : Type (max w u) := Ξ² β†’ C -- Satisfying the inhabited linter... instance inhabited_graded_object (Ξ² : Type w) (C : Type u) [inhabited C] : inhabited (graded_object Ξ² C) := ⟨λ b, inhabited.default⟩ /-- A type synonym for `Ξ² β†’ C`, used for `Ξ²`-graded objects in a category `C` with a shift functor given by translation by `s`. -/ @[nolint unused_arguments] -- `s` is here to distinguish type synonyms asking for different shifts abbreviation graded_object_with_shift {Ξ² : Type w} [add_comm_group Ξ²] (s : Ξ²) (C : Type u) : Type (max w u) := graded_object Ξ² C namespace graded_object variables {C : Type u} [category.{v} C] instance category_of_graded_objects (Ξ² : Type w) : category.{max w v} (graded_object Ξ² C) := category_theory.pi (Ξ» _, C) /-- The projection of a graded object to its `i`-th component. -/ @[simps] def eval {Ξ² : Type w} (b : Ξ²) : graded_object Ξ² C β₯€ C := { obj := Ξ» X, X b, map := Ξ» X Y f, f b, } section variable (C) /-- The natural isomorphism comparing between pulling back along two propositionally equal functions. -/ @[simps] def comap_eq {Ξ² Ξ³ : Type w} {f g : Ξ² β†’ Ξ³} (h : f = g) : comap (Ξ» _, C) f β‰… comap (Ξ» _, C) g := { hom := { app := Ξ» X b, eq_to_hom begin dsimp [comap], subst h, end }, inv := { app := Ξ» X b, eq_to_hom begin dsimp [comap], subst h, end }, } lemma comap_eq_symm {Ξ² Ξ³ : Type w} {f g : Ξ² β†’ Ξ³} (h : f = g) : comap_eq C h.symm = (comap_eq C h).symm := by tidy lemma comap_eq_trans {Ξ² Ξ³ : Type w} {f g h : Ξ² β†’ Ξ³} (k : f = g) (l : g = h) : comap_eq C (k.trans l) = comap_eq C k β‰ͺ≫ comap_eq C l := begin ext X b, simp, end @[simp] lemma eq_to_hom_apply {Ξ² : Type w} {X Y : Ξ  b : Ξ², C} (h : X = Y) (b : Ξ²) : (eq_to_hom h : X ⟢ Y) b = eq_to_hom (by subst h) := by { subst h, refl } /-- The equivalence between Ξ²-graded objects and Ξ³-graded objects, given an equivalence between Ξ² and Ξ³. -/ @[simps] def comap_equiv {Ξ² Ξ³ : Type w} (e : Ξ² ≃ Ξ³) : (graded_object Ξ² C) β‰Œ (graded_object Ξ³ C) := { functor := comap (Ξ» _, C) (e.symm : Ξ³ β†’ Ξ²), inverse := comap (Ξ» _, C) (e : Ξ² β†’ Ξ³), counit_iso := (comap_comp (Ξ» _, C) _ _).trans (comap_eq C (by { ext, simp } )), unit_iso := (comap_eq C (by { ext, simp } )).trans (comap_comp _ _ _).symm, functor_unit_iso_comp' := Ξ» X, by { ext b, dsimp, simp, }, } -- See note [dsimp, simp]. end instance has_shift {Ξ² : Type*} [add_comm_group Ξ²] (s : Ξ²) : has_shift (graded_object_with_shift s C) β„€ := has_shift_mk _ _ { F := Ξ» n, comap (Ξ» _, C) $ Ξ» (b : Ξ²), b + n β€’ s, Ξ΅ := (comap_id Ξ² (Ξ» _, C)).symm β‰ͺ≫ (comap_eq C (by { ext, simp })), ΞΌ := Ξ» m n, comap_comp _ _ _ β‰ͺ≫ comap_eq C (by { ext, simp [add_zsmul, add_comm] }), left_unitality := by { introv, ext, dsimp, simpa }, right_unitality := by { introv, ext, dsimp, simpa }, associativity := by { introv, ext, dsimp, simp } } @[simp] lemma shift_functor_obj_apply {Ξ² : Type*} [add_comm_group Ξ²] (s : Ξ²) (X : Ξ² β†’ C) (t : Ξ²) (n : β„€) : (shift_functor (graded_object_with_shift s C) n).obj X t = X (t + n β€’ s) := rfl @[simp] lemma shift_functor_map_apply {Ξ² : Type*} [add_comm_group Ξ²] (s : Ξ²) {X Y : graded_object_with_shift s C} (f : X ⟢ Y) (t : Ξ²) (n : β„€) : (shift_functor (graded_object_with_shift s C) n).map f t = f (t + n β€’ s) := rfl instance has_zero_morphisms [has_zero_morphisms C] (Ξ² : Type w) : has_zero_morphisms.{max w v} (graded_object Ξ² C) := { has_zero := Ξ» X Y, { zero := Ξ» b, 0 } } @[simp] lemma zero_apply [has_zero_morphisms C] (Ξ² : Type w) (X Y : graded_object Ξ² C) (b : Ξ²) : (0 : X ⟢ Y) b = 0 := rfl section open_locale zero_object instance has_zero_object [has_zero_object C] [has_zero_morphisms C] (Ξ² : Type w) : has_zero_object.{max w v} (graded_object Ξ² C) := by { refine ⟨⟨λ b, 0, Ξ» X, ⟨⟨⟨λ b, 0⟩, Ξ» f, _⟩⟩, Ξ» X, ⟨⟨⟨λ b, 0⟩, Ξ» f, _⟩⟩⟩⟩; ext, } end end graded_object namespace graded_object -- The universes get a little hairy here, so we restrict the universe level for the grading to 0. -- Since we're typically interested in grading by β„€ or a finite group, this should be okay. -- If you're grading by things in higher universes, have fun! variables (Ξ² : Type) variables (C : Type u) [category.{v} C] variables [has_coproducts.{0} C] section local attribute [tidy] tactic.discrete_cases /-- The total object of a graded object is the coproduct of the graded components. -/ noncomputable def total : graded_object Ξ² C β₯€ C := { obj := Ξ» X, ∐ (Ξ» i : Ξ², X i), map := Ξ» X Y f, limits.sigma.map (Ξ» i, f i) }. end variables [has_zero_morphisms C] /-- The `total` functor taking a graded object to the coproduct of its graded components is faithful. To prove this, we need to know that the coprojections into the coproduct are monomorphisms, which follows from the fact we have zero morphisms and decidable equality for the grading. -/ instance : faithful (total Ξ² C) := { map_injective' := Ξ» X Y f g w, begin classical, ext i, replace w := sigma.ΞΉ (Ξ» i : Ξ², X i) i ≫= w, erw [colimit.ΞΉ_map, colimit.ΞΉ_map] at w, simp at *, exact mono.right_cancellation _ _ w, end } end graded_object namespace graded_object noncomputable theory variables (Ξ² : Type) variables (C : Type (u+1)) [large_category C] [concrete_category C] [has_coproducts.{0} C] [has_zero_morphisms C] instance : concrete_category (graded_object Ξ² C) := { forget := total Ξ² C β‹™ forget C } instance : has_forgetβ‚‚ (graded_object Ξ² C) C := { forgetβ‚‚ := total Ξ² C } end graded_object end category_theory
b739d0a224721c9f7c87ee90f0b59904160f572f
4a894698f2ae3f991490c25af3c13ea4435dac48
/src/instructor/lectures/lecture_2.lean
5edaa5f9ebb3b35ba7cd29c802053dbdf17dcf33
[]
no_license
gnujey/cs2120f21
8a33f636346d59ade049dcc1726634f434ac1955
138d43446c443c1d15cd2f17fb607c4f0dff702f
refs/heads/main
1,690,598,880,775
1,631,132,566,000
1,631,132,566,000
405,182,739
1
0
null
1,631,299,824,000
1,631,299,823,000
null
UTF-8
Lean
false
false
5,854
lean
/- INFERENCE RULE #1/2: EQUALITY IS REFLEXIVE Everything is equal to itself. A bit more formally, if one is given a type, T, and a value, t, of this type, then you may have a proof of t = t "for free." -/ axiom eq_refl : βˆ€ (T : Type) -- if T is any type (of thing) (t : T), -- and t is thing of that type, T t = t -- the result type: proof of t = t /- Ok, you actually have to *apply* the axiom of reflexive equality. -/ example : 1 = 1 := eq_refl β„• 1 -- Our definition example : 1 = 1 := @eq.refl β„• 1 -- Lean, with inference turned off by @ example : 1 = 1 := eq.refl 1 -- Lean's definition with T=β„• inferred /- INFERENCE RULE #2/2: SUBSTITUTION OF EQUALS FOR EQUALS Given any type, T, of objects, and any *property*, P, of objects of this type, if you know x has property P (written as P x) and you know that x = y, then you can deduce that y has property P. -/ axiom eq_subst : βˆ€ (T : Type) -- if T is a type (P : T β†’ Prop) -- and P is a property of T objects (x y : T) -- and x and y are T objects (e : x = y) -- and you have a proof that x = y (px : P x), -- and you have a proof that x has property P P y -- then you can deduce (and get a proof) of P y /- SOME EXAMPLES -/ -- a predicate is a proposition with arguments def eq_3_3 : Prop := 3 = 3 -- proposition def eq_n_3 (n : β„•) : Prop := n = 3 -- predicate -- predicates applied to values yield propositions -- hover your mouse pointer over #reduce to see result #reduce eq_n_3 2 #reduce eq_n_3 3 #reduce eq_n_3 4 -- Axioms are just assumptions, so let's make some axioms (aType : Type) -- e.g., nat (aProperty : aType β†’ Prop) -- e.g., eq_n_3 (x y : aType) -- arbitary nats (e : x = y) -- a proof x = y (px : aProperty x) -- proof of eq_n_3 x /- Given the preceding assumptions, can we prove aPred y? -/ -- Yes, just apply the substitutability axiom theorem aTheorem : aProperty y := eq_subst -- apply the axiom to the following arguments aType -- the type aProperty -- the property x y -- the values e -- the proof of x = y px -- the proof that x has aProperty -- And the result is a ... #check aTheorem -- ... proof of P y! /- You can (often) think of inference rules as functions that can take proofs as arguments and that return proofs as results. The secret sauce here is that you *cannot* apply such a function unless you have the arguments that it requires. So, for example, if you don't have and can't construct a proof of x = y, then you simply cannot apply/use the eq_subset inference rule. -/ /- By the way, eq_subst is defined in Lean as eq.subst. -/ theorem aTheorem' : aProperty y := @eq.subst -- yes! apply the axiom to aType -- the type aProperty -- the property x y -- the values e -- the proof of x = y px -- the proof that x as aProperty /- And in Lean eq.subst uses type inference to infer T, P, x, and y, from e, so you don't need to give explicit values for these argument. -/ theorem aTheorem'' : aProperty y := eq.subst -- yes! apply the axiom to e -- the proof of x = y px -- the proof that x as aProperty /- Lean includes a proof building scripting language, and here are two proofs of the same thing using proof scripts. -/ theorem aTheorem''' : aProperty y := begin apply eq.subst e, -- rewrite the goal proposition using e exact px, -- we've already assumed that proposition is true end theorem aTheorem'''' : aProperty y := begin rw <- e, -- rewrite goal using e read right to left exact px, -- the new goal is true by assumption end /- Theorem: *equality is symmetric* What we are to show is that, for any objects, x and y, of any type T, if x = y then y = x. Proof: We'll *assume* the premises of the conjecture: that T is a type; x and y are values of that type; and it's true (and we have a proof, h) x = y. Now *in the context* of these assumptions, we need to construct a proof that y = x. We can do that by applying the axiom of the substitutability of equals to the proposition to be proved, using our proof of x = y as an argument, to substitute y for x whereever x appears. The result is that we must now only prove y = y. And that is done by applying the axiom of reflexivity of equality. Here's a formal proof. What's most important at this point is that you be able to follow how the *context* of the proof evolves as we make each of our "moves" in the construction of the final proof. Use SHIFT + CMD/CTRL + ENTER to open the Lean Information View, which will present the evolving proof context, then click on each line of the proof-constructing script we've give you here to see how each move affects the context. -/ theorem eq_symm : βˆ€ (T : Type) (x y : T), x = y β†’ y = x := begin assume T, assume x y, assume h, rw h, -- rw applies eq.refl automatically to complete the proof end /- Because this proposition is a universal generalization, it can be *applied* to particular arguments to get particular results! Recall that e is a proof x = y. So what do we get when we appy eq_symm to e? -/ #reduce eq_symm β„• x y x=y /- Theorem: Equality is transitive If x, y, and z are objects of some type, T, and we know (have proofs or axioms) that x = y and y = z, then we can deduce (and have a proof) that x = z. -/ theorem eq_trans : βˆ€ (T : Type) (x y z : T) (e1 : x = y) (e2 : y = z), x = z := begin assume (T : Type), -- take as temporary axiom! assume (x y z : T), -- another one: context! assume (e1 : x = y), assume (e2 : y = z), rw e1, rw e2, -- eq.refl applied automatically end
de8992a4918146a5592094c574b34de01c35a0b5
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/topology/sheaves/presheaf.lean
44e1cc181eeac3a8e9ac82933f295ff2b56de6d8
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
3,809
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 -/ 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.{v}} (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. -/ universes v u open category_theory open topological_space open opposite variables (C : Type u) [category.{v} C] namespace Top @[derive category] def presheaf (X : Top.{v}) := (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.{v}} (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.{v}} (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.{v}} (f : X ⟢ Y) (β„± : X.presheaf C) {U V : (opens Y)α΅’α΅–} (i : U ⟢ V) : (f _* β„±).map i = β„±.map ((opens.map f).op.map i) := rfl def pushforward_eq {X Y : Top.{v}} {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) β„± @[simp] lemma pushforward_eq_hom_app {X Y : Top.{v}} {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] @[simp] lemma pushforward_eq_rfl {X Y : Top.{v}} (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.{v}} {f g : X ⟢ Y} (h₁ hβ‚‚ : f = g) (β„± : X.presheaf C) : β„±.pushforward_eq h₁ = β„±.pushforward_eq hβ‚‚ := rfl namespace pushforward variables {X : Top.{v}} (β„± : X.presheaf C) def id : (πŸ™ X) _* β„± β‰… β„± := (iso_whisker_right (nat_iso.op (opens.map_id X).symm) β„±) β‰ͺ≫ functor.left_unitor _ @[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, } def comp {Y Z : Top.{v}} (f : X ⟢ Y) (g : Y ⟢ Z) : (f ≫ g) _* β„± β‰… g _* (f _* β„±) := iso_whisker_right (nat_iso.op (opens.map_comp f g).symm) β„± @[simp] lemma comp_hom_app {Y Z : Top.{v}} (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.{v}} (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.{v}} (f : X ⟢ Y) {β„± 𝒒 : X.presheaf C} (Ξ± : β„± ⟢ 𝒒) : f _* β„± ⟢ f _* 𝒒 := { app := Ξ» U, Ξ±.app _, naturality' := Ξ» U V i, by { erw Ξ±.naturality, refl, } } end presheaf end Top
ee32e3437aeba02ea932167f864a7b3cff3a7d2f
57aec6ee746bc7e3a3dd5e767e53bd95beb82f6d
/tests/lean/Process.lean
6591d72261507f571d5a48d1e059304e4d835e34
[ "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
1,519
lean
-- open IO.Process def usingIO {Ξ±} (x : IO Ξ±) : IO Ξ± := x #eval usingIO do let child ← spawn { cmd := "sh", args := #["-c", "exit 1"] }; child.wait #eval usingIO do let child ← spawn { cmd := "sh", args := #["-c", "echo hi!"] }; child.wait #eval usingIO do let child ← spawn { cmd := "sh", args := #["-c", "echo ho!"], stdout := Stdio.piped }; discard $ child.wait; child.stdout.readToEnd #eval usingIO do let child ← spawn { cmd := "head", args := #["-n1"], stdin := Stdio.piped, stdout := Stdio.piped }; child.stdin.putStrLn "hu!"; child.stdin.flush; discard $ child.wait; child.stdout.readToEnd #eval usingIO do let child ← spawn { cmd := "true", stdin := Stdio.piped }; discard $ child.wait; child.stdin.putStrLn "ha!"; child.stdin.flush <|> IO.println "flush of broken pipe failed" #eval usingIO do -- produce enough output to fill both pipes on all platforms let out ← output { cmd := "sh", args := #["-c", "printf '%100000s' >& 2; printf '%100001s'"] }; IO.println out.stdout.length; IO.println out.stderr.length #eval usingIO do -- With a non-empty stdin, cat would wait on input forever let child ← spawn { cmd := "sh", args := #["-c", "cat"], stdin := Stdio.null }; child.wait #eval usingIO do let child ← spawn { cmd := "sh", args := #["-c", "echo nullStdout"], stdout := Stdio.null }; child.wait #eval usingIO do let child ← spawn { cmd := "sh", args := #["-c", "echo nullStderr >& 2"], stderr := Stdio.null }; child.wait
a89a382d38a40198907411500df3a51fbef01c60
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/number_theory/legendre_symbol/gauss_sum.lean
d3eec3b506a903970dfc7187b997cf286bb2f617
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
14,248
lean
/- Copyright (c) 2022 Michael Stoll. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Michael Stoll -/ import number_theory.legendre_symbol.add_character import number_theory.legendre_symbol.zmod_char import algebra.char_p.char_and_card /-! # Gauss sums > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. We define the Gauss sum associated to a multiplicative and an additive character of a finite field and prove some results about them. ## Main definition Let `R` be a finite commutative ring and let `R'` be another commutative ring. If `Ο‡` is a multiplicative character `R β†’ R'` (type `mul_char R R'`) and `ψ` is an additive character `R β†’ R'` (type `add_char R R'`, which abbreviates `(multiplicative R) β†’* R'`), then the *Gauss sum* of `Ο‡` and `ψ` is `βˆ‘ a, Ο‡ a * ψ a`. ## Main results Some important results are as follows. * `gauss_sum_mul_gauss_sum_eq_card`: The product of the Gauss sums of `Ο‡` and `ψ` and that of `χ⁻¹` and `ψ⁻¹` is the cardinality of the source ring `R` (if `Ο‡` is nontrivial, `ψ` is primitive and `R` is a field). * `gauss_sum_sq`: The square of the Gauss sum is `Ο‡(-1)` times the cardinality of `R` if in addition `Ο‡` is a quadratic character. * `quad_gauss_sum_frob`: For a quadratic character `Ο‡`, raising the Gauss sum to the `p`th power (where `p` is the characteristic of the target ring `R'`) multiplies it by `Ο‡ p`. * `char.card_pow_card`: When `F` and `F'` are finite fields and `Ο‡ : F β†’ F'` is a nontrivial quadratic character, then `(Ο‡ (-1) * #F)^(#F'/2) = Ο‡ (#F')`. * `finite_field.two_pow_card`: For every finite field `F` of odd characteristic, we have `2^(#F/2) = Ο‡β‚ˆ(#F)` in `F`. This machinery can be used to derive (a generalization of) the Law of Quadratic Reciprocity. ## Tags additive character, multiplicative character, Gauss sum -/ universes u v open_locale big_operators open add_char mul_char section gauss_sum_def -- `R` is the domain of the characters variables {R : Type u} [comm_ring R] [fintype R] -- `R'` is the target of the characters variables {R' : Type v} [comm_ring R'] /-! ### Definition and first properties -/ /-- Definition of the Gauss sum associated to a multiplicative and an additive character. -/ def gauss_sum (Ο‡ : mul_char R R') (ψ : add_char R R') : R' := βˆ‘ a, Ο‡ a * ψ a /-- Replacing `ψ` by `mul_shift ψ a` and multiplying the Gauss sum by `Ο‡ a` does not change it. -/ lemma gauss_sum_mul_shift (Ο‡ : mul_char R R') (ψ : add_char R R') (a : RΛ£) : Ο‡ a * gauss_sum Ο‡ (mul_shift ψ a) = gauss_sum Ο‡ ψ := begin simp only [gauss_sum, mul_shift_apply, finset.mul_sum], simp_rw [← mul_assoc, ← map_mul], exact fintype.sum_bijective _ a.mul_left_bijective _ _ (Ξ» x, rfl), end end gauss_sum_def /-! ### The product of two Gauss sums -/ section gauss_sum_prod -- In the following, we need `R` to be a finite field and `R'` to be a domain. variables {R : Type u} [field R] [fintype R] {R' : Type v} [comm_ring R'] [is_domain R'] -- A helper lemma for `gauss_sum_mul_gauss_sum_eq_card` below -- Is this useful enough in other contexts to be public? private lemma gauss_sum_mul_aux {Ο‡ : mul_char R R'} (hΟ‡ : is_nontrivial Ο‡) (ψ : add_char R R') (b : R) : βˆ‘ a, Ο‡ (a * b⁻¹) * ψ (a - b) = βˆ‘ c, Ο‡ c * ψ (b * (c - 1)) := begin cases eq_or_ne b 0 with hb hb, { -- case `b = 0` simp only [hb, inv_zero, mul_zero, mul_char.map_zero, zero_mul, finset.sum_const_zero, map_zero_one, mul_one], exact hΟ‡.sum_eq_zero.symm, }, { -- case `b β‰  0` refine (fintype.sum_bijective _ (mul_left_bijectiveβ‚€ b hb) _ _ $ Ξ» x, _).symm, rw [mul_assoc, mul_comm x, ← mul_assoc, mul_inv_cancel hb, one_mul, mul_sub, mul_one] }, end /-- We have `gauss_sum Ο‡ ψ * gauss_sum χ⁻¹ ψ⁻¹ = fintype.card R` when `Ο‡` is nontrivial and `ψ` is primitive (and `R` is a field). -/ lemma gauss_sum_mul_gauss_sum_eq_card {Ο‡ : mul_char R R'} (hΟ‡ : is_nontrivial Ο‡) {ψ : add_char R R'} (hψ : is_primitive ψ) : gauss_sum Ο‡ ψ * gauss_sum χ⁻¹ ψ⁻¹ = fintype.card R := begin simp only [gauss_sum, add_char.inv_apply, finset.sum_mul, finset.mul_sum, mul_char.inv_apply'], conv in (_ * _ * (_ * _)) { rw [mul_mul_mul_comm, ← map_mul, ← map_add_mul, ← sub_eq_add_neg], }, simp_rw gauss_sum_mul_aux hΟ‡ ψ, rw [finset.sum_comm], classical, -- to get `[decidable_eq R]` for `sum_mul_shift` simp_rw [← finset.mul_sum, sum_mul_shift _ hψ, sub_eq_zero, mul_ite, mul_zero], rw [finset.sum_ite_eq' finset.univ (1 : R)], simp only [finset.mem_univ, map_one, one_mul, if_true], end /-- When `Ο‡` is a nontrivial quadratic character, then the square of `gauss_sum Ο‡ ψ` is `Ο‡(-1)` times the cardinality of `R`. -/ lemma gauss_sum_sq {Ο‡ : mul_char R R'} (hχ₁ : is_nontrivial Ο‡) (hΟ‡β‚‚ : is_quadratic Ο‡) {ψ : add_char R R'} (hψ : is_primitive ψ) : (gauss_sum Ο‡ ψ) ^ 2 = Ο‡ (-1) * fintype.card R := begin rw [pow_two, ← gauss_sum_mul_gauss_sum_eq_card hχ₁ hψ, hΟ‡β‚‚.inv, mul_rotate'], congr, rw [mul_comm, ← gauss_sum_mul_shift _ _ (-1 : RΛ£), inv_mul_shift], refl, end end gauss_sum_prod /-! ### Gauss sums and Frobenius -/ section gauss_sum_frob variables {R : Type u} [comm_ring R] [fintype R] {R' : Type v} [comm_ring R'] -- We assume that the target ring `R'` has prime characteristic `p`. variables (p : β„•) [fp : fact p.prime] [hch : char_p R' p] include fp hch /-- When `R'` has prime characteristic `p`, then the `p`th power of the Gauss sum of `Ο‡` and `ψ` is the Gauss sum of `Ο‡^p` and `ψ^p`. -/ lemma gauss_sum_frob (Ο‡ : mul_char R R') (ψ : add_char R R') : gauss_sum Ο‡ ψ ^ p = gauss_sum (Ο‡ ^ p) (ψ ^ p) := begin rw [← frobenius_def, gauss_sum, gauss_sum, map_sum], simp_rw [pow_apply' Ο‡ fp.1.pos, map_mul, frobenius_def], refl, end /-- For a quadratic character `Ο‡` and when the characteristic `p` of the target ring is a unit in the source ring, the `p`th power of the Gauss sum of`Ο‡` and `ψ` is `Ο‡ p` times the original Gauss sum. -/ lemma mul_char.is_quadratic.gauss_sum_frob (hp : is_unit (p : R)) {Ο‡ : mul_char R R'} (hΟ‡ : is_quadratic Ο‡) (ψ : add_char R R') : gauss_sum Ο‡ ψ ^ p = Ο‡ p * gauss_sum Ο‡ ψ := by rw [gauss_sum_frob, pow_mul_shift, hΟ‡.pow_char p, ← gauss_sum_mul_shift Ο‡ ψ hp.unit, ← mul_assoc, hp.unit_spec, ← pow_two, ← pow_apply' _ (by norm_num : 0 < 2), hΟ‡.sq_eq_one, ← hp.unit_spec, one_apply_coe, one_mul] /-- For a quadratic character `Ο‡` and when the characteristic `p` of the target ring is a unit in the source ring and `n` is a natural number, the `p^n`th power of the Gauss sum of`Ο‡` and `ψ` is `Ο‡ (p^n)` times the original Gauss sum. -/ lemma mul_char.is_quadratic.gauss_sum_frob_iter (n : β„•) (hp : is_unit (p : R)) {Ο‡ : mul_char R R'} (hΟ‡ : is_quadratic Ο‡) (ψ : add_char R R') : gauss_sum Ο‡ ψ ^ (p ^ n) = Ο‡ (p ^ n) * gauss_sum Ο‡ ψ := begin induction n with n ih, { rw [pow_zero, pow_one, pow_zero, mul_char.map_one, one_mul], }, { rw [pow_succ, mul_comm p, pow_mul, ih, mul_pow, hΟ‡.gauss_sum_frob _ hp, ← mul_assoc, pow_succ, mul_comm (p : R), map_mul, ← pow_apply' Ο‡ fp.1.pos (p ^ n), hΟ‡.pow_char p], }, end end gauss_sum_frob /-! ### Values of quadratic characters -/ section gauss_sum_values variables {R : Type u} [comm_ring R] [fintype R] {R' : Type v} [comm_ring R'] [is_domain R'] /-- If the square of the Gauss sum of a quadratic character is `Ο‡(-1) * #R`, then we get, for all `n : β„•`, the relation `(Ο‡(-1) * #R) ^ (p^n/2) = Ο‡(p^n)`, where `p` is the (odd) characteristic of the target ring `R'`. This version can be used when `R` is not a field, e.g., `β„€/8β„€`. -/ lemma char.card_pow_char_pow {Ο‡ : mul_char R R'} (hΟ‡ : is_quadratic Ο‡) (ψ : add_char R R') (p n : β„•) [fp : fact p.prime] [hch : char_p R' p] (hp : is_unit (p : R)) (hp' : p β‰  2) (hg : (gauss_sum Ο‡ ψ) ^ 2 = Ο‡ (-1) * fintype.card R) : (Ο‡ (-1) * fintype.card R) ^ (p ^ n / 2) = Ο‡ (p ^ n) := begin have : gauss_sum Ο‡ ψ β‰  0, { intro hf, rw [hf, zero_pow (by norm_num : 0 < 2), eq_comm, mul_eq_zero] at hg, exact not_is_unit_prime_of_dvd_card p ((char_p.cast_eq_zero_iff R' p _).mp $ hg.resolve_left (is_unit_one.neg.map Ο‡).ne_zero) hp }, rw ← hg, apply mul_right_cancelβ‚€ this, rw [← hΟ‡.gauss_sum_frob_iter p n hp ψ, ← pow_mul, mul_comm, ← pow_succ, nat.two_mul_div_two_add_one_of_odd ((fp.1.eq_two_or_odd').resolve_left hp').pow], end /-- When `F` and `F'` are finite fields and `Ο‡ : F β†’ F'` is a nontrivial quadratic character, then `(Ο‡(-1) * #F)^(#F'/2) = Ο‡(#F')`. -/ lemma char.card_pow_card {F : Type*} [field F] [fintype F] {F' : Type*} [field F'] [fintype F'] {Ο‡ : mul_char F F'} (hχ₁ : is_nontrivial Ο‡) (hΟ‡β‚‚ : is_quadratic Ο‡) (hch₁ : ring_char F' β‰  ring_char F) (hchβ‚‚ : ring_char F' β‰  2) : (Ο‡ (-1) * fintype.card F) ^ (fintype.card F' / 2) = Ο‡ (fintype.card F') := begin obtain ⟨n, hp, hc⟩ := finite_field.card F (ring_char F), obtain ⟨n', hp', hc'⟩ := finite_field.card F' (ring_char F'), let ψ := primitive_char_finite_field F F' hch₁, let FF' := cyclotomic_field ψ.n F', have hchar := algebra.ring_char_eq F' FF', apply (algebra_map F' FF').injective, rw [map_pow, map_mul, map_nat_cast, hc', hchar, nat.cast_pow], simp only [← mul_char.ring_hom_comp_apply], haveI := fact.mk hp', haveI := fact.mk (hchar.subst hp'), rw [ne, ← nat.prime_dvd_prime_iff_eq hp' hp, ← is_unit_iff_not_dvd_char, hchar] at hch₁, exact char.card_pow_char_pow (hΟ‡β‚‚.comp _) ψ.char (ring_char FF') n' hch₁ (hchar β–Έ hchβ‚‚) (gauss_sum_sq (hχ₁.comp $ ring_hom.injective _) (hΟ‡β‚‚.comp _) ψ.prim), end end gauss_sum_values section gauss_sum_two /-! ### The quadratic character of 2 This section proves the following result. For every finite field `F` of odd characteristic, we have `2^(#F/2) = Ο‡β‚ˆ(#F)` in `F`. This can be used to show that the quadratic character of `F` takes the value `Ο‡β‚ˆ(#F)` at `2`. The proof uses the Gauss sum of `Ο‡β‚ˆ` and a primitive additive character on `β„€/8β„€`; in this way, the result is reduced to `card_pow_char_pow`. -/ open zmod /-- For every finite field `F` of odd characteristic, we have `2^(#F/2) = Ο‡β‚ˆ(#F)` in `F`. -/ lemma finite_field.two_pow_card {F : Type*} [fintype F] [field F] (hF : ring_char F β‰  2) : (2 : F) ^ (fintype.card F / 2) = Ο‡β‚ˆ (fintype.card F) := begin have hp2 : βˆ€ (n : β„•), (2 ^ n : F) β‰  0 := Ξ» n, pow_ne_zero n (ring.two_ne_zero hF), obtain ⟨n, hp, hc⟩ := finite_field.card F (ring_char F), -- we work in `FF`, the eighth cyclotomic field extension of `F` let FF := (polynomial.cyclotomic 8 F).splitting_field, haveI : finite_dimensional F FF := polynomial.is_splitting_field.finite_dimensional FF (polynomial.cyclotomic 8 F), haveI : fintype FF := finite_dimensional.fintype_of_fintype F FF, have hchar := algebra.ring_char_eq F FF, have FFp := hchar.subst hp, haveI := fact.mk FFp, have hFF := ne_of_eq_of_ne hchar.symm hF, -- `ring_char FF β‰  2` have hu : is_unit (ring_char FF : zmod 8), { rw [is_unit_iff_not_dvd_char, ring_char_zmod_n], rw [ne, ← nat.prime_dvd_prime_iff_eq FFp nat.prime_two] at hFF, change Β¬ _ ∣ 2 ^ 3, exact mt FFp.dvd_of_dvd_pow hFF }, -- there is a primitive additive character `β„€/8β„€ β†’ FF`, sending `a + 8β„€ ↦ Ο„^a` -- with a primitive eighth root of unity `Ο„` let Οˆβ‚ˆ := primitive_zmod_char 8 F (by convert hp2 3; norm_num), let Ο„ : FF := Οˆβ‚ˆ.char 1, have Ο„_spec : Ο„ ^ 4 = -1, { refine (sq_eq_one_iff.1 _).resolve_left _; { simp only [Ο„, ← map_nsmul_pow], erw add_char.is_primitive.zmod_char_eq_one_iff 8 Οˆβ‚ˆ.prim, dec_trivial } }, -- we consider `Ο‡β‚ˆ` as a multiplicative character `β„€/8β„€ β†’ FF` let Ο‡ := Ο‡β‚ˆ.ring_hom_comp (int.cast_ring_hom FF), have hΟ‡ : Ο‡ (-1) = 1 := norm_num.int_cast_one, have hq : is_quadratic Ο‡ := is_quadratic_Ο‡β‚ˆ.comp _, -- we now show that the Gauss sum of `Ο‡` and `Οˆβ‚ˆ` has the relevant property have hg : gauss_sum Ο‡ Οˆβ‚ˆ.char ^ 2 = Ο‡ (-1) * fintype.card (zmod 8), { have h := congr_arg (^ 2) (fin.sum_univ_eight $ Ξ» x, (Ο‡β‚ˆ x : FF) * Ο„ ^ x.1), have h₁ : (Ξ» (i : fin 8), ↑(Ο‡β‚ˆ i) * Ο„ ^ i.val) = Ξ» (a : zmod 8), Ο‡ a * Οˆβ‚ˆ.char a, { ext, congr, apply pow_one }, have hβ‚‚ : (0 + 1 * Ο„ ^ 1 + 0 + (-1) * Ο„ ^ 3 + 0 + (-1) * Ο„ ^ 5 + 0 + 1 * Ο„ ^ 7) ^ 2 = 8 + (Ο„ ^ 4 + 1) * (Ο„ ^ 10 - 2 * Ο„ ^ 8 - 2 * Ο„ ^ 6 + 6 * Ο„ ^ 4 + Ο„ ^ 2 - 8) := by ring, have h₃ : 8 + (Ο„ ^ 4 + 1) * (Ο„ ^ 10 - 2 * Ο„ ^ 8 - 2 * Ο„ ^ 6 + 6 * Ο„ ^ 4 + Ο„ ^ 2 - 8) = ↑8 := by { rw Ο„_spec, norm_num }, have hβ‚„ : (0 + 1 * Ο„ ^ 1 + 0 + (-1) * Ο„ ^ 3 + 0 + (-1) * Ο„ ^ 5 + 0 + 1 * Ο„ ^ 7) ^ 2 = ↑8, { rw [← h₃, ← hβ‚‚] }, have hβ‚… : (Ξ» (x : FF), x ^ 2) (↑(Ο‡β‚ˆ 0) * Ο„ ^ 0 + ↑(Ο‡β‚ˆ 1) * Ο„ ^ 1 + ↑(Ο‡β‚ˆ 2) * Ο„ ^ 2 + ↑(Ο‡β‚ˆ 3) * Ο„ ^ 3 + ↑(Ο‡β‚ˆ 4) * Ο„ ^ 4 + ↑(Ο‡β‚ˆ 5) * Ο„ ^ 5 + ↑(Ο‡β‚ˆ 6) * Ο„ ^ 6 + ↑(Ο‡β‚ˆ 7) * Ο„ ^ 7) = ↑8, { simp only [←hβ‚„, Ο‡β‚ˆ_apply, matrix.cons_val_zero, algebra_map.coe_zero, zero_mul, matrix.cons_val_one, matrix.head_cons, algebra_map.coe_one, matrix.cons_vec_bit0_eq_alt0, matrix.cons_vec_append, matrix.cons_vec_alt0, matrix.cons_vec_bit1_eq_alt1, matrix.cons_vec_alt1, int.cast_neg] }, simpa only [hΟ‡, one_mul, card, gauss_sum, ← hβ‚…, h₁] using h, }, -- this allows us to apply `card_pow_char_pow` to our situation have h := char.card_pow_char_pow hq Οˆβ‚ˆ.char (ring_char FF) n hu hFF hg, rw [card, ← hchar, hΟ‡, one_mul, ← hc, ← nat.cast_pow (ring_char F), ← hc] at h, -- finally, we change `2` to `8` on the left hand side convert_to (8 : F) ^ (fintype.card F / 2) = _, { rw [(by norm_num : (8 : F) = 2 ^ 2 * 2), mul_pow, (finite_field.is_square_iff hF $ hp2 2).mp ⟨2, pow_two 2⟩, one_mul] }, apply (algebra_map F FF).injective, simp only [map_pow, map_bit0, map_one, map_int_cast], convert h, norm_num, end end gauss_sum_two
2d79b578c04c8c3d00d0526324306bfefcefdc59
aa101d73b1a3173c7ec56de02b96baa8ca64c42e
/src/solutions/02_iff_if_and.lean
b97ed1bb0e01f24caba9d5533ad8e1d85550fcf3
[ "Apache-2.0" ]
permissive
gihanmarasingha/tutorials
b554d4d53866c493c4341dc13e914b01444e95a6
56617114ef0f9f7b808476faffd11e22e4380918
refs/heads/master
1,671,141,758,153
1,599,173,318,000
1,599,173,318,000
282,405,870
0
0
Apache-2.0
1,595,666,751,000
1,595,666,750,000
null
UTF-8
Lean
false
false
15,323
lean
import data.real.basic /- In the previous file, we saw how to rewrite using equalities. The analogue operation with mathematical statements is rewriting using equivalences. This is also done using the `rw` tactic. Lean uses ↔ to denote equivalence instead of ⇔. In the following exercises we will use the lemma: sub_nonneg {x y : ℝ} : 0 ≀ y - x ↔ x ≀ y The curly braces around x and y instead of parentheses mean Lean will always try to figure out what x and y are from context, unless we really insist on telling it (we'll see how to insist much later). Let's not worry about that for now. In order to announce an intermediate statement we use: have my_name : my statement, This triggers the apparition of a new goal: proving the statement. After this is done, the statement becomes available under the name `my_name`. We can focus on the current goal by typing tactics between curly braces. -/ example {a b c : ℝ} (hab : a ≀ b) : c + a ≀ c + b := begin rw ← sub_nonneg, have key : (c + b) - (c + a) = b - a, -- Here we introduce an intermediate statement named key { ring, }, -- and prove it between curly braces rw key, -- we can now use the key statement rw sub_nonneg, exact hab, end /- Of course the previous lemma is already in the core library, named `add_le_add_left`, so we can use it below. Let's prove a variation (without invoking commutativity of addition since this would spoil our fun). -/ -- 0009 example {a b : ℝ} (hab : a ≀ b) (c : ℝ) : a + c ≀ b + c := begin -- sorry have key : (b + c) - (a + c) = b - a, { ring }, rw ← sub_nonneg, rw key, rw sub_nonneg, exact hab, -- sorry end /- Let's see how we could use this lemma. It is already in the core library, under the name `add_le_add_right`: add_le_add_right {a b : ℝ} (hab : a ≀ b) (c : ℝ) : a + c ≀ b + c This can be read as: "add_le_add_right is a function that will take as input real numbers a and b, an assumption `hab` claiming a ≀ b and a real number c, and will output a proof of a + c ≀ b + c". In addition, recall that curly braces around a b mean Lean will figure out those arguments unless we insist to help. This is because they can be deduced from the next argument `hab`. So it will be sufficient to feed `hab` and c to this function. -/ example {a b : ℝ} (ha : 0 ≀ a) : b ≀ a + b := begin calc b = 0 + b : by ring ... ≀ a + b : by exact add_le_add_right ha b, end /- In the second line of the above proof, we need to prove 0 + b ≀ a + b. The proof after the colon says: this is exactly lemma `add_le_add_right` applied to ha and b. Actually the `calc` block expects proof terms, and the `by` keyword is used to tell Lean we will use tactics to build such a proof term. But since the only tactic used in this block is `exact`, we can skip tactics entirely, and write: -/ example (a b : ℝ) (ha : 0 ≀ a) : b ≀ a + b := begin calc b = 0 + b : by ring ... ≀ a + b : add_le_add_right ha b, end /- Let's do a variant. -/ -- 0010 example (a b : ℝ) (hb : 0 ≀ b) : a ≀ a + b := begin -- sorry calc a = a + 0 : by ring ... ≀ a + b : add_le_add_left hb a, -- sorry end /- The two preceding examples are in the core library : le_add_of_nonneg_left {a b : ℝ} (ha : 0 ≀ a) : b ≀ a + b le_add_of_nonneg_right {a b : ℝ} (hb : 0 ≀ b) : a ≀ a + b Again, there won't be any need to memorize those names, we will soon see how to get rid of such goals automatically. But we can already try to understand how their names are built: "le_add" describe the conclusion "less or equal than some addition" It comes first because we are focussed on proving stuff, and auto-completion works by looking at the beginning of words. "of" introduces assumptions. "nonneg" is Lean's abbreviation for non-negative. "left" or "right" disambiguates between the two variations. Let's use those lemmas by hand for now. -/ -- 0011 example (a b : ℝ) (ha : 0 ≀ a) (hb : 0 ≀ b) : 0 ≀ a + b := begin -- sorry calc 0 ≀ a : ha ... ≀ a + b : le_add_of_nonneg_right hb, -- sorry end /- And let's combine with our earlier lemmas. -/ -- 0012 example (a b c d : ℝ) (hab : a ≀ b) (hcd : c ≀ d) : a + c ≀ b + d := begin -- sorry calc a + c ≀ b + c : add_le_add_right hab c ... ≀ b + d : add_le_add_left hcd b, -- sorry end /- In the above examples, we prepared proofs of assumptions of our lemmas beforehand, so that we could feed them to the lemmas. This is called forward reasonning. The `calc` proofs also belong to this category. We can also announce the use of a lemma, and provide proofs after the fact, using the `apply` tactic. This is called backward reasonning because we get the conclusion first, and provide proofs later. Using `rw` on the goal (rather than on an assumption from the local context) is also backward reasonning. Let's do that using the lemma mul_nonneg {x y : ℝ} (hx : 0 ≀ x) (hy : 0 ≀ y) : 0 ≀ x*y -/ example (a b c : ℝ) (hc : 0 ≀ c) (hab : a ≀ b) : a*c ≀ b*c := begin rw ← sub_nonneg, have key : b*c - a*c = (b - a)*c, { ring }, rw key, apply mul_nonneg, -- Here we don't provide proofs for the lemma's assumptions -- Now we need to provide the proofs. { rw sub_nonneg, exact hab }, { exact hc }, end /- Let's prove the same statement using only forward reasonning: announcing stuff, proving it by working with known facts, moving forward. -/ example (a b c : ℝ) (hc : 0 ≀ c) (hab : a ≀ b) : a*c ≀ b*c := begin have hab' : 0 ≀ b - a, { rw ← sub_nonneg at hab, exact hab, }, have h₁ : 0 ≀ (b - a)*c, { exact mul_nonneg hab' hc }, have hβ‚‚ : (b - a)*c = b*c - a*c, { ring, }, have h₃ : 0 ≀ b*c - a*c, { rw hβ‚‚ at h₁, exact h₁, }, rw sub_nonneg at h₃, exact h₃, end /- One reason why the backward reasoning proof is shorter is because Lean can infer of lot of things by comparing the goal and the lemma statement. Indeed in the `apply mul_nonneg` line, we didn't need to tell Lean that x = b - a and y = c in the lemma. It was infered by "unification" between the lemma statement and the goal. To be fair to the forward reasoning version, we should introduce a convenient variation on `rw`. The `rwa` tactic performs rewrite and then looks for an assumption matching the goal. We can use it to rewrite our latest proof as: -/ example (a b c : ℝ) (hc : 0 ≀ c) (hab : a ≀ b) : a*c ≀ b*c := begin have hab' : 0 ≀ b - a, { rwa ← sub_nonneg at hab, }, have h₁ : 0 ≀ (b - a)*c, { exact mul_nonneg hab' hc }, have hβ‚‚ : (b - a)*c = b*c - a*c, { ring, }, have h₃ : 0 ≀ b*c - a*c, { rwa hβ‚‚ at h₁, }, rwa sub_nonneg at h₃, end /- Let's now combine forward and backward reasonning, to get our most efficient proof of this statement. Note in particular how unification is used to know what to prove inside the parentheses in the `mul_nonneg` arguments. -/ example (a b c : ℝ) (hc : 0 ≀ c) (hab : a ≀ b) : a*c ≀ b*c := begin rw ← sub_nonneg, calc 0 ≀ (b - a)*c : mul_nonneg (by rwa sub_nonneg) hc ... = b*c - a*c : by ring, end /- Let's now practice all three styles using: mul_nonneg_of_nonpos_of_nonpos {a b : Ξ±} (ha : a ≀ 0) (hb : b ≀ 0) : 0 ≀ a * b sub_nonpos {a b : Ξ±} : a - b ≀ 0 ↔ a ≀ b -/ /- First using mostly backward reasonning -/ -- 0013 example (a b c : ℝ) (hc : c ≀ 0) (hab : a ≀ b) : b*c ≀ a*c := begin -- sorry rw ← sub_nonneg, have fact : a*c - b*c = (a - b)*c, ring, rw fact, apply mul_nonneg_of_nonpos_of_nonpos, { rwa sub_nonpos }, { exact hc }, -- sorry end /- Using forward reasonning -/ -- 0014 example (a b c : ℝ) (hc : c ≀ 0) (hab : a ≀ b) : b*c ≀ a*c := begin -- sorry have hab' : a - b ≀ 0, { rwa ← sub_nonpos at hab, }, have h₁ : 0 ≀ (a - b)*c, { exact mul_nonneg_of_nonpos_of_nonpos hab' hc }, have hβ‚‚ : (a - b)*c = a*c - b*c, { ring, }, have h₃ : 0 ≀ a*c - b*c, { rwa hβ‚‚ at h₁, }, rwa sub_nonneg at h₃, -- sorry end /-- Using a combination of both, with a `calc` block -/ -- 0015 example (a b c : ℝ) (hc : c ≀ 0) (hab : a ≀ b) : b*c ≀ a*c := begin -- sorry have hab' : a - b ≀ 0, { rwa sub_nonpos }, rw ← sub_nonneg, calc 0 ≀ (a - b)*c : mul_nonneg_of_nonpos_of_nonpos hab' hc ... = a*c - b*c : by ring, -- sorry end /- Let's now move to proving implications. Lean denotes implications using a simple arrow β†’, the same it uses for functions (say denoting the type of functions from β„• to β„• by β„• β†’ β„•). This is because it sees a proof of P β‡’ Q as a function turning a proof of P into a proof Q. Many of the examples that we already met are implications under the hood. For instance we proved le_add_of_nonneg_left (a b : ℝ) (ha : 0 ≀ a) : b ≀ a + b But this can be rephrased as le_add_of_nonneg_left (a b : ℝ) : 0 ≀ a β†’ b ≀ a + b In order to prove P β†’ Q, we use the tactic `intros`, followed by an assumption name. This creates an assumption with that name asserting that P holds, and turns the goal into Q. Let's check we can go from our old version of `le_add_of_nonneg_left` to the new one. -/ example (a b : ℝ): 0 ≀ a β†’ b ≀ a + b := begin intros ha, exact le_add_of_nonneg_left ha, end /- Actually Lean doesn't make any difference between those two versions. It is also happy with -/ example (a b : ℝ): 0 ≀ a β†’ b ≀ a + b := le_add_of_nonneg_left /- No tactic state is shown in the above line because we don't even need to enter tactic mode using `begin` or `by`. Let's practise using `intros`. -/ -- 0016 example (a b : ℝ): 0 ≀ b β†’ a ≀ a + b := begin -- sorry intros hb, calc a = a + 0 : by ring ... ≀ a + b : add_le_add_left hb a, -- sorry end /- What about lemmas having more than one assumption? For instance: add_nonneg {a b : ℝ}Β (ha : 0 ≀ a) (hb : 0 ≀ b) : 0 ≀ a + b A natural idea is to use the conjunction operator (logical AND), which Lean denotes by ∧. Assumptions built using this operator can be decomposed using the `cases` tactic, which is a very general assumption-decomposing tactic. -/ example {a b : ℝ} : (0 ≀ a ∧ 0 ≀ b) β†’ 0 ≀ a + b := begin intros hyp, cases hyp with ha hb, exact add_nonneg ha hb, end /- Needing that intermediate line invoking `cases` shows this formulation is not what is used by Lean. It rather sees `add_nonneg` as two nested implications: if a is non-negative then if b is non-negative then a+b is non-negative. It reads funny, but it is much more convenient to use in practice. -/ example {a b : ℝ} : 0 ≀ a β†’ (0 ≀ b β†’ 0 ≀ a + b) := add_nonneg /- The above pattern is so common that implications are defined as right-associative operators, hence parentheses are not needed above. Let's prove that the naive conjunction version implies the funny Lean version. For this we need to know how to prove a conjunction. The `split` tactic creates two goals from a conjunction goal. It can also be used to create two implication goals from an equivalence goal. -/ example {a b : ℝ} (H : (0 ≀ a ∧ 0 ≀ b) β†’ 0 ≀ a + b) : 0 ≀ a β†’ (0 ≀ b β†’ 0 ≀ a + b) := begin intros ha, intros hb, apply H, split, exact ha, exact hb, end /- Let's practice `cases` and `split`. In the next exercise, P, Q and R denote unspecified mathematical statements. -/ -- 0017 example (P Q R : Prop) : P ∧ Q β†’ Q ∧ P := begin -- sorry intro hyp, cases hyp with hP hQ, split, exact hQ, exact hP, -- sorry end /- Of course using `split` only to be able to use `exact` twice in a row feels silly. One can also use the anonymous constructor syntax: ⟨ ⟩ Beware those are not parentheses but angle brackets. This is a generic way of providing compound objects to Lean when Lean already has a very clear idea of what it is waiting for. So we could have replaced the last three lines by: exact ⟨hQ, hP⟩ We can also combine the `intros` steps. We can now compress our earlier proof to: -/ example {a b : ℝ} (H : (0 ≀ a ∧ 0 ≀ b) β†’ 0 ≀ a + b) : 0 ≀ a β†’ (0 ≀ b β†’ 0 ≀ a + b) := begin intros ha hb, exact H ⟨ha, hb⟩, end /- The anonymous contructor trick actually also works in `intros` provided we use its recursive version `rintros`. So we can replace intro h, cases h with h₁ hβ‚‚ by rintros ⟨h₁, hβ‚‚βŸ©, Now redo the previous exercise using all those compressing techniques, in exactly two lines. -/ -- 0018 example (P Q R : Prop): P ∧ Q β†’ Q ∧ P := begin -- sorry rintros ⟨hP, hQ⟩, exact ⟨hQ, hP⟩, -- sorry end /- We are ready to come back to the equivalence between the different formulations of lemmas having two assumptions. Remember the `split` tactic can be used to split an equivalence into two implications. -/ -- 0019 example (P Q R : Prop) : (P ∧ Q β†’ R) ↔ (P β†’ (Q β†’ R)) := begin -- sorry split, { intros hyp hP hQ, exact hyp ⟨hP, hQ⟩ }, { rintro hyp ⟨hP, hQ⟩, exact hyp hP hQ }, -- sorry end /- If you used more than five lines in the above exercise then try to compress things (without simply removing line ends). One last compression technique: given a proof h of a conjunction P ∧ Q, one can get a proof of P using h.left and a proof of Q using h.right, without using cases. One can also use the more generic (but less legible) names h.1 and h.2. Similarly, given a proof h of P ↔ Q, one can get a proof of P β†’ Q using h.mp and a proof of Q β†’ P using h.mpr (or the generic h.1 and h.2 that are even less legible in this case). Before the final exercise in this file, let's make sure we'll be able to leave without learning 10 lemma names. The `linarith` tactic will prove any equality or inequality or contradiction that follows by linear combinations of assumptions from the context (with constant coefficients). -/ example (a b : ℝ) (hb : 0 ≀ b) : a ≀ a + b := begin linarith, end /- Now let's enjoy this for a while. -/ -- 0020 example (a b : ℝ) (ha : 0 ≀ a) (hb : 0 ≀ b) : 0 ≀ a + b := begin -- sorry linarith, -- sorry end /- And let's combine with our earlier lemmas. -/ -- 0021 example (a b c d : ℝ) (hab : a ≀ b) (hcd : c ≀ d) : a + c ≀ b + d := begin -- sorry linarith, -- sorry end /- Final exercise In the last exercise of this file, we will use the divisibility relation on β„•, denoted by ∣ (beware this is a unicode divisibility bar, not the ASCII pipe character), and the gcd function. The definitions are the usual ones, but our goal is to avoid using these definitions and only use the following three lemmas: dvd_refl (a : β„•) : a ∣ a dvd_antisymm {a b : β„•} : a ∣ b β†’ b ∣ a β†’ a = b := dvd_gcd_iff {a b c : β„•} : c ∣ gcd a b ↔ c ∣ a ∧ c ∣ b -/ -- All functions and lemmas below are about natural numbers. open nat -- 0022 example (a b : β„•) : a ∣ b ↔ gcd a b = a := begin -- sorry have fact : gcd a b ∣ a ∧ gcd a b ∣ b, { rw ← dvd_gcd_iff }, split, { intro h, apply dvd_antisymm fact.left, rw dvd_gcd_iff, exact ⟨dvd_refl a, h⟩ }, { intro h, rw ← h, exact fact.right }, -- sorry end
7f4677d8fe1e23f3990990cbd422c6f3c4dcb519
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/linear_algebra/contraction.lean
ff9320cc0c9b4ad56eddce2ad4e4aa3b5adb463a
[ "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
1,832
lean
/- Copyright (c) 2020 Oliver Nash. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Oliver Nash -/ import linear_algebra.dual /-! # Contractions Given modules $M, N$ over a commutative ring $R$, this file defines the natural linear maps: $M^* \otimes M \to R$, $M \otimes M^* \to R$, and $M^* \otimes N β†’ Hom(M, N)$, as well as proving some basic properties of these maps. ## Tags contraction, dual module, tensor product -/ universes u v section contraction open tensor_product open_locale tensor_product variables (R : Type u) (M N : Type v) variables [comm_ring R] [add_comm_group M] [add_comm_group N] [module R M] [module R N] /-- The natural left-handed pairing between a module and its dual. -/ def contract_left : (module.dual R M) βŠ— M β†’β‚—[R] R := (uncurry _ _ _ _).to_fun linear_map.id /-- The natural right-handed pairing between a module and its dual. -/ def contract_right : M βŠ— (module.dual R M) β†’β‚—[R] R := (uncurry _ _ _ _).to_fun (linear_map.flip linear_map.id) /-- The natural map associating a linear map to the tensor product of two modules. -/ def dual_tensor_hom : (module.dual R M) βŠ— N β†’β‚—[R] M β†’β‚—[R] N := let M' := module.dual R M in (uncurry R M' N (M β†’β‚—[R] N) : _ β†’ M' βŠ— N β†’β‚—[R] M β†’β‚—[R] N) linear_map.smul_rightβ‚— variables {R M N} @[simp] lemma contract_left_apply (f : module.dual R M) (m : M) : contract_left R M (f βŠ—β‚œ m) = f m := by apply uncurry_apply @[simp] lemma contract_right_apply (f : module.dual R M) (m : M) : contract_right R M (m βŠ—β‚œ f) = f m := by apply uncurry_apply @[simp] lemma dual_tensor_hom_apply (f : module.dual R M) (m : M) (n : N) : dual_tensor_hom R M N (f βŠ—β‚œ n) m = (f m) β€’ n := by { dunfold dual_tensor_hom, rw uncurry_apply, refl, } end contraction
5f9b3bc2327a37dbc752eefe031dfe825ec6e56a
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/topology/metric_space/gromov_hausdorff.lean
6b1675dc5ae11f080364e1759bb191097cf3de30
[ "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
55,265
lean
/- Copyright (c) 2019 SΓ©bastien GouΓ«zel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: SΓ©bastien GouΓ«zel -/ import topology.metric_space.closeds import set_theory.cardinal import topology.metric_space.gromov_hausdorff_realized import topology.metric_space.completion import topology.metric_space.kuratowski /-! # Gromov-Hausdorff distance This file defines the Gromov-Hausdorff distance on the space of nonempty compact metric spaces up to isometry. We introduce the space of all nonempty compact metric spaces, up to isometry, called `GH_space`, and endow it with a metric space structure. The distance, known as the Gromov-Hausdorff distance, is defined as follows: given two nonempty compact spaces `X` and `Y`, their distance is the minimum Hausdorff distance between all possible isometric embeddings of `X` and `Y` in all metric spaces. To define properly the Gromov-Hausdorff space, we consider the non-empty compact subsets of `β„“^∞(ℝ)` up to isometry, which is a well-defined type, and define the distance as the infimum of the Hausdorff distance over all embeddings in `β„“^∞(ℝ)`. We prove that this coincides with the previous description, as all separable metric spaces embed isometrically into `β„“^∞(ℝ)`, through an embedding called the Kuratowski embedding. To prove that we have a distance, we should show that if spaces can be coupled to be arbitrarily close, then they are isometric. More generally, the Gromov-Hausdorff distance is realized, i.e., there is a coupling for which the Hausdorff distance is exactly the Gromov-Hausdorff distance. This follows from a compactness argument, essentially following from Arzela-Ascoli. ## Main results We prove the most important properties of the Gromov-Hausdorff space: it is a polish space, i.e., it is complete and second countable. We also prove the Gromov compactness criterion. -/ noncomputable theory open_locale classical topological_space universes u v w open classical set function topological_space filter metric quotient open bounded_continuous_function nat int Kuratowski_embedding open sum (inl inr) local attribute [instance] metric_space_sum namespace Gromov_Hausdorff section GH_space /- In this section, we define the Gromov-Hausdorff space, denoted `GH_space` as the quotient of nonempty compact subsets of `β„“^∞(ℝ)` by identifying isometric sets. Using the Kuratwoski embedding, we get a canonical map `to_GH_space` mapping any nonempty compact type to `GH_space`. -/ /-- Equivalence relation identifying two nonempty compact sets which are isometric -/ private definition isometry_rel : nonempty_compacts β„“_infty_ℝ β†’ nonempty_compacts β„“_infty_ℝ β†’ Prop := Ξ» x y, nonempty (x.val ≃ᡒ y.val) /-- This is indeed an equivalence relation -/ private lemma is_equivalence_isometry_rel : equivalence isometry_rel := ⟨λ x, ⟨isometric.refl _⟩, Ξ» x y ⟨e⟩, ⟨e.symm⟩, Ξ» x y z ⟨e⟩ ⟨f⟩, ⟨e.trans f⟩⟩ /-- setoid instance identifying two isometric nonempty compact subspaces of β„“^∞(ℝ) -/ instance isometry_rel.setoid : setoid (nonempty_compacts β„“_infty_ℝ) := setoid.mk isometry_rel is_equivalence_isometry_rel /-- The Gromov-Hausdorff space -/ definition GH_space : Type := quotient (isometry_rel.setoid) /-- Map any nonempty compact type to `GH_space` -/ definition to_GH_space (X : Type u) [metric_space X] [compact_space X] [nonempty X] : GH_space := ⟦nonempty_compacts.Kuratowski_embedding X⟧ instance : inhabited GH_space := ⟨quot.mk _ ⟨{0}, by simp⟩⟩ /-- A metric space representative of any abstract point in `GH_space` -/ @[nolint has_inhabited_instance] definition GH_space.rep (p : GH_space) : Type := (quot.out p).val lemma eq_to_GH_space_iff {X : Type u} [metric_space X] [compact_space X] [nonempty X] {p : nonempty_compacts β„“_infty_ℝ} : ⟦p⟧ = to_GH_space X ↔ βˆƒ Ξ¨ : X β†’ β„“_infty_ℝ, isometry Ξ¨ ∧ range Ξ¨ = p.val := begin simp only [to_GH_space, quotient.eq], split, { assume h, rcases setoid.symm h with ⟨e⟩, have f := (Kuratowski_embedding.isometry X).isometric_on_range.trans e, use Ξ» x, f x, split, { apply isometry_subtype_coe.comp f.isometry }, { rw [range_comp, f.range_eq_univ, set.image_univ, subtype.range_coe] } }, { rintros ⟨Ψ, ⟨isomΞ¨, rangeΨ⟩⟩, have f := ((Kuratowski_embedding.isometry X).isometric_on_range.symm.trans isomΞ¨.isometric_on_range).symm, have E : (range Ξ¨ ≃ᡒ (nonempty_compacts.Kuratowski_embedding X).val) = (p.val ≃ᡒ range (Kuratowski_embedding X)), by { dunfold nonempty_compacts.Kuratowski_embedding, rw [rangeΞ¨]; refl }, have g := cast E f, exact ⟨g⟩ } end lemma eq_to_GH_space {p : nonempty_compacts β„“_infty_ℝ} : ⟦p⟧ = to_GH_space p.val := begin refine eq_to_GH_space_iff.2 ⟨((Ξ» x, x) : p.val β†’ β„“_infty_ℝ), _, subtype.range_coe⟩, apply isometry_subtype_coe end section local attribute [reducible] GH_space.rep instance rep_GH_space_metric_space {p : GH_space} : metric_space (p.rep) := by apply_instance instance rep_GH_space_compact_space {p : GH_space} : compact_space (p.rep) := by apply_instance instance rep_GH_space_nonempty {p : GH_space} : nonempty (p.rep) := by apply_instance end lemma GH_space.to_GH_space_rep (p : GH_space) : to_GH_space (p.rep) = p := begin change to_GH_space (quot.out p).val = p, rw ← eq_to_GH_space, exact quot.out_eq p end /-- Two nonempty compact spaces have the same image in `GH_space` if and only if they are isometric. -/ lemma to_GH_space_eq_to_GH_space_iff_isometric {X : Type u} [metric_space X] [compact_space X] [nonempty X] {Y : Type v} [metric_space Y] [compact_space Y] [nonempty Y] : to_GH_space X = to_GH_space Y ↔ nonempty (X ≃ᡒ Y) := ⟨begin simp only [to_GH_space, quotient.eq], assume h, rcases h with ⟨e⟩, have I : ((nonempty_compacts.Kuratowski_embedding X).val ≃ᡒ (nonempty_compacts.Kuratowski_embedding Y).val) = ((range (Kuratowski_embedding X)) ≃ᡒ (range (Kuratowski_embedding Y))), by { dunfold nonempty_compacts.Kuratowski_embedding, refl }, have e' := cast I e, have f := (Kuratowski_embedding.isometry X).isometric_on_range, have g := (Kuratowski_embedding.isometry Y).isometric_on_range.symm, have h := (f.trans e').trans g, exact ⟨h⟩ end, begin rintros ⟨e⟩, simp only [to_GH_space, quotient.eq], have f := (Kuratowski_embedding.isometry X).isometric_on_range.symm, have g := (Kuratowski_embedding.isometry Y).isometric_on_range, have h := (f.trans e).trans g, have I : ((range (Kuratowski_embedding X)) ≃ᡒ (range (Kuratowski_embedding Y))) = ((nonempty_compacts.Kuratowski_embedding X).val ≃ᡒ (nonempty_compacts.Kuratowski_embedding Y).val), by { dunfold nonempty_compacts.Kuratowski_embedding, refl }, have h' := cast I h, exact ⟨h'⟩ end⟩ /-- Distance on `GH_space`: the distance between two nonempty compact spaces is the infimum Hausdorff distance between isometric copies of the two spaces in a metric space. For the definition, we only consider embeddings in `β„“^∞(ℝ)`, but we will prove below that it works for all spaces. -/ instance : has_dist (GH_space) := { dist := Ξ» x y, Inf $ (Ξ» p : nonempty_compacts β„“_infty_ℝ Γ— nonempty_compacts β„“_infty_ℝ, Hausdorff_dist p.1.val p.2.val) '' (set.prod {a | ⟦a⟧ = x} {b | ⟦b⟧ = y}) } /-- The Gromov-Hausdorff distance between two nonempty compact metric spaces, equal by definition to the distance of the equivalence classes of these spaces in the Gromov-Hausdorff space. -/ def GH_dist (X : Type u) (Y : Type v) [metric_space X] [nonempty X] [compact_space X] [metric_space Y] [nonempty Y] [compact_space Y] : ℝ := dist (to_GH_space X) (to_GH_space Y) lemma dist_GH_dist (p q : GH_space) : dist p q = GH_dist (p.rep) (q.rep) := by rw [GH_dist, p.to_GH_space_rep, q.to_GH_space_rep] /-- The Gromov-Hausdorff distance between two spaces is bounded by the Hausdorff distance of isometric copies of the spaces, in any metric space. -/ theorem GH_dist_le_Hausdorff_dist {X : Type u} [metric_space X] [compact_space X] [nonempty X] {Y : Type v} [metric_space Y] [compact_space Y] [nonempty Y] {Ξ³ : Type w} [metric_space Ξ³] {Ξ¦ : X β†’ Ξ³} {Ξ¨ : Y β†’ Ξ³} (ha : isometry Ξ¦) (hb : isometry Ξ¨) : GH_dist X Y ≀ Hausdorff_dist (range Ξ¦) (range Ξ¨) := begin /- For the proof, we want to embed `Ξ³` in `β„“^∞(ℝ)`, to say that the Hausdorff distance is realized in `β„“^∞(ℝ)` and therefore bounded below by the Gromov-Hausdorff-distance. However, `Ξ³` is not separable in general. We restrict to the union of the images of `X` and `Y` in `Ξ³`, which is separable and therefore embeddable in `β„“^∞(ℝ)`. -/ rcases exists_mem_of_nonempty X with ⟨xX, _⟩, let s : set Ξ³ := (range Ξ¦) βˆͺ (range Ξ¨), let Ξ¦' : X β†’ subtype s := Ξ» y, ⟨Φ y, mem_union_left _ (mem_range_self _)⟩, let Ξ¨' : Y β†’ subtype s := Ξ» y, ⟨Ψ y, mem_union_right _ (mem_range_self _)⟩, have IΞ¦' : isometry Ξ¦' := Ξ» x y, ha x y, have IΞ¨' : isometry Ξ¨' := Ξ» x y, hb x y, have : is_compact s, from (is_compact_range ha.continuous).union (is_compact_range hb.continuous), letI : metric_space (subtype s) := by apply_instance, haveI : compact_space (subtype s) := ⟨is_compact_iff_is_compact_univ.1 β€Ήis_compact sβ€ΊβŸ©, haveI : nonempty (subtype s) := ⟨Φ' xX⟩, have ΦΦ' : Ξ¦ = subtype.val ∘ Ξ¦', by { funext, refl }, have ΨΨ' : Ξ¨ = subtype.val ∘ Ξ¨', by { funext, refl }, have : Hausdorff_dist (range Ξ¦) (range Ξ¨) = Hausdorff_dist (range Ξ¦') (range Ξ¨'), { rw [ΦΦ', ΨΨ', range_comp, range_comp], exact Hausdorff_dist_image (isometry_subtype_coe) }, rw this, -- Embed `s` in `β„“^∞(ℝ)` through its Kuratowski embedding let F := Kuratowski_embedding (subtype s), have : Hausdorff_dist (F '' (range Ξ¦')) (F '' (range Ξ¨')) = Hausdorff_dist (range Ξ¦') (range Ξ¨') := Hausdorff_dist_image (Kuratowski_embedding.isometry _), rw ← this, -- Let `A` and `B` be the images of `X` and `Y` under this embedding. They are in `β„“^∞(ℝ)`, and -- their Hausdorff distance is the same as in the original space. let A : nonempty_compacts β„“_infty_ℝ := ⟨F '' (range Ξ¦'), ⟨(range_nonempty _).image _, (is_compact_range IΞ¦'.continuous).image (Kuratowski_embedding.isometry _).continuous⟩⟩, let B : nonempty_compacts β„“_infty_ℝ := ⟨F '' (range Ξ¨'), ⟨(range_nonempty _).image _, (is_compact_range IΞ¨'.continuous).image (Kuratowski_embedding.isometry _).continuous⟩⟩, have AX : ⟦A⟧ = to_GH_space X, { rw eq_to_GH_space_iff, exact ⟨λ x, F (Ξ¦' x), ⟨(Kuratowski_embedding.isometry _).comp IΞ¦', by rw range_comp⟩⟩ }, have BY : ⟦B⟧ = to_GH_space Y, { rw eq_to_GH_space_iff, exact ⟨λ x, F (Ξ¨' x), ⟨(Kuratowski_embedding.isometry _).comp IΞ¨', by rw range_comp⟩⟩ }, refine cInf_le ⟨0, begin simp [lower_bounds], assume t _ _ _ _ ht, rw ← ht, exact Hausdorff_dist_nonneg end⟩ _, apply (mem_image _ _ _).2, existsi (⟨A, B⟩ : nonempty_compacts β„“_infty_ℝ Γ— nonempty_compacts β„“_infty_ℝ), simp [AX, BY] end /-- The optimal coupling constructed above realizes exactly the Gromov-Hausdorff distance, essentially by design. -/ lemma Hausdorff_dist_optimal {X : Type u} [metric_space X] [compact_space X] [nonempty X] {Y : Type v} [metric_space Y] [compact_space Y] [nonempty Y] : Hausdorff_dist (range (optimal_GH_injl X Y)) (range (optimal_GH_injr X Y)) = GH_dist X Y := begin inhabit X, inhabit Y, /- we only need to check the inequality `≀`, as the other one follows from the previous lemma. As the Gromov-Hausdorff distance is an infimum, we need to check that the Hausdorff distance in the optimal coupling is smaller than the Hausdorff distance of any coupling. First, we check this for couplings which already have small Hausdorff distance: in this case, the induced "distance" on `X βŠ• Y` belongs to the candidates family introduced in the definition of the optimal coupling, and the conclusion follows from the optimality of the optimal coupling within this family. -/ have A : βˆ€ p q : nonempty_compacts (β„“_infty_ℝ), ⟦p⟧ = to_GH_space X β†’ ⟦q⟧ = to_GH_space Y β†’ Hausdorff_dist (p.val) (q.val) < diam (univ : set X) + 1 + diam (univ : set Y) β†’ Hausdorff_dist (range (optimal_GH_injl X Y)) (range (optimal_GH_injr X Y)) ≀ Hausdorff_dist (p.val) (q.val), { assume p q hp hq bound, rcases eq_to_GH_space_iff.1 hp with ⟨Φ, ⟨Φisom, Ξ¦range⟩⟩, rcases eq_to_GH_space_iff.1 hq with ⟨Ψ, ⟨Ψisom, Ξ¨range⟩⟩, have I : diam (range Ξ¦ βˆͺ range Ξ¨) ≀ 2 * diam (univ : set X) + 1 + 2 * diam (univ : set Y), { rcases exists_mem_of_nonempty X with ⟨xX, _⟩, have : βˆƒ y ∈ range Ξ¨, dist (Ξ¦ xX) y < diam (univ : set X) + 1 + diam (univ : set Y), { rw Ξ¨range, have : Ξ¦ xX ∈ p.val := Ξ¦range β–Έ mem_range_self _, exact exists_dist_lt_of_Hausdorff_dist_lt this bound (Hausdorff_edist_ne_top_of_nonempty_of_bounded p.2.1 q.2.1 p.2.2.bounded q.2.2.bounded) }, rcases this with ⟨y, hy, dy⟩, rcases mem_range.1 hy with ⟨z, hzy⟩, rw ← hzy at dy, have DΞ¦ : diam (range Ξ¦) = diam (univ : set X) := Ξ¦isom.diam_range, have DΞ¨ : diam (range Ξ¨) = diam (univ : set Y) := Ξ¨isom.diam_range, calc diam (range Ξ¦ βˆͺ range Ξ¨) ≀ diam (range Ξ¦) + dist (Ξ¦ xX) (Ξ¨ z) + diam (range Ξ¨) : diam_union (mem_range_self _) (mem_range_self _) ... ≀ diam (univ : set X) + (diam (univ : set X) + 1 + diam (univ : set Y)) + diam (univ : set Y) : by { rw [DΞ¦, DΞ¨], apply add_le_add (add_le_add (le_refl _) (le_of_lt dy)) (le_refl _) } ... = 2 * diam (univ : set X) + 1 + 2 * diam (univ : set Y) : by ring }, let f : X βŠ• Y β†’ β„“_infty_ℝ := Ξ» x, match x with | inl y := Ξ¦ y | inr z := Ξ¨ z end, let F : (X βŠ• Y) Γ— (X βŠ• Y) β†’ ℝ := Ξ» p, dist (f p.1) (f p.2), -- check that the induced "distance" is a candidate have Fgood : F ∈ candidates X Y, { simp only [candidates, forall_const, and_true, add_comm, eq_self_iff_true, dist_eq_zero, and_self, set.mem_set_of_eq], repeat {split}, { exact Ξ» x y, calc F (inl x, inl y) = dist (Ξ¦ x) (Ξ¦ y) : rfl ... = dist x y : Ξ¦isom.dist_eq x y }, { exact Ξ» x y, calc F (inr x, inr y) = dist (Ξ¨ x) (Ξ¨ y) : rfl ... = dist x y : Ξ¨isom.dist_eq x y }, { exact Ξ» x y, dist_comm _ _ }, { exact Ξ» x y z, dist_triangle _ _ _ }, { exact Ξ» x y, calc F (x, y) ≀ diam (range Ξ¦ βˆͺ range Ξ¨) : begin have A : βˆ€ z : X βŠ• Y, f z ∈ range Ξ¦ βˆͺ range Ξ¨, { assume z, cases z, { apply mem_union_left, apply mem_range_self }, { apply mem_union_right, apply mem_range_self } }, refine dist_le_diam_of_mem _ (A _) (A _), rw [Ξ¦range, Ξ¨range], exact (p.2.2.union q.2.2).bounded, end ... ≀ 2 * diam (univ : set X) + 1 + 2 * diam (univ : set Y) : I } }, let Fb := candidates_b_of_candidates F Fgood, have : Hausdorff_dist (range (optimal_GH_injl X Y)) (range (optimal_GH_injr X Y)) ≀ HD Fb := Hausdorff_dist_optimal_le_HD _ _ (candidates_b_of_candidates_mem F Fgood), refine le_trans this (le_of_forall_le_of_dense (Ξ» r hr, _)), have I1 : βˆ€ x : X, (β¨… y, Fb (inl x, inr y)) ≀ r, { assume x, have : f (inl x) ∈ p.val, by { rw [← Ξ¦range], apply mem_range_self }, rcases exists_dist_lt_of_Hausdorff_dist_lt this hr (Hausdorff_edist_ne_top_of_nonempty_of_bounded p.2.1 q.2.1 p.2.2.bounded q.2.2.bounded) with ⟨z, zq, hz⟩, have : z ∈ range Ξ¨, by rwa [← Ξ¨range] at zq, rcases mem_range.1 this with ⟨y, hy⟩, calc (β¨… y, Fb (inl x, inr y)) ≀ Fb (inl x, inr y) : cinfi_le (by simpa using HD_below_aux1 0) y ... = dist (Ξ¦ x) (Ξ¨ y) : rfl ... = dist (f (inl x)) z : by rw hy ... ≀ r : le_of_lt hz }, have I2 : βˆ€ y : Y, (β¨… x, Fb (inl x, inr y)) ≀ r, { assume y, have : f (inr y) ∈ q.val, by { rw [← Ξ¨range], apply mem_range_self }, rcases exists_dist_lt_of_Hausdorff_dist_lt' this hr (Hausdorff_edist_ne_top_of_nonempty_of_bounded p.2.1 q.2.1 p.2.2.bounded q.2.2.bounded) with ⟨z, zq, hz⟩, have : z ∈ range Ξ¦, by rwa [← Ξ¦range] at zq, rcases mem_range.1 this with ⟨x, hx⟩, calc (β¨… x, Fb (inl x, inr y)) ≀ Fb (inl x, inr y) : cinfi_le (by simpa using HD_below_aux2 0) x ... = dist (Ξ¦ x) (Ξ¨ y) : rfl ... = dist z (f (inr y)) : by rw hx ... ≀ r : le_of_lt hz }, simp [HD, csupr_le I1, csupr_le I2] }, /- Get the same inequality for any coupling. If the coupling is quite good, the desired inequality has been proved above. If it is bad, then the inequality is obvious. -/ have B : βˆ€ p q : nonempty_compacts (β„“_infty_ℝ), ⟦p⟧ = to_GH_space X β†’ ⟦q⟧ = to_GH_space Y β†’ Hausdorff_dist (range (optimal_GH_injl X Y)) (range (optimal_GH_injr X Y)) ≀ Hausdorff_dist (p.val) (q.val), { assume p q hp hq, by_cases h : Hausdorff_dist (p.val) (q.val) < diam (univ : set X) + 1 + diam (univ : set Y), { exact A p q hp hq h }, { calc Hausdorff_dist (range (optimal_GH_injl X Y)) (range (optimal_GH_injr X Y)) ≀ HD (candidates_b_dist X Y) : Hausdorff_dist_optimal_le_HD _ _ (candidates_b_dist_mem_candidates_b) ... ≀ diam (univ : set X) + 1 + diam (univ : set Y) : HD_candidates_b_dist_le ... ≀ Hausdorff_dist (p.val) (q.val) : not_lt.1 h } }, refine le_antisymm _ _, { apply le_cInf, { refine (set.nonempty.prod _ _).image _; exact ⟨_, rfl⟩ }, { rintro b ⟨⟨p, q⟩, ⟨hp, hq⟩, rfl⟩, exact B p q hp hq } }, { exact GH_dist_le_Hausdorff_dist (isometry_optimal_GH_injl X Y) (isometry_optimal_GH_injr X Y) } end /-- The Gromov-Hausdorff distance can also be realized by a coupling in `β„“^∞(ℝ)`, by embedding the optimal coupling through its Kuratowski embedding. -/ theorem GH_dist_eq_Hausdorff_dist (X : Type u) [metric_space X] [compact_space X] [nonempty X] (Y : Type v) [metric_space Y] [compact_space Y] [nonempty Y] : βˆƒ Ξ¦ : X β†’ β„“_infty_ℝ, βˆƒ Ξ¨ : Y β†’ β„“_infty_ℝ, isometry Ξ¦ ∧ isometry Ξ¨ ∧ GH_dist X Y = Hausdorff_dist (range Ξ¦) (range Ξ¨) := begin let F := Kuratowski_embedding (optimal_GH_coupling X Y), let Ξ¦ := F ∘ optimal_GH_injl X Y, let Ξ¨ := F ∘ optimal_GH_injr X Y, refine ⟨Φ, Ξ¨, _, _, _⟩, { exact (Kuratowski_embedding.isometry _).comp (isometry_optimal_GH_injl X Y) }, { exact (Kuratowski_embedding.isometry _).comp (isometry_optimal_GH_injr X Y) }, { rw [← image_univ, ← image_univ, image_comp F, image_univ, image_comp F (optimal_GH_injr X Y), image_univ, ← Hausdorff_dist_optimal], exact (Hausdorff_dist_image (Kuratowski_embedding.isometry _)).symm }, end /-- The Gromov-Hausdorff distance defines a genuine distance on the Gromov-Hausdorff space. -/ instance : metric_space GH_space := { dist_self := Ξ» x, begin rcases exists_rep x with ⟨y, hy⟩, refine le_antisymm _ _, { apply cInf_le, { exact ⟨0, by { rintro b ⟨⟨u, v⟩, ⟨hu, hv⟩, rfl⟩, exact Hausdorff_dist_nonneg } ⟩}, { simp, existsi [y, y], simpa } }, { apply le_cInf, { exact (nonempty.prod ⟨y, hy⟩ ⟨y, hy⟩).image _ }, { rintro b ⟨⟨u, v⟩, ⟨hu, hv⟩, rfl⟩, exact Hausdorff_dist_nonneg } }, end, dist_comm := Ξ» x y, begin have A : (Ξ» (p : nonempty_compacts β„“_infty_ℝ Γ— nonempty_compacts β„“_infty_ℝ), Hausdorff_dist ((p.fst).val) ((p.snd).val)) '' (set.prod {a | ⟦a⟧ = x} {b | ⟦b⟧ = y}) = ((Ξ» (p : nonempty_compacts β„“_infty_ℝ Γ— nonempty_compacts β„“_infty_ℝ), Hausdorff_dist ((p.fst).val) ((p.snd).val)) ∘ prod.swap) '' (set.prod {a | ⟦a⟧ = x} {b | ⟦b⟧ = y}) := by { congr, funext, simp, rw Hausdorff_dist_comm }, simp only [dist, A, image_comp, image_swap_prod], end, eq_of_dist_eq_zero := Ξ» x y hxy, begin /- To show that two spaces at zero distance are isometric, we argue that the distance is realized by some coupling. In this coupling, the two spaces are at zero Hausdorff distance, i.e., they coincide. Therefore, the original spaces are isometric. -/ rcases GH_dist_eq_Hausdorff_dist x.rep y.rep with ⟨Φ, Ξ¨, Ξ¦isom, Ξ¨isom, DΦΨ⟩, rw [← dist_GH_dist, hxy] at DΦΨ, have : range Ξ¦ = range Ξ¨, { have hΞ¦ : is_compact (range Ξ¦) := is_compact_range Ξ¦isom.continuous, have hΞ¨ : is_compact (range Ξ¨) := is_compact_range Ξ¨isom.continuous, apply (is_closed.Hausdorff_dist_zero_iff_eq _ _ _).1 (DΦΨ.symm), { exact hΞ¦.is_closed }, { exact hΞ¨.is_closed }, { exact Hausdorff_edist_ne_top_of_nonempty_of_bounded (range_nonempty _) (range_nonempty _) hΞ¦.bounded hΞ¨.bounded } }, have T : ((range Ξ¨) ≃ᡒ y.rep) = ((range Ξ¦) ≃ᡒ y.rep), by rw this, have eΞ¨ := cast T Ξ¨isom.isometric_on_range.symm, have e := Ξ¦isom.isometric_on_range.trans eΞ¨, rw [← x.to_GH_space_rep, ← y.to_GH_space_rep, to_GH_space_eq_to_GH_space_iff_isometric], exact ⟨e⟩ end, dist_triangle := Ξ» x y z, begin /- To show the triangular inequality between `X`, `Y` and `Z`, realize an optimal coupling between `X` and `Y` in a space `Ξ³1`, and an optimal coupling between `Y` and `Z` in a space `Ξ³2`. Then, glue these metric spaces along `Y`. We get a new space `Ξ³` in which `X` and `Y` are optimally coupled, as well as `Y` and `Z`. Apply the triangle inequality for the Hausdorff distance in `Ξ³` to conclude. -/ let X := x.rep, let Y := y.rep, let Z := z.rep, let Ξ³1 := optimal_GH_coupling X Y, let Ξ³2 := optimal_GH_coupling Y Z, let Ξ¦ : Y β†’ Ξ³1 := optimal_GH_injr X Y, have hΞ¦ : isometry Ξ¦ := isometry_optimal_GH_injr X Y, let Ξ¨ : Y β†’ Ξ³2 := optimal_GH_injl Y Z, have hΞ¨ : isometry Ξ¨ := isometry_optimal_GH_injl Y Z, let Ξ³ := glue_space hΞ¦ hΞ¨, letI : metric_space Ξ³ := metric.metric_space_glue_space hΞ¦ hΞ¨, have Comm : (to_glue_l hΞ¦ hΞ¨) ∘ (optimal_GH_injr X Y) = (to_glue_r hΞ¦ hΞ¨) ∘ (optimal_GH_injl Y Z) := to_glue_commute hΞ¦ hΞ¨, calc dist x z = dist (to_GH_space X) (to_GH_space Z) : by rw [x.to_GH_space_rep, z.to_GH_space_rep] ... ≀ Hausdorff_dist (range ((to_glue_l hΞ¦ hΞ¨) ∘ (optimal_GH_injl X Y))) (range ((to_glue_r hΞ¦ hΞ¨) ∘ (optimal_GH_injr Y Z))) : GH_dist_le_Hausdorff_dist ((to_glue_l_isometry hΞ¦ hΞ¨).comp (isometry_optimal_GH_injl X Y)) ((to_glue_r_isometry hΞ¦ hΞ¨).comp (isometry_optimal_GH_injr Y Z)) ... ≀ Hausdorff_dist (range ((to_glue_l hΞ¦ hΞ¨) ∘ (optimal_GH_injl X Y))) (range ((to_glue_l hΞ¦ hΞ¨) ∘ (optimal_GH_injr X Y))) + Hausdorff_dist (range ((to_glue_l hΞ¦ hΞ¨) ∘ (optimal_GH_injr X Y))) (range ((to_glue_r hΞ¦ hΞ¨) ∘ (optimal_GH_injr Y Z))) : begin refine Hausdorff_dist_triangle (Hausdorff_edist_ne_top_of_nonempty_of_bounded (range_nonempty _) (range_nonempty _) _ _), { exact (is_compact_range (isometry.continuous ((to_glue_l_isometry hΞ¦ hΞ¨).comp (isometry_optimal_GH_injl X Y)))).bounded }, { exact (is_compact_range (isometry.continuous ((to_glue_l_isometry hΞ¦ hΞ¨).comp (isometry_optimal_GH_injr X Y)))).bounded } end ... = Hausdorff_dist ((to_glue_l hΞ¦ hΞ¨) '' (range (optimal_GH_injl X Y))) ((to_glue_l hΞ¦ hΞ¨) '' (range (optimal_GH_injr X Y))) + Hausdorff_dist ((to_glue_r hΞ¦ hΞ¨) '' (range (optimal_GH_injl Y Z))) ((to_glue_r hΞ¦ hΞ¨) '' (range (optimal_GH_injr Y Z))) : by simp only [← range_comp, Comm, eq_self_iff_true, add_right_inj] ... = Hausdorff_dist (range (optimal_GH_injl X Y)) (range (optimal_GH_injr X Y)) + Hausdorff_dist (range (optimal_GH_injl Y Z)) (range (optimal_GH_injr Y Z)) : by rw [Hausdorff_dist_image (to_glue_l_isometry hΞ¦ hΞ¨), Hausdorff_dist_image (to_glue_r_isometry hΞ¦ hΞ¨)] ... = dist (to_GH_space X) (to_GH_space Y) + dist (to_GH_space Y) (to_GH_space Z) : by rw [Hausdorff_dist_optimal, Hausdorff_dist_optimal, GH_dist, GH_dist] ... = dist x y + dist y z: by rw [x.to_GH_space_rep, y.to_GH_space_rep, z.to_GH_space_rep] end } end GH_space --section end Gromov_Hausdorff /-- In particular, nonempty compacts of a metric space map to `GH_space`. We register this in the topological_space namespace to take advantage of the notation `p.to_GH_space`. -/ definition topological_space.nonempty_compacts.to_GH_space {X : Type u} [metric_space X] (p : nonempty_compacts X) : Gromov_Hausdorff.GH_space := Gromov_Hausdorff.to_GH_space p.val open topological_space namespace Gromov_Hausdorff section nonempty_compacts variables {X : Type u} [metric_space X] theorem GH_dist_le_nonempty_compacts_dist (p q : nonempty_compacts X) : dist p.to_GH_space q.to_GH_space ≀ dist p q := begin have ha : isometry (coe : p.val β†’ X) := isometry_subtype_coe, have hb : isometry (coe : q.val β†’ X) := isometry_subtype_coe, have A : dist p q = Hausdorff_dist p.val q.val := rfl, have I : p.val = range (coe : p.val β†’ X), by simp, have J : q.val = range (coe : q.val β†’ X), by simp, rw [I, J] at A, rw A, exact GH_dist_le_Hausdorff_dist ha hb end lemma to_GH_space_lipschitz : lipschitz_with 1 (nonempty_compacts.to_GH_space : nonempty_compacts X β†’ GH_space) := lipschitz_with.mk_one GH_dist_le_nonempty_compacts_dist lemma to_GH_space_continuous : continuous (nonempty_compacts.to_GH_space : nonempty_compacts X β†’ GH_space) := to_GH_space_lipschitz.continuous end nonempty_compacts section /- In this section, we show that if two metric spaces are isometric up to `Ξ΅β‚‚`, then their Gromov-Hausdorff distance is bounded by `Ξ΅β‚‚ / 2`. More generally, if there are subsets which are `Ρ₁`-dense and `Ρ₃`-dense in two spaces, and isometric up to `Ξ΅β‚‚`, then the Gromov-Hausdorff distance between the spaces is bounded by `Ρ₁ + Ξ΅β‚‚/2 + Ρ₃`. For this, we construct a suitable coupling between the two spaces, by gluing them (approximately) along the two matching subsets. -/ variables {X : Type u} [metric_space X] [compact_space X] [nonempty X] {Y : Type v} [metric_space Y] [compact_space Y] [nonempty Y] -- we want to ignore these instances in the following theorem local attribute [instance, priority 10] sum.topological_space sum.uniform_space /-- If there are subsets which are `Ρ₁`-dense and `Ρ₃`-dense in two spaces, and isometric up to `Ξ΅β‚‚`, then the Gromov-Hausdorff distance between the spaces is bounded by `Ρ₁ + Ξ΅β‚‚/2 + Ρ₃`. -/ theorem GH_dist_le_of_approx_subsets {s : set X} (Ξ¦ : s β†’ Y) {Ρ₁ Ξ΅β‚‚ Ρ₃ : ℝ} (hs : βˆ€ x : X, βˆƒ y ∈ s, dist x y ≀ Ρ₁) (hs' : βˆ€ x : Y, βˆƒ y : s, dist x (Ξ¦ y) ≀ Ρ₃) (H : βˆ€ x y : s, |dist x y - dist (Ξ¦ x) (Ξ¦ y)| ≀ Ξ΅β‚‚) : GH_dist X Y ≀ Ρ₁ + Ξ΅β‚‚ / 2 + Ρ₃ := begin refine le_of_forall_pos_le_add (Ξ» Ξ΄ Ξ΄0, _), rcases exists_mem_of_nonempty X with ⟨xX, _⟩, rcases hs xX with ⟨xs, hxs, Dxs⟩, have sne : s.nonempty := ⟨xs, hxs⟩, letI : nonempty s := sne.to_subtype, have : 0 ≀ Ξ΅β‚‚ := le_trans (abs_nonneg _) (H ⟨xs, hxs⟩ ⟨xs, hxs⟩), have : βˆ€ p q : s, |dist p q - dist (Ξ¦ p) (Ξ¦ q)| ≀ 2 * (Ξ΅β‚‚/2 + Ξ΄) := Ξ» p q, calc |dist p q - dist (Ξ¦ p) (Ξ¦ q)| ≀ Ξ΅β‚‚ : H p q ... ≀ 2 * (Ξ΅β‚‚/2 + Ξ΄) : by linarith, -- glue `X` and `Y` along the almost matching subsets letI : metric_space (X βŠ• Y) := glue_metric_approx (Ξ» x:s, (x:X)) (Ξ» x, Ξ¦ x) (Ξ΅β‚‚/2 + Ξ΄) (by linarith) this, let Fl := @sum.inl X Y, let Fr := @sum.inr X Y, have Il : isometry Fl := isometry_emetric_iff_metric.2 (Ξ» x y, rfl), have Ir : isometry Fr := isometry_emetric_iff_metric.2 (Ξ» x y, rfl), /- The proof goes as follows : the `GH_dist` is bounded by the Hausdorff distance of the images in the coupling, which is bounded (using the triangular inequality) by the sum of the Hausdorff distances of `X` and `s` (in the coupling or, equivalently in the original space), of `s` and `Ξ¦ s`, and of `Ξ¦ s` and `Y` (in the coupling or, equivalently, in the original space). The first term is bounded by `Ρ₁`, by `Ρ₁`-density. The third one is bounded by `Ρ₃`. And the middle one is bounded by `Ξ΅β‚‚/2` as in the coupling the points `x` and `Ξ¦ x` are at distance `Ξ΅β‚‚/2` by construction of the coupling (in fact `Ξ΅β‚‚/2 + Ξ΄` where `Ξ΄` is an arbitrarily small positive constant where positivity is used to ensure that the coupling is really a metric space and not a premetric space on `X βŠ• Y`). -/ have : GH_dist X Y ≀ Hausdorff_dist (range Fl) (range Fr) := GH_dist_le_Hausdorff_dist Il Ir, have : Hausdorff_dist (range Fl) (range Fr) ≀ Hausdorff_dist (range Fl) (Fl '' s) + Hausdorff_dist (Fl '' s) (range Fr), { have B : bounded (range Fl) := (is_compact_range Il.continuous).bounded, exact Hausdorff_dist_triangle (Hausdorff_edist_ne_top_of_nonempty_of_bounded (range_nonempty _) (sne.image _) B (B.mono (image_subset_range _ _))) }, have : Hausdorff_dist (Fl '' s) (range Fr) ≀ Hausdorff_dist (Fl '' s) (Fr '' (range Ξ¦)) + Hausdorff_dist (Fr '' (range Ξ¦)) (range Fr), { have B : bounded (range Fr) := (is_compact_range Ir.continuous).bounded, exact Hausdorff_dist_triangle' (Hausdorff_edist_ne_top_of_nonempty_of_bounded ((range_nonempty _).image _) (range_nonempty _) (bounded.mono (image_subset_range _ _) B) B) }, have : Hausdorff_dist (range Fl) (Fl '' s) ≀ Ρ₁, { rw [← image_univ, Hausdorff_dist_image Il], have : 0 ≀ Ρ₁ := le_trans dist_nonneg Dxs, refine Hausdorff_dist_le_of_mem_dist this (Ξ» x hx, hs x) (Ξ» x hx, ⟨x, mem_univ _, by simpa⟩) }, have : Hausdorff_dist (Fl '' s) (Fr '' (range Ξ¦)) ≀ Ξ΅β‚‚/2 + Ξ΄, { refine Hausdorff_dist_le_of_mem_dist (by linarith) _ _, { assume x' hx', rcases (set.mem_image _ _ _).1 hx' with ⟨x, ⟨x_in_s, xx'⟩⟩, rw ← xx', use [Fr (Ξ¦ ⟨x, x_in_s⟩), mem_image_of_mem Fr (mem_range_self _)], exact le_of_eq (glue_dist_glued_points (Ξ» x:s, (x:X)) Ξ¦ (Ξ΅β‚‚/2 + Ξ΄) ⟨x, x_in_s⟩) }, { assume x' hx', rcases (set.mem_image _ _ _).1 hx' with ⟨y, ⟨y_in_s', yx'⟩⟩, rcases mem_range.1 y_in_s' with ⟨x, xy⟩, use [Fl x, mem_image_of_mem _ x.2], rw [← yx', ← xy, dist_comm], exact le_of_eq (glue_dist_glued_points (@subtype.val X s) Ξ¦ (Ξ΅β‚‚/2 + Ξ΄) x) } }, have : Hausdorff_dist (Fr '' (range Ξ¦)) (range Fr) ≀ Ρ₃, { rw [← @image_univ _ _ Fr, Hausdorff_dist_image Ir], rcases exists_mem_of_nonempty Y with ⟨xY, _⟩, rcases hs' xY with ⟨xs', Dxs'⟩, have : 0 ≀ Ρ₃ := le_trans dist_nonneg Dxs', refine Hausdorff_dist_le_of_mem_dist this (Ξ» x hx, ⟨x, mem_univ _, by simpa⟩) (Ξ» x _, _), rcases hs' x with ⟨y, Dy⟩, exact ⟨Φ y, mem_range_self _, Dy⟩ }, linarith end end --section /-- The Gromov-Hausdorff space is second countable. -/ instance : second_countable_topology GH_space := begin refine second_countable_of_countable_discretization (Ξ» Ξ΄ Ξ΄pos, _), let Ξ΅ := (2/5) * Ξ΄, have Ξ΅pos : 0 < Ξ΅ := mul_pos (by norm_num) Ξ΄pos, have : βˆ€ p:GH_space, βˆƒ s : set (p.rep), finite s ∧ (univ βŠ† (⋃x∈s, ball x Ξ΅)) := Ξ» p, by simpa using finite_cover_balls_of_compact (@compact_univ p.rep _ _) Ξ΅pos, -- for each `p`, `s p` is a finite `Ξ΅`-dense subset of `p` (or rather the metric space -- `p.rep` representing `p`) choose s hs using this, have : βˆ€ p:GH_space, βˆ€ t:set (p.rep), finite t β†’ βˆƒ n:β„•, βˆƒ e:equiv t (fin n), true, { assume p t ht, letI : fintype t := finite.fintype ht, exact ⟨fintype.card t, fintype.equiv_fin t, trivial⟩ }, choose N e hne using this, -- cardinality of the nice finite subset `s p` of `p.rep`, called `N p` let N := Ξ» p:GH_space, N p (s p) (hs p).1, -- equiv from `s p`, a nice finite subset of `p.rep`, to `fin (N p)`, called `E p` let E := Ξ» p:GH_space, e p (s p) (hs p).1, -- A function `F` associating to `p : GH_space` the data of all distances between points -- in the `Ξ΅`-dense set `s p`. let F : GH_space β†’ Ξ£n:β„•, (fin n β†’ fin n β†’ β„€) := Ξ»p, ⟨N p, Ξ»a b, ⌊Ρ⁻¹ * dist ((E p).symm a) ((E p).symm b)βŒ‹βŸ©, refine ⟨Σ n, fin n β†’ fin n β†’ β„€, by apply_instance, F, Ξ»p q hpq, _⟩, /- As the target space of F is countable, it suffices to show that two points `p` and `q` with `F p = F q` are at distance `≀ Ξ΄`. For this, we construct a map `Ξ¦` from `s p βŠ† p.rep` (representing `p`) to `q.rep` (representing `q`) which is almost an isometry on `s p`, and with image `s q`. For this, we compose the identification of `s p` with `fin (N p)` and the inverse of the identification of `s q` with `fin (N q)`. Together with the fact that `N p = N q`, this constructs `Ξ¨` between `s p` and `s q`, and then composing with the canonical inclusion we get `Ξ¦`. -/ have Npq : N p = N q := (sigma.mk.inj_iff.1 hpq).1, let Ξ¨ : s p β†’ s q := Ξ» x, (E q).symm (fin.cast Npq ((E p) x)), let Ξ¦ : s p β†’ q.rep := Ξ» x, Ξ¨ x, -- Use the almost isometry `Ξ¦` to show that `p.rep` and `q.rep` -- are within controlled Gromov-Hausdorff distance. have main : GH_dist p.rep q.rep ≀ Ξ΅ + Ξ΅/2 + Ξ΅, { refine GH_dist_le_of_approx_subsets Ξ¦ _ _ _, show βˆ€ x : p.rep, βˆƒ (y : p.rep) (H : y ∈ s p), dist x y ≀ Ξ΅, { -- by construction, `s p` is `Ξ΅`-dense assume x, have : x ∈ ⋃y∈(s p), ball y Ξ΅ := (hs p).2 (mem_univ _), rcases mem_bUnion_iff.1 this with ⟨y, ys, hy⟩, exact ⟨y, ys, le_of_lt hy⟩ }, show βˆ€ x : q.rep, βˆƒ (z : s p), dist x (Ξ¦ z) ≀ Ξ΅, { -- by construction, `s q` is `Ξ΅`-dense, and it is the range of `Ξ¦` assume x, have : x ∈ ⋃y∈(s q), ball y Ξ΅ := (hs q).2 (mem_univ _), rcases mem_bUnion_iff.1 this with ⟨y, ys, hy⟩, let i : β„• := E q ⟨y, ys⟩, let hi := ((E q) ⟨y, ys⟩).is_lt, have ihi_eq : (⟨i, hi⟩ : fin (N q)) = (E q) ⟨y, ys⟩, by rw [fin.ext_iff, fin.coe_mk], have hiq : i < N q := hi, have hip : i < N p, { rwa Npq.symm at hiq }, let z := (E p).symm ⟨i, hip⟩, use z, have C1 : (E p) z = ⟨i, hip⟩ := (E p).apply_symm_apply ⟨i, hip⟩, have C2 : fin.cast Npq ⟨i, hip⟩ = ⟨i, hi⟩ := rfl, have C3 : (E q).symm ⟨i, hi⟩ = ⟨y, ys⟩, by { rw ihi_eq, exact (E q).symm_apply_apply ⟨y, ys⟩ }, have : Ξ¦ z = y := by { simp only [Ξ¦, Ξ¨], rw [C1, C2, C3], refl }, rw this, exact le_of_lt hy }, show βˆ€ x y : s p, |dist x y - dist (Ξ¦ x) (Ξ¦ y)| ≀ Ξ΅, { /- the distance between `x` and `y` is encoded in `F p`, and the distance between `Ξ¦ x` and `Ξ¦ y` (two points of `s q`) is encoded in `F q`, all this up to `Ξ΅`. As `F p = F q`, the distances are almost equal. -/ assume x y, have : dist (Ξ¦ x) (Ξ¦ y) = dist (Ξ¨ x) (Ξ¨ y) := rfl, rw this, -- introduce `i`, that codes both `x` and `Ξ¦ x` in `fin (N p) = fin (N q)` let i : β„• := E p x, have hip : i < N p := ((E p) x).2, have hiq : i < N q, by rwa Npq at hip, have i' : i = ((E q) (Ξ¨ x)), by { simp [Ξ¨] }, -- introduce `j`, that codes both `y` and `Ξ¦ y` in `fin (N p) = fin (N q)` let j : β„• := E p y, have hjp : j < N p := ((E p) y).2, have hjq : j < N q, by rwa Npq at hjp, have j' : j = ((E q) (Ξ¨ y)).1, by { simp [Ξ¨] }, -- Express `dist x y` in terms of `F p` have : (F p).2 ((E p) x) ((E p) y) = floor (Ρ⁻¹ * dist x y), by simp only [F, (E p).symm_apply_apply], have Ap : (F p).2 ⟨i, hip⟩ ⟨j, hjp⟩ = floor (Ρ⁻¹ * dist x y), by { rw ← this, congr; apply (fin.ext_iff _ _).2; refl }, -- Express `dist (Ξ¦ x) (Ξ¦ y)` in terms of `F q` have : (F q).2 ((E q) (Ξ¨ x)) ((E q) (Ξ¨ y)) = floor (Ρ⁻¹ * dist (Ξ¨ x) (Ξ¨ y)), by simp only [F, (E q).symm_apply_apply], have Aq : (F q).2 ⟨i, hiq⟩ ⟨j, hjq⟩ = floor (Ρ⁻¹ * dist (Ξ¨ x) (Ξ¨ y)), by { rw ← this, congr; apply (fin.ext_iff _ _).2; [exact i', exact j'] }, -- use the equality between `F p` and `F q` to deduce that the distances have equal -- integer parts have : (F p).2 ⟨i, hip⟩ ⟨j, hjp⟩ = (F q).2 ⟨i, hiq⟩ ⟨j, hjq⟩, { -- we want to `subst hpq` where `hpq : F p = F q`, except that `subst` only works -- with a constant, so replace `F q` (and everything that depends on it) by a constant `f` -- then `subst` revert hiq hjq, change N q with (F q).1, generalize_hyp : F q = f at hpq ⊒, subst hpq, intros, refl }, rw [Ap, Aq] at this, -- deduce that the distances coincide up to `Ξ΅`, by a straightforward computation -- that should be automated have I := calc |Ρ⁻¹| * |dist x y - dist (Ξ¨ x) (Ξ¨ y)| = |Ρ⁻¹ * (dist x y - dist (Ξ¨ x) (Ξ¨ y))| : (abs_mul _ _).symm ... = |(Ρ⁻¹ * dist x y) - (Ρ⁻¹ * dist (Ξ¨ x) (Ξ¨ y))| : by { congr, ring } ... ≀ 1 : le_of_lt (abs_sub_lt_one_of_floor_eq_floor this), calc |dist x y - dist (Ξ¨ x) (Ξ¨ y)| = (Ξ΅ * Ρ⁻¹) * |dist x y - dist (Ξ¨ x) (Ξ¨ y)| : by rw [mul_inv_cancel (ne_of_gt Ξ΅pos), one_mul] ... = Ξ΅ * (|Ρ⁻¹| * |dist x y - dist (Ξ¨ x) (Ξ¨ y)|) : by rw [abs_of_nonneg (le_of_lt (inv_pos.2 Ξ΅pos)), mul_assoc] ... ≀ Ξ΅ * 1 : mul_le_mul_of_nonneg_left I (le_of_lt Ξ΅pos) ... = Ξ΅ : mul_one _ } }, calc dist p q = GH_dist (p.rep) (q.rep) : dist_GH_dist p q ... ≀ Ξ΅ + Ξ΅/2 + Ξ΅ : main ... = Ξ΄ : by { simp [Ξ΅], ring } end /-- Compactness criterion: a closed set of compact metric spaces is compact if the spaces have a uniformly bounded diameter, and for all `Ξ΅` the number of balls of radius `Ξ΅` required to cover the spaces is uniformly bounded. This is an equivalence, but we only prove the interesting direction that these conditions imply compactness. -/ lemma totally_bounded {t : set GH_space} {C : ℝ} {u : β„• β†’ ℝ} {K : β„• β†’ β„•} (ulim : tendsto u at_top (𝓝 0)) (hdiam : βˆ€ p ∈ t, diam (univ : set (GH_space.rep p)) ≀ C) (hcov : βˆ€ p ∈ t, βˆ€ n:β„•, βˆƒ s : set (GH_space.rep p), cardinal.mk s ≀ K n ∧ univ βŠ† ⋃x∈s, ball x (u n)) : totally_bounded t := begin /- Let `Ξ΄>0`, and `Ξ΅ = Ξ΄/5`. For each `p`, we construct a finite subset `s p` of `p`, which is `Ξ΅`-dense and has cardinality at most `K n`. Encoding the mutual distances of points in `s p`, up to `Ξ΅`, we will get a map `F` associating to `p` finitely many data, and making it possible to reconstruct `p` up to `Ξ΅`. This is enough to prove total boundedness. -/ refine metric.totally_bounded_of_finite_discretization (Ξ» Ξ΄ Ξ΄pos, _), let Ξ΅ := (1/5) * Ξ΄, have Ξ΅pos : 0 < Ξ΅ := mul_pos (by norm_num) Ξ΄pos, -- choose `n` for which `u n < Ξ΅` rcases metric.tendsto_at_top.1 ulim Ξ΅ Ξ΅pos with ⟨n, hn⟩, have u_le_Ξ΅ : u n ≀ Ξ΅, { have := hn n (le_refl _), simp only [real.dist_eq, add_zero, sub_eq_add_neg, neg_zero] at this, exact le_of_lt (lt_of_le_of_lt (le_abs_self _) this) }, -- construct a finite subset `s p` of `p` which is `Ξ΅`-dense and has cardinal `≀ K n` have : βˆ€ p:GH_space, βˆƒ s : set (p.rep), βˆƒ N ≀ K n, βˆƒ E : equiv s (fin N), p ∈ t β†’ univ βŠ† ⋃x∈s, ball x (u n), { assume p, by_cases hp : p βˆ‰ t, { have : nonempty (equiv (βˆ… : set (p.rep)) (fin 0)), { rw ← fintype.card_eq, simp }, use [βˆ…, 0, bot_le, choice (this)] }, { rcases hcov _ (set.not_not_mem.1 hp) n with ⟨s, ⟨scard, scover⟩⟩, rcases cardinal.lt_omega.1 (lt_of_le_of_lt scard (cardinal.nat_lt_omega _)) with ⟨N, hN⟩, rw [hN, cardinal.nat_cast_le] at scard, have : cardinal.mk s = cardinal.mk (fin N), by rw [hN, cardinal.mk_fin], cases quotient.exact this with E, use [s, N, scard, E], simp [hp, scover] } }, choose s N hN E hs using this, -- Define a function `F` taking values in a finite type and associating to `p` enough data -- to reconstruct it up to `Ξ΅`, namely the (discretized) distances between elements of `s p`. let M := ⌊Ρ⁻¹ * max C 0βŒ‹β‚Š, let F : GH_space β†’ (Ξ£k:fin ((K n).succ), (fin k β†’ fin k β†’ fin (M.succ))) := Ξ» p, ⟨⟨N p, lt_of_le_of_lt (hN p) (nat.lt_succ_self _)⟩, Ξ» a b, ⟨min M ⌊Ρ⁻¹ * dist ((E p).symm a) ((E p).symm b)βŒ‹β‚Š, ( min_le_left _ _).trans_lt (nat.lt_succ_self _) ⟩ ⟩, refine ⟨_, _, (Ξ» p, F p), _⟩, apply_instance, -- It remains to show that if `F p = F q`, then `p` and `q` are `Ξ΅`-close rintros ⟨p, pt⟩ ⟨q, qt⟩ hpq, have Npq : N p = N q := (fin.ext_iff _ _).1 (sigma.mk.inj_iff.1 hpq).1, let Ξ¨ : s p β†’ s q := Ξ» x, (E q).symm (fin.cast Npq ((E p) x)), let Ξ¦ : s p β†’ q.rep := Ξ» x, Ξ¨ x, have main : GH_dist (p.rep) (q.rep) ≀ Ξ΅ + Ξ΅/2 + Ξ΅, { -- to prove the main inequality, argue that `s p` is `Ξ΅`-dense in `p`, and `s q` is `Ξ΅`-dense -- in `q`, and `s p` and `s q` are almost isometric. Then closeness follows -- from `GH_dist_le_of_approx_subsets` refine GH_dist_le_of_approx_subsets Ξ¦ _ _ _, show βˆ€ x : p.rep, βˆƒ (y : p.rep) (H : y ∈ s p), dist x y ≀ Ξ΅, { -- by construction, `s p` is `Ξ΅`-dense assume x, have : x ∈ ⋃y∈(s p), ball y (u n) := (hs p pt) (mem_univ _), rcases mem_bUnion_iff.1 this with ⟨y, ys, hy⟩, exact ⟨y, ys, le_trans (le_of_lt hy) u_le_Ρ⟩ }, show βˆ€ x : q.rep, βˆƒ (z : s p), dist x (Ξ¦ z) ≀ Ξ΅, { -- by construction, `s q` is `Ξ΅`-dense, and it is the range of `Ξ¦` assume x, have : x ∈ ⋃y∈(s q), ball y (u n) := (hs q qt) (mem_univ _), rcases mem_bUnion_iff.1 this with ⟨y, ys, hy⟩, let i : β„• := E q ⟨y, ys⟩, let hi := ((E q) ⟨y, ys⟩).2, have ihi_eq : (⟨i, hi⟩ : fin (N q)) = (E q) ⟨y, ys⟩, by rw [fin.ext_iff, fin.coe_mk], have hiq : i < N q := hi, have hip : i < N p, { rwa Npq.symm at hiq }, let z := (E p).symm ⟨i, hip⟩, use z, have C1 : (E p) z = ⟨i, hip⟩ := (E p).apply_symm_apply ⟨i, hip⟩, have C2 : fin.cast Npq ⟨i, hip⟩ = ⟨i, hi⟩ := rfl, have C3 : (E q).symm ⟨i, hi⟩ = ⟨y, ys⟩, by { rw ihi_eq, exact (E q).symm_apply_apply ⟨y, ys⟩ }, have : Ξ¦ z = y := by { simp only [Ξ¦, Ξ¨], rw [C1, C2, C3], refl }, rw this, exact le_trans (le_of_lt hy) u_le_Ξ΅ }, show βˆ€ x y : s p, |dist x y - dist (Ξ¦ x) (Ξ¦ y)| ≀ Ξ΅, { /- the distance between `x` and `y` is encoded in `F p`, and the distance between `Ξ¦ x` and `Ξ¦ y` (two points of `s q`) is encoded in `F q`, all this up to `Ξ΅`. As `F p = F q`, the distances are almost equal. -/ assume x y, have : dist (Ξ¦ x) (Ξ¦ y) = dist (Ξ¨ x) (Ξ¨ y) := rfl, rw this, -- introduce `i`, that codes both `x` and `Ξ¦ x` in `fin (N p) = fin (N q)` let i : β„• := E p x, have hip : i < N p := ((E p) x).2, have hiq : i < N q, by rwa Npq at hip, have i' : i = ((E q) (Ξ¨ x)), by { simp [Ξ¨] }, -- introduce `j`, that codes both `y` and `Ξ¦ y` in `fin (N p) = fin (N q)` let j : β„• := E p y, have hjp : j < N p := ((E p) y).2, have hjq : j < N q, by rwa Npq at hjp, have j' : j = ((E q) (Ξ¨ y)), by { simp [Ξ¨] }, -- Express `dist x y` in terms of `F p` have Ap : ((F p).2 ⟨i, hip⟩ ⟨j, hjp⟩).1 = ⌊Ρ⁻¹ * dist x yβŒ‹β‚Š := calc ((F p).2 ⟨i, hip⟩ ⟨j, hjp⟩).1 = ((F p).2 ((E p) x) ((E p) y)).1 : by { congr; apply (fin.ext_iff _ _).2; refl } ... = min M ⌊Ρ⁻¹ * dist x yβŒ‹β‚Š : by simp only [F, (E p).symm_apply_apply] ... = ⌊Ρ⁻¹ * dist x yβŒ‹β‚Š : begin refine min_eq_right (nat.floor_mono _), refine mul_le_mul_of_nonneg_left (le_trans _ (le_max_left _ _)) ((inv_pos.2 Ξ΅pos).le), change dist (x : p.rep) y ≀ C, refine le_trans (dist_le_diam_of_mem compact_univ.bounded (mem_univ _) (mem_univ _)) _, exact hdiam p pt end, -- Express `dist (Ξ¦ x) (Ξ¦ y)` in terms of `F q` have Aq : ((F q).2 ⟨i, hiq⟩ ⟨j, hjq⟩).1 = ⌊Ρ⁻¹ * dist (Ξ¨ x) (Ξ¨ y)βŒ‹β‚Š := calc ((F q).2 ⟨i, hiq⟩ ⟨j, hjq⟩).1 = ((F q).2 ((E q) (Ξ¨ x)) ((E q) (Ξ¨ y))).1 : by { congr; apply (fin.ext_iff _ _).2; [exact i', exact j'] } ... = min M ⌊Ρ⁻¹ * dist (Ξ¨ x) (Ξ¨ y)βŒ‹β‚Š : by simp only [F, (E q).symm_apply_apply] ... = ⌊Ρ⁻¹ * dist (Ξ¨ x) (Ξ¨ y)βŒ‹β‚Š : begin refine min_eq_right (nat.floor_mono _), refine mul_le_mul_of_nonneg_left (le_trans _ (le_max_left _ _)) ((inv_pos.2 Ξ΅pos).le), change dist (Ξ¨ x : q.rep) (Ξ¨ y) ≀ C, refine le_trans (dist_le_diam_of_mem compact_univ.bounded (mem_univ _) (mem_univ _)) _, exact hdiam q qt end, -- use the equality between `F p` and `F q` to deduce that the distances have equal -- integer parts have : ((F p).2 ⟨i, hip⟩ ⟨j, hjp⟩).1 = ((F q).2 ⟨i, hiq⟩ ⟨j, hjq⟩).1, { -- we want to `subst hpq` where `hpq : F p = F q`, except that `subst` only works -- with a constant, so replace `F q` (and everything that depends on it) by a constant `f` -- then `subst` revert hiq hjq, change N q with (F q).1, generalize_hyp : F q = f at hpq ⊒, subst hpq, intros, refl }, have : ⌊Ρ⁻¹ * dist x yβŒ‹ = ⌊Ρ⁻¹ * dist (Ξ¨ x) (Ξ¨ y)βŒ‹, { rw [Ap, Aq] at this, have D : 0 ≀ ⌊Ρ⁻¹ * dist x yβŒ‹ := floor_nonneg.2 (mul_nonneg (le_of_lt (inv_pos.2 Ξ΅pos)) dist_nonneg), have D' : 0 ≀ ⌊Ρ⁻¹ * dist (Ξ¨ x) (Ξ¨ y)βŒ‹ := floor_nonneg.2 (mul_nonneg (le_of_lt (inv_pos.2 Ξ΅pos)) dist_nonneg), rw [← int.to_nat_of_nonneg D, ← int.to_nat_of_nonneg D', int.floor_to_nat,int.floor_to_nat, this] }, -- deduce that the distances coincide up to `Ξ΅`, by a straightforward computation -- that should be automated have I := calc |Ρ⁻¹| * |dist x y - dist (Ξ¨ x) (Ξ¨ y)| = |Ρ⁻¹ * (dist x y - dist (Ξ¨ x) (Ξ¨ y))| : (abs_mul _ _).symm ... = |(Ρ⁻¹ * dist x y) - (Ρ⁻¹ * dist (Ξ¨ x) (Ξ¨ y))| : by { congr, ring } ... ≀ 1 : le_of_lt (abs_sub_lt_one_of_floor_eq_floor this), calc |dist x y - dist (Ξ¨ x) (Ξ¨ y)| = (Ξ΅ * Ρ⁻¹) * |dist x y - dist (Ξ¨ x) (Ξ¨ y)| : by rw [mul_inv_cancel (ne_of_gt Ξ΅pos), one_mul] ... = Ξ΅ * (|Ρ⁻¹| * |dist x y - dist (Ξ¨ x) (Ξ¨ y)|) : by rw [abs_of_nonneg (le_of_lt (inv_pos.2 Ξ΅pos)), mul_assoc] ... ≀ Ξ΅ * 1 : mul_le_mul_of_nonneg_left I (le_of_lt Ξ΅pos) ... = Ξ΅ : mul_one _ } }, calc dist p q = GH_dist (p.rep) (q.rep) : dist_GH_dist p q ... ≀ Ξ΅ + Ξ΅/2 + Ξ΅ : main ... = Ξ΄/2 : by { simp [Ξ΅], ring } ... < Ξ΄ : half_lt_self Ξ΄pos end section complete /- We will show that a sequence `u n` of compact metric spaces satisfying `dist (u n) (u (n+1)) < 1/2^n` converges, which implies completeness of the Gromov-Hausdorff space. We need to exhibit the limiting compact metric space. For this, start from a sequence `X n` of representatives of `u n`, and glue in an optimal way `X n` to `X (n+1)` for all `n`, in a common metric space. Formally, this is done as follows. Start from `Y 0 = X 0`. Then, glue `X 0` to `X 1` in an optimal way, yielding a space `Y 1` (with an embedding of `X 1`). Then, consider an optimal gluing of `X 1` and `X 2`, and glue it to `Y 1` along their common subspace `X 1`. This gives a new space `Y 2`, with an embedding of `X 2`. Go on, to obtain a sequence of spaces `Y n`. Let `Z0` be the inductive limit of the `Y n`, and finally let `Z` be the completion of `Z0`. The images `X2 n` of `X n` in `Z` are at Hausdorff distance `< 1/2^n` by construction, hence they form a Cauchy sequence for the Hausdorff distance. By completeness (of `Z`, and therefore of its set of nonempty compact subsets), they converge to a limit `L`. This is the nonempty compact metric space we are looking for. -/ variables (X : β„• β†’ Type) [βˆ€ n, metric_space (X n)] [βˆ€ n, compact_space (X n)] [βˆ€ n, nonempty (X n)] /-- Auxiliary structure used to glue metric spaces below, recording an isometric embedding of a type `A` in another metric space. -/ structure aux_gluing_struct (A : Type) [metric_space A] : Type 1 := (space : Type) (metric : metric_space space) (embed : A β†’ space) (isom : isometry embed) instance (A : Type) [metric_space A] : inhabited (aux_gluing_struct A) := ⟨{ space := A, metric := by apply_instance, embed := id, isom := Ξ» x y, rfl }⟩ /-- Auxiliary sequence of metric spaces, containing copies of `X 0`, ..., `X n`, where each `X i` is glued to `X (i+1)` in an optimal way. The space at step `n+1` is obtained from the space at step `n` by adding `X (n+1)`, glued in an optimal way to the `X n` already sitting there. -/ def aux_gluing (n : β„•) : aux_gluing_struct (X n) := nat.rec_on n { space := X 0, metric := by apply_instance, embed := id, isom := Ξ» x y, rfl } (Ξ» n Y, by letI : metric_space Y.space := Y.metric; exact { space := glue_space Y.isom (isometry_optimal_GH_injl (X n) (X (n+1))), metric := by apply_instance, embed := (to_glue_r Y.isom (isometry_optimal_GH_injl (X n) (X (n+1)))) ∘ (optimal_GH_injr (X n) (X (n+1))), isom := (to_glue_r_isometry _ _).comp (isometry_optimal_GH_injr (X n) (X (n+1))) }) /-- The Gromov-Hausdorff space is complete. -/ instance : complete_space GH_space := begin have : βˆ€ (n : β„•), 0 < ((1:ℝ) / 2) ^ n, by { apply pow_pos, norm_num }, -- start from a sequence of nonempty compact metric spaces within distance `1/2^n` of each other refine metric.complete_of_convergent_controlled_sequences (Ξ» n, (1/2)^n) this (Ξ» u hu, _), -- `X n` is a representative of `u n` let X := Ξ» n, (u n).rep, -- glue them together successively in an optimal way, getting a sequence of metric spaces `Y n` let Y := aux_gluing X, letI : βˆ€ n, metric_space (Y n).space := Ξ» n, (Y n).metric, have E : βˆ€ n : β„•, glue_space (Y n).isom (isometry_optimal_GH_injl (X n) (X n.succ)) = (Y n.succ).space := Ξ» n, by { simp [Y, aux_gluing], refl }, let c := Ξ» n, cast (E n), have ic : βˆ€ n, isometry (c n) := Ξ» n x y, rfl, -- there is a canonical embedding of `Y n` in `Y (n+1)`, by construction let f : Ξ n, (Y n).space β†’ (Y n.succ).space := Ξ» n, (c n) ∘ (to_glue_l (aux_gluing X n).isom (isometry_optimal_GH_injl (X n) (X n.succ))), have I : βˆ€ n, isometry (f n), { assume n, apply isometry.comp, { assume x y, refl }, { apply to_glue_l_isometry } }, -- consider the inductive limit `Z0` of the `Y n`, and then its completion `Z` let Z0 := metric.inductive_limit I, let Z := uniform_space.completion Z0, let Ξ¦ := to_inductive_limit I, let coeZ := (coe : Z0 β†’ Z), -- let `X2 n` be the image of `X n` in the space `Z` let X2 := Ξ» n, range (coeZ ∘ (Ξ¦ n) ∘ (Y n).embed), have isom : βˆ€ n, isometry (coeZ ∘ (Ξ¦ n) ∘ (Y n).embed), { assume n, apply isometry.comp completion.coe_isometry _, apply isometry.comp _ (Y n).isom, apply to_inductive_limit_isometry }, -- The Hausdorff distance of `X2 n` and `X2 (n+1)` is by construction the distance between -- `u n` and `u (n+1)`, therefore bounded by `1/2^n` have D2 : βˆ€ n, Hausdorff_dist (X2 n) (X2 n.succ) < (1/2)^n, { assume n, have X2n : X2 n = range ((coeZ ∘ (Ξ¦ n.succ) ∘ (c n) ∘ (to_glue_r (Y n).isom (isometry_optimal_GH_injl (X n) (X n.succ)))) ∘ (optimal_GH_injl (X n) (X n.succ))), { change X2 n = range (coeZ ∘ (Ξ¦ n.succ) ∘ (c n) ∘ (to_glue_r (Y n).isom (isometry_optimal_GH_injl (X n) (X n.succ))) ∘ (optimal_GH_injl (X n) (X n.succ))), simp only [X2, Ξ¦], rw [← to_inductive_limit_commute I], simp only [f], rw ← to_glue_commute }, rw range_comp at X2n, have X2nsucc : X2 n.succ = range ((coeZ ∘ (Ξ¦ n.succ) ∘ (c n) ∘ (to_glue_r (Y n).isom (isometry_optimal_GH_injl (X n) (X n.succ)))) ∘ (optimal_GH_injr (X n) (X n.succ))), by refl, rw range_comp at X2nsucc, rw [X2n, X2nsucc, Hausdorff_dist_image, Hausdorff_dist_optimal, ← dist_GH_dist], { exact hu n n n.succ (le_refl n) (le_succ n) }, { apply isometry.comp completion.coe_isometry _, apply isometry.comp _ ((ic n).comp (to_glue_r_isometry _ _)), apply to_inductive_limit_isometry } }, -- consider `X2 n` as a member `X3 n` of the type of nonempty compact subsets of `Z`, which -- is a metric space let X3 : β„• β†’ nonempty_compacts Z := Ξ» n, ⟨X2 n, ⟨range_nonempty _, is_compact_range (isom n).continuous ⟩⟩, -- `X3 n` is a Cauchy sequence by construction, as the successive distances are -- bounded by `(1/2)^n` have : cauchy_seq X3, { refine cauchy_seq_of_le_geometric (1/2) 1 (by norm_num) (Ξ» n, _), rw one_mul, exact le_of_lt (D2 n) }, -- therefore, it converges to a limit `L` rcases cauchy_seq_tendsto_of_complete this with ⟨L, hL⟩, -- the images of `X3 n` in the Gromov-Hausdorff space converge to the image of `L` have M : tendsto (Ξ» n, (X3 n).to_GH_space) at_top (𝓝 L.to_GH_space) := tendsto.comp (to_GH_space_continuous.tendsto _) hL, -- By construction, the image of `X3 n` in the Gromov-Hausdorff space is `u n`. have : βˆ€ n, (X3 n).to_GH_space = u n, { assume n, rw [nonempty_compacts.to_GH_space, ← (u n).to_GH_space_rep, to_GH_space_eq_to_GH_space_iff_isometric], constructor, convert (isom n).isometric_on_range.symm, }, -- Finally, we have proved the convergence of `u n` exact ⟨L.to_GH_space, by simpa [this] using M⟩ end end complete--section end Gromov_Hausdorff --namespace
72657b9d2e69bf6de4bb5780dcd7e6e310887708
d1a52c3f208fa42c41df8278c3d280f075eb020c
/tests/lean/run/irCompilerBug.lean
08d6f5649d2a15fd79bf79da778ddafd2ae07a19
[ "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
795
lean
-- structure Payload := (key : Nat) (val : Nat) @[noinline] def get? (p : Payload) (k : Nat) : OptionM Nat := if p.key == k then p.val else none inductive T | a (i : Nat) | b (i : Nat) | c (i : Nat) | d (i : Nat) @[noinline] def foo (p : Payload) : OptionM T := (do let i ← get? p 1; pure (T.a i)) <|> (do let i ← get? p 2; pure (T.b i)) <|> (do let i ← get? p 3; pure (T.c i)) <|> (do let i ← get? p 4; let i ← get? p 5; pure (T.d i)) def wrongOutput : String := match foo (Payload.mk 1 20) with | some (t : T) => match t with | T.a i => "1: " ++ toString i | T.b i => "2: " ++ toString i | T.c i => "3: " ++ toString i | T.d i => "4: " ++ toString i | none => "5" #eval wrongOutput def main (xs : List String) : IO Unit := IO.println wrongOutput
0691b60758cab7e1f65432a4cab8128c4407d845
a4673261e60b025e2c8c825dfa4ab9108246c32e
/src/Lean/Meta/Match/CaseValues.lean
83aaa377045ff9fe87132ab922c00990ebda6bb1
[ "Apache-2.0" ]
permissive
jcommelin/lean4
c02dec0cc32c4bccab009285475f265f17d73228
2909313475588cc20ac0436e55548a4502050d0a
refs/heads/master
1,674,129,550,893
1,606,415,348,000
1,606,415,348,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,516
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.Tactic.Subst import Lean.Meta.Tactic.Clear namespace Lean.Meta structure CaseValueSubgoal := (mvarId : MVarId) (newH : FVarId) (subst : FVarSubst := {}) instance : Inhabited CaseValueSubgoal := ⟨{ mvarId := arbitrary, newH := arbitrary }⟩ /-- Split goal `... |- C x` into two subgoals `..., (h : x = value) |- C value` `..., (h : x != value) |- C x` where `fvarId` is `x`s id. The type of `x` must have decidable equality. Remark: `subst` field of the second subgoal is equal to the input `subst`. -/ def caseValueAux (mvarId : MVarId) (fvarId : FVarId) (value : Expr) (hName : Name := `h) (subst : FVarSubst := {}) : MetaM (CaseValueSubgoal Γ— CaseValueSubgoal) := withMVarContext mvarId $ do let tag ← getMVarTag mvarId checkNotAssigned mvarId `caseValue let target ← getMVarType mvarId let xEqValue ← mkEq (mkFVar fvarId) value let xNeqValue := mkApp (mkConst `Not) xEqValue let thenTarget := Lean.mkForall hName BinderInfo.default xEqValue target let elseTarget := Lean.mkForall hName BinderInfo.default xNeqValue target let thenMVar ← mkFreshExprSyntheticOpaqueMVar thenTarget tag let elseMVar ← mkFreshExprSyntheticOpaqueMVar elseTarget tag let val ← mkAppOptM `dite #[none, xEqValue, none, thenMVar, elseMVar] assignExprMVar mvarId val let (elseH, elseMVarId) ← intro1P elseMVar.mvarId! let elseSubgoal := { mvarId := elseMVarId, newH := elseH, subst := subst : CaseValueSubgoal } let (thenH, thenMVarId) ← intro1P thenMVar.mvarId! let symm := false let clearH := false let (thenSubst, thenMVarId) ← substCore thenMVarId thenH symm subst clearH withMVarContext thenMVarId do trace[Meta]! "subst domain: {thenSubst.domain}" let thenH := (thenSubst.get thenH).fvarId! trace[Meta]! "searching for decl" let decl ← getLocalDecl thenH trace[Meta]! "found decl" let thenSubgoal := { mvarId := thenMVarId, newH := (thenSubst.get thenH).fvarId!, subst := thenSubst : CaseValueSubgoal } pure (thenSubgoal, elseSubgoal) def caseValue (mvarId : MVarId) (fvarId : FVarId) (value : Expr) : MetaM (CaseValueSubgoal Γ— CaseValueSubgoal) := do let s ← caseValueAux mvarId fvarId value appendTagSuffix s.1.mvarId `thenBranch appendTagSuffix s.2.mvarId `elseBranch pure s structure CaseValuesSubgoal := (mvarId : MVarId) (newHs : Array FVarId := #[]) (subst : FVarSubst := {}) instance : Inhabited CaseValuesSubgoal := ⟨{ mvarId := arbitrary }⟩ /-- Split goal `... |- C x` into values.size + 1 subgoals 1) `..., (h_1 : x = value[0]) |- C value[0]` ... n) `..., (h_n : x = value[n - 1]) |- C value[n - 1]` n+1) `..., (h_1 : x != value[0]) ... (h_n : x != value[n-1]) |- C x` where `n = values.size` where `fvarId` is `x`s id. The type of `x` must have decidable equality. Remark: the last subgoal is for the "else" catchall case, and its `subst` is `{}`. Remark: the fiels `newHs` has size 1 forall but the last subgoal. -/ def caseValues (mvarId : MVarId) (fvarId : FVarId) (values : Array Expr) (hNamePrefix := `h) : MetaM (Array CaseValuesSubgoal) := let rec loop : Nat β†’ MVarId β†’ List Expr β†’ Array FVarId β†’ Array CaseValuesSubgoal β†’ MetaM (Array CaseValuesSubgoal) | i, mvarId, [], hs, subgoals => throwTacticEx `caseValues mvarId "list of values must not be empty" | i, mvarId, v::vs, hs, subgoals => do let (thenSubgoal, elseSubgoal) ← caseValueAux mvarId fvarId v (hNamePrefix.appendIndexAfter i) {} appendTagSuffix thenSubgoal.mvarId ((`case).appendIndexAfter i) let thenMVarId ← hs.foldlM (fun thenMVarId h => match thenSubgoal.subst.get h with | Expr.fvar fvarId _ => tryClear thenMVarId fvarId | _ => pure thenMVarId) thenSubgoal.mvarId let subgoals := subgoals.push { mvarId := thenMVarId, newHs := #[thenSubgoal.newH], subst := thenSubgoal.subst } match vs with | [] => do appendTagSuffix elseSubgoal.mvarId ((`case).appendIndexAfter (i+1)) pure $ subgoals.push { mvarId := elseSubgoal.mvarId, newHs := hs.push elseSubgoal.newH, subst := {} } | vs => loop (i+1) elseSubgoal.mvarId vs (hs.push elseSubgoal.newH) subgoals loop 1 mvarId values.toList #[] #[] end Lean.Meta
a54072889060ed33ab8663573443ad2716c91fe6
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/data/finsupp/lattice.lean
6eaa6b452dfe645c923150b22e5574f2afd8b378
[ "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,975
lean
/- Copyright (c) 2020 Aaron Anderson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Aaron Anderson -/ import data.finsupp.basic /-! # Lattice structure on finsupps This file provides instances of ordered structures on finsupps. -/ open_locale classical noncomputable theory variables {Ξ± : Type*} {Ξ² : Type*} [has_zero Ξ²] {ΞΌ : Type*} [canonically_ordered_add_monoid ΞΌ] variables {Ξ³ : Type*} [canonically_linear_ordered_add_monoid Ξ³] namespace finsupp instance : order_bot (Ξ± β†’β‚€ ΞΌ) := { bot := 0, bot_le := by simp [finsupp.le_def, ← bot_eq_zero] } instance [semilattice_inf Ξ²] : semilattice_inf (Ξ± β†’β‚€ Ξ²) := { inf := zip_with (βŠ“) inf_idem, inf_le_left := Ξ» a b c, inf_le_left, inf_le_right := Ξ» a b c, inf_le_right, le_inf := Ξ» a b c h1 h2 s, le_inf (h1 s) (h2 s), ..finsupp.partial_order, } @[simp] lemma inf_apply [semilattice_inf Ξ²] {a : Ξ±} {f g : Ξ± β†’β‚€ Ξ²} : (f βŠ“ g) a = f a βŠ“ g a := rfl @[simp] lemma support_inf {f g : Ξ± β†’β‚€ Ξ³} : (f βŠ“ g).support = f.support ∩ g.support := begin ext, simp only [inf_apply, mem_support_iff, ne.def, finset.mem_union, finset.mem_filter, finset.mem_inter], simp only [inf_eq_min, ← nonpos_iff_eq_zero, min_le_iff, not_or_distrib] end instance [semilattice_sup Ξ²] : semilattice_sup (Ξ± β†’β‚€ Ξ²) := { sup := zip_with (βŠ”) sup_idem, le_sup_left := Ξ» a b c, le_sup_left, le_sup_right := Ξ» a b c, le_sup_right, sup_le := Ξ» a b c h1 h2 s, sup_le (h1 s) (h2 s), ..finsupp.partial_order, } @[simp] lemma sup_apply [semilattice_sup Ξ²] {a : Ξ±} {f g : Ξ± β†’β‚€ Ξ²} : (f βŠ” g) a = f a βŠ” g a := rfl @[simp] lemma support_sup {f g : Ξ± β†’β‚€ Ξ³} : (f βŠ” g).support = f.support βˆͺ g.support := begin ext, simp only [finset.mem_union, mem_support_iff, sup_apply, ne.def, ← bot_eq_zero], rw sup_eq_bot_iff, tauto, end instance lattice [lattice Ξ²] : lattice (Ξ± β†’β‚€ Ξ²) := { .. finsupp.semilattice_inf, .. finsupp.semilattice_sup} instance semilattice_inf_bot : semilattice_inf_bot (Ξ± β†’β‚€ Ξ³) := { ..finsupp.order_bot, ..finsupp.lattice} lemma bot_eq_zero : (βŠ₯ : Ξ± β†’β‚€ Ξ³) = 0 := rfl lemma disjoint_iff {x y : Ξ± β†’β‚€ Ξ³} : disjoint x y ↔ disjoint x.support y.support := begin unfold disjoint, repeat {rw le_bot_iff}, rw [finsupp.bot_eq_zero, ← finsupp.support_eq_empty, finsupp.support_inf], refl, end variable [partial_order Ξ²] /-- The order on `finsupp`s over a partial order embeds into the order on functions -/ def order_embedding_to_fun : (Ξ± β†’β‚€ Ξ²) β†ͺo (Ξ± β†’ Ξ²) := ⟨⟨λ (f : Ξ± β†’β‚€ Ξ²) (a : Ξ±), f a, Ξ» f g h, finsupp.ext (Ξ» a, by { dsimp at h, rw h,} )⟩, Ξ» a b, (@le_def _ _ _ _ a b).symm⟩ @[simp] lemma order_embedding_to_fun_apply {f : Ξ± β†’β‚€ Ξ²} {a : Ξ±} : order_embedding_to_fun f a = f a := rfl lemma monotone_to_fun : monotone (finsupp.to_fun : (Ξ± β†’β‚€ Ξ²) β†’ (Ξ± β†’ Ξ²)) := Ξ» f g h a, le_def.1 h a end finsupp
6e3bb429a2c16cf11e762ab0ee5ed70ba07ed745
205f0fc16279a69ea36e9fd158e3a97b06834ce2
/src/10b_Relations/00_intro.lean
069a26d9a8c6ff6191ad9df8468fa04b7a4a09db
[]
no_license
kevinsullivan/cs-dm-lean
b21d3ca1a9b2a0751ba13fcb4e7b258010a5d124
a06a94e98be77170ca1df486c8189338b16cf6c6
refs/heads/master
1,585,948,743,595
1,544,339,346,000
1,544,339,346,000
155,570,767
1
3
null
1,541,540,372,000
1,540,995,993,000
Lean
UTF-8
Lean
false
false
3,541
lean
/- As discussed in the last unit, a predicate is a proposition with one more more arguments. In the last unit, we studied predicates with one argument. We saw that such a predicate can be seen as specifying a *property* of values of its argument type. Examples included the properties of being even, being equal to 0, being a perfect square, or of (a person) being from Cville. We now look at predicates with multiple arguments. We emphasize the idea that such predicates can be used to specify properties of *tuples* (e.g., pairs) of values. We refer to properties involving tuples as relations. As an example we can consider equality as a relation between two values. A relation involving two values is said to be a binary relation. We could represent the "equals" relation as a predicate with two arguments, n and m, and rules that provide proofs of the proposition "equals m n" if and only if m = n. We can represent this predicate as a function that now takes two arguments and returns Prop. -/ def areEqual : β„• β†’ β„• β†’ Prop := Ξ» n m, n = m #check areEqual 0 0 #reduce areEqual 0 0 lemma zeqz : areEqual 0 0 := eq.refl 0 #check areEqual 2 3 #reduce areEqual 2 3 /- Be sure you're comfortable with these ideas. We define areEqual as a function that takes n and m as arguments (of type β„•) and that then returns the *proposition*, n = m. Be clear that this function does NOT return a proof. It returns a proposition that might or might not be true! The single predicate, which most mathematicians would write as areEqual(n,m), with two arguments, defines an infinity of propositions, each one about one specific pair of natural numbers. Some of these propositions have proofs and thus are true. Others do not, and are not true. Such a predicate thus "picks out" a subset of all possible pairs, namely those for which its proposition is true. These pairs include (0, 0) but not (2, 3). Such a set of pairs is what we think of as the relation that the predicate specifies. In particular, areEqual specifies a "binary" relation, the set of all pairs of natural numbers that are equal to themselves. Let's prove that a few elements are in, and that another one is not in, the areEqual(n,m) relation. -/ -- (1, 1) is in the relation example : areEqual 1 1 := eq.refl 1 -- (2, 2) is in the relation example : areEqual 2 2 := eq.refl 2 -- but (0, 1) is not in it example : Β¬(areEqual 0 1) := Ξ» zeqo, nat.no_confusion zeqo /- EXERCISE [Worked]: Define a predicate isSquare that expresses the relation between two natural numbers that the second natural number is the square of the first and prove that the pair, (3, 9), is in the isSquare, relation. -/ -- Answer def isSquare: β„• β†’ β„• β†’ Prop := Ξ» n m, n * n = m example : isSquare 3 9 := begin unfold isSquare, exact rfl, end /- EXERCISE: In lean, the function, string.length returns the length of a given string. Specify a new predicate sHasLenN taking a string and a natural number as arguments that asserts that a given string has the given length. Write the function using lambda notation to make the type of the predicate as clear as possible. -/ #eval string.length "Hello" -- answer here def sHasLenN (s :string) (n : β„•) : Prop := s.length = n example : sHasLenN "Hello" 5 := begin unfold sHasLenN, exact rfl, end def pythagoreanTriple : β„• β†’ β„• β†’ β„• β†’ Prop := Ξ» a b h, a^2 + b^2 = h^2 example : pythagoreanTriple 3 4 5 := begin unfold pythagoreanTriple, exact rfl, end
949ceae849fe88e99b56c1f27f3950b497396569
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/hott/algebra/category/functor/examples.hlean
28e698c7edd136aa65a182a777591f1bb571dfa5
[ "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
9,854
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 Definition of functors involving at least two different constructions of categories -/ import ..constructions.functor ..constructions.product ..constructions.opposite ..constructions.set open category nat_trans eq prod prod.ops namespace functor section open iso equiv variables {C D E : Precategory} (F F' : C Γ—c D β‡’ E) (G G' : C β‡’ E ^c D) /- currying a functor -/ definition functor_curry_ob [reducible] [constructor] (c : C) : D β‡’ E := F ∘f (constant_functor D c Γ—f 1) definition functor_curry_hom [constructor] ⦃c c' : C⦄ (f : c ⟢ c') : functor_curry_ob F c ⟹ functor_curry_ob F c' := F ∘fn (constant_nat_trans D f Γ—n 1) local abbreviation Fhom [constructor] := @functor_curry_hom theorem functor_curry_id (c : C) : Fhom F (ID c) = 1 := nat_trans_eq (Ξ»d, respect_id F (c, d)) theorem functor_curry_comp ⦃c c' c'' : C⦄ (f' : c' ⟢ c'') (f : c ⟢ c') : Fhom F (f' ∘ f) = Fhom F f' ∘n Fhom F f := begin apply nat_trans_eq, intro d, calc natural_map (Fhom F (f' ∘ f)) d = F (f' ∘ f, id) : by esimp ... = F (f' ∘ f, category.id ∘ category.id) : by rewrite id_id ... = F ((f',id) ∘ (f, id)) : by esimp ... = F (f',id) ∘ F (f, id) : by rewrite [respect_comp F] ... = natural_map ((Fhom F f') ∘ (Fhom F f)) d : by esimp end definition functor_curry [constructor] : C β‡’ E ^c D := functor.mk (functor_curry_ob F) (functor_curry_hom F) (functor_curry_id F) (functor_curry_comp F) /- currying a functor, flipping the arguments -/ definition functor_curry_rev_ob [reducible] [constructor] (d : D) : C β‡’ E := F ∘f (1 Γ—f constant_functor C d) definition functor_curry_rev_hom [constructor] ⦃d d' : D⦄ (g : d ⟢ d') : functor_curry_rev_ob F d ⟹ functor_curry_rev_ob F d' := F ∘fn (1 Γ—n constant_nat_trans C g) local abbreviation Fhomr [constructor] := @functor_curry_rev_hom theorem functor_curry_rev_id (d : D) : Fhomr F (ID d) = nat_trans.id := nat_trans_eq (Ξ»c, respect_id F (c, d)) theorem functor_curry_rev_comp ⦃d d' d'' : D⦄ (g' : d' ⟢ d'') (g : d ⟢ d') : Fhomr F (g' ∘ g) = Fhomr F g' ∘n Fhomr F g := begin apply nat_trans_eq, esimp, intro c, rewrite [-id_id at {1}], apply respect_comp F end definition functor_curry_rev [constructor] : D β‡’ E ^c C := functor.mk (functor_curry_rev_ob F) (functor_curry_rev_hom F) (functor_curry_rev_id F) (functor_curry_rev_comp F) /- uncurrying a functor -/ definition functor_uncurry_ob [reducible] (p : C Γ—c D) : E := to_fun_ob (G p.1) p.2 definition functor_uncurry_hom ⦃p p' : C Γ—c D⦄ (f : hom p p') : functor_uncurry_ob G p ⟢ functor_uncurry_ob G p' := to_fun_hom (to_fun_ob G p'.1) f.2 ∘ natural_map (to_fun_hom G f.1) p.2 local abbreviation Ghom := @functor_uncurry_hom theorem functor_uncurry_id (p : C Γ—c D) : Ghom G (ID p) = id := calc Ghom G (ID p) = to_fun_hom (to_fun_ob G p.1) id ∘ natural_map (to_fun_hom G id) p.2 : by esimp ... = id ∘ natural_map (to_fun_hom G id) p.2 : by rewrite respect_id ... = id ∘ natural_map nat_trans.id p.2 : by rewrite respect_id ... = id : id_id theorem functor_uncurry_comp ⦃p p' p'' : C Γ—c D⦄ (f' : p' ⟢ p'') (f : p ⟢ p') : Ghom G (f' ∘ f) = Ghom G f' ∘ Ghom G f := calc Ghom G (f' ∘ f) = to_fun_hom (to_fun_ob G p''.1) (f'.2 ∘ f.2) ∘ natural_map (to_fun_hom G (f'.1 ∘ f.1)) p.2 : by esimp ... = (to_fun_hom (to_fun_ob G p''.1) f'.2 ∘ to_fun_hom (to_fun_ob G p''.1) f.2) ∘ natural_map (to_fun_hom G (f'.1 ∘ f.1)) p.2 : by rewrite respect_comp ... = (to_fun_hom (to_fun_ob G p''.1) f'.2 ∘ to_fun_hom (to_fun_ob G p''.1) f.2) ∘ natural_map (to_fun_hom G f'.1 ∘ to_fun_hom G f.1) p.2 : by rewrite respect_comp ... = (to_fun_hom (to_fun_ob G p''.1) f'.2 ∘ to_fun_hom (to_fun_ob G p''.1) f.2) ∘ (natural_map (to_fun_hom G f'.1) p.2 ∘ natural_map (to_fun_hom G f.1) p.2) : by esimp ... = (to_fun_hom (to_fun_ob G p''.1) f'.2 ∘ natural_map (to_fun_hom G f'.1) p'.2) ∘ (to_fun_hom (to_fun_ob G p'.1) f.2 ∘ natural_map (to_fun_hom G f.1) p.2) : by rewrite [square_prepostcompose (!naturality⁻¹ᡖ) _ _] ... = Ghom G f' ∘ Ghom G f : by esimp definition functor_uncurry [constructor] : C Γ—c D β‡’ E := functor.mk (functor_uncurry_ob G) (functor_uncurry_hom G) (functor_uncurry_id G) (functor_uncurry_comp G) definition functor_uncurry_functor_curry : functor_uncurry (functor_curry F) = F := functor_eq (Ξ»p, ap (to_fun_ob F) !prod.eta) begin intro cd cd' fg, cases cd with c d, cases cd' with c' d', cases fg with f g, transitivity to_fun_hom (functor_uncurry (functor_curry F)) (f, g), apply id_leftright, show (functor_uncurry (functor_curry F)) (f, g) = F (f,g), from calc (functor_uncurry (functor_curry F)) (f, g) = to_fun_hom F (id, g) ∘ to_fun_hom F (f, id) : by esimp ... = F (category.id ∘ f, g ∘ category.id) : (respect_comp F (id,g) (f,id))⁻¹ ... = F (f, g ∘ category.id) : by rewrite id_left ... = F (f,g) : by rewrite id_right, end definition functor_curry_functor_uncurry_ob (c : C) : functor_curry (functor_uncurry G) c = G c := begin fapply functor_eq, { intro d, reflexivity}, { intro d d' g, refine !id_leftright ⬝ _, esimp, rewrite [β–Έ*, ↑functor_uncurry_hom, respect_id, β–Έ*, id_right]} end definition functor_curry_functor_uncurry : functor_curry (functor_uncurry G) = G := begin fapply functor_eq, exact (functor_curry_functor_uncurry_ob G), intro c c' f, fapply nat_trans_eq, intro d, apply concat, {apply (ap (Ξ»x, x ∘ _)), apply concat, apply natural_map_hom_of_eq, apply (ap hom_of_eq), apply ap010_functor_eq}, apply concat, {apply (ap (Ξ»x, _ ∘ x)), apply (ap (Ξ»x, _ ∘ x)), apply concat, apply natural_map_inv_of_eq, apply (ap (Ξ»x, hom_of_eq x⁻¹)), apply ap010_functor_eq}, apply concat, apply id_leftright, apply concat, apply (ap (Ξ»x, x ∘ _)), apply respect_id, apply id_left end /- This only states that the carriers of (C ^ D) ^ E and C ^ (E Γ— D) are equivalent. In [exponential laws] we prove that these are in fact isomorphic categories -/ definition prod_functor_equiv_functor_functor [constructor] (C D E : Precategory) : (C Γ—c D β‡’ E) ≃ (C β‡’ E ^c D) := equiv.MK functor_curry functor_uncurry functor_curry_functor_uncurry functor_uncurry_functor_curry variables {F F' G G'} definition nat_trans_curry_nat [constructor] (Ξ· : F ⟹ F') (c : C) : functor_curry_ob F c ⟹ functor_curry_ob F' c := begin fapply nat_trans.mk: esimp, { intro d, exact Ξ· (c, d)}, { intro d d' f, apply naturality} end definition nat_trans_curry [constructor] (Ξ· : F ⟹ F') : functor_curry F ⟹ functor_curry F' := begin fapply nat_trans.mk: esimp, { exact nat_trans_curry_nat Ξ·}, { intro c c' f, apply nat_trans_eq, intro d, esimp, apply naturality} end definition nat_trans_uncurry [constructor] (Ξ· : G ⟹ G') : functor_uncurry G ⟹ functor_uncurry G' := begin fapply nat_trans.mk: esimp, { intro v, unfold functor_uncurry_ob, exact (Ξ· v.1) v.2}, { intro v w f, unfold functor_uncurry_hom, rewrite [-assoc, ap010 natural_map (naturality Ξ· f.1) v.2, assoc, naturality, -assoc]} end end section open is_trunc /- hom-functors -/ definition hom_functor_assoc {C : Precategory} {a1 a2 a3 a4 a5 a6 : C} (f1 : hom a5 a6) (f2 : hom a4 a5) (f3 : hom a3 a4) (f4 : hom a2 a3) (f5 : hom a1 a2) : (f1 ∘ f2) ∘ f3 ∘ (f4 ∘ f5) = f1 ∘ (f2 ∘ f3 ∘ f4) ∘ f5 := calc _ = f1 ∘ f2 ∘ f3 ∘ f4 ∘ f5 : by rewrite -assoc ... = f1 ∘ (f2 ∘ f3) ∘ f4 ∘ f5 : by rewrite -assoc ... = f1 ∘ ((f2 ∘ f3) ∘ f4) ∘ f5 : by rewrite -(assoc (f2 ∘ f3) _ _) ... = _ : by rewrite (assoc f2 f3 f4) -- the functor hom(-,-) definition hom_functor.{u v} [constructor] (C : Precategory.{u v}) : Cα΅’α΅– Γ—c C β‡’ set.{v} := functor.mk (Ξ» (x : Cα΅’α΅– Γ—c C), @homset (Cα΅’α΅–) C x.1 x.2) (Ξ» (x y : Cα΅’α΅– Γ—c C) (f : @category.precategory.hom (Cα΅’α΅– Γ—c C) (Cα΅’α΅– Γ—c C) x y) (h : @homset (Cα΅’α΅–) C x.1 x.2), f.2 ∘[C] (h ∘[C] f.1)) (Ξ» x, abstract @eq_of_homotopy _ _ _ (ID (@homset Cα΅’α΅– C x.1 x.2)) (Ξ» h, concat (by apply @id_left) (by apply @id_right)) end) (Ξ» x y z g f, abstract eq_of_homotopy (by intros; apply @hom_functor_assoc) end) -- the functor hom(-, c) definition hom_functor_left.{u v} [constructor] {C : Precategory.{u v}} (c : C) : Cα΅’α΅– β‡’ set.{v} := functor_curry_rev_ob !hom_functor c -- the functor hom(c, -) definition hom_functor_right.{u v} [constructor] {C : Precategory.{u v}} (c : C) : C β‡’ set.{v} := functor_curry_ob !hom_functor c definition nat_trans_hom_functor_left [constructor] {C : Precategory} ⦃c c' : C⦄ (f : c ⟢ c') : hom_functor_left c ⟹ hom_functor_left c' := functor_curry_rev_hom !hom_functor f -- the yoneda embedding itself is defined in [yoneda]. end end functor
737c42620346f6d237720d07eeb4aaa3a42821f8
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/testing/slim_check/sampleable.lean
ac9b0ac097fe20bf7ba0b1ab989af832c299f5e0
[ "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
31,447
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 data.lazy_list.basic import data.tree import data.pnat.basic import control.bifunctor import control.ulift import testing.slim_check.gen import tactic.linarith /-! # `sampleable` Class > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This class permits the creation samples of a given type controlling the size of those values using the `gen` monad`. It also helps minimize examples by creating smaller versions of given values. When testing a proposition like `βˆ€ n : β„•, prime n β†’ n ≀ 100`, `slim_check` requires that `β„•` have an instance of `sampleable` and for `prime n` to be decidable. `slim_check` will then use the instance of `sampleable` to generate small examples of β„• and progressively increase in size. For each example `n`, `prime n` is tested. If it is false, the example will be rejected (not a test success nor a failure) and `slim_check` will move on to other examples. If `prime n` is true, `n ≀ 100` will be tested. If it is false, `n` is a counter-example of `βˆ€ n : β„•, prime n β†’ n ≀ 100` and the test fails. If `n ≀ 100` is true, the test passes and `slim_check` moves on to trying more examples. This is a port of the Haskell QuickCheck library. ## Main definitions * `sampleable` class * `sampleable_functor` and `sampleable_bifunctor` class * `sampleable_ext` class ### `sampleable` `sampleable Ξ±` provides ways of creating examples of type `Ξ±`, and given such an example `x : Ξ±`, gives us a way to shrink it and find simpler examples. ### `sampleable_ext` `sampleable_ext` generalizes the behavior of `sampleable` and makes it possible to express instances for types that do not lend themselves to introspection, such as `β„• β†’ β„•`. If we test a quantification over functions the counter-examples cannot be shrunken or printed meaningfully. For that purpose, `sampleable_ext` provides a proxy representation `proxy_repr` that can be printed and shrunken as well as interpreted (using `interp`) as an object of the right type. ### `sampleable_functor` and `sampleable_bifunctor` `sampleable_functor F` and `sampleable_bifunctor F` makes it possible to create samples of and shrink `F Ξ±` given a sampling function and a shrinking function for arbitrary `Ξ±`. This allows us to separate the logic for generating the shape of a collection from the logic for generating its contents. Specifically, the contents could be generated using either `sampleable` or `sampleable_ext` instance and the `sampleable_(bi)functor` does not need to use that information ## Shrinking Shrinking happens when `slim_check` find a counter-example to a property. It is likely that the example will be more complicated than necessary so `slim_check` proceeds to shrink it as much as possible. Although equally valid, a smaller counter-example is easier for a user to understand and use. The `sampleable` class, beside having the `sample` function, has a `shrink` function so that we can use specialized knowledge while shrinking a value. It is not responsible for the whole shrinking process however. It only has to take one step in the shrinking process. `slim_check` will repeatedly call `shrink` until no more steps can be taken. Because `shrink` guarantees that the size of the candidates it produces is strictly smaller than the argument, we know that `slim_check` is guaranteed to terminate. ## Tags random testing ## References * https://hackage.haskell.org/package/QuickCheck -/ universes u v w namespace slim_check variables (Ξ± : Type u) local infix ` β‰Ί `:50 := has_well_founded.r /-- `sizeof_lt x y` compares the sizes of `x` and `y`. -/ def sizeof_lt {Ξ±} [has_sizeof Ξ±] (x y : Ξ±) := sizeof x < sizeof y /-- `shrink_fn Ξ±` is the type of functions that shrink an argument of type `Ξ±` -/ @[reducible] def shrink_fn (Ξ± : Type*) [has_sizeof Ξ±] := Ξ  x : Ξ±, lazy_list { y : Ξ± // sizeof_lt y x } /-- `sampleable Ξ±` provides ways of creating examples of type `Ξ±`, and given such an example `x : Ξ±`, gives us a way to shrink it and find simpler examples. -/ class sampleable := [wf : has_sizeof Ξ±] (sample [] : gen Ξ±) (shrink : Ξ  x : Ξ±, lazy_list { y : Ξ± // @sizeof _ wf y < @sizeof _ wf x } := Ξ» _, lazy_list.nil) attribute [instance, priority 100] has_well_founded_of_has_sizeof default_has_sizeof attribute [instance, priority 200] sampleable.wf /-- `sampleable_functor F` makes it possible to create samples of and shrink `F Ξ±` given a sampling function and a shrinking function for arbitrary `Ξ±` -/ class sampleable_functor (F : Type u β†’ Type v) [functor F] := [wf : Ξ  Ξ± [has_sizeof Ξ±], has_sizeof (F Ξ±)] (sample [] : βˆ€ {Ξ±}, gen Ξ± β†’ gen (F Ξ±)) (shrink : βˆ€ Ξ± [has_sizeof Ξ±], shrink_fn Ξ± β†’ shrink_fn (F Ξ±)) (p_repr : βˆ€ Ξ±, has_repr Ξ± β†’ has_repr (F Ξ±)) /-- `sampleable_bifunctor F` makes it possible to create samples of and shrink `F Ξ± Ξ²` given a sampling function and a shrinking function for arbitrary `Ξ±` and `Ξ²` -/ class sampleable_bifunctor (F : Type u β†’ Type v β†’ Type w) [bifunctor F] := [wf : Ξ  Ξ± Ξ² [has_sizeof Ξ±] [has_sizeof Ξ²], has_sizeof (F Ξ± Ξ²)] (sample [] : βˆ€ {Ξ± Ξ²}, gen Ξ± β†’ gen Ξ² β†’ gen (F Ξ± Ξ²)) (shrink : βˆ€ Ξ± Ξ² [has_sizeof Ξ±] [has_sizeof Ξ²], shrink_fn Ξ± β†’ shrink_fn Ξ² β†’ shrink_fn (F Ξ± Ξ²)) (p_repr : βˆ€ Ξ± Ξ², has_repr Ξ± β†’ has_repr Ξ² β†’ has_repr (F Ξ± Ξ²)) export sampleable (sample shrink) /-- This function helps infer the proxy representation and interpretation in `sampleable_ext` instances. -/ meta def sampleable.mk_trivial_interp : tactic unit := tactic.refine ``(id) /-- `sampleable_ext` generalizes the behavior of `sampleable` and makes it possible to express instances for types that do not lend themselves to introspection, such as `β„• β†’ β„•`. If we test a quantification over functions the counter-examples cannot be shrunken or printed meaningfully. For that purpose, `sampleable_ext` provides a proxy representation `proxy_repr` that can be printed and shrunken as well as interpreted (using `interp`) as an object of the right type. -/ class sampleable_ext (Ξ± : Sort u) := (proxy_repr : Type v) [wf : has_sizeof proxy_repr] (interp [] : proxy_repr β†’ Ξ± . sampleable.mk_trivial_interp) [p_repr : has_repr proxy_repr] (sample [] : gen proxy_repr) (shrink : shrink_fn proxy_repr) attribute [instance, priority 100] sampleable_ext.p_repr sampleable_ext.wf open nat lazy_list section prio open sampleable_ext set_option default_priority 50 instance sampleable_ext.of_sampleable {Ξ±} [sampleable Ξ±] [has_repr Ξ±] : sampleable_ext Ξ± := { proxy_repr := Ξ±, sample := sampleable.sample Ξ±, shrink := shrink } instance sampleable.functor {Ξ±} {F} [functor F] [sampleable_functor F] [sampleable Ξ±] : sampleable (F Ξ±) := { wf := _, sample := sampleable_functor.sample F (sampleable.sample Ξ±), shrink := sampleable_functor.shrink Ξ± sampleable.shrink } instance sampleable.bifunctor {Ξ± Ξ²} {F} [bifunctor F] [sampleable_bifunctor F] [sampleable Ξ±] [sampleable Ξ²] : sampleable (F Ξ± Ξ²) := { wf := _, sample := sampleable_bifunctor.sample F (sampleable.sample Ξ±) (sampleable.sample Ξ²), shrink := sampleable_bifunctor.shrink Ξ± Ξ² sampleable.shrink sampleable.shrink } set_option default_priority 100 instance sampleable_ext.functor {Ξ±} {F} [functor F] [sampleable_functor F] [sampleable_ext Ξ±] : sampleable_ext (F Ξ±) := { wf := _, proxy_repr := F (proxy_repr Ξ±), interp := functor.map (interp _), sample := sampleable_functor.sample F (sampleable_ext.sample Ξ±), shrink := sampleable_functor.shrink _ sampleable_ext.shrink, p_repr := sampleable_functor.p_repr _ sampleable_ext.p_repr } instance sampleable_ext.bifunctor {Ξ± Ξ²} {F} [bifunctor F] [sampleable_bifunctor F] [sampleable_ext Ξ±] [sampleable_ext Ξ²] : sampleable_ext (F Ξ± Ξ²) := { wf := _, proxy_repr := F (proxy_repr Ξ±) (proxy_repr Ξ²), interp := bifunctor.bimap (interp _) (interp _), sample := sampleable_bifunctor.sample F (sampleable_ext.sample Ξ±) (sampleable_ext.sample Ξ²), shrink := sampleable_bifunctor.shrink _ _ sampleable_ext.shrink sampleable_ext.shrink, p_repr := sampleable_bifunctor.p_repr _ _ sampleable_ext.p_repr sampleable_ext.p_repr } end prio /-- `nat.shrink' k n` creates a list of smaller natural numbers by successively dividing `n` by 2 and subtracting the difference from `k`. For example, `nat.shrink 100 = [50, 75, 88, 94, 97, 99]`. -/ def nat.shrink' (k : β„•) : Ξ  n : β„•, n ≀ k β†’ list { m : β„• // has_well_founded.r m k } β†’ list { m : β„• // has_well_founded.r m k } | n hn ls := if h : n ≀ 1 then ls.reverse else have hβ‚‚ : 0 < n, by linarith, have 1 * n / 2 < n, from nat.div_lt_of_lt_mul (nat.mul_lt_mul_of_pos_right (by norm_num) hβ‚‚), have n / 2 < n, by simpa, let m := n / 2 in have hβ‚€ : m ≀ k, from le_trans (le_of_lt this) hn, have h₃ : 0 < m, by simp only [m, lt_iff_add_one_le, zero_add]; rw [nat.le_div_iff_mul_le]; linarith, have h₁ : k - m < k, from nat.sub_lt (lt_of_lt_of_le hβ‚‚ hn) h₃, nat.shrink' m hβ‚€ (⟨k - m, hβ‚βŸ© :: ls) /-- `nat.shrink n` creates a list of smaller natural numbers by successively dividing by 2 and subtracting the difference from `n`. For example, `nat.shrink 100 = [50, 75, 88, 94, 97, 99]`. -/ def nat.shrink (n : β„•) : list { m : β„• // has_well_founded.r m n } := if h : n > 0 then have βˆ€ k, 1 < k β†’ n / k < n, from Ξ» k hk, nat.div_lt_of_lt_mul (suffices 1 * n < k * n, by simpa, nat.mul_lt_mul_of_pos_right hk h), ⟨n/11, this _ (by norm_num)⟩ :: ⟨n/3, this _ (by norm_num)⟩ :: nat.shrink' n n le_rfl [] else [] open gen /-- Transport a `sampleable` instance from a type `Ξ±` to a type `Ξ²` using functions between the two, going in both directions. Function `g` is used to define the well-founded order that `shrink` is expected to follow. -/ def sampleable.lift (Ξ± : Type u) {Ξ² : Type u} [sampleable Ξ±] (f : Ξ± β†’ Ξ²) (g : Ξ² β†’ Ξ±) (h : βˆ€ (a : Ξ±), sizeof (g (f a)) ≀ sizeof a) : sampleable Ξ² := { wf := ⟨ sizeof ∘ g ⟩, sample := f <$> sample Ξ±, shrink := Ξ» x, have βˆ€ a, sizeof a < sizeof (g x) β†’ sizeof (g (f a)) < sizeof (g x), by introv h'; solve_by_elim [lt_of_le_of_lt], subtype.map f this <$> shrink (g x) } instance nat.sampleable : sampleable β„• := { sample := sized $ Ξ» sz, freq [(1, coe <$> choose_any (fin $ succ (sz^3))), (3, coe <$> choose_any (fin $ succ sz))] dec_trivial, shrink := Ξ» x, lazy_list.of_list $ nat.shrink x } /-- `iterate_shrink p x` takes a decidable predicate `p` and a value `x` of some sampleable type and recursively shrinks `x`. It first calls `shrink x` to get a list of candidate sample, finds the first that satisfies `p` and recursively tries to shrink that one. -/ def iterate_shrink {Ξ±} [has_to_string Ξ±] [sampleable Ξ±] (p : Ξ± β†’ Prop) [decidable_pred p] : Ξ± β†’ option Ξ± := well_founded.fix has_well_founded.wf $ Ξ» x f_rec, do trace sformat!"{x} : {(shrink x).to_list}" $ pure (), y ← (shrink x).find (Ξ» a, p a), f_rec y y.property <|> some y.val . instance fin.sampleable {n : β„•} [ne_zero n] : sampleable (fin n) := sampleable.lift β„• fin.of_nat' fin.val $ Ξ» i, (mod_le _ _ : i % n ≀ i) @[priority 100] instance fin.sampleable' {n} : sampleable (fin (succ n)) := sampleable.lift β„• fin.of_nat fin.val $ Ξ» i, (mod_le _ _ : i % succ n ≀ i) instance pnat.sampleable : sampleable β„•+ := sampleable.lift β„• nat.succ_pnat pnat.nat_pred $ Ξ» a, by unfold_wf; simp only [pnat.nat_pred, succ_pnat, pnat.mk_coe, tsub_zero, succ_sub_succ_eq_sub] /-- Redefine `sizeof` for `int` to make it easier to use with `nat` -/ def int.has_sizeof : has_sizeof β„€ := ⟨ int.nat_abs ⟩ local attribute [instance, priority 2000] int.has_sizeof instance int.sampleable : sampleable β„€ := { wf := _, sample := sized $ Ξ» sz, freq [(1, subtype.val <$> choose (-(sz^3 + 1) : β„€) (sz^3 + 1) (neg_le_self dec_trivial)), (3, subtype.val <$> choose (-(sz + 1)) (sz + 1) (neg_le_self dec_trivial))] dec_trivial, shrink := Ξ» x, lazy_list.of_list $ (nat.shrink $ int.nat_abs x).bind $ Ξ» ⟨y,h⟩, [⟨y, h⟩, ⟨-y, by dsimp [sizeof,has_sizeof.sizeof]; rw int.nat_abs_neg; exact h ⟩] } instance bool.sampleable : sampleable bool := { wf := ⟨ Ξ» b, if b then 1 else 0 ⟩, sample := do { x ← choose_any bool, return x }, shrink := Ξ» b, if h : b then lazy_list.singleton ⟨ff, by cases h; unfold_wf⟩ else lazy_list.nil } /-- Provided two shrinking functions `prod.shrink` shrinks a pair `(x, y)` by first shrinking `x` and pairing the results with `y` and then shrinking `y` and pairing the results with `x`. All pairs either contain `x` untouched or `y` untouched. We rely on shrinking being repeated for `x` to get maximally shrunken and then for `y` to get shrunken too. -/ def prod.shrink {Ξ± Ξ²} [has_sizeof Ξ±] [has_sizeof Ξ²] (shr_a : shrink_fn Ξ±) (shr_b : shrink_fn Ξ²) : shrink_fn (Ξ± Γ— Ξ²) | ⟨xβ‚€,xβ‚βŸ© := let xsβ‚€ : lazy_list { y : Ξ± Γ— Ξ² // sizeof_lt y (xβ‚€,x₁) } := (shr_a xβ‚€).map $ subtype.map (Ξ» a, (a, x₁)) (Ξ» x h, by dsimp [sizeof_lt]; unfold_wf; apply h), xs₁ : lazy_list { y : Ξ± Γ— Ξ² // sizeof_lt y (xβ‚€,x₁) } := (shr_b x₁).map $ subtype.map (Ξ» a, (xβ‚€, a)) (Ξ» x h, by dsimp [sizeof_lt]; unfold_wf; apply h) in xsβ‚€.append xs₁ instance prod.sampleable : sampleable_bifunctor.{u v} prod := { wf := _, sample := Ξ» Ξ± Ξ² sama samb, do { ⟨x⟩ ← (uliftable.up $ sama : gen (ulift.{max u v} Ξ±)), ⟨y⟩ ← (uliftable.up $ samb : gen (ulift.{max u v} Ξ²)), pure (x,y) }, shrink := @prod.shrink, p_repr := @prod.has_repr } instance sigma.sampleable {Ξ± Ξ²} [sampleable Ξ±] [sampleable Ξ²] : sampleable (Ξ£ _ : Ξ±, Ξ²) := sampleable.lift (Ξ± Γ— Ξ²) (Ξ» ⟨x,y⟩, ⟨x,y⟩) (Ξ» ⟨x,y⟩, ⟨x,y⟩) $ Ξ» ⟨x,y⟩, le_rfl /-- shrinking function for sum types -/ def sum.shrink {Ξ± Ξ²} [has_sizeof Ξ±] [has_sizeof Ξ²] (shrink_Ξ± : shrink_fn Ξ±) (shrink_Ξ² : shrink_fn Ξ²) : shrink_fn (Ξ± βŠ• Ξ²) | (sum.inr x) := (shrink_Ξ² x).map $ subtype.map sum.inr $ Ξ» a, by dsimp [sizeof_lt]; unfold_wf; solve_by_elim | (sum.inl x) := (shrink_Ξ± x).map $ subtype.map sum.inl $ Ξ» a, by dsimp [sizeof_lt]; unfold_wf; solve_by_elim instance sum.sampleable : sampleable_bifunctor.{u v} sum := { wf := _, sample := Ξ» (Ξ± : Type u) (Ξ² : Type v) sam_Ξ± sam_Ξ², (@uliftable.up_map gen.{u} gen.{max u v} _ _ _ _ (@sum.inl Ξ± Ξ²) sam_Ξ± <|> @uliftable.up_map gen.{v} gen.{max v u} _ _ _ _ (@sum.inr Ξ± Ξ²) sam_Ξ²), shrink := Ξ» Ξ± Ξ² IΞ± IΞ² shr_Ξ± shr_Ξ², @sum.shrink _ _ IΞ± IΞ² shr_Ξ± shr_Ξ², p_repr := @sum.has_repr } instance rat.sampleable : sampleable β„š := sampleable.lift (β„€ Γ— β„•+) (Ξ» x, prod.cases_on x rat.mk_pnat) (Ξ» r, (r.num, ⟨r.denom, r.pos⟩)) $ begin intro i, rcases i with ⟨x,⟨y,hy⟩⟩; unfold_wf; dsimp [rat.mk_pnat], mono*, { rw [← int.coe_nat_le, int.coe_nat_abs, int.coe_nat_abs], apply int.abs_div_le_abs }, { change _ - 1 ≀ y-1, apply tsub_le_tsub_right, apply nat.div_le_of_le_mul, suffices : 1 * y ≀ x.nat_abs.gcd y * y, { simpa }, apply nat.mul_le_mul_right, apply gcd_pos_of_pos_right _ hy } end /-- `sampleable_char` can be specialized into customized `sampleable char` instances. The resulting instance has `1 / length` chances of making an unrestricted choice of characters and it otherwise chooses a character from `characters` with uniform probabilities. -/ def sampleable_char (length : nat) (characters : string) : sampleable char := { sample := do { x ← choose_nat 0 length dec_trivial, if x.val = 0 then do n ← sample β„•, pure $ char.of_nat n else do i ← choose_nat 0 (characters.length - 1) dec_trivial, pure (characters.mk_iterator.nextn i).curr }, shrink := Ξ» _, lazy_list.nil } instance char.sampleable : sampleable char := sampleable_char 3 " 0123abcABC:,;`\\/" variables {Ξ±} section list_shrink variables [has_sizeof Ξ±] (shr : Ξ  x : Ξ±, lazy_list { y : Ξ± // sizeof_lt y x }) lemma list.sizeof_drop_lt_sizeof_of_lt_length {xs : list Ξ±} {k} (hk : 0 < k) (hk' : k < xs.length) : sizeof (list.drop k xs) < sizeof xs := begin induction xs with x xs generalizing k, { cases hk' }, cases k, { cases hk }, have : sizeof xs < sizeof (x :: xs), { unfold_wf }, cases k, { simp only [this, list.drop] }, { simp only [list.drop], transitivity, { solve_by_elim [xs_ih, lt_of_succ_lt_succ hk', zero_lt_succ] }, { assumption } } end lemma list.sizeof_cons_lt_right (a b : Ξ±) {xs : list Ξ±} (h : sizeof a < sizeof b) : sizeof (a :: xs) < sizeof (b :: xs) := by unfold_wf; assumption lemma list.sizeof_cons_lt_left (x : Ξ±) {xs xs' : list Ξ±} (h : sizeof xs < sizeof xs') : sizeof (x :: xs) < sizeof (x :: xs') := by unfold_wf; assumption lemma list.sizeof_append_lt_left {xs ys ys' : list Ξ±} (h : sizeof ys < sizeof ys') : sizeof (xs ++ ys) < sizeof (xs ++ ys') := begin induction xs, { apply h }, { unfold_wf, simp only [list.sizeof, add_lt_add_iff_left], exact xs_ih } end lemma list.one_le_sizeof (xs : list Ξ±) : 1 ≀ sizeof xs := by cases xs; unfold_wf; linarith /-- `list.shrink_removes` shrinks a list by removing chunks of size `k` in the middle of the list. -/ def list.shrink_removes (k : β„•) (hk : 0 < k) : Ξ  (xs : list Ξ±) n, n = xs.length β†’ lazy_list { ys : list Ξ± // sizeof_lt ys xs } | xs n hn := if hkn : k > n then lazy_list.nil else if hkn' : k = n then have 1 < xs.sizeof, by { subst_vars, cases xs, { contradiction }, unfold_wf, apply lt_of_lt_of_le, show 1 < 1 + has_sizeof.sizeof xs_hd + 1, { linarith }, { mono, apply list.one_le_sizeof, } }, lazy_list.singleton ⟨[], this ⟩ else have hβ‚‚ : k < xs.length, from hn β–Έ lt_of_le_of_ne (le_of_not_gt hkn) hkn', match list.split_at k xs, rfl : Ξ  ys, ys = list.split_at k xs β†’ _ with | ⟨xs₁,xsβ‚‚βŸ©, h := have hβ‚„ : xs₁ = xs.take k, by simp only [list.split_at_eq_take_drop, prod.mk.inj_iff] at h; tauto, have h₃ : xsβ‚‚ = xs.drop k, by simp only [list.split_at_eq_take_drop, prod.mk.inj_iff] at h; tauto, have sizeof xsβ‚‚ < sizeof xs, by rw h₃; solve_by_elim [list.sizeof_drop_lt_sizeof_of_lt_length], have h₁ : n - k = xsβ‚‚.length, by simp only [h₃, ←hn, list.length_drop], have hβ‚… : βˆ€ (a : list Ξ±), sizeof_lt a xsβ‚‚ β†’ sizeof_lt (xs₁ ++ a) xs, by intros a h; rw [← list.take_append_drop k xs, ← h₃, ← hβ‚„]; solve_by_elim [list.sizeof_append_lt_left], lazy_list.cons ⟨xsβ‚‚, this⟩ $ subtype.map ((++) xs₁) hβ‚… <$> list.shrink_removes xsβ‚‚ (n - k) h₁ end /-- `list.shrink_one xs` shrinks list `xs` by shrinking only one item in the list. -/ def list.shrink_one : shrink_fn (list Ξ±) | [] := lazy_list.nil | (x :: xs) := lazy_list.append (subtype.map (Ξ» x', x' :: xs) (Ξ» a, list.sizeof_cons_lt_right _ _) <$> shr x) (subtype.map ((::) x) (Ξ» _, list.sizeof_cons_lt_left _) <$> list.shrink_one xs) /-- `list.shrink_with shrink_f xs` shrinks `xs` by first considering `xs` with chunks removed in the middle (starting with chunks of size `xs.length` and halving down to `1`) and then shrinks only one element of the list. This strategy is taken directly from Haskell's QuickCheck -/ def list.shrink_with (xs : list Ξ±) : lazy_list { ys : list Ξ± // sizeof_lt ys xs } := let n := xs.length in lazy_list.append ((lazy_list.cons n $ (shrink n).reverse.map subtype.val).bind (Ξ» k, if hk : 0 < k then list.shrink_removes k hk xs n rfl else lazy_list.nil )) (list.shrink_one shr _) end list_shrink instance list.sampleable : sampleable_functor list.{u} := { wf := _, sample := Ξ» Ξ± sam_Ξ±, list_of sam_Ξ±, shrink := Ξ» Ξ± IΞ± shr_Ξ±, @list.shrink_with _ IΞ± shr_Ξ±, p_repr := @list.has_repr } instance Prop.sampleable_ext : sampleable_ext Prop := { proxy_repr := bool, interp := coe, sample := choose_any bool, shrink := Ξ» _, lazy_list.nil } /-- `no_shrink` is a type annotation to signal that a certain type is not to be shrunk. It can be useful in combination with other types: e.g. `xs : list (no_shrink β„€)` will result in the list being cut down but individual integers being kept as is. -/ def no_shrink (Ξ± : Type*) := Ξ± instance no_shrink.inhabited {Ξ±} [inhabited Ξ±] : inhabited (no_shrink Ξ±) := ⟨ (default : Ξ±) ⟩ /-- Introduction of the `no_shrink` type. -/ def no_shrink.mk {Ξ±} (x : Ξ±) : no_shrink Ξ± := x /-- Selector of the `no_shrink` type. -/ def no_shrink.get {Ξ±} (x : no_shrink Ξ±) : Ξ± := x instance no_shrink.sampleable {Ξ±} [sampleable Ξ±] : sampleable (no_shrink Ξ±) := { sample := no_shrink.mk <$> sample Ξ± } instance string.sampleable : sampleable string := { sample := do { x ← list_of (sample char), pure x.as_string }, .. sampleable.lift (list char) list.as_string string.to_list $ Ξ» _, le_rfl } /-- implementation of `sampleable (tree Ξ±)` -/ def tree.sample (sample : gen Ξ±) : β„• β†’ gen (tree Ξ±) | n := if h : n > 0 then have n / 2 < n, from div_lt_self h (by norm_num), tree.node <$> sample <*> tree.sample (n / 2) <*> tree.sample (n / 2) else pure tree.nil /-- `rec_shrink x f_rec` takes the recursive call `f_rec` introduced by `well_founded.fix` and turns it into a shrinking function whose result is adequate to use in a recursive call. -/ def rec_shrink {Ξ± : Type*} [has_sizeof Ξ±] (t : Ξ±) (sh : Ξ  x : Ξ±, sizeof_lt x t β†’ lazy_list { y : Ξ± // sizeof_lt y x }) : shrink_fn { t' : Ξ± // sizeof_lt t' t } | ⟨t',ht'⟩ := (Ξ» t'' : { y : Ξ± // sizeof_lt y t' }, ⟨⟨t''.val, lt_trans t''.property ht'⟩, t''.property⟩ ) <$> sh t' ht' lemma tree.one_le_sizeof {Ξ±} [has_sizeof Ξ±] (t : tree Ξ±) : 1 ≀ sizeof t := by cases t; unfold_wf; linarith instance : functor tree := { map := @tree.map } /-- Recursion principle for shrinking tree-like structures. -/ def rec_shrink_with [has_sizeof Ξ±] (shrink_a : Ξ  x : Ξ±, shrink_fn { y : Ξ± // sizeof_lt y x } β†’ list (lazy_list { y : Ξ± // sizeof_lt y x })) : shrink_fn Ξ± := well_founded.fix (sizeof_measure_wf _) $ Ξ» t f_rec, lazy_list.join (lazy_list.of_list $ shrink_a t $ Ξ» ⟨t', h⟩, rec_shrink _ f_rec _) lemma rec_shrink_with_eq [has_sizeof Ξ±] (shrink_a : Ξ  x : Ξ±, shrink_fn { y : Ξ± // sizeof_lt y x } β†’ list (lazy_list { y : Ξ± // sizeof_lt y x })) (x : Ξ±) : rec_shrink_with shrink_a x = lazy_list.join (lazy_list.of_list $ shrink_a x $ Ξ» t', rec_shrink _ (Ξ» x h', rec_shrink_with shrink_a x) _) := begin conv_lhs { rw [rec_shrink_with, well_founded.fix_eq], }, congr, ext ⟨y, h⟩, refl end /-- `tree.shrink_with shrink_f t` shrinks `xs` by using the empty tree, each subtrees, and by shrinking the subtree to recombine them. This strategy is taken directly from Haskell's QuickCheck -/ def tree.shrink_with [has_sizeof Ξ±] (shrink_a : shrink_fn Ξ±) : shrink_fn (tree Ξ±) := rec_shrink_with $ Ξ» t, match t with | tree.nil := Ξ» f_rec, [] | (tree.node x tβ‚€ t₁) := Ξ» f_rec, have hβ‚‚ : sizeof_lt tree.nil (tree.node x tβ‚€ t₁), by clear _match; have := tree.one_le_sizeof tβ‚€; dsimp [sizeof_lt, sizeof, has_sizeof.sizeof] at *; unfold_wf; linarith, have hβ‚€ : sizeof_lt tβ‚€ (tree.node x tβ‚€ t₁), by dsimp [sizeof_lt]; unfold_wf; linarith, have h₁ : sizeof_lt t₁ (tree.node x tβ‚€ t₁), by dsimp [sizeof_lt]; unfold_wf; linarith, [lazy_list.of_list [⟨tree.nil, hβ‚‚βŸ©, ⟨tβ‚€, hβ‚€βŸ©, ⟨t₁, hβ‚βŸ©], (prod.shrink shrink_a (prod.shrink f_rec f_rec) (x, ⟨tβ‚€, hβ‚€βŸ©, ⟨t₁, hβ‚βŸ©)).map $ Ξ» ⟨⟨y,⟨t'β‚€, _⟩,⟨t'₁, _⟩⟩,hy⟩, ⟨tree.node y t'β‚€ t'₁, by revert hy; dsimp [sizeof_lt]; unfold_wf; intro; linarith⟩] end instance sampleable_tree : sampleable_functor tree := { wf := _, sample := Ξ» Ξ± sam_Ξ±, sized $ tree.sample sam_Ξ±, shrink := Ξ» Ξ± IΞ± shr_Ξ±, @tree.shrink_with _ IΞ± shr_Ξ±, p_repr := @tree.has_repr } /-- Type tag that signals to `slim_check` to use small values for a given type. -/ def small (Ξ± : Type*) := Ξ± /-- Add the `small` type tag -/ def small.mk {Ξ±} (x : Ξ±) : small Ξ± := x /-- Type tag that signals to `slim_check` to use large values for a given type. -/ def large (Ξ± : Type*) := Ξ± /-- Add the `large` type tag -/ def large.mk {Ξ±} (x : Ξ±) : large Ξ± := x instance small.functor : functor small := id.monad.to_functor instance large.functor : functor large := id.monad.to_functor instance small.inhabited [inhabited Ξ±] : inhabited (small Ξ±) := ⟨ (default : Ξ±) ⟩ instance large.inhabited [inhabited Ξ±] : inhabited (large Ξ±) := ⟨ (default : Ξ±) ⟩ instance small.sampleable_functor : sampleable_functor small := { wf := _, sample := Ξ» Ξ± samp, gen.resize (Ξ» n, n / 5 + 5) samp, shrink := Ξ» Ξ± _, id, p_repr := Ξ» Ξ±, id } instance large.sampleable_functor : sampleable_functor large := { wf := _, sample := Ξ» Ξ± samp, gen.resize (Ξ» n, n * 5) samp, shrink := Ξ» Ξ± _, id, p_repr := Ξ» Ξ±, id } instance ulift.sampleable_functor : sampleable_functor ulift.{u v} := { wf := Ξ» Ξ± h, ⟨ Ξ» ⟨x⟩, @sizeof Ξ± h x ⟩, sample := Ξ» Ξ± samp, uliftable.up_map ulift.up $ samp, shrink := Ξ» Ξ± _ shr ⟨x⟩, (shr x).map (subtype.map ulift.up (Ξ» a h, h)), p_repr := Ξ» Ξ± h, ⟨ @repr Ξ± h ∘ ulift.down ⟩ } /-! ## Subtype instances The following instances are meant to improve the testing of properties of the form `βˆ€ i j, i ≀ j, ...` The naive way to test them is to choose two numbers `i` and `j` and check that the proper ordering is satisfied. Instead, the following instances make it so that `j` will be chosen with considerations to the required ordering constraints. The benefit is that we will not have to discard any choice of `j`. -/ /-! ### Subtypes of `β„•` -/ instance nat_le.sampleable {y} : slim_check.sampleable { x : β„• // x ≀ y } := { sample := do { ⟨x,h⟩ ← slim_check.gen.choose_nat 0 y dec_trivial, pure ⟨x, h.2⟩}, shrink := Ξ» ⟨x, h⟩, (Ξ» a : subtype _, subtype.rec_on a $ Ξ» x' h', ⟨⟨x', le_trans (le_of_lt h') h⟩, h'⟩) <$> shrink x } instance nat_ge.sampleable {x} : slim_check.sampleable { y : β„• // x ≀ y } := { sample := do { (y : β„•) ← slim_check.sampleable.sample β„•, pure ⟨x+y, by norm_num⟩ }, shrink := Ξ» ⟨y, h⟩, (Ξ» a : { y' // sizeof y' < sizeof (y - x) }, subtype.rec_on a $ Ξ» Ξ΄ h', ⟨⟨x + Ξ΄, nat.le_add_right _ _⟩, lt_tsub_iff_left.mp h'⟩) <$> shrink (y - x) } /- there is no `nat_lt.sampleable` instance because if `y = 0`, there is no valid choice to satisfy `x < y` -/ instance nat_gt.sampleable {x} : slim_check.sampleable { y : β„• // x < y } := { sample := do { (y : β„•) ← slim_check.sampleable.sample β„•, pure ⟨x+y+1, by linarith⟩ }, shrink := Ξ» x, shrink _ } /-! ### Subtypes of any `linear_ordered_add_comm_group` -/ instance le.sampleable {y : Ξ±} [sampleable Ξ±] [linear_ordered_add_comm_group Ξ±] : slim_check.sampleable { x : Ξ± // x ≀ y } := { sample := do { x ← sample Ξ±, pure ⟨y - |x|, sub_le_self _ (abs_nonneg _) ⟩ }, shrink := Ξ» _, lazy_list.nil } instance ge.sampleable {x : Ξ±} [sampleable Ξ±] [linear_ordered_add_comm_group Ξ±] : slim_check.sampleable { y : Ξ± // x ≀ y } := { sample := do { y ← sample Ξ±, pure ⟨x + |y|, by norm_num [abs_nonneg]⟩ }, shrink := Ξ» _, lazy_list.nil } /-! ### Subtypes of `β„€` Specializations of `le.sampleable` and `ge.sampleable` for `β„€` to help instance search. -/ instance int_le.sampleable {y : β„€} : slim_check.sampleable { x : β„€ // x ≀ y } := sampleable.lift β„• (Ξ» n, ⟨y - n, int.sub_left_le_of_le_add $ by simp⟩) (Ξ» ⟨i, h⟩, (y - i).nat_abs) (Ξ» n, by unfold_wf; simp [int_le.sampleable._match_1]; ring) instance int_ge.sampleable {x : β„€} : slim_check.sampleable { y : β„€ // x ≀ y } := sampleable.lift β„• (Ξ» n, ⟨x + n, by simp⟩) (Ξ» ⟨i, h⟩, (i - x).nat_abs) (Ξ» n, by unfold_wf; simp [int_ge.sampleable._match_1]; ring) instance int_lt.sampleable {y} : slim_check.sampleable { x : β„€ // x < y } := sampleable.lift β„• (Ξ» n, ⟨y - (n+1), int.sub_left_lt_of_lt_add $ by linarith [int.coe_nat_nonneg n]⟩) (Ξ» ⟨i, h⟩, (y - i - 1).nat_abs) (Ξ» n, by unfold_wf; simp [int_lt.sampleable._match_1]; ring) instance int_gt.sampleable {x} : slim_check.sampleable { y : β„€ // x < y } := sampleable.lift β„• (Ξ» n, ⟨x + (n+1), by linarith⟩) (Ξ» ⟨i, h⟩, (i - x - 1).nat_abs) (Ξ» n, by unfold_wf; simp [int_gt.sampleable._match_1]; ring) /-! ### Subtypes of any `list` -/ instance perm.slim_check {xs : list Ξ±} : slim_check.sampleable { ys : list Ξ± // list.perm xs ys } := { sample := permutation_of xs, shrink := Ξ» _, lazy_list.nil } instance perm'.slim_check {xs : list Ξ±} : slim_check.sampleable { ys : list Ξ± // list.perm ys xs } := { sample := subtype.map id (@list.perm.symm Ξ± _) <$> permutation_of xs, shrink := Ξ» _, lazy_list.nil } setup_tactic_parser open tactic /-- Print (at most) 10 samples of a given type to stdout for debugging. -/ def print_samples {t : Type u} [has_repr t] (g : gen t) : io unit := do xs ← io.run_rand $ uliftable.down $ do { xs ← (list.range 10).mmap $ g.run ∘ ulift.up, pure ⟨xs.map repr⟩ }, xs.mmap' io.put_str_ln /-- Create a `gen Ξ±` expression from the argument of `#sample` -/ meta def mk_generator (e : expr) : tactic (expr Γ— expr) := do t ← infer_type e, match t with | `(gen %%t) := do repr_inst ← mk_app ``has_repr [t] >>= mk_instance, pure (repr_inst, e) | _ := do samp_inst ← to_expr ``(sampleable_ext %%e) >>= mk_instance, repr_inst ← mk_mapp ``sampleable_ext.p_repr [e, samp_inst], gen ← mk_mapp ``sampleable_ext.sample [none, samp_inst], pure (repr_inst, gen) end /-- `#sample my_type`, where `my_type` has an instance of `sampleable`, prints ten random values of type `my_type` of using an increasing size parameter. ```lean #sample nat -- prints -- 0 -- 0 -- 2 -- 24 -- 64 -- 76 -- 5 -- 132 -- 8 -- 449 -- or some other sequence of numbers #sample list int -- prints -- [] -- [1, 1] -- [-7, 9, -6] -- [36] -- [-500, 105, 260] -- [-290] -- [17, 156] -- [-2364, -7599, 661, -2411, -3576, 5517, -3823, -968] -- [-643] -- [11892, 16329, -15095, -15461] -- or whatever ``` -/ @[user_command] meta def sample_cmd (_ : parse $ tk "#sample") : lean.parser unit := do e ← texpr, of_tactic $ do e ← i_to_expr e, (repr_inst, gen) ← mk_generator e, print_samples ← mk_mapp ``print_samples [none, repr_inst, gen], sample ← eval_expr (io unit) print_samples, unsafe_run_io sample end slim_check
397b767c7110522a4eaf7f3b761bda08814f1806
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/algebra/order/ring/char_zero.lean
abff0e877ff3d2485150c76b199038eaaff175f7
[ "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
701
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 -/ import algebra.char_zero.defs import algebra.order.ring.defs /-! # Strict ordered semiring have characteristic zero > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. -/ variables {α : Type*} @[priority 100] -- see Note [lower instance priority] instance strict_ordered_semiring.to_char_zero [strict_ordered_semiring α] : char_zero α := ⟨strict_mono.injective $ strict_mono_nat_of_lt_succ $ λ n, by { rw [nat.cast_succ], apply lt_add_one }⟩
65148884e141db4d84ac896d807baf200b17607c
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/order/well_founded.lean
09441b6d892ff2e44adc1e5813a50f0efd52bc99
[]
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
3,735
lean
/- Copyright (c) 2020 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Mario Carneiro -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.set.basic import Mathlib.PostPort universes u_1 namespace Mathlib /-! # Well-founded relations A relation is well-founded if it can be used for induction: for each `x`, `(βˆ€ y, r y x β†’ P y) β†’ P x` implies `P x`. Well-founded relations can be used for induction and recursion, including construction of fixed points in the space of dependent functions `Ξ  x : Ξ± , Ξ² x`. The predicate `well_founded` is defined in the core library. In this file we prove some extra lemmas and provide a few new definitions: `well_founded.min`, `well_founded.sup`, and `well_founded.succ`. -/ namespace well_founded /-- If `r` is a well-founded relation, then any nonempty set has a minimal element with respect to `r`. -/ theorem has_min {Ξ± : Type u_1} {r : Ξ± β†’ Ξ± β†’ Prop} (H : well_founded r) (s : set Ξ±) : set.nonempty s β†’ βˆƒ (a : Ξ±), βˆƒ (H : a ∈ s), βˆ€ (x : Ξ±), x ∈ s β†’ Β¬r x a := sorry /-- A minimal element of a nonempty set in a well-founded order -/ def min {Ξ± : Type u_1} {r : Ξ± β†’ Ξ± β†’ Prop} (H : well_founded r) (p : set Ξ±) (h : set.nonempty p) : Ξ± := classical.some (has_min H p h) theorem min_mem {Ξ± : Type u_1} {r : Ξ± β†’ Ξ± β†’ Prop} (H : well_founded r) (p : set Ξ±) (h : set.nonempty p) : min H p h ∈ p := sorry theorem not_lt_min {Ξ± : Type u_1} {r : Ξ± β†’ Ξ± β†’ Prop} (H : well_founded r) (p : set Ξ±) (h : set.nonempty p) {x : Ξ±} (xp : x ∈ p) : Β¬r x (min H p h) := sorry theorem well_founded_iff_has_min {Ξ± : Type u_1} {r : Ξ± β†’ Ξ± β†’ Prop} : well_founded r ↔ βˆ€ (p : set Ξ±), set.nonempty p β†’ βˆƒ (m : Ξ±), βˆƒ (H : m ∈ p), βˆ€ (x : Ξ±), x ∈ p β†’ Β¬r x m := sorry theorem eq_iff_not_lt_of_le {Ξ± : Type u_1} [partial_order Ξ±] {x : Ξ±} {y : Ξ±} : x ≀ y β†’ y = x ↔ Β¬x < y := sorry theorem well_founded_iff_has_max' {Ξ± : Type u_1} [partial_order Ξ±] : well_founded gt ↔ βˆ€ (p : set Ξ±), set.nonempty p β†’ βˆƒ (m : Ξ±), βˆƒ (H : m ∈ p), βˆ€ (x : Ξ±), x ∈ p β†’ m ≀ x β†’ x = m := sorry theorem well_founded_iff_has_min' {Ξ± : Type u_1} [partial_order Ξ±] : well_founded Less ↔ βˆ€ (p : set Ξ±), set.nonempty p β†’ βˆƒ (m : Ξ±), βˆƒ (H : m ∈ p), βˆ€ (x : Ξ±), x ∈ p β†’ x ≀ m β†’ x = m := well_founded_iff_has_max' /-- The supremum of a bounded, well-founded order -/ protected def sup {Ξ± : Type u_1} {r : Ξ± β†’ Ξ± β†’ Prop} (wf : well_founded r) (s : set Ξ±) (h : bounded r s) : Ξ± := min wf (set_of fun (x : Ξ±) => βˆ€ (a : Ξ±), a ∈ s β†’ r a x) h protected theorem lt_sup {Ξ± : Type u_1} {r : Ξ± β†’ Ξ± β†’ Prop} (wf : well_founded r) {s : set Ξ±} (h : bounded r s) {x : Ξ±} (hx : x ∈ s) : r x (well_founded.sup wf s h) := min_mem wf (set_of fun (x : Ξ±) => βˆ€ (a : Ξ±), a ∈ s β†’ r a x) h x hx /-- A successor of an element `x` in a well-founded order is a minimal element `y` such that `x < y` if one exists. Otherwise it is `x` itself. -/ protected def succ {Ξ± : Type u_1} {r : Ξ± β†’ Ξ± β†’ Prop} (wf : well_founded r) (x : Ξ±) : Ξ± := dite (βˆƒ (y : Ξ±), r x y) (fun (h : βˆƒ (y : Ξ±), r x y) => min wf (set_of fun (y : Ξ±) => r x y) h) fun (h : Β¬βˆƒ (y : Ξ±), r x y) => x protected theorem lt_succ {Ξ± : Type u_1} {r : Ξ± β†’ Ξ± β†’ Prop} (wf : well_founded r) {x : Ξ±} (h : βˆƒ (y : Ξ±), r x y) : r x (well_founded.succ wf x) := sorry protected theorem lt_succ_iff {Ξ± : Type u_1} {r : Ξ± β†’ Ξ± β†’ Prop} [wo : is_well_order Ξ± r] {x : Ξ±} (h : βˆƒ (y : Ξ±), r x y) (y : Ξ±) : r y (well_founded.succ is_well_order.wf x) ↔ r y x ∨ y = x := sorry
5d44b26bb3bf5dda4fbfc61117d8da8324d4da3e
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/src/ring_theory/witt_vector/defs.lean
519296c39f2a7d0842d5adc718700789af2d7423
[ "Apache-2.0" ]
permissive
waynemunro/mathlib
e3fd4ff49f4cb43d4a8ded59d17be407bc5ee552
065a70810b5480d584033f7bbf8e0409480c2118
refs/heads/master
1,693,417,182,397
1,634,644,781,000
1,634,644,781,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
11,679
lean
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Robert Y. Lewis -/ import ring_theory.witt_vector.structure_polynomial /-! # Witt vectors In this file we define the type of `p`-typical Witt vectors and ring operations on it. The ring axioms are verified in `ring_theory/witt_vector/basic.lean`. For a fixed commutative ring `R` and prime `p`, a Witt vector `x : π•Ž R` is an infinite sequence `β„• β†’ R` of elements of `R`. However, the ring operations `+` and `*` are not defined in the obvious component-wise way. Instead, these operations are defined via certain polynomials using the machinery in `structure_polynomial.lean`. The `n`th value of the sum of two Witt vectors can depend on the `0`-th through `n`th values of the summands. This effectively simulates a β€œcarrying” operation. ## Main definitions * `witt_vector p R`: the type of `p`-typical Witt vectors with coefficients in `R`. * `witt_vector.coeff x n`: projects the `n`th value of the Witt vector `x`. ## Notation We use notation `π•Ž R`, entered `\bbW`, for the Witt vectors over `R`. ## References * [Hazewinkel, *Witt Vectors*][Haze09] * [Commelin and Lewis, *Formalizing the Ring of Witt Vectors*][CL21] -/ noncomputable theory /-- `witt_vector p R` is the ring of `p`-typical Witt vectors over the commutative ring `R`, where `p` is a prime number. If `p` is invertible in `R`, this ring is isomorphic to `β„• β†’ R` (the product of `β„•` copies of `R`). If `R` is a ring of characteristic `p`, then `witt_vector p R` is a ring of characteristic `0`. The canonical example is `witt_vector p (zmod p)`, which is isomorphic to the `p`-adic integers `β„€_[p]`. -/ structure witt_vector (p : β„•) (R : Type*) := mk [] :: (coeff : β„• β†’ R) variables {p : β„•} /- We cannot make this `localized` notation, because the `p` on the RHS doesn't occur on the left Hiding the `p` in the notation is very convenient, so we opt for repeating the `local notation` in other files that use Witt vectors. -/ local notation `π•Ž` := witt_vector p -- type as `\bbW` namespace witt_vector variables (p) {R : Type*} /-- Construct a Witt vector `mk p x : π•Ž R` from a sequence `x` of elements of `R`. -/ add_decl_doc witt_vector.mk /-- `x.coeff n` is the `n`th coefficient of the Witt vector `x`. This concept does not have a standard name in the literature. -/ add_decl_doc witt_vector.coeff @[ext] lemma ext {x y : π•Ž R} (h : βˆ€ n, x.coeff n = y.coeff n) : x = y := begin cases x, cases y, simp only at h, simp [function.funext_iff, h] end lemma ext_iff {x y : π•Ž R} : x = y ↔ βˆ€ n, x.coeff n = y.coeff n := ⟨λ h n, by rw h, ext⟩ lemma coeff_mk (x : β„• β†’ R) : (mk p x).coeff = x := rfl /- These instances are not needed for the rest of the development, but it is interesting to establish early on that `witt_vector p` is a lawful functor. -/ instance : functor (witt_vector p) := { map := Ξ» Ξ± Ξ² f v, mk p (f ∘ v.coeff), map_const := Ξ» Ξ± Ξ² a v, mk p (Ξ» _, a) } instance : is_lawful_functor (witt_vector p) := { map_const_eq := Ξ» Ξ± Ξ², rfl, id_map := Ξ» Ξ± ⟨v, _⟩, rfl, comp_map := Ξ» Ξ± Ξ² Ξ³ f g v, rfl } variables (p) [hp : fact p.prime] [comm_ring R] include hp open mv_polynomial section ring_operations /-- The polynomials used for defining the element `0` of the ring of Witt vectors. -/ def witt_zero : β„• β†’ mv_polynomial (fin 0 Γ— β„•) β„€ := witt_structure_int p 0 /-- The polynomials used for defining the element `1` of the ring of Witt vectors. -/ def witt_one : β„• β†’ mv_polynomial (fin 0 Γ— β„•) β„€ := witt_structure_int p 1 /-- The polynomials used for defining the addition of the ring of Witt vectors. -/ def witt_add : β„• β†’ mv_polynomial (fin 2 Γ— β„•) β„€ := witt_structure_int p (X 0 + X 1) /-- The polynomials used for describing the subtraction of the ring of Witt vectors. -/ def witt_sub : β„• β†’ mv_polynomial (fin 2 Γ— β„•) β„€ := witt_structure_int p (X 0 - X 1) /-- The polynomials used for defining the multiplication of the ring of Witt vectors. -/ def witt_mul : β„• β†’ mv_polynomial (fin 2 Γ— β„•) β„€ := witt_structure_int p (X 0 * X 1) /-- The polynomials used for defining the negation of the ring of Witt vectors. -/ def witt_neg : β„• β†’ mv_polynomial (fin 1 Γ— β„•) β„€ := witt_structure_int p (-X 0) variable {p} omit hp /-- An auxiliary definition used in `witt_vector.eval`. Evaluates a polynomial whose variables come from the disjoint union of `k` copies of `β„•`, with a curried evaluation `x`. This can be defined more generally but we use only a specific instance here. -/ def peval {k : β„•} (Ο† : mv_polynomial (fin k Γ— β„•) β„€) (x : fin k β†’ β„• β†’ R) : R := aeval (function.uncurry x) Ο† /-- Let `Ο†` be a family of polynomials, indexed by natural numbers, whose variables come from the disjoint union of `k` copies of `β„•`, and let `xα΅’` be a Witt vector for `0 ≀ i < k`. `eval Ο† x` evaluates `Ο†` mapping the variable `X_(i, n)` to the `n`th coefficient of `xα΅’`. Instantiating `Ο†` with certain polynomials defined in `structure_polynomial.lean` establishes the ring operations on `π•Ž R`. For example, `witt_vector.witt_add` is such a `Ο†` with `k = 2`; evaluating this at `(xβ‚€, x₁)` gives us the sum of two Witt vectors `xβ‚€ + x₁`. -/ def eval {k : β„•} (Ο† : β„• β†’ mv_polynomial (fin k Γ— β„•) β„€) (x : fin k β†’ π•Ž R) : π•Ž R := mk p $ Ξ» n, peval (Ο† n) $ Ξ» i, (x i).coeff variables (R) [fact p.prime] instance : has_zero (π•Ž R) := ⟨eval (witt_zero p) ![]⟩ instance : inhabited (π•Ž R) := ⟨0⟩ instance : has_one (π•Ž R) := ⟨eval (witt_one p) ![]⟩ instance : has_add (π•Ž R) := ⟨λ x y, eval (witt_add p) ![x, y]⟩ instance : has_sub (π•Ž R) := ⟨λ x y, eval (witt_sub p) ![x, y]⟩ instance : has_mul (π•Ž R) := ⟨λ x y, eval (witt_mul p) ![x, y]⟩ instance : has_neg (π•Ž R) := ⟨λ x, eval (witt_neg p) ![x]⟩ end ring_operations section witt_structure_simplifications @[simp] lemma witt_zero_eq_zero (n : β„•) : witt_zero p n = 0 := begin apply mv_polynomial.map_injective (int.cast_ring_hom β„š) int.cast_injective, simp only [witt_zero, witt_structure_rat, bind₁, aeval_zero', constant_coeff_X_in_terms_of_W, ring_hom.map_zero, alg_hom.map_zero, map_witt_structure_int], end @[simp] lemma witt_one_zero_eq_one : witt_one p 0 = 1 := begin apply mv_polynomial.map_injective (int.cast_ring_hom β„š) int.cast_injective, simp only [witt_one, witt_structure_rat, X_in_terms_of_W_zero, alg_hom.map_one, ring_hom.map_one, bind₁_X_right, map_witt_structure_int] end @[simp] lemma witt_one_pos_eq_zero (n : β„•) (hn : 0 < n) : witt_one p n = 0 := begin apply mv_polynomial.map_injective (int.cast_ring_hom β„š) int.cast_injective, simp only [witt_one, witt_structure_rat, ring_hom.map_zero, alg_hom.map_one, ring_hom.map_one, map_witt_structure_int], revert hn, apply nat.strong_induction_on n, clear n, intros n IH hn, rw X_in_terms_of_W_eq, simp only [alg_hom.map_mul, alg_hom.map_sub, alg_hom.map_sum, alg_hom.map_pow, bind₁_X_right, bind₁_C_right], rw [sub_mul, one_mul], rw [finset.sum_eq_single 0], { simp only [inv_of_eq_inv, one_mul, inv_powβ‚€, nat.sub_zero, ring_hom.map_one, pow_zero], simp only [one_pow, one_mul, X_in_terms_of_W_zero, sub_self, bind₁_X_right] }, { intros i hin hi0, rw [finset.mem_range] at hin, rw [IH _ hin (nat.pos_of_ne_zero hi0), zero_pow (pow_pos hp.1.pos _), mul_zero], }, { rw finset.mem_range, intro, contradiction } end @[simp] lemma witt_add_zero : witt_add p 0 = X (0,0) + X (1,0) := begin apply mv_polynomial.map_injective (int.cast_ring_hom β„š) int.cast_injective, simp only [witt_add, witt_structure_rat, alg_hom.map_add, ring_hom.map_add, rename_X, X_in_terms_of_W_zero, map_X, witt_polynomial_zero, bind₁_X_right, map_witt_structure_int], end @[simp] lemma witt_sub_zero : witt_sub p 0 = X (0,0) - X (1,0) := begin apply mv_polynomial.map_injective (int.cast_ring_hom β„š) int.cast_injective, simp only [witt_sub, witt_structure_rat, alg_hom.map_sub, ring_hom.map_sub, rename_X, X_in_terms_of_W_zero, map_X, witt_polynomial_zero, bind₁_X_right, map_witt_structure_int], end @[simp] lemma witt_mul_zero : witt_mul p 0 = X (0,0) * X (1,0) := begin apply mv_polynomial.map_injective (int.cast_ring_hom β„š) int.cast_injective, simp only [witt_mul, witt_structure_rat, rename_X, X_in_terms_of_W_zero, map_X, witt_polynomial_zero, ring_hom.map_mul, bind₁_X_right, alg_hom.map_mul, map_witt_structure_int] end @[simp] lemma witt_neg_zero : witt_neg p 0 = - X (0,0) := begin apply mv_polynomial.map_injective (int.cast_ring_hom β„š) int.cast_injective, simp only [witt_neg, witt_structure_rat, rename_X, X_in_terms_of_W_zero, map_X, witt_polynomial_zero, ring_hom.map_neg, alg_hom.map_neg, bind₁_X_right, map_witt_structure_int] end @[simp] lemma constant_coeff_witt_add (n : β„•) : constant_coeff (witt_add p n) = 0 := begin apply constant_coeff_witt_structure_int p _ _ n, simp only [add_zero, ring_hom.map_add, constant_coeff_X], end @[simp] lemma constant_coeff_witt_sub (n : β„•) : constant_coeff (witt_sub p n) = 0 := begin apply constant_coeff_witt_structure_int p _ _ n, simp only [sub_zero, ring_hom.map_sub, constant_coeff_X], end @[simp] lemma constant_coeff_witt_mul (n : β„•) : constant_coeff (witt_mul p n) = 0 := begin apply constant_coeff_witt_structure_int p _ _ n, simp only [mul_zero, ring_hom.map_mul, constant_coeff_X], end @[simp] lemma constant_coeff_witt_neg (n : β„•) : constant_coeff (witt_neg p n) = 0 := begin apply constant_coeff_witt_structure_int p _ _ n, simp only [neg_zero, ring_hom.map_neg, constant_coeff_X], end end witt_structure_simplifications section coeff variables (p R) @[simp] lemma zero_coeff (n : β„•) : (0 : π•Ž R).coeff n = 0 := show (aeval _ (witt_zero p n) : R) = 0, by simp only [witt_zero_eq_zero, alg_hom.map_zero] @[simp] lemma one_coeff_zero : (1 : π•Ž R).coeff 0 = 1 := show (aeval _ (witt_one p 0) : R) = 1, by simp only [witt_one_zero_eq_one, alg_hom.map_one] @[simp] lemma one_coeff_eq_of_pos (n : β„•) (hn : 0 < n) : coeff (1 : π•Ž R) n = 0 := show (aeval _ (witt_one p n) : R) = 0, by simp only [hn, witt_one_pos_eq_zero, alg_hom.map_zero] variables {p R} omit hp @[simp] lemma v2_coeff {p' R'} (x y : witt_vector p' R') (i : fin 2) : (![x, y] i).coeff = ![x.coeff, y.coeff] i := by fin_cases i; simp include hp lemma add_coeff (x y : π•Ž R) (n : β„•) : (x + y).coeff n = peval (witt_add p n) ![x.coeff, y.coeff] := by simp [(+), eval] lemma sub_coeff (x y : π•Ž R) (n : β„•) : (x - y).coeff n = peval (witt_sub p n) ![x.coeff, y.coeff] := by simp [has_sub.sub, eval] lemma mul_coeff (x y : π•Ž R) (n : β„•) : (x * y).coeff n = peval (witt_mul p n) ![x.coeff, y.coeff] := by simp [(*), eval] lemma neg_coeff (x : π•Ž R) (n : β„•) : (-x).coeff n = peval (witt_neg p n) ![x.coeff] := by simp [has_neg.neg, eval, matrix.cons_fin_one] end coeff lemma witt_add_vars (n : β„•) : (witt_add p n).vars βŠ† finset.univ.product (finset.range (n + 1)) := witt_structure_int_vars _ _ _ lemma witt_sub_vars (n : β„•) : (witt_sub p n).vars βŠ† finset.univ.product (finset.range (n + 1)) := witt_structure_int_vars _ _ _ lemma witt_mul_vars (n : β„•) : (witt_mul p n).vars βŠ† finset.univ.product (finset.range (n + 1)) := witt_structure_int_vars _ _ _ lemma witt_neg_vars (n : β„•) : (witt_neg p n).vars βŠ† finset.univ.product (finset.range (n + 1)) := witt_structure_int_vars _ _ _ end witt_vector
cb09d1da70f5fe26218b53d1b08e888065608b39
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/ring_theory/coprime.lean
35762ec488b20232d335cd1c2fbad6c3929d3cf4
[]
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
13,579
lean
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Ken Lee, Chris Hughes -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.tactic.ring import Mathlib.algebra.big_operators.basic import Mathlib.data.fintype.basic import Mathlib.data.int.gcd import Mathlib.data.set.disjointed import Mathlib.PostPort universes u v namespace Mathlib /-! # Coprime elements of a ring ## Main definitions * `is_coprime x y`: that `x` and `y` are coprime, defined to be the existence of `a` and `b` such that `a * x + b * y = 1`. Note that elements with no common divisors are not necessarily coprime, e.g., the multivariate polynomials `x₁` and `xβ‚‚` are not coprime. -/ /-- The proposition that `x` and `y` are coprime, defined to be the existence of `a` and `b` such that `a * x + b * y = 1`. Note that elements with no common divisors are not necessarily coprime, e.g., the multivariate polynomials `x₁` and `xβ‚‚` are not coprime. -/ @[simp] def is_coprime {R : Type u} [comm_semiring R] (x : R) (y : R) := βˆƒ (a : R), βˆƒ (b : R), a * x + b * y = 1 theorem nat.is_coprime_iff_coprime {m : β„•} {n : β„•} : is_coprime ↑m ↑n ↔ nat.coprime m n := sorry theorem is_coprime.symm {R : Type u} [comm_semiring R] {x : R} {y : R} (H : is_coprime x y) : is_coprime y x := sorry theorem is_coprime_comm {R : Type u} [comm_semiring R] {x : R} {y : R} : is_coprime x y ↔ is_coprime y x := { mp := is_coprime.symm, mpr := is_coprime.symm } theorem is_coprime_self {R : Type u} [comm_semiring R] {x : R} : is_coprime x x ↔ is_unit x := sorry theorem is_coprime_zero_left {R : Type u} [comm_semiring R] {x : R} : is_coprime 0 x ↔ is_unit x := sorry theorem is_coprime_zero_right {R : Type u} [comm_semiring R] {x : R} : is_coprime x 0 ↔ is_unit x := iff.trans is_coprime_comm is_coprime_zero_left theorem is_coprime_one_left {R : Type u} [comm_semiring R] {x : R} : is_coprime 1 x := sorry theorem is_coprime_one_right {R : Type u} [comm_semiring R] {x : R} : is_coprime x 1 := sorry theorem is_coprime.dvd_of_dvd_mul_right {R : Type u} [comm_semiring R] {x : R} {y : R} {z : R} (H1 : is_coprime x z) (H2 : x ∣ y * z) : x ∣ y := sorry theorem is_coprime.dvd_of_dvd_mul_left {R : Type u} [comm_semiring R] {x : R} {y : R} {z : R} (H1 : is_coprime x y) (H2 : x ∣ y * z) : x ∣ z := sorry theorem is_coprime.mul_left {R : Type u} [comm_semiring R] {x : R} {y : R} {z : R} (H1 : is_coprime x z) (H2 : is_coprime y z) : is_coprime (x * y) z := sorry theorem is_coprime.mul_right {R : Type u} [comm_semiring R] {x : R} {y : R} {z : R} (H1 : is_coprime x y) (H2 : is_coprime x z) : is_coprime x (y * z) := eq.mpr (id (Eq._oldrec (Eq.refl (is_coprime x (y * z))) (propext is_coprime_comm))) (is_coprime.mul_left (eq.mp (Eq._oldrec (Eq.refl (is_coprime x y)) (propext is_coprime_comm)) H1) (eq.mp (Eq._oldrec (Eq.refl (is_coprime x z)) (propext is_coprime_comm)) H2)) theorem is_coprime.prod_left {R : Type u} [comm_semiring R] {x : R} {I : Type v} {s : I β†’ R} {t : finset I} : (βˆ€ (i : I), i ∈ t β†’ is_coprime (s i) x) β†’ is_coprime (finset.prod t fun (i : I) => s i) x := sorry theorem is_coprime.prod_right {R : Type u} [comm_semiring R] {x : R} {I : Type v} {s : I β†’ R} {t : finset I} : (βˆ€ (i : I), i ∈ t β†’ is_coprime x (s i)) β†’ is_coprime x (finset.prod t fun (i : I) => s i) := sorry theorem is_coprime.mul_dvd {R : Type u} [comm_semiring R] {x : R} {y : R} {z : R} (H : is_coprime x y) (H1 : x ∣ z) (H2 : y ∣ z) : x * y ∣ z := sorry theorem finset.prod_dvd_of_coprime {R : Type u} [comm_semiring R] {z : R} {I : Type v} {s : I β†’ R} {t : finset I} (Hs : set.pairwise_on (↑t) (is_coprime on s)) (Hs1 : βˆ€ (i : I), i ∈ t β†’ s i ∣ z) : (finset.prod t fun (x : I) => s x) ∣ z := sorry theorem fintype.prod_dvd_of_coprime {R : Type u} [comm_semiring R] {z : R} {I : Type v} {s : I β†’ R} [fintype I] (Hs : pairwise (is_coprime on s)) (Hs1 : βˆ€ (i : I), s i ∣ z) : (finset.prod finset.univ fun (x : I) => s x) ∣ z := finset.prod_dvd_of_coprime (pairwise.pairwise_on Hs ↑finset.univ) fun (i : I) (_x : i ∈ finset.univ) => Hs1 i theorem is_coprime.of_mul_left_left {R : Type u} [comm_semiring R] {x : R} {y : R} {z : R} (H : is_coprime (x * y) z) : is_coprime x z := sorry theorem is_coprime.of_mul_left_right {R : Type u} [comm_semiring R] {x : R} {y : R} {z : R} (H : is_coprime (x * y) z) : is_coprime y z := is_coprime.of_mul_left_left (eq.mp (Eq._oldrec (Eq.refl (is_coprime (x * y) z)) (mul_comm x y)) H) theorem is_coprime.of_mul_right_left {R : Type u} [comm_semiring R] {x : R} {y : R} {z : R} (H : is_coprime x (y * z)) : is_coprime x y := eq.mpr (id (Eq._oldrec (Eq.refl (is_coprime x y)) (propext is_coprime_comm))) (is_coprime.of_mul_left_left (eq.mp (Eq._oldrec (Eq.refl (is_coprime x (y * z))) (propext is_coprime_comm)) H)) theorem is_coprime.of_mul_right_right {R : Type u} [comm_semiring R] {x : R} {y : R} {z : R} (H : is_coprime x (y * z)) : is_coprime x z := is_coprime.of_mul_right_left (eq.mp (Eq._oldrec (Eq.refl (is_coprime x (y * z))) (mul_comm y z)) H) theorem is_coprime.mul_left_iff {R : Type u} [comm_semiring R] {x : R} {y : R} {z : R} : is_coprime (x * y) z ↔ is_coprime x z ∧ is_coprime y z := sorry theorem is_coprime.mul_right_iff {R : Type u} [comm_semiring R] {x : R} {y : R} {z : R} : is_coprime x (y * z) ↔ is_coprime x y ∧ is_coprime x z := sorry theorem is_coprime.prod_left_iff {R : Type u} [comm_semiring R] {x : R} {I : Type v} {s : I β†’ R} {t : finset I} : is_coprime (finset.prod t fun (i : I) => s i) x ↔ βˆ€ (i : I), i ∈ t β†’ is_coprime (s i) x := sorry theorem is_coprime.prod_right_iff {R : Type u} [comm_semiring R] {x : R} {I : Type v} {s : I β†’ R} {t : finset I} : is_coprime x (finset.prod t fun (i : I) => s i) ↔ βˆ€ (i : I), i ∈ t β†’ is_coprime x (s i) := sorry theorem is_coprime.of_prod_left {R : Type u} [comm_semiring R] {x : R} {I : Type v} {s : I β†’ R} {t : finset I} (H1 : is_coprime (finset.prod t fun (i : I) => s i) x) (i : I) (hit : i ∈ t) : is_coprime (s i) x := iff.mp is_coprime.prod_left_iff H1 i hit theorem is_coprime.of_prod_right {R : Type u} [comm_semiring R] {x : R} {I : Type v} {s : I β†’ R} {t : finset I} (H1 : is_coprime x (finset.prod t fun (i : I) => s i)) (i : I) (hit : i ∈ t) : is_coprime x (s i) := iff.mp is_coprime.prod_right_iff H1 i hit theorem is_coprime.pow_left {R : Type u} [comm_semiring R] {x : R} {y : R} {m : β„•} (H : is_coprime x y) : is_coprime (x ^ m) y := eq.mpr (id (Eq._oldrec (Eq.refl (is_coprime (x ^ m) y)) (Eq.symm (finset.card_range m)))) (eq.mpr (id (Eq._oldrec (Eq.refl (is_coprime (x ^ finset.card (finset.range m)) y)) (Eq.symm (finset.prod_const x)))) (is_coprime.prod_left fun (_x : β„•) (_x : _x ∈ finset.range m) => H)) theorem is_coprime.pow_right {R : Type u} [comm_semiring R] {x : R} {y : R} {n : β„•} (H : is_coprime x y) : is_coprime x (y ^ n) := eq.mpr (id (Eq._oldrec (Eq.refl (is_coprime x (y ^ n))) (Eq.symm (finset.card_range n)))) (eq.mpr (id (Eq._oldrec (Eq.refl (is_coprime x (y ^ finset.card (finset.range n)))) (Eq.symm (finset.prod_const y)))) (is_coprime.prod_right fun (_x : β„•) (_x : _x ∈ finset.range n) => H)) theorem is_coprime.pow {R : Type u} [comm_semiring R] {x : R} {y : R} {m : β„•} {n : β„•} (H : is_coprime x y) : is_coprime (x ^ m) (y ^ n) := is_coprime.pow_right (is_coprime.pow_left H) theorem is_coprime.is_unit_of_dvd {R : Type u} [comm_semiring R] {x : R} {y : R} (H : is_coprime x y) (d : x ∣ y) : is_unit x := sorry theorem is_coprime.map {R : Type u} [comm_semiring R] {x : R} {y : R} (H : is_coprime x y) {S : Type v} [comm_semiring S] (f : R β†’+* S) : is_coprime (coe_fn f x) (coe_fn f y) := sorry theorem is_coprime.of_add_mul_left_left {R : Type u} [comm_semiring R] {x : R} {y : R} {z : R} (h : is_coprime (x + y * z) y) : is_coprime x y := sorry theorem is_coprime.of_add_mul_right_left {R : Type u} [comm_semiring R] {x : R} {y : R} {z : R} (h : is_coprime (x + z * y) y) : is_coprime x y := is_coprime.of_add_mul_left_left (eq.mp (Eq._oldrec (Eq.refl (is_coprime (x + z * y) y)) (mul_comm z y)) h) theorem is_coprime.of_add_mul_left_right {R : Type u} [comm_semiring R] {x : R} {y : R} {z : R} (h : is_coprime x (y + x * z)) : is_coprime x y := eq.mpr (id (Eq._oldrec (Eq.refl (is_coprime x y)) (propext is_coprime_comm))) (is_coprime.of_add_mul_left_left (eq.mp (Eq._oldrec (Eq.refl (is_coprime x (y + x * z))) (propext is_coprime_comm)) h)) theorem is_coprime.of_add_mul_right_right {R : Type u} [comm_semiring R] {x : R} {y : R} {z : R} (h : is_coprime x (y + z * x)) : is_coprime x y := is_coprime.of_add_mul_left_right (eq.mp (Eq._oldrec (Eq.refl (is_coprime x (y + z * x))) (mul_comm z x)) h) theorem is_coprime.of_mul_add_left_left {R : Type u} [comm_semiring R] {x : R} {y : R} {z : R} (h : is_coprime (y * z + x) y) : is_coprime x y := is_coprime.of_add_mul_left_left (eq.mp (Eq._oldrec (Eq.refl (is_coprime (y * z + x) y)) (add_comm (y * z) x)) h) theorem is_coprime.of_mul_add_right_left {R : Type u} [comm_semiring R] {x : R} {y : R} {z : R} (h : is_coprime (z * y + x) y) : is_coprime x y := is_coprime.of_add_mul_right_left (eq.mp (Eq._oldrec (Eq.refl (is_coprime (z * y + x) y)) (add_comm (z * y) x)) h) theorem is_coprime.of_mul_add_left_right {R : Type u} [comm_semiring R] {x : R} {y : R} {z : R} (h : is_coprime x (x * z + y)) : is_coprime x y := is_coprime.of_add_mul_left_right (eq.mp (Eq._oldrec (Eq.refl (is_coprime x (x * z + y))) (add_comm (x * z) y)) h) theorem is_coprime.of_mul_add_right_right {R : Type u} [comm_semiring R] {x : R} {y : R} {z : R} (h : is_coprime x (z * x + y)) : is_coprime x y := is_coprime.of_add_mul_right_right (eq.mp (Eq._oldrec (Eq.refl (is_coprime x (z * x + y))) (add_comm (z * x) y)) h) namespace is_coprime theorem add_mul_left_left {R : Type u} [comm_ring R] {x : R} {y : R} (h : is_coprime x y) (z : R) : is_coprime (x + y * z) y := sorry theorem add_mul_right_left {R : Type u} [comm_ring R] {x : R} {y : R} (h : is_coprime x y) (z : R) : is_coprime (x + z * y) y := eq.mpr (id (Eq._oldrec (Eq.refl (is_coprime (x + z * y) y)) (mul_comm z y))) (add_mul_left_left h z) theorem add_mul_left_right {R : Type u} [comm_ring R] {x : R} {y : R} (h : is_coprime x y) (z : R) : is_coprime x (y + x * z) := eq.mpr (id (Eq._oldrec (Eq.refl (is_coprime x (y + x * z))) (propext is_coprime_comm))) (add_mul_left_left (symm h) z) theorem add_mul_right_right {R : Type u} [comm_ring R] {x : R} {y : R} (h : is_coprime x y) (z : R) : is_coprime x (y + z * x) := eq.mpr (id (Eq._oldrec (Eq.refl (is_coprime x (y + z * x))) (propext is_coprime_comm))) (add_mul_right_left (symm h) z) theorem mul_add_left_left {R : Type u} [comm_ring R] {x : R} {y : R} (h : is_coprime x y) (z : R) : is_coprime (y * z + x) y := eq.mpr (id (Eq._oldrec (Eq.refl (is_coprime (y * z + x) y)) (add_comm (y * z) x))) (add_mul_left_left h z) theorem mul_add_right_left {R : Type u} [comm_ring R] {x : R} {y : R} (h : is_coprime x y) (z : R) : is_coprime (z * y + x) y := eq.mpr (id (Eq._oldrec (Eq.refl (is_coprime (z * y + x) y)) (add_comm (z * y) x))) (add_mul_right_left h z) theorem mul_add_left_right {R : Type u} [comm_ring R] {x : R} {y : R} (h : is_coprime x y) (z : R) : is_coprime x (x * z + y) := eq.mpr (id (Eq._oldrec (Eq.refl (is_coprime x (x * z + y))) (add_comm (x * z) y))) (add_mul_left_right h z) theorem mul_add_right_right {R : Type u} [comm_ring R] {x : R} {y : R} (h : is_coprime x y) (z : R) : is_coprime x (z * x + y) := eq.mpr (id (Eq._oldrec (Eq.refl (is_coprime x (z * x + y))) (add_comm (z * x) y))) (add_mul_right_right h z) theorem add_mul_left_left_iff {R : Type u} [comm_ring R] {x : R} {y : R} {z : R} : is_coprime (x + y * z) y ↔ is_coprime x y := { mp := of_add_mul_left_left, mpr := fun (h : is_coprime x y) => add_mul_left_left h z } theorem add_mul_right_left_iff {R : Type u} [comm_ring R] {x : R} {y : R} {z : R} : is_coprime (x + z * y) y ↔ is_coprime x y := { mp := of_add_mul_right_left, mpr := fun (h : is_coprime x y) => add_mul_right_left h z } theorem add_mul_left_right_iff {R : Type u} [comm_ring R] {x : R} {y : R} {z : R} : is_coprime x (y + x * z) ↔ is_coprime x y := { mp := of_add_mul_left_right, mpr := fun (h : is_coprime x y) => add_mul_left_right h z } theorem add_mul_right_right_iff {R : Type u} [comm_ring R] {x : R} {y : R} {z : R} : is_coprime x (y + z * x) ↔ is_coprime x y := { mp := of_add_mul_right_right, mpr := fun (h : is_coprime x y) => add_mul_right_right h z } theorem mul_add_left_left_iff {R : Type u} [comm_ring R] {x : R} {y : R} {z : R} : is_coprime (y * z + x) y ↔ is_coprime x y := { mp := of_mul_add_left_left, mpr := fun (h : is_coprime x y) => mul_add_left_left h z } theorem mul_add_right_left_iff {R : Type u} [comm_ring R] {x : R} {y : R} {z : R} : is_coprime (z * y + x) y ↔ is_coprime x y := { mp := of_mul_add_right_left, mpr := fun (h : is_coprime x y) => mul_add_right_left h z } theorem mul_add_left_right_iff {R : Type u} [comm_ring R] {x : R} {y : R} {z : R} : is_coprime x (x * z + y) ↔ is_coprime x y := { mp := of_mul_add_left_right, mpr := fun (h : is_coprime x y) => mul_add_left_right h z } theorem mul_add_right_right_iff {R : Type u} [comm_ring R] {x : R} {y : R} {z : R} : is_coprime x (z * x + y) ↔ is_coprime x y := { mp := of_mul_add_right_right, mpr := fun (h : is_coprime x y) => mul_add_right_right h z }
32108693e27836dab9e7ad4122f1b1b45221f0e1
bb31430994044506fa42fd667e2d556327e18dfe
/src/algebraic_geometry/properties.lean
52d00b6bfb420fd7dee8807fd9e0ffcdc449d218
[ "Apache-2.0" ]
permissive
sgouezel/mathlib
0cb4e5335a2ba189fa7af96d83a377f83270e503
00638177efd1b2534fc5269363ebf42a7871df9a
refs/heads/master
1,674,527,483,042
1,673,665,568,000
1,673,665,568,000
119,598,202
0
0
null
1,517,348,647,000
1,517,348,646,000
null
UTF-8
Lean
false
false
14,027
lean
/- Copyright (c) 2021 Andrew Yang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Andrew Yang -/ import algebraic_geometry.AffineScheme import ring_theory.nilpotent import topology.sheaves.sheaf_condition.sites import algebra.category.Ring.constructions import ring_theory.local_properties /-! # Basic properties of schemes We provide some basic properties of schemes ## Main definition * `algebraic_geometry.is_integral`: A scheme is integral if it is nontrivial and all nontrivial components of the structure sheaf are integral domains. * `algebraic_geometry.is_reduced`: A scheme is reduced if all the components of the structure sheaf is reduced. -/ open topological_space opposite category_theory category_theory.limits Top namespace algebraic_geometry variable (X : Scheme) instance : t0_space X.carrier := begin refine t0_space.of_open_cover (Ξ» x, _), obtain ⟨U, R, ⟨e⟩⟩ := X.local_affine x, let e' : U.1 β‰ƒβ‚œ prime_spectrum R := homeo_of_iso ((LocallyRingedSpace.forget_to_SheafedSpace β‹™ SheafedSpace.forget _).map_iso e), exact ⟨U.1.1, U.2, U.1.2, e'.embedding.t0_space⟩ end instance : quasi_sober X.carrier := begin apply_with (quasi_sober_of_open_cover (set.range (Ξ» x, set.range $ (X.affine_cover.map x).1.base))) { instances := ff }, { rintro ⟨_,i,rfl⟩, exact (X.affine_cover.is_open i).base_open.open_range }, { rintro ⟨_,i,rfl⟩, exact @@open_embedding.quasi_sober _ _ _ (homeomorph.of_embedding _ (X.affine_cover.is_open i).base_open.to_embedding) .symm.open_embedding prime_spectrum.quasi_sober }, { rw [set.top_eq_univ, set.sUnion_range, set.eq_univ_iff_forall], intro x, exact ⟨_, ⟨_, rfl⟩, X.affine_cover.covers x⟩ } end /-- A scheme `X` is reduced if all `π’ͺβ‚“(U)` are reduced. -/ class is_reduced : Prop := (component_reduced : βˆ€ U, _root_.is_reduced (X.presheaf.obj (op U)) . tactic.apply_instance) attribute [instance] is_reduced.component_reduced lemma is_reduced_of_stalk_is_reduced [βˆ€ x : X.carrier, _root_.is_reduced (X.presheaf.stalk x)] : is_reduced X := begin refine ⟨λ U, ⟨λ s hs, _⟩⟩, apply presheaf.section_ext X.sheaf U s 0, intro x, rw ring_hom.map_zero, change X.presheaf.germ x s = 0, exact (hs.map _).eq_zero end instance stalk_is_reduced_of_reduced [is_reduced X] (x : X.carrier) : _root_.is_reduced (X.presheaf.stalk x) := begin constructor, rintros g ⟨n, e⟩, obtain ⟨U, hxU, s, rfl⟩ := X.presheaf.germ_exist x g, rw [← map_pow, ← map_zero (X.presheaf.germ ⟨x, hxU⟩)] at e, obtain ⟨V, hxV, iU, iV, e'⟩ := X.presheaf.germ_eq x hxU hxU _ 0 e, rw [map_pow, map_zero] at e', replace e' := (is_nilpotent.mk _ _ e').eq_zero, erw ← concrete_category.congr_hom (X.presheaf.germ_res iU ⟨x, hxV⟩) s, rw [comp_apply, e', map_zero] end lemma is_reduced_of_open_immersion {X Y : Scheme} (f : X ⟢ Y) [H : is_open_immersion f] [is_reduced Y] : is_reduced X := begin constructor, intro U, have : U = (opens.map f.1.base).obj (H.base_open.is_open_map.functor.obj U), { ext1, exact (set.preimage_image_eq _ H.base_open.inj).symm }, rw this, exact is_reduced_of_injective (inv $ f.1.c.app (op $ H.base_open.is_open_map.functor.obj U)) (as_iso $ f.1.c.app (op $ H.base_open.is_open_map.functor.obj U) : Y.presheaf.obj _ β‰… _).symm .CommRing_iso_to_ring_equiv.injective end instance {R : CommRing} [H : _root_.is_reduced R] : is_reduced (Scheme.Spec.obj $ op R) := begin apply_with is_reduced_of_stalk_is_reduced { instances := ff }, intro x, dsimp, haveI : _root_.is_reduced (CommRing.of $ localization.at_prime (prime_spectrum.as_ideal x)), { dsimp, apply_instance }, exact is_reduced_of_injective (structure_sheaf.stalk_iso R x).hom (structure_sheaf.stalk_iso R x).CommRing_iso_to_ring_equiv.injective, end lemma affine_is_reduced_iff (R : CommRing) : is_reduced (Scheme.Spec.obj $ op R) ↔ _root_.is_reduced R := begin refine ⟨_, Ξ» h, by exactI infer_instance⟩, intro h, resetI, haveI : _root_.is_reduced (LocallyRingedSpace.Ξ“.obj (op $ Spec.to_LocallyRingedSpace.obj $ op R)), { change _root_.is_reduced ((Scheme.Spec.obj $ op R).presheaf.obj $ op ⊀), apply_instance }, exact is_reduced_of_injective (to_Spec_Ξ“ R) ((as_iso $ to_Spec_Ξ“ R).CommRing_iso_to_ring_equiv.injective) end lemma is_reduced_of_is_affine_is_reduced [is_affine X] [h : _root_.is_reduced (X.presheaf.obj (op ⊀))] : is_reduced X := begin haveI : is_reduced (Scheme.Spec.obj (op (Scheme.Ξ“.obj (op X)))), { rw affine_is_reduced_iff, exact h }, exact is_reduced_of_open_immersion X.iso_Spec.hom, end /-- To show that a statement `P` holds for all open subsets of all schemes, it suffices to show that 1. In any scheme `X`, if `P` holds for an open cover of `U`, then `P` holds for `U`. 2. For an open immerison `f : X ⟢ Y`, if `P` holds for the entire space of `X`, then `P` holds for the image of `f`. 3. `P` holds for the entire space of an affine scheme. -/ lemma reduce_to_affine_global (P : βˆ€ (X : Scheme) (U : opens X.carrier), Prop) (h₁ : βˆ€ (X : Scheme) (U : opens X.carrier), (βˆ€ (x : U), βˆƒ {V} (h : x.1 ∈ V) (i : V ⟢ U), P X V) β†’ P X U) (hβ‚‚ : βˆ€ {X Y} (f : X ⟢ Y) [hf : is_open_immersion f], βˆƒ {U : set X.carrier} {V : set Y.carrier} (hU : U = ⊀) (hV : V = set.range f.1.base), P X ⟨U, hU.symm β–Έ is_open_univ⟩ β†’ P Y ⟨V, hV.symm β–Έ hf.base_open.open_range⟩) (h₃ : βˆ€ (R : CommRing), P (Scheme.Spec.obj $ op R) ⊀) : βˆ€ (X : Scheme) (U : opens X.carrier), P X U := begin intros X U, apply h₁, intro x, obtain ⟨_,⟨j,rfl⟩,hx,i⟩ := X.affine_basis_cover_is_basis.exists_subset_of_mem_open x.prop U.2, let U' : opens _ := ⟨_, (X.affine_basis_cover.is_open j).base_open.open_range⟩, let i' : U' ⟢ U := hom_of_le i, refine ⟨U', hx, i', _⟩, obtain ⟨_,_,rfl,rfl,hβ‚‚'⟩ := hβ‚‚ (X.affine_basis_cover.map j), apply hβ‚‚', apply h₃ end . lemma reduce_to_affine_nbhd (P : βˆ€ (X : Scheme) (x : X.carrier), Prop) (h₁ : βˆ€ (R : CommRing) (x : prime_spectrum R), P (Scheme.Spec.obj $ op R) x) (hβ‚‚ : βˆ€ {X Y} (f : X ⟢ Y) [is_open_immersion f] (x : X.carrier), P X x β†’ P Y (f.1.base x)) : βˆ€ (X : Scheme) (x : X.carrier), P X x := begin intros X x, obtain ⟨y, e⟩ := X.affine_cover.covers x, convert hβ‚‚ (X.affine_cover.map (X.affine_cover.f x)) y _, { rw e }, apply h₁, end lemma eq_zero_of_basic_open_eq_bot {X : Scheme} [hX : is_reduced X] {U : opens X.carrier} (s : X.presheaf.obj (op U)) (hs : X.basic_open s = βŠ₯) : s = 0 := begin apply Top.presheaf.section_ext X.sheaf U, simp_rw ring_hom.map_zero, unfreezingI { revert X U hX s }, refine reduce_to_affine_global _ _ _ _, { intros X U hx hX s hs x, obtain ⟨V, hx, i, H⟩ := hx x, unfreezingI { specialize H (X.presheaf.map i.op s) }, erw Scheme.basic_open_res at H, rw [hs, ← subtype.coe_injective.eq_iff, inf_bot_eq] at H, specialize H rfl ⟨x, hx⟩, erw Top.presheaf.germ_res_apply at H, exact H }, { rintros X Y f hf, have e : (f.val.base) ⁻¹' set.range ⇑(f.val.base) = ⊀, { rw [← set.image_univ, set.preimage_image_eq _ hf.base_open.inj, set.top_eq_univ] }, refine ⟨_, _, e, rfl, _⟩, rintros H hX s hs ⟨_, x, rfl⟩, unfreezingI { haveI := is_reduced_of_open_immersion f }, specialize H (f.1.c.app _ s) _ ⟨x, by { change x ∈ (f.val.base) ⁻¹' _, rw e, trivial }⟩, { rw [← Scheme.preimage_basic_open, hs], ext1, simp [opens.map] }, { erw ← PresheafedSpace.stalk_map_germ_apply f.1 ⟨_,_⟩ ⟨x,_⟩ at H, apply_fun (inv $ PresheafedSpace.stalk_map f.val x) at H, erw [category_theory.is_iso.hom_inv_id_apply, map_zero] at H, exact H } }, { intros R hX s hs x, erw [basic_open_eq_of_affine', prime_spectrum.basic_open_eq_bot_iff] at hs, replace hs := (hs.map (Spec_Ξ“_identity.app R).inv), -- what the hell?! replace hs := @is_nilpotent.eq_zero _ _ _ _ (show _, from _) hs, rw iso.hom_inv_id_apply at hs, rw [hs, map_zero], exact @@is_reduced.component_reduced hX ⊀ } end @[simp] lemma basic_open_eq_bot_iff {X : Scheme} [is_reduced X] {U : opens X.carrier} (s : X.presheaf.obj $ op U) : X.basic_open s = βŠ₯ ↔ s = 0 := begin refine ⟨eq_zero_of_basic_open_eq_bot s, _⟩, rintro rfl, simp, end /-- A scheme `X` is integral if its carrier is nonempty, and `π’ͺβ‚“(U)` is an integral domain for each `U β‰  βˆ…`. -/ class is_integral : Prop := (nonempty : nonempty X.carrier . tactic.apply_instance) (component_integral : βˆ€ (U : opens X.carrier) [_root_.nonempty U], is_domain (X.presheaf.obj (op U)) . tactic.apply_instance) attribute [instance] is_integral.component_integral is_integral.nonempty instance [h : is_integral X] : is_domain (X.presheaf.obj (op ⊀)) := @@is_integral.component_integral _ _ (by simp) @[priority 900] instance is_reduced_of_is_integral [is_integral X] : is_reduced X := begin constructor, intro U, cases U.1.eq_empty_or_nonempty, { have : U = βˆ… := subtype.eq h, haveI := CommRing.subsingleton_of_is_terminal (X.sheaf.is_terminal_of_eq_empty this), change _root_.is_reduced (X.sheaf.val.obj (op U)), apply_instance }, { haveI : nonempty U := by simpa, apply_instance } end instance is_irreducible_of_is_integral [is_integral X] : irreducible_space X.carrier := begin by_contradiction H, replace H : Β¬ is_preirreducible (⊀ : set X.carrier) := Ξ» h, H { to_preirreducible_space := ⟨h⟩, to_nonempty := infer_instance }, simp_rw [is_preirreducible_iff_closed_union_closed, not_forall, not_or_distrib] at H, rcases H with ⟨S, T, hS, hT, h₁, hβ‚‚, hβ‚ƒβŸ©, erw not_forall at hβ‚‚ h₃, simp_rw not_forall at hβ‚‚ h₃, haveI : nonempty (⟨Sᢜ, hS.1⟩ : opens X.carrier) := ⟨⟨_, hβ‚‚.some_spec.some_spec⟩⟩, haveI : nonempty (⟨Tᢜ, hT.1⟩ : opens X.carrier) := ⟨⟨_, h₃.some_spec.some_spec⟩⟩, haveI : nonempty (⟨Sᢜ, hS.1⟩ βŠ” ⟨Tᢜ, hT.1⟩ : opens X.carrier) := ⟨⟨_, or.inl hβ‚‚.some_spec.some_spec⟩⟩, let e : X.presheaf.obj _ β‰… CommRing.of _ := (X.sheaf.is_product_of_disjoint ⟨_, hS.1⟩ ⟨_, hT.1⟩ _) .cone_point_unique_up_to_iso (CommRing.prod_fan_is_limit _ _), apply_with false_of_nontrivial_of_product_domain { instances := ff }, { exact e.symm.CommRing_iso_to_ring_equiv.is_domain _ }, { apply X.to_LocallyRingedSpace.component_nontrivial }, { apply X.to_LocallyRingedSpace.component_nontrivial }, { ext x, split, { rintros ⟨hS,hT⟩, cases h₁ (show x ∈ ⊀, by trivial), exacts [hS h, hT h] }, { intro x, exact x.rec _ } } end lemma is_integral_of_is_irreducible_is_reduced [is_reduced X] [H : irreducible_space X.carrier] : is_integral X := begin split, intros U hU, haveI := (@@LocallyRingedSpace.component_nontrivial X.to_LocallyRingedSpace U hU).1, haveI : no_zero_divisors (X.to_LocallyRingedSpace.to_SheafedSpace.to_PresheafedSpace.presheaf.obj (op U)), { refine ⟨λ a b e, _⟩, simp_rw [← basic_open_eq_bot_iff, ← opens.not_nonempty_iff_eq_bot], by_contra' h, obtain ⟨_, ⟨x, hx₁, rfl⟩, ⟨x, hxβ‚‚, e'⟩⟩ := @@nonempty_preirreducible_inter _ H.1 (X.basic_open a).2 (X.basic_open b).2 h.1 h.2, replace e' := subtype.eq e', subst e', replace e := congr_arg (X.presheaf.germ x) e, rw [ring_hom.map_mul, ring_hom.map_zero] at e, refine zero_ne_one' (X.presheaf.stalk x.1) (is_unit_zero_iff.1 _), convert hx₁.mul hxβ‚‚, exact e.symm }, exact no_zero_divisors.to_is_domain _ end lemma is_integral_iff_is_irreducible_and_is_reduced : is_integral X ↔ irreducible_space X.carrier ∧ is_reduced X := ⟨λ _, by exactI ⟨infer_instance, infer_instance⟩, Ξ» ⟨_, _⟩, by exactI is_integral_of_is_irreducible_is_reduced X⟩ lemma is_integral_of_open_immersion {X Y : Scheme} (f : X ⟢ Y) [H : is_open_immersion f] [is_integral Y] [nonempty X.carrier] : is_integral X := begin constructor, intros U hU, have : U = (opens.map f.1.base).obj (H.base_open.is_open_map.functor.obj U), { ext1, exact (set.preimage_image_eq _ H.base_open.inj).symm }, rw this, haveI : is_domain (Y.presheaf.obj (op (H.base_open.is_open_map.functor.obj U))), { apply_with is_integral.component_integral { instances := ff }, apply_instance, refine ⟨⟨_, _, hU.some.prop, rfl⟩⟩ }, exact (as_iso $ f.1.c.app (op $ H.base_open.is_open_map.functor.obj U) : Y.presheaf.obj _ β‰… _).symm.CommRing_iso_to_ring_equiv.is_domain _ end instance {R : CommRing} [H : is_domain R] : is_integral (Scheme.Spec.obj $ op R) := begin apply_with is_integral_of_is_irreducible_is_reduced { instances := ff }, { apply_instance }, { dsimp [Spec.Top_obj], apply_instance }, end lemma affine_is_integral_iff (R : CommRing) : is_integral (Scheme.Spec.obj $ op R) ↔ is_domain R := ⟨λ h, by exactI ring_equiv.is_domain ((Scheme.Spec.obj $ op R).presheaf.obj _) (as_iso $ to_Spec_Ξ“ R).CommRing_iso_to_ring_equiv, Ξ» h, by exactI infer_instance⟩ lemma is_integral_of_is_affine_is_domain [is_affine X] [nonempty X.carrier] [h : is_domain (X.presheaf.obj (op ⊀))] : is_integral X := begin haveI : is_integral (Scheme.Spec.obj (op (Scheme.Ξ“.obj (op X)))), { rw affine_is_integral_iff, exact h }, exact is_integral_of_open_immersion X.iso_Spec.hom, end lemma map_injective_of_is_integral [is_integral X] {U V : opens X.carrier} (i : U ⟢ V) [H : nonempty U] : function.injective (X.presheaf.map i.op) := begin rw injective_iff_map_eq_zero, intros x hx, rw ← basic_open_eq_bot_iff at ⊒ hx, rw Scheme.basic_open_res at hx, revert hx, contrapose!, simp_rw [← opens.not_nonempty_iff_eq_bot, not_not], apply nonempty_preirreducible_inter U.prop (RingedSpace.basic_open _ _).prop, simpa using H end end algebraic_geometry
f6b0882dc8de690aa1ca7b7020351df42ccd41b4
b074a51e20fdb737b2d4c635dd292fc54685e010
/src/algebraic_geometry/presheafed_space.lean
d9ec637ce1bd410df0ff9ddbbf9ef1d3f755f376
[ "Apache-2.0" ]
permissive
minchaowu/mathlib
2daf6ffdb5a56eeca403e894af88bcaaf65aec5e
879da1cf04c2baa9eaa7bd2472100bc0335e5c73
refs/heads/master
1,609,628,676,768
1,564,310,105,000
1,564,310,105,000
99,461,307
0
0
null
null
null
null
UTF-8
Lean
false
false
7,589
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 topology.Top.presheaf /-! # Presheafed spaces Introduces the category of topological spaces equipped with a presheaf (taking values in an arbitrary target category `C`.) We further describe how to apply functors and natural transformations to the values of the presheaves. -/ universes v u open category_theory open Top open topological_space open opposite open category_theory.category category_theory.functor variables (C : Type u) [π’ž : category.{v+1} C] include π’ž local attribute [tidy] tactic.op_induction' namespace algebraic_geometry /-- A `PresheafedSpace C` is a topological space equipped with a presheaf of `C`s. -/ structure PresheafedSpace := (to_Top : Top.{v}) (π’ͺ : to_Top.presheaf C) variables {C} namespace PresheafedSpace instance coe_to_Top : has_coe (PresheafedSpace.{v} C) Top := { coe := Ξ» X, X.to_Top } @[simp] lemma as_coe (X : PresheafedSpace.{v} C) : X.to_Top = (X : Top.{v}) := rfl @[simp] lemma mk_coe (to_Top) (π’ͺ) : (({ to_Top := to_Top, π’ͺ := π’ͺ } : PresheafedSpace.{v} C) : Top.{v}) = to_Top := rfl instance (X : PresheafedSpace.{v} C) : topological_space X := X.to_Top.str /-- A morphism between presheafed spaces `X` and `Y` consists of a continuous map `f` between the underlying topological spaces, and a (notice contravariant!) map from the presheaf on `Y` to the pushforward of the presheaf on `X` via `f`. -/ structure hom (X Y : PresheafedSpace.{v} C) := (f : (X : Top.{v}) ⟢ (Y : Top.{v})) (c : Y.π’ͺ ⟢ f _* X.π’ͺ) @[extensionality] lemma ext {X Y : PresheafedSpace.{v} C} (Ξ± Ξ² : hom X Y) (w : Ξ±.f = Ξ².f) (h : Ξ±.c ≫ (whisker_right (nat_trans.op (opens.map_iso _ _ w).inv) X.π’ͺ) = Ξ².c) : Ξ± = Ξ² := begin cases Ξ±, cases Ξ², dsimp [presheaf.pushforward] at *, tidy, -- TODO including `injections` would make tidy work earlier. end . def id (X : PresheafedSpace.{v} C) : hom X X := { f := πŸ™ (X : Top.{v}), c := ((functor.left_unitor _).inv) ≫ (whisker_right (nat_trans.op (opens.map_id _).hom) _) } def comp (X Y Z : PresheafedSpace.{v} C) (Ξ± : hom X Y) (Ξ² : hom Y Z) : hom X Z := { f := Ξ±.f ≫ Ξ².f, c := Ξ².c ≫ (whisker_left (opens.map Ξ².f).op Ξ±.c) } variables (C) section local attribute [simp] id comp presheaf.pushforward /- The proofs below can be done by `tidy`, but it is too slow, and we don't have a tactic caching mechanism. -/ /-- The category of PresheafedSpaces. Morphisms are pairs, a continuous map and a presheaf map from the presheaf on the target to the pushforward of the presheaf on the source. -/ instance category_of_PresheafedSpaces : category (PresheafedSpace.{v} C) := { hom := hom, id := id, comp := comp, id_comp' := Ξ» X Y f, begin ext1, swap, { dsimp, simp only [id_comp] }, { ext1 U, op_induction, cases U, dsimp, simp only [comp_id, map_id] }, end, comp_id' := Ξ» X Y f, begin ext1, swap, { dsimp, simp only [comp_id] }, { ext1 U, op_induction, cases U, dsimp, simp only [comp_id, id_comp, map_id] } end, assoc' := Ξ» W X Y Z f g h, begin simp only [true_and, presheaf.pushforward, id, comp, whisker_left_twice, whisker_left_comp, heq_iff_eq, category.assoc], split; refl end } end variables {C} instance {X Y : PresheafedSpace.{v} C} : has_coe (X ⟢ Y) (X.to_Top ⟢ Y.to_Top) := { coe := Ξ» Ξ±, Ξ±.f } @[simp] lemma hom_mk_coe {X Y : PresheafedSpace.{v} C} (f) (c) : (({ f := f, c := c } : X ⟢ Y) : (X : Top.{v}) ⟢ (Y : Top.{v})) = f := rfl @[simp] lemma f_as_coe {X Y : PresheafedSpace.{v} C} (Ξ± : X ⟢ Y) : Ξ±.f = (Ξ± : (X : Top.{v}) ⟢ (Y : Top.{v})) := rfl @[simp] lemma id_coe (X : PresheafedSpace.{v} C) : (((πŸ™ X) : X ⟢ X) : (X : Top.{v}) ⟢ X) = πŸ™ (X : Top.{v}) := rfl @[simp] lemma comp_coe {X Y Z : PresheafedSpace.{v} C} (Ξ± : X ⟢ Y) (Ξ² : Y ⟢ Z) : ((Ξ± ≫ Ξ² : X ⟢ Z) : (X : Top.{v}) ⟢ Z) = (Ξ± : (X : Top.{v}) ⟢ Y) ≫ (Ξ² : Y ⟢ Z) := rfl lemma id_c (X : PresheafedSpace.{v} C) : ((πŸ™ X) : X ⟢ X).c = (((functor.left_unitor _).inv) ≫ (whisker_right (nat_trans.op (opens.map_id _).hom) _)) := rfl -- Implementation note: this harmless looking lemma causes deterministic timeouts, -- but happily we can survive without it. -- lemma comp_c {X Y Z : PresheafedSpace.{v} C} (Ξ± : X ⟢ Y) (Ξ² : Y ⟢ Z) : -- (Ξ± ≫ Ξ²).c = (Ξ².c ≫ (whisker_left (opens.map Ξ².f).op Ξ±.c)) := rfl @[simp] lemma id_c_app (X : PresheafedSpace.{v} C) (U) : ((πŸ™ X) : X ⟢ X).c.app U = eq_to_hom (by { op_induction U, cases U, refl }) := by { op_induction U, cases U, simp only [id_c], dsimp, simp, } @[simp] lemma comp_c_app {X Y Z : PresheafedSpace.{v} C} (Ξ± : X ⟢ Y) (Ξ² : Y ⟢ Z) (U) : (Ξ± ≫ Ξ²).c.app U = (Ξ².c).app U ≫ (Ξ±.c).app (op ((opens.map (Ξ².f)).obj (unop U))) := rfl /-- The forgetful functor from `PresheafedSpace` to `Top`. -/ def forget : PresheafedSpace.{v} C β₯€ Top := { obj := Ξ» X, (X : Top.{v}), map := Ξ» X Y f, f } end PresheafedSpace end algebraic_geometry open algebraic_geometry algebraic_geometry.PresheafedSpace variables {C} namespace category_theory variables {D : Type u} [π’Ÿ : category.{v+1} D] include π’Ÿ local attribute [simp] presheaf.pushforward namespace functor /-- We can apply a functor `F : C β₯€ D` to the values of the presheaf in any `PresheafedSpace C`, giving a functor `PresheafedSpace C β₯€ PresheafedSpace D` -/ /- The proofs below can be done by `tidy`, but it is too slow, and we don't have a tactic caching mechanism. -/ def map_presheaf (F : C β₯€ D) : PresheafedSpace.{v} C β₯€ PresheafedSpace.{v} D := { obj := Ξ» X, { to_Top := X.to_Top, π’ͺ := X.π’ͺ β‹™ F }, map := Ξ» X Y f, { f := f.f, c := whisker_right f.c F }, map_id' := Ξ» X, begin ext1, swap, { refl }, { ext1, dsimp, simp only [presheaf.pushforward, eq_to_hom_map, map_id, comp_id, id_c_app], refl } end, map_comp' := Ξ» X Y Z f g, begin ext1, swap, { refl, }, { ext, dsimp, simp only [comp_id, assoc, map_comp, map_id], }, end } @[simp] lemma map_presheaf_obj_X (F : C β₯€ D) (X : PresheafedSpace.{v} C) : ((F.map_presheaf.obj X) : Top.{v}) = (X : Top.{v}) := rfl @[simp] lemma map_presheaf_obj_π’ͺ (F : C β₯€ D) (X : PresheafedSpace.{v} C) : (F.map_presheaf.obj X).π’ͺ = X.π’ͺ β‹™ F := rfl @[simp] lemma map_presheaf_map_f (F : C β₯€ D) {X Y : PresheafedSpace.{v} C} (f : X ⟢ Y) : ((F.map_presheaf.map f) : (X : Top.{v}) ⟢ (Y : Top.{v})) = f := rfl @[simp] lemma map_presheaf_map_c (F : C β₯€ D) {X Y : PresheafedSpace.{v} C} (f : X ⟢ Y) : (F.map_presheaf.map f).c = whisker_right f.c F := rfl end functor namespace nat_trans /- The proofs below can be done by `tidy`, but it is too slow, and we don't have a tactic caching mechanism. -/ def on_presheaf {F G : C β₯€ D} (Ξ± : F ⟢ G) : G.map_presheaf ⟢ F.map_presheaf := { app := Ξ» X, { f := πŸ™ _, c := whisker_left X.π’ͺ Ξ± ≫ ((functor.left_unitor _).inv) ≫ (whisker_right (nat_trans.op (opens.map_id _).hom) _) }, naturality' := Ξ» X Y f, begin ext1, swap, { refl }, { ext1 U, op_induction, cases U, dsimp, simp only [comp_id, assoc, map_id, nat_trans.naturality] } end } -- TODO Assemble the last two constructions into a functor -- `(C β₯€ D) β₯€ (PresheafedSpace C β₯€ PresheafedSpace D)` end nat_trans end category_theory
437e5c30df865c45dc6e4203cef2b6b71328c156
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/linear_algebra/char_poly/basic_auto.lean
8d655b35de28ffaaff3011876ec0ad001ac7ad6a
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
2,591
lean
/- Copyright (c) 2020 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.tactic.apply_fun import Mathlib.ring_theory.matrix_algebra import Mathlib.ring_theory.polynomial_algebra import Mathlib.linear_algebra.nonsingular_inverse import Mathlib.tactic.squeeze import Mathlib.PostPort universes u w namespace Mathlib /-! # Characteristic polynomials and the Cayley-Hamilton theorem We define characteristic polynomials of matrices and prove the Cayley–Hamilton theorem over arbitrary commutative rings. ## Main definitions * `char_poly` is the characteristic polynomial of a matrix. ## Implementation details We follow a nice proof from http://drorbn.net/AcademicPensieve/2015-12/CayleyHamilton.pdf -/ /-- The "characteristic matrix" of `M : matrix n n R` is the matrix of polynomials $t I - M$. The determinant of this matrix is the characteristic polynomial. -/ def char_matrix {R : Type u} [comm_ring R] {n : Type w} [DecidableEq n] [fintype n] (M : matrix n n R) : matrix n n (polynomial R) := coe_fn (matrix.scalar n) polynomial.X - coe_fn (ring_hom.map_matrix polynomial.C) M @[simp] theorem char_matrix_apply_eq {R : Type u} [comm_ring R] {n : Type w} [DecidableEq n] [fintype n] (M : matrix n n R) (i : n) : char_matrix M i i = polynomial.X - coe_fn polynomial.C (M i i) := sorry @[simp] theorem char_matrix_apply_ne {R : Type u} [comm_ring R] {n : Type w} [DecidableEq n] [fintype n] (M : matrix n n R) (i : n) (j : n) (h : i β‰  j) : char_matrix M i j = -coe_fn polynomial.C (M i j) := sorry theorem mat_poly_equiv_char_matrix {R : Type u} [comm_ring R] {n : Type w} [DecidableEq n] [fintype n] (M : matrix n n R) : coe_fn mat_poly_equiv (char_matrix M) = polynomial.X - coe_fn polynomial.C M := sorry /-- The characteristic polynomial of a matrix `M` is given by $\det (t I - M)$. -/ def char_poly {R : Type u} [comm_ring R] {n : Type w} [DecidableEq n] [fintype n] (M : matrix n n R) : polynomial R := matrix.det (char_matrix M) /-- The Cayley-Hamilton theorem, that the characteristic polynomial of a matrix, applied to the matrix itself, is zero. This holds over any commutative ring. -/ -- This proof follows http://drorbn.net/AcademicPensieve/2015-12/CayleyHamilton.pdf theorem aeval_self_char_poly {R : Type u} [comm_ring R] {n : Type w} [DecidableEq n] [fintype n] (M : matrix n n R) : coe_fn (polynomial.aeval M) (char_poly M) = 0 := sorry end Mathlib
f38a159f2d2141ae63236255c8659ea79bb4ac38
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/tests/lean/run/elab_meta1.lean
f63260d440a57aefe95fd8c70649d5bbef86470f
[ "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
230
lean
meta_definition f : nat β†’ nat | n := if n / 2 = 0 then n + 1 else f (n / 2) + 1 meta_definition g : nat Γ— nat β†’ nat | (0, b) := b | (a+1, b+1) := g (a/2 - 1, a + b) | (a+1, 0) := 2*a vm_eval f 200 vm_eval g (10, 20)
dfd3302adfa311553a34021eeafb22e1c601e66c
e00ea76a720126cf9f6d732ad6216b5b824d20a7
/src/ring_theory/integral_closure.lean
5a4ab2ec6d0e0dcaef5353aebc3fa08ab5eea3b9
[ "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
17,400
lean
/- Copyright (c) 2019 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau Integral closure of a subring. -/ import ring_theory.adjoin linear_algebra.finsupp universes u v open_locale classical open polynomial submodule section variables (R : Type u) {A : Type v} variables [comm_ring R] [comm_ring A] variables [algebra R A] /-- An element `x` of an algebra `A` over a commutative ring `R` is said to be *integral*, if it is a root of some monic polynomial `p : polynomial R`. -/ def is_integral (x : A) : Prop := βˆƒ p : polynomial R, monic p ∧ aeval R A x p = 0 variables {R} theorem is_integral_algebra_map {x : R} : is_integral R (algebra_map A x) := ⟨X - C x, monic_X_sub_C _, by rw [alg_hom.map_sub, aeval_def, aeval_def, evalβ‚‚_X, evalβ‚‚_C, sub_self]⟩ theorem is_integral_of_subring {x : A} (T : set R) [is_subring T] (hx : is_integral T (algebra.comap.to_comap T R A x)) : is_integral R (x : A) := let ⟨p, hpm, hpx⟩ := hx in ⟨⟨p.support, Ξ» n, (p.to_fun n).1, Ξ» n, finsupp.mem_support_iff.trans (not_iff_not_of_iff ⟨λ H, have _ := congr_arg subtype.val H, this, Ξ» H, subtype.eq H⟩)⟩, have _ := congr_arg subtype.val hpm, this, hpx⟩ theorem is_integral_iff_is_integral_closure_finite {r : A} : is_integral R r ↔ βˆƒ s : set R, s.finite ∧ is_integral (ring.closure s) (algebra.comap.to_comap (ring.closure s) R A r) := begin split; intro hr, { rcases hr with ⟨p, hmp, hpr⟩, exact ⟨_, set.finite_mem_finset _, p.restriction, subtype.eq hmp, hpr⟩ }, rcases hr with ⟨s, hs, hsr⟩, exact is_integral_of_subring _ hsr end theorem fg_adjoin_singleton_of_integral (x : A) (hx : is_integral R x) : (algebra.adjoin R ({x} : set A) : submodule R A).fg := begin rcases hx with ⟨f, hfm, hfx⟩, existsi finset.image ((^) x) (finset.range (nat_degree f + 1)), apply le_antisymm, { rw span_le, intros s hs, rw finset.mem_coe at hs, rcases finset.mem_image.1 hs with ⟨k, hk, rfl⟩, clear hk, exact is_submonoid.pow_mem (algebra.subset_adjoin (set.mem_singleton _)) }, intros r hr, change r ∈ algebra.adjoin R ({x} : set A) at hr, rw algebra.adjoin_singleton_eq_range at hr, rcases hr with ⟨p, rfl⟩, rw ← mod_by_monic_add_div p hfm, rw [alg_hom.map_add, alg_hom.map_mul, hfx, zero_mul, add_zero], have : degree (p %β‚˜ f) ≀ degree f := degree_mod_by_monic_le p hfm, generalize_hyp : p %β‚˜ f = q at this ⊒, rw [← sum_C_mul_X_eq q, aeval_def, evalβ‚‚_sum, finsupp.sum, mem_coe], refine sum_mem _ (Ξ» k hkq, _), rw [evalβ‚‚_mul, evalβ‚‚_C, evalβ‚‚_pow, evalβ‚‚_X, ← algebra.smul_def], refine smul_mem _ _ (subset_span _), rw finset.mem_coe, refine finset.mem_image.2 ⟨_, _, rfl⟩, rw [finset.mem_range, nat.lt_succ_iff], refine le_of_not_lt (Ξ» hk, _), rw [degree_le_iff_coeff_zero] at this, rw [finsupp.mem_support_iff] at hkq, apply hkq, apply this, exact lt_of_le_of_lt degree_le_nat_degree (with_bot.coe_lt_coe.2 hk) end theorem fg_adjoin_of_finite {s : set A} (hfs : s.finite) (his : βˆ€ x ∈ s, is_integral R x) : (algebra.adjoin R s : submodule R A).fg := set.finite.induction_on hfs (Ξ» _, ⟨finset.singleton 1, le_antisymm (span_le.2 $ set.singleton_subset_iff.2 $ is_submonoid.one_mem _) begin change ring.closure _ βŠ† _, simp only [set.union_empty, finset.coe_singleton, span_singleton_eq_range, algebra.smul_def, mul_one], -- TODO drop the next `rw` once `algebra_map` will be a bundled `ring_hom` rw [← ring_hom.coe_of (algebra_map A)]; try { apply_instance }, exact ring.closure_subset (set.subset.refl _) end⟩) (Ξ» a s has hs ih his, by rw [← set.union_singleton, algebra.adjoin_union_coe_submodule]; exact fg_mul _ _ (ih $ Ξ» i hi, his i $ set.mem_insert_of_mem a hi) (fg_adjoin_singleton_of_integral _ $ his a $ set.mem_insert a s)) his theorem is_integral_of_noetherian' (H : is_noetherian R A) (x : A) : is_integral R x := begin let leval : @linear_map R (polynomial R) A _ _ _ _ _ := (aeval R A x).to_linear_map, let D : β„• β†’ submodule R A := Ξ» n, (degree_le R n).map leval, let M := well_founded.min (is_noetherian_iff_well_founded.1 H) (set.range D) ⟨_, ⟨0, rfl⟩⟩, have HM : M ∈ set.range D := well_founded.min_mem _ _ _, cases HM with N HN, have HM : Β¬M < D (N+1) := well_founded.not_lt_min (is_noetherian_iff_well_founded.1 H) (set.range D) _ ⟨N+1, rfl⟩, rw ← HN at HM, have HN2 : D (N+1) ≀ D N := classical.by_contradiction (Ξ» H, HM (lt_of_le_not_le (map_mono (degree_le_mono (with_bot.coe_le_coe.2 (nat.le_succ N)))) H)), have HN3 : leval (X^(N+1)) ∈ D N, { exact HN2 (mem_map_of_mem (mem_degree_le.2 (degree_X_pow_le _))) }, rcases HN3 with ⟨p, hdp, hpe⟩, refine ⟨X^(N+1) - p, monic_X_pow_sub (mem_degree_le.1 hdp), _⟩, show leval (X ^ (N + 1) - p) = 0, rw [linear_map.map_sub, hpe, sub_self] end theorem is_integral_of_noetherian (S : subalgebra R A) (H : is_noetherian R (S : submodule R A)) (x : A) (hx : x ∈ S) : is_integral R x := begin letI : algebra R S := S.algebra, letI : comm_ring S := S.comm_ring R A, suffices : is_integral R (⟨x, hx⟩ : S), { rcases this with ⟨p, hpm, hpx⟩, replace hpx := congr_arg subtype.val hpx, refine ⟨p, hpm, eq.trans _ hpx⟩, simp only [aeval_def, evalβ‚‚, finsupp.sum], rw ← p.support.sum_hom subtype.val, { refine finset.sum_congr rfl (Ξ» n hn, _), change _ = _ * _, rw is_semiring_hom.map_pow coe, refl, split; intros; refl }, refine { map_add := _, map_zero := _ }; intros; refl }, refine is_integral_of_noetherian' H ⟨x, hx⟩ end set_option class.instance_max_depth 100 theorem is_integral_of_mem_of_fg (S : subalgebra R A) (HS : (S : submodule R A).fg) (x : A) (hx : x ∈ S) : is_integral R x := begin cases HS with y hy, obtain ⟨lx, hlx1, hlx2⟩ : βˆƒ (l : A β†’β‚€ R) (H : l ∈ finsupp.supported R R ↑y), (finsupp.total A A R id) l = x, { rwa [←(@finsupp.mem_span_iff_total A A R _ _ _ id ↑y x), set.image_id ↑y, hy] }, have : βˆ€ (jk : (↑(y.product y) : set (A Γ— A))), jk.1.1 * jk.1.2 ∈ (span R ↑y : submodule R A), { intros jk, let j : β†₯(↑y : set A) := ⟨jk.1.1, (finset.mem_product.1 jk.2).1⟩, let k : β†₯(↑y : set A) := ⟨jk.1.2, (finset.mem_product.1 jk.2).2⟩, have hj : j.1 ∈ (span R ↑y : submodule R A) := subset_span j.2, have hk : k.1 ∈ (span R ↑y : submodule R A) := subset_span k.2, revert hj hk, rw hy, exact @is_submonoid.mul_mem A _ S _ j.1 k.1 }, rw ← set.image_id ↑y at this, simp only [finsupp.mem_span_iff_total] at this, choose ly hly1 hly2, let Sβ‚€' : finset R := lx.frange βˆͺ finset.bind finset.univ (finsupp.frange ∘ ly), let Sβ‚€ : set R := ring.closure ↑Sβ‚€', refine is_integral_of_subring (ring.closure ↑Sβ‚€') _, letI : algebra Sβ‚€ (algebra.comap Sβ‚€ R A) := algebra.comap.algebra _ _ _, letI hmod : module Sβ‚€ (algebra.comap Sβ‚€ R A) := algebra.to_module, have : (span Sβ‚€ (insert 1 (↑y:set A) : set (algebra.comap Sβ‚€ R A)) : submodule Sβ‚€ (algebra.comap Sβ‚€ R A)) = (algebra.adjoin Sβ‚€ ((↑y : set A) : set (algebra.comap Sβ‚€ R A)) : subalgebra Sβ‚€ (algebra.comap Sβ‚€ R A)), { apply le_antisymm, { rw [span_le, set.insert_subset, mem_coe], split, change _ ∈ ring.closure _, exact is_submonoid.one_mem _, exact algebra.subset_adjoin }, rw [algebra.adjoin_eq_span, span_le], intros r hr, refine monoid.in_closure.rec_on hr _ _ _, { intros r hr, exact subset_span (set.mem_insert_of_mem _ hr) }, { exact subset_span (set.mem_insert _ _) }, intros r1 r2 hr1 hr2 ih1 ih2, rw ← set.image_id (insert _ ↑y) at ih1 ih2, simp only [mem_coe, finsupp.mem_span_iff_total] at ih1 ih2, have ih1' := ih1, have ih2' := ih2, rcases ih1' with ⟨l1, hl1, rfl⟩, rcases ih2' with ⟨l2, hl2, rfl⟩, simp only [finsupp.total_apply, finsupp.sum_mul, finsupp.mul_sum, mem_coe], rw [finsupp.sum], refine sum_mem _ _, intros r2 hr2, rw [finsupp.sum], refine sum_mem _ _, intros r1 hr1, rw [algebra.mul_smul_comm, algebra.smul_mul_assoc], letI : module β†₯Sβ‚€ A := hmod, refine smul_mem _ _ (smul_mem _ _ _), rcases hl1 hr1 with rfl | hr1, { change 1 * r2 ∈ _, rw one_mul r2, exact subset_span (hl2 hr2) }, rcases hl2 hr2 with rfl | hr2, { change r1 * 1 ∈ _, rw mul_one, exact subset_span (set.mem_insert_of_mem _ hr1) }, let jk : β†₯(↑(finset.product y y) : set (A Γ— A)) := ⟨(r1, r2), finset.mem_product.2 ⟨hr1, hr2⟩⟩, specialize hly2 jk, change _ = r1 * r2 at hly2, rw [id, id, ← hly2, finsupp.total_apply], rw [finsupp.sum], refine sum_mem _ _, intros z hz, have : ly jk z ∈ Sβ‚€, { apply ring.subset_closure, apply finset.mem_union_right, apply finset.mem_bind.2, exact ⟨jk, finset.mem_univ _, by convert finset.mem_image_of_mem _ hz⟩ }, change @has_scalar.smul Sβ‚€ (algebra.comap Sβ‚€ R A) hmod.to_has_scalar ⟨ly jk z, this⟩ z ∈ _, exact smul_mem _ _ (subset_span (set.mem_insert_of_mem _ (hly1 _ hz))) }, haveI : is_noetherian_ring β†₯Sβ‚€ := by { convert is_noetherian_ring_closure _ (finset.finite_to_set _), apply_instance }, apply is_integral_of_noetherian (algebra.adjoin Sβ‚€ ((↑y : set A) : set (algebra.comap Sβ‚€ R A)) : subalgebra Sβ‚€ (algebra.comap Sβ‚€ R A)) (is_noetherian_of_fg_of_noetherian _ ⟨insert 1 y, by rw finset.coe_insert; convert this⟩), show x ∈ ((algebra.adjoin Sβ‚€ ((↑y : set A) : set (algebra.comap Sβ‚€ R A)) : subalgebra Sβ‚€ (algebra.comap Sβ‚€ R A)) : submodule Sβ‚€ (algebra.comap Sβ‚€ R A)), rw [← hlx2, finsupp.total_apply, finsupp.sum], refine sum_mem _ _, intros r hr, rw ← this, have : lx r ∈ ring.closure ↑Sβ‚€' := ring.subset_closure (finset.mem_union_left _ (by convert finset.mem_image_of_mem _ hr)), change @has_scalar.smul Sβ‚€ (algebra.comap Sβ‚€ R A) hmod.to_has_scalar ⟨lx r, this⟩ r ∈ _, rw finsupp.mem_supported at hlx1, exact smul_mem _ _ (subset_span (set.mem_insert_of_mem _ (hlx1 hr))), end theorem is_integral_of_mem_closure {x y z : A} (hx : is_integral R x) (hy : is_integral R y) (hz : z ∈ ring.closure ({x, y} : set A)) : is_integral R z := begin have := fg_mul _ _ (fg_adjoin_singleton_of_integral x hx) (fg_adjoin_singleton_of_integral y hy), rw [← algebra.adjoin_union_coe_submodule, set.union_singleton] at this, exact is_integral_of_mem_of_fg (algebra.adjoin R {x, y}) this z (ring.closure_mono (set.subset_union_right _ _) hz) end theorem is_integral_zero : is_integral R (0:A) := algebra.map_zero R A β–Έ is_integral_algebra_map theorem is_integral_one : is_integral R (1:A) := algebra.map_one R A β–Έ is_integral_algebra_map theorem is_integral_add {x y : A} (hx : is_integral R x) (hy : is_integral R y) : is_integral R (x + y) := is_integral_of_mem_closure hx hy (is_add_submonoid.add_mem (ring.subset_closure (or.inr (or.inl rfl))) (ring.subset_closure (or.inl rfl))) theorem is_integral_neg {x : A} (hx : is_integral R x) : is_integral R (-x) := is_integral_of_mem_closure hx hx (is_add_subgroup.neg_mem (ring.subset_closure (or.inl rfl))) theorem is_integral_sub {x y : A} (hx : is_integral R x) (hy : is_integral R y) : is_integral R (x - y) := is_integral_add hx (is_integral_neg hy) theorem is_integral_mul {x y : A} (hx : is_integral R x) (hy : is_integral R y) : is_integral R (x * y) := is_integral_of_mem_closure hx hy (is_submonoid.mul_mem (ring.subset_closure (or.inr (or.inl rfl))) (ring.subset_closure (or.inl rfl))) variables (R A) def integral_closure : subalgebra R A := { carrier := { r | is_integral R r }, subring := { zero_mem := is_integral_zero, one_mem := is_integral_one, add_mem := Ξ» _ _, is_integral_add, neg_mem := Ξ» _, is_integral_neg, mul_mem := Ξ» _ _, is_integral_mul }, range_le' := Ξ» y ⟨x, hx⟩, hx β–Έ is_integral_algebra_map } theorem mem_integral_closure_iff_mem_fg {r : A} : r ∈ integral_closure R A ↔ βˆƒ M : subalgebra R A, (M : submodule R A).fg ∧ r ∈ M := ⟨λ hr, ⟨algebra.adjoin R {r}, fg_adjoin_singleton_of_integral _ hr, algebra.subset_adjoin (or.inl rfl)⟩, Ξ» ⟨M, Hf, hrM⟩, is_integral_of_mem_of_fg M Hf _ hrM⟩ theorem integral_closure_idem : integral_closure (integral_closure R A : set A) A = βŠ₯ := begin rw eq_bot_iff, intros r hr, rcases is_integral_iff_is_integral_closure_finite.1 hr with ⟨s, hfs, hr⟩, apply algebra.mem_bot.2, refine ⟨⟨_, _⟩, rfl⟩, refine (mem_integral_closure_iff_mem_fg _ _).2 ⟨algebra.adjoin _ (subtype.val '' s βˆͺ {r}), algebra.fg_trans (fg_adjoin_of_finite (set.finite_image _ hfs) (Ξ» y ⟨x, hx, hxy⟩, hxy β–Έ x.2)) _, algebra.subset_adjoin (or.inr (or.inl rfl))⟩, refine fg_adjoin_singleton_of_integral _ _, rcases hr with ⟨p, hmp, hpx⟩, refine ⟨to_subring (of_subring _ (of_subring _ p)) _ _, _, hpx⟩, { intros x hx, rcases finsupp.mem_frange.1 hx with ⟨h1, n, rfl⟩, change (coeff p n).1.1 ∈ ring.closure _, rcases ring.exists_list_of_mem_closure (coeff p n).2 with ⟨L, HL1, HL2⟩, rw ← HL2, clear HL2 hfs h1 hx n hmp hpx hr r p, induction L with hd tl ih, { exact is_add_submonoid.zero_mem _ }, rw list.forall_mem_cons at HL1, rw [list.map_cons, list.sum_cons], refine is_add_submonoid.add_mem _ (ih HL1.2), cases HL1 with HL HL', clear HL' ih tl, induction hd with hd tl ih, { exact is_submonoid.one_mem _ }, rw list.forall_mem_cons at HL, rw list.prod_cons, refine is_submonoid.mul_mem _ (ih HL.2), rcases HL.1 with hs | rfl, { exact algebra.subset_adjoin (set.mem_image_of_mem _ hs) }, exact is_add_subgroup.neg_mem (is_submonoid.one_mem _) }, replace hmp := congr_arg subtype.val hmp, replace hmp := congr_arg subtype.val hmp, exact subtype.eq hmp end end section algebra open algebra variables {R : Type*} {A : Type*} {B : Type*} variables [comm_ring R] [comm_ring A] [comm_ring B] variables [algebra R A] [algebra A B] set_option class.instance_max_depth 50 lemma is_integral_trans_aux (x : B) {p : polynomial A} (pmonic : monic p) (hp : aeval A B x p = 0) (S : set (comap R A B)) (hS : S = (↑((finset.range (p.nat_degree + 1)).image (Ξ» i, to_comap R A B (p.coeff i))) : set (comap R A B))) : is_integral (adjoin R S) (comap.to_comap R A B x) := begin have coeffs_mem : βˆ€ i, coeff (map (to_comap R A B) p) i ∈ adjoin R S, { intro i, by_cases hi : i ∈ finset.range (p.nat_degree + 1), { apply algebra.subset_adjoin, subst S, rw [finset.mem_coe, finset.mem_image, coeff_map], exact ⟨i, hi, rfl⟩ }, { rw [finset.mem_range, not_lt] at hi, rw [coeff_map, coeff_eq_zero_of_nat_degree_lt hi, alg_hom.map_zero], exact submodule.zero_mem (adjoin R S : submodule R (comap R A B)) } }, obtain ⟨q, hq⟩ : βˆƒ q : polynomial (adjoin R S), q.map (algebra_map (comap R A B)) = (p.map $ to_comap R A B), { rw [← set.mem_range], dsimp only, apply (polynomial.mem_map_range _).2, { intros i, specialize coeffs_mem i, rw ← subalgebra.mem_coe at coeffs_mem, convert coeffs_mem, exact subtype.val_range }, { apply is_ring_hom.is_semiring_hom } }, use q, split, { suffices h : (q.map (algebra_map (comap R A B))).monic, { refine @monic_of_injective _ _ _ _ _ (by exact is_ring_hom.is_semiring_hom _) _ _ h, exact subtype.val_injective }, { rw hq, exact monic_map _ pmonic } }, { convert hp using 1, replace hq := congr_arg (eval (comap.to_comap R A B x)) hq, convert hq using 1; symmetry, swap, exact eval_map _ _, refine @eval_map _ _ _ _ _ _ (by exact is_ring_hom.is_semiring_hom _) _ }, end /-- If A is an R-algebra all of whose elements are integral over R, and x is an element of an A-algebra that is integral over A, then x is integral over R.-/ lemma is_integral_trans (A_int : βˆ€ x : A, is_integral R x) (x : B) (hx : is_integral A x) : is_integral R (comap.to_comap R A B x) := begin rcases hx with ⟨p, pmonic, hp⟩, let S : set (comap R A B) := (↑((finset.range (p.nat_degree + 1)).image (Ξ» i, to_comap R A B (p.coeff i))) : set (comap R A B)), refine is_integral_of_mem_of_fg (adjoin R (S βˆͺ {comap.to_comap R A B x})) _ _ _, swap, { apply subset_adjoin, simp }, apply fg_trans, { apply fg_adjoin_of_finite, { apply finset.finite_to_set }, intros x hx, rw [finset.mem_coe, finset.mem_image] at hx, rcases hx with ⟨i, hi, rfl⟩, rcases A_int (p.coeff i) with ⟨q, hq, hqx⟩, use [q, hq], replace hqx := congr_arg (to_comap R A B : A β†’ (comap R A B)) hqx, rw alg_hom.map_zero at hqx, convert hqx using 1, symmetry, exact polynomial.hom_evalβ‚‚ _ _ _ _ }, { apply fg_adjoin_singleton_of_integral, exact is_integral_trans_aux _ pmonic hp _ rfl } end /-- If A is an R-algebra all of whose elements are integral over R, and B is an A-algebra all of whose elements are integral over A, then all elements of B are integral over R.-/ lemma algebra.is_integral_trans (A_int : βˆ€ x : A, is_integral R x)(B_int : βˆ€ x:B, is_integral A x) : βˆ€ x:(comap R A B), is_integral R x := Ξ» x, is_integral_trans A_int x (B_int x) end algebra
2fcffd96a771aafe690d4d6b9825108bd30d977c
618003631150032a5676f229d13a079ac875ff77
/src/data/option/basic.lean
7c2f2358d27a2a123429e70d2a93032c2aa77740
[ "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,418
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import tactic.basic namespace option variables {Ξ± : Type*} {Ξ² : Type*} {Ξ³ : Type*} lemma some_ne_none (x : Ξ±) : some x β‰  none := Ξ» h, option.no_confusion h @[simp] theorem get_mem : βˆ€ {o : option Ξ±} (h : is_some o), option.get h ∈ o | (some a) _ := rfl theorem get_of_mem {a : Ξ±} : βˆ€ {o : option Ξ±} (h : is_some o), a ∈ o β†’ option.get h = a | _ _ rfl := rfl @[simp] lemma not_mem_none (a : Ξ±) : a βˆ‰ (none : option Ξ±) := Ξ» h, option.no_confusion h @[simp] lemma some_get : βˆ€ {x : option Ξ±} (h : is_some x), some (option.get h) = x | (some x) hx := rfl @[simp] lemma get_some (x : Ξ±) (h : is_some (some x)) : option.get h = x := rfl @[simp] lemma get_or_else_some (x y : Ξ±) : option.get_or_else (some x) y = x := rfl lemma get_or_else_of_ne_none {x : option Ξ±} (hx : x β‰  none) (y : Ξ±) : some (x.get_or_else y) = x := by cases x; [contradiction, rw get_or_else_some] theorem mem_unique {o : option Ξ±} {a b : Ξ±} (ha : a ∈ o) (hb : b ∈ o) : a = b := option.some.inj $ ha.symm.trans hb theorem injective_some (Ξ± : Type*) : function.injective (@some Ξ±) := Ξ» _ _, some_inj.mp /-- `option.map f` is injective if `f` is injective. -/ theorem injective_map {f : Ξ± β†’ Ξ²} (Hf : function.injective f) : function.injective (option.map f) | none none H := rfl | (some a₁) (some aβ‚‚) H := by rw Hf (option.some.inj H) @[ext] theorem ext : βˆ€ {o₁ oβ‚‚ : option Ξ±}, (βˆ€ a, a ∈ o₁ ↔ a ∈ oβ‚‚) β†’ o₁ = oβ‚‚ | none none H := rfl | (some a) o H := ((H _).1 rfl).symm | o (some b) H := (H _).2 rfl theorem eq_none_iff_forall_not_mem {o : option Ξ±} : o = none ↔ (βˆ€ a, a βˆ‰ o) := ⟨λ e a h, by rw e at h; cases h, Ξ» h, ext $ by simpa⟩ @[simp] theorem none_bind {Ξ± Ξ²} (f : Ξ± β†’ option Ξ²) : none >>= f = none := rfl @[simp] theorem some_bind {Ξ± Ξ²} (a : Ξ±) (f : Ξ± β†’ option Ξ²) : some a >>= f = f a := rfl @[simp] theorem none_bind' (f : Ξ± β†’ option Ξ²) : none.bind f = none := rfl @[simp] theorem some_bind' (a : Ξ±) (f : Ξ± β†’ option Ξ²) : (some a).bind f = f a := rfl @[simp] theorem bind_some : βˆ€ x : option Ξ±, x >>= some = x := @bind_pure Ξ± option _ _ @[simp] theorem bind_eq_some {Ξ± Ξ²} {x : option Ξ±} {f : Ξ± β†’ option Ξ²} {b : Ξ²} : x >>= f = some b ↔ βˆƒ a, x = some a ∧ f a = some b := by cases x; simp @[simp] theorem bind_eq_some' {x : option Ξ±} {f : Ξ± β†’ option Ξ²} {b : Ξ²} : x.bind f = some b ↔ βˆƒ a, x = some a ∧ f a = some b := by cases x; simp @[simp] theorem bind_eq_none' {o : option Ξ±} {f : Ξ± β†’ option Ξ²} : o.bind f = none ↔ (βˆ€ b a, a ∈ o β†’ b βˆ‰ f a) := by simp only [eq_none_iff_forall_not_mem, not_exists, not_and, mem_def, bind_eq_some'] @[simp] theorem bind_eq_none {Ξ± Ξ²} {o : option Ξ±} {f : Ξ± β†’ option Ξ²} : o >>= f = none ↔ (βˆ€ b a, a ∈ o β†’ b βˆ‰ f a) := bind_eq_none' lemma bind_comm {Ξ± Ξ² Ξ³} {f : Ξ± β†’ Ξ² β†’ option Ξ³} (a : option Ξ±) (b : option Ξ²) : a.bind (Ξ»x, b.bind (f x)) = b.bind (Ξ»y, a.bind (Ξ»x, f x y)) := by cases a; cases b; refl lemma bind_assoc (x : option Ξ±) (f : Ξ± β†’ option Ξ²) (g : Ξ² β†’ option Ξ³) : (x.bind f).bind g = x.bind (Ξ» y, (f y).bind g) := by cases x; refl @[simp] theorem map_none {Ξ± Ξ²} {f : Ξ± β†’ Ξ²} : f <$> none = none := rfl @[simp] theorem map_some {Ξ± Ξ²} {a : Ξ±} {f : Ξ± β†’ Ξ²} : f <$> some a = some (f a) := rfl @[simp] theorem map_none' {f : Ξ± β†’ Ξ²} : option.map f none = none := rfl @[simp] theorem map_some' {a : Ξ±} {f : Ξ± β†’ Ξ²} : option.map f (some a) = some (f a) := rfl @[simp] theorem map_eq_some {Ξ± Ξ²} {x : option Ξ±} {f : Ξ± β†’ Ξ²} {b : Ξ²} : f <$> x = some b ↔ βˆƒ a, x = some a ∧ f a = b := by cases x; simp @[simp] theorem map_eq_some' {x : option Ξ±} {f : Ξ± β†’ Ξ²} {b : Ξ²} : x.map f = some b ↔ βˆƒ a, x = some a ∧ f a = b := by cases x; simp @[simp] theorem map_id' : option.map (@id Ξ±) = id := map_id @[simp] theorem seq_some {Ξ± Ξ²} {a : Ξ±} {f : Ξ± β†’ Ξ²} : some f <*> some a = some (f a) := rfl @[simp] theorem some_orelse' (a : Ξ±) (x : option Ξ±) : (some a).orelse x = some a := rfl @[simp] theorem some_orelse (a : Ξ±) (x : option Ξ±) : (some a <|> x) = some a := rfl @[simp] theorem none_orelse' (x : option Ξ±) : none.orelse x = x := by cases x; refl @[simp] theorem none_orelse (x : option Ξ±) : (none <|> x) = x := none_orelse' x @[simp] theorem orelse_none' (x : option Ξ±) : x.orelse none = x := by cases x; refl @[simp] theorem orelse_none (x : option Ξ±) : (x <|> none) = x := orelse_none' x @[simp] theorem is_some_none : @is_some Ξ± none = ff := rfl @[simp] theorem is_some_some {a : Ξ±} : is_some (some a) = tt := rfl theorem is_some_iff_exists {x : option Ξ±} : is_some x ↔ βˆƒ a, x = some a := by cases x; simp [is_some]; exact ⟨_, rfl⟩ @[simp] theorem is_none_none : @is_none Ξ± none = tt := rfl @[simp] theorem is_none_some {a : Ξ±} : is_none (some a) = ff := rfl @[simp] theorem not_is_some {a : option Ξ±} : is_some a = ff ↔ a.is_none = tt := by cases a; simp lemma eq_some_iff_get_eq {o : option Ξ±} {a : Ξ±} : o = some a ↔ βˆƒ h : o.is_some, option.get h = a := by cases o; simp lemma not_is_some_iff_eq_none {o : option Ξ±} : Β¬o.is_some ↔ o = none := by cases o; simp lemma ne_none_iff_is_some {o : option Ξ±} : o β‰  none ↔ o.is_some := by cases o; simp lemma bex_ne_none {p : option Ξ± β†’ Prop} : (βˆƒ x β‰  none, p x) ↔ βˆƒ x, p (some x) := ⟨λ ⟨x, hx, hp⟩, ⟨get $ ne_none_iff_is_some.1 hx, by rwa [some_get]⟩, Ξ» ⟨x, hx⟩, ⟨some x, some_ne_none x, hx⟩⟩ lemma ball_ne_none {p : option Ξ± β†’ Prop} : (βˆ€ x β‰  none, p x) ↔ βˆ€ x, p (some x) := ⟨λ h x, h (some x) (some_ne_none x), Ξ» h x hx, by simpa only [some_get] using h (get $ ne_none_iff_is_some.1 hx)⟩ theorem iget_mem [inhabited Ξ±] : βˆ€ {o : option Ξ±}, is_some o β†’ o.iget ∈ o | (some a) _ := rfl theorem iget_of_mem [inhabited Ξ±] {a : Ξ±} : βˆ€ {o : option Ξ±}, a ∈ o β†’ o.iget = a | _ rfl := rfl @[simp] theorem guard_eq_some {p : Ξ± β†’ Prop} [decidable_pred p] {a b : Ξ±} : guard p a = some b ↔ a = b ∧ p a := by by_cases p a; simp [option.guard, h]; intro; contradiction @[simp] theorem guard_eq_some' {p : Prop} [decidable p] : βˆ€ u, _root_.guard p = some u ↔ p | () := by by_cases p; simp [guard, h, pure]; intro; contradiction theorem lift_or_get_choice {f : Ξ± β†’ Ξ± β†’ Ξ±} (h : βˆ€ a b, f a b = a ∨ f a b = b) : βˆ€ o₁ oβ‚‚, lift_or_get f o₁ oβ‚‚ = o₁ ∨ lift_or_get f o₁ oβ‚‚ = oβ‚‚ | none none := or.inl rfl | (some a) none := or.inl rfl | none (some b) := or.inr rfl | (some a) (some b) := by simpa [lift_or_get] using h a b @[simp] lemma lift_or_get_none_left {f} {b : option Ξ±} : lift_or_get f none b = b := by cases b; refl @[simp] lemma lift_or_get_none_right {f} {a : option Ξ±} : lift_or_get f a none = a := by cases a; refl @[simp] lemma lift_or_get_some_some {f} {a b : Ξ±} : lift_or_get f (some a) (some b) = f a b := rfl /-- given an element of `a : option Ξ±`, a default element `b : Ξ²` and a function `Ξ± β†’ Ξ²`, apply this function to `a` if it comes from `Ξ±`, and return `b` otherwise. -/ def cases_on' : option Ξ± β†’ Ξ² β†’ (Ξ± β†’ Ξ²) β†’ Ξ² | none n s := n | (some a) n s := s a end option
3217bf26685cd4a41f09e14ebad9025d99f3d7cb
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/hott/len_eq.hlean
2ddfafa8ed56f09d7a2b8d24c69e92f0939f5663
[ "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
2,970
hlean
import init.ua open nat unit equiv is_trunc inductive vector (A : Type) : nat β†’ Type := | nil {} : vector A zero | cons : Ξ  {n}, A β†’ vector A n β†’ vector A (succ n) open vector notation a :: b := cons a b definition const {A : Type} : Ξ  (n : nat), A β†’ vector A n | zero a := nil | (succ n) a := a :: const n a definition head {A : Type} : Ξ  {n : nat}, vector A (succ n) β†’ A | n (x :: xs) := x theorem singlenton_vector_unit : βˆ€ {n : nat} (v w : vector unit n), v = w | zero nil nil := rfl | (succ n) (star::xs) (star::ys) := begin have h₁ : xs = ys, from singlenton_vector_unit xs ys, rewrite h₁ end private definition f (n m : nat) (v : vector unit n) : vector unit m := const m star theorem vn_eqv_vm (n m : nat) : vector unit n ≃ vector unit m := equiv.MK (f n m) (f m n) (take v : vector unit m, singlenton_vector_unit (f n m (f m n v)) v) (take v : vector unit n, singlenton_vector_unit (f m n (f n m v)) v) theorem vn_eq_vm (n m : nat) : vector unit n = vector unit m := ua (vn_eqv_vm n m) definition vector_inj (A : Type) := βˆ€ (n m : nat), vector A n = vector A m β†’ n = m theorem not_vector_inj : Β¬ vector_inj unit := assume H : vector_inj unit, have aux₁ : 0 = 1, from H 0 1 (vn_eq_vm 0 1), lift.down (nat.no_confusion aux₁) definition cast {A B : Type} (H : A = B) (a : A) : B := eq.rec_on H a open sigma definition heq {A B : Type} (a : A) (b : B) := Ξ£ (H : A = B), cast H a = b infix `==`:50 := heq definition heq.type_eq {A B : Type} {a : A} {b : B} : a == b β†’ A = B | ⟨H, e⟩ := H definition heq.symm : βˆ€ {A B : Type} {a : A} {b : B}, a == b β†’ b == a | A A a a ⟨eq.refl A, eq.refl a⟩ := ⟨eq.refl A, eq.refl a⟩ definition heq.trans : βˆ€ {A B C : Type} {a : A} {b : B} {c : C}, a == b β†’ b == c β†’ a == c | A A A a a a ⟨eq.refl A, eq.refl a⟩ ⟨eq.refl A, eq.refl a⟩ := ⟨eq.refl A, eq.refl a⟩ theorem cast_heq : βˆ€ {A B : Type} (H : A = B) (a : A), cast H a == a | A A (eq.refl A) a := ⟨eq.refl A, eq.refl a⟩ definition lem_eq (A : Type) : Type := βˆ€ (n m : nat) (v : vector A n) (w : vector A m), v == w β†’ n = m theorem lem_eq_iff_vector_inj (A : Type) [inh : inhabited A] : lem_eq A ↔ vector_inj A := iff.intro (assume Hl : lem_eq A, assume n m he, have a : A, from default A, have v : vector A n, from const n a, have e₁ : v == cast he v, from heq.symm (cast_heq he v), Hl n m v (cast he v) e₁) (assume Hr : vector_inj A, assume n m v w he, Hr n m (heq.type_eq he)) theorem lem_eq_of_not_inhabited (A : Type) [ninh : inhabited A β†’ empty] : lem_eq A := take (n m : nat), match n with | zero := match m with | zero := take v w He, rfl | (succ m₁) := take (v : vector A zero) (w : vector A (succ m₁)), empty.elim (ninh (inhabited.mk (head w))) end | (succ n₁) := take (v : vector A (succ n₁)) (w : vector A m), empty.elim (ninh (inhabited.mk (head v))) end
960c9e4ef076bcd68184f8e4d3a073958c13b2e0
f57749ca63d6416f807b770f67559503fdb21001
/tests/lean/run/new_obtain4.lean
034a09c47db25daccdafba2e03f471c29c200e6a
[ "Apache-2.0" ]
permissive
aliassaf/lean
bd54e85bed07b1ff6f01396551867b2677cbc6ac
f9b069b6a50756588b309b3d716c447004203152
refs/heads/master
1,610,982,152,948
1,438,916,029,000
1,438,916,029,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
451
lean
import data.set open set function eq.ops variables {X Y Z : Type} lemma image_compose (f : Y β†’ X) (g : X β†’ Y) (a : set X) : (f ∘ g) '[a] = f '[g '[a]] := setext (take z, iff.intro (assume Hz, obtain x Hx₁ Hxβ‚‚, from Hz, by repeat (apply mem_image | assumption | reflexivity)) (assume Hz, obtain y [x Hz₁ Hzβ‚‚] Hyβ‚‚, from Hz, by repeat (apply mem_image | assumption | esimp [compose] | rewrite Hzβ‚‚)))
ad6cad16a6d9cb36521bcdbd75fc4ef6a1b55453
437dc96105f48409c3981d46fb48e57c9ac3a3e4
/src/data/nat/basic.lean
ca34c09f43c1189619cbb9b75a1c979fb4e2705a
[ "Apache-2.0" ]
permissive
dan-c-k/mathlib
08efec79bd7481ee6da9cc44c24a653bff4fbe0d
96efc220f6225bc7a5ed8349900391a33a38cc56
refs/heads/master
1,658,082,847,093
1,589,013,201,000
1,589,013,201,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
62,439
lean
/- Copyright (c) 2014 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Leonardo de Moura, Jeremy Avigad, Mario Carneiro -/ import algebra.ordered_ring import algebra.order_functions /-! # Basic operations on the natural numbers This files has some basic lemmas about natural numbers, definition of the `choice` function, and extra recursors: * `le_rec_on`, `le_induction`: recursion and induction principles starting at non-zero numbers. * `decreasing_induction` : recursion growing downwards. * `strong_rec'` : recursion based on strong inequalities. -/ universes u v namespace nat variables {m n k : β„•} -- Sometimes a bare `nat.add` or similar appears as a consequence of unfolding -- during pattern matching. These lemmas package them back up as typeclass -- mediated operations. @[simp] theorem add_def {a b : β„•} : nat.add a b = a + b := rfl @[simp] theorem mul_def {a b : β„•} : nat.mul a b = a * b := rfl attribute [simp] nat.add_sub_cancel nat.add_sub_cancel_left attribute [simp] nat.sub_self @[simp] lemma succ_pos' {n : β„•} : 0 < succ n := succ_pos n theorem succ_inj' {n m : β„•} : succ n = succ m ↔ n = m := ⟨succ_inj, congr_arg _⟩ theorem succ_le_succ_iff {m n : β„•} : succ m ≀ succ n ↔ m ≀ n := ⟨le_of_succ_le_succ, succ_le_succ⟩ lemma zero_max {m : nat} : max 0 m = m := max_eq_right (zero_le _) theorem max_succ_succ {m n : β„•} : max (succ m) (succ n) = succ (max m n) := begin by_cases h1 : m ≀ n, rw [max_eq_right h1, max_eq_right (succ_le_succ h1)], { rw not_le at h1, have h2 := le_of_lt h1, rw [max_eq_left h2, max_eq_left (succ_le_succ h2)] } end lemma not_succ_lt_self {n : β„•} : Β¬succ n < n := not_lt_of_ge (nat.le_succ _) theorem lt_succ_iff {m n : β„•} : m < succ n ↔ m ≀ n := succ_le_succ_iff lemma succ_le_iff {m n : β„•} : succ m ≀ n ↔ m < n := ⟨lt_of_succ_le, succ_le_of_lt⟩ lemma lt_iff_add_one_le {m n : β„•} : m < n ↔ m + 1 ≀ n := by rw succ_le_iff -- Just a restatement of `nat.lt_succ_iff` using `+1`. lemma lt_add_one_iff {a b : β„•} : a < b + 1 ↔ a ≀ b := lt_succ_iff -- A flipped version of `lt_add_one_iff`. lemma lt_one_add_iff {a b : β„•} : a < 1 + b ↔ a ≀ b := by simp only [add_comm, lt_succ_iff] -- This is true reflexively, by the definition of `≀` on β„•, -- but it's still useful to have, to convince Lean to change the syntactic type. lemma add_one_le_iff {a b : β„•} : a + 1 ≀ b ↔ a < b := iff.refl _ lemma one_add_le_iff {a b : β„•} : 1 + a ≀ b ↔ a < b := by simp only [add_comm, add_one_le_iff] theorem of_le_succ {n m : β„•} (H : n ≀ m.succ) : n ≀ m ∨ n = m.succ := (lt_or_eq_of_le H).imp le_of_lt_succ id /-- Recursion starting at a non-zero number: given a map `C k β†’ C (k+1)` for each `k`, there is a map from `C n` to each `C m`, `n ≀ m`. -/ @[elab_as_eliminator] def le_rec_on {C : β„• β†’ Sort u} {n : β„•} : Ξ  {m : β„•}, n ≀ m β†’ (Ξ  {k}, C k β†’ C (k+1)) β†’ C n β†’ C m | 0 H next x := eq.rec_on (eq_zero_of_le_zero H) x | (m+1) H next x := or.by_cases (of_le_succ H) (Ξ» h : n ≀ m, next $ le_rec_on h @next x) (Ξ» h : n = m + 1, eq.rec_on h x) theorem le_rec_on_self {C : β„• β†’ Sort u} {n} {h : n ≀ n} {next} (x : C n) : (le_rec_on h next x : C n) = x := by cases n; unfold le_rec_on or.by_cases; rw [dif_neg n.not_succ_le_self, dif_pos rfl] theorem le_rec_on_succ {C : β„• β†’ Sort u} {n m} (h1 : n ≀ m) {h2 : n ≀ m+1} {next} (x : C n) : (le_rec_on h2 @next x : C (m+1)) = next (le_rec_on h1 @next x : C m) := by conv { to_lhs, rw [le_rec_on, or.by_cases, dif_pos h1] } theorem le_rec_on_succ' {C : β„• β†’ Sort u} {n} {h : n ≀ n+1} {next} (x : C n) : (le_rec_on h next x : C (n+1)) = next x := by rw [le_rec_on_succ (le_refl n), le_rec_on_self] theorem le_rec_on_trans {C : β„• β†’ Sort u} {n m k} (hnm : n ≀ m) (hmk : m ≀ k) {next} (x : C n) : (le_rec_on (le_trans hnm hmk) @next x : C k) = le_rec_on hmk @next (le_rec_on hnm @next x) := begin induction hmk with k hmk ih, { rw le_rec_on_self }, rw [le_rec_on_succ (le_trans hnm hmk), ih, le_rec_on_succ] end theorem le_rec_on_succ_left {C : β„• β†’ Sort u} {n m} (h1 : n ≀ m) (h2 : n+1 ≀ m) {next : Ξ {{k}}, C k β†’ C (k+1)} (x : C n) : (le_rec_on h2 next (next x) : C m) = (le_rec_on h1 next x : C m) := begin rw [subsingleton.elim h1 (le_trans (le_succ n) h2), le_rec_on_trans (le_succ n) h2, le_rec_on_succ'] end theorem le_rec_on_injective {C : β„• β†’ Sort u} {n m} (hnm : n ≀ m) (next : Ξ  n, C n β†’ C (n+1)) (Hnext : βˆ€ n, function.injective (next n)) : function.injective (le_rec_on hnm next) := begin induction hnm with m hnm ih, { intros x y H, rwa [le_rec_on_self, le_rec_on_self] at H }, intros x y H, rw [le_rec_on_succ hnm, le_rec_on_succ hnm] at H, exact ih (Hnext _ H) end theorem le_rec_on_surjective {C : β„• β†’ Sort u} {n m} (hnm : n ≀ m) (next : Ξ  n, C n β†’ C (n+1)) (Hnext : βˆ€ n, function.surjective (next n)) : function.surjective (le_rec_on hnm next) := begin induction hnm with m hnm ih, { intros x, use x, rw le_rec_on_self }, intros x, rcases Hnext _ x with ⟨w, rfl⟩, rcases ih w with ⟨x, rfl⟩, use x, rw le_rec_on_succ end theorem pred_eq_of_eq_succ {m n : β„•} (H : m = n.succ) : m.pred = n := by simp [H] @[simp] lemma pred_eq_succ_iff {n m : β„•} : pred n = succ m ↔ n = m + 2 := by cases n; split; rintro ⟨⟩; refl theorem pred_sub (n m : β„•) : pred n - m = pred (n - m) := by rw [← sub_one, nat.sub_sub, one_add]; refl @[simp] lemma add_succ_sub_one (n m : β„•) : (n + succ m) - 1 = n + m := by rw [add_succ, succ_sub_one] @[simp] lemma succ_add_sub_one (n m : β„•) : (succ n + m) - 1 = n + m := by rw [succ_add, succ_sub_one] lemma pred_eq_sub_one (n : β„•) : pred n = n - 1 := rfl lemma one_le_of_lt {n m : β„•} (h : n < m) : 1 ≀ m := lt_of_le_of_lt (nat.zero_le _) h lemma le_pred_of_lt {n m : β„•} (h : m < n) : m ≀ n - 1 := nat.sub_le_sub_right h 1 lemma le_of_pred_lt {m n : β„•} : pred m < n β†’ m ≀ n := match m with | 0 := le_of_lt | m+1 := id end /-- This ensures that `simp` succeeds on `pred (n + 1) = n`. -/ @[simp] lemma pred_one_add (n : β„•) : pred (1 + n) = n := by rw [add_comm, add_one, pred_succ] theorem pos_iff_ne_zero : 0 < n ↔ n β‰  0 := ⟨ne_of_gt, nat.pos_of_ne_zero⟩ lemma one_lt_iff_ne_zero_and_ne_one : βˆ€ {n : β„•}, 1 < n ↔ n β‰  0 ∧ n β‰  1 | 0 := dec_trivial | 1 := dec_trivial | (n+2) := dec_trivial theorem eq_of_lt_succ_of_not_lt {a b : β„•} (h1 : a < b + 1) (h2 : Β¬ a < b) : a = b := have h3 : a ≀ b, from le_of_lt_succ h1, or.elim (eq_or_lt_of_not_lt h2) (Ξ» h, h) (Ξ» h, absurd h (not_lt_of_ge h3)) protected theorem le_sub_add (n m : β„•) : n ≀ n - m + m := or.elim (le_total n m) (assume : n ≀ m, begin rw [sub_eq_zero_of_le this, zero_add], exact this end) (assume : m ≀ n, begin rw (nat.sub_add_cancel this) end) theorem sub_add_eq_max (n m : β„•) : n - m + m = max n m := eq_max (nat.le_sub_add _ _) (le_add_left _ _) $ Ξ» k h₁ hβ‚‚, by rw ← nat.sub_add_cancel hβ‚‚; exact add_le_add_right (nat.sub_le_sub_right h₁ _) _ theorem sub_add_min (n m : β„•) : n - m + min n m = n := (le_total n m).elim (Ξ» h, by rw [min_eq_left h, sub_eq_zero_of_le h, zero_add]) (Ξ» h, by rw [min_eq_right h, nat.sub_add_cancel h]) protected theorem add_sub_cancel' {n m : β„•} (h : m ≀ n) : m + (n - m) = n := by rw [add_comm, nat.sub_add_cancel h] protected theorem sub_eq_of_eq_add (h : k = m + n) : k - m = n := begin rw [h, nat.add_sub_cancel_left] end theorem sub_cancel {a b c : β„•} (h₁ : a ≀ b) (hβ‚‚ : a ≀ c) (w : b - a = c - a) : b = c := by rw [←nat.sub_add_cancel h₁, ←nat.sub_add_cancel hβ‚‚, w] lemma sub_sub_sub_cancel_right {a b c : β„•} (hβ‚‚ : c ≀ b) : (a - c) - (b - c) = a - b := by rw [nat.sub_sub, ←nat.add_sub_assoc hβ‚‚, nat.add_sub_cancel_left] lemma add_sub_cancel_right (n m k : β„•) : n + (m + k) - k = n + m := by { rw [nat.add_sub_assoc, nat.add_sub_cancel], apply k.le_add_left } protected lemma sub_add_eq_add_sub {a b c : β„•} (h : b ≀ a) : (a - b) + c = (a + c) - b := by rw [add_comm a, nat.add_sub_assoc h, add_comm] theorem sub_min (n m : β„•) : n - min n m = n - m := nat.sub_eq_of_eq_add $ by rw [add_comm, sub_add_min] theorem sub_sub_assoc {a b c : β„•} (h₁ : b ≀ a) (hβ‚‚ : c ≀ b) : a - (b - c) = a - b + c := (nat.sub_eq_iff_eq_add (le_trans (nat.sub_le _ _) h₁)).2 $ by rw [add_right_comm, add_assoc, nat.sub_add_cancel hβ‚‚, nat.sub_add_cancel h₁] protected theorem lt_of_sub_pos (h : 0 < n - m) : m < n := lt_of_not_ge (assume : n ≀ m, have n - m = 0, from sub_eq_zero_of_le this, begin rw this at h, exact lt_irrefl _ h end) protected theorem lt_of_sub_lt_sub_right : m - k < n - k β†’ m < n := lt_imp_lt_of_le_imp_le (Ξ» h, nat.sub_le_sub_right h _) protected theorem lt_of_sub_lt_sub_left : m - n < m - k β†’ k < n := lt_imp_lt_of_le_imp_le (nat.sub_le_sub_left _) protected theorem sub_lt_self (h₁ : 0 < m) (hβ‚‚ : 0 < n) : m - n < m := calc m - n = succ (pred m) - succ (pred n) : by rw [succ_pred_eq_of_pos h₁, succ_pred_eq_of_pos hβ‚‚] ... = pred m - pred n : by rw succ_sub_succ ... ≀ pred m : sub_le _ _ ... < succ (pred m) : lt_succ_self _ ... = m : succ_pred_eq_of_pos h₁ protected theorem le_sub_right_of_add_le (h : m + k ≀ n) : m ≀ n - k := by rw ← nat.add_sub_cancel m k; exact nat.sub_le_sub_right h k protected theorem le_sub_left_of_add_le (h : k + m ≀ n) : m ≀ n - k := nat.le_sub_right_of_add_le (by rwa add_comm at h) protected theorem lt_sub_right_of_add_lt (h : m + k < n) : m < n - k := lt_of_succ_le $ nat.le_sub_right_of_add_le $ by rw succ_add; exact succ_le_of_lt h protected theorem lt_sub_left_of_add_lt (h : k + m < n) : m < n - k := nat.lt_sub_right_of_add_lt (by rwa add_comm at h) protected theorem add_lt_of_lt_sub_right (h : m < n - k) : m + k < n := @nat.lt_of_sub_lt_sub_right _ _ k (by rwa nat.add_sub_cancel) protected theorem add_lt_of_lt_sub_left (h : m < n - k) : k + m < n := by rw add_comm; exact nat.add_lt_of_lt_sub_right h protected theorem le_add_of_sub_le_right : n - k ≀ m β†’ n ≀ m + k := le_imp_le_of_lt_imp_lt nat.lt_sub_right_of_add_lt protected theorem le_add_of_sub_le_left : n - k ≀ m β†’ n ≀ k + m := le_imp_le_of_lt_imp_lt nat.lt_sub_left_of_add_lt protected theorem lt_add_of_sub_lt_right : n - k < m β†’ n < m + k := lt_imp_lt_of_le_imp_le nat.le_sub_right_of_add_le protected theorem lt_add_of_sub_lt_left : n - k < m β†’ n < k + m := lt_imp_lt_of_le_imp_le nat.le_sub_left_of_add_le protected theorem sub_le_left_of_le_add : n ≀ k + m β†’ n - k ≀ m := le_imp_le_of_lt_imp_lt nat.add_lt_of_lt_sub_left protected theorem sub_le_right_of_le_add : n ≀ m + k β†’ n - k ≀ m := le_imp_le_of_lt_imp_lt nat.add_lt_of_lt_sub_right protected theorem sub_lt_left_iff_lt_add (H : n ≀ k) : k - n < m ↔ k < n + m := ⟨nat.lt_add_of_sub_lt_left, Ξ» h₁, have succ k ≀ n + m, from succ_le_of_lt h₁, have succ (k - n) ≀ m, from calc succ (k - n) = succ k - n : by rw (succ_sub H) ... ≀ n + m - n : nat.sub_le_sub_right this n ... = m : by rw nat.add_sub_cancel_left, lt_of_succ_le this⟩ protected theorem le_sub_left_iff_add_le (H : m ≀ k) : n ≀ k - m ↔ m + n ≀ k := le_iff_le_iff_lt_iff_lt.2 (nat.sub_lt_left_iff_lt_add H) protected theorem le_sub_right_iff_add_le (H : n ≀ k) : m ≀ k - n ↔ m + n ≀ k := by rw [nat.le_sub_left_iff_add_le H, add_comm] protected theorem lt_sub_left_iff_add_lt : n < k - m ↔ m + n < k := ⟨nat.add_lt_of_lt_sub_left, nat.lt_sub_left_of_add_lt⟩ protected theorem lt_sub_right_iff_add_lt : m < k - n ↔ m + n < k := by rw [nat.lt_sub_left_iff_add_lt, add_comm] theorem sub_le_left_iff_le_add : m - n ≀ k ↔ m ≀ n + k := le_iff_le_iff_lt_iff_lt.2 nat.lt_sub_left_iff_add_lt theorem sub_le_right_iff_le_add : m - k ≀ n ↔ m ≀ n + k := by rw [nat.sub_le_left_iff_le_add, add_comm] protected theorem sub_lt_right_iff_lt_add (H : k ≀ m) : m - k < n ↔ m < n + k := by rw [nat.sub_lt_left_iff_lt_add H, add_comm] protected theorem sub_le_sub_left_iff (H : k ≀ m) : m - n ≀ m - k ↔ k ≀ n := ⟨λ h, have k + (m - k) - n ≀ m - k, by rwa nat.add_sub_cancel' H, nat.le_of_add_le_add_right (nat.le_add_of_sub_le_left this), nat.sub_le_sub_left _⟩ protected theorem sub_lt_sub_right_iff (H : k ≀ m) : m - k < n - k ↔ m < n := lt_iff_lt_of_le_iff_le (nat.sub_le_sub_right_iff _ _ _ H) protected theorem sub_lt_sub_left_iff (H : n ≀ m) : m - n < m - k ↔ k < n := lt_iff_lt_of_le_iff_le (nat.sub_le_sub_left_iff H) protected theorem sub_le_iff : m - n ≀ k ↔ m - k ≀ n := nat.sub_le_left_iff_le_add.trans nat.sub_le_right_iff_le_add.symm protected lemma sub_le_self (n m : β„•) : n - m ≀ n := nat.sub_le_left_of_le_add (nat.le_add_left _ _) protected theorem sub_lt_iff (h₁ : n ≀ m) (hβ‚‚ : k ≀ m) : m - n < k ↔ m - k < n := (nat.sub_lt_left_iff_lt_add h₁).trans (nat.sub_lt_right_iff_lt_add hβ‚‚).symm lemma pred_le_iff {n m : β„•} : pred n ≀ m ↔ n ≀ succ m := @nat.sub_le_right_iff_le_add n m 1 lemma lt_pred_iff {n m : β„•} : n < pred m ↔ succ n < m := @nat.lt_sub_right_iff_add_lt n 1 m lemma lt_of_lt_pred {a b : β„•} (h : a < b - 1) : a < b := lt_of_succ_lt (lt_pred_iff.1 h) protected theorem mul_ne_zero {n m : β„•} (n0 : n β‰  0) (m0 : m β‰  0) : n * m β‰  0 | nm := (eq_zero_of_mul_eq_zero nm).elim n0 m0 @[simp] protected theorem mul_eq_zero {a b : β„•} : a * b = 0 ↔ a = 0 ∨ b = 0 := iff.intro eq_zero_of_mul_eq_zero (by simp [or_imp_distrib] {contextual := tt}) @[simp] protected theorem zero_eq_mul {a b : β„•} : 0 = a * b ↔ a = 0 ∨ b = 0 := by rw [eq_comm, nat.mul_eq_zero] lemma eq_zero_of_double_le {a : β„•} (h : 2 * a ≀ a) : a = 0 := nat.eq_zero_of_le_zero $ by rwa [two_mul, nat.add_le_to_le_sub, nat.sub_self] at h; refl lemma eq_zero_of_mul_le {a b : β„•} (hb : 2 ≀ b) (h : b * a ≀ a) : a = 0 := eq_zero_of_double_le $ le_trans (nat.mul_le_mul_right _ hb) h lemma le_mul_of_pos_left {m n : β„•} (h : 0 < n) : m ≀ n * m := begin conv {to_lhs, rw [← one_mul(m)]}, exact mul_le_mul_of_nonneg_right (nat.succ_le_of_lt h) dec_trivial, end lemma le_mul_of_pos_right {m n : β„•} (h : 0 < n) : m ≀ m * n := begin conv {to_lhs, rw [← mul_one(m)]}, exact mul_le_mul_of_nonneg_left (nat.succ_le_of_lt h) dec_trivial, end theorem two_mul_ne_two_mul_add_one {n m} : 2 * n β‰  2 * m + 1 := mt (congr_arg (%2)) (by rw [add_comm, add_mul_mod_self_left, mul_mod_right]; exact dec_trivial) /-- Recursion principle based on `<`. -/ @[elab_as_eliminator] protected def strong_rec' {p : β„• β†’ Sort u} (H : βˆ€ n, (βˆ€ m, m < n β†’ p m) β†’ p n) : βˆ€ (n : β„•), p n | n := H n (Ξ» m hm, strong_rec' m) attribute [simp] nat.div_self protected lemma div_le_of_le_mul' {m n : β„•} {k} (h : m ≀ k * n) : m / k ≀ n := (eq_zero_or_pos k).elim (Ξ» k0, by rw [k0, nat.div_zero]; apply zero_le) (Ξ» k0, (decidable.mul_le_mul_left k0).1 $ calc k * (m / k) ≀ m % k + k * (m / k) : le_add_left _ _ ... = m : mod_add_div _ _ ... ≀ k * n : h) protected lemma div_le_self' (m n : β„•) : m / n ≀ m := (eq_zero_or_pos n).elim (Ξ» n0, by rw [n0, nat.div_zero]; apply zero_le) (Ξ» n0, nat.div_le_of_le_mul' $ calc m = 1 * m : (one_mul _).symm ... ≀ n * m : mul_le_mul_right _ n0) theorem le_div_iff_mul_le' {x y : β„•} {k : β„•} (k0 : 0 < k) : x ≀ y / k ↔ x * k ≀ y := begin revert x, refine nat.strong_rec' _ y, clear y, intros y IH x, cases decidable.lt_or_le y k with h h, { rw [div_eq_of_lt h], cases x with x, { simp [zero_mul, zero_le] }, { rw succ_mul, exact iff_of_false (not_succ_le_zero _) (not_le_of_lt $ lt_of_lt_of_le h (le_add_left _ _)) } }, { rw [div_eq_sub_div k0 h], cases x with x, { simp [zero_mul, zero_le] }, { rw [← add_one, nat.add_le_add_iff_le_right, succ_mul, IH _ (sub_lt_of_pos_le _ _ k0 h), add_le_to_le_sub _ h] } } end theorem div_mul_le_self' (m n : β„•) : m / n * n ≀ m := (nat.eq_zero_or_pos n).elim (Ξ» n0, by simp [n0, zero_le]) $ Ξ» n0, (le_div_iff_mul_le' n0).1 (le_refl _) theorem div_lt_iff_lt_mul' {x y : β„•} {k : β„•} (k0 : 0 < k) : x / k < y ↔ x < y * k := lt_iff_lt_of_le_iff_le $ le_div_iff_mul_le' k0 protected theorem div_le_div_right {n m : β„•} (h : n ≀ m) {k : β„•} : n / k ≀ m / k := (nat.eq_zero_or_pos k).elim (Ξ» k0, by simp [k0]) $ Ξ» hk, (le_div_iff_mul_le' hk).2 $ le_trans (nat.div_mul_le_self' _ _) h lemma lt_of_div_lt_div {m n k : β„•} (h : m / k < n / k) : m < n := by_contradiction $ Ξ» h₁, absurd h (not_lt_of_ge (nat.div_le_div_right (not_lt.1 h₁))) protected theorem eq_mul_of_div_eq_right {a b c : β„•} (H1 : b ∣ a) (H2 : a / b = c) : a = b * c := by rw [← H2, nat.mul_div_cancel' H1] protected theorem div_eq_iff_eq_mul_right {a b c : β„•} (H : 0 < b) (H' : b ∣ a) : a / b = c ↔ a = b * c := ⟨nat.eq_mul_of_div_eq_right H', nat.div_eq_of_eq_mul_right H⟩ protected theorem div_eq_iff_eq_mul_left {a b c : β„•} (H : 0 < b) (H' : b ∣ a) : a / b = c ↔ a = c * b := by rw mul_comm; exact nat.div_eq_iff_eq_mul_right H H' protected theorem eq_mul_of_div_eq_left {a b c : β„•} (H1 : b ∣ a) (H2 : a / b = c) : a = c * b := by rw [mul_comm, nat.eq_mul_of_div_eq_right H1 H2] protected theorem mul_div_cancel_left' {a b : β„•} (Hd : a ∣ b) : a * (b / a) = b := by rw [mul_comm,nat.div_mul_cancel Hd] protected theorem div_mod_unique {n k m d : β„•} (h : 0 < k) : n / k = d ∧ n % k = m ↔ m + k * d = n ∧ m < k := ⟨λ ⟨e₁, eβ‚‚βŸ©, e₁ β–Έ eβ‚‚ β–Έ ⟨mod_add_div _ _, mod_lt _ h⟩, Ξ» ⟨h₁, hβ‚‚βŸ©, h₁ β–Έ by rw [add_mul_div_left _ _ h, add_mul_mod_self_left]; simp [div_eq_of_lt, mod_eq_of_lt, hβ‚‚]⟩ lemma two_mul_odd_div_two {n : β„•} (hn : n % 2 = 1) : 2 * (n / 2) = n - 1 := by conv {to_rhs, rw [← nat.mod_add_div n 2, hn, nat.add_sub_cancel_left]} lemma div_dvd_of_dvd {a b : β„•} (h : b ∣ a) : (a / b) ∣ a := ⟨b, (nat.div_mul_cancel h).symm⟩ protected lemma div_pos {a b : β„•} (hba : b ≀ a) (hb : 0 < b) : 0 < a / b := nat.pos_of_ne_zero (Ξ» h, lt_irrefl a (calc a = a % b : by simpa [h] using (mod_add_div a b).symm ... < b : nat.mod_lt a hb ... ≀ a : hba)) protected theorem mul_right_inj {a b c : β„•} (ha : 0 < a) : b * a = c * a ↔ b = c := ⟨nat.eq_of_mul_eq_mul_right ha, Ξ» e, e β–Έ rfl⟩ protected theorem mul_left_inj {a b c : β„•} (ha : 0 < a) : a * b = a * c ↔ b = c := ⟨nat.eq_of_mul_eq_mul_left ha, Ξ» e, e β–Έ rfl⟩ protected lemma div_div_self : βˆ€ {a b : β„•}, b ∣ a β†’ 0 < a β†’ a / (a / b) = b | a 0 h₁ hβ‚‚ := by rw eq_zero_of_zero_dvd h₁; refl | 0 b h₁ hβ‚‚ := absurd hβ‚‚ dec_trivial | (a+1) (b+1) h₁ hβ‚‚ := (nat.mul_right_inj (nat.div_pos (le_of_dvd (succ_pos a) h₁) (succ_pos b))).1 $ by rw [nat.div_mul_cancel (div_dvd_of_dvd h₁), nat.mul_div_cancel' h₁] protected lemma div_lt_of_lt_mul {m n k : β„•} (h : m < n * k) : m / n < k := lt_of_mul_lt_mul_left (calc n * (m / n) ≀ m % n + n * (m / n) : nat.le_add_left _ _ ... = m : mod_add_div _ _ ... < n * k : h) (nat.zero_le n) lemma lt_mul_of_div_lt {a b c : β„•} (h : a / c < b) (w : 0 < c) : a < b * c := lt_of_not_ge $ not_le_of_gt h ∘ (nat.le_div_iff_mul_le _ _ w).2 protected lemma div_eq_zero_iff {a b : β„•} (hb : 0 < b) : a / b = 0 ↔ a < b := ⟨λ h, by rw [← mod_add_div a b, h, mul_zero, add_zero]; exact mod_lt _ hb, Ξ» h, by rw [← nat.mul_left_inj hb, ← @add_left_cancel_iff _ _ (a % b), mod_add_div, mod_eq_of_lt h, mul_zero, add_zero]⟩ lemma eq_zero_of_le_div {a b : β„•} (hb : 2 ≀ b) (h : a ≀ a / b) : a = 0 := eq_zero_of_mul_le hb $ by rw mul_comm; exact (nat.le_div_iff_mul_le' (lt_of_lt_of_le dec_trivial hb)).1 h lemma mul_div_le_mul_div_assoc (a b c : β„•) : a * (b / c) ≀ (a * b) / c := if hc0 : c = 0 then by simp [hc0] else (nat.le_div_iff_mul_le _ _ (nat.pos_of_ne_zero hc0)).2 (by rw [mul_assoc]; exact mul_le_mul_left _ (nat.div_mul_le_self _ _)) lemma div_mul_div_le_div (a b c : β„•) : ((a / c) * b) / a ≀ b / c := if ha0 : a = 0 then by simp [ha0] else calc a / c * b / a ≀ b * a / c / a : nat.div_le_div_right (by rw [mul_comm]; exact mul_div_le_mul_div_assoc _ _ _) ... = b / c : by rw [nat.div_div_eq_div_mul, mul_comm b, mul_comm c, nat.mul_div_mul _ _ (nat.pos_of_ne_zero ha0)] lemma eq_zero_of_le_half {a : β„•} (h : a ≀ a / 2) : a = 0 := eq_zero_of_le_div (le_refl _) h lemma mod_mul_right_div_self (a b c : β„•) : a % (b * c) / b = (a / b) % c := if hb : b = 0 then by simp [hb] else if hc : c = 0 then by simp [hc] else by conv {to_rhs, rw ← mod_add_div a (b * c)}; rw [mul_assoc, nat.add_mul_div_left _ _ (nat.pos_of_ne_zero hb), add_mul_mod_self_left, mod_eq_of_lt (nat.div_lt_of_lt_mul (mod_lt _ (mul_pos (nat.pos_of_ne_zero hb) (nat.pos_of_ne_zero hc))))] lemma mod_mul_left_div_self (a b c : β„•) : a % (c * b) / b = (a / b) % c := by rw [mul_comm c, mod_mul_right_div_self] /- The `n+1`-st triangle number is `n` more than the `n`-th triangle number -/ lemma triangle_succ (n : β„•) : (n + 1) * ((n + 1) - 1) / 2 = n * (n - 1) / 2 + n := begin rw [← add_mul_div_left, mul_comm 2 n, ← mul_add, nat.add_sub_cancel, mul_comm], cases n; refl, apply zero_lt_succ end @[simp] protected theorem dvd_one {n : β„•} : n ∣ 1 ↔ n = 1 := ⟨eq_one_of_dvd_one, Ξ» e, e.symm β–Έ dvd_refl _⟩ protected theorem dvd_add_left {k m n : β„•} (h : k ∣ n) : k ∣ m + n ↔ k ∣ m := (nat.dvd_add_iff_left h).symm protected theorem dvd_add_right {k m n : β„•} (h : k ∣ m) : k ∣ m + n ↔ k ∣ n := (nat.dvd_add_iff_right h).symm /-- A natural number m divides the sum m + n if and only if m divides b.-/ @[simp] protected lemma dvd_add_self_left {m n : β„•} : m ∣ m + n ↔ m ∣ n := nat.dvd_add_right (dvd_refl m) /-- A natural number m divides the sum n + m if and only if m divides b.-/ @[simp] protected lemma dvd_add_self_right {m n : β„•} : m ∣ n + m ↔ m ∣ n := nat.dvd_add_left (dvd_refl m) protected theorem mul_dvd_mul_iff_left {a b c : β„•} (ha : 0 < a) : a * b ∣ a * c ↔ b ∣ c := exists_congr $ Ξ» d, by rw [mul_assoc, nat.mul_left_inj ha] protected theorem mul_dvd_mul_iff_right {a b c : β„•} (hc : 0 < c) : a * c ∣ b * c ↔ a ∣ b := exists_congr $ Ξ» d, by rw [mul_right_comm, nat.mul_right_inj hc] lemma succ_div : βˆ€ (a b : β„•), (a + 1) / b = a / b + if b ∣ a + 1 then 1 else 0 | a 0 := by simp | 0 1 := rfl | 0 (b+2) := have hb2 : b + 2 > 1, from dec_trivial, by simp [ne_of_gt hb2, div_eq_of_lt hb2] | (a+1) (b+1) := begin rw [nat.div_def], conv_rhs { rw nat.div_def }, by_cases hb_eq_a : b = a + 1, { simp [hb_eq_a, le_refl] }, by_cases hb_le_a1 : b ≀ a + 1, { have hb_le_a : b ≀ a, from le_of_lt_succ (lt_of_le_of_ne hb_le_a1 hb_eq_a), have h₁ : (0 < b + 1 ∧ b + 1 ≀ a + 1 + 1), from ⟨succ_pos _, (add_le_add_iff_right _).2 hb_le_a1⟩, have hβ‚‚ : (0 < b + 1 ∧ b + 1 ≀ a + 1), from ⟨succ_pos _, (add_le_add_iff_right _).2 hb_le_a⟩, have dvd_iff : b + 1 ∣ a - b + 1 ↔ b + 1 ∣ a + 1 + 1, { rw [nat.dvd_add_iff_left (dvd_refl (b + 1)), ← nat.add_sub_add_right a 1 b, add_comm (_ - _), add_assoc, nat.sub_add_cancel (succ_le_succ hb_le_a), add_comm 1] }, have wf : a - b < a + 1, from lt_succ_of_le (nat.sub_le_self _ _), rw [if_pos h₁, if_pos hβ‚‚, nat.add_sub_add_right, nat.sub_add_comm hb_le_a, by exact have _ := wf, succ_div (a - b), nat.add_sub_add_right], simp [dvd_iff, succ_eq_add_one, add_comm 1] }, { have hba : Β¬ b ≀ a, from not_le_of_gt (lt_trans (lt_succ_self a) (lt_of_not_ge hb_le_a1)), have hb_dvd_a : Β¬ b + 1 ∣ a + 2, from Ξ» h, hb_le_a1 (le_of_succ_le_succ (le_of_dvd (succ_pos _) h)), simp [hba, hb_le_a1, hb_dvd_a], } end lemma succ_div_of_dvd {a b : β„•} (hba : b ∣ a + 1) : (a + 1) / b = a / b + 1 := by rw [succ_div, if_pos hba] lemma succ_div_of_not_dvd {a b : β„•} (hba : Β¬ b ∣ a + 1) : (a + 1) / b = a / b := by rw [succ_div, if_neg hba, add_zero] @[simp] theorem mod_mod_of_dvd (n : nat) {m k : nat} (h : m ∣ k) : n % k % m = n % m := begin conv { to_rhs, rw ←mod_add_div n k }, rcases h with ⟨t, rfl⟩, rw [mul_assoc, add_mul_mod_self_left] end @[simp] theorem mod_mod (a n : β„•) : (a % n) % n = a % n := (eq_zero_or_pos n).elim (Ξ» n0, by simp [n0]) (Ξ» npos, mod_eq_of_lt (mod_lt _ npos)) /-- If `a` and `b` are equal mod `c`, `a - b` is zero mod `c`. -/ lemma sub_mod_eq_zero_of_mod_eq {a b c : β„•} (h : a % c = b % c) : (a - b) % c = 0 := by rw [←nat.mod_add_div a c, ←nat.mod_add_div b c, ←h, ←nat.sub_sub, nat.add_sub_cancel_left, ←nat.mul_sub_left_distrib, nat.mul_mod_right] lemma dvd_sub_mod (k : β„•) : n ∣ (k - (k % n)) := ⟨k / n, nat.sub_eq_of_eq_add (nat.mod_add_div k n).symm⟩ lemma add_mod (a b n : β„•) : (a + b) % n = ((a % n) + (b % n)) % n := begin conv_lhs { rw [←mod_add_div a n, ←mod_add_div b n, ←add_assoc, add_mul_mod_self_left, add_assoc, add_comm _ (b % n), ←add_assoc, add_mul_mod_self_left] } end lemma mul_mod (a b n : β„•) : (a * b) % n = ((a % n) * (b % n)) % n := begin conv_lhs { rw [←mod_add_div a n, ←mod_add_div b n, right_distrib, left_distrib, left_distrib, mul_assoc, mul_assoc, ←left_distrib n _ _, add_mul_mod_self_left, mul_comm _ (n * (b / n)), mul_assoc, add_mul_mod_self_left] } end theorem add_pos_left {m : β„•} (h : 0 < m) (n : β„•) : 0 < m + n := calc m + n > 0 + n : nat.add_lt_add_right h n ... = n : nat.zero_add n ... β‰₯ 0 : zero_le n theorem add_pos_right (m : β„•) {n : β„•} (h : 0 < n) : 0 < m + n := begin rw add_comm, exact add_pos_left h m end theorem add_pos_iff_pos_or_pos (m n : β„•) : 0 < m + n ↔ 0 < m ∨ 0 < n := iff.intro begin intro h, cases m with m, {simp [zero_add] at h, exact or.inr h}, exact or.inl (succ_pos _) end begin intro h, cases h with mpos npos, { apply add_pos_left mpos }, apply add_pos_right _ npos end lemma add_eq_one_iff : βˆ€ {a b : β„•}, a + b = 1 ↔ (a = 0 ∧ b = 1) ∨ (a = 1 ∧ b = 0) | 0 0 := dec_trivial | 0 1 := dec_trivial | 1 0 := dec_trivial | 1 1 := dec_trivial | (a+2) _ := by rw add_right_comm; exact dec_trivial | _ (b+2) := by rw [← add_assoc]; simp only [nat.succ_inj', nat.succ_ne_zero]; simp lemma mul_eq_one_iff : βˆ€ {a b : β„•}, a * b = 1 ↔ a = 1 ∧ b = 1 | 0 0 := dec_trivial | 0 1 := dec_trivial | 1 0 := dec_trivial | (a+2) 0 := by simp | 0 (b+2) := by simp | (a+1) (b+1) := ⟨λ h, by simp only [add_mul, mul_add, mul_add, one_mul, mul_one, (add_assoc _ _ _).symm, nat.succ_inj', add_eq_zero_iff] at h; simp [h.1.2, h.2], by clear_aux_decl; finish⟩ lemma mul_right_eq_self_iff {a b : β„•} (ha : 0 < a) : a * b = a ↔ b = 1 := suffices a * b = a * 1 ↔ b = 1, by rwa mul_one at this, nat.mul_left_inj ha lemma mul_left_eq_self_iff {a b : β„•} (hb : 0 < b) : a * b = b ↔ a = 1 := by rw [mul_comm, nat.mul_right_eq_self_iff hb] lemma lt_succ_iff_lt_or_eq {n i : β„•} : n < i.succ ↔ (n < i ∨ n = i) := lt_succ_iff.trans le_iff_lt_or_eq theorem le_zero_iff {i : β„•} : i ≀ 0 ↔ i = 0 := ⟨nat.eq_zero_of_le_zero, assume h, h β–Έ le_refl i⟩ theorem le_add_one_iff {i j : β„•} : i ≀ j + 1 ↔ (i ≀ j ∨ i = j + 1) := ⟨assume h, match nat.eq_or_lt_of_le h with | or.inl h := or.inr h | or.inr h := or.inl $ nat.le_of_succ_le_succ h end, or.rec (assume h, le_trans h $ nat.le_add_right _ _) le_of_eq⟩ theorem mul_self_inj {n m : β„•} : n * n = m * m ↔ n = m := le_antisymm_iff.trans (le_antisymm_iff.trans (and_congr mul_self_le_mul_self_iff mul_self_le_mul_self_iff)).symm section facts -- Inject some simple facts into the typeclass system. -- This `fact` should not be confused with the factorial function `nat.fact`! instance succ_pos'' (n : β„•) : _root_.fact (0 < n.succ) := n.succ_pos instance pos_of_one_lt (n : β„•) [h : fact (1 < n)] : fact (0 < n) := lt_trans zero_lt_one h end facts instance decidable_ball_lt (n : nat) (P : Ξ  k < n, Prop) : βˆ€ [H : βˆ€ n h, decidable (P n h)], decidable (βˆ€ n h, P n h) := begin induction n with n IH; intro; resetI, { exact is_true (Ξ» n, dec_trivial) }, cases IH (Ξ» k h, P k (lt_succ_of_lt h)) with h, { refine is_false (mt _ h), intros hn k h, apply hn }, by_cases p : P n (lt_succ_self n), { exact is_true (Ξ» k h', (lt_or_eq_of_le $ le_of_lt_succ h').elim (h _) (Ξ» e, match k, e, h' with _, rfl, h := p end)) }, { exact is_false (mt (Ξ» hn, hn _ _) p) } end instance decidable_forall_fin {n : β„•} (P : fin n β†’ Prop) [H : decidable_pred P] : decidable (βˆ€ i, P i) := decidable_of_iff (βˆ€ k h, P ⟨k, h⟩) ⟨λ a ⟨k, h⟩, a k h, Ξ» a k h, a ⟨k, h⟩⟩ instance decidable_ball_le (n : β„•) (P : Ξ  k ≀ n, Prop) [H : βˆ€ n h, decidable (P n h)] : decidable (βˆ€ n h, P n h) := decidable_of_iff (βˆ€ k (h : k < succ n), P k (le_of_lt_succ h)) ⟨λ a k h, a k (lt_succ_of_le h), Ξ» a k h, a k _⟩ instance decidable_lo_hi (lo hi : β„•) (P : β„• β†’ Prop) [H : decidable_pred P] : decidable (βˆ€x, lo ≀ x β†’ x < hi β†’ P x) := decidable_of_iff (βˆ€ x < hi - lo, P (lo + x)) ⟨λal x hl hh, by have := al (x - lo) (lt_of_not_ge $ (not_congr (nat.sub_le_sub_right_iff _ _ _ hl)).2 $ not_le_of_gt hh); rwa [nat.add_sub_of_le hl] at this, Ξ»al x h, al _ (nat.le_add_right _ _) (nat.add_lt_of_lt_sub_left h)⟩ instance decidable_lo_hi_le (lo hi : β„•) (P : β„• β†’ Prop) [H : decidable_pred P] : decidable (βˆ€x, lo ≀ x β†’ x ≀ hi β†’ P x) := decidable_of_iff (βˆ€x, lo ≀ x β†’ x < hi + 1 β†’ P x) $ ball_congr $ Ξ» x hl, imp_congr lt_succ_iff iff.rfl protected theorem bit0_le {n m : β„•} (h : n ≀ m) : bit0 n ≀ bit0 m := add_le_add h h protected theorem bit1_le {n m : β„•} (h : n ≀ m) : bit1 n ≀ bit1 m := succ_le_succ (add_le_add h h) theorem bit_le : βˆ€ (b : bool) {n m : β„•}, n ≀ m β†’ bit b n ≀ bit b m | tt n m h := nat.bit1_le h | ff n m h := nat.bit0_le h theorem bit_ne_zero (b) {n} (h : n β‰  0) : bit b n β‰  0 := by cases b; [exact nat.bit0_ne_zero h, exact nat.bit1_ne_zero _] theorem bit0_le_bit : βˆ€ (b) {m n : β„•}, m ≀ n β†’ bit0 m ≀ bit b n | tt m n h := le_of_lt $ nat.bit0_lt_bit1 h | ff m n h := nat.bit0_le h theorem bit_le_bit1 : βˆ€ (b) {m n : β„•}, m ≀ n β†’ bit b m ≀ bit1 n | ff m n h := le_of_lt $ nat.bit0_lt_bit1 h | tt m n h := nat.bit1_le h theorem bit_lt_bit0 : βˆ€ (b) {n m : β„•}, n < m β†’ bit b n < bit0 m | tt n m h := nat.bit1_lt_bit0 h | ff n m h := nat.bit0_lt h theorem bit_lt_bit (a b) {n m : β„•} (h : n < m) : bit a n < bit b m := lt_of_lt_of_le (bit_lt_bit0 _ h) (bit0_le_bit _ (le_refl _)) @[simp] lemma bit0_le_bit1_iff : bit0 k ≀ bit1 n ↔ k ≀ n := ⟨λ h, by rwa [← nat.lt_succ_iff, n.bit1_eq_succ_bit0, ← n.bit0_succ_eq, bit0_lt_bit0, nat.lt_succ_iff] at h, Ξ» h, le_of_lt (nat.bit0_lt_bit1 h)⟩ @[simp] lemma bit0_lt_bit1_iff : bit0 k < bit1 n ↔ k ≀ n := ⟨λ h, bit0_le_bit1_iff.1 (le_of_lt h), nat.bit0_lt_bit1⟩ @[simp] lemma bit1_le_bit0_iff : bit1 k ≀ bit0 n ↔ k < n := ⟨λ h, by rwa [k.bit1_eq_succ_bit0, succ_le_iff, bit0_lt_bit0] at h, Ξ» h, le_of_lt (nat.bit1_lt_bit0 h)⟩ @[simp] lemma bit1_lt_bit0_iff : bit1 k < bit0 n ↔ k < n := ⟨λ h, bit1_le_bit0_iff.1 (le_of_lt h), nat.bit1_lt_bit0⟩ @[simp] lemma one_le_bit0_iff : 1 ≀ bit0 n ↔ 0 < n := by { convert bit1_le_bit0_iff, refl, } @[simp] lemma one_lt_bit0_iff : 1 < bit0 n ↔ 1 ≀ n := by { convert bit1_lt_bit0_iff, refl, } @[simp] lemma bit_le_bit_iff : βˆ€ {b : bool}, bit b k ≀ bit b n ↔ k ≀ n | ff := bit0_le_bit0 | tt := bit1_le_bit1 @[simp] lemma bit_lt_bit_iff : βˆ€ {b : bool}, bit b k < bit b n ↔ k < n | ff := bit0_lt_bit0 | tt := bit1_lt_bit1 @[simp] lemma bit_le_bit1_iff : βˆ€ {b : bool}, bit b k ≀ bit1 n ↔ k ≀ n | ff := bit0_le_bit1_iff | tt := bit1_le_bit1 lemma pos_of_bit0_pos {n : β„•} (h : 0 < bit0 n) : 0 < n := by { cases n, cases h, apply succ_pos, } /-- Define a function on `β„•` depending on parity of the argument. -/ @[elab_as_eliminator] def bit_cases {C : β„• β†’ Sort u} (H : Ξ  b n, C (bit b n)) (n : β„•) : C n := eq.rec_on n.bit_decomp (H (bodd n) (div2 n)) /- partial subtraction -/ /-- Partial predecessor operation. Returns `ppred n = some m` if `n = m + 1`, otherwise `none`. -/ @[simp] def ppred : β„• β†’ option β„• | 0 := none | (n+1) := some n /-- Partial subtraction operation. Returns `psub m n = some k` if `m = n + k`, otherwise `none`. -/ @[simp] def psub (m : β„•) : β„• β†’ option β„• | 0 := some m | (n+1) := psub n >>= ppred theorem pred_eq_ppred (n : β„•) : pred n = (ppred n).get_or_else 0 := by cases n; refl theorem sub_eq_psub (m : β„•) : βˆ€ n, m - n = (psub m n).get_or_else 0 | 0 := rfl | (n+1) := (pred_eq_ppred (m-n)).trans $ by rw [sub_eq_psub, psub]; cases psub m n; refl @[simp] theorem ppred_eq_some {m : β„•} : βˆ€ {n}, ppred n = some m ↔ succ m = n | 0 := by split; intro h; contradiction | (n+1) := by dsimp; split; intro h; injection h; subst n @[simp] theorem ppred_eq_none : βˆ€ {n : β„•}, ppred n = none ↔ n = 0 | 0 := by simp | (n+1) := by dsimp; split; contradiction theorem psub_eq_some {m : β„•} : βˆ€ {n k}, psub m n = some k ↔ k + n = m | 0 k := by simp [eq_comm] | (n+1) k := begin dsimp, apply option.bind_eq_some.trans, simp [psub_eq_some, add_comm, add_left_comm, nat.succ_eq_add_one] end theorem psub_eq_none (m n : β„•) : psub m n = none ↔ m < n := begin cases s : psub m n; simp [eq_comm], { show m < n, refine lt_of_not_ge (Ξ» h, _), cases le.dest h with k e, injection s.symm.trans (psub_eq_some.2 $ (add_comm _ _).trans e) }, { show n ≀ m, rw ← psub_eq_some.1 s, apply le_add_left } end theorem ppred_eq_pred {n} (h : 0 < n) : ppred n = some (pred n) := ppred_eq_some.2 $ succ_pred_eq_of_pos h theorem psub_eq_sub {m n} (h : n ≀ m) : psub m n = some (m - n) := psub_eq_some.2 $ nat.sub_add_cancel h theorem psub_add (m n k) : psub m (n + k) = do x ← psub m n, psub x k := by induction k; simp [*, add_succ, bind_assoc] /- pow -/ attribute [simp] nat.pow_zero nat.pow_one @[simp] lemma one_pow : βˆ€ n : β„•, 1 ^ n = 1 | 0 := rfl | (k+1) := show 1^k * 1 = 1, by rw [mul_one, one_pow] theorem pow_add (a m n : β„•) : a^(m + n) = a^m * a^n := by induction n; simp [*, pow_succ, mul_assoc] theorem pow_two (a : β„•) : a ^ 2 = a * a := show (1 * a) * a = _, by rw one_mul theorem pow_dvd_pow (a : β„•) {m n : β„•} (h : m ≀ n) : a^m ∣ a^n := by rw [← nat.add_sub_cancel' h, pow_add]; apply dvd_mul_right theorem pow_dvd_pow_of_dvd {a b : β„•} (h : a ∣ b) : βˆ€ n:β„•, a^n ∣ b^n | 0 := dvd_refl _ | (n+1) := mul_dvd_mul (pow_dvd_pow_of_dvd n) h theorem mul_pow (a b n : β„•) : (a * b) ^ n = a ^ n * b ^ n := by induction n; simp [*, nat.pow_succ, mul_comm, mul_assoc, mul_left_comm] protected theorem pow_mul (a b n : β„•) : n ^ (a * b) = (n ^ a) ^ b := by induction b; simp [*, nat.succ_eq_add_one, nat.pow_add, mul_add, mul_comm] theorem pow_pos {p : β„•} (hp : 0 < p) : βˆ€ n : β„•, 0 < p ^ n | 0 := by simp | (k+1) := mul_pos (pow_pos _) hp lemma pow_eq_mul_pow_sub (p : β„•) {m n : β„•} (h : m ≀ n) : p ^ m * p ^ (n - m) = p ^ n := by rw [←nat.pow_add, nat.add_sub_cancel' h] lemma pow_lt_pow_succ {p : β„•} (h : 1 < p) (n : β„•) : p^n < p^(n+1) := suffices p^n*1 < p^n*p, by simpa, nat.mul_lt_mul_of_pos_left h (nat.pow_pos (lt_of_succ_lt h) n) lemma lt_pow_self {p : β„•} (h : 1 < p) : βˆ€ n : β„•, n < p ^ n | 0 := by simp [zero_lt_one] | (n+1) := calc n + 1 < p^n + 1 : nat.add_lt_add_right (lt_pow_self _) _ ... ≀ p ^ (n+1) : pow_lt_pow_succ h _ lemma pow_right_strict_mono {x : β„•} (k : 2 ≀ x) : strict_mono (nat.pow x) := Ξ» _ _, pow_lt_pow_of_lt_right k lemma pow_le_iff_le_right {x m n : β„•} (k : 2 ≀ x) : x^m ≀ x^n ↔ m ≀ n := strict_mono.le_iff_le (pow_right_strict_mono k) lemma pow_lt_iff_lt_right {x m n : β„•} (k : 2 ≀ x) : x^m < x^n ↔ m < n := strict_mono.lt_iff_lt (pow_right_strict_mono k) lemma pow_right_injective {x : β„•} (k : 2 ≀ x) : function.injective (nat.pow x) := strict_mono.injective (pow_right_strict_mono k) lemma pow_left_strict_mono {m : β„•} (k : 1 ≀ m) : strict_mono (Ξ» (x : β„•), x^m) := Ξ» _ _ h, pow_lt_pow_of_lt_left h k lemma pow_le_iff_le_left {m x y : β„•} (k : 1 ≀ m) : x^m ≀ y^m ↔ x ≀ y := strict_mono.le_iff_le (pow_left_strict_mono k) lemma pow_lt_iff_lt_left {m x y : β„•} (k : 1 ≀ m) : x^m < y^m ↔ x < y := strict_mono.lt_iff_lt (pow_left_strict_mono k) lemma pow_left_injective {m : β„•} (k : 1 ≀ m) : function.injective (Ξ» (x : β„•), x^m) := strict_mono.injective (pow_left_strict_mono k) lemma not_pos_pow_dvd : βˆ€ {p k : β„•} (hp : 1 < p) (hk : 1 < k), Β¬ p^k ∣ p | (succ p) (succ k) hp hk h := have (succ p)^k * succ p ∣ 1 * succ p, by simpa, have (succ p) ^ k ∣ 1, from dvd_of_mul_dvd_mul_right (succ_pos _) this, have he : (succ p) ^ k = 1, from eq_one_of_dvd_one this, have k < (succ p) ^ k, from lt_pow_self hp k, have k < 1, by rwa [he] at this, have k = 0, from eq_zero_of_le_zero $ le_of_lt_succ this, have 1 < 1, by rwa [this] at hk, absurd this dec_trivial @[simp] theorem bodd_div2_eq (n : β„•) : bodd_div2 n = (bodd n, div2 n) := by unfold bodd div2; cases bodd_div2 n; refl @[simp] lemma bodd_bit0 (n) : bodd (bit0 n) = ff := bodd_bit ff n @[simp] lemma bodd_bit1 (n) : bodd (bit1 n) = tt := bodd_bit tt n @[simp] lemma div2_bit0 (n) : div2 (bit0 n) = n := div2_bit ff n @[simp] lemma div2_bit1 (n) : div2 (bit1 n) = n := div2_bit tt n /- iterate -/ section variables {Ξ± : Sort*} (op : Ξ± β†’ Ξ±) @[simp] theorem iterate_zero (a : Ξ±) : op^[0] a = a := rfl @[simp] theorem iterate_succ (n : β„•) (a : Ξ±) : op^[succ n] a = (op^[n]) (op a) := rfl theorem iterate_add : βˆ€ (m n : β„•) (a : Ξ±), op^[m + n] a = (op^[m]) (op^[n] a) | m 0 a := rfl | m (succ n) a := iterate_add m n _ @[simp] theorem iterate_one : op^[1] = op := funext $ Ξ» a, rfl theorem iterate_succ' (n : β„•) (a : Ξ±) : op^[succ n] a = op (op^[n] a) := by rw [← one_add, iterate_add, iterate_one] lemma iterate_mul (m : β„•) : βˆ€ n, op^[m * n] = (op^[m]^[n]) | 0 := by { ext a, simp only [mul_zero, iterate_zero] } | (n + 1) := by { ext x, simp only [mul_add, mul_one, iterate_one, iterate_add, iterate_mul n] } @[elab_as_eliminator] theorem iterate_ind {Ξ± : Type u} (f : Ξ± β†’ Ξ±) {p : (Ξ± β†’ Ξ±) β†’ Prop} (hf : p f) (hid : p id) (hcomp : βˆ€ ⦃f g⦄, p f β†’ p g β†’ p (f ∘ g)) : βˆ€ n, p (f^[n]) | 0 := hid | (n+1) := hcomp (iterate_ind n) hf theorem iterateβ‚€ {Ξ± : Type u} {op : Ξ± β†’ Ξ±} {x : Ξ±} (H : op x = x) {n : β„•} : op^[n] x = x := by induction n; [simp only [iterate_zero], simp only [iterate_succ', H, *]] theorem iterate₁ {Ξ± : Type u} {Ξ² : Type v} {op : Ξ± β†’ Ξ±} {op' : Ξ² β†’ Ξ²} {op'' : Ξ± β†’ Ξ²} (H : βˆ€ x, op' (op'' x) = op'' (op x)) {n : β„•} {x : Ξ±} : op'^[n] (op'' x) = op'' (op^[n] x) := by induction n; [simp only [iterate_zero], simp only [iterate_succ', H, *]] theorem iterateβ‚‚ {Ξ± : Type u} {op : Ξ± β†’ Ξ±} {op' : Ξ± β†’ Ξ± β†’ Ξ±} (H : βˆ€ x y, op (op' x y) = op' (op x) (op y)) {n : β„•} {x y : Ξ±} : op^[n] (op' x y) = op' (op^[n] x) (op^[n] y) := by induction n; [simp only [iterate_zero], simp only [iterate_succ', H, *]] theorem iterate_cancel {Ξ± : Type u} {op op' : Ξ± β†’ Ξ±} (H : βˆ€ x, op (op' x) = x) {n : β„•} {x : Ξ±} : op^[n] (op'^[n] x) = x := by induction n; [refl, rwa [iterate_succ, iterate_succ', H]] end /- size and shift -/ theorem shiftl'_ne_zero_left (b) {m} (h : m β‰  0) (n) : shiftl' b m n β‰  0 := by induction n; simp [shiftl', bit_ne_zero, *] theorem shiftl'_tt_ne_zero (m) : βˆ€ {n} (h : n β‰  0), shiftl' tt m n β‰  0 | 0 h := absurd rfl h | (succ n) _ := nat.bit1_ne_zero _ @[simp] theorem size_zero : size 0 = 0 := rfl @[simp] theorem size_bit {b n} (h : bit b n β‰  0) : size (bit b n) = succ (size n) := begin rw size, conv { to_lhs, rw [binary_rec], simp [h] }, rw div2_bit, end @[simp] theorem size_bit0 {n} (h : n β‰  0) : size (bit0 n) = succ (size n) := @size_bit ff n (nat.bit0_ne_zero h) @[simp] theorem size_bit1 (n) : size (bit1 n) = succ (size n) := @size_bit tt n (nat.bit1_ne_zero n) @[simp] theorem size_one : size 1 = 1 := by apply size_bit1 0 @[simp] theorem size_shiftl' {b m n} (h : shiftl' b m n β‰  0) : size (shiftl' b m n) = size m + n := begin induction n with n IH; simp [shiftl'] at h ⊒, rw [size_bit h, nat.add_succ], by_cases s0 : shiftl' b m n = 0; [skip, rw [IH s0]], rw s0 at h ⊒, cases b, {exact absurd rfl h}, have : shiftl' tt m n + 1 = 1 := congr_arg (+1) s0, rw [shiftl'_tt_eq_mul_pow] at this, have m0 := succ_inj (eq_one_of_dvd_one ⟨_, this.symm⟩), subst m0, simp at this, have : n = 0 := eq_zero_of_le_zero (le_of_not_gt $ Ξ» hn, ne_of_gt (pow_lt_pow_of_lt_right dec_trivial hn) this), subst n, refl end @[simp] theorem size_shiftl {m} (h : m β‰  0) (n) : size (shiftl m n) = size m + n := size_shiftl' (shiftl'_ne_zero_left _ h _) theorem lt_size_self (n : β„•) : n < 2^size n := begin rw [← one_shiftl], have : βˆ€ {n}, n = 0 β†’ n < shiftl 1 (size n) := Ξ» n e, by subst e; exact dec_trivial, apply binary_rec _ _ n, {apply this rfl}, intros b n IH, by_cases bit b n = 0, {apply this h}, rw [size_bit h, shiftl_succ], exact bit_lt_bit0 _ IH end theorem size_le {m n : β„•} : size m ≀ n ↔ m < 2^n := ⟨λ h, lt_of_lt_of_le (lt_size_self _) (pow_le_pow_of_le_right dec_trivial h), begin rw [← one_shiftl], revert n, apply binary_rec _ _ m, { intros n h, apply zero_le }, { intros b m IH n h, by_cases e : bit b m = 0, { rw e, apply zero_le }, rw [size_bit e], cases n with n, { exact e.elim (eq_zero_of_le_zero (le_of_lt_succ h)) }, { apply succ_le_succ (IH _), apply lt_imp_lt_of_le_imp_le (Ξ» h', bit0_le_bit _ h') h } } end⟩ theorem lt_size {m n : β„•} : m < size n ↔ 2^m ≀ n := by rw [← not_lt, iff_not_comm, not_lt, size_le] theorem size_pos {n : β„•} : 0 < size n ↔ 0 < n := by rw lt_size; refl theorem size_eq_zero {n : β„•} : size n = 0 ↔ n = 0 := by have := @size_pos n; simp [pos_iff_ne_zero] at this; exact not_iff_not.1 this theorem size_pow {n : β„•} : size (2^n) = n+1 := le_antisymm (size_le.2 $ pow_lt_pow_of_lt_right dec_trivial (lt_succ_self _)) (lt_size.2 $ le_refl _) theorem size_le_size {m n : β„•} (h : m ≀ n) : size m ≀ size n := size_le.2 $ lt_of_le_of_lt h (lt_size_self _) /- factorial -/ /-- `fact n` is the factorial of `n`. -/ @[simp] def fact : nat β†’ nat | 0 := 1 | (succ n) := succ n * fact n @[simp] theorem fact_zero : fact 0 = 1 := rfl @[simp] theorem fact_succ (n) : fact (succ n) = succ n * fact n := rfl @[simp] theorem fact_one : fact 1 = 1 := rfl theorem fact_pos : βˆ€ n, 0 < fact n | 0 := zero_lt_one | (succ n) := mul_pos (succ_pos _) (fact_pos n) theorem fact_ne_zero (n : β„•) : fact n β‰  0 := ne_of_gt (fact_pos _) theorem fact_dvd_fact {m n} (h : m ≀ n) : fact m ∣ fact n := begin induction n with n IH; simp, { have := eq_zero_of_le_zero h, subst m, simp }, { cases eq_or_lt_of_le h with he hl, { subst m, simp }, { apply dvd_mul_of_dvd_right (IH (le_of_lt_succ hl)) } } end theorem dvd_fact : βˆ€ {m n}, 0 < m β†’ m ≀ n β†’ m ∣ fact n | (succ m) n _ h := dvd_of_mul_right_dvd (fact_dvd_fact h) theorem fact_le {m n} (h : m ≀ n) : fact m ≀ fact n := le_of_dvd (fact_pos _) (fact_dvd_fact h) lemma fact_mul_pow_le_fact : βˆ€ {m n : β„•}, m.fact * m.succ ^ n ≀ (m + n).fact | m 0 := by simp | m (n+1) := by rw [← add_assoc, nat.fact_succ, mul_comm (nat.succ _), nat.pow_succ, ← mul_assoc]; exact mul_le_mul fact_mul_pow_le_fact (nat.succ_le_succ (nat.le_add_right _ _)) (nat.zero_le _) (nat.zero_le _) lemma monotone_fact : monotone fact := Ξ» n m, fact_le lemma fact_lt (h0 : 0 < n) : n.fact < m.fact ↔ n < m := begin split; intro h, { rw [← not_le], intro hmn, apply not_le_of_lt h (fact_le hmn) }, { have : βˆ€(n : β„•), 0 < n β†’ n.fact < n.succ.fact, { intros k hk, rw [fact_succ, succ_mul, lt_add_iff_pos_left], apply mul_pos hk (fact_pos k) }, induction h generalizing h0, { exact this _ h0, }, { refine lt_trans (h_ih h0) (this _ _), exact lt_trans h0 (lt_of_succ_le h_a) }} end lemma one_lt_fact : 1 < n.fact ↔ 1 < n := by { convert fact_lt _, refl, exact one_pos } lemma fact_eq_one : n.fact = 1 ↔ n ≀ 1 := begin split; intro h, { rw [← not_lt, ← one_lt_fact, h], apply lt_irrefl }, { cases h with h h, refl, cases h, refl } end lemma fact_inj (h0 : 1 < n.fact) : n.fact = m.fact ↔ n = m := begin split; intro h, { rcases lt_trichotomy n m with hnm|hnm|hnm, { exfalso, rw [← fact_lt, h] at hnm, exact lt_irrefl _ hnm, rw [one_lt_fact] at h0, exact lt_trans one_pos h0 }, { exact hnm }, { exfalso, rw [← fact_lt, h] at hnm, exact lt_irrefl _ hnm, rw [h, one_lt_fact] at h0, exact lt_trans one_pos h0 }}, { rw h } end /- choose -/ /-- `choose n k` is the number of `k`-element subsets in an `n`-element set. Also known as binomial coefficients. -/ def choose : β„• β†’ β„• β†’ β„• | _ 0 := 1 | 0 (k + 1) := 0 | (n + 1) (k + 1) := choose n k + choose n (k + 1) @[simp] lemma choose_zero_right (n : β„•) : choose n 0 = 1 := by cases n; refl @[simp] lemma choose_zero_succ (k : β„•) : choose 0 (succ k) = 0 := rfl lemma choose_succ_succ (n k : β„•) : choose (succ n) (succ k) = choose n k + choose n (succ k) := rfl lemma choose_eq_zero_of_lt : βˆ€ {n k}, n < k β†’ choose n k = 0 | _ 0 hk := absurd hk dec_trivial | 0 (k + 1) hk := choose_zero_succ _ | (n + 1) (k + 1) hk := have hnk : n < k, from lt_of_succ_lt_succ hk, have hnk1 : n < k + 1, from lt_of_succ_lt hk, by rw [choose_succ_succ, choose_eq_zero_of_lt hnk, choose_eq_zero_of_lt hnk1] @[simp] lemma choose_self (n : β„•) : choose n n = 1 := by induction n; simp [*, choose, choose_eq_zero_of_lt (lt_succ_self _)] @[simp] lemma choose_succ_self (n : β„•) : choose n (succ n) = 0 := choose_eq_zero_of_lt (lt_succ_self _) @[simp] lemma choose_one_right (n : β„•) : choose n 1 = n := by induction n; simp [*, choose, add_comm] /-- `choose n 2` is the `n`-th triangle number. -/ lemma choose_two_right (n : β„•) : choose n 2 = n * (n - 1) / 2 := begin induction n with n ih, simp, {rw triangle_succ n, simp [choose, ih], rw add_comm}, end lemma choose_pos : βˆ€ {n k}, k ≀ n β†’ 0 < choose n k | 0 _ hk := by rw [eq_zero_of_le_zero hk]; exact dec_trivial | (n + 1) 0 hk := by simp; exact dec_trivial | (n + 1) (k + 1) hk := by rw choose_succ_succ; exact add_pos_of_pos_of_nonneg (choose_pos (le_of_succ_le_succ hk)) (nat.zero_le _) lemma succ_mul_choose_eq : βˆ€ n k, succ n * choose n k = choose (succ n) (succ k) * succ k | 0 0 := dec_trivial | 0 (k + 1) := by simp [choose] | (n + 1) 0 := by simp | (n + 1) (k + 1) := by rw [choose_succ_succ (succ n) (succ k), add_mul, ←succ_mul_choose_eq, mul_succ, ←succ_mul_choose_eq, add_right_comm, ←mul_add, ←choose_succ_succ, ←succ_mul] lemma choose_mul_fact_mul_fact : βˆ€ {n k}, k ≀ n β†’ choose n k * fact k * fact (n - k) = fact n | 0 _ hk := by simp [eq_zero_of_le_zero hk] | (n + 1) 0 hk := by simp | (n + 1) (succ k) hk := begin cases lt_or_eq_of_le hk with hk₁ hk₁, { have h : choose n k * fact (succ k) * fact (n - k) = succ k * fact n := by rw ← choose_mul_fact_mul_fact (le_of_succ_le_succ hk); simp [fact_succ, mul_comm, mul_left_comm], have h₁ : fact (n - k) = (n - k) * fact (n - succ k) := by rw [← succ_sub_succ, succ_sub (le_of_lt_succ hk₁), fact_succ], have hβ‚‚ : choose n (succ k) * fact (succ k) * ((n - k) * fact (n - succ k)) = (n - k) * fact n := by rw ← choose_mul_fact_mul_fact (le_of_lt_succ hk₁); simp [fact_succ, mul_comm, mul_left_comm, mul_assoc], have h₃ : k * fact n ≀ n * fact n := mul_le_mul_right _ (le_of_succ_le_succ hk), rw [choose_succ_succ, add_mul, add_mul, succ_sub_succ, h, h₁, hβ‚‚, ← add_one, add_mul, nat.mul_sub_right_distrib, fact_succ, ← nat.add_sub_assoc h₃, add_assoc, ← add_mul, nat.add_sub_cancel_left, add_comm] }, { simp [hk₁, mul_comm, choose, nat.sub_self] } end theorem choose_eq_fact_div_fact {n k : β„•} (hk : k ≀ n) : choose n k = fact n / (fact k * fact (n - k)) := begin have : fact n = choose n k * (fact k * fact (n - k)) := by rw ← mul_assoc; exact (choose_mul_fact_mul_fact hk).symm, exact (nat.div_eq_of_eq_mul_left (mul_pos (fact_pos _) (fact_pos _)) this).symm end theorem fact_mul_fact_dvd_fact {n k : β„•} (hk : k ≀ n) : fact k * fact (n - k) ∣ fact n := by rw [←choose_mul_fact_mul_fact hk, mul_assoc]; exact dvd_mul_left _ _ @[simp] lemma choose_symm {n k : β„•} (hk : k ≀ n) : choose n (n-k) = choose n k := by rw [choose_eq_fact_div_fact hk, choose_eq_fact_div_fact (sub_le _ _), nat.sub_sub_self hk, mul_comm] lemma choose_symm_of_eq_add {n a b : β„•} (h : n = a + b) : nat.choose n a = nat.choose n b := by { convert nat.choose_symm (nat.le_add_left _ _), rw nat.add_sub_cancel} lemma choose_symm_add {a b : β„•} : choose (a+b) a = choose (a+b) b := choose_symm_of_eq_add rfl lemma choose_succ_right_eq (n k : β„•) : choose n (k + 1) * (k + 1) = choose n k * (n - k) := begin have e : (n+1) * choose n k = choose n k * (k+1) + choose n (k+1) * (k+1), rw [← right_distrib, ← choose_succ_succ, succ_mul_choose_eq], rw [← nat.sub_eq_of_eq_add e, mul_comm, ← nat.mul_sub_left_distrib, nat.add_sub_add_right] end @[simp] lemma choose_succ_self_right : βˆ€ (n:β„•), (n+1).choose n = n+1 | 0 := rfl | (n+1) := by rw [choose_succ_succ, choose_succ_self_right, choose_self] lemma choose_mul_succ_eq (n k : β„•) : (n.choose k) * (n + 1) = ((n+1).choose k) * (n + 1 - k) := begin induction k with k ih, { simp }, by_cases hk : n < k + 1, { rw [choose_eq_zero_of_lt hk, sub_eq_zero_of_le hk, zero_mul, mul_zero] }, push_neg at hk, replace hk : k + 1 ≀ n + 1 := _root_.le_add_right hk, rw [choose_succ_succ], rw [add_mul, succ_sub_succ], rw [← choose_succ_right_eq], rw [← succ_sub_succ, nat.mul_sub_left_distrib], symmetry, apply nat.add_sub_cancel', exact mul_le_mul_left _ hk, end section find_greatest /-- `find_greatest P b` is the largest `i ≀ bound` such that `P i` holds, or `0` if no such `i` exists -/ protected def find_greatest (P : β„• β†’ Prop) [decidable_pred P] : β„• β†’ β„• | 0 := 0 | (n + 1) := if P (n + 1) then n + 1 else find_greatest n variables {P : β„• β†’ Prop} [decidable_pred P] @[simp] lemma find_greatest_zero : nat.find_greatest P 0 = 0 := rfl @[simp] lemma find_greatest_eq : βˆ€{b}, P b β†’ nat.find_greatest P b = b | 0 h := rfl | (n + 1) h := by simp [nat.find_greatest, h] @[simp] lemma find_greatest_of_not {b} (h : Β¬ P (b + 1)) : nat.find_greatest P (b + 1) = nat.find_greatest P b := by simp [nat.find_greatest, h] lemma find_greatest_spec_and_le : βˆ€{b m}, m ≀ b β†’ P m β†’ P (nat.find_greatest P b) ∧ m ≀ nat.find_greatest P b | 0 m hm hP := have m = 0, from le_antisymm hm (nat.zero_le _), show P 0 ∧ m ≀ 0, from this β–Έ ⟨hP, le_refl _⟩ | (b + 1) m hm hP := begin by_cases h : P (b + 1), { simp [h, hm] }, { have : m β‰  b + 1 := assume this, h $ this β–Έ hP, have : m ≀ b := (le_of_not_gt $ assume h : b + 1 ≀ m, this $ le_antisymm hm h), have : P (nat.find_greatest P b) ∧ m ≀ nat.find_greatest P b := find_greatest_spec_and_le this hP, simp [h, this] } end lemma find_greatest_spec {b} : (βˆƒm, m ≀ b ∧ P m) β†’ P (nat.find_greatest P b) | ⟨m, hmb, hm⟩ := (find_greatest_spec_and_le hmb hm).1 lemma find_greatest_le : βˆ€ {b}, nat.find_greatest P b ≀ b | 0 := le_refl _ | (b + 1) := have nat.find_greatest P b ≀ b + 1, from le_trans find_greatest_le (nat.le_succ b), by by_cases P (b + 1); simp [h, this] lemma le_find_greatest {b m} (hmb : m ≀ b) (hm : P m) : m ≀ nat.find_greatest P b := (find_greatest_spec_and_le hmb hm).2 lemma find_greatest_is_greatest {P : β„• β†’ Prop} [decidable_pred P] {b} : (βˆƒ m, m ≀ b ∧ P m) β†’ βˆ€ k, nat.find_greatest P b < k ∧ k ≀ b β†’ Β¬ P k | ⟨m, hmb, hP⟩ k ⟨hk, hkb⟩ hPk := lt_irrefl k $ lt_of_le_of_lt (le_find_greatest hkb hPk) hk lemma find_greatest_eq_zero {P : β„• β†’ Prop} [decidable_pred P] : βˆ€ {b}, (βˆ€ n ≀ b, Β¬ P n) β†’ nat.find_greatest P b = 0 | 0 h := find_greatest_zero | (n + 1) h := begin have := nat.find_greatest_of_not (h (n + 1) (le_refl _)), rw this, exact find_greatest_eq_zero (assume k hk, h k (le_trans hk $ nat.le_succ _)) end lemma find_greatest_of_ne_zero {P : β„• β†’ Prop} [decidable_pred P] : βˆ€ {b m}, nat.find_greatest P b = m β†’ m β‰  0 β†’ P m | 0 m rfl h := by { have := @find_greatest_zero P _, contradiction } | (b + 1) m rfl h := decidable.by_cases (assume hb : P (b + 1), by { have := find_greatest_eq hb, rw this, exact hb }) (assume hb : Β¬ P (b + 1), find_greatest_of_ne_zero (find_greatest_of_not hb).symm h) end find_greatest section div lemma dvd_div_of_mul_dvd {a b c : β„•} (h : a * b ∣ c) : b ∣ c / a := if ha : a = 0 then by simp [ha] else have ha : 0 < a, from nat.pos_of_ne_zero ha, have h1 : βˆƒ d, c = a * b * d, from h, let ⟨d, hd⟩ := h1 in have hac : a ∣ c, from dvd_of_mul_right_dvd h, have h2 : c / a = b * d, from nat.div_eq_of_eq_mul_right ha (by simpa [mul_assoc] using hd), show βˆƒ d, c / a = b * d, from ⟨d, h2⟩ lemma mul_dvd_of_dvd_div {a b c : β„•} (hab : c ∣ b) (h : a ∣ b / c) : c * a ∣ b := have h1 : βˆƒ d, b / c = a * d, from h, have h2 : βˆƒ e, b = c * e, from hab, let ⟨d, hd⟩ := h1, ⟨e, he⟩ := h2 in have h3 : b = a * d * c, from nat.eq_mul_of_div_eq_left hab hd, show βˆƒ d, b = c * a * d, from ⟨d, by cc⟩ lemma div_mul_div {a b c d : β„•} (hab : b ∣ a) (hcd : d ∣ c) : (a / b) * (c / d) = (a * c) / (b * d) := have exi1 : βˆƒ x, a = b * x, from hab, have exi2 : βˆƒ y, c = d * y, from hcd, if hb : b = 0 then by simp [hb] else have 0 < b, from nat.pos_of_ne_zero hb, if hd : d = 0 then by simp [hd] else have 0 < d, from nat.pos_of_ne_zero hd, begin cases exi1 with x hx, cases exi2 with y hy, rw [hx, hy, nat.mul_div_cancel_left, nat.mul_div_cancel_left], symmetry, apply nat.div_eq_of_eq_mul_left, apply mul_pos, repeat {assumption}, cc end lemma pow_dvd_of_le_of_pow_dvd {p m n k : β„•} (hmn : m ≀ n) (hdiv : p ^ n ∣ k) : p ^ m ∣ k := have p ^ m ∣ p ^ n, from pow_dvd_pow _ hmn, dvd_trans this hdiv lemma dvd_of_pow_dvd {p k m : β„•} (hk : 1 ≀ k) (hpk : p^k ∣ m) : p ∣ m := by rw ←nat.pow_one p; exact pow_dvd_of_le_of_pow_dvd hk hpk lemma eq_of_dvd_of_div_eq_one {a b : β„•} (w : a ∣ b) (h : b / a = 1) : a = b := by rw [←nat.div_mul_cancel w, h, one_mul] lemma eq_zero_of_dvd_of_div_eq_zero {a b : β„•} (w : a ∣ b) (h : b / a = 0) : b = 0 := by rw [←nat.div_mul_cancel w, h, zero_mul] /-- If a small natural number is divisible by a larger natural number, the small number is zero. -/ lemma eq_zero_of_dvd_of_lt {a b : β„•} (w : a ∣ b) (h : b < a) : b = 0 := nat.eq_zero_of_dvd_of_div_eq_zero w ((nat.div_eq_zero_iff (lt_of_le_of_lt (zero_le b) h)).elim_right h) lemma div_le_div_left {a b c : β„•} (h₁ : c ≀ b) (hβ‚‚ : 0 < c) : a / b ≀ a / c := (nat.le_div_iff_mul_le _ _ hβ‚‚).2 $ le_trans (mul_le_mul_left _ h₁) (div_mul_le_self _ _) lemma div_eq_self {a b : β„•} : a / b = a ↔ a = 0 ∨ b = 1 := begin split, { intro, cases b, { simp * at * }, { cases b, { right, refl }, { left, have : a / (b + 2) ≀ a / 2 := div_le_div_left (by simp) dec_trivial, refine eq_zero_of_le_half _, simp * at * } } }, { rintros (rfl|rfl); simp } end end div lemma exists_eq_add_of_le : βˆ€ {m n : β„•}, m ≀ n β†’ βˆƒ k : β„•, n = m + k | 0 0 h := ⟨0, by simp⟩ | 0 (n+1) h := ⟨n+1, by simp⟩ | (m+1) (n+1) h := let ⟨k, hk⟩ := exists_eq_add_of_le (nat.le_of_succ_le_succ h) in ⟨k, by simp [hk, add_comm, add_left_comm]⟩ lemma exists_eq_add_of_lt : βˆ€ {m n : β„•}, m < n β†’ βˆƒ k : β„•, n = m + k + 1 | 0 0 h := false.elim $ lt_irrefl _ h | 0 (n+1) h := ⟨n, by simp⟩ | (m+1) (n+1) h := let ⟨k, hk⟩ := exists_eq_add_of_le (nat.le_of_succ_le_succ h) in ⟨k, by simp [hk]⟩ lemma with_bot.add_eq_zero_iff : βˆ€ {n m : with_bot β„•}, n + m = 0 ↔ n = 0 ∧ m = 0 | none m := iff_of_false dec_trivial (Ξ» h, absurd h.1 dec_trivial) | n none := iff_of_false (by cases n; exact dec_trivial) (Ξ» h, absurd h.2 dec_trivial) | (some n) (some m) := show (n + m : with_bot β„•) = (0 : β„•) ↔ (n : with_bot β„•) = (0 : β„•) ∧ (m : with_bot β„•) = (0 : β„•), by rw [← with_bot.coe_add, with_bot.coe_eq_coe, with_bot.coe_eq_coe, with_bot.coe_eq_coe, add_eq_zero_iff' (nat.zero_le _) (nat.zero_le _)] lemma with_bot.add_eq_one_iff : βˆ€ {n m : with_bot β„•}, n + m = 1 ↔ (n = 0 ∧ m = 1) ∨ (n = 1 ∧ m = 0) | none none := dec_trivial | none (some m) := dec_trivial | (some n) none := iff_of_false dec_trivial (Ξ» h, h.elim (Ξ» h, absurd h.2 dec_trivial) (Ξ» h, absurd h.2 dec_trivial)) | (some n) (some 0) := by erw [with_bot.coe_eq_coe, with_bot.coe_eq_coe, with_bot.coe_eq_coe, with_bot.coe_eq_coe]; simp | (some n) (some (m + 1)) := by erw [with_bot.coe_eq_coe, with_bot.coe_eq_coe, with_bot.coe_eq_coe, with_bot.coe_eq_coe, with_bot.coe_eq_coe]; simp [nat.add_succ, nat.succ_inj', nat.succ_ne_zero] -- induction /-- Induction principle starting at a non-zero number. For maps to a `Sort*` see `le_rec_on`. -/ @[elab_as_eliminator] lemma le_induction {P : nat β†’ Prop} {m} (h0 : P m) (h1 : βˆ€ n, m ≀ n β†’ P n β†’ P (n + 1)) : βˆ€ n, m ≀ n β†’ P n := by apply nat.less_than_or_equal.rec h0; exact h1 /-- Decreasing induction: if `P (k+1)` implies `P k`, then `P n` implies `P m` for all `m ≀ n`. Also works for functions to `Sort*`. -/ @[elab_as_eliminator] def decreasing_induction {P : β„• β†’ Sort*} (h : βˆ€n, P (n+1) β†’ P n) {m n : β„•} (mn : m ≀ n) (hP : P n) : P m := le_rec_on mn (Ξ» k ih hsk, ih $ h k hsk) (Ξ» h, h) hP @[simp] lemma decreasing_induction_self {P : β„• β†’ Sort*} (h : βˆ€n, P (n+1) β†’ P n) {n : β„•} (nn : n ≀ n) (hP : P n) : (decreasing_induction h nn hP : P n) = hP := by { dunfold decreasing_induction, rw [le_rec_on_self] } lemma decreasing_induction_succ {P : β„• β†’ Sort*} (h : βˆ€n, P (n+1) β†’ P n) {m n : β„•} (mn : m ≀ n) (msn : m ≀ n + 1) (hP : P (n+1)) : (decreasing_induction h msn hP : P m) = decreasing_induction h mn (h n hP) := by { dunfold decreasing_induction, rw [le_rec_on_succ] } @[simp] lemma decreasing_induction_succ' {P : β„• β†’ Sort*} (h : βˆ€n, P (n+1) β†’ P n) {m : β„•} (msm : m ≀ m + 1) (hP : P (m+1)) : (decreasing_induction h msm hP : P m) = h m hP := by { dunfold decreasing_induction, rw [le_rec_on_succ'] } lemma decreasing_induction_trans {P : β„• β†’ Sort*} (h : βˆ€n, P (n+1) β†’ P n) {m n k : β„•} (mn : m ≀ n) (nk : n ≀ k) (hP : P k) : (decreasing_induction h (le_trans mn nk) hP : P m) = decreasing_induction h mn (decreasing_induction h nk hP) := by { induction nk with k nk ih, rw [decreasing_induction_self], rw [decreasing_induction_succ h (le_trans mn nk), ih, decreasing_induction_succ] } lemma decreasing_induction_succ_left {P : β„• β†’ Sort*} (h : βˆ€n, P (n+1) β†’ P n) {m n : β„•} (smn : m + 1 ≀ n) (mn : m ≀ n) (hP : P n) : (decreasing_induction h mn hP : P m) = h m (decreasing_induction h smn hP) := by { rw [subsingleton.elim mn (le_trans (le_succ m) smn), decreasing_induction_trans, decreasing_induction_succ'] } end nat namespace function theorem injective.iterate {Ξ± : Type u} {op : Ξ± β†’ Ξ±} (Hinj : injective op) : βˆ€ n, injective (op^[n]) := nat.iterate_ind op Hinj injective_id $ Ξ» _ _, injective_comp theorem surjective.iterate {Ξ± : Type u} {op : Ξ± β†’ Ξ±} (Hinj : surjective op) : βˆ€ n, surjective (op^[n]) := nat.iterate_ind op Hinj surjective_id $ Ξ» _ _, surjective_comp theorem bijective.iterate {Ξ± : Type u} {op : Ξ± β†’ Ξ±} (Hinj : bijective op) : βˆ€ n, bijective (op^[n]) := nat.iterate_ind op Hinj bijective_id $ Ξ» _ _, bijective_comp end function namespace monoid_hom variables {M : Type*} {G : Type*} [monoid M] [group G] @[simp, to_additive] theorem iterate_map_one (f : M β†’* M) (n : β„•) : f^[n] 1 = 1 := nat.iterateβ‚€ f.map_one @[simp, to_additive] theorem iterate_map_mul (f : M β†’* M) (n : β„•) (x y) : f^[n] (x * y) = (f^[n] x) * (f^[n] y) := nat.iterateβ‚‚ f.map_mul @[simp, to_additive] theorem iterate_map_inv (f : G β†’* G) (n : β„•) (x) : f^[n] (x⁻¹) = (f^[n] x)⁻¹ := nat.iterate₁ f.map_inv @[simp] theorem iterate_map_sub {A : Type*} [add_group A] (f : A β†’+ A) (n : β„•) (x y) : f^[n] (x - y) = (f^[n] x) - (f^[n] y) := nat.iterateβ‚‚ f.map_sub end monoid_hom namespace ring_hom variables {R : Type*} [semiring R] {S : Type*} [ring S] @[simp] theorem iterate_map_one (f : R β†’+* R) (n : β„•) : f^[n] 1 = 1 := nat.iterateβ‚€ f.map_one @[simp] theorem iterate_map_zero (f : R β†’+* R) (n : β„•) : f^[n] 0 = 0 := nat.iterateβ‚€ f.map_zero @[simp] theorem iterate_map_mul (f : R β†’+* R) (n : β„•) (x y) : f^[n] (x * y) = (f^[n] x) * (f^[n] y) := nat.iterateβ‚‚ f.map_mul @[simp] theorem iterate_map_add (f : R β†’+* R) (n : β„•) (x y) : f^[n] (x + y) = (f^[n] x) + (f^[n] y) := nat.iterateβ‚‚ f.map_add @[simp] theorem iterate_map_neg (f : S β†’+* S) (n : β„•) (x) : f^[n] (-x) = -(f^[n] x) := nat.iterate₁ f.map_neg @[simp] theorem iterate_map_sub (f : S β†’+* S) (n : β„•) (x y) : f^[n] (x - y) = (f^[n] x) - (f^[n] y) := nat.iterateβ‚‚ f.map_sub end ring_hom
e82b654c1c2d081136d28535a6113602686411ca
0533ebfa09b11cf8a29da118dd1b86b94949511e
/src/cauchy.lean
b8ec36c3f55f422e939d96ac2aa6070c49ed0b73
[ "MIT" ]
permissive
wudcscheme/Lean-RH
89e9055aac68703876e750c23fb5327979a09822
870f78cc7c0715d03466d1211b0f032b425ec984
refs/heads/main
1,679,188,429,923
1,602,701,790,000
1,602,701,790,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
24,820
lean
/- ------------------------------------------------------------------------- -| | @project: riemann_hypothesis | | @file: cauchy.lean | | @author: Brandon H. Gomes and Alex Kontorovich | | @affil: Rutgers University | |- ------------------------------------------------------------------------- -/ import .exponential /-! -/ namespace riemann_hypothesis --β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”-- variables {Ξ± : Type*} {Ξ² : Type*} open algebra section cauchy --β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”-- variables [has_lt Ξ±] [has_zero Ξ±] variables [has_sub Ξ²] variables (abs : Ξ² β†’ Ξ±) variables (seq : nat β†’ Ξ²) /-- -/ structure is_cauchy.struct (Ξ΅ : Ξ±) (Ξ΅pos : 0 < Ξ΅) := (index : nat) (boundedness : Ξ  j, index ≀ j β†’ abs (seq j - seq index) < Ξ΅) /-- -/ def is_cauchy := Ξ  Ξ΅ Ξ΅pos, is_cauchy.struct abs seq Ξ΅ Ξ΅pos /-- -/ structure is_convergent.struct (β„“ : Ξ²) (Ξ΅ : Ξ±) (Ξ΅pos : 0 < Ξ΅) := (index : nat) (boundedness : Ξ  n, index ≀ n β†’ abs (β„“ - seq n) < Ξ΅) /-- -/ def is_convergent (β„“) := Ξ  Ξ΅ Ξ΅pos, is_convergent.struct abs seq β„“ Ξ΅ Ξ΅pos /-- -/ structure CauchySequence := (sequence : nat β†’ Ξ²) (condition : is_cauchy abs sequence) /-- -/ structure ConvergentSequence := (sequence : nat β†’ Ξ²) (limit : Ξ²) (condition : is_convergent abs sequence limit) end cauchy --β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”-- namespace is_cauchy --β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”-- variables [has_zero Ξ±] /-- -/ def from_convergent [has_add Ξ±] [preorder Ξ±] [has_add_lt_add Ξ±] [has_sub Ξ²] [has_add Ξ²] [has_sub_add_sub_cancel Ξ²] (abs : Ξ² β†’ Ξ±) (abs_sub : Ξ  x y, abs (x - y) = abs (y - x)) (abs_add : Ξ  x y, abs (x + y) ≀ abs x + abs y) (half : Half Ξ±) (seq β„“) : is_convergent abs seq β„“ β†’ is_cauchy abs seq := begin intros convergence Ξ΅ Ξ΅pos, let cond := convergence _ (half.preserve_pos Ξ΅pos), let i := cond.index, existsi i, intros _ le_j, rw ← has_sub_add_sub_cancel.eq _ β„“ _, refine lt_of_le_of_lt (abs_add _ _) _, rw abs_sub, rw ← half.doubled_inv Ξ΅, refine has_add_lt_add.lt (cond.boundedness _ le_j) (cond.boundedness _ (le_refl _)), end --β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”-- variables [has_sub Ξ±] section closed_mul --β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”-- variables [has_lt Ξ±] [has_mul Ξ±] [has_inv Ξ±] section closed_left_mul --β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”-- variables [has_left_sub_distributivity Ξ±] [has_left_inv_pos_lt Ξ±] [has_mul_zero_is_zero Ξ±] [has_right_mul_inv_lt_pos Ξ±] /-- -/ def closed.sequence_left_mul (abs : Ξ± β†’ Ξ±) (abs_mul : Ξ  x y, abs (x * y) = abs x * abs y) (seq) (C absC_pos) : is_cauchy abs seq β†’ is_cauchy abs (Ξ» n, C * seq n) := begin intros cauchy_condition Ξ΅ Ξ΅pos, let division := has_left_inv_pos_lt.lt absC_pos Ξ΅pos, rw has_mul_zero_is_zero.eq at division, let cauchy := cauchy_condition _ division, existsi cauchy.index, intros _ le_j, rw ← has_left_sub_distributivity.eq, rw abs_mul, refine has_right_mul_inv_lt_pos.lt absC_pos (cauchy.boundedness _ le_j), end /-- -/ def closed.partial_sum_left_mul [has_add Ξ±] [has_left_add_distributivity Ξ±] (abs abs_mul) (seq : nat β†’ Ξ±) (C absC_pos) : is_cauchy abs (partial_sum seq) β†’ is_cauchy abs (partial_sum (Ξ» n, C * seq n)) := begin rw partial_sum.left_mul_commute _ C, refine closed.sequence_left_mul _ abs_mul _ _ absC_pos, end end closed_left_mul --β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”-- section closed_right_mul --β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”-- variables [has_right_sub_distributivity Ξ±] [has_right_inv_pos_lt Ξ±] [has_zero_mul_is_zero Ξ±] [has_left_mul_inv_lt_pos Ξ±] /-- -/ def closed.sequence_right_mul (abs : Ξ± β†’ Ξ±) (abs_mul : Ξ  x y, abs (x * y) = abs x * abs y) (seq) (C absC_pos) : is_cauchy abs seq β†’ is_cauchy abs (Ξ» n, seq n * C) := begin intros cauchy_condition Ξ΅ Ξ΅pos, let division := has_right_inv_pos_lt.lt absC_pos Ξ΅pos, rw has_zero_mul_is_zero.eq at division, let cauchy := cauchy_condition _ division, existsi cauchy.index, intros _ le_j, rw ← has_right_sub_distributivity.eq, rw abs_mul, refine has_left_mul_inv_lt_pos.lt absC_pos (cauchy.boundedness _ le_j), end /-- -/ def closed.partial_sum_right_mul [has_add Ξ±] [has_right_add_distributivity Ξ±] (abs abs_mul) (seq : nat β†’ Ξ±) (C absC_pos) : is_cauchy abs (partial_sum seq) β†’ is_cauchy abs (partial_sum (Ξ» n, seq n * C)) := begin rw partial_sum.right_mul_commute _ C, refine closed.sequence_right_mul _ abs_mul _ _ absC_pos, end end closed_right_mul --β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”-- end closed_mul --β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”-- --β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”-- variables [has_add Ξ±] [has_sub_self_is_zero Ξ±] [has_add_sub_assoc Ξ±] /-- -/ def closed.partial_sum_translate [has_lt Ξ±] (abs : Ξ± β†’ Ξ±) (seq) (k) : is_cauchy abs (partial_sum (translate seq k)) β†’ is_cauchy abs (partial_sum seq) := begin intros cauchy_condition Ξ΅ Ξ΅pos, let cauchy := cauchy_condition _ Ξ΅pos, existsi k + cauchy.index, intros _ le, let index_ineq := nat.le_sub_left_of_add_le le, rw partial_sum.sub_as_translate seq le, rw ← translate.combine, rw ← nat.sub_sub, rw ← partial_sum.sub_as_translate (translate seq _) index_ineq, refine cauchy.boundedness _ index_ineq, end --β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”-- variables [preorder Ξ±] [has_add_nonneg Ξ±] section comparison --β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”-- variables [has_add_le_add Ξ±] variables [has_zero Ξ²] [has_add Ξ²] [has_sub Ξ²] [has_add_sub_assoc Ξ²] [has_sub_self_is_zero Ξ²] variables (abs : Ξ± β†’ Ξ±) (ge_zero_to_abs : Ξ  z, 0 ≀ z β†’ abs z = z) variables (abs_Ξ²Ξ± : Ξ² β†’ Ξ±) (abs_Ξ²Ξ±_zero : abs_Ξ²Ξ± 0 = 0) (abs_Ξ²Ξ±_add : Ξ  x y, abs_Ξ²Ξ± (x + y) ≀ abs_Ξ²Ξ± x + abs_Ξ²Ξ± y) (abs_Ξ²Ξ±_ge_zero : Ξ  x, 0 ≀ abs_Ξ²Ξ± x) include ge_zero_to_abs abs_Ξ²Ξ± abs_Ξ²Ξ±_zero abs_Ξ²Ξ±_add abs_Ξ²Ξ±_ge_zero /-- -/ def comparison (a b) (le : abs_Ξ²Ξ± ∘ b ≀ a) : is_cauchy abs (partial_sum a) β†’ is_cauchy abs_Ξ²Ξ± (partial_sum b) := begin intros cauchy_condition Ξ΅ Ξ΅pos, let cauchy := cauchy_condition _ Ξ΅pos, existsi cauchy.index, intros _ le_j, refine lt_of_le_of_lt _ (cauchy.boundedness _ le_j), rw partial_sum.sub_as_translate a le_j, rw partial_sum.sub_as_translate b le_j, rw triangle_equality abs ge_zero_to_abs _ _ _, refine le_trans (triangle_inequality _ abs_Ξ²Ξ±_zero abs_Ξ²Ξ±_add _ _) (partial_sum.monotonicity (translate.monotonicity le _) _), refine Ξ» _, le_trans (abs_Ξ²Ξ±_ge_zero _) (le _), end /-- -/ def closed.inclusion (seq) : is_cauchy abs (partial_sum (abs_Ξ²Ξ± ∘ seq)) β†’ is_cauchy abs_Ξ²Ξ± (partial_sum seq) := begin refine comparison _ ge_zero_to_abs abs_Ξ²Ξ± abs_Ξ²Ξ±_zero abs_Ξ²Ξ±_add abs_Ξ²Ξ±_ge_zero _ _ (Ξ» _, le_refl _), end end comparison --β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”-- --β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”-- variables [has_zero_right_add_cancel Ξ±] [has_add_le_add Ξ±] [has_le_sub_add_le Ξ±] [has_le_add_of_nonneg_of_le Ξ±] /-- -/ def closed.subsequence_partial_sum.ineq (seq : nat β†’ Ξ±) (nonneg Ο† sinc_Ο† i j) : i ≀ j β†’ partial_sum (seq ∘ Ο†) j - partial_sum (seq ∘ Ο†) i ≀ partial_sum seq (Ο† j) - partial_sum seq (Ο† i) := begin let strong_inc_Ο† := strictly_increasing.as_increasing_strong _ sinc_Ο†, intros i_le_j, rw partial_sum.sub_as_translate (seq ∘ Ο†) i_le_j, rw partial_sum.sub_as_translate seq (strong_inc_Ο† _ _ i_le_j), induction j with _ hj, cases i_le_j, rw [nat.sub_self, nat.sub_self, partial_sum, partial_sum], cases nat.of_le_succ i_le_j, rw nat.succ_sub h, refine le_trans (has_add_le_add.le (le_refl _) (hj h)) (has_le_sub_add_le.le _), rw partial_sum.sub_as_translate (translate seq _) (nat.sub_le_sub_right (le_of_lt (sinc_Ο† _)) _), rw translate.combine, rw nat.add_sub_of_le (strong_inc_Ο† _ _ h), rw nat.sub_sub_sub_cancel_right (strong_inc_Ο† _ _ h), rw translate, rw nat.add_sub_of_le h, refine le_trans _ (partial_sum.double_monotonicity _ 1 _ _ (translate.preserve_nonneg _ nonneg _) (Ξ» _, le_refl _) (nat.le_sub_left_of_add_le (sinc_Ο† _))), rw [partial_sum, partial_sum, translate], rw has_zero_right_add_cancel.eq, rw nat.add_zero, rw [h, nat.sub_self, partial_sum], refine partial_sum.preserve_nonneg _ (translate.preserve_nonneg _ nonneg _) _, end /-- -/ def closed.subsequence_partial_sum (abs : Ξ± β†’ Ξ±) (abs_mono : Ξ  x y, 0 ≀ x β†’ x ≀ y β†’ abs x ≀ abs y) (seq nonneg) (Ο† sinc_Ο†) : is_cauchy abs (partial_sum seq) β†’ is_cauchy abs (partial_sum (seq ∘ Ο†)) := begin intros cauchy_condition Ξ΅ Ξ΅pos, let ge_index := strictly_increasing.ge_index _ sinc_Ο†, let cauchy := cauchy_condition Ξ΅ Ξ΅pos, existsi cauchy.index, intros _ le_j, let strong_inc := strictly_increasing.as_increasing_strong _ sinc_Ο† _ _ le_j, refine lt_of_le_of_lt (le_trans (abs_mono _ _ (partial_sum.lower_differences.bottom _ (nonneg_compose_preserve _ _ nonneg) le_j) (closed.subsequence_partial_sum.ineq _ nonneg _ sinc_Ο† _ _ le_j)) (abs_mono _ _ (partial_sum.lower_differences.bottom _ nonneg strong_inc) (partial_sum.lower_differences _ nonneg (ge_index _) strong_inc))) (cauchy.boundedness _ (le_trans le_j (ge_index _))), end /-- -/ def closed.scaled_sequence_partial_sum (abs : Ξ± β†’ Ξ±) (abs_mono) (seq nonneg) (N Npos) : is_cauchy abs (partial_sum seq) β†’ is_cauchy abs (partial_sum (Ξ» n, seq (N * n))) := closed.subsequence_partial_sum _ abs_mono _ nonneg _ (Ξ» _, nat.lt_add_of_pos_right Npos) end is_cauchy --β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”-- namespace condensation --β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”-- variables [preorder Ξ±] [has_zero Ξ±] [has_one Ξ±] [has_sub Ξ±] [has_add Ξ±] [has_mul Ξ±] [has_sub_self_is_zero Ξ±] [has_add_sub_assoc Ξ±] [has_sub_add_sub_cancel Ξ±] [has_add_nonneg Ξ±] [has_add_le_add Ξ±] [has_le_add_of_nonneg_of_le Ξ±] /-- -/ def shape_sum_comparison (abs : Ξ± β†’ Ξ±) (ge_zero_to_abs : Ξ  z, 0 ≀ z β†’ abs z = z) (seq nonneg) (Ο† sinc_Ο†) : is_cauchy abs (partial_sum (shape_sum seq Ο†)) β†’ is_cauchy abs (partial_sum (translate seq (Ο† 0))) := begin intros cauchy_condition Ξ΅ Ξ΅pos, let translate_nonneg := translate.preserve_nonneg _ nonneg _, let strong_inc := strictly_increasing.as_increasing_strong _ sinc_Ο†, let cauchy := cauchy_condition _ Ξ΅pos, existsi Ο† cauchy.index - Ο† 0, intros _ le_j, let I_le_ju0 := le_trans (strictly_increasing.ge_index _ sinc_Ο† _) (nat.le_add_of_sub_le_right le_j), rw partial_sum.sub_as_translate (translate seq _) le_j, rw translate.combine, rw nat.add_sub_of_le (strong_inc _ _ (nat.zero_le _)), rw nat.neg_right_swap (strong_inc _ _ (nat.zero_le _)), rw triangle_equality abs ge_zero_to_abs _ translate_nonneg _, let condition := cauchy.boundedness _ I_le_ju0, rw partial_sum.sub_as_translate (shape_sum seq _) I_le_ju0 at condition, rw shape_sum.unfold seq _ sinc_Ο† I_le_ju0 at condition, rw triangle_equality abs ge_zero_to_abs _ translate_nonneg _ at condition, refine lt_of_le_of_lt (partial_sum.index_monotonicity _ translate_nonneg (nat.sub_le_sub_right (strictly_increasing.ge_index _ sinc_Ο† _) _)) condition, end --β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”-- variables [has_lift_t nat Ξ±] [has_lift_zero_same nat Ξ±] [has_lift_one_same nat Ξ±] [has_lift_add_comm nat Ξ±] [has_zero_mul_is_zero Ξ±] [has_left_unit Ξ±] [has_right_add_distributivity Ξ±] variables (abs : Ξ± β†’ Ξ±) (abs_zero : abs 0 = 0) (abs_ge_zero : Ξ  x, 0 ≀ abs x) (abs_add : Ξ  x y, abs (x + y) ≀ abs x + abs y) (ge_zero_to_abs : Ξ  z, 0 ≀ z β†’ abs z = z) variables (seq : nat β†’ Ξ±) (nonneg : 0 ≀ seq) (non_inc : non_increasing seq) include abs abs_zero abs_ge_zero abs_add ge_zero_to_abs seq nonneg non_inc /-- -/ def cauchy_schlomilch_test (Ο†) (sinc_Ο† : strictly_increasing Ο†) : is_cauchy abs (partial_sum (Ξ» n, ↑(nat.successive_difference Ο† n) * seq (Ο† n))) β†’ is_cauchy abs (partial_sum seq) := begin intros cauchy, let comparison := is_cauchy.comparison _ ge_zero_to_abs abs abs_zero abs_add abs_ge_zero _ _ _, refine is_cauchy.closed.partial_sum_translate _ _ _ (shape_sum_comparison _ ge_zero_to_abs _ nonneg _ sinc_Ο† (comparison cauchy)), intros _, rw function.comp_app, rw shape_sum, rw partial_sum.sub_as_translate seq (le_of_lt (sinc_Ο† _)), rw partial_sum.from_mul, refine le_trans (triangle_inequality _ abs_zero abs_add _ _) (partial_sum.monotonicity _ _), intros _, rw function.comp_app, rw translate, rw ge_zero_to_abs _ (nonneg _), refine non_increasing.as_non_increasing_strong _ non_inc _ _, end /-- -/ def cauchy_test : is_cauchy abs (partial_sum (Ξ» n, ↑(2 ^ n) * seq (2 ^ n - 1))) β†’ is_cauchy abs (partial_sum seq) := begin let result := cauchy_schlomilch_test _ abs_zero abs_ge_zero abs_add ge_zero_to_abs _ nonneg non_inc _ _, rw (_ : nat.successive_difference (Ξ» n, 2 ^ n - 1) = pow 2) at result, refine result, refine funext _, intros _, rw nat.successive_difference, rw nat.sub_sub, rw ← nat.add_sub_assoc (nat.pow_two_ge_one _), rw nat.add_sub_cancel_left, rw (nat.sub_eq_iff_eq_add _).2 _, refine nat.le_add_left _ _, refine nat.mul_two _, intros _, refine nat.sub_mono_left_strict (nat.pow_two_ge_one _) (nat.pow_two_monotonic _), end end condensation --β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”-- namespace geometric --β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”-- variables [has_zero Ξ±] [has_one Ξ±] [has_add Ξ±] [has_mul Ξ±] [has_sub Ξ±] [has_inv Ξ±] [has_mul_assoc Ξ±] [has_zero_right_add_cancel Ξ±] [has_right_unit Ξ±] [has_sub_ne_zero_of_ne Ξ±] [has_right_sub_distributivity Ξ±] [has_sub_self_is_zero Ξ±] [has_inv_mul_right_cancel_self Ξ±] [has_left_sub_distributivity Ξ±] [has_add_sub_exchange Ξ±] [has_right_add_distributivity Ξ±] section series_definitions --β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”-- local notation a `^` n := nat.power a n /-- -/ def series.formula (x : Ξ±) (x_ne_1 : x β‰  1) (n) : partial_sum (nat.power x) n = (1 - x ^ n) * (1 - x)⁻¹ := begin induction n with n hn, rw partial_sum, rw nat.power, rw has_right_sub_distributivity.eq, rw has_sub_self_is_zero.eq, rw partial_sum, rw nat.power, rw hn, rw (_ : x ^ n + (1 - x ^ n) * (1 - x)⁻¹ = x ^ n * (1 - x) * (1 - x)⁻¹ + (1 - x ^ n) * (1 - x)⁻¹), rw ← has_right_add_distributivity.eq, rw has_left_sub_distributivity.eq, rw has_right_unit.eq, rw has_add_sub_exchange.eq, rw has_sub_self_is_zero.eq, rw has_zero_right_add_cancel.eq, rw has_mul_assoc.eq, rw has_inv_mul_right_cancel_self.eq _ (has_sub_ne_zero_of_ne.ne x_ne_1.symm), rw has_right_unit.eq, end end series_definitions --β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”-- section series_is_cauchy --β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”-- /-- -/ def series.is_cauchy [has_lift_t nat Ξ±] [has_lift_add_comm nat Ξ±] [has_lift_zero_same nat Ξ±] [has_lift_one_same nat Ξ±] [preorder Ξ±] [has_add_lt_add Ξ±] [has_left_add_distributivity Ξ±] [has_sub_add_sub_cancel Ξ±] [has_sub_sub Ξ±] [has_mul_zero_is_zero Ξ±] [has_left_unit Ξ±] [has_inv_right_mul_lt_pos Ξ±] [has_left_mul_inv_lt_neg Ξ±] [has_zero_left_add_cancel Ξ±] [has_zero_lt_one Ξ±] [has_mul_pos Ξ±] [has_sub_pos Ξ±] (abs : Ξ± β†’ Ξ±) (abs_one : abs 1 = 1) (abs_inv : Ξ  a, abs (a⁻¹) = (abs a)⁻¹) (abs_sub : Ξ  x y, abs (x - y) = abs (y - x)) (abs_add : Ξ  x y, abs (x + y) ≀ abs x + abs y) (abs_mul : Ξ  x y, abs (x * y) = abs x * abs y) (ge_zero_to_abs : Ξ  a, 0 ≀ a β†’ abs a = a) (β„― : ExpLog Ξ± Ξ±) (half : Half Ξ±) (ceil : LiftCeil Ξ±) (x : Ξ±) (xpos : 0 < x) (x_lt_one : x < 1) : is_cauchy abs (partial_sum (β„―.nat_pow x xpos)) := begin refine is_cauchy.from_convergent abs abs_sub abs_add half _ (1 - x)⁻¹ _, intros Ξ΅ Ξ΅pos, have abs_x_pos : 0 < abs x, rw ge_zero_to_abs _ (le_of_lt xpos), refine xpos, have abs_x_lt_one : abs x < 1, rw ge_zero_to_abs _ (le_of_lt xpos), refine x_lt_one, let one_minus_x_pos := has_sub_pos.lt x_lt_one, let Ξ΅_mul_one_minus_x := has_mul_pos.lt Ξ΅pos one_minus_x_pos, existsi (ceil.map (β„―.log _ Ξ΅_mul_one_minus_x * (β„―.log _ abs_x_pos)⁻¹)).succ, intros n le_n, rw ← β„―.nat_pow_to_nat_power, rw series.formula _ (ne_of_lt x_lt_one), rw has_right_sub_distributivity.eq, rw has_left_unit.eq, rw has_sub_sub.eq, rw has_sub_self_is_zero.eq, rw has_zero_left_add_cancel.eq, rw abs_mul, rw abs_inv, rw ge_zero_to_abs _ (le_of_lt one_minus_x_pos), rw nat.power.mul_commute _ abs_one abs_mul, refine has_inv_right_mul_lt_pos.lt one_minus_x_pos _, rw β„―.nat_pow_to_nat_power, rw ← β„―.log_inverted _ Ξ΅_mul_one_minus_x, refine β„―.exp_monotonic (has_left_mul_inv_lt_neg.lt (β„―.log_lt_one_is_lt_zero abs_x_pos abs_x_lt_one) (ceil.lift_lt le_n)), end end series_is_cauchy --β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”-- end geometric --β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”-- end riemann_hypothesis --β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”--
17ebcbf4e37db4e73c00b32b19847f2f1c2836b6
a45212b1526d532e6e83c44ddca6a05795113ddc
/src/tactic/omega/coeffs.lean
a76b0515188ea3e5c8c5e758b13c188e63f6b22f
[ "Apache-2.0" ]
permissive
fpvandoorn/mathlib
b21ab4068db079cbb8590b58fda9cc4bc1f35df4
b3433a51ea8bc07c4159c1073838fc0ee9b8f227
refs/heads/master
1,624,791,089,608
1,556,715,231,000
1,556,715,231,000
165,722,980
5
0
Apache-2.0
1,552,657,455,000
1,547,494,646,000
Lean
UTF-8
Lean
false
false
10,176
lean
/- Copyright (c) 2019 Seul Baek. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Seul Baek Non-constant terms of linear constraints are represented by storing their coefficients in integer lists. -/ import data.list.basic import tactic.ring import tactic.omega.misc namespace omega namespace coeffs open list.func variable {v : nat β†’ int} @[simp] def val_between (v : nat β†’ int) (as : list int) (l : nat) : nat β†’ int | 0 := 0 | (o+1) := (val_between o) + (get (l+o) as * v (l+o)) @[simp] lemma val_between_nil {l : nat} : βˆ€ m, val_between v [] l m = 0 | 0 := by simp only [val_between] | (m+1) := by simp only [val_between_nil m, omega.coeffs.val_between, get_nil, zero_add, zero_mul, int.default_eq_zero] def val (v : nat β†’ int) (as : list int) : int := val_between v as 0 as.length @[simp] lemma val_nil : val v [] = 0 := rfl lemma val_between_eq_of_le {as : list int} {l : nat} : βˆ€ m, as.length ≀ l + m β†’ val_between v as l m = val_between v as l (as.length - l) | 0 h1 := begin rw (nat.sub_eq_zero_iff_le.elim_right _), apply h1 end | (m+1) h1 := begin rw le_iff_eq_or_lt at h1, cases h1, { rw [h1, add_comm l, nat.add_sub_cancel] }, have h2 : list.length as ≀ l + m, { rw ← nat.lt_succ_iff, apply h1 }, simpa [ get_eq_default_of_le _ h2, zero_mul, add_zero, val_between ] using val_between_eq_of_le _ h2 end lemma val_eq_of_le {as : list int} {k : nat} : as.length ≀ k β†’ val v as = val_between v as 0 k := begin intro h1, unfold val, rw [val_between_eq_of_le k _], refl, rw zero_add, exact h1 end lemma val_between_eq_val_between {v w : nat β†’ int} {as bs : list int} {l : nat} : βˆ€ {m}, (βˆ€ x, l ≀ x β†’ x < l + m β†’ v x = w x) β†’ (βˆ€ x, l ≀ x β†’ x < l + m β†’ get x as = get x bs) β†’ val_between v as l m = val_between w bs l m | 0 h1 h2 := rfl | (m+1) h1 h2 := begin unfold val_between, have h3 : l + m < l + (m + 1), { rw ← add_assoc, apply lt_add_one }, apply fun_mono_2, apply val_between_eq_val_between; intros x h4 h5, { apply h1 _ h4 (lt_trans h5 h3) }, { apply h2 _ h4 (lt_trans h5 h3) }, rw [h1 _ _ h3, h2 _ _ h3]; apply nat.le_add_right end local notation as ` {` m ` ↦ ` a `}` := set a as m def val_between_set {a : int} {l n : nat} : βˆ€ {m}, l ≀ n β†’ n < l + m β†’ val_between v ([] {n ↦ a}) l m = a * v n | 0 h1 h2 := begin exfalso, apply lt_irrefl l (lt_of_le_of_lt h1 h2) end | (m+1) h1 h2 := begin rw [← add_assoc, nat.lt_succ_iff, le_iff_eq_or_lt] at h2, cases h2; unfold val_between, { have h3 : val_between v ([] {l + m ↦ a}) l m = 0, { apply @eq.trans _ _ (val_between v [] l m), { apply val_between_eq_val_between, { intros, refl }, { intros x h4 h5, rw [get_nil, get_set_eq_of_ne, get_nil], apply ne_of_lt h5 } }, apply val_between_nil }, rw h2, simp only [h3, zero_add, list.func.get_set] }, { have h3 : l + m β‰  n, { apply ne_of_gt h2 }, rw [@val_between_set m h1 h2, get_set_eq_of_ne _ _ h3], simp only [h3, get_nil, add_zero, zero_mul, int.default_eq_zero] } end @[simp] def val_set {m : nat} {a : int} : val v ([] {m ↦ a}) = a * v m := begin apply val_between_set, apply zero_le, apply lt_of_lt_of_le (lt_add_one _), simp only [length_set, zero_add, le_max_right], end lemma val_between_neg {as : list int} {l : nat} : βˆ€ {o}, val_between v (neg as) l o = -(val_between v as l o) | 0 := rfl | (o+1) := begin unfold val_between, rw [neg_add, neg_mul_eq_neg_mul], apply fun_mono_2, apply val_between_neg, apply fun_mono_2 _ rfl, apply get_neg end @[simp] lemma val_neg {as : list int} : val v (neg as) = -(val v as) := by simpa only [val, length_neg] using val_between_neg lemma val_between_add {is js : list int} {l : nat} : βˆ€ m, val_between v (add is js) l m = (val_between v is l m) + (val_between v js l m) | 0 := rfl | (m+1) := by { simp only [val_between, val_between_add m, list.func.get, get_add], ring } @[simp] lemma val_add {is js : list int} : val v (add is js) = (val v is) + (val v js) := begin unfold val, rw val_between_add, apply fun_mono_2; apply val_between_eq_of_le; rw [zero_add, length_add], apply le_max_left, apply le_max_right end lemma val_between_sub {is js : list int} {l : nat} : βˆ€ m, val_between v (sub is js) l m = (val_between v is l m) - (val_between v js l m) | 0 := rfl | (m+1) := by { simp only [val_between, val_between_sub m, list.func.get, get_sub], ring } @[simp] lemma val_sub {is js : list int} : val v (sub is js) = (val v is) - (val v js) := begin unfold val, rw val_between_sub, apply fun_mono_2; apply val_between_eq_of_le; rw [zero_add, length_sub], apply le_max_left, apply le_max_right end def val_except (k : nat) (v : nat β†’ int) (as) := val_between v as 0 k + val_between v as (k+1) (as.length - (k+1)) lemma val_except_eq_val_except {k : nat} {is js : list int} {v w : nat β†’ int} : (βˆ€ x β‰  k, v x = w x) β†’ (βˆ€ x β‰  k, get x is = get x js) β†’ val_except k v is = val_except k w js := begin intros h1 h2, unfold val_except, apply fun_mono_2, { apply val_between_eq_val_between; intros x h3 h4; [ {apply h1}, {apply h2} ]; apply ne_of_lt; rw zero_add at h4; apply h4 }, { repeat { rw ← val_between_eq_of_le ((max is.length js.length) - (k+1)) }, { apply val_between_eq_val_between; intros x h3 h4; [ {apply h1}, {apply h2} ]; apply ne.symm; apply ne_of_lt; rw nat.lt_iff_add_one_le; exact h3 }, repeat { rw add_comm, apply le_trans _ (nat.le_sub_add _ _), { apply le_max_right <|> apply le_max_left } } } end local notation v ` ⟨` m ` ↦ ` a `⟩` := update m a v lemma val_except_update_set {n : nat} {as : list int} {i j : int} : val_except n (v⟨n ↦ i⟩) (as {n ↦ j}) = val_except n v as := by apply val_except_eq_val_except update_eq_of_ne (get_set_eq_of_ne _) lemma val_between_add_val_between {as : list int} {l m : nat} : βˆ€ {n}, val_between v as l m + val_between v as (l+m) n = val_between v as l (m+n) | 0 := by simp only [val_between, add_zero] | (n+1) := begin rw ← add_assoc, unfold val_between, rw add_assoc, rw ← @val_between_add_val_between n, ring, end def val_except_add_eq (n : nat) {as : list int} : (val_except n v as) + ((get n as) * (v n)) = val v as := begin unfold val_except, unfold val, by_cases h1 : n + 1 ≀ as.length, { have h4 := @val_between_add_val_between v as 0 (n+1) (as.length - (n+1)), have h5 : n + 1 + (as.length - (n + 1)) = as.length, { rw [add_comm, nat.sub_add_cancel h1] }, rw h5 at h4, apply eq.trans _ h4, simp only [val_between, zero_add], ring }, rw not_lt at h1, have h2 : (list.length as - (n + 1)) = 0, { apply nat.sub_eq_zero_of_le (le_trans h1 (nat.le_add_right _ _)) }, have h3 : val_between v as 0 (list.length as) = val_between v as 0 (n + 1), { simpa only [val] using @val_eq_of_le v as (n+1) (le_trans h1 (nat.le_add_right _ _)) }, simp only [add_zero, val_between, zero_add, h2, h3] end @[simp] lemma val_between_map_mul {i : int} {as: list int} {l : nat} : βˆ€ {m}, val_between v (list.map ((*) i) as) l m = i * val_between v as l m | 0 := by simp only [val_between, mul_zero, list.map] | (m+1) := begin unfold val_between, rw [@val_between_map_mul m, mul_add], apply fun_mono_2 rfl, by_cases h1 : l + m < as.length, { rw [get_map h1, mul_assoc] }, rw not_lt at h1, rw [get_eq_default_of_le, get_eq_default_of_le]; try {simp}; apply h1 end lemma forall_val_dvd_of_forall_mem_dvd {i : int} {as : list int} : (βˆ€ x ∈ as, i ∣ x) β†’ (βˆ€ n, i ∣ get n as) | h1 n := by { apply forall_val_of_forall_mem _ h1, apply dvd_zero } lemma dvd_val_between {i} {as: list int} {l : nat} : βˆ€ {m}, (βˆ€ x ∈ as, i ∣ x) β†’ (i ∣ val_between v as l m) | 0 h1 := dvd_zero _ | (m+1) h1 := begin unfold val_between, apply dvd_add, apply dvd_val_between h1, apply dvd_mul_of_dvd_left, by_cases h2 : get (l+m) as = 0, { rw h2, apply dvd_zero }, apply h1, apply mem_get_of_ne_zero h2 end lemma dvd_val {as : list int} {i : int} : (βˆ€ x ∈ as, i ∣ x) β†’ (i ∣ val v as) := by apply dvd_val_between @[simp] lemma val_between_map_div {as: list int} {i : int} {l : nat} (h1 : βˆ€ x ∈ as, i ∣ x) : βˆ€ {m}, val_between v (list.map (Ξ» x, x / i) as) l m = (val_between v as l m) / i | 0 := by simp only [int.zero_div, val_between, list.map] | (m+1) := begin unfold val_between, rw [@val_between_map_div m, int.add_div_of_dvd (dvd_val_between h1)], apply fun_mono_2 rfl, { apply calc get (l + m) (list.map (Ξ» (x : β„€), x / i) as) * v (l + m) = ((get (l + m) as) / i) * v (l + m) : begin apply fun_mono_2 _ rfl, rw get_map', apply int.zero_div end ... = get (l + m) as * v (l + m) / i : begin repeat {rw mul_comm _ (v (l+m))}, rw int.mul_div_assoc, apply forall_val_dvd_of_forall_mem_dvd h1 end }, apply dvd_mul_of_dvd_left, apply forall_val_dvd_of_forall_mem_dvd h1, end @[simp] lemma val_map_div {as : list int} {i : int} : (βˆ€ x ∈ as, i ∣ x) β†’ val v (list.map (Ξ» x, x / i) as) = (val v as) / i := by {intro h1, simpa only [val, list.length_map] using val_between_map_div h1} lemma val_between_eq_zero {is: list int} {l : nat} : βˆ€ {m}, (βˆ€ x : int, x ∈ is β†’ x = 0) β†’ val_between v is l m = 0 | 0 h1 := rfl | (m+1) h1 := begin have h2 := @forall_val_of_forall_mem _ _ is (Ξ» x, x = 0) rfl h1, simpa only [val_between, h2 (l+m), zero_mul, add_zero] using @val_between_eq_zero m h1, end lemma val_eq_zero {is : list int} : (βˆ€ x : int, x ∈ is β†’ x = 0) β†’ val v is = 0 := by apply val_between_eq_zero end coeffs end omega
287e6e7badd0ef01755716b49d5e861747b4b23a
63abd62053d479eae5abf4951554e1064a4c45b4
/src/category_theory/monad/bundled.lean
b3e0203ae9de2f16e3feaa711b4607fa7aa25e81
[ "Apache-2.0" ]
permissive
Lix0120/mathlib
0020745240315ed0e517cbf32e738d8f9811dd80
e14c37827456fc6707f31b4d1d16f1f3a3205e91
refs/heads/master
1,673,102,855,024
1,604,151,044,000
1,604,151,044,000
308,930,245
0
0
Apache-2.0
1,604,164,710,000
1,604,163,547,000
null
UTF-8
Lean
false
false
3,274
lean
/- Copyright (c) 2020 Adam Topaz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Adam Topaz -/ import category_theory.monad.basic import category_theory.eq_to_hom /-! # Bundled Monads We define bundled (co)monads as a structure consisting of a functor `func : C β₯€ C` endowed with a term of type `(co)monad func`. See `category_theory.monad.basic` for the definition. The type of bundled (co)monads on a category `C` is denoted `(Co)Monad C`. We also define morphisms of bundled (co)monads as morphisms of their underlying (co)monads in the sense of `category_theory.(co)monad_hom`. We construct a category instance on `(Co)Monad C`. -/ namespace category_theory open category universes v u -- declare the `v`'s first; see `category_theory.category` for an explanation variables (C : Type u) [category.{v} C] /-- Bundled monads. -/ structure Monad := (func : C β₯€ C) (str : monad func . tactic.apply_instance) /-- Bundled comonads -/ structure Comonad := (func : C β₯€ C) (str : comonad func . tactic.apply_instance) namespace Monad /-- The initial monad. TODO: Prove it's initial. -/ def initial : Monad C := { func := 𝟭 _ } variable {C} instance : inhabited (Monad C) := ⟨initial C⟩ instance {M : Monad C} : monad M.func := M.str /-- Morphisms of bundled monads. -/ def hom (M N : Monad C) := monad_hom M.func N.func namespace hom instance {M : Monad C} : inhabited (hom M M) := ⟨monad_hom.id _⟩ end hom instance : category (Monad C) := { hom := hom, id := Ξ» _, monad_hom.id _, comp := Ξ» _ _ _, monad_hom.comp } section variables (C) /-- The forgetful functor from `Monad C` to `C β₯€ C`. -/ def forget : Monad C β₯€ (C β₯€ C) := { obj := func, map := Ξ» _ _ f, f.to_nat_trans } end @[simp] lemma comp_to_nat_trans {M N L : Monad C} (f : M ⟢ N) (g : N ⟢ L) : (f ≫ g).to_nat_trans = nat_trans.vcomp f.to_nat_trans g.to_nat_trans := rfl @[simp] lemma assoc_func_app {M : Monad C} {X : C} : M.func.map ((ΞΌ_ M.func).app X) ≫ (ΞΌ_ M.func).app X = (ΞΌ_ M.func).app (M.func.obj X) ≫ (ΞΌ_ M.func).app X := by apply monad.assoc end Monad namespace Comonad /-- The terminal comonad. TODO: Prove it's terminal. -/ def terminal : Comonad C := { func := 𝟭 _ } variable {C} instance : inhabited (Comonad C) := ⟨terminal C⟩ instance {M : Comonad C} : comonad M.func := M.str /-- Morphisms of bundled comonads. -/ def hom (M N : Comonad C) := comonad_hom M.func N.func namespace hom instance {M : Comonad C} : inhabited (hom M M) := ⟨comonad_hom.id _⟩ end hom instance : category (Comonad C) := { hom := hom, id := Ξ» _, comonad_hom.id _, comp := Ξ» _ _ _, comonad_hom.comp } section variables (C) /-- The forgetful functor from `CoMonad C` to `C β₯€ C`. -/ def forget : Comonad C β₯€ (C β₯€ C) := { obj := func, map := Ξ» _ _ f, f.to_nat_trans } end @[simp] lemma comp_to_nat_trans {M N L : Comonad C} (f : M ⟢ N) (g : N ⟢ L) : (f ≫ g).to_nat_trans = nat_trans.vcomp f.to_nat_trans g.to_nat_trans := rfl @[simp] lemma coassoc_func_app {M : Comonad C} {X : C} : (Ξ΄_ M.func).app X ≫ M.func.map ((Ξ΄_ M.func).app X) = (Ξ΄_ M.func).app X ≫ (Ξ΄_ M.func).app (M.func.obj X) := by apply comonad.coassoc end Comonad end category_theory
602a0011a956c0fcd9337265aa636c063200b16b
2eab05920d6eeb06665e1a6df77b3157354316ad
/src/algebra/order/absolute_value.lean
23ddd4dd61e67bf6dfc0edd960cd43910e563949
[ "Apache-2.0" ]
permissive
ayush1801/mathlib
78949b9f789f488148142221606bf15c02b960d2
ce164e28f262acbb3de6281b3b03660a9f744e3c
refs/heads/master
1,692,886,907,941
1,635,270,866,000
1,635,270,866,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
8,877
lean
/- Copyright (c) 2021 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Anne Baanen -/ import algebra.order.field /-! # Absolute values This file defines a bundled type of absolute values `absolute_value R S`. ## Main definitions * `absolute_value R S` is the type of absolute values on `R` mapping to `S`. * `absolute_value.abs` is the "standard" absolute value on `S`, mapping negative `x` to `-x`. * `absolute_value.to_monoid_with_zero_hom`: absolute values mapping to a linear ordered field preserve `0`, `*` and `1` * `is_absolute_value`: a type class stating that `f : Ξ² β†’ Ξ±` satisfies the axioms of an abs val -/ /-- `absolute_value R S` is the type of absolute values on `R` mapping to `S`: the maps that preserve `*`, are nonnegative, positive definite and satisfy the triangle equality. -/ structure absolute_value (R S : Type*) [semiring R] [ordered_semiring S] extends mul_hom R S := (nonneg' : βˆ€ x, 0 ≀ to_fun x) (eq_zero' : βˆ€ x, to_fun x = 0 ↔ x = 0) (add_le' : βˆ€ x y, to_fun (x + y) ≀ to_fun x + to_fun y) namespace absolute_value attribute [nolint doc_blame] absolute_value.to_mul_hom initialize_simps_projections absolute_value (to_mul_hom_to_fun β†’ apply) section ordered_semiring variables {R S : Type*} [semiring R] [ordered_semiring S] (abv : absolute_value R S) instance : has_coe_to_fun (absolute_value R S) (Ξ» f, R β†’ S) := ⟨λ f, f.to_fun⟩ @[simp] lemma coe_to_mul_hom : ⇑abv.to_mul_hom = abv := rfl protected theorem nonneg (x : R) : 0 ≀ abv x := abv.nonneg' x @[simp] protected theorem eq_zero {x : R} : abv x = 0 ↔ x = 0 := abv.eq_zero' x protected theorem add_le (x y : R) : abv (x + y) ≀ abv x + abv y := abv.add_le' x y @[simp] protected theorem map_mul (x y : R) : abv (x * y) = abv x * abv y := abv.map_mul' x y protected theorem pos {x : R} (hx : x β‰  0) : 0 < abv x := lt_of_le_of_ne (abv.nonneg x) (ne.symm $ mt abv.eq_zero.mp hx) @[simp] protected theorem pos_iff {x : R} : 0 < abv x ↔ x β‰  0 := ⟨λ h₁, mt abv.eq_zero.mpr h₁.ne', abv.pos⟩ protected theorem ne_zero {x : R} (hx : x β‰  0) : abv x β‰  0 := (abv.pos hx).ne' @[simp] protected theorem map_zero : abv 0 = 0 := abv.eq_zero.2 rfl end ordered_semiring section ordered_ring variables {R S : Type*} [ring R] [ordered_ring S] (abv : absolute_value R S) protected lemma sub_le (a b c : R) : abv (a - c) ≀ abv (a - b) + abv (b - c) := by simpa [sub_eq_add_neg, add_assoc] using abv.add_le (a - b) (b - c) protected lemma le_sub (a b : R) : abv a - abv b ≀ abv (a - b) := sub_le_iff_le_add.2 $ by simpa using abv.add_le (a - b) b @[simp] lemma map_sub_eq_zero_iff (a b : R) : abv (a - b) = 0 ↔ a = b := abv.eq_zero.trans sub_eq_zero end ordered_ring section linear_ordered_ring variables {R S : Type*} [semiring R] [linear_ordered_ring S] (abv : absolute_value R S) /-- `absolute_value.abs` is `abs` as a bundled `absolute_value`. -/ @[simps] protected def abs : absolute_value S S := { to_fun := abs, nonneg' := abs_nonneg, eq_zero' := Ξ» _, abs_eq_zero, add_le' := abs_add, map_mul' := abs_mul } instance : inhabited (absolute_value S S) := ⟨absolute_value.abs⟩ variables [nontrivial R] @[simp] protected theorem map_one : abv 1 = 1 := (mul_right_inj' $ abv.ne_zero one_ne_zero).1 $ by rw [← abv.map_mul, mul_one, mul_one] /-- Absolute values from a nontrivial `R` to a linear ordered ring preserve `*`, `0` and `1`. -/ def to_monoid_with_zero_hom : monoid_with_zero_hom R S := { to_fun := abv, map_zero' := abv.map_zero, map_one' := abv.map_one, .. abv } @[simp] lemma coe_to_monoid_with_zero_hom : ⇑abv.to_monoid_with_zero_hom = abv := rfl /-- Absolute values from a nontrivial `R` to a linear ordered ring preserve `*` and `1`. -/ def to_monoid_hom : monoid_hom R S := { to_fun := abv, map_one' := abv.map_one, .. abv } @[simp] lemma coe_to_monoid_hom : ⇑abv.to_monoid_hom = abv := rfl @[simp] protected lemma map_pow (a : R) (n : β„•) : abv (a ^ n) = abv a ^ n := abv.to_monoid_hom.map_pow a n end linear_ordered_ring section linear_ordered_comm_ring section ring variables {R S : Type*} [ring R] [linear_ordered_comm_ring S] (abv : absolute_value R S) @[simp] protected theorem map_neg (a : R) : abv (-a) = abv a := begin by_cases ha : a = 0, { simp [ha] }, refine (mul_self_eq_mul_self_iff.mp (by rw [← abv.map_mul, neg_mul_neg, abv.map_mul])).resolve_right _, exact ((neg_lt_zero.mpr (abv.pos ha)).trans (abv.pos (neg_ne_zero.mpr ha))).ne' end protected theorem map_sub (a b : R) : abv (a - b) = abv (b - a) := by rw [← neg_sub, abv.map_neg] lemma abs_abv_sub_le_abv_sub (a b : R) : abs (abv a - abv b) ≀ abv (a - b) := abs_sub_le_iff.2 ⟨abv.le_sub _ _, by rw abv.map_sub; apply abv.le_sub⟩ end ring end linear_ordered_comm_ring section linear_ordered_field section field variables {R S : Type*} [field R] [linear_ordered_field S] (abv : absolute_value R S) @[simp] protected theorem map_inv (a : R) : abv a⁻¹ = (abv a)⁻¹ := abv.to_monoid_with_zero_hom.map_inv a @[simp] protected theorem map_div (a b : R) : abv (a / b) = abv a / abv b := abv.to_monoid_with_zero_hom.map_div a b end field end linear_ordered_field end absolute_value section is_absolute_value /-- A function `f` is an absolute value if it is nonnegative, zero only at 0, additive, and multiplicative. See also the type `absolute_value` which represents a bundled version of absolute values. -/ class is_absolute_value {S} [ordered_semiring S] {R} [semiring R] (f : R β†’ S) : Prop := (abv_nonneg [] : βˆ€ x, 0 ≀ f x) (abv_eq_zero [] : βˆ€ {x}, f x = 0 ↔ x = 0) (abv_add [] : βˆ€ x y, f (x + y) ≀ f x + f y) (abv_mul [] : βˆ€ x y, f (x * y) = f x * f y) namespace is_absolute_value section ordered_semiring variables {S : Type*} [ordered_semiring S] variables {R : Type*} [semiring R] (abv : R β†’ S) [is_absolute_value abv] /-- A bundled absolute value is an absolute value. -/ instance absolute_value.is_absolute_value (abv : absolute_value R S) : is_absolute_value abv := { abv_nonneg := abv.nonneg, abv_eq_zero := Ξ» _, abv.eq_zero, abv_add := abv.add_le, abv_mul := abv.map_mul } /-- Convert an unbundled `is_absolute_value` to a bundled `absolute_value`. -/ @[simps] def to_absolute_value : absolute_value R S := { to_fun := abv, add_le' := abv_add abv, eq_zero' := Ξ» _, abv_eq_zero abv, nonneg' := abv_nonneg abv, map_mul' := abv_mul abv } theorem abv_zero : abv 0 = 0 := (abv_eq_zero abv).2 rfl theorem abv_pos {a : R} : 0 < abv a ↔ a β‰  0 := by rw [lt_iff_le_and_ne, ne, eq_comm]; simp [abv_eq_zero abv, abv_nonneg abv] end ordered_semiring section linear_ordered_ring variables {S : Type*} [linear_ordered_ring S] variables {R : Type*} [semiring R] (abv : R β†’ S) [is_absolute_value abv] instance abs_is_absolute_value {S} [linear_ordered_ring S] : is_absolute_value (abs : S β†’ S) := { abv_nonneg := abs_nonneg, abv_eq_zero := Ξ» _, abs_eq_zero, abv_add := abs_add, abv_mul := abs_mul } end linear_ordered_ring section linear_ordered_field variables {S : Type*} [linear_ordered_field S] section semiring variables {R : Type*} [semiring R] (abv : R β†’ S) [is_absolute_value abv] theorem abv_one [nontrivial R] : abv 1 = 1 := (mul_right_inj' $ mt (abv_eq_zero abv).1 one_ne_zero).1 $ by rw [← abv_mul abv, mul_one, mul_one] /-- `abv` as a `monoid_with_zero_hom`. -/ def abv_hom [nontrivial R] : monoid_with_zero_hom R S := ⟨abv, abv_zero abv, abv_one abv, abv_mul abv⟩ lemma abv_pow [nontrivial R] (abv : R β†’ S) [is_absolute_value abv] (a : R) (n : β„•) : abv (a ^ n) = abv a ^ n := (abv_hom abv).to_monoid_hom.map_pow a n end semiring section ring variables {R : Type*} [ring R] (abv : R β†’ S) [is_absolute_value abv] theorem abv_neg (a : R) : abv (-a) = abv a := by rw [← mul_self_inj_of_nonneg (abv_nonneg abv _) (abv_nonneg abv _), ← abv_mul abv, ← abv_mul abv]; simp theorem abv_sub (a b : R) : abv (a - b) = abv (b - a) := by rw [← neg_sub, abv_neg abv] lemma abv_sub_le (a b c : R) : abv (a - c) ≀ abv (a - b) + abv (b - c) := by simpa [sub_eq_add_neg, add_assoc] using abv_add abv (a - b) (b - c) lemma sub_abv_le_abv_sub (a b : R) : abv a - abv b ≀ abv (a - b) := sub_le_iff_le_add.2 $ by simpa using abv_add abv (a - b) b lemma abs_abv_sub_le_abv_sub (a b : R) : abs (abv a - abv b) ≀ abv (a - b) := abs_sub_le_iff.2 ⟨sub_abv_le_abv_sub abv _ _, by rw abv_sub abv; apply sub_abv_le_abv_sub abv⟩ end ring section field variables {R : Type*} [field R] (abv : R β†’ S) [is_absolute_value abv] theorem abv_inv (a : R) : abv a⁻¹ = (abv a)⁻¹ := (abv_hom abv).map_inv a theorem abv_div (a b : R) : abv (a / b) = abv a / abv b := (abv_hom abv).map_div a b end field end linear_ordered_field end is_absolute_value end is_absolute_value
d02ef23ab2718d6ad725fa4bc474bb9a2f355c77
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/decidable.lean
1d31701f8f9a4b66a1ff54ab001f4943c5428064
[ "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
203
lean
open bool unit decidable constants a b c : bool constants u v : unit set_option pp.implicit true #check if ((a = b) ∧ (b = c) β†’ Β¬ (u = v) ∨ (a = c) β†’ (a = c) ↔ a = tt ↔ true) then a else b