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
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.