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
217a409fa24330097f982908368fec97db60cf76
4727251e0cd73359b15b664c3170e5d754078599
/src/algebra/group/to_additive.lean
cf17f07b07425010d3c5b3d94bda634400496155
[ "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
27,968
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Yury Kudryashov, Floris van Doorn -/ import tactic.transform_decl import tactic.algebra import tactic.lint.basic import tactic.alias /-! # Transport multiplicative to additive This file defines an attribute `to_additive` that can be used to automatically transport theorems and definitions (but not inductive types and structures) from a multiplicative theory to an additive theory. Usage information is contained in the doc string of `to_additive.attr`. ### Missing features * Automatically transport structures and other inductive types. * For structures, automatically generate theorems like `group α ↔ add_group (additive α)`. -/ namespace to_additive open tactic setup_tactic_parser section performance_hack -- see Note [user attribute parameters] local attribute [semireducible] reflected /-- Temporarily change the `has_reflect` instance for `name`. -/ local attribute [instance, priority 9000] meta def hacky_name_reflect : has_reflect name := λ n, `(id %%(expr.const n []) : name) /-- An auxiliary attribute used to store the names of the additive versions of declarations that have been processed by `to_additive`. -/ @[user_attribute] meta def aux_attr : user_attribute (name_map name) name := { name := `to_additive_aux, descr := "Auxiliary attribute for `to_additive`. DON'T USE IT", parser := failed, cache_cfg := ⟨λ ns, ns.mfoldl (λ dict n', do let n := match n' with | name.mk_string s pre := if s = "_to_additive" then pre else n' | _ := n' end, param ← aux_attr.get_param_untyped n', pure $ dict.insert n param.app_arg.const_name) mk_name_map, []⟩ } end performance_hack section extra_attributes /-- An attribute that tells `@[to_additive]` that certain arguments of this definition are not involved when using `@[to_additive]`. This helps the heuristic of `@[to_additive]` by also transforming definitions if `ℕ` or another fixed type occurs as one of these arguments. -/ @[user_attribute] meta def ignore_args_attr : user_attribute (name_map $ list ℕ) (list ℕ) := { name := `to_additive_ignore_args, descr := "Auxiliary attribute for `to_additive` stating that certain arguments are not additivized.", cache_cfg := ⟨λ ns, ns.mfoldl (λ dict n, do param ← ignore_args_attr.get_param_untyped n, -- see Note [user attribute parameters] return $ dict.insert n (param.to_list expr.to_nat).iget) mk_name_map, []⟩, parser := (lean.parser.small_nat)* } /-- An attribute that is automatically added to declarations tagged with `@[to_additive]`, if needed. This attribute tells which argument is the type where this declaration uses the multiplicative structure. If there are multiple argument, we typically tag the first one. If this argument contains a fixed type, this declaration will note be additivized. See the Heuristics section of `to_additive.attr` for more details. If a declaration is not tagged, it is presumed that the first argument is relevant. `@[to_additive]` uses the function `to_additive.first_multiplicative_arg` to automatically tag declarations. It is ok to update it manually if the automatic tagging made an error. Implementation note: we only allow exactly 1 relevant argument, even though some declarations (like `prod.group`) have multiple arguments with a multiplicative structure on it. The reason is that whether we additivize a declaration is an all-or-nothing decision, and if we will not be able to additivize declarations that (e.g.) talk about multiplication on `ℕ × α` anyway. Warning: adding `@[to_additive_reorder]` with an equal or smaller number than the number in this attribute is currently not supported. -/ @[user_attribute] meta def relevant_arg_attr : user_attribute (name_map ℕ) ℕ := { name := `to_additive_relevant_arg, descr := "Auxiliary attribute for `to_additive` stating which arguments are the types with a " ++ "multiplicative structure.", cache_cfg := ⟨λ ns, ns.mfoldl (λ dict n, do param ← relevant_arg_attr.get_param_untyped n, -- see Note [user attribute parameters] -- we subtract 1 from the values provided by the user. return $ dict.insert n $ param.to_nat.iget.pred) mk_name_map, []⟩, parser := lean.parser.small_nat } /-- An attribute that stores all the declarations that needs their arguments reordered when applying `@[to_additive]`. Currently, we only support swapping consecutive arguments. The list of the natural numbers contains the positions of the first of the two arguments to be swapped. If the first two arguments are swapped, the first two universe variables are also swapped. Example: `@[to_additive_reorder 1 4]` swaps the first two arguments and the arguments in positions 4 and 5. -/ @[user_attribute] meta def reorder_attr : user_attribute (name_map $ list ℕ) (list ℕ) := { name := `to_additive_reorder, descr := "Auxiliary attribute for `to_additive` that stores arguments that need to be reordered.", cache_cfg := ⟨λ ns, ns.mfoldl (λ dict n, do param ← reorder_attr.get_param_untyped n, -- see Note [user attribute parameters] return $ dict.insert n (param.to_list expr.to_nat).iget) mk_name_map, []⟩, parser := do l ← (lean.parser.small_nat)*, guard (l.all (≠ 0)) <|> exceptional.fail "The reorder positions must be positive", return l } end extra_attributes /-- Find the first argument of `nm` that has a multiplicative type-class on it. Returns 1 if there are no types with a multiplicative class as arguments. E.g. `prod.group` returns 1, and `pi.has_one` returns 2. -/ meta def first_multiplicative_arg (nm : name) : tactic ℕ := do d ← get_decl nm, let (es, _) := d.type.pi_binders, l ← es.mmap_with_index $ λ n bi, do { let tgt := bi.type.pi_codomain, let n_bi := bi.type.pi_binders.fst.length, tt ← has_attribute' `to_additive tgt.get_app_fn.const_name | return none, let n2 := tgt.get_app_args.head.get_app_fn.match_var.map $ λ m, n + n_bi - m, return $ n2 }, let l := l.reduce_option, return $ if l = [] then 1 else l.foldr min l.head /-- A command that can be used to have future uses of `to_additive` change the `src` namespace to the `tgt` namespace. For example: ``` run_cmd to_additive.map_namespace `quotient_group `quotient_add_group ``` Later uses of `to_additive` on declarations in the `quotient_group` namespace will be created in the `quotient_add_group` namespaces. -/ meta def map_namespace (src tgt : name) : command := do let n := src.mk_string "_to_additive", let decl := declaration.thm n [] `(unit) (pure (reflect ())), add_decl decl, aux_attr.set n tgt tt /-- `value_type` is the type of the arguments that can be provided to `to_additive`. `to_additive.parser` parses the provided arguments: * `replace_all`: replace all multiplicative declarations, do not use the heuristic. * `trace`: output the generated additive declaration. * `tgt : name`: the name of the target (the additive declaration). * `doc`: an optional doc string. * if `allow_auto_name` is `ff` (default) then `@[to_additive]` will check whether the given name can be auto-generated. -/ @[derive has_reflect, derive inhabited] structure value_type : Type := (replace_all : bool) (trace : bool) (tgt : name) (doc : option string) (allow_auto_name : bool) /-- `add_comm_prefix x s` returns `"comm_" ++ s` if `x = tt` and `s` otherwise. -/ meta def add_comm_prefix : bool → string → string | tt s := "comm_" ++ s | ff s := s /-- Dictionary used by `to_additive.guess_name` to autogenerate names. -/ meta def tr : bool → list string → list string | is_comm ("one" :: "le" :: s) := add_comm_prefix is_comm "nonneg" :: tr ff s | is_comm ("one" :: "lt" :: s) := add_comm_prefix is_comm "pos" :: tr ff s | is_comm ("le" :: "one" :: s) := add_comm_prefix is_comm "nonpos" :: tr ff s | is_comm ("lt" :: "one" :: s) := add_comm_prefix is_comm "neg" :: tr ff s | is_comm ("mul" :: "single" :: s) := add_comm_prefix is_comm "single" :: tr ff s | is_comm ("mul" :: "support" :: s) := add_comm_prefix is_comm "support" :: tr ff s | is_comm ("mul" :: "tsupport" :: s) := add_comm_prefix is_comm "tsupport" :: tr ff s | is_comm ("mul" :: "indicator" :: s) := add_comm_prefix is_comm "indicator" :: tr ff s | is_comm ("mul" :: s) := add_comm_prefix is_comm "add" :: tr ff s | is_comm ("smul" :: s) := add_comm_prefix is_comm "vadd" :: tr ff s | is_comm ("inv" :: s) := add_comm_prefix is_comm "neg" :: tr ff s | is_comm ("div" :: s) := add_comm_prefix is_comm "sub" :: tr ff s | is_comm ("one" :: s) := add_comm_prefix is_comm "zero" :: tr ff s | is_comm ("prod" :: s) := add_comm_prefix is_comm "sum" :: tr ff s | is_comm ("finprod" :: s) := add_comm_prefix is_comm "finsum" :: tr ff s | is_comm ("pow" :: s) := add_comm_prefix is_comm "nsmul" :: tr ff s | is_comm ("npow" :: s) := add_comm_prefix is_comm "nsmul" :: tr ff s | is_comm ("zpow" :: s) := add_comm_prefix is_comm "zsmul" :: tr ff s | is_comm ("is" :: "square" :: s) := add_comm_prefix is_comm "even" :: tr ff s | is_comm ("is" :: "regular" :: s) := add_comm_prefix is_comm "is_add_regular" :: tr ff s | is_comm ("is" :: "left" :: "regular" :: s) := add_comm_prefix is_comm "is_add_left_regular" :: tr ff s | is_comm ("is" :: "right" :: "regular" :: s) := add_comm_prefix is_comm "is_add_right_regular" :: tr ff s | is_comm ("monoid" :: s) := ("add_" ++ add_comm_prefix is_comm "monoid") :: tr ff s | is_comm ("submonoid" :: s) := ("add_" ++ add_comm_prefix is_comm "submonoid") :: tr ff s | is_comm ("group" :: s) := ("add_" ++ add_comm_prefix is_comm "group") :: tr ff s | is_comm ("subgroup" :: s) := ("add_" ++ add_comm_prefix is_comm "subgroup") :: tr ff s | is_comm ("semigroup" :: s) := ("add_" ++ add_comm_prefix is_comm "semigroup") :: tr ff s | is_comm ("magma" :: s) := ("add_" ++ add_comm_prefix is_comm "magma") :: tr ff s | is_comm ("haar" :: s) := ("add_" ++ add_comm_prefix is_comm "haar") :: tr ff s | is_comm ("prehaar" :: s) := ("add_" ++ add_comm_prefix is_comm "prehaar") :: tr ff s | is_comm ("unit" :: s) := ("add_" ++ add_comm_prefix is_comm "unit") :: tr ff s | is_comm ("units" :: s) := ("add_" ++ add_comm_prefix is_comm "units") :: tr ff s | is_comm ("comm" :: s) := tr tt s | is_comm (x :: s) := (add_comm_prefix is_comm x :: tr ff s) | tt [] := ["comm"] | ff [] := [] /-- Autogenerate target name for `to_additive`. -/ meta def guess_name : string → string := string.map_tokens ''' $ λ s, string.intercalate (string.singleton '_') $ tr ff (s.split_on '_') /-- Return the provided target name or autogenerate one if one was not provided. -/ meta def target_name (src tgt : name) (dict : name_map name) (allow_auto_name : bool) : tactic name := (if tgt.get_prefix ≠ name.anonymous ∨ allow_auto_name -- `tgt` is a full name then pure tgt else match src with | (name.mk_string s pre) := do let tgt_auto := guess_name s, guard (tgt.to_string ≠ tgt_auto ∨ tgt = src) <|> trace ("`to_additive " ++ src.to_string ++ "`: correctly autogenerated target " ++ "name, you may remove the explicit " ++ tgt_auto ++ " argument."), pure $ name.mk_string (if tgt = name.anonymous then tgt_auto else tgt.to_string) (pre.map_prefix dict.find) | _ := fail ("to_additive: can't transport " ++ src.to_string) end) >>= (λ res, if res = src ∧ tgt ≠ src then fail ("to_additive: can't transport " ++ src.to_string ++ " to itself. Give the desired additive name explicitly using `@[to_additive additive_name]`. ") else pure res) /-- the parser for the arguments to `to_additive`. -/ meta def parser : lean.parser value_type := do bang ← option.is_some <$> (tk "!")?, ques ← option.is_some <$> (tk "?")?, tgt ← ident?, e ← texpr?, doc ← match e with | some pe := some <$> ((to_expr pe >>= eval_expr string) : tactic string) | none := pure none end, return ⟨bang, ques, tgt.get_or_else name.anonymous, doc, ff⟩ private meta def proceed_fields_aux (src tgt : name) (prio : ℕ) (f : name → tactic (list string)) : command := do src_fields ← f src, tgt_fields ← f tgt, guard (src_fields.length = tgt_fields.length) <|> fail ("Failed to map fields of " ++ src.to_string), (src_fields.zip tgt_fields).mmap' $ λ names, guard (names.fst = names.snd) <|> aux_attr.set (src.append names.fst) (tgt.append names.snd) tt prio /-- Add the `aux_attr` attribute to the structure fields of `src` so that future uses of `to_additive` will map them to the corresponding `tgt` fields. -/ meta def proceed_fields (env : environment) (src tgt : name) (prio : ℕ) : command := let aux := proceed_fields_aux src tgt prio in do aux (λ n, pure $ list.map name.to_string $ (env.structure_fields n).get_or_else []) >> aux (λ n, (list.map (λ (x : name), "to_" ++ x.to_string) <$> get_tagged_ancestors n)) >> aux (λ n, (env.constructors_of n).mmap $ λ cs, match cs with | (name.mk_string s pre) := (guard (pre = n) <|> fail "Bad constructor name") >> pure s | _ := fail "Bad constructor name" end) /-- The attribute `to_additive` can be used to automatically transport theorems and definitions (but not inductive types and structures) from a multiplicative theory to an additive theory. To use this attribute, just write: ``` @[to_additive] theorem mul_comm' {α} [comm_semigroup α] (x y : α) : x * y = y * x := comm_semigroup.mul_comm ``` This code will generate a theorem named `add_comm'`. It is also possible to manually specify the name of the new declaration: ``` @[to_additive add_foo] theorem foo := sorry ``` An existing documentation string will _not_ be automatically used, so if the theorem or definition has a doc string, a doc string for the additive version should be passed explicitly to `to_additive`. ``` /-- Multiplication is commutative -/ @[to_additive "Addition is commutative"] theorem mul_comm' {α} [comm_semigroup α] (x y : α) : x * y = y * x := comm_semigroup.mul_comm ``` The transport tries to do the right thing in most cases using several heuristics described below. However, in some cases it fails, and requires manual intervention. If the declaration to be transported has attributes which need to be copied to the additive version, then `to_additive` should come last: ``` @[simp, to_additive] lemma mul_one' {G : Type*} [group G] (x : G) : x * 1 = x := mul_one x ``` The following attributes are supported and should be applied correctly by `to_additive` to the new additivized declaration, if they were present on the original one: ``` reducible, _refl_lemma, simp, norm_cast, instance, refl, symm, trans, elab_as_eliminator, no_rsimp, continuity, ext, ematch, measurability, alias, _ext_core, _ext_lemma_core, nolint ``` The exception to this rule is the `simps` attribute, which should come after `to_additive`: ``` @[to_additive, simps] instance {M N} [has_mul M] [has_mul N] : has_mul (M × N) := ⟨λ p q, ⟨p.1 * q.1, p.2 * q.2⟩⟩ ``` Additionally the `mono` attribute is not handled by `to_additive` and should be applied afterwards to both the original and additivized lemma. ## Implementation notes The transport process generally works by taking all the names of identifiers appearing in the name, type, and body of a declaration and creating a new declaration by mapping those names to additive versions using a simple string-based dictionary and also using all declarations that have previously been labeled with `to_additive`. In the `mul_comm'` example above, `to_additive` maps: * `mul_comm'` to `add_comm'`, * `comm_semigroup` to `add_comm_semigroup`, * `x * y` to `x + y` and `y * x` to `y + x`, and * `comm_semigroup.mul_comm'` to `add_comm_semigroup.add_comm'`. ### Heuristics `to_additive` uses heuristics to determine whether a particular identifier has to be mapped to its additive version. The basic heuristic is * Only map an identifier to its additive version if its first argument doesn't contain any unapplied identifiers. Examples: * `@has_mul.mul ℕ n m` (i.e. `(n * m : ℕ)`) will not change to `+`, since its first argument is `ℕ`, an identifier not applied to any arguments. * `@has_mul.mul (α × β) x y` will change to `+`. It's first argument contains only the identifier `prod`, but this is applied to arguments, `α` and `β`. * `@has_mul.mul (α × ℤ) x y` will not change to `+`, since its first argument contains `ℤ`. The reasoning behind the heuristic is that the first argument is the type which is "additivized", and this usually doesn't make sense if this is on a fixed type. There are some exceptions to this heuristic: * Identifiers that have the `@[to_additive]` attribute are ignored. For example, multiplication in `↥Semigroup` is replaced by addition in `↥AddSemigroup`. * If an identifier `d` has attribute `@[to_additive_relevant_arg n]` then the argument in position `n` is checked for a fixed type, instead of checking the first argument. `@[to_additive]` will automatically add the attribute `@[to_additive_relevant_arg n]` to a declaration when the first argument has no multiplicative type-class, but argument `n` does. * If an identifier has attribute `@[to_additive_ignore_args n1 n2 ...]` then all the arguments in positions `n1`, `n2`, ... will not be checked for unapplied identifiers (start counting from 1). For example, `cont_mdiff_map` has attribute `@[to_additive_ignore_args 21]`, which means that its 21st argument `(n : with_top ℕ)` can contain `ℕ` (usually in the form `has_top.top ℕ ...`) and still be additivized. So `@has_mul.mul (C^∞⟮I, N; I', G⟯) _ f g` will be additivized. ### Troubleshooting If `@[to_additive]` fails because the additive declaration raises a type mismatch, there are various things you can try. The first thing to do is to figure out what `@[to_additive]` did wrong by looking at the type mismatch error. * Option 1: It additivized a declaration `d` that should remain multiplicative. Solution: * Make sure the first argument of `d` is a type with a multiplicative structure. If not, can you reorder the (implicit) arguments of `d` so that the first argument becomes a type with a multiplicative structure (and not some indexing type)? The reason is that `@[to_additive]` doesn't additivize declarations if their first argument contains fixed types like `ℕ` or `ℝ`. See section Heuristics. If the first argument is not the argument with a multiplicative type-class, `@[to_additive]` should have automatically added the attribute `@[to_additive_relevant_arg]` to the declaration. You can test this by running the following (where `d` is the full name of the declaration): ``` run_cmd to_additive.relevant_arg_attr.get_param `d >>= tactic.trace ``` The expected output is `n` where the `n`-th argument of `d` is a type (family) with a multiplicative structure on it. If you get a different output (or a failure), you could add the attribute `@[to_additive_relevant_arg n]` manually, where `n` is an argument with a multiplicative structure. * Option 2: It didn't additivize a declaration that should be additivized. This happened because the heuristic applied, and the first argument contains a fixed type, like `ℕ` or `ℝ`. Solutions: * If the fixed type has an additive counterpart (like `↥Semigroup`), give it the `@[to_additive]` attribute. * If the fixed type occurs inside the `k`-th argument of a declaration `d`, and the `k`-th argument is not connected to the multiplicative structure on `d`, consider adding attribute `[to_additive_ignore_args k]` to `d`. * If you want to disable the heuristic and replace all multiplicative identifiers with their additive counterpart, use `@[to_additive!]`. * Option 3: Arguments / universe levels are incorrectly ordered in the additive version. This likely only happens when the multiplicative declaration involves `pow`/`^`. Solutions: * Ensure that the order of arguments of all relevant declarations are the same for the multiplicative and additive version. This might mean that arguments have an "unnatural" order (e.g. `monoid.npow n x` corresponds to `x ^ n`, but it is convenient that `monoid.npow` has this argument order, since it matches `add_monoid.nsmul n x`. * If this is not possible, add the `[to_additive_reorder k]` to the multiplicative declaration to indicate that the `k`-th and `(k+1)`-st arguments are reordered in the additive version. If neither of these solutions work, and `to_additive` is unable to automatically generate the additive version of a declaration, manually write and prove the additive version. Often the proof of a lemma/theorem can just be the multiplicative version of the lemma applied to `multiplicative G`. Afterwards, apply the attribute manually: ``` attribute [to_additive foo_add_bar] foo_bar ``` This will allow future uses of `to_additive` to recognize that `foo_bar` should be replaced with `foo_add_bar`. ### Handling of hidden definitions Before transporting the “main” declaration `src`, `to_additive` first scans its type and value for names starting with `src`, and transports them. This includes auxiliary definitions like `src._match_1`, `src._proof_1`. In addition to transporting the “main” declaration, `to_additive` transports its equational lemmas and tags them as equational lemmas for the new declaration, attributes present on the original equational lemmas are also transferred first (notably `_refl_lemma`). ### Structure fields and constructors If `src` is a structure, then `to_additive` automatically adds structure fields to its mapping, and similarly for constructors of inductive types. For new structures this means that `to_additive` automatically handles coercions, and for old structures it does the same, if ancestry information is present in `@[ancestor]` attributes. The `ancestor` attribute must come before the `to_additive` attribute, and it is essential that the order of the base structures passed to `ancestor` matches between the multiplicative and additive versions of the structure. ### Name generation * If `@[to_additive]` is called without a `name` argument, then the new name is autogenerated. First, it takes the longest prefix of the source name that is already known to `to_additive`, and replaces this prefix with its additive counterpart. Second, it takes the last part of the name (i.e., after the last dot), and replaces common name parts (“mul”, “one”, “inv”, “prod”) with their additive versions. * Namespaces can be transformed using `map_namespace`. For example: ``` run_cmd to_additive.map_namespace `quotient_group `quotient_add_group ``` Later uses of `to_additive` on declarations in the `quotient_group` namespace will be created in the `quotient_add_group` namespaces. * If `@[to_additive]` is called with a `name` argument `new_name` /without a dot/, then `to_additive` updates the prefix as described above, then replaces the last part of the name with `new_name`. * If `@[to_additive]` is called with a `name` argument `new_namespace.new_name` /with a dot/, then `to_additive` uses this new name as is. As a safety check, in the first case `to_additive` double checks that the new name differs from the original one. -/ @[user_attribute] protected meta def attr : user_attribute unit value_type := { name := `to_additive, descr := "Transport multiplicative to additive", parser := parser, after_set := some $ λ src prio persistent, do guard persistent <|> fail "`to_additive` can't be used as a local attribute", env ← get_env, val ← attr.get_param src, dict ← aux_attr.get_cache, ignore ← ignore_args_attr.get_cache, relevant ← relevant_arg_attr.get_cache, reorder ← reorder_attr.get_cache, tgt ← target_name src val.tgt dict val.allow_auto_name, aux_attr.set src tgt tt, let dict := dict.insert src tgt, first_mult_arg ← first_multiplicative_arg src, when (first_mult_arg ≠ 1) $ relevant_arg_attr.set src first_mult_arg tt, if env.contains tgt then proceed_fields env src tgt prio else do transform_decl_with_prefix_dict dict val.replace_all val.trace relevant ignore reorder src tgt [`reducible, `_refl_lemma, `simp, `norm_cast, `instance, `refl, `symm, `trans, `elab_as_eliminator, `no_rsimp, `continuity, `ext, `ematch, `measurability, `alias, `_ext_core, `_ext_lemma_core, `nolint, `protected], mwhen (has_attribute' `simps src) (trace "Apply the simps attribute after the to_additive attribute"), mwhen (has_attribute' `mono src) (trace $ "to_additive does not work with mono, apply the mono attribute to both" ++ "versions after"), match val.doc with | some doc := add_doc_string tgt doc | none := do some alias_target ← tactic.alias.get_alias_target src | skip, let alias_name := alias_target.to_name, some add_alias_name ← pure (dict.find alias_name) | skip, add_doc_string tgt alias_target.to_string end } add_tactic_doc { name := "to_additive", category := doc_category.attr, decl_names := [`to_additive.attr], tags := ["transport", "environment", "lemma derivation"] } end to_additive /- map operations -/ attribute [to_additive] has_mul has_one has_inv has_div /- the following types are supported by `@[to_additive]` and mapped to themselves. -/ attribute [to_additive empty] empty attribute [to_additive pempty] pempty attribute [to_additive punit] punit attribute [to_additive unit] unit section linter open tactic expr /-- A linter that checks that multiplicative and additive lemmas have both doc strings if one of them has one -/ @[linter] meta def linter.to_additive_doc : linter := { test := (λ d, do let mul_name := d.to_name, dict ← to_additive.aux_attr.get_cache, match dict.find mul_name with | some add_name := do mul_doc ← try_core $ doc_string mul_name, add_doc ← try_core $ doc_string add_name, match mul_doc.is_some, add_doc.is_some with | tt, ff := return $ some $ "declaration has a docstring, but its additive version `" ++ add_name.to_string ++ "` does not. You might want to pass a string argument to " ++ "`to_additive`." | ff, tt := return $ some $ "declaration has no docstring, but its additive version `" ++ add_name.to_string ++ "` does. You might want to add a doc string to the declaration." | _, _ := return none end | none := return none end), auto_decls := ff, no_errors_found := "Multiplicative and additive lemmas are consistently documented", errors_found := "The following declarations have doc strings, but their additive versions do " ++ "not (or vice versa).", is_fast := ff } end linter
c981165838bf7f7d51e0793bf539caf9d9f774f1
4727251e0cd73359b15b664c3170e5d754078599
/src/linear_algebra/matrix/determinant.lean
a8c5563a6fc1c954f610dc5342493a989ca962bc
[ "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
31,514
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Chris Hughes, Tim Baanen -/ import data.matrix.pequiv import data.matrix.block import data.fintype.card import group_theory.perm.fin import group_theory.perm.sign import algebra.algebra.basic import tactic.ring import linear_algebra.alternating import linear_algebra.pi /-! # Determinant of a matrix This file defines the determinant of a matrix, `matrix.det`, and its essential properties. ## Main definitions - `matrix.det`: the determinant of a square matrix, as a sum over permutations - `matrix.det_row_alternating`: the determinant, as an `alternating_map` in the rows of the matrix ## Main results - `det_mul`: the determinant of `A ⬝ B` is the product of determinants - `det_zero_of_row_eq`: the determinant is zero if there is a repeated row - `det_block_diagonal`: the determinant of a block diagonal matrix is a product of the blocks' determinants ## Implementation notes It is possible to configure `simp` to compute determinants. See the file `test/matrix.lean` for some examples. -/ universes u v w z open equiv equiv.perm finset function namespace matrix open_locale matrix big_operators variables {m n : Type*} [decidable_eq n] [fintype n] [decidable_eq m] [fintype m] variables {R : Type v} [comm_ring R] local notation `ε` σ:max := ((sign σ : ℤ ) : R) /-- `det` is an `alternating_map` in the rows of the matrix. -/ def det_row_alternating : alternating_map R (n → R) R n := ((multilinear_map.mk_pi_algebra R n R).comp_linear_map (linear_map.proj)).alternatization /-- The determinant of a matrix given by the Leibniz formula. -/ abbreviation det (M : matrix n n R) : R := det_row_alternating M lemma det_apply (M : matrix n n R) : M.det = ∑ σ : perm n, σ.sign • ∏ i, M (σ i) i := multilinear_map.alternatization_apply _ M -- This is what the old definition was. We use it to avoid having to change the old proofs below lemma det_apply' (M : matrix n n R) : M.det = ∑ σ : perm n, ε σ * ∏ i, M (σ i) i := by simp [det_apply, units.smul_def] @[simp] lemma det_diagonal {d : n → R} : det (diagonal d) = ∏ i, d i := begin rw det_apply', refine (finset.sum_eq_single 1 _ _).trans _, { intros σ h1 h2, cases not_forall.1 (mt equiv.ext h2) with x h3, convert mul_zero _, apply finset.prod_eq_zero, { change x ∈ _, simp }, exact if_neg h3 }, { simp }, { simp } end @[simp] lemma det_zero (h : nonempty n) : det (0 : matrix n n R) = 0 := (det_row_alternating : alternating_map R (n → R) R n).map_zero @[simp] lemma det_one : det (1 : matrix n n R) = 1 := by rw [← diagonal_one]; simp [-diagonal_one] lemma det_is_empty [is_empty n] {A : matrix n n R} : det A = 1 := by simp [det_apply] @[simp] lemma coe_det_is_empty [is_empty n] : (det : matrix n n R → R) = function.const _ 1 := by { ext, exact det_is_empty, } lemma det_eq_one_of_card_eq_zero {A : matrix n n R} (h : fintype.card n = 0) : det A = 1 := begin haveI : is_empty n := fintype.card_eq_zero_iff.mp h, exact det_is_empty, end /-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element. Although `unique` implies `decidable_eq` and `fintype`, the instances might not be syntactically equal. Thus, we need to fill in the args explicitly. -/ @[simp] lemma det_unique {n : Type*} [unique n] [decidable_eq n] [fintype n] (A : matrix n n R) : det A = A default default := by simp [det_apply, univ_unique] lemma det_eq_elem_of_subsingleton [subsingleton n] (A : matrix n n R) (k : n) : det A = A k k := begin convert det_unique _, exact unique_of_subsingleton k end lemma det_eq_elem_of_card_eq_one {A : matrix n n R} (h : fintype.card n = 1) (k : n) : det A = A k k := begin haveI : subsingleton n := fintype.card_le_one_iff_subsingleton.mp h.le, exact det_eq_elem_of_subsingleton _ _ end lemma det_mul_aux {M N : matrix n n R} {p : n → n} (H : ¬bijective p) : ∑ σ : perm n, (ε σ) * ∏ x, (M (σ x) (p x) * N (p x) x) = 0 := begin obtain ⟨i, j, hpij, hij⟩ : ∃ i j, p i = p j ∧ i ≠ j, { rw [← fintype.injective_iff_bijective, injective] at H, push_neg at H, exact H }, exact sum_involution (λ σ _, σ * swap i j) (λ σ _, have ∏ x, M (σ x) (p x) = ∏ x, M ((σ * swap i j) x) (p x), from fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij]), by simp [this, sign_swap hij, prod_mul_distrib]) (λ σ _ _, (not_congr mul_swap_eq_iff).mpr hij) (λ _ _, mem_univ _) (λ σ _, mul_swap_involutive i j σ) end @[simp] lemma det_mul (M N : matrix n n R) : det (M ⬝ N) = det M * det N := calc det (M ⬝ N) = ∑ p : n → n, ∑ σ : perm n, ε σ * ∏ i, (M (σ i) (p i) * N (p i) i) : by simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, fintype.pi_finset_univ]; rw [finset.sum_comm] ... = ∑ p in (@univ (n → n) _).filter bijective, ∑ σ : perm n, ε σ * ∏ i, (M (σ i) (p i) * N (p i) i) : eq.symm $ sum_subset (filter_subset _ _) (λ f _ hbij, det_mul_aux $ by simpa only [true_and, mem_filter, mem_univ] using hbij) ... = ∑ τ : perm n, ∑ σ : perm n, ε σ * ∏ i, (M (σ i) (τ i) * N (τ i) i) : sum_bij (λ p h, equiv.of_bijective p (mem_filter.1 h).2) (λ _ _, mem_univ _) (λ _ _, rfl) (λ _ _ _ _ h, by injection h) (λ b _, ⟨b, mem_filter.2 ⟨mem_univ _, b.bijective⟩, coe_fn_injective rfl⟩) ... = ∑ σ : perm n, ∑ τ : perm n, (∏ i, N (σ i) i) * ε τ * (∏ j, M (τ j) (σ j)) : by simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc] ... = ∑ σ : perm n, ∑ τ : perm n, (((∏ i, N (σ i) i) * (ε σ * ε τ)) * ∏ i, M (τ i) i) : sum_congr rfl (λ σ _, fintype.sum_equiv (equiv.mul_right σ⁻¹) _ _ (λ τ, have ∏ j, M (τ j) (σ j) = ∏ j, M ((τ * σ⁻¹) j) j, by { rw ← (σ⁻¹ : _ ≃ _).prod_comp, simp only [equiv.perm.coe_mul, apply_inv_self] }, have h : ε σ * ε (τ * σ⁻¹) = ε τ := calc ε σ * ε (τ * σ⁻¹) = ε ((τ * σ⁻¹) * σ) : by { rw [mul_comm, sign_mul (τ * σ⁻¹)], simp only [int.cast_mul, units.coe_mul] } ... = ε τ : by simp only [inv_mul_cancel_right], by { simp_rw [equiv.coe_mul_right, h], simp only [this] })) ... = det M * det N : by simp only [det_apply', finset.mul_sum, mul_comm, mul_left_comm] /-- The determinant of a matrix, as a monoid homomorphism. -/ def det_monoid_hom : matrix n n R →* R := { to_fun := det, map_one' := det_one, map_mul' := det_mul } @[simp] lemma coe_det_monoid_hom : (det_monoid_hom : matrix n n R → R) = det := rfl /-- On square matrices, `mul_comm` applies under `det`. -/ lemma det_mul_comm (M N : matrix m m R) : det (M ⬝ N) = det (N ⬝ M) := by rw [det_mul, det_mul, mul_comm] /-- On square matrices, `mul_left_comm` applies under `det`. -/ lemma det_mul_left_comm (M N P : matrix m m R) : det (M ⬝ (N ⬝ P)) = det (N ⬝ (M ⬝ P)) := by rw [←matrix.mul_assoc, ←matrix.mul_assoc, det_mul, det_mul_comm M N, ←det_mul] /-- On square matrices, `mul_right_comm` applies under `det`. -/ lemma det_mul_right_comm (M N P : matrix m m R) : det (M ⬝ N ⬝ P) = det (M ⬝ P ⬝ N) := by rw [matrix.mul_assoc, matrix.mul_assoc, det_mul, det_mul_comm N P, ←det_mul] lemma det_units_conj (M : (matrix m m R)ˣ) (N : matrix m m R) : det (↑M ⬝ N ⬝ ↑M⁻¹ : matrix m m R) = det N := by rw [det_mul_right_comm, ←mul_eq_mul, ←mul_eq_mul, units.mul_inv, one_mul] lemma det_units_conj' (M : (matrix m m R)ˣ) (N : matrix m m R) : det (↑M⁻¹ ⬝ N ⬝ ↑M : matrix m m R) = det N := det_units_conj M⁻¹ N /-- Transposing a matrix preserves the determinant. -/ @[simp] lemma det_transpose (M : matrix n n R) : Mᵀ.det = M.det := begin rw [det_apply', det_apply'], refine fintype.sum_bijective _ inv_involutive.bijective _ _ _, intros σ, rw sign_inv, congr' 1, apply fintype.prod_equiv σ, intros, simp end /-- Permuting the columns changes the sign of the determinant. -/ lemma det_permute (σ : perm n) (M : matrix n n R) : matrix.det (λ i, M (σ i)) = σ.sign * M.det := ((det_row_alternating : alternating_map R (n → R) R n).map_perm M σ).trans (by simp [units.smul_def]) /-- Permuting rows and columns with the same equivalence has no effect. -/ @[simp] lemma det_minor_equiv_self (e : n ≃ m) (A : matrix m m R) : det (A.minor e e) = det A := begin rw [det_apply', det_apply'], apply fintype.sum_equiv (equiv.perm_congr e), intro σ, rw equiv.perm.sign_perm_congr e σ, congr' 1, apply fintype.prod_equiv e, intro i, rw [equiv.perm_congr_apply, equiv.symm_apply_apply, minor_apply], end /-- Reindexing both indices along the same equivalence preserves the determinant. For the `simp` version of this lemma, see `det_minor_equiv_self`; this one is unsuitable because `matrix.reindex_apply` unfolds `reindex` first. -/ lemma det_reindex_self (e : m ≃ n) (A : matrix m m R) : det (reindex e e A) = det A := det_minor_equiv_self e.symm A /-- The determinant of a permutation matrix equals its sign. -/ @[simp] lemma det_permutation (σ : perm n) : matrix.det (σ.to_pequiv.to_matrix : matrix n n R) = σ.sign := by rw [←matrix.mul_one (σ.to_pequiv.to_matrix : matrix n n R), pequiv.to_pequiv_mul_matrix, det_permute, det_one, mul_one] lemma det_smul (A : matrix n n R) (c : R) : det (c • A) = c ^ fintype.card n * det A := calc det (c • A) = det (matrix.mul (diagonal (λ _, c)) A) : by rw [smul_eq_diagonal_mul] ... = det (diagonal (λ _, c)) * det A : det_mul _ _ ... = c ^ fintype.card n * det A : by simp [card_univ] @[simp] lemma det_smul_of_tower {α} [monoid α] [distrib_mul_action α R] [is_scalar_tower α R R] [smul_comm_class α R R] (c : α) (A : matrix n n R) : det (c • A) = c ^ fintype.card n • det A := by rw [←smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul] lemma det_neg (A : matrix n n R) : det (-A) = (-1) ^ fintype.card n * det A := by rw [←det_smul, neg_one_smul] /-- A variant of `matrix.det_neg` with scalar multiplication by `units ℤ` instead of multiplication by `R`. -/ lemma det_neg_eq_smul (A : matrix n n R) : det (-A) = (-1 : units ℤ) ^ fintype.card n • det A := by rw [←det_smul_of_tower, units.neg_smul, one_smul] /-- Multiplying each row by a fixed `v i` multiplies the determinant by the product of the `v`s. -/ lemma det_mul_row (v : n → R) (A : matrix n n R) : det (λ i j, v j * A i j) = (∏ i, v i) * det A := calc det (λ i j, v j * A i j) = det (A ⬝ diagonal v) : congr_arg det $ by { ext, simp [mul_comm] } ... = (∏ i, v i) * det A : by rw [det_mul, det_diagonal, mul_comm] /-- Multiplying each column by a fixed `v j` multiplies the determinant by the product of the `v`s. -/ lemma det_mul_column (v : n → R) (A : matrix n n R) : det (λ i j, v i * A i j) = (∏ i, v i) * det A := multilinear_map.map_smul_univ _ v A @[simp] lemma det_pow (M : matrix m m R) (n : ℕ) : det (M ^ n) = (det M) ^ n := (det_monoid_hom : matrix m m R →* R).map_pow M n section hom_map variables {S : Type w} [comm_ring S] lemma _root_.ring_hom.map_det (f : R →+* S) (M : matrix n n R) : f M.det = matrix.det (f.map_matrix M) := by simp [matrix.det_apply', f.map_sum, f.map_prod] lemma _root_.ring_equiv.map_det (f : R ≃+* S) (M : matrix n n R) : f M.det = matrix.det (f.map_matrix M) := f.to_ring_hom.map_det _ lemma _root_.alg_hom.map_det [algebra R S] {T : Type z} [comm_ring T] [algebra R T] (f : S →ₐ[R] T) (M : matrix n n S) : f M.det = matrix.det (f.map_matrix M) := f.to_ring_hom.map_det _ lemma _root_.alg_equiv.map_det [algebra R S] {T : Type z} [comm_ring T] [algebra R T] (f : S ≃ₐ[R] T) (M : matrix n n S) : f M.det = matrix.det (f.map_matrix M) := f.to_alg_hom.map_det _ end hom_map @[simp] lemma det_conj_transpose [star_ring R] (M : matrix m m R) : det (Mᴴ) = star (det M) := ((star_ring_end R).map_det _).symm.trans $ congr_arg star M.det_transpose section det_zero /-! ### `det_zero` section Prove that a matrix with a repeated column has determinant equal to zero. -/ lemma det_eq_zero_of_row_eq_zero {A : matrix n n R} (i : n) (h : ∀ j, A i j = 0) : det A = 0 := (det_row_alternating : alternating_map R (n → R) R n).map_coord_zero i (funext h) lemma det_eq_zero_of_column_eq_zero {A : matrix n n R} (j : n) (h : ∀ i, A i j = 0) : det A = 0 := by { rw ← det_transpose, exact det_eq_zero_of_row_eq_zero j h, } variables {M : matrix n n R} {i j : n} /-- If a matrix has a repeated row, the determinant will be zero. -/ theorem det_zero_of_row_eq (i_ne_j : i ≠ j) (hij : M i = M j) : M.det = 0 := (det_row_alternating : alternating_map R (n → R) R n).map_eq_zero_of_eq M hij i_ne_j /-- If a matrix has a repeated column, the determinant will be zero. -/ theorem det_zero_of_column_eq (i_ne_j : i ≠ j) (hij : ∀ k, M k i = M k j) : M.det = 0 := by { rw [← det_transpose, det_zero_of_row_eq i_ne_j], exact funext hij } end det_zero lemma det_update_row_add (M : matrix n n R) (j : n) (u v : n → R) : det (update_row M j $ u + v) = det (update_row M j u) + det (update_row M j v) := (det_row_alternating : alternating_map R (n → R) R n).map_add M j u v lemma det_update_column_add (M : matrix n n R) (j : n) (u v : n → R) : det (update_column M j $ u + v) = det (update_column M j u) + det (update_column M j v) := begin rw [← det_transpose, ← update_row_transpose, det_update_row_add], simp [update_row_transpose, det_transpose] end lemma det_update_row_smul (M : matrix n n R) (j : n) (s : R) (u : n → R) : det (update_row M j $ s • u) = s * det (update_row M j u) := (det_row_alternating : alternating_map R (n → R) R n).map_smul M j s u lemma det_update_column_smul (M : matrix n n R) (j : n) (s : R) (u : n → R) : det (update_column M j $ s • u) = s * det (update_column M j u) := begin rw [← det_transpose, ← update_row_transpose, det_update_row_smul], simp [update_row_transpose, det_transpose] end lemma det_update_row_smul' (M : matrix n n R) (j : n) (s : R) (u : n → R) : det (update_row (s • M) j u) = s ^ (fintype.card n - 1) * det (update_row M j u) := multilinear_map.map_update_smul _ M j s u lemma det_update_column_smul' (M : matrix n n R) (j : n) (s : R) (u : n → R) : det (update_column (s • M) j u) = s ^ (fintype.card n - 1) * det (update_column M j u) := begin rw [← det_transpose, ← update_row_transpose, transpose_smul, det_update_row_smul'], simp [update_row_transpose, det_transpose] end section det_eq /-! ### `det_eq` section Lemmas showing the determinant is invariant under a variety of operations. -/ lemma det_eq_of_eq_mul_det_one {A B : matrix n n R} (C : matrix n n R) (hC : det C = 1) (hA : A = B ⬝ C) : det A = det B := calc det A = det (B ⬝ C) : congr_arg _ hA ... = det B * det C : det_mul _ _ ... = det B : by rw [hC, mul_one] lemma det_eq_of_eq_det_one_mul {A B : matrix n n R} (C : matrix n n R) (hC : det C = 1) (hA : A = C ⬝ B) : det A = det B := calc det A = det (C ⬝ B) : congr_arg _ hA ... = det C * det B : det_mul _ _ ... = det B : by rw [hC, one_mul] lemma det_update_row_add_self (A : matrix n n R) {i j : n} (hij : i ≠ j) : det (update_row A i (A i + A j)) = det A := by simp [det_update_row_add, det_zero_of_row_eq hij ((update_row_self).trans (update_row_ne hij.symm).symm)] lemma det_update_column_add_self (A : matrix n n R) {i j : n} (hij : i ≠ j) : det (update_column A i (λ k, A k i + A k j)) = det A := by { rw [← det_transpose, ← update_row_transpose, ← det_transpose A], exact det_update_row_add_self Aᵀ hij } lemma det_update_row_add_smul_self (A : matrix n n R) {i j : n} (hij : i ≠ j) (c : R) : det (update_row A i (A i + c • A j)) = det A := by simp [det_update_row_add, det_update_row_smul, det_zero_of_row_eq hij ((update_row_self).trans (update_row_ne hij.symm).symm)] lemma det_update_column_add_smul_self (A : matrix n n R) {i j : n} (hij : i ≠ j) (c : R) : det (update_column A i (λ k, A k i + c • A k j)) = det A := by { rw [← det_transpose, ← update_row_transpose, ← det_transpose A], exact det_update_row_add_smul_self Aᵀ hij c } lemma det_eq_of_forall_row_eq_smul_add_const_aux {A B : matrix n n R} {s : finset n} : ∀ (c : n → R) (hs : ∀ i, i ∉ s → c i = 0) (k : n) (hk : k ∉ s) (A_eq : ∀ i j, A i j = B i j + c i * B k j), det A = det B := begin revert B, refine s.induction_on _ _, { intros A c hs k hk A_eq, have : ∀ i, c i = 0, { intros i, specialize hs i, contrapose! hs, simp [hs] }, congr, ext i j, rw [A_eq, this, zero_mul, add_zero], }, { intros i s hi ih B c hs k hk A_eq, have hAi : A i = B i + c i • B k := funext (A_eq i), rw [@ih (update_row B i (A i)) (function.update c i 0), hAi, det_update_row_add_smul_self], { exact mt (λ h, show k ∈ insert i s, from h ▸ finset.mem_insert_self _ _) hk }, { intros i' hi', rw function.update_apply, split_ifs with hi'i, { refl }, { exact hs i' (λ h, hi' ((finset.mem_insert.mp h).resolve_left hi'i)) } }, { exact λ h, hk (finset.mem_insert_of_mem h) }, { intros i' j', rw [update_row_apply, function.update_apply], split_ifs with hi'i, { simp [hi'i] }, rw [A_eq, update_row_ne (λ (h : k = i), hk $ h ▸ finset.mem_insert_self k s)] } } end /-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/ lemma det_eq_of_forall_row_eq_smul_add_const {A B : matrix n n R} (c : n → R) (k : n) (hk : c k = 0) (A_eq : ∀ i j, A i j = B i j + c i * B k j) : det A = det B := det_eq_of_forall_row_eq_smul_add_const_aux c (λ i, not_imp_comm.mp $ λ hi, finset.mem_erase.mpr ⟨mt (λ (h : i = k), show c i = 0, from h.symm ▸ hk) hi, finset.mem_univ i⟩) k (finset.not_mem_erase k finset.univ) A_eq lemma det_eq_of_forall_row_eq_smul_add_pred_aux {n : ℕ} (k : fin (n + 1)) : ∀ (c : fin n → R) (hc : ∀ (i : fin n), k < i.succ → c i = 0) {M N : matrix (fin n.succ) (fin n.succ) R} (h0 : ∀ j, M 0 j = N 0 j) (hsucc : ∀ (i : fin n) j, M i.succ j = N i.succ j + c i * M i.cast_succ j), det M = det N := begin refine fin.induction _ (λ k ih, _) k; intros c hc M N h0 hsucc, { congr, ext i j, refine fin.cases (h0 j) (λ i, _) i, rw [hsucc, hc i (fin.succ_pos _), zero_mul, add_zero] }, set M' := update_row M k.succ (N k.succ) with hM', have hM : M = update_row M' k.succ (M' k.succ + c k • M k.cast_succ), { ext i j, by_cases hi : i = k.succ, { simp [hi, hM', hsucc, update_row_self] }, rw [update_row_ne hi, hM', update_row_ne hi] }, have k_ne_succ : k.cast_succ ≠ k.succ := (fin.cast_succ_lt_succ k).ne, have M_k : M k.cast_succ = M' k.cast_succ := (update_row_ne k_ne_succ).symm, rw [hM, M_k, det_update_row_add_smul_self M' k_ne_succ.symm, ih (function.update c k 0)], { intros i hi, rw [fin.lt_iff_coe_lt_coe, fin.coe_cast_succ, fin.coe_succ, nat.lt_succ_iff] at hi, rw function.update_apply, split_ifs with hik, { refl }, exact hc _ (fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (ne.symm hik))) }, { rwa [hM', update_row_ne (fin.succ_ne_zero _).symm] }, intros i j, rw function.update_apply, split_ifs with hik, { rw [zero_mul, add_zero, hM', hik, update_row_self] }, rw [hM', update_row_ne ((fin.succ_injective _).ne hik), hsucc], by_cases hik2 : k < i, { simp [hc i (fin.succ_lt_succ_iff.mpr hik2)] }, rw update_row_ne, apply ne_of_lt, rwa [fin.lt_iff_coe_lt_coe, fin.coe_cast_succ, fin.coe_succ, nat.lt_succ_iff, ← not_lt] end /-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/ lemma det_eq_of_forall_row_eq_smul_add_pred {n : ℕ} {A B : matrix (fin (n + 1)) (fin (n + 1)) R} (c : fin n → R) (A_zero : ∀ j, A 0 j = B 0 j) (A_succ : ∀ (i : fin n) j, A i.succ j = B i.succ j + c i * A i.cast_succ j) : det A = det B := det_eq_of_forall_row_eq_smul_add_pred_aux (fin.last _) c (λ i hi, absurd hi (not_lt_of_ge (fin.le_last _))) A_zero A_succ /-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/ lemma det_eq_of_forall_col_eq_smul_add_pred {n : ℕ} {A B : matrix (fin (n + 1)) (fin (n + 1)) R} (c : fin n → R) (A_zero : ∀ i, A i 0 = B i 0) (A_succ : ∀ i (j : fin n), A i j.succ = B i j.succ + c j * A i j.cast_succ) : det A = det B := by { rw [← det_transpose A, ← det_transpose B], exact det_eq_of_forall_row_eq_smul_add_pred c A_zero (λ i j, A_succ j i) } end det_eq @[simp] lemma det_block_diagonal {o : Type*} [fintype o] [decidable_eq o] (M : o → matrix n n R) : (block_diagonal M).det = ∏ k, (M k).det := begin -- Rewrite the determinants as a sum over permutations. simp_rw [det_apply'], -- The right hand side is a product of sums, rewrite it as a sum of products. rw finset.prod_sum, simp_rw [finset.mem_univ, finset.prod_attach_univ, finset.univ_pi_univ], -- We claim that the only permutations contributing to the sum are those that -- preserve their second component. let preserving_snd : finset (equiv.perm (n × o)) := finset.univ.filter (λ σ, ∀ x, (σ x).snd = x.snd), have mem_preserving_snd : ∀ {σ : equiv.perm (n × o)}, σ ∈ preserving_snd ↔ ∀ x, (σ x).snd = x.snd := λ σ, finset.mem_filter.trans ⟨λ h, h.2, λ h, ⟨finset.mem_univ _, h⟩⟩, rw ← finset.sum_subset (finset.subset_univ preserving_snd) _, -- And that these are in bijection with `o → equiv.perm m`. rw (finset.sum_bij (λ (σ : ∀ (k : o), k ∈ finset.univ → equiv.perm n) _, prod_congr_left (λ k, σ k (finset.mem_univ k))) _ _ _ _).symm, { intros σ _, rw mem_preserving_snd, rintros ⟨k, x⟩, simp only [prod_congr_left_apply] }, { intros σ _, rw [finset.prod_mul_distrib, ←finset.univ_product_univ, finset.prod_product_right], simp only [sign_prod_congr_left, units.coe_prod, int.cast_prod, block_diagonal_apply_eq, prod_congr_left_apply] }, { intros σ σ' _ _ eq, ext x hx k, simp only at eq, have : ∀ k x, prod_congr_left (λ k, σ k (finset.mem_univ _)) (k, x) = prod_congr_left (λ k, σ' k (finset.mem_univ _)) (k, x) := λ k x, by rw eq, simp only [prod_congr_left_apply, prod.mk.inj_iff] at this, exact (this k x).1 }, { intros σ hσ, rw mem_preserving_snd at hσ, have hσ' : ∀ x, (σ⁻¹ x).snd = x.snd, { intro x, conv_rhs { rw [← perm.apply_inv_self σ x, hσ] } }, have mk_apply_eq : ∀ k x, ((σ (x, k)).fst, k) = σ (x, k), { intros k x, ext, { simp only}, { simp only [hσ] } }, have mk_inv_apply_eq : ∀ k x, ((σ⁻¹ (x, k)).fst, k) = σ⁻¹ (x, k), { intros k x, conv_lhs { rw ← perm.apply_inv_self σ (x, k) }, ext, { simp only [apply_inv_self] }, { simp only [hσ'] } }, refine ⟨λ k _, ⟨λ x, (σ (x, k)).fst, λ x, (σ⁻¹ (x, k)).fst, _, _⟩, _, _⟩, { intro x, simp only [mk_apply_eq, inv_apply_self] }, { intro x, simp only [mk_inv_apply_eq, apply_inv_self] }, { apply finset.mem_univ }, { ext ⟨k, x⟩, { simp only [coe_fn_mk, prod_congr_left_apply] }, { simp only [prod_congr_left_apply, hσ] } } }, { intros σ _ hσ, rw mem_preserving_snd at hσ, obtain ⟨⟨k, x⟩, hkx⟩ := not_forall.mp hσ, rw [finset.prod_eq_zero (finset.mem_univ (k, x)), mul_zero], rw [← @prod.mk.eta _ _ (σ (k, x)), block_diagonal_apply_ne], exact hkx } end /-- The determinant of a 2×2 block matrix with the lower-left block equal to zero is the product of the determinants of the diagonal blocks. For the generalization to any number of blocks, see `matrix.det_of_upper_triangular`. -/ @[simp] lemma det_from_blocks_zero₂₁ (A : matrix m m R) (B : matrix m n R) (D : matrix n n R) : (matrix.from_blocks A B 0 D).det = A.det * D.det := begin classical, simp_rw det_apply', convert (sum_subset (subset_univ ((sum_congr_hom m n).range : set (perm (m ⊕ n))).to_finset) _).symm, rw sum_mul_sum, simp_rw univ_product_univ, rw (sum_bij (λ (σ : perm m × perm n) _, equiv.sum_congr σ.fst σ.snd) _ _ _ _).symm, { intros σ₁₂ h, simp only [], erw [set.mem_to_finset, monoid_hom.mem_range], use σ₁₂, simp only [sum_congr_hom_apply] }, { simp only [forall_prop_of_true, prod.forall, mem_univ], intros σ₁ σ₂, rw fintype.prod_sum_type, simp_rw [equiv.sum_congr_apply, sum.map_inr, sum.map_inl, from_blocks_apply₁₁, from_blocks_apply₂₂], rw mul_mul_mul_comm, congr, rw [sign_sum_congr, units.coe_mul, int.cast_mul] }, { intros σ₁ σ₂ h₁ h₂, dsimp only [], intro h, have h2 : ∀ x, perm.sum_congr σ₁.fst σ₁.snd x = perm.sum_congr σ₂.fst σ₂.snd x, { intro x, exact congr_fun (congr_arg to_fun h) x }, simp only [sum.map_inr, sum.map_inl, perm.sum_congr_apply, sum.forall] at h2, ext, { exact h2.left x }, { exact h2.right x }}, { intros σ hσ, erw [set.mem_to_finset, monoid_hom.mem_range] at hσ, obtain ⟨σ₁₂, hσ₁₂⟩ := hσ, use σ₁₂, rw ←hσ₁₂, simp }, { intros σ hσ hσn, have h1 : ¬ (∀ x, ∃ y, sum.inl y = σ (sum.inl x)), { by_contradiction, rw set.mem_to_finset at hσn, apply absurd (mem_sum_congr_hom_range_of_perm_maps_to_inl _) hσn, rintros x ⟨a, ha⟩, rw [←ha], exact h a }, obtain ⟨a, ha⟩ := not_forall.mp h1, cases hx : σ (sum.inl a) with a2 b, { have hn := (not_exists.mp ha) a2, exact absurd hx.symm hn }, { rw [finset.prod_eq_zero (finset.mem_univ (sum.inl a)), mul_zero], rw [hx, from_blocks_apply₂₁], refl }} end /-- The determinant of a 2×2 block matrix with the upper-right block equal to zero is the product of the determinants of the diagonal blocks. For the generalization to any number of blocks, see `matrix.det_of_lower_triangular`. -/ @[simp] lemma det_from_blocks_zero₁₂ (A : matrix m m R) (C : matrix n m R) (D : matrix n n R) : (matrix.from_blocks A 0 C D).det = A.det * D.det := by rw [←det_transpose, from_blocks_transpose, transpose_zero, det_from_blocks_zero₂₁, det_transpose, det_transpose] /-- Laplacian expansion of the determinant of an `n+1 × n+1` matrix along column 0. -/ lemma det_succ_column_zero {n : ℕ} (A : matrix (fin n.succ) (fin n.succ) R) : det A = ∑ i : fin n.succ, (-1) ^ (i : ℕ) * A i 0 * det (A.minor i.succ_above fin.succ) := begin rw [matrix.det_apply, finset.univ_perm_fin_succ, ← finset.univ_product_univ], simp only [finset.sum_map, equiv.to_embedding_apply, finset.sum_product, matrix.minor], refine finset.sum_congr rfl (λ i _, fin.cases _ (λ i, _) i), { simp only [fin.prod_univ_succ, matrix.det_apply, finset.mul_sum, equiv.perm.decompose_fin_symm_apply_zero, fin.coe_zero, one_mul, equiv.perm.decompose_fin.symm_sign, equiv.swap_self, if_true, id.def, eq_self_iff_true, equiv.perm.decompose_fin_symm_apply_succ, fin.succ_above_zero, equiv.coe_refl, pow_zero, mul_smul_comm] }, -- `univ_perm_fin_succ` gives a different embedding of `perm (fin n)` into -- `perm (fin n.succ)` than the determinant of the submatrix we want, -- permute `A` so that we get the correct one. have : (-1 : R) ^ (i : ℕ) = i.cycle_range.sign, { simp [fin.sign_cycle_range] }, rw [fin.coe_succ, pow_succ, this, mul_assoc, mul_assoc, mul_left_comm ↑(equiv.perm.sign _), ← det_permute, matrix.det_apply, finset.mul_sum, finset.mul_sum], -- now we just need to move the corresponding parts to the same place refine finset.sum_congr rfl (λ σ _, _), rw [equiv.perm.decompose_fin.symm_sign, if_neg (fin.succ_ne_zero i)], calc ((-1) * σ.sign : ℤ) • ∏ i', A (equiv.perm.decompose_fin.symm (fin.succ i, σ) i') i' = ((-1) * σ.sign : ℤ) • (A (fin.succ i) 0 * ∏ i', A (((fin.succ i).succ_above) (fin.cycle_range i (σ i'))) i'.succ) : by simp only [fin.prod_univ_succ, fin.succ_above_cycle_range, equiv.perm.decompose_fin_symm_apply_zero, equiv.perm.decompose_fin_symm_apply_succ] ... = (-1) * (A (fin.succ i) 0 * (σ.sign : ℤ) • ∏ i', A (((fin.succ i).succ_above) (fin.cycle_range i (σ i'))) i'.succ) : by simp only [mul_assoc, mul_comm, _root_.neg_mul, one_mul, zsmul_eq_mul, neg_inj, neg_smul, fin.succ_above_cycle_range], end /-- Laplacian expansion of the determinant of an `n+1 × n+1` matrix along row 0. -/ lemma det_succ_row_zero {n : ℕ} (A : matrix (fin n.succ) (fin n.succ) R) : det A = ∑ j : fin n.succ, (-1) ^ (j : ℕ) * A 0 j * det (A.minor fin.succ j.succ_above) := by { rw [← det_transpose A, det_succ_column_zero], refine finset.sum_congr rfl (λ i _, _), rw [← det_transpose], simp only [transpose_apply, transpose_minor, transpose_transpose] } /-- Laplacian expansion of the determinant of an `n+1 × n+1` matrix along row `i`. -/ lemma det_succ_row {n : ℕ} (A : matrix (fin n.succ) (fin n.succ) R) (i : fin n.succ) : det A = ∑ j : fin n.succ, (-1) ^ (i + j : ℕ) * A i j * det (A.minor i.succ_above j.succ_above) := begin simp_rw [pow_add, mul_assoc, ← mul_sum], have : det A = (-1 : R) ^ (i : ℕ) * (i.cycle_range⁻¹).sign * det A, { calc det A = ↑((-1 : ℤˣ) ^ (i : ℕ) * (-1 : ℤˣ) ^ (i : ℕ) : ℤˣ) * det A : by simp ... = (-1 : R) ^ (i : ℕ) * (i.cycle_range⁻¹).sign * det A : by simp [-int.units_mul_self] }, rw [this, mul_assoc], congr, rw [← det_permute, det_succ_row_zero], refine finset.sum_congr rfl (λ j _, _), rw [mul_assoc, matrix.minor, matrix.minor], congr, { rw [equiv.perm.inv_def, fin.cycle_range_symm_zero] }, { ext i' j', rw [equiv.perm.inv_def, fin.cycle_range_symm_succ] }, end /-- Laplacian expansion of the determinant of an `n+1 × n+1` matrix along column `j`. -/ lemma det_succ_column {n : ℕ} (A : matrix (fin n.succ) (fin n.succ) R) (j : fin n.succ) : det A = ∑ i : fin n.succ, (-1) ^ (i + j : ℕ) * A i j * det (A.minor i.succ_above j.succ_above) := by { rw [← det_transpose, det_succ_row _ j], refine finset.sum_congr rfl (λ i _, _), rw [add_comm, ← det_transpose, transpose_apply, transpose_minor, transpose_transpose] } /-- Determinant of 0x0 matrix -/ @[simp] lemma det_fin_zero {A : matrix (fin 0) (fin 0) R} : det A = 1 := det_is_empty /-- Determinant of 1x1 matrix -/ lemma det_fin_one (A : matrix (fin 1) (fin 1) R) : det A = A 0 0 := det_unique A /-- Determinant of 2x2 matrix -/ lemma det_fin_two (A : matrix (fin 2) (fin 2) R) : det A = A 0 0 * A 1 1 - A 0 1 * A 1 0 := begin simp [matrix.det_succ_row_zero, fin.sum_univ_succ], ring end /-- Determinant of 3x3 matrix -/ lemma det_fin_three (A : matrix (fin 3) (fin 3) R) : det A = A 0 0 * A 1 1 * A 2 2 - A 0 0 * A 1 2 * A 2 1 - A 0 1 * A 1 0 * A 2 2 + A 0 1 * A 1 2 * A 2 0 + A 0 2 * A 1 0 * A 2 1 - A 0 2 * A 1 1 * A 2 0 := begin simp [matrix.det_succ_row_zero, fin.sum_univ_succ], ring end end matrix
834d5444b40f619908513260e038837dc2f18a01
28be2ab6091504b6ba250b367205fb94d50ab284
/levels/solutions/world1_addition.lean
15abdc10ad252bce6c19f6ccd7f8e85e1ea7298c
[ "Apache-2.0" ]
permissive
postmasters/natural_number_game
87304ac22e5e1c5ac2382d6e523d6914dd67a92d
38a7adcdfdb18c49c87b37831736c8f15300d821
refs/heads/master
1,649,856,819,031
1,586,444,676,000
1,586,444,676,000
255,006,061
0
0
Apache-2.0
1,586,664,599,000
1,586,664,598,000
null
UTF-8
Lean
false
false
6,435
lean
import mynat.definition -- Imports the natural numbers. /- Here's what you get from the import: 1) The following data: * a type called `mynat` * a term `0 : mynat`, interpreted as the number zero. * a function `succ : mynat → mynat`, with `succ n` interpreted as "the number after n". * Usual numerical notation 0,1,2,3,4,5 etc. 2) The following axioms: * `zero_ne_succ : ∀ (a : mynat), zero ≠ succ(a)`, the statement that zero isn't a successor. -- this ensures that there is more than one natural number. * `succ_inj : ∀ {a b : mynat}, succ(a) = succ(b) → a = b`, the statement that if succ(a) = succ(b) then a = b. -- this ensures that there are infinitely many natural numbers. 3) The principle of mathematical induction. * In practice this means that if you have `n : mynat` then you can use the tactic `induction n`. 4) A few useful extra things: * The theorem `one_eq_succ_zero : 1 = succ 0` * The theorem `ne_iff_implies_false : a ≠ b ↔ (a = b) → false` -/ import mynat.add -- definition of addition /- Here's what you get from the import: 1) The following data: * a function called mynat.add, and notation a + b for this function 2) The following axioms: * `add_zero : ∀ a : mynat, a + 0 = a` * `add_succ : ∀ a b : mynat, a + succ(b) = succ(a + b)` These axiom between them tell you how to work out a + x for every x; use induction on x to reduce to the case either `x = 0` or `x = succ b`, and then use `add_zero` or `add_succ` appropriately. -/ namespace mynat -- Summary: -- Naturals: -- 1) 0 and succ are constants -- 2) succ_inj and zero_ne_succ are axioms -- 3) Induction works. -- Addition: -- 1) add_zero and add_succ are the axioms -- 2) notation is a + b /- Collectibles in this level: add_comm_monoid -- collectible_02 add_monoid [zero_add] -- collectible_01 (has_zero) add_semigroup [add_assoc] (has_add) add_comm_semigroup [add_comm] add_semigroup (see above) -/ /- Instructions: First carefully explain definition of nat and add. Then guide them through the first level. "We're going to prove this by induction on n, which is a natural thing to do because we defined addition by recursion on n (you prove things by induction and define them by recursion). For the base case, we are going to use the axiom that a + 0 = 0. refl closes a goal of the form x = x. how to use add_succ here? etc. Full solution to zero_add: induction n with d hd, rw add_zero, refl, rw add_succ, rw hd, refl, " -/ lemma zero_add (n : mynat) : 0 + n = n := begin [nat_num_game] induction n with d hd, { rw add_zero, refl, }, { rw add_succ, rw hd, refl } end lemma add_assoc (a b c : mynat) : (a + b) + c = a + (b + c) := begin [nat_num_game] induction c with d hd, { -- ⊢ a + b + 0 = a + (b + 0) rw add_zero, rw add_zero, refl }, { -- ⊢ (a + b) + succ d = a + (b + succ d) rw add_succ, rw add_succ, rw add_succ, rw hd, refl, } end -- first point: needs add_assoc, zero_add, add_zero def collectible_01 : add_monoid mynat := by structure_helper --#print axioms collectible_01 -- prove you got this by uncommenting -- proving add_comm immediately is still tricky; trying it -- reveals a natural intermediate lemma which we prove first. lemma succ_add (a b : mynat) : succ a + b = succ (a + b) := begin [nat_num_game] induction b with d hd, { refl }, { rw add_succ, rw hd, rw add_succ, refl } end lemma add_comm (a b : mynat) : a + b = b + a := begin [nat_num_game] induction b with d hd, { -- ⊢ a + 0 = 0 + a, rw zero_add, rw add_zero, refl }, { rw add_succ, rw hd, rw succ_add, refl } end -- level up def collectible_02 : add_comm_monoid mynat := by structure_helper --#print axioms collectible_02 -- no more collectibles beyond this point in this file, however -- stuff below is used in other collectibles in other files. theorem succ_ne_zero : ∀ {{a : mynat}}, succ a ≠ 0 := begin [nat_num_game] intro a, symmetry, exact zero_ne_succ a, end theorem eq_iff_succ_eq_succ (a b : mynat) : succ a = succ b ↔ a = b := begin [nat_num_game] split, { exact succ_inj}, { intro H, rw H, refl, } end theorem succ_eq_add_one (n : mynat) : succ n = n + 1 := begin [nat_num_game] rw one_eq_succ_zero, rw add_succ, rw add_zero, refl, end lemma add_right_comm (a b c : mynat) : a + b + c = a + c + b := begin [nat_num_game] rw add_assoc, rw add_comm b c, rw ←add_assoc, refl, end theorem add_left_cancel ⦃ a b c : mynat⦄ : a + b = a + c → b = c := begin [nat_num_game] intro h, rw add_comm at h, rw add_comm a at h, revert b c, induction a with d hd, { intros b c, intro h, rw add_zero at h, rw add_zero at h, assumption }, { intros b c, intro h, rw add_succ at h, rw add_succ at h, rw ←succ_add at h, rw ←succ_add at h, apply succ_inj, exact hd h } end theorem add_right_cancel ⦃a b c : mynat⦄ : a + b = c + b → a = c := begin [nat_num_game] intro h, rw add_comm at h, rw add_comm c at h, exact add_left_cancel h end theorem add_right_cancel_iff (t a b : mynat) : a + t = b + t ↔ a = b := begin [nat_num_game] split, { apply add_right_cancel}, -- done that way already, { intro H, -- H : a = b, rw H, refl, } end -- this is used for antisymmetry of ≤ lemma eq_zero_of_add_right_eq_self {{a b : mynat}} : a + b = a → b = 0 := begin [nat_num_game] intro h, induction a with a ha, { rw zero_add at h, assumption }, { apply ha, apply succ_inj, rw succ_add at h, assumption, } end -- now used for antisymmetry of ≤ lemma add_left_eq_zero {{a b : mynat}} : a + b = 0 → b = 0 := begin [nat_num_game] intro H, cases b with c, { refl}, { rw add_succ at H, exfalso, apply zero_ne_succ (a + c), rw H, refl, }, end lemma add_right_eq_zero {{a b : mynat}} : a + b = 0 → a = 0 := begin [nat_num_game] intro H, rw add_comm at H, exact add_left_eq_zero H, end theorem add_one_eq_succ (d : mynat) : d + 1 = succ d := begin [nat_num_game] rw succ_eq_add_one, refl, end def ne_succ_self (n : mynat) : n ≠ succ n := begin [nat_num_game] induction n with d hd, apply zero_ne_succ, intro hs, apply hd, apply succ_inj, assumption end end mynat --
bf6495adf81c84a35ea478be95f8fceff171ac85
30b012bb72d640ec30c8fdd4c45fdfa67beb012c
/algebra/field_power.lean
1c2669d2fe9ee2e81e559d53fc7c2b1a9e84cc50
[ "Apache-2.0" ]
permissive
kckennylau/mathlib
21fb810b701b10d6606d9002a4004f7672262e83
47b3477e20ffb5a06588dd3abb01fe0fe3205646
refs/heads/master
1,634,976,409,281
1,542,042,832,000
1,542,319,733,000
109,560,458
0
0
Apache-2.0
1,542,369,208,000
1,509,867,494,000
Lean
UTF-8
Lean
false
false
4,732
lean
/- Copyright (c) 2018 Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Y. Lewis Integer power operation on fields. -/ import algebra.group_power tactic.wlog universe u section field_power open int nat variables {α : Type u} [division_ring α] @[simp] lemma zero_gpow : ∀ z : ℕ, z ≠ 0 → (0 : α)^z = 0 | 0 h := absurd rfl h | (k+1) h := zero_mul _ def fpow (a : α) : ℤ → α | (of_nat n) := a ^ n | -[1+n] := 1/(a ^ (n+1)) lemma unit_pow {a : α} (ha : a ≠ 0) : ∀ n : ℕ, a ^ n = ↑((units.mk0 a ha)^n) | 0 := units.coe_one.symm | (k+1) := by simp only [_root_.pow_succ, units.coe_mul, units.mk0_val]; rw unit_pow lemma fpow_eq_gpow {a : α} (h : a ≠ 0) : ∀ (z : ℤ), fpow a z = ↑(gpow (units.mk0 a h) z) | (of_nat k) := unit_pow _ _ | -[1+k] := by rw [fpow, gpow, one_div_eq_inv, units.inv_eq_inv, unit_pow] lemma fpow_inv (a : α) : fpow a (-1) = a⁻¹ := show 1*(a*1)⁻¹ = a⁻¹, by rw [one_mul, mul_one] lemma fpow_ne_zero_of_ne_zero {a : α} (ha : a ≠ 0) : ∀ (z : ℤ), fpow a z ≠ 0 | (of_nat n) := pow_ne_zero _ ha | -[1+n] := one_div_ne_zero $ pow_ne_zero _ ha @[simp] lemma fpow_zero {a : α} : fpow a 0 = 1 := pow_zero _ lemma fpow_add {a : α} (ha : a ≠ 0) (z1 z2 : ℤ) : fpow a (z1 + z2) = fpow a z1 * fpow a z2 := begin simp only [fpow_eq_gpow ha], rw ← units.coe_mul, congr, apply gpow_add end end field_power section discrete_field_power open int nat variables {α : Type u} [discrete_field α] lemma zero_fpow : ∀ z : ℤ, z ≠ 0 → fpow (0 : α) z = 0 | (of_nat n) h := zero_gpow _ $ by rintro rfl; exact h rfl | -[1+n] h := show 1/(0*0^n)=(0:α), by rw [zero_mul, one_div_zero] lemma fpow_neg (a : α) : ∀ n, fpow a (-n) = 1 / fpow a n | (of_nat 0) := by simp [of_nat_zero] | (of_nat (n+1)) := rfl | -[1+n] := show fpow a (n+1) = 1 / (1 / fpow a (n+1)), by rw one_div_one_div lemma fpow_sub {a : α} (ha : a ≠ 0) (z1 z2 : ℤ) : fpow a (z1 - z2) = fpow a z1 / fpow a z2 := by rw [sub_eq_add_neg, fpow_add ha, fpow_neg, ←div_eq_mul_one_div] end discrete_field_power section ordered_field_power open int variables {α : Type u} [discrete_linear_ordered_field α] lemma fpow_nonneg_of_nonneg {a : α} (ha : a ≥ 0) : ∀ (z : ℤ), fpow a z ≥ 0 | (of_nat n) := pow_nonneg ha _ | -[1+n] := div_nonneg' zero_le_one $ pow_nonneg ha _ lemma fpow_pos_of_pos {a : α} (ha : a > 0) : ∀ (z : ℤ), fpow a z > 0 | (of_nat n) := pow_pos ha _ | -[1+n] := div_pos zero_lt_one $ pow_pos ha _ lemma fpow_le_of_le {x : α} (hx : 1 ≤ x) {a b : ℤ} (h : a ≤ b) : fpow x a ≤ fpow x b := begin induction a with a a; induction b with b b, { simp only [fpow], apply pow_le_pow hx, apply le_of_coe_nat_le_coe_nat h }, { apply absurd h, apply not_le_of_gt, exact lt_of_lt_of_le (neg_succ_lt_zero _) (of_nat_nonneg _) }, { simp only [fpow, one_div_eq_inv], apply le_trans (inv_le_one _); apply one_le_pow_of_one_le hx }, { simp only [fpow], apply (one_div_le_one_div _ _).2, { apply pow_le_pow hx, have : -(↑(a+1) : ℤ) ≤ -(↑(b+1) : ℤ), from h, have h' := le_of_neg_le_neg this, apply le_of_coe_nat_le_coe_nat h' }, repeat { apply pow_pos (lt_of_lt_of_le zero_lt_one hx) } } end lemma pow_le_max_of_min_le {x : α} (hx : x ≥ 1) {a b c : ℤ} (h : min a b ≤ c) : fpow x (-c) ≤ max (fpow x (-a)) (fpow x (-b)) := begin wlog hle : a ≤ b, have hnle : -b ≤ -a, from neg_le_neg hle, have hfle : fpow x (-b) ≤ fpow x (-a), from fpow_le_of_le hx hnle, have : fpow x (-c) ≤ fpow x (-a), { apply fpow_le_of_le hx, simpa only [min_eq_left hle, neg_le_neg_iff] using h }, simpa only [max_eq_left hfle] end lemma fpow_le_one_of_nonpos {p : α} (hp : p ≥ 1) {z : ℤ} (hz : z ≤ 0) : fpow p z ≤ 1 := calc fpow p z ≤ fpow p 0 : fpow_le_of_le hp hz ... = 1 : by simp lemma fpow_ge_one_of_nonneg {p : α} (hp : p ≥ 1) {z : ℤ} (hz : z ≥ 0) : fpow p z ≥ 1 := calc fpow p z ≥ fpow p 0 : fpow_le_of_le hp hz ... = 1 : by simp end ordered_field_power lemma one_lt_pow {α} [linear_ordered_semiring α] {p : α} (hp : p > 1) : ∀ {n : ℕ}, 1 ≤ n → 1 < p ^ n | 1 h := by simp; assumption | (k+2) h := begin rw ←one_mul (1 : α), apply mul_lt_mul, { assumption }, { apply le_of_lt, simpa using one_lt_pow (nat.le_add_left 1 k)}, { apply zero_lt_one }, { apply le_of_lt (lt_trans zero_lt_one hp) } end lemma one_lt_fpow {α} [discrete_linear_ordered_field α] {p : α} (hp : p > 1) : ∀ z : ℤ, z > 0 → 1 < fpow p z | (int.of_nat n) h := one_lt_pow hp (nat.succ_le_of_lt (int.lt_of_coe_nat_lt_coe_nat h))
5c3a97510c608a0a431cadd8ab6bce6f3d634afa
36c7a18fd72e5b57229bd8ba36493daf536a19ce
/library/theories/number_theory/prime_factorization.lean
e3385d71c3b1c0861c2c81d594604f9ed4657c22
[ "Apache-2.0" ]
permissive
YHVHvx/lean
732bf0fb7a298cd7fe0f15d82f8e248c11db49e9
038369533e0136dd395dc252084d3c1853accbf2
refs/heads/master
1,610,701,080,210
1,449,128,595,000
1,449,128,595,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
14,116
lean
/- Copyright (c) 2015 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad Multiplicity and prime factors. We have: mult p n := the greatest power of p dividing n if p > 1 and n > 0, and 0 otherwise. prime_factors n := the finite set of prime factors of n, assuming n > 0 -/ import data.nat data.finset .primes open eq.ops finset well_founded decidable nat.finset open algebra namespace nat -- TODO: this should be proved more generally in ring_bigops theorem Prod_pos {A : Type} [deceqA : decidable_eq A] {s : finset A} {f : A → ℕ} (fpos : ∀ n, n ∈ s → f n > 0) : (∏ n ∈ s, f n) > 0 := begin induction s with a s anins ih, {rewrite Prod_empty; exact zero_lt_one}, rewrite [!Prod_insert_of_not_mem anins], exact (mul_pos (fpos a (mem_insert a _)) (ih (forall_of_forall_insert fpos))) end /- multiplicity -/ theorem mult_rec_decreasing {p n : ℕ} (Hp : p > 1) (Hn : n > 0) : n / p < n := have H' : n < n * p, by rewrite [-mul_one n at {1}]; apply mul_lt_mul_of_pos_left Hp Hn, nat.div_lt_of_lt_mul H' private definition mult.F (p : ℕ) (n : ℕ) (f: Π {m : ℕ}, m < n → ℕ) : ℕ := if H : (p > 1 ∧ n > 0) ∧ p ∣ n then succ (f (mult_rec_decreasing (and.left (and.left H)) (and.right (and.left H)))) else 0 definition mult (p n : ℕ) : ℕ := fix (mult.F p) n theorem mult_rec {p n : ℕ} (pgt1 : p > 1) (ngt0 : n > 0) (pdivn : p ∣ n) : mult p n = succ (mult p (n / p)) := have (p > 1 ∧ n > 0) ∧ p ∣ n, from and.intro (and.intro pgt1 ngt0) pdivn, eq.trans (well_founded.fix_eq (mult.F p) n) (dif_pos this) private theorem mult_base {p n : ℕ} (H : ¬ ((p > 1 ∧ n > 0) ∧ p ∣ n)) : mult p n = 0 := eq.trans (well_founded.fix_eq (mult.F p) n) (dif_neg H) theorem mult_zero_right (p : ℕ) : mult p 0 = 0 := mult_base (assume H, !lt.irrefl (and.right (and.left H))) theorem mult_eq_zero_of_not_dvd {p n : ℕ} (H : ¬ p ∣ n) : mult p n = 0 := mult_base (assume H', H (and.right H')) theorem mult_eq_zero_of_le_one {p : ℕ} (n : ℕ) (H : p ≤ 1) : mult p n = 0 := mult_base (assume H', not_lt_of_ge H (and.left (and.left H'))) theorem mult_zero_left (n : ℕ) : mult 0 n = 0 := mult_eq_zero_of_le_one n !dec_trivial theorem mult_one_left (n : ℕ) : mult 1 n = 0 := mult_eq_zero_of_le_one n !dec_trivial theorem mult_pos_of_dvd {p n : ℕ} (pgt1 : p > 1) (npos : n > 0) (pdvdn : p ∣ n) : mult p n > 0 := by rewrite (mult_rec pgt1 npos pdvdn); apply succ_pos theorem not_dvd_of_mult_eq_zero {p n : ℕ} (pgt1 : p > 1) (npos : n > 0) (H : mult p n = 0) : ¬ p ∣ n := suppose p ∣ n, ne_of_gt (mult_pos_of_dvd pgt1 npos this) H theorem dvd_of_mult_pos {p n : ℕ} (H : mult p n > 0) : p ∣ n := by_contradiction (suppose ¬ p ∣ n, ne_of_gt H (mult_eq_zero_of_not_dvd this)) /- properties of mult -/ theorem mult_eq_zero_of_prime_of_ne {p q : ℕ} (primep : prime p) (primeq : prime q) (pneq : p ≠ q) : mult p q = 0 := mult_eq_zero_of_not_dvd (not_dvd_of_prime_of_coprime primep (coprime_primes primep primeq pneq)) theorem pow_mult_dvd (p n : ℕ) : p^(mult p n) ∣ n := begin induction n using nat.strong_induction_on with [n, ih], cases eq_zero_or_pos n with [nz, npos], {rewrite nz, apply dvd_zero}, cases le_or_gt p 1 with [ple1, pgt1], {rewrite [!mult_eq_zero_of_le_one ple1, pow_zero], apply one_dvd}, cases (or.swap (em (p ∣ n))) with [pndvdn, pdvdn], {rewrite [mult_eq_zero_of_not_dvd pndvdn, pow_zero], apply one_dvd}, show p ^ (mult p n) ∣ n, from dvd.elim pdvdn (take n', suppose n = p * n', have p > 0, from lt.trans zero_lt_one pgt1, assert n / p = n', from !nat.div_eq_of_eq_mul_right this `n = p * n'`, assert n' < n, by rewrite -this; apply mult_rec_decreasing pgt1 npos, begin rewrite [mult_rec pgt1 npos pdvdn, `n / p = n'`, pow_succ], subst n, apply mul_dvd_mul !dvd.refl, apply ih _ this end) end theorem mult_one_right (p : ℕ) : mult p 1 = 0:= assert H : p^(mult p 1) = 1, from eq_one_of_dvd_one !pow_mult_dvd, or.elim (le_or_gt p 1) (suppose p ≤ 1, by rewrite [!mult_eq_zero_of_le_one this]) (suppose p > 1, by_contradiction (suppose mult p 1 ≠ 0, have mult p 1 > 0, from pos_of_ne_zero this, assert p^(mult p 1) > 1, from pow_gt_one `p > 1` this, show false, by rewrite H at this; apply !lt.irrefl this)) private theorem mult_pow_mul {p n : ℕ} (i : ℕ) (pgt1 : p > 1) (npos : n > 0) : mult p (p^i * n) = i + mult p n := begin induction i with [i, ih], {krewrite [pow_zero, one_mul, zero_add]}, have p > 0, from lt.trans zero_lt_one pgt1, have psin_pos : p^(succ i) * n > 0, from mul_pos (!pow_pos_of_pos this) npos, have p ∣ p^(succ i) * n, by rewrite [pow_succ, mul.assoc]; apply dvd_mul_right, rewrite [mult_rec pgt1 psin_pos this, pow_succ', mul.right_comm, !nat.mul_div_cancel `p > 0`, ih], rewrite [add.comm i, add.comm (succ i)] end theorem mult_pow_self {p : ℕ} (i : ℕ) (pgt1 : p > 1) : mult p (p^i) = i := by rewrite [-(mul_one (p^i)), mult_pow_mul i pgt1 zero_lt_one, mult_one_right] theorem mult_self {p : ℕ} (pgt1 : p > 1) : mult p p = 1 := by rewrite [-pow_one p at {2}]; apply mult_pow_self 1 pgt1 theorem le_mult {p i n : ℕ} (pgt1 : p > 1) (npos : n > 0) (pidvd : p^i ∣ n) : i ≤ mult p n := dvd.elim pidvd (take m, suppose n = p^i * m, assert m > 0, from pos_of_mul_pos_left (this ▸ npos), by subst n; rewrite [mult_pow_mul i pgt1 this]; apply le_add_right) theorem not_dvd_div_pow_mult {p n : ℕ} (pgt1 : p > 1) (npos : n > 0) : ¬ p ∣ n / p^(mult p n) := assume pdvd : p ∣ n / p^(mult p n), obtain m (H : n / p^(mult p n) = p * m), from exists_eq_mul_right_of_dvd pdvd, assert n = p^(succ (mult p n)) * m, from calc n = p^mult p n * (n / p^mult p n) : by rewrite (nat.mul_div_cancel' !pow_mult_dvd) ... = p^(succ (mult p n)) * m : by rewrite [H, pow_succ', mul.assoc], have p^(succ (mult p n)) ∣ n, by rewrite this at {2}; apply dvd_mul_right, have succ (mult p n) ≤ mult p n, from le_mult pgt1 npos this, show false, from !not_succ_le_self this theorem mult_mul {p m n : ℕ} (primep : prime p) (mpos : m > 0) (npos : n > 0) : mult p (m * n) = mult p m + mult p n := let m' := m / p^mult p m, n' := n / p^mult p n in assert p > 1, from gt_one_of_prime primep, assert meq : m = p^mult p m * m', by rewrite (nat.mul_div_cancel' !pow_mult_dvd), assert neq : n = p^mult p n * n', by rewrite (nat.mul_div_cancel' !pow_mult_dvd), have m'pos : m' > 0, from pos_of_mul_pos_left (meq ▸ mpos), have n'pos : n' > 0, from pos_of_mul_pos_left (neq ▸ npos), have npdvdm' : ¬ p ∣ m', from !not_dvd_div_pow_mult `p > 1` mpos, have npdvdn' : ¬ p ∣ n', from !not_dvd_div_pow_mult `p > 1` npos, assert npdvdm'n' : ¬ p ∣ m' * n', from not_dvd_mul_of_prime primep npdvdm' npdvdn', assert m'n'pos : m' * n' > 0, from mul_pos m'pos n'pos, assert multm'n' : mult p (m' * n') = 0, from mult_eq_zero_of_not_dvd npdvdm'n', calc mult p (m * n) = mult p (p^(mult p m + mult p n) * (m' * n')) : by rewrite [pow_add, mul.right_comm, -mul.assoc, -meq, mul.assoc, mul.comm (n / _), -neq] ... = mult p m + mult p n : by rewrite [!mult_pow_mul `p > 1` m'n'pos, multm'n'] theorem mult_pow {p m : ℕ} (n : ℕ) (mpos : m > 0) (primep : prime p) : mult p (m^n) = n * mult p m := begin induction n with n ih, krewrite [pow_zero, mult_one_right, zero_mul], rewrite [pow_succ, mult_mul primep mpos (!pow_pos_of_pos mpos), ih, succ_mul, add.comm] end theorem dvd_of_forall_prime_mult_le {m n : ℕ} (mpos : m > 0) (H : ∀ {p}, prime p → mult p m ≤ mult p n) : m ∣ n := begin revert H, revert n, induction m using nat.strong_induction_on with [m, ih], cases (decidable.em (m = 1)) with [meq, mneq], {intros, rewrite meq, apply one_dvd}, have mgt1 : m > 1, from lt_of_le_of_ne (succ_le_of_lt mpos) (ne.symm mneq), have mge2 : m ≥ 2, from succ_le_of_lt mgt1, have hpd : ∃ p, prime p ∧ p ∣ m, from exists_prime_and_dvd mge2, cases hpd with [p, H1], cases H1 with [primep, pdvdm], intro n, cases (eq_zero_or_pos n) with [nz, npos], {intros; rewrite nz; apply dvd_zero}, assume H : ∀ {p : ℕ}, prime p → mult p m ≤ mult p n, obtain m' (meq : m = p * m'), from exists_eq_mul_right_of_dvd pdvdm, assert pgt1 : p > 1, from gt_one_of_prime primep, assert m'pos : m' > 0, from pos_of_ne_zero (assume m'z, by revert mpos; rewrite [meq, m'z, mul_zero]; apply not_lt_zero), have m'ltm : m' < m, by rewrite [meq, -one_mul m' at {1}]; apply mul_lt_mul_of_lt_of_le m'pos pgt1 !le.refl, have multpm : mult p m ≥ 1, from le_mult pgt1 mpos (by rewrite pow_one; apply pdvdm), have multpn : mult p n ≥ 1, from le.trans multpm (H primep), obtain n' (neq : n = p * n'), from exists_eq_mul_right_of_dvd (dvd_of_mult_pos (lt_of_succ_le multpn)), assert n'pos : n' > 0, from pos_of_ne_zero (assume n'z, by revert npos; rewrite [neq, n'z, mul_zero]; apply not_lt_zero), have ∀q, prime q → mult q m' ≤ mult q n', from (take q, assume primeq : prime q, have multqm : mult q m = mult q p + mult q m', by rewrite [meq, mult_mul primeq (pos_of_prime primep) m'pos], have multqn : mult q n = mult q p + mult q n', by rewrite [neq, mult_mul primeq (pos_of_prime primep) n'pos], show mult q m' ≤ mult q n', from le_of_add_le_add_left (multqm ▸ multqn ▸ H primeq)), assert m'dvdn' : m' ∣ n', from ih m' m'ltm m'pos n' this, show m ∣ n, by rewrite [meq, neq]; apply mul_dvd_mul !dvd.refl m'dvdn' end theorem eq_of_forall_prime_mult_eq {m n : ℕ} (mpos : m > 0) (npos : n > 0) (H : ∀ p, prime p → mult p m = mult p n) : m = n := dvd.antisymm (dvd_of_forall_prime_mult_le mpos (take p, assume primep, H _ primep ▸ !le.refl)) (dvd_of_forall_prime_mult_le npos (take p, assume primep, H _ primep ▸ !le.refl)) /- prime factors -/ definition prime_factors (n : ℕ) : finset ℕ := { p ∈ upto (succ n) | prime p ∧ p ∣ n } theorem prime_of_mem_prime_factors {p n : ℕ} (H : p ∈ prime_factors n) : prime p := and.left (of_mem_sep H) theorem dvd_of_mem_prime_factors {p n : ℕ} (H : p ∈ prime_factors n) : p ∣ n := and.right (of_mem_sep H) theorem mem_prime_factors {p n : ℕ} (npos : n > 0) (primep : prime p) (pdvdn : p ∣ n) : p ∈ prime_factors n := have plen : p ≤ n, from le_of_dvd npos pdvdn, mem_sep_of_mem (mem_upto_of_lt (lt_succ_of_le plen)) (and.intro primep pdvdn) /- prime factorization -/ theorem mult_pow_eq_zero_of_prime_of_ne {p q : ℕ} (primep : prime p) (primeq : prime q) (pneq : p ≠ q) (i : ℕ) : mult p (q^i) = 0 := begin induction i with i ih, {rewrite [pow_zero, mult_one_right]}, have qpos : q > 0, from pos_of_prime primeq, have qipos : q^i > 0, from !pow_pos_of_pos qpos, rewrite [pow_succ', mult_mul primep qipos qpos, ih, mult_eq_zero_of_prime_of_ne primep primeq pneq] end theorem mult_prod_pow_of_not_mem {p : ℕ} (primep : prime p) {s : finset ℕ} (sprimes : ∀ p, p ∈ s → prime p) (f : ℕ → ℕ) (pns : p ∉ s) : mult p (∏ q ∈ s, q^(f q)) = 0 := begin induction s with a s anins ih, {rewrite [Prod_empty, mult_one_right]}, have pnea : p ≠ a, from assume peqa, by rewrite peqa at pns; exact pns !mem_insert, have primea : prime a, from sprimes a !mem_insert, have afapos : a ^ f a > 0, from !pow_pos_of_pos (pos_of_prime primea), have prodpos : (∏ q ∈ s, q ^ f q) > 0, from Prod_pos (take q, assume qs, !pow_pos_of_pos (pos_of_prime (forall_of_forall_insert sprimes q qs))), rewrite [!Prod_insert_of_not_mem anins, mult_mul primep afapos prodpos], rewrite (mult_pow_eq_zero_of_prime_of_ne primep primea pnea), rewrite (ih (forall_of_forall_insert sprimes) (λ H, pns (!mem_insert_of_mem H))) end theorem mult_prod_pow_of_mem {p : ℕ} (primep : prime p) {s : finset ℕ} (sprimes : ∀ p, p ∈ s → prime p) (f : ℕ → ℕ) (ps : p ∈ s) : mult p (∏ q ∈ s, q^(f q)) = f p := begin induction s with a s anins ih, {exact absurd ps !not_mem_empty}, have primea : prime a, from sprimes a !mem_insert, have afapos : a ^ f a > 0, from !pow_pos_of_pos (pos_of_prime primea), have prodpos : (∏ q ∈ s, q ^ f q) > 0, from Prod_pos (take q, assume qs, !pow_pos_of_pos (pos_of_prime (forall_of_forall_insert sprimes q qs))), rewrite [!Prod_insert_of_not_mem anins, mult_mul primep afapos prodpos], cases eq_or_mem_of_mem_insert ps with peqa pins, {rewrite [peqa, !mult_pow_self (gt_one_of_prime primea)], rewrite [mult_prod_pow_of_not_mem primea (forall_of_forall_insert sprimes) _ anins]}, have pnea : p ≠ a, from by intro peqa; rewrite peqa at pins; exact anins pins, rewrite [mult_pow_eq_zero_of_prime_of_ne primep primea pnea, zero_add], exact (ih (forall_of_forall_insert sprimes) pins) end theorem eq_prime_factorization {n : ℕ} (npos : n > 0) : n = (∏ p ∈ prime_factors n, p^(mult p n)) := let nprod := ∏ p ∈ prime_factors n, p^(mult p n) in assert primefactors : ∀ p, p ∈ prime_factors n → prime p, from take p, @prime_of_mem_prime_factors p n, have prodpos : (∏ q ∈ prime_factors n, q^(mult q n)) > 0, from Prod_pos (take q, assume qpf, !pow_pos_of_pos (pos_of_prime (prime_of_mem_prime_factors qpf))), eq_of_forall_prime_mult_eq npos prodpos (take p, assume primep, decidable.by_cases (assume pprimefactors : p ∈ prime_factors n, eq.symm (mult_prod_pow_of_mem primep primefactors (λ p, mult p n) pprimefactors)) (assume pnprimefactors : p ∉ prime_factors n, have ¬ p ∣ n, from assume H, pnprimefactors (mem_prime_factors npos primep H), assert mult p n = 0, from mult_eq_zero_of_not_dvd this, by rewrite [this, mult_prod_pow_of_not_mem primep primefactors _ pnprimefactors])) end nat
ad81af7cd11e39362e76de9793f98f4770e6dedf
f2fbd9ce3f46053c664b74a5294d7d2f584e72d3
/src/valuations.lean
733429c3ae1bbec5d2f965ce44647a7a81748881
[ "Apache-2.0" ]
permissive
jcommelin/lean-perfectoid-spaces
c656ae26a2338ee7a0072dab63baf577f079ca12
d5ed816bcc116fd4cde5ce9aaf03905d00ee391c
refs/heads/master
1,584,610,432,107
1,538,491,594,000
1,538,491,594,000
136,299,168
0
0
null
1,528,274,452,000
1,528,274,452,000
null
UTF-8
Lean
false
false
15,295
lean
import algebra.group_power import set_theory.cardinal import ring_theory.ideals import data.finsupp import group_theory.quotient_group import tactic.tidy import for_mathlib.ideals import for_mathlib.linear_ordered_comm_group universes u₁ u₂ u₃ -- v is used for valuations namespace valuation class is_valuation {R : Type u₁} [comm_ring R] {Γ : Type u₂} [linear_ordered_comm_group Γ] (v : R → with_zero Γ) : Prop := (map_zero : v 0 = 0) (map_one : v 1 = 1) (map_mul : ∀ x y, v (x * y) = v x * v y) (map_add : ∀ x y, v (x + y) ≤ v x ∨ v (x + y) ≤ v y) end valuation def valuation (R : Type u₁) [comm_ring R] (Γ : Type u₂) [linear_ordered_comm_group Γ] := { v : R → with_zero Γ // valuation.is_valuation v } namespace valuation instance (R : Type u₁) [comm_ring R] (Γ : Type u₂) [linear_ordered_comm_group Γ] : has_coe_to_fun (valuation R Γ) := { F := λ _, R → with_zero Γ, coe := subtype.val} variables {R : Type u₁} [comm_ring R] variables {Γ : Type u₂} [linear_ordered_comm_group Γ] variables (v : valuation R Γ) {x y z : R} instance : is_valuation v := v.property @[simp] lemma map_zero : v 0 = 0 := v.property.map_zero @[simp] lemma map_one : v 1 = 1 := v.property.map_one @[simp] lemma map_mul : ∀ x y, v (x * y) = v x * v y := v.property.map_mul @[simp] lemma map_add : ∀ x y, v (x + y) ≤ v x ∨ v (x + y) ≤ v y := v.property.map_add theorem map_unit (h : x * y = 1) : option.is_some (v x) := begin have h1 := v.map_mul x y, rw [h, map_one v] at h1, cases (v x), { exfalso, exact option.no_confusion h1 }, { constructor } end theorem map_neg_one : v (-1) = 1 := begin have h1 : (-1 : R) * (-1) = 1 := by simp, have h2 := map_unit v h1, have h3 := map_mul v (-1) (-1), rw [option.is_some_iff_exists] at h2, cases h2 with x h, change v (-1) = some x at h, show v (-1) = 1, rw h at h3 ⊢, congr, rw [h1, map_one v] at h3, replace h3 := eq.symm (option.some.inj h3), have h4 : x^2 = 1 := by simpa [pow_two] using h3, exact linear_ordered_comm_group.eq_one_of_pow_eq_one h4 end @[simp] theorem eq_zero_iff_le_zero {r : R} : v r = 0 ↔ v r ≤ v 0 := by split; intro h; simpa using h section variables {Γ₁ : Type u₂} [linear_ordered_comm_group Γ₁] variables {Γ₂ : Type u₃} [linear_ordered_comm_group Γ₂] variables {v₁ : R → with_zero Γ₁} {v₂ : R → with_zero Γ₂} variables {ψ : Γ₁ → Γ₂} variables (H12 : ∀ r, with_zero.map ψ (v₁ r) = v₂ r) variables (Hle : ∀ g h : Γ₁, g ≤ h ↔ ψ g ≤ ψ h) include H12 Hle theorem le_of_le (r s : R) : v₁ r ≤ v₁ s ↔ v₂ r ≤ v₂ s := begin rw ←H12 r, rw ←H12 s, cases hr : (v₁ r) with g; cases hs : (v₁ s) with h; try {simp}, { intro oops, exact option.no_confusion oops }, { exact Hle g h } end theorem valuation_of_valuation [is_group_hom ψ] (Hiψ : function.injective ψ) (H : is_valuation v₂) : is_valuation v₁ := { map_zero := begin show v₁ 0 = 0, have H0 : v₂ 0 = 0 := H.map_zero, rw ←H12 0 at H0, change with_zero.map ψ (v₁ 0) = 0 at H0, cases h : v₁ 0, refl, exfalso, rw h at H0, revert H0, exact dec_trivial end, map_one := begin show v₁ 1 = 1, have H0 : v₂ 1 = 1 := H.map_one, rw ←H12 1 at H0, cases h : v₁ 1, { change v₁ 1 = 0 at h, rw h at H0, simpa only [with_zero.map_zero] using H0 }, { rw h at H0, change some (ψ val) = some 1 at H0, congr, apply Hiψ, rw [option.some_inj.1 H0, is_group_hom.one ψ] } end, map_mul := begin intros r s, apply with_zero.map_inj Hiψ, rw [H12 (r * s), H.map_mul, ←H12 r, ←H12 s], symmetry, exact with_zero.map_mul _ _ _, end, map_add := begin intros r s, cases is_valuation.map_add v₂ r s, { left, rw [←H12 r, ←H12 (r+s)] at h, rwa with_zero.map_le Hle }, { right, rw [←H12 s, ←H12 (r+s)] at h, rwa with_zero.map_le Hle } end } end def map {S : Type u₃} [comm_ring S] (f : S → R) [is_ring_hom f] : valuation S Γ := { val := v ∘ f, property := { map_zero := by simp [is_ring_hom.map_zero f], map_one := by simp [is_ring_hom.map_one f], map_mul := by simp [is_ring_hom.map_mul f], map_add := by simp [is_ring_hom.map_add f] } } section trivial variables (S : set R) [is_prime_ideal S] [decidable_pred S] def trivial : valuation R Γ := { val := λ x, if x ∈ S then 0 else 1, property := { map_zero := if_pos (is_submodule.zero_ R S), map_one := if_neg is_proper_ideal.one_not_mem, map_mul := λ x y, begin split_ifs with hxy hx hy; try {simp}; exfalso, { cases is_prime_ideal.mem_or_mem_of_mul_mem hxy with h' h', { exact hx h' }, { exact h h' } }, { have H : x * y ∈ S, exact is_ideal.mul_right h, exact hxy H }, { have H : x * y ∈ S, exact is_ideal.mul_right h, exact hxy H }, { have H : x * y ∈ S, exact is_ideal.mul_left h_1, exact hxy H } end, map_add := λ x y, begin split_ifs with hxy hx hy; try {simp}; try {left; exact le_refl _}; try {right}; try {exact le_refl _}, { have hxy' : x + y ∈ S := is_submodule.add_ R h h_1, exfalso, exact hxy hxy' } end } } @[simp] lemma trivial_val : (trivial S).val = (λ x, if x ∈ S then 0 else 1 : R → (with_zero Γ)) := rfl end trivial section supp open with_zero def supp : set R := {x | v x = 0} instance : is_prime_ideal (supp v) := { zero_ := map_zero v, add_ := λ x y hx hy, or.cases_on (map_add v x y) (λ hxy, le_antisymm (hx ▸ hxy) zero_le) (λ hxy, le_antisymm (hy ▸ hxy) zero_le), smul := λ c x hx, calc v (c * x) = v c * v x : map_mul v c x ... = v c * 0 : congr_arg _ hx ... = 0 : mul_zero _, ne_univ := λ h, have h1 : (1:R) ∈ supp v, by rw h; trivial, have h2 : v 1 = 0 := h1, by rw [map_one v] at h2; exact option.no_confusion h2, mem_or_mem_of_mul_mem := λ x y hxy, begin dsimp [supp] at hxy ⊢, change v (x * y) = 0 at hxy, rw [map_mul v x y] at hxy, exact eq_zero_or_eq_zero_of_mul_eq_zero _ _ hxy end } /- definition extension_to_integral_domain {α : Type} [linear_ordered_comm_group α] {R : Type} [comm_ring R] (f : R → with_zero α) [H : is_valuation f] : (comm_ring.quotient R (supp f)) → with_zero α := sorry -/ end supp variable (v) definition value_group := group.closure {a : Γ | ∃ r : R, v r = some a} definition value_group_v (v : R → with_zero Γ) [is_valuation v] := group.closure ({a : Γ | ∃ r : R, v r = some a}) instance : group (value_group v) := @subtype.group _ _ (value_group v) (group.closure.is_subgroup {a : Γ | ∃ r : R, v r = some a}) instance valutaion.group_v (v : R → with_zero Γ) [is_valuation v] : group (value_group_v v) := @subtype.group _ _ (value_group_v v) (group.closure.is_subgroup {a : Γ | ∃ r : R, v r = some a}) end valuation namespace valuation open quotient_group variables {R : Type u₁} [comm_ring R] [decidable_eq R] structure minimal_valuation.parametrized_subgroup (Γ₂ : Type u₂) [linear_ordered_comm_group Γ₂] := (Γ : Type u₁) [grp : comm_group Γ] (inc : Γ → Γ₂) [hom : is_group_hom inc] (inj : function.injective inc) local attribute [instance] parametrized_subgroup.grp local attribute [instance] parametrized_subgroup.hom variables {Γ₂ : Type u₂} [linear_ordered_comm_group Γ₂] variables (v₂ : valuation R Γ₂) set_option class.instance_max_depth 41 include R v₂ def minimal_value_group : minimal_valuation.parametrized_subgroup Γ₂ := begin let FG : Type u₁ := multiplicative (R →₀ ℤ), -- free ab group on R let φ₀ : R → Γ₂ := λ r, option.get_or_else (v₂ r) 1, let φ : FG → Γ₂ := λ f, finsupp.prod f (λ r n,(φ₀ r) ^ n), haveI : is_group_hom φ := ⟨λ a b, finsupp.prod_add_index (λ a, rfl) (λ a b₁ b₂, gpow_add (φ₀ a) b₁ b₂)⟩, exact { Γ := quotient (is_group_hom.ker φ), inc := lift (is_group_hom.ker φ) φ (λ _,(is_group_hom.mem_ker φ).1), hom := quotient_group.is_group_hom_quotient_lift _ _ _, inj := injective_ker_lift φ } end namespace minimal_value_group def mk (r : R) : (minimal_value_group v₂).Γ := begin let FG : Type u₁ := multiplicative (R →₀ ℤ), -- free ab group on R let φ₀ : R → Γ₂ := λ r, option.get_or_else (v₂ r) 1, let φ : FG → Γ₂ := λ f, finsupp.prod f (λ r n,(φ₀ r) ^ n), haveI : is_group_hom φ := ⟨λ a b, finsupp.prod_add_index (λ a, rfl) (λ a b₁ b₂, gpow_add (φ₀ a) b₁ b₂)⟩, exact quotient_group.mk (finsupp.single r (1 : ℤ)) end lemma mk_some {r : R} {g : Γ₂} (h : v₂ r = some g) : v₂ r = some ((minimal_value_group v₂).inc (mk v₂ r)) := begin rw h, congr' 1, dsimp[minimal_value_group, minimal_value_group.mk], rw finsupp.prod_single_index ; finish end instance : linear_ordered_comm_group (minimal_value_group v₂).Γ := begin cases minimal_value_group v₂ with Γ₁ _ ψ _ inj, letI Γ₁linord : linear_order Γ₁ := { le := λ g h, ψ g ≤ ψ h, le_refl := λ _, le_refl _, le_trans := λ _ _ _ hab hbc, le_trans hab hbc, le_antisymm := λ g h Hgh Hhg, inj $ le_antisymm Hgh Hhg, le_total := λ g h, le_total _ _ }, exact ⟨λ a b H c, begin change ψ (c * a) ≤ ψ (c * b), rw [is_group_hom.mul ψ c b, is_group_hom.mul ψ c a], exact linear_ordered_comm_group.mul_le_mul_left H _, end⟩ end end minimal_value_group definition minimal_valuation.val (r : R) : with_zero ((minimal_value_group v₂).Γ) := match v₂ r with | some _ := some (minimal_value_group.mk v₂ r) | 0 := 0 end namespace minimal_valuation @[simp] lemma zero {r} (h : v₂ r = 0) : val v₂ r = 0 := by simp [val, h] lemma some {r} {g} (h : v₂ r = some g) : val v₂ r = some (minimal_value_group.mk v₂ r) := by simp [val, h] lemma map (r : R) : with_zero.map (minimal_value_group v₂).inc (val v₂ r) = v₂ r := begin destruct (v₂ r), { intro h, change v₂ r = 0 at h, simp [zero v₂ h, h], }, { intros g h, rw [minimal_value_group.mk_some v₂ h, some v₂ h, with_zero.map_some] }, end end minimal_valuation def minimal_valuation : valuation R (minimal_value_group v₂).Γ := { val := minimal_valuation.val v₂, property := let Γ₁ := minimal_value_group v₂ in valuation_of_valuation (minimal_valuation.map v₂) (λ g h, iff.refl _) Γ₁.inj (v₂.property) } def is_equiv {R : Type u₁} [comm_ring R] {Γ₁ : Type u₂} [linear_ordered_comm_group Γ₁] {Γ₂ : Type u₃} [linear_ordered_comm_group Γ₂] (v₁ : valuation R Γ₁) (v₂ : valuation R Γ₂) : Prop := ∀ r s, v₁ r ≤ v₁ s ↔ v₂ r ≤ v₂ s lemma minimal_valuation_equiv : is_equiv (v₂.minimal_valuation : valuation R (minimal_value_group v₂).Γ) v₂ := le_of_le (minimal_valuation.map v₂) (λ g h, iff.refl _) end valuation /- quotes from mathlib (mostly Mario) (all 2018) Jul03 class is_valuation {α : Type} [linear_ordered_comm_group α] {R : Type} [comm_ring R] (f : R → option α) : Prop := (map_zero : f 0 = 0) (map_one : f 1 = 1) (map_mul : ∀ x y, f (x * y) = f x * f y) (map_add : ∀ x y, f (x + y) ≤ f x ∨ f (x + y) ≤ f y) namespace is_valuation ... structure valuation (R : Type) [comm_ring R] (α : Type) [Hα : linear_ordered_comm_group α] := (f : R → option α) (Hf : is_valuation f) ... **All Jul03** MC: What's wrong, again, with defining Spv as the collection of all valuation relations? KB: All proofs need an actual valuation MC: You can define your own version of quot.lift and quot.mk that take valuations MC: valuation functions that is [quot.lift is the statement that if I have a function on valuations which is constant on equiv classes then I can produce a function on Spv] MC: You only use the relations as inhabitants of the type so that the universe isn't pushed up, but all the work uses functions MC: You will need to prove the computation rule, so it won't be definitional, but otherwise it should work smoothly if your API is solid MC: No equivalence class needed either MC: quot.mk takes a valuation function and produces an element of Spv MC: quot.lift takes a function defined on valuation functions and produces a function defined on Spv KB: So what about proofs which go "Spv(R) is compact. Proof: take an element of Spv(R), call it v or f or whatever, and now manipulate f in the following way..." MC: That's quot.lift MC: Actually you will want quot.ind as well ["any subset of the quotient type containing the image of quot.mk is everything"] or equivalently quot.exists_rep [lemma exists_rep {α : Sort u} {r : α → α → Prop} (q : quot r) : ∃ a : α, (quot.mk r a) = q := ] MC: that is, for every element of Spv there is a valuation function that quot.mk's to it MC: Note it's not actually a function producing valuation functions, it's an exists MC: if you prove analogues of those theorems for your type, then you have constructed the quotient up to isomorphism MC: This all has a category theoretic interpretation as a coequalizer, and all constructions are natural in that category MC: As opposed to, say, quot.out, which picks an element from an equivalence class MC: Although in your case if I understand correctly you also have a canonical way to define quot.out satisfying some other universal property to do with the ordered group where the valuation and ring have to share the same universe. You can prove that the universe need not be the same as part of the universal properties i.e. Spv.mk takes as input a valuation function (v : valuation R A) where {R : Type u} and {A : Type v} (so it isn't just instantiating the exists) KB: "If you want to be polymorphic" -- I just want to do maths. I have no idea if I want to be polymorphic. If I just want to define a perfectoid space, do I want to be polymorphic? MC : In lean, you should usually be polymorphic at least in contravariant positions (i.e. the inputs should be maximally polymorphic, the output should be minimally polymorphic) This is why we don't have nat : Type u The general rule is to keep types out of classes if at all possible. Lean behaves better when the types are given as "alpha" rather than "the type inside v", particularly if you start manipulating the functions (adding them, say). It is the same things that make the difference between bundled vs unbundled groups. When working "internally", i.e. calculations using the monoid structure, it is better for the type to be exposed as a variable -/
ffc39ce4ad16367611d1775e2b7a62b428260769
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/linear_algebra/affine_space/combination.lean
910a7bc3a92ee58cf85dac2f2926e766846fad1f
[ "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
42,541
lean
/- Copyright (c) 2020 Joseph Myers. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Joseph Myers -/ import algebra.invertible import algebra.indicator_function import linear_algebra.affine_space.affine_map import linear_algebra.affine_space.affine_subspace import linear_algebra.finsupp import tactic.fin_cases /-! # Affine combinations of points This file defines affine combinations of points. ## Main definitions * `weighted_vsub_of_point` is a general weighted combination of subtractions with an explicit base point, yielding a vector. * `weighted_vsub` uses an arbitrary choice of base point and is intended to be used when the sum of weights is 0, in which case the result is independent of the choice of base point. * `affine_combination` adds the weighted combination to the arbitrary base point, yielding a point rather than a vector, and is intended to be used when the sum of weights is 1, in which case the result is independent of the choice of base point. These definitions are for sums over a `finset`; versions for a `fintype` may be obtained using `finset.univ`, while versions for a `finsupp` may be obtained using `finsupp.support`. ## References * https://en.wikipedia.org/wiki/Affine_space -/ noncomputable theory open_locale big_operators classical affine namespace finset lemma univ_fin2 : (univ : finset (fin 2)) = {0, 1} := by { ext x, fin_cases x; simp } variables {k : Type*} {V : Type*} {P : Type*} [ring k] [add_comm_group V] [module k V] variables [S : affine_space V P] include S variables {ι : Type*} (s : finset ι) variables {ι₂ : Type*} (s₂ : finset ι₂) /-- A weighted sum of the results of subtracting a base point from the given points, as a linear map on the weights. The main cases of interest are where the sum of the weights is 0, in which case the sum is independent of the choice of base point, and where the sum of the weights is 1, in which case the sum added to the base point is independent of the choice of base point. -/ def weighted_vsub_of_point (p : ι → P) (b : P) : (ι → k) →ₗ[k] V := ∑ i in s, (linear_map.proj i : (ι → k) →ₗ[k] k).smul_right (p i -ᵥ b) @[simp] lemma weighted_vsub_of_point_apply (w : ι → k) (p : ι → P) (b : P) : s.weighted_vsub_of_point p b w = ∑ i in s, w i • (p i -ᵥ b) := by simp [weighted_vsub_of_point, linear_map.sum_apply] /-- The value of `weighted_vsub_of_point`, where the given points are equal. -/ @[simp] lemma weighted_vsub_of_point_apply_const (w : ι → k) (p : P) (b : P) : s.weighted_vsub_of_point (λ _, p) b w = (∑ i in s, w i) • (p -ᵥ b) := by rw [weighted_vsub_of_point_apply, sum_smul] /-- Given a family of points, if we use a member of the family as a base point, the `weighted_vsub_of_point` does not depend on the value of the weights at this point. -/ lemma weighted_vsub_of_point_eq_of_weights_eq (p : ι → P) (j : ι) (w₁ w₂ : ι → k) (hw : ∀ i, i ≠ j → w₁ i = w₂ i) : s.weighted_vsub_of_point p (p j) w₁ = s.weighted_vsub_of_point p (p j) w₂ := begin simp only [finset.weighted_vsub_of_point_apply], congr, ext i, cases eq_or_ne i j with h h, { simp [h], }, { simp [hw i h], }, end /-- The weighted sum is independent of the base point when the sum of the weights is 0. -/ lemma weighted_vsub_of_point_eq_of_sum_eq_zero (w : ι → k) (p : ι → P) (h : ∑ i in s, w i = 0) (b₁ b₂ : P) : s.weighted_vsub_of_point p b₁ w = s.weighted_vsub_of_point p b₂ w := begin apply eq_of_sub_eq_zero, rw [weighted_vsub_of_point_apply, weighted_vsub_of_point_apply, ←sum_sub_distrib], conv_lhs { congr, skip, funext, rw [←smul_sub, vsub_sub_vsub_cancel_left] }, rw [←sum_smul, h, zero_smul] end /-- The weighted sum, added to the base point, is independent of the base point when the sum of the weights is 1. -/ lemma weighted_vsub_of_point_vadd_eq_of_sum_eq_one (w : ι → k) (p : ι → P) (h : ∑ i in s, w i = 1) (b₁ b₂ : P) : s.weighted_vsub_of_point p b₁ w +ᵥ b₁ = s.weighted_vsub_of_point p b₂ w +ᵥ b₂ := begin erw [weighted_vsub_of_point_apply, weighted_vsub_of_point_apply, ←@vsub_eq_zero_iff_eq V, vadd_vsub_assoc, vsub_vadd_eq_vsub_sub, ←add_sub_assoc, add_comm, add_sub_assoc, ←sum_sub_distrib], conv_lhs { congr, skip, congr, skip, funext, rw [←smul_sub, vsub_sub_vsub_cancel_left] }, rw [←sum_smul, h, one_smul, vsub_add_vsub_cancel, vsub_self] end /-- The weighted sum is unaffected by removing the base point, if present, from the set of points. -/ @[simp] lemma weighted_vsub_of_point_erase (w : ι → k) (p : ι → P) (i : ι) : (s.erase i).weighted_vsub_of_point p (p i) w = s.weighted_vsub_of_point p (p i) w := begin rw [weighted_vsub_of_point_apply, weighted_vsub_of_point_apply], apply sum_erase, rw [vsub_self, smul_zero] end /-- The weighted sum is unaffected by adding the base point, whether or not present, to the set of points. -/ @[simp] lemma weighted_vsub_of_point_insert [decidable_eq ι] (w : ι → k) (p : ι → P) (i : ι) : (insert i s).weighted_vsub_of_point p (p i) w = s.weighted_vsub_of_point p (p i) w := begin rw [weighted_vsub_of_point_apply, weighted_vsub_of_point_apply], apply sum_insert_zero, rw [vsub_self, smul_zero] end /-- The weighted sum is unaffected by changing the weights to the corresponding indicator function and adding points to the set. -/ lemma weighted_vsub_of_point_indicator_subset (w : ι → k) (p : ι → P) (b : P) {s₁ s₂ : finset ι} (h : s₁ ⊆ s₂) : s₁.weighted_vsub_of_point p b w = s₂.weighted_vsub_of_point p b (set.indicator ↑s₁ w) := begin rw [weighted_vsub_of_point_apply, weighted_vsub_of_point_apply], exact set.sum_indicator_subset_of_eq_zero w (λ i wi, wi • (p i -ᵥ b : V)) h (λ i, zero_smul k _) end /-- A weighted sum, over the image of an embedding, equals a weighted sum with the same points and weights over the original `finset`. -/ lemma weighted_vsub_of_point_map (e : ι₂ ↪ ι) (w : ι → k) (p : ι → P) (b : P) : (s₂.map e).weighted_vsub_of_point p b w = s₂.weighted_vsub_of_point (p ∘ e) b (w ∘ e) := begin simp_rw [weighted_vsub_of_point_apply], exact finset.sum_map _ _ _ end /-- A weighted sum of pairwise subtractions, expressed as a subtraction of two `weighted_vsub_of_point` expressions. -/ lemma sum_smul_vsub_eq_weighted_vsub_of_point_sub (w : ι → k) (p₁ p₂ : ι → P) (b : P) : ∑ i in s, w i • (p₁ i -ᵥ p₂ i) = s.weighted_vsub_of_point p₁ b w - s.weighted_vsub_of_point p₂ b w := by simp_rw [weighted_vsub_of_point_apply, ←sum_sub_distrib, ←smul_sub, vsub_sub_vsub_cancel_right] /-- A weighted sum of pairwise subtractions, where the point on the right is constant, expressed as a subtraction involving a `weighted_vsub_of_point` expression. -/ lemma sum_smul_vsub_const_eq_weighted_vsub_of_point_sub (w : ι → k) (p₁ : ι → P) (p₂ b : P) : ∑ i in s, w i • (p₁ i -ᵥ p₂) = s.weighted_vsub_of_point p₁ b w - (∑ i in s, w i) • (p₂ -ᵥ b) := by rw [sum_smul_vsub_eq_weighted_vsub_of_point_sub, weighted_vsub_of_point_apply_const] /-- A weighted sum of pairwise subtractions, where the point on the left is constant, expressed as a subtraction involving a `weighted_vsub_of_point` expression. -/ lemma sum_smul_const_vsub_eq_sub_weighted_vsub_of_point (w : ι → k) (p₂ : ι → P) (p₁ b : P) : ∑ i in s, w i • (p₁ -ᵥ p₂ i) = (∑ i in s, w i) • (p₁ -ᵥ b) - s.weighted_vsub_of_point p₂ b w := by rw [sum_smul_vsub_eq_weighted_vsub_of_point_sub, weighted_vsub_of_point_apply_const] /-- A weighted sum of the results of subtracting a default base point from the given points, as a linear map on the weights. This is intended to be used when the sum of the weights is 0; that condition is specified as a hypothesis on those lemmas that require it. -/ def weighted_vsub (p : ι → P) : (ι → k) →ₗ[k] V := s.weighted_vsub_of_point p (classical.choice S.nonempty) /-- Applying `weighted_vsub` with given weights. This is for the case where a result involving a default base point is OK (for example, when that base point will cancel out later); a more typical use case for `weighted_vsub` would involve selecting a preferred base point with `weighted_vsub_eq_weighted_vsub_of_point_of_sum_eq_zero` and then using `weighted_vsub_of_point_apply`. -/ lemma weighted_vsub_apply (w : ι → k) (p : ι → P) : s.weighted_vsub p w = ∑ i in s, w i • (p i -ᵥ (classical.choice S.nonempty)) := by simp [weighted_vsub, linear_map.sum_apply] /-- `weighted_vsub` gives the sum of the results of subtracting any base point, when the sum of the weights is 0. -/ lemma weighted_vsub_eq_weighted_vsub_of_point_of_sum_eq_zero (w : ι → k) (p : ι → P) (h : ∑ i in s, w i = 0) (b : P) : s.weighted_vsub p w = s.weighted_vsub_of_point p b w := s.weighted_vsub_of_point_eq_of_sum_eq_zero w p h _ _ /-- The value of `weighted_vsub`, where the given points are equal and the sum of the weights is 0. -/ @[simp] lemma weighted_vsub_apply_const (w : ι → k) (p : P) (h : ∑ i in s, w i = 0) : s.weighted_vsub (λ _, p) w = 0 := by rw [weighted_vsub, weighted_vsub_of_point_apply_const, h, zero_smul] /-- The `weighted_vsub` for an empty set is 0. -/ @[simp] lemma weighted_vsub_empty (w : ι → k) (p : ι → P) : (∅ : finset ι).weighted_vsub p w = (0:V) := by simp [weighted_vsub_apply] /-- The weighted sum is unaffected by changing the weights to the corresponding indicator function and adding points to the set. -/ lemma weighted_vsub_indicator_subset (w : ι → k) (p : ι → P) {s₁ s₂ : finset ι} (h : s₁ ⊆ s₂) : s₁.weighted_vsub p w = s₂.weighted_vsub p (set.indicator ↑s₁ w) := weighted_vsub_of_point_indicator_subset _ _ _ h /-- A weighted subtraction, over the image of an embedding, equals a weighted subtraction with the same points and weights over the original `finset`. -/ lemma weighted_vsub_map (e : ι₂ ↪ ι) (w : ι → k) (p : ι → P) : (s₂.map e).weighted_vsub p w = s₂.weighted_vsub (p ∘ e) (w ∘ e) := s₂.weighted_vsub_of_point_map _ _ _ _ /-- A weighted sum of pairwise subtractions, expressed as a subtraction of two `weighted_vsub` expressions. -/ lemma sum_smul_vsub_eq_weighted_vsub_sub (w : ι → k) (p₁ p₂ : ι → P) : ∑ i in s, w i • (p₁ i -ᵥ p₂ i) = s.weighted_vsub p₁ w - s.weighted_vsub p₂ w := s.sum_smul_vsub_eq_weighted_vsub_of_point_sub _ _ _ _ /-- A weighted sum of pairwise subtractions, where the point on the right is constant and the sum of the weights is 0. -/ lemma sum_smul_vsub_const_eq_weighted_vsub (w : ι → k) (p₁ : ι → P) (p₂ : P) (h : ∑ i in s, w i = 0) : ∑ i in s, w i • (p₁ i -ᵥ p₂) = s.weighted_vsub p₁ w := by rw [sum_smul_vsub_eq_weighted_vsub_sub, s.weighted_vsub_apply_const _ _ h, sub_zero] /-- A weighted sum of pairwise subtractions, where the point on the left is constant and the sum of the weights is 0. -/ lemma sum_smul_const_vsub_eq_neg_weighted_vsub (w : ι → k) (p₂ : ι → P) (p₁ : P) (h : ∑ i in s, w i = 0) : ∑ i in s, w i • (p₁ -ᵥ p₂ i) = -s.weighted_vsub p₂ w := by rw [sum_smul_vsub_eq_weighted_vsub_sub, s.weighted_vsub_apply_const _ _ h, zero_sub] /-- A weighted sum of the results of subtracting a default base point from the given points, added to that base point, as an affine map on the weights. This is intended to be used when the sum of the weights is 1, in which case it is an affine combination (barycenter) of the points with the given weights; that condition is specified as a hypothesis on those lemmas that require it. -/ def affine_combination (p : ι → P) : (ι → k) →ᵃ[k] P := { to_fun := λ w, s.weighted_vsub_of_point p (classical.choice S.nonempty) w +ᵥ (classical.choice S.nonempty), linear := s.weighted_vsub p, map_vadd' := λ w₁ w₂, by simp_rw [vadd_vadd, weighted_vsub, vadd_eq_add, linear_map.map_add] } /-- The linear map corresponding to `affine_combination` is `weighted_vsub`. -/ @[simp] lemma affine_combination_linear (p : ι → P) : (s.affine_combination p : (ι → k) →ᵃ[k] P).linear = s.weighted_vsub p := rfl /-- Applying `affine_combination` with given weights. This is for the case where a result involving a default base point is OK (for example, when that base point will cancel out later); a more typical use case for `affine_combination` would involve selecting a preferred base point with `affine_combination_eq_weighted_vsub_of_point_vadd_of_sum_eq_one` and then using `weighted_vsub_of_point_apply`. -/ lemma affine_combination_apply (w : ι → k) (p : ι → P) : s.affine_combination p w = s.weighted_vsub_of_point p (classical.choice S.nonempty) w +ᵥ (classical.choice S.nonempty) := rfl /-- The value of `affine_combination`, where the given points are equal. -/ @[simp] lemma affine_combination_apply_const (w : ι → k) (p : P) (h : ∑ i in s, w i = 1) : s.affine_combination (λ _, p) w = p := by rw [affine_combination_apply, s.weighted_vsub_of_point_apply_const, h, one_smul, vsub_vadd] /-- `affine_combination` gives the sum with any base point, when the sum of the weights is 1. -/ lemma affine_combination_eq_weighted_vsub_of_point_vadd_of_sum_eq_one (w : ι → k) (p : ι → P) (h : ∑ i in s, w i = 1) (b : P) : s.affine_combination p w = s.weighted_vsub_of_point p b w +ᵥ b := s.weighted_vsub_of_point_vadd_eq_of_sum_eq_one w p h _ _ /-- Adding a `weighted_vsub` to an `affine_combination`. -/ lemma weighted_vsub_vadd_affine_combination (w₁ w₂ : ι → k) (p : ι → P) : s.weighted_vsub p w₁ +ᵥ s.affine_combination p w₂ = s.affine_combination p (w₁ + w₂) := by rw [←vadd_eq_add, affine_map.map_vadd, affine_combination_linear] /-- Subtracting two `affine_combination`s. -/ lemma affine_combination_vsub (w₁ w₂ : ι → k) (p : ι → P) : s.affine_combination p w₁ -ᵥ s.affine_combination p w₂ = s.weighted_vsub p (w₁ - w₂) := by rw [←affine_map.linear_map_vsub, affine_combination_linear, vsub_eq_sub] lemma attach_affine_combination_of_injective (s : finset P) (w : P → k) (f : s → P) (hf : function.injective f) : s.attach.affine_combination f (w ∘ f) = (image f univ).affine_combination id w := begin simp only [affine_combination, weighted_vsub_of_point_apply, id.def, vadd_right_cancel_iff, function.comp_app, affine_map.coe_mk], let g₁ : s → V := λ i, w (f i) • (f i -ᵥ classical.choice S.nonempty), let g₂ : P → V := λ i, w i • (i -ᵥ classical.choice S.nonempty), change univ.sum g₁ = (image f univ).sum g₂, have hgf : g₁ = g₂ ∘ f, { ext, simp, }, rw [hgf, sum_image], exact λ _ _ _ _ hxy, hf hxy, end lemma attach_affine_combination_coe (s : finset P) (w : P → k) : s.attach.affine_combination (coe : s → P) (w ∘ coe) = s.affine_combination id w := by rw [attach_affine_combination_of_injective s w (coe : s → P) subtype.coe_injective, univ_eq_attach, attach_image_coe] omit S /-- Viewing a module as an affine space modelled on itself, affine combinations are just linear combinations. -/ @[simp] lemma affine_combination_eq_linear_combination (s : finset ι) (p : ι → V) (w : ι → k) (hw : ∑ i in s, w i = 1) : s.affine_combination p w = ∑ i in s, w i • p i := by simp [s.affine_combination_eq_weighted_vsub_of_point_vadd_of_sum_eq_one w p hw 0] include S /-- An `affine_combination` equals a point if that point is in the set and has weight 1 and the other points in the set have weight 0. -/ @[simp] lemma affine_combination_of_eq_one_of_eq_zero (w : ι → k) (p : ι → P) {i : ι} (his : i ∈ s) (hwi : w i = 1) (hw0 : ∀ i2 ∈ s, i2 ≠ i → w i2 = 0) : s.affine_combination p w = p i := begin have h1 : ∑ i in s, w i = 1 := hwi ▸ sum_eq_single i hw0 (λ h, false.elim (h his)), rw [s.affine_combination_eq_weighted_vsub_of_point_vadd_of_sum_eq_one w p h1 (p i), weighted_vsub_of_point_apply], convert zero_vadd V (p i), convert sum_eq_zero _, intros i2 hi2, by_cases h : i2 = i, { simp [h] }, { simp [hw0 i2 hi2 h] } end /-- An affine combination is unaffected by changing the weights to the corresponding indicator function and adding points to the set. -/ lemma affine_combination_indicator_subset (w : ι → k) (p : ι → P) {s₁ s₂ : finset ι} (h : s₁ ⊆ s₂) : s₁.affine_combination p w = s₂.affine_combination p (set.indicator ↑s₁ w) := by rw [affine_combination_apply, affine_combination_apply, weighted_vsub_of_point_indicator_subset _ _ _ h] /-- An affine combination, over the image of an embedding, equals an affine combination with the same points and weights over the original `finset`. -/ lemma affine_combination_map (e : ι₂ ↪ ι) (w : ι → k) (p : ι → P) : (s₂.map e).affine_combination p w = s₂.affine_combination (p ∘ e) (w ∘ e) := by simp_rw [affine_combination_apply, weighted_vsub_of_point_map] /-- A weighted sum of pairwise subtractions, expressed as a subtraction of two `affine_combination` expressions. -/ lemma sum_smul_vsub_eq_affine_combination_vsub (w : ι → k) (p₁ p₂ : ι → P) : ∑ i in s, w i • (p₁ i -ᵥ p₂ i) = s.affine_combination p₁ w -ᵥ s.affine_combination p₂ w := begin simp_rw [affine_combination_apply, vadd_vsub_vadd_cancel_right], exact s.sum_smul_vsub_eq_weighted_vsub_of_point_sub _ _ _ _ end /-- A weighted sum of pairwise subtractions, where the point on the right is constant and the sum of the weights is 1. -/ lemma sum_smul_vsub_const_eq_affine_combination_vsub (w : ι → k) (p₁ : ι → P) (p₂ : P) (h : ∑ i in s, w i = 1) : ∑ i in s, w i • (p₁ i -ᵥ p₂) = s.affine_combination p₁ w -ᵥ p₂ := by rw [sum_smul_vsub_eq_affine_combination_vsub, affine_combination_apply_const _ _ _ h] /-- A weighted sum of pairwise subtractions, where the point on the left is constant and the sum of the weights is 1. -/ lemma sum_smul_const_vsub_eq_vsub_affine_combination (w : ι → k) (p₂ : ι → P) (p₁ : P) (h : ∑ i in s, w i = 1) : ∑ i in s, w i • (p₁ -ᵥ p₂ i) = p₁ -ᵥ s.affine_combination p₂ w := by rw [sum_smul_vsub_eq_affine_combination_vsub, affine_combination_apply_const _ _ _ h] variables {V} /-- Suppose an indexed family of points is given, along with a subset of the index type. A vector can be expressed as `weighted_vsub_of_point` using a `finset` lying within that subset and with a given sum of weights if and only if it can be expressed as `weighted_vsub_of_point` with that sum of weights for the corresponding indexed family whose index type is the subtype corresponding to that subset. -/ lemma eq_weighted_vsub_of_point_subset_iff_eq_weighted_vsub_of_point_subtype {v : V} {x : k} {s : set ι} {p : ι → P} {b : P} : (∃ (fs : finset ι) (hfs : ↑fs ⊆ s) (w : ι → k) (hw : ∑ i in fs, w i = x), v = fs.weighted_vsub_of_point p b w) ↔ ∃ (fs : finset s) (w : s → k) (hw : ∑ i in fs, w i = x), v = fs.weighted_vsub_of_point (λ (i : s), p i) b w := begin simp_rw weighted_vsub_of_point_apply, split, { rintros ⟨fs, hfs, w, rfl, rfl⟩, use [fs.subtype s, λ i, w i, sum_subtype_of_mem _ hfs, (sum_subtype_of_mem _ hfs).symm] }, { rintros ⟨fs, w, rfl, rfl⟩, refine ⟨fs.map (function.embedding.subtype _), map_subtype_subset _, λ i, if h : i ∈ s then w ⟨i, h⟩ else 0, _, _⟩; simp } end variables (k) /-- Suppose an indexed family of points is given, along with a subset of the index type. A vector can be expressed as `weighted_vsub` using a `finset` lying within that subset and with sum of weights 0 if and only if it can be expressed as `weighted_vsub` with sum of weights 0 for the corresponding indexed family whose index type is the subtype corresponding to that subset. -/ lemma eq_weighted_vsub_subset_iff_eq_weighted_vsub_subtype {v : V} {s : set ι} {p : ι → P} : (∃ (fs : finset ι) (hfs : ↑fs ⊆ s) (w : ι → k) (hw : ∑ i in fs, w i = 0), v = fs.weighted_vsub p w) ↔ ∃ (fs : finset s) (w : s → k) (hw : ∑ i in fs, w i = 0), v = fs.weighted_vsub (λ (i : s), p i) w := eq_weighted_vsub_of_point_subset_iff_eq_weighted_vsub_of_point_subtype variables (V) /-- Suppose an indexed family of points is given, along with a subset of the index type. A point can be expressed as an `affine_combination` using a `finset` lying within that subset and with sum of weights 1 if and only if it can be expressed an `affine_combination` with sum of weights 1 for the corresponding indexed family whose index type is the subtype corresponding to that subset. -/ lemma eq_affine_combination_subset_iff_eq_affine_combination_subtype {p0 : P} {s : set ι} {p : ι → P} : (∃ (fs : finset ι) (hfs : ↑fs ⊆ s) (w : ι → k) (hw : ∑ i in fs, w i = 1), p0 = fs.affine_combination p w) ↔ ∃ (fs : finset s) (w : s → k) (hw : ∑ i in fs, w i = 1), p0 = fs.affine_combination (λ (i : s), p i) w := begin simp_rw [affine_combination_apply, eq_vadd_iff_vsub_eq], exact eq_weighted_vsub_of_point_subset_iff_eq_weighted_vsub_of_point_subtype end variables {k V} /-- Affine maps commute with affine combinations. -/ lemma map_affine_combination {V₂ P₂ : Type*} [add_comm_group V₂] [module k V₂] [affine_space V₂ P₂] (p : ι → P) (w : ι → k) (hw : s.sum w = 1) (f : P →ᵃ[k] P₂) : f (s.affine_combination p w) = s.affine_combination (f ∘ p) w := begin have b := classical.choice (infer_instance : affine_space V P).nonempty, have b₂ := classical.choice (infer_instance : affine_space V₂ P₂).nonempty, rw [s.affine_combination_eq_weighted_vsub_of_point_vadd_of_sum_eq_one w p hw b, s.affine_combination_eq_weighted_vsub_of_point_vadd_of_sum_eq_one w (f ∘ p) hw b₂, ← s.weighted_vsub_of_point_vadd_eq_of_sum_eq_one w (f ∘ p) hw (f b) b₂], simp only [weighted_vsub_of_point_apply, ring_hom.id_apply, affine_map.map_vadd, linear_map.map_smulₛₗ, affine_map.linear_map_vsub, linear_map.map_sum], end end finset namespace finset variables (k : Type*) {V : Type*} {P : Type*} [division_ring k] [add_comm_group V] [module k V] variables [affine_space V P] {ι : Type*} (s : finset ι) {ι₂ : Type*} (s₂ : finset ι₂) /-- The weights for the centroid of some points. -/ def centroid_weights : ι → k := function.const ι (card s : k) ⁻¹ /-- `centroid_weights` at any point. -/ @[simp] lemma centroid_weights_apply (i : ι) : s.centroid_weights k i = (card s : k) ⁻¹ := rfl /-- `centroid_weights` equals a constant function. -/ lemma centroid_weights_eq_const : s.centroid_weights k = function.const ι ((card s : k) ⁻¹) := rfl variables {k} /-- The weights in the centroid sum to 1, if the number of points, converted to `k`, is not zero. -/ lemma sum_centroid_weights_eq_one_of_cast_card_ne_zero (h : (card s : k) ≠ 0) : ∑ i in s, s.centroid_weights k i = 1 := by simp [h] variables (k) /-- In the characteristic zero case, the weights in the centroid sum to 1 if the number of points is not zero. -/ lemma sum_centroid_weights_eq_one_of_card_ne_zero [char_zero k] (h : card s ≠ 0) : ∑ i in s, s.centroid_weights k i = 1 := by simp [h] /-- In the characteristic zero case, the weights in the centroid sum to 1 if the set is nonempty. -/ lemma sum_centroid_weights_eq_one_of_nonempty [char_zero k] (h : s.nonempty) : ∑ i in s, s.centroid_weights k i = 1 := s.sum_centroid_weights_eq_one_of_card_ne_zero k (ne_of_gt (card_pos.2 h)) /-- In the characteristic zero case, the weights in the centroid sum to 1 if the number of points is `n + 1`. -/ lemma sum_centroid_weights_eq_one_of_card_eq_add_one [char_zero k] {n : ℕ} (h : card s = n + 1) : ∑ i in s, s.centroid_weights k i = 1 := s.sum_centroid_weights_eq_one_of_card_ne_zero k (h.symm ▸ nat.succ_ne_zero n) include V /-- The centroid of some points. Although defined for any `s`, this is intended to be used in the case where the number of points, converted to `k`, is not zero. -/ def centroid (p : ι → P) : P := s.affine_combination p (s.centroid_weights k) /-- The definition of the centroid. -/ lemma centroid_def (p : ι → P) : s.centroid k p = s.affine_combination p (s.centroid_weights k) := rfl lemma centroid_univ (s : finset P) : univ.centroid k (coe : s → P) = s.centroid k id := by { rw [centroid, centroid, ← s.attach_affine_combination_coe], congr, ext, simp, } /-- The centroid of a single point. -/ @[simp] lemma centroid_singleton (p : ι → P) (i : ι) : ({i} : finset ι).centroid k p = p i := by simp [centroid_def, affine_combination_apply] /-- The centroid of two points, expressed directly as adding a vector to a point. -/ lemma centroid_insert_singleton [invertible (2 : k)] (p : ι → P) (i₁ i₂ : ι) : ({i₁, i₂} : finset ι).centroid k p = (2 ⁻¹ : k) • (p i₂ -ᵥ p i₁) +ᵥ p i₁ := begin by_cases h : i₁ = i₂, { simp [h] }, { have hc : (card ({i₁, i₂} : finset ι) : k) ≠ 0, { rw [card_insert_of_not_mem (not_mem_singleton.2 h), card_singleton], norm_num, exact nonzero_of_invertible _ }, rw [centroid_def, affine_combination_eq_weighted_vsub_of_point_vadd_of_sum_eq_one _ _ _ (sum_centroid_weights_eq_one_of_cast_card_ne_zero _ hc) (p i₁)], simp [h], norm_num } end /-- The centroid of two points indexed by `fin 2`, expressed directly as adding a vector to the first point. -/ lemma centroid_insert_singleton_fin [invertible (2 : k)] (p : fin 2 → P) : univ.centroid k p = (2 ⁻¹ : k) • (p 1 -ᵥ p 0) +ᵥ p 0 := begin rw univ_fin2, convert centroid_insert_singleton k p 0 1 end /-- A centroid, over the image of an embedding, equals a centroid with the same points and weights over the original `finset`. -/ lemma centroid_map (e : ι₂ ↪ ι) (p : ι → P) : (s₂.map e).centroid k p = s₂.centroid k (p ∘ e) := by simp [centroid_def, affine_combination_map, centroid_weights] omit V /-- `centroid_weights` gives the weights for the centroid as a constant function, which is suitable when summing over the points whose centroid is being taken. This function gives the weights in a form suitable for summing over a larger set of points, as an indicator function that is zero outside the set whose centroid is being taken. In the case of a `fintype`, the sum may be over `univ`. -/ def centroid_weights_indicator : ι → k := set.indicator ↑s (s.centroid_weights k) /-- The definition of `centroid_weights_indicator`. -/ lemma centroid_weights_indicator_def : s.centroid_weights_indicator k = set.indicator ↑s (s.centroid_weights k) := rfl /-- The sum of the weights for the centroid indexed by a `fintype`. -/ lemma sum_centroid_weights_indicator [fintype ι] : ∑ i, s.centroid_weights_indicator k i = ∑ i in s, s.centroid_weights k i := (set.sum_indicator_subset _ (subset_univ _)).symm /-- In the characteristic zero case, the weights in the centroid indexed by a `fintype` sum to 1 if the number of points is not zero. -/ lemma sum_centroid_weights_indicator_eq_one_of_card_ne_zero [char_zero k] [fintype ι] (h : card s ≠ 0) : ∑ i, s.centroid_weights_indicator k i = 1 := begin rw sum_centroid_weights_indicator, exact s.sum_centroid_weights_eq_one_of_card_ne_zero k h end /-- In the characteristic zero case, the weights in the centroid indexed by a `fintype` sum to 1 if the set is nonempty. -/ lemma sum_centroid_weights_indicator_eq_one_of_nonempty [char_zero k] [fintype ι] (h : s.nonempty) : ∑ i, s.centroid_weights_indicator k i = 1 := begin rw sum_centroid_weights_indicator, exact s.sum_centroid_weights_eq_one_of_nonempty k h end /-- In the characteristic zero case, the weights in the centroid indexed by a `fintype` sum to 1 if the number of points is `n + 1`. -/ lemma sum_centroid_weights_indicator_eq_one_of_card_eq_add_one [char_zero k] [fintype ι] {n : ℕ} (h : card s = n + 1) : ∑ i, s.centroid_weights_indicator k i = 1 := begin rw sum_centroid_weights_indicator, exact s.sum_centroid_weights_eq_one_of_card_eq_add_one k h end include V /-- The centroid as an affine combination over a `fintype`. -/ lemma centroid_eq_affine_combination_fintype [fintype ι] (p : ι → P) : s.centroid k p = univ.affine_combination p (s.centroid_weights_indicator k) := affine_combination_indicator_subset _ _ (subset_univ _) /-- An indexed family of points that is injective on the given `finset` has the same centroid as the image of that `finset`. This is stated in terms of a set equal to the image to provide control of definitional equality for the index type used for the centroid of the image. -/ lemma centroid_eq_centroid_image_of_inj_on {p : ι → P} (hi : ∀ i j ∈ s, p i = p j → i = j) {ps : set P} [fintype ps] (hps : ps = p '' ↑s) : s.centroid k p = (univ : finset ps).centroid k (λ x, x) := begin let f : p '' ↑s → ι := λ x, x.property.some, have hf : ∀ x, f x ∈ s ∧ p (f x) = x := λ x, x.property.some_spec, let f' : ps → ι := λ x, f ⟨x, hps ▸ x.property⟩, have hf' : ∀ x, f' x ∈ s ∧ p (f' x) = x := λ x, hf ⟨x, hps ▸ x.property⟩, have hf'i : function.injective f', { intros x y h, rw [subtype.ext_iff, ←(hf' x).2, ←(hf' y).2, h] }, let f'e : ps ↪ ι := ⟨f', hf'i⟩, have hu : finset.univ.map f'e = s, { ext x, rw mem_map, split, { rintros ⟨i, _, rfl⟩, exact (hf' i).1 }, { intro hx, use [⟨p x, hps.symm ▸ set.mem_image_of_mem _ hx⟩, mem_univ _], refine hi _ (hf' _).1 _ hx _, rw (hf' _).2, refl } }, rw [←hu, centroid_map], congr' with x, change p (f' x) = ↑x, rw (hf' x).2 end /-- Two indexed families of points that are injective on the given `finset`s and with the same points in the image of those `finset`s have the same centroid. -/ lemma centroid_eq_of_inj_on_of_image_eq {p : ι → P} (hi : ∀ i j ∈ s, p i = p j → i = j) {p₂ : ι₂ → P} (hi₂ : ∀ i j ∈ s₂, p₂ i = p₂ j → i = j) (he : p '' ↑s = p₂ '' ↑s₂) : s.centroid k p = s₂.centroid k p₂ := by rw [s.centroid_eq_centroid_image_of_inj_on k hi rfl, s₂.centroid_eq_centroid_image_of_inj_on k hi₂ he] end finset section affine_space' variables {k : Type*} {V : Type*} {P : Type*} [ring k] [add_comm_group V] [module k V] [affine_space V P] variables {ι : Type*} include V /-- A `weighted_vsub` with sum of weights 0 is in the `vector_span` of an indexed family. -/ lemma weighted_vsub_mem_vector_span {s : finset ι} {w : ι → k} (h : ∑ i in s, w i = 0) (p : ι → P) : s.weighted_vsub p w ∈ vector_span k (set.range p) := begin rcases is_empty_or_nonempty ι with hι|⟨⟨i0⟩⟩, { resetI, simp [finset.eq_empty_of_is_empty s] }, { rw [vector_span_range_eq_span_range_vsub_right k p i0, ←set.image_univ, finsupp.mem_span_image_iff_total, finset.weighted_vsub_eq_weighted_vsub_of_point_of_sum_eq_zero s w p h (p i0), finset.weighted_vsub_of_point_apply], let w' := set.indicator ↑s w, have hwx : ∀ i, w' i ≠ 0 → i ∈ s := λ i, set.mem_of_indicator_ne_zero, use [finsupp.on_finset s w' hwx, set.subset_univ _], rw [finsupp.total_apply, finsupp.on_finset_sum hwx], { apply finset.sum_congr rfl, intros i hi, simp [w', set.indicator_apply, if_pos hi] }, { exact λ _, zero_smul k _ } }, end /-- An `affine_combination` with sum of weights 1 is in the `affine_span` of an indexed family, if the underlying ring is nontrivial. -/ lemma affine_combination_mem_affine_span [nontrivial k] {s : finset ι} {w : ι → k} (h : ∑ i in s, w i = 1) (p : ι → P) : s.affine_combination p w ∈ affine_span k (set.range p) := begin have hnz : ∑ i in s, w i ≠ 0 := h.symm ▸ one_ne_zero, have hn : s.nonempty := finset.nonempty_of_sum_ne_zero hnz, cases hn with i1 hi1, let w1 : ι → k := function.update (function.const ι 0) i1 1, have hw1 : ∑ i in s, w1 i = 1, { rw [finset.sum_update_of_mem hi1, finset.sum_const_zero, add_zero] }, have hw1s : s.affine_combination p w1 = p i1 := s.affine_combination_of_eq_one_of_eq_zero w1 p hi1 (function.update_same _ _ _) (λ _ _ hne, function.update_noteq hne _ _), have hv : s.affine_combination p w -ᵥ p i1 ∈ (affine_span k (set.range p)).direction, { rw [direction_affine_span, ←hw1s, finset.affine_combination_vsub], apply weighted_vsub_mem_vector_span, simp [pi.sub_apply, h, hw1] }, rw ←vsub_vadd (s.affine_combination p w) (p i1), exact affine_subspace.vadd_mem_of_mem_direction hv (mem_affine_span k (set.mem_range_self _)) end variables (k) {V} /-- A vector is in the `vector_span` of an indexed family if and only if it is a `weighted_vsub` with sum of weights 0. -/ lemma mem_vector_span_iff_eq_weighted_vsub {v : V} {p : ι → P} : v ∈ vector_span k (set.range p) ↔ ∃ (s : finset ι) (w : ι → k) (h : ∑ i in s, w i = 0), v = s.weighted_vsub p w := begin split, { rcases is_empty_or_nonempty ι with hι|⟨⟨i0⟩⟩, swap, { rw [vector_span_range_eq_span_range_vsub_right k p i0, ←set.image_univ, finsupp.mem_span_image_iff_total], rintros ⟨l, hl, hv⟩, use insert i0 l.support, set w := (l : ι → k) - function.update (function.const ι 0 : ι → k) i0 (∑ i in l.support, l i) with hwdef, use w, have hw : ∑ i in insert i0 l.support, w i = 0, { rw hwdef, simp_rw [pi.sub_apply, finset.sum_sub_distrib, finset.sum_update_of_mem (finset.mem_insert_self _ _), finset.sum_const_zero, finset.sum_insert_of_eq_zero_if_not_mem finsupp.not_mem_support_iff.1, add_zero, sub_self] }, use hw, have hz : w i0 • (p i0 -ᵥ p i0 : V) = 0 := (vsub_self (p i0)).symm ▸ smul_zero _, change (λ i, w i • (p i -ᵥ p i0 : V)) i0 = 0 at hz, rw [finset.weighted_vsub_eq_weighted_vsub_of_point_of_sum_eq_zero _ w p hw (p i0), finset.weighted_vsub_of_point_apply, ←hv, finsupp.total_apply, finset.sum_insert_zero hz], change ∑ i in l.support, l i • _ = _, congr' with i, by_cases h : i = i0, { simp [h] }, { simp [hwdef, h] } }, { resetI, rw [set.range_eq_empty, vector_span_empty, submodule.mem_bot], rintro rfl, use [∅], simp } }, { rintros ⟨s, w, hw, rfl⟩, exact weighted_vsub_mem_vector_span hw p } end variables {k} /-- A point in the `affine_span` of an indexed family is an `affine_combination` with sum of weights 1. See also `eq_affine_combination_of_mem_affine_span_of_fintype`. -/ lemma eq_affine_combination_of_mem_affine_span {p1 : P} {p : ι → P} (h : p1 ∈ affine_span k (set.range p)) : ∃ (s : finset ι) (w : ι → k) (hw : ∑ i in s, w i = 1), p1 = s.affine_combination p w := begin have hn : ((affine_span k (set.range p)) : set P).nonempty := ⟨p1, h⟩, rw [affine_span_nonempty, set.range_nonempty_iff_nonempty] at hn, cases hn with i0, have h0 : p i0 ∈ affine_span k (set.range p) := mem_affine_span k (set.mem_range_self i0), have hd : p1 -ᵥ p i0 ∈ (affine_span k (set.range p)).direction := affine_subspace.vsub_mem_direction h h0, rw [direction_affine_span, mem_vector_span_iff_eq_weighted_vsub] at hd, rcases hd with ⟨s, w, h, hs⟩, let s' := insert i0 s, let w' := set.indicator ↑s w, have h' : ∑ i in s', w' i = 0, { rw [←h, set.sum_indicator_subset _ (finset.subset_insert i0 s)] }, have hs' : s'.weighted_vsub p w' = p1 -ᵥ p i0, { rw hs, exact (finset.weighted_vsub_indicator_subset _ _ (finset.subset_insert i0 s)).symm }, let w0 : ι → k := function.update (function.const ι 0) i0 1, have hw0 : ∑ i in s', w0 i = 1, { rw [finset.sum_update_of_mem (finset.mem_insert_self _ _), finset.sum_const_zero, add_zero] }, have hw0s : s'.affine_combination p w0 = p i0 := s'.affine_combination_of_eq_one_of_eq_zero w0 p (finset.mem_insert_self _ _) (function.update_same _ _ _) (λ _ _ hne, function.update_noteq hne _ _), use [s', w0 + w'], split, { simp [pi.add_apply, finset.sum_add_distrib, hw0, h'] }, { rw [add_comm, ←finset.weighted_vsub_vadd_affine_combination, hw0s, hs', vsub_vadd] } end lemma eq_affine_combination_of_mem_affine_span_of_fintype [fintype ι] {p1 : P} {p : ι → P} (h : p1 ∈ affine_span k (set.range p)) : ∃ (w : ι → k) (hw : ∑ i, w i = 1), p1 = finset.univ.affine_combination p w := begin obtain ⟨s, w, hw, rfl⟩ := eq_affine_combination_of_mem_affine_span h, refine ⟨(s : set ι).indicator w, _, finset.affine_combination_indicator_subset w p s.subset_univ⟩, simp only [finset.mem_coe, set.indicator_apply, ← hw], rw fintype.sum_extend_by_zero s w, end variables (k V) /-- A point is in the `affine_span` of an indexed family if and only if it is an `affine_combination` with sum of weights 1, provided the underlying ring is nontrivial. -/ lemma mem_affine_span_iff_eq_affine_combination [nontrivial k] {p1 : P} {p : ι → P} : p1 ∈ affine_span k (set.range p) ↔ ∃ (s : finset ι) (w : ι → k) (hw : ∑ i in s, w i = 1), p1 = s.affine_combination p w := begin split, { exact eq_affine_combination_of_mem_affine_span }, { rintros ⟨s, w, hw, rfl⟩, exact affine_combination_mem_affine_span hw p } end /-- Given a family of points together with a chosen base point in that family, membership of the affine span of this family corresponds to an identity in terms of `weighted_vsub_of_point`, with weights that are not required to sum to 1. -/ lemma mem_affine_span_iff_eq_weighted_vsub_of_point_vadd [nontrivial k] (p : ι → P) (j : ι) (q : P) : q ∈ affine_span k (set.range p) ↔ ∃ (s : finset ι) (w : ι → k), q = s.weighted_vsub_of_point p (p j) w +ᵥ (p j) := begin split, { intros hq, obtain ⟨s, w, hw, rfl⟩ := eq_affine_combination_of_mem_affine_span hq, exact ⟨s, w, s.affine_combination_eq_weighted_vsub_of_point_vadd_of_sum_eq_one w p hw (p j)⟩, }, { rintros ⟨s, w, rfl⟩, classical, let w' : ι → k := function.update w j (1 - (s \ {j}).sum w), have h₁ : (insert j s).sum w' = 1, { by_cases hj : j ∈ s, { simp [finset.sum_update_of_mem hj, finset.insert_eq_of_mem hj], }, { simp [w', finset.sum_insert hj, finset.sum_update_of_not_mem hj, hj], }, }, have hww : ∀ i, i ≠ j → w i = w' i, { intros i hij, simp [w', hij], }, rw [s.weighted_vsub_of_point_eq_of_weights_eq p j w w' hww, ← s.weighted_vsub_of_point_insert w' p j, ← (insert j s).affine_combination_eq_weighted_vsub_of_point_vadd_of_sum_eq_one w' p h₁ (p j)], exact affine_combination_mem_affine_span h₁ p, }, end variables {k V} /-- Given a set of points, together with a chosen base point in this set, if we affinely transport all other members of the set along the line joining them to this base point, the affine span is unchanged. -/ lemma affine_span_eq_affine_span_line_map_units [nontrivial k] {s : set P} {p : P} (hp : p ∈ s) (w : s → units k) : affine_span k (set.range (λ (q : s), affine_map.line_map p ↑q (w q : k))) = affine_span k s := begin have : s = set.range (coe : s → P), { simp, }, conv_rhs { rw this, }, apply le_antisymm; intros q hq; erw mem_affine_span_iff_eq_weighted_vsub_of_point_vadd k V _ (⟨p, hp⟩ : s) q at hq ⊢; obtain ⟨t, μ, rfl⟩ := hq; use t; [use λ x, (μ x) * ↑(w x), use λ x, (μ x) * ↑(w x)⁻¹]; simp [smul_smul], end end affine_space' section division_ring variables {k : Type*} {V : Type*} {P : Type*} [division_ring k] [add_comm_group V] [module k V] variables [affine_space V P] {ι : Type*} include V open set finset /-- The centroid lies in the affine span if the number of points, converted to `k`, is not zero. -/ lemma centroid_mem_affine_span_of_cast_card_ne_zero {s : finset ι} (p : ι → P) (h : (card s : k) ≠ 0) : s.centroid k p ∈ affine_span k (range p) := affine_combination_mem_affine_span (s.sum_centroid_weights_eq_one_of_cast_card_ne_zero h) p variables (k) /-- In the characteristic zero case, the centroid lies in the affine span if the number of points is not zero. -/ lemma centroid_mem_affine_span_of_card_ne_zero [char_zero k] {s : finset ι} (p : ι → P) (h : card s ≠ 0) : s.centroid k p ∈ affine_span k (range p) := affine_combination_mem_affine_span (s.sum_centroid_weights_eq_one_of_card_ne_zero k h) p /-- In the characteristic zero case, the centroid lies in the affine span if the set is nonempty. -/ lemma centroid_mem_affine_span_of_nonempty [char_zero k] {s : finset ι} (p : ι → P) (h : s.nonempty) : s.centroid k p ∈ affine_span k (range p) := affine_combination_mem_affine_span (s.sum_centroid_weights_eq_one_of_nonempty k h) p /-- In the characteristic zero case, the centroid lies in the affine span if the number of points is `n + 1`. -/ lemma centroid_mem_affine_span_of_card_eq_add_one [char_zero k] {s : finset ι} (p : ι → P) {n : ℕ} (h : card s = n + 1) : s.centroid k p ∈ affine_span k (range p) := affine_combination_mem_affine_span (s.sum_centroid_weights_eq_one_of_card_eq_add_one k h) p end division_ring namespace affine_map variables {k : Type*} {V : Type*} (P : Type*) [comm_ring k] [add_comm_group V] [module k V] variables [affine_space V P] {ι : Type*} (s : finset ι) include V -- TODO: define `affine_map.proj`, `affine_map.fst`, `affine_map.snd` /-- A weighted sum, as an affine map on the points involved. -/ def weighted_vsub_of_point (w : ι → k) : ((ι → P) × P) →ᵃ[k] V := { to_fun := λ p, s.weighted_vsub_of_point p.fst p.snd w, linear := ∑ i in s, w i • ((linear_map.proj i).comp (linear_map.fst _ _ _) - linear_map.snd _ _ _), map_vadd' := begin rintros ⟨p, b⟩ ⟨v, b'⟩, simp [linear_map.sum_apply, finset.weighted_vsub_of_point, vsub_vadd_eq_vsub_sub, vadd_vsub_assoc, add_sub, ← sub_add_eq_add_sub, smul_add, finset.sum_add_distrib] end } end affine_map
9abb73e9212a980189cf72d5837ecf8bf24bb7e1
75db7e3219bba2fbf41bf5b905f34fcb3c6ca3f2
/library/data/examples/vector.lean
d3d72b5df71ce5d9096c8daaf973a7fe4346e547
[ "Apache-2.0" ]
permissive
jroesch/lean
30ef0860fa905d35b9ad6f76de1a4f65c9af6871
3de4ec1a6ce9a960feb2a48eeea8b53246fa34f2
refs/heads/master
1,586,090,835,348
1,455,142,203,000
1,455,142,277,000
51,536,958
1
0
null
1,455,215,811,000
1,455,215,811,000
null
UTF-8
Lean
false
false
13,174
lean
/- 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, Leonardo de Moura This file demonstrates how to encode vectors using indexed inductive families. In standard library we do not use this approach. -/ import data.nat data.list data.fin open nat prod fin inductive vector (A : Type) : nat → Type := | nil {} : vector A zero | cons : Π {n}, A → vector A n → vector A (succ n) namespace vector notation a :: b := cons a b notation `[` l:(foldr `, ` (h t, cons h t) nil `]`) := l variables {A B C : Type} protected definition is_inhabited [instance] [h : inhabited A] : ∀ (n : nat), inhabited (vector A n) | 0 := inhabited.mk [] | (n+1) := inhabited.mk (inhabited.value h :: inhabited.value (is_inhabited n)) theorem vector0_eq_nil : ∀ (v : vector A 0), v = [] | [] := rfl definition head : Π {n : nat}, vector A (succ n) → A | n (a::v) := a definition tail : Π {n : nat}, vector A (succ n) → vector A n | n (a::v) := v theorem head_cons {n : nat} (h : A) (t : vector A n) : head (h :: t) = h := rfl theorem tail_cons {n : nat} (h : A) (t : vector A n) : tail (h :: t) = t := rfl theorem eta : ∀ {n : nat} (v : vector A (succ n)), head v :: tail v = v | n (a::v) := rfl definition last : Π {n : nat}, vector A (succ n) → A | last [a] := a | last (a::v) := last v theorem last_singleton (a : A) : last [a] = a := rfl theorem last_cons {n : nat} (a : A) (v : vector A (succ n)) : last (a :: v) = last v := rfl definition const : Π (n : nat), A → vector A n | 0 a := [] | (succ n) a := a :: const n a theorem head_const (n : nat) (a : A) : head (const (succ n) a) = a := rfl theorem last_const : ∀ (n : nat) (a : A), last (const (succ n) a) = a | 0 a := rfl | (n+1) a := last_const n a definition nth : Π {n : nat}, vector A n → fin n → A | ⌞0⌟ [] i := elim0 i | ⌞n+1⌟ (a :: v) (mk 0 _) := a | ⌞n+1⌟ (a :: v) (mk (succ i) h) := nth v (mk_pred i h) lemma nth_zero {n : nat} (a : A) (v : vector A n) (h : 0 < succ n) : nth (a::v) (mk 0 h) = a := rfl lemma nth_succ {n : nat} (a : A) (v : vector A n) (i : nat) (h : succ i < succ n) : nth (a::v) (mk (succ i) h) = nth v (mk_pred i h) := rfl definition tabulate : Π {n : nat}, (fin n → A) → vector A n | 0 f := [] | (n+1) f := f (fin.zero n) :: tabulate (λ i : fin n, f (succ i)) theorem nth_tabulate : ∀ {n : nat} (f : fin n → A) (i : fin n), nth (tabulate f) i = f i | 0 f i := elim0 i | (n+1) f (mk 0 h) := by reflexivity | (n+1) f (mk (succ i) h) := begin change nth (f (fin.zero n) :: tabulate (λ i : fin n, f (succ i))) (mk (succ i) h) = f (mk (succ i) h), rewrite nth_succ, rewrite nth_tabulate end definition map (f : A → B) : Π {n : nat}, vector A n → vector B n | map [] := [] | map (a::v) := f a :: map v theorem map_nil (f : A → B) : map f [] = [] := rfl theorem map_cons {n : nat} (f : A → B) (h : A) (t : vector A n) : map f (h :: t) = f h :: map f t := rfl theorem nth_map (f : A → B) : ∀ {n : nat} (v : vector A n) (i : fin n), nth (map f v) i = f (nth v i) | 0 v i := elim0 i | (succ n) (a :: t) (mk 0 h) := by reflexivity | (succ n) (a :: t) (mk (succ i) h) := by rewrite [map_cons, *nth_succ, nth_map] section open function theorem map_id : ∀ {n : nat} (v : vector A n), map id v = v | 0 [] := rfl | (succ n) (x::xs) := by rewrite [map_cons, map_id] theorem map_map (g : B → C) (f : A → B) : ∀ {n :nat} (v : vector A n), map g (map f v) = map (g ∘ f) v | 0 [] := rfl | (succ n) (a :: l) := show (g ∘ f) a :: map g (map f l) = map (g ∘ f) (a :: l), by rewrite (map_map l) end definition map2 (f : A → B → C) : Π {n : nat}, vector A n → vector B n → vector C n | map2 [] [] := [] | map2 (a::va) (b::vb) := f a b :: map2 va vb theorem map2_nil (f : A → B → C) : map2 f [] [] = [] := rfl theorem map2_cons {n : nat} (f : A → B → C) (h₁ : A) (h₂ : B) (t₁ : vector A n) (t₂ : vector B n) : map2 f (h₁ :: t₁) (h₂ :: t₂) = f h₁ h₂ :: map2 f t₁ t₂ := rfl definition append : Π {n m : nat}, vector A n → vector A m → vector A (n ⊕ m) | 0 m [] w := w | (succ n) m (a::v) w := a :: (append v w) theorem append_nil_left {n : nat} (v : vector A n) : append [] v = v := rfl theorem append_cons {n m : nat} (h : A) (t : vector A n) (v : vector A m) : append (h::t) v = h :: (append t v) := rfl theorem map_append (f : A → B) : ∀ {n m : nat} (v : vector A n) (w : vector A m), map f (append v w) = append (map f v) (map f w) | 0 m [] w := rfl | (n+1) m (h :: t) w := begin change (f h :: map f (append t w) = f h :: append (map f t) (map f w)), rewrite map_append end definition unzip : Π {n : nat}, vector (A × B) n → vector A n × vector B n | unzip [] := ([], []) | unzip ((a, b) :: v) := (a :: pr₁ (unzip v), b :: pr₂ (unzip v)) theorem unzip_nil : unzip (@nil (A × B)) = ([], []) := rfl theorem unzip_cons {n : nat} (a : A) (b : B) (v : vector (A × B) n) : unzip ((a, b) :: v) = (a :: pr₁ (unzip v), b :: pr₂ (unzip v)) := rfl definition zip : Π {n : nat}, vector A n → vector B n → vector (A × B) n | zip [] [] := [] | zip (a::va) (b::vb) := ((a, b) :: zip va vb) theorem zip_nil_nil : zip (@nil A) (@nil B) = nil := rfl theorem zip_cons_cons {n : nat} (a : A) (b : B) (va : vector A n) (vb : vector B n) : zip (a::va) (b::vb) = ((a, b) :: zip va vb) := rfl theorem unzip_zip : ∀ {n : nat} (v₁ : vector A n) (v₂ : vector B n), unzip (zip v₁ v₂) = (v₁, v₂) | 0 [] [] := rfl | (n+1) (a::va) (b::vb) := calc unzip (zip (a :: va) (b :: vb)) = (a :: pr₁ (unzip (zip va vb)), b :: pr₂ (unzip (zip va vb))) : rfl ... = (a :: pr₁ (va, vb), b :: pr₂ (va, vb)) : by rewrite unzip_zip ... = (a :: va, b :: vb) : rfl theorem zip_unzip : ∀ {n : nat} (v : vector (A × B) n), zip (pr₁ (unzip v)) (pr₂ (unzip v)) = v | 0 [] := rfl | (n+1) ((a, b) :: v) := calc zip (pr₁ (unzip ((a, b) :: v))) (pr₂ (unzip ((a, b) :: v))) = (a, b) :: zip (pr₁ (unzip v)) (pr₂ (unzip v)) : rfl ... = (a, b) :: v : by rewrite zip_unzip /- Concat -/ definition concat : Π {n : nat}, vector A n → A → vector A (succ n) | concat [] a := [a] | concat (b::v) a := b :: concat v a theorem concat_nil (a : A) : concat [] a = [a] := rfl theorem concat_cons {n : nat} (b : A) (v : vector A n) (a : A) : concat (b :: v) a = b :: concat v a := rfl theorem last_concat : ∀ {n : nat} (v : vector A n) (a : A), last (concat v a) = a | 0 [] a := rfl | (n+1) (b::v) a := calc last (concat (b::v) a) = last (concat v a) : rfl ... = a : last_concat v a /- Reverse -/ definition reverse : Π {n : nat}, vector A n → vector A n | 0 [] := [] | (n+1) (x :: xs) := concat (reverse xs) x theorem reverse_concat : Π {n : nat} (xs : vector A n) (a : A), reverse (concat xs a) = a :: reverse xs | 0 [] a := rfl | (n+1) (x :: xs) a := begin change (concat (reverse (concat xs a)) x = a :: reverse (x :: xs)), rewrite reverse_concat end theorem reverse_reverse : Π {n : nat} (xs : vector A n), reverse (reverse xs) = xs | 0 [] := rfl | (n+1) (x :: xs) := begin change (reverse (concat (reverse xs) x) = x :: xs), rewrite [reverse_concat, reverse_reverse] end /- list <-> vector -/ definition of_list : Π (l : list A), vector A (list.length l) | list.nil := [] | (list.cons a l) := a :: (of_list l) definition to_list : Π {n : nat}, vector A n → list A | 0 [] := list.nil | (n+1) (a :: vs) := list.cons a (to_list vs) theorem to_list_of_list : ∀ (l : list A), to_list (of_list l) = l | list.nil := rfl | (list.cons a l) := begin change (list.cons a (to_list (of_list l)) = list.cons a l), rewrite to_list_of_list end theorem to_list_nil : to_list [] = (list.nil : list A) := rfl theorem length_to_list : ∀ {n : nat} (v : vector A n), list.length (to_list v) = n | 0 [] := rfl | (n+1) (a :: vs) := begin change (succ (list.length (to_list vs)) = succ n), rewrite length_to_list end theorem heq_of_list_eq : ∀ {n m} {v₁ : vector A n} {v₂ : vector A m}, to_list v₁ = to_list v₂ → n = m → v₁ == v₂ | 0 0 [] [] h₁ h₂ := !heq.refl | 0 (m+1) [] (y::ys) h₁ h₂ := by contradiction | (n+1) 0 (x::xs) [] h₁ h₂ := by contradiction | (n+1) (m+1) (x::xs) (y::ys) h₁ h₂ := assert e₁ : n = m, from succ.inj h₂, assert e₂ : x = y, begin unfold to_list at h₁, injection h₁, assumption end, have to_list xs = to_list ys, begin unfold to_list at h₁, injection h₁, assumption end, assert xs == ys, from heq_of_list_eq this e₁, assert y :: xs == y :: ys, begin clear heq_of_list_eq h₁ h₂, revert xs ys this, induction e₁, intro xs ys h, rewrite [eq_of_heq h] end, show x :: xs == y :: ys, by rewrite e₂; exact this theorem list_eq_of_heq {n m} {v₁ : vector A n} {v₂ : vector A m} : v₁ == v₂ → n = m → to_list v₁ = to_list v₂ := begin intro h₁ h₂, revert v₁ v₂ h₁, subst n, intro v₁ v₂ h₁, rewrite [eq_of_heq h₁] end theorem of_list_to_list {n : nat} (v : vector A n) : of_list (to_list v) == v := begin apply heq_of_list_eq, rewrite to_list_of_list, rewrite length_to_list end theorem to_list_append : ∀ {n m : nat} (v₁ : vector A n) (v₂ : vector A m), to_list (append v₁ v₂) = list.append (to_list v₁) (to_list v₂) | 0 m [] ys := rfl | (succ n) m (x::xs) ys := begin unfold append, unfold to_list at {1,2}, krewrite [to_list_append xs ys] end theorem to_list_map (f : A → B) : ∀ {n : nat} (v : vector A n), to_list (map f v) = list.map f (to_list v) | 0 [] := rfl | (succ n) (x::xs) := begin unfold [map, to_list], rewrite to_list_map end theorem to_list_concat : ∀ {n : nat} (v : vector A n) (a : A), to_list (concat v a) = list.concat a (to_list v) | 0 [] a := rfl | (succ n) (x::xs) a := begin unfold [concat, to_list], rewrite to_list_concat end theorem to_list_reverse : ∀ {n : nat} (v : vector A n), to_list (reverse v) = list.reverse (to_list v) | 0 [] := rfl | (succ n) (x::xs) := begin unfold [reverse], rewrite [to_list_concat, to_list_reverse] end theorem append_nil_right {n : nat} (v : vector A n) : append v [] == v := begin apply heq_of_list_eq, rewrite [to_list_append, to_list_nil, list.append_nil_right], rewrite [-add_eq_addl] end theorem append.assoc {n₁ n₂ n₃ : nat} (v₁ : vector A n₁) (v₂ : vector A n₂) (v₃ : vector A n₃) : append v₁ (append v₂ v₃) == append (append v₁ v₂) v₃ := begin apply heq_of_list_eq, rewrite [*to_list_append, list.append.assoc], rewrite [-*add_eq_addl, add.assoc] end theorem reverse_append {n m : nat} (v : vector A n) (w : vector A m) : reverse (append v w) == append (reverse w) (reverse v) := begin apply heq_of_list_eq, rewrite [to_list_reverse, to_list_append, list.reverse_append, to_list_append, *to_list_reverse], rewrite [-*add_eq_addl, add.comm] end theorem concat_eq_append {n : nat} (v : vector A n) (a : A) : concat v a == append v [a] := begin apply heq_of_list_eq, rewrite [to_list_concat, to_list_append, list.concat_eq_append], rewrite [-add_eq_addl] end /- decidable equality -/ open decidable definition decidable_eq [H : decidable_eq A] : ∀ {n : nat} (v₁ v₂ : vector A n), decidable (v₁ = v₂) | ⌞0⌟ [] [] := by left; reflexivity | ⌞n+1⌟ (a::v₁) (b::v₂) := match H a b with | inl Hab := match decidable_eq v₁ v₂ with | inl He := by left; congruence; repeat assumption | inr Hn := by right; intro h; injection h; contradiction end | inr Hnab := by right; intro h; injection h; contradiction end section open equiv function definition vector_equiv_of_equiv {A B : Type} : A ≃ B → ∀ n, vector A n ≃ vector B n | (mk f g l r) n := mk (map f) (map g) begin intros, rewrite [map_map, id_of_left_inverse l, map_id], reflexivity end begin intros, rewrite [map_map, id_of_right_inverse r, map_id], reflexivity end end end vector
96ab5475a64e55d35a4f92c38e9eb28134bf7610
592ee40978ac7604005a4e0d35bbc4b467389241
/Library/generated/mathscheme-lean/RightRack.lean
e2fbcca3acf4453613a56847a5f724d33cd2c148
[]
no_license
ysharoda/Deriving-Definitions
3e149e6641fae440badd35ac110a0bd705a49ad2
dfecb27572022de3d4aa702cae8db19957523a59
refs/heads/master
1,679,127,857,700
1,615,939,007,000
1,615,939,007,000
229,785,731
4
0
null
null
null
null
UTF-8
Lean
false
false
8,505
lean
import init.data.nat.basic import init.data.fin.basic import data.vector import .Prelude open Staged open nat open fin open vector section RightRack structure RightRack (A : Type) : Type := (rinv : (A → (A → A))) (linv : (A → (A → A))) (rightDistributive : (∀ {x y z : A} , (rinv (rinv y z) x) = (rinv (rinv y x) (rinv z x)))) open RightRack structure Sig (AS : Type) : Type := (rinvS : (AS → (AS → AS))) (linvS : (AS → (AS → AS))) structure Product (A : Type) : Type := (rinvP : ((Prod A A) → ((Prod A A) → (Prod A A)))) (linvP : ((Prod A A) → ((Prod A A) → (Prod A A)))) (rightDistributiveP : (∀ {xP yP zP : (Prod A A)} , (rinvP (rinvP yP zP) xP) = (rinvP (rinvP yP xP) (rinvP zP xP)))) structure Hom {A1 : Type} {A2 : Type} (Ri1 : (RightRack A1)) (Ri2 : (RightRack A2)) : Type := (hom : (A1 → A2)) (pres_rinv : (∀ {x1 x2 : A1} , (hom ((rinv Ri1) x1 x2)) = ((rinv Ri2) (hom x1) (hom x2)))) (pres_linv : (∀ {x1 x2 : A1} , (hom ((linv Ri1) x1 x2)) = ((linv Ri2) (hom x1) (hom x2)))) structure RelInterp {A1 : Type} {A2 : Type} (Ri1 : (RightRack A1)) (Ri2 : (RightRack A2)) : Type 1 := (interp : (A1 → (A2 → Type))) (interp_rinv : (∀ {x1 x2 : A1} {y1 y2 : A2} , ((interp x1 y1) → ((interp x2 y2) → (interp ((rinv Ri1) x1 x2) ((rinv Ri2) y1 y2)))))) (interp_linv : (∀ {x1 x2 : A1} {y1 y2 : A2} , ((interp x1 y1) → ((interp x2 y2) → (interp ((linv Ri1) x1 x2) ((linv Ri2) y1 y2)))))) inductive RightRackTerm : Type | rinvL : (RightRackTerm → (RightRackTerm → RightRackTerm)) | linvL : (RightRackTerm → (RightRackTerm → RightRackTerm)) open RightRackTerm inductive ClRightRackTerm (A : Type) : Type | sing : (A → ClRightRackTerm) | rinvCl : (ClRightRackTerm → (ClRightRackTerm → ClRightRackTerm)) | linvCl : (ClRightRackTerm → (ClRightRackTerm → ClRightRackTerm)) open ClRightRackTerm inductive OpRightRackTerm (n : ℕ) : Type | v : ((fin n) → OpRightRackTerm) | rinvOL : (OpRightRackTerm → (OpRightRackTerm → OpRightRackTerm)) | linvOL : (OpRightRackTerm → (OpRightRackTerm → OpRightRackTerm)) open OpRightRackTerm inductive OpRightRackTerm2 (n : ℕ) (A : Type) : Type | v2 : ((fin n) → OpRightRackTerm2) | sing2 : (A → OpRightRackTerm2) | rinvOL2 : (OpRightRackTerm2 → (OpRightRackTerm2 → OpRightRackTerm2)) | linvOL2 : (OpRightRackTerm2 → (OpRightRackTerm2 → OpRightRackTerm2)) open OpRightRackTerm2 def simplifyCl {A : Type} : ((ClRightRackTerm A) → (ClRightRackTerm A)) | (rinvCl x1 x2) := (rinvCl (simplifyCl x1) (simplifyCl x2)) | (linvCl x1 x2) := (linvCl (simplifyCl x1) (simplifyCl x2)) | (sing x1) := (sing x1) def simplifyOpB {n : ℕ} : ((OpRightRackTerm n) → (OpRightRackTerm n)) | (rinvOL x1 x2) := (rinvOL (simplifyOpB x1) (simplifyOpB x2)) | (linvOL x1 x2) := (linvOL (simplifyOpB x1) (simplifyOpB x2)) | (v x1) := (v x1) def simplifyOp {n : ℕ} {A : Type} : ((OpRightRackTerm2 n A) → (OpRightRackTerm2 n A)) | (rinvOL2 x1 x2) := (rinvOL2 (simplifyOp x1) (simplifyOp x2)) | (linvOL2 x1 x2) := (linvOL2 (simplifyOp x1) (simplifyOp x2)) | (v2 x1) := (v2 x1) | (sing2 x1) := (sing2 x1) def evalB {A : Type} : ((RightRack A) → (RightRackTerm → A)) | Ri (rinvL x1 x2) := ((rinv Ri) (evalB Ri x1) (evalB Ri x2)) | Ri (linvL x1 x2) := ((linv Ri) (evalB Ri x1) (evalB Ri x2)) def evalCl {A : Type} : ((RightRack A) → ((ClRightRackTerm A) → A)) | Ri (sing x1) := x1 | Ri (rinvCl x1 x2) := ((rinv Ri) (evalCl Ri x1) (evalCl Ri x2)) | Ri (linvCl x1 x2) := ((linv Ri) (evalCl Ri x1) (evalCl Ri x2)) def evalOpB {A : Type} {n : ℕ} : ((RightRack A) → ((vector A n) → ((OpRightRackTerm n) → A))) | Ri vars (v x1) := (nth vars x1) | Ri vars (rinvOL x1 x2) := ((rinv Ri) (evalOpB Ri vars x1) (evalOpB Ri vars x2)) | Ri vars (linvOL x1 x2) := ((linv Ri) (evalOpB Ri vars x1) (evalOpB Ri vars x2)) def evalOp {A : Type} {n : ℕ} : ((RightRack A) → ((vector A n) → ((OpRightRackTerm2 n A) → A))) | Ri vars (v2 x1) := (nth vars x1) | Ri vars (sing2 x1) := x1 | Ri vars (rinvOL2 x1 x2) := ((rinv Ri) (evalOp Ri vars x1) (evalOp Ri vars x2)) | Ri vars (linvOL2 x1 x2) := ((linv Ri) (evalOp Ri vars x1) (evalOp Ri vars x2)) def inductionB {P : (RightRackTerm → Type)} : ((∀ (x1 x2 : RightRackTerm) , ((P x1) → ((P x2) → (P (rinvL x1 x2))))) → ((∀ (x1 x2 : RightRackTerm) , ((P x1) → ((P x2) → (P (linvL x1 x2))))) → (∀ (x : RightRackTerm) , (P x)))) | prinvl plinvl (rinvL x1 x2) := (prinvl _ _ (inductionB prinvl plinvl x1) (inductionB prinvl plinvl x2)) | prinvl plinvl (linvL x1 x2) := (plinvl _ _ (inductionB prinvl plinvl x1) (inductionB prinvl plinvl x2)) def inductionCl {A : Type} {P : ((ClRightRackTerm A) → Type)} : ((∀ (x1 : A) , (P (sing x1))) → ((∀ (x1 x2 : (ClRightRackTerm A)) , ((P x1) → ((P x2) → (P (rinvCl x1 x2))))) → ((∀ (x1 x2 : (ClRightRackTerm A)) , ((P x1) → ((P x2) → (P (linvCl x1 x2))))) → (∀ (x : (ClRightRackTerm A)) , (P x))))) | psing prinvcl plinvcl (sing x1) := (psing x1) | psing prinvcl plinvcl (rinvCl x1 x2) := (prinvcl _ _ (inductionCl psing prinvcl plinvcl x1) (inductionCl psing prinvcl plinvcl x2)) | psing prinvcl plinvcl (linvCl x1 x2) := (plinvcl _ _ (inductionCl psing prinvcl plinvcl x1) (inductionCl psing prinvcl plinvcl x2)) def inductionOpB {n : ℕ} {P : ((OpRightRackTerm n) → Type)} : ((∀ (fin : (fin n)) , (P (v fin))) → ((∀ (x1 x2 : (OpRightRackTerm n)) , ((P x1) → ((P x2) → (P (rinvOL x1 x2))))) → ((∀ (x1 x2 : (OpRightRackTerm n)) , ((P x1) → ((P x2) → (P (linvOL x1 x2))))) → (∀ (x : (OpRightRackTerm n)) , (P x))))) | pv prinvol plinvol (v x1) := (pv x1) | pv prinvol plinvol (rinvOL x1 x2) := (prinvol _ _ (inductionOpB pv prinvol plinvol x1) (inductionOpB pv prinvol plinvol x2)) | pv prinvol plinvol (linvOL x1 x2) := (plinvol _ _ (inductionOpB pv prinvol plinvol x1) (inductionOpB pv prinvol plinvol x2)) def inductionOp {n : ℕ} {A : Type} {P : ((OpRightRackTerm2 n A) → Type)} : ((∀ (fin : (fin n)) , (P (v2 fin))) → ((∀ (x1 : A) , (P (sing2 x1))) → ((∀ (x1 x2 : (OpRightRackTerm2 n A)) , ((P x1) → ((P x2) → (P (rinvOL2 x1 x2))))) → ((∀ (x1 x2 : (OpRightRackTerm2 n A)) , ((P x1) → ((P x2) → (P (linvOL2 x1 x2))))) → (∀ (x : (OpRightRackTerm2 n A)) , (P x)))))) | pv2 psing2 prinvol2 plinvol2 (v2 x1) := (pv2 x1) | pv2 psing2 prinvol2 plinvol2 (sing2 x1) := (psing2 x1) | pv2 psing2 prinvol2 plinvol2 (rinvOL2 x1 x2) := (prinvol2 _ _ (inductionOp pv2 psing2 prinvol2 plinvol2 x1) (inductionOp pv2 psing2 prinvol2 plinvol2 x2)) | pv2 psing2 prinvol2 plinvol2 (linvOL2 x1 x2) := (plinvol2 _ _ (inductionOp pv2 psing2 prinvol2 plinvol2 x1) (inductionOp pv2 psing2 prinvol2 plinvol2 x2)) def stageB : (RightRackTerm → (Staged RightRackTerm)) | (rinvL x1 x2) := (stage2 rinvL (codeLift2 rinvL) (stageB x1) (stageB x2)) | (linvL x1 x2) := (stage2 linvL (codeLift2 linvL) (stageB x1) (stageB x2)) def stageCl {A : Type} : ((ClRightRackTerm A) → (Staged (ClRightRackTerm A))) | (sing x1) := (Now (sing x1)) | (rinvCl x1 x2) := (stage2 rinvCl (codeLift2 rinvCl) (stageCl x1) (stageCl x2)) | (linvCl x1 x2) := (stage2 linvCl (codeLift2 linvCl) (stageCl x1) (stageCl x2)) def stageOpB {n : ℕ} : ((OpRightRackTerm n) → (Staged (OpRightRackTerm n))) | (v x1) := (const (code (v x1))) | (rinvOL x1 x2) := (stage2 rinvOL (codeLift2 rinvOL) (stageOpB x1) (stageOpB x2)) | (linvOL x1 x2) := (stage2 linvOL (codeLift2 linvOL) (stageOpB x1) (stageOpB x2)) def stageOp {n : ℕ} {A : Type} : ((OpRightRackTerm2 n A) → (Staged (OpRightRackTerm2 n A))) | (sing2 x1) := (Now (sing2 x1)) | (v2 x1) := (const (code (v2 x1))) | (rinvOL2 x1 x2) := (stage2 rinvOL2 (codeLift2 rinvOL2) (stageOp x1) (stageOp x2)) | (linvOL2 x1 x2) := (stage2 linvOL2 (codeLift2 linvOL2) (stageOp x1) (stageOp x2)) structure StagedRepr (A : Type) (Repr : (Type → Type)) : Type := (rinvT : ((Repr A) → ((Repr A) → (Repr A)))) (linvT : ((Repr A) → ((Repr A) → (Repr A)))) end RightRack
5636f23aa9b25af67a38c1eca3a8b44fead25c72
9028d228ac200bbefe3a711342514dd4e4458bff
/src/data/complex/exponential.lean
144fc733d22601777277b59b022d8332715cafe3
[ "Apache-2.0" ]
permissive
mcncm/mathlib
8d25099344d9d2bee62822cb9ed43aa3e09fa05e
fde3d78cadeec5ef827b16ae55664ef115e66f57
refs/heads/master
1,672,743,316,277
1,602,618,514,000
1,602,618,514,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
53,845
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Abhimanyu Pallavi Sudhir -/ import algebra.geom_sum import data.nat.choose.sum import data.complex.basic /-! # Exponential, trigonometric and hyperbolic trigonometric functions This file contains the definitions of the real and complex exponential, sine, cosine, tangent, hyperbolic sine, hyperbolic cosine, and hyperbolic tangent functions. -/ local notation `abs'` := _root_.abs open is_absolute_value open_locale classical big_operators nat section open real is_absolute_value finset lemma forall_ge_le_of_forall_le_succ {α : Type*} [preorder α] (f : ℕ → α) {m : ℕ} (h : ∀ n ≥ m, f n.succ ≤ f n) : ∀ {l}, ∀ k ≥ m, k ≤ l → f l ≤ f k := begin assume l k hkm hkl, generalize hp : l - k = p, have : l = k + p := add_comm p k ▸ (nat.sub_eq_iff_eq_add hkl).1 hp, subst this, clear hkl hp, induction p with p ih, { simp }, { exact le_trans (h _ (le_trans hkm (nat.le_add_right _ _))) ih } end section variables {α : Type*} {β : Type*} [ring β] [discrete_linear_ordered_field α] [archimedean α] {abv : β → α} [is_absolute_value abv] lemma is_cau_of_decreasing_bounded (f : ℕ → α) {a : α} {m : ℕ} (ham : ∀ n ≥ m, abs (f n) ≤ a) (hnm : ∀ n ≥ m, f n.succ ≤ f n) : is_cau_seq abs f := λ ε ε0, let ⟨k, hk⟩ := archimedean.arch a ε0 in have h : ∃ l, ∀ n ≥ m, a - l •ℕ ε < f n := ⟨k + k + 1, λ n hnm, lt_of_lt_of_le (show a - (k + (k + 1)) •ℕ ε < -abs (f n), from lt_neg.1 $ lt_of_le_of_lt (ham n hnm) (begin rw [neg_sub, lt_sub_iff_add_lt, add_nsmul], exact add_lt_add_of_le_of_lt hk (lt_of_le_of_lt hk (lt_add_of_pos_left _ ε0)), end)) (neg_le.2 $ (abs_neg (f n)) ▸ le_abs_self _)⟩, let l := nat.find h in have hl : ∀ (n : ℕ), n ≥ m → f n > a - l •ℕ ε := nat.find_spec h, have hl0 : l ≠ 0 := λ hl0, not_lt_of_ge (ham m (le_refl _)) (lt_of_lt_of_le (by have := hl m (le_refl m); simpa [hl0] using this) (le_abs_self (f m))), begin cases not_forall.1 (nat.find_min h (nat.pred_lt hl0)) with i hi, rw [not_imp, not_lt] at hi, existsi i, assume j hj, have hfij : f j ≤ f i := forall_ge_le_of_forall_le_succ f hnm _ hi.1 hj, rw [abs_of_nonpos (sub_nonpos.2 hfij), neg_sub, sub_lt_iff_lt_add'], exact calc f i ≤ a - (nat.pred l) •ℕ ε : hi.2 ... = a - l •ℕ ε + ε : by conv {to_rhs, rw [← nat.succ_pred_eq_of_pos (nat.pos_of_ne_zero hl0), succ_nsmul', sub_add, add_sub_cancel] } ... < f j + ε : add_lt_add_right (hl j (le_trans hi.1 hj)) _ end lemma is_cau_of_mono_bounded (f : ℕ → α) {a : α} {m : ℕ} (ham : ∀ n ≥ m, abs (f n) ≤ a) (hnm : ∀ n ≥ m, f n ≤ f n.succ) : is_cau_seq abs f := begin refine @eq.rec_on (ℕ → α) _ (is_cau_seq abs) _ _ (-⟨_, @is_cau_of_decreasing_bounded _ _ _ (λ n, -f n) a m (by simpa) (by simpa)⟩ : cau_seq α abs).2, ext, exact neg_neg _ end end section no_archimedean variables {α : Type*} {β : Type*} [ring β] [discrete_linear_ordered_field α] {abv : β → α} [is_absolute_value abv] lemma is_cau_series_of_abv_le_cau {f : ℕ → β} {g : ℕ → α} (n : ℕ) : (∀ m, n ≤ m → abv (f m) ≤ g m) → is_cau_seq abs (λ n, ∑ i in range n, g i) → is_cau_seq abv (λ n, ∑ i in range n, f i) := begin assume hm hg ε ε0, cases hg (ε / 2) (div_pos ε0 (by norm_num)) with i hi, existsi max n i, assume j ji, have hi₁ := hi j (le_trans (le_max_right n i) ji), have hi₂ := hi (max n i) (le_max_right n i), have sub_le := abs_sub_le (∑ k in range j, g k) (∑ k in range i, g k) (∑ k in range (max n i), g k), have := add_lt_add hi₁ hi₂, rw [abs_sub (∑ k in range (max n i), g k), add_halves ε] at this, refine lt_of_le_of_lt (le_trans (le_trans _ (le_abs_self _)) sub_le) this, generalize hk : j - max n i = k, clear this hi₂ hi₁ hi ε0 ε hg sub_le, rw nat.sub_eq_iff_eq_add ji at hk, rw hk, clear hk ji j, induction k with k' hi, { simp [abv_zero abv] }, { dsimp at *, simp only [nat.succ_add, sum_range_succ, sub_eq_add_neg, add_assoc], refine le_trans (abv_add _ _ _) _, exact add_le_add (hm _ (le_add_of_nonneg_of_le (nat.zero_le _) (le_max_left _ _))) hi }, end lemma is_cau_series_of_abv_cau {f : ℕ → β} : is_cau_seq abs (λ m, ∑ n in range m, abv (f n)) → is_cau_seq abv (λ m, ∑ n in range m, f n) := is_cau_series_of_abv_le_cau 0 (λ n h, le_refl _) end no_archimedean section variables {α : Type*} {β : Type*} [ring β] [discrete_linear_ordered_field α] [archimedean α] {abv : β → α} [is_absolute_value abv] lemma is_cau_geo_series {β : Type*} [field β] {abv : β → α} [is_absolute_value abv] (x : β) (hx1 : abv x < 1) : is_cau_seq abv (λ n, ∑ m in range n, x ^ m) := have hx1' : abv x ≠ 1 := λ h, by simpa [h, lt_irrefl] using hx1, is_cau_series_of_abv_cau begin simp only [abv_pow abv] {eta := ff}, have : (λ (m : ℕ), ∑ n in range m, (abv x) ^ n) = λ m, geom_series (abv x) m := rfl, simp only [this, geom_sum hx1'] {eta := ff}, conv in (_ / _) { rw [← neg_div_neg_eq, neg_sub, neg_sub] }, refine @is_cau_of_mono_bounded _ _ _ _ ((1 : α) / (1 - abv x)) 0 _ _, { assume n hn, rw abs_of_nonneg, refine div_le_div_of_le (le_of_lt $ sub_pos.2 hx1) (sub_le_self _ (abv_pow abv x n ▸ abv_nonneg _ _)), refine div_nonneg (sub_nonneg.2 _) (sub_nonneg.2 $ le_of_lt hx1), clear hn, induction n with n ih, { simp }, { rw [pow_succ, ← one_mul (1 : α)], refine mul_le_mul (le_of_lt hx1) ih (abv_pow abv x n ▸ abv_nonneg _ _) (by norm_num) } }, { assume n hn, refine div_le_div_of_le (le_of_lt $ sub_pos.2 hx1) (sub_le_sub_left _ _), rw [← one_mul (_ ^ n), pow_succ], exact mul_le_mul_of_nonneg_right (le_of_lt hx1) (pow_nonneg (abv_nonneg _ _) _) } end lemma is_cau_geo_series_const (a : α) {x : α} (hx1 : abs x < 1) : is_cau_seq abs (λ m, ∑ n in range m, a * x ^ n) := have is_cau_seq abs (λ m, a * ∑ n in range m, x ^ n) := (cau_seq.const abs a * ⟨_, is_cau_geo_series x hx1⟩).2, by simpa only [mul_sum] lemma series_ratio_test {f : ℕ → β} (n : ℕ) (r : α) (hr0 : 0 ≤ r) (hr1 : r < 1) (h : ∀ m, n ≤ m → abv (f m.succ) ≤ r * abv (f m)) : is_cau_seq abv (λ m, ∑ n in range m, f n) := have har1 : abs r < 1, by rwa abs_of_nonneg hr0, begin refine is_cau_series_of_abv_le_cau n.succ _ (is_cau_geo_series_const (abv (f n.succ) * r⁻¹ ^ n.succ) har1), assume m hmn, cases classical.em (r = 0) with r_zero r_ne_zero, { have m_pos := lt_of_lt_of_le (nat.succ_pos n) hmn, have := h m.pred (nat.le_of_succ_le_succ (by rwa [nat.succ_pred_eq_of_pos m_pos])), simpa [r_zero, nat.succ_pred_eq_of_pos m_pos, pow_succ] }, generalize hk : m - n.succ = k, have r_pos : 0 < r := lt_of_le_of_ne hr0 (ne.symm r_ne_zero), replace hk : m = k + n.succ := (nat.sub_eq_iff_eq_add hmn).1 hk, induction k with k ih generalizing m n, { rw [hk, zero_add, mul_right_comm, inv_pow' _ _, ← div_eq_mul_inv, mul_div_cancel], exact (ne_of_lt (pow_pos r_pos _)).symm }, { have kn : k + n.succ ≥ n.succ, by rw ← zero_add n.succ; exact add_le_add (zero_le _) (by simp), rw [hk, nat.succ_add, pow_succ' r, ← mul_assoc], exact le_trans (by rw mul_comm; exact h _ (nat.le_of_succ_le kn)) (mul_le_mul_of_nonneg_right (ih (k + n.succ) n h kn rfl) hr0) } end lemma sum_range_diag_flip {α : Type*} [add_comm_monoid α] (n : ℕ) (f : ℕ → ℕ → α) : ∑ m in range n, ∑ k in range (m + 1), f k (m - k) = ∑ m in range n, ∑ k in range (n - m), f m k := have h₁ : ∑ a in (range n).sigma (range ∘ nat.succ), f (a.2) (a.1 - a.2) = ∑ m in range n, ∑ k in range (m + 1), f k (m - k) := sum_sigma, have h₂ : ∑ a in (range n).sigma (λ m, range (n - m)), f (a.1) (a.2) = ∑ m in range n, ∑ k in range (n - m), f m k := sum_sigma, h₁ ▸ h₂ ▸ sum_bij (λ a _, ⟨a.2, a.1 - a.2⟩) (λ a ha, have h₁ : a.1 < n := mem_range.1 (mem_sigma.1 ha).1, have h₂ : a.2 < nat.succ a.1 := mem_range.1 (mem_sigma.1 ha).2, mem_sigma.2 ⟨mem_range.2 (lt_of_lt_of_le h₂ h₁), mem_range.2 ((nat.sub_lt_sub_right_iff (nat.le_of_lt_succ h₂)).2 h₁)⟩) (λ _ _, rfl) (λ ⟨a₁, a₂⟩ ⟨b₁, b₂⟩ ha hb h, have ha : a₁ < n ∧ a₂ ≤ a₁ := ⟨mem_range.1 (mem_sigma.1 ha).1, nat.le_of_lt_succ (mem_range.1 (mem_sigma.1 ha).2)⟩, have hb : b₁ < n ∧ b₂ ≤ b₁ := ⟨mem_range.1 (mem_sigma.1 hb).1, nat.le_of_lt_succ (mem_range.1 (mem_sigma.1 hb).2)⟩, have h : a₂ = b₂ ∧ _ := sigma.mk.inj h, have h' : a₁ = b₁ - b₂ + a₂ := (nat.sub_eq_iff_eq_add ha.2).1 (eq_of_heq h.2), sigma.mk.inj_iff.2 ⟨nat.sub_add_cancel hb.2 ▸ h'.symm ▸ h.1 ▸ rfl, (heq_of_eq h.1)⟩) (λ ⟨a₁, a₂⟩ ha, have ha : a₁ < n ∧ a₂ < n - a₁ := ⟨mem_range.1 (mem_sigma.1 ha).1, (mem_range.1 (mem_sigma.1 ha).2)⟩, ⟨⟨a₂ + a₁, a₁⟩, ⟨mem_sigma.2 ⟨mem_range.2 (nat.lt_sub_right_iff_add_lt.1 ha.2), mem_range.2 (nat.lt_succ_of_le (nat.le_add_left _ _))⟩, sigma.mk.inj_iff.2 ⟨rfl, heq_of_eq (nat.add_sub_cancel _ _).symm⟩⟩⟩) lemma sum_range_sub_sum_range {α : Type*} [add_comm_group α] {f : ℕ → α} {n m : ℕ} (hnm : n ≤ m) : ∑ k in range m, f k - ∑ k in range n, f k = ∑ k in (range m).filter (λ k, n ≤ k), f k := begin rw [← sum_sdiff (@filter_subset _ (λ k, n ≤ k) _ (range m)), sub_eq_iff_eq_add, ← eq_sub_iff_add_eq, add_sub_cancel'], refine finset.sum_congr (finset.ext $ λ a, ⟨λ h, by simp at *; finish, λ h, have ham : a < m := lt_of_lt_of_le (mem_range.1 h) hnm, by simp * at *⟩) (λ _ _, rfl), end end section no_archimedean variables {α : Type*} {β : Type*} [ring β] [discrete_linear_ordered_field α] {abv : β → α} [is_absolute_value abv] lemma abv_sum_le_sum_abv {γ : Type*} (f : γ → β) (s : finset γ) : abv (∑ k in s, f k) ≤ ∑ k in s, abv (f k) := by haveI := classical.dec_eq γ; exact finset.induction_on s (by simp [abv_zero abv]) (λ a s has ih, by rw [sum_insert has, sum_insert has]; exact le_trans (abv_add abv _ _) (add_le_add_left ih _)) lemma cauchy_product {a b : ℕ → β} (ha : is_cau_seq abs (λ m, ∑ n in range m, abv (a n))) (hb : is_cau_seq abv (λ m, ∑ n in range m, b n)) (ε : α) (ε0 : 0 < ε) : ∃ i : ℕ, ∀ j ≥ i, abv ((∑ k in range j, a k) * (∑ k in range j, b k) - ∑ n in range j, ∑ m in range (n + 1), a m * b (n - m)) < ε := let ⟨Q, hQ⟩ := cau_seq.bounded ⟨_, hb⟩ in let ⟨P, hP⟩ := cau_seq.bounded ⟨_, ha⟩ in have hP0 : 0 < P, from lt_of_le_of_lt (abs_nonneg _) (hP 0), have hPε0 : 0 < ε / (2 * P), from div_pos ε0 (mul_pos (show (2 : α) > 0, from by norm_num) hP0), let ⟨N, hN⟩ := cau_seq.cauchy₂ ⟨_, hb⟩ hPε0 in have hQε0 : 0 < ε / (4 * Q), from div_pos ε0 (mul_pos (show (0 : α) < 4, by norm_num) (lt_of_le_of_lt (abv_nonneg _ _) (hQ 0))), let ⟨M, hM⟩ := cau_seq.cauchy₂ ⟨_, ha⟩ hQε0 in ⟨2 * (max N M + 1), λ K hK, have h₁ : ∑ m in range K, ∑ k in range (m + 1), a k * b (m - k) = ∑ m in range K, ∑ n in range (K - m), a m * b n, by simpa using sum_range_diag_flip K (λ m n, a m * b n), have h₂ : (λ i, ∑ k in range (K - i), a i * b k) = (λ i, a i * ∑ k in range (K - i), b k), by simp [finset.mul_sum], have h₃ : ∑ i in range K, a i * ∑ k in range (K - i), b k = ∑ i in range K, a i * (∑ k in range (K - i), b k - ∑ k in range K, b k) + ∑ i in range K, a i * ∑ k in range K, b k, by rw ← sum_add_distrib; simp [(mul_add _ _ _).symm], have two_mul_two : (4 : α) = 2 * 2, by norm_num, have hQ0 : Q ≠ 0, from λ h, by simpa [h, lt_irrefl] using hQε0, have h2Q0 : 2 * Q ≠ 0, from mul_ne_zero two_ne_zero hQ0, have hε : ε / (2 * P) * P + ε / (4 * Q) * (2 * Q) = ε, by rw [← div_div_eq_div_mul, div_mul_cancel _ (ne.symm (ne_of_lt hP0)), two_mul_two, mul_assoc, ← div_div_eq_div_mul, div_mul_cancel _ h2Q0, add_halves], have hNMK : max N M + 1 < K, from lt_of_lt_of_le (by rw two_mul; exact lt_add_of_pos_left _ (nat.succ_pos _)) hK, have hKN : N < K, from calc N ≤ max N M : le_max_left _ _ ... < max N M + 1 : nat.lt_succ_self _ ... < K : hNMK, have hsumlesum : ∑ i in range (max N M + 1), abv (a i) * abv (∑ k in range (K - i), b k - ∑ k in range K, b k) ≤ ∑ i in range (max N M + 1), abv (a i) * (ε / (2 * P)), from sum_le_sum (λ m hmJ, mul_le_mul_of_nonneg_left (le_of_lt (hN (K - m) K (nat.le_sub_left_of_add_le (le_trans (by rw two_mul; exact add_le_add (le_of_lt (mem_range.1 hmJ)) (le_trans (le_max_left _ _) (le_of_lt (lt_add_one _)))) hK)) (le_of_lt hKN))) (abv_nonneg abv _)), have hsumltP : ∑ n in range (max N M + 1), abv (a n) < P := calc ∑ n in range (max N M + 1), abv (a n) = abs (∑ n in range (max N M + 1), abv (a n)) : eq.symm (abs_of_nonneg (sum_nonneg (λ x h, abv_nonneg abv (a x)))) ... < P : hP (max N M + 1), begin rw [h₁, h₂, h₃, sum_mul, ← sub_sub, sub_right_comm, sub_self, zero_sub, abv_neg abv], refine lt_of_le_of_lt (abv_sum_le_sum_abv _ _) _, suffices : ∑ i in range (max N M + 1), abv (a i) * abv (∑ k in range (K - i), b k - ∑ k in range K, b k) + (∑ i in range K, abv (a i) * abv (∑ k in range (K - i), b k - ∑ k in range K, b k) - ∑ i in range (max N M + 1), abv (a i) * abv (∑ k in range (K - i), b k - ∑ k in range K, b k)) < ε / (2 * P) * P + ε / (4 * Q) * (2 * Q), { rw hε at this, simpa [abv_mul abv] }, refine add_lt_add (lt_of_le_of_lt hsumlesum (by rw [← sum_mul, mul_comm]; exact (mul_lt_mul_left hPε0).mpr hsumltP)) _, rw sum_range_sub_sum_range (le_of_lt hNMK), exact calc ∑ i in (range K).filter (λ k, max N M + 1 ≤ k), abv (a i) * abv (∑ k in range (K - i), b k - ∑ k in range K, b k) ≤ ∑ i in (range K).filter (λ k, max N M + 1 ≤ k), abv (a i) * (2 * Q) : sum_le_sum (λ n hn, begin refine mul_le_mul_of_nonneg_left _ (abv_nonneg _ _), rw sub_eq_add_neg, refine le_trans (abv_add _ _ _) _, rw [two_mul, abv_neg abv], exact add_le_add (le_of_lt (hQ _)) (le_of_lt (hQ _)), end) ... < ε / (4 * Q) * (2 * Q) : by rw [← sum_mul, ← sum_range_sub_sum_range (le_of_lt hNMK)]; refine (mul_lt_mul_right $ by rw two_mul; exact add_pos (lt_of_le_of_lt (abv_nonneg _ _) (hQ 0)) (lt_of_le_of_lt (abv_nonneg _ _) (hQ 0))).2 (lt_of_le_of_lt (le_abs_self _) (hM _ _ (le_trans (nat.le_succ_of_le (le_max_right _ _)) (le_of_lt hNMK)) (nat.le_succ_of_le (le_max_right _ _)))) end⟩ end no_archimedean end open finset open cau_seq namespace complex lemma is_cau_abs_exp (z : ℂ) : is_cau_seq _root_.abs (λ n, ∑ m in range n, abs (z ^ m / m!)) := let ⟨n, hn⟩ := exists_nat_gt (abs z) in have hn0 : (0 : ℝ) < n, from lt_of_le_of_lt (abs_nonneg _) hn, series_ratio_test n (complex.abs z / n) (div_nonneg (complex.abs_nonneg _) (le_of_lt hn0)) (by rwa [div_lt_iff hn0, one_mul]) (λ m hm, by rw [abs_abs, abs_abs, nat.factorial_succ, pow_succ, mul_comm m.succ, nat.cast_mul, ← div_div_eq_div_mul, mul_div_assoc, mul_div_right_comm, abs_mul, abs_div, abs_cast_nat]; exact mul_le_mul_of_nonneg_right (div_le_div_of_le_left (abs_nonneg _) hn0 (nat.cast_le.2 (le_trans hm (nat.le_succ _)))) (abs_nonneg _)) noncomputable theory lemma is_cau_exp (z : ℂ) : is_cau_seq abs (λ n, ∑ m in range n, z ^ m / m!) := is_cau_series_of_abv_cau (is_cau_abs_exp z) /-- The Cauchy sequence consisting of partial sums of the Taylor series of the complex exponential function -/ @[pp_nodot] def exp' (z : ℂ) : cau_seq ℂ complex.abs := ⟨λ n, ∑ m in range n, z ^ m / m!, is_cau_exp z⟩ /-- The complex exponential function, defined via its Taylor series -/ @[pp_nodot] def exp (z : ℂ) : ℂ := lim (exp' z) /-- The complex sine function, defined via `exp` -/ @[pp_nodot] def sin (z : ℂ) : ℂ := ((exp (-z * I) - exp (z * I)) * I) / 2 /-- The complex cosine function, defined via `exp` -/ @[pp_nodot] def cos (z : ℂ) : ℂ := (exp (z * I) + exp (-z * I)) / 2 /-- The complex tangent function, defined as `sin z / cos z` -/ @[pp_nodot] def tan (z : ℂ) : ℂ := sin z / cos z /-- The complex hyperbolic sine function, defined via `exp` -/ @[pp_nodot] def sinh (z : ℂ) : ℂ := (exp z - exp (-z)) / 2 /-- The complex hyperbolic cosine function, defined via `exp` -/ @[pp_nodot] def cosh (z : ℂ) : ℂ := (exp z + exp (-z)) / 2 /-- The complex hyperbolic tangent function, defined as `sinh z / cosh z` -/ @[pp_nodot] def tanh (z : ℂ) : ℂ := sinh z / cosh z end complex namespace real open complex /-- The real exponential function, defined as the real part of the complex exponential -/ @[pp_nodot] def exp (x : ℝ) : ℝ := (exp x).re /-- The real sine function, defined as the real part of the complex sine -/ @[pp_nodot] def sin (x : ℝ) : ℝ := (sin x).re /-- The real cosine function, defined as the real part of the complex cosine -/ @[pp_nodot] def cos (x : ℝ) : ℝ := (cos x).re /-- The real tangent function, defined as the real part of the complex tangent -/ @[pp_nodot] def tan (x : ℝ) : ℝ := (tan x).re /-- The real hypebolic sine function, defined as the real part of the complex hyperbolic sine -/ @[pp_nodot] def sinh (x : ℝ) : ℝ := (sinh x).re /-- The real hypebolic cosine function, defined as the real part of the complex hyperbolic cosine -/ @[pp_nodot] def cosh (x : ℝ) : ℝ := (cosh x).re /-- The real hypebolic tangent function, defined as the real part of the complex hyperbolic tangent -/ @[pp_nodot] def tanh (x : ℝ) : ℝ := (tanh x).re end real namespace complex variables (x y : ℂ) @[simp] lemma exp_zero : exp 0 = 1 := lim_eq_of_equiv_const $ λ ε ε0, ⟨1, λ j hj, begin convert ε0, cases j, { exact absurd hj (not_le_of_gt zero_lt_one) }, { dsimp [exp'], induction j with j ih, { dsimp [exp']; simp }, { rw ← ih dec_trivial, simp only [sum_range_succ, pow_succ], simp } } end⟩ lemma exp_add : exp (x + y) = exp x * exp y := show lim (⟨_, is_cau_exp (x + y)⟩ : cau_seq ℂ abs) = lim (show cau_seq ℂ abs, from ⟨_, is_cau_exp x⟩) * lim (show cau_seq ℂ abs, from ⟨_, is_cau_exp y⟩), from have hj : ∀ j : ℕ, ∑ m in range j, (x + y) ^ m / m! = ∑ i in range j, ∑ k in range (i + 1), x ^ k / k! * (y ^ (i - k) / (i - k)!), from assume j, finset.sum_congr rfl (λ m hm, begin rw [add_pow, div_eq_mul_inv, sum_mul], refine finset.sum_congr rfl (λ i hi, _), have h₁ : (m.choose i : ℂ) ≠ 0 := nat.cast_ne_zero.2 (nat.pos_iff_ne_zero.1 (nat.choose_pos (nat.le_of_lt_succ (mem_range.1 hi)))), have h₂ := nat.choose_mul_factorial_mul_factorial (nat.le_of_lt_succ $ finset.mem_range.1 hi), rw [← h₂, nat.cast_mul, nat.cast_mul, mul_inv', mul_inv'], simp only [mul_left_comm (m.choose i : ℂ), mul_assoc, mul_left_comm (m.choose i : ℂ)⁻¹, mul_comm (m.choose i : ℂ)], rw inv_mul_cancel h₁, simp [div_eq_mul_inv, mul_comm, mul_assoc, mul_left_comm] end), by rw lim_mul_lim; exact eq.symm (lim_eq_lim_of_equiv (by dsimp; simp only [hj]; exact cauchy_product (is_cau_abs_exp x) (is_cau_exp y))) attribute [irreducible] complex.exp lemma exp_list_sum (l : list ℂ) : exp l.sum = (l.map exp).prod := @monoid_hom.map_list_prod (multiplicative ℂ) ℂ _ _ ⟨exp, exp_zero, exp_add⟩ l lemma exp_multiset_sum (s : multiset ℂ) : exp s.sum = (s.map exp).prod := @monoid_hom.map_multiset_prod (multiplicative ℂ) ℂ _ _ ⟨exp, exp_zero, exp_add⟩ s lemma exp_sum {α : Type*} (s : finset α) (f : α → ℂ) : exp (∑ x in s, f x) = ∏ x in s, exp (f x) := @monoid_hom.map_prod α (multiplicative ℂ) ℂ _ _ ⟨exp, exp_zero, exp_add⟩ f s lemma exp_nat_mul (x : ℂ) : ∀ n : ℕ, exp(n*x) = (exp x)^n | 0 := by rw [nat.cast_zero, zero_mul, exp_zero, pow_zero] | (nat.succ n) := by rw [pow_succ', nat.cast_add_one, add_mul, exp_add, ←exp_nat_mul, one_mul] lemma exp_ne_zero : exp x ≠ 0 := λ h, zero_ne_one $ by rw [← exp_zero, ← add_neg_self x, exp_add, h]; simp lemma exp_neg : exp (-x) = (exp x)⁻¹ := by rw [← mul_right_inj' (exp_ne_zero x), ← exp_add]; simp [mul_inv_cancel (exp_ne_zero x)] lemma exp_sub : exp (x - y) = exp x / exp y := by simp [sub_eq_add_neg, exp_add, exp_neg, div_eq_mul_inv] @[simp] lemma exp_conj : exp (conj x) = conj (exp x) := begin dsimp [exp], rw [← lim_conj], refine congr_arg lim (cau_seq.ext (λ _, _)), dsimp [exp', function.comp, cau_seq_conj], rw conj.map_sum, refine sum_congr rfl (λ n hn, _), rw [conj.map_div, conj.map_pow, ← of_real_nat_cast, conj_of_real] end @[simp] lemma of_real_exp_of_real_re (x : ℝ) : ((exp x).re : ℂ) = exp x := eq_conj_iff_re.1 $ by rw [← exp_conj, conj_of_real] @[simp, norm_cast] lemma of_real_exp (x : ℝ) : (real.exp x : ℂ) = exp x := of_real_exp_of_real_re _ @[simp] lemma exp_of_real_im (x : ℝ) : (exp x).im = 0 := by rw [← of_real_exp_of_real_re, of_real_im] lemma exp_of_real_re (x : ℝ) : (exp x).re = real.exp x := rfl lemma two_sinh : 2 * sinh x = exp x - exp (-x) := mul_div_cancel' _ two_ne_zero' lemma two_cosh : 2 * cosh x = exp x + exp (-x) := mul_div_cancel' _ two_ne_zero' @[simp] lemma sinh_zero : sinh 0 = 0 := by simp [sinh] @[simp] lemma sinh_neg : sinh (-x) = -sinh x := by simp [sinh, exp_neg, (neg_div _ _).symm, add_mul] private lemma sinh_add_aux {a b c d : ℂ} : (a - b) * (c + d) + (a + b) * (c - d) = 2 * (a * c - b * d) := by ring lemma sinh_add : sinh (x + y) = sinh x * cosh y + cosh x * sinh y := begin rw [← mul_right_inj' (@two_ne_zero' ℂ _ _ _), two_sinh, exp_add, neg_add, exp_add, eq_comm, mul_add, ← mul_assoc, two_sinh, mul_left_comm, two_sinh, ← mul_right_inj' (@two_ne_zero' ℂ _ _ _), mul_add, mul_left_comm, two_cosh, ← mul_assoc, two_cosh], exact sinh_add_aux end @[simp] lemma cosh_zero : cosh 0 = 1 := by simp [cosh] @[simp] lemma cosh_neg : cosh (-x) = cosh x := by simp [add_comm, cosh, exp_neg] private lemma cosh_add_aux {a b c d : ℂ} : (a + b) * (c + d) + (a - b) * (c - d) = 2 * (a * c + b * d) := by ring lemma cosh_add : cosh (x + y) = cosh x * cosh y + sinh x * sinh y := begin rw [← mul_right_inj' (@two_ne_zero' ℂ _ _ _), two_cosh, exp_add, neg_add, exp_add, eq_comm, mul_add, ← mul_assoc, two_cosh, ← mul_assoc, two_sinh, ← mul_right_inj' (@two_ne_zero' ℂ _ _ _), mul_add, mul_left_comm, two_cosh, mul_left_comm, two_sinh], exact cosh_add_aux end lemma sinh_sub : sinh (x - y) = sinh x * cosh y - cosh x * sinh y := by simp [sub_eq_add_neg, sinh_add, sinh_neg, cosh_neg] lemma cosh_sub : cosh (x - y) = cosh x * cosh y - sinh x * sinh y := by simp [sub_eq_add_neg, cosh_add, sinh_neg, cosh_neg] lemma sinh_conj : sinh (conj x) = conj (sinh x) := by rw [sinh, ← conj.map_neg, exp_conj, exp_conj, ← conj.map_sub, sinh, conj.map_div, conj_bit0, conj.map_one] @[simp] lemma of_real_sinh_of_real_re (x : ℝ) : ((sinh x).re : ℂ) = sinh x := eq_conj_iff_re.1 $ by rw [← sinh_conj, conj_of_real] @[simp, norm_cast] lemma of_real_sinh (x : ℝ) : (real.sinh x : ℂ) = sinh x := of_real_sinh_of_real_re _ @[simp] lemma sinh_of_real_im (x : ℝ) : (sinh x).im = 0 := by rw [← of_real_sinh_of_real_re, of_real_im] lemma sinh_of_real_re (x : ℝ) : (sinh x).re = real.sinh x := rfl lemma cosh_conj : cosh (conj x) = conj (cosh x) := begin rw [cosh, ← conj.map_neg, exp_conj, exp_conj, ← conj.map_add, cosh, conj.map_div, conj_bit0, conj.map_one] end @[simp] lemma of_real_cosh_of_real_re (x : ℝ) : ((cosh x).re : ℂ) = cosh x := eq_conj_iff_re.1 $ by rw [← cosh_conj, conj_of_real] @[simp, norm_cast] lemma of_real_cosh (x : ℝ) : (real.cosh x : ℂ) = cosh x := of_real_cosh_of_real_re _ @[simp] lemma cosh_of_real_im (x : ℝ) : (cosh x).im = 0 := by rw [← of_real_cosh_of_real_re, of_real_im] lemma cosh_of_real_re (x : ℝ) : (cosh x).re = real.cosh x := rfl lemma tanh_eq_sinh_div_cosh : tanh x = sinh x / cosh x := rfl @[simp] lemma tanh_zero : tanh 0 = 0 := by simp [tanh] @[simp] lemma tanh_neg : tanh (-x) = -tanh x := by simp [tanh, neg_div] lemma tanh_conj : tanh (conj x) = conj (tanh x) := by rw [tanh, sinh_conj, cosh_conj, ← conj.map_div, tanh] @[simp] lemma of_real_tanh_of_real_re (x : ℝ) : ((tanh x).re : ℂ) = tanh x := eq_conj_iff_re.1 $ by rw [← tanh_conj, conj_of_real] @[simp, norm_cast] lemma of_real_tanh (x : ℝ) : (real.tanh x : ℂ) = tanh x := of_real_tanh_of_real_re _ @[simp] lemma tanh_of_real_im (x : ℝ) : (tanh x).im = 0 := by rw [← of_real_tanh_of_real_re, of_real_im] lemma tanh_of_real_re (x : ℝ) : (tanh x).re = real.tanh x := rfl lemma cosh_add_sinh : cosh x + sinh x = exp x := by rw [← mul_right_inj' (@two_ne_zero' ℂ _ _ _), mul_add, two_cosh, two_sinh, add_add_sub_cancel, two_mul] lemma sinh_add_cosh : sinh x + cosh x = exp x := by rw [add_comm, cosh_add_sinh] lemma cosh_sub_sinh : cosh x - sinh x = exp (-x) := by rw [← mul_right_inj' (@two_ne_zero' ℂ _ _ _), mul_sub, two_cosh, two_sinh, add_sub_sub_cancel, two_mul] lemma cosh_sq_sub_sinh_sq : cosh x ^ 2 - sinh x ^ 2 = 1 := by rw [sq_sub_sq, cosh_add_sinh, cosh_sub_sinh, ← exp_add, add_neg_self, exp_zero] lemma cosh_square : cosh x ^ 2 = sinh x ^ 2 + 1 := begin rw ← cosh_sq_sub_sinh_sq x, ring end lemma sinh_square : sinh x ^ 2 = cosh x ^ 2 - 1 := begin rw ← cosh_sq_sub_sinh_sq x, ring end lemma cosh_two_mul : cosh (2 * x) = cosh x ^ 2 + sinh x ^ 2 := by rw [two_mul, cosh_add, pow_two, pow_two] lemma sinh_two_mul : sinh (2 * x) = 2 * sinh x * cosh x := begin rw [two_mul, sinh_add], ring end lemma cosh_three_mul : cosh (3 * x) = 4 * cosh x ^ 3 - 3 * cosh x := begin have h1 : x + 2 * x = 3 * x, by ring, rw [← h1, cosh_add x (2 * x)], simp only [cosh_two_mul, sinh_two_mul], have h2 : sinh x * (2 * sinh x * cosh x) = 2 * cosh x * sinh x ^ 2, by ring, rw [h2, sinh_square], ring end lemma sinh_three_mul : sinh (3 * x) = 4 * sinh x ^ 3 + 3 * sinh x := begin have h1 : x + 2 * x = 3 * x, by ring, rw [← h1, sinh_add x (2 * x)], simp only [cosh_two_mul, sinh_two_mul], have h2 : cosh x * (2 * sinh x * cosh x) = 2 * sinh x * cosh x ^ 2, by ring, rw [h2, cosh_square], ring, end @[simp] lemma sin_zero : sin 0 = 0 := by simp [sin] @[simp] lemma sin_neg : sin (-x) = -sin x := by simp [sin, sub_eq_add_neg, exp_neg, (neg_div _ _).symm, add_mul] lemma two_sin : 2 * sin x = (exp (-x * I) - exp (x * I)) * I := mul_div_cancel' _ two_ne_zero' lemma two_cos : 2 * cos x = exp (x * I) + exp (-x * I) := mul_div_cancel' _ two_ne_zero' lemma sinh_mul_I : sinh (x * I) = sin x * I := by rw [← mul_right_inj' (@two_ne_zero' ℂ _ _ _), two_sinh, ← mul_assoc, two_sin, mul_assoc, I_mul_I, mul_neg_one, neg_sub, neg_mul_eq_neg_mul] lemma cosh_mul_I : cosh (x * I) = cos x := by rw [← mul_right_inj' (@two_ne_zero' ℂ _ _ _), two_cosh, two_cos, neg_mul_eq_neg_mul] lemma sin_add : sin (x + y) = sin x * cos y + cos x * sin y := by rw [← mul_left_inj' I_ne_zero, ← sinh_mul_I, add_mul, add_mul, mul_right_comm, ← sinh_mul_I, mul_assoc, ← sinh_mul_I, ← cosh_mul_I, ← cosh_mul_I, sinh_add] @[simp] lemma cos_zero : cos 0 = 1 := by simp [cos] @[simp] lemma cos_neg : cos (-x) = cos x := by simp [cos, sub_eq_add_neg, exp_neg, add_comm] private lemma cos_add_aux {a b c d : ℂ} : (a + b) * (c + d) - (b - a) * (d - c) * (-1) = 2 * (a * c + b * d) := by ring lemma cos_add : cos (x + y) = cos x * cos y - sin x * sin y := by rw [← cosh_mul_I, add_mul, cosh_add, cosh_mul_I, cosh_mul_I, sinh_mul_I, sinh_mul_I, mul_mul_mul_comm, I_mul_I, mul_neg_one, sub_eq_add_neg] lemma sin_sub : sin (x - y) = sin x * cos y - cos x * sin y := by simp [sub_eq_add_neg, sin_add, sin_neg, cos_neg] lemma cos_sub : cos (x - y) = cos x * cos y + sin x * sin y := by simp [sub_eq_add_neg, cos_add, sin_neg, cos_neg] lemma sin_conj : sin (conj x) = conj (sin x) := by rw [← mul_left_inj' I_ne_zero, ← sinh_mul_I, ← conj_neg_I, ← conj.map_mul, ← conj.map_mul, sinh_conj, mul_neg_eq_neg_mul_symm, sinh_neg, sinh_mul_I, mul_neg_eq_neg_mul_symm] @[simp] lemma of_real_sin_of_real_re (x : ℝ) : ((sin x).re : ℂ) = sin x := eq_conj_iff_re.1 $ by rw [← sin_conj, conj_of_real] @[simp, norm_cast] lemma of_real_sin (x : ℝ) : (real.sin x : ℂ) = sin x := of_real_sin_of_real_re _ @[simp] lemma sin_of_real_im (x : ℝ) : (sin x).im = 0 := by rw [← of_real_sin_of_real_re, of_real_im] lemma sin_of_real_re (x : ℝ) : (sin x).re = real.sin x := rfl lemma cos_conj : cos (conj x) = conj (cos x) := by rw [← cosh_mul_I, ← conj_neg_I, ← conj.map_mul, ← cosh_mul_I, cosh_conj, mul_neg_eq_neg_mul_symm, cosh_neg] @[simp] lemma of_real_cos_of_real_re (x : ℝ) : ((cos x).re : ℂ) = cos x := eq_conj_iff_re.1 $ by rw [← cos_conj, conj_of_real] @[simp, norm_cast] lemma of_real_cos (x : ℝ) : (real.cos x : ℂ) = cos x := of_real_cos_of_real_re _ @[simp] lemma cos_of_real_im (x : ℝ) : (cos x).im = 0 := by rw [← of_real_cos_of_real_re, of_real_im] lemma cos_of_real_re (x : ℝ) : (cos x).re = real.cos x := rfl @[simp] lemma tan_zero : tan 0 = 0 := by simp [tan] lemma tan_eq_sin_div_cos : tan x = sin x / cos x := rfl @[simp] lemma tan_neg : tan (-x) = -tan x := by simp [tan, neg_div] lemma tan_conj : tan (conj x) = conj (tan x) := by rw [tan, sin_conj, cos_conj, ← conj.map_div, tan] @[simp] lemma of_real_tan_of_real_re (x : ℝ) : ((tan x).re : ℂ) = tan x := eq_conj_iff_re.1 $ by rw [← tan_conj, conj_of_real] @[simp, norm_cast] lemma of_real_tan (x : ℝ) : (real.tan x : ℂ) = tan x := of_real_tan_of_real_re _ @[simp] lemma tan_of_real_im (x : ℝ) : (tan x).im = 0 := by rw [← of_real_tan_of_real_re, of_real_im] lemma tan_of_real_re (x : ℝ) : (tan x).re = real.tan x := rfl lemma cos_add_sin_I : cos x + sin x * I = exp (x * I) := by rw [← cosh_add_sinh, sinh_mul_I, cosh_mul_I] lemma cos_sub_sin_I : cos x - sin x * I = exp (-x * I) := by rw [← neg_mul_eq_neg_mul, ← cosh_sub_sinh, sinh_mul_I, cosh_mul_I] lemma sin_sq_add_cos_sq : sin x ^ 2 + cos x ^ 2 = 1 := eq.trans (by rw [cosh_mul_I, sinh_mul_I, mul_pow, I_sq, mul_neg_one, sub_neg_eq_add, add_comm]) (cosh_sq_sub_sinh_sq (x * I)) lemma cos_two_mul' : cos (2 * x) = cos x ^ 2 - sin x ^ 2 := by rw [two_mul, cos_add, ← pow_two, ← pow_two] lemma cos_two_mul : cos (2 * x) = 2 * cos x ^ 2 - 1 := by rw [cos_two_mul', eq_sub_iff_add_eq.2 (sin_sq_add_cos_sq x), ← sub_add, sub_add_eq_add_sub, two_mul] lemma sin_two_mul : sin (2 * x) = 2 * sin x * cos x := by rw [two_mul, sin_add, two_mul, add_mul, mul_comm] lemma cos_square : cos x ^ 2 = 1 / 2 + cos (2 * x) / 2 := by simp [cos_two_mul, div_add_div_same, mul_div_cancel_left, two_ne_zero', -one_div] lemma cos_square' : cos x ^ 2 = 1 - sin x ^ 2 := by { rw [←sin_sq_add_cos_sq x], simp } lemma sin_square : sin x ^ 2 = 1 - cos x ^ 2 := by { rw [←sin_sq_add_cos_sq x], simp } lemma cos_three_mul : cos (3 * x) = 4 * cos x ^ 3 - 3 * cos x := begin have h1 : x + 2 * x = 3 * x, by ring, rw [← h1, cos_add x (2 * x)], simp only [cos_two_mul, sin_two_mul, mul_add, mul_sub, mul_one, pow_two], have h2 : 4 * cos x ^ 3 = 2 * cos x * cos x * cos x + 2 * cos x * cos x ^ 2, by ring, rw [h2, cos_square'], ring end lemma sin_three_mul : sin (3 * x) = 3 * sin x - 4 * sin x ^ 3 := begin have h1 : x + 2 * x = 3 * x, by ring, rw [← h1, sin_add x (2 * x)], simp only [cos_two_mul, sin_two_mul, cos_square'], have h2 : cos x * (2 * sin x * cos x) = 2 * sin x * cos x ^ 2, by ring, rw [h2, cos_square'], ring end lemma exp_mul_I : exp (x * I) = cos x + sin x * I := (cos_add_sin_I _).symm lemma exp_add_mul_I : exp (x + y * I) = exp x * (cos y + sin y * I) := by rw [exp_add, exp_mul_I] lemma exp_eq_exp_re_mul_sin_add_cos : exp x = exp x.re * (cos x.im + sin x.im * I) := by rw [← exp_add_mul_I, re_add_im] /-- De Moivre's formula -/ theorem cos_add_sin_mul_I_pow (n : ℕ) (z : ℂ) : (cos z + sin z * I) ^ n = cos (↑n * z) + sin (↑n * z) * I := begin rw [← exp_mul_I, ← exp_mul_I], induction n with n ih, { rw [pow_zero, nat.cast_zero, zero_mul, zero_mul, exp_zero] }, { rw [pow_succ', ih, nat.cast_succ, add_mul, add_mul, one_mul, exp_add] } end end complex namespace real open complex variables (x y : ℝ) @[simp] lemma exp_zero : exp 0 = 1 := by simp [real.exp] lemma exp_add : exp (x + y) = exp x * exp y := by simp [exp_add, exp] lemma exp_list_sum (l : list ℝ) : exp l.sum = (l.map exp).prod := @monoid_hom.map_list_prod (multiplicative ℝ) ℝ _ _ ⟨exp, exp_zero, exp_add⟩ l lemma exp_multiset_sum (s : multiset ℝ) : exp s.sum = (s.map exp).prod := @monoid_hom.map_multiset_prod (multiplicative ℝ) ℝ _ _ ⟨exp, exp_zero, exp_add⟩ s lemma exp_sum {α : Type*} (s : finset α) (f : α → ℝ) : exp (∑ x in s, f x) = ∏ x in s, exp (f x) := @monoid_hom.map_prod α (multiplicative ℝ) ℝ _ _ ⟨exp, exp_zero, exp_add⟩ f s lemma exp_nat_mul (x : ℝ) : ∀ n : ℕ, exp(n*x) = (exp x)^n | 0 := by rw [nat.cast_zero, zero_mul, exp_zero, pow_zero] | (nat.succ n) := by rw [pow_succ', nat.cast_add_one, add_mul, exp_add, ←exp_nat_mul, one_mul] lemma exp_ne_zero : exp x ≠ 0 := λ h, exp_ne_zero x $ by rw [exp, ← of_real_inj] at h; simp * at * lemma exp_neg : exp (-x) = (exp x)⁻¹ := by rw [← of_real_inj, exp, of_real_exp_of_real_re, of_real_neg, exp_neg, of_real_inv, of_real_exp] lemma exp_sub : exp (x - y) = exp x / exp y := by simp [sub_eq_add_neg, exp_add, exp_neg, div_eq_mul_inv] @[simp] lemma sin_zero : sin 0 = 0 := by simp [sin] @[simp] lemma sin_neg : sin (-x) = -sin x := by simp [sin, exp_neg, (neg_div _ _).symm, add_mul] lemma sin_add : sin (x + y) = sin x * cos y + cos x * sin y := by rw [← of_real_inj]; simp [sin, sin_add] @[simp] lemma cos_zero : cos 0 = 1 := by simp [cos] @[simp] lemma cos_neg : cos (-x) = cos x := by simp [cos, exp_neg] lemma cos_add : cos (x + y) = cos x * cos y - sin x * sin y := by rw ← of_real_inj; simp [cos, cos_add] lemma sin_sub : sin (x - y) = sin x * cos y - cos x * sin y := by simp [sub_eq_add_neg, sin_add, sin_neg, cos_neg] lemma cos_sub : cos (x - y) = cos x * cos y + sin x * sin y := by simp [sub_eq_add_neg, cos_add, sin_neg, cos_neg] lemma tan_eq_sin_div_cos : tan x = sin x / cos x := if h : complex.cos x = 0 then by simp [sin, cos, tan, *, complex.tan, div_eq_mul_inv] at * else by rw [sin, cos, tan, complex.tan, ← of_real_inj, div_eq_mul_inv, mul_re]; simp [norm_sq, (div_div_eq_div_mul _ _ _).symm, div_self h]; refl @[simp] lemma tan_zero : tan 0 = 0 := by simp [tan] @[simp] lemma tan_neg : tan (-x) = -tan x := by simp [tan, neg_div] lemma sin_sq_add_cos_sq : sin x ^ 2 + cos x ^ 2 = 1 := of_real_inj.1 $ by simpa using sin_sq_add_cos_sq x lemma sin_sq_le_one : sin x ^ 2 ≤ 1 := by rw ← sin_sq_add_cos_sq x; exact le_add_of_nonneg_right (pow_two_nonneg _) lemma cos_sq_le_one : cos x ^ 2 ≤ 1 := by rw ← sin_sq_add_cos_sq x; exact le_add_of_nonneg_left (pow_two_nonneg _) lemma abs_sin_le_one : abs' (sin x) ≤ 1 := (mul_self_le_mul_self_iff (_root_.abs_nonneg (sin x)) (by exact zero_le_one)).2 $ by rw [← _root_.abs_mul, abs_mul_self, mul_one, ← pow_two]; apply sin_sq_le_one lemma abs_cos_le_one : abs' (cos x) ≤ 1 := (mul_self_le_mul_self_iff (_root_.abs_nonneg (cos x)) (by exact zero_le_one)).2 $ by rw [← _root_.abs_mul, abs_mul_self, mul_one, ← pow_two]; apply cos_sq_le_one lemma sin_le_one : sin x ≤ 1 := (abs_le.1 (abs_sin_le_one _)).2 lemma cos_le_one : cos x ≤ 1 := (abs_le.1 (abs_cos_le_one _)).2 lemma neg_one_le_sin : -1 ≤ sin x := (abs_le.1 (abs_sin_le_one _)).1 lemma neg_one_le_cos : -1 ≤ cos x := (abs_le.1 (abs_cos_le_one _)).1 lemma cos_two_mul : cos (2 * x) = 2 * cos x ^ 2 - 1 := by rw ← of_real_inj; simp [cos_two_mul] lemma cos_two_mul' : cos (2 * x) = cos x ^ 2 - sin x ^ 2 := by rw ← of_real_inj; simp [cos_two_mul'] lemma sin_two_mul : sin (2 * x) = 2 * sin x * cos x := by rw ← of_real_inj; simp [sin_two_mul] lemma cos_square : cos x ^ 2 = 1 / 2 + cos (2 * x) / 2 := of_real_inj.1 $ by simpa using cos_square x lemma cos_square' : cos x ^ 2 = 1 - sin x ^ 2 := by { rw [←sin_sq_add_cos_sq x], simp } lemma sin_square : sin x ^ 2 = 1 - cos x ^ 2 := eq_sub_iff_add_eq.2 $ sin_sq_add_cos_sq _ lemma cos_three_mul : cos (3 * x) = 4 * cos x ^ 3 - 3 * cos x := by rw ← of_real_inj; simp [cos_three_mul] lemma sin_three_mul : sin (3 * x) = 3 * sin x - 4 * sin x ^ 3 := by rw ← of_real_inj; simp [sin_three_mul] /-- The definition of `sinh` in terms of `exp`. -/ lemma sinh_eq (x : ℝ) : sinh x = (exp x - exp (-x)) / 2 := eq_div_of_mul_eq two_ne_zero $ by rw [sinh, exp, exp, complex.of_real_neg, complex.sinh, mul_two, ← complex.add_re, ← mul_two, div_mul_cancel _ (two_ne_zero' : (2 : ℂ) ≠ 0), complex.sub_re] @[simp] lemma sinh_zero : sinh 0 = 0 := by simp [sinh] @[simp] lemma sinh_neg : sinh (-x) = -sinh x := by simp [sinh, exp_neg, (neg_div _ _).symm, add_mul] lemma sinh_add : sinh (x + y) = sinh x * cosh y + cosh x * sinh y := by rw ← of_real_inj; simp [sinh_add] /-- The definition of `cosh` in terms of `exp`. -/ lemma cosh_eq (x : ℝ) : cosh x = (exp x + exp (-x)) / 2 := eq_div_of_mul_eq two_ne_zero $ by rw [cosh, exp, exp, complex.of_real_neg, complex.cosh, mul_two, ← complex.add_re, ← mul_two, div_mul_cancel _ (two_ne_zero' : (2 : ℂ) ≠ 0), complex.add_re] @[simp] lemma cosh_zero : cosh 0 = 1 := by simp [cosh] @[simp] lemma cosh_neg : cosh (-x) = cosh x := by simp [cosh, exp_neg] lemma cosh_add : cosh (x + y) = cosh x * cosh y + sinh x * sinh y := by rw ← of_real_inj; simp [cosh, cosh_add] lemma sinh_sub : sinh (x - y) = sinh x * cosh y - cosh x * sinh y := by simp [sub_eq_add_neg, sinh_add, sinh_neg, cosh_neg] lemma cosh_sub : cosh (x - y) = cosh x * cosh y - sinh x * sinh y := by simp [sub_eq_add_neg, cosh_add, sinh_neg, cosh_neg] lemma tanh_eq_sinh_div_cosh : tanh x = sinh x / cosh x := of_real_inj.1 $ by simp [tanh_eq_sinh_div_cosh] @[simp] lemma tanh_zero : tanh 0 = 0 := by simp [tanh] @[simp] lemma tanh_neg : tanh (-x) = -tanh x := by simp [tanh, neg_div] lemma cosh_add_sinh : cosh x + sinh x = exp x := by rw ← of_real_inj; simp [cosh_add_sinh] lemma sinh_add_cosh : sinh x + cosh x = exp x := by rw ← of_real_inj; simp [sinh_add_cosh] lemma cosh_sq_sub_sinh_sq (x : ℝ) : cosh x ^ 2 - sinh x ^ 2 = 1 := by rw ← of_real_inj; simp [cosh_sq_sub_sinh_sq] lemma cosh_square : cosh x ^ 2 = sinh x ^ 2 + 1 := by rw ← of_real_inj; simp [cosh_square] lemma sinh_square : sinh x ^ 2 = cosh x ^ 2 - 1 := by rw ← of_real_inj; simp [sinh_square] lemma cosh_two_mul : cosh (2 * x) = cosh x ^ 2 + sinh x ^ 2 := by rw ← of_real_inj; simp [cosh_two_mul] lemma sinh_two_mul : sinh (2 * x) = 2 * sinh x * cosh x := by rw ← of_real_inj; simp [sinh_two_mul] lemma cosh_three_mul : cosh (3 * x) = 4 * cosh x ^ 3 - 3 * cosh x := by rw ← of_real_inj; simp [cosh_three_mul] lemma sinh_three_mul : sinh (3 * x) = 4 * sinh x ^ 3 + 3 * sinh x := by rw ← of_real_inj; simp [sinh_three_mul] open is_absolute_value /- TODO make this private and prove ∀ x -/ lemma add_one_le_exp_of_nonneg {x : ℝ} (hx : 0 ≤ x) : x + 1 ≤ exp x := calc x + 1 ≤ lim (⟨(λ n : ℕ, ((exp' x) n).re), is_cau_seq_re (exp' x)⟩ : cau_seq ℝ abs') : le_lim (cau_seq.le_of_exists ⟨2, λ j hj, show x + (1 : ℝ) ≤ (∑ m in range j, (x ^ m / m! : ℂ)).re, from have h₁ : (((λ m : ℕ, (x ^ m / m! : ℂ)) ∘ nat.succ) 0).re = x, by simp, have h₂ : ((x : ℂ) ^ 0 / 0!).re = 1, by simp, begin rw [← nat.sub_add_cancel hj, sum_range_succ', sum_range_succ', add_re, add_re, h₁, h₂, add_assoc, ← @sum_hom _ _ _ _ _ _ _ complex.re (is_add_group_hom.to_is_add_monoid_hom _)], refine le_add_of_nonneg_of_le (sum_nonneg (λ m hm, _)) (le_refl _), rw [← of_real_pow, ← of_real_nat_cast, ← of_real_div, of_real_re], exact div_nonneg (pow_nonneg hx _) (nat.cast_nonneg _), end⟩) ... = exp x : by rw [exp, complex.exp, ← cau_seq_re, lim_re] lemma one_le_exp {x : ℝ} (hx : 0 ≤ x) : 1 ≤ exp x := by linarith [add_one_le_exp_of_nonneg hx] lemma exp_pos (x : ℝ) : 0 < exp x := (le_total 0 x).elim (lt_of_lt_of_le zero_lt_one ∘ one_le_exp) (λ h, by rw [← neg_neg x, real.exp_neg]; exact inv_pos.2 (lt_of_lt_of_le zero_lt_one (one_le_exp (neg_nonneg.2 h)))) @[simp] lemma abs_exp (x : ℝ) : abs' (exp x) = exp x := abs_of_pos (exp_pos _) lemma exp_strict_mono : strict_mono exp := λ x y h, by rw [← sub_add_cancel y x, real.exp_add]; exact (lt_mul_iff_one_lt_left (exp_pos _)).2 (lt_of_lt_of_le (by linarith) (add_one_le_exp_of_nonneg (by linarith))) @[mono] lemma exp_monotone : ∀ {x y : ℝ}, x ≤ y → exp x ≤ exp y := exp_strict_mono.monotone lemma exp_lt_exp {x y : ℝ} : exp x < exp y ↔ x < y := exp_strict_mono.lt_iff_lt lemma exp_le_exp {x y : ℝ} : exp x ≤ exp y ↔ x ≤ y := exp_strict_mono.le_iff_le lemma exp_injective : function.injective exp := exp_strict_mono.injective @[simp] lemma exp_eq_one_iff : exp x = 1 ↔ x = 0 := by rw [← exp_zero, exp_injective.eq_iff] @[simp] lemma one_lt_exp_iff {x : ℝ} : 1 < exp x ↔ 0 < x := by rw [← exp_zero, exp_lt_exp] @[simp] lemma exp_lt_one_iff {x : ℝ} : exp x < 1 ↔ x < 0 := by rw [← exp_zero, exp_lt_exp] @[simp] lemma exp_le_one_iff {x : ℝ} : exp x ≤ 1 ↔ x ≤ 0 := exp_zero ▸ exp_le_exp @[simp] lemma one_le_exp_iff {x : ℝ} : 1 ≤ exp x ↔ 0 ≤ x := exp_zero ▸ exp_le_exp /-- `real.cosh` is always positive -/ lemma cosh_pos (x : ℝ) : 0 < real.cosh x := (cosh_eq x).symm ▸ half_pos (add_pos (exp_pos x) (exp_pos (-x))) end real namespace complex lemma sum_div_factorial_le {α : Type*} [discrete_linear_ordered_field α] (n j : ℕ) (hn : 0 < n) : ∑ m in filter (λ k, n ≤ k) (range j), (1 / m! : α) ≤ n.succ * (n! * n)⁻¹ := calc ∑ m in filter (λ k, n ≤ k) (range j), (1 / m! : α) = ∑ m in range (j - n), 1 / (m + n)! : sum_bij (λ m _, m - n) (λ m hm, mem_range.2 $ (nat.sub_lt_sub_right_iff (by simp at hm; tauto)).2 (by simp at hm; tauto)) (λ m hm, by rw nat.sub_add_cancel; simp at *; tauto) (λ a₁ a₂ ha₁ ha₂ h, by rwa [nat.sub_eq_iff_eq_add, ← nat.sub_add_comm, eq_comm, nat.sub_eq_iff_eq_add, add_left_inj, eq_comm] at h; simp at *; tauto) (λ b hb, ⟨b + n, mem_filter.2 ⟨mem_range.2 $ nat.add_lt_of_lt_sub_right (mem_range.1 hb), nat.le_add_left _ _⟩, by rw nat.add_sub_cancel⟩) ... ≤ ∑ m in range (j - n), (n! * n.succ ^ m)⁻¹ : begin refine sum_le_sum (assume m n, _), rw [one_div, inv_le_inv], { rw [← nat.cast_pow, ← nat.cast_mul, nat.cast_le, add_comm], exact nat.factorial_mul_pow_le_factorial }, { exact nat.cast_pos.2 (nat.factorial_pos _) }, { exact mul_pos (nat.cast_pos.2 (nat.factorial_pos _)) (pow_pos (nat.cast_pos.2 (nat.succ_pos _)) _) }, end ... = n!⁻¹ * ∑ m in range (j - n), n.succ⁻¹ ^ m : by simp [mul_inv', mul_sum.symm, sum_mul.symm, -nat.factorial_succ, mul_comm, inv_pow'] ... = (n.succ - n.succ * n.succ⁻¹ ^ (j - n)) / (n! * n) : have h₁ : (n.succ : α) ≠ 1, from @nat.cast_one α _ _ ▸ mt nat.cast_inj.1 (mt nat.succ.inj (nat.pos_iff_ne_zero.1 hn)), have h₂ : (n.succ : α) ≠ 0, from nat.cast_ne_zero.2 (nat.succ_ne_zero _), have h₃ : (n! * n : α) ≠ 0, from mul_ne_zero (nat.cast_ne_zero.2 (nat.pos_iff_ne_zero.1 (nat.factorial_pos _))) (nat.cast_ne_zero.2 (nat.pos_iff_ne_zero.1 hn)), have h₄ : (n.succ - 1 : α) = n, by simp, by rw [← geom_series_def, geom_sum_inv h₁ h₂, eq_div_iff_mul_eq h₃, mul_comm _ (n! * n : α), ← mul_assoc (n!⁻¹ : α), ← mul_inv_rev', h₄, ← mul_assoc (n! * n : α), mul_comm (n : α) n!, mul_inv_cancel h₃]; simp [mul_add, add_mul, mul_assoc, mul_comm] ... ≤ n.succ / (n! * n) : begin refine iff.mpr (div_le_div_right (mul_pos _ _)) _, exact nat.cast_pos.2 (nat.factorial_pos _), exact nat.cast_pos.2 hn, exact sub_le_self _ (mul_nonneg (nat.cast_nonneg _) (pow_nonneg (inv_nonneg.2 (nat.cast_nonneg _)) _)) end lemma exp_bound {x : ℂ} (hx : abs x ≤ 1) {n : ℕ} (hn : 0 < n) : abs (exp x - ∑ m in range n, x ^ m / m!) ≤ abs x ^ n * (n.succ * (n! * n)⁻¹) := begin rw [← lim_const (∑ m in range n, _), exp, sub_eq_add_neg, ← lim_neg, lim_add, ← lim_abs], refine lim_le (cau_seq.le_of_exists ⟨n, λ j hj, _⟩), show abs (∑ m in range j, x ^ m / m! - ∑ m in range n, x ^ m / m!) ≤ abs x ^ n * (n.succ * (n! * n)⁻¹), rw sum_range_sub_sum_range hj, exact calc abs (∑ m in (range j).filter (λ k, n ≤ k), (x ^ m / m! : ℂ)) = abs (∑ m in (range j).filter (λ k, n ≤ k), (x ^ n * (x ^ (m - n) / m!) : ℂ)) : congr_arg abs (sum_congr rfl (λ m hm, by rw [← mul_div_assoc, ← pow_add, nat.add_sub_cancel']; simp at hm; tauto)) ... ≤ ∑ m in filter (λ k, n ≤ k) (range j), abs (x ^ n * (_ / m!)) : abv_sum_le_sum_abv _ _ ... ≤ ∑ m in filter (λ k, n ≤ k) (range j), abs x ^ n * (1 / m!) : begin refine sum_le_sum (λ m hm, _), rw [abs_mul, abv_pow abs, abs_div, abs_cast_nat], refine mul_le_mul_of_nonneg_left ((div_le_div_right _).2 _) _, exact nat.cast_pos.2 (nat.factorial_pos _), rw abv_pow abs, exact (pow_le_one _ (abs_nonneg _) hx), exact pow_nonneg (abs_nonneg _) _ end ... = abs x ^ n * (∑ m in (range j).filter (λ k, n ≤ k), (1 / m! : ℝ)) : by simp [abs_mul, abv_pow abs, abs_div, mul_sum.symm] ... ≤ abs x ^ n * (n.succ * (n! * n)⁻¹) : mul_le_mul_of_nonneg_left (sum_div_factorial_le _ _ hn) (pow_nonneg (abs_nonneg _) _) end lemma abs_exp_sub_one_le {x : ℂ} (hx : abs x ≤ 1) : abs (exp x - 1) ≤ 2 * abs x := calc abs (exp x - 1) = abs (exp x - ∑ m in range 1, x ^ m / m!) : by simp [sum_range_succ] ... ≤ abs x ^ 1 * ((nat.succ 1) * (1! * (1 : ℕ))⁻¹) : exp_bound hx dec_trivial ... = 2 * abs x : by simp [two_mul, mul_two, mul_add, mul_comm] lemma abs_exp_sub_one_sub_id_le {x : ℂ} (hx : abs x ≤ 1) : abs (exp x - 1 - x) ≤ (abs x)^2 := calc abs (exp x - 1 - x) = abs (exp x - ∑ m in range 2, x ^ m / m!) : by simp [sub_eq_add_neg, sum_range_succ, add_assoc] ... ≤ (abs x)^2 * (nat.succ 2 * (2! * (2 : ℕ))⁻¹) : exp_bound hx dec_trivial ... ≤ (abs x)^2 * 1 : mul_le_mul_of_nonneg_left (by norm_num) (pow_two_nonneg (abs x)) ... = (abs x)^2 : by rw [mul_one] end complex namespace real open complex finset lemma cos_bound {x : ℝ} (hx : abs' x ≤ 1) : abs' (cos x - (1 - x ^ 2 / 2)) ≤ abs' x ^ 4 * (5 / 96) := calc abs' (cos x - (1 - x ^ 2 / 2)) = abs (complex.cos x - (1 - x ^ 2 / 2)) : by rw ← abs_of_real; simp [of_real_bit0, of_real_one, of_real_inv] ... = abs ((complex.exp (x * I) + complex.exp (-x * I) - (2 - x ^ 2)) / 2) : by simp [complex.cos, sub_div, add_div, neg_div, div_self (@two_ne_zero' ℂ _ _ _)] ... = abs (((complex.exp (x * I) - ∑ m in range 4, (x * I) ^ m / m!) + ((complex.exp (-x * I) - ∑ m in range 4, (-x * I) ^ m / m!))) / 2) : congr_arg abs (congr_arg (λ x : ℂ, x / 2) begin simp only [sum_range_succ], simp [pow_succ], apply complex.ext; simp [div_eq_mul_inv, norm_sq]; ring end) ... ≤ abs ((complex.exp (x * I) - ∑ m in range 4, (x * I) ^ m / m!) / 2) + abs ((complex.exp (-x * I) - ∑ m in range 4, (-x * I) ^ m / m!) / 2) : by rw add_div; exact abs_add _ _ ... = (abs ((complex.exp (x * I) - ∑ m in range 4, (x * I) ^ m / m!)) / 2 + abs ((complex.exp (-x * I) - ∑ m in range 4, (-x * I) ^ m / m!)) / 2) : by simp [complex.abs_div] ... ≤ ((complex.abs (x * I) ^ 4 * (nat.succ 4 * (4! * (4 : ℕ))⁻¹)) / 2 + (complex.abs (-x * I) ^ 4 * (nat.succ 4 * (4! * (4 : ℕ))⁻¹)) / 2) : add_le_add ((div_le_div_right (by norm_num)).2 (exp_bound (by simpa) dec_trivial)) ((div_le_div_right (by norm_num)).2 (exp_bound (by simpa) dec_trivial)) ... ≤ abs' x ^ 4 * (5 / 96) : by norm_num; simp [mul_assoc, mul_comm, mul_left_comm, mul_div_assoc] lemma sin_bound {x : ℝ} (hx : abs' x ≤ 1) : abs' (sin x - (x - x ^ 3 / 6)) ≤ abs' x ^ 4 * (5 / 96) := calc abs' (sin x - (x - x ^ 3 / 6)) = abs (complex.sin x - (x - x ^ 3 / 6)) : by rw ← abs_of_real; simp [of_real_bit0, of_real_one, of_real_inv] ... = abs (((complex.exp (-x * I) - complex.exp (x * I)) * I - (2 * x - x ^ 3 / 3)) / 2) : by simp [complex.sin, sub_div, add_div, neg_div, mul_div_cancel_left _ (@two_ne_zero' ℂ _ _ _), div_div_eq_div_mul, show (3 : ℂ) * 2 = 6, by norm_num] ... = abs ((((complex.exp (-x * I) - ∑ m in range 4, (-x * I) ^ m / m!) - (complex.exp (x * I) - ∑ m in range 4, (x * I) ^ m / m!)) * I) / 2) : congr_arg abs (congr_arg (λ x : ℂ, x / 2) begin simp only [sum_range_succ], simp [pow_succ], apply complex.ext; simp [div_eq_mul_inv, norm_sq]; ring end) ... ≤ abs ((complex.exp (-x * I) - ∑ m in range 4, (-x * I) ^ m / m!) * I / 2) + abs (-((complex.exp (x * I) - ∑ m in range 4, (x * I) ^ m / m!) * I) / 2) : by rw [sub_mul, sub_eq_add_neg, add_div]; exact abs_add _ _ ... = (abs ((complex.exp (x * I) - ∑ m in range 4, (x * I) ^ m / m!)) / 2 + abs ((complex.exp (-x * I) - ∑ m in range 4, (-x * I) ^ m / m!)) / 2) : by simp [add_comm, complex.abs_div, complex.abs_mul] ... ≤ ((complex.abs (x * I) ^ 4 * (nat.succ 4 * (4! * (4 : ℕ))⁻¹)) / 2 + (complex.abs (-x * I) ^ 4 * (nat.succ 4 * (4! * (4 : ℕ))⁻¹)) / 2) : add_le_add ((div_le_div_right (by norm_num)).2 (exp_bound (by simpa) dec_trivial)) ((div_le_div_right (by norm_num)).2 (exp_bound (by simpa) dec_trivial)) ... ≤ abs' x ^ 4 * (5 / 96) : by norm_num; simp [mul_assoc, mul_comm, mul_left_comm, mul_div_assoc] lemma cos_pos_of_le_one {x : ℝ} (hx : abs' x ≤ 1) : 0 < cos x := calc 0 < (1 - x ^ 2 / 2) - abs' x ^ 4 * (5 / 96) : sub_pos.2 $ lt_sub_iff_add_lt.2 (calc abs' x ^ 4 * (5 / 96) + x ^ 2 / 2 ≤ 1 * (5 / 96) + 1 / 2 : add_le_add (mul_le_mul_of_nonneg_right (pow_le_one _ (abs_nonneg _) hx) (by norm_num)) ((div_le_div_right (by norm_num)).2 (by rw [pow_two, ← abs_mul_self, _root_.abs_mul]; exact mul_le_one hx (abs_nonneg _) hx)) ... < 1 : by norm_num) ... ≤ cos x : sub_le.1 (abs_sub_le_iff.1 (cos_bound hx)).2 lemma sin_pos_of_pos_of_le_one {x : ℝ} (hx0 : 0 < x) (hx : x ≤ 1) : 0 < sin x := calc 0 < x - x ^ 3 / 6 - abs' x ^ 4 * (5 / 96) : sub_pos.2 $ lt_sub_iff_add_lt.2 (calc abs' x ^ 4 * (5 / 96) + x ^ 3 / 6 ≤ x * (5 / 96) + x / 6 : add_le_add (mul_le_mul_of_nonneg_right (calc abs' x ^ 4 ≤ abs' x ^ 1 : pow_le_pow_of_le_one (abs_nonneg _) (by rwa _root_.abs_of_nonneg (le_of_lt hx0)) dec_trivial ... = x : by simp [_root_.abs_of_nonneg (le_of_lt (hx0))]) (by norm_num)) ((div_le_div_right (by norm_num)).2 (calc x ^ 3 ≤ x ^ 1 : pow_le_pow_of_le_one (le_of_lt hx0) hx dec_trivial ... = x : pow_one _)) ... < x : by linarith) ... ≤ sin x : sub_le.1 (abs_sub_le_iff.1 (sin_bound (by rwa [_root_.abs_of_nonneg (le_of_lt hx0)]))).2 lemma sin_pos_of_pos_of_le_two {x : ℝ} (hx0 : 0 < x) (hx : x ≤ 2) : 0 < sin x := have x / 2 ≤ 1, from (div_le_iff (by norm_num)).mpr (by simpa), calc 0 < 2 * sin (x / 2) * cos (x / 2) : mul_pos (mul_pos (by norm_num) (sin_pos_of_pos_of_le_one (half_pos hx0) this)) (cos_pos_of_le_one (by rwa [_root_.abs_of_nonneg (le_of_lt (half_pos hx0))])) ... = sin x : by rw [← sin_two_mul, two_mul, add_halves] lemma cos_one_le : cos 1 ≤ 2 / 3 := calc cos 1 ≤ abs' (1 : ℝ) ^ 4 * (5 / 96) + (1 - 1 ^ 2 / 2) : sub_le_iff_le_add.1 (abs_sub_le_iff.1 (cos_bound (by simp))).1 ... ≤ 2 / 3 : by norm_num lemma cos_one_pos : 0 < cos 1 := cos_pos_of_le_one (by simp) lemma cos_two_neg : cos 2 < 0 := calc cos 2 = cos (2 * 1) : congr_arg cos (mul_one _).symm ... = _ : real.cos_two_mul 1 ... ≤ 2 * (2 / 3) ^ 2 - 1 : sub_le_sub_right (mul_le_mul_of_nonneg_left (by rw [pow_two, pow_two]; exact mul_self_le_mul_self (le_of_lt cos_one_pos) cos_one_le) (by norm_num)) _ ... < 0 : by norm_num end real namespace complex lemma abs_cos_add_sin_mul_I (x : ℝ) : abs (cos x + sin x * I) = 1 := have _ := real.sin_sq_add_cos_sq x, by simp [add_comm, abs, norm_sq, pow_two, *, sin_of_real_re, cos_of_real_re, mul_re] at * lemma abs_exp_eq_iff_re_eq {x y : ℂ} : abs (exp x) = abs (exp y) ↔ x.re = y.re := by rw [exp_eq_exp_re_mul_sin_add_cos, exp_eq_exp_re_mul_sin_add_cos y, abs_mul, abs_mul, abs_cos_add_sin_mul_I, abs_cos_add_sin_mul_I, ← of_real_exp, ← of_real_exp, abs_of_nonneg (le_of_lt (real.exp_pos _)), abs_of_nonneg (le_of_lt (real.exp_pos _)), mul_one, mul_one]; exact ⟨λ h, real.exp_injective h, congr_arg _⟩ @[simp] lemma abs_exp_of_real (x : ℝ) : abs (exp x) = real.exp x := by rw [← of_real_exp]; exact abs_of_nonneg (le_of_lt (real.exp_pos _)) end complex
4dc7d3f73ce28910867627e45b497fb7e46ebcd3
88892181780ff536a81e794003fe058062f06758
/src/xena_challenge/challenge3.lean
7a8cc2ac72d3d6f325bf6b938dd95926959ba345
[]
no_license
AtnNn/lean-sandbox
fe2c44280444e8bb8146ab8ac391c82b480c0a2e
8c68afbdc09213173aef1be195da7a9a86060a97
refs/heads/master
1,623,004,395,876
1,579,969,507,000
1,579,969,507,000
146,666,368
0
0
null
null
null
null
UTF-8
Lean
false
false
906
lean
import data.real.basic import lib.attempt theorem challenge3 : (2 : ℝ) + 2 ≠ 5 := attempt begin have h : (2 : ℝ) + 2 = 4 := rfl, rw h, unfold has_one.one, sorry end $ attempt begin apply (λ x, x), norm_num, end $ attempt begin linarith end $ attempt begin -- library_search, sorry end $ attempt begin have h: (2 + 2 : ℕ) ≠ (5 : ℕ), -- library_search, exact nat.bit0_ne_bit1 2 2, sorry end $ attempt begin exact λ h, zero_ne_one $ eq.trans (eq.symm (sub_eq_zero_of_eq h.symm)) (add_sub_cancel' _ _), end $ attempt begin intro h, apply zero_ne_one _, { exact ℝ }, { apply_instance }, apply eq.trans, apply eq.symm, apply sub_eq_zero_of_eq, apply h.symm, apply add_sub_cancel' (4 : ℝ) (1 : ℝ), end $ begin exact bit0_ne_bit1, end #print challenge3 example : Π x : ℕ, 4 ≠ (5 : ℝ) + ↑x := begin linarith, l end
eb58ef66ff347ffa23f89d30b4985f0d5cacbea1
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/Lean/Compiler/LCNF/Simp/FunDeclInfo.lean
e572e46b306e481bc673fb394b59ed1d3e574b45
[ "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
4,462
lean
/- Copyright (c) 2022 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Compiler.LCNF.Simp.Basic namespace Lean.Compiler.LCNF namespace Simp /-- Local function usage information used to decide whether it should be inlined or not. The information is an approximation, but it is on the "safe" side. That is, if we tagged a function with `.once`, then it is applied only once. A local function may be marked as `.many`, but after simplifications the number of applications may reduce to 1. This is not a big problem in practice because we run the simplifier multiple times, and this information is recomputed from scratch at the beginning of each simplification step. -/ inductive FunDeclInfo where /-- Local function is applied once, and must be inlined. -/ | once /-- Local function is applied many times or occur as an argument of another function, and will only be inlined if it is small. -/ | many /-- Function must be inlined. -/ | mustInline deriving Repr, Inhabited /-- Local function declaration statistics. -/ structure FunDeclInfoMap where /-- Mapping from local function name to inlining information. -/ map : HashMap FVarId FunDeclInfo := {} deriving Inhabited def FunDeclInfoMap.format (s : FunDeclInfoMap) : CompilerM Format := do let mut result := Format.nil for (fvarId, info) in s.map.toList do let binderName ← getBinderName fvarId result := result ++ "\n" ++ f!"{binderName} ↦ {repr info}" return result /-- Add new occurrence for the local function with binder name `key`. -/ def FunDeclInfoMap.add (s : FunDeclInfoMap) (fvarId : FVarId) : FunDeclInfoMap := match s with | { map } => match map.find? fvarId with | some .once => { map := map.insert fvarId .many } | none => { map := map.insert fvarId .once } | _ => { map } /-- Add new occurrence for the local function occurring as an argument for another function. -/ def FunDeclInfoMap.addHo (s : FunDeclInfoMap) (fvarId : FVarId) : FunDeclInfoMap := match s with | { map } => match map.find? fvarId with | some .once | none => { map := map.insert fvarId .many } | _ => { map } /-- Add new occurrence for the local function with binder name `key`. -/ def FunDeclInfoMap.addMustInline (s : FunDeclInfoMap) (fvarId : FVarId) : FunDeclInfoMap := match s with | { map } => { map := map.insert fvarId .mustInline } def FunDeclInfoMap.restore (s : FunDeclInfoMap) (fvarId : FVarId) (saved? : Option FunDeclInfo) : FunDeclInfoMap := match s, saved? with | { map }, none => { map := map.erase fvarId } | { map }, some saved => { map := map.insert fvarId saved } /-- Traverse `code` and update function occurrence map. This map is used to decide whether we inline local functions or not. If `mustInline := true`, then all local function declarations occurring in `code` are tagged as `.mustInline`. Recall that we use `.mustInline` for local function declarations occurring in type class instances. -/ partial def FunDeclInfoMap.update (s : FunDeclInfoMap) (code : Code) (mustInline := false) : CompilerM FunDeclInfoMap := do let (_, s) ← go code |>.run s return s where addArgOcc (arg : Arg) : StateRefT FunDeclInfoMap CompilerM Unit := do match arg with | .fvar fvarId => let some funDecl ← findFunDecl'? fvarId | return () modify fun s => s.addHo funDecl.fvarId | .erased .. | .type .. => return () addLetValueOccs (e : LetValue) : StateRefT FunDeclInfoMap CompilerM Unit := do match e with | .erased | .value .. | .proj .. => return () | .const _ _ args => args.forM addArgOcc | .fvar fvarId args => let some funDecl ← findFunDecl'? fvarId | return () modify fun s => s.add funDecl.fvarId args.forM addArgOcc go (code : Code) : StateRefT FunDeclInfoMap CompilerM Unit := do match code with | .let decl k => addLetValueOccs decl.value go k | .fun decl k => if mustInline then modify fun s => s.addMustInline decl.fvarId go decl.value; go k | .jp decl k => go decl.value; go k | .cases c => c.alts.forM fun alt => go alt.getCode | .jmp fvarId args => let funDecl ← getFunDecl fvarId modify fun s => s.add funDecl.fvarId args.forM addArgOcc | .return .. | .unreach .. => return () end Simp end Lean.Compiler.LCNF
dd160787627911c301ce50106531840c526545b9
9b9a16fa2cb737daee6b2785474678b6fa91d6d4
/src/topology/basic.lean
a1a9478e5d12ca4859ba68af76221c12264f6a11
[ "Apache-2.0" ]
permissive
johoelzl/mathlib
253f46daa30b644d011e8e119025b01ad69735c4
592e3c7a2dfbd5826919b4605559d35d4d75938f
refs/heads/master
1,625,657,216,488
1,551,374,946,000
1,551,374,946,000
98,915,829
0
0
Apache-2.0
1,522,917,267,000
1,501,524,499,000
Lean
UTF-8
Lean
false
false
99,813
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, Jeremy Avigad Theory of topological spaces. Parts of the formalization is based on the books: N. Bourbaki: General Topology I. M. James: Topologies and Uniformities A major difference is that this formalization is heavily based on the filter library. -/ import order.filter data.set.countable tactic open set filter lattice classical local attribute [instance] prop_decidable universes u v w structure topological_space (α : Type u) := (is_open : set α → Prop) (is_open_univ : is_open univ) (is_open_inter : ∀s t, is_open s → is_open t → is_open (s ∩ t)) (is_open_sUnion : ∀s, (∀t∈s, is_open t) → is_open (⋃₀ s)) attribute [class] topological_space section topological_space variables {α : Type u} {β : Type v} {ι : Sort w} {a a₁ a₂ : α} {s s₁ s₂ : set α} {p p₁ p₂ : α → Prop} @[extensionality] lemma topological_space_eq : ∀ {f g : topological_space α}, f.is_open = g.is_open → f = g | ⟨a, _, _, _⟩ ⟨b, _, _, _⟩ rfl := rfl section variables [t : topological_space α] include t /-- `is_open s` means that `s` is open in the ambient topological space on `α` -/ def is_open (s : set α) : Prop := topological_space.is_open t s @[simp] lemma is_open_univ : is_open (univ : set α) := topological_space.is_open_univ t lemma is_open_inter (h₁ : is_open s₁) (h₂ : is_open s₂) : is_open (s₁ ∩ s₂) := topological_space.is_open_inter t s₁ s₂ h₁ h₂ lemma is_open_sUnion {s : set (set α)} (h : ∀t ∈ s, is_open t) : is_open (⋃₀ s) := topological_space.is_open_sUnion t s h end lemma is_open_fold {s : set α} {t : topological_space α} : t.is_open s = @is_open α t s := rfl variables [topological_space α] lemma is_open_Union {f : ι → set α} (h : ∀i, is_open (f i)) : is_open (⋃i, f i) := is_open_sUnion $ by rintro _ ⟨i, rfl⟩; exact h i lemma is_open_bUnion {s : set β} {f : β → set α} (h : ∀i∈s, is_open (f i)) : is_open (⋃i∈s, f i) := is_open_Union $ assume i, is_open_Union $ assume hi, h i hi lemma is_open_union (h₁ : is_open s₁) (h₂ : is_open s₂) : is_open (s₁ ∪ s₂) := by rw union_eq_Union; exact is_open_Union (bool.forall_bool.2 ⟨h₂, h₁⟩) @[simp] lemma is_open_empty : is_open (∅ : set α) := by rw ← sUnion_empty; exact is_open_sUnion (assume a, false.elim) lemma is_open_sInter {s : set (set α)} (hs : finite s) : (∀t ∈ s, is_open t) → is_open (⋂₀ s) := finite.induction_on hs (λ _, by rw sInter_empty; exact is_open_univ) $ λ a s has hs ih h, by rw sInter_insert; exact is_open_inter (h _ $ mem_insert _ _) (ih $ λ t, h t ∘ mem_insert_of_mem _) lemma is_open_bInter {s : set β} {f : β → set α} (hs : finite s) : (∀i∈s, is_open (f i)) → is_open (⋂i∈s, f i) := finite.induction_on hs (λ _, by rw bInter_empty; exact is_open_univ) (λ a s has hs ih h, by rw bInter_insert; exact is_open_inter (h a (mem_insert _ _)) (ih (λ i hi, h i (mem_insert_of_mem _ hi)))) lemma is_open_const {p : Prop} : is_open {a : α | p} := by_cases (assume : p, begin simp only [this]; exact is_open_univ end) (assume : ¬ p, begin simp only [this]; exact is_open_empty end) lemma is_open_and : is_open {a | p₁ a} → is_open {a | p₂ a} → is_open {a | p₁ a ∧ p₂ a} := is_open_inter /-- A set is closed if its complement is open -/ def is_closed (s : set α) : Prop := is_open (-s) @[simp] lemma is_closed_empty : is_closed (∅ : set α) := by unfold is_closed; rw compl_empty; exact is_open_univ @[simp] lemma is_closed_univ : is_closed (univ : set α) := by unfold is_closed; rw compl_univ; exact is_open_empty lemma is_closed_union : is_closed s₁ → is_closed s₂ → is_closed (s₁ ∪ s₂) := λ h₁ h₂, by unfold is_closed; rw compl_union; exact is_open_inter h₁ h₂ lemma is_closed_sInter {s : set (set α)} : (∀t ∈ s, is_closed t) → is_closed (⋂₀ s) := by simp only [is_closed, compl_sInter, sUnion_image]; exact assume h, is_open_Union $ assume t, is_open_Union $ assume ht, h t ht lemma is_closed_Inter {f : ι → set α} (h : ∀i, is_closed (f i)) : is_closed (⋂i, f i ) := is_closed_sInter $ assume t ⟨i, (heq : f i = t)⟩, heq ▸ h i @[simp] lemma is_open_compl_iff {s : set α} : is_open (-s) ↔ is_closed s := iff.rfl @[simp] lemma is_closed_compl_iff {s : set α} : is_closed (-s) ↔ is_open s := by rw [←is_open_compl_iff, compl_compl] lemma is_open_diff {s t : set α} (h₁ : is_open s) (h₂ : is_closed t) : is_open (s \ t) := is_open_inter h₁ $ is_open_compl_iff.mpr h₂ lemma is_closed_inter (h₁ : is_closed s₁) (h₂ : is_closed s₂) : is_closed (s₁ ∩ s₂) := by rw [is_closed, compl_inter]; exact is_open_union h₁ h₂ lemma is_closed_Union {s : set β} {f : β → set α} (hs : finite s) : (∀i∈s, is_closed (f i)) → is_closed (⋃i∈s, f i) := finite.induction_on hs (λ _, by rw bUnion_empty; exact is_closed_empty) (λ a s has hs ih h, by rw bUnion_insert; exact is_closed_union (h a (mem_insert _ _)) (ih (λ i hi, h i (mem_insert_of_mem _ hi)))) lemma is_closed_imp {p q : α → Prop} (hp : is_open {x | p x}) (hq : is_closed {x | q x}) : is_closed {x | p x → q x} := have {x | p x → q x} = (- {x | p x}) ∪ {x | q x}, from set.ext $ λ x, imp_iff_not_or, by rw [this]; exact is_closed_union (is_closed_compl_iff.mpr hp) hq lemma is_open_neg : is_closed {a | p a} → is_open {a | ¬ p a} := is_open_compl_iff.mpr /-- The interior of a set `s` is the largest open subset of `s`. -/ def interior (s : set α) : set α := ⋃₀ {t | is_open t ∧ t ⊆ s} lemma mem_interior {s : set α} {x : α} : x ∈ interior s ↔ ∃ t ⊆ s, is_open t ∧ x ∈ t := by simp only [interior, mem_set_of_eq, exists_prop, and_assoc, and.left_comm] @[simp] lemma is_open_interior {s : set α} : is_open (interior s) := is_open_sUnion $ assume t ⟨h₁, h₂⟩, h₁ lemma interior_subset {s : set α} : interior s ⊆ s := sUnion_subset $ assume t ⟨h₁, h₂⟩, h₂ lemma interior_maximal {s t : set α} (h₁ : t ⊆ s) (h₂ : is_open t) : t ⊆ interior s := subset_sUnion_of_mem ⟨h₂, h₁⟩ lemma interior_eq_of_open {s : set α} (h : is_open s) : interior s = s := subset.antisymm interior_subset (interior_maximal (subset.refl s) h) lemma interior_eq_iff_open {s : set α} : interior s = s ↔ is_open s := ⟨assume h, h ▸ is_open_interior, interior_eq_of_open⟩ lemma subset_interior_iff_open {s : set α} : s ⊆ interior s ↔ is_open s := by simp only [interior_eq_iff_open.symm, subset.antisymm_iff, interior_subset, true_and] lemma subset_interior_iff_subset_of_open {s t : set α} (h₁ : is_open s) : s ⊆ interior t ↔ s ⊆ t := ⟨assume h, subset.trans h interior_subset, assume h₂, interior_maximal h₂ h₁⟩ lemma interior_mono {s t : set α} (h : s ⊆ t) : interior s ⊆ interior t := interior_maximal (subset.trans interior_subset h) is_open_interior @[simp] lemma interior_empty : interior (∅ : set α) = ∅ := interior_eq_of_open is_open_empty @[simp] lemma interior_univ : interior (univ : set α) = univ := interior_eq_of_open is_open_univ @[simp] lemma interior_interior {s : set α} : interior (interior s) = interior s := interior_eq_of_open is_open_interior @[simp] lemma interior_inter {s t : set α} : interior (s ∩ t) = interior s ∩ interior t := subset.antisymm (subset_inter (interior_mono $ inter_subset_left s t) (interior_mono $ inter_subset_right s t)) (interior_maximal (inter_subset_inter interior_subset interior_subset) $ is_open_inter is_open_interior is_open_interior) lemma interior_union_is_closed_of_interior_empty {s t : set α} (h₁ : is_closed s) (h₂ : interior t = ∅) : interior (s ∪ t) = interior s := have interior (s ∪ t) ⊆ s, from assume x ⟨u, ⟨(hu₁ : is_open u), (hu₂ : u ⊆ s ∪ t)⟩, (hx₁ : x ∈ u)⟩, classical.by_contradiction $ assume hx₂ : x ∉ s, have u \ s ⊆ t, from assume x ⟨h₁, h₂⟩, or.resolve_left (hu₂ h₁) h₂, have u \ s ⊆ interior t, by rwa subset_interior_iff_subset_of_open (is_open_diff hu₁ h₁), have u \ s ⊆ ∅, by rwa h₂ at this, this ⟨hx₁, hx₂⟩, subset.antisymm (interior_maximal this is_open_interior) (interior_mono $ subset_union_left _ _) lemma is_open_iff_forall_mem_open : is_open s ↔ ∀ x ∈ s, ∃ t ⊆ s, is_open t ∧ x ∈ t := by rw ← subset_interior_iff_open; simp only [subset_def, mem_interior] /-- The closure of `s` is the smallest closed set containing `s`. -/ def closure (s : set α) : set α := ⋂₀ {t | is_closed t ∧ s ⊆ t} @[simp] lemma is_closed_closure {s : set α} : is_closed (closure s) := is_closed_sInter $ assume t ⟨h₁, h₂⟩, h₁ lemma subset_closure {s : set α} : s ⊆ closure s := subset_sInter $ assume t ⟨h₁, h₂⟩, h₂ lemma closure_minimal {s t : set α} (h₁ : s ⊆ t) (h₂ : is_closed t) : closure s ⊆ t := sInter_subset_of_mem ⟨h₂, h₁⟩ lemma closure_eq_of_is_closed {s : set α} (h : is_closed s) : closure s = s := subset.antisymm (closure_minimal (subset.refl s) h) subset_closure lemma closure_eq_iff_is_closed {s : set α} : closure s = s ↔ is_closed s := ⟨assume h, h ▸ is_closed_closure, closure_eq_of_is_closed⟩ lemma closure_subset_iff_subset_of_is_closed {s t : set α} (h₁ : is_closed t) : closure s ⊆ t ↔ s ⊆ t := ⟨subset.trans subset_closure, assume h, closure_minimal h h₁⟩ lemma closure_mono {s t : set α} (h : s ⊆ t) : closure s ⊆ closure t := closure_minimal (subset.trans h subset_closure) is_closed_closure lemma is_closed_of_closure_subset {s : set α} (h : closure s ⊆ s) : is_closed s := by rw subset.antisymm subset_closure h; exact is_closed_closure @[simp] lemma closure_empty : closure (∅ : set α) = ∅ := closure_eq_of_is_closed is_closed_empty lemma closure_empty_iff (s : set α) : closure s = ∅ ↔ s = ∅ := begin split; intro h, { rw set.eq_empty_iff_forall_not_mem, intros x H, simpa only [h] using subset_closure H }, { exact (eq.symm h) ▸ closure_empty }, end @[simp] lemma closure_univ : closure (univ : set α) = univ := closure_eq_of_is_closed is_closed_univ @[simp] lemma closure_closure {s : set α} : closure (closure s) = closure s := closure_eq_of_is_closed is_closed_closure @[simp] lemma closure_union {s t : set α} : closure (s ∪ t) = closure s ∪ closure t := subset.antisymm (closure_minimal (union_subset_union subset_closure subset_closure) $ is_closed_union is_closed_closure is_closed_closure) (union_subset (closure_mono $ subset_union_left _ _) (closure_mono $ subset_union_right _ _)) lemma interior_subset_closure {s : set α} : interior s ⊆ closure s := subset.trans interior_subset subset_closure lemma closure_eq_compl_interior_compl {s : set α} : closure s = - interior (- s) := begin unfold interior closure is_closed, rw [compl_sUnion, compl_image_set_of], simp only [compl_subset_compl] end @[simp] lemma interior_compl {s : set α} : interior (- s) = - closure s := by simp [closure_eq_compl_interior_compl] @[simp] lemma closure_compl {s : set α} : closure (- s) = - interior s := by simp [closure_eq_compl_interior_compl] theorem mem_closure_iff {s : set α} {a : α} : a ∈ closure s ↔ ∀ o, is_open o → a ∈ o → o ∩ s ≠ ∅ := ⟨λ h o oo ao os, have s ⊆ -o, from λ x xs xo, @ne_empty_of_mem α (o∩s) x ⟨xo, xs⟩ os, closure_minimal this (is_closed_compl_iff.2 oo) h ao, λ H c ⟨h₁, h₂⟩, classical.by_contradiction $ λ nc, let ⟨x, hc, hs⟩ := exists_mem_of_ne_empty (H _ h₁ nc) in hc (h₂ hs)⟩ lemma dense_iff_inter_open {s : set α} : closure s = univ ↔ ∀ U, is_open U → U ≠ ∅ → U ∩ s ≠ ∅ := begin split ; intro h, { intros U U_op U_ne, cases exists_mem_of_ne_empty U_ne with x x_in, exact mem_closure_iff.1 (by simp only [h]) U U_op x_in }, { apply eq_univ_of_forall, intro x, rw mem_closure_iff, intros U U_op x_in, exact h U U_op (ne_empty_of_mem x_in) }, end /-- The frontier of a set is the set of points between the closure and interior. -/ def frontier (s : set α) : set α := closure s \ interior s lemma frontier_eq_closure_inter_closure {s : set α} : frontier s = closure s ∩ closure (- s) := by rw [closure_compl, frontier, diff_eq] @[simp] lemma frontier_compl (s : set α) : frontier (-s) = frontier s := by simp only [frontier_eq_closure_inter_closure, lattice.neg_neg, inter_comm] /-- neighbourhood filter -/ def nhds (a : α) : filter α := (⨅ s ∈ {s : set α | a ∈ s ∧ is_open s}, principal s) lemma nhds_sets {a : α} : (nhds a).sets = {s | ∃t⊆s, is_open t ∧ a ∈ t} := calc (nhds a).sets = (⋃s∈{s : set α| a ∈ s ∧ is_open s}, (principal s).sets) : infi_sets_eq' (assume x ⟨hx₁, hx₂⟩ y ⟨hy₁, hy₂⟩, ⟨x ∩ y, ⟨⟨hx₁, hy₁⟩, is_open_inter hx₂ hy₂⟩, le_principal_iff.2 (inter_subset_left _ _), le_principal_iff.2 (inter_subset_right _ _)⟩) ⟨univ, mem_univ _, is_open_univ⟩ ... = {s | ∃t⊆s, is_open t ∧ a ∈ t} : le_antisymm (supr_le $ assume i, supr_le $ assume ⟨hi₁, hi₂⟩ t ht, ⟨i, ht, hi₂, hi₁⟩) (assume t ⟨i, hi₁, hi₂, hi₃⟩, mem_Union.2 ⟨i, mem_Union.2 ⟨⟨hi₃, hi₂⟩, hi₁⟩⟩) lemma map_nhds {a : α} {f : α → β} : map f (nhds a) = (⨅ s ∈ {s : set α | a ∈ s ∧ is_open s}, principal (image f s)) := calc map f (nhds a) = (⨅ s ∈ {s : set α | a ∈ s ∧ is_open s}, map f (principal s)) : map_binfi_eq (assume x ⟨hx₁, hx₂⟩ y ⟨hy₁, hy₂⟩, ⟨x ∩ y, ⟨⟨hx₁, hy₁⟩, is_open_inter hx₂ hy₂⟩, le_principal_iff.2 (inter_subset_left _ _), le_principal_iff.2 (inter_subset_right _ _)⟩) ⟨univ, mem_univ _, is_open_univ⟩ ... = _ : by simp only [map_principal] lemma mem_nhds_sets_iff {a : α} {s : set α} : s ∈ (nhds a).sets ↔ ∃t⊆s, is_open t ∧ a ∈ t := by simp only [nhds_sets, mem_set_of_eq, exists_prop] lemma mem_of_nhds {a : α} {s : set α} : s ∈ (nhds a).sets → a ∈ s := λ H, let ⟨t, ht, _, hs⟩ := mem_nhds_sets_iff.1 H in ht hs lemma mem_nhds_sets {a : α} {s : set α} (hs : is_open s) (ha : a ∈ s) : s ∈ (nhds a).sets := mem_nhds_sets_iff.2 ⟨s, subset.refl _, hs, ha⟩ theorem all_mem_nhds (x : α) (P : set α → Prop) (hP : ∀ s t, s ⊆ t → P s → P t) : (∀ s ∈ (nhds x).sets, P s) ↔ (∀ s, is_open s → x ∈ s → P s) := iff.intro (λ h s os xs, h s (mem_nhds_sets os xs)) (λ h t, begin rw nhds_sets, rintros ⟨s, hs, opens, xs⟩, exact hP _ _ hs (h s opens xs), end) theorem all_mem_nhds_filter (x : α) (f : set α → set β) (hf : ∀ s t, s ⊆ t → f s ⊆ f t) (l : filter β) : (∀ s ∈ (nhds x).sets, f s ∈ l.sets) ↔ (∀ s, is_open s → x ∈ s → f s ∈ l.sets) := all_mem_nhds _ _ (λ s t ssubt h, mem_sets_of_superset h (hf s t ssubt)) theorem rtendsto_nhds {r : rel β α} {l : filter β} {a : α} : rtendsto r l (nhds a) ↔ (∀ s, is_open s → a ∈ s → r.core s ∈ l.sets) := all_mem_nhds_filter _ _ (λ s t h, h) _ theorem rtendsto'_nhds {r : rel β α} {l : filter β} {a : α} : rtendsto' r l (nhds a) ↔ (∀ s, is_open s → a ∈ s → r.preimage s ∈ l.sets) := by { rw [rtendsto'_def], apply all_mem_nhds_filter, apply rel.preimage_mono } theorem ptendsto_nhds {f : β →. α} {l : filter β} {a : α} : ptendsto f l (nhds a) ↔ (∀ s, is_open s → a ∈ s → f.core s ∈ l.sets) := rtendsto_nhds theorem ptendsto'_nhds {f : β →. α} {l : filter β} {a : α} : ptendsto' f l (nhds a) ↔ (∀ s, is_open s → a ∈ s → f.preimage s ∈ l.sets) := rtendsto'_nhds theorem tendsto_nhds {f : β → α} {l : filter β} {a : α} : tendsto f l (nhds a) ↔ (∀ s, is_open s → a ∈ s → f ⁻¹' s ∈ l.sets) := all_mem_nhds_filter _ _ (λ s t h, h) _ lemma tendsto_const_nhds {a : α} {f : filter β} : tendsto (λb:β, a) f (nhds a) := tendsto_nhds.mpr $ assume s hs ha, univ_mem_sets' $ assume _, ha lemma pure_le_nhds : pure ≤ (nhds : α → filter α) := assume a, le_infi $ assume s, le_infi $ assume ⟨h₁, _⟩, principal_mono.mpr $ singleton_subset_iff.2 h₁ lemma tendsto_pure_nhds [topological_space β] (f : α → β) (a : α) : tendsto f (pure a) (nhds (f a)) := begin rw [tendsto, filter.map_pure], exact pure_le_nhds (f a) end @[simp] lemma nhds_neq_bot {a : α} : nhds a ≠ ⊥ := assume : nhds a = ⊥, have pure a = (⊥ : filter α), from lattice.bot_unique $ this ▸ pure_le_nhds a, pure_neq_bot this lemma interior_eq_nhds {s : set α} : interior s = {a | nhds a ≤ principal s} := set.ext $ λ x, by simp only [mem_interior, le_principal_iff, mem_nhds_sets_iff]; refl lemma mem_interior_iff_mem_nhds {s : set α} {a : α} : a ∈ interior s ↔ s ∈ (nhds a).sets := by simp only [interior_eq_nhds, le_principal_iff]; refl lemma is_open_iff_nhds {s : set α} : is_open s ↔ ∀a∈s, nhds a ≤ principal s := calc is_open s ↔ s ⊆ interior s : subset_interior_iff_open.symm ... ↔ (∀a∈s, nhds a ≤ principal s) : by rw [interior_eq_nhds]; refl lemma is_open_iff_mem_nhds {s : set α} : is_open s ↔ ∀a∈s, s ∈ (nhds a).sets := is_open_iff_nhds.trans $ forall_congr $ λ _, imp_congr_right $ λ _, le_principal_iff lemma closure_eq_nhds {s : set α} : closure s = {a | nhds a ⊓ principal s ≠ ⊥} := calc closure s = - interior (- s) : closure_eq_compl_interior_compl ... = {a | ¬ nhds a ≤ principal (-s)} : by rw [interior_eq_nhds]; refl ... = {a | nhds a ⊓ principal s ≠ ⊥} : set.ext $ assume a, not_congr (inf_eq_bot_iff_le_compl (show principal s ⊔ principal (-s) = ⊤, by simp only [sup_principal, union_compl_self, principal_univ]) (by simp only [inf_principal, inter_compl_self, principal_empty])).symm theorem mem_closure_iff_nhds {s : set α} {a : α} : a ∈ closure s ↔ ∀ t ∈ (nhds a).sets, t ∩ s ≠ ∅ := mem_closure_iff.trans ⟨λ H t ht, subset_ne_empty (inter_subset_inter_left _ interior_subset) (H _ is_open_interior (mem_interior_iff_mem_nhds.2 ht)), λ H o oo ao, H _ (mem_nhds_sets oo ao)⟩ /-- `x` belongs to the closure of `s` if and only if some ultrafilter supported on `s` converges to `x`. -/ lemma mem_closure_iff_ultrafilter {s : set α} {x : α} : x ∈ closure s ↔ ∃ (u : ultrafilter α), s ∈ u.val.sets ∧ u.val ≤ nhds x := begin rw closure_eq_nhds, change nhds x ⊓ principal s ≠ ⊥ ↔ _, symmetry, convert exists_ultrafilter_iff _, ext u, rw [←le_principal_iff, inf_comm, le_inf_iff] end lemma is_closed_iff_nhds {s : set α} : is_closed s ↔ ∀a, nhds a ⊓ principal s ≠ ⊥ → a ∈ s := calc is_closed s ↔ closure s = s : by rw [closure_eq_iff_is_closed] ... ↔ closure s ⊆ s : ⟨assume h, by rw h, assume h, subset.antisymm h subset_closure⟩ ... ↔ (∀a, nhds a ⊓ principal s ≠ ⊥ → a ∈ s) : by rw [closure_eq_nhds]; refl lemma closure_inter_open {s t : set α} (h : is_open s) : s ∩ closure t ⊆ closure (s ∩ t) := assume a ⟨hs, ht⟩, have s ∈ (nhds a).sets, from mem_nhds_sets h hs, have nhds a ⊓ principal s = nhds a, from inf_of_le_left $ by rwa le_principal_iff, have nhds a ⊓ principal (s ∩ t) ≠ ⊥, from calc nhds a ⊓ principal (s ∩ t) = nhds a ⊓ (principal s ⊓ principal t) : by rw inf_principal ... = nhds a ⊓ principal t : by rw [←inf_assoc, this] ... ≠ ⊥ : by rw [closure_eq_nhds] at ht; assumption, by rw [closure_eq_nhds]; assumption lemma closure_diff {s t : set α} : closure s - closure t ⊆ closure (s - t) := calc closure s \ closure t = (- closure t) ∩ closure s : by simp only [diff_eq, inter_comm] ... ⊆ closure (- closure t ∩ s) : closure_inter_open $ is_open_compl_iff.mpr $ is_closed_closure ... = closure (s \ closure t) : by simp only [diff_eq, inter_comm] ... ⊆ closure (s \ t) : closure_mono $ diff_subset_diff (subset.refl s) subset_closure lemma mem_of_closed_of_tendsto {f : β → α} {b : filter β} {a : α} {s : set α} (hb : b ≠ ⊥) (hf : tendsto f b (nhds a)) (hs : is_closed s) (h : f ⁻¹' s ∈ b.sets) : a ∈ s := have b.map f ≤ nhds a ⊓ principal s, from le_trans (le_inf (le_refl _) (le_principal_iff.mpr h)) (inf_le_inf hf (le_refl _)), is_closed_iff_nhds.mp hs a $ neq_bot_of_le_neq_bot (map_ne_bot hb) this lemma mem_of_closed_of_tendsto' {f : β → α} {x : filter β} {a : α} {s : set α} (hf : tendsto f x (nhds a)) (hs : is_closed s) (h : x ⊓ principal (f ⁻¹' s) ≠ ⊥) : a ∈ s := is_closed_iff_nhds.mp hs _ $ neq_bot_of_le_neq_bot (@map_ne_bot _ _ _ f h) $ le_inf (le_trans (map_mono $ inf_le_left) hf) $ le_trans (map_mono $ inf_le_right_of_le $ by simp only [comap_principal, le_principal_iff]; exact subset.refl _) (@map_comap_le _ _ _ f) lemma mem_closure_of_tendsto {f : β → α} {b : filter β} {a : α} {s : set α} (hb : b ≠ ⊥) (hf : tendsto f b (nhds a)) (h : f ⁻¹' s ∈ b.sets) : a ∈ closure s := mem_of_closed_of_tendsto hb hf (is_closed_closure) $ filter.mem_sets_of_superset h (preimage_mono subset_closure) /- The nhds_within filter. -/ def nhds_within (a : α) (s : set α) : filter α := nhds a ⊓ principal s theorem nhds_within_eq (a : α) (s : set α) : nhds_within a s = ⨅ t ∈ {t : set α | a ∈ t ∧ is_open t}, principal (t ∩ s) := have set.univ ∈ {s : set α | a ∈ s ∧ is_open s}, from ⟨set.mem_univ _, is_open_univ⟩, begin rw [nhds_within, nhds, lattice.binfi_inf]; try { exact this }, simp only [inf_principal] end theorem nhds_within_univ (a : α) : nhds_within a set.univ = nhds a := by rw [nhds_within, principal_univ, lattice.inf_top_eq] theorem mem_nhds_within (t : set α) (a : α) (s : set α) : t ∈ (nhds_within a s).sets ↔ ∃ u, is_open u ∧ a ∈ u ∧ u ∩ s ⊆ t := begin rw [nhds_within, mem_inf_principal, mem_nhds_sets_iff], split, { rintros ⟨u, hu, openu, au⟩, exact ⟨u, openu, au, λ x ⟨xu, xs⟩, hu xu xs⟩ }, rintros ⟨u, openu, au, hu⟩, exact ⟨u, λ x xu xs, hu ⟨xu, xs⟩, openu, au⟩ end theorem nhds_within_mono (a : α) {s t : set α} (h : s ⊆ t) : nhds_within a s ≤ nhds_within a t := lattice.inf_le_inf (le_refl _) (principal_mono.mpr h) theorem nhds_within_restrict {a : α} (s : set α) {t : set α} (h₀ : a ∈ t) (h₁ : is_open t) : nhds_within a s = nhds_within a (s ∩ t) := have s ∩ t ∈ (nhds_within a s).sets, from inter_mem_sets (mem_inf_sets_of_right (mem_principal_self s)) (mem_inf_sets_of_left (mem_nhds_sets h₁ h₀)), le_antisymm (lattice.le_inf lattice.inf_le_left (le_principal_iff.mpr this)) (lattice.inf_le_inf (le_refl _) (principal_mono.mpr (set.inter_subset_left _ _))) theorem nhds_within_eq_nhds_within {a : α} {s t u : set α} (h₀ : a ∈ s) (h₁ : is_open s) (h₂ : t ∩ s = u ∩ s) : nhds_within a t = nhds_within a u := by rw [nhds_within_restrict t h₀ h₁, nhds_within_restrict u h₀ h₁, h₂] theorem nhs_within_eq_of_open {a : α} {s : set α} (h₀ : a ∈ s) (h₁ : is_open s) : nhds_within a s = nhds a := by rw [←nhds_within_univ]; apply nhds_within_eq_nhds_within h₀ h₁; rw [set.univ_inter, set.inter_self] @[simp] theorem nhds_within_empty (a : α) : nhds_within a {} = ⊥ := by rw [nhds_within, principal_empty, lattice.inf_bot_eq] theorem nhds_within_union (a : α) (s t : set α) : nhds_within a (s ∪ t) = nhds_within a s ⊔ nhds_within a t := by unfold nhds_within; rw [←lattice.inf_sup_left, sup_principal] theorem nhds_within_inter (a : α) (s t : set α) : nhds_within a (s ∩ t) = nhds_within a s ⊓ nhds_within a t := by unfold nhds_within; rw [lattice.inf_left_comm, lattice.inf_assoc, inf_principal, ←lattice.inf_assoc, lattice.inf_idem] theorem nhds_within_inter' (a : α) (s t : set α) : nhds_within a (s ∩ t) = (nhds_within a s) ⊓ principal t := by { unfold nhds_within, rw [←inf_principal, lattice.inf_assoc] } theorem tendsto_if_nhds_within {f g : α → β} {p : α → Prop} [decidable_pred p] {a : α} {s : set α} {l : filter β} (h₀ : tendsto f (nhds_within a (s ∩ p)) l) (h₁ : tendsto g (nhds_within a (s ∩ {x | ¬ p x})) l) : tendsto (λ x, if p x then f x else g x) (nhds_within a s) l := by apply tendsto_if; rw [←nhds_within_inter']; assumption lemma map_nhds_within (f : α → β) (a : α) (s : set α) : map f (nhds_within a s) = ⨅ t ∈ {t : set α | a ∈ t ∧ is_open t}, principal (set.image f (t ∩ s)) := have h₀ : directed_on ((λ (i : set α), principal (i ∩ s)) ⁻¹'o ge) {x : set α | x ∈ {t : set α | a ∈ t ∧ is_open t}}, from assume x ⟨ax, openx⟩ y ⟨ay, openy⟩, ⟨x ∩ y, ⟨⟨ax, ay⟩, is_open_inter openx openy⟩, le_principal_iff.mpr (set.inter_subset_inter_left _ (set.inter_subset_left _ _)), le_principal_iff.mpr (set.inter_subset_inter_left _ (set.inter_subset_right _ _))⟩, have h₁ : ∃ (i : set α), i ∈ {t : set α | a ∈ t ∧ is_open t}, from ⟨set.univ, set.mem_univ _, is_open_univ⟩, by { rw [nhds_within_eq, map_binfi_eq h₀ h₁], simp only [map_principal] } /- locally finite family [General Topology (Bourbaki, 1995)] -/ section locally_finite /-- A family of sets in `set α` is locally finite if at every point `x:α`, there is a neighborhood of `x` which meets only finitely many sets in the family -/ def locally_finite (f : β → set α) := ∀x:α, ∃t∈(nhds x).sets, finite {i | f i ∩ t ≠ ∅ } lemma locally_finite_of_finite {f : β → set α} (h : finite (univ : set β)) : locally_finite f := assume x, ⟨univ, univ_mem_sets, finite_subset h $ subset_univ _⟩ lemma locally_finite_subset {f₁ f₂ : β → set α} (hf₂ : locally_finite f₂) (hf : ∀b, f₁ b ⊆ f₂ b) : locally_finite f₁ := assume a, let ⟨t, ht₁, ht₂⟩ := hf₂ a in ⟨t, ht₁, finite_subset ht₂ $ assume i hi, neq_bot_of_le_neq_bot hi $ inter_subset_inter (hf i) $ subset.refl _⟩ lemma is_closed_Union_of_locally_finite {f : β → set α} (h₁ : locally_finite f) (h₂ : ∀i, is_closed (f i)) : is_closed (⋃i, f i) := is_open_iff_nhds.mpr $ assume a, assume h : a ∉ (⋃i, f i), have ∀i, a ∈ -f i, from assume i hi, h $ mem_Union.2 ⟨i, hi⟩, have ∀i, - f i ∈ (nhds a).sets, by rw [nhds_sets]; exact assume i, ⟨- f i, subset.refl _, h₂ i, this i⟩, let ⟨t, h_sets, (h_fin : finite {i | f i ∩ t ≠ ∅ })⟩ := h₁ a in calc nhds a ≤ principal (t ∩ (⋂ i∈{i | f i ∩ t ≠ ∅ }, - f i)) : begin rw [le_principal_iff], apply @filter.inter_mem_sets _ (nhds a) _ _ h_sets, apply @filter.Inter_mem_sets _ (nhds a) _ _ _ h_fin, exact assume i h, this i end ... ≤ principal (- ⋃i, f i) : begin simp only [principal_mono, subset_def, mem_compl_eq, mem_inter_eq, mem_Inter, mem_set_of_eq, mem_Union, and_imp, not_exists, not_eq_empty_iff_exists, exists_imp_distrib, (≠)], exact assume x xt ht i xfi, ht i x xfi xt xfi end end locally_finite /- compact sets -/ section compact /-- A set `s` is compact if for every filter `f` that contains `s`, every set of `f` also meets every neighborhood of some `a ∈ s`. -/ def compact (s : set α) := ∀f, f ≠ ⊥ → f ≤ principal s → ∃a∈s, f ⊓ nhds a ≠ ⊥ lemma compact_inter {s t : set α} (hs : compact s) (ht : is_closed t) : compact (s ∩ t) := assume f hnf hstf, let ⟨a, hsa, (ha : f ⊓ nhds a ≠ ⊥)⟩ := hs f hnf (le_trans hstf (le_principal_iff.2 (inter_subset_left _ _))) in have ∀a, principal t ⊓ nhds a ≠ ⊥ → a ∈ t, by intro a; rw [inf_comm]; rw [is_closed_iff_nhds] at ht; exact ht a, have a ∈ t, from this a $ neq_bot_of_le_neq_bot ha $ inf_le_inf (le_trans hstf (le_principal_iff.2 (inter_subset_right _ _))) (le_refl _), ⟨a, ⟨hsa, this⟩, ha⟩ lemma compact_diff {s t : set α} (hs : compact s) (ht : is_open t) : compact (s \ t) := compact_inter hs (is_closed_compl_iff.mpr ht) lemma compact_of_is_closed_subset {s t : set α} (hs : compact s) (ht : is_closed t) (h : t ⊆ s) : compact t := by convert ← compact_inter hs ht; exact inter_eq_self_of_subset_right h lemma compact_adherence_nhdset {s t : set α} {f : filter α} (hs : compact s) (hf₂ : f ≤ principal s) (ht₁ : is_open t) (ht₂ : ∀a∈s, nhds a ⊓ f ≠ ⊥ → a ∈ t) : t ∈ f.sets := classical.by_cases mem_sets_of_neq_bot $ assume : f ⊓ principal (- t) ≠ ⊥, let ⟨a, ha, (hfa : f ⊓ principal (-t) ⊓ nhds a ≠ ⊥)⟩ := hs _ this $ inf_le_left_of_le hf₂ in have a ∈ t, from ht₂ a ha $ neq_bot_of_le_neq_bot hfa $ le_inf inf_le_right $ inf_le_left_of_le inf_le_left, have nhds a ⊓ principal (-t) ≠ ⊥, from neq_bot_of_le_neq_bot hfa $ le_inf inf_le_right $ inf_le_left_of_le inf_le_right, have ∀s∈(nhds a ⊓ principal (-t)).sets, s ≠ ∅, from forall_sets_neq_empty_iff_neq_bot.mpr this, have false, from this _ ⟨t, mem_nhds_sets ht₁ ‹a ∈ t›, -t, subset.refl _, subset.refl _⟩ (inter_compl_self _), by contradiction lemma compact_iff_ultrafilter_le_nhds {s : set α} : compact s ↔ (∀f, is_ultrafilter f → f ≤ principal s → ∃a∈s, f ≤ nhds a) := ⟨assume hs : compact s, assume f hf hfs, let ⟨a, ha, h⟩ := hs _ hf.left hfs in ⟨a, ha, le_of_ultrafilter hf h⟩, assume hs : (∀f, is_ultrafilter f → f ≤ principal s → ∃a∈s, f ≤ nhds a), assume f hf hfs, let ⟨a, ha, (h : ultrafilter_of f ≤ nhds a)⟩ := hs (ultrafilter_of f) (ultrafilter_ultrafilter_of hf) (le_trans ultrafilter_of_le hfs) in have ultrafilter_of f ⊓ nhds a ≠ ⊥, by simp only [inf_of_le_left, h]; exact (ultrafilter_ultrafilter_of hf).left, ⟨a, ha, neq_bot_of_le_neq_bot this (inf_le_inf ultrafilter_of_le (le_refl _))⟩⟩ lemma compact_elim_finite_subcover {s : set α} {c : set (set α)} (hs : compact s) (hc₁ : ∀t∈c, is_open t) (hc₂ : s ⊆ ⋃₀ c) : ∃c'⊆c, finite c' ∧ s ⊆ ⋃₀ c' := classical.by_contradiction $ assume h, have h : ∀{c'}, c' ⊆ c → finite c' → ¬ s ⊆ ⋃₀ c', from assume c' h₁ h₂ h₃, h ⟨c', h₁, h₂, h₃⟩, let f : filter α := (⨅c':{c' : set (set α) // c' ⊆ c ∧ finite c'}, principal (s - ⋃₀ c')), ⟨a, ha⟩ := @exists_mem_of_ne_empty α s (assume h', h (empty_subset _) finite_empty $ h'.symm ▸ empty_subset _) in have f ≠ ⊥, from infi_neq_bot_of_directed ⟨a⟩ (assume ⟨c₁, hc₁, hc'₁⟩ ⟨c₂, hc₂, hc'₂⟩, ⟨⟨c₁ ∪ c₂, union_subset hc₁ hc₂, finite_union hc'₁ hc'₂⟩, principal_mono.mpr $ diff_subset_diff_right $ sUnion_mono $ subset_union_left _ _, principal_mono.mpr $ diff_subset_diff_right $ sUnion_mono $ subset_union_right _ _⟩) (assume ⟨c', hc'₁, hc'₂⟩, show principal (s \ _) ≠ ⊥, by simp only [ne.def, principal_eq_bot_iff, diff_eq_empty]; exact h hc'₁ hc'₂), have f ≤ principal s, from infi_le_of_le ⟨∅, empty_subset _, finite_empty⟩ $ show principal (s \ ⋃₀∅) ≤ principal s, from le_principal_iff.2 (diff_subset _ _), let ⟨a, ha, (h : f ⊓ nhds a ≠ ⊥)⟩ := hs f ‹f ≠ ⊥› this, ⟨t, ht₁, (ht₂ : a ∈ t)⟩ := hc₂ ha in have f ≤ principal (-t), from infi_le_of_le ⟨{t}, by rwa singleton_subset_iff, finite_insert _ finite_empty⟩ $ principal_mono.mpr $ show s - ⋃₀{t} ⊆ - t, begin rw sUnion_singleton; exact assume x ⟨_, hnt⟩, hnt end, have is_closed (- t), from is_open_compl_iff.mp $ by rw lattice.neg_neg; exact hc₁ t ht₁, have a ∈ - t, from is_closed_iff_nhds.mp this _ $ neq_bot_of_le_neq_bot h $ le_inf inf_le_right (inf_le_left_of_le ‹f ≤ principal (- t)›), this ‹a ∈ t› lemma compact_elim_finite_subcover_image {s : set α} {b : set β} {c : β → set α} (hs : compact s) (hc₁ : ∀i∈b, is_open (c i)) (hc₂ : s ⊆ ⋃i∈b, c i) : ∃b'⊆b, finite b' ∧ s ⊆ ⋃i∈b', c i := if h : b = ∅ then ⟨∅, empty_subset _, finite_empty, h ▸ hc₂⟩ else let ⟨i, hi⟩ := exists_mem_of_ne_empty h in have hc'₁ : ∀i∈c '' b, is_open i, from assume i ⟨j, hj, h⟩, h ▸ hc₁ _ hj, have hc'₂ : s ⊆ ⋃₀ (c '' b), by rwa set.sUnion_image, let ⟨d, hd₁, hd₂, hd₃⟩ := compact_elim_finite_subcover hs hc'₁ hc'₂ in have ∀x : d, ∃i, i ∈ b ∧ c i = x, from assume ⟨x, hx⟩, hd₁ hx, let ⟨f', hf⟩ := axiom_of_choice this, f := λx:set α, (if h : x ∈ d then f' ⟨x, h⟩ else i : β) in have ∀(x : α) (i : set α), i ∈ d → x ∈ i → (∃ (i : β), i ∈ f '' d ∧ x ∈ c i), from assume x i hid hxi, ⟨f i, mem_image_of_mem f hid, by simpa only [f, dif_pos hid, (hf ⟨_, hid⟩).2] using hxi⟩, ⟨f '' d, assume i ⟨j, hj, h⟩, h ▸ by simpa only [f, dif_pos hj] using (hf ⟨_, hj⟩).1, finite_image f hd₂, subset.trans hd₃ $ by simpa only [subset_def, mem_sUnion, exists_imp_distrib, mem_Union, exists_prop, and_imp]⟩ lemma compact_of_finite_subcover {s : set α} (h : ∀c, (∀t∈c, is_open t) → s ⊆ ⋃₀ c → ∃c'⊆c, finite c' ∧ s ⊆ ⋃₀ c') : compact s := assume f hfn hfs, classical.by_contradiction $ assume : ¬ (∃x∈s, f ⊓ nhds x ≠ ⊥), have hf : ∀x∈s, nhds x ⊓ f = ⊥, by simpa only [not_exists, not_not, inf_comm], have ¬ ∃x∈s, ∀t∈f.sets, x ∈ closure t, from assume ⟨x, hxs, hx⟩, have ∅ ∈ (nhds x ⊓ f).sets, by rw [empty_in_sets_eq_bot, hf x hxs], let ⟨t₁, ht₁, t₂, ht₂, ht⟩ := by rw [mem_inf_sets] at this; exact this in have ∅ ∈ (nhds x ⊓ principal t₂).sets, from (nhds x ⊓ principal t₂).sets_of_superset (inter_mem_inf_sets ht₁ (subset.refl t₂)) ht, have nhds x ⊓ principal t₂ = ⊥, by rwa [empty_in_sets_eq_bot] at this, by simp only [closure_eq_nhds] at hx; exact hx t₂ ht₂ this, have ∀x∈s, ∃t∈f.sets, x ∉ closure t, by simpa only [not_exists, not_forall], let c := (λt, - closure t) '' f.sets, ⟨c', hcc', hcf, hsc'⟩ := h c (assume t ⟨s, hs, h⟩, h ▸ is_closed_closure) (by simpa only [subset_def, sUnion_image, mem_Union]) in let ⟨b, hb⟩ := axiom_of_choice $ show ∀s:c', ∃t, t ∈ f.sets ∧ - closure t = s, from assume ⟨x, hx⟩, hcc' hx in have (⋂s∈c', if h : s ∈ c' then b ⟨s, h⟩ else univ) ∈ f.sets, from Inter_mem_sets hcf $ assume t ht, by rw [dif_pos ht]; exact (hb ⟨t, ht⟩).left, have s ∩ (⋂s∈c', if h : s ∈ c' then b ⟨s, h⟩ else univ) ∈ f.sets, from inter_mem_sets (le_principal_iff.1 hfs) this, have ∅ ∈ f.sets, from mem_sets_of_superset this $ assume x ⟨hxs, hxi⟩, let ⟨t, htc', hxt⟩ := (show ∃t ∈ c', x ∈ t, from hsc' hxs) in have -closure (b ⟨t, htc'⟩) = t, from (hb _).right, have x ∈ - t, from this ▸ (calc x ∈ b ⟨t, htc'⟩ : by rw mem_bInter_iff at hxi; have h := hxi t htc'; rwa [dif_pos htc'] at h ... ⊆ closure (b ⟨t, htc'⟩) : subset_closure ... ⊆ - - closure (b ⟨t, htc'⟩) : by rw lattice.neg_neg; exact subset.refl _), show false, from this hxt, hfn $ by rwa [empty_in_sets_eq_bot] at this lemma compact_iff_finite_subcover {s : set α} : compact s ↔ (∀c, (∀t∈c, is_open t) → s ⊆ ⋃₀ c → ∃c'⊆c, finite c' ∧ s ⊆ ⋃₀ c') := ⟨assume hc c, compact_elim_finite_subcover hc, compact_of_finite_subcover⟩ lemma compact_empty : compact (∅ : set α) := assume f hnf hsf, not.elim hnf $ empty_in_sets_eq_bot.1 $ le_principal_iff.1 hsf lemma compact_singleton {a : α} : compact ({a} : set α) := compact_of_finite_subcover $ assume c hc₁ hc₂, let ⟨i, hic, hai⟩ := (show ∃i ∈ c, a ∈ i, from mem_sUnion.1 $ singleton_subset_iff.1 hc₂) in ⟨{i}, singleton_subset_iff.2 hic, finite_singleton _, by rwa [sUnion_singleton, singleton_subset_iff]⟩ lemma compact_bUnion_of_compact {s : set β} {f : β → set α} (hs : finite s) : (∀i ∈ s, compact (f i)) → compact (⋃i ∈ s, f i) := assume hf, compact_of_finite_subcover $ assume c c_open c_cover, have ∀i : subtype s, ∃c' ⊆ c, finite c' ∧ f i ⊆ ⋃₀ c', from assume ⟨i, hi⟩, compact_elim_finite_subcover (hf i hi) c_open (calc f i ⊆ ⋃i ∈ s, f i : subset_bUnion_of_mem hi ... ⊆ ⋃₀ c : c_cover), let ⟨finite_subcovers, h⟩ := axiom_of_choice this in let c' := ⋃i, finite_subcovers i in have c' ⊆ c, from Union_subset (λi, (h i).fst), have finite c', from @finite_Union _ _ hs.fintype _ (λi, (h i).snd.1), have (⋃i ∈ s, f i) ⊆ ⋃₀ c', from bUnion_subset $ λi hi, calc f i ⊆ ⋃₀ finite_subcovers ⟨i,hi⟩ : (h ⟨i,hi⟩).snd.2 ... ⊆ ⋃₀ c' : sUnion_mono (subset_Union _ _), ⟨c', ‹c' ⊆ c›, ‹finite c'›, this⟩ lemma compact_Union_of_compact {f : β → set α} [fintype β] (h : ∀i, compact (f i)) : compact (⋃i, f i) := by rw ← bUnion_univ; exact compact_bUnion_of_compact finite_univ (λ i _, h i) lemma compact_of_finite {s : set α} (hs : finite s) : compact s := let s' : set α := ⋃i ∈ s, {i} in have e : s' = s, from ext $ λi, by simp only [mem_bUnion_iff, mem_singleton_iff, exists_eq_right'], have compact s', from compact_bUnion_of_compact hs (λ_ _, compact_singleton), e ▸ this lemma compact_union_of_compact {s t : set α} (hs : compact s) (ht : compact t) : compact (s ∪ t) := by rw union_eq_Union; exact compact_Union_of_compact (λ b, by cases b; assumption) /-- Type class for compact spaces. Separation is sometimes included in the definition, especially in the French literature, but we do not include it here. -/ class compact_space (α : Type*) [topological_space α] : Prop := (compact_univ : compact (univ : set α)) lemma compact_univ [topological_space α] [h : compact_space α] : compact (univ : set α) := h.compact_univ lemma compact_of_closed [topological_space α] [compact_space α] {s : set α} (h : is_closed s) : compact s := compact_of_is_closed_subset compact_univ h (subset_univ _) /-- There are various definitions of "locally compact space" in the literature, which agree for Hausdorff spaces but not in general. This one is the precise condition on X needed for the evaluation `map C(X, Y) × X → Y` to be continuous for all `Y` when `C(X, Y)` is given the compact-open topology. -/ class locally_compact_space (α : Type*) [topological_space α] : Prop := (local_compact_nhds : ∀ (x : α) (n ∈ (nhds x).sets), ∃ s ∈ (nhds x).sets, s ⊆ n ∧ compact s) end compact section clopen def is_clopen (s : set α) : Prop := is_open s ∧ is_closed s theorem is_clopen_union {s t : set α} (hs : is_clopen s) (ht : is_clopen t) : is_clopen (s ∪ t) := ⟨is_open_union hs.1 ht.1, is_closed_union hs.2 ht.2⟩ theorem is_clopen_inter {s t : set α} (hs : is_clopen s) (ht : is_clopen t) : is_clopen (s ∩ t) := ⟨is_open_inter hs.1 ht.1, is_closed_inter hs.2 ht.2⟩ @[simp] theorem is_clopen_empty : is_clopen (∅ : set α) := ⟨is_open_empty, is_closed_empty⟩ @[simp] theorem is_clopen_univ : is_clopen (univ : set α) := ⟨is_open_univ, is_closed_univ⟩ theorem is_clopen_compl {s : set α} (hs : is_clopen s) : is_clopen (-s) := ⟨hs.2, is_closed_compl_iff.2 hs.1⟩ @[simp] theorem is_clopen_compl_iff {s : set α} : is_clopen (-s) ↔ is_clopen s := ⟨λ h, compl_compl s ▸ is_clopen_compl h, is_clopen_compl⟩ theorem is_clopen_diff {s t : set α} (hs : is_clopen s) (ht : is_clopen t) : is_clopen (s-t) := is_clopen_inter hs (is_clopen_compl ht) end clopen section irreducible /-- A irreducible set is one where there is no non-trivial pair of disjoint opens. -/ def is_irreducible (s : set α) : Prop := ∀ (u v : set α), is_open u → is_open v → (∃ x, x ∈ s ∩ u) → (∃ x, x ∈ s ∩ v) → ∃ x, x ∈ s ∩ (u ∩ v) theorem is_irreducible_empty : is_irreducible (∅ : set α) := λ _ _ _ _ _ ⟨x, h1, h2⟩, h1.elim theorem is_irreducible_singleton {x} : is_irreducible ({x} : set α) := λ u v _ _ ⟨y, h1, h2⟩ ⟨z, h3, h4⟩, by rw mem_singleton_iff at h1 h3; substs y z; exact ⟨x, or.inl rfl, h2, h4⟩ theorem is_irreducible_closure {s : set α} (H : is_irreducible s) : is_irreducible (closure s) := λ u v hu hv ⟨y, hycs, hyu⟩ ⟨z, hzcs, hzv⟩, let ⟨p, hpu, hps⟩ := exists_mem_of_ne_empty (mem_closure_iff.1 hycs u hu hyu) in let ⟨q, hqv, hqs⟩ := exists_mem_of_ne_empty (mem_closure_iff.1 hzcs v hv hzv) in let ⟨r, hrs, hruv⟩ := H u v hu hv ⟨p, hps, hpu⟩ ⟨q, hqs, hqv⟩ in ⟨r, subset_closure hrs, hruv⟩ theorem exists_irreducible (s : set α) (H : is_irreducible s) : ∃ t : set α, is_irreducible t ∧ s ⊆ t ∧ ∀ u, is_irreducible u → t ⊆ u → u = t := let ⟨m, hm, hsm, hmm⟩ := zorn.zorn_subset₀ { t : set α | is_irreducible t } (λ c hc hcc hcn, let ⟨t, htc⟩ := exists_mem_of_ne_empty hcn in ⟨⋃₀ c, λ u v hu hv ⟨y, hy, hyu⟩ ⟨z, hz, hzv⟩, let ⟨p, hpc, hyp⟩ := mem_sUnion.1 hy, ⟨q, hqc, hzq⟩ := mem_sUnion.1 hz in or.cases_on (zorn.chain.total hcc hpc hqc) (assume hpq : p ⊆ q, let ⟨x, hxp, hxuv⟩ := hc hqc u v hu hv ⟨y, hpq hyp, hyu⟩ ⟨z, hzq, hzv⟩ in ⟨x, mem_sUnion_of_mem hxp hqc, hxuv⟩) (assume hqp : q ⊆ p, let ⟨x, hxp, hxuv⟩ := hc hpc u v hu hv ⟨y, hyp, hyu⟩ ⟨z, hqp hzq, hzv⟩ in ⟨x, mem_sUnion_of_mem hxp hpc, hxuv⟩), λ x hxc, set.subset_sUnion_of_mem hxc⟩) s H in ⟨m, hm, hsm, λ u hu hmu, hmm _ hu hmu⟩ def irreducible_component (x : α) : set α := classical.some (exists_irreducible {x} is_irreducible_singleton) theorem is_irreducible_irreducible_component {x : α} : is_irreducible (irreducible_component x) := (classical.some_spec (exists_irreducible {x} is_irreducible_singleton)).1 theorem mem_irreducible_component {x : α} : x ∈ irreducible_component x := singleton_subset_iff.1 (classical.some_spec (exists_irreducible {x} is_irreducible_singleton)).2.1 theorem eq_irreducible_component {x : α} : ∀ {s : set α}, is_irreducible s → irreducible_component x ⊆ s → s = irreducible_component x := (classical.some_spec (exists_irreducible {x} is_irreducible_singleton)).2.2 theorem is_closed_irreducible_component {x : α} : is_closed (irreducible_component x) := closure_eq_iff_is_closed.1 $ eq_irreducible_component (is_irreducible_closure is_irreducible_irreducible_component) subset_closure /-- A irreducible space is one where there is no non-trivial pair of disjoint opens. -/ class irreducible_space (α : Type u) [topological_space α] : Prop := (is_irreducible_univ : is_irreducible (univ : set α)) theorem irreducible_exists_mem_inter [irreducible_space α] {s t : set α} : is_open s → is_open t → (∃ x, x ∈ s) → (∃ x, x ∈ t) → ∃ x, x ∈ s ∩ t := by simpa only [univ_inter, univ_subset_iff] using @irreducible_space.is_irreducible_univ α _ _ s t end irreducible section connected /-- A connected set is one where there is no non-trivial open partition. -/ def is_connected (s : set α) : Prop := ∀ (u v : set α), is_open u → is_open v → s ⊆ u ∪ v → (∃ x, x ∈ s ∩ u) → (∃ x, x ∈ s ∩ v) → ∃ x, x ∈ s ∩ (u ∩ v) theorem is_connected_of_is_irreducible {s : set α} (H : is_irreducible s) : is_connected s := λ _ _ hu hv _, H _ _ hu hv theorem is_connected_empty : is_connected (∅ : set α) := is_connected_of_is_irreducible is_irreducible_empty theorem is_connected_singleton {x} : is_connected ({x} : set α) := is_connected_of_is_irreducible is_irreducible_singleton theorem is_connected_sUnion (x : α) (c : set (set α)) (H1 : ∀ s ∈ c, x ∈ s) (H2 : ∀ s ∈ c, is_connected s) : is_connected (⋃₀ c) := begin rintro u v hu hv hUcuv ⟨y, hyUc, hyu⟩ ⟨z, hzUc, hzv⟩, cases classical.em (c = ∅) with hc hc, { rw [hc, sUnion_empty] at hyUc, exact hyUc.elim }, cases ne_empty_iff_exists_mem.1 hc with s hs, cases hUcuv (mem_sUnion_of_mem (H1 s hs) hs) with hxu hxv, { rcases hzUc with ⟨t, htc, hzt⟩, specialize H2 t htc u v hu hv (subset.trans (subset_sUnion_of_mem htc) hUcuv), cases H2 ⟨x, H1 t htc, hxu⟩ ⟨z, hzt, hzv⟩ with r hr, exact ⟨r, mem_sUnion_of_mem hr.1 htc, hr.2⟩ }, { rcases hyUc with ⟨t, htc, hyt⟩, specialize H2 t htc u v hu hv (subset.trans (subset_sUnion_of_mem htc) hUcuv), cases H2 ⟨y, hyt, hyu⟩ ⟨x, H1 t htc, hxv⟩ with r hr, exact ⟨r, mem_sUnion_of_mem hr.1 htc, hr.2⟩ } end theorem is_connected_union (x : α) {s t : set α} (H1 : x ∈ s) (H2 : x ∈ t) (H3 : is_connected s) (H4 : is_connected t) : is_connected (s ∪ t) := have _ := is_connected_sUnion x {t,s} (by rintro r (rfl | rfl | h); [exact H1, exact H2, exact h.elim]) (by rintro r (rfl | rfl | h); [exact H3, exact H4, exact h.elim]), have h2 : ⋃₀ {t, s} = s ∪ t, from (sUnion_insert _ _).trans (by rw sUnion_singleton), by rwa h2 at this theorem is_connected_closure {s : set α} (H : is_connected s) : is_connected (closure s) := λ u v hu hv hcsuv ⟨y, hycs, hyu⟩ ⟨z, hzcs, hzv⟩, let ⟨p, hpu, hps⟩ := exists_mem_of_ne_empty (mem_closure_iff.1 hycs u hu hyu) in let ⟨q, hqv, hqs⟩ := exists_mem_of_ne_empty (mem_closure_iff.1 hzcs v hv hzv) in let ⟨r, hrs, hruv⟩ := H u v hu hv (subset.trans subset_closure hcsuv) ⟨p, hps, hpu⟩ ⟨q, hqs, hqv⟩ in ⟨r, subset_closure hrs, hruv⟩ def connected_component (x : α) : set α := ⋃₀ { s : set α | is_connected s ∧ x ∈ s } theorem is_connected_connected_component {x : α} : is_connected (connected_component x) := is_connected_sUnion x _ (λ _, and.right) (λ _, and.left) theorem mem_connected_component {x : α} : x ∈ connected_component x := mem_sUnion_of_mem (mem_singleton x) ⟨is_connected_singleton, mem_singleton x⟩ theorem subset_connected_component {x : α} {s : set α} (H1 : is_connected s) (H2 : x ∈ s) : s ⊆ connected_component x := λ z hz, mem_sUnion_of_mem hz ⟨H1, H2⟩ theorem is_closed_connected_component {x : α} : is_closed (connected_component x) := closure_eq_iff_is_closed.1 $ subset.antisymm (subset_connected_component (is_connected_closure is_connected_connected_component) (subset_closure mem_connected_component)) subset_closure theorem irreducible_component_subset_connected_component {x : α} : irreducible_component x ⊆ connected_component x := subset_connected_component (is_connected_of_is_irreducible is_irreducible_irreducible_component) mem_irreducible_component /-- A connected space is one where there is no non-trivial open partition. -/ class connected_space (α : Type u) [topological_space α] : Prop := (is_connected_univ : is_connected (univ : set α)) instance irreducible_space.connected_space (α : Type u) [topological_space α] [irreducible_space α] : connected_space α := ⟨is_connected_of_is_irreducible $ irreducible_space.is_irreducible_univ α⟩ theorem exists_mem_inter [connected_space α] {s t : set α} : is_open s → is_open t → s ∪ t = univ → (∃ x, x ∈ s) → (∃ x, x ∈ t) → ∃ x, x ∈ s ∩ t := by simpa only [univ_inter, univ_subset_iff] using @connected_space.is_connected_univ α _ _ s t theorem is_clopen_iff [connected_space α] {s : set α} : is_clopen s ↔ s = ∅ ∨ s = univ := ⟨λ hs, classical.by_contradiction $ λ h, have h1 : s ≠ ∅ ∧ -s ≠ ∅, from ⟨mt or.inl h, mt (λ h2, or.inr $ (by rw [← compl_compl s, h2, compl_empty] : s = univ)) h⟩, let ⟨_, h2, h3⟩ := exists_mem_inter hs.1 hs.2 (union_compl_self s) (ne_empty_iff_exists_mem.1 h1.1) (ne_empty_iff_exists_mem.1 h1.2) in h3 h2, by rintro (rfl | rfl); [exact is_clopen_empty, exact is_clopen_univ]⟩ end connected section totally_disconnected def is_totally_disconnected (s : set α) : Prop := ∀ t, t ⊆ s → is_connected t → subsingleton t theorem is_totally_disconnected_empty : is_totally_disconnected (∅ : set α) := λ t ht _, ⟨λ ⟨_, h⟩, (ht h).elim⟩ theorem is_totally_disconnected_singleton {x} : is_totally_disconnected ({x} : set α) := λ t ht _, ⟨λ ⟨p, hp⟩ ⟨q, hq⟩, subtype.eq $ show p = q, from (eq_of_mem_singleton (ht hp)).symm ▸ (eq_of_mem_singleton (ht hq)).symm⟩ class totally_disconnected_space (α : Type u) [topological_space α] : Prop := (is_totally_disconnected_univ : is_totally_disconnected (univ : set α)) end totally_disconnected section totally_separated def is_totally_separated (s : set α) : Prop := ∀ x ∈ s, ∀ y ∈ s, x ≠ y → ∃ u v : set α, is_open u ∧ is_open v ∧ x ∈ u ∧ y ∈ v ∧ s ⊆ u ∪ v ∧ u ∩ v = ∅ theorem is_totally_separated_empty : is_totally_separated (∅ : set α) := λ x, false.elim theorem is_totally_separated_singleton {x} : is_totally_separated ({x} : set α) := λ p hp q hq hpq, (hpq $ (eq_of_mem_singleton hp).symm ▸ (eq_of_mem_singleton hq).symm).elim theorem is_totally_disconnected_of_is_totally_separated {s : set α} (H : is_totally_separated s) : is_totally_disconnected s := λ t hts ht, ⟨λ ⟨x, hxt⟩ ⟨y, hyt⟩, subtype.eq $ classical.by_contradiction $ assume hxy : x ≠ y, let ⟨u, v, hu, hv, hxu, hyv, hsuv, huv⟩ := H x (hts hxt) y (hts hyt) hxy in let ⟨r, hrt, hruv⟩ := ht u v hu hv (subset.trans hts hsuv) ⟨x, hxt, hxu⟩ ⟨y, hyt, hyv⟩ in ((ext_iff _ _).1 huv r).1 hruv⟩ class totally_separated_space (α : Type u) [topological_space α] : Prop := (is_totally_separated_univ : is_totally_separated (univ : set α)) instance totally_separated_space.totally_disconnected_space (α : Type u) [topological_space α] [totally_separated_space α] : totally_disconnected_space α := ⟨is_totally_disconnected_of_is_totally_separated $ totally_separated_space.is_totally_separated_univ α⟩ end totally_separated /- separation axioms -/ section separation /-- A T₀ space, also known as a Kolmogorov space, is a topological space where for every pair `x ≠ y`, there is an open set containing one but not the other. -/ class t0_space (α : Type u) [topological_space α] : Prop := (t0 : ∀ x y, x ≠ y → ∃ U:set α, is_open U ∧ (xor (x ∈ U) (y ∈ U))) theorem exists_open_singleton_of_fintype [t0_space α] [f : fintype α] [decidable_eq α] [ha : nonempty α] : ∃ x:α, is_open ({x}:set α) := have H : ∀ (T : finset α), T ≠ ∅ → ∃ x ∈ T, ∃ u, is_open u ∧ {x} = {y | y ∈ T} ∩ u := begin intro T, apply finset.case_strong_induction_on T, { intro h, exact (h rfl).elim }, { intros x S hxS ih h, by_cases hs : S = ∅, { existsi [x, finset.mem_insert_self x S, univ, is_open_univ], rw [hs, inter_univ], refl }, { rcases ih S (finset.subset.refl S) hs with ⟨y, hy, V, hv1, hv2⟩, by_cases hxV : x ∈ V, { cases t0_space.t0 x y (λ hxy, hxS $ by rwa hxy) with U hu, rcases hu with ⟨hu1, ⟨hu2, hu3⟩ | ⟨hu2, hu3⟩⟩, { existsi [x, finset.mem_insert_self x S, U ∩ V, is_open_inter hu1 hv1], apply set.ext, intro z, split, { intro hzx, rw set.mem_singleton_iff at hzx, rw hzx, exact ⟨finset.mem_insert_self x S, ⟨hu2, hxV⟩⟩ }, { intro hz, rw set.mem_singleton_iff, rcases hz with ⟨hz1, hz2, hz3⟩, cases finset.mem_insert.1 hz1 with hz4 hz4, { exact hz4 }, { have h1 : z ∈ {y : α | y ∈ S} ∩ V, { exact ⟨hz4, hz3⟩ }, rw ← hv2 at h1, rw set.mem_singleton_iff at h1, rw h1 at hz2, exact (hu3 hz2).elim } } }, { existsi [y, finset.mem_insert_of_mem hy, U ∩ V, is_open_inter hu1 hv1], apply set.ext, intro z, split, { intro hz, rw set.mem_singleton_iff at hz, rw hz, refine ⟨finset.mem_insert_of_mem hy, hu2, _⟩, have h1 : y ∈ {y} := set.mem_singleton y, rw hv2 at h1, exact h1.2 }, { intro hz, rw set.mem_singleton_iff, cases hz with hz1 hz2, cases finset.mem_insert.1 hz1 with hz3 hz3, { rw hz3 at hz2, exact (hu3 hz2.1).elim }, { have h1 : z ∈ {y : α | y ∈ S} ∩ V := ⟨hz3, hz2.2⟩, rw ← hv2 at h1, rw set.mem_singleton_iff at h1, exact h1 } } } }, { existsi [y, finset.mem_insert_of_mem hy, V, hv1], apply set.ext, intro z, split, { intro hz, rw set.mem_singleton_iff at hz, rw hz, split, { exact finset.mem_insert_of_mem hy }, { have h1 : y ∈ {y} := set.mem_singleton y, rw hv2 at h1, exact h1.2 } }, { intro hz, rw hv2, cases hz with hz1 hz2, cases finset.mem_insert.1 hz1 with hz3 hz3, { rw hz3 at hz2, exact (hxV hz2).elim }, { exact ⟨hz3, hz2⟩ } } } } } end, begin apply nonempty.elim ha, intro x, specialize H finset.univ (finset.ne_empty_of_mem $ finset.mem_univ x), rcases H with ⟨y, hyf, U, hu1, hu2⟩, existsi y, have h1 : {y : α | y ∈ finset.univ} = (univ : set α), { exact set.eq_univ_of_forall (λ x : α, by rw mem_set_of_eq; exact finset.mem_univ x) }, rw h1 at hu2, rw set.univ_inter at hu2, rw hu2, exact hu1 end /-- A T₁ space, also known as a Fréchet space, is a topological space where every singleton set is closed. Equivalently, for every pair `x ≠ y`, there is an open set containing `x` and not `y`. -/ class t1_space (α : Type u) [topological_space α] : Prop := (t1 : ∀x, is_closed ({x} : set α)) lemma is_closed_singleton [t1_space α] {x : α} : is_closed ({x} : set α) := t1_space.t1 x instance t1_space.t0_space [t1_space α] : t0_space α := ⟨λ x y h, ⟨-{x}, is_open_compl_iff.2 is_closed_singleton, or.inr ⟨λ hyx, or.cases_on hyx h.symm id, λ hx, hx $ or.inl rfl⟩⟩⟩ lemma compl_singleton_mem_nhds [t1_space α] {x y : α} (h : y ≠ x) : - {x} ∈ (nhds y).sets := mem_nhds_sets is_closed_singleton $ by rwa [mem_compl_eq, mem_singleton_iff] @[simp] lemma closure_singleton [t1_space α] {a : α} : closure ({a} : set α) = {a} := closure_eq_of_is_closed is_closed_singleton /-- A T₂ space, also known as a Hausdorff space, is one in which for every `x ≠ y` there exists disjoint open sets around `x` and `y`. This is the most widely used of the separation axioms. -/ class t2_space (α : Type u) [topological_space α] : Prop := (t2 : ∀x y, x ≠ y → ∃u v : set α, is_open u ∧ is_open v ∧ x ∈ u ∧ y ∈ v ∧ u ∩ v = ∅) lemma t2_separation [t2_space α] {x y : α} (h : x ≠ y) : ∃u v : set α, is_open u ∧ is_open v ∧ x ∈ u ∧ y ∈ v ∧ u ∩ v = ∅ := t2_space.t2 x y h instance t2_space.t1_space [t2_space α] : t1_space α := ⟨λ x, is_open_iff_forall_mem_open.2 $ λ y hxy, let ⟨u, v, hu, hv, hyu, hxv, huv⟩ := t2_separation (mt mem_singleton_of_eq hxy) in ⟨u, λ z hz1 hz2, ((ext_iff _ _).1 huv x).1 ⟨mem_singleton_iff.1 hz2 ▸ hz1, hxv⟩, hu, hyu⟩⟩ lemma eq_of_nhds_neq_bot [ht : t2_space α] {x y : α} (h : nhds x ⊓ nhds y ≠ ⊥) : x = y := classical.by_contradiction $ assume : x ≠ y, let ⟨u, v, hu, hv, hx, hy, huv⟩ := t2_space.t2 x y this in have u ∩ v ∈ (nhds x ⊓ nhds y).sets, from inter_mem_inf_sets (mem_nhds_sets hu hx) (mem_nhds_sets hv hy), h $ empty_in_sets_eq_bot.mp $ huv ▸ this lemma t2_iff_nhds : t2_space α ↔ ∀ {x y : α}, nhds x ⊓ nhds y ≠ ⊥ → x = y := ⟨assume h, by exactI λ x y, eq_of_nhds_neq_bot, assume h, ⟨assume x y xy, have nhds x ⊓ nhds y = ⊥ := classical.by_contradiction (mt h xy), let ⟨u', hu', v', hv', u'v'⟩ := empty_in_sets_eq_bot.mpr this, ⟨u, uu', uo, hu⟩ := mem_nhds_sets_iff.mp hu', ⟨v, vv', vo, hv⟩ := mem_nhds_sets_iff.mp hv' in ⟨u, v, uo, vo, hu, hv, disjoint.eq_bot $ disjoint_mono uu' vv' u'v'⟩⟩⟩ lemma t2_iff_ultrafilter : t2_space α ↔ ∀ f {x y : α}, is_ultrafilter f → f ≤ nhds x → f ≤ nhds y → x = y := t2_iff_nhds.trans ⟨assume h f x y u fx fy, h $ neq_bot_of_le_neq_bot u.1 (le_inf fx fy), assume h x y xy, let ⟨f, hf, uf⟩ := exists_ultrafilter xy in h f uf (le_trans hf lattice.inf_le_left) (le_trans hf lattice.inf_le_right)⟩ @[simp] lemma nhds_eq_nhds_iff {a b : α} [t2_space α] : nhds a = nhds b ↔ a = b := ⟨assume h, eq_of_nhds_neq_bot $ by rw [h, inf_idem]; exact nhds_neq_bot, assume h, h ▸ rfl⟩ @[simp] lemma nhds_le_nhds_iff {a b : α} [t2_space α] : nhds a ≤ nhds b ↔ a = b := ⟨assume h, eq_of_nhds_neq_bot $ by rw [inf_of_le_left h]; exact nhds_neq_bot, assume h, h ▸ le_refl _⟩ lemma tendsto_nhds_unique [t2_space α] {f : β → α} {l : filter β} {a b : α} (hl : l ≠ ⊥) (ha : tendsto f l (nhds a)) (hb : tendsto f l (nhds b)) : a = b := eq_of_nhds_neq_bot $ neq_bot_of_le_neq_bot (map_ne_bot hl) $ le_inf ha hb end separation section regularity /-- A T₃ space, also known as a regular space (although this condition sometimes omits T₂), is one in which for every closed `C` and `x ∉ C`, there exist disjoint open sets containing `x` and `C` respectively. -/ class regular_space (α : Type u) [topological_space α] extends t1_space α : Prop := (regular : ∀{s:set α} {a}, is_closed s → a ∉ s → ∃t, is_open t ∧ s ⊆ t ∧ nhds a ⊓ principal t = ⊥) lemma nhds_is_closed [regular_space α] {a : α} {s : set α} (h : s ∈ (nhds a).sets) : ∃t∈(nhds a).sets, t ⊆ s ∧ is_closed t := let ⟨s', h₁, h₂, h₃⟩ := mem_nhds_sets_iff.mp h in have ∃t, is_open t ∧ -s' ⊆ t ∧ nhds a ⊓ principal t = ⊥, from regular_space.regular (is_closed_compl_iff.mpr h₂) (not_not_intro h₃), let ⟨t, ht₁, ht₂, ht₃⟩ := this in ⟨-t, mem_sets_of_neq_bot $ by rwa [lattice.neg_neg], subset.trans (compl_subset_comm.1 ht₂) h₁, is_closed_compl_iff.mpr ht₁⟩ variable (α) instance regular_space.t2_space [regular_space α] : t2_space α := ⟨λ x y hxy, let ⟨s, hs, hys, hxs⟩ := regular_space.regular is_closed_singleton (mt mem_singleton_iff.1 hxy), ⟨t, hxt, u, hsu, htu⟩ := empty_in_sets_eq_bot.2 hxs, ⟨v, hvt, hv, hxv⟩ := mem_nhds_sets_iff.1 hxt in ⟨v, s, hv, hs, hxv, singleton_subset_iff.1 hys, eq_empty_of_subset_empty $ λ z ⟨hzv, hzs⟩, htu ⟨hvt hzv, hsu hzs⟩⟩⟩ end regularity section normality /-- A T₄ space, also known as a normal space (although this condition sometimes omits T₂), is one in which for every pair of disjoint closed sets `C` and `D`, there exist disjoint open sets containing `C` and `D` respectively. -/ class normal_space (α : Type u) [topological_space α] extends t1_space α : Prop := (normal : ∀ s t : set α, is_closed s → is_closed t → disjoint s t → ∃ u v, is_open u ∧ is_open v ∧ s ⊆ u ∧ t ⊆ v ∧ disjoint u v) theorem normal_separation [normal_space α] (s t : set α) (H1 : is_closed s) (H2 : is_closed t) (H3 : disjoint s t) : ∃ u v, is_open u ∧ is_open v ∧ s ⊆ u ∧ t ⊆ v ∧ disjoint u v := normal_space.normal s t H1 H2 H3 variable (α) instance normal_space.regular_space [normal_space α] : regular_space α := { regular := λ s x hs hxs, let ⟨u, v, hu, hv, hsu, hxv, huv⟩ := normal_separation s {x} hs is_closed_singleton (λ _ ⟨hx, hy⟩, hxs $ set.mem_of_eq_of_mem (set.eq_of_mem_singleton hy).symm hx) in ⟨u, hu, hsu, filter.empty_in_sets_eq_bot.1 $ filter.mem_inf_sets.2 ⟨v, mem_nhds_sets hv (set.singleton_subset_iff.1 hxv), u, filter.mem_principal_self u, set.inter_comm u v ▸ huv⟩⟩ } end normality /- generating sets -/ end topological_space namespace topological_space variables {α : Type u} /-- The least topology containing a collection of basic sets. -/ inductive generate_open (g : set (set α)) : set α → Prop | basic : ∀s∈g, generate_open s | univ : generate_open univ | inter : ∀s t, generate_open s → generate_open t → generate_open (s ∩ t) | sUnion : ∀k, (∀s∈k, generate_open s) → generate_open (⋃₀ k) /-- The smallest topological space containing the collection `g` of basic sets -/ def generate_from (g : set (set α)) : topological_space α := { is_open := generate_open g, is_open_univ := generate_open.univ g, is_open_inter := generate_open.inter, is_open_sUnion := generate_open.sUnion } lemma nhds_generate_from {g : set (set α)} {a : α} : @nhds α (generate_from g) a = (⨅s∈{s | a ∈ s ∧ s ∈ g}, principal s) := le_antisymm (infi_le_infi $ assume s, infi_le_infi_const $ assume ⟨as, sg⟩, ⟨as, generate_open.basic _ sg⟩) (le_infi $ assume s, le_infi $ assume ⟨as, hs⟩, have ∀s, generate_open g s → a ∈ s → (⨅s∈{s | a ∈ s ∧ s ∈ g}, principal s) ≤ principal s, begin intros s hs, induction hs, case generate_open.basic : s hs { exact assume as, infi_le_of_le s $ infi_le _ ⟨as, hs⟩ }, case generate_open.univ { rw [principal_univ], exact assume _, le_top }, case generate_open.inter : s t hs' ht' hs ht { exact assume ⟨has, hat⟩, calc _ ≤ principal s ⊓ principal t : le_inf (hs has) (ht hat) ... = _ : inf_principal }, case generate_open.sUnion : k hk' hk { exact λ ⟨t, htk, hat⟩, calc _ ≤ principal t : hk t htk hat ... ≤ _ : le_principal_iff.2 $ subset_sUnion_of_mem htk } end, this s hs as) lemma tendsto_nhds_generate_from {β : Type*} {m : α → β} {f : filter α} {g : set (set β)} {b : β} (h : ∀s∈g, b ∈ s → m ⁻¹' s ∈ f.sets) : tendsto m f (@nhds β (generate_from g) b) := by rw [nhds_generate_from]; exact (tendsto_infi.2 $ assume s, tendsto_infi.2 $ assume ⟨hbs, hsg⟩, tendsto_principal.2 $ h s hsg hbs) protected def mk_of_nhds (n : α → filter α) : topological_space α := { is_open := λs, ∀a∈s, s ∈ (n a).sets, is_open_univ := assume x h, univ_mem_sets, is_open_inter := assume s t hs ht x ⟨hxs, hxt⟩, inter_mem_sets (hs x hxs) (ht x hxt), is_open_sUnion := assume s hs a ⟨x, hx, hxa⟩, mem_sets_of_superset (hs x hx _ hxa) (set.subset_sUnion_of_mem hx) } lemma nhds_mk_of_nhds (n : α → filter α) (a : α) (h₀ : pure ≤ n) (h₁ : ∀{a s}, s ∈ (n a).sets → ∃t∈(n a).sets, t ⊆ s ∧ ∀a'∈t, s ∈ (n a').sets) : @nhds α (topological_space.mk_of_nhds n) a = n a := begin letI := topological_space.mk_of_nhds n, refine le_antisymm (assume s hs, _) (assume s hs, _), { have h₀ : {b | s ∈ (n b).sets} ⊆ s := assume b hb, mem_pure_sets.1 $ h₀ b hb, have h₁ : {b | s ∈ (n b).sets} ∈ (nhds a).sets, { refine mem_nhds_sets (assume b (hb : s ∈ (n b).sets), _) hs, rcases h₁ hb with ⟨t, ht, hts, h⟩, exact mem_sets_of_superset ht h }, exact mem_sets_of_superset h₁ h₀ }, { rcases (@mem_nhds_sets_iff α (topological_space.mk_of_nhds n) _ _).1 hs with ⟨t, hts, ht, hat⟩, exact (n a).sets_of_superset (ht _ hat) hts }, end end topological_space section lattice variables {α : Type u} {β : Type v} instance : partial_order (topological_space α) := { le := λt s, t.is_open ≤ s.is_open, le_antisymm := assume t s h₁ h₂, topological_space_eq $ le_antisymm h₁ h₂, le_refl := assume t, le_refl t.is_open, le_trans := assume a b c h₁ h₂, @le_trans _ _ a.is_open b.is_open c.is_open h₁ h₂ } lemma generate_from_le_iff_subset_is_open {g : set (set α)} {t : topological_space α} : topological_space.generate_from g ≤ t ↔ g ⊆ {s | t.is_open s} := iff.intro (assume ht s hs, ht _ $ topological_space.generate_open.basic s hs) (assume hg s hs, hs.rec_on (assume v hv, hg hv) t.is_open_univ (assume u v _ _, t.is_open_inter u v) (assume k _, t.is_open_sUnion k)) protected def mk_of_closure (s : set (set α)) (hs : {u | (topological_space.generate_from s).is_open u} = s) : topological_space α := { is_open := λu, u ∈ s, is_open_univ := hs ▸ topological_space.generate_open.univ _, is_open_inter := hs ▸ topological_space.generate_open.inter, is_open_sUnion := hs ▸ topological_space.generate_open.sUnion } lemma mk_of_closure_sets {s : set (set α)} {hs : {u | (topological_space.generate_from s).is_open u} = s} : mk_of_closure s hs = topological_space.generate_from s := topological_space_eq hs.symm def gi_generate_from (α : Type*) : galois_insertion topological_space.generate_from (λt:topological_space α, {s | t.is_open s}) := { gc := assume g t, generate_from_le_iff_subset_is_open, le_l_u := assume ts s hs, topological_space.generate_open.basic s hs, choice := λg hg, mk_of_closure g (subset.antisymm hg $ generate_from_le_iff_subset_is_open.1 $ le_refl _), choice_eq := assume s hs, mk_of_closure_sets } lemma generate_from_mono {α} {g₁ g₂ : set (set α)} (h : g₁ ⊆ g₂) : topological_space.generate_from g₁ ≤ topological_space.generate_from g₂ := (gi_generate_from _).gc.monotone_l h instance {α : Type u} : complete_lattice (topological_space α) := (gi_generate_from α).lift_complete_lattice class discrete_topology (α : Type*) [t : topological_space α] := (eq_top : t = ⊤) @[simp] lemma is_open_discrete [topological_space α] [discrete_topology α] (s : set α) : is_open s := (discrete_topology.eq_top α).symm ▸ trivial lemma nhds_top (α : Type*) : (@nhds α ⊤) = pure := begin ext a s, rw [mem_nhds_sets_iff, mem_pure_iff], split, { exact assume ⟨t, ht, _, hta⟩, ht hta }, { exact assume h, ⟨{a}, set.singleton_subset_iff.2 h, trivial, set.mem_singleton a⟩ } end lemma nhds_discrete (α : Type*) [topological_space α] [discrete_topology α] : (@nhds α _) = pure := (discrete_topology.eq_top α).symm ▸ nhds_top α instance t2_space_discrete [topological_space α] [discrete_topology α] : t2_space α := { t2 := assume x y hxy, ⟨{x}, {y}, is_open_discrete _, is_open_discrete _, mem_insert _ _, mem_insert _ _, eq_empty_iff_forall_not_mem.2 $ by intros z hz; cases eq_of_mem_singleton hz.1; cases eq_of_mem_singleton hz.2; cc⟩ } lemma le_of_nhds_le_nhds {t₁ t₂ : topological_space α} (h : ∀x, @nhds α t₂ x ≤ @nhds α t₁ x) : t₁ ≤ t₂ := assume s, show @is_open α t₁ s → @is_open α t₂ s, begin simp only [is_open_iff_nhds, le_principal_iff]; exact assume hs a ha, h _ $ hs _ ha end lemma eq_of_nhds_eq_nhds {t₁ t₂ : topological_space α} (h : ∀x, @nhds α t₂ x = @nhds α t₁ x) : t₁ = t₂ := le_antisymm (le_of_nhds_le_nhds $ assume x, le_of_eq $ h x) (le_of_nhds_le_nhds $ assume x, le_of_eq $ (h x).symm) lemma eq_top_of_singletons_open {t : topological_space α} (h : ∀ x, t.is_open {x}) : t = ⊤ := top_unique $ le_of_nhds_le_nhds $ assume x, have nhds x ≤ pure x, from infi_le_of_le {x} (infi_le _ (by simpa using h x)), le_trans this (@pure_le_nhds _ ⊤ x) end lattice section galois_connection variables {α : Type*} {β : Type*} {γ : Type*} /-- Given `f : α → β` and a topology on `β`, the induced topology on `α` is the collection of sets that are preimages of some open set in `β`. This is the coarsest topology that makes `f` continuous. -/ def topological_space.induced {α : Type u} {β : Type v} (f : α → β) (t : topological_space β) : topological_space α := { is_open := λs, ∃s', t.is_open s' ∧ f ⁻¹' s' = s, is_open_univ := ⟨univ, t.is_open_univ, preimage_univ⟩, is_open_inter := by rintro s₁ s₂ ⟨s'₁, hs₁, rfl⟩ ⟨s'₂, hs₂, rfl⟩; exact ⟨s'₁ ∩ s'₂, t.is_open_inter _ _ hs₁ hs₂, preimage_inter⟩, is_open_sUnion := assume s h, begin simp only [classical.skolem] at h, cases h with f hf, apply exists.intro (⋃(x : set α) (h : x ∈ s), f x h), simp only [sUnion_eq_bUnion, preimage_Union, (λx h, (hf x h).right)], refine ⟨_, rfl⟩, exact (@is_open_Union β _ t _ $ assume i, show is_open (⋃h, f i h), from @is_open_Union β _ t _ $ assume h, (hf i h).left) end } lemma is_open_induced_iff [t : topological_space β] {s : set α} {f : α → β} : @is_open α (t.induced f) s ↔ (∃t, is_open t ∧ f ⁻¹' t = s) := iff.refl _ lemma is_closed_induced_iff [t : topological_space β] {s : set α} {f : α → β} : @is_closed α (t.induced f) s ↔ (∃t, is_closed t ∧ s = f ⁻¹' t) := ⟨assume ⟨t, ht, heq⟩, ⟨-t, is_closed_compl_iff.2 ht, by simp only [preimage_compl, heq, lattice.neg_neg]⟩, assume ⟨t, ht, heq⟩, ⟨-t, ht, by simp only [preimage_compl, heq.symm]⟩⟩ /-- Given `f : α → β` and a topology on `α`, the coinduced topology on `β` is defined such that `s:set β` is open if the preimage of `s` is open. This is the finest topology that makes `f` continuous. -/ def topological_space.coinduced {α : Type u} {β : Type v} (f : α → β) (t : topological_space α) : topological_space β := { is_open := λs, t.is_open (f ⁻¹' s), is_open_univ := by rw preimage_univ; exact t.is_open_univ, is_open_inter := assume s₁ s₂ h₁ h₂, by rw preimage_inter; exact t.is_open_inter _ _ h₁ h₂, is_open_sUnion := assume s h, by rw [preimage_sUnion]; exact (@is_open_Union _ _ t _ $ assume i, show is_open (⋃ (H : i ∈ s), f ⁻¹' i), from @is_open_Union _ _ t _ $ assume hi, h i hi) } lemma is_open_coinduced {t : topological_space α} {s : set β} {f : α → β} : @is_open β (topological_space.coinduced f t) s ↔ is_open (f ⁻¹' s) := iff.refl _ variables {t t₁ t₂ : topological_space α} {t' : topological_space β} {f : α → β} {g : β → α} lemma induced_le_iff_le_coinduced {f : α → β } {tα : topological_space α} {tβ : topological_space β} : tβ.induced f ≤ tα ↔ tβ ≤ tα.coinduced f := iff.intro (assume h s hs, show tα.is_open (f ⁻¹' s), from h _ ⟨s, hs, rfl⟩) (assume h s ⟨t, ht, hst⟩, hst ▸ h _ ht) lemma gc_induced_coinduced (f : α → β) : galois_connection (topological_space.induced f) (topological_space.coinduced f) := assume f g, induced_le_iff_le_coinduced lemma induced_mono (h : t₁ ≤ t₂) : t₁.induced g ≤ t₂.induced g := (gc_induced_coinduced g).monotone_l h lemma coinduced_mono (h : t₁ ≤ t₂) : t₁.coinduced f ≤ t₂.coinduced f := (gc_induced_coinduced f).monotone_u h @[simp] lemma induced_bot : (⊥ : topological_space α).induced g = ⊥ := (gc_induced_coinduced g).l_bot @[simp] lemma induced_sup : (t₁ ⊔ t₂).induced g = t₁.induced g ⊔ t₂.induced g := (gc_induced_coinduced g).l_sup @[simp] lemma induced_supr {ι : Sort w} {t : ι → topological_space α} : (⨆i, t i).induced g = (⨆i, (t i).induced g) := (gc_induced_coinduced g).l_supr @[simp] lemma coinduced_top : (⊤ : topological_space α).coinduced f = ⊤ := (gc_induced_coinduced f).u_top @[simp] lemma coinduced_inf : (t₁ ⊓ t₂).coinduced f = t₁.coinduced f ⊓ t₂.coinduced f := (gc_induced_coinduced f).u_inf @[simp] lemma coinduced_infi {ι : Sort w} {t : ι → topological_space α} : (⨅i, t i).coinduced f = (⨅i, (t i).coinduced f) := (gc_induced_coinduced f).u_infi lemma induced_id [t : topological_space α] : t.induced id = t := topological_space_eq $ funext $ assume s, propext $ ⟨assume ⟨s', hs, h⟩, h ▸ hs, assume hs, ⟨s, hs, rfl⟩⟩ lemma induced_compose [tβ : topological_space β] [tγ : topological_space γ] {f : α → β} {g : β → γ} : (tγ.induced g).induced f = tγ.induced (g ∘ f) := topological_space_eq $ funext $ assume s, propext $ ⟨assume ⟨s', ⟨s, hs, h₂⟩, h₁⟩, h₁ ▸ h₂ ▸ ⟨s, hs, rfl⟩, assume ⟨s, hs, h⟩, ⟨preimage g s, ⟨s, hs, rfl⟩, h ▸ rfl⟩⟩ lemma coinduced_id [t : topological_space α] : t.coinduced id = t := topological_space_eq rfl lemma coinduced_compose [tα : topological_space α] {f : α → β} {g : β → γ} : (tα.coinduced f).coinduced g = tα.coinduced (g ∘ f) := topological_space_eq rfl end galois_connection /- constructions using the complete lattice structure -/ section constructions open topological_space variables {α : Type u} {β : Type v} instance inhabited_topological_space {α : Type u} : inhabited (topological_space α) := ⟨⊤⟩ instance : topological_space empty := ⊤ instance : discrete_topology empty := ⟨rfl⟩ instance : topological_space unit := ⊤ instance : discrete_topology unit := ⟨rfl⟩ instance : topological_space bool := ⊤ instance : discrete_topology bool := ⟨rfl⟩ instance : topological_space ℕ := ⊤ instance : discrete_topology ℕ := ⟨rfl⟩ instance : topological_space ℤ := ⊤ instance : discrete_topology ℤ := ⟨rfl⟩ instance sierpinski_space : topological_space Prop := generate_from {{true}} instance {p : α → Prop} [t : topological_space α] : topological_space (subtype p) := induced subtype.val 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 [topological_space α] : topological_space (list α) := topological_space.mk_of_nhds (traverse nhds) lemma nhds_list [topological_space α] (as : list α) : nhds as = traverse nhds as := begin refine nhds_mk_of_nhds _ _ _ _, { assume l, induction l, case list.nil { exact le_refl _ }, case list.cons : a l ih { suffices : list.cons <$> pure a <*> pure l ≤ list.cons <$> nhds a <*> traverse nhds l, { simpa only [-filter.pure_def] with functor_norm using this }, exact filter.seq_mono (filter.map_mono $ pure_le_nhds a) ih } }, { assume l s hs, rcases (mem_traverse_sets_iff _ _).1 hs with ⟨u, hu, hus⟩, clear as hs, have : ∃v:list (set α), l.forall₂ (λa s, is_open s ∧ a ∈ s) v ∧ sequence v ⊆ s, { induction hu generalizing s, case list.forall₂.nil : hs this { existsi [], simpa only [list.forall₂_nil_left_iff, exists_eq_left] }, case list.forall₂.cons : a s as ss ht h ih t hts { rcases mem_nhds_sets_iff.1 ht with ⟨u, hut, hu⟩, rcases ih (subset.refl _) with ⟨v, hv, hvss⟩, exact ⟨u::v, list.forall₂.cons hu hv, subset.trans (set.seq_mono (set.image_subset _ hut) hvss) hts⟩ } }, rcases this with ⟨v, hv, hvs⟩, refine ⟨sequence v, mem_traverse_sets _ _ _, hvs, _⟩, { exact hv.imp (assume a s ⟨hs, ha⟩, mem_nhds_sets hs ha) }, { assume u hu, have hu := (list.mem_traverse _ _).1 hu, have : list.forall₂ (λa s, is_open s ∧ a ∈ s) u v, { refine list.forall₂.flip _, replace hv := hv.flip, simp only [list.forall₂_and_left, flip] at ⊢ hv, exact ⟨hv.1, hu.flip⟩ }, refine mem_sets_of_superset _ hvs, exact mem_traverse_sets _ _ (this.imp $ assume a s ⟨hs, ha⟩, mem_nhds_sets hs ha) } } end lemma nhds_nil [topological_space α] : nhds ([] : list α) = pure [] := by rw [nhds_list, list.traverse_nil _]; apply_instance lemma nhds_cons [topological_space α] (a : α) (l : list α) : nhds (a :: l) = list.cons <$> nhds a <*> nhds l := by rw [nhds_list, list.traverse_cons _, ← nhds_list]; apply_instance lemma quotient_dense_of_dense [setoid α] [topological_space α] {s : set α} (H : ∀ x, x ∈ closure s) : closure (quotient.mk '' s) = univ := eq_univ_of_forall $ λ x, begin rw mem_closure_iff, intros U U_op x_in_U, let V := quotient.mk ⁻¹' U, cases quotient.exists_rep x with y y_x, have y_in_V : y ∈ V, by simp only [mem_preimage_eq, y_x, x_in_U], have V_op : is_open V := U_op, have : V ∩ s ≠ ∅ := mem_closure_iff.1 (H y) V V_op y_in_V, rcases exists_mem_of_ne_empty this with ⟨w, w_in_V, w_in_range⟩, exact ne_empty_of_mem ⟨w_in_V, mem_image_of_mem quotient.mk w_in_range⟩ end lemma generate_from_le {t : topological_space α} { g : set (set α) } (h : ∀s∈g, is_open s) : generate_from g ≤ t := generate_from_le_iff_subset_is_open.2 h lemma induced_generate_from_eq {α β} {b : set (set β)} {f : α → β} : (generate_from b).induced f = topological_space.generate_from (preimage f '' b) := le_antisymm (induced_le_iff_le_coinduced.2 $ generate_from_le $ assume s hs, generate_open.basic _ $ mem_image_of_mem _ hs) (generate_from_le $ ball_image_iff.2 $ assume s hs, ⟨s, generate_open.basic _ hs, rfl⟩) protected def topological_space.nhds_adjoint (a : α) (f : filter α) : topological_space α := { is_open := λs, a ∈ s → s ∈ f.sets, is_open_univ := assume s, univ_mem_sets, is_open_inter := assume s t hs ht ⟨has, hat⟩, inter_mem_sets (hs has) (ht hat), is_open_sUnion := assume k hk ⟨u, hu, hau⟩, mem_sets_of_superset (hk u hu hau) (subset_sUnion_of_mem hu) } lemma gc_nhds (a : α) : @galois_connection _ (order_dual (filter α)) _ _ (λt, @nhds α t a) (topological_space.nhds_adjoint a) := assume t (f : filter α), show f ≤ @nhds α t a ↔ _, from iff.intro (assume h s hs has, h $ @mem_nhds_sets α t a s hs has) (assume h, le_infi $ assume u, le_infi $ assume ⟨hau, hu⟩, le_principal_iff.2 $ h _ hu hau) lemma nhds_mono {t₁ t₂ : topological_space α} {a : α} (h : t₁ ≤ t₂) : @nhds α t₂ a ≤ @nhds α t₁ a := (gc_nhds a).monotone_l h lemma nhds_supr {ι : Sort*} {t : ι → topological_space α} {a : α} : @nhds α (supr t) a = (⨅i, @nhds α (t i) a) := (gc_nhds a).l_supr lemma nhds_Sup {s : set (topological_space α)} {a : α} : @nhds α (Sup s) a = (⨅t∈s, @nhds α t a) := (gc_nhds a).l_Sup lemma nhds_sup {t₁ t₂ : topological_space α} {a : α} : @nhds α (t₁ ⊔ t₂) a = @nhds α t₁ a ⊓ @nhds α t₂ a := (gc_nhds a).l_sup lemma nhds_bot {a : α} : @nhds α ⊥ a = ⊤ := (gc_nhds a).l_bot private lemma separated_by_f [tα : topological_space α] [tβ : topological_space β] [t2_space β] (f : α → β) (hf : induced f tβ ≤ tα) {x y : α} (h : f x ≠ f y) : ∃u v : set α, is_open u ∧ is_open v ∧ x ∈ u ∧ y ∈ v ∧ u ∩ v = ∅ := let ⟨u, v, uo, vo, xu, yv, uv⟩ := t2_separation h in ⟨f ⁻¹' u, f ⁻¹' v, hf _ ⟨u, uo, rfl⟩, hf _ ⟨v, vo, rfl⟩, xu, yv, by rw [←preimage_inter, uv, preimage_empty]⟩ instance {p : α → Prop} [t : topological_space α] [t2_space α] : t2_space (subtype p) := ⟨assume x y h, separated_by_f subtype.val (le_refl _) (mt subtype.eq h)⟩ instance [t₁ : topological_space α] [t2_space α] [t₂ : topological_space β] [t2_space β] : t2_space (α × β) := ⟨assume ⟨x₁,x₂⟩ ⟨y₁,y₂⟩ h, or.elim (not_and_distrib.mp (mt prod.ext_iff.mpr h)) (λ h₁, separated_by_f prod.fst le_sup_left h₁) (λ h₂, separated_by_f prod.snd le_sup_right h₂)⟩ instance Pi.t2_space {β : α → Type v} [t₂ : Πa, topological_space (β a)] [Πa, t2_space (β a)] : t2_space (Πa, β a) := ⟨assume x y h, let ⟨i, hi⟩ := not_forall.mp (mt funext h) in separated_by_f (λz, z i) (le_supr _ i) hi⟩ instance {p : α → Prop} [topological_space α] [discrete_topology α] : discrete_topology (subtype p) := ⟨top_unique $ assume s hs, ⟨subtype.val '' s, is_open_discrete _, (set.preimage_image_eq _ subtype.val_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_top, hβ.eq_top]⟩ 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_top]⟩ /- nhds in the induced topology -/ theorem mem_nhds_induced [T : topological_space α] (f : β → α) (a : β) (s : set β) : s ∈ (@nhds β (topological_space.induced f T) a).sets ↔ ∃ u ∈ (nhds (f a)).sets, f ⁻¹' u ⊆ s := begin simp only [nhds_sets, is_open_induced_iff, exists_prop, set.mem_set_of_eq], split, { rintros ⟨u, usub, ⟨v, openv, ueq⟩, au⟩, exact ⟨v, ⟨v, set.subset.refl v, openv, by rwa ←ueq at au⟩, by rw ueq; exact usub⟩ }, rintros ⟨u, ⟨v, vsubu, openv, amem⟩, finvsub⟩, exact ⟨f ⁻¹' v, set.subset.trans (set.preimage_mono vsubu) finvsub, ⟨⟨v, openv, rfl⟩, amem⟩⟩ end theorem nhds_induced [T : topological_space α] (f : β → α) (a : β) : @nhds β (topological_space.induced f T) a = comap f (nhds (f a)) := filter_eq $ by ext s; rw mem_nhds_induced; rw mem_comap_sets theorem map_nhds_induced_of_surjective [T : topological_space α] {f : β → α} (hf : function.surjective f) (a : β) (s : set α) : map f (@nhds β (topological_space.induced f T) a) = nhds (f a) := by rw [nhds_induced, map_comap_of_surjective hf] section topα variable [topological_space α] /- The nhds filter and the subspace topology. -/ theorem mem_nhds_subtype (s : set α) (a : {x // x ∈ s}) (t : set {x // x ∈ s}) : t ∈ (nhds a).sets ↔ ∃ u ∈ (nhds a.val).sets, (@subtype.val α s) ⁻¹' u ⊆ t := by rw mem_nhds_induced theorem nhds_subtype (s : set α) (a : {x // x ∈ s}) : nhds a = comap subtype.val (nhds a.val) := by rw nhds_induced theorem principal_subtype (s : set α) (t : set {x // x ∈ s}) : principal t = comap subtype.val (principal (subtype.val '' t)) := by rw comap_principal; rw set.preimage_image_eq; apply subtype.val_injective /- nhds_within and subtypes -/ theorem mem_nhds_within_subtype (s : set α) (a : {x // x ∈ s}) (t u : set {x // x ∈ s}) : t ∈ (nhds_within a u).sets ↔ t ∈ (comap (@subtype.val _ s) (nhds_within a.val (subtype.val '' u))).sets := by rw [nhds_within, nhds_subtype, principal_subtype, ←comap_inf, ←nhds_within] theorem nhds_within_subtype (s : set α) (a : {x // x ∈ s}) (t : set {x // x ∈ s}) : nhds_within a t = comap (@subtype.val _ s) (nhds_within a.val (subtype.val '' t)) := filter_eq $ by ext u; rw mem_nhds_within_subtype theorem nhds_within_eq_map_subtype_val {s : set α} {a : α} (h : a ∈ s) : nhds_within a s = map subtype.val (nhds ⟨a, h⟩) := have h₀ : s ∈ (nhds_within a s).sets, by { rw [mem_nhds_within], existsi set.univ, simp [set.diff_eq] }, have h₁ : ∀ y ∈ s, ∃ x, @subtype.val _ s x = y, from λ y h, ⟨⟨y, h⟩, rfl⟩, begin rw [←nhds_within_univ, nhds_within_subtype, subtype.val_image_univ], exact (map_comap_of_surjective' h₀ h₁).symm, end theorem tendsto_at_within_iff_subtype {s : set α} {a : α} (h : a ∈ s) (f : α → β) (l : filter β) : tendsto f (nhds_within a s) l ↔ tendsto (function.restrict f s) (nhds ⟨a, h⟩) l := by rw [tendsto, tendsto, function.restrict, nhds_within_eq_map_subtype_val h, ←(@filter.map_map _ _ _ _ subtype.val)] end topα end constructions namespace topological_space /- countability axioms For our applications we are interested that there exists a countable basis, but we do not need the concrete basis itself. This allows us to declare these type classes as `Prop` to use them as mixins. -/ variables {α : Type u} [t : topological_space α] include t /-- A topological basis is one that satisfies the necessary conditions so that it suffices to take unions of the basis sets to get a topology (without taking finite intersections as well). -/ def is_topological_basis (s : set (set α)) : Prop := (∀t₁∈s, ∀t₂∈s, ∀ x ∈ t₁ ∩ t₂, ∃ t₃∈s, x ∈ t₃ ∧ t₃ ⊆ t₁ ∩ t₂) ∧ (⋃₀ s) = univ ∧ t = generate_from s lemma is_topological_basis_of_subbasis {s : set (set α)} (hs : t = generate_from s) : is_topological_basis ((λf, ⋂₀ f) '' {f:set (set α) | finite f ∧ f ⊆ s ∧ ⋂₀ f ≠ ∅}) := let b' := (λf, ⋂₀ f) '' {f:set (set α) | finite f ∧ f ⊆ s ∧ ⋂₀ f ≠ ∅} in ⟨assume s₁ ⟨t₁, ⟨hft₁, ht₁b, ht₁⟩, eq₁⟩ s₂ ⟨t₂, ⟨hft₂, ht₂b, ht₂⟩, eq₂⟩, have ie : ⋂₀(t₁ ∪ t₂) = ⋂₀ t₁ ∩ ⋂₀ t₂, from Inf_union, eq₁ ▸ eq₂ ▸ assume x h, ⟨_, ⟨t₁ ∪ t₂, ⟨finite_union hft₁ hft₂, union_subset ht₁b ht₂b, by simpa only [ie] using ne_empty_of_mem h⟩, ie⟩, h, subset.refl _⟩, eq_univ_iff_forall.2 $ assume a, ⟨univ, ⟨∅, ⟨finite_empty, empty_subset _, by rw sInter_empty; exact nonempty_iff_univ_ne_empty.1 ⟨a⟩⟩, sInter_empty⟩, mem_univ _⟩, have generate_from s = generate_from b', from le_antisymm (generate_from_le $ assume s hs, by_cases (assume : s = ∅, by rw [this]; apply @is_open_empty _ _) (assume : s ≠ ∅, generate_open.basic _ ⟨{s}, ⟨finite_singleton s, singleton_subset_iff.2 hs, by rwa [sInter_singleton]⟩, sInter_singleton s⟩)) (generate_from_le $ assume u ⟨t, ⟨hft, htb, ne⟩, eq⟩, eq ▸ @is_open_sInter _ (generate_from s) _ hft (assume s hs, generate_open.basic _ $ htb hs)), this ▸ hs⟩ lemma is_topological_basis_of_open_of_nhds {s : set (set α)} (h_open : ∀ u ∈ s, _root_.is_open u) (h_nhds : ∀(a:α) (u : set α), a ∈ u → _root_.is_open u → ∃v ∈ s, a ∈ v ∧ v ⊆ u) : is_topological_basis s := ⟨assume t₁ ht₁ t₂ ht₂ x ⟨xt₁, xt₂⟩, h_nhds x (t₁ ∩ t₂) ⟨xt₁, xt₂⟩ (is_open_inter _ _ _ (h_open _ ht₁) (h_open _ ht₂)), eq_univ_iff_forall.2 $ assume a, let ⟨u, h₁, h₂, _⟩ := h_nhds a univ trivial (is_open_univ _) in ⟨u, h₁, h₂⟩, le_antisymm (assume u hu, (@is_open_iff_nhds α (generate_from _) _).mpr $ assume a hau, let ⟨v, hvs, hav, hvu⟩ := h_nhds a u hau hu in by rw nhds_generate_from; exact infi_le_of_le v (infi_le_of_le ⟨hav, hvs⟩ $ le_principal_iff.2 hvu)) (generate_from_le h_open)⟩ lemma mem_nhds_of_is_topological_basis {a : α} {s : set α} {b : set (set α)} (hb : is_topological_basis b) : s ∈ (nhds a).sets ↔ ∃t∈b, a ∈ t ∧ t ⊆ s := begin rw [hb.2.2, nhds_generate_from, infi_sets_eq'], { simp only [mem_bUnion_iff, exists_prop, mem_set_of_eq, and_assoc, and.left_comm]; refl }, { exact assume s ⟨hs₁, hs₂⟩ t ⟨ht₁, ht₂⟩, have a ∈ s ∩ t, from ⟨hs₁, ht₁⟩, let ⟨u, hu₁, hu₂, hu₃⟩ := hb.1 _ hs₂ _ ht₂ _ this in ⟨u, ⟨hu₂, hu₁⟩, le_principal_iff.2 (subset.trans hu₃ (inter_subset_left _ _)), le_principal_iff.2 (subset.trans hu₃ (inter_subset_right _ _))⟩ }, { rcases eq_univ_iff_forall.1 hb.2.1 a with ⟨i, h1, h2⟩, exact ⟨i, h2, h1⟩ } end lemma is_open_of_is_topological_basis {s : set α} {b : set (set α)} (hb : is_topological_basis b) (hs : s ∈ b) : _root_.is_open s := is_open_iff_mem_nhds.2 $ λ a as, (mem_nhds_of_is_topological_basis hb).2 ⟨s, hs, as, subset.refl _⟩ lemma mem_basis_subset_of_mem_open {b : set (set α)} (hb : is_topological_basis b) {a:α} {u : set α} (au : a ∈ u) (ou : _root_.is_open u) : ∃v ∈ b, a ∈ v ∧ v ⊆ u := (mem_nhds_of_is_topological_basis hb).1 $ mem_nhds_sets ou au lemma sUnion_basis_of_is_open {B : set (set α)} (hB : is_topological_basis B) {u : set α} (ou : _root_.is_open u) : ∃ S ⊆ B, u = ⋃₀ S := ⟨{s ∈ B | s ⊆ u}, λ s h, h.1, set.ext $ λ a, ⟨λ ha, let ⟨b, hb, ab, bu⟩ := mem_basis_subset_of_mem_open hB ha ou in ⟨b, ⟨hb, bu⟩, ab⟩, λ ⟨b, ⟨hb, bu⟩, ab⟩, bu ab⟩⟩ lemma Union_basis_of_is_open {B : set (set α)} (hB : is_topological_basis B) {u : set α} (ou : _root_.is_open u) : ∃ (β : Type u) (f : β → set α), u = (⋃ i, f i) ∧ ∀ i, f i ∈ B := let ⟨S, sb, su⟩ := sUnion_basis_of_is_open hB ou in ⟨S, subtype.val, su.trans set.sUnion_eq_Union, λ ⟨b, h⟩, sb h⟩ variables (α) /-- A separable space is one with a countable dense subset. -/ class separable_space : Prop := (exists_countable_closure_eq_univ : ∃s:set α, countable s ∧ closure s = univ) /-- A first-countable space is one in which every point has a countable neighborhood basis. -/ class first_countable_topology : Prop := (nhds_generated_countable : ∀a:α, ∃s:set (set α), countable s ∧ nhds a = (⨅t∈s, principal t)) /-- A second-countable space is one with a countable basis. -/ class second_countable_topology : Prop := (is_open_generated_countable : ∃b:set (set α), countable b ∧ t = topological_space.generate_from b) instance second_countable_topology.to_first_countable_topology [second_countable_topology α] : first_countable_topology α := let ⟨b, hb, eq⟩ := second_countable_topology.is_open_generated_countable α in ⟨assume a, ⟨{s | a ∈ s ∧ s ∈ b}, countable_subset (assume x ⟨_, hx⟩, hx) hb, by rw [eq, nhds_generate_from]⟩⟩ lemma second_countable_topology_induced (β) [t : topological_space β] [second_countable_topology β] (f : α → β) : @second_countable_topology α (t.induced f) := begin rcases second_countable_topology.is_open_generated_countable β with ⟨b, hb, eq⟩, refine { is_open_generated_countable := ⟨preimage f '' b, countable_image _ hb, _⟩ }, rw [eq, induced_generate_from_eq] end instance subtype.second_countable_topology (s : set α) [topological_space α] [second_countable_topology α] : second_countable_topology s := second_countable_topology_induced s α coe lemma is_open_generated_countable_inter [second_countable_topology α] : ∃b:set (set α), countable b ∧ ∅ ∉ b ∧ is_topological_basis b := let ⟨b, hb₁, hb₂⟩ := second_countable_topology.is_open_generated_countable α in let b' := (λs, ⋂₀ s) '' {s:set (set α) | finite s ∧ s ⊆ b ∧ ⋂₀ s ≠ ∅} in ⟨b', countable_image _ $ countable_subset (by simp only [(and_assoc _ _).symm]; exact inter_subset_left _ _) (countable_set_of_finite_subset hb₁), assume ⟨s, ⟨_, _, hn⟩, hp⟩, hn hp, is_topological_basis_of_subbasis hb₂⟩ instance second_countable_topology.to_separable_space [second_countable_topology α] : separable_space α := let ⟨b, hb₁, hb₂, hb₃, hb₄, eq⟩ := is_open_generated_countable_inter α in have nhds_eq : ∀a, nhds a = (⨅ s : {s : set α // a ∈ s ∧ s ∈ b}, principal s.val), by intro a; rw [eq, nhds_generate_from, infi_subtype]; refl, have ∀s∈b, ∃a, a ∈ s, from assume s hs, exists_mem_of_ne_empty $ assume eq, hb₂ $ eq ▸ hs, have ∃f:∀s∈b, α, ∀s h, f s h ∈ s, by simp only [skolem] at this; exact this, let ⟨f, hf⟩ := this in ⟨⟨(⋃s∈b, ⋃h:s∈b, {f s h}), countable_bUnion hb₁ (λ _ _, countable_Union_Prop $ λ _, countable_singleton _), set.ext $ assume a, have a ∈ (⋃₀ b), by rw [hb₄]; exact trivial, let ⟨t, ht₁, ht₂⟩ := this in have w : {s : set α // a ∈ s ∧ s ∈ b}, from ⟨t, ht₂, ht₁⟩, suffices (⨅ (x : {s // a ∈ s ∧ s ∈ b}), principal (x.val ∩ ⋃s (h₁ h₂ : s ∈ b), {f s h₂})) ≠ ⊥, by simpa only [closure_eq_nhds, nhds_eq, infi_inf w, inf_principal, mem_set_of_eq, mem_univ, iff_true], infi_neq_bot_of_directed ⟨a⟩ (assume ⟨s₁, has₁, hs₁⟩ ⟨s₂, has₂, hs₂⟩, have a ∈ s₁ ∩ s₂, from ⟨has₁, has₂⟩, let ⟨s₃, hs₃, has₃, hs⟩ := hb₃ _ hs₁ _ hs₂ _ this in ⟨⟨s₃, has₃, hs₃⟩, begin simp only [le_principal_iff, mem_principal_sets, (≥)], simp only [subset_inter_iff] at hs, split; apply inter_subset_inter_left; simp only [hs] end⟩) (assume ⟨s, has, hs⟩, have s ∩ (⋃ (s : set α) (H h : s ∈ b), {f s h}) ≠ ∅, from ne_empty_of_mem ⟨hf _ hs, mem_bUnion hs $ mem_Union.mpr ⟨hs, mem_singleton _⟩⟩, mt principal_eq_bot_iff.1 this) ⟩⟩ variables {α} lemma is_open_Union_countable [second_countable_topology α] {ι} (s : ι → set α) (H : ∀ i, _root_.is_open (s i)) : ∃ T : set ι, countable T ∧ (⋃ i ∈ T, s i) = ⋃ i, s i := let ⟨B, cB, _, bB⟩ := is_open_generated_countable_inter α in begin let B' := {b ∈ B | ∃ i, b ⊆ s i}, choose f hf using λ b:B', b.2.2, haveI : encodable B' := (countable_subset (sep_subset _ _) cB).to_encodable, refine ⟨_, countable_range f, subset.antisymm (bUnion_subset_Union _ _) (sUnion_subset _)⟩, rintro _ ⟨i, rfl⟩ x xs, rcases mem_basis_subset_of_mem_open bB xs (H _) with ⟨b, hb, xb, bs⟩, exact ⟨_, ⟨_, rfl⟩, _, ⟨⟨⟨_, hb, _, bs⟩, rfl⟩, rfl⟩, hf _ (by exact xb)⟩ end lemma is_open_sUnion_countable [second_countable_topology α] (S : set (set α)) (H : ∀ s ∈ S, _root_.is_open s) : ∃ T : set (set α), countable T ∧ T ⊆ S ∧ ⋃₀ T = ⋃₀ S := let ⟨T, cT, hT⟩ := is_open_Union_countable (λ s:S, s.1) (λ s, H s.1 s.2) in ⟨subtype.val '' T, countable_image _ cT, image_subset_iff.2 $ λ ⟨x, xs⟩ xt, xs, by rwa [sUnion_image, sUnion_eq_Union]⟩ variable (α) def opens := {s : set α // _root_.is_open s} variable {α} namespace opens instance : has_coe (opens α) (set α) := { coe := subtype.val } instance : has_subset (opens α) := { subset := λ U V, U.val ⊆ V.val } instance : has_mem α (opens α) := { mem := λ a U, a ∈ U.val } @[extensionality] lemma ext {U V : opens α} (h : U.val = V.val) : U = V := subtype.ext.mpr h instance : partial_order (opens α) := subtype.partial_order _ def interior (s : set α) : opens α := ⟨interior s, is_open_interior⟩ def gc : galois_connection (subtype.val : opens α → set α) interior := λ U s, ⟨λ h, interior_maximal h U.property, λ h, le_trans h interior_subset⟩ def gi : @galois_insertion (order_dual (set α)) (order_dual (opens α)) _ _ interior (subtype.val) := { choice := λ s hs, ⟨s, interior_eq_iff_open.mp $ le_antisymm interior_subset hs⟩, gc := gc.dual, le_l_u := λ _, interior_subset, choice_eq := λ s hs, le_antisymm interior_subset hs } @[simp] lemma gi_choice_val {s : order_dual (set α)} {hs} : (gi.choice s hs).val = s := rfl instance : complete_lattice (opens α) := complete_lattice.copy (@order_dual.lattice.complete_lattice _ (@galois_insertion.lift_complete_lattice (order_dual (set α)) (order_dual (opens α)) _ interior (subtype.val : opens α → set α) _ gi)) /- le -/ (λ U V, U.1 ⊆ V.1) rfl /- top -/ ⟨set.univ, _root_.is_open_univ⟩ (subtype.ext.mpr interior_univ.symm) /- bot -/ ⟨∅, is_open_empty⟩ rfl /- sup -/ (λ U V, ⟨U.1 ∪ V.1, _root_.is_open_union U.2 V.2⟩) rfl /- inf -/ (λ U V, ⟨U.1 ∩ V.1, _root_.is_open_inter U.2 V.2⟩) begin funext, apply subtype.ext.mpr, symmetry, apply interior_eq_of_open, exact (_root_.is_open_inter U.2 V.2), end /- Sup -/ (λ Us, ⟨⋃₀ (subtype.val '' Us), _root_.is_open_sUnion $ λ U hU, by { rcases hU with ⟨⟨V, hV⟩, h, h'⟩, dsimp at h', subst h', exact hV}⟩) begin funext, apply subtype.ext.mpr, simp [Sup_range], refl, end /- Inf -/ _ rfl instance : has_inter (opens α) := ⟨λ U V, U ⊓ V⟩ instance : has_union (opens α) := ⟨λ U V, U ⊔ V⟩ instance : has_emptyc (opens α) := ⟨⊥⟩ @[simp] lemma inter_eq (U V : opens α) : U ∩ V = U ⊓ V := rfl @[simp] lemma union_eq (U V : opens α) : U ∪ V = U ⊔ V := rfl @[simp] lemma empty_eq : (∅ : opens α) = ⊥ := rfl @[simp] lemma Sup_s {Us : set (opens α)} : (Sup Us).val = ⋃₀ (subtype.val '' Us) := begin rw [@galois_connection.l_Sup (opens α) (set α) _ _ (subtype.val : opens α → set α) interior gc Us, set.sUnion_image], congr end def is_basis (B : set (opens α)) : Prop := is_topological_basis (subtype.val '' B) lemma is_basis_iff_nbhd {B : set (opens α)} : is_basis B ↔ ∀ {U : opens α} {x}, x ∈ U → ∃ U' ∈ B, x ∈ U' ∧ U' ⊆ U := begin split; intro h, { rintros ⟨sU, hU⟩ x hx, rcases (mem_nhds_of_is_topological_basis h).mp (mem_nhds_sets hU hx) with ⟨sV, ⟨⟨V, H₁, H₂⟩, hsV⟩⟩, refine ⟨V, H₁, _⟩, cases V, dsimp at H₂, subst H₂, exact hsV }, { refine is_topological_basis_of_open_of_nhds _ _, { rintros sU ⟨U, ⟨H₁, H₂⟩⟩, subst H₂, exact U.property }, { intros x sU hx hsU, rcases @h (⟨sU, hsU⟩ : opens α) x hx with ⟨V, hV, H⟩, exact ⟨V, ⟨V, hV, rfl⟩, H⟩ } } end lemma is_basis_iff_cover {B : set (opens α)} : is_basis B ↔ ∀ U : opens α, ∃ Us ⊆ B, U = Sup Us := begin split, { intros hB U, rcases sUnion_basis_of_is_open hB U.property with ⟨sUs, H, hU⟩, existsi {U : opens α | U ∈ B ∧ U.val ∈ sUs}, split, { intros U hU, exact hU.left }, { apply ext, rw [Sup_s, hU], congr, ext s; split; intro hs, { rcases H hs with ⟨V, hV⟩, rw ← hV.right at hs, refine ⟨V, ⟨⟨hV.left, hs⟩, hV.right⟩⟩ }, { rcases hs with ⟨V, ⟨⟨H₁, H₂⟩, H₃⟩⟩, subst H₃, exact H₂ } } }, { intro h, rw is_basis_iff_nbhd, intros U x hx, rcases h U with ⟨Us, hUs, H⟩, replace H := congr_arg subtype.val H, rw Sup_s at H, change x ∈ U.val at hx, rw H at hx, rcases set.mem_sUnion.mp hx with ⟨sV, ⟨⟨V, H₁, H₂⟩, hsV⟩⟩, refine ⟨V,hUs H₁,_⟩, cases V with V hV, dsimp at H₂, subst H₂, refine ⟨hsV,_⟩, change V ⊆ U.val, rw H, exact set.subset_sUnion_of_mem ⟨⟨V, _⟩, ⟨H₁, rfl⟩⟩ } end end opens end topological_space section limit variables {α : Type u} [inhabited α] [topological_space α] open classical /-- If `f` is a filter, then `lim f` is a limit of the filter, if it exists. -/ noncomputable def lim (f : filter α) : α := epsilon $ λa, f ≤ nhds a lemma lim_spec {f : filter α} (h : ∃a, f ≤ nhds a) : f ≤ nhds (lim f) := epsilon_spec h variables [t2_space α] {f : filter α} lemma lim_eq {a : α} (hf : f ≠ ⊥) (h : f ≤ nhds a) : lim f = a := eq_of_nhds_neq_bot $ neq_bot_of_le_neq_bot hf $ le_inf (lim_spec ⟨_, h⟩) h @[simp] lemma lim_nhds_eq {a : α} : lim (nhds a) = a := lim_eq nhds_neq_bot (le_refl _) @[simp] lemma lim_nhds_eq_of_closure {a : α} {s : set α} (h : a ∈ closure s) : lim (nhds a ⊓ principal s) = a := lim_eq begin rw [closure_eq_nhds] at h, exact h end inf_le_left end limit
1b8f8fd0ee1dd8faf2346bc3330a1c413a48c8f0
a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940
/src/Lean/Elab/PreDefinition/Basic.lean
24375f497d5332da5434a194f04d3843ba29f6b9
[ "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
6,637
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Util.SCC import Lean.Meta.AbstractNestedProofs import Lean.Elab.Term import Lean.Elab.DefView import Lean.Elab.PreDefinition.MkInhabitant namespace Lean.Elab open Meta open Term /- A (potentially recursive) definition. The elaborator converts it into Kernel definitions using many different strategies. -/ structure PreDefinition where ref : Syntax kind : DefKind levelParams : List Name modifiers : Modifiers declName : Name type : Expr value : Expr deriving Inhabited def instantiateMVarsAtPreDecls (preDefs : Array PreDefinition) : TermElabM (Array PreDefinition) := preDefs.mapM fun preDef => do pure { preDef with type := (← instantiateMVars preDef.type), value := (← instantiateMVars preDef.value) } private def levelMVarToParamPreDeclsAux (preDefs : Array PreDefinition) : StateRefT Nat TermElabM (Array PreDefinition) := preDefs.mapM fun preDef => do pure { preDef with type := (← levelMVarToParam' preDef.type), value := (← levelMVarToParam' preDef.value) } def levelMVarToParamPreDecls (preDefs : Array PreDefinition) : TermElabM (Array PreDefinition) := (levelMVarToParamPreDeclsAux preDefs).run' 1 private def getLevelParamsPreDecls (preDefs : Array PreDefinition) (scopeLevelNames allUserLevelNames : List Name) : TermElabM (List Name) := do let mut s : CollectLevelParams.State := {} for preDef in preDefs do s := collectLevelParams s preDef.type s := collectLevelParams s preDef.value match sortDeclLevelParams scopeLevelNames allUserLevelNames s.params with | Except.error msg => throwError msg | Except.ok levelParams => pure levelParams def fixLevelParams (preDefs : Array PreDefinition) (scopeLevelNames allUserLevelNames : List Name) : TermElabM (Array PreDefinition) := do -- We used to use `shareCommon` here, but is was a bottleneck let levelParams ← getLevelParamsPreDecls preDefs scopeLevelNames allUserLevelNames let us := levelParams.map mkLevelParam let fixExpr (e : Expr) : Expr := e.replace fun c => match c with | Expr.const declName _ _ => if preDefs.any fun preDef => preDef.declName == declName then some $ Lean.mkConst declName us else none | _ => none pure $ preDefs.map fun preDef => { preDef with type := fixExpr preDef.type, value := fixExpr preDef.value, levelParams := levelParams } def applyAttributesOf (preDefs : Array PreDefinition) (applicationTime : AttributeApplicationTime) : TermElabM Unit := do for preDef in preDefs do applyAttributesAt preDef.declName preDef.modifiers.attrs applicationTime def abstractNestedProofs (preDef : PreDefinition) : MetaM PreDefinition := if preDef.kind.isTheorem || preDef.kind.isExample then pure preDef else do let value ← Meta.abstractNestedProofs preDef.declName preDef.value pure { preDef with value := value } /- Auxiliary method for (temporarily) adding pre definition as an axiom -/ def addAsAxiom (preDef : PreDefinition) : MetaM Unit := do withRef preDef.ref do addDecl <| Declaration.axiomDecl { name := preDef.declName, levelParams := preDef.levelParams, type := preDef.type, isUnsafe := preDef.modifiers.isUnsafe } private def shouldGenCodeFor (preDef : PreDefinition) : Bool := !preDef.kind.isTheorem && !preDef.modifiers.isNoncomputable private def addNonRecAux (preDef : PreDefinition) (compile : Bool) : TermElabM Unit := withRef preDef.ref do let preDef ← abstractNestedProofs preDef let env ← getEnv let decl := match preDef.kind with | DefKind.«theorem» => Declaration.thmDecl { name := preDef.declName, levelParams := preDef.levelParams, type := preDef.type, value := preDef.value } | DefKind.«opaque» => Declaration.opaqueDecl { name := preDef.declName, levelParams := preDef.levelParams, type := preDef.type, value := preDef.value, isUnsafe := preDef.modifiers.isUnsafe } | DefKind.«abbrev» => Declaration.defnDecl { name := preDef.declName, levelParams := preDef.levelParams, type := preDef.type, value := preDef.value, hints := ReducibilityHints.«abbrev», safety := if preDef.modifiers.isUnsafe then DefinitionSafety.unsafe else DefinitionSafety.safe } | _ => -- definitions and examples Declaration.defnDecl { name := preDef.declName, levelParams := preDef.levelParams, type := preDef.type, value := preDef.value, hints := ReducibilityHints.regular (getMaxHeight env preDef.value + 1), safety := if preDef.modifiers.isUnsafe then DefinitionSafety.unsafe else DefinitionSafety.safe } addDecl decl applyAttributesOf #[preDef] AttributeApplicationTime.afterTypeChecking if compile && shouldGenCodeFor preDef then compileDecl decl applyAttributesOf #[preDef] AttributeApplicationTime.afterCompilation def addAndCompileNonRec (preDef : PreDefinition) : TermElabM Unit := do addNonRecAux preDef true def addNonRec (preDef : PreDefinition) : TermElabM Unit := do addNonRecAux preDef false def addAndCompileUnsafe (preDefs : Array PreDefinition) (safety := DefinitionSafety.unsafe) : TermElabM Unit := withRef preDefs[0].ref do let decl := Declaration.mutualDefnDecl $ preDefs.toList.map fun preDef => { name := preDef.declName levelParams := preDef.levelParams type := preDef.type value := preDef.value safety := safety hints := ReducibilityHints.opaque } addDecl decl applyAttributesOf preDefs AttributeApplicationTime.afterTypeChecking compileDecl decl applyAttributesOf preDefs AttributeApplicationTime.afterCompilation pure () def addAndCompilePartialRec (preDefs : Array PreDefinition) : TermElabM Unit := do if preDefs.all shouldGenCodeFor then addAndCompileUnsafe (safety := DefinitionSafety.partial) <| preDefs.map fun preDef => { preDef with declName := Compiler.mkUnsafeRecName preDef.declName, value := preDef.value.replace fun e => match e with | Expr.const declName us _ => if preDefs.any fun preDef => preDef.declName == declName then some $ mkConst (Compiler.mkUnsafeRecName declName) us else none | _ => none, modifiers := {} } end Lean.Elab
77a4c47393964f511cd532b218f33eec8542f6e1
4efff1f47634ff19e2f786deadd394270a59ecd2
/src/control/bitraversable/basic.lean
9089230c2936dfb5e6a012e84f250974f51b7155
[ "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
2,965
lean
/- Copyright (c) 2018 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Simon Hudon -/ import control.bifunctor import control.traversable.basic /-! # Bitraversable type class Type class for traversing bifunctors. The concepts and laws are taken from <https://hackage.haskell.org/package/base-4.12.0.0/docs/Data-Bitraversable.html> Simple examples of `bitraversable` are `prod` and `sum`. A more elaborate example is to define an a-list as: ``` def alist (key val : Type) := list (key × val) ``` Then we can use `f : key → io key'` and `g : val → io val'` to manipulate the `alist`'s key and value respectively with `bitraverse f g : alist key val → io (alist key' val')` ## Main definitions * bitraversable - exposes the `bitraverse` function * is_lawful_bitraversable - laws similar to is_lawful_traversable ## Tags traversable bitraversable iterator functor bifunctor applicative -/ universes u section prio set_option default_priority 100 -- see Note [default priority] class bitraversable (t : Type u → Type u → Type u) extends bifunctor t := (bitraverse : Π {m : Type u → Type u} [applicative m] {α α' β β'}, (α → m α') → (β → m β') → t α β → m (t α' β')) end prio export bitraversable ( bitraverse ) def bisequence {t m} [bitraversable t] [applicative m] {α β} : t (m α) (m β) → m (t α β) := bitraverse id id open functor section prio set_option default_priority 100 -- see Note [default priority] class is_lawful_bitraversable (t : Type u → Type u → Type u) [bitraversable t] extends is_lawful_bifunctor t := (id_bitraverse : ∀ {α β} (x : t α β), bitraverse id.mk id.mk x = id.mk x ) (comp_bitraverse : ∀ {F G} [applicative F] [applicative G] [is_lawful_applicative F] [is_lawful_applicative G] {α α' β β' γ γ'} (f : β → F γ) (f' : β' → F γ') (g : α → G β) (g' : α' → G β') (x : t α α'), bitraverse (comp.mk ∘ map f ∘ g) (comp.mk ∘ map f' ∘ g') x = comp.mk (bitraverse f f' <$> bitraverse g g' x) ) (bitraverse_eq_bimap_id : ∀ {α α' β β'} (f : α → β) (f' : α' → β') (x : t α α'), bitraverse (id.mk ∘ f) (id.mk ∘ f') x = id.mk (bimap f f' x)) (binaturality : ∀ {F G} [applicative F] [applicative G] [is_lawful_applicative F] [is_lawful_applicative G] (η : applicative_transformation F G) {α α' β β'} (f : α → F β) (f' : α' → F β') (x : t α α'), η (bitraverse f f' x) = bitraverse (@η _ ∘ f) (@η _ ∘ f') x) end prio export is_lawful_bitraversable ( id_bitraverse comp_bitraverse bitraverse_eq_bimap_id ) open is_lawful_bitraversable attribute [higher_order bitraverse_id_id] id_bitraverse attribute [higher_order bitraverse_comp] comp_bitraverse attribute [higher_order] binaturality bitraverse_eq_bimap_id export is_lawful_bitraversable (bitraverse_id_id bitraverse_comp)
17c314b8237934f5c1667be680e92ba9dc449326
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/pkg/builtin_attr/UserAttr/BlaAttr.lean
a931e061e7e49560743b9585947e5ccd52b872e2
[ "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
188
lean
import Lean open Lean -- initialize discard <| registerTagAttribute `foo "" initialize registerBuiltinAttribute { name := `bar, descr := "", add := fun _ _ _ => pure () }
6b8625c2efd94edde0578c7173618c292eb6a644
19cc34575500ee2e3d4586c15544632aa07a8e66
/src/data/set/basic.lean
9aaa45a52712ed5a2658b57d8282f6637e7a2532
[ "Apache-2.0" ]
permissive
LibertasSpZ/mathlib
b9fcd46625eb940611adb5e719a4b554138dade6
33f7870a49d7cc06d2f3036e22543e6ec5046e68
refs/heads/master
1,672,066,539,347
1,602,429,158,000
1,602,429,158,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
89,915
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura -/ import logic.unique import order.boolean_algebra /-! # Basic properties of sets Sets in Lean are homogeneous; all their elements have the same type. Sets whose elements have type `X` are thus defined as `set X := X → Prop`. Note that this function need not be decidable. The definition is in the core library. This file provides some basic definitions related to sets and functions not present in the core library, as well as extra lemmas for functions in the core library (empty set, univ, union, intersection, insert, singleton, set-theoretic difference, complement, and powerset). Note that a set is a term, not a type. There is a coersion from `set α` to `Type*` sending `s` to the corresponding subtype `↥s`. See also the file `set_theory/zfc.lean`, which contains an encoding of ZFC set theory in Lean. ## Main definitions Notation used here: - `f : α → β` is a function, - `s : set α` and `s₁ s₂ : set α` are subsets of `α` - `t : set β` is a subset of `β`. Definitions in the file: * `strict_subset s₁ s₂ : Prop` : the predicate `s₁ ⊆ s₂` but `s₁ ≠ s₂`. * `nonempty s : Prop` : the predicate `s ≠ ∅`. Note that this is the preferred way to express the fact that `s` has an element (see the Implementation Notes). * `preimage f t : set α` : the preimage f⁻¹(t) (written `f ⁻¹' t` in Lean) of a subset of β. * `subsingleton s : Prop` : the predicate saying that `s` has at most one element. * `range f : set β` : the image of `univ` under `f`. Also works for `{p : Prop} (f : p → α)` (unlike `image`) * `prod s t : set (α × β)` : the subset `s × t`. * `inclusion s₁ s₂ : ↥s₁ → ↥s₂` : the map `↥s₁ → ↥s₂` induced by an inclusion `s₁ ⊆ s₂`. ## Notation * `f ⁻¹' t` for `preimage f t` * `f '' s` for `image f s` * `sᶜ` for the complement of `s` ## Implementation notes * `s.nonempty` is to be preferred to `s ≠ ∅` or `∃ x, x ∈ s`. It has the advantage that the `s.nonempty` dot notation can be used. * For `s : set α`, do not use `subtype s`. Instead use `↥s` or `(s : Type*)` or `s`. ## Tags set, sets, subset, subsets, image, preimage, pre-image, range, union, intersection, insert, singleton, complement, powerset -/ /-! ### Set coercion to a type -/ open function universe variables u v w x run_cmd do e ← tactic.get_env, tactic.set_env $ e.mk_protected `set.compl namespace set variable {α : Type*} instance : has_le (set α) := ⟨(⊆)⟩ instance : has_lt (set α) := ⟨λ s t, s ≤ t ∧ ¬t ≤ s⟩ instance {α : Type*} : boolean_algebra (set α) := { sup := (∪), le := (≤), lt := (<), inf := (∩), bot := ∅, compl := set.compl, top := univ, sdiff := (\), .. (infer_instance : boolean_algebra (α → Prop)) } @[simp] lemma bot_eq_empty : (⊥ : set α) = ∅ := rfl @[simp] lemma sup_eq_union (s t : set α) : s ⊔ t = s ∪ t := rfl @[simp] lemma inf_eq_inter (s t : set α) : s ⊓ t = s ∩ t := rfl @[simp] lemma le_eq_subset (s t : set α) : s ≤ t = (s ⊆ t) := rfl /-- Coercion from a set to the corresponding subtype. -/ instance {α : Type*} : has_coe_to_sort (set α) := ⟨_, λ s, {x // x ∈ s}⟩ end set section set_coe variables {α : Type u} theorem set.set_coe_eq_subtype (s : set α) : coe_sort.{(u+1) (u+2)} s = {x // x ∈ s} := rfl @[simp] theorem set_coe.forall {s : set α} {p : s → Prop} : (∀ x : s, p x) ↔ (∀ x (h : x ∈ s), p ⟨x, h⟩) := subtype.forall @[simp] theorem set_coe.exists {s : set α} {p : s → Prop} : (∃ x : s, p x) ↔ (∃ x (h : x ∈ s), p ⟨x, h⟩) := subtype.exists theorem set_coe.exists' {s : set α} {p : Π x, x ∈ s → Prop} : (∃ x (h : x ∈ s), p x h) ↔ (∃ x : s, p x x.2) := (@set_coe.exists _ _ $ λ x, p x.1 x.2).symm theorem set_coe.forall' {s : set α} {p : Π x, x ∈ s → Prop} : (∀ x (h : x ∈ s), p x h) ↔ (∀ x : s, p x x.2) := (@set_coe.forall _ _ $ λ x, p x.1 x.2).symm @[simp] theorem set_coe_cast : ∀ {s t : set α} (H' : s = t) (H : @eq (Type u) s t) (x : s), cast H x = ⟨x.1, H' ▸ x.2⟩ | s _ rfl _ ⟨x, h⟩ := rfl theorem set_coe.ext {s : set α} {a b : s} : (↑a : α) = ↑b → a = b := subtype.eq theorem set_coe.ext_iff {s : set α} {a b : s} : (↑a : α) = ↑b ↔ a = b := iff.intro set_coe.ext (assume h, h ▸ rfl) end set_coe /-- See also `subtype.prop` -/ lemma subtype.mem {α : Type*} {s : set α} (p : s) : (p : α) ∈ s := p.prop lemma eq.subset {α} {s t : set α} : s = t → s ⊆ t := by { rintro rfl x hx, exact hx } namespace set variables {α : Type u} {β : Type v} {γ : Type w} {ι : Sort x} {a : α} {s t : set α} instance : inhabited (set α) := ⟨∅⟩ @[ext] theorem ext {a b : set α} (h : ∀ x, x ∈ a ↔ x ∈ b) : a = b := funext (assume x, propext (h x)) theorem ext_iff {s t : set α} : s = t ↔ ∀ x, x ∈ s ↔ x ∈ t := ⟨λ h x, by rw h, ext⟩ @[trans] theorem mem_of_mem_of_subset {x : α} {s t : set α} (hx : x ∈ s) (h : s ⊆ t) : x ∈ t := h hx /-! ### Lemmas about `mem` and `set_of` -/ @[simp] theorem mem_set_of_eq {a : α} {p : α → Prop} : a ∈ {a | p a} = p a := rfl theorem nmem_set_of_eq {a : α} {P : α → Prop} : a ∉ {a : α | P a} = ¬ P a := rfl @[simp] theorem set_of_mem_eq {s : set α} : {x | x ∈ s} = s := rfl theorem set_of_set {s : set α} : set_of s = s := rfl lemma set_of_app_iff {p : α → Prop} {x : α} : { x | p x } x ↔ p x := iff.rfl theorem mem_def {a : α} {s : set α} : a ∈ s ↔ s a := iff.rfl instance decidable_mem (s : set α) [H : decidable_pred s] : ∀ a, decidable (a ∈ s) := H instance decidable_set_of (p : α → Prop) [H : decidable_pred p] : decidable_pred {a | p a} := H @[simp] theorem set_of_subset_set_of {p q : α → Prop} : {a | p a} ⊆ {a | q a} ↔ (∀a, p a → q a) := iff.rfl @[simp] lemma sep_set_of {p q : α → Prop} : {a ∈ {a | p a } | q a} = {a | p a ∧ q a} := rfl lemma set_of_and {p q : α → Prop} : {a | p a ∧ q a} = {a | p a} ∩ {a | q a} := rfl lemma set_of_or {p q : α → Prop} : {a | p a ∨ q a} = {a | p a} ∪ {a | q a} := rfl /-! ### Lemmas about subsets -/ -- TODO(Jeremy): write a tactic to unfold specific instances of generic notation? theorem subset_def {s t : set α} : (s ⊆ t) = ∀ x, x ∈ s → x ∈ t := rfl @[refl] theorem subset.refl (a : set α) : a ⊆ a := assume x, id theorem subset.rfl {s : set α} : s ⊆ s := subset.refl s @[trans] theorem subset.trans {a b c : set α} (ab : a ⊆ b) (bc : b ⊆ c) : a ⊆ c := assume x h, bc (ab h) @[trans] theorem mem_of_eq_of_mem {x y : α} {s : set α} (hx : x = y) (h : y ∈ s) : x ∈ s := hx.symm ▸ h theorem subset.antisymm {a b : set α} (h₁ : a ⊆ b) (h₂ : b ⊆ a) : a = b := ext (λ x, iff.intro (λ ina, h₁ ina) (λ inb, h₂ inb)) theorem subset.antisymm_iff {a b : set α} : a = b ↔ a ⊆ b ∧ b ⊆ a := ⟨λ e, e ▸ ⟨subset.refl _, subset.refl _⟩, λ ⟨h₁, h₂⟩, subset.antisymm h₁ h₂⟩ -- an alternative name theorem eq_of_subset_of_subset {a b : set α} (h₁ : a ⊆ b) (h₂ : b ⊆ a) : a = b := subset.antisymm h₁ h₂ theorem mem_of_subset_of_mem {s₁ s₂ : set α} {a : α} : s₁ ⊆ s₂ → a ∈ s₁ → a ∈ s₂ := assume h₁ h₂, h₁ h₂ theorem not_subset : (¬ s ⊆ t) ↔ ∃a ∈ s, a ∉ t := by simp [subset_def, not_forall] /-! ### Definition of strict subsets `s ⊂ t` and basic properties. -/ instance : has_ssubset (set α) := ⟨(<)⟩ @[simp] lemma lt_eq_ssubset (s t : set α) : s < t = (s ⊂ t) := rfl theorem ssubset_def : (s ⊂ t) = (s ⊆ t ∧ ¬ (t ⊆ s)) := rfl theorem eq_or_ssubset_of_subset (h : s ⊆ t) : s = t ∨ s ⊂ t := classical.by_cases (λ H : t ⊆ s, or.inl $ subset.antisymm h H) (λ H, or.inr ⟨h, H⟩) lemma exists_of_ssubset {s t : set α} (h : s ⊂ t) : (∃x∈t, x ∉ s) := not_subset.1 h.2 lemma ssubset_iff_subset_ne {s t : set α} : s ⊂ t ↔ s ⊆ t ∧ s ≠ t := by split; simp [set.ssubset_def, ne.def, set.subset.antisymm_iff] {contextual := tt} lemma ssubset_iff_of_subset {s t : set α} (h : s ⊆ t) : s ⊂ t ↔ ∃ x ∈ t, x ∉ s := ⟨exists_of_ssubset, λ ⟨x, hxt, hxs⟩, ⟨h, λ h, hxs $ h hxt⟩⟩ theorem not_mem_empty (x : α) : ¬ (x ∈ (∅ : set α)) := assume h : x ∈ ∅, h @[simp] theorem not_not_mem : ¬ (a ∉ s) ↔ a ∈ s := by { classical, exact not_not } /-! ### Non-empty sets -/ /-- The property `s.nonempty` expresses the fact that the set `s` is not empty. It should be used in theorem assumptions instead of `∃ x, x ∈ s` or `s ≠ ∅` as it gives access to a nice API thanks to the dot notation. -/ protected def nonempty (s : set α) : Prop := ∃ x, x ∈ s lemma nonempty_def : s.nonempty ↔ ∃ x, x ∈ s := iff.rfl lemma nonempty_of_mem {x} (h : x ∈ s) : s.nonempty := ⟨x, h⟩ theorem nonempty.not_subset_empty : s.nonempty → ¬(s ⊆ ∅) | ⟨x, hx⟩ hs := hs hx theorem nonempty.ne_empty : s.nonempty → s ≠ ∅ | ⟨x, hx⟩ hs := by { rw hs at hx, exact hx } /-- Extract a witness from `s.nonempty`. This function might be used instead of case analysis on the argument. Note that it makes a proof depend on the `classical.choice` axiom. -/ protected noncomputable def nonempty.some (h : s.nonempty) : α := classical.some h protected lemma nonempty.some_mem (h : s.nonempty) : h.some ∈ s := classical.some_spec h lemma nonempty.mono (ht : s ⊆ t) (hs : s.nonempty) : t.nonempty := hs.imp ht lemma nonempty_of_not_subset (h : ¬s ⊆ t) : (s \ t).nonempty := let ⟨x, xs, xt⟩ := not_subset.1 h in ⟨x, xs, xt⟩ lemma nonempty_of_ssubset (ht : s ⊂ t) : (t \ s).nonempty := nonempty_of_not_subset ht.2 lemma nonempty.of_diff (h : (s \ t).nonempty) : s.nonempty := h.imp $ λ _, and.left lemma nonempty_of_ssubset' (ht : s ⊂ t) : t.nonempty := (nonempty_of_ssubset ht).of_diff lemma nonempty.inl (hs : s.nonempty) : (s ∪ t).nonempty := hs.imp $ λ _, or.inl lemma nonempty.inr (ht : t.nonempty) : (s ∪ t).nonempty := ht.imp $ λ _, or.inr @[simp] lemma union_nonempty : (s ∪ t).nonempty ↔ s.nonempty ∨ t.nonempty := exists_or_distrib lemma nonempty.left (h : (s ∩ t).nonempty) : s.nonempty := h.imp $ λ _, and.left lemma nonempty.right (h : (s ∩ t).nonempty) : t.nonempty := h.imp $ λ _, and.right lemma nonempty_inter_iff_exists_right : (s ∩ t).nonempty ↔ ∃ x : t, ↑x ∈ s := ⟨λ ⟨x, xs, xt⟩, ⟨⟨x, xt⟩, xs⟩, λ ⟨⟨x, xt⟩, xs⟩, ⟨x, xs, xt⟩⟩ lemma nonempty_inter_iff_exists_left : (s ∩ t).nonempty ↔ ∃ x : s, ↑x ∈ t := ⟨λ ⟨x, xs, xt⟩, ⟨⟨x, xs⟩, xt⟩, λ ⟨⟨x, xt⟩, xs⟩, ⟨x, xt, xs⟩⟩ lemma nonempty_iff_univ_nonempty : nonempty α ↔ (univ : set α).nonempty := ⟨λ ⟨x⟩, ⟨x, trivial⟩, λ ⟨x, _⟩, ⟨x⟩⟩ @[simp] lemma univ_nonempty : ∀ [h : nonempty α], (univ : set α).nonempty | ⟨x⟩ := ⟨x, trivial⟩ lemma nonempty.to_subtype (h : s.nonempty) : nonempty s := nonempty_subtype.2 h /-! ### Lemmas about the empty set -/ theorem empty_def : (∅ : set α) = {x | false} := rfl @[simp] theorem mem_empty_eq (x : α) : x ∈ (∅ : set α) = false := rfl @[simp] theorem set_of_false : {a : α | false} = ∅ := rfl theorem eq_empty_iff_forall_not_mem {s : set α} : s = ∅ ↔ ∀ x, x ∉ s := by simp [ext_iff] @[simp] theorem empty_subset (s : set α) : ∅ ⊆ s := assume x, assume h, false.elim h theorem subset_empty_iff {s : set α} : s ⊆ ∅ ↔ s = ∅ := by simp [subset.antisymm_iff] theorem eq_empty_of_subset_empty {s : set α} : s ⊆ ∅ → s = ∅ := subset_empty_iff.1 theorem eq_empty_of_not_nonempty (h : ¬nonempty α) (s : set α) : s = ∅ := eq_empty_of_subset_empty $ λ x hx, h ⟨x⟩ lemma not_nonempty_iff_eq_empty {s : set α} : ¬s.nonempty ↔ s = ∅ := by simp only [set.nonempty, eq_empty_iff_forall_not_mem, not_exists] lemma empty_not_nonempty : ¬(∅ : set α).nonempty := not_nonempty_iff_eq_empty.2 rfl lemma eq_empty_or_nonempty (s : set α) : s = ∅ ∨ s.nonempty := classical.by_cases or.inr (λ h, or.inl $ not_nonempty_iff_eq_empty.1 h) theorem ne_empty_iff_nonempty : s ≠ ∅ ↔ s.nonempty := (not_congr not_nonempty_iff_eq_empty.symm).trans not_not theorem subset_eq_empty {s t : set α} (h : t ⊆ s) (e : s = ∅) : t = ∅ := subset_empty_iff.1 $ e ▸ h theorem ball_empty_iff {p : α → Prop} : (∀ x ∈ (∅ : set α), p x) ↔ true := by simp [iff_def] /-! ### Universal set. In Lean `@univ α` (or `univ : set α`) is the set that contains all elements of type `α`. Mathematically it is the same as `α` but it has a different type. -/ @[simp] theorem set_of_true : {x : α | true} = univ := rfl @[simp] theorem mem_univ (x : α) : x ∈ @univ α := trivial theorem empty_ne_univ [h : nonempty α] : (∅ : set α) ≠ univ := by simp [ext_iff] @[simp] theorem subset_univ (s : set α) : s ⊆ univ := λ x H, trivial theorem univ_subset_iff {s : set α} : univ ⊆ s ↔ s = univ := by simp [subset.antisymm_iff] theorem eq_univ_of_univ_subset {s : set α} : univ ⊆ s → s = univ := univ_subset_iff.1 theorem eq_univ_iff_forall {s : set α} : s = univ ↔ ∀ x, x ∈ s := by simp [ext_iff] theorem eq_univ_of_forall {s : set α} : (∀ x, x ∈ s) → s = univ := eq_univ_iff_forall.2 lemma eq_univ_of_subset {s t : set α} (h : s ⊆ t) (hs : s = univ) : t = univ := eq_univ_of_univ_subset $ hs ▸ h @[simp] lemma univ_eq_empty_iff : (univ : set α) = ∅ ↔ ¬ nonempty α := eq_empty_iff_forall_not_mem.trans ⟨λ H ⟨x⟩, H x trivial, λ H x _, H ⟨x⟩⟩ lemma exists_mem_of_nonempty (α) : ∀ [nonempty α], ∃x:α, x ∈ (univ : set α) | ⟨x⟩ := ⟨x, trivial⟩ instance univ_decidable : decidable_pred (@set.univ α) := λ x, is_true trivial /-- `diagonal α` is the subset of `α × α` consisting of all pairs of the form `(a, a)`. -/ def diagonal (α : Type*) : set (α × α) := {p | p.1 = p.2} @[simp] lemma mem_diagonal {α : Type*} (x : α) : (x, x) ∈ diagonal α := by simp [diagonal] /-! ### Lemmas about union -/ theorem union_def {s₁ s₂ : set α} : s₁ ∪ s₂ = {a | a ∈ s₁ ∨ a ∈ s₂} := rfl theorem mem_union_left {x : α} {a : set α} (b : set α) : x ∈ a → x ∈ a ∪ b := or.inl theorem mem_union_right {x : α} {b : set α} (a : set α) : x ∈ b → x ∈ a ∪ b := or.inr theorem mem_or_mem_of_mem_union {x : α} {a b : set α} (H : x ∈ a ∪ b) : x ∈ a ∨ x ∈ b := H theorem mem_union.elim {x : α} {a b : set α} {P : Prop} (H₁ : x ∈ a ∪ b) (H₂ : x ∈ a → P) (H₃ : x ∈ b → P) : P := or.elim H₁ H₂ H₃ theorem mem_union (x : α) (a b : set α) : x ∈ a ∪ b ↔ x ∈ a ∨ x ∈ b := iff.rfl @[simp] theorem mem_union_eq (x : α) (a b : set α) : x ∈ a ∪ b = (x ∈ a ∨ x ∈ b) := rfl @[simp] theorem union_self (a : set α) : a ∪ a = a := ext (assume x, or_self _) @[simp] theorem union_empty (a : set α) : a ∪ ∅ = a := ext (assume x, or_false _) @[simp] theorem empty_union (a : set α) : ∅ ∪ a = a := ext (assume x, false_or _) theorem union_comm (a b : set α) : a ∪ b = b ∪ a := ext (assume x, or.comm) theorem union_assoc (a b c : set α) : (a ∪ b) ∪ c = a ∪ (b ∪ c) := ext (assume x, or.assoc) instance union_is_assoc : is_associative (set α) (∪) := ⟨union_assoc⟩ instance union_is_comm : is_commutative (set α) (∪) := ⟨union_comm⟩ theorem union_left_comm (s₁ s₂ s₃ : set α) : s₁ ∪ (s₂ ∪ s₃) = s₂ ∪ (s₁ ∪ s₃) := by finish theorem union_right_comm (s₁ s₂ s₃ : set α) : (s₁ ∪ s₂) ∪ s₃ = (s₁ ∪ s₃) ∪ s₂ := by finish theorem union_eq_self_of_subset_left {s t : set α} (h : s ⊆ t) : s ∪ t = t := by finish [subset_def, ext_iff, iff_def] theorem union_eq_self_of_subset_right {s t : set α} (h : t ⊆ s) : s ∪ t = s := by finish [subset_def, ext_iff, iff_def] @[simp] theorem subset_union_left (s t : set α) : s ⊆ s ∪ t := λ x, or.inl @[simp] theorem subset_union_right (s t : set α) : t ⊆ s ∪ t := λ x, or.inr theorem union_subset {s t r : set α} (sr : s ⊆ r) (tr : t ⊆ r) : s ∪ t ⊆ r := by finish [subset_def, union_def] @[simp] theorem union_subset_iff {s t u : set α} : s ∪ t ⊆ u ↔ s ⊆ u ∧ t ⊆ u := by finish [iff_def, subset_def] theorem union_subset_union {s₁ s₂ t₁ t₂ : set α} (h₁ : s₁ ⊆ s₂) (h₂ : t₁ ⊆ t₂) : s₁ ∪ t₁ ⊆ s₂ ∪ t₂ := by finish [subset_def] theorem union_subset_union_left {s₁ s₂ : set α} (t) (h : s₁ ⊆ s₂) : s₁ ∪ t ⊆ s₂ ∪ t := union_subset_union h (by refl) theorem union_subset_union_right (s) {t₁ t₂ : set α} (h : t₁ ⊆ t₂) : s ∪ t₁ ⊆ s ∪ t₂ := union_subset_union (by refl) h lemma subset_union_of_subset_left {s t : set α} (h : s ⊆ t) (u : set α) : s ⊆ t ∪ u := subset.trans h (subset_union_left t u) lemma subset_union_of_subset_right {s u : set α} (h : s ⊆ u) (t : set α) : s ⊆ t ∪ u := subset.trans h (subset_union_right t u) @[simp] theorem union_empty_iff {s t : set α} : s ∪ t = ∅ ↔ s = ∅ ∧ t = ∅ := ⟨by finish [ext_iff], by finish [ext_iff]⟩ /-! ### Lemmas about intersection -/ theorem inter_def {s₁ s₂ : set α} : s₁ ∩ s₂ = {a | a ∈ s₁ ∧ a ∈ s₂} := rfl theorem mem_inter_iff (x : α) (a b : set α) : x ∈ a ∩ b ↔ x ∈ a ∧ x ∈ b := iff.rfl @[simp] theorem mem_inter_eq (x : α) (a b : set α) : x ∈ a ∩ b = (x ∈ a ∧ x ∈ b) := rfl theorem mem_inter {x : α} {a b : set α} (ha : x ∈ a) (hb : x ∈ b) : x ∈ a ∩ b := ⟨ha, hb⟩ theorem mem_of_mem_inter_left {x : α} {a b : set α} (h : x ∈ a ∩ b) : x ∈ a := h.left theorem mem_of_mem_inter_right {x : α} {a b : set α} (h : x ∈ a ∩ b) : x ∈ b := h.right @[simp] theorem inter_self (a : set α) : a ∩ a = a := ext (assume x, and_self _) @[simp] theorem inter_empty (a : set α) : a ∩ ∅ = ∅ := ext (assume x, and_false _) @[simp] theorem empty_inter (a : set α) : ∅ ∩ a = ∅ := ext (assume x, false_and _) theorem inter_comm (a b : set α) : a ∩ b = b ∩ a := ext (assume x, and.comm) theorem inter_assoc (a b c : set α) : (a ∩ b) ∩ c = a ∩ (b ∩ c) := ext (assume x, and.assoc) instance inter_is_assoc : is_associative (set α) (∩) := ⟨inter_assoc⟩ instance inter_is_comm : is_commutative (set α) (∩) := ⟨inter_comm⟩ theorem inter_left_comm (s₁ s₂ s₃ : set α) : s₁ ∩ (s₂ ∩ s₃) = s₂ ∩ (s₁ ∩ s₃) := by finish theorem inter_right_comm (s₁ s₂ s₃ : set α) : (s₁ ∩ s₂) ∩ s₃ = (s₁ ∩ s₃) ∩ s₂ := by finish @[simp] theorem inter_subset_left (s t : set α) : s ∩ t ⊆ s := λ x H, and.left H @[simp] theorem inter_subset_right (s t : set α) : s ∩ t ⊆ t := λ x H, and.right H theorem subset_inter {s t r : set α} (rs : r ⊆ s) (rt : r ⊆ t) : r ⊆ s ∩ t := by finish [subset_def, inter_def] @[simp] theorem subset_inter_iff {s t r : set α} : r ⊆ s ∩ t ↔ r ⊆ s ∧ r ⊆ t := ⟨λ h, ⟨subset.trans h (inter_subset_left _ _), subset.trans h (inter_subset_right _ _)⟩, λ ⟨h₁, h₂⟩, subset_inter h₁ h₂⟩ @[simp] theorem inter_univ (a : set α) : a ∩ univ = a := ext (assume x, and_true _) @[simp] theorem univ_inter (a : set α) : univ ∩ a = a := ext (assume x, true_and _) theorem inter_subset_inter_left {s t : set α} (u : set α) (H : s ⊆ t) : s ∩ u ⊆ t ∩ u := by finish [subset_def] theorem inter_subset_inter_right {s t : set α} (u : set α) (H : s ⊆ t) : u ∩ s ⊆ u ∩ t := by finish [subset_def] theorem inter_subset_inter {s₁ s₂ t₁ t₂ : set α} (h₁ : s₁ ⊆ t₁) (h₂ : s₂ ⊆ t₂) : s₁ ∩ s₂ ⊆ t₁ ∩ t₂ := by finish [subset_def] theorem inter_eq_self_of_subset_left {s t : set α} (h : s ⊆ t) : s ∩ t = s := by finish [subset_def, ext_iff, iff_def] theorem inter_eq_self_of_subset_right {s t : set α} (h : t ⊆ s) : s ∩ t = t := by finish [subset_def, ext_iff, iff_def] lemma inter_compl_nonempty_iff {s t : set α} : (s ∩ tᶜ).nonempty ↔ ¬ s ⊆ t := begin split, { rintros ⟨x ,xs, xt⟩ sub, exact xt (sub xs) }, { intros h, rcases not_subset.mp h with ⟨x, xs, xt⟩, exact ⟨x, xs, xt⟩ } end theorem union_inter_cancel_left {s t : set α} : (s ∪ t) ∩ s = s := by finish [ext_iff, iff_def] theorem union_inter_cancel_right {s t : set α} : (s ∪ t) ∩ t = t := by finish [ext_iff, iff_def] /-! ### Distributivity laws -/ theorem inter_distrib_left (s t u : set α) : s ∩ (t ∪ u) = (s ∩ t) ∪ (s ∩ u) := ext (assume x, and_or_distrib_left) theorem inter_distrib_right (s t u : set α) : (s ∪ t) ∩ u = (s ∩ u) ∪ (t ∩ u) := ext (assume x, or_and_distrib_right) theorem union_distrib_left (s t u : set α) : s ∪ (t ∩ u) = (s ∪ t) ∩ (s ∪ u) := ext (assume x, or_and_distrib_left) theorem union_distrib_right (s t u : set α) : (s ∩ t) ∪ u = (s ∪ u) ∩ (t ∪ u) := ext (assume x, and_or_distrib_right) /-! ### Lemmas about `insert` `insert α s` is the set `{α} ∪ s`. -/ theorem insert_def (x : α) (s : set α) : insert x s = { y | y = x ∨ y ∈ s } := rfl @[simp] theorem subset_insert (x : α) (s : set α) : s ⊆ insert x s := assume y ys, or.inr ys theorem mem_insert (x : α) (s : set α) : x ∈ insert x s := or.inl rfl theorem mem_insert_of_mem {x : α} {s : set α} (y : α) : x ∈ s → x ∈ insert y s := or.inr theorem eq_or_mem_of_mem_insert {x a : α} {s : set α} : x ∈ insert a s → x = a ∨ x ∈ s := id theorem mem_of_mem_insert_of_ne {x a : α} {s : set α} (xin : x ∈ insert a s) : x ≠ a → x ∈ s := by finish [insert_def] @[simp] theorem mem_insert_iff {x a : α} {s : set α} : x ∈ insert a s ↔ (x = a ∨ x ∈ s) := iff.rfl @[simp] theorem insert_eq_of_mem {a : α} {s : set α} (h : a ∈ s) : insert a s = s := by finish [ext_iff, iff_def] lemma ne_insert_of_not_mem {s : set α} (t : set α) {a : α} (h : a ∉ s) : s ≠ insert a t := by { contrapose! h, simp [h] } theorem insert_subset : insert a s ⊆ t ↔ (a ∈ t ∧ s ⊆ t) := by simp [subset_def, or_imp_distrib, forall_and_distrib] theorem insert_subset_insert (h : s ⊆ t) : insert a s ⊆ insert a t := assume a', or.imp_right (@h a') theorem ssubset_iff_insert {s t : set α} : s ⊂ t ↔ ∃ a ∉ s, insert a s ⊆ t := begin simp only [insert_subset, exists_and_distrib_right, ssubset_def, not_subset], simp only [exists_prop, and_comm] end theorem ssubset_insert {s : set α} {a : α} (h : a ∉ s) : s ⊂ insert a s := ssubset_iff_insert.2 ⟨a, h, subset.refl _⟩ theorem insert_comm (a b : α) (s : set α) : insert a (insert b s) = insert b (insert a s) := by { ext, simp [or.left_comm] } theorem insert_union : insert a s ∪ t = insert a (s ∪ t) := by { ext, simp [or.comm, or.left_comm] } @[simp] theorem union_insert : s ∪ insert a t = insert a (s ∪ t) := by { ext, simp [or.comm, or.left_comm] } theorem insert_nonempty (a : α) (s : set α) : (insert a s).nonempty := ⟨a, mem_insert a s⟩ lemma insert_inter (x : α) (s t : set α) : insert x (s ∩ t) = insert x s ∩ insert x t := by { ext y, simp [←or_and_distrib_left] } -- useful in proofs by induction theorem forall_of_forall_insert {P : α → Prop} {a : α} {s : set α} (h : ∀ x, x ∈ insert a s → P x) : ∀ x, x ∈ s → P x := by finish theorem forall_insert_of_forall {P : α → Prop} {a : α} {s : set α} (h : ∀ x, x ∈ s → P x) (ha : P a) : ∀ x, x ∈ insert a s → P x := by finish theorem bex_insert_iff {P : α → Prop} {a : α} {s : set α} : (∃ x ∈ insert a s, P x) ↔ (∃ x ∈ s, P x) ∨ P a := by finish [iff_def] theorem ball_insert_iff {P : α → Prop} {a : α} {s : set α} : (∀ x ∈ insert a s, P x) ↔ P a ∧ (∀x ∈ s, P x) := by finish [iff_def] /-! ### Lemmas about singletons -/ theorem singleton_def (a : α) : ({a} : set α) = insert a ∅ := (insert_emptyc_eq _).symm @[simp] theorem mem_singleton_iff {a b : α} : a ∈ ({b} : set α) ↔ a = b := iff.rfl @[simp] lemma set_of_eq_eq_singleton {a : α} : {n | n = a} = {a} := set.ext $ λ n, (set.mem_singleton_iff).symm -- TODO: again, annotation needed @[simp] theorem mem_singleton (a : α) : a ∈ ({a} : set α) := by finish theorem eq_of_mem_singleton {x y : α} (h : x ∈ ({y} : set α)) : x = y := by finish @[simp] theorem singleton_eq_singleton_iff {x y : α} : {x} = ({y} : set α) ↔ x = y := by finish [ext_iff, iff_def] theorem mem_singleton_of_eq {x y : α} (H : x = y) : x ∈ ({y} : set α) := by finish theorem insert_eq (x : α) (s : set α) : insert x s = ({x} : set α) ∪ s := by finish [ext_iff, or_comm] @[simp] theorem pair_eq_singleton (a : α) : ({a, a} : set α) = {a} := by finish @[simp] theorem singleton_nonempty (a : α) : ({a} : set α).nonempty := ⟨a, rfl⟩ @[simp] theorem singleton_subset_iff {a : α} {s : set α} : {a} ⊆ s ↔ a ∈ s := ⟨λh, h (by simp), λh b e, by { rw [mem_singleton_iff] at e, simp [*] }⟩ theorem set_compr_eq_eq_singleton {a : α} : {b | b = a} = {a} := by { ext, simp } @[simp] theorem singleton_union : {a} ∪ s = insert a s := rfl @[simp] theorem union_singleton : s ∪ {a} = insert a s := by rw [union_comm, singleton_union] theorem singleton_inter_eq_empty : {a} ∩ s = ∅ ↔ a ∉ s := by simp [eq_empty_iff_forall_not_mem] theorem inter_singleton_eq_empty : s ∩ {a} = ∅ ↔ a ∉ s := by rw [inter_comm, singleton_inter_eq_empty] lemma nmem_singleton_empty {s : set α} : s ∉ ({∅} : set (set α)) ↔ s.nonempty := by rw [mem_singleton_iff, ← ne.def, ne_empty_iff_nonempty] instance unique_singleton (a : α) : unique ↥({a} : set α) := { default := ⟨a, mem_singleton a⟩, uniq := begin intros x, apply subtype.ext, apply eq_of_mem_singleton (subtype.mem x), end} lemma eq_singleton_iff_unique_mem {s : set α} {a : α} : s = {a} ↔ a ∈ s ∧ ∀ x ∈ s, x = a := by simp [ext_iff, @iff_def (_ ∈ s), forall_and_distrib, and_comm] /-! ### Lemmas about sets defined as `{x ∈ s | p x}`. -/ theorem mem_sep {s : set α} {p : α → Prop} {x : α} (xs : x ∈ s) (px : p x) : x ∈ {x ∈ s | p x} := ⟨xs, px⟩ @[simp] theorem sep_mem_eq {s t : set α} : {x ∈ s | x ∈ t} = s ∩ t := rfl @[simp] theorem mem_sep_eq {s : set α} {p : α → Prop} {x : α} : x ∈ {x ∈ s | p x} = (x ∈ s ∧ p x) := rfl theorem mem_sep_iff {s : set α} {p : α → Prop} {x : α} : x ∈ {x ∈ s | p x} ↔ x ∈ s ∧ p x := iff.rfl theorem eq_sep_of_subset {s t : set α} (ssubt : s ⊆ t) : s = {x ∈ t | x ∈ s} := by finish [ext_iff, iff_def, subset_def] theorem sep_subset (s : set α) (p : α → Prop) : {x ∈ s | p x} ⊆ s := assume x, and.left theorem forall_not_of_sep_empty {s : set α} {p : α → Prop} (h : {x ∈ s | p x} = ∅) : ∀ x ∈ s, ¬ p x := by finish [ext_iff] @[simp] lemma sep_univ {α} {p : α → Prop} : {a ∈ (univ : set α) | p a} = {a | p a} := by { ext, simp } /-! ### Lemmas about complement -/ theorem mem_compl {s : set α} {x : α} (h : x ∉ s) : x ∈ sᶜ := h lemma compl_set_of {α} (p : α → Prop) : {a | p a}ᶜ = { a | ¬ p a } := rfl theorem not_mem_of_mem_compl {s : set α} {x : α} (h : x ∈ sᶜ) : x ∉ s := h @[simp] theorem mem_compl_eq (s : set α) (x : α) : x ∈ sᶜ = (x ∉ s) := rfl theorem mem_compl_iff (s : set α) (x : α) : x ∈ sᶜ ↔ x ∉ s := iff.rfl @[simp] theorem inter_compl_self (s : set α) : s ∩ sᶜ = ∅ := by finish [ext_iff] @[simp] theorem compl_inter_self (s : set α) : sᶜ ∩ s = ∅ := by finish [ext_iff] @[simp] theorem compl_empty : (∅ : set α)ᶜ = univ := by finish [ext_iff] @[simp] theorem compl_union (s t : set α) : (s ∪ t)ᶜ = sᶜ ∩ tᶜ := by finish [ext_iff] local attribute [simp] -- Will be generalized to lattices in `compl_compl'` theorem compl_compl (s : set α) : sᶜᶜ = s := by finish [ext_iff] -- ditto theorem compl_inter (s t : set α) : (s ∩ t)ᶜ = sᶜ ∪ tᶜ := by finish [ext_iff] @[simp] theorem compl_univ : (univ : set α)ᶜ = ∅ := by finish [ext_iff] lemma compl_empty_iff {s : set α} : sᶜ = ∅ ↔ s = univ := by { split, intro h, rw [←compl_compl s, h, compl_empty], intro h, rw [h, compl_univ] } lemma compl_univ_iff {s : set α} : sᶜ = univ ↔ s = ∅ := by rw [←compl_empty_iff, compl_compl] lemma nonempty_compl {s : set α} : sᶜ.nonempty ↔ s ≠ univ := ne_empty_iff_nonempty.symm.trans $ not_congr $ compl_empty_iff lemma mem_compl_singleton_iff {a x : α} : x ∈ ({a} : set α)ᶜ ↔ x ≠ a := not_iff_not_of_iff mem_singleton_iff lemma compl_singleton_eq (a : α) : ({a} : set α)ᶜ = {x | x ≠ a} := ext $ λ x, mem_compl_singleton_iff theorem union_eq_compl_compl_inter_compl (s t : set α) : s ∪ t = (sᶜ ∩ tᶜ)ᶜ := by simp [compl_inter, compl_compl] theorem inter_eq_compl_compl_union_compl (s t : set α) : s ∩ t = (sᶜ ∪ tᶜ)ᶜ := by simp [compl_compl] @[simp] theorem union_compl_self (s : set α) : s ∪ sᶜ = univ := by finish [ext_iff] @[simp] theorem compl_union_self (s : set α) : sᶜ ∪ s = univ := by finish [ext_iff] theorem compl_comp_compl : compl ∘ compl = @id (set α) := funext compl_compl theorem compl_subset_comm {s t : set α} : sᶜ ⊆ t ↔ tᶜ ⊆ s := by haveI := classical.prop_decidable; exact forall_congr (λ a, not_imp_comm) lemma compl_subset_compl {s t : set α} : sᶜ ⊆ tᶜ ↔ t ⊆ s := by rw [compl_subset_comm, compl_compl] theorem compl_subset_iff_union {s t : set α} : sᶜ ⊆ t ↔ s ∪ t = univ := iff.symm $ eq_univ_iff_forall.trans $ forall_congr $ λ a, by haveI := classical.prop_decidable; exact or_iff_not_imp_left theorem subset_compl_comm {s t : set α} : s ⊆ tᶜ ↔ t ⊆ sᶜ := forall_congr $ λ a, imp_not_comm theorem subset_compl_iff_disjoint {s t : set α} : s ⊆ tᶜ ↔ s ∩ t = ∅ := iff.trans (forall_congr $ λ a, and_imp.symm) subset_empty_iff lemma subset_compl_singleton_iff {a : α} {s : set α} : s ⊆ {a}ᶜ ↔ a ∉ s := by { rw subset_compl_comm, simp } theorem inter_subset (a b c : set α) : a ∩ b ⊆ c ↔ a ⊆ bᶜ ∪ c := begin classical, split, { intros h x xa, by_cases h' : x ∈ b, simp [h ⟨xa, h'⟩], simp [h'] }, intros h x, rintro ⟨xa, xb⟩, cases h xa, contradiction, assumption end /-! ### Lemmas about set difference -/ theorem diff_eq (s t : set α) : s \ t = s ∩ tᶜ := rfl @[simp] theorem mem_diff {s t : set α} (x : α) : x ∈ s \ t ↔ x ∈ s ∧ x ∉ t := iff.rfl theorem mem_diff_of_mem {s t : set α} {x : α} (h1 : x ∈ s) (h2 : x ∉ t) : x ∈ s \ t := ⟨h1, h2⟩ theorem mem_of_mem_diff {s t : set α} {x : α} (h : x ∈ s \ t) : x ∈ s := h.left theorem not_mem_of_mem_diff {s t : set α} {x : α} (h : x ∈ s \ t) : x ∉ t := h.right theorem diff_eq_compl_inter {s t : set α} : s \ t = tᶜ ∩ s := by rw [diff_eq, inter_comm] theorem nonempty_diff {s t : set α} : (s \ t).nonempty ↔ ¬ (s ⊆ t) := ⟨λ ⟨x, xs, xt⟩, not_subset.2 ⟨x, xs, xt⟩, λ h, let ⟨x, xs, xt⟩ := not_subset.1 h in ⟨x, xs, xt⟩⟩ theorem union_diff_cancel' {s t u : set α} (h₁ : s ⊆ t) (h₂ : t ⊆ u) : t ∪ (u \ s) = u := by finish [ext_iff, iff_def, subset_def] theorem union_diff_cancel {s t : set α} (h : s ⊆ t) : s ∪ (t \ s) = t := union_diff_cancel' (subset.refl s) h theorem union_diff_cancel_left {s t : set α} (h : s ∩ t ⊆ ∅) : (s ∪ t) \ s = t := by finish [ext_iff, iff_def, subset_def] theorem union_diff_cancel_right {s t : set α} (h : s ∩ t ⊆ ∅) : (s ∪ t) \ t = s := by finish [ext_iff, iff_def, subset_def] theorem union_diff_left {s t : set α} : (s ∪ t) \ s = t \ s := by finish [ext_iff, iff_def] theorem union_diff_right {s t : set α} : (s ∪ t) \ t = s \ t := by finish [ext_iff, iff_def] theorem union_diff_distrib {s t u : set α} : (s ∪ t) \ u = s \ u ∪ t \ u := inter_distrib_right _ _ _ theorem inter_union_distrib_left {s t u : set α} : s ∩ (t ∪ u) = (s ∩ t) ∪ (s ∩ u) := set.ext $ λ _, and_or_distrib_left theorem inter_union_distrib_right {s t u : set α} : (s ∩ t) ∪ u = (s ∪ u) ∩ (t ∪ u) := set.ext $ λ _, and_or_distrib_right theorem union_inter_distrib_left {s t u : set α} : s ∪ (t ∩ u) = (s ∪ t) ∩ (s ∪ u) := set.ext $ λ _, or_and_distrib_left theorem union_inter_distrib_right {s t u : set α} : (s ∪ t) ∩ u = (s ∩ u) ∪ (t ∩ u) := set.ext $ λ _, or_and_distrib_right theorem inter_diff_assoc (a b c : set α) : (a ∩ b) \ c = a ∩ (b \ c) := inter_assoc _ _ _ theorem inter_diff_self (a b : set α) : a ∩ (b \ a) = ∅ := by finish [ext_iff] theorem inter_union_diff (s t : set α) : (s ∩ t) ∪ (s \ t) = s := by finish [ext_iff, iff_def] theorem inter_union_compl (s t : set α) : (s ∩ t) ∪ (s ∩ tᶜ) = s := inter_union_diff _ _ theorem diff_subset (s t : set α) : s \ t ⊆ s := by finish [subset_def] theorem diff_subset_diff {s₁ s₂ t₁ t₂ : set α} : s₁ ⊆ s₂ → t₂ ⊆ t₁ → s₁ \ t₁ ⊆ s₂ \ t₂ := by finish [subset_def] theorem diff_subset_diff_left {s₁ s₂ t : set α} (h : s₁ ⊆ s₂) : s₁ \ t ⊆ s₂ \ t := diff_subset_diff h (by refl) theorem diff_subset_diff_right {s t u : set α} (h : t ⊆ u) : s \ u ⊆ s \ t := diff_subset_diff (subset.refl s) h theorem compl_eq_univ_diff (s : set α) : sᶜ = univ \ s := by finish [ext_iff] @[simp] lemma empty_diff (s : set α) : (∅ \ s : set α) = ∅ := eq_empty_of_subset_empty $ assume x ⟨hx, _⟩, hx theorem diff_eq_empty {s t : set α} : s \ t = ∅ ↔ s ⊆ t := ⟨assume h x hx, classical.by_contradiction $ assume : x ∉ t, show x ∈ (∅ : set α), from h ▸ ⟨hx, this⟩, assume h, eq_empty_of_subset_empty $ assume x ⟨hx, hnx⟩, hnx $ h hx⟩ @[simp] theorem diff_empty {s : set α} : s \ ∅ = s := ext $ assume x, ⟨assume ⟨hx, _⟩, hx, assume h, ⟨h, not_false⟩⟩ theorem diff_diff {u : set α} : s \ t \ u = s \ (t ∪ u) := ext $ by simp [not_or_distrib, and.comm, and.left_comm] -- the following statement contains parentheses to help the reader lemma diff_diff_comm {s t u : set α} : (s \ t) \ u = (s \ u) \ t := by simp_rw [diff_diff, union_comm] lemma diff_subset_iff {s t u : set α} : s \ t ⊆ u ↔ s ⊆ t ∪ u := ⟨assume h x xs, classical.by_cases or.inl (assume nxt, or.inr (h ⟨xs, nxt⟩)), assume h x ⟨xs, nxt⟩, or.resolve_left (h xs) nxt⟩ lemma subset_diff_union (s t : set α) : s ⊆ (s \ t) ∪ t := by rw [union_comm, ←diff_subset_iff] @[simp] lemma diff_singleton_subset_iff {x : α} {s t : set α} : s \ {x} ⊆ t ↔ s ⊆ insert x t := by { rw [←union_singleton, union_comm], apply diff_subset_iff } lemma subset_diff_singleton {x : α} {s t : set α} (h : s ⊆ t) (hx : x ∉ s) : s ⊆ t \ {x} := subset_inter h $ subset_compl_comm.1 $ singleton_subset_iff.2 hx lemma subset_insert_diff_singleton (x : α) (s : set α) : s ⊆ insert x (s \ {x}) := by rw [←diff_singleton_subset_iff] lemma diff_subset_comm {s t u : set α} : s \ t ⊆ u ↔ s \ u ⊆ t := by rw [diff_subset_iff, diff_subset_iff, union_comm] lemma diff_inter {s t u : set α} : s \ (t ∩ u) = (s \ t) ∪ (s \ u) := ext $ λ x, by simp [not_and_distrib, and_or_distrib_left] lemma diff_inter_diff {s t u : set α} : s \ t ∩ (s \ u) = s \ (t ∪ u) := by { ext x, simp only [mem_inter_eq, mem_union_eq, mem_diff, not_or_distrib, and.left_comm, and.assoc, and_self_left] } lemma diff_compl : s \ tᶜ = s ∩ t := by rw [diff_eq, compl_compl] lemma diff_diff_right {s t u : set α} : s \ (t \ u) = (s \ t) ∪ (s ∩ u) := by rw [diff_eq t u, diff_inter, diff_compl] @[simp] theorem insert_diff_of_mem (s) (h : a ∈ t) : insert a s \ t = s \ t := by { ext, split; simp [or_imp_distrib, h] {contextual := tt} } theorem insert_diff_of_not_mem (s) (h : a ∉ t) : insert a s \ t = insert a (s \ t) := begin classical, ext x, by_cases h' : x ∈ t, { have : x ≠ a, { assume H, rw H at h', exact h h' }, simp [h, h', this] }, { simp [h, h'] } end lemma insert_diff_self_of_not_mem {a : α} {s : set α} (h : a ∉ s) : insert a s \ {a} = s := by { ext, simp [and_iff_left_of_imp (λ hx : x ∈ s, show x ≠ a, from λ hxa, h $ hxa ▸ hx)] } theorem union_diff_self {s t : set α} : s ∪ (t \ s) = s ∪ t := by finish [ext_iff, iff_def] theorem diff_union_self {s t : set α} : (s \ t) ∪ t = s ∪ t := by rw [union_comm, union_diff_self, union_comm] theorem diff_inter_self {a b : set α} : (b \ a) ∩ a = ∅ := by { ext, by simp [iff_def] {contextual:=tt} } theorem diff_inter_self_eq_diff {s t : set α} : s \ (t ∩ s) = s \ t := by { ext, simp [iff_def] {contextual := tt} } theorem diff_self_inter {s t : set α} : s \ (s ∩ t) = s \ t := by rw [inter_comm, diff_inter_self_eq_diff] theorem diff_eq_self {s t : set α} : s \ t = s ↔ t ∩ s ⊆ ∅ := by finish [ext_iff, iff_def, subset_def] @[simp] theorem diff_singleton_eq_self {a : α} {s : set α} (h : a ∉ s) : s \ {a} = s := diff_eq_self.2 $ by simp [singleton_inter_eq_empty.2 h] @[simp] theorem insert_diff_singleton {a : α} {s : set α} : insert a (s \ {a}) = insert a s := by simp [insert_eq, union_diff_self, -union_singleton, -singleton_union] @[simp] lemma diff_self {s : set α} : s \ s = ∅ := by { ext, simp } lemma diff_diff_cancel_left {s t : set α} (h : s ⊆ t) : t \ (t \ s) = s := by simp only [diff_diff_right, diff_self, inter_eq_self_of_subset_right h, empty_union] lemma mem_diff_singleton {x y : α} {s : set α} : x ∈ s \ {y} ↔ (x ∈ s ∧ x ≠ y) := iff.rfl lemma mem_diff_singleton_empty {s : set α} {t : set (set α)} : s ∈ t \ {∅} ↔ (s ∈ t ∧ s.nonempty) := mem_diff_singleton.trans $ and_congr iff.rfl ne_empty_iff_nonempty /-! ### Powerset -/ theorem mem_powerset {x s : set α} (h : x ⊆ s) : x ∈ powerset s := h theorem subset_of_mem_powerset {x s : set α} (h : x ∈ powerset s) : x ⊆ s := h @[simp] theorem mem_powerset_iff (x s : set α) : x ∈ powerset s ↔ x ⊆ s := iff.rfl theorem powerset_inter (s t : set α) : 𝒫 (s ∩ t) = 𝒫 s ∩ 𝒫 t := ext $ λ u, subset_inter_iff @[simp] theorem powerset_mono : 𝒫 s ⊆ 𝒫 t ↔ s ⊆ t := ⟨λ h, h (subset.refl s), λ h u hu, subset.trans hu h⟩ theorem monotone_powerset : monotone (powerset : set α → set (set α)) := λ s t, powerset_mono.2 @[simp] theorem powerset_nonempty : (𝒫 s).nonempty := ⟨∅, empty_subset s⟩ @[simp] theorem powerset_empty : 𝒫 (∅ : set α) = {∅} := ext $ λ s, subset_empty_iff /-! ### Inverse image -/ /-- The preimage of `s : set β` by `f : α → β`, written `f ⁻¹' s`, is the set of `x : α` such that `f x ∈ s`. -/ def preimage {α : Type u} {β : Type v} (f : α → β) (s : set β) : set α := {x | f x ∈ s} infix ` ⁻¹' `:80 := preimage section preimage variables {f : α → β} {g : β → γ} @[simp] theorem preimage_empty : f ⁻¹' ∅ = ∅ := rfl @[simp] theorem mem_preimage {s : set β} {a : α} : (a ∈ f ⁻¹' s) ↔ (f a ∈ s) := iff.rfl lemma preimage_congr {f g : α → β} {s : set β} (h : ∀ (x : α), f x = g x) : f ⁻¹' s = g ⁻¹' s := by { congr' with x, apply_assumption } theorem preimage_mono {s t : set β} (h : s ⊆ t) : f ⁻¹' s ⊆ f ⁻¹' t := assume x hx, h hx @[simp] theorem preimage_univ : f ⁻¹' univ = univ := rfl theorem subset_preimage_univ {s : set α} : s ⊆ f ⁻¹' univ := subset_univ _ @[simp] theorem preimage_inter {s t : set β} : f ⁻¹' (s ∩ t) = f ⁻¹' s ∩ f ⁻¹' t := rfl @[simp] theorem preimage_union {s t : set β} : f ⁻¹' (s ∪ t) = f ⁻¹' s ∪ f ⁻¹' t := rfl @[simp] theorem preimage_compl {s : set β} : f ⁻¹' sᶜ = (f ⁻¹' s)ᶜ := rfl @[simp] theorem preimage_diff (f : α → β) (s t : set β) : f ⁻¹' (s \ t) = f ⁻¹' s \ f ⁻¹' t := rfl @[simp] theorem preimage_set_of_eq {p : α → Prop} {f : β → α} : f ⁻¹' {a | p a} = {a | p (f a)} := rfl @[simp] theorem preimage_id {s : set α} : id ⁻¹' s = s := rfl @[simp] theorem preimage_id' {s : set α} : (λ x, x) ⁻¹' s = s := rfl theorem preimage_const_of_mem {b : β} {s : set β} (h : b ∈ s) : (λ (x : α), b) ⁻¹' s = univ := eq_univ_of_forall $ λ x, h theorem preimage_const_of_not_mem {b : β} {s : set β} (h : b ∉ s) : (λ (x : α), b) ⁻¹' s = ∅ := eq_empty_of_subset_empty $ λ x hx, h hx theorem preimage_const (b : β) (s : set β) [decidable (b ∈ s)] : (λ (x : α), b) ⁻¹' s = if b ∈ s then univ else ∅ := by { split_ifs with hb hb, exacts [preimage_const_of_mem hb, preimage_const_of_not_mem hb] } theorem preimage_comp {s : set γ} : (g ∘ f) ⁻¹' s = f ⁻¹' (g ⁻¹' s) := rfl lemma preimage_preimage {g : β → γ} {f : α → β} {s : set γ} : f ⁻¹' (g ⁻¹' s) = (λ x, g (f x)) ⁻¹' s := preimage_comp.symm theorem eq_preimage_subtype_val_iff {p : α → Prop} {s : set (subtype p)} {t : set α} : s = subtype.val ⁻¹' t ↔ (∀x (h : p x), (⟨x, h⟩ : subtype p) ∈ s ↔ x ∈ t) := ⟨assume s_eq x h, by { rw [s_eq], simp }, assume h, ext $ λ ⟨x, hx⟩, by simp [h]⟩ lemma preimage_coe_coe_diagonal {α : Type*} (s : set α) : (prod.map coe coe) ⁻¹' (diagonal α) = diagonal s := begin ext ⟨⟨x, x_in⟩, ⟨y, y_in⟩⟩, simp [set.diagonal], end end preimage /-! ### Image of a set under a function -/ section image infix ` '' `:80 := image theorem mem_image_iff_bex {f : α → β} {s : set α} {y : β} : y ∈ f '' s ↔ ∃ x (_ : x ∈ s), f x = y := bex_def.symm theorem mem_image_eq (f : α → β) (s : set α) (y: β) : y ∈ f '' s = ∃ x, x ∈ s ∧ f x = y := rfl @[simp] theorem mem_image (f : α → β) (s : set α) (y : β) : y ∈ f '' s ↔ ∃ x, x ∈ s ∧ f x = y := iff.rfl lemma image_eta (f : α → β) : f '' s = (λ x, f x) '' s := rfl theorem mem_image_of_mem (f : α → β) {x : α} {a : set α} (h : x ∈ a) : f x ∈ f '' a := ⟨_, h, rfl⟩ theorem mem_image_of_injective {f : α → β} {a : α} {s : set α} (hf : injective f) : f a ∈ f '' s ↔ a ∈ s := iff.intro (assume ⟨b, hb, eq⟩, (hf eq) ▸ hb) (assume h, mem_image_of_mem _ h) theorem ball_image_of_ball {f : α → β} {s : set α} {p : β → Prop} (h : ∀ x ∈ s, p (f x)) : ∀ y ∈ f '' s, p y := by finish [mem_image_eq] theorem ball_image_iff {f : α → β} {s : set α} {p : β → Prop} : (∀ y ∈ f '' s, p y) ↔ (∀ x ∈ s, p (f x)) := iff.intro (assume h a ha, h _ $ mem_image_of_mem _ ha) (assume h b ⟨a, ha, eq⟩, eq ▸ h a ha) theorem bex_image_iff {f : α → β} {s : set α} {p : β → Prop} : (∃ y ∈ f '' s, p y) ↔ (∃ x ∈ s, p (f x)) := by simp theorem mem_image_elim {f : α → β} {s : set α} {C : β → Prop} (h : ∀ (x : α), x ∈ s → C (f x)) : ∀{y : β}, y ∈ f '' s → C y | ._ ⟨a, a_in, rfl⟩ := h a a_in theorem mem_image_elim_on {f : α → β} {s : set α} {C : β → Prop} {y : β} (h_y : y ∈ f '' s) (h : ∀ (x : α), x ∈ s → C (f x)) : C y := mem_image_elim h h_y @[congr] lemma image_congr {f g : α → β} {s : set α} (h : ∀a∈s, f a = g a) : f '' s = g '' s := by safe [ext_iff, iff_def] /-- A common special case of `image_congr` -/ lemma image_congr' {f g : α → β} {s : set α} (h : ∀ (x : α), f x = g x) : f '' s = g '' s := image_congr (λx _, h x) theorem image_comp (f : β → γ) (g : α → β) (a : set α) : (f ∘ g) '' a = f '' (g '' a) := subset.antisymm (ball_image_of_ball $ assume a ha, mem_image_of_mem _ $ mem_image_of_mem _ ha) (ball_image_of_ball $ ball_image_of_ball $ assume a ha, mem_image_of_mem _ ha) /-- A variant of `image_comp`, useful for rewriting -/ lemma image_image (g : β → γ) (f : α → β) (s : set α) : g '' (f '' s) = (λ x, g (f x)) '' s := (image_comp g f s).symm /-- Image is monotone with respect to `⊆`. See `set.monotone_image` for the statement in terms of `≤`. -/ theorem image_subset {a b : set α} (f : α → β) (h : a ⊆ b) : f '' a ⊆ f '' b := by finish [subset_def, mem_image_eq] theorem image_union (f : α → β) (s t : set α) : f '' (s ∪ t) = f '' s ∪ f '' t := by finish [ext_iff, iff_def, mem_image_eq] @[simp] theorem image_empty (f : α → β) : f '' ∅ = ∅ := by { ext, simp } lemma image_inter_subset (f : α → β) (s t : set α) : f '' (s ∩ t) ⊆ f '' s ∩ f '' t := subset_inter (image_subset _ $ inter_subset_left _ _) (image_subset _ $ inter_subset_right _ _) theorem image_inter_on {f : α → β} {s t : set α} (h : ∀x∈t, ∀y∈s, f x = f y → x = y) : f '' s ∩ f '' t = f '' (s ∩ t) := subset.antisymm (assume b ⟨⟨a₁, ha₁, h₁⟩, ⟨a₂, ha₂, h₂⟩⟩, have a₂ = a₁, from h _ ha₂ _ ha₁ (by simp *), ⟨a₁, ⟨ha₁, this ▸ ha₂⟩, h₁⟩) (image_inter_subset _ _ _) theorem image_inter {f : α → β} {s t : set α} (H : injective f) : f '' s ∩ f '' t = f '' (s ∩ t) := image_inter_on (assume x _ y _ h, H h) theorem image_univ_of_surjective {ι : Type*} {f : ι → β} (H : surjective f) : f '' univ = univ := eq_univ_of_forall $ by { simpa [image] } @[simp] theorem image_singleton {f : α → β} {a : α} : f '' {a} = {f a} := by { ext, simp [image, eq_comm] } theorem nonempty.image_const {s : set α} (hs : s.nonempty) (a : β) : (λ _, a) '' s = {a} := ext $ λ x, ⟨λ ⟨y, _, h⟩, h ▸ mem_singleton _, λ h, (eq_of_mem_singleton h).symm ▸ hs.imp (λ y hy, ⟨hy, rfl⟩)⟩ @[simp] lemma image_eq_empty {α β} {f : α → β} {s : set α} : f '' s = ∅ ↔ s = ∅ := by { simp only [eq_empty_iff_forall_not_mem], exact ⟨λ H a ha, H _ ⟨_, ha, rfl⟩, λ H b ⟨_, ha, _⟩, H _ ha⟩ } lemma inter_singleton_nonempty {s : set α} {a : α} : (s ∩ {a}).nonempty ↔ a ∈ s := by finish [set.nonempty] -- TODO(Jeremy): there is an issue with - t unfolding to compl t theorem mem_compl_image (t : set α) (S : set (set α)) : t ∈ compl '' S ↔ tᶜ ∈ S := begin suffices : ∀ x, xᶜ = t ↔ tᶜ = x, { simp [this] }, intro x, split; { intro e, subst e, simp } end /-- A variant of `image_id` -/ @[simp] lemma image_id' (s : set α) : (λx, x) '' s = s := by { ext, simp } theorem image_id (s : set α) : id '' s = s := by simp theorem compl_compl_image (S : set (set α)) : compl '' (compl '' S) = S := by rw [← image_comp, compl_comp_compl, image_id] theorem image_insert_eq {f : α → β} {a : α} {s : set α} : f '' (insert a s) = insert (f a) (f '' s) := by { ext, simp [and_or_distrib_left, exists_or_distrib, eq_comm, or_comm, and_comm] } theorem image_pair (f : α → β) (a b : α) : f '' {a, b} = {f a, f b} := by simp only [image_insert_eq, image_singleton] theorem image_subset_preimage_of_inverse {f : α → β} {g : β → α} (I : left_inverse g f) (s : set α) : f '' s ⊆ g ⁻¹' s := λ b ⟨a, h, e⟩, e ▸ ((I a).symm ▸ h : g (f a) ∈ s) theorem preimage_subset_image_of_inverse {f : α → β} {g : β → α} (I : left_inverse g f) (s : set β) : f ⁻¹' s ⊆ g '' s := λ b h, ⟨f b, h, I b⟩ theorem image_eq_preimage_of_inverse {f : α → β} {g : β → α} (h₁ : left_inverse g f) (h₂ : right_inverse g f) : image f = preimage g := funext $ λ s, subset.antisymm (image_subset_preimage_of_inverse h₁ s) (preimage_subset_image_of_inverse h₂ s) theorem mem_image_iff_of_inverse {f : α → β} {g : β → α} {b : β} {s : set α} (h₁ : left_inverse g f) (h₂ : right_inverse g f) : b ∈ f '' s ↔ g b ∈ s := by rw image_eq_preimage_of_inverse h₁ h₂; refl theorem image_compl_subset {f : α → β} {s : set α} (H : injective f) : f '' sᶜ ⊆ (f '' s)ᶜ := subset_compl_iff_disjoint.2 $ by simp [image_inter H] theorem subset_image_compl {f : α → β} {s : set α} (H : surjective f) : (f '' s)ᶜ ⊆ f '' sᶜ := compl_subset_iff_union.2 $ by { rw ← image_union, simp [image_univ_of_surjective H] } theorem image_compl_eq {f : α → β} {s : set α} (H : bijective f) : f '' sᶜ = (f '' s)ᶜ := subset.antisymm (image_compl_subset H.1) (subset_image_compl H.2) theorem subset_image_diff (f : α → β) (s t : set α) : f '' s \ f '' t ⊆ f '' (s \ t) := begin rw [diff_subset_iff, ← image_union, union_diff_self], exact image_subset f (subset_union_right t s) end theorem image_diff {f : α → β} (hf : injective f) (s t : set α) : f '' (s \ t) = f '' s \ f '' t := subset.antisymm (subset.trans (image_inter_subset _ _ _) $ inter_subset_inter_right _ $ image_compl_subset hf) (subset_image_diff f s t) lemma nonempty.image (f : α → β) {s : set α} : s.nonempty → (f '' s).nonempty | ⟨x, hx⟩ := ⟨f x, mem_image_of_mem f hx⟩ lemma nonempty.of_image {f : α → β} {s : set α} : (f '' s).nonempty → s.nonempty | ⟨y, x, hx, _⟩ := ⟨x, hx⟩ @[simp] lemma nonempty_image_iff {f : α → β} {s : set α} : (f '' s).nonempty ↔ s.nonempty := ⟨nonempty.of_image, λ h, h.image f⟩ /-- image and preimage are a Galois connection -/ @[simp] theorem image_subset_iff {s : set α} {t : set β} {f : α → β} : f '' s ⊆ t ↔ s ⊆ f ⁻¹' t := ball_image_iff theorem image_preimage_subset (f : α → β) (s : set β) : f '' (f ⁻¹' s) ⊆ s := image_subset_iff.2 (subset.refl _) theorem subset_preimage_image (f : α → β) (s : set α) : s ⊆ f ⁻¹' (f '' s) := λ x, mem_image_of_mem f theorem preimage_image_eq {f : α → β} (s : set α) (h : injective f) : f ⁻¹' (f '' s) = s := subset.antisymm (λ x ⟨y, hy, e⟩, h e ▸ hy) (subset_preimage_image f s) theorem image_preimage_eq {f : α → β} {s : set β} (h : surjective f) : f '' (f ⁻¹' s) = s := subset.antisymm (image_preimage_subset f s) (λ x hx, let ⟨y, e⟩ := h x in ⟨y, (e.symm ▸ hx : f y ∈ s), e⟩) lemma preimage_eq_preimage {f : β → α} (hf : surjective f) : f ⁻¹' s = preimage f t ↔ s = t := iff.intro (assume eq, by rw [← @image_preimage_eq β α f s hf, ← @image_preimage_eq β α f t hf, eq]) (assume eq, eq ▸ rfl) lemma image_inter_preimage (f : α → β) (s : set α) (t : set β) : f '' (s ∩ f ⁻¹' t) = f '' s ∩ t := begin apply subset.antisymm, { calc f '' (s ∩ f ⁻¹' t) ⊆ f '' s ∩ (f '' (f⁻¹' t)) : image_inter_subset _ _ _ ... ⊆ f '' s ∩ t : inter_subset_inter_right _ (image_preimage_subset f t) }, { rintros _ ⟨⟨x, h', rfl⟩, h⟩, exact ⟨x, ⟨h', h⟩, rfl⟩ } end lemma image_preimage_inter (f : α → β) (s : set α) (t : set β) : f '' (f ⁻¹' t ∩ s) = t ∩ f '' s := by simp only [inter_comm, image_inter_preimage] lemma image_diff_preimage {f : α → β} {s : set α} {t : set β} : f '' (s \ f ⁻¹' t) = f '' s \ t := by simp_rw [diff_eq, ← preimage_compl, image_inter_preimage] theorem compl_image : image (compl : set α → set α) = preimage compl := image_eq_preimage_of_inverse compl_compl compl_compl theorem compl_image_set_of {p : set α → Prop} : compl '' {s | p s} = {s | p sᶜ} := congr_fun compl_image p theorem inter_preimage_subset (s : set α) (t : set β) (f : α → β) : s ∩ f ⁻¹' t ⊆ f ⁻¹' (f '' s ∩ t) := λ x h, ⟨mem_image_of_mem _ h.left, h.right⟩ theorem union_preimage_subset (s : set α) (t : set β) (f : α → β) : s ∪ f ⁻¹' t ⊆ f ⁻¹' (f '' s ∪ t) := λ x h, or.elim h (λ l, or.inl $ mem_image_of_mem _ l) (λ r, or.inr r) theorem subset_image_union (f : α → β) (s : set α) (t : set β) : f '' (s ∪ f ⁻¹' t) ⊆ f '' s ∪ t := image_subset_iff.2 (union_preimage_subset _ _ _) lemma preimage_subset_iff {A : set α} {B : set β} {f : α → β} : f⁻¹' B ⊆ A ↔ (∀ a : α, f a ∈ B → a ∈ A) := iff.rfl lemma image_eq_image {f : α → β} (hf : injective f) : f '' s = f '' t ↔ s = t := iff.symm $ iff.intro (assume eq, eq ▸ rfl) $ assume eq, by rw [← preimage_image_eq s hf, ← preimage_image_eq t hf, eq] lemma image_subset_image_iff {f : α → β} (hf : injective f) : f '' s ⊆ f '' t ↔ s ⊆ t := begin refine (iff.symm $ iff.intro (image_subset f) $ assume h, _), rw [← preimage_image_eq s hf, ← preimage_image_eq t hf], exact preimage_mono h end lemma prod_quotient_preimage_eq_image [s : setoid α] (g : quotient s → β) {h : α → β} (Hh : h = g ∘ quotient.mk) (r : set (β × β)) : {x : quotient s × quotient s | (g x.1, g x.2) ∈ r} = (λ a : α × α, (⟦a.1⟧, ⟦a.2⟧)) '' ((λ a : α × α, (h a.1, h a.2)) ⁻¹' r) := Hh.symm ▸ set.ext (λ ⟨a₁, a₂⟩, ⟨quotient.induction_on₂ a₁ a₂ (λ a₁ a₂ h, ⟨(a₁, a₂), h, rfl⟩), λ ⟨⟨b₁, b₂⟩, h₁, h₂⟩, show (g a₁, g a₂) ∈ r, from have h₃ : ⟦b₁⟧ = a₁ ∧ ⟦b₂⟧ = a₂ := prod.ext_iff.1 h₂, h₃.1 ▸ h₃.2 ▸ h₁⟩) /-- Restriction of `f` to `s` factors through `s.image_factorization f : s → f '' s`. -/ def image_factorization (f : α → β) (s : set α) : s → f '' s := λ p, ⟨f p.1, mem_image_of_mem f p.2⟩ lemma image_factorization_eq {f : α → β} {s : set α} : subtype.val ∘ image_factorization f s = f ∘ subtype.val := funext $ λ p, rfl lemma surjective_onto_image {f : α → β} {s : set α} : surjective (image_factorization f s) := λ ⟨_, ⟨a, ha, rfl⟩⟩, ⟨⟨a, ha⟩, rfl⟩ end image /-! ### Subsingleton -/ /-- A set `s` is a `subsingleton`, if it has at most one element. -/ protected def subsingleton (s : set α) : Prop := ∀ ⦃x⦄ (hx : x ∈ s) ⦃y⦄ (hy : y ∈ s), x = y lemma subsingleton.mono (ht : t.subsingleton) (hst : s ⊆ t) : s.subsingleton := λ x hx y hy, ht (hst hx) (hst hy) lemma subsingleton.image (hs : s.subsingleton) (f : α → β) : (f '' s).subsingleton := λ _ ⟨x, hx, Hx⟩ _ ⟨y, hy, Hy⟩, Hx ▸ Hy ▸ congr_arg f (hs hx hy) lemma subsingleton.eq_singleton_of_mem (hs : s.subsingleton) {x:α} (hx : x ∈ s) : s = {x} := ext $ λ y, ⟨λ hy, (hs hx hy) ▸ mem_singleton _, λ hy, (eq_of_mem_singleton hy).symm ▸ hx⟩ lemma subsingleton_empty : (∅ : set α).subsingleton := λ x, false.elim lemma subsingleton_singleton {a} : ({a} : set α).subsingleton := λ x hx y hy, (eq_of_mem_singleton hx).symm ▸ (eq_of_mem_singleton hy).symm ▸ rfl lemma subsingleton.eq_empty_or_singleton (hs : s.subsingleton) : s = ∅ ∨ ∃ x, s = {x} := s.eq_empty_or_nonempty.elim or.inl (λ ⟨x, hx⟩, or.inr ⟨x, hs.eq_singleton_of_mem hx⟩) lemma subsingleton_univ [subsingleton α] : (univ : set α).subsingleton := λ x hx y hy, subsingleton.elim x y /-- `s`, coerced to a type, is a subsingleton type if and only if `s` is a subsingleton set. -/ @[simp, norm_cast] lemma subsingleton_coe (s : set α) : subsingleton s ↔ s.subsingleton := begin split, { refine λ h, (λ a ha b hb, _), exact set_coe.ext_iff.2 (@subsingleton.elim s h ⟨a, ha⟩ ⟨b, hb⟩) }, { exact λ h, subsingleton.intro (λ a b, set_coe.ext (h a.property b.property)) } end theorem univ_eq_true_false : univ = ({true, false} : set Prop) := eq.symm $ eq_univ_of_forall $ classical.cases (by simp) (by simp) /-! ### Lemmas about range of a function. -/ section range variables {f : ι → α} open function /-- Range of a function. This function is more flexible than `f '' univ`, as the image requires that the domain is in Type and not an arbitrary Sort. -/ def range (f : ι → α) : set α := {x | ∃y, f y = x} @[simp] theorem mem_range {x : α} : x ∈ range f ↔ ∃ y, f y = x := iff.rfl @[simp] theorem mem_range_self (i : ι) : f i ∈ range f := ⟨i, rfl⟩ theorem forall_range_iff {p : α → Prop} : (∀ a ∈ range f, p a) ↔ (∀ i, p (f i)) := by simp theorem exists_range_iff {p : α → Prop} : (∃ a ∈ range f, p a) ↔ (∃ i, p (f i)) := by simp lemma exists_range_iff' {p : α → Prop} : (∃ a, a ∈ range f ∧ p a) ↔ ∃ i, p (f i) := by simpa only [exists_prop] using exists_range_iff theorem range_iff_surjective : range f = univ ↔ surjective f := eq_univ_iff_forall @[simp] theorem range_id : range (@id α) = univ := range_iff_surjective.2 surjective_id theorem range_inl_union_range_inr : range (@sum.inl α β) ∪ range sum.inr = univ := by { ext x, cases x; simp } @[simp] theorem range_quot_mk (r : α → α → Prop) : range (quot.mk r) = univ := range_iff_surjective.2 quot.exists_rep @[simp] theorem image_univ {ι : Type*} {f : ι → β} : f '' univ = range f := by { ext, simp [image, range] } theorem image_subset_range {ι : Type*} (f : ι → β) (s : set ι) : f '' s ⊆ range f := by rw ← image_univ; exact image_subset _ (subset_univ _) theorem range_comp {g : α → β} : range (g ∘ f) = g '' range f := subset.antisymm (forall_range_iff.mpr $ assume i, mem_image_of_mem g (mem_range_self _)) (ball_image_iff.mpr $ forall_range_iff.mpr mem_range_self) theorem range_subset_iff {s : set α} : range f ⊆ s ↔ ∀ y, f y ∈ s := forall_range_iff lemma range_comp_subset_range (f : α → β) (g : β → γ) : range (g ∘ f) ⊆ range g := by rw range_comp; apply image_subset_range lemma range_nonempty_iff_nonempty : (range f).nonempty ↔ nonempty ι := ⟨λ ⟨y, x, hxy⟩, ⟨x⟩, λ ⟨x⟩, ⟨f x, mem_range_self x⟩⟩ lemma range_nonempty [h : nonempty ι] (f : ι → α) : (range f).nonempty := range_nonempty_iff_nonempty.2 h @[simp] lemma range_eq_empty {f : ι → α} : range f = ∅ ↔ ¬ nonempty ι := not_nonempty_iff_eq_empty.symm.trans $ not_congr range_nonempty_iff_nonempty @[simp] lemma image_union_image_compl_eq_range (f : α → β) : (f '' s) ∪ (f '' sᶜ) = range f := by rw [← image_union, ← image_univ, ← union_compl_self] theorem image_preimage_eq_inter_range {f : α → β} {t : set β} : f '' (f ⁻¹' t) = t ∩ range f := ext $ assume x, ⟨assume ⟨x, hx, heq⟩, heq ▸ ⟨hx, mem_range_self _⟩, assume ⟨hx, ⟨y, h_eq⟩⟩, h_eq ▸ mem_image_of_mem f $ show y ∈ f ⁻¹' t, by simp [preimage, h_eq, hx]⟩ lemma image_preimage_eq_of_subset {f : α → β} {s : set β} (hs : s ⊆ range f) : f '' (f ⁻¹' s) = s := by rw [image_preimage_eq_inter_range, inter_eq_self_of_subset_left hs] lemma image_preimage_eq_iff {f : α → β} {s : set β} : f '' (f ⁻¹' s) = s ↔ s ⊆ range f := ⟨by { intro h, rw [← h], apply image_subset_range }, image_preimage_eq_of_subset⟩ lemma preimage_subset_preimage_iff {s t : set α} {f : β → α} (hs : s ⊆ range f) : f ⁻¹' s ⊆ f ⁻¹' t ↔ s ⊆ t := begin split, { intros h x hx, rcases hs hx with ⟨y, rfl⟩, exact h hx }, intros h x, apply h end lemma preimage_eq_preimage' {s t : set α} {f : β → α} (hs : s ⊆ range f) (ht : t ⊆ range f) : f ⁻¹' s = f ⁻¹' t ↔ s = t := begin split, { intro h, apply subset.antisymm, rw [←preimage_subset_preimage_iff hs, h], rw [←preimage_subset_preimage_iff ht, h] }, rintro rfl, refl end @[simp] theorem preimage_inter_range {f : α → β} {s : set β} : f ⁻¹' (s ∩ range f) = f ⁻¹' s := set.ext $ λ x, and_iff_left ⟨x, rfl⟩ @[simp] theorem preimage_range_inter {f : α → β} {s : set β} : f ⁻¹' (range f ∩ s) = f ⁻¹' s := by rw [inter_comm, preimage_inter_range] theorem preimage_image_preimage {f : α → β} {s : set β} : f ⁻¹' (f '' (f ⁻¹' s)) = f ⁻¹' s := by rw [image_preimage_eq_inter_range, preimage_inter_range] @[simp] theorem quot_mk_range_eq [setoid α] : range (λx : α, ⟦x⟧) = univ := range_iff_surjective.2 quot.exists_rep lemma range_const_subset {c : α} : range (λx:ι, c) ⊆ {c} := range_subset_iff.2 $ λ x, rfl @[simp] lemma range_const : ∀ [nonempty ι] {c : α}, range (λx:ι, c) = {c} | ⟨x⟩ c := subset.antisymm range_const_subset $ assume y hy, (mem_singleton_iff.1 hy).symm ▸ mem_range_self x lemma diagonal_eq_range {α : Type*} : diagonal α = range (λ x, (x, x)) := by { ext ⟨x, y⟩, simp [diagonal, eq_comm] } theorem preimage_singleton_nonempty {f : α → β} {y : β} : (f ⁻¹' {y}).nonempty ↔ y ∈ range f := iff.rfl theorem preimage_singleton_eq_empty {f : α → β} {y : β} : f ⁻¹' {y} = ∅ ↔ y ∉ range f := not_nonempty_iff_eq_empty.symm.trans $ not_congr preimage_singleton_nonempty lemma range_subset_singleton {f : ι → α} {x : α} : range f ⊆ {x} ↔ f = const ι x := by simp [range_subset_iff, funext_iff, mem_singleton] lemma image_compl_preimage {f : α → β} {s : set β} : f '' ((f ⁻¹' s)ᶜ) = range f \ s := by rw [compl_eq_univ_diff, image_diff_preimage, image_univ] @[simp] theorem range_sigma_mk {β : α → Type*} (a : α) : range (sigma.mk a : β a → Σ a, β a) = sigma.fst ⁻¹' {a} := begin apply subset.antisymm, { rintros _ ⟨b, rfl⟩, simp }, { rintros ⟨x, y⟩ (rfl|_), exact mem_range_self y } end /-- Any map `f : ι → β` factors through a map `range_factorization f : ι → range f`. -/ def range_factorization (f : ι → β) : ι → range f := λ i, ⟨f i, mem_range_self i⟩ lemma range_factorization_eq {f : ι → β} : subtype.val ∘ range_factorization f = f := funext $ λ i, rfl lemma surjective_onto_range : surjective (range_factorization f) := λ ⟨_, ⟨i, rfl⟩⟩, ⟨i, rfl⟩ lemma image_eq_range (f : α → β) (s : set α) : f '' s = range (λ(x : s), f x) := by { ext, split, rintro ⟨x, h1, h2⟩, exact ⟨⟨x, h1⟩, h2⟩, rintro ⟨⟨x, h1⟩, h2⟩, exact ⟨x, h1, h2⟩ } @[simp] lemma sum.elim_range {α β γ : Type*} (f : α → γ) (g : β → γ) : range (sum.elim f g) = range f ∪ range g := by simp [set.ext_iff, mem_range] lemma range_ite_subset' {p : Prop} [decidable p] {f g : α → β} : range (if p then f else g) ⊆ range f ∪ range g := begin by_cases h : p, {rw if_pos h, exact subset_union_left _ _}, {rw if_neg h, exact subset_union_right _ _} end lemma range_ite_subset {p : α → Prop} [decidable_pred p] {f g : α → β} : range (λ x, if p x then f x else g x) ⊆ range f ∪ range g := begin rw range_subset_iff, intro x, by_cases h : p x, simp [if_pos h, mem_union, mem_range_self], simp [if_neg h, mem_union, mem_range_self] end @[simp] lemma preimage_range (f : α → β) : f ⁻¹' (range f) = univ := eq_univ_of_forall mem_range_self /-- The range of a function from a `unique` type contains just the function applied to its single value. -/ lemma range_unique [h : unique ι] : range f = {f $ default ι} := begin ext x, rw mem_range, split, { rintros ⟨i, hi⟩, rw h.uniq i at hi, exact hi ▸ mem_singleton _ }, { exact λ h, ⟨default ι, h.symm⟩ } end end range /-- The set `s` is pairwise `r` if `r x y` for all *distinct* `x y ∈ s`. -/ def pairwise_on (s : set α) (r : α → α → Prop) := ∀ x ∈ s, ∀ y ∈ s, x ≠ y → r x y theorem pairwise_on.mono {s t : set α} {r} (h : t ⊆ s) (hp : pairwise_on s r) : pairwise_on t r := λ x xt y yt, hp x (h xt) y (h yt) theorem pairwise_on.mono' {s : set α} {r r' : α → α → Prop} (H : ∀ a b, r a b → r' a b) (hp : pairwise_on s r) : pairwise_on s r' := λ x xs y ys h, H _ _ (hp x xs y ys h) /-- If and only if `f` takes pairwise equal values on `s`, there is some value it takes everywhere on `s`. -/ lemma pairwise_on_eq_iff_exists_eq [nonempty β] (s : set α) (f : α → β) : (pairwise_on s (λ x y, f x = f y)) ↔ ∃ z, ∀ x ∈ s, f x = z := begin split, { intro h, rcases eq_empty_or_nonempty s with rfl | ⟨x, hx⟩, { exact ⟨classical.arbitrary β, λ x hx, false.elim hx⟩ }, { use f x, intros y hy, by_cases hyx : y = x, { rw hyx }, { exact h y hy x hx hyx } } }, { rintros ⟨z, hz⟩ x hx y hy hne, rw [hz x hx, hz y hy] } end end set open set namespace function variables {ι : Sort*} {α : Type*} {β : Type*} {f : α → β} lemma surjective.preimage_injective (hf : surjective f) : injective (preimage f) := assume s t, (preimage_eq_preimage hf).1 lemma injective.preimage_surjective (hf : injective f) : surjective (preimage f) := by { intro s, use f '' s, rw preimage_image_eq _ hf } lemma surjective.image_surjective (hf : surjective f) : surjective (image f) := by { intro s, use f ⁻¹' s, rw image_preimage_eq hf } lemma injective.image_injective (hf : injective f) : injective (image f) := by { intros s t h, rw [←preimage_image_eq s hf, ←preimage_image_eq t hf, h] } lemma surjective.range_eq {f : ι → α} (hf : surjective f) : range f = univ := range_iff_surjective.2 hf lemma surjective.preimage_subset_preimage_iff {s t : set β} (hf : surjective f) : f ⁻¹' s ⊆ f ⁻¹' t ↔ s ⊆ t := by { apply preimage_subset_preimage_iff, rw [hf.range_eq], apply subset_univ } lemma surjective.range_comp {ι' : Sort*} {f : ι → ι'} (hf : surjective f) (g : ι' → α) : range (g ∘ f) = range g := ext $ λ y, (@surjective.exists _ _ _ hf (λ x, g x = y)).symm lemma injective.nonempty_apply_iff {f : set α → set β} (hf : injective f) (h2 : f ∅ = ∅) {s : set α} : (f s).nonempty ↔ s.nonempty := by rw [← ne_empty_iff_nonempty, ← h2, ← ne_empty_iff_nonempty, hf.ne_iff] end function open function /-! ### Image and preimage on subtypes -/ namespace subtype variable {α : Type*} lemma coe_image {p : α → Prop} {s : set (subtype p)} : coe '' s = {x | ∃h : p x, (⟨x, h⟩ : subtype p) ∈ s} := set.ext $ assume a, ⟨assume ⟨⟨a', ha'⟩, in_s, h_eq⟩, h_eq ▸ ⟨ha', in_s⟩, assume ⟨ha, in_s⟩, ⟨⟨a, ha⟩, in_s, rfl⟩⟩ lemma range_coe {s : set α} : range (coe : s → α) = s := by { rw ← set.image_univ, simp [-set.image_univ, coe_image] } /-- A variant of `range_coe`. Try to use `range_coe` if possible. This version is useful when defining a new type that is defined as the subtype of something. In that case, the coercion doesn't fire anymore. -/ lemma range_val {s : set α} : range (subtype.val : s → α) = s := range_coe /-- We make this the simp lemma instead of `range_coe`. The reason is that if we write for `s : set α` the function `coe : s → α`, then the inferred implicit arguments of `coe` are `coe α (λ x, x ∈ s)`. -/ @[simp] lemma range_coe_subtype {p : α → Prop} : range (coe : subtype p → α) = {x | p x} := range_coe @[simp] lemma coe_preimage_self (s : set α) : (coe : s → α) ⁻¹' s = univ := by rw [← preimage_range (coe : s → α), range_coe] lemma range_val_subtype {p : α → Prop} : range (subtype.val : subtype p → α) = {x | p x} := range_coe theorem coe_image_subset (s : set α) (t : set s) : coe '' t ⊆ s := λ x ⟨y, yt, yvaleq⟩, by rw ←yvaleq; exact y.property theorem coe_image_univ (s : set α) : (coe : s → α) '' set.univ = s := image_univ.trans range_coe @[simp] theorem image_preimage_coe (s t : set α) : (coe : s → α) '' (coe ⁻¹' t) = t ∩ s := image_preimage_eq_inter_range.trans $ congr_arg _ range_coe theorem image_preimage_val (s t : set α) : (subtype.val : s → α) '' (subtype.val ⁻¹' t) = t ∩ s := image_preimage_coe s t theorem preimage_coe_eq_preimage_coe_iff {s t u : set α} : ((coe : s → α) ⁻¹' t = coe ⁻¹' u) ↔ t ∩ s = u ∩ s := begin rw [←image_preimage_coe, ←image_preimage_coe], split, { intro h, rw h }, intro h, exact coe_injective.image_injective h end theorem preimage_val_eq_preimage_val_iff (s t u : set α) : ((subtype.val : s → α) ⁻¹' t = subtype.val ⁻¹' u) ↔ (t ∩ s = u ∩ s) := preimage_coe_eq_preimage_coe_iff lemma exists_set_subtype {t : set α} (p : set α → Prop) : (∃(s : set t), p (coe '' s)) ↔ ∃(s : set α), s ⊆ t ∧ p s := begin split, { rintro ⟨s, hs⟩, refine ⟨coe '' s, _, hs⟩, convert image_subset_range _ _, rw [range_coe] }, rintro ⟨s, hs₁, hs₂⟩, refine ⟨coe ⁻¹' s, _⟩, rw [image_preimage_eq_of_subset], exact hs₂, rw [range_coe], exact hs₁ end lemma preimage_coe_nonempty {s t : set α} : ((coe : s → α) ⁻¹' t).nonempty ↔ (s ∩ t).nonempty := by rw [inter_comm, ← image_preimage_coe, nonempty_image_iff] end subtype namespace set /-! ### Lemmas about cartesian product of sets -/ section prod variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} variables {s s₁ s₂ : set α} {t t₁ t₂ : set β} /-- The cartesian product `prod s t` is the set of `(a, b)` such that `a ∈ s` and `b ∈ t`. -/ protected def prod (s : set α) (t : set β) : set (α × β) := {p | p.1 ∈ s ∧ p.2 ∈ t} lemma prod_eq (s : set α) (t : set β) : s.prod t = prod.fst ⁻¹' s ∩ prod.snd ⁻¹' t := rfl theorem mem_prod_eq {p : α × β} : p ∈ s.prod t = (p.1 ∈ s ∧ p.2 ∈ t) := rfl @[simp] theorem mem_prod {p : α × β} : p ∈ s.prod t ↔ p.1 ∈ s ∧ p.2 ∈ t := iff.rfl @[simp] theorem prod_mk_mem_set_prod_eq {a : α} {b : β} : (a, b) ∈ s.prod t = (a ∈ s ∧ b ∈ t) := rfl lemma mk_mem_prod {a : α} {b : β} (a_in : a ∈ s) (b_in : b ∈ t) : (a, b) ∈ s.prod t := ⟨a_in, b_in⟩ theorem prod_mono {s₁ s₂ : set α} {t₁ t₂ : set β} (hs : s₁ ⊆ s₂) (ht : t₁ ⊆ t₂) : s₁.prod t₁ ⊆ s₂.prod t₂ := assume x ⟨h₁, h₂⟩, ⟨hs h₁, ht h₂⟩ lemma prod_subset_iff {P : set (α × β)} : (s.prod t ⊆ P) ↔ ∀ (x ∈ s) (y ∈ t), (x, y) ∈ P := ⟨λ h _ xin _ yin, h (mk_mem_prod xin yin), λ h ⟨_, _⟩ pin, h _ pin.1 _ pin.2⟩ lemma forall_prod_set {p : α × β → Prop} : (∀ x ∈ s.prod t, p x) ↔ ∀ (x ∈ s) (y ∈ t), p (x, y) := prod_subset_iff @[simp] theorem prod_empty : s.prod ∅ = (∅ : set (α × β)) := by { ext, simp } @[simp] theorem empty_prod : set.prod ∅ t = (∅ : set (α × β)) := by { ext, simp } @[simp] theorem univ_prod_univ : (@univ α).prod (@univ β) = univ := by { ext ⟨x, y⟩, simp } lemma univ_prod {t : set β} : set.prod (univ : set α) t = prod.snd ⁻¹' t := by simp [prod_eq] lemma prod_univ {s : set α} : set.prod s (univ : set β) = prod.fst ⁻¹' s := by simp [prod_eq] @[simp] theorem singleton_prod {a : α} : set.prod {a} t = prod.mk a '' t := by { ext ⟨x, y⟩, simp [and.left_comm, eq_comm] } @[simp] theorem prod_singleton {b : β} : s.prod {b} = (λ a, (a, b)) '' s := by { ext ⟨x, y⟩, simp [and.left_comm, eq_comm] } theorem singleton_prod_singleton {a : α} {b : β} : set.prod {a} {b} = ({(a, b)} : set (α × β)) := by simp @[simp] theorem union_prod : (s₁ ∪ s₂).prod t = s₁.prod t ∪ s₂.prod t := by { ext ⟨x, y⟩, simp [or_and_distrib_right] } @[simp] theorem prod_union : s.prod (t₁ ∪ t₂) = s.prod t₁ ∪ s.prod t₂ := by { ext ⟨x, y⟩, simp [and_or_distrib_left] } theorem prod_inter_prod : s₁.prod t₁ ∩ s₂.prod t₂ = (s₁ ∩ s₂).prod (t₁ ∩ t₂) := by { ext ⟨x, y⟩, simp [and_assoc, and.left_comm] } theorem insert_prod {a : α} : (insert a s).prod t = (prod.mk a '' t) ∪ s.prod t := by { ext ⟨x, y⟩, simp [image, iff_def, or_imp_distrib, imp.swap] {contextual := tt} } theorem prod_insert {b : β} : s.prod (insert b t) = ((λa, (a, b)) '' s) ∪ s.prod t := by { ext ⟨x, y⟩, simp [image, iff_def, or_imp_distrib, imp.swap] {contextual := tt} } theorem prod_preimage_eq {f : γ → α} {g : δ → β} : (preimage f s).prod (preimage g t) = preimage (λp, (f p.1, g p.2)) (s.prod t) := rfl @[simp] lemma mk_preimage_prod_left {y : β} (h : y ∈ t) : (λ x, (x, y)) ⁻¹' s.prod t = s := by { ext x, simp [h] } @[simp] lemma mk_preimage_prod_right {x : α} (h : x ∈ s) : prod.mk x ⁻¹' s.prod t = t := by { ext y, simp [h] } @[simp] lemma mk_preimage_prod_left_eq_empty {y : β} (hy : y ∉ t) : (λ x, (x, y)) ⁻¹' s.prod t = ∅ := by { ext z, simp [hy] } @[simp] lemma mk_preimage_prod_right_eq_empty {x : α} (hx : x ∉ s) : prod.mk x ⁻¹' s.prod t = ∅ := by { ext z, simp [hx] } lemma mk_preimage_prod_left_eq_if {y : β} [decidable_pred (∈ t)] : (λ x, (x, y)) ⁻¹' s.prod t = if y ∈ t then s else ∅ := by { split_ifs; simp [h] } lemma mk_preimage_prod_right_eq_if {x : α} [decidable_pred (∈ s)] : prod.mk x ⁻¹' s.prod t = if x ∈ s then t else ∅ := by { split_ifs; simp [h] } theorem image_swap_eq_preimage_swap : image (@prod.swap α β) = preimage prod.swap := image_eq_preimage_of_inverse prod.swap_left_inverse prod.swap_right_inverse theorem preimage_swap_prod {s : set α} {t : set β} : prod.swap ⁻¹' t.prod s = s.prod t := by { ext ⟨x, y⟩, simp [and_comm] } theorem image_swap_prod : prod.swap '' t.prod s = s.prod t := by rw [image_swap_eq_preimage_swap, preimage_swap_prod] theorem prod_image_image_eq {m₁ : α → γ} {m₂ : β → δ} : (image m₁ s).prod (image m₂ t) = image (λp:α×β, (m₁ p.1, m₂ p.2)) (s.prod t) := ext $ by simp [-exists_and_distrib_right, exists_and_distrib_right.symm, and.left_comm, and.assoc, and.comm] theorem prod_range_range_eq {α β γ δ} {m₁ : α → γ} {m₂ : β → δ} : (range m₁).prod (range m₂) = range (λp:α×β, (m₁ p.1, m₂ p.2)) := ext $ by simp [range] theorem prod_range_univ_eq {α β γ} {m₁ : α → γ} : (range m₁).prod (univ : set β) = range (λp:α×β, (m₁ p.1, p.2)) := ext $ by simp [range] theorem prod_univ_range_eq {α β δ} {m₂ : β → δ} : (univ : set α).prod (range m₂) = range (λp:α×β, (p.1, m₂ p.2)) := ext $ by simp [range] theorem nonempty.prod : s.nonempty → t.nonempty → (s.prod t).nonempty | ⟨x, hx⟩ ⟨y, hy⟩ := ⟨(x, y), ⟨hx, hy⟩⟩ theorem nonempty.fst : (s.prod t).nonempty → s.nonempty | ⟨p, hp⟩ := ⟨p.1, hp.1⟩ theorem nonempty.snd : (s.prod t).nonempty → t.nonempty | ⟨p, hp⟩ := ⟨p.2, hp.2⟩ theorem prod_nonempty_iff : (s.prod t).nonempty ↔ s.nonempty ∧ t.nonempty := ⟨λ h, ⟨h.fst, h.snd⟩, λ h, nonempty.prod h.1 h.2⟩ theorem prod_eq_empty_iff : s.prod t = ∅ ↔ (s = ∅ ∨ t = ∅) := by simp only [not_nonempty_iff_eq_empty.symm, prod_nonempty_iff, not_and_distrib] lemma prod_sub_preimage_iff {W : set γ} {f : α × β → γ} : s.prod t ⊆ f ⁻¹' W ↔ ∀ a b, a ∈ s → b ∈ t → f (a, b) ∈ W := by simp [subset_def] lemma fst_image_prod_subset (s : set α) (t : set β) : prod.fst '' (s.prod t) ⊆ s := λ _ h, let ⟨_, ⟨h₂, _⟩, h₁⟩ := (set.mem_image _ _ _).1 h in h₁ ▸ h₂ lemma prod_subset_preimage_fst (s : set α) (t : set β) : s.prod t ⊆ prod.fst ⁻¹' s := image_subset_iff.1 (fst_image_prod_subset s t) lemma fst_image_prod (s : set β) {t : set α} (ht : t.nonempty) : prod.fst '' (s.prod t) = s := set.subset.antisymm (fst_image_prod_subset _ _) $ λ y y_in, let ⟨x, x_in⟩ := ht in ⟨(y, x), ⟨y_in, x_in⟩, rfl⟩ lemma snd_image_prod_subset (s : set α) (t : set β) : prod.snd '' (s.prod t) ⊆ t := λ _ h, let ⟨_, ⟨_, h₂⟩, h₁⟩ := (set.mem_image _ _ _).1 h in h₁ ▸ h₂ lemma prod_subset_preimage_snd (s : set α) (t : set β) : s.prod t ⊆ prod.snd ⁻¹' t := image_subset_iff.1 (snd_image_prod_subset s t) lemma snd_image_prod {s : set α} (hs : s.nonempty) (t : set β) : prod.snd '' (s.prod t) = t := set.subset.antisymm (snd_image_prod_subset _ _) $ λ y y_in, let ⟨x, x_in⟩ := hs in ⟨(x, y), ⟨x_in, y_in⟩, rfl⟩ /-- A product set is included in a product set if and only factors are included, or a factor of the first set is empty. -/ lemma prod_subset_prod_iff : (s.prod t ⊆ s₁.prod t₁) ↔ (s ⊆ s₁ ∧ t ⊆ t₁) ∨ (s = ∅) ∨ (t = ∅) := begin classical, cases (s.prod t).eq_empty_or_nonempty with h h, { simp [h, prod_eq_empty_iff.1 h] }, { have st : s.nonempty ∧ t.nonempty, by rwa [prod_nonempty_iff] at h, split, { assume H : s.prod t ⊆ s₁.prod t₁, have h' : s₁.nonempty ∧ t₁.nonempty := prod_nonempty_iff.1 (h.mono H), refine or.inl ⟨_, _⟩, show s ⊆ s₁, { have := image_subset (prod.fst : α × β → α) H, rwa [fst_image_prod _ st.2, fst_image_prod _ h'.2] at this }, show t ⊆ t₁, { have := image_subset (prod.snd : α × β → β) H, rwa [snd_image_prod st.1, snd_image_prod h'.1] at this } }, { assume H, simp only [st.1.ne_empty, st.2.ne_empty, or_false] at H, exact prod_mono H.1 H.2 } } end end prod /-! ### Lemmas about set-indexed products of sets -/ section pi variables {ι : Type*} {α : ι → Type*} {s : set ι} {t t₁ t₂ : Π i, set (α i)} /-- Given an index set `i` and a family of sets `s : Π i, set (α i)`, `pi i s` is the set of dependent functions `f : Πa, π a` such that `f a` belongs to `π a` whenever `a ∈ i`. -/ def pi (s : set ι) (t : Π i, set (α i)) : set (Π i, α i) := { f | ∀i ∈ s, f i ∈ t i } @[simp] lemma mem_pi {f : Π i, α i} : f ∈ s.pi t ↔ ∀ i ∈ s, f i ∈ t i := by refl @[simp] lemma mem_univ_pi {f : Π i, α i} : f ∈ pi univ t ↔ ∀ i, f i ∈ t i := by simp @[simp] lemma empty_pi (s : Π i, set (α i)) : pi ∅ s = univ := by { ext, simp [pi] } lemma pi_eq_empty {i : ι} (hs : i ∈ s) (ht : t i = ∅) : s.pi t = ∅ := by { ext f, simp only [mem_empty_eq, not_forall, iff_false, mem_pi, not_imp], exact ⟨i, hs, by simp [ht]⟩ } lemma univ_pi_eq_empty {i : ι} (ht : t i = ∅) : pi univ t = ∅ := pi_eq_empty (mem_univ i) ht lemma pi_nonempty_iff : (s.pi t).nonempty ↔ ∀ i, ∃ x, i ∈ s → x ∈ t i := by simp [classical.skolem, set.nonempty] lemma univ_pi_nonempty_iff : (pi univ t).nonempty ↔ ∀ i, (t i).nonempty := by simp [classical.skolem, set.nonempty] lemma pi_eq_empty_iff : s.pi t = ∅ ↔ ∃ i, (α i → false) ∨ (i ∈ s ∧ t i = ∅) := begin rw [← not_nonempty_iff_eq_empty, pi_nonempty_iff], push_neg, apply exists_congr, intro i, split, { intro h, by_cases hα : nonempty (α i), { cases hα with x, refine or.inr ⟨(h x).1, by simp [eq_empty_iff_forall_not_mem, h]⟩ }, { exact or.inl (λ x, hα ⟨x⟩) }}, { rintro (h|h) x, exfalso, exact h x, simp [h] } end lemma univ_pi_eq_empty_iff : pi univ t = ∅ ↔ ∃ i, t i = ∅ := by simp [← not_nonempty_iff_eq_empty, univ_pi_nonempty_iff] @[simp] lemma insert_pi (i : ι) (s : set ι) (t : Π i, set (α i)) : pi (insert i s) t = (eval i ⁻¹' t i) ∩ pi s t := by { ext, simp [pi, or_imp_distrib, forall_and_distrib] } @[simp] lemma singleton_pi (i : ι) (t : Π i, set (α i)) : pi {i} t = (eval i ⁻¹' t i) := by { ext, simp [pi] } lemma pi_if {p : ι → Prop} [h : decidable_pred p] (s : set ι) (t₁ t₂ : Π i, set (α i)) : pi s (λ i, if p i then t₁ i else t₂ i) = pi {i ∈ s | p i} t₁ ∩ pi {i ∈ s | ¬ p i} t₂ := begin ext f, split, { assume h, split; { rintros i ⟨his, hpi⟩, simpa [*] using h i } }, { rintros ⟨ht₁, ht₂⟩ i his, by_cases p i; simp * at * } end open_locale classical lemma eval_image_pi {i : ι} (hs : i ∈ s) (ht : (s.pi t).nonempty) : eval i '' s.pi t = t i := begin ext x, rcases ht with ⟨f, hf⟩, split, { rintro ⟨g, hg, rfl⟩, exact hg i hs }, { intro hg, refine ⟨update f i x, _, by simp⟩, intros j hj, by_cases hji : j = i, { subst hji, simp [hg] }, { rw [mem_pi] at hf, simp [hji, hf, hj] }}, end @[simp] lemma eval_image_univ_pi {i : ι} (ht : (pi univ t).nonempty) : (λ f : Π i, α i, f i) '' pi univ t = t i := eval_image_pi (mem_univ i) ht lemma update_preimage_pi {i : ι} {f : Π i, α i} (hi : i ∈ s) (hf : ∀ j ∈ s, j ≠ i → f j ∈ t j) : (update f i) ⁻¹' s.pi t = t i := begin ext x, split, { intro h, convert h i hi, simp }, { intros hx j hj, by_cases h : j = i, { cases h, simpa }, { rw [update_noteq h], exact hf j hj h }} end lemma update_preimage_univ_pi {i : ι} {f : Π i, α i} (hf : ∀ j ≠ i, f j ∈ t j) : (update f i) ⁻¹' pi univ t = t i := update_preimage_pi (mem_univ i) (λ j _, hf j) lemma subset_pi_eval_image (s : set ι) (u : set (Π i, α i)) : u ⊆ pi s (λ i, eval i '' u) := λ f hf i hi, ⟨f, hf, rfl⟩ end pi /-! ### Lemmas about `inclusion`, the injection of subtypes induced by `⊆` -/ section inclusion variable {α : Type*} /-- `inclusion` is the "identity" function between two subsets `s` and `t`, where `s ⊆ t` -/ def inclusion {s t : set α} (h : s ⊆ t) : s → t := λ x : s, (⟨x, h x.2⟩ : t) @[simp] lemma inclusion_self {s : set α} (x : s) : inclusion (set.subset.refl _) x = x := by { cases x, refl } @[simp] lemma inclusion_right {s t : set α} (h : s ⊆ t) (x : t) (m : (x : α) ∈ s) : inclusion h ⟨x, m⟩ = x := by { cases x, refl } @[simp] lemma inclusion_inclusion {s t u : set α} (hst : s ⊆ t) (htu : t ⊆ u) (x : s) : inclusion htu (inclusion hst x) = inclusion (set.subset.trans hst htu) x := by { cases x, refl } @[simp] lemma coe_inclusion {s t : set α} (h : s ⊆ t) (x : s) : (inclusion h x : α) = (x : α) := rfl lemma inclusion_injective {s t : set α} (h : s ⊆ t) : function.injective (inclusion h) | ⟨_, _⟩ ⟨_, _⟩ := subtype.ext_iff_val.2 ∘ subtype.ext_iff_val.1 lemma range_inclusion {s t : set α} (h : s ⊆ t) : range (inclusion h) = {x : t | (x:α) ∈ s} := by { ext ⟨x, hx⟩, simp [inclusion] } end inclusion /-! ### Injectivity and surjectivity lemmas for image and preimage -/ section image_preimage variables {α : Type u} {β : Type v} {f : α → β} @[simp] lemma preimage_injective : injective (preimage f) ↔ surjective f := begin refine ⟨λ h y, _, surjective.preimage_injective⟩, obtain ⟨x, hx⟩ : (f ⁻¹' {y}).nonempty, { rw [h.nonempty_apply_iff preimage_empty], apply singleton_nonempty }, exact ⟨x, hx⟩ end @[simp] lemma preimage_surjective : surjective (preimage f) ↔ injective f := begin refine ⟨λ h x x' hx, _, injective.preimage_surjective⟩, cases h {x} with s hs, have := mem_singleton x, rwa [← hs, mem_preimage, hx, ← mem_preimage, hs, mem_singleton_iff, eq_comm] at this end @[simp] lemma image_surjective : surjective (image f) ↔ surjective f := begin refine ⟨λ h y, _, surjective.image_surjective⟩, cases h {y} with s hs, have := mem_singleton y, rw [← hs] at this, rcases this with ⟨x, h1x, h2x⟩, exact ⟨x, h2x⟩ end @[simp] lemma image_injective : injective (image f) ↔ injective f := begin refine ⟨λ h x x' hx, _, injective.image_injective⟩, rw [← singleton_eq_singleton_iff], apply h, rw [image_singleton, image_singleton, hx] end end image_preimage /-! ### Lemmas about images of binary and ternary functions -/ section n_ary_image variables {α β γ δ ε : Type*} {f f' : α → β → γ} {g g' : α → β → γ → δ} variables {s s' : set α} {t t' : set β} {u u' : set γ} {a a' : α} {b b' : β} {c c' : γ} {d d' : δ} /-- The image of a binary function `f : α → β → γ` as a function `set α → set β → set γ`. Mathematically this should be thought of as the image of the corresponding function `α × β → γ`. -/ def image2 (f : α → β → γ) (s : set α) (t : set β) : set γ := {c | ∃ a b, a ∈ s ∧ b ∈ t ∧ f a b = c } lemma mem_image2_eq : c ∈ image2 f s t = ∃ a b, a ∈ s ∧ b ∈ t ∧ f a b = c := rfl @[simp] lemma mem_image2 : c ∈ image2 f s t ↔ ∃ a b, a ∈ s ∧ b ∈ t ∧ f a b = c := iff.rfl lemma mem_image2_of_mem (h1 : a ∈ s) (h2 : b ∈ t) : f a b ∈ image2 f s t := ⟨a, b, h1, h2, rfl⟩ lemma mem_image2_iff (hf : injective2 f) : f a b ∈ image2 f s t ↔ a ∈ s ∧ b ∈ t := ⟨ by { rintro ⟨a', b', ha', hb', h⟩, rcases hf h with ⟨rfl, rfl⟩, exact ⟨ha', hb'⟩ }, λ ⟨ha, hb⟩, mem_image2_of_mem ha hb⟩ /-- image2 is monotone with respect to `⊆`. -/ lemma image2_subset (hs : s ⊆ s') (ht : t ⊆ t') : image2 f s t ⊆ image2 f s' t' := by { rintro _ ⟨a, b, ha, hb, rfl⟩, exact mem_image2_of_mem (hs ha) (ht hb) } lemma image2_union_left : image2 f (s ∪ s') t = image2 f s t ∪ image2 f s' t := begin ext c, split, { rintros ⟨a, b, h1a|h2a, hb, rfl⟩;[left, right]; exact ⟨_, _, ‹_›, ‹_›, rfl⟩ }, { rintro (⟨_, _, _, _, rfl⟩|⟨_, _, _, _, rfl⟩); refine ⟨_, _, _, ‹_›, rfl⟩; simp [mem_union, *] } end lemma image2_union_right : image2 f s (t ∪ t') = image2 f s t ∪ image2 f s t' := begin ext c, split, { rintros ⟨a, b, ha, h1b|h2b, rfl⟩;[left, right]; exact ⟨_, _, ‹_›, ‹_›, rfl⟩ }, { rintro (⟨_, _, _, _, rfl⟩|⟨_, _, _, _, rfl⟩); refine ⟨_, _, ‹_›, _, rfl⟩; simp [mem_union, *] } end @[simp] lemma image2_empty_left : image2 f ∅ t = ∅ := ext $ by simp @[simp] lemma image2_empty_right : image2 f s ∅ = ∅ := ext $ by simp lemma image2_inter_subset_left : image2 f (s ∩ s') t ⊆ image2 f s t ∩ image2 f s' t := by { rintro _ ⟨a, b, ⟨h1a, h2a⟩, hb, rfl⟩, split; exact ⟨_, _, ‹_›, ‹_›, rfl⟩ } lemma image2_inter_subset_right : image2 f s (t ∩ t') ⊆ image2 f s t ∩ image2 f s t' := by { rintro _ ⟨a, b, ha, ⟨h1b, h2b⟩, rfl⟩, split; exact ⟨_, _, ‹_›, ‹_›, rfl⟩ } @[simp] lemma image2_singleton : image2 f {a} {b} = {f a b} := ext $ λ x, by simp [eq_comm] lemma image2_singleton_left : image2 f {a} t = f a '' t := ext $ λ x, by simp lemma image2_singleton_right : image2 f s {b} = (λ a, f a b) '' s := ext $ λ x, by simp @[congr] lemma image2_congr (h : ∀ (a ∈ s) (b ∈ t), f a b = f' a b) : image2 f s t = image2 f' s t := by { ext, split; rintro ⟨a, b, ha, hb, rfl⟩; refine ⟨a, b, ha, hb, by rw h a ha b hb⟩ } /-- A common special case of `image2_congr` -/ lemma image2_congr' (h : ∀ a b, f a b = f' a b) : image2 f s t = image2 f' s t := image2_congr (λ a _ b _, h a b) /-- The image of a ternary function `f : α → β → γ → δ` as a function `set α → set β → set γ → set δ`. Mathematically this should be thought of as the image of the corresponding function `α × β × γ → δ`. -/ def image3 (g : α → β → γ → δ) (s : set α) (t : set β) (u : set γ) : set δ := {d | ∃ a b c, a ∈ s ∧ b ∈ t ∧ c ∈ u ∧ g a b c = d } @[simp] lemma mem_image3 : d ∈ image3 g s t u ↔ ∃ a b c, a ∈ s ∧ b ∈ t ∧ c ∈ u ∧ g a b c = d := iff.rfl @[congr] lemma image3_congr (h : ∀ (a ∈ s) (b ∈ t) (c ∈ u), g a b c = g' a b c) : image3 g s t u = image3 g' s t u := by { ext x, split; rintro ⟨a, b, c, ha, hb, hc, rfl⟩; refine ⟨a, b, c, ha, hb, hc, by rw h a ha b hb c hc⟩ } /-- A common special case of `image3_congr` -/ lemma image3_congr' (h : ∀ a b c, g a b c = g' a b c) : image3 g s t u = image3 g' s t u := image3_congr (λ a _ b _ c _, h a b c) lemma image2_image2_left (f : δ → γ → ε) (g : α → β → δ) : image2 f (image2 g s t) u = image3 (λ a b c, f (g a b) c) s t u := begin ext, split, { rintro ⟨_, c, ⟨a, b, ha, hb, rfl⟩, hc, rfl⟩, refine ⟨a, b, c, ha, hb, hc, rfl⟩ }, { rintro ⟨a, b, c, ha, hb, hc, rfl⟩, refine ⟨_, c, ⟨a, b, ha, hb, rfl⟩, hc, rfl⟩ } end lemma image2_image2_right (f : α → δ → ε) (g : β → γ → δ) : image2 f s (image2 g t u) = image3 (λ a b c, f a (g b c)) s t u := begin ext, split, { rintro ⟨a, _, ha, ⟨b, c, hb, hc, rfl⟩, rfl⟩, refine ⟨a, b, c, ha, hb, hc, rfl⟩ }, { rintro ⟨a, b, c, ha, hb, hc, rfl⟩, refine ⟨a, _, ha, ⟨b, c, hb, hc, rfl⟩, rfl⟩ } end lemma image_image2 (f : α → β → γ) (g : γ → δ) : g '' image2 f s t = image2 (λ a b, g (f a b)) s t := begin ext, split, { rintro ⟨_, ⟨a, b, ha, hb, rfl⟩, rfl⟩, refine ⟨a, b, ha, hb, rfl⟩ }, { rintro ⟨a, b, ha, hb, rfl⟩, refine ⟨_, ⟨a, b, ha, hb, rfl⟩, rfl⟩ } end lemma image2_image_left (f : γ → β → δ) (g : α → γ) : image2 f (g '' s) t = image2 (λ a b, f (g a) b) s t := begin ext, split, { rintro ⟨_, b, ⟨a, ha, rfl⟩, hb, rfl⟩, refine ⟨a, b, ha, hb, rfl⟩ }, { rintro ⟨a, b, ha, hb, rfl⟩, refine ⟨_, b, ⟨a, ha, rfl⟩, hb, rfl⟩ } end lemma image2_image_right (f : α → γ → δ) (g : β → γ) : image2 f s (g '' t) = image2 (λ a b, f a (g b)) s t := begin ext, split, { rintro ⟨a, _, ha, ⟨b, hb, rfl⟩, rfl⟩, refine ⟨a, b, ha, hb, rfl⟩ }, { rintro ⟨a, b, ha, hb, rfl⟩, refine ⟨a, _, ha, ⟨b, hb, rfl⟩, rfl⟩ } end lemma image2_swap (f : α → β → γ) (s : set α) (t : set β) : image2 f s t = image2 (λ a b, f b a) t s := by { ext, split; rintro ⟨a, b, ha, hb, rfl⟩; refine ⟨b, a, hb, ha, rfl⟩ } @[simp] lemma image2_left (h : t.nonempty) : image2 (λ x y, x) s t = s := by simp [nonempty_def.mp h, ext_iff] @[simp] lemma image2_right (h : s.nonempty) : image2 (λ x y, y) s t = t := by simp [nonempty_def.mp h, ext_iff] @[simp] lemma image_prod (f : α → β → γ) : (λ x : α × β, f x.1 x.2) '' s.prod t = image2 f s t := set.ext $ λ a, ⟨ by { rintros ⟨_, _, rfl⟩, exact ⟨_, _, (mem_prod.mp ‹_›).1, (mem_prod.mp ‹_›).2, rfl⟩ }, by { rintros ⟨_, _, _, _, rfl⟩, exact ⟨(_, _), mem_prod.mpr ⟨‹_›, ‹_›⟩, rfl⟩ }⟩ lemma nonempty.image2 (hs : s.nonempty) (ht : t.nonempty) : (image2 f s t).nonempty := by { cases hs with a ha, cases ht with b hb, exact ⟨f a b, ⟨a, b, ha, hb, rfl⟩⟩ } end n_ary_image end set namespace subsingleton variables {α : Type*} [subsingleton α] lemma eq_univ_of_nonempty {s : set α} : s.nonempty → s = univ := λ ⟨x, hx⟩, eq_univ_of_forall $ λ y, subsingleton.elim x y ▸ hx @[elab_as_eliminator] lemma set_cases {p : set α → Prop} (h0 : p ∅) (h1 : p univ) (s) : p s := s.eq_empty_or_nonempty.elim (λ h, h.symm ▸ h0) $ λ h, (eq_univ_of_nonempty h).symm ▸ h1 end subsingleton
598665a94722f14348e46be698d36233a938d629
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/tactic/simp_rw.lean
ae85407c107b5fb8bf7d785536e3d5b364555f89
[ "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
2,294
lean
/- Copyright (c) 2020 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen -/ import tactic.core /-! # The `simp_rw` tactic This module defines a tactic `simp_rw` which functions as a mix of `simp` and `rw`. Like `rw`, it applies each rewrite rule in the given order, but like `simp` it repeatedly applies these rules and also under binders like `∀ x, ...`, `∃ x, ...` and `λ x, ...`. ## Implementation notes The tactic works by taking each rewrite rule in turn and applying `simp only` to it. Arguments to `simp_rw` are of the format used by `rw` and are translated to their equivalents for `simp`. -/ namespace tactic.interactive open interactive interactive.types tactic /-- `simp_rw` functions as a mix of `simp` and `rw`. Like `rw`, it applies each rewrite rule in the given order, but like `simp` it repeatedly applies these rules and also under binders like `∀ x, ...`, `∃ x, ...` and `λ x, ...`. Usage: - `simp_rw [lemma_1, ..., lemma_n]` will rewrite the goal by applying the lemmas in that order. A lemma preceded by `←` is applied in the reverse direction. - `simp_rw [lemma_1, ..., lemma_n] at h₁ ... hₙ` will rewrite the given hypotheses. - `simp_rw [...] at ⊢ h₁ ... hₙ` rewrites the goal as well as the given hypotheses. - `simp_rw [...] at *` rewrites in the whole context: all hypotheses and the goal. Lemmas passed to `simp_rw` must be expressions that are valid arguments to `simp`. For example, neither `simp` nor `rw` can solve the following, but `simp_rw` can: ```lean example {α β : Type} {f : α → β} {t : set β} : (∀ s, f '' s ⊆ t) = ∀ s : set α, ∀ x ∈ s, x ∈ f ⁻¹' t := by simp_rw [set.image_subset_iff, set.subset_def] ``` -/ meta def simp_rw (q : parse rw_rules) (l : parse location) : tactic unit := q.rules.mmap' (λ rule, do let simp_arg := if rule.symm then simp_arg_type.symm_expr rule.rule else simp_arg_type.expr rule.rule, save_info rule.pos, simp none none tt [simp_arg] [] l) -- equivalent to `simp only [rule] at l` add_tactic_doc { name := "simp_rw", category := doc_category.tactic, decl_names := [`tactic.interactive.simp_rw], tags := ["simplification"] } end tactic.interactive
2bd3687fdf9c14f821a3087bc01563801916a3c0
54d7e71c3616d331b2ec3845d31deb08f3ff1dea
/tests/lean/defeq_simp5.lean
5018144a5b83091dfeaa7909c21f2a3d140af9c1
[ "Apache-2.0" ]
permissive
pachugupta/lean
6f3305c4292288311cc4ab4550060b17d49ffb1d
0d02136a09ac4cf27b5c88361750e38e1f485a1a
refs/heads/master
1,611,110,653,606
1,493,130,117,000
1,493,167,649,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
776
lean
attribute [reducible] definition nat_has_add2 : has_add nat := has_add.mk (λ x y : nat, x + y) attribute [reducible] definition nat_has_add3 : nat → has_add nat := λ n, has_add.mk (λ x y : nat, x + y) open tactic set_option pp.all true -- Example where instance canonization does not work. -- This is a different issue. We can only make them work if we normalize (nat_has_add3 x) and (nat_has_add3 y). -- Again, the user can workaround it by manually normalizing these instances before invoking defeq_simp. example (a b : nat) (H : (λ x y : nat, @add nat (nat_has_add3 x) a b) = (λ x y : nat, @add nat (nat_has_add3 y) a x)) : true := by do s ← simp_lemmas.mk_default, get_local `H >>= infer_type >>= s^.dsimplify >>= trace, constructor
c3f5a87e3ddf7facaf11bbaaf2e90bd6f83fb205
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/elab_crash1.lean
defeadab6c4393e6ff4276fe0d5e8d90531c330d
[ "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
326
lean
open expr tactic meta definition to_expr_target (a : pexpr) : tactic expr := do tgt ← target, to_expr ```((%%a : %%tgt)) example (A : Type) (a : A) : A := by do to_expr_target ``(sorry) >>= exact example (A : Type) (a : A) : A := by do refine ``(sorry) example (a : nat) : nat := by do to_expr ``(nat.zero) >>= exact
b7e88182bf8f9f7fc6cf28bf3302d35b68cbb725
df561f413cfe0a88b1056655515399c546ff32a5
/3-multiplication-world/l5.lean
7df57462dc3dfbae0e08eeebac96100a3d441433
[]
no_license
nicholaspun/natural-number-game-solutions
31d5158415c6f582694680044c5c6469032c2a06
1e2aed86d2e76a3f4a275c6d99e795ad30cf6df0
refs/heads/main
1,675,123,625,012
1,607,633,548,000
1,607,633,548,000
318,933,860
3
1
null
null
null
null
UTF-8
Lean
false
false
168
lean
lemma mul_assoc (a b c : mynat) : (a * b) * c = a * (b * c) := begin induction c with k Pk, repeat { rw mul_zero }, repeat { rw mul_succ }, rw mul_add, rw Pk, refl, end
fc9971865a1722709071dd62b96a1568cdab9388
31f556cdeb9239ffc2fad8f905e33987ff4feab9
/stage0/src/Lean/Elab/Tactic/Basic.lean
369a902b94c010ff875d3bcacb511840c18f76a9
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
tobiasgrosser/lean4
ce0fd9cca0feba1100656679bf41f0bffdbabb71
ebdbdc10436a4d9d6b66acf78aae7a23f5bd073f
refs/heads/master
1,673,103,412,948
1,664,930,501,000
1,664,930,501,000
186,870,185
0
0
Apache-2.0
1,665,129,237,000
1,557,939,901,000
Lean
UTF-8
Lean
false
false
15,667
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, Sebastian Ullrich -/ import Lean.Elab.Term namespace Lean.Elab open Meta /-- Assign `mvarId := sorry` -/ def admitGoal (mvarId : MVarId) : MetaM Unit := mvarId.withContext do let mvarType ← inferType (mkMVar mvarId) mvarId.assign (← mkSorry mvarType (synthetic := true)) def goalsToMessageData (goals : List MVarId) : MessageData := MessageData.joinSep (goals.map MessageData.ofGoal) m!"\n\n" def Term.reportUnsolvedGoals (goals : List MVarId) : TermElabM Unit := withPPShowLetValues <| withPPInaccessibleNames do logError <| MessageData.tagged `Tactic.unsolvedGoals <| m!"unsolved goals\n{goalsToMessageData goals}" goals.forM fun mvarId => admitGoal mvarId namespace Tactic structure Context where /-- Declaration name of the executing elaborator, used by `mkTacticInfo` to persist it in the info tree -/ elaborator : Name /-- If `true`, enable "error recovery" in some tactics. For example, `cases` tactic admits unsolved alternatives when `recover == true`. The combinator `withoutRecover <tac>` disables "error recovery" while executing `<tac>`. This is useful for tactics such as `first | ... | ...`. -/ recover : Bool := true structure SavedState where term : Term.SavedState tactic : State abbrev TacticM := ReaderT Context $ StateRefT State TermElabM abbrev Tactic := Syntax → TacticM Unit -- Make the compiler generate specialized `pure`/`bind` so we do not have to optimize through the -- whole monad stack at every use site. May eventually be covered by `deriving`. instance : Monad TacticM := let i := inferInstanceAs (Monad TacticM); { pure := i.pure, bind := i.bind } def getGoals : TacticM (List MVarId) := return (← get).goals def setGoals (mvarIds : List MVarId) : TacticM Unit := modify fun _ => { goals := mvarIds } def pruneSolvedGoals : TacticM Unit := do let gs ← getGoals let gs ← gs.filterM fun g => not <$> g.isAssigned setGoals gs def getUnsolvedGoals : TacticM (List MVarId) := do pruneSolvedGoals getGoals @[inline] private def TacticM.runCore (x : TacticM α) (ctx : Context) (s : State) : TermElabM (α × State) := x ctx |>.run s @[inline] private def TacticM.runCore' (x : TacticM α) (ctx : Context) (s : State) : TermElabM α := Prod.fst <$> x.runCore ctx s def run (mvarId : MVarId) (x : TacticM Unit) : TermElabM (List MVarId) := mvarId.withContext do let pendingMVarsSaved := (← get).pendingMVars modify fun s => { s with pendingMVars := [] } let aux : TacticM (List MVarId) := /- Important: the following `try` does not backtrack the state. This is intentional because we don't want to backtrack the error messages when we catch the "abort internal exception" We must define `run` here because we define `MonadExcept` instance for `TacticM` -/ try x; getUnsolvedGoals catch ex => if isAbortTacticException ex then getUnsolvedGoals else throw ex try aux.runCore' { elaborator := .anonymous } { goals := [mvarId] } finally modify fun s => { s with pendingMVars := pendingMVarsSaved } protected def saveState : TacticM SavedState := return { term := (← Term.saveState), tactic := (← get) } def SavedState.restore (b : SavedState) (restoreInfo := false) : TacticM Unit := do b.term.restore restoreInfo set b.tactic protected def getCurrMacroScope : TacticM MacroScope := do pure (← readThe Core.Context).currMacroScope protected def getMainModule : TacticM Name := do pure (← getEnv).mainModule unsafe def mkTacticAttribute : IO (KeyedDeclsAttribute Tactic) := mkElabAttribute Tactic `Lean.Elab.Tactic.tacticElabAttribute `builtinTactic `tactic `Lean.Parser.Tactic `Lean.Elab.Tactic.Tactic "tactic" @[builtinInit mkTacticAttribute] opaque tacticElabAttribute : KeyedDeclsAttribute Tactic def mkTacticInfo (mctxBefore : MetavarContext) (goalsBefore : List MVarId) (stx : Syntax) : TacticM Info := return Info.ofTacticInfo { elaborator := (← read).elaborator mctxBefore := mctxBefore goalsBefore := goalsBefore stx := stx mctxAfter := (← getMCtx) goalsAfter := (← getUnsolvedGoals) } def mkInitialTacticInfo (stx : Syntax) : TacticM (TacticM Info) := do let mctxBefore ← getMCtx let goalsBefore ← getUnsolvedGoals return mkTacticInfo mctxBefore goalsBefore stx @[inline] def withTacticInfoContext (stx : Syntax) (x : TacticM α) : TacticM α := do withInfoContext x (← mkInitialTacticInfo stx) /-! Important: we must define `evalTactic` before we define the instance `MonadExcept` for `TacticM` since it backtracks the state including error messages, and this is bad when rethrowing the exception at the `catch` block in these methods. We marked these places with a `(*)` in these methods. -/ /-- Auxiliary datastructure for capturing exceptions at `evalTactic`. -/ structure EvalTacticFailure where exception : Exception state : SavedState partial def evalTactic (stx : Syntax) : TacticM Unit := withRef stx <| withIncRecDepth <| withFreshMacroScope <| match stx with | .node _ k _ => if k == nullKind then -- Macro writers create a sequence of tactics `t₁ ... tₙ` using `mkNullNode #[t₁, ..., tₙ]` stx.getArgs.forM evalTactic else do trace[Elab.step] "{stx}" let evalFns := tacticElabAttribute.getEntries (← getEnv) stx.getKind let macros := macroAttribute.getEntries (← getEnv) stx.getKind if evalFns.isEmpty && macros.isEmpty then throwErrorAt stx "tactic '{stx.getKind}' has not been implemented" let s ← Tactic.saveState expandEval s macros evalFns #[] | .missing => pure () | _ => throwError m!"unexpected tactic{indentD stx}" where throwExs (failures : Array EvalTacticFailure) : TacticM Unit := do if let some fail := failures[0]? then -- Recall that `failures[0]` is the highest priority evalFn/macro fail.state.restore (restoreInfo := true) throw fail.exception -- (*) else throwErrorAt stx "unexpected syntax {indentD stx}" @[inline] handleEx (s : SavedState) (failures : Array EvalTacticFailure) (ex : Exception) (k : Array EvalTacticFailure → TacticM Unit) := do match ex with | .error .. => trace[Elab.tactic.backtrack] ex.toMessageData let failures := failures.push ⟨ex, ← Tactic.saveState⟩ s.restore (restoreInfo := true); k failures | .internal id _ => if id == unsupportedSyntaxExceptionId then -- We do not store `unsupportedSyntaxExceptionId`, see throwExs s.restore (restoreInfo := true); k failures else if id == abortTacticExceptionId then for msg in (← Core.getMessageLog).toList do trace[Elab.tactic.backtrack] msg.data let failures := failures.push ⟨ex, ← Tactic.saveState⟩ s.restore (restoreInfo := true); k failures else throw ex -- (*) expandEval (s : SavedState) (macros : List _) (evalFns : List _) (failures : Array EvalTacticFailure) : TacticM Unit := match macros with | [] => eval s evalFns failures | m :: ms => try withReader ({ · with elaborator := m.declName }) do withTacticInfoContext stx do let stx' ← adaptMacro m.value stx evalTactic stx' catch ex => handleEx s failures ex (expandEval s ms evalFns) eval (s : SavedState) (evalFns : List _) (failures : Array EvalTacticFailure) : TacticM Unit := do match evalFns with | [] => throwExs failures | evalFn::evalFns => do try withReader ({ · with elaborator := evalFn.declName }) <| withTacticInfoContext stx <| evalFn.value stx catch ex => handleEx s failures ex (eval s evalFns) def throwNoGoalsToBeSolved : TacticM α := throwError "no goals to be solved" def done : TacticM Unit := do let gs ← getUnsolvedGoals unless gs.isEmpty do Term.reportUnsolvedGoals gs throwAbortTactic def focus (x : TacticM α) : TacticM α := do let mvarId :: mvarIds ← getUnsolvedGoals | throwNoGoalsToBeSolved setGoals [mvarId] let a ← x let mvarIds' ← getUnsolvedGoals setGoals (mvarIds' ++ mvarIds) pure a def focusAndDone (tactic : TacticM α) : TacticM α := focus do let a ← tactic done pure a /-- Close the main goal using the given tactic. If it fails, log the error and `admit` -/ def closeUsingOrAdmit (tac : TacticM Unit) : TacticM Unit := do /- Important: we must define `closeUsingOrAdmit` before we define the instance `MonadExcept` for `TacticM` since it backtracks the state including error messages. -/ let mvarId :: mvarIds ← getUnsolvedGoals | throwNoGoalsToBeSolved try focusAndDone tac catch ex => if (← read).recover then logException ex admitGoal mvarId setGoals mvarIds else throw ex instance : MonadBacktrack SavedState TacticM where saveState := Tactic.saveState restoreState b := b.restore @[inline] protected def tryCatch {α} (x : TacticM α) (h : Exception → TacticM α) : TacticM α := do let b ← saveState try x catch ex => b.restore; h ex instance : MonadExcept Exception TacticM where throw := throw tryCatch := Tactic.tryCatch /-- Execute `x` with error recovery disabled -/ def withoutRecover (x : TacticM α) : TacticM α := withReader (fun ctx => { ctx with recover := false }) x @[inline] protected def orElse (x : TacticM α) (y : Unit → TacticM α) : TacticM α := do try withoutRecover x catch _ => y () instance : OrElse (TacticM α) where orElse := Tactic.orElse instance : Alternative TacticM where failure := fun {_} => throwError "failed" orElse := Tactic.orElse /-- Save the current tactic state for a token `stx`. This method is a no-op if `stx` has no position information. We use this method to save the tactic state at punctuation such as `;` -/ def saveTacticInfoForToken (stx : Syntax) : TacticM Unit := do unless stx.getPos?.isNone do withTacticInfoContext stx (pure ()) /-- Elaborate `x` with `stx` on the macro stack -/ @[inline] def withMacroExpansion (beforeStx afterStx : Syntax) (x : TacticM α) : TacticM α := withMacroExpansionInfo beforeStx afterStx do withTheReader Term.Context (fun ctx => { ctx with macroStack := { before := beforeStx, after := afterStx } :: ctx.macroStack }) x /-- Adapt a syntax transformation to a regular tactic evaluator. -/ def adaptExpander (exp : Syntax → TacticM Syntax) : Tactic := fun stx => do let stx' ← exp stx withMacroExpansion stx stx' $ evalTactic stx' /-- Add the given goals at the end of the current goals collection. -/ def appendGoals (mvarIds : List MVarId) : TacticM Unit := modify fun s => { s with goals := s.goals ++ mvarIds } /-- Discard the first goal and replace it by the given list of goals, keeping the other goals. -/ def replaceMainGoal (mvarIds : List MVarId) : TacticM Unit := do let (_ :: mvarIds') ← getGoals | throwNoGoalsToBeSolved modify fun _ => { goals := mvarIds ++ mvarIds' } /-- Return the first goal. -/ def getMainGoal : TacticM MVarId := do loop (← getGoals) where loop : List MVarId → TacticM MVarId | [] => throwNoGoalsToBeSolved | mvarId :: mvarIds => do if (← mvarId.isAssigned) then loop mvarIds else setGoals (mvarId :: mvarIds) return mvarId /-- Return the main goal metavariable declaration. -/ def getMainDecl : TacticM MetavarDecl := do (← getMainGoal).getDecl /-- Return the main goal tag. -/ def getMainTag : TacticM Name := return (← getMainDecl).userName /-- Return expected type for the main goal. -/ def getMainTarget : TacticM Expr := do instantiateMVars (← getMainDecl).type /-- Execute `x` using the main goal local context and instances -/ def withMainContext (x : TacticM α) : TacticM α := do (← getMainGoal).withContext x /-- Evaluate `tac` at `mvarId`, and return the list of resulting subgoals. -/ def evalTacticAt (tac : Syntax) (mvarId : MVarId) : TacticM (List MVarId) := do let gs ← getGoals try setGoals [mvarId] evalTactic tac pruneSolvedGoals getGoals finally setGoals gs def ensureHasNoMVars (e : Expr) : TacticM Unit := do let e ← instantiateMVars e let pendingMVars ← getMVars e discard <| Term.logUnassignedUsingErrorInfos pendingMVars if e.hasExprMVar then throwError "tactic failed, resulting expression contains metavariables{indentExpr e}" /-- Close main goal using the given expression. If `checkUnassigned == true`, then `val` must not contain unassinged metavariables. -/ def closeMainGoal (val : Expr) (checkUnassigned := true): TacticM Unit := do if checkUnassigned then ensureHasNoMVars val (← getMainGoal).assign val replaceMainGoal [] @[inline] def liftMetaMAtMain (x : MVarId → MetaM α) : TacticM α := do withMainContext do x (← getMainGoal) @[inline] def liftMetaTacticAux (tac : MVarId → MetaM (α × List MVarId)) : TacticM α := do withMainContext do let (a, mvarIds) ← tac (← getMainGoal) replaceMainGoal mvarIds pure a /-- Get the mvarid of the main goal, run the given `tactic`, then set the new goals to be the resulting goal list.-/ @[inline] def liftMetaTactic (tactic : MVarId → MetaM (List MVarId)) : TacticM Unit := liftMetaTacticAux fun mvarId => do let gs ← tactic mvarId pure ((), gs) @[inline] def liftMetaTactic1 (tactic : MVarId → MetaM (Option MVarId)) : TacticM Unit := withMainContext do if let some mvarId ← tactic (← getMainGoal) then replaceMainGoal [mvarId] else replaceMainGoal [] def tryTactic? (tactic : TacticM α) : TacticM (Option α) := do try pure (some (← tactic)) catch _ => pure none def tryTactic (tactic : TacticM α) : TacticM Bool := do try discard tactic pure true catch _ => pure false /-- Use `parentTag` to tag untagged goals at `newGoals`. If there are multiple new untagged goals, they are named using `<parentTag>.<newSuffix>_<idx>` where `idx > 0`. If there is only one new untagged goal, then we just use `parentTag` -/ def tagUntaggedGoals (parentTag : Name) (newSuffix : Name) (newGoals : List MVarId) : TacticM Unit := do let mctx ← getMCtx let mut numAnonymous := 0 for g in newGoals do if mctx.isAnonymousMVar g then numAnonymous := numAnonymous + 1 modifyMCtx fun mctx => Id.run do let mut mctx := mctx let mut idx := 1 for g in newGoals do if mctx.isAnonymousMVar g then if numAnonymous == 1 then mctx := mctx.setMVarUserName g parentTag else mctx := mctx.setMVarUserName g (parentTag ++ newSuffix.appendIndexAfter idx) idx := idx + 1 pure mctx /- Recall that `ident' := ident <|> Term.hole` -/ def getNameOfIdent' (id : Syntax) : Name := if id.isIdent then id.getId else `_ /-- Use position of `=> $body` for error messages. If there is a line break before `body`, the message will be displayed on `=>` only, but the "full range" for the info view will still include `body`. -/ def withCaseRef [Monad m] [MonadRef m] (arrow body : Syntax) (x : m α) : m α := withRef (mkNullNode #[arrow, body]) x builtin_initialize registerTraceClass `Elab.tactic builtin_initialize registerTraceClass `Elab.tactic.backtrack end Lean.Elab.Tactic
1138921b1f33bb39cc122775d48a9e1fabe88ddc
200b12985a863d01fbbde6abfc9326bb82424a8b
/src/propLogic/Ex001.lean
d7c5848eb8bd8f325170aafd61d5c90eafeab576
[]
no_license
SvenWille/LeanLogicExercises
38eacd36733ac48e5a7aacf863c681c9a9a48271
2dbc920feadd63bbc50f87e69646c0081db26eba
refs/heads/master
1,629,676,667,365
1,512,161,459,000
1,512,161,459,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
181
lean
--Proof: a -> a theorem Ex001_1(a : Prop) : a -> a := assume H1 : a, show a ,from H1 --using tactics theorem Ex001_2(a : Prop) : a -> a := begin intro H, exact H end
b9da93a75d69f4540bdce59193669b18c8e9b997
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/Lean/Compiler/LCNF/Simp/InlineProj.lean
a771cf7fb546b64a787ec470011641986059650b
[ "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
3,677
lean
/- Copyright (c) 2022 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Compiler.LCNF.Simp.SimpM namespace Lean.Compiler.LCNF namespace Simp /-- Auxiliary function for projecting "type class dictionary access". That is, we are trying to extract one of the type class instance elements. Remark: We do not consider parent instances to be elements. For example, suppose `e` is `_x_4.1`, and we have ``` _x_2 : Monad (ReaderT Bool (ExceptT String Id)) := @ReaderT.Monad Bool (ExceptT String Id) _x_1 _x_3 : Applicative (ReaderT Bool (ExceptT String Id)) := _x_2.1 _x_4 : Functor (ReaderT Bool (ExceptT String Id)) := _x_3.1 ``` Then, we will expand `_x_4.1` since it corresponds to the `Functor` `map` element, and its type is not a type class, but is of the form ``` {α β : Type u} → (α → β) → ... ``` In the example above, the compiler should not expand `_x_3.1` or `_x_2.1` because they are type class applications: `Functor` and `Applicative` respectively. By eagerly expanding them, we may produce inefficient and bloated code. For example, we may be using `_x_3.1` to invoke a function that expects a `Functor` instance. By expanding `_x_3.1` we will be just expanding the code that creates this instance. The result is representing a sequence of code containing let-declarations and local function declarations (`Array CodeDecl`) and the free variable containing the result (`FVarId`). The resulting `FVarId` often depends only on a small subset of `Array CodeDecl`. However, this method does try to filter the relevant ones. We rely on the `used` var set available in `SimpM` to filter them. See `attachCodeDecls`. -/ partial def inlineProjInst? (e : LetValue) : SimpM (Option (Array CodeDecl × FVarId)) := do let .proj _ i s := e | return none let sType ← getType s unless (← isClass? sType).isSome do return none let eType ← e.inferType unless (← isClass? eType).isNone do return none let (fvarId?, decls) ← visit s [i] |>.run |>.run #[] if let some fvarId := fvarId? then return some (decls, fvarId) else eraseCodeDecls decls return none where visit (fvarId : FVarId) (projs : List Nat) : OptionT (StateRefT (Array CodeDecl) SimpM) FVarId := do let some letDecl ← findLetDecl? fvarId | failure match letDecl.value with | .proj _ i s => visit s (i :: projs) | .fvar .. | .value .. | .erased => failure | .const declName us args => if let some (.ctorInfo ctorVal) := (← getEnv).find? declName then let i :: projs := projs | unreachable! let arg := args[ctorVal.numParams + i]! let fvarId ← match arg with | .fvar fvarId => pure fvarId | .erased | .type .. => let auxDecl ← mkLetDeclErased modify (·.push (.let auxDecl)) pure auxDecl.fvarId if projs.isEmpty then return fvarId else visit fvarId projs else let some decl ← getDecl? declName | failure guard (decl.getArity == args.size) let params := decl.instantiateParamsLevelParams us let code := decl.instantiateValueLevelParams us let code ← betaReduce params code args (mustInline := true) visitCode code projs visitCode (code : Code) (projs : List Nat) : OptionT (StateRefT (Array CodeDecl) SimpM) FVarId := do match code with | .let decl k => modify (·.push (.let decl)); visitCode k projs | .fun decl k => modify (·.push (.fun decl)); visitCode k projs | .return fvarId => visit fvarId projs | _ => eraseCode code; failure
2c55795ffac5f7836deb2d4900a983833e8b38f7
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/algebra/monoid_algebra/support.lean
cb6909ab073169fc2cb5e0bc98c907e446a421d1
[ "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
5,969
lean
/- Copyright (c) 2022 Damiano Testa. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Damiano Testa -/ import algebra.monoid_algebra.basic /-! # Lemmas about the support of a finitely supported function -/ universes u₁ u₂ u₃ namespace monoid_algebra open finset finsupp variables {k : Type u₁} {G : Type u₂} [semiring k] lemma support_single_mul_subset [decidable_eq G] [has_mul G] (f : monoid_algebra k G) (r : k) (a : G) : (single a r * f : monoid_algebra k G).support ⊆ finset.image ((*) a) f.support := begin intros x hx, contrapose hx, have : ∀ y, a * y = x → f y = 0, { simpa only [not_and', mem_image, mem_support_iff, exists_prop, not_exists, not_not] using hx }, simp only [mem_support_iff, mul_apply, sum_single_index, zero_mul, if_t_t, sum_zero, not_not], exact finset.sum_eq_zero (by simp only [this, mem_support_iff, mul_zero, ne.def, ite_eq_right_iff, eq_self_iff_true, implies_true_iff] {contextual := tt}), end lemma support_mul_single_subset [decidable_eq G] [has_mul G] (f : monoid_algebra k G) (r : k) (a : G) : (f * single a r).support ⊆ finset.image (* a) f.support := begin intros x hx, contrapose hx, have : ∀ y, y * a = x → f y = 0, { simpa only [not_and', mem_image, mem_support_iff, exists_prop, not_exists, not_not] using hx }, simp only [mem_support_iff, mul_apply, sum_single_index, zero_mul, if_t_t, sum_zero, not_not], exact finset.sum_eq_zero (by simp only [this, sum_single_index, ite_eq_right_iff, eq_self_iff_true, implies_true_iff, zero_mul] {contextual := tt}), end lemma support_single_mul_eq_image [decidable_eq G] [has_mul G] (f : monoid_algebra k G) {r : k} (hr : ∀ y, r * y = 0 ↔ y = 0) {x : G} (lx : is_left_regular x) : (single x r * f : monoid_algebra k G).support = finset.image ((*) x) f.support := begin refine subset_antisymm (support_single_mul_subset f _ _) (λ y hy, _), obtain ⟨y, yf, rfl⟩ : ∃ (a : G), a ∈ f.support ∧ x * a = y, { simpa only [finset.mem_image, exists_prop] using hy }, simp only [mul_apply, mem_support_iff.mp yf, hr, mem_support_iff, sum_single_index, finsupp.sum_ite_eq', ne.def, not_false_iff, if_true, zero_mul, if_t_t, sum_zero, lx.eq_iff] end lemma support_mul_single_eq_image [decidable_eq G] [has_mul G] (f : monoid_algebra k G) {r : k} (hr : ∀ y, y * r = 0 ↔ y = 0) {x : G} (rx : is_right_regular x) : (f * single x r).support = finset.image (* x) f.support := begin refine subset_antisymm (support_mul_single_subset f _ _) (λ y hy, _), obtain ⟨y, yf, rfl⟩ : ∃ (a : G), a ∈ f.support ∧ a * x = y, { simpa only [finset.mem_image, exists_prop] using hy }, simp only [mul_apply, mem_support_iff.mp yf, hr, mem_support_iff, sum_single_index, finsupp.sum_ite_eq', ne.def, not_false_iff, if_true, mul_zero, if_t_t, sum_zero, rx.eq_iff] end lemma support_mul [has_mul G] [decidable_eq G] (a b : monoid_algebra k G) : (a * b).support ⊆ a.support.bUnion (λa₁, b.support.bUnion $ λa₂, {a₁ * a₂}) := subset.trans support_sum $ bUnion_mono $ assume a₁ _, subset.trans support_sum $ bUnion_mono $ assume a₂ _, support_single_subset lemma support_mul_single [right_cancel_semigroup G] (f : monoid_algebra k G) (r : k) (hr : ∀ y, y * r = 0 ↔ y = 0) (x : G) : (f * single x r).support = f.support.map (mul_right_embedding x) := begin classical, ext, simp only [support_mul_single_eq_image f hr (is_right_regular_of_right_cancel_semigroup x), mem_image, mem_map, mul_right_embedding_apply], end lemma support_single_mul [left_cancel_semigroup G] (f : monoid_algebra k G) (r : k) (hr : ∀ y, r * y = 0 ↔ y = 0) (x : G) : (single x r * f : monoid_algebra k G).support = f.support.map (mul_left_embedding x) := begin classical, ext, simp only [support_single_mul_eq_image f hr (is_left_regular_of_left_cancel_semigroup x), mem_image, mem_map, mul_left_embedding_apply], end section span variables [mul_one_class G] /-- An element of `monoid_algebra k G` is in the subalgebra generated by its support. -/ lemma mem_span_support (f : monoid_algebra k G) : f ∈ submodule.span k (of k G '' (f.support : set G)) := by rw [of, monoid_hom.coe_mk, ← finsupp.supported_eq_span_single, finsupp.mem_supported] end span end monoid_algebra namespace add_monoid_algebra open finset finsupp mul_opposite variables {k : Type u₁} {G : Type u₂} [semiring k] lemma support_mul [decidable_eq G] [has_add G] (a b : add_monoid_algebra k G) : (a * b).support ⊆ a.support.bUnion (λa₁, b.support.bUnion $ λa₂, {a₁ + a₂}) := @monoid_algebra.support_mul k (multiplicative G) _ _ _ _ _ lemma support_mul_single [add_right_cancel_semigroup G] (f : add_monoid_algebra k G) (r : k) (hr : ∀ y, y * r = 0 ↔ y = 0) (x : G) : (f * single x r : add_monoid_algebra k G).support = f.support.map (add_right_embedding x) := @monoid_algebra.support_mul_single k (multiplicative G) _ _ _ _ hr _ lemma support_single_mul [add_left_cancel_semigroup G] (f : add_monoid_algebra k G) (r : k) (hr : ∀ y, r * y = 0 ↔ y = 0) (x : G) : (single x r * f : add_monoid_algebra k G).support = f.support.map (add_left_embedding x) := @monoid_algebra.support_single_mul k (multiplicative G) _ _ _ _ hr _ section span /-- An element of `add_monoid_algebra k G` is in the submodule generated by its support. -/ lemma mem_span_support [add_zero_class G] (f : add_monoid_algebra k G) : f ∈ submodule.span k (of k G '' (f.support : set G)) := by rw [of, monoid_hom.coe_mk, ← finsupp.supported_eq_span_single, finsupp.mem_supported] /-- An element of `add_monoid_algebra k G` is in the subalgebra generated by its support, using unbundled inclusion. -/ lemma mem_span_support' (f : add_monoid_algebra k G) : f ∈ submodule.span k (of' k G '' (f.support : set G)) := by rw [of', ← finsupp.supported_eq_span_single, finsupp.mem_supported] end span end add_monoid_algebra
f4e5c4d38f415726da1e6a3874f2d78e3e413741
a339bc2ac96174381fb610f4b2e1ba42df2be819
/hott/algebra/trunc_group.hlean
cdd85090a3953989c645f9867c04b9d81599c28a
[ "Apache-2.0" ]
permissive
kalfsvag/lean2
25b2dccc07a98e5aa20f9a11229831f9d3edf2e7
4d4a0c7c53a9922c5f630f6f8ebdccf7ddef2cc7
refs/heads/master
1,610,513,122,164
1,483,135,198,000
1,483,135,198,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,727
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 truncating an ∞-group to a group -/ import hit.trunc algebra.group open eq is_trunc trunc namespace algebra section parameters (n : trunc_index) {A : Type} (mul : A → A → A) (inv : A → A) (one : A) (mul_assoc : ∀a b c, mul (mul a b) c = mul a (mul b c)) (one_mul : ∀a, mul one a = a) (mul_one : ∀a, mul a one = a) (mul_left_inv : ∀a, mul (inv a) a = one) local abbreviation G := trunc n A include mul definition trunc_mul [unfold 9 10] (g h : G) : G := begin induction g with p, induction h with q, exact tr (mul p q) end omit mul include inv definition trunc_inv [unfold 9] (g : G) : G := begin induction g with p, exact tr (inv p) end omit inv include one definition trunc_one [constructor] : G := tr one local notation 1 := trunc_one local postfix ⁻¹ := trunc_inv local infix * := trunc_mul parameters {mul} {inv} {one} omit one include mul_assoc theorem trunc_mul_assoc (g₁ g₂ g₃ : G) : g₁ * g₂ * g₃ = g₁ * (g₂ * g₃) := begin induction g₁ with p₁, induction g₂ with p₂, induction g₃ with p₃, exact ap tr !mul_assoc, end omit mul_assoc include one_mul theorem trunc_one_mul (g : G) : 1 * g = g := begin induction g with p, exact ap tr !one_mul end omit one_mul include mul_one theorem trunc_mul_one (g : G) : g * 1 = g := begin induction g with p, exact ap tr !mul_one end omit mul_one include mul_left_inv theorem trunc_mul_left_inv (g : G) : g⁻¹ * g = 1 := begin induction g with p, exact ap tr !mul_left_inv end omit mul_left_inv theorem trunc_mul_comm (mul_comm : ∀a b, mul a b = mul b a) (g h : G) : g * h = h * g := begin induction g with p, induction h with q, exact ap tr !mul_comm end parameters (mul) (inv) (one) definition trunc_group [constructor] : group (trunc 0 A) := ⦃group, mul := algebra.trunc_mul 0 mul, mul_assoc := algebra.trunc_mul_assoc 0 mul_assoc, one := algebra.trunc_one 0 one, one_mul := algebra.trunc_one_mul 0 one_mul, mul_one := algebra.trunc_mul_one 0 mul_one, inv := algebra.trunc_inv 0 inv, mul_left_inv := algebra.trunc_mul_left_inv 0 mul_left_inv, is_set_carrier := _⦄ definition trunc_ab_group [constructor] (mul_comm : ∀a b, mul a b = mul b a) : ab_group (trunc 0 A) := ⦃ab_group, trunc_group, mul_comm := algebra.trunc_mul_comm 0 mul_comm⦄ end end algebra
79797609f8c277762ed73ae2371f1105892b2d01
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/449.lean
04e6b81f2b048bf0ee560529fd5373479f0588f4
[ "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
273
lean
open Nat theorem mul_comm (m n : Nat) : m * n = n * m := by induction n with | zero => simp | succ n ih => have foo : m * n + m = m * n + (succ zero) * m := _ rfl theorem test (o : x ∨ y) : x := by cases o with | inl h => exact h | inr h => exact _
bdd7e461657d5149cf3d956a347de8636dc8da8d
9dc8cecdf3c4634764a18254e94d43da07142918
/src/linear_algebra/span.lean
463c5e021dec63d231234155a266a769c0c2f0d5
[ "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
35,542
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Kevin Buzzard, Yury Kudryashov, Frédéric Dupuis, Heather Macbeth -/ import linear_algebra.basic import order.omega_complete_partial_order /-! # The span of a set of vectors, as a submodule * `submodule.span s` is defined to be the smallest submodule containing the set `s`. ## Notations * We introduce the notation `R ∙ v` for the span of a singleton, `submodule.span R {v}`. This is `\.`, not the same as the scalar multiplication `•`/`\bub`. -/ variables {R R₂ K M M₂ V S : Type*} namespace submodule open function set open_locale pointwise section add_comm_monoid variables [semiring R] [add_comm_monoid M] [module R M] variables {x : M} (p p' : submodule R M) variables [semiring R₂] {σ₁₂ : R →+* R₂} variables [add_comm_monoid M₂] [module R₂ M₂] section variables (R) /-- The span of a set `s ⊆ M` is the smallest submodule of M that contains `s`. -/ def span (s : set M) : submodule R M := Inf {p | s ⊆ p} end variables {s t : set M} lemma mem_span : x ∈ span R s ↔ ∀ p : submodule R M, s ⊆ p → x ∈ p := mem_Inter₂ lemma subset_span : s ⊆ span R s := λ x h, mem_span.2 $ λ p hp, hp h lemma span_le {p} : span R s ≤ p ↔ s ⊆ p := ⟨subset.trans subset_span, λ ss x h, mem_span.1 h _ ss⟩ lemma span_mono (h : s ⊆ t) : span R s ≤ span R t := span_le.2 $ subset.trans h subset_span lemma span_monotone : monotone (span R : set M → submodule R M) := λ _ _, span_mono lemma span_eq_of_le (h₁ : s ⊆ p) (h₂ : p ≤ span R s) : span R s = p := le_antisymm (span_le.2 h₁) h₂ lemma span_eq : span R (p : set M) = p := span_eq_of_le _ (subset.refl _) subset_span lemma span_eq_span (hs : s ⊆ span R t) (ht : t ⊆ span R s) : span R s = span R t := le_antisymm (span_le.2 hs) (span_le.2 ht) /-- A version of `submodule.span_eq` for when the span is by a smaller ring. -/ @[simp] lemma span_coe_eq_restrict_scalars [semiring S] [has_smul S R] [module S M] [is_scalar_tower S R M] : span S (p : set M) = p.restrict_scalars S := span_eq (p.restrict_scalars S) lemma map_span [ring_hom_surjective σ₁₂] (f : M →ₛₗ[σ₁₂] M₂) (s : set M) : (span R s).map f = span R₂ (f '' s) := eq.symm $ span_eq_of_le _ (set.image_subset f subset_span) $ map_le_iff_le_comap.2 $ span_le.2 $ λ x hx, subset_span ⟨x, hx, rfl⟩ alias submodule.map_span ← _root_.linear_map.map_span lemma map_span_le [ring_hom_surjective σ₁₂] (f : M →ₛₗ[σ₁₂] M₂) (s : set M) (N : submodule R₂ M₂) : map f (span R s) ≤ N ↔ ∀ m ∈ s, f m ∈ N := begin rw [f.map_span, span_le, set.image_subset_iff], exact iff.rfl end alias submodule.map_span_le ← _root_.linear_map.map_span_le @[simp] lemma span_insert_zero : span R (insert (0 : M) s) = span R s := begin refine le_antisymm _ (submodule.span_mono (set.subset_insert 0 s)), rw [span_le, set.insert_subset], exact ⟨by simp only [set_like.mem_coe, submodule.zero_mem], submodule.subset_span⟩, end /- See also `span_preimage_eq` below. -/ lemma span_preimage_le (f : M →ₛₗ[σ₁₂] M₂) (s : set M₂) : span R (f ⁻¹' s) ≤ (span R₂ s).comap f := by { rw [span_le, comap_coe], exact preimage_mono (subset_span), } alias submodule.span_preimage_le ← _root_.linear_map.span_preimage_le lemma closure_subset_span {s : set M} : (add_submonoid.closure s : set M) ⊆ span R s := (@add_submonoid.closure_le _ _ _ (span R s).to_add_submonoid).mpr subset_span lemma closure_le_to_add_submonoid_span {s : set M} : add_submonoid.closure s ≤ (span R s).to_add_submonoid := closure_subset_span @[simp] lemma span_closure {s : set M} : span R (add_submonoid.closure s : set M) = span R s := le_antisymm (span_le.mpr closure_subset_span) (span_mono add_submonoid.subset_closure) /-- An induction principle for span membership. If `p` holds for 0 and all elements of `s`, and is preserved under addition and scalar multiplication, then `p` holds for all elements of the span of `s`. -/ @[elab_as_eliminator] lemma span_induction {p : M → Prop} (h : x ∈ span R s) (Hs : ∀ x ∈ s, p x) (H0 : p 0) (H1 : ∀ x y, p x → p y → p (x + y)) (H2 : ∀ (a:R) x, p x → p (a • x)) : p x := (@span_le _ _ _ _ _ _ ⟨p, H1, H0, H2⟩).2 Hs h /-- A dependent version of `submodule.span_induction`. -/ lemma span_induction' {p : Π x, x ∈ span R s → Prop} (Hs : ∀ x (h : x ∈ s), p x (subset_span h)) (H0 : p 0 (submodule.zero_mem _)) (H1 : ∀ x hx y hy, p x hx → p y hy → p (x + y) (submodule.add_mem _ ‹_› ‹_›)) (H2 : ∀ (a : R) x hx, p x hx → p (a • x) (submodule.smul_mem _ _ ‹_›)) {x} (hx : x ∈ span R s) : p x hx := begin refine exists.elim _ (λ (hx : x ∈ span R s) (hc : p x hx), hc), refine span_induction hx (λ m hm, ⟨subset_span hm, Hs m hm⟩) ⟨zero_mem _, H0⟩ (λ x y hx hy, exists.elim hx $ λ hx' hx, exists.elim hy $ λ hy' hy, ⟨add_mem hx' hy', H1 _ _ _ _ hx hy⟩) (λ r x hx, exists.elim hx $ λ hx' hx, ⟨smul_mem _ _ hx', H2 r _ _ hx⟩) end @[simp] lemma span_span_coe_preimage : span R ((coe : span R s → M) ⁻¹' s) = ⊤ := eq_top_iff.2 $ λ x, subtype.rec_on x $ λ x hx _, begin refine span_induction' (λ x hx, _) _ (λ x y _ _, _) (λ r x _, _) hx, { exact subset_span hx }, { exact zero_mem _ }, { exact add_mem }, { exact smul_mem _ _ } end lemma span_nat_eq_add_submonoid_closure (s : set M) : (span ℕ s).to_add_submonoid = add_submonoid.closure s := begin refine eq.symm (add_submonoid.closure_eq_of_le subset_span _), apply add_submonoid.to_nat_submodule.symm.to_galois_connection.l_le _, rw span_le, exact add_submonoid.subset_closure, end @[simp] lemma span_nat_eq (s : add_submonoid M) : (span ℕ (s : set M)).to_add_submonoid = s := by rw [span_nat_eq_add_submonoid_closure, s.closure_eq] lemma span_int_eq_add_subgroup_closure {M : Type*} [add_comm_group M] (s : set M) : (span ℤ s).to_add_subgroup = add_subgroup.closure s := eq.symm $ add_subgroup.closure_eq_of_le _ subset_span $ λ x hx, span_induction hx (λ x hx, add_subgroup.subset_closure hx) (add_subgroup.zero_mem _) (λ _ _, add_subgroup.add_mem _) (λ _ _ _, add_subgroup.zsmul_mem _ ‹_› _) @[simp] lemma span_int_eq {M : Type*} [add_comm_group M] (s : add_subgroup M) : (span ℤ (s : set M)).to_add_subgroup = s := by rw [span_int_eq_add_subgroup_closure, s.closure_eq] section variables (R M) /-- `span` forms a Galois insertion with the coercion from submodule to set. -/ protected def gi : galois_insertion (@span R M _ _ _) coe := { choice := λ s _, span R s, gc := λ s t, span_le, le_l_u := λ s, subset_span, choice_eq := λ s h, rfl } end @[simp] lemma span_empty : span R (∅ : set M) = ⊥ := (submodule.gi R M).gc.l_bot @[simp] lemma span_univ : span R (univ : set M) = ⊤ := eq_top_iff.2 $ set_like.le_def.2 $ subset_span lemma span_union (s t : set M) : span R (s ∪ t) = span R s ⊔ span R t := (submodule.gi R M).gc.l_sup lemma span_Union {ι} (s : ι → set M) : span R (⋃ i, s i) = ⨆ i, span R (s i) := (submodule.gi R M).gc.l_supr lemma span_Union₂ {ι} {κ : ι → Sort*} (s : Π i, κ i → set M) : span R (⋃ i j, s i j) = ⨆ i j, span R (s i j) := (submodule.gi R M).gc.l_supr₂ lemma span_attach_bUnion [decidable_eq M] {α : Type*} (s : finset α) (f : s → finset M) : span R (s.attach.bUnion f : set M) = ⨆ x, span R (f x) := by simpa [span_Union] lemma sup_span : p ⊔ span R s = span R (p ∪ s) := by rw [submodule.span_union, p.span_eq] lemma span_sup : span R s ⊔ p = span R (s ∪ p) := by rw [submodule.span_union, p.span_eq] /- Note that the character `∙` U+2219 used below is different from the scalar multiplication character `•` U+2022 and the matrix multiplication character `⬝` U+2B1D. -/ notation R` ∙ `:1000 x := span R (@singleton _ _ set.has_singleton x) lemma span_eq_supr_of_singleton_spans (s : set M) : span R s = ⨆ x ∈ s, R ∙ x := by simp only [←span_Union, set.bUnion_of_singleton s] lemma span_range_eq_supr {ι : Type*} {v : ι → M} : span R (range v) = ⨆ i, R ∙ v i := by rw [span_eq_supr_of_singleton_spans, supr_range] lemma span_smul_le (s : set M) (r : R) : span R (r • s) ≤ span R s := begin rw span_le, rintros _ ⟨x, hx, rfl⟩, exact smul_mem (span R s) r (subset_span hx), end lemma subset_span_trans {U V W : set M} (hUV : U ⊆ submodule.span R V) (hVW : V ⊆ submodule.span R W) : U ⊆ submodule.span R W := (submodule.gi R M).gc.le_u_l_trans hUV hVW /-- See `submodule.span_smul_eq` (in `ring_theory.ideal.operations`) for `span R (r • s) = r • span R s` that holds for arbitrary `r` in a `comm_semiring`. -/ lemma span_smul_eq_of_is_unit (s : set M) (r : R) (hr : is_unit r) : span R (r • s) = span R s := begin apply le_antisymm, { apply span_smul_le }, { convert span_smul_le (r • s) ((hr.unit ⁻¹ : _) : R), rw smul_smul, erw hr.unit.inv_val, rw one_smul } end @[simp] theorem coe_supr_of_directed {ι} [hι : nonempty ι] (S : ι → submodule R M) (H : directed (≤) S) : ((supr S : submodule R M) : set M) = ⋃ i, S i := begin refine subset.antisymm _ (Union_subset $ le_supr S), suffices : (span R (⋃ i, (S i : set M)) : set M) ⊆ ⋃ (i : ι), ↑(S i), by simpa only [span_Union, span_eq] using this, refine (λ x hx, span_induction hx (λ _, id) _ _ _); simp only [mem_Union, exists_imp_distrib], { exact hι.elim (λ i, ⟨i, (S i).zero_mem⟩) }, { intros x y i hi j hj, rcases H i j with ⟨k, ik, jk⟩, exact ⟨k, add_mem (ik hi) (jk hj)⟩ }, { exact λ a x i hi, ⟨i, smul_mem _ a hi⟩ }, end @[simp] theorem mem_supr_of_directed {ι} [nonempty ι] (S : ι → submodule R M) (H : directed (≤) S) {x} : x ∈ supr S ↔ ∃ i, x ∈ S i := by { rw [← set_like.mem_coe, coe_supr_of_directed S H, mem_Union], refl } theorem mem_Sup_of_directed {s : set (submodule R M)} {z} (hs : s.nonempty) (hdir : directed_on (≤) s) : z ∈ Sup s ↔ ∃ y ∈ s, z ∈ y := begin haveI : nonempty s := hs.to_subtype, simp only [Sup_eq_supr', mem_supr_of_directed _ hdir.directed_coe, set_coe.exists, subtype.coe_mk] end @[norm_cast, simp] lemma coe_supr_of_chain (a : ℕ →o submodule R M) : (↑(⨆ k, a k) : set M) = ⋃ k, (a k : set M) := coe_supr_of_directed a a.monotone.directed_le /-- We can regard `coe_supr_of_chain` as the statement that `coe : (submodule R M) → set M` is Scott continuous for the ω-complete partial order induced by the complete lattice structures. -/ lemma coe_scott_continuous : omega_complete_partial_order.continuous' (coe : submodule R M → set M) := ⟨set_like.coe_mono, coe_supr_of_chain⟩ @[simp] lemma mem_supr_of_chain (a : ℕ →o submodule R M) (m : M) : m ∈ (⨆ k, a k) ↔ ∃ k, m ∈ a k := mem_supr_of_directed a a.monotone.directed_le section variables {p p'} lemma mem_sup : x ∈ p ⊔ p' ↔ ∃ (y ∈ p) (z ∈ p'), y + z = x := ⟨λ h, begin rw [← span_eq p, ← span_eq p', ← span_union] at h, apply span_induction h, { rintro y (h | h), { exact ⟨y, h, 0, by simp, by simp⟩ }, { exact ⟨0, by simp, y, h, by simp⟩ } }, { exact ⟨0, by simp, 0, by simp⟩ }, { rintro _ _ ⟨y₁, hy₁, z₁, hz₁, rfl⟩ ⟨y₂, hy₂, z₂, hz₂, rfl⟩, exact ⟨_, add_mem hy₁ hy₂, _, add_mem hz₁ hz₂, by simp [add_assoc]; cc⟩ }, { rintro a _ ⟨y, hy, z, hz, rfl⟩, exact ⟨_, smul_mem _ a hy, _, smul_mem _ a hz, by simp [smul_add]⟩ } end, by rintro ⟨y, hy, z, hz, rfl⟩; exact add_mem ((le_sup_left : p ≤ p ⊔ p') hy) ((le_sup_right : p' ≤ p ⊔ p') hz)⟩ lemma mem_sup' : x ∈ p ⊔ p' ↔ ∃ (y : p) (z : p'), (y:M) + z = x := mem_sup.trans $ by simp only [set_like.exists, coe_mk] variables (p p') lemma coe_sup : ↑(p ⊔ p') = (p + p' : set M) := by { ext, rw [set_like.mem_coe, mem_sup, set.mem_add], simp, } lemma sup_to_add_submonoid : (p ⊔ p').to_add_submonoid = p.to_add_submonoid ⊔ p'.to_add_submonoid := begin ext x, rw [mem_to_add_submonoid, mem_sup, add_submonoid.mem_sup], refl, end lemma sup_to_add_subgroup {R M : Type*} [ring R] [add_comm_group M] [module R M] (p p' : submodule R M) : (p ⊔ p').to_add_subgroup = p.to_add_subgroup ⊔ p'.to_add_subgroup := begin ext x, rw [mem_to_add_subgroup, mem_sup, add_subgroup.mem_sup], refl, end end lemma mem_span_singleton_self (x : M) : x ∈ R ∙ x := subset_span rfl lemma nontrivial_span_singleton {x : M} (h : x ≠ 0) : nontrivial (R ∙ x) := ⟨begin use [0, x, submodule.mem_span_singleton_self x], intros H, rw [eq_comm, submodule.mk_eq_zero] at H, exact h H end⟩ lemma mem_span_singleton {y : M} : x ∈ (R ∙ y) ↔ ∃ a:R, a • y = x := ⟨λ h, begin apply span_induction h, { rintro y (rfl|⟨⟨⟩⟩), exact ⟨1, by simp⟩ }, { exact ⟨0, by simp⟩ }, { rintro _ _ ⟨a, rfl⟩ ⟨b, rfl⟩, exact ⟨a + b, by simp [add_smul]⟩ }, { rintro a _ ⟨b, rfl⟩, exact ⟨a * b, by simp [smul_smul]⟩ } end, by rintro ⟨a, y, rfl⟩; exact smul_mem _ _ (subset_span $ by simp)⟩ lemma le_span_singleton_iff {s : submodule R M} {v₀ : M} : s ≤ (R ∙ v₀) ↔ ∀ v ∈ s, ∃ r : R, r • v₀ = v := by simp_rw [set_like.le_def, mem_span_singleton] variables (R) lemma span_singleton_eq_top_iff (x : M) : (R ∙ x) = ⊤ ↔ ∀ v, ∃ r : R, r • x = v := by { rw [eq_top_iff, le_span_singleton_iff], tauto } @[simp] lemma span_zero_singleton : (R ∙ (0:M)) = ⊥ := by { ext, simp [mem_span_singleton, eq_comm] } lemma span_singleton_eq_range (y : M) : ↑(R ∙ y) = range ((• y) : R → M) := set.ext $ λ x, mem_span_singleton lemma span_singleton_smul_le {S} [monoid S] [has_smul S R] [mul_action S M] [is_scalar_tower S R M] (r : S) (x : M) : (R ∙ (r • x)) ≤ R ∙ x := begin rw [span_le, set.singleton_subset_iff, set_like.mem_coe], exact smul_of_tower_mem _ _ (mem_span_singleton_self _) end lemma span_singleton_group_smul_eq {G} [group G] [has_smul G R] [mul_action G M] [is_scalar_tower G R M] (g : G) (x : M) : (R ∙ (g • x)) = R ∙ x := begin refine le_antisymm (span_singleton_smul_le R g x) _, convert span_singleton_smul_le R (g⁻¹) (g • x), exact (inv_smul_smul g x).symm end variables {R} lemma span_singleton_smul_eq {r : R} (hr : is_unit r) (x : M) : (R ∙ (r • x)) = R ∙ x := begin lift r to Rˣ using hr, rw ←units.smul_def, exact span_singleton_group_smul_eq R r x, end lemma disjoint_span_singleton {K E : Type*} [division_ring K] [add_comm_group E] [module K E] {s : submodule K E} {x : E} : disjoint s (K ∙ x) ↔ (x ∈ s → x = 0) := begin refine disjoint_def.trans ⟨λ H hx, H x hx $ subset_span $ mem_singleton x, _⟩, assume H y hy hyx, obtain ⟨c, rfl⟩ := mem_span_singleton.1 hyx, by_cases hc : c = 0, { rw [hc, zero_smul] }, { rw [s.smul_mem_iff hc] at hy, rw [H hy, smul_zero] } end lemma disjoint_span_singleton' {K E : Type*} [division_ring K] [add_comm_group E] [module K E] {p : submodule K E} {x : E} (x0 : x ≠ 0) : disjoint p (K ∙ x) ↔ x ∉ p := disjoint_span_singleton.trans ⟨λ h₁ h₂, x0 (h₁ h₂), λ h₁ h₂, (h₁ h₂).elim⟩ lemma mem_span_singleton_trans {x y z : M} (hxy : x ∈ R ∙ y) (hyz : y ∈ R ∙ z) : x ∈ R ∙ z := begin rw [← set_like.mem_coe, ← singleton_subset_iff] at *, exact submodule.subset_span_trans hxy hyz end lemma mem_span_insert {y} : x ∈ span R (insert y s) ↔ ∃ (a:R) (z ∈ span R s), x = a • y + z := begin simp only [← union_singleton, span_union, mem_sup, mem_span_singleton, exists_prop, exists_exists_eq_and], rw [exists_comm], simp only [eq_comm, add_comm, exists_and_distrib_left] end lemma span_insert (x) (s : set M) : span R (insert x s) = span R ({x} : set M) ⊔ span R s := by rw [insert_eq, span_union] lemma span_insert_eq_span (h : x ∈ span R s) : span R (insert x s) = span R s := span_eq_of_le _ (set.insert_subset.mpr ⟨h, subset_span⟩) (span_mono $ subset_insert _ _) lemma span_span : span R (span R s : set M) = span R s := span_eq _ variables (R S s) /-- If `R` is "smaller" ring than `S` then the span by `R` is smaller than the span by `S`. -/ lemma span_le_restrict_scalars [semiring S] [has_smul R S] [module S M] [is_scalar_tower R S M] : span R s ≤ (span S s).restrict_scalars R := submodule.span_le.2 submodule.subset_span /-- A version of `submodule.span_le_restrict_scalars` with coercions. -/ @[simp] lemma span_subset_span [semiring S] [has_smul R S] [module S M] [is_scalar_tower R S M] : ↑(span R s) ⊆ (span S s : set M) := span_le_restrict_scalars R S s /-- Taking the span by a large ring of the span by the small ring is the same as taking the span by just the large ring. -/ lemma span_span_of_tower [semiring S] [has_smul R S] [module S M] [is_scalar_tower R S M] : span S (span R s : set M) = span S s := le_antisymm (span_le.2 $ span_subset_span R S s) (span_mono subset_span) variables {R S s} lemma span_eq_bot : span R (s : set M) = ⊥ ↔ ∀ x ∈ s, (x:M) = 0 := eq_bot_iff.trans ⟨ λ H x h, (mem_bot R).1 $ H $ subset_span h, λ H, span_le.2 (λ x h, (mem_bot R).2 $ H x h)⟩ @[simp] lemma span_singleton_eq_bot : (R ∙ x) = ⊥ ↔ x = 0 := span_eq_bot.trans $ by simp @[simp] lemma span_zero : span R (0 : set M) = ⊥ := by rw [←singleton_zero, span_singleton_eq_bot] lemma span_singleton_eq_span_singleton {R M : Type*} [ring R] [add_comm_group M] [module R M] [no_zero_smul_divisors R M] {x y : M} : (R ∙ x) = (R ∙ y) ↔ ∃ z : Rˣ, z • x = y := begin by_cases hx : x = 0, { rw [hx, span_zero_singleton, eq_comm, span_singleton_eq_bot], exact ⟨λ hy, ⟨1, by rw [hy, smul_zero]⟩, λ ⟨_, hz⟩, by rw [← hz, smul_zero]⟩ }, by_cases hy : y = 0, { rw [hy, span_zero_singleton, span_singleton_eq_bot], exact ⟨λ hx, ⟨1, by rw [hx, smul_zero]⟩, λ ⟨z, hz⟩, (smul_eq_zero_iff_eq z).mp hz⟩ }, split, { intro hxy, cases mem_span_singleton.mp (by { rw [hxy], apply mem_span_singleton_self }) with v hv, cases mem_span_singleton.mp (by { rw [← hxy], apply mem_span_singleton_self }) with i hi, have vi : v * i = 1 := by { rw [← one_smul R y, ← hi, smul_smul] at hv, exact smul_left_injective R hy hv }, have iv : i * v = 1 := by { rw [← one_smul R x, ← hv, smul_smul] at hi, exact smul_left_injective R hx hi }, exact ⟨⟨v, i, vi, iv⟩, hv⟩ }, { rintro ⟨v, rfl⟩, rw span_singleton_group_smul_eq } end @[simp] lemma span_image [ring_hom_surjective σ₁₂] (f : M →ₛₗ[σ₁₂] M₂) : span R₂ (f '' s) = map f (span R s) := (map_span f s).symm lemma apply_mem_span_image_of_mem_span [ring_hom_surjective σ₁₂] (f : M →ₛₗ[σ₁₂] M₂) {x : M} {s : set M} (h : x ∈ submodule.span R s) : f x ∈ submodule.span R₂ (f '' s) := begin rw submodule.span_image, exact submodule.mem_map_of_mem h end @[simp] lemma map_subtype_span_singleton {p : submodule R M} (x : p) : map p.subtype (R ∙ x) = R ∙ (x : M) := by simp [← span_image] /-- `f` is an explicit argument so we can `apply` this theorem and obtain `h` as a new goal. -/ lemma not_mem_span_of_apply_not_mem_span_image [ring_hom_surjective σ₁₂] (f : M →ₛₗ[σ₁₂] M₂) {x : M} {s : set M} (h : f x ∉ submodule.span R₂ (f '' s)) : x ∉ submodule.span R s := h.imp (apply_mem_span_image_of_mem_span f) lemma supr_span {ι : Sort*} (p : ι → set M) : (⨆ i, span R (p i)) = span R (⋃ i, p i) := le_antisymm (supr_le $ λ i, span_mono $ subset_Union _ i) $ span_le.mpr $ Union_subset $ λ i m hm, mem_supr_of_mem i $ subset_span hm lemma supr_eq_span {ι : Sort*} (p : ι → submodule R M) : (⨆ i, p i) = span R (⋃ i, ↑(p i)) := by simp_rw [← supr_span, span_eq] lemma supr_to_add_submonoid {ι : Sort*} (p : ι → submodule R M) : (⨆ i, p i).to_add_submonoid = ⨆ i, (p i).to_add_submonoid := begin refine le_antisymm (λ x, _) (supr_le $ λ i, to_add_submonoid_mono $ le_supr _ i), simp_rw [supr_eq_span, add_submonoid.supr_eq_closure, mem_to_add_submonoid, coe_to_add_submonoid], intros hx, refine submodule.span_induction hx (λ x hx, _) _ (λ x y hx hy, _) (λ r x hx, _), { exact add_submonoid.subset_closure hx }, { exact add_submonoid.zero_mem _ }, { exact add_submonoid.add_mem _ hx hy }, { apply add_submonoid.closure_induction hx, { rintros x ⟨_, ⟨i, rfl⟩, hix : x ∈ p i⟩, apply add_submonoid.subset_closure (set.mem_Union.mpr ⟨i, _⟩), exact smul_mem _ r hix }, { rw smul_zero, exact add_submonoid.zero_mem _ }, { intros x y hx hy, rw smul_add, exact add_submonoid.add_mem _ hx hy, } } end /-- An induction principle for elements of `⨆ i, p i`. If `C` holds for `0` and all elements of `p i` for all `i`, and is preserved under addition, then it holds for all elements of the supremum of `p`. -/ @[elab_as_eliminator] lemma supr_induction {ι : Sort*} (p : ι → submodule R M) {C : M → Prop} {x : M} (hx : x ∈ ⨆ i, p i) (hp : ∀ i (x ∈ p i), C x) (h0 : C 0) (hadd : ∀ x y, C x → C y → C (x + y)) : C x := begin rw [←mem_to_add_submonoid, supr_to_add_submonoid] at hx, exact add_submonoid.supr_induction _ hx hp h0 hadd, end /-- A dependent version of `submodule.supr_induction`. -/ @[elab_as_eliminator] lemma supr_induction' {ι : Sort*} (p : ι → submodule R M) {C : Π x, (x ∈ ⨆ i, p i) → Prop} (hp : ∀ i (x ∈ p i), C x (mem_supr_of_mem i ‹_›)) (h0 : C 0 (zero_mem _)) (hadd : ∀ x y hx hy, C x hx → C y hy → C (x + y) (add_mem ‹_› ‹_›)) {x : M} (hx : x ∈ ⨆ i, p i) : C x hx := begin refine exists.elim _ (λ (hx : x ∈ ⨆ i, p i) (hc : C x hx), hc), refine supr_induction p hx (λ i x hx, _) _ (λ x y, _), { exact ⟨_, hp _ _ hx⟩ }, { exact ⟨_, h0⟩ }, { rintro ⟨_, Cx⟩ ⟨_, Cy⟩, refine ⟨_, hadd _ _ _ _ Cx Cy⟩ }, end @[simp] lemma span_singleton_le_iff_mem (m : M) (p : submodule R M) : (R ∙ m) ≤ p ↔ m ∈ p := by rw [span_le, singleton_subset_iff, set_like.mem_coe] lemma singleton_span_is_compact_element (x : M) : complete_lattice.is_compact_element (span R {x} : submodule R M) := begin rw complete_lattice.is_compact_element_iff_le_of_directed_Sup_le, intros d hemp hdir hsup, have : x ∈ Sup d, from (set_like.le_def.mp hsup) (mem_span_singleton_self x), obtain ⟨y, ⟨hyd, hxy⟩⟩ := (mem_Sup_of_directed hemp hdir).mp this, exact ⟨y, ⟨hyd, by simpa only [span_le, singleton_subset_iff]⟩⟩, end /-- The span of a finite subset is compact in the lattice of submodules. -/ lemma finset_span_is_compact_element (S : finset M) : complete_lattice.is_compact_element (span R S : submodule R M) := begin rw span_eq_supr_of_singleton_spans, simp only [finset.mem_coe], rw ←finset.sup_eq_supr, exact complete_lattice.finset_sup_compact_of_compact S (λ x _, singleton_span_is_compact_element x), end /-- The span of a finite subset is compact in the lattice of submodules. -/ lemma finite_span_is_compact_element (S : set M) (h : S.finite) : complete_lattice.is_compact_element (span R S : submodule R M) := finite.coe_to_finset h ▸ (finset_span_is_compact_element h.to_finset) instance : is_compactly_generated (submodule R M) := ⟨λ s, ⟨(λ x, span R {x}) '' s, ⟨λ t ht, begin rcases (set.mem_image _ _ _).1 ht with ⟨x, hx, rfl⟩, apply singleton_span_is_compact_element, end, by rw [Sup_eq_supr, supr_image, ←span_eq_supr_of_singleton_spans, span_eq]⟩⟩⟩ /-- A submodule is equal to the supremum of the spans of the submodule's nonzero elements. -/ lemma submodule_eq_Sup_le_nonzero_spans (p : submodule R M) : p = Sup {T : submodule R M | ∃ (m : M) (hm : m ∈ p) (hz : m ≠ 0), T = span R {m}} := begin let S := {T : submodule R M | ∃ (m : M) (hm : m ∈ p) (hz : m ≠ 0), T = span R {m}}, apply le_antisymm, { intros m hm, by_cases h : m = 0, { rw h, simp }, { exact @le_Sup _ _ S _ ⟨m, ⟨hm, ⟨h, rfl⟩⟩⟩ m (mem_span_singleton_self m) } }, { rw Sup_le_iff, rintros S ⟨_, ⟨_, ⟨_, rfl⟩⟩⟩, rwa span_singleton_le_iff_mem } end lemma lt_sup_iff_not_mem {I : submodule R M} {a : M} : I < I ⊔ (R ∙ a) ↔ a ∉ I := begin split, { intro h, by_contra akey, have h1 : I ⊔ (R ∙ a) ≤ I, { simp only [sup_le_iff], split, { exact le_refl I, }, { exact (span_singleton_le_iff_mem a I).mpr akey, } }, have h2 := gt_of_ge_of_gt h1 h, exact lt_irrefl I h2, }, { intro h, apply set_like.lt_iff_le_and_exists.mpr, split, simp only [le_sup_left], use a, split, swap, { assumption, }, { have : (R ∙ a) ≤ I ⊔ (R ∙ a) := le_sup_right, exact this (mem_span_singleton_self a), } }, end lemma mem_supr {ι : Sort*} (p : ι → submodule R M) {m : M} : (m ∈ ⨆ i, p i) ↔ (∀ N, (∀ i, p i ≤ N) → m ∈ N) := begin rw [← span_singleton_le_iff_mem, le_supr_iff], simp only [span_singleton_le_iff_mem], end section open_locale classical /-- For every element in the span of a set, there exists a finite subset of the set such that the element is contained in the span of the subset. -/ lemma mem_span_finite_of_mem_span {S : set M} {x : M} (hx : x ∈ span R S) : ∃ T : finset M, ↑T ⊆ S ∧ x ∈ span R (T : set M) := begin refine span_induction hx (λ x hx, _) _ _ _, { refine ⟨{x}, _, _⟩, { rwa [finset.coe_singleton, set.singleton_subset_iff] }, { rw finset.coe_singleton, exact submodule.mem_span_singleton_self x } }, { use ∅, simp }, { rintros x y ⟨X, hX, hxX⟩ ⟨Y, hY, hyY⟩, refine ⟨X ∪ Y, _, _⟩, { rw finset.coe_union, exact set.union_subset hX hY }, rw [finset.coe_union, span_union, mem_sup], exact ⟨x, hxX, y, hyY, rfl⟩, }, { rintros a x ⟨T, hT, h2⟩, exact ⟨T, hT, smul_mem _ _ h2⟩ } end end variables {M' : Type*} [add_comm_monoid M'] [module R M'] (q₁ q₁' : submodule R M') /-- The product of two submodules is a submodule. -/ def prod : submodule R (M × M') := { carrier := p ×ˢ q₁, smul_mem' := by rintro a ⟨x, y⟩ ⟨hx, hy⟩; exact ⟨smul_mem _ a hx, smul_mem _ a hy⟩, .. p.to_add_submonoid.prod q₁.to_add_submonoid } @[simp] lemma prod_coe : (prod p q₁ : set (M × M')) = p ×ˢ q₁ := rfl @[simp] lemma mem_prod {p : submodule R M} {q : submodule R M'} {x : M × M'} : x ∈ prod p q ↔ x.1 ∈ p ∧ x.2 ∈ q := set.mem_prod lemma span_prod_le (s : set M) (t : set M') : span R (s ×ˢ t) ≤ prod (span R s) (span R t) := span_le.2 $ set.prod_mono subset_span subset_span @[simp] lemma prod_top : (prod ⊤ ⊤ : submodule R (M × M')) = ⊤ := by ext; simp @[simp] lemma prod_bot : (prod ⊥ ⊥ : submodule R (M × M')) = ⊥ := by ext ⟨x, y⟩; simp [prod.zero_eq_mk] lemma prod_mono {p p' : submodule R M} {q q' : submodule R M'} : p ≤ p' → q ≤ q' → prod p q ≤ prod p' q' := prod_mono @[simp] lemma prod_inf_prod : prod p q₁ ⊓ prod p' q₁' = prod (p ⊓ p') (q₁ ⊓ q₁') := set_like.coe_injective set.prod_inter_prod @[simp] lemma prod_sup_prod : prod p q₁ ⊔ prod p' q₁' = prod (p ⊔ p') (q₁ ⊔ q₁') := begin refine le_antisymm (sup_le (prod_mono le_sup_left le_sup_left) (prod_mono le_sup_right le_sup_right)) _, simp [set_like.le_def], intros xx yy hxx hyy, rcases mem_sup.1 hxx with ⟨x, hx, x', hx', rfl⟩, rcases mem_sup.1 hyy with ⟨y, hy, y', hy', rfl⟩, refine mem_sup.2 ⟨(x, y), ⟨hx, hy⟩, (x', y'), ⟨hx', hy'⟩, rfl⟩ end end add_comm_monoid section add_comm_group variables [ring R] [add_comm_group M] [module R M] @[simp] lemma span_neg (s : set M) : span R (-s) = span R s := calc span R (-s) = span R ((-linear_map.id : M →ₗ[R] M) '' s) : by simp ... = map (-linear_map.id) (span R s) : ((-linear_map.id).map_span _).symm ... = span R s : by simp lemma mem_span_insert' {x y} {s : set M} : x ∈ span R (insert y s) ↔ ∃(a:R), x + a • y ∈ span R s := begin rw mem_span_insert, split, { rintro ⟨a, z, hz, rfl⟩, exact ⟨-a, by simp [hz, add_assoc]⟩ }, { rintro ⟨a, h⟩, exact ⟨-a, _, h, by simp [add_comm, add_left_comm]⟩ } end instance : is_modular_lattice (submodule R M) := ⟨λ x y z xz a ha, begin rw [mem_inf, mem_sup] at ha, rcases ha with ⟨⟨b, hb, c, hc, rfl⟩, haz⟩, rw mem_sup, refine ⟨b, hb, c, mem_inf.2 ⟨hc, _⟩, rfl⟩, rw [← add_sub_cancel c b, add_comm], apply z.sub_mem haz (xz hb), end⟩ end add_comm_group section add_comm_group variables [semiring R] [semiring R₂] variables [add_comm_group M] [module R M] [add_comm_group M₂] [module R₂ M₂] variables {τ₁₂ : R →+* R₂} [ring_hom_surjective τ₁₂] variables {F : Type*} [sc : semilinear_map_class F τ₁₂ M M₂] include sc lemma comap_map_eq (f : F) (p : submodule R M) : comap f (map f p) = p ⊔ (linear_map.ker f) := begin refine le_antisymm _ (sup_le (le_comap_map _ _) (comap_mono bot_le)), rintro x ⟨y, hy, e⟩, exact mem_sup.2 ⟨y, hy, x - y, by simpa using sub_eq_zero.2 e.symm, by simp⟩ end lemma comap_map_eq_self {f : F} {p : submodule R M} (h : linear_map.ker f ≤ p) : comap f (map f p) = p := by rw [submodule.comap_map_eq, sup_of_le_left h] omit sc end add_comm_group end submodule namespace linear_map open submodule function section add_comm_group variables [semiring R] [semiring R₂] variables [add_comm_group M] [add_comm_group M₂] variables [module R M] [module R₂ M₂] variables {τ₁₂ : R →+* R₂} [ring_hom_surjective τ₁₂] variables {F : Type*} [sc : semilinear_map_class F τ₁₂ M M₂] include R include sc protected lemma map_le_map_iff (f : F) {p p'} : map f p ≤ map f p' ↔ p ≤ p' ⊔ ker f := by rw [map_le_iff_le_comap, submodule.comap_map_eq] theorem map_le_map_iff' {f : F} (hf : ker f = ⊥) {p p'} : map f p ≤ map f p' ↔ p ≤ p' := by rw [linear_map.map_le_map_iff, hf, sup_bot_eq] theorem map_injective {f : F} (hf : ker f = ⊥) : injective (map f) := λ p p' h, le_antisymm ((map_le_map_iff' hf).1 (le_of_eq h)) ((map_le_map_iff' hf).1 (ge_of_eq h)) theorem map_eq_top_iff {f : F} (hf : range f = ⊤) {p : submodule R M} : p.map f = ⊤ ↔ p ⊔ linear_map.ker f = ⊤ := by simp_rw [← top_le_iff, ← hf, range_eq_map, linear_map.map_le_map_iff] end add_comm_group section variables (R) (M) [semiring R] [add_comm_monoid M] [module R M] /-- Given an element `x` of a module `M` over `R`, the natural map from `R` to scalar multiples of `x`.-/ @[simps] def to_span_singleton (x : M) : R →ₗ[R] M := linear_map.id.smul_right x /-- The range of `to_span_singleton x` is the span of `x`.-/ lemma span_singleton_eq_range (x : M) : (R ∙ x) = (to_span_singleton R M x).range := submodule.ext $ λ y, by {refine iff.trans _ linear_map.mem_range.symm, exact mem_span_singleton } @[simp] lemma to_span_singleton_one (x : M) : to_span_singleton R M x 1 = x := one_smul _ _ @[simp] lemma to_span_singleton_zero : to_span_singleton R M 0 = 0 := by { ext, simp, } end section add_comm_monoid variables [semiring R] [add_comm_monoid M] [module R M] variables [semiring R₂] [add_comm_monoid M₂] [module R₂ M₂] variables {σ₁₂ : R →+* R₂} /-- If two linear maps are equal on a set `s`, then they are equal on `submodule.span s`. See also `linear_map.eq_on_span'` for a version using `set.eq_on`. -/ lemma eq_on_span {s : set M} {f g : M →ₛₗ[σ₁₂] M₂} (H : set.eq_on f g s) ⦃x⦄ (h : x ∈ span R s) : f x = g x := by apply span_induction h H; simp {contextual := tt} /-- If two linear maps are equal on a set `s`, then they are equal on `submodule.span s`. This version uses `set.eq_on`, and the hidden argument will expand to `h : x ∈ (span R s : set M)`. See `linear_map.eq_on_span` for a version that takes `h : x ∈ span R s` as an argument. -/ lemma eq_on_span' {s : set M} {f g : M →ₛₗ[σ₁₂] M₂} (H : set.eq_on f g s) : set.eq_on f g (span R s : set M) := eq_on_span H /-- If `s` generates the whole module and linear maps `f`, `g` are equal on `s`, then they are equal. -/ lemma ext_on {s : set M} {f g : M →ₛₗ[σ₁₂] M₂} (hv : span R s = ⊤) (h : set.eq_on f g s) : f = g := linear_map.ext (λ x, eq_on_span h (eq_top_iff'.1 hv _)) /-- If the range of `v : ι → M` generates the whole module and linear maps `f`, `g` are equal at each `v i`, then they are equal. -/ lemma ext_on_range {ι : Type*} {v : ι → M} {f g : M →ₛₗ[σ₁₂] M₂} (hv : span R (set.range v) = ⊤) (h : ∀i, f (v i) = g (v i)) : f = g := ext_on hv (set.forall_range_iff.2 h) end add_comm_monoid section field variables {K V} [field K] [add_comm_group V] [module K V] noncomputable theory open_locale classical lemma span_singleton_sup_ker_eq_top (f : V →ₗ[K] K) {x : V} (hx : f x ≠ 0) : (K ∙ x) ⊔ f.ker = ⊤ := eq_top_iff.2 (λ y hy, submodule.mem_sup.2 ⟨(f y * (f x)⁻¹) • x, submodule.mem_span_singleton.2 ⟨f y * (f x)⁻¹, rfl⟩, ⟨y - (f y * (f x)⁻¹) • x, by rw [linear_map.mem_ker, f.map_sub, f.map_smul, smul_eq_mul, mul_assoc, inv_mul_cancel hx, mul_one, sub_self], by simp only [add_sub_cancel'_right]⟩⟩) variables (K V) lemma ker_to_span_singleton {x : V} (h : x ≠ 0) : (to_span_singleton K V x).ker = ⊥ := begin ext c, split, { intros hc, rw submodule.mem_bot, rw mem_ker at hc, by_contra hc', have : x = 0, calc x = c⁻¹ • (c • x) : by rw [← mul_smul, inv_mul_cancel hc', one_smul] ... = c⁻¹ • ((to_span_singleton K V x) c) : rfl ... = 0 : by rw [hc, smul_zero], tauto }, { rw [mem_ker, submodule.mem_bot], intros h, rw h, simp } end end field end linear_map open linear_map namespace linear_equiv section field variables (K V) [field K] [add_comm_group V] [module K V] /-- Given a nonzero element `x` of a vector space `V` over a field `K`, the natural map from `K` to the span of `x`, with invertibility check to consider it as an isomorphism.-/ def to_span_nonzero_singleton (x : V) (h : x ≠ 0) : K ≃ₗ[K] (K ∙ x) := linear_equiv.trans (linear_equiv.of_injective (linear_map.to_span_singleton K V x) (ker_eq_bot.1 $ linear_map.ker_to_span_singleton K V h)) (linear_equiv.of_eq (to_span_singleton K V x).range (K ∙ x) (span_singleton_eq_range K V x).symm) lemma to_span_nonzero_singleton_one (x : V) (h : x ≠ 0) : linear_equiv.to_span_nonzero_singleton K V x h 1 = (⟨x, submodule.mem_span_singleton_self x⟩ : K ∙ x) := begin apply set_like.coe_eq_coe.mp, have : ↑(to_span_nonzero_singleton K V x h 1) = to_span_singleton K V x 1 := rfl, rw [this, to_span_singleton_one, submodule.coe_mk], end /-- Given a nonzero element `x` of a vector space `V` over a field `K`, the natural map from the span of `x` to `K`.-/ abbreviation coord (x : V) (h : x ≠ 0) : (K ∙ x) ≃ₗ[K] K := (to_span_nonzero_singleton K V x h).symm lemma coord_self (x : V) (h : x ≠ 0) : (coord K V x h) (⟨x, submodule.mem_span_singleton_self x⟩ : K ∙ x) = 1 := by rw [← to_span_nonzero_singleton_one K V x h, linear_equiv.symm_apply_apply] end field end linear_equiv
56248d2f8676d897c697fe127be8c6aa8d01bea2
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/algebra/order/group/abs.lean
a105b0dd3f452666feb273f31e50a85f8b8e1437
[ "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
9,890
lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Mario Carneiro, Johannes Hölzl -/ import algebra.abs import algebra.order.group.order_iso import order.min_max /-! # Absolute values in ordered groups. -/ variables {α : Type*} open function section covariant_add_le section has_neg /-- `abs a` is the absolute value of `a`. -/ @[to_additive "`abs a` is the absolute value of `a`", priority 100] -- see Note [lower instance priority] instance has_inv.to_has_abs [has_inv α] [has_sup α] : has_abs α := ⟨λ a, a ⊔ a⁻¹⟩ @[to_additive] lemma abs_eq_sup_inv [has_inv α] [has_sup α] (a : α) : |a| = a ⊔ a⁻¹ := rfl variables [has_neg α] [linear_order α] {a b: α} lemma abs_eq_max_neg : abs a = max a (-a) := rfl lemma abs_choice (x : α) : |x| = x ∨ |x| = -x := max_choice _ _ lemma abs_le' : |a| ≤ b ↔ a ≤ b ∧ -a ≤ b := max_le_iff lemma le_abs : a ≤ |b| ↔ a ≤ b ∨ a ≤ -b := le_max_iff lemma le_abs_self (a : α) : a ≤ |a| := le_max_left _ _ lemma neg_le_abs_self (a : α) : -a ≤ |a| := le_max_right _ _ lemma lt_abs : a < |b| ↔ a < b ∨ a < -b := lt_max_iff theorem abs_le_abs (h₀ : a ≤ b) (h₁ : -a ≤ b) : |a| ≤ |b| := (abs_le'.2 ⟨h₀, h₁⟩).trans (le_abs_self b) lemma abs_by_cases (P : α → Prop) {a : α} (h1 : P a) (h2 : P (-a)) : P (|a|) := sup_ind _ _ h1 h2 end has_neg section add_group variables [add_group α] [linear_order α] @[simp] lemma abs_neg (a : α) : | -a| = |a| := begin rw [abs_eq_max_neg, max_comm, neg_neg, abs_eq_max_neg] end lemma eq_or_eq_neg_of_abs_eq {a b : α} (h : |a| = b) : a = b ∨ a = -b := by simpa only [← h, eq_comm, eq_neg_iff_eq_neg] using abs_choice a lemma abs_eq_abs {a b : α} : |a| = |b| ↔ a = b ∨ a = -b := begin refine ⟨λ h, _, λ h, _⟩, { obtain rfl | rfl := eq_or_eq_neg_of_abs_eq h; simpa only [neg_eq_iff_neg_eq, neg_inj, or.comm, @eq_comm _ (-b)] using abs_choice b }, { cases h; simp only [h, abs_neg] }, end lemma abs_sub_comm (a b : α) : |a - b| = |b - a| := calc |a - b| = | - (b - a)| : congr_arg _ (neg_sub b a).symm ... = |b - a| : abs_neg (b - a) variables [covariant_class α α (+) (≤)] {a b c : α} lemma abs_of_nonneg (h : 0 ≤ a) : |a| = a := max_eq_left $ (neg_nonpos.2 h).trans h lemma abs_of_pos (h : 0 < a) : |a| = a := abs_of_nonneg h.le lemma abs_of_nonpos (h : a ≤ 0) : |a| = -a := max_eq_right $ h.trans (neg_nonneg.2 h) lemma abs_of_neg (h : a < 0) : |a| = -a := abs_of_nonpos h.le lemma abs_le_abs_of_nonneg (ha : 0 ≤ a) (hab : a ≤ b) : |a| ≤ |b| := by rwa [abs_of_nonneg ha, abs_of_nonneg (ha.trans hab)] @[simp] lemma abs_zero : |0| = (0:α) := abs_of_nonneg le_rfl @[simp] lemma abs_pos : 0 < |a| ↔ a ≠ 0 := begin rcases lt_trichotomy a 0 with (ha|rfl|ha), { simp [abs_of_neg ha, neg_pos, ha.ne, ha] }, { simp }, { simp [abs_of_pos ha, ha, ha.ne.symm] } end lemma abs_pos_of_pos (h : 0 < a) : 0 < |a| := abs_pos.2 h.ne.symm lemma abs_pos_of_neg (h : a < 0) : 0 < |a| := abs_pos.2 h.ne lemma neg_abs_le_self (a : α) : -|a| ≤ a := begin cases le_total 0 a with h h, { calc -|a| = - a : congr_arg (has_neg.neg) (abs_of_nonneg h) ... ≤ 0 : neg_nonpos.mpr h ... ≤ a : h }, { calc -|a| = - - a : congr_arg (has_neg.neg) (abs_of_nonpos h) ... ≤ a : (neg_neg a).le } end lemma add_abs_nonneg (a : α) : 0 ≤ a + |a| := begin rw ←add_right_neg a, apply add_le_add_left, exact (neg_le_abs_self a), end lemma neg_abs_le_neg (a : α) : -|a| ≤ -a := by simpa using neg_abs_le_self (-a) @[simp] lemma abs_nonneg (a : α) : 0 ≤ |a| := (le_total 0 a).elim (λ h, h.trans (le_abs_self a)) (λ h, (neg_nonneg.2 h).trans $ neg_le_abs_self a) @[simp] lemma abs_abs (a : α) : | |a| | = |a| := abs_of_nonneg $ abs_nonneg a @[simp] lemma abs_eq_zero : |a| = 0 ↔ a = 0 := decidable.not_iff_not.1 $ ne_comm.trans $ (abs_nonneg a).lt_iff_ne.symm.trans abs_pos @[simp] lemma abs_nonpos_iff {a : α} : |a| ≤ 0 ↔ a = 0 := (abs_nonneg a).le_iff_eq.trans abs_eq_zero variable [covariant_class α α (swap (+)) (≤)] lemma abs_le_abs_of_nonpos (ha : a ≤ 0) (hab : b ≤ a) : |a| ≤ |b| := by { rw [abs_of_nonpos ha, abs_of_nonpos (hab.trans ha)], exact neg_le_neg_iff.mpr hab } lemma abs_lt : |a| < b ↔ - b < a ∧ a < b := max_lt_iff.trans $ and.comm.trans $ by rw [neg_lt] lemma neg_lt_of_abs_lt (h : |a| < b) : -b < a := (abs_lt.mp h).1 lemma lt_of_abs_lt (h : |a| < b) : a < b := (abs_lt.mp h).2 lemma max_sub_min_eq_abs' (a b : α) : max a b - min a b = |a - b| := begin cases le_total a b with ab ba, { rw [max_eq_right ab, min_eq_left ab, abs_of_nonpos, neg_sub], rwa sub_nonpos }, { rw [max_eq_left ba, min_eq_right ba, abs_of_nonneg], rwa sub_nonneg } end lemma max_sub_min_eq_abs (a b : α) : max a b - min a b = |b - a| := by { rw abs_sub_comm, exact max_sub_min_eq_abs' _ _ } end add_group end covariant_add_le section linear_ordered_add_comm_group variables [linear_ordered_add_comm_group α] {a b c d : α} lemma abs_le : |a| ≤ b ↔ - b ≤ a ∧ a ≤ b := by rw [abs_le', and.comm, neg_le] lemma le_abs' : a ≤ |b| ↔ b ≤ -a ∨ a ≤ b := by rw [le_abs, or.comm, le_neg] lemma neg_le_of_abs_le (h : |a| ≤ b) : -b ≤ a := (abs_le.mp h).1 lemma le_of_abs_le (h : |a| ≤ b) : a ≤ b := (abs_le.mp h).2 @[to_additive] lemma apply_abs_le_mul_of_one_le' {β : Type*} [mul_one_class β] [preorder β] [covariant_class β β (*) (≤)] [covariant_class β β (swap (*)) (≤)] {f : α → β} {a : α} (h₁ : 1 ≤ f a) (h₂ : 1 ≤ f (-a)) : f (|a|) ≤ f a * f (-a) := (le_total a 0).by_cases (λ ha, (abs_of_nonpos ha).symm ▸ le_mul_of_one_le_left' h₁) (λ ha, (abs_of_nonneg ha).symm ▸ le_mul_of_one_le_right' h₂) @[to_additive] lemma apply_abs_le_mul_of_one_le {β : Type*} [mul_one_class β] [preorder β] [covariant_class β β (*) (≤)] [covariant_class β β (swap (*)) (≤)] {f : α → β} (h : ∀ x, 1 ≤ f x) (a : α) : f (|a|) ≤ f a * f (-a) := apply_abs_le_mul_of_one_le' (h _) (h _) /-- The **triangle inequality** in `linear_ordered_add_comm_group`s. -/ lemma abs_add (a b : α) : |a + b| ≤ |a| + |b| := abs_le.2 ⟨(neg_add (|a|) (|b|)).symm ▸ add_le_add (neg_le.2 $ neg_le_abs_self _) (neg_le.2 $ neg_le_abs_self _), add_le_add (le_abs_self _) (le_abs_self _)⟩ lemma abs_add' (a b : α) : |a| ≤ |b| + |b + a| := by simpa using abs_add (-b) (b + a) theorem abs_sub (a b : α) : |a - b| ≤ |a| + |b| := by { rw [sub_eq_add_neg, ←abs_neg b], exact abs_add a _ } lemma abs_sub_le_iff : |a - b| ≤ c ↔ a - b ≤ c ∧ b - a ≤ c := by rw [abs_le, neg_le_sub_iff_le_add, sub_le_iff_le_add', and_comm, sub_le_iff_le_add'] lemma abs_sub_lt_iff : |a - b| < c ↔ a - b < c ∧ b - a < c := by rw [abs_lt, neg_lt_sub_iff_lt_add', sub_lt_iff_lt_add', and_comm, sub_lt_iff_lt_add'] lemma sub_le_of_abs_sub_le_left (h : |a - b| ≤ c) : b - c ≤ a := sub_le_comm.1 $ (abs_sub_le_iff.1 h).2 lemma sub_le_of_abs_sub_le_right (h : |a - b| ≤ c) : a - c ≤ b := sub_le_of_abs_sub_le_left (abs_sub_comm a b ▸ h) lemma sub_lt_of_abs_sub_lt_left (h : |a - b| < c) : b - c < a := sub_lt_comm.1 $ (abs_sub_lt_iff.1 h).2 lemma sub_lt_of_abs_sub_lt_right (h : |a - b| < c) : a - c < b := sub_lt_of_abs_sub_lt_left (abs_sub_comm a b ▸ h) lemma abs_sub_abs_le_abs_sub (a b : α) : |a| - |b| ≤ |a - b| := sub_le_iff_le_add.2 $ calc |a| = |a - b + b| : by rw [sub_add_cancel] ... ≤ |a - b| + |b| : abs_add _ _ lemma abs_abs_sub_abs_le_abs_sub (a b : α) : | |a| - |b| | ≤ |a - b| := abs_sub_le_iff.2 ⟨abs_sub_abs_le_abs_sub _ _, by rw abs_sub_comm; apply abs_sub_abs_le_abs_sub⟩ lemma abs_eq (hb : 0 ≤ b) : |a| = b ↔ a = b ∨ a = -b := begin refine ⟨eq_or_eq_neg_of_abs_eq, _⟩, rintro (rfl|rfl); simp only [abs_neg, abs_of_nonneg hb] end lemma abs_le_max_abs_abs (hab : a ≤ b) (hbc : b ≤ c) : |b| ≤ max (|a|) (|c|) := abs_le'.2 ⟨by simp [hbc.trans (le_abs_self c)], by simp [(neg_le_neg_iff.mpr hab).trans (neg_le_abs_self a)]⟩ lemma min_abs_abs_le_abs_max : min (|a|) (|b|) ≤ |max a b| := (le_total a b).elim (λ h, (min_le_right _ _).trans_eq $ congr_arg _ (max_eq_right h).symm) (λ h, (min_le_left _ _).trans_eq $ congr_arg _ (max_eq_left h).symm) lemma min_abs_abs_le_abs_min : min (|a|) (|b|) ≤ |min a b| := (le_total a b).elim (λ h, (min_le_left _ _).trans_eq $ congr_arg _ (min_eq_left h).symm) (λ h, (min_le_right _ _).trans_eq $ congr_arg _ (min_eq_right h).symm) lemma abs_max_le_max_abs_abs : |max a b| ≤ max (|a|) (|b|) := (le_total a b).elim (λ h, (congr_arg _ $ max_eq_right h).trans_le $ le_max_right _ _) (λ h, (congr_arg _ $ max_eq_left h).trans_le $ le_max_left _ _) lemma abs_min_le_max_abs_abs : |min a b| ≤ max (|a|) (|b|) := (le_total a b).elim (λ h, (congr_arg _ $ min_eq_left h).trans_le $ le_max_left _ _) (λ h, (congr_arg _ $ min_eq_right h).trans_le $ le_max_right _ _) lemma eq_of_abs_sub_eq_zero {a b : α} (h : |a - b| = 0) : a = b := sub_eq_zero.1 $ abs_eq_zero.1 h lemma abs_sub_le (a b c : α) : |a - c| ≤ |a - b| + |b - c| := calc |a - c| = |a - b + (b - c)| : by rw [sub_add_sub_cancel] ... ≤ |a - b| + |b - c| : abs_add _ _ lemma abs_add_three (a b c : α) : |a + b + c| ≤ |a| + |b| + |c| := (abs_add _ _).trans (add_le_add_right (abs_add _ _) _) lemma dist_bdd_within_interval {a b lb ub : α} (hal : lb ≤ a) (hau : a ≤ ub) (hbl : lb ≤ b) (hbu : b ≤ ub) : |a - b| ≤ ub - lb := abs_sub_le_iff.2 ⟨sub_le_sub hau hbl, sub_le_sub hbu hal⟩ lemma eq_of_abs_sub_nonpos (h : |a - b| ≤ 0) : a = b := eq_of_abs_sub_eq_zero (le_antisymm h (abs_nonneg (a - b))) end linear_ordered_add_comm_group
96f35fb6be9f25d64b287b9ff5fd3b3635bd3ceb
46125763b4dbf50619e8846a1371029346f4c3db
/src/geometry/manifold/basic_smooth_bundle.lean
224134f08211b19f8afb620f49cec3a312c878ae
[ "Apache-2.0" ]
permissive
thjread/mathlib
a9d97612cedc2c3101060737233df15abcdb9eb1
7cffe2520a5518bba19227a107078d83fa725ddc
refs/heads/master
1,615,637,696,376
1,583,953,063,000
1,583,953,063,000
246,680,271
0
0
Apache-2.0
1,583,960,875,000
1,583,960,875,000
null
UTF-8
Lean
false
false
32,882
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 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).to_fun 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.inv_fun p.1) p.2) ((I.to_fun '' (i.1.symm.trans j.1).source).prod (univ : set F))) 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) /-- 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.to_fun x) v, coord_change_self := λi x hx v, Z.coord_change_self i (i.1.to_fun 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.to_fun 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.inv_fun p.1) p.2) ((I.to_fun '' (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.to_fun (i.1.to_fun x)) i.1.source := I.continuous_to_fun.comp_continuous_on i.1.continuous_to_fun, have C : continuous_on (λp : M × F, (⟨I.to_fun (i.1.to_fun 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.to_fun (i.1.to_fun 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.to_fun (i.1.to_fun p.1), p.2)) ⁻¹' ((I.to_fun '' (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_equiv.refl_inv_fun, local_homeomorph.trans_to_local_equiv, local_equiv.refl_target, local_homeomorph.refl_local_equiv, local_equiv.prod_target, local_homeomorph.prod_inv_fun], 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 chart_at_to_fun_fst (p q : Z.to_topological_fiber_bundle_core.total_space) : ((chart_at (H × F) q).to_fun p).1 = (chart_at H q.1).to_fun p.1 := rfl @[simp] lemma chart_at_inv_fun_fst (p : H × F) (q : Z.to_topological_fiber_bundle_core.total_space) : ((chart_at (H × F) q).inv_fun p).1 = (chart_at H q.1).inv_fun 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.to_fun ∘ ((Z.chart he).symm.trans (Z.chart he')).to_fun ∘ J.inv_fun) (J.inv_fun ⁻¹' ((Z.chart he).symm.trans (Z.chart he')).source ∩ range J.to_fun), { assume e e' he he', have : J.inv_fun ⁻¹' ((chart Z he).symm.trans (chart Z he')).source ∩ range J.to_fun = (I.inv_fun ⁻¹' (e.symm.trans e').source ∩ range I.to_fun).prod univ, { have : range (λ (p : H × F), (I.to_fun (p.fst), id p.snd)) = (range I.to_fun).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.to_fun ∘ e'.to_fun ∘ e.inv_fun ∘ I.inv_fun) p.1) ((I.inv_fun ⁻¹' (e.symm.trans e').source ∩ range I.to_fun).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.to_fun ∘ (e.symm.trans e').to_fun ∘ I.inv_fun) (I.inv_fun ⁻¹' (e.symm.trans e').source ∩ range I.to_fun) := (has_groupoid.compatible (times_cont_diff_groupoid ⊤ I) he he').1, have B : times_cont_diff_on 𝕜 ⊤ (λp : E × F, p.1) ((I.inv_fun ⁻¹' (e.symm.trans e').source ∩ range I.to_fun).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.inv_fun (I.inv_fun p.1)), _⟩ ⟨e', he'⟩ ((chart_at H (e.inv_fun (I.inv_fun p.1))).to_fun (e.inv_fun (I.inv_fun p.1))) (Z.coord_change ⟨e, he⟩ ⟨chart_at H (e.inv_fun (I.inv_fun p.1)), _⟩ (e.to_fun (e.inv_fun (I.inv_fun p.1))) p.2)) ((I.inv_fun ⁻¹' (e.symm.trans e').source ∩ range I.to_fun).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.inv_fun (I.inv_fun x)), have A : I.inv_fun 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.inv_fun 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] set_option class.instance_max_depth 50 /-- 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.to_fun ∘ j.1.to_fun ∘ i.1.inv_fun ∘ I.inv_fun) (range I.to_fun) (I.to_fun 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.to_fun ∘ (i.1.symm.trans j.1).to_fun ∘ I.inv_fun) (I.inv_fun ⁻¹' (i.1.symm.trans j.1).source ∩ range I.to_fun) := (has_groupoid.compatible (times_cont_diff_groupoid ⊤ I) i.2 j.2).1, have B : unique_diff_on 𝕜 (I.inv_fun ⁻¹' (i.1.symm.trans j.1).source ∩ range I.to_fun), { rw inter_comm, apply I.unique_diff.inter (I.continuous_inv_fun _ (local_homeomorph.open_source _)) }, have C : times_cont_diff_on 𝕜 ⊤ (λ (p : E × E), (fderiv_within 𝕜 (I.to_fun ∘ j.1.to_fun ∘ i.1.inv_fun ∘ I.inv_fun) (I.inv_fun ⁻¹' (i.1.symm.trans j.1).source ∩ range I.to_fun) p.1 : E → E) p.2) ((I.inv_fun ⁻¹' (i.1.symm.trans j.1).source ∩ range I.to_fun).prod univ) := times_cont_diff_on_fderiv_within_apply A B lattice.le_top, have D : ∀ x ∈ (I.inv_fun ⁻¹' (i.1.symm.trans j.1).source ∩ range I.to_fun), fderiv_within 𝕜 (I.to_fun ∘ j.1.to_fun ∘ i.1.inv_fun ∘ I.inv_fun) (range I.to_fun) x = fderiv_within 𝕜 (I.to_fun ∘ j.1.to_fun ∘ i.1.inv_fun ∘ I.inv_fun) (I.inv_fun ⁻¹' (i.1.symm.trans j.1).source ∩ range I.to_fun) x, { assume x hx, have N : I.inv_fun ⁻¹' (i.1.symm.trans j.1).source ∈ nhds x := I.continuous_inv_fun.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.inv_fun ⁻¹' (i.1.symm.trans j.1).source ∩ range I.to_fun, by simpa using hx, have : I.to_fun (I.inv_fun 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.inv_fun ⁻¹' (i.1.symm.trans i.1).source ∩ range I.to_fun ∈ nhds_within (I.to_fun x) (range I.to_fun), { rw inter_comm, apply inter_mem_nhds_within, apply I.continuous_inv_fun.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.to_fun x) (range I.to_fun), (I.to_fun ∘ i.1.to_fun ∘ i.1.inv_fun ∘ I.inv_fun) 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.to_fun ∘ i.1.to_fun ∘ i.1.inv_fun ∘ I.inv_fun) (range I.to_fun) (I.to_fun x) = fderiv_within 𝕜 (id : E → E) (range I.to_fun) (I.to_fun x) := fderiv_within_congr_of_mem_nhds_within (I.unique_diff _ (mem_range_self _)) B (by simp [hx]), rw fderiv_within_id (I.unique_diff _ (mem_range_self _)) at C, rw C, refl end, coord_change_comp := λi j u x hx, begin /- 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.to_fun x ∈ (I.inv_fun ⁻¹' ((i.1.symm.trans j.1).trans (j.1.symm.trans u.1)).source ∩ range I.to_fun) := ⟨by simpa using hx, mem_range_self _⟩, have U : unique_diff_within_at 𝕜 (I.inv_fun ⁻¹' ((i.1.symm.trans j.1).trans (j.1.symm.trans u.1)).source ∩ range I.to_fun) (I.to_fun x), { have : unique_diff_on 𝕜 (I.inv_fun ⁻¹' ((i.1.symm.trans j.1).trans (j.1.symm.trans u.1)).source ∩ range I.to_fun), { rw inter_comm, exact I.unique_diff.inter (I.continuous_inv_fun _ (local_homeomorph.open_source _)) }, exact this _ M }, have A : fderiv_within 𝕜 ((I.to_fun ∘ u.1.to_fun ∘ j.1.inv_fun ∘ I.inv_fun) ∘ (I.to_fun ∘ j.1.to_fun ∘ i.1.inv_fun ∘ I.inv_fun)) (I.inv_fun ⁻¹' ((i.1.symm.trans j.1).trans (j.1.symm.trans u.1)).source ∩ range I.to_fun) (I.to_fun x) = (fderiv_within 𝕜 (I.to_fun ∘ u.1.to_fun ∘ j.1.inv_fun ∘ I.inv_fun) (I.inv_fun ⁻¹' (j.1.symm.trans u.1).source ∩ range I.to_fun) ((I.to_fun ∘ j.1.to_fun ∘ i.1.inv_fun ∘ I.inv_fun) (I.to_fun x))).comp (fderiv_within 𝕜 (I.to_fun ∘ j.1.to_fun ∘ i.1.inv_fun ∘ I.inv_fun) (I.inv_fun ⁻¹' ((i.1.symm.trans j.1).trans (j.1.symm.trans u.1)).source ∩ range I.to_fun) (I.to_fun x)), { apply fderiv_within.comp _ _ _ _ U, show differentiable_within_at 𝕜 (I.to_fun ∘ j.1.to_fun ∘ i.1.inv_fun ∘ I.inv_fun) (I.inv_fun ⁻¹' ((i.1.symm.trans j.1).trans (j.1.symm.trans u.1)).source ∩ range I.to_fun) (I.to_fun x), { have A : times_cont_diff_on 𝕜 ⊤ (I.to_fun ∘ (i.1.symm.trans j.1).to_fun ∘ I.inv_fun) (I.inv_fun ⁻¹' (i.1.symm.trans j.1).source ∩ range I.to_fun) := (has_groupoid.compatible (times_cont_diff_groupoid ⊤ I) i.2 j.2).1, have B : differentiable_on 𝕜 (I.to_fun ∘ j.1.to_fun ∘ i.1.inv_fun ∘ I.inv_fun) (I.inv_fun ⁻¹' ((i.1.symm.trans j.1).trans (j.1.symm.trans u.1)).source ∩ range I.to_fun), { apply (A.differentiable_on (lattice.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.to_fun)) }, apply B, simpa [mem_inter_iff, -mem_image, -mem_range, mem_range_self] using hx }, show differentiable_within_at 𝕜 (I.to_fun ∘ u.1.to_fun ∘ j.1.inv_fun ∘ I.inv_fun) (I.inv_fun ⁻¹' (j.1.symm.trans u.1).source ∩ range I.to_fun) ((I.to_fun ∘ j.1.to_fun ∘ i.1.inv_fun ∘ I.inv_fun) (I.to_fun x)), { have A : times_cont_diff_on 𝕜 ⊤ (I.to_fun ∘ (j.1.symm.trans u.1).to_fun ∘ I.inv_fun) (I.inv_fun ⁻¹' (j.1.symm.trans u.1).source ∩ range I.to_fun) := (has_groupoid.compatible (times_cont_diff_groupoid ⊤ I) j.2 u.2).1, apply A.differentiable_on (lattice.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.inv_fun ⁻¹' ((i.1.symm.trans j.1).trans (j.1.symm.trans u.1)).source ∩ range I.to_fun) ⊆ (I.to_fun ∘ j.1.to_fun ∘ i.1.inv_fun ∘ I.inv_fun) ⁻¹' (I.inv_fun ⁻¹' (j.1.symm.trans u.1).source ∩ range I.to_fun), { assume y hy, simp [-mem_range, local_equiv.trans_source] at hy, rw [local_equiv.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.to_fun ∘ u.1.to_fun ∘ j.1.inv_fun ∘ I.inv_fun) ∘ (I.to_fun ∘ j.1.to_fun ∘ i.1.inv_fun ∘ I.inv_fun)) (I.inv_fun ⁻¹' ((i.1.symm.trans j.1).trans (j.1.symm.trans u.1)).source ∩ range I.to_fun) (I.to_fun x) = fderiv_within 𝕜 (I.to_fun ∘ u.1.to_fun ∘ i.1.inv_fun ∘ I.inv_fun) (I.inv_fun ⁻¹' ((i.1.symm.trans j.1).trans (j.1.symm.trans u.1)).source ∩ range I.to_fun) (I.to_fun x), { have E : ∀ y ∈ (I.inv_fun ⁻¹' ((i.1.symm.trans j.1).trans (j.1.symm.trans u.1)).source ∩ range I.to_fun), ((I.to_fun ∘ u.1.to_fun ∘ j.1.inv_fun ∘ I.inv_fun) ∘ (I.to_fun ∘ j.1.to_fun ∘ i.1.inv_fun ∘ I.inv_fun)) y = (I.to_fun ∘ u.1.to_fun ∘ i.1.inv_fun ∘ I.inv_fun) 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.to_fun ∘ u.1.to_fun ∘ i.1.inv_fun ∘ I.inv_fun) (I.inv_fun ⁻¹' ((i.1.symm.trans j.1).trans (j.1.symm.trans u.1)).source ∩ range I.to_fun) (I.to_fun x) = fderiv_within 𝕜 (I.to_fun ∘ u.1.to_fun ∘ i.1.inv_fun ∘ I.inv_fun) (range I.to_fun) (I.to_fun x), { rw inter_comm, apply fderiv_within_inter _ (I.unique_diff _ (mem_range_self _)), apply I.continuous_inv_fun.continuous_at.preimage_mem_nhds (mem_nhds_sets (local_homeomorph.open_source _) _), simpa using hx }, have D : fderiv_within 𝕜 (I.to_fun ∘ u.1.to_fun ∘ j.1.inv_fun ∘ I.inv_fun) (I.inv_fun ⁻¹' (j.1.symm.trans u.1).source ∩ range I.to_fun) ((I.to_fun ∘ j.1.to_fun ∘ i.1.inv_fun ∘ I.inv_fun) (I.to_fun x)) = fderiv_within 𝕜 (I.to_fun ∘ u.1.to_fun ∘ j.1.inv_fun ∘ I.inv_fun) (range I.to_fun) ((I.to_fun ∘ j.1.to_fun ∘ i.1.inv_fun ∘ I.inv_fun) (I.to_fun x)), { rw inter_comm, apply fderiv_within_inter _ (I.unique_diff _ (mem_range_self _)), apply I.continuous_inv_fun.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.to_fun ∘ j.1.to_fun ∘ i.1.inv_fun ∘ I.inv_fun) (I.inv_fun ⁻¹' ((i.1.symm.trans j.1).trans (j.1.symm.trans u.1)).source ∩ range I.to_fun) (I.to_fun x) = (fderiv_within 𝕜 (I.to_fun ∘ j.1.to_fun ∘ i.1.inv_fun ∘ I.inv_fun) (range I.to_fun) (I.to_fun x)), { rw inter_comm, apply fderiv_within_inter _ (I.unique_diff _ (mem_range_self _)), apply I.continuous_inv_fun.continuous_at.preimage_mem_nhds (mem_nhds_sets (local_homeomorph.open_source _) _), simpa using hx }, rw [B, C, D, E] at A, rw A, assume v, simp, unfold_coes, simp end } /-- The tangent bundle to a smooth manifold, as a plain type. -/ 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 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.to_fun ∘ I.inv_fun) (range I.to_fun) (I.to_fun x_fst) = continuous_linear_map.id, { assume x_fst, have : fderiv_within 𝕜 (I.to_fun ∘ I.inv_fun) (range I.to_fun) (I.to_fun x_fst) = fderiv_within 𝕜 id (range I.to_fun) (I.to_fun x_fst), { refine fderiv_within_congr (I.unique_diff _ (mem_range_self _)) (λy hy, _) (by simp), exact model_with_corners_right_inv _ hy }, rwa fderiv_within_id (I.unique_diff _ (mem_range_self _)) at this }, ext x : 1, show (chart_at (H × E) p).to_fun x = (local_equiv.refl (H × E)).to_fun 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], erw [local_equiv.refl_to_fun, local_equiv.refl_inv_fun, A], refl }, show ∀ x, ((chart_at (H × E) p).to_local_equiv).inv_fun x = (local_equiv.refl (H × E)).inv_fun 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], erw [local_equiv.refl_to_fun, local_equiv.refl_inv_fun, A], refl }, 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 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.inv_fun (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, have := e'.continuous_inv_fun o ho, simpa [e', tangent_bundle_model_space_chart_at] }, { assume ho, have := e'.continuous_to_fun o ho, simpa [e', tangent_bundle_model_space_chart_at] } end end tangent_bundle
d49d529c75ac69e9a3ac8da2c3aae62fb1b7e7a7
bb31430994044506fa42fd667e2d556327e18dfe
/src/group_theory/group_action/sub_mul_action.lean
96abf240857d2d99bbc404bee1a68269b1aff375
[ "Apache-2.0" ]
permissive
sgouezel/mathlib
0cb4e5335a2ba189fa7af96d83a377f83270e503
00638177efd1b2534fc5269363ebf42a7871df9a
refs/heads/master
1,674,527,483,042
1,673,665,568,000
1,673,665,568,000
119,598,202
0
0
null
1,517,348,647,000
1,517,348,646,000
null
UTF-8
Lean
false
false
9,964
lean
/- Copyright (c) 2020 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser -/ import algebra.hom.group_action import algebra.module.basic import data.set_like.basic import group_theory.group_action.basic /-! # Sets invariant to a `mul_action` In this file we define `sub_mul_action R M`; a subset of a `mul_action R M` which is closed with respect to scalar multiplication. For most uses, typically `submodule R M` is more powerful. ## Main definitions * `sub_mul_action.mul_action` - the `mul_action R M` transferred to the subtype. * `sub_mul_action.mul_action'` - the `mul_action S M` transferred to the subtype when `is_scalar_tower S R M`. * `sub_mul_action.is_scalar_tower` - the `is_scalar_tower S R M` transferred to the subtype. ## Tags submodule, mul_action -/ open function universes u u' u'' v variables {S : Type u'} {T : Type u''} {R : Type u} {M : Type v} set_option old_structure_cmd true /-- `smul_mem_class S R M` says `S` is a type of subsets `s ≤ M` that are closed under the scalar action of `R` on `M`. -/ class smul_mem_class (S : Type*) (R M : out_param $ Type*) [has_smul R M] [set_like S M] := (smul_mem : ∀ {s : S} (r : R) {m : M}, m ∈ s → r • m ∈ s) /-- `vadd_mem_class S R M` says `S` is a type of subsets `s ≤ M` that are closed under the additive action of `R` on `M`. -/ class vadd_mem_class (S : Type*) (R M : out_param $ Type*) [has_vadd R M] [set_like S M] := (vadd_mem : ∀ {s : S} (r : R) {m : M}, m ∈ s → r +ᵥ m ∈ s) attribute [to_additive] smul_mem_class namespace set_like variables [has_smul R M] [set_like S M] [hS : smul_mem_class S R M] (s : S) include hS open smul_mem_class /-- A subset closed under the scalar action inherits that action. -/ @[to_additive "A subset closed under the additive action inherits that action.", priority 900] -- lower priority so other instances are found first instance has_smul : has_smul R s := ⟨λ r x, ⟨r • x.1, smul_mem r x.2⟩⟩ @[simp, norm_cast, to_additive, priority 900] -- lower priority so later simp lemmas are used first; to appease simp_nf protected lemma coe_smul (r : R) (x : s) : (↑(r • x) : M) = r • x := rfl @[simp, to_additive, priority 900] -- lower priority so later simp lemmas are used first; to appease simp_nf lemma mk_smul_mk (r : R) (x : M) (hx : x ∈ s) : r • (⟨x, hx⟩ : s) = ⟨r • x, smul_mem r hx⟩ := rfl @[to_additive] lemma smul_def (r : R) (x : s) : r • x = ⟨r • x, smul_mem r x.2⟩ := rfl omit hS @[simp] lemma forall_smul_mem_iff {R M S : Type*} [monoid R] [mul_action R M] [set_like S M] [smul_mem_class S R M] {N : S} {x : M} : (∀ (a : R), a • x ∈ N) ↔ x ∈ N := ⟨λ h, by simpa using h 1, λ h a, smul_mem_class.smul_mem a h⟩ end set_like /-- A sub_mul_action is a set which is closed under scalar multiplication. -/ structure sub_mul_action (R : Type u) (M : Type v) [has_smul R M] : Type v := (carrier : set M) (smul_mem' : ∀ (c : R) {x : M}, x ∈ carrier → c • x ∈ carrier) namespace sub_mul_action variables [has_smul R M] instance : set_like (sub_mul_action R M) M := ⟨sub_mul_action.carrier, λ p q h, by cases p; cases q; congr'⟩ instance : smul_mem_class (sub_mul_action R M) R M := { smul_mem := smul_mem' } @[simp] lemma mem_carrier {p : sub_mul_action R M} {x : M} : x ∈ p.carrier ↔ x ∈ (p : set M) := iff.rfl @[ext] theorem ext {p q : sub_mul_action R M} (h : ∀ x, x ∈ p ↔ x ∈ q) : p = q := set_like.ext h /-- Copy of a sub_mul_action with a new `carrier` equal to the old one. Useful to fix definitional equalities.-/ protected def copy (p : sub_mul_action R M) (s : set M) (hs : s = ↑p) : sub_mul_action R M := { carrier := s, smul_mem' := hs.symm ▸ p.smul_mem' } @[simp] lemma coe_copy (p : sub_mul_action R M) (s : set M) (hs : s = ↑p) : (p.copy s hs : set M) = s := rfl lemma copy_eq (p : sub_mul_action R M) (s : set M) (hs : s = ↑p) : p.copy s hs = p := set_like.coe_injective hs instance : has_bot (sub_mul_action R M) := ⟨{ carrier := ∅, smul_mem' := λ c, set.not_mem_empty}⟩ instance : inhabited (sub_mul_action R M) := ⟨⊥⟩ end sub_mul_action namespace sub_mul_action section has_smul variables [has_smul R M] variables (p : sub_mul_action R M) variables {r : R} {x : M} lemma smul_mem (r : R) (h : x ∈ p) : r • x ∈ p := p.smul_mem' r h instance : has_smul R p := { smul := λ c x, ⟨c • x.1, smul_mem _ c x.2⟩ } variables {p} @[simp, norm_cast] lemma coe_smul (r : R) (x : p) : ((r • x : p) : M) = r • ↑x := rfl @[simp, norm_cast] lemma coe_mk (x : M) (hx : x ∈ p) : ((⟨x, hx⟩ : p) : M) = x := rfl variables (p) /-- Embedding of a submodule `p` to the ambient space `M`. -/ protected def subtype : p →[R] M := by refine {to_fun := coe, ..}; simp [coe_smul] @[simp] theorem subtype_apply (x : p) : p.subtype x = x := rfl lemma subtype_eq_val : ((sub_mul_action.subtype p) : p → M) = subtype.val := rfl end has_smul namespace smul_mem_class variables [monoid R] [mul_action R M] {A : Type*} [set_like A M] variables [hA : smul_mem_class A R M] (S' : A) include hA /-- A `sub_mul_action` of a `mul_action` is a `mul_action`. -/ @[priority 75] -- Prefer subclasses of `mul_action` over `smul_mem_class`. instance to_mul_action : mul_action R S' := subtype.coe_injective.mul_action coe (set_like.coe_smul S') /-- The natural `mul_action_hom` over `R` from a `sub_mul_action` of `M` to `M`. -/ protected def subtype : S' →[R] M := ⟨coe, λ _ _, rfl⟩ @[simp] protected theorem coe_subtype : (smul_mem_class.subtype S' : S' → M) = coe := rfl end smul_mem_class section mul_action_monoid variables [monoid R] [mul_action R M] section variables [has_smul S R] [has_smul S M] [is_scalar_tower S R M] variables (p : sub_mul_action R M) lemma smul_of_tower_mem (s : S) {x : M} (h : x ∈ p) : s • x ∈ p := by { rw [←one_smul R x, ←smul_assoc], exact p.smul_mem _ h } instance has_smul' : has_smul S p := { smul := λ c x, ⟨c • x.1, smul_of_tower_mem _ c x.2⟩ } instance : is_scalar_tower S R p := { smul_assoc := λ s r x, subtype.ext $ smul_assoc s r ↑x } instance is_scalar_tower' {S' : Type*} [has_smul S' R] [has_smul S' S] [has_smul S' M] [is_scalar_tower S' R M] [is_scalar_tower S' S M] : is_scalar_tower S' S p := { smul_assoc := λ s r x, subtype.ext $ smul_assoc s r ↑x } @[simp, norm_cast] lemma coe_smul_of_tower (s : S) (x : p) : ((s • x : p) : M) = s • ↑x := rfl @[simp] lemma smul_mem_iff' {G} [group G] [has_smul G R] [mul_action G M] [is_scalar_tower G R M] (g : G) {x : M} : g • x ∈ p ↔ x ∈ p := ⟨λ h, inv_smul_smul g x ▸ p.smul_of_tower_mem g⁻¹ h, p.smul_of_tower_mem g⟩ instance [has_smul Sᵐᵒᵖ R] [has_smul Sᵐᵒᵖ M] [is_scalar_tower Sᵐᵒᵖ R M] [is_central_scalar S M] : is_central_scalar S p := { op_smul_eq_smul := λ r x, subtype.ext $ op_smul_eq_smul r x } end section variables [monoid S] [has_smul S R] [mul_action S M] [is_scalar_tower S R M] variables (p : sub_mul_action R M) /-- If the scalar product forms a `mul_action`, then the subset inherits this action -/ instance mul_action' : mul_action S p := { smul := (•), one_smul := λ x, subtype.ext $ one_smul _ x, mul_smul := λ c₁ c₂ x, subtype.ext $ mul_smul c₁ c₂ x } instance : mul_action R p := p.mul_action' end /-- Orbits in a `sub_mul_action` coincide with orbits in the ambient space. -/ lemma coe_image_orbit {p : sub_mul_action R M} (m : p) : coe '' mul_action.orbit R m = mul_action.orbit R (m : M) := (set.range_comp _ _).symm /- -- Previously, the relatively useless : lemma orbit_of_sub_mul {p : sub_mul_action R M} (m : p) : (mul_action.orbit R m : set M) = mul_action.orbit R (m : M) := rfl -/ /-- Stabilizers in monoid sub_mul_action coincide with stabilizers in the ambient space -/ lemma stabilizer_of_sub_mul.submonoid {p : sub_mul_action R M} (m : p) : mul_action.stabilizer.submonoid R m = mul_action.stabilizer.submonoid R (m : M) := begin ext, simp only [mul_action.mem_stabilizer_submonoid_iff, ← sub_mul_action.coe_smul, set_like.coe_eq_coe] end end mul_action_monoid section mul_action_group variables [group R] [mul_action R M] /-- Stabilizers in group sub_mul_action coincide with stabilizers in the ambient space -/ lemma stabilizer_of_sub_mul {p : sub_mul_action R M} (m : p) : mul_action.stabilizer R m = mul_action.stabilizer R (m : M) := begin rw ← subgroup.to_submonoid_eq, exact stabilizer_of_sub_mul.submonoid m, end end mul_action_group section module variables [semiring R] [add_comm_monoid M] variables [module R M] variables (p : sub_mul_action R M) lemma zero_mem (h : (p : set M).nonempty) : (0 : M) ∈ p := let ⟨x, hx⟩ := h in zero_smul R (x : M) ▸ p.smul_mem 0 hx /-- If the scalar product forms a `module`, and the `sub_mul_action` is not `⊥`, then the subset inherits the zero. -/ instance [n_empty : nonempty p] : has_zero p := { zero := ⟨0, n_empty.elim $ λ x, p.zero_mem ⟨x, x.prop⟩⟩ } end module section add_comm_group variables [ring R] [add_comm_group M] variables [module R M] variables (p p' : sub_mul_action R M) variables {r : R} {x y : M} lemma neg_mem (hx : x ∈ p) : -x ∈ p := by { rw ← neg_one_smul R, exact p.smul_mem _ hx } @[simp] lemma neg_mem_iff : -x ∈ p ↔ x ∈ p := ⟨λ h, by { rw ←neg_neg x, exact neg_mem _ h}, neg_mem _⟩ instance : has_neg p := ⟨λx, ⟨-x.1, neg_mem _ x.2⟩⟩ @[simp, norm_cast] lemma coe_neg (x : p) : ((-x : p) : M) = -x := rfl end add_comm_group end sub_mul_action namespace sub_mul_action variables [group_with_zero S] [monoid R] [mul_action R M] variables [has_smul S R] [mul_action S M] [is_scalar_tower S R M] variables (p : sub_mul_action R M) {s : S} {x y : M} theorem smul_mem_iff (s0 : s ≠ 0) : s • x ∈ p ↔ x ∈ p := p.smul_mem_iff' (units.mk0 s s0) end sub_mul_action
150e0f45cd362fc2e638a069deba959d8ecdc3a0
ad3e8f15221a986da27c99f371922c0b3f5792b6
/src/week07/solutions/e02_tactics.lean
83dd731a5e59792943590d63732b4c8ea3549e0b
[]
no_license
VArtem/lean-itmo
a0e1424c8cc4c2de2ac85ab6fd4a12d80e9b85f1
dc44cd06f9f5b984d051831b3aaa7364e64c2dc4
refs/heads/main
1,683,761,214,467
1,622,821,295,000
1,622,821,295,000
357,236,048
12
0
null
null
null
null
UTF-8
Lean
false
false
2,865
lean
import tactic -- Если вы не работали с монадами и do-блоками раньше, почитайте туториал в интернете -- `tactic α` - функция, работающие в контексте состояния тактики и возвращает α -- `tactic unit` - ничего не возвращает (или же возвращает `()`) open tactic meta def make_nat : tactic ℕ := return 42 -- Как использовать результат работы других тактик внутри do-блока? -- n ← make_nat meta def trace_nat : tactic unit := do n ← make_nat, tactic.trace n -- Как дебагать тактики? example : false := begin trace_nat, sorry, end run_cmd trace_nat -- Как работать с окружением в тактике? -- Для первой цели есть функция tactic.target #check tactic.target meta def show_goal : tactic unit := do t ← target, trace t -- trace $ expr.to_raw_fmt t покажет полную структуру, не pretty printed example (a b : ℤ) : a^2 + b^2 ≥ 0 := begin show_goal, sorry, end -- Для локальных гипотез: функции get_local и local_context #check get_local #check tactic.local_context #check infer_type meta def inspect_local_one (nm : name) : tactic unit := do a ← get_local nm, trace a, trace (expr.to_raw_fmt a), a_type ← infer_type a, trace a_type, trace (expr.to_raw_fmt a_type) example (A : Type) (b c : A) (h : b = c) : false := begin inspect_local_one `A, inspect_local_one `h, sorry, end -- Для монадического программирования есть много (хоть и не так богато, как в Haskell) функций #check list.mmap meta def inspect_all : tactic unit := do ctx ← local_context, trace ctx, ctx_types ← list.mmap (infer_type) ctx, trace ctx_types, ctx.mmap' (λ e, do e_type ← infer_type e, trace $ (to_string e) ++ " : " ++ (to_string e_type)) example (A : Type) (b c : A) (h : b = c) : false := begin inspect_all, sorry, end -- Наконец, реализуем версию тактики `assumption` meta def assump_one (e : expr) : tactic unit := do tactic.exact e meta def assump_list : list expr → tactic unit | [] := fail "No assumption found!" | (hd :: tl) := exact hd <|> assump_list tl meta def assump : tactic unit := do -- fail "TODO: implement" ctx ← local_context, assump_list ctx -- Тест example {A B C : Prop} (ha : A) (hb : B) (hc : C) : B := begin assump, -- sorry, end -- Больше упражнений: https://github.com/leanprover-community/lftcm2020/blob/master/src/exercises_sources/monday/metaprogramming.lean
2e670a832ce57ee853f8a8b14580eb8f1e09bedf
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/compiler/rbmap_library.lean
1577cab6b7429a9eabb7dee1ef2da038ed3273c1
[ "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,300
lean
import Lean.Data.RBMap open Lean def check (b : Bool) : IO Unit := do unless b do IO.println "ERROR" def sz {α β : Type} {cmp : α → α → Ordering} (m : RBMap α β cmp) : Nat := m.fold (fun sz _ _ => sz+1) 0 def depth {α β : Type} {cmp : α → α → Ordering} (m : RBMap α β cmp) : Nat := m.depth Nat.max def tst1 : IO Unit := do let Map := RBMap String Nat compare let m : Map := {} let m := m.insert "hello" 0 let m := m.insert "world" 1 check (m.find? "hello" == some 0) check (m.find? "world" == some 1) let m := m.erase "hello" check (m.find? "hello" == none) check (m.find? "world" == some 1) pure () def tst2 : IO Unit := do let Map := RBMap Nat Nat compare let m : Map := {} let n : Nat := 10000 let mut m := n.fold (fun i (m : Map) => m.insert i (i*10)) m check (m.all (fun k v => v == k*10)) check (sz m == n) IO.println (">> " ++ toString (depth m) ++ ", " ++ toString (sz m)) for i in [:n/2] do m := m.erase (2*i) check (m.all (fun k v => v == k*10)) check (sz m == n / 2) IO.println (">> " ++ toString (depth m) ++ ", " ++ toString (sz m)) pure () abbrev Map := RBMap Nat Nat compare def mkRandMap (max : Nat) : Nat → Map → Array (Nat × Nat) → IO (Map × Array (Nat × Nat)) | 0, m, a => pure (m, a) | n+1, m, a => do let k ← IO.rand 0 max let v ← IO.rand 0 max if m.find? k == none then do let m := m.insert k v let a := a.push (k, v) mkRandMap max n m a else mkRandMap max n m a def tst3 (seed : Nat) (n : Nat) (max : Nat) : IO Unit := do IO.setRandSeed seed let mut (m, a) ← mkRandMap max n {} Array.empty check (sz m == a.size) check (a.all (fun ⟨k, v⟩ => m.find? k == some v)) IO.println ("tst3 size: " ++ toString a.size) let mut i := 0 for (k, b) in a do if i % 2 == 0 then m := m.erase k i := i + 1 check (sz m == a.size / 2) i := 0 for (k, v) in a do if i % 2 == 1 then check (m.find? k == some v) i := i + 1 IO.println ("tst3 after, depth: " ++ toString (depth m) ++ ", size: " ++ toString (sz m)) pure () def main (xs : List String) : IO Unit := tst1 *> tst2 *> tst3 1 1000 20000 *> tst3 2 1000 40000 *> tst3 3 100 4000 *> tst3 4 5000 100000 *> tst3 5 1000 40000 *> pure ()
dca51591bb6efaaec67f130cb8bcd0c51623becd
2a70b774d16dbdf5a533432ee0ebab6838df0948
/_target/deps/mathlib/src/ring_theory/int/basic.lean
17ed9f5c0b190e5b816c71b1bf769aca492d57b2
[ "Apache-2.0" ]
permissive
hjvromen/lewis
40b035973df7c77ebf927afab7878c76d05ff758
105b675f73630f028ad5d890897a51b3c1146fb0
refs/heads/master
1,677,944,636,343
1,676,555,301,000
1,676,555,301,000
327,553,599
0
0
null
null
null
null
UTF-8
Lean
false
false
12,375
lean
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Jens Wagemaker, Aaron Anderson -/ import data.int.gcd import ring_theory.multiplicity import ring_theory.principal_ideal_domain /-! # Divisibility over ℕ and ℤ This file collects results for the integers and natural numbers that use abstract algebra in their proofs or cases of ℕ and ℤ being examples of structures in abstract algebra. ## Main statements * `nat.prime_iff`: `nat.prime` coincides with the general definition of `prime` * `nat.irreducible_iff_prime`: a non-unit natural number is only divisible by `1` iff it is prime * `nat.factors_eq`: the multiset of elements of `nat.factors` is equal to the factors given by the `unique_factorization_monoid` instance * ℤ is a `normalization_monoid` * ℤ is a `gcd_monoid` ## Tags prime, irreducible, natural numbers, integers, normalization monoid, gcd monoid, greatest common divisor, prime factorization, prime factors, unique factorization, unique factors -/ theorem nat.prime_iff {p : ℕ} : p.prime ↔ prime p := begin split; intro h, { refine ⟨h.ne_zero, ⟨_, λ a b, _⟩⟩, { rw nat.is_unit_iff, apply h.ne_one }, { apply h.dvd_mul.1 } }, { refine ⟨_, λ m hm, _⟩, { cases p, { exfalso, apply h.ne_zero rfl }, cases p, { exfalso, apply h.ne_one rfl }, omega }, { cases hm with n hn, cases h.2.2 m n (hn ▸ dvd_refl _) with hpm hpn, { right, apply nat.dvd_antisymm (dvd.intro _ hn.symm) hpm }, { left, cases n, { exfalso, rw [hn, mul_zero] at h, apply h.ne_zero rfl }, apply nat.eq_of_mul_eq_mul_right (nat.succ_pos _), rw [← hn, one_mul], apply nat.dvd_antisymm hpn (dvd.intro m _), rw [mul_comm, hn], }, } } end theorem nat.irreducible_iff_prime {p : ℕ} : irreducible p ↔ prime p := begin refine ⟨λ h, _, irreducible_of_prime⟩, rw ← nat.prime_iff, refine ⟨_, λ m hm, _⟩, { cases p, { exfalso, apply h.ne_zero rfl }, cases p, { exfalso, apply h.1 is_unit_one, }, omega }, { cases hm with n hn, cases h.2 m n hn with um un, { left, rw nat.is_unit_iff.1 um, }, { right, rw [hn, nat.is_unit_iff.1 un, mul_one], } } end namespace nat instance : wf_dvd_monoid ℕ := ⟨begin apply rel_hom.well_founded _ (with_top.well_founded_lt nat.lt_wf), refine ⟨λ x, if x = 0 then ⊤ else x, _⟩, intros a b h, cases a, { exfalso, revert h, simp [dvd_not_unit] }, cases b, {simp [succ_ne_zero, with_top.coe_lt_top]}, cases dvd_and_not_dvd_iff.2 h with h1 h2, simp only [succ_ne_zero, with_top.coe_lt_coe, if_false], apply lt_of_le_of_ne (nat.le_of_dvd (nat.succ_pos _) h1) (λ con, h2 _), rw con, end⟩ instance : unique_factorization_monoid ℕ := ⟨λ _, nat.irreducible_iff_prime⟩ end nat namespace int section normalization_monoid instance : normalization_monoid ℤ := { norm_unit := λa:ℤ, if 0 ≤ a then 1 else -1, norm_unit_zero := if_pos (le_refl _), norm_unit_mul := assume a b hna hnb, begin cases hna.lt_or_lt with ha ha; cases hnb.lt_or_lt with hb hb; simp [mul_nonneg_iff, ha.le, ha.not_le, hb.le, hb.not_le] end, norm_unit_coe_units := assume u, (units_eq_one_or u).elim (assume eq, eq.symm ▸ if_pos zero_le_one) (assume eq, eq.symm ▸ if_neg (not_le_of_gt $ show (-1:ℤ) < 0, by dec_trivial)), } lemma normalize_of_nonneg {z : ℤ} (h : 0 ≤ z) : normalize z = z := show z * ↑(ite _ _ _) = z, by rw [if_pos h, units.coe_one, mul_one] lemma normalize_of_neg {z : ℤ} (h : z < 0) : normalize z = -z := show z * ↑(ite _ _ _) = -z, by rw [if_neg (not_le_of_gt h), units.coe_neg, units.coe_one, mul_neg_one] lemma normalize_coe_nat (n : ℕ) : normalize (n : ℤ) = n := normalize_of_nonneg (coe_nat_le_coe_nat_of_le $ nat.zero_le n) theorem coe_nat_abs_eq_normalize (z : ℤ) : (z.nat_abs : ℤ) = normalize z := begin by_cases 0 ≤ z, { simp [nat_abs_of_nonneg h, normalize_of_nonneg h] }, { simp [of_nat_nat_abs_of_nonpos (le_of_not_ge h), normalize_of_neg (lt_of_not_ge h)] } end end normalization_monoid section gcd_monoid instance : gcd_monoid ℤ := { gcd := λa b, int.gcd a b, lcm := λa b, int.lcm a b, gcd_dvd_left := assume a b, int.gcd_dvd_left _ _, gcd_dvd_right := assume a b, int.gcd_dvd_right _ _, dvd_gcd := assume a b c, dvd_gcd, normalize_gcd := assume a b, normalize_coe_nat _, gcd_mul_lcm := by intros; rw [← int.coe_nat_mul, gcd_mul_lcm, coe_nat_abs_eq_normalize], lcm_zero_left := assume a, coe_nat_eq_zero.2 $ nat.lcm_zero_left _, lcm_zero_right := assume a, coe_nat_eq_zero.2 $ nat.lcm_zero_right _, .. int.normalization_monoid } lemma coe_gcd (i j : ℤ) : ↑(int.gcd i j) = gcd_monoid.gcd i j := rfl lemma coe_lcm (i j : ℤ) : ↑(int.lcm i j) = gcd_monoid.lcm i j := rfl lemma nat_abs_gcd (i j : ℤ) : nat_abs (gcd_monoid.gcd i j) = int.gcd i j := rfl lemma nat_abs_lcm (i j : ℤ) : nat_abs (gcd_monoid.lcm i j) = int.lcm i j := rfl end gcd_monoid lemma exists_unit_of_abs (a : ℤ) : ∃ (u : ℤ) (h : is_unit u), (int.nat_abs a : ℤ) = u * a := begin cases (nat_abs_eq a) with h, { use [1, is_unit_one], rw [← h, one_mul], }, { use [-1, is_unit_int.mpr rfl], rw [ ← neg_eq_iff_neg_eq.mp (eq.symm h)], simp only [neg_mul_eq_neg_mul_symm, one_mul] } end lemma gcd_eq_one_iff_coprime {a b : ℤ} : int.gcd a b = 1 ↔ is_coprime a b := begin split, { intro hg, obtain ⟨ua, hua, ha⟩ := exists_unit_of_abs a, obtain ⟨ub, hub, hb⟩ := exists_unit_of_abs b, use [(nat.gcd_a (int.nat_abs a) (int.nat_abs b)) * ua, (nat.gcd_b (int.nat_abs a) (int.nat_abs b)) * ub], rw [mul_assoc, ← ha, mul_assoc, ← hb, mul_comm, mul_comm _ (int.nat_abs b : ℤ), ← nat.gcd_eq_gcd_ab], norm_cast, exact hg }, { rintro ⟨r, s, h⟩, by_contradiction hg, obtain ⟨p, ⟨hp, ha, hb⟩⟩ := nat.prime.not_coprime_iff_dvd.mp hg, apply nat.prime.not_dvd_one hp, apply coe_nat_dvd.mp, change (p : ℤ) ∣ 1, rw [← h], exact dvd_add (dvd_mul_of_dvd_right (coe_nat_dvd_left.mpr ha) _) (dvd_mul_of_dvd_right (coe_nat_dvd_left.mpr hb) _), } end lemma sqr_of_gcd_eq_one {a b c : ℤ} (h : int.gcd a b = 1) (heq : a * b = c ^ 2) : ∃ (a0 : ℤ), a = a0 ^ 2 ∨ a = - (a0 ^ 2) := begin have h' : gcd_monoid.gcd a b = 1, { rw [← coe_gcd, h], dec_trivial }, obtain ⟨d, ⟨u, hu⟩⟩ := exists_associated_pow_of_mul_eq_pow h' heq, use d, rw ← hu, cases int.units_eq_one_or u with hu' hu'; { rw hu', simp } end lemma sqr_of_coprime {a b c : ℤ} (h : is_coprime a b) (heq : a * b = c ^ 2) : ∃ (a0 : ℤ), a = a0 ^ 2 ∨ a = - (a0 ^ 2) := sqr_of_gcd_eq_one (gcd_eq_one_iff_coprime.mpr h) heq end int theorem irreducible_iff_nat_prime : ∀(a : ℕ), irreducible a ↔ nat.prime a | 0 := by simp [nat.not_prime_zero] | 1 := by simp [nat.prime, one_lt_two] | (n + 2) := have h₁ : ¬n + 2 = 1, from dec_trivial, begin simp [h₁, nat.prime, irreducible, (≥), nat.le_add_left 2 n, (∣)], refine forall_congr (assume a, forall_congr $ assume b, forall_congr $ assume hab, _), by_cases a = 1; simp [h], split, { assume hb, simpa [hb] using hab.symm }, { assume ha, subst ha, have : n + 2 > 0, from dec_trivial, refine nat.eq_of_mul_eq_mul_left this _, rw [← hab, mul_one] } end lemma nat.prime_iff_prime {p : ℕ} : p.prime ↔ _root_.prime (p : ℕ) := ⟨λ hp, ⟨pos_iff_ne_zero.1 hp.pos, mt is_unit_iff_dvd_one.1 hp.not_dvd_one, λ a b, hp.dvd_mul.1⟩, λ hp, ⟨nat.one_lt_iff_ne_zero_and_ne_one.2 ⟨hp.1, λ h1, hp.2.1 $ h1.symm ▸ is_unit_one⟩, λ a h, let ⟨b, hab⟩ := h in (hp.2.2 a b (hab ▸ dvd_refl _)).elim (λ ha, or.inr (nat.dvd_antisymm h ha)) (λ hb, or.inl (have hpb : p = b, from nat.dvd_antisymm hb (hab.symm ▸ dvd_mul_left _ _), (nat.mul_right_inj (show 0 < p, from nat.pos_of_ne_zero hp.1)).1 $ by rw [hpb, mul_comm, ← hab, hpb, mul_one]))⟩⟩ lemma nat.prime_iff_prime_int {p : ℕ} : p.prime ↔ _root_.prime (p : ℤ) := ⟨λ hp, ⟨int.coe_nat_ne_zero_iff_pos.2 hp.pos, mt is_unit_int.1 hp.ne_one, λ a b h, by rw [← int.dvd_nat_abs, int.coe_nat_dvd, int.nat_abs_mul, hp.dvd_mul] at h; rwa [← int.dvd_nat_abs, int.coe_nat_dvd, ← int.dvd_nat_abs, int.coe_nat_dvd]⟩, λ hp, nat.prime_iff_prime.2 ⟨int.coe_nat_ne_zero.1 hp.1, mt nat.is_unit_iff.1 $ λ h, by simpa [h, not_prime_one] using hp, λ a b, by simpa only [int.coe_nat_dvd, (int.coe_nat_mul _ _).symm] using hp.2.2 a b⟩⟩ /-- Maps an associate class of integers consisting of `-n, n` to `n : ℕ` -/ def associates_int_equiv_nat : associates ℤ ≃ ℕ := begin refine ⟨λz, z.out.nat_abs, λn, associates.mk n, _, _⟩, { refine (assume a, quotient.induction_on' a $ assume a, associates.mk_eq_mk_iff_associated.2 $ associated.symm $ ⟨norm_unit a, _⟩), show normalize a = int.nat_abs (normalize a), rw [int.coe_nat_abs_eq_normalize, normalize_idem] }, { intro n, dsimp, rw [associates.out_mk ↑n, ← int.coe_nat_abs_eq_normalize, int.nat_abs_of_nat, int.nat_abs_of_nat] } end lemma int.prime.dvd_mul {m n : ℤ} {p : ℕ} (hp : nat.prime p) (h : (p : ℤ) ∣ m * n) : p ∣ m.nat_abs ∨ p ∣ n.nat_abs := begin apply (nat.prime.dvd_mul hp).mp, rw ← int.nat_abs_mul, exact int.coe_nat_dvd_left.mp h end lemma int.prime.dvd_mul' {m n : ℤ} {p : ℕ} (hp : nat.prime p) (h : (p : ℤ) ∣ m * n) : (p : ℤ) ∣ m ∨ (p : ℤ) ∣ n := begin rw [int.coe_nat_dvd_left, int.coe_nat_dvd_left], exact int.prime.dvd_mul hp h end lemma int.prime.dvd_pow {n : ℤ} {k p : ℕ} (hp : nat.prime p) (h : (p : ℤ) ∣ n ^ k) : p ∣ n.nat_abs := begin apply @nat.prime.dvd_of_dvd_pow _ _ k hp, rw ← int.nat_abs_pow, exact int.coe_nat_dvd_left.mp h end lemma int.prime.dvd_pow' {n : ℤ} {k p : ℕ} (hp : nat.prime p) (h : (p : ℤ) ∣ n ^ k) : (p : ℤ) ∣ n := begin rw int.coe_nat_dvd_left, exact int.prime.dvd_pow hp h end lemma prime_two_or_dvd_of_dvd_two_mul_pow_self_two {m : ℤ} {p : ℕ} (hp : nat.prime p) (h : (p : ℤ) ∣ 2 * m ^ 2) : p = 2 ∨ p ∣ int.nat_abs m := begin cases int.prime.dvd_mul hp h with hp2 hpp, { apply or.intro_left, exact le_antisymm (nat.le_of_dvd zero_lt_two hp2) (nat.prime.two_le hp) }, { apply or.intro_right, rw [pow_two, int.nat_abs_mul] at hpp, exact (or_self _).mp ((nat.prime.dvd_mul hp).mp hpp)} end instance nat.unique_units : unique (units ℕ) := { default := 1, uniq := nat.units_eq_one } open unique_factorization_monoid theorem nat.factors_eq {n : ℕ} : factors n = n.factors := begin cases n, {refl}, rw [← multiset.rel_eq, ← associated_eq_eq], apply factors_unique (irreducible_of_factor) _, { rw [multiset.coe_prod, nat.prod_factors (nat.succ_pos _)], apply factors_prod (nat.succ_ne_zero _) }, { apply_instance }, { intros x hx, rw [nat.irreducible_iff_prime, ← nat.prime_iff], apply nat.mem_factors hx, } end lemma nat.factors_multiset_prod_of_irreducible {s : multiset ℕ} (h : ∀ (x : ℕ), x ∈ s → irreducible x) : unique_factorization_monoid.factors (s.prod) = s := begin rw [← multiset.rel_eq, ← associated_eq_eq], apply (unique_factorization_monoid.factors_unique irreducible_of_factor h (factors_prod _)), rw [ne.def, multiset.prod_eq_zero_iff], intro con, exact not_irreducible_zero (h 0 con), end namespace multiplicity lemma finite_int_iff_nat_abs_finite {a b : ℤ} : finite a b ↔ finite a.nat_abs b.nat_abs := by simp only [finite_def, ← int.nat_abs_dvd_abs_iff, int.nat_abs_pow] lemma finite_int_iff {a b : ℤ} : finite a b ↔ (a.nat_abs ≠ 1 ∧ b ≠ 0) := begin have := int.nat_abs_eq a, have := @int.nat_abs_ne_zero_of_ne_zero b, rw [finite_int_iff_nat_abs_finite, finite_nat_iff, pos_iff_ne_zero], split; finish end instance decidable_nat : decidable_rel (λ a b : ℕ, (multiplicity a b).dom) := λ a b, decidable_of_iff _ finite_nat_iff.symm instance decidable_int : decidable_rel (λ a b : ℤ, (multiplicity a b).dom) := λ a b, decidable_of_iff _ finite_int_iff.symm end multiplicity
b59023a9dc3596ef399b83b4258c90e0da814110
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/analysis/normed/group/basic.lean
6fe45df80f9c369582cfc766b8fbf55886cd1bc9
[ "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
84,147
lean
/- Copyright (c) 2018 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot, Johannes Hölzl, Yaël Dillies -/ import analysis.normed.group.seminorm import order.liminf_limsup import topology.algebra.uniform_group import topology.instances.rat import topology.metric_space.algebra import topology.metric_space.isometric_smul import topology.sequences /-! # Normed (semi)groups > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file we define 10 classes: * `has_norm`, `has_nnnorm`: auxiliary classes endowing a type `α` with a function `norm : α → ℝ` (notation: `‖x‖`) and `nnnorm : α → ℝ≥0` (notation: `‖x‖₊`), respectively; * `seminormed_..._group`: A seminormed (additive) (commutative) group is an (additive) (commutative) group with a norm and a compatible pseudometric space structure: `∀ x y, dist x y = ‖x / y‖` or `∀ x y, dist x y = ‖x - y‖`, depending on the group operation. * `normed_..._group`: A normed (additive) (commutative) group is an (additive) (commutative) group with a norm and a compatible metric space structure. We also prove basic properties of (semi)normed groups and provide some instances. ## Notes The current convention `dist x y = ‖x - y‖` means that the distance is invariant under right addition, but actions in mathlib are usually from the left. This means we might want to change it to `dist x y = ‖-x + y‖`. The normed group hierarchy would lend itself well to a mixin design (that is, having `seminormed_group` and `seminormed_add_group` not extend `group` and `add_group`), but we choose not to for performance concerns. ## Tags normed group -/ variables {𝓕 𝕜 α ι κ E F G : Type*} open filter function metric open_locale big_operators ennreal filter nnreal uniformity pointwise topology /-- Auxiliary class, endowing a type `E` with a function `norm : E → ℝ` with notation `‖x‖`. This class is designed to be extended in more interesting classes specifying the properties of the norm. -/ @[notation_class] class has_norm (E : Type*) := (norm : E → ℝ) /-- Auxiliary class, endowing a type `α` with a function `nnnorm : α → ℝ≥0` with notation `‖x‖₊`. -/ @[notation_class] class has_nnnorm (E : Type*) := (nnnorm : E → ℝ≥0) export has_norm (norm) export has_nnnorm (nnnorm) notation `‖` e `‖` := norm e notation `‖` e `‖₊` := nnnorm e /-- A seminormed group is an additive group endowed with a norm for which `dist x y = ‖x - y‖` defines a pseudometric space structure. -/ class seminormed_add_group (E : Type*) extends has_norm E, add_group E, pseudo_metric_space E := (dist := λ x y, ‖x - y‖) (dist_eq : ∀ x y, dist x y = ‖x - y‖ . obviously) /-- A seminormed group is a group endowed with a norm for which `dist x y = ‖x / y‖` defines a pseudometric space structure. -/ @[to_additive] class seminormed_group (E : Type*) extends has_norm E, group E, pseudo_metric_space E := (dist := λ x y, ‖x / y‖) (dist_eq : ∀ x y, dist x y = ‖x / y‖ . obviously) /-- A normed group is an additive group endowed with a norm for which `dist x y = ‖x - y‖` defines a metric space structure. -/ class normed_add_group (E : Type*) extends has_norm E, add_group E, metric_space E := (dist := λ x y, ‖x - y‖) (dist_eq : ∀ x y, dist x y = ‖x - y‖ . obviously) /-- A normed group is a group endowed with a norm for which `dist x y = ‖x / y‖` defines a metric space structure. -/ @[to_additive] class normed_group (E : Type*) extends has_norm E, group E, metric_space E := (dist := λ x y, ‖x / y‖) (dist_eq : ∀ x y, dist x y = ‖x / y‖ . obviously) /-- A seminormed group is an additive group endowed with a norm for which `dist x y = ‖x - y‖` defines a pseudometric space structure. -/ class seminormed_add_comm_group (E : Type*) extends has_norm E, add_comm_group E, pseudo_metric_space E := (dist := λ x y, ‖x - y‖) (dist_eq : ∀ x y, dist x y = ‖x - y‖ . obviously) /-- A seminormed group is a group endowed with a norm for which `dist x y = ‖x / y‖` defines a pseudometric space structure. -/ @[to_additive] class seminormed_comm_group (E : Type*) extends has_norm E, comm_group E, pseudo_metric_space E := (dist := λ x y, ‖x / y‖) (dist_eq : ∀ x y, dist x y = ‖x / y‖ . obviously) /-- A normed group is an additive group endowed with a norm for which `dist x y = ‖x - y‖` defines a metric space structure. -/ class normed_add_comm_group (E : Type*) extends has_norm E, add_comm_group E, metric_space E := (dist := λ x y, ‖x - y‖) (dist_eq : ∀ x y, dist x y = ‖x - y‖ . obviously) /-- A normed group is a group endowed with a norm for which `dist x y = ‖x / y‖` defines a metric space structure. -/ @[to_additive] class normed_comm_group (E : Type*) extends has_norm E, comm_group E, metric_space E := (dist := λ x y, ‖x / y‖) (dist_eq : ∀ x y, dist x y = ‖x / y‖ . obviously) @[priority 100, to_additive] -- See note [lower instance priority] instance normed_group.to_seminormed_group [normed_group E] : seminormed_group E := { ..‹normed_group E› } @[priority 100, to_additive] -- See note [lower instance priority] instance normed_comm_group.to_seminormed_comm_group [normed_comm_group E] : seminormed_comm_group E := { ..‹normed_comm_group E› } @[priority 100, to_additive] -- See note [lower instance priority] instance seminormed_comm_group.to_seminormed_group [seminormed_comm_group E] : seminormed_group E := { ..‹seminormed_comm_group E› } @[priority 100, to_additive] -- See note [lower instance priority] instance normed_comm_group.to_normed_group [normed_comm_group E] : normed_group E := { ..‹normed_comm_group E› } /-- Construct a `normed_group` from a `seminormed_group` satisfying `∀ x, ‖x‖ = 0 → x = 1`. This avoids having to go back to the `(pseudo_)metric_space` level when declaring a `normed_group` instance as a special case of a more general `seminormed_group` instance. -/ @[to_additive "Construct a `normed_add_group` from a `seminormed_add_group` satisfying `∀ x, ‖x‖ = 0 → x = 0`. This avoids having to go back to the `(pseudo_)metric_space` level when declaring a `normed_add_group` instance as a special case of a more general `seminormed_add_group` instance.", reducible] -- See note [reducible non-instances] def normed_group.of_separation [seminormed_group E] (h : ∀ x : E, ‖x‖ = 0 → x = 1) : normed_group E := { to_metric_space := { eq_of_dist_eq_zero := λ x y hxy, div_eq_one.1 $ h _ $ by rwa ←‹seminormed_group E›.dist_eq }, ..‹seminormed_group E› } /-- Construct a `normed_comm_group` from a `seminormed_comm_group` satisfying `∀ x, ‖x‖ = 0 → x = 1`. This avoids having to go back to the `(pseudo_)metric_space` level when declaring a `normed_comm_group` instance as a special case of a more general `seminormed_comm_group` instance. -/ @[to_additive "Construct a `normed_add_comm_group` from a `seminormed_add_comm_group` satisfying `∀ x, ‖x‖ = 0 → x = 0`. This avoids having to go back to the `(pseudo_)metric_space` level when declaring a `normed_add_comm_group` instance as a special case of a more general `seminormed_add_comm_group` instance.", reducible] -- See note [reducible non-instances] def normed_comm_group.of_separation [seminormed_comm_group E] (h : ∀ x : E, ‖x‖ = 0 → x = 1) : normed_comm_group E := { ..‹seminormed_comm_group E›, ..normed_group.of_separation h } /-- Construct a seminormed group from a multiplication-invariant distance. -/ @[to_additive "Construct a seminormed group from a translation-invariant distance."] def seminormed_group.of_mul_dist [has_norm E] [group E] [pseudo_metric_space E] (h₁ : ∀ x : E, ‖x‖ = dist x 1) (h₂ : ∀ x y z : E, dist x y ≤ dist (x * z) (y * z)) : seminormed_group E := { dist_eq := λ x y, begin rw h₁, apply le_antisymm, { simpa only [div_eq_mul_inv, ← mul_right_inv y] using h₂ _ _ _ }, { simpa only [div_mul_cancel', one_mul] using h₂ (x/y) 1 y } end } /-- Construct a seminormed group from a multiplication-invariant pseudodistance. -/ @[to_additive "Construct a seminormed group from a translation-invariant pseudodistance."] def seminormed_group.of_mul_dist' [has_norm E] [group E] [pseudo_metric_space E] (h₁ : ∀ x : E, ‖x‖ = dist x 1) (h₂ : ∀ x y z : E, dist (x * z) (y * z) ≤ dist x y) : seminormed_group E := { dist_eq := λ x y, begin rw h₁, apply le_antisymm, { simpa only [div_mul_cancel', one_mul] using h₂ (x/y) 1 y }, { simpa only [div_eq_mul_inv, ← mul_right_inv y] using h₂ _ _ _ } end } /-- Construct a seminormed group from a multiplication-invariant pseudodistance. -/ @[to_additive "Construct a seminormed group from a translation-invariant pseudodistance."] def seminormed_comm_group.of_mul_dist [has_norm E] [comm_group E] [pseudo_metric_space E] (h₁ : ∀ x : E, ‖x‖ = dist x 1) (h₂ : ∀ x y z : E, dist x y ≤ dist (x * z) (y * z)) : seminormed_comm_group E := { ..seminormed_group.of_mul_dist h₁ h₂ } /-- Construct a seminormed group from a multiplication-invariant pseudodistance. -/ @[to_additive "Construct a seminormed group from a translation-invariant pseudodistance."] def seminormed_comm_group.of_mul_dist' [has_norm E] [comm_group E] [pseudo_metric_space E] (h₁ : ∀ x : E, ‖x‖ = dist x 1) (h₂ : ∀ x y z : E, dist (x * z) (y * z) ≤ dist x y) : seminormed_comm_group E := { ..seminormed_group.of_mul_dist' h₁ h₂ } /-- Construct a normed group from a multiplication-invariant distance. -/ @[to_additive "Construct a normed group from a translation-invariant distance."] def normed_group.of_mul_dist [has_norm E] [group E] [metric_space E] (h₁ : ∀ x : E, ‖x‖ = dist x 1) (h₂ : ∀ x y z : E, dist x y ≤ dist (x * z) (y * z)) : normed_group E := { ..seminormed_group.of_mul_dist h₁ h₂ } /-- Construct a normed group from a multiplication-invariant pseudodistance. -/ @[to_additive "Construct a normed group from a translation-invariant pseudodistance."] def normed_group.of_mul_dist' [has_norm E] [group E] [metric_space E] (h₁ : ∀ x : E, ‖x‖ = dist x 1) (h₂ : ∀ x y z : E, dist (x * z) (y * z) ≤ dist x y) : normed_group E := { ..seminormed_group.of_mul_dist' h₁ h₂ } /-- Construct a normed group from a multiplication-invariant pseudodistance. -/ @[to_additive "Construct a normed group from a translation-invariant pseudodistance."] def normed_comm_group.of_mul_dist [has_norm E] [comm_group E] [metric_space E] (h₁ : ∀ x : E, ‖x‖ = dist x 1) (h₂ : ∀ x y z : E, dist x y ≤ dist (x * z) (y * z)) : normed_comm_group E := { ..normed_group.of_mul_dist h₁ h₂ } /-- Construct a normed group from a multiplication-invariant pseudodistance. -/ @[to_additive "Construct a normed group from a translation-invariant pseudodistance."] def normed_comm_group.of_mul_dist' [has_norm E] [comm_group E] [metric_space E] (h₁ : ∀ x : E, ‖x‖ = dist x 1) (h₂ : ∀ x y z : E, dist (x * z) (y * z) ≤ dist x y) : normed_comm_group E := { ..normed_group.of_mul_dist' h₁ h₂ } set_option old_structure_cmd true /-- Construct a seminormed group from a seminorm, i.e., registering the pseudodistance and the pseudometric space structure from the seminorm properties. Note that in most cases this instance creates bad definitional equalities (e.g., it does not take into account a possibly existing `uniform_space` instance on `E`). -/ @[to_additive "Construct a seminormed group from a seminorm, i.e., registering the pseudodistance* and the pseudometric space structure from the seminorm properties. Note that in most cases this instance creates bad definitional equalities (e.g., it does not take into account a possibly existing `uniform_space` instance on `E`)."] def group_seminorm.to_seminormed_group [group E] (f : group_seminorm E) : seminormed_group E := { dist := λ x y, f (x / y), norm := f, dist_eq := λ x y, rfl, dist_self := λ x, by simp only [div_self', map_one_eq_zero], dist_triangle := le_map_div_add_map_div f, dist_comm := map_div_rev f } /-- Construct a seminormed group from a seminorm, i.e., registering the pseudodistance and the pseudometric space structure from the seminorm properties. Note that in most cases this instance creates bad definitional equalities (e.g., it does not take into account a possibly existing `uniform_space` instance on `E`). -/ @[to_additive "Construct a seminormed group from a seminorm, i.e., registering the pseudodistance* and the pseudometric space structure from the seminorm properties. Note that in most cases this instance creates bad definitional equalities (e.g., it does not take into account a possibly existing `uniform_space` instance on `E`)."] def group_seminorm.to_seminormed_comm_group [comm_group E] (f : group_seminorm E) : seminormed_comm_group E := { ..f.to_seminormed_group } /-- Construct a normed group from a norm, i.e., registering the distance and the metric space structure from the norm properties. Note that in most cases this instance creates bad definitional equalities (e.g., it does not take into account a possibly existing `uniform_space` instance on `E`). -/ @[to_additive "Construct a normed group from a norm, i.e., registering the distance and the metric space structure from the norm properties. Note that in most cases this instance creates bad definitional equalities (e.g., it does not take into account a possibly existing `uniform_space` instance on `E`)."] def group_norm.to_normed_group [group E] (f : group_norm E) : normed_group E := { eq_of_dist_eq_zero := λ x y h, div_eq_one.1 $ eq_one_of_map_eq_zero f h, ..f.to_group_seminorm.to_seminormed_group } /-- Construct a normed group from a norm, i.e., registering the distance and the metric space structure from the norm properties. Note that in most cases this instance creates bad definitional equalities (e.g., it does not take into account a possibly existing `uniform_space` instance on `E`). -/ @[to_additive "Construct a normed group from a norm, i.e., registering the distance and the metric space structure from the norm properties. Note that in most cases this instance creates bad definitional equalities (e.g., it does not take into account a possibly existing `uniform_space` instance on `E`)."] def group_norm.to_normed_comm_group [comm_group E] (f : group_norm E) : normed_comm_group E := { ..f.to_normed_group } instance : normed_add_comm_group punit := { norm := function.const _ 0, dist_eq := λ _ _, rfl, } @[simp] lemma punit.norm_eq_zero (r : punit) : ‖r‖ = 0 := rfl section seminormed_group variables [seminormed_group E] [seminormed_group F] [seminormed_group G] {s : set E} {a a₁ a₂ b b₁ b₂ : E} {r r₁ r₂ : ℝ} @[to_additive] lemma dist_eq_norm_div (a b : E) : dist a b = ‖a / b‖ := seminormed_group.dist_eq _ _ @[to_additive] lemma dist_eq_norm_div' (a b : E) : dist a b = ‖b / a‖ := by rw [dist_comm, dist_eq_norm_div] alias dist_eq_norm_sub ← dist_eq_norm alias dist_eq_norm_sub' ← dist_eq_norm' @[to_additive] instance normed_group.to_has_isometric_smul_right : has_isometric_smul Eᵐᵒᵖ E := ⟨λ a, isometry.of_dist_eq $ λ b c, by simp [dist_eq_norm_div]⟩ @[simp, to_additive] lemma dist_one_right (a : E) : dist a 1 = ‖a‖ := by rw [dist_eq_norm_div, div_one] @[simp, to_additive] lemma dist_one_left : dist (1 : E) = norm := funext $ λ a, by rw [dist_comm, dist_one_right] @[to_additive] lemma isometry.norm_map_of_map_one {f : E → F} (hi : isometry f) (h₁ : f 1 = 1) (x : E) : ‖f x‖ = ‖x‖ := by rw [←dist_one_right, ←h₁, hi.dist_eq, dist_one_right] @[to_additive tendsto_norm_cocompact_at_top] lemma tendsto_norm_cocompact_at_top' [proper_space E] : tendsto norm (cocompact E) at_top := by simpa only [dist_one_right] using tendsto_dist_right_cocompact_at_top (1 : E) @[to_additive] lemma norm_div_rev (a b : E) : ‖a / b‖ = ‖b / a‖ := by simpa only [dist_eq_norm_div] using dist_comm a b @[simp, to_additive norm_neg] lemma norm_inv' (a : E) : ‖a⁻¹‖ = ‖a‖ := by simpa using norm_div_rev 1 a @[simp, to_additive] lemma dist_mul_self_right (a b : E) : dist b (a * b) = ‖a‖ := by rw [←dist_one_left, ←dist_mul_right 1 a b, one_mul] @[simp, to_additive] lemma dist_mul_self_left (a b : E) : dist (a * b) b = ‖a‖ := by rw [dist_comm, dist_mul_self_right] @[simp, to_additive] lemma dist_div_eq_dist_mul_left (a b c : E) : dist (a / b) c = dist a (c * b) := by rw [←dist_mul_right _ _ b, div_mul_cancel'] @[simp, to_additive] lemma dist_div_eq_dist_mul_right (a b c : E) : dist a (b / c) = dist (a * c) b := by rw [←dist_mul_right _ _ c, div_mul_cancel'] /-- In a (semi)normed group, inversion `x ↦ x⁻¹` tends to infinity at infinity. TODO: use `bornology.cobounded` instead of `filter.comap has_norm.norm filter.at_top`. -/ @[to_additive "In a (semi)normed group, negation `x ↦ -x` tends to infinity at infinity. TODO: use `bornology.cobounded` instead of `filter.comap has_norm.norm filter.at_top`."] lemma filter.tendsto_inv_cobounded : tendsto (has_inv.inv : E → E) (comap norm at_top) (comap norm at_top) := by simpa only [norm_inv', tendsto_comap_iff, (∘)] using tendsto_comap /-- **Triangle inequality** for the norm. -/ @[to_additive norm_add_le "**Triangle inequality** for the norm."] lemma norm_mul_le' (a b : E) : ‖a * b‖ ≤ ‖a‖ + ‖b‖ := by simpa [dist_eq_norm_div] using dist_triangle a 1 b⁻¹ @[to_additive] lemma norm_mul_le_of_le (h₁ : ‖a₁‖ ≤ r₁) (h₂ : ‖a₂‖ ≤ r₂) : ‖a₁ * a₂‖ ≤ r₁ + r₂ := (norm_mul_le' a₁ a₂).trans $ add_le_add h₁ h₂ @[to_additive norm_add₃_le] lemma norm_mul₃_le (a b c : E) : ‖a * b * c‖ ≤ ‖a‖ + ‖b‖ + ‖c‖ := norm_mul_le_of_le (norm_mul_le' _ _) le_rfl @[simp, to_additive norm_nonneg] lemma norm_nonneg' (a : E) : 0 ≤ ‖a‖ := by { rw [←dist_one_right], exact dist_nonneg } section open tactic tactic.positivity /-- Extension for the `positivity` tactic: norms are nonnegative. -/ @[positivity] meta def _root_.tactic.positivity_norm : expr → tactic strictness | `(‖%%a‖) := nonnegative <$> mk_app ``norm_nonneg [a] <|> nonnegative <$> mk_app ``norm_nonneg' [a] | _ := failed end @[simp, to_additive norm_zero] lemma norm_one' : ‖(1 : E)‖ = 0 := by rw [←dist_one_right, dist_self] @[to_additive] lemma ne_one_of_norm_ne_zero : ‖a‖ ≠ 0 → a ≠ 1 := mt $ by { rintro rfl, exact norm_one' } @[nontriviality, to_additive norm_of_subsingleton] lemma norm_of_subsingleton' [subsingleton E] (a : E) : ‖a‖ = 0 := by rw [subsingleton.elim a 1, norm_one'] attribute [nontriviality] norm_of_subsingleton @[to_additive zero_lt_one_add_norm_sq] lemma zero_lt_one_add_norm_sq' (x : E) : 0 < 1 + ‖x‖^2 := by positivity @[to_additive] lemma norm_div_le (a b : E) : ‖a / b‖ ≤ ‖a‖ + ‖b‖ := by simpa [dist_eq_norm_div] using dist_triangle a 1 b @[to_additive] lemma norm_div_le_of_le {r₁ r₂ : ℝ} (H₁ : ‖a₁‖ ≤ r₁) (H₂ : ‖a₂‖ ≤ r₂) : ‖a₁ / a₂‖ ≤ r₁ + r₂ := (norm_div_le a₁ a₂).trans $ add_le_add H₁ H₂ @[to_additive dist_le_norm_add_norm] lemma dist_le_norm_add_norm' (a b : E) : dist a b ≤ ‖a‖ + ‖b‖ := by { rw dist_eq_norm_div, apply norm_div_le } @[to_additive abs_norm_sub_norm_le] lemma abs_norm_sub_norm_le' (a b : E) : |‖a‖ - ‖b‖| ≤ ‖a / b‖ := by simpa [dist_eq_norm_div] using abs_dist_sub_le a b 1 @[to_additive norm_sub_norm_le] lemma norm_sub_norm_le' (a b : E) : ‖a‖ - ‖b‖ ≤ ‖a / b‖ := (le_abs_self _).trans (abs_norm_sub_norm_le' a b) @[to_additive dist_norm_norm_le] lemma dist_norm_norm_le' (a b : E) : dist ‖a‖ ‖b‖ ≤ ‖a / b‖ := abs_norm_sub_norm_le' a b @[to_additive] lemma norm_le_norm_add_norm_div' (u v : E) : ‖u‖ ≤ ‖v‖ + ‖u / v‖ := by { rw add_comm, refine (norm_mul_le' _ _).trans_eq' _, rw div_mul_cancel' } @[to_additive] lemma norm_le_norm_add_norm_div (u v : E) : ‖v‖ ≤ ‖u‖ + ‖u / v‖ := by { rw norm_div_rev, exact norm_le_norm_add_norm_div' v u } alias norm_le_norm_add_norm_sub' ← norm_le_insert' alias norm_le_norm_add_norm_sub ← norm_le_insert @[to_additive] lemma norm_le_mul_norm_add (u v : E) : ‖u‖ ≤ ‖u * v‖ + ‖v‖ := calc ‖u‖ = ‖u * v / v‖ : by rw mul_div_cancel'' ... ≤ ‖u * v‖ + ‖v‖ : norm_div_le _ _ @[to_additive ball_eq] lemma ball_eq' (y : E) (ε : ℝ) : ball y ε = {x | ‖x / y‖ < ε} := set.ext $ λ a, by simp [dist_eq_norm_div] @[to_additive] lemma ball_one_eq (r : ℝ) : ball (1 : E) r = {x | ‖x‖ < r} := set.ext $ assume a, by simp @[to_additive mem_ball_iff_norm] lemma mem_ball_iff_norm'' : b ∈ ball a r ↔ ‖b / a‖ < r := by rw [mem_ball, dist_eq_norm_div] @[to_additive mem_ball_iff_norm'] lemma mem_ball_iff_norm''' : b ∈ ball a r ↔ ‖a / b‖ < r := by rw [mem_ball', dist_eq_norm_div] @[simp, to_additive] lemma mem_ball_one_iff : a ∈ ball (1 : E) r ↔ ‖a‖ < r := by rw [mem_ball, dist_one_right] @[to_additive mem_closed_ball_iff_norm] lemma mem_closed_ball_iff_norm'' : b ∈ closed_ball a r ↔ ‖b / a‖ ≤ r := by rw [mem_closed_ball, dist_eq_norm_div] @[simp, to_additive] lemma mem_closed_ball_one_iff : a ∈ closed_ball (1 : E) r ↔ ‖a‖ ≤ r := by rw [mem_closed_ball, dist_one_right] @[to_additive mem_closed_ball_iff_norm'] lemma mem_closed_ball_iff_norm''' : b ∈ closed_ball a r ↔ ‖a / b‖ ≤ r := by rw [mem_closed_ball', dist_eq_norm_div] @[to_additive norm_le_of_mem_closed_ball] lemma norm_le_of_mem_closed_ball' (h : b ∈ closed_ball a r) : ‖b‖ ≤ ‖a‖ + r := (norm_le_norm_add_norm_div' _ _).trans $ add_le_add_left (by rwa ←dist_eq_norm_div) _ @[to_additive norm_le_norm_add_const_of_dist_le] lemma norm_le_norm_add_const_of_dist_le' : dist a b ≤ r → ‖a‖ ≤ ‖b‖ + r := norm_le_of_mem_closed_ball' @[to_additive norm_lt_of_mem_ball] lemma norm_lt_of_mem_ball' (h : b ∈ ball a r) : ‖b‖ < ‖a‖ + r := (norm_le_norm_add_norm_div' _ _).trans_lt $ add_lt_add_left (by rwa ←dist_eq_norm_div) _ @[to_additive] lemma norm_div_sub_norm_div_le_norm_div (u v w : E) : ‖u / w‖ - ‖v / w‖ ≤ ‖u / v‖ := by simpa only [div_div_div_cancel_right'] using norm_sub_norm_le' (u / w) (v / w) @[to_additive bounded_iff_forall_norm_le] lemma bounded_iff_forall_norm_le' : bounded s ↔ ∃ C, ∀ x ∈ s, ‖x‖ ≤ C := by simpa only [set.subset_def, mem_closed_ball_one_iff] using bounded_iff_subset_ball (1 : E) alias bounded_iff_forall_norm_le' ↔ metric.bounded.exists_norm_le' _ alias bounded_iff_forall_norm_le ↔ metric.bounded.exists_norm_le _ attribute [to_additive metric.bounded.exists_norm_le] metric.bounded.exists_norm_le' @[to_additive metric.bounded.exists_pos_norm_le] lemma metric.bounded.exists_pos_norm_le' (hs : metric.bounded s) : ∃ R > 0, ∀ x ∈ s, ‖x‖ ≤ R := let ⟨R₀, hR₀⟩ := hs.exists_norm_le' in ⟨max R₀ 1, by positivity, λ x hx, (hR₀ x hx).trans $ le_max_left _ _⟩ @[simp, to_additive mem_sphere_iff_norm] lemma mem_sphere_iff_norm' : b ∈ sphere a r ↔ ‖b / a‖ = r := by simp [dist_eq_norm_div] @[simp, to_additive] lemma mem_sphere_one_iff_norm : a ∈ sphere (1 : E) r ↔ ‖a‖ = r := by simp [dist_eq_norm_div] @[simp, to_additive norm_eq_of_mem_sphere] lemma norm_eq_of_mem_sphere' (x : sphere (1:E) r) : ‖(x : E)‖ = r := mem_sphere_one_iff_norm.mp x.2 @[to_additive] lemma ne_one_of_mem_sphere (hr : r ≠ 0) (x : sphere (1 : E) r) : (x : E) ≠ 1 := ne_one_of_norm_ne_zero $ by rwa norm_eq_of_mem_sphere' x @[to_additive ne_zero_of_mem_unit_sphere] lemma ne_one_of_mem_unit_sphere (x : sphere (1 : E) 1) : (x:E) ≠ 1 := ne_one_of_mem_sphere one_ne_zero _ variables (E) /-- The norm of a seminormed group as a group seminorm. -/ @[to_additive "The norm of a seminormed group as an additive group seminorm."] def norm_group_seminorm : group_seminorm E := ⟨norm, norm_one', norm_mul_le', norm_inv'⟩ @[simp, to_additive] lemma coe_norm_group_seminorm : ⇑(norm_group_seminorm E) = norm := rfl variables {E} @[to_additive] lemma normed_comm_group.tendsto_nhds_one {f : α → E} {l : filter α} : tendsto f l (𝓝 1) ↔ ∀ ε > 0, ∀ᶠ x in l, ‖ f x ‖ < ε := metric.tendsto_nhds.trans $ by simp only [dist_one_right] @[to_additive] lemma normed_comm_group.tendsto_nhds_nhds {f : E → F} {x : E} {y : F} : tendsto f (𝓝 x) (𝓝 y) ↔ ∀ ε > 0, ∃ δ > 0, ∀ x', ‖x' / x‖ < δ → ‖f x' / y‖ < ε := by simp_rw [metric.tendsto_nhds_nhds, dist_eq_norm_div] @[to_additive] lemma normed_comm_group.cauchy_seq_iff [nonempty α] [semilattice_sup α] {u : α → E} : cauchy_seq u ↔ ∀ ε > 0, ∃ N, ∀ m, N ≤ m → ∀ n, N ≤ n → ‖u m / u n‖ < ε := by simp [metric.cauchy_seq_iff, dist_eq_norm_div] @[to_additive] lemma normed_comm_group.nhds_basis_norm_lt (x : E) : (𝓝 x).has_basis (λ ε : ℝ, 0 < ε) (λ ε, {y | ‖y / x‖ < ε}) := by { simp_rw ← ball_eq', exact metric.nhds_basis_ball } @[to_additive] lemma normed_comm_group.nhds_one_basis_norm_lt : (𝓝 (1 : E)).has_basis (λ ε : ℝ, 0 < ε) (λ ε, {y | ‖y‖ < ε}) := by { convert normed_comm_group.nhds_basis_norm_lt (1 : E), simp } @[to_additive] lemma normed_comm_group.uniformity_basis_dist : (𝓤 E).has_basis (λ ε : ℝ, 0 < ε) (λ ε, {p : E × E | ‖p.fst / p.snd‖ < ε}) := by { convert metric.uniformity_basis_dist, simp [dist_eq_norm_div] } open finset /-- A homomorphism `f` of seminormed groups is Lipschitz, if there exists a constant `C` such that for all `x`, one has `‖f x‖ ≤ C * ‖x‖`. The analogous condition for a linear map of (semi)normed spaces is in `normed_space.operator_norm`. -/ @[to_additive "A homomorphism `f` of seminormed groups is Lipschitz, if there exists a constant `C` such that for all `x`, one has `‖f x‖ ≤ C * ‖x‖`. The analogous condition for a linear map of (semi)normed spaces is in `normed_space.operator_norm`."] lemma monoid_hom_class.lipschitz_of_bound [monoid_hom_class 𝓕 E F] (f : 𝓕) (C : ℝ) (h : ∀ x, ‖f x‖ ≤ C * ‖x‖) : lipschitz_with (real.to_nnreal C) f := lipschitz_with.of_dist_le' $ λ x y, by simpa only [dist_eq_norm_div, map_div] using h (x / y) @[to_additive] lemma lipschitz_on_with_iff_norm_div_le {f : E → F} {C : ℝ≥0} : lipschitz_on_with C f s ↔ ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, y ∈ s → ‖f x / f y‖ ≤ C * ‖x / y‖ := by simp only [lipschitz_on_with_iff_dist_le_mul, dist_eq_norm_div] alias lipschitz_on_with_iff_norm_div_le ↔ lipschitz_on_with.norm_div_le _ attribute [to_additive] lipschitz_on_with.norm_div_le @[to_additive] lemma lipschitz_on_with.norm_div_le_of_le {f : E → F} {C : ℝ≥0} (h : lipschitz_on_with C f s) (ha : a ∈ s) (hb : b ∈ s) (hr : ‖a / b‖ ≤ r) : ‖f a / f b‖ ≤ C * r := (h.norm_div_le ha hb).trans $ mul_le_mul_of_nonneg_left hr C.2 @[to_additive] lemma lipschitz_with_iff_norm_div_le {f : E → F} {C : ℝ≥0} : lipschitz_with C f ↔ ∀ x y, ‖f x / f y‖ ≤ C * ‖x / y‖ := by simp only [lipschitz_with_iff_dist_le_mul, dist_eq_norm_div] alias lipschitz_with_iff_norm_div_le ↔ lipschitz_with.norm_div_le _ attribute [to_additive] lipschitz_with.norm_div_le @[to_additive] lemma lipschitz_with.norm_div_le_of_le {f : E → F} {C : ℝ≥0} (h : lipschitz_with C f) (hr : ‖a / b‖ ≤ r) : ‖f a / f b‖ ≤ C * r := (h.norm_div_le _ _).trans $ mul_le_mul_of_nonneg_left hr C.2 /-- A homomorphism `f` of seminormed groups is continuous, if there exists a constant `C` such that for all `x`, one has `‖f x‖ ≤ C * ‖x‖`. -/ @[to_additive "A homomorphism `f` of seminormed groups is continuous, if there exists a constant `C` such that for all `x`, one has `‖f x‖ ≤ C * ‖x‖`"] lemma monoid_hom_class.continuous_of_bound [monoid_hom_class 𝓕 E F] (f : 𝓕) (C : ℝ) (h : ∀ x, ‖f x‖ ≤ C * ‖x‖) : continuous f := (monoid_hom_class.lipschitz_of_bound f C h).continuous @[to_additive] lemma monoid_hom_class.uniform_continuous_of_bound [monoid_hom_class 𝓕 E F] (f : 𝓕) (C : ℝ) (h : ∀x, ‖f x‖ ≤ C * ‖x‖) : uniform_continuous f := (monoid_hom_class.lipschitz_of_bound f C h).uniform_continuous @[to_additive is_compact.exists_bound_of_continuous_on] lemma is_compact.exists_bound_of_continuous_on' [topological_space α] {s : set α} (hs : is_compact s) {f : α → E} (hf : continuous_on f s) : ∃ C, ∀ x ∈ s, ‖f x‖ ≤ C := (bounded_iff_forall_norm_le'.1 (hs.image_of_continuous_on hf).bounded).imp $ λ C hC x hx, hC _ $ set.mem_image_of_mem _ hx @[to_additive] lemma monoid_hom_class.isometry_iff_norm [monoid_hom_class 𝓕 E F] (f : 𝓕) : isometry f ↔ ∀ x, ‖f x‖ = ‖x‖ := begin simp only [isometry_iff_dist_eq, dist_eq_norm_div, ←map_div], refine ⟨λ h x, _, λ h x y, h _⟩, simpa using h x 1, end alias monoid_hom_class.isometry_iff_norm ↔ _ monoid_hom_class.isometry_of_norm attribute [to_additive] monoid_hom_class.isometry_of_norm section nnnorm @[priority 100, to_additive] -- See note [lower instance priority] instance seminormed_group.to_has_nnnorm : has_nnnorm E := ⟨λ a, ⟨‖a‖, norm_nonneg' a⟩⟩ @[simp, norm_cast, to_additive coe_nnnorm] lemma coe_nnnorm' (a : E) : (‖a‖₊ : ℝ) = ‖a‖ := rfl @[simp, to_additive coe_comp_nnnorm] lemma coe_comp_nnnorm' : (coe : ℝ≥0 → ℝ) ∘ (nnnorm : E → ℝ≥0) = norm := rfl @[to_additive norm_to_nnreal] lemma norm_to_nnreal' : ‖a‖.to_nnreal = ‖a‖₊ := @real.to_nnreal_coe ‖a‖₊ @[to_additive] lemma nndist_eq_nnnorm_div (a b : E) : nndist a b = ‖a / b‖₊ := nnreal.eq $ dist_eq_norm_div _ _ alias nndist_eq_nnnorm_sub ← nndist_eq_nnnorm @[simp, to_additive nnnorm_zero] lemma nnnorm_one' : ‖(1 : E)‖₊ = 0 := nnreal.eq norm_one' @[to_additive] lemma ne_one_of_nnnorm_ne_zero {a : E} : ‖a‖₊ ≠ 0 → a ≠ 1 := mt $ by { rintro rfl, exact nnnorm_one' } @[to_additive nnnorm_add_le] lemma nnnorm_mul_le' (a b : E) : ‖a * b‖₊ ≤ ‖a‖₊ + ‖b‖₊ := nnreal.coe_le_coe.1 $ norm_mul_le' a b @[simp, to_additive nnnorm_neg] lemma nnnorm_inv' (a : E) : ‖a⁻¹‖₊ = ‖a‖₊ := nnreal.eq $ norm_inv' a @[to_additive] lemma nnnorm_div_le (a b : E) : ‖a / b‖₊ ≤ ‖a‖₊ + ‖b‖₊ := nnreal.coe_le_coe.1 $ norm_div_le _ _ @[to_additive nndist_nnnorm_nnnorm_le] lemma nndist_nnnorm_nnnorm_le' (a b : E) : nndist ‖a‖₊ ‖b‖₊ ≤ ‖a / b‖₊ := nnreal.coe_le_coe.1 $ dist_norm_norm_le' a b @[to_additive] lemma nnnorm_le_nnnorm_add_nnnorm_div (a b : E) : ‖b‖₊ ≤ ‖a‖₊ + ‖a / b‖₊ := norm_le_norm_add_norm_div _ _ @[to_additive] lemma nnnorm_le_nnnorm_add_nnnorm_div' (a b : E) : ‖a‖₊ ≤ ‖b‖₊ + ‖a / b‖₊ := norm_le_norm_add_norm_div' _ _ alias nnnorm_le_nnnorm_add_nnnorm_sub' ← nnnorm_le_insert' alias nnnorm_le_nnnorm_add_nnnorm_sub ← nnnorm_le_insert @[to_additive] lemma nnnorm_le_mul_nnnorm_add (a b : E) : ‖a‖₊ ≤ ‖a * b‖₊ + ‖b‖₊ := norm_le_mul_norm_add _ _ @[to_additive of_real_norm_eq_coe_nnnorm] lemma of_real_norm_eq_coe_nnnorm' (a : E) : ennreal.of_real ‖a‖ = ‖a‖₊ := ennreal.of_real_eq_coe_nnreal _ @[to_additive] lemma edist_eq_coe_nnnorm_div (a b : E) : edist a b = ‖a / b‖₊ := by rw [edist_dist, dist_eq_norm_div, of_real_norm_eq_coe_nnnorm'] @[to_additive edist_eq_coe_nnnorm] lemma edist_eq_coe_nnnorm' (x : E) : edist x 1 = (‖x‖₊ : ℝ≥0∞) := by rw [edist_eq_coe_nnnorm_div, div_one] @[to_additive] lemma mem_emetric_ball_one_iff {r : ℝ≥0∞} : a ∈ emetric.ball (1 : E) r ↔ ↑‖a‖₊ < r := by rw [emetric.mem_ball, edist_eq_coe_nnnorm'] @[to_additive] lemma monoid_hom_class.lipschitz_of_bound_nnnorm [monoid_hom_class 𝓕 E F] (f : 𝓕) (C : ℝ≥0) (h : ∀ x, ‖f x‖₊ ≤ C * ‖x‖₊) : lipschitz_with C f := @real.to_nnreal_coe C ▸ monoid_hom_class.lipschitz_of_bound f C h @[to_additive] lemma monoid_hom_class.antilipschitz_of_bound [monoid_hom_class 𝓕 E F] (f : 𝓕) {K : ℝ≥0} (h : ∀ x, ‖x‖ ≤ K * ‖f x‖) : antilipschitz_with K f := antilipschitz_with.of_le_mul_dist $ λ x y, by simpa only [dist_eq_norm_div, map_div] using h (x / y) @[to_additive lipschitz_with.norm_le_mul] lemma lipschitz_with.norm_le_mul' {f : E → F} {K : ℝ≥0} (h : lipschitz_with K f) (hf : f 1 = 1) (x) : ‖f x‖ ≤ K * ‖x‖ := by simpa only [dist_one_right, hf] using h.dist_le_mul x 1 @[to_additive lipschitz_with.nnorm_le_mul] lemma lipschitz_with.nnorm_le_mul' {f : E → F} {K : ℝ≥0} (h : lipschitz_with K f) (hf : f 1 = 1) (x) : ‖f x‖₊ ≤ K * ‖x‖₊ := h.norm_le_mul' hf x @[to_additive antilipschitz_with.le_mul_norm] lemma antilipschitz_with.le_mul_norm' {f : E → F} {K : ℝ≥0} (h : antilipschitz_with K f) (hf : f 1 = 1) (x) : ‖x‖ ≤ K * ‖f x‖ := by simpa only [dist_one_right, hf] using h.le_mul_dist x 1 @[to_additive antilipschitz_with.le_mul_nnnorm] lemma antilipschitz_with.le_mul_nnnorm' {f : E → F} {K : ℝ≥0} (h : antilipschitz_with K f) (hf : f 1 = 1) (x) : ‖x‖₊ ≤ K * ‖f x‖₊ := h.le_mul_norm' hf x @[to_additive] lemma one_hom_class.bound_of_antilipschitz [one_hom_class 𝓕 E F] (f : 𝓕) {K : ℝ≥0} (h : antilipschitz_with K f) (x) : ‖x‖ ≤ K * ‖f x‖ := h.le_mul_nnnorm' (map_one f) x end nnnorm @[to_additive] lemma tendsto_iff_norm_tendsto_one {f : α → E} {a : filter α} {b : E} : tendsto f a (𝓝 b) ↔ tendsto (λ e, ‖f e / b‖) a (𝓝 0) := by { convert tendsto_iff_dist_tendsto_zero, simp [dist_eq_norm_div] } @[to_additive] lemma tendsto_one_iff_norm_tendsto_one {f : α → E} {a : filter α} : tendsto f a (𝓝 1) ↔ tendsto (λ e, ‖f e‖) a (𝓝 0) := by { rw tendsto_iff_norm_tendsto_one, simp only [div_one] } @[to_additive] lemma comap_norm_nhds_one : comap norm (𝓝 0) = 𝓝 (1 : E) := by simpa only [dist_one_right] using nhds_comap_dist (1 : E) /-- Special case of the sandwich theorem: if the norm of `f` is eventually bounded by a real function `a` which tends to `0`, then `f` tends to `1`. In this pair of lemmas (`squeeze_one_norm'` and `squeeze_one_norm`), following a convention of similar lemmas in `topology.metric_space.basic` and `topology.algebra.order`, the `'` version is phrased using "eventually" and the non-`'` version is phrased absolutely. -/ @[to_additive "Special case of the sandwich theorem: if the norm of `f` is eventually bounded by a real function `a` which tends to `0`, then `f` tends to `1`. In this pair of lemmas (`squeeze_zero_norm'` and `squeeze_zero_norm`), following a convention of similar lemmas in `topology.metric_space.basic` and `topology.algebra.order`, the `'` version is phrased using \"eventually\" and the non-`'` version is phrased absolutely."] lemma squeeze_one_norm' {f : α → E} {a : α → ℝ} {t₀ : filter α} (h : ∀ᶠ n in t₀, ‖f n‖ ≤ a n) (h' : tendsto a t₀ (𝓝 0)) : tendsto f t₀ (𝓝 1) := tendsto_one_iff_norm_tendsto_one.2 $ squeeze_zero' (eventually_of_forall $ λ n, norm_nonneg' _) h h' /-- Special case of the sandwich theorem: if the norm of `f` is bounded by a real function `a` which tends to `0`, then `f` tends to `1`. -/ @[to_additive "Special case of the sandwich theorem: if the norm of `f` is bounded by a real function `a` which tends to `0`, then `f` tends to `0`."] lemma squeeze_one_norm {f : α → E} {a : α → ℝ} {t₀ : filter α} (h : ∀ n, ‖f n‖ ≤ a n) : tendsto a t₀ (𝓝 0) → tendsto f t₀ (𝓝 1) := squeeze_one_norm' $ eventually_of_forall h @[to_additive] lemma tendsto_norm_div_self (x : E) : tendsto (λ a, ‖a / x‖) (𝓝 x) (𝓝 0) := by simpa [dist_eq_norm_div] using tendsto_id.dist (tendsto_const_nhds : tendsto (λ a, (x:E)) (𝓝 x) _) @[to_additive tendsto_norm]lemma tendsto_norm' {x : E} : tendsto (λ a, ‖a‖) (𝓝 x) (𝓝 ‖x‖) := by simpa using tendsto_id.dist (tendsto_const_nhds : tendsto (λ a, (1:E)) _ _) @[to_additive] lemma tendsto_norm_one : tendsto (λ a : E, ‖a‖) (𝓝 1) (𝓝 0) := by simpa using tendsto_norm_div_self (1:E) @[continuity, to_additive continuous_norm] lemma continuous_norm' : continuous (λ a : E, ‖a‖) := by simpa using continuous_id.dist (continuous_const : continuous (λ a, (1:E))) @[continuity, to_additive continuous_nnnorm] lemma continuous_nnnorm' : continuous (λ a : E, ‖a‖₊) := continuous_norm'.subtype_mk _ @[to_additive lipschitz_with_one_norm] lemma lipschitz_with_one_norm' : lipschitz_with 1 (norm : E → ℝ) := by simpa only [dist_one_left] using lipschitz_with.dist_right (1 : E) @[to_additive lipschitz_with_one_nnnorm] lemma lipschitz_with_one_nnnorm' : lipschitz_with 1 (has_nnnorm.nnnorm : E → ℝ≥0) := lipschitz_with_one_norm' @[to_additive uniform_continuous_norm] lemma uniform_continuous_norm' : uniform_continuous (norm : E → ℝ) := lipschitz_with_one_norm'.uniform_continuous @[to_additive uniform_continuous_nnnorm] lemma uniform_continuous_nnnorm' : uniform_continuous (λ (a : E), ‖a‖₊) := uniform_continuous_norm'.subtype_mk _ @[to_additive] lemma mem_closure_one_iff_norm {x : E} : x ∈ closure ({1} : set E) ↔ ‖x‖ = 0 := by rw [←closed_ball_zero', mem_closed_ball_one_iff, (norm_nonneg' x).le_iff_eq] @[to_additive] lemma closure_one_eq : closure ({1} : set E) = {x | ‖x‖ = 0} := set.ext (λ x, mem_closure_one_iff_norm) /-- A helper lemma used to prove that the (scalar or usual) product of a function that tends to one and a bounded function tends to one. This lemma is formulated for any binary operation `op : E → F → G` with an estimate `‖op x y‖ ≤ A * ‖x‖ * ‖y‖` for some constant A instead of multiplication so that it can be applied to `(*)`, `flip (*)`, `(•)`, and `flip (•)`. -/ @[to_additive "A helper lemma used to prove that the (scalar or usual) product of a function that tends to zero and a bounded function tends to zero. This lemma is formulated for any binary operation `op : E → F → G` with an estimate `‖op x y‖ ≤ A * ‖x‖ * ‖y‖` for some constant A instead of multiplication so that it can be applied to `(*)`, `flip (*)`, `(•)`, and `flip (•)`."] lemma filter.tendsto.op_one_is_bounded_under_le' {f : α → E} {g : α → F} {l : filter α} (hf : tendsto f l (𝓝 1)) (hg : is_bounded_under (≤) l (norm ∘ g)) (op : E → F → G) (h_op : ∃ A, ∀ x y, ‖op x y‖ ≤ A * ‖x‖ * ‖y‖) : tendsto (λ x, op (f x) (g x)) l (𝓝 1) := begin cases h_op with A h_op, rcases hg with ⟨C, hC⟩, rw eventually_map at hC, rw normed_comm_group.tendsto_nhds_one at hf ⊢, intros ε ε₀, rcases exists_pos_mul_lt ε₀ (A * C) with ⟨δ, δ₀, hδ⟩, filter_upwards [hf δ δ₀, hC] with i hf hg, refine (h_op _ _).trans_lt _, cases le_total A 0 with hA hA, { exact (mul_nonpos_of_nonpos_of_nonneg (mul_nonpos_of_nonpos_of_nonneg hA $ norm_nonneg' _) $ norm_nonneg' _).trans_lt ε₀ }, calc A * ‖f i‖ * ‖g i‖ ≤ A * δ * C : mul_le_mul (mul_le_mul_of_nonneg_left hf.le hA) hg (norm_nonneg' _) (mul_nonneg hA δ₀.le) ... = A * C * δ : mul_right_comm _ _ _ ... < ε : hδ, end /-- A helper lemma used to prove that the (scalar or usual) product of a function that tends to one and a bounded function tends to one. This lemma is formulated for any binary operation `op : E → F → G` with an estimate `‖op x y‖ ≤ ‖x‖ * ‖y‖` instead of multiplication so that it can be applied to `(*)`, `flip (*)`, `(•)`, and `flip (•)`. -/ @[to_additive "A helper lemma used to prove that the (scalar or usual) product of a function that tends to zero and a bounded function tends to zero. This lemma is formulated for any binary operation `op : E → F → G` with an estimate `‖op x y‖ ≤ ‖x‖ * ‖y‖` instead of multiplication so that it can be applied to `(*)`, `flip (*)`, `(•)`, and `flip (•)`."] lemma filter.tendsto.op_one_is_bounded_under_le {f : α → E} {g : α → F} {l : filter α} (hf : tendsto f l (𝓝 1)) (hg : is_bounded_under (≤) l (norm ∘ g)) (op : E → F → G) (h_op : ∀ x y, ‖op x y‖ ≤ ‖x‖ * ‖y‖) : tendsto (λ x, op (f x) (g x)) l (𝓝 1) := hf.op_one_is_bounded_under_le' hg op ⟨1, λ x y, (one_mul (‖x‖)).symm ▸ h_op x y⟩ section variables {l : filter α} {f : α → E} @[to_additive filter.tendsto.norm] lemma filter.tendsto.norm' (h : tendsto f l (𝓝 a)) : tendsto (λ x, ‖f x‖) l (𝓝 ‖a‖) := tendsto_norm'.comp h @[to_additive filter.tendsto.nnnorm] lemma filter.tendsto.nnnorm' (h : tendsto f l (𝓝 a)) : tendsto (λ x, ‖f x‖₊) l (𝓝 (‖a‖₊)) := tendsto.comp continuous_nnnorm'.continuous_at h end section variables [topological_space α] {f : α → E} @[to_additive continuous.norm] lemma continuous.norm' : continuous f → continuous (λ x, ‖f x‖) := continuous_norm'.comp @[to_additive continuous.nnnorm] lemma continuous.nnnorm' : continuous f → continuous (λ x, ‖f x‖₊) := continuous_nnnorm'.comp @[to_additive continuous_at.norm] lemma continuous_at.norm' {a : α} (h : continuous_at f a) : continuous_at (λ x, ‖f x‖) a := h.norm' @[to_additive continuous_at.nnnorm] lemma continuous_at.nnnorm' {a : α} (h : continuous_at f a) : continuous_at (λ x, ‖f x‖₊) a := h.nnnorm' @[to_additive continuous_within_at.norm] lemma continuous_within_at.norm' {s : set α} {a : α} (h : continuous_within_at f s a) : continuous_within_at (λ x, ‖f x‖) s a := h.norm' @[to_additive continuous_within_at.nnnorm] lemma continuous_within_at.nnnorm' {s : set α} {a : α} (h : continuous_within_at f s a) : continuous_within_at (λ x, ‖f x‖₊) s a := h.nnnorm' @[to_additive continuous_on.norm] lemma continuous_on.norm' {s : set α} (h : continuous_on f s) : continuous_on (λ x, ‖f x‖) s := λ x hx, (h x hx).norm' @[to_additive continuous_on.nnnorm] lemma continuous_on.nnnorm' {s : set α} (h : continuous_on f s) : continuous_on (λ x, ‖f x‖₊) s := λ x hx, (h x hx).nnnorm' end /-- If `‖y‖ → ∞`, then we can assume `y ≠ x` for any fixed `x`. -/ @[to_additive eventually_ne_of_tendsto_norm_at_top "If `‖y‖→∞`, then we can assume `y≠x` for any fixed `x`"] lemma eventually_ne_of_tendsto_norm_at_top' {l : filter α} {f : α → E} (h : tendsto (λ y, ‖f y‖) l at_top) (x : E) : ∀ᶠ y in l, f y ≠ x := (h.eventually_ne_at_top _).mono $ λ x, ne_of_apply_ne norm @[to_additive] lemma seminormed_comm_group.mem_closure_iff : a ∈ closure s ↔ ∀ ε, 0 < ε → ∃ b ∈ s, ‖a / b‖ < ε := by simp [metric.mem_closure_iff, dist_eq_norm_div] @[to_additive norm_le_zero_iff'] lemma norm_le_zero_iff''' [t0_space E] {a : E} : ‖a‖ ≤ 0 ↔ a = 1 := begin letI : normed_group E := { to_metric_space := metric_space.of_t0_pseudo_metric_space E, ..‹seminormed_group E› }, rw [←dist_one_right, dist_le_zero], end @[to_additive norm_eq_zero'] lemma norm_eq_zero''' [t0_space E] {a : E} : ‖a‖ = 0 ↔ a = 1 := (norm_nonneg' a).le_iff_eq.symm.trans norm_le_zero_iff''' @[to_additive norm_pos_iff'] lemma norm_pos_iff''' [t0_space E] {a : E} : 0 < ‖a‖ ↔ a ≠ 1 := by rw [← not_le, norm_le_zero_iff'''] @[to_additive] lemma seminormed_group.tendsto_uniformly_on_one {f : ι → κ → G} {s : set κ} {l : filter ι} : tendsto_uniformly_on f 1 l s ↔ ∀ ε > 0, ∀ᶠ i in l, ∀ x ∈ s, ‖f i x‖ < ε := by simp_rw [tendsto_uniformly_on_iff, pi.one_apply, dist_one_left] @[to_additive] lemma seminormed_group.uniform_cauchy_seq_on_filter_iff_tendsto_uniformly_on_filter_one {f : ι → κ → G} {l : filter ι} {l' : filter κ} : uniform_cauchy_seq_on_filter f l l' ↔ tendsto_uniformly_on_filter (λ n : ι × ι, λ z, f n.fst z / f n.snd z) 1 (l ×ᶠ l) l' := begin refine ⟨λ hf u hu, _, λ hf u hu, _⟩, { obtain ⟨ε, hε, H⟩ := uniformity_basis_dist.mem_uniformity_iff.mp hu, refine (hf {p : G × G | dist p.fst p.snd < ε} $ dist_mem_uniformity hε).mono (λ x hx, H 1 (f x.fst.fst x.snd / f x.fst.snd x.snd) _), simpa [dist_eq_norm_div, norm_div_rev] using hx }, { obtain ⟨ε, hε, H⟩ := uniformity_basis_dist.mem_uniformity_iff.mp hu, refine (hf {p : G × G | dist p.fst p.snd < ε} $ dist_mem_uniformity hε).mono (λ x hx, H (f x.fst.fst x.snd) (f x.fst.snd x.snd) _), simpa [dist_eq_norm_div, norm_div_rev] using hx } end @[to_additive] lemma seminormed_group.uniform_cauchy_seq_on_iff_tendsto_uniformly_on_one {f : ι → κ → G} {s : set κ} {l : filter ι} : uniform_cauchy_seq_on f l s ↔ tendsto_uniformly_on (λ n : ι × ι, λ z, f n.fst z / f n.snd z) 1 (l ×ᶠ l) s := by rw [tendsto_uniformly_on_iff_tendsto_uniformly_on_filter, uniform_cauchy_seq_on_iff_uniform_cauchy_seq_on_filter, seminormed_group.uniform_cauchy_seq_on_filter_iff_tendsto_uniformly_on_filter_one] end seminormed_group section induced variables (E F) /-- A group homomorphism from a `group` to a `seminormed_group` induces a `seminormed_group` structure on the domain. -/ @[reducible, -- See note [reducible non-instances] to_additive "A group homomorphism from an `add_group` to a `seminormed_add_group` induces a `seminormed_add_group` structure on the domain."] def seminormed_group.induced [group E] [seminormed_group F] [monoid_hom_class 𝓕 E F] (f : 𝓕) : seminormed_group E := { norm := λ x, ‖f x‖, dist_eq := λ x y, by simpa only [map_div, ←dist_eq_norm_div], ..pseudo_metric_space.induced f _ } /-- A group homomorphism from a `comm_group` to a `seminormed_group` induces a `seminormed_comm_group` structure on the domain. -/ @[reducible, -- See note [reducible non-instances] to_additive "A group homomorphism from an `add_comm_group` to a `seminormed_add_group` induces a `seminormed_add_comm_group` structure on the domain."] def seminormed_comm_group.induced [comm_group E] [seminormed_group F] [monoid_hom_class 𝓕 E F] (f : 𝓕) : seminormed_comm_group E := { ..seminormed_group.induced E F f } /-- An injective group homomorphism from a `group` to a `normed_group` induces a `normed_group` structure on the domain. -/ @[reducible, -- See note [reducible non-instances]. to_additive "An injective group homomorphism from an `add_group` to a `normed_add_group` induces a `normed_add_group` structure on the domain."] def normed_group.induced [group E] [normed_group F] [monoid_hom_class 𝓕 E F] (f : 𝓕) (h : injective f) : normed_group E := { ..seminormed_group.induced E F f, ..metric_space.induced f h _ } /-- An injective group homomorphism from an `comm_group` to a `normed_group` induces a `normed_comm_group` structure on the domain. -/ @[reducible, -- See note [reducible non-instances]. to_additive "An injective group homomorphism from an `comm_group` to a `normed_comm_group` induces a `normed_comm_group` structure on the domain."] def normed_comm_group.induced [comm_group E] [normed_group F] [monoid_hom_class 𝓕 E F] (f : 𝓕) (h : injective f) : normed_comm_group E := { ..seminormed_group.induced E F f, ..metric_space.induced f h _ } end induced section seminormed_comm_group variables [seminormed_comm_group E] [seminormed_comm_group F] {a a₁ a₂ b b₁ b₂ : E} {r r₁ r₂ : ℝ} @[to_additive] instance normed_group.to_has_isometric_smul_left : has_isometric_smul E E := ⟨λ a, isometry.of_dist_eq $ λ b c, by simp [dist_eq_norm_div]⟩ @[to_additive] lemma dist_inv (x y : E) : dist x⁻¹ y = dist x y⁻¹ := by simp_rw [dist_eq_norm_div, ←norm_inv' (x⁻¹ / y), inv_div, div_inv_eq_mul, mul_comm] @[simp, to_additive] lemma dist_self_mul_right (a b : E) : dist a (a * b) = ‖b‖ := by rw [←dist_one_left, ←dist_mul_left a 1 b, mul_one] @[simp, to_additive] lemma dist_self_mul_left (a b : E) : dist (a * b) a = ‖b‖ := by rw [dist_comm, dist_self_mul_right] @[simp, to_additive] lemma dist_self_div_right (a b : E) : dist a (a / b) = ‖b‖ := by rw [div_eq_mul_inv, dist_self_mul_right, norm_inv'] @[simp, to_additive] lemma dist_self_div_left (a b : E) : dist (a / b) a = ‖b‖ := by rw [dist_comm, dist_self_div_right] @[to_additive] lemma dist_mul_mul_le (a₁ a₂ b₁ b₂ : E) : dist (a₁ * a₂) (b₁ * b₂) ≤ dist a₁ b₁ + dist a₂ b₂ := by simpa only [dist_mul_left, dist_mul_right] using dist_triangle (a₁ * a₂) (b₁ * a₂) (b₁ * b₂) @[to_additive] lemma dist_mul_mul_le_of_le (h₁ : dist a₁ b₁ ≤ r₁) (h₂ : dist a₂ b₂ ≤ r₂) : dist (a₁ * a₂) (b₁ * b₂) ≤ r₁ + r₂ := (dist_mul_mul_le a₁ a₂ b₁ b₂).trans $ add_le_add h₁ h₂ @[to_additive] lemma dist_div_div_le (a₁ a₂ b₁ b₂ : E) : dist (a₁ / a₂) (b₁ / b₂) ≤ dist a₁ b₁ + dist a₂ b₂ := by simpa only [div_eq_mul_inv, dist_inv_inv] using dist_mul_mul_le a₁ a₂⁻¹ b₁ b₂⁻¹ @[to_additive] lemma dist_div_div_le_of_le (h₁ : dist a₁ b₁ ≤ r₁) (h₂ : dist a₂ b₂ ≤ r₂) : dist (a₁ / a₂) (b₁ / b₂) ≤ r₁ + r₂ := (dist_div_div_le a₁ a₂ b₁ b₂).trans $ add_le_add h₁ h₂ @[to_additive] lemma abs_dist_sub_le_dist_mul_mul (a₁ a₂ b₁ b₂ : E) : |dist a₁ b₁ - dist a₂ b₂| ≤ dist (a₁ * a₂) (b₁ * b₂) := by simpa only [dist_mul_left, dist_mul_right, dist_comm b₂] using abs_dist_sub_le (a₁ * a₂) (b₁ * b₂) (b₁ * a₂) lemma norm_multiset_sum_le {E} [seminormed_add_comm_group E] (m : multiset E) : ‖m.sum‖ ≤ (m.map (λ x, ‖x‖)).sum := m.le_sum_of_subadditive norm norm_zero norm_add_le @[to_additive] lemma norm_multiset_prod_le (m : multiset E) : ‖m.prod‖ ≤ (m.map $ λ x, ‖x‖).sum := begin rw [←multiplicative.of_add_le, of_add_multiset_prod, multiset.map_map], refine multiset.le_prod_of_submultiplicative (multiplicative.of_add ∘ norm) _ (λ x y, _) _, { simp only [comp_app, norm_one', of_add_zero] }, { exact norm_mul_le' _ _ } end lemma norm_sum_le {E} [seminormed_add_comm_group E] (s : finset ι) (f : ι → E) : ‖∑ i in s, f i‖ ≤ ∑ i in s, ‖f i‖ := s.le_sum_of_subadditive norm norm_zero norm_add_le f @[to_additive] lemma norm_prod_le (s : finset ι) (f : ι → E) : ‖∏ i in s, f i‖ ≤ ∑ i in s, ‖f i‖ := begin rw [←multiplicative.of_add_le, of_add_sum], refine finset.le_prod_of_submultiplicative (multiplicative.of_add ∘ norm) _ (λ x y, _) _ _, { simp only [comp_app, norm_one', of_add_zero] }, { exact norm_mul_le' _ _ } end @[to_additive] lemma norm_prod_le_of_le (s : finset ι) {f : ι → E} {n : ι → ℝ} (h : ∀ b ∈ s, ‖f b‖ ≤ n b) : ‖∏ b in s, f b‖ ≤ ∑ b in s, n b := (norm_prod_le s f).trans $ finset.sum_le_sum h @[to_additive] lemma dist_prod_prod_le_of_le (s : finset ι) {f a : ι → E} {d : ι → ℝ} (h : ∀ b ∈ s, dist (f b) (a b) ≤ d b) : dist (∏ b in s, f b) (∏ b in s, a b) ≤ ∑ b in s, d b := by { simp only [dist_eq_norm_div, ← finset.prod_div_distrib] at *, exact norm_prod_le_of_le s h } @[to_additive] lemma dist_prod_prod_le (s : finset ι) (f a : ι → E) : dist (∏ b in s, f b) (∏ b in s, a b) ≤ ∑ b in s, dist (f b) (a b) := dist_prod_prod_le_of_le s $ λ _ _, le_rfl @[to_additive] lemma mul_mem_ball_iff_norm : a * b ∈ ball a r ↔ ‖b‖ < r := by rw [mem_ball_iff_norm'', mul_div_cancel'''] @[to_additive] lemma mul_mem_closed_ball_iff_norm : a * b ∈ closed_ball a r ↔ ‖b‖ ≤ r := by rw [mem_closed_ball_iff_norm'', mul_div_cancel'''] @[simp, to_additive] lemma preimage_mul_ball (a b : E) (r : ℝ) : ((*) b) ⁻¹' ball a r = ball (a / b) r := by { ext c, simp only [dist_eq_norm_div, set.mem_preimage, mem_ball, div_div_eq_mul_div, mul_comm] } @[simp, to_additive] lemma preimage_mul_closed_ball (a b : E) (r : ℝ) : ((*) b) ⁻¹' (closed_ball a r) = closed_ball (a / b) r := by { ext c, simp only [dist_eq_norm_div, set.mem_preimage, mem_closed_ball, div_div_eq_mul_div, mul_comm] } @[simp, to_additive] lemma preimage_mul_sphere (a b : E) (r : ℝ) : ((*) b) ⁻¹' sphere a r = sphere (a / b) r := by { ext c, simp only [set.mem_preimage, mem_sphere_iff_norm', div_div_eq_mul_div, mul_comm] } @[to_additive norm_nsmul_le] lemma norm_pow_le_mul_norm (n : ℕ) (a : E) : ‖a^n‖ ≤ n * ‖a‖ := begin induction n with n ih, { simp, }, simpa only [pow_succ', nat.cast_succ, add_mul, one_mul] using norm_mul_le_of_le ih le_rfl, end @[to_additive nnnorm_nsmul_le] lemma nnnorm_pow_le_mul_norm (n : ℕ) (a : E) : ‖a^n‖₊ ≤ n * ‖a‖₊ := by simpa only [← nnreal.coe_le_coe, nnreal.coe_mul, nnreal.coe_nat_cast] using norm_pow_le_mul_norm n a @[to_additive] lemma pow_mem_closed_ball {n : ℕ} (h : a ∈ closed_ball b r) : a^n ∈ closed_ball (b^n) (n • r) := begin simp only [mem_closed_ball, dist_eq_norm_div, ← div_pow] at h ⊢, refine (norm_pow_le_mul_norm n (a / b)).trans _, simpa only [nsmul_eq_mul] using mul_le_mul_of_nonneg_left h n.cast_nonneg, end @[to_additive] lemma pow_mem_ball {n : ℕ} (hn : 0 < n) (h : a ∈ ball b r) : a^n ∈ ball (b^n) (n • r) := begin simp only [mem_ball, dist_eq_norm_div, ← div_pow] at h ⊢, refine lt_of_le_of_lt (norm_pow_le_mul_norm n (a / b)) _, replace hn : 0 < (n : ℝ), { norm_cast, assumption, }, rw nsmul_eq_mul, nlinarith, end @[simp, to_additive] lemma mul_mem_closed_ball_mul_iff {c : E} : a * c ∈ closed_ball (b * c) r ↔ a ∈ closed_ball b r := by simp only [mem_closed_ball, dist_eq_norm_div, mul_div_mul_right_eq_div] @[simp, to_additive] lemma mul_mem_ball_mul_iff {c : E} : a * c ∈ ball (b * c) r ↔ a ∈ ball b r := by simp only [mem_ball, dist_eq_norm_div, mul_div_mul_right_eq_div] @[to_additive] lemma smul_closed_ball'' : a • closed_ball b r = closed_ball (a • b) r := by { ext, simp [mem_closed_ball, set.mem_smul_set, dist_eq_norm_div, div_eq_inv_mul, ← eq_inv_mul_iff_mul_eq, mul_assoc], } @[to_additive] lemma smul_ball'' : a • ball b r = ball (a • b) r := by { ext, simp [mem_ball, set.mem_smul_set, dist_eq_norm_div, div_eq_inv_mul, ← eq_inv_mul_iff_mul_eq, mul_assoc], } open finset @[to_additive] lemma controlled_prod_of_mem_closure {s : subgroup E} (hg : a ∈ closure (s : set E)) {b : ℕ → ℝ} (b_pos : ∀ n, 0 < b n) : ∃ v : ℕ → E, tendsto (λ n, ∏ i in range (n+1), v i) at_top (𝓝 a) ∧ (∀ n, v n ∈ s) ∧ ‖v 0 / a‖ < b 0 ∧ ∀ n, 0 < n → ‖v n‖ < b n := begin obtain ⟨u : ℕ → E, u_in : ∀ n, u n ∈ s, lim_u : tendsto u at_top (𝓝 a)⟩ := mem_closure_iff_seq_limit.mp hg, obtain ⟨n₀, hn₀⟩ : ∃ n₀, ∀ n ≥ n₀, ‖u n / a‖ < b 0, { have : {x | ‖x / a‖ < b 0} ∈ 𝓝 a, { simp_rw ← dist_eq_norm_div, exact metric.ball_mem_nhds _ (b_pos _) }, exact filter.tendsto_at_top'.mp lim_u _ this }, set z : ℕ → E := λ n, u (n + n₀), have lim_z : tendsto z at_top (𝓝 a) := lim_u.comp (tendsto_add_at_top_nat n₀), have mem_𝓤 : ∀ n, {p : E × E | ‖p.1 / p.2‖ < b (n + 1)} ∈ 𝓤 E := λ n, by simpa [← dist_eq_norm_div] using metric.dist_mem_uniformity (b_pos $ n+1), obtain ⟨φ : ℕ → ℕ, φ_extr : strict_mono φ, hφ : ∀ n, ‖z (φ $ n + 1) / z (φ n)‖ < b (n + 1)⟩ := lim_z.cauchy_seq.subseq_mem mem_𝓤, set w : ℕ → E := z ∘ φ, have hw : tendsto w at_top (𝓝 a), from lim_z.comp φ_extr.tendsto_at_top, set v : ℕ → E := λ i, if i = 0 then w 0 else w i / w (i - 1), refine ⟨v, tendsto.congr (finset.eq_prod_range_div' w) hw , _, hn₀ _ (n₀.le_add_left _), _⟩, { rintro ⟨⟩, { change w 0 ∈ s, apply u_in }, { apply s.div_mem ; apply u_in }, }, { intros l hl, obtain ⟨k, rfl⟩ : ∃ k, l = k+1, exact nat.exists_eq_succ_of_ne_zero hl.ne', apply hφ } end @[to_additive] lemma controlled_prod_of_mem_closure_range {j : E →* F} {b : F} (hb : b ∈ closure (j.range : set F)) {f : ℕ → ℝ} (b_pos : ∀ n, 0 < f n) : ∃ a : ℕ → E, tendsto (λ n, ∏ i in range (n + 1), j (a i)) at_top (𝓝 b) ∧ ‖j (a 0) / b‖ < f 0 ∧ ∀ n, 0 < n → ‖j (a n)‖ < f n := begin obtain ⟨v, sum_v, v_in, hv₀, hv_pos⟩ := controlled_prod_of_mem_closure hb b_pos, choose g hg using v_in, refine ⟨g, by simpa [← hg] using sum_v, by simpa [hg 0] using hv₀, λ n hn, by simpa [hg] using hv_pos n hn⟩, end @[to_additive] lemma nndist_mul_mul_le (a₁ a₂ b₁ b₂ : E) : nndist (a₁ * a₂) (b₁ * b₂) ≤ nndist a₁ b₁ + nndist a₂ b₂ := nnreal.coe_le_coe.1 $ dist_mul_mul_le a₁ a₂ b₁ b₂ @[to_additive] lemma edist_mul_mul_le (a₁ a₂ b₁ b₂ : E) : edist (a₁ * a₂) (b₁ * b₂) ≤ edist a₁ b₁ + edist a₂ b₂ := by { simp only [edist_nndist], norm_cast, apply nndist_mul_mul_le } @[to_additive] lemma nnnorm_multiset_prod_le (m : multiset E) : ‖m.prod‖₊ ≤ (m.map (λ x, ‖x‖₊)).sum := nnreal.coe_le_coe.1 $ by { push_cast, rw multiset.map_map, exact norm_multiset_prod_le _ } @[to_additive] lemma nnnorm_prod_le (s : finset ι) (f : ι → E) : ‖∏ a in s, f a‖₊ ≤ ∑ a in s, ‖f a‖₊ := nnreal.coe_le_coe.1 $ by { push_cast, exact norm_prod_le _ _ } @[to_additive] lemma nnnorm_prod_le_of_le (s : finset ι) {f : ι → E} {n : ι → ℝ≥0} (h : ∀ b ∈ s, ‖f b‖₊ ≤ n b) : ‖∏ b in s, f b‖₊ ≤ ∑ b in s, n b := (norm_prod_le_of_le s h).trans_eq nnreal.coe_sum.symm namespace real instance : has_norm ℝ := { norm := λ r, |r| } @[simp] lemma norm_eq_abs (r : ℝ) : ‖r‖ = |r| := rfl instance : normed_add_comm_group ℝ := ⟨λ r y, rfl⟩ lemma norm_of_nonneg (hr : 0 ≤ r) : ‖r‖ = r := abs_of_nonneg hr lemma norm_of_nonpos (hr : r ≤ 0) : ‖r‖ = -r := abs_of_nonpos hr lemma le_norm_self (r : ℝ) : r ≤ ‖r‖ := le_abs_self r @[simp] lemma norm_coe_nat (n : ℕ) : ‖(n : ℝ)‖ = n := abs_of_nonneg n.cast_nonneg @[simp] lemma nnnorm_coe_nat (n : ℕ) : ‖(n : ℝ)‖₊ = n := nnreal.eq $ norm_coe_nat _ @[simp] lemma norm_two : ‖(2 : ℝ)‖ = 2 := abs_of_pos zero_lt_two @[simp] lemma nnnorm_two : ‖(2 : ℝ)‖₊ = 2 := nnreal.eq $ by simp lemma nnnorm_of_nonneg (hr : 0 ≤ r) : ‖r‖₊ = ⟨r, hr⟩ := nnreal.eq $ norm_of_nonneg hr @[simp] lemma nnnorm_abs (r : ℝ) : ‖(|r|)‖₊ = ‖r‖₊ := by simp [nnnorm] lemma ennnorm_eq_of_real (hr : 0 ≤ r) : (‖r‖₊ : ℝ≥0∞) = ennreal.of_real r := by { rw [← of_real_norm_eq_coe_nnnorm, norm_of_nonneg hr] } lemma ennnorm_eq_of_real_abs (r : ℝ) : (‖r‖₊ : ℝ≥0∞) = ennreal.of_real (|r|) := by rw [← real.nnnorm_abs r, real.ennnorm_eq_of_real (abs_nonneg _)] lemma to_nnreal_eq_nnnorm_of_nonneg (hr : 0 ≤ r) : r.to_nnreal = ‖r‖₊ := begin rw real.to_nnreal_of_nonneg hr, congr, rw [real.norm_eq_abs, abs_of_nonneg hr], end lemma of_real_le_ennnorm (r : ℝ) : ennreal.of_real r ≤ ‖r‖₊ := begin obtain hr | hr := le_total 0 r, { exact (real.ennnorm_eq_of_real hr).ge }, { rw [ennreal.of_real_eq_zero.2 hr], exact bot_le } end end real namespace int instance : normed_add_comm_group ℤ := { norm := λ n, ‖(n : ℝ)‖, dist_eq := λ m n, by simp only [int.dist_eq, norm, int.cast_sub] } @[norm_cast] lemma norm_cast_real (m : ℤ) : ‖(m : ℝ)‖ = ‖m‖ := rfl lemma norm_eq_abs (n : ℤ) : ‖n‖ = |n| := rfl @[simp] lemma norm_coe_nat (n : ℕ) : ‖(n : ℤ)‖ = n := by simp [int.norm_eq_abs] lemma _root_.nnreal.coe_nat_abs (n : ℤ) : (n.nat_abs : ℝ≥0) = ‖n‖₊ := nnreal.eq $ calc ((n.nat_abs : ℝ≥0) : ℝ) = (n.nat_abs : ℤ) : by simp only [int.cast_coe_nat, nnreal.coe_nat_cast] ... = |n| : by simp only [int.coe_nat_abs, int.cast_abs] ... = ‖n‖ : rfl lemma abs_le_floor_nnreal_iff (z : ℤ) (c : ℝ≥0) : |z| ≤ ⌊c⌋₊ ↔ ‖z‖₊ ≤ c := begin rw [int.abs_eq_nat_abs, int.coe_nat_le, nat.le_floor_iff (zero_le c)], congr', exact nnreal.coe_nat_abs z, end end int namespace rat instance : normed_add_comm_group ℚ := { norm := λ r, ‖(r : ℝ)‖, dist_eq := λ r₁ r₂, by simp only [rat.dist_eq, norm, rat.cast_sub] } @[norm_cast, simp] lemma norm_cast_real (r : ℚ) : ‖(r : ℝ)‖ = ‖r‖ := rfl @[norm_cast, simp] lemma _root_.int.norm_cast_rat (m : ℤ) : ‖(m : ℚ)‖ = ‖m‖ := by rw [← rat.norm_cast_real, ← int.norm_cast_real]; congr' 1; norm_cast end rat -- Now that we've installed the norm on `ℤ`, -- we can state some lemmas about `zsmul`. section variables [seminormed_comm_group α] @[to_additive norm_zsmul_le] lemma norm_zpow_le_mul_norm (n : ℤ) (a : α) : ‖a^n‖ ≤ ‖n‖ * ‖a‖ := by rcases n.eq_coe_or_neg with ⟨n, rfl | rfl⟩; simpa using norm_pow_le_mul_norm n a @[to_additive nnnorm_zsmul_le] lemma nnnorm_zpow_le_mul_norm (n : ℤ) (a : α) : ‖a^n‖₊ ≤ ‖n‖₊ * ‖a‖₊ := by simpa only [← nnreal.coe_le_coe, nnreal.coe_mul] using norm_zpow_le_mul_norm n a end namespace lipschitz_with variables [pseudo_emetric_space α] {K Kf Kg : ℝ≥0} {f g : α → E} @[to_additive] lemma inv (hf : lipschitz_with K f) : lipschitz_with K (λ x, (f x)⁻¹) := λ x y, (edist_inv_inv _ _).trans_le $ hf x y @[to_additive add] lemma mul' (hf : lipschitz_with Kf f) (hg : lipschitz_with Kg g) : lipschitz_with (Kf + Kg) (λ x, f x * g x) := λ x y, calc edist (f x * g x) (f y * g y) ≤ edist (f x) (f y) + edist (g x) (g y) : edist_mul_mul_le _ _ _ _ ... ≤ Kf * edist x y + Kg * edist x y : add_le_add (hf x y) (hg x y) ... = (Kf + Kg) * edist x y : (add_mul _ _ _).symm @[to_additive] lemma div (hf : lipschitz_with Kf f) (hg : lipschitz_with Kg g) : lipschitz_with (Kf + Kg) (λ x, f x / g x) := by simpa only [div_eq_mul_inv] using hf.mul' hg.inv end lipschitz_with namespace antilipschitz_with variables [pseudo_emetric_space α] {K Kf Kg : ℝ≥0} {f g : α → E} @[to_additive] lemma mul_lipschitz_with (hf : antilipschitz_with Kf f) (hg : lipschitz_with Kg g) (hK : Kg < Kf⁻¹) : antilipschitz_with (Kf⁻¹ - Kg)⁻¹ (λ x, f x * g x) := begin letI : pseudo_metric_space α := pseudo_emetric_space.to_pseudo_metric_space hf.edist_ne_top, refine antilipschitz_with.of_le_mul_dist (λ x y, _), rw [nnreal.coe_inv, ← div_eq_inv_mul], rw le_div_iff (nnreal.coe_pos.2 $ tsub_pos_iff_lt.2 hK), rw [mul_comm, nnreal.coe_sub hK.le, sub_mul], calc ↑Kf⁻¹ * dist x y - Kg * dist x y ≤ dist (f x) (f y) - dist (g x) (g y) : sub_le_sub (hf.mul_le_dist x y) (hg.dist_le_mul x y) ... ≤ _ : le_trans (le_abs_self _) (abs_dist_sub_le_dist_mul_mul _ _ _ _), end @[to_additive] lemma mul_div_lipschitz_with (hf : antilipschitz_with Kf f) (hg : lipschitz_with Kg (g / f)) (hK : Kg < Kf⁻¹) : antilipschitz_with (Kf⁻¹ - Kg)⁻¹ g := by simpa only [pi.div_apply, mul_div_cancel'_right] using hf.mul_lipschitz_with hg hK @[to_additive le_mul_norm_sub] lemma le_mul_norm_div {f : E → F} (hf : antilipschitz_with K f) (x y : E) : ‖x / y‖ ≤ K * ‖f x / f y‖ := by simp [← dist_eq_norm_div, hf.le_mul_dist x y] end antilipschitz_with @[priority 100, to_additive] -- See note [lower instance priority] instance seminormed_comm_group.to_has_lipschitz_mul : has_lipschitz_mul E := ⟨⟨1 + 1, lipschitz_with.prod_fst.mul' lipschitz_with.prod_snd⟩⟩ /-- A seminormed group is a uniform group, i.e., multiplication and division are uniformly continuous. -/ @[priority 100, to_additive "A seminormed group is a uniform additive group, i.e., addition and subtraction are uniformly continuous."] -- See note [lower instance priority] instance seminormed_comm_group.to_uniform_group : uniform_group E := ⟨(lipschitz_with.prod_fst.div lipschitz_with.prod_snd).uniform_continuous⟩ -- short-circuit type class inference @[priority 100, to_additive] -- See note [lower instance priority] instance seminormed_comm_group.to_topological_group : topological_group E := infer_instance @[to_additive] lemma cauchy_seq_prod_of_eventually_eq {u v : ℕ → E} {N : ℕ} (huv : ∀ n ≥ N, u n = v n) (hv : cauchy_seq (λ n, ∏ k in range (n+1), v k)) : cauchy_seq (λ n, ∏ k in range (n + 1), u k) := begin let d : ℕ → E := λ n, ∏ k in range (n + 1), (u k / v k), rw show (λ n, ∏ k in range (n + 1), u k) = d * (λ n, ∏ k in range (n + 1), v k), by { ext n, simp [d] }, suffices : ∀ n ≥ N, d n = d N, { exact (tendsto_at_top_of_eventually_const this).cauchy_seq.mul hv }, intros n hn, dsimp [d], rw eventually_constant_prod _ hn, intros m hm, simp [huv m hm], end end seminormed_comm_group section normed_group variables [normed_group E] [normed_group F] {a b : E} @[simp, to_additive norm_eq_zero] lemma norm_eq_zero'' : ‖a‖ = 0 ↔ a = 1 := norm_eq_zero''' @[to_additive norm_ne_zero_iff] lemma norm_ne_zero_iff' : ‖a‖ ≠ 0 ↔ a ≠ 1 := norm_eq_zero''.not @[simp, to_additive norm_pos_iff] lemma norm_pos_iff'' : 0 < ‖a‖ ↔ a ≠ 1 := norm_pos_iff''' @[simp, to_additive norm_le_zero_iff] lemma norm_le_zero_iff'' : ‖a‖ ≤ 0 ↔ a = 1 := norm_le_zero_iff''' @[to_additive] lemma norm_div_eq_zero_iff : ‖a / b‖ = 0 ↔ a = b := by rw [norm_eq_zero'', div_eq_one] @[to_additive] lemma norm_div_pos_iff : 0 < ‖a / b‖ ↔ a ≠ b := by { rw [(norm_nonneg' _).lt_iff_ne, ne_comm], exact norm_div_eq_zero_iff.not } @[to_additive] lemma eq_of_norm_div_le_zero (h : ‖a / b‖ ≤ 0) : a = b := by rwa [←div_eq_one, ← norm_le_zero_iff''] alias norm_div_eq_zero_iff ↔ eq_of_norm_div_eq_zero _ attribute [to_additive] eq_of_norm_div_eq_zero @[simp, to_additive nnnorm_eq_zero] lemma nnnorm_eq_zero' : ‖a‖₊ = 0 ↔ a = 1 := by rw [← nnreal.coe_eq_zero, coe_nnnorm', norm_eq_zero''] @[to_additive nnnorm_ne_zero_iff] lemma nnnorm_ne_zero_iff' : ‖a‖₊ ≠ 0 ↔ a ≠ 1 := nnnorm_eq_zero'.not @[to_additive] lemma tendsto_norm_div_self_punctured_nhds (a : E) : tendsto (λ x, ‖x / a‖) (𝓝[≠] a) (𝓝[>] 0) := (tendsto_norm_div_self a).inf $ tendsto_principal_principal.2 $ λ x hx, norm_pos_iff''.2 $ div_ne_one.2 hx @[to_additive] lemma tendsto_norm_nhds_within_one : tendsto (norm : E → ℝ) (𝓝[≠] 1) (𝓝[>] 0) := tendsto_norm_one.inf $ tendsto_principal_principal.2 $ λ x, norm_pos_iff''.2 variables (E) /-- The norm of a normed group as a group norm. -/ @[to_additive "The norm of a normed group as an additive group norm."] def norm_group_norm : group_norm E := { eq_one_of_map_eq_zero' := λ _, norm_eq_zero''.1, ..norm_group_seminorm _ } @[simp] lemma coe_norm_group_norm : ⇑(norm_group_norm E) = norm := rfl end normed_group section normed_add_group variables [normed_add_group E] [topological_space α] {f : α → E} /-! Some relations with `has_compact_support` -/ lemma has_compact_support_norm_iff : has_compact_support (λ x, ‖f x‖) ↔ has_compact_support f := has_compact_support_comp_left $ λ x, norm_eq_zero alias has_compact_support_norm_iff ↔ _ has_compact_support.norm lemma continuous.bounded_above_of_compact_support (hf : continuous f) (h : has_compact_support f) : ∃ C, ∀ x, ‖f x‖ ≤ C := by simpa [bdd_above_def] using hf.norm.bdd_above_range_of_has_compact_support h.norm end normed_add_group section normed_add_group_source variables [normed_add_group α] {f : α → E} @[to_additive] lemma has_compact_mul_support.exists_pos_le_norm [has_one E] (hf : has_compact_mul_support f) : ∃ (R : ℝ), (0 < R) ∧ (∀ (x : α), (R ≤ ‖x‖) → (f x = 1)) := begin obtain ⟨K, ⟨hK1, hK2⟩⟩ := exists_compact_iff_has_compact_mul_support.mpr hf, obtain ⟨S, hS, hS'⟩ := hK1.bounded.exists_pos_norm_le, refine ⟨S + 1, by positivity, λ x hx, hK2 x ((mt $ hS' x) _)⟩, contrapose! hx, exact lt_add_of_le_of_pos hx zero_lt_one end end normed_add_group_source /-! ### `ulift` -/ namespace ulift section has_norm variables [has_norm E] instance : has_norm (ulift E) := ⟨λ x, ‖x.down‖⟩ lemma norm_def (x : ulift E) : ‖x‖ = ‖x.down‖ := rfl @[simp] lemma norm_up (x : E) : ‖ulift.up x‖ = ‖x‖ := rfl @[simp] lemma norm_down (x : ulift E) : ‖x.down‖ = ‖x‖ := rfl end has_norm section has_nnnorm variables [has_nnnorm E] instance : has_nnnorm (ulift E) := ⟨λ x, ‖x.down‖₊⟩ lemma nnnorm_def (x : ulift E) : ‖x‖₊ = ‖x.down‖₊ := rfl @[simp] lemma nnnorm_up (x : E) : ‖ulift.up x‖₊ = ‖x‖₊ := rfl @[simp] lemma nnnorm_down (x : ulift E) : ‖x.down‖₊ = ‖x‖₊ := rfl end has_nnnorm @[to_additive] instance seminormed_group [seminormed_group E] : seminormed_group (ulift E) := seminormed_group.induced _ _ (⟨ulift.down, rfl, λ _ _, rfl⟩ : ulift E →* E) @[to_additive] instance seminormed_comm_group [seminormed_comm_group E] : seminormed_comm_group (ulift E) := seminormed_comm_group.induced _ _ (⟨ulift.down, rfl, λ _ _, rfl⟩ : ulift E →* E) @[to_additive] instance normed_group [normed_group E] : normed_group (ulift E) := normed_group.induced _ _ (⟨ulift.down, rfl, λ _ _, rfl⟩ : ulift E →* E) down_injective @[to_additive] instance normed_comm_group [normed_comm_group E] : normed_comm_group (ulift E) := normed_comm_group.induced _ _ (⟨ulift.down, rfl, λ _ _, rfl⟩ : ulift E →* E) down_injective end ulift /-! ### `additive`, `multiplicative` -/ section additive_multiplicative open additive multiplicative section has_norm variables [has_norm E] instance : has_norm (additive E) := ‹has_norm E› instance : has_norm (multiplicative E) := ‹has_norm E› @[simp] lemma norm_to_mul (x) : ‖(to_mul x : E)‖ = ‖x‖ := rfl @[simp] lemma norm_of_mul (x : E) : ‖of_mul x‖ = ‖x‖ := rfl @[simp] lemma norm_to_add (x) : ‖(to_add x : E)‖ = ‖x‖ := rfl @[simp] lemma norm_of_add (x : E) : ‖of_add x‖ = ‖x‖ := rfl end has_norm section has_nnnorm variables [has_nnnorm E] instance : has_nnnorm (additive E) := ‹has_nnnorm E› instance : has_nnnorm (multiplicative E) := ‹has_nnnorm E› @[simp] lemma nnnorm_to_mul (x) : ‖(to_mul x : E)‖₊ = ‖x‖₊ := rfl @[simp] lemma nnnorm_of_mul (x : E) : ‖of_mul x‖₊ = ‖x‖₊ := rfl @[simp] lemma nnnorm_to_add (x) : ‖(to_add x : E)‖₊ = ‖x‖₊ := rfl @[simp] lemma nnnorm_of_add (x : E) : ‖of_add x‖₊ = ‖x‖₊ := rfl end has_nnnorm instance [seminormed_group E] : seminormed_add_group (additive E) := { dist_eq := dist_eq_norm_div } instance [seminormed_add_group E] : seminormed_group (multiplicative E) := { dist_eq := dist_eq_norm_sub } instance [seminormed_comm_group E] : seminormed_add_comm_group (additive E) := { ..additive.seminormed_add_group } instance [seminormed_add_comm_group E] : seminormed_comm_group (multiplicative E) := { ..multiplicative.seminormed_group } instance [normed_group E] : normed_add_group (additive E) := { ..additive.seminormed_add_group } instance [normed_add_group E] : normed_group (multiplicative E) := { ..multiplicative.seminormed_group } instance [normed_comm_group E] : normed_add_comm_group (additive E) := { ..additive.seminormed_add_group } instance [normed_add_comm_group E] : normed_comm_group (multiplicative E) := { ..multiplicative.seminormed_group } end additive_multiplicative /-! ### Order dual -/ section order_dual open order_dual section has_norm variables [has_norm E] instance : has_norm Eᵒᵈ := ‹has_norm E› @[simp] lemma norm_to_dual (x : E) : ‖to_dual x‖ = ‖x‖ := rfl @[simp] lemma norm_of_dual (x : Eᵒᵈ) : ‖of_dual x‖ = ‖x‖ := rfl end has_norm section has_nnnorm variables [has_nnnorm E] instance : has_nnnorm Eᵒᵈ := ‹has_nnnorm E› @[simp] lemma nnnorm_to_dual (x : E) : ‖to_dual x‖₊ = ‖x‖₊ := rfl @[simp] lemma nnnorm_of_dual (x : Eᵒᵈ) : ‖of_dual x‖₊ = ‖x‖₊ := rfl end has_nnnorm @[priority 100, to_additive] -- See note [lower instance priority] instance [seminormed_group E] : seminormed_group Eᵒᵈ := ‹seminormed_group E› @[priority 100, to_additive] -- See note [lower instance priority] instance [seminormed_comm_group E] : seminormed_comm_group Eᵒᵈ := ‹seminormed_comm_group E› @[priority 100, to_additive] -- See note [lower instance priority] instance [normed_group E] : normed_group Eᵒᵈ := ‹normed_group E› @[priority 100, to_additive] -- See note [lower instance priority] instance [normed_comm_group E] : normed_comm_group Eᵒᵈ := ‹normed_comm_group E› end order_dual /-! ### Binary product of normed groups -/ section has_norm variables [has_norm E] [has_norm F] {x : E × F} {r : ℝ} instance : has_norm (E × F) := ⟨λ x, ‖x.1‖ ⊔ ‖x.2‖⟩ lemma prod.norm_def (x : E × F) : ‖x‖ = (max ‖x.1‖ ‖x.2‖) := rfl lemma norm_fst_le (x : E × F) : ‖x.1‖ ≤ ‖x‖ := le_max_left _ _ lemma norm_snd_le (x : E × F) : ‖x.2‖ ≤ ‖x‖ := le_max_right _ _ lemma norm_prod_le_iff : ‖x‖ ≤ r ↔ ‖x.1‖ ≤ r ∧ ‖x.2‖ ≤ r := max_le_iff end has_norm section seminormed_group variables [seminormed_group E] [seminormed_group F] /-- Product of seminormed groups, using the sup norm. -/ @[to_additive "Product of seminormed groups, using the sup norm."] instance : seminormed_group (E × F) := ⟨λ x y, by simp only [prod.norm_def, prod.dist_eq, dist_eq_norm_div, prod.fst_div, prod.snd_div]⟩ @[to_additive prod.nnnorm_def'] lemma prod.nnorm_def (x : E × F) : ‖x‖₊ = (max ‖x.1‖₊ ‖x.2‖₊) := rfl end seminormed_group /-- Product of seminormed groups, using the sup norm. -/ @[to_additive "Product of seminormed groups, using the sup norm."] instance [seminormed_comm_group E] [seminormed_comm_group F] : seminormed_comm_group (E × F) := { ..prod.seminormed_group } /-- Product of normed groups, using the sup norm. -/ @[to_additive "Product of normed groups, using the sup norm."] instance [normed_group E] [normed_group F] : normed_group (E × F) := { ..prod.seminormed_group } /-- Product of normed groups, using the sup norm. -/ @[to_additive "Product of normed groups, using the sup norm."] instance [normed_comm_group E] [normed_comm_group F] : normed_comm_group (E × F) := { ..prod.seminormed_group } /-! ### Finite product of normed groups -/ section pi variables {π : ι → Type*} [fintype ι] section seminormed_group variables [Π i, seminormed_group (π i)] [seminormed_group E] (f : Π i, π i) {x : Π i, π i} {r : ℝ} /-- Finite product of seminormed groups, using the sup norm. -/ @[to_additive "Finite product of seminormed groups, using the sup norm."] instance : seminormed_group (Π i, π i) := { norm := λ f, ↑(finset.univ.sup (λ b, ‖f b‖₊)), dist_eq := λ x y, congr_arg (coe : ℝ≥0 → ℝ) $ congr_arg (finset.sup finset.univ) $ funext $ λ a, show nndist (x a) (y a) = ‖x a / y a‖₊, from nndist_eq_nnnorm_div (x a) (y a) } @[to_additive pi.norm_def] lemma pi.norm_def' : ‖f‖ = ↑(finset.univ.sup (λ b, ‖f b‖₊)) := rfl @[to_additive pi.nnnorm_def] lemma pi.nnnorm_def' : ‖f‖₊ = finset.univ.sup (λ b, ‖f b‖₊) := subtype.eta _ _ /-- The seminorm of an element in a product space is `≤ r` if and only if the norm of each component is. -/ @[to_additive pi_norm_le_iff_of_nonneg "The seminorm of an element in a product space is `≤ r` if and only if the norm of each component is."] lemma pi_norm_le_iff_of_nonneg' (hr : 0 ≤ r) : ‖x‖ ≤ r ↔ ∀ i, ‖x i‖ ≤ r := by simp only [←dist_one_right, dist_pi_le_iff hr, pi.one_apply] @[to_additive pi_nnnorm_le_iff] lemma pi_nnnorm_le_iff' {r : ℝ≥0} : ‖x‖₊ ≤ r ↔ ∀ i, ‖x i‖₊ ≤ r := pi_norm_le_iff_of_nonneg' r.coe_nonneg @[to_additive pi_norm_le_iff_of_nonempty] lemma pi_norm_le_iff_of_nonempty' [nonempty ι] : ‖f‖ ≤ r ↔ ∀ b, ‖f b‖ ≤ r := begin by_cases hr : 0 ≤ r, { exact pi_norm_le_iff_of_nonneg' hr }, { exact iff_of_false (λ h, hr $ (norm_nonneg' _).trans h) (λ h, hr $ (norm_nonneg' _).trans $ h $ classical.arbitrary _) } end /-- The seminorm of an element in a product space is `< r` if and only if the norm of each component is. -/ @[to_additive pi_norm_lt_iff "The seminorm of an element in a product space is `< r` if and only if the norm of each component is."] lemma pi_norm_lt_iff' (hr : 0 < r) : ‖x‖ < r ↔ ∀ i, ‖x i‖ < r := by simp only [←dist_one_right, dist_pi_lt_iff hr, pi.one_apply] @[to_additive pi_nnnorm_lt_iff] lemma pi_nnnorm_lt_iff' {r : ℝ≥0} (hr : 0 < r) : ‖x‖₊ < r ↔ ∀ i, ‖x i‖₊ < r := pi_norm_lt_iff' hr @[to_additive norm_le_pi_norm] lemma norm_le_pi_norm' (i : ι) : ‖f i‖ ≤ ‖f‖ := (pi_norm_le_iff_of_nonneg' $ norm_nonneg' _).1 le_rfl i @[to_additive nnnorm_le_pi_nnnorm] lemma nnnorm_le_pi_nnnorm' (i : ι) : ‖f i‖₊ ≤ ‖f‖₊ := norm_le_pi_norm' _ i @[to_additive pi_norm_const_le] lemma pi_norm_const_le' (a : E) : ‖(λ _ : ι, a)‖ ≤ ‖a‖ := (pi_norm_le_iff_of_nonneg' $ norm_nonneg' _).2 $ λ _, le_rfl @[to_additive pi_nnnorm_const_le] lemma pi_nnnorm_const_le' (a : E) : ‖(λ _ : ι, a)‖₊ ≤ ‖a‖₊ := pi_norm_const_le' _ @[simp, to_additive pi_norm_const] lemma pi_norm_const' [nonempty ι] (a : E) : ‖(λ i : ι, a)‖ = ‖a‖ := by simpa only [←dist_one_right] using dist_pi_const a 1 @[simp, to_additive pi_nnnorm_const] lemma pi_nnnorm_const' [nonempty ι] (a : E) : ‖(λ i : ι, a)‖₊ = ‖a‖₊ := nnreal.eq $ pi_norm_const' a /-- The $L^1$ norm is less than the $L^\infty$ norm scaled by the cardinality. -/ @[to_additive pi.sum_norm_apply_le_norm "The $L^1$ norm is less than the $L^\\infty$ norm scaled by the cardinality."] lemma pi.sum_norm_apply_le_norm' : ∑ i, ‖f i‖ ≤ fintype.card ι • ‖f‖ := finset.sum_le_card_nsmul _ _ _ $ λ i hi, norm_le_pi_norm' _ i /-- The $L^1$ norm is less than the $L^\infty$ norm scaled by the cardinality. -/ @[to_additive pi.sum_nnnorm_apply_le_nnnorm "The $L^1$ norm is less than the $L^\\infty$ norm scaled by the cardinality."] lemma pi.sum_nnnorm_apply_le_nnnorm' : ∑ i, ‖f i‖₊ ≤ fintype.card ι • ‖f‖₊ := nnreal.coe_sum.trans_le $ pi.sum_norm_apply_le_norm' _ end seminormed_group /-- Finite product of seminormed groups, using the sup norm. -/ @[to_additive "Finite product of seminormed groups, using the sup norm."] instance pi.seminormed_comm_group [Π i, seminormed_comm_group (π i)] : seminormed_comm_group (Π i, π i) := { ..pi.seminormed_group } /-- Finite product of normed groups, using the sup norm. -/ @[to_additive "Finite product of seminormed groups, using the sup norm."] instance pi.normed_group [Π i, normed_group (π i)] : normed_group (Π i, π i) := { ..pi.seminormed_group } /-- Finite product of normed groups, using the sup norm. -/ @[to_additive "Finite product of seminormed groups, using the sup norm."] instance pi.normed_comm_group [Π i, normed_comm_group (π i)] : normed_comm_group (Π i, π i) := { ..pi.seminormed_group } end pi /-! ### Multiplicative opposite -/ namespace mul_opposite /-- The (additive) norm on the multiplicative opposite is the same as the norm on the original type. Note that we do not provide this more generally as `has_norm Eᵐᵒᵖ`, as this is not always a good choice of norm in the multiplicative `seminormed_group E` case. We could repeat this instance to provide a `[seminormed_group E] : seminormed_group Eᵃᵒᵖ` instance, but that case would likely never be used. -/ instance [seminormed_add_group E] : seminormed_add_group Eᵐᵒᵖ := { norm := λ x, ‖x.unop‖, dist_eq := λ _ _, dist_eq_norm _ _, to_pseudo_metric_space := mul_opposite.pseudo_metric_space } lemma norm_op [seminormed_add_group E] (a : E) : ‖mul_opposite.op a‖ = ‖a‖ := rfl lemma norm_unop [seminormed_add_group E] (a : Eᵐᵒᵖ) : ‖mul_opposite.unop a‖ = ‖a‖ := rfl lemma nnnorm_op [seminormed_add_group E] (a : E) : ‖mul_opposite.op a‖₊ = ‖a‖₊ := rfl lemma nnnorm_unop [seminormed_add_group E] (a : Eᵐᵒᵖ) : ‖mul_opposite.unop a‖₊ = ‖a‖₊ := rfl instance [normed_add_group E] : normed_add_group Eᵐᵒᵖ := { .. mul_opposite.seminormed_add_group } instance [seminormed_add_comm_group E] : seminormed_add_comm_group Eᵐᵒᵖ := { dist_eq := λ _ _, dist_eq_norm _ _ } instance [normed_add_comm_group E] : normed_add_comm_group Eᵐᵒᵖ := { .. mul_opposite.seminormed_add_comm_group } end mul_opposite /-! ### Subgroups of normed groups -/ namespace subgroup section seminormed_group variables [seminormed_group E] {s : subgroup E} /-- A subgroup of a seminormed group is also a seminormed group, with the restriction of the norm. -/ @[to_additive "A subgroup of a seminormed group is also a seminormed group, with the restriction of the norm."] instance seminormed_group : seminormed_group s := seminormed_group.induced _ _ s.subtype /-- If `x` is an element of a subgroup `s` of a seminormed group `E`, its norm in `s` is equal to its norm in `E`. -/ @[simp, to_additive "If `x` is an element of a subgroup `s` of a seminormed group `E`, its norm in `s` is equal to its norm in `E`."] lemma coe_norm (x : s) : ‖x‖ = ‖(x : E)‖ := rfl /-- If `x` is an element of a subgroup `s` of a seminormed group `E`, its norm in `s` is equal to its norm in `E`. This is a reversed version of the `simp` lemma `subgroup.coe_norm` for use by `norm_cast`. -/ @[norm_cast, to_additive "If `x` is an element of a subgroup `s` of a seminormed group `E`, its norm in `s` is equal to its norm in `E`. This is a reversed version of the `simp` lemma `add_subgroup.coe_norm` for use by `norm_cast`."] lemma norm_coe {s : subgroup E} (x : s) : ‖(x : E)‖ = ‖x‖ := rfl end seminormed_group @[to_additive] instance seminormed_comm_group [seminormed_comm_group E] {s : subgroup E} : seminormed_comm_group s := seminormed_comm_group.induced _ _ s.subtype @[to_additive] instance normed_group [normed_group E] {s : subgroup E} : normed_group s := normed_group.induced _ _ s.subtype subtype.coe_injective @[to_additive] instance normed_comm_group [normed_comm_group E] {s : subgroup E} : normed_comm_group s := normed_comm_group.induced _ _ s.subtype subtype.coe_injective end subgroup /-! ### Submodules of normed groups -/ namespace submodule /-- A submodule of a seminormed group is also a seminormed group, with the restriction of the norm. -/ -- See note [implicit instance arguments] instance seminormed_add_comm_group {_ : ring 𝕜} [seminormed_add_comm_group E] {_ : module 𝕜 E} (s : submodule 𝕜 E) : seminormed_add_comm_group s := seminormed_add_comm_group.induced _ _ s.subtype.to_add_monoid_hom /-- If `x` is an element of a submodule `s` of a normed group `E`, its norm in `s` is equal to its norm in `E`. -/ -- See note [implicit instance arguments]. @[simp] lemma coe_norm {_ : ring 𝕜} [seminormed_add_comm_group E] {_ : module 𝕜 E} {s : submodule 𝕜 E} (x : s) : ‖x‖ = ‖(x : E)‖ := rfl /-- If `x` is an element of a submodule `s` of a normed group `E`, its norm in `E` is equal to its norm in `s`. This is a reversed version of the `simp` lemma `submodule.coe_norm` for use by `norm_cast`. -/ -- See note [implicit instance arguments]. @[norm_cast] lemma norm_coe {_ : ring 𝕜} [seminormed_add_comm_group E] {_ : module 𝕜 E} {s : submodule 𝕜 E} (x : s) : ‖(x : E)‖ = ‖x‖ := rfl /-- A submodule of a normed group is also a normed group, with the restriction of the norm. -/ -- See note [implicit instance arguments]. instance {_ : ring 𝕜} [normed_add_comm_group E] {_ : module 𝕜 E} (s : submodule 𝕜 E) : normed_add_comm_group s := { ..submodule.seminormed_add_comm_group s } end submodule
385c545f9394492ae296b6539852d598fb08c9b5
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/set_theory/surreal/dyadic.lean
74b9f7128fc4c2948b9ff43e9d8d7be2d9f960f6
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
10,084
lean
/- Copyright (c) 2021 Apurva Nakade. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Apurva Nakade -/ import algebra.algebra.basic import set_theory.game.birthday import set_theory.surreal.basic import ring_theory.localization.basic /-! # Dyadic numbers Dyadic numbers are obtained by localizing ℤ away from 2. They are the initial object in the category of rings with no 2-torsion. ## Dyadic surreal numbers We construct dyadic surreal numbers using the canonical map from ℤ[2 ^ {-1}] to surreals. As we currently do not have a ring structure on `surreal` we construct this map explicitly. Once we have the ring structure, this map can be constructed directly by sending `2 ^ {-1}` to `half`. ## Embeddings The above construction gives us an abelian group embedding of ℤ into `surreal`. The goal is to extend this to an embedding of dyadic rationals into `surreal` and use Cauchy sequences of dyadic rational numbers to construct an ordered field embedding of ℝ into `surreal`. -/ universes u local infix (name := pgame.equiv) ` ≈ ` := pgame.equiv namespace pgame /-- For a natural number `n`, the pre-game `pow_half (n + 1)` is recursively defined as `{0 | pow_half n}`. These are the explicit expressions of powers of `1 / 2`. By definition, we have `pow_half 0 = 1` and `pow_half 1 ≈ 1 / 2` and we prove later on that `pow_half (n + 1) + pow_half (n + 1) ≈ pow_half n`. -/ def pow_half : ℕ → pgame | 0 := 1 | (n + 1) := ⟨punit, punit, 0, λ _, pow_half n⟩ @[simp] lemma pow_half_zero : pow_half 0 = 1 := rfl lemma pow_half_left_moves (n) : (pow_half n).left_moves = punit := by cases n; refl lemma pow_half_zero_right_moves : (pow_half 0).right_moves = pempty := rfl lemma pow_half_succ_right_moves (n) : (pow_half (n + 1)).right_moves = punit := rfl @[simp] lemma pow_half_move_left (n i) : (pow_half n).move_left i = 0 := by cases n; cases i; refl @[simp] lemma pow_half_succ_move_right (n i) : (pow_half (n + 1)).move_right i = pow_half n := rfl instance unique_pow_half_left_moves (n) : unique (pow_half n).left_moves := by cases n; exact punit.unique instance is_empty_pow_half_zero_right_moves : is_empty (pow_half 0).right_moves := pempty.is_empty instance unique_pow_half_succ_right_moves (n) : unique (pow_half (n + 1)).right_moves := punit.unique @[simp] theorem birthday_half : birthday (pow_half 1) = 2 := by { rw birthday_def, dsimp, simpa using order.le_succ (1 : ordinal) } /-- For all natural numbers `n`, the pre-games `pow_half n` are numeric. -/ theorem numeric_pow_half (n) : (pow_half n).numeric := begin induction n with n hn, { exact numeric_one }, { split, { simpa using hn.move_left_lt default }, { exact ⟨λ _, numeric_zero, λ _, hn⟩ } } end theorem pow_half_succ_lt_pow_half (n : ℕ) : pow_half (n + 1) < pow_half n := (numeric_pow_half (n + 1)).lt_move_right default theorem pow_half_succ_le_pow_half (n : ℕ) : pow_half (n + 1) ≤ pow_half n := (pow_half_succ_lt_pow_half n).le theorem pow_half_le_one (n : ℕ) : pow_half n ≤ 1 := begin induction n with n hn, { exact le_rfl }, { exact (pow_half_succ_le_pow_half n).trans hn } end theorem pow_half_succ_lt_one (n : ℕ) : pow_half (n + 1) < 1 := (pow_half_succ_lt_pow_half n).trans_le $ pow_half_le_one n theorem pow_half_pos (n : ℕ) : 0 < pow_half n := by { rw [←lf_iff_lt numeric_zero (numeric_pow_half n), zero_lf_le], simp } theorem zero_le_pow_half (n : ℕ) : 0 ≤ pow_half n := (pow_half_pos n).le theorem add_pow_half_succ_self_eq_pow_half (n) : pow_half (n + 1) + pow_half (n + 1) ≈ pow_half n := begin induction n using nat.strong_induction_on with n hn, { split; rw le_iff_forall_lf; split, { rintro (⟨⟨ ⟩⟩ | ⟨⟨ ⟩⟩); apply lf_of_lt, { calc 0 + pow_half n.succ ≈ pow_half n.succ : zero_add_equiv _ ... < pow_half n : pow_half_succ_lt_pow_half n }, { calc pow_half n.succ + 0 ≈ pow_half n.succ : add_zero_equiv _ ... < pow_half n : pow_half_succ_lt_pow_half n } }, { cases n, { rintro ⟨ ⟩ }, rintro ⟨ ⟩, apply lf_of_move_right_le, swap, exact sum.inl default, calc pow_half n.succ + pow_half (n.succ + 1) ≤ pow_half n.succ + pow_half n.succ : add_le_add_left (pow_half_succ_le_pow_half _) _ ... ≈ pow_half n : hn _ (nat.lt_succ_self n) }, { simp only [pow_half_move_left, forall_const], apply lf_of_lt, calc 0 ≈ 0 + 0 : (add_zero_equiv 0).symm ... ≤ pow_half n.succ + 0 : add_le_add_right (zero_le_pow_half _) _ ... < pow_half n.succ + pow_half n.succ : add_lt_add_left (pow_half_pos _) _ }, { rintro (⟨⟨ ⟩⟩ | ⟨⟨ ⟩⟩); apply lf_of_lt, { calc pow_half n ≈ pow_half n + 0 : (add_zero_equiv _).symm ... < pow_half n + pow_half n.succ : add_lt_add_left (pow_half_pos _) _ }, { calc pow_half n ≈ 0 + pow_half n : (zero_add_equiv _).symm ... < pow_half n.succ + pow_half n : add_lt_add_right (pow_half_pos _) _ } } } end theorem half_add_half_equiv_one : pow_half 1 + pow_half 1 ≈ 1 := add_pow_half_succ_self_eq_pow_half 0 end pgame namespace surreal open pgame /-- Powers of the surreal number `half`. -/ def pow_half (n : ℕ) : surreal := ⟦⟨pgame.pow_half n, pgame.numeric_pow_half n⟩⟧ @[simp] lemma pow_half_zero : pow_half 0 = 1 := rfl @[simp] lemma double_pow_half_succ_eq_pow_half (n : ℕ) : 2 • pow_half n.succ = pow_half n := by { rw two_nsmul, exact quotient.sound (pgame.add_pow_half_succ_self_eq_pow_half n) } @[simp] lemma nsmul_pow_two_pow_half (n : ℕ) : 2 ^ n • pow_half n = 1 := begin induction n with n hn, { simp only [nsmul_one, pow_half_zero, nat.cast_one, pow_zero] }, { rw [← hn, ← double_pow_half_succ_eq_pow_half n, smul_smul (2^n) 2 (pow_half n.succ), mul_comm, pow_succ] } end @[simp] lemma nsmul_pow_two_pow_half' (n k : ℕ) : 2 ^ n • pow_half (n + k) = pow_half k := begin induction k with k hk, { simp only [add_zero, surreal.nsmul_pow_two_pow_half, nat.nat_zero_eq_zero, eq_self_iff_true, surreal.pow_half_zero] }, { rw [← double_pow_half_succ_eq_pow_half (n + k), ← double_pow_half_succ_eq_pow_half k, smul_algebra_smul_comm] at hk, rwa ← zsmul_eq_zsmul_iff' two_ne_zero } end lemma zsmul_pow_two_pow_half (m : ℤ) (n k : ℕ) : (m * 2 ^ n) • pow_half (n + k) = m • pow_half k := begin rw mul_zsmul, congr, norm_cast, exact nsmul_pow_two_pow_half' n k end lemma dyadic_aux {m₁ m₂ : ℤ} {y₁ y₂ : ℕ} (h₂ : m₁ * (2 ^ y₁) = m₂ * (2 ^ y₂)) : m₁ • pow_half y₂ = m₂ • pow_half y₁ := begin revert m₁ m₂, wlog h : y₁ ≤ y₂, intros m₁ m₂ h₂, obtain ⟨c, rfl⟩ := le_iff_exists_add.mp h, rw [add_comm, pow_add, ← mul_assoc, mul_eq_mul_right_iff] at h₂, cases h₂, { rw [h₂, add_comm, zsmul_pow_two_pow_half m₂ c y₁] }, { have := nat.one_le_pow y₁ 2 nat.succ_pos', norm_cast at h₂, linarith }, end /-- The additive monoid morphism `dyadic_map` sends ⟦⟨m, 2^n⟩⟧ to m • half ^ n. -/ def dyadic_map : localization.away (2 : ℤ) →+ surreal := { to_fun := λ x, localization.lift_on x (λ x y, x • pow_half (submonoid.log y)) $ begin intros m₁ m₂ n₁ n₂ h₁, obtain ⟨⟨n₃, y₃, hn₃⟩, h₂⟩ := localization.r_iff_exists.mp h₁, simp only [subtype.coe_mk, mul_eq_mul_right_iff] at h₂, cases h₂, { simp only, obtain ⟨a₁, ha₁⟩ := n₁.prop, obtain ⟨a₂, ha₂⟩ := n₂.prop, have hn₁ : n₁ = submonoid.pow 2 a₁ := subtype.ext ha₁.symm, have hn₂ : n₂ = submonoid.pow 2 a₂ := subtype.ext ha₂.symm, have h₂ : 1 < (2 : ℤ).nat_abs, from one_lt_two, rw [hn₁, hn₂, submonoid.log_pow_int_eq_self h₂, submonoid.log_pow_int_eq_self h₂], apply dyadic_aux, rwa [ha₁, ha₂] }, { have : (1 : ℤ) ≤ 2 ^ y₃ := by exact_mod_cast nat.one_le_pow y₃ 2 nat.succ_pos', linarith } end, map_zero' := localization.lift_on_zero _ _, map_add' := λ x y, localization.induction_on₂ x y $ begin rintro ⟨a, ⟨b, ⟨b', rfl⟩⟩⟩ ⟨c, ⟨d, ⟨d', rfl⟩⟩⟩, have h₂ : 1 < (2 : ℤ).nat_abs, from one_lt_two, have hpow₂ := submonoid.log_pow_int_eq_self h₂, simp_rw submonoid.pow_apply at hpow₂, simp_rw [localization.add_mk, localization.lift_on_mk, subtype.coe_mk, submonoid.log_mul (int.pow_right_injective h₂), hpow₂], calc (2 ^ b' * c + 2 ^ d' * a) • pow_half (b' + d') = (c * 2 ^ b') • pow_half (b' + d') + (a * 2 ^ d') • pow_half (d' + b') : by simp only [add_smul, mul_comm,add_comm] ... = c • pow_half d' + a • pow_half b' : by simp only [zsmul_pow_two_pow_half] ... = a • pow_half b' + c • pow_half d' : add_comm _ _, end } @[simp] lemma dyadic_map_apply (m : ℤ) (p : submonoid.powers (2 : ℤ)) : dyadic_map (is_localization.mk' (localization (submonoid.powers 2)) m p) = m • pow_half (submonoid.log p) := by { rw ← localization.mk_eq_mk', refl } @[simp] lemma dyadic_map_apply_pow (m : ℤ) (n : ℕ) : dyadic_map (is_localization.mk' (localization (submonoid.powers 2)) m (submonoid.pow 2 n)) = m • pow_half n := by rw [dyadic_map_apply, @submonoid.log_pow_int_eq_self 2 one_lt_two] /-- We define dyadic surreals as the range of the map `dyadic_map`. -/ def dyadic : set surreal := set.range dyadic_map -- We conclude with some ideas for further work on surreals; these would make fun projects. -- TODO show that the map from dyadic rationals to surreals is injective -- TODO map the reals into the surreals, using dyadic Dedekind cuts -- TODO show this is a group homomorphism, and injective -- TODO show the maps from the dyadic rationals and from the reals -- into the surreals are multiplicative end surreal
72c2a026a260f8741cf59cce09660b015574471a
ebbdcbd7ddc89a9ef7c3b397b301d5f5272a918f
/qp/p1_categories/c4_topoi.lean
d082ea5373744377bf345de5dfdafee6700b5f9e
[]
no_license
intoverflow/qvr
34b9ef23604738381ca20b7d622fd0399d88f2dd
0cfcd33fe4bf8d93851a00cec5bfd21e77105d74
refs/heads/master
1,616,591,570,371
1,492,575,772,000
1,492,575,772,000
80,061,627
0
0
null
null
null
null
UTF-8
Lean
false
false
55
lean
import .c4_topoi.s1_subobjects import .c4_topoi.s2_nno
bbf685444c9c7ceb2a91dbdf1c585f426efeea00
4727251e0cd73359b15b664c3170e5d754078599
/src/tactic/rcases.lean
e21a0367df6b76aced30ca974fb9cfc74689ed73
[ "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
41,585
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import data.dlist import tactic.core import tactic.clear /-! # Recursive cases (`rcases`) tactic and related tactics `rcases` is a tactic that will perform `cases` recursively, according to a pattern. It is used to destructure hypotheses or expressions composed of inductive types like `h1 : a ∧ b ∧ c ∨ d` or `h2 : ∃ x y, trans_rel R x y`. Usual usage might be `rcases h1 with ⟨ha, hb, hc⟩ | hd` or `rcases h2 with ⟨x, y, _ | ⟨z, hxz, hzy⟩⟩` for these examples. Each element of an `rcases` pattern is matched against a particular local hypothesis (most of which are generated during the execution of `rcases` and represent individual elements destructured from the input expression). An `rcases` pattern has the following grammar: * A name like `x`, which names the active hypothesis as `x`. * A blank `_`, which does nothing (letting the automatic naming system used by `cases` name the hypothesis). * A hyphen `-`, which clears the active hypothesis and any dependents. * The keyword `rfl`, which expects the hypothesis to be `h : a = b`, and calls `subst` on the hypothesis (which has the effect of replacing `b` with `a` everywhere or vice versa). * A type ascription `p : ty`, which sets the type of the hypothesis to `ty` and then matches it against `p`. (Of course, `ty` must unify with the actual type of `h` for this to work.) * A tuple pattern `⟨p1, p2, p3⟩`, which matches a constructor with many arguments, or a series of nested conjunctions or existentials. For example if the active hypothesis is `a ∧ b ∧ c`, then the conjunction will be destructured, and `p1` will be matched against `a`, `p2` against `b` and so on. * An alteration pattern `p1 | p2 | p3`, which matches an inductive type with multiple constructors, or a nested disjunction like `a ∨ b ∨ c`. The patterns are fairly liberal about the exact shape of the constructors, and will insert additional alternation branches and tuple arguments if there are not enough arguments provided, and reuse the tail for further matches if there are too many arguments provided to alternation and tuple patterns. This file also contains the `obtain` and `rintro` tactics, which use the same syntax of `rcases` patterns but with a slightly different use case: * `rintro` (or `rintros`) is used like `rintro x ⟨y, z⟩` and is the same as `intros` followed by `rcases` on the newly introduced arguments. * `obtain` is the same as `rcases` but with a syntax styled after `have` rather than `cases`. `obtain ⟨hx, hy⟩ | hz := foo` is equivalent to `rcases foo with ⟨hx, hy⟩ | hz`. Unlike `rcases`, `obtain` also allows one to omit `:= foo`, although a type must be provided in this case, as in `obtain ⟨hx, hy⟩ | hz : a ∧ b ∨ c`, in which case it produces a subgoal for proving `a ∧ b ∨ c` in addition to the subgoals `hx : a, hy : b |- goal` and `hz : c |- goal`. ## Tags rcases, rintro, obtain, destructuring, cases, pattern matching, match -/ open lean lean.parser namespace tactic /-! These synonyms for `list` are used to clarify the meanings of the many usages of lists in this module. - `listΣ` is used where a list represents a disjunction, such as the list of possible constructors of an inductive type. - `listΠ` is used where a list represents a conjunction, such as the list of arguments of an individual constructor. These are merely type synonyms, and so are not checked for consistency by the compiler. The `def`/`local notation` combination makes Lean retain these annotations in reported types. -/ /-- A list, with a disjunctive meaning (like a list of inductive constructors, or subgoals) -/ @[reducible] def list_Sigma := list /-- A list, with a conjunctive meaning (like a list of constructor arguments, or hypotheses) -/ @[reducible] def list_Pi := list local notation `listΣ` := list_Sigma local notation `listΠ` := list_Pi /-- A metavariable representing a subgoal, together with a list of local constants to clear. -/ @[reducible] meta def uncleared_goal := list expr × expr /-- An `rcases` pattern can be one of the following, in a nested combination: * A name like `foo` * The special keyword `rfl` (for pattern matching on equality using `subst`) * A hyphen `-`, which clears the active hypothesis and any dependents. * A type ascription like `pat : ty` (parentheses are optional) * A tuple constructor like `⟨p1, p2, p3⟩` * An alternation / variant pattern `p1 | p2 | p3` Parentheses can be used for grouping; alternation is higher precedence than type ascription, so `p1 | p2 | p3 : ty` means `(p1 | p2 | p3) : ty`. N-ary alternations are treated as a group, so `p1 | p2 | p3` is not the same as `p1 | (p2 | p3)`, and similarly for tuples. However, note that an n-ary alternation or tuple can match an n-ary conjunction or disjunction, because if the number of patterns exceeds the number of constructors in the type being destructed, the extra patterns will match on the last element, meaning that `p1 | p2 | p3` will act like `p1 | (p2 | p3)` when matching `a1 ∨ a2 ∨ a3`. If matching against a type with 3 constructors, `p1 | (p2 | p3)` will act like `p1 | (p2 | p3) | _` instead. -/ meta inductive rcases_patt : Type | one : name → rcases_patt | clear : rcases_patt | typed : rcases_patt → pexpr → rcases_patt | tuple : listΠ rcases_patt → rcases_patt | alts : listΣ rcases_patt → rcases_patt namespace rcases_patt meta instance inhabited : inhabited rcases_patt := ⟨one `_⟩ /-- Get the name from a pattern, if provided -/ meta def name : rcases_patt → option name | (one `_) := none | (one `rfl) := none | (one n) := some n | (typed p _) := p.name | (alts [p]) := p.name | _ := none /-- Interpret an rcases pattern as a tuple, where `p` becomes `⟨p⟩` if `p` is not already a tuple. -/ meta def as_tuple : rcases_patt → listΠ rcases_patt | (tuple ps) := ps | p := [p] /-- Interpret an rcases pattern as an alternation, where non-alternations are treated as one alternative. -/ meta def as_alts : rcases_patt → listΣ rcases_patt | (alts ps) := ps | p := [p] /-- Convert a list of patterns to a tuple pattern, but mapping `[p]` to `p` instead of `⟨p⟩`. -/ meta def tuple' : listΠ rcases_patt → rcases_patt | [p] := p | ps := tuple ps /-- Convert a list of patterns to an alternation pattern, but mapping `[p]` to `p` instead of a unary alternation `|p`. -/ meta def alts' : listΣ rcases_patt → rcases_patt | [p] := p | ps := alts ps /-- This function is used for producing rcases patterns based on a case tree. Suppose that we have a list of patterns `ps` that will match correctly against the branches of the case tree for one constructor. This function will merge tuples at the end of the list, so that `[a, b, ⟨c, d⟩]` becomes `⟨a, b, c, d⟩` instead of `⟨a, b, ⟨c, d⟩⟩`. We must be careful to turn `[a, ⟨⟩]` into `⟨a, ⟨⟩⟩` instead of `⟨a⟩` (which will not perform the nested match). -/ meta def tuple₁_core : listΠ rcases_patt → listΠ rcases_patt | [] := [] | [tuple []] := [tuple []] | [tuple ps] := ps | (p :: ps) := p :: tuple₁_core ps /-- This function is used for producing rcases patterns based on a case tree. This is like `tuple₁_core` but it produces a pattern instead of a tuple pattern list, converting `[n]` to `n` instead of `⟨n⟩` and `[]` to `_`, and otherwise just converting `[a, b, c]` to `⟨a, b, c⟩`. -/ meta def tuple₁ : listΠ rcases_patt → rcases_patt | [] := default | [one n] := one n | ps := tuple (tuple₁_core ps) /-- This function is used for producing rcases patterns based on a case tree. Here we are given the list of patterns to apply to each argument of each constructor after the main case, and must produce a list of alternatives with the same effect. This function calls `tuple₁` to make the individual alternatives, and handles merging `[a, b, c | d]` to `a | b | c | d` instead of `a | b | (c | d)`. -/ meta def alts₁_core : listΣ (listΠ rcases_patt) → listΣ rcases_patt | [] := [] | [[alts ps]] := ps | (p :: ps) := tuple₁ p :: alts₁_core ps /-- This function is used for producing rcases patterns based on a case tree. This is like `alts₁_core`, but it produces a cases pattern directly instead of a list of alternatives. We specially translate the empty alternation to `⟨⟩`, and translate `|(a | b)` to `⟨a | b⟩` (because we don't have any syntax for unary alternation). Otherwise we can use the regular merging of alternations at the last argument so that `a | b | (c | d)` becomes `a | b | c | d`. -/ meta def alts₁ : listΣ (listΠ rcases_patt) → rcases_patt | [[]] := tuple [] | [[alts ps]] := tuple [alts ps] | ps := alts' (alts₁_core ps) meta instance has_reflect : has_reflect rcases_patt | (one n) := `(_) | clear := `(_) | (typed l e) := (`(typed).subst (has_reflect l)).subst (reflect e) | (tuple l) := `(λ l, tuple l).subst $ by haveI := has_reflect; exact list.reflect l | (alts l) := `(λ l, alts l).subst $ by haveI := has_reflect; exact list.reflect l /-- Formats an `rcases` pattern. If the `bracket` argument is true, then it will be printed at high precedence, i.e. it will have parentheses around it if it is not already a tuple or atomic name. -/ protected meta def format : ∀ bracket : bool, rcases_patt → tactic _root_.format | _ (one n) := pure $ to_fmt n | _ clear := pure "-" | _ (tuple []) := pure "⟨⟩" | _ (tuple ls) := do fs ← ls.mmap $ format ff, pure $ "⟨" ++ _root_.format.group (_root_.format.nest 1 $ _root_.format.join $ list.intersperse ("," ++ _root_.format.line) fs) ++ "⟩" | br (alts ls) := do fs ← ls.mmap $ format tt, let fmt := _root_.format.join $ list.intersperse (↑" |" ++ _root_.format.space) fs, pure $ if br then _root_.format.bracket "(" ")" fmt else fmt | br (typed p e) := do fp ← format ff p, fe ← pp e, let fmt := fp ++ " : " ++ fe, pure $ if br then _root_.format.bracket "(" ")" fmt else fmt meta instance has_to_tactic_format : has_to_tactic_format rcases_patt := ⟨rcases_patt.format ff⟩ end rcases_patt /-- Takes the number of fields of a single constructor and patterns to match its fields against (not necessarily the same number). The returned lists each contain one element per field of the constructor. The `name` is the name which will be used in the top-level `cases` tactic, and the `rcases_patt` is the pattern which the field will be matched against by subsequent `cases` tactics. -/ meta def rcases.process_constructor : nat → listΠ rcases_patt → listΠ name × listΠ rcases_patt | 0 ps := ([], []) | 1 [] := ([`_], [default]) | 1 [p] := ([p.name.get_or_else `_], [p]) -- The interesting case: we matched the last field against multiple -- patterns, so split off the remaining patterns into a subsequent -- match. This handles matching `α × β × γ` against `⟨a, b, c⟩`. | 1 ps := ([`_], [rcases_patt.tuple ps]) | (n+1) ps := let hd := ps.head, (ns, tl) := rcases.process_constructor n ps.tail in (hd.name.get_or_else `_ :: ns, hd :: tl) /-- Takes a list of constructor names, and an (alternation) list of patterns, and matches each pattern against its constructor. It returns the list of names that will be passed to `cases`, and the list of `(constructor name, patterns)` for each constructor, where `patterns` is the (conjunctive) list of patterns to apply to each constructor argument. -/ meta def rcases.process_constructors (params : nat) : listΣ name → listΣ rcases_patt → tactic (dlist name × listΣ (name × listΠ rcases_patt)) | [] ps := pure (dlist.empty, []) | (c::cs) ps := do n ← mk_const c >>= get_arity, let (h, t) := (match cs, ps.tail with -- We matched the last constructor against multiple patterns, -- so split off the remaining constructors. This handles matching -- `α ⊕ β ⊕ γ` against `a|b|c`. | [], _::_ := ([rcases_patt.alts ps], []) | _, _ := (ps.head.as_tuple, ps.tail) end : _), let (ns, ps) := rcases.process_constructor (n - params) h, (l, r) ← rcases.process_constructors cs t, pure (dlist.of_list ns ++ l, (c, ps) :: r) /-- Like `zip`, but only elements satisfying a matching predicate `p` will go in the list, and elements of the first list that fail to match the second list will be skipped. -/ private def align {α β} (p : α → β → Prop) [∀ a b, decidable (p a b)] : list α → list β → list (α × β) | (a::as) (b::bs) := if p a b then (a, b) :: align as bs else align as (b::bs) | _ _ := [] /-- Given a local constant `e`, get its type. *But* if `e` does not exist, go find a hypothesis with the same pretty name as `e` and get it instead. This is needed because we can sometimes lose track of the unique names of hypotheses when they are revert/intro'd by `change` and `cases`. (A better solution would be for these tactics to return a map of renamed hypotheses so that we don't lose track of them.) -/ private meta def get_local_and_type (e : expr) : tactic (expr × expr) := (do t ← infer_type e, pure (t, e)) <|> (do e ← get_local e.local_pp_name, t ← infer_type e, pure (t, e)) /-- * `rcases_core p e` will match a pattern `p` against a local hypothesis `e`. It returns the list of subgoals that were produced. * `rcases.continue pes` will match a (conjunctive) list of `(p, e)` pairs which refer to patterns and local hypotheses to match against, and applies all of them. Note that this can involve matching later arguments multiple times given earlier arguments, for example `⟨a | b, ⟨c, d⟩⟩` performs the `⟨c, d⟩` match twice, once on the `a` branch and once on `b`. -/ meta mutual def rcases_core, rcases.continue with rcases_core : rcases_patt → expr → tactic (list uncleared_goal) | (rcases_patt.one `rfl) e := do (t, e) ← get_local_and_type e, subst' e, list.map (prod.mk []) <$> get_goals -- If the pattern is any other name, we already bound the name in the -- top-level `cases` tactic, so there is no more work to do for it. | (rcases_patt.one _) _ := list.map (prod.mk []) <$> get_goals | rcases_patt.clear e := do m ← try_core (get_local_and_type e), list.map (prod.mk $ m.elim [] (λ ⟨_, e⟩, [e])) <$> get_goals | (rcases_patt.typed p ty) e := do (t, e) ← get_local_and_type e, ty ← i_to_expr_no_subgoals ``(%%ty : Sort*), unify t ty, t ← instantiate_mvars t, ty ← instantiate_mvars ty, e ← if t =ₐ ty then pure e else change_core ty (some e) >> get_local e.local_pp_name, rcases_core p e | (rcases_patt.alts [p]) e := rcases_core p e | pat e := do (t, e) ← get_local_and_type e, t ← whnf t, env ← get_env, let I := t.get_app_fn.const_name, let pat := pat.as_alts, (ids, r, l) ← (if I ≠ `quot then do when (¬env.is_inductive I) $ fail format!"rcases tactic failed: {e} : {I} is not an inductive datatype", let params := env.inductive_num_params I, let c := env.constructors_of I, (ids, r) ← rcases.process_constructors params c pat, l ← cases_core e ids.to_list, pure (ids, r, l) else do (ids, r) ← rcases.process_constructors 2 [`quot.mk] pat, [(_, d)] ← induction e ids.to_list `quot.induction_on | fail format!"quotient induction on {e} failed. Maybe goal is not in Prop?", -- the result from `induction` is missing the information that the original constructor was -- `quot.mk` so we fix this up: pure (ids, r, [(`quot.mk, d)])), gs ← get_goals, -- `cases_core` may not generate a new goal for every constructor, -- as some constructors may be impossible for type reasons. (See its -- documentation.) Match up the new goals with our remaining work -- by constructor name. let ls := align (λ (a : name × _) (b : _ × name × _), a.1 = b.2.1) r (gs.zip l), list.join <$> ls.mmap (λ⟨⟨_, ps⟩, g, _, hs, _⟩, set_goals [g] >> rcases.continue (ps.zip hs)) with rcases.continue : listΠ (rcases_patt × expr) → tactic (list uncleared_goal) | [] := list.map (prod.mk []) <$> get_goals | ((pat, e) :: pes) := do gs ← rcases_core pat e, list.join <$> gs.mmap (λ ⟨cs, g⟩, do set_goals [g], ugs ← rcases.continue pes, pure $ ugs.map $ λ ⟨cs', gs⟩, (cs ++ cs', gs)) /-- Given a list of `uncleared_goal`s, each of which is a goal metavariable and a list of variables to clear, actually perform the clear and set the goals with the result. -/ meta def clear_goals (ugs : list uncleared_goal) : tactic unit := do gs ← ugs.mmap (λ ⟨cs, g⟩, do set_goals [g], cs ← cs.mfoldr (λ c cs, (do (_, c) ← get_local_and_type c, pure (c :: cs)) <|> pure cs) [], clear' tt cs, [g] ← get_goals, pure g), set_goals gs /-- `rcases h e pat` performs case distinction on `e` using `pat` to name the arising new variables and assumptions. If `h` is `some` name, a new assumption `h : e = pat` will relate the expression `e` with the current pattern. See the module comment for the syntax of `pat`. -/ meta def rcases (h : option name) (p : pexpr) (pat : rcases_patt) : tactic unit := do let p := match pat with | rcases_patt.typed _ ty := ``(%%p : %%ty) | _ := p end, e ← match h with | some h := do x ← get_unused_name $ pat.name.get_or_else `this, interactive.generalize h () (p, x), get_local x | none := i_to_expr p end, if e.is_local_constant then focus1 (rcases_core pat e >>= clear_goals) else do x ← pat.name.elim mk_fresh_name pure, n ← revert_kdependencies e semireducible, tactic.generalize e x <|> (do t ← infer_type e, tactic.assertv x t e, get_local x >>= tactic.revert, pure ()), h ← tactic.intro1, focus1 (rcases_core pat h >>= clear_goals) /-- `rcases_many es pats` performs case distinction on the `es` using `pat` to name the arising new variables and assumptions. See the module comment for the syntax of `pat`. -/ meta def rcases_many (ps : listΠ pexpr) (pat : rcases_patt) : tactic unit := do let (_, pats) := rcases.process_constructor ps.length pat.as_tuple, pes ← (ps.zip pats).mmap (λ ⟨p, pat⟩, do let p := match pat with | rcases_patt.typed _ ty := ``(%%p : %%ty) | _ := p end, e ← i_to_expr p, if e.is_local_constant then pure (pat, e) else do x ← pat.name.elim mk_fresh_name pure, n ← revert_kdependencies e semireducible, tactic.generalize e x <|> (do t ← infer_type e, tactic.assertv x t e, get_local x >>= tactic.revert, pure ()), prod.mk pat <$> tactic.intro1), focus1 (rcases.continue pes >>= clear_goals) /-- `rintro pat₁ pat₂ ... patₙ` introduces `n` arguments, then pattern matches on the `patᵢ` using the same syntax as `rcases`. -/ meta def rintro (ids : listΠ rcases_patt) : tactic unit := do l ← ids.mmap (λ id, do e ← intro $ id.name.get_or_else `_, pure (id, e)), focus1 (rcases.continue l >>= clear_goals) /-- Like `zip_with`, but if the lists don't match in length, the excess elements will be put at the end of the result. -/ def merge_list {α} (m : α → α → α) : list α → list α → list α | [] l₂ := l₂ | l₁ [] := l₁ | (a :: l₁) (b :: l₂) := m a b :: merge_list l₁ l₂ /-- Merge two `rcases` patterns. This is used to underapproximate a case tree by an `rcases` pattern. The two patterns come from cases in two branches, that due to the syntax of `rcases` patterns are forced to overlap. The rule here is that we take only the case splits that are in common between both branches. For example if one branch does `⟨a, b⟩` and the other does `c`, then we return `c` because we don't know that a case on `c` would be safe to do. -/ meta def rcases_patt.merge : rcases_patt → rcases_patt → rcases_patt | (rcases_patt.alts p₁) p₂ := rcases_patt.alts (merge_list rcases_patt.merge p₁ p₂.as_alts) | p₁ (rcases_patt.alts p₂) := rcases_patt.alts (merge_list rcases_patt.merge p₁.as_alts p₂) | (rcases_patt.tuple p₁) p₂ := rcases_patt.tuple (merge_list rcases_patt.merge p₁ p₂.as_tuple) | p₁ (rcases_patt.tuple p₂) := rcases_patt.tuple (merge_list rcases_patt.merge p₁.as_tuple p₂) | (rcases_patt.typed p₁ e) p₂ := rcases_patt.typed (p₁.merge p₂) e | p₁ (rcases_patt.typed p₂ e) := rcases_patt.typed (p₁.merge p₂) e | (rcases_patt.one `rfl) (rcases_patt.one `rfl) := rcases_patt.one `rfl | (rcases_patt.one `_) p := p | p (rcases_patt.one `_) := p | rcases_patt.clear p := p | p rcases_patt.clear := p | (rcases_patt.one n) _ := rcases_patt.one n /-- * `rcases_hint_core depth e` does the same as `rcases p e`, except the pattern `p` is an output instead of an input, controlled only by the case depth argument `depth`. We use `cases` to depth `depth` and then reconstruct an `rcases` pattern `p` that would, if passed to `rcases`, perform the same thing as the case tree we just constructed (or at least, the nearest expressible approximation to this.) * `rcases_hint.process_constructors depth cs l` takes a list of constructor names `cs` and a matching list `l` of elements `(g, c', hs, _)` where `c'` is a constructor name (used for alignment with `cs`), `g` is the subgoal, and `hs` is the list of local hypotheses created by `cases` in that subgoal. It matches on all of them, and then produces a `ΣΠ`-list of `rcases` patterns describing the result, and the list of generated subgoals. * `rcases_hint.continue depth es` does the same as `rcases.continue (ps.zip es)`, except the patterns `ps` are an output instead of an input, created by matching on everything to depth `depth` and recording the successful cases. It returns `ps`, and the list of generated subgoals. -/ meta mutual def rcases_hint_core, rcases_hint.process_constructors, rcases_hint.continue with rcases_hint_core : ℕ → expr → tactic (rcases_patt × list expr) | depth e := do (t, e) ← get_local_and_type e, t ← whnf t, env ← get_env, let I := t.get_app_fn.const_name, (do guard (I = ``eq), subst' e, prod.mk (rcases_patt.one `rfl) <$> get_goals) <|> (do let c := env.constructors_of I, some l ← try_core (guard (depth ≠ 0) >> cases_core e) | let n := match e.local_pp_name with name.anonymous := `_ | n := n end in prod.mk (rcases_patt.one n) <$> get_goals, gs ← get_goals, if gs.empty then pure (rcases_patt.tuple [], []) else do (ps, gs') ← rcases_hint.process_constructors (depth - 1) c (gs.zip l), pure (rcases_patt.alts₁ ps, gs')) with rcases_hint.process_constructors : ℕ → listΣ name → list (expr × name × listΠ expr × list (name × expr)) → tactic (listΣ (listΠ rcases_patt) × list expr) | depth [] _ := pure ([], []) | depth cs [] := pure (cs.map (λ _, []), []) | depth (c::cs) ls@((g, c', hs, _) :: l) := if c ≠ c' then do (ps, gs) ← rcases_hint.process_constructors depth cs ls, pure ([] :: ps, gs) else do (p, gs) ← set_goals [g] >> rcases_hint.continue depth hs, (ps, gs') ← rcases_hint.process_constructors depth cs l, pure (p :: ps, gs ++ gs') with rcases_hint.continue : ℕ → listΠ expr → tactic (listΠ rcases_patt × list expr) | depth [] := prod.mk [] <$> get_goals | depth (e :: es) := do (p, gs) ← rcases_hint_core depth e, (ps, gs') ← gs.mfoldl (λ (r : listΠ rcases_patt × list expr) g, do (ps, gs') ← set_goals [g] >> rcases_hint.continue depth es, pure (merge_list rcases_patt.merge r.1 ps, r.2 ++ gs')) ([], []), pure (p :: ps, gs') /-- * `rcases? e` is like `rcases e with ...`, except it generates `...` by matching on everything it can, and it outputs an `rcases` invocation that should have the same effect. * `rcases? e : n` can be used to control the depth of case splits (especially important for recursive types like `nat`, which can be cased as many times as you like). -/ meta def rcases_hint (p : pexpr) (depth : nat) : tactic rcases_patt := do e ← i_to_expr p, if e.is_local_constant then focus1 $ do (p, gs) ← rcases_hint_core depth e, set_goals gs, pure p else do x ← mk_fresh_name, n ← revert_kdependencies e semireducible, tactic.generalize e x <|> (do t ← infer_type e, tactic.assertv x t e, get_local x >>= tactic.revert, pure ()), h ← tactic.intro1, focus1 $ do (p, gs) ← rcases_hint_core depth h, set_goals gs, pure p /-- * `rcases? ⟨e1, e2, e3⟩` is like `rcases ⟨e1, e2, e3⟩ with ...`, except it generates `...` by matching on everything it can, and it outputs an `rcases` invocation that should have the same effect. * `rcases? ⟨e1, e2, e3⟩ : n` can be used to control the depth of case splits (especially important for recursive types like `nat`, which can be cased as many times as you like). -/ meta def rcases_hint_many (ps : list pexpr) (depth : nat) : tactic (listΠ rcases_patt) := do es ← ps.mmap (λ p, do e ← i_to_expr p, if e.is_local_constant then pure e else do x ← mk_fresh_name, n ← revert_kdependencies e semireducible, tactic.generalize e x <|> (do t ← infer_type e, tactic.assertv x t e, get_local x >>= tactic.revert, pure ()), tactic.intro1), focus1 $ do (ps, gs) ← rcases_hint.continue depth es, set_goals gs, pure ps /-- * `rintro?` is like `rintro ...`, except it generates `...` by introducing and matching on everything it can, and it outputs an `rintro` invocation that should have the same effect. * `rintro? : n` can be used to control the depth of case splits (especially important for recursive types like `nat`, which can be cased as many times as you like). -/ meta def rintro_hint (depth : nat) : tactic (listΠ rcases_patt) := do l ← intros, focus1 $ do (p, gs) ← rcases_hint.continue depth l, set_goals gs, pure p setup_tactic_parser /-- * `rcases_patt_parse_hi` will parse a high precedence `rcases` pattern, `patt_hi`. This means only tuples and identifiers are allowed; alternations and type ascriptions require `(...)` instead, which switches to `patt`. * `rcases_patt_parse` will parse a low precedence `rcases` pattern, `patt`. This consists of a `patt_med` (which deals with alternations), optionally followed by a `: ty` type ascription. The expression `ty` is at `texpr` precedence because it can appear at the end of a tactic, for example in `rcases e with x : ty <|> skip`. * `rcases_patt_parse_list` will parse an alternation list, `patt_med`, one or more `patt` patterns separated by `|`. It does not parse a `:` at the end, so that `a | b : ty` parses as `(a | b) : ty` where `a | b` is the `patt_med` part. * `rcases_patt_parse_list_rest a` parses an alternation list after the initial pattern, `| b | c`. ```lean patt ::= patt_med (":" expr)? patt_med ::= (patt_hi "|")* patt_hi patt_hi ::= id | "rfl" | "_" | "⟨" (patt ",")* patt "⟩" | "(" patt ")" ``` -/ meta mutual def rcases_patt_parse_hi', rcases_patt_parse', rcases_patt_parse_list', rcases_patt_parse_list_rest with rcases_patt_parse_hi' : parser rcases_patt | x := ((brackets "(" ")" rcases_patt_parse') <|> (rcases_patt.tuple <$> brackets "⟨" "⟩" (sep_by (tk ",") rcases_patt_parse')) <|> (tk "-" $> rcases_patt.clear) <|> (rcases_patt.one <$> ident_)) x with rcases_patt_parse' : parser rcases_patt | x := (do pat ← rcases_patt.alts' <$> rcases_patt_parse_list', (tk ":" *> pat.typed <$> texpr) <|> pure pat) x with rcases_patt_parse_list' : parser (listΣ rcases_patt) | x := (rcases_patt_parse_hi' >>= rcases_patt_parse_list_rest) x with rcases_patt_parse_list_rest : rcases_patt → parser (listΣ rcases_patt) | pat := (tk "|" *> list.cons pat <$> rcases_patt_parse_list') <|> -- hack to support `-|-` patterns, because `|-` is a token (tk "|-" *> list.cons pat <$> rcases_patt_parse_list_rest rcases_patt.clear) <|> pure [pat] /-- `rcases_patt_parse_hi` will parse a high precedence `rcases` pattern, `patt_hi`. This means only tuples and identifiers are allowed; alternations and type ascriptions require `(...)` instead, which switches to `patt`. ```lean patt_hi ::= id | "rfl" | "_" | "⟨" (patt ",")* patt "⟩" | "(" patt ")" ``` -/ meta def rcases_patt_parse_hi := with_desc "patt_hi" rcases_patt_parse_hi' /-- `rcases_patt_parse` will parse a low precedence `rcases` pattern, `patt`. This consists of a `patt_med` (which deals with alternations), optionally followed by a `: ty` type ascription. The expression `ty` is at `texpr` precedence because it can appear at the end of a tactic, for example in `rcases e with x : ty <|> skip`. ```lean patt ::= patt_med (":" expr)? ``` -/ meta def rcases_patt_parse := with_desc "patt" rcases_patt_parse' /-- `rcases_patt_parse_list` will parse an alternation list, `patt_med`, one or more `patt` patterns separated by `|`. It does not parse a `:` at the end, so that `a | b : ty` parses as `(a | b) : ty` where `a | b` is the `patt_med` part. ```lean patt_med ::= (patt_hi "|")* patt_hi ``` -/ meta def rcases_patt_parse_list := with_desc "patt_med" rcases_patt_parse_list' /-- Parse the optional depth argument `(: n)?` of `rcases?` and `rintro?`, with default depth 5. -/ meta def rcases_parse_depth : parser nat := do o ← (tk ":" *> small_nat)?, pure $ o.get_or_else 5 /-- The arguments to `rcases`, which in fact dispatch to several other tactics. * `rcases? expr (: n)?` or `rcases? ⟨expr, ...⟩ (: n)?` calls `rcases_hint` * `rcases? ⟨expr, ...⟩ (: n)?` calls `rcases_hint_many` * `rcases (h :)? expr (with patt)?` calls `rcases` * `rcases ⟨expr, ...⟩ (with patt)?` calls `rcases_many` -/ @[derive has_reflect] meta inductive rcases_args | hint (tgt : pexpr ⊕ list pexpr) (depth : nat) | rcases (name : option name) (tgt : pexpr) (pat : rcases_patt) | rcases_many (tgt : listΠ pexpr) (pat : rcases_patt) /-- Syntax for a `rcases` pattern: * `rcases? expr (: n)?` * `rcases (h :)? expr (with patt_list (: expr)?)?`. -/ meta def rcases_parse : parser rcases_args := with_desc "('?' expr (: n)?) | ((h :)? expr (with patt)?)" $ do hint ← (tk "?")?, p ← (sum.inr <$> brackets "⟨" "⟩" (sep_by (tk ",") (parser.pexpr 0))) <|> (sum.inl <$> texpr), match hint with | none := do p ← (do sum.inl (expr.local_const h _ _ _) ← pure p, tk ":" *> (@sum.inl _ (pexpr ⊕ list pexpr) ∘ prod.mk h) <$> texpr) <|> pure (sum.inr p), ids ← (tk "with" *> rcases_patt_parse)?, let ids := ids.get_or_else (rcases_patt.tuple []), pure $ match p with | sum.inl (name, tgt) := rcases_args.rcases (some name) tgt ids | sum.inr (sum.inl tgt) := rcases_args.rcases none tgt ids | sum.inr (sum.inr tgts) := rcases_args.rcases_many tgts ids end | some _ := do depth ← rcases_parse_depth, pure $ rcases_args.hint p depth end /-- `rintro_patt_parse_hi` and `rintro_patt_parse` are like `rcases_patt_parse`, but is used for parsing top level `rintro` patterns, which allow sequences like `(x y : t)` in addition to simple `rcases` patterns. * `rintro_patt_parse_hi` will parse a high precedence `rcases` pattern, `rintro_patt_hi` below. This means only tuples and identifiers are allowed; alternations and type ascriptions require `(...)` instead, which switches to `patt`. * `rintro_patt_parse` will parse a low precedence `rcases` pattern, `rintro_patt` below. This consists of either a sequence of patterns `p1 p2 p3` or an alternation list `p1 | p2 | p3` treated as a single pattern, optionally followed by a `: ty` type ascription, which applies to every pattern in the list. * `rintro_patt_parse_low` parses `rintro_patt_low`, which is the same as `rintro_patt_parse tt` but it does not permit an unparenthesized alternation list, it must have the form `p1 p2 p3 (: ty)?`. ```lean rintro_patt ::= (rintro_patt_hi+ | patt_med) (":" expr)? rintro_patt_low ::= rintro_patt_hi* (":" expr)? rintro_patt_hi ::= patt_hi | "(" rintro_patt ")" ``` -/ meta mutual def rintro_patt_parse_hi', rintro_patt_parse' with rintro_patt_parse_hi' : parser (listΠ rcases_patt) | x := (brackets "(" ")" (rintro_patt_parse' tt) <|> (do p ← rcases_patt_parse_hi, pure [p])) x with rintro_patt_parse' : bool → parser (listΠ rcases_patt) | med := do ll ← rintro_patt_parse_hi'*, pats ← match med, ll.join with | tt, [] := failure | tt, [pat] := do l ← rcases_patt_parse_list_rest pat, pure [rcases_patt.alts' l] | _, pats := pure pats end, (do tk ":", e ← texpr, pure (pats.map (λ p, rcases_patt.typed p e))) <|> pure pats /-- `rintro_patt_parse_hi` will parse a high precedence `rcases` pattern, `rintro_patt_hi` below. This means only tuples and identifiers are allowed; alternations and type ascriptions require `(...)` instead, which switches to `patt`. ```lean rintro_patt_hi ::= patt_hi | "(" rintro_patt ")" ``` -/ meta def rintro_patt_parse_hi := with_desc "rintro_patt_hi" rintro_patt_parse_hi' /-- `rintro_patt_parse` will parse a low precedence `rcases` pattern, `rintro_patt` below. This consists of either a sequence of patterns `p1 p2 p3` or an alternation list `p1 | p2 | p3` treated as a single pattern, optionally followed by a `: ty` type ascription, which applies to every pattern in the list. ```lean rintro_patt ::= (rintro_patt_hi+ | patt_med) (":" expr)? ``` -/ meta def rintro_patt_parse := with_desc "rintro_patt" $ rintro_patt_parse' tt /-- `rintro_patt_parse_low` parses `rintro_patt_low`, which is the same as `rintro_patt_parse tt` but it does not permit an unparenthesized alternation list, it must have the form `p1 p2 p3 (: ty)?`. ```lean rintro_patt_low ::= rintro_patt_hi* (":" expr)? ``` -/ meta def rintro_patt_parse_low := with_desc "rintro_patt_low" $ rintro_patt_parse' ff /-- Syntax for a `rintro` pattern: `('?' (: n)?) | rintro_patt`. -/ meta def rintro_parse : parser (listΠ rcases_patt ⊕ nat) := with_desc "('?' (: n)?) | patt*" $ (tk "?" >> sum.inr <$> rcases_parse_depth) <|> sum.inl <$> rintro_patt_parse_low namespace interactive open interactive interactive.types expr /-- `rcases` is a tactic that will perform `cases` recursively, according to a pattern. It is used to destructure hypotheses or expressions composed of inductive types like `h1 : a ∧ b ∧ c ∨ d` or `h2 : ∃ x y, trans_rel R x y`. Usual usage might be `rcases h1 with ⟨ha, hb, hc⟩ | hd` or `rcases h2 with ⟨x, y, _ | ⟨z, hxz, hzy⟩⟩` for these examples. Each element of an `rcases` pattern is matched against a particular local hypothesis (most of which are generated during the execution of `rcases` and represent individual elements destructured from the input expression). An `rcases` pattern has the following grammar: * A name like `x`, which names the active hypothesis as `x`. * A blank `_`, which does nothing (letting the automatic naming system used by `cases` name the hypothesis). * A hyphen `-`, which clears the active hypothesis and any dependents. * The keyword `rfl`, which expects the hypothesis to be `h : a = b`, and calls `subst` on the hypothesis (which has the effect of replacing `b` with `a` everywhere or vice versa). * A type ascription `p : ty`, which sets the type of the hypothesis to `ty` and then matches it against `p`. (Of course, `ty` must unify with the actual type of `h` for this to work.) * A tuple pattern `⟨p1, p2, p3⟩`, which matches a constructor with many arguments, or a series of nested conjunctions or existentials. For example if the active hypothesis is `a ∧ b ∧ c`, then the conjunction will be destructured, and `p1` will be matched against `a`, `p2` against `b` and so on. * An alteration pattern `p1 | p2 | p3`, which matches an inductive type with multiple constructors, or a nested disjunction like `a ∨ b ∨ c`. A pattern like `⟨a, b, c⟩ | ⟨d, e⟩` will do a split over the inductive datatype, naming the first three parameters of the first constructor as `a,b,c` and the first two of the second constructor `d,e`. If the list is not as long as the number of arguments to the constructor or the number of constructors, the remaining variables will be automatically named. If there are nested brackets such as `⟨⟨a⟩, b | c⟩ | d` then these will cause more case splits as necessary. If there are too many arguments, such as `⟨a, b, c⟩` for splitting on `∃ x, ∃ y, p x`, then it will be treated as `⟨a, ⟨b, c⟩⟩`, splitting the last parameter as necessary. `rcases` also has special support for quotient types: quotient induction into Prop works like matching on the constructor `quot.mk`. `rcases h : e with PAT` will do the same as `rcases e with PAT` with the exception that an assumption `h : e = PAT` will be added to the context. `rcases? e` will perform case splits on `e` in the same way as `rcases e`, but rather than accepting a pattern, it does a maximal cases and prints the pattern that would produce this case splitting. The default maximum depth is 5, but this can be modified with `rcases? e : n`. -/ meta def rcases : parse rcases_parse → tactic unit | (rcases_args.rcases h p ids) := tactic.rcases h p ids | (rcases_args.rcases_many ps ids) := tactic.rcases_many ps ids | (rcases_args.hint p depth) := do (pe, patt) ← match p with | sum.inl p := prod.mk <$> pp p <*> rcases_hint p depth | sum.inr ps := do patts ← rcases_hint_many ps depth, pes ← ps.mmap pp, pure (format.bracket "⟨" "⟩" (format.comma_separated pes), rcases_patt.tuple patts) end, ppat ← pp patt, trace $ ↑"Try this: rcases " ++ pe ++ " with " ++ ppat add_tactic_doc { name := "rcases", category := doc_category.tactic, decl_names := [`tactic.interactive.rcases], tags := ["induction"] } /-- The `rintro` tactic is a combination of the `intros` tactic with `rcases` to allow for destructuring patterns while introducing variables. See `rcases` for a description of supported patterns. For example, `rintro (a | ⟨b, c⟩) ⟨d, e⟩` will introduce two variables, and then do case splits on both of them producing two subgoals, one with variables `a d e` and the other with `b c d e`. `rintro`, unlike `rcases`, also supports the form `(x y : ty)` for introducing and type-ascripting multiple variables at once, similar to binders. `rintro?` will introduce and case split on variables in the same way as `rintro`, but will also print the `rintro` invocation that would have the same result. Like `rcases?`, `rintro? : n` allows for modifying the depth of splitting; the default is 5. `rintros` is an alias for `rintro`. -/ meta def rintro : parse rintro_parse → tactic unit | (sum.inl []) := intros [] | (sum.inl l) := tactic.rintro l | (sum.inr depth) := do ps ← tactic.rintro_hint depth, fs ← ps.mmap (λ p, do f ← pp $ p.format tt, pure $ format.space ++ format.group f), trace $ ↑"Try this: rintro" ++ format.join fs /-- Alias for `rintro`. -/ meta def rintros := rintro add_tactic_doc { name := "rintro", category := doc_category.tactic, decl_names := [`tactic.interactive.rintro, `tactic.interactive.rintros], tags := ["induction"], inherit_description_from := `tactic.interactive.rintro } setup_tactic_parser /-- Parses `patt? (: expr)? (:= expr)?`, the arguments for `obtain`. (This is almost the same as `rcases_patt_parse`, but it allows the pattern part to be empty.) -/ meta def obtain_parse : parser ((option rcases_patt × option pexpr) × option (pexpr ⊕ list pexpr)) := with_desc "patt? (: expr)? (:= expr)?" $ do (pat, tp) ← (do pat ← rcases_patt_parse, pure $ match pat with | rcases_patt.typed pat tp := (some pat, some tp) | _ := (some pat, none) end) <|> prod.mk none <$> (tk ":" >> texpr)?, prod.mk (pat, tp) <$> (do tk ":=", (guard tp.is_none >> sum.inr <$> brackets "⟨" "⟩" (sep_by (tk ",") (parser.pexpr 0))) <|> (sum.inl <$> texpr))? /-- The `obtain` tactic is a combination of `have` and `rcases`. See `rcases` for a description of supported patterns. ```lean obtain ⟨patt⟩ : type, { ... } ``` is equivalent to ```lean have h : type, { ... }, rcases h with ⟨patt⟩ ``` The syntax `obtain ⟨patt⟩ : type := proof` is also supported. If `⟨patt⟩` is omitted, `rcases` will try to infer the pattern. If `type` is omitted, `:= proof` is required. -/ meta def obtain : parse obtain_parse → tactic unit | ((pat, _), some (sum.inr val)) := tactic.rcases_many val (pat.get_or_else default) | ((pat, none), some (sum.inl val)) := tactic.rcases none val (pat.get_or_else default) | ((pat, some tp), some (sum.inl val)) := tactic.rcases none val $ (pat.get_or_else default).typed tp | ((pat, some tp), none) := do nm ← mk_fresh_name, e ← to_expr tp >>= assert nm, (g :: gs) ← get_goals, set_goals gs, tactic.rcases none ``(%%e) (pat.get_or_else (rcases_patt.one `this)), gs ← get_goals, set_goals (g::gs) | ((pat, none), none) := fail $ "`obtain` requires either an expected type or a value.\n" ++ "usage: `obtain ⟨patt⟩? : type (:= val)?` or `obtain ⟨patt⟩? (: type)? := val`" add_tactic_doc { name := "obtain", category := doc_category.tactic, decl_names := [`tactic.interactive.obtain], tags := ["induction"] } end interactive end tactic
fc2ed3bd4f9984542e2fe7096600097fbcc1c00c
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/inject.lean
7c3a9fddfa03cfd7c7c4129418c1895d9a584273
[ "Apache-2.0" ]
permissive
leanprover-community/lean
12b87f69d92e614daea8bcc9d4de9a9ace089d0e
cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0
refs/heads/master
1,687,508,156,644
1,684,951,104,000
1,684,951,104,000
169,960,991
457
107
Apache-2.0
1,686,744,372,000
1,549,790,268,000
C++
UTF-8
Lean
false
false
2,015
lean
open nat inductive fi : nat → Type | f0 : ∀ {n}, fi (succ n) | fs : ∀ {n} (j : fi n), fi (succ n) namespace fi def lift {m k : nat} (f : fi m → fi k) : ∀ {n} (i : fi (m + n)), fi (k + n) | 0 v := f v | (succ n) f0 := f0 | (succ n) (fs i) := fs (lift i) set_option pp.implicit true #check @lift.equations._eqn_1 #check @lift.equations._eqn_2 #check @lift.equations._eqn_3 def to_nat : ∀ {n}, fi n → nat | (succ n) f0 := 0 | (succ n) (fs i) := succ (to_nat i) #check @to_nat.equations._eqn_1 #check @to_nat.equations._eqn_2 def to_nat' : ∀ {n}, fi n → nat | ._ (@f0 n) := 0 | ._ (@fs n i) := succ (to_nat' i) def fi' {n} (i : fi n) : Type := fi (to_nat i) /- We need the unification hint to be able to get sane equations when using fi' -/ @[unify] def to_nat_hint (n m k : nat) (i : fi (succ n)) (j : fi n) : unification_hint := { pattern := @to_nat (succ n) (@fs n j) ≟ succ m, constraints := [m ≟ to_nat j] } def inject : ∀ {n} {i : fi n}, fi' i → fi n | (succ n) (fs i) f0 := f0 | (succ n) (fs i) (fs j) := fs (inject j) def inject' : ∀ {n} {i : fi n}, fi' i → fi n | ._ (@fs n i) f0 := f0 | ._ (@fs n i) (fs j) := fs (inject' j) #check @inject.equations._eqn_1 #check @inject.equations._eqn_2 #check @inject'.equations._eqn_1 #check @inject'.equations._eqn_2 def raise {m} : Π n, fi m → fi (m + n) | 0 i := i | (succ n) i := fs (raise n i) #check @raise.equations._eqn_1 #check @raise.equations._eqn_2 def deg : Π {n}, fi (n + 1) → fi n → fi (n + 1) | (succ n) f0 j := fs j | (succ n) (fs i) f0 := f0 | (succ n) (fs i) (fs j) := fs (deg i j) def deg' : Π {n}, fi (n + 1) → fi n → fi (n + 1) | ._ (@f0 (succ n)) j := fs j | ._ (@fs (succ n) i) f0 := f0 | ._ (@fs (succ n) i) (fs j) := fs (deg' i j) #check @deg.equations._eqn_1 #check @deg.equations._eqn_2 #check @deg.equations._eqn_3 #check @deg'.equations._eqn_1 #check @deg'.equations._eqn_2 #check @deg'.equations._eqn_3 end fi
2df3163711652dbe4ba916b7e835202bb4b020b2
b074a51e20fdb737b2d4c635dd292fc54685e010
/src/category_theory/natural_isomorphism.lean
4efc99a6df3e7bbbb5759a4335e4c0564523cdf8
[ "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
5,505
lean
-- Copyright (c) 2017 Scott Morrison. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Tim Baumann, Stephen Morgan, Scott Morrison, Floris van Doorn import category_theory.functor_category import category_theory.isomorphism open category_theory universes v₁ v₂ v₃ v₄ u₁ u₂ u₃ u₄ -- declare the `v`'s first; see `category_theory.category` for an explanation namespace category_theory open nat_trans /-- The application of a natural isomorphism to an object. We put this definition in a different namespace, so that we can use α.app -/ @[simp, reducible] def iso.app {C : Type u₁} [category.{v₁} C] {D : Type u₂} [category.{v₂} D] {F G : C ⥤ D} (α : F ≅ G) (X : C) : F.obj X ≅ G.obj X := { hom := α.hom.app X, inv := α.inv.app X, hom_inv_id' := begin rw [← comp_app, iso.hom_inv_id], refl end, inv_hom_id' := begin rw [← comp_app, iso.inv_hom_id], refl end } namespace nat_iso open category_theory.category category_theory.functor variables {C : Type u₁} [𝒞 : category.{v₁} C] {D : Type u₂} [𝒟 : category.{v₂} D] {E : Type u₃} [ℰ : category.{v₃} E] include 𝒞 𝒟 @[simp] lemma trans_app {F G H : C ⥤ D} (α : F ≅ G) (β : G ≅ H) (X : C) : (α ≪≫ β).app X = α.app X ≪≫ β.app X := rfl @[simp] lemma app_hom {F G : C ⥤ D} (α : F ≅ G) (X : C) : (α.app X).hom = α.hom.app X := rfl @[simp] lemma app_inv {F G : C ⥤ D} (α : F ≅ G) (X : C) : (α.app X).inv = α.inv.app X := rfl @[simp] lemma hom_inv_id_app {F G : C ⥤ D} (α : F ≅ G) (X : C) : α.hom.app X ≫ α.inv.app X = 𝟙 (F.obj X) := congr_fun (congr_arg app α.hom_inv_id) X @[simp] lemma inv_hom_id_app {F G : C ⥤ D} (α : F ≅ G) (X : C) : α.inv.app X ≫ α.hom.app X = 𝟙 (G.obj X) := congr_fun (congr_arg app α.inv_hom_id) X variables {F G : C ⥤ D} instance hom_app_is_iso (α : F ≅ G) (X : C) : is_iso (α.hom.app X) := { inv := α.inv.app X, hom_inv_id' := begin rw [←comp_app, iso.hom_inv_id, ←id_app] end, inv_hom_id' := begin rw [←comp_app, iso.inv_hom_id, ←id_app] end } instance inv_app_is_iso (α : F ≅ G) (X : C) : is_iso (α.inv.app X) := { inv := α.hom.app X, hom_inv_id' := begin rw [←comp_app, iso.inv_hom_id, ←id_app] end, inv_hom_id' := begin rw [←comp_app, iso.hom_inv_id, ←id_app] end } @[simp] lemma hom_app_inv_app_id (α : F ≅ G) (X : C) : α.hom.app X ≫ α.inv.app X = 𝟙 _ := begin rw ←comp_app, simp, end @[simp] lemma inv_app_hom_app_id (α : F ≅ G) (X : C) : α.inv.app X ≫ α.hom.app X = 𝟙 _ := begin rw ←comp_app, simp, end variables {X Y : C} @[simp] lemma naturality_1 (α : F ≅ G) (f : X ⟶ Y) : (α.inv.app X) ≫ (F.map f) ≫ (α.hom.app Y) = G.map f := begin erw [naturality, ←category.assoc, is_iso.hom_inv_id, category.id_comp] end @[simp] lemma naturality_2 (α : F ≅ G) (f : X ⟶ Y) : (α.hom.app X) ≫ (G.map f) ≫ (α.inv.app Y) = F.map f := begin erw [naturality, ←category.assoc, is_iso.hom_inv_id, category.id_comp] end def is_iso_of_is_iso_app (α : F ⟶ G) [∀ X : C, is_iso (α.app X)] : is_iso α := { inv := { app := λ X, inv (α.app X), naturality' := λ X Y f, begin have h := congr_arg (λ f, inv (α.app X) ≫ (f ≫ inv (α.app Y))) (α.naturality f).symm, simp only [is_iso.inv_hom_id_assoc, is_iso.hom_inv_id, assoc, comp_id, cancel_mono] at h, exact h end } } instance is_iso_of_is_iso_app' (α : F ⟶ G) [H : ∀ X : C, is_iso (nat_trans.app α X)] : is_iso α := @nat_iso.is_iso_of_is_iso_app _ _ _ _ _ _ α H -- TODO can we make this an instance? def is_iso_app_of_is_iso (α : F ⟶ G) [is_iso α] (X) : is_iso (α.app X) := { inv := (inv α).app X, hom_inv_id' := congr_fun (congr_arg nat_trans.app (is_iso.hom_inv_id α)) X, inv_hom_id' := congr_fun (congr_arg nat_trans.app (is_iso.inv_hom_id α)) X } def of_components (app : ∀ X : C, (F.obj X) ≅ (G.obj X)) (naturality : ∀ {X Y : C} (f : X ⟶ Y), (F.map f) ≫ ((app Y).hom) = ((app X).hom) ≫ (G.map f)) : F ≅ G := as_iso { app := λ X, (app X).hom } @[simp] def of_components.app (app' : ∀ X : C, (F.obj X) ≅ (G.obj X)) (naturality) (X) : (of_components app' naturality).app X = app' X := by tidy @[simp] def of_components.hom_app (app : ∀ X : C, (F.obj X) ≅ (G.obj X)) (naturality) (X) : (of_components app naturality).hom.app X = (app X).hom := rfl @[simp] def of_components.inv_app (app : ∀ X : C, (F.obj X) ≅ (G.obj X)) (naturality) (X) : (of_components app naturality).inv.app X = (app X).inv := rfl include ℰ def hcomp {F G : C ⥤ D} {H I : D ⥤ E} (α : F ≅ G) (β : H ≅ I) : F ⋙ H ≅ G ⋙ I := begin refine ⟨α.hom ◫ β.hom, α.inv ◫ β.inv, _, _⟩, { ext, rw [←nat_trans.exchange], simp, refl }, ext, rw [←nat_trans.exchange], simp, refl end omit ℰ -- suggested local notation for nat_iso.hcomp. Currently unused. local infix ` ■ `:80 := hcomp end nat_iso namespace functor variables {C : Type u₁} [𝒞 : category.{v₁} C] include 𝒞 def ulift_down_up : ulift_down.{v₁} C ⋙ ulift_up C ≅ functor.id (ulift.{u₂} C) := { hom := { app := λ X, @category_struct.id (ulift.{u₂} C) _ X }, inv := { app := λ X, @category_struct.id (ulift.{u₂} C) _ X } } def ulift_up_down : ulift_up.{v₁} C ⋙ ulift_down C ≅ functor.id C := { hom := { app := λ X, 𝟙 X }, inv := { app := λ X, 𝟙 X } } end functor end category_theory
10e7e56610f5b5ec523cc1cb17ff05c8cb079558
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/category_theory/monoidal/free/basic.lean
dd133c7f02b1baca83f6dea5a473a56f6a67038a
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
11,401
lean
/- Copyright (c) 2021 Markus Himmel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Markus Himmel -/ import category_theory.monoidal.functor /-! # The free monoidal category over a type Given a type `C`, the free monoidal category over `C` has as objects formal expressions built from (formal) tensor products of terms of `C` and a formal unit. Its morphisms are compositions and tensor products of identities, unitors and associators. In this file, we construct the free monoidal category and prove that it is a monoidal category. If `D` is a monoidal category, we construct the functor `free_monoidal_category C ⥤ D` associated to a function `C → D`. The free monoidal category has two important properties: it is a groupoid and it is thin. The former is obvious from the construction, and the latter is what is commonly known as the monoidal coherence theorem. Both of these properties are proved in the file `coherence.lean`. -/ universes v' u u' namespace category_theory open monoidal_category variables {C : Type u} section variables (C) /-- Given a type `C`, the free monoidal category over `C` has as objects formal expressions built from (formal) tensor products of terms of `C` and a formal unit. Its morphisms are compositions and tensor products of identities, unitors and associators. -/ @[derive inhabited] inductive free_monoidal_category : Type u | of : C → free_monoidal_category | unit : free_monoidal_category | tensor : free_monoidal_category → free_monoidal_category → free_monoidal_category end local notation `F` := free_monoidal_category namespace free_monoidal_category /-- Formal compositions and tensor products of identities, unitors and associators. The morphisms of the free monoidal category are obtained as a quotient of these formal morphisms by the relations defining a monoidal category. -/ @[nolint has_nonempty_instance] inductive hom : F C → F C → Type u | id (X) : hom X X | α_hom (X Y Z : F C) : hom ((X.tensor Y).tensor Z) (X.tensor (Y.tensor Z)) | α_inv (X Y Z : F C) : hom (X.tensor (Y.tensor Z)) ((X.tensor Y).tensor Z) | l_hom (X) : hom (unit.tensor X) X | l_inv (X) : hom X (unit.tensor X) | ρ_hom (X : F C) : hom (X.tensor unit) X | ρ_inv (X : F C) : hom X (X.tensor unit) | comp {X Y Z} (f : hom X Y) (g : hom Y Z) : hom X Z | tensor {W X Y Z} (f : hom W Y) (g : hom X Z) : hom (W.tensor X) (Y.tensor Z) local infixr ` ⟶ᵐ `:10 := hom /-- The morphisms of the free monoidal category satisfy 21 relations ensuring that the resulting category is in fact a category and that it is monoidal. -/ inductive hom_equiv : Π {X Y : F C}, (X ⟶ᵐ Y) → (X ⟶ᵐ Y) → Prop | refl {X Y} (f : X ⟶ᵐ Y) : hom_equiv f f | symm {X Y} (f g : X ⟶ᵐ Y) : hom_equiv f g → hom_equiv g f | trans {X Y} {f g h : X ⟶ᵐ Y} : hom_equiv f g → hom_equiv g h → hom_equiv f h | comp {X Y Z} {f f' : X ⟶ᵐ Y} {g g' : Y ⟶ᵐ Z} : hom_equiv f f' → hom_equiv g g' → hom_equiv (f.comp g) (f'.comp g') | tensor {W X Y Z} {f f' : W ⟶ᵐ X} {g g' : Y ⟶ᵐ Z} : hom_equiv f f' → hom_equiv g g' → hom_equiv (f.tensor g) (f'.tensor g') | comp_id {X Y} (f : X ⟶ᵐ Y) : hom_equiv (f.comp (hom.id _)) f | id_comp {X Y} (f : X ⟶ᵐ Y) : hom_equiv ((hom.id _).comp f) f | assoc {X Y U V : F C} (f : X ⟶ᵐ U) (g : U ⟶ᵐ V) (h : V ⟶ᵐ Y) : hom_equiv ((f.comp g).comp h) (f.comp (g.comp h)) | tensor_id {X Y} : hom_equiv ((hom.id X).tensor (hom.id Y)) (hom.id _) | tensor_comp {X₁ Y₁ Z₁ X₂ Y₂ Z₂ : F C} (f₁ : X₁ ⟶ᵐ Y₁) (f₂ : X₂ ⟶ᵐ Y₂) (g₁ : Y₁ ⟶ᵐ Z₁) (g₂ : Y₂ ⟶ᵐ Z₂) : hom_equiv ((f₁.comp g₁).tensor (f₂.comp g₂)) ((f₁.tensor f₂).comp (g₁.tensor g₂)) | α_hom_inv {X Y Z} : hom_equiv ((hom.α_hom X Y Z).comp (hom.α_inv X Y Z)) (hom.id _) | α_inv_hom {X Y Z} : hom_equiv ((hom.α_inv X Y Z).comp (hom.α_hom X Y Z)) (hom.id _) | associator_naturality {X₁ X₂ X₃ Y₁ Y₂ Y₃} (f₁ : X₁ ⟶ᵐ Y₁) (f₂ : X₂ ⟶ᵐ Y₂) (f₃ : X₃ ⟶ᵐ Y₃) : hom_equiv (((f₁.tensor f₂).tensor f₃).comp (hom.α_hom Y₁ Y₂ Y₃)) ((hom.α_hom X₁ X₂ X₃).comp (f₁.tensor (f₂.tensor f₃))) | ρ_hom_inv {X} : hom_equiv ((hom.ρ_hom X).comp (hom.ρ_inv X)) (hom.id _) | ρ_inv_hom {X} : hom_equiv ((hom.ρ_inv X).comp (hom.ρ_hom X)) (hom.id _) | ρ_naturality {X Y} (f : X ⟶ᵐ Y) : hom_equiv ((f.tensor (hom.id unit)).comp (hom.ρ_hom Y)) ((hom.ρ_hom X).comp f) | l_hom_inv {X} : hom_equiv ((hom.l_hom X).comp (hom.l_inv X)) (hom.id _) | l_inv_hom {X} : hom_equiv ((hom.l_inv X).comp (hom.l_hom X)) (hom.id _) | l_naturality {X Y} (f : X ⟶ᵐ Y) : hom_equiv (((hom.id unit).tensor f).comp (hom.l_hom Y)) ((hom.l_hom X).comp f) | pentagon {W X Y Z} : hom_equiv (((hom.α_hom W X Y).tensor (hom.id Z)).comp ((hom.α_hom W (X.tensor Y) Z).comp ((hom.id W).tensor (hom.α_hom X Y Z)))) ((hom.α_hom (W.tensor X) Y Z).comp (hom.α_hom W X (Y.tensor Z))) | triangle {X Y} : hom_equiv ((hom.α_hom X unit Y).comp ((hom.id X).tensor (hom.l_hom Y))) ((hom.ρ_hom X).tensor (hom.id Y)) /-- We say that two formal morphisms in the free monoidal category are equivalent if they become equal if we apply the relations that are true in a monoidal category. Note that we will prove that there is only one equivalence class -- this is the monoidal coherence theorem. -/ def setoid_hom (X Y : F C) : setoid (X ⟶ᵐ Y) := ⟨hom_equiv, ⟨λ f, hom_equiv.refl f, λ f g, hom_equiv.symm f g, λ f g h hfg hgh, hom_equiv.trans hfg hgh⟩⟩ attribute [instance] setoid_hom section open free_monoidal_category.hom_equiv instance category_free_monoidal_category : category.{u} (F C) := { hom := λ X Y, quotient (free_monoidal_category.setoid_hom X Y), id := λ X, ⟦free_monoidal_category.hom.id _⟧, comp := λ X Y Z f g, quotient.map₂ hom.comp (by { intros f f' hf g g' hg, exact comp hf hg }) f g, id_comp' := by { rintro X Y ⟨f⟩, exact quotient.sound (id_comp f) }, comp_id' := by { rintro X Y ⟨f⟩, exact quotient.sound (comp_id f) }, assoc' := by { rintro W X Y Z ⟨f⟩ ⟨g⟩ ⟨h⟩, exact quotient.sound (assoc f g h) } } instance : monoidal_category (F C) := { tensor_obj := λ X Y, free_monoidal_category.tensor X Y, tensor_hom := λ X₁ Y₁ X₂ Y₂, quotient.map₂ hom.tensor $ by { intros _ _ h _ _ h', exact hom_equiv.tensor h h'}, tensor_id' := λ X Y, quotient.sound tensor_id, tensor_comp' := λ X₁ Y₁ Z₁ X₂ Y₂ Z₂, by { rintros ⟨f₁⟩ ⟨f₂⟩ ⟨g₁⟩ ⟨g₂⟩, exact quotient.sound (tensor_comp _ _ _ _) }, tensor_unit := free_monoidal_category.unit, associator := λ X Y Z, ⟨⟦hom.α_hom X Y Z⟧, ⟦hom.α_inv X Y Z⟧, quotient.sound α_hom_inv, quotient.sound α_inv_hom⟩, associator_naturality' := λ X₁ X₂ X₃ Y₁ Y₂ Y₃, by { rintros ⟨f₁⟩ ⟨f₂⟩ ⟨f₃⟩, exact quotient.sound (associator_naturality _ _ _) }, left_unitor := λ X, ⟨⟦hom.l_hom X⟧, ⟦hom.l_inv X⟧, quotient.sound l_hom_inv, quotient.sound l_inv_hom⟩, left_unitor_naturality' := λ X Y, by { rintro ⟨f⟩, exact quotient.sound (l_naturality _) }, right_unitor := λ X, ⟨⟦hom.ρ_hom X⟧, ⟦hom.ρ_inv X⟧, quotient.sound ρ_hom_inv, quotient.sound ρ_inv_hom⟩, right_unitor_naturality' := λ X Y, by { rintro ⟨f⟩, exact quotient.sound (ρ_naturality _) }, pentagon' := λ W X Y Z, quotient.sound pentagon, triangle' := λ X Y, quotient.sound triangle } @[simp] lemma mk_comp {X Y Z : F C} (f : X ⟶ᵐ Y) (g : Y ⟶ᵐ Z) : ⟦f.comp g⟧ = @category_struct.comp (F C) _ _ _ _ ⟦f⟧ ⟦g⟧ := rfl @[simp] lemma mk_tensor {X₁ Y₁ X₂ Y₂ : F C} (f : X₁ ⟶ᵐ Y₁) (g : X₂ ⟶ᵐ Y₂) : ⟦f.tensor g⟧ = @monoidal_category.tensor_hom (F C) _ _ _ _ _ _ ⟦f⟧ ⟦g⟧ := rfl @[simp] lemma mk_id {X : F C} : ⟦hom.id X⟧ = 𝟙 X := rfl @[simp] lemma mk_α_hom {X Y Z : F C} : ⟦hom.α_hom X Y Z⟧ = (α_ X Y Z).hom := rfl @[simp] lemma mk_α_inv {X Y Z : F C} : ⟦hom.α_inv X Y Z⟧ = (α_ X Y Z).inv := rfl @[simp] lemma mk_ρ_hom {X : F C} : ⟦hom.ρ_hom X⟧ = (ρ_ X).hom := rfl @[simp] lemma mk_ρ_inv {X : F C} : ⟦hom.ρ_inv X⟧ = (ρ_ X).inv := rfl @[simp] lemma mk_l_hom {X : F C} : ⟦hom.l_hom X⟧ = (λ_ X).hom := rfl @[simp] lemma mk_l_inv {X : F C} : ⟦hom.l_inv X⟧ = (λ_ X).inv := rfl @[simp] lemma tensor_eq_tensor {X Y : F C} : X.tensor Y = X ⊗ Y := rfl @[simp] lemma unit_eq_unit : free_monoidal_category.unit = 𝟙_ (F C) := rfl section functor variables {D : Type u'} [category.{v'} D] [monoidal_category D] (f : C → D) /-- Auxiliary definition for `free_monoidal_category.project`. -/ def project_obj : F C → D | (free_monoidal_category.of X) := f X | free_monoidal_category.unit := 𝟙_ D | (free_monoidal_category.tensor X Y) := project_obj X ⊗ project_obj Y section open hom /-- Auxiliary definition for `free_monoidal_category.project`. -/ @[simp] def project_map_aux : Π {X Y : F C}, (X ⟶ᵐ Y) → (project_obj f X ⟶ project_obj f Y) | _ _ (id _) := 𝟙 _ | _ _ (α_hom _ _ _) := (α_ _ _ _).hom | _ _ (α_inv _ _ _) := (α_ _ _ _).inv | _ _ (l_hom _) := (λ_ _).hom | _ _ (l_inv _) := (λ_ _).inv | _ _ (ρ_hom _) := (ρ_ _).hom | _ _ (ρ_inv _) := (ρ_ _).inv | _ _ (comp f g) := project_map_aux f ≫ project_map_aux g | _ _ (hom.tensor f g) := project_map_aux f ⊗ project_map_aux g /-- Auxiliary definition for `free_monoidal_category.project`. -/ def project_map (X Y : F C) : (X ⟶ Y) → (project_obj f X ⟶ project_obj f Y) := quotient.lift (project_map_aux f) begin intros f g h, induction h with X Y f X Y f g hfg hfg' X Y f g h _ _ hfg hgh X Y Z f f' g g' _ _ hf hg W X Y Z f g f' g' _ _ hfg hfg', { refl }, { exact hfg'.symm }, { exact hfg.trans hgh }, { simp only [project_map_aux, hf, hg] }, { simp only [project_map_aux, hfg, hfg'] }, { simp only [project_map_aux, category.comp_id] }, { simp only [project_map_aux, category.id_comp] }, { simp only [project_map_aux, category.assoc ] }, { simp only [project_map_aux, monoidal_category.tensor_id], refl }, { simp only [project_map_aux, monoidal_category.tensor_comp] }, { simp only [project_map_aux, iso.hom_inv_id] }, { simp only [project_map_aux, iso.inv_hom_id] }, { simp only [project_map_aux, monoidal_category.associator_naturality] }, { simp only [project_map_aux, iso.hom_inv_id] }, { simp only [project_map_aux, iso.inv_hom_id] }, { simp only [project_map_aux], dsimp [project_obj], exact monoidal_category.right_unitor_naturality _ }, { simp only [project_map_aux, iso.hom_inv_id] }, { simp only [project_map_aux, iso.inv_hom_id] }, { simp only [project_map_aux], dsimp [project_obj], exact monoidal_category.left_unitor_naturality _ }, { simp only [project_map_aux], exact monoidal_category.pentagon _ _ _ _ }, { simp only [project_map_aux], exact monoidal_category.triangle _ _ } end end /-- If `D` is a monoidal category and we have a function `C → D`, then we have a functor from the free monoidal category over `C` to the category `D`. -/ def project : monoidal_functor (F C) D := { obj := project_obj f, map := project_map f, ε := 𝟙 _, μ := λ X Y, 𝟙 _ } end functor end end free_monoidal_category end category_theory
b6fb1d421f6b68641ca7003c11bbc63edcdbcdca
d29d82a0af640c937e499f6be79fc552eae0aa13
/src/data/complex/exponential.lean
8d9d4ceda71afe458e9a12f37b2ac5bc24894790
[ "Apache-2.0" ]
permissive
AbdulMajeedkhurasani/mathlib
835f8a5c5cf3075b250b3737172043ab4fa1edf6
79bc7323b164aebd000524ebafd198eb0e17f956
refs/heads/master
1,688,003,895,660
1,627,788,521,000
1,627,788,521,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
61,925
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Abhimanyu Pallavi Sudhir -/ import algebra.geom_sum import data.complex.basic import data.nat.choose.sum /-! # Exponential, trigonometric and hyperbolic trigonometric functions This file contains the definitions of the real and complex exponential, sine, cosine, tangent, hyperbolic sine, hyperbolic cosine, and hyperbolic tangent functions. -/ local notation `abs'` := _root_.abs open is_absolute_value open_locale classical big_operators nat section open real is_absolute_value finset lemma forall_ge_le_of_forall_le_succ {α : Type*} [preorder α] (f : ℕ → α) {m : ℕ} (h : ∀ n ≥ m, f n.succ ≤ f n) : ∀ {l}, ∀ k ≥ m, k ≤ l → f l ≤ f k := begin assume l k hkm hkl, generalize hp : l - k = p, have : l = k + p := add_comm p k ▸ (nat.sub_eq_iff_eq_add hkl).1 hp, subst this, clear hkl hp, induction p with p ih, { simp }, { exact le_trans (h _ (le_trans hkm (nat.le_add_right _ _))) ih } end section variables {α : Type*} {β : Type*} [ring β] [linear_ordered_field α] [archimedean α] {abv : β → α} [is_absolute_value abv] lemma is_cau_of_decreasing_bounded (f : ℕ → α) {a : α} {m : ℕ} (ham : ∀ n ≥ m, abs (f n) ≤ a) (hnm : ∀ n ≥ m, f n.succ ≤ f n) : is_cau_seq abs f := λ ε ε0, let ⟨k, hk⟩ := archimedean.arch a ε0 in have h : ∃ l, ∀ n ≥ m, a - l • ε < f n := ⟨k + k + 1, λ n hnm, lt_of_lt_of_le (show a - (k + (k + 1)) • ε < -abs (f n), from lt_neg.1 $ lt_of_le_of_lt (ham n hnm) (begin rw [neg_sub, lt_sub_iff_add_lt, add_nsmul, add_nsmul, one_nsmul], exact add_lt_add_of_le_of_lt hk (lt_of_le_of_lt hk (lt_add_of_pos_right _ ε0)), end)) (neg_le.2 $ (abs_neg (f n)) ▸ le_abs_self _)⟩, let l := nat.find h in have hl : ∀ (n : ℕ), n ≥ m → f n > a - l • ε := nat.find_spec h, have hl0 : l ≠ 0 := λ hl0, not_lt_of_ge (ham m (le_refl _)) (lt_of_lt_of_le (by have := hl m (le_refl m); simpa [hl0] using this) (le_abs_self (f m))), begin cases not_forall.1 (nat.find_min h (nat.pred_lt hl0)) with i hi, rw [not_imp, not_lt] at hi, existsi i, assume j hj, have hfij : f j ≤ f i := forall_ge_le_of_forall_le_succ f hnm _ hi.1 hj, rw [abs_of_nonpos (sub_nonpos.2 hfij), neg_sub, sub_lt_iff_lt_add'], exact calc f i ≤ a - (nat.pred l) • ε : hi.2 ... = a - l • ε + ε : by conv {to_rhs, rw [← nat.succ_pred_eq_of_pos (nat.pos_of_ne_zero hl0), succ_nsmul', sub_add, add_sub_cancel] } ... < f j + ε : add_lt_add_right (hl j (le_trans hi.1 hj)) _ end lemma is_cau_of_mono_bounded (f : ℕ → α) {a : α} {m : ℕ} (ham : ∀ n ≥ m, abs (f n) ≤ a) (hnm : ∀ n ≥ m, f n ≤ f n.succ) : is_cau_seq abs f := begin refine @eq.rec_on (ℕ → α) _ (is_cau_seq abs) _ _ (-⟨_, @is_cau_of_decreasing_bounded _ _ _ (λ n, -f n) a m (by simpa) (by simpa)⟩ : cau_seq α abs).2, ext, exact neg_neg _ end end section no_archimedean variables {α : Type*} {β : Type*} [ring β] [linear_ordered_field α] {abv : β → α} [is_absolute_value abv] lemma is_cau_series_of_abv_le_cau {f : ℕ → β} {g : ℕ → α} (n : ℕ) : (∀ m, n ≤ m → abv (f m) ≤ g m) → is_cau_seq abs (λ n, ∑ i in range n, g i) → is_cau_seq abv (λ n, ∑ i in range n, f i) := begin assume hm hg ε ε0, cases hg (ε / 2) (div_pos ε0 (by norm_num)) with i hi, existsi max n i, assume j ji, have hi₁ := hi j (le_trans (le_max_right n i) ji), have hi₂ := hi (max n i) (le_max_right n i), have sub_le := abs_sub_le (∑ k in range j, g k) (∑ k in range i, g k) (∑ k in range (max n i), g k), have := add_lt_add hi₁ hi₂, rw [abs_sub_comm (∑ k in range (max n i), g k), add_halves ε] at this, refine lt_of_le_of_lt (le_trans (le_trans _ (le_abs_self _)) sub_le) this, generalize hk : j - max n i = k, clear this hi₂ hi₁ hi ε0 ε hg sub_le, rw nat.sub_eq_iff_eq_add ji at hk, rw hk, clear hk ji j, induction k with k' hi, { simp [abv_zero abv] }, { simp only [nat.succ_add, sum_range_succ_comm, sub_eq_add_neg, add_assoc], refine le_trans (abv_add _ _ _) _, simp only [sub_eq_add_neg] at hi, exact add_le_add (hm _ (le_add_of_nonneg_of_le (nat.zero_le _) (le_max_left _ _))) hi }, end lemma is_cau_series_of_abv_cau {f : ℕ → β} : is_cau_seq abs (λ m, ∑ n in range m, abv (f n)) → is_cau_seq abv (λ m, ∑ n in range m, f n) := is_cau_series_of_abv_le_cau 0 (λ n h, le_refl _) end no_archimedean section variables {α : Type*} {β : Type*} [ring β] [linear_ordered_field α] [archimedean α] {abv : β → α} [is_absolute_value abv] lemma is_cau_geo_series {β : Type*} [field β] {abv : β → α} [is_absolute_value abv] (x : β) (hx1 : abv x < 1) : is_cau_seq abv (λ n, ∑ m in range n, x ^ m) := have hx1' : abv x ≠ 1 := λ h, by simpa [h, lt_irrefl] using hx1, is_cau_series_of_abv_cau begin simp only [abv_pow abv] {eta := ff}, have : (λ (m : ℕ), ∑ n in range m, (abv x) ^ n) = λ m, geom_sum (abv x) m := rfl, simp only [this, geom_sum_eq hx1'] {eta := ff}, conv in (_ / _) { rw [← neg_div_neg_eq, neg_sub, neg_sub] }, refine @is_cau_of_mono_bounded _ _ _ _ ((1 : α) / (1 - abv x)) 0 _ _, { assume n hn, rw abs_of_nonneg, refine div_le_div_of_le (le_of_lt $ sub_pos.2 hx1) (sub_le_self _ (abv_pow abv x n ▸ abv_nonneg _ _)), refine div_nonneg (sub_nonneg.2 _) (sub_nonneg.2 $ le_of_lt hx1), clear hn, induction n with n ih, { simp }, { rw [pow_succ, ← one_mul (1 : α)], refine mul_le_mul (le_of_lt hx1) ih (abv_pow abv x n ▸ abv_nonneg _ _) (by norm_num) } }, { assume n hn, refine div_le_div_of_le (le_of_lt $ sub_pos.2 hx1) (sub_le_sub_left _ _), rw [← one_mul (_ ^ n), pow_succ], exact mul_le_mul_of_nonneg_right (le_of_lt hx1) (pow_nonneg (abv_nonneg _ _) _) } end lemma is_cau_geo_series_const (a : α) {x : α} (hx1 : abs x < 1) : is_cau_seq abs (λ m, ∑ n in range m, a * x ^ n) := have is_cau_seq abs (λ m, a * ∑ n in range m, x ^ n) := (cau_seq.const abs a * ⟨_, is_cau_geo_series x hx1⟩).2, by simpa only [mul_sum] lemma series_ratio_test {f : ℕ → β} (n : ℕ) (r : α) (hr0 : 0 ≤ r) (hr1 : r < 1) (h : ∀ m, n ≤ m → abv (f m.succ) ≤ r * abv (f m)) : is_cau_seq abv (λ m, ∑ n in range m, f n) := have har1 : abs r < 1, by rwa abs_of_nonneg hr0, begin refine is_cau_series_of_abv_le_cau n.succ _ (is_cau_geo_series_const (abv (f n.succ) * r⁻¹ ^ n.succ) har1), assume m hmn, cases classical.em (r = 0) with r_zero r_ne_zero, { have m_pos := lt_of_lt_of_le (nat.succ_pos n) hmn, have := h m.pred (nat.le_of_succ_le_succ (by rwa [nat.succ_pred_eq_of_pos m_pos])), simpa [r_zero, nat.succ_pred_eq_of_pos m_pos, pow_succ] }, generalize hk : m - n.succ = k, have r_pos : 0 < r := lt_of_le_of_ne hr0 (ne.symm r_ne_zero), replace hk : m = k + n.succ := (nat.sub_eq_iff_eq_add hmn).1 hk, induction k with k ih generalizing m n, { rw [hk, zero_add, mul_right_comm, inv_pow' _ _, ← div_eq_mul_inv, mul_div_cancel], exact (ne_of_lt (pow_pos r_pos _)).symm }, { have kn : k + n.succ ≥ n.succ, by rw ← zero_add n.succ; exact add_le_add (zero_le _) (by simp), rw [hk, nat.succ_add, pow_succ' r, ← mul_assoc], exact le_trans (by rw mul_comm; exact h _ (nat.le_of_succ_le kn)) (mul_le_mul_of_nonneg_right (ih (k + n.succ) n h kn rfl) hr0) } end lemma sum_range_diag_flip {α : Type*} [add_comm_monoid α] (n : ℕ) (f : ℕ → ℕ → α) : ∑ m in range n, ∑ k in range (m + 1), f k (m - k) = ∑ m in range n, ∑ k in range (n - m), f m k := by rw [sum_sigma', sum_sigma']; exact sum_bij (λ a _, ⟨a.2, a.1 - a.2⟩) (λ a ha, have h₁ : a.1 < n := mem_range.1 (mem_sigma.1 ha).1, have h₂ : a.2 < nat.succ a.1 := mem_range.1 (mem_sigma.1 ha).2, mem_sigma.2 ⟨mem_range.2 (lt_of_lt_of_le h₂ h₁), mem_range.2 ((nat.sub_lt_sub_right_iff (nat.le_of_lt_succ h₂)).2 h₁)⟩) (λ _ _, rfl) (λ ⟨a₁, a₂⟩ ⟨b₁, b₂⟩ ha hb h, have ha : a₁ < n ∧ a₂ ≤ a₁ := ⟨mem_range.1 (mem_sigma.1 ha).1, nat.le_of_lt_succ (mem_range.1 (mem_sigma.1 ha).2)⟩, have hb : b₁ < n ∧ b₂ ≤ b₁ := ⟨mem_range.1 (mem_sigma.1 hb).1, nat.le_of_lt_succ (mem_range.1 (mem_sigma.1 hb).2)⟩, have h : a₂ = b₂ ∧ _ := sigma.mk.inj h, have h' : a₁ = b₁ - b₂ + a₂ := (nat.sub_eq_iff_eq_add ha.2).1 (eq_of_heq h.2), sigma.mk.inj_iff.2 ⟨nat.sub_add_cancel hb.2 ▸ h'.symm ▸ h.1 ▸ rfl, (heq_of_eq h.1)⟩) (λ ⟨a₁, a₂⟩ ha, have ha : a₁ < n ∧ a₂ < n - a₁ := ⟨mem_range.1 (mem_sigma.1 ha).1, (mem_range.1 (mem_sigma.1 ha).2)⟩, ⟨⟨a₂ + a₁, a₁⟩, ⟨mem_sigma.2 ⟨mem_range.2 (nat.lt_sub_right_iff_add_lt.1 ha.2), mem_range.2 (nat.lt_succ_of_le (nat.le_add_left _ _))⟩, sigma.mk.inj_iff.2 ⟨rfl, heq_of_eq (nat.add_sub_cancel _ _).symm⟩⟩⟩) lemma sum_range_sub_sum_range {α : Type*} [add_comm_group α] {f : ℕ → α} {n m : ℕ} (hnm : n ≤ m) : ∑ k in range m, f k - ∑ k in range n, f k = ∑ k in (range m).filter (λ k, n ≤ k), f k := begin rw [← sum_sdiff (@filter_subset _ (λ k, n ≤ k) _ (range m)), sub_eq_iff_eq_add, ← eq_sub_iff_add_eq, add_sub_cancel'], refine finset.sum_congr (finset.ext $ λ a, ⟨λ h, by simp at *; finish, λ h, have ham : a < m := lt_of_lt_of_le (mem_range.1 h) hnm, by simp * at *⟩) (λ _ _, rfl), end end section no_archimedean variables {α : Type*} {β : Type*} [ring β] [linear_ordered_field α] {abv : β → α} [is_absolute_value abv] lemma abv_sum_le_sum_abv {γ : Type*} (f : γ → β) (s : finset γ) : abv (∑ k in s, f k) ≤ ∑ k in s, abv (f k) := by haveI := classical.dec_eq γ; exact finset.induction_on s (by simp [abv_zero abv]) (λ a s has ih, by rw [sum_insert has, sum_insert has]; exact le_trans (abv_add abv _ _) (add_le_add_left ih _)) lemma cauchy_product {a b : ℕ → β} (ha : is_cau_seq abs (λ m, ∑ n in range m, abv (a n))) (hb : is_cau_seq abv (λ m, ∑ n in range m, b n)) (ε : α) (ε0 : 0 < ε) : ∃ i : ℕ, ∀ j ≥ i, abv ((∑ k in range j, a k) * (∑ k in range j, b k) - ∑ n in range j, ∑ m in range (n + 1), a m * b (n - m)) < ε := let ⟨Q, hQ⟩ := cau_seq.bounded ⟨_, hb⟩ in let ⟨P, hP⟩ := cau_seq.bounded ⟨_, ha⟩ in have hP0 : 0 < P, from lt_of_le_of_lt (abs_nonneg _) (hP 0), have hPε0 : 0 < ε / (2 * P), from div_pos ε0 (mul_pos (show (2 : α) > 0, from by norm_num) hP0), let ⟨N, hN⟩ := cau_seq.cauchy₂ ⟨_, hb⟩ hPε0 in have hQε0 : 0 < ε / (4 * Q), from div_pos ε0 (mul_pos (show (0 : α) < 4, by norm_num) (lt_of_le_of_lt (abv_nonneg _ _) (hQ 0))), let ⟨M, hM⟩ := cau_seq.cauchy₂ ⟨_, ha⟩ hQε0 in ⟨2 * (max N M + 1), λ K hK, have h₁ : ∑ m in range K, ∑ k in range (m + 1), a k * b (m - k) = ∑ m in range K, ∑ n in range (K - m), a m * b n, by simpa using sum_range_diag_flip K (λ m n, a m * b n), have h₂ : (λ i, ∑ k in range (K - i), a i * b k) = (λ i, a i * ∑ k in range (K - i), b k), by simp [finset.mul_sum], have h₃ : ∑ i in range K, a i * ∑ k in range (K - i), b k = ∑ i in range K, a i * (∑ k in range (K - i), b k - ∑ k in range K, b k) + ∑ i in range K, a i * ∑ k in range K, b k, by rw ← sum_add_distrib; simp [(mul_add _ _ _).symm], have two_mul_two : (4 : α) = 2 * 2, by norm_num, have hQ0 : Q ≠ 0, from λ h, by simpa [h, lt_irrefl] using hQε0, have h2Q0 : 2 * Q ≠ 0, from mul_ne_zero two_ne_zero hQ0, have hε : ε / (2 * P) * P + ε / (4 * Q) * (2 * Q) = ε, by rw [← div_div_eq_div_mul, div_mul_cancel _ (ne.symm (ne_of_lt hP0)), two_mul_two, mul_assoc, ← div_div_eq_div_mul, div_mul_cancel _ h2Q0, add_halves], have hNMK : max N M + 1 < K, from lt_of_lt_of_le (by rw two_mul; exact lt_add_of_pos_left _ (nat.succ_pos _)) hK, have hKN : N < K, from calc N ≤ max N M : le_max_left _ _ ... < max N M + 1 : nat.lt_succ_self _ ... < K : hNMK, have hsumlesum : ∑ i in range (max N M + 1), abv (a i) * abv (∑ k in range (K - i), b k - ∑ k in range K, b k) ≤ ∑ i in range (max N M + 1), abv (a i) * (ε / (2 * P)), from sum_le_sum (λ m hmJ, mul_le_mul_of_nonneg_left (le_of_lt (hN (K - m) K (nat.le_sub_left_of_add_le (le_trans (by rw two_mul; exact add_le_add (le_of_lt (mem_range.1 hmJ)) (le_trans (le_max_left _ _) (le_of_lt (lt_add_one _)))) hK)) (le_of_lt hKN))) (abv_nonneg abv _)), have hsumltP : ∑ n in range (max N M + 1), abv (a n) < P := calc ∑ n in range (max N M + 1), abv (a n) = abs (∑ n in range (max N M + 1), abv (a n)) : eq.symm (abs_of_nonneg (sum_nonneg (λ x h, abv_nonneg abv (a x)))) ... < P : hP (max N M + 1), begin rw [h₁, h₂, h₃, sum_mul, ← sub_sub, sub_right_comm, sub_self, zero_sub, abv_neg abv], refine lt_of_le_of_lt (abv_sum_le_sum_abv _ _) _, suffices : ∑ i in range (max N M + 1), abv (a i) * abv (∑ k in range (K - i), b k - ∑ k in range K, b k) + (∑ i in range K, abv (a i) * abv (∑ k in range (K - i), b k - ∑ k in range K, b k) - ∑ i in range (max N M + 1), abv (a i) * abv (∑ k in range (K - i), b k - ∑ k in range K, b k)) < ε / (2 * P) * P + ε / (4 * Q) * (2 * Q), { rw hε at this, simpa [abv_mul abv] }, refine add_lt_add (lt_of_le_of_lt hsumlesum (by rw [← sum_mul, mul_comm]; exact (mul_lt_mul_left hPε0).mpr hsumltP)) _, rw sum_range_sub_sum_range (le_of_lt hNMK), exact calc ∑ i in (range K).filter (λ k, max N M + 1 ≤ k), abv (a i) * abv (∑ k in range (K - i), b k - ∑ k in range K, b k) ≤ ∑ i in (range K).filter (λ k, max N M + 1 ≤ k), abv (a i) * (2 * Q) : sum_le_sum (λ n hn, begin refine mul_le_mul_of_nonneg_left _ (abv_nonneg _ _), rw sub_eq_add_neg, refine le_trans (abv_add _ _ _) _, rw [two_mul, abv_neg abv], exact add_le_add (le_of_lt (hQ _)) (le_of_lt (hQ _)), end) ... < ε / (4 * Q) * (2 * Q) : by rw [← sum_mul, ← sum_range_sub_sum_range (le_of_lt hNMK)]; refine (mul_lt_mul_right $ by rw two_mul; exact add_pos (lt_of_le_of_lt (abv_nonneg _ _) (hQ 0)) (lt_of_le_of_lt (abv_nonneg _ _) (hQ 0))).2 (lt_of_le_of_lt (le_abs_self _) (hM _ _ (le_trans (nat.le_succ_of_le (le_max_right _ _)) (le_of_lt hNMK)) (nat.le_succ_of_le (le_max_right _ _)))) end⟩ end no_archimedean end open finset open cau_seq namespace complex lemma is_cau_abs_exp (z : ℂ) : is_cau_seq _root_.abs (λ n, ∑ m in range n, abs (z ^ m / m!)) := let ⟨n, hn⟩ := exists_nat_gt (abs z) in have hn0 : (0 : ℝ) < n, from lt_of_le_of_lt (abs_nonneg _) hn, series_ratio_test n (complex.abs z / n) (div_nonneg (complex.abs_nonneg _) (le_of_lt hn0)) (by rwa [div_lt_iff hn0, one_mul]) (λ m hm, by rw [abs_abs, abs_abs, nat.factorial_succ, pow_succ, mul_comm m.succ, nat.cast_mul, ← div_div_eq_div_mul, mul_div_assoc, mul_div_right_comm, abs_mul, abs_div, abs_cast_nat]; exact mul_le_mul_of_nonneg_right (div_le_div_of_le_left (abs_nonneg _) hn0 (nat.cast_le.2 (le_trans hm (nat.le_succ _)))) (abs_nonneg _)) noncomputable theory lemma is_cau_exp (z : ℂ) : is_cau_seq abs (λ n, ∑ m in range n, z ^ m / m!) := is_cau_series_of_abv_cau (is_cau_abs_exp z) /-- The Cauchy sequence consisting of partial sums of the Taylor series of the complex exponential function -/ @[pp_nodot] def exp' (z : ℂ) : cau_seq ℂ complex.abs := ⟨λ n, ∑ m in range n, z ^ m / m!, is_cau_exp z⟩ /-- The complex exponential function, defined via its Taylor series -/ @[pp_nodot] def exp (z : ℂ) : ℂ := lim (exp' z) /-- The complex sine function, defined via `exp` -/ @[pp_nodot] def sin (z : ℂ) : ℂ := ((exp (-z * I) - exp (z * I)) * I) / 2 /-- The complex cosine function, defined via `exp` -/ @[pp_nodot] def cos (z : ℂ) : ℂ := (exp (z * I) + exp (-z * I)) / 2 /-- The complex tangent function, defined as `sin z / cos z` -/ @[pp_nodot] def tan (z : ℂ) : ℂ := sin z / cos z /-- The complex hyperbolic sine function, defined via `exp` -/ @[pp_nodot] def sinh (z : ℂ) : ℂ := (exp z - exp (-z)) / 2 /-- The complex hyperbolic cosine function, defined via `exp` -/ @[pp_nodot] def cosh (z : ℂ) : ℂ := (exp z + exp (-z)) / 2 /-- The complex hyperbolic tangent function, defined as `sinh z / cosh z` -/ @[pp_nodot] def tanh (z : ℂ) : ℂ := sinh z / cosh z end complex namespace real open complex /-- The real exponential function, defined as the real part of the complex exponential -/ @[pp_nodot] def exp (x : ℝ) : ℝ := (exp x).re /-- The real sine function, defined as the real part of the complex sine -/ @[pp_nodot] def sin (x : ℝ) : ℝ := (sin x).re /-- The real cosine function, defined as the real part of the complex cosine -/ @[pp_nodot] def cos (x : ℝ) : ℝ := (cos x).re /-- The real tangent function, defined as the real part of the complex tangent -/ @[pp_nodot] def tan (x : ℝ) : ℝ := (tan x).re /-- The real hypebolic sine function, defined as the real part of the complex hyperbolic sine -/ @[pp_nodot] def sinh (x : ℝ) : ℝ := (sinh x).re /-- The real hypebolic cosine function, defined as the real part of the complex hyperbolic cosine -/ @[pp_nodot] def cosh (x : ℝ) : ℝ := (cosh x).re /-- The real hypebolic tangent function, defined as the real part of the complex hyperbolic tangent -/ @[pp_nodot] def tanh (x : ℝ) : ℝ := (tanh x).re end real namespace complex variables (x y : ℂ) @[simp] lemma exp_zero : exp 0 = 1 := lim_eq_of_equiv_const $ λ ε ε0, ⟨1, λ j hj, begin convert ε0, cases j, { exact absurd hj (not_le_of_gt zero_lt_one) }, { dsimp [exp'], induction j with j ih, { dsimp [exp']; simp }, { rw ← ih dec_trivial, simp only [sum_range_succ, pow_succ], simp } } end⟩ lemma exp_add : exp (x + y) = exp x * exp y := show lim (⟨_, is_cau_exp (x + y)⟩ : cau_seq ℂ abs) = lim (show cau_seq ℂ abs, from ⟨_, is_cau_exp x⟩) * lim (show cau_seq ℂ abs, from ⟨_, is_cau_exp y⟩), from have hj : ∀ j : ℕ, ∑ m in range j, (x + y) ^ m / m! = ∑ i in range j, ∑ k in range (i + 1), x ^ k / k! * (y ^ (i - k) / (i - k)!), from assume j, finset.sum_congr rfl (λ m hm, begin rw [add_pow, div_eq_mul_inv, sum_mul], refine finset.sum_congr rfl (λ i hi, _), have h₁ : (m.choose i : ℂ) ≠ 0 := nat.cast_ne_zero.2 (pos_iff_ne_zero.1 (nat.choose_pos (nat.le_of_lt_succ (mem_range.1 hi)))), have h₂ := nat.choose_mul_factorial_mul_factorial (nat.le_of_lt_succ $ finset.mem_range.1 hi), rw [← h₂, nat.cast_mul, nat.cast_mul, mul_inv', mul_inv'], simp only [mul_left_comm (m.choose i : ℂ), mul_assoc, mul_left_comm (m.choose i : ℂ)⁻¹, mul_comm (m.choose i : ℂ)], rw inv_mul_cancel h₁, simp [div_eq_mul_inv, mul_comm, mul_assoc, mul_left_comm] end), by rw lim_mul_lim; exact eq.symm (lim_eq_lim_of_equiv (by dsimp; simp only [hj]; exact cauchy_product (is_cau_abs_exp x) (is_cau_exp y))) attribute [irreducible] complex.exp lemma exp_list_sum (l : list ℂ) : exp l.sum = (l.map exp).prod := @monoid_hom.map_list_prod (multiplicative ℂ) ℂ _ _ ⟨exp, exp_zero, exp_add⟩ l lemma exp_multiset_sum (s : multiset ℂ) : exp s.sum = (s.map exp).prod := @monoid_hom.map_multiset_prod (multiplicative ℂ) ℂ _ _ ⟨exp, exp_zero, exp_add⟩ s lemma exp_sum {α : Type*} (s : finset α) (f : α → ℂ) : exp (∑ x in s, f x) = ∏ x in s, exp (f x) := @monoid_hom.map_prod (multiplicative ℂ) α ℂ _ _ ⟨exp, exp_zero, exp_add⟩ f s lemma exp_nat_mul (x : ℂ) : ∀ n : ℕ, exp(n*x) = (exp x)^n | 0 := by rw [nat.cast_zero, zero_mul, exp_zero, pow_zero] | (nat.succ n) := by rw [pow_succ', nat.cast_add_one, add_mul, exp_add, ←exp_nat_mul, one_mul] lemma exp_ne_zero : exp x ≠ 0 := λ h, zero_ne_one $ by rw [← exp_zero, ← add_neg_self x, exp_add, h]; simp lemma exp_neg : exp (-x) = (exp x)⁻¹ := by rw [← mul_right_inj' (exp_ne_zero x), ← exp_add]; simp [mul_inv_cancel (exp_ne_zero x)] lemma exp_sub : exp (x - y) = exp x / exp y := by simp [sub_eq_add_neg, exp_add, exp_neg, div_eq_mul_inv] lemma exp_int_mul (z : ℂ) (n : ℤ) : complex.exp (n * z) = (complex.exp z) ^ n := begin cases n, { apply complex.exp_nat_mul }, { simpa [complex.exp_neg, add_comm, ← neg_mul_eq_neg_mul_symm] using complex.exp_nat_mul (-z) (1 + n) }, end @[simp] lemma exp_conj : exp (conj x) = conj (exp x) := begin dsimp [exp], rw [← lim_conj], refine congr_arg lim (cau_seq.ext (λ _, _)), dsimp [exp', function.comp, cau_seq_conj], rw conj.map_sum, refine sum_congr rfl (λ n hn, _), rw [conj.map_div, conj.map_pow, ← of_real_nat_cast, conj_of_real] end @[simp] lemma of_real_exp_of_real_re (x : ℝ) : ((exp x).re : ℂ) = exp x := eq_conj_iff_re.1 $ by rw [← exp_conj, conj_of_real] @[simp, norm_cast] lemma of_real_exp (x : ℝ) : (real.exp x : ℂ) = exp x := of_real_exp_of_real_re _ @[simp] lemma exp_of_real_im (x : ℝ) : (exp x).im = 0 := by rw [← of_real_exp_of_real_re, of_real_im] lemma exp_of_real_re (x : ℝ) : (exp x).re = real.exp x := rfl lemma two_sinh : 2 * sinh x = exp x - exp (-x) := mul_div_cancel' _ two_ne_zero' lemma two_cosh : 2 * cosh x = exp x + exp (-x) := mul_div_cancel' _ two_ne_zero' @[simp] lemma sinh_zero : sinh 0 = 0 := by simp [sinh] @[simp] lemma sinh_neg : sinh (-x) = -sinh x := by simp [sinh, exp_neg, (neg_div _ _).symm, add_mul] private lemma sinh_add_aux {a b c d : ℂ} : (a - b) * (c + d) + (a + b) * (c - d) = 2 * (a * c - b * d) := by ring lemma sinh_add : sinh (x + y) = sinh x * cosh y + cosh x * sinh y := begin rw [← mul_right_inj' (@two_ne_zero' ℂ _ _ _), two_sinh, exp_add, neg_add, exp_add, eq_comm, mul_add, ← mul_assoc, two_sinh, mul_left_comm, two_sinh, ← mul_right_inj' (@two_ne_zero' ℂ _ _ _), mul_add, mul_left_comm, two_cosh, ← mul_assoc, two_cosh], exact sinh_add_aux end @[simp] lemma cosh_zero : cosh 0 = 1 := by simp [cosh] @[simp] lemma cosh_neg : cosh (-x) = cosh x := by simp [add_comm, cosh, exp_neg] private lemma cosh_add_aux {a b c d : ℂ} : (a + b) * (c + d) + (a - b) * (c - d) = 2 * (a * c + b * d) := by ring lemma cosh_add : cosh (x + y) = cosh x * cosh y + sinh x * sinh y := begin rw [← mul_right_inj' (@two_ne_zero' ℂ _ _ _), two_cosh, exp_add, neg_add, exp_add, eq_comm, mul_add, ← mul_assoc, two_cosh, ← mul_assoc, two_sinh, ← mul_right_inj' (@two_ne_zero' ℂ _ _ _), mul_add, mul_left_comm, two_cosh, mul_left_comm, two_sinh], exact cosh_add_aux end lemma sinh_sub : sinh (x - y) = sinh x * cosh y - cosh x * sinh y := by simp [sub_eq_add_neg, sinh_add, sinh_neg, cosh_neg] lemma cosh_sub : cosh (x - y) = cosh x * cosh y - sinh x * sinh y := by simp [sub_eq_add_neg, cosh_add, sinh_neg, cosh_neg] lemma sinh_conj : sinh (conj x) = conj (sinh x) := by rw [sinh, ← conj.map_neg, exp_conj, exp_conj, ← conj.map_sub, sinh, conj.map_div, conj_bit0, conj.map_one] @[simp] lemma of_real_sinh_of_real_re (x : ℝ) : ((sinh x).re : ℂ) = sinh x := eq_conj_iff_re.1 $ by rw [← sinh_conj, conj_of_real] @[simp, norm_cast] lemma of_real_sinh (x : ℝ) : (real.sinh x : ℂ) = sinh x := of_real_sinh_of_real_re _ @[simp] lemma sinh_of_real_im (x : ℝ) : (sinh x).im = 0 := by rw [← of_real_sinh_of_real_re, of_real_im] lemma sinh_of_real_re (x : ℝ) : (sinh x).re = real.sinh x := rfl lemma cosh_conj : cosh (conj x) = conj (cosh x) := begin rw [cosh, ← conj.map_neg, exp_conj, exp_conj, ← conj.map_add, cosh, conj.map_div, conj_bit0, conj.map_one] end @[simp] lemma of_real_cosh_of_real_re (x : ℝ) : ((cosh x).re : ℂ) = cosh x := eq_conj_iff_re.1 $ by rw [← cosh_conj, conj_of_real] @[simp, norm_cast] lemma of_real_cosh (x : ℝ) : (real.cosh x : ℂ) = cosh x := of_real_cosh_of_real_re _ @[simp] lemma cosh_of_real_im (x : ℝ) : (cosh x).im = 0 := by rw [← of_real_cosh_of_real_re, of_real_im] lemma cosh_of_real_re (x : ℝ) : (cosh x).re = real.cosh x := rfl lemma tanh_eq_sinh_div_cosh : tanh x = sinh x / cosh x := rfl @[simp] lemma tanh_zero : tanh 0 = 0 := by simp [tanh] @[simp] lemma tanh_neg : tanh (-x) = -tanh x := by simp [tanh, neg_div] lemma tanh_conj : tanh (conj x) = conj (tanh x) := by rw [tanh, sinh_conj, cosh_conj, ← conj.map_div, tanh] @[simp] lemma of_real_tanh_of_real_re (x : ℝ) : ((tanh x).re : ℂ) = tanh x := eq_conj_iff_re.1 $ by rw [← tanh_conj, conj_of_real] @[simp, norm_cast] lemma of_real_tanh (x : ℝ) : (real.tanh x : ℂ) = tanh x := of_real_tanh_of_real_re _ @[simp] lemma tanh_of_real_im (x : ℝ) : (tanh x).im = 0 := by rw [← of_real_tanh_of_real_re, of_real_im] lemma tanh_of_real_re (x : ℝ) : (tanh x).re = real.tanh x := rfl lemma cosh_add_sinh : cosh x + sinh x = exp x := by rw [← mul_right_inj' (@two_ne_zero' ℂ _ _ _), mul_add, two_cosh, two_sinh, add_add_sub_cancel, two_mul] lemma sinh_add_cosh : sinh x + cosh x = exp x := by rw [add_comm, cosh_add_sinh] lemma cosh_sub_sinh : cosh x - sinh x = exp (-x) := by rw [← mul_right_inj' (@two_ne_zero' ℂ _ _ _), mul_sub, two_cosh, two_sinh, add_sub_sub_cancel, two_mul] lemma cosh_sq_sub_sinh_sq : cosh x ^ 2 - sinh x ^ 2 = 1 := by rw [sq_sub_sq, cosh_add_sinh, cosh_sub_sinh, ← exp_add, add_neg_self, exp_zero] lemma cosh_sq : cosh x ^ 2 = sinh x ^ 2 + 1 := begin rw ← cosh_sq_sub_sinh_sq x, ring end lemma sinh_sq : sinh x ^ 2 = cosh x ^ 2 - 1 := begin rw ← cosh_sq_sub_sinh_sq x, ring end lemma cosh_two_mul : cosh (2 * x) = cosh x ^ 2 + sinh x ^ 2 := by rw [two_mul, cosh_add, sq, sq] lemma sinh_two_mul : sinh (2 * x) = 2 * sinh x * cosh x := begin rw [two_mul, sinh_add], ring end lemma cosh_three_mul : cosh (3 * x) = 4 * cosh x ^ 3 - 3 * cosh x := begin have h1 : x + 2 * x = 3 * x, by ring, rw [← h1, cosh_add x (2 * x)], simp only [cosh_two_mul, sinh_two_mul], have h2 : sinh x * (2 * sinh x * cosh x) = 2 * cosh x * sinh x ^ 2, by ring, rw [h2, sinh_sq], ring end lemma sinh_three_mul : sinh (3 * x) = 4 * sinh x ^ 3 + 3 * sinh x := begin have h1 : x + 2 * x = 3 * x, by ring, rw [← h1, sinh_add x (2 * x)], simp only [cosh_two_mul, sinh_two_mul], have h2 : cosh x * (2 * sinh x * cosh x) = 2 * sinh x * cosh x ^ 2, by ring, rw [h2, cosh_sq], ring, end @[simp] lemma sin_zero : sin 0 = 0 := by simp [sin] @[simp] lemma sin_neg : sin (-x) = -sin x := by simp [sin, sub_eq_add_neg, exp_neg, (neg_div _ _).symm, add_mul] lemma two_sin : 2 * sin x = (exp (-x * I) - exp (x * I)) * I := mul_div_cancel' _ two_ne_zero' lemma two_cos : 2 * cos x = exp (x * I) + exp (-x * I) := mul_div_cancel' _ two_ne_zero' lemma sinh_mul_I : sinh (x * I) = sin x * I := by rw [← mul_right_inj' (@two_ne_zero' ℂ _ _ _), two_sinh, ← mul_assoc, two_sin, mul_assoc, I_mul_I, mul_neg_one, neg_sub, neg_mul_eq_neg_mul] lemma cosh_mul_I : cosh (x * I) = cos x := by rw [← mul_right_inj' (@two_ne_zero' ℂ _ _ _), two_cosh, two_cos, neg_mul_eq_neg_mul] lemma tanh_mul_I : tanh (x * I) = tan x * I := by rw [tanh_eq_sinh_div_cosh, cosh_mul_I, sinh_mul_I, mul_div_right_comm, tan] lemma cos_mul_I : cos (x * I) = cosh x := by rw ← cosh_mul_I; ring_nf; simp lemma sin_mul_I : sin (x * I) = sinh x * I := have h : I * sin (x * I) = -sinh x := by { rw [mul_comm, ← sinh_mul_I], ring_nf, simp }, by simpa only [neg_mul_eq_neg_mul_symm, div_I, neg_neg] using cancel_factors.cancel_factors_eq_div h I_ne_zero lemma tan_mul_I : tan (x * I) = tanh x * I := by rw [tan, sin_mul_I, cos_mul_I, mul_div_right_comm, tanh_eq_sinh_div_cosh] lemma sin_add : sin (x + y) = sin x * cos y + cos x * sin y := by rw [← mul_left_inj' I_ne_zero, ← sinh_mul_I, add_mul, add_mul, mul_right_comm, ← sinh_mul_I, mul_assoc, ← sinh_mul_I, ← cosh_mul_I, ← cosh_mul_I, sinh_add] @[simp] lemma cos_zero : cos 0 = 1 := by simp [cos] @[simp] lemma cos_neg : cos (-x) = cos x := by simp [cos, sub_eq_add_neg, exp_neg, add_comm] private lemma cos_add_aux {a b c d : ℂ} : (a + b) * (c + d) - (b - a) * (d - c) * (-1) = 2 * (a * c + b * d) := by ring lemma cos_add : cos (x + y) = cos x * cos y - sin x * sin y := by rw [← cosh_mul_I, add_mul, cosh_add, cosh_mul_I, cosh_mul_I, sinh_mul_I, sinh_mul_I, mul_mul_mul_comm, I_mul_I, mul_neg_one, sub_eq_add_neg] lemma sin_sub : sin (x - y) = sin x * cos y - cos x * sin y := by simp [sub_eq_add_neg, sin_add, sin_neg, cos_neg] lemma cos_sub : cos (x - y) = cos x * cos y + sin x * sin y := by simp [sub_eq_add_neg, cos_add, sin_neg, cos_neg] lemma sin_add_mul_I (x y : ℂ) : sin (x + y*I) = sin x * cosh y + cos x * sinh y * I := by rw [sin_add, cos_mul_I, sin_mul_I, mul_assoc] lemma sin_eq (z : ℂ) : sin z = sin z.re * cosh z.im + cos z.re * sinh z.im * I := by convert sin_add_mul_I z.re z.im; exact (re_add_im z).symm lemma cos_add_mul_I (x y : ℂ) : cos (x + y*I) = cos x * cosh y - sin x * sinh y * I := by rw [cos_add, cos_mul_I, sin_mul_I, mul_assoc] lemma cos_eq (z : ℂ) : cos z = cos z.re * cosh z.im - sin z.re * sinh z.im * I := by convert cos_add_mul_I z.re z.im; exact (re_add_im z).symm theorem sin_sub_sin : sin x - sin y = 2 * sin((x - y)/2) * cos((x + y)/2) := begin have s1 := sin_add ((x + y) / 2) ((x - y) / 2), have s2 := sin_sub ((x + y) / 2) ((x - y) / 2), rw [div_add_div_same, add_sub, add_right_comm, add_sub_cancel, half_add_self] at s1, rw [div_sub_div_same, ←sub_add, add_sub_cancel', half_add_self] at s2, rw [s1, s2], ring end theorem cos_sub_cos : cos x - cos y = -2 * sin((x + y)/2) * sin((x - y)/2) := begin have s1 := cos_add ((x + y) / 2) ((x - y) / 2), have s2 := cos_sub ((x + y) / 2) ((x - y) / 2), rw [div_add_div_same, add_sub, add_right_comm, add_sub_cancel, half_add_self] at s1, rw [div_sub_div_same, ←sub_add, add_sub_cancel', half_add_self] at s2, rw [s1, s2], ring, end lemma cos_add_cos : cos x + cos y = 2 * cos ((x + y) / 2) * cos ((x - y) / 2) := begin have h2 : (2:ℂ) ≠ 0 := by norm_num, calc cos x + cos y = cos ((x + y) / 2 + (x - y) / 2) + cos ((x + y) / 2 - (x - y) / 2) : _ ... = (cos ((x + y) / 2) * cos ((x - y) / 2) - sin ((x + y) / 2) * sin ((x - y) / 2)) + (cos ((x + y) / 2) * cos ((x - y) / 2) + sin ((x + y) / 2) * sin ((x - y) / 2)) : _ ... = 2 * cos ((x + y) / 2) * cos ((x - y) / 2) : _, { congr; field_simp [h2]; ring }, { rw [cos_add, cos_sub] }, ring, end lemma sin_conj : sin (conj x) = conj (sin x) := by rw [← mul_left_inj' I_ne_zero, ← sinh_mul_I, ← conj_neg_I, ← conj.map_mul, ← conj.map_mul, sinh_conj, mul_neg_eq_neg_mul_symm, sinh_neg, sinh_mul_I, mul_neg_eq_neg_mul_symm] @[simp] lemma of_real_sin_of_real_re (x : ℝ) : ((sin x).re : ℂ) = sin x := eq_conj_iff_re.1 $ by rw [← sin_conj, conj_of_real] @[simp, norm_cast] lemma of_real_sin (x : ℝ) : (real.sin x : ℂ) = sin x := of_real_sin_of_real_re _ @[simp] lemma sin_of_real_im (x : ℝ) : (sin x).im = 0 := by rw [← of_real_sin_of_real_re, of_real_im] lemma sin_of_real_re (x : ℝ) : (sin x).re = real.sin x := rfl lemma cos_conj : cos (conj x) = conj (cos x) := by rw [← cosh_mul_I, ← conj_neg_I, ← conj.map_mul, ← cosh_mul_I, cosh_conj, mul_neg_eq_neg_mul_symm, cosh_neg] @[simp] lemma of_real_cos_of_real_re (x : ℝ) : ((cos x).re : ℂ) = cos x := eq_conj_iff_re.1 $ by rw [← cos_conj, conj_of_real] @[simp, norm_cast] lemma of_real_cos (x : ℝ) : (real.cos x : ℂ) = cos x := of_real_cos_of_real_re _ @[simp] lemma cos_of_real_im (x : ℝ) : (cos x).im = 0 := by rw [← of_real_cos_of_real_re, of_real_im] lemma cos_of_real_re (x : ℝ) : (cos x).re = real.cos x := rfl @[simp] lemma tan_zero : tan 0 = 0 := by simp [tan] lemma tan_eq_sin_div_cos : tan x = sin x / cos x := rfl lemma tan_mul_cos {x : ℂ} (hx : cos x ≠ 0) : tan x * cos x = sin x := by rw [tan_eq_sin_div_cos, div_mul_cancel _ hx] @[simp] lemma tan_neg : tan (-x) = -tan x := by simp [tan, neg_div] lemma tan_conj : tan (conj x) = conj (tan x) := by rw [tan, sin_conj, cos_conj, ← conj.map_div, tan] @[simp] lemma of_real_tan_of_real_re (x : ℝ) : ((tan x).re : ℂ) = tan x := eq_conj_iff_re.1 $ by rw [← tan_conj, conj_of_real] @[simp, norm_cast] lemma of_real_tan (x : ℝ) : (real.tan x : ℂ) = tan x := of_real_tan_of_real_re _ @[simp] lemma tan_of_real_im (x : ℝ) : (tan x).im = 0 := by rw [← of_real_tan_of_real_re, of_real_im] lemma tan_of_real_re (x : ℝ) : (tan x).re = real.tan x := rfl lemma cos_add_sin_I : cos x + sin x * I = exp (x * I) := by rw [← cosh_add_sinh, sinh_mul_I, cosh_mul_I] lemma cos_sub_sin_I : cos x - sin x * I = exp (-x * I) := by rw [← neg_mul_eq_neg_mul, ← cosh_sub_sinh, sinh_mul_I, cosh_mul_I] @[simp] lemma sin_sq_add_cos_sq : sin x ^ 2 + cos x ^ 2 = 1 := eq.trans (by rw [cosh_mul_I, sinh_mul_I, mul_pow, I_sq, mul_neg_one, sub_neg_eq_add, add_comm]) (cosh_sq_sub_sinh_sq (x * I)) @[simp] lemma cos_sq_add_sin_sq : cos x ^ 2 + sin x ^ 2 = 1 := by rw [add_comm, sin_sq_add_cos_sq] lemma cos_two_mul' : cos (2 * x) = cos x ^ 2 - sin x ^ 2 := by rw [two_mul, cos_add, ← sq, ← sq] lemma cos_two_mul : cos (2 * x) = 2 * cos x ^ 2 - 1 := by rw [cos_two_mul', eq_sub_iff_add_eq.2 (sin_sq_add_cos_sq x), ← sub_add, sub_add_eq_add_sub, two_mul] lemma sin_two_mul : sin (2 * x) = 2 * sin x * cos x := by rw [two_mul, sin_add, two_mul, add_mul, mul_comm] lemma cos_sq : cos x ^ 2 = 1 / 2 + cos (2 * x) / 2 := by simp [cos_two_mul, div_add_div_same, mul_div_cancel_left, two_ne_zero', -one_div] lemma cos_sq' : cos x ^ 2 = 1 - sin x ^ 2 := by rw [←sin_sq_add_cos_sq x, add_sub_cancel'] lemma sin_sq : sin x ^ 2 = 1 - cos x ^ 2 := by rw [←sin_sq_add_cos_sq x, add_sub_cancel] lemma inv_one_add_tan_sq {x : ℂ} (hx : cos x ≠ 0) : (1 + tan x ^ 2)⁻¹ = cos x ^ 2 := have cos x ^ 2 ≠ 0, from pow_ne_zero 2 hx, by { rw [tan_eq_sin_div_cos, div_pow], field_simp [this] } lemma tan_sq_div_one_add_tan_sq {x : ℂ} (hx : cos x ≠ 0) : tan x ^ 2 / (1 + tan x ^ 2) = sin x ^ 2 := by simp only [← tan_mul_cos hx, mul_pow, ← inv_one_add_tan_sq hx, div_eq_mul_inv, one_mul] lemma cos_three_mul : cos (3 * x) = 4 * cos x ^ 3 - 3 * cos x := begin have h1 : x + 2 * x = 3 * x, by ring, rw [← h1, cos_add x (2 * x)], simp only [cos_two_mul, sin_two_mul, mul_add, mul_sub, mul_one, sq], have h2 : 4 * cos x ^ 3 = 2 * cos x * cos x * cos x + 2 * cos x * cos x ^ 2, by ring, rw [h2, cos_sq'], ring end lemma sin_three_mul : sin (3 * x) = 3 * sin x - 4 * sin x ^ 3 := begin have h1 : x + 2 * x = 3 * x, by ring, rw [← h1, sin_add x (2 * x)], simp only [cos_two_mul, sin_two_mul, cos_sq'], have h2 : cos x * (2 * sin x * cos x) = 2 * sin x * cos x ^ 2, by ring, rw [h2, cos_sq'], ring end lemma exp_mul_I : exp (x * I) = cos x + sin x * I := (cos_add_sin_I _).symm lemma exp_add_mul_I : exp (x + y * I) = exp x * (cos y + sin y * I) := by rw [exp_add, exp_mul_I] lemma exp_eq_exp_re_mul_sin_add_cos : exp x = exp x.re * (cos x.im + sin x.im * I) := by rw [← exp_add_mul_I, re_add_im] lemma exp_re : (exp x).re = real.exp x.re * real.cos x.im := by { rw [exp_eq_exp_re_mul_sin_add_cos], simp [exp_of_real_re, cos_of_real_re] } lemma exp_im : (exp x).im = real.exp x.re * real.sin x.im := by { rw [exp_eq_exp_re_mul_sin_add_cos], simp [exp_of_real_re, sin_of_real_re] } /-- **De Moivre's formula** -/ theorem cos_add_sin_mul_I_pow (n : ℕ) (z : ℂ) : (cos z + sin z * I) ^ n = cos (↑n * z) + sin (↑n * z) * I := begin rw [← exp_mul_I, ← exp_mul_I], induction n with n ih, { rw [pow_zero, nat.cast_zero, zero_mul, zero_mul, exp_zero] }, { rw [pow_succ', ih, nat.cast_succ, add_mul, add_mul, one_mul, exp_add] } end end complex namespace real open complex variables (x y : ℝ) @[simp] lemma exp_zero : exp 0 = 1 := by simp [real.exp] lemma exp_add : exp (x + y) = exp x * exp y := by simp [exp_add, exp] lemma exp_list_sum (l : list ℝ) : exp l.sum = (l.map exp).prod := @monoid_hom.map_list_prod (multiplicative ℝ) ℝ _ _ ⟨exp, exp_zero, exp_add⟩ l lemma exp_multiset_sum (s : multiset ℝ) : exp s.sum = (s.map exp).prod := @monoid_hom.map_multiset_prod (multiplicative ℝ) ℝ _ _ ⟨exp, exp_zero, exp_add⟩ s lemma exp_sum {α : Type*} (s : finset α) (f : α → ℝ) : exp (∑ x in s, f x) = ∏ x in s, exp (f x) := @monoid_hom.map_prod (multiplicative ℝ) α ℝ _ _ ⟨exp, exp_zero, exp_add⟩ f s lemma exp_nat_mul (x : ℝ) : ∀ n : ℕ, exp(n*x) = (exp x)^n | 0 := by rw [nat.cast_zero, zero_mul, exp_zero, pow_zero] | (nat.succ n) := by rw [pow_succ', nat.cast_add_one, add_mul, exp_add, ←exp_nat_mul, one_mul] lemma exp_ne_zero : exp x ≠ 0 := λ h, exp_ne_zero x $ by rw [exp, ← of_real_inj] at h; simp * at * lemma exp_neg : exp (-x) = (exp x)⁻¹ := by rw [← of_real_inj, exp, of_real_exp_of_real_re, of_real_neg, exp_neg, of_real_inv, of_real_exp] lemma exp_sub : exp (x - y) = exp x / exp y := by simp [sub_eq_add_neg, exp_add, exp_neg, div_eq_mul_inv] @[simp] lemma sin_zero : sin 0 = 0 := by simp [sin] @[simp] lemma sin_neg : sin (-x) = -sin x := by simp [sin, exp_neg, (neg_div _ _).symm, add_mul] lemma sin_add : sin (x + y) = sin x * cos y + cos x * sin y := by rw [← of_real_inj]; simp [sin, sin_add] @[simp] lemma cos_zero : cos 0 = 1 := by simp [cos] @[simp] lemma cos_neg : cos (-x) = cos x := by simp [cos, exp_neg] lemma cos_add : cos (x + y) = cos x * cos y - sin x * sin y := by rw ← of_real_inj; simp [cos, cos_add] lemma sin_sub : sin (x - y) = sin x * cos y - cos x * sin y := by simp [sub_eq_add_neg, sin_add, sin_neg, cos_neg] lemma cos_sub : cos (x - y) = cos x * cos y + sin x * sin y := by simp [sub_eq_add_neg, cos_add, sin_neg, cos_neg] lemma sin_sub_sin : sin x - sin y = 2 * sin((x - y)/2) * cos((x + y)/2) := begin rw ← of_real_inj, simp only [sin, cos, of_real_sin_of_real_re, of_real_sub, of_real_add, of_real_div, of_real_mul, of_real_one, of_real_bit0], convert sin_sub_sin _ _; norm_cast end theorem cos_sub_cos : cos x - cos y = -2 * sin((x + y)/2) * sin((x - y)/2) := begin rw ← of_real_inj, simp only [cos, neg_mul_eq_neg_mul_symm, of_real_sin, of_real_sub, of_real_add, of_real_cos_of_real_re, of_real_div, of_real_mul, of_real_one, of_real_neg, of_real_bit0], convert cos_sub_cos _ _, ring, end lemma cos_add_cos : cos x + cos y = 2 * cos ((x + y) / 2) * cos ((x - y) / 2) := begin rw ← of_real_inj, simp only [cos, of_real_sub, of_real_add, of_real_cos_of_real_re, of_real_div, of_real_mul, of_real_one, of_real_bit0], convert cos_add_cos _ _; norm_cast, end lemma tan_eq_sin_div_cos : tan x = sin x / cos x := by rw [← of_real_inj, of_real_tan, tan_eq_sin_div_cos, of_real_div, of_real_sin, of_real_cos] lemma tan_mul_cos {x : ℝ} (hx : cos x ≠ 0) : tan x * cos x = sin x := by rw [tan_eq_sin_div_cos, div_mul_cancel _ hx] @[simp] lemma tan_zero : tan 0 = 0 := by simp [tan] @[simp] lemma tan_neg : tan (-x) = -tan x := by simp [tan, neg_div] @[simp] lemma sin_sq_add_cos_sq : sin x ^ 2 + cos x ^ 2 = 1 := of_real_inj.1 $ by simp @[simp] lemma cos_sq_add_sin_sq : cos x ^ 2 + sin x ^ 2 = 1 := by rw [add_comm, sin_sq_add_cos_sq] lemma sin_sq_le_one : sin x ^ 2 ≤ 1 := by rw ← sin_sq_add_cos_sq x; exact le_add_of_nonneg_right (sq_nonneg _) lemma cos_sq_le_one : cos x ^ 2 ≤ 1 := by rw ← sin_sq_add_cos_sq x; exact le_add_of_nonneg_left (sq_nonneg _) lemma abs_sin_le_one : abs' (sin x) ≤ 1 := abs_le_one_iff_mul_self_le_one.2 $ by simp only [← sq, sin_sq_le_one] lemma abs_cos_le_one : abs' (cos x) ≤ 1 := abs_le_one_iff_mul_self_le_one.2 $ by simp only [← sq, cos_sq_le_one] lemma sin_le_one : sin x ≤ 1 := (abs_le.1 (abs_sin_le_one _)).2 lemma cos_le_one : cos x ≤ 1 := (abs_le.1 (abs_cos_le_one _)).2 lemma neg_one_le_sin : -1 ≤ sin x := (abs_le.1 (abs_sin_le_one _)).1 lemma neg_one_le_cos : -1 ≤ cos x := (abs_le.1 (abs_cos_le_one _)).1 lemma cos_two_mul : cos (2 * x) = 2 * cos x ^ 2 - 1 := by rw ← of_real_inj; simp [cos_two_mul] lemma cos_two_mul' : cos (2 * x) = cos x ^ 2 - sin x ^ 2 := by rw ← of_real_inj; simp [cos_two_mul'] lemma sin_two_mul : sin (2 * x) = 2 * sin x * cos x := by rw ← of_real_inj; simp [sin_two_mul] lemma cos_sq : cos x ^ 2 = 1 / 2 + cos (2 * x) / 2 := of_real_inj.1 $ by simpa using cos_sq x lemma cos_sq' : cos x ^ 2 = 1 - sin x ^ 2 := by rw [←sin_sq_add_cos_sq x, add_sub_cancel'] lemma sin_sq : sin x ^ 2 = 1 - cos x ^ 2 := eq_sub_iff_add_eq.2 $ sin_sq_add_cos_sq _ lemma abs_sin_eq_sqrt_one_sub_cos_sq (x : ℝ) : abs' (sin x) = sqrt (1 - cos x ^ 2) := by rw [← sin_sq, sqrt_sq_eq_abs] lemma abs_cos_eq_sqrt_one_sub_sin_sq (x : ℝ) : abs' (cos x) = sqrt (1 - sin x ^ 2) := by rw [← cos_sq', sqrt_sq_eq_abs] lemma inv_one_add_tan_sq {x : ℝ} (hx : cos x ≠ 0) : (1 + tan x ^ 2)⁻¹ = cos x ^ 2 := have complex.cos x ≠ 0, from mt (congr_arg re) hx, of_real_inj.1 $ by simpa using complex.inv_one_add_tan_sq this lemma tan_sq_div_one_add_tan_sq {x : ℝ} (hx : cos x ≠ 0) : tan x ^ 2 / (1 + tan x ^ 2) = sin x ^ 2 := by simp only [← tan_mul_cos hx, mul_pow, ← inv_one_add_tan_sq hx, div_eq_mul_inv, one_mul] lemma inv_sqrt_one_add_tan_sq {x : ℝ} (hx : 0 < cos x) : (sqrt (1 + tan x ^ 2))⁻¹ = cos x := by rw [← sqrt_sq hx.le, ← sqrt_inv, inv_one_add_tan_sq hx.ne'] lemma tan_div_sqrt_one_add_tan_sq {x : ℝ} (hx : 0 < cos x) : tan x / sqrt (1 + tan x ^ 2) = sin x := by rw [← tan_mul_cos hx.ne', ← inv_sqrt_one_add_tan_sq hx, div_eq_mul_inv] lemma cos_three_mul : cos (3 * x) = 4 * cos x ^ 3 - 3 * cos x := by rw ← of_real_inj; simp [cos_three_mul] lemma sin_three_mul : sin (3 * x) = 3 * sin x - 4 * sin x ^ 3 := by rw ← of_real_inj; simp [sin_three_mul] /-- The definition of `sinh` in terms of `exp`. -/ lemma sinh_eq (x : ℝ) : sinh x = (exp x - exp (-x)) / 2 := eq_div_of_mul_eq two_ne_zero $ by rw [sinh, exp, exp, complex.of_real_neg, complex.sinh, mul_two, ← complex.add_re, ← mul_two, div_mul_cancel _ (two_ne_zero' : (2 : ℂ) ≠ 0), complex.sub_re] @[simp] lemma sinh_zero : sinh 0 = 0 := by simp [sinh] @[simp] lemma sinh_neg : sinh (-x) = -sinh x := by simp [sinh, exp_neg, (neg_div _ _).symm, add_mul] lemma sinh_add : sinh (x + y) = sinh x * cosh y + cosh x * sinh y := by rw ← of_real_inj; simp [sinh_add] /-- The definition of `cosh` in terms of `exp`. -/ lemma cosh_eq (x : ℝ) : cosh x = (exp x + exp (-x)) / 2 := eq_div_of_mul_eq two_ne_zero $ by rw [cosh, exp, exp, complex.of_real_neg, complex.cosh, mul_two, ← complex.add_re, ← mul_two, div_mul_cancel _ (two_ne_zero' : (2 : ℂ) ≠ 0), complex.add_re] @[simp] lemma cosh_zero : cosh 0 = 1 := by simp [cosh] @[simp] lemma cosh_neg : cosh (-x) = cosh x := by simp [cosh, exp_neg] lemma cosh_add : cosh (x + y) = cosh x * cosh y + sinh x * sinh y := by rw ← of_real_inj; simp [cosh, cosh_add] lemma sinh_sub : sinh (x - y) = sinh x * cosh y - cosh x * sinh y := by simp [sub_eq_add_neg, sinh_add, sinh_neg, cosh_neg] lemma cosh_sub : cosh (x - y) = cosh x * cosh y - sinh x * sinh y := by simp [sub_eq_add_neg, cosh_add, sinh_neg, cosh_neg] lemma tanh_eq_sinh_div_cosh : tanh x = sinh x / cosh x := of_real_inj.1 $ by simp [tanh_eq_sinh_div_cosh] @[simp] lemma tanh_zero : tanh 0 = 0 := by simp [tanh] @[simp] lemma tanh_neg : tanh (-x) = -tanh x := by simp [tanh, neg_div] lemma cosh_add_sinh : cosh x + sinh x = exp x := by rw ← of_real_inj; simp [cosh_add_sinh] lemma sinh_add_cosh : sinh x + cosh x = exp x := by rw ← of_real_inj; simp [sinh_add_cosh] lemma cosh_sq_sub_sinh_sq (x : ℝ) : cosh x ^ 2 - sinh x ^ 2 = 1 := by rw ← of_real_inj; simp [cosh_sq_sub_sinh_sq] lemma cosh_sq : cosh x ^ 2 = sinh x ^ 2 + 1 := by rw ← of_real_inj; simp [cosh_sq] lemma sinh_sq : sinh x ^ 2 = cosh x ^ 2 - 1 := by rw ← of_real_inj; simp [sinh_sq] lemma cosh_two_mul : cosh (2 * x) = cosh x ^ 2 + sinh x ^ 2 := by rw ← of_real_inj; simp [cosh_two_mul] lemma sinh_two_mul : sinh (2 * x) = 2 * sinh x * cosh x := by rw ← of_real_inj; simp [sinh_two_mul] lemma cosh_three_mul : cosh (3 * x) = 4 * cosh x ^ 3 - 3 * cosh x := by rw ← of_real_inj; simp [cosh_three_mul] lemma sinh_three_mul : sinh (3 * x) = 4 * sinh x ^ 3 + 3 * sinh x := by rw ← of_real_inj; simp [sinh_three_mul] open is_absolute_value /- TODO make this private and prove ∀ x -/ lemma add_one_le_exp_of_nonneg {x : ℝ} (hx : 0 ≤ x) : x + 1 ≤ exp x := calc x + 1 ≤ lim (⟨(λ n : ℕ, ((exp' x) n).re), is_cau_seq_re (exp' x)⟩ : cau_seq ℝ abs') : le_lim (cau_seq.le_of_exists ⟨2, λ j hj, show x + (1 : ℝ) ≤ (∑ m in range j, (x ^ m / m! : ℂ)).re, from have h₁ : (((λ m : ℕ, (x ^ m / m! : ℂ)) ∘ nat.succ) 0).re = x, by simp, have h₂ : ((x : ℂ) ^ 0 / 0!).re = 1, by simp, begin rw [← nat.sub_add_cancel hj, sum_range_succ', sum_range_succ', add_re, add_re, h₁, h₂, add_assoc, ← @sum_hom _ _ _ _ _ _ _ complex.re (is_add_group_hom.to_is_add_monoid_hom _)], refine le_add_of_nonneg_of_le (sum_nonneg (λ m hm, _)) (le_refl _), rw [← of_real_pow, ← of_real_nat_cast, ← of_real_div, of_real_re], exact div_nonneg (pow_nonneg hx _) (nat.cast_nonneg _), end⟩) ... = exp x : by rw [exp, complex.exp, ← cau_seq_re, lim_re] lemma one_le_exp {x : ℝ} (hx : 0 ≤ x) : 1 ≤ exp x := by linarith [add_one_le_exp_of_nonneg hx] lemma exp_pos (x : ℝ) : 0 < exp x := (le_total 0 x).elim (lt_of_lt_of_le zero_lt_one ∘ one_le_exp) (λ h, by rw [← neg_neg x, real.exp_neg]; exact inv_pos.2 (lt_of_lt_of_le zero_lt_one (one_le_exp (neg_nonneg.2 h)))) @[simp] lemma abs_exp (x : ℝ) : abs' (exp x) = exp x := abs_of_pos (exp_pos _) lemma exp_strict_mono : strict_mono exp := λ x y h, by rw [← sub_add_cancel y x, real.exp_add]; exact (lt_mul_iff_one_lt_left (exp_pos _)).2 (lt_of_lt_of_le (by linarith) (add_one_le_exp_of_nonneg (by linarith))) @[mono] lemma exp_monotone : ∀ {x y : ℝ}, x ≤ y → exp x ≤ exp y := exp_strict_mono.monotone @[simp] lemma exp_lt_exp {x y : ℝ} : exp x < exp y ↔ x < y := exp_strict_mono.lt_iff_lt @[simp] lemma exp_le_exp {x y : ℝ} : exp x ≤ exp y ↔ x ≤ y := exp_strict_mono.le_iff_le lemma exp_injective : function.injective exp := exp_strict_mono.injective @[simp] lemma exp_eq_exp {x y : ℝ} : exp x = exp y ↔ x = y := exp_injective.eq_iff @[simp] lemma exp_eq_one_iff : exp x = 1 ↔ x = 0 := by rw [← exp_zero, exp_injective.eq_iff] @[simp] lemma one_lt_exp_iff {x : ℝ} : 1 < exp x ↔ 0 < x := by rw [← exp_zero, exp_lt_exp] @[simp] lemma exp_lt_one_iff {x : ℝ} : exp x < 1 ↔ x < 0 := by rw [← exp_zero, exp_lt_exp] @[simp] lemma exp_le_one_iff {x : ℝ} : exp x ≤ 1 ↔ x ≤ 0 := exp_zero ▸ exp_le_exp @[simp] lemma one_le_exp_iff {x : ℝ} : 1 ≤ exp x ↔ 0 ≤ x := exp_zero ▸ exp_le_exp /-- `real.cosh` is always positive -/ lemma cosh_pos (x : ℝ) : 0 < real.cosh x := (cosh_eq x).symm ▸ half_pos (add_pos (exp_pos x) (exp_pos (-x))) end real namespace complex lemma sum_div_factorial_le {α : Type*} [linear_ordered_field α] (n j : ℕ) (hn : 0 < n) : ∑ m in filter (λ k, n ≤ k) (range j), (1 / m! : α) ≤ n.succ / (n! * n) := calc ∑ m in filter (λ k, n ≤ k) (range j), (1 / m! : α) = ∑ m in range (j - n), 1 / (m + n)! : sum_bij (λ m _, m - n) (λ m hm, mem_range.2 $ (nat.sub_lt_sub_right_iff (by simp at hm; tauto)).2 (by simp at hm; tauto)) (λ m hm, by rw nat.sub_add_cancel; simp at *; tauto) (λ a₁ a₂ ha₁ ha₂ h, by rwa [nat.sub_eq_iff_eq_add, ← nat.sub_add_comm, eq_comm, nat.sub_eq_iff_eq_add, add_left_inj, eq_comm] at h; simp at *; tauto) (λ b hb, ⟨b + n, mem_filter.2 ⟨mem_range.2 $ nat.add_lt_of_lt_sub_right (mem_range.1 hb), nat.le_add_left _ _⟩, by rw nat.add_sub_cancel⟩) ... ≤ ∑ m in range (j - n), (n! * n.succ ^ m)⁻¹ : begin refine sum_le_sum (assume m n, _), rw [one_div, inv_le_inv], { rw [← nat.cast_pow, ← nat.cast_mul, nat.cast_le, add_comm], exact nat.factorial_mul_pow_le_factorial }, { exact nat.cast_pos.2 (nat.factorial_pos _) }, { exact mul_pos (nat.cast_pos.2 (nat.factorial_pos _)) (pow_pos (nat.cast_pos.2 (nat.succ_pos _)) _) }, end ... = n!⁻¹ * ∑ m in range (j - n), n.succ⁻¹ ^ m : by simp [mul_inv', mul_sum.symm, sum_mul.symm, -nat.factorial_succ, mul_comm, inv_pow'] ... = (n.succ - n.succ * n.succ⁻¹ ^ (j - n)) / (n! * n) : have h₁ : (n.succ : α) ≠ 1, from @nat.cast_one α _ _ ▸ mt nat.cast_inj.1 (mt nat.succ.inj (pos_iff_ne_zero.1 hn)), have h₂ : (n.succ : α) ≠ 0, from nat.cast_ne_zero.2 (nat.succ_ne_zero _), have h₃ : (n! * n : α) ≠ 0, from mul_ne_zero (nat.cast_ne_zero.2 (pos_iff_ne_zero.1 (nat.factorial_pos _))) (nat.cast_ne_zero.2 (pos_iff_ne_zero.1 hn)), have h₄ : (n.succ - 1 : α) = n, by simp, by rw [← geom_sum_def, geom_sum_inv h₁ h₂, eq_div_iff_mul_eq h₃, mul_comm _ (n! * n : α), ← mul_assoc (n!⁻¹ : α), ← mul_inv_rev', h₄, ← mul_assoc (n! * n : α), mul_comm (n : α) n!, mul_inv_cancel h₃]; simp [mul_add, add_mul, mul_assoc, mul_comm] ... ≤ n.succ / (n! * n) : begin refine iff.mpr (div_le_div_right (mul_pos _ _)) _, exact nat.cast_pos.2 (nat.factorial_pos _), exact nat.cast_pos.2 hn, exact sub_le_self _ (mul_nonneg (nat.cast_nonneg _) (pow_nonneg (inv_nonneg.2 (nat.cast_nonneg _)) _)) end lemma exp_bound {x : ℂ} (hx : abs x ≤ 1) {n : ℕ} (hn : 0 < n) : abs (exp x - ∑ m in range n, x ^ m / m!) ≤ abs x ^ n * (n.succ * (n! * n)⁻¹) := begin rw [← lim_const (∑ m in range n, _), exp, sub_eq_add_neg, ← lim_neg, lim_add, ← lim_abs], refine lim_le (cau_seq.le_of_exists ⟨n, λ j hj, _⟩), simp_rw ← sub_eq_add_neg, show abs (∑ m in range j, x ^ m / m! - ∑ m in range n, x ^ m / m!) ≤ abs x ^ n * (n.succ * (n! * n)⁻¹), rw sum_range_sub_sum_range hj, exact calc abs (∑ m in (range j).filter (λ k, n ≤ k), (x ^ m / m! : ℂ)) = abs (∑ m in (range j).filter (λ k, n ≤ k), (x ^ n * (x ^ (m - n) / m!) : ℂ)) : begin refine congr_arg abs (sum_congr rfl (λ m hm, _)), rw [mem_filter, mem_range] at hm, rw [← mul_div_assoc, ← pow_add, nat.add_sub_cancel' hm.2] end ... ≤ ∑ m in filter (λ k, n ≤ k) (range j), abs (x ^ n * (_ / m!)) : abv_sum_le_sum_abv _ _ ... ≤ ∑ m in filter (λ k, n ≤ k) (range j), abs x ^ n * (1 / m!) : begin refine sum_le_sum (λ m hm, _), rw [abs_mul, abv_pow abs, abs_div, abs_cast_nat], refine mul_le_mul_of_nonneg_left ((div_le_div_right _).2 _) _, exact nat.cast_pos.2 (nat.factorial_pos _), rw abv_pow abs, exact (pow_le_one _ (abs_nonneg _) hx), exact pow_nonneg (abs_nonneg _) _ end ... = abs x ^ n * (∑ m in (range j).filter (λ k, n ≤ k), (1 / m! : ℝ)) : by simp [abs_mul, abv_pow abs, abs_div, mul_sum.symm] ... ≤ abs x ^ n * (n.succ * (n! * n)⁻¹) : mul_le_mul_of_nonneg_left (sum_div_factorial_le _ _ hn) (pow_nonneg (abs_nonneg _) _) end lemma abs_exp_sub_one_le {x : ℂ} (hx : abs x ≤ 1) : abs (exp x - 1) ≤ 2 * abs x := calc abs (exp x - 1) = abs (exp x - ∑ m in range 1, x ^ m / m!) : by simp [sum_range_succ] ... ≤ abs x ^ 1 * ((nat.succ 1) * (1! * (1 : ℕ))⁻¹) : exp_bound hx dec_trivial ... = 2 * abs x : by simp [two_mul, mul_two, mul_add, mul_comm] lemma abs_exp_sub_one_sub_id_le {x : ℂ} (hx : abs x ≤ 1) : abs (exp x - 1 - x) ≤ (abs x)^2 := calc abs (exp x - 1 - x) = abs (exp x - ∑ m in range 2, x ^ m / m!) : by simp [sub_eq_add_neg, sum_range_succ_comm, add_assoc] ... ≤ (abs x)^2 * (nat.succ 2 * (2! * (2 : ℕ))⁻¹) : exp_bound hx dec_trivial ... ≤ (abs x)^2 * 1 : mul_le_mul_of_nonneg_left (by norm_num) (sq_nonneg (abs x)) ... = (abs x)^2 : by rw [mul_one] end complex namespace real open complex finset lemma exp_bound {x : ℝ} (hx : abs' x ≤ 1) {n : ℕ} (hn : 0 < n) : abs' (exp x - ∑ m in range n, x ^ m / m!) ≤ abs' x ^ n * (n.succ / (n! * n)) := begin have hxc : complex.abs x ≤ 1, by exact_mod_cast hx, convert exp_bound hxc hn; norm_cast end /-- A finite initial segment of the exponential series, followed by an arbitrary tail. For fixed `n` this is just a linear map wrt `r`, and each map is a simple linear function of the previous (see `exp_near_succ`), with `exp_near n x r ⟶ exp x` as `n ⟶ ∞`, for any `r`. -/ def exp_near (n : ℕ) (x r : ℝ) : ℝ := ∑ m in range n, x ^ m / m! + x ^ n / n! * r @[simp] theorem exp_near_zero (x r) : exp_near 0 x r = r := by simp [exp_near] @[simp] theorem exp_near_succ (n x r) : exp_near (n + 1) x r = exp_near n x (1 + x / (n+1) * r) := by simp [exp_near, range_succ, mul_add, add_left_comm, add_assoc, pow_succ, div_eq_mul_inv, mul_inv']; ac_refl theorem exp_near_sub (n x r₁ r₂) : exp_near n x r₁ - exp_near n x r₂ = x ^ n / n! * (r₁ - r₂) := by simp [exp_near, mul_sub] lemma exp_approx_end (n m : ℕ) (x : ℝ) (e₁ : n + 1 = m) (h : abs' x ≤ 1) : abs' (exp x - exp_near m x 0) ≤ abs' x ^ m / m! * ((m+1)/m) := by { simp [exp_near], convert exp_bound h _ using 1, field_simp [mul_comm], linarith } lemma exp_approx_succ {n} {x a₁ b₁ : ℝ} (m : ℕ) (e₁ : n + 1 = m) (a₂ b₂ : ℝ) (e : abs' (1 + x / m * a₂ - a₁) ≤ b₁ - abs' x / m * b₂) (h : abs' (exp x - exp_near m x a₂) ≤ abs' x ^ m / m! * b₂) : abs' (exp x - exp_near n x a₁) ≤ abs' x ^ n / n! * b₁ := begin refine (_root_.abs_sub_le _ _ _).trans ((add_le_add_right h _).trans _), subst e₁, rw [exp_near_succ, exp_near_sub, _root_.abs_mul], convert mul_le_mul_of_nonneg_left (le_sub_iff_add_le'.1 e) _, { simp [mul_add, pow_succ', div_eq_mul_inv, _root_.abs_mul, _root_.abs_inv, ← pow_abs, mul_inv'], ac_refl }, { simp [_root_.div_nonneg, _root_.abs_nonneg] } end lemma exp_approx_end' {n} {x a b : ℝ} (m : ℕ) (e₁ : n + 1 = m) (rm : ℝ) (er : ↑m = rm) (h : abs' x ≤ 1) (e : abs' (1 - a) ≤ b - abs' x / rm * ((rm+1)/rm)) : abs' (exp x - exp_near n x a) ≤ abs' x ^ n / n! * b := by subst er; exact exp_approx_succ _ e₁ _ _ (by simpa using e) (exp_approx_end _ _ _ e₁ h) lemma exp_1_approx_succ_eq {n} {a₁ b₁ : ℝ} {m : ℕ} (en : n + 1 = m) {rm : ℝ} (er : ↑m = rm) (h : abs' (exp 1 - exp_near m 1 ((a₁ - 1) * rm)) ≤ abs' 1 ^ m / m! * (b₁ * rm)) : abs' (exp 1 - exp_near n 1 a₁) ≤ abs' 1 ^ n / n! * b₁ := begin subst er, refine exp_approx_succ _ en _ _ _ h, field_simp [show (m : ℝ) ≠ 0, by norm_cast; linarith], end lemma exp_approx_start (x a b : ℝ) (h : abs' (exp x - exp_near 0 x a) ≤ abs' x ^ 0 / 0! * b) : abs' (exp x - a) ≤ b := by simpa using h lemma cos_bound {x : ℝ} (hx : abs' x ≤ 1) : abs' (cos x - (1 - x ^ 2 / 2)) ≤ abs' x ^ 4 * (5 / 96) := calc abs' (cos x - (1 - x ^ 2 / 2)) = abs (complex.cos x - (1 - x ^ 2 / 2)) : by rw ← abs_of_real; simp [of_real_bit0, of_real_one, of_real_inv] ... = abs ((complex.exp (x * I) + complex.exp (-x * I) - (2 - x ^ 2)) / 2) : by simp [complex.cos, sub_div, add_div, neg_div, div_self (@two_ne_zero' ℂ _ _ _)] ... = abs (((complex.exp (x * I) - ∑ m in range 4, (x * I) ^ m / m!) + ((complex.exp (-x * I) - ∑ m in range 4, (-x * I) ^ m / m!))) / 2) : congr_arg abs (congr_arg (λ x : ℂ, x / 2) begin simp only [sum_range_succ], simp [pow_succ], apply complex.ext; simp [div_eq_mul_inv, norm_sq]; ring end) ... ≤ abs ((complex.exp (x * I) - ∑ m in range 4, (x * I) ^ m / m!) / 2) + abs ((complex.exp (-x * I) - ∑ m in range 4, (-x * I) ^ m / m!) / 2) : by rw add_div; exact abs_add _ _ ... = (abs ((complex.exp (x * I) - ∑ m in range 4, (x * I) ^ m / m!)) / 2 + abs ((complex.exp (-x * I) - ∑ m in range 4, (-x * I) ^ m / m!)) / 2) : by simp [complex.abs_div] ... ≤ ((complex.abs (x * I) ^ 4 * (nat.succ 4 * (4! * (4 : ℕ))⁻¹)) / 2 + (complex.abs (-x * I) ^ 4 * (nat.succ 4 * (4! * (4 : ℕ))⁻¹)) / 2) : add_le_add ((div_le_div_right (by norm_num)).2 (complex.exp_bound (by simpa) dec_trivial)) ((div_le_div_right (by norm_num)).2 (complex.exp_bound (by simpa) dec_trivial)) ... ≤ abs' x ^ 4 * (5 / 96) : by norm_num; simp [mul_assoc, mul_comm, mul_left_comm, mul_div_assoc] lemma sin_bound {x : ℝ} (hx : abs' x ≤ 1) : abs' (sin x - (x - x ^ 3 / 6)) ≤ abs' x ^ 4 * (5 / 96) := calc abs' (sin x - (x - x ^ 3 / 6)) = abs (complex.sin x - (x - x ^ 3 / 6)) : by rw ← abs_of_real; simp [of_real_bit0, of_real_one, of_real_inv] ... = abs (((complex.exp (-x * I) - complex.exp (x * I)) * I - (2 * x - x ^ 3 / 3)) / 2) : by simp [complex.sin, sub_div, add_div, neg_div, mul_div_cancel_left _ (@two_ne_zero' ℂ _ _ _), div_div_eq_div_mul, show (3 : ℂ) * 2 = 6, by norm_num] ... = abs ((((complex.exp (-x * I) - ∑ m in range 4, (-x * I) ^ m / m!) - (complex.exp (x * I) - ∑ m in range 4, (x * I) ^ m / m!)) * I) / 2) : congr_arg abs (congr_arg (λ x : ℂ, x / 2) begin simp only [sum_range_succ], simp [pow_succ], apply complex.ext; simp [div_eq_mul_inv, norm_sq]; ring end) ... ≤ abs ((complex.exp (-x * I) - ∑ m in range 4, (-x * I) ^ m / m!) * I / 2) + abs (-((complex.exp (x * I) - ∑ m in range 4, (x * I) ^ m / m!) * I) / 2) : by rw [sub_mul, sub_eq_add_neg, add_div]; exact abs_add _ _ ... = (abs ((complex.exp (x * I) - ∑ m in range 4, (x * I) ^ m / m!)) / 2 + abs ((complex.exp (-x * I) - ∑ m in range 4, (-x * I) ^ m / m!)) / 2) : by simp [add_comm, complex.abs_div, complex.abs_mul] ... ≤ ((complex.abs (x * I) ^ 4 * (nat.succ 4 * (4! * (4 : ℕ))⁻¹)) / 2 + (complex.abs (-x * I) ^ 4 * (nat.succ 4 * (4! * (4 : ℕ))⁻¹)) / 2) : add_le_add ((div_le_div_right (by norm_num)).2 (complex.exp_bound (by simpa) dec_trivial)) ((div_le_div_right (by norm_num)).2 (complex.exp_bound (by simpa) dec_trivial)) ... ≤ abs' x ^ 4 * (5 / 96) : by norm_num; simp [mul_assoc, mul_comm, mul_left_comm, mul_div_assoc] lemma cos_pos_of_le_one {x : ℝ} (hx : abs' x ≤ 1) : 0 < cos x := calc 0 < (1 - x ^ 2 / 2) - abs' x ^ 4 * (5 / 96) : sub_pos.2 $ lt_sub_iff_add_lt.2 (calc abs' x ^ 4 * (5 / 96) + x ^ 2 / 2 ≤ 1 * (5 / 96) + 1 / 2 : add_le_add (mul_le_mul_of_nonneg_right (pow_le_one _ (abs_nonneg _) hx) (by norm_num)) ((div_le_div_right (by norm_num)).2 (by rw [sq, ← abs_mul_self, _root_.abs_mul]; exact mul_le_one hx (abs_nonneg _) hx)) ... < 1 : by norm_num) ... ≤ cos x : sub_le.1 (abs_sub_le_iff.1 (cos_bound hx)).2 lemma sin_pos_of_pos_of_le_one {x : ℝ} (hx0 : 0 < x) (hx : x ≤ 1) : 0 < sin x := calc 0 < x - x ^ 3 / 6 - abs' x ^ 4 * (5 / 96) : sub_pos.2 $ lt_sub_iff_add_lt.2 (calc abs' x ^ 4 * (5 / 96) + x ^ 3 / 6 ≤ x * (5 / 96) + x / 6 : add_le_add (mul_le_mul_of_nonneg_right (calc abs' x ^ 4 ≤ abs' x ^ 1 : pow_le_pow_of_le_one (abs_nonneg _) (by rwa _root_.abs_of_nonneg (le_of_lt hx0)) dec_trivial ... = x : by simp [_root_.abs_of_nonneg (le_of_lt (hx0))]) (by norm_num)) ((div_le_div_right (by norm_num)).2 (calc x ^ 3 ≤ x ^ 1 : pow_le_pow_of_le_one (le_of_lt hx0) hx dec_trivial ... = x : pow_one _)) ... < x : by linarith) ... ≤ sin x : sub_le.1 (abs_sub_le_iff.1 (sin_bound (by rwa [_root_.abs_of_nonneg (le_of_lt hx0)]))).2 lemma sin_pos_of_pos_of_le_two {x : ℝ} (hx0 : 0 < x) (hx : x ≤ 2) : 0 < sin x := have x / 2 ≤ 1, from (div_le_iff (by norm_num)).mpr (by simpa), calc 0 < 2 * sin (x / 2) * cos (x / 2) : mul_pos (mul_pos (by norm_num) (sin_pos_of_pos_of_le_one (half_pos hx0) this)) (cos_pos_of_le_one (by rwa [_root_.abs_of_nonneg (le_of_lt (half_pos hx0))])) ... = sin x : by rw [← sin_two_mul, two_mul, add_halves] lemma cos_one_le : cos 1 ≤ 2 / 3 := calc cos 1 ≤ abs' (1 : ℝ) ^ 4 * (5 / 96) + (1 - 1 ^ 2 / 2) : sub_le_iff_le_add.1 (abs_sub_le_iff.1 (cos_bound (by simp))).1 ... ≤ 2 / 3 : by norm_num lemma cos_one_pos : 0 < cos 1 := cos_pos_of_le_one (le_of_eq abs_one) lemma cos_two_neg : cos 2 < 0 := calc cos 2 = cos (2 * 1) : congr_arg cos (mul_one _).symm ... = _ : real.cos_two_mul 1 ... ≤ 2 * (2 / 3) ^ 2 - 1 : sub_le_sub_right (mul_le_mul_of_nonneg_left (by { rw [sq, sq], exact mul_self_le_mul_self (le_of_lt cos_one_pos) cos_one_le }) zero_le_two) _ ... < 0 : by norm_num end real namespace complex lemma abs_cos_add_sin_mul_I (x : ℝ) : abs (cos x + sin x * I) = 1 := have _ := real.sin_sq_add_cos_sq x, by simp [add_comm, abs, norm_sq, sq, *, sin_of_real_re, cos_of_real_re, mul_re] at * lemma abs_exp_eq_iff_re_eq {x y : ℂ} : abs (exp x) = abs (exp y) ↔ x.re = y.re := by rw [exp_eq_exp_re_mul_sin_add_cos, exp_eq_exp_re_mul_sin_add_cos y, abs_mul, abs_mul, abs_cos_add_sin_mul_I, abs_cos_add_sin_mul_I, ← of_real_exp, ← of_real_exp, abs_of_nonneg (le_of_lt (real.exp_pos _)), abs_of_nonneg (le_of_lt (real.exp_pos _)), mul_one, mul_one]; exact ⟨λ h, real.exp_injective h, congr_arg _⟩ @[simp] lemma abs_exp_of_real (x : ℝ) : abs (exp x) = real.exp x := by rw [← of_real_exp]; exact abs_of_nonneg (le_of_lt (real.exp_pos _)) end complex
aca247a2c4b3d5e9e30f6e95f1ef23ddab49d9bf
297c4ceafbbaed2a59b6215504d09e6bf201a2ee
/kruskal_final/kruskal.lean
e51ebdbf4c2b7cceab527b17206136b1b83e4014
[]
no_license
minchaowu/Kruskal.lean3
559c91b82033ce44ea61593adcec9cfff725c88d
a14516f47b21e636e9df914fc6ebe64cbe5cd38d
refs/heads/master
1,611,010,001,429
1,497,935,421,000
1,497,935,421,000
82,000,982
1
0
null
null
null
null
UTF-8
Lean
false
false
13,080
lean
import .higman open nat function prod set fin classical subtype namespace list section -- Suml variables {A : Type} definition Suml (f : A → ℕ) : list A → ℕ | [] := 0 | (a :: ls) := f a + Suml ls theorem le_add_of_le {a b c : ℕ} : a ≤ b → a ≤ b + c := begin induction c with n ih, intro,super, intro h2, apply le_succ_of_le, exact ih h2 end theorem le_of_mem_Suml {f : A → ℕ} {a : A} {l : list A} : a ∈ l → f a ≤ Suml f l := begin induction l with b ls ih, intro h, exact absurd h (not_mem_nil _), dsimp [Suml], intro h, assert h' : a = b ∨ a ∈ ls, exact h, cases h' with l r, rw l, apply le_add_right, rw add_comm,apply le_add_of_le, exact ih r end end def upto : nat → list nat | 0 := [] | (n+1) := n :: upto n def dmap {α β : Type} (p : α → Prop) [h : decidable_pred p] (f : Π a, p a → β) : list α → list β | [] := [] | (a::l) := if P : (p a) then cons (f a P) (dmap l) else (dmap l) theorem mem_upto_succ_of_mem_upto {n i : nat} : i ∈ upto n → i ∈ upto (succ n) := assume i, mem_cons_of_mem _ i theorem mem_upto_of_lt : ∀ ⦃n i : nat⦄, i < n → i ∈ upto n | 0 := λ i h, absurd h (not_lt_zero i) | (succ n) := λ i h, begin cases nat.lt_or_eq_of_le (le_of_lt_succ h) with ilt ieq, { apply mem_upto_succ_of_mem_upto, apply mem_upto_of_lt ilt }, simp [ieq], super end section dmap variables {α β : Type} variable {p : α → Prop} variable [h : decidable_pred p] include h variable {f : Π a, p a → β} lemma dmap_nil : dmap p f [] = [] := rfl lemma dmap_cons_of_pos {a : α} (P : p a) : ∀ l, dmap p f (a::l) = (f a P) :: dmap p f l := λ l, dif_pos P lemma dmap_cons_of_neg {a : α} (P : ¬ p a) : ∀ l, dmap p f (a::l) = dmap p f l := λ l, dif_neg P lemma mem_dmap : ∀ {l : list α} {a} (Pa : p a), a ∈ l → (f a Pa) ∈ dmap p f l | [] := take a Pa Pinnil, absurd Pinnil (not_mem_nil _) | (a::l) := take b Pb Pbin, or.elim (eq_or_mem_of_mem_cons Pbin) (assume Pbeqa, begin rw [eq.symm Pbeqa, dmap_cons_of_pos Pb], apply mem_cons_self end) (assume Pbinl, if pa : p a then begin rw [dmap_cons_of_pos pa], apply mem_cons_of_mem, exact mem_dmap Pb Pbinl end else begin rw [dmap_cons_of_neg pa], exact mem_dmap Pb Pbinl end) end dmap end list namespace fin open list attribute [simp] private theorem length_nil {A : Type} : length (@nil A) = 0 := rfl lemma val_mk (n i : nat) (Plt : i < n) : fin.val (fin.mk i Plt) = i := rfl def upto (n : ℕ) : list (fin n) := dmap (λ i, i < n) fin.mk (list.upto n) lemma mem_upto (n : nat) : ∀ (i : fin n), i ∈ upto n := take i, fin.rec_on i (take ival Piltn, have ival ∈ list.upto n, from mem_upto_of_lt Piltn, mem_dmap Piltn this) end fin namespace kruskal -- universe u inductive finite_tree (Q : Type): Type | cons : Q → list finite_tree → finite_tree open finite_tree -- def embeds' {Q : Type} [has_le Q] : finite_tree Q → finite_tree Q → Prop -- | (cons a l) (cons b l') := sorry -- ∃ l₁, l₁ ∈ l' ∧ embeds' (cons a l) l₁ open finite_tree list function #check @sublist' inductive embeds {Q : Type} [has_le Q] : finite_tree Q → finite_tree Q → Prop | embeds_subtree (t t' : finite_tree Q) (a : Q) (l' : list (finite_tree Q)) : t' ∈ l' → embeds t t' → embeds t (cons a l') | embeds_par (a a' : Q) (l l' : list (finite_tree Q)) : a ≤ a' → @sublist' _ ⟨embeds⟩ l l' → embeds (cons a l) (cons a' l') -- def size' {Q : Type} : finite_tree Q → ℕ -- | (cons a l) := end kruskal -- inductive finite_tree (Q : Type): Type -- | cons : Π {n : ℕ}, Q → (fin n → finite_tree) → finite_tree -- open finite_tree -- def size {Q : Type} : finite_tree Q → ℕ -- | (@cons ._ n _ ts) := list.Suml (λ i, size (ts i)) (fin.upto n) + 1 -- theorem lt_of_size_branches_aux {Q : Type} {n : ℕ} (ts : fin n → finite_tree Q) (k : fin n) : -- size (ts k) < list.Suml (λ i, size (ts i)) (fin.upto n) + 1 := -- begin -- assert kin : k ∈ fin.upto n, exact fin.mem_upto n k, -- assert h : size (ts k) ≤ list.Suml (λ i, size (ts i)) (fin.upto n), -- apply list.le_of_mem_Suml kin, -- apply lt_succ_of_le, assumption -- end -- def embeds {Q : Type} [has_le Q] : finite_tree Q → finite_tree Q → Prop -- | (@cons ._ n a ts) (@cons ._ m b us) := (∃ j, embeds (cons a ts) (us j)) ∨ (a ≤ b ∧ ∃ f, injective f ∧ ∀ i, embeds (ts i) (us (f i))) -- infix ` ≼ `:50 := embeds -- \preceq -- def {u} fin_zero_absurd {α : Sort u} (i : fin 0) : α := -- absurd i.2 (not_lt_zero i.1) -- -- def branches_aux {Q : Type} {n : ℕ} (ts : fin n → finite_tree Q) : set (finite_tree Q × ℕ) := -- -- {x : finite_tree Q × ℕ | ∃ a : fin n, ts a = x.1 ∧ val a = x.2} -- def branches_aux {Q : Type} {n : ℕ} (ts : fin n → finite_tree Q) : list (finite_tree Q) := -- list.map ts (fin.upto n) -- definition branches {Q : Type} : finite_tree Q → list (finite_tree Q) -- | (@cons ._ n _ ts) := branches_aux ts -- -- definition branches {Q : Type} : finite_tree Q → set (finite_tree Q × ℕ) -- -- | (@cons ._ n _ ts) := branches_aux ts -- section -- variable {Q : Type} -- variable [quasiorder Q] -- theorem cons_embeds_cons_left {m n : ℕ} {a b : Q} {ss : fin m → finite_tree Q} {ts : fin n → finite_tree Q} -- {j : fin n} (H : cons a ss ≼ ts j) : -- cons a ss ≼ cons b ts := -- begin -- dsimp [embeds], apply or.inl,existsi j, exact H -- end -- theorem cons_embeds_cons_right {m n : ℕ} {a b : Q} {ss : fin m → finite_tree Q} {ts : fin n → finite_tree Q} -- {f : fin m → fin n} (H : a ≤ b) (injf : injective f) (Hf : ∀ i, ss i ≼ ts (f i)) : -- cons a ss ≼ cons b ts := -- begin -- dsimp [embeds], apply or.inr, split, exact H, -- existsi f, apply (and.intro injf Hf) -- end -- theorem embeds_refl (t : finite_tree Q) : t ≼ t := -- begin -- induction t with n b ts ih, -- dsimp [embeds], apply or.inr, split, apply quasiorder.refl, -- existsi id, apply and.intro, exact injective_id, exact ih -- end -- theorem embeds_trans_aux : ∀ {u s t : finite_tree Q}, t ≼ u → s ≼ t → s ≼ u := -- begin -- intro u, -- induction u with ul ua us ihu, -- intros s t, cases s with sl sa ss, -- cases t with tl ta ts, -- intro H₁, dsimp [embeds] at H₁, cases H₁ with H₁₁ H₁₂, -- cases H₁₁ with i H₁₁, intro H₂, -- apply cons_embeds_cons_left (ihu _ H₁₁ H₂), -- cases H₁₂ with hab H₁₂r, cases H₁₂r with f Hf, -- cases Hf with injf Hf, -- intro H₂, dsimp [embeds] at H₂, cases H₂ with H₂₁ H₂₂, -- cases H₂₁ with j H₂₁, -- apply cons_embeds_cons_left (ihu _ (Hf j) H₂₁), -- cases H₂₂ with Hab H₂₂r, cases H₂₂r with g Hg, -- cases Hg with injg Hg, -- apply cons_embeds_cons_right, -- apply quasiorder.trans Hab hab, -- apply injective_comp injf injg, -- intro i, apply ihu _ (Hf (g i)) (Hg i) -- end -- theorem embeds_trans {s t u : finite_tree Q} (H₁ : s ≼ t) (H₂ : t ≼ u) : s ≼ u := -- embeds_trans_aux H₂ H₁ -- theorem embeds_of_branches {t : finite_tree Q} {T : finite_tree Q} : t ∈ (branches T) → t ≼ T := -- begin -- cases T with n a ts, -- cases t with tn ta tss, -- intro H, dsimp [embeds], -- dsimp [branches] at H, -- dsimp [branches_aux] at H, -- end -- end -- theorem lt_of_size_of_branches {Q : Type} {t : finite_tree Q × ℕ} {T : finite_tree Q} : t ∈ branches T → size t.1 < size T := -- begin -- cases T with n a ts, -- intro h, -- assert h' : ∃ i, ts i = t.1, cases h with b hb, -- {exact exists.intro b hb^.left}, -- cases h' with c hc, rw -hc, apply lt_of_size_branches_aux -- end -- noncomputable theory -- section -- parameter {Q : Type} -- parameter [wqo Q] -- parameter H : ∃ f : ℕ → finite_tree Q, ¬ is_good f embeds -- def mbs_of_finite_tree := minimal_bad_seq size H -- theorem bad_mbs_finite_tree : ¬ is_good mbs_of_finite_tree embeds := badness_of_mbs size H -- def seq_branches_of_mbs_tree (n : ℕ) : set (finite_tree Q × ℕ) := branches (mbs_of_finite_tree n) -- def mbs_tree : Type := {t : finite_tree Q × ℕ // ∃ i, t ∈ seq_branches_of_mbs_tree i} -- def embeds' (t : mbs_tree) (s : mbs_tree) : Prop := t.val.1 ≼ s.val.1 -- theorem embeds'_refl (t : mbs_tree) : embeds' t t := embeds_refl t.val.1 -- theorem embeds'_trans (a b c : mbs_tree) : embeds' a b → embeds' b c → embeds' a c := -- λ H₁ H₂, embeds_trans H₁ H₂ -- section -- parameter H' : ∃ f, ¬ is_good f embeds' -- def R : ℕ → mbs_tree := some H' -- definition family_index (n : ℕ) : ℕ := some ((R n).2) -- definition index_set_of_mbs_tree : set ℕ := image family_index univ -- lemma index_ne_empty : index_set_of_mbs_tree ≠ ∅ := ne_empty_of_image_on_univ family_index -- definition least_family_index := least index_set_of_mbs_tree index_ne_empty -- lemma exists_least : ∃ i, family_index i = least_family_index := -- have least_family_index ∈ index_set_of_mbs_tree, from least_is_mem index_set_of_mbs_tree index_ne_empty, -- let ⟨i,h⟩ := this in -- ⟨i, h^.right⟩ -- definition least_index : ℕ := some exists_least -- definition Kruskal's_g (n : ℕ) : mbs_tree := R (least_index + n) -- definition Kruskal's_h (n : ℕ) : ℕ := family_index (least_index + n) -- theorem bad_Kruskal's_g : ¬ is_good Kruskal's_g embeds' := -- suppose is_good Kruskal's_g embeds', -- let ⟨i,j,hij⟩ := this in -- have Hr : embeds' (Kruskal's_g i) (Kruskal's_g j), from hij^.right, -- have least_index + i < least_index + j, from add_lt_add_left hij^.left _, -- have is_good R embeds', from ⟨least_index + i, ⟨least_index + j,⟨this, Hr⟩⟩⟩, -- (some_spec H') this -- theorem Kruskal's_Hg : ¬ is_good (fst ∘ (val ∘ Kruskal's_g)) embeds := bad_Kruskal's_g -- theorem trans_of_Kruskal's_g {i j : ℕ} (H1 : mbs_of_finite_tree i ≼ (Kruskal's_g j).val.1) : -- mbs_of_finite_tree i ≼ mbs_of_finite_tree (Kruskal's_h j) := -- have (Kruskal's_g j).val ∈ branches (mbs_of_finite_tree (Kruskal's_h j)), from some_spec (Kruskal's_g j).2, -- have (Kruskal's_g j).val.1 ≼ mbs_of_finite_tree (Kruskal's_h j), from embeds_of_branches this, -- embeds_trans H1 this -- theorem size_elt_Kruskal's_g_lt_mbs_finite_tree (n : ℕ) : size (Kruskal's_g n).val.1 < size (mbs_of_finite_tree (Kruskal's_h n)) := -- lt_of_size_of_branches (some_spec (Kruskal's_g n).2) -- theorem Kruskal's_Hbp : size (Kruskal's_g 0).val.1 < size (mbs_of_finite_tree (Kruskal's_h 0)) := size_elt_Kruskal's_g_lt_mbs_finite_tree 0 -- lemma family_index_in_index_of_mbs_tree (n : ℕ) : Kruskal's_h n ∈ index_set_of_mbs_tree := -- have Kruskal's_h n = family_index (least_index + n), from rfl, -- ⟨(least_index + n),⟨trivial,rfl⟩⟩ -- theorem Kruskal's_Hh (n : ℕ) : Kruskal's_h 0 ≤ Kruskal's_h n := -- have Kruskal's_h 0 = family_index least_index, from rfl,--by simph, -- have family_index least_index = least_family_index, from some_spec exists_least, -- have Kruskal's_h 0 = least_family_index, by simph, -- begin rw this, apply minimality, apply family_index_in_index_of_mbs_tree end -- theorem Kruskal's_H : ∀ i j, mbs_of_finite_tree i ≼ (Kruskal's_g (j - Kruskal's_h 0)).val.1 → mbs_of_finite_tree i ≼ mbs_of_finite_tree (Kruskal's_h (j - Kruskal's_h 0)) := λ i j, λ H1, trans_of_Kruskal's_g H1 -- definition Kruskal's_comb_seq (n : ℕ) : finite_tree Q := @comb_seq_with_mbs _ embeds (fst ∘ (val ∘ Kruskal's_g)) Kruskal's_h size H n -- theorem Kruskal's_local_contradiction : false := local_contra_of_comb_seq_with_mbs Kruskal's_h size Kruskal's_Hh H Kruskal's_Hg Kruskal's_H Kruskal's_Hbp -- end -- theorem embeds'_is_good : ∀ f, is_good f embeds' := -- by_contradiction -- (suppose ¬ ∀ f, is_good f embeds', -- have ∃ f, ¬ is_good f embeds', from classical.exists_not_of_not_forall this, -- Kruskal's_local_contradiction this) -- instance wqo_mbs_tree : wqo mbs_tree := -- ⟨⟨⟨embeds'⟩,embeds'_refl,embeds'_trans⟩,embeds'_is_good⟩ -- def wqo_list_mbs_tree : wqo (list mbs_tree) := wqo_list -- def sub : list mbs_tree → list mbs_tree → Prop := wqo_list_mbs_tree.le -- theorem good_list_mbs_tree : ∀ f, is_good f sub := wqo_list_mbs_tree.is_good -- definition elt_mirror (n : ℕ) : set mbs_tree := {x : mbs_tree | x.1 ∈ seq_branches_of_mbs_tree n} -- theorem mirror_refl_left (x : mbs_tree) (n : ℕ) : x ∈ elt_mirror n → x.1 ∈ seq_branches_of_mbs_tree n := λ Hx, Hx -- theorem mirror_refl_right (x : mbs_tree) (n : ℕ) : x.1 ∈ seq_branches_of_mbs_tree n → x ∈ elt_mirror n := λ Hx, Hx -- definition mirror (n : ℕ) : list mbs_tree := _ -- #check list -- end -- end kruskal
641c2e172750d835cab3e143b222bf3bf3ef19a2
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/src/linear_algebra/alternating.lean
0f4411519a4dfec1215ce205ba5fc1ef5c2d43c6
[ "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
30,259
lean
/- Copyright (c) 2020 Zhangir Azerbayev. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser, Zhangir Azerbayev -/ import linear_algebra.multilinear.tensor_product import linear_algebra.linear_independent import group_theory.perm.sign import group_theory.perm.subgroup import data.equiv.fin import group_theory.quotient_group /-! # Alternating Maps We construct the bundled function `alternating_map`, which extends `multilinear_map` with all the arguments of the same type. ## Main definitions * `alternating_map R M N ι` is the space of `R`-linear alternating maps from `ι → M` to `N`. * `f.map_eq_zero_of_eq` expresses that `f` is zero when two inputs are equal. * `f.map_swap` expresses that `f` is negated when two inputs are swapped. * `f.map_perm` expresses how `f` varies by a sign change under a permutation of its inputs. * An `add_comm_monoid`, `add_comm_group`, and `module` structure over `alternating_map`s that matches the definitions over `multilinear_map`s. * `multilinear_map.alternatization`, which makes an alternating map out of a non-alternating one. * `alternating_map.dom_coprod`, which behaves as a product between two alternating maps. ## Implementation notes `alternating_map` is defined in terms of `map_eq_zero_of_eq`, as this is easier to work with than using `map_swap` as a definition, and does not require `has_neg N`. `alternating_map`s are provided with a coercion to `multilinear_map`, along with a set of `norm_cast` lemmas that act on the algebraic structure: * `alternating_map.coe_add` * `alternating_map.coe_zero` * `alternating_map.coe_sub` * `alternating_map.coe_neg` * `alternating_map.coe_smul` -/ -- semiring / add_comm_monoid variables {R : Type*} [semiring R] variables {M : Type*} [add_comm_monoid M] [module R M] variables {N : Type*} [add_comm_monoid N] [module R N] -- semiring / add_comm_group variables {M' : Type*} [add_comm_group M'] [module R M'] variables {N' : Type*} [add_comm_group N'] [module R N'] variables {ι : Type*} [decidable_eq ι] set_option old_structure_cmd true section variables (R M N ι) /-- An alternating map is a multilinear map that vanishes when two of its arguments are equal. -/ structure alternating_map extends multilinear_map R (λ i : ι, M) N := (map_eq_zero_of_eq' : ∀ (v : ι → M) (i j : ι) (h : v i = v j) (hij : i ≠ j), to_fun v = 0) end /-- The multilinear map associated to an alternating map -/ add_decl_doc alternating_map.to_multilinear_map namespace alternating_map variables (f f' : alternating_map R M N ι) variables (g g₂ : alternating_map R M N' ι) variables (g' : alternating_map R M' N' ι) variables (v : ι → M) (v' : ι → M') open function /-! Basic coercion simp lemmas, largely copied from `ring_hom` and `multilinear_map` -/ section coercions instance : has_coe_to_fun (alternating_map R M N ι) := ⟨_, λ x, x.to_fun⟩ initialize_simps_projections alternating_map (to_fun → apply) @[simp] lemma to_fun_eq_coe : f.to_fun = f := rfl @[simp] lemma coe_mk (f : (ι → M) → N) (h₁ h₂ h₃) : ⇑(⟨f, h₁, h₂, h₃⟩ : alternating_map R M N ι) = f := rfl theorem congr_fun {f g : alternating_map R M N ι} (h : f = g) (x : ι → M) : f x = g x := congr_arg (λ h : alternating_map R M N ι, h x) h theorem congr_arg (f : alternating_map R M N ι) {x y : ι → M} (h : x = y) : f x = f y := congr_arg (λ x : ι → M, f x) h theorem coe_inj ⦃f g : alternating_map R M N ι⦄ (h : ⇑f = g) : f = g := by { cases f, cases g, cases h, refl } @[ext] theorem ext {f f' : alternating_map R M N ι} (H : ∀ x, f x = f' x) : f = f' := coe_inj (funext H) theorem ext_iff {f g : alternating_map R M N ι} : f = g ↔ ∀ x, f x = g x := ⟨λ h x, h ▸ rfl, λ h, ext h⟩ instance : has_coe (alternating_map R M N ι) (multilinear_map R (λ i : ι, M) N) := ⟨λ x, x.to_multilinear_map⟩ @[simp, norm_cast] lemma coe_multilinear_map : ⇑(f : multilinear_map R (λ i : ι, M) N) = f := rfl lemma coe_multilinear_map_injective : function.injective (coe : alternating_map R M N ι → multilinear_map R (λ i : ι, M) N) := λ x y h, ext $ multilinear_map.congr_fun h @[simp] lemma to_multilinear_map_eq_coe : f.to_multilinear_map = f := rfl @[simp] lemma coe_multilinear_map_mk (f : (ι → M) → N) (h₁ h₂ h₃) : ((⟨f, h₁, h₂, h₃⟩ : alternating_map R M N ι) : multilinear_map R (λ i : ι, M) N) = ⟨f, h₁, h₂⟩ := rfl end coercions /-! ### Simp-normal forms of the structure fields These are expressed in terms of `⇑f` instead of `f.to_fun`. -/ @[simp] lemma map_add (i : ι) (x y : M) : f (update v i (x + y)) = f (update v i x) + f (update v i y) := f.to_multilinear_map.map_add' v i x y @[simp] lemma map_sub (i : ι) (x y : M') : g' (update v' i (x - y)) = g' (update v' i x) - g' (update v' i y) := g'.to_multilinear_map.map_sub v' i x y @[simp] lemma map_neg (i : ι) (x : M') : g' (update v' i (-x)) = -g' (update v' i x) := g'.to_multilinear_map.map_neg v' i x @[simp] lemma map_smul (i : ι) (r : R) (x : M) : f (update v i (r • x)) = r • f (update v i x) := f.to_multilinear_map.map_smul' v i r x @[simp] lemma map_eq_zero_of_eq (v : ι → M) {i j : ι} (h : v i = v j) (hij : i ≠ j) : f v = 0 := f.map_eq_zero_of_eq' v i j h hij lemma map_coord_zero {m : ι → M} (i : ι) (h : m i = 0) : f m = 0 := f.to_multilinear_map.map_coord_zero i h @[simp] lemma map_update_zero (m : ι → M) (i : ι) : f (update m i 0) = 0 := f.to_multilinear_map.map_update_zero m i @[simp] lemma map_zero [nonempty ι] : f 0 = 0 := f.to_multilinear_map.map_zero /-! ### Algebraic structure inherited from `multilinear_map` `alternating_map` carries the same `add_comm_monoid`, `add_comm_group`, and `module` structure as `multilinear_map` -/ instance : has_add (alternating_map R M N ι) := ⟨λ a b, { map_eq_zero_of_eq' := λ v i j h hij, by simp [a.map_eq_zero_of_eq v h hij, b.map_eq_zero_of_eq v h hij], ..(a + b : multilinear_map R (λ i : ι, M) N)}⟩ @[simp] lemma add_apply : (f + f') v = f v + f' v := rfl @[norm_cast] lemma coe_add : (↑(f + f') : multilinear_map R (λ i : ι, M) N) = f + f' := rfl instance : has_zero (alternating_map R M N ι) := ⟨{map_eq_zero_of_eq' := λ v i j h hij, by simp, ..(0 : multilinear_map R (λ i : ι, M) N)}⟩ @[simp] lemma zero_apply : (0 : alternating_map R M N ι) v = 0 := rfl @[norm_cast] lemma coe_zero : ((0 : alternating_map R M N ι) : multilinear_map R (λ i : ι, M) N) = 0 := rfl instance : inhabited (alternating_map R M N ι) := ⟨0⟩ instance : add_comm_monoid (alternating_map R M N ι) := { zero := 0, add := (+), zero_add := by intros; ext; simp [add_comm, add_left_comm], add_zero := by intros; ext; simp [add_comm, add_left_comm], add_comm := by intros; ext; simp [add_comm, add_left_comm], add_assoc := by intros; ext; simp [add_comm, add_left_comm], nsmul := λ n f, { map_eq_zero_of_eq' := λ v i j h hij, by simp [f.map_eq_zero_of_eq v h hij], .. ((n • f : multilinear_map R (λ i : ι, M) N)) }, nsmul_zero' := by { intros, ext, simp [add_smul], }, nsmul_succ' := by { intros, ext, simp [add_smul, nat.succ_eq_one_add], } } instance : has_neg (alternating_map R M N' ι) := ⟨λ f, { map_eq_zero_of_eq' := λ v i j h hij, by simp [f.map_eq_zero_of_eq v h hij], ..(-(f : multilinear_map R (λ i : ι, M) N')) }⟩ @[simp] lemma neg_apply (m : ι → M) : (-g) m = -(g m) := rfl @[norm_cast] lemma coe_neg : ((-g : alternating_map R M N' ι) : multilinear_map R (λ i : ι, M) N') = -g := rfl instance : has_sub (alternating_map R M N' ι) := ⟨λ f g, { map_eq_zero_of_eq' := λ v i j h hij, by simp [f.map_eq_zero_of_eq v h hij, g.map_eq_zero_of_eq v h hij], ..(f - g : multilinear_map R (λ i : ι, M) N') }⟩ @[simp] lemma sub_apply (m : ι → M) : (g - g₂) m = g m - g₂ m := rfl @[norm_cast] lemma coe_sub : (↑(g - g₂) : multilinear_map R (λ i : ι, M) N') = g - g₂ := rfl instance : add_comm_group (alternating_map R M N' ι) := by refine { zero := 0, add := (+), neg := has_neg.neg, sub := has_sub.sub, sub_eq_add_neg := _, nsmul := λ n f, { map_eq_zero_of_eq' := λ v i j h hij, by simp [f.map_eq_zero_of_eq v h hij], .. ((n • f : multilinear_map R (λ i : ι, M) N')) }, gsmul := λ n f, { map_eq_zero_of_eq' := λ v i j h hij, by simp [f.map_eq_zero_of_eq v h hij], .. ((n • f : multilinear_map R (λ i : ι, M) N')) }, gsmul_zero' := _, gsmul_succ' := _, gsmul_neg' := _, .. alternating_map.add_comm_monoid, .. }; intros; ext; simp [add_comm, add_left_comm, sub_eq_add_neg, add_smul, nat.succ_eq_add_one, gsmul_coe_nat] section distrib_mul_action variables {S : Type*} [monoid S] [distrib_mul_action S N] [smul_comm_class R S N] instance : has_scalar S (alternating_map R M N ι) := ⟨λ c f, { map_eq_zero_of_eq' := λ v i j h hij, by simp [f.map_eq_zero_of_eq v h hij], ..((c • f : multilinear_map R (λ i : ι, M) N)) }⟩ @[simp] lemma smul_apply (c : S) (m : ι → M) : (c • f) m = c • f m := rfl @[norm_cast] lemma coe_smul (c : S): ((c • f : alternating_map R M N ι) : multilinear_map R (λ i : ι, M) N) = c • f := rfl instance : distrib_mul_action S (alternating_map R M N ι) := { one_smul := λ f, ext $ λ x, one_smul _ _, mul_smul := λ c₁ c₂ f, ext $ λ x, mul_smul _ _ _, smul_zero := λ r, ext $ λ x, smul_zero _, smul_add := λ r f₁ f₂, ext $ λ x, smul_add _ _ _ } end distrib_mul_action section module variables {S : Type*} [semiring S] [module S N] [smul_comm_class R S N] /-- The space of multilinear maps over an algebra over `R` is a module over `R`, for the pointwise addition and scalar multiplication. -/ instance : module S (alternating_map R M N ι) := { add_smul := λ r₁ r₂ f, ext $ λ x, add_smul _ _ _, zero_smul := λ f, ext $ λ x, zero_smul _ _ } end module section variables (R N) /-- The evaluation map from `ι → N` to `N` at a given `i` is alternating when `ι` is subsingleton. -/ @[simps] def of_subsingleton [subsingleton ι] (i : ι) : alternating_map R N N ι := { to_fun := function.eval i, map_eq_zero_of_eq' := λ v i j hv hij, (hij $ subsingleton.elim _ _).elim, ..multilinear_map.of_subsingleton R N i } end end alternating_map /-! ### Composition with linear maps -/ namespace linear_map variables {N₂ : Type*} [add_comm_monoid N₂] [module R N₂] /-- Composing a alternating map with a linear map gives again a alternating map. -/ def comp_alternating_map (g : N →ₗ[R] N₂) : alternating_map R M N ι →+ alternating_map R M N₂ ι := { to_fun := λ f, { map_eq_zero_of_eq' := λ v i j h hij, by simp [f.map_eq_zero_of_eq v h hij], ..(g.comp_multilinear_map (f : multilinear_map R (λ _ : ι, M) N)) }, map_zero' := by { ext, simp }, map_add' := λ a b, by { ext, simp } } @[simp] lemma coe_comp_alternating_map (g : N →ₗ[R] N₂) (f : alternating_map R M N ι) : ⇑(g.comp_alternating_map f) = g ∘ f := rfl lemma comp_alternating_map_apply (g : N →ₗ[R] N₂) (f : alternating_map R M N ι) (m : ι → M) : g.comp_alternating_map f m = g (f m) := rfl end linear_map namespace alternating_map variables (f f' : alternating_map R M N ι) variables (g g₂ : alternating_map R M N' ι) variables (g' : alternating_map R M' N' ι) variables (v : ι → M) (v' : ι → M') open function /-! ### Other lemmas from `multilinear_map` -/ section open_locale big_operators lemma map_update_sum {α : Type*} (t : finset α) (i : ι) (g : α → M) (m : ι → M): f (update m i (∑ a in t, g a)) = ∑ a in t, f (update m i (g a)) := f.to_multilinear_map.map_update_sum t i g m end /-! ### Theorems specific to alternating maps Various properties of reordered and repeated inputs which follow from `alternating_map.map_eq_zero_of_eq`. -/ lemma map_update_self {i j : ι} (hij : i ≠ j) : f (function.update v i (v j)) = 0 := f.map_eq_zero_of_eq _ (by rw [function.update_same, function.update_noteq hij.symm]) hij lemma map_update_update {i j : ι} (hij : i ≠ j) (m : M) : f (function.update (function.update v i m) j m) = 0 := f.map_eq_zero_of_eq _ (by rw [function.update_same, function.update_noteq hij, function.update_same]) hij lemma map_swap_add {i j : ι} (hij : i ≠ j) : f (v ∘ equiv.swap i j) + f v = 0 := begin rw equiv.comp_swap_eq_update, convert f.map_update_update v hij (v i + v j), simp [f.map_update_self _ hij, f.map_update_self _ hij.symm, function.update_comm hij (v i + v j) (v _) v, function.update_comm hij.symm (v i) (v i) v], end lemma map_add_swap {i j : ι} (hij : i ≠ j) : f v + f (v ∘ equiv.swap i j) = 0 := by { rw add_comm, exact f.map_swap_add v hij } lemma map_swap {i j : ι} (hij : i ≠ j) : g (v ∘ equiv.swap i j) = - g v := eq_neg_of_add_eq_zero (g.map_swap_add v hij) lemma map_perm [fintype ι] (v : ι → M) (σ : equiv.perm ι) : g (v ∘ σ) = σ.sign • g v := begin apply equiv.perm.swap_induction_on' σ, { simp }, { intros s x y hxy hI, simpa [g.map_swap (v ∘ s) hxy, equiv.perm.sign_swap hxy] using hI, } end lemma map_congr_perm [fintype ι] (σ : equiv.perm ι) : g v = σ.sign • g (v ∘ σ) := by { rw [g.map_perm, smul_smul], simp } lemma coe_dom_dom_congr [fintype ι] (σ : equiv.perm ι) : (g : multilinear_map R (λ _ : ι, M) N').dom_dom_congr σ = σ.sign • (g : multilinear_map R (λ _ : ι, M) N') := multilinear_map.ext $ λ v, g.map_perm v σ /-- If the arguments are linearly dependent then the result is `0`. -/ lemma map_linear_dependent {K : Type*} [ring K] {M : Type*} [add_comm_group M] [module K M] {N : Type*} [add_comm_group N] [module K N] [no_zero_smul_divisors K N] (f : alternating_map K M N ι) (v : ι → M) (h : ¬linear_independent K v) : f v = 0 := begin obtain ⟨s, g, h, i, hi, hz⟩ := linear_dependent_iff.mp h, suffices : f (update v i (g i • v i)) = 0, { rw [f.map_smul, function.update_eq_self, smul_eq_zero] at this, exact or.resolve_left this hz, }, conv at h in (g _ • v _) { rw ←if_t_t (i = x) (g _ • v _), }, rw [finset.sum_ite, finset.filter_eq, finset.filter_ne, if_pos hi, finset.sum_singleton, add_eq_zero_iff_eq_neg] at h, rw [h, f.map_neg, f.map_update_sum, neg_eq_zero, finset.sum_eq_zero], intros j hj, obtain ⟨hij, _⟩ := finset.mem_erase.mp hj, rw [f.map_smul, f.map_update_self _ hij.symm, smul_zero], end end alternating_map open_locale big_operators namespace multilinear_map open equiv variables [fintype ι] private lemma alternization_map_eq_zero_of_eq_aux (m : multilinear_map R (λ i : ι, M) N') (v : ι → M) (i j : ι) (i_ne_j : i ≠ j) (hv : v i = v j) : (∑ (σ : perm ι), σ.sign • m.dom_dom_congr σ) v = 0 := begin rw sum_apply, exact finset.sum_involution (λ σ _, swap i j * σ) (λ σ _, by simp [perm.sign_swap i_ne_j, apply_swap_eq_self hv]) (λ σ _ _, (not_congr swap_mul_eq_iff).mpr i_ne_j) (λ σ _, finset.mem_univ _) (λ σ _, swap_mul_involutive i j σ) end /-- Produce an `alternating_map` out of a `multilinear_map`, by summing over all argument permutations. -/ def alternatization : multilinear_map R (λ i : ι, M) N' →+ alternating_map R M N' ι := { to_fun := λ m, { to_fun := ⇑(∑ (σ : perm ι), σ.sign • m.dom_dom_congr σ), map_eq_zero_of_eq' := λ v i j hvij hij, alternization_map_eq_zero_of_eq_aux m v i j hij hvij, .. (∑ (σ : perm ι), σ.sign • m.dom_dom_congr σ)}, map_add' := λ a b, begin ext, simp only [ finset.sum_add_distrib, smul_add, add_apply, dom_dom_congr_apply, alternating_map.add_apply, alternating_map.coe_mk, smul_apply, sum_apply], end, map_zero' := begin ext, simp only [ finset.sum_const_zero, smul_zero, zero_apply, dom_dom_congr_apply, alternating_map.zero_apply, alternating_map.coe_mk, smul_apply, sum_apply], end } lemma alternatization_def (m : multilinear_map R (λ i : ι, M) N') : ⇑(alternatization m) = (∑ (σ : perm ι), σ.sign • m.dom_dom_congr σ : _) := rfl lemma alternatization_coe (m : multilinear_map R (λ i : ι, M) N') : ↑m.alternatization = (∑ (σ : perm ι), σ.sign • m.dom_dom_congr σ : _) := coe_inj rfl lemma alternatization_apply (m : multilinear_map R (λ i : ι, M) N') (v : ι → M) : alternatization m v = ∑ (σ : perm ι), σ.sign • m.dom_dom_congr σ v := by simp only [alternatization_def, smul_apply, sum_apply] end multilinear_map namespace alternating_map /-- Alternatizing a multilinear map that is already alternating results in a scale factor of `n!`, where `n` is the number of inputs. -/ lemma coe_alternatization [fintype ι] (a : alternating_map R M N' ι) : (↑a : multilinear_map R (λ ι, M) N').alternatization = nat.factorial (fintype.card ι) • a := begin apply alternating_map.coe_inj, simp_rw [multilinear_map.alternatization_def, coe_dom_dom_congr, smul_smul, int.units_mul_self, one_smul, finset.sum_const, finset.card_univ, fintype.card_perm, ←coe_multilinear_map, coe_smul], end end alternating_map namespace linear_map variables {N'₂ : Type*} [add_comm_group N'₂] [module R N'₂] [fintype ι] /-- Composition with a linear map before and after alternatization are equivalent. -/ lemma comp_multilinear_map_alternatization (g : N' →ₗ[R] N'₂) (f : multilinear_map R (λ _ : ι, M) N') : (g.comp_multilinear_map f).alternatization = g.comp_alternating_map (f.alternatization) := by { ext, simp [multilinear_map.alternatization_def] } end linear_map section coprod open_locale big_operators open_locale tensor_product variables {ιa ιb : Type*} [decidable_eq ιa] [decidable_eq ιb] [fintype ιa] [fintype ιb] variables {R' : Type*} {Mᵢ N₁ N₂ : Type*} [comm_semiring R'] [add_comm_group N₁] [module R' N₁] [add_comm_group N₂] [module R' N₂] [add_comm_monoid Mᵢ] [module R' Mᵢ] namespace equiv.perm /-- Elements which are considered equivalent if they differ only by swaps within α or β -/ abbreviation mod_sum_congr (α β : Type*) := quotient_group.quotient (equiv.perm.sum_congr_hom α β).range lemma mod_sum_congr.swap_smul_involutive {α β : Type*} [decidable_eq (α ⊕ β)] (i j : α ⊕ β) : function.involutive (has_scalar.smul (equiv.swap i j) : mod_sum_congr α β → mod_sum_congr α β) := λ σ, begin apply σ.induction_on' (λ σ, _), exact _root_.congr_arg quotient.mk' (equiv.swap_mul_involutive i j σ) end end equiv.perm namespace alternating_map open equiv /-- summand used in `alternating_map.dom_coprod` -/ def dom_coprod.summand (a : alternating_map R' Mᵢ N₁ ιa) (b : alternating_map R' Mᵢ N₂ ιb) (σ : perm.mod_sum_congr ιa ιb) : multilinear_map R' (λ _ : ιa ⊕ ιb, Mᵢ) (N₁ ⊗[R'] N₂) := quotient.lift_on' σ (λ σ, σ.sign • (multilinear_map.dom_coprod ↑a ↑b : multilinear_map R' (λ _, Mᵢ) (N₁ ⊗ N₂)).dom_dom_congr σ) (λ σ₁ σ₂ ⟨⟨sl, sr⟩, h⟩, begin ext v, simp only [multilinear_map.dom_dom_congr_apply, multilinear_map.dom_coprod_apply, coe_multilinear_map, multilinear_map.smul_apply], replace h := inv_mul_eq_iff_eq_mul.mp h.symm, have : (σ₁ * perm.sum_congr_hom _ _ (sl, sr)).sign = σ₁.sign * (sl.sign * sr.sign) := by simp, rw [h, this, mul_smul, mul_smul, smul_left_cancel_iff, ←tensor_product.tmul_smul, tensor_product.smul_tmul'], simp only [sum.map_inr, perm.sum_congr_hom_apply, perm.sum_congr_apply, sum.map_inl, function.comp_app, perm.coe_mul], rw [←a.map_congr_perm (λ i, v (σ₁ _)), ←b.map_congr_perm (λ i, v (σ₁ _))], end) lemma dom_coprod.summand_mk' (a : alternating_map R' Mᵢ N₁ ιa) (b : alternating_map R' Mᵢ N₂ ιb) (σ : equiv.perm (ιa ⊕ ιb)) : dom_coprod.summand a b (quotient.mk' σ) = σ.sign • (multilinear_map.dom_coprod ↑a ↑b : multilinear_map R' (λ _, Mᵢ) (N₁ ⊗ N₂)).dom_dom_congr σ := rfl /-- Swapping elements in `σ` with equal values in `v` results in an addition that cancels -/ lemma dom_coprod.summand_add_swap_smul_eq_zero (a : alternating_map R' Mᵢ N₁ ιa) (b : alternating_map R' Mᵢ N₂ ιb) (σ : perm.mod_sum_congr ιa ιb) {v : ιa ⊕ ιb → Mᵢ} {i j : ιa ⊕ ιb} (hv : v i = v j) (hij : i ≠ j) : dom_coprod.summand a b σ v + dom_coprod.summand a b (swap i j • σ) v = 0 := begin apply σ.induction_on' (λ σ, _), dsimp only [quotient.lift_on'_mk', quotient.map'_mk', mul_action.quotient.smul_mk, dom_coprod.summand], rw [perm.sign_mul, perm.sign_swap hij], simp only [one_mul, units.neg_mul, function.comp_app, units.neg_smul, perm.coe_mul, units.coe_neg, multilinear_map.smul_apply, multilinear_map.neg_apply, multilinear_map.dom_dom_congr_apply, multilinear_map.dom_coprod_apply], convert add_right_neg _; { ext k, rw equiv.apply_swap_eq_self hv }, end /-- Swapping elements in `σ` with equal values in `v` result in zero if the swap has no effect on the quotient. -/ lemma dom_coprod.summand_eq_zero_of_smul_invariant (a : alternating_map R' Mᵢ N₁ ιa) (b : alternating_map R' Mᵢ N₂ ιb) (σ : perm.mod_sum_congr ιa ιb) {v : ιa ⊕ ιb → Mᵢ} {i j : ιa ⊕ ιb} (hv : v i = v j) (hij : i ≠ j) : swap i j • σ = σ → dom_coprod.summand a b σ v = 0 := begin apply σ.induction_on' (λ σ, _), dsimp only [quotient.lift_on'_mk', quotient.map'_mk', multilinear_map.smul_apply, multilinear_map.dom_dom_congr_apply, multilinear_map.dom_coprod_apply, dom_coprod.summand], intro hσ, with_cases { cases hi : σ⁻¹ i; cases hj : σ⁻¹ j; rw perm.inv_eq_iff_eq at hi hj; substs hi hj, }, case [sum.inl sum.inr : i' j', sum.inr sum.inl : i' j'] { -- the term pairs with and cancels another term all_goals { obtain ⟨⟨sl, sr⟩, hσ⟩ := quotient.exact' hσ, }, work_on_goal 0 { replace hσ := equiv.congr_fun hσ (sum.inl i'), }, work_on_goal 1 { replace hσ := equiv.congr_fun hσ (sum.inr i'), }, all_goals { rw [←equiv.mul_swap_eq_swap_mul, mul_inv_rev, equiv.swap_inv, inv_mul_cancel_right] at hσ, simpa using hσ, }, }, case [sum.inr sum.inr : i' j', sum.inl sum.inl : i' j'] { -- the term does not pair but is zero all_goals { convert smul_zero _, }, work_on_goal 0 { convert tensor_product.tmul_zero _ _, }, work_on_goal 1 { convert tensor_product.zero_tmul _ _, }, all_goals { exact alternating_map.map_eq_zero_of_eq _ _ hv (λ hij', hij (hij' ▸ rfl)), } }, end /-- Like `multilinear_map.dom_coprod`, but ensures the result is also alternating. Note that this is usually defined (for instance, as used in Proposition 22.24 in [Gallier2011Notes]) over integer indices `ιa = fin n` and `ιb = fin m`, as $$ (f \wedge g)(u_1, \ldots, u_{m+n}) = \sum_{\operatorname{shuffle}(m, n)} \operatorname{sign}(\sigma) f(u_{\sigma(1)}, \ldots, u_{\sigma(m)}) g(u_{\sigma(m+1)}, \ldots, u_{\sigma(m+n)}), $$ where $\operatorname{shuffle}(m, n)$ consists of all permutations of $[1, m+n]$ such that $\sigma(1) < \cdots < \sigma(m)$ and $\sigma(m+1) < \cdots < \sigma(m+n)$. Here, we generalize this by replacing: * the product in the sum with a tensor product * the filtering of $[1, m+n]$ to shuffles with an isomorphic quotient * the additions in the subscripts of $\sigma$ with an index of type `sum` The specialized version can be obtained by combining this definition with `fin_sum_fin_equiv` and `algebra.lmul'`. -/ @[simps] def dom_coprod (a : alternating_map R' Mᵢ N₁ ιa) (b : alternating_map R' Mᵢ N₂ ιb) : alternating_map R' Mᵢ (N₁ ⊗[R'] N₂) (ιa ⊕ ιb) := { to_fun := λ v, ⇑(∑ σ : perm.mod_sum_congr ιa ιb, dom_coprod.summand a b σ) v, map_eq_zero_of_eq' := λ v i j hv hij, begin dsimp only, rw multilinear_map.sum_apply, exact finset.sum_involution (λ σ _, equiv.swap i j • σ) (λ σ _, dom_coprod.summand_add_swap_smul_eq_zero a b σ hv hij) (λ σ _, mt $ dom_coprod.summand_eq_zero_of_smul_invariant a b σ hv hij) (λ σ _, finset.mem_univ _) (λ σ _, equiv.perm.mod_sum_congr.swap_smul_involutive i j σ), end, ..(∑ σ : perm.mod_sum_congr ιa ιb, dom_coprod.summand a b σ) } lemma dom_coprod_coe (a : alternating_map R' Mᵢ N₁ ιa) (b : alternating_map R' Mᵢ N₂ ιb) : (↑(a.dom_coprod b) : multilinear_map R' (λ _, Mᵢ) _) = ∑ σ : perm.mod_sum_congr ιa ιb, dom_coprod.summand a b σ := multilinear_map.ext $ λ _, rfl /-- A more bundled version of `alternating_map.dom_coprod` that maps `((ι₁ → N) → N₁) ⊗ ((ι₂ → N) → N₂)` to `(ι₁ ⊕ ι₂ → N) → N₁ ⊗ N₂`. -/ def dom_coprod' : (alternating_map R' Mᵢ N₁ ιa ⊗[R'] alternating_map R' Mᵢ N₂ ιb) →ₗ[R'] alternating_map R' Mᵢ (N₁ ⊗[R'] N₂) (ιa ⊕ ιb) := tensor_product.lift $ by refine linear_map.mk₂ R' (dom_coprod) (λ m₁ m₂ n, _) (λ c m n, _) (λ m n₁ n₂, _) (λ c m n, _); { ext, simp only [dom_coprod_apply, add_apply, smul_apply, ←finset.sum_add_distrib, finset.smul_sum, multilinear_map.sum_apply, dom_coprod.summand], congr, ext σ, apply σ.induction_on' (λ σ, _), simp only [quotient.lift_on'_mk', coe_add, coe_smul, multilinear_map.smul_apply, ←multilinear_map.dom_coprod'_apply], simp only [tensor_product.add_tmul, ←tensor_product.smul_tmul', tensor_product.tmul_add, tensor_product.tmul_smul, linear_map.map_add, linear_map.map_smul], rw ←smul_add <|> rw smul_comm, congr } @[simp] lemma dom_coprod'_apply (a : alternating_map R' Mᵢ N₁ ιa) (b : alternating_map R' Mᵢ N₂ ιb) : dom_coprod' (a ⊗ₜ[R'] b) = dom_coprod a b := by simp only [dom_coprod', tensor_product.lift.tmul, linear_map.mk₂_apply] end alternating_map open equiv /-- A helper lemma for `multilinear_map.dom_coprod_alternization`. -/ lemma multilinear_map.dom_coprod_alternization_coe (a : multilinear_map R' (λ _ : ιa, Mᵢ) N₁) (b : multilinear_map R' (λ _ : ιb, Mᵢ) N₂) : multilinear_map.dom_coprod ↑a.alternatization ↑b.alternatization = ∑ (σa : perm ιa) (σb : perm ιb), σa.sign • σb.sign • multilinear_map.dom_coprod (a.dom_dom_congr σa) (b.dom_dom_congr σb) := begin simp_rw [←multilinear_map.dom_coprod'_apply, multilinear_map.alternatization_coe], simp_rw [tensor_product.sum_tmul, tensor_product.tmul_sum, linear_map.map_sum, ←tensor_product.smul_tmul', tensor_product.tmul_smul, linear_map.map_smul_of_tower], end open alternating_map /-- Computing the `multilinear_map.alternatization` of the `multilinear_map.dom_coprod` is the same as computing the `alternating_map.dom_coprod` of the `multilinear_map.alternatization`s. -/ lemma multilinear_map.dom_coprod_alternization (a : multilinear_map R' (λ _ : ιa, Mᵢ) N₁) (b : multilinear_map R' (λ _ : ιb, Mᵢ) N₂) : (multilinear_map.dom_coprod a b).alternatization = a.alternatization.dom_coprod b.alternatization := begin apply coe_multilinear_map_injective, rw [dom_coprod_coe, multilinear_map.alternatization_coe, finset.sum_partition (quotient_group.left_rel (perm.sum_congr_hom ιa ιb).range)], congr' 1, ext1 σ, apply σ.induction_on' (λ σ, _), -- unfold the quotient mess left by `finset.sum_partition` conv in (_ = quotient.mk' _) { change quotient.mk' _ = quotient.mk' _, rw quotient.eq', rw [quotient_group.left_rel], dsimp only [setoid.r] }, -- eliminate a multiplication have : @finset.univ (perm (ιa ⊕ ιb)) _ = finset.univ.image ((*) σ) := (finset.eq_univ_iff_forall.mpr $ λ a, let ⟨a', ha'⟩ := mul_left_surjective σ a in finset.mem_image.mpr ⟨a', finset.mem_univ _, ha'⟩).symm, rw [this, finset.image_filter], simp only [function.comp, mul_inv_rev, inv_mul_cancel_right, subgroup.inv_mem_iff], simp only [monoid_hom.mem_range], -- needs to be separate from the above `simp only` rw [finset.filter_congr_decidable, finset.univ_filter_exists (perm.sum_congr_hom ιa ιb), finset.sum_image (λ x _ y _ (h : _ = _), mul_right_injective _ h), finset.sum_image (λ x _ y _ (h : _ = _), perm.sum_congr_hom_injective h)], dsimp only, -- now we're ready to clean up the RHS, pulling out the summation rw [dom_coprod.summand_mk', multilinear_map.dom_coprod_alternization_coe, ←finset.sum_product', finset.univ_product_univ, ←multilinear_map.dom_dom_congr_equiv_apply, add_equiv.map_sum, finset.smul_sum], congr' 1, ext1 ⟨al, ar⟩, dsimp only, -- pull out the pair of smuls on the RHS, by rewriting to `_ →ₗ[ℤ] _` and back rw [←add_equiv.coe_to_add_monoid_hom, ←add_monoid_hom.coe_to_int_linear_map, linear_map.map_smul_of_tower, linear_map.map_smul_of_tower, add_monoid_hom.coe_to_int_linear_map, add_equiv.coe_to_add_monoid_hom, multilinear_map.dom_dom_congr_equiv_apply], -- pick up the pieces rw [multilinear_map.dom_dom_congr_mul, perm.sign_mul, perm.sum_congr_hom_apply, multilinear_map.dom_coprod_dom_dom_congr_sum_congr, perm.sign_sum_congr, mul_smul, mul_smul], end /-- Taking the `multilinear_map.alternatization` of the `multilinear_map.dom_coprod` of two `alternating_map`s gives a scaled version of the `alternating_map.coprod` of those maps. -/ lemma multilinear_map.dom_coprod_alternization_eq (a : alternating_map R' Mᵢ N₁ ιa) (b : alternating_map R' Mᵢ N₂ ιb) : (multilinear_map.dom_coprod a b : multilinear_map R' (λ _ : ιa ⊕ ιb, Mᵢ) (N₁ ⊗ N₂)) .alternatization = ((fintype.card ιa).factorial * (fintype.card ιb).factorial) • a.dom_coprod b := begin rw [multilinear_map.dom_coprod_alternization, coe_alternatization, coe_alternatization, mul_smul, ←dom_coprod'_apply, ←dom_coprod'_apply, ←tensor_product.smul_tmul', tensor_product.tmul_smul, linear_map.map_smul_of_tower dom_coprod', linear_map.map_smul_of_tower dom_coprod'], -- typeclass resolution is a little confused here apply_instance, apply_instance, end end coprod
735ff6709a3e49896d8b48220e02628db66ce1b4
9dd3f3912f7321eb58ee9aa8f21778ad6221f87c
/library/tools/debugger/cli.lean
743729b360370d2911156140e7a72065ac92e73c
[ "Apache-2.0" ]
permissive
bre7k30/lean
de893411bcfa7b3c5572e61b9e1c52951b310aa4
5a924699d076dab1bd5af23a8f910b433e598d7a
refs/heads/master
1,610,900,145,817
1,488,006,845,000
1,488,006,845,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
8,335
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 Simple command line interface for debugging Lean programs and tactics. -/ import tools.debugger.util namespace debugger inductive mode | init | step | run | done instance : decidable_eq mode := by tactic.mk_dec_eq_instance structure state := (md : mode) (csz : nat) (fn_bps : list name) (active_bps : list (nat × name)) def init_state : state := { md := mode.init, csz := 0, fn_bps := [], active_bps := [] } meta def show_help : vm unit := do vm.put_str "exit - stop debugger\n", vm.put_str "help - display this message\n", vm.put_str "run - continue execution\n", vm.put_str "step - execute until another function in on the top of the stack\n", vm.put_str "stack trace\n", vm.put_str " up - move up in the stack trace\n", vm.put_str " down - move down in the stack trace\n", vm.put_str " vars - display variables in the current stack frame\n", vm.put_str " stack - display all functions on the call stack\n", vm.put_str " print var - display the value of variable named 'var' in the current stack frame\n", vm.put_str " pidx idx - display the value of variable at position #idx in the current stack frame\n", vm.put_str "breakpoints\n", vm.put_str " break fn - add breakpoint for fn\n", vm.put_str " rbreak fn - remove breakpoint\n", vm.put_str " bs - show breakpoints\n" meta def add_breakpoint (s : state) (args : list string) : vm state := match args with | [arg] := do fn ← return $ to_qualified_name arg, ok ← is_valid_fn_prefix fn, if ok then return {s with fn_bps := fn :: list.filter (λ fn', fn ≠ fn') s^.fn_bps} else vm.put_str "invalid 'break' command, given name is not the prefix for any function\n" >> return s | _ := vm.put_str "invalid 'break <fn>' command, incorrect number of arguments\n" >> return s end meta def remove_breakpoint (s : state) (args : list string) : vm state := match args with | [arg] := do fn ← return $ to_qualified_name arg, return {s with fn_bps := list.filter (λ fn', fn ≠ fn') s^.fn_bps} | _ := vm.put_str "invalid 'rbreak <fn>' command, incorrect number of arguments\n" >> return s end meta def show_breakpoints : list name → vm unit | [] := return () | (fn::fns) := do vm.put_str " ", vm.put_str fn^.to_string, vm.put_str "\n", show_breakpoints fns meta def up_cmd (frame : nat) : vm nat := if frame = 0 then return 0 else show_frame (frame - 1) >> return (frame - 1) meta def down_cmd (frame : nat) : vm nat := do sz ← vm.call_stack_size, if frame >= sz - 1 then return frame else show_frame (frame + 1) >> return (frame + 1) meta def pidx_cmd : nat → list string → vm unit | frame [arg] := do idx ← return $ arg^.to_nat, sz ← vm.stack_size, (bp, ep) ← vm.call_stack_var_range frame, if bp + idx >= ep then vm.put_str "invalid 'pidx <idx>' command, index out of bounds\n" else do v ← vm.pp_stack_obj (bp+idx), (n, t) ← vm.stack_obj_info (bp+idx), opts ← vm.get_options, vm.put_str n^.to_string, vm.put_str " := ", vm.put_str (v^.to_string opts), vm.put_str "\n" | _ _ := vm.put_str "invalid 'pidx <idx>' command, incorrect number of arguments\n" meta def print_var : nat → nat → name → vm unit | i ep v := do if i = ep then vm.put_str "invalid 'print <var>', unknown variable\n" else do (n, t) ← vm.stack_obj_info i, if n = v then do v ← vm.pp_stack_obj i, opts ← vm.get_options, vm.put_str n^.to_string, vm.put_str " := ", vm.put_str (v^.to_string opts), vm.put_str "\n" else print_var (i+1) ep v meta def print_cmd : nat → list string → vm unit | frame [arg] := do (bp, ep) ← vm.call_stack_var_range frame, print_var bp ep (to_qualified_name arg) | _ _ := vm.put_str "invalid 'print <var>' command, incorrect number of arguments\n" meta def cmd_loop_core : state → nat → list string → vm state | s frame default_cmd := do is_eof ← vm.eof, if is_eof then do vm.put_str "stopping debugger... 'end of file' has been found\n", return {s with md := mode.done } else do vm.put_str "% ", l ← vm.get_line, tks ← return $ split l, tks ← return $ if tks = [] then default_cmd else tks, match tks with | [] := cmd_loop_core s frame default_cmd | (cmd::args) := if cmd = "help" ∨ cmd = "h" then show_help >> cmd_loop_core s frame [] else if cmd = "exit" then return {s with md := mode.done } else if cmd = "run" ∨ cmd = "r" then return {s with md := mode.run } else if cmd = "step" ∨ cmd = "s" then return {s with md := mode.step } else if cmd = "break" ∨ cmd = "b" then do new_s ← add_breakpoint s args, cmd_loop_core new_s frame [] else if cmd = "rbreak" then do new_s ← remove_breakpoint s args, cmd_loop_core new_s frame [] else if cmd = "bs" then do vm.put_str "breakpoints\n", show_breakpoints s^.fn_bps, cmd_loop_core s frame default_cmd else if cmd = "up" ∨ cmd = "u" then do frame ← up_cmd frame, cmd_loop_core s frame ["u"] else if cmd = "down" ∨ cmd = "d" then do frame ← down_cmd frame, cmd_loop_core s frame ["d"] else if cmd = "vars" ∨ cmd = "v" then do show_vars frame, cmd_loop_core s frame [] else if cmd = "stack" then do show_stack, cmd_loop_core s frame [] else if cmd = "pidx" then do pidx_cmd frame args, cmd_loop_core s frame [] else if cmd = "print" ∨ cmd = "p" then do print_cmd frame args, cmd_loop_core s frame [] else do vm.put_str "unknown command, type 'help' for help\n", cmd_loop_core s frame default_cmd end meta def cmd_loop (s : state) (default_cmd : list string) : vm state := do csz ← vm.call_stack_size, cmd_loop_core s (csz - 1) default_cmd def prune_active_bps_core (csz : nat) : list (nat × name) → list (nat × name) | [] := [] | ((csz', n)::ls) := if csz < csz' then prune_active_bps_core ls else ((csz',n)::ls) meta def prune_active_bps (s : state) : vm state := do sz ← vm.call_stack_size, return {s with active_bps := prune_active_bps_core sz s^.active_bps} meta def updt_csz (s : state) : vm state := do sz ← vm.call_stack_size, return {s with csz := sz} meta def init_transition (s : state) : vm state := do opts ← vm.get_options, if opts^.get_bool `server ff then return {s with md := mode.done} else do bps ← vm.get_attribute `breakpoint, new_s ← return {s with fn_bps := bps}, if opts^.get_bool `debugger.autorun ff then return {new_s with md := mode.run} else do vm.put_str "Lean debugger\n", show_curr_fn "debugging", vm.put_str "type 'help' for help\n", cmd_loop new_s [] meta def step_transition (s : state) : vm state := do sz ← vm.call_stack_size, if sz = s^.csz then return s else do show_curr_fn "step", cmd_loop s ["s"] meta def bp_reached (s : state) : vm bool := (do fn ← vm.curr_fn, return $ s^.fn_bps^.any (λ p, p^.is_prefix_of fn)) <|> return ff meta def in_active_bps (s : state) : vm bool := do sz ← vm.call_stack_size, match s^.active_bps with | [] := return ff | (csz, _)::_ := return (sz = csz) end meta def run_transition (s : state) : vm state := do b1 ← in_active_bps s, b2 ← bp_reached s, if b1 ∨ not b2 then return s else do show_curr_fn "breakpoint", fn ← vm.curr_fn, sz ← vm.call_stack_size, new_s ← return $ {s with active_bps := (sz, fn) :: s^.active_bps}, cmd_loop new_s ["r"] meta def step_fn (s : state) : vm state := do s ← prune_active_bps s, if s^.md = mode.init then do new_s ← init_transition s, updt_csz new_s else if s^.md = mode.done then return s else if s^.md = mode.step then do new_s ← step_transition s, updt_csz new_s else if s^.md = mode.run then do new_s ← run_transition s, updt_csz new_s else return s meta def monitor : vm_monitor state := { init := init_state, step := step_fn } end debugger run_command vm_monitor.register `debugger.monitor
4d71a211db9455e64d279ab78ffd2ff776cdf78b
9dd3f3912f7321eb58ee9aa8f21778ad6221f87c
/tests/lean/anc1.lean
079106b0e4e924fa4682343d640fd946321302aa
[ "Apache-2.0" ]
permissive
bre7k30/lean
de893411bcfa7b3c5572e61b9e1c52951b310aa4
5a924699d076dab1bd5af23a8f910b433e598d7a
refs/heads/master
1,610,900,145,817
1,488,006,845,000
1,488,006,845,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,298
lean
check (⟨1, 2⟩ : nat × nat) check (⟨trivial, trivial⟩ : true ∧ true) example : true := sorry check (⟨1, sorry⟩ : Σ' x : nat, x > 0) open tactic check show true, from ⟨⟩ check (⟨1, by intro1 >> contradiction⟩ : ∃ x : nat, 1 ≠ 0) universe variables u v check λ (A B C : Prop), assume (Ha : A) (Hb : B) (Hc : C), show B ∧ A, from ⟨Hb, Ha⟩ check λ (A B C : Prop), assume (Ha : A) (Hb : B) (Hc : C), show B ∧ A ∧ C ∧ A, from ⟨Hb, ⟨Ha, ⟨Hc, Ha⟩⟩⟩ check λ (A B C : Prop), assume (Ha : A) (Hb : B) (Hc : C), show B ∧ A ∧ C ∧ A, from ⟨Hb, Ha, Hc, Ha⟩ check λ (A B C : Prop), assume (Ha : A) (Hb : B) (Hc : C), show ((B ∧ true) ∧ A) ∧ (C ∧ A), from ⟨⟨⟨Hb, ⟨⟩⟩, Ha⟩, ⟨Hc, Ha⟩⟩ check λ (A : Type u) (P : A → Prop) (Q : A → Prop), take (a : A), assume (H1 : P a) (H2 : Q a), show ∃ x, P x ∧ Q x, from ⟨a, ⟨H1, H2⟩⟩ check λ (A : Type u) (P : A → Prop) (Q : A → Prop), take (a : A) (b : A), assume (H1 : P a) (H2 : Q b), show ∃ x y, P x ∧ Q y, from ⟨a, ⟨b, ⟨H1, H2⟩⟩⟩ check λ (A : Type u) (P : A → Prop) (Q : A → Prop), take (a : A) (b : A), assume (H1 : P a) (H2 : Q b), show ∃ x y, P x ∧ Q y, from ⟨a, b, H1, H2⟩
6c81628f44b6b3fe04e980ff4c67bf10b0b65d30
9d2e3d5a2e2342a283affd97eead310c3b528a24
/src/solutions/thursday/afternoon/category_theory/exercise6.lean
2c378fbb004cb907a64735aada7eec3e9eb47a46
[]
permissive
Vtec234/lftcm2020
ad2610ab614beefe44acc5622bb4a7fff9a5ea46
bbbd4c8162f8c2ef602300ab8fdeca231886375d
refs/heads/master
1,668,808,098,623
1,594,989,081,000
1,594,990,079,000
280,423,039
0
0
MIT
1,594,990,209,000
1,594,990,209,000
null
UTF-8
Lean
false
false
2,068
lean
import category_theory.limits.shapes.pullbacks /-! Thanks to Markus Himmel for suggesting this question. -/ open category_theory open category_theory.limits /-! Let C be a category, X and Y be objects and f : X ⟶ Y be a morphism. Show that f is an epimorphism if and only if the diagram X --f--→ Y | | f 𝟙 | | ↓ ↓ Y --𝟙--→ Y is a pushout. -/ variables {C : Type*} [category C] def pushout_of_epi {X Y : C} (f : X ⟶ Y) [epi f] : is_colimit (pushout_cocone.mk (𝟙 Y) (𝟙 Y) rfl : pushout_cocone f f) := -- Hint: you can start a proof with `fapply pushout_cocone.is_colimit.mk` -- to save a little bit of work over just building a `is_colimit` structure directly. -- sorry begin fapply pushout_cocone.is_colimit.mk, { intro s, apply s.ι.app walking_span.left, }, { tidy, }, { tidy, apply (cancel_epi f).1, have fst := s.ι.naturality walking_span.hom.fst, simp at fst, rw fst, have snd := s.ι.naturality walking_span.hom.snd, simp at snd, rw snd, }, { tidy, } end -- sorry theorem epi_of_pushout {X Y : C} (f : X ⟶ Y) (is_colim : is_colimit (pushout_cocone.mk (𝟙 Y) (𝟙 Y) rfl : pushout_cocone f f)) : epi f := -- Hint: You can use `pushout_cocone.mk` to conveniently construct a cocone over a cospan. -- Hint: use `is_colim.desc` to construct the map from a colimit cocone to any other cocone. -- Hint: use `is_colim.fac` to show that this map gives a factorisation of the cocone maps through the colimit cocone. -- Hint: if `simp` won't correctly simplify `𝟙 X ≫ f`, try `dsimp, simp`. -- sorry { left_cancellation := λ Z g h hf, begin let a := pushout_cocone.mk _ _ hf, have hg : is_colim.desc a = g, { convert is_colim.fac a walking_span.left, simp, dsimp, simp, }, have hh : is_colim.desc a = h, { convert is_colim.fac a walking_span.right, simp, dsimp, simp, }, rw [←hg, ←hh], end } -- sorry /-! There are some further hints in `src/hints/thursday/afternoon/category_theory/exercise6/` -/
96f3c6620c3ecaf6a44aa7d8a2534bc20278928a
94e33a31faa76775069b071adea97e86e218a8ee
/src/analysis/inner_product_space/l2_space.lean
3c285c1ebc41a0a8490e670ec3f7924875f3bde3
[ "Apache-2.0" ]
permissive
urkud/mathlib
eab80095e1b9f1513bfb7f25b4fa82fa4fd02989
6379d39e6b5b279df9715f8011369a301b634e41
refs/heads/master
1,658,425,342,662
1,658,078,703,000
1,658,078,703,000
186,910,338
0
0
Apache-2.0
1,568,512,083,000
1,557,958,709,000
Lean
UTF-8
Lean
false
false
19,365
lean
/- Copyright (c) 2022 Heather Macbeth. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Heather Macbeth -/ import analysis.inner_product_space.projection import analysis.normed_space.lp_space /-! # Hilbert sum of a family of inner product spaces Given a family `(G : ι → Type*) [Π i, inner_product_space 𝕜 (G i)]` of inner product spaces, this file equips `lp G 2` with an inner product space structure, where `lp G 2` consists of those dependent functions `f : Π i, G i` for which `∑' i, ∥f i∥ ^ 2`, the sum of the norms-squared, is summable. This construction is sometimes called the *Hilbert sum* of the family `G`. By choosing `G` to be `ι → 𝕜`, the Hilbert space `ℓ²(ι, 𝕜)` may be seen as a special case of this construction. ## Main definitions * `orthogonal_family.linear_isometry`: Given a Hilbert space `E`, a family `G` of inner product spaces and a family `V : Π i, G i →ₗᵢ[𝕜] E` of isometric embeddings of the `G i` into `E` with mutually-orthogonal images, there is an induced isometric embedding of the Hilbert sum of `G` into `E`. * `orthogonal_family.linear_isometry_equiv`: Given a Hilbert space `E`, a family `G` of inner product spaces and a family `V : Π i, G i →ₗᵢ[𝕜] E` of isometric embeddings of the `G i` into `E` with mutually-orthogonal images whose span is dense in `E`, there is an induced isometric isomorphism of the Hilbert sum of `G` with `E`. * `hilbert_basis`: We define a *Hilbert basis* of a Hilbert space `E` to be a structure whose single field `hilbert_basis.repr` is an isometric isomorphism of `E` with `ℓ²(ι, 𝕜)` (i.e., the Hilbert sum of `ι` copies of `𝕜`). This parallels the definition of `basis`, in `linear_algebra.basis`, as an isomorphism of an `R`-module with `ι →₀ R`. * `hilbert_basis.has_coe_to_fun`: More conventionally a Hilbert basis is thought of as a family `ι → E` of vectors in `E` satisfying certain properties (orthonormality, completeness). We obtain this interpretation of a Hilbert basis `b` by defining `⇑b`, of type `ι → E`, to be the image under `b.repr` of `lp.single 2 i (1:𝕜)`. This parallels the definition `basis.has_coe_to_fun` in `linear_algebra.basis`. * `hilbert_basis.mk`: Make a Hilbert basis of `E` from an orthonormal family `v : ι → E` of vectors in `E` whose span is dense. This parallels the definition `basis.mk` in `linear_algebra.basis`. * `hilbert_basis.mk_of_orthogonal_eq_bot`: Make a Hilbert basis of `E` from an orthonormal family `v : ι → E` of vectors in `E` whose span has trivial orthogonal complement. ## Main results * `lp.inner_product_space`: Construction of the inner product space instance on the Hilbert sum `lp G 2`. Note that from the file `analysis.normed_space.lp_space`, the space `lp G 2` already held a normed space instance (`lp.normed_space`), and if each `G i` is a Hilbert space (i.e., complete), then `lp G 2` was already known to be complete (`lp.complete_space`). So the work here is to define the inner product and show it is compatible. * `orthogonal_family.range_linear_isometry`: Given a family `G` of inner product spaces and a family `V : Π i, G i →ₗᵢ[𝕜] E` of isometric embeddings of the `G i` into `E` with mutually-orthogonal images, the image of the embedding `orthogonal_family.linear_isometry` of the Hilbert sum of `G` into `E` is the closure of the span of the images of the `G i`. * `hilbert_basis.repr_apply_apply`: Given a Hilbert basis `b` of `E`, the entry `b.repr x i` of `x`'s representation in `ℓ²(ι, 𝕜)` is the inner product `⟪b i, x⟫`. * `hilbert_basis.has_sum_repr`: Given a Hilbert basis `b` of `E`, a vector `x` in `E` can be expressed as the "infinite linear combination" `∑' i, b.repr x i • b i` of the basis vectors `b i`, with coefficients given by the entries `b.repr x i` of `x`'s representation in `ℓ²(ι, 𝕜)`. * `exists_hilbert_basis`: A Hilbert space admits a Hilbert basis. ## Keywords Hilbert space, Hilbert sum, l2, Hilbert basis, unitary equivalence, isometric isomorphism -/ open is_R_or_C submodule filter open_locale big_operators nnreal ennreal classical complex_conjugate noncomputable theory variables {ι : Type*} variables {𝕜 : Type*} [is_R_or_C 𝕜] {E : Type*} [inner_product_space 𝕜 E] [cplt : complete_space E] variables {G : ι → Type*} [Π i, inner_product_space 𝕜 (G i)] local notation `⟪`x`, `y`⟫` := @inner 𝕜 _ _ x y notation `ℓ²(` ι `,` 𝕜 `)` := lp (λ i : ι, 𝕜) 2 /-! ### Inner product space structure on `lp G 2` -/ namespace lp lemma summable_inner (f g : lp G 2) : summable (λ i, ⟪f i, g i⟫) := begin -- Apply the Direct Comparison Test, comparing with ∑' i, ∥f i∥ * ∥g i∥ (summable by Hölder) refine summable_of_norm_bounded (λ i, ∥f i∥ * ∥g i∥) (lp.summable_mul _ f g) _, { rw real.is_conjugate_exponent_iff; norm_num }, intros i, -- Then apply Cauchy-Schwarz pointwise exact norm_inner_le_norm _ _, end instance : inner_product_space 𝕜 (lp G 2) := { inner := λ f g, ∑' i, ⟪f i, g i⟫, norm_sq_eq_inner := λ f, begin calc ∥f∥ ^ 2 = ∥f∥ ^ (2:ℝ≥0∞).to_real : by norm_cast ... = ∑' i, ∥f i∥ ^ (2:ℝ≥0∞).to_real : lp.norm_rpow_eq_tsum _ f ... = ∑' i, ∥f i∥ ^ 2 : by norm_cast ... = ∑' i, re ⟪f i, f i⟫ : by simp only [norm_sq_eq_inner] ... = re (∑' i, ⟪f i, f i⟫) : (is_R_or_C.re_clm.map_tsum _).symm ... = _ : by congr, { norm_num }, { exact summable_inner f f }, end, conj_sym := λ f g, begin calc conj _ = conj ∑' i, ⟪g i, f i⟫ : by congr ... = ∑' i, conj ⟪g i, f i⟫ : is_R_or_C.conj_cle.map_tsum ... = ∑' i, ⟪f i, g i⟫ : by simp only [inner_conj_sym] ... = _ : by congr, end, add_left := λ f₁ f₂ g, begin calc _ = ∑' i, ⟪(f₁ + f₂) i, g i⟫ : _ ... = ∑' i, (⟪f₁ i, g i⟫ + ⟪f₂ i, g i⟫) : by simp only [inner_add_left, pi.add_apply, coe_fn_add] ... = (∑' i, ⟪f₁ i, g i⟫) + ∑' i, ⟪f₂ i, g i⟫ : tsum_add _ _ ... = _ : by congr, { congr, }, { exact summable_inner f₁ g }, { exact summable_inner f₂ g } end, smul_left := λ f g c, begin calc _ = ∑' i, ⟪c • f i, g i⟫ : _ ... = ∑' i, conj c * ⟪f i, g i⟫ : by simp only [inner_smul_left] ... = conj c * ∑' i, ⟪f i, g i⟫ : tsum_mul_left ... = _ : _, { simp only [coe_fn_smul, pi.smul_apply] }, { congr }, end, .. lp.normed_space } lemma inner_eq_tsum (f g : lp G 2) : ⟪f, g⟫ = ∑' i, ⟪f i, g i⟫ := rfl lemma has_sum_inner (f g : lp G 2) : has_sum (λ i, ⟪f i, g i⟫) ⟪f, g⟫ := (summable_inner f g).has_sum lemma inner_single_left (i : ι) (a : G i) (f : lp G 2) : ⟪lp.single 2 i a, f⟫ = ⟪a, f i⟫ := begin refine (has_sum_inner (lp.single 2 i a) f).unique _, convert has_sum_ite_eq i ⟪a, f i⟫, ext j, rw lp.single_apply, split_ifs, { subst h }, { simp } end lemma inner_single_right (i : ι) (a : G i) (f : lp G 2) : ⟪f, lp.single 2 i a⟫ = ⟪f i, a⟫ := by simpa [inner_conj_sym] using congr_arg conj (inner_single_left i a f) end lp /-! ### Identification of a general Hilbert space `E` with a Hilbert sum -/ namespace orthogonal_family variables {V : Π i, G i →ₗᵢ[𝕜] E} (hV : orthogonal_family 𝕜 V) include cplt hV protected lemma summable_of_lp (f : lp G 2) : summable (λ i, V i (f i)) := begin rw hV.summable_iff_norm_sq_summable, convert (lp.mem_ℓp f).summable _, { norm_cast }, { norm_num } end /-- A mutually orthogonal family of subspaces of `E` induce a linear isometry from `lp 2` of the subspaces into `E`. -/ protected def linear_isometry : lp G 2 →ₗᵢ[𝕜] E := { to_fun := λ f, ∑' i, V i (f i), map_add' := λ f g, by simp only [tsum_add (hV.summable_of_lp f) (hV.summable_of_lp g), lp.coe_fn_add, pi.add_apply, linear_isometry.map_add], map_smul' := λ c f, by simpa only [linear_isometry.map_smul, pi.smul_apply, lp.coe_fn_smul] using tsum_const_smul (hV.summable_of_lp f), norm_map' := λ f, begin classical, -- needed for lattice instance on `finset ι`, for `filter.at_top_ne_bot` have H : 0 < (2:ℝ≥0∞).to_real := by norm_num, suffices : ∥∑' (i : ι), V i (f i)∥ ^ ((2:ℝ≥0∞).to_real) = ∥f∥ ^ ((2:ℝ≥0∞).to_real), { exact real.rpow_left_inj_on H.ne' (norm_nonneg _) (norm_nonneg _) this }, refine tendsto_nhds_unique _ (lp.has_sum_norm H f), convert (hV.summable_of_lp f).has_sum.norm.rpow_const (or.inr H.le), ext s, exact_mod_cast (hV.norm_sum f s).symm, end } protected lemma linear_isometry_apply (f : lp G 2) : hV.linear_isometry f = ∑' i, V i (f i) := rfl protected lemma has_sum_linear_isometry (f : lp G 2) : has_sum (λ i, V i (f i)) (hV.linear_isometry f) := (hV.summable_of_lp f).has_sum @[simp] protected lemma linear_isometry_apply_single {i : ι} (x : G i) : hV.linear_isometry (lp.single 2 i x) = V i x := begin rw [hV.linear_isometry_apply, ← tsum_ite_eq i (V i x)], congr, ext j, rw [lp.single_apply], split_ifs, { subst h }, { simp } end @[simp] protected lemma linear_isometry_apply_dfinsupp_sum_single (W₀ : Π₀ (i : ι), G i) : hV.linear_isometry (W₀.sum (lp.single 2)) = W₀.sum (λ i, V i) := begin have : hV.linear_isometry (∑ i in W₀.support, lp.single 2 i (W₀ i)) = ∑ i in W₀.support, hV.linear_isometry (lp.single 2 i (W₀ i)), { exact hV.linear_isometry.to_linear_map.map_sum }, simp [dfinsupp.sum, this] {contextual := tt}, end /-- The canonical linear isometry from the `lp 2` of a mutually orthogonal family of subspaces of `E` into E, has range the closure of the span of the subspaces. -/ protected lemma range_linear_isometry [Π i, complete_space (G i)] : hV.linear_isometry.to_linear_map.range = (⨆ i, (V i).to_linear_map.range).topological_closure := begin refine le_antisymm _ _, { rintros x ⟨f, rfl⟩, refine mem_closure_of_tendsto (hV.has_sum_linear_isometry f) (eventually_of_forall _), intros s, rw set_like.mem_coe, refine sum_mem _, intros i hi, refine mem_supr_of_mem i _, exact linear_map.mem_range_self _ (f i) }, { apply topological_closure_minimal, { refine supr_le _, rintros i x ⟨x, rfl⟩, use lp.single 2 i x, exact hV.linear_isometry_apply_single x }, exact hV.linear_isometry.isometry.uniform_inducing.is_complete_range.is_closed } end /-- A mutually orthogonal family of complete subspaces of `E`, whose range is dense in `E`, induces a isometric isomorphism from E to `lp 2` of the subspaces. Note that this goes in the opposite direction from `orthogonal_family.linear_isometry`. -/ noncomputable def linear_isometry_equiv [Π i, complete_space (G i)] (hV' : (⨆ i, (V i).to_linear_map.range).topological_closure = ⊤) : E ≃ₗᵢ[𝕜] lp G 2 := linear_isometry_equiv.symm $ linear_isometry_equiv.of_surjective hV.linear_isometry begin rw [←linear_isometry.coe_to_linear_map], refine linear_map.range_eq_top.mp _, rw ← hV', rw hV.range_linear_isometry, end /-- In the canonical isometric isomorphism `E ≃ₗᵢ[𝕜] lp G 2` induced by an orthogonal family `G`, a vector `w : lp G 2` is the image of the infinite sum of the associated elements in `E`. -/ protected lemma linear_isometry_equiv_symm_apply [Π i, complete_space (G i)] (hV' : (⨆ i, (V i).to_linear_map.range).topological_closure = ⊤) (w : lp G 2) : (hV.linear_isometry_equiv hV').symm w = ∑' i, V i (w i) := by simp [orthogonal_family.linear_isometry_equiv, orthogonal_family.linear_isometry_apply] /-- In the canonical isometric isomorphism `E ≃ₗᵢ[𝕜] lp G 2` induced by an orthogonal family `G`, a vector `w : lp G 2` is the image of the infinite sum of the associated elements in `E`, and this sum indeed converges. -/ protected lemma has_sum_linear_isometry_equiv_symm [Π i, complete_space (G i)] (hV' : (⨆ i, (V i).to_linear_map.range).topological_closure = ⊤) (w : lp G 2) : has_sum (λ i, V i (w i)) ((hV.linear_isometry_equiv hV').symm w) := by simp [orthogonal_family.linear_isometry_equiv, orthogonal_family.has_sum_linear_isometry] /-- In the canonical isometric isomorphism `E ≃ₗᵢ[𝕜] lp G 2` induced by an `ι`-indexed orthogonal family `G`, an "elementary basis vector" in `lp G 2` supported at `i : ι` is the image of the associated element in `E`. -/ @[simp] protected lemma linear_isometry_equiv_symm_apply_single [Π i, complete_space (G i)] (hV' : (⨆ i, (V i).to_linear_map.range).topological_closure = ⊤) {i : ι} (x : G i) : (hV.linear_isometry_equiv hV').symm (lp.single 2 i x) = V i x := by simp [orthogonal_family.linear_isometry_equiv, orthogonal_family.linear_isometry_apply_single] /-- In the canonical isometric isomorphism `E ≃ₗᵢ[𝕜] lp G 2` induced by an `ι`-indexed orthogonal family `G`, a finitely-supported vector in `lp G 2` is the image of the associated finite sum of elements of `E`. -/ @[simp] protected lemma linear_isometry_equiv_symm_apply_dfinsupp_sum_single [Π i, complete_space (G i)] (hV' : (⨆ i, (V i).to_linear_map.range).topological_closure = ⊤) (W₀ : Π₀ (i : ι), G i) : (hV.linear_isometry_equiv hV').symm (W₀.sum (lp.single 2)) = (W₀.sum (λ i, V i)) := by simp [orthogonal_family.linear_isometry_equiv, orthogonal_family.linear_isometry_apply_dfinsupp_sum_single] /-- In the canonical isometric isomorphism `E ≃ₗᵢ[𝕜] lp G 2` induced by an `ι`-indexed orthogonal family `G`, a finitely-supported vector in `lp G 2` is the image of the associated finite sum of elements of `E`. -/ @[simp] protected lemma linear_isometry_equiv_apply_dfinsupp_sum_single [Π i, complete_space (G i)] (hV' : (⨆ i, (V i).to_linear_map.range).topological_closure = ⊤) (W₀ : Π₀ (i : ι), G i) : (hV.linear_isometry_equiv hV' (W₀.sum (λ i, V i)) : Π i, G i) = W₀ := begin rw ← hV.linear_isometry_equiv_symm_apply_dfinsupp_sum_single hV', rw linear_isometry_equiv.apply_symm_apply, ext i, simp [dfinsupp.sum, lp.single_apply] {contextual := tt}, end end orthogonal_family /-! ### Hilbert bases -/ section variables (ι) (𝕜) (E) /-- A Hilbert basis on `ι` for an inner product space `E` is an identification of `E` with the `lp` space `ℓ²(ι, 𝕜)`. -/ structure hilbert_basis := of_repr :: (repr : E ≃ₗᵢ[𝕜] ℓ²(ι, 𝕜)) end namespace hilbert_basis instance {ι : Type*} : inhabited (hilbert_basis ι 𝕜 ℓ²(ι, 𝕜)) := ⟨of_repr (linear_isometry_equiv.refl 𝕜 _)⟩ /-- `b i` is the `i`th basis vector. -/ instance : has_coe_to_fun (hilbert_basis ι 𝕜 E) (λ _, ι → E) := { coe := λ b i, b.repr.symm (lp.single 2 i (1:𝕜)) } @[simp] protected lemma repr_symm_single (b : hilbert_basis ι 𝕜 E) (i : ι) : b.repr.symm (lp.single 2 i (1:𝕜)) = b i := rfl @[simp] protected lemma repr_self (b : hilbert_basis ι 𝕜 E) (i : ι) : b.repr (b i) = lp.single 2 i (1:𝕜) := by rw [← b.repr_symm_single, linear_isometry_equiv.apply_symm_apply] protected lemma repr_apply_apply (b : hilbert_basis ι 𝕜 E) (v : E) (i : ι) : b.repr v i = ⟪b i, v⟫ := begin rw [← b.repr.inner_map_map (b i) v, b.repr_self, lp.inner_single_left], simp, end @[simp] protected lemma orthonormal (b : hilbert_basis ι 𝕜 E) : orthonormal 𝕜 b := begin rw orthonormal_iff_ite, intros i j, rw [← b.repr.inner_map_map (b i) (b j), b.repr_self, b.repr_self, lp.inner_single_left, lp.single_apply], simp, end protected lemma has_sum_repr_symm (b : hilbert_basis ι 𝕜 E) (f : ℓ²(ι, 𝕜)) : has_sum (λ i, f i • b i) (b.repr.symm f) := begin suffices H : (λ (i : ι), f i • b i) = (λ (b_1 : ι), (b.repr.symm.to_continuous_linear_equiv) ((λ (i : ι), lp.single 2 i (f i)) b_1)), { rw H, have : has_sum (λ (i : ι), lp.single 2 i (f i)) f := lp.has_sum_single ennreal.two_ne_top f, exact (↑(b.repr.symm.to_continuous_linear_equiv) : ℓ²(ι, 𝕜) →L[𝕜] E).has_sum this }, ext i, apply b.repr.injective, have : lp.single 2 i (f i * 1) = f i • lp.single 2 i 1 := lp.single_smul 2 i (1:𝕜) (f i), rw mul_one at this, rw [linear_isometry_equiv.map_smul, b.repr_self, ← this, linear_isometry_equiv.coe_to_continuous_linear_equiv], exact (b.repr.apply_symm_apply (lp.single 2 i (f i))).symm, end protected lemma has_sum_repr (b : hilbert_basis ι 𝕜 E) (x : E) : has_sum (λ i, b.repr x i • b i) x := by simpa using b.has_sum_repr_symm (b.repr x) @[simp] protected lemma dense_span (b : hilbert_basis ι 𝕜 E) : (span 𝕜 (set.range b)).topological_closure = ⊤ := begin classical, rw eq_top_iff, rintros x -, refine mem_closure_of_tendsto (b.has_sum_repr x) (eventually_of_forall _), intros s, simp only [set_like.mem_coe], refine sum_mem _, rintros i -, refine smul_mem _ _ _, exact subset_span ⟨i, rfl⟩ end variables {v : ι → E} (hv : orthonormal 𝕜 v) include hv cplt /-- An orthonormal family of vectors whose span is dense in the whole module is a Hilbert basis. -/ protected def mk (hsp : (span 𝕜 (set.range v)).topological_closure = ⊤) : hilbert_basis ι 𝕜 E := hilbert_basis.of_repr $ hv.orthogonal_family.linear_isometry_equiv begin convert hsp, simp [← linear_map.span_singleton_eq_range, ← submodule.span_Union], end lemma _root_.orthonormal.linear_isometry_equiv_symm_apply_single_one (h i) : (hv.orthogonal_family.linear_isometry_equiv h).symm (lp.single 2 i 1) = v i := by rw [orthogonal_family.linear_isometry_equiv_symm_apply_single, linear_isometry.to_span_singleton_apply, one_smul] @[simp] protected lemma coe_mk (hsp : (span 𝕜 (set.range v)).topological_closure = ⊤) : ⇑(hilbert_basis.mk hv hsp) = v := funext $ orthonormal.linear_isometry_equiv_symm_apply_single_one hv _ /-- An orthonormal family of vectors whose span has trivial orthogonal complement is a Hilbert basis. -/ protected def mk_of_orthogonal_eq_bot (hsp : (span 𝕜 (set.range v))ᗮ = ⊥) : hilbert_basis ι 𝕜 E := hilbert_basis.mk hv (by rw [← orthogonal_orthogonal_eq_closure, orthogonal_eq_top_iff, hsp]) @[simp] protected lemma coe_of_orthogonal_eq_bot_mk (hsp : (span 𝕜 (set.range v))ᗮ = ⊥) : ⇑(hilbert_basis.mk_of_orthogonal_eq_bot hv hsp) = v := hilbert_basis.coe_mk hv _ omit hv /-- A Hilbert space admits a Hilbert basis extending a given orthonormal subset. -/ lemma _root_.orthonormal.exists_hilbert_basis_extension {s : set E} (hs : orthonormal 𝕜 (coe : s → E)) : ∃ (w : set E) (b : hilbert_basis w 𝕜 E), s ⊆ w ∧ ⇑b = (coe : w → E) := let ⟨w, hws, hw_ortho, hw_max⟩ := exists_maximal_orthonormal hs in ⟨ w, hilbert_basis.mk_of_orthogonal_eq_bot hw_ortho (by simpa [maximal_orthonormal_iff_orthogonal_complement_eq_bot hw_ortho] using hw_max), hws, hilbert_basis.coe_of_orthogonal_eq_bot_mk _ _ ⟩ variables (𝕜 E) /-- A Hilbert space admits a Hilbert basis. -/ lemma _root_.exists_hilbert_basis : ∃ (w : set E) (b : hilbert_basis w 𝕜 E), ⇑b = (coe : w → E) := let ⟨w, hw, hw', hw''⟩ := (orthonormal_empty 𝕜 E).exists_hilbert_basis_extension in ⟨w, hw, hw''⟩ end hilbert_basis
22d4bd4b70ea5e2ba8a5ed57dc5b421d85464fbf
9dc8cecdf3c4634764a18254e94d43da07142918
/src/data/real/ennreal.lean
99a303554d9a8a6788aa92343e51e1d6de9cbc80
[ "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
81,142
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 data.real.nnreal /-! # Extended non-negative reals We define `ennreal = ℝ≥0∞ := with_top ℝ≥0` to be the type of extended nonnegative real numbers, i.e., the interval `[0, +∞]`. This type is used as the codomain of a `measure_theory.measure`, and of the extended distance `edist` in a `emetric_space`. In this file we define some algebraic operations and a linear order on `ℝ≥0∞` and prove basic properties of these operations, order, and conversions to/from `ℝ`, `ℝ≥0`, and `ℕ`. ## Main definitions * `ℝ≥0∞`: the extended nonnegative real numbers `[0, ∞]`; defined as `with_top ℝ≥0`; it is equipped with the following structures: - coercion from `ℝ≥0` defined in the natural way; - the natural structure of a complete dense linear order: `↑p ≤ ↑q ↔ p ≤ q` and `∀ a, a ≤ ∞`; - `a + b` is defined so that `↑p + ↑q = ↑(p + q)` for `(p q : ℝ≥0)` and `a + ∞ = ∞ + a = ∞`; - `a * b` is defined so that `↑p * ↑q = ↑(p * q)` for `(p q : ℝ≥0)`, `0 * ∞ = ∞ * 0 = 0`, and `a * ∞ = ∞ * a = ∞` for `a ≠ 0`; - `a - b` is defined as the minimal `d` such that `a ≤ d + b`; this way we have `↑p - ↑q = ↑(p - q)`, `∞ - ↑p = ∞`, `↑p - ∞ = ∞ - ∞ = 0`; note that there is no negation, only subtraction; - `a⁻¹` is defined as `Inf {b | 1 ≤ a * b}`. This way we have `(↑p)⁻¹ = ↑(p⁻¹)` for `p : ℝ≥0`, `p ≠ 0`, `0⁻¹ = ∞`, and `∞⁻¹ = 0`. - `a / b` is defined as `a * b⁻¹`. The addition and multiplication defined this way together with `0 = ↑0` and `1 = ↑1` turn `ℝ≥0∞` into a canonically ordered commutative semiring of characteristic zero. * Coercions to/from other types: - coercion `ℝ≥0 → ℝ≥0∞` is defined as `has_coe`, so one can use `(p : ℝ≥0)` in a context that expects `a : ℝ≥0∞`, and Lean will apply `coe` automatically; - `ennreal.to_nnreal` sends `↑p` to `p` and `∞` to `0`; - `ennreal.to_real := coe ∘ ennreal.to_nnreal` sends `↑p`, `p : ℝ≥0` to `(↑p : ℝ)` and `∞` to `0`; - `ennreal.of_real := coe ∘ real.to_nnreal` sends `x : ℝ` to `↑⟨max x 0, _⟩` - `ennreal.ne_top_equiv_nnreal` is an equivalence between `{a : ℝ≥0∞ // a ≠ 0}` and `ℝ≥0`. ## Implementation notes We define a `can_lift ℝ≥0∞ ℝ≥0` instance, so one of the ways to prove theorems about an `ℝ≥0∞` number `a` is to consider the cases `a = ∞` and `a ≠ ∞`, and use the tactic `lift a to ℝ≥0 using ha` in the second case. This instance is even more useful if one already has `ha : a ≠ ∞` in the context, or if we have `(f : α → ℝ≥0∞) (hf : ∀ x, f x ≠ ∞)`. ## Notations * `ℝ≥0∞`: the type of the extended nonnegative real numbers; * `ℝ≥0`: the type of nonnegative real numbers `[0, ∞)`; defined in `data.real.nnreal`; * `∞`: a localized notation in `ℝ≥0∞` for `⊤ : ℝ≥0∞`. -/ open classical set open_locale classical big_operators nnreal variables {α : Type*} {β : Type*} /-- The extended nonnegative real numbers. This is usually denoted [0, ∞], and is relevant as the codomain of a measure. -/ @[derive [ has_zero, add_comm_monoid_with_one, canonically_ordered_comm_semiring, complete_linear_order, densely_ordered, nontrivial, canonically_linear_ordered_add_monoid, has_sub, has_ordered_sub, linear_ordered_add_comm_monoid_with_top]] def ennreal := with_top ℝ≥0 localized "notation (name := ennreal) `ℝ≥0∞` := ennreal" in ennreal localized "notation (name := ennreal.top) `∞` := (⊤ : ennreal)" in ennreal namespace ennreal variables {a b c d : ℝ≥0∞} {r p q : ℝ≥0} -- TODO: why are the two covariant instances necessary? why aren't they inferred? instance covariant_class_mul_le : covariant_class ℝ≥0∞ ℝ≥0∞ (*) (≤) := canonically_ordered_comm_semiring.to_covariant_mul_le instance covariant_class_add_le : covariant_class ℝ≥0∞ ℝ≥0∞ (+) (≤) := ordered_add_comm_monoid.to_covariant_class_left ℝ≥0∞ noncomputable instance : linear_ordered_comm_monoid_with_zero ℝ≥0∞ := { mul_le_mul_left := λ a b, mul_le_mul_left', zero_le_one := zero_le 1, .. ennreal.linear_ordered_add_comm_monoid_with_top, .. (show comm_semiring ℝ≥0∞, from infer_instance) } instance : inhabited ℝ≥0∞ := ⟨0⟩ instance : has_coe ℝ≥0 ℝ≥0∞ := ⟨ option.some ⟩ instance : can_lift ℝ≥0∞ ℝ≥0 := { coe := coe, cond := λ r, r ≠ ∞, prf := λ x hx, ⟨option.get $ option.ne_none_iff_is_some.1 hx, option.some_get _⟩ } @[simp] lemma none_eq_top : (none : ℝ≥0∞) = ∞ := rfl @[simp] lemma some_eq_coe (a : ℝ≥0) : (some a : ℝ≥0∞) = (↑a : ℝ≥0∞) := rfl /-- `to_nnreal x` returns `x` if it is real, otherwise 0. -/ protected def to_nnreal : ℝ≥0∞ → ℝ≥0 := with_top.untop' 0 /-- `to_real x` returns `x` if it is real, `0` otherwise. -/ protected def to_real (a : ℝ≥0∞) : real := coe (a.to_nnreal) /-- `of_real x` returns `x` if it is nonnegative, `0` otherwise. -/ protected noncomputable def of_real (r : real) : ℝ≥0∞ := coe (real.to_nnreal r) @[simp, norm_cast] lemma to_nnreal_coe : (r : ℝ≥0∞).to_nnreal = r := rfl @[simp] lemma coe_to_nnreal : ∀{a:ℝ≥0∞}, a ≠ ∞ → ↑(a.to_nnreal) = a | (some r) h := rfl | none h := (h rfl).elim @[simp] lemma of_real_to_real {a : ℝ≥0∞} (h : a ≠ ∞) : ennreal.of_real (a.to_real) = a := by simp [ennreal.to_real, ennreal.of_real, h] @[simp] lemma to_real_of_real {r : ℝ} (h : 0 ≤ r) : ennreal.to_real (ennreal.of_real r) = r := by simp [ennreal.to_real, ennreal.of_real, real.coe_to_nnreal _ h] lemma to_real_of_real' {r : ℝ} : ennreal.to_real (ennreal.of_real r) = max r 0 := rfl lemma coe_to_nnreal_le_self : ∀{a:ℝ≥0∞}, ↑(a.to_nnreal) ≤ a | (some r) := by rw [some_eq_coe, to_nnreal_coe]; exact le_rfl | none := le_top lemma coe_nnreal_eq (r : ℝ≥0) : (r : ℝ≥0∞) = ennreal.of_real r := by { rw [ennreal.of_real, real.to_nnreal], cases r with r h, congr, dsimp, rw max_eq_left h } lemma of_real_eq_coe_nnreal {x : ℝ} (h : 0 ≤ x) : ennreal.of_real x = @coe ℝ≥0 ℝ≥0∞ _ (⟨x, h⟩ : ℝ≥0) := by { rw [coe_nnreal_eq], refl } @[simp] lemma of_real_coe_nnreal : ennreal.of_real p = p := (coe_nnreal_eq p).symm @[simp, norm_cast] lemma coe_zero : ↑(0 : ℝ≥0) = (0 : ℝ≥0∞) := rfl @[simp, norm_cast] lemma coe_one : ↑(1 : ℝ≥0) = (1 : ℝ≥0∞) := rfl @[simp] lemma to_real_nonneg {a : ℝ≥0∞} : 0 ≤ a.to_real := by simp [ennreal.to_real] @[simp] lemma top_to_nnreal : ∞.to_nnreal = 0 := rfl @[simp] lemma top_to_real : ∞.to_real = 0 := rfl @[simp] lemma one_to_real : (1 : ℝ≥0∞).to_real = 1 := rfl @[simp] lemma one_to_nnreal : (1 : ℝ≥0∞).to_nnreal = 1 := rfl @[simp] lemma coe_to_real (r : ℝ≥0) : (r : ℝ≥0∞).to_real = r := rfl @[simp] lemma zero_to_nnreal : (0 : ℝ≥0∞).to_nnreal = 0 := rfl @[simp] lemma zero_to_real : (0 : ℝ≥0∞).to_real = 0 := rfl @[simp] lemma of_real_zero : ennreal.of_real (0 : ℝ) = 0 := by simp [ennreal.of_real]; refl @[simp] lemma of_real_one : ennreal.of_real (1 : ℝ) = (1 : ℝ≥0∞) := by simp [ennreal.of_real] lemma of_real_to_real_le {a : ℝ≥0∞} : ennreal.of_real (a.to_real) ≤ a := if ha : a = ∞ then ha.symm ▸ le_top else le_of_eq (of_real_to_real ha) lemma forall_ennreal {p : ℝ≥0∞ → Prop} : (∀a, p a) ↔ (∀r:ℝ≥0, p r) ∧ p ∞ := ⟨assume h, ⟨assume r, h _, h _⟩, assume ⟨h₁, h₂⟩ a, match a with some r := h₁ _ | none := h₂ end⟩ lemma forall_ne_top {p : ℝ≥0∞ → Prop} : (∀ a ≠ ∞, p a) ↔ ∀ r : ℝ≥0, p r := option.ball_ne_none lemma exists_ne_top {p : ℝ≥0∞ → Prop} : (∃ a ≠ ∞, p a) ↔ ∃ r : ℝ≥0, p r := option.bex_ne_none lemma to_nnreal_eq_zero_iff (x : ℝ≥0∞) : x.to_nnreal = 0 ↔ x = 0 ∨ x = ∞ := ⟨begin cases x, { simp [none_eq_top] }, { rintro (rfl : x = 0), exact or.inl rfl }, end, by rintro (h | h); simp [h]⟩ lemma to_real_eq_zero_iff (x : ℝ≥0∞) : x.to_real = 0 ↔ x = 0 ∨ x = ∞ := by simp [ennreal.to_real, to_nnreal_eq_zero_iff] @[simp] lemma coe_ne_top : (r : ℝ≥0∞) ≠ ∞ := with_top.coe_ne_top @[simp] lemma top_ne_coe : ∞ ≠ (r : ℝ≥0∞) := with_top.top_ne_coe @[simp] lemma of_real_ne_top {r : ℝ} : ennreal.of_real r ≠ ∞ := by simp [ennreal.of_real] @[simp] lemma of_real_lt_top {r : ℝ} : ennreal.of_real r < ∞ := lt_top_iff_ne_top.2 of_real_ne_top @[simp] lemma top_ne_of_real {r : ℝ} : ∞ ≠ ennreal.of_real r := by simp [ennreal.of_real] @[simp] lemma zero_ne_top : 0 ≠ ∞ := coe_ne_top @[simp] lemma top_ne_zero : ∞ ≠ 0 := top_ne_coe @[simp] lemma one_ne_top : 1 ≠ ∞ := coe_ne_top @[simp] lemma top_ne_one : ∞ ≠ 1 := top_ne_coe @[simp, norm_cast] lemma coe_eq_coe : (↑r : ℝ≥0∞) = ↑q ↔ r = q := with_top.coe_eq_coe @[simp, norm_cast] lemma coe_le_coe : (↑r : ℝ≥0∞) ≤ ↑q ↔ r ≤ q := with_top.coe_le_coe @[simp, norm_cast] lemma coe_lt_coe : (↑r : ℝ≥0∞) < ↑q ↔ r < q := with_top.coe_lt_coe lemma coe_mono : monotone (coe : ℝ≥0 → ℝ≥0∞) := λ _ _, coe_le_coe.2 @[simp, norm_cast] lemma coe_eq_zero : (↑r : ℝ≥0∞) = 0 ↔ r = 0 := coe_eq_coe @[simp, norm_cast] lemma zero_eq_coe : 0 = (↑r : ℝ≥0∞) ↔ 0 = r := coe_eq_coe @[simp, norm_cast] lemma coe_eq_one : (↑r : ℝ≥0∞) = 1 ↔ r = 1 := coe_eq_coe @[simp, norm_cast] lemma one_eq_coe : 1 = (↑r : ℝ≥0∞) ↔ 1 = r := coe_eq_coe @[simp, norm_cast] lemma coe_nonneg : 0 ≤ (↑r : ℝ≥0∞) ↔ 0 ≤ r := coe_le_coe @[simp, norm_cast] lemma coe_pos : 0 < (↑r : ℝ≥0∞) ↔ 0 < r := coe_lt_coe lemma coe_ne_zero : (r : ℝ≥0∞) ≠ 0 ↔ r ≠ 0 := not_congr coe_eq_coe @[simp, norm_cast] lemma coe_add : ↑(r + p) = (r + p : ℝ≥0∞) := with_top.coe_add @[simp, norm_cast] lemma coe_mul : ↑(r * p) = (r * p : ℝ≥0∞) := with_top.coe_mul @[simp, norm_cast] lemma coe_bit0 : (↑(bit0 r) : ℝ≥0∞) = bit0 r := coe_add @[simp, norm_cast] lemma coe_bit1 : (↑(bit1 r) : ℝ≥0∞) = bit1 r := by simp [bit1] lemma coe_two : ((2:ℝ≥0) : ℝ≥0∞) = 2 := by norm_cast protected lemma zero_lt_one : 0 < (1 : ℝ≥0∞) := canonically_ordered_comm_semiring.zero_lt_one @[simp] lemma one_lt_two : (1 : ℝ≥0∞) < 2 := coe_one ▸ coe_two ▸ by exact_mod_cast (@one_lt_two ℕ _ _) @[simp] lemma zero_lt_two : (0:ℝ≥0∞) < 2 := lt_trans ennreal.zero_lt_one one_lt_two lemma two_ne_zero : (2:ℝ≥0∞) ≠ 0 := (ne_of_lt zero_lt_two).symm lemma two_ne_top : (2:ℝ≥0∞) ≠ ∞ := coe_two ▸ coe_ne_top /-- `(1 : ℝ≥0∞) ≤ 1`, recorded as a `fact` for use with `Lp` spaces. -/ instance _root_.fact_one_le_one_ennreal : fact ((1 : ℝ≥0∞) ≤ 1) := ⟨le_rfl⟩ /-- `(1 : ℝ≥0∞) ≤ 2`, recorded as a `fact` for use with `Lp` spaces. -/ instance _root_.fact_one_le_two_ennreal : fact ((1 : ℝ≥0∞) ≤ 2) := ⟨one_le_two⟩ /-- `(1 : ℝ≥0∞) ≤ ∞`, recorded as a `fact` for use with `Lp` spaces. -/ instance _root_.fact_one_le_top_ennreal : fact ((1 : ℝ≥0∞) ≤ ∞) := ⟨le_top⟩ /-- The set of numbers in `ℝ≥0∞` that are not equal to `∞` is equivalent to `ℝ≥0`. -/ def ne_top_equiv_nnreal : {a | a ≠ ∞} ≃ ℝ≥0 := { to_fun := λ x, ennreal.to_nnreal x, inv_fun := λ x, ⟨x, coe_ne_top⟩, left_inv := λ ⟨x, hx⟩, subtype.eq $ coe_to_nnreal hx, right_inv := λ x, to_nnreal_coe } lemma cinfi_ne_top [has_Inf α] (f : ℝ≥0∞ → α) : (⨅ x : {x // x ≠ ∞}, f x) = ⨅ x : ℝ≥0, f x := eq.symm $ ne_top_equiv_nnreal.symm.surjective.infi_congr _$ λ x, rfl lemma infi_ne_top [complete_lattice α] (f : ℝ≥0∞ → α) : (⨅ x ≠ ∞, f x) = ⨅ x : ℝ≥0, f x := by rw [infi_subtype', cinfi_ne_top] lemma csupr_ne_top [has_Sup α] (f : ℝ≥0∞ → α) : (⨆ x : {x // x ≠ ∞}, f x) = ⨆ x : ℝ≥0, f x := @cinfi_ne_top αᵒᵈ _ _ lemma supr_ne_top [complete_lattice α] (f : ℝ≥0∞ → α) : (⨆ x ≠ ∞, f x) = ⨆ x : ℝ≥0, f x := @infi_ne_top αᵒᵈ _ _ lemma infi_ennreal {α : Type*} [complete_lattice α] {f : ℝ≥0∞ → α} : (⨅ n, f n) = (⨅ n : ℝ≥0, f n) ⊓ f ∞ := le_antisymm (le_inf (le_infi $ assume i, infi_le _ _) (infi_le _ _)) (le_infi $ forall_ennreal.2 ⟨λ r, inf_le_of_left_le $ infi_le _ _, inf_le_right⟩) lemma supr_ennreal {α : Type*} [complete_lattice α] {f : ℝ≥0∞ → α} : (⨆ n, f n) = (⨆ n : ℝ≥0, f n) ⊔ f ∞ := @infi_ennreal αᵒᵈ _ _ @[simp] lemma add_top : a + ∞ = ∞ := add_top _ @[simp] lemma top_add : ∞ + a = ∞ := top_add _ /-- Coercion `ℝ≥0 → ℝ≥0∞` as a `ring_hom`. -/ def of_nnreal_hom : ℝ≥0 →+* ℝ≥0∞ := ⟨coe, coe_one, λ _ _, coe_mul, coe_zero, λ _ _, coe_add⟩ @[simp] lemma coe_of_nnreal_hom : ⇑of_nnreal_hom = coe := rfl section actions /-- A `mul_action` over `ℝ≥0∞` restricts to a `mul_action` over `ℝ≥0`. -/ noncomputable instance {M : Type*} [mul_action ℝ≥0∞ M] : mul_action ℝ≥0 M := mul_action.comp_hom M of_nnreal_hom.to_monoid_hom lemma smul_def {M : Type*} [mul_action ℝ≥0∞ M] (c : ℝ≥0) (x : M) : c • x = (c : ℝ≥0∞) • x := rfl instance {M N : Type*} [mul_action ℝ≥0∞ M] [mul_action ℝ≥0∞ N] [has_smul M N] [is_scalar_tower ℝ≥0∞ M N] : is_scalar_tower ℝ≥0 M N := { smul_assoc := λ r, (smul_assoc (r : ℝ≥0∞) : _)} instance smul_comm_class_left {M N : Type*} [mul_action ℝ≥0∞ N] [has_smul M N] [smul_comm_class ℝ≥0∞ M N] : smul_comm_class ℝ≥0 M N := { smul_comm := λ r, (smul_comm (r : ℝ≥0∞) : _)} instance smul_comm_class_right {M N : Type*} [mul_action ℝ≥0∞ N] [has_smul M N] [smul_comm_class M ℝ≥0∞ N] : smul_comm_class M ℝ≥0 N := { smul_comm := λ m r, (smul_comm m (r : ℝ≥0∞) : _)} /-- A `distrib_mul_action` over `ℝ≥0∞` restricts to a `distrib_mul_action` over `ℝ≥0`. -/ noncomputable instance {M : Type*} [add_monoid M] [distrib_mul_action ℝ≥0∞ M] : distrib_mul_action ℝ≥0 M := distrib_mul_action.comp_hom M of_nnreal_hom.to_monoid_hom /-- A `module` over `ℝ≥0∞` restricts to a `module` over `ℝ≥0`. -/ noncomputable instance {M : Type*} [add_comm_monoid M] [module ℝ≥0∞ M] : module ℝ≥0 M := module.comp_hom M of_nnreal_hom /-- An `algebra` over `ℝ≥0∞` restricts to an `algebra` over `ℝ≥0`. -/ noncomputable instance {A : Type*} [semiring A] [algebra ℝ≥0∞ A] : algebra ℝ≥0 A := { smul := (•), commutes' := λ r x, by simp [algebra.commutes], smul_def' := λ r x, by simp [←algebra.smul_def (r : ℝ≥0∞) x, smul_def], to_ring_hom := ((algebra_map ℝ≥0∞ A).comp (of_nnreal_hom : ℝ≥0 →+* ℝ≥0∞)) } -- verify that the above produces instances we might care about noncomputable example : algebra ℝ≥0 ℝ≥0∞ := infer_instance noncomputable example : distrib_mul_action ℝ≥0ˣ ℝ≥0∞ := infer_instance lemma coe_smul {R} (r : R) (s : ℝ≥0) [has_smul R ℝ≥0] [has_smul R ℝ≥0∞] [is_scalar_tower R ℝ≥0 ℝ≥0] [is_scalar_tower R ℝ≥0 ℝ≥0∞] : (↑(r • s) : ℝ≥0∞) = r • ↑s := by rw [←smul_one_smul ℝ≥0 r (s: ℝ≥0∞), smul_def, smul_eq_mul, ←ennreal.coe_mul, smul_mul_assoc, one_mul] end actions @[simp, norm_cast] lemma coe_indicator {α} (s : set α) (f : α → ℝ≥0) (a : α) : ((s.indicator f a : ℝ≥0) : ℝ≥0∞) = s.indicator (λ x, f x) a := (of_nnreal_hom : ℝ≥0 →+ ℝ≥0∞).map_indicator _ _ _ @[simp, norm_cast] lemma coe_pow (n : ℕ) : (↑(r^n) : ℝ≥0∞) = r^n := of_nnreal_hom.map_pow r n @[simp] lemma add_eq_top : a + b = ∞ ↔ a = ∞ ∨ b = ∞ := with_top.add_eq_top @[simp] lemma add_lt_top : a + b < ∞ ↔ a < ∞ ∧ b < ∞ := with_top.add_lt_top lemma to_nnreal_add {r₁ r₂ : ℝ≥0∞} (h₁ : r₁ ≠ ∞) (h₂ : r₂ ≠ ∞) : (r₁ + r₂).to_nnreal = r₁.to_nnreal + r₂.to_nnreal := by { lift r₁ to ℝ≥0 using h₁, lift r₂ to ℝ≥0 using h₂, refl } lemma not_lt_top {x : ℝ≥0∞} : ¬ x < ∞ ↔ x = ∞ := by rw [lt_top_iff_ne_top, not_not] lemma add_ne_top : a + b ≠ ∞ ↔ a ≠ ∞ ∧ b ≠ ∞ := by simpa only [lt_top_iff_ne_top] using add_lt_top lemma mul_top : a * ∞ = (if a = 0 then 0 else ∞) := begin split_ifs, { simp [h] }, { exact with_top.mul_top h } end lemma top_mul : ∞ * a = (if a = 0 then 0 else ∞) := begin split_ifs, { simp [h] }, { exact with_top.top_mul h } end @[simp] lemma top_mul_top : ∞ * ∞ = ∞ := with_top.top_mul_top lemma top_pow {n:ℕ} (h : 0 < n) : ∞^n = ∞ := nat.le_induction (pow_one _) (λ m hm hm', by rw [pow_succ, hm', top_mul_top]) _ (nat.succ_le_of_lt h) lemma mul_eq_top : a * b = ∞ ↔ (a ≠ 0 ∧ b = ∞) ∨ (a = ∞ ∧ b ≠ 0) := with_top.mul_eq_top_iff lemma mul_lt_top : a ≠ ∞ → b ≠ ∞ → a * b < ∞ := with_top.mul_lt_top lemma mul_ne_top : a ≠ ∞ → b ≠ ∞ → a * b ≠ ∞ := by simpa only [lt_top_iff_ne_top] using mul_lt_top lemma lt_top_of_mul_ne_top_left (h : a * b ≠ ∞) (hb : b ≠ 0) : a < ∞ := lt_top_iff_ne_top.2 $ λ ha, h $ mul_eq_top.2 (or.inr ⟨ha, hb⟩) lemma lt_top_of_mul_ne_top_right (h : a * b ≠ ∞) (ha : a ≠ 0) : b < ∞ := lt_top_of_mul_ne_top_left (by rwa [mul_comm]) ha lemma mul_lt_top_iff {a b : ℝ≥0∞} : a * b < ∞ ↔ (a < ∞ ∧ b < ∞) ∨ a = 0 ∨ b = 0 := begin split, { intro h, rw [← or_assoc, or_iff_not_imp_right, or_iff_not_imp_right], intros hb ha, exact ⟨lt_top_of_mul_ne_top_left h.ne hb, lt_top_of_mul_ne_top_right h.ne ha⟩ }, { rintro (⟨ha, hb⟩|rfl|rfl); [exact mul_lt_top ha.ne hb.ne, simp, simp] } end lemma mul_self_lt_top_iff {a : ℝ≥0∞} : a * a < ⊤ ↔ a < ⊤ := by { rw [ennreal.mul_lt_top_iff, and_self, or_self, or_iff_left_iff_imp], rintro rfl, norm_num } lemma mul_pos_iff : 0 < a * b ↔ 0 < a ∧ 0 < b := canonically_ordered_comm_semiring.mul_pos lemma mul_pos (ha : a ≠ 0) (hb : b ≠ 0) : 0 < a * b := mul_pos_iff.2 ⟨pos_iff_ne_zero.2 ha, pos_iff_ne_zero.2 hb⟩ @[simp] lemma pow_eq_top_iff {n : ℕ} : a ^ n = ∞ ↔ a = ∞ ∧ n ≠ 0 := begin induction n with n ihn, { simp }, rw [pow_succ, mul_eq_top, ihn], fsplit, { rintro (⟨-,rfl,h0⟩|⟨rfl,h0⟩); exact ⟨rfl, n.succ_ne_zero⟩ }, { rintro ⟨rfl, -⟩, exact or.inr ⟨rfl, pow_ne_zero n top_ne_zero⟩ } end lemma pow_eq_top (n : ℕ) (h : a ^ n = ∞) : a = ∞ := (pow_eq_top_iff.1 h).1 lemma pow_ne_top (h : a ≠ ∞) {n:ℕ} : a^n ≠ ∞ := mt (pow_eq_top n) h lemma pow_lt_top : a < ∞ → ∀ n:ℕ, a^n < ∞ := by simpa only [lt_top_iff_ne_top] using pow_ne_top @[simp, norm_cast] lemma coe_finset_sum {s : finset α} {f : α → ℝ≥0} : ↑(∑ a in s, f a) = (∑ a in s, f a : ℝ≥0∞) := of_nnreal_hom.map_sum f s @[simp, norm_cast] lemma coe_finset_prod {s : finset α} {f : α → ℝ≥0} : ↑(∏ a in s, f a) = ((∏ a in s, f a) : ℝ≥0∞) := of_nnreal_hom.map_prod f s section order @[simp] lemma bot_eq_zero : (⊥ : ℝ≥0∞) = 0 := rfl @[simp] lemma coe_lt_top : coe r < ∞ := with_top.coe_lt_top r @[simp] lemma not_top_le_coe : ¬ ∞ ≤ ↑r := with_top.not_top_le_coe r @[simp, norm_cast] lemma one_le_coe_iff : (1:ℝ≥0∞) ≤ ↑r ↔ 1 ≤ r := coe_le_coe @[simp, norm_cast] lemma coe_le_one_iff : ↑r ≤ (1:ℝ≥0∞) ↔ r ≤ 1 := coe_le_coe @[simp, norm_cast] lemma coe_lt_one_iff : (↑p : ℝ≥0∞) < 1 ↔ p < 1 := coe_lt_coe @[simp, norm_cast] lemma one_lt_coe_iff : 1 < (↑p : ℝ≥0∞) ↔ 1 < p := coe_lt_coe @[simp, norm_cast] lemma coe_nat (n : ℕ) : ((n : ℝ≥0) : ℝ≥0∞) = n := with_top.coe_nat n @[simp] lemma of_real_coe_nat (n : ℕ) : ennreal.of_real n = n := by simp [ennreal.of_real] @[simp] lemma nat_ne_top (n : ℕ) : (n : ℝ≥0∞) ≠ ∞ := with_top.nat_ne_top n @[simp] lemma top_ne_nat (n : ℕ) : ∞ ≠ n := with_top.top_ne_nat n @[simp] lemma one_lt_top : 1 < ∞ := coe_lt_top @[simp, norm_cast] lemma to_nnreal_nat (n : ℕ) : (n : ℝ≥0∞).to_nnreal = n := by conv_lhs { rw [← ennreal.coe_nat n, ennreal.to_nnreal_coe] } @[simp, norm_cast] lemma to_real_nat (n : ℕ) : (n : ℝ≥0∞).to_real = n := by conv_lhs { rw [← ennreal.of_real_coe_nat n, ennreal.to_real_of_real (nat.cast_nonneg _)] } lemma le_coe_iff : a ≤ ↑r ↔ (∃p:ℝ≥0, a = p ∧ p ≤ r) := with_top.le_coe_iff lemma coe_le_iff : ↑r ≤ a ↔ (∀p:ℝ≥0, a = p → r ≤ p) := with_top.coe_le_iff lemma lt_iff_exists_coe : a < b ↔ (∃p:ℝ≥0, a = p ∧ ↑p < b) := with_top.lt_iff_exists_coe lemma to_real_le_coe_of_le_coe {a : ℝ≥0∞} {b : ℝ≥0} (h : a ≤ b) : a.to_real ≤ b := show ↑a.to_nnreal ≤ ↑b, begin have : ↑a.to_nnreal = a := ennreal.coe_to_nnreal (lt_of_le_of_lt h coe_lt_top).ne, rw ← this at h, exact_mod_cast h end @[simp, norm_cast] lemma coe_finset_sup {s : finset α} {f : α → ℝ≥0} : ↑(s.sup f) = s.sup (λ x, (f x : ℝ≥0∞)) := finset.comp_sup_eq_sup_comp_of_is_total _ coe_mono rfl lemma pow_le_pow {n m : ℕ} (ha : 1 ≤ a) (h : n ≤ m) : a ^ n ≤ a ^ m := begin cases a, { cases m, { rw eq_bot_iff.mpr h, exact le_rfl }, { rw [none_eq_top, top_pow (nat.succ_pos m)], exact le_top } }, { rw [some_eq_coe, ← coe_pow, ← coe_pow, coe_le_coe], exact pow_le_pow (by simpa using ha) h } end lemma one_le_pow_of_one_le (ha : 1 ≤ a) (n : ℕ) : 1 ≤ a ^ n := by simpa using pow_le_pow ha (zero_le n) @[simp] lemma max_eq_zero_iff : max a b = 0 ↔ a = 0 ∧ b = 0 := by simp only [nonpos_iff_eq_zero.symm, max_le_iff] @[simp] lemma max_zero_left : max 0 a = a := max_eq_right (zero_le a) @[simp] lemma max_zero_right : max a 0 = a := max_eq_left (zero_le a) @[simp] lemma sup_eq_max : a ⊔ b = max a b := rfl protected lemma pow_pos : 0 < a → ∀ n : ℕ, 0 < a^n := canonically_ordered_comm_semiring.pow_pos protected lemma pow_ne_zero : a ≠ 0 → ∀ n : ℕ, a^n ≠ 0 := by simpa only [pos_iff_ne_zero] using ennreal.pow_pos @[simp] lemma not_lt_zero : ¬ a < 0 := by simp protected lemma le_of_add_le_add_left : a ≠ ∞ → a + b ≤ a + c → b ≤ c := with_top.le_of_add_le_add_left protected lemma le_of_add_le_add_right : a ≠ ∞ → b + a ≤ c + a → b ≤ c := with_top.le_of_add_le_add_right protected lemma add_lt_add_left : a ≠ ∞ → b < c → a + b < a + c := with_top.add_lt_add_left protected lemma add_lt_add_right : a ≠ ∞ → b < c → b + a < c + a := with_top.add_lt_add_right protected lemma add_le_add_iff_left : a ≠ ∞ → (a + b ≤ a + c ↔ b ≤ c) := with_top.add_le_add_iff_left protected lemma add_le_add_iff_right : a ≠ ∞ → (b + a ≤ c + a ↔ b ≤ c) := with_top.add_le_add_iff_right protected lemma add_lt_add_iff_left : a ≠ ∞ → (a + b < a + c ↔ b < c) := with_top.add_lt_add_iff_left protected lemma add_lt_add_iff_right : a ≠ ∞ → (b + a < c + a ↔ b < c) := with_top.add_lt_add_iff_right protected lemma add_lt_add_of_le_of_lt : a ≠ ∞ → a ≤ b → c < d → a + c < b + d := with_top.add_lt_add_of_le_of_lt protected lemma add_lt_add_of_lt_of_le : c ≠ ∞ → a < b → c ≤ d → a + c < b + d := with_top.add_lt_add_of_lt_of_le instance contravariant_class_add_lt : contravariant_class ℝ≥0∞ ℝ≥0∞ (+) (<) := with_top.contravariant_class_add_lt lemma lt_add_right (ha : a ≠ ∞) (hb : b ≠ 0) : a < a + b := by rwa [← pos_iff_ne_zero, ←ennreal.add_lt_add_iff_left ha, add_zero] at hb lemma le_of_forall_pos_le_add : ∀{a b : ℝ≥0∞}, (∀ε : ℝ≥0, 0 < ε → b < ∞ → a ≤ b + ε) → a ≤ b | a none h := le_top | none (some a) h := have ∞ ≤ ↑a + ↑(1:ℝ≥0), from h 1 zero_lt_one coe_lt_top, by rw [← coe_add] at this; exact (not_top_le_coe this).elim | (some a) (some b) h := by simp only [none_eq_top, some_eq_coe, coe_add.symm, coe_le_coe, coe_lt_top, true_implies_iff] at *; exact nnreal.le_of_forall_pos_le_add h lemma lt_iff_exists_rat_btwn : a < b ↔ (∃q:ℚ, 0 ≤ q ∧ a < real.to_nnreal q ∧ (real.to_nnreal q:ℝ≥0∞) < b) := ⟨λ h, begin rcases lt_iff_exists_coe.1 h with ⟨p, rfl, _⟩, rcases exists_between h with ⟨c, pc, cb⟩, rcases lt_iff_exists_coe.1 cb with ⟨r, rfl, _⟩, rcases (nnreal.lt_iff_exists_rat_btwn _ _).1 (coe_lt_coe.1 pc) with ⟨q, hq0, pq, qr⟩, exact ⟨q, hq0, coe_lt_coe.2 pq, lt_trans (coe_lt_coe.2 qr) cb⟩ end, λ ⟨q, q0, qa, qb⟩, lt_trans qa qb⟩ lemma lt_iff_exists_real_btwn : a < b ↔ (∃r:ℝ, 0 ≤ r ∧ a < ennreal.of_real r ∧ (ennreal.of_real r:ℝ≥0∞) < b) := ⟨λ h, let ⟨q, q0, aq, qb⟩ := ennreal.lt_iff_exists_rat_btwn.1 h in ⟨q, rat.cast_nonneg.2 q0, aq, qb⟩, λ ⟨q, q0, qa, qb⟩, lt_trans qa qb⟩ lemma lt_iff_exists_nnreal_btwn : a < b ↔ (∃r:ℝ≥0, a < r ∧ (r : ℝ≥0∞) < b) := with_top.lt_iff_exists_coe_btwn lemma lt_iff_exists_add_pos_lt : a < b ↔ (∃ r : ℝ≥0, 0 < r ∧ a + r < b) := begin refine ⟨λ hab, _, λ ⟨r, rpos, hr⟩, lt_of_le_of_lt (le_self_add) hr⟩, cases a, { simpa using hab }, rcases lt_iff_exists_real_btwn.1 hab with ⟨c, c_nonneg, ac, cb⟩, let d : ℝ≥0 := ⟨c, c_nonneg⟩, have ad : a < d, { rw of_real_eq_coe_nnreal c_nonneg at ac, exact coe_lt_coe.1 ac }, refine ⟨d-a, tsub_pos_iff_lt.2 ad, _⟩, rw [some_eq_coe, ← coe_add], convert cb, have : real.to_nnreal c = d, by { rw [← nnreal.coe_eq, real.coe_to_nnreal _ c_nonneg], refl }, rw [add_comm, this], exact tsub_add_cancel_of_le ad.le end lemma coe_nat_lt_coe {n : ℕ} : (n : ℝ≥0∞) < r ↔ ↑n < r := ennreal.coe_nat n ▸ coe_lt_coe lemma coe_lt_coe_nat {n : ℕ} : (r : ℝ≥0∞) < n ↔ r < n := ennreal.coe_nat n ▸ coe_lt_coe @[simp, norm_cast] lemma coe_nat_lt_coe_nat {m n : ℕ} : (m : ℝ≥0∞) < n ↔ m < n := ennreal.coe_nat n ▸ coe_nat_lt_coe.trans nat.cast_lt lemma coe_nat_mono : strict_mono (coe : ℕ → ℝ≥0∞) := λ _ _, coe_nat_lt_coe_nat.2 @[simp, norm_cast] lemma coe_nat_le_coe_nat {m n : ℕ} : (m : ℝ≥0∞) ≤ n ↔ m ≤ n := coe_nat_mono.le_iff_le instance : char_zero ℝ≥0∞ := ⟨coe_nat_mono.injective⟩ protected lemma exists_nat_gt {r : ℝ≥0∞} (h : r ≠ ∞) : ∃n:ℕ, r < n := begin lift r to ℝ≥0 using h, rcases exists_nat_gt r with ⟨n, hn⟩, exact ⟨n, coe_lt_coe_nat.2 hn⟩, end @[simp] lemma Union_Iio_coe_nat : (⋃ n : ℕ, Iio (n : ℝ≥0∞)) = {∞}ᶜ := begin ext x, rw [mem_Union], exact ⟨λ ⟨n, hn⟩, ne_top_of_lt hn, ennreal.exists_nat_gt⟩ end @[simp] lemma Union_Iic_coe_nat : (⋃ n : ℕ, Iic (n : ℝ≥0∞)) = {∞}ᶜ := subset.antisymm (Union_subset $ λ n x hx, ne_top_of_le_ne_top (nat_ne_top n) hx) $ Union_Iio_coe_nat ▸ Union_mono (λ n, Iio_subset_Iic_self) @[simp] lemma Union_Ioc_coe_nat : (⋃ n : ℕ, Ioc a n) = Ioi a \ {∞} := by simp only [← Ioi_inter_Iic, ← inter_Union, Union_Iic_coe_nat, diff_eq] @[simp] lemma Union_Ioo_coe_nat : (⋃ n : ℕ, Ioo a n) = Ioi a \ {∞} := by simp only [← Ioi_inter_Iio, ← inter_Union, Union_Iio_coe_nat, diff_eq] @[simp] lemma Union_Icc_coe_nat : (⋃ n : ℕ, Icc a n) = Ici a \ {∞} := by simp only [← Ici_inter_Iic, ← inter_Union, Union_Iic_coe_nat, diff_eq] @[simp] lemma Union_Ico_coe_nat : (⋃ n : ℕ, Ico a n) = Ici a \ {∞} := by simp only [← Ici_inter_Iio, ← inter_Union, Union_Iio_coe_nat, diff_eq] @[simp] lemma Inter_Ici_coe_nat : (⋂ n : ℕ, Ici (n : ℝ≥0∞)) = {∞} := by simp only [← compl_Iio, ← compl_Union, Union_Iio_coe_nat, compl_compl] @[simp] lemma Inter_Ioi_coe_nat : (⋂ n : ℕ, Ioi (n : ℝ≥0∞)) = {∞} := by simp only [← compl_Iic, ← compl_Union, Union_Iic_coe_nat, compl_compl] lemma add_lt_add (ac : a < c) (bd : b < d) : a + b < c + d := begin lift a to ℝ≥0 using ne_top_of_lt ac, lift b to ℝ≥0 using ne_top_of_lt bd, cases c, { simp }, cases d, { simp }, simp only [← coe_add, some_eq_coe, coe_lt_coe] at *, exact add_lt_add ac bd end @[norm_cast] lemma coe_min : ((min r p:ℝ≥0):ℝ≥0∞) = min r p := coe_mono.map_min @[norm_cast] lemma coe_max : ((max r p:ℝ≥0):ℝ≥0∞) = max r p := coe_mono.map_max lemma le_of_top_imp_top_of_to_nnreal_le {a b : ℝ≥0∞} (h : a = ⊤ → b = ⊤) (h_nnreal : a ≠ ⊤ → b ≠ ⊤ → a.to_nnreal ≤ b.to_nnreal) : a ≤ b := begin by_cases ha : a = ⊤, { rw h ha, exact le_top, }, by_cases hb : b = ⊤, { rw hb, exact le_top, }, rw [←coe_to_nnreal hb, ←coe_to_nnreal ha, coe_le_coe], exact h_nnreal ha hb, end end order section complete_lattice lemma coe_Sup {s : set ℝ≥0} : bdd_above s → (↑(Sup s) : ℝ≥0∞) = (⨆a∈s, ↑a) := with_top.coe_Sup lemma coe_Inf {s : set ℝ≥0} : s.nonempty → (↑(Inf s) : ℝ≥0∞) = (⨅a∈s, ↑a) := with_top.coe_Inf @[simp] lemma top_mem_upper_bounds {s : set ℝ≥0∞} : ∞ ∈ upper_bounds s := assume x hx, le_top lemma coe_mem_upper_bounds {s : set ℝ≥0} : ↑r ∈ upper_bounds ((coe : ℝ≥0 → ℝ≥0∞) '' s) ↔ r ∈ upper_bounds s := by simp [upper_bounds, ball_image_iff, -mem_image, *] {contextual := tt} end complete_lattice section mul @[mono] lemma mul_le_mul : a ≤ b → c ≤ d → a * c ≤ b * d := mul_le_mul' @[mono] lemma mul_lt_mul (ac : a < c) (bd : b < d) : a * b < c * d := begin rcases lt_iff_exists_nnreal_btwn.1 ac with ⟨a', aa', a'c⟩, lift a to ℝ≥0 using ne_top_of_lt aa', rcases lt_iff_exists_nnreal_btwn.1 bd with ⟨b', bb', b'd⟩, lift b to ℝ≥0 using ne_top_of_lt bb', norm_cast at *, calc ↑(a * b) < ↑(a' * b') : coe_lt_coe.2 (mul_lt_mul' aa'.le bb' (zero_le _) ((zero_le a).trans_lt aa')) ... = ↑a' * ↑b' : coe_mul ... ≤ c * d : mul_le_mul a'c.le b'd.le end lemma mul_left_mono : monotone ((*) a) := λ b c, mul_le_mul le_rfl lemma mul_right_mono : monotone (λ x, x * a) := λ b c h, mul_le_mul h le_rfl lemma pow_strict_mono {n : ℕ} (hn : n ≠ 0) : strict_mono (λ (x : ℝ≥0∞), x^n) := begin assume x y hxy, obtain ⟨n, rfl⟩ := nat.exists_eq_succ_of_ne_zero hn, induction n with n IH, { simp only [hxy, pow_one] }, { simp only [pow_succ _ n.succ, mul_lt_mul hxy (IH (nat.succ_pos _).ne')] } end lemma max_mul : max a b * c = max (a * c) (b * c) := mul_right_mono.map_max lemma mul_max : a * max b c = max (a * b) (a * c) := mul_left_mono.map_max lemma mul_eq_mul_left : a ≠ 0 → a ≠ ∞ → (a * b = a * c ↔ b = c) := begin cases a; cases b; cases c; simp [none_eq_top, some_eq_coe, mul_top, top_mul, -coe_mul, coe_mul.symm, nnreal.mul_eq_mul_left] {contextual := tt}, end lemma mul_eq_mul_right : c ≠ 0 → c ≠ ∞ → (a * c = b * c ↔ a = b) := mul_comm c a ▸ mul_comm c b ▸ mul_eq_mul_left lemma mul_le_mul_left : a ≠ 0 → a ≠ ∞ → (a * b ≤ a * c ↔ b ≤ c) := begin cases a; cases b; cases c; simp [none_eq_top, some_eq_coe, mul_top, top_mul, -coe_mul, coe_mul.symm] {contextual := tt}, assume h, exact mul_le_mul_left (pos_iff_ne_zero.2 h) end lemma mul_le_mul_right : c ≠ 0 → c ≠ ∞ → (a * c ≤ b * c ↔ a ≤ b) := mul_comm c a ▸ mul_comm c b ▸ mul_le_mul_left lemma mul_lt_mul_left : a ≠ 0 → a ≠ ∞ → (a * b < a * c ↔ b < c) := λ h0 ht, by simp only [mul_le_mul_left h0 ht, lt_iff_le_not_le] lemma mul_lt_mul_right : c ≠ 0 → c ≠ ∞ → (a * c < b * c ↔ a < b) := mul_comm c a ▸ mul_comm c b ▸ mul_lt_mul_left end mul section cancel /-- An element `a` is `add_le_cancellable` if `a + b ≤ a + c` implies `b ≤ c` for all `b` and `c`. This is true in `ℝ≥0∞` for all elements except `∞`. -/ lemma add_le_cancellable_iff_ne {a : ℝ≥0∞} : add_le_cancellable a ↔ a ≠ ∞ := begin split, { rintro h rfl, refine ennreal.zero_lt_one.not_le (h _), simp, }, { rintro h b c hbc, apply ennreal.le_of_add_le_add_left h hbc } end /-- This lemma has an abbreviated name because it is used frequently. -/ lemma cancel_of_ne {a : ℝ≥0∞} (h : a ≠ ∞) : add_le_cancellable a := add_le_cancellable_iff_ne.mpr h /-- This lemma has an abbreviated name because it is used frequently. -/ lemma cancel_of_lt {a : ℝ≥0∞} (h : a < ∞) : add_le_cancellable a := cancel_of_ne h.ne /-- This lemma has an abbreviated name because it is used frequently. -/ lemma cancel_of_lt' {a b : ℝ≥0∞} (h : a < b) : add_le_cancellable a := cancel_of_ne h.ne_top /-- This lemma has an abbreviated name because it is used frequently. -/ lemma cancel_coe {a : ℝ≥0} : add_le_cancellable (a : ℝ≥0∞) := cancel_of_ne coe_ne_top lemma add_right_inj (h : a ≠ ∞) : a + b = a + c ↔ b = c := (cancel_of_ne h).inj lemma add_left_inj (h : a ≠ ∞) : b + a = c + a ↔ b = c := (cancel_of_ne h).inj_left end cancel section sub lemma sub_eq_Inf {a b : ℝ≥0∞} : a - b = Inf {d | a ≤ d + b} := le_antisymm (le_Inf $ λ c, tsub_le_iff_right.mpr) $ Inf_le le_tsub_add /-- This is a special case of `with_top.coe_sub` in the `ennreal` namespace -/ lemma coe_sub : (↑(r - p) : ℝ≥0∞) = ↑r - ↑p := with_top.coe_sub /-- This is a special case of `with_top.top_sub_coe` in the `ennreal` namespace -/ lemma top_sub_coe : ∞ - ↑r = ∞ := with_top.top_sub_coe /-- This is a special case of `with_top.sub_top` in the `ennreal` namespace -/ lemma sub_top : a - ∞ = 0 := with_top.sub_top lemma sub_eq_top_iff : a - b = ∞ ↔ a = ∞ ∧ b ≠ ∞ := by { cases a; cases b; simp [← with_top.coe_sub] } lemma sub_ne_top (ha : a ≠ ∞) : a - b ≠ ∞ := mt sub_eq_top_iff.mp $ mt and.left ha protected lemma sub_eq_of_eq_add (hb : b ≠ ∞) : a = c + b → a - b = c := (cancel_of_ne hb).tsub_eq_of_eq_add protected lemma eq_sub_of_add_eq (hc : c ≠ ∞) : a + c = b → a = b - c := (cancel_of_ne hc).eq_tsub_of_add_eq protected lemma sub_eq_of_eq_add_rev (hb : b ≠ ∞) : a = b + c → a - b = c := (cancel_of_ne hb).tsub_eq_of_eq_add_rev lemma sub_eq_of_add_eq (hb : b ≠ ∞) (hc : a + b = c) : c - b = a := ennreal.sub_eq_of_eq_add hb hc.symm @[simp] protected lemma add_sub_cancel_left (ha : a ≠ ∞) : a + b - a = b := (cancel_of_ne ha).add_tsub_cancel_left @[simp] protected lemma add_sub_cancel_right (hb : b ≠ ∞) : a + b - b = a := (cancel_of_ne hb).add_tsub_cancel_right protected lemma lt_add_of_sub_lt_left (h : a ≠ ∞ ∨ b ≠ ∞) : a - b < c → a < b + c := begin obtain rfl | hb := eq_or_ne b ∞, { rw [top_add, lt_top_iff_ne_top], exact λ _, h.resolve_right (not_not.2 rfl) }, { exact (cancel_of_ne hb).lt_add_of_tsub_lt_left } end protected lemma lt_add_of_sub_lt_right (h : a ≠ ∞ ∨ c ≠ ∞) : a - c < b → a < b + c := add_comm c b ▸ ennreal.lt_add_of_sub_lt_left h lemma le_sub_of_add_le_left (ha : a ≠ ∞) : a + b ≤ c → b ≤ c - a := (cancel_of_ne ha).le_tsub_of_add_le_left lemma le_sub_of_add_le_right (hb : b ≠ ∞) : a + b ≤ c → a ≤ c - b := (cancel_of_ne hb).le_tsub_of_add_le_right protected lemma sub_lt_of_lt_add (hac : c ≤ a) (h : a < b + c) : a - c < b := ((cancel_of_lt' $ hac.trans_lt h).tsub_lt_iff_right hac).mpr h protected lemma sub_lt_iff_lt_right (hb : b ≠ ∞) (hab : b ≤ a) : a - b < c ↔ a < c + b := (cancel_of_ne hb).tsub_lt_iff_right hab protected lemma sub_lt_self (ha : a ≠ ∞) (ha₀ : a ≠ 0) (hb : b ≠ 0) : a - b < a := (cancel_of_ne ha).tsub_lt_self (pos_iff_ne_zero.2 ha₀) (pos_iff_ne_zero.2 hb) protected lemma sub_lt_self_iff (ha : a ≠ ∞) : a - b < a ↔ 0 < a ∧ 0 < b := (cancel_of_ne ha).tsub_lt_self_iff lemma sub_lt_of_sub_lt (h₂ : c ≤ a) (h₃ : a ≠ ∞ ∨ b ≠ ∞) (h₁ : a - b < c) : a - c < b := ennreal.sub_lt_of_lt_add h₂ (add_comm c b ▸ ennreal.lt_add_of_sub_lt_right h₃ h₁) lemma sub_sub_cancel (h : a ≠ ∞) (h2 : b ≤ a) : a - (a - b) = b := (cancel_of_ne $ sub_ne_top h).tsub_tsub_cancel_of_le h2 lemma sub_right_inj {a b c : ℝ≥0∞} (ha : a ≠ ∞) (hb : b ≤ a) (hc : c ≤ a) : a - b = a - c ↔ b = c := (cancel_of_ne ha).tsub_right_inj (cancel_of_ne $ ne_top_of_le_ne_top ha hb) (cancel_of_ne $ ne_top_of_le_ne_top ha hc) hb hc lemma sub_mul (h : 0 < b → b < a → c ≠ ∞) : (a - b) * c = a * c - b * c := begin cases le_or_lt a b with hab hab, { simp [hab, mul_right_mono hab] }, rcases eq_or_lt_of_le (zero_le b) with rfl|hb, { simp }, exact (cancel_of_ne $ mul_ne_top hab.ne_top (h hb hab)).tsub_mul end lemma mul_sub (h : 0 < c → c < b → a ≠ ∞) : a * (b - c) = a * b - a * c := by { simp only [mul_comm a], exact sub_mul h } end sub section sum open finset /-- A product of finite numbers is still finite -/ lemma prod_lt_top {s : finset α} {f : α → ℝ≥0∞} (h : ∀ a ∈ s, f a ≠ ∞) : (∏ a in s, f a) < ∞ := with_top.prod_lt_top h /-- A sum of finite numbers is still finite -/ lemma sum_lt_top {s : finset α} {f : α → ℝ≥0∞} (h : ∀ a ∈ s, f a ≠ ∞) : ∑ a in s, f a < ∞ := with_top.sum_lt_top h /-- A sum of finite numbers is still finite -/ lemma sum_lt_top_iff {s : finset α} {f : α → ℝ≥0∞} : ∑ a in s, f a < ∞ ↔ (∀ a ∈ s, f a < ∞) := with_top.sum_lt_top_iff /-- A sum of numbers is infinite iff one of them is infinite -/ lemma sum_eq_top_iff {s : finset α} {f : α → ℝ≥0∞} : (∑ x in s, f x) = ∞ ↔ (∃ a ∈ s, f a = ∞) := with_top.sum_eq_top_iff lemma lt_top_of_sum_ne_top {s : finset α} {f : α → ℝ≥0∞} (h : (∑ x in s, f x) ≠ ∞) {a : α} (ha : a ∈ s) : f a < ∞ := sum_lt_top_iff.1 h.lt_top a ha /-- seeing `ℝ≥0∞` as `ℝ≥0` does not change their sum, unless one of the `ℝ≥0∞` is infinity -/ lemma to_nnreal_sum {s : finset α} {f : α → ℝ≥0∞} (hf : ∀a∈s, f a ≠ ∞) : ennreal.to_nnreal (∑ a in s, f a) = ∑ a in s, ennreal.to_nnreal (f a) := begin rw [← coe_eq_coe, coe_to_nnreal, coe_finset_sum, sum_congr rfl], { intros x hx, exact (coe_to_nnreal (hf x hx)).symm }, { exact (sum_lt_top hf).ne } end /-- seeing `ℝ≥0∞` as `real` does not change their sum, unless one of the `ℝ≥0∞` is infinity -/ lemma to_real_sum {s : finset α} {f : α → ℝ≥0∞} (hf : ∀ a ∈ s, f a ≠ ∞) : ennreal.to_real (∑ a in s, f a) = ∑ a in s, ennreal.to_real (f a) := by { rw [ennreal.to_real, to_nnreal_sum hf, nnreal.coe_sum], refl } lemma of_real_sum_of_nonneg {s : finset α} {f : α → ℝ} (hf : ∀ i, i ∈ s → 0 ≤ f i) : ennreal.of_real (∑ i in s, f i) = ∑ i in s, ennreal.of_real (f i) := begin simp_rw [ennreal.of_real, ←coe_finset_sum, coe_eq_coe], exact real.to_nnreal_sum_of_nonneg hf, end theorem sum_lt_sum_of_nonempty {s : finset α} (hs : s.nonempty) {f g : α → ℝ≥0∞} (Hlt : ∀ i ∈ s, f i < g i) : ∑ i in s, f i < ∑ i in s, g i := begin induction hs using finset.nonempty.cons_induction with a a s as hs IH, { simp [Hlt _ (finset.mem_singleton_self _)] }, { simp only [as, finset.sum_cons, not_false_iff], exact ennreal.add_lt_add (Hlt _ (finset.mem_cons_self _ _)) (IH (λ i hi, Hlt _ (finset.mem_cons.2 $ or.inr hi))) } end theorem exists_le_of_sum_le {s : finset α} (hs : s.nonempty) {f g : α → ℝ≥0∞} (Hle : ∑ i in s, f i ≤ ∑ i in s, g i) : ∃ i ∈ s, f i ≤ g i := begin contrapose! Hle, apply ennreal.sum_lt_sum_of_nonempty hs Hle, end end sum section interval variables {x y z : ℝ≥0∞} {ε ε₁ ε₂ : ℝ≥0∞} {s : set ℝ≥0∞} protected lemma Ico_eq_Iio : (Ico 0 y) = (Iio y) := Ico_bot lemma mem_Iio_self_add : x ≠ ∞ → ε ≠ 0 → x ∈ Iio (x + ε) := assume xt ε0, lt_add_right xt ε0 lemma mem_Ioo_self_sub_add : x ≠ ∞ → x ≠ 0 → ε₁ ≠ 0 → ε₂ ≠ 0 → x ∈ Ioo (x - ε₁) (x + ε₂) := assume xt x0 ε0 ε0', ⟨ennreal.sub_lt_self xt x0 ε0, lt_add_right xt ε0'⟩ end interval section bit @[mono] lemma bit0_strict_mono : strict_mono (bit0 : ℝ≥0∞ → ℝ≥0∞) := λ a b h, add_lt_add h h lemma bit0_injective : function.injective (bit0 : ℝ≥0∞ → ℝ≥0∞) := bit0_strict_mono.injective @[simp] lemma bit0_lt_bit0 : bit0 a < bit0 b ↔ a < b := bit0_strict_mono.lt_iff_lt @[simp, mono] lemma bit0_le_bit0 : bit0 a ≤ bit0 b ↔ a ≤ b := bit0_strict_mono.le_iff_le @[simp] lemma bit0_inj : bit0 a = bit0 b ↔ a = b := bit0_injective.eq_iff @[simp] lemma bit0_eq_zero_iff : bit0 a = 0 ↔ a = 0 := bit0_injective.eq_iff' bit0_zero @[simp] lemma bit0_top : bit0 ∞ = ∞ := add_top @[simp] lemma bit0_eq_top_iff : bit0 a = ∞ ↔ a = ∞ := bit0_injective.eq_iff' bit0_top @[mono] lemma bit1_strict_mono : strict_mono (bit1 : ℝ≥0∞ → ℝ≥0∞) := λ a b h, ennreal.add_lt_add_right one_ne_top (bit0_strict_mono h) lemma bit1_injective : function.injective (bit1 : ℝ≥0∞ → ℝ≥0∞) := bit1_strict_mono.injective @[simp] lemma bit1_lt_bit1 : bit1 a < bit1 b ↔ a < b := bit1_strict_mono.lt_iff_lt @[simp, mono] lemma bit1_le_bit1 : bit1 a ≤ bit1 b ↔ a ≤ b := bit1_strict_mono.le_iff_le @[simp] lemma bit1_inj : bit1 a = bit1 b ↔ a = b := bit1_injective.eq_iff @[simp] lemma bit1_ne_zero : bit1 a ≠ 0 := by simp [bit1] @[simp] lemma bit1_top : bit1 ∞ = ∞ := by rw [bit1, bit0_top, top_add] @[simp] lemma bit1_eq_top_iff : bit1 a = ∞ ↔ a = ∞ := bit1_injective.eq_iff' bit1_top @[simp] lemma bit1_eq_one_iff : bit1 a = 1 ↔ a = 0 := bit1_injective.eq_iff' bit1_zero end bit section inv noncomputable theory instance : has_inv ℝ≥0∞ := ⟨λa, Inf {b | 1 ≤ a * b}⟩ instance : div_inv_monoid ℝ≥0∞ := { inv := has_inv.inv, .. (infer_instance : monoid ℝ≥0∞) } lemma div_eq_inv_mul : a / b = b⁻¹ * a := by rw [div_eq_mul_inv, mul_comm] @[simp] lemma inv_zero : (0 : ℝ≥0∞)⁻¹ = ∞ := show Inf {b : ℝ≥0∞ | 1 ≤ 0 * b} = ∞, by simp; refl @[simp] lemma inv_top : ∞⁻¹ = 0 := bot_unique $ le_of_forall_le_of_dense $ λ a (h : a > 0), Inf_le $ by simp [*, ne_of_gt h, top_mul] lemma coe_inv_le : (↑r⁻¹ : ℝ≥0∞) ≤ (↑r)⁻¹ := le_Inf $ assume b (hb : 1 ≤ ↑r * b), coe_le_iff.2 $ by { rintro b rfl, apply nnreal.inv_le_of_le_mul, rwa [← coe_mul, ← coe_one, coe_le_coe] at hb } @[simp, norm_cast] lemma coe_inv (hr : r ≠ 0) : (↑r⁻¹ : ℝ≥0∞) = (↑r)⁻¹ := coe_inv_le.antisymm $ Inf_le $ le_of_eq $ by rw [← coe_mul, mul_inv_cancel hr, coe_one] @[norm_cast] lemma coe_inv_two : ((2⁻¹ : ℝ≥0) : ℝ≥0∞) = 2⁻¹ := by rw [coe_inv _root_.two_ne_zero, coe_two] @[simp, norm_cast] lemma coe_div (hr : r ≠ 0) : (↑(p / r) : ℝ≥0∞) = p / r := by rw [div_eq_mul_inv, div_eq_mul_inv, coe_mul, coe_inv hr] lemma div_zero (h : a ≠ 0) : a / 0 = ∞ := by simp [div_eq_mul_inv, h] @[simp] lemma inv_one : (1 : ℝ≥0∞)⁻¹ = 1 := by simpa only [coe_inv one_ne_zero, coe_one] using coe_eq_coe.2 inv_one @[simp] lemma div_one {a : ℝ≥0∞} : a / 1 = a := by rw [div_eq_mul_inv, inv_one, mul_one] protected lemma inv_pow {n : ℕ} : (a^n)⁻¹ = (a⁻¹)^n := begin cases n, { simp only [pow_zero, inv_one] }, induction a using with_top.rec_top_coe, { simp [top_pow n.succ_pos] }, rcases eq_or_ne a 0 with rfl|ha, { simp [top_pow, zero_pow, n.succ_pos] }, rw [← coe_inv ha, ← coe_pow, ← coe_inv (pow_ne_zero _ ha), ← inv_pow, coe_pow] end lemma mul_inv_cancel (h0 : a ≠ 0) (ht : a ≠ ∞) : a * a⁻¹ = 1 := begin lift a to ℝ≥0 using ht, norm_cast at *, exact mul_inv_cancel h0 end lemma inv_mul_cancel (h0 : a ≠ 0) (ht : a ≠ ∞) : a⁻¹ * a = 1 := mul_comm a a⁻¹ ▸ mul_inv_cancel h0 ht lemma div_mul_cancel (h0 : a ≠ 0) (hI : a ≠ ∞) : (b / a) * a = b := by rw [div_eq_mul_inv, mul_assoc, inv_mul_cancel h0 hI, mul_one] lemma mul_div_cancel' (h0 : a ≠ 0) (hI : a ≠ ∞) : a * (b / a) = b := by rw [mul_comm, div_mul_cancel h0 hI] instance : has_involutive_inv ℝ≥0∞ := { inv := has_inv.inv, inv_inv := λ a, by by_cases a = 0; cases a; simp [*, none_eq_top, some_eq_coe, -coe_inv, (coe_inv _).symm] at * } @[simp] lemma inv_eq_top : a⁻¹ = ∞ ↔ a = 0 := inv_zero ▸ inv_inj lemma inv_ne_top : a⁻¹ ≠ ∞ ↔ a ≠ 0 := by simp @[simp] lemma inv_lt_top {x : ℝ≥0∞} : x⁻¹ < ∞ ↔ 0 < x := by { simp only [lt_top_iff_ne_top, inv_ne_top, pos_iff_ne_zero] } lemma div_lt_top {x y : ℝ≥0∞} (h1 : x ≠ ∞) (h2 : y ≠ 0) : x / y < ∞ := mul_lt_top h1 (inv_ne_top.mpr h2) @[simp] lemma inv_eq_zero : a⁻¹ = 0 ↔ a = ∞ := inv_top ▸ inv_inj lemma inv_ne_zero : a⁻¹ ≠ 0 ↔ a ≠ ∞ := by simp lemma mul_inv {a b : ℝ≥0∞} (ha : a ≠ 0 ∨ b ≠ ∞) (hb : a ≠ ∞ ∨ b ≠ 0) : (a * b)⁻¹ = a⁻¹ * b⁻¹ := begin induction b using with_top.rec_top_coe, { replace ha : a ≠ 0 := ha.neg_resolve_right rfl, simp [ha], }, induction a using with_top.rec_top_coe, { replace hb : b ≠ 0 := coe_ne_zero.1 (hb.neg_resolve_left rfl), simp [hb] }, by_cases h'a : a = 0, { simp only [h'a, with_top.top_mul, ennreal.inv_zero, ennreal.coe_ne_top, zero_mul, ne.def, not_false_iff, ennreal.coe_zero, ennreal.inv_eq_zero] }, by_cases h'b : b = 0, { simp only [h'b, ennreal.inv_zero, ennreal.coe_ne_top, with_top.mul_top, ne.def, not_false_iff, mul_zero, ennreal.coe_zero, ennreal.inv_eq_zero] }, rw [← ennreal.coe_mul, ← ennreal.coe_inv, ← ennreal.coe_inv h'a, ← ennreal.coe_inv h'b, ← ennreal.coe_mul, mul_inv_rev, mul_comm], simp [h'a, h'b], end @[simp] lemma inv_pos : 0 < a⁻¹ ↔ a ≠ ∞ := pos_iff_ne_zero.trans inv_ne_zero lemma inv_strict_anti : strict_anti (has_inv.inv : ℝ≥0∞ → ℝ≥0∞) := begin intros a b h, lift a to ℝ≥0 using h.ne_top, induction b using with_top.rec_top_coe, { simp }, rw [coe_lt_coe] at h, rcases eq_or_ne a 0 with rfl|ha, { simp [h] }, rw [← coe_inv h.ne_bot, ← coe_inv ha, coe_lt_coe], exact nnreal.inv_lt_inv ha h end @[simp] lemma inv_lt_inv : a⁻¹ < b⁻¹ ↔ b < a := inv_strict_anti.lt_iff_lt lemma inv_lt_iff_inv_lt : a⁻¹ < b ↔ b⁻¹ < a := by simpa only [inv_inv] using @inv_lt_inv a b⁻¹ lemma lt_inv_iff_lt_inv : a < b⁻¹ ↔ b < a⁻¹ := by simpa only [inv_inv] using @inv_lt_inv a⁻¹ b @[simp, priority 1100] -- higher than le_inv_iff_mul_le lemma inv_le_inv : a⁻¹ ≤ b⁻¹ ↔ b ≤ a := inv_strict_anti.le_iff_le lemma inv_le_iff_inv_le : a⁻¹ ≤ b ↔ b⁻¹ ≤ a := by simpa only [inv_inv] using @inv_le_inv a b⁻¹ lemma le_inv_iff_le_inv : a ≤ b⁻¹ ↔ b ≤ a⁻¹ := by simpa only [inv_inv] using @inv_le_inv a⁻¹ b @[simp] lemma inv_le_one : a⁻¹ ≤ 1 ↔ 1 ≤ a := inv_le_iff_inv_le.trans $ by rw inv_one lemma one_le_inv : 1 ≤ a⁻¹ ↔ a ≤ 1 := le_inv_iff_le_inv.trans $ by rw inv_one @[simp] lemma inv_lt_one : a⁻¹ < 1 ↔ 1 < a := inv_lt_iff_inv_lt.trans $ by rw [inv_one] /-- The inverse map `λ x, x⁻¹` is an order isomorphism between `ℝ≥0∞` and its `order_dual` -/ @[simps apply] def _root_.order_iso.inv_ennreal : ℝ≥0∞ ≃o ℝ≥0∞ᵒᵈ := { map_rel_iff' := λ a b, ennreal.inv_le_inv, to_equiv := (equiv.inv ℝ≥0∞).trans order_dual.to_dual } @[simp] lemma _root_.order_iso.inv_ennreal_symm_apply : order_iso.inv_ennreal.symm a = (order_dual.of_dual a)⁻¹ := rfl lemma pow_le_pow_of_le_one {n m : ℕ} (ha : a ≤ 1) (h : n ≤ m) : a ^ m ≤ a ^ n := begin rw [←inv_inv a, ← ennreal.inv_pow, ← @ennreal.inv_pow a⁻¹, inv_le_inv], exact pow_le_pow (one_le_inv.2 ha) h end @[simp] lemma div_top : a / ∞ = 0 := by rw [div_eq_mul_inv, inv_top, mul_zero] @[simp] lemma top_div_coe : ∞ / p = ∞ := by simp [div_eq_mul_inv, top_mul] lemma top_div_of_ne_top (h : a ≠ ∞) : ∞ / a = ∞ := by { lift a to ℝ≥0 using h, exact top_div_coe } lemma top_div_of_lt_top (h : a < ∞) : ∞ / a = ∞ := top_div_of_ne_top h.ne lemma top_div : ∞ / a = if a = ∞ then 0 else ∞ := by by_cases a = ∞; simp [top_div_of_ne_top, *] @[simp] lemma zero_div : 0 / a = 0 := zero_mul a⁻¹ lemma div_eq_top : a / b = ∞ ↔ (a ≠ 0 ∧ b = 0) ∨ (a = ∞ ∧ b ≠ ∞) := by simp [div_eq_mul_inv, ennreal.mul_eq_top] lemma le_div_iff_mul_le (h0 : b ≠ 0 ∨ c ≠ 0) (ht : b ≠ ∞ ∨ c ≠ ∞) : a ≤ c / b ↔ a * b ≤ c := begin induction b using with_top.rec_top_coe, { lift c to ℝ≥0 using ht.neg_resolve_left rfl, rw [div_top, nonpos_iff_eq_zero, mul_top], rcases eq_or_ne a 0 with rfl|ha; simp * }, rcases eq_or_ne b 0 with (rfl | hb), { have hc : c ≠ 0, from h0.neg_resolve_left rfl, simp [div_zero hc] }, { rw [← coe_ne_zero] at hb, rw [← ennreal.mul_le_mul_right hb coe_ne_top, div_mul_cancel hb coe_ne_top] }, end lemma div_le_iff_le_mul (hb0 : b ≠ 0 ∨ c ≠ ∞) (hbt : b ≠ ∞ ∨ c ≠ 0) : a / b ≤ c ↔ a ≤ c * b := begin suffices : a * b⁻¹ ≤ c ↔ a ≤ c / b⁻¹, by simpa [div_eq_mul_inv], refine (le_div_iff_mul_le _ _).symm; simpa end lemma lt_div_iff_mul_lt (hb0 : b ≠ 0 ∨ c ≠ ∞) (hbt : b ≠ ∞ ∨ c ≠ 0) : c < a / b ↔ c * b < a := lt_iff_lt_of_le_iff_le (div_le_iff_le_mul hb0 hbt) lemma div_le_of_le_mul (h : a ≤ b * c) : a / c ≤ b := begin by_cases h0 : c = 0, { have : a = 0, by simpa [h0] using h, simp [*] }, by_cases hinf : c = ∞, by simp [hinf], exact (div_le_iff_le_mul (or.inl h0) (or.inl hinf)).2 h end lemma div_le_of_le_mul' (h : a ≤ b * c) : a / b ≤ c := div_le_of_le_mul $ mul_comm b c ▸ h lemma mul_le_of_le_div (h : a ≤ b / c) : a * c ≤ b := begin rw [← inv_inv c], exact div_le_of_le_mul h, end lemma mul_le_of_le_div' (h : a ≤ b / c) : c * a ≤ b := mul_comm a c ▸ mul_le_of_le_div h protected lemma div_lt_iff (h0 : b ≠ 0 ∨ c ≠ 0) (ht : b ≠ ∞ ∨ c ≠ ∞) : c / b < a ↔ c < a * b := lt_iff_lt_of_le_iff_le $ le_div_iff_mul_le h0 ht lemma mul_lt_of_lt_div (h : a < b / c) : a * c < b := by { contrapose! h, exact ennreal.div_le_of_le_mul h } lemma mul_lt_of_lt_div' (h : a < b / c) : c * a < b := mul_comm a c ▸ mul_lt_of_lt_div h lemma inv_le_iff_le_mul (h₁ : b = ∞ → a ≠ 0) (h₂ : a = ∞ → b ≠ 0) : a⁻¹ ≤ b ↔ 1 ≤ a * b := begin rw [← one_div, div_le_iff_le_mul, mul_comm], exacts [or_not_of_imp h₁, not_or_of_imp h₂] end @[simp] lemma le_inv_iff_mul_le : a ≤ b⁻¹ ↔ a * b ≤ 1 := by rw [← one_div, le_div_iff_mul_le]; { right, simp } lemma div_le_div {a b c d : ℝ≥0∞} (hab : a ≤ b) (hdc : d ≤ c) : a / c ≤ b / d := div_eq_mul_inv b d ▸ div_eq_mul_inv a c ▸ ennreal.mul_le_mul hab (ennreal.inv_le_inv.mpr hdc) lemma eq_inv_of_mul_eq_one_left (h : a * b = 1) : a = b⁻¹ := begin have hb : b ≠ ∞, { rintro rfl, simpa [left_ne_zero_of_mul_eq_one h] using h }, rw [← mul_one a, ← mul_inv_cancel (right_ne_zero_of_mul_eq_one h) hb, ← mul_assoc, h, one_mul] end lemma mul_le_iff_le_inv {a b r : ℝ≥0∞} (hr₀ : r ≠ 0) (hr₁ : r ≠ ∞) : (r * a ≤ b ↔ a ≤ r⁻¹ * b) := by rw [← @ennreal.mul_le_mul_left _ a _ hr₀ hr₁, ← mul_assoc, mul_inv_cancel hr₀ hr₁, one_mul] lemma le_of_forall_nnreal_lt {x y : ℝ≥0∞} (h : ∀ r : ℝ≥0, ↑r < x → ↑r ≤ y) : x ≤ y := begin refine le_of_forall_ge_of_dense (λ r hr, _), lift r to ℝ≥0 using ne_top_of_lt hr, exact h r hr end lemma le_of_forall_pos_nnreal_lt {x y : ℝ≥0∞} (h : ∀ r : ℝ≥0, 0 < r → ↑r < x → ↑r ≤ y) : x ≤ y := le_of_forall_nnreal_lt $ λ r hr, (zero_le r).eq_or_lt.elim (λ h, h ▸ zero_le _) (λ h0, h r h0 hr) lemma eq_top_of_forall_nnreal_le {x : ℝ≥0∞} (h : ∀ r : ℝ≥0, ↑r ≤ x) : x = ∞ := top_unique $ le_of_forall_nnreal_lt $ λ r hr, h r lemma add_div : (a + b) / c = a / c + b / c := right_distrib a b (c⁻¹) lemma div_add_div_same {a b c : ℝ≥0∞} : a / c + b / c = (a + b) / c := add_div.symm lemma div_self (h0 : a ≠ 0) (hI : a ≠ ∞) : a / a = 1 := mul_inv_cancel h0 hI lemma mul_div_le : a * (b / a) ≤ b := mul_le_of_le_div' le_rfl -- TODO: add this lemma for an `is_unit` in any `division_monoid` lemma eq_div_iff (ha : a ≠ 0) (ha' : a ≠ ∞) : b = c / a ↔ a * b = c := ⟨λ h, by rw [h, mul_div_cancel' ha ha'], λ h, by rw [← h, mul_div_assoc, mul_div_cancel' ha ha']⟩ lemma div_eq_div_iff (ha : a ≠ 0) (ha' : a ≠ ∞) (hb : b ≠ 0) (hb' : b ≠ ∞) : c / b = d / a ↔ a * c = b * d := begin rw eq_div_iff ha ha', conv_rhs { rw eq_comm }, rw [← eq_div_iff hb hb', mul_div_assoc, eq_comm], end lemma inv_two_add_inv_two : (2:ℝ≥0∞)⁻¹ + 2⁻¹ = 1 := by rw [← two_mul, ← div_eq_mul_inv, div_self two_ne_zero two_ne_top] lemma inv_three_add_inv_three : (3 : ℝ≥0∞)⁻¹ + 3⁻¹ + 3⁻¹ = 1 := begin rw [show (3 : ℝ≥0∞)⁻¹ + 3⁻¹ + 3⁻¹ = 3 * 3⁻¹, by ring, ← div_eq_mul_inv, ennreal.div_self]; simp, end @[simp] lemma add_halves (a : ℝ≥0∞) : a / 2 + a / 2 = a := by rw [div_eq_mul_inv, ← mul_add, inv_two_add_inv_two, mul_one] @[simp] lemma add_thirds (a : ℝ≥0∞) : a / 3 + a / 3 + a / 3 = a := by rw [div_eq_mul_inv, ← mul_add, ← mul_add, inv_three_add_inv_three, mul_one] @[simp] lemma div_zero_iff : a / b = 0 ↔ a = 0 ∨ b = ∞ := by simp [div_eq_mul_inv] @[simp] lemma div_pos_iff : 0 < a / b ↔ a ≠ 0 ∧ b ≠ ∞ := by simp [pos_iff_ne_zero, not_or_distrib] lemma half_pos {a : ℝ≥0∞} (h : a ≠ 0) : 0 < a / 2 := by simp [h] lemma one_half_lt_one : (2⁻¹:ℝ≥0∞) < 1 := inv_lt_one.2 $ one_lt_two lemma half_lt_self {a : ℝ≥0∞} (hz : a ≠ 0) (ht : a ≠ ∞) : a / 2 < a := begin lift a to ℝ≥0 using ht, rw coe_ne_zero at hz, rw [← coe_two, ← coe_div, coe_lt_coe], exacts [nnreal.half_lt_self hz, two_ne_zero'] end lemma half_le_self : a / 2 ≤ a := le_add_self.trans_eq (add_halves _) lemma sub_half (h : a ≠ ∞) : a - a / 2 = a / 2 := begin lift a to ℝ≥0 using h, exact sub_eq_of_add_eq (mul_ne_top coe_ne_top $ by simp) (add_halves a) end @[simp] lemma one_sub_inv_two : (1:ℝ≥0∞) - 2⁻¹ = 2⁻¹ := by simpa only [div_eq_mul_inv, one_mul] using sub_half one_ne_top /-- The birational order isomorphism between `ℝ≥0∞` and the unit interval `set.Iic (1 : ℝ≥0∞)`. -/ @[simps apply_coe] def order_iso_Iic_one_birational : ℝ≥0∞ ≃o Iic (1 : ℝ≥0∞) := begin refine strict_mono.order_iso_of_right_inverse (λ x, ⟨(x⁻¹ + 1)⁻¹, inv_le_one.2 $ le_add_self⟩) (λ x y hxy, _) (λ x, (x⁻¹ - 1)⁻¹) (λ x, subtype.ext _), { simpa only [subtype.mk_lt_mk, inv_lt_inv, ennreal.add_lt_add_iff_right one_ne_top] }, { have : (1 : ℝ≥0∞) ≤ x⁻¹, from one_le_inv.2 x.2, simp only [inv_inv, subtype.coe_mk, tsub_add_cancel_of_le this] } end @[simp] lemma order_iso_Iic_one_birational_symm_apply (x : Iic (1 : ℝ≥0∞)) : order_iso_Iic_one_birational.symm x = (x⁻¹ - 1)⁻¹ := rfl /-- Order isomorphism between an initial interval in `ℝ≥0∞` and an initial interval in `ℝ≥0`. -/ @[simps apply_coe] def order_iso_Iic_coe (a : ℝ≥0) : Iic (a : ℝ≥0∞) ≃o Iic a := order_iso.symm { to_fun := λ x, ⟨x, coe_le_coe.2 x.2⟩, inv_fun := λ x, ⟨ennreal.to_nnreal x, coe_le_coe.1 $ coe_to_nnreal_le_self.trans x.2⟩, left_inv := λ x, subtype.ext $ to_nnreal_coe, right_inv := λ x, subtype.ext $ coe_to_nnreal (ne_top_of_le_ne_top coe_ne_top x.2), map_rel_iff' := λ x y, by simp only [equiv.coe_fn_mk, subtype.mk_le_mk, coe_coe, coe_le_coe, subtype.coe_le_coe] } @[simp] lemma order_iso_Iic_coe_symm_apply_coe (a : ℝ≥0) (b : Iic a) : ((order_iso_Iic_coe a).symm b : ℝ≥0∞) = b := rfl /-- An order isomorphism between the extended nonnegative real numbers and the unit interval. -/ def order_iso_unit_interval_birational : ℝ≥0∞ ≃o Icc (0 : ℝ) 1 := order_iso_Iic_one_birational.trans $ (order_iso_Iic_coe 1).trans $ (nnreal.order_iso_Icc_zero_coe 1).symm @[simp] lemma order_iso_unit_interval_birational_apply_coe (x : ℝ≥0∞) : (order_iso_unit_interval_birational x : ℝ) = (x⁻¹ + 1)⁻¹.to_real := rfl lemma exists_inv_nat_lt {a : ℝ≥0∞} (h : a ≠ 0) : ∃n:ℕ, (n:ℝ≥0∞)⁻¹ < a := inv_inv a ▸ by simp only [inv_lt_inv, ennreal.exists_nat_gt (inv_ne_top.2 h)] lemma exists_nat_pos_mul_gt (ha : a ≠ 0) (hb : b ≠ ∞) : ∃ n > 0, b < (n : ℕ) * a := begin have : b / a ≠ ∞, from mul_ne_top hb (inv_ne_top.2 ha), refine (ennreal.exists_nat_gt this).imp (λ n hn, _), have : ↑0 < (n : ℝ≥0∞), from lt_of_le_of_lt (by simp) hn, refine ⟨coe_nat_lt_coe_nat.1 this, _⟩, rwa [← ennreal.div_lt_iff (or.inl ha) (or.inr hb)] end lemma exists_nat_mul_gt (ha : a ≠ 0) (hb : b ≠ ∞) : ∃ n : ℕ, b < n * a := (exists_nat_pos_mul_gt ha hb).imp $ λ n, Exists.snd lemma exists_nat_pos_inv_mul_lt (ha : a ≠ ∞) (hb : b ≠ 0) : ∃ n > 0, ((n : ℕ) : ℝ≥0∞)⁻¹ * a < b := begin rcases exists_nat_pos_mul_gt hb ha with ⟨n, npos, hn⟩, have : (n : ℝ≥0∞) ≠ 0 := nat.cast_ne_zero.2 npos.lt.ne', use [n, npos], rwa [← one_mul b, ← inv_mul_cancel this (nat_ne_top n), mul_assoc, mul_lt_mul_left (inv_ne_zero.2 $ nat_ne_top _) (inv_ne_top.2 this)] end lemma exists_nnreal_pos_mul_lt (ha : a ≠ ∞) (hb : b ≠ 0) : ∃ n > 0, ↑(n : ℝ≥0) * a < b := begin rcases exists_nat_pos_inv_mul_lt ha hb with ⟨n, npos : 0 < n, hn⟩, use (n : ℝ≥0)⁻¹, simp [*, npos.ne', zero_lt_one] end lemma exists_inv_two_pow_lt (ha : a ≠ 0) : ∃ n : ℕ, 2⁻¹ ^ n < a := begin rcases exists_inv_nat_lt ha with ⟨n, hn⟩, refine ⟨n, lt_trans _ hn⟩, rw [← ennreal.inv_pow, inv_lt_inv], norm_cast, exact n.lt_two_pow end @[simp, norm_cast] lemma coe_zpow (hr : r ≠ 0) (n : ℤ) : (↑(r^n) : ℝ≥0∞) = r^n := begin cases n, { simp only [int.of_nat_eq_coe, coe_pow, zpow_coe_nat] }, { have : r ^ n.succ ≠ 0 := pow_ne_zero (n+1) hr, simp only [zpow_neg_succ_of_nat, coe_inv this, coe_pow] } end lemma zpow_pos (ha : a ≠ 0) (h'a : a ≠ ∞) (n : ℤ) : 0 < a ^ n := begin cases n, { exact ennreal.pow_pos ha.bot_lt n }, { simp only [h'a, pow_eq_top_iff, zpow_neg_succ_of_nat, ne.def, not_false_iff, inv_pos, false_and] } end lemma zpow_lt_top (ha : a ≠ 0) (h'a : a ≠ ∞) (n : ℤ) : a ^ n < ∞ := begin cases n, { exact ennreal.pow_lt_top h'a.lt_top _ }, { simp only [ennreal.pow_pos ha.bot_lt (n + 1), zpow_neg_succ_of_nat, inv_lt_top] } end lemma exists_mem_Ico_zpow {x y : ℝ≥0∞} (hx : x ≠ 0) (h'x : x ≠ ∞) (hy : 1 < y) (h'y : y ≠ ⊤) : ∃ n : ℤ, x ∈ Ico (y ^ n) (y ^ (n + 1)) := begin lift x to ℝ≥0 using h'x, lift y to ℝ≥0 using h'y, have A : y ≠ 0, { simpa only [ne.def, coe_eq_zero] using (ennreal.zero_lt_one.trans hy).ne' }, obtain ⟨n, hn, h'n⟩ : ∃ n : ℤ, y ^ n ≤ x ∧ x < y ^ (n + 1), { refine nnreal.exists_mem_Ico_zpow _ (one_lt_coe_iff.1 hy), simpa only [ne.def, coe_eq_zero] using hx }, refine ⟨n, _, _⟩, { rwa [← ennreal.coe_zpow A, ennreal.coe_le_coe] }, { rwa [← ennreal.coe_zpow A, ennreal.coe_lt_coe] } end lemma exists_mem_Ioc_zpow {x y : ℝ≥0∞} (hx : x ≠ 0) (h'x : x ≠ ∞) (hy : 1 < y) (h'y : y ≠ ⊤) : ∃ n : ℤ, x ∈ Ioc (y ^ n) (y ^ (n + 1)) := begin lift x to ℝ≥0 using h'x, lift y to ℝ≥0 using h'y, have A : y ≠ 0, { simpa only [ne.def, coe_eq_zero] using (ennreal.zero_lt_one.trans hy).ne' }, obtain ⟨n, hn, h'n⟩ : ∃ n : ℤ, y ^ n < x ∧ x ≤ y ^ (n + 1), { refine nnreal.exists_mem_Ioc_zpow _ (one_lt_coe_iff.1 hy), simpa only [ne.def, coe_eq_zero] using hx }, refine ⟨n, _, _⟩, { rwa [← ennreal.coe_zpow A, ennreal.coe_lt_coe] }, { rwa [← ennreal.coe_zpow A, ennreal.coe_le_coe] } end lemma Ioo_zero_top_eq_Union_Ico_zpow {y : ℝ≥0∞} (hy : 1 < y) (h'y : y ≠ ⊤) : Ioo (0 : ℝ≥0∞) (∞ : ℝ≥0∞) = ⋃ (n : ℤ), Ico (y^n) (y^(n+1)) := begin ext x, simp only [mem_Union, mem_Ioo, mem_Ico], split, { rintros ⟨hx, h'x⟩, exact exists_mem_Ico_zpow hx.ne' h'x.ne hy h'y }, { rintros ⟨n, hn, h'n⟩, split, { apply lt_of_lt_of_le _ hn, exact ennreal.zpow_pos (ennreal.zero_lt_one.trans hy).ne' h'y _ }, { apply lt_trans h'n _, exact ennreal.zpow_lt_top (ennreal.zero_lt_one.trans hy).ne' h'y _ } } end lemma zpow_le_of_le {x : ℝ≥0∞} (hx : 1 ≤ x) {a b : ℤ} (h : a ≤ b) : x ^ a ≤ x ^ b := begin induction a with a a; induction b with b b, { simp only [int.of_nat_eq_coe, zpow_coe_nat], exact pow_le_pow hx (int.le_of_coe_nat_le_coe_nat h), }, { apply absurd h (not_le_of_gt _), exact lt_of_lt_of_le (int.neg_succ_lt_zero _) (int.of_nat_nonneg _) }, { simp only [zpow_neg_succ_of_nat, int.of_nat_eq_coe, zpow_coe_nat], refine le_trans (inv_le_one.2 _) _; exact ennreal.one_le_pow_of_one_le hx _, }, { simp only [zpow_neg_succ_of_nat, inv_le_inv], apply pow_le_pow hx, simpa only [←int.coe_nat_le_coe_nat_iff, neg_le_neg_iff, int.coe_nat_add, int.coe_nat_one, int.neg_succ_of_nat_eq] using h } end lemma monotone_zpow {x : ℝ≥0∞} (hx : 1 ≤ x) : monotone ((^) x : ℤ → ℝ≥0∞) := λ a b h, zpow_le_of_le hx h lemma zpow_add {x : ℝ≥0∞} (hx : x ≠ 0) (h'x : x ≠ ∞) (m n : ℤ) : x ^ (m + n) = x ^ m * x ^ n := begin lift x to ℝ≥0 using h'x, replace hx : x ≠ 0, by simpa only [ne.def, coe_eq_zero] using hx, simp only [← coe_zpow hx, zpow_add₀ hx, coe_mul] end end inv section real lemma to_real_add (ha : a ≠ ∞) (hb : b ≠ ∞) : (a+b).to_real = a.to_real + b.to_real := begin lift a to ℝ≥0 using ha, lift b to ℝ≥0 using hb, refl end lemma to_real_sub_of_le {a b : ℝ≥0∞} (h : b ≤ a) (ha : a ≠ ∞): (a - b).to_real = a.to_real - b.to_real := begin lift b to ℝ≥0 using ne_top_of_le_ne_top ha h, lift a to ℝ≥0 using ha, simp only [← ennreal.coe_sub, ennreal.coe_to_real, nnreal.coe_sub (ennreal.coe_le_coe.mp h)], end lemma le_to_real_sub {a b : ℝ≥0∞} (hb : b ≠ ∞) : a.to_real - b.to_real ≤ (a - b).to_real := begin lift b to ℝ≥0 using hb, induction a using with_top.rec_top_coe, { simp }, { simp only [←coe_sub, nnreal.sub_def, real.coe_to_nnreal', coe_to_real], exact le_max_left _ _ } end lemma to_real_add_le : (a+b).to_real ≤ a.to_real + b.to_real := if ha : a = ∞ then by simp only [ha, top_add, top_to_real, zero_add, to_real_nonneg] else if hb : b = ∞ then by simp only [hb, add_top, top_to_real, add_zero, to_real_nonneg] else le_of_eq (to_real_add ha hb) lemma of_real_add {p q : ℝ} (hp : 0 ≤ p) (hq : 0 ≤ q) : ennreal.of_real (p + q) = ennreal.of_real p + ennreal.of_real q := by rw [ennreal.of_real, ennreal.of_real, ennreal.of_real, ← coe_add, coe_eq_coe, real.to_nnreal_add hp hq] lemma of_real_add_le {p q : ℝ} : ennreal.of_real (p + q) ≤ ennreal.of_real p + ennreal.of_real q := coe_le_coe.2 real.to_nnreal_add_le @[simp] lemma to_real_le_to_real (ha : a ≠ ∞) (hb : b ≠ ∞) : a.to_real ≤ b.to_real ↔ a ≤ b := begin lift a to ℝ≥0 using ha, lift b to ℝ≥0 using hb, norm_cast end lemma to_real_mono (hb : b ≠ ∞) (h : a ≤ b) : a.to_real ≤ b.to_real := (to_real_le_to_real (h.trans_lt (lt_top_iff_ne_top.2 hb)).ne hb).2 h @[simp] lemma to_real_lt_to_real (ha : a ≠ ∞) (hb : b ≠ ∞) : a.to_real < b.to_real ↔ a < b := begin lift a to ℝ≥0 using ha, lift b to ℝ≥0 using hb, norm_cast end lemma to_real_strict_mono (hb : b ≠ ∞) (h : a < b) : a.to_real < b.to_real := (to_real_lt_to_real (h.trans (lt_top_iff_ne_top.2 hb)).ne hb).2 h lemma to_nnreal_mono (hb : b ≠ ∞) (h : a ≤ b) : a.to_nnreal ≤ b.to_nnreal := by simpa [←ennreal.coe_le_coe, hb, (h.trans_lt hb.lt_top).ne] @[simp] lemma to_nnreal_le_to_nnreal (ha : a ≠ ∞) (hb : b ≠ ∞) : a.to_nnreal ≤ b.to_nnreal ↔ a ≤ b := ⟨λ h, by rwa [←coe_to_nnreal ha, ←coe_to_nnreal hb, coe_le_coe], to_nnreal_mono hb⟩ lemma to_nnreal_strict_mono (hb : b ≠ ∞) (h : a < b) : a.to_nnreal < b.to_nnreal := by simpa [←ennreal.coe_lt_coe, hb, (h.trans hb.lt_top).ne] @[simp] lemma to_nnreal_lt_to_nnreal (ha : a ≠ ∞) (hb : b ≠ ∞) : a.to_nnreal < b.to_nnreal ↔ a < b := ⟨λ h, by rwa [←coe_to_nnreal ha, ←coe_to_nnreal hb, coe_lt_coe], to_nnreal_strict_mono hb⟩ lemma to_real_max (hr : a ≠ ∞) (hp : b ≠ ∞) : ennreal.to_real (max a b) = max (ennreal.to_real a) (ennreal.to_real b) := (le_total a b).elim (λ h, by simp only [h, (ennreal.to_real_le_to_real hr hp).2 h, max_eq_right]) (λ h, by simp only [h, (ennreal.to_real_le_to_real hp hr).2 h, max_eq_left]) lemma to_real_min {a b : ℝ≥0∞} (hr : a ≠ ∞) (hp : b ≠ ∞) : ennreal.to_real (min a b) = min (ennreal.to_real a) (ennreal.to_real b) := (le_total a b).elim (λ h, by simp only [h, (ennreal.to_real_le_to_real hr hp).2 h, min_eq_left]) (λ h, by simp only [h, (ennreal.to_real_le_to_real hp hr).2 h, min_eq_right]) lemma to_real_sup {a b : ℝ≥0∞} : a ≠ ∞ → b ≠ ∞ → (a ⊔ b).to_real = a.to_real ⊔ b.to_real := to_real_max lemma to_real_inf {a b : ℝ≥0∞} : a ≠ ∞ → b ≠ ∞ → (a ⊓ b).to_real = a.to_real ⊓ b.to_real := to_real_min lemma to_nnreal_pos_iff : 0 < a.to_nnreal ↔ (0 < a ∧ a < ∞) := by { induction a using with_top.rec_top_coe; simp } lemma to_nnreal_pos {a : ℝ≥0∞} (ha₀ : a ≠ 0) (ha_top : a ≠ ∞) : 0 < a.to_nnreal := to_nnreal_pos_iff.mpr ⟨bot_lt_iff_ne_bot.mpr ha₀, lt_top_iff_ne_top.mpr ha_top⟩ lemma to_real_pos_iff : 0 < a.to_real ↔ (0 < a ∧ a < ∞):= (nnreal.coe_pos).trans to_nnreal_pos_iff lemma to_real_pos {a : ℝ≥0∞} (ha₀ : a ≠ 0) (ha_top : a ≠ ∞) : 0 < a.to_real := to_real_pos_iff.mpr ⟨bot_lt_iff_ne_bot.mpr ha₀, lt_top_iff_ne_top.mpr ha_top⟩ lemma of_real_le_of_real {p q : ℝ} (h : p ≤ q) : ennreal.of_real p ≤ ennreal.of_real q := by simp [ennreal.of_real, real.to_nnreal_le_to_nnreal h] lemma of_real_le_of_le_to_real {a : ℝ} {b : ℝ≥0∞} (h : a ≤ ennreal.to_real b) : ennreal.of_real a ≤ b := (of_real_le_of_real h).trans of_real_to_real_le @[simp] lemma of_real_le_of_real_iff {p q : ℝ} (h : 0 ≤ q) : ennreal.of_real p ≤ ennreal.of_real q ↔ p ≤ q := by rw [ennreal.of_real, ennreal.of_real, coe_le_coe, real.to_nnreal_le_to_nnreal_iff h] @[simp] lemma of_real_lt_of_real_iff {p q : ℝ} (h : 0 < q) : ennreal.of_real p < ennreal.of_real q ↔ p < q := by rw [ennreal.of_real, ennreal.of_real, coe_lt_coe, real.to_nnreal_lt_to_nnreal_iff h] lemma of_real_lt_of_real_iff_of_nonneg {p q : ℝ} (hp : 0 ≤ p) : ennreal.of_real p < ennreal.of_real q ↔ p < q := by rw [ennreal.of_real, ennreal.of_real, coe_lt_coe, real.to_nnreal_lt_to_nnreal_iff_of_nonneg hp] @[simp] lemma of_real_pos {p : ℝ} : 0 < ennreal.of_real p ↔ 0 < p := by simp [ennreal.of_real] @[simp] lemma of_real_eq_zero {p : ℝ} : ennreal.of_real p = 0 ↔ p ≤ 0 := by simp [ennreal.of_real] @[simp] lemma zero_eq_of_real {p : ℝ} : 0 = ennreal.of_real p ↔ p ≤ 0 := eq_comm.trans of_real_eq_zero alias of_real_eq_zero ↔ _ of_real_of_nonpos lemma of_real_sub (p : ℝ) (hq : 0 ≤ q) : ennreal.of_real (p - q) = ennreal.of_real p - ennreal.of_real q := begin obtain h | h := le_total p q, { rw [of_real_of_nonpos (sub_nonpos_of_le h), tsub_eq_zero_of_le (of_real_le_of_real h)] }, refine ennreal.eq_sub_of_add_eq of_real_ne_top _, rw [←of_real_add (sub_nonneg_of_le h) hq, sub_add_cancel], end lemma of_real_le_iff_le_to_real {a : ℝ} {b : ℝ≥0∞} (hb : b ≠ ∞) : ennreal.of_real a ≤ b ↔ a ≤ ennreal.to_real b := begin lift b to ℝ≥0 using hb, simpa [ennreal.of_real, ennreal.to_real] using real.to_nnreal_le_iff_le_coe end lemma of_real_lt_iff_lt_to_real {a : ℝ} {b : ℝ≥0∞} (ha : 0 ≤ a) (hb : b ≠ ∞) : ennreal.of_real a < b ↔ a < ennreal.to_real b := begin lift b to ℝ≥0 using hb, simpa [ennreal.of_real, ennreal.to_real] using real.to_nnreal_lt_iff_lt_coe ha end lemma le_of_real_iff_to_real_le {a : ℝ≥0∞} {b : ℝ} (ha : a ≠ ∞) (hb : 0 ≤ b) : a ≤ ennreal.of_real b ↔ ennreal.to_real a ≤ b := begin lift a to ℝ≥0 using ha, simpa [ennreal.of_real, ennreal.to_real] using real.le_to_nnreal_iff_coe_le hb end lemma to_real_le_of_le_of_real {a : ℝ≥0∞} {b : ℝ} (hb : 0 ≤ b) (h : a ≤ ennreal.of_real b) : ennreal.to_real a ≤ b := have ha : a ≠ ∞, from ne_top_of_le_ne_top of_real_ne_top h, (le_of_real_iff_to_real_le ha hb).1 h lemma lt_of_real_iff_to_real_lt {a : ℝ≥0∞} {b : ℝ} (ha : a ≠ ∞) : a < ennreal.of_real b ↔ ennreal.to_real a < b := begin lift a to ℝ≥0 using ha, simpa [ennreal.of_real, ennreal.to_real] using real.lt_to_nnreal_iff_coe_lt end lemma of_real_mul {p q : ℝ} (hp : 0 ≤ p) : ennreal.of_real (p * q) = ennreal.of_real p * ennreal.of_real q := by simp only [ennreal.of_real, ← coe_mul, real.to_nnreal_mul hp] lemma of_real_mul' {p q : ℝ} (hq : 0 ≤ q) : ennreal.of_real (p * q) = ennreal.of_real p * ennreal.of_real q := by rw [mul_comm, of_real_mul hq, mul_comm] lemma of_real_pow {p : ℝ} (hp : 0 ≤ p) (n : ℕ) : ennreal.of_real (p ^ n) = ennreal.of_real p ^ n := by rw [of_real_eq_coe_nnreal hp, ← coe_pow, ← of_real_coe_nnreal, nnreal.coe_pow, nnreal.coe_mk] lemma of_real_inv_of_pos {x : ℝ} (hx : 0 < x) : (ennreal.of_real x)⁻¹ = ennreal.of_real x⁻¹ := by rw [ennreal.of_real, ennreal.of_real, ←@coe_inv (real.to_nnreal x) (by simp [hx]), coe_eq_coe, real.to_nnreal_inv.symm] lemma of_real_div_of_pos {x y : ℝ} (hy : 0 < y) : ennreal.of_real (x / y) = ennreal.of_real x / ennreal.of_real y := by rw [div_eq_mul_inv, div_eq_mul_inv, of_real_mul' (inv_nonneg.2 hy.le), of_real_inv_of_pos hy] @[simp] lemma to_nnreal_mul {a b : ℝ≥0∞} : (a * b).to_nnreal = a.to_nnreal * b.to_nnreal := with_top.untop'_zero_mul a b lemma to_nnreal_mul_top (a : ℝ≥0∞) : ennreal.to_nnreal (a * ∞) = 0 := by simp lemma to_nnreal_top_mul (a : ℝ≥0∞) : ennreal.to_nnreal (∞ * a) = 0 := by simp @[simp] lemma smul_to_nnreal (a : ℝ≥0) (b : ℝ≥0∞) : (a • b).to_nnreal = a * b.to_nnreal := begin change ((a : ℝ≥0∞) * b).to_nnreal = a * b.to_nnreal, simp only [ennreal.to_nnreal_mul, ennreal.to_nnreal_coe], end /-- `ennreal.to_nnreal` as a `monoid_hom`. -/ def to_nnreal_hom : ℝ≥0∞ →* ℝ≥0 := { to_fun := ennreal.to_nnreal, map_one' := to_nnreal_coe, map_mul' := λ _ _, to_nnreal_mul } @[simp] lemma to_nnreal_pow (a : ℝ≥0∞) (n : ℕ) : (a ^ n).to_nnreal = a.to_nnreal ^ n := to_nnreal_hom.map_pow a n @[simp] lemma to_nnreal_prod {ι : Type*} {s : finset ι} {f : ι → ℝ≥0∞} : (∏ i in s, f i).to_nnreal = ∏ i in s, (f i).to_nnreal := to_nnreal_hom.map_prod _ _ /-- `ennreal.to_real` as a `monoid_hom`. -/ def to_real_hom : ℝ≥0∞ →* ℝ := (nnreal.to_real_hom : ℝ≥0 →* ℝ).comp to_nnreal_hom @[simp] lemma to_real_mul : (a * b).to_real = a.to_real * b.to_real := to_real_hom.map_mul a b @[simp] lemma to_real_pow (a : ℝ≥0∞) (n : ℕ) : (a ^ n).to_real = a.to_real ^ n := to_real_hom.map_pow a n @[simp] lemma to_real_prod {ι : Type*} {s : finset ι} {f : ι → ℝ≥0∞} : (∏ i in s, f i).to_real = ∏ i in s, (f i).to_real := to_real_hom.map_prod _ _ lemma to_real_of_real_mul (c : ℝ) (a : ℝ≥0∞) (h : 0 ≤ c) : ennreal.to_real ((ennreal.of_real c) * a) = c * ennreal.to_real a := by rw [ennreal.to_real_mul, ennreal.to_real_of_real h] lemma to_real_mul_top (a : ℝ≥0∞) : ennreal.to_real (a * ∞) = 0 := by rw [to_real_mul, top_to_real, mul_zero] lemma to_real_top_mul (a : ℝ≥0∞) : ennreal.to_real (∞ * a) = 0 := by { rw mul_comm, exact to_real_mul_top _ } lemma to_real_eq_to_real (ha : a ≠ ∞) (hb : b ≠ ∞) : ennreal.to_real a = ennreal.to_real b ↔ a = b := begin lift a to ℝ≥0 using ha, lift b to ℝ≥0 using hb, simp only [coe_eq_coe, nnreal.coe_eq, coe_to_real], end lemma to_real_smul (r : ℝ≥0) (s : ℝ≥0∞) : (r • s).to_real = r • s.to_real := by { rw [ennreal.smul_def, smul_eq_mul, to_real_mul, coe_to_real], refl } protected lemma trichotomy (p : ℝ≥0∞) : p = 0 ∨ p = ∞ ∨ 0 < p.to_real := by simpa only [or_iff_not_imp_left] using to_real_pos protected lemma trichotomy₂ {p q : ℝ≥0∞} (hpq : p ≤ q) : (p = 0 ∧ q = 0) ∨ (p = 0 ∧ q = ∞) ∨ (p = 0 ∧ 0 < q.to_real) ∨ (p = ∞ ∧ q = ∞) ∨ (0 < p.to_real ∧ q = ∞) ∨ (0 < p.to_real ∧ 0 < q.to_real ∧ p.to_real ≤ q.to_real) := begin rcases eq_or_lt_of_le (bot_le : 0 ≤ p) with (rfl : 0 = p) | (hp : 0 < p), { simpa using q.trichotomy }, rcases eq_or_lt_of_le (le_top : q ≤ ∞) with rfl | hq, { simpa using p.trichotomy }, repeat { right }, have hq' : 0 < q := lt_of_lt_of_le hp hpq, have hp' : p < ∞ := lt_of_le_of_lt hpq hq, simp [ennreal.to_real_le_to_real hp'.ne hq.ne, ennreal.to_real_pos_iff, hpq, hp, hp', hq', hq], end protected lemma dichotomy (p : ℝ≥0∞) [fact (1 ≤ p)] : p = ∞ ∨ 1 ≤ p.to_real := begin have : p = ⊤ ∨ 0 < p.to_real ∧ 1 ≤ p.to_real, { simpa using ennreal.trichotomy₂ (fact.out _ : 1 ≤ p) }, exact this.imp_right (λ h, h.2) end lemma to_real_pos_iff_ne_top (p : ℝ≥0∞) [fact (1 ≤ p)] : 0 < p.to_real ↔ p ≠ ∞ := ⟨λ h hp, let this : (0 : ℝ) ≠ 0 := top_to_real ▸ (hp ▸ h.ne : 0 ≠ ∞.to_real) in this rfl, λ h, zero_lt_one.trans_le (p.dichotomy.resolve_left h)⟩ lemma to_nnreal_inv (a : ℝ≥0∞) : (a⁻¹).to_nnreal = (a.to_nnreal)⁻¹ := begin induction a using with_top.rec_top_coe, { simp }, rcases eq_or_ne a 0 with rfl|ha, { simp }, rw [← coe_inv ha, to_nnreal_coe, to_nnreal_coe] end lemma to_nnreal_div (a b : ℝ≥0∞) : (a / b).to_nnreal = a.to_nnreal / b.to_nnreal := by rw [div_eq_mul_inv, to_nnreal_mul, to_nnreal_inv, div_eq_mul_inv] lemma to_real_inv (a : ℝ≥0∞) : (a⁻¹).to_real = (a.to_real)⁻¹ := by { simp_rw ennreal.to_real, norm_cast, exact to_nnreal_inv a, } lemma to_real_div (a b : ℝ≥0∞) : (a / b).to_real = a.to_real / b.to_real := by rw [div_eq_mul_inv, to_real_mul, to_real_inv, div_eq_mul_inv] lemma of_real_prod_of_nonneg {s : finset α} {f : α → ℝ} (hf : ∀ i, i ∈ s → 0 ≤ f i) : ennreal.of_real (∏ i in s, f i) = ∏ i in s, ennreal.of_real (f i) := begin simp_rw [ennreal.of_real, ←coe_finset_prod, coe_eq_coe], exact real.to_nnreal_prod_of_nonneg hf, end @[simp] lemma to_nnreal_bit0 {x : ℝ≥0∞} : (bit0 x).to_nnreal = bit0 (x.to_nnreal) := begin induction x using with_top.rec_top_coe, { simp }, { exact to_nnreal_add coe_ne_top coe_ne_top } end @[simp] lemma to_nnreal_bit1 {x : ℝ≥0∞} (hx_top : x ≠ ∞) : (bit1 x).to_nnreal = bit1 (x.to_nnreal) := by simp [bit1, bit1, to_nnreal_add (by rwa [ne.def, bit0_eq_top_iff]) ennreal.one_ne_top] @[simp] lemma to_real_bit0 {x : ℝ≥0∞} : (bit0 x).to_real = bit0 (x.to_real) := by simp [ennreal.to_real] @[simp] lemma to_real_bit1 {x : ℝ≥0∞} (hx_top : x ≠ ∞) : (bit1 x).to_real = bit1 (x.to_real) := by simp [ennreal.to_real, hx_top] @[simp] lemma of_real_bit0 (r : ℝ) : ennreal.of_real (bit0 r) = bit0 (ennreal.of_real r) := by simp [ennreal.of_real] @[simp] lemma of_real_bit1 {r : ℝ} (hr : 0 ≤ r) : ennreal.of_real (bit1 r) = bit1 (ennreal.of_real r) := (of_real_add (by simp [hr]) zero_le_one).trans (by simp [real.to_nnreal_one, bit1]) end real section infi variables {ι : Sort*} {f g : ι → ℝ≥0∞} lemma infi_add : infi f + a = ⨅i, f i + a := le_antisymm (le_infi $ assume i, add_le_add (infi_le _ _) $ le_rfl) (tsub_le_iff_right.1 $ le_infi $ assume i, tsub_le_iff_right.2 $ infi_le _ _) lemma supr_sub : (⨆i, f i) - a = (⨆i, f i - a) := le_antisymm (tsub_le_iff_right.2 $ supr_le $ assume i, tsub_le_iff_right.1 $ le_supr _ i) (supr_le $ assume i, tsub_le_tsub (le_supr _ _) (le_refl a)) lemma sub_infi : a - (⨅i, f i) = (⨆i, a - f i) := begin refine (eq_of_forall_ge_iff $ λ c, _), rw [tsub_le_iff_right, add_comm, infi_add], simp [tsub_le_iff_right, sub_eq_add_neg, add_comm], end lemma Inf_add {s : set ℝ≥0∞} : Inf s + a = ⨅b∈s, b + a := by simp [Inf_eq_infi, infi_add] lemma add_infi {a : ℝ≥0∞} : a + infi f = ⨅b, a + f b := by rw [add_comm, infi_add]; simp [add_comm] lemma infi_add_infi (h : ∀i j, ∃k, f k + g k ≤ f i + g j) : infi f + infi g = (⨅a, f a + g a) := suffices (⨅a, f a + g a) ≤ infi f + infi g, from le_antisymm (le_infi $ assume a, add_le_add (infi_le _ _) (infi_le _ _)) this, calc (⨅a, f a + g a) ≤ (⨅ a a', f a + g a') : le_infi $ assume a, le_infi $ assume a', let ⟨k, h⟩ := h a a' in infi_le_of_le k h ... = infi f + infi g : by simp [add_infi, infi_add] lemma infi_sum {f : ι → α → ℝ≥0∞} {s : finset α} [nonempty ι] (h : ∀(t : finset α) (i j : ι), ∃k, ∀a∈t, f k a ≤ f i a ∧ f k a ≤ f j a) : (⨅i, ∑ a in s, f i a) = ∑ a in s, ⨅i, f i a := begin induction s using finset.induction_on with a s ha ih, { simp }, have : ∀ (i j : ι), ∃ (k : ι), f k a + ∑ b in s, f k b ≤ f i a + ∑ b in s, f j b, { intros i j, obtain ⟨k, hk⟩ := h (insert a s) i j, exact ⟨k, add_le_add (hk a (finset.mem_insert_self _ _)).left $ finset.sum_le_sum $ λ a ha, (hk _ $ finset.mem_insert_of_mem ha).right⟩ }, simp [ha, ih.symm, infi_add_infi this] end /-- If `x ≠ 0` and `x ≠ ∞`, then right multiplication by `x` maps infimum to infimum. See also `ennreal.infi_mul` that assumes `[nonempty ι]` but does not require `x ≠ 0`. -/ lemma infi_mul_of_ne {ι} {f : ι → ℝ≥0∞} {x : ℝ≥0∞} (h0 : x ≠ 0) (h : x ≠ ∞) : infi f * x = ⨅ i, f i * x := le_antisymm mul_right_mono.map_infi_le ((div_le_iff_le_mul (or.inl h0) $ or.inl h).mp $ le_infi $ λ i, (div_le_iff_le_mul (or.inl h0) $ or.inl h).mpr $ infi_le _ _) /-- If `x ≠ ∞`, then right multiplication by `x` maps infimum over a nonempty type to infimum. See also `ennreal.infi_mul_of_ne` that assumes `x ≠ 0` but does not require `[nonempty ι]`. -/ lemma infi_mul {ι} [nonempty ι] {f : ι → ℝ≥0∞} {x : ℝ≥0∞} (h : x ≠ ∞) : infi f * x = ⨅ i, f i * x := begin by_cases h0 : x = 0, { simp only [h0, mul_zero, infi_const] }, { exact infi_mul_of_ne h0 h } end /-- If `x ≠ ∞`, then left multiplication by `x` maps infimum over a nonempty type to infimum. See also `ennreal.mul_infi_of_ne` that assumes `x ≠ 0` but does not require `[nonempty ι]`. -/ lemma mul_infi {ι} [nonempty ι] {f : ι → ℝ≥0∞} {x : ℝ≥0∞} (h : x ≠ ∞) : x * infi f = ⨅ i, x * f i := by simpa only [mul_comm] using infi_mul h /-- If `x ≠ 0` and `x ≠ ∞`, then left multiplication by `x` maps infimum to infimum. See also `ennreal.mul_infi` that assumes `[nonempty ι]` but does not require `x ≠ 0`. -/ lemma mul_infi_of_ne {ι} {f : ι → ℝ≥0∞} {x : ℝ≥0∞} (h0 : x ≠ 0) (h : x ≠ ∞) : x * infi f = ⨅ i, x * f i := by simpa only [mul_comm] using infi_mul_of_ne h0 h /-! `supr_mul`, `mul_supr` and variants are in `topology.instances.ennreal`. -/ end infi section supr @[simp] lemma supr_eq_zero {ι : Sort*} {f : ι → ℝ≥0∞} : (⨆ i, f i) = 0 ↔ ∀ i, f i = 0 := supr_eq_bot @[simp] lemma supr_zero_eq_zero {ι : Sort*} : (⨆ i : ι, (0 : ℝ≥0∞)) = 0 := by simp lemma sup_eq_zero {a b : ℝ≥0∞} : a ⊔ b = 0 ↔ a = 0 ∧ b = 0 := sup_eq_bot_iff lemma supr_coe_nat : (⨆n:ℕ, (n : ℝ≥0∞)) = ∞ := (supr_eq_top _).2 $ assume b hb, ennreal.exists_nat_gt (lt_top_iff_ne_top.1 hb) end supr end ennreal namespace set namespace ord_connected variables {s : set ℝ} {t : set ℝ≥0} {u : set ℝ≥0∞} lemma preimage_coe_nnreal_ennreal (h : u.ord_connected) : (coe ⁻¹' u : set ℝ≥0).ord_connected := h.preimage_mono ennreal.coe_mono lemma image_coe_nnreal_ennreal (h : t.ord_connected) : (coe '' t : set ℝ≥0∞).ord_connected := begin refine ⟨ball_image_iff.2 $ λ x hx, ball_image_iff.2 $ λ y hy z hz, _⟩, rcases ennreal.le_coe_iff.1 hz.2 with ⟨z, rfl, hzy⟩, exact mem_image_of_mem _ (h.out hx hy ⟨ennreal.coe_le_coe.1 hz.1, ennreal.coe_le_coe.1 hz.2⟩) end lemma preimage_ennreal_of_real (h : u.ord_connected) : (ennreal.of_real ⁻¹' u).ord_connected := h.preimage_coe_nnreal_ennreal.preimage_real_to_nnreal lemma image_ennreal_of_real (h : s.ord_connected) : (ennreal.of_real '' s).ord_connected := by simpa only [image_image] using h.image_real_to_nnreal.image_coe_nnreal_ennreal end ord_connected end set
0b32b926514bf764fec4c70f5850bc304fcb90e8
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/category_theory/monoidal/Mon_.lean
e8a71d0f6983991cb4dd573225cbf781a623f721
[ "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
9,389
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 category_theory.monoidal.discrete import category_theory.limits.shapes.terminal import algebra.punit_instances /-! # The category of monoids in a monoidal category. -/ universes v₁ v₂ u₁ u₂ u open category_theory open category_theory.monoidal_category variables (C : Type u₁) [category.{v₁} C] [monoidal_category.{v₁} C] /-- A monoid object internal to a monoidal category. When the monoidal category is preadditive, this is also sometimes called an "algebra object". -/ structure Mon_ := (X : C) (one : 𝟙_ C ⟶ X) (mul : X ⊗ X ⟶ X) (one_mul' : (one ⊗ 𝟙 X) ≫ mul = (λ_ X).hom . obviously) (mul_one' : (𝟙 X ⊗ one) ≫ mul = (ρ_ X).hom . obviously) -- Obviously there is some flexibility stating this axiom. -- This one has left- and right-hand sides matching the statement of `monoid.mul_assoc`, -- and chooses to place the associator on the right-hand side. -- The heuristic is that unitors and associators "don't have much weight". (mul_assoc' : (mul ⊗ 𝟙 X) ≫ mul = (α_ X X X).hom ≫ (𝟙 X ⊗ mul) ≫ mul . obviously) restate_axiom Mon_.one_mul' restate_axiom Mon_.mul_one' restate_axiom Mon_.mul_assoc' attribute [reassoc] Mon_.one_mul Mon_.mul_one -- We prove a more general `@[simp]` lemma below. attribute [simp, reassoc] Mon_.mul_assoc namespace Mon_ /-- The trivial monoid object. We later show this is initial in `Mon_ C`. -/ @[simps] def trivial : Mon_ C := { X := 𝟙_ C, one := 𝟙 _, mul := (λ_ _).hom, mul_assoc' := by simp_rw [triangle_assoc, iso.cancel_iso_hom_right, tensor_right_iff, unitors_equal], mul_one' := by simp [unitors_equal] } instance : inhabited (Mon_ C) := ⟨trivial C⟩ variables {C} {M : Mon_ C} @[simp] lemma one_mul_hom {Z : C} (f : Z ⟶ M.X) : (M.one ⊗ f) ≫ M.mul = (λ_ Z).hom ≫ f := by rw [←id_tensor_comp_tensor_id, category.assoc, M.one_mul, left_unitor_naturality] @[simp] lemma mul_one_hom {Z : C} (f : Z ⟶ M.X) : (f ⊗ M.one) ≫ M.mul = (ρ_ Z).hom ≫ f := by rw [←tensor_id_comp_id_tensor, category.assoc, M.mul_one, right_unitor_naturality] lemma assoc_flip : (𝟙 M.X ⊗ M.mul) ≫ M.mul = (α_ M.X M.X M.X).inv ≫ (M.mul ⊗ 𝟙 M.X) ≫ M.mul := by simp /-- A morphism of monoid objects. -/ @[ext] structure hom (M N : Mon_ C) := (hom : M.X ⟶ N.X) (one_hom' : M.one ≫ hom = N.one . obviously) (mul_hom' : M.mul ≫ hom = (hom ⊗ hom) ≫ N.mul . obviously) restate_axiom hom.one_hom' restate_axiom hom.mul_hom' attribute [simp, reassoc] hom.one_hom hom.mul_hom /-- The identity morphism on a monoid object. -/ @[simps] def id (M : Mon_ C) : hom M M := { hom := 𝟙 M.X, } instance hom_inhabited (M : Mon_ C) : inhabited (hom M M) := ⟨id M⟩ /-- Composition of morphisms of monoid objects. -/ @[simps] def comp {M N O : Mon_ C} (f : hom M N) (g : hom N O) : hom M O := { hom := f.hom ≫ g.hom, } instance : category (Mon_ C) := { hom := λ M N, hom M N, id := id, comp := λ M N O f g, comp f g, } @[simp] lemma id_hom' (M : Mon_ C) : (𝟙 M : hom M M).hom = 𝟙 M.X := rfl @[simp] lemma comp_hom' {M N K : Mon_ C} (f : M ⟶ N) (g : N ⟶ K) : (f ≫ g : hom M K).hom = f.hom ≫ g.hom := rfl section variables (C) /-- The forgetful functor from monoid objects to the ambient category. -/ @[simps] def forget : Mon_ C ⥤ C := { obj := λ A, A.X, map := λ A B f, f.hom, } end instance forget_faithful : faithful (@forget C _ _) := { } instance {A B : Mon_ C} (f : A ⟶ B) [e : is_iso ((forget C).map f)] : is_iso f.hom := e /-- The forgetful functor from monoid objects to the ambient category reflects isomorphisms. -/ instance : reflects_isomorphisms (forget C) := { reflects := λ X Y f e, by exactI ⟨⟨{ hom := inv f.hom, mul_hom' := begin simp only [is_iso.comp_inv_eq, hom.mul_hom, category.assoc, ←tensor_comp_assoc, is_iso.inv_hom_id, tensor_id, category.id_comp], end }, by tidy⟩⟩ } instance unique_hom_from_trivial (A : Mon_ C) : unique (trivial C ⟶ A) := { default := { hom := A.one, one_hom' := by { dsimp, simp, }, mul_hom' := by { dsimp, simp [A.one_mul, unitors_equal], } }, uniq := λ f, begin ext, simp, rw [←category.id_comp f.hom], erw f.one_hom, end } open category_theory.limits instance : has_initial (Mon_ C) := has_initial_of_unique (trivial C) end Mon_ namespace category_theory.lax_monoidal_functor variables {C} {D : Type u₂} [category.{v₂} D] [monoidal_category.{v₂} D] /-- A lax monoidal functor takes monoid objects to monoid objects. That is, a lax monoidal functor `F : C ⥤ D` induces a functor `Mon_ C ⥤ Mon_ D`. -/ -- TODO: map_Mod F A : Mod A ⥤ Mod (F.map_Mon A) @[simps] def map_Mon (F : lax_monoidal_functor C D) : Mon_ C ⥤ Mon_ D := { obj := λ A, { X := F.obj A.X, one := F.ε ≫ F.map A.one, mul := F.μ _ _ ≫ F.map A.mul, one_mul' := begin conv_lhs { rw [comp_tensor_id, ←F.to_functor.map_id], }, slice_lhs 2 3 { rw [F.μ_natural], }, slice_lhs 3 4 { rw [←F.to_functor.map_comp, A.one_mul], }, rw [F.to_functor.map_id], rw [F.left_unitality], end, mul_one' := begin conv_lhs { rw [id_tensor_comp, ←F.to_functor.map_id], }, slice_lhs 2 3 { rw [F.μ_natural], }, slice_lhs 3 4 { rw [←F.to_functor.map_comp, A.mul_one], }, rw [F.to_functor.map_id], rw [F.right_unitality], end, mul_assoc' := begin conv_lhs { rw [comp_tensor_id, ←F.to_functor.map_id], }, slice_lhs 2 3 { rw [F.μ_natural], }, slice_lhs 3 4 { rw [←F.to_functor.map_comp, A.mul_assoc], }, conv_lhs { rw [F.to_functor.map_id] }, conv_lhs { rw [F.to_functor.map_comp, F.to_functor.map_comp] }, conv_rhs { rw [id_tensor_comp, ←F.to_functor.map_id], }, slice_rhs 3 4 { rw [F.μ_natural], }, conv_rhs { rw [F.to_functor.map_id] }, slice_rhs 1 3 { rw [←F.associativity], }, simp only [category.assoc], end, }, map := λ A B f, { hom := F.map f.hom, one_hom' := by { dsimp, rw [category.assoc, ←F.to_functor.map_comp, f.one_hom], }, mul_hom' := begin dsimp, rw [category.assoc, F.μ_natural_assoc, ←F.to_functor.map_comp, ←F.to_functor.map_comp, f.mul_hom], end }, map_id' := λ A, by { ext, simp, }, map_comp' := λ A B C f g, by { ext, simp, }, } variables (C D) /-- `map_Mon` is functorial in the lax monoidal functor. -/ def map_Mon_functor : (lax_monoidal_functor C D) ⥤ (Mon_ C ⥤ Mon_ D) := { obj := map_Mon, map := λ F G α, { app := λ A, { hom := α.app A.X, } } } end category_theory.lax_monoidal_functor namespace Mon_ open category_theory.lax_monoidal_functor namespace equiv_lax_monoidal_functor_punit /-- Implementation of `Mon_.equiv_lax_monoidal_functor_punit`. -/ @[simps] def lax_monoidal_to_Mon : lax_monoidal_functor (discrete punit.{u+1}) C ⥤ Mon_ C := { obj := λ F, (F.map_Mon : Mon_ _ ⥤ Mon_ C).obj (trivial (discrete punit)), map := λ F G α, ((map_Mon_functor (discrete punit) C).map α).app _ } /-- Implementation of `Mon_.equiv_lax_monoidal_functor_punit`. -/ @[simps] def Mon_to_lax_monoidal : Mon_ C ⥤ lax_monoidal_functor (discrete punit.{u+1}) C := { obj := λ A, { obj := λ _, A.X, map := λ _ _ _, 𝟙 _, ε := A.one, μ := λ _ _, A.mul, map_id' := λ _, rfl, map_comp' := λ _ _ _ _ _, (category.id_comp (𝟙 A.X)).symm, }, map := λ A B f, { app := λ _, f.hom, naturality' := λ _ _ _, by { dsimp, rw [category.id_comp, category.comp_id], }, unit' := f.one_hom, tensor' := λ _ _, f.mul_hom, }, } /-- Implementation of `Mon_.equiv_lax_monoidal_functor_punit`. -/ @[simps] def unit_iso : 𝟭 (lax_monoidal_functor (discrete punit.{u+1}) C) ≅ lax_monoidal_to_Mon C ⋙ Mon_to_lax_monoidal C := nat_iso.of_components (λ F, monoidal_nat_iso.of_components (λ _, F.to_functor.map_iso (eq_to_iso (by ext))) (by tidy) (by tidy) (by tidy)) (by tidy) /-- Implementation of `Mon_.equiv_lax_monoidal_functor_punit`. -/ @[simps] def counit_iso : Mon_to_lax_monoidal C ⋙ lax_monoidal_to_Mon C ≅ 𝟭 (Mon_ C) := nat_iso.of_components (λ F, { hom := { hom := 𝟙 _, }, inv := { hom := 𝟙 _, } }) (by tidy) end equiv_lax_monoidal_functor_punit open equiv_lax_monoidal_functor_punit /-- Monoid objects in `C` are "just" lax monoidal functors from the trivial monoidal category to `C`. -/ @[simps] def equiv_lax_monoidal_functor_punit : lax_monoidal_functor (discrete punit.{u+1}) C ≌ Mon_ C := { functor := lax_monoidal_to_Mon C, inverse := Mon_to_lax_monoidal C, unit_iso := unit_iso C, counit_iso := counit_iso C, } end Mon_ /-! Projects: * Check that `Mon_ Mon ≌ CommMon`, via the Eckmann-Hilton argument. (You'll have to hook up the cartesian monoidal structure on `Mon` first, available in #3463) * Check that `Mon_ Top ≌ [bundled topological monoids]`. * Check that `Mon_ AddCommGroup ≌ Ring`. (We've already got `Mon_ (Module R) ≌ Algebra R`, in `category_theory.monoidal.internal.Module`.) * Can you transport this monoidal structure to `Ring` or `Algebra R`? How does it compare to the "native" one? * Show that if `C` is braided then `Mon_ C` is naturally monoidal. -/
9713ad8200a01897895046b15bf0092b28ef516e
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/634.lean
8c213d0e2af6fce32ec5288f85fb5d01edaf2dd5
[ "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
303
lean
open nat namespace foo section parameter (X : Type₁) definition A {n : ℕ} : Type₁ := X variable {n : ℕ} set_option pp.implicit true check @A n set_option pp.full_names true check @foo.A X n check @A n set_option pp.full_names false check @foo.A X n check @A n end end foo
ba425c05eb34f2f4b98baff71679edcb9001d64d
1fbca480c1574e809ae95a3eda58188ff42a5e41
/src/util/data/subtype.lean
479144cedeadffc84d49fcf1a71e21450630ba64
[]
no_license
unitb/lean-lib
560eea0acf02b1fd4bcaac9986d3d7f1a4290e7e
439b80e606b4ebe4909a08b1d77f4f5c0ee3dee9
refs/heads/master
1,610,706,025,400
1,570,144,245,000
1,570,144,245,000
99,579,229
5
0
null
null
null
null
UTF-8
Lean
false
false
258
lean
universe variables u v variables {α : Type u} @[simp] lemma coe_subtype_eq_self {x : α} {P : α → Prop} (h : P x) : ↑ (⟨x, h⟩ : subtype P) = x := rfl @[simp] lemma coe_eq_subtype_val {P : α → Prop} (x : subtype P) : ↑ x = x.val := rfl
9ca67e1c3a18781d124d0c3d57ad7691d7b4fc2b
94e33a31faa76775069b071adea97e86e218a8ee
/src/topology/sheaves/sheaf_condition/sites.lean
0acfebdfea8a7455dcda5911dcd40119fc091b74
[ "Apache-2.0" ]
permissive
urkud/mathlib
eab80095e1b9f1513bfb7f25b4fa82fa4fd02989
6379d39e6b5b279df9715f8011369a301b634e41
refs/heads/master
1,658,425,342,662
1,658,078,703,000
1,658,078,703,000
186,910,338
0
0
Apache-2.0
1,568,512,083,000
1,557,958,709,000
Lean
UTF-8
Lean
false
false
24,133
lean
/- Copyright (c) 2021 Justus Springer. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Justus Springer -/ import category_theory.sites.spaces import topology.sheaves.sheaf import category_theory.sites.dense_subsite /-! # The sheaf condition in terms of sites. The theory of sheaves on sites is developed independently from sheaves on spaces in `category_theory/sites`. In this file, we connect the two theories: We show that for a topological space `X`, a presheaf `F : (opens X)ᵒᵖ ⥤ C` is a sheaf on the site `opens X` if and only if it is a sheaf on `X` in the usual sense. Recall that a presheaf `F : (opens X)ᵒᵖ ⥤ C` is called a *sheaf* on the space `X`, if for every family of opens `U : ι → opens X`, the object `F.obj (op (supr U))` is the limit of some fork diagram. On the other hand, `F` is called a *sheaf* on the site `opens X`, if for every open set `U : opens X` and every presieve `R : presieve U`, the object `F.obj (op U)` is the limit of a very similar fork diagram. In this file, we will construct the two functions `covering_of_presieve` and `presieve_of_covering`, which translate between the two concepts. We then prove a bunch of naturality lemmas relating the two fork diagrams to each other. ## Main statements * `is_sheaf_sites_iff_is_sheaf_spaces`. A presheaf `F : (opens X)ᵒᵖ ⥤ C` is a sheaf on the site `opens X` if and only if it is a sheaf on the space `X`. * `Sheaf_sites_eq_sheaf_spaces`. The type of sheaves on the site `opens X` is *equal* to the type of sheaves on the space `X`. -/ noncomputable theory universes u v w namespace Top.presheaf open category_theory topological_space Top category_theory.limits opposite open Top.presheaf.sheaf_condition_equalizer_products variables {C : Type u} [category.{v} C] [has_products.{v} C] variables {X : Top.{v}} (F : presheaf C X) /-- Given a presieve `R` on `U`, we obtain a covering family of open sets in `X`, by taking as index type the type of dependent pairs `(V, f)`, where `f : V ⟶ U` is in `R`. -/ def covering_of_presieve (U : opens X) (R : presieve U) : (Σ V, {f : V ⟶ U // R f}) → opens X := λ f, f.1 @[simp] lemma covering_of_presieve_apply (U : opens X) (R : presieve U) (f : Σ V, {f : V ⟶ U // R f}) : covering_of_presieve U R f = f.1 := rfl namespace covering_of_presieve variables (U : opens X) (R : presieve U) /-! In this section, we will relate two different fork diagrams to each other. The first one is the defining fork diagram for the sheaf condition in terms of sites, applied to the presieve `R`. It will henceforth be called the _sites diagram_. Its objects are called `presheaf.first_obj` and `presheaf.second_obj` and its morphisms are `presheaf.first_map` and `presheaf.second_obj`. The fork map into this diagram is called `presheaf.fork_map`. The second one is the defining fork diagram for the sheaf condition in terms of spaces, applied to the family of opens `covering_of_presieve U R`. It will henceforth be called the _spaces diagram_. Its objects are called `pi_opens` and `pi_inters` and its morphisms are `left_res` and `right_res`. The fork map into this diagram is called `res`. -/ /-- If `R` is a presieve in the grothendieck topology on `opens X`, the covering family associated to `R` really is _covering_, i.e. the union of all open sets equals `U`. -/ lemma supr_eq_of_mem_grothendieck (hR : sieve.generate R ∈ opens.grothendieck_topology X U) : supr (covering_of_presieve U R) = U := begin apply le_antisymm, { refine supr_le _, intro f, exact f.2.1.le, }, intros x hxU, rw [opens.mem_coe, opens.mem_supr], obtain ⟨V, iVU, ⟨W, iVW, iWU, hiWU, -⟩, hxV⟩ := hR x hxU, exact ⟨⟨W, ⟨iWU, hiWU⟩⟩, iVW.le hxV⟩, end /-- The first object in the sites diagram is isomorphic to the first object in the spaces diagram. Actually, they are even definitionally equal, but it is convenient to give this isomorphism a name. -/ def first_obj_iso_pi_opens : presheaf.first_obj R F ≅ pi_opens F (covering_of_presieve U R) := eq_to_iso rfl /-- The isomorphism `first_obj_iso_pi_opens` is compatible with canonical projections out of the product. -/ lemma first_obj_iso_pi_opens_π (f : Σ V, {f : V ⟶ U // R f}) : (first_obj_iso_pi_opens F U R).hom ≫ pi.π _ f = pi.π _ f := category.id_comp _ /-- The second object in the sites diagram is isomorphic to the second object in the spaces diagram. -/ def second_obj_iso_pi_inters : presheaf.second_obj R F ≅ pi_inters F (covering_of_presieve U R) := has_limit.iso_of_nat_iso $ discrete.nat_iso $ λ i, F.map_iso (eq_to_iso (complete_lattice.pullback_eq_inf _ _).symm).op /-- The isomorphism `second_obj_iso_pi_inters` is compatible with canonical projections out of the product. Here, we have to insert an `eq_to_hom` arrow to pass from `F.obj (op (pullback f.2.1 g.2.1))` to `F.obj (op (f.1 ⊓ g.1))`. -/ lemma second_obj_iso_pi_inters_π (f g : Σ V, {f : V ⟶ U // R f}) : (second_obj_iso_pi_inters F U R).hom ≫ pi.π _ (f, g) = pi.π _ (f, g) ≫ F.map (eq_to_hom (complete_lattice.pullback_eq_inf f.2.1 g.2.1).symm).op := begin dunfold second_obj_iso_pi_inters, rw has_limit.iso_of_nat_iso_hom_π, refl, end /-- Composing the fork map of the sites diagram with the isomorphism `first_obj_iso_pi_opens` is the same as the fork map of the spaces diagram (modulo an `eq_to_hom` arrow). -/ lemma fork_map_comp_first_obj_iso_pi_opens_eq (hR : sieve.generate R ∈ opens.grothendieck_topology X U) : presheaf.fork_map R F ≫ (first_obj_iso_pi_opens F U R).hom = F.map (eq_to_hom (supr_eq_of_mem_grothendieck U R hR)).op ≫ res F (covering_of_presieve U R) := begin ext ⟨f⟩, rw [category.assoc, category.assoc, first_obj_iso_pi_opens_π], dunfold presheaf.fork_map res, rw [limit.lift_π, fan.mk_π_app, limit.lift_π, fan.mk_π_app, ← F.map_comp], congr, end /-- First naturality condition. Under the isomorphisms `first_obj_iso_pi_opens` and `second_obj_iso_pi_inters`, the map `presheaf.first_map` corresponds to `left_res`. -/ lemma first_obj_iso_comp_left_res_eq : presheaf.first_map R F ≫ (second_obj_iso_pi_inters F U R).hom = (first_obj_iso_pi_opens F U R).hom ≫ left_res F (covering_of_presieve U R) := begin ext ⟨f, g⟩, rw [category.assoc, category.assoc, second_obj_iso_pi_inters_π], dunfold left_res presheaf.first_map, rw [limit.lift_π, fan.mk_π_app, limit.lift_π_assoc, fan.mk_π_app, ← category.assoc], erw [first_obj_iso_pi_opens_π, category.assoc, ← F.map_comp], refl, end /-- Second naturality condition. Under the isomorphisms `first_obj_iso_pi_opens` and `second_obj_iso_pi_inters`, the map `presheaf.second_map` corresponds to `right_res`. -/ lemma first_obj_iso_comp_right_res_eq : presheaf.second_map R F ≫ (second_obj_iso_pi_inters F U R).hom = (first_obj_iso_pi_opens F U R).hom ≫ right_res F (covering_of_presieve U R) := begin ext ⟨f, g⟩, dunfold right_res presheaf.second_map, rw [category.assoc, category.assoc, second_obj_iso_pi_inters_π, limit.lift_π, fan.mk_π_app, limit.lift_π_assoc, fan.mk_π_app, ← category.assoc, first_obj_iso_pi_opens_π, category.assoc, ← F.map_comp], refl, end /-- The natural isomorphism between the sites diagram and the spaces diagram. -/ @[simps] def diagram_nat_iso : parallel_pair (presheaf.first_map R F) (presheaf.second_map R F) ≅ diagram F (covering_of_presieve U R) := nat_iso.of_components (λ i, walking_parallel_pair.cases_on i (first_obj_iso_pi_opens F U R) (second_obj_iso_pi_inters F U R)) $ begin intros i j f, cases i, { cases j, { cases f, simp }, { cases f, { exact first_obj_iso_comp_left_res_eq F U R, }, { exact first_obj_iso_comp_right_res_eq F U R, } } }, { cases j, { cases f, }, { cases f, simp } }, end /-- Postcomposing the given fork of the _sites_ diagram with the natural isomorphism between the diagrams gives us a fork of the _spaces_ diagram. We construct a morphism from this fork to the given fork of the _spaces_ diagram. This is shown to be an isomorphism below. -/ @[simps] def postcompose_diagram_fork_hom (hR : sieve.generate R ∈ opens.grothendieck_topology X U) : (cones.postcompose (diagram_nat_iso F U R).hom).obj (fork.of_ι _ (presheaf.w R F)) ⟶ fork F (covering_of_presieve U R) := fork.mk_hom (F.map (eq_to_hom (supr_eq_of_mem_grothendieck U R hR)).op) (fork_map_comp_first_obj_iso_pi_opens_eq F U R hR).symm instance is_iso_postcompose_diagram_fork_hom_hom (hR : sieve.generate R ∈ opens.grothendieck_topology X U) : is_iso (postcompose_diagram_fork_hom F U R hR).hom := begin rw [postcompose_diagram_fork_hom_hom, eq_to_hom_map], apply eq_to_hom.is_iso, end instance is_iso_postcompose_diagram_fork_hom (hR : sieve.generate R ∈ opens.grothendieck_topology X U) : is_iso (postcompose_diagram_fork_hom F U R hR) := cones.cone_iso_of_hom_iso _ /-- See `postcompose_diagram_fork_hom`. -/ def postcompose_diagram_fork_iso (hR : sieve.generate R ∈ opens.grothendieck_topology X U) : (cones.postcompose (diagram_nat_iso F U R).hom).obj (fork.of_ι _ (presheaf.w R F)) ≅ fork F (covering_of_presieve U R) := as_iso (postcompose_diagram_fork_hom F U R hR) end covering_of_presieve lemma is_sheaf_sites_of_is_sheaf_spaces (Fsh : F.is_sheaf) : presheaf.is_sheaf (opens.grothendieck_topology X) F := begin rw presheaf.is_sheaf_iff_is_sheaf', intros U R hR, refine ⟨_⟩, apply (is_limit.of_cone_equiv (cones.postcompose_equivalence (covering_of_presieve.diagram_nat_iso F U R : _))).to_fun, apply (is_limit.equiv_iso_limit (covering_of_presieve.postcompose_diagram_fork_iso F U R hR)).inv_fun, exact (Fsh (covering_of_presieve U R)).some, end /-- Given a family of opens `U : ι → opens X` and any open `Y : opens X`, we obtain a presieve on `Y` by declaring that a morphism `f : V ⟶ Y` is a member of the presieve if and only if there exists an index `i : ι` such that `V = U i`. -/ def presieve_of_covering_aux {ι : Type v} (U : ι → opens X) (Y : opens X) : presieve Y := λ V f, ∃ i, V = U i /-- Take `Y` to be `supr U` and obtain a presieve over `supr U`. -/ def presieve_of_covering {ι : Type v} (U : ι → opens X) : presieve (supr U) := presieve_of_covering_aux U (supr U) /-- Given a presieve `R` on `Y`, if we take its associated family of opens via `covering_of_presieve` (which may not cover `Y` if `R` is not covering), and take the presieve on `Y` associated to the family of opens via `presieve_of_covering_aux`, then we get back the original presieve `R`. -/ @[simp] lemma covering_presieve_eq_self {Y : opens X} (R : presieve Y) : presieve_of_covering_aux (covering_of_presieve Y R) Y = R := by { ext Z f, exact ⟨λ ⟨⟨_,_,h⟩,rfl⟩, by convert h, λ h, ⟨⟨Z,f,h⟩,rfl⟩⟩ } namespace presieve_of_covering /-! In this section, we will relate two different fork diagrams to each other. The first one is the defining fork diagram for the sheaf condition in terms of spaces, applied to the family of opens `U`. It will henceforth be called the _spaces diagram_. Its objects are called `pi_opens` and `pi_inters` and its morphisms are `left_res` and `right_res`. The fork map into this diagram is called `res`. The second one is the defining fork diagram for the sheaf condition in terms of sites, applied to the presieve `presieve_of_covering U`. It will henceforth be called the _sites diagram_. Its objects are called `presheaf.first_obj` and `presheaf.second_obj` and its morphisms are `presheaf.first_map` and `presheaf.second_obj`. The fork map into this diagram is called `presheaf.fork_map`. -/ variables {ι : Type v} (U : ι → opens X) /-- The sieve generated by `presieve_of_covering U` is a member of the grothendieck topology. -/ lemma mem_grothendieck_topology : sieve.generate (presieve_of_covering U) ∈ opens.grothendieck_topology X (supr U) := begin intros x hx, obtain ⟨i, hxi⟩ := opens.mem_supr.mp hx, exact ⟨U i, opens.le_supr U i, ⟨U i, 𝟙 _, opens.le_supr U i, ⟨i, rfl⟩, category.id_comp _⟩, hxi⟩, end /-- An index `i : ι` can be turned into a dependent pair `(V, f)`, where `V` is an open set and `f : V ⟶ supr U` is a member of `presieve_of_covering U f`. -/ def hom_of_index (i : ι) : Σ V, {f : V ⟶ supr U // presieve_of_covering U f} := ⟨U i, opens.le_supr U i, i, rfl⟩ /-- By using the axiom of choice, a dependent pair `(V, f)` where `f : V ⟶ supr U` is a member of `presieve_of_covering U f` can be turned into an index `i : ι`, such that `V = U i`. -/ def index_of_hom (f : Σ V, {f : V ⟶ supr U // presieve_of_covering U f}) : ι := f.2.2.some lemma index_of_hom_spec (f : Σ V, {f : V ⟶ supr U // presieve_of_covering U f}) : f.1 = U (index_of_hom U f) := f.2.2.some_spec /-- The canonical morphism from the first object in the sites diagram to the first object in the spaces diagram. Note that this is *not* an isomorphism, as the product `pi_opens F U` may contain duplicate factors, i.e. `U : ι → opens X` may not be injective. -/ def first_obj_to_pi_opens : presheaf.first_obj (presieve_of_covering U) F ⟶ pi_opens F U := pi.lift (λ i, pi.π _ (hom_of_index U i)) /-- The canonical morphism from the first object in the spaces diagram to the first object in the sites diagram. Note that this is *not* an isomorphism, as the product `pi_opens F U` may contain duplicate factors, i.e. `U : ι → opens X` may not be injective. -/ def pi_opens_to_first_obj : pi_opens F U ⟶ presheaf.first_obj.{v v u} (presieve_of_covering U) F := pi.lift (λ f, pi.π _ (index_of_hom U f) ≫ F.map (eq_to_hom (index_of_hom_spec U f)).op) /-- Even though `first_obj_to_pi_opens` and `pi_opens_to_first_obj` are not inverse to each other, applying them both after a fork map `s.ι` does nothing. The intuition here is that a compatible family `s : Π i : ι, F.obj (op (U i))` does not care about duplicate open sets: If `U i = U j` the compatible family coincides on the intersection `U i ⊓ U j = U i = U j`, hence `s i = s j` (module an `eq_to_hom` arrow). -/ lemma fork_ι_comp_pi_opens_to_first_obj_to_pi_opens_eq (s : limits.fork (left_res F U) (right_res F U)) : s.ι ≫ pi_opens_to_first_obj F U ≫ first_obj_to_pi_opens F U = s.ι := begin ext ⟨j⟩, dunfold first_obj_to_pi_opens pi_opens_to_first_obj, rw [category.assoc, category.assoc, limit.lift_π, fan.mk_π_app, limit.lift_π, fan.mk_π_app], -- The issue here is that `index_of_hom U (hom_of_index U j)` need not be equal to `j`. -- But `U j = U (index_of_hom U (hom_of_index U j))` and hence we obtain the following -- `eq_to_hom` arrow: have i_eq : U j ⟶ U j ⊓ U (index_of_hom U (hom_of_index U j)), { apply eq_to_hom, rw ← index_of_hom_spec U, exact inf_idem.symm, }, -- Since `s` is a fork, we know that `s.ι ≫ left_res F U = s.ι ≫ right_res F U`. -- We compose both sides of this equality with the canonical projection at the index pair -- `(j, index_of_hom U (hom_of_index U j)` and the restriction along `i_eq`. have := congr_arg (λ f, f ≫ pi.π (λ p : ι × ι, F.obj (op (U p.1 ⊓ U p.2))) (j, index_of_hom U (hom_of_index U j)) ≫ F.map i_eq.op) s.condition, dsimp at this, rw [category.assoc, category.assoc] at this, symmetry, -- We claim that this is equality is our goal convert this using 2, { dunfold left_res, rw [limit.lift_π_assoc, fan.mk_π_app, category.assoc, ← F.map_comp], erw F.map_id, rw category.comp_id }, { dunfold right_res, rw [limit.lift_π_assoc, fan.mk_π_app, category.assoc, ← F.map_comp], congr, } end /-- The canonical morphism from the second object of the spaces diagram to the second object of the sites diagram. -/ def pi_inters_to_second_obj : pi_inters F U ⟶ presheaf.second_obj.{v v u} (presieve_of_covering U) F := pi.lift (λ f, pi.π _ (index_of_hom U f.fst, index_of_hom U f.snd) ≫ F.map (eq_to_hom (by rw [complete_lattice.pullback_eq_inf, ← index_of_hom_spec U, ← index_of_hom_spec U])).op) lemma pi_opens_to_first_obj_comp_fist_map_eq : pi_opens_to_first_obj F U ≫ presheaf.first_map (presieve_of_covering U) F = left_res F U ≫ pi_inters_to_second_obj F U := begin ext ⟨f, g⟩, dunfold pi_opens_to_first_obj presheaf.first_map left_res pi_inters_to_second_obj, rw [category.assoc, category.assoc, limit.lift_π, fan.mk_π_app, limit.lift_π, fan.mk_π_app, ← category.assoc, ← category.assoc, limit.lift_π, fan.mk_π_app, limit.lift_π, fan.mk_π_app, category.assoc, category.assoc, ← F.map_comp, ← F.map_comp], refl, end lemma pi_opens_to_first_obj_comp_second_map_eq : pi_opens_to_first_obj F U ≫ presheaf.second_map (presieve_of_covering U) F = right_res F U ≫ pi_inters_to_second_obj F U := begin ext ⟨f, g⟩, dunfold pi_opens_to_first_obj presheaf.second_map right_res pi_inters_to_second_obj, rw [category.assoc, category.assoc, limit.lift_π, fan.mk_π_app, limit.lift_π, fan.mk_π_app, ← category.assoc, ← category.assoc, limit.lift_π, fan.mk_π_app, limit.lift_π, fan.mk_π_app, category.assoc, category.assoc, ← F.map_comp, ← F.map_comp], refl, end lemma fork_map_comp_first_map_to_pi_opens_eq : presheaf.fork_map (presieve_of_covering U) F ≫ first_obj_to_pi_opens F U = res F U := begin ext i, dsimp [presheaf.fork_map, first_obj_to_pi_opens, res], rw [category.assoc, limit.lift_π, fan.mk_π_app, limit.lift_π, fan.mk_π_app, limit.lift_π, fan.mk_π_app], refl, end lemma res_comp_pi_opens_to_first_obj_eq : res F U ≫ pi_opens_to_first_obj F U = presheaf.fork_map (presieve_of_covering U) F := begin ext f, dunfold res pi_opens_to_first_obj presheaf.fork_map, rw [category.assoc, limit.lift_π, fan.mk_π_app, limit.lift_π, fan.mk_π_app, ← category.assoc, limit.lift_π, fan.mk_π_app, ← F.map_comp], congr, end end presieve_of_covering open presieve_of_covering lemma is_sheaf_spaces_of_is_sheaf_sites (Fsh : presheaf.is_sheaf (opens.grothendieck_topology X) F) : F.is_sheaf := begin intros ι U, rw presheaf.is_sheaf_iff_is_sheaf' at Fsh, -- We know that the sites diagram for `presieve_of_covering U` is a limit fork obtain ⟨h_limit⟩ := Fsh (supr U) (presieve_of_covering U) (presieve_of_covering.mem_grothendieck_topology U), refine ⟨fork.is_limit.mk' _ _⟩, -- Here, we are given an arbitrary fork of the spaces diagram and need to show that it factors -- uniquely through our limit fork. intro s, -- Composing `s.ι` with `pi_opens_to_first_obj F U` gives a fork of the sites diagram, which -- must factor through `presheaf.fork_map`. obtain ⟨l, hl⟩ := fork.is_limit.lift' h_limit (s.ι ≫ pi_opens_to_first_obj F U) _, swap, { rw [category.assoc, category.assoc, pi_opens_to_first_obj_comp_fist_map_eq, pi_opens_to_first_obj_comp_second_map_eq, ← category.assoc, ← category.assoc, s.condition] }, -- We claim that `l` also gives a factorization of `s.ι` refine ⟨l, _, _⟩, { rw [← fork_ι_comp_pi_opens_to_first_obj_to_pi_opens_eq F U s, ← category.assoc, ← hl, category.assoc, fork.ι_of_ι, fork_map_comp_first_map_to_pi_opens_eq], refl }, { intros m hm, apply fork.is_limit.hom_ext h_limit, rw [hl, fork.ι_of_ι], simp_rw ← res_comp_pi_opens_to_first_obj_eq, erw [← category.assoc, hm], }, end lemma is_sheaf_sites_iff_is_sheaf_spaces : presheaf.is_sheaf (opens.grothendieck_topology X) F ↔ F.is_sheaf := iff.intro (is_sheaf_spaces_of_is_sheaf_sites F) (is_sheaf_sites_of_is_sheaf_spaces F) variables (C X) /-- Turn a sheaf on the site `opens X` into a sheaf on the space `X`. -/ @[simps] def Sheaf_sites_to_sheaf_spaces : Sheaf (opens.grothendieck_topology X) C ⥤ sheaf C X := { obj := λ F, ⟨F.1, is_sheaf_spaces_of_is_sheaf_sites F.1 F.2⟩, map := λ F G f, f.val } /-- Turn a sheaf on the space `X` into a sheaf on the site `opens X`. -/ @[simps] def Sheaf_spaces_to_sheaf_sites : sheaf C X ⥤ Sheaf (opens.grothendieck_topology X) C := { obj := λ F, ⟨F.1, is_sheaf_sites_of_is_sheaf_spaces F.1 F.2⟩, map := λ F G f, ⟨f⟩ } /-- The equivalence of categories between sheaves on the site `opens X` and sheaves on the space `X`. -/ @[simps] def Sheaf_spaces_equiv_sheaf_sites : Sheaf (opens.grothendieck_topology X) C ≌ sheaf C X := { functor := Sheaf_sites_to_sheaf_spaces C X, inverse := Sheaf_spaces_to_sheaf_sites C X, unit_iso := nat_iso.of_components (λ t, ⟨⟨𝟙 _⟩, ⟨𝟙 _⟩, by { ext1, simp }, by { ext1, simp }⟩) $ by { intros, ext1, dsimp, simp }, counit_iso := nat_iso.of_components (λ t, ⟨𝟙 _, 𝟙 _, by { ext, simp }, by { ext, simp }⟩) $ by { intros, ext, dsimp, simp } } /-- The two forgetful functors are isomorphic via `Sheaf_spaces_equiv_sheaf_sites`. -/ def Sheaf_spaces_equiv_sheaf_sites_functor_forget : (Sheaf_spaces_equiv_sheaf_sites C X).functor ⋙ sheaf.forget C X ≅ Sheaf_to_presheaf _ _ := nat_iso.of_components (λ F, (iso.refl F.1)) (λ F G f, by { erw [category.comp_id, category.id_comp], refl }) /-- The two forgetful functors are isomorphic via `Sheaf_spaces_equiv_sheaf_sites`. -/ def Sheaf_spaces_equiv_sheaf_sites_inverse_forget : (Sheaf_spaces_equiv_sheaf_sites C X).inverse ⋙ Sheaf_to_presheaf _ _ ≅ sheaf.forget C X := nat_iso.of_components (λ F, (iso.refl F.1)) (λ F G f, by { erw [category.comp_id, category.id_comp], refl }) end Top.presheaf namespace Top.opens open category_theory topological_space variables {X : Top} {ι : Type*} lemma cover_dense_iff_is_basis [category ι] (B : ι ⥤ opens X) : cover_dense (opens.grothendieck_topology X) B ↔ opens.is_basis (set.range B.obj) := begin rw opens.is_basis_iff_nbhd, split, intros hd U x hx, rcases hd.1 U x hx with ⟨V,f,⟨i,f₁,f₂,hc⟩,hV⟩, exact ⟨B.obj i, ⟨i,rfl⟩, f₁.le hV, f₂.le⟩, intro hb, split, intros U x hx, rcases hb hx with ⟨_,⟨i,rfl⟩,hx,hi⟩, exact ⟨B.obj i, ⟨⟨hi⟩⟩, ⟨⟨i, 𝟙 _, ⟨⟨hi⟩⟩, rfl⟩⟩, hx⟩, end lemma cover_dense_induced_functor {B : ι → opens X} (h : opens.is_basis (set.range B)) : cover_dense (opens.grothendieck_topology X) (induced_functor B) := (cover_dense_iff_is_basis _).2 h end Top.opens namespace Top.sheaf open category_theory topological_space Top opposite variables {C : Type u} [category.{v} C] [limits.has_products.{v} C] variables {X : Top.{v}} {ι : Type*} {B : ι → opens X} variables (F : presheaf C X) (F' : sheaf C X) (h : opens.is_basis (set.range B)) /-- The empty component of a sheaf is terminal -/ def is_terminal_of_empty (F : sheaf C X) : limits.is_terminal (F.val.obj (op ∅)) := ((presheaf.Sheaf_spaces_to_sheaf_sites C X).obj F).is_terminal_of_bot_cover ∅ (by tidy) /-- A variant of `is_terminal_of_empty` that is easier to `apply`. -/ def is_terminal_of_eq_empty (F : X.sheaf C) {U : opens X} (h : U = ∅) : limits.is_terminal (F.val.obj (op U)) := by convert F.is_terminal_of_empty /-- If a family `B` of open sets forms a basis of the topology on `X`, and if `F'` is a sheaf on `X`, then a homomorphism between a presheaf `F` on `X` and `F'` is equivalent to a homomorphism between their restrictions to the indexing type `ι` of `B`, with the induced category structure on `ι`. -/ def restrict_hom_equiv_hom : ((induced_functor B).op ⋙ F ⟶ (induced_functor B).op ⋙ F'.1) ≃ (F ⟶ F'.1) := @cover_dense.restrict_hom_equiv_hom _ _ _ _ _ _ _ _ (opens.cover_dense_induced_functor h) _ F ((presheaf.Sheaf_spaces_to_sheaf_sites C X).obj F') @[simp] lemma extend_hom_app (α : ((induced_functor B).op ⋙ F ⟶ (induced_functor B).op ⋙ F'.1)) (i : ι) : (restrict_hom_equiv_hom F F' h α).app (op (B i)) = α.app (op i) := by { nth_rewrite 1 ← (restrict_hom_equiv_hom F F' h).left_inv α, refl } include h lemma hom_ext {α β : F ⟶ F'.1} (he : ∀ i, α.app (op (B i)) = β.app (op (B i))) : α = β := by { apply (restrict_hom_equiv_hom F F' h).symm.injective, ext i, exact he i.unop } end Top.sheaf
65c776579b279a932ebf6206bb08422b5477e31f
26bff4ed296b8373c92b6b025f5d60cdf02104b9
/library/data/num.lean
6ce40d21fa3dcca90b63a502795d8e370ce12317
[ "Apache-2.0" ]
permissive
guiquanz/lean
b8a878ea24f237b84b0e6f6be2f300e8bf028229
242f8ba0486860e53e257c443e965a82ee342db3
refs/heads/master
1,526,680,092,098
1,427,492,833,000
1,427,493,281,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
17,770
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Module: data.num Author: Leonardo de Moura -/ import data.bool tools.helper_tactics open bool eq.ops decidable helper_tactics namespace pos_num theorem succ_not_is_one (a : pos_num) : is_one (succ a) = ff := pos_num.induction_on a rfl (take n iH, rfl) (take n iH, rfl) theorem succ_one : succ one = bit0 one theorem succ_bit1 (a : pos_num) : succ (bit1 a) = bit0 (succ a) theorem succ_bit0 (a : pos_num) : succ (bit0 a) = bit1 a theorem ne_of_bit0_ne_bit0 {a b : pos_num} (H₁ : bit0 a ≠ bit0 b) : a ≠ b := assume H : a = b, absurd rfl (H ▸ H₁) theorem ne_of_bit1_ne_bit1 {a b : pos_num} (H₁ : bit1 a ≠ bit1 b) : a ≠ b := assume H : a = b, absurd rfl (H ▸ H₁) theorem pred_succ : ∀ (a : pos_num), pred (succ a) = a | one := rfl | (bit0 a) := by rewrite succ_bit0 | (bit1 a) := calc pred (succ (bit1 a)) = cond (is_one (succ a)) one (bit1 (pred (succ a))) : rfl ... = cond ff one (bit1 (pred (succ a))) : succ_not_is_one ... = bit1 (pred (succ a)) : rfl ... = bit1 a : pred_succ a section variables (a b : pos_num) theorem one_add_one : one + one = bit0 one theorem one_add_bit0 : one + (bit0 a) = bit1 a theorem one_add_bit1 : one + (bit1 a) = succ (bit1 a) theorem bit0_add_one : (bit0 a) + one = bit1 a theorem bit1_add_one : (bit1 a) + one = succ (bit1 a) theorem bit0_add_bit0 : (bit0 a) + (bit0 b) = bit0 (a + b) theorem bit0_add_bit1 : (bit0 a) + (bit1 b) = bit1 (a + b) theorem bit1_add_bit0 : (bit1 a) + (bit0 b) = bit1 (a + b) theorem bit1_add_bit1 : (bit1 a) + (bit1 b) = succ (bit1 (a + b)) theorem one_mul : one * a = a end theorem mul_one : ∀ a, a * one = a | one := rfl | (bit1 n) := calc bit1 n * one = bit0 (n * one) + one : rfl ... = bit0 n + one : mul_one n ... = bit1 n : bit0_add_one | (bit0 n) := calc bit0 n * one = bit0 (n * one) : rfl ... = bit0 n : mul_one n theorem decidable_eq [instance] : ∀ (a b : pos_num), decidable (a = b) | one one := inl rfl | one (bit0 b) := inr (λ H, pos_num.no_confusion H) | one (bit1 b) := inr (λ H, pos_num.no_confusion H) | (bit0 a) one := inr (λ H, pos_num.no_confusion H) | (bit0 a) (bit0 b) := match decidable_eq a b with | inl H₁ := inl (eq.rec_on H₁ rfl) | inr H₁ := inr (λ H, pos_num.no_confusion H (λ H₂, absurd H₂ H₁)) end | (bit0 a) (bit1 b) := inr (λ H, pos_num.no_confusion H) | (bit1 a) one := inr (λ H, pos_num.no_confusion H) | (bit1 a) (bit0 b) := inr (λ H, pos_num.no_confusion H) | (bit1 a) (bit1 b) := match decidable_eq a b with | inl H₁ := inl (eq.rec_on H₁ rfl) | inr H₁ := inr (λ H, pos_num.no_confusion H (λ H₂, absurd H₂ H₁)) end local notation a < b := (lt a b = tt) local notation a `≮`:50 b:50 := (lt a b = ff) theorem lt_one_right_eq_ff : ∀ a : pos_num, a ≮ one | one := rfl | (bit0 a) := rfl | (bit1 a) := rfl theorem lt_one_succ_eq_tt : ∀ a : pos_num, one < succ a | one := rfl | (bit0 a) := rfl | (bit1 a) := rfl theorem lt_of_lt_bit0_bit0 {a b : pos_num} (H : bit0 a < bit0 b) : a < b := H theorem lt_of_lt_bit0_bit1 {a b : pos_num} (H : bit1 a < bit0 b) : a < b := H theorem lt_of_lt_bit1_bit1 {a b : pos_num} (H : bit1 a < bit1 b) : a < b := H theorem lt_of_lt_bit1_bit0 {a b : pos_num} (H : bit0 a < bit1 b) : a < succ b := H theorem lt_bit0_bit0_eq_lt (a b : pos_num) : lt (bit0 a) (bit0 b) = lt a b := rfl theorem lt_bit1_bit1_eq_lt (a b : pos_num) : lt (bit1 a) (bit1 b) = lt a b := rfl theorem lt_bit1_bit0_eq_lt (a b : pos_num) : lt (bit1 a) (bit0 b) = lt a b := rfl theorem lt_bit0_bit1_eq_lt_succ (a b : pos_num) : lt (bit0 a) (bit1 b) = lt a (succ b) := rfl theorem lt_irrefl : ∀ (a : pos_num), a ≮ a | one := rfl | (bit0 a) := begin rewrite lt_bit0_bit0_eq_lt, apply lt_irrefl end | (bit1 a) := begin rewrite lt_bit1_bit1_eq_lt, apply lt_irrefl end theorem ne_of_lt_eq_tt : ∀ {a b : pos_num}, a < b → a = b → false | one ⌞one⌟ H₁ (eq.refl one) := absurd H₁ ff_ne_tt | (bit0 a) ⌞(bit0 a)⌟ H₁ (eq.refl (bit0 a)) := begin rewrite lt_bit0_bit0_eq_lt at H₁, apply (ne_of_lt_eq_tt H₁ (eq.refl a)) end | (bit1 a) ⌞(bit1 a)⌟ H₁ (eq.refl (bit1 a)) := begin rewrite lt_bit1_bit1_eq_lt at H₁, apply (ne_of_lt_eq_tt H₁ (eq.refl a)) end theorem lt_base : ∀ a : pos_num, a < succ a | one := rfl | (bit0 a) := begin rewrite [succ_bit0, lt_bit0_bit1_eq_lt_succ], apply lt_base end | (bit1 a) := begin rewrite [succ_bit1, lt_bit1_bit0_eq_lt], apply lt_base end theorem lt_step : ∀ {a b : pos_num}, a < b → a < succ b | one one H := rfl | one (bit0 b) H := rfl | one (bit1 b) H := rfl | (bit0 a) one H := absurd H ff_ne_tt | (bit0 a) (bit0 b) H := begin rewrite [succ_bit0, lt_bit0_bit1_eq_lt_succ, lt_bit0_bit0_eq_lt at H], apply (lt_step H) end | (bit0 a) (bit1 b) H := begin rewrite [succ_bit1, lt_bit0_bit0_eq_lt, lt_bit0_bit1_eq_lt_succ at H], exact H end | (bit1 a) one H := absurd H ff_ne_tt | (bit1 a) (bit0 b) H := begin rewrite [succ_bit0, lt_bit1_bit1_eq_lt, lt_bit1_bit0_eq_lt at H], exact H end | (bit1 a) (bit1 b) H := begin rewrite [succ_bit1, lt_bit1_bit0_eq_lt, lt_bit1_bit1_eq_lt at H], apply (lt_step H) end theorem lt_of_lt_succ_succ : ∀ {a b : pos_num}, succ a < succ b → a < b | one one H := absurd H ff_ne_tt | one (bit0 b) H := rfl | one (bit1 b) H := rfl | (bit0 a) one H := begin rewrite [succ_bit0 at H, succ_one at H, lt_bit1_bit0_eq_lt at H], apply (absurd_of_eq_ff_of_eq_tt (lt_one_right_eq_ff a) H) end | (bit0 a) (bit0 b) H := by exact H | (bit0 a) (bit1 b) H := by exact H | (bit1 a) one H := begin rewrite [succ_bit1 at H, succ_one at H, lt_bit0_bit0_eq_lt at H], apply (absurd_of_eq_ff_of_eq_tt (lt_one_right_eq_ff (succ a)) H) end | (bit1 a) (bit0 b) H := begin rewrite [succ_bit1 at H, succ_bit0 at H, lt_bit0_bit1_eq_lt_succ at H], rewrite lt_bit1_bit0_eq_lt, apply (lt_of_lt_succ_succ H) end | (bit1 a) (bit1 b) H := begin rewrite [lt_bit1_bit1_eq_lt, *succ_bit1 at H, lt_bit0_bit0_eq_lt at H], apply (lt_of_lt_succ_succ H) end theorem lt_succ_succ : ∀ {a b : pos_num}, a < b → succ a < succ b | one one H := absurd H ff_ne_tt | one (bit0 b) H := begin rewrite [succ_bit0, succ_one, lt_bit0_bit1_eq_lt_succ], apply lt_one_succ_eq_tt end | one (bit1 b) H := begin rewrite [succ_one, succ_bit1, lt_bit0_bit0_eq_lt], apply lt_one_succ_eq_tt end | (bit0 a) one H := absurd H ff_ne_tt | (bit0 a) (bit0 b) H := by exact H | (bit0 a) (bit1 b) H := by exact H | (bit1 a) one H := absurd H ff_ne_tt | (bit1 a) (bit0 b) H := begin rewrite [succ_bit1, succ_bit0, lt_bit0_bit1_eq_lt_succ, lt_bit1_bit0_eq_lt at H], apply (lt_succ_succ H) end | (bit1 a) (bit1 b) H := begin rewrite [lt_bit1_bit1_eq_lt at H, *succ_bit1, lt_bit0_bit0_eq_lt], apply (lt_succ_succ H) end theorem lt_of_lt_succ : ∀ {a b : pos_num}, succ a < b → a < b | one one H := absurd_of_eq_ff_of_eq_tt !lt_one_right_eq_ff H | one (bit0 b) H := rfl | one (bit1 b) H := rfl | (bit0 a) one H := absurd_of_eq_ff_of_eq_tt !lt_one_right_eq_ff H | (bit0 a) (bit0 b) H := by exact H | (bit0 a) (bit1 b) H := begin rewrite [succ_bit0 at H, lt_bit1_bit1_eq_lt at H, lt_bit0_bit1_eq_lt_succ], apply (lt_step H) end | (bit1 a) one H := absurd_of_eq_ff_of_eq_tt !lt_one_right_eq_ff H | (bit1 a) (bit0 b) H := begin rewrite [lt_bit1_bit0_eq_lt, succ_bit1 at H, lt_bit0_bit0_eq_lt at H], apply (lt_of_lt_succ H) end | (bit1 a) (bit1 b) H := begin rewrite [succ_bit1 at H, lt_bit0_bit1_eq_lt_succ at H, lt_bit1_bit1_eq_lt], apply (lt_of_lt_succ_succ H) end theorem lt_of_lt_succ_of_ne : ∀ {a b : pos_num}, a < succ b → a ≠ b → a < b | one one H₁ H₂ := absurd rfl H₂ | one (bit0 b) H₁ H₂ := rfl | one (bit1 b) H₁ H₂ := rfl | (bit0 a) one H₁ H₂ := begin rewrite [succ_one at H₁, lt_bit0_bit0_eq_lt at H₁], apply (absurd_of_eq_ff_of_eq_tt (lt_one_right_eq_ff _) H₁) end | (bit0 a) (bit0 b) H₁ H₂ := begin rewrite [lt_bit0_bit0_eq_lt, succ_bit0 at H₁, lt_bit0_bit1_eq_lt_succ at H₁], apply (lt_of_lt_succ_of_ne H₁ (ne_of_bit0_ne_bit0 H₂)) end | (bit0 a) (bit1 b) H₁ H₂ := begin rewrite [succ_bit1 at H₁, lt_bit0_bit0_eq_lt at H₁, lt_bit0_bit1_eq_lt_succ], exact H₁ end | (bit1 a) one H₁ H₂ := begin rewrite [succ_one at H₁, lt_bit1_bit0_eq_lt at H₁], apply (absurd_of_eq_ff_of_eq_tt (lt_one_right_eq_ff _) H₁) end | (bit1 a) (bit0 b) H₁ H₂ := begin rewrite [succ_bit0 at H₁, lt_bit1_bit1_eq_lt at H₁, lt_bit1_bit0_eq_lt], exact H₁ end | (bit1 a) (bit1 b) H₁ H₂ := begin rewrite [succ_bit1 at H₁, lt_bit1_bit0_eq_lt at H₁, lt_bit1_bit1_eq_lt], apply (lt_of_lt_succ_of_ne H₁ (ne_of_bit1_ne_bit1 H₂)) end theorem lt_trans : ∀ {a b c : pos_num}, a < b → b < c → a < c | one b (bit0 c) H₁ H₂ := rfl | one b (bit1 c) H₁ H₂ := rfl | a (bit0 b) one H₁ H₂ := absurd_of_eq_ff_of_eq_tt (lt_one_right_eq_ff _) H₂ | a (bit1 b) one H₁ H₂ := absurd_of_eq_ff_of_eq_tt (lt_one_right_eq_ff _) H₂ | (bit0 a) (bit0 b) (bit0 c) H₁ H₂ := begin rewrite lt_bit0_bit0_eq_lt at *, apply (lt_trans H₁ H₂) end | (bit0 a) (bit0 b) (bit1 c) H₁ H₂ := begin rewrite [lt_bit0_bit1_eq_lt_succ at *, lt_bit0_bit0_eq_lt at H₁], apply (lt_trans H₁ H₂) end | (bit0 a) (bit1 b) (bit0 c) H₁ H₂ := begin rewrite [lt_bit0_bit1_eq_lt_succ at H₁, lt_bit1_bit0_eq_lt at H₂, lt_bit0_bit0_eq_lt], apply (@by_cases (a = b)), begin intro H, rewrite -H at H₂, exact H₂ end, begin intro H, apply (lt_trans (lt_of_lt_succ_of_ne H₁ H) H₂) end end | (bit0 a) (bit1 b) (bit1 c) H₁ H₂ := begin rewrite [lt_bit0_bit1_eq_lt_succ at *, lt_bit1_bit1_eq_lt at H₂], apply (lt_trans H₁ (lt_succ_succ H₂)) end | (bit1 a) (bit0 b) (bit0 c) H₁ H₂ := begin rewrite [lt_bit0_bit0_eq_lt at H₂, lt_bit1_bit0_eq_lt at *], apply (lt_trans H₁ H₂) end | (bit1 a) (bit0 b) (bit1 c) H₁ H₂ := begin rewrite [lt_bit1_bit0_eq_lt at H₁, lt_bit0_bit1_eq_lt_succ at H₂, lt_bit1_bit1_eq_lt], apply (@by_cases (b = c)), begin intro H, rewrite H at H₁, exact H₁ end, begin intro H, apply (lt_trans H₁ (lt_of_lt_succ_of_ne H₂ H)) end end | (bit1 a) (bit1 b) (bit0 c) H₁ H₂ := begin rewrite [lt_bit1_bit1_eq_lt at H₁, lt_bit1_bit0_eq_lt at H₂, lt_bit1_bit0_eq_lt], apply (lt_trans H₁ H₂) end | (bit1 a) (bit1 b) (bit1 c) H₁ H₂ := begin rewrite lt_bit1_bit1_eq_lt at *, apply (lt_trans H₁ H₂) end theorem lt_antisymm : ∀ {a b : pos_num}, a < b → b ≮ a | one one H := rfl | one (bit0 b) H := rfl | one (bit1 b) H := rfl | (bit0 a) one H := absurd H ff_ne_tt | (bit0 a) (bit0 b) H := begin rewrite lt_bit0_bit0_eq_lt at *, apply (lt_antisymm H) end | (bit0 a) (bit1 b) H := begin rewrite lt_bit1_bit0_eq_lt, rewrite lt_bit0_bit1_eq_lt_succ at H, have H₁ : succ b ≮ a, from lt_antisymm H, apply eq_ff_of_ne_tt, intro H₂, apply (@by_cases (succ b = a)), show succ b = a → false, begin intro Hp, rewrite -Hp at H, apply (absurd_of_eq_ff_of_eq_tt (lt_irrefl (succ b)) H) end, show succ b ≠ a → false, begin intro Hn, have H₃ : succ b < succ a, from lt_succ_succ H₂, have H₄ : succ b < a, from lt_of_lt_succ_of_ne H₃ Hn, apply (absurd_of_eq_ff_of_eq_tt H₁ H₄) end, end | (bit1 a) one H := absurd H ff_ne_tt | (bit1 a) (bit0 b) H := begin rewrite lt_bit0_bit1_eq_lt_succ, rewrite lt_bit1_bit0_eq_lt at H, have H₁ : lt b a = ff, from lt_antisymm H, apply eq_ff_of_ne_tt, intro H₂, apply (@by_cases (b = a)), show b = a → false, begin intro Hp, rewrite -Hp at H, apply (absurd_of_eq_ff_of_eq_tt (lt_irrefl b) H) end, show b ≠ a → false, begin intro Hn, have H₃ : b < a, from lt_of_lt_succ_of_ne H₂ Hn, apply (absurd_of_eq_ff_of_eq_tt H₁ H₃) end, end | (bit1 a) (bit1 b) H := begin rewrite lt_bit1_bit1_eq_lt at *, apply (lt_antisymm H) end local notation a ≤ b := (le a b = tt) theorem le_refl : ∀ a : pos_num, a ≤ a := lt_base theorem le_eq_lt_succ {a b : pos_num} : le a b = lt a (succ b) := rfl theorem not_lt_of_le : ∀ {a b : pos_num}, a ≤ b → b < a → false | one one H₁ H₂ := absurd H₂ ff_ne_tt | one (bit0 b) H₁ H₂ := absurd_of_eq_ff_of_eq_tt (lt_one_right_eq_ff _) H₂ | one (bit1 b) H₁ H₂ := absurd_of_eq_ff_of_eq_tt (lt_one_right_eq_ff _) H₂ | (bit0 a) one H₁ H₂ := begin rewrite [le_eq_lt_succ at H₁, succ_one at H₁, lt_bit0_bit0_eq_lt at H₁], apply (absurd_of_eq_ff_of_eq_tt (lt_one_right_eq_ff _) H₁) end | (bit0 a) (bit0 b) H₁ H₂ := begin rewrite [le_eq_lt_succ at H₁, succ_bit0 at H₁, lt_bit0_bit1_eq_lt_succ at H₁], rewrite [lt_bit0_bit0_eq_lt at H₂], apply (not_lt_of_le H₁ H₂) end | (bit0 a) (bit1 b) H₁ H₂ := begin rewrite [le_eq_lt_succ at H₁, succ_bit1 at H₁, lt_bit0_bit0_eq_lt at H₁], rewrite [lt_bit1_bit0_eq_lt at H₂], apply (not_lt_of_le H₁ H₂) end | (bit1 a) one H₁ H₂ := begin rewrite [le_eq_lt_succ at H₁, succ_one at H₁, lt_bit1_bit0_eq_lt at H₁], apply (absurd_of_eq_ff_of_eq_tt (lt_one_right_eq_ff _) H₁) end | (bit1 a) (bit0 b) H₁ H₂ := begin rewrite [le_eq_lt_succ at H₁, succ_bit0 at H₁, lt_bit1_bit1_eq_lt at H₁], rewrite lt_bit0_bit1_eq_lt_succ at H₂, have H₃ : a < succ b, from lt_step H₁, apply (@by_cases (b = a)), begin intro Hba, rewrite -Hba at H₁, apply (absurd_of_eq_ff_of_eq_tt (lt_irrefl b) H₁) end, begin intro Hnba, have H₄ : b < a, from lt_of_lt_succ_of_ne H₂ Hnba, apply (not_lt_of_le H₃ H₄) end end | (bit1 a) (bit1 b) H₁ H₂ := begin rewrite [le_eq_lt_succ at H₁, succ_bit1 at H₁, lt_bit1_bit0_eq_lt at H₁], rewrite [lt_bit1_bit1_eq_lt at H₂], apply (not_lt_of_le H₁ H₂) end theorem le_antisymm : ∀ {a b : pos_num}, a ≤ b → b ≤ a → a = b | one one H₁ H₂ := rfl | one (bit0 b) H₁ H₂ := by apply (absurd_of_eq_ff_of_eq_tt (lt_one_right_eq_ff b) H₂) | one (bit1 b) H₁ H₂ := by apply (absurd_of_eq_ff_of_eq_tt (lt_one_right_eq_ff b) H₂) | (bit0 a) one H₁ H₂ := by apply (absurd_of_eq_ff_of_eq_tt (lt_one_right_eq_ff a) H₁) | (bit0 a) (bit0 b) H₁ H₂ := begin rewrite [le_eq_lt_succ at *, succ_bit0 at *, lt_bit0_bit1_eq_lt_succ at *], have H : a = b, from le_antisymm H₁ H₂, rewrite H end | (bit0 a) (bit1 b) H₁ H₂ := begin rewrite [le_eq_lt_succ at *, succ_bit1 at H₁, succ_bit0 at H₂], rewrite [lt_bit0_bit0_eq_lt at H₁, lt_bit1_bit1_eq_lt at H₂], apply (false.rec _ (not_lt_of_le H₁ H₂)) end | (bit1 a) one H₁ H₂ := by apply (absurd_of_eq_ff_of_eq_tt (lt_one_right_eq_ff a) H₁) | (bit1 a) (bit0 b) H₁ H₂ := begin rewrite [le_eq_lt_succ at *, succ_bit0 at H₁, succ_bit1 at H₂], rewrite [lt_bit1_bit1_eq_lt at H₁, lt_bit0_bit0_eq_lt at H₂], apply (false.rec _ (not_lt_of_le H₂ H₁)) end | (bit1 a) (bit1 b) H₁ H₂ := begin rewrite [le_eq_lt_succ at *, succ_bit1 at *, lt_bit1_bit0_eq_lt at *], have H : a = b, from le_antisymm H₁ H₂, rewrite H end theorem le_trans {a b c : pos_num} : a ≤ b → b ≤ c → a ≤ c := begin intros (H₁, H₂), rewrite [le_eq_lt_succ at *], apply (@by_cases (a = b)), begin intro Hab, rewrite Hab, exact H₂ end, begin intro Hnab, have Haltb : a < b, from lt_of_lt_succ_of_ne H₁ Hnab, apply (lt_trans Haltb H₂) end, end end pos_num
97cee66b2e64328b1b2435d78e99b7e3d1f4f935
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/Init/Data/Stream.lean
2eb2fb5143c79d8b419e4930c562f9d0be084c79
[ "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
3,198
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sebastian Ullrich, Andrew Kent, Leonardo de Moura -/ prelude import Init.Data.Array.Subarray import Init.Data.Range /-! Remark: we considered using the following alternative design ``` structure Stream (α : Type u) where stream : Type u next? : stream → Option (α × stream) class ToStream (collection : Type u) (value : outParam (Type v)) where toStream : collection → Stream value ``` where `Stream` is not a class, and its state is encapsulated. The key problem is that the type `Stream α` "lives" in a universe higher than `α`. This is a problem because we want to use `Stream`s in monadic code. -/ /-- Streams are used to implement parallel `for` statements. Example: ``` for x in xs, y in ys do ... ``` is expanded into ``` let mut s := toStream ys for x in xs do match Stream.next? s with | none => break | some (y, s') => s := s' ... ``` -/ class ToStream (collection : Type u) (stream : outParam (Type u)) : Type u where toStream : collection → stream export ToStream (toStream) class Stream (stream : Type u) (value : outParam (Type v)) : Type (max u v) where next? : stream → Option (value × stream) protected partial def Stream.forIn [Stream ρ α] [Monad m] (s : ρ) (b : β) (f : α → β → m (ForInStep β)) : m β := do let _ : Inhabited (m β) := ⟨pure b⟩ let rec visit (s : ρ) (b : β) : m β := do match Stream.next? s with | some (a, s) => match (← f a b) with | ForInStep.done b => return b | ForInStep.yield b => visit s b | none => return b visit s b instance (priority := low) [Stream ρ α] : ForIn m ρ α where forIn := Stream.forIn instance : ToStream (List α) (List α) where toStream c := c instance : ToStream (Array α) (Subarray α) where toStream a := a[:a.size] instance : ToStream (Subarray α) (Subarray α) where toStream a := a instance : ToStream String Substring where toStream s := s.toSubstring instance : ToStream Std.Range Std.Range where toStream r := r instance [Stream ρ α] [Stream γ β] : Stream (ρ × γ) (α × β) where next? | (s₁, s₂) => match Stream.next? s₁ with | none => none | some (a, s₁) => match Stream.next? s₂ with | none => none | some (b, s₂) => some ((a, b), (s₁, s₂)) instance : Stream (List α) α where next? | [] => none | a::as => some (a, as) instance : Stream (Subarray α) α where next? s := if h : s.start < s.stop then have : s.start + 1 ≤ s.stop := Nat.succ_le_of_lt h some (s.as.get ⟨s.start, Nat.lt_of_lt_of_le h s.h₂⟩, { s with start := s.start + 1, h₁ := this }) else none instance : Stream Std.Range Nat where next? r := if r.start < r.stop then some (r.start, { r with start := r.start + r.step }) else none instance : Stream Substring Char where next? s := if s.startPos < s.stopPos then some (s.str.get s.startPos, { s with startPos := s.str.next s.startPos }) else none
28fb582ecf2fb5bc196d1ca9fc9ca726ec985b25
592ee40978ac7604005a4e0d35bbc4b467389241
/Library/generated/mathscheme-lean/Carrier.lean
eebad3ee2d665670923d308d7aa39a6d448f83f5
[]
no_license
ysharoda/Deriving-Definitions
3e149e6641fae440badd35ac110a0bd705a49ad2
dfecb27572022de3d4aa702cae8db19957523a59
refs/heads/master
1,679,127,857,700
1,615,939,007,000
1,615,939,007,000
229,785,731
4
0
null
null
null
null
UTF-8
Lean
false
false
2,950
lean
import init.data.nat.basic import init.data.fin.basic import data.vector import .Prelude open Staged open nat open fin open vector section Carrier structure Carrier (A : Type) : Type open Carrier structure Sig (AS : Type) : Type structure Product (A : Type) : Type structure Hom {A1 : Type} {A2 : Type} (Ca1 : (Carrier A1)) (Ca2 : (Carrier A2)) : Type := (hom : (A1 → A2)) structure RelInterp {A1 : Type} {A2 : Type} (Ca1 : (Carrier A1)) (Ca2 : (Carrier A2)) : Type 1 := (interp : (A1 → (A2 → Type))) inductive CarrierTerm : Type open CarrierTerm inductive ClCarrierTerm (A : Type) : Type | sing : (A → ClCarrierTerm) open ClCarrierTerm inductive OpCarrierTerm (n : ℕ) : Type | v : ((fin n) → OpCarrierTerm) open OpCarrierTerm inductive OpCarrierTerm2 (n : ℕ) (A : Type) : Type | v2 : ((fin n) → OpCarrierTerm2) | sing2 : (A → OpCarrierTerm2) open OpCarrierTerm2 def simplifyCl {A : Type} : ((ClCarrierTerm A) → (ClCarrierTerm A)) | (sing x1) := (sing x1) def simplifyOpB {n : ℕ} : ((OpCarrierTerm n) → (OpCarrierTerm n)) | (v x1) := (v x1) def simplifyOp {n : ℕ} {A : Type} : ((OpCarrierTerm2 n A) → (OpCarrierTerm2 n A)) | (v2 x1) := (v2 x1) | (sing2 x1) := (sing2 x1) def evalCl {A : Type} : ((Carrier A) → ((ClCarrierTerm A) → A)) | Ca (sing x1) := x1 def evalOpB {A : Type} {n : ℕ} : ((Carrier A) → ((vector A n) → ((OpCarrierTerm n) → A))) | Ca vars (v x1) := (nth vars x1) def evalOp {A : Type} {n : ℕ} : ((Carrier A) → ((vector A n) → ((OpCarrierTerm2 n A) → A))) | Ca vars (v2 x1) := (nth vars x1) | Ca vars (sing2 x1) := x1 def inductionCl {A : Type} {P : ((ClCarrierTerm A) → Type)} : ((∀ (x1 : A) , (P (sing x1))) → (∀ (x : (ClCarrierTerm A)) , (P x))) | psing (sing x1) := (psing x1) def inductionOpB {n : ℕ} {P : ((OpCarrierTerm n) → Type)} : ((∀ (fin : (fin n)) , (P (v fin))) → (∀ (x : (OpCarrierTerm n)) , (P x))) | pv (v x1) := (pv x1) def inductionOp {n : ℕ} {A : Type} {P : ((OpCarrierTerm2 n A) → Type)} : ((∀ (fin : (fin n)) , (P (v2 fin))) → ((∀ (x1 : A) , (P (sing2 x1))) → (∀ (x : (OpCarrierTerm2 n A)) , (P x)))) | pv2 psing2 (v2 x1) := (pv2 x1) | pv2 psing2 (sing2 x1) := (psing2 x1) def stageCl {A : Type} : ((ClCarrierTerm A) → (Staged (ClCarrierTerm A))) | (sing x1) := (Now (sing x1)) def stageOpB {n : ℕ} : ((OpCarrierTerm n) → (Staged (OpCarrierTerm n))) | (v x1) := (const (code (v x1))) def stageOp {n : ℕ} {A : Type} : ((OpCarrierTerm2 n A) → (Staged (OpCarrierTerm2 n A))) | (sing2 x1) := (Now (sing2 x1)) | (v2 x1) := (const (code (v2 x1))) structure StagedRepr (A : Type) (Repr : (Type → Type)) : Type end Carrier
f117b401fbcbc801c6b00563291e2ea591a5e226
5ae26df177f810c5006841e9c73dc56e01b978d7
/src/ring_theory/algebra.lean
63693d21384fde37a0ea826c998067750f87bd01
[ "Apache-2.0" ]
permissive
ChrisHughes24/mathlib
98322577c460bc6b1fe5c21f42ce33ad1c3e5558
a2a867e827c2a6702beb9efc2b9282bd801d5f9a
refs/heads/master
1,583,848,251,477
1,565,164,247,000
1,565,164,247,000
129,409,993
0
1
Apache-2.0
1,565,164,817,000
1,523,628,059,000
Lean
UTF-8
Lean
false
false
19,826
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau Algebra over Commutative Ring (under category) -/ import data.polynomial data.mv_polynomial import data.complex.basic import linear_algebra.tensor_product import ring_theory.subring universes u v w u₁ v₁ open lattice local infix ` ⊗ `:100 := tensor_product /-- The category of R-algebras where R is a commutative ring is the under category R ↓ CRing. In the categorical setting we have a forgetful functor R-Alg ⥤ R-Mod. However here it extends module in order to preserve definitional equality in certain cases. -/ class algebra (R : Type u) (A : Type v) [comm_ring R] [ring A] extends module R A := (to_fun : R → A) [hom : is_ring_hom to_fun] (commutes' : ∀ r x, x * to_fun r = to_fun r * x) (smul_def' : ∀ r x, r • x = to_fun r * x) attribute [instance] algebra.hom def algebra_map {R : Type u} (A : Type v) [comm_ring R] [ring A] [algebra R A] (x : R) : A := algebra.to_fun A x namespace algebra variables {R : Type u} {S : Type v} {A : Type w} variables [comm_ring R] [comm_ring S] [ring A] [algebra R A] /-- The codomain of an algebra. -/ instance : module R A := infer_instance instance : has_scalar R A := infer_instance instance {F : Type u} {K : Type v} [discrete_field F] [ring K] [algebra F K] : vector_space F K := @vector_space.mk F _ _ _ algebra.module include R instance : is_ring_hom (algebra_map A : R → A) := algebra.hom _ A variables (A) @[simp] lemma map_add (r s : R) : algebra_map A (r + s) = algebra_map A r + algebra_map A s := is_ring_hom.map_add _ @[simp] lemma map_neg (r : R) : algebra_map A (-r) = -algebra_map A r := is_ring_hom.map_neg _ @[simp] lemma map_sub (r s : R) : algebra_map A (r - s) = algebra_map A r - algebra_map A s := is_ring_hom.map_sub _ @[simp] lemma map_mul (r s : R) : algebra_map A (r * s) = algebra_map A r * algebra_map A s := is_ring_hom.map_mul _ variables (R) @[simp] lemma map_zero : algebra_map A (0 : R) = 0 := is_ring_hom.map_zero _ @[simp] lemma map_one : algebra_map A (1 : R) = 1 := is_ring_hom.map_one _ variables {R A} /-- Creating an algebra from a morphism in CRing. -/ def of_ring_hom (i : R → S) (hom : is_ring_hom i) : algebra R S := { smul := λ c x, i c * x, smul_zero := λ x, mul_zero (i x), smul_add := λ r x y, mul_add (i r) x y, add_smul := λ r s x, show i (r + s) * x = _, by rw [hom.3, add_mul], mul_smul := λ r s x, show i (r * s) * x = _, by rw [hom.2, mul_assoc], one_smul := λ x, show i 1 * x = _, by rw [hom.1, one_mul], zero_smul := λ x, show i 0 * x = _, by rw [@@is_ring_hom.map_zero _ _ i hom, zero_mul], to_fun := i, commutes' := λ _ _, mul_comm _ _, smul_def' := λ c x, rfl } theorem smul_def (r : R) (x : A) : r • x = algebra_map A r * x := algebra.smul_def' r x theorem commutes (r : R) (x : A) : x * algebra_map A r = algebra_map A r * x := algebra.commutes' r x theorem left_comm (r : R) (x y : A) : x * (algebra_map A r * y) = algebra_map A r * (x * y) := by rw [← mul_assoc, commutes, mul_assoc] @[simp] lemma mul_smul_comm (s : R) (x y : A) : x * (s • y) = s • (x * y) := by rw [smul_def, smul_def, left_comm] @[simp] lemma smul_mul_assoc (r : R) (x y : A) : (r • x) * y = r • (x * y) := by rw [smul_def, smul_def, mul_assoc] /-- R[X] is the generator of the category R-Alg. -/ instance polynomial (R : Type u) [comm_ring R] [decidable_eq R] : algebra R (polynomial R) := { to_fun := polynomial.C, commutes' := λ _ _, mul_comm _ _, smul_def' := λ c p, (polynomial.C_mul' c p).symm, .. polynomial.module } /-- The algebra of multivariate polynomials. -/ instance mv_polynomial (R : Type u) [comm_ring R] [decidable_eq R] (ι : Type v) [decidable_eq ι] : algebra R (mv_polynomial ι R) := { to_fun := mv_polynomial.C, commutes' := λ _ _, mul_comm _ _, smul_def' := λ c p, (mv_polynomial.C_mul' c p).symm, .. mv_polynomial.module } /-- Creating an algebra from a subring. This is the dual of ring extension. -/ instance of_subring (S : set R) [is_subring S] : algebra S R := of_ring_hom subtype.val ⟨rfl, λ _ _, rfl, λ _ _, rfl⟩ variables (R A) /-- The multiplication in an algebra is a bilinear map. -/ def lmul : A →ₗ A →ₗ A := linear_map.mk₂ R (*) (λ x y z, add_mul x y z) (λ c x y, by rw [smul_def, smul_def, mul_assoc _ x y]) (λ x y z, mul_add x y z) (λ c x y, by rw [smul_def, smul_def, left_comm]) set_option class.instance_max_depth 39 def lmul_left (r : A) : A →ₗ A := lmul R A r def lmul_right (r : A) : A →ₗ A := (lmul R A).flip r variables {R A} @[simp] lemma lmul_apply (p q : A) : lmul R A p q = p * q := rfl @[simp] lemma lmul_left_apply (p q : A) : lmul_left R A p q = p * q := rfl @[simp] lemma lmul_right_apply (p q : A) : lmul_right R A p q = q * p := rfl end algebra /-- Defining the homomorphism in the category R-Alg. -/ structure alg_hom (R : Type u) (A : Type v) (B : Type w) [comm_ring R] [ring A] [ring B] [algebra R A] [algebra R B] := (to_fun : A → B) [hom : is_ring_hom to_fun] (commutes' : ∀ r : R, to_fun (algebra_map A r) = algebra_map B r) infixr ` →ₐ `:25 := alg_hom _ notation A ` →ₐ[`:25 R `] ` B := alg_hom R A B namespace alg_hom variables {R : Type u} {A : Type v} {B : Type w} {C : Type u₁} {D : Type v₁} variables [comm_ring R] [ring A] [ring B] [ring C] [ring D] variables [algebra R A] [algebra R B] [algebra R C] [algebra R D] include R instance : has_coe_to_fun (A →ₐ[R] B) := ⟨λ _, A → B, to_fun⟩ variables (φ : A →ₐ[R] B) instance : is_ring_hom ⇑φ := hom φ @[extensionality] theorem ext {φ₁ φ₂ : A →ₐ[R] B} (H : ∀ x, φ₁ x = φ₂ x) : φ₁ = φ₂ := by cases φ₁; cases φ₂; congr' 1; ext; apply H theorem commutes (r : R) : φ (algebra_map A r) = algebra_map B r := φ.commutes' r @[simp] lemma map_add (r s : A) : φ (r + s) = φ r + φ s := is_ring_hom.map_add _ @[simp] lemma map_zero : φ 0 = 0 := is_ring_hom.map_zero _ @[simp] lemma map_neg (x) : φ (-x) = -φ x := is_ring_hom.map_neg _ @[simp] lemma map_sub (x y) : φ (x - y) = φ x - φ y := is_ring_hom.map_sub _ @[simp] lemma map_mul (x y) : φ (x * y) = φ x * φ y := is_ring_hom.map_mul _ @[simp] lemma map_one : φ 1 = 1 := is_ring_hom.map_one _ /-- R-Alg ⥤ R-Mod -/ def to_linear_map : A →ₗ B := { to_fun := φ, add := φ.map_add, smul := λ c x, by rw [algebra.smul_def, φ.map_mul, φ.commutes c, algebra.smul_def] } @[simp] lemma to_linear_map_apply (p : A) : φ.to_linear_map p = φ p := rfl theorem to_linear_map_inj {φ₁ φ₂ : A →ₐ[R] B} (H : φ₁.to_linear_map = φ₂.to_linear_map) : φ₁ = φ₂ := ext $ λ x, show φ₁.to_linear_map x = φ₂.to_linear_map x, by rw H variables (R A) protected def id : A →ₐ[R] A := { to_fun := id, commutes' := λ _, rfl } variables {R A} @[simp] lemma id_to_linear_map : (alg_hom.id R A).to_linear_map = @linear_map.id R A _ _ _ := rfl @[simp] lemma id_apply (p : A) : alg_hom.id R A p = p := rfl def comp (φ₁ : B →ₐ[R] C) (φ₂ : A →ₐ[R] B) : A →ₐ C := { to_fun := φ₁ ∘ φ₂, commutes' := λ r, by rw [function.comp_apply, φ₂.commutes, φ₁.commutes] } @[simp] lemma comp_to_linear_map (f : A →ₐ[R] B) (g : B →ₐ[R] C) : (g.comp f).to_linear_map = g.to_linear_map.comp f.to_linear_map := rfl @[simp] lemma comp_apply (φ₁ : B →ₐ[R] C) (φ₂ : A →ₐ[R] B) (p : A) : φ₁.comp φ₂ p = φ₁ (φ₂ p) := rfl @[simp] theorem comp_id : φ.comp (alg_hom.id R A) = φ := ext $ λ x, rfl @[simp] theorem id_comp : (alg_hom.id R B).comp φ = φ := ext $ λ x, rfl theorem comp_assoc (φ₁ : C →ₐ[R] D) (φ₂ : B →ₐ[R] C) (φ₃ : A →ₐ[R] B) : (φ₁.comp φ₂).comp φ₃ = φ₁.comp (φ₂.comp φ₃) := ext $ λ x, rfl end alg_hom namespace algebra variables (R : Type u) (S : Type v) (A : Type w) variables [comm_ring R] [comm_ring S] [ring A] [algebra R S] [algebra S A] include R S A def comap : Type w := A def comap.to_comap : A → comap R S A := id def comap.of_comap : comap R S A → A := id omit R S A instance comap.ring : ring (comap R S A) := _inst_3 instance comap.comm_ring (R : Type u) (S : Type v) (A : Type w) [comm_ring R] [comm_ring S] [comm_ring A] [algebra R S] [algebra S A] : comm_ring (comap R S A) := _inst_8 instance comap.module : module S (comap R S A) := _inst_5.to_module instance comap.has_scalar : has_scalar S (comap R S A) := _inst_5.to_module.to_has_scalar set_option class.instance_max_depth 40 /-- R ⟶ S induces S-Alg ⥤ R-Alg -/ instance comap.algebra : algebra R (comap R S A) := { smul := λ r x, (algebra_map S r • x : A), smul_add := λ _ _ _, smul_add _ _ _, add_smul := λ _ _ _, by simp only [algebra.map_add]; from add_smul _ _ _, mul_smul := λ _ _ _, by simp only [algebra.map_mul]; from mul_smul _ _ _, one_smul := λ _, by simp only [algebra.map_one]; from one_smul _ _, zero_smul := λ _, by simp only [algebra.map_zero]; from zero_smul _ _, smul_zero := λ _, smul_zero _, to_fun := (algebra_map A : S → A) ∘ algebra_map S, hom := by letI : is_ring_hom (algebra_map A) := _inst_5.hom; apply_instance, commutes' := λ r x, algebra.commutes _ _, smul_def' := λ _ _, algebra.smul_def _ _ } def to_comap : S →ₐ[R] comap R S A := { to_fun := (algebra_map A : S → A), hom := _inst_5.hom, commutes' := λ r, rfl } theorem to_comap_apply (x) : to_comap R S A x = (algebra_map A : S → A) x := rfl end algebra namespace alg_hom variables {R : Type u} {S : Type v} {A : Type w} {B : Type u₁} variables [comm_ring R] [comm_ring S] [ring A] [ring B] variables [algebra R S] [algebra S A] [algebra S B] (φ : A →ₐ[S] B) include R /-- R ⟶ S induces S-Alg ⥤ R-Alg -/ def comap : algebra.comap R S A →ₐ[R] algebra.comap R S B := { to_fun := φ, hom := alg_hom.is_ring_hom _, commutes' := λ r, φ.commutes (algebra_map S r) } end alg_hom namespace polynomial variables (R : Type u) (A : Type v) variables [comm_ring R] [comm_ring A] [algebra R A] variables [decidable_eq R] (x : A) /-- A → Hom[R-Alg](R[X],A) -/ def aeval : polynomial R →ₐ[R] A := { to_fun := eval₂ (algebra_map A) x, hom := ⟨eval₂_one _ x, λ _ _, eval₂_mul _ x, λ _ _, eval₂_add _ x⟩, commutes' := λ r, eval₂_C _ _ } theorem aeval_def (p : polynomial R) : aeval R A x p = eval₂ (algebra_map A) x p := rfl instance aeval.is_ring_hom : is_ring_hom (aeval R A x) := alg_hom.hom _ theorem eval_unique (φ : polynomial R →ₐ[R] A) (p) : φ p = eval₂ (algebra_map A) (φ X) p := begin apply polynomial.induction_on p, { intro r, rw eval₂_C, exact φ.commutes r }, { intros f g ih1 ih2, rw [is_ring_hom.map_add φ, ih1, ih2, eval₂_add] }, { intros n r ih, rw [pow_succ', ← mul_assoc, is_ring_hom.map_mul φ, eval₂_mul (algebra_map A : R → A), eval₂_X, ih] } end end polynomial namespace mv_polynomial variables (R : Type u) (A : Type v) variables [comm_ring R] [comm_ring A] [algebra R A] variables [decidable_eq R] [decidable_eq A] (σ : set A) /-- (ι → A) → Hom[R-Alg](R[ι],A) -/ def aeval : mv_polynomial σ R →ₐ[R] A := { to_fun := eval₂ (algebra_map A) subtype.val, hom := ⟨eval₂_one _ _, λ _ _, eval₂_mul _ _, λ _ _, eval₂_add _ _⟩, commutes' := λ r, eval₂_C _ _ _ } theorem aeval_def (p : mv_polynomial σ R) : aeval R A σ p = eval₂ (algebra_map A) subtype.val p := rfl instance aeval.is_ring_hom : is_ring_hom (aeval R A σ) := alg_hom.hom _ variables (ι : Type w) [decidable_eq ι] theorem eval_unique (φ : mv_polynomial ι R →ₐ[R] A) (p) : φ p = eval₂ (algebra_map A) (φ ∘ X) p := begin apply mv_polynomial.induction_on p, { intro r, rw eval₂_C, exact φ.commutes r }, { intros f g ih1 ih2, rw [is_ring_hom.map_add φ, ih1, ih2, eval₂_add] }, { intros p j ih, rw [is_ring_hom.map_mul φ, eval₂_mul, eval₂_X, ih] } end end mv_polynomial namespace complex instance algebra_over_reals : algebra ℝ ℂ := algebra.of_ring_hom coe $ by constructor; intros; simp [one_re] instance : has_scalar ℝ ℂ := { smul := λ r c, ↑r * c} end complex structure subalgebra (R : Type u) (A : Type v) [comm_ring R] [ring A] [algebra R A] : Type v := (carrier : set A) [subring : is_subring carrier] (range_le : set.range (algebra_map A : R → A) ≤ carrier) attribute [instance] subalgebra.subring namespace subalgebra variables {R : Type u} {A : Type v} variables [comm_ring R] [ring A] [algebra R A] include R instance : has_coe (subalgebra R A) (set A) := ⟨λ S, S.carrier⟩ instance : has_mem A (subalgebra R A) := ⟨λ x S, x ∈ S.carrier⟩ variables {A} theorem mem_coe {x : A} {s : subalgebra R A} : x ∈ (s : set A) ↔ x ∈ s := iff.rfl @[extensionality] theorem ext {S T : subalgebra R A} (h : ∀ x : A, x ∈ S ↔ x ∈ T) : S = T := by cases S; cases T; congr; ext x; exact h x variables (S : subalgebra R A) instance : is_subring (S : set A) := S.subring instance : ring S := @@subtype.ring _ S.is_subring instance (R : Type u) (A : Type v) [comm_ring R] [comm_ring A] [algebra R A] (S : subalgebra R A) : comm_ring S := @@subtype.comm_ring _ S.is_subring instance algebra : algebra R S := { smul := λ (c:R) x, ⟨c • x.1, by rw algebra.smul_def; exact @@is_submonoid.mul_mem _ S.2.2 (S.3 ⟨c, rfl⟩) x.2⟩, smul_add := λ c x y, subtype.eq $ smul_add _ _ _, add_smul := λ c x y, subtype.eq $ add_smul _ _ _, mul_smul := λ c x y, subtype.eq $ mul_smul _ _ _, one_smul := λ x, subtype.eq $ one_smul _ _, zero_smul := λ x, subtype.eq $ zero_smul _ _, smul_zero := λ x, subtype.eq $ smul_zero _, to_fun := λ r, ⟨algebra_map A r, S.range_le ⟨r, rfl⟩⟩, hom := ⟨subtype.eq $ algebra.map_one R A, λ x y, subtype.eq $ algebra.map_mul A x y, λ x y, subtype.eq $ algebra.map_add A x y⟩, commutes' := λ c x, subtype.eq $ by apply _inst_3.4, smul_def' := λ c x, subtype.eq $ by apply _inst_3.5 } instance to_algebra (R : Type u) (A : Type v) [comm_ring R] [comm_ring A] [algebra R A] (S : subalgebra R A) : algebra S A := algebra.of_subring _ def val : S →ₐ[R] A := { to_fun := subtype.val, hom := ⟨rfl, λ _ _, rfl, λ _ _, rfl⟩, commutes' := λ r, rfl } def to_submodule : submodule R A := { carrier := S.carrier, zero := (0:S).2, add := λ x y hx hy, (⟨x, hx⟩ + ⟨y, hy⟩ : S).2, smul := λ c x hx, (algebra.smul_def c x).symm ▸ (⟨algebra_map A c, S.range_le ⟨c, rfl⟩⟩ * ⟨x, hx⟩:S).2 } instance coe_to_submodule : has_coe (subalgebra R A) (submodule R A) := ⟨to_submodule⟩ instance to_submodule.is_subring : is_subring ((S : submodule R A) : set A) := S.2 instance : partial_order (subalgebra R A) := { le := λ S T, S.carrier ≤ T.carrier, le_refl := λ _, le_refl _, le_trans := λ _ _ _, le_trans, le_antisymm := λ S T hst hts, ext $ λ x, ⟨@hst x, @hts x⟩ } def comap {R : Type u} {S : Type v} {A : Type w} [comm_ring R] [comm_ring S] [ring A] [algebra R S] [algebra S A] (iSB : subalgebra S A) : subalgebra R (algebra.comap R S A) := { carrier := (iSB : set A), subring := iSB.is_subring, range_le := λ a ⟨r, hr⟩, hr ▸ iSB.range_le ⟨_, rfl⟩ } set_option class.instance_max_depth 48 def under {R : Type u} {A : Type v} [comm_ring R] [comm_ring A] {i : algebra R A} (S : subalgebra R A) (T : subalgebra S A) : subalgebra R A := { carrier := T, range_le := (λ a ⟨r, hr⟩, hr ▸ T.range_le ⟨⟨algebra_map A r, S.range_le ⟨r, rfl⟩⟩, rfl⟩) } end subalgebra namespace alg_hom variables {R : Type u} {A : Type v} {B : Type w} variables [comm_ring R] [ring A] [ring B] [algebra R A] [algebra R B] variables (φ : A →ₐ[R] B) protected def range : subalgebra R B := { carrier := set.range φ, subring := { one_mem := ⟨1, φ.map_one⟩, mul_mem := λ y₁ y₂ ⟨x₁, hx₁⟩ ⟨x₂, hx₂⟩, ⟨x₁ * x₂, hx₁ ▸ hx₂ ▸ φ.map_mul x₁ x₂⟩ }, range_le := λ y ⟨r, hr⟩, ⟨algebra_map A r, hr ▸ φ.commutes r⟩ } end alg_hom namespace algebra variables {R : Type u} (A : Type v) variables [comm_ring R] [ring A] [algebra R A] include R variables (R) instance id : algebra R R := algebra.of_ring_hom id $ by apply_instance def of_id : R →ₐ A := { to_fun := algebra_map A, commutes' := λ _, rfl } variables {R} theorem of_id_apply (r) : of_id R A r = algebra_map A r := rfl variables (R) {A} def adjoin (s : set A) : subalgebra R A := { carrier := ring.closure (set.range (algebra_map A : R → A) ∪ s), range_le := le_trans (set.subset_union_left _ _) ring.subset_closure } variables {R} protected def gc : galois_connection (adjoin R : set A → subalgebra R A) coe := λ s S, ⟨λ H, le_trans (le_trans (set.subset_union_right _ _) ring.subset_closure) H, λ H, ring.closure_subset $ set.union_subset S.range_le H⟩ protected def gi : galois_insertion (adjoin R : set A → subalgebra R A) coe := { choice := λ s hs, adjoin R s, gc := algebra.gc, le_l_u := λ S, (algebra.gc (S : set A) (adjoin R S)).1 $ le_refl _, choice_eq := λ _ _, rfl } instance : complete_lattice (subalgebra R A) := galois_insertion.lift_complete_lattice algebra.gi theorem mem_bot {x : A} : x ∈ (⊥ : subalgebra R A) ↔ x ∈ set.range (algebra_map A : R → A) := suffices (⊥ : subalgebra R A) = (of_id R A).range, by rw this; refl, le_antisymm bot_le $ subalgebra.range_le _ theorem mem_top {x : A} : x ∈ (⊤ : subalgebra R A) := ring.mem_closure $ or.inr trivial def to_top : A →ₐ[R] (⊤ : subalgebra R A) := { to_fun := λ x, ⟨x, mem_top⟩, hom := ⟨rfl, λ _ _, rfl, λ _ _, rfl⟩, commutes' := λ _, rfl } end algebra section int variables (R : Type*) [comm_ring R] /-- CRing ⥤ ℤ-Alg -/ def alg_hom_int {R : Type u} [comm_ring R] [algebra ℤ R] {S : Type v} [comm_ring S] [algebra ℤ S] (f : R → S) [is_ring_hom f] : R →ₐ[ℤ] S := { to_fun := f, hom := by apply_instance, commutes' := λ i, int.induction_on i (by rw [algebra.map_zero, algebra.map_zero, is_ring_hom.map_zero f]) (λ i ih, by rw [algebra.map_add, algebra.map_add, algebra.map_one, algebra.map_one]; rw [is_ring_hom.map_add f, is_ring_hom.map_one f, ih]) (λ i ih, by rw [algebra.map_sub, algebra.map_sub, algebra.map_one, algebra.map_one]; rw [is_ring_hom.map_sub f, is_ring_hom.map_one f, ih]) } /-- CRing ⥤ ℤ-Alg -/ instance algebra_int : algebra ℤ R := algebra.of_ring_hom coe $ by constructor; intros; simp variables {R} /-- CRing ⥤ ℤ-Alg -/ def subalgebra_of_subring (S : set R) [is_subring S] : subalgebra ℤ R := { carrier := S, range_le := λ x ⟨i, h⟩, h ▸ int.induction_on i (by rw algebra.map_zero; exact is_add_submonoid.zero_mem _) (λ i hi, by rw [algebra.map_add, algebra.map_one]; exact is_add_submonoid.add_mem hi (is_submonoid.one_mem _)) (λ i hi, by rw [algebra.map_sub, algebra.map_one]; exact is_add_subgroup.sub_mem _ _ _ hi (is_submonoid.one_mem _)) } @[simp] lemma mem_subalgebra_of_subring {x : R} {S : set R} [is_subring S] : x ∈ subalgebra_of_subring S ↔ x ∈ S := iff.rfl section span_int open submodule lemma span_int_eq_add_group_closure (s : set R) : ↑(span ℤ s) = add_group.closure s := set.subset.antisymm (λ x hx, span_induction hx (λ _, add_group.mem_closure) (is_add_submonoid.zero_mem _) (λ a b ha hb, is_add_submonoid.add_mem ha hb) (λ n a ha, by { erw [show n • a = gsmul n a, from (gsmul_eq_mul a n).symm], exact is_add_subgroup.gsmul_mem ha})) (add_group.closure_subset subset_span) @[simp] lemma span_int_eq (s : set R) [is_add_subgroup s] : (↑(span ℤ s) : set R) = s := by rw [span_int_eq_add_group_closure, add_group.closure_add_subgroup] end span_int end int
bde292321318c7fb2446da9f544af36f7bde703e
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/linear_algebra/sesquilinear_form.lean
2416142748070d7604436c5c047a432ce658f670
[ "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
11,063
lean
/- Copyright (c) 2018 Andreas Swerdlow. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Andreas Swerdlow -/ import algebra.module.linear_map import linear_algebra.bilinear_map import linear_algebra.matrix.basis /-! # Sesquilinear form This files provides properties about sesquilinear forms. The maps considered are of the form `M₁ →ₛₗ[I₁] M₂ →ₛₗ[I₂] R`, where `I₁ : R₁ →+* R` and `I₂ : R₂ →+* R` are ring homomorphisms and `M₁` is a module over `R₁` and `M₂` is a module over `R₂`. Sesquilinear forms are the special case that `M₁ = M₂`, `R₁ = R₂ = R`, and `I₁ = ring_hom.id R`. Taking additionally `I₂ = ring_hom.id R`, then one obtains bilinear forms. These forms are a special case of the bilinear maps defined in `bilinear_map.lean` and all basic lemmas about construction and elementary calculations are found there. ## Main declarations * `is_ortho`: states that two vectors are orthogonal with respect to a sesquilinear form * `is_symm`, `is_alt`: states that a sesquilinear form is symmetric and alternating, respectively * `orthogonal_bilin`: provides the orthogonal complement with respect to sesquilinear form ## References * <https://en.wikipedia.org/wiki/Sesquilinear_form#Over_arbitrary_rings> ## Tags Sesquilinear form, -/ open_locale big_operators variables {R R₁ R₂ R₃ M M₁ M₂ K K₁ K₂ V V₁ V₂ n: Type*} namespace linear_map /-! ### Orthogonal vectors -/ section comm_ring -- the `ₗ` subscript variables are for special cases about linear (as opposed to semilinear) maps variables [comm_semiring R] [comm_semiring R₁] [add_comm_monoid M₁] [module R₁ M₁] [comm_semiring R₂] [add_comm_monoid M₂] [module R₂ M₂] {I₁ : R₁ →+* R} {I₂ : R₂ →+* R} {I₁' : R₁ →+* R} /-- The proposition that two elements of a sesquilinear form space are orthogonal -/ def is_ortho (B : M₁ →ₛₗ[I₁] M₂ →ₛₗ[I₂] R) (x y) : Prop := B x y = 0 lemma is_ortho_def {B : M₁ →ₛₗ[I₁] M₂ →ₛₗ[I₂] R} {x y} : B.is_ortho x y ↔ B x y = 0 := iff.rfl lemma is_ortho_zero_left (B : M₁ →ₛₗ[I₁] M₂ →ₛₗ[I₂] R) (x) : is_ortho B (0 : M₁) x := by { dunfold is_ortho, rw [ map_zero B, zero_apply] } lemma is_ortho_zero_right (B : M₁ →ₛₗ[I₁] M₂ →ₛₗ[I₂] R) (x) : is_ortho B x (0 : M₂) := map_zero (B x) /-- A set of vectors `v` is orthogonal with respect to some bilinear form `B` if and only if for all `i ≠ j`, `B (v i) (v j) = 0`. For orthogonality between two elements, use `bilin_form.is_ortho` -/ def is_Ortho {n : Type*} (B : M₁ →ₛₗ[I₁] M₁ →ₛₗ[I₁'] R) (v : n → M₁) : Prop := pairwise (B.is_ortho on v) lemma is_Ortho_def {n : Type*} {B : M₁ →ₛₗ[I₁] M₁ →ₛₗ[I₁'] R} {v : n → M₁} : B.is_Ortho v ↔ ∀ i j : n, i ≠ j → B (v i) (v j) = 0 := iff.rfl end comm_ring section field variables [field K] [field K₁] [add_comm_group V₁] [module K₁ V₁] [field K₂] [add_comm_group V₂] [module K₂ V₂] {I₁ : K₁ →+* K} {I₂ : K₂ →+* K} {I₁' : K₁ →+* K} {J₁ : K →+* K} {J₂ : K →+* K} -- todo: this also holds for [comm_ring R] [is_domain R] when J₁ is invertible lemma ortho_smul_left {B : V₁ →ₛₗ[I₁] V₂ →ₛₗ[I₂] K} {x y} {a : K₁} (ha : a ≠ 0) : (is_ortho B x y) ↔ (is_ortho B (a • x) y) := begin dunfold is_ortho, split; intro H, { rw [map_smulₛₗ₂, H, smul_zero]}, { rw [map_smulₛₗ₂, smul_eq_zero] at H, cases H, { rw I₁.map_eq_zero at H, trivial }, { exact H }} end -- todo: this also holds for [comm_ring R] [is_domain R] when J₂ is invertible lemma ortho_smul_right {B : V₁ →ₛₗ[I₁] V₂ →ₛₗ[I₂] K} {x y} {a : K₂} {ha : a ≠ 0} : (is_ortho B x y) ↔ (is_ortho B x (a • y)) := begin dunfold is_ortho, split; intro H, { rw [map_smulₛₗ, H, smul_zero] }, { rw [map_smulₛₗ, smul_eq_zero] at H, cases H, { simp at H, exfalso, exact ha H }, { exact H }} end /-- A set of orthogonal vectors `v` with respect to some sesquilinear form `B` is linearly independent if for all `i`, `B (v i) (v i) ≠ 0`. -/ lemma linear_independent_of_is_Ortho {B : V₁ →ₛₗ[I₁] V₁ →ₛₗ[I₁'] K} {v : n → V₁} (hv₁ : B.is_Ortho v) (hv₂ : ∀ i, ¬ B.is_ortho (v i) (v i)) : linear_independent K₁ v := begin classical, rw linear_independent_iff', intros s w hs i hi, have : B (s.sum $ λ (i : n), w i • v i) (v i) = 0, { rw [hs, map_zero, zero_apply] }, have hsum : s.sum (λ (j : n), I₁(w j) * B (v j) (v i)) = I₁(w i) * B (v i) (v i), { apply finset.sum_eq_single_of_mem i hi, intros j hj hij, rw [is_Ortho_def.1 hv₁ _ _ hij, mul_zero], }, simp_rw [B.map_sum₂, map_smulₛₗ₂, smul_eq_mul, hsum] at this, apply I₁.map_eq_zero.mp, exact eq_zero_of_ne_zero_of_mul_right_eq_zero (hv₂ i) this, end end field variables [comm_ring R] [add_comm_group M] [module R M] [comm_ring R₁] [add_comm_group M₁] [module R₁ M₁] {I : R →+* R} {I₁ : R₁ →+* R} {I₂ : R₁ →+* R} {B : M₁ →ₛₗ[I₁] M₁ →ₛₗ[I₂] R} {B' : M →ₗ[R] M →ₛₗ[I] R} /-! ### Reflexive bilinear forms -/ /-- The proposition that a sesquilinear form is reflexive -/ def is_refl (B : M₁ →ₛₗ[I₁] M₁ →ₛₗ[I₂] R) : Prop := ∀ (x y), B x y = 0 → B y x = 0 namespace is_refl variable (H : B.is_refl) lemma eq_zero : ∀ {x y}, B x y = 0 → B y x = 0 := λ x y, H x y lemma ortho_comm {x y} : is_ortho B x y ↔ is_ortho B y x := ⟨eq_zero H, eq_zero H⟩ end is_refl /-! ### Symmetric bilinear forms -/ /-- The proposition that a sesquilinear form is symmetric -/ def is_symm (B : M →ₗ[R] M →ₛₗ[I] R) : Prop := ∀ (x y), I (B x y) = B y x namespace is_symm variable (H : B'.is_symm) include H protected lemma eq (x y) : (I (B' x y)) = B' y x := H x y lemma is_refl : B'.is_refl := λ x y H1, by { rw [←H], simp [H1] } lemma ortho_comm {x y} : is_ortho B' x y ↔ is_ortho B' y x := H.is_refl.ortho_comm end is_symm /-! ### Alternating bilinear forms -/ /-- The proposition that a sesquilinear form is alternating -/ def is_alt (B : M₁ →ₛₗ[I₁] M₁ →ₛₗ[I₂] R) : Prop := ∀ x, B x x = 0 namespace is_alt variable (H : B.is_alt) include H lemma self_eq_zero (x) : B x x = 0 := H x lemma neg (x y) : - B x y = B y x := begin have H1 : B (y + x) (y + x) = 0, { exact self_eq_zero H (y + x) }, simp [map_add, self_eq_zero H] at H1, rw [add_eq_zero_iff_neg_eq] at H1, exact H1, end lemma is_refl : B.is_refl := begin intros x y h, rw [←neg H, h, neg_zero], end lemma ortho_comm {x y} : is_ortho B x y ↔ is_ortho B y x := H.is_refl.ortho_comm end is_alt end linear_map namespace submodule /-! ### The orthogonal complement -/ variables [comm_ring R] [comm_ring R₁] [add_comm_group M₁] [module R₁ M₁] {I₁ : R₁ →+* R} {I₂ : R₁ →+* R} {B : M₁ →ₛₗ[I₁] M₁ →ₛₗ[I₂] R} /-- The orthogonal complement of a submodule `N` with respect to some bilinear form is the set of elements `x` which are orthogonal to all elements of `N`; i.e., for all `y` in `N`, `B x y = 0`. Note that for general (neither symmetric nor antisymmetric) bilinear forms this definition has a chirality; in addition to this "left" orthogonal complement one could define a "right" orthogonal complement for which, for all `y` in `N`, `B y x = 0`. This variant definition is not currently provided in mathlib. -/ def orthogonal_bilin (N : submodule R₁ M₁) (B : M₁ →ₛₗ[I₁] M₁ →ₛₗ[I₂] R) : submodule R₁ M₁ := { carrier := { m | ∀ n ∈ N, B.is_ortho n m }, zero_mem' := λ x _, B.is_ortho_zero_right x, add_mem' := λ x y hx hy n hn, by rw [linear_map.is_ortho, map_add, show B n x = 0, by exact hx n hn, show B n y = 0, by exact hy n hn, zero_add], smul_mem' := λ c x hx n hn, by rw [linear_map.is_ortho, linear_map.map_smulₛₗ, show B n x = 0, by exact hx n hn, smul_zero] } variables {N L : submodule R₁ M₁} @[simp] lemma mem_orthogonal_bilin_iff {m : M₁} : m ∈ N.orthogonal_bilin B ↔ ∀ n ∈ N, B.is_ortho n m := iff.rfl lemma orthogonal_bilin_le (h : N ≤ L) : L.orthogonal_bilin B ≤ N.orthogonal_bilin B := λ _ hn l hl, hn l (h hl) lemma le_orthogonal_bilin_orthogonal_bilin (b : B.is_refl) : N ≤ (N.orthogonal_bilin B).orthogonal_bilin B := λ n hn m hm, b _ _ (hm n hn) end submodule namespace linear_map section orthogonal variables [field K] [add_comm_group V] [module K V] [field K₁] [add_comm_group V₁] [module K₁ V₁] {J : K →+* K} {J₁ : K₁ →+* K} {J₁' : K₁ →+* K} -- ↓ This lemma only applies in fields as we require `a * b = 0 → a = 0 ∨ b = 0` lemma span_singleton_inf_orthogonal_eq_bot (B : V₁ →ₛₗ[J₁] V₁ →ₛₗ[J₁'] K) (x : V₁) (hx : ¬ B.is_ortho x x) : (K₁ ∙ x) ⊓ submodule.orthogonal_bilin (K₁ ∙ x) B = ⊥ := begin rw ← finset.coe_singleton, refine eq_bot_iff.2 (λ y h, _), rcases mem_span_finset.1 h.1 with ⟨μ, rfl⟩, have := h.2 x _, { rw finset.sum_singleton at this ⊢, suffices hμzero : μ x = 0, { rw [hμzero, zero_smul, submodule.mem_bot] }, change B x (μ x • x) = 0 at this, rw [map_smulₛₗ, smul_eq_mul] at this, exact or.elim (zero_eq_mul.mp this.symm) (λ y, by { simp at y, exact y }) (λ hfalse, false.elim $ hx hfalse) }, { rw submodule.mem_span; exact λ _ hp, hp $ finset.mem_singleton_self _ } end -- ↓ This lemma only applies in fields since we use the `mul_eq_zero` lemma orthogonal_span_singleton_eq_to_lin_ker {B : V →ₗ[K] V →ₛₗ[J] K} (x : V) : submodule.orthogonal_bilin (K ∙ x) B = (B x).ker := begin ext y, simp_rw [submodule.mem_orthogonal_bilin_iff, linear_map.mem_ker, submodule.mem_span_singleton ], split, { exact λ h, h x ⟨1, one_smul _ _⟩ }, { rintro h _ ⟨z, rfl⟩, rw [is_ortho, map_smulₛₗ₂, smul_eq_zero], exact or.intro_right _ h } end -- todo: Generalize this to sesquilinear maps lemma span_singleton_sup_orthogonal_eq_top {B : V →ₗ[K] V →ₗ[K] K} {x : V} (hx : ¬ B.is_ortho x x) : (K ∙ x) ⊔ submodule.orthogonal_bilin (K ∙ x) B = ⊤ := begin rw orthogonal_span_singleton_eq_to_lin_ker, exact (B x).span_singleton_sup_ker_eq_top hx, end -- todo: Generalize this to sesquilinear maps /-- Given a bilinear form `B` and some `x` such that `B x x ≠ 0`, the span of the singleton of `x` is complement to its orthogonal complement. -/ lemma is_compl_span_singleton_orthogonal {B : V →ₗ[K] V →ₗ[K] K} {x : V} (hx : ¬ B.is_ortho x x) : is_compl (K ∙ x) (submodule.orthogonal_bilin (K ∙ x) B) := { inf_le_bot := eq_bot_iff.1 $ (span_singleton_inf_orthogonal_eq_bot B x hx), top_le_sup := eq_top_iff.1 $ span_singleton_sup_orthogonal_eq_top hx } end orthogonal end linear_map
999b6907ba4a89263123c448cc0f49692aac9b0d
35b83be3126daae10419b573c55e1fed009d3ae8
/_target/deps/mathlib/data/equiv/algebra.lean
81c51d84080814f987b16ccd41d897276d170a10
[]
no_license
AHassan1024/Lean_Playground
ccb25b72029d199c0d23d002db2d32a9f2689ebc
a00b004c3a2eb9e3e863c361aa2b115260472414
refs/heads/master
1,586,221,905,125
1,544,951,310,000
1,544,951,310,000
157,934,290
0
0
null
null
null
null
UTF-8
Lean
false
false
1,555
lean
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl -/ import data.equiv.basic algebra.group namespace equiv variables {α : Type*} [group α] protected def mul_left (a : α) : α ≃ α := { to_fun := λx, a * x, inv_fun := λx, a⁻¹ * x, left_inv := assume x, show a⁻¹ * (a * x) = x, from inv_mul_cancel_left a x, right_inv := assume x, show a * (a⁻¹ * x) = x, from mul_inv_cancel_left a x } attribute [to_additive equiv.add_left._proof_1] equiv.mul_left._proof_1 attribute [to_additive equiv.add_left._proof_2] equiv.mul_left._proof_2 attribute [to_additive equiv.add_left] equiv.mul_left protected def mul_right (a : α) : α ≃ α := { to_fun := λx, x * a, inv_fun := λx, x * a⁻¹, left_inv := assume x, show (x * a) * a⁻¹ = x, from mul_inv_cancel_right x a, right_inv := assume x, show (x * a⁻¹) * a = x, from inv_mul_cancel_right x a } attribute [to_additive equiv.add_right._proof_1] equiv.mul_right._proof_1 attribute [to_additive equiv.add_right._proof_2] equiv.mul_right._proof_2 attribute [to_additive equiv.add_right] equiv.mul_right protected def inv (α) [group α] : α ≃ α := { to_fun := λa, a⁻¹, inv_fun := λa, a⁻¹, left_inv := assume a, inv_inv a, right_inv := assume a, inv_inv a } attribute [to_additive equiv.neg._proof_1] equiv.inv._proof_1 attribute [to_additive equiv.neg._proof_2] equiv.inv._proof_2 attribute [to_additive equiv.neg] equiv.inv end equiv
7c92618a5d0740f16737bb62cf978e105530a838
9028d228ac200bbefe3a711342514dd4e4458bff
/src/computability/tm_to_partrec.lean
972f3effcd50d670dee6af39b51b84d3d7d826c4
[ "Apache-2.0" ]
permissive
mcncm/mathlib
8d25099344d9d2bee62822cb9ed43aa3e09fa05e
fde3d78cadeec5ef827b16ae55664ef115e66f57
refs/heads/master
1,672,743,316,277
1,602,618,514,000
1,602,618,514,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
64,504
lean
/- Copyright (c) 2020 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro -/ import computability.halting import computability.turing_machine import data.num.lemmas /-! # Modelling partial recursive functions using Turing machines This file defines a simplified basis for partial recursive functions, and a `turing.TM2` model Turing machine for evaluating these functions. This amounts to a constructive proof that every `partrec` function can be evaluated by a Turing machine. ## Main definitions * `to_partrec.code`: a simplified basis for partial recursive functions, valued in `list ℕ →. list ℕ`. * `to_partrec.code.eval`: semantics for a `to_partrec.code` program * `partrec_to_TM2.tr`: A TM2 turing machine which can evaluate `code` programs -/ open function (update) open relation namespace turing /-! ## A simplified basis for partrec This section constructs the type `code`, which is a data type of programs with `list ℕ` input and output, with enough expressivity to write any partial recursive function. The primitives are: * `zero'` appends a `0` to the input. That is, `zero' v = 0 :: v`. * `succ` returns the successor of the head of the input, defaulting to zero if there is no head: * `succ [] = [1]` * `succ (n :: v) = [n + 1]` * `tail` returns the tail of the input * `tail [] = []` * `tail (n :: v) = v` * `cons f fs` calls `f` and `fs` on the input and conses the results: * `cons f fs v = (f v).head :: fs v` * `comp f g` calls `f` on the output of `g`: * `comp f g v = f (g v)` * `case f g` cases on the head of the input, calling `f` or `g` depending on whether it is zero or a successor (similar to `nat.cases_on`). * `case f g [] = f []` * `case f g (0 :: v) = f v` * `case f g (n+1 :: v) = g (n :: v)` * `fix f` calls `f` repeatedly, using the head of the result of `f` to decide whether to call `f` again or finish: * `fix f v = []` if `f v = []` * `fix f v = w` if `f v = 0 :: w` * `fix f v = fix f w` if `f v = n+1 :: w` (the exact value of `n` is discarded) This basis is convenient because it is closer to the Turing machine model - the key operations are splitting and merging of lists of unknown length, while the messy `n`-ary composition operation from the traditional basis for partial recursive functions is absent - but it retains a compositional semantics. The first step in transitioning to Turing machines is to make a sequential evaluator for this basis, which we take up in the next section. -/ namespace to_partrec /-- The type of codes for primitive recursive functions. Unlike `nat.partrec.code`, this uses a set of operations on `list ℕ`. See `code.eval` for a description of the behavior of the primitives. -/ @[derive inhabited] inductive code | zero' | succ | tail | cons : code → code → code | comp : code → code → code | case : code → code → code | fix : code → code /-- The semantics of the `code` primitives, as partial functions `list ℕ →. list ℕ`. By convention we functions that return a single result return a singleton `[n]`, or in some cases `n :: v` where `v` will be ignored by a subsequent function. * `zero'` appends a `0` to the input. That is, `zero' v = 0 :: v`. * `succ` returns the successor of the head of the input, defaulting to zero if there is no head: * `succ [] = [1]` * `succ (n :: v) = [n + 1]` * `tail` returns the tail of the input * `tail [] = []` * `tail (n :: v) = v` * `cons f fs` calls `f` and `fs` on the input and conses the results: * `cons f fs v = (f v).head :: fs v` * `comp f g` calls `f` on the output of `g`: * `comp f g v = f (g v)` * `case f g` cases on the head of the input, calling `f` or `g` depending on whether it is zero or a successor (similar to `nat.cases_on`). * `case f g [] = f []` * `case f g (0 :: v) = f v` * `case f g (n+1 :: v) = g (n :: v)` * `fix f` calls `f` repeatedly, using the head of the result of `f` to decide whether to call `f` again or finish: * `fix f v = []` if `f v = []` * `fix f v = w` if `f v = 0 :: w` * `fix f v = fix f w` if `f v = n+1 :: w` (the exact value of `n` is discarded) -/ @[simp] def code.eval : code → list ℕ →. list ℕ | code.zero' := λ v, pure (0 :: v) | code.succ := λ v, pure [v.head.succ] | code.tail := λ v, pure v.tail | (code.cons f fs) := λ v, do n ← code.eval f v, ns ← code.eval fs v, pure (n.head :: ns) | (code.comp f g) := λ v, g.eval v >>= f.eval | (code.case f g) := λ v, v.head.elim (f.eval v.tail) (λ y _, g.eval (y :: v.tail)) | (code.fix f) := pfun.fix $ λ v, (f.eval v).map $ λ v, if v.head = 0 then sum.inl v.tail else sum.inr v.tail namespace code /-- `nil` is the constant nil function: `nil v = []`. -/ def nil : code := tail.comp succ @[simp] theorem nil_eval (v) : nil.eval v = pure [] := by simp [nil] /-- `id` is the identity function: `id v = v`. -/ def id : code := tail.comp zero' @[simp] theorem id_eval (v) : id.eval v = pure v := by simp [id] /-- `head` gets the head of the input list: `head [] = [0]`, `head (n :: v) = [n]`. -/ def head : code := cons id nil @[simp] theorem head_eval (v) : head.eval v = pure [v.head] := by simp [head] /-- `zero` is the constant zero function: `zero v = [0]`. -/ def zero : code := cons zero' nil @[simp] theorem zero_eval (v) : zero.eval v = pure [0] := by simp [zero] /-- `pred` returns the predecessor of the head of the input: `pred [] = [0]`, `pred (0 :: v) = [0]`, `pred (n+1 :: v) = [n]`. -/ def pred : code := case zero head @[simp] theorem pred_eval (v) : pred.eval v = pure [v.head.pred] := by simp [pred]; cases v.head; simp /-- `rfind f` performs the function of the `rfind` primitive of partial recursive functions. `rfind f v` returns the smallest `n` such that `(f (n :: v)).head = 0`. It is implemented as: rfind f v = pred (fix (λ (n::v), f (n::v) :: n+1 :: v) (0 :: v)) The idea is that the initial state is `0 :: v`, and the `fix` keeps `n :: v` as its internal state; it calls `f (n :: v)` as the exit test and `n+1 :: v` as the next state. At the end we get `n+1 :: v` where `n` is the desired output, and `pred (n+1 :: v) = [n]` returns the result. -/ def rfind (f : code) : code := comp pred $ comp (fix $ cons f $ cons succ tail) zero' /-- `prec f g` implements the `prec` (primitive recursion) operation of partial recursive functions. `prec f g` evaluates as: * `prec f g [] = [f []]` * `prec f g (0 :: v) = [f v]` * `prec f g (n+1 :: v) = [g (n :: prec f g (n :: v) :: v)]` It is implemented as: G (a :: b :: IH :: v) = (b :: a+1 :: b-1 :: g (a :: IH :: v) :: v) F (0 :: f_v :: v) = (f_v :: v) F (n+1 :: f_v :: v) = (fix G (0 :: n :: f_v :: v)).tail.tail prec f g (a :: v) = [(F (a :: f v :: v)).head] Because `fix` always evaluates its body at least once, we must special case the `0` case to avoid calling `g` more times than necessary (which could be bad if `g` diverges). If the input is `0 :: v`, then `F (0 :: f v :: v) = (f v :: v)` so we return `[f v]`. If the input is `n+1 :: v`, we evaluate the function from the bottom up, with initial state `0 :: n :: f v :: v`. The first number counts up, providing arguments for the applications to `g`, while the second number counts down, providing the exit condition (this is the initial `b` in the return value of `G`, which is stripped by `fix`). After the `fix` is complete, the final state is `n :: 0 :: res :: v` where `res` is the desired result, and the rest reduces this to `[res]`. -/ def prec (f g : code) : code := let G := cons tail $ cons succ $ cons (comp pred tail) $ cons (comp g $ cons id $ comp tail tail) $ comp tail $ comp tail tail in let F := case id $ comp (comp (comp tail tail) (fix G)) zero' in cons (comp F (cons head $ cons (comp f tail) tail)) nil local attribute [-simp] roption.bind_eq_bind roption.map_eq_map roption.pure_eq_some theorem exists_code.comp {m n} {f : vector ℕ n →. ℕ} {g : fin n → vector ℕ m →. ℕ} (hf : ∃ c : code, ∀ v : vector ℕ n, c.eval v.1 = pure <$> f v) (hg : ∀ i, ∃ c : code, ∀ v : vector ℕ m, c.eval v.1 = pure <$> g i v) : ∃ c : code, ∀ v : vector ℕ m, c.eval v.1 = pure <$> (vector.m_of_fn (λ i, g i v) >>= f) := begin suffices : ∃ c : code, ∀ v : vector ℕ m, c.eval v.1 = subtype.val <$> vector.m_of_fn (λ i, g i v), { obtain ⟨cf, hf⟩ := hf, obtain ⟨cg, hg⟩ := this, exact ⟨cf.comp cg, λ v, by { simp [hg, hf, map_bind, seq_bind_eq, (∘), -subtype.val_eq_coe], refl }⟩ }, clear hf f, induction n with n IH, { exact ⟨nil, λ v, by simp [vector.m_of_fn]; refl⟩ }, { obtain ⟨cg, hg₁⟩ := hg 0, obtain ⟨cl, hl⟩ := IH (λ i, hg i.succ), exact ⟨cons cg cl, λ v, by { simp [vector.m_of_fn, hg₁, map_bind, seq_bind_eq, bind_assoc, (∘), hl, -subtype.val_eq_coe], refl }⟩ }, end theorem exists_code {n} {f : vector ℕ n →. ℕ} (hf : nat.partrec' f) : ∃ c : code, ∀ v : vector ℕ n, c.eval v.1 = pure <$> f v := begin induction hf with n f hf, induction hf, case prim zero { exact ⟨zero', λ ⟨[], _⟩, rfl⟩ }, case prim succ { exact ⟨succ, λ ⟨[v], _⟩, rfl⟩ }, case prim nth : n i { refine fin.succ_rec (λ n, _) (λ n i IH, _) i, { exact ⟨head, λ ⟨list.cons a as, _⟩, by simp; refl⟩ }, { obtain ⟨c, h⟩ := IH, exact ⟨c.comp tail, λ v, by simpa [← vector.nth_tail] using h v.tail⟩ } }, case prim comp : m n f g hf hg IHf IHg { simpa [roption.bind_eq_bind] using exists_code.comp IHf IHg }, case prim prec : n f g hf hg IHf IHg { obtain ⟨cf, hf⟩ := IHf, obtain ⟨cg, hg⟩ := IHg, simp only [roption.map_eq_map, roption.map_some, pfun.coe_val] at hf hg, refine ⟨prec cf cg, λ v, _⟩, rw ← v.cons_head_tail, specialize hf v.tail, replace hg := λ a b, hg (a ::ᵥ b ::ᵥ v.tail), simp only [vector.cons_val, vector.tail_val] at hf hg, simp only [roption.map_eq_map, roption.map_some, vector.cons_val, vector.cons_tail, vector.cons_head, pfun.coe_val, vector.tail_val], simp only [← roption.pure_eq_some] at hf hg ⊢, induction v.head with n IH; simp [prec, hf, bind_assoc, ← roption.map_eq_map, ← bind_pure_comp_eq_map, show ∀ x, pure x = [x], from λ _, rfl, -subtype.val_eq_coe], suffices : ∀ a b, a + b = n → (n.succ :: 0 :: g (n ::ᵥ (nat.elim (f v.tail) (λ y IH, g (y ::ᵥ IH ::ᵥ v.tail)) n) ::ᵥ v.tail) :: v.val.tail : list ℕ) ∈ pfun.fix (λ v : list ℕ, do x ← cg.eval (v.head :: v.tail.tail), pure $ if v.tail.head = 0 then sum.inl (v.head.succ :: v.tail.head.pred :: x.head :: v.tail.tail.tail : list ℕ) else sum.inr (v.head.succ :: v.tail.head.pred :: x.head :: v.tail.tail.tail)) (a :: b :: nat.elim (f v.tail) (λ y IH, g (y ::ᵥ IH ::ᵥ v.tail)) a :: v.val.tail), { rw (_ : pfun.fix _ _ = pure _), swap, exact roption.eq_some_iff.2 (this 0 n (zero_add n)), simp only [list.head, pure_bind, list.tail_cons] }, intros a b e, induction b with b IH generalizing a e, { refine pfun.mem_fix_iff.2 (or.inl $ roption.eq_some_iff.1 _), simp only [hg, ← e, pure_bind, list.tail_cons], refl }, { refine pfun.mem_fix_iff.2 (or.inr ⟨_, _, IH (a+1) (by rwa add_right_comm)⟩), simp only [hg, eval, pure_bind, nat.elim_succ, list.tail], exact roption.mem_some_iff.2 rfl } }, case comp : m n f g hf hg IHf IHg { exact exists_code.comp IHf IHg }, case rfind : n f hf IHf { obtain ⟨cf, hf⟩ := IHf, refine ⟨rfind cf, λ v, _⟩, replace hf := λ a, hf (a ::ᵥ v), simp only [roption.map_eq_map, roption.map_some, vector.cons_val, pfun.coe_val, show ∀ x, pure x = [x], from λ _, rfl] at hf ⊢, refine roption.ext (λ x, _), simp only [rfind, roption.bind_eq_bind, roption.pure_eq_some, roption.map_eq_map, roption.bind_some, exists_prop, eval, list.head, pred_eval, roption.map_some, bool.ff_eq_to_bool_iff, roption.mem_bind_iff, list.length, roption.mem_map_iff, nat.mem_rfind, list.tail, bool.tt_eq_to_bool_iff, roption.mem_some_iff, roption.map_bind], split, { rintro ⟨v', h1, rfl⟩, suffices : ∀ (v₁ : list ℕ), v' ∈ pfun.fix (λ v, (cf.eval v).bind $ λ y, roption.some $ if y.head = 0 then sum.inl (v.head.succ :: v.tail) else sum.inr (v.head.succ :: v.tail)) v₁ → ∀ n, v₁ = n :: v.val → (∀ m < n, ¬f (m ::ᵥ v) = 0) → (∃ (a : ℕ), (f (a ::ᵥ v) = 0 ∧ ∀ {m : ℕ}, m < a → ¬f (m ::ᵥ v) = 0) ∧ [a] = [v'.head.pred]), { exact this _ h1 0 rfl (by rintro _ ⟨⟩) }, clear h1, intros v₀ h1, refine pfun.fix_induction h1 (λ v₁ h2 IH, _), clear h1, rintro n rfl hm, have := pfun.mem_fix_iff.1 h2, simp only [hf, roption.bind_some] at this, split_ifs at this, { simp only [list.head, exists_false, or_false, roption.mem_some_iff, list.tail_cons, false_and] at this, subst this, exact ⟨_, ⟨h, hm⟩, rfl⟩ }, { simp only [list.head, exists_eq_left, roption.mem_some_iff, list.tail_cons, false_or] at this, refine IH _ this (by simp [hf, h, -subtype.val_eq_coe]) _ rfl (λ m h', _), obtain h|rfl := nat.lt_succ_iff_lt_or_eq.1 h', exacts [hm _ h, h] } }, { rintro ⟨n, ⟨hn, hm⟩, rfl⟩, refine ⟨n.succ :: v.1, _, rfl⟩, have : (n.succ :: v.1 : list ℕ) ∈ pfun.fix (λ v, (cf.eval v).bind $ λ y, roption.some $ if y.head = 0 then sum.inl (v.head.succ :: v.tail) else sum.inr (v.head.succ :: v.tail)) (n :: v.val) := pfun.mem_fix_iff.2 (or.inl (by simp [hf, hn, -subtype.val_eq_coe])), generalize_hyp : (n.succ :: v.1 : list ℕ) = w at this ⊢, clear hn, induction n with n IH, {exact this}, refine IH (λ m h', hm (nat.lt_succ_of_lt h')) (pfun.mem_fix_iff.2 (or.inr ⟨_, _, this⟩)), simp only [hf, hm n.lt_succ_self, roption.bind_some, list.head, eq_self_iff_true, if_false, roption.mem_some_iff, and_self, list.tail_cons] } } end end code /-! ## From compositional semantics to sequential semantics Our initial sequential model is designed to be as similar as possible to the compositional semantics in terms of its primitives, but it is a sequential semantics, meaning that rather than defining an `eval c : list ℕ →. list ℕ` function for each program, defined by recursion on programs, we have a type `cfg` with a step function `step : cfg → option cfg` that provides a deterministic evaluation order. In order to do this, we introduce the notion of a *continuation*, which can be viewed as a `code` with a hole in it where evaluation is currently taking place. Continuations can be assigned a `list ℕ →. list ℕ` semantics as well, with the interpretation being that given a `list ℕ` result returned from the code in the hole, the remainder of the program will evaluate to a `list ℕ` final value. The continuations are: * `halt`: the empty continuation: the hole is the whole program, whatever is returned is the final result. In our notation this is just `_`. * `cons₁ fs v k`: evaluating the first part of a `cons`, that is `k (_ :: fs v)`, where `k` is the outer continuation. * `cons₂ ns k`: evaluating the second part of a `cons`: `k (ns.head :: _)`. (Technically we don't need to hold on to all of `ns` here since we are already committed to taking the head, but this is more regular.) * `comp f k`: evaluating the first part of a composition: `k (f _)`. * `fix f k`: waiting for the result of `f` in a `fix f` expression: `k (if _.head = 0 then _.tail else fix f (_.tail))` The type `cfg` of evaluation states is: * `ret k v`: we have received a result, and are now evaluating the continuation `k` with result `v`; that is, `k v` where `k` is ready to evaluate. * `halt v`: we are done and the result is `v`. The main theorem of this section is that for each code `c`, the state `step_normal c halt v` steps to `v'` in finitely many steps if and only if `code.eval c v = some v'`. -/ /-- The type of continuations, built up during evaluation of a `code` expression. -/ @[derive inhabited] inductive cont | halt | cons₁ : code → list ℕ → cont → cont | cons₂ : list ℕ → cont → cont | comp : code → cont → cont | fix : code → cont → cont /-- The semantics of a continuation. -/ def cont.eval : cont → list ℕ →. list ℕ | cont.halt := pure | (cont.cons₁ fs as k) := λ v, do ns ← code.eval fs as, cont.eval k (v.head :: ns) | (cont.cons₂ ns k) := λ v, cont.eval k (ns.head :: v) | (cont.comp f k) := λ v, code.eval f v >>= cont.eval k | (cont.fix f k) := λ v, if v.head = 0 then k.eval v.tail else f.fix.eval v.tail >>= k.eval /-- The semantics of a continuation. -/ @[derive inhabited] inductive cfg | halt : list ℕ → cfg | ret : cont → list ℕ → cfg /-- Evaluating `c : code` in a continuation `k : cont` and input `v : list ℕ`. This goes by recursion on `c`, building an augmented continuation and a value to pass to it. * `zero' v = 0 :: v` evaluates immediately, so we return it to the parent continuation * `succ v = [v.head.succ]` evaluates immediately, so we return it to the parent continuation * `tail v = v.tail` evaluates immediately, so we return it to the parent continuation * `cons f fs v = (f v).head :: fs v` requires two sub-evaluations, so we evaluate `f v` in the continuation `k (_.head :: fs v)` (called `cont.cons₁ fs v k`) * `comp f g v = f (g v)` requires two sub-evaluations, so we evaluate `g v` in the continuation `k (f _)` (called `cont.comp f k`) * `case f g v = v.head.cases_on (f v.tail) (λ n, g (n :: v.tail))` has the information needed to evaluate the case statement, so we do that and transition to either `f v` or `g (n :: v.tail)`. * `fix f v = let v' := f v in if v'.head = 0 then k v'.tail else fix f v'.tail` needs to first evaluate `f v`, so we do that and leave the rest for the continuation (called `cont.fix f k`) -/ def step_normal : code → cont → list ℕ → cfg | code.zero' k v := cfg.ret k (0 :: v) | code.succ k v := cfg.ret k [v.head.succ] | code.tail k v := cfg.ret k v.tail | (code.cons f fs) k v := step_normal f (cont.cons₁ fs v k) v | (code.comp f g) k v := step_normal g (cont.comp f k) v | (code.case f g) k v := v.head.elim (step_normal f k v.tail) (λ y _, step_normal g k (y :: v.tail)) | (code.fix f) k v := step_normal f (cont.fix f k) v /-- Evaluating a continuation `k : cont` on input `v : list ℕ`. This is the second part of evaluation, when we receive results from continuations built by `step_normal`. * `cont.halt v = v`, so we are done and transition to the `cfg.halt v` state * `cont.cons₁ fs as k v = k (v.head :: fs as)`, so we evaluate `fs as` now with the continuation `k (v.head :: _)` (called `cons₂ v k`). * `cont.cons₂ ns k v = k (ns.head :: v)`, where we now have everything we need to evaluate `ns.head :: v`, so we return it to `k`. * `cont.comp f k v = k (f v)`, so we call `f v` with `k` as the continuation. * `cont.fix f k v = k (if v.head = 0 then k v.tail else fix f v.tail)`, where `v` is a value, so we evaluate the if statement and either call `k` with `v.tail`, or call `fix f v` with `k` as the continuation (which immediately calls `f` with `cont.fix f k` as the continuation). -/ def step_ret : cont → list ℕ → cfg | cont.halt v := cfg.halt v | (cont.cons₁ fs as k) v := step_normal fs (cont.cons₂ v k) as | (cont.cons₂ ns k) v := step_ret k (ns.head :: v) | (cont.comp f k) v := step_normal f k v | (cont.fix f k) v := if v.head = 0 then step_ret k v.tail else step_normal f (cont.fix f k) v.tail /-- If we are not done (in `cfg.halt` state), then we must be still stuck on a continuation, so this main loop calls `step_ret` with the new continuation. The overall `step` function transitions from one `cfg` to another, only halting at the `cfg.halt` state. -/ def step : cfg → option cfg | (cfg.halt _) := none | (cfg.ret k v) := some (step_ret k v) /-- In order to extract a compositional semantics from the sequential execution behavior of configurations, we observe that continuations have a monoid structure, with `cont.halt` as the unit and `cont.then` as the multiplication. `cont.then k₁ k₂` runs `k₁` until it halts, and then takes the result of `k₁` and passes it to `k₂`. We will not prove it is associative (although it is), but we are instead interested in the associativity law `k₂ (eval c k₁) = eval c (k₁.then k₂)`. This holds at both the sequential and compositional levels, and allows us to express running a machine without the ambient continuation and relate it to the original machine's evaluation steps. In the literature this is usually where one uses Turing machines embedded inside other Turing machines, but this approach allows us to avoid changing the ambient type `cfg` in the middle of the recursion. -/ def cont.then : cont → cont → cont | cont.halt k' := k' | (cont.cons₁ fs as k) k' := cont.cons₁ fs as (k.then k') | (cont.cons₂ ns k) k' := cont.cons₂ ns (k.then k') | (cont.comp f k) k' := cont.comp f (k.then k') | (cont.fix f k) k' := cont.fix f (k.then k') theorem cont.then_eval {k k' : cont} {v} : (k.then k').eval v = k.eval v >>= k'.eval := begin induction k generalizing v; simp only [cont.eval, cont.then, bind_assoc, pure_bind, *], { simp only [← k_ih] }, { split_ifs; [refl, simp only [← k_ih, bind_assoc]] } end /-- The `then k` function is a "configuration homomorphism". Its operation on states is to append `k` to the continuation of a `cfg.ret` state, and to run `k` on `v` if we are in the `cfg.halt v` state. -/ def cfg.then : cfg → cont → cfg | (cfg.halt v) k' := step_ret k' v | (cfg.ret k v) k' := cfg.ret (k.then k') v /-- The `step_normal` function respects the `then k'` homomorphism. Note that this is an exact equality, not a simulation; the original and embedded machines move in lock-step until the embedded machine reaches the halt state. -/ theorem step_normal_then (c) (k k' : cont) (v) : step_normal c (k.then k') v = (step_normal c k v).then k' := begin induction c generalizing k v; simp only [cont.then, step_normal, cfg.then, *] {constructor_eq := ff}, { rw [← c_ih_a, cont.then] }, { rw [← c_ih_a_1, cont.then] }, { cases v.head; simp only [nat.elim] }, { rw [← c_ih, cont.then] }, end /-- The `step_ret` function respects the `then k'` homomorphism. Note that this is an exact equality, not a simulation; the original and embedded machines move in lock-step until the embedded machine reaches the halt state. -/ theorem step_ret_then {k k' : cont} {v} : step_ret (k.then k') v = (step_ret k v).then k' := begin induction k generalizing v; simp only [cont.then, step_ret, cfg.then, *], { rw ← step_normal_then, refl }, { rw ← step_normal_then }, { split_ifs, {rw ← k_ih}, {rw ← step_normal_then, refl} }, end /-- This is a temporary definition, because we will prove in `code_is_ok` that it always holds. It asserts that `c` is semantically correct; that is, for any `k` and `v`, `eval (step_normal c k v) = eval (cfg.ret k (code.eval c v))`, as an equality of partial values (so one diverges iff the other does). In particular, we can let `k = cont.halt`, and then this asserts that `step_normal c cont.halt v` evaluates to `cfg.halt (code.eval c v)`. -/ def code.ok (c : code) := ∀ k v, eval step (step_normal c k v) = code.eval c v >>= λ v, eval step (cfg.ret k v) theorem code.ok.zero {c} (h : code.ok c) {v} : eval step (step_normal c cont.halt v) = cfg.halt <$> code.eval c v := begin rw [h, ← bind_pure_comp_eq_map], congr, funext v, exact roption.eq_some_iff.2 (mem_eval.2 ⟨refl_trans_gen.single rfl, rfl⟩), end theorem step_normal.is_ret (c k v) : ∃ k' v', step_normal c k v = cfg.ret k' v' := begin induction c generalizing k v, iterate 3 { exact ⟨_, _, rfl⟩ }, case cons : f fs IHf IHfs { apply IHf }, case comp : f g IHf IHg { apply IHg }, case case : f g IHf IHg { rw step_normal, cases v.head; simp only [nat.elim]; [apply IHf, apply IHg] }, case fix : f IHf { apply IHf }, end theorem cont_eval_fix {f k v} (fok : code.ok f) : eval step (step_normal f (cont.fix f k) v) = f.fix.eval v >>= λ v, eval step (cfg.ret k v) := begin refine roption.ext (λ x, _), simp only [roption.bind_eq_bind, roption.mem_bind_iff], split, { suffices : ∀ c, x ∈ eval step c → ∀ v c', c = cfg.then c' (cont.fix f k) → reaches step (step_normal f cont.halt v) c' → ∃ v₁ ∈ f.eval v, ∃ v₂ ∈ (if list.head v₁ = 0 then pure v₁.tail else f.fix.eval v₁.tail), x ∈ eval step (cfg.ret k v₂), { intro h, obtain ⟨v₁, hv₁, v₂, hv₂, h₃⟩ := this _ h _ _ (step_normal_then _ cont.halt _ _) refl_trans_gen.refl, refine ⟨v₂, pfun.mem_fix_iff.2 _, h₃⟩, simp only [roption.eq_some_iff.2 hv₁, roption.map_some], split_ifs at hv₂ ⊢, { rw roption.mem_some_iff.1 hv₂, exact or.inl (roption.mem_some _) }, { exact or.inr ⟨_, roption.mem_some _, hv₂⟩ } }, refine λ c he, eval_induction he (λ y h IH, _), rintro v (⟨v'⟩ | ⟨k',v'⟩) rfl hr; rw cfg.then at h IH, { have := mem_eval.2 ⟨hr, rfl⟩, rw [fok, roption.bind_eq_bind, roption.mem_bind_iff] at this, obtain ⟨v'', h₁, h₂⟩ := this, rw reaches_eval at h₂, swap, exact refl_trans_gen.single rfl, cases roption.mem_unique h₂ (mem_eval.2 ⟨refl_trans_gen.refl, rfl⟩), refine ⟨v', h₁, _⟩, rw [step_ret] at h, revert h, by_cases he : v'.head = 0; simp only [exists_prop, if_pos, if_false, he]; intro h, { refine ⟨_, roption.mem_some _, _⟩, rw reaches_eval, exact h, exact refl_trans_gen.single rfl }, { obtain ⟨k₀, v₀, e₀⟩ := step_normal.is_ret f cont.halt v'.tail, have e₁ := step_normal_then f cont.halt (cont.fix f k) v'.tail, rw [e₀, cont.then, cfg.then] at e₁, obtain ⟨v₁, hv₁, v₂, hv₂, h₃⟩ := IH (step_ret (k₀.then (cont.fix f k)) v₀) _ _ v'.tail _ step_ret_then _, { refine ⟨_, pfun.mem_fix_iff.2 _, h₃⟩, simp only [roption.eq_some_iff.2 hv₁, roption.map_some, roption.mem_some_iff], split_ifs at hv₂ ⊢; [exact or.inl (roption.mem_some_iff.1 hv₂), exact or.inr ⟨_, rfl, hv₂⟩] }, { rwa [← @reaches_eval _ _ (cfg.ret (k₀.then (cont.fix f k)) v₀), ← e₁], exact refl_trans_gen.single rfl }, { rw [step_ret, if_neg he, e₁], refl }, { apply refl_trans_gen.single, rw e₀, exact rfl } } }, { rw reaches_eval at h, swap, exact refl_trans_gen.single rfl, exact IH _ h rfl _ _ step_ret_then (refl_trans_gen.tail hr rfl) } }, { rintro ⟨v', he, hr⟩, rw reaches_eval at hr, swap, exact refl_trans_gen.single rfl, refine pfun.fix_induction he (λ v (he : v' ∈ f.fix.eval v) IH, _), rw [fok, roption.bind_eq_bind, roption.mem_bind_iff], obtain he | ⟨v'', he₁', he₂'⟩ := pfun.mem_fix_iff.1 he, { obtain ⟨v', he₁, he₂⟩ := (roption.mem_map_iff _).1 he, split_ifs at he₂; cases he₂, refine ⟨_, he₁, _⟩, rw reaches_eval, swap, exact refl_trans_gen.single rfl, rwa [step_ret, if_pos h] }, { obtain ⟨v₁, he₁, he₂⟩ := (roption.mem_map_iff _).1 he₁', split_ifs at he₂; cases he₂, clear he₂ he₁', change _ ∈ f.fix.eval _ at he₂', refine ⟨_, he₁, _⟩, rw reaches_eval, swap, exact refl_trans_gen.single rfl, rwa [step_ret, if_neg h], exact IH v₁.tail he₂' ((roption.mem_map_iff _).2 ⟨_, he₁, if_neg h⟩) } } end theorem code_is_ok (c) : code.ok c := begin induction c; intros k v; rw step_normal, iterate 3 { simp only [code.eval, pure_bind] }, case cons : f fs IHf IHfs { rw [code.eval, IHf], simp only [bind_assoc, cont.eval, pure_bind], congr, funext v, rw [reaches_eval], swap, exact refl_trans_gen.single rfl, rw [step_ret, IHfs], congr, funext v', refine eq.trans _ (eq.symm _); try {exact reaches_eval (refl_trans_gen.single rfl)} }, case comp : f g IHf IHg { rw [code.eval, IHg], simp only [bind_assoc, cont.eval, pure_bind], congr, funext v, rw [reaches_eval], swap, exact refl_trans_gen.single rfl, rw [step_ret, IHf] }, case case : f g IHf IHg { simp only [code.eval], cases v.head; simp only [nat.elim, code.eval]; [apply IHf, apply IHg] }, case fix : f IHf { rw cont_eval_fix IHf }, end theorem step_normal_eval (c v) : eval step (step_normal c cont.halt v) = cfg.halt <$> c.eval v := (code_is_ok c).zero theorem step_ret_eval {k v} : eval step (step_ret k v) = cfg.halt <$> k.eval v := begin induction k generalizing v, case halt : { simp only [mem_eval, cont.eval, map_pure], exact roption.eq_some_iff.2 (mem_eval.2 ⟨refl_trans_gen.refl, rfl⟩) }, case cons₁ : fs as k IH { rw [cont.eval, step_ret, code_is_ok], simp only [← bind_pure_comp_eq_map, bind_assoc], congr, funext v', rw [reaches_eval], swap, exact refl_trans_gen.single rfl, rw [step_ret, IH, bind_pure_comp_eq_map] }, case cons₂ : ns k IH { rw [cont.eval, step_ret], exact IH }, case comp : f k IH { rw [cont.eval, step_ret, code_is_ok], simp only [← bind_pure_comp_eq_map, bind_assoc], congr, funext v', rw [reaches_eval], swap, exact refl_trans_gen.single rfl, rw [IH, bind_pure_comp_eq_map] }, case fix : f k IH { rw [cont.eval, step_ret], simp only [bind_pure_comp_eq_map], split_ifs, { exact IH }, simp only [← bind_pure_comp_eq_map, bind_assoc, cont_eval_fix (code_is_ok _)], congr, funext, rw [bind_pure_comp_eq_map, ← IH], exact reaches_eval (refl_trans_gen.single rfl) }, end end to_partrec /-! ## Simulating sequentialized partial recursive functions in TM2 At this point we have a sequential model of partial recursive functions: the `cfg` type and `step : cfg → option cfg` function from the previous section. The key feature of this model is that it does a finite amount of computation (in fact, an amount which is statically bounded by the size of the program) between each step, and no individual step can diverge (unlike the compositional semantics, where every sub-part of the computation is potentially divergent). So we can utilize the same techniques as in the other TM simulations in `computability.turing_machine` to prove that each step corresponds to a finite number of steps in a lower level model. (We don't prove it here, but in anticipation of the complexity class P, the simulation is actually polynomial-time as well.) The target model is `turing.TM2`, which has a fixed finite set of stacks, a bit of local storage, with programs selected from a potentially infinite (but finitely accessible) set of program positions, or labels `Λ`, each of which executes a finite sequence of basic stack commands. For this program we will need four stacks, each on an alphabet `Γ'` like so: inductive Γ' | Cons | cons | bit0 | bit1 We represent a number as a bit sequence, lists of numbers by putting `cons` after each element, and lists of lists of natural numbers by putting `Cons` after each list. For example: 0 ~> [] 1 ~> [bit1] 6 ~> [bit0, bit1, bit1] [1, 2] ~> [bit1, cons, bit0, bit1, cons] [[], [1, 2]] ~> [Cons, bit1, cons, bit0, bit1, cons, Cons] The four stacks are `main`, `rev`, `aux`, `stack`. In normal mode, `main` contains the input to the current program (a `list ℕ`) and `stack` contains data (a `list (list ℕ)`) associated to the current continuation, and in `ret` mode `main` contains the value that is being passed to the continuation and `stack` contains the data for the continuation. The `rev` and `aux` stacks are usually empty; `rev` is used to store reversed data when e.g. moving a value from one stack to another, while `aux` is used as a temporary for a `main`/`stack` swap that happens during `cons₁` evaluation. The only local store we need is `option Γ'`, which stores the result of the last pop operation. (Most of our working data are natural numbers, which are too large to fit in the local store.) The continuations from the previous section are data-carrying, containing all the values that have been computed and are awaiting other arguments. In order to have only a finite number of continuations appear in the program so that they can be used in machine states, we separate the data part (anything with type `list ℕ`) from the `cont` type, producing a `cont'` type that lacks this information. The data is kept on the `stack` stack. Because we want to have subroutines for e.g. moving an entire stack to another place, we use an infinite inductive type `Λ'` so that we can execute a program and then return to do something else without having to define too many different kinds of intermediate states. (We must nevertheless prove that only finitely many labels are accessible.) The labels are: * `move p k₁ k₂ q`: move elements from stack `k₁` to `k₂` while `p` holds of the value being moved. The last element, that fails `p`, is placed in neither stack but left in the local store. At the end of the operation, `k₂` will have the elements of `k₁` in reverse order. Then do `q`. * `clear p k q`: delete elements from stack `k` until `p` is true. Like `move`, the last element is left in the local storage. Then do `q`. * `copy q`: Move all elements from `rev` to both `main` and `stack` (in reverse order), then do `q`. That is, it takes `(a, b, c, d)` to `(b.reverse ++ a, [], c, b.reverse ++ d)`. * `push k f q`: push `f s`, where `s` is the local store, to stack `k`, then do `q`. This is a duplicate of the `push` instruction that is part of the TM2 model, but by having a subroutine just for this purpose we can build up programs to execute inside a `goto` statement, where we have the flexibility to be general recursive. * `read (f : option Γ' → Λ')`: go to state `f s` where `s` is the local store. Again this is only here for convenience. * `succ q`: perform a successor operation. Assuming `[n]` is encoded on `main` before, `[n+1]` will be on main after. This implements successor for binary natural numbers. * `pred q₁ q₂`: perform a predecessor operation or `case` statement. If `[]` is encoded on `main` before, then we transition to `q₁` with `[]` on main; if `(0 :: v)` is on `main` before then `v` will be on `main` after and we transition to `q₁`; and if `(n+1 :: v)` is on `main` before then `n :: v` will be on `main` after and we transition to `q₂`. * `ret k`: call continuation `k`. Each continuation has its own interpretation of the data in `stack` and sets up the data for the next continuation. * `ret (cons₁ fs k)`: `v :: k_data` on `stack` and `ns` on `main`, and the next step expects `v` on `main` and `ns :: k_data` on `stack`. So we have to do a little dance here with six reverse-moves using the `aux` stack to perform a three-point swap, each of which involves two reversals. * `ret (cons₂ k)`: `ns :: k_data` is on `stack` and `v` is on `main`, and we have to put `ns.head :: v` on `main` and `k_data` on `stack`. This is done using the `head` subroutine. * `ret (fix f k)`: This stores no data, so we just check if `main` starts with `0` and if so, remove it and call `k`, otherwise `clear` the first value and call `f`. * `ret halt`: the stack is empty, and `main` has the output. Do nothing and halt. In addition to these basic states, we define some additional subroutines that are used in the above: * `push'`, `peek'`, `pop'` are special versions of the builtins that use the local store to supply inputs and outputs. * `unrev`: special case `move ff rev main` to move everything from `rev` back to `main`. Used as a cleanup operation in several functions. * `move_excl p k₁ k₂ q`: same as `move` but pushes the last value read back onto the source stack. * `move₂ p k₁ k₂ q`: double `move`, so that the result comes out in the right order at the target stack. Implemented as `move_excl p k rev; move ff rev k₂`. Assumes that neither `k₁` nor `k₂` is `rev` and `rev` is initially empty. * `head k q`: get the first natural number from stack `k` and reverse-move it to `rev`, then clear the rest of the list at `k` and then `unrev` to reverse-move the head value to `main`. This is used with `k = main` to implement regular `head`, i.e. if `v` is on `main` before then `[v.head]` will be on `main` after; and also with `k = stack` for the `cons` operation, which has `v` on `main` and `ns :: k_data` on `stack`, and results in `k_data` on `stack` and `ns.head :: v` on `main`. * `tr_normal` is the main entry point, defining states that perform a given `code` computation. It mostly just dispatches to functions written above. The main theorem of this section is `tr_eval`, which asserts that for each that for each code `c`, the state `init c v` steps to `halt v'` in finitely many steps if and only if `code.eval c v = some v'`. -/ namespace partrec_to_TM2 section open to_partrec /-- The alphabet for the stacks in the program. `bit0` and `bit1` are used to represent `ℕ` values as lists of binary digits, `cons` is used to separate `list ℕ` values, and `Cons` is used to separate `list (list ℕ)` values. See the section documentation. -/ @[derive [decidable_eq, inhabited]] inductive Γ' | Cons | cons | bit0 | bit1 /-- The four stacks used by the program. `main` is used to store the input value in `tr_normal` mode and the output value in `Λ'.ret` mode, while `stack` is used to keep all the data for the continuations. `rev` is used to store reversed lists when transferring values between stacks, and `aux` is only used once in `cons₁`. See the section documentation. -/ @[derive [decidable_eq, inhabited]] inductive K' | main | rev | aux | stack open K' /-- Continuations as in `to_partrec.cont` but with the data removed. This is done because we want the set of all continuations in the program to be finite (so that it can ultimately be encoded into the finite state machine of a Turing machine), but a continuation can handle a potentially infinite number of data values during execution. -/ @[derive inhabited] inductive cont' | halt | cons₁ : code → cont' → cont' | cons₂ : cont' → cont' | comp : code → cont' → cont' | fix : code → cont' → cont' /-- The set of program positions. We make extensive use of inductive types here to let us describe "subroutines"; for example `clear p k q` is a program that clears stack `k`, then does `q` where `q` is another label. In order to prevent this from resulting in an infinite number of distinct accessible states, we are careful to be non-recursive (although loops are okay). See the section documentation for a description of all the programs. -/ inductive Λ' | move (p : Γ' → bool) (k₁ k₂ : K') (q : Λ') | clear (p : Γ' → bool) (k : K') (q : Λ') | copy (q : Λ') | push (k : K') (s : option Γ' → option Γ') (q : Λ') | read (f : option Γ' → Λ') | succ (q : Λ') | pred (q₁ q₂ : Λ') | ret (k : cont') instance : inhabited Λ' := ⟨Λ'.ret cont'.halt⟩ /-- The type of TM2 statements used by this machine. -/ @[derive inhabited] def stmt' := TM2.stmt (λ _:K', Γ') Λ' (option Γ') /-- The type of TM2 configurations used by this machine. -/ @[derive inhabited] def cfg' := TM2.cfg (λ _:K', Γ') Λ' (option Γ') open TM2.stmt /-- A predicate that detects the end of a natural number, either `Γ'.cons` or `Γ'.Cons` (or implicitly the end of the list), for use in predicate-taking functions like `move` and `clear`. -/ def nat_end : Γ' → bool | Γ'.Cons := tt | Γ'.cons := tt | _ := ff /-- Pop a value from the stack and place the result in local store. -/ @[simp] def pop' (k : K') : stmt' → stmt' := pop k (λ x v, v) /-- Peek a value from the stack and place the result in local store. -/ @[simp] def peek' (k : K') : stmt' → stmt' := peek k (λ x v, v) /-- Push the value in the local store to the given stack. -/ @[simp] def push' (k : K') : stmt' → stmt' := push k (λ x, x.iget) /-- Move everything from the `rev` stack to the `main` stack (reversed). -/ def unrev := Λ'.move (λ _, ff) rev main /-- Move elements from `k₁` to `k₂` while `p` holds, with the last element being left on `k₁`. -/ def move_excl (p k₁ k₂ q) := Λ'.move p k₁ k₂ $ Λ'.push k₁ id q /-- Move elements from `k₁` to `k₂` without reversion, by performing a double move via the `rev` stack. -/ def move₂ (p k₁ k₂ q) := move_excl p k₁ rev $ Λ'.move (λ _, ff) rev k₂ q /-- Assuming `tr_list v` is on the front of stack `k`, remove it, and push `v.head` onto `main`. See the section documentation. -/ def head (k : K') (q : Λ') : Λ' := Λ'.move nat_end k rev $ Λ'.push rev (λ _, some Γ'.cons) $ Λ'.read $ λ s, (if s = some Γ'.Cons then id else Λ'.clear (λ x, x = Γ'.Cons) k) $ unrev q /-- The program that evaluates code `c` with continuation `k`. This expects an initial state where `tr_list v` is on `main`, `tr_cont_stack k` is on `stack`, and `aux` and `rev` are empty. See the section documentation for details. -/ @[simp] def tr_normal : code → cont' → Λ' | code.zero' k := Λ'.push main (λ _, some Γ'.cons) $ Λ'.ret k | code.succ k := head main $ Λ'.succ $ Λ'.ret k | code.tail k := Λ'.clear nat_end main $ Λ'.ret k | (code.cons f fs) k := Λ'.push stack (λ _, some Γ'.Cons) $ Λ'.move (λ _, ff) main rev $ Λ'.copy $ tr_normal f (cont'.cons₁ fs k) | (code.comp f g) k := tr_normal g (cont'.comp f k) | (code.case f g) k := Λ'.pred (tr_normal f k) (tr_normal g k) | (code.fix f) k := tr_normal f (cont'.fix f k) /-- The main program. See the section documentation for details. -/ @[simp] def tr : Λ' → stmt' | (Λ'.move p k₁ k₂ q) := pop' k₁ $ branch (λ s, s.elim tt p) ( goto $ λ _, q ) ( push' k₂ $ goto $ λ _, Λ'.move p k₁ k₂ q ) | (Λ'.push k f q) := branch (λ s, (f s).is_some) ( push k (λ s, (f s).iget) $ goto $ λ _, q ) ( goto $ λ _, q ) | (Λ'.read q) := goto q | (Λ'.clear p k q) := pop' k $ branch (λ s, s.elim tt p) ( goto $ λ _, q ) ( goto $ λ _, Λ'.clear p k q ) | (Λ'.copy q) := pop' rev $ branch option.is_some ( push' main $ push' stack $ goto $ λ _, Λ'.copy q ) ( goto $ λ _, q ) | (Λ'.succ q) := pop' main $ branch (λ s, s = some Γ'.bit1) ( push rev (λ _, Γ'.bit0) $ goto $ λ _, Λ'.succ q ) $ branch (λ s, s = some Γ'.cons) ( push main (λ _, Γ'.cons) $ push main (λ _, Γ'.bit1) $ goto $ λ _, unrev q ) ( push main (λ _, Γ'.bit1) $ goto $ λ _, unrev q ) | (Λ'.pred q₁ q₂) := pop' main $ branch (λ s, s = some Γ'.bit0) ( push rev (λ _, Γ'.bit1) $ goto $ λ _, Λ'.pred q₁ q₂ ) $ branch (λ s, nat_end s.iget) ( goto $ λ _, q₁ ) ( peek' main $ branch (λ s, nat_end s.iget) ( goto $ λ _, unrev q₂ ) ( push rev (λ _, Γ'.bit0) $ goto $ λ _, unrev q₂ ) ) | (Λ'.ret (cont'.cons₁ fs k)) := goto $ λ _, move₂ (λ _, ff) main aux $ move₂ (λ s, s = Γ'.Cons) stack main $ move₂ (λ _, ff) aux stack $ tr_normal fs (cont'.cons₂ k) | (Λ'.ret (cont'.cons₂ k)) := goto $ λ _, head stack $ Λ'.ret k | (Λ'.ret (cont'.comp f k)) := goto $ λ _, tr_normal f k | (Λ'.ret (cont'.fix f k)) := pop' main $ goto $ λ s, cond (nat_end s.iget) (Λ'.ret k) $ Λ'.clear nat_end main $ tr_normal f (cont'.fix f k) | (Λ'.ret cont'.halt) := load (λ _, none) $ halt /-- Translating a `cont` continuation to a `cont'` continuation simply entails dropping all the data. This data is instead encoded in `tr_cont_stack` in the configuration. -/ def tr_cont : cont → cont' | cont.halt := cont'.halt | (cont.cons₁ c _ k) := cont'.cons₁ c (tr_cont k) | (cont.cons₂ _ k) := cont'.cons₂ (tr_cont k) | (cont.comp c k) := cont'.comp c (tr_cont k) | (cont.fix c k) := cont'.fix c (tr_cont k) /-- We use `pos_num` to define the translation of binary natural numbers. A natural number is represented as a little-endian list of `bit0` and `bit1` elements: 1 = [bit1] 2 = [bit0, bit1] 3 = [bit1, bit1] 4 = [bit0, bit0, bit1] In particular, this representation guarantees no trailing `bit0`'s at the end of the list. -/ def tr_pos_num : pos_num → list Γ' | pos_num.one := [Γ'.bit1] | (pos_num.bit0 n) := Γ'.bit0 :: tr_pos_num n | (pos_num.bit1 n) := Γ'.bit1 :: tr_pos_num n /-- We use `num` to define the translation of binary natural numbers. Positive numbers are translated using `tr_pos_num`, and `tr_num 0 = []`. So there are never any trailing `bit0`'s in a translated `num`. 0 = [] 1 = [bit1] 2 = [bit0, bit1] 3 = [bit1, bit1] 4 = [bit0, bit0, bit1] -/ def tr_num : num → list Γ' | num.zero := [] | (num.pos n) := tr_pos_num n /-- Because we use binary encoding, we define `tr_nat` in terms of `tr_num`, using `num`, which are binary natural numbers. (We could also use `nat.binary_rec_on`, but `num` and `pos_num` make for easy inductions.) -/ def tr_nat (n : ℕ) : list Γ' := tr_num n @[simp] theorem tr_nat_zero : tr_nat 0 = [] := rfl /-- Lists are translated with a `cons` after each encoded number. For example: [] = [] [0] = [cons] [1] = [bit1, cons] [6, 0] = [bit0, bit1, bit1, cons, cons] -/ @[simp] def tr_list : list ℕ → list Γ' | [] := [] | (n :: ns) := tr_nat n ++ Γ'.cons :: tr_list ns /-- Lists of lists are translated with a `Cons` after each encoded list. For example: [] = [] [[]] = [Cons] [[], []] = [Cons, Cons] [[0]] = [cons, Cons] [[1, 2], [0]] = [bit1, cons, bit0, bit1, cons, Cons, cons, Cons] -/ @[simp] def tr_llist : list (list ℕ) → list Γ' | [] := [] | (l :: ls) := tr_list l ++ Γ'.Cons :: tr_llist ls /-- The data part of a continuation is a list of lists, which is encoded on the `stack` stack using `tr_llist`. -/ @[simp] def cont_stack : cont → list (list ℕ) | cont.halt := [] | (cont.cons₁ _ ns k) := ns :: cont_stack k | (cont.cons₂ ns k) := ns :: cont_stack k | (cont.comp _ k) := cont_stack k | (cont.fix _ k) := cont_stack k /-- The data part of a continuation is a list of lists, which is encoded on the `stack` stack using `tr_llist`. -/ def tr_cont_stack (k : cont) := tr_llist (cont_stack k) /-- This is the nondependent eliminator for `K'`, but we use it specifically here in order to represent the stack data as four lists rather than as a function `K' → list Γ'`, because this makes rewrites easier. The theorems `K'.elim_update_main` et. al. show how such a function is updated after an `update` to one of the components. -/ @[simp] def K'.elim (a b c d : list Γ') : K' → list Γ' | K'.main := a | K'.rev := b | K'.aux := c | K'.stack := d @[simp] theorem K'.elim_update_main {a b c d a'} : update (K'.elim a b c d) main a' = K'.elim a' b c d := by funext x; cases x; refl @[simp] theorem K'.elim_update_rev {a b c d b'} : update (K'.elim a b c d) rev b' = K'.elim a b' c d := by funext x; cases x; refl @[simp] theorem K'.elim_update_aux {a b c d c'} : update (K'.elim a b c d) aux c' = K'.elim a b c' d := by funext x; cases x; refl @[simp] theorem K'.elim_update_stack {a b c d d'} : update (K'.elim a b c d) stack d' = K'.elim a b c d' := by funext x; cases x; refl /-- The halting state corresponding to a `list ℕ` output value. -/ def halt (v : list ℕ) : cfg' := ⟨none, none, K'.elim (tr_list v) [] [] []⟩ /-- The `cfg` states map to `cfg'` states almost one to one, except that in normal operation the local store contains an arbitrary garbage value. To make the final theorem cleaner we explicitly clear it in the halt state so that there is exactly one configuration corresponding to output `v`. -/ def tr_cfg : cfg → cfg' → Prop | (cfg.ret k v) c' := ∃ s, c' = ⟨some (Λ'.ret (tr_cont k)), s, K'.elim (tr_list v) [] [] (tr_cont_stack k)⟩ | (cfg.halt v) c' := c' = halt v /-- This could be a general list definition, but it is also somewhat specialized to this application. `split_at_pred p L` will search `L` for the first element satisfying `p`. If it is found, say `L = l₁ ++ a :: l₂` where `a` satisfies `p` but `l₁` does not, then it returns `(l₁, some a, l₂)`. Otherwise, if there is no such element, it returns `(L, none, [])`. -/ def split_at_pred {α} (p : α → bool) : list α → list α × option α × list α | [] := ([], none, []) | (a :: as) := cond (p a) ([], some a, as) $ let ⟨l₁, o, l₂⟩ := split_at_pred as in ⟨a :: l₁, o, l₂⟩ theorem split_at_pred_eq {α} (p : α → bool) : ∀ L l₁ o l₂, (∀ x ∈ l₁, p x = ff) → option.elim o (L = l₁ ∧ l₂ = []) (λ a, p a = tt ∧ L = l₁ ++ a :: l₂) → split_at_pred p L = (l₁, o, l₂) | [] _ none _ _ ⟨rfl, rfl⟩ := rfl | [] l₁ (some o) l₂ h₁ ⟨h₂, h₃⟩ := by simp at h₃; contradiction | (a :: L) l₁ o l₂ h₁ h₂ := begin rw [split_at_pred], have IH := split_at_pred_eq L, cases o, { cases l₁ with a' l₁; rcases h₂ with ⟨⟨⟩, rfl⟩, rw [h₁ a (or.inl rfl), cond, IH L none [] _ ⟨rfl, rfl⟩], refl, exact λ x h, h₁ x (or.inr h) }, { cases l₁ with a' l₁; rcases h₂ with ⟨h₂, ⟨⟩⟩, {rw [h₂, cond]}, rw [h₁ a (or.inl rfl), cond, IH l₁ (some o) l₂ _ ⟨h₂, _⟩]; try {refl}, exact λ x h, h₁ x (or.inr h) }, end theorem split_at_pred_ff {α} (L : list α) : split_at_pred (λ _, ff) L = (L, none, []) := split_at_pred_eq _ _ _ _ _ (λ _ _, rfl) ⟨rfl, rfl⟩ theorem move_ok {p k₁ k₂ q s L₁ o L₂} {S : K' → list Γ'} (h₁ : k₁ ≠ k₂) (e : split_at_pred p (S k₁) = (L₁, o, L₂)) : reaches₁ (TM2.step tr) ⟨some (Λ'.move p k₁ k₂ q), s, S⟩ ⟨some q, o, update (update S k₁ L₂) k₂ (L₁.reverse_core (S k₂))⟩ := begin induction L₁ with a L₁ IH generalizing S s, { rw [(_ : [].reverse_core _ = _), function.update_eq_self], swap, { rw function.update_noteq h₁.symm, refl }, refine trans_gen.head' rfl _, simp, cases S k₁ with a Sk, {cases e, refl}, simp [split_at_pred] at e ⊢, cases p a; simp at e ⊢, { revert e, rcases split_at_pred p Sk with ⟨_, _, _⟩, rintro ⟨⟩ }, { simp only [e] } }, { refine trans_gen.head rfl _, simp, cases e₁ : S k₁ with a' Sk; rw [e₁, split_at_pred] at e, {cases e}, cases e₂ : p a'; simp only [e₂, cond] at e, swap, {cases e}, rcases e₃ : split_at_pred p Sk with ⟨_, _, _⟩, rw [e₃, split_at_pred] at e, cases e, simp [e₂], convert @IH (update (update S k₁ Sk) k₂ (a :: S k₂)) _ _ using 2; simp [function.update_noteq, h₁, h₁.symm, e₃, list.reverse_core], simp [function.update_comm h₁.symm] } end theorem unrev_ok {q s} {S : K' → list Γ'} : reaches₁ (TM2.step tr) ⟨some (unrev q), s, S⟩ ⟨some q, none, update (update S rev []) main (list.reverse_core (S rev) (S main))⟩ := move_ok dec_trivial $ split_at_pred_ff _ theorem move₂_ok {p k₁ k₂ q s L₁ o L₂} {S : K' → list Γ'} (h₁ : k₁ ≠ rev ∧ k₂ ≠ rev ∧ k₁ ≠ k₂) (h₂ : S rev = []) (e : split_at_pred p (S k₁) = (L₁, o, L₂)) : reaches₁ (TM2.step tr) ⟨some (move₂ p k₁ k₂ q), s, S⟩ ⟨some q, none, update (update S k₁ (o.elim id list.cons L₂)) k₂ (L₁ ++ S k₂)⟩ := begin refine (move_ok h₁.1 e).trans (trans_gen.head rfl _), cases o; simp only [option.elim, tr, id.def], { convert move_ok h₁.2.1.symm (split_at_pred_ff _) using 2, simp only [function.update_comm h₁.1, function.update_idem], rw show update S rev [] = S, by rw [← h₂, function.update_eq_self], simp only [function.update_noteq h₁.2.2.symm, function.update_noteq h₁.2.1, function.update_noteq h₁.1.symm, list.reverse_core_eq, h₂, function.update_same, list.append_nil, list.reverse_reverse] }, { convert move_ok h₁.2.1.symm (split_at_pred_ff _) using 2, simp only [h₂, function.update_comm h₁.1, list.reverse_core_eq, function.update_same, list.append_nil, function.update_idem], rw show update S rev [] = S, by rw [← h₂, function.update_eq_self], simp only [function.update_noteq h₁.1.symm, function.update_noteq h₁.2.2.symm, function.update_noteq h₁.2.1, function.update_same, list.reverse_reverse] }, end theorem clear_ok {p k q s L₁ o L₂} {S : K' → list Γ'} (e : split_at_pred p (S k) = (L₁, o, L₂)) : reaches₁ (TM2.step tr) ⟨some (Λ'.clear p k q), s, S⟩ ⟨some q, o, update S k L₂⟩ := begin induction L₁ with a L₁ IH generalizing S s, { refine trans_gen.head' rfl _, simp, cases S k with a Sk, {cases e, refl}, simp [split_at_pred] at e ⊢, cases p a; simp at e ⊢, { revert e, rcases split_at_pred p Sk with ⟨_, _, _⟩, rintro ⟨⟩ }, { simp only [e] } }, { refine trans_gen.head rfl _, simp, cases e₁ : S k with a' Sk; rw [e₁, split_at_pred] at e, {cases e}, cases e₂ : p a'; simp only [e₂, cond] at e, swap, {cases e}, rcases e₃ : split_at_pred p Sk with ⟨_, _, _⟩, rw [e₃, split_at_pred] at e, cases e, simp [e₂], convert @IH (update S k Sk) _ _ using 2; simp [e₃] } end theorem copy_ok (q s a b c d) : reaches₁ (TM2.step tr) ⟨some (Λ'.copy q), s, K'.elim a b c d⟩ ⟨some q, none, K'.elim (list.reverse_core b a) [] c (list.reverse_core b d)⟩ := begin induction b with x b IH generalizing a d s, { refine trans_gen.single _, simp, refl }, refine trans_gen.head rfl _, simp, exact IH _ _ _, end theorem tr_pos_num_nat_end : ∀ n (x ∈ tr_pos_num n), nat_end x = ff | pos_num.one _ (or.inl rfl) := rfl | (pos_num.bit0 n) _ (or.inl rfl) := rfl | (pos_num.bit0 n) _ (or.inr h) := tr_pos_num_nat_end n _ h | (pos_num.bit1 n) _ (or.inl rfl) := rfl | (pos_num.bit1 n) _ (or.inr h) := tr_pos_num_nat_end n _ h theorem tr_num_nat_end : ∀ n (x ∈ tr_num n), nat_end x = ff | (num.pos n) x h := tr_pos_num_nat_end n x h theorem tr_nat_nat_end (n) : ∀ x ∈ tr_nat n, nat_end x = ff := tr_num_nat_end _ theorem tr_list_ne_Cons : ∀ l (x ∈ tr_list l), x ≠ Γ'.Cons | (a :: l) x h := begin simp [tr_list] at h, obtain h | rfl | h := h, { rintro rfl, cases tr_nat_nat_end _ _ h }, { rintro ⟨⟩ }, { exact tr_list_ne_Cons l _ h } end theorem head_main_ok {q s L} {c d : list Γ'} : reaches₁ (TM2.step tr) ⟨some (head main q), s, K'.elim (tr_list L) [] c d⟩ ⟨some q, none, K'.elim (tr_list [L.head]) [] c d⟩ := begin let o : option Γ' := list.cases_on L none (λ _ _, some Γ'.cons), refine (move_ok dec_trivial (split_at_pred_eq _ _ (tr_nat L.head) o (tr_list L.tail) (tr_nat_nat_end _) _)).trans (trans_gen.head rfl (trans_gen.head rfl _)), { cases L; exact ⟨rfl, rfl⟩ }, simp [show o ≠ some Γ'.Cons, by cases L; rintro ⟨⟩], refine (clear_ok (split_at_pred_eq _ _ _ none [] _ ⟨rfl, rfl⟩)).trans _, { exact λ x h, (to_bool_ff (tr_list_ne_Cons _ _ h)) }, convert unrev_ok, simp [list.reverse_core_eq], end theorem head_stack_ok {q s L₁ L₂ L₃} : reaches₁ (TM2.step tr) ⟨some (head stack q), s, K'.elim (tr_list L₁) [] [] (tr_list L₂ ++ Γ'.Cons :: L₃)⟩ ⟨some q, none, K'.elim (tr_list (L₂.head :: L₁)) [] [] L₃⟩ := begin cases L₂ with a L₂, { refine trans_gen.trans (move_ok dec_trivial (split_at_pred_eq _ _ [] (some Γ'.Cons) L₃ (by rintro _ ⟨⟩) ⟨rfl, rfl⟩)) (trans_gen.head rfl (trans_gen.head rfl _)), convert unrev_ok, simp, refl }, { refine trans_gen.trans (move_ok dec_trivial (split_at_pred_eq _ _ (tr_nat a) (some Γ'.cons) (tr_list L₂ ++ Γ'.Cons :: L₃) (tr_nat_nat_end _) ⟨rfl, by simp⟩)) (trans_gen.head rfl (trans_gen.head rfl _)), simp, refine trans_gen.trans (clear_ok (split_at_pred_eq _ _ (tr_list L₂) (some Γ'.Cons) L₃ (λ x h, (to_bool_ff (tr_list_ne_Cons _ _ h))) ⟨rfl, by simp⟩)) _, convert unrev_ok, simp [list.reverse_core_eq] }, end theorem succ_ok {q s n} {c d : list Γ'} : reaches₁ (TM2.step tr) ⟨some (Λ'.succ q), s, K'.elim (tr_list [n]) [] c d⟩ ⟨some q, none, K'.elim (tr_list [n.succ]) [] c d⟩ := begin simp [tr_nat, num.add_one], cases (n:num), { refine trans_gen.head rfl _, simp, rw if_neg, swap, rintro ⟨⟩, rw if_pos, swap, refl, convert unrev_ok, simp, refl }, simp [num.succ, tr_num, num.succ'], suffices : ∀ l₁, ∃ l₁' l₂' s', list.reverse_core l₁ (tr_pos_num a.succ) = list.reverse_core l₁' l₂' ∧ reaches₁ (TM2.step tr) ⟨some q.succ, s, K'.elim (tr_pos_num a ++ [Γ'.cons]) l₁ c d⟩ ⟨some (unrev q), s', K'.elim (l₂' ++ [Γ'.cons]) l₁' c d⟩, { obtain ⟨l₁', l₂', s', e, h⟩ := this [], simp [list.reverse_core] at e, refine h.trans _, convert unrev_ok using 2, simp [e, list.reverse_core_eq] }, induction a with m IH m IH generalizing s; intro l₁, { refine ⟨Γ'.bit0 :: l₁, [Γ'.bit1], some Γ'.cons, rfl, trans_gen.head rfl (trans_gen.single _)⟩, simp [tr_pos_num] }, { obtain ⟨l₁', l₂', s', e, h⟩ := IH (Γ'.bit0 :: l₁), refine ⟨l₁', l₂', s', e, trans_gen.head _ h⟩, swap, simp [pos_num.succ, tr_pos_num] }, { refine ⟨l₁, _, some Γ'.bit0, rfl, trans_gen.single _⟩, simp, refl }, end theorem pred_ok (q₁ q₂ s v) (c d : list Γ') : ∃ s', reaches₁ (TM2.step tr) ⟨some (Λ'.pred q₁ q₂), s, K'.elim (tr_list v) [] c d⟩ (v.head.elim ⟨some q₁, s', K'.elim (tr_list v.tail) [] c d⟩ (λ n _, ⟨some q₂, s', K'.elim (tr_list (n :: v.tail)) [] c d⟩)) := begin rcases v with _|⟨_|n, v⟩, { refine ⟨none, trans_gen.single _⟩, simp, refl }, { refine ⟨some Γ'.cons, trans_gen.single _⟩, simp, refl }, refine ⟨none, _⟩, simp [tr_nat, num.add_one, num.succ, tr_num], cases (n:num), { simp [tr_pos_num, tr_num, show num.zero.succ' = pos_num.one, from rfl], refine trans_gen.head rfl _, convert unrev_ok, simp, refl }, simp [tr_num, num.succ'], suffices : ∀ l₁, ∃ l₁' l₂' s', list.reverse_core l₁ (tr_pos_num a) = list.reverse_core l₁' l₂' ∧ reaches₁ (TM2.step tr) ⟨some (q₁.pred q₂), s, K'.elim (tr_pos_num a.succ ++ Γ'.cons :: tr_list v) l₁ c d⟩ ⟨some (unrev q₂), s', K'.elim (l₂' ++ Γ'.cons :: tr_list v) l₁' c d⟩, { obtain ⟨l₁', l₂', s', e, h⟩ := this [], simp [list.reverse_core] at e, refine h.trans _, convert unrev_ok using 2, simp [e, list.reverse_core_eq] }, induction a with m IH m IH generalizing s; intro l₁, { refine ⟨Γ'.bit1 :: l₁, [], some Γ'.cons, rfl, trans_gen.head rfl (trans_gen.single _)⟩, simp [tr_pos_num, show pos_num.one.succ = pos_num.one.bit0, from rfl], refl }, { obtain ⟨l₁', l₂', s', e, h⟩ := IH (some Γ'.bit0) (Γ'.bit1 :: l₁), refine ⟨l₁', l₂', s', e, trans_gen.head _ h⟩, simp, refl }, { obtain ⟨a, l, e, h⟩ : ∃ a l, tr_pos_num m = a :: l ∧ nat_end a = ff, { cases m; refine ⟨_, _, rfl, rfl⟩ }, refine ⟨Γ'.bit0 :: l₁, _, some a, rfl, trans_gen.single _⟩, simp [tr_pos_num, pos_num.succ, e, h, nat_end, show some Γ'.bit1 ≠ some Γ'.bit0, from dec_trivial] }, end theorem tr_normal_respects (c k v s) : ∃ b₂, tr_cfg (step_normal c k v) b₂ ∧ reaches₁ (TM2.step tr) ⟨some (tr_normal c (tr_cont k)), s, K'.elim (tr_list v) [] [] (tr_cont_stack k)⟩ b₂ := begin induction c generalizing k v s, case zero' : { refine ⟨_, ⟨s, rfl⟩, trans_gen.single _⟩, simp }, case succ : { refine ⟨_, ⟨none, rfl⟩, head_main_ok.trans succ_ok⟩ }, case tail : { let o : option Γ' := list.cases_on v none (λ _ _, some Γ'.cons), refine ⟨_, ⟨o, rfl⟩, _⟩, convert clear_ok _, simp, swap, refine split_at_pred_eq _ _ (tr_nat v.head) _ _ (tr_nat_nat_end _) _, cases v; exact ⟨rfl, rfl⟩ }, case cons : f fs IHf IHfs { obtain ⟨c, h₁, h₂⟩ := IHf (cont.cons₁ fs v k) v none, refine ⟨c, h₁, trans_gen.head rfl $ (move_ok dec_trivial (split_at_pred_ff _)).trans _⟩, simp [step_normal], refine (copy_ok _ none [] (tr_list v).reverse _ _).trans _, convert h₂ using 2, simp [list.reverse_core_eq, tr_cont_stack] }, case comp : f g IHf IHg { exact IHg (cont.comp f k) v s }, case case : f g IHf IHg { rw step_normal, obtain ⟨s', h⟩ := pred_ok _ _ s v _ _, cases v.head with n, { obtain ⟨c, h₁, h₂⟩ := IHf k _ s', exact ⟨_, h₁, h.trans h₂⟩ }, { obtain ⟨c, h₁, h₂⟩ := IHg k _ s', exact ⟨_, h₁, h.trans h₂⟩ } }, case fix : f IH { apply IH } end theorem tr_ret_respects (k v s) : ∃ b₂, tr_cfg (step_ret k v) b₂ ∧ reaches₁ (TM2.step tr) ⟨some (Λ'.ret (tr_cont k)), s, K'.elim (tr_list v) [] [] (tr_cont_stack k)⟩ b₂ := begin induction k generalizing v s, case halt : { exact ⟨_, rfl, trans_gen.single rfl⟩ }, case cons₁ : fs as k IH { obtain ⟨s', h₁, h₂⟩ := tr_normal_respects fs (cont.cons₂ v k) as none, refine ⟨s', h₁, trans_gen.head rfl _⟩, simp, refine (move₂_ok dec_trivial _ (split_at_pred_ff _)).trans _, {refl}, simp, refine (move₂_ok dec_trivial _ _).trans _, swap 4, {refl}, swap 4, {exact (split_at_pred_eq _ _ _ (some Γ'.Cons) _ (λ x h, to_bool_ff (tr_list_ne_Cons _ _ h)) ⟨rfl, rfl⟩)}, refine (move₂_ok dec_trivial _ (split_at_pred_ff _)).trans _, {refl}, simp, exact h₂ }, case cons₂ : ns k IH { obtain ⟨c, h₁, h₂⟩ := IH (ns.head :: v) none, exact ⟨c, h₁, trans_gen.head rfl $ head_stack_ok.trans h₂⟩ }, case comp : f k IH { obtain ⟨s', h₁, h₂⟩ := tr_normal_respects f k v s, exact ⟨_, h₁, trans_gen.head rfl h₂⟩ }, case fix : f k IH { rw [step_ret], have : if v.head = 0 then nat_end (tr_list v).head'.iget = tt ∧ (tr_list v).tail = tr_list v.tail else nat_end (tr_list v).head'.iget = ff ∧ (tr_list v).tail = (tr_nat v.head).tail ++ Γ'.cons :: tr_list v.tail, { cases v with n, {exact ⟨rfl, rfl⟩}, cases n, {exact ⟨rfl, rfl⟩}, rw [tr_list, list.head, tr_nat, nat.cast_succ, num.add_one, num.succ, list.tail], cases (n:num).succ'; exact ⟨rfl, rfl⟩ }, by_cases v.head = 0; simp [h] at this ⊢, { obtain ⟨c, h₁, h₂⟩ := IH v.tail (tr_list v).head', refine ⟨c, h₁, trans_gen.head rfl _⟩, simp [tr_cont, tr_cont_stack, this], exact h₂ }, { obtain ⟨s', h₁, h₂⟩ := tr_normal_respects f (cont.fix f k) v.tail (some Γ'.cons), refine ⟨_, h₁, trans_gen.head rfl $ trans_gen.trans _ h₂⟩, swap 3, simp [tr_cont, this.1], convert clear_ok (split_at_pred_eq _ _ (tr_nat v.head).tail (some Γ'.cons) _ _ _) using 2, { simp }, { exact λ x h, tr_nat_nat_end _ _ (list.tail_subset _ h) }, { exact ⟨rfl, this.2⟩ } } }, end theorem tr_respects : respects step (TM2.step tr) tr_cfg | (cfg.ret k v) _ ⟨s, rfl⟩ := tr_ret_respects _ _ _ | (cfg.halt v) _ rfl := rfl /-- The initial state, evaluating function `c` on input `v`. -/ def init (c : code) (v : list ℕ) : cfg' := ⟨some (tr_normal c cont'.halt), none, K'.elim (tr_list v) [] [] []⟩ theorem tr_init (c v) : ∃ b, tr_cfg (step_normal c cont.halt v) b ∧ reaches₁ (TM2.step tr) (init c v) b := tr_normal_respects _ _ _ _ theorem tr_eval (c v) : eval (TM2.step tr) (init c v) = halt <$> code.eval c v := begin obtain ⟨i, h₁, h₂⟩ := tr_init c v, refine roption.ext (λ x, _), rw [reaches_eval h₂.to_refl], simp, refine ⟨λ h, _, _⟩, { obtain ⟨c, hc₁, hc₂⟩ := tr_eval_rev tr_respects h₁ h, simp [step_normal_eval] at hc₂, obtain ⟨v', hv, rfl⟩ := hc₂, exact ⟨_, hv, hc₁.symm⟩ }, { rintro ⟨v', hv, rfl⟩, have := tr_eval tr_respects h₁, simp [step_normal_eval] at this, obtain ⟨_, ⟨⟩, h⟩ := this _ hv rfl, exact h } end end end partrec_to_TM2 end turing
4a0abe17344560af1edd8b38a18c5ccea26297f8
a45212b1526d532e6e83c44ddca6a05795113ddc
/src/data/sigma/default.lean
98ed296c3344033273ad813fa9e1244cda2013e6
[ "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
24
lean
import data.sigma.basic
9be1dd987da475dbf75adde76b38f8beb37c4dbd
206422fb9edabf63def0ed2aa3f489150fb09ccb
/src/analysis/normed_space/multilinear.lean
d2d46d22b4c0b45861f40580981557b050185a1b
[ "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
58,771
lean
/- Copyright (c) 2020 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import analysis.normed_space.operator_norm import topology.algebra.multilinear /-! # Operator norm on the space of continuous multilinear maps When `f` is a continuous multilinear map in finitely many variables, we define its norm `∥f∥` as the smallest number such that `∥f m∥ ≤ ∥f∥ * ∏ i, ∥m i∥` for all `m`. We show that it is indeed a norm, and prove its basic properties. ## Main results Let `f` be a multilinear map in finitely many variables. * `exists_bound_of_continuous` asserts that, if `f` is continuous, then there exists `C > 0` with `∥f m∥ ≤ C * ∏ i, ∥m i∥` for all `m`. * `continuous_of_bound`, conversely, asserts that this bound implies continuity. * `mk_continuous` constructs the associated continuous multilinear map. Let `f` be a continuous multilinear map in finitely many variables. * `∥f∥` is its norm, i.e., the smallest number such that `∥f m∥ ≤ ∥f∥ * ∏ i, ∥m i∥` for all `m`. * `le_op_norm f m` asserts the fundamental inequality `∥f m∥ ≤ ∥f∥ * ∏ i, ∥m i∥`. * `norm_image_sub_le f m₁ m₂` gives a control of the difference `f m₁ - f m₂` in terms of `∥f∥` and `∥m₁ - m₂∥`. We also register isomorphisms corresponding to currying or uncurrying variables, transforming a continuous multilinear function `f` in `n+1` variables into a continuous linear function taking values in continuous multilinear functions in `n` variables, and also into a continuous multilinear function in `n` variables taking values in continuous linear functions. These operations are called `f.curry_left` and `f.curry_right` respectively (with inverses `f.uncurry_left` and `f.uncurry_right`). They induce continuous linear equivalences between spaces of continuous multilinear functions in `n+1` variables and spaces of continuous linear functions into continuous multilinear functions in `n` variables (resp. continuous multilinear functions in `n` variables taking values in continuous linear functions), called respectively `continuous_multilinear_curry_left_equiv` and `continuous_multilinear_curry_right_equiv`. ## Implementation notes We mostly follow the API (and the proofs) of `operator_norm.lean`, with the additional complexity that we should deal with multilinear maps in several variables. The currying/uncurrying constructions are based on those in `multilinear.lean`. From the mathematical point of view, all the results follow from the results on operator norm in one variable, by applying them to one variable after the other through currying. However, this is only well defined when there is an order on the variables (for instance on `fin n`) although the final result is independent of the order. While everything could be done following this approach, it turns out that direct proofs are easier and more efficient. -/ noncomputable theory open_locale classical big_operators open finset metric local attribute [instance, priority 1001] add_comm_group.to_add_comm_monoid normed_group.to_add_comm_group normed_space.to_semimodule universes u v w w₁ w₂ wG variables {𝕜 : Type u} {ι : Type v} {n : ℕ} {G : Type wG} {E : fin n.succ → Type w} {E₁ : ι → Type w₁} {E₂ : Type w₂} [decidable_eq ι] [fintype ι] [nondiscrete_normed_field 𝕜] [normed_group G] [∀i, normed_group (E i)] [∀i, normed_group (E₁ i)] [normed_group E₂] [normed_space 𝕜 G] [∀i, normed_space 𝕜 (E i)] [∀i, normed_space 𝕜 (E₁ i)] [normed_space 𝕜 E₂] /-! ### Continuity properties of multilinear maps We relate continuity of multilinear maps to the inequality `∥f m∥ ≤ C * ∏ i, ∥m i∥`, in both directions. Along the way, we prove useful bounds on the difference `∥f m₁ - f m₂∥`. -/ namespace multilinear_map variable (f : multilinear_map 𝕜 E₁ E₂) /-- If a multilinear map in finitely many variables on normed spaces satisfies the inequality `∥f m∥ ≤ C * ∏ i, ∥m i∥` on a shell `ε i / ∥c i∥ < ∥m i∥ < ε i` for some positive numbers `ε i` and elements `c i : 𝕜`, `1 < ∥c i∥`, then it satisfies this inequality for all `m`. -/ lemma bound_of_shell {ε : ι → ℝ} {C : ℝ} (hε : ∀ i, 0 < ε i) {c : ι → 𝕜} (hc : ∀ i, 1 < ∥c i∥) (hf : ∀ m : Π i, E₁ i, (∀ i, ε i / ∥c i∥ ≤ ∥m i∥) → (∀ i, ∥m i∥ < ε i) → ∥f m∥ ≤ C * ∏ i, ∥m i∥) (m : Π i, E₁ i) : ∥f m∥ ≤ C * ∏ i, ∥m i∥ := begin rcases em (∃ i, m i = 0) with ⟨i, hi⟩|hm; [skip, push_neg at hm], { simp [f.map_coord_zero i hi, prod_eq_zero (mem_univ i), hi] }, choose δ hδ0 hδm_lt hle_δm hδinv using λ i, rescale_to_shell (hc i) (hε i) (hm i), have hδ0 : 0 < ∏ i, ∥δ i∥, from prod_pos (λ i _, norm_pos_iff.2 (hδ0 i)), simpa [map_smul_univ, norm_smul, prod_mul_distrib, mul_left_comm C, mul_le_mul_left hδ0] using hf (λ i, δ i • m i) hle_δm hδm_lt, end /-- If a multilinear map in finitely many variables on normed spaces is continuous, then it satisfies the inequality `∥f m∥ ≤ C * ∏ i, ∥m i∥`, for some `C` which can be chosen to be positive. -/ theorem exists_bound_of_continuous (hf : continuous f) : ∃ (C : ℝ), 0 < C ∧ (∀ m, ∥f m∥ ≤ C * ∏ i, ∥m i∥) := begin by_cases hι : nonempty ι, swap, { refine ⟨∥f 0∥ + 1, add_pos_of_nonneg_of_pos (norm_nonneg _) zero_lt_one, λ m, _⟩, obtain rfl : m = 0, from funext (λ i, (hι ⟨i⟩).elim), simp [univ_eq_empty.2 hι, zero_le_one] }, resetI, obtain ⟨ε : ℝ, ε0 : 0 < ε, hε : ∀ m : Π i, E₁ i, ∥m - 0∥ < ε → ∥f m - f 0∥ < 1⟩ := normed_group.tendsto_nhds_nhds.1 (hf.tendsto 0) 1 zero_lt_one, simp only [sub_zero, f.map_zero] at hε, rcases normed_field.exists_one_lt_norm 𝕜 with ⟨c, hc⟩, have : 0 < (∥c∥ / ε) ^ fintype.card ι, from pow_pos (div_pos (zero_lt_one.trans hc) ε0) _, refine ⟨_, this, _⟩, refine f.bound_of_shell (λ _, ε0) (λ _, hc) (λ m hcm hm, _), refine (hε m ((pi_norm_lt_iff ε0).2 hm)).le.trans _, rw [← div_le_iff' this, one_div, ← inv_pow', inv_div, fintype.card, ← prod_const], exact prod_le_prod (λ _ _, div_nonneg ε0.le (norm_nonneg _)) (λ i _, hcm i) end /-- If `f` satisfies a boundedness property around `0`, one can deduce a bound on `f m₁ - f m₂` using the multilinearity. Here, we give a precise but hard to use version. See `norm_image_sub_le_of_bound` for a less precise but more usable version. The bound reads `∥f m - f m'∥ ≤ C * ∥m 1 - m' 1∥ * max ∥m 2∥ ∥m' 2∥ * max ∥m 3∥ ∥m' 3∥ * ... * max ∥m n∥ ∥m' n∥ + ...`, where the other terms in the sum are the same products where `1` is replaced by any `i`. -/ lemma norm_image_sub_le_of_bound' {C : ℝ} (hC : 0 ≤ C) (H : ∀ m, ∥f m∥ ≤ C * ∏ i, ∥m i∥) (m₁ m₂ : Πi, E₁ i) : ∥f m₁ - f m₂∥ ≤ C * ∑ i, ∏ j, if j = i then ∥m₁ i - m₂ i∥ else max ∥m₁ j∥ ∥m₂ j∥ := begin have A : ∀(s : finset ι), ∥f m₁ - f (s.piecewise m₂ m₁)∥ ≤ C * ∑ i in s, ∏ j, if j = i then ∥m₁ i - m₂ i∥ else max ∥m₁ j∥ ∥m₂ j∥, { refine finset.induction (by simp) _, assume i s his Hrec, have I : ∥f (s.piecewise m₂ m₁) - f ((insert i s).piecewise m₂ m₁)∥ ≤ C * ∏ j, if j = i then ∥m₁ i - m₂ i∥ else max ∥m₁ j∥ ∥m₂ j∥, { have A : ((insert i s).piecewise m₂ m₁) = function.update (s.piecewise m₂ m₁) i (m₂ i) := s.piecewise_insert _ _ _, have B : s.piecewise m₂ m₁ = function.update (s.piecewise m₂ m₁) i (m₁ i), { ext j, by_cases h : j = i, { rw h, simp [his] }, { simp [h] } }, rw [B, A, ← f.map_sub], apply le_trans (H _) (mul_le_mul_of_nonneg_left _ hC), refine prod_le_prod (λj hj, norm_nonneg _) (λj hj, _), by_cases h : j = i, { rw h, simp }, { by_cases h' : j ∈ s; simp [h', h, le_refl] } }, calc ∥f m₁ - f ((insert i s).piecewise m₂ m₁)∥ ≤ ∥f m₁ - f (s.piecewise m₂ m₁)∥ + ∥f (s.piecewise m₂ m₁) - f ((insert i s).piecewise m₂ m₁)∥ : by { rw [← dist_eq_norm, ← dist_eq_norm, ← dist_eq_norm], exact dist_triangle _ _ _ } ... ≤ (C * ∑ i in s, ∏ j, if j = i then ∥m₁ i - m₂ i∥ else max ∥m₁ j∥ ∥m₂ j∥) + C * ∏ j, if j = i then ∥m₁ i - m₂ i∥ else max ∥m₁ j∥ ∥m₂ j∥ : add_le_add Hrec I ... = C * ∑ i in insert i s, ∏ j, if j = i then ∥m₁ i - m₂ i∥ else max ∥m₁ j∥ ∥m₂ j∥ : by simp [his, add_comm, left_distrib] }, convert A univ, simp end /-- If `f` satisfies a boundedness property around `0`, one can deduce a bound on `f m₁ - f m₂` using the multilinearity. Here, we give a usable but not very precise version. See `norm_image_sub_le_of_bound'` for a more precise but less usable version. The bound is `∥f m - f m'∥ ≤ C * card ι * ∥m - m'∥ * (max ∥m∥ ∥m'∥) ^ (card ι - 1)`. -/ lemma norm_image_sub_le_of_bound {C : ℝ} (hC : 0 ≤ C) (H : ∀ m, ∥f m∥ ≤ C * ∏ i, ∥m i∥) (m₁ m₂ : Πi, E₁ i) : ∥f m₁ - f m₂∥ ≤ C * (fintype.card ι) * (max ∥m₁∥ ∥m₂∥) ^ (fintype.card ι - 1) * ∥m₁ - m₂∥ := begin have A : ∀ (i : ι), ∏ j, (if j = i then ∥m₁ i - m₂ i∥ else max ∥m₁ j∥ ∥m₂ j∥) ≤ ∥m₁ - m₂∥ * (max ∥m₁∥ ∥m₂∥) ^ (fintype.card ι - 1), { assume i, calc ∏ j, (if j = i then ∥m₁ i - m₂ i∥ else max ∥m₁ j∥ ∥m₂ j∥) ≤ ∏ j : ι, function.update (λ j, max ∥m₁∥ ∥m₂∥) i (∥m₁ - m₂∥) j : begin apply prod_le_prod, { assume j hj, by_cases h : j = i; simp [h, norm_nonneg] }, { assume j hj, by_cases h : j = i, { rw h, simp, exact norm_le_pi_norm (m₁ - m₂) i }, { simp [h, max_le_max, norm_le_pi_norm] } } end ... = ∥m₁ - m₂∥ * (max ∥m₁∥ ∥m₂∥) ^ (fintype.card ι - 1) : by { rw prod_update_of_mem (finset.mem_univ _), simp [card_univ_diff] } }, calc ∥f m₁ - f m₂∥ ≤ C * ∑ i, ∏ j, if j = i then ∥m₁ i - m₂ i∥ else max ∥m₁ j∥ ∥m₂ j∥ : f.norm_image_sub_le_of_bound' hC H m₁ m₂ ... ≤ C * ∑ i, ∥m₁ - m₂∥ * (max ∥m₁∥ ∥m₂∥) ^ (fintype.card ι - 1) : mul_le_mul_of_nonneg_left (sum_le_sum (λi hi, A i)) hC ... = C * (fintype.card ι) * (max ∥m₁∥ ∥m₂∥) ^ (fintype.card ι - 1) * ∥m₁ - m₂∥ : by { rw [sum_const, card_univ, nsmul_eq_mul], ring } end /-- If a multilinear map satisfies an inequality `∥f m∥ ≤ C * ∏ i, ∥m i∥`, then it is continuous. -/ theorem continuous_of_bound (C : ℝ) (H : ∀ m, ∥f m∥ ≤ C * ∏ i, ∥m i∥) : continuous f := begin let D := max C 1, have D_pos : 0 ≤ D := le_trans zero_le_one (le_max_right _ _), replace H : ∀ m, ∥f m∥ ≤ D * ∏ i, ∥m i∥, { assume m, apply le_trans (H m) (mul_le_mul_of_nonneg_right (le_max_left _ _) _), exact prod_nonneg (λ(i : ι) hi, norm_nonneg (m i)) }, refine continuous_iff_continuous_at.2 (λm, _), refine continuous_at_of_locally_lipschitz zero_lt_one (D * (fintype.card ι) * (∥m∥ + 1) ^ (fintype.card ι - 1)) (λm' h', _), rw [dist_eq_norm, dist_eq_norm], have : 0 ≤ (max ∥m'∥ ∥m∥), by simp, have : (max ∥m'∥ ∥m∥) ≤ ∥m∥ + 1, by simp [zero_le_one, norm_le_of_mem_closed_ball (le_of_lt h'), -add_comm], calc ∥f m' - f m∥ ≤ D * (fintype.card ι) * (max ∥m'∥ ∥m∥) ^ (fintype.card ι - 1) * ∥m' - m∥ : f.norm_image_sub_le_of_bound D_pos H m' m ... ≤ D * (fintype.card ι) * (∥m∥ + 1) ^ (fintype.card ι - 1) * ∥m' - m∥ : by apply_rules [mul_le_mul_of_nonneg_right, mul_le_mul_of_nonneg_left, mul_nonneg, norm_nonneg, nat.cast_nonneg, pow_le_pow_of_le_left] end /-- Constructing a continuous multilinear map from a multilinear map satisfying a boundedness condition. -/ def mk_continuous (C : ℝ) (H : ∀ m, ∥f m∥ ≤ C * ∏ i, ∥m i∥) : continuous_multilinear_map 𝕜 E₁ E₂ := { cont := f.continuous_of_bound C H, ..f } @[simp] lemma coe_mk_continuous (C : ℝ) (H : ∀ m, ∥f m∥ ≤ C * ∏ i, ∥m i∥) : ⇑(f.mk_continuous C H) = f := rfl /-- Given a multilinear map in `n` variables, if one restricts it to `k` variables putting `z` on the other coordinates, then the resulting restricted function satisfies an inequality `∥f.restr v∥ ≤ C * ∥z∥^(n-k) * Π ∥v i∥` if the original function satisfies `∥f v∥ ≤ C * Π ∥v i∥`. -/ lemma restr_norm_le {k n : ℕ} (f : (multilinear_map 𝕜 (λ i : fin n, G) E₂ : _)) (s : finset (fin n)) (hk : s.card = k) (z : G) {C : ℝ} (H : ∀ m, ∥f m∥ ≤ C * ∏ i, ∥m i∥) (v : fin k → G) : ∥f.restr s hk z v∥ ≤ C * ∥z∥ ^ (n - k) * ∏ i, ∥v i∥ := begin rw [mul_right_comm, mul_assoc], convert H _ using 2, simp only [apply_dite norm, fintype.prod_dite, prod_const (∥z∥), finset.card_univ, fintype.card_of_subtype sᶜ (λ x, mem_compl), card_compl, fintype.card_fin, hk, mk_coe, ← (s.order_iso_of_fin hk).symm.bijective.prod_comp (λ x, ∥v x∥)], refl end end multilinear_map /-! ### Continuous multilinear maps We define the norm `∥f∥` of a continuous multilinear map `f` in finitely many variables as the smallest number such that `∥f m∥ ≤ ∥f∥ * ∏ i, ∥m i∥` for all `m`. We show that this defines a normed space structure on `continuous_multilinear_map 𝕜 E₁ E₂`. -/ namespace continuous_multilinear_map variables (c : 𝕜) (f g : continuous_multilinear_map 𝕜 E₁ E₂) (m : Πi, E₁ i) theorem bound : ∃ (C : ℝ), 0 < C ∧ (∀ m, ∥f m∥ ≤ C * ∏ i, ∥m i∥) := f.to_multilinear_map.exists_bound_of_continuous f.2 open real /-- The operator norm of a continuous multilinear map is the inf of all its bounds. -/ def op_norm := Inf {c | 0 ≤ (c : ℝ) ∧ ∀ m, ∥f m∥ ≤ c * ∏ i, ∥m i∥} instance has_op_norm : has_norm (continuous_multilinear_map 𝕜 E₁ E₂) := ⟨op_norm⟩ lemma norm_def : ∥f∥ = Inf {c | 0 ≤ (c : ℝ) ∧ ∀ m, ∥f m∥ ≤ c * ∏ i, ∥m i∥} := rfl -- So that invocations of `real.Inf_le` make sense: we show that the set of -- bounds is nonempty and bounded below. lemma bounds_nonempty {f : continuous_multilinear_map 𝕜 E₁ E₂} : ∃ c, c ∈ {c | 0 ≤ c ∧ ∀ m, ∥f m∥ ≤ c * ∏ i, ∥m i∥} := let ⟨M, hMp, hMb⟩ := f.bound in ⟨M, le_of_lt hMp, hMb⟩ lemma bounds_bdd_below {f : continuous_multilinear_map 𝕜 E₁ E₂} : bdd_below {c | 0 ≤ c ∧ ∀ m, ∥f m∥ ≤ c * ∏ i, ∥m i∥} := ⟨0, λ _ ⟨hn, _⟩, hn⟩ lemma op_norm_nonneg : 0 ≤ ∥f∥ := lb_le_Inf _ bounds_nonempty (λ _ ⟨hx, _⟩, hx) /-- The fundamental property of the operator norm of a continuous multilinear map: `∥f m∥` is bounded by `∥f∥` times the product of the `∥m i∥`. -/ theorem le_op_norm : ∥f m∥ ≤ ∥f∥ * ∏ i, ∥m i∥ := begin have A : 0 ≤ ∏ i, ∥m i∥ := prod_nonneg (λj hj, norm_nonneg _), by_cases h : ∏ i, ∥m i∥ = 0, { rcases prod_eq_zero_iff.1 h with ⟨i, _, hi⟩, rw norm_eq_zero at hi, have : f m = 0 := f.map_coord_zero i hi, rw [this, norm_zero], exact mul_nonneg (op_norm_nonneg f) A }, { have hlt : 0 < ∏ i, ∥m i∥ := lt_of_le_of_ne A (ne.symm h), rw [← div_le_iff hlt], apply (le_Inf _ bounds_nonempty bounds_bdd_below).2, rintro c ⟨_, hc⟩, rw [div_le_iff hlt], apply hc } end lemma ratio_le_op_norm : ∥f m∥ / ∏ i, ∥m i∥ ≤ ∥f∥ := begin have : 0 ≤ ∏ i, ∥m i∥ := prod_nonneg (λj hj, norm_nonneg _), cases eq_or_lt_of_le this with h h, { simp [h.symm, op_norm_nonneg f] }, { rw div_le_iff h, exact le_op_norm f m } end /-- The image of the unit ball under a continuous multilinear map is bounded. -/ lemma unit_le_op_norm (h : ∥m∥ ≤ 1) : ∥f m∥ ≤ ∥f∥ := calc ∥f m∥ ≤ ∥f∥ * ∏ i, ∥m i∥ : f.le_op_norm m ... ≤ ∥f∥ * ∏ i : ι, 1 : mul_le_mul_of_nonneg_left (prod_le_prod (λi hi, norm_nonneg _) (λi hi, le_trans (norm_le_pi_norm _ _) h)) (op_norm_nonneg f) ... = ∥f∥ : by simp /-- If one controls the norm of every `f x`, then one controls the norm of `f`. -/ lemma op_norm_le_bound {M : ℝ} (hMp: 0 ≤ M) (hM : ∀ m, ∥f m∥ ≤ M * ∏ i, ∥m i∥) : ∥f∥ ≤ M := Inf_le _ bounds_bdd_below ⟨hMp, hM⟩ /-- The operator norm satisfies the triangle inequality. -/ theorem op_norm_add_le : ∥f + g∥ ≤ ∥f∥ + ∥g∥ := Inf_le _ bounds_bdd_below ⟨add_nonneg (op_norm_nonneg _) (op_norm_nonneg _), λ x, by { rw add_mul, exact norm_add_le_of_le (le_op_norm _ _) (le_op_norm _ _) }⟩ /-- A continuous linear map is zero iff its norm vanishes. -/ theorem op_norm_zero_iff : ∥f∥ = 0 ↔ f = 0 := begin split, { assume h, ext m, simpa [h] using f.le_op_norm m }, { rintro rfl, apply le_antisymm (op_norm_le_bound 0 le_rfl (λm, _)) (op_norm_nonneg _), simp } end variables {𝕜' : Type*} [nondiscrete_normed_field 𝕜'] [normed_algebra 𝕜' 𝕜] [normed_space 𝕜' E₂] [is_scalar_tower 𝕜' 𝕜 E₂] lemma op_norm_smul_le (c : 𝕜') : ∥c • f∥ ≤ ∥c∥ * ∥f∥ := (c • f).op_norm_le_bound (mul_nonneg (norm_nonneg _) (op_norm_nonneg _)) begin intro m, erw [norm_smul, mul_assoc], exact mul_le_mul_of_nonneg_left (le_op_norm _ _) (norm_nonneg _) end lemma op_norm_neg : ∥-f∥ = ∥f∥ := by { rw norm_def, apply congr_arg, ext, simp } /-- Continuous multilinear maps themselves form a normed space with respect to the operator norm. -/ instance to_normed_group : normed_group (continuous_multilinear_map 𝕜 E₁ E₂) := normed_group.of_core _ ⟨op_norm_zero_iff, op_norm_add_le, op_norm_neg⟩ instance to_normed_space : normed_space 𝕜' (continuous_multilinear_map 𝕜 E₁ E₂) := ⟨λ c f, f.op_norm_smul_le c⟩ section restrict_scalars variables [Π i, normed_space 𝕜' (E₁ i)] [∀ i, is_scalar_tower 𝕜' 𝕜 (E₁ i)] @[simp] lemma norm_restrict_scalars : ∥f.restrict_scalars 𝕜'∥ = ∥f∥ := by simp only [norm_def, coe_restrict_scalars] variable (𝕜') /-- `continuous_multilinear_map.restrict_scalars` as a `continuous_multilinear_map`. -/ def restrict_scalars_linear : continuous_multilinear_map 𝕜 E₁ E₂ →L[𝕜'] continuous_multilinear_map 𝕜' E₁ E₂ := linear_map.mk_continuous { to_fun := restrict_scalars 𝕜', map_add' := λ m₁ m₂, rfl, map_smul' := λ c m, rfl } 1 $ λ f, by simp variable {𝕜'} lemma continuous_restrict_scalars : continuous (restrict_scalars 𝕜' : continuous_multilinear_map 𝕜 E₁ E₂ → continuous_multilinear_map 𝕜' E₁ E₂) := (restrict_scalars_linear 𝕜').continuous end restrict_scalars /-- The difference `f m₁ - f m₂` is controlled in terms of `∥f∥` and `∥m₁ - m₂∥`, precise version. For a less precise but more usable version, see `norm_image_sub_le`. The bound reads `∥f m - f m'∥ ≤ ∥f∥ * ∥m 1 - m' 1∥ * max ∥m 2∥ ∥m' 2∥ * max ∥m 3∥ ∥m' 3∥ * ... * max ∥m n∥ ∥m' n∥ + ...`, where the other terms in the sum are the same products where `1` is replaced by any `i`.-/ lemma norm_image_sub_le' (m₁ m₂ : Πi, E₁ i) : ∥f m₁ - f m₂∥ ≤ ∥f∥ * ∑ i, ∏ j, if j = i then ∥m₁ i - m₂ i∥ else max ∥m₁ j∥ ∥m₂ j∥ := f.to_multilinear_map.norm_image_sub_le_of_bound' (norm_nonneg _) f.le_op_norm _ _ /-- The difference `f m₁ - f m₂` is controlled in terms of `∥f∥` and `∥m₁ - m₂∥`, less precise version. For a more precise but less usable version, see `norm_image_sub_le'`. The bound is `∥f m - f m'∥ ≤ ∥f∥ * card ι * ∥m - m'∥ * (max ∥m∥ ∥m'∥) ^ (card ι - 1)`.-/ lemma norm_image_sub_le (m₁ m₂ : Πi, E₁ i) : ∥f m₁ - f m₂∥ ≤ ∥f∥ * (fintype.card ι) * (max ∥m₁∥ ∥m₂∥) ^ (fintype.card ι - 1) * ∥m₁ - m₂∥ := f.to_multilinear_map.norm_image_sub_le_of_bound (norm_nonneg _) f.le_op_norm _ _ /-- Applying a multilinear map to a vector is continuous in both coordinates. -/ lemma continuous_eval : continuous (λ (p : (continuous_multilinear_map 𝕜 E₁ E₂ × (Πi, E₁ i))), p.1 p.2) := begin apply continuous_iff_continuous_at.2 (λp, _), apply continuous_at_of_locally_lipschitz zero_lt_one ((∥p∥ + 1) * (fintype.card ι) * (∥p∥ + 1) ^ (fintype.card ι - 1) + ∏ i, ∥p.2 i∥) (λq hq, _), have : 0 ≤ (max ∥q.2∥ ∥p.2∥), by simp, have : 0 ≤ ∥p∥ + 1, by simp [le_trans zero_le_one], have A : ∥q∥ ≤ ∥p∥ + 1 := norm_le_of_mem_closed_ball (le_of_lt hq), have : (max ∥q.2∥ ∥p.2∥) ≤ ∥p∥ + 1 := le_trans (max_le_max (norm_snd_le q) (norm_snd_le p)) (by simp [A, -add_comm, zero_le_one]), have : ∀ (i : ι), i ∈ univ → 0 ≤ ∥p.2 i∥ := λ i hi, norm_nonneg _, calc dist (q.1 q.2) (p.1 p.2) ≤ dist (q.1 q.2) (q.1 p.2) + dist (q.1 p.2) (p.1 p.2) : dist_triangle _ _ _ ... = ∥q.1 q.2 - q.1 p.2∥ + ∥q.1 p.2 - p.1 p.2∥ : by rw [dist_eq_norm, dist_eq_norm] ... ≤ ∥q.1∥ * (fintype.card ι) * (max ∥q.2∥ ∥p.2∥) ^ (fintype.card ι - 1) * ∥q.2 - p.2∥ + ∥q.1 - p.1∥ * ∏ i, ∥p.2 i∥ : add_le_add (norm_image_sub_le _ _ _) ((q.1 - p.1).le_op_norm p.2) ... ≤ (∥p∥ + 1) * (fintype.card ι) * (∥p∥ + 1) ^ (fintype.card ι - 1) * ∥q - p∥ + ∥q - p∥ * ∏ i, ∥p.2 i∥ : by apply_rules [add_le_add, mul_le_mul, le_refl, le_trans (norm_fst_le q) A, nat.cast_nonneg, mul_nonneg, pow_le_pow_of_le_left, pow_nonneg, norm_snd_le (q - p), norm_nonneg, norm_fst_le (q - p), prod_nonneg] ... = ((∥p∥ + 1) * (fintype.card ι) * (∥p∥ + 1) ^ (fintype.card ι - 1) + (∏ i, ∥p.2 i∥)) * dist q p : by { rw dist_eq_norm, ring } end lemma continuous_eval_left (m : Π i, E₁ i) : continuous (λ (p : (continuous_multilinear_map 𝕜 E₁ E₂)), (p : (Π i, E₁ i) → E₂) m) := continuous_eval.comp (continuous.prod_mk continuous_id continuous_const) lemma has_sum_eval {α : Type*} {p : α → continuous_multilinear_map 𝕜 E₁ E₂} {q : continuous_multilinear_map 𝕜 E₁ E₂} (h : has_sum p q) (m : Π i, E₁ i) : has_sum (λ a, p a m) (q m) := begin dsimp [has_sum] at h ⊢, convert ((continuous_eval_left m).tendsto _).comp h, ext s, simp end open_locale topological_space open filter /-- If the target space is complete, the space of continuous multilinear maps with its norm is also complete. The proof is essentially the same as for the space of continuous linear maps (modulo the addition of `finset.prod` where needed. The duplication could be avoided by deducing the linear case from the multilinear case via a currying isomorphism. However, this would mess up imports, and it is more satisfactory to have the simplest case as a standalone proof. -/ instance [complete_space E₂] : complete_space (continuous_multilinear_map 𝕜 E₁ E₂) := begin have nonneg : ∀ (v : Π i, E₁ i), 0 ≤ ∏ i, ∥v i∥ := λ v, finset.prod_nonneg (λ i hi, norm_nonneg _), -- We show that every Cauchy sequence converges. refine metric.complete_of_cauchy_seq_tendsto (λ f hf, _), -- We now expand out the definition of a Cauchy sequence, rcases cauchy_seq_iff_le_tendsto_0.1 hf with ⟨b, b0, b_bound, b_lim⟩, -- and establish that the evaluation at any point `v : Π i, E₁ i` is Cauchy. have cau : ∀ v, cauchy_seq (λ n, f n v), { assume v, apply cauchy_seq_iff_le_tendsto_0.2 ⟨λ n, b n * ∏ i, ∥v i∥, λ n, _, _, _⟩, { exact mul_nonneg (b0 n) (nonneg v) }, { assume n m N hn hm, rw dist_eq_norm, apply le_trans ((f n - f m).le_op_norm v) _, exact mul_le_mul_of_nonneg_right (b_bound n m N hn hm) (nonneg v) }, { simpa using b_lim.mul tendsto_const_nhds } }, -- We assemble the limits points of those Cauchy sequences -- (which exist as `E₂` is complete) -- into a function which we call `F`. choose F hF using λv, cauchy_seq_tendsto_of_complete (cau v), -- Next, we show that this `F` is multilinear, let Fmult : multilinear_map 𝕜 E₁ E₂ := { to_fun := F, map_add' := λ v i x y, begin have A := hF (function.update v i (x + y)), have B := (hF (function.update v i x)).add (hF (function.update v i y)), simp at A B, exact tendsto_nhds_unique A B end, map_smul' := λ v i c x, begin have A := hF (function.update v i (c • x)), have B := filter.tendsto.smul (@tendsto_const_nhds _ ℕ _ c _) (hF (function.update v i x)), simp at A B, exact tendsto_nhds_unique A B end }, -- and that `F` has norm at most `(b 0 + ∥f 0∥)`. have Fnorm : ∀ v, ∥F v∥ ≤ (b 0 + ∥f 0∥) * ∏ i, ∥v i∥, { assume v, have A : ∀ n, ∥f n v∥ ≤ (b 0 + ∥f 0∥) * ∏ i, ∥v i∥, { assume n, apply le_trans ((f n).le_op_norm _) _, apply mul_le_mul_of_nonneg_right _ (nonneg v), calc ∥f n∥ = ∥(f n - f 0) + f 0∥ : by { congr' 1, abel } ... ≤ ∥f n - f 0∥ + ∥f 0∥ : norm_add_le _ _ ... ≤ b 0 + ∥f 0∥ : begin apply add_le_add_right, simpa [dist_eq_norm] using b_bound n 0 0 (zero_le _) (zero_le _) end }, exact le_of_tendsto (hF v).norm (eventually_of_forall A) }, -- Thus `F` is continuous, and we propose that as the limit point of our original Cauchy sequence. let Fcont := Fmult.mk_continuous _ Fnorm, use Fcont, -- Our last task is to establish convergence to `F` in norm. have : ∀ n, ∥f n - Fcont∥ ≤ b n, { assume n, apply op_norm_le_bound _ (b0 n) (λ v, _), have A : ∀ᶠ m in at_top, ∥(f n - f m) v∥ ≤ b n * ∏ i, ∥v i∥, { refine eventually_at_top.2 ⟨n, λ m hm, _⟩, apply le_trans ((f n - f m).le_op_norm _) _, exact mul_le_mul_of_nonneg_right (b_bound n m n (le_refl _) hm) (nonneg v) }, have B : tendsto (λ m, ∥(f n - f m) v∥) at_top (𝓝 (∥(f n - Fcont) v∥)) := tendsto.norm (tendsto_const_nhds.sub (hF v)), exact le_of_tendsto B A }, erw tendsto_iff_norm_tendsto_zero, exact squeeze_zero (λ n, norm_nonneg _) this b_lim, end end continuous_multilinear_map /-- If a continuous multilinear map is constructed from a multilinear map via the constructor `mk_continuous`, then its norm is bounded by the bound given to the constructor if it is nonnegative. -/ lemma multilinear_map.mk_continuous_norm_le (f : multilinear_map 𝕜 E₁ E₂) {C : ℝ} (hC : 0 ≤ C) (H : ∀ m, ∥f m∥ ≤ C * ∏ i, ∥m i∥) : ∥f.mk_continuous C H∥ ≤ C := continuous_multilinear_map.op_norm_le_bound _ hC (λm, H m) namespace continuous_multilinear_map /-- Given a continuous multilinear map `f` on `n` variables (parameterized by `fin n`) and a subset `s` of `k` of these variables, one gets a new continuous multilinear map on `fin k` by varying these variables, and fixing the other ones equal to a given value `z`. It is denoted by `f.restr s hk z`, where `hk` is a proof that the cardinality of `s` is `k`. The implicit identification between `fin k` and `s` that we use is the canonical (increasing) bijection. -/ def restr {k n : ℕ} (f : (G [×n]→L[𝕜] E₂ : _)) (s : finset (fin n)) (hk : s.card = k) (z : G) : G [×k]→L[𝕜] E₂ := (f.to_multilinear_map.restr s hk z).mk_continuous (∥f∥ * ∥z∥^(n-k)) $ λ v, multilinear_map.restr_norm_le _ _ _ _ f.le_op_norm _ lemma norm_restr {k n : ℕ} (f : G [×n]→L[𝕜] E₂) (s : finset (fin n)) (hk : s.card = k) (z : G) : ∥f.restr s hk z∥ ≤ ∥f∥ * ∥z∥ ^ (n - k) := begin apply multilinear_map.mk_continuous_norm_le, exact mul_nonneg (norm_nonneg _) (pow_nonneg (norm_nonneg _) _) end section variables (𝕜 ι) (A : Type*) [normed_comm_ring A] [normed_algebra 𝕜 A] /-- The continuous multilinear map on `A^ι`, where `A` is a normed commutative algebra over `𝕜`, associating to `m` the product of all the `m i`. See also `continuous_multilinear_map.mk_pi_algebra_fin`. -/ protected def mk_pi_algebra : continuous_multilinear_map 𝕜 (λ i : ι, A) A := @multilinear_map.mk_continuous 𝕜 ι (λ i : ι, A) A _ _ _ _ _ _ _ (multilinear_map.mk_pi_algebra 𝕜 ι A) (if nonempty ι then 1 else ∥(1 : A)∥) $ begin intro m, by_cases hι : nonempty ι, { resetI, simp [hι, norm_prod_le' univ univ_nonempty] }, { simp [eq_empty_of_not_nonempty hι univ, hι] } end variables {A 𝕜 ι} @[simp] lemma mk_pi_algebra_apply (m : ι → A) : continuous_multilinear_map.mk_pi_algebra 𝕜 ι A m = ∏ i, m i := rfl lemma norm_mk_pi_algebra_le [nonempty ι] : ∥continuous_multilinear_map.mk_pi_algebra 𝕜 ι A∥ ≤ 1 := calc ∥continuous_multilinear_map.mk_pi_algebra 𝕜 ι A∥ ≤ if nonempty ι then 1 else ∥(1 : A)∥ : multilinear_map.mk_continuous_norm_le _ (by split_ifs; simp [zero_le_one]) _ ... = _ : if_pos ‹_› lemma norm_mk_pi_algebra_of_empty (h : ¬nonempty ι) : ∥continuous_multilinear_map.mk_pi_algebra 𝕜 ι A∥ = ∥(1 : A)∥ := begin apply le_antisymm, calc ∥continuous_multilinear_map.mk_pi_algebra 𝕜 ι A∥ ≤ if nonempty ι then 1 else ∥(1 : A)∥ : multilinear_map.mk_continuous_norm_le _ (by split_ifs; simp [zero_le_one]) _ ... = ∥(1 : A)∥ : if_neg ‹_›, convert ratio_le_op_norm _ (λ _, 1); [skip, apply_instance], simp [eq_empty_of_not_nonempty h univ] end @[simp] lemma norm_mk_pi_algebra [norm_one_class A] : ∥continuous_multilinear_map.mk_pi_algebra 𝕜 ι A∥ = 1 := begin by_cases hι : nonempty ι, { resetI, refine le_antisymm norm_mk_pi_algebra_le _, convert ratio_le_op_norm _ (λ _, 1); [skip, apply_instance], simp }, { simp [norm_mk_pi_algebra_of_empty hι] } end end section variables (𝕜 n) (A : Type*) [normed_ring A] [normed_algebra 𝕜 A] /-- The continuous multilinear map on `A^n`, where `A` is a normed algebra over `𝕜`, associating to `m` the product of all the `m i`. See also: `multilinear_map.mk_pi_algebra`. -/ protected def mk_pi_algebra_fin : continuous_multilinear_map 𝕜 (λ i : fin n, A) A := @multilinear_map.mk_continuous 𝕜 (fin n) (λ i : fin n, A) A _ _ _ _ _ _ _ (multilinear_map.mk_pi_algebra_fin 𝕜 n A) (nat.cases_on n ∥(1 : A)∥ (λ _, 1)) $ begin intro m, cases n, { simp }, { have : @list.of_fn A n.succ m ≠ [] := by simp, simpa [← fin.prod_of_fn] using list.norm_prod_le' this } end variables {A 𝕜 n} @[simp] lemma mk_pi_algebra_fin_apply (m : fin n → A) : continuous_multilinear_map.mk_pi_algebra_fin 𝕜 n A m = (list.of_fn m).prod := rfl lemma norm_mk_pi_algebra_fin_succ_le : ∥continuous_multilinear_map.mk_pi_algebra_fin 𝕜 n.succ A∥ ≤ 1 := multilinear_map.mk_continuous_norm_le _ zero_le_one _ lemma norm_mk_pi_algebra_fin_le_of_pos (hn : 0 < n) : ∥continuous_multilinear_map.mk_pi_algebra_fin 𝕜 n A∥ ≤ 1 := by cases n; [exact hn.false.elim, exact norm_mk_pi_algebra_fin_succ_le] lemma norm_mk_pi_algebra_fin_zero : ∥continuous_multilinear_map.mk_pi_algebra_fin 𝕜 0 A∥ = ∥(1 : A)∥ := begin refine le_antisymm (multilinear_map.mk_continuous_norm_le _ (norm_nonneg _) _) _, convert ratio_le_op_norm _ (λ _, 1); [simp, apply_instance] end lemma norm_mk_pi_algebra_fin [norm_one_class A] : ∥continuous_multilinear_map.mk_pi_algebra_fin 𝕜 n A∥ = 1 := begin cases n, { simp [norm_mk_pi_algebra_fin_zero] }, { refine le_antisymm norm_mk_pi_algebra_fin_succ_le _, convert ratio_le_op_norm _ (λ _, 1); [skip, apply_instance], simp } end end variables (𝕜 ι) /-- The canonical continuous multilinear map on `𝕜^ι`, associating to `m` the product of all the `m i` (multiplied by a fixed reference element `z` in the target module) -/ protected def mk_pi_field (z : E₂) : continuous_multilinear_map 𝕜 (λ(i : ι), 𝕜) E₂ := @multilinear_map.mk_continuous 𝕜 ι (λ(i : ι), 𝕜) E₂ _ _ _ _ _ _ _ (multilinear_map.mk_pi_ring 𝕜 ι z) (∥z∥) (λ m, by simp only [multilinear_map.mk_pi_ring_apply, norm_smul, normed_field.norm_prod, mul_comm]) variables {𝕜 ι} @[simp] lemma mk_pi_field_apply (z : E₂) (m : ι → 𝕜) : (continuous_multilinear_map.mk_pi_field 𝕜 ι z : (ι → 𝕜) → E₂) m = (∏ i, m i) • z := rfl lemma mk_pi_field_apply_one_eq_self (f : continuous_multilinear_map 𝕜 (λ(i : ι), 𝕜) E₂) : continuous_multilinear_map.mk_pi_field 𝕜 ι (f (λi, 1)) = f := to_multilinear_map_inj f.to_multilinear_map.mk_pi_ring_apply_one_eq_self variables (𝕜 ι E₂) /-- Continuous multilinear maps on `𝕜^n` with values in `E₂` are in bijection with `E₂`, as such a continuous multilinear map is completely determined by its value on the constant vector made of ones. We register this bijection as a linear equivalence in `continuous_multilinear_map.pi_field_equiv_aux`. The continuous linear equivalence is `continuous_multilinear_map.pi_field_equiv`. -/ protected def pi_field_equiv_aux : E₂ ≃ₗ[𝕜] (continuous_multilinear_map 𝕜 (λ(i : ι), 𝕜) E₂) := { to_fun := λ z, continuous_multilinear_map.mk_pi_field 𝕜 ι z, inv_fun := λ f, f (λi, 1), map_add' := λ z z', by { ext m, simp [smul_add] }, map_smul' := λ c z, by { ext m, simp [smul_smul, mul_comm] }, left_inv := λ z, by simp, right_inv := λ f, f.mk_pi_field_apply_one_eq_self } /-- Continuous multilinear maps on `𝕜^n` with values in `E₂` are in bijection with `E₂`, as such a continuous multilinear map is completely determined by its value on the constant vector made of ones. We register this bijection as a continuous linear equivalence in `continuous_multilinear_map.pi_field_equiv`. -/ protected def pi_field_equiv : E₂ ≃L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : ι), 𝕜) E₂) := { continuous_to_fun := begin refine (continuous_multilinear_map.pi_field_equiv_aux 𝕜 ι E₂).to_linear_map.continuous_of_bound (1 : ℝ) (λz, _), rw one_mul, change ∥continuous_multilinear_map.mk_pi_field 𝕜 ι z∥ ≤ ∥z∥, exact multilinear_map.mk_continuous_norm_le _ (norm_nonneg _) _ end, continuous_inv_fun := begin refine (continuous_multilinear_map.pi_field_equiv_aux 𝕜 ι E₂).symm.to_linear_map.continuous_of_bound (1 : ℝ) (λf, _), rw one_mul, change ∥f (λi, 1)∥ ≤ ∥f∥, apply @continuous_multilinear_map.unit_le_op_norm 𝕜 ι (λ (i : ι), 𝕜) E₂ _ _ _ _ _ _ _ f, simp [pi_norm_le_iff zero_le_one, le_refl] end, .. continuous_multilinear_map.pi_field_equiv_aux 𝕜 ι E₂ } end continuous_multilinear_map section currying /-! ### Currying We associate to a continuous multilinear map in `n+1` variables (i.e., based on `fin n.succ`) two curried functions, named `f.curry_left` (which is a continuous linear map on `E 0` taking values in continuous multilinear maps in `n` variables) and `f.curry_right` (which is a continuous multilinear map in `n` variables taking values in continuous linear maps on `E (last n)`). The inverse operations are called `uncurry_left` and `uncurry_right`. We also register continuous linear equiv versions of these correspondences, in `continuous_multilinear_curry_left_equiv` and `continuous_multilinear_curry_right_equiv`. -/ open fin function lemma continuous_linear_map.norm_map_tail_le (f : E 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), E i.succ) E₂)) (m : Πi, E i) : ∥f (m 0) (tail m)∥ ≤ ∥f∥ * ∏ i, ∥m i∥ := calc ∥f (m 0) (tail m)∥ ≤ ∥f (m 0)∥ * ∏ i, ∥(tail m) i∥ : (f (m 0)).le_op_norm _ ... ≤ (∥f∥ * ∥m 0∥) * ∏ i, ∥(tail m) i∥ : mul_le_mul_of_nonneg_right (f.le_op_norm _) (prod_nonneg (λi hi, norm_nonneg _)) ... = ∥f∥ * (∥m 0∥ * ∏ i, ∥(tail m) i∥) : by ring ... = ∥f∥ * ∏ i, ∥m i∥ : by { rw prod_univ_succ, refl } lemma continuous_multilinear_map.norm_map_init_le (f : continuous_multilinear_map 𝕜 (λ(i : fin n), E i.cast_succ) (E (last n) →L[𝕜] E₂)) (m : Πi, E i) : ∥f (init m) (m (last n))∥ ≤ ∥f∥ * ∏ i, ∥m i∥ := calc ∥f (init m) (m (last n))∥ ≤ ∥f (init m)∥ * ∥m (last n)∥ : (f (init m)).le_op_norm _ ... ≤ (∥f∥ * (∏ i, ∥(init m) i∥)) * ∥m (last n)∥ : mul_le_mul_of_nonneg_right (f.le_op_norm _) (norm_nonneg _) ... = ∥f∥ * ((∏ i, ∥(init m) i∥) * ∥m (last n)∥) : mul_assoc _ _ _ ... = ∥f∥ * ∏ i, ∥m i∥ : by { rw prod_univ_cast_succ, refl } lemma continuous_multilinear_map.norm_map_cons_le (f : continuous_multilinear_map 𝕜 E E₂) (x : E 0) (m : Π(i : fin n), E i.succ) : ∥f (cons x m)∥ ≤ ∥f∥ * ∥x∥ * ∏ i, ∥m i∥ := calc ∥f (cons x m)∥ ≤ ∥f∥ * ∏ i, ∥cons x m i∥ : f.le_op_norm _ ... = (∥f∥ * ∥x∥) * ∏ i, ∥m i∥ : by { rw prod_univ_succ, simp [mul_assoc] } lemma continuous_multilinear_map.norm_map_snoc_le (f : continuous_multilinear_map 𝕜 E E₂) (m : Π(i : fin n), E i.cast_succ) (x : E (last n)) : ∥f (snoc m x)∥ ≤ ∥f∥ * (∏ i, ∥m i∥) * ∥x∥ := calc ∥f (snoc m x)∥ ≤ ∥f∥ * ∏ i, ∥snoc m x i∥ : f.le_op_norm _ ... = ∥f∥ * (∏ i, ∥m i∥) * ∥x∥ : by { rw prod_univ_cast_succ, simp [mul_assoc] } /-! #### Left currying -/ /-- Given a continuous linear map `f` from `E 0` to continuous multilinear maps on `n` variables, construct the corresponding continuous multilinear map on `n+1` variables obtained by concatenating the variables, given by `m ↦ f (m 0) (tail m)`-/ def continuous_linear_map.uncurry_left (f : E 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), E i.succ) E₂)) : continuous_multilinear_map 𝕜 E E₂ := (@linear_map.uncurry_left 𝕜 n E E₂ _ _ _ _ _ (continuous_multilinear_map.to_multilinear_map_linear.comp f.to_linear_map)).mk_continuous (∥f∥) (λm, continuous_linear_map.norm_map_tail_le f m) @[simp] lemma continuous_linear_map.uncurry_left_apply (f : E 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), E i.succ) E₂)) (m : Πi, E i) : f.uncurry_left m = f (m 0) (tail m) := rfl /-- Given a continuous multilinear map `f` in `n+1` variables, split the first variable to obtain a continuous linear map into continuous multilinear maps in `n` variables, given by `x ↦ (m ↦ f (cons x m))`. -/ def continuous_multilinear_map.curry_left (f : continuous_multilinear_map 𝕜 E E₂) : E 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), E i.succ) E₂) := linear_map.mk_continuous { -- define a linear map into `n` continuous multilinear maps from an `n+1` continuous multilinear -- map to_fun := λx, (f.to_multilinear_map.curry_left x).mk_continuous (∥f∥ * ∥x∥) (f.norm_map_cons_le x), map_add' := λx y, by { ext m, exact f.cons_add m x y }, map_smul' := λc x, by { ext m, exact f.cons_smul m c x } } -- then register its continuity thanks to its boundedness properties. (∥f∥) (λx, multilinear_map.mk_continuous_norm_le _ (mul_nonneg (norm_nonneg _) (norm_nonneg _)) _) @[simp] lemma continuous_multilinear_map.curry_left_apply (f : continuous_multilinear_map 𝕜 E E₂) (x : E 0) (m : Π(i : fin n), E i.succ) : f.curry_left x m = f (cons x m) := rfl @[simp] lemma continuous_linear_map.curry_uncurry_left (f : E 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), E i.succ) E₂)) : f.uncurry_left.curry_left = f := begin ext m x, simp only [tail_cons, continuous_linear_map.uncurry_left_apply, continuous_multilinear_map.curry_left_apply], rw cons_zero end @[simp] lemma continuous_multilinear_map.uncurry_curry_left (f : continuous_multilinear_map 𝕜 E E₂) : f.curry_left.uncurry_left = f := by { ext m, simp } @[simp] lemma continuous_multilinear_map.curry_left_norm (f : continuous_multilinear_map 𝕜 E E₂) : ∥f.curry_left∥ = ∥f∥ := begin apply le_antisymm (linear_map.mk_continuous_norm_le _ (norm_nonneg _) _), have : ∥f.curry_left.uncurry_left∥ ≤ ∥f.curry_left∥ := multilinear_map.mk_continuous_norm_le _ (norm_nonneg _) _, simpa end @[simp] lemma continuous_linear_map.uncurry_left_norm (f : E 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), E i.succ) E₂)) : ∥f.uncurry_left∥ = ∥f∥ := begin apply le_antisymm (multilinear_map.mk_continuous_norm_le _ (norm_nonneg _) _), have : ∥f.uncurry_left.curry_left∥ ≤ ∥f.uncurry_left∥ := linear_map.mk_continuous_norm_le _ (norm_nonneg _) _, simpa end variables (𝕜 E E₂) /-- The space of continuous multilinear maps on `Π(i : fin (n+1)), E i` is canonically isomorphic to the space of continuous linear maps from `E 0` to the space of continuous multilinear maps on `Π(i : fin n), E i.succ `, by separating the first variable. We register this isomorphism as a linear isomorphism in `continuous_multilinear_curry_left_equiv_aux 𝕜 E E₂`. The algebraic version (without continuity assumption on the maps) is `multilinear_curry_left_equiv 𝕜 E E₂`, and the topological isomorphism (registering additionally that the isomorphism is continuous) is `continuous_multilinear_curry_left_equiv 𝕜 E E₂`. The direct and inverse maps are given by `f.uncurry_left` and `f.curry_left`. Use these unless you need the full framework of linear equivs. -/ def continuous_multilinear_curry_left_equiv_aux : (E 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), E i.succ) E₂)) ≃ₗ[𝕜] (continuous_multilinear_map 𝕜 E E₂) := { to_fun := continuous_linear_map.uncurry_left, map_add' := λf₁ f₂, by { ext m, refl }, map_smul' := λc f, by { ext m, refl }, inv_fun := continuous_multilinear_map.curry_left, left_inv := continuous_linear_map.curry_uncurry_left, right_inv := continuous_multilinear_map.uncurry_curry_left } /-- The space of continuous multilinear maps on `Π(i : fin (n+1)), E i` is canonically isomorphic to the space of continuous linear maps from `E 0` to the space of continuous multilinear maps on `Π(i : fin n), E i.succ `, by separating the first variable. We register this isomorphism in `continuous_multilinear_curry_left_equiv 𝕜 E E₂`. The algebraic version (without topology) is given in `multilinear_curry_left_equiv 𝕜 E E₂`. The direct and inverse maps are given by `f.uncurry_left` and `f.curry_left`. Use these unless you need the full framework of continuous linear equivs. -/ def continuous_multilinear_curry_left_equiv : (E 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), E i.succ) E₂)) ≃L[𝕜] (continuous_multilinear_map 𝕜 E E₂) := { continuous_to_fun := begin refine (continuous_multilinear_curry_left_equiv_aux 𝕜 E E₂).to_linear_map.continuous_of_bound (1 : ℝ) (λf, le_of_eq _), rw one_mul, exact f.uncurry_left_norm end, continuous_inv_fun := begin refine (continuous_multilinear_curry_left_equiv_aux 𝕜 E E₂).symm.to_linear_map.continuous_of_bound (1 : ℝ) (λf, le_of_eq _), rw one_mul, exact f.curry_left_norm end, .. continuous_multilinear_curry_left_equiv_aux 𝕜 E E₂ } variables {𝕜 E E₂} @[simp] lemma continuous_multilinear_curry_left_equiv_apply (f : E 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), E i.succ) E₂)) (v : Π i, E i) : continuous_multilinear_curry_left_equiv 𝕜 E E₂ f v = f (v 0) (tail v) := rfl @[simp] lemma continuous_multilinear_curry_left_equiv_symm_apply (f : continuous_multilinear_map 𝕜 E E₂) (x : E 0) (v : Π (i : fin n), E i.succ) : (continuous_multilinear_curry_left_equiv 𝕜 E E₂).symm f x v = f (cons x v) := rfl /-! #### Right currying -/ /-- Given a continuous linear map `f` from continuous multilinear maps on `n` variables to continuous linear maps on `E 0`, construct the corresponding continuous multilinear map on `n+1` variables obtained by concatenating the variables, given by `m ↦ f (init m) (m (last n))`. -/ def continuous_multilinear_map.uncurry_right (f : continuous_multilinear_map 𝕜 (λ(i : fin n), E i.cast_succ) (E (last n) →L[𝕜] E₂)) : continuous_multilinear_map 𝕜 E E₂ := let f' : multilinear_map 𝕜 (λ(i : fin n), E i.cast_succ) (E (last n) →ₗ[𝕜] E₂) := { to_fun := λ m, (f m).to_linear_map, map_add' := λ m i x y, by { simp, refl }, map_smul' := λ m i c x, by { simp, refl } } in (@multilinear_map.uncurry_right 𝕜 n E E₂ _ _ _ _ _ f').mk_continuous (∥f∥) (λm, f.norm_map_init_le m) @[simp] lemma continuous_multilinear_map.uncurry_right_apply (f : continuous_multilinear_map 𝕜 (λ(i : fin n), E i.cast_succ) (E (last n) →L[𝕜] E₂)) (m : Πi, E i) : f.uncurry_right m = f (init m) (m (last n)) := rfl /-- Given a continuous multilinear map `f` in `n+1` variables, split the last variable to obtain a continuous multilinear map in `n` variables into continuous linear maps, given by `m ↦ (x ↦ f (snoc m x))`. -/ def continuous_multilinear_map.curry_right (f : continuous_multilinear_map 𝕜 E E₂) : continuous_multilinear_map 𝕜 (λ(i : fin n), E i.cast_succ) (E (last n) →L[𝕜] E₂) := let f' : multilinear_map 𝕜 (λ(i : fin n), E i.cast_succ) (E (last n) →L[𝕜] E₂) := { to_fun := λm, (f.to_multilinear_map.curry_right m).mk_continuous (∥f∥ * ∏ i, ∥m i∥) $ λx, f.norm_map_snoc_le m x, map_add' := λ m i x y, by { simp, refl }, map_smul' := λ m i c x, by { simp, refl } } in f'.mk_continuous (∥f∥) (λm, linear_map.mk_continuous_norm_le _ (mul_nonneg (norm_nonneg _) (prod_nonneg (λj hj, norm_nonneg _))) _) @[simp] lemma continuous_multilinear_map.curry_right_apply (f : continuous_multilinear_map 𝕜 E E₂) (m : Π(i : fin n), E i.cast_succ) (x : E (last n)) : f.curry_right m x = f (snoc m x) := rfl @[simp] lemma continuous_multilinear_map.curry_uncurry_right (f : continuous_multilinear_map 𝕜 (λ(i : fin n), E i.cast_succ) (E (last n) →L[𝕜] E₂)) : f.uncurry_right.curry_right = f := begin ext m x, simp only [snoc_last, continuous_multilinear_map.curry_right_apply, continuous_multilinear_map.uncurry_right_apply], rw init_snoc end @[simp] lemma continuous_multilinear_map.uncurry_curry_right (f : continuous_multilinear_map 𝕜 E E₂) : f.curry_right.uncurry_right = f := by { ext m, simp } @[simp] lemma continuous_multilinear_map.curry_right_norm (f : continuous_multilinear_map 𝕜 E E₂) : ∥f.curry_right∥ = ∥f∥ := begin refine le_antisymm (multilinear_map.mk_continuous_norm_le _ (norm_nonneg _) _) _, have : ∥f.curry_right.uncurry_right∥ ≤ ∥f.curry_right∥ := multilinear_map.mk_continuous_norm_le _ (norm_nonneg _) _, simpa end @[simp] lemma continuous_multilinear_map.uncurry_right_norm (f : continuous_multilinear_map 𝕜 (λ(i : fin n), E i.cast_succ) (E (last n) →L[𝕜] E₂)) : ∥f.uncurry_right∥ = ∥f∥ := begin refine le_antisymm (multilinear_map.mk_continuous_norm_le _ (norm_nonneg _) _) _, have : ∥f.uncurry_right.curry_right∥ ≤ ∥f.uncurry_right∥ := multilinear_map.mk_continuous_norm_le _ (norm_nonneg _) _, simpa end variables (𝕜 E E₂) /-- The space of continuous multilinear maps on `Π(i : fin (n+1)), E i` is canonically isomorphic to the space of continuous multilinear maps on `Π(i : fin n), E i.cast_succ` with values in the space of continuous linear maps on `E (last n)`, by separating the last variable. We register this isomorphism as a linear equiv in `continuous_multilinear_curry_right_equiv_aux 𝕜 E E₂`. The algebraic version (without continuity assumption on the maps) is `multilinear_curry_right_equiv 𝕜 E E₂`, and the topological isomorphism (registering additionally that the isomorphism is continuous) is `continuous_multilinear_curry_right_equiv 𝕜 E E₂`. The direct and inverse maps are given by `f.uncurry_right` and `f.curry_right`. Use these unless you need the full framework of linear equivs. -/ def continuous_multilinear_curry_right_equiv_aux : (continuous_multilinear_map 𝕜 (λ(i : fin n), E i.cast_succ) (E (last n) →L[𝕜] E₂)) ≃ₗ[𝕜] (continuous_multilinear_map 𝕜 E E₂) := { to_fun := continuous_multilinear_map.uncurry_right, map_add' := λf₁ f₂, by { ext m, refl }, map_smul' := λc f, by { ext m, refl }, inv_fun := continuous_multilinear_map.curry_right, left_inv := continuous_multilinear_map.curry_uncurry_right, right_inv := continuous_multilinear_map.uncurry_curry_right } /-- The space of continuous multilinear maps on `Π(i : fin (n+1)), E i` is canonically isomorphic to the space of continuous multilinear maps on `Π(i : fin n), E i.cast_succ` with values in the space of continuous linear maps on `E (last n)`, by separating the last variable. We register this isomorphism as a continuous linear equiv in `continuous_multilinear_curry_right_equiv 𝕜 E E₂`. The algebraic version (without topology) is given in `multilinear_curry_right_equiv 𝕜 E E₂`. The direct and inverse maps are given by `f.uncurry_right` and `f.curry_right`. Use these unless you need the full framework of continuous linear equivs. -/ def continuous_multilinear_curry_right_equiv : (continuous_multilinear_map 𝕜 (λ(i : fin n), E i.cast_succ) (E (last n) →L[𝕜] E₂)) ≃L[𝕜] (continuous_multilinear_map 𝕜 E E₂) := { continuous_to_fun := begin refine (continuous_multilinear_curry_right_equiv_aux 𝕜 E E₂).to_linear_map.continuous_of_bound (1 : ℝ) (λf, le_of_eq _), rw one_mul, exact f.uncurry_right_norm end, continuous_inv_fun := begin refine (continuous_multilinear_curry_right_equiv_aux 𝕜 E E₂).symm.to_linear_map.continuous_of_bound (1 : ℝ) (λf, le_of_eq _), rw one_mul, exact f.curry_right_norm end, .. continuous_multilinear_curry_right_equiv_aux 𝕜 E E₂ } variables (n G) /-- The space of continuous multilinear maps on `Π(i : fin (n+1)), G` is canonically isomorphic to the space of continuous multilinear maps on `Π(i : fin n), G` with values in the space of continuous linear maps on `G`, by separating the last variable. We register this isomorphism as a continuous linear equiv in `continuous_multilinear_curry_right_equiv' 𝕜 n G E₂`. For a version allowing dependent types, see `continuous_multilinear_curry_right_equiv`. When there are no dependent types, use the primed version as it helps Lean a lot for unification. The direct and inverse maps are given by `f.uncurry_right` and `f.curry_right`. Use these unless you need the full framework of continuous linear equivs. -/ def continuous_multilinear_curry_right_equiv' : (continuous_multilinear_map 𝕜 (λ(i : fin n), G) (G →L[𝕜] E₂)) ≃L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n.succ), G) E₂) := continuous_multilinear_curry_right_equiv 𝕜 (λ (i : fin n.succ), G) E₂ variables {n 𝕜 G E E₂} @[simp] lemma continuous_multilinear_curry_right_equiv_apply (f : (continuous_multilinear_map 𝕜 (λ(i : fin n), E i.cast_succ) (E (last n) →L[𝕜] E₂))) (v : Π i, E i) : (continuous_multilinear_curry_right_equiv 𝕜 E E₂) f v = f (init v) (v (last n)) := rfl @[simp] lemma continuous_multilinear_curry_right_equiv_symm_apply (f : continuous_multilinear_map 𝕜 E E₂) (v : Π (i : fin n), E i.cast_succ) (x : E (last n)) : (continuous_multilinear_curry_right_equiv 𝕜 E E₂).symm f v x = f (snoc v x) := rfl @[simp] lemma continuous_multilinear_curry_right_equiv_apply' (f : (continuous_multilinear_map 𝕜 (λ(i : fin n), G) (G →L[𝕜] E₂))) (v : Π (i : fin n.succ), G) : (continuous_multilinear_curry_right_equiv' 𝕜 n G E₂) f v = f (init v) (v (last n)) := rfl @[simp] lemma continuous_multilinear_curry_right_equiv_symm_apply' (f : continuous_multilinear_map 𝕜 (λ(i : fin n.succ), G) E₂) (v : Π (i : fin n), G) (x : G) : (continuous_multilinear_curry_right_equiv' 𝕜 n G E₂).symm f v x = f (snoc v x) := rfl /-! #### Currying with `0` variables The space of multilinear maps with `0` variables is trivial: such a multilinear map is just an arbitrary constant (note that multilinear maps in `0` variables need not map `0` to `0`!). Therefore, the space of continuous multilinear maps on `(fin 0) → G` with values in `E₂` is isomorphic (and even isometric) to `E₂`. As this is the zeroth step in the construction of iterated derivatives, we register this isomorphism. -/ variables {𝕜 G E₂} /-- Associating to a continuous multilinear map in `0` variables the unique value it takes. -/ def continuous_multilinear_map.uncurry0 (f : continuous_multilinear_map 𝕜 (λ (i : fin 0), G) E₂) : E₂ := f 0 variables (𝕜 G) /-- Associating to an element `x` of a vector space `E₂` the continuous multilinear map in `0` variables taking the (unique) value `x` -/ def continuous_multilinear_map.curry0 (x : E₂) : continuous_multilinear_map 𝕜 (λ (i : fin 0), G) E₂ := { to_fun := λm, x, map_add' := λ m i, fin.elim0 i, map_smul' := λ m i, fin.elim0 i, cont := continuous_const } variable {G} @[simp] lemma continuous_multilinear_map.curry0_apply (x : E₂) (m : (fin 0) → G) : (continuous_multilinear_map.curry0 𝕜 G x : ((fin 0) → G) → E₂) m = x := rfl variable {𝕜} @[simp] lemma continuous_multilinear_map.uncurry0_apply (f : continuous_multilinear_map 𝕜 (λ (i : fin 0), G) E₂) : f.uncurry0 = f 0 := rfl @[simp] lemma continuous_multilinear_map.apply_zero_curry0 (f : continuous_multilinear_map 𝕜 (λ (i : fin 0), G) E₂) {x : fin 0 → G} : continuous_multilinear_map.curry0 𝕜 G (f x) = f := by { ext m, simp [(subsingleton.elim _ _ : x = m)] } lemma continuous_multilinear_map.uncurry0_curry0 (f : continuous_multilinear_map 𝕜 (λ (i : fin 0), G) E₂) : continuous_multilinear_map.curry0 𝕜 G (f.uncurry0) = f := by simp variables (𝕜 G) @[simp] lemma continuous_multilinear_map.curry0_uncurry0 (x : E₂) : (continuous_multilinear_map.curry0 𝕜 G x).uncurry0 = x := rfl @[simp] lemma continuous_multilinear_map.uncurry0_norm (x : E₂) : ∥continuous_multilinear_map.curry0 𝕜 G x∥ = ∥x∥ := begin apply le_antisymm, { exact continuous_multilinear_map.op_norm_le_bound _ (norm_nonneg _) (λm, by simp) }, { simpa using (continuous_multilinear_map.curry0 𝕜 G x).le_op_norm 0 } end variables {𝕜 G} @[simp] lemma continuous_multilinear_map.fin0_apply_norm (f : continuous_multilinear_map 𝕜 (λ (i : fin 0), G) E₂) {x : fin 0 → G} : ∥f x∥ = ∥f∥ := begin have : x = 0 := subsingleton.elim _ _, subst this, refine le_antisymm (by simpa using f.le_op_norm 0) _, have : ∥continuous_multilinear_map.curry0 𝕜 G (f.uncurry0)∥ ≤ ∥f.uncurry0∥ := continuous_multilinear_map.op_norm_le_bound _ (norm_nonneg _) (λm, by simp [-continuous_multilinear_map.apply_zero_curry0]), simpa end lemma continuous_multilinear_map.curry0_norm (f : continuous_multilinear_map 𝕜 (λ (i : fin 0), G) E₂) : ∥f.uncurry0∥ = ∥f∥ := by simp variables (𝕜 G E₂) /-- The linear isomorphism between elements of a normed space, and continuous multilinear maps in `0` variables with values in this normed space. The continuous version is given in `continuous_multilinear_curry_fin0`. The direct and inverse maps are `uncurry0` and `curry0`. Use these unless you need the full framework of linear equivs. -/ def continuous_multilinear_curry_fin0_aux : (continuous_multilinear_map 𝕜 (λ (i : fin 0), G) E₂) ≃ₗ[𝕜] E₂ := { to_fun := λf, continuous_multilinear_map.uncurry0 f, inv_fun := λf, continuous_multilinear_map.curry0 𝕜 G f, map_add' := λf g, rfl, map_smul' := λc f, rfl, left_inv := continuous_multilinear_map.uncurry0_curry0, right_inv := continuous_multilinear_map.curry0_uncurry0 𝕜 G } /-- The continuous linear isomorphism between elements of a normed space, and continuous multilinear maps in `0` variables with values in this normed space. The direct and inverse maps are `uncurry0` and `curry0`. Use these unless you need the full framework of continuous linear equivs. -/ def continuous_multilinear_curry_fin0 : (continuous_multilinear_map 𝕜 (λ (i : fin 0), G) E₂) ≃L[𝕜] E₂ := { continuous_to_fun := begin change continuous (λ (f : continuous_multilinear_map 𝕜 (λ (i : fin 0), G) E₂), (f : ((fin 0) → G) → E₂) 0), exact continuous_multilinear_map.continuous_eval.comp (continuous_id.prod_mk continuous_const) end, continuous_inv_fun := begin refine (continuous_multilinear_curry_fin0_aux 𝕜 G E₂).symm.to_linear_map.continuous_of_bound (1 : ℝ) (λf, le_of_eq _), rw one_mul, exact continuous_multilinear_map.uncurry0_norm _ _ _ end, .. continuous_multilinear_curry_fin0_aux 𝕜 G E₂ } variables {𝕜 G E₂} @[simp] lemma continuous_multilinear_curry_fin0_apply (f : (continuous_multilinear_map 𝕜 (λ (i : fin 0), G) E₂)) : continuous_multilinear_curry_fin0 𝕜 G E₂ f = f 0 := rfl @[simp] lemma continuous_multilinear_curry_fin0_symm_apply (x : E₂) (v : (fin 0) → G) : (continuous_multilinear_curry_fin0 𝕜 G E₂).symm x v = x := rfl /-! #### With 1 variable -/ variables (𝕜 G E₂) /-- Continuous multilinear maps from `G^1` to `E₂` are isomorphic with continuous linear maps from `G` to `E₂`. -/ def continuous_multilinear_curry_fin1 : (continuous_multilinear_map 𝕜 (λ (i : fin 1), G) E₂) ≃L[𝕜] (G →L[𝕜] E₂) := (continuous_multilinear_curry_right_equiv 𝕜 (λ (i : fin 1), G) E₂).symm.trans (continuous_multilinear_curry_fin0 𝕜 G (G →L[𝕜] E₂)) variables {𝕜 G E₂} @[simp] lemma continuous_multilinear_curry_fin1_apply (f : (continuous_multilinear_map 𝕜 (λ (i : fin 1), G) E₂)) (x : G) : continuous_multilinear_curry_fin1 𝕜 G E₂ f x = f (fin.snoc 0 x) := rfl @[simp] lemma continuous_multilinear_curry_fin1_symm_apply (f : G →L[𝕜] E₂) (v : (fin 1) → G) : (continuous_multilinear_curry_fin1 𝕜 G E₂).symm f v = f (v 0) := rfl end currying
c05ffca7a546594bdccf3b9792c64ddd247a8334
e00ea76a720126cf9f6d732ad6216b5b824d20a7
/src/computability/partrec_code.lean
b3478e80242c603a309644c83bd2305d7c8a3f8c
[ "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
37,980
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro Godel numbering for partial recursive functions. -/ import computability.partrec open encodable denumerable namespace nat.partrec open nat (mkpair) theorem rfind' {f} (hf : nat.partrec f) : nat.partrec (nat.unpaired (λ a m, (nat.rfind (λ n, (λ m, m = 0) <$> f (mkpair a (n + m)))).map (+ m))) := partrec₂.unpaired'.2 $ begin refine partrec.map ((@partrec₂.unpaired' (λ (a b : ℕ), nat.rfind (λ n, (λ m, m = 0) <$> f (mkpair a (n + b))))).1 _) (primrec.nat_add.comp primrec.snd $ primrec.snd.comp primrec.fst).to_comp.to₂, have := rfind (partrec₂.unpaired'.2 ((partrec.nat_iff.2 hf).comp (primrec₂.mkpair.comp (primrec.fst.comp $ primrec.unpair.comp primrec.fst) (primrec.nat_add.comp primrec.snd (primrec.snd.comp $ primrec.unpair.comp primrec.fst))).to_comp).to₂), simp at this, exact this end inductive code : Type | zero : code | succ : code | left : code | right : code | pair : code → code → code | comp : code → code → code | prec : code → code → code | rfind' : code → code end nat.partrec namespace nat.partrec.code open nat (mkpair unpair) open nat.partrec (code) instance : inhabited code := ⟨zero⟩ protected def const : ℕ → code | 0 := zero | (n+1) := comp succ (const n) protected def id : code := pair left right def curry (c : code) (n : ℕ) : code := comp c (pair (code.const n) code.id) def encode_code : code → ℕ | zero := 0 | succ := 1 | left := 2 | right := 3 | (pair cf cg) := bit0 (bit0 $ mkpair (encode_code cf) (encode_code cg)) + 4 | (comp cf cg) := bit0 (bit1 $ mkpair (encode_code cf) (encode_code cg)) + 4 | (prec cf cg) := bit1 (bit0 $ mkpair (encode_code cf) (encode_code cg)) + 4 | (rfind' cf) := bit1 (bit1 $ encode_code cf) + 4 def of_nat_code : ℕ → code | 0 := zero | 1 := succ | 2 := left | 3 := right | (n+4) := let m := n.div2.div2 in have hm : m < n + 4, by simp [m, nat.div2_val]; from lt_of_le_of_lt (le_trans (nat.div_le_self _ _) (nat.div_le_self _ _)) (nat.succ_le_succ (nat.le_add_right _ _)), have m1 : m.unpair.1 < n + 4, from lt_of_le_of_lt m.unpair_le_left hm, have m2 : m.unpair.2 < n + 4, from lt_of_le_of_lt m.unpair_le_right hm, match n.bodd, n.div2.bodd with | ff, ff := pair (of_nat_code m.unpair.1) (of_nat_code m.unpair.2) | ff, tt := comp (of_nat_code m.unpair.1) (of_nat_code m.unpair.2) | tt, ff := prec (of_nat_code m.unpair.1) (of_nat_code m.unpair.2) | tt, tt := rfind' (of_nat_code m) end private theorem encode_of_nat_code : ∀ n, encode_code (of_nat_code n) = n | 0 := rfl | 1 := rfl | 2 := rfl | 3 := rfl | (n+4) := let m := n.div2.div2 in have hm : m < n + 4, by simp [m, nat.div2_val]; from lt_of_le_of_lt (le_trans (nat.div_le_self _ _) (nat.div_le_self _ _)) (nat.succ_le_succ (nat.le_add_right _ _)), have m1 : m.unpair.1 < n + 4, from lt_of_le_of_lt m.unpair_le_left hm, have m2 : m.unpair.2 < n + 4, from lt_of_le_of_lt m.unpair_le_right hm, have IH : _ := encode_of_nat_code m, have IH1 : _ := encode_of_nat_code m.unpair.1, have IH2 : _ := encode_of_nat_code m.unpair.2, begin transitivity, swap, rw [← nat.bit_decomp n, ← nat.bit_decomp n.div2], simp [encode_code, of_nat_code, -add_comm], cases n.bodd; cases n.div2.bodd; simp [encode_code, of_nat_code, -add_comm, IH, IH1, IH2, m, nat.bit] end instance : denumerable code := mk' ⟨encode_code, of_nat_code, λ c, by induction c; try {refl}; simp [ encode_code, of_nat_code, -add_comm, *], encode_of_nat_code⟩ theorem encode_code_eq : encode = encode_code := rfl theorem of_nat_code_eq : of_nat code = of_nat_code := rfl theorem encode_lt_pair (cf cg) : encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := begin simp [encode_code_eq, encode_code, -add_comm], have := nat.mul_le_mul_right _ (dec_trivial : 1 ≤ 2*2), rw [one_mul, mul_assoc, ← bit0_eq_two_mul, ← bit0_eq_two_mul] at this, have := lt_of_le_of_lt this (lt_add_of_pos_right _ (dec_trivial:0<4)), exact ⟨ lt_of_le_of_lt (nat.le_mkpair_left _ _) this, lt_of_le_of_lt (nat.le_mkpair_right _ _) this⟩ end theorem encode_lt_comp (cf cg) : encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := begin suffices, exact (encode_lt_pair cf cg).imp (λ h, lt_trans h this) (λ h, lt_trans h this), change _, simp [encode_code_eq, encode_code, -add_comm], exact nat.bit0_lt (nat.lt_succ_self _), end theorem encode_lt_prec (cf cg) : encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := begin suffices, exact (encode_lt_pair cf cg).imp (λ h, lt_trans h this) (λ h, lt_trans h this), change _, simp [encode_code_eq, encode_code, -add_comm], exact nat.lt_succ_self _, end theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := begin simp [encode_code_eq, encode_code, -add_comm], have := nat.mul_le_mul_right _ (dec_trivial : 1 ≤ 2*2), rw [one_mul, mul_assoc, ← bit0_eq_two_mul, ← bit0_eq_two_mul] at this, refine lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (dec_trivial:0<4)), exact le_of_lt (nat.bit0_lt_bit1 $ le_of_lt $ nat.bit0_lt_bit1 $ le_refl _), end section open primrec theorem pair_prim : primrec₂ pair := primrec₂.of_nat_iff.2 $ primrec₂.encode_iff.1 $ nat_add.comp (nat_bit0.comp $ nat_bit0.comp $ primrec₂.mkpair.comp (encode_iff.2 $ (primrec.of_nat code).comp fst) (encode_iff.2 $ (primrec.of_nat code).comp snd)) (primrec₂.const 4) theorem comp_prim : primrec₂ comp := primrec₂.of_nat_iff.2 $ primrec₂.encode_iff.1 $ nat_add.comp (nat_bit0.comp $ nat_bit1.comp $ primrec₂.mkpair.comp (encode_iff.2 $ (primrec.of_nat code).comp fst) (encode_iff.2 $ (primrec.of_nat code).comp snd)) (primrec₂.const 4) theorem prec_prim : primrec₂ prec := primrec₂.of_nat_iff.2 $ primrec₂.encode_iff.1 $ nat_add.comp (nat_bit1.comp $ nat_bit0.comp $ primrec₂.mkpair.comp (encode_iff.2 $ (primrec.of_nat code).comp fst) (encode_iff.2 $ (primrec.of_nat code).comp snd)) (primrec₂.const 4) theorem rfind_prim : primrec rfind' := of_nat_iff.2 $ encode_iff.1 $ nat_add.comp (nat_bit1.comp $ nat_bit1.comp $ encode_iff.2 $ primrec.of_nat code) (const 4) theorem rec_prim' {α σ} [primcodable α] [primcodable σ] {c : α → code} (hc : primrec c) {z : α → σ} (hz : primrec z) {s : α → σ} (hs : primrec s) {l : α → σ} (hl : primrec l) {r : α → σ} (hr : primrec r) {pr : α → code × code × σ × σ → σ} (hpr : primrec₂ pr) {co : α → code × code × σ × σ → σ} (hco : primrec₂ co) {pc : α → code × code × σ × σ → σ} (hpc : primrec₂ pc) {rf : α → code × σ → σ} (hrf : primrec₂ rf) : let PR (a) := λ cf cg hf hg, pr a (cf, cg, hf, hg), CO (a) := λ cf cg hf hg, co a (cf, cg, hf, hg), PC (a) := λ cf cg hf hg, pc a (cf, cg, hf, hg), RF (a) := λ cf hf, rf a (cf, hf), F (a c) : σ := nat.partrec.code.rec_on c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a) in primrec (λ a, F a (c a)) := begin intros, let G₁ : (α × list σ) × ℕ × ℕ → option σ := λ p, let a := p.1.1, IH := p.1.2, n := p.2.1, m := p.2.2 in (IH.nth m).bind $ λ s, (IH.nth m.unpair.1).bind $ λ s₁, (IH.nth m.unpair.2).map $ λ s₂, cond n.bodd (cond n.div2.bodd (rf a (of_nat code m, s)) (pc a (of_nat code m.unpair.1, of_nat code m.unpair.2, s₁, s₂))) (cond n.div2.bodd (co a (of_nat code m.unpair.1, of_nat code m.unpair.2, s₁, s₂)) (pr a (of_nat code m.unpair.1, of_nat code m.unpair.2, s₁, s₂))), have : primrec G₁, { refine option_bind (list_nth.comp (snd.comp fst) (snd.comp snd)) _, refine option_bind ((list_nth.comp (snd.comp fst) (fst.comp $ primrec.unpair.comp (snd.comp snd))).comp fst) _, refine option_map ((list_nth.comp (snd.comp fst) (snd.comp $ primrec.unpair.comp (snd.comp snd))).comp $ fst.comp fst) _, have a := fst.comp (fst.comp $ fst.comp $ fst.comp fst), have n := fst.comp (snd.comp $ fst.comp $ fst.comp fst), have m := snd.comp (snd.comp $ fst.comp $ fst.comp fst), have m₁ := fst.comp (primrec.unpair.comp m), have m₂ := snd.comp (primrec.unpair.comp m), have s := snd.comp (fst.comp fst), have s₁ := snd.comp fst, have s₂ := snd, exact (nat_bodd.comp n).cond ((nat_bodd.comp $ nat_div2.comp n).cond (hrf.comp a (((primrec.of_nat code).comp m).pair s)) (hpc.comp a (((primrec.of_nat code).comp m₁).pair $ ((primrec.of_nat code).comp m₂).pair $ s₁.pair s₂))) (primrec.cond (nat_bodd.comp $ nat_div2.comp n) (hco.comp a (((primrec.of_nat code).comp m₁).pair $ ((primrec.of_nat code).comp m₂).pair $ s₁.pair s₂)) (hpr.comp a (((primrec.of_nat code).comp m₁).pair $ ((primrec.of_nat code).comp m₂).pair $ s₁.pair s₂))) }, let G : α → list σ → option σ := λ a IH, IH.length.cases (some (z a)) $ λ n, n.cases (some (s a)) $ λ n, n.cases (some (l a)) $ λ n, n.cases (some (r a)) $ λ n, G₁ ((a, IH), n, n.div2.div2), have : primrec₂ G := (nat_cases (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) $ nat_cases snd (option_some_iff.2 (hs.comp (fst.comp fst))) $ nat_cases snd (option_some_iff.2 (hl.comp (fst.comp $ fst.comp fst))) $ nat_cases snd (option_some_iff.2 (hr.comp (fst.comp $ fst.comp $ fst.comp fst))) (this.comp $ ((fst.pair snd).comp $ fst.comp $ fst.comp $ fst.comp $ fst).pair $ snd.pair $ nat_div2.comp $ nat_div2.comp snd)), refine ((nat_strong_rec (λ a n, F a (of_nat code n)) this.to₂ $ λ a n, _).comp primrec.id $ encode_iff.2 hc).of_eq (λ a, by simp), simp, iterate 4 {cases n with n, {refl}}, simp [G], rw [list.length_map, list.length_range], let m := n.div2.div2, show G₁ ((a, (list.range (n+4)).map (λ n, F a (of_nat code n))), n, m) = some (F a (of_nat code (n+4))), have hm : m < n + 4, by simp [nat.div2_val, m]; from lt_of_le_of_lt (le_trans (nat.div_le_self _ _) (nat.div_le_self _ _)) (nat.succ_le_succ (nat.le_add_right _ _)), have m1 : m.unpair.1 < n + 4, from lt_of_le_of_lt m.unpair_le_left hm, have m2 : m.unpair.2 < n + 4, from lt_of_le_of_lt m.unpair_le_right hm, simp [G₁], simp [list.nth_map, list.nth_range, hm, m1, m2], change of_nat code (n+4) with of_nat_code (n+4), simp [of_nat_code], cases n.bodd; cases n.div2.bodd; refl end theorem rec_prim {α σ} [primcodable α] [primcodable σ] {c : α → code} (hc : primrec c) {z : α → σ} (hz : primrec z) {s : α → σ} (hs : primrec s) {l : α → σ} (hl : primrec l) {r : α → σ} (hr : primrec r) {pr : α → code → code → σ → σ → σ} (hpr : primrec (λ a : α × code × code × σ × σ, pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)) {co : α → code → code → σ → σ → σ} (hco : primrec (λ a : α × code × code × σ × σ, co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)) {pc : α → code → code → σ → σ → σ} (hpc : primrec (λ a : α × code × code × σ × σ, pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)) {rf : α → code → σ → σ} (hrf : primrec (λ a : α × code × σ, rf a.1 a.2.1 a.2.2)) : let F (a c) : σ := nat.partrec.code.rec_on c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a) in primrec (λ a, F a (c a)) := begin intros, let G₁ : (α × list σ) × ℕ × ℕ → option σ := λ p, let a := p.1.1, IH := p.1.2, n := p.2.1, m := p.2.2 in (IH.nth m).bind $ λ s, (IH.nth m.unpair.1).bind $ λ s₁, (IH.nth m.unpair.2).map $ λ s₂, cond n.bodd (cond n.div2.bodd (rf a (of_nat code m) s) (pc a (of_nat code m.unpair.1) (of_nat code m.unpair.2) s₁ s₂)) (cond n.div2.bodd (co a (of_nat code m.unpair.1) (of_nat code m.unpair.2) s₁ s₂) (pr a (of_nat code m.unpair.1) (of_nat code m.unpair.2) s₁ s₂)), have : primrec G₁, { refine option_bind (list_nth.comp (snd.comp fst) (snd.comp snd)) _, refine option_bind ((list_nth.comp (snd.comp fst) (fst.comp $ primrec.unpair.comp (snd.comp snd))).comp fst) _, refine option_map ((list_nth.comp (snd.comp fst) (snd.comp $ primrec.unpair.comp (snd.comp snd))).comp $ fst.comp fst) _, have a := fst.comp (fst.comp $ fst.comp $ fst.comp fst), have n := fst.comp (snd.comp $ fst.comp $ fst.comp fst), have m := snd.comp (snd.comp $ fst.comp $ fst.comp fst), have m₁ := fst.comp (primrec.unpair.comp m), have m₂ := snd.comp (primrec.unpair.comp m), have s := snd.comp (fst.comp fst), have s₁ := snd.comp fst, have s₂ := snd, exact (nat_bodd.comp n).cond ((nat_bodd.comp $ nat_div2.comp n).cond (hrf.comp $ a.pair (((primrec.of_nat code).comp m).pair s)) (hpc.comp $ a.pair (((primrec.of_nat code).comp m₁).pair $ ((primrec.of_nat code).comp m₂).pair $ s₁.pair s₂))) (primrec.cond (nat_bodd.comp $ nat_div2.comp n) (hco.comp $ a.pair (((primrec.of_nat code).comp m₁).pair $ ((primrec.of_nat code).comp m₂).pair $ s₁.pair s₂)) (hpr.comp $ a.pair (((primrec.of_nat code).comp m₁).pair $ ((primrec.of_nat code).comp m₂).pair $ s₁.pair s₂))) }, let G : α → list σ → option σ := λ a IH, IH.length.cases (some (z a)) $ λ n, n.cases (some (s a)) $ λ n, n.cases (some (l a)) $ λ n, n.cases (some (r a)) $ λ n, G₁ ((a, IH), n, n.div2.div2), have : primrec₂ G := (nat_cases (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) $ nat_cases snd (option_some_iff.2 (hs.comp (fst.comp fst))) $ nat_cases snd (option_some_iff.2 (hl.comp (fst.comp $ fst.comp fst))) $ nat_cases snd (option_some_iff.2 (hr.comp (fst.comp $ fst.comp $ fst.comp fst))) (this.comp $ ((fst.pair snd).comp $ fst.comp $ fst.comp $ fst.comp $ fst).pair $ snd.pair $ nat_div2.comp $ nat_div2.comp snd)), refine ((nat_strong_rec (λ a n, F a (of_nat code n)) this.to₂ $ λ a n, _).comp primrec.id $ encode_iff.2 hc).of_eq (λ a, by simp), simp, iterate 4 {cases n with n, {refl}}, simp [G], rw [list.length_map, list.length_range], let m := n.div2.div2, show G₁ ((a, (list.range (n+4)).map (λ n, F a (of_nat code n))), n, m) = some (F a (of_nat code (n+4))), have hm : m < n + 4, by simp [nat.div2_val, m]; from lt_of_le_of_lt (le_trans (nat.div_le_self _ _) (nat.div_le_self _ _)) (nat.succ_le_succ (nat.le_add_right _ _)), have m1 : m.unpair.1 < n + 4, from lt_of_le_of_lt m.unpair_le_left hm, have m2 : m.unpair.2 < n + 4, from lt_of_le_of_lt m.unpair_le_right hm, simp [G₁], simp [list.nth_map, list.nth_range, hm, m1, m2], change of_nat code (n+4) with of_nat_code (n+4), simp [of_nat_code], cases n.bodd; cases n.div2.bodd; refl end end section open computable /- TODO(Mario): less copy-paste from previous proof -/ theorem rec_computable {α σ} [primcodable α] [primcodable σ] {c : α → code} (hc : computable c) {z : α → σ} (hz : computable z) {s : α → σ} (hs : computable s) {l : α → σ} (hl : computable l) {r : α → σ} (hr : computable r) {pr : α → code × code × σ × σ → σ} (hpr : computable₂ pr) {co : α → code × code × σ × σ → σ} (hco : computable₂ co) {pc : α → code × code × σ × σ → σ} (hpc : computable₂ pc) {rf : α → code × σ → σ} (hrf : computable₂ rf) : let PR (a) := λ cf cg hf hg, pr a (cf, cg, hf, hg), CO (a) := λ cf cg hf hg, co a (cf, cg, hf, hg), PC (a) := λ cf cg hf hg, pc a (cf, cg, hf, hg), RF (a) := λ cf hf, rf a (cf, hf), F (a c) : σ := nat.partrec.code.rec_on c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a) in computable (λ a, F a (c a)) := begin intros, let G₁ : (α × list σ) × ℕ × ℕ → option σ := λ p, let a := p.1.1, IH := p.1.2, n := p.2.1, m := p.2.2 in (IH.nth m).bind $ λ s, (IH.nth m.unpair.1).bind $ λ s₁, (IH.nth m.unpair.2).map $ λ s₂, cond n.bodd (cond n.div2.bodd (rf a (of_nat code m, s)) (pc a (of_nat code m.unpair.1, of_nat code m.unpair.2, s₁, s₂))) (cond n.div2.bodd (co a (of_nat code m.unpair.1, of_nat code m.unpair.2, s₁, s₂)) (pr a (of_nat code m.unpair.1, of_nat code m.unpair.2, s₁, s₂))), have : computable G₁, { refine option_bind (list_nth.comp (snd.comp fst) (snd.comp snd)) _, refine option_bind ((list_nth.comp (snd.comp fst) (fst.comp $ computable.unpair.comp (snd.comp snd))).comp fst) _, refine option_map ((list_nth.comp (snd.comp fst) (snd.comp $ computable.unpair.comp (snd.comp snd))).comp $ fst.comp fst) _, have a := fst.comp (fst.comp $ fst.comp $ fst.comp fst), have n := fst.comp (snd.comp $ fst.comp $ fst.comp fst), have m := snd.comp (snd.comp $ fst.comp $ fst.comp fst), have m₁ := fst.comp (computable.unpair.comp m), have m₂ := snd.comp (computable.unpair.comp m), have s := snd.comp (fst.comp fst), have s₁ := snd.comp fst, have s₂ := snd, exact (nat_bodd.comp n).cond ((nat_bodd.comp $ nat_div2.comp n).cond (hrf.comp a (((computable.of_nat code).comp m).pair s)) (hpc.comp a (((computable.of_nat code).comp m₁).pair $ ((computable.of_nat code).comp m₂).pair $ s₁.pair s₂))) (computable.cond (nat_bodd.comp $ nat_div2.comp n) (hco.comp a (((computable.of_nat code).comp m₁).pair $ ((computable.of_nat code).comp m₂).pair $ s₁.pair s₂)) (hpr.comp a (((computable.of_nat code).comp m₁).pair $ ((computable.of_nat code).comp m₂).pair $ s₁.pair s₂))) }, let G : α → list σ → option σ := λ a IH, IH.length.cases (some (z a)) $ λ n, n.cases (some (s a)) $ λ n, n.cases (some (l a)) $ λ n, n.cases (some (r a)) $ λ n, G₁ ((a, IH), n, n.div2.div2), have : computable₂ G := (nat_cases (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) $ nat_cases snd (option_some_iff.2 (hs.comp (fst.comp fst))) $ nat_cases snd (option_some_iff.2 (hl.comp (fst.comp $ fst.comp fst))) $ nat_cases snd (option_some_iff.2 (hr.comp (fst.comp $ fst.comp $ fst.comp fst))) (this.comp $ ((fst.pair snd).comp $ fst.comp $ fst.comp $ fst.comp $ fst).pair $ snd.pair $ nat_div2.comp $ nat_div2.comp snd)), refine ((nat_strong_rec (λ a n, F a (of_nat code n)) this.to₂ $ λ a n, _).comp computable.id $ encode_iff.2 hc).of_eq (λ a, by simp), simp, iterate 4 {cases n with n, {refl}}, simp [G], rw [list.length_map, list.length_range], let m := n.div2.div2, show G₁ ((a, (list.range (n+4)).map (λ n, F a (of_nat code n))), n, m) = some (F a (of_nat code (n+4))), have hm : m < n + 4, by simp [nat.div2_val, m]; from lt_of_le_of_lt (le_trans (nat.div_le_self _ _) (nat.div_le_self _ _)) (nat.succ_le_succ (nat.le_add_right _ _)), have m1 : m.unpair.1 < n + 4, from lt_of_le_of_lt m.unpair_le_left hm, have m2 : m.unpair.2 < n + 4, from lt_of_le_of_lt m.unpair_le_right hm, simp [G₁], simp [list.nth_map, list.nth_range, hm, m1, m2], change of_nat code (n+4) with of_nat_code (n+4), simp [of_nat_code], cases n.bodd; cases n.div2.bodd; refl end end def eval : code → ℕ →. ℕ | zero := pure 0 | succ := nat.succ | left := λ n, n.unpair.1 | right := λ n, n.unpair.2 | (pair cf cg) := λ n, mkpair <$> eval cf n <*> eval cg n | (comp cf cg) := λ n, eval cg n >>= eval cf | (prec cf cg) := nat.unpaired (λ a n, n.elim (eval cf a) (λ y IH, do i ← IH, eval cg (mkpair a (mkpair y i)))) | (rfind' cf) := nat.unpaired (λ a m, (nat.rfind (λ n, (λ m, m = 0) <$> eval cf (mkpair a (n + m)))).map (+ m)) instance : has_mem (ℕ →. ℕ) code := ⟨λ f c, eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (code.const n) m = roption.some n | 0 m := rfl | (n+1) m := by simp! * @[simp] theorem eval_id (n) : eval code.id n = roption.some n := by simp! [(<*>)] @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (mkpair n x) := by simp! [(<*>)] theorem const_prim : primrec code.const := (primrec.id.nat_iterate (primrec.const zero) (comp_prim.comp (primrec.const succ) primrec.snd).to₂).of_eq $ λ n, by simp; induction n; simp [*, code.const, nat.iterate_succ'] theorem curry_prim : primrec₂ curry := comp_prim.comp primrec.fst $ pair_prim.comp (const_prim.comp primrec.snd) (primrec.const code.id) theorem exists_code {f : ℕ →. ℕ} : nat.partrec f ↔ ∃ c : code, eval c = f := ⟨λ h, begin induction h, case nat.partrec.zero { exact ⟨zero, rfl⟩ }, case nat.partrec.succ { exact ⟨succ, rfl⟩ }, case nat.partrec.left { exact ⟨left, rfl⟩ }, case nat.partrec.right { exact ⟨right, rfl⟩ }, case nat.partrec.pair : f g pf pg hf hg { rcases hf with ⟨cf, rfl⟩, rcases hg with ⟨cg, rfl⟩, exact ⟨pair cf cg, rfl⟩ }, case nat.partrec.comp : f g pf pg hf hg { rcases hf with ⟨cf, rfl⟩, rcases hg with ⟨cg, rfl⟩, exact ⟨comp cf cg, rfl⟩ }, case nat.partrec.prec : f g pf pg hf hg { rcases hf with ⟨cf, rfl⟩, rcases hg with ⟨cg, rfl⟩, exact ⟨prec cf cg, rfl⟩ }, case nat.partrec.rfind : f pf hf { rcases hf with ⟨cf, rfl⟩, refine ⟨comp (rfind' cf) (pair code.id zero), _⟩, simp [eval, (<*>), pure, pfun.pure, roption.map_id'] }, end, λ h, begin rcases h with ⟨c, rfl⟩, induction c, case nat.partrec.code.zero { exact nat.partrec.zero }, case nat.partrec.code.succ { exact nat.partrec.succ }, case nat.partrec.code.left { exact nat.partrec.left }, case nat.partrec.code.right { exact nat.partrec.right }, case nat.partrec.code.pair : cf cg pf pg { exact pf.pair pg }, case nat.partrec.code.comp : cf cg pf pg { exact pf.comp pg }, case nat.partrec.code.prec : cf cg pf pg { exact pf.prec pg }, case nat.partrec.code.rfind' : cf pf { exact pf.rfind' }, end⟩ def evaln : ∀ k : ℕ, code → ℕ → option ℕ | 0 _ := λ m, none | (k+1) zero := λ n, guard (n ≤ k) >> pure 0 | (k+1) succ := λ n, guard (n ≤ k) >> pure (nat.succ n) | (k+1) left := λ n, guard (n ≤ k) >> pure n.unpair.1 | (k+1) right := λ n, guard (n ≤ k) >> pure n.unpair.2 | (k+1) (pair cf cg) := λ n, guard (n ≤ k) >> mkpair <$> evaln (k+1) cf n <*> evaln (k+1) cg n | (k+1) (comp cf cg) := λ n, guard (n ≤ k) >> do x ← evaln (k+1) cg n, evaln (k+1) cf x | (k+1) (prec cf cg) := λ n, guard (n ≤ k) >> n.unpaired (λ a n, n.cases (evaln (k+1) cf a) $ λ y, do i ← evaln k (prec cf cg) (mkpair a y), evaln (k+1) cg (mkpair a (mkpair y i))) | (k+1) (rfind' cf) := λ n, guard (n ≤ k) >> n.unpaired (λ a m, do x ← evaln (k+1) cf (mkpair a m), if x = 0 then pure m else evaln k (rfind' cf) (mkpair a (m+1))) using_well_founded wf_tacs theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0 c n x h := by simp [evaln] at h; cases h | (k+1) c n x h := begin suffices : ∀ {o : option ℕ}, x ∈ guard (n ≤ k) >> o → n < k + 1, { cases c; rw [evaln] at h; exact this h }, simp [(>>)], exact λ _ h _, nat.lt_succ_of_le h end theorem evaln_mono : ∀ {k₁ k₂ c n x}, k₁ ≤ k₂ → x ∈ evaln k₁ c n → x ∈ evaln k₂ c n | 0 k₂ c n x hl h := by simp [evaln] at h; cases h | (k+1) (k₂+1) c n x hl h := begin have hl' := nat.le_of_succ_le_succ hl, have : ∀ {k k₂ n x : ℕ} {o₁ o₂ : option ℕ}, k ≤ k₂ → (x ∈ o₁ → x ∈ o₂) → x ∈ guard (n ≤ k) >> o₁ → x ∈ guard (n ≤ k₂) >> o₂, { simp [(>>)], introv h h₁ h₂ h₃, exact ⟨le_trans h₂ h, h₁ h₃⟩ }, simp at h ⊢, induction c with cf cg hf hg cf cg hf hg cf cg hf hg cf hf generalizing x n; rw [evaln] at h ⊢; refine this hl' (λ h, _) h, iterate 4 {exact h}, { -- pair cf cg simp [(<*>)] at h ⊢, exact h.imp (λ a, and.imp (Exists.imp (λ b, and.imp_left (hf _ _))) (Exists.imp (λ b, and.imp_left (hg _ _)))) }, { -- comp cf cg simp at h ⊢, exact h.imp (λ a, and.imp (hg _ _) (hf _ _)) }, { -- prec cf cg revert h, simp, induction n.unpair.2; simp, { apply hf }, { exact λ y h₁ h₂, ⟨y, evaln_mono hl' h₁, hg _ _ h₂⟩ } }, { -- rfind' cf simp at h ⊢, refine h.imp (λ x, and.imp (hf _ _) _), by_cases x0 : x = 0; simp [x0], exact evaln_mono hl' } end theorem evaln_sound : ∀ {k c n x}, x ∈ evaln k c n → x ∈ eval c n | 0 _ n x h := by simp [evaln] at h; cases h | (k+1) c n x h := begin induction c with cf cg hf hg cf cg hf hg cf cg hf hg cf hf generalizing x n; simp [eval, evaln, (>>), (<*>)] at h ⊢; cases h with _ h, iterate 4 {simpa [pure, pfun.pure, eq_comm] using h}, { -- pair cf cg rcases h with ⟨_, ⟨y, ef, rfl⟩, z, eg, rfl⟩, exact ⟨_, hf _ _ ef, _, hg _ _ eg, rfl⟩ }, { --comp hf hg rcases h with ⟨y, eg, ef⟩, exact ⟨_, hg _ _ eg, hf _ _ ef⟩ }, { -- prec cf cg revert h, induction n.unpair.2 with m IH generalizing x; simp, { apply hf }, { refine λ y h₁ h₂, ⟨y, IH _ _, _⟩, { have := evaln_mono k.le_succ h₁, simp [evaln, (>>)] at this, exact this.2 }, { exact hg _ _ h₂ } } }, { -- rfind' cf rcases h with ⟨m, h₁, h₂⟩, by_cases m0 : m = 0; simp [m0] at h₂, { exact ⟨0, ⟨by simpa [m0] using hf _ _ h₁, λ m, (nat.not_lt_zero _).elim⟩, by injection h₂ with h₂; simp [h₂]⟩ }, { have := evaln_sound h₂, simp [eval] at this, rcases this with ⟨y, ⟨hy₁, hy₂⟩, rfl⟩, refine ⟨ y+1, ⟨by simpa [add_comm, add_left_comm] using hy₁, λ i im, _⟩, by simp [add_comm, add_left_comm] ⟩, cases i with i, { exact ⟨m, by simpa using hf _ _ h₁, m0⟩ }, { rcases hy₂ (nat.lt_of_succ_lt_succ im) with ⟨z, hz, z0⟩, exact ⟨z, by simpa [nat.succ_eq_add_one, add_comm, add_left_comm] using hz, z0⟩ } } } end theorem evaln_complete {c n x} : x ∈ eval c n ↔ ∃ k, x ∈ evaln k c n := ⟨λ h, begin suffices : ∃ k, x ∈ evaln (k+1) c n, { exact let ⟨k, h⟩ := this in ⟨k+1, h⟩ }, induction c generalizing n x; simp [eval, evaln, pure, pfun.pure, (<*>), (>>)] at h ⊢, iterate 4 { exact ⟨⟨_, le_refl _⟩, h.symm⟩ }, case nat.partrec.code.pair : cf cg hf hg { rcases h with ⟨x, hx, y, hy, rfl⟩, rcases hf hx with ⟨k₁, hk₁⟩, rcases hg hy with ⟨k₂, hk₂⟩, refine ⟨max k₁ k₂, _⟩, exact ⟨le_max_left_of_le $ nat.le_of_lt_succ $ evaln_bound hk₁, _, ⟨_, evaln_mono (nat.succ_le_succ $ le_max_left _ _) hk₁, rfl⟩, _, evaln_mono (nat.succ_le_succ $ le_max_right _ _) hk₂, rfl⟩ }, case nat.partrec.code.comp : cf cg hf hg { rcases h with ⟨y, hy, hx⟩, rcases hg hy with ⟨k₁, hk₁⟩, rcases hf hx with ⟨k₂, hk₂⟩, refine ⟨max k₁ k₂, _⟩, exact ⟨le_max_left_of_le $ nat.le_of_lt_succ $ evaln_bound hk₁, _, evaln_mono (nat.succ_le_succ $ le_max_left _ _) hk₁, evaln_mono (nat.succ_le_succ $ le_max_right _ _) hk₂⟩ }, case nat.partrec.code.prec : cf cg hf hg { revert h, generalize : n.unpair.1 = n₁, generalize : n.unpair.2 = n₂, induction n₂ with m IH generalizing x n; simp, { intro, rcases hf h with ⟨k, hk⟩, exact ⟨_, le_max_left _ _, evaln_mono (nat.succ_le_succ $ le_max_right _ _) hk⟩ }, { intros y hy hx, rcases IH hy with ⟨k₁, nk₁, hk₁⟩, rcases hg hx with ⟨k₂, hk₂⟩, refine ⟨(max k₁ k₂).succ, nat.le_succ_of_le $ le_max_left_of_le $ le_trans (le_max_left _ (mkpair n₁ m)) nk₁, y, evaln_mono (nat.succ_le_succ $ le_max_left _ _) _, evaln_mono (nat.succ_le_succ $ nat.le_succ_of_le $ le_max_right _ _) hk₂⟩, simp [evaln, (>>)], exact ⟨le_trans (le_max_right _ _) nk₁, hk₁⟩ } }, case nat.partrec.code.rfind' : cf hf { rcases h with ⟨y, ⟨hy₁, hy₂⟩, rfl⟩, suffices : ∃ k, y + n.unpair.2 ∈ evaln (k+1) (rfind' cf) (mkpair n.unpair.1 n.unpair.2), {simpa [evaln, (>>)]}, revert hy₁ hy₂, generalize : n.unpair.2 = m, intros, induction y with y IH generalizing m; simp [evaln, (>>)], { simp at hy₁, rcases hf hy₁ with ⟨k, hk⟩, exact ⟨_, nat.le_of_lt_succ $ evaln_bound hk, _, hk, by simp; refl⟩ }, { rcases hy₂ (nat.succ_pos _) with ⟨a, ha, a0⟩, rcases hf ha with ⟨k₁, hk₁⟩, rcases IH m.succ (by simpa [nat.succ_eq_add_one, add_comm, add_left_comm] using hy₁) (λ i hi, by simpa [nat.succ_eq_add_one, add_comm, add_left_comm] using hy₂ (nat.succ_lt_succ hi)) with ⟨k₂, hk₂⟩, use (max k₁ k₂).succ, rw [zero_add] at hk₁, use (nat.le_succ_of_le $ le_max_left_of_le $ nat.le_of_lt_succ $ evaln_bound hk₁), use a, use evaln_mono (nat.succ_le_succ $ nat.le_succ_of_le $ le_max_left _ _) hk₁, simpa [nat.succ_eq_add_one, a0, -max_eq_left, -max_eq_right, add_comm, add_left_comm] using evaln_mono (nat.succ_le_succ $ le_max_right _ _) hk₂ } } end, λ ⟨k, h⟩, evaln_sound h⟩ section open primrec private def lup (L : list (list (option ℕ))) (p : ℕ × code) (n : ℕ) := do l ← L.nth (encode p), o ← l.nth n, o private lemma hlup : primrec (λ p:_×(_×_)×_, lup p.1 p.2.1 p.2.2) := option_bind (list_nth.comp fst (primrec.encode.comp $ fst.comp snd)) (option_bind (list_nth.comp snd $ snd.comp $ snd.comp fst) snd) private def G (L : list (list (option ℕ))) : option (list (option ℕ)) := option.some $ let a := of_nat (ℕ × code) L.length, k := a.1, c := a.2 in (list.range k).map (λ n, k.cases none $ λ k', nat.partrec.code.rec_on c (some 0) -- zero (some (nat.succ n)) (some n.unpair.1) (some n.unpair.2) (λ cf cg _ _, do x ← lup L (k, cf) n, y ← lup L (k, cg) n, some (mkpair x y)) (λ cf cg _ _, do x ← lup L (k, cg) n, lup L (k, cf) x) (λ cf cg _ _, let z := n.unpair.1 in n.unpair.2.cases (lup L (k, cf) z) (λ y, do i ← lup L (k', c) (mkpair z y), lup L (k, cg) (mkpair z (mkpair y i)))) (λ cf _, let z := n.unpair.1, m := n.unpair.2 in do x ← lup L (k, cf) (mkpair z m), x.cases (some m) (λ _, lup L (k', c) (mkpair z (m+1))))) private lemma hG : primrec G := begin have a := (primrec.of_nat (ℕ × code)).comp list_length, have k := fst.comp a, refine option_some.comp (list_map (list_range.comp k) (_ : primrec _)), replace k := k.comp fst, have n := snd, refine nat_cases k (const none) (_ : primrec _), have k := k.comp fst, have n := n.comp fst, have k' := snd, have c := snd.comp (a.comp $ fst.comp fst), apply rec_prim c (const (some 0)) (option_some.comp (primrec.succ.comp n)) (option_some.comp (fst.comp $ primrec.unpair.comp n)) (option_some.comp (snd.comp $ primrec.unpair.comp n)), { have L := (fst.comp fst).comp fst, have k := k.comp fst, have n := n.comp fst, have cf := fst.comp snd, have cg := (fst.comp snd).comp snd, exact option_bind (hlup.comp $ L.pair $ (k.pair cf).pair n) (option_map ((hlup.comp $ L.pair $ (k.pair cg).pair n).comp fst) (primrec₂.mkpair.comp (snd.comp fst) snd)) }, { have L := (fst.comp fst).comp fst, have k := k.comp fst, have n := n.comp fst, have cf := fst.comp snd, have cg := (fst.comp snd).comp snd, exact option_bind (hlup.comp $ L.pair $ (k.pair cg).pair n) (hlup.comp ((L.comp fst).pair $ ((k.pair cf).comp fst).pair snd)) }, { have L := (fst.comp fst).comp fst, have k := k.comp fst, have n := n.comp fst, have cf := fst.comp snd, have cg := (fst.comp snd).comp snd, have z := fst.comp (primrec.unpair.comp n), refine nat_cases (snd.comp (primrec.unpair.comp n)) (hlup.comp $ L.pair $ (k.pair cf).pair z) (_ : primrec _), have L := L.comp fst, have z := z.comp fst, have y := snd, refine option_bind (hlup.comp $ L.pair $ (((k'.pair c).comp fst).comp fst).pair (primrec₂.mkpair.comp z y)) (_ : primrec _), have z := z.comp fst, have y := y.comp fst, have i := snd, exact hlup.comp ((L.comp fst).pair $ ((k.pair cg).comp $ fst.comp fst).pair $ primrec₂.mkpair.comp z $ primrec₂.mkpair.comp y i) }, { have L := (fst.comp fst).comp fst, have k := k.comp fst, have n := n.comp fst, have cf := fst.comp snd, have z := fst.comp (primrec.unpair.comp n), have m := snd.comp (primrec.unpair.comp n), refine option_bind (hlup.comp $ L.pair $ (k.pair cf).pair (primrec₂.mkpair.comp z m)) (_ : primrec _), have m := m.comp fst, exact nat_cases snd (option_some.comp m) ((hlup.comp ((L.comp fst).pair $ ((k'.pair c).comp $ fst.comp fst).pair (primrec₂.mkpair.comp (z.comp fst) (primrec.succ.comp m)))).comp fst) } end private lemma evaln_map (k c n) : (((list.range k).nth n).map (evaln k c)).bind (λ b, b) = evaln k c n := begin by_cases kn : n < k, { simp [list.nth_range kn] }, { rw list.nth_len_le, { cases e : evaln k c n, {refl}, exact kn.elim (evaln_bound e) }, simpa using kn } end theorem evaln_prim : primrec (λ (a : (ℕ × code) × ℕ), evaln a.1.1 a.1.2 a.2) := have primrec₂ (λ (_:unit) (n : ℕ), let a := of_nat (ℕ × code) n in (list.range a.1).map (evaln a.1 a.2)), from primrec.nat_strong_rec _ (hG.comp snd).to₂ $ λ _ p, begin simp [G], rw (_ : (of_nat (ℕ × code) _).snd = of_nat code p.unpair.2), swap, {simp}, apply list.map_congr (λ n, _), rw (by simp : list.range p = list.range (mkpair p.unpair.1 (encode (of_nat code p.unpair.2)))), generalize : p.unpair.1 = k, generalize : of_nat code p.unpair.2 = c, intro nk, cases k with k', {simp [evaln]}, let k := k'+1, change k'.succ with k, simp [nat.lt_succ_iff] at nk, have hg : ∀ {k' c' n}, mkpair k' (encode c') < mkpair k (encode c) → lup ((list.range (mkpair k (encode c))).map (λ n, (list.range n.unpair.1).map (evaln n.unpair.1 (of_nat code n.unpair.2)))) (k', c') n = evaln k' c' n, { intros k₁ c₁ n₁ hl, simp [lup, list.nth_range hl, evaln_map, (>>=)] }, cases c with cf cg cf cg cf cg cf; simp [evaln, nk, (>>), (>>=), (<$>), (<*>), pure], { cases encode_lt_pair cf cg with lf lg, rw [hg (nat.mkpair_lt_mkpair_right _ lf), hg (nat.mkpair_lt_mkpair_right _ lg)], cases evaln k cf n, {refl}, cases evaln k cg n; refl }, { cases encode_lt_comp cf cg with lf lg, rw hg (nat.mkpair_lt_mkpair_right _ lg), cases evaln k cg n, {refl}, simp [hg (nat.mkpair_lt_mkpair_right _ lf)] }, { cases encode_lt_prec cf cg with lf lg, rw hg (nat.mkpair_lt_mkpair_right _ lf), cases n.unpair.2, {refl}, simp, rw hg (nat.mkpair_lt_mkpair_left _ k'.lt_succ_self), cases evaln k' _ _, {refl}, simp [hg (nat.mkpair_lt_mkpair_right _ lg)] }, { have lf := encode_lt_rfind' cf, rw hg (nat.mkpair_lt_mkpair_right _ lf), cases evaln k cf n with x, {refl}, simp, cases x; simp [nat.succ_ne_zero], rw hg (nat.mkpair_lt_mkpair_left _ k'.lt_succ_self) } end, (option_bind (list_nth.comp (this.comp (const ()) (encode_iff.2 fst)) snd) snd.to₂).of_eq $ λ ⟨⟨k, c⟩, n⟩, by simp [evaln_map] end section open partrec computable theorem eval_eq_rfind_opt (c n) : eval c n = nat.rfind_opt (λ k, evaln k c n) := roption.ext $ λ x, begin refine evaln_complete.trans (nat.rfind_opt_mono _).symm, intros a m n hl, apply evaln_mono hl, end theorem eval_part : partrec₂ eval := (rfind_opt (evaln_prim.to_comp.comp ((snd.pair (fst.comp fst)).pair (snd.comp fst))).to₂).of_eq $ λ a, by simp [eval_eq_rfind_opt] theorem fixed_point {f : code → code} (hf : computable f) : ∃ c : code, eval (f c) = eval c := let g (x y : ℕ) : roption ℕ := eval (of_nat code x) x >>= λ b, eval (of_nat code b) y in have partrec₂ g := (eval_part.comp ((computable.of_nat _).comp fst) fst).bind (eval_part.comp ((computable.of_nat _).comp snd) (snd.comp fst)).to₂, let ⟨cg, eg⟩ := exists_code.1 this in have eg' : ∀ a n, eval cg (mkpair a n) = roption.map encode (g a n) := by simp [eg], let F (x : ℕ) : code := f (curry cg x) in have computable F := hf.comp (curry_prim.comp (primrec.const cg) primrec.id).to_comp, let ⟨cF, eF⟩ := exists_code.1 this in have eF' : eval cF (encode cF) = roption.some (encode (F (encode cF))), by simp [eF], ⟨curry cg (encode cF), funext (λ n, show eval (f (curry cg (encode cF))) n = eval (curry cg (encode cF)) n, by simp [eg', eF', roption.map_id', g])⟩ theorem fixed_point₂ {f : code → ℕ →. ℕ} (hf : partrec₂ f) : ∃ c : code, eval c = f c := let ⟨cf, ef⟩ := exists_code.1 hf in (fixed_point (curry_prim.comp (primrec.const cf) primrec.encode).to_comp).imp $ λ c e, funext $ λ n, by simp [e.symm, ef, roption.map_id'] end end nat.partrec.code
9480cc8250d7480c715cb4ef909d3f0f9f038157
a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940
/src/Init/Data/Nat/Basic.lean
2f115007c83ac8deb7442aa1b5bad12013361360
[ "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
13,676
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Leonardo de Moura -/ prelude import Init.SimpLemmas universe u namespace Nat @[specialize] def foldAux {α : Type u} (f : Nat → α → α) (s : Nat) : Nat → α → α | 0, a => a | succ n, a => foldAux f s n (f (s - (succ n)) a) @[inline] def fold {α : Type u} (f : Nat → α → α) (n : Nat) (init : α) : α := foldAux f n n init @[inline] def foldRev {α : Type u} (f : Nat → α → α) (n : Nat) (init : α) : α := let rec @[specialize] loop | 0, a => a | succ n, a => loop n (f n a) loop n init @[specialize] def anyAux (f : Nat → Bool) (s : Nat) : Nat → Bool | 0 => false | succ n => f (s - (succ n)) || anyAux f s n /- `any f n = true` iff there is `i in [0, n-1]` s.t. `f i = true` -/ @[inline] def any (f : Nat → Bool) (n : Nat) : Bool := anyAux f n n @[inline] def all (f : Nat → Bool) (n : Nat) : Bool := !any (fun i => !f i) n @[inline] def repeat {α : Type u} (f : α → α) (n : Nat) (a : α) : α := let rec @[specialize] loop | 0, a => a | succ n, a => loop n (f a) loop n a /- Nat.add theorems -/ @[simp] theorem zero_Eq : Nat.zero = 0 := rfl @[simp] protected theorem zero_add : ∀ (n : Nat), 0 + n = n | 0 => rfl | n+1 => congrArg succ (Nat.zero_add n) theorem succ_add : ∀ (n m : Nat), (succ n) + m = succ (n + m) | n, 0 => rfl | n, m+1 => congrArg succ (succ_add n m) theorem add_succ (n m : Nat) : n + succ m = succ (n + m) := rfl @[simp] protected theorem add_zero (n : Nat) : n + 0 = n := rfl theorem add_one (n : Nat) : n + 1 = succ n := rfl theorem succ_Eq_add_one (n : Nat) : succ n = n + 1 := rfl protected theorem add_comm : ∀ (n m : Nat), n + m = m + n | n, 0 => Eq.symm (Nat.zero_add n) | n, m+1 => by have : succ (n + m) = succ (m + n) := by apply congrArg; apply Nat.add_comm rw [succ_add m n] apply this protected theorem add_assoc : ∀ (n m k : Nat), (n + m) + k = n + (m + k) | n, m, 0 => rfl | n, m, succ k => congrArg succ (Nat.add_assoc n m k) protected theorem add_left_comm (n m k : Nat) : n + (m + k) = m + (n + k) := by rw [← Nat.add_assoc, Nat.add_comm n m, Nat.add_assoc] protected theorem add_right_comm (n m k : Nat) : (n + m) + k = (n + k) + m := by rw [Nat.add_assoc, Nat.add_comm m k, ← Nat.add_assoc] protected theorem add_left_cancel {n m k : Nat} : n + m = n + k → m = k := by induction n with | zero => simp; intros; assumption | succ n ih => simp [succ_add]; intro h; apply ih h protected theorem add_right_cancel {n m k : Nat} (h : n + m = k + m) : n = k := by rw [Nat.add_comm n m, Nat.add_comm k m] at h apply Nat.add_left_cancel h /- Nat.mul theorems -/ @[simp] protected theorem mul_zero (n : Nat) : n * 0 = 0 := rfl theorem mul_succ (n m : Nat) : n * succ m = n * m + n := rfl @[simp] protected theorem zero_mul : ∀ (n : Nat), 0 * n = 0 | 0 => rfl | succ n => mul_succ 0 n ▸ (Nat.zero_mul n).symm ▸ rfl theorem succ_mul (n m : Nat) : (succ n) * m = (n * m) + m := by induction m with | zero => rfl | succ m ih => rw [mul_succ, add_succ, ih, mul_succ, add_succ, Nat.add_right_comm] protected theorem mul_comm : ∀ (n m : Nat), n * m = m * n | n, 0 => (Nat.zero_mul n).symm ▸ (Nat.mul_zero n).symm ▸ rfl | n, succ m => (mul_succ n m).symm ▸ (succ_mul m n).symm ▸ (Nat.mul_comm n m).symm ▸ rfl @[simp] protected theorem mul_one : ∀ (n : Nat), n * 1 = n := Nat.zero_add @[simp] protected theorem one_mul (n : Nat) : 1 * n = n := Nat.mul_comm n 1 ▸ Nat.mul_one n protected theorem left_distrib (n m k : Nat) : n * (m + k) = n * m + n * k := by induction n generalizing m k with | zero => repeat rw [Nat.zero_mul] | succ n ih => simp [succ_mul, ih]; rw [Nat.add_assoc, Nat.add_assoc (n*m)]; apply congrArg; apply Nat.add_left_comm protected theorem right_distrib (n m k : Nat) : (n + m) * k = n * k + m * k := have h₁ : (n + m) * k = k * (n + m) := Nat.mul_comm .. have h₂ : k * (n + m) = k * n + k * m := Nat.left_distrib .. have h₃ : k * n + k * m = n * k + k * m := Nat.mul_comm n k ▸ rfl have h₄ : n * k + k * m = n * k + m * k := Nat.mul_comm m k ▸ rfl ((h₁.trans h₂).trans h₃).trans h₄ protected theorem mul_assoc : ∀ (n m k : Nat), (n * m) * k = n * (m * k) | n, m, 0 => rfl | n, m, succ k => have h₁ : n * m * succ k = n * m * (k + 1) := rfl have h₂ : n * m * (k + 1) = (n * m * k) + n * m * 1 := Nat.left_distrib .. have h₃ : (n * m * k) + n * m * 1 = (n * m * k) + n * m := by rw [Nat.mul_one (n*m)] have h₄ : (n * m * k) + n * m = (n * (m * k)) + n * m := by rw [Nat.mul_assoc n m k] have h₅ : (n * (m * k)) + n * m = n * (m * k + m) := (Nat.left_distrib n (m*k) m).symm have h₆ : n * (m * k + m) = n * (m * succ k) := Nat.mul_succ m k ▸ rfl ((((h₁.trans h₂).trans h₃).trans h₄).trans h₅).trans h₆ /- Inequalities -/ theorem succ_lt_succ {n m : Nat} : n < m → succ n < succ m := succLeSucc theorem lt_succ_of_le {n m : Nat} : n ≤ m → n < succ m := succLeSucc @[simp] protected theorem sub_zero (n : Nat) : n - 0 = n := rfl theorem succ_sub_succ_eq_sub (n m : Nat) : succ n - succ m = n - m := by induction m with | zero => exact rfl | succ m ih => apply congrArg pred ih theorem notSuccLeSelf (n : Nat) : ¬succ n ≤ n := by induction n with | zero => intro h; apply notSuccLeZero 0 h | succ n ih => intro h; exact ih (leOfSuccLeSucc h) protected theorem ltIrrefl (n : Nat) : ¬n < n := notSuccLeSelf n theorem predLe : ∀ (n : Nat), pred n ≤ n | zero => rfl | succ n => leSucc _ theorem predLt : ∀ {n : Nat}, n ≠ 0 → pred n < n | zero, h => absurd rfl h | succ n, h => lt_succ_of_le (Nat.leRefl _) theorem subLe (n m : Nat) : n - m ≤ n := by induction m with | zero => exact Nat.leRefl (n - 0) | succ m ih => apply Nat.leTrans (predLe (n - m)) ih theorem subLt : ∀ {n m : Nat}, 0 < n → 0 < m → n - m < n | 0, m, h1, h2 => absurd h1 (Nat.ltIrrefl 0) | n+1, 0, h1, h2 => absurd h2 (Nat.ltIrrefl 0) | n+1, m+1, h1, h2 => Eq.symm (succ_sub_succ_eq_sub n m) ▸ show n - m < succ n from lt_succ_of_le (subLe n m) theorem sub_succ (n m : Nat) : n - succ m = pred (n - m) := rfl theorem succ_sub_succ (n m : Nat) : succ n - succ m = n - m := succ_sub_succ_eq_sub n m protected theorem sub_self : ∀ (n : Nat), n - n = 0 | 0 => by rw [Nat.sub_zero] | (succ n) => by rw [succ_sub_succ, Nat.sub_self n] protected theorem ltOfLtOfLe {n m k : Nat} : n < m → m ≤ k → n < k := Nat.leTrans protected theorem ltOfLtOfEq {n m k : Nat} : n < m → m = k → n < k := fun h₁ h₂ => h₂ ▸ h₁ protected theorem leOfEq {n m : Nat} (p : n = m) : n ≤ m := p ▸ Nat.leRefl n theorem leOfSuccLe {n m : Nat} (h : succ n ≤ m) : n ≤ m := Nat.leTrans (leSucc n) h protected theorem leOfLt {n m : Nat} (h : n < m) : n ≤ m := leOfSuccLe h def lt.step {n m : Nat} : n < m → n < succ m := leStep def succPos := zeroLtSucc theorem eqZeroOrPos : ∀ (n : Nat), n = 0 ∨ n > 0 | 0 => Or.inl rfl | n+1 => Or.inr (succPos _) protected theorem ltOfLeOfLt {n m k : Nat} (h₁ : n ≤ m) : m < k → n < k := Nat.leTrans (succLeSucc h₁) def lt.base (n : Nat) : n < succ n := Nat.leRefl (succ n) theorem ltSuccSelf (n : Nat) : n < succ n := lt.base n protected theorem leTotal (m n : Nat) : m ≤ n ∨ n ≤ m := match Nat.ltOrGe m n with | Or.inl h => Or.inl (Nat.leOfLt h) | Or.inr h => Or.inr h protected theorem ltOfLeAndNe {m n : Nat} (h₁ : m ≤ n) (h₂ : m ≠ n) : m < n := match Nat.eqOrLtOfLe h₁ with | Or.inl h => absurd h h₂ | Or.inr h => h theorem eqZeroOfLeZero {n : Nat} (h : n ≤ 0) : n = 0 := Nat.leAntisymm h (zeroLe _) theorem ltOfSuccLt {n m : Nat} : succ n < m → n < m := leOfSuccLe theorem lt_of_succ_lt_succ {n m : Nat} : succ n < succ m → n < m := leOfSuccLeSucc theorem ltOfSuccLe {n m : Nat} (h : succ n ≤ m) : n < m := h theorem succLeOfLt {n m : Nat} (h : n < m) : succ n ≤ m := h theorem ltOrEqOrLeSucc {m n : Nat} (h : m ≤ succ n) : m ≤ n ∨ m = succ n := Decidable.byCases (fun (h' : m = succ n) => Or.inr h') (fun (h' : m ≠ succ n) => have : m < succ n := Nat.ltOfLeAndNe h h' have : succ m ≤ succ n := succLeOfLt this Or.inl (leOfSuccLeSucc this)) theorem leAddRight : ∀ (n k : Nat), n ≤ n + k | n, 0 => Nat.leRefl n | n, k+1 => leSuccOfLe (leAddRight n k) theorem leAddLeft (n m : Nat): n ≤ m + n := Nat.add_comm n m ▸ leAddRight n m theorem le.dest : ∀ {n m : Nat}, n ≤ m → Exists (fun k => n + k = m) | zero, zero, h => ⟨0, rfl⟩ | zero, succ n, h => ⟨succ n, Nat.add_comm 0 (succ n) ▸ rfl⟩ | succ n, zero, h => Bool.noConfusion h | succ n, succ m, h => have : n ≤ m := h have : Exists (fun k => n + k = m) := dest this match this with | ⟨k, h⟩ => ⟨k, show succ n + k = succ m from ((succ_add n k).symm ▸ h ▸ rfl)⟩ theorem le.intro {n m k : Nat} (h : n + k = m) : n ≤ m := h ▸ leAddRight n k protected theorem notLeOfGt {n m : Nat} (h : n > m) : ¬ n ≤ m := fun h₁ => match Nat.ltOrGe n m with | Or.inl h₂ => absurd (Nat.ltTrans h h₂) (Nat.ltIrrefl _) | Or.inr h₂ => have Heq : n = m := Nat.leAntisymm h₁ h₂ absurd (@Eq.subst _ _ _ _ Heq h) (Nat.ltIrrefl m) theorem gtOfNotLe {n m : Nat} (h : ¬ n ≤ m) : n > m := match Nat.ltOrGe m n with | Or.inl h₁ => h₁ | Or.inr h₁ => absurd h₁ h protected theorem addLeAddLeft {n m : Nat} (h : n ≤ m) (k : Nat) : k + n ≤ k + m := match le.dest h with | ⟨w, hw⟩ => have h₁ : k + n + w = k + (n + w) := Nat.add_assoc .. have h₂ : k + (n + w) = k + m := congrArg _ hw le.intro <| h₁.trans h₂ protected theorem addLeAddRight {n m : Nat} (h : n ≤ m) (k : Nat) : n + k ≤ m + k := by rw [Nat.add_comm n k, Nat.add_comm m k] apply Nat.addLeAddLeft assumption protected theorem addLtAddLeft {n m : Nat} (h : n < m) (k : Nat) : k + n < k + m := ltOfSuccLe (add_succ k n ▸ Nat.addLeAddLeft (succLeOfLt h) k) protected theorem addLtAddRight {n m : Nat} (h : n < m) (k : Nat) : n + k < m + k := Nat.add_comm k m ▸ Nat.add_comm k n ▸ Nat.addLtAddLeft h k protected theorem zeroLtOne : 0 < (1:Nat) := zeroLtSucc 0 theorem addLeAdd {a b c d : Nat} (h₁ : a ≤ b) (h₂ : c ≤ d) : a + c ≤ b + d := Nat.leTrans (Nat.addLeAddRight h₁ c) (Nat.addLeAddLeft h₂ b) theorem addLtAdd {a b c d : Nat} (h₁ : a < b) (h₂ : c < d) : a + c < b + d := Nat.ltTrans (Nat.addLtAddRight h₁ c) (Nat.addLtAddLeft h₂ b) /- Basic theorems for comparing numerals -/ theorem natZeroEqZero : Nat.zero = 0 := rfl protected theorem oneNeZero : 1 ≠ (0 : Nat) := fun h => Nat.noConfusion h protected theorem zeroNeOne : 0 ≠ (1 : Nat) := fun h => Nat.noConfusion h theorem succNeZero (n : Nat) : succ n ≠ 0 := fun h => Nat.noConfusion h /- mul + order -/ theorem mulLeMulLeft {n m : Nat} (k : Nat) (h : n ≤ m) : k * n ≤ k * m := match le.dest h with | ⟨l, hl⟩ => have : k * n + k * l = k * m := Nat.left_distrib k n l ▸ hl.symm ▸ rfl le.intro this theorem mulLeMulRight {n m : Nat} (k : Nat) (h : n ≤ m) : n * k ≤ m * k := Nat.mul_comm k m ▸ Nat.mul_comm k n ▸ mulLeMulLeft k h protected theorem mulLeMul {n₁ m₁ n₂ m₂ : Nat} (h₁ : n₁ ≤ n₂) (h₂ : m₁ ≤ m₂) : n₁ * m₁ ≤ n₂ * m₂ := Nat.leTrans (mulLeMulRight _ h₁) (mulLeMulLeft _ h₂) protected theorem mulLtMulOfPosLeft {n m k : Nat} (h : n < m) (hk : k > 0) : k * n < k * m := Nat.ltOfLtOfLe (Nat.addLtAddLeft hk _) (Nat.mul_succ k n ▸ Nat.mulLeMulLeft k (succLeOfLt h)) protected theorem mulLtMulOfPosRight {n m k : Nat} (h : n < m) (hk : k > 0) : n * k < m * k := Nat.mul_comm k m ▸ Nat.mul_comm k n ▸ Nat.mulLtMulOfPosLeft h hk protected theorem mulPos {n m : Nat} (ha : n > 0) (hb : m > 0) : n * m > 0 := have h : 0 * m < n * m := Nat.mulLtMulOfPosRight ha hb Nat.zero_mul m ▸ h /- power -/ theorem powSucc (n m : Nat) : n^(succ m) = n^m * n := rfl theorem powZero (n : Nat) : n^0 = 1 := rfl theorem powLePowOfLeLeft {n m : Nat} (h : n ≤ m) : ∀ (i : Nat), n^i ≤ m^i | 0 => Nat.leRefl _ | succ i => Nat.mulLeMul (powLePowOfLeLeft h i) h theorem powLePowOfLeRight {n : Nat} (hx : n > 0) {i : Nat} : ∀ {j}, i ≤ j → n^i ≤ n^j | 0, h => have : i = 0 := eqZeroOfLeZero h this.symm ▸ Nat.leRefl _ | succ j, h => match ltOrEqOrLeSucc h with | Or.inl h => show n^i ≤ n^j * n from have : n^i * 1 ≤ n^j * n := Nat.mulLeMul (powLePowOfLeRight hx h) hx Nat.mul_one (n^i) ▸ this | Or.inr h => h.symm ▸ Nat.leRefl _ theorem posPowOfPos {n : Nat} (m : Nat) (h : 0 < n) : 0 < n^m := powLePowOfLeRight h (Nat.zeroLe _) /- min/max -/ protected def min (n m : Nat) : Nat := if n ≤ m then n else m protected def max (n m : Nat) : Nat := if n ≤ m then m else n end Nat namespace Prod @[inline] def foldI {α : Type u} (f : Nat → α → α) (i : Nat × Nat) (a : α) : α := Nat.foldAux f i.2 (i.2 - i.1) a @[inline] def anyI (f : Nat → Bool) (i : Nat × Nat) : Bool := Nat.anyAux f i.2 (i.2 - i.1) @[inline] def allI (f : Nat → Bool) (i : Nat × Nat) : Bool := Nat.anyAux (fun a => !f a) i.2 (i.2 - i.1) end Prod
2c2b6d86b9a3423b111591705b222e8cc43ae539
367134ba5a65885e863bdc4507601606690974c1
/src/category_theory/sites/sieves.lean
2043ff7498b635d11898a32f66e73e2298185483
[ "Apache-2.0" ]
permissive
kodyvajjha/mathlib
9bead00e90f68269a313f45f5561766cfd8d5cad
b98af5dd79e13a38d84438b850a2e8858ec21284
refs/heads/master
1,624,350,366,310
1,615,563,062,000
1,615,563,062,000
162,666,963
0
0
Apache-2.0
1,545,367,651,000
1,545,367,651,000
null
UTF-8
Lean
false
false
14,134
lean
/- Copyright (c) 2020 Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta, E. W. Ayers -/ import category_theory.over import category_theory.limits.shapes.finite_limits import category_theory.yoneda import order.complete_lattice import data.set.lattice /-! # Theory of sieves - For an object `X` of a category `C`, a `sieve X` is a set of morphisms to `X` which is closed under left-composition. - The complete lattice structure on sieves is given, as well as the Galois insertion given by downward-closing. - A `sieve X` (functorially) induces a presheaf on `C` together with a monomorphism to the yoneda embedding of `X`. ## Tags sieve, pullback -/ universes v u namespace category_theory variables {C : Type u} [category.{v} C] variables {X Y Z : C} (f : Y ⟶ X) /-- A set of arrows all with codomain `X`. -/ @[derive complete_lattice] def presieve (X : C) := Π ⦃Y⦄, set (Y ⟶ X) namespace presieve instance : inhabited (presieve X) := ⟨⊤⟩ /-- Given a set of arrows `S` all with codomain `X`, and a set of arrows with codomain `Y` for each `f : Y ⟶ X` in `S`, produce a set of arrows with codomain `X`: `{ g ≫ f | (f : Y ⟶ X) ∈ S, (g : Z ⟶ Y) ∈ R f }`. -/ def bind (S : presieve X) (R : Π ⦃Y⦄ ⦃f : Y ⟶ X⦄, S f → presieve Y) : presieve X := λ Z h, ∃ (Y : C) (g : Z ⟶ Y) (f : Y ⟶ X) (H : S f), R H g ∧ g ≫ f = h @[simp] lemma bind_comp {S : presieve X} {R : Π ⦃Y : C⦄ ⦃f : Y ⟶ X⦄, S f → presieve Y} {g : Z ⟶ Y} (h₁ : S f) (h₂ : R h₁ g) : bind S R (g ≫ f) := ⟨_, _, _, h₁, h₂, rfl⟩ /-- The singleton presieve. -/ -- Note we can't make this into `has_singleton` because of the out-param. inductive singleton : presieve X | mk : singleton f @[simp] lemma singleton_eq_iff_domain (f g : Y ⟶ X) : singleton f g ↔ f = g := begin split, { rintro ⟨a, rfl⟩, refl }, { rintro rfl, apply singleton.mk, } end lemma singleton_self : singleton f f := singleton.mk end presieve /-- For an object `X` of a category `C`, a `sieve X` is a set of morphisms to `X` which is closed under left-composition. -/ structure sieve {C : Type u} [category.{v} C] (X : C) := (arrows : presieve X) (downward_closed' : ∀ {Y Z f} (hf : arrows f) (g : Z ⟶ Y), arrows (g ≫ f)) namespace sieve instance {X : C} : has_coe_to_fun (sieve X) := ⟨_, sieve.arrows⟩ initialize_simps_projections sieve (arrows → apply) variables {S R : sieve X} @[simp, priority 100] lemma downward_closed (S : sieve X) {f : Y ⟶ X} (hf : S f) (g : Z ⟶ Y) : S (g ≫ f) := S.downward_closed' hf g lemma arrows_ext : Π {R S : sieve X}, R.arrows = S.arrows → R = S | ⟨Ra, _⟩ ⟨Sa, _⟩ rfl := rfl @[ext] protected lemma ext {R S : sieve X} (h : ∀ ⦃Y⦄ (f : Y ⟶ X), R f ↔ S f) : R = S := arrows_ext $ funext $ λ x, funext $ λ f, propext $ h f protected lemma ext_iff {R S : sieve X} : R = S ↔ (∀ ⦃Y⦄ (f : Y ⟶ X), R f ↔ S f) := ⟨λ h Y f, h ▸ iff.rfl, sieve.ext⟩ open lattice /-- The supremum of a collection of sieves: the union of them all. -/ protected def Sup (𝒮 : set (sieve X)) : (sieve X) := { arrows := λ Y, {f | ∃ S ∈ 𝒮, sieve.arrows S f}, downward_closed' := λ Y Z f, by { rintro ⟨S, hS, hf⟩ g, exact ⟨S, hS, S.downward_closed hf _⟩ } } /-- The infimum of a collection of sieves: the intersection of them all. -/ protected def Inf (𝒮 : set (sieve X)) : (sieve X) := { arrows := λ Y, {f | ∀ S ∈ 𝒮, sieve.arrows S f}, downward_closed' := λ Y Z f hf g S H, S.downward_closed (hf S H) g } /-- The union of two sieves is a sieve. -/ protected def union (S R : sieve X) : sieve X := { arrows := λ Y f, S f ∨ R f, downward_closed' := by { rintros Y Z f (h | h) g; simp [h] } } /-- The intersection of two sieves is a sieve. -/ protected def inter (S R : sieve X) : sieve X := { arrows := λ Y f, S f ∧ R f, downward_closed' := by { rintros Y Z f ⟨h₁, h₂⟩ g, simp [h₁, h₂] } } /-- Sieves on an object `X` form a complete lattice. We generate this directly rather than using the galois insertion for nicer definitional properties. -/ instance : complete_lattice (sieve X) := { le := λ S R, ∀ ⦃Y⦄ (f : Y ⟶ X), S f → R f, le_refl := λ S f q, id, le_trans := λ S₁ S₂ S₃ S₁₂ S₂₃ Y f h, S₂₃ _ (S₁₂ _ h), le_antisymm := λ S R p q, sieve.ext (λ Y f, ⟨p _, q _⟩), top := { arrows := λ _, set.univ, downward_closed' := λ Y Z f g h, ⟨⟩ }, bot := { arrows := λ _, ∅, downward_closed' := λ _ _ _ p _, false.elim p }, sup := sieve.union, inf := sieve.inter, Sup := sieve.Sup, Inf := sieve.Inf, le_Sup := λ 𝒮 S hS Y f hf, ⟨S, hS, hf⟩, Sup_le := λ ℰ S hS Y f, by { rintro ⟨R, hR, hf⟩, apply hS R hR _ hf }, Inf_le := λ _ _ hS _ _ h, h _ hS, le_Inf := λ _ _ hS _ _ hf _ hR, hS _ hR _ hf, le_sup_left := λ _ _ _ _, or.inl, le_sup_right := λ _ _ _ _, or.inr, sup_le := λ _ _ _ a b _ _ hf, hf.elim (a _) (b _), inf_le_left := λ _ _ _ _, and.left, inf_le_right := λ _ _ _ _, and.right, le_inf := λ _ _ _ p q _ _ z, ⟨p _ z, q _ z⟩, le_top := λ _ _ _ _, trivial, bot_le := λ _ _ _, false.elim } /-- The maximal sieve always exists. -/ instance sieve_inhabited : inhabited (sieve X) := ⟨⊤⟩ @[simp] lemma Inf_apply {Ss : set (sieve X)} {Y} (f : Y ⟶ X) : Inf Ss f ↔ ∀ (S : sieve X) (H : S ∈ Ss), S f := iff.rfl @[simp] lemma Sup_apply {Ss : set (sieve X)} {Y} (f : Y ⟶ X) : Sup Ss f ↔ ∃ (S : sieve X) (H : S ∈ Ss), S f := iff.rfl @[simp] lemma inter_apply {R S : sieve X} {Y} (f : Y ⟶ X) : (R ⊓ S) f ↔ R f ∧ S f := iff.rfl @[simp] lemma union_apply {R S : sieve X} {Y} (f : Y ⟶ X) : (R ⊔ S) f ↔ R f ∨ S f := iff.rfl @[simp] lemma top_apply (f : Y ⟶ X) : (⊤ : sieve X) f := trivial /-- Generate the smallest sieve containing the given set of arrows. -/ @[simps] def generate (R : presieve X) : sieve X := { arrows := λ Z f, ∃ Y (h : Z ⟶ Y) (g : Y ⟶ X), R g ∧ h ≫ g = f, downward_closed' := begin rintro Y Z _ ⟨W, g, f, hf, rfl⟩ h, exact ⟨_, h ≫ g, _, hf, by simp⟩, end } /-- Given a presieve on `X`, and a sieve on each domain of an arrow in the presieve, we can bind to produce a sieve on `X`. -/ @[simps] def bind (S : presieve X) (R : Π ⦃Y⦄ ⦃f : Y ⟶ X⦄, S f → sieve Y) : sieve X := { arrows := S.bind (λ Y f h, R h), downward_closed' := begin rintro Y Z f ⟨W, f, h, hh, hf, rfl⟩ g, exact ⟨_, g ≫ f, _, hh, by simp [hf]⟩, end } open order lattice lemma sets_iff_generate (R : presieve X) (S : sieve X) : generate R ≤ S ↔ R ≤ S := ⟨λ H Y g hg, H _ ⟨_, 𝟙 _, _, hg, category.id_comp _⟩, λ ss Y f, begin rintro ⟨Z, f, g, hg, rfl⟩, exact S.downward_closed (ss Z hg) f, end⟩ /-- Show that there is a galois insertion (generate, set_over). -/ def gi_generate : galois_insertion (generate : presieve X → sieve X) arrows := { gc := sets_iff_generate, choice := λ 𝒢 _, generate 𝒢, choice_eq := λ _ _, rfl, le_l_u := λ S Y f hf, ⟨_, 𝟙 _, _, hf, category.id_comp _⟩ } lemma le_generate (R : presieve X) : R ≤ generate R := gi_generate.gc.le_u_l R /-- If the identity arrow is in a sieve, the sieve is maximal. -/ lemma id_mem_iff_eq_top : S (𝟙 X) ↔ S = ⊤ := ⟨λ h, top_unique $ λ Y f _, by simpa using downward_closed _ h f, λ h, h.symm ▸ trivial⟩ /-- If an arrow set contains a split epi, it generates the maximal sieve. -/ lemma generate_of_contains_split_epi {R : presieve X} (f : Y ⟶ X) [split_epi f] (hf : R f) : generate R = ⊤ := begin rw ← id_mem_iff_eq_top, exact ⟨_, section_ f, f, hf, by simp⟩, end @[simp] lemma generate_of_singleton_split_epi (f : Y ⟶ X) [split_epi f] : generate (presieve.singleton f) = ⊤ := generate_of_contains_split_epi f (presieve.singleton_self _) @[simp] lemma generate_top : generate (⊤ : presieve X) = ⊤ := generate_of_contains_split_epi (𝟙 _) ⟨⟩ /-- Given a morphism `h : Y ⟶ X`, send a sieve S on X to a sieve on Y as the inverse image of S with `_ ≫ h`. That is, `sieve.pullback S h := (≫ h) '⁻¹ S`. -/ @[simps] def pullback (h : Y ⟶ X) (S : sieve X) : sieve Y := { arrows := λ Y sl, S (sl ≫ h), downward_closed' := λ Z W f g h, by simp [g] } @[simp] lemma pullback_id : S.pullback (𝟙 _) = S := by simp [sieve.ext_iff] @[simp] lemma pullback_top {f : Y ⟶ X} : (⊤ : sieve X).pullback f = ⊤ := top_unique (λ _ g, id) lemma pullback_comp {f : Y ⟶ X} {g : Z ⟶ Y} (S : sieve X) : S.pullback (g ≫ f) = (S.pullback f).pullback g := by simp [sieve.ext_iff] @[simp] lemma pullback_inter {f : Y ⟶ X} (S R : sieve X) : (S ⊓ R).pullback f = S.pullback f ⊓ R.pullback f := by simp [sieve.ext_iff] lemma pullback_eq_top_iff_mem (f : Y ⟶ X) : S f ↔ S.pullback f = ⊤ := by rw [← id_mem_iff_eq_top, pullback_apply, category.id_comp] lemma pullback_eq_top_of_mem (S : sieve X) {f : Y ⟶ X} : S f → S.pullback f = ⊤ := (pullback_eq_top_iff_mem f).1 /-- Push a sieve `R` on `Y` forward along an arrow `f : Y ⟶ X`: `gf : Z ⟶ X` is in the sieve if `gf` factors through some `g : Z ⟶ Y` which is in `R`. -/ @[simps] def pushforward (f : Y ⟶ X) (R : sieve Y) : sieve X := { arrows := λ Z gf, ∃ g, g ≫ f = gf ∧ R g, downward_closed' := λ Z₁ Z₂ g ⟨j, k, z⟩ h, ⟨h ≫ j, by simp [k], by simp [z]⟩ } lemma pushforward_apply_comp {R : sieve Y} {Z : C} {g : Z ⟶ Y} (hg : R g) (f : Y ⟶ X) : R.pushforward f (g ≫ f) := ⟨g, rfl, hg⟩ lemma pushforward_comp {f : Y ⟶ X} {g : Z ⟶ Y} (R : sieve Z) : R.pushforward (g ≫ f) = (R.pushforward g).pushforward f := sieve.ext (λ W h, ⟨λ ⟨f₁, hq, hf₁⟩, ⟨f₁ ≫ g, by simpa, f₁, rfl, hf₁⟩, λ ⟨y, hy, z, hR, hz⟩, ⟨z, by rwa reassoc_of hR, hz⟩⟩) lemma galois_connection (f : Y ⟶ X) : galois_connection (sieve.pushforward f) (sieve.pullback f) := λ S R, ⟨λ hR Z g hg, hR _ ⟨g, rfl, hg⟩, λ hS Z g ⟨h, hg, hh⟩, hg ▸ hS h hh⟩ lemma pullback_monotone (f : Y ⟶ X) : monotone (sieve.pullback f) := (galois_connection f).monotone_u lemma pushforward_monotone (f : Y ⟶ X) : monotone (sieve.pushforward f) := (galois_connection f).monotone_l lemma le_pushforward_pullback (f : Y ⟶ X) (R : sieve Y) : R ≤ (R.pushforward f).pullback f := (galois_connection f).le_u_l _ lemma pullback_pushforward_le (f : Y ⟶ X) (R : sieve X) : (R.pullback f).pushforward f ≤ R := (galois_connection f).l_u_le _ lemma pushforward_union {f : Y ⟶ X} (S R : sieve Y) : (S ⊔ R).pushforward f = S.pushforward f ⊔ R.pushforward f := (galois_connection f).l_sup lemma pushforward_le_bind_of_mem (S : presieve X) (R : Π ⦃Y : C⦄ ⦃f : Y ⟶ X⦄, S f → sieve Y) (f : Y ⟶ X) (h : S f) : (R h).pushforward f ≤ bind S R := begin rintro Z _ ⟨g, rfl, hg⟩, exact ⟨_, g, f, h, hg, rfl⟩, end lemma le_pullback_bind (S : presieve X) (R : Π ⦃Y : C⦄ ⦃f : Y ⟶ X⦄, S f → sieve Y) (f : Y ⟶ X) (h : S f) : R h ≤ (bind S R).pullback f := begin rw ← galois_connection f, apply pushforward_le_bind_of_mem, end /-- If `f` is a monomorphism, the pushforward-pullback adjunction on sieves is coreflective. -/ def galois_coinsertion_of_mono (f : Y ⟶ X) [mono f] : galois_coinsertion (sieve.pushforward f) (sieve.pullback f) := begin apply (galois_connection f).to_galois_coinsertion, rintros S Z g ⟨g₁, hf, hg₁⟩, rw cancel_mono f at hf, rwa ← hf, end /-- If `f` is a split epi, the pushforward-pullback adjunction on sieves is reflective. -/ def galois_insertion_of_split_epi (f : Y ⟶ X) [split_epi f] : galois_insertion (sieve.pushforward f) (sieve.pullback f) := begin apply (galois_connection f).to_galois_insertion, intros S Z g hg, refine ⟨g ≫ section_ f, by simpa⟩, end /-- A sieve induces a presheaf. -/ @[simps] def functor (S : sieve X) : Cᵒᵖ ⥤ Type v := { obj := λ Y, {g : Y.unop ⟶ X // S g}, map := λ Y Z f g, ⟨f.unop ≫ g.1, downward_closed _ g.2 _⟩ } /-- If a sieve S is contained in a sieve T, then we have a morphism of presheaves on their induced presheaves. -/ @[simps] def nat_trans_of_le {S T : sieve X} (h : S ≤ T) : S.functor ⟶ T.functor := { app := λ Y f, ⟨f.1, h _ f.2⟩ }. /-- The natural inclusion from the functor induced by a sieve to the yoneda embedding. -/ @[simps] def functor_inclusion (S : sieve X) : S.functor ⟶ yoneda.obj X := { app := λ Y f, f.1 }. lemma nat_trans_of_le_comm {S T : sieve X} (h : S ≤ T) : nat_trans_of_le h ≫ functor_inclusion _ = functor_inclusion _ := rfl /-- The presheaf induced by a sieve is a subobject of the yoneda embedding. -/ instance functor_inclusion_is_mono : mono S.functor_inclusion := ⟨λ Z f g h, by { ext Y y, apply congr_fun (nat_trans.congr_app h Y) y }⟩ /-- A natural transformation to a representable functor induces a sieve. This is the left inverse of `functor_inclusion`, shown in `sieve_of_functor_inclusion`. -/ -- TODO: Show that when `f` is mono, this is right inverse to `functor_inclusion` up to isomorphism. @[simps] def sieve_of_subfunctor {R} (f : R ⟶ yoneda.obj X) : sieve X := { arrows := λ Y g, ∃ t, f.app (opposite.op Y) t = g, downward_closed' := λ Y Z _, begin rintro ⟨t, rfl⟩ g, refine ⟨R.map g.op t, _⟩, rw functor_to_types.naturality _ _ f, simp, end } lemma sieve_of_subfunctor_functor_inclusion : sieve_of_subfunctor S.functor_inclusion = S := begin ext, simp only [functor_inclusion_app, sieve_of_subfunctor_apply, subtype.val_eq_coe], split, { rintro ⟨⟨f, hf⟩, rfl⟩, exact hf }, { intro hf, exact ⟨⟨_, hf⟩, rfl⟩ } end instance functor_inclusion_top_is_iso : is_iso ((⊤ : sieve X).functor_inclusion) := { inv := { app := λ Y a, ⟨a, ⟨⟩⟩ } } end sieve end category_theory
5bcdcd8a26a2edfb5534610fdefd79b73849c8c5
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/Lean/Meta/Eqns.lean
3f07258d9ded666419d83e12910291c6d7b44cb5
[ "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
5,244
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Meta.Basic import Lean.Meta.AppBuilder namespace Lean.Meta def GetEqnsFn := Name → MetaM (Option (Array Name)) private builtin_initialize getEqnsFnsRef : IO.Ref (List GetEqnsFn) ← IO.mkRef [] /-- Register a new function for retrieving equation theorems. We generate equations theorems on demand, and they are generated by more than one module. For example, the structural and well-founded recursion modules generate them. Most recent getters are tried first. A getter returns an `Option (Array Name)`. The result is `none` if the getter failed. Otherwise, it is a sequence of theorem names where each one of them corresponds to an alternative. Example: the definition ``` def f (xs : List Nat) : List Nat := match xs with | [] => [] | x::xs => (x+1)::f xs ``` should have two equational theorems associated with it ``` f [] = [] ``` and ``` (x : Nat) → (xs : List Nat) → f (x :: xs) = (x+1) :: f xs ``` -/ def registerGetEqnsFn (f : GetEqnsFn) : IO Unit := do unless (← initializing) do throw (IO.userError "failed to register equation getter, this kind of extension can only be registered during initialization") getEqnsFnsRef.modify (f :: ·) /-- Return true iff `declName` is a definition and its type is not a proposition. -/ private def shouldGenerateEqnThms (declName : Name) : MetaM Bool := do if let some (.defnInfo info) := (← getEnv).find? declName then return !(← isProp info.type) else return false structure EqnsExtState where map : PHashMap Name (Array Name) := {} deriving Inhabited /- We generate the equations on demand, and do not save them on .olean files. -/ builtin_initialize eqnsExt : EnvExtension EqnsExtState ← registerEnvExtension (pure {}) /-- Simple equation theorem for nonrecursive definitions. -/ private def mkSimpleEqThm (declName : Name) : MetaM (Option Name) := do if let some (.defnInfo info) := (← getEnv).find? declName then lambdaTelescope info.value fun xs body => do let lhs := mkAppN (mkConst info.name <| info.levelParams.map mkLevelParam) xs let type ← mkForallFVars xs (← mkEq lhs body) let value ← mkLambdaFVars xs (← mkEqRefl lhs) let name := mkPrivateName (← getEnv) declName ++ `_eq_1 addDecl <| Declaration.thmDecl { name, type, value levelParams := info.levelParams } return some name else return none /-- Return equation theorems for the given declaration. By default, we not create equation theorems for nonrecursive definitions. You can use `nonRec := true` to override this behavior, a dummy `rfl` proof is created on the fly. -/ def getEqnsFor? (declName : Name) (nonRec := false) : MetaM (Option (Array Name)) := withLCtx {} {} do if let some eqs := eqnsExt.getState (← getEnv) |>.map.find? declName then return some eqs else if (← shouldGenerateEqnThms declName) then for f in (← getEqnsFnsRef.get) do if let some r ← f declName then modifyEnv fun env => eqnsExt.modifyState env fun s => { s with map := s.map.insert declName r } return some r if nonRec then let some eqThm ← mkSimpleEqThm declName | return none let r := #[eqThm] modifyEnv fun env => eqnsExt.modifyState env fun s => { s with map := s.map.insert declName r } return some r return none def GetUnfoldEqnFn := Name → MetaM (Option Name) private builtin_initialize getUnfoldEqnFnsRef : IO.Ref (List GetUnfoldEqnFn) ← IO.mkRef [] /-- Register a new function for retrieving a "unfold" equation theorem. We generate this kind of equation theorem on demand, and it is generated by more than one module. For example, the structural and well-founded recursion modules generate it. Most recent getters are tried first. A getter returns an `Option Name`. The result is `none` if the getter failed. Otherwise, it is a theorem name. Example: the definition ``` def f (xs : List Nat) : List Nat := match xs with | [] => [] | x::xs => (x+1)::f xs ``` should have the theorem ``` (xs : Nat) → f xs = match xs with | [] => [] | x::xs => (x+1)::f xs ``` -/ def registerGetUnfoldEqnFn (f : GetUnfoldEqnFn) : IO Unit := do unless (← initializing) do throw (IO.userError "failed to register equation getter, this kind of extension can only be registered during initialization") getUnfoldEqnFnsRef.modify (f :: ·) /-- Return a "unfold" theorem for the given declaration. By default, we not create unfold theorems for nonrecursive definitions. You can use `nonRec := true` to override this behavior. -/ def getUnfoldEqnFor? (declName : Name) (nonRec := false) : MetaM (Option Name) := withLCtx {} {} do if (← shouldGenerateEqnThms declName) then for f in (← getUnfoldEqnFnsRef.get) do if let some r ← f declName then return some r if nonRec then let some #[eqThm] ← getEqnsFor? declName (nonRec := true) | return none return some eqThm return none end Lean.Meta
4385b5cebde97b0830c698c1e851f4ccb394c42d
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/algebra/lie/solvable.lean
e8978a2402ef80576123f00e13fe08d9f054f5f1
[ "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,861
lean
/- Copyright (c) 2021 Oliver Nash. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Oliver Nash -/ import algebra.lie.abelian import algebra.lie.ideal_operations import order.hom.basic /-! # Solvable Lie algebras Like groups, Lie algebras admit a natural concept of solvability. We define this here via the derived series and prove some related results. We also define the radical of a Lie algebra and prove that it is solvable when the Lie algebra is Noetherian. ## Main definitions * `lie_algebra.derived_series_of_ideal` * `lie_algebra.derived_series` * `lie_algebra.is_solvable` * `lie_algebra.is_solvable_add` * `lie_algebra.radical` * `lie_algebra.radical_is_solvable` * `lie_algebra.derived_length_of_ideal` * `lie_algebra.derived_length` * `lie_algebra.derived_abelian_of_ideal` ## Tags lie algebra, derived series, derived length, solvable, radical -/ universes u v w w₁ w₂ variables (R : Type u) (L : Type v) (M : Type w) {L' : Type w₁} variables [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L'] variables (I J : lie_ideal R L) {f : L' →ₗ⁅R⁆ L} namespace lie_algebra /-- A generalisation of the derived series of a Lie algebra, whose zeroth term is a specified ideal. It can be more convenient to work with this generalisation when considering the derived series of an ideal since it provides a type-theoretic expression of the fact that the terms of the ideal's derived series are also ideals of the enclosing algebra. See also `lie_ideal.derived_series_eq_derived_series_of_ideal_comap` and `lie_ideal.derived_series_eq_derived_series_of_ideal_map` below. -/ def derived_series_of_ideal (k : ℕ) : lie_ideal R L → lie_ideal R L := (λ I, ⁅I, I⁆)^[k] @[simp] lemma derived_series_of_ideal_zero : derived_series_of_ideal R L 0 I = I := rfl @[simp] lemma derived_series_of_ideal_succ (k : ℕ) : derived_series_of_ideal R L (k + 1) I = ⁅derived_series_of_ideal R L k I, derived_series_of_ideal R L k I⁆ := function.iterate_succ_apply' (λ I, ⁅I, I⁆) k I /-- The derived series of Lie ideals of a Lie algebra. -/ abbreviation derived_series (k : ℕ) : lie_ideal R L := derived_series_of_ideal R L k ⊤ lemma derived_series_def (k : ℕ) : derived_series R L k = derived_series_of_ideal R L k ⊤ := rfl variables {R L} local notation `D` := derived_series_of_ideal R L lemma derived_series_of_ideal_add (k l : ℕ) : D (k + l) I = D k (D l I) := begin induction k with k ih, { rw [zero_add, derived_series_of_ideal_zero], }, { rw [nat.succ_add k l, derived_series_of_ideal_succ, derived_series_of_ideal_succ, ih], }, end @[mono] lemma derived_series_of_ideal_le {I J : lie_ideal R L} {k l : ℕ} (h₁ : I ≤ J) (h₂ : l ≤ k) : D k I ≤ D l J := begin revert l, induction k with k ih; intros l h₂, { rw nat.le_zero_iff at h₂, rw [h₂, derived_series_of_ideal_zero], exact h₁, }, { have h : l = k.succ ∨ l ≤ k, by rwa [le_iff_eq_or_lt, nat.lt_succ_iff] at h₂, cases h, { rw [h, derived_series_of_ideal_succ, derived_series_of_ideal_succ], exact lie_submodule.mono_lie _ _ _ _ (ih (le_refl k)) (ih (le_refl k)), }, { rw derived_series_of_ideal_succ, exact le_trans (lie_submodule.lie_le_left _ _) (ih h), }, }, end lemma derived_series_of_ideal_succ_le (k : ℕ) : D (k + 1) I ≤ D k I := derived_series_of_ideal_le (le_refl I) k.le_succ lemma derived_series_of_ideal_le_self (k : ℕ) : D k I ≤ I := derived_series_of_ideal_le (le_refl I) (zero_le k) lemma derived_series_of_ideal_mono {I J : lie_ideal R L} (h : I ≤ J) (k : ℕ) : D k I ≤ D k J := derived_series_of_ideal_le h (le_refl k) lemma derived_series_of_ideal_antitone {k l : ℕ} (h : l ≤ k) : D k I ≤ D l I := derived_series_of_ideal_le (le_refl I) h lemma derived_series_of_ideal_add_le_add (J : lie_ideal R L) (k l : ℕ) : D (k + l) (I + J) ≤ (D k I) + (D l J) := begin let D₁ : lie_ideal R L →o lie_ideal R L := { to_fun := λ I, ⁅I, I⁆, monotone' := λ I J h, lie_submodule.mono_lie I J I J h h, }, have h₁ : ∀ (I J : lie_ideal R L), D₁ (I ⊔ J) ≤ (D₁ I) ⊔ J, { simp [lie_submodule.lie_le_right, lie_submodule.lie_le_left, le_sup_of_le_right], }, rw ← D₁.iterate_sup_le_sup_iff at h₁, exact h₁ k l I J, end lemma derived_series_of_bot_eq_bot (k : ℕ) : derived_series_of_ideal R L k ⊥ = ⊥ := by { rw eq_bot_iff, exact derived_series_of_ideal_le_self ⊥ k, } lemma abelian_iff_derived_one_eq_bot : is_lie_abelian I ↔ derived_series_of_ideal R L 1 I = ⊥ := by rw [derived_series_of_ideal_succ, derived_series_of_ideal_zero, lie_submodule.lie_abelian_iff_lie_self_eq_bot] lemma abelian_iff_derived_succ_eq_bot (I : lie_ideal R L) (k : ℕ) : is_lie_abelian (derived_series_of_ideal R L k I) ↔ derived_series_of_ideal R L (k + 1) I = ⊥ := by rw [add_comm, derived_series_of_ideal_add I 1 k, abelian_iff_derived_one_eq_bot] end lie_algebra namespace lie_ideal open lie_algebra variables {R L} lemma derived_series_eq_derived_series_of_ideal_comap (k : ℕ) : derived_series R I k = (derived_series_of_ideal R L k I).comap I.incl := begin induction k with k ih, { simp only [derived_series_def, comap_incl_self, derived_series_of_ideal_zero], }, { simp only [derived_series_def, derived_series_of_ideal_succ] at ⊢ ih, rw ih, exact comap_bracket_incl_of_le I (derived_series_of_ideal_le_self I k) (derived_series_of_ideal_le_self I k), }, end lemma derived_series_eq_derived_series_of_ideal_map (k : ℕ) : (derived_series R I k).map I.incl = derived_series_of_ideal R L k I := by { rw [derived_series_eq_derived_series_of_ideal_comap, map_comap_incl, inf_eq_right], apply derived_series_of_ideal_le_self, } lemma derived_series_eq_bot_iff (k : ℕ) : derived_series R I k = ⊥ ↔ derived_series_of_ideal R L k I = ⊥ := by rw [← derived_series_eq_derived_series_of_ideal_map, map_eq_bot_iff, ker_incl, eq_bot_iff] lemma derived_series_add_eq_bot {k l : ℕ} {I J : lie_ideal R L} (hI : derived_series R I k = ⊥) (hJ : derived_series R J l = ⊥) : derived_series R ↥(I + J) (k + l) = ⊥ := begin rw lie_ideal.derived_series_eq_bot_iff at hI hJ ⊢, rw ← le_bot_iff, let D := derived_series_of_ideal R L, change D k I = ⊥ at hI, change D l J = ⊥ at hJ, calc D (k + l) (I + J) ≤ (D k I) + (D l J) : derived_series_of_ideal_add_le_add I J k l ... ≤ ⊥ : by { rw [hI, hJ], simp, }, end lemma derived_series_map_le (k : ℕ) : (derived_series R L' k).map f ≤ derived_series R L k := begin induction k with k ih, { simp only [derived_series_def, derived_series_of_ideal_zero, le_top], }, { simp only [derived_series_def, derived_series_of_ideal_succ] at ih ⊢, exact le_trans (map_bracket_le f) (lie_submodule.mono_lie _ _ _ _ ih ih), }, end lemma derived_series_map_eq (k : ℕ) (h : function.surjective f) : (derived_series R L' k).map f = derived_series R L k := begin induction k with k ih, { change (⊤ : lie_ideal R L').map f = ⊤, rw ←f.ideal_range_eq_map, exact f.ideal_range_eq_top_of_surjective h, }, { simp only [derived_series_def, map_bracket_eq f h, ih, derived_series_of_ideal_succ], }, end end lie_ideal namespace lie_algebra /-- A Lie algebra is solvable if its derived series reaches 0 (in a finite number of steps). -/ class is_solvable : Prop := (solvable : ∃ k, derived_series R L k = ⊥) instance is_solvable_bot : is_solvable R ↥(⊥ : lie_ideal R L) := ⟨⟨0, @subsingleton.elim _ lie_ideal.subsingleton_of_bot _ ⊥⟩⟩ instance is_solvable_add {I J : lie_ideal R L} [hI : is_solvable R I] [hJ : is_solvable R J] : is_solvable R ↥(I + J) := begin obtain ⟨k, hk⟩ := id hI, obtain ⟨l, hl⟩ := id hJ, exact ⟨⟨k+l, lie_ideal.derived_series_add_eq_bot hk hl⟩⟩, end end lie_algebra variables {R L} namespace function open lie_algebra lemma injective.lie_algebra_is_solvable [h₁ : is_solvable R L] (h₂ : injective f) : is_solvable R L' := begin obtain ⟨k, hk⟩ := id h₁, use k, apply lie_ideal.bot_of_map_eq_bot h₂, rw [eq_bot_iff, ← hk], apply lie_ideal.derived_series_map_le, end lemma surjective.lie_algebra_is_solvable [h₁ : is_solvable R L'] (h₂ : surjective f) : is_solvable R L := begin obtain ⟨k, hk⟩ := id h₁, use k, rw [← lie_ideal.derived_series_map_eq k h₂, hk], simp only [lie_ideal.map_eq_bot_iff, bot_le], end end function lemma lie_hom.is_solvable_range (f : L' →ₗ⁅R⁆ L) [h : lie_algebra.is_solvable R L'] : lie_algebra.is_solvable R f.range := f.surjective_range_restrict.lie_algebra_is_solvable namespace lie_algebra lemma solvable_iff_equiv_solvable (e : L' ≃ₗ⁅R⁆ L) : is_solvable R L' ↔ is_solvable R L := begin split; introsI h, { exact e.symm.injective.lie_algebra_is_solvable, }, { exact e.injective.lie_algebra_is_solvable, }, end lemma le_solvable_ideal_solvable {I J : lie_ideal R L} (h₁ : I ≤ J) (h₂ : is_solvable R J) : is_solvable R I := (lie_ideal.hom_of_le_injective h₁).lie_algebra_is_solvable variables (R L) @[priority 100] instance of_abelian_is_solvable [is_lie_abelian L] : is_solvable R L := begin use 1, rw [← abelian_iff_derived_one_eq_bot, lie_abelian_iff_equiv_lie_abelian lie_ideal.top_equiv_self], apply_instance, end /-- The (solvable) radical of Lie algebra is the `Sup` of all solvable ideals. -/ def radical := Sup { I : lie_ideal R L | is_solvable R I } /-- The radical of a Noetherian Lie algebra is solvable. -/ instance radical_is_solvable [is_noetherian R L] : is_solvable R (radical R L) := begin have hwf := lie_submodule.well_founded_of_noetherian R L L, rw ← complete_lattice.is_sup_closed_compact_iff_well_founded at hwf, refine hwf { I : lie_ideal R L | is_solvable R I } ⟨⊥, _⟩ (λ I hI J hJ, _), { exact lie_algebra.is_solvable_bot R L, }, { apply lie_algebra.is_solvable_add R L, exacts [hI, hJ] }, end /-- The `→` direction of this lemma is actually true without the `is_noetherian` assumption. -/ lemma lie_ideal.solvable_iff_le_radical [is_noetherian R L] (I : lie_ideal R L) : is_solvable R I ↔ I ≤ radical R L := ⟨λ h, le_Sup h, λ h, le_solvable_ideal_solvable h infer_instance⟩ lemma center_le_radical : center R L ≤ radical R L := have h : is_solvable R (center R L), { apply_instance, }, le_Sup h /-- Given a solvable Lie ideal `I` with derived series `I = D₀ ≥ D₁ ≥ ⋯ ≥ Dₖ = ⊥`, this is the natural number `k` (the number of inclusions). For a non-solvable ideal, the value is 0. -/ noncomputable def derived_length_of_ideal (I : lie_ideal R L) : ℕ := Inf {k | derived_series_of_ideal R L k I = ⊥} /-- The derived length of a Lie algebra is the derived length of its 'top' Lie ideal. See also `lie_algebra.derived_length_eq_derived_length_of_ideal`. -/ noncomputable abbreviation derived_length : ℕ := derived_length_of_ideal R L ⊤ lemma derived_series_of_derived_length_succ (I : lie_ideal R L) (k : ℕ) : derived_length_of_ideal R L I = k + 1 ↔ is_lie_abelian (derived_series_of_ideal R L k I) ∧ derived_series_of_ideal R L k I ≠ ⊥ := begin rw abelian_iff_derived_succ_eq_bot, let s := {k | derived_series_of_ideal R L k I = ⊥}, change Inf s = k + 1 ↔ k + 1 ∈ s ∧ k ∉ s, have hs : ∀ (k₁ k₂ : ℕ), k₁ ≤ k₂ → k₁ ∈ s → k₂ ∈ s, { intros k₁ k₂ h₁₂ h₁, suffices : derived_series_of_ideal R L k₂ I ≤ ⊥, { exact eq_bot_iff.mpr this, }, change derived_series_of_ideal R L k₁ I = ⊥ at h₁, rw ← h₁, exact derived_series_of_ideal_antitone I h₁₂, }, exact nat.Inf_upward_closed_eq_succ_iff hs k, end lemma derived_length_eq_derived_length_of_ideal (I : lie_ideal R L) : derived_length R I = derived_length_of_ideal R L I := begin let s₁ := {k | derived_series R I k = ⊥}, let s₂ := {k | derived_series_of_ideal R L k I = ⊥}, change Inf s₁ = Inf s₂, congr, ext k, exact I.derived_series_eq_bot_iff k, end variables {R L} /-- Given a solvable Lie ideal `I` with derived series `I = D₀ ≥ D₁ ≥ ⋯ ≥ Dₖ = ⊥`, this is the `k-1`th term in the derived series (and is therefore an Abelian ideal contained in `I`). For a non-solvable ideal, this is the zero ideal, `⊥`. -/ noncomputable def derived_abelian_of_ideal (I : lie_ideal R L) : lie_ideal R L := match derived_length_of_ideal R L I with | 0 := ⊥ | k + 1 := derived_series_of_ideal R L k I end lemma abelian_derived_abelian_of_ideal (I : lie_ideal R L) : is_lie_abelian (derived_abelian_of_ideal I) := begin dunfold derived_abelian_of_ideal, cases h : derived_length_of_ideal R L I with k, { exact is_lie_abelian_bot R L, }, { rw derived_series_of_derived_length_succ at h, exact h.1, }, end lemma derived_length_zero (I : lie_ideal R L) [hI : is_solvable R I] : derived_length_of_ideal R L I = 0 ↔ I = ⊥ := begin let s := {k | derived_series_of_ideal R L k I = ⊥}, change Inf s = 0 ↔ _, have hne : s ≠ ∅, { rw set.ne_empty_iff_nonempty, obtain ⟨k, hk⟩ := id hI, use k, rw [derived_series_def, lie_ideal.derived_series_eq_bot_iff] at hk, exact hk, }, simp [hne], end lemma abelian_of_solvable_ideal_eq_bot_iff (I : lie_ideal R L) [h : is_solvable R I] : derived_abelian_of_ideal I = ⊥ ↔ I = ⊥ := begin dunfold derived_abelian_of_ideal, cases h : derived_length_of_ideal R L I with k, { rw derived_length_zero at h, rw h, refl, }, { obtain ⟨h₁, h₂⟩ := (derived_series_of_derived_length_succ R L I k).mp h, have h₃ : I ≠ ⊥, { intros contra, apply h₂, rw contra, apply derived_series_of_bot_eq_bot, }, change derived_series_of_ideal R L k I = ⊥ ↔ I = ⊥, split; contradiction, }, end end lie_algebra
7a45042f0f4ed506d75e1c665d7e16211e4f6bfe
9dc8cecdf3c4634764a18254e94d43da07142918
/src/group_theory/submonoid/operations.lean
2aa27c94f47dc3cade38e632675afa244ee6cf90
[ "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
45,103
lean
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Kenny Lau, Johan Commelin, Mario Carneiro, Kevin Buzzard, Amelia Livingston, Yury Kudryashov -/ import group_theory.group_action.defs import group_theory.submonoid.basic import group_theory.subsemigroup.operations /-! # Operations on `submonoid`s In this file we define various operations on `submonoid`s and `monoid_hom`s. ## Main definitions ### Conversion between multiplicative and additive definitions * `submonoid.to_add_submonoid`, `submonoid.to_add_submonoid'`, `add_submonoid.to_submonoid`, `add_submonoid.to_submonoid'`: convert between multiplicative and additive submonoids of `M`, `multiplicative M`, and `additive M`. These are stated as `order_iso`s. ### (Commutative) monoid structure on a submonoid * `submonoid.to_monoid`, `submonoid.to_comm_monoid`: a submonoid inherits a (commutative) monoid structure. ### Group actions by submonoids * `submonoid.mul_action`, `submonoid.distrib_mul_action`: a submonoid inherits (distributive) multiplicative actions. ### Operations on submonoids * `submonoid.comap`: preimage of a submonoid under a monoid homomorphism as a submonoid of the domain; * `submonoid.map`: image of a submonoid under a monoid homomorphism as a submonoid of the codomain; * `submonoid.prod`: product of two submonoids `s : submonoid M` and `t : submonoid N` as a submonoid of `M × N`; ### Monoid homomorphisms between submonoid * `submonoid.subtype`: embedding of a submonoid into the ambient monoid. * `submonoid.inclusion`: given two submonoids `S`, `T` such that `S ≤ T`, `S.inclusion T` is the inclusion of `S` into `T` as a monoid homomorphism; * `mul_equiv.submonoid_congr`: converts a proof of `S = T` into a monoid isomorphism between `S` and `T`. * `submonoid.prod_equiv`: monoid isomorphism between `s.prod t` and `s × t`; ### Operations on `monoid_hom`s * `monoid_hom.mrange`: range of a monoid homomorphism as a submonoid of the codomain; * `monoid_hom.mker`: kernel of a monoid homomorphism as a submonoid of the domain; * `monoid_hom.restrict`: restrict a monoid homomorphism to a submonoid; * `monoid_hom.cod_restrict`: restrict the codomain of a monoid homomorphism to a submonoid; * `monoid_hom.mrange_restrict`: restrict a monoid homomorphism to its range; ## Tags submonoid, range, product, map, comap -/ variables {M N P : Type*} [mul_one_class M] [mul_one_class N] [mul_one_class P] (S : submonoid M) /-! ### Conversion to/from `additive`/`multiplicative` -/ section /-- Submonoids of monoid `M` are isomorphic to additive submonoids of `additive M`. -/ @[simps] def submonoid.to_add_submonoid : submonoid M ≃o add_submonoid (additive M) := { to_fun := λ S, { carrier := additive.to_mul ⁻¹' S, zero_mem' := S.one_mem', add_mem' := λ _ _, S.mul_mem' }, inv_fun := λ S, { carrier := additive.of_mul ⁻¹' S, one_mem' := S.zero_mem', mul_mem' := λ _ _, S.add_mem' }, left_inv := λ x, by cases x; refl, right_inv := λ x, by cases x; refl, map_rel_iff' := λ a b, iff.rfl, } /-- Additive submonoids of an additive monoid `additive M` are isomorphic to submonoids of `M`. -/ abbreviation add_submonoid.to_submonoid' : add_submonoid (additive M) ≃o submonoid M := submonoid.to_add_submonoid.symm lemma submonoid.to_add_submonoid_closure (S : set M) : (submonoid.closure S).to_add_submonoid = add_submonoid.closure (additive.to_mul ⁻¹' S) := le_antisymm (submonoid.to_add_submonoid.le_symm_apply.1 $ submonoid.closure_le.2 add_submonoid.subset_closure) (add_submonoid.closure_le.2 submonoid.subset_closure) lemma add_submonoid.to_submonoid'_closure (S : set (additive M)) : (add_submonoid.closure S).to_submonoid' = submonoid.closure (multiplicative.of_add ⁻¹' S) := le_antisymm (add_submonoid.to_submonoid'.le_symm_apply.1 $ add_submonoid.closure_le.2 submonoid.subset_closure) (submonoid.closure_le.2 add_submonoid.subset_closure) end section variables {A : Type*} [add_zero_class A] /-- Additive submonoids of an additive monoid `A` are isomorphic to multiplicative submonoids of `multiplicative A`. -/ @[simps] def add_submonoid.to_submonoid : add_submonoid A ≃o submonoid (multiplicative A) := { to_fun := λ S, { carrier := multiplicative.to_add ⁻¹' S, one_mem' := S.zero_mem', mul_mem' := λ _ _, S.add_mem' }, inv_fun := λ S, { carrier := multiplicative.of_add ⁻¹' S, zero_mem' := S.one_mem', add_mem' := λ _ _, S.mul_mem' }, left_inv := λ x, by cases x; refl, right_inv := λ x, by cases x; refl, map_rel_iff' := λ a b, iff.rfl, } /-- Submonoids of a monoid `multiplicative A` are isomorphic to additive submonoids of `A`. -/ abbreviation submonoid.to_add_submonoid' : submonoid (multiplicative A) ≃o add_submonoid A := add_submonoid.to_submonoid.symm lemma add_submonoid.to_submonoid_closure (S : set A) : (add_submonoid.closure S).to_submonoid = submonoid.closure (multiplicative.to_add ⁻¹' S) := le_antisymm (add_submonoid.to_submonoid.to_galois_connection.l_le $ add_submonoid.closure_le.2 submonoid.subset_closure) (submonoid.closure_le.2 add_submonoid.subset_closure) lemma submonoid.to_add_submonoid'_closure (S : set (multiplicative A)) : (submonoid.closure S).to_add_submonoid' = add_submonoid.closure (additive.of_mul ⁻¹' S) := le_antisymm (submonoid.to_add_submonoid'.to_galois_connection.l_le $ submonoid.closure_le.2 add_submonoid.subset_closure) (add_submonoid.closure_le.2 submonoid.subset_closure) end namespace submonoid variables {F : Type*} [mc : monoid_hom_class F M N] open set /-! ### `comap` and `map` -/ include mc /-- The preimage of a submonoid along a monoid homomorphism is a submonoid. -/ @[to_additive "The preimage of an `add_submonoid` along an `add_monoid` homomorphism is an `add_submonoid`."] def comap (f : F) (S : submonoid N) : submonoid M := { carrier := (f ⁻¹' S), one_mem' := show f 1 ∈ S, by rw map_one; exact S.one_mem, mul_mem' := λ a b ha hb, show f (a * b) ∈ S, by rw map_mul; exact S.mul_mem ha hb } @[simp, to_additive] lemma coe_comap (S : submonoid N) (f : F) : (S.comap f : set M) = f ⁻¹' S := rfl @[simp, to_additive] lemma mem_comap {S : submonoid N} {f : F} {x : M} : x ∈ S.comap f ↔ f x ∈ S := iff.rfl omit mc @[to_additive] lemma comap_comap (S : submonoid P) (g : N →* P) (f : M →* N) : (S.comap g).comap f = S.comap (g.comp f) := rfl @[simp, to_additive] lemma comap_id (S : submonoid P) : S.comap (monoid_hom.id P) = S := ext (by simp) include mc /-- The image of a submonoid along a monoid homomorphism is a submonoid. -/ @[to_additive "The image of an `add_submonoid` along an `add_monoid` homomorphism is an `add_submonoid`."] def map (f : F) (S : submonoid M) : submonoid N := { carrier := (f '' S), one_mem' := ⟨1, S.one_mem, map_one f⟩, mul_mem' := begin rintros _ _ ⟨x, hx, rfl⟩ ⟨y, hy, rfl⟩, exact ⟨x * y, S.mul_mem hx hy, by rw map_mul; refl⟩ end } @[simp, to_additive] lemma coe_map (f : F) (S : submonoid M) : (S.map f : set N) = f '' S := rfl @[simp, to_additive] lemma mem_map {f : F} {S : submonoid M} {y : N} : y ∈ S.map f ↔ ∃ x ∈ S, f x = y := mem_image_iff_bex @[to_additive] lemma mem_map_of_mem (f : F) {S : submonoid M} {x : M} (hx : x ∈ S) : f x ∈ S.map f := mem_image_of_mem f hx @[to_additive] lemma apply_coe_mem_map (f : F) (S : submonoid M) (x : S) : f x ∈ S.map f := mem_map_of_mem f x.prop omit mc @[to_additive] lemma map_map (g : N →* P) (f : M →* N) : (S.map f).map g = S.map (g.comp f) := set_like.coe_injective $ image_image _ _ _ include mc @[to_additive] lemma mem_map_iff_mem {f : F} (hf : function.injective f) {S : submonoid M} {x : M} : f x ∈ S.map f ↔ x ∈ S := hf.mem_set_image @[to_additive] lemma map_le_iff_le_comap {f : F} {S : submonoid M} {T : submonoid N} : S.map f ≤ T ↔ S ≤ T.comap f := image_subset_iff @[to_additive] lemma gc_map_comap (f : F) : galois_connection (map f) (comap f) := λ S T, map_le_iff_le_comap @[to_additive] lemma map_le_of_le_comap {T : submonoid N} {f : F} : S ≤ T.comap f → S.map f ≤ T := (gc_map_comap f).l_le @[to_additive] lemma le_comap_of_map_le {T : submonoid N} {f : F} : S.map f ≤ T → S ≤ T.comap f := (gc_map_comap f).le_u @[to_additive] lemma le_comap_map {f : F} : S ≤ (S.map f).comap f := (gc_map_comap f).le_u_l _ @[to_additive] lemma map_comap_le {S : submonoid N} {f : F} : (S.comap f).map f ≤ S := (gc_map_comap f).l_u_le _ @[to_additive] lemma monotone_map {f : F} : monotone (map f) := (gc_map_comap f).monotone_l @[to_additive] lemma monotone_comap {f : F} : monotone (comap f) := (gc_map_comap f).monotone_u @[simp, to_additive] lemma map_comap_map {f : F} : ((S.map f).comap f).map f = S.map f := (gc_map_comap f).l_u_l_eq_l _ @[simp, to_additive] lemma comap_map_comap {S : submonoid N} {f : F} : ((S.comap f).map f).comap f = S.comap f := (gc_map_comap f).u_l_u_eq_u _ @[to_additive] lemma map_sup (S T : submonoid M) (f : F) : (S ⊔ T).map f = S.map f ⊔ T.map f := (gc_map_comap f : galois_connection (map f) (comap f)).l_sup @[to_additive] lemma map_supr {ι : Sort*} (f : F) (s : ι → submonoid M) : (supr s).map f = ⨆ i, (s i).map f := (gc_map_comap f : galois_connection (map f) (comap f)).l_supr @[to_additive] lemma comap_inf (S T : submonoid N) (f : F) : (S ⊓ T).comap f = S.comap f ⊓ T.comap f := (gc_map_comap f : galois_connection (map f) (comap f)).u_inf @[to_additive] lemma comap_infi {ι : Sort*} (f : F) (s : ι → submonoid N) : (infi s).comap f = ⨅ i, (s i).comap f := (gc_map_comap f : galois_connection (map f) (comap f)).u_infi @[simp, to_additive] lemma map_bot (f : F) : (⊥ : submonoid M).map f = ⊥ := (gc_map_comap f).l_bot @[simp, to_additive] lemma comap_top (f : F) : (⊤ : submonoid N).comap f = ⊤ := (gc_map_comap f).u_top omit mc @[simp, to_additive] lemma map_id (S : submonoid M) : S.map (monoid_hom.id M) = S := ext (λ x, ⟨λ ⟨_, h, rfl⟩, h, λ h, ⟨_, h, rfl⟩⟩) section galois_coinsertion variables {ι : Type*} {f : F} (hf : function.injective f) include hf /-- `map f` and `comap f` form a `galois_coinsertion` when `f` is injective. -/ @[to_additive /-" `map f` and `comap f` form a `galois_coinsertion` when `f` is injective. "-/] def gci_map_comap : galois_coinsertion (map f) (comap f) := (gc_map_comap f).to_galois_coinsertion (λ S x, by simp [mem_comap, mem_map, hf.eq_iff]) @[to_additive] lemma comap_map_eq_of_injective (S : submonoid M) : (S.map f).comap f = S := (gci_map_comap hf).u_l_eq _ @[to_additive] lemma comap_surjective_of_injective : function.surjective (comap f) := (gci_map_comap hf).u_surjective @[to_additive] lemma map_injective_of_injective : function.injective (map f) := (gci_map_comap hf).l_injective @[to_additive] lemma comap_inf_map_of_injective (S T : submonoid M) : (S.map f ⊓ T.map f).comap f = S ⊓ T := (gci_map_comap hf).u_inf_l _ _ @[to_additive] lemma comap_infi_map_of_injective (S : ι → submonoid M) : (⨅ i, (S i).map f).comap f = infi S := (gci_map_comap hf).u_infi_l _ @[to_additive] lemma comap_sup_map_of_injective (S T : submonoid M) : (S.map f ⊔ T.map f).comap f = S ⊔ T := (gci_map_comap hf).u_sup_l _ _ @[to_additive] lemma comap_supr_map_of_injective (S : ι → submonoid M) : (⨆ i, (S i).map f).comap f = supr S := (gci_map_comap hf).u_supr_l _ @[to_additive] lemma map_le_map_iff_of_injective {S T : submonoid M} : S.map f ≤ T.map f ↔ S ≤ T := (gci_map_comap hf).l_le_l_iff @[to_additive] lemma map_strict_mono_of_injective : strict_mono (map f) := (gci_map_comap hf).strict_mono_l end galois_coinsertion section galois_insertion variables {ι : Type*} {f : F} (hf : function.surjective f) include hf /-- `map f` and `comap f` form a `galois_insertion` when `f` is surjective. -/ @[to_additive /-" `map f` and `comap f` form a `galois_insertion` when `f` is surjective. "-/] def gi_map_comap : galois_insertion (map f) (comap f) := (gc_map_comap f).to_galois_insertion (λ S x h, let ⟨y, hy⟩ := hf x in mem_map.2 ⟨y, by simp [hy, h]⟩) @[to_additive] lemma map_comap_eq_of_surjective (S : submonoid N) : (S.comap f).map f = S := (gi_map_comap hf).l_u_eq _ @[to_additive] lemma map_surjective_of_surjective : function.surjective (map f) := (gi_map_comap hf).l_surjective @[to_additive] lemma comap_injective_of_surjective : function.injective (comap f) := (gi_map_comap hf).u_injective @[to_additive] lemma map_inf_comap_of_surjective (S T : submonoid N) : (S.comap f ⊓ T.comap f).map f = S ⊓ T := (gi_map_comap hf).l_inf_u _ _ @[to_additive] lemma map_infi_comap_of_surjective (S : ι → submonoid N) : (⨅ i, (S i).comap f).map f = infi S := (gi_map_comap hf).l_infi_u _ @[to_additive] lemma map_sup_comap_of_surjective (S T : submonoid N) : (S.comap f ⊔ T.comap f).map f = S ⊔ T := (gi_map_comap hf).l_sup_u _ _ @[to_additive] lemma map_supr_comap_of_surjective (S : ι → submonoid N) : (⨆ i, (S i).comap f).map f = supr S := (gi_map_comap hf).l_supr_u _ @[to_additive] lemma comap_le_comap_iff_of_surjective {S T : submonoid N} : S.comap f ≤ T.comap f ↔ S ≤ T := (gi_map_comap hf).u_le_u_iff @[to_additive] lemma comap_strict_mono_of_surjective : strict_mono (comap f) := (gi_map_comap hf).strict_mono_u end galois_insertion end submonoid namespace one_mem_class variables {A M₁ : Type*} [set_like A M₁] [has_one M₁] [hA : one_mem_class A M₁] (S' : A) include hA /-- A submonoid of a monoid inherits a 1. -/ @[to_additive "An `add_submonoid` of an `add_monoid` inherits a zero."] instance has_one : has_one S' := ⟨⟨1, one_mem_class.one_mem S'⟩⟩ @[simp, norm_cast, to_additive] lemma coe_one : ((1 : S') : M₁) = 1 := rfl variables {S'} @[simp, norm_cast, to_additive] lemma coe_eq_one {x : S'} : (↑x : M₁) = 1 ↔ x = 1 := (subtype.ext_iff.symm : (x : M₁) = (1 : S') ↔ x = 1) variables (S') @[to_additive] lemma one_def : (1 : S') = ⟨1, one_mem_class.one_mem S'⟩ := rfl end one_mem_class namespace submonoid_class variables {A : Type*} [set_like A M] [hA : submonoid_class A M] (S' : A) /-- An `add_submonoid` of an `add_monoid` inherits a scalar multiplication. -/ instance _root_.add_submonoid_class.has_nsmul {M} [add_monoid M] {A : Type*} [set_like A M] [add_submonoid_class A M] (S : A) : has_smul ℕ S := ⟨λ n a, ⟨n • a.1, nsmul_mem a.2 n⟩⟩ /-- A submonoid of a monoid inherits a power operator. -/ instance has_pow {M} [monoid M] {A : Type*} [set_like A M] [submonoid_class A M] (S : A) : has_pow S ℕ := ⟨λ a n, ⟨a.1 ^ n, pow_mem a.2 n⟩⟩ attribute [to_additive] submonoid_class.has_pow @[simp, norm_cast, to_additive] lemma coe_pow {M} [monoid M] {A : Type*} [set_like A M] [submonoid_class A M] {S : A} (x : S) (n : ℕ) : (↑(x ^ n) : M) = ↑x ^ n := rfl @[simp, to_additive] lemma mk_pow {M} [monoid M] {A : Type*} [set_like A M] [submonoid_class A M] {S : A} (x : M) (hx : x ∈ S) (n : ℕ) : (⟨x, hx⟩ : S) ^ n = ⟨x ^ n, pow_mem hx n⟩ := rfl /-- A submonoid of a unital magma inherits a unital magma structure. -/ @[to_additive "An `add_submonoid` of an unital additive magma inherits an unital additive magma structure.", priority 75] -- Prefer subclasses of `monoid` over subclasses of `submonoid_class`. instance to_mul_one_class {M : Type*} [mul_one_class M] {A : Type*} [set_like A M] [submonoid_class A M] (S : A) : mul_one_class S := subtype.coe_injective.mul_one_class _ rfl (λ _ _, rfl) /-- A submonoid of a monoid inherits a monoid structure. -/ @[to_additive "An `add_submonoid` of an `add_monoid` inherits an `add_monoid` structure.", priority 75] -- Prefer subclasses of `monoid` over subclasses of `submonoid_class`. instance to_monoid {M : Type*} [monoid M] {A : Type*} [set_like A M] [submonoid_class A M] (S : A) : monoid S := subtype.coe_injective.monoid coe rfl (λ _ _, rfl) (λ _ _, rfl) /-- A submonoid of a `comm_monoid` is a `comm_monoid`. -/ @[to_additive "An `add_submonoid` of an `add_comm_monoid` is an `add_comm_monoid`.", priority 75] -- Prefer subclasses of `monoid` over subclasses of `submonoid_class`. instance to_comm_monoid {M} [comm_monoid M] {A : Type*} [set_like A M] [submonoid_class A M] (S : A) : comm_monoid S := subtype.coe_injective.comm_monoid coe rfl (λ _ _, rfl) (λ _ _, rfl) /-- A submonoid of an `ordered_comm_monoid` is an `ordered_comm_monoid`. -/ @[to_additive "An `add_submonoid` of an `ordered_add_comm_monoid` is an `ordered_add_comm_monoid`.", priority 75] -- Prefer subclasses of `monoid` over subclasses of `submonoid_class`. instance to_ordered_comm_monoid {M} [ordered_comm_monoid M] {A : Type*} [set_like A M] [submonoid_class A M] (S : A) : ordered_comm_monoid S := subtype.coe_injective.ordered_comm_monoid coe rfl (λ _ _, rfl) (λ _ _, rfl) /-- A submonoid of a `linear_ordered_comm_monoid` is a `linear_ordered_comm_monoid`. -/ @[to_additive "An `add_submonoid` of a `linear_ordered_add_comm_monoid` is a `linear_ordered_add_comm_monoid`.", priority 75] -- Prefer subclasses of `monoid` over subclasses of `submonoid_class`. instance to_linear_ordered_comm_monoid {M} [linear_ordered_comm_monoid M] {A : Type*} [set_like A M] [submonoid_class A M] (S : A) : linear_ordered_comm_monoid S := subtype.coe_injective.linear_ordered_comm_monoid coe rfl (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) /-- A submonoid of an `ordered_cancel_comm_monoid` is an `ordered_cancel_comm_monoid`. -/ @[to_additive "An `add_submonoid` of an `ordered_cancel_add_comm_monoid` is an `ordered_cancel_add_comm_monoid`.", priority 75] -- Prefer subclasses of `monoid` over subclasses of `submonoid_class`. instance to_ordered_cancel_comm_monoid {M} [ordered_cancel_comm_monoid M] {A : Type*} [set_like A M] [submonoid_class A M] (S : A) : ordered_cancel_comm_monoid S := subtype.coe_injective.ordered_cancel_comm_monoid coe rfl (λ _ _, rfl) (λ _ _, rfl) /-- A submonoid of a `linear_ordered_cancel_comm_monoid` is a `linear_ordered_cancel_comm_monoid`. -/ @[to_additive "An `add_submonoid` of a `linear_ordered_cancel_add_comm_monoid` is a `linear_ordered_cancel_add_comm_monoid`.", priority 75] -- Prefer subclasses of `monoid` over subclasses of `submonoid_class`. instance to_linear_ordered_cancel_comm_monoid {M} [linear_ordered_cancel_comm_monoid M] {A : Type*} [set_like A M] [submonoid_class A M] (S : A) : linear_ordered_cancel_comm_monoid S := subtype.coe_injective.linear_ordered_cancel_comm_monoid coe rfl (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) include hA /-- The natural monoid hom from a submonoid of monoid `M` to `M`. -/ @[to_additive "The natural monoid hom from an `add_submonoid` of `add_monoid` `M` to `M`."] def subtype : S' →* M := ⟨coe, rfl, λ _ _, rfl⟩ @[simp, to_additive] theorem coe_subtype : (submonoid_class.subtype S' : S' → M) = coe := rfl end submonoid_class namespace submonoid /-- A submonoid of a monoid inherits a multiplication. -/ @[to_additive "An `add_submonoid` of an `add_monoid` inherits an addition."] instance has_mul : has_mul S := ⟨λ a b, ⟨a.1 * b.1, S.mul_mem a.2 b.2⟩⟩ /-- A submonoid of a monoid inherits a 1. -/ @[to_additive "An `add_submonoid` of an `add_monoid` inherits a zero."] instance has_one : has_one S := ⟨⟨_, S.one_mem⟩⟩ @[simp, norm_cast, to_additive] lemma coe_mul (x y : S) : (↑(x * y) : M) = ↑x * ↑y := rfl @[simp, norm_cast, to_additive] lemma coe_one : ((1 : S) : M) = 1 := rfl @[simp, to_additive] lemma mk_mul_mk (x y : M) (hx : x ∈ S) (hy : y ∈ S) : (⟨x, hx⟩ : S) * ⟨y, hy⟩ = ⟨x * y, S.mul_mem hx hy⟩ := rfl @[to_additive] lemma mul_def (x y : S) : x * y = ⟨x * y, S.mul_mem x.2 y.2⟩ := rfl @[to_additive] lemma one_def : (1 : S) = ⟨1, S.one_mem⟩ := rfl /-- A submonoid of a unital magma inherits a unital magma structure. -/ @[to_additive "An `add_submonoid` of an unital additive magma inherits an unital additive magma structure."] instance to_mul_one_class {M : Type*} [mul_one_class M] (S : submonoid M) : mul_one_class S := subtype.coe_injective.mul_one_class coe rfl (λ _ _, rfl) @[to_additive] protected lemma pow_mem {M : Type*} [monoid M] (S : submonoid M) {x : M} (hx : x ∈ S) (n : ℕ) : x ^ n ∈ S := pow_mem hx n @[simp, norm_cast, to_additive] theorem coe_pow {M : Type*} [monoid M] {S : submonoid M} (x : S) (n : ℕ) : ↑(x ^ n) = (x ^ n : M) := rfl /-- A submonoid of a monoid inherits a monoid structure. -/ @[to_additive "An `add_submonoid` of an `add_monoid` inherits an `add_monoid` structure."] instance to_monoid {M : Type*} [monoid M] (S : submonoid M) : monoid S := subtype.coe_injective.monoid coe rfl (λ _ _, rfl) (λ _ _, rfl) /-- A submonoid of a `comm_monoid` is a `comm_monoid`. -/ @[to_additive "An `add_submonoid` of an `add_comm_monoid` is an `add_comm_monoid`."] instance to_comm_monoid {M} [comm_monoid M] (S : submonoid M) : comm_monoid S := subtype.coe_injective.comm_monoid coe rfl (λ _ _, rfl) (λ _ _, rfl) /-- A submonoid of an `ordered_comm_monoid` is an `ordered_comm_monoid`. -/ @[to_additive "An `add_submonoid` of an `ordered_add_comm_monoid` is an `ordered_add_comm_monoid`."] instance to_ordered_comm_monoid {M} [ordered_comm_monoid M] (S : submonoid M) : ordered_comm_monoid S := subtype.coe_injective.ordered_comm_monoid coe rfl (λ _ _, rfl) (λ _ _, rfl) /-- A submonoid of a `linear_ordered_comm_monoid` is a `linear_ordered_comm_monoid`. -/ @[to_additive "An `add_submonoid` of a `linear_ordered_add_comm_monoid` is a `linear_ordered_add_comm_monoid`."] instance to_linear_ordered_comm_monoid {M} [linear_ordered_comm_monoid M] (S : submonoid M) : linear_ordered_comm_monoid S := subtype.coe_injective.linear_ordered_comm_monoid coe rfl (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) /-- A submonoid of an `ordered_cancel_comm_monoid` is an `ordered_cancel_comm_monoid`. -/ @[to_additive "An `add_submonoid` of an `ordered_cancel_add_comm_monoid` is an `ordered_cancel_add_comm_monoid`."] instance to_ordered_cancel_comm_monoid {M} [ordered_cancel_comm_monoid M] (S : submonoid M) : ordered_cancel_comm_monoid S := subtype.coe_injective.ordered_cancel_comm_monoid coe rfl (λ _ _, rfl) (λ _ _, rfl) /-- A submonoid of a `linear_ordered_cancel_comm_monoid` is a `linear_ordered_cancel_comm_monoid`. -/ @[to_additive "An `add_submonoid` of a `linear_ordered_cancel_add_comm_monoid` is a `linear_ordered_cancel_add_comm_monoid`."] instance to_linear_ordered_cancel_comm_monoid {M} [linear_ordered_cancel_comm_monoid M] (S : submonoid M) : linear_ordered_cancel_comm_monoid S := subtype.coe_injective.linear_ordered_cancel_comm_monoid coe rfl (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) /-- The natural monoid hom from a submonoid of monoid `M` to `M`. -/ @[to_additive "The natural monoid hom from an `add_submonoid` of `add_monoid` `M` to `M`."] def subtype : S →* M := ⟨coe, rfl, λ _ _, rfl⟩ @[simp, to_additive] theorem coe_subtype : ⇑S.subtype = coe := rfl /-- The top submonoid is isomorphic to the monoid. -/ @[to_additive "The top additive submonoid is isomorphic to the additive monoid.", simps] def top_equiv : (⊤ : submonoid M) ≃* M := { to_fun := λ x, x, inv_fun := λ x, ⟨x, mem_top x⟩, left_inv := λ x, x.eta _, right_inv := λ _, rfl, map_mul' := λ _ _, rfl } @[simp, to_additive] lemma top_equiv_to_monoid_hom : (top_equiv : _ ≃* M).to_monoid_hom = (⊤ : submonoid M).subtype := rfl /-- A submonoid is isomorphic to its image under an injective function -/ @[to_additive "An additive submonoid is isomorphic to its image under an injective function"] noncomputable def equiv_map_of_injective (f : M →* N) (hf : function.injective f) : S ≃* S.map f := { map_mul' := λ _ _, subtype.ext (f.map_mul _ _), ..equiv.set.image f S hf } @[simp, to_additive] lemma coe_equiv_map_of_injective_apply (f : M →* N) (hf : function.injective f) (x : S) : (equiv_map_of_injective S f hf x : N) = f x := rfl @[simp, to_additive] lemma closure_closure_coe_preimage {s : set M} : closure ((coe : closure s → M) ⁻¹' s) = ⊤ := eq_top_iff.2 $ λ x, subtype.rec_on x $ λ x hx _, begin refine closure_induction' _ (λ g hg, _) _ (λ g₁ g₂ hg₁ hg₂, _) hx, { exact subset_closure hg }, { exact submonoid.one_mem _ }, { exact submonoid.mul_mem _ }, end /-- Given `submonoid`s `s`, `t` of monoids `M`, `N` respectively, `s × t` as a submonoid of `M × N`. -/ @[to_additive prod "Given `add_submonoid`s `s`, `t` of `add_monoid`s `A`, `B` respectively, `s × t` as an `add_submonoid` of `A × B`."] def prod (s : submonoid M) (t : submonoid N) : submonoid (M × N) := { carrier := s ×ˢ t, one_mem' := ⟨s.one_mem, t.one_mem⟩, mul_mem' := λ p q hp hq, ⟨s.mul_mem hp.1 hq.1, t.mul_mem hp.2 hq.2⟩ } @[to_additive coe_prod] lemma coe_prod (s : submonoid M) (t : submonoid N) : (s.prod t : set (M × N)) = s ×ˢ t := rfl @[to_additive mem_prod] lemma mem_prod {s : submonoid M} {t : submonoid N} {p : M × N} : p ∈ s.prod t ↔ p.1 ∈ s ∧ p.2 ∈ t := iff.rfl @[to_additive prod_mono] lemma prod_mono {s₁ s₂ : submonoid M} {t₁ t₂ : submonoid N} (hs : s₁ ≤ s₂) (ht : t₁ ≤ t₂) : s₁.prod t₁ ≤ s₂.prod t₂ := set.prod_mono hs ht @[to_additive prod_top] lemma prod_top (s : submonoid M) : s.prod (⊤ : submonoid N) = s.comap (monoid_hom.fst M N) := ext $ λ x, by simp [mem_prod, monoid_hom.coe_fst] @[to_additive top_prod] lemma top_prod (s : submonoid N) : (⊤ : submonoid M).prod s = s.comap (monoid_hom.snd M N) := ext $ λ x, by simp [mem_prod, monoid_hom.coe_snd] @[simp, to_additive top_prod_top] lemma top_prod_top : (⊤ : submonoid M).prod (⊤ : submonoid N) = ⊤ := (top_prod _).trans $ comap_top _ @[to_additive] lemma bot_prod_bot : (⊥ : submonoid M).prod (⊥ : submonoid N) = ⊥ := set_like.coe_injective $ by simp [coe_prod, prod.one_eq_mk] /-- The product of submonoids is isomorphic to their product as monoids. -/ @[to_additive prod_equiv "The product of additive submonoids is isomorphic to their product as additive monoids"] def prod_equiv (s : submonoid M) (t : submonoid N) : s.prod t ≃* s × t := { map_mul' := λ x y, rfl, .. equiv.set.prod ↑s ↑t } open monoid_hom @[to_additive] lemma map_inl (s : submonoid M) : s.map (inl M N) = s.prod ⊥ := ext $ λ p, ⟨λ ⟨x, hx, hp⟩, hp ▸ ⟨hx, set.mem_singleton 1⟩, λ ⟨hps, hp1⟩, ⟨p.1, hps, prod.ext rfl $ (set.eq_of_mem_singleton hp1).symm⟩⟩ @[to_additive] lemma map_inr (s : submonoid N) : s.map (inr M N) = prod ⊥ s := ext $ λ p, ⟨λ ⟨x, hx, hp⟩, hp ▸ ⟨set.mem_singleton 1, hx⟩, λ ⟨hp1, hps⟩, ⟨p.2, hps, prod.ext (set.eq_of_mem_singleton hp1).symm rfl⟩⟩ @[simp, to_additive prod_bot_sup_bot_prod] lemma prod_bot_sup_bot_prod (s : submonoid M) (t : submonoid N) : (s.prod ⊥) ⊔ (prod ⊥ t) = s.prod t := le_antisymm (sup_le (prod_mono (le_refl s) bot_le) (prod_mono bot_le (le_refl t))) $ assume p hp, prod.fst_mul_snd p ▸ mul_mem ((le_sup_left : s.prod ⊥ ≤ s.prod ⊥ ⊔ prod ⊥ t) ⟨hp.1, set.mem_singleton 1⟩) ((le_sup_right : prod ⊥ t ≤ s.prod ⊥ ⊔ prod ⊥ t) ⟨set.mem_singleton 1, hp.2⟩) @[to_additive] lemma mem_map_equiv {f : M ≃* N} {K : submonoid M} {x : N} : x ∈ K.map f.to_monoid_hom ↔ f.symm x ∈ K := @set.mem_image_equiv _ _ ↑K f.to_equiv x @[to_additive] lemma map_equiv_eq_comap_symm (f : M ≃* N) (K : submonoid M) : K.map f.to_monoid_hom = K.comap f.symm.to_monoid_hom := set_like.coe_injective (f.to_equiv.image_eq_preimage K) @[to_additive] lemma comap_equiv_eq_map_symm (f : N ≃* M) (K : submonoid M) : K.comap f.to_monoid_hom = K.map f.symm.to_monoid_hom := (map_equiv_eq_comap_symm f.symm K).symm @[simp, to_additive] lemma map_equiv_top (f : M ≃* N) : (⊤ : submonoid M).map f.to_monoid_hom = ⊤ := set_like.coe_injective $ set.image_univ.trans f.surjective.range_eq @[to_additive le_prod_iff] lemma le_prod_iff {s : submonoid M} {t : submonoid N} {u : submonoid (M × N)} : u ≤ s.prod t ↔ u.map (fst M N) ≤ s ∧ u.map (snd M N) ≤ t := begin split, { intros h, split, { rintros x ⟨⟨y1,y2⟩, ⟨hy1,rfl⟩⟩, exact (h hy1).1 }, { rintros x ⟨⟨y1,y2⟩, ⟨hy1,rfl⟩⟩, exact (h hy1).2 }, }, { rintros ⟨hH, hK⟩ ⟨x1, x2⟩ h, exact ⟨hH ⟨_ , h, rfl⟩, hK ⟨ _, h, rfl⟩⟩, } end @[to_additive prod_le_iff] lemma prod_le_iff {s : submonoid M} {t : submonoid N} {u : submonoid (M × N)} : s.prod t ≤ u ↔ s.map (inl M N) ≤ u ∧ t.map (inr M N) ≤ u := begin split, { intros h, split, { rintros _ ⟨x, hx, rfl⟩, apply h, exact ⟨hx, (submonoid.one_mem _)⟩, }, { rintros _ ⟨x, hx, rfl⟩, apply h, exact ⟨submonoid.one_mem _, hx⟩, }, }, { rintros ⟨hH, hK⟩ ⟨x1, x2⟩ ⟨h1, h2⟩, have h1' : inl M N x1 ∈ u, { apply hH, simpa using h1, }, have h2' : inr M N x2 ∈ u, { apply hK, simpa using h2, }, simpa using submonoid.mul_mem _ h1' h2', } end end submonoid namespace monoid_hom variables {F : Type*} [mc : monoid_hom_class F M N] open submonoid /-- For many categories (monoids, modules, rings, ...) the set-theoretic image of a morphism `f` is a subobject of the codomain. When this is the case, it is useful to define the range of a morphism in such a way that the underlying carrier set of the range subobject is definitionally `set.range f`. In particular this means that the types `↥(set.range f)` and `↥f.range` are interchangeable without proof obligations. A convenient candidate definition for range which is mathematically correct is `map ⊤ f`, just as `set.range` could have been defined as `f '' set.univ`. However, this lacks the desired definitional convenience, in that it both does not match `set.range`, and that it introduces a redudant `x ∈ ⊤` term which clutters proofs. In such a case one may resort to the `copy` pattern. A `copy` function converts the definitional problem for the carrier set of a subobject into a one-off propositional proof obligation which one discharges while writing the definition of the definitionally convenient range (the parameter `hs` in the example below). A good example is the case of a morphism of monoids. A convenient definition for `monoid_hom.mrange` would be `(⊤ : submonoid M).map f`. However since this lacks the required definitional convenience, we first define `submonoid.copy` as follows: ```lean protected def copy (S : submonoid M) (s : set M) (hs : s = S) : submonoid M := { carrier := s, one_mem' := hs.symm ▸ S.one_mem', mul_mem' := hs.symm ▸ S.mul_mem' } ``` and then finally define: ```lean def mrange (f : M →* N) : submonoid N := ((⊤ : submonoid M).map f).copy (set.range f) set.image_univ.symm ``` -/ library_note "range copy pattern" include mc /-- The range of a monoid homomorphism is a submonoid. See Note [range copy pattern]. -/ @[to_additive "The range of an `add_monoid_hom` is an `add_submonoid`."] def mrange (f : F) : submonoid N := ((⊤ : submonoid M).map f).copy (set.range f) set.image_univ.symm @[simp, to_additive] lemma coe_mrange (f : F) : (mrange f : set N) = set.range f := rfl @[simp, to_additive] lemma mem_mrange {f : F} {y : N} : y ∈ mrange f ↔ ∃ x, f x = y := iff.rfl @[to_additive] lemma mrange_eq_map (f : F) : mrange f = (⊤ : submonoid M).map f := copy_eq _ omit mc @[to_additive] lemma map_mrange (g : N →* P) (f : M →* N) : f.mrange.map g = (g.comp f).mrange := by simpa only [mrange_eq_map] using (⊤ : submonoid M).map_map g f include mc @[to_additive] lemma mrange_top_iff_surjective {f : F} : mrange f = (⊤ : submonoid N) ↔ function.surjective f := set_like.ext'_iff.trans $ iff.trans (by rw [coe_mrange, coe_top]) set.range_iff_surjective /-- The range of a surjective monoid hom is the whole of the codomain. -/ @[to_additive "The range of a surjective `add_monoid` hom is the whole of the codomain."] lemma mrange_top_of_surjective (f : F) (hf : function.surjective f) : mrange f = (⊤ : submonoid N) := mrange_top_iff_surjective.2 hf @[to_additive] lemma mclosure_preimage_le (f : F) (s : set N) : closure (f ⁻¹' s) ≤ (closure s).comap f := closure_le.2 $ λ x hx, set_like.mem_coe.2 $ mem_comap.2 $ subset_closure hx /-- The image under a monoid hom of the submonoid generated by a set equals the submonoid generated by the image of the set. -/ @[to_additive "The image under an `add_monoid` hom of the `add_submonoid` generated by a set equals the `add_submonoid` generated by the image of the set."] lemma map_mclosure (f : F) (s : set M) : (closure s).map f = closure (f '' s) := le_antisymm (map_le_iff_le_comap.2 $ le_trans (closure_mono $ set.subset_preimage_image _ _) (mclosure_preimage_le _ _)) (closure_le.2 $ set.image_subset _ subset_closure) omit mc /-- Restriction of a monoid hom to a submonoid of the domain. -/ @[to_additive "Restriction of an add_monoid hom to an `add_submonoid` of the domain."] def restrict {N S : Type*} [mul_one_class N] [set_like S M] [submonoid_class S M] (f : M →* N) (s : S) : s →* N := f.comp (submonoid_class.subtype _) @[simp, to_additive] lemma restrict_apply {N S : Type*} [mul_one_class N] [set_like S M] [submonoid_class S M] (f : M →* N) (s : S) (x : s) : f.restrict s x = f x := rfl /-- Restriction of a monoid hom to a submonoid of the codomain. -/ @[to_additive "Restriction of an `add_monoid` hom to an `add_submonoid` of the codomain.", simps apply] def cod_restrict {S} [set_like S N] [submonoid_class S N] (f : M →* N) (s : S) (h : ∀ x, f x ∈ s) : M →* s := { to_fun := λ n, ⟨f n, h n⟩, map_one' := subtype.eq f.map_one, map_mul' := λ x y, subtype.eq (f.map_mul x y) } /-- Restriction of a monoid hom to its range interpreted as a submonoid. -/ @[to_additive "Restriction of an `add_monoid` hom to its range interpreted as a submonoid."] def mrange_restrict {N} [mul_one_class N] (f : M →* N) : M →* f.mrange := f.cod_restrict f.mrange $ λ x, ⟨x, rfl⟩ @[simp, to_additive] lemma coe_mrange_restrict {N} [mul_one_class N] (f : M →* N) (x : M) : (f.mrange_restrict x : N) = f x := rfl @[to_additive] lemma mrange_restrict_surjective (f : M →* N) : function.surjective f.mrange_restrict := λ ⟨_, ⟨x, rfl⟩⟩, ⟨x, rfl⟩ include mc /-- The multiplicative kernel of a monoid homomorphism is the submonoid of elements `x : G` such that `f x = 1` -/ @[to_additive "The additive kernel of an `add_monoid` homomorphism is the `add_submonoid` of elements such that `f x = 0`"] def mker (f : F) : submonoid M := (⊥ : submonoid N).comap f @[to_additive] lemma mem_mker (f : F) {x : M} : x ∈ mker f ↔ f x = 1 := iff.rfl @[to_additive] lemma coe_mker (f : F) : (mker f : set M) = (f : M → N) ⁻¹' {1} := rfl @[to_additive] instance decidable_mem_mker [decidable_eq N] (f : F) : decidable_pred (∈ mker f) := λ x, decidable_of_iff (f x = 1) (mem_mker f) omit mc @[to_additive] lemma comap_mker (g : N →* P) (f : M →* N) : g.mker.comap f = (g.comp f).mker := rfl include mc @[simp, to_additive] lemma comap_bot' (f : F) : (⊥ : submonoid N).comap f = mker f := rfl omit mc @[to_additive] lemma range_restrict_mker (f : M →* N) : mker (mrange_restrict f) = mker f := begin ext, change (⟨f x, _⟩ : mrange f) = ⟨1, _⟩ ↔ f x = 1, simp only [], end @[simp, to_additive] lemma mker_one : (1 : M →* N).mker = ⊤ := by { ext, simp [mem_mker] } @[to_additive] lemma prod_map_comap_prod' {M' : Type*} {N' : Type*} [mul_one_class M'] [mul_one_class N'] (f : M →* N) (g : M' →* N') (S : submonoid N) (S' : submonoid N') : (S.prod S').comap (prod_map f g) = (S.comap f).prod (S'.comap g) := set_like.coe_injective $ set.preimage_prod_map_prod f g _ _ @[to_additive] lemma mker_prod_map {M' : Type*} {N' : Type*} [mul_one_class M'] [mul_one_class N'] (f : M →* N) (g : M' →* N') : (prod_map f g).mker = f.mker.prod g.mker := by rw [←comap_bot', ←comap_bot', ←comap_bot', ←prod_map_comap_prod', bot_prod_bot] @[simp, to_additive] lemma mker_inl : (inl M N).mker = ⊥ := by { ext x, simp [mem_mker] } @[simp, to_additive] lemma mker_inr : (inr M N).mker = ⊥ := by { ext x, simp [mem_mker] } /-- The `monoid_hom` from the preimage of a submonoid to itself. -/ @[to_additive "the `add_monoid_hom` from the preimage of an additive submonoid to itself.", simps] def submonoid_comap (f : M →* N) (N' : submonoid N) : N'.comap f →* N' := { to_fun := λ x, ⟨f x, x.prop⟩, map_one' := subtype.eq f.map_one, map_mul' := λ x y, subtype.eq (f.map_mul x y) } /-- The `monoid_hom` from a submonoid to its image. See `mul_equiv.submonoid_map` for a variant for `mul_equiv`s. -/ @[to_additive "the `add_monoid_hom` from an additive submonoid to its image. See `add_equiv.add_submonoid_map` for a variant for `add_equiv`s.", simps] def submonoid_map (f : M →* N) (M' : submonoid M) : M' →* M'.map f := { to_fun := λ x, ⟨f x, ⟨x, x.prop, rfl⟩⟩, map_one' := subtype.eq $ f.map_one, map_mul' := λ x y, subtype.eq $ f.map_mul x y } @[to_additive] lemma submonoid_map_surjective (f : M →* N) (M' : submonoid M) : function.surjective (f.submonoid_map M') := by { rintro ⟨_, x, hx, rfl⟩, exact ⟨⟨x, hx⟩, rfl⟩ } end monoid_hom namespace submonoid open monoid_hom @[to_additive] lemma mrange_inl : (inl M N).mrange = prod ⊤ ⊥ := by simpa only [mrange_eq_map] using map_inl ⊤ @[to_additive] lemma mrange_inr : (inr M N).mrange = prod ⊥ ⊤ := by simpa only [mrange_eq_map] using map_inr ⊤ @[to_additive] lemma mrange_inl' : (inl M N).mrange = comap (snd M N) ⊥ := mrange_inl.trans (top_prod _) @[to_additive] lemma mrange_inr' : (inr M N).mrange = comap (fst M N) ⊥ := mrange_inr.trans (prod_top _) @[simp, to_additive] lemma mrange_fst : (fst M N).mrange = ⊤ := mrange_top_of_surjective (fst M N) $ @prod.fst_surjective _ _ ⟨1⟩ @[simp, to_additive] lemma mrange_snd : (snd M N).mrange = ⊤ := mrange_top_of_surjective (snd M N) $ @prod.snd_surjective _ _ ⟨1⟩ @[to_additive] lemma prod_eq_bot_iff {s : submonoid M} {t : submonoid N} : s.prod t = ⊥ ↔ s = ⊥ ∧ t = ⊥ := by simp only [eq_bot_iff, prod_le_iff, (gc_map_comap _).le_iff_le, comap_bot', mker_inl, mker_inr] @[to_additive] lemma prod_eq_top_iff {s : submonoid M} {t : submonoid N} : s.prod t = ⊤ ↔ s = ⊤ ∧ t = ⊤ := by simp only [eq_top_iff, le_prod_iff, ← (gc_map_comap _).le_iff_le, ← mrange_eq_map, mrange_fst, mrange_snd] @[simp, to_additive] lemma mrange_inl_sup_mrange_inr : (inl M N).mrange ⊔ (inr M N).mrange = ⊤ := by simp only [mrange_inl, mrange_inr, prod_bot_sup_bot_prod, top_prod_top] /-- The monoid hom associated to an inclusion of submonoids. -/ @[to_additive "The `add_monoid` hom associated to an inclusion of submonoids."] def inclusion {S T : submonoid M} (h : S ≤ T) : S →* T := S.subtype.cod_restrict _ (λ x, h x.2) @[simp, to_additive] lemma range_subtype (s : submonoid M) : s.subtype.mrange = s := set_like.coe_injective $ (coe_mrange _).trans $ subtype.range_coe @[to_additive] lemma eq_top_iff' : S = ⊤ ↔ ∀ x : M, x ∈ S := eq_top_iff.trans ⟨λ h m, h $ mem_top m, λ h m _, h m⟩ @[to_additive] lemma eq_bot_iff_forall : S = ⊥ ↔ ∀ x ∈ S, x = (1 : M) := set_like.ext_iff.trans $ by simp [iff_def, S.one_mem] { contextual := tt } @[to_additive] lemma nontrivial_iff_exists_ne_one (S : submonoid M) : nontrivial S ↔ ∃ x ∈ S, x ≠ (1:M) := calc nontrivial S ↔ ∃ x : S, x ≠ 1 : nontrivial_iff_exists_ne 1 ... ↔ ∃ x (hx : x ∈ S), (⟨x, hx⟩ : S) ≠ ⟨1, S.one_mem⟩ : subtype.exists ... ↔ ∃ x ∈ S, x ≠ (1 : M) : by simp only [ne.def] /-- A submonoid is either the trivial submonoid or nontrivial. -/ @[to_additive "An additive submonoid is either the trivial additive submonoid or nontrivial."] lemma bot_or_nontrivial (S : submonoid M) : S = ⊥ ∨ nontrivial S := by simp only [eq_bot_iff_forall, nontrivial_iff_exists_ne_one, ← not_forall, classical.em] /-- A submonoid is either the trivial submonoid or contains a nonzero element. -/ @[to_additive "An additive submonoid is either the trivial additive submonoid or contains a nonzero element."] lemma bot_or_exists_ne_one (S : submonoid M) : S = ⊥ ∨ ∃ x ∈ S, x ≠ (1:M) := S.bot_or_nontrivial.imp_right S.nontrivial_iff_exists_ne_one.mp end submonoid namespace mul_equiv variables {S} {T : submonoid M} /-- Makes the identity isomorphism from a proof that two submonoids of a multiplicative monoid are equal. -/ @[to_additive "Makes the identity additive isomorphism from a proof two submonoids of an additive monoid are equal."] def submonoid_congr (h : S = T) : S ≃* T := { map_mul' := λ _ _, rfl, ..equiv.set_congr $ congr_arg _ h } -- this name is primed so that the version to `f.range` instead of `f.mrange` can be unprimed. /-- A monoid homomorphism `f : M →* N` with a left-inverse `g : N → M` defines a multiplicative equivalence between `M` and `f.mrange`. This is a bidirectional version of `monoid_hom.mrange_restrict`. -/ @[to_additive /-" An additive monoid homomorphism `f : M →+ N` with a left-inverse `g : N → M` defines an additive equivalence between `M` and `f.mrange`. This is a bidirectional version of `add_monoid_hom.mrange_restrict`. "-/, simps {simp_rhs := tt}] def of_left_inverse' (f : M →* N) {g : N → M} (h : function.left_inverse g f) : M ≃* f.mrange := { to_fun := f.mrange_restrict, inv_fun := g ∘ f.mrange.subtype, left_inv := h, right_inv := λ x, subtype.ext $ let ⟨x', hx'⟩ := monoid_hom.mem_mrange.mp x.prop in show f (g x) = x, by rw [←hx', h x'], .. f.mrange_restrict } /-- A `mul_equiv` `φ` between two monoids `M` and `N` induces a `mul_equiv` between a submonoid `S ≤ M` and the submonoid `φ(S) ≤ N`. See `monoid_hom.submonoid_map` for a variant for `monoid_hom`s. -/ @[to_additive "An `add_equiv` `φ` between two additive monoids `M` and `N` induces an `add_equiv` between a submonoid `S ≤ M` and the submonoid `φ(S) ≤ N`. See `add_monoid_hom.add_submonoid_map` for a variant for `add_monoid_hom`s.", simps] def submonoid_map (e : M ≃* N) (S : submonoid M) : S ≃* S.map e.to_monoid_hom := { to_fun := λ x, ⟨e x, _⟩, inv_fun := λ x, ⟨e.symm x, _⟩, -- we restate this for `simps` to avoid `⇑e.symm.to_equiv x` ..e.to_monoid_hom.submonoid_map S, ..e.to_equiv.image S } end mul_equiv section actions /-! ### Actions by `submonoid`s These instances tranfer the action by an element `m : M` of a monoid `M` written as `m • a` onto the action by an element `s : S` of a submonoid `S : submonoid M` such that `s • a = (s : M) • a`. These instances work particularly well in conjunction with `monoid.to_mul_action`, enabling `s • m` as an alias for `↑s * m`. -/ namespace submonoid variables {M' : Type*} {α β : Type*} section mul_one_class variables [mul_one_class M'] @[to_additive] instance [has_smul M' α] (S : submonoid M') : has_smul S α := has_smul.comp _ S.subtype @[to_additive] instance smul_comm_class_left [has_smul M' β] [has_smul α β] [smul_comm_class M' α β] (S : submonoid M') : smul_comm_class S α β := ⟨λ a, (smul_comm (a : M') : _)⟩ @[to_additive] instance smul_comm_class_right [has_smul α β] [has_smul M' β] [smul_comm_class α M' β] (S : submonoid M') : smul_comm_class α S β := ⟨λ a s, (smul_comm a (s : M') : _)⟩ /-- Note that this provides `is_scalar_tower S M' M'` which is needed by `smul_mul_assoc`. -/ instance [has_smul α β] [has_smul M' α] [has_smul M' β] [is_scalar_tower M' α β] (S : submonoid M') : is_scalar_tower S α β := ⟨λ a, (smul_assoc (a : M') : _)⟩ @[to_additive] lemma smul_def [has_smul M' α] {S : submonoid M'} (g : S) (m : α) : g • m = (g : M') • m := rfl instance [has_smul M' α] [has_faithful_smul M' α] (S : submonoid M') : has_faithful_smul S α := ⟨λ x y h, subtype.ext $ eq_of_smul_eq_smul h⟩ end mul_one_class variables [monoid M'] /-- The action by a submonoid is the action by the underlying monoid. -/ @[to_additive /-"The additive action by an add_submonoid is the action by the underlying add_monoid. "-/] instance [mul_action M' α] (S : submonoid M') : mul_action S α := mul_action.comp_hom _ S.subtype /-- The action by a submonoid is the action by the underlying monoid. -/ instance [add_monoid α] [distrib_mul_action M' α] (S : submonoid M') : distrib_mul_action S α := distrib_mul_action.comp_hom _ S.subtype /-- The action by a submonoid is the action by the underlying monoid. -/ instance [monoid α] [mul_distrib_mul_action M' α] (S : submonoid M') : mul_distrib_mul_action S α := mul_distrib_mul_action.comp_hom _ S.subtype example {S : submonoid M'} : is_scalar_tower S M' M' := by apply_instance end submonoid end actions
e7c6a12e9f62360a7c0473f38f1a5e5242d70e13
a4673261e60b025e2c8c825dfa4ab9108246c32e
/stage0/src/Lean/Elab/Attributes.lean
c669cc30cafdfcd0d10f19c8fcc67e6af1144f11
[ "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
1,752
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, Sebastian Ullrich -/ import Lean.Parser.Basic import Lean.Attributes import Lean.MonadEnv namespace Lean.Elab structure Attribute := (name : Name) (args : Syntax := Syntax.missing) instance : ToFormat Attribute := ⟨fun attr => Format.bracket "@[" (toString attr.name ++ (if attr.args.isMissing then "" else toString attr.args)) "]"⟩ instance : Inhabited Attribute := ⟨{ name := arbitrary }⟩ def elabAttr {m} [Monad m] [MonadEnv m] [MonadExceptOf Exception m] [MonadRef m] [AddErrorMessageContext m] (stx : Syntax) : m Attribute := do -- rawIdent >> many attrArg let nameStx := stx[0] let attrName ← match nameStx.isIdOrAtom? with | none => withRef nameStx $ throwError "identifier expected" | some str => pure $ Name.mkSimple str unless isAttribute (← getEnv) attrName do throwError! "unknown attribute [{attrName}]" let mut args := stx[1] -- the old frontend passes Syntax.missing for empty args, for reasons if args.getNumArgs == 0 then args := Syntax.missing pure { name := attrName, args := args } -- sepBy1 attrInstance ", " def elabAttrs {m} [Monad m] [MonadEnv m] [MonadExceptOf Exception m] [MonadRef m] [AddErrorMessageContext m] (stx : Syntax) : m (Array Attribute) := do let mut attrs := #[] for attr in stx.getSepArgs do attrs := attrs.push (← elabAttr attr) return attrs -- parser! "@[" >> sepBy1 attrInstance ", " >> "]" def elabDeclAttrs {m} [Monad m] [MonadEnv m] [MonadExceptOf Exception m] [MonadRef m] [AddErrorMessageContext m] (stx : Syntax) : m (Array Attribute) := elabAttrs stx[1] end Lean.Elab
d8eaf2c89585b1e66c0fef3a9d17812de5d1801e
a45212b1526d532e6e83c44ddca6a05795113ddc
/src/tactic/algebra.lean
43bfde7560c14e856c15f9ab3a008bda41376875
[ "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
1,855
lean
/- Copyright (c) 2018 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon -/ import tactic.basic open lean.parser namespace tactic @[user_attribute] meta def ancestor_attr : user_attribute unit (list name) := { name := `ancestor, descr := "ancestor of old structures", parser := many ident } meta def get_ancestors (cl : name) : tactic (list name) := (++) <$> (prod.fst <$> subobject_names cl <|> pure []) <*> (user_attribute.get_param ancestor_attr cl <|> pure []) meta def find_ancestors : name → expr → tactic (list expr) | cl arg := do cs ← get_ancestors cl, r ← cs.mmap $ λ c, list.ret <$> (mk_app c [arg] >>= mk_instance) <|> find_ancestors c arg, return r.join end tactic attribute [ancestor has_mul] semigroup attribute [ancestor semigroup has_one] monoid attribute [ancestor monoid has_inv] group attribute [ancestor group has_comm] comm_group attribute [ancestor has_add] add_semigroup attribute [ancestor add_semigroup has_zero] add_monoid attribute [ancestor add_monoid has_neg] add_group attribute [ancestor add_group has_add_comm] add_comm_group attribute [ancestor ring has_inv zero_ne_one_class] division_ring attribute [ancestor division_ring comm_ring] field attribute [ancestor field] discrete_field attribute [ancestor has_mul has_add] distrib attribute [ancestor has_mul has_zero] mul_zero_class attribute [ancestor has_zero has_one] zero_ne_one_class attribute [ancestor add_comm_monoid monoid distrib mul_zero_class] semiring attribute [ancestor semiring comm_monoid] comm_semiring attribute [ancestor add_comm_group monoid distrib] ring attribute [ancestor ring comm_semigroup] comm_ring attribute [ancestor has_mul has_zero] no_zero_divisors attribute [ancestor comm_ring no_zero_divisors zero_ne_one_class] integral_domain
100b3b494c96937b80626a605b7834e6b6399842
7cdf3413c097e5d36492d12cdd07030eb991d394
/src/game/world4/level5.lean
c502d72cf5c70878af3b295d9894022ccfd0a90f
[]
no_license
alreadydone/natural_number_game
3135b9385a9f43e74cfbf79513fc37e69b99e0b3
1a39e693df4f4e871eb449890d3c7715a25c2ec9
refs/heads/master
1,599,387,390,105
1,573,200,587,000
1,573,200,691,000
220,397,084
0
0
null
1,573,192,734,000
1,573,192,733,000
null
UTF-8
Lean
false
false
441
lean
import game.world4.level4 -- hide namespace mynat -- hide /- # World 4 : Power World ## Level 5: `pow_add` -/ /- Lemma For all naturals $m$, $a$, $b$, we have $a^{m + n} = a ^ m a ^ n$. -/ lemma pow_add (a m n : mynat) : a ^ (m + n) = a ^ m * a ^ n := begin [less_leaky] induction n with t ht, rw [add_zero, pow_zero, mul_one], refl, rw [add_succ, pow_succ, pow_succ, ht, mul_assoc], refl, end end mynat -- hide
e59e0052af0b6e5eb379bbd654197819396b4875
bb31430994044506fa42fd667e2d556327e18dfe
/src/analysis/inner_product_space/projection.lean
482ec4b838a5b2922cccf8b3c933b5fbcdf55115
[ "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
58,576
lean
/- Copyright (c) 2019 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou, Frédéric Dupuis, Heather Macbeth -/ import analysis.convex.basic import analysis.inner_product_space.symmetric import analysis.normed_space.is_R_or_C /-! # The orthogonal projection Given a nonempty complete subspace `K` of an inner product space `E`, this file constructs `orthogonal_projection K : E →L[𝕜] K`, the orthogonal projection of `E` onto `K`. This map satisfies: for any point `u` in `E`, the point `v = orthogonal_projection K u` in `K` minimizes the distance `‖u - v‖` to `u`. Also a linear isometry equivalence `reflection K : E ≃ₗᵢ[𝕜] E` is constructed, by choosing, for each `u : E`, the point `reflection K u` to satisfy `u + (reflection K u) = 2 • orthogonal_projection K u`. Basic API for `orthogonal_projection` and `reflection` is developed. Next, the orthogonal projection is used to prove a series of more subtle lemmas about the the orthogonal complement of complete subspaces of `E` (the orthogonal complement itself was defined in `analysis.inner_product_space.basic`); the lemma `submodule.sup_orthogonal_of_is_complete`, stating that for a complete subspace `K` of `E` we have `K ⊔ Kᗮ = ⊤`, is a typical example. ## References The orthogonal projection construction is adapted from * [Clément & Martin, *The Lax-Milgram Theorem. A detailed proof to be formalized in Coq*] * [Clément & Martin, *A Coq formal proof of the Lax–Milgram theorem*] The Coq code is available at the following address: <http://www.lri.fr/~sboldo/elfic/index.html> -/ noncomputable theory open is_R_or_C real filter linear_map (ker range) open_locale big_operators topological_space variables {𝕜 E F : Type*} [is_R_or_C 𝕜] variables [inner_product_space 𝕜 E] [inner_product_space ℝ F] local notation `⟪`x`, `y`⟫` := @inner 𝕜 _ _ x y local notation `absR` := has_abs.abs /-! ### Orthogonal projection in inner product spaces -/ /-- Existence of minimizers Let `u` be a point in a real inner product space, and let `K` be a nonempty complete convex subset. Then there exists a (unique) `v` in `K` that minimizes the distance `‖u - v‖` to `u`. -/ -- FIXME this monolithic proof causes a deterministic timeout with `-T50000` -- It should be broken in a sequence of more manageable pieces, -- perhaps with individual statements for the three steps below. theorem exists_norm_eq_infi_of_complete_convex {K : set F} (ne : K.nonempty) (h₁ : is_complete K) (h₂ : convex ℝ K) : ∀ u : F, ∃ v ∈ K, ‖u - v‖ = ⨅ w : K, ‖u - w‖ := assume u, begin let δ := ⨅ w : K, ‖u - w‖, letI : nonempty K := ne.to_subtype, have zero_le_δ : 0 ≤ δ := le_cinfi (λ _, norm_nonneg _), have δ_le : ∀ w : K, δ ≤ ‖u - w‖, from cinfi_le ⟨0, set.forall_range_iff.2 $ λ _, norm_nonneg _⟩, have δ_le' : ∀ w ∈ K, δ ≤ ‖u - w‖ := assume w hw, δ_le ⟨w, hw⟩, -- Step 1: since `δ` is the infimum, can find a sequence `w : ℕ → K` in `K` -- such that `‖u - w n‖ < δ + 1 / (n + 1)` (which implies `‖u - w n‖ --> δ`); -- maybe this should be a separate lemma have exists_seq : ∃ w : ℕ → K, ∀ n, ‖u - w n‖ < δ + 1 / (n + 1), { have hδ : ∀n:ℕ, δ < δ + 1 / (n + 1), from λ n, lt_add_of_le_of_pos le_rfl nat.one_div_pos_of_nat, have h := λ n, exists_lt_of_cinfi_lt (hδ n), let w : ℕ → K := λ n, classical.some (h n), exact ⟨w, λ n, classical.some_spec (h n)⟩ }, rcases exists_seq with ⟨w, hw⟩, have norm_tendsto : tendsto (λ n, ‖u - w n‖) at_top (nhds δ), { have h : tendsto (λ n:ℕ, δ) at_top (nhds δ) := tendsto_const_nhds, have h' : tendsto (λ n:ℕ, δ + 1 / (n + 1)) at_top (nhds δ), { convert h.add tendsto_one_div_add_at_top_nhds_0_nat, simp only [add_zero] }, exact tendsto_of_tendsto_of_tendsto_of_le_of_le h h' (λ x, δ_le _) (λ x, le_of_lt (hw _)) }, -- Step 2: Prove that the sequence `w : ℕ → K` is a Cauchy sequence have seq_is_cauchy : cauchy_seq (λ n, ((w n):F)), { rw cauchy_seq_iff_le_tendsto_0, -- splits into three goals let b := λ n:ℕ, (8 * δ * (1/(n+1)) + 4 * (1/(n+1)) * (1/(n+1))), use (λn, sqrt (b n)), split, -- first goal : `∀ (n : ℕ), 0 ≤ sqrt (b n)` assume n, exact sqrt_nonneg _, split, -- second goal : `∀ (n m N : ℕ), N ≤ n → N ≤ m → dist ↑(w n) ↑(w m) ≤ sqrt (b N)` assume p q N hp hq, let wp := ((w p):F), let wq := ((w q):F), let a := u - wq, let b := u - wp, let half := 1 / (2:ℝ), let div := 1 / ((N:ℝ) + 1), have : 4 * ‖u - half • (wq + wp)‖ * ‖u - half • (wq + wp)‖ + ‖wp - wq‖ * ‖wp - wq‖ = 2 * (‖a‖ * ‖a‖ + ‖b‖ * ‖b‖) := calc 4 * ‖u - half•(wq + wp)‖ * ‖u - half•(wq + wp)‖ + ‖wp - wq‖ * ‖wp - wq‖ = (2*‖u - half•(wq + wp)‖) * (2 * ‖u - half•(wq + wp)‖) + ‖wp-wq‖*‖wp-wq‖ : by ring ... = (absR ((2:ℝ)) * ‖u - half•(wq + wp)‖) * (absR ((2:ℝ)) * ‖u - half•(wq+wp)‖) + ‖wp-wq‖*‖wp-wq‖ : by { rw _root_.abs_of_nonneg, exact zero_le_two } ... = ‖(2:ℝ) • (u - half • (wq + wp))‖ * ‖(2:ℝ) • (u - half • (wq + wp))‖ + ‖wp-wq‖ * ‖wp-wq‖ : by simp [norm_smul] ... = ‖a + b‖ * ‖a + b‖ + ‖a - b‖ * ‖a - b‖ : begin rw [smul_sub, smul_smul, mul_one_div_cancel (_root_.two_ne_zero : (2 : ℝ) ≠ 0), ← one_add_one_eq_two, add_smul], simp only [one_smul], have eq₁ : wp - wq = a - b, from (sub_sub_sub_cancel_left _ _ _).symm, have eq₂ : u + u - (wq + wp) = a + b, show u + u - (wq + wp) = (u - wq) + (u - wp), abel, rw [eq₁, eq₂], end ... = 2 * (‖a‖ * ‖a‖ + ‖b‖ * ‖b‖) : parallelogram_law_with_norm _ _, have eq : δ ≤ ‖u - half • (wq + wp)‖, { rw smul_add, apply δ_le', apply h₂, repeat {exact subtype.mem _}, repeat {exact le_of_lt one_half_pos}, exact add_halves 1 }, have eq₁ : 4 * δ * δ ≤ 4 * ‖u - half • (wq + wp)‖ * ‖u - half • (wq + wp)‖, { simp_rw mul_assoc, exact mul_le_mul_of_nonneg_left (mul_self_le_mul_self zero_le_δ eq) zero_le_four }, have eq₂ : ‖a‖ * ‖a‖ ≤ (δ + div) * (δ + div) := mul_self_le_mul_self (norm_nonneg _) (le_trans (le_of_lt $ hw q) (add_le_add_left (nat.one_div_le_one_div hq) _)), have eq₂' : ‖b‖ * ‖b‖ ≤ (δ + div) * (δ + div) := mul_self_le_mul_self (norm_nonneg _) (le_trans (le_of_lt $ hw p) (add_le_add_left (nat.one_div_le_one_div hp) _)), rw dist_eq_norm, apply nonneg_le_nonneg_of_sq_le_sq, { exact sqrt_nonneg _ }, rw mul_self_sqrt, calc ‖wp - wq‖ * ‖wp - wq‖ = 2 * (‖a‖*‖a‖ + ‖b‖*‖b‖) - 4 * ‖u - half • (wq+wp)‖ * ‖u - half • (wq+wp)‖ : by { rw ← this, simp } ... ≤ 2 * (‖a‖ * ‖a‖ + ‖b‖ * ‖b‖) - 4 * δ * δ : sub_le_sub_left eq₁ _ ... ≤ 2 * ((δ + div) * (δ + div) + (δ + div) * (δ + div)) - 4 * δ * δ : sub_le_sub_right (mul_le_mul_of_nonneg_left (add_le_add eq₂ eq₂') (by norm_num)) _ ... = 8 * δ * div + 4 * div * div : by ring, exact add_nonneg (mul_nonneg (mul_nonneg (by norm_num) zero_le_δ) (le_of_lt nat.one_div_pos_of_nat)) (mul_nonneg (mul_nonneg (by norm_num) nat.one_div_pos_of_nat.le) nat.one_div_pos_of_nat.le), -- third goal : `tendsto (λ (n : ℕ), sqrt (b n)) at_top (𝓝 0)` apply tendsto.comp, { convert continuous_sqrt.continuous_at, exact sqrt_zero.symm }, have eq₁ : tendsto (λ (n : ℕ), 8 * δ * (1 / (n + 1))) at_top (nhds (0:ℝ)), { convert (@tendsto_const_nhds _ _ _ (8 * δ) _).mul tendsto_one_div_add_at_top_nhds_0_nat, simp only [mul_zero] }, have : tendsto (λ (n : ℕ), (4:ℝ) * (1 / (n + 1))) at_top (nhds (0:ℝ)), { convert (@tendsto_const_nhds _ _ _ (4:ℝ) _).mul tendsto_one_div_add_at_top_nhds_0_nat, simp only [mul_zero] }, have eq₂ : tendsto (λ (n : ℕ), (4:ℝ) * (1 / (n + 1)) * (1 / (n + 1))) at_top (nhds (0:ℝ)), { convert this.mul tendsto_one_div_add_at_top_nhds_0_nat, simp only [mul_zero] }, convert eq₁.add eq₂, simp only [add_zero] }, -- Step 3: By completeness of `K`, let `w : ℕ → K` converge to some `v : K`. -- Prove that it satisfies all requirements. rcases cauchy_seq_tendsto_of_is_complete h₁ (λ n, _) seq_is_cauchy with ⟨v, hv, w_tendsto⟩, use v, use hv, have h_cont : continuous (λ v, ‖u - v‖) := continuous.comp continuous_norm (continuous.sub continuous_const continuous_id), have : tendsto (λ n, ‖u - w n‖) at_top (nhds ‖u - v‖), convert (tendsto.comp h_cont.continuous_at w_tendsto), exact tendsto_nhds_unique this norm_tendsto, exact subtype.mem _ end /-- Characterization of minimizers for the projection on a convex set in a real inner product space. -/ theorem norm_eq_infi_iff_real_inner_le_zero {K : set F} (h : convex ℝ K) {u : F} {v : F} (hv : v ∈ K) : ‖u - v‖ = (⨅ w : K, ‖u - w‖) ↔ ∀ w ∈ K, ⟪u - v, w - v⟫_ℝ ≤ 0 := iff.intro begin assume eq w hw, let δ := ⨅ w : K, ‖u - w‖, let p := ⟪u - v, w - v⟫_ℝ, let q := ‖w - v‖^2, letI : nonempty K := ⟨⟨v, hv⟩⟩, have zero_le_δ : 0 ≤ δ, apply le_cinfi, intro, exact norm_nonneg _, have δ_le : ∀ w : K, δ ≤ ‖u - w‖, assume w, apply cinfi_le, use (0:ℝ), rintros _ ⟨_, rfl⟩, exact norm_nonneg _, have δ_le' : ∀ w ∈ K, δ ≤ ‖u - w‖ := assume w hw, δ_le ⟨w, hw⟩, have : ∀θ:ℝ, 0 < θ → θ ≤ 1 → 2 * p ≤ θ * q, assume θ hθ₁ hθ₂, have : ‖u - v‖^2 ≤ ‖u - v‖^2 - 2 * θ * ⟪u - v, w - v⟫_ℝ + θ*θ*‖w - v‖^2 := calc ‖u - v‖^2 ≤ ‖u - (θ•w + (1-θ)•v)‖^2 : begin simp only [sq], apply mul_self_le_mul_self (norm_nonneg _), rw [eq], apply δ_le', apply h hw hv, exacts [le_of_lt hθ₁, sub_nonneg.2 hθ₂, add_sub_cancel'_right _ _], end ... = ‖(u - v) - θ • (w - v)‖^2 : begin have : u - (θ•w + (1-θ)•v) = (u - v) - θ • (w - v), { rw [smul_sub, sub_smul, one_smul], simp only [sub_eq_add_neg, add_comm, add_left_comm, add_assoc, neg_add_rev] }, rw this end ... = ‖u - v‖^2 - 2 * θ * inner (u - v) (w - v) + θ*θ*‖w - v‖^2 : begin rw [norm_sub_sq, inner_smul_right, norm_smul], simp only [sq], show ‖u-v‖*‖u-v‖-2*(θ*inner(u-v)(w-v))+absR (θ)*‖w-v‖*(absR (θ)*‖w-v‖)= ‖u-v‖*‖u-v‖-2*θ*inner(u-v)(w-v)+θ*θ*(‖w-v‖*‖w-v‖), rw abs_of_pos hθ₁, ring end, have eq₁ : ‖u-v‖^2-2*θ*inner(u-v)(w-v)+θ*θ*‖w-v‖^2=‖u-v‖^2+(θ*θ*‖w-v‖^2-2*θ*inner(u-v)(w-v)), by abel, rw [eq₁, le_add_iff_nonneg_right] at this, have eq₂ : θ*θ*‖w-v‖^2-2*θ*inner(u-v)(w-v)=θ*(θ*‖w-v‖^2-2*inner(u-v)(w-v)), ring, rw eq₂ at this, have := le_of_sub_nonneg (nonneg_of_mul_nonneg_right this hθ₁), exact this, by_cases hq : q = 0, { rw hq at this, have : p ≤ 0, have := this (1:ℝ) (by norm_num) (by norm_num), linarith, exact this }, { have q_pos : 0 < q, apply lt_of_le_of_ne, exact sq_nonneg _, intro h, exact hq h.symm, by_contradiction hp, rw not_le at hp, let θ := min (1:ℝ) (p / q), have eq₁ : θ*q ≤ p := calc θ*q ≤ (p/q) * q : mul_le_mul_of_nonneg_right (min_le_right _ _) (sq_nonneg _) ... = p : div_mul_cancel _ hq, have : 2 * p ≤ p := calc 2 * p ≤ θ*q : by { refine this θ (lt_min (by norm_num) (div_pos hp q_pos)) (by norm_num) } ... ≤ p : eq₁, linarith } end begin assume h, letI : nonempty K := ⟨⟨v, hv⟩⟩, apply le_antisymm, { apply le_cinfi, assume w, apply nonneg_le_nonneg_of_sq_le_sq (norm_nonneg _), have := h w w.2, calc ‖u - v‖ * ‖u - v‖ ≤ ‖u - v‖ * ‖u - v‖ - 2 * inner (u - v) ((w:F) - v) : by linarith ... ≤ ‖u - v‖^2 - 2 * inner (u - v) ((w:F) - v) + ‖(w:F) - v‖^2 : by { rw sq, refine le_add_of_nonneg_right _, exact sq_nonneg _ } ... = ‖(u - v) - (w - v)‖^2 : norm_sub_sq.symm ... = ‖u - w‖ * ‖u - w‖ : by { have : (u - v) - (w - v) = u - w, abel, rw [this, sq] } }, { show (⨅ (w : K), ‖u - w‖) ≤ (λw:K, ‖u - w‖) ⟨v, hv⟩, apply cinfi_le, use 0, rintros y ⟨z, rfl⟩, exact norm_nonneg _ } end variables (K : submodule 𝕜 E) /-- Existence of projections on complete subspaces. Let `u` be a point in an inner product space, and let `K` be a nonempty complete subspace. Then there exists a (unique) `v` in `K` that minimizes the distance `‖u - v‖` to `u`. This point `v` is usually called the orthogonal projection of `u` onto `K`. -/ theorem exists_norm_eq_infi_of_complete_subspace (h : is_complete (↑K : set E)) : ∀ u : E, ∃ v ∈ K, ‖u - v‖ = ⨅ w : (K : set E), ‖u - w‖ := begin letI : inner_product_space ℝ E := inner_product_space.is_R_or_C_to_real 𝕜 E, letI : module ℝ E := restrict_scalars.module ℝ 𝕜 E, let K' : submodule ℝ E := submodule.restrict_scalars ℝ K, exact exists_norm_eq_infi_of_complete_convex ⟨0, K'.zero_mem⟩ h K'.convex end /-- Characterization of minimizers in the projection on a subspace, in the real case. Let `u` be a point in a real inner product space, and let `K` be a nonempty subspace. Then point `v` minimizes the distance `‖u - v‖` over points in `K` if and only if for all `w ∈ K`, `⟪u - v, w⟫ = 0` (i.e., `u - v` is orthogonal to the subspace `K`). This is superceded by `norm_eq_infi_iff_inner_eq_zero` that gives the same conclusion over any `is_R_or_C` field. -/ theorem norm_eq_infi_iff_real_inner_eq_zero (K : submodule ℝ F) {u : F} {v : F} (hv : v ∈ K) : ‖u - v‖ = (⨅ w : (↑K : set F), ‖u - w‖) ↔ ∀ w ∈ K, ⟪u - v, w⟫_ℝ = 0 := iff.intro begin assume h, have h : ∀ w ∈ K, ⟪u - v, w - v⟫_ℝ ≤ 0, { rwa [norm_eq_infi_iff_real_inner_le_zero] at h, exacts [K.convex, hv] }, assume w hw, have le : ⟪u - v, w⟫_ℝ ≤ 0, let w' := w + v, have : w' ∈ K := submodule.add_mem _ hw hv, have h₁ := h w' this, have h₂ : w' - v = w, simp only [add_neg_cancel_right, sub_eq_add_neg], rw h₂ at h₁, exact h₁, have ge : ⟪u - v, w⟫_ℝ ≥ 0, let w'' := -w + v, have : w'' ∈ K := submodule.add_mem _ (submodule.neg_mem _ hw) hv, have h₁ := h w'' this, have h₂ : w'' - v = -w, simp only [neg_inj, add_neg_cancel_right, sub_eq_add_neg], rw [h₂, inner_neg_right] at h₁, linarith, exact le_antisymm le ge end begin assume h, have : ∀ w ∈ K, ⟪u - v, w - v⟫_ℝ ≤ 0, assume w hw, let w' := w - v, have : w' ∈ K := submodule.sub_mem _ hw hv, have h₁ := h w' this, exact le_of_eq h₁, rwa norm_eq_infi_iff_real_inner_le_zero, exacts [submodule.convex _, hv] end /-- Characterization of minimizers in the projection on a subspace. Let `u` be a point in an inner product space, and let `K` be a nonempty subspace. Then point `v` minimizes the distance `‖u - v‖` over points in `K` if and only if for all `w ∈ K`, `⟪u - v, w⟫ = 0` (i.e., `u - v` is orthogonal to the subspace `K`) -/ theorem norm_eq_infi_iff_inner_eq_zero {u : E} {v : E} (hv : v ∈ K) : ‖u - v‖ = (⨅ w : K, ‖u - w‖) ↔ ∀ w ∈ K, ⟪u - v, w⟫ = 0 := begin letI : inner_product_space ℝ E := inner_product_space.is_R_or_C_to_real 𝕜 E, letI : module ℝ E := restrict_scalars.module ℝ 𝕜 E, let K' : submodule ℝ E := K.restrict_scalars ℝ, split, { assume H, have A : ∀ w ∈ K, re ⟪u - v, w⟫ = 0 := (norm_eq_infi_iff_real_inner_eq_zero K' hv).1 H, assume w hw, apply ext, { simp [A w hw] }, { symmetry, calc im (0 : 𝕜) = 0 : im.map_zero ... = re ⟪u - v, (-I) • w⟫ : (A _ (K.smul_mem (-I) hw)).symm ... = re ((-I) * ⟪u - v, w⟫) : by rw inner_smul_right ... = im ⟪u - v, w⟫ : by simp } }, { assume H, have : ∀ w ∈ K', ⟪u - v, w⟫_ℝ = 0, { assume w hw, rw [real_inner_eq_re_inner, H w hw], exact zero_re' }, exact (norm_eq_infi_iff_real_inner_eq_zero K' hv).2 this } end section orthogonal_projection variables [complete_space K] /-- The orthogonal projection onto a complete subspace, as an unbundled function. This definition is only intended for use in setting up the bundled version `orthogonal_projection` and should not be used once that is defined. -/ def orthogonal_projection_fn (v : E) := (exists_norm_eq_infi_of_complete_subspace K (complete_space_coe_iff_is_complete.mp ‹_›) v).some variables {K} /-- The unbundled orthogonal projection is in the given subspace. This lemma is only intended for use in setting up the bundled version and should not be used once that is defined. -/ lemma orthogonal_projection_fn_mem (v : E) : orthogonal_projection_fn K v ∈ K := (exists_norm_eq_infi_of_complete_subspace K (complete_space_coe_iff_is_complete.mp ‹_›) v).some_spec.some /-- The characterization of the unbundled orthogonal projection. This lemma is only intended for use in setting up the bundled version and should not be used once that is defined. -/ lemma orthogonal_projection_fn_inner_eq_zero (v : E) : ∀ w ∈ K, ⟪v - orthogonal_projection_fn K v, w⟫ = 0 := begin rw ←norm_eq_infi_iff_inner_eq_zero K (orthogonal_projection_fn_mem v), exact (exists_norm_eq_infi_of_complete_subspace K (complete_space_coe_iff_is_complete.mp ‹_›) v).some_spec.some_spec end /-- The unbundled orthogonal projection is the unique point in `K` with the orthogonality property. This lemma is only intended for use in setting up the bundled version and should not be used once that is defined. -/ lemma eq_orthogonal_projection_fn_of_mem_of_inner_eq_zero {u v : E} (hvm : v ∈ K) (hvo : ∀ w ∈ K, ⟪u - v, w⟫ = 0) : orthogonal_projection_fn K u = v := begin rw [←sub_eq_zero, ←inner_self_eq_zero], have hvs : orthogonal_projection_fn K u - v ∈ K := submodule.sub_mem K (orthogonal_projection_fn_mem u) hvm, have huo : ⟪u - orthogonal_projection_fn K u, orthogonal_projection_fn K u - v⟫ = 0 := orthogonal_projection_fn_inner_eq_zero u _ hvs, have huv : ⟪u - v, orthogonal_projection_fn K u - v⟫ = 0 := hvo _ hvs, have houv : ⟪(u - v) - (u - orthogonal_projection_fn K u), orthogonal_projection_fn K u - v⟫ = 0, { rw [inner_sub_left, huo, huv, sub_zero] }, rwa sub_sub_sub_cancel_left at houv end variables (K) lemma orthogonal_projection_fn_norm_sq (v : E) : ‖v‖ * ‖v‖ = ‖v - (orthogonal_projection_fn K v)‖ * ‖v - (orthogonal_projection_fn K v)‖ + ‖orthogonal_projection_fn K v‖ * ‖orthogonal_projection_fn K v‖ := begin set p := orthogonal_projection_fn K v, have h' : ⟪v - p, p⟫ = 0, { exact orthogonal_projection_fn_inner_eq_zero _ _ (orthogonal_projection_fn_mem v) }, convert norm_add_sq_eq_norm_sq_add_norm_sq_of_inner_eq_zero (v - p) p h' using 2; simp, end /-- The orthogonal projection onto a complete subspace. -/ def orthogonal_projection : E →L[𝕜] K := linear_map.mk_continuous { to_fun := λ v, ⟨orthogonal_projection_fn K v, orthogonal_projection_fn_mem v⟩, map_add' := λ x y, begin have hm : orthogonal_projection_fn K x + orthogonal_projection_fn K y ∈ K := submodule.add_mem K (orthogonal_projection_fn_mem x) (orthogonal_projection_fn_mem y), have ho : ∀ w ∈ K, ⟪x + y - (orthogonal_projection_fn K x + orthogonal_projection_fn K y), w⟫ = 0, { intros w hw, rw [add_sub_add_comm, inner_add_left, orthogonal_projection_fn_inner_eq_zero _ w hw, orthogonal_projection_fn_inner_eq_zero _ w hw, add_zero] }, ext, simp [eq_orthogonal_projection_fn_of_mem_of_inner_eq_zero hm ho] end, map_smul' := λ c x, begin have hm : c • orthogonal_projection_fn K x ∈ K := submodule.smul_mem K _ (orthogonal_projection_fn_mem x), have ho : ∀ w ∈ K, ⟪c • x - c • orthogonal_projection_fn K x, w⟫ = 0, { intros w hw, rw [←smul_sub, inner_smul_left, orthogonal_projection_fn_inner_eq_zero _ w hw, mul_zero] }, ext, simp [eq_orthogonal_projection_fn_of_mem_of_inner_eq_zero hm ho] end } 1 (λ x, begin simp only [one_mul, linear_map.coe_mk], refine le_of_pow_le_pow 2 (norm_nonneg _) (by norm_num) _, change ‖orthogonal_projection_fn K x‖ ^ 2 ≤ ‖x‖ ^ 2, nlinarith [orthogonal_projection_fn_norm_sq K x] end) variables {K} @[simp] lemma orthogonal_projection_fn_eq (v : E) : orthogonal_projection_fn K v = (orthogonal_projection K v : E) := rfl /-- The characterization of the orthogonal projection. -/ @[simp] lemma orthogonal_projection_inner_eq_zero (v : E) : ∀ w ∈ K, ⟪v - orthogonal_projection K v, w⟫ = 0 := orthogonal_projection_fn_inner_eq_zero v /-- The difference of `v` from its orthogonal projection onto `K` is in `Kᗮ`. -/ @[simp] lemma sub_orthogonal_projection_mem_orthogonal (v : E) : v - orthogonal_projection K v ∈ Kᗮ := begin intros w hw, rw inner_eq_zero_sym, exact orthogonal_projection_inner_eq_zero _ _ hw end /-- The orthogonal projection is the unique point in `K` with the orthogonality property. -/ lemma eq_orthogonal_projection_of_mem_of_inner_eq_zero {u v : E} (hvm : v ∈ K) (hvo : ∀ w ∈ K, ⟪u - v, w⟫ = 0) : (orthogonal_projection K u : E) = v := eq_orthogonal_projection_fn_of_mem_of_inner_eq_zero hvm hvo /-- The orthogonal projection of `y` on `U` minimizes the distance `‖y - x‖` for `x ∈ U`. -/ lemma orthogonal_projection_minimal {U : submodule 𝕜 E} [complete_space U] (y : E) : ‖y - orthogonal_projection U y‖ = ⨅ x : U, ‖y - x‖ := begin rw norm_eq_infi_iff_inner_eq_zero _ (submodule.coe_mem _), exact orthogonal_projection_inner_eq_zero _ end /-- The orthogonal projections onto equal subspaces are coerced back to the same point in `E`. -/ lemma eq_orthogonal_projection_of_eq_submodule {K' : submodule 𝕜 E} [complete_space K'] (h : K = K') (u : E) : (orthogonal_projection K u : E) = (orthogonal_projection K' u : E) := begin change orthogonal_projection_fn K u = orthogonal_projection_fn K' u, congr, exact h end /-- The orthogonal projection sends elements of `K` to themselves. -/ @[simp] lemma orthogonal_projection_mem_subspace_eq_self (v : K) : orthogonal_projection K v = v := by { ext, apply eq_orthogonal_projection_of_mem_of_inner_eq_zero; simp } /-- A point equals its orthogonal projection if and only if it lies in the subspace. -/ lemma orthogonal_projection_eq_self_iff {v : E} : (orthogonal_projection K v : E) = v ↔ v ∈ K := begin refine ⟨λ h, _, λ h, eq_orthogonal_projection_of_mem_of_inner_eq_zero h _⟩, { rw ← h, simp }, { simp } end lemma linear_isometry.map_orthogonal_projection {E E' : Type*} [inner_product_space 𝕜 E] [inner_product_space 𝕜 E'] (f : E →ₗᵢ[𝕜] E') (p : submodule 𝕜 E) [complete_space p] (x : E) : f (orthogonal_projection p x) = orthogonal_projection (p.map f.to_linear_map) (f x) := begin refine (eq_orthogonal_projection_of_mem_of_inner_eq_zero _ $ λ y hy, _).symm, refine submodule.apply_coe_mem_map _ _, rcases hy with ⟨x', hx', rfl : f x' = y⟩, rw [← f.map_sub, f.inner_map_map, orthogonal_projection_inner_eq_zero x x' hx'] end lemma linear_isometry.map_orthogonal_projection' {E E' : Type*} [inner_product_space 𝕜 E] [inner_product_space 𝕜 E'] (f : E →ₗᵢ[𝕜] E') (p : submodule 𝕜 E) [complete_space p] (x : E) : f (orthogonal_projection p x) = orthogonal_projection (p.map f) (f x) := begin refine (eq_orthogonal_projection_of_mem_of_inner_eq_zero _ $ λ y hy, _).symm, refine submodule.apply_coe_mem_map _ _, rcases hy with ⟨x', hx', rfl : f x' = y⟩, rw [← f.map_sub, f.inner_map_map, orthogonal_projection_inner_eq_zero x x' hx'] end /-- Orthogonal projection onto the `submodule.map` of a subspace. -/ lemma orthogonal_projection_map_apply {E E' : Type*} [inner_product_space 𝕜 E] [inner_product_space 𝕜 E'] (f : E ≃ₗᵢ[𝕜] E') (p : submodule 𝕜 E) [complete_space p] (x : E') : (orthogonal_projection (p.map (f.to_linear_equiv : E →ₗ[𝕜] E')) x : E') = f (orthogonal_projection p (f.symm x)) := by simpa only [f.coe_to_linear_isometry, f.apply_symm_apply] using (f.to_linear_isometry.map_orthogonal_projection p (f.symm x)).symm /-- The orthogonal projection onto the trivial submodule is the zero map. -/ @[simp] lemma orthogonal_projection_bot : orthogonal_projection (⊥ : submodule 𝕜 E) = 0 := by ext variables (K) /-- The orthogonal projection has norm `≤ 1`. -/ lemma orthogonal_projection_norm_le : ‖orthogonal_projection K‖ ≤ 1 := linear_map.mk_continuous_norm_le _ (by norm_num) _ variables (𝕜) lemma smul_orthogonal_projection_singleton {v : E} (w : E) : (‖v‖ ^ 2 : 𝕜) • (orthogonal_projection (𝕜 ∙ v) w : E) = ⟪v, w⟫ • v := begin suffices : ↑(orthogonal_projection (𝕜 ∙ v) ((‖v‖ ^ 2 : 𝕜) • w)) = ⟪v, w⟫ • v, { simpa using this }, apply eq_orthogonal_projection_of_mem_of_inner_eq_zero, { rw submodule.mem_span_singleton, use ⟪v, w⟫ }, { intros x hx, obtain ⟨c, rfl⟩ := submodule.mem_span_singleton.mp hx, have hv : ↑‖v‖ ^ 2 = ⟪v, v⟫ := by { norm_cast, simp [norm_sq_eq_inner] }, simp [inner_sub_left, inner_smul_left, inner_smul_right, map_div₀, mul_comm, hv, inner_product_space.conj_sym, hv] } end /-- Formula for orthogonal projection onto a single vector. -/ lemma orthogonal_projection_singleton {v : E} (w : E) : (orthogonal_projection (𝕜 ∙ v) w : E) = (⟪v, w⟫ / ‖v‖ ^ 2) • v := begin by_cases hv : v = 0, { rw [hv, eq_orthogonal_projection_of_eq_submodule (submodule.span_zero_singleton 𝕜)], { simp }, { apply_instance } }, have hv' : ‖v‖ ≠ 0 := ne_of_gt (norm_pos_iff.mpr hv), have key : ((‖v‖ ^ 2 : 𝕜)⁻¹ * ‖v‖ ^ 2) • ↑(orthogonal_projection (𝕜 ∙ v) w) = ((‖v‖ ^ 2 : 𝕜)⁻¹ * ⟪v, w⟫) • v, { simp [mul_smul, smul_orthogonal_projection_singleton 𝕜 w] }, convert key; field_simp [hv'] end /-- Formula for orthogonal projection onto a single unit vector. -/ lemma orthogonal_projection_unit_singleton {v : E} (hv : ‖v‖ = 1) (w : E) : (orthogonal_projection (𝕜 ∙ v) w : E) = ⟪v, w⟫ • v := by { rw ← smul_orthogonal_projection_singleton 𝕜 w, simp [hv] } end orthogonal_projection section reflection variables {𝕜} (K) [complete_space K] /-- Auxiliary definition for `reflection`: the reflection as a linear equivalence. -/ def reflection_linear_equiv : E ≃ₗ[𝕜] E := linear_equiv.of_involutive (bit0 (K.subtype.comp (orthogonal_projection K).to_linear_map) - linear_map.id) (λ x, by simp [bit0]) /-- Reflection in a complete subspace of an inner product space. The word "reflection" is sometimes understood to mean specifically reflection in a codimension-one subspace, and sometimes more generally to cover operations such as reflection in a point. The definition here, of reflection in a subspace, is a more general sense of the word that includes both those common cases. -/ def reflection : E ≃ₗᵢ[𝕜] E := { norm_map' := begin intros x, let w : K := orthogonal_projection K x, let v := x - w, have : ⟪v, w⟫ = 0 := orthogonal_projection_inner_eq_zero x w w.2, convert norm_sub_eq_norm_add this using 2, { rw [linear_equiv.coe_mk, reflection_linear_equiv, linear_equiv.to_fun_eq_coe, linear_equiv.coe_of_involutive, linear_map.sub_apply, linear_map.id_apply, bit0, linear_map.add_apply, linear_map.comp_apply, submodule.subtype_apply, continuous_linear_map.to_linear_map_eq_coe, continuous_linear_map.coe_coe], dsimp [w, v], abel, }, { simp only [add_sub_cancel'_right, eq_self_iff_true], } end, ..reflection_linear_equiv K } variables {K} /-- The result of reflecting. -/ lemma reflection_apply (p : E) : reflection K p = bit0 ↑(orthogonal_projection K p) - p := rfl /-- Reflection is its own inverse. -/ @[simp] lemma reflection_symm : (reflection K).symm = reflection K := rfl /-- Reflection is its own inverse. -/ @[simp] lemma reflection_inv : (reflection K)⁻¹ = reflection K := rfl variables (K) /-- Reflecting twice in the same subspace. -/ @[simp] lemma reflection_reflection (p : E) : reflection K (reflection K p) = p := (reflection K).left_inv p /-- Reflection is involutive. -/ lemma reflection_involutive : function.involutive (reflection K) := reflection_reflection K /-- Reflection is involutive. -/ @[simp] lemma reflection_trans_reflection : (reflection K).trans (reflection K) = linear_isometry_equiv.refl 𝕜 E := linear_isometry_equiv.ext $ reflection_involutive K /-- Reflection is involutive. -/ @[simp] lemma reflection_mul_reflection : reflection K * reflection K = 1 := reflection_trans_reflection _ variables {K} /-- A point is its own reflection if and only if it is in the subspace. -/ lemma reflection_eq_self_iff (x : E) : reflection K x = x ↔ x ∈ K := begin rw [←orthogonal_projection_eq_self_iff, reflection_apply, sub_eq_iff_eq_add', ← two_smul 𝕜, ← two_smul' 𝕜], refine (smul_right_injective E _).eq_iff, exact two_ne_zero end lemma reflection_mem_subspace_eq_self {x : E} (hx : x ∈ K) : reflection K x = x := (reflection_eq_self_iff x).mpr hx /-- Reflection in the `submodule.map` of a subspace. -/ lemma reflection_map_apply {E E' : Type*} [inner_product_space 𝕜 E] [inner_product_space 𝕜 E'] (f : E ≃ₗᵢ[𝕜] E') (K : submodule 𝕜 E) [complete_space K] (x : E') : reflection (K.map (f.to_linear_equiv : E →ₗ[𝕜] E')) x = f (reflection K (f.symm x)) := by simp [bit0, reflection_apply, orthogonal_projection_map_apply f K x] /-- Reflection in the `submodule.map` of a subspace. -/ lemma reflection_map {E E' : Type*} [inner_product_space 𝕜 E] [inner_product_space 𝕜 E'] (f : E ≃ₗᵢ[𝕜] E') (K : submodule 𝕜 E) [complete_space K] : reflection (K.map (f.to_linear_equiv : E →ₗ[𝕜] E')) = f.symm.trans ((reflection K).trans f) := linear_isometry_equiv.ext $ reflection_map_apply f K /-- Reflection through the trivial subspace {0} is just negation. -/ @[simp] lemma reflection_bot : reflection (⊥ : submodule 𝕜 E) = linear_isometry_equiv.neg 𝕜 := by ext; simp [reflection_apply] end reflection section orthogonal /-- If `K₁` is complete and contained in `K₂`, `K₁` and `K₁ᗮ ⊓ K₂` span `K₂`. -/ lemma submodule.sup_orthogonal_inf_of_complete_space {K₁ K₂ : submodule 𝕜 E} (h : K₁ ≤ K₂) [complete_space K₁] : K₁ ⊔ (K₁ᗮ ⊓ K₂) = K₂ := begin ext x, rw submodule.mem_sup, let v : K₁ := orthogonal_projection K₁ x, have hvm : x - v ∈ K₁ᗮ := sub_orthogonal_projection_mem_orthogonal x, split, { rintro ⟨y, hy, z, hz, rfl⟩, exact K₂.add_mem (h hy) hz.2 }, { exact λ hx, ⟨v, v.prop, x - v, ⟨hvm, K₂.sub_mem hx (h v.prop)⟩, add_sub_cancel'_right _ _⟩ } end variables {K} /-- If `K` is complete, `K` and `Kᗮ` span the whole space. -/ lemma submodule.sup_orthogonal_of_complete_space [complete_space K] : K ⊔ Kᗮ = ⊤ := begin convert submodule.sup_orthogonal_inf_of_complete_space (le_top : K ≤ ⊤), simp end variables (K) /-- If `K` is complete, any `v` in `E` can be expressed as a sum of elements of `K` and `Kᗮ`. -/ lemma submodule.exists_sum_mem_mem_orthogonal [complete_space K] (v : E) : ∃ (y ∈ K) (z ∈ Kᗮ), v = y + z := begin have h_mem : v ∈ K ⊔ Kᗮ := by simp [submodule.sup_orthogonal_of_complete_space], obtain ⟨y, hy, z, hz, hyz⟩ := submodule.mem_sup.mp h_mem, exact ⟨y, hy, z, hz, hyz.symm⟩ end /-- If `K` is complete, then the orthogonal complement of its orthogonal complement is itself. -/ @[simp] lemma submodule.orthogonal_orthogonal [complete_space K] : Kᗮᗮ = K := begin ext v, split, { obtain ⟨y, hy, z, hz, rfl⟩ := K.exists_sum_mem_mem_orthogonal v, intros hv, have hz' : z = 0, { have hyz : ⟪z, y⟫ = 0 := by simp [hz y hy, inner_eq_zero_sym], simpa [inner_add_right, hyz] using hv z hz }, simp [hy, hz'] }, { intros hv w hw, rw inner_eq_zero_sym, exact hw v hv } end lemma submodule.orthogonal_orthogonal_eq_closure [complete_space E] : Kᗮᗮ = K.topological_closure := begin refine le_antisymm _ _, { convert submodule.orthogonal_orthogonal_monotone K.le_topological_closure, haveI : complete_space K.topological_closure := K.is_closed_topological_closure.complete_space_coe, rw K.topological_closure.orthogonal_orthogonal }, { exact K.topological_closure_minimal K.le_orthogonal_orthogonal Kᗮ.is_closed_orthogonal } end variables {K} /-- If `K` is complete, `K` and `Kᗮ` are complements of each other. -/ lemma submodule.is_compl_orthogonal_of_complete_space [complete_space K] : is_compl K Kᗮ := ⟨K.orthogonal_disjoint, codisjoint_iff.2 submodule.sup_orthogonal_of_complete_space⟩ @[simp] lemma submodule.orthogonal_eq_bot_iff [complete_space (K : set E)] : Kᗮ = ⊥ ↔ K = ⊤ := begin refine ⟨_, λ h, by rw [h, submodule.top_orthogonal_eq_bot] ⟩, intro h, have : K ⊔ Kᗮ = ⊤ := submodule.sup_orthogonal_of_complete_space, rwa [h, sup_comm, bot_sup_eq] at this, end /-- A point in `K` with the orthogonality property (here characterized in terms of `Kᗮ`) must be the orthogonal projection. -/ lemma eq_orthogonal_projection_of_mem_orthogonal [complete_space K] {u v : E} (hv : v ∈ K) (hvo : u - v ∈ Kᗮ) : (orthogonal_projection K u : E) = v := eq_orthogonal_projection_fn_of_mem_of_inner_eq_zero hv (λ w, inner_eq_zero_sym.mp ∘ (hvo w)) /-- A point in `K` with the orthogonality property (here characterized in terms of `Kᗮ`) must be the orthogonal projection. -/ lemma eq_orthogonal_projection_of_mem_orthogonal' [complete_space K] {u v z : E} (hv : v ∈ K) (hz : z ∈ Kᗮ) (hu : u = v + z) : (orthogonal_projection K u : E) = v := eq_orthogonal_projection_of_mem_orthogonal hv (by simpa [hu]) /-- The orthogonal projection onto `K` of an element of `Kᗮ` is zero. -/ lemma orthogonal_projection_mem_subspace_orthogonal_complement_eq_zero [complete_space K] {v : E} (hv : v ∈ Kᗮ) : orthogonal_projection K v = 0 := by { ext, convert eq_orthogonal_projection_of_mem_orthogonal _ _; simp [hv] } /-- The reflection in `K` of an element of `Kᗮ` is its negation. -/ lemma reflection_mem_subspace_orthogonal_complement_eq_neg [complete_space K] {v : E} (hv : v ∈ Kᗮ) : reflection K v = - v := by simp [reflection_apply, orthogonal_projection_mem_subspace_orthogonal_complement_eq_zero hv] /-- The orthogonal projection onto `Kᗮ` of an element of `K` is zero. -/ lemma orthogonal_projection_mem_subspace_orthogonal_precomplement_eq_zero [complete_space E] {v : E} (hv : v ∈ K) : orthogonal_projection Kᗮ v = 0 := orthogonal_projection_mem_subspace_orthogonal_complement_eq_zero (K.le_orthogonal_orthogonal hv) /-- If `U ≤ V`, then projecting on `V` and then on `U` is the same as projecting on `U`. -/ lemma orthogonal_projection_orthogonal_projection_of_le {U V : submodule 𝕜 E} [complete_space U] [complete_space V] (h : U ≤ V) (x : E) : orthogonal_projection U (orthogonal_projection V x) = orthogonal_projection U x := eq.symm $ by simpa only [sub_eq_zero, map_sub] using orthogonal_projection_mem_subspace_orthogonal_complement_eq_zero (submodule.orthogonal_le h (sub_orthogonal_projection_mem_orthogonal x)) /-- Given a monotone family `U` of complete submodules of `E` and a fixed `x : E`, the orthogonal projection of `x` on `U i` tends to the orthogonal projection of `x` on `(⨆ i, U i).topological_closure` along `at_top`. -/ lemma orthogonal_projection_tendsto_closure_supr [complete_space E] {ι : Type*} [semilattice_sup ι] (U : ι → submodule 𝕜 E) [∀ i, complete_space (U i)] (hU : monotone U) (x : E) : filter.tendsto (λ i, (orthogonal_projection (U i) x : E)) at_top (𝓝 (orthogonal_projection (⨆ i, U i).topological_closure x : E)) := begin casesI is_empty_or_nonempty ι, { rw filter_eq_bot_of_is_empty (at_top : filter ι), exact tendsto_bot }, let y := (orthogonal_projection (⨆ i, U i).topological_closure x : E), have proj_x : ∀ i, orthogonal_projection (U i) x = orthogonal_projection (U i) y := λ i, (orthogonal_projection_orthogonal_projection_of_le ((le_supr U i).trans (supr U).le_topological_closure) _).symm, suffices : ∀ ε > 0, ∃ I, ∀ i ≥ I, ‖(orthogonal_projection (U i) y : E) - y‖ < ε, { simpa only [proj_x, normed_add_comm_group.tendsto_at_top] using this }, intros ε hε, obtain ⟨a, ha, hay⟩ : ∃ a ∈ ⨆ i, U i, dist y a < ε, { have y_mem : y ∈ (⨆ i, U i).topological_closure := submodule.coe_mem _, rw [← set_like.mem_coe, submodule.topological_closure_coe, metric.mem_closure_iff] at y_mem, exact y_mem ε hε }, rw dist_eq_norm at hay, obtain ⟨I, hI⟩ : ∃ I, a ∈ U I, { rwa [submodule.mem_supr_of_directed _ (hU.directed_le)] at ha }, refine ⟨I, λ i (hi : I ≤ i), _⟩, rw [norm_sub_rev, orthogonal_projection_minimal], refine lt_of_le_of_lt _ hay, change _ ≤ ‖y - (⟨a, hU hi hI⟩ : U i)‖, exact cinfi_le ⟨0, set.forall_range_iff.mpr $ λ _, norm_nonneg _⟩ _, end /-- Given a monotone family `U` of complete submodules of `E` with dense span supremum, and a fixed `x : E`, the orthogonal projection of `x` on `U i` tends to `x` along `at_top`. -/ lemma orthogonal_projection_tendsto_self [complete_space E] {ι : Type*} [semilattice_sup ι] (U : ι → submodule 𝕜 E) [∀ t, complete_space (U t)] (hU : monotone U) (x : E) (hU' : ⊤ ≤ (⨆ t, U t).topological_closure) : filter.tendsto (λ t, (orthogonal_projection (U t) x : E)) at_top (𝓝 x) := begin rw ← eq_top_iff at hU', convert orthogonal_projection_tendsto_closure_supr U hU x, rw orthogonal_projection_eq_self_iff.mpr _, rw hU', trivial end /-- The orthogonal complement satisfies `Kᗮᗮᗮ = Kᗮ`. -/ lemma submodule.triorthogonal_eq_orthogonal [complete_space E] : Kᗮᗮᗮ = Kᗮ := begin rw Kᗮ.orthogonal_orthogonal_eq_closure, exact K.is_closed_orthogonal.submodule_topological_closure_eq, end /-- The closure of `K` is the full space iff `Kᗮ` is trivial. -/ lemma submodule.topological_closure_eq_top_iff [complete_space E] : K.topological_closure = ⊤ ↔ Kᗮ = ⊥ := begin rw ←submodule.orthogonal_orthogonal_eq_closure, split; intro h, { rw [←submodule.triorthogonal_eq_orthogonal, h, submodule.top_orthogonal_eq_bot] }, { rw [h, submodule.bot_orthogonal_eq_top] } end namespace dense open submodule variables {x y : E} [complete_space E] /-- If `S` is dense and `x - y ∈ Kᗮ`, then `x = y`. -/ lemma eq_of_sub_mem_orthogonal (hK : dense (K : set E)) (h : x - y ∈ Kᗮ) : x = y := begin rw [dense_iff_topological_closure_eq_top, topological_closure_eq_top_iff] at hK, rwa [hK, submodule.mem_bot, sub_eq_zero] at h, end lemma eq_zero_of_mem_orthogonal (hK : dense (K : set E)) (h : x ∈ Kᗮ) : x = 0 := hK.eq_of_sub_mem_orthogonal (by rwa [sub_zero]) lemma eq_of_inner_left (hK : dense (K : set E)) (h : ∀ v : K, ⟪x, v⟫ = ⟪y, v⟫) : x = y := hK.eq_of_sub_mem_orthogonal (submodule.sub_mem_orthogonal_of_inner_left h) lemma eq_zero_of_inner_left (hK : dense (K : set E)) (h : ∀ v : K, ⟪x, v⟫ = 0) : x = 0 := hK.eq_of_inner_left (λ v, by rw [inner_zero_left, h v]) lemma eq_of_inner_right (hK : dense (K : set E)) (h : ∀ v : K, ⟪(v : E), x⟫ = ⟪(v : E), y⟫) : x = y := hK.eq_of_sub_mem_orthogonal (submodule.sub_mem_orthogonal_of_inner_right h) lemma eq_zero_of_inner_right (hK : dense (K : set E)) (h : ∀ v : K, ⟪(v : E), x⟫ = 0) : x = 0 := hK.eq_of_inner_right (λ v, by rw [inner_zero_right, h v]) end dense /-- The reflection in `Kᗮ` of an element of `K` is its negation. -/ lemma reflection_mem_subspace_orthogonal_precomplement_eq_neg [complete_space E] {v : E} (hv : v ∈ K) : reflection Kᗮ v = -v := reflection_mem_subspace_orthogonal_complement_eq_neg (K.le_orthogonal_orthogonal hv) /-- The orthogonal projection onto `(𝕜 ∙ v)ᗮ` of `v` is zero. -/ lemma orthogonal_projection_orthogonal_complement_singleton_eq_zero [complete_space E] (v : E) : orthogonal_projection (𝕜 ∙ v)ᗮ v = 0 := orthogonal_projection_mem_subspace_orthogonal_precomplement_eq_zero (submodule.mem_span_singleton_self v) /-- The reflection in `(𝕜 ∙ v)ᗮ` of `v` is `-v`. -/ lemma reflection_orthogonal_complement_singleton_eq_neg [complete_space E] (v : E) : reflection (𝕜 ∙ v)ᗮ v = -v := reflection_mem_subspace_orthogonal_precomplement_eq_neg (submodule.mem_span_singleton_self v) lemma reflection_sub [complete_space F] {v w : F} (h : ‖v‖ = ‖w‖) : reflection (ℝ ∙ (v - w))ᗮ v = w := begin set R : F ≃ₗᵢ[ℝ] F := reflection (ℝ ∙ (v - w))ᗮ, suffices : R v + R v = w + w, { apply smul_right_injective F (by norm_num : (2:ℝ) ≠ 0), simpa [two_smul] using this }, have h₁ : R (v - w) = -(v - w) := reflection_orthogonal_complement_singleton_eq_neg (v - w), have h₂ : R (v + w) = v + w, { apply reflection_mem_subspace_eq_self, rw submodule.mem_orthogonal_singleton_iff_inner_left, rw real_inner_add_sub_eq_zero_iff, exact h }, convert congr_arg2 (+) h₂ h₁ using 1, { simp }, { abel } end variables (K) /-- In a complete space `E`, a vector splits as the sum of its orthogonal projections onto a complete submodule `K` and onto the orthogonal complement of `K`.-/ lemma eq_sum_orthogonal_projection_self_orthogonal_complement [complete_space E] [complete_space K] (w : E) : w = (orthogonal_projection K w : E) + (orthogonal_projection Kᗮ w : E) := begin obtain ⟨y, hy, z, hz, hwyz⟩ := K.exists_sum_mem_mem_orthogonal w, convert hwyz, { exact eq_orthogonal_projection_of_mem_orthogonal' hy hz hwyz }, { rw add_comm at hwyz, refine eq_orthogonal_projection_of_mem_orthogonal' hz _ hwyz, simp [hy] } end /-- The Pythagorean theorem, for an orthogonal projection.-/ lemma norm_sq_eq_add_norm_sq_projection (x : E) (S : submodule 𝕜 E) [complete_space E] [complete_space S] : ‖x‖^2 = ‖orthogonal_projection S x‖^2 + ‖orthogonal_projection Sᗮ x‖^2 := begin let p1 := orthogonal_projection S, let p2 := orthogonal_projection Sᗮ, have x_decomp : x = p1 x + p2 x := eq_sum_orthogonal_projection_self_orthogonal_complement S x, have x_orth : ⟪ (p1 x : E), p2 x ⟫ = 0 := submodule.inner_right_of_mem_orthogonal (set_like.coe_mem (p1 x)) (set_like.coe_mem (p2 x)), nth_rewrite 0 [x_decomp], simp only [sq, norm_add_sq_eq_norm_sq_add_norm_sq_of_inner_eq_zero ((p1 x) : E) (p2 x) x_orth, add_left_inj, mul_eq_mul_left_iff, norm_eq_zero, true_or, eq_self_iff_true, submodule.coe_norm, submodule.coe_eq_zero] end /-- In a complete space `E`, the projection maps onto a complete subspace `K` and its orthogonal complement sum to the identity. -/ lemma id_eq_sum_orthogonal_projection_self_orthogonal_complement [complete_space E] [complete_space K] : continuous_linear_map.id 𝕜 E = K.subtypeL.comp (orthogonal_projection K) + Kᗮ.subtypeL.comp (orthogonal_projection Kᗮ) := by { ext w, exact eq_sum_orthogonal_projection_self_orthogonal_complement K w } @[simp] lemma inner_orthogonal_projection_eq_of_mem_right [complete_space K] (u : K) (v : E) : ⟪orthogonal_projection K v, u⟫ = ⟪v, u⟫ := calc ⟪orthogonal_projection K v, u⟫ = ⟪(orthogonal_projection K v : E), u⟫ : K.coe_inner _ _ ... = ⟪(orthogonal_projection K v : E), u⟫ + ⟪v - orthogonal_projection K v, u⟫ : by rw [orthogonal_projection_inner_eq_zero _ _ (submodule.coe_mem _), add_zero] ... = ⟪v, u⟫ : by rw [← inner_add_left, add_sub_cancel'_right] @[simp] lemma inner_orthogonal_projection_eq_of_mem_left [complete_space K] (u : K) (v : E) : ⟪u, orthogonal_projection K v⟫ = ⟪(u : E), v⟫ := by rw [← inner_conj_sym, ← inner_conj_sym (u : E), inner_orthogonal_projection_eq_of_mem_right] /-- The orthogonal projection is self-adjoint. -/ lemma inner_orthogonal_projection_left_eq_right [complete_space K] (u v : E) : ⟪↑(orthogonal_projection K u), v⟫ = ⟪u, orthogonal_projection K v⟫ := by rw [← inner_orthogonal_projection_eq_of_mem_left, inner_orthogonal_projection_eq_of_mem_right] /-- The orthogonal projection is symmetric. -/ lemma orthogonal_projection_is_symmetric [complete_space K] : (K.subtypeL ∘L orthogonal_projection K : E →ₗ[𝕜] E).is_symmetric := inner_orthogonal_projection_left_eq_right K open finite_dimensional /-- Given a finite-dimensional subspace `K₂`, and a subspace `K₁` containined in it, the dimensions of `K₁` and the intersection of its orthogonal subspace with `K₂` add to that of `K₂`. -/ lemma submodule.finrank_add_inf_finrank_orthogonal {K₁ K₂ : submodule 𝕜 E} [finite_dimensional 𝕜 K₂] (h : K₁ ≤ K₂) : finrank 𝕜 K₁ + finrank 𝕜 (K₁ᗮ ⊓ K₂ : submodule 𝕜 E) = finrank 𝕜 K₂ := begin haveI := submodule.finite_dimensional_of_le h, haveI := proper_is_R_or_C 𝕜 K₁, have hd := submodule.dim_sup_add_dim_inf_eq K₁ (K₁ᗮ ⊓ K₂), rw [←inf_assoc, (submodule.orthogonal_disjoint K₁).eq_bot, bot_inf_eq, finrank_bot, submodule.sup_orthogonal_inf_of_complete_space h] at hd, rw add_zero at hd, exact hd.symm end /-- Given a finite-dimensional subspace `K₂`, and a subspace `K₁` containined in it, the dimensions of `K₁` and the intersection of its orthogonal subspace with `K₂` add to that of `K₂`. -/ lemma submodule.finrank_add_inf_finrank_orthogonal' {K₁ K₂ : submodule 𝕜 E} [finite_dimensional 𝕜 K₂] (h : K₁ ≤ K₂) {n : ℕ} (h_dim : finrank 𝕜 K₁ + n = finrank 𝕜 K₂) : finrank 𝕜 (K₁ᗮ ⊓ K₂ : submodule 𝕜 E) = n := by { rw ← add_right_inj (finrank 𝕜 K₁), simp [submodule.finrank_add_inf_finrank_orthogonal h, h_dim] } /-- Given a finite-dimensional space `E` and subspace `K`, the dimensions of `K` and `Kᗮ` add to that of `E`. -/ lemma submodule.finrank_add_finrank_orthogonal [finite_dimensional 𝕜 E] (K : submodule 𝕜 E) : finrank 𝕜 K + finrank 𝕜 Kᗮ = finrank 𝕜 E := begin convert submodule.finrank_add_inf_finrank_orthogonal (le_top : K ≤ ⊤) using 1, { rw inf_top_eq }, { simp } end /-- Given a finite-dimensional space `E` and subspace `K`, the dimensions of `K` and `Kᗮ` add to that of `E`. -/ lemma submodule.finrank_add_finrank_orthogonal' [finite_dimensional 𝕜 E] {K : submodule 𝕜 E} {n : ℕ} (h_dim : finrank 𝕜 K + n = finrank 𝕜 E) : finrank 𝕜 Kᗮ = n := by { rw ← add_right_inj (finrank 𝕜 K), simp [submodule.finrank_add_finrank_orthogonal, h_dim] } local attribute [instance] fact_finite_dimensional_of_finrank_eq_succ /-- In a finite-dimensional inner product space, the dimension of the orthogonal complement of the span of a nonzero vector is one less than the dimension of the space. -/ lemma finrank_orthogonal_span_singleton {n : ℕ} [_i : fact (finrank 𝕜 E = n + 1)] {v : E} (hv : v ≠ 0) : finrank 𝕜 (𝕜 ∙ v)ᗮ = n := submodule.finrank_add_finrank_orthogonal' $ by simp [finrank_span_singleton hv, _i.elim, add_comm] /-- An element `φ` of the orthogonal group of `F` can be factored as a product of reflections, and specifically at most as many reflections as the dimension of the complement of the fixed subspace of `φ`. -/ lemma linear_isometry_equiv.reflections_generate_dim_aux [finite_dimensional ℝ F] {n : ℕ} (φ : F ≃ₗᵢ[ℝ] F) (hn : finrank ℝ (ker (continuous_linear_map.id ℝ F - φ))ᗮ ≤ n) : ∃ l : list F, l.length ≤ n ∧ φ = (l.map (λ v, reflection (ℝ ∙ v)ᗮ)).prod := begin -- We prove this by strong induction on `n`, the dimension of the orthogonal complement of the -- fixed subspace of the endomorphism `φ` induction n with n IH generalizing φ, { -- Base case: `n = 0`, the fixed subspace is the whole space, so `φ = id` refine ⟨[], rfl.le, show φ = 1, from _⟩, have : ker (continuous_linear_map.id ℝ F - φ) = ⊤, { rwa [le_zero_iff, finrank_eq_zero, submodule.orthogonal_eq_bot_iff] at hn }, symmetry, ext x, have := linear_map.congr_fun (linear_map.ker_eq_top.mp this) x, simpa only [sub_eq_zero, continuous_linear_map.to_linear_map_eq_coe, continuous_linear_map.coe_sub, linear_map.sub_apply, linear_map.zero_apply] using this }, { -- Inductive step. Let `W` be the fixed subspace of `φ`. We suppose its complement to have -- dimension at most n + 1. let W := ker (continuous_linear_map.id ℝ F - φ), have hW : ∀ w ∈ W, φ w = w := λ w hw, (sub_eq_zero.mp hw).symm, by_cases hn' : finrank ℝ Wᗮ ≤ n, { obtain ⟨V, hV₁, hV₂⟩ := IH φ hn', exact ⟨V, hV₁.trans n.le_succ, hV₂⟩ }, -- Take a nonzero element `v` of the orthogonal complement of `W`. haveI : nontrivial Wᗮ := nontrivial_of_finrank_pos (by linarith [zero_le n] : 0 < finrank ℝ Wᗮ), obtain ⟨v, hv⟩ := exists_ne (0 : Wᗮ), have hφv : φ v ∈ Wᗮ, { intros w hw, rw [← hW w hw, linear_isometry_equiv.inner_map_map], exact v.prop w hw }, have hv' : (v:F) ∉ W, { intros h, exact hv ((submodule.mem_left_iff_eq_zero_of_disjoint W.orthogonal_disjoint).mp h) }, -- Let `ρ` be the reflection in `v - φ v`; this is designed to swap `v` and `φ v` let x : F := v - φ v, let ρ := reflection (ℝ ∙ x)ᗮ, -- Notation: Let `V` be the fixed subspace of `φ.trans ρ` let V := ker (continuous_linear_map.id ℝ F - (φ.trans ρ)), have hV : ∀ w, ρ (φ w) = w → w ∈ V, { intros w hw, change w - ρ (φ w) = 0, rw [sub_eq_zero, hw] }, -- Everything fixed by `φ` is fixed by `φ.trans ρ` have H₂V : W ≤ V, { intros w hw, apply hV, rw hW w hw, refine reflection_mem_subspace_eq_self _, rw submodule.mem_orthogonal_singleton_iff_inner_left, exact submodule.sub_mem _ v.prop hφv _ hw }, -- `v` is also fixed by `φ.trans ρ` have H₁V : (v : F) ∈ V, { apply hV, have : ρ v = φ v := reflection_sub (φ.norm_map v).symm, rw ←this, exact reflection_reflection _ _, }, -- By dimension-counting, the complement of the fixed subspace of `φ.trans ρ` has dimension at -- most `n` have : finrank ℝ Vᗮ ≤ n, { change finrank ℝ Wᗮ ≤ n + 1 at hn, have : finrank ℝ W + 1 ≤ finrank ℝ V := submodule.finrank_lt_finrank_of_lt (set_like.lt_iff_le_and_exists.2 ⟨H₂V, v, H₁V, hv'⟩), have : finrank ℝ V + finrank ℝ Vᗮ = finrank ℝ F := V.finrank_add_finrank_orthogonal, have : finrank ℝ W + finrank ℝ Wᗮ = finrank ℝ F := W.finrank_add_finrank_orthogonal, linarith }, -- So apply the inductive hypothesis to `φ.trans ρ` obtain ⟨l, hl, hφl⟩ := IH (ρ * φ) this, -- Prepend `ρ` to the factorization into reflections obtained for `φ.trans ρ`; this gives a -- factorization into reflections for `φ`. refine ⟨x :: l, nat.succ_le_succ hl, _⟩, rw [list.map_cons, list.prod_cons], have := congr_arg ((*) ρ) hφl, rwa [←mul_assoc, reflection_mul_reflection, one_mul] at this, } end /-- The orthogonal group of `F` is generated by reflections; specifically each element `φ` of the orthogonal group is a product of at most as many reflections as the dimension of `F`. Special case of the **Cartan–Dieudonné theorem**. -/ lemma linear_isometry_equiv.reflections_generate_dim [finite_dimensional ℝ F] (φ : F ≃ₗᵢ[ℝ] F) : ∃ l : list F, l.length ≤ finrank ℝ F ∧ φ = (l.map (λ v, reflection (ℝ ∙ v)ᗮ)).prod := let ⟨l, hl₁, hl₂⟩ := φ.reflections_generate_dim_aux le_rfl in ⟨l, hl₁.trans (submodule.finrank_le _), hl₂⟩ /-- The orthogonal group of `F` is generated by reflections. -/ lemma linear_isometry_equiv.reflections_generate [finite_dimensional ℝ F] : subgroup.closure (set.range (λ v : F, reflection (ℝ ∙ v)ᗮ)) = ⊤ := begin rw subgroup.eq_top_iff', intros φ, rcases φ.reflections_generate_dim with ⟨l, _, rfl⟩, apply (subgroup.closure _).list_prod_mem, intros x hx, rcases list.mem_map.mp hx with ⟨a, _, hax⟩, exact subgroup.subset_closure ⟨a, hax⟩, end end orthogonal section orthogonal_family variables {ι : Type*} /-- An orthogonal family of subspaces of `E` satisfies `direct_sum.is_internal` (that is, they provide an internal direct sum decomposition of `E`) if and only if their span has trivial orthogonal complement. -/ lemma orthogonal_family.is_internal_iff_of_is_complete [decidable_eq ι] {V : ι → submodule 𝕜 E} (hV : @orthogonal_family 𝕜 _ _ _ _ (λ i, V i) _ (λ i, (V i).subtypeₗᵢ)) (hc : is_complete (↑(supr V) : set E)) : direct_sum.is_internal V ↔ (supr V)ᗮ = ⊥ := begin haveI : complete_space ↥(supr V) := hc.complete_space_coe, simp only [direct_sum.is_internal_submodule_iff_independent_and_supr_eq_top, hV.independent, true_and, submodule.orthogonal_eq_bot_iff] end /-- An orthogonal family of subspaces of `E` satisfies `direct_sum.is_internal` (that is, they provide an internal direct sum decomposition of `E`) if and only if their span has trivial orthogonal complement. -/ lemma orthogonal_family.is_internal_iff [decidable_eq ι] [finite_dimensional 𝕜 E] {V : ι → submodule 𝕜 E} (hV : @orthogonal_family 𝕜 _ _ _ _ (λ i, V i) _ (λ i, (V i).subtypeₗᵢ)) : direct_sum.is_internal V ↔ (supr V)ᗮ = ⊥ := begin haveI h := finite_dimensional.proper_is_R_or_C 𝕜 ↥(supr V), exact hV.is_internal_iff_of_is_complete (complete_space_coe_iff_is_complete.mp infer_instance) end end orthogonal_family section orthonormal_basis variables {𝕜 E} {v : set E} open finite_dimensional submodule set /-- An orthonormal set in an `inner_product_space` is maximal, if and only if the orthogonal complement of its span is empty. -/ lemma maximal_orthonormal_iff_orthogonal_complement_eq_bot (hv : orthonormal 𝕜 (coe : v → E)) : (∀ u ⊇ v, orthonormal 𝕜 (coe : u → E) → u = v) ↔ (span 𝕜 v)ᗮ = ⊥ := begin rw submodule.eq_bot_iff, split, { contrapose!, -- ** direction 1: nonempty orthogonal complement implies nonmaximal rintros ⟨x, hx', hx⟩, -- take a nonzero vector and normalize it let e := (‖x‖⁻¹ : 𝕜) • x, have he : ‖e‖ = 1 := by simp [e, norm_smul_inv_norm hx], have he' : e ∈ (span 𝕜 v)ᗮ := smul_mem' _ _ hx', have he'' : e ∉ v, { intros hev, have : e = 0, { have : e ∈ (span 𝕜 v) ⊓ (span 𝕜 v)ᗮ := ⟨subset_span hev, he'⟩, simpa [(span 𝕜 v).inf_orthogonal_eq_bot] using this }, have : e ≠ 0 := hv.ne_zero ⟨e, hev⟩, contradiction }, -- put this together with `v` to provide a candidate orthonormal basis for the whole space refine ⟨insert e v, v.subset_insert e, ⟨_, _⟩, (v.ne_insert_of_not_mem he'').symm⟩, { -- show that the elements of `insert e v` have unit length rintros ⟨a, ha'⟩, cases eq_or_mem_of_mem_insert ha' with ha ha, { simp [ha, he] }, { exact hv.1 ⟨a, ha⟩ } }, { -- show that the elements of `insert e v` are orthogonal have h_end : ∀ a ∈ v, ⟪a, e⟫ = 0, { intros a ha, exact he' a (submodule.subset_span ha) }, rintros ⟨a, ha'⟩, cases eq_or_mem_of_mem_insert ha' with ha ha, { rintros ⟨b, hb'⟩ hab', have hb : b ∈ v, { refine mem_of_mem_insert_of_ne hb' _, intros hbe', apply hab', simp [ha, hbe'] }, rw inner_eq_zero_sym, simpa [ha] using h_end b hb }, rintros ⟨b, hb'⟩ hab', cases eq_or_mem_of_mem_insert hb' with hb hb, { simpa [hb] using h_end a ha }, have : (⟨a, ha⟩ : v) ≠ ⟨b, hb⟩, { intros hab'', apply hab', simpa using hab'' }, exact hv.2 this } }, { -- ** direction 2: empty orthogonal complement implies maximal simp only [subset.antisymm_iff], rintros h u (huv : v ⊆ u) hu, refine ⟨_, huv⟩, intros x hxu, refine ((mt (h x)) (hu.ne_zero ⟨x, hxu⟩)).imp_symm _, intros hxv y hy, have hxv' : (⟨x, hxu⟩ : u) ∉ (coe ⁻¹' v : set u) := by simp [huv, hxv], obtain ⟨l, hl, rfl⟩ : ∃ l ∈ finsupp.supported 𝕜 𝕜 (coe ⁻¹' v : set u), (finsupp.total ↥u E 𝕜 coe) l = y, { rw ← finsupp.mem_span_image_iff_total, simp [huv, inter_eq_self_of_subset_left, hy] }, exact hu.inner_finsupp_eq_zero hxv' hl } end variables [finite_dimensional 𝕜 E] /-- An orthonormal set in a finite-dimensional `inner_product_space` is maximal, if and only if it is a basis. -/ lemma maximal_orthonormal_iff_basis_of_finite_dimensional (hv : orthonormal 𝕜 (coe : v → E)) : (∀ u ⊇ v, orthonormal 𝕜 (coe : u → E) → u = v) ↔ ∃ b : basis v 𝕜 E, ⇑b = coe := begin haveI := proper_is_R_or_C 𝕜 (span 𝕜 v), rw maximal_orthonormal_iff_orthogonal_complement_eq_bot hv, have hv_compl : is_complete (span 𝕜 v : set E) := (span 𝕜 v).complete_of_finite_dimensional, rw submodule.orthogonal_eq_bot_iff, have hv_coe : range (coe : v → E) = v := by simp, split, { refine λ h, ⟨basis.mk hv.linear_independent _, basis.coe_mk _ _⟩, convert h.ge }, { rintros ⟨h, coe_h⟩, rw [← h.span_eq, coe_h, hv_coe] } end end orthonormal_basis
972b3f575971b1be61e196fe4e90b9aa1d11c051
618003631150032a5676f229d13a079ac875ff77
/src/number_theory/sum_two_squares.lean
a833e09a9fef6744657bc0624e1f5d7787c36103
[ "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
786
lean
/- Copyright (c) 2019 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Chris Hughes -/ import data.zsqrtd.gaussian_int /-! # Sums of two squares Proof of Fermat's theorem on the sum of two squares. Every prime congruent to 1 mod 4 is the sum of two squares -/ open gaussian_int principal_ideal_domain namespace nat namespace prime /-- Fermat's theorem on the sum of two squares. Every prime congruent to 1 mod 4 is the sum of two squares -/ lemma sum_two_squares (p : ℕ) [hp : _root_.fact p.prime] (hp1 : p % 4 = 1) : ∃ a b : ℕ, a ^ 2 + b ^ 2 = p := sum_two_squares_of_nat_prime_of_not_irreducible p (by rw [irreducible_iff_prime, prime_iff_mod_four_eq_three_of_nat_prime p, hp1]; norm_num) end prime end nat
a53b8e8a63cc212b546eccb345c26f023c98b0a6
a45212b1526d532e6e83c44ddca6a05795113ddc
/src/category_theory/instances/CommRing/basic.lean
6df7f277ebd35dc144659add66e96d7bcc1f01f4
[ "Apache-2.0" ]
permissive
fpvandoorn/mathlib
b21ab4068db079cbb8590b58fda9cc4bc1f35df4
b3433a51ea8bc07c4159c1073838fc0ee9b8f227
refs/heads/master
1,624,791,089,608
1,556,715,231,000
1,556,715,231,000
165,722,980
5
0
Apache-2.0
1,552,657,455,000
1,547,494,646,000
Lean
UTF-8
Lean
false
false
3,105
lean
/- Copyright (c) 2018 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Johannes Hölzl Introduce CommRing -- the category of commutative rings. -/ import category_theory.instances.monoids import algebra.ring import data.int.basic universes u v open category_theory namespace category_theory.instances /-- The category of rings. -/ @[reducible] def Ring : Type (u+1) := bundled ring instance (x : Ring) : ring x := x.str instance concrete_is_ring_hom : concrete_category @is_ring_hom := ⟨by introsI α ia; apply_instance, by introsI α β γ ia ib ic f g hf hg; apply_instance⟩ instance Ring.hom_is_ring_hom {R S : Ring} (f : R ⟶ S) : is_ring_hom (f : R → S) := f.2 /-- The category of commutative rings. -/ @[reducible] def CommRing : Type (u+1) := bundled comm_ring instance (x : CommRing) : comm_ring x := x.str -- Here we don't use the `concrete` machinery, -- because it would require introducing a useless synonym for `is_ring_hom`. instance : category CommRing := { hom := λ R S, { f : R → S // is_ring_hom f }, id := λ R, ⟨ id, by resetI; apply_instance ⟩, comp := λ R S T g h, ⟨ h.1 ∘ g.1, begin haveI := g.2, haveI := h.2, apply_instance end ⟩ } namespace CommRing variables {R S T : CommRing.{u}} @[simp] lemma id_val : subtype.val (𝟙 R) = id := rfl @[simp] lemma comp_val (f : R ⟶ S) (g : S ⟶ T) : (f ≫ g).val = g.val ∘ f.val := rfl instance hom_coe : has_coe_to_fun (R ⟶ S) := { F := λ f, R → S, coe := λ f, f.1 } @[simp] lemma hom_coe_app (f : R ⟶ S) (r : R) : f r = f.val r := rfl instance hom_is_ring_hom (f : R ⟶ S) : is_ring_hom (f : R → S) := f.2 def Int : CommRing := ⟨ℤ, infer_instance⟩ def Int.cast {R : CommRing} : Int ⟶ R := { val := int.cast, property := by apply_instance } def int.eq_cast' {R : Type u} [ring R] (f : int → R) [is_ring_hom f] : f = int.cast := funext $ int.eq_cast f (is_ring_hom.map_one f) (λ _ _, is_ring_hom.map_add f) def Int.hom_unique {R : CommRing} : unique (Int ⟶ R) := { default := Int.cast, uniq := λ f, subtype.ext.mpr $ funext $ int.eq_cast f f.2.map_one f.2.map_add } /-- The forgetful functor commutative rings to Type. -/ def forget : CommRing.{u} ⥤ Type u := { obj := λ R, R, map := λ _ _ f, f } instance forget.faithful : faithful (forget) := {} /-- The functor from commutative rings to rings. -/ def to_Ring : CommRing.{u} ⥤ Ring.{u} := { obj := λ X, { α := X.1, str := by apply_instance }, map := λ X Y f, ⟨ f, by apply_instance ⟩ } instance to_Ring.faithful : faithful (to_Ring) := {} /-- The forgetful functor from commutative rings to (multiplicative) commutative monoids. -/ def forget_to_CommMon : CommRing.{u} ⥤ CommMon.{u} := { obj := λ X, { α := X.1, str := by apply_instance }, map := λ X Y f, ⟨ f, by apply_instance ⟩ } instance forget_to_CommMon.faithful : faithful (forget_to_CommMon) := {} example : faithful (forget_to_CommMon ⋙ CommMon.forget_to_Mon) := by apply_instance end CommRing end category_theory.instances
7e923f2bddecacdd47359d6406086e1da78d48dc
4b846d8dabdc64e7ea03552bad8f7fa74763fc67
/tests/lean/run/basic_monitor.lean
61d943488781e75953eaa351417cc3ca2b879c05
[ "Apache-2.0" ]
permissive
pacchiano/lean
9324b33f3ac3b5c5647285160f9f6ea8d0d767dc
fdadada3a970377a6df8afcd629a6f2eab6e84e8
refs/heads/master
1,611,357,380,399
1,489,870,101,000
1,489,870,101,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
299
lean
meta def basic_monitor : vm_monitor nat := { init := 0, step := λ s, return (trace ("step " ++ s^.to_string) (s+1)) } run_cmd vm_monitor.register `basic_monitor set_option debugger true example (a b : Prop) : a → b → a ∧ b := begin intros, constructor, assumption, assumption end
669f1f3a7dbdcee68148713f23c7aa01d3f0750e
969dbdfed67fda40a6f5a2b4f8c4a3c7dc01e0fb
/src/category_theory/adjunction/mates.lean
4edaeb4f8aedef5edfb55325e1a922db002f6ed8
[ "Apache-2.0" ]
permissive
SAAluthwela/mathlib
62044349d72dd63983a8500214736aa7779634d3
83a4b8b990907291421de54a78988c024dc8a552
refs/heads/master
1,679,433,873,417
1,615,998,031,000
1,615,998,031,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
10,481
lean
/- Copyright (c) 2020 Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Bhavik Mehta -/ import category_theory.adjunction.basic import category_theory.conj import category_theory.yoneda /-! # Mate of natural transformations This file establishes the bijection between the 2-cells L₁ R₁ C --→ D C ←-- D G ↓ ↗ ↓ H G ↓ ↘ ↓ H E --→ F E ←-- F L₂ R₂ where `L₁ ⊣ R₁` and `L₂ ⊣ R₂`, and shows that in the special case where `G,H` are identity then the bijection preserves and reflects isomorphisms (i.e. we have bijections `(L₂ ⟶ L₁) ≃ (R₁ ⟶ R₂)`, and if either side is an iso then the other side is as well). On its own, this bijection is not particularly useful but it includes a number of interesting cases as specializations. For instance, this generalises the fact that adjunctions are unique (since if `L₁ ≅ L₂` then we deduce `R₁ ≅ R₂`). Another example arises from considering the square representing that a functor `H` preserves products, in particular the morphism `HA ⨯ H- ⟶ H(A ⨯ -)`. Then provided `(A ⨯ -)` and `HA ⨯ -` have left adjoints (for instance if the relevant categories are cartesian closed), the transferred natural transformation is the exponential comparison morphism: `H(A ^ -) ⟶ HA ^ H-`. Furthermore if `H` has a left adjoint `L`, this morphism is an isomorphism iff its mate `L(HA ⨯ -) ⟶ A ⨯ L-` is an isomorphism, see https://ncatlab.org/nlab/show/Frobenius+reciprocity#InCategoryTheory. This also relates to Grothendieck's yoga of six operations, though this is not spelled out in mathlib: https://ncatlab.org/nlab/show/six+operations. -/ universes v₁ v₂ v₃ v₄ u₁ u₂ u₃ u₄ namespace category_theory open category variables {C : Type u₁} {D : Type u₂} [category.{v₁} C] [category.{v₂} D] section square variables {E : Type u₃} {F : Type u₄} [category.{v₃} E] [category.{v₄} F] variables {G : C ⥤ E} {H : D ⥤ F} {L₁ : C ⥤ D} {R₁ : D ⥤ C} {L₂ : E ⥤ F} {R₂ : F ⥤ E} variables (adj₁ : L₁ ⊣ R₁) (adj₂ : L₂ ⊣ R₂) include adj₁ adj₂ /-- Suppose we have a square of functors (where the top and bottom are adjunctions `L₁ ⊣ R₁` and `L₂ ⊣ R₂` respectively). C ↔ D G ↓ ↓ H E ↔ F Then we have a bijection between natural transformations `G ⋙ L₂ ⟶ L₁ ⋙ H` and `R₁ ⋙ G ⟶ H ⋙ R₂`. This can be seen as a bijection of the 2-cells: L₁ R₁ C --→ D C ←-- D G ↓ ↗ ↓ H G ↓ ↘ ↓ H E --→ F E ←-- F L₂ R₂ Note that if one of the transformations is an iso, it does not imply the other is an iso. -/ def transfer_nat_trans : (G ⋙ L₂ ⟶ L₁ ⋙ H) ≃ (R₁ ⋙ G ⟶ H ⋙ R₂) := { to_fun := λ h, { app := λ X, adj₂.unit.app _ ≫ R₂.map (h.app _ ≫ H.map (adj₁.counit.app _)), naturality' := λ X Y f, begin dsimp, rw [assoc, ← R₂.map_comp, assoc, ← H.map_comp, ← adj₁.counit_naturality, H.map_comp, ←functor.comp_map L₁, ←h.naturality_assoc], simp, end }, inv_fun := λ h, { app := λ X, L₂.map (G.map (adj₁.unit.app _) ≫ h.app _) ≫ adj₂.counit.app _, naturality' := λ X Y f, begin dsimp, rw [← L₂.map_comp_assoc, ← G.map_comp_assoc, ← adj₁.unit_naturality, G.map_comp_assoc, ← functor.comp_map, h.naturality], simp, end }, left_inv := λ h, begin ext X, dsimp, simp only [L₂.map_comp, assoc, adj₂.counit_naturality, adj₂.left_triangle_components_assoc, ←functor.comp_map G L₂, h.naturality_assoc, functor.comp_map L₁, ←H.map_comp, adj₁.left_triangle_components], dsimp, simp, -- See library note [dsimp, simp]. end, right_inv := λ h, begin ext X, dsimp, simp [-functor.comp_map, ←functor.comp_map H, functor.comp_map R₁, -nat_trans.naturality, ←h.naturality, -functor.map_comp, ←functor.map_comp_assoc G, R₂.map_comp], end } lemma transfer_nat_trans_counit (f : G ⋙ L₂ ⟶ L₁ ⋙ H) (Y : D) : L₂.map ((transfer_nat_trans adj₁ adj₂ f).app _) ≫ adj₂.counit.app _ = f.app _ ≫ H.map (adj₁.counit.app Y) := by { erw functor.map_comp, simp } lemma unit_transfer_nat_trans (f : G ⋙ L₂ ⟶ L₁ ⋙ H) (X : C) : G.map (adj₁.unit.app X) ≫ (transfer_nat_trans adj₁ adj₂ f).app _ = adj₂.unit.app _ ≫ R₂.map (f.app _) := begin dsimp [transfer_nat_trans], rw [←adj₂.unit_naturality_assoc, ←R₂.map_comp, ← functor.comp_map G L₂, f.naturality_assoc, functor.comp_map, ← H.map_comp], dsimp, simp, -- See library note [dsimp, simp] end end square section self variables {L₁ L₂ L₃ : C ⥤ D} {R₁ R₂ R₃ : D ⥤ C} variables (adj₁ : L₁ ⊣ R₁) (adj₂ : L₂ ⊣ R₂) (adj₃ : L₃ ⊣ R₃) /-- Given two adjunctions `L₁ ⊣ R₁` and `L₂ ⊣ R₂` both between categories `C`, `D`, there is a bijection between natural transformations `L₂ ⟶ L₁` and natural transformations `R₁ ⟶ R₂`. This is defined as a special case of `transfer_nat_trans`, where the two "vertical" functors are identity. TODO: Generalise to when the two vertical functors are equivalences rather than being exactly `𝟭`. Furthermore, this bijection preserves (and reflects) isomorphisms, i.e. a transformation is an iso iff its image under the bijection is an iso, see eg `category_theory.transfer_nat_trans_self_iso`. This is in contrast to the general case `transfer_nat_trans` which does not in general have this property. -/ def transfer_nat_trans_self : (L₂ ⟶ L₁) ≃ (R₁ ⟶ R₂) := calc (L₂ ⟶ L₁) ≃ _ : (iso.hom_congr L₂.left_unitor L₁.right_unitor).symm ... ≃ _ : transfer_nat_trans adj₁ adj₂ ... ≃ (R₁ ⟶ R₂) : R₁.right_unitor.hom_congr R₂.left_unitor lemma transfer_nat_trans_self_counit (f : L₂ ⟶ L₁) (X) : L₂.map ((transfer_nat_trans_self adj₁ adj₂ f).app _) ≫ adj₂.counit.app X = f.app _ ≫ adj₁.counit.app X := begin dsimp [transfer_nat_trans_self], rw [id_comp, comp_id], have := transfer_nat_trans_counit adj₁ adj₂ (L₂.left_unitor.hom ≫ f ≫ L₁.right_unitor.inv) X, dsimp at this, rw this, simp, end lemma unit_transfer_nat_trans_self (f : L₂ ⟶ L₁) (X) : adj₁.unit.app _ ≫ (transfer_nat_trans_self adj₁ adj₂ f).app _ = adj₂.unit.app X ≫ functor.map _ (f.app _) := begin dsimp [transfer_nat_trans_self], rw [id_comp, comp_id], have := unit_transfer_nat_trans adj₁ adj₂ (L₂.left_unitor.hom ≫ f ≫ L₁.right_unitor.inv) X, dsimp at this, rw this, simp end @[simp] lemma transfer_nat_trans_self_id : transfer_nat_trans_self adj₁ adj₁ (𝟙 _) = 𝟙 _ := by { ext, dsimp [transfer_nat_trans_self, transfer_nat_trans], simp } -- See library note [dsimp, simp] @[simp] lemma transfer_nat_trans_self_symm_id : (transfer_nat_trans_self adj₁ adj₁).symm (𝟙 _) = 𝟙 _ := by { rw equiv.symm_apply_eq, simp } lemma transfer_nat_trans_self_comp (f g) : transfer_nat_trans_self adj₁ adj₂ f ≫ transfer_nat_trans_self adj₂ adj₃ g = transfer_nat_trans_self adj₁ adj₃ (g ≫ f) := begin ext, dsimp [transfer_nat_trans_self, transfer_nat_trans], simp only [id_comp, comp_id], rw [←adj₃.unit_naturality_assoc, ←R₃.map_comp, g.naturality_assoc, L₂.map_comp, assoc, adj₂.counit_naturality, adj₂.left_triangle_components_assoc, assoc], end lemma transfer_nat_trans_self_symm_comp (f g) : (transfer_nat_trans_self adj₂ adj₁).symm f ≫ (transfer_nat_trans_self adj₃ adj₂).symm g = (transfer_nat_trans_self adj₃ adj₁).symm (g ≫ f) := by { rw [equiv.eq_symm_apply, ← transfer_nat_trans_self_comp _ adj₂], simp } lemma transfer_nat_trans_self_comm {f g} (gf : g ≫ f = 𝟙 _) : transfer_nat_trans_self adj₁ adj₂ f ≫ transfer_nat_trans_self adj₂ adj₁ g = 𝟙 _ := by rw [transfer_nat_trans_self_comp, gf, transfer_nat_trans_self_id] lemma transfer_nat_trans_self_symm_comm {f g} (gf : g ≫ f = 𝟙 _) : (transfer_nat_trans_self adj₁ adj₂).symm f ≫ (transfer_nat_trans_self adj₂ adj₁).symm g = 𝟙 _ := by rw [transfer_nat_trans_self_symm_comp, gf, transfer_nat_trans_self_symm_id] /-- If `f` is an isomorphism, then the transferred natural transformation is an isomorphism. The converse is given in `transfer_nat_trans_self_of_iso`. -/ instance transfer_nat_trans_self_iso (f : L₂ ⟶ L₁) [is_iso f] : is_iso (transfer_nat_trans_self adj₁ adj₂ f) := ⟨transfer_nat_trans_self adj₂ adj₁ (inv f), ⟨transfer_nat_trans_self_comm _ _ (by simp), transfer_nat_trans_self_comm _ _ (by simp)⟩⟩ /-- If `f` is an isomorphism, then the un-transferred natural transformation is an isomorphism. The converse is given in `transfer_nat_trans_self_symm_of_iso`. -/ instance transfer_nat_trans_self_symm_iso (f : R₁ ⟶ R₂) [is_iso f] : is_iso ((transfer_nat_trans_self adj₁ adj₂).symm f) := ⟨(transfer_nat_trans_self adj₂ adj₁).symm (inv f), ⟨transfer_nat_trans_self_symm_comm _ _ (by simp), transfer_nat_trans_self_symm_comm _ _ (by simp)⟩⟩ /-- If `f` is a natural transformation whose transferred natural transformation is an isomorphism, then `f` is an isomorphism. The converse is given in `transfer_nat_trans_self_iso`. -/ lemma transfer_nat_trans_self_of_iso (f : L₂ ⟶ L₁) [is_iso (transfer_nat_trans_self adj₁ adj₂ f)] : is_iso f := begin suffices : is_iso ((transfer_nat_trans_self adj₁ adj₂).symm (transfer_nat_trans_self adj₁ adj₂ f)), { simpa using this }, apply_instance, end /-- If `f` is a natural transformation whose un-transferred natural transformation is an isomorphism, then `f` is an isomorphism. The converse is given in `transfer_nat_trans_self_symm_iso`. -/ lemma transfer_nat_trans_self_symm_of_iso (f : R₁ ⟶ R₂) [is_iso ((transfer_nat_trans_self adj₁ adj₂).symm f)] : is_iso f := begin suffices : is_iso ((transfer_nat_trans_self adj₁ adj₂) ((transfer_nat_trans_self adj₁ adj₂).symm f)), { simpa using this }, apply_instance, end end self end category_theory
d5af595553dfd25054ae533b53b21110fbc47f37
4727251e0cd73359b15b664c3170e5d754078599
/src/number_theory/legendre_symbol/gauss_eisenstein_lemmas.lean
be4dcb44bc93b4019ed2981194585ec02e11a489
[ "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
13,777
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 field_theory.finite.basic import data.zmod.basic /-! # Lemmas of Gauss and Eisenstein This file contains code for the proof of the Lemmas of Gauss and Eisenstein on the Legendre symbol. The main results are `gauss_lemma_aux` and `eisenstein_lemma_aux`; they are used in `quadratic_reciprocity.lean` to prove `gauss_lemma` and `eisenstein_lemma`, respectively. -/ open function finset nat finite_field zmod open_locale big_operators nat namespace zmod section wilson variables (p : ℕ) [fact p.prime] -- One can probably deduce the following from `finite_field.prod_univ_units_id_eq_neg_one` /-- **Wilson's Lemma**: the product of `1`, ..., `p-1` is `-1` modulo `p`. -/ @[simp] lemma wilsons_lemma : ((p - 1)! : zmod p) = -1 := begin refine calc ((p - 1)! : zmod p) = (∏ x in Ico 1 (succ (p - 1)), x) : by rw [← finset.prod_Ico_id_eq_factorial, prod_nat_cast] ... = (∏ x : (zmod p)ˣ, x) : _ ... = -1 : by simp_rw [← units.coe_hom_apply, ← (units.coe_hom (zmod p)).map_prod, prod_univ_units_id_eq_neg_one, units.coe_hom_apply, units.coe_neg, units.coe_one], have hp : 0 < p := (fact.out p.prime).pos, symmetry, refine prod_bij (λ a _, (a : zmod p).val) _ _ _ _, { intros a ha, rw [mem_Ico, ← nat.succ_sub hp, nat.succ_sub_one], split, { apply nat.pos_of_ne_zero, rw ← @val_zero p, assume h, apply units.ne_zero a (val_injective p h) }, { exact val_lt _ } }, { intros a ha, simp only [cast_id, nat_cast_val], }, { intros _ _ _ _ h, rw units.ext_iff, exact val_injective p h }, { intros b hb, rw [mem_Ico, nat.succ_le_iff, ← succ_sub hp, succ_sub_one, pos_iff_ne_zero] at hb, refine ⟨units.mk0 b _, finset.mem_univ _, _⟩, { assume h, apply hb.1, apply_fun val at h, simpa only [val_cast_of_lt hb.right, val_zero] using h }, { simp only [val_cast_of_lt hb.right, units.coe_mk0], } } end @[simp] lemma prod_Ico_one_prime : (∏ x in Ico 1 p, (x : zmod p)) = -1 := begin conv in (Ico 1 p) { rw [← succ_sub_one p, succ_sub (fact.out p.prime).pos] }, rw [← prod_nat_cast, finset.prod_Ico_id_eq_factorial, wilsons_lemma] end end wilson end zmod section gauss_eisenstein namespace legendre_symbol /-- The image of the map sending a non zero natural number `x ≤ p / 2` to the absolute value of the element of interger in the interval `(-p/2, p/2]` congruent to `a * x` mod p is the set of non zero natural numbers `x` such that `x ≤ p / 2` -/ lemma Ico_map_val_min_abs_nat_abs_eq_Ico_map_id (p : ℕ) [hp : fact p.prime] (a : zmod p) (hap : a ≠ 0) : (Ico 1 (p / 2).succ).1.map (λ x, (a * x).val_min_abs.nat_abs) = (Ico 1 (p / 2).succ).1.map (λ a, a) := begin have he : ∀ {x}, x ∈ Ico 1 (p / 2).succ → x ≠ 0 ∧ x ≤ p / 2, by simp [nat.lt_succ_iff, nat.succ_le_iff, pos_iff_ne_zero] {contextual := tt}, have hep : ∀ {x}, x ∈ Ico 1 (p / 2).succ → x < p, from λ x hx, lt_of_le_of_lt (he hx).2 (nat.div_lt_self hp.1.pos dec_trivial), have hpe : ∀ {x}, x ∈ Ico 1 (p / 2).succ → ¬ p ∣ x, from λ x hx hpx, not_lt_of_ge (le_of_dvd (nat.pos_of_ne_zero (he hx).1) hpx) (hep hx), have hmem : ∀ (x : ℕ) (hx : x ∈ Ico 1 (p / 2).succ), (a * x : zmod p).val_min_abs.nat_abs ∈ Ico 1 (p / 2).succ, { assume x hx, simp [hap, char_p.cast_eq_zero_iff (zmod p) p, hpe hx, lt_succ_iff, succ_le_iff, pos_iff_ne_zero, nat_abs_val_min_abs_le _], }, have hsurj : ∀ (b : ℕ) (hb : b ∈ Ico 1 (p / 2).succ), ∃ x ∈ Ico 1 (p / 2).succ, b = (a * x : zmod p).val_min_abs.nat_abs, { assume b hb, refine ⟨(b / a : zmod p).val_min_abs.nat_abs, mem_Ico.mpr ⟨_, _⟩, _⟩, { apply nat.pos_of_ne_zero, simp only [div_eq_mul_inv, hap, char_p.cast_eq_zero_iff (zmod p) p, hpe hb, not_false_iff, val_min_abs_eq_zero, inv_eq_zero, int.nat_abs_eq_zero, ne.def, mul_eq_zero, or_self] }, { apply lt_succ_of_le, apply nat_abs_val_min_abs_le }, { rw nat_cast_nat_abs_val_min_abs, split_ifs, { erw [mul_div_cancel' _ hap, val_min_abs_def_pos, val_cast_of_lt (hep hb), if_pos (le_of_lt_succ (mem_Ico.1 hb).2), int.nat_abs_of_nat], }, { erw [mul_neg, mul_div_cancel' _ hap, nat_abs_val_min_abs_neg, val_min_abs_def_pos, val_cast_of_lt (hep hb), if_pos (le_of_lt_succ (mem_Ico.1 hb).2), int.nat_abs_of_nat] } } }, exact multiset.map_eq_map_of_bij_of_nodup _ _ (finset.nodup _) (finset.nodup _) (λ x _, (a * x : zmod p).val_min_abs.nat_abs) hmem (λ _ _, rfl) (inj_on_of_surj_on_of_card_le _ hmem hsurj le_rfl) hsurj end private lemma gauss_lemma_aux₁ (p : ℕ) [fact p.prime] [fact (p % 2 = 1)] {a : ℤ} (hap : (a : zmod p) ≠ 0) : (a^(p / 2) * (p / 2)! : zmod p) = (-1)^((Ico 1 (p / 2).succ).filter (λ x : ℕ, ¬(a * x : zmod p).val ≤ p / 2)).card * (p / 2)! := calc (a ^ (p / 2) * (p / 2)! : zmod p) = (∏ x in Ico 1 (p / 2).succ, a * x) : by rw [prod_mul_distrib, ← prod_nat_cast, prod_Ico_id_eq_factorial, prod_const, card_Ico, succ_sub_one]; simp ... = (∏ x in Ico 1 (p / 2).succ, (a * x : zmod p).val) : by simp ... = (∏ x in Ico 1 (p / 2).succ, (if (a * x : zmod p).val ≤ p / 2 then 1 else -1) * (a * x : zmod p).val_min_abs.nat_abs) : prod_congr rfl $ λ _ _, begin simp only [nat_cast_nat_abs_val_min_abs], split_ifs; simp end ... = (-1)^((Ico 1 (p / 2).succ).filter (λ x : ℕ, ¬(a * x : zmod p).val ≤ p / 2)).card * (∏ x in Ico 1 (p / 2).succ, (a * x : zmod p).val_min_abs.nat_abs) : have (∏ x in Ico 1 (p / 2).succ, if (a * x : zmod p).val ≤ p / 2 then (1 : zmod p) else -1) = (∏ x in (Ico 1 (p / 2).succ).filter (λ x : ℕ, ¬(a * x : zmod p).val ≤ p / 2), -1), from prod_bij_ne_one (λ x _ _, x) (λ x, by split_ifs; simp * at * {contextual := tt}) (λ _ _ _ _ _ _, id) (λ b h _, ⟨b, by simp [-not_le, *] at *⟩) (by intros; split_ifs at *; simp * at *), by rw [prod_mul_distrib, this]; simp ... = (-1)^((Ico 1 (p / 2).succ).filter (λ x : ℕ, ¬(a * x : zmod p).val ≤ p / 2)).card * (p / 2)! : by rw [← prod_nat_cast, finset.prod_eq_multiset_prod, Ico_map_val_min_abs_nat_abs_eq_Ico_map_id p a hap, ← finset.prod_eq_multiset_prod, prod_Ico_id_eq_factorial] lemma gauss_lemma_aux (p : ℕ) [hp : fact p.prime] [fact (p % 2 = 1)] {a : ℤ} (hap : (a : zmod p) ≠ 0) : (a^(p / 2) : zmod p) = (-1)^((Ico 1 (p / 2).succ).filter (λ x : ℕ, p / 2 < (a * x : zmod p).val)).card := (mul_left_inj' (show ((p / 2)! : zmod p) ≠ 0, by rw [ne.def, char_p.cast_eq_zero_iff (zmod p) p, hp.1.dvd_factorial, not_le]; exact nat.div_lt_self hp.1.pos dec_trivial)).1 $ by simpa using gauss_lemma_aux₁ p hap private lemma eisenstein_lemma_aux₁ (p : ℕ) [fact p.prime] [hp2 : fact (p % 2 = 1)] {a : ℕ} (hap : (a : zmod p) ≠ 0) : ((∑ x in Ico 1 (p / 2).succ, a * x : ℕ) : zmod 2) = ((Ico 1 (p / 2).succ).filter ((λ x : ℕ, p / 2 < (a * x : zmod p).val))).card + ∑ x in Ico 1 (p / 2).succ, x + (∑ x in Ico 1 (p / 2).succ, (a * x) / p : ℕ) := have hp2 : (p : zmod 2) = (1 : ℕ), from (eq_iff_modeq_nat _).2 hp2.1, calc ((∑ x in Ico 1 (p / 2).succ, a * x : ℕ) : zmod 2) = ((∑ x in Ico 1 (p / 2).succ, ((a * x) % p + p * ((a * x) / p)) : ℕ) : zmod 2) : by simp only [mod_add_div] ... = (∑ x in Ico 1 (p / 2).succ, ((a * x : ℕ) : zmod p).val : ℕ) + (∑ x in Ico 1 (p / 2).succ, (a * x) / p : ℕ) : by simp only [val_nat_cast]; simp [sum_add_distrib, mul_sum.symm, nat.cast_add, nat.cast_mul, nat.cast_sum, hp2] ... = _ : congr_arg2 (+) (calc ((∑ x in Ico 1 (p / 2).succ, ((a * x : ℕ) : zmod p).val : ℕ) : zmod 2) = ∑ x in Ico 1 (p / 2).succ, ((((a * x : zmod p).val_min_abs + (if (a * x : zmod p).val ≤ p / 2 then 0 else p)) : ℤ) : zmod 2) : by simp only [(val_eq_ite_val_min_abs _).symm]; simp [nat.cast_sum] ... = ((Ico 1 (p / 2).succ).filter (λ x : ℕ, p / 2 < (a * x : zmod p).val)).card + ((∑ x in Ico 1 (p / 2).succ, (a * x : zmod p).val_min_abs.nat_abs) : ℕ) : by { simp [ite_cast, add_comm, sum_add_distrib, finset.sum_ite, hp2, nat.cast_sum], } ... = _ : by rw [finset.sum_eq_multiset_sum, Ico_map_val_min_abs_nat_abs_eq_Ico_map_id p a hap, ← finset.sum_eq_multiset_sum]; simp [nat.cast_sum]) rfl lemma eisenstein_lemma_aux (p : ℕ) [fact p.prime] [fact (p % 2 = 1)] {a : ℕ} (ha2 : a % 2 = 1) (hap : (a : zmod p) ≠ 0) : ((Ico 1 (p / 2).succ).filter ((λ x : ℕ, p / 2 < (a * x : zmod p).val))).card ≡ ∑ x in Ico 1 (p / 2).succ, (x * a) / p [MOD 2] := have ha2 : (a : zmod 2) = (1 : ℕ), from (eq_iff_modeq_nat _).2 ha2, (eq_iff_modeq_nat 2).1 $ sub_eq_zero.1 $ by simpa [add_left_comm, sub_eq_add_neg, finset.mul_sum.symm, mul_comm, ha2, nat.cast_sum, add_neg_eq_iff_eq_add.symm, neg_eq_self_mod_two, add_assoc] using eq.symm (eisenstein_lemma_aux₁ p hap) lemma div_eq_filter_card {a b c : ℕ} (hb0 : 0 < b) (hc : a / b ≤ c) : a / b = ((Ico 1 c.succ).filter (λ x, x * b ≤ a)).card := calc a / b = (Ico 1 (a / b).succ).card : by simp ... = ((Ico 1 c.succ).filter (λ x, x * b ≤ a)).card : congr_arg _ $ finset.ext $ λ x, have x * b ≤ a → x ≤ c, from λ h, le_trans (by rwa [le_div_iff_mul_le _ _ hb0]) hc, by simp [lt_succ_iff, le_div_iff_mul_le _ _ hb0]; tauto /-- The given sum is the number of integer points in the triangle formed by the diagonal of the rectangle `(0, p/2) × (0, q/2)` -/ private lemma sum_Ico_eq_card_lt {p q : ℕ} : ∑ a in Ico 1 (p / 2).succ, (a * q) / p = (((Ico 1 (p / 2).succ).product (Ico 1 (q / 2).succ)).filter (λ x : ℕ × ℕ, x.2 * p ≤ x.1 * q)).card := if hp0 : p = 0 then by simp [hp0, finset.ext_iff] else calc ∑ a in Ico 1 (p / 2).succ, (a * q) / p = ∑ a in Ico 1 (p / 2).succ, ((Ico 1 (q / 2).succ).filter (λ x, x * p ≤ a * q)).card : finset.sum_congr rfl $ λ x hx, div_eq_filter_card (nat.pos_of_ne_zero hp0) (calc x * q / p ≤ (p / 2) * q / p : nat.div_le_div_right (mul_le_mul_of_nonneg_right (le_of_lt_succ $ (mem_Ico.mp hx).2) (nat.zero_le _)) ... ≤ _ : nat.div_mul_div_le_div _ _ _) ... = _ : by rw [← card_sigma]; exact card_congr (λ a _, ⟨a.1, a.2⟩) (by simp only [mem_filter, mem_sigma, and_self, forall_true_iff, mem_product] {contextual := tt}) (λ ⟨_, _⟩ ⟨_, _⟩, by simp only [prod.mk.inj_iff, eq_self_iff_true, and_self, heq_iff_eq, forall_true_iff] {contextual := tt}) (λ ⟨b₁, b₂⟩ h, ⟨⟨b₁, b₂⟩, by revert h; simp only [mem_filter, eq_self_iff_true, exists_prop_of_true, mem_sigma, and_self, forall_true_iff, mem_product] {contextual := tt}⟩) /-- Each of the sums in this lemma is the cardinality of the set integer points in each of the two triangles formed by the diagonal of the rectangle `(0, p/2) × (0, q/2)`. Adding them gives the number of points in the rectangle. -/ lemma sum_mul_div_add_sum_mul_div_eq_mul (p q : ℕ) [hp : fact p.prime] (hq0 : (q : zmod p) ≠ 0) : ∑ a in Ico 1 (p / 2).succ, (a * q) / p + ∑ a in Ico 1 (q / 2).succ, (a * p) / q = (p / 2) * (q / 2) := begin have hswap : (((Ico 1 (q / 2).succ).product (Ico 1 (p / 2).succ)).filter (λ x : ℕ × ℕ, x.2 * q ≤ x.1 * p)).card = (((Ico 1 (p / 2).succ).product (Ico 1 (q / 2).succ)).filter (λ x : ℕ × ℕ, x.1 * q ≤ x.2 * p)).card := card_congr (λ x _, prod.swap x) (λ ⟨_, _⟩, by simp only [mem_filter, and_self, prod.swap_prod_mk, forall_true_iff, mem_product] {contextual := tt}) (λ ⟨_, _⟩ ⟨_, _⟩, by simp only [prod.mk.inj_iff, eq_self_iff_true, and_self, prod.swap_prod_mk, forall_true_iff] {contextual := tt}) (λ ⟨x₁, x₂⟩ h, ⟨⟨x₂, x₁⟩, by revert h; simp only [mem_filter, eq_self_iff_true, and_self, exists_prop_of_true, prod.swap_prod_mk, forall_true_iff, mem_product] {contextual := tt}⟩), have hdisj : disjoint (((Ico 1 (p / 2).succ).product (Ico 1 (q / 2).succ)).filter (λ x : ℕ × ℕ, x.2 * p ≤ x.1 * q)) (((Ico 1 (p / 2).succ).product (Ico 1 (q / 2).succ)).filter (λ x : ℕ × ℕ, x.1 * q ≤ x.2 * p)), { apply disjoint_filter.2 (λ x hx hpq hqp, _), have hxp : x.1 < p, from lt_of_le_of_lt (show x.1 ≤ p / 2, by simp only [*, lt_succ_iff, mem_Ico, mem_product] at *; tauto) (nat.div_lt_self hp.1.pos dec_trivial), have : (x.1 : zmod p) = 0, { simpa [hq0] using congr_arg (coe : ℕ → zmod p) (le_antisymm hpq hqp) }, apply_fun zmod.val at this, rw [val_cast_of_lt hxp, val_zero] at this, simpa only [this, nonpos_iff_eq_zero, mem_Ico, one_ne_zero, false_and, mem_product] using hx }, have hunion : ((Ico 1 (p / 2).succ).product (Ico 1 (q / 2).succ)).filter (λ x : ℕ × ℕ, x.2 * p ≤ x.1 * q) ∪ ((Ico 1 (p / 2).succ).product (Ico 1 (q / 2).succ)).filter (λ x : ℕ × ℕ, x.1 * q ≤ x.2 * p) = ((Ico 1 (p / 2).succ).product (Ico 1 (q / 2).succ)), from finset.ext (λ x, by have := le_total (x.2 * p) (x.1 * q); simp only [mem_union, mem_filter, mem_Ico, mem_product]; tauto), rw [sum_Ico_eq_card_lt, sum_Ico_eq_card_lt, hswap, ← card_disjoint_union hdisj, hunion, card_product], simp only [card_Ico, tsub_zero, succ_sub_succ_eq_sub] end end legendre_symbol end gauss_eisenstein
b82e3ffc406b476d75b709002c3a4aba7f32fa09
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/class8.lean
dd8fd8268c963aac9c987e62aa7d5f3e936c9256
[ "Apache-2.0" ]
permissive
soonhokong/lean
cb8aa01055ffe2af0fb99a16b4cda8463b882cd1
38607e3eb57f57f77c0ac114ad169e9e4262e24f
refs/heads/master
1,611,187,284,081
1,450,766,737,000
1,476,122,547,000
11,513,992
2
0
null
1,401,763,102,000
1,374,182,235,000
C++
UTF-8
Lean
false
false
1,043
lean
import logic data.prod open tactic prod inductive inh [class] (A : Type) : Prop := intro : A -> inh A attribute inh.intro [instance] theorem inh_elim {A : Type} {B : Prop} (H1 : inh A) (H2 : A → B) : B := inh.rec H2 H1 theorem inh_exists {A : Type} {P : A → Prop} (H : ∃x, P x) : inh A := obtain w Hw, from H, inh.intro w theorem inh_bool [instance] : inh Prop := inh.intro true theorem inh_fun [instance] {A B : Type} [H : inh B] : inh (A → B) := inh.rec (λb, inh.intro (λa : A, b)) H theorem pair_inh [instance] {A : Type} {B : Type} [H1 : inh A] [H2 : inh B] : inh (prod A B) := inh_elim H1 (λa, inh_elim H2 (λb, inh.intro (pair a b))) definition assump := eassumption tactic_hint assump theorem tst {A B : Type} (H : inh B) : inh (A → B → B) set_option trace.class_instances true theorem T1 {A B C D : Type} {P : C → Prop} (a : A) (H1 : inh B) (H2 : ∃x, P x) : inh ((A → A) × B × (D → C) × Prop) := have h1 : inh A, from inh.intro a, have h2 : inh C, from inh_exists H2, by exact _ reveal T1 print T1
10a32d4185c182bd2c3580b07917b1d499eee947
82b86ba2ae0d5aed0f01f49c46db5afec0eb2bd7
/stage0/src/Init/Data/Array.lean
c2a9a1b84f8b829bb06de904189e48a93d3a28d2
[ "Apache-2.0" ]
permissive
banksonian/lean4
3a2e6b0f1eb63aa56ff95b8d07b2f851072d54dc
78da6b3aa2840693eea354a41e89fc5b212a5011
refs/heads/master
1,673,703,624,165
1,605,123,551,000
1,605,123,551,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
289
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Gabriel Ebner -/ prelude import Init.Data.Array.Basic import Init.Data.Array.QSort import Init.Data.Array.BinSearch import Init.Data.Array.Macros
e269cfb5014048efa5535a0082b68203f7f9ca7c
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/data/ordmap/ordset.lean
ac50c1f974c6279f62e3a16279db533cde8f1cac
[ "Apache-2.0" ]
permissive
hikari0108/mathlib
b7ea2b7350497ab1a0b87a09d093ecc025a50dfa
a9e7d333b0cfd45f13a20f7b96b7d52e19fa2901
refs/heads/master
1,690,483,608,260
1,631,541,580,000
1,631,541,580,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
69,880
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import data.ordmap.ordnode import algebra.ordered_ring import data.nat.dist import tactic.linarith /-! # Verification of the `ordnode α` datatype This file proves the correctness of the operations in `data.ordmap.ordnode`. The public facing version is the type `ordset α`, which is a wrapper around `ordnode α` which includes the correctness invariant of the type, and it exposes parallel operations like `insert` as functions on `ordset` that do the same thing but bundle the correctness proofs. The advantage is that it is possible to, for example, prove that the result of `find` on `insert` will actually find the element, while `ordnode` cannot guarantee this if the input tree did not satisfy the type invariants. ## Main definitions * `ordset α`: A well formed set of values of type `α` ## Implementation notes The majority of this file is actually in the `ordnode` namespace, because we first have to prove the correctness of all the operations (and defining what correctness means here is actually somewhat subtle). So all the actual `ordset` operations are at the very end, once we have all the theorems. An `ordnode α` is an inductive type which describes a tree which stores the `size` at internal nodes. The correctness invariant of an `ordnode α` is: * `ordnode.sized t`: All internal `size` fields must match the actual measured size of the tree. (This is not hard to satisfy.) * `ordnode.balanced t`: Unless the tree has the form `()` or `((a) b)` or `(a (b))` (that is, nil or a single singleton subtree), the two subtrees must satisfy `size l ≤ δ * size r` and `size r ≤ δ * size l`, where `δ := 3` is a global parameter of the data structure (and this property must hold recursively at subtrees). This is why we say this is a "size balanced tree" data structure. * `ordnode.bounded lo hi t`: The members of the tree must be in strictly increasing order, meaning that if `a` is in the left subtree and `b` is the root, then `a ≤ b` and `¬ (b ≤ a)`. We enforce this using `ordnode.bounded` which includes also a global upper and lower bound. Because the `ordnode` file was ported from Haskell, the correctness invariants of some of the functions have not been spelled out, and some theorems like `ordnode.valid'.balance_l_aux` show very intricate assumptions on the sizes, which may need to be revised if it turns out some operations violate these assumptions, because there is a decent amount of slop in the actual data structure invariants, so the theorem will go through with multiple choices of assumption. **Note:** This file is incomplete, in the sense that the intent is to have verified versions and lemmas about all the definitions in `ordnode.lean`, but at the moment only a few operations are verified (the hard part should be out of the way, but still). Contributors are encouraged to pick this up and finish the job, if it appeals to you. ## Tags ordered map, ordered set, data structure, verified programming -/ variable {α : Type*} namespace ordnode /-! ### delta and ratio -/ theorem not_le_delta {s} (H : 1 ≤ s) : ¬ s ≤ delta * 0 := not_le_of_gt H theorem delta_lt_false {a b : ℕ} (h₁ : delta * a < b) (h₂ : delta * b < a) : false := not_le_of_lt (lt_trans ((mul_lt_mul_left dec_trivial).2 h₁) h₂) $ by simpa [mul_assoc] using nat.mul_le_mul_right a (dec_trivial : 1 ≤ delta * delta) /-! ### `singleton` -/ /-! ### `size` and `empty` -/ /-- O(n). Computes the actual number of elements in the set, ignoring the cached `size` field. -/ def real_size : ordnode α → ℕ | nil := 0 | (node _ l _ r) := real_size l + real_size r + 1 /-! ### `sized` -/ /-- The `sized` property asserts that all the `size` fields in nodes match the actual size of the respective subtrees. -/ def sized : ordnode α → Prop | nil := true | (node s l _ r) := s = size l + size r + 1 ∧ sized l ∧ sized r theorem sized.node' {l x r} (hl : @sized α l) (hr : sized r) : sized (node' l x r) := ⟨rfl, hl, hr⟩ theorem sized.eq_node' {s l x r} (h : @sized α (node s l x r)) : node s l x r = node' l x r := by rw h.1; refl theorem sized.size_eq {s l x r} (H : sized (@node α s l x r)) : size (@node α s l x r) = size l + size r + 1 := H.1 @[elab_as_eliminator] theorem sized.induction {t} (hl : @sized α t) {C : ordnode α → Prop} (H0 : C nil) (H1 : ∀ l x r, C l → C r → C (node' l x r)) : C t := begin induction t, {exact H0}, rw hl.eq_node', exact H1 _ _ _ (t_ih_l hl.2.1) (t_ih_r hl.2.2) end theorem size_eq_real_size : ∀ {t : ordnode α}, sized t → size t = real_size t | nil _ := rfl | (node s l x r) ⟨h₁, h₂, h₃⟩ := by rw [size, h₁, size_eq_real_size h₂, size_eq_real_size h₃]; refl @[simp] theorem sized.size_eq_zero {t : ordnode α} (ht : sized t) : size t = 0 ↔ t = nil := by cases t; [simp, simp [ht.1]] theorem sized.pos {s l x r} (h : sized (@node α s l x r)) : 0 < s := by rw h.1; apply nat.le_add_left /-! `dual` -/ theorem dual_dual : ∀ (t : ordnode α), dual (dual t) = t | nil := rfl | (node s l x r) := by rw [dual, dual, dual_dual, dual_dual] @[simp] theorem size_dual (t : ordnode α) : size (dual t) = size t := by cases t; refl /-! `balanced` -/ /-- The `balanced_sz l r` asserts that a hypothetical tree with children of sizes `l` and `r` is balanced: either `l ≤ δ * r` and `r ≤ δ * r`, or the tree is trivial with a singleton on one side and nothing on the other. -/ def balanced_sz (l r : ℕ) : Prop := l + r ≤ 1 ∨ (l ≤ delta * r ∧ r ≤ delta * l) instance balanced_sz.dec : decidable_rel balanced_sz := λ l r, or.decidable /-- The `balanced t` asserts that the tree `t` satisfies the balance invariants (at every level). -/ def balanced : ordnode α → Prop | nil := true | (node _ l _ r) := balanced_sz (size l) (size r) ∧ balanced l ∧ balanced r instance balanced.dec : decidable_pred (@balanced α) | t := by induction t; unfold balanced; resetI; apply_instance theorem balanced_sz.symm {l r : ℕ} : balanced_sz l r → balanced_sz r l := or.imp (by rw add_comm; exact id) and.symm theorem balanced_sz_zero {l : ℕ} : balanced_sz l 0 ↔ l ≤ 1 := by simp [balanced_sz] { contextual := tt } theorem balanced_sz_up {l r₁ r₂ : ℕ} (h₁ : r₁ ≤ r₂) (h₂ : l + r₂ ≤ 1 ∨ r₂ ≤ delta * l) (H : balanced_sz l r₁) : balanced_sz l r₂ := begin refine or_iff_not_imp_left.2 (λ h, _), refine ⟨_, h₂.resolve_left h⟩, cases H, { cases r₂, { cases h (le_trans (nat.add_le_add_left (nat.zero_le _) _) H) }, { exact le_trans (le_trans (nat.le_add_right _ _) H) (nat.le_add_left 1 _) } }, { exact le_trans H.1 (nat.mul_le_mul_left _ h₁) } end theorem balanced_sz_down {l r₁ r₂ : ℕ} (h₁ : r₁ ≤ r₂) (h₂ : l + r₂ ≤ 1 ∨ l ≤ delta * r₁) (H : balanced_sz l r₂) : balanced_sz l r₁ := have l + r₂ ≤ 1 → balanced_sz l r₁, from λ H, or.inl (le_trans (nat.add_le_add_left h₁ _) H), or.cases_on H this (λ H, or.cases_on h₂ this (λ h₂, or.inr ⟨h₂, le_trans h₁ H.2⟩)) theorem balanced.dual : ∀ {t : ordnode α}, balanced t → balanced (dual t) | nil h := ⟨⟩ | (node s l x r) ⟨b, bl, br⟩ := ⟨by rw [size_dual, size_dual]; exact b.symm, br.dual, bl.dual⟩ /-! ### `rotate` and `balance` -/ /-- Build a tree from three nodes, left associated (ignores the invariants). -/ def node3_l (l : ordnode α) (x : α) (m : ordnode α) (y : α) (r : ordnode α) : ordnode α := node' (node' l x m) y r /-- Build a tree from three nodes, right associated (ignores the invariants). -/ def node3_r (l : ordnode α) (x : α) (m : ordnode α) (y : α) (r : ordnode α) : ordnode α := node' l x (node' m y r) /-- Build a tree from three nodes, with `a () b -> (a ()) b` and `a (b c) d -> ((a b) (c d))`. -/ def node4_l : ordnode α → α → ordnode α → α → ordnode α → ordnode α | l x (node _ ml y mr) z r := node' (node' l x ml) y (node' mr z r) | l x nil z r := node3_l l x nil z r -- should not happen /-- Build a tree from three nodes, with `a () b -> a (() b)` and `a (b c) d -> ((a b) (c d))`. -/ def node4_r : ordnode α → α → ordnode α → α → ordnode α → ordnode α | l x (node _ ml y mr) z r := node' (node' l x ml) y (node' mr z r) | l x nil z r := node3_r l x nil z r -- should not happen /-- Concatenate two nodes, performing a left rotation `x (y z) -> ((x y) z)` if balance is upset. -/ def rotate_l : ordnode α → α → ordnode α → ordnode α | l x (node _ m y r) := if size m < ratio * size r then node3_l l x m y r else node4_l l x m y r | l x nil := node' l x nil -- should not happen /-- Concatenate two nodes, performing a right rotation `(x y) z -> (x (y z))` if balance is upset. -/ def rotate_r : ordnode α → α → ordnode α → ordnode α | (node _ l x m) y r := if size m < ratio * size l then node3_r l x m y r else node4_r l x m y r | nil y r := node' nil y r -- should not happen /-- A left balance operation. This will rebalance a concatenation, assuming the original nodes are not too far from balanced. -/ def balance_l' (l : ordnode α) (x : α) (r : ordnode α) : ordnode α := if size l + size r ≤ 1 then node' l x r else if size l > delta * size r then rotate_r l x r else node' l x r /-- A right balance operation. This will rebalance a concatenation, assuming the original nodes are not too far from balanced. -/ def balance_r' (l : ordnode α) (x : α) (r : ordnode α) : ordnode α := if size l + size r ≤ 1 then node' l x r else if size r > delta * size l then rotate_l l x r else node' l x r /-- The full balance operation. This is the same as `balance`, but with less manual inlining. It is somewhat easier to work with this version in proofs. -/ def balance' (l : ordnode α) (x : α) (r : ordnode α) : ordnode α := if size l + size r ≤ 1 then node' l x r else if size r > delta * size l then rotate_l l x r else if size l > delta * size r then rotate_r l x r else node' l x r theorem dual_node' (l : ordnode α) (x : α) (r : ordnode α) : dual (node' l x r) = node' (dual r) x (dual l) := by simp [node', add_comm] theorem dual_node3_l (l : ordnode α) (x : α) (m : ordnode α) (y : α) (r : ordnode α) : dual (node3_l l x m y r) = node3_r (dual r) y (dual m) x (dual l) := by simp [node3_l, node3_r, dual_node'] theorem dual_node3_r (l : ordnode α) (x : α) (m : ordnode α) (y : α) (r : ordnode α) : dual (node3_r l x m y r) = node3_l (dual r) y (dual m) x (dual l) := by simp [node3_l, node3_r, dual_node'] theorem dual_node4_l (l : ordnode α) (x : α) (m : ordnode α) (y : α) (r : ordnode α) : dual (node4_l l x m y r) = node4_r (dual r) y (dual m) x (dual l) := by cases m; simp [node4_l, node4_r, dual_node3_l, dual_node'] theorem dual_node4_r (l : ordnode α) (x : α) (m : ordnode α) (y : α) (r : ordnode α) : dual (node4_r l x m y r) = node4_l (dual r) y (dual m) x (dual l) := by cases m; simp [node4_l, node4_r, dual_node3_r, dual_node'] theorem dual_rotate_l (l : ordnode α) (x : α) (r : ordnode α) : dual (rotate_l l x r) = rotate_r (dual r) x (dual l) := by cases r; simp [rotate_l, rotate_r, dual_node']; split_ifs; simp [dual_node3_l, dual_node4_l] theorem dual_rotate_r (l : ordnode α) (x : α) (r : ordnode α) : dual (rotate_r l x r) = rotate_l (dual r) x (dual l) := by rw [← dual_dual (rotate_l _ _ _), dual_rotate_l, dual_dual, dual_dual] theorem dual_balance' (l : ordnode α) (x : α) (r : ordnode α) : dual (balance' l x r) = balance' (dual r) x (dual l) := begin simp [balance', add_comm], split_ifs; simp [dual_node', dual_rotate_l, dual_rotate_r], cases delta_lt_false h_1 h_2 end theorem dual_balance_l (l : ordnode α) (x : α) (r : ordnode α) : dual (balance_l l x r) = balance_r (dual r) x (dual l) := begin unfold balance_l balance_r, cases r with rs rl rx rr, { cases l with ls ll lx lr, {refl}, cases ll with lls lll llx llr; cases lr with lrs lrl lrx lrr; dsimp only [dual]; try {refl}, split_ifs; repeat {simp [h, add_comm]} }, { cases l with ls ll lx lr, {refl}, dsimp only [dual], split_ifs, swap, {simp [add_comm]}, cases ll with lls lll llx llr; cases lr with lrs lrl lrx lrr; try {refl}, dsimp only [dual], split_ifs; simp [h, add_comm] }, end theorem dual_balance_r (l : ordnode α) (x : α) (r : ordnode α) : dual (balance_r l x r) = balance_l (dual r) x (dual l) := by rw [← dual_dual (balance_l _ _ _), dual_balance_l, dual_dual, dual_dual] theorem sized.node3_l {l x m y r} (hl : @sized α l) (hm : sized m) (hr : sized r) : sized (node3_l l x m y r) := (hl.node' hm).node' hr theorem sized.node3_r {l x m y r} (hl : @sized α l) (hm : sized m) (hr : sized r) : sized (node3_r l x m y r) := hl.node' (hm.node' hr) theorem sized.node4_l {l x m y r} (hl : @sized α l) (hm : sized m) (hr : sized r) : sized (node4_l l x m y r) := by cases m; [exact (hl.node' hm).node' hr, exact (hl.node' hm.2.1).node' (hm.2.2.node' hr)] theorem node3_l_size {l x m y r} : size (@node3_l α l x m y r) = size l + size m + size r + 2 := by dsimp [node3_l, node', size]; rw add_right_comm _ 1 theorem node3_r_size {l x m y r} : size (@node3_r α l x m y r) = size l + size m + size r + 2 := by dsimp [node3_r, node', size]; rw [← add_assoc, ← add_assoc] theorem node4_l_size {l x m y r} (hm : sized m) : size (@node4_l α l x m y r) = size l + size m + size r + 2 := by cases m; simp [node4_l, node3_l, node', add_comm, add_left_comm]; [skip, simp [size, hm.1]]; rw [← add_assoc, ← bit0]; simp [add_comm, add_left_comm] theorem sized.dual : ∀ {t : ordnode α} (h : sized t), sized (dual t) | nil h := ⟨⟩ | (node s l x r) ⟨rfl, sl, sr⟩ := ⟨by simp [size_dual, add_comm], sized.dual sr, sized.dual sl⟩ theorem sized.dual_iff {t : ordnode α} : sized (dual t) ↔ sized t := ⟨λ h, by rw ← dual_dual t; exact h.dual, sized.dual⟩ theorem sized.rotate_l {l x r} (hl : @sized α l) (hr : sized r) : sized (rotate_l l x r) := begin cases r, {exact hl.node' hr}, rw rotate_l, split_ifs, { exact hl.node3_l hr.2.1 hr.2.2 }, { exact hl.node4_l hr.2.1 hr.2.2 } end theorem sized.rotate_r {l x r} (hl : @sized α l) (hr : sized r) : sized (rotate_r l x r) := sized.dual_iff.1 $ by rw dual_rotate_r; exact hr.dual.rotate_l hl.dual theorem sized.rotate_l_size {l x r} (hm : sized r) : size (@rotate_l α l x r) = size l + size r + 1 := begin cases r; simp [rotate_l], simp [size, hm.1, add_comm, add_left_comm], rw [← add_assoc, ← bit0], simp, split_ifs; simp [node3_l_size, node4_l_size hm.2.1, add_comm, add_left_comm] end theorem sized.rotate_r_size {l x r} (hl : sized l) : size (@rotate_r α l x r) = size l + size r + 1 := by rw [← size_dual, dual_rotate_r, hl.dual.rotate_l_size, size_dual, size_dual, add_comm (size l)] theorem sized.balance' {l x r} (hl : @sized α l) (hr : sized r) : sized (balance' l x r) := begin unfold balance', split_ifs, { exact hl.node' hr }, { exact hl.rotate_l hr }, { exact hl.rotate_r hr }, { exact hl.node' hr } end theorem size_balance' {l x r} (hl : @sized α l) (hr : sized r) : size (@balance' α l x r) = size l + size r + 1 := begin unfold balance', split_ifs, { refl }, { exact hr.rotate_l_size }, { exact hl.rotate_r_size }, { refl } end /-! ## `all`, `any`, `emem`, `amem` -/ theorem all.imp {P Q : α → Prop} (H : ∀ a, P a → Q a) : ∀ {t}, all P t → all Q t | nil h := ⟨⟩ | (node _ l x r) ⟨h₁, h₂, h₃⟩ := ⟨h₁.imp, H _ h₂, h₃.imp⟩ theorem any.imp {P Q : α → Prop} (H : ∀ a, P a → Q a) : ∀ {t}, any P t → any Q t | nil := id | (node _ l x r) := or.imp any.imp $ or.imp (H _) any.imp theorem all_singleton {P : α → Prop} {x : α} : all P (singleton x) ↔ P x := ⟨λ h, h.2.1, λ h, ⟨⟨⟩, h, ⟨⟩⟩⟩ theorem any_singleton {P : α → Prop} {x : α} : any P (singleton x) ↔ P x := ⟨by rintro (⟨⟨⟩⟩ | h | ⟨⟨⟩⟩); exact h, λ h, or.inr (or.inl h)⟩ theorem all_dual {P : α → Prop} : ∀ {t : ordnode α}, all P (dual t) ↔ all P t | nil := iff.rfl | (node s l x r) := ⟨λ ⟨hr, hx, hl⟩, ⟨all_dual.1 hl, hx, all_dual.1 hr⟩, λ ⟨hl, hx, hr⟩, ⟨all_dual.2 hr, hx, all_dual.2 hl⟩⟩ theorem all_iff_forall {P : α → Prop} : ∀ {t}, all P t ↔ ∀ x, emem x t → P x | nil := (iff_true_intro $ by rintro _ ⟨⟩).symm | (node _ l x r) := by simp [all, emem, all_iff_forall, any, or_imp_distrib, forall_and_distrib] theorem any_iff_exists {P : α → Prop} : ∀ {t}, any P t ↔ ∃ x, emem x t ∧ P x | nil := ⟨by rintro ⟨⟩, by rintro ⟨_, ⟨⟩, _⟩⟩ | (node _ l x r) := by simp [any, emem, any_iff_exists, or_and_distrib_right, exists_or_distrib] theorem emem_iff_all {x : α} {t} : emem x t ↔ ∀ P, all P t → P x := ⟨λ h P al, all_iff_forall.1 al _ h, λ H, H _ $ all_iff_forall.2 $ λ _, id⟩ theorem all_node' {P l x r} : @all α P (node' l x r) ↔ all P l ∧ P x ∧ all P r := iff.rfl theorem all_node3_l {P l x m y r} : @all α P (node3_l l x m y r) ↔ all P l ∧ P x ∧ all P m ∧ P y ∧ all P r := by simp [node3_l, all_node', and_assoc] theorem all_node3_r {P l x m y r} : @all α P (node3_r l x m y r) ↔ all P l ∧ P x ∧ all P m ∧ P y ∧ all P r := iff.rfl theorem all_node4_l {P l x m y r} : @all α P (node4_l l x m y r) ↔ all P l ∧ P x ∧ all P m ∧ P y ∧ all P r := by cases m; simp [node4_l, all_node', all, all_node3_l, and_assoc] theorem all_node4_r {P l x m y r} : @all α P (node4_r l x m y r) ↔ all P l ∧ P x ∧ all P m ∧ P y ∧ all P r := by cases m; simp [node4_r, all_node', all, all_node3_r, and_assoc] theorem all_rotate_l {P l x r} : @all α P (rotate_l l x r) ↔ all P l ∧ P x ∧ all P r := by cases r; simp [rotate_l, all_node']; split_ifs; simp [all_node3_l, all_node4_l, all] theorem all_rotate_r {P l x r} : @all α P (rotate_r l x r) ↔ all P l ∧ P x ∧ all P r := by rw [← all_dual, dual_rotate_r, all_rotate_l]; simp [all_dual, and_comm, and.left_comm] theorem all_balance' {P l x r} : @all α P (balance' l x r) ↔ all P l ∧ P x ∧ all P r := by rw balance'; split_ifs; simp [all_node', all_rotate_l, all_rotate_r] /-! ### `to_list` -/ theorem foldr_cons_eq_to_list : ∀ (t : ordnode α) (r : list α), t.foldr list.cons r = to_list t ++ r | nil r := rfl | (node _ l x r) r' := by rw [foldr, foldr_cons_eq_to_list, foldr_cons_eq_to_list, ← list.cons_append, ← list.append_assoc, ← foldr_cons_eq_to_list]; refl @[simp] theorem to_list_nil : to_list (@nil α) = [] := rfl @[simp] theorem to_list_node (s l x r) : to_list (@node α s l x r) = to_list l ++ x :: to_list r := by rw [to_list, foldr, foldr_cons_eq_to_list]; refl theorem emem_iff_mem_to_list {x : α} {t} : emem x t ↔ x ∈ to_list t := by unfold emem; induction t; simp [any, *, or_assoc] theorem length_to_list' : ∀ t : ordnode α, (to_list t).length = t.real_size | nil := rfl | (node _ l _ r) := by rw [to_list_node, list.length_append, list.length_cons, length_to_list', length_to_list']; refl theorem length_to_list {t : ordnode α} (h : sized t) : (to_list t).length = t.size := by rw [length_to_list', size_eq_real_size h] theorem equiv_iff {t₁ t₂ : ordnode α} (h₁ : sized t₁) (h₂ : sized t₂) : equiv t₁ t₂ ↔ to_list t₁ = to_list t₂ := and_iff_right_of_imp $ λ h, by rw [← length_to_list h₁, h, length_to_list h₂] /-! ### `mem` -/ theorem pos_size_of_mem [has_le α] [@decidable_rel α (≤)] {x : α} {t : ordnode α} (h : sized t) (h_mem : x ∈ t) : 0 < size t := by { cases t, { contradiction }, { simp [h.1] } } /-! ### `(find/erase/split)_(min/max)` -/ theorem find_min'_dual : ∀ t (x : α), find_min' (dual t) x = find_max' x t | nil x := rfl | (node _ l x r) _ := find_min'_dual r x theorem find_max'_dual (t) (x : α) : find_max' x (dual t) = find_min' t x := by rw [← find_min'_dual, dual_dual] theorem find_min_dual : ∀ t : ordnode α, find_min (dual t) = find_max t | nil := rfl | (node _ l x r) := congr_arg some $ find_min'_dual _ _ theorem find_max_dual (t : ordnode α) : find_max (dual t) = find_min t := by rw [← find_min_dual, dual_dual] theorem dual_erase_min : ∀ t : ordnode α, dual (erase_min t) = erase_max (dual t) | nil := rfl | (node _ nil x r) := rfl | (node _ l@(node _ _ _ _) x r) := by rw [erase_min, dual_balance_r, dual_erase_min, dual, dual, dual, erase_max] theorem dual_erase_max (t : ordnode α) : dual (erase_max t) = erase_min (dual t) := by rw [← dual_dual (erase_min _), dual_erase_min, dual_dual] theorem split_min_eq : ∀ s l (x : α) r, split_min' l x r = (find_min' l x, erase_min (node s l x r)) | _ nil x r := rfl | _ (node ls ll lx lr) x r := by rw [split_min', split_min_eq, split_min', find_min', erase_min] theorem split_max_eq : ∀ s l (x : α) r, split_max' l x r = (erase_max (node s l x r), find_max' x r) | _ l x nil := rfl | _ l x (node ls ll lx lr) := by rw [split_max', split_max_eq, split_max', find_max', erase_max] @[elab_as_eliminator] theorem find_min'_all {P : α → Prop} : ∀ t (x : α), all P t → P x → P (find_min' t x) | nil x h hx := hx | (node _ ll lx lr) x ⟨h₁, h₂, h₃⟩ hx := find_min'_all _ _ h₁ h₂ @[elab_as_eliminator] theorem find_max'_all {P : α → Prop} : ∀ (x : α) t, P x → all P t → P (find_max' x t) | x nil hx h := hx | x (node _ ll lx lr) hx ⟨h₁, h₂, h₃⟩ := find_max'_all _ _ h₂ h₃ /-! ### `glue` -/ /-! ### `merge` -/ @[simp] theorem merge_nil_left (t : ordnode α) : merge t nil = t := by cases t; refl @[simp] theorem merge_nil_right (t : ordnode α) : merge nil t = t := rfl @[simp] theorem merge_node {ls ll lx lr rs rl rx rr} : merge (@node α ls ll lx lr) (node rs rl rx rr) = if delta * ls < rs then balance_l (merge (node ls ll lx lr) rl) rx rr else if delta * rs < ls then balance_r ll lx (merge lr (node rs rl rx rr)) else glue (node ls ll lx lr) (node rs rl rx rr) := rfl /-! ### `insert` -/ theorem dual_insert [preorder α] [is_total α (≤)] [@decidable_rel α (≤)] (x : α) : ∀ t : ordnode α, dual (ordnode.insert x t) = @ordnode.insert (order_dual α) _ _ x (dual t) | nil := rfl | (node _ l y r) := begin rw [ordnode.insert, dual, ordnode.insert, order_dual.cmp_le_flip, ← cmp_le_swap x y], cases cmp_le x y; simp [ordering.swap, ordnode.insert, dual_balance_l, dual_balance_r, dual_insert] end /-! ### `balance` properties -/ theorem balance_eq_balance' {l x r} (hl : balanced l) (hr : balanced r) (sl : sized l) (sr : sized r) : @balance α l x r = balance' l x r := begin cases l with ls ll lx lr, { cases r with rs rl rx rr, { refl }, { rw sr.eq_node' at hr ⊢, cases rl with rls rll rlx rlr; cases rr with rrs rrl rrx rrr; dsimp [balance, balance'], { refl }, { have : size rrl = 0 ∧ size rrr = 0, { have := balanced_sz_zero.1 hr.1.symm, rwa [size, sr.2.2.1, nat.succ_le_succ_iff, nat.le_zero_iff, add_eq_zero_iff] at this }, cases sr.2.2.2.1.size_eq_zero.1 this.1, cases sr.2.2.2.2.size_eq_zero.1 this.2, have : rrs = 1 := sr.2.2.1, subst rrs, rw [if_neg, if_pos, rotate_l, if_pos], {refl}, all_goals {exact dec_trivial} }, { have : size rll = 0 ∧ size rlr = 0, { have := balanced_sz_zero.1 hr.1, rwa [size, sr.2.1.1, nat.succ_le_succ_iff, nat.le_zero_iff, add_eq_zero_iff] at this }, cases sr.2.1.2.1.size_eq_zero.1 this.1, cases sr.2.1.2.2.size_eq_zero.1 this.2, have : rls = 1 := sr.2.1.1, subst rls, rw [if_neg, if_pos, rotate_l, if_neg], {refl}, all_goals {exact dec_trivial} }, { symmetry, rw [zero_add, if_neg, if_pos, rotate_l], { split_ifs, { simp [node3_l, node', add_comm, add_left_comm] }, { simp [node4_l, node', sr.2.1.1, add_comm, add_left_comm] } }, { exact dec_trivial }, { exact not_le_of_gt (nat.succ_lt_succ (add_pos sr.2.1.pos sr.2.2.pos)) } } } }, { cases r with rs rl rx rr, { rw sl.eq_node' at hl ⊢, cases ll with lls lll llx llr; cases lr with lrs lrl lrx lrr; dsimp [balance, balance'], { refl }, { have : size lrl = 0 ∧ size lrr = 0, { have := balanced_sz_zero.1 hl.1.symm, rwa [size, sl.2.2.1, nat.succ_le_succ_iff, nat.le_zero_iff, add_eq_zero_iff] at this }, cases sl.2.2.2.1.size_eq_zero.1 this.1, cases sl.2.2.2.2.size_eq_zero.1 this.2, have : lrs = 1 := sl.2.2.1, subst lrs, rw [if_neg, if_neg, if_pos, rotate_r, if_neg], {refl}, all_goals {exact dec_trivial} }, { have : size lll = 0 ∧ size llr = 0, { have := balanced_sz_zero.1 hl.1, rwa [size, sl.2.1.1, nat.succ_le_succ_iff, nat.le_zero_iff, add_eq_zero_iff] at this }, cases sl.2.1.2.1.size_eq_zero.1 this.1, cases sl.2.1.2.2.size_eq_zero.1 this.2, have : lls = 1 := sl.2.1.1, subst lls, rw [if_neg, if_neg, if_pos, rotate_r, if_pos], {refl}, all_goals {exact dec_trivial} }, { symmetry, rw [if_neg, if_neg, if_pos, rotate_r], { split_ifs, { simp [node3_r, node', add_comm, add_left_comm] }, { simp [node4_r, node', sl.2.2.1, add_comm, add_left_comm] } }, { exact dec_trivial }, { exact dec_trivial }, { exact not_le_of_gt (nat.succ_lt_succ (add_pos sl.2.1.pos sl.2.2.pos)) } } }, { simp [balance, balance'], symmetry, rw [if_neg], { split_ifs, { have rd : delta ≤ size rl + size rr, { have := lt_of_le_of_lt (nat.mul_le_mul_left _ sl.pos) h, rwa [sr.1, nat.lt_succ_iff] at this }, cases rl with rls rll rlx rlr, { rw [size, zero_add] at rd, exact absurd (le_trans rd (balanced_sz_zero.1 hr.1.symm)) dec_trivial }, cases rr with rrs rrl rrx rrr, { exact absurd (le_trans rd (balanced_sz_zero.1 hr.1)) dec_trivial }, dsimp [rotate_l], split_ifs, { simp [node3_l, node', sr.1, add_comm, add_left_comm] }, { simp [node4_l, node', sr.1, sr.2.1.1, add_comm, add_left_comm] } }, { have ld : delta ≤ size ll + size lr, { have := lt_of_le_of_lt (nat.mul_le_mul_left _ sr.pos) h_1, rwa [sl.1, nat.lt_succ_iff] at this }, cases ll with lls lll llx llr, { rw [size, zero_add] at ld, exact absurd (le_trans ld (balanced_sz_zero.1 hl.1.symm)) dec_trivial }, cases lr with lrs lrl lrx lrr, { exact absurd (le_trans ld (balanced_sz_zero.1 hl.1)) dec_trivial }, dsimp [rotate_r], split_ifs, { simp [node3_r, node', sl.1, add_comm, add_left_comm] }, { simp [node4_r, node', sl.1, sl.2.2.1, add_comm, add_left_comm] } }, { simp [node'] } }, { exact not_le_of_gt (add_le_add sl.pos sr.pos : 2 ≤ ls + rs) } } } end theorem balance_l_eq_balance {l x r} (sl : sized l) (sr : sized r) (H1 : size l = 0 → size r ≤ 1) (H2 : 1 ≤ size l → 1 ≤ size r → size r ≤ delta * size l) : @balance_l α l x r = balance l x r := begin cases r with rs rl rx rr, { refl }, { cases l with ls ll lx lr, { have : size rl = 0 ∧ size rr = 0, { have := H1 rfl, rwa [size, sr.1, nat.succ_le_succ_iff, nat.le_zero_iff, add_eq_zero_iff] at this }, cases sr.2.1.size_eq_zero.1 this.1, cases sr.2.2.size_eq_zero.1 this.2, rw sr.eq_node', refl }, { replace H2 : ¬ rs > delta * ls := not_lt_of_le (H2 sl.pos sr.pos), simp [balance_l, balance, H2]; split_ifs; simp [add_comm] } } end /-- `raised n m` means `m` is either equal or one up from `n`. -/ def raised (n m : ℕ) : Prop := m = n ∨ m = n + 1 theorem raised_iff {n m} : raised n m ↔ n ≤ m ∧ m ≤ n + 1 := begin split, rintro (rfl | rfl), { exact ⟨le_refl _, nat.le_succ _⟩ }, { exact ⟨nat.le_succ _, le_refl _⟩ }, { rintro ⟨h₁, h₂⟩, rcases eq_or_lt_of_le h₁ with rfl | h₁, { exact or.inl rfl }, { exact or.inr (le_antisymm h₂ h₁) } } end theorem raised.dist_le {n m} (H : raised n m) : nat.dist n m ≤ 1 := by cases raised_iff.1 H with H1 H2; rwa [nat.dist_eq_sub_of_le H1, nat.sub_le_left_iff_le_add] theorem raised.dist_le' {n m} (H : raised n m) : nat.dist m n ≤ 1 := by rw nat.dist_comm; exact H.dist_le theorem raised.add_left (k) {n m} (H : raised n m) : raised (k + n) (k + m) := begin rcases H with rfl | rfl, { exact or.inl rfl }, { exact or.inr rfl } end theorem raised.add_right (k) {n m} (H : raised n m) : raised (n + k) (m + k) := by rw [add_comm, add_comm m]; exact H.add_left _ theorem raised.right {l x₁ x₂ r₁ r₂} (H : raised (size r₁) (size r₂)) : raised (size (@node' α l x₁ r₁)) (size (@node' α l x₂ r₂)) := begin dsimp [node', size], generalize_hyp : size r₂ = m at H ⊢, rcases H with rfl | rfl, { exact or.inl rfl }, { exact or.inr rfl } end theorem balance_l_eq_balance' {l x r} (hl : balanced l) (hr : balanced r) (sl : sized l) (sr : sized r) (H : (∃ l', raised l' (size l) ∧ balanced_sz l' (size r)) ∨ (∃ r', raised (size r) r' ∧ balanced_sz (size l) r')) : @balance_l α l x r = balance' l x r := begin rw [← balance_eq_balance' hl hr sl sr, balance_l_eq_balance sl sr], { intro l0, rw l0 at H, rcases H with ⟨_, ⟨⟨⟩⟩|⟨⟨⟩⟩, H⟩ | ⟨r', e, H⟩, { exact balanced_sz_zero.1 H.symm }, exact le_trans (raised_iff.1 e).1 (balanced_sz_zero.1 H.symm) }, { intros l1 r1, rcases H with ⟨l', e, H | ⟨H₁, H₂⟩⟩ | ⟨r', e, H | ⟨H₁, H₂⟩⟩, { exact le_trans (le_trans (nat.le_add_left _ _) H) (mul_pos dec_trivial l1 : (0:ℕ)<_) }, { exact le_trans H₂ (nat.mul_le_mul_left _ (raised_iff.1 e).1) }, { cases raised_iff.1 e, unfold delta, linarith }, { exact le_trans (raised_iff.1 e).1 H₂ } } end theorem balance_sz_dual {l r} (H : (∃ l', raised (@size α l) l' ∧ balanced_sz l' (@size α r)) ∨ ∃ r', raised r' (size r) ∧ balanced_sz (size l) r') : (∃ l', raised l' (size (dual r)) ∧ balanced_sz l' (size (dual l))) ∨ ∃ r', raised (size (dual l)) r' ∧ balanced_sz (size (dual r)) r' := begin rw [size_dual, size_dual], exact H.symm.imp (Exists.imp $ λ _, and.imp_right balanced_sz.symm) (Exists.imp $ λ _, and.imp_right balanced_sz.symm) end theorem size_balance_l {l x r} (hl : balanced l) (hr : balanced r) (sl : sized l) (sr : sized r) (H : (∃ l', raised l' (size l) ∧ balanced_sz l' (size r)) ∨ (∃ r', raised (size r) r' ∧ balanced_sz (size l) r')) : size (@balance_l α l x r) = size l + size r + 1 := by rw [balance_l_eq_balance' hl hr sl sr H, size_balance' sl sr] theorem all_balance_l {P l x r} (hl : balanced l) (hr : balanced r) (sl : sized l) (sr : sized r) (H : (∃ l', raised l' (size l) ∧ balanced_sz l' (size r)) ∨ (∃ r', raised (size r) r' ∧ balanced_sz (size l) r')) : all P (@balance_l α l x r) ↔ all P l ∧ P x ∧ all P r := by rw [balance_l_eq_balance' hl hr sl sr H, all_balance'] theorem balance_r_eq_balance' {l x r} (hl : balanced l) (hr : balanced r) (sl : sized l) (sr : sized r) (H : (∃ l', raised (size l) l' ∧ balanced_sz l' (size r)) ∨ (∃ r', raised r' (size r) ∧ balanced_sz (size l) r')) : @balance_r α l x r = balance' l x r := by rw [← dual_dual (balance_r l x r), dual_balance_r, balance_l_eq_balance' hr.dual hl.dual sr.dual sl.dual (balance_sz_dual H), ← dual_balance', dual_dual] theorem size_balance_r {l x r} (hl : balanced l) (hr : balanced r) (sl : sized l) (sr : sized r) (H : (∃ l', raised (size l) l' ∧ balanced_sz l' (size r)) ∨ (∃ r', raised r' (size r) ∧ balanced_sz (size l) r')) : size (@balance_r α l x r) = size l + size r + 1 := by rw [balance_r_eq_balance' hl hr sl sr H, size_balance' sl sr] theorem all_balance_r {P l x r} (hl : balanced l) (hr : balanced r) (sl : sized l) (sr : sized r) (H : (∃ l', raised (size l) l' ∧ balanced_sz l' (size r)) ∨ (∃ r', raised r' (size r) ∧ balanced_sz (size l) r')) : all P (@balance_r α l x r) ↔ all P l ∧ P x ∧ all P r := by rw [balance_r_eq_balance' hl hr sl sr H, all_balance'] /-! ### `bounded` -/ section variable [preorder α] /-- `bounded t lo hi` says that every element `x ∈ t` is in the range `lo < x < hi`, and also this property holds recursively in subtrees, making the full tree a BST. The bounds can be set to `lo = ⊥` and `hi = ⊤` if we care only about the internal ordering constraints. -/ def bounded : ordnode α → with_bot α → with_top α → Prop | nil (some a) (some b) := a < b | nil _ _ := true | (node _ l x r) o₁ o₂ := bounded l o₁ ↑x ∧ bounded r ↑x o₂ theorem bounded.dual : ∀ {t : ordnode α} {o₁ o₂} (h : bounded t o₁ o₂), @bounded (order_dual α) _ (dual t) o₂ o₁ | nil o₁ o₂ h := by cases o₁; cases o₂; try {trivial}; exact h | (node s l x r) _ _ ⟨ol, or⟩ := ⟨or.dual, ol.dual⟩ theorem bounded.dual_iff {t : ordnode α} {o₁ o₂} : bounded t o₁ o₂ ↔ @bounded (order_dual α) _ (dual t) o₂ o₁ := ⟨bounded.dual, λ h, by have := bounded.dual h; rwa [dual_dual, order_dual.preorder.dual_dual] at this⟩ theorem bounded.weak_left : ∀ {t : ordnode α} {o₁ o₂}, bounded t o₁ o₂ → bounded t ⊥ o₂ | nil o₁ o₂ h := by cases o₂; try {trivial}; exact h | (node s l x r) _ _ ⟨ol, or⟩ := ⟨ol.weak_left, or⟩ theorem bounded.weak_right : ∀ {t : ordnode α} {o₁ o₂}, bounded t o₁ o₂ → bounded t o₁ ⊤ | nil o₁ o₂ h := by cases o₁; try {trivial}; exact h | (node s l x r) _ _ ⟨ol, or⟩ := ⟨ol, or.weak_right⟩ theorem bounded.weak {t : ordnode α} {o₁ o₂} (h : bounded t o₁ o₂) : bounded t ⊥ ⊤ := h.weak_left.weak_right theorem bounded.mono_left {x y : α} (xy : x ≤ y) : ∀ {t : ordnode α} {o}, bounded t ↑y o → bounded t ↑x o | nil none h := ⟨⟩ | nil (some z) h := lt_of_le_of_lt xy h | (node s l z r) o ⟨ol, or⟩ := ⟨ol.mono_left, or⟩ theorem bounded.mono_right {x y : α} (xy : x ≤ y) : ∀ {t : ordnode α} {o}, bounded t o ↑x → bounded t o ↑y | nil none h := ⟨⟩ | nil (some z) h := lt_of_lt_of_le h xy | (node s l z r) o ⟨ol, or⟩ := ⟨ol, or.mono_right⟩ theorem bounded.to_lt : ∀ {t : ordnode α} {x y : α}, bounded t x y → x < y | nil x y h := h | (node _ l y r) x z ⟨h₁, h₂⟩ := lt_trans h₁.to_lt h₂.to_lt theorem bounded.to_nil {t : ordnode α} : ∀ {o₁ o₂}, bounded t o₁ o₂ → bounded nil o₁ o₂ | none _ h := ⟨⟩ | (some _) none h := ⟨⟩ | (some x) (some y) h := h.to_lt theorem bounded.trans_left {t₁ t₂ : ordnode α} {x : α} : ∀ {o₁ o₂}, bounded t₁ o₁ ↑x → bounded t₂ ↑x o₂ → bounded t₂ o₁ o₂ | none o₂ h₁ h₂ := h₂.weak_left | (some y) o₂ h₁ h₂ := h₂.mono_left (le_of_lt h₁.to_lt) theorem bounded.trans_right {t₁ t₂ : ordnode α} {x : α} : ∀ {o₁ o₂}, bounded t₁ o₁ ↑x → bounded t₂ ↑x o₂ → bounded t₁ o₁ o₂ | o₁ none h₁ h₂ := h₁.weak_right | o₁ (some y) h₁ h₂ := h₁.mono_right (le_of_lt h₂.to_lt) theorem bounded.mem_lt : ∀ {t o} {x : α}, bounded t o ↑x → all (< x) t | nil o x _ := ⟨⟩ | (node _ l y r) o x ⟨h₁, h₂⟩ := ⟨h₁.mem_lt.imp (λ z h, lt_trans h h₂.to_lt), h₂.to_lt, h₂.mem_lt⟩ theorem bounded.mem_gt : ∀ {t o} {x : α}, bounded t ↑x o → all (> x) t | nil o x _ := ⟨⟩ | (node _ l y r) o x ⟨h₁, h₂⟩ := ⟨h₁.mem_gt, h₁.to_lt, h₂.mem_gt.imp (λ z, lt_trans h₁.to_lt)⟩ theorem bounded.of_lt : ∀ {t o₁ o₂} {x : α}, bounded t o₁ o₂ → bounded nil o₁ ↑x → all (< x) t → bounded t o₁ ↑x | nil o₁ o₂ x _ hn _ := hn | (node _ l y r) o₁ o₂ x ⟨h₁, h₂⟩ hn ⟨al₁, al₂, al₃⟩ := ⟨h₁, h₂.of_lt al₂ al₃⟩ theorem bounded.of_gt : ∀ {t o₁ o₂} {x : α}, bounded t o₁ o₂ → bounded nil ↑x o₂ → all (> x) t → bounded t ↑x o₂ | nil o₁ o₂ x _ hn _ := hn | (node _ l y r) o₁ o₂ x ⟨h₁, h₂⟩ hn ⟨al₁, al₂, al₃⟩ := ⟨h₁.of_gt al₂ al₁, h₂⟩ theorem bounded.to_sep {t₁ t₂ o₁ o₂} {x : α} (h₁ : bounded t₁ o₁ ↑x) (h₂ : bounded t₂ ↑x o₂) : t₁.all (λ y, t₂.all (λ z : α, y < z)) := h₁.mem_lt.imp $ λ y yx, h₂.mem_gt.imp $ λ z xz, lt_trans yx xz end /-! ### `valid` -/ section variable [preorder α] /-- The validity predicate for an `ordnode` subtree. This asserts that the `size` fields are correct, the tree is balanced, and the elements of the tree are organized according to the ordering. This version of `valid` also puts all elements in the tree in the interval `(lo, hi)`. -/ structure valid' (lo : with_bot α) (t : ordnode α) (hi : with_top α) : Prop := (ord : t.bounded lo hi) (sz : t.sized) (bal : t.balanced) /-- The validity predicate for an `ordnode` subtree. This asserts that the `size` fields are correct, the tree is balanced, and the elements of the tree are organized according to the ordering. -/ def valid (t : ordnode α) : Prop := valid' ⊥ t ⊤ theorem valid'.mono_left {x y : α} (xy : x ≤ y) {t : ordnode α} {o} (h : valid' ↑y t o) : valid' ↑x t o := ⟨h.1.mono_left xy, h.2, h.3⟩ theorem valid'.mono_right {x y : α} (xy : x ≤ y) {t : ordnode α} {o} (h : valid' o t ↑x) : valid' o t ↑y := ⟨h.1.mono_right xy, h.2, h.3⟩ theorem valid'.trans_left {t₁ t₂ : ordnode α} {x : α} {o₁ o₂} (h : bounded t₁ o₁ ↑x) (H : valid' ↑x t₂ o₂) : valid' o₁ t₂ o₂ := ⟨h.trans_left H.1, H.2, H.3⟩ theorem valid'.trans_right {t₁ t₂ : ordnode α} {x : α} {o₁ o₂} (H : valid' o₁ t₁ ↑x) (h : bounded t₂ ↑x o₂) : valid' o₁ t₁ o₂ := ⟨H.1.trans_right h, H.2, H.3⟩ theorem valid'.of_lt {t : ordnode α} {x : α} {o₁ o₂} (H : valid' o₁ t o₂) (h₁ : bounded nil o₁ ↑x) (h₂ : all (< x) t) : valid' o₁ t ↑x := ⟨H.1.of_lt h₁ h₂, H.2, H.3⟩ theorem valid'.of_gt {t : ordnode α} {x : α} {o₁ o₂} (H : valid' o₁ t o₂) (h₁ : bounded nil ↑x o₂) (h₂ : all (> x) t) : valid' ↑x t o₂ := ⟨H.1.of_gt h₁ h₂, H.2, H.3⟩ theorem valid'.valid {t o₁ o₂} (h : @valid' α _ o₁ t o₂) : valid t := ⟨h.1.weak, h.2, h.3⟩ theorem valid'_nil {o₁ o₂} (h : bounded nil o₁ o₂) : valid' o₁ (@nil α) o₂ := ⟨h, ⟨⟩, ⟨⟩⟩ theorem valid_nil : valid (@nil α) := valid'_nil ⟨⟩ theorem valid'.node {s l x r o₁ o₂} (hl : valid' o₁ l ↑x) (hr : valid' ↑x r o₂) (H : balanced_sz (size l) (size r)) (hs : s = size l + size r + 1) : valid' o₁ (@node α s l x r) o₂ := ⟨⟨hl.1, hr.1⟩, ⟨hs, hl.2, hr.2⟩, ⟨H, hl.3, hr.3⟩⟩ theorem valid'.dual : ∀ {t : ordnode α} {o₁ o₂} (h : valid' o₁ t o₂), @valid' (order_dual α) _ o₂ (dual t) o₁ | nil o₁ o₂ h := valid'_nil h.1.dual | (node s l x r) o₁ o₂ ⟨⟨ol, or⟩, ⟨rfl, sl, sr⟩, ⟨b, bl, br⟩⟩ := let ⟨ol', sl', bl'⟩ := valid'.dual ⟨ol, sl, bl⟩, ⟨or', sr', br'⟩ := valid'.dual ⟨or, sr, br⟩ in ⟨⟨or', ol'⟩, ⟨by simp [size_dual, add_comm], sr', sl'⟩, ⟨by rw [size_dual, size_dual]; exact b.symm, br', bl'⟩⟩ theorem valid'.dual_iff {t : ordnode α} {o₁ o₂} : valid' o₁ t o₂ ↔ @valid' (order_dual α) _ o₂ (dual t) o₁ := ⟨valid'.dual, λ h, by have := valid'.dual h; rwa [dual_dual, order_dual.preorder.dual_dual] at this⟩ theorem valid.dual {t : ordnode α} : valid t → @valid (order_dual α) _ (dual t) := valid'.dual theorem valid.dual_iff {t : ordnode α} : valid t ↔ @valid (order_dual α) _ (dual t) := valid'.dual_iff theorem valid'.left {s l x r o₁ o₂} (H : valid' o₁ (@node α s l x r) o₂) : valid' o₁ l x := ⟨H.1.1, H.2.2.1, H.3.2.1⟩ theorem valid'.right {s l x r o₁ o₂} (H : valid' o₁ (@node α s l x r) o₂) : valid' ↑x r o₂ := ⟨H.1.2, H.2.2.2, H.3.2.2⟩ theorem valid.left {s l x r} (H : valid (@node α s l x r)) : valid l := H.left.valid theorem valid.right {s l x r} (H : valid (@node α s l x r)) : valid r := H.right.valid theorem valid.size_eq {s l x r} (H : valid (@node α s l x r)) : size (@node α s l x r) = size l + size r + 1 := H.2.1 theorem valid'.node' {l x r o₁ o₂} (hl : valid' o₁ l ↑x) (hr : valid' ↑x r o₂) (H : balanced_sz (size l) (size r)) : valid' o₁ (@node' α l x r) o₂ := hl.node hr H rfl theorem valid'_singleton {x : α} {o₁ o₂} (h₁ : bounded nil o₁ ↑x) (h₂ : bounded nil ↑x o₂) : valid' o₁ (singleton x : ordnode α) o₂ := (valid'_nil h₁).node (valid'_nil h₂) (or.inl zero_le_one) rfl theorem valid_singleton {x : α} : valid (singleton x : ordnode α) := valid'_singleton ⟨⟩ ⟨⟩ theorem valid'.node3_l {l x m y r o₁ o₂} (hl : valid' o₁ l ↑x) (hm : valid' ↑x m ↑y) (hr : valid' ↑y r o₂) (H1 : balanced_sz (size l) (size m)) (H2 : balanced_sz (size l + size m + 1) (size r)) : valid' o₁ (@node3_l α l x m y r) o₂ := (hl.node' hm H1).node' hr H2 theorem valid'.node3_r {l x m y r o₁ o₂} (hl : valid' o₁ l ↑x) (hm : valid' ↑x m ↑y) (hr : valid' ↑y r o₂) (H1 : balanced_sz (size l) (size m + size r + 1)) (H2 : balanced_sz (size m) (size r)) : valid' o₁ (@node3_r α l x m y r) o₂ := hl.node' (hm.node' hr H2) H1 theorem valid'.node4_l_lemma₁ {a b c d : ℕ} (lr₂ : 3 * (b + c + 1 + d) ≤ 16 * a + 9) (mr₂ : b + c + 1 ≤ 3 * d) (mm₁ : b ≤ 3 * c) : b < 3 * a + 1 := by linarith theorem valid'.node4_l_lemma₂ {b c d : ℕ} (mr₂ : b + c + 1 ≤ 3 * d) : c ≤ 3 * d := by linarith theorem valid'.node4_l_lemma₃ {b c d : ℕ} (mr₁ : 2 * d ≤ b + c + 1) (mm₁ : b ≤ 3 * c) : d ≤ 3 * c := by linarith theorem valid'.node4_l_lemma₄ {a b c d : ℕ} (lr₁ : 3 * a ≤ b + c + 1 + d) (mr₂ : b + c + 1 ≤ 3 * d) (mm₁ : b ≤ 3 * c) : a + b + 1 ≤ 3 * (c + d + 1) := by linarith theorem valid'.node4_l_lemma₅ {a b c d : ℕ} (lr₂ : 3 * (b + c + 1 + d) ≤ 16 * a + 9) (mr₁ : 2 * d ≤ b + c + 1) (mm₂ : c ≤ 3 * b) : c + d + 1 ≤ 3 * (a + b + 1) := by linarith theorem valid'.node4_l {l x m y r o₁ o₂} (hl : valid' o₁ l ↑x) (hm : valid' ↑x m ↑y) (hr : valid' ↑y r o₂) (Hm : 0 < size m) (H : (size l = 0 ∧ size m = 1 ∧ size r ≤ 1) ∨ (0 < size l ∧ ratio * size r ≤ size m ∧ delta * size l ≤ size m + size r ∧ 3 * (size m + size r) ≤ 16 * size l + 9 ∧ size m ≤ delta * size r)) : valid' o₁ (@node4_l α l x m y r) o₂ := begin cases m with s ml z mr, {cases Hm}, suffices : balanced_sz (size l) (size ml) ∧ balanced_sz (size mr) (size r) ∧ balanced_sz (size l + size ml + 1) (size mr + size r + 1), from (valid'.node' (hl.node' hm.left this.1) (hm.right.node' hr this.2.1) this.2.2), rcases H with ⟨l0, m1, r0⟩ | ⟨l0, mr₁, lr₁, lr₂, mr₂⟩, { rw [hm.2.size_eq, nat.succ_inj', add_eq_zero_iff] at m1, rw [l0, m1.1, m1.2], rcases size r with _|_|_; exact dec_trivial }, { cases nat.eq_zero_or_pos (size r) with r0 r0, { rw r0 at mr₂, cases not_le_of_lt Hm mr₂ }, rw [hm.2.size_eq] at lr₁ lr₂ mr₁ mr₂, by_cases mm : size ml + size mr ≤ 1, { have r1 := le_antisymm ((mul_le_mul_left dec_trivial).1 (le_trans mr₁ (nat.succ_le_succ mm) : _ ≤ ratio * 1)) r0, rw [r1, add_assoc] at lr₁, have l1 := le_antisymm ((mul_le_mul_left dec_trivial).1 (le_trans lr₁ (add_le_add_right mm 2) : _ ≤ delta * 1)) l0, rw [l1, r1], cases size ml; cases size mr, { exact dec_trivial }, { rw zero_add at mm, rcases mm with _|⟨_,⟨⟩⟩, exact dec_trivial }, { rcases mm with _|⟨_,⟨⟩⟩, exact dec_trivial }, { rw nat.succ_add at mm, rcases mm with _|⟨_,⟨⟩⟩ } }, rcases hm.3.1.resolve_left mm with ⟨mm₁, mm₂⟩, cases nat.eq_zero_or_pos (size ml) with ml0 ml0, { rw [ml0, mul_zero, nat.le_zero_iff] at mm₂, rw [ml0, mm₂] at mm, cases mm dec_trivial }, cases nat.eq_zero_or_pos (size mr) with mr0 mr0, { rw [mr0, mul_zero, nat.le_zero_iff] at mm₁, rw [mr0, mm₁] at mm, cases mm dec_trivial }, have : 2 * size l ≤ size ml + size mr + 1, { have := nat.mul_le_mul_left _ lr₁, rw [mul_left_comm, mul_add] at this, have := le_trans this (add_le_add_left mr₁ _), rw [← nat.succ_mul] at this, exact (mul_le_mul_left dec_trivial).1 this }, refine ⟨or.inr ⟨_, _⟩, or.inr ⟨_, _⟩, or.inr ⟨_, _⟩⟩, { refine (mul_le_mul_left dec_trivial).1 (le_trans this _), rw [two_mul, nat.succ_le_iff], refine add_lt_add_of_lt_of_le _ mm₂, simpa using (mul_lt_mul_right ml0).2 (dec_trivial:1<3) }, { exact nat.le_of_lt_succ (valid'.node4_l_lemma₁ lr₂ mr₂ mm₁) }, { exact valid'.node4_l_lemma₂ mr₂ }, { exact valid'.node4_l_lemma₃ mr₁ mm₁ }, { exact valid'.node4_l_lemma₄ lr₁ mr₂ mm₁ }, { exact valid'.node4_l_lemma₅ lr₂ mr₁ mm₂ } } end theorem valid'.rotate_l_lemma₁ {a b c : ℕ} (H2 : 3 * a ≤ b + c) (hb₂ : c ≤ 3 * b) : a ≤ 3 * b := by linarith theorem valid'.rotate_l_lemma₂ {a b c : ℕ} (H3 : 2 * (b + c) ≤ 9 * a + 3) (h : b < 2 * c) : b < 3 * a + 1 := by linarith theorem valid'.rotate_l_lemma₃ {a b c : ℕ} (H2 : 3 * a ≤ b + c) (h : b < 2 * c) : a + b < 3 * c := by linarith theorem valid'.rotate_l_lemma₄ {a b : ℕ} (H3 : 2 * b ≤ 9 * a + 3) : 3 * b ≤ 16 * a + 9 := by linarith theorem valid'.rotate_l {l x r o₁ o₂} (hl : valid' o₁ l ↑x) (hr : valid' ↑x r o₂) (H1 : ¬ size l + size r ≤ 1) (H2 : delta * size l < size r) (H3 : 2 * size r ≤ 9 * size l + 5 ∨ size r ≤ 3) : valid' o₁ (@rotate_l α l x r) o₂ := begin cases r with rs rl rx rr, {cases H2}, rw [hr.2.size_eq, nat.lt_succ_iff] at H2, rw [hr.2.size_eq] at H3, replace H3 : 2 * (size rl + size rr) ≤ 9 * size l + 3 ∨ size rl + size rr ≤ 2 := H3.imp (@nat.le_of_add_le_add_right 2 _ _) nat.le_of_succ_le_succ, have H3_0 : size l = 0 → size rl + size rr ≤ 2, { intro l0, rw l0 at H3, exact (or_iff_right_of_imp $ by exact λ h, (mul_le_mul_left dec_trivial).1 (le_trans h dec_trivial)).1 H3 }, have H3p : size l > 0 → 2 * (size rl + size rr) ≤ 9 * size l + 3 := λ l0 : 1 ≤ size l, (or_iff_left_of_imp $ by intro; linarith).1 H3, have ablem : ∀ {a b : ℕ}, 1 ≤ a → a + b ≤ 2 → b ≤ 1, {intros, linarith}, have hlp : size l > 0 → ¬ size rl + size rr ≤ 1 := λ l0 hb, absurd (le_trans (le_trans (nat.mul_le_mul_left _ l0) H2) hb) dec_trivial, rw rotate_l, split_ifs, { have rr0 : size rr > 0 := (mul_lt_mul_left dec_trivial).1 (lt_of_le_of_lt (nat.zero_le _) h : ratio * 0 < _), suffices : balanced_sz (size l) (size rl) ∧ balanced_sz (size l + size rl + 1) (size rr), { exact hl.node3_l hr.left hr.right this.1 this.2 }, cases nat.eq_zero_or_pos (size l) with l0 l0, { rw l0, replace H3 := H3_0 l0, have := hr.3.1, cases nat.eq_zero_or_pos (size rl) with rl0 rl0, { rw rl0 at this ⊢, rw le_antisymm (balanced_sz_zero.1 this.symm) rr0, exact dec_trivial }, have rr1 : size rr = 1 := le_antisymm (ablem rl0 H3) rr0, rw add_comm at H3, rw [rr1, show size rl = 1, from le_antisymm (ablem rr0 H3) rl0], exact dec_trivial }, replace H3 := H3p l0, rcases hr.3.1.resolve_left (hlp l0) with ⟨hb₁, hb₂⟩, cases nat.eq_zero_or_pos (size rl) with rl0 rl0, { rw rl0 at hb₂, cases not_le_of_gt rr0 hb₂ }, cases eq_or_lt_of_le (show 1 ≤ size rr, from rr0) with rr1 rr1, { rw [← rr1] at h H2 ⊢, have : size rl = 1 := le_antisymm (nat.lt_succ_iff.1 h) rl0, rw this at H2, exact absurd (le_trans (nat.mul_le_mul_left _ l0) H2) dec_trivial }, refine ⟨or.inr ⟨_, _⟩, or.inr ⟨_, _⟩⟩, { exact valid'.rotate_l_lemma₁ H2 hb₂ }, { exact nat.le_of_lt_succ (valid'.rotate_l_lemma₂ H3 h) }, { exact valid'.rotate_l_lemma₃ H2 h }, { exact le_trans hb₂ (nat.mul_le_mul_left _ $ le_trans (nat.le_add_left _ _) (nat.le_add_right _ _)) } }, { cases nat.eq_zero_or_pos (size rl) with rl0 rl0, { rw [rl0, not_lt, nat.le_zero_iff, nat.mul_eq_zero] at h, replace h := h.resolve_left dec_trivial, rw [rl0, h, nat.le_zero_iff, nat.mul_eq_zero] at H2, rw [hr.2.size_eq, rl0, h, H2.resolve_left dec_trivial] at H1, cases H1 dec_trivial }, refine hl.node4_l hr.left hr.right rl0 _, cases nat.eq_zero_or_pos (size l) with l0 l0, { replace H3 := H3_0 l0, cases nat.eq_zero_or_pos (size rr) with rr0 rr0, { have := hr.3.1, rw rr0 at this, exact or.inl ⟨l0, le_antisymm (balanced_sz_zero.1 this) rl0, rr0.symm ▸ zero_le_one⟩ }, exact or.inl ⟨l0, le_antisymm (ablem rr0 $ by rwa add_comm) rl0, ablem rl0 H3⟩ }, exact or.inr ⟨l0, not_lt.1 h, H2, valid'.rotate_l_lemma₄ (H3p l0), (hr.3.1.resolve_left (hlp l0)).1⟩ } end theorem valid'.rotate_r {l x r o₁ o₂} (hl : valid' o₁ l ↑x) (hr : valid' ↑x r o₂) (H1 : ¬ size l + size r ≤ 1) (H2 : delta * size r < size l) (H3 : 2 * size l ≤ 9 * size r + 5 ∨ size l ≤ 3) : valid' o₁ (@rotate_r α l x r) o₂ := begin refine valid'.dual_iff.2 _, rw dual_rotate_r, refine hr.dual.rotate_l hl.dual _ _ _, { rwa [size_dual, size_dual, add_comm] }, { rwa [size_dual, size_dual] }, { rwa [size_dual, size_dual] } end theorem valid'.balance'_aux {l x r o₁ o₂} (hl : valid' o₁ l ↑x) (hr : valid' ↑x r o₂) (H₁ : 2 * @size α r ≤ 9 * size l + 5 ∨ size r ≤ 3) (H₂ : 2 * @size α l ≤ 9 * size r + 5 ∨ size l ≤ 3) : valid' o₁ (@balance' α l x r) o₂ := begin rw balance', split_ifs, { exact hl.node' hr (or.inl h) }, { exact hl.rotate_l hr h h_1 H₁ }, { exact hl.rotate_r hr h h_2 H₂ }, { exact hl.node' hr (or.inr ⟨not_lt.1 h_2, not_lt.1 h_1⟩) } end theorem valid'.balance'_lemma {α l l' r r'} (H1 : balanced_sz l' r') (H2 : nat.dist (@size α l) l' ≤ 1 ∧ size r = r' ∨ nat.dist (size r) r' ≤ 1 ∧ size l = l') : 2 * @size α r ≤ 9 * size l + 5 ∨ size r ≤ 3 := begin suffices : @size α r ≤ 3 * (size l + 1), { cases nat.eq_zero_or_pos (size l) with l0 l0, { apply or.inr, rwa l0 at this }, change 1 ≤ _ at l0, apply or.inl, linarith }, rcases H2 with ⟨hl, rfl⟩ | ⟨hr, rfl⟩; rcases H1 with h | ⟨h₁, h₂⟩, { exact le_trans (nat.le_add_left _ _) (le_trans h (nat.le_add_left _ _)) }, { exact le_trans h₂ (nat.mul_le_mul_left _ $ le_trans (nat.dist_tri_right _ _) (nat.add_le_add_left hl _)) }, { exact le_trans (nat.dist_tri_left' _ _) (le_trans (add_le_add hr (le_trans (nat.le_add_left _ _) h)) dec_trivial) }, { rw nat.mul_succ, exact le_trans (nat.dist_tri_right' _ _) (add_le_add h₂ (le_trans hr dec_trivial)) }, end theorem valid'.balance' {l x r o₁ o₂} (hl : valid' o₁ l ↑x) (hr : valid' ↑x r o₂) (H : ∃ l' r', balanced_sz l' r' ∧ (nat.dist (size l) l' ≤ 1 ∧ size r = r' ∨ nat.dist (size r) r' ≤ 1 ∧ size l = l')) : valid' o₁ (@balance' α l x r) o₂ := let ⟨l', r', H1, H2⟩ := H in valid'.balance'_aux hl hr (valid'.balance'_lemma H1 H2) (valid'.balance'_lemma H1.symm H2.symm) theorem valid'.balance {l x r o₁ o₂} (hl : valid' o₁ l ↑x) (hr : valid' ↑x r o₂) (H : ∃ l' r', balanced_sz l' r' ∧ (nat.dist (size l) l' ≤ 1 ∧ size r = r' ∨ nat.dist (size r) r' ≤ 1 ∧ size l = l')) : valid' o₁ (@balance α l x r) o₂ := by rw balance_eq_balance' hl.3 hr.3 hl.2 hr.2; exact hl.balance' hr H theorem valid'.balance_l_aux {l x r o₁ o₂} (hl : valid' o₁ l ↑x) (hr : valid' ↑x r o₂) (H₁ : size l = 0 → size r ≤ 1) (H₂ : 1 ≤ size l → 1 ≤ size r → size r ≤ delta * size l) (H₃ : 2 * @size α l ≤ 9 * size r + 5 ∨ size l ≤ 3) : valid' o₁ (@balance_l α l x r) o₂ := begin rw [balance_l_eq_balance hl.2 hr.2 H₁ H₂, balance_eq_balance' hl.3 hr.3 hl.2 hr.2], refine hl.balance'_aux hr (or.inl _) H₃, cases nat.eq_zero_or_pos (size r) with r0 r0, { rw r0, exact nat.zero_le _ }, cases nat.eq_zero_or_pos (size l) with l0 l0, { rw l0, exact le_trans (nat.mul_le_mul_left _ (H₁ l0)) dec_trivial }, replace H₂ : _ ≤ 3 * _ := H₂ l0 r0, linarith end theorem valid'.balance_l {l x r o₁ o₂} (hl : valid' o₁ l ↑x) (hr : valid' ↑x r o₂) (H : (∃ l', raised l' (size l) ∧ balanced_sz l' (size r)) ∨ (∃ r', raised (size r) r' ∧ balanced_sz (size l) r')) : valid' o₁ (@balance_l α l x r) o₂ := begin rw balance_l_eq_balance' hl.3 hr.3 hl.2 hr.2 H, refine hl.balance' hr _, rcases H with ⟨l', e, H⟩ | ⟨r', e, H⟩, { exact ⟨_, _, H, or.inl ⟨e.dist_le', rfl⟩⟩ }, { exact ⟨_, _, H, or.inr ⟨e.dist_le, rfl⟩⟩ }, end theorem valid'.balance_r_aux {l x r o₁ o₂} (hl : valid' o₁ l ↑x) (hr : valid' ↑x r o₂) (H₁ : size r = 0 → size l ≤ 1) (H₂ : 1 ≤ size r → 1 ≤ size l → size l ≤ delta * size r) (H₃ : 2 * @size α r ≤ 9 * size l + 5 ∨ size r ≤ 3) : valid' o₁ (@balance_r α l x r) o₂ := begin rw [valid'.dual_iff, dual_balance_r], have := hr.dual.balance_l_aux hl.dual, rw [size_dual, size_dual] at this, exact this H₁ H₂ H₃ end theorem valid'.balance_r {l x r o₁ o₂} (hl : valid' o₁ l ↑x) (hr : valid' ↑x r o₂) (H : (∃ l', raised (size l) l' ∧ balanced_sz l' (size r)) ∨ (∃ r', raised r' (size r) ∧ balanced_sz (size l) r')) : valid' o₁ (@balance_r α l x r) o₂ := by rw [valid'.dual_iff, dual_balance_r]; exact hr.dual.balance_l hl.dual (balance_sz_dual H) theorem valid'.erase_max_aux {s l x r o₁ o₂} (H : valid' o₁ (node s l x r) o₂) : valid' o₁ (@erase_max α (node' l x r)) ↑(find_max' x r) ∧ size (node' l x r) = size (erase_max (node' l x r)) + 1 := begin have := H.2.eq_node', rw this at H, clear this, induction r with rs rl rx rr IHrl IHrr generalizing l x o₁, { exact ⟨H.left, rfl⟩ }, have := H.2.2.2.eq_node', rw this at H ⊢, rcases IHrr H.right with ⟨h, e⟩, refine ⟨valid'.balance_l H.left h (or.inr ⟨_, or.inr e, H.3.1⟩), _⟩, rw [erase_max, size_balance_l H.3.2.1 h.3 H.2.2.1 h.2 (or.inr ⟨_, or.inr e, H.3.1⟩)], rw [size, e], refl end theorem valid'.erase_min_aux {s l x r o₁ o₂} (H : valid' o₁ (node s l x r) o₂) : valid' ↑(find_min' l x) (@erase_min α (node' l x r)) o₂ ∧ size (node' l x r) = size (erase_min (node' l x r)) + 1 := by have := H.dual.erase_max_aux; rwa [← dual_node', size_dual, ← dual_erase_min, size_dual, ← valid'.dual_iff, find_max'_dual] at this theorem erase_min.valid : ∀ {t} (h : @valid α _ t), valid (erase_min t) | nil _ := valid_nil | (node _ l x r) h := by rw h.2.eq_node'; exact h.erase_min_aux.1.valid theorem erase_max.valid {t} (h : @valid α _ t) : valid (erase_max t) := by rw [valid.dual_iff, dual_erase_max]; exact erase_min.valid h.dual theorem valid'.glue_aux {l r o₁ o₂} (hl : valid' o₁ l o₂) (hr : valid' o₁ r o₂) (sep : l.all (λ x, r.all (λ y, x < y))) (bal : balanced_sz (size l) (size r)) : valid' o₁ (@glue α l r) o₂ ∧ size (glue l r) = size l + size r := begin cases l with ls ll lx lr, {exact ⟨hr, (zero_add _).symm⟩ }, cases r with rs rl rx rr, {exact ⟨hl, rfl⟩ }, dsimp [glue], split_ifs, { rw [split_max_eq, glue], cases valid'.erase_max_aux hl with v e, suffices H, refine ⟨valid'.balance_r v (hr.of_gt _ _) H, _⟩, { refine find_max'_all lx lr hl.1.2.to_nil (sep.2.2.imp _), exact λ x h, hr.1.2.to_nil.mono_left (le_of_lt h.2.1) }, { exact @find_max'_all _ (λ a, all (> a) (node rs rl rx rr)) lx lr sep.2.1 sep.2.2 }, { rw [size_balance_r v.3 hr.3 v.2 hr.2 H, add_right_comm, ← e, hl.2.1], refl }, { refine or.inl ⟨_, or.inr e, _⟩, rwa hl.2.eq_node' at bal } }, { rw [split_min_eq, glue], cases valid'.erase_min_aux hr with v e, suffices H, refine ⟨valid'.balance_l (hl.of_lt _ _) v H, _⟩, { refine @find_min'_all _ (λ a, bounded nil o₁ ↑a) rl rx (sep.2.1.1.imp _) hr.1.1.to_nil, exact λ y h, hl.1.1.to_nil.mono_right (le_of_lt h) }, { exact @find_min'_all _ (λ a, all (< a) (node ls ll lx lr)) rl rx (all_iff_forall.2 $ λ x hx, sep.imp $ λ y hy, all_iff_forall.1 hy.1 _ hx) (sep.imp $ λ y hy, hy.2.1) }, { rw [size_balance_l hl.3 v.3 hl.2 v.2 H, add_assoc, ← e, hr.2.1], refl }, { refine or.inr ⟨_, or.inr e, _⟩, rwa hr.2.eq_node' at bal } }, end theorem valid'.glue {l x r o₁ o₂} (hl : valid' o₁ l ↑(x:α)) (hr : valid' ↑x r o₂) : balanced_sz (size l) (size r) → valid' o₁ (@glue α l r) o₂ ∧ size (@glue α l r) = size l + size r := valid'.glue_aux (hl.trans_right hr.1) (hr.trans_left hl.1) (hl.1.to_sep hr.1) theorem valid'.merge_lemma {a b c : ℕ} (h₁ : 3 * a < b + c + 1) (h₂ : b ≤ 3 * c) : 2 * (a + b) ≤ 9 * c + 5 := by linarith theorem valid'.merge_aux₁ {o₁ o₂ ls ll lx lr rs rl rx rr t} (hl : valid' o₁ (@node α ls ll lx lr) o₂) (hr : valid' o₁ (node rs rl rx rr) o₂) (h : delta * ls < rs) (v : valid' o₁ t ↑rx) (e : size t = ls + size rl) : valid' o₁ (balance_l t rx rr) o₂ ∧ size (balance_l t rx rr) = ls + rs := begin rw hl.2.1 at e, rw [hl.2.1, hr.2.1, delta] at h, rcases hr.3.1 with H|⟨hr₁, hr₂⟩, {linarith}, suffices H₂, suffices H₁, refine ⟨valid'.balance_l_aux v hr.right H₁ H₂ _, _⟩, { rw e, exact or.inl (valid'.merge_lemma h hr₁) }, { rw [balance_l_eq_balance v.2 hr.2.2.2 H₁ H₂, balance_eq_balance' v.3 hr.3.2.2 v.2 hr.2.2.2, size_balance' v.2 hr.2.2.2, e, hl.2.1, hr.2.1], simp [add_comm, add_left_comm] }, { rw [e, add_right_comm], rintro ⟨⟩ }, { intros _ h₁, rw e, unfold delta at hr₂ ⊢, linarith } end theorem valid'.merge_aux {l r o₁ o₂} (hl : valid' o₁ l o₂) (hr : valid' o₁ r o₂) (sep : l.all (λ x, r.all (λ y, x < y))) : valid' o₁ (@merge α l r) o₂ ∧ size (merge l r) = size l + size r := begin induction l with ls ll lx lr IHll IHlr generalizing o₁ o₂ r, { exact ⟨hr, (zero_add _).symm⟩ }, induction r with rs rl rx rr IHrl IHrr generalizing o₁ o₂, { exact ⟨hl, rfl⟩ }, rw [merge_node], split_ifs, { cases IHrl (sep.imp $ λ x h, h.1) (hl.of_lt hr.1.1.to_nil $ sep.imp $ λ x h, h.2.1) hr.left with v e, exact valid'.merge_aux₁ hl hr h v e }, { cases IHlr hl.right (hr.of_gt hl.1.2.to_nil sep.2.1) sep.2.2 with v e, have := valid'.merge_aux₁ hr.dual hl.dual h_1 v.dual, rw [size_dual, add_comm, size_dual, ← dual_balance_r, ← valid'.dual_iff, size_dual, add_comm rs] at this, exact this e }, { refine valid'.glue_aux hl hr sep (or.inr ⟨not_lt.1 h_1, not_lt.1 h⟩) } end theorem valid.merge {l r} (hl : valid l) (hr : valid r) (sep : l.all (λ x, r.all (λ y, x < y))) : valid (@merge α l r) := (valid'.merge_aux hl hr sep).1 theorem insert_with.valid_aux [is_total α (≤)] [@decidable_rel α (≤)] (f : α → α) (x : α) (hf : ∀ y, x ≤ y ∧ y ≤ x → x ≤ f y ∧ f y ≤ x) : ∀ {t o₁ o₂}, valid' o₁ t o₂ → bounded nil o₁ ↑x → bounded nil ↑x o₂ → valid' o₁ (insert_with f x t) o₂ ∧ raised (size t) (size (insert_with f x t)) | nil o₁ o₂ _ bl br := ⟨valid'_singleton bl br, or.inr rfl⟩ | (node sz l y r) o₁ o₂ h bl br := begin rw [insert_with, cmp_le], split_ifs; rw [insert_with], { rcases h with ⟨⟨lx, xr⟩, hs, hb⟩, rcases hf _ ⟨h_1, h_2⟩ with ⟨xf, fx⟩, refine ⟨⟨⟨lx.mono_right (le_trans h_2 xf), xr.mono_left (le_trans fx h_1)⟩, hs, hb⟩, or.inl rfl⟩ }, { rcases insert_with.valid_aux h.left bl (lt_of_le_not_le h_1 h_2) with ⟨vl, e⟩, suffices H, { refine ⟨vl.balance_l h.right H, _⟩, rw [size_balance_l vl.3 h.3.2.2 vl.2 h.2.2.2 H, h.2.size_eq], refine (e.add_right _).add_right _ }, { exact or.inl ⟨_, e, h.3.1⟩ } }, { have : y < x := lt_of_le_not_le ((total_of (≤) _ _).resolve_left h_1) h_1, rcases insert_with.valid_aux h.right this br with ⟨vr, e⟩, suffices H, { refine ⟨h.left.balance_r vr H, _⟩, rw [size_balance_r h.3.2.1 vr.3 h.2.2.1 vr.2 H, h.2.size_eq], refine (e.add_left _).add_right _ }, { exact or.inr ⟨_, e, h.3.1⟩ } }, end theorem insert_with.valid [is_total α (≤)] [@decidable_rel α (≤)] (f : α → α) (x : α) (hf : ∀ y, x ≤ y ∧ y ≤ x → x ≤ f y ∧ f y ≤ x) {t} (h : valid t) : valid (insert_with f x t) := (insert_with.valid_aux _ _ hf h ⟨⟩ ⟨⟩).1 theorem insert_eq_insert_with [@decidable_rel α (≤)] (x : α) : ∀ t, ordnode.insert x t = insert_with (λ _, x) x t | nil := rfl | (node _ l y r) := by unfold ordnode.insert insert_with; cases cmp_le x y; unfold ordnode.insert insert_with; simp [insert_eq_insert_with] theorem insert.valid [is_total α (≤)] [@decidable_rel α (≤)] (x : α) {t} (h : valid t) : valid (ordnode.insert x t) := by rw insert_eq_insert_with; exact insert_with.valid _ _ (λ _ _, ⟨le_refl _, le_refl _⟩) h theorem insert'_eq_insert_with [@decidable_rel α (≤)] (x : α) : ∀ t, insert' x t = insert_with id x t | nil := rfl | (node _ l y r) := by unfold insert' insert_with; cases cmp_le x y; unfold insert' insert_with; simp [insert'_eq_insert_with] theorem insert'.valid [is_total α (≤)] [@decidable_rel α (≤)] (x : α) {t} (h : valid t) : valid (insert' x t) := by rw insert'_eq_insert_with; exact insert_with.valid _ _ (λ _, id) h theorem valid'.map_aux {β} [preorder β] {f : α → β} (f_strict_mono : strict_mono f) {t a₁ a₂} (h : valid' a₁ t a₂) : valid' (option.map f a₁) (map f t) (option.map f a₂) ∧ (map f t).size = t.size := begin induction t generalizing a₁ a₂, { simp [map], apply valid'_nil, cases a₁, { trivial }, cases a₂, { trivial }, simp [bounded], exact f_strict_mono h.ord }, { have t_ih_l' := t_ih_l h.left, have t_ih_r' := t_ih_r h.right, clear t_ih_l t_ih_r, cases t_ih_l' with t_l_valid t_l_size, cases t_ih_r' with t_r_valid t_r_size, simp [map], split, { exact and.intro t_l_valid.ord t_r_valid.ord }, { repeat { split }, { rw [t_l_size, t_r_size], exact h.sz.1 }, { exact t_l_valid.sz }, { exact t_r_valid.sz } }, { repeat { split }, { rw [t_l_size, t_r_size], exact h.bal.1 }, { exact t_l_valid.bal }, { exact t_r_valid.bal } } }, end theorem map.valid {β} [preorder β] {f : α → β} (f_strict_mono : strict_mono f) {t} (h : valid t) : valid (map f t) := (valid'.map_aux f_strict_mono h).1 theorem valid'.erase_aux [@decidable_rel α (≤)] (x : α) {t a₁ a₂} (h : valid' a₁ t a₂) : valid' a₁ (erase x t) a₂ ∧ raised (erase x t).size t.size := begin induction t generalizing a₁ a₂, { simp [erase, raised], exact h }, { simp [erase], have t_ih_l' := t_ih_l h.left, have t_ih_r' := t_ih_r h.right, clear t_ih_l t_ih_r, cases t_ih_l' with t_l_valid t_l_size, cases t_ih_r' with t_r_valid t_r_size, cases (cmp_le x t_x); simp [erase._match_1]; rw h.sz.1, { suffices h_balanceable, split, { exact valid'.balance_r t_l_valid h.right h_balanceable }, { rw size_balance_r t_l_valid.bal h.right.bal t_l_valid.sz h.right.sz h_balanceable, repeat { apply raised.add_right }, exact t_l_size }, { left, existsi t_l.size, exact (and.intro t_l_size h.bal.1) } }, { have h_glue := valid'.glue h.left h.right h.bal.1, cases h_glue with h_glue_valid h_glue_sized, split, { exact h_glue_valid }, { right, rw h_glue_sized } }, { suffices h_balanceable, split, { exact valid'.balance_l h.left t_r_valid h_balanceable }, { rw size_balance_l h.left.bal t_r_valid.bal h.left.sz t_r_valid.sz h_balanceable, apply raised.add_right, apply raised.add_left, exact t_r_size }, { right, existsi t_r.size, exact (and.intro t_r_size h.bal.1) } } }, end theorem erase.valid [@decidable_rel α (≤)] (x : α) {t} (h : valid t) : valid (erase x t) := (valid'.erase_aux x h).1 theorem size_erase_of_mem [@decidable_rel α (≤)] {x : α} {t a₁ a₂} (h : valid' a₁ t a₂) (h_mem : x ∈ t) : size (erase x t) = size t - 1 := begin induction t generalizing a₁ a₂ h h_mem, { contradiction }, { have t_ih_l' := t_ih_l h.left, have t_ih_r' := t_ih_r h.right, clear t_ih_l t_ih_r, unfold has_mem.mem mem at h_mem, unfold erase, cases (cmp_le x t_x); simp [mem._match_1] at h_mem; simp [erase._match_1], { have t_ih_l := t_ih_l' h_mem, clear t_ih_l' t_ih_r', have t_l_h := valid'.erase_aux x h.left, cases t_l_h with t_l_valid t_l_size, rw size_balance_r t_l_valid.bal h.right.bal t_l_valid.sz h.right.sz (or.inl (exists.intro t_l.size (and.intro t_l_size h.bal.1))), rw [t_ih_l, h.sz.1], have h_pos_t_l_size := pos_size_of_mem h.left.sz h_mem, cases t_l.size with t_l_size, { cases h_pos_t_l_size }, simp [nat.succ_add] }, { rw [(valid'.glue h.left h.right h.bal.1).2, h.sz.1], refl }, { have t_ih_r := t_ih_r' h_mem, clear t_ih_l' t_ih_r', have t_r_h := valid'.erase_aux x h.right, cases t_r_h with t_r_valid t_r_size, rw size_balance_l h.left.bal t_r_valid.bal h.left.sz t_r_valid.sz (or.inr (exists.intro t_r.size (and.intro t_r_size h.bal.1))), rw [t_ih_r, h.sz.1], have h_pos_t_r_size := pos_size_of_mem h.right.sz h_mem, cases t_r.size with t_r_size, { cases h_pos_t_r_size }, simp [nat.succ_add, nat.add_succ] } }, end end end ordnode /-- An `ordset α` is a finite set of values, represented as a tree. The operations on this type maintain that the tree is balanced and correctly stores subtree sizes at each level. The correctness property of the tree is baked into the type, so all operations on this type are correct by construction. -/ def ordset (α : Type*) [preorder α] := {t : ordnode α // t.valid} namespace ordset open ordnode variable [preorder α] /-- O(1). The empty set. -/ def nil : ordset α := ⟨nil, ⟨⟩, ⟨⟩, ⟨⟩⟩ /-- O(1). Get the size of the set. -/ def size (s : ordset α) : ℕ := s.1.size /-- O(1). Construct a singleton set containing value `a`. -/ protected def singleton (a : α) : ordset α := ⟨singleton a, valid_singleton⟩ instance : has_emptyc (ordset α) := ⟨nil⟩ instance : inhabited (ordset α) := ⟨nil⟩ instance : has_singleton α (ordset α) := ⟨ordset.singleton⟩ /-- O(1). Is the set empty? -/ def empty (s : ordset α) : Prop := s = ∅ theorem empty_iff {s : ordset α} : s = ∅ ↔ s.1.empty := ⟨λ h, by cases h; exact rfl, λ h, by cases s; cases s_val; [exact rfl, cases h]⟩ instance : decidable_pred (@empty α _) := λ s, decidable_of_iff' _ empty_iff /-- O(log n). Insert an element into the set, preserving balance and the BST property. If an equivalent element is already in the set, this replaces it. -/ protected def insert [is_total α (≤)] [@decidable_rel α (≤)] (x : α) (s : ordset α) : ordset α := ⟨ordnode.insert x s.1, insert.valid _ s.2⟩ instance [is_total α (≤)] [@decidable_rel α (≤)] : has_insert α (ordset α) := ⟨ordset.insert⟩ /-- O(log n). Insert an element into the set, preserving balance and the BST property. If an equivalent element is already in the set, the set is returned as is. -/ def insert' [is_total α (≤)] [@decidable_rel α (≤)] (x : α) (s : ordset α) : ordset α := ⟨insert' x s.1, insert'.valid _ s.2⟩ section variables [@decidable_rel α (≤)] /-- O(log n). Does the set contain the element `x`? That is, is there an element that is equivalent to `x` in the order? -/ def mem (x : α) (s : ordset α) : bool := x ∈ s.val /-- O(log n). Retrieve an element in the set that is equivalent to `x` in the order, if it exists. -/ def find (x : α) (s : ordset α) : option α := ordnode.find x s.val instance : has_mem α (ordset α) := ⟨λ x s, mem x s⟩ instance mem.decidable (x : α) (s : ordset α) : decidable (x ∈ s) := bool.decidable_eq _ _ theorem pos_size_of_mem {x : α} {t : ordset α} (h_mem : x ∈ t) : 0 < size t := begin simp [has_mem.mem, mem] at h_mem, apply ordnode.pos_size_of_mem t.property.sz h_mem, end end /-- O(log n). Remove an element from the set equivalent to `x`. Does nothing if there is no such element. -/ def erase [@decidable_rel α (≤)] (x : α) (s : ordset α) : ordset α := ⟨ordnode.erase x s.val, ordnode.erase.valid x s.property⟩ /-- O(n). Map a function across a tree, without changing the structure. -/ def map {β} [preorder β] (f : α → β) (f_strict_mono : strict_mono f) (s : ordset α) : ordset β := ⟨ordnode.map f s.val, ordnode.map.valid f_strict_mono s.property⟩ end ordset
b33928e4392b392a282f2bc3d7767b9589bdfdcb
853df553b1d6ca524e3f0a79aedd32dde5d27ec3
/src/data/equiv/mul_add.lean
93d0514e262928b85cd1bcacf7c969408c071461
[ "Apache-2.0" ]
permissive
DanielFabian/mathlib
efc3a50b5dde303c59eeb6353ef4c35a345d7112
f520d07eba0c852e96fe26da71d85bf6d40fcc2a
refs/heads/master
1,668,739,922,971
1,595,201,756,000
1,595,201,756,000
279,469,476
0
0
null
1,594,696,604,000
1,594,696,604,000
null
UTF-8
Lean
false
false
13,436
lean
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Callum Sutton, Yury Kudryashov -/ import data.equiv.basic import deprecated.group /-! # Multiplicative and additive equivs In this file we define two extensions of `equiv` called `add_equiv` and `mul_equiv`, which are datatypes representing isomorphisms of `add_monoid`s/`add_group`s and `monoid`s/`group`s. We also introduce the corresponding groups of automorphisms `add_aut` and `mul_aut`. ## Notations The extended equivs all have coercions to functions, and the coercions are the canonical notation when treating the isomorphisms as maps. ## Implementation notes The fields for `mul_equiv`, `add_equiv` now avoid the unbundled `is_mul_hom` and `is_add_hom`, as these are deprecated. Definition of multiplication in the groups of automorphisms agrees with function composition, multiplication in `equiv.perm`, and multiplication in `category_theory.End`, not with `category_theory.comp`. ## Tags equiv, mul_equiv, add_equiv, mul_aut, add_aut -/ variables {A : Type*} {B : Type*} {M : Type*} {N : Type*} {P : Type*} {G : Type*} {H : Type*} set_option old_structure_cmd true /-- add_equiv α β is the type of an equiv α ≃ β which preserves addition. -/ structure add_equiv (A B : Type*) [has_add A] [has_add B] extends A ≃ B := (map_add' : ∀ x y : A, to_fun (x + y) = to_fun x + to_fun y) /-- `mul_equiv α β` is the type of an equiv `α ≃ β` which preserves multiplication. -/ @[to_additive] structure mul_equiv (M N : Type*) [has_mul M] [has_mul N] extends M ≃ N := (map_mul' : ∀ x y : M, to_fun (x * y) = to_fun x * to_fun y) infix ` ≃* `:25 := mul_equiv infix ` ≃+ `:25 := add_equiv namespace mul_equiv @[to_additive] instance [has_mul M] [has_mul N] : has_coe_to_fun (M ≃* N) := ⟨_, mul_equiv.to_fun⟩ variables [has_mul M] [has_mul N] [has_mul P] @[simp, to_additive] lemma to_fun_apply {f : M ≃* N} {m : M} : f.to_fun m = f m := rfl /-- A multiplicative isomorphism preserves multiplication (canonical form). -/ @[to_additive] lemma map_mul (f : M ≃* N) : ∀ x y, f (x * y) = f x * f y := f.map_mul' /-- A multiplicative isomorphism preserves multiplication (deprecated). -/ @[to_additive] instance (h : M ≃* N) : is_mul_hom h := ⟨h.map_mul⟩ /-- Makes a multiplicative isomorphism from a bijection which preserves multiplication. -/ @[to_additive] def mk' (f : M ≃ N) (h : ∀ x y, f (x * y) = f x * f y) : M ≃* N := ⟨f.1, f.2, f.3, f.4, h⟩ /-- The identity map is a multiplicative isomorphism. -/ @[refl, to_additive] def refl (M : Type*) [has_mul M] : M ≃* M := { map_mul' := λ _ _, rfl, ..equiv.refl _} /-- The inverse of an isomorphism is an isomorphism. -/ @[symm, to_additive] def symm (h : M ≃* N) : N ≃* M := { map_mul' := λ n₁ n₂, h.left_inv.injective begin show h.to_equiv (h.to_equiv.symm (n₁ * n₂)) = h ((h.to_equiv.symm n₁) * (h.to_equiv.symm n₂)), rw h.map_mul, show _ = h.to_equiv (_) * h.to_equiv (_), rw [h.to_equiv.apply_symm_apply, h.to_equiv.apply_symm_apply, h.to_equiv.apply_symm_apply], end, ..h.to_equiv.symm} @[simp, to_additive] theorem to_equiv_symm (f : M ≃* N) : f.symm.to_equiv = f.to_equiv.symm := rfl @[simp, to_additive] theorem coe_mk (f : M → N) (g h₁ h₂ h₃) : ⇑(mul_equiv.mk f g h₁ h₂ h₃) = f := rfl @[simp, to_additive] theorem coe_symm_mk (f : M → N) (g h₁ h₂ h₃) : ⇑(mul_equiv.mk f g h₁ h₂ h₃).symm = g := rfl /-- Transitivity of multiplication-preserving isomorphisms -/ @[trans, to_additive] def trans (h1 : M ≃* N) (h2 : N ≃* P) : (M ≃* P) := { map_mul' := λ x y, show h2 (h1 (x * y)) = h2 (h1 x) * h2 (h1 y), by rw [h1.map_mul, h2.map_mul], ..h1.to_equiv.trans h2.to_equiv } /-- e.right_inv in canonical form -/ @[simp, to_additive] lemma apply_symm_apply (e : M ≃* N) : ∀ y, e (e.symm y) = y := e.to_equiv.apply_symm_apply /-- e.left_inv in canonical form -/ @[simp, to_additive] lemma symm_apply_apply (e : M ≃* N) : ∀ x, e.symm (e x) = x := e.to_equiv.symm_apply_apply /-- a multiplicative equiv of monoids sends 1 to 1 (and is hence a monoid isomorphism) -/ @[simp, to_additive] lemma map_one {M N} [monoid M] [monoid N] (h : M ≃* N) : h 1 = 1 := by rw [←mul_one (h 1), ←h.apply_symm_apply 1, ←h.map_mul, one_mul] @[simp, to_additive] lemma map_eq_one_iff {M N} [monoid M] [monoid N] (h : M ≃* N) {x : M} : h x = 1 ↔ x = 1 := h.map_one ▸ h.to_equiv.apply_eq_iff_eq x 1 @[to_additive] lemma map_ne_one_iff {M N} [monoid M] [monoid N] (h : M ≃* N) {x : M} : h x ≠ 1 ↔ x ≠ 1 := ⟨mt h.map_eq_one_iff.2, mt h.map_eq_one_iff.1⟩ /-- Extract the forward direction of a multiplicative equivalence as a multiplication preserving function. -/ @[to_additive to_add_monoid_hom] def to_monoid_hom {M N} [monoid M] [monoid N] (h : M ≃* N) : (M →* N) := { map_one' := h.map_one, .. h } @[simp, to_additive] lemma to_monoid_hom_apply {M N} [monoid M] [monoid N] (e : M ≃* N) (x : M) : e.to_monoid_hom x = e x := rfl /-- A multiplicative equivalence of groups preserves inversion. -/ @[to_additive] lemma map_inv [group G] [group H] (h : G ≃* H) (x : G) : h x⁻¹ = (h x)⁻¹ := h.to_monoid_hom.map_inv x /-- A multiplicative bijection between two monoids is a monoid hom (deprecated -- use to_monoid_hom). -/ @[to_additive is_add_monoid_hom] instance is_monoid_hom {M N} [monoid M] [monoid N] (h : M ≃* N) : is_monoid_hom h := ⟨h.map_one⟩ /-- A multiplicative bijection between two groups is a group hom (deprecated -- use to_monoid_hom). -/ @[to_additive is_add_group_hom] instance is_group_hom {G H} [group G] [group H] (h : G ≃* H) : is_group_hom h := { map_mul := h.map_mul } /-- Two multiplicative isomorphisms agree if they are defined by the same underlying function. -/ @[ext, to_additive "Two additive isomorphisms agree if they are defined by the same underlying function."] lemma ext {f g : mul_equiv M N} (h : ∀ x, f x = g x) : f = g := begin have h₁ : f.to_equiv = g.to_equiv := equiv.ext h, cases f, cases g, congr, { exact (funext h) }, { exact congr_arg equiv.inv_fun h₁ } end attribute [ext] add_equiv.ext end mul_equiv /-- An additive equivalence of additive groups preserves subtraction. -/ lemma add_equiv.map_sub [add_group A] [add_group B] (h : A ≃+ B) (x y : A) : h (x - y) = h x - h y := h.to_add_monoid_hom.map_sub x y /-- The group of multiplicative automorphisms. -/ @[to_additive "The group of additive automorphisms."] def mul_aut (M : Type*) [has_mul M] := M ≃* M attribute [reducible] mul_aut add_aut namespace mul_aut variables (M) [has_mul M] /-- The group operation on multiplicative automorphisms is defined by `λ g h, mul_equiv.trans h g`. This means that multiplication agrees with composition, `(g*h)(x) = g (h x)`. -/ instance : group (mul_aut M) := by refine_struct { mul := λ g h, mul_equiv.trans h g, one := mul_equiv.refl M, inv := mul_equiv.symm }; intros; ext; try { refl }; apply equiv.left_inv instance : inhabited (mul_aut M) := ⟨1⟩ @[simp] lemma coe_mul (e₁ e₂ : mul_aut M) : ⇑(e₁ * e₂) = e₁ ∘ e₂ := rfl @[simp] lemma coe_one : ⇑(1 : mul_aut M) = id := rfl lemma mul_def (e₁ e₂ : mul_aut M) : e₁ * e₂ = e₂.trans e₁ := rfl lemma one_def : (1 : mul_aut M) = mul_equiv.refl _ := rfl lemma inv_def (e₁ : mul_aut M) : e₁⁻¹ = e₁.symm := rfl @[simp] lemma mul_apply (e₁ e₂ : mul_aut M) (m : M) : (e₁ * e₂) m = e₁ (e₂ m) := rfl @[simp] lemma one_apply (m : M) : (1 : mul_aut M) m = m := rfl /-- Monoid hom from the group of multiplicative automorphisms to the group of permutations. -/ def to_perm : mul_aut M →* equiv.perm M := by refine_struct { to_fun := mul_equiv.to_equiv }; intros; refl /-- group conjugation as a group homomorphism into the automorphism group. `conj g h = g * h * g⁻¹` -/ def conj [group G] : G →* mul_aut G := { to_fun := λ g, { to_fun := λ h, g * h * g⁻¹, inv_fun := λ h, g⁻¹ * h * g, left_inv := λ _, by simp [mul_assoc], right_inv := λ _, by simp [mul_assoc], map_mul' := by simp [mul_assoc] }, map_mul' := λ _ _, by ext; simp [mul_assoc], map_one' := by ext; simp [mul_assoc] } @[simp] lemma conj_apply [group G] (g h : G) : conj g h = g * h * g⁻¹ := rfl @[simp] lemma conj_symm_apply [group G] (g h : G) : (conj g).symm h = g⁻¹ * h * g := rfl end mul_aut namespace add_aut variables (A) [has_add A] /-- The group operation on additive automorphisms is defined by `λ g h, mul_equiv.trans h g`. This means that multiplication agrees with composition, `(g*h)(x) = g (h x)`. -/ instance group : group (add_aut A) := by refine_struct { mul := λ g h, add_equiv.trans h g, one := add_equiv.refl A, inv := add_equiv.symm }; intros; ext; try { refl }; apply equiv.left_inv instance : inhabited (add_aut A) := ⟨1⟩ @[simp] lemma coe_mul (e₁ e₂ : add_aut A) : ⇑(e₁ * e₂) = e₁ ∘ e₂ := rfl @[simp] lemma coe_one : ⇑(1 : add_aut A) = id := rfl lemma mul_def (e₁ e₂ : add_aut A) : e₁ * e₂ = e₂.trans e₁ := rfl lemma one_def : (1 : add_aut A) = add_equiv.refl _ := rfl lemma inv_def (e₁ : add_aut A) : e₁⁻¹ = e₁.symm := rfl @[simp] lemma mul_apply (e₁ e₂ : add_aut A) (a : A) : (e₁ * e₂) a = e₁ (e₂ a) := rfl @[simp] lemma one_apply (a : A) : (1 : add_aut A) a = a := rfl /-- Monoid hom from the group of multiplicative automorphisms to the group of permutations. -/ def to_perm : add_aut A →* equiv.perm A := by refine_struct { to_fun := add_equiv.to_equiv }; intros; refl end add_aut /-- A group is isomorphic to its group of units. -/ def to_units (G) [group G] : G ≃* units G := { to_fun := λ x, ⟨x, x⁻¹, mul_inv_self _, inv_mul_self _⟩, inv_fun := coe, left_inv := λ x, rfl, right_inv := λ u, units.ext rfl, map_mul' := λ x y, units.ext rfl } namespace units variables [monoid M] [monoid N] [monoid P] /-- A multiplicative equivalence of monoids defines a multiplicative equivalence of their groups of units. -/ def map_equiv (h : M ≃* N) : units M ≃* units N := { inv_fun := map h.symm.to_monoid_hom, left_inv := λ u, ext $ h.left_inv u, right_inv := λ u, ext $ h.right_inv u, .. map h.to_monoid_hom } end units namespace equiv section group variables [group G] @[to_additive] protected def mul_left (a : G) : perm G := { to_fun := λx, a * x, inv_fun := λx, a⁻¹ * x, left_inv := assume x, show a⁻¹ * (a * x) = x, from inv_mul_cancel_left a x, right_inv := assume x, show a * (a⁻¹ * x) = x, from mul_inv_cancel_left a x } @[simp, to_additive] lemma coe_mul_left (a : G) : ⇑(equiv.mul_left a) = (*) a := rfl @[simp, to_additive] lemma mul_left_symm (a : G) : (equiv.mul_left a).symm = equiv.mul_left a⁻¹ := ext $ λ x, rfl @[to_additive] protected def mul_right (a : G) : perm G := { to_fun := λx, x * a, inv_fun := λx, x * a⁻¹, left_inv := assume x, show (x * a) * a⁻¹ = x, from mul_inv_cancel_right x a, right_inv := assume x, show (x * a⁻¹) * a = x, from inv_mul_cancel_right x a } @[simp, to_additive] lemma coe_mul_right (a : G) : ⇑(equiv.mul_right a) = λ x, x * a := rfl @[simp, to_additive] lemma mul_right_symm (a : G) : (equiv.mul_right a).symm = equiv.mul_right a⁻¹ := ext $ λ x, rfl variable (G) @[to_additive] protected def inv : perm G := { to_fun := λa, a⁻¹, inv_fun := λa, a⁻¹, left_inv := assume a, inv_inv a, right_inv := assume a, inv_inv a } variable {G} @[simp, to_additive] lemma coe_inv : ⇑(equiv.inv G) = has_inv.inv := rfl @[simp, to_additive] lemma inv_symm : (equiv.inv G).symm = equiv.inv G := rfl end group section point_reflection variables [add_comm_group A] (x y : A) /-- Point reflection in `x` as a permutation. -/ def point_reflection (x : A) : perm A := (equiv.neg A).trans (equiv.add_left (x + x)) lemma point_reflection_apply : point_reflection x y = x + x - y := rfl @[simp] lemma point_reflection_self : point_reflection x x = x := add_sub_cancel _ _ lemma point_reflection_involutive : function.involutive (point_reflection x : A → A) := λ y, by simp only [point_reflection_apply, sub_sub_cancel] @[simp] lemma point_reflection_symm : (point_reflection x).symm = point_reflection x := by { ext y, rw [symm_apply_eq, point_reflection_involutive x y] } /-- `x` is the only fixed point of `point_reflection x`. This lemma requires `x + x = y + y ↔ x = y`. There is no typeclass to use here, so we add it as an explicit argument. -/ lemma point_reflection_fixed_iff_of_bit0_injective {x y : A} (h : function.injective (bit0 : A → A)) : point_reflection x y = y ↔ y = x := sub_eq_iff_eq_add.trans $ h.eq_iff.trans eq_comm end point_reflection end equiv section type_tags /-- Reinterpret `f : G ≃+ H` as `multiplicative G ≃* multiplicative H`. -/ def add_equiv.to_multiplicative [add_monoid G] [add_monoid H] (f : G ≃+ H) : multiplicative G ≃* multiplicative H := ⟨f.to_add_monoid_hom.to_multiplicative, f.symm.to_add_monoid_hom.to_multiplicative, f.3, f.4, f.5⟩ /-- Reinterpret `f : G ≃* H` as `additive G ≃+ additive H`. -/ def mul_equiv.to_additive [monoid G] [monoid H] (f : G ≃* H) : additive G ≃+ additive H := ⟨f.to_monoid_hom.to_additive, f.symm.to_monoid_hom.to_additive, f.3, f.4, f.5⟩ end type_tags
259160076438e6d9fcc9491370559e9c7103e0ce
853df553b1d6ca524e3f0a79aedd32dde5d27ec3
/src/category_theory/equivalence.lean
220952d2d4fbc82ed86af4d6f3dd6e45dd42ca77
[ "Apache-2.0" ]
permissive
DanielFabian/mathlib
efc3a50b5dde303c59eeb6353ef4c35a345d7112
f520d07eba0c852e96fe26da71d85bf6d40fcc2a
refs/heads/master
1,668,739,922,971
1,595,201,756,000
1,595,201,756,000
279,469,476
0
0
null
1,594,696,604,000
1,594,696,604,000
null
UTF-8
Lean
false
false
16,473
lean
/- Copyright (c) 2017 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Tim Baumann, Stephen Morgan, Scott Morrison, Floris van Doorn -/ import category_theory.fully_faithful import category_theory.whiskering import tactic.slice namespace category_theory open category_theory.functor nat_iso category universes v₁ v₂ v₃ u₁ u₂ u₃ -- declare the `v`'s first; see `category_theory.category` for an explanation /-- We define an equivalence as a (half)-adjoint equivalence, a pair of functors with a unit and counit which are natural isomorphisms and the triangle law `Fη ≫ εF = 1`, or in other words the composite `F ⟶ FGF ⟶ F` is the identity. The triangle equation is written as a family of equalities between morphisms, it is more complicated if we write it as an equality of natural transformations, because then we would have to insert natural transformations like `F ⟶ F1`. -/ structure equivalence (C : Type u₁) [category.{v₁} C] (D : Type u₂) [category.{v₂} D] := mk' :: (functor : C ⥤ D) (inverse : D ⥤ C) (unit_iso : 𝟭 C ≅ functor ⋙ inverse) (counit_iso : inverse ⋙ functor ≅ 𝟭 D) (functor_unit_iso_comp' : ∀(X : C), functor.map ((unit_iso.hom : 𝟭 C ⟶ functor ⋙ inverse).app X) ≫ counit_iso.hom.app (functor.obj X) = 𝟙 (functor.obj X) . obviously) restate_axiom equivalence.functor_unit_iso_comp' infixr ` ≌ `:10 := equivalence variables {C : Type u₁} [category.{v₁} C] {D : Type u₂} [category.{v₂} D] namespace equivalence abbreviation unit (e : C ≌ D) : 𝟭 C ⟶ e.functor ⋙ e.inverse := e.unit_iso.hom abbreviation counit (e : C ≌ D) : e.inverse ⋙ e.functor ⟶ 𝟭 D := e.counit_iso.hom abbreviation unit_inv (e : C ≌ D) : e.functor ⋙ e.inverse ⟶ 𝟭 C := e.unit_iso.inv abbreviation counit_inv (e : C ≌ D) : 𝟭 D ⟶ e.inverse ⋙ e.functor := e.counit_iso.inv /- While these abbreviations are convenient, they also cause some trouble, preventing structure projections from unfolding. -/ @[simp] lemma equivalence_mk'_unit (functor inverse unit_iso counit_iso f) : (⟨functor, inverse, unit_iso, counit_iso, f⟩ : C ≌ D).unit = unit_iso.hom := rfl @[simp] lemma equivalence_mk'_counit (functor inverse unit_iso counit_iso f) : (⟨functor, inverse, unit_iso, counit_iso, f⟩ : C ≌ D).counit = counit_iso.hom := rfl @[simp] lemma equivalence_mk'_unit_inv (functor inverse unit_iso counit_iso f) : (⟨functor, inverse, unit_iso, counit_iso, f⟩ : C ≌ D).unit_inv = unit_iso.inv := rfl @[simp] lemma equivalence_mk'_counit_inv (functor inverse unit_iso counit_iso f) : (⟨functor, inverse, unit_iso, counit_iso, f⟩ : C ≌ D).counit_inv = counit_iso.inv := rfl @[simp] lemma functor_unit_comp (e : C ≌ D) (X : C) : e.functor.map (e.unit.app X) ≫ e.counit.app (e.functor.obj X) = 𝟙 (e.functor.obj X) := e.functor_unit_iso_comp X @[simp] lemma counit_inv_functor_comp (e : C ≌ D) (X : C) : e.counit_inv.app (e.functor.obj X) ≫ e.functor.map (e.unit_inv.app X) = 𝟙 (e.functor.obj X) := begin erw [iso.inv_eq_inv (e.functor.map_iso (e.unit_iso.app X) ≪≫ e.counit_iso.app (e.functor.obj X)) (iso.refl _)], exact e.functor_unit_comp X end lemma functor_unit (e : C ≌ D) (X : C) : e.functor.map (e.unit.app X) = e.counit_inv.app (e.functor.obj X) := by { erw [←iso.comp_hom_eq_id (e.counit_iso.app _), functor_unit_comp], refl } lemma counit_functor (e : C ≌ D) (X : C) : e.counit.app (e.functor.obj X) = e.functor.map (e.unit_inv.app X) := by { erw [←iso.hom_comp_eq_id (e.functor.map_iso (e.unit_iso.app X)), functor_unit_comp], refl } /-- The other triangle equality. The proof follows the following proof in Globular: http://globular.science/1905.001 -/ @[simp] lemma unit_inverse_comp (e : C ≌ D) (Y : D) : e.unit.app (e.inverse.obj Y) ≫ e.inverse.map (e.counit.app Y) = 𝟙 (e.inverse.obj Y) := begin rw [←id_comp (e.inverse.map _), ←map_id e.inverse, ←counit_inv_functor_comp, map_comp, ←iso.hom_inv_id_assoc (e.unit_iso.app _) (e.inverse.map (e.functor.map _)), app_hom, app_inv], slice_lhs 2 3 { erw [e.unit.naturality] }, slice_lhs 1 2 { erw [e.unit.naturality] }, slice_lhs 4 4 { rw [←iso.hom_inv_id_assoc (e.inverse.map_iso (e.counit_iso.app _)) (e.unit_inv.app _)] }, slice_lhs 3 4 { erw [←map_comp e.inverse, e.counit.naturality], erw [(e.counit_iso.app _).hom_inv_id, map_id] }, erw [id_comp], slice_lhs 2 3 { erw [←map_comp e.inverse, e.counit_iso.inv.naturality, map_comp] }, slice_lhs 3 4 { erw [e.unit_inv.naturality] }, slice_lhs 4 5 { erw [←map_comp (e.functor ⋙ e.inverse), (e.unit_iso.app _).hom_inv_id, map_id] }, erw [id_comp], slice_lhs 3 4 { erw [←e.unit_inv.naturality] }, slice_lhs 2 3 { erw [←map_comp e.inverse, ←e.counit_iso.inv.naturality, (e.counit_iso.app _).hom_inv_id, map_id] }, erw [id_comp, (e.unit_iso.app _).hom_inv_id], refl end @[simp] lemma inverse_counit_inv_comp (e : C ≌ D) (Y : D) : e.inverse.map (e.counit_inv.app Y) ≫ e.unit_inv.app (e.inverse.obj Y) = 𝟙 (e.inverse.obj Y) := begin erw [iso.inv_eq_inv (e.unit_iso.app (e.inverse.obj Y) ≪≫ e.inverse.map_iso (e.counit_iso.app Y)) (iso.refl _)], exact e.unit_inverse_comp Y end lemma unit_inverse (e : C ≌ D) (Y : D) : e.unit.app (e.inverse.obj Y) = e.inverse.map (e.counit_inv.app Y) := by { erw [←iso.comp_hom_eq_id (e.inverse.map_iso (e.counit_iso.app Y)), unit_inverse_comp], refl } lemma inverse_counit (e : C ≌ D) (Y : D) : e.inverse.map (e.counit.app Y) = e.unit_inv.app (e.inverse.obj Y) := by { erw [←iso.hom_comp_eq_id (e.unit_iso.app _), unit_inverse_comp], refl } @[simp] lemma fun_inv_map (e : C ≌ D) (X Y : D) (f : X ⟶ Y) : e.functor.map (e.inverse.map f) = e.counit.app X ≫ f ≫ e.counit_inv.app Y := (nat_iso.naturality_2 (e.counit_iso) f).symm @[simp] lemma inv_fun_map (e : C ≌ D) (X Y : C) (f : X ⟶ Y) : e.inverse.map (e.functor.map f) = e.unit_inv.app X ≫ f ≫ e.unit.app Y := (nat_iso.naturality_1 (e.unit_iso) f).symm section -- In this section we convert an arbitrary equivalence to a half-adjoint equivalence. variables {F : C ⥤ D} {G : D ⥤ C} (η : 𝟭 C ≅ F ⋙ G) (ε : G ⋙ F ≅ 𝟭 D) def adjointify_η : 𝟭 C ≅ F ⋙ G := calc 𝟭 C ≅ F ⋙ G : η ... ≅ F ⋙ (𝟭 D ⋙ G) : iso_whisker_left F (left_unitor G).symm ... ≅ F ⋙ ((G ⋙ F) ⋙ G) : iso_whisker_left F (iso_whisker_right ε.symm G) ... ≅ F ⋙ (G ⋙ (F ⋙ G)) : iso_whisker_left F (associator G F G) ... ≅ (F ⋙ G) ⋙ (F ⋙ G) : (associator F G (F ⋙ G)).symm ... ≅ 𝟭 C ⋙ (F ⋙ G) : iso_whisker_right η.symm (F ⋙ G) ... ≅ F ⋙ G : left_unitor (F ⋙ G) lemma adjointify_η_ε (X : C) : F.map ((adjointify_η η ε).hom.app X) ≫ ε.hom.app (F.obj X) = 𝟙 (F.obj X) := begin dsimp [adjointify_η], simp, have := ε.hom.naturality (F.map (η.inv.app X)), dsimp at this, rw [this], clear this, rw [←assoc _ _ (F.map _)], have := ε.hom.naturality (ε.inv.app $ F.obj X), dsimp at this, rw [this], clear this, have := (ε.app $ F.obj X).hom_inv_id, dsimp at this, rw [this], clear this, rw [id_comp], have := (F.map_iso $ η.app X).hom_inv_id, dsimp at this, rw [this] end end protected definition mk (F : C ⥤ D) (G : D ⥤ C) (η : 𝟭 C ≅ F ⋙ G) (ε : G ⋙ F ≅ 𝟭 D) : C ≌ D := ⟨F, G, adjointify_η η ε, ε, adjointify_η_ε η ε⟩ @[refl, simps] def refl : C ≌ C := ⟨𝟭 C, 𝟭 C, iso.refl _, iso.refl _, λ X, category.id_comp _⟩ @[symm, simps] def symm (e : C ≌ D) : D ≌ C := ⟨e.inverse, e.functor, e.counit_iso.symm, e.unit_iso.symm, e.inverse_counit_inv_comp⟩ variables {E : Type u₃} [category.{v₃} E] @[trans, simps] def trans (e : C ≌ D) (f : D ≌ E) : C ≌ E := { functor := e.functor ⋙ f.functor, inverse := f.inverse ⋙ e.inverse, unit_iso := begin refine iso.trans e.unit_iso _, exact iso_whisker_left e.functor (iso_whisker_right f.unit_iso e.inverse) , end, counit_iso := begin refine iso.trans _ f.counit_iso, exact iso_whisker_left f.inverse (iso_whisker_right e.counit_iso f.functor) end, -- We wouldn't have need to give this proof if we'd used `equivalence.mk`, -- but we choose to avoid using that here, for the sake of good structure projection `simp` lemmas. functor_unit_iso_comp' := λ X, begin dsimp, rw [← f.functor.map_comp_assoc, e.functor.map_comp, functor_unit, fun_inv_map, inv_hom_id_app_assoc, assoc, inv_hom_id_app, counit_functor, ← functor.map_comp], erw [comp_id, hom_inv_id_app, functor.map_id], end } def fun_inv_id_assoc (e : C ≌ D) (F : C ⥤ E) : e.functor ⋙ e.inverse ⋙ F ≅ F := (functor.associator _ _ _).symm ≪≫ iso_whisker_right e.unit_iso.symm F ≪≫ F.left_unitor @[simp] lemma fun_inv_id_assoc_hom_app (e : C ≌ D) (F : C ⥤ E) (X : C) : (fun_inv_id_assoc e F).hom.app X = F.map (e.unit_inv.app X) := by { dsimp [fun_inv_id_assoc], tidy } @[simp] lemma fun_inv_id_assoc_inv_app (e : C ≌ D) (F : C ⥤ E) (X : C) : (fun_inv_id_assoc e F).inv.app X = F.map (e.unit.app X) := by { dsimp [fun_inv_id_assoc], tidy } def inv_fun_id_assoc (e : C ≌ D) (F : D ⥤ E) : e.inverse ⋙ e.functor ⋙ F ≅ F := (functor.associator _ _ _).symm ≪≫ iso_whisker_right e.counit_iso F ≪≫ F.left_unitor @[simp] lemma inv_fun_id_assoc_hom_app (e : C ≌ D) (F : D ⥤ E) (X : D) : (inv_fun_id_assoc e F).hom.app X = F.map (e.counit.app X) := by { dsimp [inv_fun_id_assoc], tidy } @[simp] lemma inv_fun_id_assoc_inv_app (e : C ≌ D) (F : D ⥤ E) (X : D) : (inv_fun_id_assoc e F).inv.app X = F.map (e.counit_inv.app X) := by { dsimp [inv_fun_id_assoc], tidy } section -- There's of course a monoid structure on `C ≌ C`, -- but let's not encourage using it. -- The power structure is nevertheless useful. /-- Powers of an auto-equivalence. -/ def pow (e : C ≌ C) : ℤ → (C ≌ C) | (int.of_nat 0) := equivalence.refl | (int.of_nat 1) := e | (int.of_nat (n+2)) := e.trans (pow (int.of_nat (n+1))) | (int.neg_succ_of_nat 0) := e.symm | (int.neg_succ_of_nat (n+1)) := e.symm.trans (pow (int.neg_succ_of_nat n)) instance : has_pow (C ≌ C) ℤ := ⟨pow⟩ @[simp] lemma pow_zero (e : C ≌ C) : e^(0 : ℤ) = equivalence.refl := rfl @[simp] lemma pow_one (e : C ≌ C) : e^(1 : ℤ) = e := rfl @[simp] lemma pow_minus_one (e : C ≌ C) : e^(-1 : ℤ) = e.symm := rfl -- TODO as necessary, add the natural isomorphisms `(e^a).trans e^b ≅ e^(a+b)`. -- At this point, we haven't even defined the category of equivalences. end end equivalence /-- A functor that is part of a (half) adjoint equivalence -/ class is_equivalence (F : C ⥤ D) := mk' :: (inverse : D ⥤ C) (unit_iso : 𝟭 C ≅ F ⋙ inverse) (counit_iso : inverse ⋙ F ≅ 𝟭 D) (functor_unit_iso_comp' : ∀ (X : C), F.map ((unit_iso.hom : 𝟭 C ⟶ F ⋙ inverse).app X) ≫ counit_iso.hom.app (F.obj X) = 𝟙 (F.obj X) . obviously) restate_axiom is_equivalence.functor_unit_iso_comp' namespace is_equivalence instance of_equivalence (F : C ≌ D) : is_equivalence F.functor := { ..F } instance of_equivalence_inverse (F : C ≌ D) : is_equivalence F.inverse := is_equivalence.of_equivalence F.symm open equivalence protected definition mk {F : C ⥤ D} (G : D ⥤ C) (η : 𝟭 C ≅ F ⋙ G) (ε : G ⋙ F ≅ 𝟭 D) : is_equivalence F := ⟨G, adjointify_η η ε, ε, adjointify_η_ε η ε⟩ end is_equivalence namespace functor def as_equivalence (F : C ⥤ D) [is_equivalence F] : C ≌ D := ⟨F, is_equivalence.inverse F, is_equivalence.unit_iso, is_equivalence.counit_iso, is_equivalence.functor_unit_iso_comp⟩ instance is_equivalence_refl : is_equivalence (𝟭 C) := is_equivalence.of_equivalence equivalence.refl def inv (F : C ⥤ D) [is_equivalence F] : D ⥤ C := is_equivalence.inverse F instance is_equivalence_inv (F : C ⥤ D) [is_equivalence F] : is_equivalence F.inv := is_equivalence.of_equivalence F.as_equivalence.symm @[simp] lemma as_equivalence_functor (F : C ⥤ D) [is_equivalence F] : F.as_equivalence.functor = F := rfl @[simp] lemma as_equivalence_inverse (F : C ⥤ D) [is_equivalence F] : F.as_equivalence.inverse = inv F := rfl @[simp] lemma inv_inv (F : C ⥤ D) [is_equivalence F] : inv (inv F) = F := rfl def fun_inv_id (F : C ⥤ D) [is_equivalence F] : F ⋙ F.inv ≅ 𝟭 C := is_equivalence.unit_iso.symm def inv_fun_id (F : C ⥤ D) [is_equivalence F] : F.inv ⋙ F ≅ 𝟭 D := is_equivalence.counit_iso variables {E : Type u₃} [category.{v₃} E] instance is_equivalence_trans (F : C ⥤ D) (G : D ⥤ E) [is_equivalence F] [is_equivalence G] : is_equivalence (F ⋙ G) := is_equivalence.of_equivalence (equivalence.trans (as_equivalence F) (as_equivalence G)) end functor namespace equivalence @[simp] lemma functor_inv (E : C ≌ D) : E.functor.inv = E.inverse := rfl @[simp] lemma inverse_inv (E : C ≌ D) : E.inverse.inv = E.functor := rfl @[simp] lemma functor_as_equivalence (E : C ≌ D) : E.functor.as_equivalence = E := by { cases E, congr, } @[simp] lemma inverse_as_equivalence (E : C ≌ D) : E.inverse.as_equivalence = E.symm := by { cases E, congr, } end equivalence namespace is_equivalence @[simp] lemma fun_inv_map (F : C ⥤ D) [is_equivalence F] (X Y : D) (f : X ⟶ Y) : F.map (F.inv.map f) = F.inv_fun_id.hom.app X ≫ f ≫ F.inv_fun_id.inv.app Y := begin erw [nat_iso.naturality_2], refl end @[simp] lemma inv_fun_map (F : C ⥤ D) [is_equivalence F] (X Y : C) (f : X ⟶ Y) : F.inv.map (F.map f) = F.fun_inv_id.hom.app X ≫ f ≫ F.fun_inv_id.inv.app Y := begin erw [nat_iso.naturality_2], refl end -- We should probably restate many of the lemmas about `equivalence` for `is_equivalence`, -- but these are the only ones I need for now. @[simp] lemma functor_unit_comp (E : C ⥤ D) [is_equivalence E] (Y) : E.map (is_equivalence.unit_iso.hom.app Y) ≫ is_equivalence.counit_iso.hom.app (E.obj Y) = 𝟙 _ := equivalence.functor_unit_comp (E.as_equivalence) Y @[simp] lemma counit_inv_functor_comp (E : C ⥤ D) [is_equivalence E] (Y) : is_equivalence.counit_iso.inv.app (E.obj Y) ≫ E.map (is_equivalence.unit_iso.inv.app Y) = 𝟙 _ := eq_of_inv_eq_inv (functor_unit_comp _ _) end is_equivalence class ess_surj (F : C ⥤ D) := (obj_preimage (d : D) : C) (iso' (d : D) : F.obj (obj_preimage d) ≅ d . obviously) restate_axiom ess_surj.iso' namespace functor def obj_preimage (F : C ⥤ D) [ess_surj F] (d : D) : C := ess_surj.obj_preimage.{v₁ v₂} F d def fun_obj_preimage_iso (F : C ⥤ D) [ess_surj F] (d : D) : F.obj (F.obj_preimage d) ≅ d := ess_surj.iso d end functor namespace equivalence def ess_surj_of_equivalence (F : C ⥤ D) [is_equivalence F] : ess_surj F := ⟨ λ Y : D, F.inv.obj Y, λ Y : D, (F.inv_fun_id.app Y) ⟩ @[priority 100] -- see Note [lower instance priority] instance faithful_of_equivalence (F : C ⥤ D) [is_equivalence F] : faithful F := { map_injective' := λ X Y f g w, begin have p := congr_arg (@category_theory.functor.map _ _ _ _ F.inv _ _) w, simpa only [cancel_epi, cancel_mono, is_equivalence.inv_fun_map] using p end }. @[priority 100] -- see Note [lower instance priority] instance full_of_equivalence (F : C ⥤ D) [is_equivalence F] : full F := { preimage := λ X Y f, F.fun_inv_id.inv.app X ≫ F.inv.map f ≫ F.fun_inv_id.hom.app Y, witness' := λ X Y f, F.inv.map_injective (by simpa only [is_equivalence.inv_fun_map, assoc, hom_inv_id_app_assoc, hom_inv_id_app] using comp_id _) } @[simp] private def equivalence_inverse (F : C ⥤ D) [full F] [faithful F] [ess_surj F] : D ⥤ C := { obj := λ X, F.obj_preimage X, map := λ X Y f, F.preimage ((F.fun_obj_preimage_iso X).hom ≫ f ≫ (F.fun_obj_preimage_iso Y).inv), map_id' := λ X, begin apply F.map_injective, tidy end, map_comp' := λ X Y Z f g, by apply F.map_injective; simp } def equivalence_of_fully_faithfully_ess_surj (F : C ⥤ D) [full F] [faithful F] [ess_surj F] : is_equivalence F := is_equivalence.mk (equivalence_inverse F) (nat_iso.of_components (λ X, (preimage_iso $ F.fun_obj_preimage_iso $ F.obj X).symm) (λ X Y f, by { apply F.map_injective, obviously })) (nat_iso.of_components (λ Y, F.fun_obj_preimage_iso Y) (by obviously)) end equivalence end category_theory
fb9e40a720b4f76802cae4b78e410faff121282d
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/tests/lean/inaccessible2.lean
f2f55e80a0a0b1e8fec7bdf05371a95877dab1a3
[ "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
791
lean
inductive imf {A B : Type*} (f : A → B) : B → Type* | mk : ∀ (a : A), imf (f a) definition inv_1 {A B : Type*} (f : A → B) : ∀ (b : B), imf f b → A | .(f .a) (imf.mk .f a) := a -- Error inaccessible annotation inside inaccessible annotation definition inv_2 {A B : Type*} (f : A → B) : ∀ (b : B), imf f b → A | .(f a) (let x := (imf.mk .f a) in x) := a -- Error invalid occurrence of 'let' expression definition inv_3 {A B : Type*} (f : A → B) : ∀ (b : B), imf f b → A | .(f a) ((λ (x : imf f b), x) (imf.mk .f a)) := a -- Error invalid occurrence of 'lambda' expression definition symm {A : Type*} : ∀ a b : A, a = b → b = a | .a .a (eq.refl a) := rfl -- Error `a` in eq.refl must be marked as inaccessible since it is an inductive datatype parameter
c0a3eba8263403911b90b0bae661ff20824a522e
6094e25ea0b7699e642463b48e51b2ead6ddc23f
/library/init/tactic.lean
5271777f6b53b9070cf444055d8f163007b05bf6
[ "Apache-2.0" ]
permissive
gbaz/lean
a7835c4e3006fbbb079e8f8ffe18aacc45adebfb
a501c308be3acaa50a2c0610ce2e0d71becf8032
refs/heads/master
1,611,198,791,433
1,451,339,111,000
1,451,339,111,000
48,713,797
0
0
null
1,451,338,939,000
1,451,338,939,000
null
UTF-8
Lean
false
false
7,308
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura This is just a trick to embed the 'tactic language' as a Lean expression. We should view 'tactic' as automation that when execute produces a term. tactic.builtin is just a "dummy" for creating the definitions that are actually implemented in C++ -/ prelude import init.datatypes init.reserved_notation init.num inductive tactic : Type := builtin : tactic namespace tactic -- Remark the following names are not arbitrary, the tactic module -- uses them when converting Lean expressions into actual tactic objects. -- The bultin 'by' construct triggers the process of converting a -- a term of type 'tactic' into a tactic that sythesizes a term definition and_then (t1 t2 : tactic) : tactic := builtin definition or_else (t1 t2 : tactic) : tactic := builtin definition append (t1 t2 : tactic) : tactic := builtin definition interleave (t1 t2 : tactic) : tactic := builtin definition par (t1 t2 : tactic) : tactic := builtin definition fixpoint (f : tactic → tactic) : tactic := builtin definition repeat (t : tactic) : tactic := builtin definition at_most (t : tactic) (k : num) : tactic := builtin definition discard (t : tactic) (k : num) : tactic := builtin definition focus_at (t : tactic) (i : num) : tactic := builtin definition try_for (t : tactic) (ms : num) : tactic := builtin definition all_goals (t : tactic) : tactic := builtin definition now : tactic := builtin definition assumption : tactic := builtin definition eassumption : tactic := builtin definition state : tactic := builtin definition fail : tactic := builtin definition id : tactic := builtin definition beta : tactic := builtin definition info : tactic := builtin definition whnf : tactic := builtin definition contradiction : tactic := builtin definition exfalso : tactic := builtin definition congruence : tactic := builtin definition rotate_left (k : num) := builtin definition rotate_right (k : num) := builtin definition rotate (k : num) := rotate_left k definition norm_num : tactic := builtin -- This is just a trick to embed expressions into tactics. -- The nested expressions are "raw". They tactic should -- elaborate them when it is executed. inductive expr : Type := builtin : expr inductive expr_list : Type := | nil : expr_list | cons : expr → expr_list → expr_list -- auxiliary type used to mark optional list of arguments definition opt_expr_list := expr_list -- auxiliary types used to mark that the expression is suppose to be an identifier, optional, or a list. definition identifier := expr definition identifier_list := expr_list definition opt_identifier_list := expr_list -- Remark: the parser has special support for tactics containing `location` parameters. -- It will parse the optional `at ...` modifier. definition location := expr -- Marker for instructing the parser to parse it as 'with <expr>' definition with_expr := expr -- Marker for instructing the parser to parse it as '?(using <expr>)' definition using_expr := expr -- Constant used to denote the case were no expression was provided definition none_expr : expr := expr.builtin definition apply (e : expr) : tactic := builtin definition eapply (e : expr) : tactic := builtin definition fapply (e : expr) : tactic := builtin definition rename (a b : identifier) : tactic := builtin definition intro (e : opt_identifier_list) : tactic := builtin definition generalize_tac (e : expr) (id : identifier) : tactic := builtin definition clear (e : identifier_list) : tactic := builtin definition revert (e : identifier_list) : tactic := builtin definition refine (e : expr) : tactic := builtin definition exact (e : expr) : tactic := builtin -- Relaxed version of exact that does not enforce goal type definition rexact (e : expr) : tactic := builtin definition check_expr (e : expr) : tactic := builtin definition trace (s : string) : tactic := builtin -- rewrite_tac is just a marker for the builtin 'rewrite' notation -- used to create instances of this tactic. definition rewrite_tac (e : expr_list) : tactic := builtin definition xrewrite_tac (e : expr_list) : tactic := builtin definition krewrite_tac (e : expr_list) : tactic := builtin -- Arguments: -- - ls : lemmas to be used (if not provided, then blast will choose them) -- - ds : definitions that can be unfolded (if not provided, then blast will choose them) definition blast (ls : opt_identifier_list) (ds : opt_identifier_list) : tactic := builtin -- with_options_tac is just a marker for the builtin 'with_options' notation definition with_options_tac (o : expr) (t : tactic) : tactic := builtin -- with_options_tac is just a marker for the builtin 'with_attributes' notation definition with_attributes_tac (o : expr) (n : identifier_list) (t : tactic) : tactic := builtin definition simp : tactic := #tactic with_options [blast.strategy "simp"] blast definition simp_nohyps : tactic := #tactic with_options [blast.strategy "simp_nohyps"] blast definition cases (h : expr) (ids : opt_identifier_list) : tactic := builtin definition induction (h : expr) (rec : using_expr) (ids : opt_identifier_list) : tactic := builtin definition intros (ids : opt_identifier_list) : tactic := builtin definition generalizes (es : expr_list) : tactic := builtin definition clears (ids : identifier_list) : tactic := builtin definition reverts (ids : identifier_list) : tactic := builtin definition change (e : expr) : tactic := builtin definition assert_hypothesis (id : identifier) (e : expr) : tactic := builtin definition note_tac (id : identifier) (e : expr) : tactic := builtin definition constructor (k : option num) : tactic := builtin definition fconstructor (k : option num) : tactic := builtin definition existsi (e : expr) : tactic := builtin definition split : tactic := builtin definition left : tactic := builtin definition right : tactic := builtin definition injection (e : expr) (ids : opt_identifier_list) : tactic := builtin definition subst (ids : identifier_list) : tactic := builtin definition substvars : tactic := builtin definition reflexivity : tactic := builtin definition symmetry : tactic := builtin definition transitivity (e : expr) : tactic := builtin definition try (t : tactic) : tactic := or_else t id definition repeat1 (t : tactic) : tactic := and_then t (repeat t) definition focus (t : tactic) : tactic := focus_at t 0 definition determ (t : tactic) : tactic := at_most t 1 definition trivial : tactic := or_else (or_else (apply eq.refl) (apply true.intro)) assumption definition do (n : num) (t : tactic) : tactic := nat.rec id (λn t', and_then t t') (nat.of_num n) end tactic tactic_infixl `;`:15 := tactic.and_then tactic_notation T1 `:`:15 T2 := tactic.focus (tactic.and_then T1 (tactic.all_goals T2)) tactic_notation `(` h `|` r:(foldl `|` (e r, tactic.or_else r e) h) `)` := r
606445f79d3d1e426cd2f2d5d353ae762c3b4eb7
e9dbaaae490bc072444e3021634bf73664003760
/src/Problems/2018/IMO_2018_P6.lean
b1a111e33ad1e81649a4224a9e0b918e84af4d2b
[ "Apache-2.0" ]
permissive
liaofei1128/geometry
566d8bfe095ce0c0113d36df90635306c60e975b
3dd128e4eec8008764bb94e18b932f9ffd66e6b3
refs/heads/master
1,678,996,510,399
1,581,454,543,000
1,583,337,839,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
468
lean
import Geo.Geo.Core namespace Geo open Analytic Angle Quadrilateral Triangle def IMO_2018_P6 : Prop := ∀ (A B C D : Point), convex (Quadrilateral.mk A B C D) → ulen (Seg.mk A B) * ulen (Seg.mk C D) = ulen (Seg.mk B C) * ulen (Seg.mk D A) → ∀ (X : Point), inside X (Quadrilateral.mk A B C D) → uangle ⟨X, A, B⟩ = uangle ⟨X, C, D⟩ → uangle ⟨X, B, C⟩ = uangle ⟨X, D, A⟩ → deg2π 180 = uangle ⟨B, X, A⟩ + uangle ⟨D, X, C⟩ end Geo
821c61b0193531cc7c3740a98d8cf905ed12e278
618003631150032a5676f229d13a079ac875ff77
/src/init_/algebra/norm_num.lean
46617492bf9a15d88f2f768490e1875bd4f0df33
[ "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
8,873
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Lewis, Leonardo de Moura -/ import algebra.field import algebra.ordered_ring namespace norm_num universe u variable {α : Type u} def add1 [has_add α] [has_one α] (a : α) : α := a + 1 local attribute [reducible] bit0 bit1 add1 local attribute [simp] right_distrib left_distrib sub_eq_add_neg private meta def u : tactic unit := `[unfold bit0 bit1 add1] private meta def usimp : tactic unit := u >> `[simp [add_comm, add_left_comm]] lemma mul_zero [mul_zero_class α] (a : α) : a * 0 = 0 := by simp lemma zero_mul [mul_zero_class α] (a : α) : 0 * a = 0 := by simp lemma mul_one [monoid α] (a : α) : a * 1 = a := by simp lemma mul_bit0 [distrib α] (a b : α) : a * (bit0 b) = bit0 (a * b) := by simp lemma mul_bit0_helper [distrib α] (a b t : α) (h : a * b = t) : a * (bit0 b) = bit0 t := begin rw [← h], simp end lemma mul_bit1 [semiring α] (a b : α) : a * (bit1 b) = bit0 (a * b) + a := by simp lemma mul_bit1_helper [semiring α] (a b s t : α) (hs : a * b = s) (ht : bit0 s + a = t) : a * (bit1 b) = t := by simp [hs, ht] lemma subst_into_prod [has_mul α] (l r tl tr t : α) (prl : l = tl) (prr : r = tr) (prt : tl * tr = t) : l * r = t := by simp [prl, prr, prt] lemma mk_cong (op : α → α) (a b : α) (h : a = b) : op a = op b := by simp [h] lemma neg_add_neg_eq_of_add_add_eq_zero [add_comm_group α] (a b c : α) (h : c + a + b = 0) : -a + -b = c := begin apply add_neg_eq_of_eq_add, apply neg_eq_of_add_eq_zero, simp [add_comm, add_left_comm] at h, simp [add_comm], assumption end lemma neg_add_neg_helper [add_comm_group α] (a b c : α) (h : a + b = c) : -a + -b = -c := begin apply @neg_inj α, simp [neg_add, neg_neg], assumption end lemma neg_add_pos_eq_of_eq_add [add_comm_group α] (a b c : α) (h : b = c + a) : -a + b = c := begin apply neg_add_eq_of_eq_add, simp [add_comm] at h, assumption end lemma neg_add_pos_helper1 [add_comm_group α] (a b c : α) (h : b + c = a) : -a + b = -c := begin apply neg_add_eq_of_eq_add, apply eq_add_neg_of_add_eq h end lemma neg_add_pos_helper2 [add_comm_group α] (a b c : α) (h : a + c = b) : -a + b = c := begin apply neg_add_eq_of_eq_add, rw h end lemma pos_add_neg_helper [add_comm_group α] (a b c : α) (h : b + a = c) : a + b = c := by rw [← h, add_comm a b] lemma subst_into_subtr [add_group α] (l r t : α) (h : l + -r = t) : l - r = t := by simp [h] lemma neg_neg_helper [add_group α] (a b : α) (h : a = -b) : -a = b := by simp [h] lemma neg_mul_neg_helper [ring α] (a b c : α) (h : a * b = c) : (-a) * (-b) = c := by simp [h] lemma neg_mul_pos_helper [ring α] (a b c : α) (h : a * b = c) : (-a) * b = -c := by simp [h] lemma pos_mul_neg_helper [ring α] (a b c : α) (h : a * b = c) : a * (-b) = -c := by simp [h] lemma div_add_helper [field α] (n d b c val : α) (hd : d ≠ 0) (h : n + b * d = val) (h2 : c * d = val) : n / d + b = c := begin apply eq_of_mul_eq_mul_of_nonzero_right hd, rw [h2, ← h, right_distrib, div_mul_cancel _ hd] end lemma add_div_helper [field α] (n d b c val : α) (hd : d ≠ 0) (h : d * b + n = val) (h2 : d * c = val) : b + n / d = c := begin apply eq_of_mul_eq_mul_of_nonzero_left hd, rw [h2, ← h, left_distrib, mul_div_cancel' _ hd] end lemma div_mul_helper [field α] (n d c v : α) (h : (n * c) / d = v) : (n / d) * c = v := by rw [← h, div_mul_eq_mul_div_comm, mul_div_assoc] lemma mul_div_helper [field α] (a n d v : α) (hd : d ≠ 0) (h : (a * n) / d = v) : a * (n / d) = v := by rw [← h, mul_div_assoc] lemma nonzero_of_div_helper [field α] (a b : α) (ha : a ≠ 0) (hb : b ≠ 0) : a / b ≠ 0 := begin intro hab, have habb : (a / b) * b = 0, rw [hab, zero_mul], rw [div_mul_cancel _ hb] at habb, exact ha habb end lemma div_helper [field α] (n d v : α) (hd : d ≠ 0) (h : v * d = n) : n / d = v := begin apply eq_of_mul_eq_mul_of_nonzero_right hd, rw (div_mul_cancel _ hd), exact eq.symm h end lemma div_eq_div_helper [field α] (a b c d v : α) (h1 : a * d = v) (h2 : c * b = v) (hb : b ≠ 0) (hd : d ≠ 0) : a / b = c / d := begin apply eq_div_of_mul_eq, exact hd, rw div_mul_eq_mul_div, apply eq.symm, apply eq_div_of_mul_eq, exact hb, rw [h1, h2] end lemma subst_into_div [has_div α] (a₁ b₁ a₂ b₂ v : α) (h : a₁ / b₁ = v) (h1 : a₂ = a₁) (h2 : b₂ = b₁) : a₂ / b₂ = v := by rw [h1, h2, h] lemma add_comm_four [add_comm_semigroup α] (a b : α) : a + a + (b + b) = (a + b) + (a + b) := by simp [add_left_comm, add_assoc] lemma add_comm_middle [add_comm_semigroup α] (a b c : α) : a + b + c = a + c + b := by simp [add_comm, add_left_comm] lemma bit0_add_bit0 [add_comm_semigroup α] (a b : α) : bit0 a + bit0 b = bit0 (a + b) := by usimp lemma bit0_add_bit0_helper [add_comm_semigroup α] (a b t : α) (h : a + b = t) : bit0 a + bit0 b = bit0 t := begin rw [← h], usimp end lemma bit1_add_bit0 [add_comm_semigroup α] [has_one α] (a b : α) : bit1 a + bit0 b = bit1 (a + b) := by rw add_comm; usimp lemma bit1_add_bit0_helper [add_comm_semigroup α] [has_one α] (a b t : α) (h : a + b = t) : bit1 a + bit0 b = bit1 t := begin rw [← h, add_comm], usimp end lemma bit0_add_bit1 [add_comm_semigroup α] [has_one α] (a b : α) : bit0 a + bit1 b = bit1 (a + b) := by usimp lemma bit0_add_bit1_helper [add_comm_semigroup α] [has_one α] (a b t : α) (h : a + b = t) : bit0 a + bit1 b = bit1 t := begin rw [← h], usimp end lemma bit1_add_bit1 [add_comm_semigroup α] [has_one α] (a b : α) : bit1 a + bit1 b = bit0 (add1 (a + b)) := by usimp lemma bit1_add_bit1_helper [add_comm_semigroup α] [has_one α] (a b t s : α) (h : (a + b) = t) (h2 : add1 t = s) : bit1 a + bit1 b = bit0 s := begin rw [← h] at h2, rw [← h2], usimp end lemma bin_add_zero [add_monoid α] (a : α) : a + 0 = a := by simp lemma bin_zero_add [add_monoid α] (a : α) : 0 + a = a := by simp lemma one_add_bit0 [add_comm_semigroup α] [has_one α] (a : α) : 1 + bit0 a = bit1 a := begin unfold bit0 bit1, simp [add_comm] end lemma bit0_add_one [has_add α] [has_one α] (a : α) : bit0 a + 1 = bit1 a := rfl lemma bit1_add_one [has_add α] [has_one α] (a : α) : bit1 a + 1 = add1 (bit1 a) := rfl lemma bit1_add_one_helper [has_add α] [has_one α] (a t : α) (h : add1 (bit1 a) = t) : bit1 a + 1 = t := by rw [← h] lemma one_add_bit1 [add_comm_semigroup α] [has_one α] (a : α) : 1 + bit1 a = add1 (bit1 a) := begin unfold bit0 bit1 add1, simp [add_left_comm, add_assoc] end lemma one_add_bit1_helper [add_comm_semigroup α] [has_one α] (a t : α) (h : add1 (bit1 a) = t) : 1 + bit1 a = t := begin rw [← h], usimp end lemma add1_bit0 [has_add α] [has_one α] (a : α) : add1 (bit0 a) = bit1 a := rfl lemma add1_bit1 [add_comm_semigroup α] [has_one α] (a : α) : add1 (bit1 a) = bit0 (add1 a) := by usimp lemma add1_bit1_helper [add_comm_semigroup α] [has_one α] (a t : α) (h : add1 a = t) : add1 (bit1 a) = bit0 t := begin rw [← h], usimp end lemma add1_one [has_add α] [has_one α] : add1 (1 : α) = bit0 1 := rfl lemma add1_zero [add_monoid α] [has_one α] : add1 (0 : α) = 1 := by usimp lemma one_add_one [has_add α] [has_one α] : (1 : α) + 1 = bit0 1 := rfl lemma subst_into_sum [has_add α] (l r tl tr t : α) (prl : l = tl) (prr : r = tr) (prt : tl + tr = t) : l + r = t := by rw [← prt, prr, prl] lemma neg_zero_helper [add_group α] (a : α) (h : a = 0) : - a = 0 := begin rw h, simp end lemma pos_bit0_helper [linear_ordered_semiring α] (a : α) (h : a > 0) : bit0 a > 0 := begin u, apply add_pos h h end lemma nonneg_bit0_helper [linear_ordered_semiring α] (a : α) (h : a ≥ 0) : bit0 a ≥ 0 := begin u, apply add_nonneg h h end lemma pos_bit1_helper [linear_ordered_semiring α] (a : α) (h : a ≥ 0) : bit1 a > 0 := begin u, apply add_pos_of_nonneg_of_pos, apply nonneg_bit0_helper _ h, apply zero_lt_one end lemma nonneg_bit1_helper [linear_ordered_semiring α] (a : α) (h : a ≥ 0) : bit1 a ≥ 0 := begin apply le_of_lt, apply pos_bit1_helper _ h end lemma nonzero_of_pos_helper [linear_ordered_semiring α] (a : α) (h : a > 0) : a ≠ 0 := ne_of_gt h lemma nonzero_of_neg_helper [linear_ordered_ring α] (a : α) (h : a ≠ 0) : -a ≠ 0 := begin intro ha, apply h, apply neg_inj, rwa neg_zero end lemma sub_nat_zero_helper {a b c d: ℕ} (hac : a = c) (hbd : b = d) (hcd : c < d) : a - b = 0 := begin simp *, apply nat.sub_eq_zero_of_le, apply le_of_lt, assumption end lemma sub_nat_pos_helper {a b c d e : ℕ} (hac : a = c) (hbd : b = d) (hced : e + d = c) : a - b = e := begin simp *, rw [← hced, nat.add_sub_cancel] end end norm_num
6743305d7c659f442d2752301208d3b00fd47830
4b846d8dabdc64e7ea03552bad8f7fa74763fc67
/library/init/meta/decl_cmds.lean
adf52d4b028cc256d91b50285f2271a37917e0d2
[ "Apache-2.0" ]
permissive
pacchiano/lean
9324b33f3ac3b5c5647285160f9f6ea8d0d767dc
fdadada3a970377a6df8afcd629a6f2eab6e84e8
refs/heads/master
1,611,357,380,399
1,489,870,101,000
1,489,870,101,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,038
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.meta.tactic init.meta.rb_map open tactic private meta def apply_replacement (replacements : name_map name) (e : expr) : expr := e^.replace (λ e d, match e with | expr.const n ls := match replacements^.find n with | some new_n := some (expr.const new_n ls) | none := none end | _ := none end) /- Given a set of constant renamings `replacements` and a declaration name `src_decl_name`, create a new declaration called `new_decl_name` s.t. its type is the type of `src_decl_name` after applying the given constant replacement. Remark: the new type must be definitionally equal to the type of `src_decl_name`. Example: Assume the environment contains def f : nat -> nat := ... def g : nat -> nat := f lemma f_lemma : forall a, f a > 0 := ... Moreover, assume we have a mapping M containing `f -> `g Then, the command run_command copy_decl_updating_type M `f_lemma `g_lemma creates the declaration lemma g_lemma : forall a, g a > 0 := ... -/ meta def copy_decl_updating_type (replacements : name_map name) (src_decl_name : name) (new_decl_name : name) : command := do env ← get_env, decl ← env^.get src_decl_name, let decl := decl^.update_name $ new_decl_name, let decl := decl^.update_type $ apply_replacement replacements decl^.type, let decl := decl^.update_value $ expr.const src_decl_name (decl^.univ_params^.map level.param), add_decl decl meta def copy_decl_using (replacements : name_map name) (src_decl_name : name) (new_decl_name : name) : command := do env ← get_env, decl ← env^.get src_decl_name, let decl := decl^.update_name $ new_decl_name, let decl := decl^.update_type $ apply_replacement replacements decl^.type, let decl := decl^.map_value $ apply_replacement replacements, add_decl decl
511c7f1b8765abe4109ba88652d84c44f061eefc
618003631150032a5676f229d13a079ac875ff77
/src/data/num/lemmas.lean
5bcd4b36caa4901f4089bfbf6b7a1f92fa7ccaba
[ "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
48,317
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro -/ import data.num.bitwise import data.int.basic import data.nat.gcd /-! # Properties of the binary representation of integers -/ local attribute [simp] add_assoc namespace pos_num variables {α : Type*} @[simp, norm_cast] theorem cast_one [has_zero α] [has_one α] [has_add α] : ((1 : pos_num) : α) = 1 := rfl @[simp] theorem cast_one' [has_zero α] [has_one α] [has_add α] : (pos_num.one : α) = 1 := rfl @[simp, norm_cast] theorem cast_bit0 [has_zero α] [has_one α] [has_add α] (n : pos_num) : (n.bit0 : α) = _root_.bit0 n := rfl @[simp, norm_cast] theorem cast_bit1 [has_zero α] [has_one α] [has_add α] (n : pos_num) : (n.bit1 : α) = _root_.bit1 n := rfl @[simp, norm_cast] theorem cast_to_nat [add_monoid α] [has_one α] : ∀ n : pos_num, ((n : ℕ) : α) = n | 1 := nat.cast_one | (bit0 p) := (nat.cast_bit0 _).trans $ congr_arg _root_.bit0 p.cast_to_nat | (bit1 p) := (nat.cast_bit1 _).trans $ congr_arg _root_.bit1 p.cast_to_nat @[simp, norm_cast] theorem to_nat_to_int (n : pos_num) : ((n : ℕ) : ℤ) = n := by rw [← int.nat_cast_eq_coe_nat, cast_to_nat] @[simp, norm_cast] theorem cast_to_int [add_group α] [has_one α] (n : pos_num) : ((n : ℤ) : α) = n := by rw [← to_nat_to_int, int.cast_coe_nat, cast_to_nat] theorem succ_to_nat : ∀ n, (succ n : ℕ) = n + 1 | 1 := rfl | (bit0 p) := rfl | (bit1 p) := (congr_arg _root_.bit0 (succ_to_nat p)).trans $ show ↑p + 1 + ↑p + 1 = ↑p + ↑p + 1 + 1, by simp [add_left_comm] theorem one_add (n : pos_num) : 1 + n = succ n := by cases n; refl theorem add_one (n : pos_num) : n + 1 = succ n := by cases n; refl @[norm_cast] theorem add_to_nat : ∀ m n, ((m + n : pos_num) : ℕ) = m + n | 1 b := by rw [one_add b, succ_to_nat, add_comm]; refl | a 1 := by rw [add_one a, succ_to_nat]; refl | (bit0 a) (bit0 b) := (congr_arg _root_.bit0 (add_to_nat a b)).trans $ show ((a + b) + (a + b) : ℕ) = (a + a) + (b + b), by simp [add_left_comm] | (bit0 a) (bit1 b) := (congr_arg _root_.bit1 (add_to_nat a b)).trans $ show ((a + b) + (a + b) + 1 : ℕ) = (a + a) + (b + b + 1), by simp [add_left_comm] | (bit1 a) (bit0 b) := (congr_arg _root_.bit1 (add_to_nat a b)).trans $ show ((a + b) + (a + b) + 1 : ℕ) = (a + a + 1) + (b + b), by simp [add_comm, add_left_comm] | (bit1 a) (bit1 b) := show (succ (a + b) + succ (a + b) : ℕ) = (a + a + 1) + (b + b + 1), by rw [succ_to_nat, add_to_nat]; simp [add_left_comm] theorem add_succ : ∀ (m n : pos_num), m + succ n = succ (m + n) | 1 b := by simp [one_add] | (bit0 a) 1 := congr_arg bit0 (add_one a) | (bit1 a) 1 := congr_arg bit1 (add_one a) | (bit0 a) (bit0 b) := rfl | (bit0 a) (bit1 b) := congr_arg bit0 (add_succ a b) | (bit1 a) (bit0 b) := rfl | (bit1 a) (bit1 b) := congr_arg bit1 (add_succ a b) theorem bit0_of_bit0 : Π n, _root_.bit0 n = bit0 n | 1 := rfl | (bit0 p) := congr_arg bit0 (bit0_of_bit0 p) | (bit1 p) := show bit0 (succ (_root_.bit0 p)) = _, by rw bit0_of_bit0; refl theorem bit1_of_bit1 (n : pos_num) : _root_.bit1 n = bit1 n := show _root_.bit0 n + 1 = bit1 n, by rw [add_one, bit0_of_bit0]; refl @[norm_cast] theorem mul_to_nat (m) : ∀ n, ((m * n : pos_num) : ℕ) = m * n | 1 := (mul_one _).symm | (bit0 p) := show (↑(m * p) + ↑(m * p) : ℕ) = ↑m * (p + p), by rw [mul_to_nat, left_distrib] | (bit1 p) := (add_to_nat (bit0 (m * p)) m).trans $ show (↑(m * p) + ↑(m * p) + ↑m : ℕ) = ↑m * (p + p) + m, by rw [mul_to_nat, left_distrib] theorem to_nat_pos : ∀ n : pos_num, 0 < (n : ℕ) | 1 := zero_lt_one | (bit0 p) := let h := to_nat_pos p in add_pos h h | (bit1 p) := nat.succ_pos _ theorem cmp_to_nat_lemma {m n : pos_num} : (m:ℕ) < n → (bit1 m : ℕ) < bit0 n := show (m:ℕ) < n → (m + m + 1 + 1 : ℕ) ≤ n + n, by intro h; rw [nat.add_right_comm m m 1, add_assoc]; exact add_le_add h h theorem cmp_swap (m) : ∀n, (cmp m n).swap = cmp n m := by induction m with m IH m IH; intro n; cases n with n n; try {unfold cmp}; try {refl}; rw ←IH; cases cmp m n; refl theorem cmp_to_nat : ∀ (m n), (ordering.cases_on (cmp m n) ((m:ℕ) < n) (m = n) ((m:ℕ) > n) : Prop) | 1 1 := rfl | (bit0 a) 1 := let h : (1:ℕ) ≤ a := to_nat_pos a in add_le_add h h | (bit1 a) 1 := nat.succ_lt_succ $ to_nat_pos $ bit0 a | 1 (bit0 b) := let h : (1:ℕ) ≤ b := to_nat_pos b in add_le_add h h | 1 (bit1 b) := nat.succ_lt_succ $ to_nat_pos $ bit0 b | (bit0 a) (bit0 b) := begin have := cmp_to_nat a b, revert this, cases cmp a b; dsimp; intro, { exact add_lt_add this this }, { rw this }, { exact add_lt_add this this } end | (bit0 a) (bit1 b) := begin dsimp [cmp], have := cmp_to_nat a b, revert this, cases cmp a b; dsimp; intro, { exact nat.le_succ_of_le (add_lt_add this this) }, { rw this, apply nat.lt_succ_self }, { exact cmp_to_nat_lemma this } end | (bit1 a) (bit0 b) := begin dsimp [cmp], have := cmp_to_nat a b, revert this, cases cmp a b; dsimp; intro, { exact cmp_to_nat_lemma this }, { rw this, apply nat.lt_succ_self }, { exact nat.le_succ_of_le (add_lt_add this this) }, end | (bit1 a) (bit1 b) := begin have := cmp_to_nat a b, revert this, cases cmp a b; dsimp; intro, { exact nat.succ_lt_succ (add_lt_add this this) }, { rw this }, { exact nat.succ_lt_succ (add_lt_add this this) } end @[norm_cast] theorem lt_to_nat {m n : pos_num} : (m:ℕ) < n ↔ m < n := show (m:ℕ) < n ↔ cmp m n = ordering.lt, from match cmp m n, cmp_to_nat m n with | ordering.lt, h := by simp at h; simp [h] | ordering.eq, h := by simp at h; simp [h, lt_irrefl]; exact dec_trivial | ordering.gt, h := by simp [not_lt_of_gt h]; exact dec_trivial end @[norm_cast] theorem le_to_nat {m n : pos_num} : (m:ℕ) ≤ n ↔ m ≤ n := by rw ← not_lt; exact not_congr lt_to_nat end pos_num namespace num variables {α : Type*} open pos_num theorem add_zero (n : num) : n + 0 = n := by cases n; refl theorem zero_add (n : num) : 0 + n = n := by cases n; refl theorem add_one : ∀ n : num, n + 1 = succ n | 0 := rfl | (pos p) := by cases p; refl theorem add_succ : ∀ (m n : num), m + succ n = succ (m + n) | 0 n := by simp [zero_add] | (pos p) 0 := show pos (p + 1) = succ (pos p + 0), by rw [pos_num.add_one, add_zero]; refl | (pos p) (pos q) := congr_arg pos (pos_num.add_succ _ _) @[simp, norm_cast] theorem add_of_nat (m) : ∀ n, ((m + n : ℕ) : num) = m + n | 0 := (add_zero _).symm | (n+1) := show ((m + n : ℕ) + 1 : num) = m + (↑ n + 1), by rw [add_one, add_one, add_succ, add_of_nat] theorem bit0_of_bit0 : ∀ n : num, bit0 n = n.bit0 | 0 := rfl | (pos p) := congr_arg pos p.bit0_of_bit0 theorem bit1_of_bit1 : ∀ n : num, bit1 n = n.bit1 | 0 := rfl | (pos p) := congr_arg pos p.bit1_of_bit1 @[simp, norm_cast] theorem cast_zero [has_zero α] [has_one α] [has_add α] : ((0 : num) : α) = 0 := rfl @[simp] theorem cast_zero' [has_zero α] [has_one α] [has_add α] : (num.zero : α) = 0 := rfl @[simp, norm_cast] theorem cast_one [has_zero α] [has_one α] [has_add α] : ((1 : num) : α) = 1 := rfl @[simp] theorem cast_pos [has_zero α] [has_one α] [has_add α] (n : pos_num) : (num.pos n : α) = n := rfl theorem succ'_to_nat : ∀ n, (succ' n : ℕ) = n + 1 | 0 := (_root_.zero_add _).symm | (pos p) := pos_num.succ_to_nat _ theorem succ_to_nat (n) : (succ n : ℕ) = n + 1 := succ'_to_nat n @[simp, norm_cast] theorem cast_to_nat [add_monoid α] [has_one α] : ∀ n : num, ((n : ℕ) : α) = n | 0 := nat.cast_zero | (pos p) := p.cast_to_nat @[simp, norm_cast] theorem to_nat_to_int (n : num) : ((n : ℕ) : ℤ) = n := by rw [← int.nat_cast_eq_coe_nat, cast_to_nat] @[simp, norm_cast] theorem cast_to_int [add_group α] [has_one α] (n : num) : ((n : ℤ) : α) = n := by rw [← to_nat_to_int, int.cast_coe_nat, cast_to_nat] @[norm_cast] theorem to_of_nat : Π (n : ℕ), ((n : num) : ℕ) = n | 0 := rfl | (n+1) := by rw [nat.cast_add_one, add_one, succ_to_nat, to_of_nat] @[simp, norm_cast] theorem of_nat_cast [add_monoid α] [has_one α] (n : ℕ) : ((n : num) : α) = n := by rw [← cast_to_nat, to_of_nat] @[norm_cast] theorem of_nat_inj {m n : ℕ} : (m : num) = n ↔ m = n := ⟨λ h, function.left_inverse.injective to_of_nat h, congr_arg _⟩ @[norm_cast] theorem add_to_nat : ∀ m n, ((m + n : num) : ℕ) = m + n | 0 0 := rfl | 0 (pos q) := (_root_.zero_add _).symm | (pos p) 0 := rfl | (pos p) (pos q) := pos_num.add_to_nat _ _ @[norm_cast] theorem mul_to_nat : ∀ m n, ((m * n : num) : ℕ) = m * n | 0 0 := rfl | 0 (pos q) := (zero_mul _).symm | (pos p) 0 := rfl | (pos p) (pos q) := pos_num.mul_to_nat _ _ theorem cmp_to_nat : ∀ (m n), (ordering.cases_on (cmp m n) ((m:ℕ) < n) (m = n) ((m:ℕ) > n) : Prop) | 0 0 := rfl | 0 (pos b) := to_nat_pos _ | (pos a) 0 := to_nat_pos _ | (pos a) (pos b) := by { have := pos_num.cmp_to_nat a b; revert this; dsimp [cmp]; cases pos_num.cmp a b, exacts [id, congr_arg pos, id] } @[norm_cast] theorem lt_to_nat {m n : num} : (m:ℕ) < n ↔ m < n := show (m:ℕ) < n ↔ cmp m n = ordering.lt, from match cmp m n, cmp_to_nat m n with | ordering.lt, h := by simp at h; simp [h] | ordering.eq, h := by simp at h; simp [h, lt_irrefl]; exact dec_trivial | ordering.gt, h := by simp [not_lt_of_gt h]; exact dec_trivial end @[norm_cast] theorem le_to_nat {m n : num} : (m:ℕ) ≤ n ↔ m ≤ n := by rw ← not_lt; exact not_congr lt_to_nat end num namespace pos_num @[simp] theorem of_to_nat : Π (n : pos_num), ((n : ℕ) : num) = num.pos n | 1 := rfl | (bit0 p) := show ↑(p + p : ℕ) = num.pos p.bit0, by rw [num.add_of_nat, of_to_nat]; exact congr_arg num.pos p.bit0_of_bit0 | (bit1 p) := show ((p + p : ℕ) : num) + 1 = num.pos p.bit1, by rw [num.add_of_nat, of_to_nat]; exact congr_arg num.pos p.bit1_of_bit1 end pos_num namespace num @[simp, norm_cast] theorem of_to_nat : Π (n : num), ((n : ℕ) : num) = n | 0 := rfl | (pos p) := p.of_to_nat @[norm_cast] theorem to_nat_inj {m n : num} : (m : ℕ) = n ↔ m = n := ⟨λ h, function.left_inverse.injective of_to_nat h, congr_arg _⟩ meta def transfer_rw : tactic unit := `[repeat {rw ← to_nat_inj <|> rw ← lt_to_nat <|> rw ← le_to_nat}, repeat {rw add_to_nat <|> rw mul_to_nat <|> rw cast_one <|> rw cast_zero}] meta def transfer : tactic unit := `[intros, transfer_rw, try {simp}] instance : comm_semiring num := by refine { add := (+), zero := 0, zero_add := zero_add, add_zero := add_zero, mul := (*), one := 1, .. }; try {transfer}; simp [mul_add, mul_left_comm, mul_comm, add_comm] instance : ordered_cancel_add_comm_monoid num := { add_left_cancel := by {intros a b c, transfer_rw, apply add_left_cancel}, add_right_cancel := by {intros a b c, transfer_rw, apply add_right_cancel}, lt := (<), lt_iff_le_not_le := by {intros a b, transfer_rw, apply lt_iff_le_not_le}, le := (≤), le_refl := by transfer, le_trans := by {intros a b c, transfer_rw, apply le_trans}, le_antisymm := by {intros a b, transfer_rw, apply le_antisymm}, add_le_add_left := by {intros a b h c, revert h, transfer_rw, exact λ h, add_le_add_left h c}, le_of_add_le_add_left := by {intros a b c, transfer_rw, apply le_of_add_le_add_left}, ..num.comm_semiring } instance : decidable_linear_ordered_semiring num := { le_total := by {intros a b, transfer_rw, apply le_total}, zero_lt_one := dec_trivial, mul_lt_mul_of_pos_left := by {intros a b c, transfer_rw, apply mul_lt_mul_of_pos_left}, mul_lt_mul_of_pos_right := by {intros a b c, transfer_rw, apply mul_lt_mul_of_pos_right}, decidable_lt := num.decidable_lt, decidable_le := num.decidable_le, decidable_eq := num.decidable_eq, ..num.comm_semiring, ..num.ordered_cancel_add_comm_monoid } @[norm_cast] theorem dvd_to_nat (m n : num) : (m : ℕ) ∣ n ↔ m ∣ n := ⟨λ ⟨k, e⟩, ⟨k, by rw [← of_to_nat n, e]; simp⟩, λ ⟨k, e⟩, ⟨k, by simp [e, mul_to_nat]⟩⟩ end num namespace pos_num variables {α : Type*} open num @[norm_cast] theorem to_nat_inj {m n : pos_num} : (m : ℕ) = n ↔ m = n := ⟨λ h, num.pos.inj $ by rw [← pos_num.of_to_nat, ← pos_num.of_to_nat, h], congr_arg _⟩ theorem pred'_to_nat : ∀ n, (pred' n : ℕ) = nat.pred n | 1 := rfl | (bit0 n) := have nat.succ ↑(pred' n) = ↑n, by rw [pred'_to_nat n, nat.succ_pred_eq_of_pos (to_nat_pos n)], match pred' n, this : ∀ k : num, nat.succ ↑k = ↑n → ↑(num.cases_on k 1 bit1 : pos_num) = nat.pred (_root_.bit0 n) with | 0, (h : ((1:num):ℕ) = n) := by rw ← to_nat_inj.1 h; refl | num.pos p, (h : nat.succ ↑p = n) := by rw ← h; exact (nat.succ_add p p).symm end | (bit1 n) := rfl @[simp] theorem pred'_succ' (n) : pred' (succ' n) = n := num.to_nat_inj.1 $ by rw [pred'_to_nat, succ'_to_nat, nat.add_one, nat.pred_succ] @[simp] theorem succ'_pred' (n) : succ' (pred' n) = n := to_nat_inj.1 $ by rw [succ'_to_nat, pred'_to_nat, nat.add_one, nat.succ_pred_eq_of_pos (to_nat_pos _)] theorem size_to_nat : ∀ n, (size n : ℕ) = nat.size n | 1 := nat.size_one.symm | (bit0 n) := by rw [size, succ_to_nat, size_to_nat, cast_bit0, nat.size_bit0 $ ne_of_gt $ to_nat_pos n] | (bit1 n) := by rw [size, succ_to_nat, size_to_nat, cast_bit1, nat.size_bit1] theorem size_eq_nat_size : ∀ n, (size n : ℕ) = nat_size n | 1 := rfl | (bit0 n) := by rw [size, succ_to_nat, nat_size, size_eq_nat_size] | (bit1 n) := by rw [size, succ_to_nat, nat_size, size_eq_nat_size] theorem nat_size_to_nat (n) : nat_size n = nat.size n := by rw [← size_eq_nat_size, size_to_nat] theorem nat_size_pos (n) : 0 < nat_size n := by cases n; apply nat.succ_pos meta def transfer_rw : tactic unit := `[repeat {rw ← to_nat_inj <|> rw ← lt_to_nat <|> rw ← le_to_nat}, repeat {rw add_to_nat <|> rw mul_to_nat <|> rw cast_one <|> rw cast_zero}] meta def transfer : tactic unit := `[intros, transfer_rw, try {simp [add_comm, add_left_comm, mul_comm, mul_left_comm]}] instance : add_comm_semigroup pos_num := by refine {add := (+), ..}; transfer instance : comm_monoid pos_num := by refine {mul := (*), one := 1, ..}; transfer instance : distrib pos_num := by refine {add := (+), mul := (*), ..}; {transfer, simp [mul_add, mul_comm]} instance : decidable_linear_order pos_num := { lt := (<), lt_iff_le_not_le := by {intros a b, transfer_rw, apply lt_iff_le_not_le}, le := (≤), le_refl := by transfer, le_trans := by {intros a b c, transfer_rw, apply le_trans}, le_antisymm := by {intros a b, transfer_rw, apply le_antisymm}, le_total := by {intros a b, transfer_rw, apply le_total}, decidable_lt := by apply_instance, decidable_le := by apply_instance, decidable_eq := by apply_instance } @[simp] theorem cast_to_num (n : pos_num) : ↑n = num.pos n := by rw [← cast_to_nat, ← of_to_nat n] @[simp, norm_cast] theorem bit_to_nat (b n) : (bit b n : ℕ) = nat.bit b n := by cases b; refl @[simp, norm_cast] theorem cast_add [add_monoid α] [has_one α] (m n) : ((m + n : pos_num) : α) = m + n := by rw [← cast_to_nat, add_to_nat, nat.cast_add, cast_to_nat, cast_to_nat] @[simp, norm_cast, priority 500] theorem cast_succ [add_monoid α] [has_one α] (n : pos_num) : (succ n : α) = n + 1 := by rw [← add_one, cast_add, cast_one] @[simp, norm_cast] theorem cast_inj [add_monoid α] [has_one α] [char_zero α] {m n : pos_num} : (m:α) = n ↔ m = n := by rw [← cast_to_nat m, ← cast_to_nat n, nat.cast_inj, to_nat_inj] @[simp] theorem one_le_cast [linear_ordered_semiring α] (n : pos_num) : (1 : α) ≤ n := by rw [← cast_to_nat, ← nat.cast_one, nat.cast_le]; apply to_nat_pos @[simp] theorem cast_pos [linear_ordered_semiring α] (n : pos_num) : 0 < (n : α) := lt_of_lt_of_le zero_lt_one (one_le_cast n) @[simp, norm_cast] theorem cast_mul [semiring α] (m n) : ((m * n : pos_num) : α) = m * n := by rw [← cast_to_nat, mul_to_nat, nat.cast_mul, cast_to_nat, cast_to_nat] @[simp] theorem cmp_eq (m n) : cmp m n = ordering.eq ↔ m = n := begin have := cmp_to_nat m n, cases cmp m n; simp at this ⊢; try {exact this}; { simp [show m ≠ n, from λ e, by rw e at this; exact lt_irrefl _ this] } end @[simp, norm_cast] theorem cast_lt [linear_ordered_semiring α] {m n : pos_num} : (m:α) < n ↔ m < n := by rw [← cast_to_nat m, ← cast_to_nat n, nat.cast_lt, lt_to_nat] @[simp, norm_cast] theorem cast_le [linear_ordered_semiring α] {m n : pos_num} : (m:α) ≤ n ↔ m ≤ n := by rw ← not_lt; exact not_congr cast_lt end pos_num namespace num variables {α : Type*} open pos_num theorem bit_to_nat (b n) : (bit b n : ℕ) = nat.bit b n := by cases b; cases n; refl theorem cast_succ' [add_monoid α] [has_one α] (n) : (succ' n : α) = n + 1 := by rw [← pos_num.cast_to_nat, succ'_to_nat, nat.cast_add_one, cast_to_nat] theorem cast_succ [add_monoid α] [has_one α] (n) : (succ n : α) = n + 1 := cast_succ' n @[simp, norm_cast] theorem cast_add [semiring α] (m n) : ((m + n : num) : α) = m + n := by rw [← cast_to_nat, add_to_nat, nat.cast_add, cast_to_nat, cast_to_nat] @[simp, norm_cast] theorem cast_bit0 [semiring α] (n : num) : (n.bit0 : α) = _root_.bit0 n := by rw [← bit0_of_bit0, _root_.bit0, cast_add]; refl @[simp, norm_cast] theorem cast_bit1 [semiring α] (n : num) : (n.bit1 : α) = _root_.bit1 n := by rw [← bit1_of_bit1, _root_.bit1, bit0_of_bit0, cast_add, cast_bit0]; refl @[simp, norm_cast] theorem cast_mul [semiring α] : ∀ m n, ((m * n : num) : α) = m * n | 0 0 := (zero_mul _).symm | 0 (pos q) := (zero_mul _).symm | (pos p) 0 := (mul_zero _).symm | (pos p) (pos q) := pos_num.cast_mul _ _ theorem size_to_nat : ∀ n, (size n : ℕ) = nat.size n | 0 := nat.size_zero.symm | (pos p) := p.size_to_nat theorem size_eq_nat_size : ∀ n, (size n : ℕ) = nat_size n | 0 := rfl | (pos p) := p.size_eq_nat_size theorem nat_size_to_nat (n) : nat_size n = nat.size n := by rw [← size_eq_nat_size, size_to_nat] @[simp] theorem of_nat'_eq : ∀ n, num.of_nat' n = n := nat.binary_rec rfl $ λ b n IH, begin rw of_nat' at IH ⊢, rw [nat.binary_rec_eq, IH], { cases b; simp [nat.bit, bit0_of_bit0, bit1_of_bit1] }, { refl } end theorem zneg_to_znum (n : num) : -n.to_znum = n.to_znum_neg := by cases n; refl theorem zneg_to_znum_neg (n : num) : -n.to_znum_neg = n.to_znum := by cases n; refl theorem to_znum_inj {m n : num} : m.to_znum = n.to_znum ↔ m = n := ⟨λ h, by cases m; cases n; cases h; refl, congr_arg _⟩ @[simp, norm_cast squash] theorem cast_to_znum [has_zero α] [has_one α] [has_add α] [has_neg α] : ∀ n : num, (n.to_znum : α) = n | 0 := rfl | (num.pos p) := rfl @[simp] theorem cast_to_znum_neg [add_group α] [has_one α] : ∀ n : num, (n.to_znum_neg : α) = -n | 0 := neg_zero.symm | (num.pos p) := rfl @[simp] theorem add_to_znum (m n : num) : num.to_znum (m + n) = m.to_znum + n.to_znum := by cases m; cases n; refl end num namespace pos_num open num theorem pred_to_nat {n : pos_num} (h : n > 1) : (pred n : ℕ) = nat.pred n := begin unfold pred, have := pred'_to_nat n, cases e : pred' n, { have : (1:ℕ) ≤ nat.pred n := nat.pred_le_pred ((@cast_lt ℕ _ _ _).2 h), rw [← pred'_to_nat, e] at this, exact absurd this dec_trivial }, { rw [← pred'_to_nat, e], refl } end theorem sub'_one (a : pos_num) : sub' a 1 = (pred' a).to_znum := by cases a; refl theorem one_sub' (a : pos_num) : sub' 1 a = (pred' a).to_znum_neg := by cases a; refl theorem lt_iff_cmp {m n} : m < n ↔ cmp m n = ordering.lt := iff.rfl theorem le_iff_cmp {m n} : m ≤ n ↔ cmp m n ≠ ordering.gt := not_congr $ lt_iff_cmp.trans $ by rw ← cmp_swap; cases cmp m n; exact dec_trivial end pos_num namespace num variables {α : Type*} open pos_num theorem pred_to_nat : ∀ (n : num), (pred n : ℕ) = nat.pred n | 0 := rfl | (pos p) := by rw [pred, pos_num.pred'_to_nat]; refl theorem ppred_to_nat : ∀ (n : num), coe <$> ppred n = nat.ppred n | 0 := rfl | (pos p) := by rw [ppred, option.map_some, nat.ppred_eq_some.2]; rw [pos_num.pred'_to_nat, nat.succ_pred_eq_of_pos (pos_num.to_nat_pos _)]; refl theorem cmp_swap (m n) : (cmp m n).swap = cmp n m := by cases m; cases n; try {unfold cmp}; try {refl}; apply pos_num.cmp_swap theorem cmp_eq (m n) : cmp m n = ordering.eq ↔ m = n := begin have := cmp_to_nat m n, cases cmp m n; simp at this ⊢; try {exact this}; { simp [show m ≠ n, from λ e, by rw e at this; exact lt_irrefl _ this] } end @[simp, norm_cast] theorem cast_lt [linear_ordered_semiring α] {m n : num} : (m:α) < n ↔ m < n := by rw [← cast_to_nat m, ← cast_to_nat n, nat.cast_lt, lt_to_nat] @[simp, norm_cast] theorem cast_le [linear_ordered_semiring α] {m n : num} : (m:α) ≤ n ↔ m ≤ n := by rw ← not_lt; exact not_congr cast_lt @[simp, norm_cast] theorem cast_inj [linear_ordered_semiring α] {m n : num} : (m:α) = n ↔ m = n := by rw [← cast_to_nat m, ← cast_to_nat n, nat.cast_inj, to_nat_inj] theorem lt_iff_cmp {m n} : m < n ↔ cmp m n = ordering.lt := iff.rfl theorem le_iff_cmp {m n} : m ≤ n ↔ cmp m n ≠ ordering.gt := not_congr $ lt_iff_cmp.trans $ by rw ← cmp_swap; cases cmp m n; exact dec_trivial theorem bitwise_to_nat {f : num → num → num} {g : bool → bool → bool} (p : pos_num → pos_num → num) (gff : g ff ff = ff) (f00 : f 0 0 = 0) (f0n : ∀ n, f 0 (pos n) = cond (g ff tt) (pos n) 0) (fn0 : ∀ n, f (pos n) 0 = cond (g tt ff) (pos n) 0) (fnn : ∀ m n, f (pos m) (pos n) = p m n) (p11 : p 1 1 = cond (g tt tt) 1 0) (p1b : ∀ b n, p 1 (pos_num.bit b n) = bit (g tt b) (cond (g ff tt) (pos n) 0)) (pb1 : ∀ a m, p (pos_num.bit a m) 1 = bit (g a tt) (cond (g tt ff) (pos m) 0)) (pbb : ∀ a b m n, p (pos_num.bit a m) (pos_num.bit b n) = bit (g a b) (p m n)) : ∀ m n : num, (f m n : ℕ) = nat.bitwise g m n := begin intros, cases m with m; cases n with n; try { change zero with 0 }; try { change ((0:num):ℕ) with 0 }, { rw [f00, nat.bitwise_zero]; refl }, { unfold nat.bitwise, rw [f0n, nat.binary_rec_zero], cases g ff tt; refl }, { unfold nat.bitwise, generalize h : (pos m : ℕ) = m', revert h, apply nat.bit_cases_on m' _, intros b m' h, rw [fn0, nat.binary_rec_eq, nat.binary_rec_zero, ←h], cases g tt ff; refl, apply nat.bitwise_bit_aux gff }, { rw fnn, have : ∀b (n : pos_num), (cond b ↑n 0 : ℕ) = ↑(cond b (pos n) 0 : num) := by intros; cases b; refl, induction m with m IH m IH generalizing n; cases n with n n, any_goals { change one with 1 }, any_goals { change pos 1 with 1 }, any_goals { change pos_num.bit0 with pos_num.bit ff }, any_goals { change pos_num.bit1 with pos_num.bit tt }, any_goals { change ((1:num):ℕ) with nat.bit tt 0 }, all_goals { repeat { rw show ∀ b n, (pos (pos_num.bit b n) : ℕ) = nat.bit b ↑n, by intros; cases b; refl }, rw nat.bitwise_bit }, any_goals { assumption }, any_goals { rw [nat.bitwise_zero, p11], cases g tt tt; refl }, any_goals { rw [nat.bitwise_zero_left, this, ← bit_to_nat, p1b] }, any_goals { rw [nat.bitwise_zero_right _ gff, this, ← bit_to_nat, pb1] }, all_goals { rw [← show ∀ n, ↑(p m n) = nat.bitwise g ↑m ↑n, from IH], rw [← bit_to_nat, pbb] } } end @[simp, norm_cast] theorem lor_to_nat : ∀ m n, (lor m n : ℕ) = nat.lor m n := by apply bitwise_to_nat (λx y, pos (pos_num.lor x y)); intros; try {cases a}; try {cases b}; refl @[simp, norm_cast] theorem land_to_nat : ∀ m n, (land m n : ℕ) = nat.land m n := by apply bitwise_to_nat pos_num.land; intros; try {cases a}; try {cases b}; refl @[simp, norm_cast] theorem ldiff_to_nat : ∀ m n, (ldiff m n : ℕ) = nat.ldiff m n := by apply bitwise_to_nat pos_num.ldiff; intros; try {cases a}; try {cases b}; refl @[simp, norm_cast] theorem lxor_to_nat : ∀ m n, (lxor m n : ℕ) = nat.lxor m n := by apply bitwise_to_nat pos_num.lxor; intros; try {cases a}; try {cases b}; refl @[simp, norm_cast] theorem shiftl_to_nat (m n) : (shiftl m n : ℕ) = nat.shiftl m n := begin cases m; dunfold shiftl, {symmetry, apply nat.zero_shiftl}, simp, induction n with n IH, {refl}, simp [pos_num.shiftl, nat.shiftl_succ], rw ←IH end @[simp, norm_cast] theorem shiftr_to_nat (m n) : (shiftr m n : ℕ) = nat.shiftr m n := begin cases m with m; dunfold shiftr, {symmetry, apply nat.zero_shiftr}, induction n with n IH generalizing m, {cases m; refl}, cases m with m m; dunfold pos_num.shiftr, { rw [nat.shiftr_eq_div_pow], symmetry, apply nat.div_eq_of_lt, exact @nat.pow_lt_pow_of_lt_right 2 dec_trivial 0 (n+1) (nat.succ_pos _) }, { transitivity, apply IH, change nat.shiftr m n = nat.shiftr (bit1 m) (n+1), rw [add_comm n 1, nat.shiftr_add], apply congr_arg (λx, nat.shiftr x n), unfold nat.shiftr, change (bit1 ↑m : ℕ) with nat.bit tt m, rw nat.div2_bit }, { transitivity, apply IH, change nat.shiftr m n = nat.shiftr (bit0 m) (n + 1), rw [add_comm n 1, nat.shiftr_add], apply congr_arg (λx, nat.shiftr x n), unfold nat.shiftr, change (bit0 ↑m : ℕ) with nat.bit ff m, rw nat.div2_bit } end @[simp] theorem test_bit_to_nat (m n) : test_bit m n = nat.test_bit m n := begin cases m with m; unfold test_bit nat.test_bit, { change (zero : nat) with 0, rw nat.zero_shiftr, refl }, induction n with n IH generalizing m; cases m; dunfold pos_num.test_bit, {refl}, { exact (nat.bodd_bit _ _).symm }, { exact (nat.bodd_bit _ _).symm }, { change ff = nat.bodd (nat.shiftr 1 (n + 1)), rw [add_comm, nat.shiftr_add], change nat.shiftr 1 1 with 0, rw nat.zero_shiftr; refl }, { change pos_num.test_bit m n = nat.bodd (nat.shiftr (nat.bit tt m) (n + 1)), rw [add_comm, nat.shiftr_add], unfold nat.shiftr, rw nat.div2_bit, apply IH }, { change pos_num.test_bit m n = nat.bodd (nat.shiftr (nat.bit ff m) (n + 1)), rw [add_comm, nat.shiftr_add], unfold nat.shiftr, rw nat.div2_bit, apply IH }, end end num namespace znum variables {α : Type*} open pos_num @[simp, norm_cast] theorem cast_zero [has_zero α] [has_one α] [has_add α] [has_neg α] : ((0 : znum) : α) = 0 := rfl @[simp] theorem cast_zero' [has_zero α] [has_one α] [has_add α] [has_neg α] : (znum.zero : α) = 0 := rfl @[simp, norm_cast] theorem cast_one [has_zero α] [has_one α] [has_add α] [has_neg α] : ((1 : znum) : α) = 1 := rfl @[simp] theorem cast_pos [has_zero α] [has_one α] [has_add α] [has_neg α] (n : pos_num) : (pos n : α) = n := rfl @[simp] theorem cast_neg [has_zero α] [has_one α] [has_add α] [has_neg α] (n : pos_num) : (neg n : α) = -n := rfl @[simp, norm_cast] theorem cast_zneg [add_group α] [has_one α] : ∀ n, ((-n : znum) : α) = -n | 0 := neg_zero.symm | (pos p) := rfl | (neg p) := (neg_neg _).symm theorem neg_zero : (-0 : znum) = 0 := rfl theorem zneg_pos (n : pos_num) : -pos n = neg n := rfl theorem zneg_neg (n : pos_num) : -neg n = pos n := rfl theorem zneg_zneg (n : znum) : - -n = n := by cases n; refl theorem zneg_bit1 (n : znum) : -n.bit1 = (-n).bitm1 := by cases n; refl theorem zneg_bitm1 (n : znum) : -n.bitm1 = (-n).bit1 := by cases n; refl theorem zneg_succ (n : znum) : -n.succ = (-n).pred := by cases n; try {refl}; rw [succ, num.zneg_to_znum_neg]; refl theorem zneg_pred (n : znum) : -n.pred = (-n).succ := by rw [← zneg_zneg (succ (-n)), zneg_succ, zneg_zneg] @[simp, norm_cast] theorem neg_of_int : ∀ n, ((-n : ℤ) : znum) = -n | (n+1:ℕ) := rfl | 0 := rfl | -[1+n] := (zneg_zneg _).symm @[simp] theorem abs_to_nat : ∀ n, (abs n : ℕ) = int.nat_abs n | 0 := rfl | (pos p) := congr_arg int.nat_abs p.to_nat_to_int | (neg p) := show int.nat_abs ((p:ℕ):ℤ) = int.nat_abs (- p), by rw [p.to_nat_to_int, int.nat_abs_neg] @[simp] theorem abs_to_znum : ∀ n : num, abs n.to_znum = n | 0 := rfl | (num.pos p) := rfl @[simp, norm_cast] theorem cast_to_int [add_group α] [has_one α] : ∀ n : znum, ((n : ℤ) : α) = n | 0 := rfl | (pos p) := by rw [cast_pos, cast_pos, pos_num.cast_to_int] | (neg p) := by rw [cast_neg, cast_neg, int.cast_neg, pos_num.cast_to_int] theorem bit0_of_bit0 : ∀ n : znum, _root_.bit0 n = n.bit0 | 0 := rfl | (pos a) := congr_arg pos a.bit0_of_bit0 | (neg a) := congr_arg neg a.bit0_of_bit0 theorem bit1_of_bit1 : ∀ n : znum, _root_.bit1 n = n.bit1 | 0 := rfl | (pos a) := congr_arg pos a.bit1_of_bit1 | (neg a) := show pos_num.sub' 1 (_root_.bit0 a) = _, by rw [pos_num.one_sub', a.bit0_of_bit0]; refl @[simp, norm_cast] theorem cast_bit0 [add_group α] [has_one α] : ∀ n : znum, (n.bit0 : α) = bit0 n | 0 := (add_zero _).symm | (pos p) := by rw [znum.bit0, cast_pos, cast_pos]; refl | (neg p) := by rw [znum.bit0, cast_neg, cast_neg, pos_num.cast_bit0, _root_.bit0, _root_.bit0, neg_add_rev] @[simp, norm_cast] theorem cast_bit1 [add_group α] [has_one α] : ∀ n : znum, (n.bit1 : α) = bit1 n | 0 := by simp [znum.bit1, _root_.bit1, _root_.bit0] | (pos p) := by rw [znum.bit1, cast_pos, cast_pos]; refl | (neg p) := begin rw [znum.bit1, cast_neg, cast_neg], cases e : pred' p; have : p = _ := (succ'_pred' p).symm.trans (congr_arg num.succ' e), { change p=1 at this, subst p, simp [_root_.bit1, _root_.bit0] }, { rw [num.succ'] at this, subst p, have : (↑(-↑a:ℤ) : α) = -1 + ↑(-↑a + 1 : ℤ), {simp [add_comm]}, simpa [_root_.bit1, _root_.bit0, -add_comm] }, end @[simp] theorem cast_bitm1 [add_group α] [has_one α] (n : znum) : (n.bitm1 : α) = bit0 n - 1 := begin conv { to_lhs, rw ← zneg_zneg n }, rw [← zneg_bit1, cast_zneg, cast_bit1], have : ((-1 + n + n : ℤ) : α) = (n + n + -1 : ℤ), {simp [add_comm, add_left_comm]}, simpa [_root_.bit1, _root_.bit0, sub_eq_add_neg] end theorem add_zero (n : znum) : n + 0 = n := by cases n; refl theorem zero_add (n : znum) : 0 + n = n := by cases n; refl theorem add_one : ∀ n : znum, n + 1 = succ n | 0 := rfl | (pos p) := congr_arg pos p.add_one | (neg p) := by cases p; refl end znum namespace pos_num variables {α : Type*} theorem cast_to_znum : ∀ n : pos_num, (n : znum) = znum.pos n | 1 := rfl | (bit0 p) := (znum.bit0_of_bit0 p).trans $ congr_arg _ (cast_to_znum p) | (bit1 p) := (znum.bit1_of_bit1 p).trans $ congr_arg _ (cast_to_znum p) theorem cast_sub' [add_group α] [has_one α] : ∀ m n : pos_num, (sub' m n : α) = m - n | a 1 := by rw [sub'_one, num.cast_to_znum, ← num.cast_to_nat, pred'_to_nat, ← nat.sub_one]; simp [pos_num.cast_pos] | 1 b := by rw [one_sub', num.cast_to_znum_neg, ← neg_sub, neg_inj', ← num.cast_to_nat, pred'_to_nat, ← nat.sub_one]; simp [pos_num.cast_pos] | (bit0 a) (bit0 b) := begin rw [sub', znum.cast_bit0, cast_sub'], have : ((a + -b + (a + -b) : ℤ) : α) = a + a + (-b + -b), {simp [add_left_comm]}, simpa [_root_.bit0, sub_eq_add_neg] end | (bit0 a) (bit1 b) := begin rw [sub', znum.cast_bitm1, cast_sub'], have : ((-b + (a + (-b + -1)) : ℤ) : α) = (a + -1 + (-b + -b):ℤ), { simp [add_comm, add_left_comm] }, simpa [_root_.bit1, _root_.bit0, sub_eq_add_neg] end | (bit1 a) (bit0 b) := begin rw [sub', znum.cast_bit1, cast_sub'], have : ((-b + (a + (-b + 1)) : ℤ) : α) = (a + 1 + (-b + -b):ℤ), { simp [add_comm, add_left_comm] }, simpa [_root_.bit1, _root_.bit0, sub_eq_add_neg] end | (bit1 a) (bit1 b) := begin rw [sub', znum.cast_bit0, cast_sub'], have : ((-b + (a + -b) : ℤ) : α) = a + (-b + -b), {simp [add_left_comm]}, simpa [_root_.bit1, _root_.bit0, sub_eq_add_neg] end theorem to_nat_eq_succ_pred (n : pos_num) : (n:ℕ) = n.pred' + 1 := by rw [← num.succ'_to_nat, n.succ'_pred'] theorem to_int_eq_succ_pred (n : pos_num) : (n:ℤ) = (n.pred' : ℕ) + 1 := by rw [← n.to_nat_to_int, to_nat_eq_succ_pred]; refl end pos_num namespace num variables {α : Type*} @[simp] theorem cast_sub' [add_group α] [has_one α] : ∀ m n : num, (sub' m n : α) = m - n | 0 0 := (sub_zero _).symm | (pos a) 0 := (sub_zero _).symm | 0 (pos b) := (zero_sub _).symm | (pos a) (pos b) := pos_num.cast_sub' _ _ @[simp] theorem of_nat_to_znum : ∀ n : ℕ, to_znum n = n | 0 := rfl | (n+1) := by rw [nat.cast_add_one, nat.cast_add_one, znum.add_one, add_one, ← of_nat_to_znum]; cases (n:num); refl @[simp] theorem of_nat_to_znum_neg (n : ℕ) : to_znum_neg n = -n := by rw [← of_nat_to_znum, zneg_to_znum] theorem mem_of_znum' : ∀ {m : num} {n : znum}, m ∈ of_znum' n ↔ n = to_znum m | 0 0 := ⟨λ _, rfl, λ _, rfl⟩ | (pos m) 0 := ⟨λ h, by cases h, λ h, by cases h⟩ | m (znum.pos p) := option.some_inj.trans $ by cases m; split; intro h; try {cases h}; refl | m (znum.neg p) := ⟨λ h, by cases h, λ h, by cases m; cases h⟩ theorem of_znum'_to_nat : ∀ (n : znum), coe <$> of_znum' n = int.to_nat' n | 0 := rfl | (znum.pos p) := show _ = int.to_nat' p, by rw [← pos_num.to_nat_to_int p]; refl | (znum.neg p) := congr_arg (λ x, int.to_nat' (-x)) $ show ((p.pred' + 1 : ℕ) : ℤ) = p, by rw ← succ'_to_nat; simp @[simp] theorem of_znum_to_nat : ∀ (n : znum), (of_znum n : ℕ) = int.to_nat n | 0 := rfl | (znum.pos p) := show _ = int.to_nat p, by rw [← pos_num.to_nat_to_int p]; refl | (znum.neg p) := congr_arg (λ x, int.to_nat (-x)) $ show ((p.pred' + 1 : ℕ) : ℤ) = p, by rw ← succ'_to_nat; simp @[simp] theorem cast_of_znum [add_group α] [has_one α] (n : znum) : (of_znum n : α) = int.to_nat n := by rw [← cast_to_nat, of_znum_to_nat] @[simp, norm_cast] theorem sub_to_nat (m n) : ((m - n : num) : ℕ) = m - n := show (of_znum _ : ℕ) = _, by rw [of_znum_to_nat, cast_sub', ← to_nat_to_int, ← to_nat_to_int, int.to_nat_sub] end num namespace znum variables {α : Type*} @[simp, norm_cast] theorem cast_add [add_group α] [has_one α] : ∀ m n, ((m + n : znum) : α) = m + n | 0 a := by cases a; exact (_root_.zero_add _).symm | b 0 := by cases b; exact (_root_.add_zero _).symm | (pos a) (pos b) := pos_num.cast_add _ _ | (pos a) (neg b) := pos_num.cast_sub' _ _ | (neg a) (pos b) := (pos_num.cast_sub' _ _).trans $ show ↑b + -↑a = -↑a + ↑b, by rw [← pos_num.cast_to_int a, ← pos_num.cast_to_int b, ← int.cast_neg, ← int.cast_add (-a)]; simp [add_comm] | (neg a) (neg b) := show -(↑(a + b) : α) = -a + -b, by rw [ pos_num.cast_add, neg_eq_iff_neg_eq, neg_add_rev, neg_neg, neg_neg, ← pos_num.cast_to_int a, ← pos_num.cast_to_int b, ← int.cast_add]; simp [add_comm] @[simp] theorem cast_succ [add_group α] [has_one α] (n) : ((succ n : znum) : α) = n + 1 := by rw [← add_one, cast_add, cast_one] @[simp, norm_cast] theorem mul_to_int : ∀ m n, ((m * n : znum) : ℤ) = m * n | 0 a := by cases a; exact (_root_.zero_mul _).symm | b 0 := by cases b; exact (_root_.mul_zero _).symm | (pos a) (pos b) := pos_num.cast_mul a b | (pos a) (neg b) := show -↑(a * b) = ↑a * -↑b, by rw [pos_num.cast_mul, neg_mul_eq_mul_neg] | (neg a) (pos b) := show -↑(a * b) = -↑a * ↑b, by rw [pos_num.cast_mul, neg_mul_eq_neg_mul] | (neg a) (neg b) := show ↑(a * b) = -↑a * -↑b, by rw [pos_num.cast_mul, neg_mul_neg] theorem cast_mul [ring α] (m n) : ((m * n : znum) : α) = m * n := by rw [← cast_to_int, mul_to_int, int.cast_mul, cast_to_int, cast_to_int] @[simp, norm_cast] theorem of_to_int : Π (n : znum), ((n : ℤ) : znum) = n | 0 := rfl | (pos a) := by rw [cast_pos, ← pos_num.cast_to_nat, int.cast_coe_nat', ← num.of_nat_to_znum, pos_num.of_to_nat]; refl | (neg a) := by rw [cast_neg, neg_of_int, ← pos_num.cast_to_nat, int.cast_coe_nat', ← num.of_nat_to_znum_neg, pos_num.of_to_nat]; refl @[norm_cast] theorem to_of_int : Π (n : ℤ), ((n : znum) : ℤ) = n | (n : ℕ) := by rw [int.cast_coe_nat, ← num.of_nat_to_znum, num.cast_to_znum, ← num.cast_to_nat, int.nat_cast_eq_coe_nat, num.to_of_nat] | -[1+ n] := by rw [int.cast_neg_succ_of_nat, cast_zneg, add_one, cast_succ, int.neg_succ_of_nat_eq, ← num.of_nat_to_znum, num.cast_to_znum, ← num.cast_to_nat, int.nat_cast_eq_coe_nat, num.to_of_nat] theorem to_int_inj {m n : znum} : (m : ℤ) = n ↔ m = n := ⟨λ h, function.left_inverse.injective of_to_int h, congr_arg _⟩ @[simp, norm_cast] theorem of_int_cast [add_group α] [has_one α] (n : ℤ) : ((n : znum) : α) = n := by rw [← cast_to_int, to_of_int] @[simp, norm_cast] theorem of_nat_cast [add_group α] [has_one α] (n : ℕ) : ((n : znum) : α) = n := of_int_cast n @[simp] theorem of_int'_eq : ∀ n, znum.of_int' n = n | (n : ℕ) := to_int_inj.1 $ by simp [znum.of_int'] | -[1+ n] := to_int_inj.1 $ by simp [znum.of_int'] theorem cmp_to_int : ∀ (m n), (ordering.cases_on (cmp m n) ((m:ℤ) < n) (m = n) ((m:ℤ) > n) : Prop) | 0 0 := rfl | (pos a) (pos b) := begin have := pos_num.cmp_to_nat a b; revert this; dsimp [cmp]; cases pos_num.cmp a b; dsimp; [simp, exact congr_arg pos, simp [gt]] end | (neg a) (neg b) := begin have := pos_num.cmp_to_nat b a; revert this; dsimp [cmp]; cases pos_num.cmp b a; dsimp; [simp, simp {contextual := tt}, simp [gt]] end | (pos a) 0 := pos_num.cast_pos _ | (pos a) (neg b) := lt_trans (neg_lt_zero.2 $ pos_num.cast_pos _) (pos_num.cast_pos _) | 0 (neg b) := neg_lt_zero.2 $ pos_num.cast_pos _ | (neg a) 0 := neg_lt_zero.2 $ pos_num.cast_pos _ | (neg a) (pos b) := lt_trans (neg_lt_zero.2 $ pos_num.cast_pos _) (pos_num.cast_pos _) | 0 (pos b) := pos_num.cast_pos _ @[norm_cast] theorem lt_to_int {m n : znum} : (m:ℤ) < n ↔ m < n := show (m:ℤ) < n ↔ cmp m n = ordering.lt, from match cmp m n, cmp_to_int m n with | ordering.lt, h := by simp at h; simp [h] | ordering.eq, h := by simp at h; simp [h, lt_irrefl]; exact dec_trivial | ordering.gt, h := by simp [not_lt_of_gt h]; exact dec_trivial end theorem le_to_int {m n : znum} : (m:ℤ) ≤ n ↔ m ≤ n := by rw ← not_lt; exact not_congr lt_to_int @[simp, norm_cast] theorem cast_lt [linear_ordered_ring α] {m n : znum} : (m:α) < n ↔ m < n := by rw [← cast_to_int m, ← cast_to_int n, int.cast_lt, lt_to_int] @[simp, norm_cast] theorem cast_le [linear_ordered_ring α] {m n : znum} : (m:α) ≤ n ↔ m ≤ n := by rw ← not_lt; exact not_congr cast_lt @[simp, norm_cast] theorem cast_inj [linear_ordered_ring α] {m n : znum} : (m:α) = n ↔ m = n := by rw [← cast_to_int m, ← cast_to_int n, int.cast_inj, to_int_inj] meta def transfer_rw : tactic unit := `[repeat {rw ← to_int_inj <|> rw ← lt_to_int <|> rw ← le_to_int}, repeat {rw cast_add <|> rw mul_to_int <|> rw cast_one <|> rw cast_zero}] meta def transfer : tactic unit := `[intros, transfer_rw, try {simp [add_comm, add_left_comm, mul_comm, mul_left_comm]}] instance : decidable_linear_order znum := { lt := (<), lt_iff_le_not_le := by {intros a b, transfer_rw, apply lt_iff_le_not_le}, le := (≤), le_refl := by transfer, le_trans := by {intros a b c, transfer_rw, apply le_trans}, le_antisymm := by {intros a b, transfer_rw, apply le_antisymm}, le_total := by {intros a b, transfer_rw, apply le_total}, decidable_eq := znum.decidable_eq, decidable_le := znum.decidable_le, decidable_lt := znum.decidable_lt } instance : add_comm_group znum := { add := (+), add_assoc := by transfer, zero := 0, zero_add := zero_add, add_zero := add_zero, add_comm := by transfer, neg := has_neg.neg, add_left_neg := by transfer } instance : decidable_linear_ordered_comm_ring znum := { mul := (*), mul_assoc := by transfer, one := 1, one_mul := by transfer, mul_one := by transfer, left_distrib := by {transfer, simp [mul_add]}, right_distrib := by {transfer, simp [mul_add, mul_comm]}, mul_comm := by transfer, zero_ne_one := dec_trivial, add_le_add_left := by {intros a b h c, revert h, transfer_rw, exact λ h, add_le_add_left h c}, mul_pos := λ a b, show 0 < a → 0 < b → 0 < a * b, by {transfer_rw, apply mul_pos}, zero_lt_one := dec_trivial, ..znum.decidable_linear_order, ..znum.add_comm_group } @[simp, norm_cast] theorem dvd_to_int (m n : znum) : (m : ℤ) ∣ n ↔ m ∣ n := ⟨λ ⟨k, e⟩, ⟨k, by rw [← of_to_int n, e]; simp⟩, λ ⟨k, e⟩, ⟨k, by simp [e]⟩⟩ end znum namespace pos_num theorem divmod_to_nat_aux {n d : pos_num} {q r : num} (h₁ : (r:ℕ) + d * _root_.bit0 q = n) (h₂ : (r:ℕ) < 2 * d) : ((divmod_aux d q r).2 + d * (divmod_aux d q r).1 : ℕ) = ↑n ∧ ((divmod_aux d q r).2 : ℕ) < d := begin unfold divmod_aux, have : ∀ {r₂}, num.of_znum' (num.sub' r (num.pos d)) = some r₂ ↔ (r : ℕ) = r₂ + d, { intro r₂, apply num.mem_of_znum'.trans, rw [← znum.to_int_inj, num.cast_to_znum, num.cast_sub', sub_eq_iff_eq_add, ← int.coe_nat_inj'], simp }, cases e : num.of_znum' (num.sub' r (num.pos d)) with r₂; simp [divmod_aux], { refine ⟨h₁, lt_of_not_ge (λ h, _)⟩, cases nat.le.dest h with r₂ e', rw [← num.to_of_nat r₂, add_comm] at e', cases e.symm.trans (this.2 e'.symm) }, { have := this.1 e, split, { rwa [_root_.bit1, add_comm _ 1, mul_add, mul_one, ← add_assoc, ← this] }, { rwa [this, two_mul, add_lt_add_iff_right] at h₂ } } end theorem divmod_to_nat (d n : pos_num) : (n / d : ℕ) = (divmod d n).1 ∧ (n % d : ℕ) = (divmod d n).2 := begin rw nat.div_mod_unique (pos_num.cast_pos _), induction n with n IH n IH, { exact divmod_to_nat_aux (by simp; refl) (nat.mul_le_mul_left 2 (pos_num.cast_pos d : (0 : ℕ) < d)) }, { unfold divmod, cases divmod d n with q r, simp only [divmod] at IH ⊢, apply divmod_to_nat_aux; simp, { rw [_root_.bit1, _root_.bit1, add_right_comm, bit0_eq_two_mul ↑n, ← IH.1, mul_add, ← bit0_eq_two_mul, mul_left_comm, ← bit0_eq_two_mul] }, { rw ← bit0_eq_two_mul, exact nat.bit1_lt_bit0 IH.2 } }, { unfold divmod, cases divmod d n with q r, simp only [divmod] at IH ⊢, apply divmod_to_nat_aux; simp, { rw [bit0_eq_two_mul ↑n, ← IH.1, mul_add, ← bit0_eq_two_mul, mul_left_comm, ← bit0_eq_two_mul] }, { rw ← bit0_eq_two_mul, exact nat.bit0_lt IH.2 } } end @[simp] theorem div'_to_nat (n d) : (div' n d : ℕ) = n / d := (divmod_to_nat _ _).1.symm @[simp] theorem mod'_to_nat (n d) : (mod' n d : ℕ) = n % d := (divmod_to_nat _ _).2.symm end pos_num namespace num @[simp, norm_cast] theorem div_to_nat : ∀ n d, ((n / d : num) : ℕ) = n / d | 0 0 := rfl | 0 (pos d) := (nat.zero_div _).symm | (pos n) 0 := (nat.div_zero _).symm | (pos n) (pos d) := pos_num.div'_to_nat _ _ @[simp, norm_cast] theorem mod_to_nat : ∀ n d, ((n % d : num) : ℕ) = n % d | 0 0 := rfl | 0 (pos d) := (nat.zero_mod _).symm | (pos n) 0 := (nat.mod_zero _).symm | (pos n) (pos d) := pos_num.mod'_to_nat _ _ theorem gcd_to_nat_aux : ∀ {n} {a b : num}, a ≤ b → (a * b).nat_size ≤ n → (gcd_aux n a b : ℕ) = nat.gcd a b | 0 0 b ab h := (nat.gcd_zero_left _).symm | 0 (pos a) 0 ab h := (not_lt_of_ge ab).elim rfl | 0 (pos a) (pos b) ab h := (not_lt_of_le h).elim $ pos_num.nat_size_pos _ | (nat.succ n) 0 b ab h := (nat.gcd_zero_left _).symm | (nat.succ n) (pos a) b ab h := begin simp [gcd_aux], rw [nat.gcd_rec, gcd_to_nat_aux, mod_to_nat], {refl}, { rw [← le_to_nat, mod_to_nat], exact le_of_lt (nat.mod_lt _ (pos_num.cast_pos _)) }, rw [nat_size_to_nat, mul_to_nat, nat.size_le] at h ⊢, rw [mod_to_nat, mul_comm], rw [nat.pow_succ, ← nat.mod_add_div b (pos a)] at h, refine lt_of_mul_lt_mul_right (lt_of_le_of_lt _ h) (nat.zero_le 2), rw [mul_two, mul_add], refine add_le_add_left (nat.mul_le_mul_left _ (le_trans (le_of_lt (nat.mod_lt _ (pos_num.cast_pos _))) _)) _, suffices : 1 ≤ _, simpa using nat.mul_le_mul_left (pos a) this, rw [nat.le_div_iff_mul_le _ _ a.cast_pos, one_mul], exact le_to_nat.2 ab end @[simp] theorem gcd_to_nat : ∀ a b, (gcd a b : ℕ) = nat.gcd a b := have ∀ a b : num, (a * b).nat_size ≤ a.nat_size + b.nat_size, begin intros, simp [nat_size_to_nat], rw [nat.size_le, nat.pow_add], exact mul_lt_mul'' (nat.lt_size_self _) (nat.lt_size_self _) (nat.zero_le _) (nat.zero_le _) end, begin intros, unfold gcd, split_ifs, { exact gcd_to_nat_aux h (this _ _) }, { rw nat.gcd_comm, exact gcd_to_nat_aux (le_of_not_le h) (this _ _) } end theorem dvd_iff_mod_eq_zero {m n : num} : m ∣ n ↔ n % m = 0 := by rw [← dvd_to_nat, nat.dvd_iff_mod_eq_zero, ← to_nat_inj, mod_to_nat]; refl instance : decidable_rel ((∣) : num → num → Prop) | a b := decidable_of_iff' _ dvd_iff_mod_eq_zero end num namespace znum @[simp, norm_cast] theorem div_to_int : ∀ n d, ((n / d : znum) : ℤ) = n / d | 0 0 := rfl | 0 (pos d) := (int.zero_div _).symm | 0 (neg d) := (int.zero_div _).symm | (pos n) 0 := (int.div_zero _).symm | (neg n) 0 := (int.div_zero _).symm | (pos n) (pos d) := (num.cast_to_znum _).trans $ by rw ← num.to_nat_to_int; simp | (pos n) (neg d) := (num.cast_to_znum_neg _).trans $ by rw ← num.to_nat_to_int; simp | (neg n) (pos d) := show - _ = (-_/↑d), begin rw [n.to_int_eq_succ_pred, d.to_int_eq_succ_pred, ← pos_num.to_nat_to_int, num.succ'_to_nat, num.div_to_nat], change -[1+ n.pred' / ↑d] = -[1+ n.pred' / (d.pred' + 1)], rw d.to_nat_eq_succ_pred end | (neg n) (neg d) := show ↑(pos_num.pred' n / num.pos d).succ' = (-_ / -↑d), begin rw [n.to_int_eq_succ_pred, d.to_int_eq_succ_pred, ← pos_num.to_nat_to_int, num.succ'_to_nat, num.div_to_nat], change (nat.succ (_/d) : ℤ) = nat.succ (n.pred'/(d.pred' + 1)), rw d.to_nat_eq_succ_pred end @[simp, norm_cast] theorem mod_to_int : ∀ n d, ((n % d : znum) : ℤ) = n % d | 0 d := (int.zero_mod _).symm | (pos n) d := (num.cast_to_znum _).trans $ by rw [← num.to_nat_to_int, cast_pos, num.mod_to_nat, ← pos_num.to_nat_to_int, abs_to_nat]; refl | (neg n) d := (num.cast_sub' _ _).trans $ by rw [← num.to_nat_to_int, cast_neg, ← num.to_nat_to_int, num.succ_to_nat, num.mod_to_nat, abs_to_nat, ← int.sub_nat_nat_eq_coe, n.to_int_eq_succ_pred]; refl @[simp] theorem gcd_to_nat (a b) : (gcd a b : ℕ) = int.gcd a b := (num.gcd_to_nat _ _).trans $ by simpa theorem dvd_iff_mod_eq_zero {m n : znum} : m ∣ n ↔ n % m = 0 := by rw [← dvd_to_int, int.dvd_iff_mod_eq_zero, ← to_int_inj, mod_to_int]; refl instance : decidable_rel ((∣) : znum → znum → Prop) | a b := decidable_of_iff' _ dvd_iff_mod_eq_zero end znum namespace int def of_snum : snum → ℤ := snum.rec' (λ a, cond a (-1) 0) (λa p IH, cond a (bit1 IH) (bit0 IH)) instance snum_coe : has_coe snum ℤ := ⟨of_snum⟩ end int instance : has_lt snum := ⟨λa b, (a : ℤ) < b⟩ instance : has_le snum := ⟨λa b, (a : ℤ) ≤ b⟩
166f5aeea84a39bdbf81d0e61e2377175a4bc0cf
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/scripts/lint_mathlib.lean
9ef459bd5498d75bf996e927556e6321a2bb5096
[ "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
3,237
lean
/- Copyright (c) 2020 Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Y. Lewis, Gabriel Ebner -/ import tactic.lint import system.io -- these are required import all -- then import everything, to parse the library for failing linters /-! # lint_mathlib Script that runs the linters listed in `mathlib_linters` on all of mathlib. As a side effect, the file `nolints.txt` is generated in the current directory. This script needs to be run in the root directory of mathlib. It assumes that files generated by `mk_all.sh` are present. This is used by the CI script for mathlib. Usage: `lean --run scripts/lint_mathlib.lean` -/ open native tactic /-- Returns the contents of the `nolints.txt` file. -/ meta def mk_nolint_file (env : environment) (mathlib_path_len : ℕ) (results : list (name × linter × rb_map name string)) : format := do let failed_decls_by_file := rb_lmap.of_list (do (linter_name, _, decls) ← results, (decl_name, _) ← decls.to_list, let file_name := (env.decl_olean decl_name).get_or_else "", pure (file_name.popn mathlib_path_len, decl_name.to_string, linter_name.last)), format.intercalate format.line $ "import .all" :: "run_cmd tactic.skip" :: do (file_name, decls) ← failed_decls_by_file.to_list.reverse, "" :: ("-- " ++ file_name) :: do (decl, linters) ← (rb_lmap.of_list decls).to_list.reverse, pure $ "apply_nolint " ++ decl ++ " " ++ " ".intercalate linters /-- Parses the list of lines of the `nolints.txt` into an `rb_lmap` from linters to declarations. -/ meta def parse_nolints (lines : list string) : rb_lmap name name := rb_lmap.of_list $ do line ← lines, guard $ line.front = 'a', _ :: decl :: linters ← pure $ line.split (= ' ') | [], let decl := name.from_string decl, linter ← linters, pure (linter, decl) open io io.fs /-- Reads the `nolints.txt`, and returns it as an `rb_lmap` from linters to declarations. -/ meta def read_nolints_file (fn := "scripts/nolints.txt") : io (rb_lmap name name) := do cont ← io.fs.read_file fn, pure $ parse_nolints $ cont.to_string.split (= '\n') meta instance coe_tactic_to_io {α} : has_coe (tactic α) (io α) := ⟨run_tactic⟩ /-- Writes a file with the given contents. -/ meta def io.write_file (fn : string) (contents : string) : io unit := do h ← mk_file_handle fn mode.write, put_str h contents, close h /-- Runs when called with `lean --run` -/ meta def main : io unit := do env ← get_env, mathlib_path ← get_mathlib_dir, decls ← lint_project_decls mathlib_path, linters ← get_linters mathlib_linters, let non_auto_decls := decls.filter (λ d, ¬ d.is_auto_or_internal env), results₀ ← lint_core decls non_auto_decls linters, nolint_file ← read_nolints_file, let results := (do (linter_name, linter, decls) ← results₀, [(linter_name, linter, (nolint_file.find linter_name).foldl rb_map.erase decls)]), io.print $ to_string $ format_linter_results env results decls non_auto_decls mathlib_path.length "in mathlib" tt lint_verbosity.medium linters.length, io.write_file "nolints.txt" $ to_string $ mk_nolint_file env mathlib_path.length results₀, if results.all (λ r, r.2.2.empty) then pure () else io.fail ""
b39865bb842ace0beb11c509bad804754bdc8339
947fa6c38e48771ae886239b4edce6db6e18d0fb
/src/measure_theory/measure/measure_space.lean
529820cdfca5f3083562c2b6023bfab09595c90e
[ "Apache-2.0" ]
permissive
ramonfmir/mathlib
c5dc8b33155473fab97c38bd3aa6723dc289beaa
14c52e990c17f5a00c0cc9e09847af16fabbed25
refs/heads/master
1,661,979,343,526
1,660,830,384,000
1,660,830,384,000
182,072,989
0
0
null
1,555,585,876,000
1,555,585,876,000
null
UTF-8
Lean
false
false
156,890
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 measure_theory.measure.null_measurable import measure_theory.measurable_space /-! # Measure spaces The definition of a measure and a measure space are in `measure_theory.measure_space_def`, with only a few basic properties. This file provides many more properties of these objects. This separation allows the measurability tactic to import only the file `measure_space_def`, and to be available in `measure_space` (through `measurable_space`). Given a measurable space `α`, a measure on `α` is a function that sends measurable sets to the extended nonnegative reals that satisfies the following conditions: 1. `μ ∅ = 0`; 2. `μ` is countably additive. This means that the measure of a countable union of pairwise disjoint sets is equal to the measure of the individual sets. Every measure can be canonically extended to an outer measure, so that it assigns values to all subsets, not just the measurable subsets. On the other hand, a measure that is countably additive on measurable sets can be restricted to measurable sets to obtain a measure. In this file a measure is defined to be an outer measure that is countably additive on measurable sets, with the additional assumption that the outer measure is the canonical extension of the restricted measure. Measures on `α` form a complete lattice, and are closed under scalar multiplication with `ℝ≥0∞`. We introduce the following typeclasses for measures: * `is_probability_measure μ`: `μ univ = 1`; * `is_finite_measure μ`: `μ univ < ∞`; * `sigma_finite μ`: there exists a countable collection of sets that cover `univ` where `μ` is finite; * `is_locally_finite_measure μ` : `∀ x, ∃ s ∈ 𝓝 x, μ s < ∞`; * `has_no_atoms μ` : `∀ x, μ {x} = 0`; possibly should be redefined as `∀ s, 0 < μ s → ∃ t ⊆ s, 0 < μ t ∧ μ t < μ s`. Given a measure, the null sets are the sets where `μ s = 0`, where `μ` denotes the corresponding outer measure (so `s` might not be measurable). We can then define the completion of `μ` as the measure on the least `σ`-algebra that also contains all null sets, by defining the measure to be `0` on the null sets. ## Main statements * `completion` is the completion of a measure to all null measurable sets. * `measure.of_measurable` and `outer_measure.to_measure` are two important ways to define a measure. ## Implementation notes Given `μ : measure α`, `μ s` is the value of the *outer measure* applied to `s`. This conveniently allows us to apply the measure to sets without proving that they are measurable. We get countable subadditivity for all sets, but only countable additivity for measurable sets. You often don't want to define a measure via its constructor. Two ways that are sometimes more convenient: * `measure.of_measurable` is a way to define a measure by only giving its value on measurable sets and proving the properties (1) and (2) mentioned above. * `outer_measure.to_measure` is a way of obtaining a measure from an outer measure by showing that all measurable sets in the measurable space are Carathéodory measurable. To prove that two measures are equal, there are multiple options: * `ext`: two measures are equal if they are equal on all measurable sets. * `ext_of_generate_from_of_Union`: two measures are equal if they are equal on a π-system generating the measurable sets, if the π-system contains a spanning increasing sequence of sets where the measures take finite value (in particular the measures are σ-finite). This is a special case of the more general `ext_of_generate_from_of_cover` * `ext_of_generate_finite`: two finite measures are equal if they are equal on a π-system generating the measurable sets. This is a special case of `ext_of_generate_from_of_Union` using `C ∪ {univ}`, but is easier to work with. A `measure_space` is a class that is a measurable space with a canonical measure. The measure is denoted `volume`. ## References * <https://en.wikipedia.org/wiki/Measure_(mathematics)> * <https://en.wikipedia.org/wiki/Complete_measure> * <https://en.wikipedia.org/wiki/Almost_everywhere> ## Tags measure, almost everywhere, measure space, completion, null set, null measurable set -/ noncomputable theory open set filter (hiding map) function measurable_space topological_space (second_countable_topology) open_locale classical topological_space big_operators filter ennreal nnreal interval measure_theory variables {α β γ δ ι R R' : Type*} namespace measure_theory section variables {m : measurable_space α} {μ μ₁ μ₂ : measure α} {s s₁ s₂ t : set α} instance ae_is_measurably_generated : is_measurably_generated μ.ae := ⟨λ s hs, let ⟨t, hst, htm, htμ⟩ := exists_measurable_superset_of_null hs in ⟨tᶜ, compl_mem_ae_iff.2 htμ, htm.compl, compl_subset_comm.1 hst⟩⟩ /-- See also `measure_theory.ae_restrict_interval_oc_iff`. -/ lemma ae_interval_oc_iff [linear_order α] {a b : α} {P : α → Prop} : (∀ᵐ x ∂μ, x ∈ Ι a b → P x) ↔ (∀ᵐ x ∂μ, x ∈ Ioc a b → P x) ∧ (∀ᵐ x ∂μ, x ∈ Ioc b a → P x) := by simp only [interval_oc_eq_union, mem_union_eq, or_imp_distrib, eventually_and] lemma measure_union (hd : disjoint s₁ s₂) (h : measurable_set s₂) : μ (s₁ ∪ s₂) = μ s₁ + μ s₂ := measure_union₀ h.null_measurable_set hd.ae_disjoint lemma measure_union' (hd : disjoint s₁ s₂) (h : measurable_set s₁) : μ (s₁ ∪ s₂) = μ s₁ + μ s₂ := measure_union₀' h.null_measurable_set hd.ae_disjoint lemma measure_inter_add_diff (s : set α) (ht : measurable_set t) : μ (s ∩ t) + μ (s \ t) = μ s := measure_inter_add_diff₀ _ ht.null_measurable_set lemma measure_diff_add_inter (s : set α) (ht : measurable_set t) : μ (s \ t) + μ (s ∩ t) = μ s := (add_comm _ _).trans (measure_inter_add_diff s ht) lemma measure_union_add_inter (s : set α) (ht : measurable_set t) : μ (s ∪ t) + μ (s ∩ t) = μ s + μ t := by { rw [← measure_inter_add_diff (s ∪ t) ht, set.union_inter_cancel_right, union_diff_right, ← measure_inter_add_diff s ht], ac_refl } lemma measure_union_add_inter' (hs : measurable_set s) (t : set α) : μ (s ∪ t) + μ (s ∩ t) = μ s + μ t := by rw [union_comm, inter_comm, measure_union_add_inter t hs, add_comm] lemma measure_add_measure_compl (h : measurable_set s) : μ s + μ sᶜ = μ univ := by { rw [← measure_union' _ h, union_compl_self], exact disjoint_compl_right } lemma measure_bUnion₀ {s : set β} {f : β → set α} (hs : s.countable) (hd : s.pairwise (ae_disjoint μ on f)) (h : ∀ b ∈ s, null_measurable_set (f b) μ) : μ (⋃ b ∈ s, f b) = ∑' p : s, μ (f p) := begin haveI := hs.to_encodable, rw bUnion_eq_Union, exact measure_Union₀ (hd.on_injective subtype.coe_injective $ λ x, x.2) (λ x, h x x.2) end lemma measure_bUnion {s : set β} {f : β → set α} (hs : s.countable) (hd : s.pairwise_disjoint f) (h : ∀ b ∈ s, measurable_set (f b)) : μ (⋃ b ∈ s, f b) = ∑' p : s, μ (f p) := measure_bUnion₀ hs hd.ae_disjoint (λ b hb, (h b hb).null_measurable_set) lemma measure_sUnion₀ {S : set (set α)} (hs : S.countable) (hd : S.pairwise (ae_disjoint μ)) (h : ∀ s ∈ S, null_measurable_set s μ) : μ (⋃₀ S) = ∑' s : S, μ s := by rw [sUnion_eq_bUnion, measure_bUnion₀ hs hd h] lemma measure_sUnion {S : set (set α)} (hs : S.countable) (hd : S.pairwise disjoint) (h : ∀ s ∈ S, measurable_set s) : μ (⋃₀ S) = ∑' s : S, μ s := by rw [sUnion_eq_bUnion, measure_bUnion hs hd h] lemma measure_bUnion_finset₀ {s : finset ι} {f : ι → set α} (hd : set.pairwise ↑s (ae_disjoint μ on f)) (hm : ∀ b ∈ s, null_measurable_set (f b) μ) : μ (⋃ b ∈ s, f b) = ∑ p in s, μ (f p) := begin rw [← finset.sum_attach, finset.attach_eq_univ, ← tsum_fintype], exact measure_bUnion₀ s.countable_to_set hd hm end lemma measure_bUnion_finset {s : finset ι} {f : ι → set α} (hd : pairwise_disjoint ↑s f) (hm : ∀ b ∈ s, measurable_set (f b)) : μ (⋃ b ∈ s, f b) = ∑ p in s, μ (f p) := measure_bUnion_finset₀ hd.ae_disjoint (λ b hb, (hm b hb).null_measurable_set) /-- If `s` is a countable set, then the measure of its preimage can be found as the sum of measures of the fibers `f ⁻¹' {y}`. -/ lemma tsum_measure_preimage_singleton {s : set β} (hs : s.countable) {f : α → β} (hf : ∀ y ∈ s, measurable_set (f ⁻¹' {y})) : ∑' b : s, μ (f ⁻¹' {↑b}) = μ (f ⁻¹' s) := by rw [← set.bUnion_preimage_singleton, measure_bUnion hs (pairwise_disjoint_fiber _ _) hf] /-- If `s` is a `finset`, then the measure of its preimage can be found as the sum of measures of the fibers `f ⁻¹' {y}`. -/ lemma sum_measure_preimage_singleton (s : finset β) {f : α → β} (hf : ∀ y ∈ s, measurable_set (f ⁻¹' {y})) : ∑ b in s, μ (f ⁻¹' {b}) = μ (f ⁻¹' ↑s) := by simp only [← measure_bUnion_finset (pairwise_disjoint_fiber _ _) hf, finset.set_bUnion_preimage_singleton] lemma measure_diff_null' (h : μ (s₁ ∩ s₂) = 0) : μ (s₁ \ s₂) = μ s₁ := measure_congr $ diff_ae_eq_self.2 h lemma measure_diff_null (h : μ s₂ = 0) : μ (s₁ \ s₂) = μ s₁ := measure_diff_null' $ measure_mono_null (inter_subset_right _ _) h lemma measure_add_diff (hs : measurable_set s) (t : set α) : μ s + μ (t \ s) = μ (s ∪ t) := by rw [← measure_union' disjoint_diff hs, union_diff_self] lemma measure_diff' (s : set α) (hm : measurable_set t) (h_fin : μ t ≠ ∞) : μ (s \ t) = μ (s ∪ t) - μ t := eq.symm $ ennreal.sub_eq_of_add_eq h_fin $ by rw [add_comm, measure_add_diff hm, union_comm] lemma measure_diff (h : s₂ ⊆ s₁) (h₂ : measurable_set s₂) (h_fin : μ s₂ ≠ ∞) : μ (s₁ \ s₂) = μ s₁ - μ s₂ := by rw [measure_diff' _ h₂ h_fin, union_eq_self_of_subset_right h] lemma le_measure_diff : μ s₁ - μ s₂ ≤ μ (s₁ \ s₂) := tsub_le_iff_left.2 $ calc μ s₁ ≤ μ (s₂ ∪ s₁) : measure_mono (subset_union_right _ _) ... = μ (s₂ ∪ s₁ \ s₂) : congr_arg μ union_diff_self.symm ... ≤ μ s₂ + μ (s₁ \ s₂) : measure_union_le _ _ lemma measure_diff_lt_of_lt_add (hs : measurable_set s) (hst : s ⊆ t) (hs' : μ s ≠ ∞) {ε : ℝ≥0∞} (h : μ t < μ s + ε) : μ (t \ s) < ε := begin rw [measure_diff hst hs hs'], rw add_comm at h, exact ennreal.sub_lt_of_lt_add (measure_mono hst) h end lemma measure_diff_le_iff_le_add (hs : measurable_set s) (hst : s ⊆ t) (hs' : μ s ≠ ∞) {ε : ℝ≥0∞} : μ (t \ s) ≤ ε ↔ μ t ≤ μ s + ε := by rwa [measure_diff hst hs hs', tsub_le_iff_left] lemma measure_eq_measure_of_null_diff {s t : set α} (hst : s ⊆ t) (h_nulldiff : μ (t \ s) = 0) : μ s = μ t := measure_congr (hst.eventually_le.antisymm $ ae_le_set.mpr h_nulldiff) lemma measure_eq_measure_of_between_null_diff {s₁ s₂ s₃ : set α} (h12 : s₁ ⊆ s₂) (h23 : s₂ ⊆ s₃) (h_nulldiff : μ (s₃ \ s₁) = 0) : (μ s₁ = μ s₂) ∧ (μ s₂ = μ s₃) := begin have le12 : μ s₁ ≤ μ s₂ := measure_mono h12, have le23 : μ s₂ ≤ μ s₃ := measure_mono h23, have key : μ s₃ ≤ μ s₁ := calc μ s₃ = μ ((s₃ \ s₁) ∪ s₁) : by rw (diff_union_of_subset (h12.trans h23)) ... ≤ μ (s₃ \ s₁) + μ s₁ : measure_union_le _ _ ... = μ s₁ : by simp only [h_nulldiff, zero_add], exact ⟨le12.antisymm (le23.trans key), le23.antisymm (key.trans le12)⟩, end lemma measure_eq_measure_smaller_of_between_null_diff {s₁ s₂ s₃ : set α} (h12 : s₁ ⊆ s₂) (h23 : s₂ ⊆ s₃) (h_nulldiff : μ (s₃ \ s₁) = 0) : μ s₁ = μ s₂ := (measure_eq_measure_of_between_null_diff h12 h23 h_nulldiff).1 lemma measure_eq_measure_larger_of_between_null_diff {s₁ s₂ s₃ : set α} (h12 : s₁ ⊆ s₂) (h23 : s₂ ⊆ s₃) (h_nulldiff : μ (s₃ \ s₁) = 0) : μ s₂ = μ s₃ := (measure_eq_measure_of_between_null_diff h12 h23 h_nulldiff).2 lemma measure_compl (h₁ : measurable_set s) (h_fin : μ s ≠ ∞) : μ (sᶜ) = μ univ - μ s := by { rw compl_eq_univ_diff, exact measure_diff (subset_univ s) h₁ h_fin } /-- If `s ⊆ t`, `μ t ≤ μ s`, `μ t ≠ ∞`, and `s` is measurable, then `s =ᵐ[μ] t`. -/ lemma ae_eq_of_subset_of_measure_ge (h₁ : s ⊆ t) (h₂ : μ t ≤ μ s) (hsm : measurable_set s) (ht : μ t ≠ ∞) : s =ᵐ[μ] t := have A : μ t = μ s, from h₂.antisymm (measure_mono h₁), have B : μ s ≠ ∞, from A ▸ ht, h₁.eventually_le.antisymm $ ae_le_set.2 $ by rw [measure_diff h₁ hsm B, A, tsub_self] lemma measure_Union_congr_of_subset [encodable β] {s : β → set α} {t : β → set α} (hsub : ∀ b, s b ⊆ t b) (h_le : ∀ b, μ (t b) ≤ μ (s b)) : μ (⋃ b, s b) = μ (⋃ b, t b) := begin rcases em (∃ b, μ (t b) = ∞) with ⟨b, hb⟩|htop, { calc μ (⋃ b, s b) = ∞ : top_unique (hb ▸ (h_le b).trans $ measure_mono $ subset_Union _ _) ... = μ (⋃ b, t b) : eq.symm $ top_unique $ hb ▸ measure_mono $ subset_Union _ _ }, push_neg at htop, refine le_antisymm (measure_mono (Union_mono hsub)) _, set M := to_measurable μ, have H : ∀ b, (M (t b) ∩ M (⋃ b, s b) : set α) =ᵐ[μ] M (t b), { refine λ b, ae_eq_of_subset_of_measure_ge (inter_subset_left _ _) _ _ _, { calc μ (M (t b)) = μ (t b) : measure_to_measurable _ ... ≤ μ (s b) : h_le b ... ≤ μ (M (t b) ∩ M (⋃ b, s b)) : measure_mono $ subset_inter ((hsub b).trans $ subset_to_measurable _ _) ((subset_Union _ _).trans $ subset_to_measurable _ _) }, { exact (measurable_set_to_measurable _ _).inter (measurable_set_to_measurable _ _) }, { rw measure_to_measurable, exact htop b } }, calc μ (⋃ b, t b) ≤ μ (⋃ b, M (t b)) : measure_mono (Union_mono $ λ b, subset_to_measurable _ _) ... = μ (⋃ b, M (t b) ∩ M (⋃ b, s b)) : measure_congr (eventually_eq.countable_Union H).symm ... ≤ μ (M (⋃ b, s b)) : measure_mono (Union_subset $ λ b, inter_subset_right _ _) ... = μ (⋃ b, s b) : measure_to_measurable _ end lemma measure_union_congr_of_subset {t₁ t₂ : set α} (hs : s₁ ⊆ s₂) (hsμ : μ s₂ ≤ μ s₁) (ht : t₁ ⊆ t₂) (htμ : μ t₂ ≤ μ t₁) : μ (s₁ ∪ t₁) = μ (s₂ ∪ t₂) := begin rw [union_eq_Union, union_eq_Union], exact measure_Union_congr_of_subset (bool.forall_bool.2 ⟨ht, hs⟩) (bool.forall_bool.2 ⟨htμ, hsμ⟩) end @[simp] lemma measure_Union_to_measurable [encodable β] (s : β → set α) : μ (⋃ b, to_measurable μ (s b)) = μ (⋃ b, s b) := eq.symm $ measure_Union_congr_of_subset (λ b, subset_to_measurable _ _) (λ b, (measure_to_measurable _).le) lemma measure_bUnion_to_measurable {I : set β} (hc : I.countable) (s : β → set α) : μ (⋃ b ∈ I, to_measurable μ (s b)) = μ (⋃ b ∈ I, s b) := by { haveI := hc.to_encodable, simp only [bUnion_eq_Union, measure_Union_to_measurable] } @[simp] lemma measure_to_measurable_union : μ (to_measurable μ s ∪ t) = μ (s ∪ t) := eq.symm $ measure_union_congr_of_subset (subset_to_measurable _ _) (measure_to_measurable _).le subset.rfl le_rfl @[simp] lemma measure_union_to_measurable : μ (s ∪ to_measurable μ t) = μ (s ∪ t) := eq.symm $ measure_union_congr_of_subset subset.rfl le_rfl (subset_to_measurable _ _) (measure_to_measurable _).le lemma sum_measure_le_measure_univ {s : finset ι} {t : ι → set α} (h : ∀ i ∈ s, measurable_set (t i)) (H : set.pairwise_disjoint ↑s t) : ∑ i in s, μ (t i) ≤ μ (univ : set α) := by { rw ← measure_bUnion_finset H h, exact measure_mono (subset_univ _) } lemma tsum_measure_le_measure_univ {s : ι → set α} (hs : ∀ i, measurable_set (s i)) (H : pairwise (disjoint on s)) : ∑' i, μ (s i) ≤ μ (univ : set α) := begin rw [ennreal.tsum_eq_supr_sum], exact supr_le (λ s, sum_measure_le_measure_univ (λ i hi, hs i) (λ i hi j hj hij, H i j hij)) end /-- Pigeonhole principle for measure spaces: if `∑' i, μ (s i) > μ univ`, then one of the intersections `s i ∩ s j` is not empty. -/ lemma exists_nonempty_inter_of_measure_univ_lt_tsum_measure {m : measurable_space α} (μ : measure α) {s : ι → set α} (hs : ∀ i, measurable_set (s i)) (H : μ (univ : set α) < ∑' i, μ (s i)) : ∃ i j (h : i ≠ j), (s i ∩ s j).nonempty := begin contrapose! H, apply tsum_measure_le_measure_univ hs, exact λ i j hij x hx, H i j hij ⟨x, hx⟩ end /-- Pigeonhole principle for measure spaces: if `s` is a `finset` and `∑ i in s, μ (t i) > μ univ`, then one of the intersections `t i ∩ t j` is not empty. -/ lemma exists_nonempty_inter_of_measure_univ_lt_sum_measure {m : measurable_space α} (μ : measure α) {s : finset ι} {t : ι → set α} (h : ∀ i ∈ s, measurable_set (t i)) (H : μ (univ : set α) < ∑ i in s, μ (t i)) : ∃ (i ∈ s) (j ∈ s) (h : i ≠ j), (t i ∩ t j).nonempty := begin contrapose! H, apply sum_measure_le_measure_univ h, exact λ i hi j hj hij x hx, H i hi j hj hij ⟨x, hx⟩ end /-- If two sets `s` and `t` are included in a set `u`, and `μ s + μ t > μ u`, then `s` intersects `t`. Version assuming that `t` is measurable. -/ lemma nonempty_inter_of_measure_lt_add {m : measurable_space α} (μ : measure α) {s t u : set α} (ht : measurable_set t) (h's : s ⊆ u) (h't : t ⊆ u) (h : μ u < μ s + μ t) : (s ∩ t).nonempty := begin contrapose! h, calc μ s + μ t = μ (s ∪ t) : by { rw measure_union _ ht, exact λ x hx, h ⟨x, hx⟩ } ... ≤ μ u : measure_mono (union_subset h's h't) end /-- If two sets `s` and `t` are included in a set `u`, and `μ s + μ t > μ u`, then `s` intersects `t`. Version assuming that `s` is measurable. -/ lemma nonempty_inter_of_measure_lt_add' {m : measurable_space α} (μ : measure α) {s t u : set α} (hs : measurable_set s) (h's : s ⊆ u) (h't : t ⊆ u) (h : μ u < μ s + μ t) : (s ∩ t).nonempty := begin rw add_comm at h, rw inter_comm, exact nonempty_inter_of_measure_lt_add μ hs h't h's h end /-- Continuity from below: the measure of the union of a directed sequence of (not necessarily -measurable) sets is the supremum of the measures. -/ lemma measure_Union_eq_supr [encodable ι] {s : ι → set α} (hd : directed (⊆) s) : μ (⋃ i, s i) = ⨆ i, μ (s i) := begin -- WLOG, `ι = ℕ` generalize ht : function.extend encodable.encode s ⊥ = t, replace hd : directed (⊆) t := ht ▸ hd.extend_bot encodable.encode_injective, suffices : μ (⋃ n, t n) = ⨆ n, μ (t n), { simp only [← ht, apply_extend encodable.encode_injective μ, ← supr_eq_Union, supr_extend_bot encodable.encode_injective, (∘), pi.bot_apply, bot_eq_empty, measure_empty] at this, exact this.trans (supr_extend_bot encodable.encode_injective _) }, unfreezingI { clear_dependent ι }, -- The `≥` inequality is trivial refine le_antisymm _ (supr_le $ λ i, measure_mono $ subset_Union _ _), -- Choose `T n ⊇ t n` of the same measure, put `Td n = disjointed T` set T : ℕ → set α := λ n, to_measurable μ (t n), set Td : ℕ → set α := disjointed T, have hm : ∀ n, measurable_set (Td n), from measurable_set.disjointed (λ n, measurable_set_to_measurable _ _), calc μ (⋃ n, t n) ≤ μ (⋃ n, T n) : measure_mono (Union_mono $ λ i, subset_to_measurable _ _) ... = μ (⋃ n, Td n) : by rw [Union_disjointed] ... ≤ ∑' n, μ (Td n) : measure_Union_le _ ... = ⨆ I : finset ℕ, ∑ n in I, μ (Td n) : ennreal.tsum_eq_supr_sum ... ≤ ⨆ n, μ (t n) : supr_le (λ I, _), rcases hd.finset_le I with ⟨N, hN⟩, calc ∑ n in I, μ (Td n) = μ (⋃ n ∈ I, Td n) : (measure_bUnion_finset ((disjoint_disjointed T).set_pairwise I) (λ n _, hm n)).symm ... ≤ μ (⋃ n ∈ I, T n) : measure_mono (Union₂_mono $ λ n hn, disjointed_subset _ _) ... = μ (⋃ n ∈ I, t n) : measure_bUnion_to_measurable I.countable_to_set _ ... ≤ μ (t N) : measure_mono (Union₂_subset hN) ... ≤ ⨆ n, μ (t n) : le_supr (μ ∘ t) N, end lemma measure_bUnion_eq_supr {s : ι → set α} {t : set ι} (ht : t.countable) (hd : directed_on ((⊆) on s) t) : μ (⋃ i ∈ t, s i) = ⨆ i ∈ t, μ (s i) := begin haveI := ht.to_encodable, rw [bUnion_eq_Union, measure_Union_eq_supr hd.directed_coe, ← supr_subtype''] end /-- Continuity from above: the measure of the intersection of a decreasing sequence of measurable sets is the infimum of the measures. -/ lemma measure_Inter_eq_infi [encodable ι] {s : ι → set α} (h : ∀ i, measurable_set (s i)) (hd : directed (⊇) s) (hfin : ∃ i, μ (s i) ≠ ∞) : μ (⋂ i, s i) = (⨅ i, μ (s i)) := begin rcases hfin with ⟨k, hk⟩, have : ∀ t ⊆ s k, μ t ≠ ∞, from λ t ht, ne_top_of_le_ne_top hk (measure_mono ht), rw [← ennreal.sub_sub_cancel (by exact hk) (infi_le _ k), ennreal.sub_infi, ← ennreal.sub_sub_cancel (by exact hk) (measure_mono (Inter_subset _ k)), ← measure_diff (Inter_subset _ k) (measurable_set.Inter h) (this _ (Inter_subset _ k)), diff_Inter, measure_Union_eq_supr], { congr' 1, refine le_antisymm (supr_mono' $ λ i, _) (supr_mono $ λ i, _), { rcases hd i k with ⟨j, hji, hjk⟩, use j, rw [← measure_diff hjk (h _) (this _ hjk)], exact measure_mono (diff_subset_diff_right hji) }, { rw [tsub_le_iff_right, ← measure_union disjoint_diff.symm (h i), set.union_comm], exact measure_mono (diff_subset_iff.1 $ subset.refl _) } }, { exact hd.mono_comp _ (λ _ _, diff_subset_diff_right) } end /-- Continuity from below: the measure of the union of an increasing sequence of measurable sets is the limit of the measures. -/ lemma tendsto_measure_Union [semilattice_sup ι] [encodable ι] {s : ι → set α} (hm : monotone s) : tendsto (μ ∘ s) at_top (𝓝 (μ (⋃ n, s n))) := begin rw measure_Union_eq_supr (directed_of_sup hm), exact tendsto_at_top_supr (λ n m hnm, measure_mono $ hm hnm) end /-- Continuity from above: the measure of the intersection of a decreasing sequence of measurable sets is the limit of the measures. -/ lemma tendsto_measure_Inter [encodable ι] [semilattice_sup ι] {s : ι → set α} (hs : ∀ n, measurable_set (s n)) (hm : antitone s) (hf : ∃ i, μ (s i) ≠ ∞) : tendsto (μ ∘ s) at_top (𝓝 (μ (⋂ n, s n))) := begin rw measure_Inter_eq_infi hs (directed_of_sup hm) hf, exact tendsto_at_top_infi (λ n m hnm, measure_mono $ hm hnm), end /-- The measure of the intersection of a decreasing sequence of measurable sets indexed by a linear order with first countable topology is the limit of the measures. -/ lemma tendsto_measure_bInter_gt {ι : Type*} [linear_order ι] [topological_space ι] [order_topology ι] [densely_ordered ι] [topological_space.first_countable_topology ι] {s : ι → set α} {a : ι} (hs : ∀ r > a, measurable_set (s r)) (hm : ∀ i j, a < i → i ≤ j → s i ⊆ s j) (hf : ∃ r > a, μ (s r) ≠ ∞) : tendsto (μ ∘ s) (𝓝[Ioi a] a) (𝓝 (μ (⋂ r > a, s r))) := begin refine tendsto_order.2 ⟨λ l hl, _, λ L hL, _⟩, { filter_upwards [self_mem_nhds_within] with r hr using hl.trans_le (measure_mono (bInter_subset_of_mem hr)), }, obtain ⟨u, u_anti, u_pos, u_lim⟩ : ∃ (u : ℕ → ι), strict_anti u ∧ (∀ (n : ℕ), a < u n) ∧ tendsto u at_top (𝓝 a), { rcases hf with ⟨r, ar, hr⟩, rcases exists_seq_strict_anti_tendsto' ar with ⟨w, w_anti, w_mem, w_lim⟩, exact ⟨w, w_anti, λ n, (w_mem n).1, w_lim⟩ }, have A : tendsto (μ ∘ (s ∘ u)) at_top (𝓝(μ (⋂ n, s (u n)))), { refine tendsto_measure_Inter (λ n, hs _ (u_pos n)) _ _, { intros m n hmn, exact hm _ _ (u_pos n) (u_anti.antitone hmn) }, { rcases hf with ⟨r, rpos, hr⟩, obtain ⟨n, hn⟩ : ∃ (n : ℕ), u n < r := ((tendsto_order.1 u_lim).2 r rpos).exists, refine ⟨n, ne_of_lt (lt_of_le_of_lt _ hr.lt_top)⟩, exact measure_mono (hm _ _ (u_pos n) hn.le) } }, have B : (⋂ n, s (u n)) = (⋂ r > a, s r), { apply subset.antisymm, { simp only [subset_Inter_iff, gt_iff_lt], intros r rpos, obtain ⟨n, hn⟩ : ∃ n, u n < r := ((tendsto_order.1 u_lim).2 _ rpos).exists, exact subset.trans (Inter_subset _ n) (hm (u n) r (u_pos n) hn.le) }, { simp only [subset_Inter_iff, gt_iff_lt], intros n, apply bInter_subset_of_mem, exact u_pos n } }, rw B at A, obtain ⟨n, hn⟩ : ∃ n, μ (s (u n)) < L := ((tendsto_order.1 A).2 _ hL).exists, have : Ioc a (u n) ∈ 𝓝[>] a := Ioc_mem_nhds_within_Ioi ⟨le_rfl, u_pos n⟩, filter_upwards [this] with r hr using lt_of_le_of_lt (measure_mono (hm _ _ hr.1 hr.2)) hn, end /-- One direction of the **Borel-Cantelli lemma**: if (sᵢ) is a sequence of sets such that `∑ μ sᵢ` is finite, then the limit superior of the `sᵢ` is a null set. -/ lemma measure_limsup_eq_zero {s : ℕ → set α} (hs : ∑' i, μ (s i) ≠ ∞) : μ (limsup at_top s) = 0 := begin -- First we replace the sequence `sₙ` with a sequence of measurable sets `tₙ ⊇ sₙ` of the same -- measure. set t : ℕ → set α := λ n, to_measurable μ (s n), have ht : ∑' i, μ (t i) ≠ ∞, by simpa only [t, measure_to_measurable] using hs, suffices : μ (limsup at_top t) = 0, { have A : s ≤ t := λ n, subset_to_measurable μ (s n), -- TODO default args fail exact measure_mono_null (limsup_le_limsup (eventually_of_forall (pi.le_def.mp A)) is_cobounded_le_of_bot is_bounded_le_of_top) this }, -- Next we unfold `limsup` for sets and replace equality with an inequality simp only [limsup_eq_infi_supr_of_nat', set.infi_eq_Inter, set.supr_eq_Union, ← nonpos_iff_eq_zero], -- Finally, we estimate `μ (⋃ i, t (i + n))` by `∑ i', μ (t (i + n))` refine le_of_tendsto_of_tendsto' (tendsto_measure_Inter (λ i, measurable_set.Union (λ b, measurable_set_to_measurable _ _)) _ ⟨0, ne_top_of_le_ne_top ht (measure_Union_le t)⟩) (ennreal.tendsto_sum_nat_add (μ ∘ t) ht) (λ n, measure_Union_le _), intros n m hnm x, simp only [set.mem_Union], exact λ ⟨i, hi⟩, ⟨i + (m - n), by simpa only [add_assoc, tsub_add_cancel_of_le hnm] using hi⟩ end lemma measure_if {x : β} {t : set β} {s : set α} : μ (if x ∈ t then s else ∅) = indicator t (λ _, μ s) x := by { split_ifs; simp [h] } end section outer_measure variables [ms : measurable_space α] {s t : set α} include ms /-- Obtain a measure by giving an outer measure where all sets in the σ-algebra are Carathéodory measurable. -/ def outer_measure.to_measure (m : outer_measure α) (h : ms ≤ m.caratheodory) : measure α := measure.of_measurable (λ s _, m s) m.empty (λ f hf hd, m.Union_eq_of_caratheodory (λ i, h _ (hf i)) hd) lemma le_to_outer_measure_caratheodory (μ : measure α) : ms ≤ μ.to_outer_measure.caratheodory := λ s hs t, (measure_inter_add_diff _ hs).symm @[simp] lemma to_measure_to_outer_measure (m : outer_measure α) (h : ms ≤ m.caratheodory) : (m.to_measure h).to_outer_measure = m.trim := rfl @[simp] lemma to_measure_apply (m : outer_measure α) (h : ms ≤ m.caratheodory) {s : set α} (hs : measurable_set s) : m.to_measure h s = m s := m.trim_eq hs lemma le_to_measure_apply (m : outer_measure α) (h : ms ≤ m.caratheodory) (s : set α) : m s ≤ m.to_measure h s := m.le_trim s lemma to_measure_apply₀ (m : outer_measure α) (h : ms ≤ m.caratheodory) {s : set α} (hs : null_measurable_set s (m.to_measure h)) : m.to_measure h s = m s := begin refine le_antisymm _ (le_to_measure_apply _ _ _), rcases hs.exists_measurable_subset_ae_eq with ⟨t, hts, htm, heq⟩, calc m.to_measure h s = m.to_measure h t : measure_congr heq.symm ... = m t : to_measure_apply m h htm ... ≤ m s : m.mono hts end @[simp] lemma to_outer_measure_to_measure {μ : measure α} : μ.to_outer_measure.to_measure (le_to_outer_measure_caratheodory _) = μ := measure.ext $ λ s, μ.to_outer_measure.trim_eq @[simp] lemma bounded_by_measure (μ : measure α) : outer_measure.bounded_by μ = μ.to_outer_measure := μ.to_outer_measure.bounded_by_eq_self end outer_measure variables {m0 : measurable_space α} [measurable_space β] [measurable_space γ] variables {μ μ₁ μ₂ μ₃ ν ν' ν₁ ν₂ : measure α} {s s' t : set α} namespace measure /-- If `u` is a superset of `t` with the same (finite) measure (both sets possibly non-measurable), then for any measurable set `s` one also has `μ (t ∩ s) = μ (u ∩ s)`. -/ lemma measure_inter_eq_of_measure_eq {s t u : set α} (hs : measurable_set s) (h : μ t = μ u) (htu : t ⊆ u) (ht_ne_top : μ t ≠ ∞) : μ (t ∩ s) = μ (u ∩ s) := begin rw h at ht_ne_top, refine le_antisymm (measure_mono (inter_subset_inter_left _ htu)) _, have A : μ (u ∩ s) + μ (u \ s) ≤ μ (t ∩ s) + μ (u \ s) := calc μ (u ∩ s) + μ (u \ s) = μ u : measure_inter_add_diff _ hs ... = μ t : h.symm ... = μ (t ∩ s) + μ (t \ s) : (measure_inter_add_diff _ hs).symm ... ≤ μ (t ∩ s) + μ (u \ s) : add_le_add le_rfl (measure_mono (diff_subset_diff htu subset.rfl)), have B : μ (u \ s) ≠ ∞ := (lt_of_le_of_lt (measure_mono (diff_subset _ _)) ht_ne_top.lt_top).ne, exact ennreal.le_of_add_le_add_right B A end /-- The measurable superset `to_measurable μ t` of `t` (which has the same measure as `t`) satisfies, for any measurable set `s`, the equality `μ (to_measurable μ t ∩ s) = μ (u ∩ s)`. Here, we require that the measure of `t` is finite. The conclusion holds without this assumption when the measure is sigma_finite, see `measure_to_measurable_inter_of_sigma_finite`. -/ lemma measure_to_measurable_inter {s t : set α} (hs : measurable_set s) (ht : μ t ≠ ∞) : μ (to_measurable μ t ∩ s) = μ (t ∩ s) := (measure_inter_eq_of_measure_eq hs (measure_to_measurable t).symm (subset_to_measurable μ t) ht).symm /-! ### The `ℝ≥0∞`-module of measures -/ instance [measurable_space α] : has_zero (measure α) := ⟨{ to_outer_measure := 0, m_Union := λ f hf hd, tsum_zero.symm, trimmed := outer_measure.trim_zero }⟩ @[simp] theorem zero_to_outer_measure {m : measurable_space α} : (0 : measure α).to_outer_measure = 0 := rfl @[simp, norm_cast] theorem coe_zero {m : measurable_space α} : ⇑(0 : measure α) = 0 := rfl lemma eq_zero_of_is_empty [is_empty α] {m : measurable_space α} (μ : measure α) : μ = 0 := ext $ λ s hs, by simp only [eq_empty_of_is_empty s, measure_empty] instance [measurable_space α] : inhabited (measure α) := ⟨0⟩ instance [measurable_space α] : has_add (measure α) := ⟨λ μ₁ μ₂, { to_outer_measure := μ₁.to_outer_measure + μ₂.to_outer_measure, m_Union := λ s hs hd, show μ₁ (⋃ i, s i) + μ₂ (⋃ i, s i) = ∑' i, (μ₁ (s i) + μ₂ (s i)), by rw [ennreal.tsum_add, measure_Union hd hs, measure_Union hd hs], trimmed := by rw [outer_measure.trim_add, μ₁.trimmed, μ₂.trimmed] }⟩ @[simp] theorem add_to_outer_measure {m : measurable_space α} (μ₁ μ₂ : measure α) : (μ₁ + μ₂).to_outer_measure = μ₁.to_outer_measure + μ₂.to_outer_measure := rfl @[simp, norm_cast] theorem coe_add {m : measurable_space α} (μ₁ μ₂ : measure α) : ⇑(μ₁ + μ₂) = μ₁ + μ₂ := rfl theorem add_apply {m : measurable_space α} (μ₁ μ₂ : measure α) (s : set α) : (μ₁ + μ₂) s = μ₁ s + μ₂ s := rfl section has_smul variables [has_smul R ℝ≥0∞] [is_scalar_tower R ℝ≥0∞ ℝ≥0∞] variables [has_smul R' ℝ≥0∞] [is_scalar_tower R' ℝ≥0∞ ℝ≥0∞] instance [measurable_space α] : has_smul R (measure α) := ⟨λ c μ, { to_outer_measure := c • μ.to_outer_measure, m_Union := λ s hs hd, begin rw ←smul_one_smul ℝ≥0∞ c (_ : outer_measure α), dsimp, simp_rw [measure_Union hd hs, ennreal.tsum_mul_left], end, trimmed := by rw [outer_measure.trim_smul, μ.trimmed] }⟩ @[simp] theorem smul_to_outer_measure {m : measurable_space α} (c : R) (μ : measure α) : (c • μ).to_outer_measure = c • μ.to_outer_measure := rfl @[simp, norm_cast] theorem coe_smul {m : measurable_space α} (c : R) (μ : measure α) : ⇑(c • μ) = c • μ := rfl @[simp] theorem smul_apply {m : measurable_space α} (c : R) (μ : measure α) (s : set α) : (c • μ) s = c • μ s := rfl instance [smul_comm_class R R' ℝ≥0∞] [measurable_space α] : smul_comm_class R R' (measure α) := ⟨λ _ _ _, ext $ λ _ _, smul_comm _ _ _⟩ instance [has_smul R R'] [is_scalar_tower R R' ℝ≥0∞] [measurable_space α] : is_scalar_tower R R' (measure α) := ⟨λ _ _ _, ext $ λ _ _, smul_assoc _ _ _⟩ instance [has_smul Rᵐᵒᵖ ℝ≥0∞] [is_central_scalar R ℝ≥0∞] [measurable_space α] : is_central_scalar R (measure α) := ⟨λ _ _, ext $ λ _ _, op_smul_eq_smul _ _⟩ end has_smul instance [monoid R] [mul_action R ℝ≥0∞] [is_scalar_tower R ℝ≥0∞ ℝ≥0∞] [measurable_space α] : mul_action R (measure α) := injective.mul_action _ to_outer_measure_injective smul_to_outer_measure instance add_comm_monoid [measurable_space α] : add_comm_monoid (measure α) := to_outer_measure_injective.add_comm_monoid to_outer_measure zero_to_outer_measure add_to_outer_measure (λ _ _, smul_to_outer_measure _ _) /-- Coercion to function as an additive monoid homomorphism. -/ def coe_add_hom {m : measurable_space α} : measure α →+ (set α → ℝ≥0∞) := ⟨coe_fn, coe_zero, coe_add⟩ @[simp] lemma coe_finset_sum {m : measurable_space α} (I : finset ι) (μ : ι → measure α) : ⇑(∑ i in I, μ i) = ∑ i in I, μ i := (@coe_add_hom α m).map_sum _ _ theorem finset_sum_apply {m : measurable_space α} (I : finset ι) (μ : ι → measure α) (s : set α) : (∑ i in I, μ i) s = ∑ i in I, μ i s := by rw [coe_finset_sum, finset.sum_apply] instance [monoid R] [distrib_mul_action R ℝ≥0∞] [is_scalar_tower R ℝ≥0∞ ℝ≥0∞] [measurable_space α] : distrib_mul_action R (measure α) := injective.distrib_mul_action ⟨to_outer_measure, zero_to_outer_measure, add_to_outer_measure⟩ to_outer_measure_injective smul_to_outer_measure instance [semiring R] [module R ℝ≥0∞] [is_scalar_tower R ℝ≥0∞ ℝ≥0∞] [measurable_space α] : module R (measure α) := injective.module R ⟨to_outer_measure, zero_to_outer_measure, add_to_outer_measure⟩ to_outer_measure_injective smul_to_outer_measure @[simp] theorem coe_nnreal_smul_apply {m : measurable_space α} (c : ℝ≥0) (μ : measure α) (s : set α) : (c • μ) s = c * μ s := rfl lemma ae_smul_measure_iff {p : α → Prop} {c : ℝ≥0∞} (hc : c ≠ 0) : (∀ᵐ x ∂(c • μ), p x) ↔ ∀ᵐ x ∂μ, p x := by simp [ae_iff, hc] lemma measure_eq_left_of_subset_of_measure_add_eq {s t : set α} (h : (μ + ν) t ≠ ∞) (h' : s ⊆ t) (h'' : (μ + ν) s = (μ + ν) t) : μ s = μ t := begin refine le_antisymm (measure_mono h') _, have : μ t + ν t ≤ μ s + ν t := calc μ t + ν t = μ s + ν s : h''.symm ... ≤ μ s + ν t : add_le_add le_rfl (measure_mono h'), apply ennreal.le_of_add_le_add_right _ this, simp only [not_or_distrib, ennreal.add_eq_top, pi.add_apply, ne.def, coe_add] at h, exact h.2 end lemma measure_eq_right_of_subset_of_measure_add_eq {s t : set α} (h : (μ + ν) t ≠ ∞) (h' : s ⊆ t) (h'' : (μ + ν) s = (μ + ν) t) : ν s = ν t := begin rw add_comm at h'' h, exact measure_eq_left_of_subset_of_measure_add_eq h h' h'' end lemma measure_to_measurable_add_inter_left {s t : set α} (hs : measurable_set s) (ht : (μ + ν) t ≠ ∞) : μ (to_measurable (μ + ν) t ∩ s) = μ (t ∩ s) := begin refine (measure_inter_eq_of_measure_eq hs _ (subset_to_measurable _ _) _).symm, { refine measure_eq_left_of_subset_of_measure_add_eq _ (subset_to_measurable _ _) (measure_to_measurable t).symm, rwa measure_to_measurable t, }, { simp only [not_or_distrib, ennreal.add_eq_top, pi.add_apply, ne.def, coe_add] at ht, exact ht.1 } end lemma measure_to_measurable_add_inter_right {s t : set α} (hs : measurable_set s) (ht : (μ + ν) t ≠ ∞) : ν (to_measurable (μ + ν) t ∩ s) = ν (t ∩ s) := begin rw add_comm at ht ⊢, exact measure_to_measurable_add_inter_left hs ht end /-! ### The complete lattice of measures -/ /-- Measures are partially ordered. The definition of less equal here is equivalent to the definition without the measurable set condition, and this is shown by `measure.le_iff'`. It is defined this way since, to prove `μ ≤ ν`, we may simply `intros s hs` instead of rewriting followed by `intros s hs`. -/ instance [measurable_space α] : partial_order (measure α) := { le := λ m₁ m₂, ∀ s, measurable_set s → m₁ s ≤ m₂ s, le_refl := λ m s hs, le_rfl, le_trans := λ m₁ m₂ m₃ h₁ h₂ s hs, le_trans (h₁ s hs) (h₂ s hs), le_antisymm := λ m₁ m₂ h₁ h₂, ext $ λ s hs, le_antisymm (h₁ s hs) (h₂ s hs) } theorem le_iff : μ₁ ≤ μ₂ ↔ ∀ s, measurable_set s → μ₁ s ≤ μ₂ s := iff.rfl theorem to_outer_measure_le : μ₁.to_outer_measure ≤ μ₂.to_outer_measure ↔ μ₁ ≤ μ₂ := by rw [← μ₂.trimmed, outer_measure.le_trim_iff]; refl theorem le_iff' : μ₁ ≤ μ₂ ↔ ∀ s, μ₁ s ≤ μ₂ s := to_outer_measure_le.symm theorem lt_iff : μ < ν ↔ μ ≤ ν ∧ ∃ s, measurable_set s ∧ μ s < ν s := lt_iff_le_not_le.trans $ and_congr iff.rfl $ by simp only [le_iff, not_forall, not_le, exists_prop] theorem lt_iff' : μ < ν ↔ μ ≤ ν ∧ ∃ s, μ s < ν s := lt_iff_le_not_le.trans $ and_congr iff.rfl $ by simp only [le_iff', not_forall, not_le] instance covariant_add_le [measurable_space α] : covariant_class (measure α) (measure α) (+) (≤) := ⟨λ ν μ₁ μ₂ hμ s hs, add_le_add_left (hμ s hs) _⟩ protected lemma le_add_left (h : μ ≤ ν) : μ ≤ ν' + ν := λ s hs, le_add_left (h s hs) protected lemma le_add_right (h : μ ≤ ν) : μ ≤ ν + ν' := λ s hs, le_add_right (h s hs) section Inf variables {m : set (measure α)} lemma Inf_caratheodory (s : set α) (hs : measurable_set s) : measurable_set[(Inf (to_outer_measure '' m)).caratheodory] s := begin rw [outer_measure.Inf_eq_bounded_by_Inf_gen], refine outer_measure.bounded_by_caratheodory (λ t, _), simp only [outer_measure.Inf_gen, le_infi_iff, ball_image_iff, coe_to_outer_measure, measure_eq_infi t], intros μ hμ u htu hu, have hm : ∀ {s t}, s ⊆ t → outer_measure.Inf_gen (to_outer_measure '' m) s ≤ μ t, { intros s t hst, rw [outer_measure.Inf_gen_def], refine infi_le_of_le (μ.to_outer_measure) (infi_le_of_le (mem_image_of_mem _ hμ) _), rw [to_outer_measure_apply], refine measure_mono hst }, rw [← measure_inter_add_diff u hs], refine add_le_add (hm $ inter_subset_inter_left _ htu) (hm $ diff_subset_diff_left htu) end instance [measurable_space α] : has_Inf (measure α) := ⟨λ m, (Inf (to_outer_measure '' m)).to_measure $ Inf_caratheodory⟩ lemma Inf_apply (hs : measurable_set s) : Inf m s = Inf (to_outer_measure '' m) s := to_measure_apply _ _ hs private lemma measure_Inf_le (h : μ ∈ m) : Inf m ≤ μ := have Inf (to_outer_measure '' m) ≤ μ.to_outer_measure := Inf_le (mem_image_of_mem _ h), λ s hs, by rw [Inf_apply hs, ← to_outer_measure_apply]; exact this s private lemma measure_le_Inf (h : ∀ μ' ∈ m, μ ≤ μ') : μ ≤ Inf m := have μ.to_outer_measure ≤ Inf (to_outer_measure '' m) := le_Inf $ ball_image_of_ball $ λ μ hμ, to_outer_measure_le.2 $ h _ hμ, λ s hs, by rw [Inf_apply hs, ← to_outer_measure_apply]; exact this s instance [measurable_space α] : complete_semilattice_Inf (measure α) := { Inf_le := λ s a, measure_Inf_le, le_Inf := λ s a, measure_le_Inf, ..(by apply_instance : partial_order (measure α)), ..(by apply_instance : has_Inf (measure α)), } instance [measurable_space α] : complete_lattice (measure α) := { bot := 0, bot_le := λ a s hs, by exact bot_le, /- Adding an explicit `top` makes `leanchecker` fail, see lean#364, disable for now top := (⊤ : outer_measure α).to_measure (by rw [outer_measure.top_caratheodory]; exact le_top), le_top := λ a s hs, by cases s.eq_empty_or_nonempty with h h; simp [h, to_measure_apply ⊤ _ hs, outer_measure.top_apply], -/ .. complete_lattice_of_complete_semilattice_Inf (measure α) } end Inf @[simp] lemma top_add : ⊤ + μ = ⊤ := top_unique $ measure.le_add_right le_rfl @[simp] lemma add_top : μ + ⊤ = ⊤ := top_unique $ measure.le_add_left le_rfl protected lemma zero_le {m0 : measurable_space α} (μ : measure α) : 0 ≤ μ := bot_le lemma nonpos_iff_eq_zero' : μ ≤ 0 ↔ μ = 0 := μ.zero_le.le_iff_eq @[simp] lemma measure_univ_eq_zero : μ univ = 0 ↔ μ = 0 := ⟨λ h, bot_unique $ λ s hs, trans_rel_left (≤) (measure_mono (subset_univ s)) h, λ h, h.symm ▸ rfl⟩ /-! ### Pushforward and pullback -/ /-- Lift a linear map between `outer_measure` spaces such that for each measure `μ` every measurable set is caratheodory-measurable w.r.t. `f μ` to a linear map between `measure` spaces. -/ def lift_linear {m0 : measurable_space α} (f : outer_measure α →ₗ[ℝ≥0∞] outer_measure β) (hf : ∀ μ : measure α, ‹_› ≤ (f μ.to_outer_measure).caratheodory) : measure α →ₗ[ℝ≥0∞] measure β := { to_fun := λ μ, (f μ.to_outer_measure).to_measure (hf μ), map_add' := λ μ₁ μ₂, ext $ λ s hs, by simp [hs], map_smul' := λ c μ, ext $ λ s hs, by simp [hs] } @[simp] lemma lift_linear_apply {f : outer_measure α →ₗ[ℝ≥0∞] outer_measure β} (hf) {s : set β} (hs : measurable_set s) : lift_linear f hf μ s = f μ.to_outer_measure s := to_measure_apply _ _ hs lemma le_lift_linear_apply {f : outer_measure α →ₗ[ℝ≥0∞] outer_measure β} (hf) (s : set β) : f μ.to_outer_measure s ≤ lift_linear f hf μ s := le_to_measure_apply _ _ s /-- The pushforward of a measure as a linear map. It is defined to be `0` if `f` is not a measurable function. -/ def mapₗ [measurable_space α] (f : α → β) : measure α →ₗ[ℝ≥0∞] measure β := if hf : measurable f then lift_linear (outer_measure.map f) $ λ μ s hs t, le_to_outer_measure_caratheodory μ _ (hf hs) (f ⁻¹' t) else 0 lemma mapₗ_congr {f g : α → β} (hf : measurable f) (hg : measurable g) (h : f =ᵐ[μ] g) : mapₗ f μ = mapₗ g μ := begin ext1 s hs, simpa only [mapₗ, hf, hg, hs, dif_pos, lift_linear_apply, outer_measure.map_apply, coe_to_outer_measure] using measure_congr (h.preimage s), end /-- The pushforward of a measure. It is defined to be `0` if `f` is not an almost everywhere measurable function. -/ @[irreducible] def map [measurable_space α] (f : α → β) (μ : measure α) : measure β := if hf : ae_measurable f μ then mapₗ (hf.mk f) μ else 0 include m0 lemma mapₗ_mk_apply_of_ae_measurable {f : α → β} (hf : ae_measurable f μ) : mapₗ (hf.mk f) μ = map f μ := by simp [map, hf] lemma mapₗ_apply_of_measurable {f : α → β} (hf : measurable f) (μ : measure α) : mapₗ f μ = map f μ := begin simp only [← mapₗ_mk_apply_of_ae_measurable hf.ae_measurable], exact mapₗ_congr hf hf.ae_measurable.measurable_mk hf.ae_measurable.ae_eq_mk end @[simp] lemma map_add (μ ν : measure α) {f : α → β} (hf : measurable f) : (μ + ν).map f = μ.map f + ν.map f := by simp [← mapₗ_apply_of_measurable hf] @[simp] lemma map_zero (f : α → β) : (0 : measure α).map f = 0 := begin by_cases hf : ae_measurable f (0 : measure α); simp [map, hf], end theorem map_of_not_ae_measurable {f : α → β} {μ : measure α} (hf : ¬ ae_measurable f μ) : μ.map f = 0 := by simp [map, hf] lemma map_congr {f g : α → β} (h : f =ᵐ[μ] g) : measure.map f μ = measure.map g μ := begin by_cases hf : ae_measurable f μ, { have hg : ae_measurable g μ := hf.congr h, simp only [← mapₗ_mk_apply_of_ae_measurable hf, ← mapₗ_mk_apply_of_ae_measurable hg], exact mapₗ_congr hf.measurable_mk hg.measurable_mk (hf.ae_eq_mk.symm.trans (h.trans hg.ae_eq_mk)) }, { have hg : ¬ (ae_measurable g μ), by simpa [← ae_measurable_congr h] using hf, simp [map_of_not_ae_measurable, hf, hg] } end @[simp] protected lemma map_smul (c : ℝ≥0∞) (μ : measure α) (f : α → β) : (c • μ).map f = c • μ.map f := begin rcases eq_or_ne c 0 with rfl|hc, { simp }, by_cases hf : ae_measurable f μ, { have hfc : ae_measurable f (c • μ) := ⟨hf.mk f, hf.measurable_mk, (ae_smul_measure_iff hc).2 hf.ae_eq_mk⟩, simp only [←mapₗ_mk_apply_of_ae_measurable hf, ←mapₗ_mk_apply_of_ae_measurable hfc, linear_map.map_smulₛₗ, ring_hom.id_apply], congr' 1, apply mapₗ_congr hfc.measurable_mk hf.measurable_mk, exact eventually_eq.trans ((ae_smul_measure_iff hc).1 hfc.ae_eq_mk.symm) hf.ae_eq_mk }, { have hfc : ¬ (ae_measurable f (c • μ)), { assume hfc, exact hf ⟨hfc.mk f, hfc.measurable_mk, (ae_smul_measure_iff hc).1 hfc.ae_eq_mk⟩ }, simp [map_of_not_ae_measurable hf, map_of_not_ae_measurable hfc] } end /-- We can evaluate the pushforward on measurable sets. For non-measurable sets, see `measure_theory.measure.le_map_apply` and `measurable_equiv.map_apply`. -/ @[simp] theorem map_apply_of_ae_measurable {f : α → β} (hf : ae_measurable f μ) {s : set β} (hs : measurable_set s) : μ.map f s = μ (f ⁻¹' s) := by simpa only [mapₗ, hf.measurable_mk, hs, dif_pos, lift_linear_apply, outer_measure.map_apply, coe_to_outer_measure, ← mapₗ_mk_apply_of_ae_measurable hf] using measure_congr (hf.ae_eq_mk.symm.preimage s) @[simp] theorem map_apply {f : α → β} (hf : measurable f) {s : set β} (hs : measurable_set s) : μ.map f s = μ (f ⁻¹' s) := map_apply_of_ae_measurable hf.ae_measurable hs lemma map_to_outer_measure {f : α → β} (hf : ae_measurable f μ) : (μ.map f).to_outer_measure = (outer_measure.map f μ.to_outer_measure).trim := begin rw [← trimmed, outer_measure.trim_eq_trim_iff], intros s hs, rw [coe_to_outer_measure, map_apply_of_ae_measurable hf hs, outer_measure.map_apply, coe_to_outer_measure] end @[simp] lemma map_id : map id μ = μ := ext $ λ s, map_apply measurable_id @[simp] lemma map_id' : map (λ x, x) μ = μ := map_id lemma map_map {g : β → γ} {f : α → β} (hg : measurable g) (hf : measurable f) : (μ.map f).map g = μ.map (g ∘ f) := ext $ λ s hs, by simp [hf, hg, hs, hg hs, hg.comp hf, ← preimage_comp] @[mono] lemma map_mono {f : α → β} (h : μ ≤ ν) (hf : measurable f) : μ.map f ≤ ν.map f := λ s hs, by simp [hf.ae_measurable, hs, h _ (hf hs)] /-- Even if `s` is not measurable, we can bound `map f μ s` from below. See also `measurable_equiv.map_apply`. -/ theorem le_map_apply {f : α → β} (hf : ae_measurable f μ) (s : set β) : μ (f ⁻¹' s) ≤ μ.map f s := calc μ (f ⁻¹' s) ≤ μ (f ⁻¹' (to_measurable (μ.map f) s)) : measure_mono $ preimage_mono $ subset_to_measurable _ _ ... = μ.map f (to_measurable (μ.map f) s) : (map_apply_of_ae_measurable hf $ measurable_set_to_measurable _ _).symm ... = μ.map f s : measure_to_measurable _ /-- Even if `s` is not measurable, `map f μ s = 0` implies that `μ (f ⁻¹' s) = 0`. -/ lemma preimage_null_of_map_null {f : α → β} (hf : ae_measurable f μ) {s : set β} (hs : μ.map f s = 0) : μ (f ⁻¹' s) = 0 := nonpos_iff_eq_zero.mp $ (le_map_apply hf s).trans_eq hs lemma tendsto_ae_map {f : α → β} (hf : ae_measurable f μ) : tendsto f μ.ae (μ.map f).ae := λ s hs, preimage_null_of_map_null hf hs omit m0 /-- Pullback of a `measure` as a linear map. If `f` sends each measurable set to a measurable set, then for each measurable set `s` we have `comapₗ f μ s = μ (f '' s)`. If the linearity is not needed, please use `comap` instead, which works for a larger class of functions. -/ def comapₗ [measurable_space α] (f : α → β) : measure β →ₗ[ℝ≥0∞] measure α := if hf : injective f ∧ ∀ s, measurable_set s → measurable_set (f '' s) then lift_linear (outer_measure.comap f) $ λ μ s hs t, begin simp only [coe_to_outer_measure, outer_measure.comap_apply, ← image_inter hf.1, image_diff hf.1], apply le_to_outer_measure_caratheodory, exact hf.2 s hs end else 0 lemma comapₗ_apply {β} [measurable_space α] {mβ : measurable_space β} (f : α → β) (hfi : injective f) (hf : ∀ s, measurable_set s → measurable_set (f '' s)) (μ : measure β) (hs : measurable_set s) : comapₗ f μ s = μ (f '' s) := begin rw [comapₗ, dif_pos, lift_linear_apply _ hs, outer_measure.comap_apply, coe_to_outer_measure], exact ⟨hfi, hf⟩ end /-- Pullback of a `measure`. If `f` sends each measurable set to a null-measurable set, then for each measurable set `s` we have `comap f μ s = μ (f '' s)`. -/ def comap [measurable_space α] (f : α → β) (μ : measure β) : measure α := if hf : injective f ∧ ∀ s, measurable_set s → null_measurable_set (f '' s) μ then (outer_measure.comap f μ.to_outer_measure).to_measure $ λ s hs t, begin simp only [coe_to_outer_measure, outer_measure.comap_apply, ← image_inter hf.1, image_diff hf.1], exact (measure_inter_add_diff₀ _ (hf.2 s hs)).symm end else 0 lemma comap_apply₀ [measurable_space α] (f : α → β) (μ : measure β) (hfi : injective f) (hf : ∀ s, measurable_set s → null_measurable_set (f '' s) μ) (hs : null_measurable_set s (comap f μ)) : comap f μ s = μ (f '' s) := begin rw [comap, dif_pos (and.intro hfi hf)] at hs ⊢, rw [to_measure_apply₀ _ _ hs, outer_measure.comap_apply, coe_to_outer_measure] end lemma comap_apply {β} [measurable_space α] {mβ : measurable_space β} (f : α → β) (hfi : injective f) (hf : ∀ s, measurable_set s → measurable_set (f '' s)) (μ : measure β) (hs : measurable_set s) : comap f μ s = μ (f '' s) := comap_apply₀ f μ hfi (λ s hs, (hf s hs).null_measurable_set) hs.null_measurable_set lemma comapₗ_eq_comap {β} [measurable_space α] {mβ : measurable_space β} (f : α → β) (hfi : injective f) (hf : ∀ s, measurable_set s → measurable_set (f '' s)) (μ : measure β) (hs : measurable_set s) : comapₗ f μ s = comap f μ s := (comapₗ_apply f hfi hf μ hs).trans (comap_apply f hfi hf μ hs).symm /-! ### Restricting a measure -/ /-- Restrict a measure `μ` to a set `s` as an `ℝ≥0∞`-linear map. -/ def restrictₗ {m0 : measurable_space α} (s : set α) : measure α →ₗ[ℝ≥0∞] measure α := lift_linear (outer_measure.restrict s) $ λ μ s' hs' t, begin suffices : μ (s ∩ t) = μ (s ∩ t ∩ s') + μ (s ∩ t \ s'), { simpa [← set.inter_assoc, set.inter_comm _ s, ← inter_diff_assoc] }, exact le_to_outer_measure_caratheodory _ _ hs' _, end /-- Restrict a measure `μ` to a set `s`. -/ def restrict {m0 : measurable_space α} (μ : measure α) (s : set α) : measure α := restrictₗ s μ @[simp] lemma restrictₗ_apply {m0 : measurable_space α} (s : set α) (μ : measure α) : restrictₗ s μ = μ.restrict s := rfl /-- This lemma shows that `restrict` and `to_outer_measure` commute. Note that the LHS has a restrict on measures and the RHS has a restrict on outer measures. -/ lemma restrict_to_outer_measure_eq_to_outer_measure_restrict (h : measurable_set s) : (μ.restrict s).to_outer_measure = outer_measure.restrict s μ.to_outer_measure := by simp_rw [restrict, restrictₗ, lift_linear, linear_map.coe_mk, to_measure_to_outer_measure, outer_measure.restrict_trim h, μ.trimmed] lemma restrict_apply₀ (ht : null_measurable_set t (μ.restrict s)) : μ.restrict s t = μ (t ∩ s) := (to_measure_apply₀ _ _ ht).trans $ by simp only [coe_to_outer_measure, outer_measure.restrict_apply] /-- If `t` is a measurable set, then the measure of `t` with respect to the restriction of the measure to `s` equals the outer measure of `t ∩ s`. An alternate version requiring that `s` be measurable instead of `t` exists as `measure.restrict_apply'`. -/ @[simp] lemma restrict_apply (ht : measurable_set t) : μ.restrict s t = μ (t ∩ s) := restrict_apply₀ ht.null_measurable_set /-- Restriction of a measure to a subset is monotone both in set and in measure. -/ lemma restrict_mono' {m0 : measurable_space α} ⦃s s' : set α⦄ ⦃μ ν : measure α⦄ (hs : s ≤ᵐ[μ] s') (hμν : μ ≤ ν) : μ.restrict s ≤ ν.restrict s' := assume t ht, calc μ.restrict s t = μ (t ∩ s) : restrict_apply ht ... ≤ μ (t ∩ s') : measure_mono_ae $ hs.mono $ λ x hx ⟨hxt, hxs⟩, ⟨hxt, hx hxs⟩ ... ≤ ν (t ∩ s') : le_iff'.1 hμν (t ∩ s') ... = ν.restrict s' t : (restrict_apply ht).symm /-- Restriction of a measure to a subset is monotone both in set and in measure. -/ @[mono] lemma restrict_mono {m0 : measurable_space α} ⦃s s' : set α⦄ (hs : s ⊆ s') ⦃μ ν : measure α⦄ (hμν : μ ≤ ν) : μ.restrict s ≤ ν.restrict s' := restrict_mono' (ae_of_all _ hs) hμν lemma restrict_mono_ae (h : s ≤ᵐ[μ] t) : μ.restrict s ≤ μ.restrict t := restrict_mono' h (le_refl μ) lemma restrict_congr_set (h : s =ᵐ[μ] t) : μ.restrict s = μ.restrict t := le_antisymm (restrict_mono_ae h.le) (restrict_mono_ae h.symm.le) /-- If `s` is a measurable set, then the outer measure of `t` with respect to the restriction of the measure to `s` equals the outer measure of `t ∩ s`. This is an alternate version of `measure.restrict_apply`, requiring that `s` is measurable instead of `t`. -/ @[simp] lemma restrict_apply' (hs : measurable_set s) : μ.restrict s t = μ (t ∩ s) := by rw [← coe_to_outer_measure, measure.restrict_to_outer_measure_eq_to_outer_measure_restrict hs, outer_measure.restrict_apply s t _, coe_to_outer_measure] lemma restrict_apply₀' (hs : null_measurable_set s μ) : μ.restrict s t = μ (t ∩ s) := by rw [← restrict_congr_set hs.to_measurable_ae_eq, restrict_apply' (measurable_set_to_measurable _ _), measure_congr ((ae_eq_refl t).inter hs.to_measurable_ae_eq)] lemma restrict_le_self : μ.restrict s ≤ μ := λ t ht, calc μ.restrict s t = μ (t ∩ s) : restrict_apply ht ... ≤ μ t : measure_mono $ inter_subset_left t s variable (μ) lemma restrict_eq_self (h : s ⊆ t) : μ.restrict t s = μ s := (le_iff'.1 restrict_le_self s).antisymm $ calc μ s ≤ μ (to_measurable (μ.restrict t) s ∩ t) : measure_mono (subset_inter (subset_to_measurable _ _) h) ... = μ.restrict t s : by rw [← restrict_apply (measurable_set_to_measurable _ _), measure_to_measurable] @[simp] lemma restrict_apply_self (s : set α): (μ.restrict s) s = μ s := restrict_eq_self μ subset.rfl variable {μ} lemma restrict_apply_univ (s : set α) : μ.restrict s univ = μ s := by rw [restrict_apply measurable_set.univ, set.univ_inter] lemma le_restrict_apply (s t : set α) : μ (t ∩ s) ≤ μ.restrict s t := calc μ (t ∩ s) = μ.restrict s (t ∩ s) : (restrict_eq_self μ (inter_subset_right _ _)).symm ... ≤ μ.restrict s t : measure_mono (inter_subset_left _ _) lemma restrict_apply_superset (h : s ⊆ t) : μ.restrict s t = μ s := ((measure_mono (subset_univ _)).trans_eq $ restrict_apply_univ _).antisymm ((restrict_apply_self μ s).symm.trans_le $ measure_mono h) @[simp] lemma restrict_add {m0 : measurable_space α} (μ ν : measure α) (s : set α) : (μ + ν).restrict s = μ.restrict s + ν.restrict s := (restrictₗ s).map_add μ ν @[simp] lemma restrict_zero {m0 : measurable_space α} (s : set α) : (0 : measure α).restrict s = 0 := (restrictₗ s).map_zero @[simp] lemma restrict_smul {m0 : measurable_space α} (c : ℝ≥0∞) (μ : measure α) (s : set α) : (c • μ).restrict s = c • μ.restrict s := (restrictₗ s).map_smul c μ lemma restrict_restrict₀ (hs : null_measurable_set s (μ.restrict t)) : (μ.restrict t).restrict s = μ.restrict (s ∩ t) := ext $ λ u hu, by simp only [set.inter_assoc, restrict_apply hu, restrict_apply₀ (hu.null_measurable_set.inter hs)] @[simp] lemma restrict_restrict (hs : measurable_set s) : (μ.restrict t).restrict s = μ.restrict (s ∩ t) := restrict_restrict₀ hs.null_measurable_set lemma restrict_restrict_of_subset (h : s ⊆ t) : (μ.restrict t).restrict s = μ.restrict s := begin ext1 u hu, rw [restrict_apply hu, restrict_apply hu, restrict_eq_self], exact (inter_subset_right _ _).trans h end lemma restrict_restrict₀' (ht : null_measurable_set t μ) : (μ.restrict t).restrict s = μ.restrict (s ∩ t) := ext $ λ u hu, by simp only [restrict_apply hu, restrict_apply₀' ht, inter_assoc] lemma restrict_restrict' (ht : measurable_set t) : (μ.restrict t).restrict s = μ.restrict (s ∩ t) := restrict_restrict₀' ht.null_measurable_set lemma restrict_comm (hs : measurable_set s) : (μ.restrict t).restrict s = (μ.restrict s).restrict t := by rw [restrict_restrict hs, restrict_restrict' hs, inter_comm] lemma restrict_apply_eq_zero (ht : measurable_set t) : μ.restrict s t = 0 ↔ μ (t ∩ s) = 0 := by rw [restrict_apply ht] lemma measure_inter_eq_zero_of_restrict (h : μ.restrict s t = 0) : μ (t ∩ s) = 0 := nonpos_iff_eq_zero.1 (h ▸ le_restrict_apply _ _) lemma restrict_apply_eq_zero' (hs : measurable_set s) : μ.restrict s t = 0 ↔ μ (t ∩ s) = 0 := by rw [restrict_apply' hs] @[simp] lemma restrict_eq_zero : μ.restrict s = 0 ↔ μ s = 0 := by rw [← measure_univ_eq_zero, restrict_apply_univ] lemma restrict_zero_set {s : set α} (h : μ s = 0) : μ.restrict s = 0 := restrict_eq_zero.2 h @[simp] lemma restrict_empty : μ.restrict ∅ = 0 := restrict_zero_set measure_empty @[simp] lemma restrict_univ : μ.restrict univ = μ := ext $ λ s hs, by simp [hs] lemma restrict_inter_add_diff₀ (s : set α) (ht : null_measurable_set t μ) : μ.restrict (s ∩ t) + μ.restrict (s \ t) = μ.restrict s := begin ext1 u hu, simp only [add_apply, restrict_apply hu, ← inter_assoc, diff_eq], exact measure_inter_add_diff₀ (u ∩ s) ht end lemma restrict_inter_add_diff (s : set α) (ht : measurable_set t) : μ.restrict (s ∩ t) + μ.restrict (s \ t) = μ.restrict s := restrict_inter_add_diff₀ s ht.null_measurable_set lemma restrict_union_add_inter₀ (s : set α) (ht : null_measurable_set t μ) : μ.restrict (s ∪ t) + μ.restrict (s ∩ t) = μ.restrict s + μ.restrict t := by rw [← restrict_inter_add_diff₀ (s ∪ t) ht, union_inter_cancel_right, union_diff_right, ← restrict_inter_add_diff₀ s ht, add_comm, ← add_assoc, add_right_comm] lemma restrict_union_add_inter (s : set α) (ht : measurable_set t) : μ.restrict (s ∪ t) + μ.restrict (s ∩ t) = μ.restrict s + μ.restrict t := restrict_union_add_inter₀ s ht.null_measurable_set lemma restrict_union_add_inter' (hs : measurable_set s) (t : set α) : μ.restrict (s ∪ t) + μ.restrict (s ∩ t) = μ.restrict s + μ.restrict t := by simpa only [union_comm, inter_comm, add_comm] using restrict_union_add_inter t hs lemma restrict_union₀ (h : ae_disjoint μ s t) (ht : null_measurable_set t μ) : μ.restrict (s ∪ t) = μ.restrict s + μ.restrict t := by simp [← restrict_union_add_inter₀ s ht, restrict_zero_set h] lemma restrict_union (h : disjoint s t) (ht : measurable_set t) : μ.restrict (s ∪ t) = μ.restrict s + μ.restrict t := restrict_union₀ h.ae_disjoint ht.null_measurable_set lemma restrict_union' (h : disjoint s t) (hs : measurable_set s) : μ.restrict (s ∪ t) = μ.restrict s + μ.restrict t := by rw [union_comm, restrict_union h.symm hs, add_comm] @[simp] lemma restrict_add_restrict_compl (hs : measurable_set s) : μ.restrict s + μ.restrict sᶜ = μ := by rw [← restrict_union (@disjoint_compl_right (set α) _ _) hs.compl, union_compl_self, restrict_univ] @[simp] lemma restrict_compl_add_restrict (hs : measurable_set s) : μ.restrict sᶜ + μ.restrict s = μ := by rw [add_comm, restrict_add_restrict_compl hs] lemma restrict_union_le (s s' : set α) : μ.restrict (s ∪ s') ≤ μ.restrict s + μ.restrict s' := begin intros t ht, suffices : μ (t ∩ s ∪ t ∩ s') ≤ μ (t ∩ s) + μ (t ∩ s'), by simpa [ht, inter_union_distrib_left], apply measure_union_le end lemma restrict_Union_apply_ae [encodable ι] {s : ι → set α} (hd : pairwise (ae_disjoint μ on s)) (hm : ∀ i, null_measurable_set (s i) μ) {t : set α} (ht : measurable_set t) : μ.restrict (⋃ i, s i) t = ∑' i, μ.restrict (s i) t := begin simp only [restrict_apply, ht, inter_Union], exact measure_Union₀ (hd.mono $ λ i j h, h.mono (inter_subset_right _ _) (inter_subset_right _ _)) (λ i, (ht.null_measurable_set.inter (hm i))) end lemma restrict_Union_apply [encodable ι] {s : ι → set α} (hd : pairwise (disjoint on s)) (hm : ∀ i, measurable_set (s i)) {t : set α} (ht : measurable_set t) : μ.restrict (⋃ i, s i) t = ∑' i, μ.restrict (s i) t := restrict_Union_apply_ae hd.ae_disjoint (λ i, (hm i).null_measurable_set) ht lemma restrict_Union_apply_eq_supr [encodable ι] {s : ι → set α} (hd : directed (⊆) s) {t : set α} (ht : measurable_set t) : μ.restrict (⋃ i, s i) t = ⨆ i, μ.restrict (s i) t := begin simp only [restrict_apply ht, inter_Union], rw [measure_Union_eq_supr], exacts [hd.mono_comp _ (λ s₁ s₂, inter_subset_inter_right _)] end /-- The restriction of the pushforward measure is the pushforward of the restriction. For a version assuming only `ae_measurable`, see `restrict_map_of_ae_measurable`. -/ lemma restrict_map {f : α → β} (hf : measurable f) {s : set β} (hs : measurable_set s) : (μ.map f).restrict s = (μ.restrict $ f ⁻¹' s).map f := ext $ λ t ht, by simp [*, hf ht] lemma restrict_to_measurable (h : μ s ≠ ∞) : μ.restrict (to_measurable μ s) = μ.restrict s := ext $ λ t ht, by rw [restrict_apply ht, restrict_apply ht, inter_comm, measure_to_measurable_inter ht h, inter_comm] lemma restrict_eq_self_of_ae_mem {m0 : measurable_space α} ⦃s : set α⦄ ⦃μ : measure α⦄ (hs : ∀ᵐ x ∂μ, x ∈ s) : μ.restrict s = μ := calc μ.restrict s = μ.restrict univ : restrict_congr_set (eventually_eq_univ.mpr hs) ... = μ : restrict_univ lemma restrict_congr_meas (hs : measurable_set s) : μ.restrict s = ν.restrict s ↔ ∀ t ⊆ s, measurable_set t → μ t = ν t := ⟨λ H t hts ht, by rw [← inter_eq_self_of_subset_left hts, ← restrict_apply ht, H, restrict_apply ht], λ H, ext $ λ t ht, by rw [restrict_apply ht, restrict_apply ht, H _ (inter_subset_right _ _) (ht.inter hs)]⟩ lemma restrict_congr_mono (hs : s ⊆ t) (h : μ.restrict t = ν.restrict t) : μ.restrict s = ν.restrict s := by rw [← restrict_restrict_of_subset hs, h, restrict_restrict_of_subset hs] /-- If two measures agree on all measurable subsets of `s` and `t`, then they agree on all measurable subsets of `s ∪ t`. -/ lemma restrict_union_congr : μ.restrict (s ∪ t) = ν.restrict (s ∪ t) ↔ μ.restrict s = ν.restrict s ∧ μ.restrict t = ν.restrict t := begin refine ⟨λ h, ⟨restrict_congr_mono (subset_union_left _ _) h, restrict_congr_mono (subset_union_right _ _) h⟩, _⟩, rintro ⟨hs, ht⟩, ext1 u hu, simp only [restrict_apply hu, inter_union_distrib_left], rcases exists_measurable_superset₂ μ ν (u ∩ s) with ⟨US, hsub, hm, hμ, hν⟩, calc μ (u ∩ s ∪ u ∩ t) = μ (US ∪ u ∩ t) : measure_union_congr_of_subset hsub hμ.le subset.rfl le_rfl ... = μ US + μ (u ∩ t \ US) : (measure_add_diff hm _).symm ... = restrict μ s u + restrict μ t (u \ US) : by simp only [restrict_apply, hu, hu.diff hm, hμ, ← inter_comm t, inter_diff_assoc] ... = restrict ν s u + restrict ν t (u \ US) : by rw [hs, ht] ... = ν US + ν (u ∩ t \ US) : by simp only [restrict_apply, hu, hu.diff hm, hν, ← inter_comm t, inter_diff_assoc] ... = ν (US ∪ u ∩ t) : measure_add_diff hm _ ... = ν (u ∩ s ∪ u ∩ t) : eq.symm $ measure_union_congr_of_subset hsub hν.le subset.rfl le_rfl end lemma restrict_finset_bUnion_congr {s : finset ι} {t : ι → set α} : μ.restrict (⋃ i ∈ s, t i) = ν.restrict (⋃ i ∈ s, t i) ↔ ∀ i ∈ s, μ.restrict (t i) = ν.restrict (t i) := begin induction s using finset.induction_on with i s hi hs, { simp }, simp only [forall_eq_or_imp, Union_Union_eq_or_left, finset.mem_insert], rw [restrict_union_congr, ← hs] end lemma restrict_Union_congr [encodable ι] {s : ι → set α} : μ.restrict (⋃ i, s i) = ν.restrict (⋃ i, s i) ↔ ∀ i, μ.restrict (s i) = ν.restrict (s i) := begin refine ⟨λ h i, restrict_congr_mono (subset_Union _ _) h, λ h, _⟩, ext1 t ht, have D : directed (⊆) (λ t : finset ι, ⋃ i ∈ t, s i) := directed_of_sup (λ t₁ t₂ ht, bUnion_subset_bUnion_left ht), rw [Union_eq_Union_finset], simp only [restrict_Union_apply_eq_supr D ht, restrict_finset_bUnion_congr.2 (λ i hi, h i)], end lemma restrict_bUnion_congr {s : set ι} {t : ι → set α} (hc : s.countable) : μ.restrict (⋃ i ∈ s, t i) = ν.restrict (⋃ i ∈ s, t i) ↔ ∀ i ∈ s, μ.restrict (t i) = ν.restrict (t i) := begin haveI := hc.to_encodable, simp only [bUnion_eq_Union, set_coe.forall', restrict_Union_congr] end lemma restrict_sUnion_congr {S : set (set α)} (hc : S.countable) : μ.restrict (⋃₀ S) = ν.restrict (⋃₀ S) ↔ ∀ s ∈ S, μ.restrict s = ν.restrict s := by rw [sUnion_eq_bUnion, restrict_bUnion_congr hc] /-- This lemma shows that `Inf` and `restrict` commute for measures. -/ lemma restrict_Inf_eq_Inf_restrict {m0 : measurable_space α} {m : set (measure α)} (hm : m.nonempty) (ht : measurable_set t) : (Inf m).restrict t = Inf ((λ μ : measure α, μ.restrict t) '' m) := begin ext1 s hs, simp_rw [Inf_apply hs, restrict_apply hs, Inf_apply (measurable_set.inter hs ht), set.image_image, restrict_to_outer_measure_eq_to_outer_measure_restrict ht, ← set.image_image _ to_outer_measure, ← outer_measure.restrict_Inf_eq_Inf_restrict _ (hm.image _), outer_measure.restrict_apply] end lemma exists_mem_of_measure_ne_zero_of_ae (hs : μ s ≠ 0) {p : α → Prop} (hp : ∀ᵐ x ∂μ.restrict s, p x) : ∃ x, x ∈ s ∧ p x := begin rw [← μ.restrict_apply_self, ← frequently_ae_mem_iff] at hs, exact (hs.and_eventually hp).exists, end /-! ### Extensionality results -/ /-- Two measures are equal if they have equal restrictions on a spanning collection of sets (formulated using `Union`). -/ lemma ext_iff_of_Union_eq_univ [encodable ι] {s : ι → set α} (hs : (⋃ i, s i) = univ) : μ = ν ↔ ∀ i, μ.restrict (s i) = ν.restrict (s i) := by rw [← restrict_Union_congr, hs, restrict_univ, restrict_univ] alias ext_iff_of_Union_eq_univ ↔ _ ext_of_Union_eq_univ /-- Two measures are equal if they have equal restrictions on a spanning collection of sets (formulated using `bUnion`). -/ lemma ext_iff_of_bUnion_eq_univ {S : set ι} {s : ι → set α} (hc : S.countable) (hs : (⋃ i ∈ S, s i) = univ) : μ = ν ↔ ∀ i ∈ S, μ.restrict (s i) = ν.restrict (s i) := by rw [← restrict_bUnion_congr hc, hs, restrict_univ, restrict_univ] alias ext_iff_of_bUnion_eq_univ ↔ _ ext_of_bUnion_eq_univ /-- Two measures are equal if they have equal restrictions on a spanning collection of sets (formulated using `sUnion`). -/ lemma ext_iff_of_sUnion_eq_univ {S : set (set α)} (hc : S.countable) (hs : (⋃₀ S) = univ) : μ = ν ↔ ∀ s ∈ S, μ.restrict s = ν.restrict s := ext_iff_of_bUnion_eq_univ hc $ by rwa ← sUnion_eq_bUnion alias ext_iff_of_sUnion_eq_univ ↔ _ ext_of_sUnion_eq_univ lemma ext_of_generate_from_of_cover {S T : set (set α)} (h_gen : ‹_› = generate_from S) (hc : T.countable) (h_inter : is_pi_system S) (hU : ⋃₀ T = univ) (htop : ∀ t ∈ T, μ t ≠ ∞) (ST_eq : ∀ (t ∈ T) (s ∈ S), μ (s ∩ t) = ν (s ∩ t)) (T_eq : ∀ t ∈ T, μ t = ν t) : μ = ν := begin refine ext_of_sUnion_eq_univ hc hU (λ t ht, _), ext1 u hu, simp only [restrict_apply hu], refine induction_on_inter h_gen h_inter _ (ST_eq t ht) _ _ hu, { simp only [set.empty_inter, measure_empty] }, { intros v hv hvt, have := T_eq t ht, rw [set.inter_comm] at hvt ⊢, rwa [← measure_inter_add_diff t hv, ← measure_inter_add_diff t hv, ← hvt, ennreal.add_right_inj] at this, exact ne_top_of_le_ne_top (htop t ht) (measure_mono $ set.inter_subset_left _ _) }, { intros f hfd hfm h_eq, simp only [← restrict_apply (hfm _), ← restrict_apply (measurable_set.Union hfm)] at h_eq ⊢, simp only [measure_Union hfd hfm, h_eq] } end /-- Two measures are equal if they are equal on the π-system generating the σ-algebra, and they are both finite on a increasing spanning sequence of sets in the π-system. This lemma is formulated using `sUnion`. -/ lemma ext_of_generate_from_of_cover_subset {S T : set (set α)} (h_gen : ‹_› = generate_from S) (h_inter : is_pi_system S) (h_sub : T ⊆ S) (hc : T.countable) (hU : ⋃₀ T = univ) (htop : ∀ s ∈ T, μ s ≠ ∞) (h_eq : ∀ s ∈ S, μ s = ν s) : μ = ν := begin refine ext_of_generate_from_of_cover h_gen hc h_inter hU htop _ (λ t ht, h_eq t (h_sub ht)), intros t ht s hs, cases (s ∩ t).eq_empty_or_nonempty with H H, { simp only [H, measure_empty] }, { exact h_eq _ (h_inter _ hs _ (h_sub ht) H) } end /-- Two measures are equal if they are equal on the π-system generating the σ-algebra, and they are both finite on a increasing spanning sequence of sets in the π-system. This lemma is formulated using `Union`. `finite_spanning_sets_in.ext` is a reformulation of this lemma. -/ lemma ext_of_generate_from_of_Union (C : set (set α)) (B : ℕ → set α) (hA : ‹_› = generate_from C) (hC : is_pi_system C) (h1B : (⋃ i, B i) = univ) (h2B : ∀ i, B i ∈ C) (hμB : ∀ i, μ (B i) ≠ ∞) (h_eq : ∀ s ∈ C, μ s = ν s) : μ = ν := begin refine ext_of_generate_from_of_cover_subset hA hC _ (countable_range B) h1B _ h_eq, { rintro _ ⟨i, rfl⟩, apply h2B }, { rintro _ ⟨i, rfl⟩, apply hμB } end section dirac variable [measurable_space α] /-- The dirac measure. -/ def dirac (a : α) : measure α := (outer_measure.dirac a).to_measure (by simp) instance : measure_space punit := ⟨dirac punit.star⟩ lemma le_dirac_apply {a} : s.indicator 1 a ≤ dirac a s := outer_measure.dirac_apply a s ▸ le_to_measure_apply _ _ _ @[simp] lemma dirac_apply' (a : α) (hs : measurable_set s) : dirac a s = s.indicator 1 a := to_measure_apply _ _ hs @[simp] lemma dirac_apply_of_mem {a : α} (h : a ∈ s) : dirac a s = 1 := begin have : ∀ t : set α, a ∈ t → t.indicator (1 : α → ℝ≥0∞) a = 1, from λ t ht, indicator_of_mem ht 1, refine le_antisymm (this univ trivial ▸ _) (this s h ▸ le_dirac_apply), rw [← dirac_apply' a measurable_set.univ], exact measure_mono (subset_univ s) end @[simp] lemma dirac_apply [measurable_singleton_class α] (a : α) (s : set α) : dirac a s = s.indicator 1 a := begin by_cases h : a ∈ s, by rw [dirac_apply_of_mem h, indicator_of_mem h, pi.one_apply], rw [indicator_of_not_mem h, ← nonpos_iff_eq_zero], calc dirac a s ≤ dirac a {a}ᶜ : measure_mono (subset_compl_comm.1 $ singleton_subset_iff.2 h) ... = 0 : by simp [dirac_apply' _ (measurable_set_singleton _).compl] end lemma map_dirac {f : α → β} (hf : measurable f) (a : α) : (dirac a).map f = dirac (f a) := ext $ λ s hs, by simp [hs, map_apply hf hs, hf hs, indicator_apply] @[simp] lemma restrict_singleton (μ : measure α) (a : α) : μ.restrict {a} = μ {a} • dirac a := begin ext1 s hs, by_cases ha : a ∈ s, { have : s ∩ {a} = {a}, by simpa, simp * }, { have : s ∩ {a} = ∅, from inter_singleton_eq_empty.2 ha, simp * } end end dirac section sum include m0 /-- Sum of an indexed family of measures. -/ def sum (f : ι → measure α) : measure α := (outer_measure.sum (λ i, (f i).to_outer_measure)).to_measure $ le_trans (by exact le_infi (λ i, le_to_outer_measure_caratheodory _)) (outer_measure.le_sum_caratheodory _) lemma le_sum_apply (f : ι → measure α) (s : set α) : (∑' i, f i s) ≤ sum f s := le_to_measure_apply _ _ _ @[simp] lemma sum_apply (f : ι → measure α) {s : set α} (hs : measurable_set s) : sum f s = ∑' i, f i s := to_measure_apply _ _ hs lemma le_sum (μ : ι → measure α) (i : ι) : μ i ≤ sum μ := λ s hs, by simp only [sum_apply μ hs, ennreal.le_tsum i] @[simp] lemma sum_apply_eq_zero [encodable ι] {μ : ι → measure α} {s : set α} : sum μ s = 0 ↔ ∀ i, μ i s = 0 := begin refine ⟨λ h i, nonpos_iff_eq_zero.1 $ h ▸ le_iff'.1 (le_sum μ i) _, λ h, nonpos_iff_eq_zero.1 _⟩, rcases exists_measurable_superset_forall_eq μ s with ⟨t, hst, htm, ht⟩, calc sum μ s ≤ sum μ t : measure_mono hst ... = 0 : by simp * end lemma sum_apply_eq_zero' {μ : ι → measure α} {s : set α} (hs : measurable_set s) : sum μ s = 0 ↔ ∀ i, μ i s = 0 := by simp [hs] lemma ae_sum_iff [encodable ι] {μ : ι → measure α} {p : α → Prop} : (∀ᵐ x ∂(sum μ), p x) ↔ ∀ i, ∀ᵐ x ∂(μ i), p x := sum_apply_eq_zero lemma ae_sum_iff' {μ : ι → measure α} {p : α → Prop} (h : measurable_set {x | p x}) : (∀ᵐ x ∂(sum μ), p x) ↔ ∀ i, ∀ᵐ x ∂(μ i), p x := sum_apply_eq_zero' h.compl @[simp] lemma sum_fintype [fintype ι] (μ : ι → measure α) : sum μ = ∑ i, μ i := by { ext1 s hs, simp only [sum_apply, finset_sum_apply, hs, tsum_fintype] } @[simp] lemma sum_coe_finset (s : finset ι) (μ : ι → measure α) : sum (λ i : s, μ i) = ∑ i in s, μ i := by rw [sum_fintype, finset.sum_coe_sort s μ] @[simp] lemma ae_sum_eq [encodable ι] (μ : ι → measure α) : (sum μ).ae = ⨆ i, (μ i).ae := filter.ext $ λ s, ae_sum_iff.trans mem_supr.symm @[simp] lemma sum_bool (f : bool → measure α) : sum f = f tt + f ff := by rw [sum_fintype, fintype.sum_bool] @[simp] lemma sum_cond (μ ν : measure α) : sum (λ b, cond b μ ν) = μ + ν := sum_bool _ @[simp] lemma restrict_sum (μ : ι → measure α) {s : set α} (hs : measurable_set s) : (sum μ).restrict s = sum (λ i, (μ i).restrict s) := ext $ λ t ht, by simp only [sum_apply, restrict_apply, ht, ht.inter hs] @[simp] lemma sum_of_empty [is_empty ι] (μ : ι → measure α) : sum μ = 0 := by rw [← measure_univ_eq_zero, sum_apply _ measurable_set.univ, tsum_empty] lemma sum_add_sum_compl (s : set ι) (μ : ι → measure α) : sum (λ i : s, μ i) + sum (λ i : sᶜ, μ i) = sum μ := begin ext1 t ht, simp only [add_apply, sum_apply _ ht], exact @tsum_add_tsum_compl ℝ≥0∞ ι _ _ _ (λ i, μ i t) _ s ennreal.summable ennreal.summable end lemma sum_congr {μ ν : ℕ → measure α} (h : ∀ n, μ n = ν n) : sum μ = sum ν := congr_arg sum (funext h) lemma sum_add_sum (μ ν : ℕ → measure α) : sum μ + sum ν = sum (λ n, μ n + ν n) := begin ext1 s hs, simp only [add_apply, sum_apply _ hs, pi.add_apply, coe_add, tsum_add ennreal.summable ennreal.summable], end /-- If `f` is a map with encodable codomain, then `μ.map f` is the sum of Dirac measures -/ lemma map_eq_sum [encodable β] [measurable_singleton_class β] (μ : measure α) (f : α → β) (hf : measurable f) : μ.map f = sum (λ b : β, μ (f ⁻¹' {b}) • dirac b) := begin ext1 s hs, have : ∀ y ∈ s, measurable_set (f ⁻¹' {y}), from λ y _, hf (measurable_set_singleton _), simp [← tsum_measure_preimage_singleton (to_countable s) this, *, tsum_subtype s (λ b, μ (f ⁻¹' {b})), ← indicator_mul_right s (λ b, μ (f ⁻¹' {b}))] end /-- A measure on an encodable type is a sum of dirac measures. -/ @[simp] lemma sum_smul_dirac [encodable α] [measurable_singleton_class α] (μ : measure α) : sum (λ a, μ {a} • dirac a) = μ := by simpa using (map_eq_sum μ id measurable_id).symm omit m0 end sum lemma restrict_Union_ae [encodable ι] {s : ι → set α} (hd : pairwise (ae_disjoint μ on s)) (hm : ∀ i, null_measurable_set (s i) μ) : μ.restrict (⋃ i, s i) = sum (λ i, μ.restrict (s i)) := ext $ λ t ht, by simp only [sum_apply _ ht, restrict_Union_apply_ae hd hm ht] lemma restrict_Union [encodable ι] {s : ι → set α} (hd : pairwise (disjoint on s)) (hm : ∀ i, measurable_set (s i)) : μ.restrict (⋃ i, s i) = sum (λ i, μ.restrict (s i)) := restrict_Union_ae hd.ae_disjoint (λ i, (hm i).null_measurable_set) lemma restrict_Union_le [encodable ι] {s : ι → set α} : μ.restrict (⋃ i, s i) ≤ sum (λ i, μ.restrict (s i)) := begin intros t ht, suffices : μ (⋃ i, t ∩ s i) ≤ ∑' i, μ (t ∩ s i), by simpa [ht, inter_Union], apply measure_Union_le end section count variable [measurable_space α] /-- Counting measure on any measurable space. -/ def count : measure α := sum dirac lemma le_count_apply : (∑' i : s, 1 : ℝ≥0∞) ≤ count s := calc (∑' i : s, 1 : ℝ≥0∞) = ∑' i, indicator s 1 i : tsum_subtype s 1 ... ≤ ∑' i, dirac i s : ennreal.tsum_le_tsum $ λ x, le_dirac_apply ... ≤ count s : le_sum_apply _ _ lemma count_apply (hs : measurable_set s) : count s = ∑' i : s, 1 := by simp only [count, sum_apply, hs, dirac_apply', ← tsum_subtype s 1, pi.one_apply] @[simp] lemma count_empty : count (∅ : set α) = 0 := by rw [count_apply measurable_set.empty, tsum_empty] @[simp] lemma count_apply_finset [measurable_singleton_class α] (s : finset α) : count (↑s : set α) = s.card := calc count (↑s : set α) = ∑' i : (↑s : set α), 1 : count_apply s.measurable_set ... = ∑ i in s, 1 : s.tsum_subtype 1 ... = s.card : by simp lemma count_apply_finite [measurable_singleton_class α] (s : set α) (hs : s.finite) : count s = hs.to_finset.card := by rw [← count_apply_finset, finite.coe_to_finset] /-- `count` measure evaluates to infinity at infinite sets. -/ lemma count_apply_infinite (hs : s.infinite) : count s = ∞ := begin refine top_unique (le_of_tendsto' ennreal.tendsto_nat_nhds_top $ λ n, _), rcases hs.exists_subset_card_eq n with ⟨t, ht, rfl⟩, calc (t.card : ℝ≥0∞) = ∑ i in t, 1 : by simp ... = ∑' i : (t : set α), 1 : (t.tsum_subtype 1).symm ... ≤ count (t : set α) : le_count_apply ... ≤ count s : measure_mono ht end variable [measurable_singleton_class α] @[simp] lemma count_apply_eq_top : count s = ∞ ↔ s.infinite := begin by_cases hs : s.finite, { simp [set.infinite, hs, count_apply_finite] }, { change s.infinite at hs, simp [hs, count_apply_infinite] } end @[simp] lemma count_apply_lt_top : count s < ∞ ↔ s.finite := calc count s < ∞ ↔ count s ≠ ∞ : lt_top_iff_ne_top ... ↔ ¬s.infinite : not_congr count_apply_eq_top ... ↔ s.finite : not_not lemma empty_of_count_eq_zero (hsc : count s = 0) : s = ∅ := begin have hs : s.finite, { rw [← count_apply_lt_top, hsc], exact with_top.zero_lt_top }, rw count_apply_finite _ hs at hsc, simpa using hsc, end @[simp] lemma count_eq_zero_iff : count s = 0 ↔ s = ∅ := ⟨empty_of_count_eq_zero, λ h, h.symm ▸ count_empty⟩ lemma count_ne_zero (hs' : s.nonempty) : count s ≠ 0 := begin rw [ne.def, count_eq_zero_iff], exact hs'.ne_empty, end @[simp] lemma count_singleton (a : α) : count ({a} : set α) = 1 := begin rw [count_apply_finite ({a} : set α) (set.finite_singleton _), set.finite.to_finset], simp, end lemma count_injective_image [measurable_singleton_class β] {f : β → α} (hf : function.injective f) (s : set β) : count (f '' s) = count s := begin by_cases hs : s.finite, { lift s to finset β using hs, rw [← finset.coe_image, count_apply_finset, count_apply_finset, s.card_image_of_injective hf] }, rw count_apply_infinite hs, rw ← (finite_image_iff $ hf.inj_on _) at hs, rw count_apply_infinite hs, end end count /-! ### Absolute continuity -/ /-- We say that `μ` is absolutely continuous with respect to `ν`, or that `μ` is dominated by `ν`, if `ν(A) = 0` implies that `μ(A) = 0`. -/ def absolutely_continuous {m0 : measurable_space α} (μ ν : measure α) : Prop := ∀ ⦃s : set α⦄, ν s = 0 → μ s = 0 localized "infix ` ≪ `:50 := measure_theory.measure.absolutely_continuous" in measure_theory lemma absolutely_continuous_of_le (h : μ ≤ ν) : μ ≪ ν := λ s hs, nonpos_iff_eq_zero.1 $ hs ▸ le_iff'.1 h s alias absolutely_continuous_of_le ← _root_.has_le.le.absolutely_continuous lemma absolutely_continuous_of_eq (h : μ = ν) : μ ≪ ν := h.le.absolutely_continuous alias absolutely_continuous_of_eq ← _root_.eq.absolutely_continuous namespace absolutely_continuous lemma mk (h : ∀ ⦃s : set α⦄, measurable_set s → ν s = 0 → μ s = 0) : μ ≪ ν := begin intros s hs, rcases exists_measurable_superset_of_null hs with ⟨t, h1t, h2t, h3t⟩, exact measure_mono_null h1t (h h2t h3t), end @[refl] protected lemma refl {m0 : measurable_space α} (μ : measure α) : μ ≪ μ := rfl.absolutely_continuous protected lemma rfl : μ ≪ μ := λ s hs, hs instance [measurable_space α] : is_refl (measure α) (≪) := ⟨λ μ, absolutely_continuous.rfl⟩ @[trans] protected lemma trans (h1 : μ₁ ≪ μ₂) (h2 : μ₂ ≪ μ₃) : μ₁ ≪ μ₃ := λ s hs, h1 $ h2 hs @[mono] protected lemma map (h : μ ≪ ν) {f : α → β} (hf : measurable f) : μ.map f ≪ ν.map f := absolutely_continuous.mk $ λ s hs, by simpa [hf, hs] using @h _ protected lemma smul [monoid R] [distrib_mul_action R ℝ≥0∞] [is_scalar_tower R ℝ≥0∞ ℝ≥0∞] (h : μ ≪ ν) (c : R) : c • μ ≪ ν := λ s hνs, by simp only [h hνs, smul_eq_mul, smul_apply, smul_zero] end absolutely_continuous lemma absolutely_continuous_of_le_smul {μ' : measure α} {c : ℝ≥0∞} (hμ'_le : μ' ≤ c • μ) : μ' ≪ μ := (measure.absolutely_continuous_of_le hμ'_le).trans (measure.absolutely_continuous.rfl.smul c) lemma ae_le_iff_absolutely_continuous : μ.ae ≤ ν.ae ↔ μ ≪ ν := ⟨λ h s, by { rw [measure_zero_iff_ae_nmem, measure_zero_iff_ae_nmem], exact λ hs, h hs }, λ h s hs, h hs⟩ alias ae_le_iff_absolutely_continuous ↔ _root_.has_le.le.absolutely_continuous_of_ae absolutely_continuous.ae_le alias absolutely_continuous.ae_le ← ae_mono' lemma absolutely_continuous.ae_eq (h : μ ≪ ν) {f g : α → δ} (h' : f =ᵐ[ν] g) : f =ᵐ[μ] g := h.ae_le h' /-! ### Quasi measure preserving maps (a.k.a. non-singular maps) -/ /-- A map `f : α → β` is said to be *quasi measure preserving* (a.k.a. non-singular) w.r.t. measures `μa` and `μb` if it is measurable and `μb s = 0` implies `μa (f ⁻¹' s) = 0`. -/ @[protect_proj] structure quasi_measure_preserving {m0 : measurable_space α} (f : α → β) (μa : measure α . volume_tac) (μb : measure β . volume_tac) : Prop := (measurable : measurable f) (absolutely_continuous : μa.map f ≪ μb) namespace quasi_measure_preserving protected lemma id {m0 : measurable_space α} (μ : measure α) : quasi_measure_preserving id μ μ := ⟨measurable_id, map_id.absolutely_continuous⟩ variables {μa μa' : measure α} {μb μb' : measure β} {μc : measure γ} {f : α → β} protected lemma _root_.measurable.quasi_measure_preserving {m0 : measurable_space α} (hf : measurable f) (μ : measure α) : quasi_measure_preserving f μ (μ.map f) := ⟨hf, absolutely_continuous.rfl⟩ lemma mono_left (h : quasi_measure_preserving f μa μb) (ha : μa' ≪ μa) : quasi_measure_preserving f μa' μb := ⟨h.1, (ha.map h.1).trans h.2⟩ lemma mono_right (h : quasi_measure_preserving f μa μb) (ha : μb ≪ μb') : quasi_measure_preserving f μa μb' := ⟨h.1, h.2.trans ha⟩ @[mono] lemma mono (ha : μa' ≪ μa) (hb : μb ≪ μb') (h : quasi_measure_preserving f μa μb) : quasi_measure_preserving f μa' μb' := (h.mono_left ha).mono_right hb protected lemma comp {g : β → γ} {f : α → β} (hg : quasi_measure_preserving g μb μc) (hf : quasi_measure_preserving f μa μb) : quasi_measure_preserving (g ∘ f) μa μc := ⟨hg.measurable.comp hf.measurable, by { rw ← map_map hg.1 hf.1, exact (hf.2.map hg.1).trans hg.2 }⟩ protected lemma iterate {f : α → α} (hf : quasi_measure_preserving f μa μa) : ∀ n, quasi_measure_preserving (f^[n]) μa μa | 0 := quasi_measure_preserving.id μa | (n + 1) := (iterate n).comp hf protected lemma ae_measurable (hf : quasi_measure_preserving f μa μb) : ae_measurable f μa := hf.1.ae_measurable lemma ae_map_le (h : quasi_measure_preserving f μa μb) : (μa.map f).ae ≤ μb.ae := h.2.ae_le lemma tendsto_ae (h : quasi_measure_preserving f μa μb) : tendsto f μa.ae μb.ae := (tendsto_ae_map h.ae_measurable).mono_right h.ae_map_le lemma ae (h : quasi_measure_preserving f μa μb) {p : β → Prop} (hg : ∀ᵐ x ∂μb, p x) : ∀ᵐ x ∂μa, p (f x) := h.tendsto_ae hg lemma ae_eq (h : quasi_measure_preserving f μa μb) {g₁ g₂ : β → δ} (hg : g₁ =ᵐ[μb] g₂) : g₁ ∘ f =ᵐ[μa] g₂ ∘ f := h.ae hg lemma preimage_null (h : quasi_measure_preserving f μa μb) {s : set β} (hs : μb s = 0) : μa (f ⁻¹' s) = 0 := preimage_null_of_map_null h.ae_measurable (h.2 hs) end quasi_measure_preserving /-! ### The `cofinite` filter -/ /-- The filter of sets `s` such that `sᶜ` has finite measure. -/ def cofinite {m0 : measurable_space α} (μ : measure α) : filter α := { sets := {s | μ sᶜ < ∞}, univ_sets := by simp, inter_sets := λ s t hs ht, by { simp only [compl_inter, mem_set_of_eq], calc μ (sᶜ ∪ tᶜ) ≤ μ sᶜ + μ tᶜ : measure_union_le _ _ ... < ∞ : ennreal.add_lt_top.2 ⟨hs, ht⟩ }, sets_of_superset := λ s t hs hst, lt_of_le_of_lt (measure_mono $ compl_subset_compl.2 hst) hs } lemma mem_cofinite : s ∈ μ.cofinite ↔ μ sᶜ < ∞ := iff.rfl lemma compl_mem_cofinite : sᶜ ∈ μ.cofinite ↔ μ s < ∞ := by rw [mem_cofinite, compl_compl] lemma eventually_cofinite {p : α → Prop} : (∀ᶠ x in μ.cofinite, p x) ↔ μ {x | ¬p x} < ∞ := iff.rfl end measure open measure open_locale measure_theory /-- The preimage of a null measurable set under a (quasi) measure preserving map is a null measurable set. -/ lemma null_measurable_set.preimage {ν : measure β} {f : α → β} {t : set β} (ht : null_measurable_set t ν) (hf : quasi_measure_preserving f μ ν) : null_measurable_set (f ⁻¹' t) μ := ⟨f ⁻¹' (to_measurable ν t), hf.measurable (measurable_set_to_measurable _ _), hf.ae_eq ht.to_measurable_ae_eq.symm⟩ lemma null_measurable_set.mono_ac (h : null_measurable_set s μ) (hle : ν ≪ μ) : null_measurable_set s ν := h.preimage $ (quasi_measure_preserving.id μ).mono_left hle lemma null_measurable_set.mono (h : null_measurable_set s μ) (hle : ν ≤ μ) : null_measurable_set s ν := h.mono_ac hle.absolutely_continuous lemma ae_disjoint.preimage {ν : measure β} {f : α → β} {s t : set β} (ht : ae_disjoint ν s t) (hf : quasi_measure_preserving f μ ν) : ae_disjoint μ (f ⁻¹' s) (f ⁻¹' t) := hf.preimage_null ht @[simp] lemma ae_eq_bot : μ.ae = ⊥ ↔ μ = 0 := by rw [← empty_mem_iff_bot, mem_ae_iff, compl_empty, measure_univ_eq_zero] @[simp] lemma ae_ne_bot : μ.ae.ne_bot ↔ μ ≠ 0 := ne_bot_iff.trans (not_congr ae_eq_bot) @[simp] lemma ae_zero {m0 : measurable_space α} : (0 : measure α).ae = ⊥ := ae_eq_bot.2 rfl @[mono] lemma ae_mono (h : μ ≤ ν) : μ.ae ≤ ν.ae := h.absolutely_continuous.ae_le lemma mem_ae_map_iff {f : α → β} (hf : ae_measurable f μ) {s : set β} (hs : measurable_set s) : s ∈ (μ.map f).ae ↔ (f ⁻¹' s) ∈ μ.ae := by simp only [mem_ae_iff, map_apply_of_ae_measurable hf hs.compl, preimage_compl] lemma mem_ae_of_mem_ae_map {f : α → β} (hf : ae_measurable f μ) {s : set β} (hs : s ∈ (μ.map f).ae) : f ⁻¹' s ∈ μ.ae := (tendsto_ae_map hf).eventually hs lemma ae_map_iff {f : α → β} (hf : ae_measurable f μ) {p : β → Prop} (hp : measurable_set {x | p x}) : (∀ᵐ y ∂ (μ.map f), p y) ↔ ∀ᵐ x ∂ μ, p (f x) := mem_ae_map_iff hf hp lemma ae_of_ae_map {f : α → β} (hf : ae_measurable f μ) {p : β → Prop} (h : ∀ᵐ y ∂ (μ.map f), p y) : ∀ᵐ x ∂ μ, p (f x) := mem_ae_of_mem_ae_map hf h lemma ae_map_mem_range {m0 : measurable_space α} (f : α → β) (hf : measurable_set (range f)) (μ : measure α) : ∀ᵐ x ∂(μ.map f), x ∈ range f := begin by_cases h : ae_measurable f μ, { change range f ∈ (μ.map f).ae, rw mem_ae_map_iff h hf, apply eventually_of_forall, exact mem_range_self }, { simp [map_of_not_ae_measurable h] } end @[simp] lemma ae_restrict_Union_eq [encodable ι] (s : ι → set α) : (μ.restrict (⋃ i, s i)).ae = ⨆ i, (μ.restrict (s i)).ae := le_antisymm (ae_sum_eq (λ i, μ.restrict (s i)) ▸ ae_mono restrict_Union_le) $ supr_le $ λ i, ae_mono $ restrict_mono (subset_Union s i) le_rfl @[simp] lemma ae_restrict_union_eq (s t : set α) : (μ.restrict (s ∪ t)).ae = (μ.restrict s).ae ⊔ (μ.restrict t).ae := by simp [union_eq_Union, supr_bool_eq] lemma ae_restrict_interval_oc_eq [linear_order α] (a b : α) : (μ.restrict (Ι a b)).ae = (μ.restrict (Ioc a b)).ae ⊔ (μ.restrict (Ioc b a)).ae := by simp only [interval_oc_eq_union, ae_restrict_union_eq] /-- See also `measure_theory.ae_interval_oc_iff`. -/ lemma ae_restrict_interval_oc_iff [linear_order α] {a b : α} {P : α → Prop} : (∀ᵐ x ∂μ.restrict (Ι a b), P x) ↔ (∀ᵐ x ∂μ.restrict (Ioc a b), P x) ∧ (∀ᵐ x ∂μ.restrict (Ioc b a), P x) := by rw [ae_restrict_interval_oc_eq, eventually_sup] lemma ae_restrict_iff {p : α → Prop} (hp : measurable_set {x | p x}) : (∀ᵐ x ∂(μ.restrict s), p x) ↔ ∀ᵐ x ∂μ, x ∈ s → p x := begin simp only [ae_iff, ← compl_set_of, restrict_apply hp.compl], congr' with x, simp [and_comm] end lemma ae_imp_of_ae_restrict {s : set α} {p : α → Prop} (h : ∀ᵐ x ∂(μ.restrict s), p x) : ∀ᵐ x ∂μ, x ∈ s → p x := begin simp only [ae_iff] at h ⊢, simpa [set_of_and, inter_comm] using measure_inter_eq_zero_of_restrict h end lemma ae_restrict_iff' {p : α → Prop} (hs : measurable_set s) : (∀ᵐ x ∂(μ.restrict s), p x) ↔ ∀ᵐ x ∂μ, x ∈ s → p x := begin simp only [ae_iff, ← compl_set_of, restrict_apply_eq_zero' hs], congr' with x, simp [and_comm] end lemma _root_.filter.eventually_eq.restrict {f g : α → δ} {s : set α} (hfg : f =ᵐ[μ] g) : f =ᵐ[μ.restrict s] g := begin -- note that we cannot use `ae_restrict_iff` since we do not require measurability refine hfg.filter_mono _, rw measure.ae_le_iff_absolutely_continuous, exact measure.absolutely_continuous_of_le measure.restrict_le_self, end lemma ae_restrict_mem (hs : measurable_set s) : ∀ᵐ x ∂(μ.restrict s), x ∈ s := (ae_restrict_iff' hs).2 (filter.eventually_of_forall (λ x, id)) lemma ae_restrict_mem₀ (hs : null_measurable_set s μ) : ∀ᵐ x ∂(μ.restrict s), x ∈ s := begin rcases hs.exists_measurable_subset_ae_eq with ⟨t, hts, htm, ht_eq⟩, rw ← restrict_congr_set ht_eq, exact (ae_restrict_mem htm).mono hts end lemma ae_restrict_of_ae {s : set α} {p : α → Prop} (h : ∀ᵐ x ∂μ, p x) : (∀ᵐ x ∂(μ.restrict s), p x) := eventually.filter_mono (ae_mono measure.restrict_le_self) h lemma ae_restrict_of_ae_restrict_of_subset {s t : set α} {p : α → Prop} (hst : s ⊆ t) (h : ∀ᵐ x ∂(μ.restrict t), p x) : (∀ᵐ x ∂(μ.restrict s), p x) := h.filter_mono (ae_mono $ measure.restrict_mono hst (le_refl μ)) lemma ae_of_ae_restrict_of_ae_restrict_compl (t : set α) {p : α → Prop} (ht : ∀ᵐ x ∂(μ.restrict t), p x) (htc : ∀ᵐ x ∂(μ.restrict tᶜ), p x) : ∀ᵐ x ∂μ, p x := nonpos_iff_eq_zero.1 $ calc μ {x | ¬p x} = μ ({x | ¬p x} ∩ t ∪ {x | ¬p x} ∩ tᶜ) : by rw [← inter_union_distrib_left, union_compl_self, inter_univ] ... ≤ μ ({x | ¬p x} ∩ t) + μ ({x | ¬p x} ∩ tᶜ) : measure_union_le _ _ ... ≤ μ.restrict t {x | ¬p x} + μ.restrict tᶜ {x | ¬p x} : add_le_add (le_restrict_apply _ _) (le_restrict_apply _ _) ... = 0 : by rw [ae_iff.1 ht, ae_iff.1 htc, zero_add] lemma mem_map_restrict_ae_iff {β} {s : set α} {t : set β} {f : α → β} (hs : measurable_set s) : t ∈ filter.map f (μ.restrict s).ae ↔ μ ((f ⁻¹' t)ᶜ ∩ s) = 0 := by rw [mem_map, mem_ae_iff, measure.restrict_apply' hs] lemma ae_smul_measure {p : α → Prop} [monoid R] [distrib_mul_action R ℝ≥0∞] [is_scalar_tower R ℝ≥0∞ ℝ≥0∞] (h : ∀ᵐ x ∂μ, p x) (c : R) : ∀ᵐ x ∂(c • μ), p x := ae_iff.2 $ by rw [smul_apply, ae_iff.1 h, smul_zero] lemma ae_add_measure_iff {p : α → Prop} {ν} : (∀ᵐ x ∂μ + ν, p x) ↔ (∀ᵐ x ∂μ, p x) ∧ ∀ᵐ x ∂ν, p x := add_eq_zero_iff lemma ae_eq_comp' {ν : measure β} {f : α → β} {g g' : β → δ} (hf : ae_measurable f μ) (h : g =ᵐ[ν] g') (h2 : μ.map f ≪ ν) : g ∘ f =ᵐ[μ] g' ∘ f := (tendsto_ae_map hf).mono_right h2.ae_le h lemma ae_eq_comp {f : α → β} {g g' : β → δ} (hf : ae_measurable f μ) (h : g =ᵐ[μ.map f] g') : g ∘ f =ᵐ[μ] g' ∘ f := ae_eq_comp' hf h absolutely_continuous.rfl lemma sub_ae_eq_zero {β} [add_group β] (f g : α → β) : f - g =ᵐ[μ] 0 ↔ f =ᵐ[μ] g := begin refine ⟨λ h, h.mono (λ x hx, _), λ h, h.mono (λ x hx, _)⟩, { rwa [pi.sub_apply, pi.zero_apply, sub_eq_zero] at hx, }, { rwa [pi.sub_apply, pi.zero_apply, sub_eq_zero], }, end lemma le_ae_restrict : μ.ae ⊓ 𝓟 s ≤ (μ.restrict s).ae := λ s hs, eventually_inf_principal.2 (ae_imp_of_ae_restrict hs) @[simp] lemma ae_restrict_eq (hs : measurable_set s) : (μ.restrict s).ae = μ.ae ⊓ 𝓟 s := begin ext t, simp only [mem_inf_principal, mem_ae_iff, restrict_apply_eq_zero' hs, compl_set_of, not_imp, and_comm (_ ∈ s)], refl end @[simp] lemma ae_restrict_eq_bot {s} : (μ.restrict s).ae = ⊥ ↔ μ s = 0 := ae_eq_bot.trans restrict_eq_zero @[simp] lemma ae_restrict_ne_bot {s} : (μ.restrict s).ae.ne_bot ↔ 0 < μ s := ne_bot_iff.trans $ (not_congr ae_restrict_eq_bot).trans pos_iff_ne_zero.symm lemma self_mem_ae_restrict {s} (hs : measurable_set s) : s ∈ (μ.restrict s).ae := by simp only [ae_restrict_eq hs, exists_prop, mem_principal, mem_inf_iff]; exact ⟨_, univ_mem, s, subset.rfl, (univ_inter s).symm⟩ /-- If two measurable sets are ae_eq then any proposition that is almost everywhere true on one is almost everywhere true on the other -/ lemma ae_restrict_of_ae_eq_of_ae_restrict {s t} (hst : s =ᵐ[μ] t) {p : α → Prop} : (∀ᵐ x ∂μ.restrict s, p x) → (∀ᵐ x ∂μ.restrict t, p x) := by simp [measure.restrict_congr_set hst] /-- If two measurable sets are ae_eq then any proposition that is almost everywhere true on one is almost everywhere true on the other -/ lemma ae_restrict_congr_set {s t} (hst : s =ᵐ[μ] t) {p : α → Prop} : (∀ᵐ x ∂μ.restrict s, p x) ↔ (∀ᵐ x ∂μ.restrict t, p x) := ⟨ae_restrict_of_ae_eq_of_ae_restrict hst, ae_restrict_of_ae_eq_of_ae_restrict hst.symm⟩ /-- A version of the **Borel-Cantelli lemma**: if `pᵢ` is a sequence of predicates such that `∑ μ {x | pᵢ x}` is finite, then the measure of `x` such that `pᵢ x` holds frequently as `i → ∞` (or equivalently, `pᵢ x` holds for infinitely many `i`) is equal to zero. -/ lemma measure_set_of_frequently_eq_zero {p : ℕ → α → Prop} (hp : ∑' i, μ {x | p i x} ≠ ∞) : μ {x | ∃ᶠ n in at_top, p n x} = 0 := by simpa only [limsup_eq_infi_supr_of_nat, frequently_at_top, set_of_forall, set_of_exists] using measure_limsup_eq_zero hp /-- A version of the **Borel-Cantelli lemma**: if `sᵢ` is a sequence of sets such that `∑ μ sᵢ` exists, then for almost all `x`, `x` does not belong to almost all `sᵢ`. -/ lemma ae_eventually_not_mem {s : ℕ → set α} (hs : ∑' i, μ (s i) ≠ ∞) : ∀ᵐ x ∂ μ, ∀ᶠ n in at_top, x ∉ s n := measure_set_of_frequently_eq_zero hs section intervals lemma bsupr_measure_Iic [preorder α] {s : set α} (hsc : s.countable) (hst : ∀ x : α, ∃ y ∈ s, x ≤ y) (hdir : directed_on (≤) s) : (⨆ x ∈ s, μ (Iic x)) = μ univ := begin rw ← measure_bUnion_eq_supr hsc, { congr, exact Union₂_eq_univ_iff.2 hst }, { exact directed_on_iff_directed.2 (hdir.directed_coe.mono_comp _ $ λ x y, Iic_subset_Iic.2) } end variables [partial_order α] {a b : α} lemma Iio_ae_eq_Iic' (ha : μ {a} = 0) : Iio a =ᵐ[μ] Iic a := by rw [←Iic_diff_right, diff_ae_eq_self, measure_mono_null (set.inter_subset_right _ _) ha] lemma Ioi_ae_eq_Ici' (ha : μ {a} = 0) : Ioi a =ᵐ[μ] Ici a := @Iio_ae_eq_Iic' αᵒᵈ ‹_› ‹_› _ _ ha lemma Ioo_ae_eq_Ioc' (hb : μ {b} = 0) : Ioo a b =ᵐ[μ] Ioc a b := (ae_eq_refl _).inter (Iio_ae_eq_Iic' hb) lemma Ioc_ae_eq_Icc' (ha : μ {a} = 0) : Ioc a b =ᵐ[μ] Icc a b := (Ioi_ae_eq_Ici' ha).inter (ae_eq_refl _) lemma Ioo_ae_eq_Ico' (ha : μ {a} = 0) : Ioo a b =ᵐ[μ] Ico a b := (Ioi_ae_eq_Ici' ha).inter (ae_eq_refl _) lemma Ioo_ae_eq_Icc' (ha : μ {a} = 0) (hb : μ {b} = 0) : Ioo a b =ᵐ[μ] Icc a b := (Ioi_ae_eq_Ici' ha).inter (Iio_ae_eq_Iic' hb) lemma Ico_ae_eq_Icc' (hb : μ {b} = 0) : Ico a b =ᵐ[μ] Icc a b := (ae_eq_refl _).inter (Iio_ae_eq_Iic' hb) lemma Ico_ae_eq_Ioc' (ha : μ {a} = 0) (hb : μ {b} = 0) : Ico a b =ᵐ[μ] Ioc a b := (Ioo_ae_eq_Ico' ha).symm.trans (Ioo_ae_eq_Ioc' hb) end intervals section dirac variable [measurable_space α] lemma mem_ae_dirac_iff {a : α} (hs : measurable_set s) : s ∈ (dirac a).ae ↔ a ∈ s := by by_cases a ∈ s; simp [mem_ae_iff, dirac_apply', hs.compl, indicator_apply, *] lemma ae_dirac_iff {a : α} {p : α → Prop} (hp : measurable_set {x | p x}) : (∀ᵐ x ∂(dirac a), p x) ↔ p a := mem_ae_dirac_iff hp @[simp] lemma ae_dirac_eq [measurable_singleton_class α] (a : α) : (dirac a).ae = pure a := by { ext s, simp [mem_ae_iff, imp_false] } lemma ae_eq_dirac' [measurable_singleton_class β] {a : α} {f : α → β} (hf : measurable f) : f =ᵐ[dirac a] const α (f a) := (ae_dirac_iff $ show measurable_set (f ⁻¹' {f a}), from hf $ measurable_set_singleton _).2 rfl lemma ae_eq_dirac [measurable_singleton_class α] {a : α} (f : α → δ) : f =ᵐ[dirac a] const α (f a) := by simp [filter.eventually_eq] end dirac section is_finite_measure include m0 /-- A measure `μ` is called finite if `μ univ < ∞`. -/ class is_finite_measure (μ : measure α) : Prop := (measure_univ_lt_top : μ univ < ∞) instance restrict.is_finite_measure (μ : measure α) [hs : fact (μ s < ∞)] : is_finite_measure (μ.restrict s) := ⟨by simp [hs.elim]⟩ lemma measure_lt_top (μ : measure α) [is_finite_measure μ] (s : set α) : μ s < ∞ := (measure_mono (subset_univ s)).trans_lt is_finite_measure.measure_univ_lt_top instance is_finite_measure_restrict (μ : measure α) (s : set α) [h : is_finite_measure μ] : is_finite_measure (μ.restrict s) := ⟨by simp [measure_lt_top μ s]⟩ lemma measure_ne_top (μ : measure α) [is_finite_measure μ] (s : set α) : μ s ≠ ∞ := ne_of_lt (measure_lt_top μ s) lemma measure_compl_le_add_of_le_add [is_finite_measure μ] (hs : measurable_set s) (ht : measurable_set t) {ε : ℝ≥0∞} (h : μ s ≤ μ t + ε) : μ tᶜ ≤ μ sᶜ + ε := begin rw [measure_compl ht (measure_ne_top μ _), measure_compl hs (measure_ne_top μ _), tsub_le_iff_right], calc μ univ = μ univ - μ s + μ s : (tsub_add_cancel_of_le $ measure_mono s.subset_univ).symm ... ≤ μ univ - μ s + (μ t + ε) : add_le_add_left h _ ... = _ : by rw [add_right_comm, add_assoc] end lemma measure_compl_le_add_iff [is_finite_measure μ] (hs : measurable_set s) (ht : measurable_set t) {ε : ℝ≥0∞} : μ sᶜ ≤ μ tᶜ + ε ↔ μ t ≤ μ s + ε := ⟨λ h, compl_compl s ▸ compl_compl t ▸ measure_compl_le_add_of_le_add hs.compl ht.compl h, measure_compl_le_add_of_le_add ht hs⟩ /-- The measure of the whole space with respect to a finite measure, considered as `ℝ≥0`. -/ def measure_univ_nnreal (μ : measure α) : ℝ≥0 := (μ univ).to_nnreal @[simp] lemma coe_measure_univ_nnreal (μ : measure α) [is_finite_measure μ] : ↑(measure_univ_nnreal μ) = μ univ := ennreal.coe_to_nnreal (measure_ne_top μ univ) instance is_finite_measure_zero : is_finite_measure (0 : measure α) := ⟨by simp⟩ @[priority 100] instance is_finite_measure_of_is_empty [is_empty α] : is_finite_measure μ := by { rw eq_zero_of_is_empty μ, apply_instance } @[simp] lemma measure_univ_nnreal_zero : measure_univ_nnreal (0 : measure α) = 0 := rfl omit m0 instance is_finite_measure_add [is_finite_measure μ] [is_finite_measure ν] : is_finite_measure (μ + ν) := { measure_univ_lt_top := begin rw [measure.coe_add, pi.add_apply, ennreal.add_lt_top], exact ⟨measure_lt_top _ _, measure_lt_top _ _⟩, end } instance is_finite_measure_smul_nnreal [is_finite_measure μ] {r : ℝ≥0} : is_finite_measure (r • μ) := { measure_univ_lt_top := ennreal.mul_lt_top ennreal.coe_ne_top (measure_ne_top _ _) } instance is_finite_measure_smul_of_nnreal_tower {R} [has_smul R ℝ≥0] [has_smul R ℝ≥0∞] [is_scalar_tower R ℝ≥0 ℝ≥0∞] [is_scalar_tower R ℝ≥0∞ ℝ≥0∞] [is_finite_measure μ] {r : R} : is_finite_measure (r • μ) := begin rw ←smul_one_smul ℝ≥0 r μ, apply_instance, end lemma is_finite_measure_of_le (μ : measure α) [is_finite_measure μ] (h : ν ≤ μ) : is_finite_measure ν := { measure_univ_lt_top := lt_of_le_of_lt (h set.univ measurable_set.univ) (measure_lt_top _ _) } @[instance] lemma measure.is_finite_measure_map {m : measurable_space α} (μ : measure α) [is_finite_measure μ] (f : α → β) : is_finite_measure (μ.map f) := begin by_cases hf : ae_measurable f μ, { constructor, rw map_apply_of_ae_measurable hf measurable_set.univ, exact measure_lt_top μ _ }, { rw map_of_not_ae_measurable hf, exact measure_theory.is_finite_measure_zero } end @[simp] lemma measure_univ_nnreal_eq_zero [is_finite_measure μ] : measure_univ_nnreal μ = 0 ↔ μ = 0 := begin rw [← measure_theory.measure.measure_univ_eq_zero, ← coe_measure_univ_nnreal], norm_cast end lemma measure_univ_nnreal_pos [is_finite_measure μ] (hμ : μ ≠ 0) : 0 < measure_univ_nnreal μ := begin contrapose! hμ, simpa [measure_univ_nnreal_eq_zero, le_zero_iff] using hμ end /-- `le_of_add_le_add_left` is normally applicable to `ordered_cancel_add_comm_monoid`, but it holds for measures with the additional assumption that μ is finite. -/ lemma measure.le_of_add_le_add_left [is_finite_measure μ] (A2 : μ + ν₁ ≤ μ + ν₂) : ν₁ ≤ ν₂ := λ S B1, ennreal.le_of_add_le_add_left (measure_theory.measure_ne_top μ S) (A2 S B1) lemma summable_measure_to_real [hμ : is_finite_measure μ] {f : ℕ → set α} (hf₁ : ∀ (i : ℕ), measurable_set (f i)) (hf₂ : pairwise (disjoint on f)) : summable (λ x, (μ (f x)).to_real) := begin apply ennreal.summable_to_real, rw ← measure_theory.measure_Union hf₂ hf₁, exact ne_of_lt (measure_lt_top _ _) end end is_finite_measure section is_probability_measure include m0 /-- A measure `μ` is called a probability measure if `μ univ = 1`. -/ class is_probability_measure (μ : measure α) : Prop := (measure_univ : μ univ = 1) export is_probability_measure (measure_univ) attribute [simp] is_probability_measure.measure_univ @[priority 100] instance is_probability_measure.to_is_finite_measure (μ : measure α) [is_probability_measure μ] : is_finite_measure μ := ⟨by simp only [measure_univ, ennreal.one_lt_top]⟩ lemma is_probability_measure.ne_zero (μ : measure α) [is_probability_measure μ] : μ ≠ 0 := mt measure_univ_eq_zero.2 $ by simp [measure_univ] @[priority 200] instance is_probability_measure.ae_ne_bot [is_probability_measure μ] : ne_bot μ.ae := ae_ne_bot.2 (is_probability_measure.ne_zero μ) omit m0 instance measure.dirac.is_probability_measure [measurable_space α] {x : α} : is_probability_measure (dirac x) := ⟨dirac_apply_of_mem $ mem_univ x⟩ lemma prob_add_prob_compl [is_probability_measure μ] (h : measurable_set s) : μ s + μ sᶜ = 1 := (measure_add_measure_compl h).trans measure_univ lemma prob_le_one [is_probability_measure μ] : μ s ≤ 1 := (measure_mono $ set.subset_univ _).trans_eq measure_univ lemma is_probability_measure_smul [is_finite_measure μ] (h : μ ≠ 0) : is_probability_measure ((μ univ)⁻¹ • μ) := begin constructor, rw [smul_apply, smul_eq_mul, ennreal.inv_mul_cancel], { rwa [ne, measure_univ_eq_zero] }, { exact measure_ne_top _ _ } end lemma is_probability_measure_map [is_probability_measure μ] {f : α → β} (hf : ae_measurable f μ) : is_probability_measure (map f μ) := ⟨by simp [map_apply_of_ae_measurable, hf]⟩ end is_probability_measure section no_atoms /-- Measure `μ` *has no atoms* if the measure of each singleton is zero. NB: Wikipedia assumes that for any measurable set `s` with positive `μ`-measure, there exists a measurable `t ⊆ s` such that `0 < μ t < μ s`. While this implies `μ {x} = 0`, the converse is not true. -/ class has_no_atoms {m0 : measurable_space α} (μ : measure α) : Prop := (measure_singleton : ∀ x, μ {x} = 0) export has_no_atoms (measure_singleton) attribute [simp] measure_singleton variables [has_no_atoms μ] lemma _root_.set.subsingleton.measure_zero {α : Type*} {m : measurable_space α} {s : set α} (hs : s.subsingleton) (μ : measure α) [has_no_atoms μ] : μ s = 0 := hs.induction_on measure_empty measure_singleton lemma measure.restrict_singleton' {a : α} : μ.restrict {a} = 0 := by simp only [measure_singleton, measure.restrict_eq_zero] instance (s : set α) : has_no_atoms (μ.restrict s) := begin refine ⟨λ x, _⟩, obtain ⟨t, hxt, ht1, ht2⟩ := exists_measurable_superset_of_null (measure_singleton x : μ {x} = 0), apply measure_mono_null hxt, rw measure.restrict_apply ht1, apply measure_mono_null (inter_subset_left t s) ht2 end lemma _root_.set.countable.measure_zero {α : Type*} {m : measurable_space α} {s : set α} (h : s.countable) (μ : measure α) [has_no_atoms μ] : μ s = 0 := begin rw [← bUnion_of_singleton s, ← nonpos_iff_eq_zero], refine le_trans (measure_bUnion_le h _) _, simp end lemma _root_.set.countable.ae_not_mem {α : Type*} {m : measurable_space α} {s : set α} (h : s.countable) (μ : measure α) [has_no_atoms μ] : ∀ᵐ x ∂μ, x ∉ s := by simpa only [ae_iff, not_not] using h.measure_zero μ lemma _root_.set.finite.measure_zero {α : Type*} {m : measurable_space α} {s : set α} (h : s.finite) (μ : measure α) [has_no_atoms μ] : μ s = 0 := h.countable.measure_zero μ lemma _root_.finset.measure_zero {α : Type*} {m : measurable_space α} (s : finset α) (μ : measure α) [has_no_atoms μ] : μ s = 0 := s.finite_to_set.measure_zero μ lemma insert_ae_eq_self (a : α) (s : set α) : (insert a s : set α) =ᵐ[μ] s := union_ae_eq_right.2 $ measure_mono_null (diff_subset _ _) (measure_singleton _) section variables [partial_order α] {a b : α} lemma Iio_ae_eq_Iic : Iio a =ᵐ[μ] Iic a := Iio_ae_eq_Iic' (measure_singleton a) lemma Ioi_ae_eq_Ici : Ioi a =ᵐ[μ] Ici a := Ioi_ae_eq_Ici' (measure_singleton a) lemma Ioo_ae_eq_Ioc : Ioo a b =ᵐ[μ] Ioc a b := Ioo_ae_eq_Ioc' (measure_singleton b) lemma Ioc_ae_eq_Icc : Ioc a b =ᵐ[μ] Icc a b := Ioc_ae_eq_Icc' (measure_singleton a) lemma Ioo_ae_eq_Ico : Ioo a b =ᵐ[μ] Ico a b := Ioo_ae_eq_Ico' (measure_singleton a) lemma Ioo_ae_eq_Icc : Ioo a b =ᵐ[μ] Icc a b := Ioo_ae_eq_Icc' (measure_singleton a) (measure_singleton b) lemma Ico_ae_eq_Icc : Ico a b =ᵐ[μ] Icc a b := Ico_ae_eq_Icc' (measure_singleton b) lemma Ico_ae_eq_Ioc : Ico a b =ᵐ[μ] Ioc a b := Ico_ae_eq_Ioc' (measure_singleton a) (measure_singleton b) end open_locale interval lemma interval_oc_ae_eq_interval [linear_order α] {a b : α} : Ι a b =ᵐ[μ] [a, b] := Ioc_ae_eq_Icc end no_atoms lemma ite_ae_eq_of_measure_zero {γ} (f : α → γ) (g : α → γ) (s : set α) (hs_zero : μ s = 0) : (λ x, ite (x ∈ s) (f x) (g x)) =ᵐ[μ] g := begin have h_ss : sᶜ ⊆ {a : α | ite (a ∈ s) (f a) (g a) = g a}, from λ x hx, by simp [(set.mem_compl_iff _ _).mp hx], refine measure_mono_null _ hs_zero, nth_rewrite 0 ←compl_compl s, rwa set.compl_subset_compl, end lemma ite_ae_eq_of_measure_compl_zero {γ} (f : α → γ) (g : α → γ) (s : set α) (hs_zero : μ sᶜ = 0) : (λ x, ite (x ∈ s) (f x) (g x)) =ᵐ[μ] f := by { filter_upwards [hs_zero], intros, split_ifs, refl } namespace measure /-- A measure is called finite at filter `f` if it is finite at some set `s ∈ f`. Equivalently, it is eventually finite at `s` in `f.small_sets`. -/ def finite_at_filter {m0 : measurable_space α} (μ : measure α) (f : filter α) : Prop := ∃ s ∈ f, μ s < ∞ lemma finite_at_filter_of_finite {m0 : measurable_space α} (μ : measure α) [is_finite_measure μ] (f : filter α) : μ.finite_at_filter f := ⟨univ, univ_mem, measure_lt_top μ univ⟩ lemma finite_at_filter.exists_mem_basis {f : filter α} (hμ : finite_at_filter μ f) {p : ι → Prop} {s : ι → set α} (hf : f.has_basis p s) : ∃ i (hi : p i), μ (s i) < ∞ := (hf.exists_iff (λ s t hst ht, (measure_mono hst).trans_lt ht)).1 hμ lemma finite_at_bot {m0 : measurable_space α} (μ : measure α) : μ.finite_at_filter ⊥ := ⟨∅, mem_bot, by simp only [measure_empty, with_top.zero_lt_top]⟩ /-- `μ` has finite spanning sets in `C` if there is a countable sequence of sets in `C` that have finite measures. This structure is a type, which is useful if we want to record extra properties about the sets, such as that they are monotone. `sigma_finite` is defined in terms of this: `μ` is σ-finite if there exists a sequence of finite spanning sets in the collection of all measurable sets. -/ @[protect_proj, nolint has_nonempty_instance] structure finite_spanning_sets_in {m0 : measurable_space α} (μ : measure α) (C : set (set α)) := (set : ℕ → set α) (set_mem : ∀ i, set i ∈ C) (finite : ∀ i, μ (set i) < ∞) (spanning : (⋃ i, set i) = univ) end measure open measure /-- A measure `μ` is called σ-finite if there is a countable collection of sets `{ A i | i ∈ ℕ }` such that `μ (A i) < ∞` and `⋃ i, A i = s`. -/ class sigma_finite {m0 : measurable_space α} (μ : measure α) : Prop := (out' : nonempty (μ.finite_spanning_sets_in univ)) theorem sigma_finite_iff : sigma_finite μ ↔ nonempty (μ.finite_spanning_sets_in univ) := ⟨λ h, h.1, λ h, ⟨h⟩⟩ theorem sigma_finite.out (h : sigma_finite μ) : nonempty (μ.finite_spanning_sets_in univ) := h.1 include m0 /-- If `μ` is σ-finite it has finite spanning sets in the collection of all measurable sets. -/ def measure.to_finite_spanning_sets_in (μ : measure α) [h : sigma_finite μ] : μ.finite_spanning_sets_in {s | measurable_set s} := { set := λ n, to_measurable μ (h.out.some.set n), set_mem := λ n, measurable_set_to_measurable _ _, finite := λ n, by { rw measure_to_measurable, exact h.out.some.finite n }, spanning := eq_univ_of_subset (Union_mono $ λ n, subset_to_measurable _ _) h.out.some.spanning } /-- A noncomputable way to get a monotone collection of sets that span `univ` and have finite measure using `classical.some`. This definition satisfies monotonicity in addition to all other properties in `sigma_finite`. -/ def spanning_sets (μ : measure α) [sigma_finite μ] (i : ℕ) : set α := accumulate μ.to_finite_spanning_sets_in.set i lemma monotone_spanning_sets (μ : measure α) [sigma_finite μ] : monotone (spanning_sets μ) := monotone_accumulate lemma measurable_spanning_sets (μ : measure α) [sigma_finite μ] (i : ℕ) : measurable_set (spanning_sets μ i) := measurable_set.Union $ λ j, measurable_set.Union_Prop $ λ hij, μ.to_finite_spanning_sets_in.set_mem j lemma measure_spanning_sets_lt_top (μ : measure α) [sigma_finite μ] (i : ℕ) : μ (spanning_sets μ i) < ∞ := measure_bUnion_lt_top (finite_le_nat i) $ λ j _, (μ.to_finite_spanning_sets_in.finite j).ne lemma Union_spanning_sets (μ : measure α) [sigma_finite μ] : (⋃ i : ℕ, spanning_sets μ i) = univ := by simp_rw [spanning_sets, Union_accumulate, μ.to_finite_spanning_sets_in.spanning] lemma is_countably_spanning_spanning_sets (μ : measure α) [sigma_finite μ] : is_countably_spanning (range (spanning_sets μ)) := ⟨spanning_sets μ, mem_range_self, Union_spanning_sets μ⟩ /-- `spanning_sets_index μ x` is the least `n : ℕ` such that `x ∈ spanning_sets μ n`. -/ def spanning_sets_index (μ : measure α) [sigma_finite μ] (x : α) : ℕ := nat.find $ Union_eq_univ_iff.1 (Union_spanning_sets μ) x lemma measurable_spanning_sets_index (μ : measure α) [sigma_finite μ] : measurable (spanning_sets_index μ) := measurable_find _ $ measurable_spanning_sets μ lemma preimage_spanning_sets_index_singleton (μ : measure α) [sigma_finite μ] (n : ℕ) : spanning_sets_index μ ⁻¹' {n} = disjointed (spanning_sets μ) n := preimage_find_eq_disjointed _ _ _ lemma spanning_sets_index_eq_iff (μ : measure α) [sigma_finite μ] {x : α} {n : ℕ} : spanning_sets_index μ x = n ↔ x ∈ disjointed (spanning_sets μ) n := by convert set.ext_iff.1 (preimage_spanning_sets_index_singleton μ n) x lemma mem_disjointed_spanning_sets_index (μ : measure α) [sigma_finite μ] (x : α) : x ∈ disjointed (spanning_sets μ) (spanning_sets_index μ x) := (spanning_sets_index_eq_iff μ).1 rfl lemma mem_spanning_sets_index (μ : measure α) [sigma_finite μ] (x : α) : x ∈ spanning_sets μ (spanning_sets_index μ x) := disjointed_subset _ _ (mem_disjointed_spanning_sets_index μ x) lemma mem_spanning_sets_of_index_le (μ : measure α) [sigma_finite μ] (x : α) {n : ℕ} (hn : spanning_sets_index μ x ≤ n) : x ∈ spanning_sets μ n := monotone_spanning_sets μ hn (mem_spanning_sets_index μ x) lemma eventually_mem_spanning_sets (μ : measure α) [sigma_finite μ] (x : α) : ∀ᶠ n in at_top, x ∈ spanning_sets μ n := eventually_at_top.2 ⟨spanning_sets_index μ x, λ b, mem_spanning_sets_of_index_le μ x⟩ omit m0 namespace measure lemma supr_restrict_spanning_sets [sigma_finite μ] (hs : measurable_set s) : (⨆ i, μ.restrict (spanning_sets μ i) s) = μ s := calc (⨆ i, μ.restrict (spanning_sets μ i) s) = μ.restrict (⋃ i, spanning_sets μ i) s : (restrict_Union_apply_eq_supr (directed_of_sup (monotone_spanning_sets μ)) hs).symm ... = μ s : by rw [Union_spanning_sets, restrict_univ] /-- In a σ-finite space, any measurable set of measure `> r` contains a measurable subset of finite measure `> r`. -/ lemma exists_subset_measure_lt_top [sigma_finite μ] {r : ℝ≥0∞} (hs : measurable_set s) (h's : r < μ s) : ∃ t, measurable_set t ∧ t ⊆ s ∧ r < μ t ∧ μ t < ∞ := begin rw [← supr_restrict_spanning_sets hs, @lt_supr_iff _ _ _ r (λ (i : ℕ), μ.restrict (spanning_sets μ i) s)] at h's, rcases h's with ⟨n, hn⟩, simp only [restrict_apply hs] at hn, refine ⟨s ∩ spanning_sets μ n, hs.inter (measurable_spanning_sets _ _), inter_subset_left _ _, hn, _⟩, exact (measure_mono (inter_subset_right _ _)).trans_lt (measure_spanning_sets_lt_top _ _), end /-- The measurable superset `to_measurable μ t` of `t` (which has the same measure as `t`) satisfies, for any measurable set `s`, the equality `μ (to_measurable μ t ∩ s) = μ (t ∩ s)`. This only holds when `μ` is σ-finite. For a version without this assumption (but requiring that `t` has finite measure), see `measure_to_measurable_inter`. -/ lemma measure_to_measurable_inter_of_sigma_finite [sigma_finite μ] {s : set α} (hs : measurable_set s) (t : set α) : μ (to_measurable μ t ∩ s) = μ (t ∩ s) := begin -- we show that there is a measurable superset of `t` satisfying the conclusion for any -- measurable set `s`. It is built on each member of a spanning family using `to_measurable` -- (which is well behaved for finite measure sets thanks to `measure_to_measurable_inter`), and -- the desired property passes to the union. have A : ∃ t' ⊇ t, measurable_set t' ∧ (∀ u, measurable_set u → μ (t' ∩ u) = μ (t ∩ u)), { set t' := ⋃ n, to_measurable μ (t ∩ disjointed (spanning_sets μ) n) with ht', have tt' : t ⊆ t' := calc t ⊆ ⋃ n, t ∩ disjointed (spanning_sets μ) n : by rw [← inter_Union, Union_disjointed, Union_spanning_sets, inter_univ] ... ⊆ ⋃ n, to_measurable μ (t ∩ disjointed (spanning_sets μ) n) : Union_mono (λ n, subset_to_measurable _ _), refine ⟨t', tt', measurable_set.Union (λ n, measurable_set_to_measurable μ _), λ u hu, _⟩, apply le_antisymm _ (measure_mono (inter_subset_inter tt' subset.rfl)), calc μ (t' ∩ u) ≤ ∑' n, μ (to_measurable μ (t ∩ disjointed (spanning_sets μ) n) ∩ u) : by { rw [ht', Union_inter], exact measure_Union_le _ } ... = ∑' n, μ ((t ∩ disjointed (spanning_sets μ) n) ∩ u) : begin congr' 1, ext1 n, apply measure_to_measurable_inter hu, apply ne_of_lt, calc μ (t ∩ disjointed (spanning_sets μ) n) ≤ μ (disjointed (spanning_sets μ) n) : measure_mono (inter_subset_right _ _) ... ≤ μ (spanning_sets μ n) : measure_mono (disjointed_le (spanning_sets μ) n) ... < ∞ : measure_spanning_sets_lt_top _ _ end ... = ∑' n, μ.restrict (t ∩ u) (disjointed (spanning_sets μ) n) : begin congr' 1, ext1 n, rw [restrict_apply, inter_comm t _, inter_assoc], exact measurable_set.disjointed (measurable_spanning_sets _) _ end ... = μ.restrict (t ∩ u) (⋃ n, disjointed (spanning_sets μ) n) : begin rw measure_Union, { exact disjoint_disjointed _ }, { intro i, exact measurable_set.disjointed (measurable_spanning_sets _) _ } end ... = μ (t ∩ u) : by rw [Union_disjointed, Union_spanning_sets, restrict_apply measurable_set.univ, univ_inter] }, -- thanks to the definition of `to_measurable`, the previous property will also be shared -- by `to_measurable μ t`, which is enough to conclude the proof. rw [to_measurable], split_ifs with ht, { apply measure_congr, exact ae_eq_set_inter ht.some_spec.snd.2 (ae_eq_refl _) }, { exact A.some_spec.snd.2 s hs }, end @[simp] lemma restrict_to_measurable_of_sigma_finite [sigma_finite μ] (s : set α) : μ.restrict (to_measurable μ s) = μ.restrict s := ext $ λ t ht, by simp only [restrict_apply ht, inter_comm t, measure_to_measurable_inter_of_sigma_finite ht] namespace finite_spanning_sets_in variables {C D : set (set α)} /-- If `μ` has finite spanning sets in `C` and `C ∩ {s | μ s < ∞} ⊆ D` then `μ` has finite spanning sets in `D`. -/ protected def mono' (h : μ.finite_spanning_sets_in C) (hC : C ∩ {s | μ s < ∞} ⊆ D) : μ.finite_spanning_sets_in D := ⟨h.set, λ i, hC ⟨h.set_mem i, h.finite i⟩, h.finite, h.spanning⟩ /-- If `μ` has finite spanning sets in `C` and `C ⊆ D` then `μ` has finite spanning sets in `D`. -/ protected def mono (h : μ.finite_spanning_sets_in C) (hC : C ⊆ D) : μ.finite_spanning_sets_in D := h.mono' (λ s hs, hC hs.1) /-- If `μ` has finite spanning sets in the collection of measurable sets `C`, then `μ` is σ-finite. -/ protected lemma sigma_finite (h : μ.finite_spanning_sets_in C) : sigma_finite μ := ⟨⟨h.mono $ subset_univ C⟩⟩ /-- An extensionality for measures. It is `ext_of_generate_from_of_Union` formulated in terms of `finite_spanning_sets_in`. -/ protected lemma ext {ν : measure α} {C : set (set α)} (hA : ‹_› = generate_from C) (hC : is_pi_system C) (h : μ.finite_spanning_sets_in C) (h_eq : ∀ s ∈ C, μ s = ν s) : μ = ν := ext_of_generate_from_of_Union C _ hA hC h.spanning h.set_mem (λ i, (h.finite i).ne) h_eq protected lemma is_countably_spanning (h : μ.finite_spanning_sets_in C) : is_countably_spanning C := ⟨h.set, h.set_mem, h.spanning⟩ end finite_spanning_sets_in lemma sigma_finite_of_countable {S : set (set α)} (hc : S.countable) (hμ : ∀ s ∈ S, μ s < ∞) (hU : ⋃₀ S = univ) : sigma_finite μ := begin obtain ⟨s, hμ, hs⟩ : ∃ s : ℕ → set α, (∀ n, μ (s n) < ∞) ∧ (⋃ n, s n) = univ, from (@exists_seq_cover_iff_countable _ (λ x, μ x < ⊤) ⟨∅, by simp⟩).2 ⟨S, hc, hμ, hU⟩, exact ⟨⟨⟨λ n, s n, λ n, trivial, hμ, hs⟩⟩⟩, end /-- Given measures `μ`, `ν` where `ν ≤ μ`, `finite_spanning_sets_in.of_le` provides the induced `finite_spanning_set` with respect to `ν` from a `finite_spanning_set` with respect to `μ`. -/ def finite_spanning_sets_in.of_le (h : ν ≤ μ) {C : set (set α)} (S : μ.finite_spanning_sets_in C) : ν.finite_spanning_sets_in C := { set := S.set, set_mem := S.set_mem, finite := λ n, lt_of_le_of_lt (le_iff'.1 h _) (S.finite n), spanning := S.spanning } lemma sigma_finite_of_le (μ : measure α) [hs : sigma_finite μ] (h : ν ≤ μ) : sigma_finite ν := ⟨hs.out.map $ finite_spanning_sets_in.of_le h⟩ end measure include m0 /-- Every finite measure is σ-finite. -/ @[priority 100] instance is_finite_measure.to_sigma_finite (μ : measure α) [is_finite_measure μ] : sigma_finite μ := ⟨⟨⟨λ _, univ, λ _, trivial, λ _, measure_lt_top μ _, Union_const _⟩⟩⟩ instance restrict.sigma_finite (μ : measure α) [sigma_finite μ] (s : set α) : sigma_finite (μ.restrict s) := begin refine ⟨⟨⟨spanning_sets μ, λ _, trivial, λ i, _, Union_spanning_sets μ⟩⟩⟩, rw [restrict_apply (measurable_spanning_sets μ i)], exact (measure_mono $ inter_subset_left _ _).trans_lt (measure_spanning_sets_lt_top μ i) end instance sum.sigma_finite {ι} [fintype ι] (μ : ι → measure α) [∀ i, sigma_finite (μ i)] : sigma_finite (sum μ) := begin haveI : encodable ι := fintype.to_encodable ι, have : ∀ n, measurable_set (⋂ (i : ι), spanning_sets (μ i) n) := λ n, measurable_set.Inter (λ i, measurable_spanning_sets (μ i) n), refine ⟨⟨⟨λ n, ⋂ i, spanning_sets (μ i) n, λ _, trivial, λ n, _, _⟩⟩⟩, { rw [sum_apply _ (this n), tsum_fintype, ennreal.sum_lt_top_iff], rintro i -, exact (measure_mono $ Inter_subset _ i).trans_lt (measure_spanning_sets_lt_top (μ i) n) }, { rw [Union_Inter_of_monotone], simp_rw [Union_spanning_sets, Inter_univ], exact λ i, monotone_spanning_sets (μ i), } end instance add.sigma_finite (μ ν : measure α) [sigma_finite μ] [sigma_finite ν] : sigma_finite (μ + ν) := by { rw [← sum_cond], refine @sum.sigma_finite _ _ _ _ _ (bool.rec _ _); simpa } lemma sigma_finite.of_map (μ : measure α) {f : α → β} (hf : ae_measurable f μ) (h : sigma_finite (μ.map f)) : sigma_finite μ := ⟨⟨⟨λ n, f ⁻¹' (spanning_sets (μ.map f) n), λ n, trivial, λ n, by simp only [← map_apply_of_ae_measurable hf, measurable_spanning_sets, measure_spanning_sets_lt_top], by rw [← preimage_Union, Union_spanning_sets, preimage_univ]⟩⟩⟩ lemma _root_.measurable_equiv.sigma_finite_map {μ : measure α} (f : α ≃ᵐ β) (h : sigma_finite μ) : sigma_finite (μ.map f) := by { refine sigma_finite.of_map _ f.symm.measurable.ae_measurable _, rwa [map_map f.symm.measurable f.measurable, f.symm_comp_self, measure.map_id] } /-- Similar to `ae_of_forall_measure_lt_top_ae_restrict`, but where you additionally get the hypothesis that another σ-finite measure has finite values on `s`. -/ lemma ae_of_forall_measure_lt_top_ae_restrict' {μ : measure α} (ν : measure α) [sigma_finite μ] [sigma_finite ν] (P : α → Prop) (h : ∀ s, measurable_set s → μ s < ∞ → ν s < ∞ → ∀ᵐ x ∂(μ.restrict s), P x) : ∀ᵐ x ∂μ, P x := begin have : ∀ n, ∀ᵐ x ∂μ, x ∈ spanning_sets (μ + ν) n → P x, { intro n, have := h (spanning_sets (μ + ν) n) (measurable_spanning_sets _ _) _ _, exacts [(ae_restrict_iff' (measurable_spanning_sets _ _)).mp this, (self_le_add_right _ _).trans_lt (measure_spanning_sets_lt_top (μ + ν) _), (self_le_add_left _ _).trans_lt (measure_spanning_sets_lt_top (μ + ν) _)] }, filter_upwards [ae_all_iff.2 this] with _ hx using hx _ (mem_spanning_sets_index _ _) end /-- To prove something for almost all `x` w.r.t. a σ-finite measure, it is sufficient to show that this holds almost everywhere in sets where the measure has finite value. -/ lemma ae_of_forall_measure_lt_top_ae_restrict {μ : measure α} [sigma_finite μ] (P : α → Prop) (h : ∀ s, measurable_set s → μ s < ∞ → ∀ᵐ x ∂(μ.restrict s), P x) : ∀ᵐ x ∂μ, P x := ae_of_forall_measure_lt_top_ae_restrict' μ P $ λ s hs h2s _, h s hs h2s /-- A measure is called locally finite if it is finite in some neighborhood of each point. -/ class is_locally_finite_measure [topological_space α] (μ : measure α) : Prop := (finite_at_nhds : ∀ x, μ.finite_at_filter (𝓝 x)) @[priority 100] -- see Note [lower instance priority] instance is_finite_measure.to_is_locally_finite_measure [topological_space α] (μ : measure α) [is_finite_measure μ] : is_locally_finite_measure μ := ⟨λ x, finite_at_filter_of_finite _ _⟩ lemma measure.finite_at_nhds [topological_space α] (μ : measure α) [is_locally_finite_measure μ] (x : α) : μ.finite_at_filter (𝓝 x) := is_locally_finite_measure.finite_at_nhds x lemma measure.smul_finite (μ : measure α) [is_finite_measure μ] {c : ℝ≥0∞} (hc : c ≠ ∞) : is_finite_measure (c • μ) := begin lift c to ℝ≥0 using hc, exact measure_theory.is_finite_measure_smul_nnreal, end lemma measure.exists_is_open_measure_lt_top [topological_space α] (μ : measure α) [is_locally_finite_measure μ] (x : α) : ∃ s : set α, x ∈ s ∧ is_open s ∧ μ s < ∞ := by simpa only [exists_prop, and.assoc] using (μ.finite_at_nhds x).exists_mem_basis (nhds_basis_opens x) instance is_locally_finite_measure_smul_nnreal [topological_space α] (μ : measure α) [is_locally_finite_measure μ] (c : ℝ≥0) : is_locally_finite_measure (c • μ) := begin refine ⟨λ x, _⟩, rcases μ.exists_is_open_measure_lt_top x with ⟨o, xo, o_open, μo⟩, refine ⟨o, o_open.mem_nhds xo, _⟩, apply ennreal.mul_lt_top _ μo.ne, simp only [ring_hom.to_monoid_hom_eq_coe, ring_hom.coe_monoid_hom, ennreal.coe_ne_top, ennreal.coe_of_nnreal_hom, ne.def, not_false_iff], end /-- A measure `μ` is finite on compacts if any compact set `K` satisfies `μ K < ∞`. -/ @[protect_proj] class is_finite_measure_on_compacts [topological_space α] (μ : measure α) : Prop := (lt_top_of_is_compact : ∀ ⦃K : set α⦄, is_compact K → μ K < ∞) /-- A compact subset has finite measure for a measure which is finite on compacts. -/ lemma _root_.is_compact.measure_lt_top [topological_space α] {μ : measure α} [is_finite_measure_on_compacts μ] ⦃K : set α⦄ (hK : is_compact K) : μ K < ∞ := is_finite_measure_on_compacts.lt_top_of_is_compact hK /-- A bounded subset has finite measure for a measure which is finite on compact sets, in a proper space. -/ lemma _root_.metric.bounded.measure_lt_top [pseudo_metric_space α] [proper_space α] {μ : measure α} [is_finite_measure_on_compacts μ] ⦃s : set α⦄ (hs : metric.bounded s) : μ s < ∞ := calc μ s ≤ μ (closure s) : measure_mono subset_closure ... < ∞ : (metric.is_compact_of_is_closed_bounded is_closed_closure hs.closure).measure_lt_top lemma measure_closed_ball_lt_top [pseudo_metric_space α] [proper_space α] {μ : measure α} [is_finite_measure_on_compacts μ] {x : α} {r : ℝ} : μ (metric.closed_ball x r) < ∞ := metric.bounded_closed_ball.measure_lt_top lemma measure_ball_lt_top [pseudo_metric_space α] [proper_space α] {μ : measure α} [is_finite_measure_on_compacts μ] {x : α} {r : ℝ} : μ (metric.ball x r) < ∞ := metric.bounded_ball.measure_lt_top protected lemma is_finite_measure_on_compacts.smul [topological_space α] (μ : measure α) [is_finite_measure_on_compacts μ] {c : ℝ≥0∞} (hc : c ≠ ∞) : is_finite_measure_on_compacts (c • μ) := ⟨λ K hK, ennreal.mul_lt_top hc (hK.measure_lt_top).ne⟩ /-- Note this cannot be an instance because it would form a typeclass loop with `is_finite_measure_on_compacts_of_is_locally_finite_measure`. -/ lemma compact_space.is_finite_measure [topological_space α] [compact_space α] [is_finite_measure_on_compacts μ] : is_finite_measure μ := ⟨is_finite_measure_on_compacts.lt_top_of_is_compact compact_univ⟩ omit m0 @[priority 100] -- see Note [lower instance priority] instance sigma_finite_of_locally_finite [topological_space α] [second_countable_topology α] [is_locally_finite_measure μ] : sigma_finite μ := begin choose s hsx hsμ using μ.finite_at_nhds, rcases topological_space.countable_cover_nhds hsx with ⟨t, htc, htU⟩, refine measure.sigma_finite_of_countable (htc.image s) (ball_image_iff.2 $ λ x hx, hsμ x) _, rwa sUnion_image end /-- A measure which is finite on compact sets in a locally compact space is locally finite. Not registered as an instance to avoid a loop with the other direction. -/ lemma is_locally_finite_measure_of_is_finite_measure_on_compacts [topological_space α] [locally_compact_space α] [is_finite_measure_on_compacts μ] : is_locally_finite_measure μ := ⟨begin intro x, rcases exists_compact_mem_nhds x with ⟨K, K_compact, K_mem⟩, exact ⟨K, K_mem, K_compact.measure_lt_top⟩, end⟩ lemma exists_pos_measure_of_cover [encodable ι] {U : ι → set α} (hU : (⋃ i, U i) = univ) (hμ : μ ≠ 0) : ∃ i, 0 < μ (U i) := begin contrapose! hμ with H, rw [← measure_univ_eq_zero, ← hU], exact measure_Union_null (λ i, nonpos_iff_eq_zero.1 (H i)) end lemma exists_pos_preimage_ball [pseudo_metric_space δ] (f : α → δ) (x : δ) (hμ : μ ≠ 0) : ∃ n : ℕ, 0 < μ (f ⁻¹' metric.ball x n) := exists_pos_measure_of_cover (by rw [← preimage_Union, metric.Union_ball_nat, preimage_univ]) hμ lemma exists_pos_ball [pseudo_metric_space α] (x : α) (hμ : μ ≠ 0) : ∃ n : ℕ, 0 < μ (metric.ball x n) := exists_pos_preimage_ball id x hμ /-- If a set has zero measure in a neighborhood of each of its points, then it has zero measure in a second-countable space. -/ lemma null_of_locally_null [topological_space α] [second_countable_topology α] (s : set α) (hs : ∀ x ∈ s, ∃ u ∈ 𝓝[s] x, μ u = 0) : μ s = 0 := μ.to_outer_measure.null_of_locally_null s hs lemma exists_mem_forall_mem_nhds_within_pos_measure [topological_space α] [second_countable_topology α] {s : set α} (hs : μ s ≠ 0) : ∃ x ∈ s, ∀ t ∈ 𝓝[s] x, 0 < μ t := μ.to_outer_measure.exists_mem_forall_mem_nhds_within_pos hs lemma exists_ne_forall_mem_nhds_pos_measure_preimage {β} [topological_space β] [t1_space β] [second_countable_topology β] [nonempty β] {f : α → β} (h : ∀ b, ∃ᵐ x ∂μ, f x ≠ b) : ∃ a b : β, a ≠ b ∧ (∀ s ∈ 𝓝 a, 0 < μ (f ⁻¹' s)) ∧ (∀ t ∈ 𝓝 b, 0 < μ (f ⁻¹' t)) := begin -- We use an `outer_measure` so that the proof works without `measurable f` set m : outer_measure β := outer_measure.map f μ.to_outer_measure, replace h : ∀ b : β, m {b}ᶜ ≠ 0 := λ b, not_eventually.mpr (h b), inhabit β, have : m univ ≠ 0, from ne_bot_of_le_ne_bot (h default) (m.mono' $ subset_univ _), rcases m.exists_mem_forall_mem_nhds_within_pos this with ⟨b, -, hb⟩, simp only [nhds_within_univ] at hb, rcases m.exists_mem_forall_mem_nhds_within_pos (h b) with ⟨a, hab : a ≠ b, ha⟩, simp only [is_open_compl_singleton.nhds_within_eq hab] at ha, exact ⟨a, b, hab, ha, hb⟩ end /-- If two finite measures give the same mass to the whole space and coincide on a π-system made of measurable sets, then they coincide on all sets in the σ-algebra generated by the π-system. -/ lemma ext_on_measurable_space_of_generate_finite {α} (m₀ : measurable_space α) {μ ν : measure α} [is_finite_measure μ] (C : set (set α)) (hμν : ∀ s ∈ C, μ s = ν s) {m : measurable_space α} (h : m ≤ m₀) (hA : m = measurable_space.generate_from C) (hC : is_pi_system C) (h_univ : μ set.univ = ν set.univ) {s : set α} (hs : measurable_set[m] s) : μ s = ν s := begin haveI : is_finite_measure ν := begin constructor, rw ← h_univ, apply is_finite_measure.measure_univ_lt_top, end, refine induction_on_inter hA hC (by simp) hμν _ _ hs, { intros t h1t h2t, have h1t_ : @measurable_set α m₀ t, from h _ h1t, rw [@measure_compl α m₀ μ t h1t_ (@measure_ne_top α m₀ μ _ t), @measure_compl α m₀ ν t h1t_ (@measure_ne_top α m₀ ν _ t), h_univ, h2t], }, { intros f h1f h2f h3f, have h2f_ : ∀ (i : ℕ), @measurable_set α m₀ (f i), from (λ i, h _ (h2f i)), have h_Union : @measurable_set α m₀ (⋃ (i : ℕ), f i),from @measurable_set.Union α ℕ m₀ _ f h2f_, simp [measure_Union, h_Union, h1f, h3f, h2f_], }, end /-- Two finite measures are equal if they are equal on the π-system generating the σ-algebra (and `univ`). -/ lemma ext_of_generate_finite (C : set (set α)) (hA : m0 = generate_from C) (hC : is_pi_system C) [is_finite_measure μ] (hμν : ∀ s ∈ C, μ s = ν s) (h_univ : μ univ = ν univ) : μ = ν := measure.ext (λ s hs, ext_on_measurable_space_of_generate_finite m0 C hμν le_rfl hA hC h_univ hs) namespace measure section disjointed include m0 /-- Given `S : μ.finite_spanning_sets_in {s | measurable_set s}`, `finite_spanning_sets_in.disjointed` provides a `finite_spanning_sets_in {s | measurable_set s}` such that its underlying sets are pairwise disjoint. -/ protected def finite_spanning_sets_in.disjointed {μ : measure α} (S : μ.finite_spanning_sets_in {s | measurable_set s}) : μ.finite_spanning_sets_in {s | measurable_set s} := ⟨disjointed S.set, measurable_set.disjointed S.set_mem, λ n, lt_of_le_of_lt (measure_mono (disjointed_subset S.set n)) (S.finite _), S.spanning ▸ Union_disjointed⟩ lemma finite_spanning_sets_in.disjointed_set_eq {μ : measure α} (S : μ.finite_spanning_sets_in {s | measurable_set s}) : S.disjointed.set = disjointed S.set := rfl lemma exists_eq_disjoint_finite_spanning_sets_in (μ ν : measure α) [sigma_finite μ] [sigma_finite ν] : ∃ (S : μ.finite_spanning_sets_in {s | measurable_set s}) (T : ν.finite_spanning_sets_in {s | measurable_set s}), S.set = T.set ∧ pairwise (disjoint on S.set) := let S := (μ + ν).to_finite_spanning_sets_in.disjointed in ⟨S.of_le (measure.le_add_right le_rfl), S.of_le (measure.le_add_left le_rfl), rfl, disjoint_disjointed _⟩ end disjointed namespace finite_at_filter variables {f g : filter α} lemma filter_mono (h : f ≤ g) : μ.finite_at_filter g → μ.finite_at_filter f := λ ⟨s, hs, hμ⟩, ⟨s, h hs, hμ⟩ lemma inf_of_left (h : μ.finite_at_filter f) : μ.finite_at_filter (f ⊓ g) := h.filter_mono inf_le_left lemma inf_of_right (h : μ.finite_at_filter g) : μ.finite_at_filter (f ⊓ g) := h.filter_mono inf_le_right @[simp] lemma inf_ae_iff : μ.finite_at_filter (f ⊓ μ.ae) ↔ μ.finite_at_filter f := begin refine ⟨_, λ h, h.filter_mono inf_le_left⟩, rintros ⟨s, ⟨t, ht, u, hu, rfl⟩, hμ⟩, suffices : μ t ≤ μ (t ∩ u), from ⟨t, ht, this.trans_lt hμ⟩, exact measure_mono_ae (mem_of_superset hu (λ x hu ht, ⟨ht, hu⟩)) end alias inf_ae_iff ↔ of_inf_ae _ lemma filter_mono_ae (h : f ⊓ μ.ae ≤ g) (hg : μ.finite_at_filter g) : μ.finite_at_filter f := inf_ae_iff.1 (hg.filter_mono h) protected lemma measure_mono (h : μ ≤ ν) : ν.finite_at_filter f → μ.finite_at_filter f := λ ⟨s, hs, hν⟩, ⟨s, hs, (measure.le_iff'.1 h s).trans_lt hν⟩ @[mono] protected lemma mono (hf : f ≤ g) (hμ : μ ≤ ν) : ν.finite_at_filter g → μ.finite_at_filter f := λ h, (h.filter_mono hf).measure_mono hμ protected lemma eventually (h : μ.finite_at_filter f) : ∀ᶠ s in f.small_sets, μ s < ∞ := (eventually_small_sets' $ λ s t hst ht, (measure_mono hst).trans_lt ht).2 h lemma filter_sup : μ.finite_at_filter f → μ.finite_at_filter g → μ.finite_at_filter (f ⊔ g) := λ ⟨s, hsf, hsμ⟩ ⟨t, htg, htμ⟩, ⟨s ∪ t, union_mem_sup hsf htg, (measure_union_le s t).trans_lt (ennreal.add_lt_top.2 ⟨hsμ, htμ⟩)⟩ end finite_at_filter lemma finite_at_nhds_within [topological_space α] {m0 : measurable_space α} (μ : measure α) [is_locally_finite_measure μ] (x : α) (s : set α) : μ.finite_at_filter (𝓝[s] x) := (finite_at_nhds μ x).inf_of_left @[simp] lemma finite_at_principal : μ.finite_at_filter (𝓟 s) ↔ μ s < ∞ := ⟨λ ⟨t, ht, hμ⟩, (measure_mono ht).trans_lt hμ, λ h, ⟨s, mem_principal_self s, h⟩⟩ lemma is_locally_finite_measure_of_le [topological_space α] {m : measurable_space α} {μ ν : measure α} [H : is_locally_finite_measure μ] (h : ν ≤ μ) : is_locally_finite_measure ν := let F := H.finite_at_nhds in ⟨λ x, (F x).measure_mono h⟩ end measure end measure_theory open measure_theory measure_theory.measure namespace measurable_embedding variables {m0 : measurable_space α} {m1 : measurable_space β} {f : α → β} (hf : measurable_embedding f) include hf theorem map_apply (μ : measure α) (s : set β) : μ.map f s = μ (f ⁻¹' s) := begin refine le_antisymm _ (le_map_apply hf.measurable.ae_measurable s), set t := f '' (to_measurable μ (f ⁻¹' s)) ∪ (range f)ᶜ, have htm : measurable_set t, from (hf.measurable_set_image.2 $ measurable_set_to_measurable _ _).union hf.measurable_set_range.compl, have hst : s ⊆ t, { rw [subset_union_compl_iff_inter_subset, ← image_preimage_eq_inter_range], exact image_subset _ (subset_to_measurable _ _) }, have hft : f ⁻¹' t = to_measurable μ (f ⁻¹' s), by rw [preimage_union, preimage_compl, preimage_range, compl_univ, union_empty, hf.injective.preimage_image], calc μ.map f s ≤ μ.map f t : measure_mono hst ... = μ (f ⁻¹' s) : by rw [map_apply hf.measurable htm, hft, measure_to_measurable] end lemma map_comap (μ : measure β) : (comap f μ).map f = μ.restrict (range f) := begin ext1 t ht, rw [hf.map_apply, comap_apply f hf.injective hf.measurable_set_image' _ (hf.measurable ht), image_preimage_eq_inter_range, restrict_apply ht] end lemma comap_apply (μ : measure β) (s : set α) : comap f μ s = μ (f '' s) := calc comap f μ s = comap f μ (f ⁻¹' (f '' s)) : by rw hf.injective.preimage_image ... = (comap f μ).map f (f '' s) : (hf.map_apply _ _).symm ... = μ (f '' s) : by rw [hf.map_comap, restrict_apply' hf.measurable_set_range, inter_eq_self_of_subset_left (image_subset_range _ _)] lemma ae_map_iff {p : β → Prop} {μ : measure α} : (∀ᵐ x ∂(μ.map f), p x) ↔ ∀ᵐ x ∂μ, p (f x) := by simp only [ae_iff, hf.map_apply, preimage_set_of_eq] lemma restrict_map (μ : measure α) (s : set β) : (μ.map f).restrict s = (μ.restrict $ f ⁻¹' s).map f := measure.ext $ λ t ht, by simp [hf.map_apply, ht, hf.measurable ht] end measurable_embedding section subtype lemma comap_subtype_coe_apply {m0 : measurable_space α} {s : set α} (hs : measurable_set s) (μ : measure α) (t : set s) : comap coe μ t = μ (coe '' t) := (measurable_embedding.subtype_coe hs).comap_apply _ _ lemma map_comap_subtype_coe {m0 : measurable_space α} {s : set α} (hs : measurable_set s) (μ : measure α) : (comap coe μ).map (coe : s → α) = μ.restrict s := by rw [(measurable_embedding.subtype_coe hs).map_comap, subtype.range_coe] lemma ae_restrict_iff_subtype {m0 : measurable_space α} {μ : measure α} {s : set α} (hs : measurable_set s) {p : α → Prop} : (∀ᵐ x ∂(μ.restrict s), p x) ↔ ∀ᵐ x ∂(comap (coe : s → α) μ), p ↑x := by rw [← map_comap_subtype_coe hs, (measurable_embedding.subtype_coe hs).ae_map_iff] variables [measure_space α] /-! ### Volume on `s : set α` -/ instance _root_.set_coe.measure_space (s : set α) : measure_space s := ⟨comap (coe : s → α) volume⟩ lemma volume_set_coe_def (s : set α) : (volume : measure s) = comap (coe : s → α) volume := rfl lemma measurable_set.map_coe_volume {s : set α} (hs : measurable_set s) : volume.map (coe : s → α)= restrict volume s := by rw [volume_set_coe_def, (measurable_embedding.subtype_coe hs).map_comap volume, subtype.range_coe] lemma volume_image_subtype_coe {s : set α} (hs : measurable_set s) (t : set s) : volume (coe '' t : set α) = volume t := (comap_subtype_coe_apply hs volume t).symm end subtype namespace measurable_equiv /-! Interactions of measurable equivalences and measures -/ open equiv measure_theory.measure variables [measurable_space α] [measurable_space β] {μ : measure α} {ν : measure β} /-- If we map a measure along a measurable equivalence, we can compute the measure on all sets (not just the measurable ones). -/ protected theorem map_apply (f : α ≃ᵐ β) (s : set β) : μ.map f s = μ (f ⁻¹' s) := f.measurable_embedding.map_apply _ _ @[simp] lemma map_symm_map (e : α ≃ᵐ β) : (μ.map e).map e.symm = μ := by simp [map_map e.symm.measurable e.measurable] @[simp] lemma map_map_symm (e : α ≃ᵐ β) : (ν.map e.symm).map e = ν := by simp [map_map e.measurable e.symm.measurable] lemma map_measurable_equiv_injective (e : α ≃ᵐ β) : injective (map e) := by { intros μ₁ μ₂ hμ, apply_fun map e.symm at hμ, simpa [map_symm_map e] using hμ } lemma map_apply_eq_iff_map_symm_apply_eq (e : α ≃ᵐ β) : μ.map e = ν ↔ ν.map e.symm = μ := by rw [← (map_measurable_equiv_injective e).eq_iff, map_map_symm, eq_comm] lemma restrict_map (e : α ≃ᵐ β) (s : set β) : (μ.map e).restrict s = (μ.restrict $ e ⁻¹' s).map e := e.measurable_embedding.restrict_map _ _ lemma map_ae (f : α ≃ᵐ β) (μ : measure α) : filter.map f μ.ae = (map f μ).ae := by { ext s, simp_rw [mem_map, mem_ae_iff, ← preimage_compl, f.map_apply] } end measurable_equiv namespace measure_theory lemma outer_measure.to_measure_zero [measurable_space α] : (0 : outer_measure α).to_measure ((le_top).trans outer_measure.zero_caratheodory.symm.le) = 0 := by rw [← measure.measure_univ_eq_zero, to_measure_apply _ _ measurable_set.univ, outer_measure.coe_zero, pi.zero_apply] section trim /-- Restriction of a measure to a sub-sigma algebra. It is common to see a measure `μ` on a measurable space structure `m0` as being also a measure on any `m ≤ m0`. Since measures in mathlib have to be trimmed to the measurable space, `μ` itself cannot be a measure on `m`, hence the definition of `μ.trim hm`. This notion is related to `outer_measure.trim`, see the lemma `to_outer_measure_trim_eq_trim_to_outer_measure`. -/ def measure.trim {m m0 : measurable_space α} (μ : @measure α m0) (hm : m ≤ m0) : @measure α m := @outer_measure.to_measure α m μ.to_outer_measure (hm.trans (le_to_outer_measure_caratheodory μ)) @[simp] lemma trim_eq_self [measurable_space α] {μ : measure α} : μ.trim le_rfl = μ := by simp [measure.trim] variables {m m0 : measurable_space α} {μ : measure α} {s : set α} lemma to_outer_measure_trim_eq_trim_to_outer_measure (μ : measure α) (hm : m ≤ m0) : @measure.to_outer_measure _ m (μ.trim hm) = @outer_measure.trim _ m μ.to_outer_measure := by rw [measure.trim, to_measure_to_outer_measure] @[simp] lemma zero_trim (hm : m ≤ m0) : (0 : measure α).trim hm = (0 : @measure α m) := by simp [measure.trim, outer_measure.to_measure_zero] lemma trim_measurable_set_eq (hm : m ≤ m0) (hs : @measurable_set α m s) : μ.trim hm s = μ s := by simp [measure.trim, hs] lemma le_trim (hm : m ≤ m0) : μ s ≤ μ.trim hm s := by { simp_rw [measure.trim], exact (@le_to_measure_apply _ m _ _ _), } lemma measure_eq_zero_of_trim_eq_zero (hm : m ≤ m0) (h : μ.trim hm s = 0) : μ s = 0 := le_antisymm ((le_trim hm).trans (le_of_eq h)) (zero_le _) lemma measure_trim_to_measurable_eq_zero {hm : m ≤ m0} (hs : μ.trim hm s = 0) : μ (@to_measurable α m (μ.trim hm) s) = 0 := measure_eq_zero_of_trim_eq_zero hm (by rwa measure_to_measurable) lemma ae_of_ae_trim (hm : m ≤ m0) {μ : measure α} {P : α → Prop} (h : ∀ᵐ x ∂(μ.trim hm), P x) : ∀ᵐ x ∂μ, P x := measure_eq_zero_of_trim_eq_zero hm h lemma ae_eq_of_ae_eq_trim {E} {hm : m ≤ m0} {f₁ f₂ : α → E} (h12 : f₁ =ᶠ[@measure.ae α m (μ.trim hm)] f₂) : f₁ =ᵐ[μ] f₂ := measure_eq_zero_of_trim_eq_zero hm h12 lemma ae_le_of_ae_le_trim {E} [has_le E] {hm : m ≤ m0} {f₁ f₂ : α → E} (h12 : f₁ ≤ᶠ[@measure.ae α m (μ.trim hm)] f₂) : f₁ ≤ᵐ[μ] f₂ := measure_eq_zero_of_trim_eq_zero hm h12 lemma trim_trim {m₁ m₂ : measurable_space α} {hm₁₂ : m₁ ≤ m₂} {hm₂ : m₂ ≤ m0} : (μ.trim hm₂).trim hm₁₂ = μ.trim (hm₁₂.trans hm₂) := begin ext1 t ht, rw [trim_measurable_set_eq hm₁₂ ht, trim_measurable_set_eq (hm₁₂.trans hm₂) ht, trim_measurable_set_eq hm₂ (hm₁₂ t ht)], end lemma restrict_trim (hm : m ≤ m0) (μ : measure α) (hs : @measurable_set α m s) : @measure.restrict α m (μ.trim hm) s = (μ.restrict s).trim hm := begin ext1 t ht, rw [@measure.restrict_apply α m _ _ _ ht, trim_measurable_set_eq hm ht, measure.restrict_apply (hm t ht), trim_measurable_set_eq hm (@measurable_set.inter α m t s ht hs)], end instance is_finite_measure_trim (hm : m ≤ m0) [is_finite_measure μ] : is_finite_measure (μ.trim hm) := { measure_univ_lt_top := by { rw trim_measurable_set_eq hm (@measurable_set.univ _ m), exact measure_lt_top _ _, } } lemma sigma_finite_trim_mono {m m₂ m0 : measurable_space α} {μ : measure α} (hm : m ≤ m0) (hm₂ : m₂ ≤ m) [sigma_finite (μ.trim (hm₂.trans hm))] : sigma_finite (μ.trim hm) := begin have h := measure.finite_spanning_sets_in (μ.trim (hm₂.trans hm)) set.univ, refine measure.finite_spanning_sets_in.sigma_finite _, { use set.univ, }, { refine { set := spanning_sets (μ.trim (hm₂.trans hm)), set_mem := λ _, set.mem_univ _, finite := λ i, _, -- This is the only one left to prove spanning := Union_spanning_sets _, }, calc (μ.trim hm) (spanning_sets (μ.trim (hm₂.trans hm)) i) = ((μ.trim hm).trim hm₂) (spanning_sets (μ.trim (hm₂.trans hm)) i) : by rw @trim_measurable_set_eq α m₂ m (μ.trim hm) _ hm₂ (measurable_spanning_sets _ _) ... = (μ.trim (hm₂.trans hm)) (spanning_sets (μ.trim (hm₂.trans hm)) i) : by rw @trim_trim _ _ μ _ _ hm₂ hm ... < ∞ : measure_spanning_sets_lt_top _ _, }, end end trim end measure_theory namespace is_compact variables [topological_space α] [measurable_space α] {μ : measure α} {s : set α} /-- If `s` is a compact set and `μ` is finite at `𝓝 x` for every `x ∈ s`, then `s` admits an open superset of finite measure. -/ lemma exists_open_superset_measure_lt_top' (h : is_compact s) (hμ : ∀ x ∈ s, μ.finite_at_filter (𝓝 x)) : ∃ U ⊇ s, is_open U ∧ μ U < ∞ := begin refine is_compact.induction_on h _ _ _ _, { use ∅, simp [superset] }, { rintro s t hst ⟨U, htU, hUo, hU⟩, exact ⟨U, hst.trans htU, hUo, hU⟩ }, { rintro s t ⟨U, hsU, hUo, hU⟩ ⟨V, htV, hVo, hV⟩, refine ⟨U ∪ V, union_subset_union hsU htV, hUo.union hVo, (measure_union_le _ _).trans_lt $ ennreal.add_lt_top.2 ⟨hU, hV⟩⟩ }, { intros x hx, rcases (hμ x hx).exists_mem_basis (nhds_basis_opens _) with ⟨U, ⟨hx, hUo⟩, hU⟩, exact ⟨U, nhds_within_le_nhds (hUo.mem_nhds hx), U, subset.rfl, hUo, hU⟩ } end /-- If `s` is a compact set and `μ` is a locally finite measure, then `s` admits an open superset of finite measure. -/ lemma exists_open_superset_measure_lt_top (h : is_compact s) (μ : measure α) [is_locally_finite_measure μ] : ∃ U ⊇ s, is_open U ∧ μ U < ∞ := h.exists_open_superset_measure_lt_top' $ λ x hx, μ.finite_at_nhds x lemma measure_lt_top_of_nhds_within (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 measure_zero_of_nhds_within (hs : is_compact s) : (∀ a ∈ s, ∃ t ∈ 𝓝[s] a, μ t = 0) → μ s = 0 := by simpa only [← compl_mem_ae_iff] using hs.compl_mem_sets_of_nhds_within end is_compact @[priority 100] -- see Note [lower instance priority] instance is_finite_measure_on_compacts_of_is_locally_finite_measure [topological_space α] {m : measurable_space α} {μ : measure α} [is_locally_finite_measure μ] : is_finite_measure_on_compacts μ := ⟨λ s hs, hs.measure_lt_top_of_nhds_within $ λ x hx, μ.finite_at_nhds_within _ _⟩ lemma is_finite_measure_iff_is_finite_measure_on_compacts_of_compact_space [topological_space α] [measurable_space α] {μ : measure α} [compact_space α] : is_finite_measure μ ↔ is_finite_measure_on_compacts μ := begin split; introsI, { apply_instance, }, { exact compact_space.is_finite_measure, }, end /-- Compact covering of a `σ`-compact topological space as `measure_theory.measure.finite_spanning_sets_in`. -/ def measure_theory.measure.finite_spanning_sets_in_compact [topological_space α] [sigma_compact_space α] {m : measurable_space α} (μ : measure α) [is_locally_finite_measure μ] : μ.finite_spanning_sets_in {K | is_compact K} := { set := compact_covering α, set_mem := is_compact_compact_covering α, finite := λ n, (is_compact_compact_covering α n).measure_lt_top, spanning := Union_compact_covering α } /-- A locally finite measure on a `σ`-compact topological space admits a finite spanning sequence of open sets. -/ def measure_theory.measure.finite_spanning_sets_in_open [topological_space α] [sigma_compact_space α] {m : measurable_space α} (μ : measure α) [is_locally_finite_measure μ] : μ.finite_spanning_sets_in {K | is_open K} := { set := λ n, ((is_compact_compact_covering α n).exists_open_superset_measure_lt_top μ).some, set_mem := λ n, ((is_compact_compact_covering α n).exists_open_superset_measure_lt_top μ).some_spec.snd.1, finite := λ n, ((is_compact_compact_covering α n).exists_open_superset_measure_lt_top μ).some_spec.snd.2, spanning := eq_univ_of_subset (Union_mono $ λ n, ((is_compact_compact_covering α n).exists_open_superset_measure_lt_top μ).some_spec.fst) (Union_compact_covering α) } section measure_Ixx variables [preorder α] [topological_space α] [compact_Icc_space α] {m : measurable_space α} {μ : measure α} [is_locally_finite_measure μ] {a b : α} lemma measure_Icc_lt_top : μ (Icc a b) < ∞ := is_compact_Icc.measure_lt_top lemma measure_Ico_lt_top : μ (Ico a b) < ∞ := (measure_mono Ico_subset_Icc_self).trans_lt measure_Icc_lt_top lemma measure_Ioc_lt_top : μ (Ioc a b) < ∞ := (measure_mono Ioc_subset_Icc_self).trans_lt measure_Icc_lt_top lemma measure_Ioo_lt_top : μ (Ioo a b) < ∞ := (measure_mono Ioo_subset_Icc_self).trans_lt measure_Icc_lt_top end measure_Ixx section piecewise variables [measurable_space α] {μ : measure α} {s t : set α} {f g : α → β} lemma piecewise_ae_eq_restrict (hs : measurable_set s) : piecewise s f g =ᵐ[μ.restrict s] f := begin rw [ae_restrict_eq hs], exact (piecewise_eq_on s f g).eventually_eq.filter_mono inf_le_right end lemma piecewise_ae_eq_restrict_compl (hs : measurable_set s) : piecewise s f g =ᵐ[μ.restrict sᶜ] g := begin rw [ae_restrict_eq hs.compl], exact (piecewise_eq_on_compl s f g).eventually_eq.filter_mono inf_le_right end lemma piecewise_ae_eq_of_ae_eq_set (hst : s =ᵐ[μ] t) : s.piecewise f g =ᵐ[μ] t.piecewise f g := hst.mem_iff.mono $ λ x hx, by simp [piecewise, hx] end piecewise section indicator_function variables [measurable_space α] {μ : measure α} {s t : set α} {f : α → β} lemma mem_map_indicator_ae_iff_mem_map_restrict_ae_of_zero_mem [has_zero β] {t : set β} (ht : (0 : β) ∈ t) (hs : measurable_set s) : t ∈ filter.map (s.indicator f) μ.ae ↔ t ∈ filter.map f (μ.restrict s).ae := begin simp_rw [mem_map, mem_ae_iff], rw [measure.restrict_apply' hs, set.indicator_preimage, set.ite], simp_rw [set.compl_union, set.compl_inter], change μ (((f ⁻¹' t)ᶜ ∪ sᶜ) ∩ ((λ x, (0 : β)) ⁻¹' t \ s)ᶜ) = 0 ↔ μ ((f ⁻¹' t)ᶜ ∩ s) = 0, simp only [ht, ← set.compl_eq_univ_diff, compl_compl, set.compl_union, if_true, set.preimage_const], simp_rw [set.union_inter_distrib_right, set.compl_inter_self s, set.union_empty], end lemma mem_map_indicator_ae_iff_of_zero_nmem [has_zero β] {t : set β} (ht : (0 : β) ∉ t) : t ∈ filter.map (s.indicator f) μ.ae ↔ μ ((f ⁻¹' t)ᶜ ∪ sᶜ) = 0 := begin rw [mem_map, mem_ae_iff, set.indicator_preimage, set.ite, set.compl_union, set.compl_inter], change μ (((f ⁻¹' t)ᶜ ∪ sᶜ) ∩ ((λ x, (0 : β)) ⁻¹' t \ s)ᶜ) = 0 ↔ μ ((f ⁻¹' t)ᶜ ∪ sᶜ) = 0, simp only [ht, if_false, set.compl_empty, set.empty_diff, set.inter_univ, set.preimage_const], end lemma map_restrict_ae_le_map_indicator_ae [has_zero β] (hs : measurable_set s) : filter.map f (μ.restrict s).ae ≤ filter.map (s.indicator f) μ.ae := begin intro t, by_cases ht : (0 : β) ∈ t, { rw mem_map_indicator_ae_iff_mem_map_restrict_ae_of_zero_mem ht hs, exact id, }, rw [mem_map_indicator_ae_iff_of_zero_nmem ht, mem_map_restrict_ae_iff hs], exact λ h, measure_mono_null ((set.inter_subset_left _ _).trans (set.subset_union_left _ _)) h, end variables [has_zero β] lemma indicator_ae_eq_restrict (hs : measurable_set s) : indicator s f =ᵐ[μ.restrict s] f := piecewise_ae_eq_restrict hs lemma indicator_ae_eq_restrict_compl (hs : measurable_set s) : indicator s f =ᵐ[μ.restrict sᶜ] 0 := piecewise_ae_eq_restrict_compl hs lemma indicator_ae_eq_of_restrict_compl_ae_eq_zero (hs : measurable_set s) (hf : f =ᵐ[μ.restrict sᶜ] 0) : s.indicator f =ᵐ[μ] f := begin rw [filter.eventually_eq, ae_restrict_iff' hs.compl] at hf, filter_upwards [hf] with x hx, by_cases hxs : x ∈ s, { simp only [hxs, set.indicator_of_mem], }, { simp only [hx hxs, pi.zero_apply, set.indicator_apply_eq_zero, eq_self_iff_true, implies_true_iff], }, end lemma indicator_ae_eq_zero_of_restrict_ae_eq_zero (hs : measurable_set s) (hf : f =ᵐ[μ.restrict s] 0) : s.indicator f =ᵐ[μ] 0 := begin rw [filter.eventually_eq, ae_restrict_iff' hs] at hf, filter_upwards [hf] with x hx, by_cases hxs : x ∈ s, { simp only [hxs, hx hxs, set.indicator_of_mem], }, { simp [hx, hxs], }, end lemma indicator_ae_eq_of_ae_eq_set (hst : s =ᵐ[μ] t) : s.indicator f =ᵐ[μ] t.indicator f := piecewise_ae_eq_of_ae_eq_set hst lemma indicator_meas_zero (hs : μ s = 0) : indicator s f =ᵐ[μ] 0 := (indicator_empty' f) ▸ indicator_ae_eq_of_ae_eq_set (ae_eq_empty.2 hs) lemma ae_eq_restrict_iff_indicator_ae_eq {g : α → β} (hs : measurable_set s) : f =ᵐ[μ.restrict s] g ↔ s.indicator f =ᵐ[μ] s.indicator g := begin rw [filter.eventually_eq, ae_restrict_iff' hs], refine ⟨λ h, _, λ h, _⟩; filter_upwards [h] with x hx, { by_cases hxs : x ∈ s, { simp [hxs, hx hxs], }, { simp [hxs], }, }, { intros hxs, simpa [hxs] using hx, }, end end indicator_function
89dce1f5c4dc6ed54f8f889b7fbcaefed5ce6619
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/tests/lean/choice_expl.lean
2a4d97e976fedc9e78975b93c0953f3e03a3f1b8
[ "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
247
lean
universe variables u namespace N1 definition pr {A : Type u} (a b : A) := a end N1 namespace N2 definition pr {A : Type u} (a b : A) := b end N2 open N1 N2 constant N : Type.{1} constants a b : N check @N1.pr check @N2.pr N a b check pr a b
5fb7626c2d1b71d5268e2b8c8736e55d6bed7119
4fa161becb8ce7378a709f5992a594764699e268
/src/category_theory/limits/shapes/products.lean
112f5d45569b8818140d0a6f6bb84f6533e14e13
[ "Apache-2.0" ]
permissive
laughinggas/mathlib
e4aa4565ae34e46e834434284cb26bd9d67bc373
86dcd5cda7a5017c8b3c8876c89a510a19d49aad
refs/heads/master
1,669,496,232,688
1,592,831,995,000
1,592,831,995,000
274,155,979
0
0
Apache-2.0
1,592,835,190,000
1,592,835,189,000
null
UTF-8
Lean
false
false
3,053
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.limits.limits import category_theory.discrete_category universes v u open category_theory namespace category_theory.limits variables {β : Type v} variables {C : Type u} [category.{v} C] -- We don't need an analogue of `pair` (for binary products), `parallel_pair` (for equalizers), -- or `(co)span`, since we already have `discrete.functor`. abbreviation fan (f : β → C) := cone (discrete.functor f) abbreviation cofan (f : β → C) := cocone (discrete.functor f) def fan.mk {f : β → C} {P : C} (p : Π b, P ⟶ f b) : fan f := { X := P, π := { app := p } } def cofan.mk {f : β → C} {P : C} (p : Π b, f b ⟶ P) : cofan f := { X := P, ι := { app := p } } @[simp] lemma fan.mk_π_app {f : β → C} {P : C} (p : Π b, P ⟶ f b) (b : β) : (fan.mk p).π.app b = p b := rfl @[simp] lemma cofan.mk_π_app {f : β → C} {P : C} (p : Π b, f b ⟶ P) (b : β) : (cofan.mk p).ι.app b = p b := rfl /-- `pi_obj f` computes the product of a family of elements `f`. (It is defined as an abbreviation for `limit (discrete.functor f)`, so for most facts about `pi_obj f`, you will just use general facts about limits.) -/ abbreviation pi_obj (f : β → C) [has_limit (discrete.functor f)] := limit (discrete.functor f) /-- `sigma_obj f` computes the coproduct of a family of elements `f`. (It is defined as an abbreviation for `colimit (discrete.functor f)`, so for most facts about `sigma_obj f`, you will just use general facts about colimits.) -/ abbreviation sigma_obj (f : β → C) [has_colimit (discrete.functor f)] := colimit (discrete.functor f) notation `∏ ` f:20 := pi_obj f notation `∐ ` f:20 := sigma_obj f abbreviation pi.π (f : β → C) [has_limit (discrete.functor f)] (b : β) : ∏ f ⟶ f b := limit.π (discrete.functor f) b abbreviation sigma.ι (f : β → C) [has_colimit (discrete.functor f)] (b : β) : f b ⟶ ∐ f := colimit.ι (discrete.functor f) b abbreviation pi.lift {f : β → C} [has_limit (discrete.functor f)] {P : C} (p : Π b, P ⟶ f b) : P ⟶ ∏ f := limit.lift _ (fan.mk p) abbreviation sigma.desc {f : β → C} [has_colimit (discrete.functor f)] {P : C} (p : Π b, f b ⟶ P) : ∐ f ⟶ P := colimit.desc _ (cofan.mk p) abbreviation pi.map {f g : β → C} [has_limits_of_shape.{v} (discrete β) C] (p : Π b, f b ⟶ g b) : ∏ f ⟶ ∏ g := lim.map (discrete.nat_trans p) abbreviation sigma.map {f g : β → C} [has_colimits_of_shape.{v} (discrete β) C] (p : Π b, f b ⟶ g b) : ∐ f ⟶ ∐ g := colim.map (discrete.nat_trans p) variables (C) class has_products := (has_limits_of_shape : Π (J : Type v), has_limits_of_shape.{v} (discrete J) C) class has_coproducts := (has_colimits_of_shape : Π (J : Type v), has_colimits_of_shape.{v} (discrete J) C) attribute [instance] has_products.has_limits_of_shape has_coproducts.has_colimits_of_shape end category_theory.limits
5d1f70fe08fe0b786c3f9437403b1d319a078acd
5d62e434e81e3303af5bef665e46bef75f10b45e
/polynomial_ideal/src/test.lean
9fa8e196555a1a5b24ffef7336b53cb2066a3e6b
[]
no_license
ChrisHughes24/type_class
4b49764e9b96b2c7d2b6e4e7827730ca7a64a3b2
b582b60bdc84b7cd17b4243600088143afddc65c
refs/heads/master
1,595,404,449,845
1,567,951,656,000
1,567,951,656,000
207,121,926
0
0
null
null
null
null
UTF-8
Lean
false
false
371
lean
import data.polynomial ring_theory.localization variables (α : Type*) [discrete_field α] variables (I : ideal (polynomial α)) -- `ring_theory.localization` import breaks this -- https://leanprover.zulipchat.com/#narrow/stream/113488-general/topic/timeout.20when.20working.20with.20ideal.20of.20polynomial.20ring #check (I : submodule (polynomial α) (polynomial α))
1193042eda58f86c26982402347ceaa3d41abc4d
22e97a5d648fc451e25a06c668dc03ac7ed7bc25
/src/data/matrix/pequiv.lean
d8a8d7809c9f0c57cf3bd347b4cccb2ef8b7a391
[ "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
5,733
lean
/- Copyright (c) 2019 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import data.matrix.basic import data.pequiv /- # partial equivalences for matrices Using partial equivalences to represent matrices. This file introduces the function `pequiv.to_matrix`, which returns a matrix containing ones and zeros. For any partial equivalence `f`, `f.to_matrix i j = 1 ↔ f i = some j`. The following important properties of this function are proved `to_matrix_trans : (f.trans g).to_matrix = f.to_matrix ⬝ g.to_matrix` `to_matrix_symm : f.symm.to_matrix = f.to_matrixᵀ` `to_matrix_refl : (pequiv.refl n).to_matrix = 1` `to_matrix_bot : ⊥.to_matrix = 0` This theory gives the matrix representation of projection linear maps, and their right inverses. For example, the matrix `(single (0 : fin 1) (i : fin n)).to_matrix` corresponds to the the ith projection map from R^n to R. Any injective function `fin m → fin n` gives rise to a `pequiv`, whose matrix is the projection map from R^m → R^n represented by the same function. The transpose of this matrix is the right inverse of this map, sending anything not in the image to zero. ## notations This file uses the notation ` ⬝ ` for `matrix.mul` and `ᵀ` for `matrix.transpose`. -/ namespace pequiv open matrix universes u v variables {k l m n : Type u} variables [fintype k] [fintype l] [fintype m] [fintype n] variables {α : Type v} open_locale matrix /-- `to_matrix` returns a matrix containing ones and zeros. `f.to_matrix i j` is `1` if `f i = some j` and `0` otherwise -/ def to_matrix [decidable_eq n] [has_zero α] [has_one α] (f : m ≃. n) : matrix m n α | i j := if j ∈ f i then 1 else 0 lemma mul_matrix_apply [decidable_eq m] [semiring α] (f : l ≃. m) (M : matrix m n α) (i j) : (f.to_matrix ⬝ M) i j = option.cases_on (f i) 0 (λ fi, M fi j) := begin dsimp [to_matrix, matrix.mul_val], cases h : f i with fi, { simp [h] }, { rw finset.sum_eq_single fi; simp [h, eq_comm] {contextual := tt} } end lemma to_matrix_symm [decidable_eq m] [decidable_eq n] [has_zero α] [has_one α] (f : m ≃. n) : (f.symm.to_matrix : matrix n m α) = f.to_matrixᵀ := by ext; simp only [transpose, mem_iff_mem f, to_matrix]; congr @[simp] lemma to_matrix_refl [decidable_eq n] [has_zero α] [has_one α] : ((pequiv.refl n).to_matrix : matrix n n α) = 1 := by ext; simp [to_matrix, one_val]; congr lemma matrix_mul_apply [semiring α] [decidable_eq n] (M : matrix l m α) (f : m ≃. n) (i j) : (M ⬝ f.to_matrix) i j = option.cases_on (f.symm j) 0 (λ fj, M i fj) := begin dsimp [to_matrix, matrix.mul_val], cases h : f.symm j with fj, { simp [h, f.eq_some_iff.symm] }, { conv in (_ ∈ _) { rw ← f.mem_iff_mem }, rw finset.sum_eq_single fj, { simp [h, f.eq_some_iff.symm], }, { intros b H n, simp [h, f.eq_some_iff.symm, n.symm], }, { simp, } } end lemma to_pequiv_mul_matrix [decidable_eq m] [semiring α] (f : m ≃ m) (M : matrix m n α) : (f.to_pequiv.to_matrix ⬝ M) = λ i, M (f i) := by { ext i j, rw [mul_matrix_apply, equiv.to_pequiv_apply] } lemma to_matrix_trans [decidable_eq m] [decidable_eq n] [semiring α] (f : l ≃. m) (g : m ≃. n) : ((f.trans g).to_matrix : matrix l n α) = f.to_matrix ⬝ g.to_matrix := begin ext i j, rw [mul_matrix_apply], dsimp [to_matrix, pequiv.trans], cases f i; simp end @[simp] lemma to_matrix_bot [decidable_eq n] [has_zero α] [has_one α] : ((⊥ : pequiv m n).to_matrix : matrix m n α) = 0 := rfl lemma to_matrix_injective [decidable_eq n] [zero_ne_one_class α] : function.injective (@to_matrix m n _ _ α _ _ _) := begin classical, assume f g, refine not_imp_not.1 _, simp only [matrix.ext_iff.symm, to_matrix, pequiv.ext_iff, classical.not_forall, exists_imp_distrib], assume i hi, use i, cases hf : f i with fi, { cases hg : g i with gi, { cc }, { use gi, simp } }, { use fi, simp [hf.symm, ne.symm hi] } end lemma to_matrix_swap [decidable_eq n] [ring α] (i j : n) : (equiv.swap i j).to_pequiv.to_matrix = (1 : matrix n n α) - (single i i).to_matrix - (single j j).to_matrix + (single i j).to_matrix + (single j i).to_matrix := begin ext, dsimp [to_matrix, single, equiv.swap_apply_def, equiv.to_pequiv, one_val], split_ifs; simp * at * end @[simp] lemma single_mul_single [decidable_eq k] [decidable_eq m] [decidable_eq n] [semiring α] (a : m) (b : n) (c : k) : ((single a b).to_matrix : matrix _ _ α) ⬝ (single b c).to_matrix = (single a c).to_matrix := by rw [← to_matrix_trans, single_trans_single] lemma single_mul_single_of_ne [decidable_eq k] [decidable_eq m] [decidable_eq n] [semiring α] {b₁ b₂ : n} (hb : b₁ ≠ b₂) (a : m) (c : k) : ((single a b₁).to_matrix : matrix _ _ α) ⬝ (single b₂ c).to_matrix = 0 := by rw [← to_matrix_trans, single_trans_single_of_ne hb, to_matrix_bot] /-- Restatement of `single_mul_single`, which will simplify expressions in `simp` normal form, when associativity may otherwise need to be carefully applied. -/ @[simp] lemma single_mul_single_right [decidable_eq k] [decidable_eq m] [decidable_eq n] [semiring α] (a : m) (b : n) (c : k) (M : matrix k l α) : (single a b).to_matrix ⬝ ((single b c).to_matrix ⬝ M) = (single a c).to_matrix ⬝ M := by rw [← matrix.mul_assoc, single_mul_single] /-- We can also define permutation matrices by permuting the rows of the identity matrix. -/ lemma equiv_to_pequiv_to_matrix [decidable_eq n] [has_zero α] [has_one α] (σ : equiv n n) (i j : n) : σ.to_pequiv.to_matrix i j = (1 : matrix n n α) (σ i) j := if_congr option.some_inj rfl rfl end pequiv
3b75526662c692183a83a960c2415ff00dd7c036
4727251e0cd73359b15b664c3170e5d754078599
/src/category_theory/limits/shapes/kernels.lean
3a244f923f03c1715157430c4ea9839f9a9fb9b8
[ "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
36,260
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Markus Himmel -/ import category_theory.limits.preserves.shapes.zero /-! # Kernels and cokernels In a category with zero morphisms, the kernel of a morphism `f : X ⟶ Y` is the equalizer of `f` and `0 : X ⟶ Y`. (Similarly the cokernel is the coequalizer.) The basic definitions are * `kernel : (X ⟶ Y) → C` * `kernel.ι : kernel f ⟶ X` * `kernel.condition : kernel.ι f ≫ f = 0` and * `kernel.lift (k : W ⟶ X) (h : k ≫ f = 0) : W ⟶ kernel f` (as well as the dual versions) ## Main statements Besides the definition and lifts, we prove * `kernel.ι_zero_is_iso`: a kernel map of a zero morphism is an isomorphism * `kernel.eq_zero_of_epi_kernel`: if `kernel.ι f` is an epimorphism, then `f = 0` * `kernel.of_mono`: the kernel of a monomorphism is the zero object * `kernel.lift_mono`: the lift of a monomorphism `k : W ⟶ X` such that `k ≫ f = 0` is still a monomorphism * `kernel.is_limit_cone_zero_cone`: if our category has a zero object, then the map from the zero obect is a kernel map of any monomorphism * `kernel.ι_of_zero`: `kernel.ι (0 : X ⟶ Y)` is an isomorphism and the corresponding dual statements. ## Future work * TODO: connect this with existing working in the group theory and ring theory libraries. ## Implementation notes As with the other special shapes in the limits library, all the definitions here are given as `abbreviation`s of the general statements for limits, so all the `simp` lemmas and theorems about general limits can be used. ## References * [F. Borceux, *Handbook of Categorical Algebra 2*][borceux-vol2] -/ noncomputable theory universes v u u' u₂ open category_theory open category_theory.limits.walking_parallel_pair namespace category_theory.limits variables {C : Type u} [category.{v} C] variables [has_zero_morphisms C] /-- A morphism `f` has a kernel if the functor `parallel_pair f 0` has a limit. -/ abbreviation has_kernel {X Y : C} (f : X ⟶ Y) : Prop := has_limit (parallel_pair f 0) /-- A morphism `f` has a cokernel if the functor `parallel_pair f 0` has a colimit. -/ abbreviation has_cokernel {X Y : C} (f : X ⟶ Y) : Prop := has_colimit (parallel_pair f 0) variables {X Y : C} (f : X ⟶ Y) section /-- A kernel fork is just a fork where the second morphism is a zero morphism. -/ abbreviation kernel_fork := fork f 0 variables {f} @[simp, reassoc] lemma kernel_fork.condition (s : kernel_fork f) : fork.ι s ≫ f = 0 := by erw [fork.condition, has_zero_morphisms.comp_zero] @[simp] lemma kernel_fork.app_one (s : kernel_fork f) : s.π.app one = 0 := by simp [fork.app_one_eq_ι_comp_right] /-- A morphism `ι` satisfying `ι ≫ f = 0` determines a kernel fork over `f`. -/ abbreviation kernel_fork.of_ι {Z : C} (ι : Z ⟶ X) (w : ι ≫ f = 0) : kernel_fork f := fork.of_ι ι $ by rw [w, has_zero_morphisms.comp_zero] @[simp] lemma kernel_fork.ι_of_ι {X Y P : C} (f : X ⟶ Y) (ι : P ⟶ X) (w : ι ≫ f = 0) : fork.ι (kernel_fork.of_ι ι w) = ι := rfl section local attribute [tidy] tactic.case_bash /-- Every kernel fork `s` is isomorphic (actually, equal) to `fork.of_ι (fork.ι s) _`. -/ def iso_of_ι (s : fork f 0) : s ≅ fork.of_ι (fork.ι s) (fork.condition s) := cones.ext (iso.refl _) $ by tidy /-- If `ι = ι'`, then `fork.of_ι ι _` and `fork.of_ι ι' _` are isomorphic. -/ def of_ι_congr {P : C} {ι ι' : P ⟶ X} {w : ι ≫ f = 0} (h : ι = ι') : kernel_fork.of_ι ι w ≅ kernel_fork.of_ι ι' (by rw [←h, w]) := cones.ext (iso.refl _) $ by tidy /-- If `F` is an equivalence, then applying `F` to a diagram indexing a (co)kernel of `f` yields the diagram indexing the (co)kernel of `F.map f`. -/ def comp_nat_iso {D : Type u'} [category.{v} D] [has_zero_morphisms D] (F : C ⥤ D) [is_equivalence F] : parallel_pair f 0 ⋙ F ≅ parallel_pair (F.map f) 0 := nat_iso.of_components (λ j, match j with | zero := iso.refl _ | one := iso.refl _ end) $ by tidy end /-- If `s` is a limit kernel fork and `k : W ⟶ X` satisfies ``k ≫ f = 0`, then there is some `l : W ⟶ s.X` such that `l ≫ fork.ι s = k`. -/ def kernel_fork.is_limit.lift' {s : kernel_fork f} (hs : is_limit s) {W : C} (k : W ⟶ X) (h : k ≫ f = 0) : {l : W ⟶ s.X // l ≫ fork.ι s = k} := ⟨hs.lift $ kernel_fork.of_ι _ h, hs.fac _ _⟩ /-- This is a slightly more convenient method to verify that a kernel fork is a limit cone. It only asks for a proof of facts that carry any mathematical content -/ def is_limit_aux (t : kernel_fork f) (lift : Π (s : kernel_fork f), s.X ⟶ t.X) (fac : ∀ (s : kernel_fork f), lift s ≫ t.ι = s.ι) (uniq : ∀ (s : kernel_fork f) (m : s.X ⟶ t.X) (w : m ≫ t.ι = s.ι), m = lift s) : is_limit t := { lift := lift, fac' := λ s j, by { cases j, { exact fac s, }, { simp, }, }, uniq' := λ s m w, uniq s m (w limits.walking_parallel_pair.zero), } /-- This is a more convenient formulation to show that a `kernel_fork` constructed using `kernel_fork.of_ι` is a limit cone. -/ def is_limit.of_ι {W : C} (g : W ⟶ X) (eq : g ≫ f = 0) (lift : Π {W' : C} (g' : W' ⟶ X) (eq' : g' ≫ f = 0), W' ⟶ W) (fac : ∀ {W' : C} (g' : W' ⟶ X) (eq' : g' ≫ f = 0), lift g' eq' ≫ g = g') (uniq : ∀ {W' : C} (g' : W' ⟶ X) (eq' : g' ≫ f = 0) (m : W' ⟶ W) (w : m ≫ g = g'), m = lift g' eq') : is_limit (kernel_fork.of_ι g eq) := is_limit_aux _ (λ s, lift s.ι s.condition) (λ s, fac s.ι s.condition) (λ s, uniq s.ι s.condition) /-- Every kernel of `f` induces a kernel of `f ≫ g` if `g` is mono. -/ def is_kernel_comp_mono {c : kernel_fork f} (i : is_limit c) {Z} (g : Y ⟶ Z) [hg : mono g] {h : X ⟶ Z} (hh : h = f ≫ g) : is_limit (kernel_fork.of_ι c.ι (by simp [hh]) : kernel_fork h) := fork.is_limit.mk' _ $ λ s, let s' : kernel_fork f := fork.of_ι s.ι (by rw [←cancel_mono g]; simp [←hh, s.condition]) in let l := kernel_fork.is_limit.lift' i s'.ι s'.condition in ⟨l.1, l.2, λ m hm, by apply fork.is_limit.hom_ext i; rw fork.ι_of_ι at hm; rw hm; exact l.2.symm⟩ lemma is_kernel_comp_mono_lift {c : kernel_fork f} (i : is_limit c) {Z} (g : Y ⟶ Z) [hg : mono g] {h : X ⟶ Z} (hh : h = f ≫ g) (s : kernel_fork h) : (is_kernel_comp_mono i g hh).lift s = i.lift (fork.of_ι s.ι (by { rw [←cancel_mono g, category.assoc, ←hh], simp })) := rfl end section variables [has_kernel f] /-- The kernel of a morphism, expressed as the equalizer with the 0 morphism. -/ abbreviation kernel : C := equalizer f 0 /-- The map from `kernel f` into the source of `f`. -/ abbreviation kernel.ι : kernel f ⟶ X := equalizer.ι f 0 @[simp] lemma equalizer_as_kernel : equalizer.ι f 0 = kernel.ι f := rfl @[simp, reassoc] lemma kernel.condition : kernel.ι f ≫ f = 0 := kernel_fork.condition _ /-- The kernel built from `kernel.ι f` is limiting. -/ def kernel_is_kernel : is_limit (fork.of_ι (kernel.ι f) ((kernel.condition f).trans (comp_zero.symm))) := is_limit.of_iso_limit (limit.is_limit _) (fork.ext (iso.refl _) (by tidy)) /-- Given any morphism `k : W ⟶ X` satisfying `k ≫ f = 0`, `k` factors through `kernel.ι f` via `kernel.lift : W ⟶ kernel f`. -/ abbreviation kernel.lift {W : C} (k : W ⟶ X) (h : k ≫ f = 0) : W ⟶ kernel f := limit.lift (parallel_pair f 0) (kernel_fork.of_ι k h) @[simp, reassoc] lemma kernel.lift_ι {W : C} (k : W ⟶ X) (h : k ≫ f = 0) : kernel.lift f k h ≫ kernel.ι f = k := limit.lift_π _ _ @[simp] lemma kernel.lift_zero {W : C} {h} : kernel.lift f (0 : W ⟶ X) h = 0 := by { ext, simp, } instance kernel.lift_mono {W : C} (k : W ⟶ X) (h : k ≫ f = 0) [mono k] : mono (kernel.lift f k h) := ⟨λ Z g g' w, begin replace w := w =≫ kernel.ι f, simp only [category.assoc, kernel.lift_ι] at w, exact (cancel_mono k).1 w, end⟩ /-- Any morphism `k : W ⟶ X` satisfying `k ≫ f = 0` induces a morphism `l : W ⟶ kernel f` such that `l ≫ kernel.ι f = k`. -/ def kernel.lift' {W : C} (k : W ⟶ X) (h : k ≫ f = 0) : {l : W ⟶ kernel f // l ≫ kernel.ι f = k} := ⟨kernel.lift f k h, kernel.lift_ι _ _ _⟩ /-- A commuting square induces a morphism of kernels. -/ abbreviation kernel.map {X' Y' : C} (f' : X' ⟶ Y') [has_kernel f'] (p : X ⟶ X') (q : Y ⟶ Y') (w : f ≫ q = p ≫ f') : kernel f ⟶ kernel f' := kernel.lift f' (kernel.ι f ≫ p) (by simp [←w]) /-- Given a commutative diagram X --f--> Y --g--> Z | | | | | | v v v X' -f'-> Y' -g'-> Z' with horizontal arrows composing to zero, then we obtain a commutative square X ---> kernel g | | | | kernel.map | | v v X' --> kernel g' -/ lemma kernel.lift_map {X Y Z X' Y' Z' : C} (f : X ⟶ Y) (g : Y ⟶ Z) [has_kernel g] (w : f ≫ g = 0) (f' : X' ⟶ Y') (g' : Y' ⟶ Z') [has_kernel g'] (w' : f' ≫ g' = 0) (p : X ⟶ X') (q : Y ⟶ Y') (r : Z ⟶ Z') (h₁ : f ≫ q = p ≫ f') (h₂ : g ≫ r = q ≫ g') : kernel.lift g f w ≫ kernel.map g g' q r h₂ = p ≫ kernel.lift g' f' w' := by { ext, simp [h₁], } /-- A commuting square of isomorphisms induces an isomorphism of kernels. -/ @[simps] def kernel.map_iso {X' Y' : C} (f' : X' ⟶ Y') [has_kernel f'] (p : X ≅ X') (q : Y ≅ Y') (w : f ≫ q.hom = p.hom ≫ f') : kernel f ≅ kernel f' := { hom := kernel.map f f' p.hom q.hom w, inv := kernel.map f' f p.inv q.inv (by { refine (cancel_mono q.hom).1 _, simp [w], }), } /-- Every kernel of the zero morphism is an isomorphism -/ instance kernel.ι_zero_is_iso : is_iso (kernel.ι (0 : X ⟶ Y)) := equalizer.ι_of_self _ lemma eq_zero_of_epi_kernel [epi (kernel.ι f)] : f = 0 := (cancel_epi (kernel.ι f)).1 (by simp) /-- The kernel of a zero morphism is isomorphic to the source. -/ def kernel_zero_iso_source : kernel (0 : X ⟶ Y) ≅ X := equalizer.iso_source_of_self 0 @[simp] lemma kernel_zero_iso_source_hom : kernel_zero_iso_source.hom = kernel.ι (0 : X ⟶ Y) := rfl @[simp] lemma kernel_zero_iso_source_inv : kernel_zero_iso_source.inv = kernel.lift (0 : X ⟶ Y) (𝟙 X) (by simp) := by { ext, simp [kernel_zero_iso_source], } /-- If two morphisms are known to be equal, then their kernels are isomorphic. -/ def kernel_iso_of_eq {f g : X ⟶ Y} [has_kernel f] [has_kernel g] (h : f = g) : kernel f ≅ kernel g := has_limit.iso_of_nat_iso (by simp[h]) @[simp] lemma kernel_iso_of_eq_refl {h : f = f} : kernel_iso_of_eq h = iso.refl (kernel f) := by { ext, simp [kernel_iso_of_eq], } @[simp, reassoc] lemma kernel_iso_of_eq_hom_comp_ι {f g : X ⟶ Y} [has_kernel f] [has_kernel g] (h : f = g) : (kernel_iso_of_eq h).hom ≫ kernel.ι _ = kernel.ι _ := by { unfreezingI { induction h, simp } } @[simp, reassoc] lemma kernel_iso_of_eq_inv_comp_ι {f g : X ⟶ Y} [has_kernel f] [has_kernel g] (h : f = g) : (kernel_iso_of_eq h).inv ≫ kernel.ι _ = kernel.ι _ := by { unfreezingI { induction h, simp } } @[simp, reassoc] lemma lift_comp_kernel_iso_of_eq_hom {Z} {f g : X ⟶ Y} [has_kernel f] [has_kernel g] (h : f = g) (e : Z ⟶ X) (he) : kernel.lift _ e he ≫ (kernel_iso_of_eq h).hom = kernel.lift _ e (by simp [← h, he]) := by { unfreezingI { induction h, simp } } @[simp, reassoc] lemma lift_comp_kernel_iso_of_eq_inv {Z} {f g : X ⟶ Y} [has_kernel f] [has_kernel g] (h : f = g) (e : Z ⟶ X) (he) : kernel.lift _ e he ≫ (kernel_iso_of_eq h).inv = kernel.lift _ e (by simp [h, he]) := by { unfreezingI { induction h, simp } } @[simp] lemma kernel_iso_of_eq_trans {f g h : X ⟶ Y} [has_kernel f] [has_kernel g] [has_kernel h] (w₁ : f = g) (w₂ : g = h) : kernel_iso_of_eq w₁ ≪≫ kernel_iso_of_eq w₂ = kernel_iso_of_eq (w₁.trans w₂) := by { unfreezingI { induction w₁, induction w₂, }, ext, simp [kernel_iso_of_eq], } variables {f} lemma kernel_not_epi_of_nonzero (w : f ≠ 0) : ¬epi (kernel.ι f) := λ I, by exactI w (eq_zero_of_epi_kernel f) lemma kernel_not_iso_of_nonzero (w : f ≠ 0) : (is_iso (kernel.ι f)) → false := λ I, kernel_not_epi_of_nonzero w $ by { resetI, apply_instance } instance has_kernel_comp_mono {X Y Z : C} (f : X ⟶ Y) [has_kernel f] (g : Y ⟶ Z) [mono g] : has_kernel (f ≫ g) := ⟨⟨{ cone := _, is_limit := is_kernel_comp_mono (limit.is_limit _) g rfl }⟩⟩ /-- When `g` is a monomorphism, the kernel of `f ≫ g` is isomorphic to the kernel of `f`. -/ @[simps] def kernel_comp_mono {X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z) [has_kernel f] [mono g] : kernel (f ≫ g) ≅ kernel f := { hom := kernel.lift _ (kernel.ι _) (by { rw [←cancel_mono g], simp, }), inv := kernel.lift _ (kernel.ι _) (by simp), } instance has_kernel_iso_comp {X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z) [is_iso f] [has_kernel g] : has_kernel (f ≫ g) := { exists_limit := ⟨{ cone := kernel_fork.of_ι (kernel.ι g ≫ inv f) (by simp), is_limit := is_limit_aux _ (λ s, kernel.lift _ (s.ι ≫ f) (by tidy)) (by tidy) (λ s m w, by { simp_rw [←w], ext, simp, }), }⟩ } /-- When `f` is an isomorphism, the kernel of `f ≫ g` is isomorphic to the kernel of `g`. -/ @[simps] def kernel_is_iso_comp {X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z) [is_iso f] [has_kernel g] : kernel (f ≫ g) ≅ kernel g := { hom := kernel.lift _ (kernel.ι _ ≫ f) (by simp), inv := kernel.lift _ (kernel.ι _ ≫ inv f) (by simp), } end section has_zero_object variables [has_zero_object C] open_locale zero_object /-- The morphism from the zero object determines a cone on a kernel diagram -/ def kernel.zero_kernel_fork : kernel_fork f := { X := 0, π := { app := λ j, 0 }} /-- The map from the zero object is a kernel of a monomorphism -/ def kernel.is_limit_cone_zero_cone [mono f] : is_limit (kernel.zero_kernel_fork f) := fork.is_limit.mk _ (λ s, 0) (λ s, by { erw zero_comp, convert (zero_of_comp_mono f _).symm, exact kernel_fork.condition _ }) (λ _ _ _, zero_of_to_zero _) /-- The kernel of a monomorphism is isomorphic to the zero object -/ def kernel.of_mono [has_kernel f] [mono f] : kernel f ≅ 0 := functor.map_iso (cones.forget _) $ is_limit.unique_up_to_iso (limit.is_limit (parallel_pair f 0)) (kernel.is_limit_cone_zero_cone f) /-- The kernel morphism of a monomorphism is a zero morphism -/ lemma kernel.ι_of_mono [has_kernel f] [mono f] : kernel.ι f = 0 := zero_of_source_iso_zero _ (kernel.of_mono f) /-- If `g ≫ f = 0` implies `g = 0` for all `g`, then `0 : 0 ⟶ X` is a kernel of `f`. -/ def zero_kernel_of_cancel_zero {X Y : C} (f : X ⟶ Y) (hf : ∀ (Z : C) (g : Z ⟶ X) (hgf : g ≫ f = 0), g = 0) : is_limit (kernel_fork.of_ι (0 : 0 ⟶ X) (show 0 ≫ f = 0, by simp)) := fork.is_limit.mk _ (λ s, 0) (λ s, by rw [hf _ _ (kernel_fork.condition s), zero_comp]) (λ s m h, by ext) end has_zero_object section transport /-- If `i` is an isomorphism such that `l ≫ i.hom = f`, then any kernel of `f` is a kernel of `l`.-/ def is_kernel.of_comp_iso {Z : C} (l : X ⟶ Z) (i : Z ≅ Y) (h : l ≫ i.hom = f) {s : kernel_fork f} (hs : is_limit s) : is_limit (kernel_fork.of_ι (fork.ι s) $ show fork.ι s ≫ l = 0, by simp [←i.comp_inv_eq.2 h.symm]) := fork.is_limit.mk _ (λ s, hs.lift $ kernel_fork.of_ι (fork.ι s) $ by simp [←h]) (λ s, by simp) (λ s m h, by { apply fork.is_limit.hom_ext hs, simpa using h }) /-- If `i` is an isomorphism such that `l ≫ i.hom = f`, then the kernel of `f` is a kernel of `l`.-/ def kernel.of_comp_iso [has_kernel f] {Z : C} (l : X ⟶ Z) (i : Z ≅ Y) (h : l ≫ i.hom = f) : is_limit (kernel_fork.of_ι (kernel.ι f) $ show kernel.ι f ≫ l = 0, by simp [←i.comp_inv_eq.2 h.symm]) := is_kernel.of_comp_iso f l i h $ limit.is_limit _ /-- If `s` is any limit kernel cone over `f` and if `i` is an isomorphism such that `i.hom ≫ s.ι = l`, then `l` is a kernel of `f`. -/ def is_kernel.iso_kernel {Z : C} (l : Z ⟶ X) {s : kernel_fork f} (hs : is_limit s) (i : Z ≅ s.X) (h : i.hom ≫ fork.ι s = l) : is_limit (kernel_fork.of_ι l $ show l ≫ f = 0, by simp [←h]) := is_limit.of_iso_limit hs $ cones.ext i.symm $ λ j, by { cases j, { exact (iso.eq_inv_comp i).2 h }, { simp } } /-- If `i` is an isomorphism such that `i.hom ≫ kernel.ι f = l`, then `l` is a kernel of `f`. -/ def kernel.iso_kernel [has_kernel f] {Z : C} (l : Z ⟶ X) (i : Z ≅ kernel f) (h : i.hom ≫ kernel.ι f = l) : is_limit (kernel_fork.of_ι l $ by simp [←h]) := is_kernel.iso_kernel f l (limit.is_limit _) i h end transport section variables (X Y) /-- The kernel morphism of a zero morphism is an isomorphism -/ lemma kernel.ι_of_zero : is_iso (kernel.ι (0 : X ⟶ Y)) := equalizer.ι_of_self _ end section /-- A cokernel cofork is just a cofork where the second morphism is a zero morphism. -/ abbreviation cokernel_cofork := cofork f 0 variables {f} @[simp, reassoc] lemma cokernel_cofork.condition (s : cokernel_cofork f) : f ≫ s.π = 0 := by rw [cofork.condition, zero_comp] @[simp] lemma cokernel_cofork.π_eq_zero (s : cokernel_cofork f) : s.ι.app zero = 0 := by simp [cofork.app_zero_eq_comp_π_right] /-- A morphism `π` satisfying `f ≫ π = 0` determines a cokernel cofork on `f`. -/ abbreviation cokernel_cofork.of_π {Z : C} (π : Y ⟶ Z) (w : f ≫ π = 0) : cokernel_cofork f := cofork.of_π π $ by rw [w, zero_comp] @[simp] lemma cokernel_cofork.π_of_π {X Y P : C} (f : X ⟶ Y) (π : Y ⟶ P) (w : f ≫ π = 0) : cofork.π (cokernel_cofork.of_π π w) = π := rfl /-- Every cokernel cofork `s` is isomorphic (actually, equal) to `cofork.of_π (cofork.π s) _`. -/ def iso_of_π (s : cofork f 0) : s ≅ cofork.of_π (cofork.π s) (cofork.condition s) := cocones.ext (iso.refl _) $ λ j, by cases j; tidy /-- If `π = π'`, then `cokernel_cofork.of_π π _` and `cokernel_cofork.of_π π' _` are isomorphic. -/ def of_π_congr {P : C} {π π' : Y ⟶ P} {w : f ≫ π = 0} (h : π = π') : cokernel_cofork.of_π π w ≅ cokernel_cofork.of_π π' (by rw [←h, w]) := cocones.ext (iso.refl _) $ λ j, by cases j; tidy /-- If `s` is a colimit cokernel cofork, then every `k : Y ⟶ W` satisfying `f ≫ k = 0` induces `l : s.X ⟶ W` such that `cofork.π s ≫ l = k`. -/ def cokernel_cofork.is_colimit.desc' {s : cokernel_cofork f} (hs : is_colimit s) {W : C} (k : Y ⟶ W) (h : f ≫ k = 0) : {l : s.X ⟶ W // cofork.π s ≫ l = k} := ⟨hs.desc $ cokernel_cofork.of_π _ h, hs.fac _ _⟩ /-- This is a slightly more convenient method to verify that a cokernel cofork is a colimit cocone. It only asks for a proof of facts that carry any mathematical content -/ def is_colimit_aux (t : cokernel_cofork f) (desc : Π (s : cokernel_cofork f), t.X ⟶ s.X) (fac : ∀ (s : cokernel_cofork f), t.π ≫ desc s = s.π) (uniq : ∀ (s : cokernel_cofork f) (m : t.X ⟶ s.X) (w : t.π ≫ m = s.π), m = desc s) : is_colimit t := { desc := desc, fac' := λ s j, by { cases j, { simp, }, { exact fac s, }, }, uniq' := λ s m w, uniq s m (w limits.walking_parallel_pair.one), } /-- This is a more convenient formulation to show that a `cokernel_cofork` constructed using `cokernel_cofork.of_π` is a limit cone. -/ def is_colimit.of_π {Z : C} (g : Y ⟶ Z) (eq : f ≫ g = 0) (desc : Π {Z' : C} (g' : Y ⟶ Z') (eq' : f ≫ g' = 0), Z ⟶ Z') (fac : ∀ {Z' : C} (g' : Y ⟶ Z') (eq' : f ≫ g' = 0), g ≫ desc g' eq' = g') (uniq : ∀ {Z' : C} (g' : Y ⟶ Z') (eq' : f ≫ g' = 0) (m : Z ⟶ Z') (w : g ≫ m = g'), m = desc g' eq') : is_colimit (cokernel_cofork.of_π g eq) := is_colimit_aux _ (λ s, desc s.π s.condition) (λ s, fac s.π s.condition) (λ s, uniq s.π s.condition) /-- Every cokernel of `f` induces a cokernel of `g ≫ f` if `g` is epi. -/ def is_cokernel_epi_comp {c : cokernel_cofork f} (i : is_colimit c) {W} (g : W ⟶ X) [hg : epi g] {h : W ⟶ Y} (hh : h = g ≫ f) : is_colimit (cokernel_cofork.of_π c.π (by rw [hh]; simp) : cokernel_cofork h) := cofork.is_colimit.mk' _ $ λ s, let s' : cokernel_cofork f := cofork.of_π s.π (by { apply hg.left_cancellation, rw [←category.assoc, ←hh, s.condition], simp }) in let l := cokernel_cofork.is_colimit.desc' i s'.π s'.condition in ⟨l.1, l.2, λ m hm, by apply cofork.is_colimit.hom_ext i; rw cofork.π_of_π at hm; rw hm; exact l.2.symm⟩ @[simp] lemma is_cokernel_epi_comp_desc {c : cokernel_cofork f} (i : is_colimit c) {W} (g : W ⟶ X) [hg : epi g] {h : W ⟶ Y} (hh : h = g ≫ f) (s : cokernel_cofork h) : (is_cokernel_epi_comp i g hh).desc s = i.desc (cofork.of_π s.π (by { rw [←cancel_epi g, ←category.assoc, ←hh], simp })) := rfl end section variables [has_cokernel f] /-- The cokernel of a morphism, expressed as the coequalizer with the 0 morphism. -/ abbreviation cokernel : C := coequalizer f 0 /-- The map from the target of `f` to `cokernel f`. -/ abbreviation cokernel.π : Y ⟶ cokernel f := coequalizer.π f 0 @[simp] lemma coequalizer_as_cokernel : coequalizer.π f 0 = cokernel.π f := rfl @[simp, reassoc] lemma cokernel.condition : f ≫ cokernel.π f = 0 := cokernel_cofork.condition _ /-- The cokernel built from `cokernel.π f` is colimiting. -/ def cokernel_is_cokernel : is_colimit (cofork.of_π (cokernel.π f) ((cokernel.condition f).trans (zero_comp.symm))) := is_colimit.of_iso_colimit (colimit.is_colimit _) (cofork.ext (iso.refl _) (by tidy)) /-- Given any morphism `k : Y ⟶ W` such that `f ≫ k = 0`, `k` factors through `cokernel.π f` via `cokernel.desc : cokernel f ⟶ W`. -/ abbreviation cokernel.desc {W : C} (k : Y ⟶ W) (h : f ≫ k = 0) : cokernel f ⟶ W := colimit.desc (parallel_pair f 0) (cokernel_cofork.of_π k h) @[simp, reassoc] lemma cokernel.π_desc {W : C} (k : Y ⟶ W) (h : f ≫ k = 0) : cokernel.π f ≫ cokernel.desc f k h = k := colimit.ι_desc _ _ @[simp] lemma cokernel.desc_zero {W : C} {h} : cokernel.desc f (0 : Y ⟶ W) h = 0 := by { ext, simp, } instance cokernel.desc_epi {W : C} (k : Y ⟶ W) (h : f ≫ k = 0) [epi k] : epi (cokernel.desc f k h) := ⟨λ Z g g' w, begin replace w := cokernel.π f ≫= w, simp only [cokernel.π_desc_assoc] at w, exact (cancel_epi k).1 w, end⟩ /-- Any morphism `k : Y ⟶ W` satisfying `f ≫ k = 0` induces `l : cokernel f ⟶ W` such that `cokernel.π f ≫ l = k`. -/ def cokernel.desc' {W : C} (k : Y ⟶ W) (h : f ≫ k = 0) : {l : cokernel f ⟶ W // cokernel.π f ≫ l = k} := ⟨cokernel.desc f k h, cokernel.π_desc _ _ _⟩ /-- A commuting square induces a morphism of cokernels. -/ abbreviation cokernel.map {X' Y' : C} (f' : X' ⟶ Y') [has_cokernel f'] (p : X ⟶ X') (q : Y ⟶ Y') (w : f ≫ q = p ≫ f') : cokernel f ⟶ cokernel f' := cokernel.desc f (q ≫ cokernel.π f') (by simp [reassoc_of w]) /-- Given a commutative diagram X --f--> Y --g--> Z | | | | | | v v v X' -f'-> Y' -g'-> Z' with horizontal arrows composing to zero, then we obtain a commutative square cokernel f ---> Z | | | cokernel.map | | | v v cokernel f' --> Z' -/ lemma cokernel.map_desc {X Y Z X' Y' Z' : C} (f : X ⟶ Y) [has_cokernel f] (g : Y ⟶ Z) (w : f ≫ g = 0) (f' : X' ⟶ Y') [has_cokernel f'] (g' : Y' ⟶ Z') (w' : f' ≫ g' = 0) (p : X ⟶ X') (q : Y ⟶ Y') (r : Z ⟶ Z') (h₁ : f ≫ q = p ≫ f') (h₂ : g ≫ r = q ≫ g') : cokernel.map f f' p q h₁ ≫ cokernel.desc f' g' w' = cokernel.desc f g w ≫ r := by { ext, simp [h₂], } /-- A commuting square of isomorphisms induces an isomorphism of cokernels. -/ @[simps] def cokernel.map_iso {X' Y' : C} (f' : X' ⟶ Y') [has_cokernel f'] (p : X ≅ X') (q : Y ≅ Y') (w : f ≫ q.hom = p.hom ≫ f') : cokernel f ≅ cokernel f' := { hom := cokernel.map f f' p.hom q.hom w, inv := cokernel.map f' f p.inv q.inv (by { refine (cancel_mono q.hom).1 _, simp [w], }), } /-- The cokernel of the zero morphism is an isomorphism -/ instance cokernel.π_zero_is_iso : is_iso (cokernel.π (0 : X ⟶ Y)) := coequalizer.π_of_self _ lemma eq_zero_of_mono_cokernel [mono (cokernel.π f)] : f = 0 := (cancel_mono (cokernel.π f)).1 (by simp) /-- The cokernel of a zero morphism is isomorphic to the target. -/ def cokernel_zero_iso_target : cokernel (0 : X ⟶ Y) ≅ Y := coequalizer.iso_target_of_self 0 @[simp] lemma cokernel_zero_iso_target_hom : cokernel_zero_iso_target.hom = cokernel.desc (0 : X ⟶ Y) (𝟙 Y) (by simp) := by { ext, simp [cokernel_zero_iso_target], } @[simp] lemma cokernel_zero_iso_target_inv : cokernel_zero_iso_target.inv = cokernel.π (0 : X ⟶ Y) := rfl /-- If two morphisms are known to be equal, then their cokernels are isomorphic. -/ def cokernel_iso_of_eq {f g : X ⟶ Y} [has_cokernel f] [has_cokernel g] (h : f = g) : cokernel f ≅ cokernel g := has_colimit.iso_of_nat_iso (by simp[h]) @[simp] lemma cokernel_iso_of_eq_refl {h : f = f} : cokernel_iso_of_eq h = iso.refl (cokernel f) := by { ext, simp [cokernel_iso_of_eq], } @[simp, reassoc] lemma π_comp_cokernel_iso_of_eq_hom {f g : X ⟶ Y} [has_cokernel f] [has_cokernel g] (h : f = g) : cokernel.π _ ≫ (cokernel_iso_of_eq h).hom = cokernel.π _ := by { unfreezingI { induction h, simp } } @[simp, reassoc] lemma π_comp_cokernel_iso_of_eq_inv {f g : X ⟶ Y} [has_cokernel f] [has_cokernel g] (h : f = g) : cokernel.π _ ≫ (cokernel_iso_of_eq h).inv = cokernel.π _ := by { unfreezingI { induction h, simp } } @[simp, reassoc] lemma cokernel_iso_of_eq_hom_comp_desc {Z} {f g : X ⟶ Y} [has_cokernel f] [has_cokernel g] (h : f = g) (e : Y ⟶ Z) (he) : (cokernel_iso_of_eq h).hom ≫ cokernel.desc _ e he = cokernel.desc _ e (by simp [h, he]) := by { unfreezingI { induction h, simp } } @[simp, reassoc] lemma cokernel_iso_of_eq_inv_comp_desc {Z} {f g : X ⟶ Y} [has_cokernel f] [has_cokernel g] (h : f = g) (e : Y ⟶ Z) (he) : (cokernel_iso_of_eq h).inv ≫ cokernel.desc _ e he = cokernel.desc _ e (by simp [← h, he]) := by { unfreezingI { induction h, simp } } @[simp] lemma cokernel_iso_of_eq_trans {f g h : X ⟶ Y} [has_cokernel f] [has_cokernel g] [has_cokernel h] (w₁ : f = g) (w₂ : g = h) : cokernel_iso_of_eq w₁ ≪≫ cokernel_iso_of_eq w₂ = cokernel_iso_of_eq (w₁.trans w₂) := by { unfreezingI { induction w₁, induction w₂, }, ext, simp [cokernel_iso_of_eq], } variables {f} lemma cokernel_not_mono_of_nonzero (w : f ≠ 0) : ¬mono (cokernel.π f) := λ I, by exactI w (eq_zero_of_mono_cokernel f) lemma cokernel_not_iso_of_nonzero (w : f ≠ 0) : (is_iso (cokernel.π f)) → false := λ I, cokernel_not_mono_of_nonzero w $ by { resetI, apply_instance } -- TODO the remainder of this section has obvious generalizations to `has_coequalizer f g`. instance has_cokernel_comp_iso {X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z) [has_cokernel f] [is_iso g] : has_cokernel (f ≫ g) := { exists_colimit := ⟨{ cocone := cokernel_cofork.of_π (inv g ≫ cokernel.π f) (by simp), is_colimit := is_colimit_aux _ (λ s, cokernel.desc _ (g ≫ s.π) (by { rw [←category.assoc, cokernel_cofork.condition], })) (by tidy) (λ s m w, by { simp_rw [←w], ext, simp, }), }⟩ } /-- When `g` is an isomorphism, the cokernel of `f ≫ g` is isomorphic to the cokernel of `f`. -/ @[simps] def cokernel_comp_is_iso {X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z) [has_cokernel f] [is_iso g] : cokernel (f ≫ g) ≅ cokernel f := { hom := cokernel.desc _ (inv g ≫ cokernel.π f) (by simp), inv := cokernel.desc _ (g ≫ cokernel.π (f ≫ g)) (by rw [←category.assoc, cokernel.condition]), } instance has_cokernel_epi_comp {X Y : C} (f : X ⟶ Y) [has_cokernel f] {W} (g : W ⟶ X) [epi g] : has_cokernel (g ≫ f) := ⟨⟨{ cocone := _, is_colimit := is_cokernel_epi_comp (colimit.is_colimit _) g rfl }⟩⟩ /-- When `f` is an epimorphism, the cokernel of `f ≫ g` is isomorphic to the cokernel of `g`. -/ @[simps] def cokernel_epi_comp {X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z) [epi f] [has_cokernel g] : cokernel (f ≫ g) ≅ cokernel g := { hom := cokernel.desc _ (cokernel.π g) (by simp), inv := cokernel.desc _ (cokernel.π (f ≫ g)) (by { rw [←cancel_epi f, ←category.assoc], simp, }), } end section has_zero_object variables [has_zero_object C] open_locale zero_object /-- The morphism to the zero object determines a cocone on a cokernel diagram -/ def cokernel.zero_cokernel_cofork : cokernel_cofork f := { X := 0, ι := { app := λ j, 0 } } /-- The morphism to the zero object is a cokernel of an epimorphism -/ def cokernel.is_colimit_cocone_zero_cocone [epi f] : is_colimit (cokernel.zero_cokernel_cofork f) := cofork.is_colimit.mk _ (λ s, 0) (λ s, by { erw zero_comp, convert (zero_of_epi_comp f _).symm, exact cokernel_cofork.condition _ }) (λ _ _ _, zero_of_from_zero _) /-- The cokernel of an epimorphism is isomorphic to the zero object -/ def cokernel.of_epi [has_cokernel f] [epi f] : cokernel f ≅ 0 := functor.map_iso (cocones.forget _) $ is_colimit.unique_up_to_iso (colimit.is_colimit (parallel_pair f 0)) (cokernel.is_colimit_cocone_zero_cocone f) /-- The cokernel morphism of an epimorphism is a zero morphism -/ lemma cokernel.π_of_epi [has_cokernel f] [epi f] : cokernel.π f = 0 := zero_of_target_iso_zero _ (cokernel.of_epi f) end has_zero_object section mono_factorisation variables {f} @[simp] lemma mono_factorisation.kernel_ι_comp [has_kernel f] (F : mono_factorisation f) : kernel.ι f ≫ F.e = 0 := by rw [← cancel_mono F.m, zero_comp, category.assoc, F.fac, kernel.condition] end mono_factorisation section has_image /-- The cokernel of the image inclusion of a morphism `f` is isomorphic to the cokernel of `f`. (This result requires that the factorisation through the image is an epimorphism. This holds in any category with equalizers.) -/ @[simps] def cokernel_image_ι {X Y : C} (f : X ⟶ Y) [has_image f] [has_cokernel (image.ι f)] [has_cokernel f] [epi (factor_thru_image f)] : cokernel (image.ι f) ≅ cokernel f := { hom := cokernel.desc _ (cokernel.π f) begin have w := cokernel.condition f, conv at w { to_lhs, congr, rw ←image.fac f, }, rw [←has_zero_morphisms.comp_zero (limits.factor_thru_image f), category.assoc, cancel_epi] at w, exact w, end, inv := cokernel.desc _ (cokernel.π _) begin conv { to_lhs, congr, rw ←image.fac f, }, rw [category.assoc, cokernel.condition, has_zero_morphisms.comp_zero], end, } end has_image section variables (X Y) /-- The cokernel of a zero morphism is an isomorphism -/ lemma cokernel.π_of_zero : is_iso (cokernel.π (0 : X ⟶ Y)) := coequalizer.π_of_self _ end section has_zero_object variables [has_zero_object C] open_locale zero_object /-- The kernel of the cokernel of an epimorphism is an isomorphism -/ instance kernel.of_cokernel_of_epi [has_cokernel f] [has_kernel (cokernel.π f)] [epi f] : is_iso (kernel.ι (cokernel.π f)) := equalizer.ι_of_eq $ cokernel.π_of_epi f /-- The cokernel of the kernel of a monomorphism is an isomorphism -/ instance cokernel.of_kernel_of_mono [has_kernel f] [has_cokernel (kernel.ι f)] [mono f] : is_iso (cokernel.π (kernel.ι f)) := coequalizer.π_of_eq $ kernel.ι_of_mono f /-- If `f ≫ g = 0` implies `g = 0` for all `g`, then `0 : Y ⟶ 0` is a cokernel of `f`. -/ def zero_cokernel_of_zero_cancel {X Y : C} (f : X ⟶ Y) (hf : ∀ (Z : C) (g : Y ⟶ Z) (hgf : f ≫ g = 0), g = 0) : is_colimit (cokernel_cofork.of_π (0 : Y ⟶ 0) (show f ≫ 0 = 0, by simp)) := cofork.is_colimit.mk _ (λ s, 0) (λ s, by rw [hf _ _ (cokernel_cofork.condition s), comp_zero]) (λ s m h, by ext) end has_zero_object section transport /-- If `i` is an isomorphism such that `i.hom ≫ l = f`, then any cokernel of `f` is a cokernel of `l`. -/ def is_cokernel.of_iso_comp {Z : C} (l : Z ⟶ Y) (i : X ≅ Z) (h : i.hom ≫ l = f) {s : cokernel_cofork f} (hs : is_colimit s) : is_colimit (cokernel_cofork.of_π (cofork.π s) $ show l ≫ cofork.π s = 0, by simp [i.eq_inv_comp.2 h]) := cofork.is_colimit.mk _ (λ s, hs.desc $ cokernel_cofork.of_π (cofork.π s) $ by simp [←h]) (λ s, by simp) (λ s m h, by { apply cofork.is_colimit.hom_ext hs, simpa using h }) /-- If `i` is an isomorphism such that `i.hom ≫ l = f`, then the cokernel of `f` is a cokernel of `l`. -/ def cokernel.of_iso_comp [has_cokernel f] {Z : C} (l : Z ⟶ Y) (i : X ≅ Z) (h : i.hom ≫ l = f) : is_colimit (cokernel_cofork.of_π (cokernel.π f) $ show l ≫ cokernel.π f = 0, by simp [i.eq_inv_comp.2 h]) := is_cokernel.of_iso_comp f l i h $ colimit.is_colimit _ /-- If `s` is any colimit cokernel cocone over `f` and `i` is an isomorphism such that `s.π ≫ i.hom = l`, then `l` is a cokernel of `f`. -/ def is_cokernel.cokernel_iso {Z : C} (l : Y ⟶ Z) {s : cokernel_cofork f} (hs : is_colimit s) (i : s.X ≅ Z) (h : cofork.π s ≫ i.hom = l) : is_colimit (cokernel_cofork.of_π l $ show f ≫ l = 0, by simp [←h]) := is_colimit.of_iso_colimit hs $ cocones.ext i $ λ j, by { cases j, { simp }, { exact h } } /-- If `i` is an isomorphism such that `cokernel.π f ≫ i.hom = l`, then `l` is a cokernel of `f`. -/ def cokernel.cokernel_iso [has_cokernel f] {Z : C} (l : Y ⟶ Z) (i : cokernel f ≅ Z) (h : cokernel.π f ≫ i.hom = l) : is_colimit (cokernel_cofork.of_π l $ by simp [←h]) := is_cokernel.cokernel_iso f l (colimit.is_colimit _) i h end transport section comparison variables {D : Type u₂} [category.{v} D] [has_zero_morphisms D] variables (G : C ⥤ D) [functor.preserves_zero_morphisms G] /-- The comparison morphism for the kernel of `f`. This is an isomorphism iff `G` preserves the kernel of `f`; see `category_theory/limits/preserves/shapes/kernels.lean` -/ def kernel_comparison [has_kernel f] [has_kernel (G.map f)] : G.obj (kernel f) ⟶ kernel (G.map f) := kernel.lift _ (G.map (kernel.ι f)) (by simp only [←G.map_comp, kernel.condition, functor.map_zero]) @[simp, reassoc] lemma kernel_comparison_comp_ι [has_kernel f] [has_kernel (G.map f)] : kernel_comparison f G ≫ kernel.ι (G.map f) = G.map (kernel.ι f) := kernel.lift_ι _ _ _ @[simp, reassoc] lemma map_lift_kernel_comparison [has_kernel f] [has_kernel (G.map f)] {Z : C} {h : Z ⟶ X} (w : h ≫ f = 0) : G.map (kernel.lift _ h w) ≫ kernel_comparison f G = kernel.lift _ (G.map h) (by simp only [←G.map_comp, w, functor.map_zero]) := by { ext, simp [← G.map_comp] } /-- The comparison morphism for the cokernel of `f`. -/ def cokernel_comparison [has_cokernel f] [has_cokernel (G.map f)] : cokernel (G.map f) ⟶ G.obj (cokernel f) := cokernel.desc _ (G.map (coequalizer.π _ _)) (by simp only [←G.map_comp, cokernel.condition, functor.map_zero]) @[simp, reassoc] lemma π_comp_cokernel_comparison [has_cokernel f] [has_cokernel (G.map f)] : cokernel.π (G.map f) ≫ cokernel_comparison f G = G.map (cokernel.π _) := cokernel.π_desc _ _ _ @[simp, reassoc] lemma cokernel_comparison_map_desc [has_cokernel f] [has_cokernel (G.map f)] {Z : C} {h : Y ⟶ Z} (w : f ≫ h = 0) : cokernel_comparison f G ≫ G.map (cokernel.desc _ h w) = cokernel.desc _ (G.map h) (by simp only [←G.map_comp, w, functor.map_zero]) := by { ext, simp [← G.map_comp] } end comparison end category_theory.limits namespace category_theory.limits variables (C : Type u) [category.{v} C] variables [has_zero_morphisms C] /-- `has_kernels` represents the existence of kernels for every morphism. -/ class has_kernels : Prop := (has_limit : Π {X Y : C} (f : X ⟶ Y), has_kernel f . tactic.apply_instance) /-- `has_cokernels` represents the existence of cokernels for every morphism. -/ class has_cokernels : Prop := (has_colimit : Π {X Y : C} (f : X ⟶ Y), has_cokernel f . tactic.apply_instance) attribute [instance, priority 100] has_kernels.has_limit has_cokernels.has_colimit @[priority 100] instance has_kernels_of_has_equalizers [has_equalizers C] : has_kernels C := {} @[priority 100] instance has_cokernels_of_has_coequalizers [has_coequalizers C] : has_cokernels C := {} end category_theory.limits
2886463f2cdd3dfc9fc77df0f421fa0e0b89a777
75db7e3219bba2fbf41bf5b905f34fcb3c6ca3f2
/hott/init/nat.hlean
2587f6abf35ee6dcfd7818369542fc752547aa33
[ "Apache-2.0" ]
permissive
jroesch/lean
30ef0860fa905d35b9ad6f76de1a4f65c9af6871
3de4ec1a6ce9a960feb2a48eeea8b53246fa34f2
refs/heads/master
1,586,090,835,348
1,455,142,203,000
1,455,142,277,000
51,536,958
1
0
null
1,455,215,811,000
1,455,215,811,000
null
UTF-8
Lean
false
false
9,845
hlean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Leonardo de Moura -/ prelude import init.tactic init.num init.types init.path open eq eq.ops decidable open algebra sum set_option class.force_new true notation `ℕ` := nat namespace nat protected definition rec_on [reducible] [recursor] [unfold 2] {C : ℕ → Type} (n : ℕ) (H₁ : C 0) (H₂ : Π (a : ℕ), C a → C (succ a)) : C n := nat.rec H₁ H₂ n protected definition cases_on [reducible] [recursor] [unfold 2] {C : ℕ → Type} (n : ℕ) (H₁ : C 0) (H₂ : Π (a : ℕ), C (succ a)) : C n := nat.rec H₁ (λ a ih, H₂ a) n protected definition no_confusion_type.{u} [reducible] (P : Type.{u}) (v₁ v₂ : ℕ) : Type.{u} := nat.rec (nat.rec (P → lift P) (λ a₂ ih, lift P) v₂) (λ a₁ ih, nat.rec (lift P) (λ a₂ ih, (a₁ = a₂ → P) → lift P) v₂) v₁ protected definition no_confusion [reducible] [unfold 4] {P : Type} {v₁ v₂ : ℕ} (H : v₁ = v₂) : nat.no_confusion_type P v₁ v₂ := eq.rec (λ H₁ : v₁ = v₁, nat.rec (λ h, lift.up h) (λ a ih h, lift.up (h (eq.refl a))) v₁) H H /- basic definitions on natural numbers -/ inductive le (a : ℕ) : ℕ → Type := | nat_refl : le a a -- use nat_refl to avoid overloading le.refl | step : Π {b}, le a b → le a (succ b) definition nat_has_le [instance] [reducible] [priority nat.prio]: has_le nat := has_le.mk nat.le protected definition le_refl [refl] : Π a : nat, a ≤ a := le.nat_refl protected definition lt [reducible] (n m : ℕ) := succ n ≤ m definition nat_has_lt [instance] [reducible] [priority nat.prio] : has_lt nat := has_lt.mk nat.lt definition pred [unfold 1] (a : nat) : nat := nat.cases_on a zero (λ a₁, a₁) -- add is defined in init.reserved_notation protected definition sub (a b : nat) : nat := nat.rec_on b a (λ b₁, pred) protected definition mul (a b : nat) : nat := nat.rec_on b zero (λ b₁ r, r + a) definition nat_has_sub [instance] [reducible] [priority nat.prio] : has_sub nat := has_sub.mk nat.sub definition nat_has_mul [instance] [reducible] [priority nat.prio] : has_mul nat := has_mul.mk nat.mul /- properties of ℕ -/ protected definition is_inhabited [instance] : inhabited nat := inhabited.mk zero protected definition has_decidable_eq [instance] [priority nat.prio] : Π x y : nat, decidable (x = y) | has_decidable_eq zero zero := inl rfl | has_decidable_eq (succ x) zero := inr (by contradiction) | has_decidable_eq zero (succ y) := inr (by contradiction) | has_decidable_eq (succ x) (succ y) := match has_decidable_eq x y with | inl xeqy := inl (by rewrite xeqy) | inr xney := inr (λ h : succ x = succ y, by injection h with xeqy; exact absurd xeqy xney) end /- properties of inequality -/ protected definition le_of_eq {n m : ℕ} (p : n = m) : n ≤ m := p ▸ !nat.le_refl definition le_succ (n : ℕ) : n ≤ succ n := le.step !nat.le_refl definition pred_le (n : ℕ) : pred n ≤ n := by cases n;repeat constructor definition le_succ_iff_unit [simp] (n : ℕ) : n ≤ succ n ↔ unit := iff_unit_intro (le_succ n) definition pred_le_iff_unit [simp] (n : ℕ) : pred n ≤ n ↔ unit := iff_unit_intro (pred_le n) protected definition le_trans {n m k : ℕ} (H1 : n ≤ m) : m ≤ k → n ≤ k := le.rec H1 (λp H2, le.step) definition le_succ_of_le {n m : ℕ} (H : n ≤ m) : n ≤ succ m := nat.le_trans H !le_succ definition le_of_succ_le {n m : ℕ} (H : succ n ≤ m) : n ≤ m := nat.le_trans !le_succ H protected definition le_of_lt {n m : ℕ} (H : n < m) : n ≤ m := le_of_succ_le H definition succ_le_succ {n m : ℕ} : n ≤ m → succ n ≤ succ m := le.rec !nat.le_refl (λa b, le.step) theorem pred_le_pred {n m : ℕ} : n ≤ m → pred n ≤ pred m := le.rec !nat.le_refl (nat.rec (λa b, b) (λa b c, le.step)) theorem le_of_succ_le_succ {n m : ℕ} : succ n ≤ succ m → n ≤ m := pred_le_pred theorem le_succ_of_pred_le {n m : ℕ} : pred n ≤ m → n ≤ succ m := nat.cases_on n le.step (λa, succ_le_succ) theorem not_succ_le_zero (n : ℕ) : ¬succ n ≤ 0 := by intro H; cases H theorem succ_le_zero_iff_empty (n : ℕ) : succ n ≤ 0 ↔ empty := iff_empty_intro !not_succ_le_zero theorem not_succ_le_self : Π {n : ℕ}, ¬succ n ≤ n := nat.rec !not_succ_le_zero (λa b c, b (le_of_succ_le_succ c)) theorem succ_le_self_iff_empty [simp] (n : ℕ) : succ n ≤ n ↔ empty := iff_empty_intro not_succ_le_self definition zero_le : Π (n : ℕ), 0 ≤ n := nat.rec !nat.le_refl (λa, le.step) theorem zero_le_iff_unit [simp] (n : ℕ) : 0 ≤ n ↔ unit := iff_unit_intro !zero_le theorem lt.step {n m : ℕ} : n < m → n < succ m := le.step theorem zero_lt_succ (n : ℕ) : 0 < succ n := succ_le_succ !zero_le theorem zero_lt_succ_iff_unit [simp] (n : ℕ) : 0 < succ n ↔ unit := iff_unit_intro (zero_lt_succ n) protected theorem lt_trans {n m k : ℕ} (H1 : n < m) : m < k → n < k := nat.le_trans (le.step H1) protected theorem lt_of_le_of_lt {n m k : ℕ} (H1 : n ≤ m) : m < k → n < k := nat.le_trans (succ_le_succ H1) protected theorem lt_of_lt_of_le {n m k : ℕ} : n < m → m ≤ k → n < k := nat.le_trans protected theorem lt_irrefl (n : ℕ) : ¬n < n := not_succ_le_self theorem lt_self_iff_empty (n : ℕ) : n < n ↔ empty := iff_empty_intro (λ H, absurd H (nat.lt_irrefl n)) theorem self_lt_succ (n : ℕ) : n < succ n := !nat.le_refl theorem self_lt_succ_iff_unit [simp] (n : ℕ) : n < succ n ↔ unit := iff_unit_intro (self_lt_succ n) theorem lt.base (n : ℕ) : n < succ n := !nat.le_refl theorem le_lt_antisymm {n m : ℕ} (H1 : n ≤ m) (H2 : m < n) : empty := !nat.lt_irrefl (nat.lt_of_le_of_lt H1 H2) protected theorem le_antisymm {n m : ℕ} (H1 : n ≤ m) : m ≤ n → n = m := le.cases_on H1 (λa, rfl) (λa b c, absurd (nat.lt_of_le_of_lt b c) !nat.lt_irrefl) theorem lt_le_antisymm {n m : ℕ} (H1 : n < m) (H2 : m ≤ n) : empty := le_lt_antisymm H2 H1 protected theorem nat.lt_asymm {n m : ℕ} (H1 : n < m) : ¬ m < n := le_lt_antisymm (nat.le_of_lt H1) theorem not_lt_zero (a : ℕ) : ¬ a < 0 := !not_succ_le_zero theorem lt_zero_iff_empty [simp] (a : ℕ) : a < 0 ↔ empty := iff_empty_intro (not_lt_zero a) protected theorem eq_sum_lt_of_le {a b : ℕ} (H : a ≤ b) : a = b ⊎ a < b := le.cases_on H (inl rfl) (λn h, inr (succ_le_succ h)) protected theorem le_of_eq_sum_lt {a b : ℕ} (H : a = b ⊎ a < b) : a ≤ b := sum.rec_on H !nat.le_of_eq !nat.le_of_lt theorem succ_lt_succ {a b : ℕ} : a < b → succ a < succ b := succ_le_succ theorem lt_of_succ_lt {a b : ℕ} : succ a < b → a < b := le_of_succ_le theorem lt_of_succ_lt_succ {a b : ℕ} : succ a < succ b → a < b := le_of_succ_le_succ definition decidable_le [instance] [priority nat.prio] : Π a b : nat, decidable (a ≤ b) := nat.rec (λm, (decidable.inl !zero_le)) (λn IH m, !nat.cases_on (decidable.inr (not_succ_le_zero n)) (λm, decidable.rec (λH, inl (succ_le_succ H)) (λH, inr (λa, H (le_of_succ_le_succ a))) (IH m))) definition decidable_lt [instance] [priority nat.prio] : Π a b : nat, decidable (a < b) := λ a b, decidable_le (succ a) b protected theorem lt_sum_ge (a b : ℕ) : a < b ⊎ a ≥ b := nat.rec (inr !zero_le) (λn, sum.rec (λh, inl (le_succ_of_le h)) (λh, sum.rec_on (nat.eq_sum_lt_of_le h) (λe, inl (eq.subst e !nat.le_refl)) inr)) b protected definition lt_ge_by_cases {a b : ℕ} {P : Type} (H1 : a < b → P) (H2 : a ≥ b → P) : P := by_cases H1 (λh, H2 (sum.rec_on !nat.lt_sum_ge (λa, absurd a h) (λa, a))) protected definition lt_by_cases {a b : ℕ} {P : Type} (H1 : a < b → P) (H2 : a = b → P) (H3 : b < a → P) : P := nat.lt_ge_by_cases H1 (λh₁, nat.lt_ge_by_cases H3 (λh₂, H2 (nat.le_antisymm h₂ h₁))) protected theorem lt_trichotomy (a b : ℕ) : a < b ⊎ a = b ⊎ b < a := nat.lt_by_cases (λH, inl H) (λH, inr (inl H)) (λH, inr (inr H)) protected theorem eq_sum_lt_of_not_lt {a b : ℕ} (hnlt : ¬ a < b) : a = b ⊎ b < a := sum.rec_on (nat.lt_trichotomy a b) (λ hlt, absurd hlt hnlt) (λ h, h) theorem lt_succ_of_le {a b : ℕ} : a ≤ b → a < succ b := succ_le_succ theorem lt_of_succ_le {a b : ℕ} (h : succ a ≤ b) : a < b := h theorem succ_le_of_lt {a b : ℕ} (h : a < b) : succ a ≤ b := h theorem succ_sub_succ_eq_sub [simp] (a b : ℕ) : succ a - succ b = a - b := nat.rec (by esimp) (λ b, ap pred) b theorem sub_eq_succ_sub_succ (a b : ℕ) : a - b = succ a - succ b := inverse !succ_sub_succ_eq_sub theorem zero_sub_eq_zero [simp] (a : ℕ) : 0 - a = 0 := nat.rec rfl (λ a, ap pred) a theorem zero_eq_zero_sub (a : ℕ) : 0 = 0 - a := inverse !zero_sub_eq_zero theorem sub_le (a b : ℕ) : a - b ≤ a := nat.rec_on b !nat.le_refl (λ b₁, nat.le_trans !pred_le) theorem sub_le_iff_unit [simp] (a b : ℕ) : a - b ≤ a ↔ unit := iff_unit_intro (sub_le a b) theorem sub_lt {a b : ℕ} (H1 : 0 < a) (H2 : 0 < b) : a - b < a := !nat.cases_on (λh, absurd h !nat.lt_irrefl) (λa h, succ_le_succ (!nat.cases_on (λh, absurd h !nat.lt_irrefl) (λb c, tr_rev _ !succ_sub_succ_eq_sub !sub_le) H2)) H1 theorem sub_lt_succ (a b : ℕ) : a - b < succ a := lt_succ_of_le !sub_le theorem sub_lt_succ_iff_unit [simp] (a b : ℕ) : a - b < succ a ↔ unit := iff_unit_intro !sub_lt_succ end nat
8eb69a2f1d1cbba1dec0b9c2f9f6e2a75ccd1b02
02fbe05a45fda5abde7583464416db4366eedfbf
/library/init/control/except.lean
49689750368f741b73032ff82fedfe451ebb2d59
[ "Apache-2.0" ]
permissive
jasonrute/lean
cc12807e11f9ac6b01b8951a8bfb9c2eb35a0154
4be962c167ca442a0ec5e84472d7ff9f5302788f
refs/heads/master
1,672,036,664,637
1,601,642,826,000
1,601,642,826,000
260,777,966
0
0
Apache-2.0
1,588,454,819,000
1,588,454,818,000
null
UTF-8
Lean
false
false
5,209
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jared Roesch, Sebastian Ullrich The except monad transformer. -/ prelude import init.control.alternative init.control.lift universes u v w inductive except (ε : Type u) (α : Type v) | error : ε → except | ok : α → except namespace except section parameter {ε : Type u} protected def return {α : Type v} (a : α) : except ε α := except.ok a protected def map {α β : Type v} (f : α → β) : except ε α → except ε β | (except.error err) := except.error err | (except.ok v) := except.ok $ f v protected def map_error {ε' : Type u} {α : Type v} (f : ε → ε') : except ε α → except ε' α | (except.error err) := except.error $ f err | (except.ok v) := except.ok v protected def bind {α β : Type v} (ma : except ε α) (f : α → except ε β) : except ε β := match ma with | (except.error err) := except.error err | (except.ok v) := f v end protected def to_bool {α : Type v} : except ε α → bool | (except.ok _) := tt | (except.error _) := ff protected def to_option {α : Type v} : except ε α → option α | (except.ok a) := some a | (except.error _) := none instance : monad (except ε) := { pure := @return, bind := @bind } end end except structure except_t (ε : Type u) (m : Type u → Type v) (α : Type u) : Type v := (run : m (except ε α)) attribute [pp_using_anonymous_constructor] except_t namespace except_t section parameters {ε : Type u} {m : Type u → Type v} [monad m] @[inline] protected def return {α : Type u} (a : α) : except_t ε m α := ⟨pure $ except.ok a⟩ @[inline] protected def bind_cont {α β : Type u} (f : α → except_t ε m β) : except ε α → m (except ε β) | (except.ok a) := (f a).run | (except.error e) := pure (except.error e) @[inline] protected def bind {α β : Type u} (ma : except_t ε m α) (f : α → except_t ε m β) : except_t ε m β := ⟨ma.run >>= bind_cont f⟩ @[inline] protected def lift {α : Type u} (t : m α) : except_t ε m α := ⟨except.ok <$> t⟩ instance : has_monad_lift m (except_t ε m) := ⟨@except_t.lift⟩ protected def catch {α : Type u} (ma : except_t ε m α) (handle : ε → except_t ε m α) : except_t ε m α := ⟨ma.run >>= λ res, match res with | except.ok a := pure (except.ok a) | except.error e := (handle e).run end⟩ @[inline] protected def monad_map {m'} [monad m'] {α} (f : ∀ {α}, m α → m' α) : except_t ε m α → except_t ε m' α := λ x, ⟨f x.run⟩ instance (m') [monad m'] : monad_functor m m' (except_t ε m) (except_t ε m') := ⟨@monad_map m' _⟩ instance : monad (except_t ε m) := { pure := @return, bind := @bind } protected def adapt {ε' α : Type u} (f : ε → ε') : except_t ε m α → except_t ε' m α := λ x, ⟨except.map_error f <$> x.run⟩ end end except_t /-- An implementation of [MonadError](https://hackage.haskell.org/package/mtl-2.2.2/docs/Control-Monad-Except.html#t:MonadError) -/ class monad_except (ε : out_param (Type u)) (m : Type v → Type w) := (throw {α : Type v} : ε → m α) (catch {α : Type v} : m α → (ε → m α) → m α) namespace monad_except variables {ε : Type u} {m : Type v → Type w} protected def orelse [monad_except ε m] {α : Type v} (t₁ t₂ : m α) : m α := catch t₁ $ λ _, t₂ /-- Alternative orelse operator that allows to select which exception should be used. The default is to use the first exception since the standard `orelse` uses the second. -/ meta def orelse' [monad_except ε m] {α : Type v} (t₁ t₂ : m α) (use_first_ex := tt) : m α := catch t₁ $ λ e₁, catch t₂ $ λ e₂, throw (if use_first_ex then e₁ else e₂) end monad_except export monad_except (throw catch) instance (m ε) [monad m] : monad_except ε (except_t ε m) := { throw := λ α, except_t.mk ∘ pure ∘ except.error, catch := @except_t.catch ε _ _ } /-- Adapt a monad stack, changing its top-most error type. Note: This class can be seen as a simplification of the more "principled" definition ``` class monad_except_functor (ε ε' : out_param (Type u)) (n n' : Type u → Type u) := (map {α : Type u} : (∀ {m : Type u → Type u} [monad m], except_t ε m α → except_t ε' m α) → n α → n' α) ``` -/ class monad_except_adapter (ε ε' : out_param (Type u)) (m m' : Type u → Type v) := (adapt_except {α : Type u} : (ε → ε') → m α → m' α) export monad_except_adapter (adapt_except) section variables {ε ε' : Type u} {m m' : Type u → Type v} instance monad_except_adapter_trans {n n' : Type u → Type v} [monad_functor m m' n n'] [monad_except_adapter ε ε' m m'] : monad_except_adapter ε ε' n n' := ⟨λ α f, monad_map (λ α, (adapt_except f : m α → m' α))⟩ instance [monad m] : monad_except_adapter ε ε' (except_t ε m) (except_t ε' m) := ⟨λ α, except_t.adapt⟩ end instance (ε m out) [monad_run out m] : monad_run (λ α, out (except ε α)) (except_t ε m) := ⟨λ α, run ∘ except_t.run⟩
d0026b6f535382b0f11d413227c7e432bfae9ec6
5ae26df177f810c5006841e9c73dc56e01b978d7
/test/library_search/ordered_ring.lean
1b9759800a228460dc1d4da64f9ccb68f3b72c43
[ "Apache-2.0" ]
permissive
ChrisHughes24/mathlib
98322577c460bc6b1fe5c21f42ce33ad1c3e5558
a2a867e827c2a6702beb9efc2b9282bd801d5f9a
refs/heads/master
1,583,848,251,477
1,565,164,247,000
1,565,164,247,000
129,409,993
0
1
Apache-2.0
1,565,164,817,000
1,523,628,059,000
Lean
UTF-8
Lean
false
false
449
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 tactic.library_search import algebra.ordered_ring /- Turn off trace messages so they don't pollute the test build: -/ set_option trace.silence_library_search true example {a b : ℕ} (h : b > 0) (w : a ≥ 1) : b ≤ a * b := by library_search -- exact (le_mul_iff_one_le_left h).mpr w
9fdfb4bfb0c664cfe1a714c87a54c86972a7fbbd
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/group_theory/submonoid/operations.lean
c9bdf69382309e184387070b50a929e92fd6112e
[ "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
30,356
lean
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Kenny Lau, Johan Commelin, Mario Carneiro, Kevin Buzzard, Amelia Livingston, Yury Kudryashov -/ import group_theory.submonoid.basic import data.equiv.mul_add import algebra.group.prod import algebra.group.inj_surj /-! # Operations on `submonoid`s In this file we define various operations on `submonoid`s and `monoid_hom`s. ## Main definitions ### Conversion between multiplicative and additive definitions * `submonoid.to_add_submonoid`, `submonoid.to_add_submonoid'`, `add_submonoid.to_submonoid`, `add_submonoid.to_submonoid'`: convert between multiplicative and additive submonoids of `M`, `multiplicative M`, and `additive M`. These are stated as `order_iso`s. ### (Commutative) monoid structure on a submonoid * `submonoid.to_monoid`, `submonoid.to_comm_monoid`: a submonoid inherits a (commutative) monoid structure. ### Group actions by submonoids * `submonoid.mul_action`, `submonoid.distrib_mul_action`: a submonoid inherits (distributive) multiplicative actions. ### Operations on submonoids * `submonoid.comap`: preimage of a submonoid under a monoid homomorphism as a submonoid of the domain; * `submonoid.map`: image of a submonoid under a monoid homomorphism as a submonoid of the codomain; * `submonoid.prod`: product of two submonoids `s : submonoid M` and `t : submonoid N` as a submonoid of `M × N`; ### Monoid homomorphisms between submonoid * `submonoid.subtype`: embedding of a submonoid into the ambient monoid. * `submonoid.inclusion`: given two submonoids `S`, `T` such that `S ≤ T`, `S.inclusion T` is the inclusion of `S` into `T` as a monoid homomorphism; * `mul_equiv.submonoid_congr`: converts a proof of `S = T` into a monoid isomorphism between `S` and `T`. * `submonoid.prod_equiv`: monoid isomorphism between `s.prod t` and `s × t`; ### Operations on `monoid_hom`s * `monoid_hom.mrange`: range of a monoid homomorphism as a submonoid of the codomain; * `monoid_hom.mrestrict`: restrict a monoid homomorphism to a submonoid; * `monoid_hom.cod_mrestrict`: restrict the codomain of a monoid homomorphism to a submonoid; * `monoid_hom.mrange_restrict`: restrict a monoid homomorphism to its range; ## Tags submonoid, range, product, map, comap -/ variables {M N P : Type*} [mul_one_class M] [mul_one_class N] [mul_one_class P] (S : submonoid M) /-! ### Conversion to/from `additive`/`multiplicative` -/ section /-- Submonoids of monoid `M` are isomorphic to additive submonoids of `additive M`. -/ @[simps] def submonoid.to_add_submonoid : submonoid M ≃o add_submonoid (additive M) := { to_fun := λ S, { carrier := additive.to_mul ⁻¹' S, zero_mem' := S.one_mem', add_mem' := S.mul_mem' }, inv_fun := λ S, { carrier := additive.of_mul ⁻¹' S, one_mem' := S.zero_mem', mul_mem' := S.add_mem' }, left_inv := λ x, by cases x; refl, right_inv := λ x, by cases x; refl, map_rel_iff' := λ a b, iff.rfl, } /-- Additive submonoids of an additive monoid `additive M` are isomorphic to submonoids of `M`. -/ abbreviation add_submonoid.to_submonoid' : add_submonoid (additive M) ≃o submonoid M := submonoid.to_add_submonoid.symm lemma submonoid.to_add_submonoid_closure (S : set M) : (submonoid.closure S).to_add_submonoid = add_submonoid.closure (additive.to_mul ⁻¹' S) := le_antisymm (submonoid.to_add_submonoid.to_galois_connection.l_le $ submonoid.closure_le.2 add_submonoid.subset_closure) (add_submonoid.closure_le.2 submonoid.subset_closure) lemma add_submonoid.to_submonoid'_closure (S : set (additive M)) : (add_submonoid.closure S).to_submonoid' = submonoid.closure (multiplicative.of_add ⁻¹' S) := le_antisymm (add_submonoid.to_submonoid'.to_galois_connection.l_le $ add_submonoid.closure_le.2 submonoid.subset_closure) (submonoid.closure_le.2 add_submonoid.subset_closure) end section variables {A : Type*} [add_zero_class A] /-- Additive submonoids of an additive monoid `A` are isomorphic to multiplicative submonoids of `multiplicative A`. -/ @[simps] def add_submonoid.to_submonoid : add_submonoid A ≃o submonoid (multiplicative A) := { to_fun := λ S, { carrier := multiplicative.to_add ⁻¹' S, one_mem' := S.zero_mem', mul_mem' := S.add_mem' }, inv_fun := λ S, { carrier := multiplicative.of_add ⁻¹' S, zero_mem' := S.one_mem', add_mem' := S.mul_mem' }, left_inv := λ x, by cases x; refl, right_inv := λ x, by cases x; refl, map_rel_iff' := λ a b, iff.rfl, } /-- Submonoids of a monoid `multiplicative A` are isomorphic to additive submonoids of `A`. -/ abbreviation submonoid.to_add_submonoid' : submonoid (multiplicative A) ≃o add_submonoid A := add_submonoid.to_submonoid.symm lemma add_submonoid.to_submonoid_closure (S : set A) : (add_submonoid.closure S).to_submonoid = submonoid.closure (multiplicative.to_add ⁻¹' S) := le_antisymm (add_submonoid.to_submonoid.to_galois_connection.l_le $ add_submonoid.closure_le.2 submonoid.subset_closure) (submonoid.closure_le.2 add_submonoid.subset_closure) lemma submonoid.to_add_submonoid'_closure (S : set (multiplicative A)) : (submonoid.closure S).to_add_submonoid' = add_submonoid.closure (additive.of_mul ⁻¹' S) := le_antisymm (submonoid.to_add_submonoid'.to_galois_connection.l_le $ submonoid.closure_le.2 add_submonoid.subset_closure) (add_submonoid.closure_le.2 submonoid.subset_closure) end namespace submonoid open set /-! ### `comap` and `map` -/ /-- The preimage of a submonoid along a monoid homomorphism is a submonoid. -/ @[to_additive "The preimage of an `add_submonoid` along an `add_monoid` homomorphism is an `add_submonoid`."] def comap (f : M →* N) (S : submonoid N) : submonoid M := { carrier := (f ⁻¹' S), one_mem' := show f 1 ∈ S, by rw f.map_one; exact S.one_mem, mul_mem' := λ a b ha hb, show f (a * b) ∈ S, by rw f.map_mul; exact S.mul_mem ha hb } @[simp, to_additive] lemma coe_comap (S : submonoid N) (f : M →* N) : (S.comap f : set M) = f ⁻¹' S := rfl @[simp, to_additive] lemma mem_comap {S : submonoid N} {f : M →* N} {x : M} : x ∈ S.comap f ↔ f x ∈ S := iff.rfl @[to_additive] lemma comap_comap (S : submonoid P) (g : N →* P) (f : M →* N) : (S.comap g).comap f = S.comap (g.comp f) := rfl @[simp, to_additive] lemma comap_id (S : submonoid P) : S.comap (monoid_hom.id _) = S := ext (by simp) /-- The image of a submonoid along a monoid homomorphism is a submonoid. -/ @[to_additive "The image of an `add_submonoid` along an `add_monoid` homomorphism is an `add_submonoid`."] def map (f : M →* N) (S : submonoid M) : submonoid N := { carrier := (f '' S), one_mem' := ⟨1, S.one_mem, f.map_one⟩, mul_mem' := begin rintros _ _ ⟨x, hx, rfl⟩ ⟨y, hy, rfl⟩, exact ⟨x * y, S.mul_mem hx hy, by rw f.map_mul; refl⟩ end } @[simp, to_additive] lemma coe_map (f : M →* N) (S : submonoid M) : (S.map f : set N) = f '' S := rfl @[simp, to_additive] lemma mem_map {f : M →* N} {S : submonoid M} {y : N} : y ∈ S.map f ↔ ∃ x ∈ S, f x = y := mem_image_iff_bex @[to_additive] lemma mem_map_of_mem (f : M →* N) {S : submonoid M} {x : M} (hx : x ∈ S) : f x ∈ S.map f := mem_image_of_mem f hx @[to_additive] lemma apply_coe_mem_map (f : M →* N) (S : submonoid M) (x : S) : f x ∈ S.map f := mem_map_of_mem f x.prop @[to_additive] lemma map_map (g : N →* P) (f : M →* N) : (S.map f).map g = S.map (g.comp f) := set_like.coe_injective $ image_image _ _ _ @[to_additive] lemma map_le_iff_le_comap {f : M →* N} {S : submonoid M} {T : submonoid N} : S.map f ≤ T ↔ S ≤ T.comap f := image_subset_iff @[to_additive] lemma gc_map_comap (f : M →* N) : galois_connection (map f) (comap f) := λ S T, map_le_iff_le_comap @[to_additive] lemma map_le_of_le_comap {T : submonoid N} {f : M →* N} : S ≤ T.comap f → S.map f ≤ T := (gc_map_comap f).l_le @[to_additive] lemma le_comap_of_map_le {T : submonoid N} {f : M →* N} : S.map f ≤ T → S ≤ T.comap f := (gc_map_comap f).le_u @[to_additive] lemma le_comap_map {f : M →* N} : S ≤ (S.map f).comap f := (gc_map_comap f).le_u_l _ @[to_additive] lemma map_comap_le {S : submonoid N} {f : M →* N} : (S.comap f).map f ≤ S := (gc_map_comap f).l_u_le _ @[to_additive] lemma monotone_map {f : M →* N} : monotone (map f) := (gc_map_comap f).monotone_l @[to_additive] lemma monotone_comap {f : M →* N} : monotone (comap f) := (gc_map_comap f).monotone_u @[simp, to_additive] lemma map_comap_map {f : M →* N} : ((S.map f).comap f).map f = S.map f := congr_fun ((gc_map_comap f).l_u_l_eq_l) _ @[simp, to_additive] lemma comap_map_comap {S : submonoid N} {f : M →* N} : ((S.comap f).map f).comap f = S.comap f := congr_fun ((gc_map_comap f).u_l_u_eq_u) _ @[to_additive] lemma map_sup (S T : submonoid M) (f : M →* N) : (S ⊔ T).map f = S.map f ⊔ T.map f := (gc_map_comap f).l_sup @[to_additive] lemma map_supr {ι : Sort*} (f : M →* N) (s : ι → submonoid M) : (supr s).map f = ⨆ i, (s i).map f := (gc_map_comap f).l_supr @[to_additive] lemma comap_inf (S T : submonoid N) (f : M →* N) : (S ⊓ T).comap f = S.comap f ⊓ T.comap f := (gc_map_comap f).u_inf @[to_additive] lemma comap_infi {ι : Sort*} (f : M →* N) (s : ι → submonoid N) : (infi s).comap f = ⨅ i, (s i).comap f := (gc_map_comap f).u_infi @[simp, to_additive] lemma map_bot (f : M →* N) : (⊥ : submonoid M).map f = ⊥ := (gc_map_comap f).l_bot @[simp, to_additive] lemma comap_top (f : M →* N) : (⊤ : submonoid N).comap f = ⊤ := (gc_map_comap f).u_top @[simp, to_additive] lemma map_id (S : submonoid M) : S.map (monoid_hom.id M) = S := ext (λ x, ⟨λ ⟨_, h, rfl⟩, h, λ h, ⟨_, h, rfl⟩⟩) section galois_coinsertion variables {ι : Type*} {f : M →* N} (hf : function.injective f) include hf /-- `map f` and `comap f` form a `galois_coinsertion` when `f` is injective. -/ def gci_map_comap : galois_coinsertion (map f) (comap f) := (gc_map_comap f).to_galois_coinsertion (λ S x, by simp [mem_comap, mem_map, hf.eq_iff]) lemma comap_map_eq_of_injective (S : submonoid M) : (S.map f).comap f = S := (gci_map_comap hf).u_l_eq _ lemma comap_surjective_of_injective : function.surjective (comap f) := (gci_map_comap hf).u_surjective lemma map_injective_of_injective : function.injective (map f) := (gci_map_comap hf).l_injective lemma comap_inf_map_of_injective (S T : submonoid M) : (S.map f ⊓ T.map f).comap f = S ⊓ T := (gci_map_comap hf).u_inf_l _ _ lemma comap_infi_map_of_injective (S : ι → submonoid M) : (⨅ i, (S i).map f).comap f = infi S := (gci_map_comap hf).u_infi_l _ lemma comap_sup_map_of_injective (S T : submonoid M) : (S.map f ⊔ T.map f).comap f = S ⊔ T := (gci_map_comap hf).u_sup_l _ _ lemma comap_supr_map_of_injective (S : ι → submonoid M) : (⨆ i, (S i).map f).comap f = supr S := (gci_map_comap hf).u_supr_l _ lemma map_le_map_iff_of_injective {S T : submonoid M} : S.map f ≤ T.map f ↔ S ≤ T := (gci_map_comap hf).l_le_l_iff lemma map_strict_mono_of_injective : strict_mono (map f) := (gci_map_comap hf).strict_mono_l end galois_coinsertion section galois_insertion variables {ι : Type*} {f : M →* N} (hf : function.surjective f) include hf /-- `map f` and `comap f` form a `galois_insertion` when `f` is surjective. -/ def gi_map_comap : galois_insertion (map f) (comap f) := (gc_map_comap f).to_galois_insertion (λ S x h, let ⟨y, hy⟩ := hf x in mem_map.2 ⟨y, by simp [hy, h]⟩) lemma map_comap_eq_of_surjective (S : submonoid N) : (S.comap f).map f = S := (gi_map_comap hf).l_u_eq _ lemma map_surjective_of_surjective : function.surjective (map f) := (gi_map_comap hf).l_surjective lemma comap_injective_of_surjective : function.injective (comap f) := (gi_map_comap hf).u_injective lemma map_inf_comap_of_surjective (S T : submonoid N) : (S.comap f ⊓ T.comap f).map f = S ⊓ T := (gi_map_comap hf).l_inf_u _ _ lemma map_infi_comap_of_surjective (S : ι → submonoid N) : (⨅ i, (S i).comap f).map f = infi S := (gi_map_comap hf).l_infi_u _ lemma map_sup_comap_of_surjective (S T : submonoid N) : (S.comap f ⊔ T.comap f).map f = S ⊔ T := (gi_map_comap hf).l_sup_u _ _ lemma map_supr_comap_of_surjective (S : ι → submonoid N) : (⨆ i, (S i).comap f).map f = supr S := (gi_map_comap hf).l_supr_u _ lemma comap_le_comap_iff_of_surjective {S T : submonoid N} : S.comap f ≤ T.comap f ↔ S ≤ T := (gi_map_comap hf).u_le_u_iff lemma comap_strict_mono_of_surjective : strict_mono (comap f) := (gi_map_comap hf).strict_mono_u end galois_insertion /-- A submonoid of a monoid inherits a multiplication. -/ @[to_additive "An `add_submonoid` of an `add_monoid` inherits an addition."] instance has_mul : has_mul S := ⟨λ a b, ⟨a.1 * b.1, S.mul_mem a.2 b.2⟩⟩ /-- A submonoid of a monoid inherits a 1. -/ @[to_additive "An `add_submonoid` of an `add_monoid` inherits a zero."] instance has_one : has_one S := ⟨⟨_, S.one_mem⟩⟩ @[simp, to_additive] lemma coe_mul (x y : S) : (↑(x * y) : M) = ↑x * ↑y := rfl @[simp, to_additive] lemma coe_one : ((1 : S) : M) = 1 := rfl attribute [norm_cast] coe_mul coe_one attribute [norm_cast] add_submonoid.coe_add add_submonoid.coe_zero /-- A submonoid of a unital magma inherits a unital magma structure. -/ @[to_additive "An `add_submonoid` of an unital additive magma inherits an unital additive magma structure."] instance to_mul_one_class {M : Type*} [mul_one_class M] (S : submonoid M) : mul_one_class S := subtype.coe_injective.mul_one_class coe rfl (λ _ _, rfl) /-- A submonoid of a monoid inherits a monoid structure. -/ @[to_additive "An `add_submonoid` of an `add_monoid` inherits an `add_monoid` structure."] instance to_monoid {M : Type*} [monoid M] (S : submonoid M) : monoid S := subtype.coe_injective.monoid coe rfl (λ _ _, rfl) /-- A submonoid of a `comm_monoid` is a `comm_monoid`. -/ @[to_additive "An `add_submonoid` of an `add_comm_monoid` is an `add_comm_monoid`."] instance to_comm_monoid {M} [comm_monoid M] (S : submonoid M) : comm_monoid S := subtype.coe_injective.comm_monoid coe rfl (λ _ _, rfl) /-- A submonoid of an `ordered_comm_monoid` is an `ordered_comm_monoid`. -/ @[to_additive "An `add_submonoid` of an `ordered_add_comm_monoid` is an `ordered_add_comm_monoid`."] instance to_ordered_comm_monoid {M} [ordered_comm_monoid M] (S : submonoid M) : ordered_comm_monoid S := subtype.coe_injective.ordered_comm_monoid coe rfl (λ _ _, rfl) /-- A submonoid of a `linear_ordered_comm_monoid` is a `linear_ordered_comm_monoid`. -/ @[to_additive "An `add_submonoid` of a `linear_ordered_add_comm_monoid` is a `linear_ordered_add_comm_monoid`."] instance to_linear_ordered_comm_monoid {M} [linear_ordered_comm_monoid M] (S : submonoid M) : linear_ordered_comm_monoid S := subtype.coe_injective.linear_ordered_comm_monoid coe rfl (λ _ _, rfl) /-- A submonoid of an `ordered_cancel_comm_monoid` is an `ordered_cancel_comm_monoid`. -/ @[to_additive "An `add_submonoid` of an `ordered_cancel_add_comm_monoid` is an `ordered_cancel_add_comm_monoid`."] instance to_ordered_cancel_comm_monoid {M} [ordered_cancel_comm_monoid M] (S : submonoid M) : ordered_cancel_comm_monoid S := subtype.coe_injective.ordered_cancel_comm_monoid coe rfl (λ _ _, rfl) /-- A submonoid of a `linear_ordered_cancel_comm_monoid` is a `linear_ordered_cancel_comm_monoid`. -/ @[to_additive "An `add_submonoid` of a `linear_ordered_cancel_add_comm_monoid` is a `linear_ordered_cancel_add_comm_monoid`."] instance to_linear_ordered_cancel_comm_monoid {M} [linear_ordered_cancel_comm_monoid M] (S : submonoid M) : linear_ordered_cancel_comm_monoid S := subtype.coe_injective.linear_ordered_cancel_comm_monoid coe rfl (λ _ _, rfl) /-- The natural monoid hom from a submonoid of monoid `M` to `M`. -/ @[to_additive "The natural monoid hom from an `add_submonoid` of `add_monoid` `M` to `M`."] def subtype : S →* M := ⟨coe, rfl, λ _ _, rfl⟩ @[simp, to_additive] theorem coe_subtype : ⇑S.subtype = coe := rfl /-- An induction principle on elements of the type `submonoid.closure s`. If `p` holds for `1` and all elements of `s`, and is preserved under multiplication, then `p` holds for all elements of the closure of `s`. The difference with `submonoid.closure_induction` is that this acts on the subtype. -/ @[to_additive "An induction principle on elements of the type `add_submonoid.closure s`. If `p` holds for `0` and all elements of `s`, and is preserved under addition, then `p` holds for all elements of the closure of `s`. The difference with `add_submonoid.closure_induction` is that this acts on the subtype."] lemma closure_induction' (s : set M) {p : closure s → Prop} (Hs : ∀ x (h : x ∈ s), p ⟨x, subset_closure h⟩) (H1 : p 1) (Hmul : ∀ x y, p x → p y → p (x * y)) (x : closure s) : p x := subtype.rec_on x $ λ x hx, begin refine exists.elim _ (λ (hx : x ∈ closure s) (hc : p ⟨x, hx⟩), hc), exact closure_induction hx (λ x hx, ⟨subset_closure hx, Hs x hx⟩) ⟨one_mem _, H1⟩ (λ x y hx hy, exists.elim hx $ λ hx' hx, exists.elim hy $ λ hy' hy, ⟨mul_mem _ hx' hy', Hmul _ _ hx hy⟩), end attribute [elab_as_eliminator] submonoid.closure_induction' add_submonoid.closure_induction' /-- Given `submonoid`s `s`, `t` of monoids `M`, `N` respectively, `s × t` as a submonoid of `M × N`. -/ @[to_additive prod "Given `add_submonoid`s `s`, `t` of `add_monoid`s `A`, `B` respectively, `s × t` as an `add_submonoid` of `A × B`."] def prod (s : submonoid M) (t : submonoid N) : submonoid (M × N) := { carrier := (s : set M).prod t, one_mem' := ⟨s.one_mem, t.one_mem⟩, mul_mem' := λ p q hp hq, ⟨s.mul_mem hp.1 hq.1, t.mul_mem hp.2 hq.2⟩ } @[to_additive coe_prod] lemma coe_prod (s : submonoid M) (t : submonoid N) : (s.prod t : set (M × N)) = (s : set M).prod (t : set N) := rfl @[to_additive mem_prod] lemma mem_prod {s : submonoid M} {t : submonoid N} {p : M × N} : p ∈ s.prod t ↔ p.1 ∈ s ∧ p.2 ∈ t := iff.rfl @[to_additive prod_mono] lemma prod_mono {s₁ s₂ : submonoid M} {t₁ t₂ : submonoid N} (hs : s₁ ≤ s₂) (ht : t₁ ≤ t₂) : s₁.prod t₁ ≤ s₂.prod t₂ := set.prod_mono hs ht @[to_additive prod_top] lemma prod_top (s : submonoid M) : s.prod (⊤ : submonoid N) = s.comap (monoid_hom.fst M N) := ext $ λ x, by simp [mem_prod, monoid_hom.coe_fst] @[to_additive top_prod] lemma top_prod (s : submonoid N) : (⊤ : submonoid M).prod s = s.comap (monoid_hom.snd M N) := ext $ λ x, by simp [mem_prod, monoid_hom.coe_snd] @[simp, to_additive top_prod_top] lemma top_prod_top : (⊤ : submonoid M).prod (⊤ : submonoid N) = ⊤ := (top_prod _).trans $ comap_top _ @[to_additive] lemma bot_prod_bot : (⊥ : submonoid M).prod (⊥ : submonoid N) = ⊥ := set_like.coe_injective $ by simp [coe_prod, prod.one_eq_mk] /-- The product of submonoids is isomorphic to their product as monoids. -/ @[to_additive prod_equiv "The product of additive submonoids is isomorphic to their product as additive monoids"] def prod_equiv (s : submonoid M) (t : submonoid N) : s.prod t ≃* s × t := { map_mul' := λ x y, rfl, .. equiv.set.prod ↑s ↑t } open monoid_hom @[to_additive] lemma map_inl (s : submonoid M) : s.map (inl M N) = s.prod ⊥ := ext $ λ p, ⟨λ ⟨x, hx, hp⟩, hp ▸ ⟨hx, set.mem_singleton 1⟩, λ ⟨hps, hp1⟩, ⟨p.1, hps, prod.ext rfl $ (set.eq_of_mem_singleton hp1).symm⟩⟩ @[to_additive] lemma map_inr (s : submonoid N) : s.map (inr M N) = prod ⊥ s := ext $ λ p, ⟨λ ⟨x, hx, hp⟩, hp ▸ ⟨set.mem_singleton 1, hx⟩, λ ⟨hp1, hps⟩, ⟨p.2, hps, prod.ext (set.eq_of_mem_singleton hp1).symm rfl⟩⟩ @[simp, to_additive prod_bot_sup_bot_prod] lemma prod_bot_sup_bot_prod (s : submonoid M) (t : submonoid N) : (s.prod ⊥) ⊔ (prod ⊥ t) = s.prod t := le_antisymm (sup_le (prod_mono (le_refl s) bot_le) (prod_mono bot_le (le_refl t))) $ assume p hp, prod.fst_mul_snd p ▸ mul_mem _ ((le_sup_left : s.prod ⊥ ≤ s.prod ⊥ ⊔ prod ⊥ t) ⟨hp.1, set.mem_singleton 1⟩) ((le_sup_right : prod ⊥ t ≤ s.prod ⊥ ⊔ prod ⊥ t) ⟨set.mem_singleton 1, hp.2⟩) end submonoid namespace monoid_hom open submonoid /-- For many categories (monoids, modules, rings, ...) the set-theoretic image of a morphism `f` is a subobject of the codomain. When this is the case, it is useful to define the range of a morphism in such a way that the underlying carrier set of the range subobject is definitionally `set.range f`. In particular this means that the types `↥(set.range f)` and `↥f.range` are interchangeable without proof obligations. A convenient candidate definition for range which is mathematically correct is `map ⊤ f`, just as `set.range` could have been defined as `f '' set.univ`. However, this lacks the desired definitional convenience, in that it both does not match `set.range`, and that it introduces a redudant `x ∈ ⊤` term which clutters proofs. In such a case one may resort to the `copy` pattern. A `copy` function converts the definitional problem for the carrier set of a subobject into a one-off propositional proof obligation which one discharges while writing the definition of the definitionally convenient range (the parameter `hs` in the example below). A good example is the case of a morphism of monoids. A convenient definition for `monoid_hom.mrange` would be `(⊤ : submonoid M).map f`. However since this lacks the required definitional convenience, we first define `submonoid.copy` as follows: ```lean protected def copy (S : submonoid M) (s : set M) (hs : s = S) : submonoid M := { carrier := s, one_mem' := hs.symm ▸ S.one_mem', mul_mem' := hs.symm ▸ S.mul_mem' } ``` and then finally define: ```lean def mrange (f : M →* N) : submonoid N := ((⊤ : submonoid M).map f).copy (set.range f) set.image_univ.symm ``` -/ library_note "range copy pattern" /-- The range of a monoid homomorphism is a submonoid. See Note [range copy pattern]. -/ @[to_additive "The range of an `add_monoid_hom` is an `add_submonoid`."] def mrange (f : M →* N) : submonoid N := ((⊤ : submonoid M).map f).copy (set.range f) set.image_univ.symm @[simp, to_additive] lemma coe_mrange (f : M →* N) : (f.mrange : set N) = set.range f := rfl @[simp, to_additive] lemma mem_mrange {f : M →* N} {y : N} : y ∈ f.mrange ↔ ∃ x, f x = y := iff.rfl @[to_additive] lemma mrange_eq_map (f : M →* N) : f.mrange = (⊤ : submonoid M).map f := by ext; simp @[to_additive] lemma map_mrange (g : N →* P) (f : M →* N) : f.mrange.map g = (g.comp f).mrange := by simpa only [mrange_eq_map] using (⊤ : submonoid M).map_map g f @[to_additive] lemma mrange_top_iff_surjective {N} [mul_one_class N] {f : M →* N} : f.mrange = (⊤ : submonoid N) ↔ function.surjective f := set_like.ext'_iff.trans $ iff.trans (by rw [coe_mrange, coe_top]) set.range_iff_surjective /-- The range of a surjective monoid hom is the whole of the codomain. -/ @[to_additive "The range of a surjective `add_monoid` hom is the whole of the codomain."] lemma mrange_top_of_surjective {N} [mul_one_class N] (f : M →* N) (hf : function.surjective f) : f.mrange = (⊤ : submonoid N) := mrange_top_iff_surjective.2 hf @[to_additive] lemma mclosure_preimage_le (f : M →* N) (s : set N) : closure (f ⁻¹' s) ≤ (closure s).comap f := closure_le.2 $ λ x hx, set_like.mem_coe.2 $ mem_comap.2 $ subset_closure hx /-- The image under a monoid hom of the submonoid generated by a set equals the submonoid generated by the image of the set. -/ @[to_additive "The image under an `add_monoid` hom of the `add_submonoid` generated by a set equals the `add_submonoid` generated by the image of the set."] lemma map_mclosure (f : M →* N) (s : set M) : (closure s).map f = closure (f '' s) := le_antisymm (map_le_iff_le_comap.2 $ le_trans (closure_mono $ set.subset_preimage_image _ _) (mclosure_preimage_le _ _)) (closure_le.2 $ set.image_subset _ subset_closure) /-- Restriction of a monoid hom to a submonoid of the domain. -/ @[to_additive "Restriction of an add_monoid hom to an `add_submonoid` of the domain."] def mrestrict {N : Type*} [mul_one_class N] (f : M →* N) (S : submonoid M) : S →* N := f.comp S.subtype @[simp, to_additive] lemma mrestrict_apply {N : Type*} [mul_one_class N] (f : M →* N) (x : S) : f.mrestrict S x = f x := rfl /-- Restriction of a monoid hom to a submonoid of the codomain. -/ @[to_additive "Restriction of an `add_monoid` hom to an `add_submonoid` of the codomain."] def cod_mrestrict (f : M →* N) (S : submonoid N) (h : ∀ x, f x ∈ S) : M →* S := { to_fun := λ n, ⟨f n, h n⟩, map_one' := subtype.eq f.map_one, map_mul' := λ x y, subtype.eq (f.map_mul x y) } /-- Restriction of a monoid hom to its range interpreted as a submonoid. -/ @[to_additive "Restriction of an `add_monoid` hom to its range interpreted as a submonoid."] def mrange_restrict {N} [mul_one_class N] (f : M →* N) : M →* f.mrange := f.cod_mrestrict f.mrange $ λ x, ⟨x, rfl⟩ @[simp, to_additive] lemma coe_mrange_restrict {N} [mul_one_class N] (f : M →* N) (x : M) : (f.mrange_restrict x : N) = f x := rfl end monoid_hom namespace submonoid open monoid_hom @[to_additive] lemma mrange_inl : (inl M N).mrange = prod ⊤ ⊥ := by simpa only [mrange_eq_map] using map_inl ⊤ @[to_additive] lemma mrange_inr : (inr M N).mrange = prod ⊥ ⊤ := by simpa only [mrange_eq_map] using map_inr ⊤ @[to_additive] lemma mrange_inl' : (inl M N).mrange = comap (snd M N) ⊥ := mrange_inl.trans (top_prod _) @[to_additive] lemma mrange_inr' : (inr M N).mrange = comap (fst M N) ⊥ := mrange_inr.trans (prod_top _) @[simp, to_additive] lemma mrange_fst : (fst M N).mrange = ⊤ := (fst M N).mrange_top_of_surjective $ @prod.fst_surjective _ _ ⟨1⟩ @[simp, to_additive] lemma mrange_snd : (snd M N).mrange = ⊤ := (snd M N).mrange_top_of_surjective $ @prod.snd_surjective _ _ ⟨1⟩ @[simp, to_additive] lemma mrange_inl_sup_mrange_inr : (inl M N).mrange ⊔ (inr M N).mrange = ⊤ := by simp only [mrange_inl, mrange_inr, prod_bot_sup_bot_prod, top_prod_top] /-- The monoid hom associated to an inclusion of submonoids. -/ @[to_additive "The `add_monoid` hom associated to an inclusion of submonoids."] def inclusion {S T : submonoid M} (h : S ≤ T) : S →* T := S.subtype.cod_mrestrict _ (λ x, h x.2) @[simp, to_additive] lemma range_subtype (s : submonoid M) : s.subtype.mrange = s := set_like.coe_injective $ (coe_mrange _).trans $ subtype.range_coe @[to_additive] lemma eq_top_iff' : S = ⊤ ↔ ∀ x : M, x ∈ S := eq_top_iff.trans ⟨λ h m, h $ mem_top m, λ h m _, h m⟩ @[to_additive] lemma eq_bot_iff_forall : S = ⊥ ↔ ∀ x ∈ S, x = (1 : M) := begin split, { intros h x x_in, rwa [h, mem_bot] at x_in }, { intros h, ext x, rw mem_bot, exact ⟨h x, by { rintros rfl, exact S.one_mem }⟩ }, end @[to_additive] lemma nontrivial_iff_exists_ne_one (S : submonoid M) : nontrivial S ↔ ∃ x ∈ S, x ≠ (1:M) := begin split, { introI h, rcases exists_ne (1 : S) with ⟨⟨h, h_in⟩, h_ne⟩, use [h, h_in], intro hyp, apply h_ne, simpa [hyp] }, { rintros ⟨x, x_in, hx⟩, apply nontrivial_of_ne (⟨x, x_in⟩ : S) 1, intro hyp, apply hx, simpa [has_one.one] using hyp }, end /-- A submonoid is either the trivial submonoid or nontrivial. -/ @[to_additive] lemma bot_or_nontrivial (S : submonoid M) : S = ⊥ ∨ nontrivial S := begin classical, by_cases h : ∀ x ∈ S, x = (1 : M), { left, exact S.eq_bot_iff_forall.mpr h }, { right, push_neg at h, simpa [nontrivial_iff_exists_ne_one] using h }, end /-- A submonoid is either the trivial submonoid or contains a nonzero element. -/ @[to_additive] lemma bot_or_exists_ne_one (S : submonoid M) : S = ⊥ ∨ ∃ x ∈ S, x ≠ (1:M) := begin convert S.bot_or_nontrivial, rw nontrivial_iff_exists_ne_one end end submonoid namespace mul_equiv variables {S} {T : submonoid M} /-- Makes the identity isomorphism from a proof that two submonoids of a multiplicative monoid are equal. -/ @[to_additive "Makes the identity additive isomorphism from a proof two submonoids of an additive monoid are equal."] def submonoid_congr (h : S = T) : S ≃* T := { map_mul' := λ _ _, rfl, ..equiv.set_congr $ congr_arg _ h } end mul_equiv /-! ### Actions by `submonoid`s These instances tranfer the action by an element `m : M` of a monoid `M` written as `m • a` onto the action by an element `s : S` of a submonoid `S : submonoid M` such that `s • a = (s : M) • a`. These instances work particularly well in conjunction with `monoid.to_mul_action`, enabling `s • m` as an alias for `↑s * m`. -/ section actions namespace submonoid variables {M' : Type*} {α β : Type*} [monoid M'] /-- The action by a submonoid is the action by the underlying monoid. -/ @[to_additive /-"The additive action by an add_submonoid is the action by the underlying add_monoid. "-/] instance [mul_action M' α] (S : submonoid M') : mul_action S α := mul_action.comp_hom _ S.subtype @[to_additive] lemma smul_def [mul_action M' α] {S : submonoid M'} (g : S) (m : α) : g • m = (g : M') • m := rfl /-- The action by a submonoid is the action by the underlying monoid. -/ instance [add_monoid α] [distrib_mul_action M' α] (S : submonoid M') : distrib_mul_action S α := distrib_mul_action.comp_hom _ S.subtype @[to_additive] instance smul_comm_class_left [mul_action M' β] [has_scalar α β] [smul_comm_class M' α β] (S : submonoid M') : smul_comm_class S α β := ⟨λ a, (smul_comm (a : M') : _)⟩ @[to_additive] instance smul_comm_class_right [has_scalar α β] [mul_action M' β] [smul_comm_class α M' β] (S : submonoid M') : smul_comm_class α S β := ⟨λ a s, (smul_comm a (s : M') : _)⟩ /-- Note that this provides `is_scalar_tower S M' M'` which is needed by `smul_mul_assoc`. -/ instance [has_scalar α β] [mul_action M' α] [mul_action M' β] [is_scalar_tower M' α β] (S : submonoid M') : is_scalar_tower S α β := ⟨λ a, (smul_assoc (a : M') : _)⟩ example {S : submonoid M'} : is_scalar_tower S M' M' := by apply_instance end submonoid end actions
a9083a79b383759d6282f8baa06a3d5df9ebe96a
ea5678cc400c34ff95b661fa26d15024e27ea8cd
/series1.lean
093d1a790d76c3a56a163ffa054addfb55abd930
[]
no_license
ChrisHughes24/leanstuff
dca0b5349c3ed893e8792ffbd98cbcadaff20411
9efa85f72efaccd1d540385952a6acc18fce8687
refs/heads/master
1,654,883,241,759
1,652,873,885,000
1,652,873,885,000
134,599,537
1
0
null
null
null
null
UTF-8
Lean
false
false
23,927
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 algebra.big_operators data.real.cau_seq tactic.ring algebra.archimedean data.nat.choose analysis.limits .disjoint_finset open nat is_absolute_value finset variables {α : Type*} {β : Type*} (f g : ℕ → α) (n m : ℕ) local infix `^` := monoid.pow section sum_range variable [add_comm_monoid α] lemma sum_range_succ : (range (succ n)).sum f = f n + (range n).sum f := have h : n ∉ finset.range n := by rw finset.mem_range; exact lt_irrefl _, by rw [finset.range_succ, finset.sum_insert h] lemma sum_range_succ' : ∀ n : ℕ, f ∑ succ n = (λ m, f (succ m)) ∑ n + f 0 | 0 := by simp | (succ n) := by rw [sum_range_succ (λ m, f (succ m)), add_assoc, ← sum_range_succ']; exact sum_range_succ _ _ lemma sum_range_comm : f ∑ n = (λ m, f (n - (succ m))) ∑ n := begin induction n with n hi, { simp }, { rw [sum_range_succ, sum_range_succ', hi, succ_sub_one, add_comm], simp [succ_sub_succ] } end lemma sum_range_diag_flip1 (f : ℕ → ℕ → α) : (range n).sum (λ m, (range (m+1)).sum (λ k, f k (m - k))) = (range n).sum (λ m, (range (n - m)).sum (f m)) := let f' : ℕ → ℕ → α := λ m k, if m + k < n then f m k else 0 in calc (range n).sum (λ m, (range (m+1)).sum (λ k, f k (m - k))) = (range n).sum (λ m, (range n).sum (λ k, f' k (m - k))) : sum_congr rfl (λ x hx, begin end) #print nat.lt_sub lemma sum_range_diag_flip2 (f : ℕ → ℕ → α) : (range n).sum (λ m, (range (m+1)).sum (λ k, f k (m - k))) = (range n).sum (λ m, (range (n - m)).sum (f m)) := have h₁ : ((range n).sigma (range ∘ succ)).sum (λ (a : Σ m, ℕ), f (a.2) (a.1 - a.2)) = (range n).sum (λ m, (range (m + 1)).sum (λ k, f k (m - k))) := sum_sigma, have h₂ : ((range n).sigma (λ m, range (n - m))).sum (λ a : Σ (m : ℕ), ℕ, f (a.1) (a.2)) = (range n).sum (λ m, sum (range (n - m)) (f m)) := sum_sigma, h₁ ▸ h₂ ▸ sum_bij (λ a _, ⟨a.2, a.1 - a.2⟩) (λ a ha, have h₁ : a.1 < n := mem_range.1 (mem_sigma.1 ha).1, have h₂ : a.2 < succ a.1 := mem_range.1 (mem_sigma.1 ha).2, mem_sigma.2 ⟨mem_range.2 (lt_of_lt_of_le h₂ h₁), mem_range.2 ((nat.sub_lt_sub_right_iff (le_of_lt_succ h₂)).2 h₁)⟩) (λ _ _, rfl) (λ ⟨a₁, a₂⟩ ⟨b₁, b₂⟩ ha hb h, have ha : a₁ < n ∧ a₂ ≤ a₁ := ⟨mem_range.1 (mem_sigma.1 ha).1, le_of_lt_succ (mem_range.1 (mem_sigma.1 ha).2)⟩, have hb : b₁ < n ∧ b₂ ≤ b₁ := ⟨mem_range.1 (mem_sigma.1 hb).1, le_of_lt_succ (mem_range.1 (mem_sigma.1 hb).2)⟩, have h : a₂ = b₂ ∧ _ := sigma.mk.inj h, have h' : a₁ - a₂ = b₁ - b₂ := eq_of_heq h.2, sigma.mk.inj_iff.2 ⟨by rwa [nat.sub_eq_iff_eq_add ha.2, h.1, nat.sub_add_cancel hb.2] at h', (heq_of_eq h.1)⟩) (λ ⟨a₁, a₂⟩ ha, have ha : a₁ < n ∧ a₂ < n - a₁ := ⟨mem_range.1 (mem_sigma.1 ha).1, (mem_range.1 (mem_sigma.1 ha).2)⟩, ⟨⟨a₂ + a₁, a₁⟩, ⟨mem_sigma.2 ⟨mem_range.2 ((nat.lt_sub_right_iff_add_lt (le_of_lt ha.1)).1 ha.2), mem_range.2 (lt_succ_of_le (le_add_left _ _))⟩, sigma.mk.inj_iff.2 ⟨rfl, heq_of_eq (nat.add_sub_cancel _ _).symm⟩⟩⟩) lemma sum_range_diag_flip (f : ℕ → ℕ → α) : (λ m, (λ k, f k (m - k)) ∑ (succ m)) ∑ n = (λ m, (λ k, f m k) ∑ (n - m)) ∑ n := begin suffices : ∀ j < n, (λ m, (λ k, f k (m - k)) ∑ succ (min j m)) ∑ n = (λ m, (λ k, f m k) ∑ (n - m)) ∑ succ j, { cases n with n, { simp }, { rw ← this n (lt_succ_self _), apply finset.sum_congr rfl, assume m hm, rw finset.mem_range at hm, rw min_eq_right (le_of_lt_succ hm) } }, assume j hj, induction j with j hi, { clear hj, induction n; simp * at * }, { specialize hi (le_of_succ_le hj), rw [sum_range_succ _ (succ j), ← hi], clear hi, induction n with n hi, { exact absurd hj dec_trivial }, { cases lt_or_eq_of_le (le_of_lt_succ hj) with hj' hj', { specialize hi hj', rw [sum_range_succ, sum_range_succ _ n, hi, min_eq_left (le_of_lt hj'), succ_sub (le_of_lt hj'), min_eq_left (le_of_lt (lt_of_succ_lt_succ hj)), sum_range_succ _ (_ - _), sum_range_succ _ (succ j)], simp }, { rw [sum_range_succ, sum_range_succ _ n, ← hj', min_eq_left (le_refl _), min_eq_left (le_succ _), sum_range_succ _ (succ _), succ_sub (le_refl _), nat.sub_self, sum_range_succ _ 0, finset.range_zero, finset.sum_empty, add_zero, ← add_assoc], refine congr_arg _ (finset.sum_congr rfl _), assume m hm, rw finset.mem_range at hm, rw [min_eq_right (le_of_lt hm), min_eq_right (le_of_lt_succ hm)] } } } end end sum_range theorem binomial [comm_semiring α] (x y : α) : ∀ n : ℕ, (x + y)^n = (λ m, x^m * y^(n - m) * choose n m) ∑ succ n | 0 := by simp | (succ n) := begin rw [_root_.pow_succ, binomial, add_mul, finset.mul_sum, finset.mul_sum, sum_range_succ, sum_range_succ', sum_range_succ, sum_range_succ', add_assoc, ← add_assoc (_ ∑ n), ← finset.sum_add_distrib], have h₁ : x * (x^n * y^(n - n) * choose n n) = x^succ n * y^(succ n - succ n) * choose (succ n) (succ n) := by simp [_root_.pow_succ, mul_assoc, mul_comm, mul_left_comm], have h₂ : y * (x^0 * y^(n - 0) * choose n 0) = x^0 * y^(succ n - 0) * choose (succ n) 0 := by simp [_root_.pow_succ, mul_assoc, mul_comm, mul_left_comm], have h₃ : (λ m, x * (x^m * y^(n - m) * choose n m) + y * (x^succ m * y^(n - succ m) * choose n (succ m))) ∑ n = (λ m, x^succ m * y^(succ n - succ m) * ↑(choose (succ n) (succ m))) ∑ n := finset.sum_congr rfl begin assume m hm, rw finset.mem_range at hm, rw [← mul_assoc y, ← mul_assoc y, mul_right_comm y, ← _root_.pow_succ, add_one, ← succ_sub hm], simp [succ_sub_succ, _root_.pow_succ, choose_succ_succ, mul_add, mul_comm, mul_assoc, mul_left_comm] end, rw [h₁, h₂, h₃] end lemma zero_pow [semiring α] {n : ℕ} (hn : 0 < n) : (0 : α)^n = 0 := begin induction n with n hi, { exact absurd hn dec_trivial }, { cases n with n, { simp }, { simp [hi dec_trivial, _root_.pow_succ _ (succ _), zero_mul] } } end lemma pow_abv (x : β) {n : ℕ} (hn : 1 ≤ n) (abv: β → α) [is_absolute_value abv] : abv (x^n) = (abv x)^n := begin induction n with n hi, { exact absurd hn dec_trivial }, { cases n with n, { simp }, { simp [hi dec_trivial, _root_.pow_succ _ (succ _), abv_mul abv] } } end section geo_series open cau_seq variables [discrete_linear_ordered_field α] [ring β] {abv : β → α} [is_absolute_value abv] private lemma lim_zero_pow_lt_one_aux [archimedean α] {x : β} (hx : abv x < 1) : ∀ ε > 0, ∃ i : ℕ, ∀ j ≥ i, abv (x^j) < ε := begin assume ε ε0, cases classical.em (x = 0), { existsi 1, assume j hj, simpa [h, _root_.zero_pow hj, abv_zero abv] }, { have hx : (abv x)⁻¹ > 1 := one_lt_inv ((abv_pos abv).mpr h) hx, cases pow_unbounded_of_gt_one ε⁻¹ hx with i hi, have hax : 0 < abv x := by rwa abv_pos abv, existsi max i 1, assume j hj, rw [pow_abv _ (le_trans (le_max_right _ _) hj), ← inv_lt_inv ε0 (pow_pos hax _), pow_inv _ _ (ne_of_lt hax).symm], exact lt_of_lt_of_le hi (pow_le_pow (le_of_lt hx) (le_trans (le_max_left _ _) hj)) } end lemma is_cau_pow_lt_one [archimedean α] {x : β} (hx : abv x < 1) : is_cau_seq abv (λ n, x^n) := of_near _ (const abv 0) $ by simp only [const_apply, sub_zero]; exact lim_zero_pow_lt_one_aux hx lemma lim_zero_pow_lt_one [archimedean α] (x : β) (hx : abv x < 1) : lim_zero ⟨λ n, x^n, is_cau_pow_lt_one _ hx⟩ := lim_zero_pow_lt_one_aux abv hx lemma sum_geo {x : β} (hx : x ≠ 1) : ∀ n, (λ m, x^m) ∑ n * (1 - x) = 1 - x^n | 0 := by simp | (n + 1) := by rw [sum_range_succ, add_mul, sum_geo]; simp [mul_add, _root_.pow_succ'] lemma is_cau_abv_geo_series [archimedean α] {x : β} (hx : abv x < 1) : is_cau_seq abs (λ n, (λ m, (abv x)^m) ∑ n) := begin have haa : abs (abv x) < 1 := by rwa abs_of_nonneg (abv_nonneg abv _), have hax1 : ¬ 1 - abv x = 0 := by rw [sub_eq_zero_iff_eq]; exact (ne_of_lt hx).symm, have hax1' : ¬ abv x = 1 := by rwa [eq_comm, ← sub_eq_zero_iff_eq], let s : cau_seq α abs := (cau_seq.const abs 1 - ⟨_, is_cau_pow_lt_one abs haa⟩) * cau_seq.inv (cau_seq.const abs (1 - abv x)) (by rwa const_lim_zero), refine (cau_seq.of_eq s _ _).2, assume n, simp [s], rw [← div_eq_mul_inv, div_eq_iff_mul_eq], exact sum_geo hax1' _, assumption, end end geo_series -- -- proof that two different ways of representing a sum across a 2D plane are equal, used -- -- in proof of exp (x + y) = exp x * exp y -- lemma series_series_diag_flip [add_comm_monoid α] (f : ℕ → ℕ → α) (n : ℕ) : series (λ i, -- series (λ k, f k (i - k)) i) n = series (λ i, series (λ k, f i k) (n - i)) n := begin -- have : ∀ m : ℕ, m ≤ n → series (λ (i : ℕ), series (λ k, f k (i - k)) (min m i)) n = -- series (λ i, series (λ k, f i k) (n - i)) m := by -- { assume m mn, induction m with m' hi, -- simp[series_succ,series_zero,mul_add,max_eq_left (zero_le n)], -- simp only [series_succ _ m'],rw ←hi (le_of_succ_le mn),clear hi, -- induction n with n' hi, -- simp[series_succ],exact absurd mn dec_trivial,cases n' with n₂, -- simp [series_succ],rw [min_eq_left mn,series_succ,min_eq_left (le_of_succ_le mn)], -- rw eq_zero_of_le_zero (le_of_succ_le_succ mn),simp, -- cases lt_or_eq_of_le mn, -- simp [series_succ _ (succ n₂),min_eq_left mn,hi (le_of_lt_succ h)],rw [←add_assoc,←add_assoc], -- suffices : series (f (succ m')) (n₂ - m') + series (λ (k : ℕ), f k (succ (succ n₂) - k)) (succ m') -- = series (f (succ m')) (succ n₂ - m') + -- series (λ (k : ℕ), f k (succ (succ n₂) - k)) (min m' (succ (succ n₂))), -- rw this,rw[min_eq_left (le_of_succ_le mn),series_succ,succ_sub_succ,succ_sub (le_of_succ_le_succ (le_of_lt_succ h)),series_succ], -- rw [add_comm (series (λ (k : ℕ), f k (succ (succ n₂) - k)) m'),add_assoc], -- rw ←h,simp[nat.sub_self],clear hi mn h,simp[series_succ,nat.sub_self], -- suffices : series (λ (i : ℕ), series (λ (k : ℕ), f k (i - k)) (min (succ m') i)) m' = series (λ (i : ℕ), series (λ (k : ℕ), f k (i - k)) (min m' i)) m', -- rw [this,min_eq_left (le_succ _)],clear n₂, -- have h₁ : ∀ i ≤ m', (λ (i : ℕ), series (λ (k : ℕ), f k (i - k)) (min (succ m') i)) i = (λ (i : ℕ), series (λ (k : ℕ), f k (i - k)) (min m' i)) i, -- assume i im,simp, rw [min_eq_right im,min_eq_right (le_succ_of_le im)], -- rw series_congr h₁}, -- specialize this n (le_refl _), -- rw ←this,refine series_congr _,assume i ni,rw min_eq_right ni, -- end -- open monoid -- theorem series_binomial {α : Type*} [comm_semiring α] (x y : α) (i : ℕ) : pow (x + y) i = -- series (λ j, choose i j * pow x j * pow y (i - j)) i := begin -- induction i with i' hi, -- simp!,unfold monoid.pow,rw hi, -- rw [←series_mul_left], -- have : ∀ j : ℕ, j ≤ i' → (λ (i : ℕ), (x + y) * (↑(choose i' i) * pow x i * pow y (i' - i))) j -- = choose i' j * pow x (succ j) * pow y (i' - j) + choose i' j * pow x j * pow y (succ i' - j) := by -- { assume j ji,dsimp only,rw add_mul, -- have : x * (↑(choose i' j) * pow x j * pow y (i' - j)) + y * (↑(choose i' j) * pow x j * pow y (i' - j)) -- = ↑(choose i' j) * (x * pow x j) * pow y (i' - j) + ↑(choose i' j) * pow x j * (y * pow y (i' - j)), -- simp[mul_comm,_root_.mul_assoc,mul_left_comm], -- rw [this,←_root_.pow_succ,←_root_.pow_succ,succ_sub ji]}, -- rw [series_congr this],clear this, -- clear hi,rw series_add, -- have : series (λ (i : ℕ), ↑(choose i' i) * pow x i * pow y (succ i' - i)) i' = -- series (λ (i : ℕ), ↑(choose i' i) * pow x i * pow y (succ i' - i)) (succ i') := by -- { simp[series_succ],}, -- rw [this,series_succ₁,series_succ₁], -- simp[nat.sub_self],rw ←series_add, -- refine congr_arg _ (series_congr _), -- assume j ji,unfold choose,rw [nat.cast_add,add_mul,add_mul], -- end -- lemma geo_series_eq {α : Type*} [field α] (x : α) (n : ℕ) : x ≠ 1 → series (pow x) n = (1 - pow x (succ n)) / (1 - x) := begin -- assume x1,have x1' : 1 + -x ≠ 0,assume h,rw [eq_comm, ←sub_eq_iff_eq_add] at h,simp at h,trivial, -- induction n with n' hi, -- {simp![div_self x1']}, -- {rw eq_div_iff_mul_eq,simpa, -- rw [_root_.series_succ,_root_.pow_succ _ (succ n')], -- rw hi,simp,rw [add_mul,div_mul_cancel _ x1',mul_add],ring,exact x1'}, -- end -- lemma is_cau_geo_series {α : Type*} [discrete_linear_ordered_field α] [archimedean α] (x : α) : -- abs x < 1 → is_cau_seq abs (series (pow x)) := begin -- assume x1, have : series (pow x) = λ n,(1 - pow x (succ n)) / (1 - x), -- apply funext,assume n,refine geo_series_eq x n _ ,assume h, rw h at x1,exact absurd x1 (by norm_num),rw this, -- have absx : 0 < abs (1 - x),refine abs_pos_of_ne_zero _,assume h,rw sub_eq_zero_iff_eq at h,rw ←h at x1, -- have : ¬abs (1 : α) < 1,norm_num,trivial,simp at absx, -- cases classical.em (x = 0),rw h,simp[monoid.pow],assume ε ε0,existsi 1,assume j j1,simpa!, -- have x2: 1 < (abs x)⁻¹,rw lt_inv,simpa,{norm_num},exact abs_pos_of_ne_zero h, -- have pos_x : 0 < abs x := abs_pos_of_ne_zero h, -- assume ε ε0, -- cases pow_unbounded_of_gt_one (2 / (ε * abs (1 - x))) x2 with i hi, -- have ε2 : 0 < 2 / (ε * abs (1 - x)) := div_pos (by norm_num) (mul_pos ε0 absx), -- rw [pow_inv,lt_inv ε2 (pow_pos pos_x _)] at hi, -- existsi i,assume j ji,rw [inv_eq_one_div,div_div_eq_mul_div,_root_.one_mul,lt_div_iff (by norm_num : (0 : α) < 2)] at hi, -- rw [div_sub_div_same,abs_div,div_lt_iff absx], -- refine lt_of_le_of_lt _ hi, -- simp, -- refine le_trans (abs_add _ _) _, -- have : pow (abs x) i * 2 = pow (abs x) i + pow (abs x) i,ring, -- rw this, -- refine add_le_add _ _, -- {rw [←_root_.one_mul (pow (abs x) i),pow_abs,_root_.pow_succ,abs_mul], -- exact mul_le_mul_of_nonneg_right (le_of_lt x1) (abs_nonneg _)}, -- {rw [abs_neg,←pow_abs], -- rw [←inv_le_inv (pow_pos pos_x _) (pow_pos pos_x _),←pow_inv,←pow_inv], -- refine pow_le_pow (le_of_lt x2) (le_succ_of_le ji),} -- end -- lemma is_cau_geo_series_const {α : Type*} [discrete_linear_ordered_field α] [archimedean α] (a x : α) : -- abs x < 1 → is_cau_seq abs (series (λ n, a * pow x n)) := begin -- assume x1 ε ε0, -- cases classical.em (a = 0), -- existsi 0,intros,rw [series_mul_left],induction j,simp!,assumption,rw h,simpa!, -- cases is_cau_geo_series x x1 (ε / abs a) (div_pos ε0 (abs_pos_of_ne_zero h)) with i hi, -- existsi i,assume j ji,rw [series_mul_left,series_mul_left,←mul_sub,abs_mul,mul_comm,←lt_div_iff], -- exact hi j ji,exact abs_pos_of_ne_zero h, -- end -- lemma is_cau_series_of_abv_le_cau {α : Type*} {β : Type*} [discrete_linear_ordered_field α] [ring β] {f : ℕ → β} -- {g : ℕ → α} {abv : β → α} [is_absolute_value abv] (n : ℕ) : (∀ m, n ≤ m → abv (f m) ≤ g m) → -- is_cau_seq abs (series g) → is_cau_seq abv (series f) := begin -- assume hm hg ε ε0,cases hg (ε / 2) (div_pos ε0 (by norm_num)) with i hi, -- existsi max n i, -- assume j ji, -- have hi₁ := hi j (le_trans (le_max_right n i) ji), -- have hi₂ := hi (max n i) (le_max_right n i), -- have sub_le := abs_sub_le (series g j) (series g i) (series g (max n i)), -- have := add_lt_add hi₁ hi₂,rw abs_sub (series g (max n i)) at this, -- have ε2 : ε / 2 + ε / 2 = ε,ring, -- rw ε2 at this, -- refine lt_of_le_of_lt _ this, -- refine le_trans _ sub_le, -- refine le_trans _ (le_abs_self _), -- generalize hk : j - max n i = k,clear this ε2 hi₂ hi₁ hi ε0 ε hg sub_le, -- rw nat.sub_eq_iff_eq_add ji at hk,rw hk, clear hk ji j, -- induction k with k' hi,simp,rw abv_zero abv, -- rw succ_add,unfold series, -- rw [add_comm,add_sub_assoc], -- refine le_trans (abv_add _ _ _) _, -- rw [add_comm (series g (k' + max n i)),add_sub_assoc], -- refine add_le_add _ _, -- refine hm _ _,rw [←zero_add n,←succ_add],refine add_le_add _ _,exact zero_le _, -- simp, exact le_max_left _ _,assumption, -- end -- -- The form of ratio test with 0 ≤ r < 1, and abv (f (succ m)) ≤ r * abv (f m) handled zero terms of series the best -- lemma series_ratio_test {α : Type*} {β : Type*} [discrete_linear_ordered_field α] [ring β] -- [archimedean α] {abv : β → α} [is_absolute_value abv] {f : ℕ → β} (n : ℕ) (r : α) : -- 0 ≤ r → r < 1 → (∀ m, n ≤ m → abv (f (succ m)) ≤ r * abv (f m)) → is_cau_seq abv (series f) -- := begin -- assume r0 r1 h, -- refine is_cau_series_of_abv_le_cau (succ n) _ (is_cau_geo_series_const (abv (f (succ n)) * pow r⁻¹ (succ n)) r _), -- assume m mn, -- generalize hk : m - (succ n) = k,rw nat.sub_eq_iff_eq_add mn at hk, -- cases classical.em (r = 0) with r_zero r_pos,have m_pos := lt_of_lt_of_le (succ_pos n) mn, -- have := pred_le_pred mn,simp at this, -- have := h (pred m) this,simp[r_zero,succ_pred_eq_of_pos m_pos] at this, -- refine le_trans this _,refine mul_nonneg _ _,refine mul_nonneg (abv_nonneg _ _) (pow_nonneg (inv_nonneg.mpr r0) _),exact pow_nonneg r0 _, -- replace r_pos : 0 < r,cases lt_or_eq_of_le r0 with h h,exact h,exact absurd h.symm r_pos, -- revert m n, -- induction k with k' hi,assume m n h mn hk, -- rw [hk,zero_add,mul_right_comm,←pow_inv _ _ (ne_of_lt r_pos).symm,←div_eq_mul_inv,mul_div_cancel], -- exact (ne_of_lt (pow_pos r_pos _)).symm, -- assume m n h mn hk,rw [hk,succ_add], -- have kn : k' + (succ n) ≥ (succ n), rw ←zero_add (succ n),refine add_le_add _ _,exact zero_le _,simp, -- replace hi := hi (k' + (succ n)) n h kn rfl, -- rw [(by simp!;ring : pow r (succ (k' + succ n)) = pow r (k' + succ n) * r),←_root_.mul_assoc], -- replace h := h (k' + succ n) (le_of_succ_le kn),rw mul_comm at h, -- exact le_trans h (mul_le_mul_of_nonneg_right hi r0), -- rwa abs_of_nonneg r0, -- end -- lemma series_cau_of_abv_cau {α : Type*} {β : Type*} [discrete_linear_ordered_field α] [ring β] {abv : β → α} {f : ℕ → β} -- [is_absolute_value abv] : is_cau_seq abs (series (λ n, abv (f n))) → is_cau_seq abv (series f) := -- λ h, is_cau_series_of_abv_le_cau 0 (λ n h, le_refl _) h -- -- I did not use the equivalent function on cauchy sequences as I do not have a proof -- -- series (λ n, series (λ m, a m * b (n - m)) n) j) is a cauchy sequence. Using this lemma -- -- and of_near, this can be proven to be a cauchy sequence -- lemma series_cauchy_prod {α β : Type*} [discrete_linear_ordered_field α] [ring β] {a b : ℕ → β} -- {abv : β → α} [is_absolute_value abv] : is_cau_seq abs (series (λ n, abv (a n))) → is_cau_seq abv (series b) → -- ∀ ε : α, 0 < ε → ∃ i : ℕ, ∀ j ≥ i, abv (series a j * series b j - series (λ n, -- series (λ m, a m * b (n - m)) n) j) < ε := begin -- -- slightly adapted version of theorem 9.4.7 from "The Real Numbers and Real Analysis", Ethan D. Bloch -- assume ha hb ε ε0, -- cases cau_seq.bounded ⟨_, hb⟩ with Q hQ,simp at hQ, -- cases cau_seq.bounded ⟨_, ha⟩ with P hP,simp at hP, -- have P0 : 0 < P,exact lt_of_le_of_lt (abs_nonneg _) (hP 0), -- have Pε0 := div_pos ε0 (mul_pos (show (2 : α) > 0, from by norm_num) P0), -- cases cau_seq.cauchy₂ ⟨_, hb⟩ Pε0 with N hN,simp at hN, -- have Qε0 := div_pos ε0 (mul_pos (show (4 : α) > 0, from by norm_num) (lt_of_le_of_lt (abv_nonneg _ _) (hQ 0))), -- cases cau_seq.cauchy₂ ⟨_, ha⟩ Qε0 with M hM,simp at hM, -- existsi 2 * (max N M + 1), -- assume K hK,have := series_series_diag_flip (λ m n, a m * b n) K,simp at this,rw this,clear this, -- have : (λ (i : ℕ), series (λ (k : ℕ), a i * b k) (K - i)) = (λ (i : ℕ), a i * series (λ (k : ℕ), b k) (K - i)) := by -- {apply funext,assume i,rw series_mul_left}, -- rw this,clear this,simp, -- have : series (λ (i : ℕ), a i * series b (K - i)) K = series (λ (i : ℕ), a i * (series b (K - i) - series b K)) -- K + series (λ i, a i * series b K) K, -- {rw ←series_add,simp[(mul_add _ _ _).symm]}, -- rw this, clear this, -- rw series_mul_series,simp, -- rw abv_neg abv, -- refine lt_of_le_of_lt (abv_series_le_series_abv _) _, -- simp [abv_mul abv], -- suffices : series (λ (i : ℕ), abv (a i) * abv (series b (K - i) + -series b K)) (max N M + 1) + -- (series (λ (i : ℕ), abv (a i) * abv (series b (K - i) + -series b K)) K -series (λ (i : ℕ), -- abv (a i) * abv (series b (K - i) + -series b K)) (max N M + 1)) < ε / (2 * P) * P + ε / (4 * Q) * (2 * Q), -- { simp [(div_div_eq_div_mul _ _ _).symm] at this, -- rwa[div_mul_cancel _ (ne_of_lt P0).symm,(by norm_num : (4 : α) = 2 * 2),←div_div_eq_div_mul,mul_comm (2 : α),←_root_.mul_assoc, -- div_mul_cancel _ (ne_of_lt (lt_of_le_of_lt (abv_nonneg _ _) (hQ 0))).symm,div_mul_cancel,add_halves] at this, -- norm_num}, -- refine add_lt_add _ _, -- {have : series (λ (i : ℕ), abv (a i) * abv (series b (K - i) + -series b K)) (max N M + 1) ≤ series -- (λ (i : ℕ), abv (a i) * (ε / (2 * P))) (max N M + 1) := by -- {refine series_le_series _,assume m mJ,refine mul_le_mul_of_nonneg_left _ _, -- {refine le_of_lt (hN (K - m) K _ _),{ -- refine nat.le_sub_left_of_add_le (le_trans _ hK), -- rw[succ_mul,_root_.one_mul], -- exact add_le_add mJ (le_trans (le_max_left _ _) (le_of_lt (lt_add_one _)))}, -- {refine le_trans _ hK,rw ←_root_.one_mul N, -- refine mul_le_mul (by norm_num) (by rw _root_.one_mul;exact le_trans (le_max_left _ _) -- (le_of_lt (lt_add_one _))) (zero_le _) (zero_le _)}}, -- exact abv_nonneg abv _}, -- refine lt_of_le_of_lt this _, -- rw [series_mul_right,mul_comm], -- specialize hP (max N M + 1),rwa abs_of_nonneg at hP, -- exact (mul_lt_mul_left Pε0).mpr hP, -- exact series_nonneg (λ x h, abv_nonneg abv _)}, -- {have hNMK : max N M + 1 < K := by -- {refine lt_of_lt_of_le _ hK, -- rw [succ_mul,_root_.one_mul,←add_zero (max N M + 1)], -- refine add_lt_add_of_le_of_lt (le_refl _) _,rw add_zero, -- refine add_pos_of_nonneg_of_pos (zero_le _) (by norm_num)}, -- rw series_sub_series _ hNMK, -- have : series (λ (k : ℕ), abv (a (k + (max N M + 1 + 1))) * abv -- (series b (K - (k + (max N M + 1 + 1))) + -series b K)) (K - (max N M + 1 + 1)) ≤ -- series (λ (k : ℕ), abv (a (k + (max N M + 1 + 1))) * (2 * Q)) (K - (max N M + 1 + 1)) := by -- {refine series_le_series _, -- assume m hm, -- refine mul_le_mul_of_nonneg_left _ _, -- {refine le_trans (abv_add abv _ _) _, -- rw (by ring : 2 * Q = Q + Q), -- refine add_le_add (le_of_lt (hQ _)) _, -- rw abv_neg abv, exact le_of_lt (hQ _)}, -- exact abv_nonneg abv _}, -- refine lt_of_le_of_lt this _, -- rw [series_mul_right], -- refine (mul_lt_mul_right (mul_pos (by norm_num) (lt_of_le_of_lt (abv_nonneg abv _) (hQ 0)))).mpr _, -- refine lt_of_le_of_lt (le_abs_self _) _, -- rw[←@series_sub_series _ _ (λ k, abv (a k)) (max N M + 1) K hNMK], -- refine hM _ _ _ (le_trans (le_max_right _ _) (le_of_lt (lt_add_one _))), -- refine le_trans _ hK, -- rw [succ_mul,_root_.one_mul,←add_zero M], -- exact add_le_add (le_trans (le_max_right _ _) (le_of_lt (lt_add_one _))) (zero_le _)}, -- end